You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CodeAttribute.java 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved.
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. Alternatively, the contents of this file may be used under
  8. * the terms of the GNU Lesser General Public License Version 2.1 or later,
  9. * or the Apache License Version 2.0.
  10. *
  11. * Software distributed under the License is distributed on an "AS IS" basis,
  12. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. * for the specific language governing rights and limitations under the
  14. * License.
  15. */
  16. package javassist.bytecode;
  17. import javassist.*;
  18. import java.io.DataInputStream;
  19. import java.io.DataOutputStream;
  20. import java.io.IOException;
  21. import java.util.ArrayList;
  22. import java.util.List;
  23. import java.util.Map;
  24. /**
  25. * <code>Code_attribute</code>.
  26. *
  27. * <p>To browse the <code>code</code> field of
  28. * a <code>Code_attribute</code> structure,
  29. * use <code>CodeIterator</code>.
  30. *
  31. * @see CodeIterator
  32. * @see #iterator()
  33. */
  34. public class CodeAttribute extends AttributeInfo implements Opcode {
  35. /**
  36. * The name of this attribute <code>"Code"</code>.
  37. */
  38. public static final String tag = "Code";
  39. // code[] is stored in AttributeInfo.info.
  40. private int maxStack;
  41. private int maxLocals;
  42. private ExceptionTable exceptions;
  43. private List<AttributeInfo> attributes;
  44. /**
  45. * Constructs a <code>Code_attribute</code>.
  46. *
  47. * @param cp constant pool table
  48. * @param stack <code>max_stack</code>
  49. * @param locals <code>max_locals</code>
  50. * @param code <code>code[]</code>
  51. * @param etable <code>exception_table[]</code>
  52. */
  53. public CodeAttribute(ConstPool cp, int stack, int locals, byte[] code,
  54. ExceptionTable etable)
  55. {
  56. super(cp, tag);
  57. maxStack = stack;
  58. maxLocals = locals;
  59. info = code;
  60. exceptions = etable;
  61. attributes = new ArrayList<AttributeInfo>();
  62. }
  63. /**
  64. * Constructs a copy of <code>Code_attribute</code>.
  65. * Specified class names are replaced during the copy.
  66. *
  67. * @param cp constant pool table.
  68. * @param src source Code attribute.
  69. * @param classnames pairs of replaced and substituted
  70. * class names.
  71. */
  72. private CodeAttribute(ConstPool cp, CodeAttribute src, Map<String,String> classnames)
  73. throws BadBytecode, NotFoundException, CannotCompileException {
  74. super(cp, tag);
  75. maxStack = src.getMaxStack();
  76. maxLocals = src.getMaxLocals();
  77. exceptions = src.getExceptionTable().copy(cp, classnames);
  78. attributes = new ArrayList<AttributeInfo>();
  79. List<AttributeInfo> src_attr = src.getAttributes();
  80. int num = src_attr.size();
  81. for (int i = 0; i < num; ++i) {
  82. AttributeInfo ai = src_attr.get(i);
  83. attributes.add(ai.copy(cp, classnames));
  84. }
  85. info = src.copyCode(cp, classnames, exceptions, this);
  86. }
  87. CodeAttribute(ConstPool cp, int name_id, DataInputStream in)
  88. throws IOException
  89. {
  90. super(cp, name_id, (byte[])null);
  91. @SuppressWarnings("unused")
  92. int attr_len = in.readInt();
  93. maxStack = in.readUnsignedShort();
  94. maxLocals = in.readUnsignedShort();
  95. int code_len = in.readInt();
  96. info = new byte[code_len];
  97. in.readFully(info);
  98. exceptions = new ExceptionTable(cp, in);
  99. attributes = new ArrayList<AttributeInfo>();
  100. int num = in.readUnsignedShort();
  101. for (int i = 0; i < num; ++i)
  102. attributes.add(AttributeInfo.read(cp, in));
  103. }
  104. /**
  105. * Makes a copy. Class names are replaced according to the
  106. * given <code>Map</code> object.
  107. *
  108. * @param newCp the constant pool table used by the new copy.
  109. * @param classnames pairs of replaced and substituted
  110. * class names.
  111. * @exception RuntimeCopyException if a <code>BadBytecode</code>
  112. * exception is thrown, it is
  113. * converted into
  114. * <code>RuntimeCopyException</code>.
  115. *
  116. * @return <code>CodeAttribute</code> object.
  117. */
  118. @Override
  119. public AttributeInfo copy(ConstPool newCp, Map<String,String> classnames)
  120. throws RuntimeCopyException
  121. {
  122. try {
  123. return new CodeAttribute(newCp, this, classnames);
  124. }
  125. catch (BadBytecode e) {
  126. throw new RuntimeCopyException("bad bytecode. fatal?");
  127. } catch (NotFoundException e) {
  128. throw new RuntimeException(e);
  129. } catch (CannotCompileException e) {
  130. throw new RuntimeException(e);
  131. }
  132. }
  133. /**
  134. * An exception that may be thrown by <code>copy()</code>
  135. * in <code>CodeAttribute</code>.
  136. */
  137. public static class RuntimeCopyException extends RuntimeException {
  138. /** default serialVersionUID */
  139. private static final long serialVersionUID = 1L;
  140. /**
  141. * Constructs an exception.
  142. */
  143. public RuntimeCopyException(String s) {
  144. super(s);
  145. }
  146. }
  147. /**
  148. * Returns the length of this <code>attribute_info</code>
  149. * structure.
  150. * The returned value is <code>attribute_length + 6</code>.
  151. */
  152. @Override
  153. public int length() {
  154. return 18 + info.length + exceptions.size() * 8
  155. + AttributeInfo.getLength(attributes);
  156. }
  157. @Override
  158. void write(DataOutputStream out) throws IOException {
  159. out.writeShort(name); // attribute_name_index
  160. out.writeInt(length() - 6); // attribute_length
  161. out.writeShort(maxStack); // max_stack
  162. out.writeShort(maxLocals); // max_locals
  163. out.writeInt(info.length); // code_length
  164. out.write(info); // code
  165. exceptions.write(out);
  166. out.writeShort(attributes.size()); // attributes_count
  167. AttributeInfo.writeAll(attributes, out); // attributes
  168. }
  169. /**
  170. * This method is not available.
  171. *
  172. * @throws java.lang.UnsupportedOperationException always thrown.
  173. */
  174. @Override
  175. public byte[] get() {
  176. throw new UnsupportedOperationException("CodeAttribute.get()");
  177. }
  178. /**
  179. * This method is not available.
  180. *
  181. * @throws java.lang.UnsupportedOperationException always thrown.
  182. */
  183. @Override
  184. public void set(byte[] newinfo) {
  185. throw new UnsupportedOperationException("CodeAttribute.set()");
  186. }
  187. @Override
  188. void renameClass(String oldname, String newname) {
  189. AttributeInfo.renameClass(attributes, oldname, newname);
  190. }
  191. @Override
  192. void renameClass(Map<String,String> classnames) {
  193. AttributeInfo.renameClass(attributes, classnames);
  194. }
  195. @Override
  196. void getRefClasses(Map<String,String> classnames) {
  197. AttributeInfo.getRefClasses(attributes, classnames);
  198. }
  199. /**
  200. * Returns the name of the class declaring the method including
  201. * this code attribute.
  202. */
  203. public String getDeclaringClass() {
  204. ConstPool cp = getConstPool();
  205. return cp.getClassName();
  206. }
  207. /**
  208. * Returns <code>max_stack</code>.
  209. */
  210. public int getMaxStack() {
  211. return maxStack;
  212. }
  213. /**
  214. * Sets <code>max_stack</code>.
  215. */
  216. public void setMaxStack(int value) {
  217. maxStack = value;
  218. }
  219. /**
  220. * Computes the maximum stack size and sets <code>max_stack</code>
  221. * to the computed size.
  222. *
  223. * @throws BadBytecode if this method fails in computing.
  224. * @return the newly computed value of <code>max_stack</code>
  225. */
  226. public int computeMaxStack() throws BadBytecode {
  227. maxStack = new CodeAnalyzer(this).computeMaxStack();
  228. return maxStack;
  229. }
  230. /**
  231. * Returns <code>max_locals</code>.
  232. */
  233. public int getMaxLocals() {
  234. return maxLocals;
  235. }
  236. /**
  237. * Sets <code>max_locals</code>.
  238. */
  239. public void setMaxLocals(int value) {
  240. maxLocals = value;
  241. }
  242. /**
  243. * Returns <code>code_length</code>.
  244. */
  245. public int getCodeLength() {
  246. return info.length;
  247. }
  248. /**
  249. * Returns <code>code[]</code>.
  250. */
  251. public byte[] getCode() {
  252. return info;
  253. }
  254. /**
  255. * Sets <code>code[]</code>.
  256. */
  257. void setCode(byte[] newinfo) { super.set(newinfo); }
  258. /**
  259. * Makes a new iterator for reading this code attribute.
  260. */
  261. public CodeIterator iterator() {
  262. return new CodeIterator(this);
  263. }
  264. /**
  265. * Returns <code>exception_table[]</code>.
  266. */
  267. public ExceptionTable getExceptionTable() { return exceptions; }
  268. /**
  269. * Returns <code>attributes[]</code>.
  270. * It returns a list of <code>AttributeInfo</code>.
  271. * A new element can be added to the returned list
  272. * and an existing element can be removed from the list.
  273. *
  274. * @see AttributeInfo
  275. */
  276. public List<AttributeInfo> getAttributes() { return attributes; }
  277. /**
  278. * Returns the attribute with the specified name.
  279. * If it is not found, this method returns null.
  280. *
  281. * @param name attribute name
  282. * @return an <code>AttributeInfo</code> object or null.
  283. */
  284. public AttributeInfo getAttribute(String name) {
  285. return AttributeInfo.lookup(attributes, name);
  286. }
  287. /**
  288. * Adds a stack map table. If another copy of stack map table
  289. * is already contained, the old one is removed.
  290. *
  291. * @param smt the stack map table added to this code attribute.
  292. * If it is null, a new stack map is not added.
  293. * Only the old stack map is removed.
  294. */
  295. public void setAttribute(StackMapTable smt) {
  296. AttributeInfo.remove(attributes, StackMapTable.tag);
  297. if (smt != null)
  298. attributes.add(smt);
  299. }
  300. /**
  301. * Adds a stack map table for J2ME (CLDC). If another copy of stack map table
  302. * is already contained, the old one is removed.
  303. *
  304. * @param sm the stack map table added to this code attribute.
  305. * If it is null, a new stack map is not added.
  306. * Only the old stack map is removed.
  307. * @since 3.12
  308. */
  309. public void setAttribute(StackMap sm) {
  310. AttributeInfo.remove(attributes, StackMap.tag);
  311. if (sm != null)
  312. attributes.add(sm);
  313. }
  314. /**
  315. * Copies code.
  316. */
  317. private byte[] copyCode(ConstPool destCp, Map<String,String> classnames,
  318. ExceptionTable etable, CodeAttribute destCa)
  319. throws BadBytecode, NotFoundException, CannotCompileException {
  320. int len = getCodeLength();
  321. byte[] newCode = new byte[len];
  322. destCa.info = newCode;
  323. LdcEntry ldc = copyCode(this.info, 0, len, this.getConstPool(),
  324. newCode, destCp, classnames);
  325. return LdcEntry.doit(newCode, ldc, etable, destCa);
  326. }
  327. private static LdcEntry copyCode(byte[] code, int beginPos, int endPos,
  328. ConstPool srcCp, byte[] newcode,
  329. ConstPool destCp, Map<String,String> classnameMap)
  330. throws BadBytecode, NotFoundException, CannotCompileException {
  331. int i2, index;
  332. LdcEntry ldcEntry = null;
  333. for (int i = beginPos; i < endPos; i = i2) {
  334. i2 = CodeIterator.nextOpcode(code, i);
  335. byte c = code[i];
  336. newcode[i] = c;
  337. switch (c & 0xff) {
  338. case LDC_W :
  339. case LDC2_W :
  340. case GETSTATIC :
  341. case PUTSTATIC :
  342. case GETFIELD :
  343. case PUTFIELD :
  344. case INVOKEVIRTUAL :
  345. case INVOKESPECIAL :
  346. case INVOKESTATIC :
  347. case NEW :
  348. case ANEWARRAY :
  349. case CHECKCAST :
  350. case INSTANCEOF :
  351. copyConstPoolInfo(i + 1, code, srcCp, newcode, destCp,
  352. classnameMap);
  353. break;
  354. case LDC :
  355. index = code[i + 1] & 0xff;
  356. index = srcCp.copy(index, destCp, classnameMap);
  357. if (index < 0x100)
  358. newcode[i + 1] = (byte)index;
  359. else {
  360. newcode[i] = NOP;
  361. newcode[i + 1] = NOP;
  362. LdcEntry ldc = new LdcEntry();
  363. ldc.where = i;
  364. ldc.index = index;
  365. ldc.next = ldcEntry;
  366. ldcEntry = ldc;
  367. }
  368. break;
  369. case INVOKEINTERFACE :
  370. copyConstPoolInfo(i + 1, code, srcCp, newcode, destCp,
  371. classnameMap);
  372. newcode[i + 3] = code[i + 3];
  373. newcode[i + 4] = code[i + 4];
  374. break;
  375. case INVOKEDYNAMIC :
  376. copyConstPoolInfo(i + 1, code, srcCp, newcode, destCp,
  377. classnameMap);
  378. copyBootstrapMethod(srcCp, destCp, i + 1, code, newcode, classnameMap);
  379. newcode[i + 3] = 0;
  380. newcode[i + 4] = 0;
  381. break;
  382. case MULTIANEWARRAY :
  383. copyConstPoolInfo(i + 1, code, srcCp, newcode, destCp,
  384. classnameMap);
  385. newcode[i + 3] = code[i + 3];
  386. break;
  387. default :
  388. while (++i < i2)
  389. newcode[i] = code[i];
  390. break;
  391. }
  392. }
  393. return ldcEntry;
  394. }
  395. /**
  396. * Copy the Bootstrap method of the specified index referenced in the source <code>InvokeDynamic</code> directive
  397. * to the specified index in the destination Boostrap Attribute.<br>
  398. * if the Bootstrap Attribute does not exist in the destination class, create a new Bootstrap Attribute; <br>
  399. * if the destination Bootstrap Method already exists at the specified index method,
  400. * the method at that position will be overwritten, otherwise it will be added
  401. * at the end of the destination Bootstrap method.
  402. *
  403. * @param srcCp the constant pool table of source
  404. * @param destCp the constant pool table of destination
  405. * @param codeIndex the index of the invoke dynamic first parameter in code array
  406. * @param srcCode the code array of source
  407. * @param newCode the code array of destination
  408. * @param classnameMap pairs of replaced and substituted class names.
  409. *
  410. * @throws NotFoundException this exception thrown when the class
  411. * cannot be found in the default <code>ClassPool</code>
  412. * @throws CannotCompileException this exception thrown from the method
  413. * {@link #copyInvokeStaticMethod(CtClass, ConstPool,
  414. * BootstrapMethodsAttribute.BootstrapMethod, CtClass, Map)}
  415. */
  416. private static void copyBootstrapMethod(ConstPool srcCp, ConstPool destCp, int codeIndex, byte[] srcCode,
  417. byte[] newCode, Map<String,String> classnameMap)
  418. throws NotFoundException, CannotCompileException {
  419. ClassPool classPool = ClassPool.getDefault();
  420. CtClass srcCc = classPool.get(srcCp.getClassName());
  421. CtClass destCc = classPool.get(destCp.getClassName());
  422. ClassFile srcCf = srcCc.getClassFile();
  423. ClassFile destCf = destCc.getClassFile();
  424. BootstrapMethodsAttribute srcBma = (BootstrapMethodsAttribute)
  425. srcCf.getAttribute(BootstrapMethodsAttribute.tag);
  426. // if source class does not have bootstrap attribute then stop copy
  427. if (srcBma == null) {
  428. return;
  429. }
  430. BootstrapMethodsAttribute destBma = (BootstrapMethodsAttribute)
  431. destCf.getAttribute(BootstrapMethodsAttribute.tag);
  432. int srcCpIndex = ((srcCode[codeIndex] & 0xff) << 8) | (srcCode[codeIndex + 1] & 0xff);
  433. int destCpIndex = ((newCode[codeIndex] & 0xff) << 8) | (newCode[codeIndex + 1] & 0xff);
  434. int srcBmIndex = srcCp.getInvokeDynamicBootstrap(srcCpIndex);
  435. int destBmIndex = destCp.getInvokeDynamicBootstrap(destCpIndex);
  436. // if source class does not have bootstrap attribute, then create bootstrap attribute
  437. if (destBma == null) {
  438. destBma = new BootstrapMethodsAttribute(destCp,
  439. new BootstrapMethodsAttribute.BootstrapMethod[0]);
  440. destCf.addAttribute(destBma);
  441. }
  442. BootstrapMethodsAttribute.BootstrapMethod srcBm = srcBma.getMethods()[srcBmIndex];
  443. destBma.addMethod(srcCp, srcBm, destBmIndex, classnameMap);
  444. copyInvokeStaticMethod(srcCc, srcCp, srcBm, destCc, classnameMap);
  445. }
  446. /**
  447. * Copy the static methods referenced by the bootstrap method in this class (such as some lambda methods).<br>
  448. * If the source method exists in the destination class, it will be ignored.
  449. *
  450. * @param srcCc source class
  451. * @param srcCp constant pool table of source class
  452. * @param srcBm source method to be copied
  453. * @param destCc destination class
  454. * @param classnameMap irs of replaced and substituted class names.
  455. *
  456. * @throws CannotCompileException thrown by {@link CtNewMethod#copy(CtMethod, CtClass, ClassMap)}
  457. * or{@link CtClass#addMethod(CtMethod)}
  458. */
  459. private static void copyInvokeStaticMethod(CtClass srcCc, ConstPool srcCp,
  460. BootstrapMethodsAttribute.BootstrapMethod srcBm, CtClass destCc,
  461. Map<String, String> classnameMap) throws CannotCompileException {
  462. for (int argument : srcBm.arguments) {
  463. ConstInfo constInfo = srcCp.getItem(argument);
  464. if (!(constInfo instanceof MethodHandleInfo)) continue;
  465. MethodHandleInfo methodHandleInfo = (MethodHandleInfo) constInfo;
  466. if (ConstPool.REF_invokeStatic != methodHandleInfo.refKind) continue;
  467. String methodRefClassName = srcCp.getMethodrefClassName(methodHandleInfo.refIndex);
  468. if (methodRefClassName == null || !methodRefClassName.equals(srcCc.getName())) continue;
  469. String staticMethodName = srcCp.getMethodrefName(methodHandleInfo.refIndex);
  470. String staticMethodSignature = srcCp.getMethodrefType(methodHandleInfo.refIndex);
  471. CtMethod srcMethod = getStaticCtMethod(srcCc, staticMethodName, staticMethodSignature);
  472. if (!checkStaticMethodExisted(destCc, staticMethodName, staticMethodSignature)) {
  473. ClassMap classMap = new ClassMap();
  474. classMap.putAll(classnameMap);
  475. CtMethod ctMethod = CtNewMethod.copy(srcMethod, destCc, classMap);
  476. destCc.addMethod(ctMethod);
  477. }
  478. }
  479. }
  480. private static CtMethod getStaticCtMethod(CtClass ctClass, String staticMethodName, String staticMethodSignature) {
  481. CtMethod srcMethod = null;
  482. for (CtMethod declaredMethod : ctClass.getDeclaredMethods()) {
  483. if (Modifier.isStatic(declaredMethod.getModifiers())
  484. && declaredMethod.getName().equals(staticMethodName)
  485. && declaredMethod.getSignature().equals(staticMethodSignature)) {
  486. srcMethod = declaredMethod;
  487. break;
  488. }
  489. }
  490. if (srcMethod == null) {
  491. throw new RuntimeException("Can not found static method:" + staticMethodName);
  492. }
  493. return srcMethod;
  494. }
  495. private static boolean checkStaticMethodExisted(CtClass ctClass, String staticMethodName, String staticMethodSignature) {
  496. for (CtMethod declaredMethod : ctClass.getDeclaredMethods()) {
  497. if (Modifier.isStatic(declaredMethod.getModifiers())
  498. && declaredMethod.getName().equals(staticMethodName)
  499. && declaredMethod.getSignature().equals(staticMethodSignature)) {
  500. return true;
  501. }
  502. }
  503. return false;
  504. }
  505. private static void copyConstPoolInfo(int i, byte[] code, ConstPool srcCp,
  506. byte[] newcode, ConstPool destCp,
  507. Map<String,String> classnameMap) {
  508. int index = ((code[i] & 0xff) << 8) | (code[i + 1] & 0xff);
  509. index = srcCp.copy(index, destCp, classnameMap);
  510. newcode[i] = (byte)(index >> 8);
  511. newcode[i + 1] = (byte)index;
  512. }
  513. static class LdcEntry {
  514. LdcEntry next;
  515. int where;
  516. int index;
  517. static byte[] doit(byte[] code, LdcEntry ldc, ExceptionTable etable,
  518. CodeAttribute ca)
  519. throws BadBytecode
  520. {
  521. if (ldc != null)
  522. code = CodeIterator.changeLdcToLdcW(code, etable, ca, ldc);
  523. /* The original code was the following:
  524. while (ldc != null) {
  525. int where = ldc.where;
  526. code = CodeIterator.insertGapCore0(code, where, 1, false, etable, ca);
  527. code[where] = (byte)Opcode.LDC_W;
  528. ByteArray.write16bit(ldc.index, code, where + 1);
  529. ldc = ldc.next;
  530. }
  531. But this code does not support a large method > 32KB.
  532. */
  533. return code;
  534. }
  535. }
  536. /**
  537. * Changes the index numbers of the local variables
  538. * to append a new parameter.
  539. * This method does not update <code>LocalVariableAttribute</code>,
  540. * <code>LocalVariableTypeAttribute</code>,
  541. * <code>StackMapTable</code>, or <code>StackMap</code>.
  542. * These attributes must be explicitly updated.
  543. *
  544. * @param where the index of the new parameter.
  545. * @param size the type size of the new parameter (1 or 2).
  546. *
  547. * @see LocalVariableAttribute#shiftIndex(int, int)
  548. * @see LocalVariableTypeAttribute#shiftIndex(int, int)
  549. * @see StackMapTable#insertLocal(int, int, int)
  550. * @see StackMap#insertLocal(int, int, int)
  551. */
  552. public void insertLocalVar(int where, int size) throws BadBytecode {
  553. CodeIterator ci = iterator();
  554. while (ci.hasNext())
  555. shiftIndex(ci, where, size);
  556. setMaxLocals(getMaxLocals() + size);
  557. }
  558. /**
  559. * @param lessThan If the index of the local variable is
  560. * less than this value, it does not change.
  561. * Otherwise, the index is increased.
  562. * @param delta the indexes of the local variables are
  563. * increased by this value.
  564. */
  565. private static void shiftIndex(CodeIterator ci, int lessThan, int delta) throws BadBytecode {
  566. int index = ci.next();
  567. int opcode = ci.byteAt(index);
  568. if (opcode < ILOAD)
  569. return;
  570. else if (opcode < IASTORE) {
  571. if (opcode < ILOAD_0) {
  572. // iload, lload, fload, dload, aload
  573. shiftIndex8(ci, index, opcode, lessThan, delta);
  574. }
  575. else if (opcode < IALOAD) {
  576. // iload_0, ..., aload_3
  577. shiftIndex0(ci, index, opcode, lessThan, delta, ILOAD_0, ILOAD);
  578. }
  579. else if (opcode < ISTORE)
  580. return;
  581. else if (opcode < ISTORE_0) {
  582. // istore, lstore, ...
  583. shiftIndex8(ci, index, opcode, lessThan, delta);
  584. }
  585. else {
  586. // istore_0, ..., astore_3
  587. shiftIndex0(ci, index, opcode, lessThan, delta, ISTORE_0, ISTORE);
  588. }
  589. }
  590. else if (opcode == IINC) {
  591. int var = ci.byteAt(index + 1);
  592. if (var < lessThan)
  593. return;
  594. var += delta;
  595. if (var < 0x100)
  596. ci.writeByte(var, index + 1);
  597. else {
  598. int plus = (byte)ci.byteAt(index + 2);
  599. int pos = ci.insertExGap(3);
  600. ci.writeByte(WIDE, pos - 3);
  601. ci.writeByte(IINC, pos - 2);
  602. ci.write16bit(var, pos - 1);
  603. ci.write16bit(plus, pos + 1);
  604. }
  605. }
  606. else if (opcode == RET)
  607. shiftIndex8(ci, index, opcode, lessThan, delta);
  608. else if (opcode == WIDE) {
  609. int var = ci.u16bitAt(index + 2);
  610. if (var < lessThan)
  611. return;
  612. var += delta;
  613. ci.write16bit(var, index + 2);
  614. }
  615. }
  616. private static void shiftIndex8(CodeIterator ci, int index, int opcode,
  617. int lessThan, int delta)
  618. throws BadBytecode
  619. {
  620. int var = ci.byteAt(index + 1);
  621. if (var < lessThan)
  622. return;
  623. var += delta;
  624. if (var < 0x100)
  625. ci.writeByte(var, index + 1);
  626. else {
  627. int pos = ci.insertExGap(2);
  628. ci.writeByte(WIDE, pos - 2);
  629. ci.writeByte(opcode, pos - 1);
  630. ci.write16bit(var, pos);
  631. }
  632. }
  633. private static void shiftIndex0(CodeIterator ci, int index, int opcode,
  634. int lessThan, int delta,
  635. int opcode_i_0, int opcode_i)
  636. throws BadBytecode
  637. {
  638. int var = (opcode - opcode_i_0) % 4;
  639. if (var < lessThan)
  640. return;
  641. var += delta;
  642. if (var < 4)
  643. ci.writeByte(opcode + delta, index);
  644. else {
  645. opcode = (opcode - opcode_i_0) / 4 + opcode_i;
  646. if (var < 0x100) {
  647. int pos = ci.insertExGap(1);
  648. ci.writeByte(opcode, pos - 1);
  649. ci.writeByte(var, pos);
  650. }
  651. else {
  652. int pos = ci.insertExGap(3);
  653. ci.writeByte(WIDE, pos - 1);
  654. ci.writeByte(opcode, pos);
  655. ci.write16bit(var, pos + 1);
  656. }
  657. }
  658. }
  659. }