History

float update

bellard committed 24 years ago

Commit 2694c101 parent 5ee15842 files · +568 −232Browse files

Unified · Split

2 files
  1. i386-gen.c +82 −19
  2. tcc.c +486 −213
i386-gen.c+82−19
@@ -242,12 +242,13 @@ void store(r, ft, fc) · 241 unmodified lines
242242 /* XXX: incorrect if reg to reg */
243243 /* XXX: should not flush float stack */
244244 if (bt == VT_FLOAT) {
245− o(0xd9); /* fstps */
246− r = 3;
245+ o(0xd9); /* fsts */
246+ r = 2;
247247 } else if (bt == VT_DOUBLE) {
248248 o(0xdd); /* fstpl */
249− r = 3;
249+ r = 2;
250250 } else if (bt == VT_LDOUBLE) {
251+ o(0xc0d9); /* fld %st(0) */
251252 o(0xdb); /* fstpt */
252253 r = 7;
253254 } else {
@@ -296,9 +297,7 @@ void gfunc_param(GFuncContext *c) · 42 unmodified lines
296297 vswap();
297298 vstore();
298299 c->args_size += size;
299− } else if ((vtop->t & VT_BTYPE) == VT_LDOUBLE ||
300− (vtop->t & VT_BTYPE) == VT_DOUBLE ||
301− (vtop->t & VT_BTYPE) == VT_FLOAT) {
300+ } else if (is_float(vtop->t)) {
302301 gv(); /* only one float register */
303302 if ((vtop->t & VT_BTYPE) == VT_FLOAT)
304303 size = 4;
@@ -333,10 +332,10 @@ void gfunc_call(GFuncContext *c) · 28 unmodified lines
333332 /* constant case */
334333 /* forward reference */
335334 if (vtop->t & VT_FORWARD) {
336− greloc((Sym *)vtop->c, ind + 1, RELOC_REL32);
335+ greloc(vtop->c.sym, ind + 1, RELOC_REL32);
337336 oad(0xe8, 0);
338337 } else {
339− oad(0xe8, vtop->c - ind - 5);
338+ oad(0xe8, vtop->c.ul - ind - 5);
340339 }
341340 } else {
342341 /* otherwise, indirect call */
@@ -362,23 +361,23 @@ int gtst(int inv, int t) · 19 unmodified lines
362361 if (v == VT_CMP) {
363362 /* fast case : can jump directly since flags are set */
364363 g(0x0f);
365− t = psym((vtop->c - 16) ^ inv, t);
364+ t = psym((vtop->c.i - 16) ^ inv, t);
366365 } else if (v == VT_JMP || v == VT_JMPI) {
367366 /* && or || optimization */
368367 if ((v & 1) == inv) {
369368 /* insert vtop->c jump list in t */
370− p = &vtop->c;
369+ p = &vtop->c.i;
371370 while (*p != 0)
372371 p = (int *)*p;
373372 *p = t;
374− t = vtop->c;
373+ t = vtop->c.i;
375374 } else {
376375 t = gjmp(t);
377− gsym(vtop->c);
376+ gsym(vtop->c.i);
378377 }
379378 } else if ((vtop->t & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
380379 /* constant jmp optimization */
381− if ((vtop->c != 0) != inv)
380+ if ((vtop->c.i != 0) != inv)
382381 t = gjmp(t);
383382 } else {
384383 /* XXX: floats */
@@ -472,6 +471,15 @@ void gen_opf(int op) · 87 unmodified lines
472471 {
473472 int a, ft, fc, swapped, r;
474473
474+ /* convert constants to memory references */
475+ if ((vtop[-1].t & (VT_CONST | VT_LVAL)) == VT_CONST) {
476+ vswap();
477+ gv();
478+ vswap();
479+ }
480+ if ((vtop[0].t & (VT_CONST | VT_LVAL)) == VT_CONST)
481+ gv();
482+
475483 /* must put at least one value in the floating point register */
476484 if ((vtop[-1].t & VT_LVAL) &&
477485 (vtop[0].t & VT_LVAL)) {
@@ -481,7 +489,7 @@ void gen_opf(int op) · 3 unmodified lines
481489 }
482490 if (op >= TOK_EQ && op <= TOK_GT) {
483491 /* load on stack second operand */
484− load(REG_ST0, vtop->t, vtop->c);
492+ load(REG_ST0, vtop->t, vtop->c.ul);
485493 if (op == TOK_GE || op == TOK_GT)
486494 o(0xc9d9); /* fxch %st(1) */
487495 o(0xe9da); /* fucompp */
@@ -502,7 +510,7 @@ void gen_opf(int op) · 14 unmodified lines
502510 }
503511 vtop--;
504512 vtop->t = (vtop->t & VT_TYPE) | VT_CMP;
505− vtop->c = op;
513+ vtop->c.i = op;
506514 } else {
507515 /* swap the stack if needed so that t1 is the register and t2 is
508516 the memory reference */
@@ -532,7 +540,7 @@ void gen_opf(int op) · 23 unmodified lines
532540 break;
533541 }
534542 ft = vtop->t;
535− fc = vtop->c;
543+ fc = vtop->c.ul;
536544 if ((ft & VT_BTYPE) == VT_DOUBLE)
537545 o(0xdc);
538546 else
@@ -551,10 +559,10 @@ void gen_opf(int op) · 12 unmodified lines
551559 }
552560 }
553561
554−/* convert integers to floating point 't' type (float/double/long
555− double) */
556−void gen_cvtf(int t)
562+/* convert integers to fp 't' type */
563+void gen_cvt_itof(int t)
557564 {
565+ gv();
558566 if ((vtop->t & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED)) {
559567 /* unsigned int to float/double/long double */
560568 o(0x6a); /* push $0 */
@@ -568,8 +576,63 @@ void gen_cvtf(int t) · 7 unmodified lines
568576 o(0x2404db); /* fildl (%esp) */
569577 o(0x04c483); /* add $4, %esp */
570578 }
579+ vtop->t = t | REG_ST0;
580+}
581+
582+/* FPU control word for rounding to nearest mode */
583+/* XXX: should move that into tcc lib support code ! */
584+static unsigned short __tcc_fpu_control = 0x137f;
585+/* FPU control word for round to zero mode for int convertion */
586+static unsigned short __tcc_int_fpu_control = 0x137f | 0x0c00;
587+
588+/* convert fp to int 't' type */
589+/* XXX: handle long long case */
590+void gen_cvt_ftoi(int t)
591+{
592+ int r, size;
593+
594+ gv();
595+ if (t == VT_INT | VT_UNSIGNED &&
596+ t == VT_LLONG | VT_UNSIGNED &&
597+ t == VT_LLONG)
598+ size = 8;
599+ else
600+ size = 4;
601+
602+ r = get_reg(REG_CLASS_INT);
603+ oad(0x2dd9, (int)&__tcc_int_fpu_control); /* ldcw xxx */
604+ oad(0xec81, size); /* sub $xxx, %esp */
605+ if (size == 4)
606+ o(0x1cdb); /* fistpl */
607+ else
608+ o(0x3cdb); /* fistpll */
609+ o(0x24);
610+ oad(0x2dd9, (int)&__tcc_fpu_control); /* ldcw xxx */
611+ o(0x58 + r); /* pop r */
612+ if (size == 8)
613+ o(0x04c483); /* add $4, %esp */
614+ vtop->t = t | r;
571615 }
572616
617+/* convert from one floating point type to another */
618+void gen_cvt_ftof(int t)
619+{
620+ /* all we have to do on i386 is to put the float in a register */
621+ gv();
622+}
623+
624+/* pop stack value */
625+void vpop(void)
626+{
627+ /* for x86, we need to pop the FP stack */
628+ if ((vtop->t & VT_VALMASK) == REG_ST0) {
629+ o(0xd9dd); /* fstp %st(1) */
630+ }
631+ vtop--;
632+}
633+
634+
635+
573636 /* end of X86 code generator */
574637 /*************************************************************/
575638
tcc.c+486−213
@@ -60,14 +60,18 @@ typedef union CValue { · 59 unmodified lines
6060 float f;
6161 int i;
6262 unsigned int ui;
63+ unsigned int ul; /* address (should be unsigned long on 64 bit cpu) */
64+ long long ll;
65+ unsigned long long ull;
6366 struct TokenSym *ts;
6467 int tab[1];
68+ struct Sym *sym;
6569 } CValue;
6670
6771 /* value on stack */
6872 typedef struct SValue {
6973 int t;
70− int c;
74+ CValue c;
7175 } SValue;
7276
7377 /* symbol management */
@@ -332,7 +336,7 @@ void next(void); · 258 unmodified lines
332336 void next_nomacro(void);
333337 int expr_const(void);
334338 void expr_eq(void);
335−void expr(void);
339+void gexpr(void);
336340 void decl(int l);
337341 void decl_initializer(int t, int c, int first, int size_only);
338342 int decl_initializer_alloc(int t, int has_init);
@@ -358,6 +362,14 @@ int type_decl(int *v, int t, int td); · 19 unmodified lines
358362 void error(const char *fmt, ...);
359363 void vset(int t, int v);
360364
365+/* true if float/double/long double type */
366+static inline int is_float(int t)
367+{
368+ int bt;
369+ bt = t & VT_BTYPE;
370+ return bt == VT_LDOUBLE || bt == VT_DOUBLE || bt == VT_FLOAT;
371+}
372+
361373 #include "i386-gen.c"
362374
363375 #ifdef CONFIG_TCC_STATIC
@@ -983,7 +995,7 @@ void preprocess(void) · 619 unmodified lines
983995 }
984996 tok_add(&str, &len, 0);
985997 #ifdef PP_DEBUG
986− printf("define %s %d: ", get_tok_str(v, 0), t);
998+ printf("define %s %d: ", get_tok_str(v, NULL), t);
987999 tok_print(str);
9881000 #endif
9891001 s = sym_push1(&define_stack, v, t, (int)str);
@@ -1220,7 +1232,6 @@ void parse_number(void) · 230 unmodified lines
12201232 char *q;
12211233 unsigned int n, n1;
12221234 unsigned int bn[BN_SIZE];
1223− CValue cval;
12241235 double d;
12251236
12261237 /* number */
@@ -1343,7 +1354,6 @@ void parse_number(void) · 116 unmodified lines
13431354 cinp();
13441355 }
13451356 exp_val = exp_val * s;
1346− printf("num=%08x %08x %d %d\n", bn[1], bn[0], frac_bits, exp_val);
13471357
13481358 /* now we can generate the number */
13491359 /* XXX: should patch directly float number */
@@ -1354,15 +1364,15 @@ void parse_number(void) · 4 unmodified lines
13541364 cinp();
13551365 tok = TOK_CFLOAT;
13561366 /* float : should handle overflow */
1357− cval.f = (float)d;
1367+ tokc.f = (float)d;
13581368 } else if (t == 'L') {
13591369 cinp();
13601370 tok = TOK_CLDOUBLE;
13611371 /* XXX: not large enough */
1362− cval.ld = (long double)d;
1372+ tokc.ld = (long double)d;
13631373 } else {
13641374 tok = TOK_CDOUBLE;
1365− cval.d = d;
1375+ tokc.d = d;
13661376 }
13671377 } else {
13681378 /* decimal floats */
@@ -1405,14 +1415,14 @@ void parse_number(void) · 36 unmodified lines
14051415 if (t == 'F') {
14061416 cinp();
14071417 tok = TOK_CFLOAT;
1408− cval.f = strtof(token_buf, NULL);
1418+ tokc.f = strtof(token_buf, NULL);
14091419 } else if (t == 'L') {
14101420 cinp();
14111421 tok = TOK_CLDOUBLE;
1412− cval.ld = strtold(token_buf, NULL);
1422+ tokc.ld = strtold(token_buf, NULL);
14131423 } else {
14141424 tok = TOK_CDOUBLE;
1415− cval.d = strtod(token_buf, NULL);
1425+ tokc.d = strtod(token_buf, NULL);
14161426 }
14171427 }
14181428 } else {
@@ -1858,7 +1868,7 @@ void swap(int *p, int *q) · 439 unmodified lines
18581868 *q = t;
18591869 }
18601870
1861−void vset(int t, int v)
1871+void vsetc(int t, CValue *vc)
18621872 {
18631873 if (vtop >= vstack + VSTACK_SIZE)
18641874 error("memory full");
@@ -1868,12 +1878,15 @@ void vset(int t, int v) · 3 unmodified lines
18681878 gv();
18691879 vtop++;
18701880 vtop->t = t;
1871− vtop->c = v;
1881+ vtop->c = *vc;
18721882 }
18731883
1874−void vpop(void)
1884+void vset(int t, int v)
18751885 {
1876− vtop--;
1886+ CValue cval;
1887+
1888+ cval.i = v;
1889+ vsetc(t, &cval);
18771890 }
18781891
18791892 void vswap(void)
@@ -1912,7 +1925,7 @@ int save_reg_forced(int r) · 32 unmodified lines
19121925 else
19131926 t = VT_LOCAL;
19141927 p->t = (p->t & VT_TYPE) | VT_LVAL | t;
1915− p->c = l;
1928+ p->c.ul = l;
19161929 }
19171930 }
19181931 return l;
@@ -1994,7 +2007,7 @@ void move_reg(int r, int s) · 75 unmodified lines
19942007 (such as structures). */
19952008 int gv(void)
19962009 {
1997− int r, bit_pos, bit_size, rc;
2010+ int r, bit_pos, bit_size, rc, size, align, i;
19982011
19992012 /* NOTE: get_reg can modify vstack[] */
20002013 if (vtop->t & VT_BITFIELD) {
@@ -2010,16 +2023,28 @@ int gv(void) · 9 unmodified lines
20102023 gen_op(TOK_SAR);
20112024 r = gv();
20122025 } else {
2026+ if (is_float(vtop->t) && (vtop->t & (VT_CONST | VT_LVAL)) == VT_CONST) {
2027+ /* CPUs usually cannot use float constants, so we store them
2028+ generically in data segment */
2029+ size = type_size(vtop->t, &align);
2030+ glo = (glo + align - 1) & -align;
2031+ /* XXX: not portable yet */
2032+ size = size >> 2;
2033+ for(i=0;i<size;i++)
2034+ ((int *)glo)[i] = vtop->c.tab[i];
2035+ vtop->t |= VT_LVAL;
2036+ vtop->c.ul = glo;
2037+ glo += size << 2;
2038+ }
20132039 r = vtop->t & VT_VALMASK;
20142040 if (r >= VT_CONST || (vtop->t & VT_LVAL)) {
2015− if ((vtop->t & VT_BTYPE) == VT_FLOAT ||
2016− (vtop->t & VT_BTYPE) == VT_DOUBLE)
2041+ if (is_float(vtop->t))
20172042 rc = REG_CLASS_FLOAT;
20182043 else
20192044 rc = REG_CLASS_INT;
20202045 r = get_reg(rc);
20212046 }
2022− load(r, vtop->t, vtop->c);
2047+ load(r, vtop->t, vtop->c.ul);
20232048 vtop->t = (vtop->t & VT_TYPE) | r;
20242049 }
20252050 return r;
@@ -2037,36 +2062,36 @@ void gen_opc(int op) · 11 unmodified lines
20372062 c1 = (v1->t & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST;
20382063 c2 = (v2->t & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST;
20392064 if (c1 && c2) {
2040− fc = v2->c;
2065+ fc = v2->c.i;
20412066 switch(op) {
2042− case '+': v1->c += fc; break;
2043− case '-': v1->c -= fc; break;
2044− case '&': v1->c &= fc; break;
2045− case '^': v1->c ^= fc; break;
2046− case '|': v1->c |= fc; break;
2047− case '*': v1->c *= fc; break;
2067+ case '+': v1->c.i += fc; break;
2068+ case '-': v1->c.i -= fc; break;
2069+ case '&': v1->c.i &= fc; break;
2070+ case '^': v1->c.i ^= fc; break;
2071+ case '|': v1->c.i |= fc; break;
2072+ case '*': v1->c.i *= fc; break;
20482073 case TOK_PDIV:
2049− case '/': v1->c /= fc; break; /* XXX: zero case ? */
2050− case '%': v1->c %= fc; break; /* XXX: zero case ? */
2051− case TOK_UDIV: v1->c = (unsigned)v1->c / fc; break; /* XXX: zero case ? */
2052− case TOK_UMOD: v1->c = (unsigned)v1->c % fc; break; /* XXX: zero case ? */
2053− case TOK_SHL: v1->c <<= fc; break;
2054− case TOK_SHR: v1->c = (unsigned)v1->c >> fc; break;
2055− case TOK_SAR: v1->c >>= fc; break;
2074+ case '/': v1->c.i /= fc; break; /* XXX: zero case ? */
2075+ case '%': v1->c.i %= fc; break; /* XXX: zero case ? */
2076+ case TOK_UDIV: v1->c.i = (unsigned)v1->c.i / fc; break; /* XXX: zero case ? */
2077+ case TOK_UMOD: v1->c.i = (unsigned)v1->c.i % fc; break; /* XXX: zero case ? */
2078+ case TOK_SHL: v1->c.i <<= fc; break;
2079+ case TOK_SHR: v1->c.i = (unsigned)v1->c.i >> fc; break;
2080+ case TOK_SAR: v1->c.i >>= fc; break;
20562081 /* tests */
2057− case TOK_ULT: v1->c = (unsigned)v1->c < (unsigned)fc; break;
2058− case TOK_UGE: v1->c = (unsigned)v1->c >= (unsigned)fc; break;
2059− case TOK_EQ: v1->c = v1->c == fc; break;
2060− case TOK_NE: v1->c = v1->c != fc; break;
2061− case TOK_ULE: v1->c = (unsigned)v1->c <= (unsigned)fc; break;
2062− case TOK_UGT: v1->c = (unsigned)v1->c > (unsigned)fc; break;
2063− case TOK_LT: v1->c = v1->c < fc; break;
2064− case TOK_GE: v1->c = v1->c >= fc; break;
2065− case TOK_LE: v1->c = v1->c <= fc; break;
2066− case TOK_GT: v1->c = v1->c > fc; break;
2082+ case TOK_ULT: v1->c.i = (unsigned)v1->c.i < (unsigned)fc; break;
2083+ case TOK_UGE: v1->c.i = (unsigned)v1->c.i >= (unsigned)fc; break;
2084+ case TOK_EQ: v1->c.i = v1->c.i == fc; break;
2085+ case TOK_NE: v1->c.i = v1->c.i != fc; break;
2086+ case TOK_ULE: v1->c.i = (unsigned)v1->c.i <= (unsigned)fc; break;
2087+ case TOK_UGT: v1->c.i = (unsigned)v1->c.i > (unsigned)fc; break;
2088+ case TOK_LT: v1->c.i = v1->c.i < fc; break;
2089+ case TOK_GE: v1->c.i = v1->c.i >= fc; break;
2090+ case TOK_LE: v1->c.i = v1->c.i <= fc; break;
2091+ case TOK_GT: v1->c.i = v1->c.i > fc; break;
20672092 /* logical */
2068− case TOK_LAND: v1->c = v1->c && fc; break;
2069− case TOK_LOR: v1->c = v1->c || fc; break;
2093+ case TOK_LAND: v1->c.i = v1->c.i && fc; break;
2094+ case TOK_LOR: v1->c.i = v1->c.i || fc; break;
20702095 default:
20712096 goto general_case;
20722097 }
@@ -2078,7 +2103,7 @@ void gen_opc(int op) · 5 unmodified lines
20782103 vswap();
20792104 swap(&c1, &c2);
20802105 }
2081− fc = vtop->c;
2106+ fc = vtop->c.i;
20822107 if (c2 && (((op == '*' || op == '/' || op == TOK_UDIV ||
20832108 op == TOK_PDIV) &&
20842109 fc == 1) ||
@@ -2097,7 +2122,7 @@ void gen_opc(int op) · 12 unmodified lines
20972122 fc >>= 1;
20982123 n++;
20992124 }
2100− vtop->c = n;
2125+ vtop->c.i = n;
21012126 if (op == '*')
21022127 op = TOK_SHL;
21032128 else if (op == TOK_PDIV)
@@ -2120,14 +2145,6 @@ int pointed_size(int t) · 16 unmodified lines
21202145 return type_size(pointed_type(t), &t);
21212146 }
21222147
2123−/* true if float/double/long double type */
2124−static inline int is_float(int t)
2125−{
2126− int bt;
2127− bt = t & VT_BTYPE;
2128− return bt == VT_LDOUBLE || bt == VT_DOUBLE || bt == VT_FLOAT;
2129−}
2130−
21312148 /* generic gen_op: handles types problems */
21322149 void gen_op(int op)
21332150 {
@@ -2219,23 +2236,111 @@ void gen_op(int op) · 85 unmodified lines
22192236 /* cast 'vtop' to 't' type */
22202237 void gen_cast(int t)
22212238 {
2222− int r, bits, bt, vbt;
2239+ int r, bits, sbt, dbt, sf, df, c, st1, dt1;
22232240
22242241 r = vtop->t & VT_VALMASK;
22252242 if (!(t & VT_LVAL)) {
22262243 /* if not lvalue, then we convert now */
2227− bt = t & VT_BTYPE;
2228− vbt = vtop->t & VT_BTYPE;
2229− if (bt != vbt) {
2230− if (is_float(bt)) {
2231− /* need to generate value and do explicit cast */
2232− gv();
2233− gen_cvtf(bt);
2234− } else if (bt == VT_BOOL) {
2244+ dbt = t & VT_BTYPE;
2245+ sbt = vtop->t & VT_BTYPE;
2246+ if (sbt != dbt) {
2247+ sf = is_float(sbt);
2248+ df = is_float(dbt);
2249+ c = (vtop->t & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST;
2250+ if (sf && df) {
2251+ /* convert from fp to fp */
2252+ if (c) {
2253+ /* constant case: we can do it now */
2254+ /* XXX: in ISOC, cannot do it if error in convert */
2255+ if (dbt == VT_FLOAT && sbt == VT_DOUBLE)
2256+ vtop->c.f = (float)vtop->c.d;
2257+ else if (dbt == VT_FLOAT && sbt == VT_LDOUBLE)
2258+ vtop->c.f = (float)vtop->c.ld;
2259+ else if (dbt == VT_DOUBLE && sbt == VT_FLOAT)
2260+ vtop->c.d = (double)vtop->c.f;
2261+ else if (dbt == VT_DOUBLE && sbt == VT_LDOUBLE)
2262+ vtop->c.d = (double)vtop->c.ld;
2263+ else if (dbt == VT_LDOUBLE && sbt == VT_FLOAT)
2264+ vtop->c.ld = (long double)vtop->c.f;
2265+ else if (dbt == VT_LDOUBLE && sbt == VT_DOUBLE)
2266+ vtop->c.ld = (long double)vtop->c.d;
2267+ } else {
2268+ /* non constant case: generate code */
2269+ gen_cvt_ftof(dbt);
2270+ }
2271+ } else if (df) {
2272+ /* convert int to fp */
2273+ /* XXX: add const cases */
2274+ st1 = vtop->t & (VT_BTYPE | VT_UNSIGNED);
2275+ if (c) {
2276+ switch(st1) {
2277+ case VT_LLONG | VT_UNSIGNED:
2278+ case VT_LLONG:
2279+ /* well, currently not needed */
2280+ goto do_itof;
2281+ case VT_INT | VT_UNSIGNED:
2282+ switch(dbt) {
2283+ case VT_FLOAT: vtop->c.f = (float)vtop->c.ui; break;
2284+ case VT_DOUBLE: vtop->c.d = (double)vtop->c.ui; break;
2285+ case VT_LDOUBLE: vtop->c.ld = (long double)vtop->c.ui; break;
2286+ }
2287+ break;
2288+ default:
2289+ switch(dbt) {
2290+ case VT_FLOAT: vtop->c.f = (float)vtop->c.i; break;
2291+ case VT_DOUBLE: vtop->c.d = (double)vtop->c.i; break;
2292+ case VT_LDOUBLE: vtop->c.ld = (long double)vtop->c.i; break;
2293+ }
2294+ break;
2295+ }
2296+ } else {
2297+ do_itof:
2298+ gen_cvt_itof(dbt);
2299+ }
2300+ } else if (sf) {
2301+ /* convert fp to int */
2302+ dt1 = t & (VT_BTYPE | VT_UNSIGNED);
2303+ /* we handle char/short/etc... with generic code */
2304+ if (dt1 != VT_INT | VT_UNSIGNED &&
2305+ dt1 != VT_LLONG | VT_UNSIGNED &&
2306+ dt1 != VT_LLONG)
2307+ dt1 = VT_INT;
2308+ if (c) {
2309+ switch(dt1) {
2310+ case VT_LLONG | VT_UNSIGNED:
2311+ case VT_LLONG:
2312+ /* well, currently not needed */
2313+ goto do_ftoi;
2314+ case VT_INT | VT_UNSIGNED:
2315+ switch(sbt) {
2316+ case VT_FLOAT: vtop->c.ui = (unsigned int)vtop->c.d; break;
2317+ case VT_DOUBLE: vtop->c.ui = (unsigned int)vtop->c.d; break;
2318+ case VT_LDOUBLE: vtop->c.ui = (unsigned int)vtop->c.d; break;
2319+ }
2320+ break;
2321+ default:
2322+ /* int case */
2323+ switch(sbt) {
2324+ case VT_FLOAT: vtop->c.i = (int)vtop->c.d; break;
2325+ case VT_DOUBLE: vtop->c.i = (int)vtop->c.d; break;
2326+ case VT_LDOUBLE: vtop->c.i = (int)vtop->c.d; break;
2327+ }
2328+ break;
2329+ }
2330+ } else {
2331+ do_ftoi:
2332+ gen_cvt_ftoi(dt1);
2333+ }
2334+ if (dt1 == VT_INT && (t & (VT_TYPE | VT_UNSIGNED)) != dt1) {
2335+ /* additionnal cast for char/short/bool... */
2336+ vtop->t = (vtop->t & ~VT_TYPE) | dt1;
2337+ gen_cast(t);
2338+ }
2339+ } else if (dbt == VT_BOOL) {
22352340 vset(VT_CONST, 0);
22362341 gen_op(TOK_NE);
2237− } else if (bt == VT_BYTE || bt == VT_SHORT) {
2238− if (bt == VT_BYTE)
2342+ } else if (dbt == VT_BYTE || dbt == VT_SHORT) {
2343+ if (dbt == VT_BYTE)
22392344 bits = 8;
22402345 else
22412346 bits = 16;
@@ -2310,6 +2415,181 @@ int mk_pointer(int t) · 68 unmodified lines
23102415 return VT_PTR | (p << VT_STRUCT_SHIFT) | (t & ~VT_TYPE);
23112416 }
23122417
2418+int is_compatible_types(int t1, int t2)
2419+{
2420+ Sym *s1, *s2;
2421+ int bt1, bt2;
2422+
2423+ t1 &= VT_TYPE;
2424+ t2 &= VT_TYPE;
2425+ bt1 = t1 & VT_BTYPE;
2426+ bt2 = t2 & VT_BTYPE;
2427+ if (bt1 == VT_PTR) {
2428+ t1 = pointed_type(t1);
2429+ /* if function, then convert implicitely to function pointer */
2430+ if (bt2 != VT_FUNC) {
2431+ if (bt2 != VT_PTR)
2432+ return 0;
2433+ t2 = pointed_type(t2);
2434+ }
2435+ /* void matches everything */
2436+ t1 &= VT_TYPE;
2437+ t2 &= VT_TYPE;
2438+ if (t1 == VT_VOID || t2 == VT_VOID)
2439+ return 1;
2440+ return is_compatible_types(t1, t2);
2441+ } else if (bt1 == VT_STRUCT) {
2442+ return (t2 == t1);
2443+ } else if (bt1 == VT_FUNC) {
2444+ if (bt2 != VT_FUNC)
2445+ return 0;
2446+ s1 = sym_find(((unsigned)t1 >> VT_STRUCT_SHIFT));
2447+ s2 = sym_find(((unsigned)t2 >> VT_STRUCT_SHIFT));
2448+ if (!is_compatible_types(s1->t, s2->t))
2449+ return 0;
2450+ /* XXX: not complete */
2451+ if (s1->c == FUNC_OLD || s2->c == FUNC_OLD)
2452+ return 1;
2453+ if (s1->c != s2->c)
2454+ return 0;
2455+ while (s1 != NULL) {
2456+ if (s2 == NULL)
2457+ return 0;
2458+ if (!is_compatible_types(s1->t, s2->t))
2459+ return 0;
2460+ s1 = s1->next;
2461+ s2 = s2->next;
2462+ }
2463+ if (s2)
2464+ return 0;
2465+ return 1;
2466+ } else {
2467+ /* XXX: not complete */
2468+ return 1;
2469+ }
2470+}
2471+
2472+int check_assign_types(int t1, int t2)
2473+{
2474+ t1 &= VT_TYPE;
2475+ t2 &= VT_TYPE;
2476+ if ((t1 & VT_BTYPE) == VT_PTR &&
2477+ (t2 & VT_BTYPE) == VT_FUNC) {
2478+ return is_compatible_types(pointed_type(t1), t2);
2479+ } else {
2480+ return is_compatible_types(t1, t2);
2481+ }
2482+}
2483+
2484+/* print a type. If 'varstr' is not NULL, then the variable is also
2485+ printed in the type */
2486+/* XXX: add array and function pointers */
2487+/* XXX: buffer overflows */
2488+void type_to_str(char *buf, int buf_size,
2489+ int t, const char *varstr)
2490+{
2491+ int bt, v;
2492+ Sym *s, *sa;
2493+ char buf1[256];
2494+
2495+ t = t & VT_TYPE;
2496+ bt = t & VT_BTYPE;
2497+ buf[0] = '\0';
2498+ if (t & VT_UNSIGNED)
2499+ strcat(buf, "unsigned ");
2500+ switch(bt) {
2501+ case VT_VOID:
2502+ strcat(buf, "void");
2503+ break;
2504+ case VT_BOOL:
2505+ strcat(buf, "_Bool");
2506+ break;
2507+ case VT_BYTE:
2508+ strcat(buf, "char");
2509+ break;
2510+ case VT_SHORT:
2511+ strcat(buf, "short");
2512+ break;
2513+ case VT_INT:
2514+ strcat(buf, "int");
2515+ break;
2516+ case VT_LONG:
2517+ strcat(buf, "long");
2518+ break;
2519+ case VT_LLONG:
2520+ strcat(buf, "long long");
2521+ break;
2522+ case VT_FLOAT:
2523+ strcat(buf, "float");
2524+ break;
2525+ case VT_DOUBLE:
2526+ strcat(buf, "double");
2527+ break;
2528+ case VT_LDOUBLE:
2529+ strcat(buf, "long double");
2530+ break;
2531+ case VT_ENUM:
2532+ case VT_STRUCT:
2533+ if (bt == VT_STRUCT)
2534+ strcat(buf, "struct ");
2535+ else
2536+ strcat(buf, "enum ");
2537+ v = (unsigned)t >> VT_STRUCT_SHIFT;
2538+ if (v >= SYM_FIRST_ANOM)
2539+ strcat(buf, "<anonymous>");
2540+ else
2541+ strcat(buf, get_tok_str(v, NULL));
2542+ break;
2543+ case VT_FUNC:
2544+ s = sym_find((unsigned)t >> VT_STRUCT_SHIFT);
2545+ type_to_str(buf, buf_size, s->t, varstr);
2546+ strcat(buf, "(");
2547+ sa = s->next;
2548+ while (sa != NULL) {
2549+ type_to_str(buf1, sizeof(buf1), sa->t, NULL);
2550+ strcat(buf, buf1);
2551+ sa = sa->next;
2552+ if (sa)
2553+ strcat(buf, ", ");
2554+ }
2555+ strcat(buf, ")");
2556+ goto no_var;
2557+ case VT_PTR:
2558+ s = sym_find((unsigned)t >> VT_STRUCT_SHIFT);
2559+ strcpy(buf1, "*");
2560+ if (varstr)
2561+ strcat(buf1, varstr);
2562+ type_to_str(buf, buf_size, s->t, buf1);
2563+ goto no_var;
2564+ }
2565+ if (varstr) {
2566+ strcat(buf, " ");
2567+ strcat(buf, varstr);
2568+ }
2569+ no_var: ;
2570+}
2571+
2572+
2573+
2574+/* verify type compatibility to store vtop in 'st' type, and generate
2575+ casts if needed */
2576+void gen_assign_cast(int dt)
2577+{
2578+ int st;
2579+ char buf1[256], buf2[256];
2580+
2581+ st = vtop->t; /* destination type */
2582+ if (!check_assign_types(dt, st)) {
2583+ type_to_str(buf1, sizeof(buf1), st, NULL);
2584+ type_to_str(buf2, sizeof(buf2), dt, NULL);
2585+ error("cannot cast '%s' to '%s'", buf1, buf2);
2586+ }
2587+ if ((dt & VT_BTYPE) == VT_BOOL || is_float(dt)) {
2588+ gen_cast(dt & VT_BTYPE);
2589+ }
2590+}
2591+
2592+
23132593 /* store vtop in lvalue pushed on stack */
23142594 void vstore(void)
23152595 {
@@ -2317,6 +2597,8 @@ void vstore(void) · 1 unmodified line
23172597 GFuncContext gf;
23182598
23192599 ft = vtop[-1].t;
2600+ gen_assign_cast(ft & VT_TYPE);
2601+
23202602 if ((vtop->t & VT_BTYPE) == VT_STRUCT) {
23212603 /* if structure, only generate pointer */
23222604 /* structure assignment : generate memcpy */
@@ -2364,13 +2646,9 @@ void vstore(void) · 41 unmodified lines
23642646 /* store result */
23652647 vstore();
23662648 } else {
2367− /* generate cast if needed implicit cast for bool */
2368− if ((ft & VT_BTYPE) == VT_BOOL) {
2369− gen_cast(VT_BOOL);
2370− }
23712649 r = gv(); /* generate value */
23722650 ft = vtop[-1].t;
2373− fc = vtop[-1].c;
2651+ fc = vtop[-1].c.i;
23742652 /* if lvalue was saved on stack, must read it */
23752653 if ((ft & VT_VALMASK) == VT_LLOCAL) {
23762654 t = get_reg(REG_CLASS_INT);
@@ -2380,7 +2658,7 @@ void vstore(void) · 3 unmodified lines
23802658 store(r, ft, fc);
23812659 vtop--;
23822660 vtop->t = (ft & VT_TYPE) | r;
2383− vtop->c = 0;
2661+ vtop->c.i = 0;
23842662 }
23852663 }
23862664
@@ -2401,7 +2679,7 @@ void inc(int post, int c) · 14 unmodified lines
24012679 load(r1, r, 0); /* move r to r1 */
24022680 /* duplicates value */
24032681 vtop[-2].t = (vtop->t & VT_TYPE) | r1;
2404− vtop[-2].c = 0;
2682+ vtop[-2].c.i = 0;
24052683 }
24062684 /* add constant */
24072685 vset(VT_CONST, c - TOK_MID);
@@ -2470,7 +2748,8 @@ int struct_decl(int u) · 62 unmodified lines
24702748 t = type_decl(&v, b, TYPE_DIRECT);
24712749 if ((t & VT_BTYPE) == VT_FUNC ||
24722750 (t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN)))
2473− error("invalid type for '%s'", get_tok_str(v, 0));
2751+ error("invalid type for '%s'",
2752+ get_tok_str(v, NULL));
24742753 } else {
24752754 t = b;
24762755 }
@@ -2480,10 +2759,10 @@ int struct_decl(int u) · 3 unmodified lines
24802759 /* XXX: handle v = 0 case for messages */
24812760 if (bit_size < 0)
24822761 error("negative width in bit-field '%s'",
2483− get_tok_str(v, 0));
2762+ get_tok_str(v, NULL));
24842763 if (v && bit_size == 0)
24852764 error("zero width for bit-field '%s'",
2486− get_tok_str(v, 0));
2765+ get_tok_str(v, NULL));
24872766 }
24882767 size = type_size(t, &align);
24892768 lbit_pos = 0;
@@ -2496,7 +2775,7 @@ int struct_decl(int u) · 6 unmodified lines
24962775 bsize = size * 8;
24972776 if (bit_size > bsize) {
24982777 error("width of '%s' exceeds its type",
2499− get_tok_str(v, 0));
2778+ get_tok_str(v, NULL));
25002779 } else if (bit_size == bsize) {
25012780 /* no need for bit fields */
25022781 bit_pos = 0;
@@ -2538,7 +2817,7 @@ int struct_decl(int u) · 35 unmodified lines
25382817 }
25392818 #if 0
25402819 printf("add field %s offset=%d",
2541− get_tok_str(v, 0), offset);
2820+ get_tok_str(v, NULL), offset);
25422821 if (t & VT_BITFIELD) {
25432822 printf(" pos=%d size=%d",
25442823 (t >> VT_STRUCT_SHIFT) & 0x3f,
@@ -2698,10 +2977,12 @@ int post_type(int t) · 153 unmodified lines
26982977 goto old_proto;
26992978 }
27002979 }
2980+ l = FUNC_NEW;
27012981 if ((pt & VT_BTYPE) == VT_VOID && tok == ')')
27022982 break;
2703− l = FUNC_NEW;
27042983 pt = type_decl(&n, pt, TYPE_DIRECT | TYPE_ABSTRACT);
2984+ if ((pt & VT_BTYPE) == VT_VOID)
2985+ error("parameter declared as void");
27052986 } else {
27062987 old_proto:
27072988 n = tok;
@@ -2722,6 +3003,9 @@ int post_type(int t) · 14 unmodified lines
27223003 }
27233004 }
27243005 }
3006+ /* if no parameters, then old type prototype */
3007+ if (l == 0)
3008+ l = FUNC_OLD;
27253009 skip(')');
27263010 t1 = t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN);
27273011 t = post_type(t & ~(VT_TYPEDEF | VT_STATIC | VT_EXTERN));
@@ -2826,6 +3110,24 @@ void indir(void) · 98 unmodified lines
28263110 vtop->t |= VT_LVAL;
28273111 }
28283112
3113+/* pass a parameter to a function and do type checking and casting */
3114+void gfunc_param_typed(GFuncContext *gf, Sym *func, Sym *arg)
3115+{
3116+ int func_type;
3117+ func_type = func->c;
3118+ if (func_type == FUNC_OLD ||
3119+ (func_type == FUNC_ELLIPSIS && arg == NULL)) {
3120+ /* default casting : only need to convert float to double */
3121+ if ((vtop->t & VT_BTYPE) == VT_FLOAT)
3122+ gen_cast(VT_DOUBLE);
3123+ } else if (arg == NULL) {
3124+ error("too many arguments to function");
3125+ } else {
3126+ gen_assign_cast(arg->t);
3127+ }
3128+ gfunc_param(gf);
3129+}
3130+
28293131 void unary(void)
28303132 {
28313133 int n, t, ft, fc, p, align, size;
@@ -2836,16 +3138,13 @@ void unary(void) · 4 unmodified lines
28363138 vset(VT_CONST | VT_INT, tokc.i);
28373139 next();
28383140 } else if (tok == TOK_CFLOAT) {
2839− /* currently, cannot do more */
2840− vset(VT_CONST | VT_FLOAT, 0);
3141+ vsetc(VT_CONST | VT_FLOAT, &tokc);
28413142 next();
28423143 } else if (tok == TOK_CDOUBLE) {
2843− /* currently, cannot do more */
2844− vset(VT_CONST | VT_DOUBLE, 0);
3144+ vsetc(VT_CONST | VT_DOUBLE, &tokc);
28453145 next();
28463146 } else if (tok == TOK_CLDOUBLE) {
2847− /* currently, cannot do more */
2848− vset(VT_CONST | VT_LDOUBLE, 0);
3147+ vsetc(VT_CONST | VT_LDOUBLE, &tokc);
28493148 next();
28503149 } else if (tok == TOK___FUNC__) {
28513150 /* special function name identifier */
@@ -2895,7 +3194,7 @@ void unary(void) · 43 unmodified lines
28953194 gen_cast(ft);
28963195 }
28973196 } else {
2898− expr();
3197+ gexpr();
28993198 skip(')');
29003199 }
29013200 } else if (t == '*') {
@@ -2914,9 +3213,9 @@ void unary(void) · 12 unmodified lines
29143213 if (t == '!') {
29153214 unary();
29163215 if ((vtop->t & (VT_VALMASK | VT_LVAL)) == VT_CONST)
2917− vtop->c = !vtop->c;
3216+ vtop->c.i = !vtop->c.i;
29183217 else if ((vtop->t & VT_VALMASK) == VT_CMP)
2919− vtop->c = vtop->c ^ 1;
3218+ vtop->c.i = vtop->c.i ^ 1;
29203219 else
29213220 vset(VT_JMP, gtst(1, 0));
29223221 } else
@@ -2936,7 +3235,7 @@ void unary(void) · 13 unmodified lines
29363235 else {
29373236 /* XXX: some code could be generated: add eval
29383237 flag */
2939− expr();
3238+ gexpr();
29403239 t = vtop->t;
29413240 vpop();
29423241 }
@@ -2960,7 +3259,7 @@ void unary(void) · 17 unmodified lines
29603259 s = sym_find(t);
29613260 if (!s) {
29623261 if (tok != '(')
2963− error("'%s' undeclared", get_tok_str(t, 0));
3262+ error("'%s' undeclared", get_tok_str(t, NULL));
29643263 /* for simple function calls, we tolerate undeclared
29653264 external reference */
29663265 p = anon_sym++;
@@ -2971,7 +3270,7 @@ void unary(void) · 4 unmodified lines
29713270 vset(s->t, s->c);
29723271 /* if forward reference, we must point to s */
29733272 if (vtop->t & VT_FORWARD)
2974− vtop->c = (int)s;
3273+ vtop->c.sym = s;
29753274 }
29763275 }
29773276
@@ -3011,12 +3310,14 @@ void unary(void) · 33 unmodified lines
30113310 next();
30123311 } else if (tok == '[') {
30133312 next();
3014− expr();
3313+ gexpr();
30153314 gen_op('+');
30163315 indir();
30173316 skip(']');
30183317 } else if (tok == '(') {
3019− int rett, retc;
3318+ int rett;
3319+ CValue retc;
3320+ Sym *sa;
30203321
30213322 /* function call */
30223323 if ((vtop->t & VT_BTYPE) != VT_FUNC) {
@@ -3032,12 +3333,12 @@ void unary(void) · 9 unmodified lines
30323333 } else {
30333334 vtop->t &= ~VT_LVAL; /* no lvalue */
30343335 }
3035−
30363336 /* get return type */
30373337 s = sym_find((unsigned)vtop->t >> VT_STRUCT_SHIFT);
30383338 save_regs(); /* save used temporary registers */
30393339 gfunc_start(&gf);
30403340 next();
3341+ sa = s->next; /* first parameter */
30413342 #ifdef INVERT_FUNC_PARAMS
30423343 {
30433344 int *str, len, parlevel, *saved_macro_ptr;
@@ -3061,7 +3362,10 @@ void unary(void) · 17 unmodified lines
30613362 }
30623363 tok_add(&str, &len, -1); /* end of file added */
30633364 tok_add(&str, &len, 0);
3064− sym_push2(&args, 0, 0, (int)str);
3365+ s1 = sym_push2(&args, 0, 0, (int)str);
3366+ s1->next = sa; /* add reference to argument */
3367+ if (sa)
3368+ sa = sa->next;
30653369 if (tok != ',')
30663370 break;
30673371 next();
@@ -3077,7 +3381,7 @@ void unary(void) · 9 unmodified lines
30773381 expr_eq();
30783382 if (tok != -1)
30793383 expect("',' or ')'");
3080− gfunc_param(&gf);
3384+ gfunc_param_typed(&gf, s, args->next);
30813385 s1 = args->prev;
30823386 free((int *)args->c);
30833387 free(args);
@@ -3101,93 +3405,30 @@ void unary(void) · 17 unmodified lines
31013405 gfunc_param(&gf);
31023406 } else {
31033407 rett = s->t | FUNC_RET_REG; /* return in register */
3104− retc = 0;
3408+ retc.i = 0;
31053409 }
31063410 #ifndef INVERT_FUNC_PARAMS
31073411 while (tok != ')') {
31083412 expr_eq();
3109− gfunc_param(&gf);
3413+ gfunc_param_typed(&gf, s, sa);
3414+ if (sa)
3415+ sa = sa->next;
31103416 if (tok == ',')
31113417 next();
31123418 }
31133419 #endif
3420+ if (sa)
3421+ error("too few arguments to function %x", sa->t);
31143422 skip(')');
31153423 gfunc_call(&gf);
31163424 /* return value */
3117− vset(rett, retc);
3425+ vsetc(rett, &retc);
31183426 } else {
31193427 break;
31203428 }
31213429 }
31223430 }
31233431
3124−int is_compatible_types(int t1, int t2)
3125−{
3126− Sym *s1, *s2;
3127− int bt1, bt2;
3128−
3129− t1 &= VT_TYPE;
3130− t2 &= VT_TYPE;
3131− bt1 = t1 & VT_BTYPE;
3132− bt2 = t2 & VT_BTYPE;
3133− if (bt1 == VT_PTR) {
3134− t1 = pointed_type(t1);
3135− /* if function, then convert implicitely to function pointer */
3136− if (bt2 != VT_FUNC) {
3137− if (bt2 != VT_PTR)
3138− return 0;
3139− t2 = pointed_type(t2);
3140− }
3141− /* void matches everything */
3142− t1 &= VT_TYPE;
3143− t2 &= VT_TYPE;
3144− if (t1 == VT_VOID || t2 == VT_VOID)
3145− return 1;
3146− return is_compatible_types(t1, t2);
3147− } else if (bt1 == VT_STRUCT) {
3148− return (t2 == t1);
3149− } else if (bt1 == VT_FUNC) {
3150− if (bt2 != VT_FUNC)
3151− return 0;
3152− s1 = sym_find(((unsigned)t1 >> VT_STRUCT_SHIFT));
3153− s2 = sym_find(((unsigned)t2 >> VT_STRUCT_SHIFT));
3154− if (!is_compatible_types(s1->t, s2->t))
3155− return 0;
3156− /* XXX: not complete */
3157− if (s1->c == FUNC_OLD || s2->c == FUNC_OLD)
3158− return 1;
3159− if (s1->c != s2->c)
3160− return 0;
3161− while (s1 != NULL) {
3162− if (s2 == NULL)
3163− return 0;
3164− if (!is_compatible_types(s1->t, s2->t))
3165− return 0;
3166− s1 = s1->next;
3167− s2 = s2->next;
3168− }
3169− if (s2)
3170− return 0;
3171− return 1;
3172− } else {
3173− /* XXX: not complete */
3174− return 1;
3175− }
3176−}
3177−
3178−int check_assign_types(int t1, int t2)
3179−{
3180− t1 &= VT_TYPE;
3181− t2 &= VT_TYPE;
3182− if ((t1 & VT_BTYPE) == VT_PTR &&
3183− (t2 & VT_BTYPE) == VT_FUNC) {
3184− return is_compatible_types(pointed_type(t1), t2);
3185− } else {
3186− return is_compatible_types(t1, t2);
3187− }
3188−}
3189−
3190−
31913432 void uneq(void)
31923433 {
31933434 int t;
@@ -3202,8 +3443,6 @@ void uneq(void) · 8 unmodified lines
32023443 next();
32033444 if (t == '=') {
32043445 expr_eq();
3205− if (!check_assign_types(vtop[-1].t, vtop[0].t))
3206− warning("incompatible types");
32073446 } else {
32083447 vdup();
32093448 expr_eq();
@@ -3289,16 +3528,16 @@ void expr_eq(void) · 79 unmodified lines
32893528 if (const_wanted) {
32903529 sum(10);
32913530 if (tok == '?') {
3292− c = vtop->c;
3531+ c = vtop->c.i;
32933532 vpop();
32943533 next();
3295− expr();
3296− t = vtop->c;
3534+ gexpr();
3535+ t = vtop->c.i;
32973536 vpop();
32983537 skip(':');
32993538 expr_eq();
33003539 if (c)
3301− vtop->c = t;
3540+ vtop->c.i = t;
33023541 }
33033542 } else {
33043543 eor();
@@ -3306,7 +3545,7 @@ void expr_eq(void) · 1 unmodified line
33063545 next();
33073546 t = gtst(1, 0);
33083547
3309− expr();
3548+ gexpr();
33103549 r1 = gv();
33113550 vpop();
33123551 skip(':');
@@ -3322,7 +3561,7 @@ void expr_eq(void) · 9 unmodified lines
33223561 }
33233562 }
33243563
3325−void expr(void)
3564+void gexpr(void)
33263565 {
33273566 while (1) {
33283567 expr_eq();
@@ -3333,16 +3572,24 @@ void expr(void) · 4 unmodified lines
33333572 }
33343573 }
33353574
3336−int expr_const(void)
3575+/* parse a constant expression and return value in vtop */
3576+void expr_const1(void)
33373577 {
3338− int a, c;
3578+ int a;
33393579 a = const_wanted;
33403580 const_wanted = 1;
33413581 expr_eq();
33423582 if ((vtop->t & (VT_CONST | VT_LVAL)) != VT_CONST)
33433583 expect("constant");
33443584 const_wanted = a;
3345− c = vtop->c;
3585+}
3586+
3587+/* parse an integer constant and return its value */
3588+int expr_const(void)
3589+{
3590+ int c;
3591+ expr_const1();
3592+ c = vtop->c.i;
33463593 vpop();
33473594 return c;
33483595 }
@@ -3383,7 +3630,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 34 unmodified lines
33833630 /* if test */
33843631 next();
33853632 skip('(');
3386− expr();
3633+ gexpr();
33873634 skip(')');
33883635 a = gtst(1, 0);
33893636 block(bsym, csym, case_sym, def_sym, case_reg);
@@ -3400,7 +3647,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 10 unmodified lines
34003647 next();
34013648 d = ind;
34023649 skip('(');
3403− expr();
3650+ gexpr();
34043651 skip(')');
34053652 a = gtst(1, 0);
34063653 b = 0;
@@ -3423,7 +3670,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 16 unmodified lines
34233670 } else if (tok == TOK_RETURN) {
34243671 next();
34253672 if (tok != ';') {
3426− expr();
3673+ gexpr();
34273674 if ((func_vt & VT_BTYPE) == VT_STRUCT) {
34283675 /* if returning structure, must copy it to implicit
34293676 first pointer arg location */
@@ -3432,6 +3679,9 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 2 unmodified lines
34323679 vswap();
34333680 /* copy structure value to pointer */
34343681 vstore();
3682+ } else if (is_float(func_vt)) {
3683+ /* move return value to float return register */
3684+ move_reg(FUNC_RET_FREG, gv());
34353685 } else {
34363686 /* move return value to standard return register */
34373687 move_reg(FUNC_RET_REG, gv());
@@ -3459,7 +3709,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 21 unmodified lines
34593709 next();
34603710 skip('(');
34613711 if (tok != ';') {
3462− expr();
3712+ gexpr();
34633713 vpop();
34643714 }
34653715 skip(';');
@@ -3468,14 +3718,14 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 2 unmodified lines
34683718 a = 0;
34693719 b = 0;
34703720 if (tok != ';') {
3471− expr();
3721+ gexpr();
34723722 a = gtst(1, 0);
34733723 }
34743724 skip(';');
34753725 if (tok != ')') {
34763726 e = gjmp(0);
34773727 c = ind;
3478− expr();
3728+ gexpr();
34793729 vpop();
34803730 oad(0xe9, d - ind - 5); /* jmp */
34813731 gsym(e);
@@ -3495,7 +3745,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 13 unmodified lines
34953745 skip(TOK_WHILE);
34963746 skip('(');
34973747 gsym(b);
3498− expr();
3748+ gexpr();
34993749 c = gtst(0, 0);
35003750 gsym_addr(c, d);
35013751 skip(')');
@@ -3505,7 +3755,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 3 unmodified lines
35053755 if (tok == TOK_SWITCH) {
35063756 next();
35073757 skip('(');
3508− expr();
3758+ gexpr();
35093759 case_reg = gv();
35103760 vpop();
35113761 skip(')');
@@ -3582,7 +3832,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 70 unmodified lines
35823832 } else {
35833833 /* expression case */
35843834 if (tok != ';') {
3585− expr();
3835+ gexpr();
35863836 vpop();
35873837 }
35883838 skip(';');
@@ -3670,28 +3920,50 @@ void decl_designator(int t, int c, · 81 unmodified lines
36703920
36713921 void init_putv(int t, int c, int v, int is_expr)
36723922 {
3673− int saved_global_expr;
3923+ int saved_global_expr, bt;
36743924
36753925 if ((t & VT_VALMASK) == VT_CONST) {
36763926 if (is_expr) {
36773927 /* compound literals must be allocated globally in this case */
36783928 saved_global_expr = global_expr;
36793929 global_expr = 1;
3680− v = expr_const();
3930+ expr_const1();
36813931 global_expr = saved_global_expr;
3932+ } else {
3933+ vset(VT_CONST | VT_INT, v);
36823934 }
3683− if ((t & VT_BTYPE) == VT_BYTE)
3684− *(char *)c = v;
3685− else if ((t & VT_BTYPE) == VT_SHORT)
3686− *(short *)c = v;
3687− else
3688− *(int *)c = v;
3935+ /* XXX: do casting */
3936+ /* XXX: not portable */
3937+ bt = vtop->t & VT_BTYPE;
3938+ switch(bt) {
3939+ case VT_BYTE:
3940+ *(char *)c = vtop->c.i;
3941+ break;
3942+ case VT_SHORT:
3943+ *(short *)c = vtop->c.i;
3944+ break;
3945+ case VT_DOUBLE:
3946+ *(double *)c = vtop->c.d;
3947+ break;
3948+ case VT_LDOUBLE:
3949+ *(long double *)c = vtop->c.ld;
3950+ break;
3951+#if 0
3952+ case VT_LLONG:
3953+ *(long long *)c = vtop->c.ll;
3954+ break;
3955+#endif
3956+ default:
3957+ *(int *)c = vtop->c.i;
3958+ break;
3959+ }
3960+ vpop();
36893961 } else {
36903962 vset(t, c);
36913963 if (is_expr)
36923964 expr_eq();
36933965 else
3694− vset(VT_CONST, v);
3966+ vset(VT_CONST | VT_INT, v);
36953967 vstore();
36963968 vpop();
36973969 }
@@ -3969,6 +4241,13 @@ void decl(int l) · 271 unmodified lines
39694241 }
39704242 while (1) { /* iterate thru each declaration */
39714243 t = type_decl(&v, b, TYPE_DIRECT);
4244+#if 0
4245+ {
4246+ char buf[500];
4247+ type_to_str(buf, sizeof(buf), t, get_tok_str(v, NULL));
4248+ printf("type = '%s'\n", buf);
4249+ }
4250+#endif
39724251 if (tok == '{') {
39734252 if (l == VT_LOCAL)
39744253 error("cannot use local functions");
@@ -3982,7 +4261,7 @@ void decl(int l) · 7 unmodified lines
39824261 /* put function address */
39834262 sym_push1(&global_stack, v, VT_CONST | t, ind);
39844263 }
3985− funcname = get_tok_str(v, 0);
4264+ funcname = get_tok_str(v, NULL);
39864265 /* push a dummy symbol to enable local sym storage */
39874266 sym_push1(&local_stack, 0, 0, 0);
39884267 /* define parameters */
@@ -4002,27 +4281,21 @@ void decl(int l) · 13 unmodified lines
40024281 sym_push(sym->v & ~SYM_FIELD,
40034282 u | VT_LOCAL | VT_LVAL,
40044283 addr);
4005− if ((u & VT_BTYPE) == VT_STRUCT) {
4284+ size = type_size(u, &align);
4285+ size = (size + 3) & ~3;
40064286 #ifdef FUNC_STRUCT_PARAM_AS_PTR
4007− /* structs are passed as pointer */
4008− size = 4;
4009−#else
4010− /* structs are directly put on stack (x86
4011− like) */
4012− size = type_size(u, &align);
4013− size = (size + 3) & ~3;
4014−#endif
4015− } else {
4016− /* XXX: size will be different someday */
4287+ /* structs are passed as pointer */
4288+ if ((u & VT_BTYPE) == VT_STRUCT) {
40174289 size = 4;
40184290 }
4291+#endif
40194292 addr += size;
40204293 }
40214294 loc = 0;
40224295 o(0xe58955); /* push %ebp, mov %esp, %ebp */
40234296 a = (int *)oad(0xec81, 0); /* sub $xxx, %esp */
40244297 rsym = 0;
4025− block(0, 0, 0, 0, 0);
4298+ block(NULL, NULL, NULL, NULL, 0);
40264299 gsym(rsym);
40274300 o(0xc3c9); /* leave, ret */
40284301 *a = (-loc + 3) & -4; /* align local size to word &
@@ -4063,9 +4336,9 @@ void decl(int l) · 34 unmodified lines
40634336 goto do_def;
40644337 if (!is_compatible_types(sym->t, u))
40654338 error("incompatible types for redefinition of '%s'",
4066− get_tok_str(v, 0));
4339+ get_tok_str(v, NULL));
40674340 if (!(sym->t & VT_FORWARD))
4068− error("redefinition of '%s'", get_tok_str(v, 0));
4341+ error("redefinition of '%s'", get_tok_str(v, NULL));
40694342 greloc_patch(sym, addr);
40704343 } else {
40714344 do_def:
@@ -4120,7 +4393,7 @@ void resolve_global_syms(void) · 48 unmodified lines
41204393 /* external symbol already exists and is defined :
41214394 patch all references to it */
41224395 if (!(s->t & VT_FORWARD))
4123− error("'%s' defined twice", get_tok_str(s->v, 0));
4396+ error("'%s' defined twice", get_tok_str(s->v, NULL));
41244397 greloc_patch(s, ext_sym->c);
41254398 }
41264399 }
@@ -4192,7 +4465,7 @@ void resolve_extern_syms(void) · 65 unmodified lines
41924465 /* if there is at least one relocation to do, then find it
41934466 and patch it */
41944467 if (s->c) {
4195− str = get_tok_str(s->v, 0);
4468+ str = get_tok_str(s->v, NULL);
41964469 addr = (int)dlsym(NULL, str);
41974470 if (!addr)
41984471 error("unresolved external reference '%s'", str);

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