From bbd0ad7fad36761f55555347b01a32887fc4a5f9 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 7 Jul 2023 18:22:49 +0100 Subject: Add int-sub instruction --- src/main/java/org/shedlang/compiler/backends/llvm/LlvmCodeGen.java | 7 +++++++ src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGen.java | 3 +++ .../backends/wasm/structure/ast/instructions/WasmI32Sub.java | 4 ++++ .../backends/wasm/structure/ast/instructions/WasmInstruction.java | 2 +- .../wasm/structure/binary/WasmBinaryFormatInstructions.java | 3 +++ 5 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmI32Sub.java (limited to 'src/main/java/org/shedlang/compiler/backends') 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 == -- cgit v1.2.3