public Construct VisitSection(Section node) { // Ensure function buffer is not null. if (this.function == null) { throw new Exception("Expected function buffer to be set"); } // Create the block. LlvmBlock block = this.function.AppendBlock(node.Identifier); // Set builder. this.builder = block.Builder; // Process the section's instructions. foreach (Instruction inst in node.Instructions) { // Visit the instruction. this.Visit(inst); // Pop the resulting LLVM instruction off the stack. this.valueStack.Pop(); } // Append the block onto the stack. this.blockStack.Push(block); // Return the node. return(node); }
public Construct VisitIf(If node) { // TODO: Action and alternative blocks not being handled, for debugging purposes. // Visit the condition. this.Visit(node.Condition); // Pop the condition off the stack. LlvmValue conditionValue = this.valueStack.Pop(); // Create a zero-value double for the boolean comparison. LlvmValue zero = LlvmFactory.Double(0); // TODO: Hard-coded name. // Build the comparison, condition will be convered to a boolean for a 'ONE' (non-equal) comparison. LlvmValue comparison = LLVM.BuildFCmp(this.builder.Unwrap(), LLVMRealPredicate.LLVMRealONE, conditionValue.Unwrap(), zero.Unwrap(), "ifcond").Wrap(); // Retrieve the parent function from the builder. LlvmFunction function = this.builder.Block.Parent; // Create the action block. LlvmBlock action = function.AppendBlock("then"); // TODO: Debugging, Ret void for action. action.Builder.CreateReturnVoid(); LlvmBlock otherwise = function.AppendBlock("else"); // TODO: Debugging, ret void for otherwise. otherwise.Builder.CreateReturnVoid(); LlvmBlock merge = function.AppendBlock("ifcont"); // TODO: Debugging, ret void for merge. merge.Builder.CreateReturnVoid(); // Build the if construct. LlvmValue @if = LLVM.BuildCondBr(this.builder.Unwrap(), comparison.Unwrap(), action.Unwrap(), otherwise.Unwrap()).Wrap(); // TODO: Complete implementation, based off: https://github.com/microsoft/LLVMSharp/blob/master/KaleidoscopeTutorial/Chapter5/KaleidoscopeLLVM/CodeGenVisitor.cs#L214 // ... // TODO: Debugging, not complete. action.Builder.PositionAtEnd(); // ? Delete.. // Append the if construct onto the stack. this.valueStack.Push(@if); // Return the node. return(node); }