Example #1
0
  // Ast0.Block ---
  // Ast0.Stmt[] stmts;
  //
  // AG:
  //   code: {stmt.c}
  //
  static List<IR0.Inst> gen(Ast0.Block n) throws Exception {
    List<IR0.Inst> code = new ArrayList<IR0.Inst>();

    // ... need code ...
    for (Ast0.Stmt s : n.stmts) code.addAll(gen(s));
    return code;
  }
Example #2
0
  // Ast0.Assign ---
  // Ast0.Id lhs;
  // Ast0.Exp rhs;
  //
  // AG:
  //   code: rhs.c + "lhs.s = rhs.v"
  //
  static List<IR0.Inst> gen(Ast0.Assign n) throws Exception {
    List<IR0.Inst> code = new ArrayList<IR0.Inst>();

    // ... need code ...
    CodePack rhs = gen(n.rhs);
    code.addAll(rhs.code);
    code.add(new IR0.Move(new IR0.Id(n.lhs.nm), rhs.src));
    return code;
  }
Example #3
0
  // Ast0.Print ---
  // Ast0.Exp arg;
  //
  // AG:
  //   code: arg.c + "print (arg.v)"
  //
  static List<IR0.Inst> gen(Ast0.Print n) throws Exception {
    List<IR0.Inst> code = new ArrayList<IR0.Inst>();

    // ... need code ...
    CodePack arg = gen(n.arg);
    code.addAll(arg.code);
    code.add(new IR0.Print(arg.src));
    return code;
  }
Example #4
0
  // Ast0.Unop ---
  // Ast0.UOP op;
  // Ast0.Exp e;
  //
  // AG:
  //   newTemp: t
  //   code: e.c + "t = op e.v"
  //
  static CodePack gen(Ast0.Unop n) throws Exception {
    List<IR0.Inst> code = new ArrayList<IR0.Inst>();

    // ... need code ...
    IR0.Temp temp = new IR0.Temp();
    CodePack e = gen(n.e);
    code.addAll(e.code);
    IR0.UOP op = gen(n.op);
    code.add(new IR0.Unop(op, temp, e.src));
    return new CodePack(temp, code);
  }
Example #5
0
  // Ast0.Binop ---
  // Ast0.BOP op;
  // Ast0.Exp e1,e2;
  //
  // AG:
  //   newTemp: t
  //   code: e1.c + e2.c
  //         + "t = e1.v op e2.v"
  //
  static CodePack gen(Ast0.Binop n) throws Exception {
    List<IR0.Inst> code = new ArrayList<IR0.Inst>();

    // ... need code ...
    IR0.Temp temp = new IR0.Temp();
    CodePack e1 = gen(n.e1);
    CodePack e2 = gen(n.e2);
    code.addAll(e1.code);
    code.addAll(e2.code);
    IR0.BOP op = gen(n.op);
    code.add(new IR0.Binop(op, temp, e1.src, e2.src));
    return new CodePack(temp, code);
  }
Example #6
0
  // Ast0.While ---
  // Ast0.Exp cond;
  // Ast0.Stmt s;
  //
  // AG:
  //   newLabel: L1,L2
  //   code: "L1:"
  //         + cond.c
  //         + "if cond.v == false goto L2"
  //         + s.c
  //         + "goto L1"
  //         + "L2:"
  //
  static List<IR0.Inst> gen(Ast0.While n) throws Exception {
    List<IR0.Inst> code = new ArrayList<IR0.Inst>();

    // ... need code ...
    IR0.Label L1 = new IR0.Label();
    IR0.Label L2 = new IR0.Label();
    CodePack cond = gen(n.cond);
    code.add(new IR0.LabelDec(L1));
    code.addAll(cond.code);
    code.add(new IR0.CJump(IR0.ROP.EQ, cond.src, IR0.FALSE, L2));
    for (IR0.Inst s : gen(n.s)) code.add(s);
    code.add(new IR0.Jump(L1));
    code.add(new IR0.LabelDec(L2));
    return code;
  }
Example #7
0
 // Ast0.Program ---
 // Ast0.Stmt[] stmts;
 //
 // AG:
 //   code: stmts.c  -- append all individual stmt.c
 //
 public static IR0.Program gen(Ast0.Program n) throws Exception {
   List<IR0.Inst> code = new ArrayList<IR0.Inst>();
   for (Ast0.Stmt s : n.stmts) code.addAll(gen(s));
   return new IR0.Program(code);
 }