Charj: promote local SDAG vars to class vars with mangled names
authorAaron Becker <akbecker@gmail.com>
Fri, 1 Oct 2010 00:11:23 +0000 (19:11 -0500)
committerAaron Becker <akbecker@gmail.com>
Fri, 1 Oct 2010 00:11:23 +0000 (19:11 -0500)
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
src/langs/charj/src/charj/translator/ProxyType.java
src/langs/charj/src/charj/translator/SymbolResolver.g
src/langs/charj/src/charj/translator/Type.java
src/langs/charj/tests/unit/SDAG.cj

index 47af1501bb86ba0d834f12f1234f2cc06e1c24e7..024f8ff60990f0717f33648cf2ec8ec315c2579a 100644 (file)
@@ -350,6 +350,7 @@ class <ident>: public CBase_<ident> {
     static bool _trace_registered;
     void _initTrace();
        <sym.sectionInitializers : { private: CProxySection_<it.classType> <it.methodName>(CProxy_<it.classType>); }>
+    <sym.SDAGLocalTypeDefinitions; separator="\n">
 };
 <endif>
 >>
@@ -491,6 +492,10 @@ var_decl_list(var_decls) ::=
 <var_decls; separator=", ">
 >>
 
+var_decl_list_sdag_ci(var_decls) ::=
+<<
+<var_decls; separator="\n">
+>>
 
 var_decl_cc(id, initializer) ::=
 <<
@@ -507,6 +512,13 @@ var_decl_ci(id, initializer) ::=
 <id><if(initializer)> = <initializer><endif>
 >>
 
+var_decl_sdag_ci(id, initializer) ::=
+<<
+<if(initializer)>
+<id> = <initializer>;
+<endif>
+>>
+
 var_decl_id(id, domainExp) ::=
 <<
 <id><if(domainExp)>(<domainExp>)<endif>
@@ -539,6 +551,11 @@ local_var_decl(modList, type, declList, domainExps) ::=
 <if(modList)><modList> <endif><type> <declList>;
 >>
 
+local_var_decl_sdag_ci(declList) ::=
+<<
+<declList>
+>>
+
 local_mod_list(mods) ::= 
 <<
 <mods; separator=" ">
index bfe338aab85bfae7798fd3ae4465dcff526d1572..7ac77501e0bc43cd2a41bca57045e6359dc43703 100644 (file)
@@ -371,6 +371,8 @@ interfaceScopeDeclaration
 
 variableDeclaratorList[StringTemplate obtype]
     :   ^(VAR_DECLARATOR_LIST (var_decls+=variableDeclarator[obtype])+ )
+        -> {emitCI() && currentClass != null && currentMethod != null && currentMethod.hasSDAG}?
+                var_decl_list_sdag_ci(var_decls={$var_decls})
         -> var_decl_list(var_decls={$var_decls})
     ;
 
@@ -378,6 +380,8 @@ variableDeclarator[StringTemplate obtype]
     :   ^(VAR_DECLARATOR id=variableDeclaratorId initializer=variableInitializer[obtype]?)
         -> {emitCC()}? var_decl_cc(id={$id.st}, initializer={$initializer.st})
         -> {emitH()}?  var_decl_h(id={$id.st}, initializer={$initializer.st})
+        -> {emitCI() && currentClass != null && currentMethod != null && currentMethod.hasSDAG}?
+                var_decl_sdag_ci(id={currentClass.getSDAGLocalName($id.st.toString())}, initializer={$initializer.st})
         -> {emitCI()}? var_decl_ci(id={$id.st}, initializer={$initializer.st})
         ->
     ; 
@@ -656,6 +660,8 @@ formalParameterVarargDecl
     
 qualifiedIdentifier
     :   IDENT
+        -> {emitCI() && currentClass != null && currentMethod != null && currentMethod.hasSDAG}?
+           template(t={currentClass.getSDAGLocalName($text)}) "<t>"
         -> template(t={$text}) "<t>"
     |   ^(DOT qualifiedIdentifier IDENT)
         -> template(t={$text}) "<t>"
@@ -687,7 +693,7 @@ sdagBlock
 sdagBasicBlock
     :   sdagStatement
         -> {$sdagStatement.st}
-    |   (s+=statement)+
+    |   (s+=blockStatement)+
         -> block_atomic(s={$s})
     ;
     
@@ -701,11 +707,15 @@ blockStatement
 
 localVariableDeclaration
     :   ^(PRIMITIVE_VAR_DECLARATION localModifierList? simpleType vdl=variableDeclaratorList[null])
+        -> {emitCI() && currentClass != null && currentMethod != null && currentMethod.hasSDAG}?
+                local_var_decl_sdag_ci(declList={$vdl.st})
         -> local_var_decl(
             modList={$localModifierList.st},
             type={$simpleType.st},
             declList={$vdl.st})
     |   ^(OBJECT_VAR_DECLARATION localModifierList? objectType vdl=variableDeclaratorList[$objectType.st])
+        -> {emitCI() && currentClass != null && currentMethod != null && currentMethod.hasSDAG}?
+                local_var_decl_sdag_ci(declList={$vdl.st})
         -> local_var_decl(
             modList={$localModifierList.st},
             type={$objectType.st},
@@ -947,6 +957,8 @@ primaryExpression
     |   parenthesizedExpression
         -> {$parenthesizedExpression.st}
     |   IDENT
+        -> {emitCI() && currentClass != null && currentMethod != null && currentMethod.hasSDAG}?
+           template(t={currentClass.getSDAGLocalName($IDENT.text)}) "<t>"
         -> {%{$IDENT.text}}
     |   CHELPER
         -> {%{"constructorHelper"}}
index 68b7ceeca4811a688da21dd91c6a403fb31405ba..7d2f7c3f9bd18bce68eb85c09b7941737aee962f 100644 (file)
@@ -24,6 +24,9 @@ public class ClassSymbol extends SymbolWithScope implements Scope, Type {
     public Map<String, VariableSymbol> fields = new LinkedHashMap<String, VariableSymbol>();
     public Map<String, MethodSymbol> methods = new LinkedHashMap<String, MethodSymbol>();
 
+    public Map<String, String> sdag_local_names = new LinkedHashMap<String, String>();
+    public Map<String, String> sdag_local_typenames = new LinkedHashMap<String, String>();
+
     public boolean hasCopyCtor = false;
     public boolean isPrimitive = false;
     public boolean isChare = false;
@@ -313,6 +316,10 @@ public class ClassSymbol extends SymbolWithScope implements Scope, Type {
         return name;
     }
 
+    public String getTranslatedTypeName() {
+        return getTypeName();
+    }
+
     private boolean requiresInit() {
         for (CharjAST varAst : varsToPup) {
             if (varAst.def instanceof VariableSymbol &&
@@ -349,4 +356,22 @@ public class ClassSymbol extends SymbolWithScope implements Scope, Type {
         }
         return false;
     }
+
+    public void addSDAGLocal(String typename, String name, String mangledName) {
+        sdag_local_names.put(name, mangledName);
+        sdag_local_typenames.put(name, typename);
+    }
+
+    public String getSDAGLocalName(String name) {
+        String result = sdag_local_names.get(name);
+        return result == null ? name : result;
+    }
+
+    public List<String> getSDAGLocalTypeDefinitions() {
+        List<String> defs = new ArrayList<String>();
+        for (Map.Entry<String, String> def : sdag_local_typenames.entrySet()) {
+            defs.add(def.getValue() + " " + sdag_local_names.get(def.getKey()) + ";");
+        }
+        return defs;
+    }
 }
index 28e2b40d985c1587cdd3ed0614cf29cbd4a81dcc..be7be59ab76406fcc6bb14e787e0295c3bcb5b18 100644 (file)
@@ -12,4 +12,8 @@ public class PointerType extends Symbol implements Type {
     public String getTypeName() {
         return baseType.getTypeName();
     }
+
+    public String getTranslatedTypeName() {
+        return baseType.getTypeName() + "*";
+    }
 }
index 86c5ae3463e34c47d2343802867caec4bb3ce7d0..eea5ec9d2e0e4e3a347c3772708f42e8ef4feefa 100644 (file)
@@ -12,4 +12,8 @@ public class ProxyType extends Symbol implements Type {
     public String getTypeName() {
         return baseType.getTypeName() + "@";
     }
+
+    public String getTranslatedTypeName() {
+        return "CProxy_" + baseType.getTypeName();
+    }
 }
index 97ea116dcd53ece1f4929f5f07abe80708bc6718..ce72a8fb2c3dfeab5970679354111fb7a870fa94 100644 (file)
@@ -17,6 +17,7 @@ import java.util.Iterator;
     SymbolTable symtab;
     Scope currentScope;
     ClassSymbol currentClass = null;
+    MethodSymbol currentMethod = null;
 
     public SymbolResolver(TreeNodeStream input, SymbolTable symtab) {
         this(input);
@@ -36,20 +37,29 @@ topdown
 
 bottomup
     :   exitClass
+    |   exitMethod
     ;
 
 enterMethod
-@init {
-boolean entry = false;
-}
-    :   ^((FUNCTION_METHOD_DECL | ENTRY_FUNCTION_DECL {entry = true;})
+    :   ^((FUNCTION_METHOD_DECL | ENTRY_FUNCTION_DECL)
             (^(MODIFIER_LIST .*))?
             (^(GENERIC_TYPE_PARAM_LIST .*))? 
-            type IDENT .*)
-    |   ^((CONSTRUCTOR_DECL | ENTRY_CONSTRUCTOR_DECL {entry = true;})
+            type IDENT .*) { currentMethod = (MethodSymbol)$IDENT.def; }
+    |   ^((CONSTRUCTOR_DECL | ENTRY_CONSTRUCTOR_DECL)
             (^(MODIFIER_LIST .*))?
             (^(GENERIC_TYPE_PARAM_LIST .*))? 
-            IDENT .*)
+            IDENT .*) { currentMethod = (MethodSymbol)$IDENT.def; }
+    ;
+
+exitMethod
+    :   ^((FUNCTION_METHOD_DECL | ENTRY_FUNCTION_DECL)
+            (^(MODIFIER_LIST .*))?
+            (^(GENERIC_TYPE_PARAM_LIST .*))? 
+            type IDENT .*) { currentMethod = null; }
+    |   ^((CONSTRUCTOR_DECL | ENTRY_CONSTRUCTOR_DECL)
+            (^(MODIFIER_LIST .*))?
+            (^(GENERIC_TYPE_PARAM_LIST .*))? 
+            IDENT .*) { currentMethod = null; }
     ;
 
 enterClass
@@ -77,8 +87,9 @@ varDeclaration
             (^(MODIFIER_LIST .*))? type
             ^(VAR_DECLARATOR_LIST (^(VAR_DECLARATOR ^(IDENT .*) .*)
             {
-                //System.out.println("Resolved type of variable " + $IDENT.text + ": " +
-                //    $IDENT.def.type + ", symbol is " + $IDENT.def);
+                /*System.out.println("Resolved type of variable " + $IDENT.text + ": " +
+                    $IDENT.def.type + ", symbol is " + $IDENT.def);*/
+                Type varType = (Type)$IDENT.def.type;
                 if (currentClass != null) {
                     ClassSymbol declType = null;
                     if ($type.sym instanceof ClassSymbol) {
@@ -88,12 +99,16 @@ varDeclaration
                     }
 
                     if (declType != null) {
-                        //System.out.println("Looking to extern " + $IDENT.text + " as " + declType);
                         if (declType.isChare && declType != currentClass) {
-                            //System.out.println("extern added");
                             currentClass.addExtern(declType.getTypeName());
                         }
                     }
+                    if (currentMethod != null && currentMethod.hasSDAG && varType != null) {
+                        String mangledName = "_sdag_" + currentMethod.getScopeName() + "_" + $IDENT.text;
+                        //System.out.println("var " + $IDENT.text + " is an sdag local; adding to class as " +
+                        //    varType.getTypeName() + " " + mangledName + "\n");
+                        currentClass.addSDAGLocal(varType.getTranslatedTypeName(), $IDENT.text, mangledName);
+                    }
                 }
             }
             )+))
index 3db5c4d5b2bb4a55eb488e64d34209b30d950936..94854da35c0b76777698080118b65428268f027b 100644 (file)
@@ -3,4 +3,5 @@ package charj.translator;
 
 public interface Type {
     public String getTypeName();
+    public String getTranslatedTypeName();
 }
index ee33bee19aad6394e34f0ff4cbf6992542bf9410..efed296de21f1a53be1fb4ace0ca040fe27f9e0e 100644 (file)
@@ -9,6 +9,11 @@ public mainchare Main {
     }
 }
 
+public class Foo {
+    public Foo() {}
+    public int m;
+}
+
 public chare SDAG_Test{
 
     public entry SDAG_Test() {
@@ -22,6 +27,11 @@ public chare SDAG_Test{
     public entry void third(int t);
 
     public entry void test1() {
+        int xx, yy = 1;
+        int zz = yy;
+        Main@ main_proxy;
+        Foo moo_instance = new Foo();
+
         x(0);
         overlap { 
             when first(int f) { x(f); }
@@ -30,9 +40,9 @@ public chare SDAG_Test{
         x(1);
         x(2);
         for (int i=0; i<10; ++i) {
-            x(3);
+            x(i);
             for (int j=0; j<10; ++j) {
-                x(4);
+                x(i+j);
                 x(5);
             }
             for (int j=0; j<10; ++j) {