Skip to content

Commit fa97527

Browse files
committed
Refactoring Op encodings towards CAD3
1 parent 44661c4 commit fa97527

File tree

6 files changed

+42
-25
lines changed

6 files changed

+42
-25
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package convex.core.data;
2+
3+
public abstract class ACode extends ACell {
4+
5+
6+
7+
}

convex-core/src/main/java/convex/core/data/Format.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import convex.core.exceptions.MissingDataException;
2626
import convex.core.exceptions.Panic;
2727
import convex.core.lang.AFn;
28-
import convex.core.lang.AOp;
2928
import convex.core.lang.Core;
3029
import convex.core.lang.Ops;
3130
import convex.core.lang.RT;
@@ -54,7 +53,7 @@ public class Format {
5453
*
5554
* Technical reasons for this choice:
5655
* <ul>
57-
* <li>This is the maximum length that can be VLQ encoded in a 2 byte message header. This simplifies message encoding and decoding.</li>
56+
* <li>This is the maximum length that can be VLQ encoded in 2 bytes. This simplifies message encoding and decoding.</li>
5857
* <li>It is big enough to include a 4096-byte Blob</li>
5958
* <li>It is small enough to fit in a UDP message</li>
6059
* </ul>
@@ -488,7 +487,8 @@ private static <T extends ACell> T readDataStructure(byte tag, Blob b, int pos)
488487
}
489488

490489
private static ACell readCode(byte tag, Blob b, int pos) throws BadFormatException {
491-
if (tag == Tag.CORE_DEF) return Core.read(b, pos);
490+
if (tag == Tag.OP) return Ops.read(b, pos);
491+
492492

493493
if (tag == Tag.FN_MULTI) {
494494
AFn<?> fn = MultiFn.read(b,pos);
@@ -575,8 +575,6 @@ private static <T extends ACell> T read(byte tag, Blob blob, int offset) throws
575575
if (tag == Tag.ADDRESS) return (T) Address.read(blob,offset);
576576

577577
if (high == 0xB0) return (T) AByteFlag.read(tag);
578-
579-
if (high == 0xE0) return (T) readOp(tag,blob,offset);
580578

581579
if (high == 0xC0) return (T) readCode(tag,blob,offset);
582580

@@ -586,7 +584,9 @@ private static <T extends ACell> T read(byte tag, Blob blob, int offset) throws
586584

587585
if (high == 0xD0) return (T) readTransaction(tag, blob, offset);
588586

589-
if (high == 0xA0) return (T) readRecord(tag,blob,offset);
587+
if (high == 0xE0) return (T) readExtension(tag, blob, offset);
588+
589+
if (high == 0xA0) return (T) readRecord(tag,blob,offset);
590590
} catch (BadFormatException e) {
591591
throw e;
592592
} catch (IndexOutOfBoundsException e) {
@@ -599,8 +599,11 @@ private static <T extends ACell> T read(byte tag, Blob blob, int offset) throws
599599
throw new BadFormatException(badTagMessage(tag));
600600
}
601601

602-
private static <T extends ACell> AOp<T> readOp(byte tag, Blob blob, int offset) throws BadFormatException {
603-
return Ops.read(blob, offset, (byte) (tag&0x0f));
602+
private static ACell readExtension(byte tag, Blob blob, int offset) throws BadFormatException {
603+
if (tag == Tag.CORE_DEF) return Core.read(blob, offset);
604+
605+
throw new BadFormatException(badTagMessage(tag));
606+
604607
}
605608

606609
private static <T extends ACell> SignedData<T> 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
624627
}
625628

626629
private static ACell readBasicObject(byte tag, Blob blob, int offset) throws BadFormatException{
627-
if (tag == Tag.SYMBOL) return Symbol.read(blob,offset);
628-
if (tag == Tag.KEYWORD) return Keyword.read(blob,offset);
629-
if (tag == Tag.BLOB) return Blobs.read(blob,offset);
630-
if (tag == Tag.STRING) return Strings.read(blob,offset);
630+
switch (tag) {
631+
case Tag.SYMBOL: return Symbol.read(blob,offset);
632+
case Tag.KEYWORD: return Keyword.read(blob,offset);
633+
case Tag.BLOB: return Blobs.read(blob,offset);
634+
case Tag.STRING: return Strings.read(blob,offset);
635+
}
631636

632637
if ((tag&Tag.CHAR_MASK)==Tag.CHAR_BASE) {
633638
int len=CVMChar.byteCountFromTag(tag);

convex-core/src/main/java/convex/core/data/Tag.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,11 @@ public class Tag {
8888

8989
// ==========================================
9090
// Coded data (0xCx)
91+
92+
public static final byte CODE_BASE = (byte) 0xC0;
9193

92-
public static final byte COMMAND = (byte) 0xC0;
93-
94-
// Code
95-
public static final byte CORE_DEF = (byte) 0xCD;
94+
// CVM Code
95+
public static final byte OP = (byte) 0xC0;
9696
public static final byte FN = (byte) 0xCF;
9797
public static final byte FN_MULTI = (byte) 0xCB;
9898

@@ -111,8 +111,10 @@ public class Tag {
111111

112112
//==========================================
113113
// Extension values (0xEx)
114-
115-
public static final byte OP = (byte) 0xE0;
114+
115+
// CVM Core definitions
116+
public static final byte CORE_DEF = (byte) 0xED;
117+
116118

117119
//===========================================
118120
// Illegal / reserved for special values (0xFx)

convex-core/src/main/java/convex/core/lang/AOp.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package convex.core.lang;
22

33
import convex.core.data.ACell;
4+
import convex.core.data.ACode;
45
import convex.core.data.Format;
56
import convex.core.data.IRefFunction;
67
import convex.core.data.Tag;
@@ -19,7 +20,7 @@
1920
*
2021
* @param <T> the type of the operation return value
2122
*/
22-
public abstract class AOp<T extends ACell> extends ACell {
23+
public abstract class AOp<T extends ACell> extends ACode {
2324

2425
/**
2526
* Executes this op with the given context. Must preserve depth unless an
@@ -70,6 +71,7 @@ public ACell toCanonical() {
7071
@Override
7172
public final int encode(byte[] bs, int pos) {
7273
bs[pos++]=getTag();
74+
bs[pos++]=opCode();
7375
return encodeRaw(bs,pos);
7476
}
7577

@@ -89,7 +91,7 @@ public final int encode(byte[] bs, int pos) {
8991

9092
@Override
9193
public byte getTag() {
92-
return (byte) (Tag.OP+opCode());
94+
return Tag.OP;
9395
}
9496

9597
@Override

convex-core/src/main/java/convex/core/lang/Ops.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public class Ops {
4343
/**
4444
* Offset of Op data from tag byte
4545
*/
46-
public static final int OP_DATA_OFFSET=1;
46+
public static final int OP_DATA_OFFSET=2;
4747

4848
/**
4949
* Reads an Op from the given Blob. Assumes tag specifying an Op already read.
@@ -55,7 +55,8 @@ public class Ops {
5555
* @throws BadFormatException In the event of any encoding error
5656
*/
5757
@SuppressWarnings("unchecked")
58-
public static <T extends ACell> AOp<T> read(Blob b, int pos, byte opCode) throws BadFormatException {
58+
public static <T extends ACell> AOp<T> read(Blob b, int pos) throws BadFormatException {
59+
byte opCode=b.byteAt(pos+1);
5960
switch (opCode) {
6061
case Ops.CONSTANT:
6162
return Constant.read(b,pos);

convex-core/src/test/java/convex/core/lang/OpsTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public void testConstant() {
7777
assertNull(c2.getResult());
7878
doOpTest(op);
7979

80-
assertEquals(Blob.wrap(new byte[] {op.getTag(),Tag.NULL}),op.getEncoding());
80+
assertEquals(Blob.wrap(new byte[] {Tag.OP,Ops.CONSTANT,Tag.NULL}),op.getEncoding());
8181
}
8282

8383
{// nested constant
@@ -88,7 +88,7 @@ public void testConstant() {
8888
assertEquals(Constant.nil(),c2.getResult());
8989
doOpTest(op);
9090

91-
assertEquals(Blob.wrap(new byte[] {op.getTag(),op.getTag(),Tag.NULL}),op.getEncoding());
91+
assertEquals(Blob.wrap(new byte[] {Tag.OP,Ops.CONSTANT,Tag.OP,Ops.CONSTANT,Tag.NULL}),op.getEncoding());
9292
}
9393
}
9494

@@ -184,7 +184,7 @@ public void testSpecial() {
184184
@Test
185185
public void testSet() throws BadFormatException {
186186
AOp<Address> op = Set.create(45, Constant.nil());
187-
Blob expectedEncoding=Blob.fromHex("eb2de000");
187+
Blob expectedEncoding=Blob.fromHex("c00b2dc00000");
188188
assertEquals(expectedEncoding,op.getEncoding());
189189
assertEquals(op,Format.read(expectedEncoding));
190190
doOpTest(op);

0 commit comments

Comments
 (0)