@@ -4577,15 +4577,26 @@ export class Compiler extends DiagnosticEmitter {
4577
4577
4578
4578
// simplify if only interested in true or false
4579
4579
if ( contextualType == Type . bool || contextualType == Type . void ) {
4580
- rightExpr = this . compileExpression ( right , leftType , inheritedConstraints ) ;
4581
- rightType = this . currentType ;
4582
- rightFlow . freeScopedLocals ( ) ;
4580
+ leftExpr = this . makeIsTrueish ( leftExpr , leftType , left ) ;
4581
+
4582
+ // shortcut if lhs is always false
4583
+ let condKind = this . evaluateCondition ( leftExpr ) ;
4584
+ if ( condKind == ConditionKind . FALSE ) {
4585
+ expr = leftExpr ;
4586
+ } else {
4587
+ rightExpr = this . compileExpression ( right , leftType , inheritedConstraints ) ;
4588
+ rightType = this . currentType ;
4589
+ rightFlow . freeScopedLocals ( ) ;
4590
+ rightExpr = this . makeIsTrueish ( rightExpr , rightType , right ) ;
4591
+
4592
+ // simplify if lhs is always true
4593
+ if ( condKind == ConditionKind . TRUE ) {
4594
+ expr = rightExpr ;
4595
+ } else {
4596
+ expr = module . if ( leftExpr , rightExpr , module . i32 ( 0 ) ) ;
4597
+ }
4598
+ }
4583
4599
this . currentFlow = flow ;
4584
- expr = module . if (
4585
- this . makeIsTrueish ( leftExpr , leftType , left ) ,
4586
- this . makeIsTrueish ( rightExpr , rightType , right ) ,
4587
- module . i32 ( 0 )
4588
- ) ;
4589
4600
this . currentType = Type . bool ;
4590
4601
4591
4602
} else {
@@ -4630,15 +4641,26 @@ export class Compiler extends DiagnosticEmitter {
4630
4641
4631
4642
// simplify if only interested in true or false
4632
4643
if ( contextualType == Type . bool || contextualType == Type . void ) {
4633
- rightExpr = this . compileExpression ( right , leftType , inheritedConstraints ) ;
4634
- rightType = this . currentType ;
4635
- rightFlow . freeScopedLocals ( ) ;
4644
+ leftExpr = this . makeIsTrueish ( leftExpr , leftType , left ) ;
4645
+
4646
+ // shortcut if lhs is always true
4647
+ let condKind = this . evaluateCondition ( leftExpr ) ;
4648
+ if ( condKind == ConditionKind . TRUE ) {
4649
+ expr = leftExpr ;
4650
+ } else {
4651
+ rightExpr = this . compileExpression ( right , leftType , inheritedConstraints ) ;
4652
+ rightType = this . currentType ;
4653
+ rightFlow . freeScopedLocals ( ) ;
4654
+ rightExpr = this . makeIsTrueish ( rightExpr , rightType , right ) ;
4655
+
4656
+ // simplify if lhs is always false
4657
+ if ( condKind == ConditionKind . FALSE ) {
4658
+ expr = rightExpr ;
4659
+ } else {
4660
+ expr = module . if ( leftExpr , module . i32 ( 1 ) , rightExpr ) ;
4661
+ }
4662
+ }
4636
4663
this . currentFlow = flow ;
4637
- expr = module . if (
4638
- this . makeIsTrueish ( leftExpr , leftType , left ) ,
4639
- module . i32 ( 1 ) ,
4640
- this . makeIsTrueish ( rightExpr , rightType , right )
4641
- ) ;
4642
4664
this . currentType = Type . bool ;
4643
4665
4644
4666
} else {
@@ -10037,6 +10059,7 @@ export class Compiler extends DiagnosticEmitter {
10037
10059
10038
10060
/** Evaluates a boolean condition, determining whether it is TRUE, FALSE or UNKNOWN. */
10039
10061
evaluateCondition ( expr : ExpressionRef ) : ConditionKind {
10062
+ assert ( getExpressionType ( expr ) == TypeRef . I32 ) ;
10040
10063
var module = this . module ;
10041
10064
var evaled = module . runExpression ( expr , ExpressionRunnerFlags . Default ) ;
10042
10065
if ( evaled ) {
0 commit comments