Commit 4366700d authored by Sven Gestegård Robertz's avatar Sven Gestegård Robertz
Browse files

compiler code cleanup

parent 2bc1b87e
...@@ -15,9 +15,6 @@ aspect CS_CodeGenEnv { ...@@ -15,9 +15,6 @@ aspect CS_CodeGenEnv {
private CS_printer printer; private CS_printer printer;
private HashMap unique = new HashMap(); private HashMap unique = new HashMap();
// public boolean versionHasMetaData() {
// return version != 2006;
// }
final private static class CS_printer { final private static class CS_printer {
private boolean newline = true; private boolean newline = true;
...@@ -84,7 +81,7 @@ aspect CS_CodeGenEnv { ...@@ -84,7 +81,7 @@ aspect CS_CodeGenEnv {
this.version = version; this.version = version;
this.indent = indent; this.indent = indent;
this.printer = printer; this.printer = printer;
this.verStr = (version == 2006) ? "2006" : ""; this.verStr = LabCommVersion.versionString(version);
} }
public CS_env(File f, int version) { public CS_env(File f, int version) {
...@@ -234,16 +231,6 @@ aspect CS_CodeGen { ...@@ -234,16 +231,6 @@ aspect CS_CodeGen {
aspect CS_Register { aspect CS_Register {
public void Program.CS_emitTypeRegister(CS_env env) { public void Program.CS_emitTypeRegister(CS_env env) {
/*
env.println("static void register(LabCommChannel c) {");
env.indent();
for (int i = 0; i < getNumDecl(); i++) {
getDecl(i).CS_emitTypeRegister(env);
}
env.unindent();
env.println("}");
*/
} }
public void Decl.CS_emitTypeRegister(CS_env env) { public void Decl.CS_emitTypeRegister(CS_env env) {
...@@ -281,32 +268,18 @@ aspect CS_Class { ...@@ -281,32 +268,18 @@ aspect CS_Class {
} }
public void Decl.CS_emitUserTypeDeps(CS_env env, String via, boolean outputCode){ public void Decl.CS_emitUserTypeDeps(CS_env env, String via, boolean outputCode){
// if(env.versionHasMetaData() && hasDependencies() || isReferenced() ) { Iterator<Decl> it = type_dependencies().iterator();
// if(env.versionHasMetaData() && isSampleDecl() && outputCode) { while(it.hasNext()) {
// env.println("if(sendMetaData){"); Decl t = it.next();
// env.indent();
// } t.CS_emitUserTypeDeps(env, t.getName(), outputCode);
// Iterator<Decl> it = type_dependencies().iterator(); if( outputCode && t.getType().isUserType() ) {
// while(it.hasNext()) { env.println(t.getName()+".register(e);");
// Decl t = it.next(); } else { // Just output a comment
// String refpath = (via == null) ? "directly" : "indirectly via "+via;
// t.CS_emitUserTypeDeps(env, t.getName(), outputCode); env.println(" //Depends ("+refpath+") on "+t.getName() );
// if( outputCode && t.getType().isUserType() ) { }
// //env.println("/* FIXME: " + t.getName() + " does not exist"); }
// env.println(t.getName()+".register(e, sendMetaData);");
// //env.println("*/");
// } else { // Just output a comment
// String refpath = (via == null) ? "directly" : "indirectly via "+via;
// //env.println(" //Depends ("+refpath+") on "+t.getName() + " (" + t +") " );
// //env.println(" //Depends ("+refpath+") on "+t.getName() );
// env.println(" //Depends ("+refpath+") on "+t.getName()+" -- "+t.getType() );
// }
// }
// if(env.versionHasMetaData() && isSampleDecl() && outputCode) {
// env.unindent();
// env.println("}");
// }
// }
} }
public void Decl.CS_emitUserTypeRefs(CS_env env, String via, boolean outputCode){ public void Decl.CS_emitUserTypeRefs(CS_env env, String via, boolean outputCode){
...@@ -332,22 +305,17 @@ aspect CS_Class { ...@@ -332,22 +305,17 @@ aspect CS_Class {
env.println("e.register(Dispatcher.singleton());"); env.println("e.register(Dispatcher.singleton());");
env.unindent(); env.unindent();
env.println("}"); env.println("}");
// env.println();
// env.println("public static void register(Encoder e, bool sendMetaData){");
// env.indent();
//
// CS_emitUserTypeDeps(env, null, true);
// if(env.versionHasMetaData()) {
// env.println("e.register(Dispatcher.singleton(), sendMetaData);");
// } else {
// env.println("e.register(Dispatcher.singleton());");
// }
// env.unindent();
// env.println("}");
// env.println();
} }
public void Decl.CS_emitRegisterDecoder(CS_env env) {
env.println("public static void register(Decoder d, Handler h) {");
env.indent();
env.println("d.register(Dispatcher.singleton(), h);");
env.unindent();
env.println("}");
env.println();
}
public void TypeDecl.CS_emitClass(CS_env env) { public void TypeDecl.CS_emitClass(CS_env env) {
if (getType().CS_needInstance()) { if (getType().CS_needInstance()) {
// Hackish prettyprint preamble // Hackish prettyprint preamble
...@@ -389,91 +357,17 @@ aspect CS_Class { ...@@ -389,91 +357,17 @@ aspect CS_Class {
env.println(");"); env.println(");");
env.println("}"); env.println("}");
env.println(); env.println();
env.println("public static void register(Decoder d, Handler h) {");
env.indent();
env.println("d.register(new Dispatcher(), h);");
env.unindent();
env.println("}");
env.println();
/*
env.println("public static void register(Encoder e) {");
env.indent();
env.println("e.register(new Dispatcher());");
env.unindent();
env.println("}");
env.println(); */
CS_emitRegisterEncoder(env);
/*env.println("private class Dispatcher : LabCommDispatcher {");
env.indent();
env.println();
env.println("public Type getSampleClass() {");
env.indent();
env.println("return typeof(" + getName() + ");");
env.unindent();
env.println("}");
env.println();
env.println("public String getName() {");
env.indent();
env.println("return \"" + getName() + "\";");
env.unindent();
env.println("}");
env.println();
env.println("public byte[] getSignature() {");
env.indent();
env.println("return signature;");
env.unindent();
env.println("}");
env.println();
env.println("public void decodeAndHandle(Decoder d, SampleHandler h) {");
env.indent();
if (isVoid()) {
env.println(getName() + ".decode(d);");
env.println("((Handler)h).handle();");
} else {
env.println("((Handler)h).handle(" + getName() + ".decode(d));");
}
env.unindent();
env.println("}");
env.println("");
env.unindent();
env.println("}");
env.println("");*/
CS_emitDispatcher(env,true); CS_emitDispatcher(env,true);
CS_emitRegisterEncoder(env);
CS_emitRegisterDecoder(env);
CS_emitEncoder(env); CS_emitEncoder(env);
CS_emitDecoder(env); CS_emitDecoder(env);
/*env.println("private static byte[] signature = new byte[] {");
env.indent();
SignatureList signature = flatSignature(env.version);
for (int i = 0 ; i < signature.size() ; i++) {
String comment = signature.getComment(i);
if (comment != null) {
env.println(signature.getIndent(i) + "// " + comment);
}
byte[] data = signature.getData(i, env.version);
if (data != null) {
env.print(signature.getIndent(i));
for (int j = 0 ; j < data.length ; j++) {
env.print(data[j] + ", ");
}
env.println();
}
}
env.unindent();
env.println("};");
env.unindent();
env.println();*/
CS_emitSignature(env); CS_emitSignature(env);
env.println("}"); env.println("}");
} }
public void Decl.CS_emitSignature(CS_env env) { public void Decl.CS_emitSignature(CS_env env) {
//always emit the flat signature, as it is needed
//for matching at the decoder side (which cannot know
//the type_ids of dependent types. Therefore, flat sigs
//are used for matching
CS_emitFlatSignature(env); CS_emitFlatSignature(env);
// if(isReferenced() || isSampleDecl()){ // if(isReferenced() || isSampleDecl()){
// Signature signature = getSignature(); // Signature signature = getSignature();
...@@ -494,7 +388,8 @@ aspect CS_Class { ...@@ -494,7 +388,8 @@ aspect CS_Class {
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++) {
env.print(data[j] + ", "); //env.print(data[j] + ", ");
env.print(String.format("0x%02X, ", data[j])) ;
} }
env.println(); env.println();
} }
...@@ -550,13 +445,7 @@ aspect CS_Class { ...@@ -550,13 +445,7 @@ aspect CS_Class {
env.println("return "+isSample+";"); env.println("return "+isSample+";");
env.unindent(); env.unindent();
env.println("}"); env.println("}");
env.println("public bool hasStaticSignature() {"); env.println("/** return the flat signature. */");
env.indent();
env.println("return "+!hasDependencies()+";");
env.unindent();
env.println("}");
env.println();
env.println("/** return the flat signature. Intended use is on decoder side */");
env.println("public byte[] getSignature() {"); env.println("public byte[] getSignature() {");
env.indent(); env.indent();
env.println("return signature;"); env.println("return signature;");
......
...@@ -33,7 +33,7 @@ aspect C_CodeGenEnv { ...@@ -33,7 +33,7 @@ aspect C_CodeGenEnv {
} }
} }
public final int version; //labcomm version (2006 or 2013) public final int version; //labcomm version (2006 or 2014)
public final String verStr; // version suffix to append (currently _2006 and empty string) public final String verStr; // version suffix to append (currently _2006 and empty string)
public final String qualid; public final String qualid;
...@@ -56,7 +56,7 @@ aspect C_CodeGenEnv { ...@@ -56,7 +56,7 @@ aspect C_CodeGenEnv {
int nestedLevel, int version) int nestedLevel, int version)
{ {
this.version = version; this.version = version;
this.verStr = (version == 2006 ? "2006" : ""); this.verStr = LabCommVersion.versionString(version);
this.qualid = qualid; this.qualid = qualid;
this.lcName = lcName; this.lcName = lcName;
this.rawPrefix = rawPrefix; this.rawPrefix = rawPrefix;
...@@ -986,13 +986,14 @@ aspect C_DecoderIoctl { ...@@ -986,13 +986,14 @@ aspect C_DecoderIoctl {
aspect C_Encoder { aspect C_Encoder {
public void Decl.C_emitEncoder(C_env env) { public void Decl.C_emitEncoder(C_env env) {
//XXX throw new Error(this.getClass().getName() + throw new Error(this.getClass().getName() +
// ".C_emitEncoder()" + ".C_emitEncoder()" +
// " not declared"); " not declared");
} }
// public void TypeDecl.C_emitEncoder(C_env env) { public void TypeDecl.C_emitEncoder(C_env env) {
// } // do nothing for type decls
}
public void SampleDecl.C_emitEncoder(C_env env) { public void SampleDecl.C_emitEncoder(C_env env) {
env = env.nestStruct("(*v)"); env = env.nestStruct("(*v)");
...@@ -1099,15 +1100,15 @@ aspect C_Encoder { ...@@ -1099,15 +1100,15 @@ aspect C_Encoder {
} }
public void Decl.C_emitEncoderRegisterHandler(C_env env) { public void Decl.C_emitEncoderRegisterHandler(C_env env) {
//XXX throw new Error(this.getClass().getName() +
// throw new Error(this.getClass().getName() + ".C_emitEncoderRegisterHandler(C_env env)" +
// ".C_emitEncoderRegisterHandler(C_env env)" + " not declared");
// " not declared"); }
}
// public void TypeDecl.C_emitEncoderRegisterHandler(C_env env) {
//public void TypeDecl.C_emitEncoderRegisterHandler(C_env env) { // do nothing for type decls
//} }
//
public void SampleDecl.C_emitEncoderRegisterHandler(C_env env) { public void SampleDecl.C_emitEncoderRegisterHandler(C_env env) {
env.println("int labcomm"+env.verStr+"_encoder_register_" + env.println("int labcomm"+env.verStr+"_encoder_register_" +
env.prefix + getName() + "("); env.prefix + getName() + "(");
...@@ -1118,7 +1119,7 @@ aspect C_Encoder { ...@@ -1118,7 +1119,7 @@ aspect C_Encoder {
env.println("{"); env.println("{");
env.indent(); env.indent();
C_emitUserTypeDeps(env, null, false); //XXX HERE BE DRAGONS C_emitUserTypeDeps(env, null, false); //XXX HERE BE DRAGONS
//set to false to turn off //currently set to false to turn off
//outputting of code //outputting of code
env.println("return labcomm"+env.verStr+"_internal_encoder_register("); env.println("return labcomm"+env.verStr+"_internal_encoder_register(");
env.indent(); env.indent();
...@@ -1183,7 +1184,6 @@ aspect C_TypeDependencies { ...@@ -1183,7 +1184,6 @@ aspect C_TypeDependencies {
env.println("labcomm"+env.verStr+"_encoder_register_"+env.prefix + t.getName()+"(e);"); env.println("labcomm"+env.verStr+"_encoder_register_"+env.prefix + t.getName()+"(e);");
} else { // Just output a comment } else { // Just output a comment
String refpath = (via == null) ? "directly" : "indirectly via "+via; String refpath = (via == null) ? "directly" : "indirectly via "+via;
//env.println(" //Depends ("+refpath+") on "+t.getName() + " (" + t +") " );
env.println(" //Depends ("+refpath+") on "+t.getName() ); env.println(" //Depends ("+refpath+") on "+t.getName() );
} }
} }
...@@ -1220,30 +1220,26 @@ aspect C_Signature { ...@@ -1220,30 +1220,26 @@ aspect C_Signature {
eq TypeDecl.C_DeclTypeString() = "LABCOMM_TYPEDEF"; eq TypeDecl.C_DeclTypeString() = "LABCOMM_TYPEDEF";
public void Decl.C_emitSignature(C_env env) { public void Decl.C_emitSignature(C_env env) {
//always emit the flat signature, as it is needed
//for matching at the decoder side (which cannot know
//the type_ids of dependent types. Therefore, flat sigs
//are used for matching
C_emitFlatSignature(env); C_emitFlatSignature(env);
/* //
if( false && (isReferenced() || isSampleDecl())){ // if( false && (isReferenced() || isSampleDecl())){
Signature signature = getSignature(); // Signature signature = getSignature();
signature.C_emitSignature(env, !isSampleDecl()); // signature.C_emitSignature(env, !isSampleDecl());
} else { // } else {
env.println("// not emitting signature for "+getName()+isReferenced()+isSampleDecl()); // env.println("// not emitting signature for "+getName()+isReferenced()+isSampleDecl());
} // }
if(env.versionHasMetaData()) { // if(env.versionHasMetaData()) {
if(isReferenced() || isSampleDecl()){ // if(isReferenced() || isSampleDecl()){
env.println("(int (*)(void *))labcomm"+env.verStr+"_signature_" + // env.println("(int (*)(void *))labcomm"+env.verStr+"_signature_" +
env.prefix + getName() + "_emit_signature"); // env.prefix + getName() + "_emit_signature");
} else { // } else {
env.println("NULL"); // HERE BE DRAGONS! Is it worth the size saving to skip emitting the emit_signature function for unused types? // env.println("NULL"); // HERE BE DRAGONS! Is it worth the size saving to skip emitting the emit_signature function for unused types?
// The code won't likely end up in a target system anyway? // // The code won't likely end up in a target system anyway?
} // }
} // }
env.unindent(); // env.unindent();
env.println(" };"); // env.println(" };");
*/ //
} }
public void ASTNode.C_emitFlatSignature(C_env env) { public void ASTNode.C_emitFlatSignature(C_env env) {
...@@ -1318,41 +1314,33 @@ aspect C_Signature { ...@@ -1318,41 +1314,33 @@ aspect C_Signature {
// env.println("\"),"); // env.println("\"),");
// } // }
} }
/* //
//
byte[] data = getData(env.version); // byte[] data = getData(env.version);
if (data != null) { // if (data != null) {
for (int j = 0 ; j < data.length ; j++) { // for (int j = 0 ; j < data.length ; j++) {
env.print(getIndentString()); // env.print(getIndentString());
//env.print("printf(\"labcomm"+env.verStr+"_write_byte( w, (unsigned char)"+ String.format("0x%02X ", data[j]) +")\\n\"); "); // //env.print("printf(\"labcomm"+env.verStr+"_write_byte( w, (unsigned char)"+ String.format("0x%02X ", data[j]) +")\\n\"); ");
env.print("labcomm"+env.verStr+"_write_byte( w, (unsigned char)"+ String.format("0x%02X ", data[j]) +"); "); // env.print("labcomm"+env.verStr+"_write_byte( w, (unsigned char)"+ String.format("0x%02X ", data[j]) +"); ");
env.println("if (result != 0) { return result; }"); // env.println("if (result != 0) { return result; }");
} // }
env.println(); // env.println();
} // }
*/ //
//} //}
/**
static int encode_test_twoLines(
struct labcomm_writer *w
, test_twoLines *v
)
{
result = labcomm_write_int(w, (*v).l1.start.x.val);
**/
public void SignatureList.C_emitSignature(C_env env, boolean decl) { public void SignatureList.C_emitSignature(C_env env, boolean decl) {
env.println("static struct labcomm_signature_data signature_tree_" + // env.println("static struct labcomm_signature_data signature_tree_" +
env.prefix + parentDecl().getName() + "[] = {"); // env.prefix + parentDecl().getName() + "[] = {");
env.indent(); // env.indent();
for (int i = 0 ; i < size() ; i++) { // for (int i = 0 ; i < size() ; i++) {
SignatureLine l = getSignatureLine(i); // SignatureLine l = getSignatureLine(i);
l.C_emitSignature(env, decl); // l.C_emitSignature(env, decl);
} // }
//
env.println("LABCOMM_SIGDEF_END"); // env.println("LABCOMM_SIGDEF_END");
env.println("};"); // env.println("};");
env.unindent(); // env.unindent();
env.println(); // env.println();
} }
......
...@@ -15,10 +15,6 @@ aspect Java_CodeGenEnv { ...@@ -15,10 +15,6 @@ aspect Java_CodeGenEnv {
private Java_printer printer; private Java_printer printer;
private HashMap unique = new HashMap(); private HashMap unique = new HashMap();
public boolean versionHasMetaData() {
return version != 2006;
}
final private class Java_printer { final private class Java_printer {
private boolean newline = true; private boolean newline = true;
...@@ -83,7 +79,7 @@ aspect Java_CodeGenEnv { ...@@ -83,7 +79,7 @@ aspect Java_CodeGenEnv {
private Java_env(int version, int indent) { private Java_env(int version, int indent) {
this.version = version; this.version = version;
this.verStr = (version == 2006) ? "2006" : ""; this.verStr = LabCommVersion.versionString(version);
this.indent = indent; this.indent = indent;
} }
...@@ -289,11 +285,11 @@ aspect Java_Class { ...@@ -289,11 +285,11 @@ aspect Java_Class {
// // all type decls // // all type decls
//} //}
//public void SampleDecl.Java_emitUserTypeDeps(Java_env env, String via, boolean outputCode) { //public void SampleDecl.Java_emitUserTypeDeps(Java_env env, String via, boolean outputCode) {
if(env.versionHasMetaData() && hasDependencies() || isReferenced() ) { // if(env.versionHasMetaData() && hasDependencies() || isReferenced() ) {
if(env.versionHasMetaData() && isSampleDecl() && outputCode) { // if(env.versionHasMetaData() && isSampleDecl() && outputCode) {
env.println("if(sendMetaData){"); // env.println("if(sendMetaData){");
env.indent(); // 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();
...@@ -303,16 +299,14 @@ aspect Java_Class { ...@@ -303,16 +299,14 @@ aspect Java_Class {
env.println(t.getName()+".register(e);"); env.println(t.getName()+".register(e);");
} else { // Just output a comment } else { // Just output a comment
String refpath = (via == null) ? "directly" : "indirectly via "+via; String refpath = (via == null) ? "directly" : "indirectly via "+via;
//env.println(" //Depends ("+refpath+") on "+t.getName() + " (" + t +") " ); env.println(" //Depends ("+refpath+") on "+t.getName() );
//env.println(" //Depends ("+refpath+") on "+t.getName() );
env.println(" //Depends ("+refpath+") on "+t.getName()+" -- "+t.getType() );
} }
} }
if(env.versionHasMetaData() && isSampleDecl() && outputCode) { // if(env.versionHasMetaData() && isSampleDecl() && outputCode) {
env.unindent(); // env.unindent();
env.println("}"); // 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() ) {
...@@ -344,11 +338,7 @@ aspect Java_Class { ...@@ -344,11 +338,7 @@ aspect Java_Class {
env.indent(); env.indent();
Java_emitUserTypeDeps(env, null, true); Java_emitUserTypeDeps(env, null, true);
if(env.versionHasMetaData()) { env.println("e.register(Dispatcher.singleton());");
env.println("e.register(Dispatcher.singleton());");
} else {
env.println("e.register(Dispatcher.singleton());");
}
env.unindent(); env.unindent();
env.println("}"); env.println("}");
env.println(); env.println();
...@@ -499,7 +489,7 @@ aspect Java_Class { ...@@ -499,7 +489,7 @@ aspect Java_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.Java_emitDispatcher(Java_env env, boolean isSample) { public void Decl.Java_emitDispatcher(Java_env env, boolean isSample) {
String genericStr = env.versionHasMetaData()?"<"+getName()+">":""; String genericStr = ""; //env.versionHasMetaData()?"<"+getName()+">":"";
env.println("private static class Dispatcher implements SampleDispatcher "+genericStr+"{"); env.println("private static class Dispatcher implements SampleDispatcher "+genericStr+"{");
env.indent(); env.indent();
env.println(); env.println();
...@@ -547,13 +537,13 @@ aspect Java_Class { ...@@ -547,13 +537,13 @@ aspect Java_Class {
env.println("return "+isSample+";"); env.println("return "+isSample+";");
env.unindent(); env.unindent();
env.println("}"); env.println("}");
env.println("public boolean hasStaticSignature() {"); // env.println("public boolean hasStaticSignature() {");
env.indent(); // env.indent();
env.println("return "+!hasDependencies()+";"); // env.println("return "+!hasDependencies()+";");
env.unindent(); // env.unindent();
env.println("}"); // env.println("}");
env.println(); env.println();
env.println("/** return the flat signature. Intended use is on decoder side */"); env.println("/** return the flat signature. */");
env.println("public byte[] getSignature() {"); env.println("public byte[] getSignature() {");
env.indent(); env.indent();
env.println("return signature;"); env.println("return signature;");
......
...@@ -34,7 +34,6 @@ aspect NameAnalysis { ...@@ -34,7 +34,6 @@ aspect NameAnalysis {
return null; return null;
} }
//syn TypeDecl UserType.decl() = lookupType(getName());
syn TypeDecl Type.decl(); syn TypeDecl Type.decl();
eq Type.decl() = null; eq Type.decl() = null;
eq UserType.decl() = lookupType(getName()); eq UserType.decl() = lookupType(getName());
......
...@@ -23,34 +23,21 @@ aspect Signature { ...@@ -23,34 +23,21 @@ aspect Signature {
Signature sig = new Signature(); Signature sig = new Signature();
sig.setSignatureList(sl); sig.setSignatureList(sl);
sig.setFlatSignatureList(fsl); sig.setFlatSignatureList(fsl);
// HERE BE DRAGONS. Is this correct for a NTA?
setSignature(sig); setSignature(sig);
return sig;