From 594488a5ac0edef9074ed8957c65137cee453e37 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 11 Jun 2023 23:01:47 +0100 Subject: Compile push-bool instruction to LLVM IR --- .../compiler/backends/llvm/LlvmCompiler.java | 23 +++++++++++++----- .../backends/llvm/LlvmCompilerContext.java | 28 +++++++++++++++++++++- 2 files changed, 44 insertions(+), 7 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/org/shedlang/compiler/backends/llvm/LlvmCompiler.java b/src/main/java/org/shedlang/compiler/backends/llvm/LlvmCompiler.java index 8921ad8d..be5dbf8c 100644 --- a/src/main/java/org/shedlang/compiler/backends/llvm/LlvmCompiler.java +++ b/src/main/java/org/shedlang/compiler/backends/llvm/LlvmCompiler.java @@ -1,37 +1,48 @@ package org.shedlang.compiler.backends.llvm; +import org.shedlang.compiler.backends.llvm.ir.ast.operands.LlvmOperandInt; import org.shedlang.compiler.stackir.instructions.*; import org.shedlang.compiler.types.NamespaceName; import java.util.List; public class LlvmCompiler { - public void compileNamespace(NamespaceName namespaceName, List instructions) { + public static void compileNamespace(NamespaceName namespaceName, List instructions) { } - public void compileInstruction(Instruction instruction) { - switch (instruction) { + public static LlvmCompilerContext compileInstruction(Instruction instruction, LlvmCompilerContext context) { + return switch (instruction) { case CallInstruction callInstruction -> { + throw new UnsupportedOperationException("TODO"); } case DefineFunctionInstruction defineFunctionInstruction -> { + throw new UnsupportedOperationException("TODO"); } case DiscardInstruction discardInstruction -> { + throw new UnsupportedOperationException("TODO"); } case LocalLoadInstruction localLoadInstruction -> { + throw new UnsupportedOperationException("TODO"); } case LocalStoreInstruction localStoreInstruction -> { + throw new UnsupportedOperationException("TODO"); } case ModuleExportInstruction moduleExportInstruction -> { + throw new UnsupportedOperationException("TODO"); } - case PushBoolInstruction pushBoolInstruction -> { - } + case PushBoolInstruction pushBool -> + context.push(new LlvmOperandInt(pushBool.value() ? 1 : 0)); + case PushIntInstruction pushIntInstruction -> { + throw new UnsupportedOperationException("TODO"); } case PushStringInstruction pushStringInstruction -> { + throw new UnsupportedOperationException("TODO"); } case PushUnitInstruction pushUnitInstruction -> { + throw new UnsupportedOperationException("TODO"); } - } + }; } } diff --git a/src/main/java/org/shedlang/compiler/backends/llvm/LlvmCompilerContext.java b/src/main/java/org/shedlang/compiler/backends/llvm/LlvmCompilerContext.java index 924b9091..6d917e49 100644 --- a/src/main/java/org/shedlang/compiler/backends/llvm/LlvmCompilerContext.java +++ b/src/main/java/org/shedlang/compiler/backends/llvm/LlvmCompilerContext.java @@ -1,4 +1,30 @@ package org.shedlang.compiler.backends.llvm; -public class LlvmCompilerContext { +import org.pcollections.Empty; +import org.pcollections.PVector; +import org.shedlang.compiler.backends.llvm.ir.ast.instructions.LlvmInstruction; +import org.shedlang.compiler.backends.llvm.ir.ast.operands.LlvmOperand; + +public record LlvmCompilerContext( + PVector stack, + PVector instructions +) { + public static LlvmCompilerContext EMPTY = new LlvmCompilerContext( + Empty.vector(), + Empty.vector() + ); + + public LlvmCompilerContext addInstruction(LlvmInstruction instruction) { + return new LlvmCompilerContext( + stack, + instructions.plus(instruction) + ); + } + + public LlvmCompilerContext push(LlvmOperand operand) { + return new LlvmCompilerContext( + stack.plus(operand), + instructions + ); + } } -- cgit v1.2.3