Commit 05c5fb26 authored by Anders Blomdell's avatar Anders Blomdell
Browse files

A version of labcomm2006 that is hopefully working after some major

lobotomy.
parent 1f17be8a
SUBDIRS=compiler lib test examples packaging SUBDIRS=compiler lib test examples packaging
export LABCOMM_JAR=$(shell pwd)/compiler/labcomm_compiler.jar export LABCOMM_JAR=$(shell pwd)/compiler/labcomm2006_compiler.jar
export LABCOMM=java -jar $(LABCOMM_JAR) export LABCOMM=java -jar $(LABCOMM_JAR)
.PHONY: all .PHONY: all
......
gen gen
labcomm2006_compiler.jar labcomm2006_compiler.jar
labcomm2014_compiler.jar
labcomm_compiler.jar
...@@ -8,8 +8,6 @@ aspect CS_CodeGenEnv { ...@@ -8,8 +8,6 @@ aspect CS_CodeGenEnv {
public class CS_env { public class CS_env {
public final int version;
public final String verStr;
private int indent; private int indent;
private int depth; private int depth;
private CS_printer printer; private CS_printer printer;
...@@ -77,19 +75,17 @@ aspect CS_CodeGenEnv { ...@@ -77,19 +75,17 @@ aspect CS_CodeGenEnv {
} }
} }
private CS_env(int indent, CS_printer printer, int version) { private CS_env(int indent, CS_printer printer) {
this.version = version;
this.indent = indent; this.indent = indent;
this.printer = printer; this.printer = printer;
this.verStr = LabCommVersion.versionString(version);
} }
public CS_env(File f, int version) { public CS_env(File f) {
this(0, new CS_printer(f), version); this(0, new CS_printer(f));
} }
public CS_env(PrintStream out, int version) { public CS_env(PrintStream out) {
this(0, new CS_printer(out), version); this(0, new CS_printer(out));
} }
public void close() throws IOException { public void close() throws IOException {
...@@ -202,9 +198,9 @@ aspect CS_Void { ...@@ -202,9 +198,9 @@ aspect CS_Void {
aspect CS_CodeGen { aspect CS_CodeGen {
public void Program.CS_gen(String file, public void Program.CS_gen(String file,
String namespace, int version) throws IOException { String namespace) throws IOException {
// Registration class // Registration class
CS_env env = new CS_env(new File(file), version); CS_env env = new CS_env(new File(file));
if (namespace != null && namespace.length() > 0) { if (namespace != null && namespace.length() > 0) {
env.println("namespace " + namespace + "{"); env.println("namespace " + namespace + "{");
env.indent(); env.indent();
...@@ -244,7 +240,6 @@ aspect CS_Register { ...@@ -244,7 +240,6 @@ aspect CS_Register {
} }
public void TypeDecl.CS_emitTypeRegister(CS_env env) { public void TypeDecl.CS_emitTypeRegister(CS_env env) {
// TODO
} }
} }
...@@ -258,45 +253,9 @@ aspect CS_Class { ...@@ -258,45 +253,9 @@ aspect CS_Class {
} }
public void Decl.CS_emitDeclPP(CS_env env){ public void Decl.CS_emitDeclPP(CS_env env){
env.println("/* "); env.println("/* ");
pp(env.getPrintStream()); pp(env.getPrintStream());
env.println("*/");
CS_emitUserTypeDeps(env,null,false);
CS_emitUserTypeRefs(env,null,false);
env.println("*/");
}
public void Decl.CS_emitUserTypeDeps(CS_env env, String via, boolean outputCode){
Iterator<Decl> it = type_dependencies().iterator();
while(it.hasNext()) {
Decl t = it.next();
t.CS_emitUserTypeDeps(env, t.getName(), outputCode);
if( outputCode && t.getType().isUserType() ) {
env.println(t.getName()+".register(e);");
} else { // Just output a comment
String refpath = (via == null) ? "directly" : "indirectly via "+via;
env.println(" //Depends ("+refpath+") on "+t.getName() );
}
}
}
public void Decl.CS_emitUserTypeRefs(CS_env env, String via, boolean outputCode){
if( isReferenced() ) {
Iterator<Decl> it = type_references().iterator();
while(it.hasNext()) {
Decl t = it.next();
t.CS_emitUserTypeRefs(env, t.getName(), outputCode);
if(outputCode) {
env.println(t.getName()+".register(e);");
} else { // Just output a comment
String refpath = (via == null) ? "directly" : "indirectly via "+via;
env.println(" //Is referenced ("+refpath+") by "+t.getName() );
}
}
}
} }
public void Decl.CS_emitRegisterEncoder(CS_env env) { public void Decl.CS_emitRegisterEncoder(CS_env env) {
...@@ -305,11 +264,6 @@ aspect CS_Class { ...@@ -305,11 +264,6 @@ aspect CS_Class {
env.println("e.register(dispatcher);"); env.println("e.register(dispatcher);");
env.unindent(); env.unindent();
env.println("}"); env.println("}");
env.println("public static void registerSampleRef(Encoder e) {");
env.indent();
env.println("e.registerSampleRef(dispatcher);");
env.unindent();
env.println("}");
env.println(); env.println();
} }
...@@ -320,12 +274,6 @@ aspect CS_Class { ...@@ -320,12 +274,6 @@ aspect CS_Class {
env.unindent(); env.unindent();
env.println("}"); env.println("}");
env.println(); env.println();
env.println("public static void registerSampleRef(Decoder d) {");
env.indent();
env.println("d.registerSampleRef(dispatcher);");
env.unindent();
env.println("}");
env.println();
} }
public void TypeDecl.CS_emitClass(CS_env env) { public void TypeDecl.CS_emitClass(CS_env env) {
...@@ -339,11 +287,6 @@ aspect CS_Class { ...@@ -339,11 +287,6 @@ aspect CS_Class {
env.println(); env.println();
env.indent(); env.indent();
getType().CS_emitInstance(env); getType().CS_emitInstance(env);
if( isReferenced()) {
CS_emitRegisterEncoder(env);
CS_emitDispatcher(env,false);
}
CS_emitSignature(env);
CS_emitEncoder(env); CS_emitEncoder(env);
CS_emitDecoder(env); CS_emitDecoder(env);
env.unindent(); env.unindent();
...@@ -381,22 +324,18 @@ aspect CS_Class { ...@@ -381,22 +324,18 @@ aspect CS_Class {
public void Decl.CS_emitSignature(CS_env env) { public void Decl.CS_emitSignature(CS_env env) {
CS_emitFlatSignature(env); CS_emitFlatSignature(env);
// if(isReferenced() || isSampleDecl()){
// Signature signature = getSignature();
// signature.CS_emitSignature(env, !isSampleDecl());
// }
} }
public void Decl.CS_emitFlatSignature(CS_env env){ public void Decl.CS_emitFlatSignature(CS_env env){
env.println("private static byte[] signature = new byte[] {"); env.println("private static byte[] signature = new byte[] {");
env.indent(); env.indent();
SignatureList signature = flatSignature(env.version); SignatureList signature = flatSignature(2006);
for (int i = 0 ; i < signature.size() ; i++) { for (int i = 0 ; i < signature.size() ; i++) {
String comment = signature.getComment(i); String comment = signature.getComment(i);
if (comment != null) { if (comment != null) {
env.println(signature.getIndent(i) + "// " + comment); env.println(signature.getIndent(i) + "// " + comment);
} }
byte[] data = signature.getData(i, env.version); byte[] data = signature.getData(i, 2006);
if (data != null) { if (data != null) {
env.print(signature.getIndent(i)); env.print(signature.getIndent(i));
for (int j = 0 ; j < data.length ; j++) { for (int j = 0 ; j < data.length ; j++) {
...@@ -413,7 +352,7 @@ aspect CS_Class { ...@@ -413,7 +352,7 @@ aspect CS_Class {
//XXX TODO: refactor: split into a static class ("TypeDefSingleton"?)and a (smaller) dispatcher //XXX TODO: refactor: split into a static class ("TypeDefSingleton"?)and a (smaller) dispatcher
public void Decl.CS_emitDispatcher(CS_env env, boolean isSample) { public void Decl.CS_emitDispatcher(CS_env env, boolean isSample) {
String genericStr = ""; //env.versionHasMetaData()?"<"+getName()+">":""; String genericStr = "";
env.println("private static Dispatcher dispatcher = new Dispatcher();"); env.println("private static Dispatcher dispatcher = new Dispatcher();");
env.println(); env.println();
env.println("public SampleDispatcher getDispatcher() {"); env.println("public SampleDispatcher getDispatcher() {");
...@@ -467,8 +406,8 @@ aspect CS_Class { ...@@ -467,8 +406,8 @@ aspect CS_Class {
// env.println(); // env.println();
// env.println("public void encodeSignatureMetadata(Encoder e, int index){"); // env.println("public void encodeSignatureMetadata(Encoder e, int index){");
// env.indent(); // env.indent();
// env.println("e.encodePacked32(Constant.TYPE_DEF);"); // env.println("e.encodeInt(Constant.TYPE_DEF);");
// env.println("e.encodePacked32(index);"); // env.println("e.encodeInt(index);");
// env.println("e.encodeString(getName());"); // env.println("e.encodeString(getName());");
// env.println("emitSignature(e);"); // env.println("emitSignature(e);");
// env.unindent(); // env.unindent();
...@@ -553,7 +492,6 @@ aspect CS_Class { ...@@ -553,7 +492,6 @@ aspect CS_Class {
case LABCOMM_FLOAT: { env.print("e.encodeFloat"); } break; case LABCOMM_FLOAT: { env.print("e.encodeFloat"); } break;
case LABCOMM_DOUBLE: { env.print("e.encodeDouble"); } break; case LABCOMM_DOUBLE: { env.print("e.encodeDouble"); } break;
case LABCOMM_STRING: { env.print("e.encodeString"); } break; case LABCOMM_STRING: { env.print("e.encodeString"); } break;
case LABCOMM_SAMPLE: { env.print("e.encodeSampleRef"); } break;
} }
env.println("(" + name + ");"); env.println("(" + name + ");");
} }
...@@ -593,7 +531,7 @@ aspect CS_Class { ...@@ -593,7 +531,7 @@ aspect CS_Class {
} }
public String VariableSize.CS_emitEncoder(CS_env env, String name) { public String VariableSize.CS_emitEncoder(CS_env env, String name) {
env.println("e.encodePacked32(" + name + ");"); env.println("e.encodeInt(" + name + ");");
return name; return name;
} }
...@@ -648,7 +586,6 @@ aspect CS_Class { ...@@ -648,7 +586,6 @@ aspect CS_Class {
case LABCOMM_FLOAT: { env.println("d.decodeFloat();"); } break; case LABCOMM_FLOAT: { env.println("d.decodeFloat();"); } break;
case LABCOMM_DOUBLE: { env.println("d.decodeDouble();"); } break; case LABCOMM_DOUBLE: { env.println("d.decodeDouble();"); } break;
case LABCOMM_STRING: { env.println("d.decodeString();"); } break; case LABCOMM_STRING: { env.println("d.decodeString();"); } break;
case LABCOMM_SAMPLE: { env.println("d.decodeSampleRef();"); } break;
default: { default: {
throw new Error("PrimType.CS_emitDecoder(CS_env env, String name)" + throw new Error("PrimType.CS_emitDecoder(CS_env env, String name)" +
" unknown token type"); " unknown token type");
...@@ -706,7 +643,7 @@ aspect CS_Class { ...@@ -706,7 +643,7 @@ aspect CS_Class {
} }
public void VariableSize.CS_emitDecoder(CS_env env) { public void VariableSize.CS_emitDecoder(CS_env env) {
env.print("d.decodePacked32()"); env.print("d.decodeInt()");
} }
public void StructType.CS_emitDecoder(CS_env env, String name) { public void StructType.CS_emitDecoder(CS_env env, String name) {
...@@ -892,51 +829,51 @@ aspect CS_Class { ...@@ -892,51 +829,51 @@ aspect CS_Class {
} }
aspect CS_Signature { aspect CS_Signature {
public void Signature.CS_emitSignature(CS_env env, boolean decl){
SignatureList sl = getSignatureList();
sl.CS_emitSignature(env, decl);
}
public abstract void SignatureLine.CS_emitSignature(CS_env env, boolean decl); public void Signature.CS_emitSignature(CS_env env, boolean decl) {
SignatureList sl = getSignatureList();
sl.CS_emitSignature(env, decl);
}
public abstract void SignatureLine.CS_emitSignature(CS_env env, boolean decl);
public void TypeRefSignatureLine.CS_emitSignature(CS_env env, boolean isDecl) {
env.print(getIndentString());
env.println("e.encodeInt(e.getTypeId( typeof("+decl.getName()+")));");
}
public void TypeRefSignatureLine.CS_emitSignature(CS_env env, boolean isDecl){ public void DataSignatureLine.CS_emitSignature(CS_env env, boolean decl) {
byte[] data = getData(2006);
if (data != null) {
env.print(getIndentString()); env.print(getIndentString());
env.println("e.encodePacked32(e.getTypeId( typeof("+decl.getName()+")));"); for (int j = 0 ; j < data.length ; j++) {
} env.print("e.encodeByte((byte)"+ String.format("0x%02X ", data[j]) +"); ");
}
public void DataSignatureLine.CS_emitSignature(CS_env env, boolean decl){ env.println();
byte[] data = getData(env.version); }
if (data != null) {
env.print(getIndentString());
for (int j = 0 ; j < data.length ; j++) {
//env.print("e.encodeByte((byte)"+data[j]+");");
env.print("e.encodeByte((byte)"+ String.format("0x%02X ", data[j]) +"); ");
}
env.println();
}
} }
public void SignatureList.CS_emitSignature(CS_env env, boolean decl) {
env.println("private static void emitSignature(Encoder e){"); public void SignatureList.CS_emitSignature(CS_env env, boolean decl) {
env.indent(); env.println("private static void emitSignature(Encoder e){");
for (int i = 0 ; i < size() ; i++) { env.indent();
String comment = getComment(i); for (int i = 0 ; i < size() ; i++) {
if (comment != null && comment.length() > 0) { String comment = getComment(i);
env.println(getIndent(i) + "// " + comment); if (comment != null && comment.length() > 0) {
} env.println(getIndent(i) + "// " + comment);
SignatureLine l = getSignatureLine(i);
l.CS_emitSignature(env, decl);
} }
env.println("}"); SignatureLine l = getSignatureLine(i);
env.unindent(); l.CS_emitSignature(env, decl);
}
env.println("}");
env.unindent();
} }
} }
aspect CS_Info { aspect CS_Info {
public void Program.CS_info(PrintStream out, String namespace, int version) { public void Program.CS_info(PrintStream out, String namespace) {
CS_env env = new CS_env(out, version); CS_env env = new CS_env(out);
if (namespace == null) { if (namespace == null) {
namespace = ""; namespace = "";
} else { } else {
......
This diff is collapsed.
...@@ -8,8 +8,6 @@ aspect Java_CodeGenEnv { ...@@ -8,8 +8,6 @@ aspect Java_CodeGenEnv {
public class Java_env { public class Java_env {
public final int version; //labcomm version to generate code for
public final String verStr;
private int indent; private int indent;
private int depth; private int depth;
private Java_printer printer; private Java_printer printer;
...@@ -77,24 +75,22 @@ aspect Java_CodeGenEnv { ...@@ -77,24 +75,22 @@ aspect Java_CodeGenEnv {
} }
} }
private Java_env(int version, int indent) { private Java_env(int indent) {
this.version = version;
this.verStr = LabCommVersion.versionString(version);
this.indent = indent; this.indent = indent;
} }
private Java_env(int version, Java_printer printer) { private Java_env(Java_printer printer) {
this(version, 0); this(0);
this.printer = printer; this.printer = printer;
} }
public Java_env(int version, File f) { public Java_env(File f) {
this(version, 0); this(0);
this.printer = new Java_printer(f); this.printer = new Java_printer(f);
} }
public Java_env(int version, PrintStream out) { public Java_env(PrintStream out) {
this(version, 0); this(0);
this.printer = new Java_printer(out); this.printer = new Java_printer(out);
} }
...@@ -204,9 +200,9 @@ aspect Java_Void { ...@@ -204,9 +200,9 @@ aspect Java_Void {
aspect Java_CodeGen { aspect Java_CodeGen {
public void Program.J_gen(PrintStream ps, String pack, int version) throws IOException { public void Program.J_gen(PrintStream ps, String pack) throws IOException {
Java_env env; Java_env env;
env = new Java_env(version, ps); env = new Java_env(ps);
for (int i = 0; i < getNumDecl(); i++) { for (int i = 0; i < getNumDecl(); i++) {
Decl d = getDecl(i); Decl d = getDecl(i);
try { try {
...@@ -219,12 +215,12 @@ aspect Java_CodeGen { ...@@ -219,12 +215,12 @@ aspect Java_CodeGen {
env.close(); env.close();
} }
public void Program.J_gen(String dir, String pack, int version) throws IOException { public void Program.J_gen(String dir, String pack) throws IOException {
Java_env env; Java_env env;
for (int i = 0; i < getNumDecl(); i++) { for (int i = 0; i < getNumDecl(); i++) {
Decl d = getDecl(i); Decl d = getDecl(i);
try { try {
env = new Java_env(version, new File(dir, d.getName() + ".java")); env = new Java_env(new File(dir, d.getName() + ".java"));
d.Java_emitClass(env, pack); d.Java_emitClass(env, pack);
env.close(); env.close();
} catch (Error e) { } catch (Error e) {
...@@ -236,14 +232,14 @@ aspect Java_CodeGen { ...@@ -236,14 +232,14 @@ aspect Java_CodeGen {
/** Experimental method for generating code to a map <classname, source> /** Experimental method for generating code to a map <classname, source>
*/ */
public void Program.J_gen(Map<String,String> src, String pack, int version) throws IOException { public void Program.J_gen(Map<String,String> src, String pack) throws IOException {
Java_env env; Java_env env;
for (int i = 0; i < getNumDecl(); i++) { for (int i = 0; i < getNumDecl(); i++) {
Decl d = getDecl(i); Decl d = getDecl(i);
try { try {
ByteArrayOutputStream bs = new ByteArrayOutputStream(); ByteArrayOutputStream bs = new ByteArrayOutputStream();
PrintStream out = new PrintStream(bs); PrintStream out = new PrintStream(bs);
env = new Java_env(version, out); env = new Java_env(out);
d.Java_emitClass(env, pack); d.Java_emitClass(env, pack);
env.close(); env.close();
src.put(d.getName(), bs.toString()); src.put(d.getName(), bs.toString());
...@@ -276,20 +272,6 @@ aspect Java_Class { ...@@ -276,20 +272,6 @@ aspect Java_Class {
} }
public void Decl.Java_emitUserTypeDeps(Java_env env, String via, boolean outputCode) { public void Decl.Java_emitUserTypeDeps(Java_env env, String via, boolean outputCode) {
// XXX TODO will generate unnecessary recursion for types. fix this per commented out code
// XXX but ensure that types with references actually register themselves.. (i.e., add "nested" argument)
//public abstract void Decl.Java_emitUserTypeDeps(Java_env env, String via, boolean outputCode);
//public void TypeDecl.Java_emitUserTypeDeps(Java_env env, String via, boolean outputCode) {
// // do nothing for type decls; sampledecl iterates over all dependencies and outputs
// // all type decls
//}
//public void SampleDecl.Java_emitUserTypeDeps(Java_env env, String via, boolean outputCode) {
// if(env.versionHasMetaData() && hasDependencies() || isReferenced() ) {
// if(env.versionHasMetaData() && isSampleDecl() && outputCode) {
// env.println("if(sendMetaData){");
// env.indent();
// }
Iterator<Decl> it = type_dependencies().iterator(); Iterator<Decl> it = type_dependencies().iterator();
while(it.hasNext()) { while(it.hasNext()) {
Decl t = it.next(); Decl t = it.next();
...@@ -302,13 +284,9 @@ aspect Java_Class { ...@@ -302,13 +284,9 @@ aspect Java_Class {
env.println(" //Depends ("+refpath+") on "+t.getName() ); env.println(" //Depends ("+refpath+") on "+t.getName() );
} }
} }
// if(env.versionHasMetaData() && isSampleDecl() && outputCode) {
// env.unindent();
// env.println("}");
// }
// }
} }
public void Decl.Java_emitUserTypeRefs(Java_env env, String via, boolean outputCode) {
public void Decl.Java_emitUserTypeRefs(Java_env env, String via, boolean outputCode) {
if( isReferenced() ) { if( isReferenced() ) {
Iterator<Decl> it = type_references().iterator(); Iterator<Decl> it = type_references().iterator();
while(it.hasNext()) { while(it.hasNext()) {
...@@ -348,19 +326,15 @@ aspect Java_Class { ...@@ -348,19 +326,15 @@ aspect Java_Class {
env.println("package " + pack + ";"); env.println("package " + pack + ";");
} }
env.println("import se.lth.control.labcomm"+env.verStr+".Constant;"); env.println("import se.lth.control.labcomm2006.Constant;");
env.println("import se.lth.control.labcomm"+env.verStr+".SampleType;"); env.println("import se.lth.control.labcomm2006.SampleType;");
if (getType().Java_needInstance() || hasDependencies() || isReferenced()) { if (getType().Java_needInstance() || hasDependencies() || isReferenced()) {
env.println("import se.lth.control.labcomm"+env.verStr+".Encoder;"); env.println("import se.lth.control.labcomm2006.Encoder;");
env.println("import se.lth.control.labcomm"+env.verStr+".SampleDispatcher;"); env.println("import se.lth.control.labcomm2006.SampleDispatcher;");
env.println("import se.lth.control.labcomm"+env.verStr+".SampleHandler;"); env.println("import se.lth.control.labcomm2006.SampleHandler;");
// env.println();
// }
//
// if (getType().Java_needInstance()) {
env.println("import java.io.IOException;"); env.println("import java.io.IOException;");
env.println("import se.lth.control.labcomm"+env.verStr+".Decoder;"); env.println("import se.lth.control.labcomm2006.Decoder;");
} }
// For types without type_dependencies and not needing an instance, // For types without type_dependencies and not needing an instance,
// currently just an empty class is generated // currently just an empty class is generated
...@@ -397,21 +371,14 @@ aspect Java_Class { ...@@ -397,21 +371,14 @@ aspect Java_Class {
} }
env.println("import java.io.IOException;"); env.println("import java.io.IOException;");
env.println("import se.lth.control.labcomm"+env.verStr+".Constant;"); env.println("import se.lth.control.labcomm2006.Constant;");
env.println("import se.lth.control.labcomm"+env.verStr+".Decoder;"); env.println("import se.lth.control.labcomm2006.Decoder;");
env.println("import se.lth.control.labcomm"+env.verStr+".SampleDispatcher;"); env.println("import se.lth.control.labcomm2006.SampleDispatcher;");
env.println("import se.lth.control.labcomm"+env.verStr+".Encoder;"); env.println("import se.lth.control.labcomm2006.Encoder;");
env.println("import se.lth.control.labcomm"+env.verStr+".SampleHandler;"); env.println("import se.lth.control.labcomm2006.SampleHandler;");
env.println("import se.lth.control.labcomm"+env.verStr+".Sample;"); env.println("import se.lth.control.labcomm2006.Sample;");
env.println(); env.println();
env.print("public class " + getName()); env.print("public class " + getName());
// TODO: ?
// Code for making samples of user types extend their type
// currently commented out. Is this a good idea or not?
//
// if(getType().isUserType()) {
// env.print(" extends "+getType().getTypeName());
// }
env.println(" implements Sample {"); env.println(" implements Sample {");
env.println(); env.println();
env.indent(); env.indent();
...@@ -464,13 +431,13 @@ aspect Java_Class { ...@@ -464,13 +431,13 @@ aspect Java_Class {
public void Decl.Java_emitFlatSignature(Java_env env){ public void Decl.Java_emitFlatSignature(Java_env env){
env.println("private static byte[] signature = new byte[] {"); env.println("private static byte[] signature = new byte[] {");
env.indent(); env.indent();
SignatureList signature = flatSignature(env.version); SignatureList signature = flatSignature(2006);
for (int i = 0 ; i < signature.size() ; i++) { for (int i = 0 ; i < signature.size() ; i++) {
String comment = signature.getComment(i); String comment = signature.getComment(i);