summaryrefslogtreecommitdiff
path: root/chapter21/src/main/java/com
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2025-03-14 13:38:11 -0700
committerCliff Click <cliffc@acm.org>2025-03-14 13:38:11 -0700
commit8784736848d846f40c90c41edeb9162c00d0bdbd (patch)
treef50174b6aa6e28f72af6438712bf16441a99cb5a /chapter21/src/main/java/com
parentde7fbdcf3f2002903d03c7737f573fa1e84cb306 (diff)
downloadSimple-8784736848d846f40c90c41edeb9162c00d0bdbd.tar.gz
Simple-8784736848d846f40c90c41edeb9162c00d0bdbd.zip
ASM printer includes encodings
Diffstat (limited to 'chapter21/src/main/java/com')
-rw-r--r--chapter21/src/main/java/com/seaofnodes/simple/SB.java8
-rw-r--r--chapter21/src/main/java/com/seaofnodes/simple/codegen/Encoding.java16
-rw-r--r--chapter21/src/main/java/com/seaofnodes/simple/print/ASMPrinter.java11
3 files changed, 28 insertions, 7 deletions
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<size; i++ )
+ sb.hex2(code._encoding._bits.buf()[iadr++]);
+ for( int i=size*2; i<encWidth; i++ )
+ sb.p(" ");
+ } else
+ sb.fix(encWidth,"");
sb.p(" ");
// Op; generally "ld4" or "call"