History

Move logic for if (int value) to tccgen.c

Move the logic to do a test of an integer value (ex if (0)) out of arch-specific code to tccgen.c to avoid code duplication. This also fixes test of long long value which was only testing the bottom half of such values on 32 bits architectures.

Thomas Preud'homme committed 12 years ago

Commit eda2c751 parent c634c796 files · +37 −118Browse files

Unified · Split

6 files
  1. arm-gen.c +1 −24
  2. c67-gen.c +1 −32
  3. i386-gen.c +1 −18
  4. il-gen.c +1 −14
  5. tccgen.c +32 −12
  6. x86_64-gen.c +1 −18
arm-gen.c+1−24
@@ -1390,7 +1390,7 @@ int gtst(int inv, int t) · 1389 unmodified lines
13901390 op|=encbranch(r,t,1);
13911391 o(op);
13921392 t=r;
1393− } else if (v == VT_JMP || v == VT_JMPI) {
1393+ } else { /* VT_JMP || VT_JMPI */
13941394 if ((v & 1) == inv) {
13951395 if(!vtop->c.i)
13961396 vtop->c.i=t;
@@ -1412,29 +1412,6 @@ int gtst(int inv, int t) · 15 unmodified lines
14121412 t = gjmp(t);
14131413 gsym(vtop->c.i);
14141414 }
1415− } else {
1416− if (is_float(vtop->type.t)) {
1417− r=gv(RC_FLOAT);
1418−#ifdef TCC_ARM_VFP
1419− o(0xEEB50A40|(vfpr(r)<<12)|T2CPR(vtop->type.t)); /* fcmpzX */
1420− o(0xEEF1FA10); /* fmstat */
1421−#else
1422− o(0xEE90F118|(fpr(r)<<16));
1423−#endif
1424− vtop->r = VT_CMP;
1425− vtop->c.i = TOK_NE;
1426− return gtst(inv, t);
1427− } else if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
1428− /* constant jmp optimization */
1429− if ((vtop->c.i != 0) != inv)
1430− t = gjmp(t);
1431− } else {
1432− v = gv(RC_INT);
1433− o(0xE3300000|(intr(v)<<16));
1434− vtop->r = VT_CMP;
1435− vtop->c.i = TOK_NE;
1436− return gtst(inv, t);
1437− }
14381415 }
14391416 vtop--;
14401417 return t;
c67-gen.c+1−32
@@ -2103,7 +2103,7 @@ int gtst(int inv, int t) · 2102 unmodified lines
21032103 C67_NOP(5);
21042104 t = ind1; //return where we need to patch
21052105
2106− } else if (v == VT_JMP || v == VT_JMPI) {
2106+ } else { /* VT_JMP || VT_JMPI */
21072107 /* && or || optimization */
21082108 if ((v & 1) == inv) {
21092109 /* insert vtop->c jump list in t */
@@ -2129,37 +2129,6 @@ int gtst(int inv, int t) · 19 unmodified lines
21292129 t = gjmp(t);
21302130 gsym(vtop->c.i);
21312131 }
2132− } else {
2133− if (is_float(vtop->type.t)) {
2134− vpushi(0);
2135− gen_op(TOK_NE);
2136− }
2137− if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
2138− /* constant jmp optimization */
2139− if ((vtop->c.i != 0) != inv)
2140− t = gjmp(t);
2141− } else {
2142− // I think we need to get the value on the stack
2143− // into a register, test it, and generate a branch
2144− // return the address of the branch, so it can be
2145− // later patched
2146−
2147− v = gv(RC_INT); // get value into a reg
2148− ind1 = ind;
2149− C67_MVKL(C67_A0, t); //r=reg to load, constant
2150− C67_MVKH(C67_A0, t); //r=reg to load, constant
2151−
2152− if (v != TREG_EAX && // check if not already in a conditional test reg
2153− v != TREG_EDX && v != TREG_ST0 && v != C67_B2) {
2154− C67_MV(v, C67_B2);
2155− v = C67_B2;
2156− }
2157−
2158− C67_IREG_B_REG(inv, v, C67_A0); // [!R] B.S2x A0
2159− C67_NOP(5);
2160− t = ind1; //return where we need to patch
2161− ind1 = ind;
2162− }
21632132 }
21642133 vtop--;
21652134 return t;
i386-gen.c+1−18
@@ -682,7 +682,7 @@ ST_FUNC int gtst(int inv, int t) · 681 unmodified lines
682682 /* fast case : can jump directly since flags are set */
683683 g(0x0f);
684684 t = psym((vtop->c.i - 16) ^ inv, t);
685− } else if (v == VT_JMP || v == VT_JMPI) {
685+ } else { /* VT_JMP || VT_JMPI */
686686 /* && or || optimization */
687687 if ((v & 1) == inv) {
688688 /* insert vtop->c jump list in t */
@@ -695,23 +695,6 @@ ST_FUNC int gtst(int inv, int t) · 6 unmodified lines
695695 t = gjmp(t);
696696 gsym(vtop->c.i);
697697 }
698− } else {
699− if (is_float(vtop->type.t) ||
700− (vtop->type.t & VT_BTYPE) == VT_LLONG) {
701− vpushi(0);
702− gen_op(TOK_NE);
703− }
704− if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
705− /* constant jmp optimization */
706− if ((vtop->c.i != 0) != inv)
707− t = gjmp(t);
708− } else {
709− v = gv(RC_INT);
710− o(0x85);
711− o(0xc0 + v * 9);
712− g(0x0f);
713− t = psym(0x85 ^ inv, t);
714− }
715698 }
716699 vtop--;
717700 return t;
il-gen.c+1−14
@@ -515,7 +515,7 @@ int gtst(int inv, int t) · 514 unmodified lines
515515 break;
516516 }
517517 t = out_opj(c, t);
518− } else if (v == VT_JMP || v == VT_JMPI) {
518+ } else { /* VT_JMP || VT_JMPI */
519519 /* && or || optimization */
520520 if ((v & 1) == inv) {
521521 /* insert vtop->c jump list in t */
@@ -528,19 +528,6 @@ int gtst(int inv, int t) · 6 unmodified lines
528528 t = gjmp(t);
529529 gsym(vtop->c.i);
530530 }
531− } else {
532− if (is_float(vtop->t)) {
533− vpushi(0);
534− gen_op(TOK_NE);
535− }
536− if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST) {
537− /* constant jmp optimization */
538− if ((vtop->c.i != 0) != inv)
539− t = gjmp(t);
540− } else {
541− v = gv(RC_INT);
542− t = out_opj(IL_OP_BRTRUE - inv, t);
543− }
544531 }
545532 vtop--;
546533 return t;
tccgen.c+32−12
@@ -1095,6 +1095,26 @@ static void gv_dup(void) · 1094 unmodified lines
10951095 }
10961096 }
10971097
1098+/* Generate value test
1099+ *
1100+ * Generate a test for any value (jump, comparison and integers) */
1101+int gvtst(int inv, int t)
1102+{
1103+ int v = vtop->r & VT_VALMASK;
1104+ if (v != VT_CMP && v != VT_JMP && v != VT_JMPI) {
1105+ vpushi(0);
1106+ gen_op(TOK_NE);
1107+ }
1108+ if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
1109+ /* constant jmp optimization */
1110+ if ((vtop->c.i != 0) != inv)
1111+ t = gjmp(t);
1112+ vtop--;
1113+ return t;
1114+ }
1115+ return gtst(inv, t);
1116+}
1117+
10981118 #ifndef TCC_TARGET_X86_64
10991119 /* generate CPU independent (unsigned) long long operations */
11001120 static void gen_opl(int op)
@@ -1293,13 +1313,13 @@ static void gen_opl(int op) · 192 unmodified lines
12931313 b = 0;
12941314 gen_op(op1);
12951315 if (op1 != TOK_NE) {
1296− a = gtst(1, 0);
1316+ a = gvtst(1, 0);
12971317 }
12981318 if (op != TOK_EQ) {
12991319 /* generate non equal test */
13001320 /* XXX: NOT PORTABLE yet */
13011321 if (a == 0) {
1302− b = gtst(0, 0);
1322+ b = gvtst(0, 0);
13031323 } else {
13041324 #if defined(TCC_TARGET_I386)
13051325 b = psym(0x850f, 0);
@@ -1324,7 +1344,7 @@ static void gen_opl(int op) · 18 unmodified lines
13241344 else if (op1 == TOK_GE)
13251345 op1 = TOK_UGE;
13261346 gen_op(op1);
1327− a = gtst(1, a);
1347+ a = gvtst(1, a);
13281348 gsym(b);
13291349 vseti(VT_JMPI, a);
13301350 break;
@@ -3665,7 +3685,7 @@ ST_FUNC void unary(void) · 2334 unmodified lines
36653685 vtop->c.i = vtop->c.i ^ 1;
36663686 else {
36673687 save_regs(1);
3668− vseti(VT_JMP, gtst(1, 0));
3688+ vseti(VT_JMP, gvtst(1, 0));
36693689 }
36703690 break;
36713691 case '~':
@@ -4182,7 +4202,7 @@ static void expr_land(void) · 510 unmodified lines
41824202 t = 0;
41834203 save_regs(1);
41844204 for(;;) {
4185− t = gtst(1, t);
4205+ t = gvtst(1, t);
41864206 if (tok != TOK_LAND) {
41874207 vseti(VT_JMPI, t);
41884208 break;
@@ -4202,7 +4222,7 @@ static void expr_lor(void) · 13 unmodified lines
42024222 t = 0;
42034223 save_regs(1);
42044224 for(;;) {
4205− t = gtst(0, t);
4225+ t = gvtst(0, t);
42064226 if (tok != TOK_LOR) {
42074227 vseti(VT_JMP, t);
42084228 break;
@@ -4264,9 +4284,9 @@ static void expr_cond(void) · 55 unmodified lines
42644284 }
42654285 if (tok == ':' && gnu_ext) {
42664286 gv_dup();
4267− tt = gtst(1, 0);
4287+ tt = gvtst(1, 0);
42684288 } else {
4269− tt = gtst(1, 0);
4289+ tt = gvtst(1, 0);
42704290 gexpr();
42714291 }
42724292 type1 = vtop->type;
@@ -4512,7 +4532,7 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, · 239 unmodified lines
45124532 skip('(');
45134533 gexpr();
45144534 skip(')');
4515− a = gtst(1, 0);
4535+ a = gvtst(1, 0);
45164536 block(bsym, csym, case_sym, def_sym, case_reg, 0);
45174537 c = tok;
45184538 if (c == TOK_ELSE) {
@@ -4529,7 +4549,7 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, · 10 unmodified lines
45294549 skip('(');
45304550 gexpr();
45314551 skip(')');
4532− a = gtst(1, 0);
4552+ a = gvtst(1, 0);
45334553 b = 0;
45344554 block(&a, &b, case_sym, def_sym, case_reg, 0);
45354555 gjmp_addr(d);
@@ -4707,7 +4727,7 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, · 171 unmodified lines
47074727 b = 0;
47084728 if (tok != ';') {
47094729 gexpr();
4710− a = gtst(1, 0);
4730+ a = gvtst(1, 0);
47114731 }
47124732 skip(';');
47134733 if (tok != ')') {
@@ -4736,7 +4756,7 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, · 22 unmodified lines
47364756 skip('(');
47374757 gsym(b);
47384758 gexpr();
4739− c = gtst(0, 0);
4759+ c = gvtst(0, 0);
47404760 gsym_addr(c, d);
47414761 skip(')');
47424762 gsym(a);
x86_64-gen.c+1−18
@@ -1582,7 +1582,7 @@ int gtst(int inv, int t) · 1581 unmodified lines
15821582 }
15831583 g(0x0f);
15841584 t = psym((vtop->c.i - 16) ^ inv, t);
1585− } else if (v == VT_JMP || v == VT_JMPI) {
1585+ } else { /* VT_JMP || VT_JMPI */
15861586 /* && or || optimization */
15871587 if ((v & 1) == inv) {
15881588 /* insert vtop->c jump list in t */
@@ -1595,23 +1595,6 @@ int gtst(int inv, int t) · 6 unmodified lines
15951595 t = gjmp(t);
15961596 gsym(vtop->c.i);
15971597 }
1598− } else {
1599− if (is_float(vtop->type.t) ||
1600− (vtop->type.t & VT_BTYPE) == VT_LLONG) {
1601− vpushi(0);
1602− gen_op(TOK_NE);
1603− }
1604− if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
1605− /* constant jmp optimization */
1606− if ((vtop->c.i != 0) != inv)
1607− t = gjmp(t);
1608− } else {
1609− v = gv(RC_INT);
1610− orex(0,v,v,0x85);
1611− o(0xc0 + REG_VALUE(v) * 9);
1612− g(0x0f);
1613− t = psym(0x85 ^ inv, t);
1614− }
16151598 }
16161599 vtop--;
16171600 return t;

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