History

added function prototypes and correct recursive type parsing

bellard committed 25 years ago

Commit b0bdbb11 parent aca44291 file · +98 −38Browse files

Unified · Split

tcc.c+98−38
@@ -41,8 +41,12 @@ typedef struct Sym { · 40 unmodified lines
4141 #define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */
4242 #define SYM_FIELD 0x20000000 /* struct/union field symbol space */
4343
44+#define FUNC_NEW 1 /* ansi function prototype */
45+#define FUNC_OLD 2 /* old function prototype */
46+#define FUNC_ELLIPSIS 3 /* ansi function prototype with ... */
47+
4448 typedef struct {
45− FILE *file; /* stdio file */
49+ FILE *file;
4650 char *filename;
4751 int line_num;
4852 } IncludeFile;
@@ -139,12 +143,17 @@ IncludeFile include_stack[INCLUDE_STACK_SIZE], *include_stack_ptr; · 90 unmodified lines
139143 #define TOK_SIZEOF 289
140144 #define TOK_INCLUDE 290
141145
142−#define TOK_EQ 0x94 /* warning: depend on asm code */
143−#define TOK_NE 0x95 /* warning: depend on asm code */
144−#define TOK_LT 0x9c /* warning: depend on asm code */
145−#define TOK_GE 0x9d /* warning: depend on asm code */
146−#define TOK_LE 0x9e /* warning: depend on asm code */
147−#define TOK_GT 0x9f /* warning: depend on asm code */
146+/* warning: the following compare tokens depend on i386 asm code */
147+#define TOK_ULT 0x92
148+#define TOK_UGE 0x93
149+#define TOK_EQ 0x94
150+#define TOK_NE 0x95
151+#define TOK_ULE 0x96
152+#define TOK_UGT 0x97
153+#define TOK_LT 0x9c
154+#define TOK_GE 0x9d
155+#define TOK_LE 0x9e
156+#define TOK_GT 0x9f
148157
149158 #define TOK_LAND 0xa0
150159 #define TOK_LOR 0xa1
@@ -197,28 +206,41 @@ int isnum(c) · 46 unmodified lines
197206 }
198207
199208 #ifndef TINY
209+
210+void printline()
211+{
212+ IncludeFile *f;
213+ for(f = include_stack; f < include_stack_ptr; f++)
214+ printf("In file included from %s:%d:\n", f->filename, f->line_num);
215+ printf("%s:%d: ", filename, line_num);
216+}
217+
200218 /* XXX: use stderr ? */
201219 void error(char *msg)
202220 {
203− printf("%s:%d: %s\n", filename, line_num, msg);
221+ printline();
222+ printf("%s\n", msg);
204223 exit(1);
205224 }
206225
207226 void expect(char *msg)
208227 {
209− printf("%s:%d: %s expected\n", filename, line_num, msg);
228+ printline();
229+ printf("%s expected\n", msg);
210230 exit(1);
211231 }
212232
213233 void warning(char *msg)
214234 {
215− printf("%s:%d: warning: %s\n", filename, line_num, msg);
235+ printline();
236+ printf("warning: %s\n", msg);
216237 }
217238
218239 void skip(c)
219240 {
220241 if (tok != c) {
221− printf("%s:%d: '%c' expected\n", filename, line_num, c);
242+ printline();
243+ printf("'%c' expected\n", c);
222244 exit(1);
223245 }
224246 next();
@@ -1106,6 +1128,14 @@ void gen_op(op) · 881 unmodified lines
11061128 op = TOK_UDIV;
11071129 else if (op == '%')
11081130 op = TOK_UMOD;
1131+ else if (op == TOK_LT)
1132+ op = TOK_ULT;
1133+ else if (op == TOK_GT)
1134+ op = TOK_UGT;
1135+ else if (op == TOK_LE)
1136+ op = TOK_ULE;
1137+ else if (op == TOK_GE)
1138+ op = TOK_UGE;
11091139 }
11101140 gen_opc(op);
11111141 }
@@ -1335,31 +1365,31 @@ int ist() · 223 unmodified lines
13351365 return t;
13361366 }
13371367
1338−int post_type(u, t)
1368+int post_type(t)
13391369 {
1340− int p, n, pt, l;
1370+ int p, n, pt, l, a;
1371+ Sym *last, *s;
13411372
13421373 if (tok == '(') {
13431374 /* function declaration */
13441375 next();
1345− /* push a dummy symbol to force local symbol stack usage */
1346− sym_push1(&local_stack, 0, 0, 0);
1347− p = 4;
1376+ a = 4;
13481377 l = 0;
1378+ last = NULL;
13491379 while (tok != ')') {
13501380 /* read param name and compute offset */
1351− if (l != 2) {
1381+ if (l != FUNC_OLD) {
13521382 if (!(pt = ist())) {
13531383 if (l) {
13541384 error("invalid type");
13551385 } else {
1356− l = 2;
1386+ l = FUNC_OLD;
13571387 goto old_proto;
13581388 }
13591389 }
13601390 if (pt & VT_VOID && tok == ')')
13611391 break;
1362− l = 1;
1392+ l = FUNC_NEW;
13631393 pt = typ(&n, pt); /* XXX: should accept
13641394 both arg/non arg if v == 0 */
13651395 } else {
@@ -1370,23 +1400,27 @@ int post_type(u, t) · 4 unmodified lines
13701400 }
13711401 /* array must be transformed to pointer according to ANSI C */
13721402 pt &= ~VT_ARRAY;
1373− p = p + 4;
1374− sym_push(n, VT_LOCAL | VT_LVAL | pt, p);
1403+ /* XXX: size will be different someday */
1404+ a = a + 4;
1405+ s = sym_push(n | SYM_FIELD, VT_LOCAL | VT_LVAL | pt, a);
1406+ s->next = last;
1407+ last = s;
13751408 if (tok == ',') {
13761409 next();
1377− if (l == 1 && tok == TOK_DOTS) {
1410+ if (l == FUNC_NEW && tok == TOK_DOTS) {
1411+ l = FUNC_ELLIPSIS;
13781412 next();
13791413 break;
13801414 }
13811415 }
13821416 }
13831417 skip(')');
1384− t = post_type(0, t); /* XXX: may be incorrect */
1385− /* transform function pointer to 'char *' */
1386− if (u)
1387− t = u + VT_BYTE;
1388− else
1389− t = t | VT_FUNC;
1418+ t = post_type(t);
1419+ /* we push a anonymous symbol which will contain the function prototype */
1420+ p = anon_sym++;
1421+ s = sym_push(p, t, l);
1422+ s->next = last;
1423+ t = VT_FUNC | (p << VT_STRUCT_SHIFT);
13901424 } else if (tok == '[') {
13911425 /* array definition */
13921426 next();
@@ -1398,7 +1432,7 @@ int post_type(u, t) · 5 unmodified lines
13981432 }
13991433 skip(']');
14001434 /* parse next post type */
1401− t = post_type(u, t);
1435+ t = post_type(t);
14021436
14031437 /* we push a anonymous symbol which will contain the array
14041438 element type */
@@ -1413,7 +1447,8 @@ int post_type(u, t) · 8 unmodified lines
14131447 type. If v is true, then also put variable name in 'vc' */
14141448 int typ(int *v, int t)
14151449 {
1416− int u;
1450+ int u, p;
1451+ Sym *s;
14171452
14181453 t = t & -3; /* suppress the ored '2' */
14191454 while (tok == '*') {
@@ -1435,13 +1470,26 @@ int typ(int *v, int t) · 15 unmodified lines
14351470 next();
14361471 }
14371472 }
1438− return post_type(u, t);
1473+ /* append t at the end of u */
1474+ t = post_type(t);
1475+ if (!u)
1476+ return t;
1477+ p = u;
1478+ while(1) {
1479+ s = sym_find((unsigned)p >> VT_STRUCT_SHIFT);
1480+ p = s->t;
1481+ if (!p) {
1482+ s->t = t;
1483+ break;
1484+ }
1485+ }
1486+ return u;
14391487 }
14401488
14411489 /* define a new external reference to a function 'v' of type 'u' */
14421490 Sym *external_func(v, u)
14431491 {
1444− int t, n;
1492+ int t, n, p;
14451493 Sym *s;
14461494 s = sym_find(v);
14471495 if (!s) {
@@ -1589,7 +1637,10 @@ void unary() · 141 unmodified lines
15891637 error("undefined symbol");
15901638 /* for simple function calls, we tolerate undeclared
15911639 external reference */
1592− s = external_func(t, VT_FUNC); /* int() function */
1640+ p = anon_sym++;
1641+ sym_push1(&global_stack, p, 0, FUNC_OLD);
1642+ /* int() function */
1643+ s = external_func(t, VT_FUNC | (p << VT_STRUCT_SHIFT));
15931644 }
15941645 vset(s->t, s->c);
15951646 /* if forward reference, we must point to s->c */
@@ -1678,16 +1729,15 @@ void unary() · 82 unmodified lines
16781729 *(int *)fc = psym(0xe8, *(int *)fc);
16791730 } else
16801731 oad(0xe8, fc - ind - 5);
1681− /* return value is %eax, and take type from function proto */
1682− vt = 0 | (ft & VT_TYPE & VT_FUNCN);
16831732 } else {
16841733 oad(0x2494ff, t); /* call *xxx(%esp) */
16851734 t = t + 4;
1686− /* return value is %eax, integer */
1687− vt = 0;
16881735 }
16891736 if (t)
16901737 oad(0xc481, t);
1738+ /* get return type */
1739+ s = sym_find((unsigned)ft >> VT_STRUCT_SHIFT);
1740+ vt = s->t | 0; /* return register is eax */
16911741 } else {
16921742 break;
16931743 }
@@ -1732,7 +1782,8 @@ void sum(l) · 38 unmodified lines
17321782 while ((l == 0 & (tok == '*' | tok == '/' | tok == '%')) |
17331783 (l == 1 & (tok == '+' | tok == '-')) |
17341784 (l == 2 & (tok == TOK_SHL | tok == TOK_SAR)) |
1735− (l == 3 & (tok >= TOK_LT & tok <= TOK_GT)) |
1785+ (l == 3 & ((tok >= TOK_ULE & tok <= TOK_GT) |
1786+ tok == TOK_ULT | tok == TOK_UGE)) |
17361787 (l == 4 & (tok == TOK_EQ | tok == TOK_NE)) |
17371788 (l == 5 & tok == '&') |
17381789 (l == 6 & tok == '^') |
@@ -2037,6 +2088,8 @@ void decl(l) · 298 unmodified lines
20372088 while (1) { /* iterate thru each declaration */
20382089 t = typ(&v, b);
20392090 if (tok == '{') {
2091+ if (!(t & VT_FUNC))
2092+ expect("function defintion");
20402093 /* patch forward references */
20412094 if ((sym = sym_find(v)) && (sym->t & VT_FORWARD)) {
20422095 gsym(sym->c);
@@ -2046,6 +2099,13 @@ void decl(l) · 3 unmodified lines
20462099 /* put function address */
20472100 sym_push1(&global_stack, v, VT_CONST | VT_LVAL | t, ind);
20482101 }
2102+ /* push a dummy symbol to enable local sym storage */
2103+ sym_push1(&local_stack, 0, 0, 0);
2104+ /* define parameters */
2105+ sym = sym_find((unsigned)t >> VT_STRUCT_SHIFT);
2106+ while (sym = sym->next) {
2107+ sym_push(sym->v & ~SYM_FIELD, sym->t, sym->c);
2108+ }
20492109 loc = 0;
20502110 o(0xe58955); /* push %ebp, mov %esp, %ebp */
20512111 a = (int *)oad(0xec81, 0); /* sub $xxx, %esp */

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t