/** Generate code */ public void write(Environment env, DataOutputStream out, MemberDefinition field, ConstantPool tab) throws IOException { // listing(System.out); if ((field != null) && field.getArguments() != null) { int sum = 0; Vector v = field.getArguments(); for (Enumeration e = v.elements(); e.hasMoreElements(); ) { MemberDefinition f = ((MemberDefinition) e.nextElement()); sum += f.getType().stackSize(); } maxvar = sum; } // Make sure the stack balances. Also calculate maxvar and maxstack try { balance(first, 0); } catch (CompilerError e) { System.out.println("ERROR: " + e); listing(System.out); throw e; } // Assign PCs int pc = 0, nexceptions = 0; for (Instruction inst = first; inst != null; inst = inst.next) { inst.pc = pc; int sz = inst.size(tab); if (pc < 65536 && (pc + sz) >= 65536) { env.error(inst.where, "warn.method.too.long"); } pc += sz; if (inst.opc == opc_try) { nexceptions += ((TryData) inst.value).catches.size(); } } // Write header out.writeShort(maxdepth); out.writeShort(maxvar); out.writeInt(maxpc = pc); // Generate code for (Instruction inst = first.next; inst != null; inst = inst.next) { inst.write(out, tab); } // write exceptions out.writeShort(nexceptions); if (nexceptions > 0) { // listing(System.out); writeExceptions(env, out, tab, first, last); } }
/** Collect all constants into the constant table */ public void collect(Environment env, MemberDefinition field, ConstantPool tab) { // Collect constants for arguments only // if a local variable table is generated if ((field != null) && env.debug_vars()) { if (field.getArguments() != null) { for (Enumeration e = field.getArguments().elements(); e.hasMoreElements(); ) { MemberDefinition f = (MemberDefinition) e.nextElement(); tab.put(f.getName().toString()); tab.put(f.getType().getTypeSignature()); } } } // Collect constants from the instructions for (Instruction inst = first; inst != null; inst = inst.next) { inst.collect(tab); } }
/** * Write the local variable table. The necessary constants have already been added to the constant * table by the collect() method. The flowFields method is used to determine which variables are * alive at each pc. */ public void writeLocalVariableTable( Environment env, MemberDefinition field, DataOutputStream out, ConstantPool tab) throws IOException { MemberDefinition locals[] = new MemberDefinition[maxvar]; int i = 0; // Initialize arguments if ((field != null) && (field.getArguments() != null)) { int reg = 0; Vector v = field.getArguments(); for (Enumeration e = v.elements(); e.hasMoreElements(); ) { MemberDefinition f = ((MemberDefinition) e.nextElement()); locals[reg] = f; reg += f.getType().stackSize(); } } flowFields(env, first, locals); LocalVariableTable lvtab = new LocalVariableTable(); // Initialize arguments again for (i = 0; i < maxvar; i++) locals[i] = null; if ((field != null) && (field.getArguments() != null)) { int reg = 0; Vector v = field.getArguments(); for (Enumeration e = v.elements(); e.hasMoreElements(); ) { MemberDefinition f = ((MemberDefinition) e.nextElement()); locals[reg] = f; lvtab.define(f, reg, 0, maxpc); reg += f.getType().stackSize(); } } int pcs[] = new int[maxvar]; for (Instruction inst = first; inst != null; inst = inst.next) { switch (inst.opc) { case opc_istore: case opc_istore_0: case opc_istore_1: case opc_istore_2: case opc_istore_3: case opc_fstore: case opc_fstore_0: case opc_fstore_1: case opc_fstore_2: case opc_fstore_3: case opc_astore: case opc_astore_0: case opc_astore_1: case opc_astore_2: case opc_astore_3: case opc_lstore: case opc_lstore_0: case opc_lstore_1: case opc_lstore_2: case opc_lstore_3: case opc_dstore: case opc_dstore_0: case opc_dstore_1: case opc_dstore_2: case opc_dstore_3: if (inst.value instanceof LocalVariable) { LocalVariable v = (LocalVariable) inst.value; int pc = (inst.next != null) ? inst.next.pc : inst.pc; if (locals[v.slot] != null) { lvtab.define(locals[v.slot], v.slot, pcs[v.slot], pc); } pcs[v.slot] = pc; locals[v.slot] = v.field; } break; case opc_label: { // flush previous labels for (i = 0; i < maxvar; i++) { if (locals[i] != null) { lvtab.define(locals[i], i, pcs[i], inst.pc); } } // init new labels int pc = inst.pc; MemberDefinition[] labelLocals = ((Label) inst).locals; if (labelLocals == null) { // unreachable code?? for (i = 0; i < maxvar; i++) locals[i] = null; } else { System.arraycopy(labelLocals, 0, locals, 0, maxvar); } for (i = 0; i < maxvar; i++) { pcs[i] = pc; } break; } } } // flush remaining labels for (i = 0; i < maxvar; i++) { if (locals[i] != null) { lvtab.define(locals[i], i, pcs[i], maxpc); } } // write the local variable table lvtab.write(env, out, tab); }