// Ranjay public static ExpressionInstruction generateTile(MemIR mem, TempTable tTable) { ExpressionInstruction inst = new ExpressionInstruction(); SyntaxIR address = (SyntaxIR) mem.getChildren().get(0); Instruction childInst = generateTile(address, tTable); inst.addAll(childInst.getTiles()); AddressingMode addr = ((ExpressionInstruction) childInst).getAddr(); if (addr.getMemAccess()) { inst.add(new Tile(1, "movq", addr, new AddressingMode("r12"))); inst.setAddr(new AddressingMode(true, "r12")); } else { inst.setAddr(new AddressingMode(true, addr.getBaseReg())); } return inst; }
// Ranjay, public static ExpressionInstruction generateTile(ConstIR cons, TempTable tTable) { ExpressionInstruction inst = new ExpressionInstruction(); AddressingMode addr = new AddressingMode(cons.getValue()); inst.setAddr(addr); return inst; }
// Ranjay but needs to be optimized private static Instruction handleArguments(CallIR call, TempTable tTable) { Instruction inst = new Instruction(); // Now that we have saved the caller-saved registers we get the tilings // for the arguments given to the function ArrayList<ExpressionInstruction> argumentTileLists = new ArrayList<ExpressionInstruction>(); for (int i = 1; i < call.getChildren().size(); i++) { argumentTileLists.add( (ExpressionInstruction) generateTile((SyntaxIR) call.getChildren().get(i), tTable)); } // Now that we have the arguments we must push them on the stack and or registers AddressingMode addr12 = new AddressingMode("r12"); AddressingMode addrR9 = new AddressingMode("r9"); AddressingMode addrR8 = new AddressingMode("r8"); AddressingMode addrRDX = new AddressingMode("rdx"); AddressingMode addrRCX = new AddressingMode("rcx"); AddressingMode addrRSI = new AddressingMode("rsi"); AddressingMode addrRDI = new AddressingMode("rdi"); int retsize = call.getretsize(); if (retsize > 1) { CallIR c = new CallIR(new NameIR("_I_alloc_i"), new ConstIR(8 * (retsize + 1))); c.setretsize(1); ExpressionInstruction ex = (ExpressionInstruction) generateTile(c, tTable); inst.addAll(ex.getTiles()); inst.add(new Tile(1, "movq", ex.getAddr(), new AddressingMode("rcx"))); inst.add(new Tile(1, "movq", new AddressingMode(retsize), new AddressingMode(0, "rcx"))); inst.add(new Tile(1, "add", new AddressingMode(8), new AddressingMode("rcx"))); } if ((argumentTileLists.size() <= 4 && retsize <= 1) || (argumentTileLists.size() <= 3 && retsize > 1)) { for (int i = 0; i < argumentTileLists.size(); i++) { inst.addAll(argumentTileLists.get(i).getTiles()); inst.add(new Tile(1, "pushq", argumentTileLists.get(i).getAddr(), null)); } } else { if (retsize > 1) { for (int i = argumentTileLists.size() - 1; i >= 3; i--) { inst.addAll(argumentTileLists.get(i).getTiles()); inst.add(new Tile(1, "pushq", argumentTileLists.get(i).getAddr(), null)); } for (int i = 0; i < 3; i++) { inst.addAll(argumentTileLists.get(i).getTiles()); inst.add(new Tile(1, "pushq", argumentTileLists.get(i).getAddr(), null)); } } else { for (int i = argumentTileLists.size() - 1; i >= 4; i--) { inst.addAll(argumentTileLists.get(i).getTiles()); inst.add(new Tile(1, "pushq", argumentTileLists.get(i).getAddr(), null)); } for (int i = 0; i < 4; i++) { inst.addAll(argumentTileLists.get(i).getTiles()); inst.add(new Tile(1, "pushq", argumentTileLists.get(i).getAddr(), null)); } } } switch (argumentTileLists.size()) { case 4: if (retsize <= 1) { inst.add(new Tile(1, "popq", addrR9, null)); } case 3: if (retsize > 1) { inst.add(new Tile(1, "popq", addrR9, null)); } else { inst.add(new Tile(1, "popq", addrR8, null)); } case 2: if (retsize > 1) { inst.add(new Tile(1, "popq", addrR8, null)); } else { inst.add(new Tile(1, "popq", addrRDX, null)); } case 1: if (retsize > 1) { inst.add(new Tile(1, "popq", addrRDX, null)); } else { inst.add(new Tile(1, "popq", addrRCX, null)); } case 0: // Do nothing break; default: // we have more than five arguments // push the first 6 arguments to registers // and we're not using RDI (not returning a tuple) if (retsize <= 1) { inst.add(new Tile(1, "popq", addrR9, null)); inst.add(new Tile(1, "popq", addrR8, null)); inst.add(new Tile(1, "popq", addrRDX, null)); inst.add(new Tile(1, "popq", addrRCX, null)); } // we're returning a tuple else { inst.add(new Tile(1, "popq", addrR9, null)); inst.add(new Tile(1, "popq", addrR8, null)); inst.add(new Tile(1, "popq", addrRDX, null)); } } return inst; }
// Ranjay public static Instruction generateTile(CallIR call, TempTable tTable) { ExpressionInstruction inst = new ExpressionInstruction(); // Push caller saved: rcx,rdx,rsp,rsi,rdi,r8,r9,r10,r11 inst.add( new Tile( 1, "movq", new AddressingMode("rcx"), new AddressingMode( -1 * tTable.getCurrentFunction().getStackLocation().get("rcx"), "rbp"))); inst.add( new Tile( 1, "movq", new AddressingMode("rdx"), new AddressingMode( -1 * tTable.getCurrentFunction().getStackLocation().get("rdx"), "rbp"))); inst.add( new Tile( 1, "movq", new AddressingMode("rsp"), new AddressingMode( -1 * tTable.getCurrentFunction().getStackLocation().get("rsp"), "rbp"))); inst.add( new Tile( 1, "movq", new AddressingMode("rsi"), new AddressingMode( -1 * tTable.getCurrentFunction().getStackLocation().get("rsi"), "rbp"))); inst.add( new Tile( 1, "movq", new AddressingMode("rdi"), new AddressingMode( -1 * tTable.getCurrentFunction().getStackLocation().get("rdi"), "rbp"))); inst.add( new Tile( 1, "movq", new AddressingMode("r8"), new AddressingMode( -1 * tTable.getCurrentFunction().getStackLocation().get("r8"), "rbp"))); inst.add( new Tile( 1, "movq", new AddressingMode("r9"), new AddressingMode( -1 * tTable.getCurrentFunction().getStackLocation().get("r9"), "rbp"))); inst.add( new Tile( 1, "movq", new AddressingMode("r10"), new AddressingMode( -1 * tTable.getCurrentFunction().getStackLocation().get("r10"), "rbp"))); inst.add( new Tile( 1, "movq", new AddressingMode("r11"), new AddressingMode( -1 * tTable.getCurrentFunction().getStackLocation().get("r11"), "rbp"))); inst.add( new Tile( 1, "movq", new AddressingMode("r15"), new AddressingMode( -1 * tTable.getCurrentFunction().getStackLocation().get("r15"), "rbp"))); inst.add(new Tile(1, "pushq", new AddressingMode("rbx"), null)); inst.add(new Tile(1, "pushq", new AddressingMode("rbp"), null)); // handle arguments Instruction args = handleArguments(call, tTable); inst.addAll(args.getTiles()); // make call Instruction ooo = makeCall(call, tTable); inst.addAll(ooo.getTiles()); // pop caller saved inst.add(new Tile(1, "popq", new AddressingMode("rbp"), null)); inst.add(new Tile(1, "popq", new AddressingMode("rbx"), null)); inst.add( new Tile( 1, "movq", new AddressingMode( -1 * tTable.getCurrentFunction().getStackLocation().get("r15"), "rbp"), new AddressingMode("r15"))); inst.add( new Tile( 1, "movq", new AddressingMode( -1 * tTable.getCurrentFunction().getStackLocation().get("r11"), "rbp"), new AddressingMode("r11"))); inst.add( new Tile( 1, "movq", new AddressingMode( -1 * tTable.getCurrentFunction().getStackLocation().get("r10"), "rbp"), new AddressingMode("r10"))); inst.add( new Tile( 1, "movq", new AddressingMode( -1 * tTable.getCurrentFunction().getStackLocation().get("r9"), "rbp"), new AddressingMode("r9"))); inst.add( new Tile( 1, "movq", new AddressingMode( -1 * tTable.getCurrentFunction().getStackLocation().get("r8"), "rbp"), new AddressingMode("r8"))); inst.add( new Tile( 1, "movq", new AddressingMode( -1 * tTable.getCurrentFunction().getStackLocation().get("rdi"), "rbp"), new AddressingMode("rdi"))); inst.add( new Tile( 1, "movq", new AddressingMode( -1 * tTable.getCurrentFunction().getStackLocation().get("rsi"), "rbp"), new AddressingMode("rsi"))); inst.add( new Tile( 1, "movq", new AddressingMode( -1 * tTable.getCurrentFunction().getStackLocation().get("rsp"), "rbp"), new AddressingMode("rsp"))); inst.add( new Tile( 1, "movq", new AddressingMode( -1 * tTable.getCurrentFunction().getStackLocation().get("rdx"), "rbp"), new AddressingMode("rdx"))); inst.add( new Tile( 1, "movq", new AddressingMode( -1 * tTable.getCurrentFunction().getStackLocation().get("rcx"), "rbp"), new AddressingMode("rcx"))); inst.setAddr(new AddressingMode("rax")); return inst; }
// Ranjay public static ExpressionInstruction generateTile(OpIR op, TempTable tTable) { ExpressionInstruction inst = new ExpressionInstruction(); if (op.getChildren().size() > 1) { ExpressionInstruction child1 = (ExpressionInstruction) generateTile((SyntaxIR) op.getChildren().get(0), tTable); ExpressionInstruction child2 = (ExpressionInstruction) generateTile((SyntaxIR) op.getChildren().get(1), tTable); inst.addAll(child1.getTiles()); inst.add(new Tile(1, "pushq", child1.getAddr(), null)); inst.addAll(child2.getTiles()); inst.add(new Tile(1, "movq", child2.getAddr(), new AddressingMode("r12"))); inst.add(new Tile(1, "popq", new AddressingMode("r13"), null)); if (op.getOp() == opEnum.DIVIDE || op.getOp() == opEnum.MODULO) { inst.add(new Tile(1, "pushq", new AddressingMode("rdx"), null)); inst.add(new Tile(1, "pushq", new AddressingMode("rax"), null)); inst.add(new Tile(1, "movq", new AddressingMode("r13"), new AddressingMode("rax"))); // we need to sign extend r13 into rdx inst.add(new Tile(1, "cmp", new AddressingMode(0), new AddressingMode("r13"))); // if r12 is less than 0 we move FFFFFFFFFFFFFFFF to rdx otherwise move zero int div = tTable.getNewJumpLabel(); inst.add(new Tile(1, "jl", new AddressingMode("div" + div, true), null)); // fall through to moving zero inst.add(new Tile(1, "movq", new AddressingMode(0), new AddressingMode("rdx"))); inst.add(new Tile(1, "jmp", new AddressingMode("divEnd" + div, true), null)); inst.add(new Tile(1, "div" + div + ":", null, null)); inst.add(new Tile(1, "movq", new AddressingMode(-1), new AddressingMode("rdx"))); inst.add(new Tile(1, "divEnd" + div + ":", null, null)); inst.add(new Tile(1, "idiv", new AddressingMode("r12"), null)); // The result is put in RAX so we need to push that on the stack if (op.getOp() == opEnum.DIVIDE) inst.add(new Tile(1, "movq", new AddressingMode("rax"), new AddressingMode("r12"))); else inst.add(new Tile(1, "movq", new AddressingMode("rdx"), new AddressingMode("r12"))); inst.add(new Tile(1, "popq", new AddressingMode("rax"), null)); inst.add(new Tile(1, "popq", new AddressingMode("rdx"), null)); inst.setAddr(new AddressingMode("r12")); } else if (op.returnsBool() && op.getOp() != opEnum.AND && op.getOp() != opEnum.OR) { int x = tTable.getNewJumpLabel(); inst.add(new Tile(1, "cmp", new AddressingMode("r12"), new AddressingMode("r13"))); inst.add(new Tile(1, op.opToAssembly(), new AddressingMode("_true" + x, true), null)); inst.add(new Tile(1, "movq", new AddressingMode(0), new AddressingMode("r12"))); inst.add(new Tile(1, "jmp", new AddressingMode("_end" + x, true), null)); inst.add(new Tile(1, "_true" + x + ": ", null, null)); inst.add(new Tile(1, "movq", new AddressingMode(1), new AddressingMode("r12"))); inst.add(new Tile(1, "_end" + x + ": ", null, null)); inst.setAddr(new AddressingMode("r12")); } else if (op.getOp() == opEnum.LSHIFT || op.getOp() == opEnum.RSHIFT) { inst.add(new Tile(1, op.opToAssembly(), new AddressingMode(3), new AddressingMode("r13"))); inst.setAddr(new AddressingMode("r13")); } else { inst.add( new Tile(1, op.opToAssembly(), new AddressingMode("r12"), new AddressingMode("r13"))); inst.setAddr(new AddressingMode("r13")); } } else { ExpressionInstruction child = (ExpressionInstruction) generateTile((SyntaxIR) op.getChildren().get(0), tTable); inst.addAll(child.getTiles()); switch (op.getOp()) { case NOT: inst.add(new Tile(1, "btc", new AddressingMode(0), child.getAddr())); inst.setAddr(child.getAddr()); break; case UNARY_MINUS: inst.add(new Tile(1, "neg", child.getAddr(), null)); inst.setAddr(child.getAddr()); break; } } return inst; }
// Ranjay public static ExpressionInstruction generateTile(TempIR temp, TempTable tTable) { ExpressionInstruction inst = new ExpressionInstruction(); String funcname = tTable.getCurrentFunction().label(); int lastindex = funcname.lastIndexOf("_"); String letter = funcname.substring(lastindex + 1, lastindex + 2); String id = temp.getId(); if (id.startsWith("_ret")) { Integer offset = Integer.parseInt((id.substring(4, id.length()))); offset = 1 * (offset) * 8; inst.setAddr(new AddressingMode(offset, "rax")); } else if (id.startsWith("_rret")) { Integer offset = Integer.parseInt((id.substring(5, id.length()))); offset = 1 * (offset) * 8; inst.setAddr(new AddressingMode(offset, "rcx")); } else if (!(letter.equals("t"))) { if (id.startsWith("_args0")) inst.setAddr(new AddressingMode("rcx")); else if (id.startsWith("_args1")) inst.setAddr(new AddressingMode("rdx")); else if (id.startsWith("_args2")) inst.setAddr(new AddressingMode("r8")); else if (id.startsWith("_args3")) inst.setAddr(new AddressingMode("r9")); else if (id.startsWith("_args")) inst.setAddr( new AddressingMode((Integer.parseInt(id.substring(5, id.length())) - 5) * 8, "rbp")); else { // DANIEL!!, FIX line 203 & 226 so that HashMap<String, String> regs = tTable.getCurrentFunction().getRegAlloc(); // we can have more than 10 arguments HashMap<String, Integer> stack = tTable.getCurrentFunction().getStackLocation(); if (regs.containsKey(id)) { inst.setAddr(new AddressingMode(regs.get(id))); } else { // System.out.println(id); int offset = stack.get(id); inst.setAddr(new AddressingMode(-offset, "rbp")); } } } else { if (id.startsWith("_args0")) { inst.setAddr(new AddressingMode("rdx")); } else if (id.startsWith("_args1")) { inst.setAddr(new AddressingMode("r8")); } else if (id.startsWith("_args2")) { inst.setAddr(new AddressingMode("r9")); } else if (id.startsWith("_args")) { inst.setAddr( new AddressingMode((Integer.parseInt(id.substring(5, id.length())) - 3) * 8, "rbp")); } else { HashMap<String, String> regs = tTable.getCurrentFunction().getRegAlloc(); if (regs.containsKey(id)) { inst.setAddr(new AddressingMode(regs.get(id))); } else { HashMap<String, Integer> stack = tTable.getCurrentFunction().getStackLocation(); int offset = stack.get(id); inst.setAddr(new AddressingMode(-offset, "rbp")); } } } return inst; }