@@ -135,6 +135,39 @@ bool VerifyIntegerType(AST *astForError, AST *typ, const char *opname)
135
135
return false;
136
136
}
137
137
138
+ /*
139
+ * check the current boolean type
140
+ */
141
+ AST *
142
+ CurBoolType ()
143
+ {
144
+ int language = GetCurrentLang ();
145
+ if (IsCLang (language )) {
146
+ return ast_type_c_boolean ;
147
+ }
148
+ return ast_type_basic_boolean ;
149
+ }
150
+
151
+ /*
152
+ * provide a result for unordered comparisons
153
+ * like NaN = NaN
154
+ */
155
+ static int
156
+ UnorderedResult (int op )
157
+ {
158
+ switch (op ) {
159
+ case '>' :
160
+ case K_GE :
161
+ return -1 ;
162
+ case '<' :
163
+ case K_LE :
164
+ case K_EQ :
165
+ case K_NE :
166
+ default :
167
+ return 1 ;
168
+ }
169
+ }
170
+
138
171
// create a call to function func with parameters ast->left, ast->right
139
172
// there is an optional 3rd argument too
140
173
static AST *
@@ -407,30 +440,34 @@ domakefloat(AST *typ, AST *ast)
407
440
}
408
441
409
442
static AST *
410
- dofloatToInt (AST * ast , AST * typ )
443
+ dofloatToInt (AST * ast , AST * srctyp , AST * dsttyp )
411
444
{
412
445
AST * ret ;
413
446
414
- if (gl_fixedreal ) {
447
+ if (IsBoolType (dsttyp )) {
448
+ ret = AstOperator (K_NE , NULL , NULL );
449
+ if (gl_fixedreal ) {
450
+ ret -> left = ast ;
451
+ } else {
452
+ ret -> left = MakeOperatorCall (float_cmp , ast , AstInteger (0 ), AstInteger (UnorderedResult (K_NE )));
453
+ }
454
+ ret -> right = AstInteger (0 );
455
+ } else if (gl_fixedreal ) {
415
456
// FIXME: should we round here??
416
457
ret = AstOperator (K_SAR , ast , AstInteger (G_FIXPOINT ));
417
- return ret ;
418
- }
419
- if (IsFloat64Type (typ )) {
420
- ast = MakeOperatorCall (double_toint , ast , NULL , NULL );
421
- return ast ;
422
- }
423
- if (IsConstExpr (ast )) {
458
+ } else if (IsFloat64Type (srctyp )) {
459
+ ret = MakeOperatorCall (double_toint , ast , NULL , NULL );
460
+ } else if (IsConstExpr (ast )) {
424
461
union f_or_i {
425
462
float floatbits ;
426
463
int intbits ;
427
464
} g ;
428
465
g .intbits = EvalConstExpr (ast );
429
- ast = AstInteger ((int )g .floatbits );
466
+ ret = AstInteger ((int )g .floatbits );
430
467
} else {
431
- ast = MakeOperatorCall (float_toint , ast , NULL , NULL );
468
+ ret = MakeOperatorCall (float_toint , ast , NULL , NULL );
432
469
}
433
- return ast ;
470
+ return ret ;
434
471
}
435
472
436
473
static AST *
@@ -453,11 +490,11 @@ dofloatToDouble(AST *ast, AST *typ)
453
490
bool MakeBothIntegers (AST * ast , AST * ltyp , AST * rtyp , const char * opname )
454
491
{
455
492
if (IsFloatType (ltyp )) {
456
- ast -> left = dofloatToInt (ast -> left , ltyp );
493
+ ast -> left = dofloatToInt (ast -> left , ltyp , ast_type_long );
457
494
ltyp = ast_type_long ;
458
495
}
459
496
if (IsFloatType (rtyp )) {
460
- ast -> right = dofloatToInt (ast -> right , rtyp );
497
+ ast -> right = dofloatToInt (ast -> right , rtyp , ast_type_long );
461
498
rtyp = ast_type_long ;
462
499
}
463
500
return VerifyIntegerType (ast , ltyp , opname ) && VerifyIntegerType (ast , rtyp , opname );
@@ -478,11 +515,11 @@ HandleTwoNumerics(int op, AST *ast, AST *lefttype, AST *righttype)
478
515
if (op == K_MODULUS ) {
479
516
// MOD operator converts float operands to integer
480
517
if (IsFloatType (lefttype )) {
481
- ast -> left = dofloatToInt (ast -> left , lefttype );
518
+ ast -> left = dofloatToInt (ast -> left , lefttype , ast_type_long );
482
519
lefttype = ast_type_long ;
483
520
}
484
521
if (IsFloatType (righttype )) {
485
- ast -> right = dofloatToInt (ast -> right , righttype );
522
+ ast -> right = dofloatToInt (ast -> right , righttype , ast_type_long );
486
523
righttype = ast_type_long ;
487
524
}
488
525
}
@@ -733,26 +770,6 @@ IsBasicString(AST *typ)
733
770
return 0 ;
734
771
}
735
772
736
- /*
737
- * provide a result for unordered comparisons
738
- * like NaN = NaN
739
- */
740
- static int
741
- UnorderedResult (int op )
742
- {
743
- switch (op ) {
744
- case '>' :
745
- case K_GE :
746
- return -1 ;
747
- case '<' :
748
- case K_LE :
749
- case K_EQ :
750
- case K_NE :
751
- default :
752
- return 1 ;
753
- }
754
- }
755
-
756
773
void CompileComparison (int op , AST * ast , AST * lefttype , AST * righttype )
757
774
{
758
775
int isfloat = 0 ;
@@ -853,7 +870,9 @@ void CompileComparison(int op, AST *ast, AST *lefttype, AST *righttype)
853
870
int lsize = TypeSize (lefttype );
854
871
int rsize = TypeSize (righttype );
855
872
if (lsize == 4 && rsize == 4 && op != K_EQ && op != K_NE ) {
856
- WARNING (ast , "signed/unsigned comparison may not work properly" );
873
+ if (!IsBoolType (lefttype ) && !IsBoolType (righttype )) {
874
+ WARNING (ast , "signed/unsigned comparison may not work properly" );
875
+ }
857
876
}
858
877
}
859
878
}
@@ -982,11 +1001,11 @@ AST *CoerceOperatorTypes(AST *ast, AST *lefttype, AST *righttype)
982
1001
case K_SAR :
983
1002
case K_SHL :
984
1003
if (lefttype && IsFloatType (lefttype )) {
985
- ast -> left = dofloatToInt (ast -> left , lefttype );
1004
+ ast -> left = dofloatToInt (ast -> left , lefttype , ast_type_long );
986
1005
lefttype = ExprType (ast -> left );
987
1006
}
988
1007
if (righttype && IsFloatType (righttype )) {
989
- ast -> right = dofloatToInt (ast -> right , righttype );
1008
+ ast -> right = dofloatToInt (ast -> right , righttype , ast_type_long );
990
1009
righttype = ExprType (ast -> right );
991
1010
}
992
1011
if (ast -> d .ival == K_SAR && lefttype && IsUnsignedType (lefttype )) {
@@ -1068,8 +1087,12 @@ AST *CoerceOperatorTypes(AST *ast, AST *lefttype, AST *righttype)
1068
1087
case K_NE :
1069
1088
case K_GE :
1070
1089
case '>' :
1090
+ case K_LTU :
1091
+ case K_LEU :
1092
+ case K_GTU :
1093
+ case K_GEU :
1071
1094
CompileComparison (ast -> d .ival , ast , lefttype , righttype );
1072
- return ast_type_long ;
1095
+ return CurBoolType () ;
1073
1096
case K_NEGATE :
1074
1097
case K_ABS :
1075
1098
case K_SQRT :
@@ -1179,19 +1202,19 @@ AST *CoerceOperatorTypes(AST *ast, AST *lefttype, AST *righttype)
1179
1202
if (IsFloatType (lefttype )) {
1180
1203
isfloat64 = IsFloat64Type (lefttype );
1181
1204
ast -> left = MakeOperatorCall (isfloat64 ? double_cmp : float_cmp , ast -> left , AstInteger (0 ), AstInteger (1 ));
1182
- lefttype = ast_type_long ;
1205
+ lefttype = CurBoolType () ;
1183
1206
}
1184
1207
if (IsFloatType (righttype )) {
1185
1208
isfloat64 = IsFloat64Type (righttype );
1186
1209
ast -> right = MakeOperatorCall (isfloat64 ? double_cmp : float_cmp , ast -> right , AstInteger (0 ), AstInteger (1 ));
1187
- righttype = ast_type_long ;
1210
+ righttype = CurBoolType () ;
1188
1211
}
1189
1212
if (lefttype && !IsBoolCompatibleType (lefttype )) {
1190
1213
ERROR (ast , "Expression not compatible with boolean operation" );
1191
1214
} else if (righttype && !IsBoolCompatibleType (righttype )) {
1192
1215
ERROR (ast , "Expression not compatible with boolean operation" );
1193
1216
}
1194
- return ast_type_long ;
1217
+ return CurBoolType () ;
1195
1218
case K_INCREMENT :
1196
1219
case K_DECREMENT :
1197
1220
if ( (lefttype && IsConstType (lefttype ) )
@@ -1292,7 +1315,7 @@ AST *CoerceAssignTypes(AST *line, int kind, AST **astptr, AST *desttype, AST *sr
1292
1315
if (!astptr ) {
1293
1316
ERROR (line , "Unable to convert float function result to integer" );
1294
1317
} else {
1295
- expr = dofloatToInt (expr , srctype );
1318
+ expr = dofloatToInt (expr , srctype , desttype );
1296
1319
* astptr = expr ;
1297
1320
}
1298
1321
AstReportDone (& saveinfo );
@@ -1475,7 +1498,7 @@ doCast(AST *desttype, AST *srctype, AST *src)
1475
1498
}
1476
1499
if (IsPointerType (desttype ) || IsGenericType (desttype )) {
1477
1500
if (IsFloatType (srctype )) {
1478
- src = dofloatToInt (src , srctype );
1501
+ src = dofloatToInt (src , srctype , ast_type_long );
1479
1502
srctype = ast_type_long ;
1480
1503
}
1481
1504
if (IsArrayType (srctype )) {
@@ -1534,7 +1557,7 @@ doCast(AST *desttype, AST *srctype, AST *src)
1534
1557
}
1535
1558
if (IsIntType (desttype )) {
1536
1559
if (IsFloatType (srctype )) {
1537
- src = dofloatToInt (src , srctype );
1560
+ src = dofloatToInt (src , srctype , ast_type_long );
1538
1561
srctype = ast_type_long ;
1539
1562
}
1540
1563
if (IsPointerType (srctype )) {
0 commit comments