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 {
private CS_printer printer;
private HashMap unique = new HashMap();
// public boolean versionHasMetaData() {
// return version != 2006;
// }
final private static class CS_printer {
private boolean newline = true;
......@@ -84,7 +81,7 @@ aspect CS_CodeGenEnv {
this.version = version;
this.indent = indent;
this.printer = printer;
this.verStr = (version == 2006) ? "2006" : "";
this.verStr = LabCommVersion.versionString(version);
}
public CS_env(File f, int version) {
......@@ -234,16 +231,6 @@ aspect CS_CodeGen {
aspect CS_Register {
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) {
......@@ -281,32 +268,18 @@ aspect CS_Class {
}
public void Decl.CS_emitUserTypeDeps(CS_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();
// while(it.hasNext()) {
// Decl t = it.next();
//
// t.CS_emitUserTypeDeps(env, t.getName(), outputCode);
// 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("}");
// }
// }
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){
......@@ -332,22 +305,17 @@ aspect CS_Class {
env.println("e.register(Dispatcher.singleton());");
env.unindent();
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) {
if (getType().CS_needInstance()) {
// Hackish prettyprint preamble
......@@ -389,91 +357,17 @@ aspect CS_Class {
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_emitRegisterEncoder(env);
CS_emitRegisterDecoder(env);
CS_emitEncoder(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);
env.println("}");
}
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);
// if(isReferenced() || isSampleDecl()){
// Signature signature = getSignature();
......@@ -494,7 +388,8 @@ aspect CS_Class {
if (data != null) {
env.print(signature.getIndent(i));
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();
}
......@@ -550,13 +445,7 @@ aspect CS_Class {
env.println("return "+isSample+";");
env.unindent();
env.println("}");
env.println("public bool hasStaticSignature() {");
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("/** return the flat signature. */");
env.println("public byte[] getSignature() {");
env.indent();
env.println("return signature;");
......
......@@ -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 qualid;
......@@ -56,7 +56,7 @@ aspect C_CodeGenEnv {
int nestedLevel, int version)
{
this.version = version;
this.verStr = (version == 2006 ? "2006" : "");
this.verStr = LabCommVersion.versionString(version);
this.qualid = qualid;
this.lcName = lcName;
this.rawPrefix = rawPrefix;
......@@ -986,13 +986,14 @@ aspect C_DecoderIoctl {
aspect C_Encoder {
public void Decl.C_emitEncoder(C_env env) {
//XXX throw new Error(this.getClass().getName() +
// ".C_emitEncoder()" +
// " not declared");
throw new Error(this.getClass().getName() +
".C_emitEncoder()" +
" 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) {
env = env.nestStruct("(*v)");
......@@ -1099,15 +1100,15 @@ aspect C_Encoder {
}
public void Decl.C_emitEncoderRegisterHandler(C_env env) {
//XXX
// throw new Error(this.getClass().getName() +
// ".C_emitEncoderRegisterHandler(C_env env)" +
// " not declared");
}
//
//public void TypeDecl.C_emitEncoderRegisterHandler(C_env env) {
//}
//
throw new Error(this.getClass().getName() +
".C_emitEncoderRegisterHandler(C_env env)" +
" not declared");
}
public void TypeDecl.C_emitEncoderRegisterHandler(C_env env) {
// do nothing for type decls
}
public void SampleDecl.C_emitEncoderRegisterHandler(C_env env) {
env.println("int labcomm"+env.verStr+"_encoder_register_" +
env.prefix + getName() + "(");
......@@ -1118,7 +1119,7 @@ aspect C_Encoder {
env.println("{");
env.indent();
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
env.println("return labcomm"+env.verStr+"_internal_encoder_register(");
env.indent();
......@@ -1183,7 +1184,6 @@ aspect C_TypeDependencies {
env.println("labcomm"+env.verStr+"_encoder_register_"+env.prefix + t.getName()+"(e);");
} 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() );
}
}
......@@ -1220,30 +1220,26 @@ aspect C_Signature {
eq TypeDecl.C_DeclTypeString() = "LABCOMM_TYPEDEF";
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);
/*
if( false && (isReferenced() || isSampleDecl())){
Signature signature = getSignature();
signature.C_emitSignature(env, !isSampleDecl());
} else {
env.println("// not emitting signature for "+getName()+isReferenced()+isSampleDecl());
}
if(env.versionHasMetaData()) {
if(isReferenced() || isSampleDecl()){
env.println("(int (*)(void *))labcomm"+env.verStr+"_signature_" +
env.prefix + getName() + "_emit_signature");
} else {
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?
}
}
env.unindent();
env.println(" };");
*/
//
// if( false && (isReferenced() || isSampleDecl())){
// Signature signature = getSignature();
// signature.C_emitSignature(env, !isSampleDecl());
// } else {
// env.println("// not emitting signature for "+getName()+isReferenced()+isSampleDecl());
// }
// if(env.versionHasMetaData()) {
// if(isReferenced() || isSampleDecl()){
// env.println("(int (*)(void *))labcomm"+env.verStr+"_signature_" +
// env.prefix + getName() + "_emit_signature");
// } else {
// 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?
// }
// }
// env.unindent();
// env.println(" };");
//
}
public void ASTNode.C_emitFlatSignature(C_env env) {
......@@ -1318,41 +1314,33 @@ aspect C_Signature {
// env.println("\"),");
// }
}
/*
byte[] data = getData(env.version);
if (data != null) {
for (int j = 0 ; j < data.length ; j++) {
env.print(getIndentString());
//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.println("if (result != 0) { return result; }");
}
env.println();
}
*/
//}
/**
static int encode_test_twoLines(
struct labcomm_writer *w
, test_twoLines *v
)
{
result = labcomm_write_int(w, (*v).l1.start.x.val);
**/
//
//
// byte[] data = getData(env.version);
// if (data != null) {
// for (int j = 0 ; j < data.length ; j++) {
// env.print(getIndentString());
// //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.println("if (result != 0) { return result; }");
// }
// env.println();
// }
//
//}
public void SignatureList.C_emitSignature(C_env env, boolean decl) {
env.println("static struct labcomm_signature_data signature_tree_" +
env.prefix + parentDecl().getName() + "[] = {");
env.indent();
for (int i = 0 ; i < size() ; i++) {
SignatureLine l = getSignatureLine(i);
l.C_emitSignature(env, decl);
}
env.println("LABCOMM_SIGDEF_END");
env.println("};");
env.unindent();
env.println();
// env.println("static struct labcomm_signature_data signature_tree_" +
// env.prefix + parentDecl().getName() + "[] = {");
// env.indent();
// for (int i = 0 ; i < size() ; i++) {
// SignatureLine l = getSignatureLine(i);
// l.C_emitSignature(env, decl);
// }
//
// env.println("LABCOMM_SIGDEF_END");
// env.println("};");
// env.unindent();
// env.println();
}
......
......@@ -15,10 +15,6 @@ aspect Java_CodeGenEnv {
private Java_printer printer;
private HashMap unique = new HashMap();
public boolean versionHasMetaData() {
return version != 2006;
}
final private class Java_printer {
private boolean newline = true;
......@@ -83,7 +79,7 @@ aspect Java_CodeGenEnv {
private Java_env(int version, int indent) {
this.version = version;
this.verStr = (version == 2006) ? "2006" : "";
this.verStr = LabCommVersion.versionString(version);
this.indent = indent;
}
......@@ -289,11 +285,11 @@ aspect Java_Class {
// // 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();
}
// if(env.versionHasMetaData() && hasDependencies() || isReferenced() ) {
// if(env.versionHasMetaData() && isSampleDecl() && outputCode) {
// env.println("if(sendMetaData){");
// env.indent();
// }
Iterator<Decl> it = type_dependencies().iterator();
while(it.hasNext()) {
Decl t = it.next();
......@@ -303,16 +299,14 @@ aspect Java_Class {
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() + " (" + t +") " );
//env.println(" //Depends ("+refpath+") on "+t.getName() );
env.println(" //Depends ("+refpath+") on "+t.getName()+" -- "+t.getType() );
env.println(" //Depends ("+refpath+") on "+t.getName() );
}
}
if(env.versionHasMetaData() && isSampleDecl() && outputCode) {
env.unindent();
env.println("}");
}
}
// if(env.versionHasMetaData() && isSampleDecl() && outputCode) {
// env.unindent();
// env.println("}");
// }
// }
}
public void Decl.Java_emitUserTypeRefs(Java_env env, String via, boolean outputCode) {
if( isReferenced() ) {
......@@ -344,11 +338,7 @@ aspect Java_Class {
env.indent();
Java_emitUserTypeDeps(env, null, true);
if(env.versionHasMetaData()) {
env.println("e.register(Dispatcher.singleton());");
} else {
env.println("e.register(Dispatcher.singleton());");
}
env.println("e.register(Dispatcher.singleton());");
env.unindent();
env.println("}");
env.println();
......@@ -499,7 +489,7 @@ aspect Java_Class {
//XXX TODO: refactor: split into a static class ("TypeDefSingleton"?)and a (smaller) dispatcher
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.indent();
env.println();
......@@ -547,13 +537,13 @@ aspect Java_Class {
env.println("return "+isSample+";");
env.unindent();
env.println("}");
env.println("public boolean hasStaticSignature() {");
env.indent();
env.println("return "+!hasDependencies()+";");
env.unindent();
env.println("}");
// env.println("public boolean hasStaticSignature() {");
// 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("/** return the flat signature. */");
env.println("public byte[] getSignature() {");
env.indent();
env.println("return signature;");
......
......@@ -34,7 +34,6 @@ aspect NameAnalysis {
return null;
}
//syn TypeDecl UserType.decl() = lookupType(getName());
syn TypeDecl Type.decl();
eq Type.decl() = null;
eq UserType.decl() = lookupType(getName());
......
......@@ -23,34 +23,21 @@ aspect Signature {
Signature sig = new Signature();
sig.setSignatureList(sl);
sig.setFlatSignatureList(fsl);
// HERE BE DRAGONS. Is this correct for a NTA?
setSignature(sig);
return sig;
}
//syn nta SignatureList Signature.getSignatureList() {
// SignatureList result = new SignatureList();
// genSigLineForDecl(result, true);
// return result;
//}
public String SignatureLine.getIndentString() {
StringBuffer result = new StringBuffer();
int indent = getIndent();
for (int i = 0 ; i < indent ; i++) {
result.append(" ");
}
return result.toString();
public String SignatureLine.getIndentString() {
StringBuffer result = new StringBuffer();
int indent = getIndent();
for (int i = 0 ; i < indent ; i++) {
result.append(" ");
}
return result.toString();
}
//Very temporary kludge: the flat signature generation
//predates the SignatureLine class hierarchy. This hack
//returns size zero arrays, which doesn't break the sig
//generation
//public byte[] getData() {
syn byte[] SignatureLine.getData(int version) {
return new byte[0];
}
syn byte[] SignatureLine.getData(int version) = null;
// return new byte[0];
private Decl TypeRefSignatureLine.decl;
public TypeRefSignatureLine.TypeRefSignatureLine(int indent, Decl decl, String comment) {
......@@ -171,7 +158,7 @@ aspect Signature {
}
public void TypeDecl.genSigLineForDecl(SignatureList list, boolean decl) {
System.out.println("************ TypeDecl.genSigLine("+decl+").... for "+getName());
//System.out.println("************ TypeDecl.genSigLine("+decl+").... for "+getName());
if(decl){
getType().genSigLineForDecl(list, decl);
}else{
......@@ -180,7 +167,7 @@ aspect Signature {
}
public void SampleDecl.genSigLineForDecl(SignatureList list, boolean decl) {
System.out.println("************ SampleDecl.genSigLine("+decl+").... for "+getName());
//System.out.println("************ SampleDecl.genSigLine("+decl+").... for "+getName());
getType().genSigLineForDecl(list, decl);
}
......@@ -195,9 +182,9 @@ aspect Signature {
public void UserType.genSigLineForDecl(SignatureList list, boolean decl) {
if(decl){
System.out.println("************ UserType.genSigLine("+decl+").... for "+getName());
//System.out.println("************ UserType.genSigLine("+decl+").... for "+getName());
TypeDecl thet=lookupType(getName());
System.out.println("************ thet: "+thet.getName() +":"+thet.getType());
//System.out.println("************ thet: "+thet.getName() +":"+thet.getType());
thet.genSigLineForDecl(list, decl);
}else{
//System.out.println("************ UserType.genSigLine("+decl+").... for "+getName());
......
......@@ -100,7 +100,7 @@ run:
@echo "************ running python decoder (from wiki_example):"
PYTHONPATH=${LCDIR}/lib/python ../wiki_example/example_decoder.py encoded_data_p LabComm2014
runwcs: ExampleEncoder.exe ExampleDecoder.exe
runwcs: build ExampleEncoder.exe ExampleDecoder.exe
export LD_LIBRARY_PATH=${LCDIR}/lib/c/
@echo
@echo "********************************************"
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment