diff options
| author | Cliff Click <cliffc@acm.org> | 2025-04-15 19:41:55 -0700 |
|---|---|---|
| committer | Cliff Click <cliffc@acm.org> | 2025-04-15 19:41:55 -0700 |
| commit | 2a3b2d4d4bc6099ab9fead00cfaa3f0a8e6aeb9f (patch) | |
| tree | 6a94bfc5734ba84497083d233281a569ffe0c67f /chapter21/src/main/java | |
| parent | 2fb0ec312d76f22534331023c86e68b7a62b98e6 (diff) | |
| download | Simple-2a3b2d4d4bc6099ab9fead00cfaa3f0a8e6aeb9f.tar.gz Simple-2a3b2d4d4bc6099ab9fead00cfaa3f0a8e6aeb9f.zip | |
R5 fcmp, flt load from pool
exportELF(null) now patches the cpool entries as well as globals.
Repeated cpool entries folded.
Fixed FltRISC, FCMP encoding.
Diffstat (limited to 'chapter21/src/main/java')
6 files changed, 34 insertions, 26 deletions
diff --git a/chapter21/src/main/java/com/seaofnodes/simple/codegen/CodeGen.java b/chapter21/src/main/java/com/seaofnodes/simple/codegen/CodeGen.java index 146c016a..2e1a4709 100644 --- a/chapter21/src/main/java/com/seaofnodes/simple/codegen/CodeGen.java +++ b/chapter21/src/main/java/com/seaofnodes/simple/codegen/CodeGen.java @@ -379,17 +379,16 @@ public class CodeGen { public int _tEncode; public Encoding _encoding; // Encoding object public void preEncode() { } // overridden by alternative ports - public CodeGen encode(boolean jit) { + public CodeGen encode() { assert _phase == Phase.RegAlloc; _phase = Phase.Encoding; long t0 = System.currentTimeMillis(); _encoding = new Encoding(this); preEncode(); - _encoding.encode(jit); + _encoding.encode(); _tEncode = (int)(System.currentTimeMillis() - t0); return this; } - public CodeGen encode() { return encode(false); } // --------------------------- // Exporting to external formats diff --git a/chapter21/src/main/java/com/seaofnodes/simple/codegen/Encoding.java b/chapter21/src/main/java/com/seaofnodes/simple/codegen/Encoding.java index 118ca47c..395b3d46 100644 --- a/chapter21/src/main/java/com/seaofnodes/simple/codegen/Encoding.java +++ b/chapter21/src/main/java/com/seaofnodes/simple/codegen/Encoding.java @@ -153,7 +153,7 @@ public class Encoding { _bigCons.put(relo,new Relo(relo,t,(byte)off,(byte)elf)); } - void encode(boolean jit) { + void encode() { // Basic block layout: invert branches to keep blocks in-order; insert // unconditional jumps. Attempt to keep backwards branches taken, // forwards not-taken (this is the default prediction on most @@ -170,10 +170,6 @@ public class Encoding { // Patch RIP-relative and local encodings now. patchLocalRelocations(); - - // If JIT'ing (in memory execution), patch the global relocations now - if( jit ) - patchGlobalRelocations(); } // Basic block layout: invert branches to keep blocks in-order; insert @@ -382,14 +378,10 @@ public class Encoding { // Write the constant pool into the BAOS and optionally patch locally void writeConstantPool( BAOS bits, boolean patch ) { - HashSet<Relo> ts = new HashSet<>(); - for( Relo t : _bigCons.values() ) { - if( ts.contains(t) ) - throw Utils.TODO(); // Dup! Compress! - ts.add(t); - } padN(16,bits); + HashMap<Type,Integer> targets = new HashMap<>(); + // By log size for( int log = 3; log >= 0; log-- ) { // Write the 8-byte constants @@ -397,17 +389,21 @@ public class Encoding { Relo relo = _bigCons.get(op); if( relo._t.log_size()==log ) { // Map from relo to constant start and patch - relo._target = bits.size(); + Integer target = targets.get(relo._t); + if( target==null ) { + targets.put(relo._t,target = bits.size()); + // Put constant into code space. + if( relo._t instanceof TypeTuple tt ) // Constant tuples put all entries + for( Type tx : tt._types ) + addN(log,tx,bits); + else + addN(log,relo._t,bits); + } + relo._target = target; relo._opStart= _opStart[op._nid]; // Go ahead and locally patch in-memory if( patch ) ((RIPRelSize)op).patch(this, relo._opStart, _opLen[op._nid], relo._target - relo._opStart); - // Put constant into code space. - if( relo._t instanceof TypeTuple tt ) // Constant tuples put all entries - for( Type tx : tt._types ) - addN(log,tx,bits); - else - addN(log,relo._t,bits); } } } diff --git a/chapter21/src/main/java/com/seaofnodes/simple/codegen/LinkMem.java b/chapter21/src/main/java/com/seaofnodes/simple/codegen/LinkMem.java index 07a5dd12..e1e849e6 100644 --- a/chapter21/src/main/java/com/seaofnodes/simple/codegen/LinkMem.java +++ b/chapter21/src/main/java/com/seaofnodes/simple/codegen/LinkMem.java @@ -1,14 +1,22 @@ package com.seaofnodes.simple.codegen; +import com.seaofnodes.simple.Utils; +import com.seaofnodes.simple.node.Node; + // In-memory linking public class LinkMem { final CodeGen _code; LinkMem( CodeGen code ) { _code = code; } public CodeGen link() { + Encoding enc = _code._encoding; + + // Patch external calls internally + enc.patchGlobalRelocations(); + // Write any large constants into a constant pool; they // are accessed by RIP-relative addressing. - _code._encoding.writeConstantPool(_code._encoding._bits,true); + enc.writeConstantPool(enc._bits,true); return _code; } diff --git a/chapter21/src/main/java/com/seaofnodes/simple/node/cpus/riscv/FltRISC.java b/chapter21/src/main/java/com/seaofnodes/simple/node/cpus/riscv/FltRISC.java index 0a7b02e3..35de7e04 100644 --- a/chapter21/src/main/java/com/seaofnodes/simple/node/cpus/riscv/FltRISC.java +++ b/chapter21/src/main/java/com/seaofnodes/simple/node/cpus/riscv/FltRISC.java @@ -22,7 +22,7 @@ public class FltRISC extends ConstantNode implements MachNode, RIPRelSize { // AUIPC dst,#hi20_constant_pool enc.add4(riscv.u_type(riscv.OP_AUIPC, tmp, 0)); // Load dst,[dst+#low12_constant_pool] - enc.add4(riscv.i_type(riscv.OP_LOAD, dst, 0b11, tmp, 0)); + enc.add4(riscv.i_type(riscv.OP_LOADFP, dst, 0b11, tmp, 0)); } @Override public RegMask killmap() { return new RegMask(riscv.T6); } @@ -35,9 +35,9 @@ public class FltRISC extends ConstantNode implements MachNode, RIPRelSize { short dst = (short)(enc.reg(this) - riscv.F_OFFSET); short tmp = (short)riscv.T6; // AUIPC dst,#hi20_constant_pool - enc.patch4(opStart , riscv.u_type(riscv.OP_AUIPC, tmp, delta)); + enc.patch4(opStart , riscv.u_type(riscv.OP_AUIPC, tmp, delta>>12)); // Load dst,[dst+#low12_constant_pool] - enc.patch4(opStart+4, riscv.i_type(riscv.OP_LOAD, dst, 0b11, tmp, delta)); + enc.patch4(opStart+4, riscv.i_type(riscv.OP_LOADFP, dst, 0b11, tmp, delta & 0xFFF)); } @Override public void asm(CodeGen code, SB sb) { diff --git a/chapter21/src/main/java/com/seaofnodes/simple/node/cpus/riscv/SetFRISC.java b/chapter21/src/main/java/com/seaofnodes/simple/node/cpus/riscv/SetFRISC.java index 64440536..9ee19e42 100644 --- a/chapter21/src/main/java/com/seaofnodes/simple/node/cpus/riscv/SetFRISC.java +++ b/chapter21/src/main/java/com/seaofnodes/simple/node/cpus/riscv/SetFRISC.java @@ -23,7 +23,7 @@ public class SetFRISC extends MachConcreteNode implements MachNode { short dst = enc.reg(this ); short src1 = (short)(enc.reg(in(1))-riscv.F_OFFSET); short src2 = (short)(enc.reg(in(2))-riscv.F_OFFSET); - int body = riscv.r_type(riscv.OP_FP,dst,riscv.fsetop(_bop),src1,src2,0); + int body = riscv.r_type(riscv.OP_FP,dst,riscv.fsetop(_bop),src1,src2,0b1010001/*FCMP*/); enc.add4(body); } 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 902aa3c2..8f3c9bb2 100644 --- a/chapter21/src/main/java/com/seaofnodes/simple/print/ASMPrinter.java +++ b/chapter21/src/main/java/com/seaofnodes/simple/print/ASMPrinter.java @@ -6,6 +6,7 @@ import com.seaofnodes.simple.codegen.CodeGen; import com.seaofnodes.simple.codegen.Encoding; import com.seaofnodes.simple.node.*; import com.seaofnodes.simple.type.*; +import java.util.HashSet; public abstract class ASMPrinter { @@ -26,11 +27,15 @@ public abstract class ASMPrinter { // constant pool Encoding enc = code._encoding; if( enc!=null && !enc._bigCons.isEmpty() ) { + iadr = (iadr+15)&-16; // pad to 16 + HashSet<Type> targets = new HashSet<>(); sb.p("--- Constant Pool ------").nl(); // By log size for( int log = 3; log >= 0; log-- ) { for( Node op : enc._bigCons.keySet() ) { Encoding.Relo relo = enc._bigCons.get(op); + if( targets.contains(relo._t) ) continue; + targets.add(relo._t); if( relo._t.log_size()==log ) { sb.hex2(iadr).p(" "); if( relo._t instanceof TypeTuple tt ) { |
