Example #1
0
        public override INode DoResolve(ResolveContext rc)
        {
            Label lb = rc.DefineLabel(LabelType.IF);

            ExitIf         = rc.DefineLabel(lb.Name + "_EXIT");
            Else           = rc.DefineLabel(lb.Name + "_ELSE");
            ParentIf       = rc.EnclosingIf;
            rc.EnclosingIf = this;


            rc.CreateNewState();
            rc.CurrentGlobalScope |= ResolveScopes.If;

            // enter if
            Expression     = (Expression)Expression.DoResolve(rc);
            TrueStatement  = (Statement)TrueStatement.DoResolve(rc);
            FalseStatement = (Statement)FalseStatement.DoResolve(rc);

            if (Expression.IsVoid)
            {
                ResolveContext.Report.Error(3, Location, "cannot evaluate void type in if statement");
            }

            rc.RestoreOldState();
            // exit current if
            rc.EnclosingIf = ParentIf;


            return(base.DoResolve(rc));
        }
Example #2
0
        void EmitIfConstant(EmitContext ec)
        {
            IntegralExpression ce = null;

            if (Expression is IntegralExpression)
            {
                ce = (IntegralExpression)Expression;
            }

            bool val = ce.Value != 0;

            if (!val) // emit else
            {
                FalseStatement.Emit(ec);
            }
            else
            {
                TrueStatement.Emit(ec);
            }
        }
Example #3
0
        public override string ToString(int indent)
        {
            var sb = new StringBuilder();

            for (int i = 0; i < indent; i++)
            {
                sb.Append("\t");
            }
            sb.AppendLine($"IF {Expression} THEN");
            sb.AppendLine(TrueStatement.ToString(indent + 1));
            for (int i = 0; i < indent; i++)
            {
                sb.Append("\t");
            }
            sb.AppendLine("ELSE");
            sb.AppendLine(FalseStatement.ToString(indent + 1));
            for (int i = 0; i < indent; i++)
            {
                sb.Append("\t");
            }
            sb.AppendLine("ENDIF");
            return(sb.ToString());
        }
        protected override void DoEmitCpp(CppEmitContext cec)
        {
            //
            // If we're a boolean constant, Resolve() already
            // eliminated dead code for us.
            //
            Constant c = expr as Constant;

            if (c != null)
            {
                if (!c.IsDefaultValue)
                {
                    TrueStatement.EmitCpp(cec);
                }
                else if (FalseStatement != null)
                {
                    FalseStatement.EmitCpp(cec);
                }

                return;
            }

            cec.Buf.Write("\tif (", loc);
            expr.EmitCpp(cec);
            cec.Buf.Write(") ");

            cec.Buf.WriteBlockStatement(TrueStatement);

            if (FalseStatement != null)
            {
                cec.Buf.Write(" else ");

                cec.Buf.WriteBlockStatement(FalseStatement);
            }

            cec.Buf.Write("\n");
        }
Example #5
0
 /// <summary>
 /// Emit code
 /// </summary>
 /// <returns>Success or fail</returns>
 public override bool Emit(EmitContext ec)
 {
     if (Expression is IntegralExpression)
     {
         EmitIfConstant(ec);
     }
     else
     {
         // emit expression branchable
         ec.EmitComment("if-expression evaluation");
         Expression.EmitBranchable(ec, Else, false);
         ec.EmitComment("(" + Expression.CommentString() + ") is true");
         TrueStatement.Emit(ec);
         ec.EmitInstruction(new Jump()
         {
             DestinationLabel = ExitIf.Name
         });
         ec.MarkLabel(Else);
         ec.EmitComment("Else ");
         FalseStatement.Emit(ec);
         ec.MarkLabel(ExitIf);
     }
     return(true);
 }
Example #6
0
        protected override void DoEmit(EmitContext ec)
        {
            var endLabel = ec.DefineLabel();

            Condition.Emit(ec);
            ec.EmitCastToBoolean(Condition.GetEvaluatedCType(ec));

            if (FalseStatement == null)
            {
                ec.Emit(OpCode.BranchIfFalse, endLabel);
                TrueStatement.Emit(ec);
            }
            else
            {
                var falseLabel = ec.DefineLabel();
                ec.Emit(OpCode.BranchIfFalse, falseLabel);
                TrueStatement.Emit(ec);
                ec.Emit(OpCode.Jump, endLabel);
                ec.EmitLabel(falseLabel);
                FalseStatement.Emit(ec);
            }

            ec.EmitLabel(endLabel);
        }
Example #7
0
 public override void OptimizeChilds()
 {
     Expression = Expression.Optimize();
     TrueStatement.OptimizeChilds();
     FalseStatement.OptimizeChilds();
 }
Example #8
0
 public override void AddDeclarationToBlock(BlockContext context)
 {
     TrueStatement.AddDeclarationToBlock(context);
     FalseStatement?.AddDeclarationToBlock(context);
 }