Ejemplo n.º 1
0
        protected override void GenerateCode(CodeGenContext context)
        {
            var target = ThenPart.GetBranchTarget();

            if (target.HasValue)
            {
                //
                // Normally, we evaluate
                //		if ( a ) { b = 1; }
                //	such that if "a" evaluates to false, then we branch around the then-part
                //		and if "a" evaluates to true, we fall-thru to the then-part
                // But when the then-part is itself a goto statement of some sort:
                //		if ( a ) { goto L; }
                //		if ( a ) { break; }
                //		if ( a ) { continue; }
                // Then instead of branching around a branch instruction,
                //	we reverse the evaluation condition and supply the control flow target:
                //	so that if "a" evaluates to true, then we branch (goto/break/continue),
                //		and if "a" evaluates to false, we don't branch
                //
                context.SetPrettyPrintProlog("If-Goto\t");
                Condition.GenerateCodeForConditionalBranchWithPrettyPrint(context, target.Value, true);
                Dump.WriteLine("<then part branch incorporated into preceding condition>");

                context.SetPrettyPrintProlog("ElsePart\t");
                ElsePart?.GenerateCodeWithPrettyPrint(context);
            }
            else if (ElsePart == null)
            {
                context.SetPrettyPrintProlog("Condition\t");
                var joinPoint = context.CreateLabel();
                Condition.GenerateCodeForConditionalBranchWithPrettyPrint(context, joinPoint, false);

                context.SetPrettyPrintProlog("ThenPart");
                ThenPart.GenerateCodeWithPrettyPrint(context);

                context.InsertComment("if-then rejoin");
                context.PlaceLabelHere(joinPoint);
            }
            /* else if ( ElsePart.IsBranchStatement () { }*/
            else
            {
                var elsePartLabel = context.CreateLabel();
                context.SetPrettyPrintProlog("Condition\t");

                Condition.GenerateCodeForConditionalBranchWithPrettyPrint(context, elsePartLabel, false);
                context.SetPrettyPrintProlog("ThenPart\t");
                ThenPart.GenerateCodeWithPrettyPrint(context);

                var joinPoint = context.CreateLabel();
                context.InsertComment("end of ThenPart");
                context.GenerateUnconditionalBranch(joinPoint);

                context.SetPrettyPrintProlog("ElsePart\t");

                context.PlaceLabelHere(elsePartLabel);

                context.InsertComment("start of ElsePart");
                ElsePart.GenerateCodeWithPrettyPrint(context);

                context.InsertComment("if-then-else rejoin");
                context.PlaceLabelHere(joinPoint);
            }
        }
Ejemplo n.º 2
0
 protected override void GenerateCode(CodeGenContext context)
 {
     context.InsertComment("empty statement");
     // do nothing!
 }
Ejemplo n.º 3
0
 protected override void GenerateCode(CodeGenContext context)
 {
     context.InsertComment("declaration statement");
     // do nothing?
 }
Ejemplo n.º 4
0
 protected override void GenerateCode(CodeGenContext context)
 {
     context.InsertComment("return statement");
     ReturnValue?.GenerateCodeForValueWithPrettyPrint(context, EvaluationIntention.Value);
     context.GenerateInstruction("RET");
 }