From 8784736848d846f40c90c41edeb9162c00d0bdbd Mon Sep 17 00:00:00 2001 From: Cliff Click Date: Fri, 14 Mar 2025 13:38:11 -0700 Subject: ASM printer includes encodings --- chapter21/src/main/java/com/seaofnodes/simple/SB.java | 8 ++++++++ .../java/com/seaofnodes/simple/codegen/Encoding.java | 16 ++++++++++++---- .../java/com/seaofnodes/simple/print/ASMPrinter.java | 11 ++++++++--- 3 files changed, 28 insertions(+), 7 deletions(-) (limited to 'chapter21/src/main/java/com/seaofnodes') diff --git a/chapter21/src/main/java/com/seaofnodes/simple/SB.java b/chapter21/src/main/java/com/seaofnodes/simple/SB.java index a45f0ddf..72649a39 100644 --- a/chapter21/src/main/java/com/seaofnodes/simple/SB.java +++ b/chapter21/src/main/java/com/seaofnodes/simple/SB.java @@ -29,6 +29,14 @@ public final class SB { public SB p( int s ) { _sb.append(s); return this; } public SB p( long s ) { _sb.append(s); return this; } public SB p( boolean s) { _sb.append(s); return this; } + // 2 hex digits + public SB hex2(int s) { + int digit = (s>>4) & 0xf; + _sb.append((char)((digit <= 9 ? '0' : ('A'-10))+digit)); + digit = s & 0xf; + _sb.append((char)((digit <= 9 ? '0' : ('A'-10))+digit)); + return this; + } // 4 hex digits public SB hex4(int s) { assert (s>>4*4)==0; // Fits in 16 bits 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 50eab8f1..413754b0 100644 --- a/chapter21/src/main/java/com/seaofnodes/simple/codegen/Encoding.java +++ b/chapter21/src/main/java/com/seaofnodes/simple/codegen/Encoding.java @@ -35,11 +35,14 @@ public class Encoding { // - RIP-relative to external chunks have a zero offset; the matching // relocation info will be used to patch the correct value. - final ByteArrayOutputStream _bits; + public class BAOS extends ByteArrayOutputStream { public byte[] buf() { return buf; } }; + final public BAOS _bits; + + public byte[] _opLen; Encoding( CodeGen code ) { _code = code; - _bits = new ByteArrayOutputStream(); + _bits = new BAOS(); } // Short cut to the defining register @@ -100,15 +103,20 @@ public class Encoding { rpo.swap(i,rpo.size()-1-i); visit.clear(); _code._cfg = rpo; // Save the new ordering + _opLen = new byte[_code.UID()]; // Write encoding bits in order for( CFGNode bb : _code._cfg ) for( Node n : bb._outputs ) - if( n instanceof MachNode mach ) + if( n instanceof MachNode mach ) { + int off = _bits.size(); mach.encoding(this); + _opLen[n._nid] = (byte)(_bits.size()-off); + } + System.out.println(_code.asm()); // Patch RIP-relative encodings now - throw Utils.TODO(); + //throw Utils.TODO(); } // Basic block layout. Now that RegAlloc is finished, no more spill code 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 a7a0bce0..c97d809d 100644 --- a/chapter21/src/main/java/com/seaofnodes/simple/print/ASMPrinter.java +++ b/chapter21/src/main/java/com/seaofnodes/simple/print/ASMPrinter.java @@ -124,9 +124,14 @@ public abstract class ASMPrinter { sb.p(" "); // Encoding - int size = 1; // TODO: Fake encoding size - iadr += size; - sb.fix(encWidth,"??"); + if( code._encoding != null ) { + int size = code._encoding._opLen[n._nid]; + for( int i=0; i