Method from com.sun.tools.javac.jvm.Code Detail: |
public void addCatch(char startPc,
char endPc,
char handlerPc,
char catchType) {
catchInfo.append(new char[]{startPc, endPc, handlerPc, catchType});
}
Add a catch clause to code. |
public void addLineNumber(char startPc,
char lineNumber) {
if (lineDebugInfo) {
if (lineInfo.nonEmpty() && lineInfo.head[0] == startPc)
lineInfo = lineInfo.tail;
if (lineInfo.isEmpty() || lineInfo.head[1] != lineNumber)
lineInfo = lineInfo.prepend(new char[]{startPc, lineNumber});
}
}
|
public void align(int incr) {
if (alive)
while (cp % incr != 0) emitop0(nop);
}
Align code pointer to next `incr' boundary. |
public static int arraycode(Type type) {
switch (type.tag) {
case BYTE: return 8;
case BOOLEAN: return 4;
case SHORT: return 9;
case CHAR: return 5;
case INT: return 10;
case LONG: return 11;
case FLOAT: return 6;
case DOUBLE: return 7;
case CLASS: return 0;
case ARRAY: return 1;
default: throw new AssertionError("arraycode " + type);
}
}
Given a type, return its code for allocating arrays of that type. |
public Chain branch(int opcode) {
Chain result = null;
if (opcode == goto_) {
result = pendingJumps;
pendingJumps = null;
}
if (opcode != dontgoto && isAlive()) {
result = new Chain(emitJump(opcode),
result,
state.dup());
fixedPc = fatcode;
if (opcode == goto_) alive = false;
}
return result;
}
Emit a branch with given opcode; return its chain.
branch differs from jump in that jsr is treated as no-op. |
public boolean checkLimits(DiagnosticPosition pos,
Log log) {
if (cp > ClassFile.MAX_CODE) {
log.error(pos, "limit.code");
return true;
}
if (max_locals > ClassFile.MAX_LOCALS) {
log.error(pos, "limit.locals");
return true;
}
if (max_stack > ClassFile.MAX_STACK) {
log.error(pos, "limit.stack");
return true;
}
return false;
}
Check the code against VM spec limits; if
problems report them and return true. |
public int curPc() {
if (pendingJumps != null) resolvePending();
if (pendingStatPos != Position.NOPOS) markStatBegin();
fixedPc = true;
return cp;
}
The current output code pointer. |
public void emit4(int od) {
if (!alive) return;
if (cp + 4 > code.length) {
emit1(od > > 24);
emit1(od > > 16);
emit1(od > > 8);
emit1(od);
} else {
code[cp++] = (byte)(od > > 24);
code[cp++] = (byte)(od > > 16);
code[cp++] = (byte)(od > > 8);
code[cp++] = (byte)od;
}
}
|
public void emitAnewarray(int od,
Type arrayType) {
emitop(anewarray);
if (!alive) return;
emit2(od);
state.pop(1);
state.push(arrayType);
}
|
void emitCLDCStackMap(int pc,
int localsSize) {
if (lastStackMapPC == pc) {
// drop existing stackmap at this offset
stackMapBuffer[--stackMapBufferSize] = null;
}
lastStackMapPC = pc;
if (stackMapBuffer == null) {
stackMapBuffer = new StackMapFrame[20];
} else if (stackMapBuffer.length == stackMapBufferSize) {
StackMapFrame[] newStackMapBuffer =
new StackMapFrame[stackMapBufferSize < < 1];
System.arraycopy(stackMapBuffer, 0, newStackMapBuffer,
0, stackMapBufferSize);
stackMapBuffer = newStackMapBuffer;
}
StackMapFrame frame =
stackMapBuffer[stackMapBufferSize++] = new StackMapFrame();
frame.pc = pc;
frame.locals = new Type[localsSize];
for (int i=0; i< localsSize; i++) {
if (state.defined.isMember(i) && lvar[i] != null) {
Type vtype = lvar[i].sym.type;
if (!(vtype instanceof UninitializedType))
vtype = types.erasure(vtype);
frame.locals[i] = vtype;
}
}
frame.stack = new Type[state.stacksize];
for (int i=0; i< state.stacksize; i++)
frame.stack[i] = state.stack[i];
}
Emit a CLDC stack map frame. |
public void emitInvokedynamic(int desc,
Type mtype) {
// N.B. this format is under consideration by the JSR 292 EG
int argsize = width(mtype.getParameterTypes());
emitop(invokedynamic);
if (!alive) return;
emit2(desc);
emit2(0);
state.pop(argsize);
state.push(mtype.getReturnType());
}
Emit an invokedynamic instruction. |
public void emitInvokeinterface(int meth,
Type mtype) {
int argsize = width(mtype.getParameterTypes());
emitop(invokeinterface);
if (!alive) return;
emit2(meth);
emit1(argsize + 1);
emit1(0);
state.pop(argsize + 1);
state.push(mtype.getReturnType());
}
Emit an invokeinterface instruction. |
public void emitInvokespecial(int meth,
Type mtype) {
int argsize = width(mtype.getParameterTypes());
emitop(invokespecial);
if (!alive) return;
emit2(meth);
Symbol sym = (Symbol)pool.pool[meth];
state.pop(argsize);
if (sym.isConstructor())
state.markInitialized((UninitializedType)state.peek());
state.pop(1);
state.push(mtype.getReturnType());
}
Emit an invokespecial instruction. |
public void emitInvokestatic(int meth,
Type mtype) {
int argsize = width(mtype.getParameterTypes());
emitop(invokestatic);
if (!alive) return;
emit2(meth);
state.pop(argsize);
state.push(mtype.getReturnType());
}
Emit an invokestatic instruction. |
public void emitInvokevirtual(int meth,
Type mtype) {
int argsize = width(mtype.getParameterTypes());
emitop(invokevirtual);
if (!alive) return;
emit2(meth);
state.pop(argsize + 1);
state.push(mtype.getReturnType());
}
Emit an invokevirtual instruction. |
public int emitJump(int opcode) {
if (fatcode) {
if (opcode == goto_ || opcode == jsr) {
emitop4(opcode + goto_w - goto_, 0);
} else {
emitop2(negate(opcode), 8);
emitop4(goto_w, 0);
alive = true;
pendingStackMap = needStackMap;
}
return cp - 5;
} else {
emitop2(opcode, 0);
return cp - 3;
}
}
Emit a jump instruction.
Return code pointer of instruction to be patched. |
public void emitMultianewarray(int ndims,
int type,
Type arrayType) {
emitop(multianewarray);
if (!alive) return;
emit2(type);
emit1(ndims);
state.pop(ndims);
state.push(arrayType);
}
Emit a multinewarray instruction. |
public void emitNewarray(int elemcode,
Type arrayType) {
emitop(newarray);
if (!alive) return;
emit1(elemcode);
state.pop(1); // count
state.push(arrayType);
}
|
public void emitStackMap() {
int pc = curPc();
if (!needStackMap) return;
switch (stackMap) {
case CLDC:
emitCLDCStackMap(pc, getLocalsSize());
break;
case JSR202:
emitStackMapFrame(pc, getLocalsSize());
break;
default:
throw new AssertionError("Should have chosen a stackmap format");
}
// DEBUG code follows
if (debugCode) state.dump(pc);
}
|
void emitStackMapFrame(int pc,
int localsSize) {
if (lastFrame == null) {
// first frame
lastFrame = getInitialFrame();
} else if (lastFrame.pc == pc) {
// drop existing stackmap at this offset
stackMapTableBuffer[--stackMapBufferSize] = null;
lastFrame = frameBeforeLast;
frameBeforeLast = null;
}
StackMapFrame frame = new StackMapFrame();
frame.pc = pc;
int localCount = 0;
Type[] locals = new Type[localsSize];
for (int i=0; i< localsSize; i++, localCount++) {
if (state.defined.isMember(i) && lvar[i] != null) {
Type vtype = lvar[i].sym.type;
if (!(vtype instanceof UninitializedType))
vtype = types.erasure(vtype);
locals[i] = vtype;
if (width(vtype) > 1) i++;
}
}
frame.locals = new Type[localCount];
for (int i=0, j=0; i< localsSize; i++, j++) {
Assert.check(j < localCount);
frame.locals[j] = locals[i];
if (width(locals[i]) > 1) i++;
}
int stackCount = 0;
for (int i=0; i< state.stacksize; i++) {
if (state.stack[i] != null) {
stackCount++;
}
}
frame.stack = new Type[stackCount];
stackCount = 0;
for (int i=0; i< state.stacksize; i++) {
if (state.stack[i] != null) {
frame.stack[stackCount++] = types.erasure(state.stack[i]);
}
}
if (stackMapTableBuffer == null) {
stackMapTableBuffer = new StackMapTableFrame[20];
} else if (stackMapTableBuffer.length == stackMapBufferSize) {
StackMapTableFrame[] newStackMapTableBuffer =
new StackMapTableFrame[stackMapBufferSize < < 1];
System.arraycopy(stackMapTableBuffer, 0, newStackMapTableBuffer,
0, stackMapBufferSize);
stackMapTableBuffer = newStackMapTableBuffer;
}
stackMapTableBuffer[stackMapBufferSize++] =
StackMapTableFrame.getInstance(frame, lastFrame.pc, lastFrame.locals, types);
frameBeforeLast = lastFrame;
lastFrame = frame;
}
|
public void emitop0(int op) {
emitop(op);
if (!alive) return;
switch (op) {
case aaload: {
state.pop(1);// index
Type a = state.stack[state.stacksize-1];
state.pop(1);
//sometimes 'null type' is treated as a one-dimensional array type
//see Gen.visitLiteral - we should handle this case accordingly
Type stackType = a.tag == BOT ?
syms.objectType :
types.erasure(types.elemtype(a));
state.push(stackType); }
break;
case goto_:
markDead();
break;
case nop:
case ineg:
case lneg:
case fneg:
case dneg:
break;
case aconst_null:
state.push(syms.botType);
break;
case iconst_m1:
case iconst_0:
case iconst_1:
case iconst_2:
case iconst_3:
case iconst_4:
case iconst_5:
case iload_0:
case iload_1:
case iload_2:
case iload_3:
state.push(syms.intType);
break;
case lconst_0:
case lconst_1:
case lload_0:
case lload_1:
case lload_2:
case lload_3:
state.push(syms.longType);
break;
case fconst_0:
case fconst_1:
case fconst_2:
case fload_0:
case fload_1:
case fload_2:
case fload_3:
state.push(syms.floatType);
break;
case dconst_0:
case dconst_1:
case dload_0:
case dload_1:
case dload_2:
case dload_3:
state.push(syms.doubleType);
break;
case aload_0:
state.push(lvar[0].sym.type);
break;
case aload_1:
state.push(lvar[1].sym.type);
break;
case aload_2:
state.push(lvar[2].sym.type);
break;
case aload_3:
state.push(lvar[3].sym.type);
break;
case iaload:
case baload:
case caload:
case saload:
state.pop(2);
state.push(syms.intType);
break;
case laload:
state.pop(2);
state.push(syms.longType);
break;
case faload:
state.pop(2);
state.push(syms.floatType);
break;
case daload:
state.pop(2);
state.push(syms.doubleType);
break;
case istore_0:
case istore_1:
case istore_2:
case istore_3:
case fstore_0:
case fstore_1:
case fstore_2:
case fstore_3:
case astore_0:
case astore_1:
case astore_2:
case astore_3:
case pop:
case lshr:
case lshl:
case lushr:
state.pop(1);
break;
case areturn:
case ireturn:
case freturn:
Assert.check(state.nlocks == 0);
state.pop(1);
markDead();
break;
case athrow:
state.pop(1);
markDead();
break;
case lstore_0:
case lstore_1:
case lstore_2:
case lstore_3:
case dstore_0:
case dstore_1:
case dstore_2:
case dstore_3:
case pop2:
state.pop(2);
break;
case lreturn:
case dreturn:
Assert.check(state.nlocks == 0);
state.pop(2);
markDead();
break;
case dup:
state.push(state.stack[state.stacksize-1]);
break;
case return_:
Assert.check(state.nlocks == 0);
markDead();
break;
case arraylength:
state.pop(1);
state.push(syms.intType);
break;
case isub:
case iadd:
case imul:
case idiv:
case imod:
case ishl:
case ishr:
case iushr:
case iand:
case ior:
case ixor:
state.pop(1);
// state.pop(1);
// state.push(syms.intType);
break;
case aastore:
state.pop(3);
break;
case land:
case lor:
case lxor:
case lmod:
case ldiv:
case lmul:
case lsub:
case ladd:
state.pop(2);
break;
case lcmp:
state.pop(4);
state.push(syms.intType);
break;
case l2i:
state.pop(2);
state.push(syms.intType);
break;
case i2l:
state.pop(1);
state.push(syms.longType);
break;
case i2f:
state.pop(1);
state.push(syms.floatType);
break;
case i2d:
state.pop(1);
state.push(syms.doubleType);
break;
case l2f:
state.pop(2);
state.push(syms.floatType);
break;
case l2d:
state.pop(2);
state.push(syms.doubleType);
break;
case f2i:
state.pop(1);
state.push(syms.intType);
break;
case f2l:
state.pop(1);
state.push(syms.longType);
break;
case f2d:
state.pop(1);
state.push(syms.doubleType);
break;
case d2i:
state.pop(2);
state.push(syms.intType);
break;
case d2l:
state.pop(2);
state.push(syms.longType);
break;
case d2f:
state.pop(2);
state.push(syms.floatType);
break;
case tableswitch:
case lookupswitch:
state.pop(1);
// the caller is responsible for patching up the state
break;
case dup_x1: {
Type val1 = state.pop1();
Type val2 = state.pop1();
state.push(val1);
state.push(val2);
state.push(val1);
break;
}
case bastore:
state.pop(3);
break;
case int2byte:
case int2char:
case int2short:
break;
case fmul:
case fadd:
case fsub:
case fdiv:
case fmod:
state.pop(1);
break;
case castore:
case iastore:
case fastore:
case sastore:
state.pop(3);
break;
case lastore:
case dastore:
state.pop(4);
break;
case dup2:
if (state.stack[state.stacksize-1] != null) {
Type value1 = state.pop1();
Type value2 = state.pop1();
state.push(value2);
state.push(value1);
state.push(value2);
state.push(value1);
} else {
Type value = state.pop2();
state.push(value);
state.push(value);
}
break;
case dup2_x1:
if (state.stack[state.stacksize-1] != null) {
Type value1 = state.pop1();
Type value2 = state.pop1();
Type value3 = state.pop1();
state.push(value2);
state.push(value1);
state.push(value3);
state.push(value2);
state.push(value1);
} else {
Type value1 = state.pop2();
Type value2 = state.pop1();
state.push(value1);
state.push(value2);
state.push(value1);
}
break;
case dup2_x2:
if (state.stack[state.stacksize-1] != null) {
Type value1 = state.pop1();
Type value2 = state.pop1();
if (state.stack[state.stacksize-1] != null) {
// form 1
Type value3 = state.pop1();
Type value4 = state.pop1();
state.push(value2);
state.push(value1);
state.push(value4);
state.push(value3);
state.push(value2);
state.push(value1);
} else {
// form 3
Type value3 = state.pop2();
state.push(value2);
state.push(value1);
state.push(value3);
state.push(value2);
state.push(value1);
}
} else {
Type value1 = state.pop2();
if (state.stack[state.stacksize-1] != null) {
// form 2
Type value2 = state.pop1();
Type value3 = state.pop1();
state.push(value1);
state.push(value3);
state.push(value2);
state.push(value1);
} else {
// form 4
Type value2 = state.pop2();
state.push(value1);
state.push(value2);
state.push(value1);
}
}
break;
case dup_x2: {
Type value1 = state.pop1();
if (state.stack[state.stacksize-1] != null) {
// form 1
Type value2 = state.pop1();
Type value3 = state.pop1();
state.push(value1);
state.push(value3);
state.push(value2);
state.push(value1);
} else {
// form 2
Type value2 = state.pop2();
state.push(value1);
state.push(value2);
state.push(value1);
}
}
break;
case fcmpl:
case fcmpg:
state.pop(2);
state.push(syms.intType);
break;
case dcmpl:
case dcmpg:
state.pop(4);
state.push(syms.intType);
break;
case swap: {
Type value1 = state.pop1();
Type value2 = state.pop1();
state.push(value1);
state.push(value2);
break;
}
case dadd:
case dsub:
case dmul:
case ddiv:
case dmod:
state.pop(2);
break;
case ret:
markDead();
break;
case wide:
// must be handled by the caller.
return;
case monitorenter:
case monitorexit:
state.pop(1);
break;
default:
throw new AssertionError(mnem(op));
}
postop();
}
Emit an opcode with no operand field. |
public void emitop1(int op,
int od) {
emitop(op);
if (!alive) return;
emit1(od);
switch (op) {
case bipush:
state.push(syms.intType);
break;
case ldc1:
state.push(typeForPool(pool.pool[od]));
break;
default:
throw new AssertionError(mnem(op));
}
postop();
}
Emit an opcode with a one-byte operand field. |
public void emitop1w(int op,
int od) {
if (od > 0xFF) {
emitop(wide);
emitop(op);
emit2(od);
} else {
emitop(op);
emit1(od);
}
if (!alive) return;
switch (op) {
case iload:
state.push(syms.intType);
break;
case lload:
state.push(syms.longType);
break;
case fload:
state.push(syms.floatType);
break;
case dload:
state.push(syms.doubleType);
break;
case aload:
state.push(lvar[od].sym.type);
break;
case lstore:
case dstore:
state.pop(2);
break;
case istore:
case fstore:
case astore:
state.pop(1);
break;
case ret:
markDead();
break;
default:
throw new AssertionError(mnem(op));
}
postop();
}
Emit an opcode with a one-byte operand field;
widen if field does not fit in a byte. |
public void emitop1w(int op,
int od1,
int od2) {
if (od1 > 0xFF || od2 < -128 || od2 > 127) {
emitop(wide);
emitop(op);
emit2(od1);
emit2(od2);
} else {
emitop(op);
emit1(od1);
emit1(od2);
}
if (!alive) return;
switch (op) {
case iinc:
break;
default:
throw new AssertionError(mnem(op));
}
}
Emit an opcode with two one-byte operand fields;
widen if either field does not fit in a byte. |
public void emitop2(int op,
int od) {
emitop(op);
if (!alive) return;
emit2(od);
switch (op) {
case getstatic:
state.push(((Symbol)(pool.pool[od])).erasure(types));
break;
case putstatic:
state.pop(((Symbol)(pool.pool[od])).erasure(types));
break;
case new_:
state.push(uninitializedObject(((Symbol)(pool.pool[od])).erasure(types), cp-3));
break;
case sipush:
state.push(syms.intType);
break;
case if_acmp_null:
case if_acmp_nonnull:
case ifeq:
case ifne:
case iflt:
case ifge:
case ifgt:
case ifle:
state.pop(1);
break;
case if_icmpeq:
case if_icmpne:
case if_icmplt:
case if_icmpge:
case if_icmpgt:
case if_icmple:
case if_acmpeq:
case if_acmpne:
state.pop(2);
break;
case goto_:
markDead();
break;
case putfield:
state.pop(((Symbol)(pool.pool[od])).erasure(types));
state.pop(1); // object ref
break;
case getfield:
state.pop(1); // object ref
state.push(((Symbol)(pool.pool[od])).erasure(types));
break;
case checkcast: {
state.pop(1); // object ref
Object o = pool.pool[od];
Type t = (o instanceof Symbol)
? ((Symbol)o).erasure(types)
: types.erasure(((Type)o));
state.push(t);
break; }
case ldc2w:
state.push(typeForPool(pool.pool[od]));
break;
case instanceof_:
state.pop(1);
state.push(syms.intType);
break;
case ldc2:
state.push(typeForPool(pool.pool[od]));
break;
case jsr:
break;
default:
throw new AssertionError(mnem(op));
}
// postop();
}
Emit an opcode with a two-byte operand field. |
public void emitop4(int op,
int od) {
emitop(op);
if (!alive) return;
emit4(od);
switch (op) {
case goto_w:
markDead();
break;
case jsr_w:
break;
default:
throw new AssertionError(mnem(op));
}
// postop();
}
Emit an opcode with a four-byte operand field. |
public void endScopes(int first) {
int prevNextReg = nextreg;
nextreg = first;
for (int i = nextreg; i < prevNextReg; i++) endScope(i);
}
End scopes of all variables with registers >= first. |
public int entryPoint() {
int pc = curPc();
alive = true;
pendingStackMap = needStackMap;
return pc;
}
Declare an entry point; return current code pointer |
public int entryPoint(State state) {
int pc = curPc();
alive = true;
this.state = state.dup();
Assert.check(state.stacksize < = max_stack);
if (debugCode) System.err.println("entry point " + state);
pendingStackMap = needStackMap;
return pc;
}
Declare an entry point with initial state;
return current code pointer |
public int entryPoint(State state,
Type pushed) {
int pc = curPc();
alive = true;
this.state = state.dup();
Assert.check(state.stacksize < = max_stack);
this.state.push(pushed);
if (debugCode) System.err.println("entry point " + state);
pendingStackMap = needStackMap;
return pc;
}
Declare an entry point with initial state plus a pushed value;
return current code pointer |
public int get4(int pc) {
// pre: pc + 4 < = cp
return
(get1(pc) < < 24) |
(get1(pc+1) < < 16) |
(get1(pc+2) < < 8) |
(get1(pc+3));
}
Return four code bytes at position pc as an int. |
StackMapFrame getInitialFrame() {
StackMapFrame frame = new StackMapFrame();
List< Type > arg_types = ((MethodType)meth.externalType(types)).argtypes;
int len = arg_types.length();
int count = 0;
if (!meth.isStatic()) {
Type thisType = meth.owner.type;
frame.locals = new Type[len+1];
if (meth.isConstructor() && thisType != syms.objectType) {
frame.locals[count++] = UninitializedType.uninitializedThis(thisType);
} else {
frame.locals[count++] = types.erasure(thisType);
}
} else {
frame.locals = new Type[len];
}
for (Type arg_type : arg_types) {
frame.locals[count++] = types.erasure(arg_type);
}
frame.pc = -1;
frame.stack = null;
return frame;
}
|
public boolean isAlive() {
return alive || pendingJumps != null;
}
Is code generation currently enabled? |
public void markDead() {
alive = false;
}
Switch code generation on/off. |
public void markStatBegin() {
if (alive && lineDebugInfo) {
int line = lineMap.getLineNumber(pendingStatPos);
char cp1 = (char)cp;
char line1 = (char)line;
if (cp1 == cp && line1 == line)
addLineNumber(cp1, line1);
}
pendingStatPos = Position.NOPOS;
}
|
public static Chain mergeChains(Chain chain1,
Chain chain2) {
// recursive merge sort
if (chain2 == null) return chain1;
if (chain1 == null) return chain2;
Assert.check(
chain1.state.stacksize == chain2.state.stacksize &&
chain1.state.nlocks == chain2.state.nlocks);
if (chain1.pc < chain2.pc)
return new Chain(
chain2.pc,
mergeChains(chain1, chain2.next),
chain2.state);
return new Chain(
chain1.pc,
mergeChains(chain1.next, chain2),
chain1.state);
}
Merge the jumps in of two chains into one. |
public static String mnem(int opcode) {
return Mneumonics.mnem[opcode];
}
static tables
************************************************************************ |
public static int negate(int opcode) {
if (opcode == if_acmp_null) return if_acmp_nonnull;
else if (opcode == if_acmp_nonnull) return if_acmp_null;
else return ((opcode + 1) ^ 1) - 1;
}
|
public int newLocal(VarSymbol v) {
int reg = v.adr = newLocal(v.erasure(types));
addLocalVar(v);
return reg;
}
|
public void newRegSegment() {
nextreg = max_locals;
}
Start a set of fresh registers. |
void postop() {
Assert.check(alive || state.stacksize == 0);
}
|
public void put4(int pc,
int od) {
// pre: pc + 4 < = cp
put1(pc , od > > 24);
put1(pc+1, od > > 16);
put1(pc+2, od > > 8);
put1(pc+3, od);
}
Place four bytes into code at address pc. Pre: pc + 4 <= cp. |
void putVar(LocalVar var) {
if (!varDebugInfo) return;
if ((var.sym.flags() & Flags.SYNTHETIC) != 0) return;
if (varBuffer == null)
varBuffer = new LocalVar[20];
else if (varBufferSize >= varBuffer.length) {
LocalVar[] newVarBuffer = new LocalVar[varBufferSize*2];
System.arraycopy(varBuffer, 0, newVarBuffer, 0, varBuffer.length);
varBuffer = newVarBuffer;
}
varBuffer[varBufferSize++] = var;
}
Put a live variable range into the buffer to be output to the
class file. |
public void resolve(Chain chain) {
Assert.check(
!alive ||
chain==null ||
state.stacksize == chain.state.stacksize &&
state.nlocks == chain.state.nlocks);
pendingJumps = mergeChains(chain, pendingJumps);
}
Resolve chain to point to current code pointer. |
public void resolve(Chain chain,
int target) {
boolean changed = false;
State newState = state;
for (; chain != null; chain = chain.next) {
Assert.check(state != chain.state
&& (target > chain.pc || state.stacksize == 0));
if (target >= cp) {
target = cp;
} else if (get1(target) == goto_) {
if (fatcode) target = target + get4(target + 1);
else target = target + get2(target + 1);
}
if (get1(chain.pc) == goto_ &&
chain.pc + 3 == target && target == cp && !fixedPc) {
// If goto the next instruction, the jump is not needed:
// compact the code.
cp = cp - 3;
target = target - 3;
if (chain.next == null) {
// This is the only jump to the target. Exit the loop
// without setting new state. The code is reachable
// from the instruction before goto_.
alive = true;
break;
}
} else {
if (fatcode)
put4(chain.pc + 1, target - chain.pc);
else if (target - chain.pc < Short.MIN_VALUE ||
target - chain.pc > Short.MAX_VALUE)
fatcode = true;
else
put2(chain.pc + 1, target - chain.pc);
Assert.check(!alive ||
chain.state.stacksize == newState.stacksize &&
chain.state.nlocks == newState.nlocks);
}
fixedPc = true;
if (cp == target) {
changed = true;
if (debugCode)
System.err.println("resolving chain state=" + chain.state);
if (alive) {
newState = chain.state.join(newState);
} else {
newState = chain.state;
alive = true;
}
}
}
Assert.check(!changed || state != newState);
if (state != newState) {
setDefined(newState.defined);
state = newState;
pendingStackMap = needStackMap;
}
}
Resolve chain to point to given target. |
public void resolvePending() {
Chain x = pendingJumps;
pendingJumps = null;
resolve(x, cp);
}
Resolve any pending jumps. |
public void setDefined(Bits newDefined) {
if (alive && newDefined != state.defined) {
Bits diff = state.defined.dup().xorSet(newDefined);
for (int adr = diff.nextBit(0);
adr >= 0;
adr = diff.nextBit(adr+1)) {
if (adr >= nextreg)
state.defined.excl(adr);
else if (state.defined.isMember(adr))
setUndefined(adr);
else
setDefined(adr);
}
}
}
Set the current variable defined state. |
public void setDefined(int adr) {
LocalVar v = lvar[adr];
if (v == null) {
state.defined.excl(adr);
} else {
state.defined.incl(adr);
if (cp < Character.MAX_VALUE) {
if (v.start_pc == Character.MAX_VALUE)
v.start_pc = (char)cp;
}
}
}
Mark a register as being (possibly) defined. |
public void setUndefined(int adr) {
state.defined.excl(adr);
if (adr < lvar.length &&
lvar[adr] != null &&
lvar[adr].start_pc != Character.MAX_VALUE) {
LocalVar v = lvar[adr];
char length = (char)(curPc() - v.start_pc);
if (length > 0 && length < Character.MAX_VALUE) {
lvar[adr] = v.dup();
v.length = length;
putVar(v);
} else {
v.start_pc = Character.MAX_VALUE;
}
}
}
Mark a register as being undefined. |
public void statBegin(int pos) {
if (pos != Position.NOPOS) {
pendingStatPos = pos;
}
}
Mark beginning of statement. |
public static int truncate(int tc) {
switch (tc) {
case BYTEcode: case SHORTcode: case CHARcode: return INTcode;
default: return tc;
}
}
Collapse type code for subtypes of int to INTcode. |
public static int typecode(Type type) {
switch (type.tag) {
case BYTE: return BYTEcode;
case SHORT: return SHORTcode;
case CHAR: return CHARcode;
case INT: return INTcode;
case LONG: return LONGcode;
case FLOAT: return FLOATcode;
case DOUBLE: return DOUBLEcode;
case BOOLEAN: return BYTEcode;
case VOID: return VOIDcode;
case CLASS:
case ARRAY:
case METHOD:
case BOT:
case TYPEVAR:
case UNINITIALIZED_THIS:
case UNINITIALIZED_OBJECT:
return OBJECTcode;
default: throw new AssertionError("typecode " + type.tag);
}
}
Given a type, return its type code (used implicitly in the
JVM architecture). |
public static int width(int typecode) {
switch (typecode) {
case LONGcode: case DOUBLEcode: return 2;
case VOIDcode: return 0;
default: return 1;
}
}
The width in bytes of objects of the type. |
public static int width(Type type) {
return type == null ? 1 : width(typecode(type));
}
|
public static int width(List<Type> types) {
int w = 0;
for (List< Type > l = types; l.nonEmpty(); l = l.tail)
w = w + width(l.head);
return w;
}
The total width taken up by a vector of objects. |