From 81a9149bcefc262ac33ab2d2ecbb39d3ef053bf0 Mon Sep 17 00:00:00 2001
From: Sven Gestegard Robertz <sven.robertz@cs.lth.se>
Date: Fri, 7 Feb 2014 12:41:37 +0100
Subject: [PATCH] version 2013 still works, version 2006 untested

---
 compiler/CS_CodeGen.jrag     | 26 ++++++++--------
 compiler/C_CodeGen.jrag      | 39 ++++++++++--------------
 compiler/Java_CodeGen.jrag   | 38 +++++++++++++----------
 compiler/LabComm.java        | 59 ++++++++++++++++++++++--------------
 compiler/Python_CodeGen.jrag |  7 +++--
 compiler/RAPID_CodeGen.jrag  | 10 +++---
 compiler/Signature.jrag      | 45 ++++++++++++++++++---------
 7 files changed, 129 insertions(+), 95 deletions(-)

diff --git a/compiler/CS_CodeGen.jrag b/compiler/CS_CodeGen.jrag
index 23bda60..9c6278a 100644
--- a/compiler/CS_CodeGen.jrag
+++ b/compiler/CS_CodeGen.jrag
@@ -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) {
@@ -752,8 +752,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 {
diff --git a/compiler/C_CodeGen.jrag b/compiler/C_CodeGen.jrag
index 008bf70..4531c86 100644
--- a/compiler/C_CodeGen.jrag
+++ b/compiler/C_CodeGen.jrag
@@ -8,7 +8,7 @@ aspect C_CodeGenEnv {
 
   public class C_env {
 
-    final private class C_printer {
+    final private static class C_printer {
       
       private boolean newline = true;
       private PrintStream out;
@@ -33,6 +33,8 @@ aspect C_CodeGenEnv {
       }
     }
 
+    public final int version; //labcomm version (2006 or 2013)
+
     public final String qualid;
     public final String lcName;
     public final String rawPrefix;
@@ -42,7 +44,8 @@ aspect C_CodeGenEnv {
     private C_printer printer;
 
     private C_env(String qualid, String lcName, String rawPrefix, 
-		  int indent, int depth, C_printer printer) {
+		  int indent, int depth, C_printer printer, int version) {
+      this.version = version;
       this.qualid = qualid;
       this.lcName = lcName;
       this.rawPrefix = rawPrefix;
@@ -57,28 +60,18 @@ aspect C_CodeGenEnv {
     }
 
     public C_env(String qualid, String lcName, String rawPrefix, 
-		 PrintStream out) {
-      this.qualid = qualid;
-      this.lcName = lcName;
-      this.rawPrefix = rawPrefix;
-      if (rawPrefix.equals("")) {
-        this.prefix = rawPrefix;
-      } else {
-        this.prefix = rawPrefix + "_";
-      }
-      this.depth = 0;
-      this.indent = 0;
-      this.printer = new C_printer(out);
+		 PrintStream out, int version) {
+      this(qualid, lcName, rawPrefix, 0, 0, new C_printer(out), version);
     }
 
     public C_env nestArray(String suffix) {
       return new C_env(qualid + suffix, lcName, rawPrefix, 
-		       indent, depth + 1, printer);
+		       indent, depth + 1, printer, version);
     }
 
     public C_env nestStruct(String suffix) {
       return new C_env(qualid + suffix, lcName, rawPrefix, 
-		       indent, depth, printer);
+		       indent, depth, printer, version);
     }
 
     public void indent() {
@@ -147,8 +140,8 @@ aspect C_IsDynamic {
 aspect C_CodeGen {
 
   public void Program.C_genH(PrintStream out, Vector includes, 
-			     String lcName, String prefix) {
-    C_env env = new C_env("", lcName, prefix, out);
+			     String lcName, String prefix, int version) {
+    C_env env = new C_env("", lcName, prefix, out, version);
 
     // Hackish prettyprint preamble
     out.println("/* LabComm declarations:");
@@ -174,8 +167,8 @@ aspect C_CodeGen {
   }
 
   public void Program.C_genC(PrintStream out, Vector includes, 
-			     String lcName, String prefix) {
-    C_env env = new C_env("", lcName, prefix, out);
+			     String lcName, String prefix, int version) {
+    C_env env = new C_env("", lcName, prefix, out, version);
 
     // Include
     env.println("#include \"labcomm.h\"");
@@ -882,7 +875,7 @@ aspect C_Signature {
   public void SampleDecl.C_emitSignature(C_env env) {
     env.println("static unsigned char signature_bytes_" + 
 		       env.prefix + getName() + "[] = {");
-    SignatureList signature = signature();
+    SignatureList signature = signature(env.version);
     for (int i = 0 ; i < signature.size() ; i++) {
       String comment = signature.getComment(i);
       if (comment != null) {
@@ -1146,8 +1139,8 @@ aspect C_forAll {
 
 aspect C_Info {
 
-  public void Program.C_info(PrintStream out, String prefix) {
-    C_env env = new C_env("", "", prefix, out);
+  public void Program.C_info(PrintStream out, String prefix, int version) {
+    C_env env = new C_env("", "", prefix, out, version);
     for (int i = 0; i < getNumDecl(); i++) {
       getDecl(i).C_info(env);
     }
diff --git a/compiler/Java_CodeGen.jrag b/compiler/Java_CodeGen.jrag
index b0ff598..e3bdc22 100644
--- a/compiler/Java_CodeGen.jrag
+++ b/compiler/Java_CodeGen.jrag
@@ -8,6 +8,7 @@ aspect Java_CodeGenEnv {
 
   public class Java_env {
 
+    public final int version;  //labcomm version to generate code for
     private int indent;
     private int depth;
     private Java_printer printer;
@@ -75,18 +76,23 @@ aspect Java_CodeGenEnv {
       }
     }
 
-    private Java_env(int indent, Java_printer printer) {
+    private Java_env(int version, int indent) {
+      this.version = version; 
       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);
     }
 
@@ -195,11 +201,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 +218,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 +231,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 +251,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 +263,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 +273,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());
@@ -449,7 +455,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) {
@@ -841,8 +847,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);
     }
diff --git a/compiler/LabComm.java b/compiler/LabComm.java
index c5a59b8..c1c7251 100644
--- a/compiler/LabComm.java
+++ b/compiler/LabComm.java
@@ -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");
@@ -53,6 +54,16 @@ public class LabComm {
     return s.substring(s.lastIndexOf('/') + 1, s.length());
   }
 
+  /** 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);
+     }
+  }
+
   public static void main(String[] args) {
     String fileName = null;
     // Scan for first non-option
@@ -69,6 +80,7 @@ public class LabComm {
       prefix = getPrefix(coreName);
     }
     boolean verbose = false;
+    int ver = 2013; //Version 2013 as default
     String cFile = null;
     String hFile = null;
     Vector cIncludes = new Vector();
@@ -91,6 +103,9 @@ public class LabComm {
 	System.exit(0);
       } else if (args[i].equals("-v")) {
 	verbose=true;
+      } else if (args[i].startsWith("--ver=")) {
+	ver = Integer.parseInt(args[i].substring(6));
+        checkVersion(ver);
       } else if (args[i].equals("-C")) {
 	cFile = coreName + ".c";
 	hFile = coreName + ".h";
@@ -170,36 +185,36 @@ public class LabComm {
 	boolean prettyOnStdout = true;
 	if (cFile != null) {
 	  if (verbose) { System.err.println("Generating C: " + cFile); }
-	  genC(ast, cFile, cIncludes, coreName, cPrefix);
+	  genC(ast, cFile, cIncludes, coreName, cPrefix, ver);
 	  prettyOnStdout = false;
 	}
 	if (hFile != null) {
 	  if (verbose) { System.err.println("Generating H: " + hFile); }
-	  genH(ast, hFile, hIncludes, coreName, cPrefix);
+	  genH(ast, hFile, hIncludes, coreName, cPrefix, ver);
 	  prettyOnStdout = false;
 	}
 	if (csFile != null) {
 	  if (verbose) { System.err.println("Generating C#: " + csFile); }
-	  genCS(ast, csFile, csNamespace);
+	  genCS(ast, csFile, csNamespace, ver);
 	  prettyOnStdout = false;
 	}
 	if (javaDir != null) {
 	  if (verbose) { System.err.println("Generating Java: " + javaDir); }
-	  genJava(ast, javaDir, javaPackage);
+	  genJava(ast, javaDir, javaPackage, ver);
 	  prettyOnStdout = false;
 	}
 	if (pythonFile != null) {
 	  if (verbose) { 
 	    System.err.println("Generating Python: " + pythonFile); 
 	  }
-	  genPython(ast, pythonFile, prefix);
+	  genPython(ast, pythonFile, prefix, ver);
 	  prettyOnStdout = false;
 	}
 	if (rapidFile != null) {
 		if (verbose) {
 			System.err.println("Generating RAPID: " + rapidFile);
 		}
-		genRAPID(ast, rapidFile, coreName);
+		genRAPID(ast, rapidFile, coreName, ver);
 		prettyOnStdout = false;
 	}
 	if (prettyFile != null) {
@@ -223,9 +238,9 @@ public class LabComm {
 	  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);
+	    ast.C_info(out, cPrefix, ver);
+	    ast.Java_info(out, ver);
+	    ast.CS_info(out, csNamespace, ver);
 	    prettyOnStdout = false;
 	  } catch (IOException e) {
 	    System.err.println("IOException: " + typeinfoFile + " " + e);
@@ -239,14 +254,14 @@ public class LabComm {
   } 
 
   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 +269,57 @@ 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);
     }
   }
 
-  private static void genPython(Program p, String filename, String prefix) {
+  private static void genPython(Program p, String filename, String prefix, int ver) {
     try {
       FileOutputStream f;
       PrintStream out;
 
       f = new FileOutputStream(filename);
       out = new PrintStream(f);
-      p.Python_gen(out, prefix);
+      p.Python_gen(out, prefix, ver);
       out.close();
     } catch (IOException e) {
       System.err.println("IOException: " + filename + " " + e);
     }
   }
 
-  private static void genRAPID(Program p, String filename, String prefix) {
+  private static void genRAPID(Program p, String filename, String prefix, int ver) {
     try {
-      p.RAPID_gen(filename, prefix);
+      p.RAPID_gen(filename, prefix, ver);
     } catch (IOException e) {
       System.err.println("IOException: " + filename + " " + e);
     }
   }
-
-
 }
diff --git a/compiler/Python_CodeGen.jrag b/compiler/Python_CodeGen.jrag
index fd25e18..ff6cef0 100644
--- a/compiler/Python_CodeGen.jrag
+++ b/compiler/Python_CodeGen.jrag
@@ -72,7 +72,10 @@ aspect Python_CodeGenEnv {
 
 aspect Python_CodeGen {
 
-  public void Program.Python_gen(PrintStream out, String baseName) {
+  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);
@@ -217,4 +220,4 @@ aspect PythonTypes {
     return "0";
   }
 
-}
\ No newline at end of file
+}
diff --git a/compiler/RAPID_CodeGen.jrag b/compiler/RAPID_CodeGen.jrag
index 9606105..9910048 100644
--- a/compiler/RAPID_CodeGen.jrag
+++ b/compiler/RAPID_CodeGen.jrag
@@ -1,14 +1,16 @@
 
 aspect RAPID_env {
 	public class RAPID_env {
+                public final int version;
 		private String prefix;
 		private StringBuilder types;
 		private StringBuilder constants;
 		private StringBuilder procedures;
 		private PrintStream ps;
 
-		public RAPID_env(PrintStream ps, String prefix)
+		public RAPID_env(PrintStream ps, String prefix, int version)
 		{
+			this.version = version;
 			this.types = new StringBuilder();
 			this.constants = new StringBuilder();
 			this.procedures = new StringBuilder();
@@ -75,11 +77,11 @@ aspect RAPID_CodeGen {
 		throw new UnsupportedOperationException();
 	}
 
-	public void Program.RAPID_gen(String file, String prefix)
+	public void Program.RAPID_gen(String file, String prefix, int version)
 			throws IOException
 	{
 		PrintStream ps = new PrintStream(new FileOutputStream(new File(file)));
-		RAPID_env env = new RAPID_env(ps, prefix);
+		RAPID_env env = new RAPID_env(ps, prefix, version);
 		RAPID_gen(env);
 	}
 
@@ -101,7 +103,7 @@ aspect RAPID_CodeGen {
 		// Add signature constants
 		String sig_len_name = "signature_len_" + getName();
 		String sig_name = "signature_" + getName();
-		SignatureList sig = signature();
+		SignatureList sig = signature(env.version);
 		StringBuilder sb = new StringBuilder();
 		sb.append("[");
 		byte[] d = null;
diff --git a/compiler/Signature.jrag b/compiler/Signature.jrag
index d329629..8004f2f 100644
--- a/compiler/Signature.jrag
+++ b/compiler/Signature.jrag
@@ -31,25 +31,42 @@ aspect Signature {
   public class SignatureList {
 
     private int indent;
+    private final int ver;
     private ArrayList list = new ArrayList();
     
+    public SignatureList(int version) {
+      this.ver = version; 
+    }
+
     public void add(byte[] data, String comment) {
       list.add(new SignatureLine(indent, data, comment));
     }
 
     public void addInt(int value, String comment) {
-	byte[] tmp = new byte[5];
- 	long v = value & 0xffffffff;
-        int i, j;
-
-        for (i = 0 ; i == 0 || v != 0 ; i++, v = (v >> 7)) {
-          tmp[i] = (byte)(v & 0x7f);
-        }
- 	byte[] packed = new byte[i];
-        for (i = i - 1, j = 0 ; i >= 0 ; i--, j++) {
-	  packed[j] = (byte)(tmp[i] | (i!=0?0x80:0x00));
-        }
-	add(packed, comment);
+      switch(ver) {
+        case 2006:             // Use old encoding with 32 bit integers
+	    byte[] data = new byte[4];
+	    for (int i = 0 ; i < 4 ; i++) {
+	        data[3 - i] = (byte)((value >> (8 * i)) & 0xff);
+	    }
+	    add(data, comment);
+        break;
+        case 2013:             // Use new encoding with varints
+	    byte[] tmp = new byte[5];
+ 	    long v = value & 0xffffffff;
+            int i, j;
+            for (i = 0 ; i == 0 || v != 0 ; i++, v = (v >> 7)) {
+		tmp[i] = (byte)(v & 0x7f);
+            }
+	    byte[] packed = new byte[i];
+            for (i = i - 1, j = 0 ; i >= 0 ; i--, j++) {
+		packed[j] = (byte)(tmp[i] | (i!=0?0x80:0x00));
+            }
+	    add(packed, comment);
+        break;
+        default: 
+            throw new RuntimeException("Version = "+ver+". This should never happen.");
+      }
     }
 
     public void addString(String value, String comment) {
@@ -92,8 +109,8 @@ aspect Signature {
 
   }
 
-  public SignatureList Decl.signature() {
-    SignatureList result = new SignatureList();
+  public SignatureList Decl.signature(int version) {
+    SignatureList result = new SignatureList(version);
     flatSignature(result);
     return result;
   }
-- 
GitLab