Пример #1
0
        public override AstNode Visit(IfStatement node)
        {
            // Begin the node.
            builder.BeginNode(node);

            // Get the condition, then and else.
            Expression condition = node.GetCondition();
            AstNode thenNode = node.GetThen();
            AstNode elseNode = node.GetElse();

            IChelaType condType = condition.GetNodeType();

            // Visit the condition.
            condition.Accept(this);
            if(condType != ChelaType.GetBoolType()) // Read variables values.
                Cast(node, condition.GetNodeValue(), condType, ChelaType.GetBoolType());

            // Create the basic blocks.
            BasicBlock thenBlock = CreateBasicBlock();
            BasicBlock elseBlock = CreateBasicBlock();
            thenBlock.SetName("then");
            if(elseNode != null)
                elseBlock.SetName("else");
            else
                elseBlock.SetName("merge");

            // Perform the branch.
            builder.CreateBr(thenBlock, elseBlock);
            builder.SetBlock(thenBlock);

            // Visit the then statement.
            thenNode.Accept(this);

            // Update the then block.
            thenBlock = builder.GetBlock();

            // Write the else block.
            if(elseNode != null)
            {
                // Set the current block.
                builder.SetBlock(elseBlock);

                // Visit the else statement.
                elseNode.Accept(this);

                // Update the else block.
                elseBlock = builder.GetBlock();
            }

            if(thenBlock.IsLastTerminator() && elseBlock.IsLastTerminator())
                return builder.EndNode();

            // Get or create the merge block.
            BasicBlock mergeBlock = null;
            if(elseNode != null)
            {
                mergeBlock = CreateBasicBlock();
                mergeBlock.SetName("merge");
            }
            else
            {
                mergeBlock = elseBlock;
            }

            // Write the block terminators.
            if(!thenBlock.IsLastTerminator())
            {
                builder.SetBlock(thenBlock);
                builder.CreateJmp(mergeBlock);
            }

            if(elseNode != null && !elseBlock.IsLastTerminator())
            {
                builder.SetBlock(elseBlock);
                builder.CreateJmp(mergeBlock);
            }

            // Continue.
            builder.SetBlock(mergeBlock);

            return builder.EndNode();
        }
Пример #2
0
        public override AstNode Visit(IfStatement node)
        {
            // Visit the then node.
            AstNode thenNode = node.GetThen();
            thenNode.Accept(this);

            // Visit the else node.
            AstNode elseNode = node.GetElse();
            if(elseNode != null)
                elseNode.Accept(this);

            return node;
        }
Пример #3
0
        public override AstNode Visit(IfStatement node)
        {
            // Get the condition, then and else.
            Expression cond = node.GetCondition();
            AstNode thenNode = node.GetThen();
            AstNode elseNode = node.GetElse();

            // Visit the condition, then and else.
            cond.Accept(this);
            thenNode.Accept(this);
            if(elseNode != null)
                elseNode.Accept(this);

            // Check the condition type.
            IChelaType condType = cond.GetNodeType();
            if(condType != ChelaType.GetBoolType() &&
               Coerce(condType, ChelaType.GetBoolType()) != ChelaType.GetBoolType())
                Error(node, "condition must be a boolean expression.");

            return node;
        }