diff options
| author | Cliff Click <cliffc@acm.org> | 2025-04-17 09:42:52 -0700 |
|---|---|---|
| committer | Cliff Click <cliffc@acm.org> | 2025-04-17 09:42:52 -0700 |
| commit | 38fd3c17c672c42fc768b0f31680b5d9679cac6d (patch) | |
| tree | ff48dfc8941a227cf2b968595f5101e5467972e8 /chapter21/src/main | |
| parent | bd0f7963b117d3760cd0206ddfa54573c3e9b149 (diff) | |
| download | Simple-38fd3c17c672c42fc768b0f31680b5d9679cac6d.tar.gz Simple-38fd3c17c672c42fc768b0f31680b5d9679cac6d.zip | |
Fix CmpIX86/Jmp swap args bug
Some x86 cmp need to swap args, then Set/Jmp both need to swap `bop`.
Fix missing padding in ASM printer.
MergeSort test like brainfuck - runs on arm.
Diffstat (limited to 'chapter21/src/main')
4 files changed, 34 insertions, 32 deletions
diff --git a/chapter21/src/main/java/com/seaofnodes/simple/node/cpus/x86_64_v2/CmpIX86.java b/chapter21/src/main/java/com/seaofnodes/simple/node/cpus/x86_64_v2/CmpIX86.java index d274875a..91b6d43c 100644 --- a/chapter21/src/main/java/com/seaofnodes/simple/node/cpus/x86_64_v2/CmpIX86.java +++ b/chapter21/src/main/java/com/seaofnodes/simple/node/cpus/x86_64_v2/CmpIX86.java @@ -7,9 +7,9 @@ import com.seaofnodes.simple.node.*; // Compare immediate. Sets flags. public class CmpIX86 extends MachConcreteNode implements MachNode { final int _imm; - CmpIX86( Node cmp, int imm ) { + CmpIX86( Node cmp, int imm, boolean swap ) { super(cmp); - _inputs.pop(); + _inputs.del(swap ? 1 : 2); _imm = imm; } CmpIX86( CmpIX86 cmp ) { @@ -18,11 +18,6 @@ public class CmpIX86 extends MachConcreteNode implements MachNode { addDef(cmp.in(1)); _imm = cmp._imm; } - CmpIX86( Node lhs, int imm, double ignore ) { - super(lhs); - _inputs.del(1); - _imm = imm; - } @Override public String op() { return _imm==0 ? "test" : "cmp"; } @Override public RegMask regmap(int i) { return x86_64_v2.RMASK; } diff --git a/chapter21/src/main/java/com/seaofnodes/simple/node/cpus/x86_64_v2/CmpMemX86.java b/chapter21/src/main/java/com/seaofnodes/simple/node/cpus/x86_64_v2/CmpMemX86.java index f9cd597b..489853eb 100644 --- a/chapter21/src/main/java/com/seaofnodes/simple/node/cpus/x86_64_v2/CmpMemX86.java +++ b/chapter21/src/main/java/com/seaofnodes/simple/node/cpus/x86_64_v2/CmpMemX86.java @@ -7,10 +7,11 @@ import com.seaofnodes.simple.type.Type; import com.seaofnodes.simple.type.TypeFloat; public class CmpMemX86 extends MemOpX86 { - final boolean _invert; // Op switched LHS, RHS - CmpMemX86( BoolNode bool, LoadNode ld, Node base, Node idx, int off, int scale, int imm, Node val, boolean invert ) { + final boolean _swap; // Op switched LHS, RHS + CmpMemX86( BoolNode bool, LoadNode ld, Node base, Node idx, int off, int scale, int imm, Node val, boolean swap ) { super(bool,ld, base, idx, off, scale, imm, val ); - _invert = invert; + _swap = swap; + assert !_swap || val() == null || bool.op()=="==" || bool.op()=="!="; // Cannot swap with immediate } @Override public String op() { return ((val()==null && _imm==0) ? "test" : "cmp") + _sz; } @Override public RegMask outregmap() { return x86_64_v2.FLAGS_MASK; } @@ -23,52 +24,55 @@ public class CmpMemX86 extends MemOpX86 { short src = enc.reg(val()); if(src == -1) { + assert !_swap; int log = _declaredType.log_size(); x86_64_v2.rexF(-1, ptr, idx, log==3, enc); switch(log) { - case 0: enc.add1(0x80); break; - case 1: enc.add1(0x66).add1(0x81); break; - case 2: enc.add1(0x81); break; - case 3: enc.add1(0x81); break; + case 0: enc.add1(0x80); break; + case 1: enc.add1(0x66).add1(0x81); break; + case 2: enc.add1(0x81); break; + case 3: enc.add1(0x81); break; } x86_64_v2.indirectAdr(_scale, idx, ptr, _off, 7, enc); switch(log) { - case 0: enc.add1(_imm); break; - case 1: enc.add2(_imm); break; - case 2: enc.add4(_imm); break; - case 3: enc.add8(_imm); break; + case 0: enc.add1(_imm); break; + case 1: enc.add2(_imm); break; + case 2: enc.add4(_imm); break; + case 3: enc.add8(_imm); break; } } else { - encVal(enc, _declaredType, ptr, idx, src, _off, _scale); + encVal(enc, _declaredType, ptr, idx, src, _off, _scale, _swap); } } - static void encVal(Encoding enc, Type decl, short ptr, short idx, short src, int off, int scale) { + static void encVal(Encoding enc, Type decl, short ptr, short idx, short src, int off, int scale, boolean _swap) { if(decl instanceof TypeFloat) { src -= (short)x86_64_v2.XMM_OFFSET; enc.add1(decl==TypeFloat.F32 ? 0xF3 : 0xF2).add1(0x0F).add1(0xC2); } else { int log = decl.log_size(); x86_64_v2.rexF(src, ptr, idx, log == 3, enc); + int swap = _swap ? 2 : 0; switch(log) { - case 0: enc.add1(0x38); break; - case 1: enc.add1(0x66).add1(0x3B); break; - case 2: enc.add1(0x3B); break; - case 3: enc.add1(0x3B); break; + case 0: enc.add1(0x38+swap); break; + case 1: enc.add1(0x66).add1(0x39+swap); break; + case 2: enc.add1(0x39+swap); break; + case 3: enc.add1(0x39+swap); break; } } x86_64_v2.indirectAdr(scale, idx, ptr, off, src, enc); } + // General form: "cmp dst = src, [base + idx<<2 + 12]" @Override public void asm(CodeGen code, SB sb) { String dst = code.reg(this); if( dst!="flags" ) sb.p(dst).p(" = "); - if( _invert ) asm_address(code,sb).p(","); + if( _swap ) asm_address(code,sb).p(","); if( val()==null ) { if( _imm!=0 ) sb.p("#").p(_imm); } else { sb.p(code.reg(val())); } - if( !_invert ) asm_address(code,sb.p(", ")); + if( !_swap ) asm_address(code,sb.p(", ")); } } 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 e6390386..23c12ae7 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 @@ -476,21 +476,23 @@ public class x86_64_v2 extends Machine { Node lhs = bool.in(1); Node rhs = bool.in(2); + // Since cmp does not record a BOP, SetX/Jmp need to know if the CMP is swapped. + // Vs memory if( lhs instanceof LoadNode ld && ld.nOuts() == 1 && rhs._type.isa(ld._declaredType) ) return new CmpMemX86(bool, address(ld), ld.ptr(), idx, off, scale, imm(rhs), val, false); + // Operands swap in the encoding directly, no need for Set/Jmp to swap `bop` if( rhs instanceof LoadNode ld && ld.nOuts() == 1 && lhs._type.isa(ld._declaredType)) return new CmpMemX86(bool, address(ld), ld.ptr(), idx, off, scale, imm(lhs), val, true ); // Vs immediate if( rhs instanceof ConstantNode con && con._con instanceof TypeInteger ti && imm32(ti.value()) ) - return new CmpIX86(bool, (int)ti.value()); + return new CmpIX86(bool, (int)ti.value(), false); - if( lhs instanceof ConstantNode con && con._con instanceof TypeInteger ti && imm32(ti.value()) ) { - swap = true; - return new CmpIX86(bool, (int)ti.value(), 0.5); - } + // Operand swap compare; Set and Jmp need to swap `bop` + if( lhs instanceof ConstantNode con && con._con instanceof TypeInteger ti && imm32(ti.value()) ) + return new CmpIX86(bool, (int)ti.value(), swap=true); // x vs y return new CmpX86(bool); @@ -519,7 +521,7 @@ public class x86_64_v2 extends Machine { // Most general arith ops will also set flags, which the Jmp needs directly. // Loads do not set the flags, and will need an explicit TEST String op = "!="; - if( iff.in(1) instanceof BoolNode bool ) op = bool.op(); + if( iff.in(1) instanceof BoolNode bool ) op = swap ? IfNode.swap(bool.op()) : bool.op(); else if( iff.in(1)==null ) op = "=="; // Never-node cutout else iff.setDef(1, new BoolNode.NE(iff.in(1), new ConstantNode(TypeInteger.ZERO))); return new JmpX86(iff, op); diff --git a/chapter21/src/main/java/com/seaofnodes/simple/print/ASMPrinter.java b/chapter21/src/main/java/com/seaofnodes/simple/print/ASMPrinter.java index 8f3c9bb2..cf3bdde3 100644 --- a/chapter21/src/main/java/com/seaofnodes/simple/print/ASMPrinter.java +++ b/chapter21/src/main/java/com/seaofnodes/simple/print/ASMPrinter.java @@ -79,6 +79,7 @@ public abstract class ASMPrinter { if( fun._name != null ) sb.p(fun._name).p(" "); fun.sig().print(sb); sb.p("---------------------------").nl(); + iadr = (iadr + 15)&-16; // All function entries padded to 16 align if( fun._frameAdjust != 0 ) iadr = doInst(iadr,sb,code,fun,cfgidx,fun,true,true); |
