-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlex.l
536 lines (445 loc) · 9.15 KB
/
lex.l
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
/* Hey, Emacs, this is not -*- C -*-, but it should look like as it were...
*
* ADSP 2181 Assembler -- Lexical Analysis
*
* (c) 1996 Martin Mares, <mj@k332.feld.cvut.cz>
*
* This software may be freely distributed and used according to the terms
* of the GNU General Public License. See file COPYING in any of the GNU utilities.
*/
%option nounput
%option noinput
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/wait.h>
#include "as2181.h"
#include "syn.tab.h"
#define YY_NO_UNPUT
/* Internal functions */
static void map_lines(ulg, char *);
static void upcase_it(char *);
/* Scanner input */
FILE *infile;
#define yyin infile
ulg lino; /* Line number */
int err_max = 10; /* Maximal number of errors before stopping */
%}
%x PCOMM CCOMM
ALPHA [a-zA-Z_]
DIGIT [0-9]
ODIGIT [0-7]
HDIGIT [0-9a-fA-F]
ALNUM [0-9a-zA-Z_@]
WHITE [ \t]
%%
0{ODIGIT}+ { /* Numbers */
yylval.i = strtoul(yytext+1, NULL, 8);
return NUM;
}
0x{HDIGIT}+ {
yylval.i = strtoul(yytext+2, NULL, 16);
return NUM;
}
{DIGIT}+ {
yylval.i = strtoul(yytext, NULL, 10);
return NUM;
}
"H#"{HDIGIT}+ {
yylval.i = strtoul(yytext+2, NULL, 16);
return NUM;
}
"B#"[01]+ {
yylval.i = strtoul(yytext+2, NULL, 2);
return NUM;
}
"(SS)" { yylval.i = MC_SS; return MACCLASS; } /* Classes */
"(SU)" { yylval.i = MC_SU; return MACCLASS; }
"(US)" { yylval.i = MC_US; return MACCLASS; }
"(UU)" { yylval.i = MC_UU; return MACCLASS; }
"(RND)" { yylval.i = MC_RND; return MACCLASS; }
"(HI)" { yylval.i = SC_HI; return SHCLASS; }
"(LO)" { yylval.i = SC_LO; return SHCLASS; }
"(HIX)" { yylval.i = SC_HIX; return SHCLASS; }
"MR" { return MR; }
"MF" { return MF; }
"SR" { return SR; }
"<<" return LL; /* Operators */
">>" return RR;
[-+*()/\[\],;:=%^] return yytext[0];
'.' { /* Character constants */
yylval.i = yytext[1];
return NUM;
}
{ALPHA}{ALNUM}* { /* Symbols */
const struct as_kwd *k;
char buf[64];
if (yyleng > 63)
{
cerr("Symbol too long");
yytext[63] = 0;
yyleng = 63;
}
upcase_it(buf);
k = is_kwd(buf, yyleng);
if (k)
{
yylval.i = k->value;
return k->class;
}
yylval.s = find_symbol(case_insensitive ? buf : yytext, yyleng, thismod);
return SYM;
}
"."{ALNUM}+ { /* Directives */
const struct as_kwd *k;
upcase_it(yytext);
k = is_kwd(yytext, yyleng);
if (k)
{
yylval.i = k->value;
return k->class;
}
err("Invalid compiler directive");
}
<INITIAL><<EOF>> { /* End of file */
return GOT_EOF;
}
"# ".*\n { /* CPP line info */
char *c = yytext + 2;
char *d = c;
char *e = yytext + yyleng - 2;
ulg xxx;
while (*d >= '0' && *d <= '9')
d++;
while (c != e && *e != '"')
e--;
if (c == d || *d != ' ' || d[1] != '"' || *e != '"')
err("Invalid CPP file / line number information");
*e = *d = 0;
xxx = strtoul(c, NULL, 10);
map_lines(xxx, d+2);
}
<*>\n lino++;
"{" BEGIN(PCOMM); /* Comments */
<PCOMM>"}" BEGIN(INITIAL);
<PCOMM>"{" warn("'{' in comment");
"/*" BEGIN(CCOMM);
<CCOMM>"*/" BEGIN(INITIAL);
<CCOMM>"/*" warn("/* in comment");
<PCOMM,CCOMM>. ;
<PCOMM,CCOMM><<EOF>> err("Non-terminated comment");
{WHITE}+ ; /* Eat up white space */
. err("Invalid character (`%c')", yytext[0]); /* "Catch all" rule */
%%
/* LEX Wrapper Function */
int
yywrap(void)
{
return 1;
}
/* Line lookup */
struct line_info {
struct line_info *next;
ulg abs, rel;
char filename[1];
};
static struct line_info *first_li;
static struct line_info **last_li = &first_li;
static void
map_lines(ulg rel, char *name)
{
struct line_info *l = xmalloc(sizeof(struct line_info) + strlen(name));
*last_li = l;
last_li = &l->next;
l->next = NULL;
l->abs = lino;
l->rel = rel;
strcpy(l->filename, name);
}
ulg
lookup_line(ulg abs, char **name)
{
struct line_info *l;
static struct line_info *cache = NULL;
if (cache && cache->abs <= abs)
l = cache;
else
l = first_li;
if (!l)
fatal_error("Corrupted line list");
while (l->next && l->next->abs <= abs)
l = l->next;
*name = l->filename;
cache = l;
return abs - l->abs + l->rel;
}
/* Error messages */
static int err_count, warn_count;
static void
repnum(char *form, int count)
{
fprintf(stderr, form, count, (count == 1) ? "" : "s");
}
void
report_warns(void)
{
if (warn_count)
repnum("%d warning%s detected.\n", warn_count);
}
static void errhalt(void) NONRET;
static void
errhalt(void)
{
if (warn_count)
repnum("%d warning%s, ", warn_count);
repnum("%d error%s detected.\n", err_count);
exit(1);
}
void
errstop(void)
{
if (err_count)
errhalt();
}
static void
vmsg(char *msg, va_list args, char *start, char *finish, ulg line)
{
char *fn;
if (line == ~0)
fprintf(stderr, "%s: ", start);
else
{
line = lookup_line(line, &fn);
fprintf(stderr, "%s in file %s, line %d: ", start, fn, line);
}
vfprintf(stderr, msg, args);
fputs(finish, stderr);
}
static void
errcheck(void)
{
if (++err_count >= err_max)
{
if (err_max > 1)
fprintf(stderr, "That makes %d errors, try again...\n", err_count);
errstop();
}
}
static char emsg[] = "Error", wmsg[] = "Warning", imsg[] = "Internal error",
etrail[] = "!\n", wtrail[] = ".\n", nmsg[] = "Note";
void
err(char *msg, ...)
{
va_list args;
va_start(args, msg);
vmsg(msg, args, emsg, etrail, lino);
errcheck();
errhalt();
}
void
cerr(char *msg, ...)
{
va_list args;
va_start(args, msg);
vmsg(msg, args, emsg, etrail, lino);
errcheck();
va_end(args);
}
void
ierr(char *msg, ...)
{
va_list args;
va_start(args, msg);
vmsg(msg, args, imsg, etrail, lino);
errcheck();
errhalt();
}
void
warn(char *msg, ...)
{
va_list args;
va_start(args, msg);
if (!hide_warns)
{
vmsg(msg, args, wmsg, wtrail, lino);
warn_count++;
}
va_end(args);
}
void
lerr(ulg li, char *msg, ...)
{
va_list args;
va_start(args, msg);
vmsg(msg, args, emsg, etrail, li);
errcheck();
errhalt();
}
void
clerr(ulg li, char *msg, ...)
{
va_list args;
va_start(args, msg);
vmsg(msg, args, emsg, etrail, li);
errcheck();
va_end(args);
}
void
ilerr(ulg li, char *msg, ...)
{
va_list args;
va_start(args, msg);
vmsg(msg, args, imsg, etrail, li);
errcheck();
errhalt();
}
void
lwarn(ulg li, char *msg, ...)
{
va_list args;
va_start(args, msg);
if (!hide_warns)
{
vmsg(msg, args, wmsg, wtrail, li);
warn_count++;
}
va_end(args);
}
void
lnote(ulg li, char *msg, ...)
{
va_list args;
va_start(args, msg);
vmsg(msg, args, nmsg, wtrail, li);
va_end(args);
}
char *
syserr(void)
{
return strerror(errno);
}
/* Symbols */
#define HASH_SIZE 1024
static struct symbol *hash[HASH_SIZE];
ulg
hash_value(char *c, ulg len)
{
ulg h;
char *d;
h = len;
for(d=c; *d; d++)
h = 13*h + *d;
h = h % HASH_SIZE;
return h;
}
struct symbol *
find_symbol(char *c, ulg len, struct module *context)
{
struct symbol *s, *b;
ulg h = hash_value(c, len);
b = NULL;
for(s=hash[h]; s; s=s->next)
if (!strcmp(s->name, c) && (!s->scope || s->scope == context) && (!b || b->scope))
b = s;
if (b)
return b;
s = xmalloc(sizeof(struct symbol) + len);
strcpy(s->name, c);
s->next = hash[h];
s->value = NULL;
s->scope = context;
hash[h] = s;
return s;
}
void
expose_symbol(struct symbol *s)
{
ulg h;
struct symbol *k;
ulg coll = 0;
h = hash_value(s->name, strlen(s->name));
for(k=hash[h]; k; k=k->next)
if (!strcmp(k->name, s->name) && k != s)
{
if (!k->scope)
{
coll++;
cerr("`%s' already defined as public");
}
else if (k->value)
{
if (!coll++)
cerr("`%s' already defined elsewhere", s->name);
lnote(LINE(k->value), "See this line for previous definition");
}
else
{
struct symref *m = NEW_NODE(symref, N_SYMREF);
m->sym = s;
k->value = NODE m;
}
}
if (!coll)
s->scope = NULL;
}
/* Init */
void
init_lex(char *src, int preprocess)
{
lino = ~0;
if (!preprocess)
{
infile = fopen(src, "r");
if (!infile)
err("Unable to open `%s': %s", src, syserr());
}
else
{
pid_t pid;
int fh;
int pip[2];
fh = open(src, O_RDONLY); /* Test if the file exists */
if (fh < 0)
err("Unable to open `%s': %s", src, syserr());
close(fh);
if (pipe(pip) < 0)
err("Unable to create pipe: %s", syserr());
pid = fork();
if (pid < 0)
err("Fork failed: %s", src, syserr());
if (!pid) /* child CPP process */
{
dup2(pip[1], STDOUT_FILENO); /* duplicate pipe write end into stdout */
close(pip[0]);
close(pip[1]);
/* Call cpp and direct output to pipe */
execlp("cpp", "cpp",
"-Wall", "-nostdinc", "-undef", "-DADSP2181",
"-traditional-cpp", src, NULL);
err("Cannot execute preprocessor: %s", syserr());
}
close(pip[1]); /* close write end of the pipe */
wait(NULL); /* wait for preprocessor to finish */
infile = fdopen(pip[0], "r"); /* open preprocessed buffer for reading */
if (!infile)
err("Error opening cpp pipe: %s", syserr());
}
lino = 0;
map_lines(1, src);
}
static void
upcase_it(char *d)
{
char *c;
for(c=yytext; *c; c++)
if (*c >= 'a' && *c <= 'z')
*d++ = *c - 0x20;
else
*d++ = *c;
*d = 0;
}