History

new code generator with constant propagation and register allocation - new parser - added #include handling

bellard committed 25 years ago

Commit 97364931 parent 946b6001 file · +1066 −552Browse files

Unified · Split

tcc.c+1066−552
@@ -17,12 +17,17 @@ · 16 unmodified lines
1717 * along with this program; if not, write to the Free Software
1818 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
1919 */
20−#include <stdio.h>
20+#include "tcclib.h"
2121
22−#define TEXT_SIZE 20000
23−#define DATA_SIZE 2000
24−#define SYM_TABLE_SIZE 10000
25−#define VAR_TABLE_SIZE 4096
22+#define INCLUDE_PATH "/usr/include"
23+
24+#define TEXT_SIZE 50000
25+#define DATA_SIZE 50000
26+#define SYM_TABLE_SIZE 10000
27+#define MACRO_STACK_SIZE 32
28+#define INCLUDE_STACK_SIZE 32
29+
30+#define NB_REGS 3
2631
2732 /* symbol management */
2833 typedef struct Sym {
@@ -36,68 +41,62 @@ typedef struct Sym { · 7 unmodified lines
3641 #define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */
3742 #define SYM_FIELD 0x20000000 /* struct/union field symbol space */
3843
44+typedef struct {
45+ FILE *file; /* stdio file */
46+ char *filename;
47+ int line_num;
48+} IncludeFile;
49+
3950 /* loc : local variable index
4051 glo : global variable index
41− parm : parameter variable index
4252 ind : output code ptr
4353 rsym: return symbol
4454 prog: output code
45− astk: arg position stack
55+ anon_sym: anonymous symbol index
4656 */
47−void *file;
48−int tok, tok1, rsym,
49− prog, ind, loc, glo, vt,
50− vc, *macro_stack, *macro_stack_ptr, line_num;
57+FILE *file;
58+int tok, tok1, tokc, rsym, anon_sym,
59+ prog, ind, loc, glo, vt, *vstack, *vstack_ptr,
60+ vc, line_num;
5161 char *idtable, *idptr, *filename;
5262 Sym *define_stack, *global_stack, *local_stack, *label_stack;
5363
54−/* The current value can be: */
55−#define VT_CONST 0x0002 /* constant in vc */
56−#define VT_VAR 0x0004 /* value is in eax */
57−#define VT_LOCAL 0x0008 /* offset on stack */
58−
59−#define VT_LVAL 0x0010 /* const or var is an lvalue */
60−#define VT_CMP 0x0020 /* the value is stored in processor flags (in vc) */
61−#define VT_FORWARD 0x0040 /* value is forward reference (only used for functions) */
62−#define VT_JMP 0x0080 /* value is the consequence of jmp. bit 0 is set if inv */
64+char *macro_stack[MACRO_STACK_SIZE], **macro_stack_ptr, *macro_ptr;
65+IncludeFile include_stack[INCLUDE_STACK_SIZE], *include_stack_ptr;
6366
67+/* The current value can be: */
68+#define VT_VALMASK 0x000f
69+#define VT_CONST 0x000a /* constant in vc
70+ (must be first non register value) */
71+#define VT_LLOCAL 0x000b /* lvalue, offset on stack */
72+#define VT_LOCAL 0x000c /* offset on stack */
73+#define VT_CMP 0x000d /* the value is stored in processor flags (in vc) */
74+#define VT_JMP 0x000e /* value is the consequence of jmp true */
75+#define VT_JMPI 0x000f /* value is the consequence of jmp false */
76+#define VT_LVAL 0x0010 /* var is an lvalue */
6477 #define VT_LVALN -17 /* ~VT_LVAL */
65−
66−/*
67− *
68− * VT_FUNC indicates a function. The return type is the stored type. A
69− * function pointer is stored as a 'char' pointer.
70− *
71− * If VT_PTRMASK is non nul, then it indicates the number of pointer
72− * iterations to reach the basic type.
73− *
74− * Basic types:
75− *
76− * VT_BYTE indicate a char
77− * VT_UNSIGNED indicates unsigned type
78− *
79− * otherwise integer type is assumed.
80− *
81− */
82−
83−#define VT_BYTE 0x00001 /* byte type, HARDCODED VALUE */
84−#define VT_PTRMASK 0x00f00 /* pointer mask */
85−#define VT_PTRINC 0x00100 /* pointer increment */
78+#define VT_FORWARD 0x0020 /* value is forward reference
79+ (only used for functions) */
80+
81+#define VT_VOID 0x00040
82+#define VT_BYTE 0x00080 /* byte type */
83+#define VT_PTR 0x00100 /* pointer increment */
84+#define VT_UNSIGNED 0x00200 /* unsigned type */
85+#define VT_ARRAY 0x00400 /* array type (only used in parsing) */
86+#define VT_ENUM 0x00800 /* enum definition */
8687 #define VT_FUNC 0x01000 /* function type */
87−#define VT_UNSIGNED 0x02000 /* unsigned type */
88−#define VT_ARRAY 0x04000 /* array type (only used in parsing) */
89−#define VT_TYPE 0xffffff01 /* type mask */
90−#define VT_TYPEN 0x000000fe /* ~VT_TYPE */
91−#define VT_FUNCN -4097 /* ~VT_FUNC */
92−
88+#define VT_STRUCT 0x002000 /* struct/union definition */
89+#define VT_TYPEDEF 0x004000 /* typedef definition */
9390 #define VT_EXTERN 0x008000 /* extern definition */
9491 #define VT_STATIC 0x010000 /* static variable */
92+#define VT_STRUCT_SHIFT 17 /* structure/enum name shift (12 bits lefts) */
93+
94+#define VT_TYPE 0xffffffc0 /* type mask */
95+#define VT_TYPEN 0x0000003f /* ~VT_TYPE */
96+#define VT_FUNCN -4097 /* ~VT_FUNC */
97+
9598
9699 /* Special infos */
97−#define VT_ENUM 0x020000 /* enum definition */
98−#define VT_STRUCT 0x040000 /* struct/union definition */
99−#define VT_TYPEDEF 0x080000 /* typedef definition */
100−#define VT_STRUCT_SHIFT 20 /* structure/enum name shift (12 bits lefts) */
101100
102101 /* token values */
103102 #define TOK_INT 256
@@ -126,15 +125,19 @@ Sym *define_stack, *global_stack, *local_stack, *label_stack; · 22 unmodified lines
126125 #define TOK_LONG 277
127126 #define TOK_REGISTER 278
128127 #define TOK_SIGNED 279
128+#define TOK_AUTO 280
129+#define TOK_INLINE 281
129130
130−#define TOK_FLOAT 280 /* unsupported */
131−#define TOK_DOUBLE 281 /* unsupported */
131+#define TOK_FLOAT 282 /* unsupported */
132+#define TOK_DOUBLE 283 /* unsupported */
132133
133−#define TOK_STRUCT 282
134−#define TOK_UNION 283
135−#define TOK_TYPEDEF 284
136−#define TOK_DEFAULT 285
137−#define TOK_ENUM 286
134+#define TOK_STRUCT 284
135+#define TOK_UNION 285
136+#define TOK_TYPEDEF 286
137+#define TOK_DEFAULT 287
138+#define TOK_ENUM 288
139+#define TOK_SIZEOF 289
140+#define TOK_INCLUDE 290
138141
139142 #define TOK_EQ 0x94 /* warning: depend on asm code */
140143 #define TOK_NE 0x95 /* warning: depend on asm code */
@@ -143,16 +146,22 @@ Sym *define_stack, *global_stack, *local_stack, *label_stack; · 2 unmodified lines
143146 #define TOK_LE 0x9e /* warning: depend on asm code */
144147 #define TOK_GT 0x9f /* warning: depend on asm code */
145148
146−#define TOK_LAND 0xa0
147−#define TOK_LOR 0xa1
149+#define TOK_LAND 0xa0
150+#define TOK_LOR 0xa1
148151
149152 #define TOK_DEC 0xa2
150153 #define TOK_MID 0xa3 /* inc/dec, to void constant */
151154 #define TOK_INC 0xa4
152155 #define TOK_ARROW 0xa7
153−
154−#define TOK_SHL 0x01
155−#define TOK_SHR 0x02
156+#define TOK_DOTS 0xa8 /* three dots */
157+#define TOK_SHR 0xa9 /* unsigned shift right */
158+#define TOK_UDIV 0xb0 /* unsigned division */
159+#define TOK_UMOD 0xb1 /* unsigned modulo */
160+#define TOK_PDIV 0xb2 /* fast division with undefined rounding for pointers */
161+#define TOK_NUM 0xb3 /* number in tokc */
162+
163+#define TOK_SHL 0x01 /* shift left */
164+#define TOK_SAR 0x02 /* signed shift right */
156165
157166 /* assignement operators : normal operator or 0x80 */
158167 #define TOK_A_MOD 0xa5
@@ -164,30 +173,16 @@ Sym *define_stack, *global_stack, *local_stack, *label_stack; · 5 unmodified lines
164173 #define TOK_A_XOR 0xde
165174 #define TOK_A_OR 0xfc
166175 #define TOK_A_SHL 0x81
167−#define TOK_A_SHR 0x82
176+#define TOK_A_SAR 0x82
168177
169−#ifdef TINY
170−#define expr_eq() expr()
171−#else
172178 void sum();
173179 void next();
174180 void expr_eq();
175181 void expr();
176182 void decl();
177−#endif
178−
179−
180−int inp()
181−{
182−#if 0
183− int c;
184− c = fgetc(file);
185− printf("c=%c\n", c);
186− return c;
187−#else
188− return fgetc(file);
189−#endif
190−}
183+int gv();
184+void move_reg();
185+void save_reg();
191186
192187 int isid(c)
193188 {
@@ -297,7 +292,7 @@ Sym *sym_find(int v) · 103 unmodified lines
297292 /* push a given symbol on the symbol stack */
298293 Sym *sym_push(int v, int t, int c)
299294 {
300− // printf("sym_push: %s type=%x\n", get_tok_str(v), t);
295+ // printf("sym_push: %x %s type=%x\n", v, get_tok_str(v), t);
301296 if (local_stack)
302297 return sym_push1(&local_stack, v, t, c);
303298 else
@@ -312,17 +307,214 @@ void sym_pop(Sym **ps, Sym *b) · 8 unmodified lines
312307 s = *ps;
313308 while(s != b) {
314309 ss = s->prev;
315− // printf("sym_pop: %s type=%x\n", get_tok_str(s->v), s->t);
310+ // printf("sym_pop: %x %s type=%x\n", s->v, get_tok_str(s->v), s->t);
316311 free(s);
317312 s = ss;
318313 }
319314 *ps = b;
320315 }
321316
317+int ch, ch1;
318+
319+/* read next char from current input file */
320+void inp()
321+{
322+ int c;
323+
324+ redo:
325+ ch1 = fgetc(file);
326+ if (ch1 == -1) {
327+ if (include_stack_ptr == include_stack)
328+ return;
329+ /* pop include stack */
330+ fclose(file);
331+ free(filename);
332+ include_stack_ptr--;
333+ file = include_stack_ptr->file;
334+ filename = include_stack_ptr->filename;
335+ line_num = include_stack_ptr->line_num;
336+ goto redo;
337+ }
338+ if (ch1 == '\n')
339+ line_num++;
340+ // printf("ch1=%c\n", ch1);
341+}
342+
343+/* input with '\\n' handling and macro subtitution if in macro state */
344+void minp()
345+{
346+ if (macro_ptr != 0) {
347+ ch = *macro_ptr++;
348+ /* end of macro ? */
349+ if (ch == '\0') {
350+ macro_ptr = *--macro_stack_ptr;
351+ ch = (int)*--macro_stack_ptr;
352+ }
353+ } else {
354+ redo:
355+ ch = ch1;
356+ inp();
357+ if (ch == '\\' && ch1 == '\n') {
358+ inp();
359+ goto redo;
360+ }
361+ }
362+}
363+
364+/* same as minp, but also skip comments */
365+void cinp()
366+{
367+ int c;
368+
369+ if (ch1 == '/') {
370+ inp();
371+ if (ch1 == '/') {
372+ /* single line C++ comments */
373+ inp();
374+ while (ch1 != '\n' && ch1 != -1)
375+ inp();
376+ inp();
377+ ch = ' '; /* return space */
378+ } else if (ch1 == '*') {
379+ /* C comments */
380+ inp();
381+ while (ch1 != -1) {
382+ c = ch1;
383+ inp();
384+ if (c == '*' && ch1 == '/') {
385+ inp();
386+ ch = ' '; /* return space */
387+ break;
388+ }
389+ }
390+ } else {
391+ ch = '/';
392+ }
393+ } else {
394+ minp();
395+ }
396+}
397+
398+void skip_spaces()
399+{
400+ while (ch == ' ' || ch == '\t')
401+ cinp();
402+}
403+
404+void preprocess()
405+{
406+ char *str;
407+ int size, n, c;
408+ char buf[1024], *q, *p;
409+ char buf1[1024];
410+ FILE *f;
411+
412+ cinp();
413+ next(); /* XXX: should pass parameter to avoid macro subst */
414+ if (tok == TOK_DEFINE) {
415+ next(); /* XXX: should pass parameter to avoid macro subst */
416+ skip_spaces();
417+ /* now 'tok' is the macro symbol */
418+ str = NULL;
419+ size = 0;
420+ n = 0;
421+ while (1) {
422+ if ((n + 1) >= size) {
423+ size += 128;
424+ str = realloc(str, size);
425+ if (!str)
426+ error("memory full");
427+ }
428+ if (ch == -1 || ch == '\n') {
429+ str[n++] = ' '; /* a space is inserted after each macro */
430+ str[n++] = '\0';
431+ break;
432+ }
433+ str[n++] = ch;
434+ cinp();
435+ }
436+ sym_push1(&define_stack, tok, 0, (int)str);
437+ } else if (tok == TOK_INCLUDE) {
438+ skip_spaces();
439+ if (ch == '<') {
440+ c = '>';
441+ goto read_name;
442+ } else if (ch == '\"') {
443+ c = ch;
444+ read_name:
445+ minp();
446+ q = buf;
447+ while (ch != c && ch != '\n' && ch != -1) {
448+ if ((q - buf) < sizeof(buf) - 1)
449+ *q++ = ch;
450+ minp();
451+ }
452+ *q = '\0';
453+ if (include_stack_ptr >= include_stack + INCLUDE_STACK_SIZE)
454+ error("memory full");
455+ if (c == '\"') {
456+ /* first search in current dir if "header.h" */
457+ /* XXX: buffer overflow */
458+ size = 0;
459+ p = strrchr(filename, '/');
460+ if (p)
461+ size = p + 1 - filename;
462+ memcpy(buf1, filename, size);
463+ buf1[size] = '\0';
464+ strcat(buf1, buf);
465+ f = fopen(buf1, "r");
466+ if (f)
467+ goto found;
468+ }
469+ /* now search in standard include path */
470+ strcpy(buf1, INCLUDE_PATH);
471+ strcat(buf1, "/");
472+ strcat(buf1, buf);
473+ f = fopen(buf1, "r");
474+ if (!f)
475+ error("include file not found");
476+ found:
477+ /* push current file in stack */
478+ include_stack_ptr->file = file;
479+ include_stack_ptr->filename = filename;
480+ include_stack_ptr->line_num = line_num;
481+ include_stack_ptr++;
482+ file = f;
483+ filename = strdup(buf1);
484+ line_num = 1;
485+ }
486+
487+ }
488+ /* ignore other preprocess commands or #! for C scripts */
489+ while (ch != '\n' && ch != -1)
490+ cinp();
491+}
492+
493+/* read a number in base b */
494+int getn(b)
495+{
496+ int n, t;
497+ n = 0;
498+ while (1) {
499+ if (ch >= 'a' & ch <= 'f')
500+ t = ch - 'a' + 10;
501+ else if (ch >= 'A' & ch <= 'F')
502+ t = ch - 'A' + 10;
503+ else if (isnum(ch))
504+ t = ch - '0';
505+ else
506+ break;
507+ if (t < 0 | t >= b)
508+ break;
509+ n = n * b + t;
510+ cinp();
511+ }
512+ return n;
513+}
322514
323515 void next()
324516 {
325− int c, v;
517+ int v, b;
326518 char *q, *p;
327519 Sym *s;
328520
@@ -332,67 +524,30 @@ void next() · 3 unmodified lines
332524 tok1 = 0;
333525 return;
334526 }
335−
527+ redo:
528+ /* skip spaces */
336529 while(1) {
337− c = inp();
338−#ifndef TINY
339− if (c == '/') {
340− /* comments */
341− c = inp();
342− if (c == '/') {
343− /* single line comments */
344− while (c != '\n')
345− c = inp();
346− } else if (c == '*') {
347− /* comments */
348− while ((c = inp()) >= 0) {
349− if (c == '*') {
350− c = inp();
351− if (c == '/') {
352− c = ' ';
353− break;
354− } else if (c == '*')
355− ungetc(c, file);
356− } else if (c == '\n')
357− line_num++;
358− }
359− } else {
360− ungetc(c, file);
361− c = '/';
362− break;
363− }
364− } else
365−#endif
366− if (c == 35) {
367− /* preprocessor: we handle only define */
368− next();
369− if (tok == TOK_DEFINE) {
370− next();
371− /* now tok is the macro symbol */
372− sym_push1(&define_stack, tok, 0, ftell(file));
530+ while (ch == '\n') {
531+ cinp();
532+ while (ch == ' ' || ch == 9)
533+ cinp();
534+ if (ch == '#') {
535+ /* preprocessor command if # at start of line after
536+ spaces */
537+ preprocess();
373538 }
374− /* ignore preprocessor or shell */
375− while (c != '\n')
376− c = inp();
377539 }
378− if (c == '\n') {
379− /* end of line : check if we are in macro state. if so,
380− pop new file position */
381− if (macro_stack_ptr > macro_stack)
382− fseek(file, *--macro_stack_ptr, 0);
383− else
384− line_num++;
385− } else if (c != ' ' & c != 9)
540+ if (ch != ' ' && ch != 9)
386541 break;
542+ cinp();
387543 }
388− if (isid(c)) {
544+ if (isid(ch)) {
389545 q = idptr;
390− while(isid(c) | isnum(c)) {
391− *q++ = c;
392− c = inp();
546+ while(isid(ch) | isnum(ch)) {
547+ *q++ = ch;
548+ cinp();
393549 }
394550 *q++ = '\0';
395− ungetc(c, file);
396551 p = idtable;
397552 tok = 256;
398553 while (p < idptr) {
@@ -401,48 +556,86 @@ void next() · 2 unmodified lines
401556 while (*p++);
402557 tok++;
403558 }
559+ // printf("id=%s\n", idptr);
404560 /* if not found, add symbol */
405561 if (p == idptr)
406562 idptr = q;
407− /* eval defines */
563+ /* if symbol is a define, prepare substitution */
408564 if (s = sym_find1(define_stack, tok)) {
409− *macro_stack_ptr++ = ftell(file);
410− fseek(file, s->c, 0);
411− next();
565+ if ((macro_stack_ptr - macro_stack) >= MACRO_STACK_SIZE)
566+ error("too many nested macros");
567+ *macro_stack_ptr++ = (char *)ch;
568+ *macro_stack_ptr++ = macro_ptr;
569+ macro_ptr = (char *)s->c;
570+ cinp();
571+ goto redo;
412572 }
573+ } else if (isnum(ch)) {
574+ /* number */
575+ b = 10;
576+ if (ch == '0') {
577+ cinp();
578+ b = 8;
579+ if (ch == 'x') {
580+ cinp();
581+ b = 16;
582+ }
583+ }
584+ tokc = getn(b);
585+ tok = TOK_NUM;
413586 } else {
414587 #ifdef TINY
415588 q = "<=\236>=\235!=\225++\244--\242==\224";
416589 #else
417− q = "<=\236>=\235!=\225&&\240||\241++\244--\242==\224<<\1>>\2+=\253-=\255*=\252/=\257%=\245&=\246^=\336|=\374->\247";
590+ q = "<=\236>=\235!=\225&&\240||\241++\244--\242==\224<<\1>>\2+=\253-=\255*=\252/=\257%=\245&=\246^=\336|=\374->\247..\250";
418591 #endif
419592 /* two chars */
420− v = inp();
593+ tok = ch;
594+ cinp();
421595 while (*q) {
422− if (*q == c & q[1] == v) {
596+ if (*q == tok & q[1] == ch) {
597+ cinp();
423598 tok = q[2] & 0xff;
424− if (tok == TOK_SHL | tok == TOK_SHR) {
425− v = inp();
426− if (v == '=')
599+ /* three chars tests */
600+ if (tok == TOK_SHL | tok == TOK_SAR) {
601+ if (ch == '=') {
427602 tok = tok | 0x80;
428− else
429− ungetc(v, file);
603+ cinp();
604+ }
605+ } else if (tok == TOK_DOTS) {
606+ if (ch != '.')
607+ error("parse error");
608+ cinp();
430609 }
431610 return;
432611 }
433612 q = q + 3;
434613 }
435− ungetc(v, file);
436614 /* single char substitutions */
437− if (c == '<')
615+ if (tok == '<')
438616 tok = TOK_LT;
439− else if (c == '>')
617+ else if (tok == '>')
440618 tok = TOK_GT;
441− else
442− tok = c;
443619 }
444620 }
445621
622+void swap(int *p, int *q)
623+{
624+ int t;
625+ t = *p;
626+ *p = *q;
627+ *q = t;
628+}
629+
630+void vset(t, v)
631+{
632+ vt = t;
633+ vc = v;
634+}
635+
636+/******************************************************/
637+/* X86 code generator */
638+
446639 void g(c)
447640 {
448641 *(char *)ind++ = c;
@@ -486,195 +679,520 @@ int oad(c, s) · 37 unmodified lines
486679 return s;
487680 }
488681
489−void vset(t, v)
490−{
491− vt = t;
492− vc = v;
493−}
494−
495−/* generate a value in eax from vt and vc */
496682 /* XXX: generate correct pointer for forward references to functions */
497−void gv()
683+/* r = (ft, fc) */
684+void load(r, ft, fc)
498685 {
499−#ifndef TINY
500− int t;
501−#endif
502− if (vt & VT_LVAL) {
503− if ((vt & VT_TYPE) == VT_BYTE)
504− o(0xbe0f); /* movsbl x, %eax */
505− else
506− o(0x8b); /* movl x,%eax */
507− if (vt & VT_CONST)
508− oad(0x05, vc);
509− else if (vt & VT_LOCAL)
510− oad(0x85, vc);
686+ int v, t;
687+
688+ v = ft & VT_VALMASK;
689+ if (ft & VT_LVAL) {
690+ if (v == VT_LLOCAL) {
691+ load(r, VT_LOCAL | VT_LVAL, fc);
692+ v = r;
693+ }
694+ if ((ft & VT_TYPE) == VT_BYTE)
695+ o(0xbe0f); /* movsbl */
511696 else
512− g(0x00);
513− } else {
514− if (vt & VT_CONST) {
515− oad(0xb8, vc); /* mov $xx, %eax */
516− } else if (vt & VT_LOCAL) {
517− oad(0x858d, vc); /* lea xxx(%ebp), %eax */
518− } else if (vt & VT_CMP) {
519− oad(0xb8, 0); /* mov $0, %eax */
520− o(0x0f); /* setxx %al */
521− o(vc);
522− o(0xc0);
697+ o(0x8b); /* movl */
698+ if (v == VT_CONST) {
699+ oad(0x05 + r * 8, fc); /* 0xXX, r */
700+ } else if (v == VT_LOCAL) {
701+ oad(0x85 + r * 8, fc); /* xx(%ebp), r */
702+ } else {
703+ g(0x00 + r * 8 + v); /* (v), r */
523704 }
524−#ifndef TINY
525− else if (vt & VT_JMP) {
526− t = vt & 1;
527− oad(0xb8, t); /* mov $1, %eax */
705+ } else {
706+ if (v == VT_CONST) {
707+ oad(0xb8 + r, fc); /* mov $xx, r */
708+ } else if (v == VT_LOCAL) {
709+ o(0x8d);
710+ oad(0x85 + r * 8, fc); /* lea xxx(%ebp), r */
711+ } else if (v == VT_CMP) {
712+ oad(0xb8 + r, 0); /* mov $0, r */
713+ o(0x0f); /* setxx %br */
714+ o(fc);
715+ o(0xc0 + r);
716+ } else if (v == VT_JMP || v == VT_JMPI) {
717+ t = v & 1;
718+ oad(0xb8 + r, t); /* mov $1, r */
528719 oad(0xe9, 5); /* jmp after */
529− gsym(vc);
530− oad(0xb8, t ^ 1); /* mov $0, %eax */
720+ gsym(fc);
721+ oad(0xb8 + r, t ^ 1); /* mov $0, r */
722+ } else if (v != r) {
723+ o(0x89);
724+ o(0xc0 + r + v * 8); /* mov v, r */
531725 }
532−#endif
533726 }
534− vt = (vt & VT_TYPE) | VT_VAR;
727+}
728+
729+/* (ft, fc) = r */
730+/* WARNING: r must not be allocated on the stack */
731+void store(r, ft, fc)
732+{
733+ int fr, b;
734+
735+ fr = ft & VT_VALMASK;
736+ b = (ft & VT_TYPE) == VT_BYTE;
737+ o(0x89 - b);
738+ if (fr == VT_CONST) {
739+ oad(0x05 + r * 8, fc); /* mov r,xxx */
740+ } else if (fr == VT_LOCAL) {
741+ oad(0x85 + r * 8, fc); /* mov r,xxx(%ebp) */
742+ } else if (ft & VT_LVAL) {
743+ g(fr + r * 8); /* mov r, (fr) */
744+ } else if (fr != r) {
745+ o(0xc0 + fr + r * 8); /* mov r, fr */
746+ }
747+}
748+
749+int gjmp(t)
750+{
751+ return psym(0xe9, t);
535752 }
536753
537754 /* generate a test. set 'inv' to invert test */
538−/* XXX: handle constant */
539755 int gtst(inv, t)
540756 {
541− if (vt & VT_CMP) {
757+ int v, *p;
758+ v = vt & VT_VALMASK;
759+ if (v == VT_CMP) {
542760 /* fast case : can jump directly since flags are set */
543761 g(0x0f);
544762 t = psym((vc - 16) ^ inv, t);
545− } else
546−#ifndef TINY
547− if (vt & VT_JMP) {
763+ } else if (v == VT_JMP || v == VT_JMPI) {
548764 /* && or || optimization */
549− if ((vt & 1) == inv)
765+ if ((v & 1) == inv) {
766+ /* insert vc jump list in t */
767+ p = &vc;
768+ while (*p != 0)
769+ p = (int *)*p;
770+ *p = t;
550771 t = vc;
551− else {
552− t = psym(0xe9, t);
772+ } else {
773+ t = gjmp(t);
553774 gsym(vc);
554775 }
555− } else
556− if ((vt & (VT_CONST | VT_LVAL)) == VT_CONST) {
776+ } else if ((vt & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
557777 /* constant jmp optimization */
558778 if ((vc != 0) != inv)
559− t = psym(0xe9, t);
560− } else
561−#endif
562− {
563− gv();
564− o(0xc085); /* test %eax, %eax */
779+ t = gjmp(t);
780+ } else {
781+ v = gv();
782+ o(0x85);
783+ o(0xc0 + v * 9);
565784 g(0x0f);
566785 t = psym(0x85 ^ inv, t);
567786 }
568787 return t;
569788 }
570789
790+/* generate a binary operation 'v = r op fr' instruction and modifies
791+ (vt,vc) if needed */
792+void gen_op1(op, r, fr)
793+{
794+ int t;
795+ if (op == '+') {
796+ o(0x01);
797+ o(0xc0 + r + fr * 8);
798+ } else if (op == '-') {
799+ o(0x29);
800+ o(0xc0 + r + fr * 8);
801+ } else if (op == '&') {
802+ o(0x21);
803+ o(0xc0 + r + fr * 8);
804+ } else if (op == '^') {
805+ o(0x31);
806+ o(0xc0 + r + fr * 8);
807+ } else if (op == '|') {
808+ o(0x09);
809+ o(0xc0 + r + fr * 8);
810+ } else if (op == '*') {
811+ o(0xaf0f); /* imul fr, r */
812+ o(0xc0 + fr + r * 8);
813+ } else if (op == TOK_SHL | op == TOK_SHR | op == TOK_SAR) {
814+ /* op2 is %ecx */
815+ if (fr != 1) {
816+ if (r == 1) {
817+ r = fr;
818+ fr = 1;
819+ o(0x87); /* xchg r, %ecx */
820+ o(0xc1 + r * 8);
821+ } else
822+ move_reg(1, fr);
823+ }
824+ o(0xd3); /* shl/shr/sar %cl, r */
825+ if (op == TOK_SHL)
826+ o(0xe0 + r);
827+ else if (op == TOK_SHR)
828+ o(0xe8 + r);
829+ else
830+ o(0xf8 + r);
831+ vt = (vt & VT_TYPE) | r;
832+ } else if (op == '/' | op == TOK_UDIV | op == TOK_PDIV |
833+ op == '%' | op == TOK_UMOD) {
834+ save_reg(2); /* save edx */
835+ t = save_reg_forced(fr); /* save fr and get op2 location */
836+ move_reg(0, r); /* op1 is %eax */
837+ if (op == TOK_UDIV | op == TOK_UMOD) {
838+ o(0xf7d231); /* xor %edx, %edx, div t(%ebp), %eax */
839+ oad(0xb5, t);
840+ } else {
841+ o(0xf799); /* cltd, idiv t(%ebp), %eax */
842+ oad(0xbd, t);
843+ }
844+ if (op == '%' | op == TOK_UMOD)
845+ r = 2;
846+ else
847+ r = 0;
848+ vt = (vt & VT_TYPE) | r;
849+ } else {
850+ o(0x39);
851+ o(0xc0 + r + fr * 8); /* cmp fr, r */
852+ vset(VT_CMP, op);
853+ }
854+}
855+
856+/* end of X86 code generator */
857+/*************************************************************/
858+
859+int save_reg_forced(r)
860+{
861+ int i, l, *p, t;
862+ /* store register */
863+ loc = (loc - 4) & -3;
864+ store(r, VT_LOCAL, loc);
865+ l = loc;
866+
867+ /* modify all stack values */
868+ for(p=vstack;p<vstack_ptr;p+=2) {
869+ i = p[0] & VT_VALMASK;
870+ if (i == r) {
871+ if (p[0] & VT_LVAL)
872+ t = VT_LLOCAL;
873+ else
874+ t = VT_LOCAL;
875+ p[0] = (p[0] & VT_TYPE) | VT_LVAL | t;
876+ p[1] = l;
877+ }
878+ }
879+ return l;
880+}
881+
882+/* save r to memory. and mark it as being free */
883+void save_reg(r)
884+{
885+ int i, *p;
886+
887+ /* modify all stack values */
888+ for(p=vstack;p<vstack_ptr;p+=2) {
889+ i = p[0] & VT_VALMASK;
890+ if (i == r) {
891+ save_reg_forced(r);
892+ break;
893+ }
894+ }
895+}
896+
897+/* find a free register. If none, save one register */
898+int get_reg()
899+{
900+ int r, i, *p;
901+
902+ /* find a free register */
903+ for(r=0;r<NB_REGS;r++) {
904+ for(p=vstack;p<vstack_ptr;p+=2) {
905+ i = p[0] & VT_VALMASK;
906+ if (i == r)
907+ goto notfound;
908+ }
909+ return r;
910+ notfound: ;
911+ }
912+
913+ /* no register left : free the first one on the stack (very
914+ important to start from the bottom to ensure that we don't
915+ spill registers used in gen_op()) */
916+ for(p=vstack;p<vstack_ptr;p+=2) {
917+ r = p[0] & VT_VALMASK;
918+ if (r < VT_CONST) {
919+ save_reg(r);
920+ break;
921+ }
922+ }
923+ return r;
924+}
925+
926+void save_regs()
927+{
928+ int r, *p;
929+ for(p=vstack;p<vstack_ptr;p+=2) {
930+ r = p[0] & VT_VALMASK;
931+ if (r < VT_CONST) {
932+ save_reg(r);
933+ }
934+ }
935+}
936+
937+/* move register 's' to 'r', and flush previous value of r to memory
938+ if needed */
939+void move_reg(r, s)
940+{
941+ if (r != s) {
942+ save_reg(r);
943+ load(r, s, 0);
944+ }
945+}
946+
947+/* convert a stack entry in register */
948+int gvp(int *p)
949+{
950+ int r;
951+ r = p[0] & VT_VALMASK;
952+ if (r >= VT_CONST || (p[0] & VT_LVAL))
953+ r = get_reg();
954+ load(r, p[0], p[1]);
955+ p[0] = (p[0] & VT_TYPE) | r;
956+ return r;
957+}
958+
959+void vpush()
960+{
961+ *vstack_ptr++ = vt;
962+ *vstack_ptr++ = vc;
963+ /* cannot let cpu flags if other instruction are generated */
964+ if ((vt & VT_VALMASK) == VT_CMP)
965+ gvp(vstack_ptr - 2);
966+}
967+
968+void vpop(int *ft, int *fc)
969+{
970+ *fc = *--vstack_ptr;
971+ *ft = *--vstack_ptr;
972+}
973+
974+/* generate a value in a register from vt and vc */
975+int gv()
976+{
977+ int r;
978+ vpush();
979+ r = gvp(vstack_ptr - 2);
980+ vpop(&vt, &vc);
981+ return r;
982+}
983+
984+/* handle constant optimizations and various machine independant opt */
985+void gen_opc(op)
986+{
987+ int fr, ft, fc, r, c1, c2, n;
988+
989+ vpop(&ft, &fc);
990+ vpop(&vt, &vc);
991+ c1 = (vt & (VT_VALMASK | VT_LVAL)) == VT_CONST;
992+ c2 = (ft & (VT_VALMASK | VT_LVAL)) == VT_CONST;
993+ if (c1 && c2) {
994+ switch(op) {
995+ case '+': vc += fc; break;
996+ case '-': vc -= fc; break;
997+ case '&': vc &= fc; break;
998+ case '^': vc ^= fc; break;
999+ case '|': vc |= fc; break;
1000+ case '*': vc *= fc; break;
1001+ case TOK_PDIV:
1002+ case '/': vc /= fc; break; /* XXX: zero case ? */
1003+ case '%': vc %= fc; break; /* XXX: zero case ? */
1004+ case TOK_UDIV: vc = (unsigned)vc / fc; break; /* XXX: zero case ? */
1005+ case TOK_UMOD: vc = (unsigned)vc % fc; break; /* XXX: zero case ? */
1006+ case TOK_SHL: vc <<= fc; break;
1007+ case TOK_SHR: vc = (unsigned)vc >> fc; break;
1008+ case TOK_SAR: vc >>= fc; break;
1009+ default:
1010+ goto general_case;
1011+ }
1012+ } else {
1013+ /* if commutative ops, put c2 as constant */
1014+ if (c1 && (op == '+' || op == '&' || op == '^' ||
1015+ op == '|' || op == '*')) {
1016+ swap(&vt, &ft);
1017+ swap(&vc, &fc);
1018+ swap(&c1, &c2);
1019+ }
1020+ if (c2 && (((op == '*' || op == '/' || op == TOK_UDIV ||
1021+ op == TOK_PDIV) &&
1022+ fc == 1) ||
1023+ ((op == '+' || op == '-' || op == '|' || op == '^' ||
1024+ op == TOK_SHL || op == TOK_SHR || op == TOK_SAR) &&
1025+ fc == 0) ||
1026+ (op == '&' &&
1027+ fc == -1))) {
1028+ } else if (c2 && (op == '*' || op == TOK_PDIV || op == TOK_UDIV)) {
1029+ /* try to use shifts instead of muls or divs */
1030+ if (fc > 0 && (fc & (fc - 1)) == 0) {
1031+ n = -1;
1032+ while (fc) {
1033+ fc >>= 1;
1034+ n++;
1035+ }
1036+ fc = n;
1037+ if (op == '*')
1038+ op = TOK_SHL;
1039+ else if (op == TOK_PDIV)
1040+ op = TOK_SAR;
1041+ else
1042+ op = TOK_SHR;
1043+ }
1044+ goto general_case;
1045+ } else {
1046+ general_case:
1047+ vpush();
1048+ vt = ft;
1049+ vc = fc;
1050+ vpush();
1051+ r = gvp(vstack_ptr - 4);
1052+ fr = gvp(vstack_ptr - 2);
1053+ vpop(&ft, &fc);
1054+ vpop(&vt, &vc);
1055+ /* call low level op generator */
1056+ gen_op1(op, r, fr);
1057+ }
1058+ }
1059+}
1060+
1061+int pointed_size(t)
1062+{
1063+ return type_size(pointed_type(t), &t);
1064+}
1065+
1066+/* generic gen_op: handles types problems */
1067+void gen_op(op)
1068+{
1069+ int u, t1, t2;
1070+
1071+ vpush();
1072+ t1 = vstack_ptr[-4];
1073+ t2 = vstack_ptr[-2];
1074+ if (op == '+' | op == '-') {
1075+ if ((t1 & VT_PTR) && (t2 & VT_PTR)) {
1076+ if (op != '-')
1077+ error("invalid type");
1078+ /* XXX: check that types are compatible */
1079+ u = pointed_size(t1);
1080+ gen_opc(op);
1081+ vpush();
1082+ vstack_ptr[-2] &= ~VT_TYPE; /* set to integer */
1083+ vset(VT_CONST, u);
1084+ gen_op(TOK_PDIV);
1085+ } else if ((t1 | t2) & VT_PTR) {
1086+ if (t2 & VT_PTR) {
1087+ swap(vstack_ptr - 4, vstack_ptr - 2);
1088+ swap(vstack_ptr - 3, vstack_ptr - 1);
1089+ swap(&t1, &t2);
1090+ }
1091+ /* stack-4 contains pointer, stack-2 value to add */
1092+ vset(VT_CONST, pointed_size(vstack_ptr[-4]));
1093+ gen_op('*');
1094+ vpush();
1095+ gen_opc(op);
1096+ /* put again type if gen_opc() swaped operands */
1097+ vt = (vt & VT_TYPEN) | (t1 & VT_TYPE);
1098+ } else {
1099+ gen_opc(op);
1100+ }
1101+ } else {
1102+ if ((t1 | t2) & VT_UNSIGNED) {
1103+ if (op == TOK_SAR)
1104+ op = TOK_SHR;
1105+ else if (op == '/')
1106+ op = TOK_UDIV;
1107+ else if (op == '%')
1108+ op = TOK_UMOD;
1109+ }
1110+ gen_opc(op);
1111+ }
1112+}
1113+
5711114 /* return type size. Put alignment at 'a' */
5721115 int type_size(int t, int *a)
5731116 {
5741117 Sym *s;
5751118
5761119 /* int, enum or pointer */
577− if ((t & VT_PTRMASK) >= VT_PTRINC |
578− (t & VT_TYPE) == 0 |
579− (t & VT_ENUM)) {
580− *a = 4;
581− return 4;
582− } else if (t & VT_STRUCT) {
1120+ if (t & VT_STRUCT) {
5831121 /* struct/union */
5841122 s = sym_find(((unsigned)t >> VT_STRUCT_SHIFT) | SYM_STRUCT);
5851123 *a = 4; /* XXX: cannot store it yet. Doing that is safe */
5861124 return s->c;
1125+ } else if (t & VT_ARRAY) {
1126+ s = sym_find(((unsigned)t >> VT_STRUCT_SHIFT));
1127+ return type_size(s->t, a) * s->c;
1128+ } else if ((t & VT_PTR) |
1129+ (t & VT_TYPE) == 0 |
1130+ (t & VT_ENUM)) {
1131+ *a = 4;
1132+ return 4;
5871133 } else {
5881134 *a = 1;
5891135 return 1;
5901136 }
5911137 }
5921138
593−/* return the number size in bytes of a given type */
594−int incr_value(t)
1139+/* return the pointed type of t */
1140+int pointed_type(int t)
5951141 {
596− int a;
597−
598− if ((t & VT_PTRMASK) >= VT_PTRINC)
599− return type_size(t - VT_PTRINC, &a);
600− else
601− return 1;
1142+ Sym *s;
1143+ s = sym_find(((unsigned)t >> VT_STRUCT_SHIFT));
1144+ return s->t | (t & VT_TYPEN);
6021145 }
6031146
604−#define POST_ADD 0x1000
605−#define PRE_ADD 0
606−
607−/* a defines POST/PRE add. c is the token ++ or -- */
608−void inc(a, c)
1147+int mk_pointer(int t)
6091148 {
610− test_lvalue();
611− vt = vt & VT_LVALN;
612− gv();
613− o(0x018bc189); /* movl %eax, %ecx ; mov (%ecx), %eax */
614− o(0x408d | a); /* leal x(%eax), %eax/%edx */
615− g((c - TOK_MID) * incr_value(vt));
616− o(0x0189 | a); /* mov %eax/%edx, (%ecx) */
1149+ int p;
1150+ p = anon_sym++;
1151+ sym_push(p, t, -1);
1152+ return VT_PTR | (p << VT_STRUCT_SHIFT) | (t & VT_TYPEN);
6171153 }
6181154
619−/* XXX: handle ptr sub and 'int + ptr' case (only 'ptr + int' handled) */
620−/* XXX: handle constant propagation (need to track live eax) */
621−/* XXX: handle unsigned propagation */
622−void gen_op(op, l)
1155+/* store value in lvalue pushed on stack */
1156+void vstore()
6231157 {
624− int t;
625− gv();
626− t = vt;
627− o(0x50); /* push %eax */
628− next();
629− if (l == -1)
630− expr();
631− else if (l == -2)
632− expr_eq();
633− else
634− sum(l);
635− gv();
636− o(0x59); /* pop %ecx */
637− if (op == '+' | op == '-') {
638− /* XXX: incorrect for short (futur!) */
639− if (incr_value(t) == 4)
640− o(0x02e0c1); /* shl $2, %eax */
641− if (op == '-')
642− o(0xd8f7); /* neg %eax */
643− o(0xc801); /* add %ecx, %eax */
644− vt = t;
645− } else if (op == '&')
646− o(0xc821);
647− else if (op == '^')
648− o(0xc831);
649− else if (op == '|')
650− o(0xc809);
651− else if (op == '*')
652− o(0xc1af0f); /* imul %ecx, %eax */
653−#ifndef TINY
654− else if (op == TOK_SHL | op == TOK_SHR) {
655− o(0xd391); /* xchg %ecx, %eax, shl/shr/sar %cl, %eax */
656− if (op == TOK_SHL)
657− o(0xe0);
658− else if (t & VT_UNSIGNED)
659− o(0xe8);
660− else
661− o(0xf8);
1158+ int ft, fc, r, t;
1159+
1160+ r = gv(); /* generate value */
1161+ vpush();
1162+ ft = vstack_ptr[-4];
1163+ fc = vstack_ptr[-3];
1164+ if ((ft & VT_VALMASK) == VT_LLOCAL) {
1165+ t = get_reg();
1166+ load(t, VT_LOCAL | VT_LVAL, fc);
1167+ ft = (ft & ~VT_VALMASK) | t;
6621168 }
663−#endif
664− else if (op == '/' | op == '%') {
665− o(0x91); /* xchg %ecx, %eax */
666− if (t & VT_UNSIGNED) {
667− o(0xd231); /* xor %edx, %edx */
668− o(0xf1f7); /* div %ecx, %eax */
669− } else {
670− o(0xf9f799); /* cltd, idiv %ecx, %eax */
671− }
672− if (op == '%')
673− o(0x92); /* xchg %edx, %eax */
674− } else {
675− o(0xc139); /* cmp %eax,%ecx */
676− vset(VT_CMP, op);
1169+ store(r, ft, fc);
1170+ vstack_ptr -= 4;
1171+}
1172+
1173+/* post defines POST/PRE add. c is the token ++ or -- */
1174+void inc(post, c)
1175+{
1176+ int r, r1;
1177+ test_lvalue();
1178+ if (post)
1179+ vpush(); /* room for returned value */
1180+ vpush(); /* save lvalue */
1181+ r = gv();
1182+ vpush(); /* save value */
1183+ if (post) {
1184+ /* duplicate value */
1185+ r1 = get_reg();
1186+ load(r1, r, 0); /* move r to r1 */
1187+ vstack_ptr[-6] = (vt & VT_TYPE) | r1;
1188+ vstack_ptr[-5] = 0;
6771189 }
1190+ /* add constant */
1191+ vset(VT_CONST, c - TOK_MID);
1192+ gen_op('+');
1193+ vstore(); /* store value */
1194+ if (post)
1195+ vpop(&vt, &vc);
6781196 }
6791197
6801198 int expr_const()
@@ -685,8 +1203,6 @@ int expr_const() · 4 unmodified lines
6851203 return vc;
6861204 }
6871205
688−#ifndef TINY
689−
6901206 /* enum/struct/union declaration */
6911207 int struct_decl(u)
6921208 {
@@ -695,7 +1211,6 @@ int struct_decl(u) · 2 unmodified lines
6951211
6961212 a = tok; /* save decl type */
6971213 next();
698− v = 0;
6991214 if (tok != '{') {
7001215 v = tok;
7011216 next();
@@ -707,6 +1222,8 @@ int struct_decl(u) · 5 unmodified lines
7071222 u = u | (v << VT_STRUCT_SHIFT);
7081223 return u;
7091224 }
1225+ } else {
1226+ v = anon_sym++;
7101227 }
7111228 s = sym_push(v | SYM_STRUCT, a, 0);
7121229 /* put struct/union/enum name in type */
@@ -733,7 +1250,7 @@ int struct_decl(u) · 20 unmodified lines
7331250 } else {
7341251 b = ist();
7351252 while (1) {
736− t = typ(&v, b, &size);
1253+ t = typ(&v, b);
7371254 if (t & (VT_FUNC | VT_TYPEDEF))
7381255 error("invalid type");
7391256 /* XXX: align & correct type size */
@@ -768,7 +1285,6 @@ int struct_decl(u) · 28 unmodified lines
7681285 }
7691286 return u;
7701287 }
771−#endif
7721288
7731289 /* return 0 if no type declaration. otherwise, return the basic type
7741290 and skip it.
@@ -783,16 +1299,18 @@ int ist() · 8 unmodified lines
7831299 while(1) {
7841300 #ifndef TINY
7851301 if (tok == TOK_ENUM) {
786− t = struct_decl(VT_ENUM);
1302+ t |= struct_decl(VT_ENUM);
7871303 } else if (tok == TOK_STRUCT || tok == TOK_UNION) {
788− t = struct_decl(VT_STRUCT);
1304+ t |= struct_decl(VT_STRUCT);
7891305 } else
7901306 #endif
7911307 {
792− if (tok == TOK_CHAR | tok == TOK_VOID) {
1308+ if (tok == TOK_CHAR) {
7931309 t |= VT_BYTE;
1310+ } else if (tok == TOK_VOID) {
1311+ t |= VT_VOID;
7941312 } else if (tok == TOK_INT |
795− (tok >= TOK_CONST & tok <= TOK_SIGNED)) {
1313+ (tok >= TOK_CONST & tok <= TOK_INLINE)) {
7961314 /* ignored types */
7971315 } else if (tok == TOK_FLOAT & tok == TOK_DOUBLE) {
7981316 error("floats not supported");
@@ -817,23 +1335,97 @@ int ist() · 18 unmodified lines
8171335 return t;
8181336 }
8191337
1338+int post_type(u, t)
1339+{
1340+ int p, n, pt, l;
1341+
1342+ if (tok == '(') {
1343+ /* function declaration */
1344+ next();
1345+ /* push a dummy symbol to force local symbol stack usage */
1346+ sym_push1(&local_stack, 0, 0, 0);
1347+ p = 4;
1348+ l = 0;
1349+ while (tok != ')') {
1350+ /* read param name and compute offset */
1351+ if (l != 2) {
1352+ if (!(pt = ist())) {
1353+ if (l) {
1354+ error("invalid type");
1355+ } else {
1356+ l = 2;
1357+ goto old_proto;
1358+ }
1359+ }
1360+ if (pt & VT_VOID && tok == ')')
1361+ break;
1362+ l = 1;
1363+ pt = typ(&n, pt); /* XXX: should accept
1364+ both arg/non arg if v == 0 */
1365+ } else {
1366+ old_proto:
1367+ n = tok;
1368+ pt = 0; /* int type */
1369+ next();
1370+ }
1371+ /* array must be transformed to pointer according to ANSI C */
1372+ pt &= ~VT_ARRAY;
1373+ p = p + 4;
1374+ sym_push(n, VT_LOCAL | VT_LVAL | pt, p);
1375+ if (tok == ',') {
1376+ next();
1377+ if (l == 1 && tok == TOK_DOTS) {
1378+ next();
1379+ break;
1380+ }
1381+ }
1382+ }
1383+ 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;
1390+ } else if (tok == '[') {
1391+ /* array definition */
1392+ next();
1393+ n = -1;
1394+ if (tok != ']') {
1395+ n = expr_const();
1396+ if (n < 0)
1397+ error("invalid array size");
1398+ }
1399+ skip(']');
1400+ /* parse next post type */
1401+ t = post_type(u, t);
1402+
1403+ /* we push a anonymous symbol which will contain the array
1404+ element type */
1405+ p = anon_sym++;
1406+ sym_push(p, t, n);
1407+ t = VT_ARRAY | VT_PTR | (p << VT_STRUCT_SHIFT);
1408+ }
1409+ return t;
1410+}
1411+
8201412 /* Read a type declaration (except basic type), and return the
8211413 type. If v is true, then also put variable name in 'vc' */
822−int typ(int *v, int t, int *array_size_ptr)
1414+int typ(int *v, int t)
8231415 {
824− int u, p, n;
1416+ int u;
8251417
8261418 t = t & -3; /* suppress the ored '2' */
8271419 while (tok == '*') {
8281420 next();
829− t = t + VT_PTRINC;
830− }
1421+ t = mk_pointer(t);
1422+ }
8311423
8321424 /* recursive type */
8331425 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
8341426 if (tok == '(') {
8351427 next();
836− u = typ(v, 0, 0);
1428+ u = typ(v, 0);
8371429 skip(')');
8381430 } else {
8391431 u = 0;
@@ -843,51 +1435,7 @@ int typ(int *v, int t, int *array_size_ptr) · 3 unmodified lines
8431435 next();
8441436 }
8451437 }
846− while(1) {
847− if (tok == '(') {
848− /* function declaration */
849− next();
850− /* push a dummy symbol to force local symbol stack usage */
851− sym_push1(&local_stack, 0, 0, 0);
852− p = 4;
853− while (tok != ')') {
854− /* read param name and compute offset */
855− if (t = ist())
856− t = typ(&n, t, 0); /* XXX: should accept both arg/non arg if v == 0 */
857− else {
858− n = tok;
859− t = 0;
860− next();
861− }
862− p = p + 4;
863− sym_push(n, VT_LOCAL | VT_LVAL | t, p);
864− if (tok == ',')
865− next();
866− }
867− next(); /* skip ')' */
868− if (u)
869− t = u + VT_BYTE;
870− else
871− t = t | VT_FUNC;
872− } else if (tok == '[') {
873− /* array definition */
874− if (t & VT_ARRAY)
875− error("multi dimension arrays not supported");
876− next();
877− n = 0;
878− if (tok != ']') {
879− n = expr_const();
880− if (array_size_ptr)
881− *array_size_ptr = n;
882− }
883− if (n <= 0 & array_size_ptr != 0)
884− error("invalid array size");
885− skip(']');
886− t = (t + VT_PTRINC) | VT_ARRAY;
887− } else
888− break;
889− }
890− return t;
1438+ return post_type(u, t);
8911439 }
8921440
8931441 /* define a new external reference to a function 'v' of type 'u' */
@@ -911,88 +1459,65 @@ Sym *external_func(v, u) · 17 unmodified lines
9111459 return s;
9121460 }
9131461
914−/* read a number in base b */
915−int getn(c, b)
1462+/* read a character for string or char constant and eval escape codes */
1463+int getq()
9161464 {
917− int n, t;
918− n = 0;
919−#ifndef TINY
920− while (1) {
921− if (c >= 'a')
922− t = c - 'a' + 10;
923− else if (c >= 'A')
924− t = c - 'A' + 10;
925− else
926− t = c - '0';
927− if (t < 0 | t >= b)
928− break;
929− n = n * b + t;
930− c = inp();
931− }
932−#else
933− while (isnum(c)) {
934− n = n * b + c - '0';
935− c = inp();
1465+ int c;
1466+
1467+ c = ch;
1468+ minp();
1469+ if (c == '\\') {
1470+ if (isnum(ch)) {
1471+ return getn(8);
1472+ } else {
1473+ if (ch == 'n')
1474+ c = '\n';
1475+ else if (ch == 'r')
1476+ c = '\r';
1477+ else if (ch == 't')
1478+ c = '\t';
1479+ else
1480+ c = ch;
1481+ minp();
1482+ }
9361483 }
937−#endif
938− ungetc(c, file);
939− return n;
1484+ return c;
9401485 }
9411486
942−int getq(n)
1487+void indir()
9431488 {
944− if (n == '\\') {
945− n = inp();
946− if (n == 'n')
947− n = '\n';
948−#ifndef TINY
949− else if (n == 'r')
950− n = '\r';
951− else if (n == 't')
952− n = '\t';
953−#endif
954− else if (isnum(n))
955− n = getn(n, 8);
956− }
957− return n;
1489+ if (vt & VT_LVAL)
1490+ gv();
1491+ if (!(vt & VT_PTR))
1492+ expect("pointer");
1493+ vt = pointed_type(vt);
1494+ if (!(vt & VT_ARRAY)) /* an array is never an lvalue */
1495+ vt |= VT_LVAL;
9581496 }
9591497
9601498 void unary()
9611499 {
962− int n, t, ft, fc, p;
1500+ int n, t, ft, fc, p, r;
9631501 Sym *s;
9641502
965− if (isnum(tok)) {
966− /* number */
967−#ifndef TINY
968− t = 10;
969− if (tok == '0') {
970− t = 8;
971− tok = inp();
972− if (tok == 'x') {
973− t = 16;
974− tok = inp();
975− }
976− }
977− vset(VT_CONST, getn(tok, t));
978−#else
979− vset(VT_CONST, getn(tok, 10));
980−#endif
1503+ if (tok == TOK_NUM) {
1504+ vset(VT_CONST, tokc);
9811505 next();
9821506 } else
983−#ifndef TINY
9841507 if (tok == '\'') {
985− vset(VT_CONST, getq(inp()));
1508+ vset(VT_CONST, getq());
9861509 next(); /* skip char */
9871510 skip('\'');
988− } else
989−#endif
990− if (tok == '\"') {
991− vset(VT_CONST | VT_PTRINC | VT_BYTE, glo);
1511+ } else if (tok == '\"') {
1512+ /* generate (char *) type */
1513+ vset(VT_CONST | mk_pointer(VT_TYPE), glo);
9921514 while (tok == '\"') {
993− while((n = inp()) != 34) {
994− *(char *)glo++ = getq(n);
1515+ while (ch != '\"') {
1516+ if (ch == -1)
1517+ error("unterminated string");
1518+ *(char *)glo++ = getq();
9951519 }
1520+ minp();
9961521 next();
9971522 }
9981523 *(char *)glo++ = 0;
@@ -1002,7 +1527,7 @@ void unary() · 3 unmodified lines
10021527 if (t == '(') {
10031528 /* cast ? */
10041529 if (t = ist()) {
1005− ft = typ(0, t, 0);
1530+ ft = typ(0, t);
10061531 skip(')');
10071532 unary();
10081533 vt = (vt & VT_TYPEN) | ft;
@@ -1012,50 +1537,50 @@ void unary() · 3 unmodified lines
10121537 }
10131538 } else if (t == '*') {
10141539 unary();
1015− if (vt & VT_LVAL)
1016− gv();
1017−#ifndef TINY
1018− if (!(vt & VT_PTRMASK))
1019− expect("pointer");
1020−#endif
1021− vt = (vt - VT_PTRINC) | VT_LVAL;
1540+ indir();
10221541 } else if (t == '&') {
10231542 unary();
10241543 test_lvalue();
1025− vt = (vt & VT_LVALN) + VT_PTRINC;
1544+ vt = mk_pointer(vt & VT_LVALN);
10261545 } else
1027−#ifndef TINY
10281546 if (t == '!') {
10291547 unary();
1030− if (vt & VT_CMP)
1548+ if ((vt & VT_VALMASK) == VT_CMP)
10311549 vc = vc ^ 1;
10321550 else
10331551 vset(VT_JMP, gtst(1, 0));
10341552 } else
10351553 if (t == '~') {
10361554 unary();
1037− if ((vt & (VT_CONST | VT_LVAL)) == VT_CONST)
1038− vc = ~vc;
1039− else {
1040− gv();
1041− o(0xd0f7);
1042− }
1555+ vpush();
1556+ vset(VT_CONST, -1);
1557+ gen_op('^');
10431558 } else
10441559 if (t == '+') {
10451560 unary();
10461561 } else
1047−#endif
1562+ if (t == TOK_SIZEOF) {
1563+ /* XXX: some code can be generated */
1564+ if (tok == '(') {
1565+ next();
1566+ if (t = ist())
1567+ vt = typ(0, t);
1568+ else
1569+ expr();
1570+ skip(')');
1571+ } else {
1572+ unary();
1573+ }
1574+ vset(VT_CONST, type_size(vt, &t));
1575+ } else
10481576 if (t == TOK_INC | t == TOK_DEC) {
10491577 unary();
1050− inc(PRE_ADD, t);
1578+ inc(0, t);
10511579 } else if (t == '-') {
1580+ vset(VT_CONST, 0);
1581+ vpush();
10521582 unary();
1053− if ((vt & (VT_CONST | VT_LVAL)) == VT_CONST)
1054− vc = -vc;
1055− else {
1056− gv();
1057− o(0xd8f7); /* neg %eax */
1058− }
1583+ gen_op('-');
10591584 } else
10601585 {
10611586 s = sym_find(t);
@@ -1076,17 +1601,17 @@ void unary() · 14 unmodified lines
10761601 /* post operations */
10771602 while (1) {
10781603 if (tok == TOK_INC | tok == TOK_DEC) {
1079− inc(POST_ADD, tok);
1604+ inc(1, tok);
10801605 next();
10811606 } else if (tok == '.' | tok == TOK_ARROW) {
10821607 /* field */
1083− if (tok == '.') {
1084− test_lvalue();
1085− vt = (vt & VT_LVALN) + VT_PTRINC;
1086− }
1608+ if (tok == TOK_ARROW)
1609+ indir();
1610+ test_lvalue();
1611+ vt &= VT_LVALN;
10871612 next();
10881613 /* expect pointer on structure */
1089− if (!(vt & VT_STRUCT) || (vt & VT_PTRMASK) == 0)
1614+ if (!(vt & VT_STRUCT))
10901615 expect("struct or union");
10911616 s = sym_find(((unsigned)vt >> VT_STRUCT_SHIFT) | SYM_STRUCT);
10921617 /* find field */
@@ -1098,40 +1623,39 @@ void unary() · 5 unmodified lines
10981623 if (!s)
10991624 error("field not found");
11001625 /* add field offset to pointer */
1101− gv();
1102− if (s->c)
1103− oad(0x05, s->c);
1626+ vt = vt & VT_TYPEN; /* change type to int */
1627+ vpush();
1628+ vset(VT_CONST, s->c);
1629+ gen_op('+');
11041630 /* change type to field type, and set to lvalue */
11051631 vt = (vt & VT_TYPEN) | VT_LVAL | s->t;
11061632 next();
11071633 } else if (tok == '[') {
1108−#ifndef TINY
1109− if (!(vt & VT_PTRMASK))
1110− expect("pointer");
1111−#endif
1112− gen_op('+', -1);
1113− /* dereference pointer */
1114− vt = (vt - VT_PTRINC) | VT_LVAL;
1634+ next();
1635+ vpush();
1636+ expr();
1637+ gen_op('+');
1638+ indir();
11151639 skip(']');
11161640 } else if (tok == '(') {
11171641 /* function call */
1642+ save_regs(); /* save used temporary registers */
11181643 /* lvalue is implied */
11191644 vt = vt & VT_LVALN;
1120− if ((vt & VT_CONST) == 0) {
1645+ if ((vt & VT_VALMASK) != VT_CONST) {
11211646 /* evaluate function address */
1122− gv();
1123− o(0x50); /* push %eax */
1647+ r = gv();
1648+ o(0x50 + r); /* push r */
11241649 }
11251650 ft = vt;
11261651 fc = vc;
1127−
11281652 next();
11291653 t = 0;
11301654 while (tok != ')') {
11311655 t = t + 4;
11321656 expr_eq();
1133− gv();
1134− o(0x50); /* push %eax */
1657+ r = gv();
1658+ o(0x50 + r); /* push r */
11351659 if (tok == ',')
11361660 next();
11371661 }
@@ -1148,19 +1672,19 @@ void unary() · 10 unmodified lines
11481672 n = n + 4;
11491673 p = p - 4;
11501674 }
1151− if (ft & VT_CONST) {
1675+ if ((ft & VT_VALMASK) == VT_CONST) {
11521676 /* forward reference */
11531677 if (ft & VT_FORWARD) {
11541678 *(int *)fc = psym(0xe8, *(int *)fc);
11551679 } else
11561680 oad(0xe8, fc - ind - 5);
1157− /* return value is variable, and take type from function proto */
1158− vt = VT_VAR | (ft & VT_TYPE & VT_FUNCN);
1681+ /* return value is %eax, and take type from function proto */
1682+ vt = 0 | (ft & VT_TYPE & VT_FUNCN);
11591683 } else {
11601684 oad(0x2494ff, t); /* call *xxx(%esp) */
11611685 t = t + 4;
1162− /* return value is variable, int */
1163− vt = VT_VAR;
1686+ /* return value is %eax, integer */
1687+ vt = 0;
11641688 }
11651689 if (t)
11661690 oad(0xc481, t);
@@ -1172,60 +1696,52 @@ void unary() · 5 unmodified lines
11721696
11731697 void uneq()
11741698 {
1175− int ft, fc, b;
1699+ int t;
11761700
11771701 unary();
11781702 if (tok == '=' |
1179− (tok >= TOK_A_MOD & TOK_A_DIV) |
1703+ (tok >= TOK_A_MOD & tok <= TOK_A_DIV) |
11801704 tok == TOK_A_XOR | tok == TOK_A_OR |
1181− tok == TOK_A_SHL | tok == TOK_A_SHR) {
1705+ tok == TOK_A_SHL | tok == TOK_A_SAR) {
11821706 test_lvalue();
1183− fc = vc;
1184− ft = vt;
1185− b = (vt & VT_TYPE) == VT_BYTE;
1186− if (ft & VT_VAR)
1187− o(0x50); /* push %eax */
1188− if (tok == '=') {
1189− next();
1707+ vpush();
1708+ t = tok;
1709+ next();
1710+ if (t == '=') {
11901711 expr_eq();
1191−#ifndef TINY
1192− if ((vt & VT_PTRMASK) != (ft & VT_PTRMASK))
1712+ /* XXX: be more precise */
1713+ if ((vt & VT_PTR) != (vstack_ptr[-2] & VT_PTR))
11931714 warning("incompatible type");
1194−#endif
1195− gv(); /* generate value */
1196− } else
1197− gen_op(tok & 0x7f, -2); /* XXX: incorrect, must call expr_eq */
1198−
1199− if (ft & VT_VAR) {
1200− o(0x59); /* pop %ecx */
1201− o(0x0189 - b); /* mov %eax/%al, (%ecx) */
1202− } else if (ft & VT_LOCAL)
1203− oad(0x8589 - b, fc); /* mov %eax/%al,xxx(%ebp) */
1204− else
1205− oad(0xa3 - b, fc); /* mov %eax/%al,xxx */
1715+ } else {
1716+ vpush();
1717+ expr_eq();
1718+ gen_op(t & 0x7f);
1719+ }
1720+ vstore();
12061721 }
12071722 }
12081723
12091724 void sum(l)
12101725 {
1211−#ifndef TINY
1212− int t;
1213−#endif
1726+ int ft, fc, t;
1727+
12141728 if (l == 0)
12151729 uneq();
12161730 else {
12171731 sum(--l);
12181732 while ((l == 0 & (tok == '*' | tok == '/' | tok == '%')) |
12191733 (l == 1 & (tok == '+' | tok == '-')) |
1220−#ifndef TINY
1221− (l == 2 & (tok == TOK_SHL | tok == TOK_SHR)) |
1222−#endif
1734+ (l == 2 & (tok == TOK_SHL | tok == TOK_SAR)) |
12231735 (l == 3 & (tok >= TOK_LT & tok <= TOK_GT)) |
12241736 (l == 4 & (tok == TOK_EQ | tok == TOK_NE)) |
12251737 (l == 5 & tok == '&') |
12261738 (l == 6 & tok == '^') |
12271739 (l == 7 & tok == '|')) {
1228− gen_op(tok, l);
1740+ vpush();
1741+ t = tok;
1742+ next();
1743+ sum(l);
1744+ gen_op(t);
12291745 }
12301746 }
12311747 }
@@ -1246,7 +1762,7 @@ void eand() · 14 unmodified lines
12461762 if (tok != TOK_LAND) {
12471763 if (t) {
12481764 t = gtst(1, t);
1249− vset(VT_JMP | 1, t);
1765+ vset(VT_JMPI, t);
12501766 }
12511767 break;
12521768 }
@@ -1287,7 +1803,7 @@ void expr_eq() · 34 unmodified lines
12871803 expr();
12881804 gv();
12891805 skip(':');
1290− u = psym(0xe9, 0);
1806+ u = gjmp(0);
12911807 gsym(t);
12921808 expr_eq();
12931809 gv();
@@ -1307,7 +1823,7 @@ void expr() · 13 unmodified lines
13071823
13081824 #endif
13091825
1310−void block(int *bsym, int *csym, int *case_sym, int *def_sym)
1826+void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg)
13111827 {
13121828 int a, b, c, d;
13131829 Sym *s;
@@ -1319,13 +1835,13 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym) · 5 unmodified lines
13191835 expr();
13201836 skip(')');
13211837 a = gtst(1, 0);
1322− block(bsym, csym, case_sym, def_sym);
1838+ block(bsym, csym, case_sym, def_sym, case_reg);
13231839 c = tok;
13241840 if (c == TOK_ELSE) {
13251841 next();
1326− d = psym(0xe9, 0); /* jmp */
1842+ d = gjmp(0);
13271843 gsym(a);
1328− block(bsym, csym, case_sym, def_sym);
1844+ block(bsym, csym, case_sym, def_sym, case_reg);
13291845 gsym(d); /* patch else jmp */
13301846 } else
13311847 gsym(a);
@@ -1337,7 +1853,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym) · 5 unmodified lines
13371853 skip(')');
13381854 a = gtst(1, 0);
13391855 b = 0;
1340− block(&a, &b, case_sym, def_sym);
1856+ block(&a, &b, case_sym, def_sym, case_reg);
13411857 oad(0xe9, d - ind - 5); /* jmp */
13421858 gsym(a);
13431859 gsym_addr(b, d);
@@ -1347,7 +1863,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym) · 3 unmodified lines
13471863 s = local_stack;
13481864 decl(VT_LOCAL);
13491865 while (tok != '}')
1350− block(bsym, csym, case_sym, def_sym);
1866+ block(bsym, csym, case_sym, def_sym, case_reg);
13511867 /* pop locally defined symbols */
13521868 sym_pop(&local_stack, s);
13531869 next();
@@ -1355,22 +1871,22 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym) · 1 unmodified line
13551871 next();
13561872 if (tok != ';') {
13571873 expr();
1358− gv();
1874+ move_reg(0, gv());
13591875 }
13601876 skip(';');
1361− rsym = psym(0xe9, rsym); /* jmp */
1877+ rsym = gjmp(rsym); /* jmp */
13621878 } else if (tok == TOK_BREAK) {
13631879 /* compute jump */
13641880 if (!bsym)
13651881 error("cannot break");
1366− *bsym = psym(0xe9, *bsym);
1882+ *bsym = gjmp(*bsym);
13671883 next();
13681884 skip(';');
13691885 } else if (tok == TOK_CONTINUE) {
13701886 /* compute jump */
13711887 if (!csym)
13721888 error("cannot continue");
1373− *csym = psym(0xe9, *csym);
1889+ *csym = gjmp(*csym);
13741890 next();
13751891 skip(';');
13761892 } else
@@ -1392,14 +1908,14 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym) · 15 unmodified lines
13921908 }
13931909 skip(';');
13941910 if (tok != ')') {
1395− e = psym(0xe9, 0);
1911+ e = gjmp(0);
13961912 c = ind;
13971913 expr();
13981914 oad(0xe9, d - ind - 5); /* jmp */
13991915 gsym(e);
14001916 }
14011917 skip(')');
1402− block(&a, &b, case_sym, def_sym);
1918+ block(&a, &b, case_sym, def_sym, case_reg);
14031919 oad(0xe9, c - ind - 5); /* jmp */
14041920 gsym(a);
14051921 gsym_addr(b, c);
@@ -1409,7 +1925,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym) · 3 unmodified lines
14091925 a = 0;
14101926 b = 0;
14111927 d = ind;
1412− block(&a, &b, case_sym, def_sym);
1928+ block(&a, &b, case_sym, def_sym, case_reg);
14131929 skip(TOK_WHILE);
14141930 skip('(');
14151931 gsym(b);
@@ -1423,12 +1939,12 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym) · 7 unmodified lines
14231939 next();
14241940 skip('(');
14251941 expr();
1426− gv();
1942+ case_reg = gv();
14271943 skip(')');
14281944 a = 0;
14291945 b = 0;
14301946 c = 0;
1431− block(&a, csym, &b, &c);
1947+ block(&a, csym, &b, &c, case_reg);
14321948 /* if no default, jmp after switch */
14331949 if (c == 0)
14341950 c = ind;
@@ -1443,10 +1959,13 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym) · 8 unmodified lines
14431959 if (!case_sym)
14441960 expect("switch");
14451961 gsym(*case_sym);
1446− oad(0x3d, a); /* cmp $xxx, %eax */
1447− *case_sym = psym(0x850f, 0); /* jne xxx */
1962+ vset(case_reg, 0);
1963+ vpush();
1964+ vset(VT_CONST, a);
1965+ gen_op(TOK_EQ);
1966+ *case_sym = gtst(1, 0);
14481967 skip(':');
1449− block(bsym, csym, case_sym, def_sym);
1968+ block(bsym, csym, case_sym, def_sym, case_reg);
14501969 } else
14511970 if (tok == TOK_DEFAULT) {
14521971 next();
@@ -1456,7 +1975,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym) · 3 unmodified lines
14561975 if (*def_sym)
14571976 error("too many 'default'");
14581977 *def_sym = ind;
1459− block(bsym, csym, case_sym, def_sym);
1978+ block(bsym, csym, case_sym, def_sym, case_reg);
14601979 } else
14611980 if (tok == TOK_GOTO) {
14621981 next();
@@ -1466,7 +1985,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym) · 3 unmodified lines
14661985 s = sym_push1(&label_stack, tok, VT_FORWARD, 0);
14671986 /* label already defined */
14681987 if (s->t & VT_FORWARD)
1469− s->c = psym(0xe9, s->c); /* jmp xxx */
1988+ s->c = gjmp(s->c); /* jmp xxx */
14701989 else
14711990 oad(0xe9, s->c - ind - 5); /* jmp xxx */
14721991 next();
@@ -1489,7 +2008,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym) · 16 unmodified lines
14892008 } else {
14902009 sym_push1(&label_stack, b, 0, ind);
14912010 }
1492− block(bsym, csym, case_sym, def_sym);
2011+ block(bsym, csym, case_sym, def_sym, case_reg);
14932012 } else {
14942013 /* expression case: go backward of one token */
14952014 /* XXX: currently incorrect if number/string/char */
@@ -1506,8 +2025,8 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym) · 10 unmodified lines
15062025 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
15072026 void decl(l)
15082027 {
1509− int *a, t, b, s, align, v, u, n;
1510− Sym *sym, *slocal;
2028+ int *a, t, b, size, align, v, u, n;
2029+ Sym *sym;
15112030
15122031 while (b = ist()) {
15132032 if ((b & (VT_ENUM | VT_STRUCT)) && tok == ';') {
@@ -1516,9 +2035,7 @@ void decl(l) · 2 unmodified lines
15162035 continue;
15172036 }
15182037 while (1) { /* iterate thru each declaration */
1519− s = 1;
1520− slocal = local_stack; /* save local stack position, to restore it */
1521− t = typ(&v, b, &s);
2038+ t = typ(&v, b);
15222039 if (tok == '{') {
15232040 /* patch forward references */
15242041 if ((sym = sym_find(v)) && (sym->t & VT_FORWARD)) {
@@ -1533,7 +2050,7 @@ void decl(l) · 8 unmodified lines
15332050 o(0xe58955); /* push %ebp, mov %esp, %ebp */
15342051 a = (int *)oad(0xec81, 0); /* sub $xxx, %esp */
15352052 rsym = 0;
1536− block(0, 0, 0, 0);
2053+ block(0, 0, 0, 0, 0);
15372054 gsym(rsym);
15382055 o(0xc3c9); /* leave, ret */
15392056 *a = (-loc + 3) & -4; /* align local size to word &
@@ -1542,13 +2059,12 @@ void decl(l) · 2 unmodified lines
15422059 sym_pop(&local_stack, 0); /* reset local stack */
15432060 break;
15442061 } else {
1545− /* reset local stack (needed because of dummy function
1546− parameters */
1547− sym_pop(&local_stack, slocal);
1548− if (t & VT_TYPEDEF) {
2062+ if (b & VT_TYPEDEF) {
15492063 /* save typedefed type */
1550− sym_push(v, t, 0);
2064+ sym_push(v, t | VT_TYPEDEF, 0);
15512065 } else if (t & VT_FUNC) {
2066+ /* XXX: incorrect to flush, but needed while
2067+ waiting for function prototypes */
15522068 /* external function definition */
15532069 external_func(v, t);
15542070 } else {
@@ -1567,19 +2083,18 @@ void decl(l) · 12 unmodified lines
15672083 if (t & VT_STATIC)
15682084 u = VT_CONST;
15692085 u |= t;
1570− if (t & VT_ARRAY)
1571− t -= VT_PTRINC;
1572− align = type_size(t, &align);
1573− s *= align;
1574− if (u & VT_LOCAL) {
2086+ size = type_size(t, &align);
2087+ if (size < 0)
2088+ error("invalid size");
2089+ if ((u & VT_VALMASK) == VT_LOCAL) {
15752090 /* allocate space down on the stack */
1576− loc = (loc - s) & -align;
2091+ loc = (loc - size) & -align;
15772092 sym_push(v, u, loc);
15782093 } else {
15792094 /* allocate space up in the data space */
15802095 glo = (glo + align - 1) & -align;
15812096 sym_push(v, u, glo);
1582− glo += s;
2097+ glo += size;
15832098 }
15842099 }
15852100 }
@@ -1598,35 +2113,34 @@ int main(int c, char **v) · 12 unmodified lines
15982113 Sym *s;
15992114 int (*t)();
16002115 if (c < 2) {
1601− printf("usage: tc src\n");
2116+ printf("usage: tcc source ...\n");
16022117 return 1;
16032118 }
16042119 v++;
16052120 filename = *v;
2121+ line_num = 1;
16062122 file = fopen(filename, "r");
1607−#ifndef TINY
16082123 if (!file) {
16092124 perror(filename);
16102125 exit(1);
16112126 }
1612−#endif
1613−
2127+ include_stack_ptr = include_stack;
2128+
16142129 idtable = malloc(SYM_TABLE_SIZE);
1615−#ifdef TINY
1616− memcpy(idtable,
1617− "int\0void\0char\0if\0else\0while\0break\0return\0define\0main", 53);
1618− idptr = idtable + 53;
1619−#else
16202130 memcpy(idtable,
1621− "int\0void\0char\0if\0else\0while\0break\0return\0define\0main\0for\0extern\0static\0unsigned\0goto\0do\0continue\0switch\0case\0const\0volatile\0long\0register\0signed\0float\0double\0struct\0union\0typedef\0default\0enum", 192);
1622− idptr = idtable + 192;
1623−#endif
2131+ "int\0void\0char\0if\0else\0while\0break\0return\0define\0main\0for\0extern\0static\0unsigned\0goto\0do\0continue\0switch\0case\0const\0volatile\0long\0register\0signed\0auto\0inline\0float\0double\0struct\0union\0typedef\0default\0enum\0sizeof\0include", 219);
2132+ idptr = idtable + 219;
2133+
16242134 glo = malloc(DATA_SIZE);
2135+ memset((void *)glo, 0, DATA_SIZE);
16252136 prog = malloc(TEXT_SIZE);
1626− macro_stack = malloc(256);
2137+ vstack = malloc(256);
2138+ vstack_ptr = vstack;
16272139 macro_stack_ptr = macro_stack;
2140+ anon_sym = 1 << (31 - VT_STRUCT_SHIFT);
16282141 ind = prog;
1629− line_num = 1;
2142+ inp();
2143+ ch = '\n'; /* needed to parse correctly first preprocessor command */
16302144 next();
16312145 decl(VT_CONST);
16322146 #ifdef TEST

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