1 package org.ibex.nestedvm;
4 import org.ibex.nestedvm.util.*;
6 import org.apache.bcel.generic.*;
8 // FEATURE: Use BCEL to do peephole optimization
9 // FEATURE: Special mode to support single-precision only - regs are floats not ints
11 /* FEATURE: Span large binaries across several classfiles
12 * We should be able to do this with no performance penalty
13 * Every method in the inner classes is static and takes the main class as an arg
14 * This makes them look just like methods in the main class because arg1 gets loaded into
18 /* FEATURE: smarter with local regs
19 * Be even smarter with the use of local registers. We need to only load fields into
20 * local regs when they are actually used and only write to fields if the regs could have
21 * changed. This should allow us to put more regs in local vars. Right now putting all used
22 * regs local vars makes code like this slower.
24 * void work(int a, int b) {
31 * Because all the regs used in "real work" are loaded/restored even for fast path
35 public class ClassFileCompiler extends Compiler implements org.apache.bcel.Constants {
36 /** The stream to write the compiled output to */
37 private OutputStream os;
38 private PrintStream warn = System.err;
41 private ConstantPoolGen cp;
42 private InstructionList clinitExtras = new InstructionList();
43 private InstructionList initExtras = new InstructionList();
44 private InstructionFactory fac;
46 // Handy wrappers around the BCEL functions
47 private InstructionList insnList;
48 private void selectMethod(MethodGen m) { insnList = m.getInstructionList(); }
49 private void selectList(InstructionList l) { insnList = l; }
50 private InstructionHandle a(Instruction i) { return insnList.append(i); }
51 private BranchHandle a(BranchInstruction i) { return insnList.append(i); }
52 private InstructionHandle a(InstructionList l) { return insnList.append(l); }
53 private InstructionHandle a(CompoundInstruction c) { return insnList.append(c); }
55 // This works around a bug in InstructionList
56 // FEATURE: fix this in bcel, send them a patch, etc
57 private static class FixedInstructionList extends InstructionList {
58 public void move(InstructionHandle start, InstructionHandle end, InstructionHandle target) {
59 InstructionHandle extra = target != null && target == getEnd() ? append(InstructionConstants.NOP) : null;
60 super.move(start,end,target);
61 if(extra != null) try { delete(extra); } catch (TargetLostException e) { /* won't happen */ }
65 private MethodGen newMethod(int flags, Type ret, Type[] args, String name) {
66 return new MethodGen(flags,ret,args,null,name,fullClassName,new FixedInstructionList(),cp);
69 public ClassFileCompiler(String path, String className, OutputStream os) throws IOException { this(new Seekable.File(path),className,os); }
70 public ClassFileCompiler(Seekable binary, String className, OutputStream os) throws IOException {
71 super(binary,className);
75 public void setWarnWriter(PrintStream warn) { this.warn = warn; }
77 protected void _go() throws Exn, IOException {
78 if(lessConstants) throw new Exn("ClassFileCompiler doesn't support -o lessconstants");
79 if(!pruneCases) throw new Exn("-o prunecases MUST be enabled for ClassFileCompiler");
82 cl = new ClassGen(fullClassName,runtimeClass,source,ACC_SUPER|ACC_PUBLIC|ACC_FINAL,null);
83 cp = cl.getConstantPool();
84 fac = new InstructionFactory(cl,cp);
87 cl.addField(new FieldGen(ACC_PRIVATE,Type.INT,"pc",cp).getField());
89 cl.addField(new FieldGen(ACC_PRIVATE,Type.INT,"r"+i,cp).getField());
91 cl.addField(new FieldGen(ACC_PRIVATE,Type.INT,"f"+i,cp).getField());
93 cl.addField(new FieldGen(ACC_PRIVATE,Type.INT,"hi",cp).getField());
94 cl.addField(new FieldGen(ACC_PRIVATE,Type.INT,"lo",cp).getField());
95 cl.addField(new FieldGen(ACC_PRIVATE,Type.INT,"fcsr",cp).getField());
98 cl.addField(new FieldGen(ACC_PRIVATE|ACC_FINAL,new ArrayType(Type.INT,1),"page",cp).getField());
100 selectList(initExtras);
101 a(InstructionConstants.ALOAD_0);
102 a(InstructionConstants.DUP);
103 a(fac.createFieldAccess(fullClassName,"readPages",new ArrayType(Type.INT, 2), GETFIELD));
105 a(InstructionConstants.AALOAD);
106 a(fac.createFieldAccess(fullClassName,"page",new ArrayType(Type.INT,1), PUTFIELD));
110 cl.addField(new FieldGen(ACC_PRIVATE|ACC_STATIC|ACC_FINAL,Type.getType("L"+hashClass.replace('.','/')+";"),"symbols",cp).getField());
114 for(int i=0;i<elf.sheaders.length;i++) {
115 ELF.SHeader sheader = elf.sheaders[i];
116 String name = sheader.name;
117 // if this section doesn't get loaded into our address space don't worry about it
118 if(sheader.addr == 0x0) continue;
120 highestAddr = Math.max(highestAddr, sheader.addr + sheader.size);
122 if(name.equals(".text"))
123 emitText(sheader.addr, new DataInputStream(sheader.getInputStream()),sheader.size);
124 else if(name.equals(".data") || name.equals(".sdata") || name.equals(".rodata") || name.equals(".ctors") || name.equals(".dtors"))
125 emitData(sheader.addr, new DataInputStream(sheader.getInputStream()), sheader.size,name.equals(".rodata"));
126 else if(name.equals(".bss") || name.equals(".sbss"))
127 emitBSS(sheader.addr,sheader.size);
129 throw new Exn("Unknown segment: " + name);
132 ELF.SHeader text = elf.sectionWithName(".text");
135 MethodGen tramp = newMethod(ACC_PRIVATE,Type.VOID, Type.NO_ARGS, "trampoline");
136 tramp.addException("org.ibex.nestedvm.Runtime$ExecutionException");
139 InstructionHandle start = a(InstructionConstants.ALOAD_0);
140 a(fac.createFieldAccess(fullClassName,"state",Type.INT, GETFIELD));
141 pushConst(Runtime.RUNNING);
142 BranchInstruction stateCheck = InstructionFactory.createBranchInstruction(IF_ICMPNE,null);
144 a(InstructionConstants.ALOAD_0);
145 a(InstructionConstants.ALOAD_0);
146 a(fac.createFieldAccess(fullClassName,"pc",Type.INT, GETFIELD));
147 pushConst(methodShift);
148 a(InstructionConstants.IUSHR);
150 int beg = text.addr >>> methodShift;
151 int end = ((text.addr + text.size) >>> methodShift);
153 // This data is redundant but BCEL wants it
154 int[] matches = new int[end-beg];
155 for(int i=beg;i<end;i++) matches[i-beg] = i;
156 TABLESWITCH ts = new TABLESWITCH(matches,new InstructionHandle[matches.length],null);
158 for(int n=beg;n<end;n++){
159 InstructionHandle h = a(fac.createInvoke(fullClassName,"run_"+toHex(n<<methodShift),Type.VOID,Type.NO_ARGS,INVOKESPECIAL));
160 a(InstructionFactory.createBranchInstruction(GOTO,start));
161 ts.setTarget(n-beg,h);
164 ts.setTarget(a(InstructionConstants.POP)); // default case
165 a(fac.createNew("org.ibex.nestedvm.Runtime$ExecutionException"));
166 a(InstructionConstants.DUP);
167 a(fac.createNew("java.lang.StringBuffer"));
168 a(InstructionConstants.DUP);
169 a(new PUSH(cp,"Jumped to invalid address in trampoline (r2: "));
170 a(fac.createInvoke("java.lang.StringBuffer","<init>",Type.VOID,new Type[]{Type.STRING},INVOKESPECIAL));
171 a(InstructionConstants.ALOAD_0);
172 a(fac.createFieldAccess(fullClassName,"r2",Type.INT, GETFIELD));
173 a(fac.createInvoke("java.lang.StringBuffer","append",Type.STRINGBUFFER,new Type[]{Type.INT},INVOKEVIRTUAL));
174 a(new PUSH(cp," pc:"));
175 a(fac.createInvoke("java.lang.StringBuffer","append",Type.STRINGBUFFER,new Type[]{Type.STRING},INVOKEVIRTUAL));
176 a(InstructionConstants.ALOAD_0);
177 a(fac.createFieldAccess(fullClassName,"pc",Type.INT, GETFIELD));
178 a(fac.createInvoke("java.lang.StringBuffer","append",Type.STRINGBUFFER,new Type[]{Type.INT},INVOKEVIRTUAL));
180 a(fac.createInvoke("java.lang.StringBuffer","append",Type.STRINGBUFFER,new Type[]{Type.CHAR},INVOKEVIRTUAL));
181 a(fac.createInvoke("java.lang.StringBuffer","toString",Type.STRING,Type.NO_ARGS,INVOKEVIRTUAL));
182 a(fac.createInvoke("org.ibex.nestedvm.Runtime$ExecutionException","<init>",Type.VOID,new Type[]{Type.STRING},INVOKESPECIAL));
183 a(InstructionConstants.ATHROW);
185 stateCheck.setTarget(a(InstructionConstants.RETURN));
188 tramp.setMaxLocals();
190 cl.addMethod(tramp.getMethod());
191 } catch(ClassGenException e) {
192 e.printStackTrace(warn);
193 throw new Exn("Generation of the trampoline method failed. Try increasing maxInsnPerMethod");
196 addConstReturnMethod("gp",gp.addr);
197 addConstReturnMethod("entryPoint",elf.header.entry);
198 addConstReturnMethod("heapStart",highestAddr);
200 if(userInfo != null) {
201 addConstReturnMethod("userInfoBase",userInfo.addr);
202 addConstReturnMethod("userInfoSize",userInfo.size);
205 // FEATURE: Allow specification of memory size at runtime (numpages)
207 MethodGen init = newMethod(ACC_PUBLIC,Type.VOID, Type.NO_ARGS, "<init>");
209 a(InstructionConstants.ALOAD_0);
211 pushConst(totalPages);
212 a(fac.createInvoke(runtimeClass,"<init>",Type.VOID,new Type[]{Type.INT,Type.INT},INVOKESPECIAL));
216 a(InstructionConstants.RETURN);
220 cl.addMethod(init.getMethod());
223 MethodGen clinit = newMethod(ACC_PRIVATE|ACC_STATIC,Type.VOID, Type.NO_ARGS, "<clinit>");
224 selectMethod(clinit);
228 a(fac.createNew(hashClass));
229 a(InstructionConstants.DUP);
230 a(InstructionConstants.DUP);
231 a(fac.createInvoke(hashClass,"<init>",Type.VOID,Type.NO_ARGS,INVOKESPECIAL));
232 a(fac.createFieldAccess(fullClassName,"symbols",Type.getType("L"+hashClass.replace('.','/')+";"), PUTSTATIC));
233 ELF.Symbol[] symbols = elf.getSymtab().symbols;
234 for(int i=0;i<symbols.length;i++) {
235 ELF.Symbol s = symbols[i];
236 if(s.type == ELF.Symbol.STT_FUNC && s.binding == ELF.Symbol.STB_GLOBAL && (s.name.equals("_call_helper") || !s.name.startsWith("_"))) {
237 a(InstructionConstants.DUP);
238 a(new PUSH(cp,s.name));
239 a(fac.createNew("java.lang.Integer"));
240 a(InstructionConstants.DUP);
241 a(new PUSH(cp,s.addr));
242 a(fac.createInvoke("java.lang.Integer","<init>",Type.VOID,new Type[]{Type.INT},INVOKESPECIAL));
243 a(fac.createInvoke(hashClass,"put",Type.OBJECT,new Type[]{Type.OBJECT,Type.OBJECT},INVOKEVIRTUAL));
244 a(InstructionConstants.POP);
247 a(InstructionConstants.POP);
250 a(InstructionConstants.RETURN);
251 clinit.setMaxLocals();
252 clinit.setMaxStack();
253 cl.addMethod(clinit.getMethod());
256 MethodGen lookupSymbol = newMethod(ACC_PROTECTED,Type.INT,new Type[]{Type.STRING},"lookupSymbol");
257 selectMethod(lookupSymbol);
258 a(fac.createFieldAccess(fullClassName,"symbols",Type.getType("L"+hashClass.replace('.','/')+";"), GETSTATIC));
259 a(InstructionConstants.ALOAD_1);
260 a(fac.createInvoke(hashClass,"get",Type.OBJECT,new Type[]{Type.OBJECT},INVOKEVIRTUAL));
261 a(InstructionConstants.DUP);
262 BranchHandle bh = a(InstructionFactory.createBranchInstruction(IFNULL,null));
263 a(fac.createCheckCast(new ObjectType("java.lang.Integer")));
264 a(fac.createInvoke("java.lang.Integer","intValue",Type.INT,Type.NO_ARGS,INVOKEVIRTUAL));
265 a(InstructionConstants.IRETURN);
266 bh.setTarget(a(InstructionConstants.ICONST_M1));
267 a(InstructionConstants.IRETURN);
268 lookupSymbol.setMaxLocals();
269 lookupSymbol.setMaxStack();
270 cl.addMethod(lookupSymbol.getMethod());
273 MethodGen setCPUState = newMethod(ACC_PROTECTED,Type.VOID,new Type[]{Type.getType("Lorg/ibex/nestedvm/Runtime$CPUState;")},"setCPUState");
274 selectMethod(setCPUState);
275 a(InstructionConstants.ALOAD_1);
276 a(fac.createFieldAccess("org.ibex.nestedvm.Runtime$CPUState","r",new ArrayType(Type.INT,1),GETFIELD));
277 a(InstructionConstants.ASTORE_2);
278 for(int i=1;i<32;i++) {
279 a(InstructionConstants.ALOAD_0);
280 a(InstructionConstants.ALOAD_2);
282 a(InstructionConstants.IALOAD);
283 a(fac.createFieldAccess(fullClassName,"r"+i,Type.INT, PUTFIELD));
285 a(InstructionConstants.ALOAD_1);
286 a(fac.createFieldAccess("org.ibex.nestedvm.Runtime$CPUState","f",new ArrayType(Type.INT,1),GETFIELD));
287 a(InstructionConstants.ASTORE_2);
288 for(int i=0;i<32;i++) {
289 a(InstructionConstants.ALOAD_0);
290 a(InstructionConstants.ALOAD_2);
292 a(InstructionConstants.IALOAD);
293 a(fac.createFieldAccess(fullClassName,"f"+i,Type.INT, PUTFIELD));
295 a(InstructionConstants.ALOAD_0);
296 a(InstructionConstants.ALOAD_1);
297 a(fac.createFieldAccess("org.ibex.nestedvm.Runtime$CPUState","hi",Type.INT,GETFIELD));
298 a(fac.createFieldAccess(fullClassName,"hi",Type.INT, PUTFIELD));
299 a(InstructionConstants.ALOAD_0);
300 a(InstructionConstants.ALOAD_1);
301 a(fac.createFieldAccess("org.ibex.nestedvm.Runtime$CPUState","lo",Type.INT,GETFIELD));
302 a(fac.createFieldAccess(fullClassName,"lo",Type.INT, PUTFIELD));
303 a(InstructionConstants.ALOAD_0);
304 a(InstructionConstants.ALOAD_1);
305 a(fac.createFieldAccess("org.ibex.nestedvm.Runtime$CPUState","fcsr",Type.INT,GETFIELD));
306 a(fac.createFieldAccess(fullClassName,"fcsr",Type.INT, PUTFIELD));
307 a(InstructionConstants.ALOAD_0);
308 a(InstructionConstants.ALOAD_1);
309 a(fac.createFieldAccess("org.ibex.nestedvm.Runtime$CPUState","pc",Type.INT,GETFIELD));
310 a(fac.createFieldAccess(fullClassName,"pc",Type.INT, PUTFIELD));
312 a(InstructionConstants.RETURN);
313 setCPUState.setMaxLocals();
314 setCPUState.setMaxStack();
315 cl.addMethod(setCPUState.getMethod());
317 MethodGen getCPUState = newMethod(ACC_PROTECTED,Type.VOID,new Type[]{Type.getType("Lorg/ibex/nestedvm/Runtime$CPUState;")},"getCPUState");
318 selectMethod(getCPUState);
319 a(InstructionConstants.ALOAD_1);
320 a(fac.createFieldAccess("org.ibex.nestedvm.Runtime$CPUState","r",new ArrayType(Type.INT,1),GETFIELD));
321 a(InstructionConstants.ASTORE_2);
322 for(int i=1;i<32;i++) {
323 a(InstructionConstants.ALOAD_2);
325 a(InstructionConstants.ALOAD_0);
326 a(fac.createFieldAccess(fullClassName,"r"+i,Type.INT, GETFIELD));
327 a(InstructionConstants.IASTORE);
330 a(InstructionConstants.ALOAD_1);
331 a(fac.createFieldAccess("org.ibex.nestedvm.Runtime$CPUState","f",new ArrayType(Type.INT,1),GETFIELD));
332 a(InstructionConstants.ASTORE_2);
333 for(int i=0;i<32;i++) {
334 a(InstructionConstants.ALOAD_2);
336 a(InstructionConstants.ALOAD_0);
337 a(fac.createFieldAccess(fullClassName,"f"+i,Type.INT, GETFIELD));
338 a(InstructionConstants.IASTORE);
340 a(InstructionConstants.ALOAD_1);
341 a(InstructionConstants.ALOAD_0);
342 a(fac.createFieldAccess(fullClassName,"hi",Type.INT, GETFIELD));
343 a(fac.createFieldAccess("org.ibex.nestedvm.Runtime$CPUState","hi",Type.INT,PUTFIELD));
344 a(InstructionConstants.ALOAD_1);
345 a(InstructionConstants.ALOAD_0);
346 a(fac.createFieldAccess(fullClassName,"lo",Type.INT, GETFIELD));
347 a(fac.createFieldAccess("org.ibex.nestedvm.Runtime$CPUState","lo",Type.INT,PUTFIELD));
348 a(InstructionConstants.ALOAD_1);
349 a(InstructionConstants.ALOAD_0);
350 a(fac.createFieldAccess(fullClassName,"fcsr",Type.INT, GETFIELD));
351 a(fac.createFieldAccess("org.ibex.nestedvm.Runtime$CPUState","fcsr",Type.INT,PUTFIELD));
352 a(InstructionConstants.ALOAD_1);
353 a(InstructionConstants.ALOAD_0);
354 a(fac.createFieldAccess(fullClassName,"pc",Type.INT, GETFIELD));
355 a(fac.createFieldAccess("org.ibex.nestedvm.Runtime$CPUState","pc",Type.INT,PUTFIELD));
357 a(InstructionConstants.RETURN);
358 getCPUState.setMaxLocals();
359 getCPUState.setMaxStack();
360 cl.addMethod(getCPUState.getMethod());
363 // FEATURE: Catch RuntimeException and turn it into a fault exception
364 MethodGen execute = newMethod(ACC_PROTECTED,Type.VOID,Type.NO_ARGS,"_execute");
365 selectMethod(execute);
366 a(InstructionConstants.ALOAD_0);
367 a(fac.createInvoke(fullClassName,"trampoline",Type.VOID,Type.NO_ARGS,INVOKESPECIAL));
368 a(InstructionConstants.RETURN);
369 execute.setMaxLocals();
370 execute.setMaxStack();
371 cl.addMethod(execute.getMethod());
374 MethodGen main = newMethod(ACC_STATIC|ACC_PUBLIC,Type.VOID,new Type[]{new ArrayType(Type.STRING,1)},"main");
376 a(fac.createNew(fullClassName));
377 a(InstructionConstants.DUP);
378 a(fac.createInvoke(fullClassName,"<init>",Type.VOID,Type.NO_ARGS,INVOKESPECIAL));
379 a(new PUSH(cp,fullClassName));
380 a(InstructionConstants.ALOAD_0);
382 a(fac.createInvoke("org.ibex.nestedvm.UnixRuntime","runAndExec",Type.INT,
383 new Type[]{Type.getType("Lorg/ibex/nestedvm/UnixRuntime;"),Type.STRING,new ArrayType(Type.STRING,1)},
386 a(fac.createInvoke(fullClassName,"run",Type.INT,new Type[]{Type.STRING,new ArrayType(Type.STRING,1)},INVOKEVIRTUAL));
387 a(fac.createInvoke("java.lang.System","exit",Type.VOID,new Type[]{Type.INT},INVOKESTATIC));
388 a(InstructionConstants.RETURN);
391 cl.addMethod(main.getMethod());
394 System.out.println("Constant Pool Size: " + cp.getSize());
395 cl.getJavaClass().dump(os);
398 private void addConstReturnMethod(String name, int val) {
399 MethodGen method = newMethod(ACC_PROTECTED,Type.INT, Type.NO_ARGS,name);
400 selectMethod(method);
402 a(InstructionConstants.IRETURN);
403 method.setMaxLocals();
404 method.setMaxStack();
405 cl.addMethod(method.getMethod());
408 private static int initDataCount;
409 private void emitData(int addr, DataInputStream dis, int size, boolean readOnly) throws Exn,IOException {
410 if((addr&3)!=0 || (size&3)!=0) throw new Exn("Data section on weird boundaries");
411 int last = addr + size;
413 int segSize = Math.min(size,28000); // must be a multiple of 56
414 StringBuffer sb = new StringBuffer();
415 for(int i=0;i<segSize;i+=7) {
417 for(int j=0;j<7;j++) {
419 byte b = (i+j < size) ? dis.readByte() : 1;
423 sb.append((char) ((l>>>(7*(7-j)))&0x7f));
425 String fieldname = "_data" + (++initDataCount);
426 cl.addField(new FieldGen(ACC_PRIVATE|ACC_STATIC|ACC_FINAL,new ArrayType(Type.INT,1),fieldname,cp).getField());
428 selectList(clinitExtras);
429 a(new PUSH(cp,sb.toString()));
430 a(new PUSH(cp,segSize/4));
431 a(fac.createInvoke("org.ibex.nestedvm.Runtime","decodeData",new ArrayType(Type.INT,1),new Type[]{Type.STRING,Type.INT},INVOKESTATIC));
432 a(fac.createPutStatic(fullClassName,fieldname,new ArrayType(Type.INT,1)));
434 selectList(initExtras);
435 a(InstructionConstants.ALOAD_0);
436 a(fac.createGetStatic(fullClassName,fieldname,new ArrayType(Type.INT,1)));
437 a(new PUSH(cp,addr));
438 a(new PUSH(cp,readOnly));
439 a(fac.createInvoke(fullClassName,"initPages",Type.VOID,new Type[]{new ArrayType(Type.INT,1),Type.INT,Type.BOOLEAN},INVOKEVIRTUAL));
447 private void emitBSS(int addr, int size) throws Exn {
448 if((addr&3)!=0) throw new Exn("BSS section on weird boundaries");
451 selectList(initExtras);
452 a(InstructionConstants.ALOAD_0);
453 a(new PUSH(cp,addr));
454 a(new PUSH(cp,count));
455 a(fac.createInvoke(fullClassName,"clearPages",Type.VOID,new Type[]{Type.INT,Type.INT},INVOKEVIRTUAL));
459 private boolean textDone;
460 private int startOfMethod = 0;
461 private int endOfMethod = 0;
462 private boolean unreachable = false;
463 private InstructionHandle[] jumpHandles;
464 private InstructionHandle defaultHandle;
465 private InstructionHandle returnHandle;
466 private InstructionHandle realStart;
467 private MethodGen curMethod;
469 private boolean jumpable(int addr) { return jumpableAddresses.contains(new Integer(addr)); }
471 private void emitText(int addr, DataInputStream dis, int size) throws Exn,IOException {
472 if(textDone) throw new Exn("Multiple text segments");
475 if((addr&3)!=0 || (size&3)!=0) throw new Exn("Section on weird boundaries");
477 int insn,nextInsn=-1;
478 boolean skipNext = true;
480 for(int i=0;i<count;i++,addr+=4) {
481 insn = skipNext ? dis.readInt() : nextInsn;
482 nextInsn = (i == count-1) ? -1 : dis.readInt();
483 if(addr >= endOfMethod) { endMethod(addr); startMethod(addr); }
484 if(jumpHandles[(addr-startOfMethod)/4] != null) {
485 // Move the fake jump target to the current location
486 insnList.move(jumpHandles[(addr-startOfMethod)/4],insnList.getEnd());
488 } else if(unreachable) {
492 skipNext = emitInstruction(addr,insn,nextInsn);
493 } catch(RuntimeException e) {
494 warn.println("Exception at " + toHex(addr));
497 if(skipNext) { addr+=4; i++; }
503 private void startMethod(int first) {
504 startOfMethod = first & methodMask;
505 endOfMethod = startOfMethod + maxBytesPerMethod;
506 curMethod = newMethod(ACC_PRIVATE,Type.VOID,Type.NO_ARGS,"run_" + toHex(startOfMethod));
507 selectMethod(curMethod);
509 int[] buf = new int[maxBytesPerMethod/4];
510 jumpHandles = new InstructionHandle[maxBytesPerMethod/4];
512 for(int addr=first;addr<endOfMethod;addr+=4) {
515 // append NOPs for GOTO jumps (these will be moved to the correct location later)
516 jumpHandles[(addr-startOfMethod)/4] = a(InstructionConstants.NOP);
520 // append NOP for default case (throw exn) (this will be moved later)
521 defaultHandle = a(InstructionConstants.NOP);
522 returnHandle = a(InstructionConstants.NOP);
524 int[] matches = new int[n];
525 System.arraycopy(buf,0,matches,0,n);
526 InstructionHandle[] targets = new InstructionHandle[n];
527 for(int i=0;i<matches.length;i++)
528 targets[i] = jumpHandles[(matches[i]-startOfMethod)/4];
531 // First instruction of the actual method - everything above this should be removed
532 // before we get to the end
533 realStart = a(InstructionConstants.NOP);
536 a(InstructionConstants.ALOAD_0);
537 a(fac.createFieldAccess(fullClassName,"page",new ArrayType(Type.INT,1), GETFIELD));
538 a(InstructionConstants.ASTORE_2);
540 a(InstructionConstants.ALOAD_0);
541 a(fac.createFieldAccess(fullClassName,"readPages",new ArrayType(Type.INT,2), GETFIELD));
542 a(InstructionConstants.ASTORE_2);
543 a(InstructionConstants.ALOAD_0);
544 a(fac.createFieldAccess(fullClassName,"writePages",new ArrayType(Type.INT,2), GETFIELD));
545 a(InstructionFactory.createStore(Type.OBJECT,3));
548 LOOKUPSWITCH initialSwitch = new LOOKUPSWITCH(matches,targets,defaultHandle);
549 a(InstructionConstants.ALOAD_0);
550 a(fac.createFieldAccess(fullClassName,"pc",Type.INT, GETFIELD));
554 private void endMethod(int firstAddrOfNext) {
555 if(startOfMethod == 0) return;
559 pushConst(firstAddrOfNext);
561 // mark the start of the next method as jumpable
562 jumpableAddresses.add(new Integer(firstAddrOfNext));
565 insnList.move(returnHandle,insnList.getEnd());
567 a(InstructionConstants.RETURN);
569 // move the default jump target (lookupswitch) to before the throw
570 insnList.move(defaultHandle,insnList.getEnd());
572 a(fac.createNew("org.ibex.nestedvm.Runtime$ExecutionException"));
573 a(InstructionConstants.DUP);
574 a(fac.createNew("java.lang.StringBuffer"));
575 a(InstructionConstants.DUP);
576 a(new PUSH(cp,"Jumped to invalid address: "));
577 a(fac.createInvoke("java.lang.StringBuffer","<init>",Type.VOID,new Type[]{Type.STRING},INVOKESPECIAL));
578 a(InstructionConstants.ALOAD_0);
579 a(fac.createFieldAccess(fullClassName,"pc",Type.INT, GETFIELD));
580 a(fac.createInvoke("java.lang.StringBuffer","append",Type.STRINGBUFFER,new Type[]{Type.INT},INVOKEVIRTUAL));
581 a(fac.createInvoke("java.lang.StringBuffer","toString",Type.STRING,Type.NO_ARGS,INVOKEVIRTUAL));
582 a(fac.createInvoke("org.ibex.nestedvm.Runtime$ExecutionException","<init>",Type.VOID,new Type[]{Type.STRING},INVOKESPECIAL));
583 a(InstructionConstants.ATHROW);
585 a(fac.createNew("org.ibex.nestedvm.Runtime$ExecutionException"));
586 a(InstructionConstants.DUP);
587 a(new PUSH(cp,"Jumped to invalid address"));
588 a(fac.createInvoke("org.ibex.nestedvm.Runtime$ExecutionException","<init>",Type.VOID,new Type[]{Type.STRING},INVOKESPECIAL));
589 a(InstructionConstants.ATHROW);
592 if(insnList.getStart() != realStart) {
593 System.err.println(insnList);
594 throw new Error("A jumpHandle wasn't moved into place");
597 curMethod.removeNOPs();
598 curMethod.setMaxLocals();
599 curMethod.setMaxStack();
601 cl.addMethod(curMethod.getMethod());
603 endOfMethod = startOfMethod = 0;
607 private void leaveMethod() {
608 a(InstructionFactory.createBranchInstruction(GOTO,returnHandle));
611 private void branch(int pc, int target) {
612 if((pc&methodMask) == (target&methodMask)) {
613 a(InstructionFactory.createBranchInstruction(GOTO,jumpHandles[(target-startOfMethod)/4]));
622 // This assumes everything needed by ifInsn is already on the stack
623 private boolean doIfInstruction(short op, int pc, int target, int nextInsn) throws Exn {
624 emitInstruction(-1,nextInsn,-1); // delay slot
626 IfInstruction ifInsn = (IfInstruction) InstructionFactory.createBranchInstruction(op,null);
627 if((target&methodMask) == (pc&methodMask)) {
629 h.setTarget(jumpHandles[(target-startOfMethod)/4]);
631 h = a(ifInsn.negate());
633 h.setTarget(a(InstructionConstants.NOP));
635 if(!jumpable(pc+4)) return true; // done - skip it
637 //System.err.println("Delay slot is jumpable - This code is untested + " + toHex(nextInsn));
638 if(pc+4==endOfMethod) {
639 // the delay slot is at the start of the next method
640 jumpableAddresses.add(new Integer(pc+8)); // make the 2nd insn of the next method jumpable
641 branch(pc,pc+8); // jump over it
642 //System.err.println("delay slot: " + toHex(pc+8));
644 return false; // we still need to output it
646 //System.err.println("jumped over delay slot: " + toHex(pc+4));
647 // add another copy and jump over
648 h = a(InstructionFactory.createBranchInstruction(GOTO,null));
649 insnList.move(jumpHandles[(pc+4-startOfMethod)/4],insnList.getEnd());
650 emitInstruction(-1,nextInsn,-1); // delay slot
651 h.setTarget(a(InstructionConstants.NOP));
656 private boolean emitInstruction(int pc, int insn, int nextInsn) throws Exn {
657 if(insn == -1) throw new Exn("insn is -1");
659 int op = (insn >>> 26) & 0xff; // bits 26-31
660 int rs = (insn >>> 21) & 0x1f; // bits 21-25
661 int rt = (insn >>> 16) & 0x1f; // bits 16-20
662 int ft = (insn >>> 16) & 0x1f;
663 int rd = (insn >>> 11) & 0x1f; // bits 11-15
664 int fs = (insn >>> 11) & 0x1f;
665 int shamt = (insn >>> 6) & 0x1f; // bits 6-10
666 int fd = (insn >>> 6) & 0x1f;
667 int subcode = insn & 0x3f; // bits 0-5
668 int breakCode = (insn >>> 6) & 0xfffff; // bits 6-20
670 int jumpTarget = (insn & 0x03ffffff); // bits 0-25
671 int unsignedImmediate = insn & 0xffff;
672 int signedImmediate = (insn << 16) >> 16;
673 int branchTarget = signedImmediate;
686 a(InstructionConstants.ISHL);
693 a(InstructionConstants.IUSHR);
700 a(InstructionConstants.ISHR);
707 a(InstructionConstants.ISHL);
714 a(InstructionConstants.IUSHR);
721 a(InstructionConstants.ISHR);
725 if(pc == -1) throw new Exn("pc modifying insn in delay slot");
726 emitInstruction(-1,nextInsn,-1);
734 if(pc == -1) throw new Exn("pc modifying insn in delay slot");
735 emitInstruction(-1,nextInsn,-1);
751 restoreChangedRegs();
754 a(InstructionConstants.ALOAD_0);
760 a(fac.createInvoke(fullClassName,"syscall",Type.INT,new Type[]{Type.INT,Type.INT,Type.INT,Type.INT,Type.INT},INVOKEVIRTUAL));
763 a(InstructionConstants.ALOAD_0);
764 a(fac.createFieldAccess(fullClassName,"state",Type.INT, GETFIELD));
765 pushConst(Runtime.RUNNING);
766 b1 = a(InstructionFactory.createBranchInstruction(IF_ICMPEQ,null));
771 b1.setTarget(a(InstructionConstants.NOP));
775 a(fac.createNew("org.ibex.nestedvm.Runtime$ExecutionException"));
776 a(InstructionConstants.DUP);
777 a(new PUSH(cp,"BREAK Code " + toHex(breakCode)));
778 a(fac.createInvoke("org.ibex.nestedvm.Runtime$ExecutionException","<init>",Type.VOID,new Type[]{Type.STRING},INVOKESPECIAL));
779 a(InstructionConstants.ATHROW);
804 a(InstructionConstants.I2L);
806 a(InstructionConstants.I2L);
807 a(InstructionConstants.LMUL);
808 a(InstructionConstants.DUP2);
810 a(InstructionConstants.L2I);
812 a(InstructionConstants.SWAP);
816 a(InstructionConstants.LUSHR);
817 a(InstructionConstants.L2I);
819 a(InstructionConstants.SWAP);
825 a(InstructionConstants.I2L);
826 pushConst(0xffffffffL);
827 a(InstructionConstants.LAND);
829 a(InstructionConstants.I2L);
830 pushConst(0xffffffffL);
831 a(InstructionConstants.LAND);
832 a(InstructionConstants.LMUL);
833 a(InstructionConstants.DUP2);
835 a(InstructionConstants.L2I);
837 a(InstructionConstants.SWAP);
841 a(InstructionConstants.LUSHR);
842 a(InstructionConstants.L2I);
844 a(InstructionConstants.SWAP);
851 a(InstructionConstants.DUP2);
853 a(InstructionConstants.IDIV);
855 a(InstructionConstants.SWAP);
858 a(InstructionConstants.IREM);
860 a(InstructionConstants.SWAP);
866 a(InstructionConstants.DUP);
868 b1 = a(InstructionFactory.createBranchInstruction(IFEQ,null));
871 a(InstructionConstants.I2L);
872 pushConst(0xffffffffL);
873 a(InstructionConstants.LAND);
874 a(InstructionConstants.DUP2);
876 a(InstructionConstants.I2L);
877 pushConst(0xffffffffL);
879 a(InstructionConstants.LAND);
880 a(InstructionConstants.DUP2_X2);
881 a(InstructionConstants.LDIV);
883 a(InstructionConstants.L2I);
885 a(InstructionConstants.SWAP);
888 a(InstructionConstants.LREM);
889 a(InstructionConstants.L2I);
891 a(InstructionConstants.SWAP);
894 b1.setTarget(a(InstructionConstants.NOP));
899 throw new Exn("ADD (add with oveflow trap) not suported");
902 if(rt != 0 && rs != 0) {
905 a(InstructionConstants.IADD);
914 throw new Exn("SUB (add with oveflow trap) not suported");
917 if(rt != 0 && rs != 0) {
920 a(InstructionConstants.ISUB);
923 a(InstructionConstants.INEG);
933 a(InstructionConstants.IAND);
940 a(InstructionConstants.IOR);
947 a(InstructionConstants.IXOR);
952 if(rs != 0 || rt != 0) {
953 if(rs != 0 && rt != 0) {
956 a(InstructionConstants.IOR);
962 a(InstructionConstants.ICONST_M1);
963 a(InstructionConstants.IXOR);
974 b1 = a(InstructionFactory.createBranchInstruction(IF_ICMPLT,null));
975 a(InstructionConstants.ICONST_0);
976 b2 = a(InstructionFactory.createBranchInstruction(GOTO,null));
977 b1.setTarget(a(InstructionConstants.ICONST_1));
978 b2.setTarget(a(InstructionConstants.NOP));
989 a(InstructionConstants.I2L);
990 pushConst(0xffffffffL);
991 a(InstructionConstants.LAND);
993 a(InstructionConstants.I2L);
994 pushConst(0xffffffffL);
995 a(InstructionConstants.LAND);
996 a(InstructionConstants.LCMP);
997 b1 = a(InstructionFactory.createBranchInstruction(IFLT,null));
1000 b1 = a(InstructionFactory.createBranchInstruction(IFNE,null));
1002 a(InstructionConstants.ICONST_0);
1003 b2 = a(InstructionFactory.createBranchInstruction(GOTO,null));
1004 b1.setTarget(a(InstructionConstants.ICONST_1));
1005 b2.setTarget(a(InstructionConstants.NOP));
1012 throw new RuntimeException("Illegal instruction 0/" + subcode);
1019 if(pc == -1) throw new Exn("pc modifying insn in delay slot");
1021 return doIfInstruction(IFLT,pc,pc+branchTarget*4+4,nextInsn);
1023 if(pc == -1) throw new Exn("pc modifying insn in delay slot");
1025 return doIfInstruction(IFGE,pc,pc+branchTarget*4+4,nextInsn);
1027 if(pc == -1) throw new Exn("pc modifying insn in delay slot");
1029 b1 = a(InstructionFactory.createBranchInstruction(IFGE,null));
1030 emitInstruction(-1,nextInsn,-1);
1034 branch(pc,pc+branchTarget*4+4);
1035 b1.setTarget(a(InstructionConstants.NOP));
1038 if(pc == -1) throw new Exn("pc modifying insn in delay slot");
1040 if(rs != 0) { // r0 is always >= 0
1042 b1 = a(InstructionFactory.createBranchInstruction(IFLT,null));
1044 emitInstruction(-1,nextInsn,-1);
1048 branch(pc,pc+branchTarget*4+4);
1049 if(b1 != null) b1.setTarget(a(InstructionConstants.NOP));
1050 if(b1 == null) unreachable = true;
1053 throw new RuntimeException("Illegal Instruction 1/" + rt);
1058 if(pc == -1) throw new Exn("pc modifying insn in delay slot");
1059 emitInstruction(-1,nextInsn,-1);
1060 branch(pc,(pc&0xf0000000)|(jumpTarget << 2));
1065 if(pc == -1) throw new Exn("pc modifying insn in delay slot");
1066 int target = (pc&0xf0000000)|(jumpTarget << 2);
1067 emitInstruction(-1,nextInsn,-1);
1068 if(optimizedMemcpy && (target == memcpy || target == memset)) {
1069 a(InstructionConstants.ALOAD_0);
1073 a(fac.createInvoke(fullClassName,target==memcpy ? "memcpy" : "memset", Type.VOID, new Type[]{Type.INT,Type.INT,Type.INT},INVOKEVIRTUAL));
1088 if(pc == -1) throw new Exn("pc modifying insn in delay slot");
1090 emitInstruction(-1,nextInsn,-1);
1091 branch(pc,pc+branchTarget*4+4);
1093 } else if(rs == 0 || rt == 0) {
1094 pushReg(rt == 0 ? R+rs : R+rt);
1095 return doIfInstruction(IFEQ,pc,pc+branchTarget*4+4,nextInsn);
1099 return doIfInstruction(IF_ICMPEQ,pc,pc+branchTarget*4+4,nextInsn);
1103 if(pc == -1) throw new Exn("pc modifying insn in delay slot");
1106 return doIfInstruction(IFNE,pc,pc+branchTarget*4+4,nextInsn);
1109 return doIfInstruction(IF_ICMPNE,pc,pc+branchTarget*4+4,nextInsn);
1112 if(pc == -1) throw new Exn("pc modifying insn in delay slot");
1114 return doIfInstruction(IFLE,pc,pc+branchTarget*4+4,nextInsn);
1116 if(pc == -1) throw new Exn("pc modifying insn in delay slot");
1118 return doIfInstruction(IFGT,pc,pc+branchTarget*4+4,nextInsn);
1120 throw new Exn("ADDI (add immediate with oveflow trap) not suported");
1123 addiu(rs,signedImmediate);
1129 pushConst(signedImmediate);
1130 b1 = a(InstructionFactory.createBranchInstruction(IF_ICMPLT,null));
1131 a(InstructionConstants.ICONST_0);
1132 b2 = a(InstructionFactory.createBranchInstruction(GOTO,null));
1133 b1.setTarget(a(InstructionConstants.ICONST_1));
1134 b2.setTarget(a(InstructionConstants.NOP));
1140 a(InstructionConstants.I2L);
1141 pushConst(0xffffffffL);
1142 a(InstructionConstants.LAND);
1143 pushConst((long)unsignedImmediate);
1144 a(InstructionConstants.LCMP);
1146 b1 = a(InstructionFactory.createBranchInstruction(IFLT,null));
1147 a(InstructionConstants.ICONST_0);
1148 b2 = a(InstructionFactory.createBranchInstruction(GOTO,null));
1149 b1.setTarget(a(InstructionConstants.ICONST_1));
1150 b2.setTarget(a(InstructionConstants.NOP));
1157 pushConst(unsignedImmediate);
1158 a(InstructionConstants.IAND);
1163 if(rs != 0 && unsignedImmediate != 0) {
1165 pushConst(unsignedImmediate);
1166 a(InstructionConstants.IOR);
1170 pushConst(unsignedImmediate);
1177 pushConst(unsignedImmediate);
1178 a(InstructionConstants.IXOR);
1183 pushConst(unsignedImmediate << 16);
1187 throw new Exn("TLB/Exception support not implemented");
1196 if(fs != 31) throw new Exn("FCR " + fs + " unavailable");
1210 if(fs != 31) throw new Exn("FCR " + fs + " unavailable");
1215 case 8: {// BC1F, BC1T
1217 pushConst(0x800000);
1218 a(InstructionConstants.IAND);
1219 return doIfInstruction(((insn>>>16)&1) == 0 ? IFEQ : IFNE,pc,pc+branchTarget*4+4,nextInsn);
1223 { // Single/Double math
1224 boolean d = rs == 17;
1227 preSetDouble(F+fd,d);
1230 a(d ? InstructionConstants.DADD : InstructionConstants.FADD);
1234 preSetDouble(F+fd,d);
1237 a(d ? InstructionConstants.DSUB : InstructionConstants.FSUB);
1241 preSetDouble(F+fd,d);
1244 a(d ? InstructionConstants.DMUL : InstructionConstants.FMUL);
1248 preSetDouble(F+fd,d);
1251 a(d ? InstructionConstants.DDIV : InstructionConstants.FDIV);
1255 preSetDouble(F+fd,d);
1256 // NOTE: We can't use fneg/dneg here since they'll turn +0.0 into -0.0
1259 a(d ? InstructionConstants.DUP2 : InstructionConstants.DUP);
1260 a(d ? InstructionConstants.DCONST_0 : InstructionConstants.FCONST_0);
1261 a(d ? InstructionConstants.DCMPG : InstructionConstants.FCMPG);
1263 b1 = a(InstructionFactory.createBranchInstruction(IFGT,null));
1264 a(d ? InstructionConstants.DCONST_0 : InstructionConstants.FCONST_0);
1266 a(InstructionConstants.DUP2_X2);
1267 a(InstructionConstants.POP2);
1269 a(InstructionConstants.POP);
1271 a(InstructionConstants.DSUB);
1273 b1.setTarget(setDouble(d));
1287 preSetDouble(F+fd,d);
1289 a(d ? InstructionConstants.DNEG : InstructionConstants.FNEG);
1295 if(d) a(InstructionConstants.D2F);
1301 if(!d) a(InstructionConstants.F2D);
1304 case 36: { // CVT.W.D
1305 int[] matches = new int[4];
1306 for(int i=0;i<4;i++) matches[i] = i;
1308 TABLESWITCH ts = new TABLESWITCH(matches,new InstructionHandle[4], null);
1313 a(InstructionConstants.ICONST_3);
1314 a(InstructionConstants.IAND);
1317 // Round towards plus infinity
1318 ts.setTarget(2,a(InstructionConstants.NOP));
1319 if(!d) a(InstructionConstants.F2D); // Ugh.. java.lang.Math doesn't have a float ceil/floor
1320 a(fac.createInvoke("java.lang.Math","ceil",Type.DOUBLE,new Type[]{Type.DOUBLE},INVOKESTATIC));
1321 if(!d) a(InstructionConstants.D2F);
1322 b1 = a(InstructionFactory.createBranchInstruction(GOTO,null));
1325 ts.setTarget(0,d ? pushConst(0.5d) : pushConst(0.5f));
1326 a(d ? InstructionConstants.DADD : InstructionConstants.FADD);
1329 // Round towards minus infinity
1330 ts.setTarget(3,a(InstructionConstants.NOP));
1331 if(!d) a(InstructionConstants.F2D);
1332 a(fac.createInvoke("java.lang.Math","floor",Type.DOUBLE,new Type[]{Type.DOUBLE},INVOKESTATIC));
1333 if(!d) a(InstructionConstants.D2F);
1335 InstructionHandle h = a(d ? InstructionConstants.D2I : InstructionConstants.F2I);
1349 pushConst(~0x800000);
1350 a(InstructionConstants.IAND);
1353 a(d ? InstructionConstants.DCMPG : InstructionConstants.FCMPG);
1355 case 50: b1 = a(InstructionFactory.createBranchInstruction(IFEQ,null)); break;
1356 case 60: b1 = a(InstructionFactory.createBranchInstruction(IFLT,null)); break;
1357 case 62: b1 = a(InstructionFactory.createBranchInstruction(IFLE,null)); break;
1361 pushConst(0x000000);
1362 b2 = a(InstructionFactory.createBranchInstruction(GOTO,null));
1363 b1.setTarget(pushConst(0x800000));
1364 b2.setTarget(a(InstructionConstants.IOR));
1367 default: throw new Exn("Invalid Instruction 17/" + rs + "/" + subcode);
1371 case 20: { // Integer
1376 a(InstructionConstants.I2F);
1382 a(InstructionConstants.I2D);
1385 default: throw new Exn("Invalid Instruction 17/" + rs + "/" + subcode);
1390 throw new Exn("Invalid Instruction 17/" + rs);
1395 throw new Exn("coprocessor 2 and 3 instructions not available");
1398 addiu(R+rs,signedImmediate);
1405 a(InstructionConstants.ICONST_M1);
1406 a(InstructionConstants.IXOR);
1407 a(InstructionConstants.ICONST_3);
1408 a(InstructionConstants.IAND);
1409 a(InstructionConstants.ICONST_3);
1410 a(InstructionConstants.ISHL);
1411 a(InstructionConstants.IUSHR);
1412 a(InstructionConstants.I2B);
1418 addiu(R+rs,signedImmediate);
1425 a(InstructionConstants.ICONST_M1);
1426 a(InstructionConstants.IXOR);
1427 a(InstructionConstants.ICONST_2);
1428 a(InstructionConstants.IAND);
1429 a(InstructionConstants.ICONST_3);
1430 a(InstructionConstants.ISHL);
1431 a(InstructionConstants.IUSHR);
1432 a(InstructionConstants.I2S);
1438 addiu(R+rs,signedImmediate);
1442 pushConst(0x00ffffff);
1445 a(InstructionConstants.ICONST_M1);
1446 a(InstructionConstants.IXOR);
1447 a(InstructionConstants.ICONST_3);
1448 a(InstructionConstants.IAND);
1449 a(InstructionConstants.ICONST_3);
1450 a(InstructionConstants.ISHL);
1451 a(InstructionConstants.IUSHR);
1452 a(InstructionConstants.IAND);
1459 a(InstructionConstants.ICONST_3);
1460 a(InstructionConstants.IAND);
1461 a(InstructionConstants.ICONST_3);
1462 a(InstructionConstants.ISHL);
1463 a(InstructionConstants.ISHL);
1464 a(InstructionConstants.IOR);
1473 memRead(R+rs,signedImmediate);
1478 addiu(R+rs,signedImmediate);
1485 a(InstructionConstants.ICONST_M1);
1486 a(InstructionConstants.IXOR);
1487 a(InstructionConstants.ICONST_3);
1488 a(InstructionConstants.IAND);
1489 a(InstructionConstants.ICONST_3);
1490 a(InstructionConstants.ISHL);
1491 a(InstructionConstants.IUSHR);
1493 a(InstructionConstants.IAND);
1499 addiu(R+rs,signedImmediate);
1506 a(InstructionConstants.ICONST_M1);
1507 a(InstructionConstants.IXOR);
1508 a(InstructionConstants.ICONST_2);
1509 a(InstructionConstants.IAND);
1510 a(InstructionConstants.ICONST_3);
1511 a(InstructionConstants.ISHL);
1512 a(InstructionConstants.IUSHR);
1514 // chars are unsigend so this works
1515 a(InstructionConstants.I2C);
1521 addiu(R+rs,signedImmediate);
1525 pushConst(0xffffff00);
1528 a(InstructionConstants.ICONST_3);
1529 a(InstructionConstants.IAND);
1530 a(InstructionConstants.ICONST_3);
1531 a(InstructionConstants.ISHL);
1532 a(InstructionConstants.ISHL);
1533 a(InstructionConstants.IAND);
1540 a(InstructionConstants.ICONST_M1);
1541 a(InstructionConstants.IXOR);
1542 a(InstructionConstants.ICONST_3);
1543 a(InstructionConstants.IAND);
1544 a(InstructionConstants.ICONST_3);
1545 a(InstructionConstants.ISHL);
1546 a(InstructionConstants.IUSHR);
1547 a(InstructionConstants.IOR);
1554 addiu(R+rs,signedImmediate);
1557 // FEATURE: DO the preMemRead(true) thing for the rest of the S* instructions
1562 pushConst(0xff000000);
1565 a(InstructionConstants.ICONST_3);
1566 a(InstructionConstants.IAND);
1567 a(InstructionConstants.ICONST_3);
1568 a(InstructionConstants.ISHL);
1569 a(InstructionConstants.IUSHR);
1570 a(InstructionConstants.ICONST_M1);
1571 a(InstructionConstants.IXOR);
1572 a(InstructionConstants.IAND);
1577 a(InstructionConstants.IAND);
1583 a(InstructionConstants.ICONST_M1);
1584 a(InstructionConstants.IXOR);
1585 a(InstructionConstants.ICONST_3);
1586 a(InstructionConstants.IAND);
1587 a(InstructionConstants.ICONST_3);
1588 a(InstructionConstants.ISHL);
1589 a(InstructionConstants.ISHL);
1590 a(InstructionConstants.IOR);
1599 addiu(R+rs,signedImmediate);
1601 a(InstructionConstants.DUP);
1613 a(InstructionConstants.ICONST_2);
1614 a(InstructionConstants.IAND);
1615 a(InstructionConstants.ICONST_3);
1616 a(InstructionConstants.ISHL);
1617 a(InstructionConstants.ISHL);
1618 a(InstructionConstants.IAND);
1623 a(InstructionConstants.IAND);
1629 a(InstructionConstants.ICONST_M1);
1630 a(InstructionConstants.IXOR);
1631 a(InstructionConstants.ICONST_2);
1632 a(InstructionConstants.IAND);
1633 a(InstructionConstants.ICONST_3);
1634 a(InstructionConstants.ISHL);
1635 a(InstructionConstants.ISHL);
1636 a(InstructionConstants.IOR);
1645 addiu(R+rs,signedImmediate);
1646 a(InstructionConstants.DUP);
1655 pushConst(0xffffff00);
1658 a(InstructionConstants.ICONST_M1);
1659 a(InstructionConstants.IXOR);
1660 a(InstructionConstants.ICONST_3);
1661 a(InstructionConstants.IAND);
1662 a(InstructionConstants.ICONST_3);
1663 a(InstructionConstants.ISHL);
1664 a(InstructionConstants.ISHL);
1665 a(InstructionConstants.IAND);
1670 a(InstructionConstants.ICONST_3);
1671 a(InstructionConstants.IAND);
1672 a(InstructionConstants.ICONST_3);
1673 a(InstructionConstants.ISHL);
1674 a(InstructionConstants.IUSHR);
1675 a(InstructionConstants.IOR);
1683 preMemWrite2(R+rs,signedImmediate);
1690 addiu(R+rs,signedImmediate);
1691 a(InstructionConstants.DUP);
1700 pushConst(0x00ffffff);
1703 a(InstructionConstants.ICONST_3);
1704 a(InstructionConstants.IAND);
1705 a(InstructionConstants.ICONST_3);
1706 a(InstructionConstants.ISHL);
1707 a(InstructionConstants.IUSHR);
1708 a(InstructionConstants.IAND);
1713 a(InstructionConstants.ICONST_M1);
1714 a(InstructionConstants.IXOR);
1715 a(InstructionConstants.ICONST_3);
1716 a(InstructionConstants.IAND);
1717 a(InstructionConstants.ICONST_3);
1718 a(InstructionConstants.ISHL);
1719 a(InstructionConstants.ISHL);
1720 a(InstructionConstants.IOR);
1725 // FEATURE: This need to be atomic if we ever support threads (see SWC0/SC)
1728 memRead(R+rs,signedImmediate);
1734 memRead(R+rs,signedImmediate);
1738 /* FEATURE: This needs to fail (set rt to 0) if the memory location was modified
1739 * between the LL and SC if we every support threads.
1744 preMemWrite2(R+rs,signedImmediate);
1753 preMemWrite2(R+rs,signedImmediate);
1758 throw new Exn("Invalid Instruction: " + op + " at " + toHex(pc));
1763 // Helper functions for emitText
1765 private static final int R = 0;
1766 private static final int F = 32;
1767 private static final int HI = 64;
1768 private static final int LO = 65;
1769 private static final int FCSR = 66;
1770 private static final int REG_COUNT=67;
1772 private int[] regLocalMapping = new int[REG_COUNT];
1773 private int[] regLocalReadCount = new int[REG_COUNT];
1774 private int[] regLocalWriteCount = new int[REG_COUNT];
1775 private int nextAvailLocal;
1777 private int getLocalForReg(int reg) {
1778 if(regLocalMapping[reg] != 0) return regLocalMapping[reg];
1779 if(nextAvailLocal == 0) nextAvailLocal = onePage ? 3 : 4;
1780 regLocalMapping[reg] = nextAvailLocal++;
1781 return regLocalMapping[reg];
1784 private void fixupRegs() {
1785 InstructionHandle prev = realStart;
1786 for(int i=0;i<REG_COUNT;i++) {
1787 if(regLocalMapping[i] == 0) continue;
1789 prev = insnList.append(prev,InstructionConstants.ALOAD_0);
1790 prev = insnList.append(prev,fac.createFieldAccess(fullClassName,regField(i),Type.INT, GETFIELD));
1791 prev = insnList.append(prev,InstructionFactory.createStore(Type.INT,regLocalMapping[i]));
1793 if(regLocalWriteCount[i] > 0) {
1794 a(InstructionConstants.ALOAD_0);
1795 a(InstructionFactory.createLoad(Type.INT,regLocalMapping[i]));
1796 a(fac.createFieldAccess(fullClassName,regField(i),Type.INT, PUTFIELD));
1799 regLocalMapping[i] = regLocalReadCount[i] = regLocalWriteCount[i] = 0;
1804 private void restoreChangedRegs() {
1805 for(int i=0;i<REG_COUNT;i++) {
1806 if(regLocalWriteCount[i] > 0) {
1807 a(InstructionConstants.ALOAD_0);
1808 a(InstructionFactory.createLoad(Type.INT,regLocalMapping[i]));
1809 a(fac.createFieldAccess(fullClassName,regField(i),Type.INT, PUTFIELD));
1814 private String regField(int reg) {
1817 case HI: field = "hi"; break;
1818 case LO: field = "lo"; break;
1819 case FCSR: field = "fcsr"; break;
1821 if(reg > R && reg < R+32) field="r"+(reg-R);
1822 else if(reg >= F && reg < F+32) field="f"+(reg-F);
1823 else throw new IllegalArgumentException(""+reg);
1828 private boolean doLocal(int reg) {
1830 return reg == R+2 || reg == R+3 || reg == R+4 || reg == R+29;
1833 private InstructionHandle pushRegWZ(int reg) {
1835 warn.println("Warning: Pushing r0!");
1836 new Exception().printStackTrace(warn);
1838 return pushRegZ(reg);
1841 private InstructionHandle pushRegZ(int reg) {
1842 if(reg == R+0) return pushConst(0);
1843 else return pushReg(reg);
1847 private InstructionHandle pushReg(int reg) {
1848 InstructionHandle h;
1850 regLocalReadCount[reg]++;
1851 h = a(InstructionFactory.createLoad(Type.INT,getLocalForReg(reg)));
1853 h = a(InstructionConstants.ALOAD_0);
1854 a(fac.createFieldAccess(fullClassName,regField(reg),Type.INT, GETFIELD));
1859 private int preSetRegStackPos;
1860 private int[] preSetRegStack = new int[8];
1862 // This can push ONE or ZERO words to the stack. If it pushed one it returns true
1863 private boolean preSetReg(int reg) {
1864 regField(reg); // just to check for validity
1865 preSetRegStack[preSetRegStackPos] = reg;
1866 preSetRegStackPos++;
1870 a(InstructionConstants.ALOAD_0);
1875 private InstructionHandle setReg() {
1876 if(preSetRegStackPos==0) throw new RuntimeException("didn't do preSetReg");
1877 preSetRegStackPos--;
1878 int reg = preSetRegStack[preSetRegStackPos];
1879 InstructionHandle h;
1881 h = a(InstructionFactory.createStore(Type.INT,getLocalForReg(reg)));
1882 regLocalWriteCount[reg]++;
1884 h = a(fac.createFieldAccess(fullClassName,regField(reg),Type.INT, PUTFIELD));
1889 private InstructionHandle preSetPC() { return a(InstructionConstants.ALOAD_0); }
1890 private InstructionHandle setPC() { return a(fac.createFieldAccess(fullClassName,"pc",Type.INT, PUTFIELD)); }
1892 //unused - private InstructionHandle pushFloat(int reg) throws CompilationException { return pushDouble(reg,false); }
1893 //unused - private InstructionHandle pushDouble(int reg) throws CompilationException { return pushDouble(reg,true); }
1894 private InstructionHandle pushDouble(int reg, boolean d) throws Exn {
1895 if(reg < F || reg >= F+32) throw new IllegalArgumentException(""+reg);
1896 InstructionHandle h;
1898 if(reg == F+31) throw new Exn("Tried to use a double in f31");
1900 a(InstructionConstants.I2L);
1902 a(InstructionConstants.LSHL);
1904 a(InstructionConstants.I2L);
1905 pushConst(0xffffffffL);
1906 a(InstructionConstants.LAND);
1907 a(InstructionConstants.LOR);
1908 //p("invokestatic java/lang/Double/longBitsToDouble(J)D");
1909 a(fac.createInvoke("java.lang.Double","longBitsToDouble",Type.DOUBLE,new Type[]{Type.LONG},INVOKESTATIC));
1912 a(fac.createInvoke("java.lang.Float","intBitsToFloat",Type.FLOAT,new Type[]{Type.INT},INVOKESTATIC));
1917 private void preSetFloat(int reg) { preSetDouble(reg,false); }
1918 private void preSetDouble(int reg) { preSetDouble(reg,true); }
1919 private void preSetDouble(int reg, boolean d) { preSetReg(reg); }
1921 private InstructionHandle setFloat() throws Exn { return setDouble(false); }
1922 private InstructionHandle setDouble() throws Exn { return setDouble(true); }
1923 private InstructionHandle setDouble(boolean d) throws Exn {
1924 int reg = preSetRegStack[preSetRegStackPos-1];
1925 if(reg < F || reg >= F+32) throw new IllegalArgumentException(""+reg);
1926 //p("invokestatic java/lang/Double/doubleToLongBits(D)J");
1927 InstructionHandle h;
1929 if(reg == F+31) throw new Exn("Tried to use a double in f31");
1930 h = a(fac.createInvoke("java.lang.Double","doubleToLongBits",Type.LONG,new Type[]{Type.DOUBLE},INVOKESTATIC));
1931 a(InstructionConstants.DUP2);
1933 a(InstructionConstants.LUSHR);
1934 a(InstructionConstants.L2I);
1935 if(preSetReg(reg+1))
1936 a(InstructionConstants.SWAP);
1938 a(InstructionConstants.L2I);
1939 setReg(); // preSetReg was already done for this by preSetDouble
1941 h = a(fac.createInvoke("java.lang.Float","floatToRawIntBits",Type.INT,new Type[]{Type.FLOAT},INVOKESTATIC));
1947 private InstructionHandle pushConst(int n) {
1948 if(n >= 0 && n <= 5) {
1950 case 0: return a(InstructionConstants.ICONST_0);
1951 case 1: return a(InstructionConstants.ICONST_1);
1952 case 2: return a(InstructionConstants.ICONST_2);
1953 case 3: return a(InstructionConstants.ICONST_3);
1954 case 4: return a(InstructionConstants.ICONST_4);
1955 case 5: return a(InstructionConstants.ICONST_5);
1956 default: return null;
1958 } else if(n == -1) {
1959 return a(InstructionConstants.ICONST_M1);
1960 } else if(n >= -128 && n <= 127) {
1961 return a(new BIPUSH((byte) n));
1962 } else if(n >= -32768 && n <= 32767) {
1963 return a(new SIPUSH((short) n));
1965 return a(new PUSH(cp,n));
1969 private InstructionHandle pushConst(long l) { return a(new PUSH(cp,l)); }
1970 private InstructionHandle pushConst(float f) { return a(new PUSH(cp,f)); }
1971 private InstructionHandle pushConst(double d) { return a(new PUSH(cp,d)); }
1973 private void pushTmp() { a(InstructionConstants.ILOAD_1); }
1974 private void setTmp() { a(InstructionConstants.ISTORE_1); }
1976 private void addiu(int reg, int offset) {
1977 if(reg != R+0 && offset != 0) {
1980 a(InstructionConstants.IADD);
1981 } else if(reg != R+0) {
1987 private int memWriteStage;
1988 private void preMemWrite1() {
1989 if(memWriteStage!=0) throw new Error("pending preMemWrite1/2");
1992 a(InstructionConstants.ALOAD_2);
1994 a(InstructionFactory.createLoad(Type.OBJECT,3));
1996 a(InstructionConstants.ALOAD_0);
1999 private void preMemWrite2(int reg, int offset) {
2004 private void preMemWrite2() { preMemWrite2(false); }
2005 private void preMemWrite2(boolean addrInTmp) {
2006 if(memWriteStage!=1) throw new Error("pending preMemWrite2 or no preMemWrite1");
2009 if(nullPointerCheck) {
2010 a(InstructionConstants.DUP);
2011 a(InstructionConstants.ALOAD_0);
2012 a(InstructionConstants.SWAP);
2013 a(fac.createInvoke(fullClassName,"nullPointerCheck",Type.VOID,new Type[]{Type.INT},INVOKEVIRTUAL));
2017 a(InstructionConstants.ICONST_2);
2018 a(InstructionConstants.IUSHR);
2019 } else if(fastMem) {
2021 a(InstructionConstants.DUP_X1);
2022 pushConst(pageShift);
2023 a(InstructionConstants.IUSHR);
2024 a(InstructionConstants.AALOAD);
2028 a(InstructionConstants.SWAP);
2029 a(InstructionConstants.ICONST_2);
2030 a(InstructionConstants.IUSHR);
2031 pushConst((pageSize>>2)-1);
2032 a(InstructionConstants.IAND);
2036 // pops an address and value off the stack, sets *addr to value
2037 private void memWrite() {
2038 if(memWriteStage!=2) throw new Error("didn't do preMemWrite1 or preMemWrite2");
2042 a(InstructionConstants.IASTORE);
2043 } else if(fastMem) {
2044 a(InstructionConstants.IASTORE);
2046 a(fac.createInvoke(fullClassName,"unsafeMemWrite",Type.VOID,new Type[]{Type.INT,Type.INT},INVOKEVIRTUAL));
2051 // reads the word at r[reg]+offset
2052 private void memRead(int reg, int offset) {
2058 private boolean didPreMemRead;
2059 private boolean preMemReadDoPreWrite;
2061 private void preMemRead() { preMemRead(false); }
2062 private void preMemRead(boolean preWrite) {
2063 if(didPreMemRead) throw new Error("pending preMemRead");
2064 didPreMemRead = true;
2065 preMemReadDoPreWrite = preWrite;
2067 a(InstructionConstants.ALOAD_2);
2069 a(InstructionFactory.createLoad(Type.OBJECT,preWrite ? 3 : 2));
2071 a(InstructionConstants.ALOAD_0);
2073 // memRead pops an address off the stack, reads the value at that addr, and pushed the value
2074 // preMemRead MUST be called BEFORE the addresses is pushed
2075 private void memRead() { memRead(false); }
2077 private void memRead(boolean addrInTmp) {
2078 if(!didPreMemRead) throw new Error("didn't do preMemRead");
2079 didPreMemRead = false;
2080 if(preMemReadDoPreWrite)
2083 if(nullPointerCheck) {
2084 a(InstructionConstants.DUP);
2085 a(InstructionConstants.ALOAD_0);
2086 a(InstructionConstants.SWAP);
2087 a(fac.createInvoke(fullClassName,"nullPointerCheck",Type.VOID,new Type[]{Type.INT},INVOKEVIRTUAL));
2091 // p(target + "= page[(" + addr + ")>>>2];");
2092 a(InstructionConstants.ICONST_2);
2093 a(InstructionConstants.IUSHR);
2094 if(preMemReadDoPreWrite)
2095 a(InstructionConstants.DUP2);
2096 a(InstructionConstants.IALOAD);
2097 } else if(fastMem) {
2098 //p(target + " = readPages[("+addr+")>>>"+pageShift+"][(("+addr+")>>>2)&"+toHex((pageSize>>2)-1)+"];");
2101 a(InstructionConstants.DUP_X1);
2102 pushConst(pageShift);
2103 a(InstructionConstants.IUSHR);
2104 a(InstructionConstants.AALOAD);
2108 a(InstructionConstants.SWAP);
2109 a(InstructionConstants.ICONST_2);
2110 a(InstructionConstants.IUSHR);
2111 pushConst((pageSize>>2)-1);
2112 a(InstructionConstants.IAND);
2113 if(preMemReadDoPreWrite)
2114 a(InstructionConstants.DUP2);
2115 a(InstructionConstants.IALOAD);
2118 if(preMemReadDoPreWrite)
2119 a(InstructionConstants.DUP2);
2120 a(fac.createInvoke(fullClassName,"unsafeMemWrite",Type.INT,new Type[]{Type.INT},INVOKEVIRTUAL));
2125 // This might come in handy for something else
2126 /*private boolean touchesReg(int insn, int reg) {
2127 if((reg < R+0 || reg >= R+32) && reg != FCSR) throw new IllegalArgumentException(""+reg);
2128 if(reg == R+0) return false; // r0 is never modified
2129 int op = (insn >>> 26) & 0xff; // bits 26-31
2130 int subcode = insn & 0x3f; // bits 0-5
2131 int rd = (insn >>> 11) & 0x1f; // bits 11-15
2132 int rt = (insn >>> 16) & 0x1f; // bits 16-20
2133 int rs = (insn >>> 21) & 0x1f; // bits 21-25
2137 if(subcode >= 0 && subcode <= 7) return reg == R+rd; // Shift ops
2138 if(subcode >= 32 && subcode <= 43) return reg == R+rd; // Other math ops
2139 if(subcode >= 24 && subcode <= 27) return reg == HI || reg == LO; // MULT/DIV
2141 case 13: return false; // BREAK
2144 case 0: return reg == R+rt; // MFC.1
2145 case 2: return reg == R+rt; // CFC.1
2146 case 4: return false; // MTC.1
2147 case 6: return false; // CTC.1
2150 if(subcode == 50 || subcode == 60 || subcode == 62) return reg == FCSR;
2151 return false; // everything else just touches f0-f31
2152 case 20: return false; // Integer - just touches f0-f31
2156 if(op >= 8 && op <= 15) return reg == R+rt; // XXXI instructions
2157 if(op >= 40 && op <= 46) return false; // Memory WRITE ops
2158 if(op == 49) return reg == F+rt; // LWC1
2159 if(op == 57) return false; // SWC1
2162 warn.println("Unknown instruction in touchesReg()- assuming it modifies all regs " + op + " " + subcode);
2163 new Exception().fillInStackTrace().printStackTrace(warn);