History

Fix type of cond-op

the optimization of a ternary op with two boolean operands had the same problem (as in the last commit) of loosing the type.

Michael Matz committed 3 years ago

Commit ef3eb021 parent 96b31ba2 files · +10 −7Browse files

Unified · Split

2 files
  1. tccgen.c +9 −7
  2. tests/tcctest.c +1 −0
tccgen.c+9−7
@@ -6351,6 +6351,14 @@ static void expr_cond(void) · 6350 unmodified lines
63516351 skip(':');
63526352 expr_cond();
63536353
6354+ if ((vtop->type.t & VT_BTYPE) == VT_FUNC)
6355+ mk_pointer(&vtop->type);
6356+
6357+ /* cast operands to correct type according to ISOC rules */
6358+ if (!combine_types(&type, &sv, vtop, '?'))
6359+ type_incompatibility_error(&sv.type, &vtop->type,
6360+ "type mismatch in conditional expression (have '%s' and '%s')");
6361+
63546362 if (c < 0 && is_cond_bool(vtop) && is_cond_bool(&sv)) {
63556363 /* optimize "if (f ? a > b : c || d) ..." for example, where normally
63566364 "a < b" and "c || d" would be forced to "(int)0/1" first, whereas
@@ -6362,17 +6370,11 @@ static void expr_cond(void) · 5 unmodified lines
63626370 /* combine jump targets of 2nd op with VT_CMP of 1st op */
63636371 gvtst_set(0, t1);
63646372 gvtst_set(1, t2);
6373+ gen_cast(&type);
63656374 // tcc_warning("two conditions expr_cond");
63666375 return;
63676376 }
63686377
6369− if ((vtop->type.t & VT_BTYPE) == VT_FUNC)
6370− mk_pointer(&vtop->type);
6371−
6372− /* cast operands to correct type according to ISOC rules */
6373− if (!combine_types(&type, &sv, vtop, '?'))
6374− type_incompatibility_error(&sv.type, &vtop->type,
6375− "type mismatch in conditional expression (have '%s' and '%s')");
63766378 /* keep structs lvalue by transforming `(expr ? a : b)` to `*(expr ? &a : &b)` so
63776379 that `(expr ? a : b).mem` does not error with "lvalue expected" */
63786380 islv = (vtop->r & VT_LVAL) && (sv.r & VT_LVAL) && VT_STRUCT == (type.t & VT_BTYPE);
tests/tcctest.c+1−0
@@ -1269,6 +1269,7 @@ void bool_test() · 1268 unmodified lines
12691269 t = 1;
12701270 printf("type of bool: %d\n", (int) ( (~ ((unsigned int) (t && 1))) / 2) );
12711271
1272+ printf("type of cond: %d\n", (~(t ? 0U : (unsigned int)0)) / 2 );
12721273 /* test ? : cast */
12731274 {
12741275 int aspect_on;

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