/** * returns the local variable at an index int the scope of PC * * @param lvt * the local variable table * @param index * the variable index * @param pc * the PC where the variable is used */ public static LocalVariable getLocalVariableAtPC(@Nonnull LocalVariableTable lvt, int index, int pc) { int length = lvt.getTableLength(); LocalVariable[] lvs = lvt.getLocalVariableTable(); for (int i = 0; i < length; i++) { if (lvs[i].getIndex() == index) { int startPC = lvs[i].getStartPC(); if ((pc >= startPC) && (pc < (startPC + lvs[i].getLength()))) { return lvs[i]; } } } return null; } }
@CheckForNull GenericObjectType getLocalVariable(int index, int pos) { if (genericLocalVariables == null || !genericLocalVariables.get(index)) { return null; } for (LocalVariable local : localTypeTable.getLocalVariableTypeTable()) { if (local.getIndex() == index && local.getStartPC() <= pos && pos < +local.getStartPC() + local.getLength()) { String signature = local.getSignature(); if (signature.indexOf('<') < 0) { continue; } Type t; try { t = GenericUtilities.getType(signature); if (t instanceof GenericObjectType) { return (GenericObjectType) t; } } catch (RuntimeException e) { AnalysisContext.logError("Bad signature " + signature + " for " + local.getName(), e); } return null; } } return null; }
/** * * @param index the variable slot * @param pc the current pc that this variable is alive * * @return the LocalVariable that matches or null if not found */ public final LocalVariable getLocalVariable( final int index, final int pc ) { for (final LocalVariable variable : local_variable_table) { if (variable.getIndex() == index) { final int start_pc = variable.getStartPC(); final int end_pc = start_pc + variable.getLength(); if ((pc >= start_pc) && (pc <= end_pc)) { return variable; } } } return null; }
private static boolean isSuperpositionDetected(LocalVariable[] table, LocalVariable lvar) { for (LocalVariable localVariable : table) { if (lvar != localVariable && lvar.getName().equals(localVariable.getName()) && lvar.getStartPC() >= localVariable.getStartPC() && (lvar.getStartPC() + lvar.getLength() <= localVariable.getStartPC() + localVariable.getLength())) return true; } return false; }
/** * returns the end pc of the visible range of this register at this pc * * @param lvt * the local variable table for this method * @param reg * the register to examine * @param curPC * the pc of the current instruction * @return the endpc */ public static int getLocalVariableEndRange(LocalVariableTable lvt, int reg, int curPC) { int endRange = Integer.MAX_VALUE; if (lvt != null) { LocalVariable lv = lvt.getLocalVariable(reg, curPC); if (lv != null) { endRange = lv.getStartPC() + lv.getLength(); } } return endRange; }
/** * returns the end pc of the visible range of this register at this pc * * @param lvt * the local variable table for this method * @param reg * the register to examine * @param curPC * the pc of the current instruction * @return the endpc */ public static int getLocalVariableEndRange(LocalVariableTable lvt, int reg, int curPC) { int endRange = Integer.MAX_VALUE; if (lvt != null) { LocalVariable lv = lvt.getLocalVariable(reg, curPC); if (lv != null) { endRange = lv.getStartPC() + lv.getLength(); } } return endRange; }
private void buildVariableEndScopeMap() { endOfScopes = new HashMap<>(); LocalVariableTable lvt = getMethod().getLocalVariableTable(); if (lvt != null) { int len = lvt.getLength(); for (int i = 0; i < len; i++) { @SuppressWarnings("deprecation") LocalVariable lv = lvt.getLocalVariable(i); if (lv != null) { Integer endPC = Integer.valueOf(lv.getStartPC() + lv.getLength()); BitSet vars = endOfScopes.get(endPC); if (vars == null) { vars = new BitSet(); endOfScopes.put(endPC, vars); } vars.set(lv.getIndex()); } } } }
private void buildVariableEndScopeMap() { LocalVariableTable lvt = getMethod().getLocalVariableTable(); if ((lvt == null) || (lvt.getLength() == 0)) { endOfScopes = Collections.emptyMap(); } else { int len = lvt.getLength(); endOfScopes = new HashMap<>((int) (len * 1.6)); for (int i = 0; i < len; i++) { @SuppressWarnings("deprecation") LocalVariable lv = lvt.getLocalVariable(i); if (lv != null) { Integer endPC = Integer.valueOf(lv.getStartPC() + lv.getLength()); BitSet vars = endOfScopes.get(endPC); if (vars == null) { vars = new BitSet(); endOfScopes.put(endPC, vars); } vars.set(lv.getIndex()); } } } }
/** * returns the local variable at an index int the scope of PC * * @param lvt * the local variable table * @param index * the variable index * @param pc * the PC where the variable is used */ public static LocalVariable getLocalVariableAtPC(@Nonnull LocalVariableTable lvt, int index, int pc) { int length = lvt.getTableLength(); LocalVariable[] lvs = lvt.getLocalVariableTable(); for (int i = 0; i < length; i++) { if (lvs[i].getIndex() == index) { int startPC = lvs[i].getStartPC(); if ((pc >= startPC) && (pc < (startPC + lvs[i].getLength()))) { return lvs[i]; } } } return null; } }
public RealLocalVariableTableEntry(LocalVariable []locals, int _nameIndex, int _length) { super(_nameIndex, _length); for (LocalVariable i : locals) { getPool().add(new RealLocalVariableInfo(i.getStartPC(), i.getLength(), i.getNameIndex(), i.getSignatureIndex(), i.getIndex())); } }
/** * Initialize from another object. Note that both objects use the same * references (shallow copy). Use copy() for a physical copy. */ public LocalVariable(LocalVariable c) { this(c.getStartPC(), c.getLength(), c.getNameIndex(), c.getSignatureIndex(), c.getIndex(), c.getConstantPool()); }
@CheckForNull GenericObjectType getLocalVariable(int index, int pos) { if (genericLocalVariables == null || !genericLocalVariables.get(index)) { return null; } for (LocalVariable local : localTypeTable.getLocalVariableTypeTable()) { if (local.getIndex() == index && local.getStartPC() <= pos && pos < +local.getStartPC() + local.getLength()) { String signature = local.getSignature(); if (signature.indexOf('<') < 0) { continue; } Type t; try { t = GenericUtilities.getType(signature); if (t instanceof GenericObjectType) { return (GenericObjectType) t; } } catch (RuntimeException e) { AnalysisContext.logError("Bad signature " + signature + " for " + local.getName(), e); } return null; } } return null; }
/** * Initialize from another object. Note that both objects use the same * references (shallow copy). Use copy() for a physical copy. */ public LocalVariable(final LocalVariable c) { this(c.getStartPC(), c.getLength(), c.getNameIndex(), c.getSignatureIndex(), c.getIndex(), c.getConstantPool()); this.orig_index = c.getOrigIndex(); }
LocalVariable lv = lvt.getLocalVariable(reg, pc + 2); if (lv != null) { int finish = lv.getStartPC() + lv.getLength(); if (finish < ci.getFinish()) { ci.setFinish(finish);
public static String getLocalVariableName(Method method, int slot, int pc) { if (method.getLocalVariableTable() != null) { LocalVariable[] table= method.getLocalVariableTable().getLocalVariableTable(); LocalVariable lvar= null; for (int i= 0; i < table.length; i++) { lvar= table[i]; if (lvar.getIndex() == slot && lvar.getStartPC() <= pc && pc <= lvar.getStartPC() + lvar.getLength()) { String name2= lvar.getName(); if (name2.equals("in")) name2= "in_"; if (isSuperpositionDetected(table, lvar)) throw new UnhandledCompilerProblemException(); return name2; } } } return "l" + slot; }
private void adjustLocalVariableTypeTable(final LocalVariableTable lvt) { final LocalVariable[] lv = lvt.getLocalVariableTable(); final LocalVariable[] lvg = local_variable_type_table.getLocalVariableTypeTable(); for (final LocalVariable element : lvg) { for (final LocalVariable l : lv) { if (element.getName().equals(l.getName()) && element.getIndex() == l.getOrigIndex()) { element.setLength(l.getLength()); element.setStartPC(l.getStartPC()); element.setIndex(l.getIndex()); break; } } } }
/** * reduces the end pc based on the optional LocalVariableTable's exception * register scope * * @param infos the list of active catch blocks * @param pc the current pc * @param seen the currently parsed opcode */ private void updateEndPCsOnCatchRegScope(List<CatchInfo> infos, int pc, int seen) { if (lvt != null) { for (CatchInfo ci : infos) { if ((ci.getStart() == pc) && OpcodeUtils.isAStore(seen)) { int exReg = RegisterUtils.getAStoreReg(this, seen); LocalVariable lv = lvt.getLocalVariable(exReg, pc + 1); if (lv != null) { ci.setFinish(lv.getStartPC() + lv.getLength()); } break; } } } }
/** * reduces the end pc based on the optional LocalVariableTable's exception register scope * * @param infos * the list of active catch blocks * @param pc * the current pc * @param seen * the currently parsed opcode */ private void updateEndPCsOnCatchRegScope(List<CatchInfo> infos, int pc, int seen) { if (lvt != null) { for (CatchInfo ci : infos) { if ((ci.getStart() == pc) && OpcodeUtils.isAStore(seen)) { int exReg = RegisterUtils.getAStoreReg(this, seen); LocalVariable lv = lvt.getLocalVariable(exReg, pc + 1); if (lv != null) { ci.setFinish(lv.getStartPC() + lv.getLength()); } break; } } } }
private void updateLocalVariableTable(final LocalVariableTable a) { final LocalVariable[] lv = a.getLocalVariableTable(); removeLocalVariables(); for (final LocalVariable l : lv) { InstructionHandle start = il.findHandle(l.getStartPC()); final InstructionHandle end = il.findHandle(l.getStartPC() + l.getLength()); // Repair malformed handles if (null == start) { start = il.getStart(); } // end == null => live to end of method // Since we are recreating the LocalVaraible, we must // propagate the orig_index to new copy. addLocalVariable(l.getName(), Type.getType(l.getSignature()), l .getIndex(), start, end, l.getOrigIndex()); } }
/** * BCEL-295: */ public void testB295() throws Exception { final JavaClass clazz = getTestClass(PACKAGE_BASE_NAME+".data.PLSETestClass2"); final ClassGen cg = new ClassGen(clazz); final ConstantPoolGen pool = cg.getConstantPool(); final Method m = cg.getMethodAt(1); // 'main' final LocalVariableTable lvt = m.getLocalVariableTable(); final LocalVariable lv = lvt.getLocalVariable(2, 4); // 'i' //System.out.println(lv); final MethodGen mg = new MethodGen(m, cg.getClassName(), pool); final LocalVariableTable new_lvt = mg.getLocalVariableTable(mg.getConstantPool()); final LocalVariable new_lv = new_lvt.getLocalVariable(2, 4); // 'i' //System.out.println(new_lv); assertEquals("live range length", lv.getLength(), new_lv.getLength()); } }