History

eval function args in reverse order - added short and unsigned char support - added more messages

bellard committed 25 years ago

Commit 605a0281 parent d2614521 file · +99 −31Browse files

Unified · Split

tcc.c+99−31
@@ -23,15 +23,21 @@ · 22 unmodified lines
2323 /* preprocessor debug */
2424 //#define PP_DEBUG
2525
26−#define TEXT_SIZE 50000
27−#define DATA_SIZE 50000
26+/* these sizes are dummy for unix, because malloc() does not use
27+ memory when the pages are not used */
28+#define TEXT_SIZE (4*1024*1024)
29+#define DATA_SIZE (4*1024*1024)
30+
2831 #define INCLUDE_STACK_SIZE 32
2932 #define IFDEF_STACK_SIZE 64
3033 #define VSTACK_SIZE 64
3134 #define STRING_MAX_SIZE 1024
3235 #define INCLUDE_PATHS_MAX 32
3336
37+/* number of available temporary registers */
3438 #define NB_REGS 3
39+/* defined if function parameters must be evaluated in revert order */
40+#define INVERT_FUNC_PARAMS
3541
3642 /* token symbol management */
3743 typedef struct TokenSym {
@@ -107,8 +113,9 @@ int nb_include_paths; · 69 unmodified lines
107113 #define VT_FORWARD 0x0020 /* value is forward reference
108114 (only used for functions) */
109115
116+/* types */
110117 #define VT_VOID 0x00040
111−#define VT_BYTE 0x00080 /* byte type */
118+#define VT_BYTE 0x00080 /* signed byte type */
112119 #define VT_PTR 0x00100 /* pointer increment */
113120 #define VT_UNSIGNED 0x00200 /* unsigned type */
114121 #define VT_ARRAY 0x00400 /* array type (only used in parsing) */
@@ -116,17 +123,15 @@ int nb_include_paths; · 1 unmodified line
116123 #define VT_FUNC 0x01000 /* function type */
117124 #define VT_STRUCT 0x002000 /* struct/union definition */
118125 #define VT_TYPEDEF 0x004000 /* typedef definition */
119−#define VT_EXTERN 0x008000 /* extern definition */
120−#define VT_STATIC 0x010000 /* static variable */
121−#define VT_STRUCT_SHIFT 17 /* structure/enum name shift (12 bits lefts) */
126+#define VT_EXTERN 0x008000 /* extern definition */
127+#define VT_STATIC 0x010000 /* static variable */
128+#define VT_SHORT 0x020000 /* short type */
129+#define VT_STRUCT_SHIFT 18 /* structure/enum name shift (14 bits left) */
122130
123131 #define VT_TYPE 0xffffffc0 /* type mask */
124132 #define VT_TYPEN 0x0000003f /* ~VT_TYPE */
125133 #define VT_FUNCN -4097 /* ~VT_FUNC */
126134
127−
128−/* Special infos */
129−
130135 /* token values */
131136
132137 /* warning: the following compare tokens depend on i386 asm code */
@@ -516,7 +521,6 @@ void minp() · 383 unmodified lines
516521 }
517522
518523 /* same as minp, but also skip comments */
519−/* XXX: skip strings & chars */
520524 void cinp()
521525 {
522526 int c;
@@ -1196,6 +1200,12 @@ void macro_subst(int **tok_str, int *tok_len, · 673 unmodified lines
11961200 expect(")");
11971201 /* now subst each arg */
11981202 mstr = macro_arg_subst(nested_list, mstr, args);
1203+ /* free memory */
1204+ sa = args;
1205+ while (sa) {
1206+ free((int *)sa->c);
1207+ sa = sa->prev;
1208+ }
11991209 sym_pop(&args, NULL);
12001210 mstr_allocated = 1;
12011211 }
@@ -1335,6 +1345,12 @@ void load(r, ft, fc) · 133 unmodified lines
13351345 }
13361346 if ((ft & VT_TYPE) == VT_BYTE)
13371347 o(0xbe0f); /* movsbl */
1348+ else if ((ft & VT_TYPE) == (VT_BYTE | VT_UNSIGNED))
1349+ o(0xb60f); /* movzbl */
1350+ else if ((ft & VT_TYPE) == VT_SHORT)
1351+ o(0xbf0f); /* movswl */
1352+ else if ((ft & VT_TYPE) == (VT_SHORT | VT_UNSIGNED))
1353+ o(0xb70f); /* movzwl */
13381354 else
13391355 o(0x8b); /* movl */
13401356 if (v == VT_CONST) {
@@ -1376,6 +1392,9 @@ void store(r, ft, fc) · 35 unmodified lines
13761392
13771393 fr = ft & VT_VALMASK;
13781394 b = (ft & VT_TYPE) == VT_BYTE;
1395+ /* XXX: incorrect if reg to reg */
1396+ if (ft & VT_SHORT)
1397+ o(0x66);
13791398 o(0x89 - b);
13801399 if (fr == VT_CONST) {
13811400 oad(0x05 + r * 8, fc); /* mov r,xxx */
@@ -1388,6 +1407,11 @@ void store(r, ft, fc) · 6 unmodified lines
13881407 }
13891408 }
13901409
1410+void gfunc_param(void)
1411+{
1412+ o(0x50 + gv()); /* push r */
1413+}
1414+
13911415 int gjmp(t)
13921416 {
13931417 return psym(0xe9, t);
@@ -1797,6 +1821,9 @@ int type_size(int t, int *a) · 403 unmodified lines
17971821 (t & VT_ENUM)) {
17981822 *a = 4;
17991823 return 4;
1824+ } else if (t & VT_SHORT) {
1825+ *a = 2;
1826+ return 2;
18001827 } else {
18011828 *a = 1;
18021829 return 1;
@@ -1967,7 +1994,9 @@ int ist(void) · 164 unmodified lines
19671994 t |= VT_BYTE;
19681995 } else if (tok == TOK_VOID) {
19691996 t |= VT_VOID;
1970− } else if (tok == TOK_INT | tok == TOK_SHORT |
1997+ } else if (tok == TOK_SHORT) {
1998+ t |= VT_SHORT;
1999+ } else if (tok == TOK_INT |
19712000 (tok >= TOK_CONST & tok <= TOK_INLINE)) {
19722001 /* ignored types */
19732002 } else if (tok == TOK_FLOAT || tok == TOK_DOUBLE) {
@@ -2308,28 +2337,64 @@ void unary() · 334 unmodified lines
23082337 ft = vt;
23092338 fc = vc;
23102339 next();
2340+#ifdef INVERT_FUNC_PARAMS
2341+ {
2342+ int *str, len, parlevel, *saved_macro_ptr;
2343+ Sym *args;
2344+
2345+ /* read each argument and store it on a stack */
2346+ /* XXX: merge it with macro args ? */
2347+ args = NULL;
2348+ while (tok != ')') {
2349+ len = 0;
2350+ str = NULL;
2351+ parlevel = 0;
2352+ while ((parlevel > 0 || (tok != ')' && tok != ',')) &&
2353+ tok != -1) {
2354+ if (tok == '(')
2355+ parlevel++;
2356+ else if (tok == ')')
2357+ parlevel--;
2358+ tok_add2(&str, &len, tok, tokc);
2359+ next();
2360+ }
2361+ tok_add(&str, &len, -1); /* end of file added */
2362+ tok_add(&str, &len, 0);
2363+ sym_push1(&args, 0, 0, (int)str);
2364+ if (tok != ',')
2365+ break;
2366+ next();
2367+ }
2368+ if (tok != ')')
2369+ expect(")");
2370+
2371+ /* now generate code in reverse order by reading the stack */
2372+ saved_macro_ptr = macro_ptr;
2373+ t = 0;
2374+ while (args) {
2375+ t += 4;
2376+ macro_ptr = (int *)args->c;
2377+ next();
2378+ expr_eq();
2379+ gfunc_param();
2380+ free((int *)args->c);
2381+ sym_pop(&args, args->prev);
2382+ }
2383+ macro_ptr = saved_macro_ptr;
2384+ /* restore token */
2385+ tok = ')';
2386+ }
2387+#else
23112388 t = 0;
23122389 while (tok != ')') {
2313− t = t + 4;
2390+ t += 4;
23142391 expr_eq();
2315− r = gv();
2316− o(0x50 + r); /* push r */
2392+ gfunc_param();
23172393 if (tok == ',')
23182394 next();
23192395 }
2396+#endif
23202397 skip(')');
2321− /* horrible, but needed : convert to native ordering (could
2322− parse parameters in reverse order, but would cost more
2323− code) */
2324− n = 0;
2325− p = t - 4;
2326− while (n < p) {
2327− oad(0x24848b, p); /* mov x(%esp,1), %eax */
2328− oad(0x248487, n); /* xchg x(%esp,1), %eax */
2329− oad(0x248489, p); /* mov %eax, x(%esp,1) */
2330− n = n + 4;
2331− p = p - 4;
2332− }
23332398 if ((ft & VT_VALMASK) == VT_CONST) {
23342399 /* forward reference */
23352400 if (ft & VT_FORWARD) {
@@ -2450,7 +2515,7 @@ void eor() · 114 unmodified lines
24502515 /* XXX: better constant handling */
24512516 void expr_eq()
24522517 {
2453− int t, u, c;
2518+ int t, u, c, r1, r2;
24542519
24552520 if (const_wanted) {
24562521 sum(10);
@@ -2470,12 +2535,14 @@ void expr_eq() · 13 unmodified lines
24702535 next();
24712536 t = gtst(1, 0);
24722537 expr();
2473− gv();
2538+ r1 = gv();
24742539 skip(':');
24752540 u = gjmp(0);
24762541 gsym(t);
24772542 expr_eq();
2478− gv();
2543+ r2 = gv();
2544+ move_reg(r1, r2);
2545+ vt = (vt & VT_TYPE) | r1;
24792546 gsym(u);
24802547 }
24812548 }
@@ -2835,7 +2902,8 @@ int main(int argc, char **argv) · 353 unmodified lines
28352902 optind = 1;
28362903 while (1) {
28372904 if (optind >= argc) {
2838− printf("usage: tcc [-Idir] [-Dsym] [-llib] infile [infile_arg...]\n");
2905+ printf("tcc version 0.9 - Tiny C Compiler - Copyright (C) 2001 Fabrice Bellard\n"
2906+ "usage: tcc [-Idir] [-Dsym] [-llib] infile [infile_arg...]\n");
28392907 return 1;
28402908 }
28412909 r = argv[optind];
@@ -2890,7 +2958,7 @@ int main(int argc, char **argv) · 48 unmodified lines
28902958 s = sym_find(TOK_MAIN);
28912959 if (!s)
28922960 error("main() not defined");
2893− t = s->c;
2961+ t = (int (*)())s->c;
28942962 #ifdef PROFILE
28952963 return 1;
28962964 #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