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

Merge branch 'master' into stable

Version 2013 and version 2006 working
parents 2d6b229a 073a15e1
*.class
*.o
lib/c/liblabcomm.a
lib/c/liblabcomm.so
lib/c/liblabcomm.so.1
lib/c/liblabcomm2006.so.1
lib/c/liblabcomm2006.so
lib/c/liblabcomm2006.a
lib/c/liblabcomm2013.so.1
lib/c/liblabcomm2013.so
lib/c/liblabcomm2013.a
lib/c/test/test_labcomm
lib/c/test/test_labcomm_basic_type_encoding
lib/c/test/test_labcomm_copy
lib/c/test/test_labcomm_generated_encoding
lib/c/test/test_labcomm_pthread_scheduler
lib/c/test/test_signature_numbers
compiler/AST/
compiler/labComm.jar
encoded_data
encoded_data06
gen
gen06
*.pyc
examples/twoway/gen/
lib/csharp/labcomm.dll
lib/java/gen/
lib/java/labcomm*.jar
......@@ -17,6 +17,7 @@ test: $(SUBDIRS:%=test-%)
.PHONY: test-compiler
test-compiler:
$(MAKE) -C test compiler_errors
.PHONY: test-%
test-%:
......
......@@ -8,12 +8,13 @@ aspect CS_CodeGenEnv {
public class CS_env {
public final int version;
private int indent;
private int depth;
private CS_printer printer;
private HashMap unique = new HashMap();
final private class CS_printer {
final private static class CS_printer {
private boolean newline = true;
private File file;
......@@ -75,19 +76,18 @@ aspect CS_CodeGenEnv {
}
}
private CS_env(int indent, CS_printer printer) {
private CS_env(int indent, CS_printer printer, int version) {
this.version = version;
this.indent = indent;
this.printer = printer;
}
public CS_env(File f) {
this.indent = 0;
this.printer = new CS_printer(f);
public CS_env(File f, int version) {
this(0, new CS_printer(f), version);
}
public CS_env(PrintStream out) {
this.indent = 0;
this.printer = new CS_printer(out);
public CS_env(PrintStream out, int version) {
this(0, new CS_printer(out), version);
}
public void close() throws IOException {
......@@ -196,9 +196,9 @@ aspect CS_Void {
aspect CS_CodeGen {
public void Program.CS_gen(String file,
String namespace) throws IOException {
String namespace, int version) throws IOException {
// Registration class
CS_env env = new CS_env(new File(file));
CS_env env = new CS_env(new File(file), version);
if (namespace != null && namespace.length() > 0) {
env.println("namespace " + namespace + "{");
env.indent();
......@@ -351,7 +351,7 @@ aspect CS_Class {
CS_emitDecoder(env);
env.println("private static byte[] signature = new byte[] {");
env.indent();
SignatureList signature = signature();
SignatureList signature = signature(env.version);
for (int i = 0 ; i < signature.size() ; i++) {
String comment = signature.getComment(i);
if (comment != null) {
......@@ -645,6 +645,10 @@ aspect CS_Class {
" not declared");
}
public boolean VoidType.CS_needInstance() {
return false;
}
public boolean PrimType.CS_needInstance() {
return false;
}
......@@ -752,8 +756,8 @@ aspect CS_Class {
aspect CS_Info {
public void Program.CS_info(PrintStream out, String namespace) {
CS_env env = new CS_env(out);
public void Program.CS_info(PrintStream out, String namespace, int version) {
CS_env env = new CS_env(out, version);
if (namespace == null) {
namespace = "";
} else {
......@@ -772,13 +776,15 @@ aspect CS_Info {
public void TypeDecl.CS_info(CS_env env, String namespace) {
env.print(";C#;typedef;" + namespace + getName() + ";");
getType().CS_emitType(env);
getType().CS_emitType(env) ;
env.print(";not_applicable_for_C#");
env.println();
}
public void SampleDecl.CS_info(CS_env env, String namespace) {
env.print(";C#;sample;" + namespace + getName() + ";");
getType().CS_emitType(env);
env.print(";not_applicable_for_C#");
env.println();
}
......
This diff is collapsed.
aspect DeclNames {
inh String Type.declName();
eq Decl.getType().declName() = getName();
inh String Field.declName();
eq StructType.getField(int i).declName() = declName();
}
......@@ -20,6 +20,7 @@ aspect ErrorCheck {
}
public void ASTNode.errorCheck(Collection collection) {
nameCheck();
typeCheck();
if(hasErrors())
collection.add(errors);
for(int i = 0; i < getNumChild(); i++) {
......
......@@ -8,6 +8,8 @@ aspect Java_CodeGenEnv {
public class Java_env {
public final int version; //labcomm version to generate code for
public final String verStr;
private int indent;
private int depth;
private Java_printer printer;
......@@ -75,18 +77,24 @@ aspect Java_CodeGenEnv {
}
}
private Java_env(int indent, Java_printer printer) {
private Java_env(int version, int indent) {
this.version = version;
this.verStr = (version == 2006) ? "2006" : "";
this.indent = indent;
}
private Java_env(int version, Java_printer printer) {
this(version, 0);
this.printer = printer;
}
public Java_env(File f) {
this.indent = 0;
public Java_env(int version, File f) {
this(version, 0);
this.printer = new Java_printer(f);
}
public Java_env(PrintStream out) {
this.indent = 0;
public Java_env(int version, PrintStream out) {
this(version, 0);
this.printer = new Java_printer(out);
}
......@@ -188,6 +196,7 @@ aspect Java_StructName {
aspect Java_Void {
syn boolean Decl.isVoid() = getType().isVoid();
syn boolean UserType.isVoid() = decl().isVoid();
syn boolean Type.isVoid() = false;
syn boolean VoidType.isVoid() = true;
......@@ -195,11 +204,11 @@ aspect Java_Void {
aspect Java_CodeGen {
public void Program.J_gen(PrintStream ps, String pack) throws IOException {
public void Program.J_gen(PrintStream ps, String pack, int version) throws IOException {
Java_env env;
/*
// Registration class
env = new Java_env(ps);
env = new Java_env(version, ps);
if (pack != null && pack.length() > 0) {
env.println("package " + pack + ";");
}
......@@ -212,7 +221,7 @@ aspect Java_CodeGen {
env.println("}");
// env.close();
*/
env = new Java_env(ps);
env = new Java_env(version, ps);
for (int i = 0; i < getNumDecl(); i++) {
Decl d = getDecl(i);
try {
......@@ -225,11 +234,11 @@ aspect Java_CodeGen {
env.close();
}
public void Program.J_gen(String dir, String pack) throws IOException {
public void Program.J_gen(String dir, String pack, int version) throws IOException {
Java_env env;
/*
// Registration class
env = new Java_env(new File(dir, "LabCommRegister.java"));
env = new Java_env(version, new File(dir, "LabCommRegister.java"));
if (pack != null && pack.length() > 0) {
env.println("package " + pack + ";");
}
......@@ -245,7 +254,7 @@ aspect Java_CodeGen {
for (int i = 0; i < getNumDecl(); i++) {
Decl d = getDecl(i);
try {
env = new Java_env(new File(dir, d.getName() + ".java"));
env = new Java_env(version, new File(dir, d.getName() + ".java"));
d.Java_emitClass(env, pack);
env.close();
} catch (Error e) {
......@@ -257,7 +266,7 @@ aspect Java_CodeGen {
/** Experimental method for generating code to a map <classname, source>
*/
public void Program.J_gen(Map<String,String> src, String pack) throws IOException {
public void Program.J_gen(Map<String,String> src, String pack, int version) throws IOException {
Java_env env;
/*
// Registration class was commented out, so got removed in this copy
......@@ -267,7 +276,7 @@ aspect Java_CodeGen {
try {
ByteArrayOutputStream bs = new ByteArrayOutputStream();
PrintStream out = new PrintStream(bs);
env = new Java_env(out);
env = new Java_env(version, out);
d.Java_emitClass(env, pack);
env.close();
src.put(d.getName(), bs.toString());
......@@ -331,9 +340,9 @@ aspect Java_Class {
}
env.println("import java.io.IOException;");
env.println("import se.lth.control.labcomm.LabCommType;");
env.println("import se.lth.control.labcomm.LabCommEncoder;");
env.println("import se.lth.control.labcomm.LabCommDecoder;");
env.println("import se.lth.control.labcomm"+env.verStr+".LabCommType;");
env.println("import se.lth.control.labcomm"+env.verStr+".LabCommEncoder;");
env.println("import se.lth.control.labcomm"+env.verStr+".LabCommDecoder;");
env.println();
env.println("public class " + getName() + " implements LabCommType {");
env.println();
......@@ -371,11 +380,11 @@ aspect Java_Class {
}
env.println("import java.io.IOException;");
env.println("import se.lth.control.labcomm.LabCommDecoder;");
env.println("import se.lth.control.labcomm.LabCommDispatcher;");
env.println("import se.lth.control.labcomm.LabCommEncoder;");
env.println("import se.lth.control.labcomm.LabCommHandler;");
env.println("import se.lth.control.labcomm.LabCommSample;");
env.println("import se.lth.control.labcomm"+env.verStr+".LabCommDecoder;");
env.println("import se.lth.control.labcomm"+env.verStr+".LabCommDispatcher;");
env.println("import se.lth.control.labcomm"+env.verStr+".LabCommEncoder;");
env.println("import se.lth.control.labcomm"+env.verStr+".LabCommHandler;");
env.println("import se.lth.control.labcomm"+env.verStr+".LabCommSample;");
env.println();
env.print("public class " + getName());
// if(getType().isUserType()) {
......@@ -449,7 +458,7 @@ aspect Java_Class {
Java_emitDecoder(env);
env.println("private static byte[] signature = new byte[] {");
env.indent();
SignatureList signature = signature();
SignatureList signature = signature(env.version);
for (int i = 0 ; i < signature.size() ; i++) {
String comment = signature.getComment(i);
if (comment != null) {
......@@ -738,6 +747,10 @@ aspect Java_Class {
" not declared");
}
public boolean VoidType.Java_needInstance() {
return false;
}
public boolean PrimType.Java_needInstance() {
return false;
}
......@@ -841,8 +854,8 @@ aspect Java_Class {
aspect Java_Info {
public void Program.Java_info(PrintStream out) {
Java_env env = new Java_env(out);
public void Program.Java_info(PrintStream out, int version) {
Java_env env = new Java_env(version, out);
for (int i = 0; i < getNumDecl(); i++) {
getDecl(i).Java_info(env);
}
......@@ -856,13 +869,15 @@ aspect Java_Info {
public void TypeDecl.Java_info(Java_env env) {
env.print(",Java,typedef," + getName() + ",");
getType().Java_emitType(env);
getType().Java_emitType(env);
env.print(",not_applicable_for_Java");
env.println();
}
public void SampleDecl.Java_info(Java_env env) {
env.print(",Java,sample," + getName() + ",");
getType().Java_emitType(env);
env.print(",not_applicable_for_Java");
env.println();
}
......
......@@ -12,7 +12,7 @@ PrimType : Type ::= <Name:String> <Token:int>;
UserType : Type ::= <Name:String>;
StructType : Type ::= Field*;
ParseArrayType : Type ::= Type Dim*;
abstract ArrayType : Type ::= Type Exp*;
abstract ArrayType :Type ::= Type Exp*;
VariableArrayType : ArrayType;
FixedArrayType : ArrayType;
......
......@@ -13,6 +13,7 @@ public class LabComm {
println("");
println(" --help Shows this help text");
println(" -v Be verbose");
println(" --ver=VERSION Generate code for labcomm VERSION (=2006 or 2013)");
println("[ C options ]");
println(" -C Generates C/H code in FILE.[ch]");
println(" --cprefix=PREFIX Prefixes C types with PREFIX");
......@@ -29,224 +30,32 @@ 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 Pretty prints on standard output");
println(" --typeinfo=TIFILE Generates typeinfo in TIFILE");
}
private static String getCoreName(String s) {
int i = s.lastIndexOf('.');
return s.substring(0, i > 0 ? i : s.length());
}
private static String getFileName(String s) {
return s.substring(s.lastIndexOf('/') + 1, s.length());
}
private static String getBaseName(String s) {
s = getFileName(s);
int i = s.lastIndexOf('.');
return s.substring(0, i > 0 ? i : s.length());
}
private static String getPrefix(String s) {
return s.substring(s.lastIndexOf('/') + 1, s.length());
}
public static void main(String[] args) {
String fileName = null;
// Scan for first non-option
for (int i = 0 ; i < args.length ; i++) {
if (! args[i].startsWith("-")) {
fileName = args[i];
break;
}
}
String coreName = null;
String prefix = null;
if (fileName != null) {
coreName = getBaseName(fileName);
prefix = getPrefix(coreName);
}
boolean verbose = false;
String cFile = null;
String hFile = null;
Vector cIncludes = new Vector();
String cPrefix = prefix;
String csFile = null;
String csNamespace = null;
String javaDir = null;
String javaPackage = "";
String pythonFile = null;
String prettyFile = null;
String typeinfoFile = null;
String rapidFile = null;
for (int i = 0 ; i < args.length ; i++) {
if (fileName == null ||
args[i].equals("-help") ||
args[i].equals("-h") ||
args[i].equals("--help")) {
print_help();
System.exit(0);
} else if (args[i].equals("-v")) {
verbose=true;
} else if (args[i].equals("-C")) {
cFile = coreName + ".c";
hFile = coreName + ".h";
} else if (args[i].startsWith("--cinclude=")) {
cIncludes.add(args[i].substring(11));
} else if (args[i].startsWith("--cprefix=")) {
cPrefix = args[i].substring(10);
} else if (args[i].startsWith("--c=")) {
cFile = args[i].substring(4);
} else if (args[i].startsWith("--h=")) {
hFile = args[i].substring(4);
} else if (args[i].equals("--cs")) {
csFile = coreName + ".cs";
} else if (args[i].startsWith("--cs=")) {
csFile = args[i].substring(5);
} else if (args[i].startsWith("--csnamespace=")) {
csNamespace = args[i].substring(14);
} else if (args[i].startsWith("--java=")) {
javaDir = args[i].substring(7);
} else if (args[i].startsWith("--javapackage=")) {
javaPackage = args[i].substring(14);
} else if (args[i].equals("-P")) {
pythonFile = coreName + ".py";
} else if (args[i].startsWith("--python=")) {
pythonFile = args[i].substring(9);
} else if (args[i].startsWith("--pretty=")) {
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 {
System.err.println(" Unknown argument " + args[i]);
print_help();
/** To be cleaned up.
*/
private static void checkVersion(int v) {
if(! (v == 2006 || v == 2013) ) {
System.err.println(" Unknown version: " + v);
System.err.println(" Supported versions: 2006, 2013 ");
System.exit(2);
}
}
if (fileName == null) {
print_help();
System.exit(1);
} else {
Program ast = null;
try {
// Check for errors
LabCommScanner scanner = new LabCommScanner(new FileReader(fileName));
LabCommParser parser = new LabCommParser();
Program p = (Program)parser.parse(scanner);
Collection errors = new LinkedList();
p.errorCheck(errors);
if (errors.isEmpty()) {
ast = p;
} else {
for (Iterator iter = errors.iterator(); iter.hasNext(); ) {
String s = (String)iter.next();
System.out.println(s);
}
}
} catch (FileNotFoundException e) {
System.err.println("Could not find file: " + fileName);
} catch (IOException e) {
System.err.println("IOException: " + fileName + " " + e);
} catch (beaver.Parser.Exception e) {
System.err.println(e.getMessage());
}
if (ast != null) {
Vector hIncludes = new Vector(cIncludes);
if (hFile != null) {
cIncludes.add(hFile);
}
boolean prettyOnStdout = true;
if (cFile != null) {
if (verbose) { System.err.println("Generating C: " + cFile); }
genC(ast, cFile, cIncludes, coreName, cPrefix);
prettyOnStdout = false;
}
if (hFile != null) {
if (verbose) { System.err.println("Generating H: " + hFile); }
genH(ast, hFile, hIncludes, coreName, cPrefix);
prettyOnStdout = false;
}
if (csFile != null) {
if (verbose) { System.err.println("Generating C#: " + csFile); }
genCS(ast, csFile, csNamespace);
prettyOnStdout = false;
}
if (javaDir != null) {
if (verbose) { System.err.println("Generating Java: " + javaDir); }
genJava(ast, javaDir, javaPackage);
prettyOnStdout = false;
}
if (pythonFile != null) {
if (verbose) {
System.err.println("Generating Python: " + pythonFile);
}
genPython(ast, pythonFile, prefix);
prettyOnStdout = false;
}
if (rapidFile != null) {
if (verbose) {
System.err.println("Generating RAPID: " + rapidFile);
}
genRAPID(ast, rapidFile, coreName);
prettyOnStdout = false;
}
if (prettyFile != null) {
if (verbose) {
System.err.println("Generating Pretty: " + prettyFile);
}
try {
FileOutputStream f = new FileOutputStream(prettyFile);
PrintStream out = new PrintStream(f);
ast.pp(out);
out.close();
prettyOnStdout = false;
} catch (IOException e) {
System.err.println("IOException: " + prettyFile + " " + e);
}
}
if (typeinfoFile != null) {
if (verbose) {
System.err.println("Generating TypeInfo: " + typeinfoFile);
}
try {
FileOutputStream f = new FileOutputStream(typeinfoFile);
PrintStream out = new PrintStream(f);
ast.C_info(out, cPrefix);
ast.Java_info(out);
ast.CS_info(out, csNamespace);
prettyOnStdout = false;
} catch (IOException e) {
System.err.println("IOException: " + typeinfoFile + " " + e);
}
}
if (prettyOnStdout) {
ast.pp(System.out);
}
}
}
}
}
}
private static void genH(Program p, String hName,
Vector cIncludes, String coreName, String prefix) {
Vector cIncludes, String coreName, String prefix, int ver) {
try {
FileOutputStream f;
PrintStream out;
f = new FileOutputStream(hName);
out = new PrintStream(f);
p.C_genH(out, cIncludes, coreName, prefix);
p.C_genH(out, cIncludes, coreName, prefix, ver);
out.close();
} catch (IOException e) {
System.err.println("IOException: " + hName + " " + e);
......@@ -254,59 +63,340 @@ public class LabComm {
}
private static void genC(Program p, String cName,
Vector cIncludes, String coreName, String prefix) {
Vector cIncludes, String coreName, String prefix, int ver) {
try {
FileOutputStream f;
PrintStream out;
f = new FileOutputStream(cName);
out = new PrintStream(f);
p.C_genC(out, cIncludes, coreName, prefix);
p.C_genC(out, cIncludes, coreName, prefix, ver);
out.close();
} catch (IOException e) {
System.err.println("IOException: " + cName + " " + e);
}
}
private static void genCS(Program p, String csName, String csNamespace) {
private static void genCS(Program p, String csName, String csNamespace, int ver) {
try {
p.CS_gen(csName, csNamespace);
p.CS_gen(csName, csNamespace, ver);
} catch (IOException e) {
System.err.println("IOException: " + csName + " " +
csNamespace + " " + e);
}
}
private static void genJava(Program p, String dirName, String packageName) {
private static void genJava(Program p, String dirName, String packageName, int ver) {
try {
p.J_gen(dirName, packageName);
p.J_gen(dirName, packageName, ver);
} catch (IOException e) {
System.err.println("IOException: " + dirName + " " +
packageName + " " + e);
}
}
<