summaryrefslogtreecommitdiff
path: root/chapter20/src
diff options
context:
space:
mode:
Diffstat (limited to 'chapter20/src')
-rw-r--r--chapter20/src/main/java/com/seaofnodes/simple/node/cpus/x86_64_v2/x86_64_v2.java2
-rw-r--r--chapter20/src/test/java/com/seaofnodes/simple/X86StoreTest.java35
2 files changed, 36 insertions, 1 deletions
diff --git a/chapter20/src/main/java/com/seaofnodes/simple/node/cpus/x86_64_v2/x86_64_v2.java b/chapter20/src/main/java/com/seaofnodes/simple/node/cpus/x86_64_v2/x86_64_v2.java
index 06e3e578..2b813379 100644
--- a/chapter20/src/main/java/com/seaofnodes/simple/node/cpus/x86_64_v2/x86_64_v2.java
+++ b/chapter20/src/main/java/com/seaofnodes/simple/node/cpus/x86_64_v2/x86_64_v2.java
@@ -488,7 +488,7 @@ public class x86_64_v2 extends Machine {
private int imm( Node xval ) {
assert val==null && imm==0;
- if( xval instanceof ConstantNode con && con._con instanceof TypeInteger ti ) {
+ if( xval instanceof ConstantNode con && con._con instanceof TypeInteger ti && ti.isConstant() && (int)ti.value()==ti.value() ) {
val = null;
imm = (int)ti.value();
assert imm == ti.value(); // In 32-bit range
diff --git a/chapter20/src/test/java/com/seaofnodes/simple/X86StoreTest.java b/chapter20/src/test/java/com/seaofnodes/simple/X86StoreTest.java
new file mode 100644
index 00000000..c853082f
--- /dev/null
+++ b/chapter20/src/test/java/com/seaofnodes/simple/X86StoreTest.java
@@ -0,0 +1,35 @@
+package com.seaofnodes.simple;
+
+import com.seaofnodes.simple.codegen.CodeGen;
+import com.seaofnodes.simple.node.cpus.x86_64_v2.StoreX86;
+import com.seaofnodes.simple.type.TypeInteger;
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+public class X86StoreTest {
+ @Test public void testConstantStores() {
+ check("u8",255,false);
+ check("u16",65535,false);
+ check("u32",Integer.MAX_VALUE,false);
+ check("u32",2147483648L,true);
+ check("u32",4294967295L,true);
+ check("int",Integer.MIN_VALUE,false);
+ check("int",Integer.MAX_VALUE,false);
+ for( long value : new long[]{2147483648L,4294967295L,-2147483649L,
+ 0x123456789abcdefL,Long.MIN_VALUE,Long.MAX_VALUE} )
+ check("int",value,true);
+ }
+
+ private static void check(String type, long value, boolean register) {
+ // Returning the object keeps the Store; returning its field could fold.
+ String literal = value==Long.MIN_VALUE ? "(-9223372036854775807-1)" : Long.toString(value);
+ String src = "struct S { "+type+" x="+literal+"; }; return new S;";
+ CodeGen code = new CodeGen(src).driver(CodeGen.Phase.InstSelect,"x86_64_v2","SystemV");
+ StoreX86 store = code._stop.walk(n -> n instanceof StoreX86 st && st._name.equals("x") ? st : null);
+ assertNotNull(src,store);
+ if( register ) {
+ assertNotNull(src,store.in(4));
+ assertEquals(src,value,((TypeInteger)store.in(4)._type).value());
+ } else assertNull(src,store.in(4));
+ }
+}