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
41#define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */41#define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */
42#define SYM_FIELD 0x20000000 /* struct/union field symbol space */42#define SYM_FIELD 0x20000000 /* struct/union field symbol space */
43 43
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
44typedef struct {48typedef struct {
45 FILE *file; /* stdio file */49 FILE *file;
46 char *filename;50 char *filename;
47 int line_num;51 int line_num;
48} IncludeFile;52} IncludeFile;
@@ -139,12 +143,17 @@ IncludeFile include_stack[INCLUDE_STACK_SIZE], *include_stack_ptr; · 90 unmodified lines
139#define TOK_SIZEOF 289143#define TOK_SIZEOF 289
140#define TOK_INCLUDE 290144#define TOK_INCLUDE 290
141 145
142#define TOK_EQ 0x94 /* warning: depend on asm code */146/* warning: the following compare tokens depend on i386 asm code */
143#define TOK_NE 0x95 /* warning: depend on asm code */147#define TOK_ULT 0x92
144#define TOK_LT 0x9c /* warning: depend on asm code */148#define TOK_UGE 0x93
145#define TOK_GE 0x9d /* warning: depend on asm code */149#define TOK_EQ 0x94
146#define TOK_LE 0x9e /* warning: depend on asm code */150#define TOK_NE 0x95
147#define TOK_GT 0x9f /* warning: depend on asm code */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
148 157
149#define TOK_LAND 0xa0158#define TOK_LAND 0xa0
150#define TOK_LOR 0xa1159#define TOK_LOR 0xa1
@@ -197,28 +206,41 @@ int isnum(c) · 46 unmodified lines
197}206}
198 207
199#ifndef TINY208#ifndef TINY
209
210void 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
200/* XXX: use stderr ? */218/* XXX: use stderr ? */
201void error(char *msg)219void error(char *msg)
202{220{
203 printf("%s:%d: %s\n", filename, line_num, msg);221 printline();
222 printf("%s\n", msg);
204 exit(1);223 exit(1);
205}224}
206 225
207void expect(char *msg)226void expect(char *msg)
208{227{
209 printf("%s:%d: %s expected\n", filename, line_num, msg);228 printline();
229 printf("%s expected\n", msg);
210 exit(1);230 exit(1);
211}231}
212 232
213void warning(char *msg)233void warning(char *msg)
214{234{
215 printf("%s:%d: warning: %s\n", filename, line_num, msg);235 printline();
236 printf("warning: %s\n", msg);
216}237}
217 238
218void skip(c)239void skip(c)
219{240{
220 if (tok != c) {241 if (tok != c) {
221 printf("%s:%d: '%c' expected\n", filename, line_num, c);242 printline();
243 printf("'%c' expected\n", c);
222 exit(1);244 exit(1);
223 }245 }
224 next();246 next();
@@ -1106,6 +1128,14 @@ void gen_op(op) · 881 unmodified lines
1106 op = TOK_UDIV;1128 op = TOK_UDIV;
1107 else if (op == '%')1129 else if (op == '%')
1108 op = TOK_UMOD;1130 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;
1109 }1139 }
1110 gen_opc(op);1140 gen_opc(op);
1111 }1141 }
@@ -1335,31 +1365,31 @@ int ist() · 223 unmodified lines
1335 return t;1365 return t;
1336}1366}
1337 1367
1338int post_type(u, t)1368int post_type(t)
1339{1369{
1340 int p, n, pt, l;1370 int p, n, pt, l, a;
1371 Sym *last, *s;
1341 1372
1342 if (tok == '(') {1373 if (tok == '(') {
1343 /* function declaration */1374 /* function declaration */
1344 next();1375 next();
1345 /* push a dummy symbol to force local symbol stack usage */1376 a = 4;
1346 sym_push1(&local_stack, 0, 0, 0);
1347 p = 4;
1348 l = 0;1377 l = 0;
1378 last = NULL;
1349 while (tok != ')') {1379 while (tok != ')') {
1350 /* read param name and compute offset */1380 /* read param name and compute offset */
1351 if (l != 2) {1381 if (l != FUNC_OLD) {
1352 if (!(pt = ist())) {1382 if (!(pt = ist())) {
1353 if (l) {1383 if (l) {
1354 error("invalid type");1384 error("invalid type");
1355 } else {1385 } else {
1356 l = 2;1386 l = FUNC_OLD;
1357 goto old_proto;1387 goto old_proto;
1358 }1388 }
1359 }1389 }
1360 if (pt & VT_VOID && tok == ')')1390 if (pt & VT_VOID && tok == ')')
1361 break;1391 break;
1362 l = 1;1392 l = FUNC_NEW;
1363 pt = typ(&n, pt); /* XXX: should accept 1393 pt = typ(&n, pt); /* XXX: should accept
1364 both arg/non arg if v == 0 */1394 both arg/non arg if v == 0 */
1365 } else {1395 } else {
@@ -1370,23 +1400,27 @@ int post_type(u, t) · 4 unmodified lines
1370 }1400 }
1371 /* array must be transformed to pointer according to ANSI C */1401 /* array must be transformed to pointer according to ANSI C */
1372 pt &= ~VT_ARRAY;1402 pt &= ~VT_ARRAY;
1373 p = p + 4;1403 /* XXX: size will be different someday */
1374 sym_push(n, VT_LOCAL | VT_LVAL | pt, p);1404 a = a + 4;
1405 s = sym_push(n | SYM_FIELD, VT_LOCAL | VT_LVAL | pt, a);
1406 s->next = last;
1407 last = s;
1375 if (tok == ',') {1408 if (tok == ',') {
1376 next();1409 next();
1377 if (l == 1 && tok == TOK_DOTS) {1410 if (l == FUNC_NEW && tok == TOK_DOTS) {
1411 l = FUNC_ELLIPSIS;
1378 next();1412 next();
1379 break;1413 break;
1380 }1414 }
1381 }1415 }
1382 }1416 }
1383 skip(')');1417 skip(')');
1384 t = post_type(0, t); /* XXX: may be incorrect */1418 t = post_type(t);
1385 /* transform function pointer to 'char *' */1419 /* we push a anonymous symbol which will contain the function prototype */
1386 if (u)1420 p = anon_sym++;
1387 t = u + VT_BYTE;1421 s = sym_push(p, t, l);
1388 else1422 s->next = last;
1389 t = t | VT_FUNC;1423 t = VT_FUNC | (p << VT_STRUCT_SHIFT);
1390 } else if (tok == '[') {1424 } else if (tok == '[') {
1391 /* array definition */1425 /* array definition */
1392 next();1426 next();
@@ -1398,7 +1432,7 @@ int post_type(u, t) · 5 unmodified lines
1398 }1432 }
1399 skip(']');1433 skip(']');
1400 /* parse next post type */1434 /* parse next post type */
1401 t = post_type(u, t);1435 t = post_type(t);
1402 1436
1403 /* we push a anonymous symbol which will contain the array1437 /* we push a anonymous symbol which will contain the array
1404 element type */1438 element type */
@@ -1413,7 +1447,8 @@ int post_type(u, t) · 8 unmodified lines
1413 type. If v is true, then also put variable name in 'vc' */1447 type. If v is true, then also put variable name in 'vc' */
1414int typ(int *v, int t)1448int typ(int *v, int t)
1415{1449{
1416 int u;1450 int u, p;
1451 Sym *s;
1417 1452
1418 t = t & -3; /* suppress the ored '2' */1453 t = t & -3; /* suppress the ored '2' */
1419 while (tok == '*') {1454 while (tok == '*') {
@@ -1435,13 +1470,26 @@ int typ(int *v, int t) · 15 unmodified lines
1435 next();1470 next();
1436 }1471 }
1437 }1472 }
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;
1439}1487}
1440 1488
1441/* define a new external reference to a function 'v' of type 'u' */1489/* define a new external reference to a function 'v' of type 'u' */
1442Sym *external_func(v, u)1490Sym *external_func(v, u)
1443{1491{
1444 int t, n;1492 int t, n, p;
1445 Sym *s;1493 Sym *s;
1446 s = sym_find(v);1494 s = sym_find(v);
1447 if (!s) {1495 if (!s) {
@@ -1589,7 +1637,10 @@ void unary() · 141 unmodified lines
1589 error("undefined symbol");1637 error("undefined symbol");
1590 /* for simple function calls, we tolerate undeclared1638 /* for simple function calls, we tolerate undeclared
1591 external reference */1639 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));
1593 }1644 }
1594 vset(s->t, s->c);1645 vset(s->t, s->c);
1595 /* if forward reference, we must point to s->c */1646 /* if forward reference, we must point to s->c */
@@ -1678,16 +1729,15 @@ void unary() · 82 unmodified lines
1678 *(int *)fc = psym(0xe8, *(int *)fc);1729 *(int *)fc = psym(0xe8, *(int *)fc);
1679 } else1730 } else
1680 oad(0xe8, fc - ind - 5);1731 oad(0xe8, fc - ind - 5);
1681 /* return value is %eax, and take type from function proto */
1682 vt = 0 | (ft & VT_TYPE & VT_FUNCN);
1683 } else {1732 } else {
1684 oad(0x2494ff, t); /* call *xxx(%esp) */1733 oad(0x2494ff, t); /* call *xxx(%esp) */
1685 t = t + 4;1734 t = t + 4;
1686 /* return value is %eax, integer */
1687 vt = 0;
1688 }1735 }
1689 if (t)1736 if (t)
1690 oad(0xc481, t);1737 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 */
1691 } else {1741 } else {
1692 break;1742 break;
1693 }1743 }
@@ -1732,7 +1782,8 @@ void sum(l) · 38 unmodified lines
1732 while ((l == 0 & (tok == '*' | tok == '/' | tok == '%')) |1782 while ((l == 0 & (tok == '*' | tok == '/' | tok == '%')) |
1733 (l == 1 & (tok == '+' | tok == '-')) |1783 (l == 1 & (tok == '+' | tok == '-')) |
1734 (l == 2 & (tok == TOK_SHL | tok == TOK_SAR)) |1784 (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)) |
1736 (l == 4 & (tok == TOK_EQ | tok == TOK_NE)) |1787 (l == 4 & (tok == TOK_EQ | tok == TOK_NE)) |
1737 (l == 5 & tok == '&') |1788 (l == 5 & tok == '&') |
1738 (l == 6 & tok == '^') |1789 (l == 6 & tok == '^') |
@@ -2037,6 +2088,8 @@ void decl(l) · 298 unmodified lines
2037 while (1) { /* iterate thru each declaration */2088 while (1) { /* iterate thru each declaration */
2038 t = typ(&v, b);2089 t = typ(&v, b);
2039 if (tok == '{') {2090 if (tok == '{') {
2091 if (!(t & VT_FUNC))
2092 expect("function defintion");
2040 /* patch forward references */2093 /* patch forward references */
2041 if ((sym = sym_find(v)) && (sym->t & VT_FORWARD)) {2094 if ((sym = sym_find(v)) && (sym->t & VT_FORWARD)) {
2042 gsym(sym->c);2095 gsym(sym->c);
@@ -2046,6 +2099,13 @@ void decl(l) · 3 unmodified lines
2046 /* put function address */2099 /* put function address */
2047 sym_push1(&global_stack, v, VT_CONST | VT_LVAL | t, ind);2100 sym_push1(&global_stack, v, VT_CONST | VT_LVAL | t, ind);
2048 }2101 }
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 }
2049 loc = 0;2109 loc = 0;
2050 o(0xe58955); /* push %ebp, mov %esp, %ebp */2110 o(0xe58955); /* push %ebp, mov %esp, %ebp */
2051 a = (int *)oad(0xec81, 0); /* sub $xxx, %esp */2111 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