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
6297 int tt, u, r1, r2, rc, t1, t2, islv, c, g;6297 int tt, u, r1, r2, rc, t1, t2, islv, c, g;
6298 SValue sv;6298 SValue sv;
6299 CType type;6299 CType type;
6300 int ncw_prev;
6301 6300
6302 expr_lor();6301 expr_lor();
6303 if (tok == '?') {6302 if (tok == '?') {
@@ -6320,7 +6319,6 @@ static void expr_cond(void) · 16 unmodified lines
6320 tt = gvtst(0, 0);6319 tt = gvtst(0, 0);
6321 }6320 }
6322 6321
6323 ncw_prev = nocode_wanted;
6324 if (c == 0)6322 if (c == 0)
6325 nocode_wanted++;6323 nocode_wanted++;
6326 if (!g)6324 if (!g)
@@ -6339,7 +6337,8 @@ static void expr_cond(void) · 12 unmodified lines
6339 } else6337 } else
6340 u = 0;6338 u = 0;
6341 6339
6342 nocode_wanted = ncw_prev;6340 if (c == 0)
6341 nocode_wanted--;
6343 if (c == 1)6342 if (c == 1)
6344 nocode_wanted++;6343 nocode_wanted++;
6345 skip(':');6344 skip(':');
@@ -6356,7 +6355,6 @@ static void expr_cond(void) · 10 unmodified lines
6356 /* combine jump targets of 2nd op with VT_CMP of 1st op */6355 /* combine jump targets of 2nd op with VT_CMP of 1st op */
6357 gvtst_set(0, t1);6356 gvtst_set(0, t1);
6358 gvtst_set(1, t2);6357 gvtst_set(1, t2);
6359 nocode_wanted = ncw_prev;
6360 // tcc_warning("two conditions expr_cond");6358 // tcc_warning("two conditions expr_cond");
6361 return;6359 return;
6362 }6360 }
@@ -6394,7 +6392,8 @@ static void expr_cond(void) · 31 unmodified lines
6394 tt = gjmp(0);6392 tt = gjmp(0);
6395 }6393 }
6396 gsym(u);6394 gsym(u);
6397 nocode_wanted = ncw_prev;6395 if (c == 1)
6396 nocode_wanted--;
6398 6397
6399 /* this is horrible, but we must also convert first6398 /* this is horrible, but we must also convert first
6400 operand */6399 operand */
tests/tests2/33_ternary_op.c+13−0
@@ -39,6 +39,17 @@ static int getme(struct condstruct* s, int i) · 38 unmodified lines
39 return i1 + i2 + i3 + i4;39 return i1 + i2 + i3 + i4;
40}40}
41 41
42int someglobal;
43
44void 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
42int main()53int main()
43{54{
44 int Count;55 int Count;
@@ -73,6 +84,8 @@ int main() · 28 unmodified lines
73 //0 ? a : 0.0;84 //0 ? a : 0.0;
74 }85 }
75 86
87 constantcond();
88
76 89
77 return 0;90 return 0;
78}91}
tests/tests2/33_ternary_op.expect+1−0
@@ -10,3 +10,4 @@ · 9 unmodified lines
10241024
11271127
1215212152
13okay

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