summaryrefslogtreecommitdiff
path: root/chapter24b/src/test/java/com/seaofnodes
diff options
context:
space:
mode:
Diffstat (limited to 'chapter24b/src/test/java/com/seaofnodes')
-rw-r--r--chapter24b/src/test/java/com/seaofnodes/simple/X86StoreNativeTest.java50
-rw-r--r--chapter24b/src/test/java/com/seaofnodes/simple/X86StoreTest.java35
2 files changed, 85 insertions, 0 deletions
diff --git a/chapter24b/src/test/java/com/seaofnodes/simple/X86StoreNativeTest.java b/chapter24b/src/test/java/com/seaofnodes/simple/X86StoreNativeTest.java
new file mode 100644
index 00000000..f0c9d37f
--- /dev/null
+++ b/chapter24b/src/test/java/com/seaofnodes/simple/X86StoreNativeTest.java
@@ -0,0 +1,50 @@
+package com.seaofnodes.simple;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import org.junit.Test;
+
+public class X86StoreNativeTest {
+ @Test public void testConstantStoresAndAdds() throws IOException {
+ Path dir = Path.of("build/objs");
+ Files.createDirectories(dir);
+ Files.writeString(dir.resolve("x86ConstantStore.smp"), """
+ struct Narrow { u32 x; u32 guard; };
+ struct Wide { int x; int guard; };
+ val storeNarrow = { !Narrow s -> s.x=4294967295; return 0; };
+ val storePositive = { !Wide s -> s.x=81985529216486895; return 0; };
+ val storeNegative = { !Wide s -> s.x=-2147483649; return 0; };
+ val addPositive = { !Wide s -> s.x+=4294967295; return 0; };
+ val addNegative = { !Wide s -> s.x+=-2147483649; return 0; };
+ """);
+ // Read from C so load forwarding cannot hide an incorrectly encoded Store.
+ Files.writeString(dir.resolve("x86ConstantStore.c"), """
+ #include <stdint.h>
+ struct Narrow { uint32_t x, guard; };
+ struct Wide { int64_t x, guard; };
+ extern long long storeNarrow(struct Narrow *);
+ extern long long storePositive(struct Wide *);
+ extern long long storeNegative(struct Wide *);
+ extern long long addPositive(struct Wide *);
+ extern long long addNegative(struct Wide *);
+ int main(void) {
+ struct Narrow n = {0, 0x12345678};
+ struct Wide w = {0, 0x123456789abcdefLL};
+ storeNarrow(&n);
+ if (n.x != UINT32_MAX || n.guard != 0x12345678) return 1;
+ storePositive(&w);
+ if (w.x != 81985529216486895LL || w.guard != 0x123456789abcdefLL) return 2;
+ storeNegative(&w);
+ if (w.x != -2147483649LL || w.guard != 0x123456789abcdefLL) return 3;
+ w.x = 7;
+ addPositive(&w);
+ if (w.x != 4294967302LL || w.guard != 0x123456789abcdefLL) return 4;
+ addNegative(&w);
+ if (w.x != 2147483653LL || w.guard != 0x123456789abcdefLL) return 5;
+ return 0;
+ }
+ """);
+ TestC.run("build/objs","x86ConstantStore","",-1);
+ }
+}
diff --git a/chapter24b/src/test/java/com/seaofnodes/simple/X86StoreTest.java b/chapter24b/src/test/java/com/seaofnodes/simple/X86StoreTest.java
new file mode 100644
index 00000000..c07ac904
--- /dev/null
+++ b/chapter24b/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.Select,"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));
+ }
+}