From 8331098d1904538a3a97d22e4d847cf2cc94e57d Mon Sep 17 00:00:00 2001 From: Cliff Click Date: Thu, 17 Sep 2026 15:04:32 -0700 Subject: x86 mul reg reg encoding fix --- chapter21/Makefile | 2 +- .../com/seaofnodes/simple/X86EncodingTest.java | 33 ---------------------- .../seaofnodes/simple/codegen/X86EncodingTest.java | 32 +++++++++++++++++++++ 3 files changed, 33 insertions(+), 34 deletions(-) delete mode 100644 chapter21/src/test/java/com/seaofnodes/simple/X86EncodingTest.java create mode 100644 chapter21/src/test/java/com/seaofnodes/simple/codegen/X86EncodingTest.java (limited to 'chapter21') diff --git a/chapter21/Makefile b/chapter21/Makefile index 055c632a..429e40df 100644 --- a/chapter21/Makefile +++ b/chapter21/Makefile @@ -37,7 +37,7 @@ main_javas := $(wildcard $(SRC)/$(SIMPLE)/*java $(SRC)/$(SIMPLE)/*/*java $(SRC test_javas := $(wildcard $(TST)/$(SIMPLE)/*java $(TST)/$(SIMPLE)/*/*java) main_classes := $(patsubst $(SRC)/%java,$(CLZDIR)/main/%class,$(main_javas)) test_classes := $(patsubst $(TST)/%java,$(CLZDIR)/test/%class,$(test_javas)) -test_cp := $(patsubst $(TST)/$(SIMPLE)/%.java,com.seaofnodes.simple.%,$(wildcard $(TST)/$(SIMPLE)/*Test.java)) +test_cp := $(patsubst $(TST)/$(SIMPLE)/%.java,com.seaofnodes.simple.%,$(wildcard $(TST)/$(SIMPLE)/*Test.java)) com.seaofnodes.simple.codegen.X86EncodingTest classes = $(main_classes) $(test_classes) # All the libraries libs = $(wildcard lib/*jar) diff --git a/chapter21/src/test/java/com/seaofnodes/simple/X86EncodingTest.java b/chapter21/src/test/java/com/seaofnodes/simple/X86EncodingTest.java deleted file mode 100644 index 54fa96a8..00000000 --- a/chapter21/src/test/java/com/seaofnodes/simple/X86EncodingTest.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.seaofnodes.simple; - -import com.seaofnodes.simple.codegen.CodeGen; -import com.seaofnodes.simple.node.cpus.x86_64_v2.CmpMemX86; -import org.junit.Test; -import static org.junit.Assert.*; - -public class X86EncodingTest { - @Test - public void testMemoryCompare64Immediate32() { - for (int immediate : new int[]{123456789, -123456789}) { - String src = "int[] !a = new int[2]; a[0] = arg; return a[arg & 1] == " + immediate + ";"; - CodeGen code = new CodeGen(src).driver(CodeGen.Phase.Encoding, "x86_64_v2", "SystemV"); - var cmp = code._stop.walk(n -> n instanceof CmpMemX86 ? n : null); - assertNotNull("Must exercise a memory comparison", cmp); - var enc = code._encoding; - int start = enc._opStart[cmp._nid]; - - // REX.W, 81 /7, SIB with scale 8, disp8, and a sign-extended imm32. - assertEquals(0x48, enc.read1(start) & 0xF8); - assertEquals(0x81, enc.read1(start + 1)); - assertEquals(0x7C, enc.read1(start + 2)); - assertEquals(0xC0, enc.read1(start + 3) & 0xC0); - assertEquals(8, enc.read1(start + 4)); - assertEquals(immediate, enc.read4(start + 5)); - assertEquals("CMP r/m64 uses four immediate bytes", 9, enc._opLen[cmp._nid]); - - // SETE must immediately follow the CPU's nine-byte CMP instruction. - assertEquals(0x0F, enc.read1(start + 9)); - assertEquals(0x94, enc.read1(start + 10)); - } - } -} diff --git a/chapter21/src/test/java/com/seaofnodes/simple/codegen/X86EncodingTest.java b/chapter21/src/test/java/com/seaofnodes/simple/codegen/X86EncodingTest.java new file mode 100644 index 00000000..03a57675 --- /dev/null +++ b/chapter21/src/test/java/com/seaofnodes/simple/codegen/X86EncodingTest.java @@ -0,0 +1,32 @@ +package com.seaofnodes.simple.codegen; + +import com.seaofnodes.simple.node.cpus.x86_64_v2.CmpMemX86; +import org.junit.Test; +import static org.junit.Assert.*; + +public class X86EncodingTest { + @Test + public void testMemoryCompare64Immediate32() { + for (int immediate : new int[]{123456789, -123456789}) { + String src = "int[] !a = new int[2]; a[0] = arg; return a[arg & 1] == " + immediate + ";"; + CodeGen code = new CodeGen(src).driver(CodeGen.Phase.Encoding, "x86_64_v2", "SystemV"); + var cmp = code._stop.walk(n -> n instanceof CmpMemX86 ? n : null); + assertNotNull("Must exercise a memory comparison", cmp); + var enc = code._encoding; + int start = enc._opStart[cmp._nid]; + + // REX.W, 81 /7, SIB with scale 8, disp8, and a sign-extended imm32. + assertEquals(0x48, enc.read1(start) & 0xF8); + assertEquals(0x81, enc.read1(start + 1)); + assertEquals(0x7C, enc.read1(start + 2)); + assertEquals(0xC0, enc.read1(start + 3) & 0xC0); + assertEquals(8, enc.read1(start + 4)); + assertEquals(immediate, enc.read4(start + 5)); + assertEquals("CMP r/m64 uses four immediate bytes", 9, enc._opLen[cmp._nid]); + + // SETE must immediately follow the CPU's nine-byte CMP instruction. + assertEquals(0x0F, enc.read1(start + 9)); + assertEquals(0x94, enc.read1(start + 10)); + } + } +} -- cgit v1.2.3