History

Fix more jumpopts (in ternary op)

the code in expr_cond save nocode_wanted around some parts of expression evaluation, but at the wrong spots. If the evaluation of the condition itself (e.g. in the testcase the first whole ternary expression) resulted in CODE_OFF, then that was saved, and restored before return, even if in-between codegen would have CODE_ON'ed already. Thus the whole CODE_OFF state bled out to outside the expression evaluation and also disabled the whole if-block. Found by yarpgen v1 (seed 64).

Michael Matz committed 3 years ago

Commit 2cf3a6e1 parent ec818773 files · +18 −5Browse files

Unified · Split

3 files
  1. tccgen.c +4 −5
  2. tests/tests2/33_ternary_op.c +13 −0
  3. tests/tests2/33_ternary_op.expect +1 −0
tccgen.c+4−5
@@ -6297,7 +6297,6 @@ static void expr_cond(void) · 6296 unmodified lines
62976297 int tt, u, r1, r2, rc, t1, t2, islv, c, g;
62986298 SValue sv;
62996299 CType type;
6300− int ncw_prev;
63016300
63026301 expr_lor();
63036302 if (tok == '?') {
@@ -6320,7 +6319,6 @@ static void expr_cond(void) · 16 unmodified lines
63206319 tt = gvtst(0, 0);
63216320 }
63226321
6323− ncw_prev = nocode_wanted;
63246322 if (c == 0)
63256323 nocode_wanted++;
63266324 if (!g)
@@ -6339,7 +6337,8 @@ static void expr_cond(void) · 12 unmodified lines
63396337 } else
63406338 u = 0;
63416339
6342− nocode_wanted = ncw_prev;
6340+ if (c == 0)
6341+ nocode_wanted--;
63436342 if (c == 1)
63446343 nocode_wanted++;
63456344 skip(':');
@@ -6356,7 +6355,6 @@ static void expr_cond(void) · 10 unmodified lines
63566355 /* combine jump targets of 2nd op with VT_CMP of 1st op */
63576356 gvtst_set(0, t1);
63586357 gvtst_set(1, t2);
6359− nocode_wanted = ncw_prev;
63606358 // tcc_warning("two conditions expr_cond");
63616359 return;
63626360 }
@@ -6394,7 +6392,8 @@ static void expr_cond(void) · 31 unmodified lines
63946392 tt = gjmp(0);
63956393 }
63966394 gsym(u);
6397− nocode_wanted = ncw_prev;
6395+ if (c == 1)
6396+ nocode_wanted--;
63986397
63996398 /* this is horrible, but we must also convert first
64006399 operand */
tests/tests2/33_ternary_op.c+13−0
@@ -39,6 +39,17 @@ static int getme(struct condstruct* s, int i) · 38 unmodified lines
3939 return i1 + i2 + i3 + i4;
4040 }
4141
42+int someglobal;
43+
44+void constantcond(void)
45+{
46+ /* This was broken by 8227db3a2, it saved/restored the CODE_OFF state
47+ during the expression and that bled out to the outer one disabling
48+ codegen for if-body. */
49+ if (( (someglobal ? 0 : 0) ? 8 : 9))
50+ printf("okay\n");
51+}
52+
4253 int main()
4354 {
4455 int Count;
@@ -73,6 +84,8 @@ int main() · 28 unmodified lines
7384 //0 ? a : 0.0;
7485 }
7586
87+ constantcond();
88+
7689
7790 return 0;
7891 }
tests/tests2/33_ternary_op.expect+1−0
@@ -10,3 +10,4 @@ · 9 unmodified lines
1010 24
1111 27
1212 152
13+okay

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