diff --git a/Jocaml.java b/Jocaml.java index ffacb56..9293415 100644 --- a/Jocaml.java +++ b/Jocaml.java @@ -389,14 +389,14 @@ public class Jocaml { } } - public void unmarshal() throws Exception { + public List unmarshal() throws Exception { this.data.order(ByteOrder.BIG_ENDIAN); int magic = this.data.getInt(0); if (magic != MARSHAL_MAGIC_SMALL) { throw new Exception("bad marshal magic"); } - int byteLength = this.data.getInt(4); + // int byteLength = this.data.getInt(4); int numObjs = this.data.getInt(8); System.out.println(numObjs); @@ -411,26 +411,39 @@ public class Jocaml { } System.out.println(shared.size()); - - System.out.println(objs); + return objs; } - public void disassemble() throws Exception { + public void disassemble(List prim) throws Exception { this.data.order(ByteOrder.LITTLE_ENDIAN); this.data.position(0); + int offset = 0; while (this.data.position() < this.data.capacity()) { int op = this.data.getInt(); String rep = null; try { CamlInst inst = CamlInst.values()[op]; rep = inst.name(); - for (int i = 0; i < inst.numArgs; i++) { - rep += " " + this.data.getInt(); + if (inst == CamlInst.C_CALL1 || inst == CamlInst.C_CALL2 + || inst == CamlInst.C_CALL3 || inst == CamlInst.C_CALL4 + || inst == CamlInst.C_CALL5 || inst == CamlInst.C_CALLN) { + int p = this.data.getInt(); + rep += " " + prim.get(p); + if (inst == CamlInst.C_CALLN) { + int n = this.data.getInt(); + rep += " " + n; + } + } else { + for (int i = 0; i < inst.numArgs; i++) { + rep += " " + this.data.getInt(); + } } } catch (ArrayIndexOutOfBoundsException e) { rep = ""; } + System.out.printf("%04d: ", offset); System.out.println(rep); + offset += 1; } } } @@ -479,9 +492,13 @@ public class Jocaml { } System.out.println(Arrays.toString(sections)); - findSection(sections, "SYMB").unmarshal(); - findSection(sections, "CRCS").unmarshal(); - findSection(sections, "DATA").unmarshal(); - findSection(sections, "CODE").disassemble(); + List symb = findSection(sections, "SYMB").unmarshal(); + System.out.println(symb); + List crcs = findSection(sections, "CRCS").unmarshal(); + System.out.println(crcs); + List data = findSection(sections, "DATA").unmarshal(); + System.out.println(data); + List prim = findSection(sections, "PRIM").unstring(); + findSection(sections, "CODE").disassemble(prim); } }