From 69ce0b822262c9ea5ae9cc4e89be0fa4498eaf25 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 7 Jul 2023 18:01:28 +0100 Subject: Add int-ne instruction --- .../compiler/backends/llvm/LlvmCodeGen.java | 75 ++++++++++++---------- .../backends/llvm/ir/LlvmIrSerializer.java | 1 + .../ir/ast/instructions/LlvmConditionCode.java | 1 + .../compiler/backends/wasm/WasmCodeGen.java | 5 +- .../wasm/structure/ast/instructions/WasmI32Ne.java | 4 ++ .../ast/instructions/WasmInstruction.java | 2 +- .../binary/WasmBinaryFormatInstructions.java | 3 + .../compiler/stackir/InstructionSerializer.java | 5 +- .../compiler/stackir/instructions/Instruction.java | 2 +- .../instructions/IntNotEqualInstruction.java | 10 +++ 10 files changed, 71 insertions(+), 37 deletions(-) create mode 100644 src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmI32Ne.java create mode 100644 src/main/java/org/shedlang/compiler/stackir/instructions/IntNotEqualInstruction.java (limited to 'src/main/java') 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 8b2520e7..389067b9 100644 --- a/src/main/java/org/shedlang/compiler/backends/llvm/LlvmCodeGen.java +++ b/src/main/java/org/shedlang/compiler/backends/llvm/LlvmCodeGen.java @@ -156,39 +156,11 @@ public class LlvmCodeGen { yield context.push(fieldValue); } - case IntEqualsInstruction intEquals -> { - var rightResult = context.pop(); - var right = rightResult.value(); - context = rightResult.context(); - - var leftResult = context.pop(); - var left = leftResult.value(); - context = leftResult.context(); - - var targetI1Result = context.generateVariableLocal(LlvmCodeGenLocalVariableSpace.TEMPORARY); - var targetI1 = targetI1Result.value(); - context = targetI1Result.context(); - - var targetResult = context.generateVariableLocal(LlvmCodeGenLocalVariableSpace.TEMPORARY); - var target = targetResult.value(); - context = targetResult.context(); - - yield context - .addInstruction(new LlvmIcmp( - targetI1, - LlvmConditionCode.EQ, - LlvmCodeGenTypes.INT_TYPE, - left, - right - )) - .addInstruction(new LlvmZext( - target, - LlvmTypes.INT_1, - targetI1, - LlvmCodeGenTypes.VALUE_TYPE - )) - .push(target); - } + case IntEqualsInstruction intEquals -> + generateBinaryComparison(LlvmConditionCode.EQ, context); + + case IntNotEqualInstruction intNotEqual -> + generateBinaryComparison(LlvmConditionCode.NE, context); case LocalLoadInstruction localLoad -> context.push(context.shedLocal(localLoad.name())); @@ -253,4 +225,41 @@ public class LlvmCodeGen { } }; } + + private static LlvmCodeGenContext generateBinaryComparison( + LlvmConditionCode conditionCode, + LlvmCodeGenContext context + ) { + var rightResult = context.pop(); + var right = rightResult.value(); + context = rightResult.context(); + + var leftResult = context.pop(); + var left = leftResult.value(); + context = leftResult.context(); + + var targetI1Result = context.generateVariableLocal(LlvmCodeGenLocalVariableSpace.TEMPORARY); + var targetI1 = targetI1Result.value(); + context = targetI1Result.context(); + + var targetResult = context.generateVariableLocal(LlvmCodeGenLocalVariableSpace.TEMPORARY); + var target = targetResult.value(); + context = targetResult.context(); + + return context + .addInstruction(new LlvmIcmp( + targetI1, + conditionCode, + LlvmCodeGenTypes.INT_TYPE, + left, + right + )) + .addInstruction(new LlvmZext( + target, + LlvmTypes.INT_1, + targetI1, + LlvmCodeGenTypes.VALUE_TYPE + )) + .push(target); + } } diff --git a/src/main/java/org/shedlang/compiler/backends/llvm/ir/LlvmIrSerializer.java b/src/main/java/org/shedlang/compiler/backends/llvm/ir/LlvmIrSerializer.java index cad69bc0..9985d244 100644 --- a/src/main/java/org/shedlang/compiler/backends/llvm/ir/LlvmIrSerializer.java +++ b/src/main/java/org/shedlang/compiler/backends/llvm/ir/LlvmIrSerializer.java @@ -36,6 +36,7 @@ public class LlvmIrSerializer { ) throws IOException { writer.append(switch (conditionCode) { case EQ -> "eq"; + case NE -> "ne"; }); } diff --git a/src/main/java/org/shedlang/compiler/backends/llvm/ir/ast/instructions/LlvmConditionCode.java b/src/main/java/org/shedlang/compiler/backends/llvm/ir/ast/instructions/LlvmConditionCode.java index 4dcc331e..3566926b 100644 --- a/src/main/java/org/shedlang/compiler/backends/llvm/ir/ast/instructions/LlvmConditionCode.java +++ b/src/main/java/org/shedlang/compiler/backends/llvm/ir/ast/instructions/LlvmConditionCode.java @@ -2,4 +2,5 @@ package org.shedlang.compiler.backends.llvm.ir.ast.instructions; public enum LlvmConditionCode { EQ, + NE, } 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 77887f94..d3d99bbc 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGen.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGen.java @@ -179,9 +179,12 @@ public class WasmCodeGen { ); } - case IntEqualsInstruction intEqualsInstruction -> + case IntEqualsInstruction intEquals -> context.addInstruction(new WasmI32Eq()); + case IntNotEqualInstruction intNotEqual -> + context.addInstruction(new WasmI32Ne()); + 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/WasmI32Ne.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmI32Ne.java new file mode 100644 index 00000000..a76bddd3 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmI32Ne.java @@ -0,0 +1,4 @@ +package org.shedlang.compiler.backends.wasm.structure.ast.instructions; + +public record WasmI32Ne() 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 2e4b166c..2286536a 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, WasmI32Const, WasmI32Eq, WasmI32Load, WasmI32Store, WasmI64Const, WasmIf, WasmLocalGet, WasmLocalSet, WasmMemoryAddressConst, WasmMemoryGrow, WasmReturn, WasmTableElementIndexConst { +public sealed interface WasmInstruction permits WasmCall, WasmCallIndirect, WasmDrop, WasmGlobalGet, WasmGlobalSet, WasmI32Const, WasmI32Eq, WasmI32Load, WasmI32Ne, WasmI32Store, 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 549c5004..ea162fbd 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 @@ -107,6 +107,9 @@ public class WasmBinaryFormatInstructions { case WasmI32Eq i32Eq -> { writer.writeByte(0x46); } + case WasmI32Ne i32Ne -> { + writer.writeByte(0x47); + } // == 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 ac58103c..00757988 100644 --- a/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java +++ b/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java @@ -83,7 +83,10 @@ public class InstructionSerializer { ); case IntEqualsInstruction intEquals -> - treeList(treeIdentifier("int-equals")); + treeList(treeIdentifier("int-eq")); + + case IntNotEqualInstruction intNotEqual -> + treeList(treeIdentifier("int-ne")); 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 f3c8a30a..5d4bd139 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, IntEqualsInstruction, LocalLoadInstruction, LocalStoreInstruction, NamespaceFieldStoreInstruction, NamespaceImportInstruction, PushBoolInstruction, PushIntInstruction, PushStringInstruction, PushUnitInstruction, ReturnInstruction { + permits CallInstruction, DefineFunctionInstruction, DiscardInstruction, FieldLoadInstruction, IntEqualsInstruction, IntNotEqualInstruction, LocalLoadInstruction, LocalStoreInstruction, NamespaceFieldStoreInstruction, NamespaceImportInstruction, PushBoolInstruction, PushIntInstruction, PushStringInstruction, PushUnitInstruction, ReturnInstruction { Source source(); } diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/IntNotEqualInstruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/IntNotEqualInstruction.java new file mode 100644 index 00000000..722723d2 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/stackir/instructions/IntNotEqualInstruction.java @@ -0,0 +1,10 @@ +package org.shedlang.compiler.stackir.instructions; + +import org.shedlang.compiler.sources.Source; + +/** + * Pop two values from the stack, compare them as Ints, and push a Bool onto + * the stack: true if not equal, false if equal. + */ +public record IntNotEqualInstruction(Source source) implements Instruction { +} -- cgit v1.2.3