Ejemplo n.º 1
0
 public AstTryCatchFinally(ISourcePosition p, AstBlock lexicalScope)
     : base(p, lexicalScope)
 {
     TryBlock = new AstScopedBlock(p, this);
     CatchBlock = new AstScopedBlock(p, TryBlock);
     FinallyBlock = new AstScopedBlock(p, TryBlock);
 }
Ejemplo n.º 2
0
 public AstCondition(ISourcePosition p, AstBlock parentBlock, AstExpr condition, bool isNegative = false)
     : base(p)
 {
     IfBlock = new AstScopedBlock(p,parentBlock,prefix: "if");
     ElseBlock = new AstScopedBlock(p,parentBlock,prefix:"else");
     if (condition == null)
         throw new ArgumentNullException("condition");
     Condition = condition;
     IsNegative = isNegative;
 }
Ejemplo n.º 3
0
        public void DuplicatingJustEffectBlockExpression()
        {
            var ldr =
                Compile(@"
var s;
function main()[is volatile;]
{
    s = ""BEGIN--"";
}
");
            var pos = new SourcePosition("file", -1, -2);
            var mn = ldr.ParentApplication.Module.Name;
            var ct = ldr.FunctionTargets["main"];
            ct.Function.Code.RemoveAt(ct.Function.Code.Count - 1);
            var block = new AstScopedBlock(new SourcePosition("file", -1, -2),ct.Ast);

            var assignStmt = ct.Factory.Call(pos, EntityRef.Variable.Global.Create("s",mn),PCall.Set);
            assignStmt.Arguments.Add(new AstConstant("file", -1, -2, "stmt."));
            var incStmt = ct.Factory.ModifyingAssignment(NoSourcePosition.Instance,
                                                         assignStmt,
                                                         BinaryOperator.Addition);

            var assignExpr = ct.Factory.Call(pos, EntityRef.Variable.Global.Create("s", mn), PCall.Set);
            assignExpr.Arguments.Add(new AstConstant("file", -1, -2, "expr."));
            var incExpr = ct.Factory.ModifyingAssignment(pos, assignExpr, BinaryOperator.Addition);

            block.Statements.Add(incStmt);
            block.Expression = incExpr;

            Assert.That(block,Is.InstanceOf<AstExpr>(),string.Format("{0} is expected to handle emission of effect code.", block));
            block.EmitEffectCode(ct);

            var sourcePosition = new SourcePosition("file", -1, -2);
            ct.EmitLoadGlobal(sourcePosition, "s", null);
            ct.Emit(sourcePosition, OpCode.ret_value);

            if (CompileToCil)
                Prexonite.Compiler.Cil.Compiler.Compile(ldr, target, StaticLinking);

            Expect("BEGIN--stmt.expr.");
        }
Ejemplo n.º 4
0
 /// <summary>
 /// This constructor should only be called from the public constructor.
 /// It is just here to wire up the loop block to be a sub block of the 
 /// initialization and next iteration blocks. (So that symbols declared in 
 /// initialization are available in the loop body)
 /// </summary>
 /// <param name="position">The source position for this node and all block nodes.</param>
 /// <param name="nextBlock">The block reserved for the "next iteration" code. 
 /// It's parent block must be the initialization block.</param>
 private AstForLoop(ISourcePosition position, AstScopedBlock nextBlock)
     : base(position, nextBlock)
 {
     _initialize = (AstScopedBlock)nextBlock.LexicalScope;
     _nextIteration = nextBlock;
 }
Ejemplo n.º 5
0
        protected override void DoEmitCode(CompilerTarget target, StackSemantics stackSemantics)
        {
            //Optimize condition
            _OptimizeNode(target, ref Condition);

            // Invert condition when unary logical not
            AstIndirectCall unaryCond;
            while (Condition.IsCommandCall(Commands.Core.Operators.LogicalNot.DefaultAlias, out unaryCond))
            {
                Condition = unaryCond.Arguments[0];
                IsNegative = !IsNegative;
            }

            //Constant conditions
            if (Condition is AstConstant)
            {
                var constCond = (AstConstant) Condition;
                PValue condValue;
                if (
                    !constCond.ToPValue(target).TryConvertTo(
                        target.Loader, PType.Bool, out condValue))
                    goto continueFull;
                else if (((bool) condValue.Value) ^ IsNegative)
                    IfBlock.EmitEffectCode(target);
                else
                    ElseBlock.EmitEffectCode(target);
                return;
            }
            //Conditions with empty blocks
            if (IfBlock.IsEmpty && ElseBlock.IsEmpty)
            {
                Condition.EmitEffectCode(target);
                return;
            }
            continueFull:
            ;

            //Switch If and Else block in case the if-block is empty
            if (IfBlock.IsEmpty)
            {
                IsNegative = !IsNegative;
                var tmp = IfBlock;
                IfBlock = ElseBlock;
                ElseBlock = tmp;
            }

            var elseLabel = "else\\" + _depth + "\\assembler";
            var endLabel = "endif\\" + _depth + "\\assembler";
            _depth++;

            //Emit
            var ifGoto = IfBlock.IsSingleStatement
                ? IfBlock[0] as AstExplicitGoTo
                : null;
            var elseGoto = ElseBlock.IsSingleStatement
                ? ElseBlock[0] as AstExplicitGoTo
                : null;
            ;

            var ifIsGoto = ifGoto != null;
            var elseIsGoto = elseGoto != null;

            if (ifIsGoto && elseIsGoto)
            {
                //only jumps
                AstLazyLogical.EmitJumpCondition(
                    target,
                    Condition,
                    ifGoto.Destination,
                    elseGoto.Destination,
                    !IsNegative);
            }
            else if (ifIsGoto)
            {
                //if => jump / else => block
                AstLazyLogical.EmitJumpCondition(target, Condition, ifGoto.Destination, !IsNegative);
                ElseBlock.EmitEffectCode(target);
            }
            else if (elseIsGoto)
            {
                //if => block / else => jump
                AstLazyLogical.EmitJumpCondition(
                    target, Condition, elseGoto.Destination, IsNegative); //inverted
                IfBlock.EmitEffectCode(target);
            }
            else
            {
                //if => block / else => block
                AstLazyLogical.EmitJumpCondition(target, Condition, elseLabel, IsNegative);
                IfBlock.EmitEffectCode(target);
                target.EmitJump(Position, endLabel);
                target.EmitLabel(Position, elseLabel);
                ElseBlock.EmitEffectCode(target);
                target.EmitLabel(Position, endLabel);
            }

            target.FreeLabel(elseLabel);
            target.FreeLabel(endLabel);
        }
Ejemplo n.º 6
0
 public AstUsing([NotNull] ISourcePosition p, 
     [NotNull] AstBlock lexicalScope)
     : base(p, lexicalScope)
 {
     _block = new AstScopedBlock(p, this,prefix:LabelPrefix);
 }
Ejemplo n.º 7
0
 public AstScopedBlock BeginBlock(string prefix)
 {
     var currentBlock = CurrentBlock;
     var bl = new AstScopedBlock(currentBlock.Position, currentBlock, GenerateLocalId(), prefix);
     _scopeBlocks.Push(bl);
     return bl;
 }
Ejemplo n.º 8
0
 public void BeginBlock(AstScopedBlock bl)
 {
     if (bl == null)
         throw new ArgumentNullException("bl");
     _scopeBlocks.Push(bl);
 }