From fa97527a69975ef988a587d55635d45fc706099a Mon Sep 17 00:00:00 2001 From: mikera Date: Mon, 7 Oct 2024 09:34:41 +0100 Subject: [PATCH] Refactoring Op encodings towards CAD3 --- .../src/main/java/convex/core/data/ACode.java | 7 +++++ .../main/java/convex/core/data/Format.java | 29 +++++++++++-------- .../src/main/java/convex/core/data/Tag.java | 14 +++++---- .../src/main/java/convex/core/lang/AOp.java | 6 ++-- .../src/main/java/convex/core/lang/Ops.java | 5 ++-- .../test/java/convex/core/lang/OpsTest.java | 6 ++-- 6 files changed, 42 insertions(+), 25 deletions(-) create mode 100644 convex-core/src/main/java/convex/core/data/ACode.java diff --git a/convex-core/src/main/java/convex/core/data/ACode.java b/convex-core/src/main/java/convex/core/data/ACode.java new file mode 100644 index 000000000..0ea98775d --- /dev/null +++ b/convex-core/src/main/java/convex/core/data/ACode.java @@ -0,0 +1,7 @@ +package convex.core.data; + +public abstract class ACode extends ACell { + + + +} diff --git a/convex-core/src/main/java/convex/core/data/Format.java b/convex-core/src/main/java/convex/core/data/Format.java index 543960ddc..7bc433211 100644 --- a/convex-core/src/main/java/convex/core/data/Format.java +++ b/convex-core/src/main/java/convex/core/data/Format.java @@ -25,7 +25,6 @@ import convex.core.exceptions.MissingDataException; import convex.core.exceptions.Panic; import convex.core.lang.AFn; -import convex.core.lang.AOp; import convex.core.lang.Core; import convex.core.lang.Ops; import convex.core.lang.RT; @@ -54,7 +53,7 @@ public class Format { * * Technical reasons for this choice: * @@ -488,7 +487,8 @@ private static T readDataStructure(byte tag, Blob b, int pos) } private static ACell readCode(byte tag, Blob b, int pos) throws BadFormatException { - if (tag == Tag.CORE_DEF) return Core.read(b, pos); + if (tag == Tag.OP) return Ops.read(b, pos); + if (tag == Tag.FN_MULTI) { AFn fn = MultiFn.read(b,pos); @@ -575,8 +575,6 @@ private static T read(byte tag, Blob blob, int offset) throws if (tag == Tag.ADDRESS) return (T) Address.read(blob,offset); if (high == 0xB0) return (T) AByteFlag.read(tag); - - if (high == 0xE0) return (T) readOp(tag,blob,offset); if (high == 0xC0) return (T) readCode(tag,blob,offset); @@ -586,7 +584,9 @@ private static T read(byte tag, Blob blob, int offset) throws if (high == 0xD0) return (T) readTransaction(tag, blob, offset); - if (high == 0xA0) return (T) readRecord(tag,blob,offset); + if (high == 0xE0) return (T) readExtension(tag, blob, offset); + + if (high == 0xA0) return (T) readRecord(tag,blob,offset); } catch (BadFormatException e) { throw e; } catch (IndexOutOfBoundsException e) { @@ -599,8 +599,11 @@ private static T read(byte tag, Blob blob, int offset) throws throw new BadFormatException(badTagMessage(tag)); } - private static AOp readOp(byte tag, Blob blob, int offset) throws BadFormatException { - return Ops.read(blob, offset, (byte) (tag&0x0f)); + private static ACell readExtension(byte tag, Blob blob, int offset) throws BadFormatException { + if (tag == Tag.CORE_DEF) return Core.read(blob, offset); + + throw new BadFormatException(badTagMessage(tag)); + } private static SignedData readSignedData(byte tag,Blob blob, int offset) throws BadFormatException { @@ -624,10 +627,12 @@ private static ANumeric readNumeric(byte tag, Blob blob, int offset) throws BadF } private static ACell readBasicObject(byte tag, Blob blob, int offset) throws BadFormatException{ - if (tag == Tag.SYMBOL) return Symbol.read(blob,offset); - if (tag == Tag.KEYWORD) return Keyword.read(blob,offset); - if (tag == Tag.BLOB) return Blobs.read(blob,offset); - if (tag == Tag.STRING) return Strings.read(blob,offset); + switch (tag) { + case Tag.SYMBOL: return Symbol.read(blob,offset); + case Tag.KEYWORD: return Keyword.read(blob,offset); + case Tag.BLOB: return Blobs.read(blob,offset); + case Tag.STRING: return Strings.read(blob,offset); + } if ((tag&Tag.CHAR_MASK)==Tag.CHAR_BASE) { int len=CVMChar.byteCountFromTag(tag); diff --git a/convex-core/src/main/java/convex/core/data/Tag.java b/convex-core/src/main/java/convex/core/data/Tag.java index b4e5d3f24..dc730daca 100644 --- a/convex-core/src/main/java/convex/core/data/Tag.java +++ b/convex-core/src/main/java/convex/core/data/Tag.java @@ -88,11 +88,11 @@ public class Tag { // ========================================== // Coded data (0xCx) + + public static final byte CODE_BASE = (byte) 0xC0; - public static final byte COMMAND = (byte) 0xC0; - - // Code - public static final byte CORE_DEF = (byte) 0xCD; + // CVM Code + public static final byte OP = (byte) 0xC0; public static final byte FN = (byte) 0xCF; public static final byte FN_MULTI = (byte) 0xCB; @@ -111,8 +111,10 @@ public class Tag { //========================================== // Extension values (0xEx) - - public static final byte OP = (byte) 0xE0; + + // CVM Core definitions + public static final byte CORE_DEF = (byte) 0xED; + //=========================================== // Illegal / reserved for special values (0xFx) diff --git a/convex-core/src/main/java/convex/core/lang/AOp.java b/convex-core/src/main/java/convex/core/lang/AOp.java index e3f9f7f07..76dfcce5d 100644 --- a/convex-core/src/main/java/convex/core/lang/AOp.java +++ b/convex-core/src/main/java/convex/core/lang/AOp.java @@ -1,6 +1,7 @@ package convex.core.lang; import convex.core.data.ACell; +import convex.core.data.ACode; import convex.core.data.Format; import convex.core.data.IRefFunction; import convex.core.data.Tag; @@ -19,7 +20,7 @@ * * @param the type of the operation return value */ -public abstract class AOp extends ACell { +public abstract class AOp extends ACode { /** * Executes this op with the given context. Must preserve depth unless an @@ -70,6 +71,7 @@ public ACell toCanonical() { @Override public final int encode(byte[] bs, int pos) { bs[pos++]=getTag(); + bs[pos++]=opCode(); return encodeRaw(bs,pos); } @@ -89,7 +91,7 @@ public final int encode(byte[] bs, int pos) { @Override public byte getTag() { - return (byte) (Tag.OP+opCode()); + return Tag.OP; } @Override diff --git a/convex-core/src/main/java/convex/core/lang/Ops.java b/convex-core/src/main/java/convex/core/lang/Ops.java index 8c2c034b6..1827a7d9c 100644 --- a/convex-core/src/main/java/convex/core/lang/Ops.java +++ b/convex-core/src/main/java/convex/core/lang/Ops.java @@ -43,7 +43,7 @@ public class Ops { /** * Offset of Op data from tag byte */ - public static final int OP_DATA_OFFSET=1; + public static final int OP_DATA_OFFSET=2; /** * Reads an Op from the given Blob. Assumes tag specifying an Op already read. @@ -55,7 +55,8 @@ public class Ops { * @throws BadFormatException In the event of any encoding error */ @SuppressWarnings("unchecked") - public static AOp read(Blob b, int pos, byte opCode) throws BadFormatException { + public static AOp read(Blob b, int pos) throws BadFormatException { + byte opCode=b.byteAt(pos+1); switch (opCode) { case Ops.CONSTANT: return Constant.read(b,pos); diff --git a/convex-core/src/test/java/convex/core/lang/OpsTest.java b/convex-core/src/test/java/convex/core/lang/OpsTest.java index 77cc36e29..5582f1bfc 100644 --- a/convex-core/src/test/java/convex/core/lang/OpsTest.java +++ b/convex-core/src/test/java/convex/core/lang/OpsTest.java @@ -77,7 +77,7 @@ public void testConstant() { assertNull(c2.getResult()); doOpTest(op); - assertEquals(Blob.wrap(new byte[] {op.getTag(),Tag.NULL}),op.getEncoding()); + assertEquals(Blob.wrap(new byte[] {Tag.OP,Ops.CONSTANT,Tag.NULL}),op.getEncoding()); } {// nested constant @@ -88,7 +88,7 @@ public void testConstant() { assertEquals(Constant.nil(),c2.getResult()); doOpTest(op); - assertEquals(Blob.wrap(new byte[] {op.getTag(),op.getTag(),Tag.NULL}),op.getEncoding()); + assertEquals(Blob.wrap(new byte[] {Tag.OP,Ops.CONSTANT,Tag.OP,Ops.CONSTANT,Tag.NULL}),op.getEncoding()); } } @@ -184,7 +184,7 @@ public void testSpecial() { @Test public void testSet() throws BadFormatException { AOp
op = Set.create(45, Constant.nil()); - Blob expectedEncoding=Blob.fromHex("eb2de000"); + Blob expectedEncoding=Blob.fromHex("c00b2dc00000"); assertEquals(expectedEncoding,op.getEncoding()); assertEquals(op,Format.read(expectedEncoding)); doOpTest(op);