History

added enum, typedefs, goto, switch

bellard committed 25 years ago

Commit b15f2691 parent 4e92bac1 file · +172 −35Browse files

Unified · Split

tcc.c+172−35
@@ -15,7 +15,7 @@ · 14 unmodified lines
15 prog: output code15 prog: output code
16 astk: arg position stack16 astk: arg position stack
17*/17*/
18int tok, *vac, *vat, rsym, 18int tok, tok1, *vac, *vat, rsym,
19 prog, ind, loc, glo, file, vt, 19 prog, ind, loc, glo, file, vt,
20 vc, *macro_stack, *macro_stack_ptr, line_num;20 vc, *macro_stack, *macro_stack_ptr, line_num;
21char *idtable, *idptr, *filename;21char *idtable, *idptr, *filename;
@@ -62,7 +62,10 @@ char *idtable, *idptr, *filename; · 40 unmodified lines
62#define VT_STATIC 0x10000 /* static variable */62#define VT_STATIC 0x10000 /* static variable */
63 63
64/* Special infos */64/* Special infos */
65#define VT_LABEL 0x40000 /* goto label symbol */
65#define VT_DEFINE 0x80000 /* special value for #defined symbols */66#define VT_DEFINE 0x80000 /* special value for #defined symbols */
67#define VT_ENUM 0x100000 /* enum definition */
68#define VT_TYPEDEF 0x200000 /* typedef definition */
66 69
67/* token values */70/* token values */
68#define TOK_INT 25671#define TOK_INT 256
@@ -97,8 +100,8 @@ char *idtable, *idptr, *filename; · 28 unmodified lines
97#define TOK_DOUBLE 281100#define TOK_DOUBLE 281
98#define TOK_STRUCT 282101#define TOK_STRUCT 282
99#define TOK_UNION 283102#define TOK_UNION 283
100#define TOK_TYPEDEF 284
101 103
104#define TOK_TYPEDEF 284
102#define TOK_DEFAULT 285105#define TOK_DEFAULT 285
103#define TOK_ENUM 286106#define TOK_ENUM 286
104 107
@@ -214,6 +217,13 @@ void next() · 109 unmodified lines
214 int c, v;217 int c, v;
215 char *q, *p;218 char *q, *p;
216 219
220 /* special 'ungettok' case for label parsing */
221 if (tok1) {
222 tok = tok1;
223 tok1 = 0;
224 return;
225 }
226
217 while(1) {227 while(1) {
218 c = inp();228 c = inp();
219#ifndef TINY229#ifndef TINY
@@ -543,32 +553,72 @@ void gen_op(op, l) · 323 unmodified lines
543 }553 }
544}554}
545 555
556int expr_const()
557{
558 expr_eq();
559 if ((vt & (VT_CONST | VT_LVAL)) != VT_CONST)
560 error("constant expected");
561 return vc;
562}
563
546/* return 0 if no type declaration. otherwise, return the basic type564/* return 0 if no type declaration. otherwise, return the basic type
547 and skip it. 565 and skip it.
548 XXX: A '2' is ored to ensure non zero return if int type.566 XXX: A '2' is ored to ensure non zero return if int type.
549 */567 */
550int ist()568int ist()
551{569{
552 int t;570 int t, n, v;
571
553 t = 0;572 t = 0;
554 while(1) {573 while(1) {
555 if (tok == TOK_CHAR | tok == TOK_VOID) {574 if (tok == TOK_ENUM) {
556 t |= VT_BYTE;575 next();
557 } else if (tok == TOK_INT |576 if (tok != '{')
558 (tok >= TOK_CONST & tok <= TOK_SIGNED)) {577 next();
559 /* ignored types */578 if (tok == '{') {
560 } else if (tok >= TOK_FLOAT & tok <= TOK_TYPEDEF) {579 next();
561 error("unsupported type");580 n = 0;
562 } else if (tok == TOK_EXTERN) {581 while (tok != '}' & tok != -1) {
563 t |= VT_EXTERN;582 v = tok;
564 } else if (tok == TOK_STATIC) {583 next();
565 t |= VT_STATIC;584 if (tok == '=') {
566 } else if (tok == TOK_UNSIGNED) {585 next();
567 t |= VT_UNSIGNED;586 n = expr_const();
587 }
588 vat[v] = VT_CONST;
589 vac[v] = n;
590 if (tok != ',')
591 break;
592 next();
593 n++;
594 }
595 skip('}');
596 }
597 t |= VT_ENUM;
568 } else {598 } else {
569 break;599 if (tok == TOK_CHAR | tok == TOK_VOID) {
600 t |= VT_BYTE;
601 } else if (tok == TOK_INT |
602 (tok >= TOK_CONST & tok <= TOK_SIGNED)) {
603 /* ignored types */
604 } else if (tok >= TOK_FLOAT & tok <= TOK_UNION) {
605 error("unsupported type");
606 } else if (tok == TOK_EXTERN) {
607 t |= VT_EXTERN;
608 } else if (tok == TOK_STATIC) {
609 t |= VT_STATIC;
610 } else if (tok == TOK_UNSIGNED) {
611 t |= VT_UNSIGNED;
612 } else if (tok == TOK_TYPEDEF) {
613 t |= VT_TYPEDEF;
614 } else {
615 v = vat[tok];
616 if (!(v & VT_TYPEDEF))
617 break;
618 t = v & ~VT_TYPEDEF;
619 }
620 next();
570 }621 }
571 next();
572 t |= 2;622 t |= 2;
573 }623 }
574 return t;624 return t;
@@ -630,14 +680,13 @@ int typ(int *v, int t, int *array_size_ptr) · 55 unmodified lines
630 if (t & VT_ARRAY) 680 if (t & VT_ARRAY)
631 error("multi dimension arrays not supported");681 error("multi dimension arrays not supported");
632 next();682 next();
633 vc = 0;683 n = 0;
634 if (tok != ']') {684 if (tok != ']') {
635 expr();685 n = expr_const();
636 if (array_size_ptr)686 if (array_size_ptr)
637 *array_size_ptr = vc;687 *array_size_ptr = n;
638 }688 }
639 if ((vt & (VT_CONST | VT_LVAL)) != VT_CONST | 689 if (n <= 0 & array_size_ptr != 0)
640 (vc <= 0 & array_size_ptr != 0))
641 error("invalid array size");690 error("invalid array size");
642 skip(']');691 skip(']');
643 t = (t + VT_PTRINC) | VT_ARRAY;692 t = (t + VT_PTRINC) | VT_ARRAY;
@@ -1030,7 +1079,7 @@ void expr() · 386 unmodified lines
1030 1079
1031#endif1080#endif
1032 1081
1033void block(int *bsym, int *csym)1082void block(int *bsym, int *csym, int *case_sym, int *def_sym)
1034{1083{
1035 int a, b, c, d;1084 int a, b, c, d;
1036 1085
@@ -1041,13 +1090,13 @@ void block(int *bsym, int *csym) · 4 unmodified lines
1041 expr();1090 expr();
1042 skip(')');1091 skip(')');
1043 a = gtst(1, 0);1092 a = gtst(1, 0);
1044 block(bsym, csym);1093 block(bsym, csym, case_sym, def_sym);
1045 c = tok;1094 c = tok;
1046 if (c == TOK_ELSE) {1095 if (c == TOK_ELSE) {
1047 next();1096 next();
1048 d = psym(0xe9, 0); /* jmp */1097 d = psym(0xe9, 0); /* jmp */
1049 gsym(a);1098 gsym(a);
1050 block(bsym, csym);1099 block(bsym, csym, case_sym, def_sym);
1051 gsym(d); /* patch else jmp */1100 gsym(d); /* patch else jmp */
1052 } else1101 } else
1053 gsym(a);1102 gsym(a);
@@ -1059,7 +1108,7 @@ void block(int *bsym, int *csym) · 5 unmodified lines
1059 skip(')');1108 skip(')');
1060 a = gtst(1, 0);1109 a = gtst(1, 0);
1061 b = 0;1110 b = 0;
1062 block(&a, &b);1111 block(&a, &b, case_sym, def_sym);
1063 oad(0xe9, d - ind - 5); /* jmp */1112 oad(0xe9, d - ind - 5); /* jmp */
1064 gsym(a);1113 gsym(a);
1065 gsym_addr(b, d);1114 gsym_addr(b, d);
@@ -1068,7 +1117,7 @@ void block(int *bsym, int *csym) · 2 unmodified lines
1068 /* declarations */1117 /* declarations */
1069 decl(VT_LOCAL);1118 decl(VT_LOCAL);
1070 while (tok != '}')1119 while (tok != '}')
1071 block(bsym, csym);1120 block(bsym, csym, case_sym, def_sym);
1072 next();1121 next();
1073 } else if (tok == TOK_RETURN) {1122 } else if (tok == TOK_RETURN) {
1074 next();1123 next();
@@ -1118,7 +1167,7 @@ void block(int *bsym, int *csym) · 43 unmodified lines
1118 gsym(e);1167 gsym(e);
1119 }1168 }
1120 skip(')');1169 skip(')');
1121 block(&a, &b);1170 block(&a, &b, case_sym, def_sym);
1122 oad(0xe9, c - ind - 5); /* jmp */1171 oad(0xe9, c - ind - 5); /* jmp */
1123 gsym(a);1172 gsym(a);
1124 gsym_addr(b, c);1173 gsym_addr(b, c);
@@ -1128,7 +1177,7 @@ void block(int *bsym, int *csym) · 3 unmodified lines
1128 a = 0;1177 a = 0;
1129 b = 0;1178 b = 0;
1130 d = ind;1179 d = ind;
1131 block(&a, &b);1180 block(&a, &b, case_sym, def_sym);
1132 skip(TOK_WHILE);1181 skip(TOK_WHILE);
1133 skip('(');1182 skip('(');
1134 gsym(b);1183 gsym(b);
@@ -1138,11 +1187,91 @@ void block(int *bsym, int *csym) · 3 unmodified lines
1138 skip(')');1187 skip(')');
1139 gsym(a);1188 gsym(a);
1140 } else1189 } else
1190 if (tok == TOK_SWITCH) {
1191 next();
1192 skip('(');
1193 expr();
1194 gv();
1195 skip(')');
1196 a = 0;
1197 b = 0;
1198 c = 0;
1199 block(&a, csym, &b, &c);
1200 /* if no default, jmp after switch */
1201 if (c == 0)
1202 c = ind;
1203 /* default label */
1204 gsym_addr(b, c);
1205 /* break label */
1206 gsym(a);
1207 } else
1208 if (tok == TOK_CASE) {
1209 next();
1210 a = expr_const();
1211 if (!case_sym)
1212 error("switch expected");
1213 gsym(*case_sym);
1214 oad(0x3d, a); /* cmp $xxx, %eax */
1215 *case_sym = psym(0x850f, 0); /* jne xxx */
1216 skip(':');
1217 block(bsym, csym, case_sym, def_sym);
1218 } else
1219 if (tok == TOK_DEFAULT) {
1220 next();
1221 skip(':');
1222 if (!def_sym)
1223 error("switch expected");
1224 if (*def_sym)
1225 error("too many 'default'");
1226 *def_sym = ind;
1227 block(bsym, csym, case_sym, def_sym);
1228 } else
1229 if (tok == TOK_GOTO) {
1230 next();
1231 a = vat[tok];
1232 if (a == 0) {
1233 /* put forward definition */
1234 a = VT_LABEL | VT_FORWARD;
1235 vat[tok] = a;
1236 vac[tok] = 0;
1237 } else if (!(a & VT_LABEL))
1238 error("invalid label name");
1239 /* label already defined */
1240 if (a & VT_FORWARD)
1241 vac[tok] = psym(0xe9, vac[tok]); /* jmp xxx */
1242 else
1243 oad(0xe9, vac[tok] - ind - 5); /* jmp xxx */
1244 next();
1245 skip(';');
1246 } else
1141#endif1247#endif
1142 {1248 {
1143 if (tok != ';')1249 b = tok;
1144 expr();1250 next();
1145 skip(';');1251 if (tok == ':') {
1252 next();
1253 /* label case */
1254 a = vat[b];
1255 if (a != 0) {
1256 if (!(a & VT_LABEL))
1257 error("invalid label name");
1258 else if (!(a & VT_FORWARD))
1259 error("multiple defined label");
1260 gsym(vac[b]);
1261 }
1262 vac[b] = ind;
1263 vat[b] = VT_LABEL;
1264 block(bsym, csym, case_sym, def_sym);
1265 } else {
1266 /* expression case: go backward of one token */
1267 /* XXX: currently incorrect if number/string/char */
1268 tok1 = tok;
1269 tok = b;
1270 if (tok != ';') {
1271 expr();
1272 }
1273 skip(';');
1274 }
1146 }1275 }
1147}1276}
1148 1277
@@ -1152,6 +1281,12 @@ void decl(l) · 3 unmodified lines
1152 int *a, t, b, s, align, v, u, n;1281 int *a, t, b, s, align, v, u, n;
1153 1282
1154 while (b = ist()) {1283 while (b = ist()) {
1284 if ((b & VT_ENUM) && tok == ';') {
1285 /* we accept no variable after */
1286 next();
1287 continue;
1288 }
1289
1155 while (1) { /* iterate thru each declaration */1290 while (1) { /* iterate thru each declaration */
1156 s = 1;1291 s = 1;
1157 t = typ(&v, b, &s);1292 t = typ(&v, b, &s);
@@ -1166,14 +1301,16 @@ void decl(l) · 8 unmodified lines
1166 o(0xe58955); /* push %ebp, mov %esp, %ebp */1301 o(0xe58955); /* push %ebp, mov %esp, %ebp */
1167 a = oad(0xec81, 0); /* sub $xxx, %esp */1302 a = oad(0xec81, 0); /* sub $xxx, %esp */
1168 rsym = 0;1303 rsym = 0;
1169 block(0, 0);1304 block(0, 0, 0, 0);
1170 gsym(rsym);1305 gsym(rsym);
1171 o(0xc3c9); /* leave, ret */1306 o(0xc3c9); /* leave, ret */
1172 *a = (-loc + 3) & -4; /* align local size to word & 1307 *a = (-loc + 3) & -4; /* align local size to word &
1173 save local variables */1308 save local variables */
1174 break;1309 break;
1175 } else {1310 } else {
1176 if (t & VT_FUNC) {1311 if (t & VT_TYPEDEF) {
1312 vat[v] = t; /* save typedefed type */
1313 } else if (t & VT_FUNC) {
1177 /* external function definition */1314 /* external function definition */
1178 external_func(v, t);1315 external_func(v, t);
1179 } else {1316 } else {

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