protected override BoundStatement RewriteWhileStatement(BoundWhileStatement node) { // while <condition> // <body> // // -----> // // goto continue // body: // <body> // continue: // gotoTrue <condition> body // break: var bodyLabel = GenerateLabel(); var result = BoundNodeFactory.Block(node.Syntax, BoundNodeFactory.Goto(node.Syntax, node.ContinueLabel), BoundNodeFactory.Label(node.Syntax, bodyLabel), node.Body, BoundNodeFactory.Label(node.Syntax, node.ContinueLabel), BoundNodeFactory.GotoTrue(node.Syntax, bodyLabel, node.Condition), BoundNodeFactory.Label(node.Syntax, node.BreakLabel) ); return(RewriteStatement(result)); }
protected override BoundStatement RewriteIfStatement(BoundIfStatement node) { if (node.ElseStatement == null) { // if <condition> // <then> // // ----> // // gotoFalse <condition> end // <then> // end: var endLabel = GenerateLabel(); var result = BoundNodeFactory.Block(node.Syntax, BoundNodeFactory.GotoFalse(node.Syntax, endLabel, node.Condition), node.ThenStatement, BoundNodeFactory.Label(node.Syntax, endLabel) ); return(RewriteStatement(result)); } else { // if <condition> // <then> // else // <else> // // ----> // // gotoFalse <condition> else // <then> // goto end // else: // <else> // end: var elseLabel = GenerateLabel(); var endLabel = GenerateLabel(); var result = BoundNodeFactory.Block(node.Syntax, BoundNodeFactory.GotoFalse(node.Syntax, elseLabel, node.Condition), node.ThenStatement, BoundNodeFactory.Goto(node.Syntax, endLabel), BoundNodeFactory.Label(node.Syntax, elseLabel), node.ElseStatement, BoundNodeFactory.Label(node.Syntax, endLabel) ); return(RewriteStatement(result)); } }
protected override BoundStatement RewriteConditionalGotoStatement(BoundConditionalGotoStatement node) { if (node.Condition.ConstantValue != null) { var condition = (bool)node.Condition.ConstantValue.Value !; condition = node.JumpIfTrue ? condition : !condition; if (condition) { return(RewriteStatement(BoundNodeFactory.Goto(node.Syntax, node.Label))); } else { return(RewriteStatement(BoundNodeFactory.Nop(node.Syntax))); } } return(base.RewriteConditionalGotoStatement(node)); }