Commit e7848681 authored by Anders Blomdell's avatar Anders Blomdell
Browse files

More cleanups

parent 05c5fb26
......@@ -206,7 +206,7 @@ aspect CS_CodeGen {
env.indent();
}
env.println("using System;");
env.println("using se.lth.control.labcomm;");
env.println("using se.lth.control.labcomm2006;");
for (int i = 0; i < getNumDecl(); i++) {
Decl d = getDecl(i);
try {
......
......@@ -30,8 +30,6 @@ public class LabComm {
println("[ Python options ]");
println(" -P Generates Python code in FILE.py");
println(" --python=PFILE Generates Python code in PFILE");
println("[ RAPID options ]");
println(" --rapid Generates RAPID code in FILE.sys");
println("[ Misc options ]");
println(" --pretty=PFILE Pretty prints to PFILE");
println(" --typeinfo=TIFILE Generates typeinfo in TIFILE");
......@@ -68,7 +66,6 @@ public class LabComm {
}
private static void genCS(Program p, String csName, String csNamespace) {
// throw new Error("C# generation currently disabled");
try {
p.CS_gen(csName, csNamespace);
} catch (IOException e) {
......@@ -100,14 +97,6 @@ public class LabComm {
}
}
private static void genRAPID(Program p, String filename, String prefix) {
try {
p.RAPID_gen(filename, prefix);
} catch (IOException e) {
System.err.println("IOException: " + filename + " " + e);
}
}
/** Helper class to contain command line options
and their associated behaviour
**/
......@@ -127,7 +116,6 @@ public class LabComm {
String pythonFile = null;
String prettyFile = null;
String typeinfoFile = null;
String rapidFile = null;
String fileName = null;
Opts(String[] args) {
......@@ -208,8 +196,6 @@ public class LabComm {
prettyFile = args[i].substring(9);
} else if (args[i].startsWith("--typeinfo=")) {
typeinfoFile = args[i].substring(11);
} else if (args[i].equals("--rapid")) {
rapidFile = coreName + ".sys";
} else if (i == args.length - 1) {
fileName = args[i];
} else {
......@@ -302,15 +288,6 @@ public class LabComm {
return wroteFile;
}
boolean generateRAPID(Program ast) {
boolean wroteFile = false;
if (rapidFile != null) {
printStatus("RAPID: " , rapidFile);
genRAPID(ast, rapidFile, coreName);
wroteFile = true;
}
return wroteFile;
}
boolean generatePrettyPrint(Program ast) {
boolean wroteFile = false;
if (prettyFile != null) {
......@@ -371,7 +348,6 @@ public class LabComm {
fileWritten |= opts.generateCS(ast);
fileWritten |= opts.generateJava(ast);
fileWritten |= opts.generatePython(ast);
fileWritten |= opts.generateRAPID(ast);
fileWritten |= opts.generatePrettyPrint(ast);
fileWritten |= opts.generateTypeinfo(ast);
......
......@@ -77,8 +77,7 @@ aspect Python_CodeGen {
env.println("#!/usr/bin/python");
env.println("# Auto generated " + baseName);
env.println();
env.println("import labcomm");
env.println("import StringIO");
env.println("import labcomm2006");
env.println();
Python_genTypes(env);
env.println("sample = tuple([");
......@@ -112,7 +111,7 @@ aspect PythonTypes {
public void SampleDecl.Python_genSignature(Python_env env) {
env.println("class " + getName() + "(object):");
env.indent();
env.println("signature = labcomm.sample('" + getName() + "', ");
env.println("signature = labcomm2006.sample('" + getName() + "', ");
env.indent();
getType().Python_genSignature(env);
env.unindent();
......@@ -133,20 +132,20 @@ 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("labcomm2006.BOOLEAN()"); } break;
case LABCOMM_BYTE: { env.print("labcomm2006.BYTE()"); } break;
case LABCOMM_SHORT: { env.print("labcomm2006.SHORT()"); } break;
case LABCOMM_INT: { env.print("labcomm2006.INTEGER()"); } break;
case LABCOMM_LONG: { env.print("labcomm2006.LONG()"); } break;
case LABCOMM_FLOAT: { env.print("labcomm2006.FLOAT()"); } break;
case LABCOMM_DOUBLE: { env.print("labcomm2006.DOUBLE()"); } break;
case LABCOMM_STRING: { env.print("labcomm2006.STRING()"); } break;
case LABCOMM_SAMPLE: { env.print("labcomm2006.SAMPLE()"); } break;
}
}
public void ArrayType.Python_genSignature(Python_env env) {
env.print("labcomm.array([");
env.print("labcomm2006.array([");
for (int i = 0 ; i < getNumExp() ; i++) {
if (i > 0) { env.print(", "); }
env.print(getExp(i).Python_getValue());
......@@ -159,7 +158,7 @@ aspect PythonTypes {
}
public void StructType.Python_genSignature(Python_env env) {
env.println("labcomm.struct([");
env.println("labcomm2006.struct([");
env.indent();
for (int i = 0 ; i < getNumField() ; i++) {
if (i > 0) { env.println(","); }
......@@ -170,7 +169,7 @@ aspect PythonTypes {
}
public void VoidType.Python_genSignature(Python_env env) {
env.println("labcomm.struct([])");
env.println("labcomm2006.struct([])");
}
public void Field.Python_genSignature(Python_env env) {
......
aspect RAPID_env {
public class RAPID_env {
private String prefix;
private StringBuilder types;
private StringBuilder constants;
private StringBuilder procedures;
private PrintStream ps;
public RAPID_env(PrintStream ps, String prefix)
{
this.types = new StringBuilder();
this.constants = new StringBuilder();
this.procedures = new StringBuilder();
this.prefix = prefix;
this.ps = ps;
}
public String prefix() { return this.prefix; }
public String addRecord(String name, java.util.List<String> components)
{
String recordName = this.prefix + "_" + name;
types.append("\tRECORD " + recordName);
types.append("\n");
for (String c : components) {
types.append("\t\t" + c + "\n");
}
types.append("\tENDRECORD");
types.append("\n\n");
return recordName;
}
public void addConstant(String type, String name, String value) {
this.constants.append("\tLOCAL CONST " + type + " " + name +
" := " + value + ";\n");
}
public void addProc(String name, java.util.List<String> params,
java.util.List<String> stmts)
{
this.procedures.append("\tLOCAL PROC " + name + "(");
for (int i = 0; i < params.size(); i++) {
this.procedures.append(params.get(i));
if (i < params.size() - 1) {
this.procedures.append(", ");
}
}
this.procedures.append(")\n");
for (String stmt : stmts) {
this.procedures.append("\t\t" + stmt + "\n");
}
this.procedures.append("\tERROR\n\t\tRAISE ;\n\tENDPROC\n\n");
}
public void flush()
{
ps.println("MODULE " + prefix() + "(SYSMODULE)");
ps.println();
ps.print(types.toString());
ps.println();
ps.println("\tLOCAL CONST string prefix:=\"" + this.prefix + "\";");
ps.print(constants.toString());
ps.println();
ps.print(procedures.toString());
ps.println();
ps.print("ENDMODULE");
}
}
}
aspect RAPID_CodeGen {
public void ASTNode.RAPID_gen(RAPID_env env) {
throw new UnsupportedOperationException();
}
public void Program.RAPID_gen(String file, String prefix)
throws IOException
{
PrintStream ps = new PrintStream(new FileOutputStream(new File(file)));
RAPID_env env = new RAPID_env(ps, prefix);
RAPID_gen(env);
}
public void Program.RAPID_gen(RAPID_env env)
{
for (int i = 0; i < getNumDecl(); i++) {
getDecl(i).RAPID_gen(env);
}
env.flush();
}
public void Decl.RAPID_gen(RAPID_env env) {
throw new UnsupportedOperationException("RAPID code generation (currently) does not support "+getClass().getSimpleName());
}
public void TypeDecl.RAPID_gen(RAPID_env env) {
System.out.println("***WARNING! TypeDecl.RapidGen(.) a NOP after sig reorganization.");
System.out.println(" (Tell a developer to) remove this warning when tested");
}
public void Decl.RAPID_emitFlatSignature(RAPID_env env, String sig_len_name, String sig_name) {
System.out.println("***WARNING! Code not tested after reorganization of signatures.");
System.out.println(" (Tell a developer to) remove this warning when tested");
SignatureList sig = flatSignature(2006);
StringBuilder sb = new StringBuilder();
sb.append("[");
byte[] d = null;
int sig_len = 0;
for (int i = 0; i < sig.size(); i++) {
d = sig.getData(i, 2006);
for (int j = 0; d != null && j < d.length; j++) {
sb.append(d[j] + ",");
sig_len++;
}
}
sb.delete(sb.length() - 1, sb.length());
sb.append("]");
env.addConstant("num", sig_len_name, "" + sig_len);
env.addConstant("byte", sig_name + "{" + sig_len_name + "}",
sb.toString());
}
public void SampleDecl.RAPID_gen(RAPID_env env) {
// Add type declarations
String fullName = getType().RAPID_AddType(env, getName());
// Add signature constants
String sig_len_name = "signature_len_" + getName();
String sig_name = "signature_" + getName();
RAPID_emitFlatSignature(env, sig_len_name, sig_name);
// Add decode procedures
ArrayList<String> params = new ArrayList<String>();
ArrayList<String> stmts = new ArrayList<String>();
params.add("VAR LabComm_Decoder_Sample s");
params.add("string handler");
stmts.add("s.prefix := prefix;");
stmts.add("s.name := \"" + getName() + "\";");
stmts.add("s.handler := handler;");
env.addProc("Dec_Reg_" + getName(), params, stmts);
params.clear();
stmts.clear();
params.add("VAR LabComm_Decoder_Sample s");
params.add("VAR rawbytes sig");
params.add("num user_id");
stmts.add("VAR byte tmp_sig{" + sig_len_name + "};");
stmts.add("IF RawBytesLen(sig)<>" + sig_len_name + " THEN");
stmts.add("\tRETURN;");
stmts.add("ENDIF");
stmts.add("FOR i FROM 1 TO " + sig_len_name + " DO");
stmts.add("\tUnpackRawBytes sig, i, tmp_sig{i}, \\Hex1;");
stmts.add("ENDFOR");
stmts.add("IF tmp_sig<>" + sig_name + " THEN");
stmts.add("\tRETURN;");
stmts.add("ENDIF");
stmts.add("s.user_id := user_id;");
env.addProc("Reg_If_Signature_Of_" + getName(), params, stmts);
params.clear();
stmts.clear();
params.add("VAR Decoder d");
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");
stmts.add("% s.handler % tmp;");
env.addProc("Decode_And_Handle_" + getName(), params, stmts);
params.clear();
stmts.clear();
params.add("VAR Encoder e");
params.add("VAR LabComm_Stream st");
params.add("VAR LabComm_Encoder_Sample s");
stmts.add("s.prefix := prefix;");
stmts.add("s.name := \"" + getName() + "\";");
stmts.add("Encoder_Register_Sample e, st, s;");
env.addProc("Enc_Reg_" + getName(), params, stmts);
params.clear();
stmts.clear();
params.add("VAR Encoder e");
params.add("VAR LabComm_Stream s");
stmts.add("VAR rawbytes buffer;");
stmts.add("FOR i FROM 1 TO " + sig_len_name + " DO");
stmts.add("\tPackRawBytes " + sig_name +
"{i}, buffer, \\Network, i, \\Hex1;");
stmts.add("ENDFOR");
stmts.add("SocketSend s.soc, \\RawData:=buffer, \\NoOfBytes:=" +
sig_len_name + ";");
env.addProc("Encode_Signature_" + getName(), params, stmts);
params.clear();
stmts.clear();
params.add("VAR Encoder e");
params.add("VAR LabComm_Stream st");
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");
env.addProc("Encode_" + getName(), params, stmts);
}
public String Type.RAPID_AddType(RAPID_env env, String name) {
throw new UnsupportedOperationException("RAPID code generation does (currently) not support "+getClass().getSimpleName());
}
public String StructType.RAPID_AddType(RAPID_env env, String name) {
ArrayList<String> components = new ArrayList<String>();
for (int i = 0; i < getNumField(); i++) {
Field f = getField(i);
components.add(
f.getType().RAPID_AddType(env, name + "_" + f.getName()) +
" " + f.getName() + ";");
}
String typeName = env.addRecord(name, components);
return typeName;
}
public String FixedArrayType.RAPID_AddType(RAPID_env env, String name) {
String typeName = getType().RAPID_AddType(env, name + "_e");
if (getNumExp() > 1) {
throw new UnsupportedOperationException("RAPID generation only (currently) supports one-dimensional arrays");
}
ArrayList<String> components = new ArrayList<String>();
for (int i = 1; i <= getExp(0).RAPID_getValue(); i++) {
components.add(typeName + " e" + i + ";");
}
String completeName = env.addRecord("list_" + name, components);
return completeName;
}
public String PrimType.RAPID_AddType(RAPID_env env, String name) {
if (getToken() == LABCOMM_SHORT ||
getToken() == LABCOMM_FLOAT ||
getToken() == LABCOMM_INT) {
return "num";
} else if (getToken() == LABCOMM_LONG) {
return "dnum";
} else if (getToken() == LABCOMM_STRING) {
return "string";
} else if (getToken() == LABCOMM_BOOLEAN) {
return "bool";
} else if (getToken() == LABCOMM_BYTE) {
return "byte";
}
throw new UnsupportedOperationException("RAPID code generation does not (currently) support "+getName());
}
public void Type.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());
}
public void StructType.RAPID_AddDecodeInstr(RAPID_env env,
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,
var_name + "." + getField(i).getName(), stream_name);
}
}
public void FixedArrayType.RAPID_AddDecodeInstr(RAPID_env env,
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,
var_name + ".e" + i, stream_name);
}
}
public void PrimType.RAPID_AddDecodeInstr(RAPID_env env,
java.util.List<String> instrs,
String var_name, String stream_name) {
switch(getToken()) {
case LABCOMM_BYTE:
instrs.add("Decode_Byte " + stream_name + "," + var_name + ";");
break;
case LABCOMM_BOOLEAN:
instrs.add("Decode_Bool " + stream_name + "," + var_name + ";");
break;
case LABCOMM_SHORT:
instrs.add("Decode_Short " + stream_name + "," + var_name + ";");
break;
case LABCOMM_INT:
instrs.add("Decode_Int " + stream_name + "," + var_name + ";");
break;
case LABCOMM_LONG:
instrs.add("Decode_Long " + stream_name + "," + var_name + ";");
break;
case LABCOMM_FLOAT:
instrs.add("Decode_Float " + stream_name + "," + var_name + ";");
break;
case LABCOMM_STRING:
instrs.add("Decode_String " + stream_name + "," + var_name + ";");
break;
default:
throw new UnsupportedOperationException("RAPID code generation does not (currently) support "+getName());
}
}
public void Type.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());
}
public void StructType.RAPID_AddEncodeInstr(RAPID_env env,
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,
var_name + "." + getField(i).getName(), stream_name);
}
}
public void FixedArrayType.RAPID_AddEncodeInstr(RAPID_env env,
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,
var_name + ".e" + i, stream_name);
}
}
public void PrimType.RAPID_AddEncodeInstr(RAPID_env env,
java.util.List<String> instrs,
String var_name, String stream_name) {
switch(getToken()) {
case LABCOMM_BYTE:
instrs.add("Encode_Byte " + stream_name + "," + var_name + ";");
break;
case LABCOMM_BOOLEAN:
instrs.add("Encode_Bool " + stream_name + "," + var_name + ";");
break;
case LABCOMM_SHORT:
instrs.add("Encode_Short " + stream_name + "," + var_name + ";");
break;
case LABCOMM_INT:
instrs.add("Encode_Int " + stream_name + "," + var_name + ";");
break;
case LABCOMM_LONG:
instrs.add("Encode_Long " + stream_name + "," + var_name + ";");
break;
case LABCOMM_FLOAT:
instrs.add("Encode_Float " + stream_name + "," + var_name + ";");
break;
case LABCOMM_STRING:
instrs.add("Encode_String " + stream_name + "," + var_name + ";");
break;
default:
throw new UnsupportedOperationException("RAPID code generation does not (currently) support "+getName());
}
}
public int Exp.RAPID_getValue() {
throw new UnsupportedOperationException("RAPID code generation does not (currently) support "+getClass().getSimpleName());
}
public int IntegerLiteral.RAPID_getValue() {
return Integer.parseInt(getValue());
}
}
aspect Python_CodeGenEnv {
// Environment wrapper for Python code generation
// 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;
}
}
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);
}
}
}
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);
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();
Python_genTypes(env);
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("])");
}
}
aspect PythonTypes {
public void Program.Python_genTypes(Python_env env) {
for (int i = 0 ; i < getNumDecl() ; i++) {
getDecl(i).Python_genSignature(env);
}
}
public void Decl.Python_genSignature(Python_env env) {
throw new Error(this.getClass().getName() +
".Python_genSignature(Python_env env)" +
" not declared");
}