Fix up UDT arguments to entry methods
authorAaron Becker <akbecker@gmail.com>
Wed, 21 Mar 2012 21:37:58 +0000 (16:37 -0500)
committerAaron Becker <akbecker@gmail.com>
Wed, 21 Mar 2012 21:37:58 +0000 (16:37 -0500)
Passing user-defined types to entry methods require that they be value
types. Create references to those value type parameters for the rest of
the function to deal with so that function bodies are all just reference
types.

src/langs/charj/src/charj/translator/Charj.stg
src/langs/charj/src/charj/translator/CharjEmitter.g
src/langs/charj/src/charj/translator/MethodSymbol.java
src/langs/charj/src/charj/translator/Pair.java [new file with mode: 0644]
src/langs/charj/src/charj/translator/SymbolResolver.g

index bca4d105de6327dab97e08010d5745a2a8c94cf3..81117b9f20a763976882550e3af89cff673eb773 100644 (file)
@@ -423,11 +423,23 @@ funcMethodDecl_h(modl, gtpl, ty, id, fpl, adl, block) ::=
 <modl><ty><gtpl> <id><fpl><adl>;
 >>
 
+
+entryMethodDecl_h(modl, gtpl, ty, id, fpl, adl, block) ::=
+<<
+<modl><ty><gtpl> <id><fpl><adl>;
+>>
+
+
 funcMethodDecl_ci(modl, gtpl, ty, id, fpl, adl, block) ::=
 <<
 <modl><ty><gtpl> <id><fpl><adl>;
 >>
 
+entryMethodDecl_ci(modl, gtpl, ty, id, fpl, adl, block) ::=
+<<
+<modl><ty><gtpl> <id><fpl><adl>;
+>>
+
 
 funcMethodDecl_sdag_ci(classSym, methodSym, modl, gtpl, ty, id, fpl, adl, block) ::=
 <<
@@ -466,6 +478,24 @@ funcMethodDecl_cc(classSym, methodSym, modl, gtpl, ty, id, fpl, adl, block) ::=
 >>
 
 
+entryMethodDecl_cc(classSym, methodSym, modl, gtpl, ty, id, fpl, adl, block) ::=
+<<
+<if(block)>
+<modl><ty><gtpl> <classSym.Name>::<id><fpl><adl>
+{
+    <methodSym.entryArgInitializers; separator="\n">
+    #if _CHARJ_TRACE_ALL_METHODS<if(methodSym.isTraced)> || _CHARJ_TRACE_TRACED_METHODS<endif>
+    int _charj_method_trace_timer = CkWallTimer();
+    #endif
+    <block>
+    #if _CHARJ_TRACE_ALL_METHODS<if(methodSym.isTraced)> || _CHARJ_TRACE_TRACED_METHODS<endif>
+    traceUserBracketEvent(<methodSym.traceID>, _charj_method_trace_timer, CkWallTimer());
+    #endif
+}
+<endif>
+>>
+
+
 ctorDecl_ci(modl, gtpl, id, fpl, block) ::=
 <<
 <modl><gtpl><id><fpl>;
@@ -485,6 +515,25 @@ ctorDecl_cc(modl, gtpl, id, fpl, block) ::=
 >>
 
 
+entryCtorDecl_ci(modl, gtpl, id, fpl, block) ::=
+<<
+<modl><gtpl><id><fpl>;
+>>
+
+
+entryCtorDecl_h(modl, gtpl, id, fpl, block) ::=
+<<
+<modl><id><fpl>;
+>>
+
+
+entryCtorDecl_cc(modl, gtpl, id, fpl, block) ::=
+<<
+<modl><id>::<id><fpl>
+<block>
+>>
+
+
 block_cc(bsl, braces) ::=
 <<
 <if(braces)>{<endif>
@@ -643,12 +692,23 @@ formal_param_list(sdecl, vdecl) ::=
 >>
 
 
+entry_formal_param_list(sdecl) ::=
+<<
+(<sdecl; separator=", ">)
+>>
+
+
 formal_param_decl(modList, type, declID) ::=
 <<
 <if(modList)><modList> <endif><type> <declID>
 >>
 
 
+entry_formal_param_decl(modList, type, declID) ::=
+<<
+<if(modList)><modList> <endif><type> __<declID>
+>>
+
 assert(cond, msg) ::=
 <<
 <if(msg)>
index 34f9bbec6006df51847f8d0b25a1935d12c59a7e..81e708aed6e65b79d9b8891f5705565be638868b 100644 (file)
@@ -238,8 +238,9 @@ classScopeDeclaration
     boolean sdagMethod = false;
 }
     :   ^(FUNCTION_METHOD_DECL m=modifierList? g=genericTypeParameterList? 
-            ty=type IDENT f=formalParameterList
+            ty=type IDENT
             { currentMethod = (MethodSymbol)$IDENT.def; }
+            f=formalParameterList
             b=block?)
         -> {emitCC()}? funcMethodDecl_cc(
                 classSym={currentClass},
@@ -260,44 +261,46 @@ classScopeDeclaration
         -> {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]? 
+            ty=type IDENT
             {
                 currentMethod = (MethodSymbol)$IDENT.def;
                 sdagMethod = currentMethod.hasSDAG;
             }
+            ef=entryFormalParameterList a=domainExpression[null]? 
             b=block?) 
-        -> {emitCC()}? funcMethodDecl_cc(
+        -> {emitCC()}? entryMethodDecl_cc(
                 classSym={currentClass},
                 methodSym={currentMethod},
                 modl={$m.st}, 
                 gtpl={$g.st}, 
                 ty={$ty.st},
                 id={$IDENT.text}, 
-                fpl={$f.st}, 
+                fpl={$ef.st}, 
                 adl={$a.st},
                 block={$b.st})
-        -> {emitH()}? funcMethodDecl_h(
+        -> {emitH()}? entryMethodDecl_h(
                 modl={$m.st}, 
                 gtpl={$g.st}, 
                 ty={$ty.st},
                 id={$IDENT.text}, 
-                fpl={$f.st}, 
+                fpl={$ef.st}, 
                 adl={$a.st},
                 block={$b.st})
-        -> {emitCI()}? funcMethodDecl_ci(
+        -> {emitCI()}? entryMethodDecl_ci(
                 modl={$m.st}, 
                 gtpl={$g.st}, 
                 ty={$ty.st},
                 id={$IDENT.text}, 
-                fpl={$f.st}, 
+                fpl={$ef.st}, 
                 block={$b.st})
         ->
     |   ^(SDAG_FUNCTION_DECL m=modifierList? g=genericTypeParameterList? 
-            ty=type IDENT f=formalParameterList a=domainExpression[null]? 
+            ty=type IDENT
             {
             currentMethod = (MethodSymbol)$IDENT.def;
             sdagMethod = currentMethod.hasSDAG;
             }
+            ef=entryFormalParameterList a=domainExpression[null]? 
             ^(BLOCK (sdg+=sdagBasicBlock)*))
         -> {emitCI()}? funcMethodDecl_sdag_ci(
                 classSym={currentClass},
@@ -306,7 +309,7 @@ classScopeDeclaration
                 gtpl={$g.st}, 
                 ty={$ty.st},
                 id={$IDENT.text}, 
-                fpl={$f.st}, 
+                fpl={$ef.st}, 
                 adl={$a.st},
                 block={$sdg})
         ->
@@ -342,29 +345,29 @@ classScopeDeclaration
                 fpl={$f.st}, 
                 block={$b.st})
         ->
-    |   ^(ENTRY_CONSTRUCTOR_DECL m=modifierList? g=genericTypeParameterList? IDENT f=formalParameterList
+    |   ^(ENTRY_CONSTRUCTOR_DECL m=modifierList? g=genericTypeParameterList? IDENT ef=entryFormalParameterList
             {
                 currentMethod = (MethodSymbol)$IDENT.def;
                 migrationCtor = currentClass.migrationCtor == $ENTRY_CONSTRUCTOR_DECL;
             }
             b=block)
-        -> {emitCC()}? ctorDecl_cc(
+        -> {emitCC()}? entryCtorDecl_cc(
                 modl={$m.st},
                 gtpl={$g.st}, 
                 id={$IDENT.text}, 
-                fpl={$f.st}, 
+                fpl={$ef.st}, 
                 block={$b.st})
-        -> {emitCI() && !migrationCtor}? ctorDecl_ci(
+        -> {emitCI() && !migrationCtor}? entryCtorDecl_ci(
                 modl={$m.st},
                 gtpl={$g.st}, 
                 id={$IDENT.text}, 
-                fpl={$f.st}, 
+                fpl={$ef.st}, 
                 block={$b.st})
-        -> {emitH()}? ctorDecl_h(
+        -> {emitH()}? entryCtorDecl_h(
                 modl={$m.st},
                 gtpl={$g.st}, 
                 id={$IDENT.text}, 
-                fpl={$f.st}, 
+                fpl={$ef.st}, 
                 block={$b.st})
         ->
     ;
@@ -562,18 +565,27 @@ type
         -> {$simpleType.st}
     |   objectType 
         -> {$objectType.st}
-    |   VOID
-        {
-            $st = %{"void"};
-        }
+    |   VOID { $st = %{"void"}; }
+    ;
+
+typeInEntryDecl
+    :   ^(SIMPLE_TYPE primitiveType domainExpression[null]?)
+        -> simple_type(typeID={$primitiveType.st}, arrDeclList={$domainExpression.st})
+    |   ^(OBJECT_TYPE qualifiedTypeIdent domainExpression[null]?)
+        -> obj_type(typeID={$qualifiedTypeIdent.st}, arrDeclList={$domainExpression.st})
+    |   ^(POINTER_TYPE qualifiedTypeIdent domainExpression[null]?)
+        -> obj_type(typeID={$qualifiedTypeIdent.st}, arrDeclList={$domainExpression.st})
+    |   ^(REFERENCE_TYPE qualifiedTypeIdent domainExpression[null]?)
+        -> obj_type(typeID={$qualifiedTypeIdent.st}, arrDeclList={$domainExpression.st})
     ;
 
+
 simpleType
     :   ^(SIMPLE_TYPE primitiveType domainExpression[null]?)
         -> simple_type(typeID={$primitiveType.st}, arrDeclList={$domainExpression.st})
     ;
 
-objectType //[boolean forNewExpression]
+objectType
     : proxyType -> {$proxyType.st;}
     | nonProxyType -> {$nonProxyType.st}
     ;
@@ -652,10 +664,24 @@ genericTypeArgument
         -> template(t={$text}) "/* genericTypeArgument: wildcard bound types not implemented */ <t>"
     ;
 
+
+entryFormalParameterList
+    :   ^(FORMAL_PARAM_LIST (efp+=entryFormalParameter)*)
+        -> entry_formal_param_list(sdecl={$efp})
+    ;
+
+
+entryFormalParameter
+    :   ^(FORMAL_PARAM_STD_DECL t=typeInEntryDecl vdid=variableDeclaratorId)
+        -> entry_formal_param_decl(type={$t.st}, declID={$vdid.st})
+    ;
+
+
 formalParameterList
     :   ^(FORMAL_PARAM_LIST (fpsd+=formalParameterStandardDecl)* fpvd=formalParameterVarargDecl?)
         -> formal_param_list(sdecl={$fpsd}, vdecl={$fpvd.st})
     ;
+
     
 formalParameterStandardDecl
     :   ^(FORMAL_PARAM_STD_DECL lms=localModifierList? t=type vdid=variableDeclaratorId)
@@ -968,7 +994,7 @@ primaryExpression
     |   ^(METHOD_CALL pe=primaryExpression gtal=genericTypeArgumentList? args=arguments)
         -> method_call(primary={$pe.st}, generic_types={$gtal.st}, args={$args.st})
     |   ^(ENTRY_METHOD_CALL pe=primaryExpression gtal=genericTypeArgumentList? args=arguments)
-        -> method_call(primary={$pe.st}, generic_types={$gtal.st}, args={$args.st})
+        -> entry_method_call(primary={$pe.st}, generic_types={$gtal.st}, args={$args.st})
     |   explicitConstructorCall
         -> {$explicitConstructorCall.st}
     |   ^(ARRAY_ELEMENT_ACCESS pe=primaryExpression ex=expressionArrayAccess) {
index 3e20ea25959bf475a9df998aeb14568639eccb3d..611e370bb4e96a0b581ec976d5bd3042d55b7ef3 100644 (file)
@@ -4,6 +4,8 @@ package charj.translator;
 import org.antlr.stringtemplate.StringTemplate;
 import org.antlr.stringtemplate.language.AngleBracketTemplateLexer;
 
+import java.util.List;
+import java.util.ArrayList;
 import java.util.LinkedHashMap;
 import java.util.Map;
 
@@ -18,6 +20,7 @@ public class MethodSymbol
 
     /** The list of local variables defined anywhere in the method */
     LocalScope locals;
+    Map<String, String> entryArgs = new LinkedHashMap();
 
     public boolean isEntry = false;
     public boolean isStatic = false;
@@ -154,4 +157,20 @@ public class MethodSymbol
         }
         return mangled;
     }
+
+    public void addEntryArg(String type, String name)
+    {
+        entryArgs.put(type, name);
+    }
+
+    public List<String> getEntryArgInitializers()
+    {
+        List<String> inits = new ArrayList<String>();
+        for (Map.Entry<String, String> entry : entryArgs.entrySet()) {
+            String name = entry.getValue();
+            String type = entry.getKey();
+            inits.add(type + "* " + name + " = &__" + name + ";");
+        }
+        return inits;
+    }
 }
diff --git a/src/langs/charj/src/charj/translator/Pair.java b/src/langs/charj/src/charj/translator/Pair.java
new file mode 100644 (file)
index 0000000..d95b48f
--- /dev/null
@@ -0,0 +1,54 @@
+
+package charj.translator;
+
+import java.lang.*;
+
+public class Pair<FIRST, SECOND> implements Comparable<Pair<FIRST, SECOND>> {
+    public final FIRST first;
+    public final SECOND second;
+
+    private Pair(FIRST first, SECOND second) {
+        this.first = first;
+        this.second = second;
+    }
+
+    public static <FIRST, SECOND> Pair<FIRST, SECOND> of(FIRST first, SECOND second) {
+        return new Pair<FIRST, SECOND>(first, second);
+    }
+
+    @Override
+    public int compareTo(Pair<FIRST, SECOND> o) {
+        int cmp = compare(first, o.first);
+        return cmp == 0 ? compare(second, o.second) : cmp;
+    }
+
+    private static int compare(Object o1, Object o2) {
+        return o1 == null ? o2 == null ? 0 : -1 : o2 == null ? +1 : ((Comparable) o1).compareTo(o2);
+    }
+
+    @Override
+    public int hashCode() {
+        return 31 * hashcode(first) + hashcode(second);
+    }
+
+    private static int hashcode(Object o) {
+        return o == null ? 0 : o.hashCode();
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (!(obj instanceof Pair)) return false;
+        if (this == obj) return true;
+        return equal(first, ((Pair) obj).first) && equal(second, ((Pair) obj).second);
+    }
+
+    private boolean equal(Object o1, Object o2) {
+        return o1 == null ? o2 == null : (o1 == o2 || o1.equals(o2));
+    }
+
+    @Override
+    public String toString() {
+        return "(" + first + ", " + second + ')';
+    }
+}
+
index b761d794a9467db967501e1f6378aaf208196438..b46d7ff8e35e5e0955deb83d5317834e1988bfb3 100644 (file)
@@ -44,7 +44,18 @@ enterMethod
     :   ^((FUNCTION_METHOD_DECL | ENTRY_FUNCTION_DECL)
             (^(MODIFIER_LIST .*))?
             (^(GENERIC_TYPE_PARAM_LIST .*))? 
-            type IDENT .*) { currentMethod = (MethodSymbol)$IDENT.def; }
+            type funcid=IDENT { currentMethod = (MethodSymbol)$funcid.def; }
+            ^(FORMAL_PARAM_LIST (^(FORMAL_PARAM_STD_DECL type ^(argid=IDENT .*)
+            {
+                if (currentMethod.isEntry) {
+                    Type argType = (Type)$argid.def.type;
+                    currentMethod.addEntryArg(argType.getTypeName(), $argid.text);
+                    System.out.println("processing method argument " + $argid.text);
+                    System.out.println("type is " + argType.getTypeName());
+                }
+            }
+            ))*)
+            .*)
     |   ^((CONSTRUCTOR_DECL | ENTRY_CONSTRUCTOR_DECL)
             (^(MODIFIER_LIST .*))?
             (^(GENERIC_TYPE_PARAM_LIST .*))?