Exemple #1
0
 public int maxLocals() {
   // Be sure to add in local slots for each of the expression values...
   int maxLocals = 0;
   for (AST exp : exps) maxLocals = Math.max(maxLocals, exp.maxLocals());
   for (BArm arm : arms) maxLocals = Math.max(maxLocals, arm.maxLocals());
   return maxLocals + exps.length;
 }
Exemple #2
0
 private void compileApplyFun(
     List<FrameVar> locals, List<DynamicVar> dynamics, CodeBox code, boolean isLast) {
   code.add(new StartCall(getLineStart()), locals, dynamics);
   for (AST arg : args) arg.compile(locals, dynamics, code, false);
   Fun fun = (Fun) op;
   fun.compileApply(locals, dynamics, code, isLast);
 }
Exemple #3
0
 public void setPath(String path) {
   this.path = path;
   for (Binding b : bindings) b.setPath(path);
   init.setPath(path);
   name.setPath(path);
   for (BArm b : arms) b.setPath(path);
 }
Exemple #4
0
 private void compileApplyDynamic(
     List<FrameVar> locals, List<DynamicVar> dynamics, CodeBox code, boolean isLast) {
   code.add(new StartCall(getLineStart()), locals, dynamics);
   for (AST arg : args) arg.compile(locals, dynamics, code, false);
   Var v = (Var) op;
   lookup(v.name, dynamics).apply(args.length, getLineStart(), code, locals, dynamics, isLast);
 }
Exemple #5
0
 public Type type(Env<String, Type> env) {
   Type t = op.type(env).deref(env);
   ast.types.Fun funType =
       Type.expect(op.getLineStart(), op.getLineEnd(), ast.types.Fun.class, this, t);
   checkArgTypes(funType, env);
   setType(funType.getRange());
   return getType();
 }
Exemple #6
0
 public void compile(
     List<FrameVar> locals, List<DynamicVar> dynamics, CodeBox code, boolean isLast) {
   if (isApplyLocal(locals)) compileApplyLocal(locals, dynamics, code, isLast);
   else if (isApplyDynamic(dynamics)) compileApplyDynamic(locals, dynamics, code, isLast);
   else if (isApplyFun()) compileApplyFun(locals, dynamics, code, isLast);
   else {
     code.add(new StartCall(getLineStart()), locals, dynamics);
     for (AST arg : args) arg.compile(locals, dynamics, code, false);
     op.compile(locals, dynamics, code, false);
     code.add(new instrs.apply.Apply(getLineStart(), args.length), locals, dynamics);
   }
 }
Exemple #7
0
  public int maxLocals() {

    // This does not remove those bindings that will be implemented as
    // dynamic variables, however it is fail safe...

    int maxLocals = name.maxLocals() + init.maxLocals() + Binding.valueBindings(bindings).length;
    for (BArm arm : arms) maxLocals += arm.maxLocals();
    int valueLocals = 0;
    for (Binding b : Binding.valueBindings(bindings))
      valueLocals = Math.max(valueLocals, b.getValue().maxLocals());
    return maxLocals + valueLocals;
  }
Exemple #8
0
  public void compile(
      List<FrameVar> locals, List<DynamicVar> dynamics, CodeBox code, boolean isLast) {

    // The expressions matched by the case arms are set in the stack frame as
    // locals...

    int base = locals.length();
    int index = 0;
    for (AST exp : exps) {
      exp.compile(locals, dynamics, code, false);
      locals = locals.cons(new FrameVar("$case-" + (index++), locals.length()));
      code.add(new SetFrame(getLineStart(), locals.length() - 1), locals, dynamics);
      code.add(new Pop(getLineStart()), locals, dynamics);
    }
    compileArms(base, locals, dynamics, code, isLast);
  }
Exemple #9
0
  public void compile(
      List<FrameVar> locals, List<DynamicVar> dynamics, CodeBox code, boolean isLast) {

    // An act definition returns a behaviour. The bindings in the behaviour must
    // be constructed as recursive bindings *and* be dynamic variables so that
    // '.' can find them. Therefore, compile a behaviour as though it were
    // constructed in the context of a letrec, but force the bindings to be
    // dynamic...

    bindings = Binding.mergeBindings(bindings);

    for (Binding b : Binding.valueBindings(bindings)) {
      code.add(new Null(getLineStart()), locals, dynamics);
      code.add(new NewDynamic(getLineStart()), locals, dynamics);
      dynamics = dynamics.map(DynamicVar::incDynamic).cons(new DynamicVar(b.name, 0));
    }
    for (Binding b : Binding.valueBindings(bindings)) {
      b.getValue().compile(locals, dynamics, code, false);
      code.add(
          new SetDynamic(b.getValue().getLineStart(), lookup(b.name, dynamics).getIndex()),
          locals,
          dynamics);
      code.add(new Pop(b.getValue().getLineStart()), locals, dynamics);
    }
    orderExports(dynamics);
    name.compile(locals, dynamics, code, false);
    compileBehaviour(locals, dynamics, code);

    // Remove the dynamics...
    for (Binding b : Binding.valueBindings(bindings)) {
      code.add(new PopDynamic(getLineStart()), locals, dynamics);
      dynamics = dynamics.getTail();
    }
  }
Exemple #10
0
  public Type type(Env<String, Type> env) {

    // Check the bindings as through they were established in a letrec...

    env = env.bind("self", selfType(env));
    env = Binding.typeBindingsRec(bindings, env);
    init.type(env);
    name.type(env);
    MessageType[] handlers = new MessageType[arms.length];
    for (int i = 0; i < arms.length; i++) {
      HandlerType handlerType = arms[i].type(env);
      handlers[i] =
          new MessageType(
              handlerType.getLineStart(), handlerType.getLineEnd(), handlerType.getTypes());
    }
    setType(new ast.types.Act(getLineStart(), getLineEnd(), getExportedTypes(env), handlers));
    return getType();
  }
Exemple #11
0
 public void FV(HashSet<String> vars) {
   HashSet<String> bound = new HashSet<String>();
   for (Binding b : Binding.valueBindings(bindings)) {
     bound.add(b.getName());
   }
   for (Binding b : Binding.valueBindings(bindings)) {
     HashSet<String> free = new HashSet<String>();
     b.FV(free);
     free.removeAll(bound);
     vars.addAll(free);
   }
   HashSet<String> free = new HashSet<String>();
   for (BArm arm : arms) arm.FV(vars);
   init.FV(vars);
   name.FV(vars);
   free.removeAll(bound);
   vars.addAll(free);
 }
Exemple #12
0
 public AST subst(AST ast, String name) {
   return new Act(
       getLineStart(),
       path,
       this.name.subst(ast, name),
       exports,
       substBindings(ast, name),
       init.subst(ast, name),
       substArms(ast, name));
 }
Exemple #13
0
  public void compileBehaviour(List<FrameVar> locals, List<DynamicVar> dynamics, CodeBox code) {

    // Compilation of a behaviour produces a closure-like value that captures
    // the current
    // dynamics and waits to be transformed into an actor via 'new'.

    CodeBox bodyCode = new CodeBox(path, maxLocals() + 1);
    // Message will be local 0 in the stack frame...
    locals = new Nil<FrameVar>().cons(new FrameVar("$0", 0));
    bodyCode.add(new instrs.vars.FrameVar(getLineStart(), 0), locals, dynamics);
    Case handlers = new Case(getFirstHandlerLine(), getLineEnd(), new Dec[] {}, new AST[] {}, arms);
    handlers.compileArms(0, locals, dynamics, bodyCode, true);
    bodyCode.add(new Return(getLineStart()), locals, dynamics);
    int initIndex = bodyCode.getCode().size();
    init.compile(locals, dynamics, bodyCode, false);
    bodyCode.add(new PopFrame(getLineStart()), locals, dynamics);
    // Set the locals + 1 since the message is the first local...
    code.add(
        new instrs.data.Behaviour(
            getLineStart(), toKeys(exports.getStrings()), initIndex, bodyCode, handlesTime()),
        locals,
        dynamics);
  }
Exemple #14
0
 public void FV(HashSet<String> vars) {
   op.FV(vars);
   for (AST arg : args) arg.FV(vars);
 }
Exemple #15
0
 public void setPath(String path) {
   for (AST exp : exps) exp.setPath(path);
   for (BArm arm : arms) arm.setPath(path);
 }
Exemple #16
0
 public AST subst(AST ast, String name) {
   return new Apply(getLineStart(), getLineEnd(), op.subst(ast, name), subst(args, ast, name));
 }
Exemple #17
0
 public int maxLocals() {
   return Math.max(op.maxLocals(), maxLocals(args));
 }
Exemple #18
0
 public void setPath(String path) {
   op.setPath(path);
   for (AST arg : args) arg.setPath(path);
 }
Exemple #19
0
 public void visitKids(AST t) {
   for (AST kid : t.getKids()) {
     kid.accept(this);
   }
   return;
 }
Exemple #20
0
 public void DV(HashSet<String> vars) {
   for (AST exp : exps) exp.DV(vars);
   for (BArm arm : arms) arm.DV(vars);
 }