diff options
| author | Cliff Click <cliffc@acm.org> | 2025-04-12 11:41:58 -0700 |
|---|---|---|
| committer | Cliff Click <cliffc@acm.org> | 2025-04-12 11:41:58 -0700 |
| commit | d08925c1ff212e2157356eacbd748e679b80954d (patch) | |
| tree | 5761010cf6fba6101734a3bb6f806fdf3841210b | |
| parent | 798bd959410eb6c2e0b9f7ae1222181ac2b295d4 (diff) | |
| download | Simple-d08925c1ff212e2157356eacbd748e679b80954d.tar.gz Simple-d08925c1ff212e2157356eacbd748e679b80954d.zip | |
2 more encoding bugs
Missing constant NIL, arm CAST
7 files changed, 46 insertions, 7 deletions
diff --git a/chapter21/src/main/java/com/seaofnodes/simple/node/cpus/arm/CastARM.java b/chapter21/src/main/java/com/seaofnodes/simple/node/cpus/arm/CastARM.java new file mode 100644 index 00000000..a5a4e112 --- /dev/null +++ b/chapter21/src/main/java/com/seaofnodes/simple/node/cpus/arm/CastARM.java @@ -0,0 +1,18 @@ +package com.seaofnodes.simple.node.cpus.arm; + +import com.seaofnodes.simple.SB; +import com.seaofnodes.simple.codegen.CodeGen; +import com.seaofnodes.simple.codegen.Encoding; +import com.seaofnodes.simple.codegen.RegMask; +import com.seaofnodes.simple.node.CastNode; +import com.seaofnodes.simple.node.MachNode; + +public class CastARM extends CastNode implements MachNode { + public CastARM( CastNode cast ) { super(cast); } + @Override public String op() { return label(); } + @Override public RegMask regmap(int i) { assert i==1; return RegMask.FULL; } + @Override public RegMask outregmap() { return RegMask.FULL; } + @Override public int twoAddress( ) { return 1; } + @Override public void encoding( Encoding enc ) { } + @Override public void asm(CodeGen code, SB sb) { _t.print(sb.p(code.reg(in(1))).p(" isa ")); } +} diff --git a/chapter21/src/main/java/com/seaofnodes/simple/node/cpus/arm/IntARM.java b/chapter21/src/main/java/com/seaofnodes/simple/node/cpus/arm/IntARM.java index 8affb4c6..0a07b07a 100644 --- a/chapter21/src/main/java/com/seaofnodes/simple/node/cpus/arm/IntARM.java +++ b/chapter21/src/main/java/com/seaofnodes/simple/node/cpus/arm/IntARM.java @@ -4,6 +4,7 @@ import com.seaofnodes.simple.*; import com.seaofnodes.simple.codegen.*; import com.seaofnodes.simple.node.ConstantNode; import com.seaofnodes.simple.node.MachNode; +import com.seaofnodes.simple.type.Type; import com.seaofnodes.simple.type.TypeInteger; // Integer constants @@ -19,7 +20,7 @@ public class IntARM extends ConstantNode implements MachNode { @Override public void encoding( Encoding enc ) { short self = enc.reg(this); - long x = ((TypeInteger)_con).value(); + long x = _con==Type.NIL ? 0 : ((TypeInteger)_con).value(); int nb0 = 0; int nb1 = 0; // Count number of 0000 and FFFF blocks diff --git a/chapter21/src/main/java/com/seaofnodes/simple/node/cpus/arm/arm.java b/chapter21/src/main/java/com/seaofnodes/simple/node/cpus/arm/arm.java index 0aea0ae9..bedda4e2 100644 --- a/chapter21/src/main/java/com/seaofnodes/simple/node/cpus/arm/arm.java +++ b/chapter21/src/main/java/com/seaofnodes/simple/node/cpus/arm/arm.java @@ -620,7 +620,7 @@ public class arm extends Machine { case AndNode and -> and(and); case BoolNode bool -> cmp(bool); case CallNode call -> call(call); - case CastNode cast -> new CastNode(cast); + case CastNode cast -> new CastARM(cast); case CallEndNode cend -> new CallEndARM(cend); case CProjNode c -> new CProjNode(c); case ConstantNode con -> con(con); @@ -696,7 +696,7 @@ public class arm extends Machine { : new SubARM(sub); } - private Node con(ConstantNode con) { + private Node con( ConstantNode con ) { if( !con._con.isConstant() ) return new ConstantNode( con ); // Default unknown caller inputs return switch( con._con ) { case TypeInteger ti -> new IntARM(con); @@ -705,7 +705,7 @@ public class arm extends Machine { case TypeMemPtr tmp -> new ConstantNode(con); case TypeNil tn -> throw Utils.TODO(); // TOP, BOTTOM, XCtrl, Ctrl, etc. Never any executable code. - case Type t -> new ConstantNode(con); + case Type t -> t==Type.NIL ? new IntARM(con) : new ConstantNode(con); }; } diff --git a/chapter21/src/main/java/com/seaofnodes/simple/node/cpus/riscv/IntRISC.java b/chapter21/src/main/java/com/seaofnodes/simple/node/cpus/riscv/IntRISC.java index 764c49e6..70d5c18f 100644 --- a/chapter21/src/main/java/com/seaofnodes/simple/node/cpus/riscv/IntRISC.java +++ b/chapter21/src/main/java/com/seaofnodes/simple/node/cpus/riscv/IntRISC.java @@ -4,6 +4,7 @@ import com.seaofnodes.simple.*; import com.seaofnodes.simple.codegen.*; import com.seaofnodes.simple.node.ConstantNode; import com.seaofnodes.simple.node.MachNode; +import com.seaofnodes.simple.type.Type; import com.seaofnodes.simple.type.TypeInteger; // 12-bit integer constant. Larger constants are made up in the instruction @@ -17,10 +18,10 @@ public class IntRISC extends ConstantNode implements MachNode { @Override public IntRISC copy() { return new IntRISC(this); } @Override public void encoding( Encoding enc ) { short dst = enc.reg(this); - TypeInteger ti = (TypeInteger)_con; + int val = _con==Type.NIL ? 0 : (int)(((TypeInteger)_con).value() & 0xFFF); // Explicit truncation of larger immediates; this will sign-extend on // load and this is handled during instruction selection. - enc.add4(riscv.i_type(riscv.OP_IMM, dst, 0, riscv.ZERO, (int)(ti.value() & 0xFFF))); + enc.add4(riscv.i_type(riscv.OP_IMM, dst, 0, riscv.ZERO, val)); } @Override public void asm(CodeGen code, SB sb) { String reg = code.reg(this); diff --git a/chapter21/src/main/java/com/seaofnodes/simple/node/cpus/x86_64_v2/x86_64_v2.java b/chapter21/src/main/java/com/seaofnodes/simple/node/cpus/x86_64_v2/x86_64_v2.java index b4c382c5..12b3a12c 100644 --- a/chapter21/src/main/java/com/seaofnodes/simple/node/cpus/x86_64_v2/x86_64_v2.java +++ b/chapter21/src/main/java/com/seaofnodes/simple/node/cpus/x86_64_v2/x86_64_v2.java @@ -573,7 +573,7 @@ public class x86_64_v2 extends Machine { return new CmpX86(bool); } - private Node con(ConstantNode con) { + private Node con( ConstantNode con ) { if(!con._con.isConstant()) return new ConstantNode(con); // Default unknown caller inputs return switch (con._con) { case TypeInteger ti -> new IntX86(con); diff --git a/chapter21/src/test/java/com/seaofnodes/simple/Chapter21Test.java b/chapter21/src/test/java/com/seaofnodes/simple/Chapter21Test.java index 0610d294..e584f9bc 100644 --- a/chapter21/src/test/java/com/seaofnodes/simple/Chapter21Test.java +++ b/chapter21/src/test/java/com/seaofnodes/simple/Chapter21Test.java @@ -64,6 +64,14 @@ public class Chapter21Test { testCPU(src,"arm" , "SystemV",19,null); } + @Test + public void testAntiDeps1() throws IOException { + String src = Files.readString(Path.of("src/test/java/com/seaofnodes/simple/progs/antiDep1.smp")); + testCPU(src,"x86_64_v2", "SystemV", 3,"return mov(mov(S));"); + testCPU(src,"riscv" , "SystemV",10,"return mov(mov(S));"); + testCPU(src,"arm" , "SystemV",10,"return mov(mov(S));"); + } + @Test public void testNewtonExport() throws IOException { String result = """ 0 0.000000 diff --git a/chapter21/src/test/java/com/seaofnodes/simple/progs/antiDep1.smp b/chapter21/src/test/java/com/seaofnodes/simple/progs/antiDep1.smp new file mode 100644 index 00000000..b18dfd16 --- /dev/null +++ b/chapter21/src/test/java/com/seaofnodes/simple/progs/antiDep1.smp @@ -0,0 +1,11 @@ +// -*- mode: java; -*- +struct S { int f; }; +var v0 = new S; +S? v1; +if (arg) v1 = new S; +if (v1) { + v0.f = v1.f; +} else { + v0.f = 2; +} +return v0; |
