private static byte[] makeExapndedCode(byte[] code, ArrayList jumps, int where, int gapLength) throws BadBytecode { int n = jumps.size(); int size = code.length + gapLength; for (int i = 0; i < n; i++) { Branch b = (Branch) jumps.get(i); size += b.deltaSize(); } byte[] newcode = new byte[size]; int src = 0, dest = 0, bindex = 0; int len = code.length; Branch b; int bpos; if (0 < n) { b = (Branch) jumps.get(0); bpos = b.orgPos; } else { b = null; bpos = len; // src will be never equal to bpos } while (src < len) { if (src == where) { int pos2 = dest + gapLength; while (dest < pos2) newcode[dest++] = NOP; } if (src != bpos) newcode[dest++] = code[src++]; else { int s = b.write(src, code, dest, newcode); src += s; dest += s + b.deltaSize(); if (++bindex < n) { b = (Branch) jumps.get(bindex); bpos = b.orgPos; } else { b = null; bpos = len; } } } return newcode; }
private static byte[] insertGap2w( byte[] code, int where, int gapLength, boolean exclusive, ArrayList jumps, Pointers ptrs) throws BadBytecode { int n = jumps.size(); if (gapLength > 0) { ptrs.shiftPc(where, gapLength, exclusive); for (int i = 0; i < n; i++) ((Branch) jumps.get(i)).shift(where, gapLength, exclusive); } boolean unstable = true; do { while (unstable) { unstable = false; for (int i = 0; i < n; i++) { Branch b = (Branch) jumps.get(i); if (b.expanded()) { unstable = true; int p = b.pos; int delta = b.deltaSize(); ptrs.shiftPc(p, delta, false); for (int j = 0; j < n; j++) ((Branch) jumps.get(j)).shift(p, delta, false); } } } for (int i = 0; i < n; i++) { Branch b = (Branch) jumps.get(i); int diff = b.gapChanged(); if (diff > 0) { unstable = true; int p = b.pos; ptrs.shiftPc(p, diff, false); for (int j = 0; j < n; j++) ((Branch) jumps.get(j)).shift(p, diff, false); } } } while (unstable); return makeExapndedCode(code, jumps, where, gapLength); }