Charj: SDAG: collect and save 'when' triggers and output entry method for them.
authorJonathan Lifflander <jliffl2@illinois.edu>
Fri, 23 Mar 2012 20:38:43 +0000 (15:38 -0500)
committerJonathan Lifflander <jliffl2@illinois.edu>
Mon, 26 Mar 2012 23:04:39 +0000 (18:04 -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/MethodSymbol.java
src/langs/charj/src/charj/translator/SymbolDefiner.g

index 81117b9f20a763976882550e3af89cff673eb773..7a97d6356fd8e1156783f72c4ec4e7c4a9fb0005 100644 (file)
@@ -383,7 +383,7 @@ class <ident>: public CBase_<ident> {
 <endif>
 >>
 
-chareDeclaration_ci(basename, sym, chareType, arrayDim, ident, ext, csds) ::=
+chareDeclaration_ci(basename, sym, chareType, arrayDim, ident, ext, csds, entries) ::=
 <<
 <if(sym.isMainChare)>main<endif>module <ident> {
 <if(ext)>
@@ -394,20 +394,21 @@ chareDeclaration_ci(basename, sym, chareType, arrayDim, ident, ext, csds) ::=
 extern module <basename>_readonly;
 <endif>
     }>
-    <chareDeclaration_ci_cont(pds = sym.packageNames, chareType = chareType, arrayDim = arrayDim, ident = ident, ext = ext, csds = csds)>
+    <chareDeclaration_ci_cont(pds = sym.packageNames, chareType = chareType, arrayDim = arrayDim, ident = ident, ext = ext, csds = csds, entries = entries)>
 }
 >>
 
 
-chareDeclaration_ci_cont(pds, sym, chareType, arrayDim, ident, ext, csds) ::=
+chareDeclaration_ci_cont(pds, sym, chareType, arrayDim, ident, ext, csds, entries) ::=
 <<
 <if(first(pds))>namespace <first(pds)>
 {
-    <chareDeclaration_ci_cont(pds = rest(pds), sym = sym, chareType = chareType, arrayDim = arrayDim, ident = ident, ext = ext, csds = csds)>
+    <chareDeclaration_ci_cont(pds = rest(pds), sym = sym, chareType = chareType, arrayDim = arrayDim, ident = ident, ext = ext, csds = csds, entries = entries)>
 } // namespace <first(pds)>
 <else>
 <chareType><if(arrayDim)> [<arrayDim>]<endif> <ident><if(ext)> : <ext><endif> {
     <csds; separator="\n">
+    <entries; separator="\n">
 };
 <endif>
 >>
index ea9c04d841eaf599a1a7543b48fe0a130f77518d..777a699c167150ee75b2afc840ea440859b1dd75 100644 (file)
@@ -142,6 +142,7 @@ typeDeclaration
     boolean needsMigration = false;
     List<String> inits = new ArrayList<String>();
     List<String> pupInits = new ArrayList<String>();
+    List<String> sdagEntries = new ArrayList<String>();
 }
     :   ^(TYPE CLASS IDENT (^('extends' su=type))? (^('implements' type+))?
         {
@@ -181,7 +182,7 @@ typeDeclaration
         {
             currentClass = (ClassSymbol)$IDENT.def;
             needsMigration = currentClass.isChareArray && !currentClass.hasMigrationCtor;
-
+            sdagEntries = currentClass.generateSDAGEntries();
             inits = currentClass.generateInits(currentClass.initializers);
             pupInits = currentClass.generateInits(currentClass.pupInitializers);
         }
@@ -202,7 +203,8 @@ typeDeclaration
                 arrayDim={null},
                 ident={$IDENT.text}, 
                 ext={$su.st}, 
-                csds={$csds})
+                csds={$csds},
+                entries={sdagEntries})
         -> {emitH()}? chareDeclaration_h(
                 sym={currentClass},
                 ident={$IDENT.text}, 
index 5bb3360626b18321beb4facb138b0dcfa680a573..2fc17830b8196688680a56a600328b9dbe526cd2 100644 (file)
@@ -1,6 +1,11 @@
 
 package charj.translator;
 
+import org.antlr.runtime.*;
+import org.antlr.runtime.tree.*;
+import org.antlr.stringtemplate.*;
+import org.antlr.stringtemplate.StringTemplate;
+import org.antlr.stringtemplate.language.AngleBracketTemplateLexer;
 import java.util.*;
 
 public class ClassSymbol extends SymbolWithScope implements Scope, Type {
@@ -24,6 +29,7 @@ public class ClassSymbol extends SymbolWithScope implements Scope, Type {
     public Map<String, Symbol> members = new LinkedHashMap<String, Symbol>();
     public Map<String, VariableSymbol> fields = new LinkedHashMap<String, VariableSymbol>();
     public Map<String, MethodSymbol> methods = new LinkedHashMap<String, MethodSymbol>();
+    public Map<String, MethodSymbol> sdagMethods = 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>();
@@ -381,4 +387,47 @@ public class ClassSymbol extends SymbolWithScope implements Scope, Type {
         }
         return defs;
     }
+
+    public List<String> generateSDAGEntries() {
+        List<String> entries = new ArrayList<String>();
+        for (Map.Entry<String, MethodSymbol> entry : sdagMethods.entrySet()) {
+          String ident = entry.getKey();
+          MethodSymbol method = entry.getValue();
+          entries.add(emitSDAGEntry(ident, method.sdagFPL));
+        }
+        return entries;
+    }
+
+    public static TreeAdaptor m_adaptor = new CommonTreeAdaptor() {
+        public Object create(Token token) {
+            return new CharjAST(token);
+        }
+        public Object dupNode(Object t) {
+            if (t == null) {
+                return null;
+            }
+            return create(((CharjAST)t).token);
+        }
+    };
+
+    public String emitSDAGEntry(String ident, CharjAST fpl) {
+      try {
+        CommonTree m_ast = (CommonTree)fpl;
+        CommonTreeNodeStream m_nodes = new CommonTreeNodeStream(m_ast);
+        m_nodes.setTreeAdaptor(m_adaptor);
+        CharjEmitter emitter = new CharjEmitter(m_nodes);
+        emitter.setTemplateLib(Translator.getTemplates(Translator.templateFile));
+        StringTemplate fplst = (StringTemplate)emitter.formalParameterList().getTemplate();
+        StringTemplate st = new StringTemplate("entry void <id><fpl>;",
+                                               AngleBracketTemplateLexer.class);
+        st.setAttribute("id", ident);
+        st.setAttribute("fpl", fplst.toString());
+        return st.toString();
+      } catch (RecognitionException ex) {
+        System.err.println(ex.getMessage());
+        ex.printStackTrace(System.err);
+        return "";
+      }
+    }
+
 }
index 611e370bb4e96a0b581ec976d5bd3042d55b7ef3..1967ff34a4fada521f3ce841175029e83ed0004a 100644 (file)
@@ -28,6 +28,8 @@ public class MethodSymbol
     public boolean isTraced = false;
     public boolean hasSDAG = false;
 
+    public CharjAST sdagFPL;
+
     public MethodSymbol(SymbolTable symtab) {
         super(symtab);
     }
index 77987f3cb16acacd3b87c46523ce68eb3d6b42a0..ae11c322d8b0aec026bee2a09e4574cbe3649def 100644 (file)
@@ -359,8 +359,36 @@ atoms
             assert currentScope != null;
             t.scope = currentScope;
        }
-    |  (WHEN|OVERLAP) {
+    |  ^(WHEN sdagTrigger+ ^(BLOCK .*)) {
             assert currentMethod != null;
             currentMethod.hasSDAG = true;
        }
+    |  OVERLAP {
+            assert currentMethod != null;
+            currentMethod.hasSDAG = true;
+       }
+    ;
+
+sdagTrigger
+    :  IDENT .* ^(FORMAL_PARAM_LIST .*) {
+         ArrayList<TypeName> typeName = new ArrayList<TypeName>();
+         typeName.add(new TypeName("void"));
+         Type returnType = currentScope.resolveType(typeName);
+         MethodSymbol sym = new MethodSymbol(symtab, $IDENT.text, currentClass, returnType);
+         sym.sdagFPL = $FORMAL_PARAM_LIST;
+         sym.isEntry = true;
+         // @todo fix this??
+         sym.isTraced = false;
+         //sym.definition = $IDENT.start;
+         sym.hasSDAG = true;
+         sym.definitionTokenStream = input.getTokenStream();
+         currentScope.define($IDENT.text, sym);
+         currentScope = sym;
+         //currentMethod = sym;
+         $IDENT.def = sym;
+         $IDENT.symbolType = sym.type;
+         $IDENT.scope = currentScope;
+         currentClass.sdagMethods.put($IDENT.text, sym);
+         currentScope.define($IDENT.text, sym);
+      }
     ;