diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-07-07 18:22:49 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-07-07 18:22:49 +0100 |
| commit | bbd0ad7fad36761f55555347b01a32887fc4a5f9 (patch) | |
| tree | 4f91676bba01b3aa85e9d8bf87b140f4ba3f8722 /src/main/java | |
| parent | 4123de472e0442cccd8fd01767e399dc05cc6b11 (diff) | |
Add int-sub instruction
Diffstat (limited to 'src/main/java')
8 files changed, 33 insertions, 2 deletions
diff --git a/src/main/java/org/shedlang/compiler/backends/llvm/LlvmCodeGen.java b/src/main/java/org/shedlang/compiler/backends/llvm/LlvmCodeGen.java index e7132d56..3062d48d 100644 --- a/src/main/java/org/shedlang/compiler/backends/llvm/LlvmCodeGen.java +++ b/src/main/java/org/shedlang/compiler/backends/llvm/LlvmCodeGen.java @@ -167,6 +167,13 @@ public class LlvmCodeGen { case IntNotEqualInstruction intNotEqual -> generateBinaryComparison(LlvmConditionCode.NE, context); + case IntSubInstruction intSub -> + generateBinaryOperation( + (target, left, right) -> + new LlvmSub(target, LlvmCodeGenTypes.INT_TYPE, left, right), + context + ); + case LocalLoadInstruction localLoad -> context.push(context.shedLocal(localLoad.name())); diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGen.java b/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGen.java index 3c57853f..b3a6a4ba 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGen.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGen.java @@ -188,6 +188,9 @@ public class WasmCodeGen { case IntNotEqualInstruction intNotEqual -> context.addInstruction(new WasmI32Ne()); + case IntSubInstruction intSub -> + context.addInstruction(new WasmI32Sub()); + case LocalLoadInstruction localLoad -> context.addInstruction(new WasmLocalGet(context.shedLocal(localLoad.name()))); diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmI32Sub.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmI32Sub.java new file mode 100644 index 00000000..757cdaa2 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmI32Sub.java @@ -0,0 +1,4 @@ +package org.shedlang.compiler.backends.wasm.structure.ast.instructions; + +public record WasmI32Sub() implements WasmInstruction { +} diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmInstruction.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmInstruction.java index 2e2be69f..1c3a1c9f 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmInstruction.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmInstruction.java @@ -1,4 +1,4 @@ package org.shedlang.compiler.backends.wasm.structure.ast.instructions; -public sealed interface WasmInstruction permits WasmCall, WasmCallIndirect, WasmDrop, WasmGlobalGet, WasmGlobalSet, WasmI32Add, WasmI32Const, WasmI32Eq, WasmI32Load, WasmI32Ne, WasmI32Store, WasmI64Const, WasmIf, WasmLocalGet, WasmLocalSet, WasmMemoryAddressConst, WasmMemoryGrow, WasmReturn, WasmTableElementIndexConst { +public sealed interface WasmInstruction permits WasmCall, WasmCallIndirect, WasmDrop, WasmGlobalGet, WasmGlobalSet, WasmI32Add, WasmI32Const, WasmI32Eq, WasmI32Load, WasmI32Ne, WasmI32Store, WasmI32Sub, WasmI64Const, WasmIf, WasmLocalGet, WasmLocalSet, WasmMemoryAddressConst, WasmMemoryGrow, WasmReturn, WasmTableElementIndexConst { } diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatInstructions.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatInstructions.java index 9bfdd2ae..00766dbd 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatInstructions.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatInstructions.java @@ -113,6 +113,9 @@ public class WasmBinaryFormatInstructions { case WasmI32Add i32Add -> { writer.writeByte(0x6a); } + case WasmI32Sub i32Sub -> { + writer.writeByte(0x6b); + } // == Relocatable Instructions == diff --git a/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java b/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java index e98c4e02..c6544550 100644 --- a/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java +++ b/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java @@ -90,6 +90,9 @@ public class InstructionSerializer { case IntNotEqualInstruction intNotEqual -> treeList(treeIdentifier("int-ne")); + + case IntSubInstruction intSub -> + treeList(treeIdentifier("int-sub")); case LocalLoadInstruction localLoadInstruction -> treeList( diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/Instruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/Instruction.java index 6be42213..6d361ddb 100644 --- a/src/main/java/org/shedlang/compiler/stackir/instructions/Instruction.java +++ b/src/main/java/org/shedlang/compiler/stackir/instructions/Instruction.java @@ -3,7 +3,7 @@ package org.shedlang.compiler.stackir.instructions; import org.shedlang.compiler.sources.Source; public sealed interface Instruction - permits CallInstruction, DefineFunctionInstruction, DiscardInstruction, FieldLoadInstruction, IntAddInstruction, IntEqualsInstruction, IntNotEqualInstruction, LocalLoadInstruction, LocalStoreInstruction, NamespaceFieldStoreInstruction, NamespaceImportInstruction, PushBoolInstruction, PushIntInstruction, PushStringInstruction, PushUnitInstruction, ReturnInstruction { + permits CallInstruction, DefineFunctionInstruction, DiscardInstruction, FieldLoadInstruction, IntAddInstruction, IntEqualsInstruction, IntNotEqualInstruction, IntSubInstruction, LocalLoadInstruction, LocalStoreInstruction, NamespaceFieldStoreInstruction, NamespaceImportInstruction, PushBoolInstruction, PushIntInstruction, PushStringInstruction, PushUnitInstruction, ReturnInstruction { Source source(); } diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/IntSubInstruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/IntSubInstruction.java new file mode 100644 index 00000000..4d105695 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/stackir/instructions/IntSubInstruction.java @@ -0,0 +1,11 @@ +package org.shedlang.compiler.stackir.instructions; + +import org.shedlang.compiler.sources.Source; + +/** + * Pop the right operand from the stack, then the left operand from the stack, + * calculate the left operand minus the right operand, then push the result onto + * the stack. + */ +public record IntSubInstruction(Source source) implements Instruction { +} |
