import java.io.*; import java.util.*; public class x86coder implements amvisitor { PrintWriter pr; codeblock cb=null; // code ArrayList strs; // string constants in the program String println(String s) // encapsulate try-catch { try { pr.println(s); } catch (Exception ie) {System.out.println(ie); System.exit(1);} return s; } // should let amcoder collect all strings public x86coder(String prefix, codeblock c, ArrayList scs) { cb = c; strs = scs; setup(prefix); } void setup(String prefix) { am7c.wordsize=32; am7c.regpool = new String[4]; am7c.regpool[0]="edx"; am7c.regpool[1]="ebx"; am7c.regpool[2]="ecx"; am7c.regpool[3]="edi"; am7c.sp = new register("esp"); // top of stack am7c.bp = new register("ebp"); // base of stack am7c.thisp = new register("esi"); // "this" object am7c.rtp = new register("eax"); // return val am7c.reserved = new register("eax"); try { String fname = prefix+".s"; pr = new PrintWriter(new FileWriter(fname)); pr.println("\t.file\t\""+fname+"\""); pr.println("\t.section\t .rodata"); for(int i=0;i=0) return "%"+am7c.regpool[r.ri]; // else return "%"+r.special; switch(r.special) { case "sp": return "%esp"; case "bp": return "%ebp"; case "thisp": return "%esi"; default: return "%eax"; } } public String visit(fcall fc) { return println("\tcall "+fc.dst.val); } public String visit(freturn fr) { return println("\tret"); } public String visit(aluop op) { String src=null; if (op.src!=null) src = op.src.accept(this); String dst = op.dst.accept(this); String s=""; switch(op.opcode) { case "+": s="addl"; break; case "*": s="imul"; break; case "-": s="subl"; break; case "/": s="idiv"; break; // not supported case "&&": s="andl"; break; case "||": s="orl"; break; // not supported case "!": s="not"; break; case "cmp": s="cmp"; break; default: s=null; } if (s==null) // must be compare op { s = "\txor %eax, %eax\n"; s += "\tcmp "+src+", "+dst+"\n"; if (op.opcode.equals("==")) s+= "\tsete %al"; else if (op.opcode.equals("<")) // opcode must be < s+= "\tsetl %al"; return println(s); } if (s.equals("not")) { s = "\tbtc $0, "+dst; return println(s); } if (src!=null) { //if (s.equals("cmp")) s = "\t"+s+" "+dst+", "+src; else s = "\t"+s+ " "+src+", "+dst; } else s = "\t"+s+" "+dst; return println(s); }//aluop public String visit(stackalloc sa) { int abssize = sa.size; if (abssize==0) return ""; if (abssize<0) abssize *=-1; int size = stackalloc.align(abssize) * stackalloc.direction; if (sa.size<0) size *= -1; return println("\taddl $"+size+", %esp"); } public String visit(move mv) { String s = mv.src.accept(this); String d = mv.dst.accept(this); if (s.equals(d)) return ""; if ((mv.src instanceof mem) && (mv.dst instanceof mem)) // mem-mem op { String t = "\tmovl "+s+", %eax\n"; t+= "\tmovl %eax, "+d; return println(t); } return println("\tmovl "+s+", "+d); } public String visit(push ph) { if (ph.src!=null) return println("\tpushl "+ph.src.accept(this)); String s = "\tpushl %edx\n\tpushl %ebx\n"; s += "\tpushl %ecx\n\tpushl %esi"; return println(s); } public String visit(pop ph) { if (ph.dst!=null) return println("\tpopl "+ph.dst.accept(this)); String s = "\tpopl %esi\n\tpopl %ecx\n"; s += "\tpopl %ebx\n\tpopl %edx"; return println(s); } public String visit(jump j) { String dst; if (j.operdst!=null) dst = j.operdst.accept(this); else dst = j.dst.val; if (j.cond==null) return println("\tjmp "+dst); return println("\tj"+j.cond+" "+dst); } public String visit(nop n) { return println("\tnop"); } public String visit(nothing n) { return ""; } public String visit(unpush up) { String s="\tsubl $"+(stackalloc.direction*am7c.wordsize/8)+", %esp"; return println(s); } public String visit(codeblock cb) { //System.out.println("CODEBLOCK: "+cb.comment); String s =""; if (cb.comment.equals("main")) s += ".globl "+cb.comment; if (!s.equals("")) println(s); for(operation op:cb.ops) { op.accept(this); } return s; } }//x86coder