private Node translateSJRecurse(Node parent, SJRecurse r, QQ qq) {
    if (!(parent instanceof Eval)) {
      throw new RuntimeException("[SJCompoundOperationTranslator] Shouldn't get here.");
    }

    String translation = "";
    List<Object> mapping = new LinkedList<Object>();

    translation += "%s = %E";
    mapping.add(getRecursionBooleanName(getSJSessionOperationExt(r).targetNames(), r.label()));
    mapping.add(r);

    return qq.parseExpr(translation, mapping.toArray());
  }
示例#2
0
  /** Flatten complex expressions within the AST */
  public Node leave(Node old, Node n, NodeVisitor v) {
    if (n == noFlatten) {
      noFlatten = null;
      return n;
    }

    if (n instanceof Block) {
      List l = (List) stack.removeFirst();
      return ((Block) n).statements(l);
    } else if (n instanceof Stmt && !(n instanceof LocalDecl)) {
      List l = (List) stack.getFirst();
      l.add(n);
      return n;
    } else if (n instanceof Expr
        && !(n instanceof Lit)
        && !(n instanceof Special)
        && !(n instanceof Local)) {

      Expr e = (Expr) n;

      if (e instanceof Assign) {
        return n;
      }

      // create a local temp, initialized to the value of the complex
      // expression

      String name = newID();
      LocalDecl def =
          nf.LocalDecl(
              e.position(), Flags.FINAL, nf.CanonicalTypeNode(e.position(), e.type()), name, e);
      def = def.localInstance(ts.localInstance(e.position(), Flags.FINAL, e.type(), name));

      List l = (List) stack.getFirst();
      l.add(def);

      // return the local temp instead of the complex expression
      Local use = nf.Local(e.position(), name);
      use = (Local) use.type(e.type());
      use = use.localInstance(ts.localInstance(e.position(), Flags.FINAL, e.type(), name));
      return use;
    }

    return n;
  }
  // FIXME: does not integrate with recursive session method calls: recursionEnter/Exit and also
  // recurse do not match the control flow of recursive calls, and hence runtime type monitoring
  // does not work.
  private Node translateSJRecursion(SJRecursion r, QQ qq)
        // recursionEnter inserted by node factory, but translation is finished here..
      {
    SJSessionOperationExt soe = getSJSessionOperationExt(r);

    Position pos = r.position();

    Collection<Object> mapping = new LinkedList<Object>();

    String bname = getRecursionBooleanName(soe.targetNames(), r.label());

    mapping.add(bname);
    mapping.add(bname);

    String translation = "for (boolean %s = true; %s; ) { }";
    For f = (For) qq.parseStmt(translation, mapping.toArray());

    mapping.clear();

    r = (SJRecursion) r.inits(f.inits());
    r = (SJRecursion) r.cond(f.cond());

    List stmts = new LinkedList();

    stmts.addAll(r.body().statements());

    translation = "%s = %E;";
    mapping.add(bname);
    mapping.add(((Eval) stmts.remove(0)).expr()); // Factor out constant.

    Eval e = (Eval) qq.parseStmt(translation, mapping.toArray());

    stmts.add(0, e);

    r = (SJRecursion) r.body(sjnf.Block(pos, stmts));

    /*// Appending the recursion-exit hook. // Disabled to support delegation from within recursion scopes (socket will be null on recursion-exit).
    List<Local> targets = new LinkedList<Local>(); // FIXME: should be SJLocalSockets.

    for (String sjname : soe.targetNames()) // Unicast optimisation for SJRecursionExit is done within the NodeFactory method - this pass comes after SJUnicastOptimiser.
    {
    	targets.add(sjnf.Local(pos, sjnf.Id(pos, sjname))); // Would it be bad to instead alias the recursionEnter targets?
    }

    SJRecursionExit re = sjnf.SJRecursionExit(pos, targets); // Problem: the sockets argument array is not yet filled (for other (internal) basic operations, this was done earlier by SJSessionOperationParser)...

    re = (SJRecursionExit) SJVariableParser.parseSJSessionOperation(this, re); // ...Current fix: use those routines form those earlier passes.
    re = (SJRecursionExit) SJSessionOperationParser.fixSJBasicOperationArguments(this, re);*/

    // return sjnf.Block(pos, r, sjnf.Eval(pos, re));
    return sjnf.Block(pos, r);
  }
  private Node translateSJOutinwhile(SJOutInwhile outinwhile, QQ qq) {
    List sources = outinwhile.insyncSources();
    List targets = outinwhile.outsyncTargets();

    String loopCond = UniqueID.newID("loopCond");
    String peerInterruptible = UniqueID.newID("peerInterruptible");

    List<Object> subst = new LinkedList<Object>();
    String code;

    // FIXME: this should be better factored. here, should treat sources and targets separately. but
    // also should integrate better with the same "optimisations" in the translation of in/outwhile
    if (sources.size() == 1 && targets.size() == 1) {
      String sourceName = ((Local) sources.get(0)).name();
      String targetName = ((Local) targets.get(0)).name();

      code = "{ sessionj.runtime.net.SJRuntime.negotiateOutsync(false, %s); ";
      subst.add(targetName);
      if (outinwhile.hasCondition()) {
        /*code += "boolean %s = ";
        subst.add(peerInterruptible);*/
        throw new RuntimeException("[SJCompoundOperation] TODO.");
      }
      code += "sessionj.runtime.net.SJRuntime.";
      if (outinwhile.hasCondition()) {
        // code += "negotiateInterruptingInwhile"
        throw new RuntimeException("[SJCompoundOperation] TODO.");
      } else {
        code += "negotiateNormalInwhile";
      }
      code += "(%s); while(sessionj.runtime.net.SJRuntime.outsync(";
      subst.add(sourceName);

      if (outinwhile.hasCondition()) {
        /*code += "interruptingInsync(%E, %s, %E)";
        subst.add(outinwhile.cond());
        subst.add(peerInterruptible);
        subst.add(sourcesArray);*/
        throw new RuntimeException("[SJCompoundOperation] TODO.");
      } else {
        code += "sessionj.runtime.net.SJRuntime.insync(%s)";
        subst.add(sourceName);
      }
      code += ", %s)) %S  }";
      code += "\n" + "%s.flush();"; // <By MQ> to flush sends after outwhile
      subst.add(targetName);
      subst.add(outinwhile.body());
      subst.add(targetName); // <By MQ>
    } else {
      Expr sourcesArray = buildNewArray(outinwhile.position(), sources); // inwhile sockets
      Expr targetsArray = buildNewArray(outinwhile.position(), targets); // outwhile sockets

      subst = new LinkedList<Object>(Arrays.asList(loopCond, targetsArray));
      code =
          "{ sessionj.runtime.net.LoopCondition %s = "
              + "sessionj.runtime.net.SJRuntime.negotiateOutsync(false, %E); ";
      if (outinwhile.hasCondition()) {
        code += "boolean %s = ";
        subst.add(peerInterruptible);
      }
      code += "sessionj.runtime.net.SJRuntime.";
      code += outinwhile.hasCondition() ? "negotiateInterruptingInwhile" : "negotiateNormalInwhile";
      code += "(%E); while(%s.call(sessionj.runtime.net.SJRuntime.";

      subst.add(sourcesArray);
      subst.add(loopCond);

      if (outinwhile.hasCondition()) {
        code += "interruptingInsync(%E, %s, %E)";
        subst.add(outinwhile.cond());
        subst.add(peerInterruptible);
        subst.add(sourcesArray);
      } else {
        code += "insync(%E)";
        subst.add(sourcesArray);
      }
      code += ")) %S  }";
      subst.add(outinwhile.body());
    }

    return qq.parseStmt(code, subst);
  }