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
1515 prog: output code
1616 astk: arg position stack
1717 */
18−int tok, *vac, *vat, rsym,
18+int tok, tok1, *vac, *vat, rsym,
1919 prog, ind, loc, glo, file, vt,
2020 vc, *macro_stack, *macro_stack_ptr, line_num;
2121 char *idtable, *idptr, *filename;
@@ -62,7 +62,10 @@ char *idtable, *idptr, *filename; · 40 unmodified lines
6262 #define VT_STATIC 0x10000 /* static variable */
6363
6464 /* Special infos */
65+#define VT_LABEL 0x40000 /* goto label symbol */
6566 #define VT_DEFINE 0x80000 /* special value for #defined symbols */
67+#define VT_ENUM 0x100000 /* enum definition */
68+#define VT_TYPEDEF 0x200000 /* typedef definition */
6669
6770 /* token values */
6871 #define TOK_INT 256
@@ -97,8 +100,8 @@ char *idtable, *idptr, *filename; · 28 unmodified lines
97100 #define TOK_DOUBLE 281
98101 #define TOK_STRUCT 282
99102 #define TOK_UNION 283
100−#define TOK_TYPEDEF 284
101103
104+#define TOK_TYPEDEF 284
102105 #define TOK_DEFAULT 285
103106 #define TOK_ENUM 286
104107
@@ -214,6 +217,13 @@ void next() · 109 unmodified lines
214217 int c, v;
215218 char *q, *p;
216219
220+ /* special 'ungettok' case for label parsing */
221+ if (tok1) {
222+ tok = tok1;
223+ tok1 = 0;
224+ return;
225+ }
226+
217227 while(1) {
218228 c = inp();
219229 #ifndef TINY
@@ -543,32 +553,72 @@ void gen_op(op, l) · 323 unmodified lines
543553 }
544554 }
545555
556+int expr_const()
557+{
558+ expr_eq();
559+ if ((vt & (VT_CONST | VT_LVAL)) != VT_CONST)
560+ error("constant expected");
561+ return vc;
562+}
563+
546564 /* return 0 if no type declaration. otherwise, return the basic type
547565 and skip it.
548566 XXX: A '2' is ored to ensure non zero return if int type.
549567 */
550568 int ist()
551569 {
552− int t;
570+ int t, n, v;
571+
553572 t = 0;
554573 while(1) {
555− if (tok == TOK_CHAR | tok == TOK_VOID) {
556− t |= VT_BYTE;
557− } else if (tok == TOK_INT |
558− (tok >= TOK_CONST & tok <= TOK_SIGNED)) {
559− /* ignored types */
560− } else if (tok >= TOK_FLOAT & tok <= TOK_TYPEDEF) {
561− error("unsupported type");
562− } else if (tok == TOK_EXTERN) {
563− t |= VT_EXTERN;
564− } else if (tok == TOK_STATIC) {
565− t |= VT_STATIC;
566− } else if (tok == TOK_UNSIGNED) {
567− t |= VT_UNSIGNED;
574+ if (tok == TOK_ENUM) {
575+ next();
576+ if (tok != '{')
577+ next();
578+ if (tok == '{') {
579+ next();
580+ n = 0;
581+ while (tok != '}' & tok != -1) {
582+ v = tok;
583+ next();
584+ if (tok == '=') {
585+ next();
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;
568598 } 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();
570621 }
571− next();
572622 t |= 2;
573623 }
574624 return t;
@@ -630,14 +680,13 @@ int typ(int *v, int t, int *array_size_ptr) · 55 unmodified lines
630680 if (t & VT_ARRAY)
631681 error("multi dimension arrays not supported");
632682 next();
633− vc = 0;
683+ n = 0;
634684 if (tok != ']') {
635− expr();
685+ n = expr_const();
636686 if (array_size_ptr)
637− *array_size_ptr = vc;
687+ *array_size_ptr = n;
638688 }
639− if ((vt & (VT_CONST | VT_LVAL)) != VT_CONST |
640− (vc <= 0 & array_size_ptr != 0))
689+ if (n <= 0 & array_size_ptr != 0)
641690 error("invalid array size");
642691 skip(']');
643692 t = (t + VT_PTRINC) | VT_ARRAY;
@@ -1030,7 +1079,7 @@ void expr() · 386 unmodified lines
10301079
10311080 #endif
10321081
1033−void block(int *bsym, int *csym)
1082+void block(int *bsym, int *csym, int *case_sym, int *def_sym)
10341083 {
10351084 int a, b, c, d;
10361085
@@ -1041,13 +1090,13 @@ void block(int *bsym, int *csym) · 4 unmodified lines
10411090 expr();
10421091 skip(')');
10431092 a = gtst(1, 0);
1044− block(bsym, csym);
1093+ block(bsym, csym, case_sym, def_sym);
10451094 c = tok;
10461095 if (c == TOK_ELSE) {
10471096 next();
10481097 d = psym(0xe9, 0); /* jmp */
10491098 gsym(a);
1050− block(bsym, csym);
1099+ block(bsym, csym, case_sym, def_sym);
10511100 gsym(d); /* patch else jmp */
10521101 } else
10531102 gsym(a);
@@ -1059,7 +1108,7 @@ void block(int *bsym, int *csym) · 5 unmodified lines
10591108 skip(')');
10601109 a = gtst(1, 0);
10611110 b = 0;
1062− block(&a, &b);
1111+ block(&a, &b, case_sym, def_sym);
10631112 oad(0xe9, d - ind - 5); /* jmp */
10641113 gsym(a);
10651114 gsym_addr(b, d);
@@ -1068,7 +1117,7 @@ void block(int *bsym, int *csym) · 2 unmodified lines
10681117 /* declarations */
10691118 decl(VT_LOCAL);
10701119 while (tok != '}')
1071− block(bsym, csym);
1120+ block(bsym, csym, case_sym, def_sym);
10721121 next();
10731122 } else if (tok == TOK_RETURN) {
10741123 next();
@@ -1118,7 +1167,7 @@ void block(int *bsym, int *csym) · 43 unmodified lines
11181167 gsym(e);
11191168 }
11201169 skip(')');
1121− block(&a, &b);
1170+ block(&a, &b, case_sym, def_sym);
11221171 oad(0xe9, c - ind - 5); /* jmp */
11231172 gsym(a);
11241173 gsym_addr(b, c);
@@ -1128,7 +1177,7 @@ void block(int *bsym, int *csym) · 3 unmodified lines
11281177 a = 0;
11291178 b = 0;
11301179 d = ind;
1131− block(&a, &b);
1180+ block(&a, &b, case_sym, def_sym);
11321181 skip(TOK_WHILE);
11331182 skip('(');
11341183 gsym(b);
@@ -1138,11 +1187,91 @@ void block(int *bsym, int *csym) · 3 unmodified lines
11381187 skip(')');
11391188 gsym(a);
11401189 } 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
11411247 #endif
11421248 {
1143− if (tok != ';')
1144− expr();
1145− skip(';');
1249+ b = tok;
1250+ next();
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+ }
11461275 }
11471276 }
11481277
@@ -1152,6 +1281,12 @@ void decl(l) · 3 unmodified lines
11521281 int *a, t, b, s, align, v, u, n;
11531282
11541283 while (b = ist()) {
1284+ if ((b & VT_ENUM) && tok == ';') {
1285+ /* we accept no variable after */
1286+ next();
1287+ continue;
1288+ }
1289+
11551290 while (1) { /* iterate thru each declaration */
11561291 s = 1;
11571292 t = typ(&v, b, &s);
@@ -1166,14 +1301,16 @@ void decl(l) · 8 unmodified lines
11661301 o(0xe58955); /* push %ebp, mov %esp, %ebp */
11671302 a = oad(0xec81, 0); /* sub $xxx, %esp */
11681303 rsym = 0;
1169− block(0, 0);
1304+ block(0, 0, 0, 0);
11701305 gsym(rsym);
11711306 o(0xc3c9); /* leave, ret */
11721307 *a = (-loc + 3) & -4; /* align local size to word &
11731308 save local variables */
11741309 break;
11751310 } else {
1176− if (t & VT_FUNC) {
1311+ if (t & VT_TYPEDEF) {
1312+ vat[v] = t; /* save typedefed type */
1313+ } else if (t & VT_FUNC) {
11771314 /* external function definition */
11781315 external_func(v, t);
11791316 } 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