// 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 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; }