@@ -1416,20 +1416,29 @@ struct LowerDefinitions
14161416
14171417 void BuildDeferred (DeferredBody & deferred )
14181418 {
1419+ SpiteIR ::Label * currentLabel = GetCurrentLabel ();
1420+ //Move label terminator to defer end label
1421+ SpiteIR ::Instruction * term = currentLabel -> terminator ;
1422+ currentLabel -> terminator = nullptr ;
1423+
1424+ funcContext .deferCount += 1 ;
1425+ eastl ::string deferStartName = "defer_" + eastl ::to_string (funcContext .deferCount );
1426+ SpiteIR ::Label * deferStartLabel = BuildLabel (deferStartName );
1427+
1428+ SpiteIR ::Instruction * currToDeferStart = BuildJump (currentLabel );
1429+ currToDeferStart -> jump .label = deferStartLabel ;
1430+ AddLabel (deferStartLabel );
1431+
14191432 if (deferred .runTest .reg != InvalidRegister )
14201433 {
1421- SpiteIR ::Label * currentLabel = GetCurrentLabel ();
1422- //Move label terminator to defer end label
1423- SpiteIR ::Instruction * term = currentLabel -> terminator ;
1424- currentLabel -> terminator = nullptr ;
1434+ SpiteIR ::Operand cmpOp = BuildRegisterOperand (HandleAutoCast (deferred .runTest , castBool ));
1435+ SpiteIR ::Instruction * branch = BuildBranch (GetCurrentLabel (), cmpOp );
14251436
1426- eastl ::string deferStartName = "defer_ " + eastl ::to_string (funcContext .deferCount );
1437+ eastl ::string deferTrueName = "defer_true_ " + eastl ::to_string (funcContext .deferCount );
14271438 eastl ::string deferEndName = "defer_end_" + eastl ::to_string (funcContext .deferCount );
14281439 SpiteIR ::Label * deferEndLabel = BuildLabel (deferEndName );
1429- SpiteIR ::Operand cmpOp = BuildRegisterOperand (HandleAutoCast (deferred .runTest , castBool ));
1430- SpiteIR ::Instruction * branch = BuildBranch (GetCurrentLabel (), cmpOp );
14311440
1432- SpiteIR ::Label * deferBodyLabel = BuildLabelBody (deferStartName , deferred .body );
1441+ SpiteIR ::Label * deferBodyLabel = BuildLabelBody (deferTrueName , deferred .body );
14331442 SpiteIR ::Label * currentDeferBodyLabel = GetCurrentLabel ();
14341443 SpiteIR ::Instruction * bodyToEnd = BuildJump (currentDeferBodyLabel );
14351444 bodyToEnd -> jump .label = deferEndLabel ;
@@ -1442,6 +1451,8 @@ struct LowerDefinitions
14421451 else
14431452 {
14441453 BuildBody (deferred .body );
1454+ SpiteIR ::Label * currentDeferBodyLabel = GetCurrentLabel ();
1455+ currentDeferBodyLabel -> terminator = term ;
14451456 }
14461457 }
14471458
0 commit comments