-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathdecl.c
798 lines (737 loc) · 17.9 KB
/
decl.c
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
/*
* NMH's Simple C Compiler, 2011--2014
* Declaration parser
*/
#include "defs.h"
#include "data.h"
#include "decl.h"
static int declarator(int arg, int scls, char *name, int *pprim, int *psize,
int *pval, int *pinit);
/*
* enumdecl := { enumlist } ;
*
* enumlist :=
* enumerator
* | enumerator , enumlist
*
* enumerator :=
* IDENT
* | IDENT = constexpr
*/
// Parse an enum declaration. If glob is true, this
// is a global declaration.
static void enumdecl(int glob) {
// Start the first enumerator as value zero
int v = 0;
char name[NAMELEN+1];
// Get the next token. Skip it if it's an identifier
Token = scan();
if (IDENT == Token)
Token = scan();
// Ensure we have a following '{'.
// Then loop until we get the end '}'
lbrace();
while (RBRACE != Token) {
// Copy the enumerator name and ensure
// it's a valid identifier
copyname(name, Text);
ident();
// If this is followed by an '=',
// scan the following token(s), verify
// this is a constant and get its value
if (ASSIGN == Token) {
Token = scan();
v = constexpr();
}
// Add the enumerator name and value
// to the relevant symbol table
if (glob)
addglob(name, PINT, TCONSTANT, 0, 0, v++, NULL, 0);
else
addloc(name, PINT, TCONSTANT, 0, 0, v++, 0);
// Look back if next token is a ',', else stop now.
// Also deal with end of files
if (Token != COMMA)
break;
Token = scan();
if (eofcheck()) return;
}
// The definition must end with a '}' ';'
rbrace();
semi();
}
/*
* initlist :=
* { const_list }
* | STRLIT
*
* const_list :=
* constexpr
* | constexpr , const_list
*/
// Parse either a string literal or
// a list of constant expressions
static int initlist(char *name, int prim) {
int n = 0, v;
char buf[30]; // Used to sprintf out the value
// Move to the data section
gendata();
// Generate a label for the name
genname(name);
// If it's a string literal, an error if char isn't the
// primitive type.
if (STRLIT == Token) {
if (PCHAR != prim)
error("initializer type mismatch: %s", name);
// Generate the byte values after the name's label
// ending with a NUL character. Align on a word boundary.
gendefs(Text, Value);
gendefb(0);
genalign(Value-1);
// Scan in the next token and return the literal's length
Token = scan();
return Value-1; // XXX: Why -1?
}
// Not a string literal, so scan the '{' token
// Until we get a '}' token ...
lbrace();
while (Token != RBRACE) {
// Parse the constant expression
v = constexpr();
// Error check the range if it should be of 'char' type
if (PCHAR == prim) {
if (v < 0 || v > 255) {
sprintf(buf, "%d", v);
error("initializer out of range: %s", buf);
}
// Generate a byte of data, or a word if an 'int'
gendefb(v);
}
else {
gendefw(v);
}
// Increment the number of literals found
n++;
// Skip past any commas and loop back
// (doing an EOF check), or leave loop
// if no commas
if (COMMA == Token)
Token = scan();
else
break;
if (eofcheck()) return 0;
}
// Align the final storage if we were storing 'char's.
// Scan in the next token. Error if nothing in the '{ .. }'
// Return the number of literals read in.
if (PCHAR == prim) genalign(n);
Token = scan();
if (!n) error("too few initializers", NULL);
return n;
}
// Given a token t, convert it into a primitive type.
// If it's a struct/union, s holds the name of this.
// Return the type value.
int primtype(int t, char *s) {
int p, y;
char sname[NAMELEN+1];
// Convert from token value to type value
p = t == CHAR? PCHAR:
t == INT? PINT:
t == STRUCT? PSTRUCT:
t == UNION? PUNION:
PVOID;
// If a struct or union and no name given,
// scan the next token to get it. Check this
// is an IDENT token.
if (PUNION == p || PSTRUCT == p) {
if (!s) {
Token = scan();
copyname(sname, Text);
s = sname;
if (IDENT != Token) {
error("struct/union name expected: %s", Text);
return p;
}
}
// Now check that this struct/union exists
if ((y = findstruct(s)) == 0 || Syms[y].prim != p)
error("no such struct/union: %s", s);
// Add in the struct/union bitfield to the primitive type
p |= y;
}
return p;
}
/*
* pmtrdecl :=
* ( )
* | ( pmtrlist )
* | ( pmtrlist , ... )
*
* pmtrlist :=
* primtype declarator
* | primtype declarator , pmtrlist
*/
// Parse the declaration of a function's parameters
static int pmtrdecls(void) {
char name[NAMELEN+1];
int prim, type, size, na, addr;
int dummy;
// We've already parsed the '('.
// Do nothing if we get a ')' immediately.
if (RPAREN == Token)
return 0;
// No arguments at present
na = 0;
addr = 2*BPW; // XXX Why?
// Loop until we find the ')'
for (;;) {
// XXX: binary invert the number of arguments
// if we see a '...' token. Why?
if (na > 0 && ELLIPSIS == Token) {
Token = scan();
na = -(na + 1);
break;
}
// Parameter is an identifier, mark it as integer type
else if (IDENT == Token) {
prim = PINT;
}
// Otherwise it has to be a type specifier
else {
if ( Token != CHAR && Token != INT &&
Token != VOID &&
Token != STRUCT && Token != UNION
) {
// But it wasn't, so issue an error
// and try to synchronise at the next ';'
error("type specifier expected at: %s", Text);
Token = synch(RPAREN);
return na;
}
// It was a type specifier. Get its primitive type
// and move up to the next token. Return when we
// see a ')' and we had some arguments
name[0] = 0;
prim = primtype(Token, NULL);
Token = scan();
if (RPAREN == Token && prim == PVOID && !na)
return 0;
}
// XXX: Not sure what's going on below yet
size = 1;
type = declarator(1, CAUTO, name, &prim, &size, &dummy,
&dummy);
addloc(name, prim, type, CAUTO, size, addr, 0);
addr += BPW;
// We have one more argument, scan past the following ','
na++;
if (COMMA == Token)
Token = scan();
else
break;
}
return na;
}
// Given a primitive type value, return
// the type that is a pointer to it.
// Ensure we don't have too many levels
// of indirection. At worst, return a void pointer.
int pointerto(int prim) {
int y;
if (CHARPP == prim || INTPP == prim || VOIDPP == prim ||
FUNPTR == prim ||
(prim & STCMASK) == STCPP || (prim & STCMASK) == UNIPP
)
error("too many levels of indirection", NULL);
y = prim & ~STCMASK;
switch (prim & STCMASK) {
case PSTRUCT: return STCPTR | y;
case STCPTR: return STCPP | y;
case PUNION: return UNIPTR | y;
case UNIPTR: return UNIPP | y;
}
return PINT == prim? INTPTR:
PCHAR == prim? CHARPTR:
PVOID == prim? VOIDPTR:
INTPTR == prim? INTPP:
CHARPTR == prim? CHARPP: VOIDPP;
}
/*
* declarator :=
* IDENT
* | * IDENT
* | * * IDENT
* | * IDENT [ constexpr ]
* | IDENT [ constexpr ]
* | IDENT = constexpr
* | IDENT [ ] = initlist
* | IDENT pmtrdecl
* | IDENT [ ]
* | * IDENT [ ]
* | ( * IDENT ) ( )
*/
// Parse the identifier part of a declarator
static int declarator(int pmtr, int scls, char *name, int *pprim, int *psize,
int *pval, int *pinit)
{
int type = TVARIABLE;
int ptrptr = 0;
if (STAR == Token) {
Token = scan();
*pprim = pointerto(*pprim);
if (STAR == Token) {
Token = scan();
*pprim = pointerto(*pprim);
ptrptr = 1;
}
}
else if (LPAREN == Token) {
if (*pprim != PINT)
error("function pointers are limited to type 'int'",
NULL);
Token = scan();
*pprim = FUNPTR;
match(STAR, "(*name)()");
}
if (IDENT != Token) {
error("missing identifier at: %s", Text);
name[0] = 0;
}
else {
copyname(name, Text);
Token = scan();
}
if (FUNPTR == *pprim) {
rparen();
lparen();
rparen();
}
if (!pmtr && ASSIGN == Token) {
Token = scan();
*pval = constexpr();
if (PCHAR == *pprim)
*pval &= 0xff;
if (*pval && !inttype(*pprim))
error("non-zero pointer initialization", NULL);
*pinit = 1;
}
else if (!pmtr && LPAREN == Token) {
Token = scan();
*psize = pmtrdecls();
rparen();
return TFUNCTION;
}
else if (LBRACK == Token) {
if (ptrptr)
error("too many levels of indirection: %s", name);
Token = scan();
if (RBRACK == Token) {
Token = scan();
if (pmtr) {
*pprim = pointerto(*pprim);
}
else {
type = TARRAY;
*psize = 1;
if (ASSIGN == Token) {
Token = scan();
if (!inttype(*pprim))
error("initialization of"
" pointer array not"
" supported",
NULL);
*psize = initlist(name, *pprim);
if (CAUTO == scls)
error("initialization of"
" local arrays"
" not supported: %s",
name);
*pinit = 1;
}
else if (CEXTERN != scls) {
error("automatically-sized array"
" lacking initialization: %s",
name);
}
}
}
else {
*psize = constexpr();
if (*psize < 0) {
error("invalid array size", NULL);
*psize = 0;
}
type = TARRAY;
rbrack();
}
}
if (PVOID == *pprim)
error("'void' is not a valid type: %s", name);
return type;
}
/*
* localdecls :=
* ldecl
* | ldecl localdecls
*
* ldecl :=
* primtype ldecl_list ;
* | lclass primtype ldecl_list ;
* | lclass ldecl_list ;
* | enum_decl
* | struct_decl
*
* lclass :=
* | AUTO
* | EXTERN
* | REGISTER
* | STATIC
* | VOLATILE
*
* ldecl_list :=
* declarator
* | declarator , ldecl_list
*/
static int localdecls(void) {
char name[NAMELEN+1];
int prim, type, size, addr = 0, val, ini;
int stat, extn;
int pbase, rsize;
Nli = 0;
while ( AUTO == Token || EXTERN == Token || REGISTER == Token ||
STATIC == Token || VOLATILE == Token ||
INT == Token || CHAR == Token || VOID == Token ||
ENUM == Token ||
STRUCT == Token || UNION == Token
) {
if (ENUM == Token) {
enumdecl(0);
continue;
}
extn = stat = 0;
if (AUTO == Token || REGISTER == Token || STATIC == Token ||
VOLATILE == Token || EXTERN == Token
) {
stat = STATIC == Token;
extn = EXTERN == Token;
Token = scan();
if ( INT == Token || CHAR == Token ||
VOID == Token ||
STRUCT == Token || UNION == Token
) {
prim = primtype(Token, NULL);
Token = scan();
}
else
prim = PINT;
}
else {
prim = primtype(Token, NULL);
Token = scan();
}
pbase = prim;
for (;;) {
prim = pbase;
if (eofcheck()) return 0;
size = 1;
ini = val = 0;
type = declarator(0, CAUTO, name, &prim, &size,
&val, &ini);
rsize = objsize(prim, type, size);
rsize = (rsize + INTSIZE-1) / INTSIZE * INTSIZE;
if (stat) {
addloc(name, prim, type, CLSTATC, size,
label(), val);
}
else if (extn) {
addloc(name, prim, type, CEXTERN, size,
0, val);
}
else {
addr -= rsize;
addloc(name, prim, type, CAUTO, size, addr, 0);
}
if (ini && !stat) {
if (Nli >= MAXLOCINIT) {
error("too many local initializers",
NULL);
Nli = 0;
}
LIaddr[Nli] = addr;
LIval[Nli++] = val;
}
if (COMMA == Token)
Token = scan();
else
break;
}
semi();
}
return addr;
}
// Walk two integer lists and return the difference
// between the first different elements. The list
// ends with a zero element. This is used to
// compare the signature of an existing function
// against a new function signature.
static int intcmp(int *x1, int *x2) {
while (*x1 && *x1 == *x2)
x1++, x2++;
return *x1 - *x2;
}
static void signature(int fn, int from, int to) {
int types[MAXFNARGS+1], i;
if (to - from > MAXFNARGS)
error("too many function parameters", Syms[fn].name);
for (i=0; i<MAXFNARGS && from < to; i++)
types[i] = Syms[--to].prim;
types[i] = 0;
if (NULL == Syms[fn].mtext) {
Syms[fn].mtext = galloc((i+1) * sizeof(int), 1);
memcpy(Syms[fn].mtext, types, (i+1) * sizeof(int));
}
else if (intcmp((int *) Syms[fn].mtext, types))
error("declaration does not match prior prototype: %s",
Syms[fn].name);
}
/*
* decl :=
* declarator { localdecls stmt_list }
* | decl_list ;
*
* decl_list :=
* declarator
* | declarator , decl_list
*/
void decl(int clss, int prim) {
char name[NAMELEN+1];
int pbase, type, size = 0, val, init;
int lsize;
pbase = prim;
for (;;) {
prim = pbase;
val = 0;
init = 0;
type = declarator(0, clss, name, &prim, &size, &val, &init);
if (TFUNCTION == type) {
clss = clss == CSTATIC? CSPROTO: CEXTERN;
Thisfn = addglob(name, prim, type, clss, size, 0,
NULL, 0);
signature(Thisfn, Locs, NSYMBOLS);
if (LBRACE == Token) {
clss = clss == CSPROTO? CSTATIC:
clss == CEXTERN? CPUBLIC: clss;
Thisfn = addglob(name, prim, type, clss, size,
0, NULL, 0);
Token = scan();
lsize = localdecls();
gentext();
if (CPUBLIC == clss) genpublic(name);
genaligntext();
genname(name);
genentry();
genstack(lsize);
genlocinit();
Retlab = label();
compound(0);
genlab(Retlab);
genstack(-lsize);
genexit();
if (O_debug & D_LSYM)
dumpsyms("LOCALS: ", name, Locs,
NSYMBOLS);
}
else {
semi();
}
clrlocs();
return;
}
if (CEXTERN == clss && init) {
error("initialization of 'extern': %s", name);
}
addglob(name, prim, type, clss, size, val, NULL, init);
if (COMMA == Token)
Token = scan();
else
break;
}
semi();
}
/*
* structdecl :=
* STRUCT identifier { member_list } ;
*
* member_list :=
* primtype mdecl_list ;
* | primtype mdecl_list ; member_list
*
* mdecl_list :=
* declarator
* | declatator , mdecl_list
*/
// Parse the definition of a struct or union.
// clss is the storage class already specified.
// uniondecl is true if this is a union declaration.
void structdecl(int clss, int uniondecl) {
int base, prim, size, dummy, type, addr = 0;
char name[NAMELEN+1], sname[NAMELEN+1];
int y, usize = 0;
// Get the name of the struct/union and copy it
// into sname. Confirm this was an identifier
// and get the next token next token
Token = scan();
copyname(sname, Text);
ident();
// The next token wasn't an '{', so this must be the
// declaration of a variable. Check that such a struct/union
// exists with primtype(). Then call decl() to parse the
// variable's definition.
if (Token != LBRACE) {
prim = primtype(uniondecl? UNION: STRUCT, sname);
decl(clss, prim);
return;
}
// This is a struct/union definition. Add a symbol for
// it in the global symbol table.
y = addglob(sname, uniondecl? PUNION: PSTRUCT, TSTRUCT,
CMEMBER, 0, 0, NULL, 0);
// Loop reading in the member definitions for the struct/union
Token = scan();
while ( INT == Token || CHAR == Token || VOID == Token ||
STRUCT == Token || UNION == Token
) {
// Convert the token into a type value: PCHAR, PINT etc.
base = primtype(Token, NULL);
size = 0;
Token = scan();
for (;;) {
if (eofcheck()) return;
// XXX Not sure why prim wasn't used 5 lines above
prim = base;
// Parse and verify the member's name
type = declarator(1, clss, name, &prim, &size,
&dummy, &dummy);
// Add it as a member of the struct/union
addglob(name, prim, type, CMEMBER, size, addr,
NULL, 0);
// Get the member's size in bytes
size = objsize(prim, type, size);
if (size < 0)
error("size of struct/union member"
" is unknown: %s",
name);
// If a union, keep the size of the biggest member
if (uniondecl) {
usize = size > usize? size: usize;
}
// Otherwise add on member's size to the total size.
// Ensure this is aligned to the size of an int.
else {
addr += size;
addr = (addr + INTSIZE-1) / INTSIZE * INTSIZE;
}
// Loop back on a ',' for another member, else stop
if (Token != COMMA) break;
Token = scan();
}
semi(); // Finally parse the ';' at the end
}
// The definition must end with a '}' ';'
// Set up the size of the struct/union in bytes
rbrace();
semi();
Syms[y].size = uniondecl? usize: addr;
}
/*
* top :=
* ENUM enumdecl
* | decl
* | primtype decl
* | storclass decl
* | storclass primtype decl
*
* storclass :=
* EXTERN
* | STATIC
*/
// Parse the top of a C file
void top(void) {
// Assume the default class is public
int prim, clss = CPUBLIC;
// Change the class to extern or static based on the next token.
// Ignore the keyword volatile.
switch (Token) {
case EXTERN: clss = CEXTERN; Token = scan(); break;
case STATIC: clss = CSTATIC; Token = scan(); break;
case VOLATILE: Token = scan(); break;
}
// If we get an enum, struct or union, parse them separately
switch (Token) {
case ENUM:
enumdecl(1);
break;
case STRUCT:
case UNION:
structdecl(clss, UNION == Token);
break;
// Set the primitive type to char, int or void
// Skip past this token and use decl() to parse the
// rest of the declaration
case CHAR:
case INT:
case VOID:
prim = primtype(Token, NULL);
Token = scan();
decl(clss, prim);
break;
// If we get an identifier with no type specifier, assume int
case IDENT:
decl(clss, PINT);
break;
// Error as we didn't get a type specifier
default:
error("type specifier expected at: %s", Text);
Token = synch(SEMI);
break;
}
}
// Print out the compile statistics
// at the end of the compilation
static void stats(void) {
printf( "Memory usage: "
"Symbols: %5d/%5d, "
"Names: %5d/%5d, "
"Nodes: %5d/%5d\n",
Globs, NSYMBOLS,
Nbot, POOLSIZE,
Ndmax, NODEPOOLSZ);
}
// Define a macro given on the command line
void defarg(char *s) {
char *p;
if (NULL == s) return;
if ((p = strchr(s, '=')) != NULL)
*p++ = 0;
else
p = "";
addglob(s, 0, TMACRO, 0, 0, 0, globname(p), 0);
if (*p) *--p = '=';
}
// Parse the specified file and generate assembly output
void program(char *name, FILE *in, FILE *out, char *def) {
init(); // Initialise all variables
defarg(def); // Define any -D from the cmd line
Infile = in; // Set the current in/out FILE pointers
Outfile = out;
File = Basefile = name; // Set the input file's name
genprelude(); // Generate the assembly prelude
Token = scan(); // Get the first token from the input
while (XEOF != Token) // Until we hit the end of the file
top();
genpostlude(); // Generate the assembly postlude
// Optionally, dump symbols and stats
if (O_debug & D_GSYM) dumpsyms("GLOBALS", "", 1, Globs);
if (O_debug & D_STAT) stats();
}