Charj: simplify and improve SDAG handling
authorAaron Becker <akbecker@gmail.com>
Tue, 21 Sep 2010 22:05:18 +0000 (17:05 -0500)
committerAaron Becker <akbecker@gmail.com>
Tue, 21 Sep 2010 22:05:18 +0000 (17:05 -0500)
src/langs/charj/src/charj/translator/AtomicBlocks.g [deleted file]
src/langs/charj/src/charj/translator/Charj.g
src/langs/charj/src/charj/translator/Charj.stg
src/langs/charj/src/charj/translator/CharjASTModifier2.g
src/langs/charj/src/charj/translator/CharjEmitter.g
src/langs/charj/src/charj/translator/Translator.java
src/langs/charj/tests/unit/SDAG.cj [new file with mode: 0644]

diff --git a/src/langs/charj/src/charj/translator/AtomicBlocks.g b/src/langs/charj/src/charj/translator/AtomicBlocks.g
deleted file mode 100644 (file)
index 102ba14..0000000
+++ /dev/null
@@ -1,133 +0,0 @@
-
-tree grammar AtomicBlocks;
-
-options {
-    tokenVocab = Charj;
-    ASTLabelType = CharjAST;
-    filter = true;
-    output = AST;
-}
-
-@header {
-package charj.translator;
-import java.util.Iterator;
-}
-
-@members {
-    SymbolTable symtab;
-
-    public AtomicBlocks(TreeNodeStream input, SymbolTable symtab) {
-        this(input);
-        this.symtab = symtab;
-    }
-}
-
-
-topdown
-    :   block
-    ;
-
-
-parenthesizedExpression
-    :   ^(PAREN_EXPR exp=expression)
-    ;
-
-expression 
-    :   ^(EXPR .*)
-    ;
-
-assignment
-    :   ^(ASSIGNMENT IDENT expression)
-    ;
-
-
-type
-    :   VOID 
-    |   ^(SIMPLE_TYPE .*)
-    |   ^(OBJECT_TYPE .*)
-    |   ^(REFERENCE_TYPE .*)
-    |   ^(PROXY_TYPE .*)
-    |   ^(POINTER_TYPE .*)
-    ;
-
-classType
-    :   CLASS
-    |   CHARE
-    |   GROUP
-    |   NODEGROUP
-    |   MAINCHARE
-    |   ^(CHARE_ARRAY ARRAY_DIMENSION)
-    ;
-
-
-block returns [boolean containsSDAG]
-@init { boolean hasSDAGstatement = false; }
-    :   ^(BLOCK (sdagNonLeader |
-            sdagLeader {hasSDAGstatement = $sdagLeader.containsSDAG; } |
-            b=block {hasSDAGstatement |= $b.containsSDAG;})*) {
-            $containsSDAG = hasSDAGstatement;
-            ((LocalScope)$BLOCK.def).hasSDAG = $containsSDAG;
-        }
-    ;
-    
-statement
-    :   sdagNonLeader
-    |   sdagLeader
-    |   block
-    ;
-
-sdagLeader returns [boolean containsSDAG]
-@init { $containsSDAG = false; }
-    :   ^(OVERLAP block) {$containsSDAG = true;}
-    |   ^(WHEN (IDENT expression? ^(FORMAL_PARAM_LIST .*))* block) {$containsSDAG = true;}
-    |   ^(IF parenthesizedExpression ifblock=block elseblock=block?
-            {$ifblock.containsSDAG || $elseblock.containsSDAG}?)
-        {
-            $containsSDAG = true;
-        }
-        -> ^(SDAG_IF parenthesizedExpression $ifblock $elseblock)
-    |   ^(FOR forInit? FOR_EXPR expression? FOR_UPDATE expression* block {$block.containsSDAG}?) {
-            $containsSDAG = true;
-        }
-        -> ^(SDAG_FOR forInit? FOR_EXPR expression? FOR_UPDATE expression* block)
-    |   ^(WHILE parenthesizedExpression block {$block.containsSDAG}?) {
-            $containsSDAG = true;
-        }
-        -> ^(SDAG_WHILE parenthesizedExpression block)
-    |   ^(DO block parenthesizedExpression {$block.containsSDAG}?) {
-            $containsSDAG = true;
-        }
-        -> ^(SDAG_DO block parenthesizedExpression)
-    ;
-
-sdagNonLeader
-    :   ^(PRIMITIVE_VAR_DECLARATION .*)
-    |   ^(OBJECT_VAR_DECLARATION .*)
-    |   ^(ASSERT expression expression?)
-    |   ^(SWITCH parenthesizedExpression switchCaseLabel*)
-    |   ^(RETURN expression?)
-    |   ^(THROW expression)
-    |   ^(BREAK IDENT?)
-    |   ^(CONTINUE IDENT?)
-    |   ^(LABELED_STATEMENT IDENT statement)
-    |   expression
-    |   ^('delete' expression)
-    |   ^(EMBED STRING_LITERAL EMBED_BLOCK)
-    |   ';' // Empty statement.
-    |   ^(PRINT expression*)
-    |   ^(PRINTLN expression*)
-    |   ^(EXIT expression?)
-    |   EXITALL
-    ;
-        
-switchCaseLabel
-    :   ^(CASE expression statement*)
-    |   ^(DEFAULT statement*)
-    ;
-    
-forInit
-    :   ^(PRIMITIVE_VAR_DECLARATION .*)
-    |   ^(OBJECT_VAR_DECLARATION .*)
-    |   expression+
-    ;
-
index 217f73044e0c8f979f62654821b31ee5ade8d6fc..ae3244082ff8cd3dc265aac861a5d69e19186437 100644 (file)
@@ -233,6 +233,7 @@ tokens {
     OTHER_MODIFIER_LIST;
     POINTER_DEREFERENCE;
     ENTRY_FUNCTION_DECL;
+    SDAG_FUNCTION_DECL;
     ENTRY_CONSTRUCTOR_DECL;
 }
 
index 2348c79f85477e42bb24da582c2c59f7efae5baa..943aa45a2ac1273eb81778afcaa51b8e84032045 100644 (file)
@@ -446,15 +446,16 @@ block_cc(bsl) ::=
 {
     <bsl; separator="\n">
 }
+
 >>
 
-block_sdag_ci(bsl) ::=
+block_atomic(s) ::=
 <<
 atomic {
-    <bsl; separator="\n">
+    <s; separator="\n">
 }
->>
 
+>>
 
 embed_cc(str, blk) ::=
 <<
index 89adb2dc778dd91e3f0312ab412bbb4f89019f71..ca4d536cffeac909a3c0aab92d852bc722742e65 100644 (file)
@@ -98,7 +98,11 @@ classScopeDeclaration
     :   ^(FUNCTION_METHOD_DECL modifierList? genericTypeParameterList?
             type IDENT formalParameterList domainExpression? b=block)
     |   ^(ENTRY_FUNCTION_DECL modifierList? genericTypeParameterList?
-            type IDENT entryFormalParameterList domainExpression? b=block)
+            type IDENT entryFormalParameterList domainExpression? b=block?)
+        -> {$b.sdag}? ^(SDAG_FUNCTION_DECL modifierList? genericTypeParameterList?
+                type IDENT entryFormalParameterList domainExpression? block?)
+        -> ^(ENTRY_FUNCTION_DECL modifierList? genericTypeParameterList?
+            type IDENT entryFormalParameterList domainExpression? block?)
     |   ^(DIVCON_METHOD_DECL modifierList? type IDENT formalParameterList divconBlock)
     |   ^(PRIMITIVE_VAR_DECLARATION modifierList? simpleType variableDeclaratorList)
             //^(VAR_DECLARATOR_LIST field[$simpleType.type]+))
@@ -306,13 +310,15 @@ qualifiedIdentifier
     |   ^(DOT qualifiedIdentifier IDENT)
     ;
     
-block
-    :   ^(BLOCK (blockStatement)*)
+block returns [boolean sdag]
+@init { $sdag = false; }
+    :   ^(BLOCK (blockStatement { $sdag |= $blockStatement.sdag; })*)
     ;
-    
-blockStatement
+
+blockStatement returns [boolean sdag]
+@init { $sdag = false; }
     :   localVariableDeclaration
-    |   statement
+    |   statement { $sdag = $statement.sdag; }
     ;
     
 localVariableDeclaration
@@ -320,10 +326,11 @@ localVariableDeclaration
     |   ^(OBJECT_VAR_DECLARATION localModifierList? objectType variableDeclaratorList)
     ;
 
-statement
-    : nonBlockStatement
-    | sdagStatement
-    | block
+statement returns [boolean sdag]
+@init { $sdag = false; }
+    : nonBlockStatement { $sdag = $nonBlockStatement.sdag; }
+    | sdagStatement { $sdag = true; }
+    | block { $sdag = $block.sdag; }
     ;
 
 divconBlock
@@ -349,13 +356,24 @@ sdagStatement
     |   ^(WHEN (IDENT expression? formalParameterList)* block)
     ;
 
-nonBlockStatement
+nonBlockStatement returns [boolean sdag]
+@init { $sdag = false; }
     :   ^(ASSERT expression expression?)
-    |   ^(IF parenthesizedExpression block block?)
-    |   ^(FOR forInit? FOR_EXPR expression? FOR_UPDATE expression* block)
-    |   ^(FOR_EACH localModifierList? type IDENT expression block) 
-    |   ^(WHILE parenthesizedExpression block)
-    |   ^(DO block parenthesizedExpression)
+    |   ^(IF parenthesizedExpression (i=block { $sdag |= $i.sdag; }) (e=block { $sdag |= $e.sdag; })?)
+        -> {$sdag}? ^(SDAG_IF parenthesizedExpression $i $e)
+        -> ^(IF parenthesizedExpression $i $e)
+    |   ^(FOR forInit? FOR_EXPR (e1=expression)? FOR_UPDATE (e2+=expression)* block {
+            $sdag = $block.sdag;
+        })
+        -> {$sdag}? ^(SDAG_FOR forInit? FOR_EXPR $e1 FOR_UPDATE $e2 block)
+        -> ^(FOR forInit? FOR_EXPR $e1 FOR_UPDATE $e2 block)
+    |   ^(FOR_EACH localModifierList? type IDENT expression block { $sdag = $block.sdag; })
+    |   ^(WHILE parenthesizedExpression block { $sdag = $block.sdag; })
+        -> {$sdag}? ^(SDAG_WHILE parenthesizedExpression block)
+        -> ^(WHILE parenthesizedExpression block)
+    |   ^(DO block parenthesizedExpression { $sdag = $block.sdag; })
+        -> {$sdag}? ^(SDAG_DO block parenthesizedExpression)
+        -> ^(DO block parenthesizedExpression)
     |   ^(SWITCH parenthesizedExpression switchCaseLabel*)
     |   ^(RETURN expression?)
     |   ^(THROW expression)
index 0f8562f059fff402e316f9ed8f9946040474a17c..2c6ba487c10220c23b6f460a43aa1ddf2c131174 100644 (file)
@@ -249,14 +249,24 @@ classScopeDeclaration
                 block={$b.st})
         -> {emitCI()}? // do nothing, since it's not an entry method
         ->
+    |   ^(ENTRY_FUNCTION_DECL m=modifierList? g=genericTypeParameterList? 
+            ty=type IDENT f=formalParameterList a=domainExpression[null]?) 
+        -> {emitCI()}?  funcMethodDecl_ci(
+                modl={$m.st}, 
+                gtpl={$g.st}, 
+                ty={$ty.st},
+                id={$IDENT.text}, 
+                fpl={$f.st}, 
+                block={null})
+        ->
     |   ^(ENTRY_FUNCTION_DECL m=modifierList? g=genericTypeParameterList? 
             ty=type IDENT f=formalParameterList a=domainExpression[null]? 
             {
                 currentMethod = (MethodSymbol)$IDENT.def;
                 sdagMethod = currentMethod.hasSDAG;
             }
-            b=block?
-        -> {emitCC() && !sdagMethod}? funcMethodDecl_cc(
+            b=block) 
+        -> {emitCC()}? funcMethodDecl_cc(
                 classSym={currentClass},
                 methodSym={currentMethod},
                 modl={$m.st}, 
@@ -266,7 +276,7 @@ classScopeDeclaration
                 fpl={$f.st}, 
                 adl={$a.st},
                 block={$b.st})
-        -> {emitH() && !sdagMethod}? funcMethodDecl_h(
+        -> {emitH()}? funcMethodDecl_h(
                 modl={$m.st}, 
                 gtpl={$g.st}, 
                 ty={$ty.st},
@@ -274,23 +284,32 @@ classScopeDeclaration
                 fpl={$f.st}, 
                 adl={$a.st},
                 block={$b.st})
-        -> {emitCI() && sdagMethod}? funcMethodDecl_sdag_ci(
-                classSym={currentClass},
-                methodSym={currentMethod},
+        -> {emitCI()}? funcMethodDecl_ci(
                 modl={$m.st}, 
                 gtpl={$g.st}, 
                 ty={$ty.st},
                 id={$IDENT.text}, 
                 fpl={$f.st}, 
-                adl={$a.st},
                 block={$b.st})
-        -> {emitCI() && !sdagMethod}? funcMethodDecl_ci(
+        ->
+    |   ^(SDAG_FUNCTION_DECL m=modifierList? g=genericTypeParameterList? 
+            ty=type IDENT f=formalParameterList a=domainExpression[null]? 
+            {
+            currentMethod = (MethodSymbol)$IDENT.def;
+            sdagMethod = currentMethod.hasSDAG;
+            }
+            ^(BLOCK (sdg+=sdagBasicBlock)*))
+            //sdgb=nakedSdagBlock?) 
+        -> {emitCI()}? funcMethodDecl_sdag_ci(
+                classSym={currentClass},
+                methodSym={currentMethod},
                 modl={$m.st}, 
                 gtpl={$g.st}, 
                 ty={$ty.st},
                 id={$IDENT.text}, 
                 fpl={$f.st}, 
-                block={$b.st})
+                adl={$a.st},
+                block={$sdg})
         ->
     |   ^(DIVCON_METHOD_DECL modifierList? type IDENT formalParameterList divconBlock)
     |   ^(PRIMITIVE_VAR_DECLARATION modifierList? simpleType variableDeclaratorList[null])
@@ -330,7 +349,6 @@ classScopeDeclaration
                 migrationCtor = currentClass.migrationCtor == $ENTRY_CONSTRUCTOR_DECL;
             }
             b=block)
-
         -> {emitCC()}? ctorDecl_cc(
                 modl={$m.st},
                 gtpl={$g.st}, 
@@ -658,9 +676,28 @@ block
         -> {((emitCC() && (currentMethod == null || !currentMethod.hasSDAG)) ||
             (emitCI() && (currentMethod != null && currentMethod.hasSDAG))) && emptyBlock}? template(bsl={$b}) "{ }"
         -> {emitCC() && (currentMethod == null || !currentMethod.hasSDAG)}? block_cc(bsl={$b})
-        -> {emitCI() && (currentMethod != null && currentMethod.hasSDAG)}? block_sdag_ci(bsl={$b})
+        -> {emitCI() && (currentMethod != null && currentMethod.hasSDAG)}? block_cc(bsl={$b})
         ->
     ;
+
+
+nakedSdagBlock
+    :   ^(BLOCK (sdg+=sdagBasicBlock)*)
+        -> template(sdg={$sdg}) "<sdg>"
+    ;
+
+
+sdagBlock
+    :   ^(BLOCK (sdg+=sdagBasicBlock)*)
+        -> block_cc(bsl={$sdg})
+    ;
+
+sdagBasicBlock
+    :   sdagStatement
+        -> {$sdagStatement.st}
+    |   (s+=statement)+
+        -> block_atomic(s={$s})
+    ;
     
 blockStatement
     :   localVariableDeclaration
@@ -687,8 +724,6 @@ localVariableDeclaration
 statement
     :   nonBlockStatement
         -> {$nonBlockStatement.st}
-    |   sdagStatement
-        -> {$sdagStatement.st}
     |   block
         -> {$block.st}
     ;
@@ -712,14 +747,21 @@ divconExpr
     ;
 
 sdagStatement
-    :   ^(OVERLAP block)
-        -> template(b={$block.st}) "} overlap <b> atomic {"
-    |   ^(WHEN (wa+=whenArgument)* block)
-        -> template(w={wa}, b={$block.st}) "} when <w> <b> atomic {"
-    |   ^(SDAG_IF parenthesizedExpression ifblock=block elseblock=block?)
-    |   ^(SDAG_FOR forInit? FOR_EXPR expression? FOR_UPDATE expression* block)
-    |   ^(SDAG_WHILE parenthesizedExpression block)
-    |   ^(SDAG_DO block parenthesizedExpression)
+    :   ^(OVERLAP sdagBlock)
+        -> template(b={$sdagBlock.st}) "overlap <b>"
+    |   ^(WHEN (wa+=whenArgument)* nakedSdagBlock)
+        -> template(w={wa}, b={$nakedSdagBlock.st}) "when <w> <b>"
+    |   ^(SDAG_IF pe=parenthesizedExpression
+            ifblock=sdagBlock elseblock=sdagBlock?)
+        -> if(cond={$pe.st}, then={$ifblock.st}, else_={$elseblock.st})
+    |   ^(SDAG_FOR forInit? FOR_EXPR cond=expression?
+            FOR_UPDATE (update+=expression)* b=sdagBlock)
+        -> for(initializer={$forInit.st}, cond={$cond.st},
+                update={$update}, body={$b.st})
+    |   ^(SDAG_WHILE pe=parenthesizedExpression b=sdagBlock)
+        -> while(cond={$pe.st}, body={$b.st})
+    |   ^(SDAG_DO b=sdagBlock pe=parenthesizedExpression)
+        -> dowhile(cond={$pe.st}, block={$b.st})
     ;
 
 whenArgument
index 6a6f073806d6f19c28a24c447f72fb4e0603f41f..2abadbf1c753a64c9918af59d43e2f142074c4ce 100644 (file)
@@ -73,7 +73,12 @@ public class Translator {
             if (t == null) {
                 return null;
             }
-            return create(((CharjAST)t).token);
+            CharjAST orig = (CharjAST)t;
+            CharjAST node = (CharjAST)create(orig.token);
+            node.def = orig.def;
+            node.symbolType = orig.symbolType;
+            node.scope = orig.scope;
+            return node;
         }
     };
 
@@ -155,8 +160,7 @@ public class Translator {
         m_nodes.reset();
         CharjASTModifier2 mod = new CharjASTModifier2(m_nodes);
         mod.setTreeAdaptor(m_adaptor);
-        //m_ast = (CommonTree)mod.charjSource(m_symtab).getTree();
-        mod.charjSource(m_symtab);
+        m_ast = (CommonTree)mod.charjSource(m_symtab).getTree();
         m_nodes = new CommonTreeNodeStream(m_ast);
         m_nodes.setTokenStream(m_tokens);
         m_nodes.setTreeAdaptor(m_adaptor);
diff --git a/src/langs/charj/tests/unit/SDAG.cj b/src/langs/charj/tests/unit/SDAG.cj
new file mode 100644 (file)
index 0000000..ee33bee
--- /dev/null
@@ -0,0 +1,46 @@
+
+package tests;
+
+readonly Main@ m;
+
+public mainchare Main {
+
+    public entry Main(CkArgMsg m) {
+    }
+}
+
+public chare SDAG_Test{
+
+    public entry SDAG_Test() {
+    }
+
+    public void x(int i) {
+    }
+
+    public entry void first(int f);
+    public entry void second(int s);
+    public entry void third(int t);
+
+    public entry void test1() {
+        x(0);
+        overlap { 
+            when first(int f) { x(f); }
+            when second(int s) x(s);
+        } 
+        x(1);
+        x(2);
+        for (int i=0; i<10; ++i) {
+            x(3);
+            for (int j=0; j<10; ++j) {
+                x(4);
+                x(5);
+            }
+            for (int j=0; j<10; ++j) {
+                when third(int t) {
+                    x(6);
+                    x(7);
+                }
+            }
+        }
+    }
+}