Charj: Completely rewrote PUPers, to use the emitter instead of modifing the AST.
authorJonathan Lifflander <jliffl2@illinois.edu>
Tue, 29 Jun 2010 03:24:41 +0000 (22:24 -0500)
committerJonathan Lifflander <jliffl2@illinois.edu>
Tue, 29 Jun 2010 03:24:41 +0000 (22:24 -0500)
src/langs/charj/build.xml
src/langs/charj/src/charj/translator/Charj.stg
src/langs/charj/src/charj/translator/CharjEmitter.g
src/langs/charj/src/charj/translator/ClassSymbol.java
src/langs/charj/src/charj/translator/PointerType.java [new file with mode: 0644]
src/langs/charj/src/charj/translator/SymbolResolver.g
src/langs/charj/src/charj/translator/Translator.java
src/langs/charj/src/charj/translator/VariableSymbol.java

index fc1b5973e6aaf18a7911e6dcd1c13b8ad37ac119..246af79c41a09126dd54c3d57b3969923736cfb1 100644 (file)
             grammar.path="${buildsrc}/charj/translator"/>
     </target>
 
+    <target name="InitPUPCollector" depends="Charj">
+        <antlr3 grammar.name="InitPUPCollector.g"
+            grammar.path="${buildsrc}/charj/translator"/>
+    </target>
+
     <target name="CharjASTModifier2" depends="Charj">
         <antlr3 grammar.name="CharjASTModifier2.g"
             grammar.path="${buildsrc}/charj/translator"/>
@@ -94,7 +99,7 @@
             grammar.path="${buildsrc}/charj/translator"/>
     </target>
 
-    <target name="CharjEmitter" depends="CharjSemantics,CharjASTModifier,CharjASTModifier2,SymbolDefiner,SymbolResolver">
+    <target name="CharjEmitter" depends="CharjSemantics,CharjASTModifier,CharjASTModifier2,SymbolDefiner,SymbolResolver,InitPUPCollector">
         <antlr3 grammar.name="CharjEmitter.g"
             grammar.path="${buildsrc}/charj/translator"/>
     </target>
index d2023df144b16166e673b3cce4c8f8884fc03b6b..0bcbdc6557d25ad37abf3b423a95d01c7559fe55 100644 (file)
@@ -173,21 +173,35 @@ classDeclaration_cc_cont(pds, sym, ident, ext, csds) ::=
 >>
 
 
-chareDeclaration_cc(sym, ident, ext, csds) ::=
+chareDeclaration_cc(sym, ident, ext, csds, pupers, pupInits) ::=
 <<
 #include "<ident>.decl.h"
-<chareDeclaration_cc_cont(pds = sym.packageNames, sym = sym, ident = ident, ext = ext, csds = csds)>
+<chareDeclaration_cc_cont(pds = sym.packageNames, sym = sym, ident = ident, ext = ext, csds = csds, pupers = pupers, pupInits = pupInits)>
 #include "<ident>.def.h"
 >>
 
-chareDeclaration_cc_cont(pds, sym, ident, ext, csds) ::=
+chareDeclaration_cc_cont(pds, sym, ident, ext, csds, pupers, pupInits) ::=
 <<
 <if(first(pds))>namespace <first(pds)>
 {
-    <chareDeclaration_cc_cont(pds = rest(pds), sym = sym, ident = ident, ext = ext, csds = csds)>
+    <chareDeclaration_cc_cont(pds = rest(pds), sym = sym, ident = ident, ext = ext, csds = csds, pupers = pupers, pupInits = pupInits)>
 } // namespace <first(pds)>
 <else>
 <csds; separator="\n\n">
+
+<if(pupInits)>
+void pupInitMethod() {
+    <pupInits; separator="\n">
+}
+<endif>
+
+void pup(PUP::er &p) {
+<if(pupInits)>
+    pupInitMethod();
+
+<endif>
+    <pupers; separator="\n">
+}
 <endif>
 >>
 
@@ -209,6 +223,7 @@ chareDeclaration_h_cont(pds, sym, ident, ext, csds) ::=
 <if(ext)>/* superclass: <ext> */<endif>
 class <ident>: public CBase_<ident> {
     <csds; separator="\n">
+    public: void pup(PUP::er& p);
 };
 <endif>
 >>
index c4671b1962f2b620d7d01927eeb3289c2af1ebb3..cd55d9d374621f89def9ee1badac34c4b240c880 100644 (file)
@@ -168,7 +168,9 @@ typeDeclaration
                 sym={currentClass},
                 ident={$IDENT.text}, 
                 ext={$su.st}, 
-                csds={$csds})
+                csds={$csds},
+                pupInits={currentClass.generateInits()},
+                pupers={currentClass.generatePUPers()})
         -> {emitCI()}? chareDeclaration_ci(
                 sym={currentClass},
                 chareType={$chareType.st},
index c22a50e1f8948e9109dff9cad04061d5a0420ecd..634b65ac88419654444a9ede9cbf87ecf6e8940d 100644 (file)
@@ -9,6 +9,7 @@ public class ClassSymbol extends SymbolWithScope implements Scope, Type {
     public List<String> interfaceImpls;
     public List<String> templateArgs;
     public List<CharjAST> initializers;
+    public List<CharjAST> varsToPup;
 
     public CharjAST constructor;
 
@@ -39,6 +40,7 @@ public class ClassSymbol extends SymbolWithScope implements Scope, Type {
             importPackage(pkg);
         }
        this.initializers = new ArrayList<CharjAST>();
+        varsToPup = new ArrayList<CharjAST>();
        constructor = null;
     }
 
@@ -290,4 +292,39 @@ public class ClassSymbol extends SymbolWithScope implements Scope, Type {
     {
         return name;
     }
+
+    private boolean requiresInit() {
+        for (CharjAST varAst : varsToPup) {
+            if (varAst.def instanceof VariableSymbol &&
+                ((VariableSymbol)varAst.def).isPointerType()) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    public List<String> generateInits() {
+        List<String> inits = new ArrayList<String>();
+        for (CharjAST varAst : varsToPup) {
+            if (varAst.def instanceof VariableSymbol &&
+                ((VariableSymbol)varAst.def).isPointerType()) {
+                VariableSymbol vs = (VariableSymbol)varAst.def;
+                inits.add(vs.generateInit());
+            }
+        }
+        if (inits.size() == 0)
+            return null;
+        else
+            return inits;
+    }
+
+    public List<String> generatePUPers() {
+        List<String> PUPers = new ArrayList<String>();
+        for (CharjAST varAst : varsToPup) {
+            if (varAst.def instanceof VariableSymbol) {
+                PUPers.add(((VariableSymbol)varAst.def).generatePUP());
+            }
+        }
+        return PUPers;
+    }
 }
diff --git a/src/langs/charj/src/charj/translator/PointerType.java b/src/langs/charj/src/charj/translator/PointerType.java
new file mode 100644 (file)
index 0000000..28e2b40
--- /dev/null
@@ -0,0 +1,15 @@
+
+package charj.translator;
+
+public class PointerType extends Symbol implements Type {
+    public Type baseType;
+
+    public PointerType(SymbolTable symtab, Type _baseType) {
+        super(symtab, _baseType.getTypeName(), null);
+        baseType = _baseType;
+    }
+
+    public String getTypeName() {
+        return baseType.getTypeName();
+    }
+}
index 9a1386ed622d5a72289b6c30d41541230d53636c..efe6a6fd5f68a84c3aaa5963a4a82e6411ca6d58 100644 (file)
@@ -327,6 +327,7 @@ type returns [Type sym]
     CharjAST head = null;
     Scope scope = null;
     boolean proxy = false;
+    boolean pointer = false;
 }
 @after {
     typeText = typeText.substring(1);
@@ -335,6 +336,7 @@ type returns [Type sym]
     $start.symbolType = scope.resolveType(typeText);
     //System.out.println("symbolType: " + $start.symbolType);
     if (proxy && $start.symbolType != null) $start.symbolType = new ProxyType(symtab, $start.symbolType);
+    if (pointer && $start.symbolType != null) $start.symbolType = new PointerType(symtab, $start.symbolType);
     $sym = $start.symbolType;
     if ($sym == null) System.out.println("Couldn't resolve type: " + typeText);
 }
@@ -352,7 +354,7 @@ type returns [Type sym]
             ^(QUALIFIED_TYPE_IDENT (^(IDENT  {typeText += "." + $IDENT.text;} .*))+) .*)
     |   ^(PROXY_TYPE { scope = $PROXY_TYPE.scope; proxy = true; }
             ^(QUALIFIED_TYPE_IDENT (^(IDENT {typeText += "." + $IDENT.text;} .*))+) .*)
-    |   ^(POINTER_TYPE { scope = $POINTER_TYPE.scope; }
+    |   ^(POINTER_TYPE { scope = $POINTER_TYPE.scope; pointer = true; }
             ^(QUALIFIED_TYPE_IDENT (^(IDENT {typeText += "." + $IDENT.text;} .*))+) .*)
     ;
 
index 8377765123f7b69503ce96cec40f60bfcea0dd5e..d7b3ed52b5913ee15b4a02d6f585b66b49ec4a30 100644 (file)
@@ -108,6 +108,9 @@ public class Translator {
         resolveTypes();
         if (m_printAST) printAST("After Semantic Pass", "after_sem.html");
 
+        initPupCollect();
+        if (m_printAST) printAST("After Collector Pass", "after_collector.html");
+
         postSemanticPass();
         if (m_printAST) printAST("After PostSemantics Pass", "after_postsem.html");
 
@@ -183,6 +186,15 @@ public class Translator {
         resolver.downup(m_ast);
     }
 
+    private void initPupCollect() throws
+        RecognitionException, IOException, InterruptedException
+    {
+        m_nodes.reset();
+        if (m_verbose) System.out.println("\nInitPupCollector Phase\n----------------");
+        InitPUPCollector collector = new InitPUPCollector(m_nodes);
+        collector.downup(m_ast);
+    }
+
     private CharjAST addConstructor(ClassSymbol sem) {
        CharjAST ast1 = new CharjAST(CharjParser.CONSTRUCTOR_DECL, 
                                     "CONSTRUCTOR_DECL");
index f27b637f9cf807bfb6feb0fbc96af40f506ac1b0..fa3a3fbb5a6aa1f11934fdcdd9b42a32752aa79a 100644 (file)
@@ -1,6 +1,9 @@
 
 package charj.translator;
 
+import org.antlr.stringtemplate.StringTemplate;
+import org.antlr.stringtemplate.language.AngleBracketTemplateLexer;
+
 /** Represents a variable definition (name,type) in symbol table (a scope 
  *  thereof)
  */
@@ -25,4 +28,38 @@ public class VariableSymbol extends Symbol {
         buf.append(name);
         return buf.toString();
     }
+
+    public boolean isPointerType() {
+        return this.type instanceof PointerType;
+    }
+
+    public String generateInit() {
+        if (this.type instanceof PointerType) {
+            PointerType pt = (PointerType)this.type;
+            StringTemplate st = new StringTemplate
+                ("<varname> = new <typename>();", 
+                 AngleBracketTemplateLexer.class);
+            st.setAttribute("varname", name);
+            st.setAttribute("typename", pt.name);
+            return st.toString();
+        } else {
+            return "";
+        }
+    }
+
+    public String generatePUP() {
+        if (isPointerType()) {
+            StringTemplate st = new StringTemplate
+                ("<name>->pup(p);", 
+                 AngleBracketTemplateLexer.class);
+            st.setAttribute("name", name);
+            return st.toString();
+        } else {
+            StringTemplate st = new StringTemplate
+                ("p | <name>;", 
+                 AngleBracketTemplateLexer.class);
+            st.setAttribute("name", name);
+            return st.toString();
+        }
+    }
 }