Skip to content

Commit

Permalink
Refactoring Op encodings towards CAD3
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Oct 7, 2024
1 parent 44661c4 commit fa97527
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 25 deletions.
7 changes: 7 additions & 0 deletions convex-core/src/main/java/convex/core/data/ACode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package convex.core.data;

public abstract class ACode extends ACell {



}
29 changes: 17 additions & 12 deletions convex-core/src/main/java/convex/core/data/Format.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -54,7 +53,7 @@ public class Format {
*
* Technical reasons for this choice:
* <ul>
* <li>This is the maximum length that can be VLQ encoded in a 2 byte message header. This simplifies message encoding and decoding.</li>
* <li>This is the maximum length that can be VLQ encoded in 2 bytes. This simplifies message encoding and decoding.</li>
* <li>It is big enough to include a 4096-byte Blob</li>
* <li>It is small enough to fit in a UDP message</li>
* </ul>
Expand Down Expand Up @@ -488,7 +487,8 @@ private static <T extends ACell> 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);
Expand Down Expand Up @@ -575,8 +575,6 @@ private static <T extends ACell> 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);

Expand All @@ -586,7 +584,9 @@ private static <T extends ACell> 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) {
Expand All @@ -599,8 +599,11 @@ private static <T extends ACell> T read(byte tag, Blob blob, int offset) throws
throw new BadFormatException(badTagMessage(tag));
}

private static <T extends ACell> AOp<T> 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 <T extends ACell> SignedData<T> readSignedData(byte tag,Blob blob, int offset) throws BadFormatException {
Expand All @@ -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);
Expand Down
14 changes: 8 additions & 6 deletions convex-core/src/main/java/convex/core/data/Tag.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions convex-core/src/main/java/convex/core/lang/AOp.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -19,7 +20,7 @@
*
* @param <T> the type of the operation return value
*/
public abstract class AOp<T extends ACell> extends ACell {
public abstract class AOp<T extends ACell> extends ACode {

/**
* Executes this op with the given context. Must preserve depth unless an
Expand Down Expand Up @@ -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);
}

Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions convex-core/src/main/java/convex/core/lang/Ops.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -55,7 +55,8 @@ public class Ops {
* @throws BadFormatException In the event of any encoding error
*/
@SuppressWarnings("unchecked")
public static <T extends ACell> AOp<T> read(Blob b, int pos, byte opCode) throws BadFormatException {
public static <T extends ACell> AOp<T> read(Blob b, int pos) throws BadFormatException {
byte opCode=b.byteAt(pos+1);
switch (opCode) {
case Ops.CONSTANT:
return Constant.read(b,pos);
Expand Down
6 changes: 3 additions & 3 deletions convex-core/src/test/java/convex/core/lang/OpsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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());
}
}

Expand Down Expand Up @@ -184,7 +184,7 @@ public void testSpecial() {
@Test
public void testSet() throws BadFormatException {
AOp<Address> 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);
Expand Down

0 comments on commit fa97527

Please sign in to comment.