示例#1
0
        private ForStatementUpNode Visit(ForStatementUpNode node)
        {
            Visit(node.expr1);
            builder.BuildStore(GetProperty(node.expr1).addr, LookupSymbolOrAlloc(node.Identifier));
            var condBB = func.AppendBasicBlock($"cond_{++count}");
            var bodyBB = func.AppendBasicBlock($"body_{++count}");
            var contBB = func.AppendBasicBlock($"cont_{++count}");

            builder.BuildBr(condBB);
            builder.PositionAtEnd(condBB);
            Visit(node.expr2);
            var t = builder.BuildLoad(LookupSymbolOrAlloc(node.Identifier));
            var b = builder.BuildICmp(LLVMIntPredicate.LLVMIntSLT, t, GetProperty(node.expr2).addr);

            builder.BuildCondBr(b, bodyBB, contBB);
            builder.PositionAtEnd(bodyBB);
            Visit(node.StatementNode);
            t = builder.BuildLoad(LookupSymbolOrAlloc(node.Identifier));
            var r = builder.BuildAdd(t, LLVMValueRef.CreateConstInt(LLVMTypeRef.Int32, 1));

            builder.BuildStore(r, LookupSymbolOrAlloc(node.Identifier));
            builder.BuildBr(condBB);
            builder.PositionAtEnd(contBB);
            return(node);
        }
示例#2
0
        private ForStatementUpNode Visit(ForStatementUpNode node)
        {
            var beginLabel = MakeLabel();
            var trueLabel  = MakeLabel();
            var nextLabel  = MakeLabel();

            node.expr1.Visit(this);
            generatedCode.Add(new CodeEntity()
            {
                Code = $"{node.Identifier} = {properties[node.expr1].addr}"
            });
            generatedCode.Add(beginLabel);
            node.expr2.Visit(this);
            generatedCode.Add(new CodeEntity()
            {
                Code = $"if {node.Identifier} < {properties[node.expr2].addr} goto <label:{trueLabel.LabelId}>"
            });
            generatedCode.Add(MakeLabeledGoto(nextLabel));
            generatedCode.Add(trueLabel);
            node.StatementNode.Visit(this);
            var temp = MakeTemp();

            generatedCode.Add(new CodeEntity()
            {
                Code = $"{temp} = {node.Identifier} + 1"
            });
            generatedCode.Add(new CodeEntity()
            {
                Code = $"{node.Identifier} = {temp}"
            });
            generatedCode.Add(MakeLabeledGoto(beginLabel));
            generatedCode.Add(nextLabel);
            return(node);
        }
示例#3
0
        private ForStatementUpNode Visit(ForStatementUpNode node)
        {
            dynamic props = new ExpandoObject();

            properties.Add(node, props);
            var code = new List <CodeEntry>();

            props.code = code;
            var beginLabel = MakeLabel();
            var trueLabel  = MakeLabel();
            var nextLabel  = MakeLabel();
            var temp       = MakeTemp();

            code.AddRange(properties[node.expr1].code);
            code.Add(new CodeEntity()
            {
                Code = $"{node.Identifier} = {properties[node.expr1].addr}"
            });
            code.Add(beginLabel);
            code.AddRange(properties[node.expr2].code);
            code.Add(new CodeEntity()
            {
                Code = $"if {node.Identifier} < {properties[node.expr2].addr} goto <label:{trueLabel.LabelId}>"
            });
            code.Add(MakeLabeledGoto(nextLabel));
            code.Add(trueLabel);
            code.AddRange(properties[node.StatementNode].code);
            code.Add(new CodeEntity()
            {
                Code = $"{temp} = {node.Identifier} + 1"
            });
            code.Add(new CodeEntity()
            {
                Code = $"{node.Identifier} = {temp}"
            });
            code.Add(MakeLabeledGoto(beginLabel));
            code.Add(nextLabel);
            return(node);
        }