Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
Loading items

Target

Select target project
  • anders_blomdell/labcomm
  • klaren/labcomm
  • tommyo/labcomm
  • erikj/labcomm
  • sven/labcomm
5 results
Select Git revision
Loading items
Show changes
Showing
with 1688 additions and 618 deletions
......@@ -6,7 +6,7 @@ aspect PPIndentation {
inh String Field.pp_indent();
inh String StructType.pp_indent();
eq StructType.getField(int index).pp_indent() = pp_indent() + " ";
eq Program.getDecl(int index).pp_indent() = "";
eq Specification.getDecl(int index).pp_indent() = "";
}
......@@ -18,7 +18,7 @@ aspect PrettyPrint {
" not declared");
}
public void Program.pp(PrintStream out) {
public void Specification.pp(PrintStream out) {
for(int i = 0; i < getNumDecl(); i++) {
getDecl(i).pp(out);
}
......@@ -27,24 +27,24 @@ aspect PrettyPrint {
// Pretty print declarations
public void TypeDecl.pp(PrintStream out) {
out.print("typedef ");
getType().ppIdentifier(out, getName());
getDataType().ppIdentifier(out, getName());
out.println(";");
}
public void SampleDecl.pp(PrintStream out) {
out.print("sample ");
getType().ppIdentifier(out, getName());
getDataType().ppIdentifier(out, getName());
out.println(";");
}
public void Field.pp(PrintStream out) {
out.print(pp_indent());
getType().ppIdentifier(out, getName());
getDataType().ppIdentifier(out, getName());
out.println(";");
}
// Pretty print variable of a given type
public void Type.ppIdentifier(PrintStream out, String id) {
public void DataType.ppIdentifier(PrintStream out, String id) {
ppPrefix(out);
out.print(" ");
out.print(id);
......@@ -58,7 +58,7 @@ aspect PrettyPrint {
}
// PrettyPrint prefix type info
public void Type.ppPrefix(PrintStream out) {
public void DataType.ppPrefix(PrintStream out) {
throw new Error(this.getClass().getName() +
".ppPrefix(PrintStream out)" +
" not declared");
......@@ -68,9 +68,9 @@ aspect PrettyPrint {
out.print("void");
}
public void SampleRefType.ppPrefix(PrintStream out) {
out.print("sample");
}
// public void SampleRefType.ppPrefix(PrintStream out) {
// out.print("sample");
// }
public void PrimType.ppPrefix(PrintStream out) {
out.print(getName());
......@@ -81,7 +81,7 @@ aspect PrettyPrint {
}
public void ArrayType.ppPrefix(PrintStream out) {
getType().ppPrefix(out);
getDataType().ppPrefix(out);
}
public void StructType.ppPrefix(PrintStream out) {
......@@ -94,7 +94,7 @@ aspect PrettyPrint {
}
// PrettyPrint suffix type info (array dimensions)
public void Type.ppSuffix(PrintStream out) { }
public void DataType.ppSuffix(PrintStream out) { }
public void ArrayType.ppSuffix(PrintStream out) {
out.print("[");
......@@ -103,7 +103,7 @@ aspect PrettyPrint {
getExp(i).pp(out);
}
out.print("]");
getType().ppSuffix(out);
getDataType().ppSuffix(out);
}
public void IntegerLiteral.pp(PrintStream out) {
......
......@@ -4,149 +4,122 @@ aspect Python_CodeGenEnv {
// handles qualid nesting, indentation, file writing and
// prefix propagation
public class Python_env {
final private class Python_printer {
private boolean newline = true;
private PrintStream out;
public Python_printer(PrintStream out) {
this.out = out;
}
public void print(Python_env env, String s) {
if (newline) {
newline = false;
for (int i = 0 ; i < env.indent ; i++) {
out.print(" ");
}
}
out.print(s);
}
public void println(Python_env env, String s) {
print(env, s);
out.println();
newline = true;
}
public void println(Python_env env) {
out.println();
newline = true;
}
public class Python_env extends PrintEnv {
final private class Python_printer extends PrintEnv.Printer {
// public C_printer(PrintStream out) {
// super(out, " ");
// }
}
private int indent;
private Python_printer printer;
public Python_env(PrintStream out) {
this.indent = 0;
this.printer = new Python_printer(out);
}
public void indent() {
indent++;
}
public void unindent() {
indent--;
}
public void print(String s) {
printer.print(this, s);
}
public void println(String s) {
printer.println(this, s);
}
public void println() {
printer.println(this);
super(out);
}
}
}
aspect Python_CodeGen {
public void Program.Python_gen(PrintStream out, String baseName, int version) {
// Remove when Blomdell has verified that it is OK to ignore version
// when generating python code.
System.err.println("*** Warning! Python_gen ignores version: "+version);
public void Specification.Python_gen(PrintStream out, String baseName, int version) {
Python_env env = new Python_env(out);
env.println("#!/usr/bin/python");
env.println("# Auto generated " + baseName);
env.println();
env.println("import labcomm");
env.println("import StringIO");
env.println("import labcomm2014");
env.println();
Python_genTypes(env);
//env.println("typedef = [");
//env.indent();
//for (int i = 0 ; i < getNumDecl() ; i++) {
// getDecl(i).Python_genTypedefListEntry(env);
//}
//env.unindent();
//env.println("]");
env.println("sample = [");
env.println("typedef = tuple([");
env.indent();
for (int i = 0 ; i < getNumDecl() ; i++) {
getDecl(i).Python_genTypedefListEntry(env);
}
env.unindent();
env.println("])");
env.println("sample = tuple([");
env.indent();
for (int i = 0 ; i < getNumDecl() ; i++) {
getDecl(i).Python_genSampleListEntry(env);
}
env.unindent();
env.println("]");
env.println("])");
}
}
aspect PythonTypes {
public void Program.Python_genTypes(Python_env env) {
public void Specification.Python_genTypes(Python_env env) {
for (int i = 0 ; i < getNumDecl() ; i++) {
getDecl(i).Python_genSignature(env);
getDecl(i).Python_genSignatureAndTypedef(env);
}
}
public void Decl.Python_genSignature(Python_env env) {
public void Decl.Python_genSignatureAndTypedef(Python_env env) {
throw new Error(this.getClass().getName() +
".Python_genSignature(Python_env env)" +
".Python_genSignatureAndTypedef(Python_env env)" +
" not declared");
}
public void TypeDecl.Python_genSignature(Python_env env) {
/*
public void TypeDecl.Python_genSignatureAndTypedef(Python_env env) {
env.println("class " + getName() + "(object):");
env.indent();
env.println("signature = labcomm.typedef('" + getName() + "',");
env.print("typedef = labcomm2014.typedef(");
Python_genIntentions(env);
env.println(",");
env.indent();
getType().Python_genSignature(env);
getTypeInstance().Python_genTypedef(env);
env.unindent();
env.println(")");
env.unindent();
env.println();
*/
}
public void SampleDecl.Python_genSignature(Python_env env) {
public void SampleDecl.Python_genSignatureAndTypedef(Python_env env) {
env.println("class " + getName() + "(object):");
env.indent();
env.println("signature = labcomm.sample('" + getName() + "', ");
env.print("signature = labcomm2014.sample(");
Python_genIntentions(env);
env.println(",");
env.indent();
getType().Python_genSignature(env);
getDataType().Python_genSignature(env);
env.unindent();
env.println(")");
env.print("typedef = labcomm2014.sample(");
Python_genIntentions(env);
env.println(",");
env.indent();
getTypeInstance().Python_genTypedef(env);
env.unindent();
env.println(")");
env.unindent();
env.println();
}
public void Decl.Python_genIntentions(Python_env env) {
getTypeInstance().Python_genIntentions(env);
}
public void TypeInstance.Python_genIntentions(Python_env env) {
// env.print("{");
// for(Intention i : sortedIntentions()) {
// env.print("'"+i.getKey()+"':'"+new String(i.getValue())+"', ");
// }
// env.print("}");
env.print("tuple((");
for(Intention i : sortedIntentions()) {
env.print("('"+i.getKey()+"','"+new String(i.getValue())+"'), ");
}
env.print("))");
}
public void TypeInstance.Python_genTypedef(Python_env env) {
getDataType().Python_genTypedef(env);
}
public void UserType.Python_genSignature(Python_env env) {
lookupType(getName()).getType().Python_genSignature(env);
lookupType(getName()).getDataType().Python_genSignature(env);
}
public void Type.Python_genSignature(Python_env env) {
public void DataType.Python_genSignature(Python_env env) {
throw new Error(this.getClass().getName() +
".Python_genSignature(Python_env env)" +
" not declared");
......@@ -154,33 +127,33 @@ aspect PythonTypes {
public void PrimType.Python_genSignature(Python_env env) {
switch (getToken()) {
case LABCOMM_BOOLEAN: { env.print("labcomm.BOOLEAN()"); } break;
case LABCOMM_BYTE: { env.print("labcomm.BYTE()"); } break;
case LABCOMM_SHORT: { env.print("labcomm.SHORT()"); } break;
case LABCOMM_INT: { env.print("labcomm.INTEGER()"); } break;
case LABCOMM_LONG: { env.print("labcomm.LONG()"); } break;
case LABCOMM_FLOAT: { env.print("labcomm.FLOAT()"); } break;
case LABCOMM_DOUBLE: { env.print("labcomm.DOUBLE()"); } break;
case LABCOMM_STRING: { env.print("labcomm.STRING()"); } break;
case LABCOMM_SAMPLE: { env.print("labcomm.SAMPLE()"); } break;
case LABCOMM_BOOLEAN: { env.print("labcomm2014.BOOLEAN()"); } break;
case LABCOMM_BYTE: { env.print("labcomm2014.BYTE()"); } break;
case LABCOMM_SHORT: { env.print("labcomm2014.SHORT()"); } break;
case LABCOMM_INT: { env.print("labcomm2014.INTEGER()"); } break;
case LABCOMM_LONG: { env.print("labcomm2014.LONG()"); } break;
case LABCOMM_FLOAT: { env.print("labcomm2014.FLOAT()"); } break;
case LABCOMM_DOUBLE: { env.print("labcomm2014.DOUBLE()"); } break;
case LABCOMM_STRING: { env.print("labcomm2014.STRING()"); } break;
case LABCOMM_SAMPLE: { env.print("labcomm2014.SAMPLE()"); } break;
}
}
public void ArrayType.Python_genSignature(Python_env env) {
env.print("labcomm.array([");
env.print("labcomm2014.array([");
for (int i = 0 ; i < getNumExp() ; i++) {
if (i > 0) { env.print(", "); }
env.print(getExp(i).Python_getValue());
}
env.println("],");
env.indent();
getType().Python_genSignature(env);
getDataType().Python_genSignature(env);
env.print(")");
env.unindent();
}
public void StructType.Python_genSignature(Python_env env) {
env.println("labcomm.struct([");
env.println("labcomm2014.struct([");
env.indent();
for (int i = 0 ; i < getNumField() ; i++) {
if (i > 0) { env.println(","); }
......@@ -191,12 +164,74 @@ aspect PythonTypes {
}
public void VoidType.Python_genSignature(Python_env env) {
env.println("labcomm.struct([])");
env.println("labcomm2014.struct([])");
}
public void Field.Python_genSignature(Python_env env) {
env.print("('" + getName() + "', ");
getType().Python_genSignature(env);
env.print("(");
Python_genIntentions(env);
env.print(", ");
getDataType().Python_genSignature(env);
env.print(")");
}
public void UserType.Python_genTypedef(Python_env env) {
env.println(getName() + ".typedef");
}
public void DataType.Python_genTypedef(Python_env env) {
throw new Error(this.getClass().getName() +
".Python_genTypedef(Python_env env)" +
" not declared");
}
public void PrimType.Python_genTypedef(Python_env env) {
switch (getToken()) {
case LABCOMM_BOOLEAN: { env.print("labcomm2014.BOOLEAN()"); } break;
case LABCOMM_BYTE: { env.print("labcomm2014.BYTE()"); } break;
case LABCOMM_SHORT: { env.print("labcomm2014.SHORT()"); } break;
case LABCOMM_INT: { env.print("labcomm2014.INTEGER()"); } break;
case LABCOMM_LONG: { env.print("labcomm2014.LONG()"); } break;
case LABCOMM_FLOAT: { env.print("labcomm2014.FLOAT()"); } break;
case LABCOMM_DOUBLE: { env.print("labcomm2014.DOUBLE()"); } break;
case LABCOMM_STRING: { env.print("labcomm2014.STRING()"); } break;
case LABCOMM_SAMPLE: { env.print("labcomm2014.SAMPLE()"); } break;
}
}
public void ArrayType.Python_genTypedef(Python_env env) {
env.print("labcomm2014.array([");
for (int i = 0 ; i < getNumExp() ; i++) {
if (i > 0) { env.print(", "); }
env.print(getExp(i).Python_getValue());
}
env.println("],");
env.indent();
getDataType().Python_genTypedef(env);
env.print(")");
env.unindent();
}
public void StructType.Python_genTypedef(Python_env env) {
env.println("labcomm2014.struct([");
env.indent();
for (int i = 0 ; i < getNumField() ; i++) {
if (i > 0) { env.println(","); }
getField(i).Python_genTypedef(env);
}
env.print("])");
env.unindent();
}
public void VoidType.Python_genTypedef(Python_env env) {
env.println("labcomm2014.struct([])");
}
public void Field.Python_genTypedef(Python_env env) {
env.print("(");
Python_genIntentions(env);
env.print(", ");
getDataType().Python_genTypedef(env);
env.print(")");
}
......@@ -204,14 +239,14 @@ aspect PythonTypes {
}
public void TypeDecl.Python_genTypedefListEntry(Python_env env) {
env.println("('" + getName() + "', " + getName() + ".signature),");
env.println(getName() + ",");
}
public void Decl.Python_genSampleListEntry(Python_env env) {
}
public void SampleDecl.Python_genSampleListEntry(Python_env env) {
env.println("('" + getName() + "', " + getName() + ".signature),");
env.println(getName()+ ",");
}
public String Exp.Python_getValue() {
......
......@@ -77,7 +77,7 @@ aspect RAPID_CodeGen {
throw new UnsupportedOperationException();
}
public void Program.RAPID_gen(String file, String prefix, int version)
public void Specification.RAPID_gen(String file, String prefix, int version)
throws IOException
{
PrintStream ps = new PrintStream(new FileOutputStream(new File(file)));
......@@ -85,7 +85,7 @@ aspect RAPID_CodeGen {
RAPID_gen(env);
}
public void Program.RAPID_gen(RAPID_env env)
public void Specification.RAPID_gen(RAPID_env env)
{
for (int i = 0; i < getNumDecl(); i++) {
getDecl(i).RAPID_gen(env);
......@@ -126,7 +126,7 @@ aspect RAPID_CodeGen {
public void SampleDecl.RAPID_gen(RAPID_env env) {
// Add type declarations
String fullName = getType().RAPID_AddType(env, getName());
String fullName = getDataType().RAPID_AddType(env, getName());
// Add signature constants
String sig_len_name = "signature_len_" + getName();
String sig_name = "signature_" + getName();
......@@ -167,7 +167,7 @@ aspect RAPID_CodeGen {
params.add("VAR LabComm_Stream st");
params.add("VAR LabComm_Decoder_Sample s");
stmts.add("VAR " + fullName + " tmp;");
getType().RAPID_AddDecodeInstr(env, stmts, "tmp", "st");
getDataType().RAPID_AddDecodeInstr(env, stmts, "tmp", "st");
stmts.add("% s.handler % tmp;");
env.addProc("Decode_And_Handle_" + getName(), params, stmts);
......@@ -201,11 +201,11 @@ aspect RAPID_CodeGen {
params.add("VAR LabComm_Encoder_Sample s");
params.add("VAR " + fullName + " val");
stmts.add("Encode_Packed st, s.user_id;");
getType().RAPID_AddEncodeInstr(env, stmts, "val", "st");
getDataType().RAPID_AddEncodeInstr(env, stmts, "val", "st");
env.addProc("Encode_" + getName(), params, stmts);
}
public String Type.RAPID_AddType(RAPID_env env, String name) {
public String DataType.RAPID_AddType(RAPID_env env, String name) {
throw new UnsupportedOperationException("RAPID code generation does (currently) not support "+getClass().getSimpleName());
}
......@@ -214,7 +214,7 @@ aspect RAPID_CodeGen {
for (int i = 0; i < getNumField(); i++) {
Field f = getField(i);
components.add(
f.getType().RAPID_AddType(env, name + "_" + f.getName()) +
f.getDataType().RAPID_AddType(env, name + "_" + f.getName()) +
" " + f.getName() + ";");
}
String typeName = env.addRecord(name, components);
......@@ -222,7 +222,7 @@ aspect RAPID_CodeGen {
}
public String FixedArrayType.RAPID_AddType(RAPID_env env, String name) {
String typeName = getType().RAPID_AddType(env, name + "_e");
String typeName = getDataType().RAPID_AddType(env, name + "_e");
if (getNumExp() > 1) {
throw new UnsupportedOperationException("RAPID generation only (currently) supports one-dimensional arrays");
}
......@@ -251,7 +251,7 @@ aspect RAPID_CodeGen {
throw new UnsupportedOperationException("RAPID code generation does not (currently) support "+getName());
}
public void Type.RAPID_AddDecodeInstr(RAPID_env env,
public void DataType.RAPID_AddDecodeInstr(RAPID_env env,
java.util.List<String> instrs,
String var_name, String stream_name) {
throw new UnsupportedOperationException("RAPID code generation does not (currently) support "+getClass().getSimpleName());
......@@ -261,7 +261,7 @@ aspect RAPID_CodeGen {
java.util.List<String> instrs,
String var_name, String stream_name) {
for (int i = 0; i < getNumField(); i++) {
getField(i).getType().RAPID_AddDecodeInstr(env, instrs,
getField(i).getDataType().RAPID_AddDecodeInstr(env, instrs,
var_name + "." + getField(i).getName(), stream_name);
}
}
......@@ -270,7 +270,7 @@ aspect RAPID_CodeGen {
java.util.List<String> instrs,
String var_name, String stream_name) {
for (int i = 1; i <= getExp(0).RAPID_getValue(); i++) {
getType().RAPID_AddDecodeInstr(env, instrs,
getDataType().RAPID_AddDecodeInstr(env, instrs,
var_name + ".e" + i, stream_name);
}
}
......@@ -305,7 +305,7 @@ aspect RAPID_CodeGen {
}
}
public void Type.RAPID_AddEncodeInstr(RAPID_env env,
public void DataType.RAPID_AddEncodeInstr(RAPID_env env,
java.util.List<String> instrs,
String var_name, String stream_name) {
throw new UnsupportedOperationException("RAPID code generation does not (currently) support "+getClass().getSimpleName());
......@@ -315,7 +315,7 @@ aspect RAPID_CodeGen {
java.util.List<String> instrs,
String var_name, String stream_name) {
for (int i = 0; i < getNumField(); i++) {
getField(i).getType().RAPID_AddEncodeInstr(env, instrs,
getField(i).getDataType().RAPID_AddEncodeInstr(env, instrs,
var_name + "." + getField(i).getName(), stream_name);
}
}
......@@ -324,7 +324,7 @@ aspect RAPID_CodeGen {
java.util.List<String> instrs,
String var_name, String stream_name) {
for (int i = 1; i <= getExp(0).RAPID_getValue(); i++) {
getType().RAPID_AddEncodeInstr(env, instrs,
getDataType().RAPID_AddEncodeInstr(env, instrs,
var_name + ".e" + i, stream_name);
}
}
......
/* Temporary aspect with forwarding methods */
aspect Refactoring {
syn int ArrayType.getNumExp() = getDim().getNumExp();
syn Exp ArrayType.getExp(int i) = getDim().getExp(i);
syn String Decl.getName() = getTypeInstance().getName();
syn DataType Decl.getDataType() = getTypeInstance().getDataType();
syn String TypeInstance.getName() = getAnnotations().getName();
public Annotations Annotations.addName(String n) {
//XXX TODO: check if name already exists
addAnnotation(new Intention("",n.getBytes()));
return this;
}
public Field.Field(TypeInstance t) {
this(t.getDataType(), t.getAnnotations());
}
public TypeInstance.TypeInstance(DataType t, String n, Annotations a) {
this(t, a.addName(n));
}
public TypeInstance.TypeInstance(DataType t, String n) {
this(t, new Annotations().addName(n));
System.out.println("WARNING! TypeInstance(DataType, String) ignoring intention list");
}
syn Annotation TypeInstance.getAnnotation(int i) = getAnnotations().getAnnotation(i);
}
......@@ -14,10 +14,9 @@ aspect Signature {
inh Decl Signature.parentDecl();
inh Decl SignatureList.parentDecl();
syn nta Signature Decl.getSignature() {
SignatureList sl = new SignatureList();
genSigLineForDecl(sl, true);
genSigLineForDecl(sl, true, this);
SignatureList fsl = new SignatureList();
flatSignature(fsl);
Signature sig = new Signature();
......@@ -100,6 +99,29 @@ aspect Signature {
return getIntBytes(getData(), version);
}
public void SignatureList.addIntentions(Set<Intention> data, String comment) {
//addString(TypeInstance.getIntentionString(data), comment);
//create IntenionSignatureLine
IntentionSignatureLine line = new IntentionSignatureLine(indent, comment, new List());
//TODO: refactor out creation of sorted list of intentions
java.util.ArrayList<Intention> sorted = new ArrayList(data);
java.util.Collections.sort(sorted, TypeInstance.intentionComp);
for(Intention i : sorted) {
line.addIntention(i);
}
addSignatureLine(line);
}
eq IntentionSignatureLine.getData(int version) {
//String tmpString = TypeInstance.getIntentionString(getIntentions());
byte[] bs = TypeInstance.getIntentionBytes(getIntentions());
return bs;
}
public void SignatureList.addString(String data, String comment) {
addSignatureLine(new StringSignatureLine(indent, comment, data));
}
......@@ -151,86 +173,200 @@ aspect Signature {
}
public void ASTNode.genSigLineForDecl(SignatureList list, boolean decl) {
public void ASTNode.genSigLineForDecl(SignatureList list, boolean decl, ASTNode inst) {
throw new Error(this.getClass().getName() +
".genSigLineForDecl(SignatureList list)" +
" not declared");
}
public void TypeDecl.genSigLineForDecl(SignatureList list, boolean decl) {
//System.out.println("************ TypeDecl.genSigLine("+decl+").... for "+getName());
public String TypeInstance.getIntentionString() {
return getIntentionString(intentions());
}
public static String TypeInstance.getIntentionString(List<Intention> intentions) {
if(intentions==null) return "";
Iterator<Intention> it = intentions.iterator();
return getIntentionString(it);
}
public static String TypeInstance.getIntentionString(Set<Intention> intentions) {
if(intentions==null) return "";
Iterator<Intention> it = intentions.iterator();
return getIntentionString(it);
}
public static String TypeInstance.getIntentionString(Iterator<Intention> it) {
StringBuilder sb = new StringBuilder();
while(it.hasNext()) {
Intention i = it.next();
sb.append(i.toString());
}
return sb.toString();
}
syn byte[] Intention.keyBytes() = getKey().getBytes();
syn byte[] Intention.valBytes() = getValue();
syn byte[] Intention.toByteArray() {
byte[] k = keyBytes();
byte[] v = valBytes();
int klen = Utilities.size_packed32(k.length);
int vlen = Utilities.size_packed32(v.length);
int tlen = k.length + v.length + Utilities.size_packed32(klen) + Utilities.size_packed32(vlen);
//int size = Utilities.size_packed32(tlen)+tlen;
byte result[] = new byte[tlen];
int pos=0;
// pos = Utilities.encodePacked32(tlen, result, pos, Utilities.size_packed32(tlen));
pos = Utilities.encodePacked32(k.length, result, pos, klen);
for(byte kb : k) {
result[pos++] = kb;
}
pos = Utilities.encodePacked32(v.length, result, pos, vlen);
for(byte vb : v) {
result[pos++] = vb;
}
return result;
}
public byte[] TypeInstance.getIntentionBytes() {
return getIntentionBytes(intentions());
}
public static byte[] TypeInstance.getIntentionBytes(List<Intention> intentions) {
if(intentions==null) return new byte[0];
Iterator<Intention> it = intentions.iterator();
return getIntentionBytes(it);
}
public static byte[] TypeInstance.getIntentionBytes(Set<Intention> intentions) {
if(intentions==null) return new byte[0];
Iterator<Intention> it = intentions.iterator();
return getIntentionBytes(it);
}
public static byte[] TypeInstance.getIntentionBytes(Iterator<Intention> it) {
java.util.ArrayList<byte[]> tmp = new java.util.ArrayList<byte[]>();
int tmpLen=0;
int numIntentions=0;
while(it.hasNext()) {
Intention i = it.next();
byte[] bs = i.toByteArray();
tmp.add(bs);
tmpLen+=bs.length;
numIntentions++;
}
byte result[] = new byte[tmpLen + Utilities.size_packed32(numIntentions)];
int pos = 0;
pos = Utilities.encodePacked32(numIntentions, result, 0, Utilities.size_packed32(numIntentions));
for(byte[] bs : tmp) {
for(byte b : bs) {
result[pos++] = b;
}
}
return result;
}
syn Set<Intention> Specification.emptyIntentions() = new HashSet<Intention>();
inh Set<Intention> ASTNode.noIntentions();
eq Specification.getChild(int i).noIntentions() = emptyIntentions();
syn Set<Intention> ASTNode.intentions();
eq ASTNode.intentions() = noIntentions();
eq TypeInstance.intentions() = intentionSet();
public void TypeInstance.genSigLineForDecl(SignatureList list, boolean decl, ASTNode inst) {
// debugAnnotations(this.getName());
// list.addString(inst.getIntentionString(), "intention string");
if(addIntentions()) {
list.addIntentions(intentionSet(), "intentions");
}
getDataType().genSigLineForDecl(list, decl, this);
}
public void TypeDecl.genSigLineForDecl(SignatureList list, boolean decl, ASTNode inst) {
//TODO intent
if(decl){
getType().genSigLineForDecl(list, decl);
getTypeInstance().genSigLineForDecl(list, decl, this);
}else{
list.addTypeRef(this, "//TODO (from list.addTypeRef)");
}
}
public void SampleDecl.genSigLineForDecl(SignatureList list, boolean decl) {
//System.out.println("************ SampleDecl.genSigLine("+decl+").... for "+getName());
getType().genSigLineForDecl(list, decl);
public void SampleDecl.genSigLineForDecl(SignatureList list, boolean decl, ASTNode inst) {
//TODO intent
getTypeInstance().genSigLineForDecl(list, decl, this);
}
public void VoidType.genSigLineForDecl(SignatureList list, boolean decl) {
public void VoidType.genSigLineForDecl(SignatureList list, boolean decl, ASTNode inst) {
list.addInt(LABCOMM_STRUCT, "void");
list.addInt(0, null);
}
public void SampleRefType.genSigLineForDecl(SignatureList list, boolean decl) {
list.addInt(LABCOMM_SAMPLE_REF, "sample");
}
public void PrimType.genSigLineForDecl(SignatureList list, boolean decl) {
// public void SampleRefType.genSigLineForDecl(SignatureList list, boolean decl, ASTNode inst) {
// list.addInt(LABCOMM_SAMPLE_REF, "sample");
// }
public void PrimType.genSigLineForDecl(SignatureList list, boolean decl, ASTNode inst) {
list.addInt(getToken(), null);
}
public void UserType.genSigLineForDecl(SignatureList list, boolean decl) {
if(decl){
//System.out.println("************ UserType.genSigLine("+decl+").... for "+getName());
TypeDecl thet=lookupType(getName());
//System.out.println("************ thet: "+thet.getName() +":"+thet.getType());
thet.genSigLineForDecl(list, decl);
}else{
//System.out.println("************ UserType.genSigLine("+decl+").... for "+getName());
/* For UserType, the decl parameter is ignored, as a UserType
* will always be a TypeRef
*/
public void UserType.genSigLineForDecl(SignatureList list, boolean decl, ASTNode inst) {
TypeDecl thet = lookupType(getName());
// System.out.println("************ thet: "+thet.getName() +":"+thet.getType());
list.addTypeRef(thet, null);
}
}
public void ArrayType.genSigLineForDecl(SignatureList list, boolean decl) {
public void ArrayType.genSigLineForDecl(SignatureList list, boolean decl, ASTNode inst) {
list.addInt(LABCOMM_ARRAY, signatureComment());
list.indent();
list.addInt(getNumExp(), null);
for (int i = 0 ; i < getNumExp() ; i++) {
getExp(i).genSigLineForDecl(list, false);
getExp(i).genSigLineForDecl(list, false, null);
}
getType().genSigLineForDecl(list, false);
getDataType().genSigLineForDecl(list, false, null);
list.unindent();
list.add(null, "}");
}
public void StructType.genSigLineForDecl(SignatureList list, boolean decl) {
public void StructType.genSigLineForDecl(SignatureList list, boolean decl, ASTNode inst) {
list.addInt(LABCOMM_STRUCT, "struct { " + getNumField() + " fields");
list.indent();
list.addInt(getNumField(), null);
for (int i = 0 ; i < getNumField() ; i++) {
getField(i).genSigLineForDecl(list, false);
getField(i).genSigLineForDecl(list, false, inst);
}
list.unindent();
list.add(null, "}");
}
public void Field.genSigLineForDecl(SignatureList list, boolean decl) {
list.addString(getName(), signatureComment());
getType().genSigLineForDecl(list, decl);
}
// public void Field.genSigLineForDecl(SignatureList list, boolean decl, ASTNode inst) {
// //XXX make intention
// list.addString(getName(), signatureComment());
// super.genSigLineForDecl(list, decl, inst);
// //TODOintent
// //getDataType().genSigLineForDecl(list, decl, inst);
// }
public void IntegerLiteral.genSigLineForDecl(SignatureList list, boolean decl) {
public void IntegerLiteral.genSigLineForDecl(SignatureList list, boolean decl, ASTNode inst) {
list.addInt(Integer.parseInt(getValue()), null);
}
public void VariableSize.genSigLineForDecl(SignatureList list, boolean decl) {
public void VariableSize.genSigLineForDecl(SignatureList list, boolean decl, ASTNode inst) {
list.addInt(0, null);
}
}
......@@ -6,31 +6,49 @@ aspect TypeCheck {
// void is not allowed as a field in a struct or an array element
syn boolean Type.isNull();
eq Type.isNull() = false;
syn boolean DataType.isNull();
eq DataType.isNull() = false;
eq VoidType.isNull() = true;
eq UserType.isNull() = decl().isNull();
syn boolean TypeDecl.isNull();
eq TypeDecl.isNull() = getType().isNull();
eq TypeDecl.isNull() = getDataType().isNull();
public void ASTNode.nullTypeCheck() {}
public void Field.nullTypeCheck() {
if(getType().isNull()) {
if(getDataType().isNull()) {
error("field " + getName() + " of struct "+ declName()+ " may not be of type void");
}
}
public void ParseArrayType.nullTypeCheck() {
if(getType().isNull()) {
if(getDataType().isNull()) {
error("elements of array "+declName()+" may not be of type void");
}
}
public void ArrayType.nullTypeCheck() {
if(getType().isNull()) {
if(getDataType().isNull()) {
error("elements of array "+declName()+" may not be of type void");
}
}
}
aspect AnnotationCheck {
refine TypeCheck void ASTNode.typeCheck() {
refined(); // similar to call to super
annotationCheck();
}
public void ASTNode.annotationCheck() {}
public void TypeDecl.annotationCheck() {
Iterator<Intention> it = getTypeInstance().intentions().iterator();;
while(it.hasNext()) {
if(!it.next().getKey().equals("")) {
error("TypeDecl " + getName() + " has intentions. (Not allowed for typedefs)");
}
}
}
}
aspect User_Types {
syn String Type.getTypeName();
eq Type.getTypeName() = getClass().getName();
syn String DataType.getTypeName();
eq DataType.getTypeName() = getClass().getName();
eq PrimType.getTypeName() = getName();
eq UserType.getTypeName() = getName();
syn boolean Type.isUserType();
eq Type.isUserType() = false;
syn boolean DataType.isUserType();
eq DataType.isUserType() = false;
eq UserType.isUserType() = true;
}
......@@ -14,8 +14,8 @@ aspect Type_References {
// The dependencies on other type declarations for a Decl.
coll Set<Decl> Decl.type_dependencies() [new HashSet<Decl>()] with add;
Field contributes ((UserType)getType()).decl()
when parentDecl() != null && getType().isUserType()
Field contributes ((UserType)getDataType()).decl()
when parentDecl() != null && getDataType().isUserType()
to Decl.type_dependencies()
for parentDecl();
......@@ -24,8 +24,8 @@ aspect Type_References {
to Decl.type_dependencies()
for parentDecl();
/*
Field contributes getType().decl()
when parentDecl() != null && getType().isLeafType()
Field contributes getDataType().decl()
when parentDecl() != null && getDataType().isLeafType()
to Decl.type_dependencies()
for parentDecl();
*/
......
aspect Encoding {
public class Utilities {
/* Size of packed32 variable */
public static int size_packed32(long data)
{
long d = data & 0xffffffff;
int result = 0;
int i;
for (i = 0 ; i == 0 || d != 0; i++, d = (d >>> 7)) {
result++;
}
return result;
}
public static int encodePacked32(long value, byte[] buf, int start, int len) {
int pos = start;
byte[] tmp = new byte[5];
long v = value & 0xffffffff;
int i;
for (i = 0 ; i == 0 || v != 0 ; i++, v = (v >> 7)) {
tmp[i] = (byte)(v & 0x7f);
}
if(i != len) {
throw new Error("wrong length, was: "+i+", expected "+len);
}
for (i = i - 1 ; i >= 0 ; i--) {
buf[pos++] = (byte)(tmp[i] | (i!=0?0x80:0x00));
}
return pos;
}
}
}
aspect PrintEnv {
public abstract class PrintEnv {
protected static class Printer {
private final String indentString = " ";
private boolean newline = true; // last print ended with newline
protected PrintStream out;
private Printer printer;
/** dummy constructor motivated by the FilePrinter subclass */
protected Printer() {
this.out = null;
}
public Printer(PrintStream out) {
this.out = out;
}
public void print(PrintEnv env, String s) {
if (newline) {
newline = false;
for (int i = 0 ; i < env.getIndent() ; i++) {
out.print(indentString);
}
}
out.print(s);
}
public void println(PrintEnv env, String s) {
print(env, s);
out.println();
newline = true;
}
public void println(PrintEnv env) {
out.println();
newline = true;
}
public PrintStream getPrintStream() {
return(out);
}
public void close() throws IOException {
//do nothing
}
}
protected static class FilePrinter extends Printer {
private File file;
private IOException exception;
public FilePrinter(PrintStream out) {
super(out);
}
public FilePrinter(File f) {
file = f;
File parentFile = f.getParentFile();
if(parentFile != null) {
parentFile.mkdirs();
}
}
public void close() throws IOException {
if (out != null) {
out.close();
}
if (exception != null) {
throw exception;
}
}
public void checkOpen() {
if (out == null && exception == null) {
try {
out = new PrintStream(new FileOutputStream(file));
} catch (IOException e) {
exception = e;
}
}
}
public void print(PrintEnv env, String s) {
checkOpen();
super.print(env,s);
}
public void println(PrintEnv env, String s) {
checkOpen();
super.println(env, s);
}
}
public final int version; //labcomm version (2006 or 2014)
public final String verStr; // version suffix to append (currently _2006 and empty string)
private Printer printer;
private int indent;
private int depth;
protected PrintEnv(PrintStream out) {
this(new Printer(out));
}
protected PrintEnv(Printer printer) {
this(printer, 2014);
}
protected PrintEnv(Printer printer, int version) {
this(0, printer, version);
}
protected PrintEnv(int indent, Printer printer, int version) {
this(indent, printer, version, 0);
}
protected PrintEnv(int indent, Printer printer, int version, int depth) {
this.version = version;
this.indent = indent;
this.printer = printer;
this.verStr = LabCommVersion.versionString(version);
this.depth = depth;
}
public void close() throws IOException {
printer.close();
}
public PrintStream getPrintStream() {
return printer.getPrintStream();
}
public void indent(int amount) {
indent += amount;
}
public void indent() {
indent(1);
}
public void unindent(int amount) {
indent -= amount;
if (indent < 0) {
throw new Error("Negative indent level");
}
}
public void unindent() {
unindent(1);
}
public void print(String s) {
printer.print(this, s);
}
public void println(String s) {
printer.println(this, s);
}
public void println() {
printer.println(this, "");
}
public void incDepth() {
depth++;
}
public void decDepth() {
if(depth<=0) {
throw new RuntimeException("decDepth() called when depth = "+depth);
}
depth--;
}
public int getDepth() {
return depth;
}
public int getVersion() {
return version;
}
public int getIndent() {
return indent;
}
public Printer getPrinter() {
return printer;
}
public boolean versionHasMetaData() {
return version != 2006;
}
}
}
......@@ -5,7 +5,14 @@ aspect Version {
*/
class LabCommVersion {
public static String versionString(int version) {
return (version == 2006) ? "2006" : "";
switch(version) {
case 2006:
return "2006";
case 2014:
return "2014";
default:
throw new Error("no versionString for version "+version);
}
}
public static boolean versionHasPragma(int version) {
......
import java.util.Vector;
public class LabComm {
public static void main(String[] args) throws Exception {
String ver = null;
Vector<String> outargs = new Vector<String>();
for (String s: args) {
if (s.startsWith("--ver=")) {
String newver = s.substring(6);
if (ver != null && !ver.equals(newver)) {
throw new Exception("Mismatching versions '" + ver +
"' != '" + newver);
}
ver = newver;
} else {
outargs.add(s);
}
}
for (String s: outargs) {
System.out.println(s);
}
if (ver != null && ver.equals("2006")) {
outargs.add(0, "--ver=2006");
se.lth.control.labcomm2006.compiler.LabComm.main(outargs.toArray(
new String[0]));
} else if (ver == null || ver.equals("2014")) {
outargs.add(0, "--ver=2014");
se.lth.control.labcomm2014.compiler.LabComm.main(outargs.toArray(
new String[0]));
}
}
}
.PHONY: all
all: ant-all
.PHONY: test
test: ant-test
.PHONY: clean
clean: ant-clean
rm -f *~
.PHONY: distclean
distclean: clean ant-distclean
.PHONY: ant-%
ant-%:
ant $*
......@@ -6,17 +6,11 @@
gen - generates java files
cleanGen - removes all generated files and their class files
-->
<project name="LabComm" default="build" basedir=".">
<!-- "package" is the directory where generated files will be stored -->
<property name="package" value="AST"/>
<project name="LabComm" default="all" basedir=".">
<!-- "tools" is the directory where generators and libraries are located. -->
<property name="tools" value="tools"/>
<!-- "test" is the directory where tests are located. -->
<property name="test" value="../test"/>
<!-- "jflex" is an ant task class for the scanner generator in JFlex.jar -->
<taskdef name="jflex" classname="JFlex.anttask.JFlexTask" classpath="tools/JFlex.jar"/>
<!-- "beaver" is an ant task class for the parser generator in beaver.jar -->
......@@ -25,63 +19,19 @@
<taskdef name="jastadd" classname="jastadd.JastAddTask"
classpath="tools/jastadd2.jar"/>
<!-- compile sources -->
<target name="test" depends="jar">
<echo message = "Running tests"/>
<exec executable="./run" dir="../test">
<env key="PYTHONPATH" value="../lib/python"/>
<!--arg value="hej"/-->
</exec>
</target>
<!-- remove generated source files and .class files -->
<target name="clean" depends="cleanGen">
<!-- delete all .class files recursively -->
<delete dir="gen"/>
<delete>
<fileset dir="." includes="**/*.class"/>
<fileset dir="." includes="gen"/>
<fileset dir="." includes="labcomm_compiler.jar"/>
</delete>
</target>
<!-- remove generated source files and their .class files -->
<target name="cleanGen">
<delete dir="${package}"/>
</target>
<target name="jar" depends="build">
<jar destfile="labcomm_compiler.jar">
<fileset dir="gen" includes="**/*.class"/>
<zipfileset src="tools/beaver-rt.jar" includes="beaver/*.class"/>
<manifest>
<attribute name="Main-Class" value="LabComm"/>
</manifest>
</jar>
<target name="jastadd.cu">
<uptodate property="jastadd.u">
<srcfiles dir='.'>
<include name="${version}/*.ast"/>
<include name="${version}/*.jrag"/>
<include name="${version}/*.jadd"/>
</srcfiles>
<mapper type="merge"
to="${outdir}/${package_path}/jastadd.uptodate"/>
</uptodate>
</target>
<!-- generate compiler source files -->
<target name="gen_compiler_version">
<local name="package"/>
<local name="package_path"/>
<property name="package" value="se.lth.control.labcomm${version}.compiler"/>
<loadresource property="package_path">
<propertyresource name="package"/>
<filterchain>
<tokenfilter>
<filetokenizer/>
<replacestring from="." to="/"/>
</tokenfilter>
</filterchain>
</loadresource>
<mkdir dir="${outdir}"/>
<!-- create AST node types and weave aspect modules -->
<target name="jastadd" depends="jastadd.cu" unless="jastadd.u">
<echo message = "Running JastAdd"/>
<jastadd package="${package}" rewrite="true" beaver="true"
novisitcheck="true" lazyMaps="true" outdir="${outdir}">
......@@ -91,21 +41,59 @@
<include name="${version}/*.jadd"/>
</fileset>
</jastadd>
<touch file="${outdir}/${package_path}/jastadd.uptodate"/>
</target>
<target name="scanner.cu">
<uptodate property="scanner.u">
<srcfiles dir='.'>
<include name="${version}/LabCommScanner.flex"/>
<!--include name="${tools}/JFlex.jar"/-->
</srcfiles>
<mapper type="merge"
to="${outdir}/${package_path}/LabCommScanner.java"/>
</uptodate>
</target>
<!-- generate the scanner -->
<echo message = "Running jflex -> ${package} ${package_path}"/>
<target name="scanner" depends="scanner.cu" unless="scanner.u">
<echo message = "Generating scanner ${version}"/>
<echo message = "Running jflex -> ${package} ${outdir}/${package_path}"/>
<jflex file="${version}/LabCommScanner.flex"
outdir="${outdir}/${package_path}" nobak="yes"/>
</target>
<target name="parser.1.cu">
<uptodate property="parser.1.u">
<srcfiles dir='.'>
<include name="${version}/*.parser"/>
</srcfiles>
<mapper type="merge"
to="${outdir}/${package_path}/LabCommParser.all"/>
</uptodate>
</target>
<target name="parser.1" depends="parser.1.cu" unless="parser.1.u">
<!-- generate the parser phase 1, create a full .lalr specification
from fragments-->
<echo message = "Running parser phase 1"/>
<echo message = "Joining parser fragments"/>
<concat destfile="${outdir}/${package_path}/LabCommParser.all" binary="true">
<fileset dir=".">
<include name="${version}/*.parser"/>
</fileset>
</concat>
</target>
<target name="parser.2.cu">
<uptodate property="parser.2.u">
<srcfiles dir='.'>
<include name="${outdir}/${package_path}/LabCommParser.all"/>
</srcfiles>
<mapper type="merge"
to="${outdir}/${package_path}/LabCommParser.beaver"/>
</uptodate>
</target>
<target name="parser.2" depends="parser.1, parser.2.cu" unless="parser.2.u">
<!-- generate the parser phase 2, translating .lalr to .beaver -->
<echo message = "translating .lalr to .beaver"/>
<java fork="true" dir="${basedir}"
......@@ -113,41 +101,126 @@
<arg line="${outdir}/${package_path}/LabCommParser.all
${outdir}/${package_path}/LabCommParser.beaver"/>
</java>
</target>
<!-- generate the parser phase 3, translating .beaver to .java -->
<target name="parser.3.cu">
<uptodate property="parser.3.u">
<srcfiles dir='.'>
<include name="${outdir}/${package_path}/LabCommParser.beaver"/>
</srcfiles>
<mapper type="merge"
to="${outdir}/${package_path}/LabCommParser.java"/>
</uptodate>
</target>
<target name="parser.3" depends="parser.2, parser.3.cu" unless="parser.3.u">
<echo message = "translating .beaver to .java"/>
<beaver file="${outdir}/${package_path}/LabCommParser.beaver"
terminalNames="yes" compress="yes" useSwitch="yes"/>
</target>
<echo message = "compiling .java"/>
<!--
<target name="all.version" depends="jastadd, scanner, parser.3">
</target>
<target name="compile.cu">
<uptodate property="compile.u">
<srcfiles dir='.'>
<include name="*.java"/>
<include name="2014/*.java"/>
<include name="${outdir}/**/*.java"/>
</srcfiles>
<mapper type="merge"
to="${outdir}/compile.uptodate"/>
</uptodate>
</target>
<target name="compile" depends="compile.cu" unless="compile.u">
<echo message = "compiling"/>
<javac debug="true" srcdir="." destdir="${outdir}"
includes="${version}/*.java ${outdir}/${package_path}/*.java"
classpath="gen:${tools}/beaver-rt.jar:${tools}/junit.jar"
includes="*.java 2014/*.java gen/**/*.java"
classpath="${outdir}:${tools}/beaver-rt.jar:${tools}/junit.jar"
includeantruntime="false"
fork="true" memoryMaximumSize="128M">
<!--compilerarg value="-Xlint"/-->
</javac>
-->
<touch file="${outdir}/compile.uptodate"/>
</target>
<target name="jar.version.cu">
<uptodate property="jar.version.u">
<srcfiles dir=".">
<include name="${outdir}/${package_path}/*.class"/>
</srcfiles>
<mapper type="merge"
to="labcomm${version}_compiler.jar"/>
</uptodate>
</target>
<target name="jar.version" depends="jar.version.cu" unless="jar.version.u">
<echo message = "Generating labcomm${version}_compiler.jar"/>
<jar destfile="labcomm${version}_compiler.jar">
<fileset dir="${outdir}"
includes="${package_path}/*.class"/>
<zipfileset src="tools/beaver-rt.jar" includes="beaver/*.class"/>
<manifest>
<attribute name="Main-Class"
value="${package}.LabComm"/>
</manifest>
</jar>
</target>
<target name="do.version">
<!-- Wrapper that sets up package and package_path based on ${version} -->
<local name="package"/>
<local name="package_path"/>
<property name="package" value="se.lth.control.labcomm${version}.compiler"/>
<loadresource property="package_path">
<propertyresource name="package"/>
<filterchain>
<tokenfilter>
<filetokenizer/>
<replacestring from="." to="/"/>
</tokenfilter>
</filterchain>
</loadresource>
<echo>${do} ${version} ${outdir}</echo>
<mkdir dir="${outdir}"/>
<antcall target="${do}">
<param name="version" value="${version}"/>
<param name="outdir" value="${outdir}"/>
<param name="package" value="${package}"/>
<param name="package_path" value="${package_path}"/>
</antcall>
</target>
<target name="build">
<antcall target="gen_compiler_version">
<param name="version" value="2006"/>
<target name="all">
<antcall target="do.version">
<param name="do" value="all.version"/>
<param name="version" value="2014"/>
<param name="outdir" value="gen"/>
</antcall>
<antcall target="gen_compiler_version">
<antcall target="compile">
<param name="outdir" value="gen"/>
</antcall>
<antcall target="do.version">
<param name="do" value="jar.version"/>
<param name="version" value="2014"/>
<param name="outdir" value="gen"/>
</antcall>
<echo message = "compiling main"/>
<javac debug="true" srcdir="." destdir="gen"
includes="*.java 2006/*.java 2014/*.java gen/**/*.java"
classpath="gen:${tools}/beaver-rt.jar:${tools}/junit.jar"
includeantruntime="false"
fork="true" memoryMaximumSize="128M">
<!--compilerarg value="-Xlint"/-->
</javac>
</target>
<target name="clean">
<delete dir="gen"/>
</target>
<target name="distclean" depends="clean">
<delete>
<fileset dir="." includes="labcomm*_compiler.jar"/>
</delete>
</target>
<target name="test">
<echo>No tests defined yet</echo>
</target>
</project>
......
#!/bin/sh
java -jar /lib/labcomm2014_compiler.jar "$@"
tech_report.aux
tech_report.bbl
tech_report.blg
tech_report.fdb_latexmk
tech_report.fls
tech_report.log
tech_report.pdf
@TechReport{SunRPC,
author = {},
title = {RFC 1057: Remote Procdeure Call Protocol Specification, Version 2},
institution = {Sun Microsystems},
year = {1988},
OPTkey = {},
OPTtype = {},
OPTnumber = {},
OPTaddress = {},
OPTmonth = {},
OPTnote = {},
OPTannote = {}
}
@Misc{ASN1,
author = {ITU-T},
title = {ASN.1 project web page},
howpublished ={\verb+http://www.itu.int/en/ITU-T/asn1/+},
OPTnote = {}
}
@article{jastadd,
author = {Ekman, Torbj\"{o}rn and Hedin, G\"{o}rel},
title = {The Jastadd Extensible Java Compiler},
journal = {SIGPLAN Not.},
issue_date = {October 2007},
volume = {42},
number = {10},
month = oct,
year = {2007},
issn = {0362-1340},
pages = {1--18},
numpages = {18},
url = {http://doi.acm.org/10.1145/1297105.1297029},
doi = {10.1145/1297105.1297029},
acmid = {1297029},
publisher = {ACM},
address = {New York, NY, USA},
keywords = {OOP, compilers, declarative frameworks, extensibility, java, modularity},
}
% *** en embryo of a technical report describing the labcomm design rationale and implementation ***
\documentclass[a4paper]{article}
\usepackage{listings}
%\usepackage{verbatims}
%\usepackage{todo}
\begin{document}
\title{Labcomm tech report}
\author{Anders Blomdell and Sven Gesteg\aa{}rd Robertz }
\date{embryo of draft, \today}
\date{draft, \today}
\maketitle
......@@ -28,7 +29,10 @@ application sees fit.
The LabComm system is based on a binary protocol and
and a compiler that generates encoder/decoder routines for popular languages
including C, Java, and Python.
including C, Java, and Python. There is also a standard library for the
languages supported by the compiler, containing generic routines for
encoding and decoding types and samples, and interaction with
application code.
The LabComm compiler accepts type and sample declarations in a small language
that is similar to C or Java type-declarations.
......@@ -39,15 +43,21 @@ that is similar to C or Java type-declarations.
%[[http://asn1.org|ASN1]].
LabComm has got it's inspiration from Sun RPC~\cite{SunRPC}
and ASN1~\cite{ANS1}. LabComm is primarily intended for situations
and ASN1~\cite{ASN1}. LabComm is primarily intended for situations
where the overhead of communication has to be kept at a minimum, hence LabComm
only requires one-way communication to operate. The one-way operation also has
the added benefit of making LabComm suitable as a storage format.
Two-way comminication adds complexity, in particular for hand-shaking
during establishment of connections, and the LabComm library provides
support for (for instance) avoiding deadlocks during such hand-shaking.
\pagebreak
\section{Communication model}
LabComm provides self-describing communication channels, by always transmitting
a machine readable description of the data before actual data is sent.
a machine readable description of the data before actual data is
sent\footnote{Sometimes referred to as \emph{in-band} self-describing}.
Therefore, communication on a LabComm channel has two phases
\begin{enumerate}
......@@ -61,9 +71,9 @@ During operation, LabComm will ensure (i.e., monitor) that a communication
channel is fully configured, meaning that both ends agree on what messages that
may be passed over that channel. If an unregistered sample type is sent or
received, the LabComm encoder or decoder will detect it and take action.
In more dynamic applications, it is possible to reconfigure a channel in order to add,
remove, or change the set of registerd sample types.
remove, or change the set of registered sample types. This is discussed
in Section~\ref{sec:reconfig}.
The roles in setting up, and maintaining, the configuration of a channel are as follows:
......@@ -88,11 +98,63 @@ The roles in setting up, and maintaining, the configuration of a channel are as
\item if an unhandled signature is received, pauses the channel and informs the application
\end{itemize}
The fundamental communication model applies to all LabComm channels and
deals with the individual unidirectional channels. In addition to that,
the labcomm libraries support the implementation of higher-level
handshaking and establishment of bidirectional channels both through
means of interacting with the underlying transport layer (e.g., for
marking packets containing signatures as \emph{important}, for
transports that handle resending of dropped packets selectively), or
requesting retransmission of signatures.
In order to be both lean and generic, LabComm does not provide a
complete protocol for establishing and negotiating bidirectional
channels, but does provide support for building such protocols on top
of LabComm.
\subsection{Reconfiguration}
\label{sec:reconfig}
The fundamental communication model can be generalized to the life-cycle
of a concrete communication channel, including the transport layer,
between two end-points. Then, the communication phases are
\begin{enumerate}
\item \emph{Establishment} of communication channel at the transport layer
\item \emph{Configuration} of the LabComm channel (registration of sample
types)
\item \emph{Operation} (transmission of samples)
\end{enumerate}
where it is possible to \emph{reconfigure} a channel by transitioning
back from phase 3 to phase 2. That allows dynamic behaviour, as a sample
type can be added or redefined at run-time. It also facilitates error
handling in two-way channels.
One example of this, more dynamic, view of a labcomm channel is that the
action taken when an unregistered sample is sent or received is to
revert back to the configuration phase and redo the handshaking to
ensure that both sides agree on the set of sample types (i.e.,
signatures) that are currently configured for the channel.
From the system perspective, the LabComm protocol is involved in
phases 2 and 3. The establishement of the \emph{transport-layer}
channels is left to external application code. In the Labcomm library,
that application code is connected to the LabComm routines through
the \emph{reader} and \emph{writer} interfaces,
with default implementations for sockets or file descriptors (i.e.,
files and streams).
\section{The Labcomm language}
The following examples do not cover the entire language
specification (see appendix~\ref{language_grammar}), but might serve as a
gentle introduction to the LabComm language.
The LabComm language is used to describe data types, and from such data
descriptions, the compiler generates code for encoding and decoding
samples. The language is quite similar to C struct declarations, with
some exceptions. We will now introduce the language through a set of
examples.
These examples do not cover the entire language
specification (see appendix~\ref{sec:LanguageGrammar} for the complete
grammar), but serve as a gentle introduction to the LabComm
language covering most common use-cases.
\subsection{Primitive types}
......@@ -107,6 +169,22 @@ gentle introduction to the LabComm language.
sample string a_string;
\end{verbatim}
\subsection{The void type}
There is a type, \verb+void+, which can be used to send
a sample that contains no data.
\begin{verbatim}
typedef void an_empty_type;
sample an_empty_type no_data1;
sample void no_data2;
\end{verbatim}
\verb+void+ type can may not be used as a field in a struct or
the element type of an array.
\subsection{Arrays}
\begin{verbatim}
......@@ -120,7 +198,7 @@ gentle introduction to the LabComm language.
\begin{enumerate}
\item In contrast to C, LabComm supports both fixed and variable (denoted
by \verb+_+) sized arrays.
by~\verb+_+) sized arrays.
\item In contrast to Java, LabComm supports multidimensional arrays and not
only arrays of arrays.
......@@ -136,7 +214,46 @@ only arrays of arrays.
} a_struct;
\end{verbatim}
\section{User defined types}
\subsection{Sample type refereces}
In addition to the primitive types, a sample may contain
a reference to a sample type. References are declared using
the \verb+sample+ keyword.
Examples:
\begin{verbatim}
sample sample a_ref;
sample sample ref_list[4];
sample struct {
sample ref1;
sample ref2;
int x;
int y;
} refs_and_ints;
\end{verbatim}
Sample references are need to be registered on both encoder and decoder
side, using the functions
\begin{verbatim}
int labcomm_decoder_sample_ref_register(
struct labcomm_decoder *decoder\nonumber
const struct labcomm_signature *signature);
int labcomm_encoder_sample_ref_register(
struct labcomm_encoder *encoder\nonumber
const struct labcomm_signature *signature);
\end{verbatim}
The value of an unregistered sample reference will be decoded as \verb+null+.
\subsection{User defined types}
User defined types are declared with the \verb+typedef+ reserved word,
and can then be used in type and sample declarations.
\begin{verbatim}
typedef struct {
......@@ -151,7 +268,8 @@ only arrays of arrays.
The LabComm system consists of a compiler for generating code from the data
descriptions, and libraries providing LabComm communication facilities in,
currently, C, Java, Python, and C\#.
currently, C, Java, Python, C\#, and RAPID\footnote{excluding variable
size samples, as RAPID has limited support for dynamic memory allocation}.
\subsection{The LabComm compiler}
......@@ -160,7 +278,7 @@ The LabComm compiler generates code for the declared samples, including marshall
demarshalling code, in the supported target languages.
The compiler itself is implemented in Java using the JastAdd~\cite{jastadd} compiler compiler.
\pagebreak
\subsection{The LabComm library}
The LabComm libraries contain functionality for the end-to-end transmission
......@@ -222,73 +340,32 @@ But
With the following `example.lc` file:
\begin{verbatim}
sample struct {
int sequence;
struct {
boolean last;
string data;
} line[_];
} log_message;
sample float data;
\end{verbatim}
\lstinputlisting[basicstyle=\footnotesize\ttfamily]{../examples/wiki_example/example.lc}
and this \verb+example_encoder.c+ file
\lstinputlisting[basicstyle=\footnotesize\ttfamily,language=C]{../examples/wiki_example/example_encoder.c}
\begin{verbatim}
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <labcomm_fd_reader.h>
#include <labcomm_fd_writer.h>
#include "example.h"
int main(int argc, char *argv[]) {
int fd;
struct labcomm_encoder *encoder;
int i, j;
fd = open("example.encoded", O_WRONLY|O_CREAT|O_TRUNC, 0644);
encoder = labcomm_encoder_new(labcomm_fd_writer, &fd);
labcomm_encoder_register_example_log_message(encoder);
labcomm_encoder_register_example_data(encoder);
for (i = 0 ; i < argc ; i++) {
example_log_message message;
message.sequence = i + 1;
message.line.n_0 = i;
message.line.a = malloc(message.line.n_0*sizeof(message.line));
for (j = 0 ; j < i ; j++) {
message.line.a[j].last = (j == message.line.n_0 - 1);
message.line.a[j].data = argv[j + 1];
}
labcomm_encode_example_log_message(encoder, &message);
free(message.line.a);
}
for (i = 0 ; i < argc ; i++) {
float f = i;
labcomm_encode_example_data(encoder, &f);
}
}
\end{verbatim}
\newpage
Running \verb+./example_encoder one two+, will yield the following result in example.encoded:
\begin{verbatim}
00000000 02 40 0b 6c 6f 67 5f 6d 65 73 73 61 67 65 11 02 |.@.log_message..|
00000010 08 73 65 71 75 65 6e 63 65 23 04 6c 69 6e 65 10 |.sequence#.line.|
00000020 01 00 11 02 04 6c 61 73 74 20 04 64 61 74 61 27 |.....last .data'|
00000030 02 41 04 64 61 74 61 25 40 00 00 00 01 00 40 00 |.A.data%@.....@.|
00000040 00 00 02 01 01 03 6f 6e 65 40 00 00 00 03 02 00 |......one@......|
00000050 03 6f 6e 65 01 03 74 77 6f 41 00 00 00 00 41 3f |.one..twoA....A?|
00000060 80 00 00 41 40 00 00 00 |...A@...|
00000068
00000000 01 0c 0b 4c 61 62 43 6f 6d 6d 32 30 31 34 02 30 |...LabComm2014.0|
00000010 40 0b 6c 6f 67 5f 6d 65 73 73 61 67 65 22 11 02 |@.log_message"..|
00000020 08 73 65 71 75 65 6e 63 65 23 04 6c 69 6e 65 10 |.sequence#.line.|
00000030 01 00 11 02 04 6c 61 73 74 20 04 64 61 74 61 27 |.....last .data'|
00000040 02 08 41 04 64 61 74 61 01 25 40 04 00 00 00 01 |..A.data.%@.....|
00000050 00 40 09 00 00 00 02 01 01 03 6f 6e 65 40 0e 00 |.@........one@..|
00000060 00 00 03 02 00 03 6f 6e 65 01 03 74 77 6f 41 04 |......one..twoA.|
00000070 00 00 00 00 41 04 3f 80 00 00 41 04 40 00 00 00 |....A.?...A.@...|
00000080
\end{verbatim}
i.e.,
\begin{verbatim}
<sample_decl> <user_id: 0x40> <string: <len: 11> <"log_message">
<version> <length: 12> <string: <len: 11> <"LabComm2014">>
<sample_decl> <length: 48
<user_id: 0x40>
<string: <len: 11> <"log_message">
<signature_length: 34>
<struct_decl:
<number_of_fields: 2>
<string: <len: 8> <"sequence"> <type: <integer_type>>
......@@ -301,10 +378,155 @@ i.e.,
>>
>
>
<sample_decl> <user_id: 0x41>
...
<sample_decl> <length: 8> <user_id: 0x41> <string: <len: 4> <"data">>
<signature_length: 1> <float_type>
<sample_data> <user_id: 40> <length: 4> <packed_sample_data>
<sample_data> <user_id: 40> <length: 9> <packed_sample_data>
<sample_data> <user_id: 40> <length: 14> <packed_sample_data>
\end{verbatim}
\section{Type and sample declarations}
LabComm has two constructs for declaring sample types, \emph{sample
declarations} and \emph{type declarations}. A sample declaration is used
for the concrete sample types that may be transmitted, and is always
encoded as a \emph{flattened} signature. That means that a sample
containing user types, like
\begin{verbatim}
typedef struct {
int x;
int y;
} point;
sample struct {
point start;
point end;
} line;
\end{verbatim}
is flattened to
\begin{verbatim}
sample struct {
struct {
int x;
int y;
} start;
struct {
int x;
int y;
} end;
} line;
\end{verbatim}
Sample declarations are always sent, and is the fundamental identity of
a type in LabComm.
Type declarations is the hierarchical counterpart to sample
declarations: here, fields of user types are encoded as a reference to
the type instead of being flattened. As the flattened sample decl is the
fundamental identity of a type, type declarations can be regarded as
meta-data, describing the internal structure of a sample. They are
intended to be read by higher-level software and human system developers
and integrators.
Sample declarations and type declarations have separate name-spaces in
the sense that the numbers assigned to them by a labcomm encoder
come from two independent number series. To identify which
\verb+TYPE_DECL+ a particular \verb+SAMPLE_DECL+ corresponds to, the
\verb+TYPE_BINDING+ packet is used.
For sample types that do not depend on any typedefs, no \verb+TYPE_DECL+
is sent, and the \verb+TYPE_BINDING+ binds the sample id to the special
value zero to indicate that the type definition is identical to the
flattened signature.
\subsection{Example}
The labcomm declaration
\lstinputlisting[basicstyle=\footnotesize\ttfamily]{../examples/user_types/test.lc}
can be is encoded as
\begin{lstlisting}[basicstyle=\footnotesize\ttfamily]
TYPE_DECL 0x40 "coord" <int> val
TYPE_DECL 0x41 "point" <struct> <2 fields>
"x" <type: 0x40>
"y" <type: 0x40>
TYPE_DECL 0x42 "line" <struct> <2 fields>
"start" <type: 0x41>
"end" <type: 0x41>
TYPE_DECL 0x43 "foo" <struct> <3 fields>
"a" <int>
"b" <int>
"c" <boolean>
TYPE_DECL 0x44 "twolines" <struct> <3 fields>
"l1" <type:0x42>
"l2" <type:0x42>
"f" <type:0x43>
SAMPLE_DECL 0x40 "twolines" <flat signature>
TYPE_BINDING 0x40 0x44
\end{lstlisting}
Note that the id 0x40 is used both for the \verb+TYPE_DECL+ of
\verb+coord+ and the \verb+SAMPLE_DECL+ of \verb+twoline+, and that the
\verb+TYPE_BINDING+ binds the sample id \verb+0x40+ to the type id
\verb+0x44+.
\subsection{Run-time behaviour}
When a sample type is registered on an encoder, a \verb+SAMPLE_DECL+
(i.e., the flat signature) is always generated on that encoder channel.
If the sample depends on user types (i.e., typedefs), \verb+TYPE_DECL+
packets are encoded, recursively, for the dependent types and a
corresponding \verb+TYPE_BINDING+ is encoded.
If a \verb+TYPE_DECL+ is included via multiple sample types, or
dependency paths, an encoder may choose to only encode it once, but is
not required to do so. However, if multiple \verb+TYPE_DECL+ packets are
sent for the same \verb+typedef+, the encoder must use the same
\verb+type_id+.
\subsection{Decoding in-band type descriptions}
In LabComm, the in-band data descriptions are equivalent to \footnote{in
the sense that they contain all information needed to recreate} the data
description source (i.e., the ``.lc-file'').
%
As the type declarations (a.k.a. \emph{signatures}) are written before
sample data on every channel, they can be used to interpret data with
an unknown (by the receiver) format.
The libraries provide functionality to subscribe to (i.e., register a
\emph{handler} for) sample and type declarations.
On the low level, the handler receives an instance of the signature
data structure corresponding to the received declaration.
For higher-level processing, the Java library provides the
\verb+ASTbuilder+ class, which builds an abstract syntax tree in
the internal representation of the LabComm compiler.
That enables the user to use the complete functionality of the
LabComm compiler, e.g. code generation, on declarations received in a
LabComm stream.
In combination with on-the-fly compilation and class-loading (or
linking) that makes it possible to dynamically create handlers for
previously unknown data types. Thereby, it enables dynamic configuration
of LabComm endpoints in static languages without the overhead of
interpreting signatures (at the one-time cost of code generation and
compilation).
\section{Ideas/Discussion}:
The labcomm language is more expressive than its target languages regarding data types.
......@@ -317,28 +539,261 @@ Java primitive types. However, it is unlikely that the entire range is actually
way of supporting the common cases is to include run-time checks for overflow in the Java encoders
and decoders.
\section{Related work}
Two in-band self-descibing communication protocols are Apache
Avro\cite{avro} and EDN, the extensible data notation developed for
Clojure and Datomic\cite{EDN}.
EDN encodes \emph{values} as UTF-8 strings. The documentation says
``edn is a system for the conveyance of values. It is not a type system,
and has no schemas.'' That said, it is \emph{extensible} in the sense
that it has a special \emph{dispatch charachter}, \verb+#+, which can
be used to add a \emph{tag} to a value. A tag indicates a semantic
interpretation of a value, and that allows the reader to support
handlers for specific tags, enabling functionality similar to that of
labcomm.
\subsection{Apache Avro}
Apache Avro is similar to LabComm in that it has a textual language
for declaring data, a binary protocol for transmitting data, and code
generation for several languages.
Avro is a larger system, including RPC \emph{protocols}, support for
using different \emph{codecs} for data compression, and \emph{schema
resolution} to support handling schema evolution and transparent
interoperability between different versions of a schema.
\subsubsection*{Data types}
In the table, the Avro type names are listed, and matched to the
corresponding LabComm type:
\begin{tabular}{|l|c|c|}
\hline
Type & Labcomm & Avro \\
\hline Primitive types \\ \hline
int & 4 bytes & varint \\
long & 8 bytes & varint \\
float & 4 bytes & 4 bytes \\
long & 8 bytes & 8 bytes \\
string & varint + utf8[] & varint + utf8[] \\
bytes & varint + byte[] & varint + byte[]\\
\hline Complex types \\ \hline
struct/record & concat of fields & concat of fields \\
arrays & varIdx[] : elements & block[] \\
map & n/a & block[] \\
union & n/a & (varint idx) : value \\
fixed & byte[n] & the number of bytes declared in
the schema\\
\hline
\end{tabular}
where
\begin{verbatim}
block ::= (varint count) : elem[count] [*1]
count == 0 --> no more blocks
[*1] for arrays, count == 0 --> end of array
if count < 0, there are |count| elements
preceded by a varint block_size to allow
fast skipping
\end{verbatim}
In maps, keys are strings, and values according to the schema.
In unions, the index indicates the kind of value and the
value is encoded according to the schema.
Note that the Avro data type \verb+bytes+ corresponds to the
LabComm declaration \verb+byte[_]+, i.e. a varaible length byte array.
\subsubsection*{the wire protocol}
\begin{tabular}{|l|c|c|}
\hline
What & LabComm & Avro \\ \hline
Data description & Binary signature & JSON schema \\
Signature sent only once pre connection& posible & possible \\
Signature sent with each sample & possible & possible \\
Data encoding & binary & binary \\
\hline
\end{tabular}
Both avro and labcomm use varints when encoding data, similar in that
they both send a sequence of bytes containing 7 bit chunks (with the
eight bit signalling more chunks to come), but they differ in range,
endianness and signedness.
\begin{verbatim}
LabComm Avro
unsigned 32 bit signed zig-zag coding
most significant chunk least significant chunk
first first
0 -> 00 0 -> 00
1 -> 01 -1 -> 01
2 -> 02 1 -> 02
... -2 -> 03
2 -> 04
...
127 -> 7f -64 -> 7f
128 -> 81 00 64 -> 80 01
129 -> 81 01 -65 -> 81 01
130 -> 81 02 65 -> 82 01
... ...
\end{verbatim}
\paragraph{Avro Object Container Files} can be seen as a counterpart
to a LabComm channel:
Avro includes a simple object container file format. A file has a
schema, and all objects stored in the file must be written according to
that schema, using binary encoding. Objects are stored in blocks that
may be compressed. Syncronization markers are used between blocks to
permit efficient splitting of files, and enable detection of
corrupt blocks.
The major difference is the sync markers that LabComm does not have, as
LabComm assumes that, while the transport may drop packets, there will
be no bit errors in a received packet. If data integrity is required,
that is delegated to the reader and writer for the particular transport.
\subsubsection{Representation of hierarchical data types}
For a type that contains fields of other user types, like
\begin{verbatim}
typedef struct {
int x;
int y;
} Point;
sample struct {
Point start;
Point end;
} line;
\end{verbatim}
LabComm encodes both the flattened signature and the
typedef which allows the hierarchical type structure to be
reconstructed.
%
The avro encoding is quite similar.
The \verb+Line+ example, corresponds to the two schemas
\begin{verbatim}
{"namespace": "example.avro",
"type": "record",
"name": "Point",
"fields": [
{"name": "x", "type": "int"},
{"name": "y", "type": "int"}
]
}
\end{verbatim}
and
\begin{verbatim}
{"namespace": "example.avro",
"type": "record",
"name": "Line",
"fields": [
{"name": "start", "type": "Point"},
{"name": "end", "type": "Point"}
]
}
\end{verbatim}
which is encoded in an Object Container File as
\begin{verbatim}
{"type":"record",
"name":"Line",
"namespace":"example.avro",
"fields":[{"name":"start",
"type":{"type":"record",
"name":"Point",
"fields":[{"name":"x","type":"int"},
{"name":"y","type":"int"}]}},
{"name":"end",
"type":"Point"}
]
}
\end{verbatim}
\subsubsection{Fetures not in LabComm}
Avro has a set of features with no counterpart in LabComm. They include
\paragraph{Codecs.}
Avro has multiple codecs (for compression of the data):
\begin{verbatim}
Required Codecs:
- null : The "null" codec simply passes through data uncompressed.
- deflate : The "deflate" codec writes the data block using the deflate
algorithm as specified in RFC 1951, and typically implemented using the
zlib library. Note that this format (unlike the "zlib format" in RFC
1950) does not have a checksum.
Optional Codecs
- snappy: The "snappy" codec uses Google's Snappy compression library. Each
compressed block is followed by the 4-byte, big-endian CRC32 checksum of
the uncompressed data in the block.
\end{verbatim}
\paragraph{Schema Resolution.} The main objective of LabComm is to
ensure correct operation at run-time. Therefore, a LabComm decoder
requires the signatures for each handled sample to match exactly.
Avro, on the other hand, supports the evolution of schemas and
provides support for reading data where the ordering of fields
differ (but names and types are the same), numerical types differ
but can be
\emph{promoted} (E.g., \verb+int+ can be promoted to \verb+long+,
\verb+float+, or \verb+double+.), and record fields have been added
or removed (but are nullable or have default values).
\paragraph{Schema fingerprints.} Avro defines a \emph{Parsing
Canonical Form} to define when two JSON schemas are ``the same''.
To reduce the overhead when, e.g., tagging data with the schema
there is support for creating a \emph{fingerprint} using 64/128/256
bit hashing, in combination with a centralized repository for
fingerprint/schema pairs.
\bibliography{refs}{}
\bibliographystyle{plain}
\appendix
\newpage
\section{The LabComm language}
\label{sec:LanguageGrammar}
\subsection{Abstract syntax}
\begin{verbatim}
Program ::= Decl*;
Specification ::= Decl*;
abstract Decl ::= Type <Name:String>;
abstract Decl ::= DataType <Name:String>;
TypeDecl : Decl;
SampleDecl : Decl;
Field ::= Type <Name:String>;
Field ::= DataType <Name:String>;
abstract Type;
VoidType : Type;
PrimType : Type ::= <Name:String> <Token:int>;
UserType : Type ::= <Name:String>;
StructType : Type ::= Field*;
ParseArrayType : Type ::= Type Dim*;
abstract ArrayType : Type ::= Type Exp*;
abstract DataType;
VoidType : DataType;
SampleRefType : DataType;
PrimType : DataType ::= <Name:String> <Token:int>;
UserType : DataType ::= <Name:String>;
StructType : DataType ::= Field*;
ParseArrayType : DataType ::= DataType Dim*;
abstract ArrayType : DataType ::= DataType Exp*;
VariableArrayType : ArrayType;
FixedArrayType : ArrayType;
......@@ -353,39 +808,27 @@ VariableSize : Exp;
\section{The LabComm protocol}
\label{sec:ProtocolGrammar}
Each LabComm2014 packet has the layout
\begin{verbatim}
<packet> := ( <type_decl> | <sample_decl> | <sample_data> )*
<type_decl> := 0x01 ''(packed)'' <user_id> <string> <type>
<sample_decl> := 0x02 ''(packed)''<user_id> <string> <type>
<user_id> := 0x40..0xffffffff ''(packed)''
<string> := <string_length> <char>*
<string_length> := 0x00..0xffffffff ''(packed)''
<char> := any UTF-8 char
<type> := ( <basic_type> | <user_id> | <array_decl> | <struct_decl> )
<basic_type> := ( <boolean_type> | <byte_type> | <short_type> |
<integer_type> | <long_type> | <float_type> |
<double_type> | <string_type> )
<boolean_type> := 0x20 ''(packed)''
<byte_type> := 0x21 ''(packed)''
<short_type> := 0x22 ''(packed)''
<integer_type> := 0x23 ''(packed)''
<long_type> := 0x24 ''(packed)''
<float_type> := 0x25 ''(packed)''
<double_type> := 0x26 ''(packed)''
<string_type> := 0x27 ''(packed)''
<array_decl> := 0x10 ''(packed)'' <number_of_indices> <indices> <type>
<number_of_indices> := 0x00..0xffffffff ''(packed)''
<indices> := ( <variable_index> | <fixed_index> )*
<variable_index> := 0x00 ''(packed)''
<fixed_index> := 0x01..0xffffffff ''(packed)''
<struct_decl> := 0x11 ''(packed)'' <number_of_fields> <field>*
<number_of_fields> := 0x00..0xffffffff ''(packed)''
<field> := <string> <type>
<sample_data> := <user_id> <packed_sample_data>
<packed_sample_data> := is sent in network order, sizes are as follows:
<id> <length> <data...>
\end{verbatim}
where \verb+length+ is the number of bytes of the \verb+data+ part
(i.e., excluding the \verb+id+ and \verb+length+ fields), and
the \verb+id+ gives the layout of the \verb+data+ part as defined
in \ref{sec:ConcreteGrammar}
\subsection{Data encoding}
LabComm primitive types are encoded as fixed width values, sent in
network order. Type fields, user IDs, number of indices and lengths are
sent in a variable length (\emph{varint}) form. A varint integer value
is sent as a sequence of bytes where the lower seven bits contain a
chunk of the actual number and the high bit indicates if more chunks
follow. The sequence of chunks are sent with the least significant chunk
first.
The built-in data types are encoded as follows:
\begin{lstlisting}[basicstyle=\footnotesize\ttfamily]
||Type ||Encoding/Size ||
||---------------||------------------------------------------------------||
||-----------||---------------------------------------------------||
||boolean || 8 bits ||
||byte || 8 bits ||
||short || 16 bits ||
......@@ -393,18 +836,76 @@ VariableSize : Exp;
||long || 64 bits ||
||float || 32 bits ||
||double || 64 bits ||
||string || length ''(packed)'', followed by UTF8 encoded string ||
||array || each variable index ''(packed)'', ||
||sample_ref || 32 bits ||
||string || length (varint), followed by UTF8 encoded string ||
||array || each variable index (varint), ||
|| || followed by encoded elements ||
||struct || concatenation of encoding of each element ||
|| || in declaration order ||
\end{verbatim}
Type fields, user IDs, number of indices and lengths are sent in a packed, or
variable length, form. An integer is sent as a sequence of bytes where the
lower seven bits contain a chunk of the actual number and the high bit
indicates if more chunks follow. The sequence of chunks are sent with the least
significant chunk first. (The numbers encoded in this form are indicated above
with \textit{(packed)}.)
\end{lstlisting}
\pagebreak
\subsection{Protocol grammar}
\label{sec:ConcreteGrammar}
\begin{lstlisting}[basicstyle=\footnotesize\ttfamily]
<packet> := <id> <length> ( <version> |
<type_decl> |
<sample_decl> |
<sample_ref> |
<type_binding> |
<sample_data> )
<version> := <string>
<sample_decl> := <sample_id> <string> <signature>
<sample_ref> := <sample_id> <string> <signature>
<type_decl> := <type_id> <string> <signature>
<type_binding> := <sample_id> <type_id>
<user_id> := 0x40..0xffffffff
<sample_id> : <user_id>
<type_id> : <user_id>
<string> := <string_length> <char>*
<string_length> := 0x00..0xffffffff
<char> := any UTF-8 char
<signature> := <length> <type>
<type> := <length> ( <basic_type> |
<array_decl> |
<struct_decl> |
<type_id> )
<basic_type> := ( <void_type> | <boolean_type> | <byte_type> | <short_type> |
<integer_type> | <long_type> | <float_type> |
<double_type> | <string_type> | <sample_ref_type>)
<void_type> := <struct_decl> 0 //void is encoded as empty struct
<boolean_type> := 0x20
<byte_type> := 0x21
<short_type> := 0x22
<integer_type> := 0x23
<long_type> := 0x24
<float_type> := 0x25
<double_type> := 0x26
<string_type> := 0x27
<sample_ref_type> := 0x28
<array_decl> := 0x10 <nbr_of_indices> <indices> <type>
<nbr_of_indices> := 0x00..0xffffffff
<indices> := ( <variable_index> | <fixed_index> )*
<variable_index> := 0x00
<fixed_index> := 0x01..0xffffffff
<struct_decl> := 0x11 <nbr_of_fields> <field>*
<nbr_of_fields> := 0x00..0xffffffff
<field> := <string> <type>
<sample_data> := packed sample data sent in network order, with
primitive type elements encoded according to
the sizes above
\end{lstlisting}
where the \verb+<id>+ in \verb+<packet>+ signals the type of payload,
and may be either a \verb+<sample_id>+ or a system packet id.
The labcomm sytem packet ids are:
\begin{lstlisting}[basicstyle=\footnotesize\ttfamily]
version: 0x01
sample_decl: 0x02
sample_ref: 0x03
type_decl: 0x04
type_binding: 0x05
\end{lstlisting}
Note that since the signature transmitted in a \verb+<sample_def>+ is
flattened, the \verb+<type>+ transmitted in a \verb+<sample_def>+ may
not contain any \verb+<type_id>+ fields.
\end{document}
......@@ -7,10 +7,18 @@ all:
echo More to be done...
$(MAKE) -C twoway all
UNAME_S=$(shell uname -s)
.PHONY: test
test:
echo More to be done...
ifeq ($(UNAME_S),Darwin)
$(MAKE) -C user_types all
else
cd simple ; sh compile.sh && sh run.sh
$(MAKE) -C wiki_example test
$(MAKE) -C user_types all
endif
$(MAKE) -C duck_typing test
$(MAKE) -C twoway test
......
gen
LABCOMM_JAR=../../compiler/labcomm_compiler.jar
LABCOMM_JAR=../../compiler/labcomm2014_compiler.jar
LABCOMM=java -jar $(LABCOMM_JAR)
all: gen/animal.py
......