123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647 |
- /*
- * Javassist, a Java-bytecode translator toolkit.
- * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved.
- *
- * The contents of this file are subject to the Mozilla Public License Version
- * 1.1 (the "License"); you may not use this file except in compliance with
- * the License. Alternatively, the contents of this file may be used under
- * the terms of the GNU Lesser General Public License Version 2.1 or later,
- * or the Apache License Version 2.0.
- *
- * Software distributed under the License is distributed on an "AS IS" basis,
- * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
- * for the specific language governing rights and limitations under the
- * License.
- */
-
- package javassist.bytecode;
-
- import java.util.ArrayList;
- import java.util.List;
-
- /**
- * An iterator for editing a code attribute.
- *
- * <p>To directly read or edit a bytecode sequence, call {@link #byteAt(int)}, {@link #s16bitAt(int)},
- * {@link #writeByte(int, int)}, {@link #write16bit(int, int)}, and other methods.
- * For example, if <code>method</code> refers to a <code>CtMethod</code> object,
- * the following code substitutes the <code>NOP</code> instruction for the first
- * instruction of the method:
- *
- * <pre>
- * CodeAttribute ca = method.getMethodInfo().getCodeAttribute();
- * CodeIterator ci = ca.iterator();
- * ci.writeByte(Opcode.NOP, 0);</pre>
- *
- * <p>To visit every instruction, call {@link #next()} on a <code>CodeIterator</code>.
- * It returns the index of the first byte of the next instruction.
- *
- * <p>If there are multiple <code>CodeIterator</code>s referring to the
- * same <code>Code_attribute</code>, then inserting a gap by one
- * <code>CodeIterator</code> will break the other
- * <code>CodeIterator</code>.
- *
- * <p>This iterator does not provide <code>remove()</code>.
- * If a piece of code in a <code>Code_attribute</code> is unnecessary,
- * it should be overwritten with <code>NOP</code>.
- *
- * @see CodeAttribute#iterator()
- */
- public class CodeIterator implements Opcode {
- protected CodeAttribute codeAttr;
- protected byte[] bytecode;
- protected int endPos;
- protected int currentPos;
- protected int mark, mark2;
-
- protected CodeIterator(CodeAttribute ca) {
- codeAttr = ca;
- bytecode = ca.getCode();
- begin();
- }
-
- /**
- * Moves to the first instruction.
- */
- public void begin() {
- currentPos = mark = mark2 = 0;
- endPos = getCodeLength();
- }
-
- /**
- * Moves to the given index.
- *
- * <p>The index of the next instruction is set to the given index.
- * The successive call to <code>next()</code>
- * returns the index that has been given to <code>move()</code>.
- *
- * <p>Note that the index is into the byte array returned by
- * <code>get().getCode()</code>.
- *
- * @see CodeAttribute#getCode()
- */
- public void move(int index) {
- currentPos = index;
- }
-
- /**
- * Sets a mark to the bytecode at the given index.
- * The mark can be used to track the position of that bytecode
- * when code blocks are inserted.
- * If a code block is inclusively inserted at the position of the
- * bytecode, the mark is set to the inserted code block.
- *
- * @see #getMark()
- * @since 3.11
- */
- public void setMark(int index) {
- mark = index;
- }
-
- /**
- * Sets a mark to the bytecode at the given index.
- * The mark can be used to track the position of that bytecode
- * when code blocks are inserted.
- * If a code block is inclusively inserted at the position of the
- * bytecode, the mark is set to the inserted code block.
- *
- * @see #getMark2()
- * @since 3.26
- */
- public void setMark2(int index) {
- mark2 = index;
- }
-
- /**
- * Gets the index of the position of the mark set by
- * <code>setMark</code>.
- *
- * @return the index of the position.
- * @see #setMark(int)
- * @since 3.11
- */
- public int getMark() { return mark; }
-
- /**
- * Gets the index of the position of the mark set by
- * <code>setMark2</code>.
- *
- * @return the index of the position.
- * @see #setMark2(int)
- * @since 3.26
- */
- public int getMark2() { return mark2; }
-
- /**
- * Returns a Code attribute read with this iterator.
- */
- public CodeAttribute get() {
- return codeAttr;
- }
-
- /**
- * Returns <code>code_length</code> of <code>Code_attribute</code>.
- */
- public int getCodeLength() {
- return bytecode.length;
- }
-
- /**
- * Returns the unsigned 8bit value at the given index.
- */
- public int byteAt(int index) { return bytecode[index] & 0xff; }
-
- /**
- * Returns the signed 8bit value at the given index.
- */
- public int signedByteAt(int index) { return bytecode[index]; }
-
- /**
- * Writes an 8bit value at the given index.
- */
- public void writeByte(int value, int index) {
- bytecode[index] = (byte)value;
- }
-
- /**
- * Returns the unsigned 16bit value at the given index.
- */
- public int u16bitAt(int index) {
- return ByteArray.readU16bit(bytecode, index);
- }
-
- /**
- * Returns the signed 16bit value at the given index.
- */
- public int s16bitAt(int index) {
- return ByteArray.readS16bit(bytecode, index);
- }
-
- /**
- * Writes a 16 bit integer at the index.
- */
- public void write16bit(int value, int index) {
- ByteArray.write16bit(value, bytecode, index);
- }
-
- /**
- * Returns the signed 32bit value at the given index.
- */
- public int s32bitAt(int index) {
- return ByteArray.read32bit(bytecode, index);
- }
-
- /**
- * Writes a 32bit integer at the index.
- */
- public void write32bit(int value, int index) {
- ByteArray.write32bit(value, bytecode, index);
- }
-
- /**
- * Writes a byte array at the index.
- *
- * @param code may be a zero-length array.
- */
- public void write(byte[] code, int index) {
- int len = code.length;
- for (int j = 0; j < len; ++j)
- bytecode[index++] = code[j];
- }
-
- /**
- * Returns true if there is more instructions.
- */
- public boolean hasNext() { return currentPos < endPos; }
-
- /**
- * Returns the index of the next instruction
- * (not the operand following the current opcode).
- *
- * <p>Note that the index is into the byte array returned by
- * <code>get().getCode()</code>.
- *
- * @see CodeAttribute#getCode()
- * @see CodeIterator#byteAt(int)
- */
- public int next() throws BadBytecode {
- int pos = currentPos;
- currentPos = nextOpcode(bytecode, pos);
- return pos;
- }
-
- /**
- * Obtains the value that the next call
- * to <code>next()</code> will return.
- *
- * <p>This method is side-effects free.
- * Successive calls to <code>lookAhead()</code> return the
- * same value until <code>next()</code> is called.
- */
- public int lookAhead() {
- return currentPos;
- }
-
- /**
- * Moves to the instruction for
- * either <code>super()</code> or <code>this()</code>.
- *
- * <p>This method skips all the instructions for computing arguments
- * to <code>super()</code> or <code>this()</code>, which should be
- * placed at the beginning of a constructor body.
- *
- * <p>This method returns the index of INVOKESPECIAL instruction
- * executing <code>super()</code> or <code>this()</code>.
- * A successive call to <code>next()</code> returns the
- * index of the next instruction following that INVOKESPECIAL.
- *
- * <p>This method works only for a constructor.
- *
- * @return the index of the INVOKESPECIAL instruction, or -1
- * if a constructor invocation is not found.
- */
- public int skipConstructor() throws BadBytecode {
- return skipSuperConstructor0(-1);
- }
-
- /**
- * Moves to the instruction for <code>super()</code>.
- *
- * <p>This method skips all the instructions for computing arguments to
- * <code>super()</code>, which should be
- * placed at the beginning of a constructor body.
- *
- * <p>This method returns the index of INVOKESPECIAL instruction
- * executing <code>super()</code>.
- * A successive call to <code>next()</code> returns the
- * index of the next instruction following that INVOKESPECIAL.
- *
- * <p>This method works only for a constructor.
- *
- * @return the index of the INVOKESPECIAL instruction, or -1
- * if a super constructor invocation is not found
- * but <code>this()</code> is found.
- */
- public int skipSuperConstructor() throws BadBytecode {
- return skipSuperConstructor0(0);
- }
-
- /**
- * Moves to the instruction for <code>this()</code>.
- *
- * <p>This method skips all the instructions for computing arguments to
- * <code>this()</code>, which should be
- * placed at the beginning of a constructor body.
- *
- * <p>This method returns the index of INVOKESPECIAL instruction
- * executing <code>this()</code>.
- * A successive call to <code>next()</code> returns the
- * index of the next instruction following that INVOKESPECIAL.
- *
- * <p>This method works only for a constructor.
- *
- * @return the index of the INVOKESPECIAL instruction, or -1
- * if a explicit constructor invocation is not found
- * but <code>super()</code> is found.
- */
- public int skipThisConstructor() throws BadBytecode {
- return skipSuperConstructor0(1);
- }
-
- /* skipSuper 1: this(), 0: super(), -1: both.
- */
- private int skipSuperConstructor0(int skipThis) throws BadBytecode {
- begin();
- ConstPool cp = codeAttr.getConstPool();
- String thisClassName = codeAttr.getDeclaringClass();
- int nested = 0;
- while (hasNext()) {
- int index = next();
- int c = byteAt(index);
- if (c == NEW)
- ++nested;
- else if (c == INVOKESPECIAL) {
- int mref = ByteArray.readU16bit(bytecode, index + 1);
- if (cp.getMethodrefName(mref).equals(MethodInfo.nameInit))
- if (--nested < 0) {
- if (skipThis < 0)
- return index;
-
- String cname = cp.getMethodrefClassName(mref);
- if (cname.equals(thisClassName) == (skipThis > 0))
- return index;
-
- break;
- }
- }
- }
-
- begin();
- return -1;
- }
-
- /**
- * Inserts the given bytecode sequence
- * before the next instruction that would be returned by
- * <code>next()</code> (not before the instruction returned
- * by the last call to <code>next()</code>).
- * Branch offsets and the exception table are also updated.
- *
- * <p>If the next instruction is at the beginning of a block statement,
- * then the bytecode is inserted within that block.
- *
- * <p>An extra gap may be inserted at the end of the inserted
- * bytecode sequence for adjusting alignment if the code attribute
- * includes <code>LOOKUPSWITCH</code> or <code>TABLESWITCH</code>.
- *
- * @param code inserted bytecode sequence.
- * @return the index indicating the first byte of the
- * inserted byte sequence.
- */
- public int insert(byte[] code)
- throws BadBytecode
- {
- return insert0(currentPos, code, false);
- }
-
- /**
- * Inserts the given bytecode sequence
- * before the instruction at the given index <code>pos</code>.
- * Branch offsets and the exception table are also updated.
- *
- * <p>If the instruction at the given index is at the beginning
- * of a block statement,
- * then the bytecode is inserted within that block.
- *
- * <p>An extra gap may be inserted at the end of the inserted
- * bytecode sequence for adjusting alignment if the code attribute
- * includes <code>LOOKUPSWITCH</code> or <code>TABLESWITCH</code>.
- *
- * <p>The index at which the byte sequence is actually inserted
- * might be different from pos since some other bytes might be
- * inserted at other positions (e.g. to change <code>GOTO</code>
- * to <code>GOTO_W</code>).
- *
- * @param pos the index at which a byte sequence is inserted.
- * @param code inserted bytecode sequence.
- */
- public void insert(int pos, byte[] code) throws BadBytecode {
- insert0(pos, code, false);
- }
-
- /**
- * Inserts the given bytecode sequence
- * before the instruction at the given index <code>pos</code>.
- * Branch offsets and the exception table are also updated.
- *
- * <p>If the instruction at the given index is at the beginning
- * of a block statement,
- * then the bytecode is inserted within that block.
- *
- * <p>An extra gap may be inserted at the end of the inserted
- * bytecode sequence for adjusting alignment if the code attribute
- * includes <code>LOOKUPSWITCH</code> or <code>TABLESWITCH</code>.
- *
- * @param pos the index at which a byte sequence is inserted.
- * @param code inserted bytecode sequence.
- * @return the index indicating the first byte of the
- * inserted byte sequence, which might be
- * different from pos.
- * @since 3.11
- */
- public int insertAt(int pos, byte[] code) throws BadBytecode {
- return insert0(pos, code, false);
- }
-
- /**
- * Inserts the given bytecode sequence exclusively
- * before the next instruction that would be returned by
- * <code>next()</code> (not before the instruction returned
- * by tha last call to <code>next()</code>).
- * Branch offsets and the exception table are also updated.
- *
- * <p>If the next instruction is at the beginning of a block statement,
- * then the bytecode is excluded from that block.
- *
- * <p>An extra gap may be inserted at the end of the inserted
- * bytecode sequence for adjusting alignment if the code attribute
- * includes <code>LOOKUPSWITCH</code> or <code>TABLESWITCH</code>.
- *
- * @param code inserted bytecode sequence.
- * @return the index indicating the first byte of the
- * inserted byte sequence.
- */
- public int insertEx(byte[] code)
- throws BadBytecode
- {
- return insert0(currentPos, code, true);
- }
-
- /**
- * Inserts the given bytecode sequence exclusively
- * before the instruction at the given index <code>pos</code>.
- * Branch offsets and the exception table are also updated.
- *
- * <p>If the instruction at the given index is at the beginning
- * of a block statement,
- * then the bytecode is excluded from that block.
- *
- * <p>An extra gap may be inserted at the end of the inserted
- * bytecode sequence for adjusting alignment if the code attribute
- * includes <code>LOOKUPSWITCH</code> or <code>TABLESWITCH</code>.
- *
- * <p>The index at which the byte sequence is actually inserted
- * might be different from pos since some other bytes might be
- * inserted at other positions (e.g. to change <code>GOTO</code>
- * to <code>GOTO_W</code>).
- *
- * @param pos the index at which a byte sequence is inserted.
- * @param code inserted bytecode sequence.
- */
- public void insertEx(int pos, byte[] code) throws BadBytecode {
- insert0(pos, code, true);
- }
-
- /**
- * Inserts the given bytecode sequence exclusively
- * before the instruction at the given index <code>pos</code>.
- * Branch offsets and the exception table are also updated.
- *
- * <p>If the instruction at the given index is at the beginning
- * of a block statement,
- * then the bytecode is excluded from that block.
- *
- * <p>An extra gap may be inserted at the end of the inserted
- * bytecode sequence for adjusting alignment if the code attribute
- * includes <code>LOOKUPSWITCH</code> or <code>TABLESWITCH</code>.
- *
- * @param pos the index at which a byte sequence is inserted.
- * @param code inserted bytecode sequence.
- * @return the index indicating the first byte of the
- * inserted byte sequence, which might be
- * different from pos.
- * @since 3.11
- */
- public int insertExAt(int pos, byte[] code) throws BadBytecode {
- return insert0(pos, code, true);
- }
-
- /**
- * @return the index indicating the first byte of the
- * inserted byte sequence.
- */
- private int insert0(int pos, byte[] code, boolean exclusive)
- throws BadBytecode
- {
- int len = code.length;
- if (len <= 0)
- return pos;
-
- // currentPos will change.
- pos = insertGapAt(pos, len, exclusive).position;
-
- int p = pos;
- for (int j = 0; j < len; ++j)
- bytecode[p++] = code[j];
-
- return pos;
- }
-
- /**
- * Inserts a gap
- * before the next instruction that would be returned by
- * <code>next()</code> (not before the instruction returned
- * by the last call to <code>next()</code>).
- * Branch offsets and the exception table are also updated.
- * The inserted gap is filled with NOP. The gap length may be
- * extended to a multiple of 4.
- *
- * <p>If the next instruction is at the beginning of a block statement,
- * then the gap is inserted within that block.
- *
- * @param length gap length
- * @return the index indicating the first byte of the inserted gap.
- */
- public int insertGap(int length) throws BadBytecode {
- return insertGapAt(currentPos, length, false).position;
- }
-
- /**
- * Inserts a gap in front of the instruction at the given
- * index <code>pos</code>.
- * Branch offsets and the exception table are also updated.
- * The inserted gap is filled with NOP. The gap length may be
- * extended to a multiple of 4.
- *
- * <p>If the instruction at the given index is at the beginning
- * of a block statement,
- * then the gap is inserted within that block.
- *
- * @param pos the index at which a gap is inserted.
- * @param length gap length.
- * @return the length of the inserted gap.
- * It might be bigger than <code>length</code>.
- */
- public int insertGap(int pos, int length) throws BadBytecode {
- return insertGapAt(pos, length, false).length;
- }
-
- /**
- * Inserts an exclusive gap
- * before the next instruction that would be returned by
- * <code>next()</code> (not before the instruction returned
- * by the last call to <code>next()</code>).
- * Branch offsets and the exception table are also updated.
- * The inserted gap is filled with NOP. The gap length may be
- * extended to a multiple of 4.
- *
- * <p>If the next instruction is at the beginning of a block statement,
- * then the gap is excluded from that block.
- *
- * @param length gap length
- * @return the index indicating the first byte of the inserted gap.
- */
- public int insertExGap(int length) throws BadBytecode {
- return insertGapAt(currentPos, length, true).position;
- }
-
- /**
- * Inserts an exclusive gap in front of the instruction at the given
- * index <code>pos</code>.
- * Branch offsets and the exception table are also updated.
- * The inserted gap is filled with NOP. The gap length may be
- * extended to a multiple of 4.
- *
- * <p>If the instruction at the given index is at the beginning
- * of a block statement,
- * then the gap is excluded from that block.
- *
- * @param pos the index at which a gap is inserted.
- * @param length gap length.
- * @return the length of the inserted gap.
- * It might be bigger than <code>length</code>.
- */
- public int insertExGap(int pos, int length) throws BadBytecode {
- return insertGapAt(pos, length, true).length;
- }
-
- /**
- * An inserted gap.
- *
- * @since 3.11
- */
- public static class Gap {
- /**
- * The position of the gap.
- */
- public int position;
-
- /**
- * The length of the gap.
- */
- public int length;
- }
-
- /**
- * Inserts an inclusive or exclusive gap in front of the instruction
- * at the given index <code>pos</code>.
- * Branch offsets and the exception table in the method body
- * are also updated. The inserted gap is filled with NOP.
- * The gap length may be extended to a multiple of 4.
- *
- * <p>Suppose that the instruction at the given index is at the
- * beginning of a block statement. If the gap is inclusive,
- * then it is included within that block. If the gap is exclusive,
- * then it is excluded from that block.
- *
- * <p>The index at which the gap is actually inserted
- * might be different from pos since some other bytes might be
- * inserted at other positions (e.g. to change <code>GOTO</code>
- * to <code>GOTO_W</code>). The index is available from the <code>Gap</code>
- * object returned by this method.
- *
- * <p>Suppose that the gap is inserted at the position of
- * the next instruction that would be returned by
- * <code>next()</code> (not the last instruction returned
- * by the last call to <code>next()</code>). The next
- * instruction returned by <code>next()</code> after the gap is
- * inserted is still the same instruction. It is not <code>NOP</code>
- * at the first byte of the inserted gap.
- *
- * @param pos the index at which a gap is inserted.
- * @param length gap length.
- * @param exclusive true if exclusive, otherwise false.
- * @return the position and the length of the inserted gap.
- * @since 3.11
- */
- public Gap insertGapAt(int pos, int length, boolean exclusive)
- throws BadBytecode
- {
- /**
- * cursorPos indicates the next bytecode whichever exclusive is
- * true or false.
- */
- Gap gap = new Gap();
- if (length <= 0) {
- gap.position = pos;
- gap.length = 0;
- return gap;
- }
-
- byte[] c;
- int length2;
- if (bytecode.length + length > Short.MAX_VALUE) {
- // currentPos might change after calling insertGapCore0w().
- c = insertGapCore0w(bytecode, pos, length, exclusive,
- get().getExceptionTable(), codeAttr, gap);
- pos = gap.position;
- length2 = length; // == gap.length
- }
- else {
- int cur = currentPos;
- c = insertGapCore0(bytecode, pos, length, exclusive,
- get().getExceptionTable(), codeAttr);
- // insertGapCore0() never changes pos.
- length2 = c.length - bytecode.length;
- gap.position = pos;
- gap.length = length2;
- if (cur >= pos)
- currentPos = cur + length2;
-
- if (mark > pos || (mark == pos && exclusive))
- mark += length2;
-
- if (mark2 > pos || (mark2 == pos && exclusive))
- mark2 += length2;
- }
-
- codeAttr.setCode(c);
- bytecode = c;
- endPos = getCodeLength();
- updateCursors(pos, length2);
- return gap;
- }
-
- /**
- * Is called when a gap is inserted. The default implementation is empty.
- * A subclass can override this method so that cursors will be updated.
- *
- * @param pos the position where a gap is inserted.
- * @param length the length of the gap.
- */
- protected void updateCursors(int pos, int length) {
- // empty
- }
-
- /**
- * Copies and inserts the entries in the given exception table
- * at the beginning of the exception table in the code attribute
- * edited by this object.
- *
- * @param offset the value added to the code positions included
- * in the entries.
- */
- public void insert(ExceptionTable et, int offset) {
- codeAttr.getExceptionTable().add(0, et, offset);
- }
-
- /**
- * Appends the given bytecode sequence at the end.
- *
- * @param code the bytecode appended.
- * @return the position of the first byte of the appended bytecode.
- */
- public int append(byte[] code) {
- int size = getCodeLength();
- int len = code.length;
- if (len <= 0)
- return size;
-
- appendGap(len);
- byte[] dest = bytecode;
- for (int i = 0; i < len; ++i)
- dest[i + size] = code[i];
-
- return size;
- }
-
- /**
- * Appends a gap at the end of the bytecode sequence.
- *
- * @param gapLength gap length
- */
- public void appendGap(int gapLength) {
- byte[] code = bytecode;
- int codeLength = code.length;
- byte[] newcode = new byte[codeLength + gapLength];
-
- int i;
- for (i = 0; i < codeLength; ++i)
- newcode[i] = code[i];
-
- for (i = codeLength; i < codeLength + gapLength; ++i)
- newcode[i] = NOP;
-
- codeAttr.setCode(newcode);
- bytecode = newcode;
- endPos = getCodeLength();
- }
-
- /**
- * Copies and appends the entries in the given exception table
- * at the end of the exception table in the code attribute
- * edited by this object.
- *
- * @param offset the value added to the code positions included
- * in the entries.
- */
- public void append(ExceptionTable et, int offset) {
- ExceptionTable table = codeAttr.getExceptionTable();
- table.add(table.size(), et, offset);
- }
-
- /* opcodeLegth is used for implementing nextOpcode().
- */
- private static final int opcodeLength[] = {
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 2, 3,
- 3, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 1,
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 3, 3,
- 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 0, 0, 1, 1, 1, 1, 1, 1, 3, 3,
- 3, 3, 3, 3, 3, 5, 5, 3, 2, 3, 1, 1, 3, 3, 1, 1, 0, 4, 3, 3,
- 5, 5
- };
- // 0 .. LOOKUPSWITCH, TABLESWITCH, WIDE
-
- /**
- * Calculates the index of the next opcode.
- */
- static int nextOpcode(byte[] code, int index)
- throws BadBytecode
- {
- int opcode;
- try {
- opcode = code[index] & 0xff;
- }
- catch (IndexOutOfBoundsException e) {
- throw new BadBytecode("invalid opcode address");
- }
-
- try {
- int len = opcodeLength[opcode];
- if (len > 0)
- return index + len;
- else if (opcode == WIDE)
- if (code[index + 1] == (byte)IINC) // WIDE IINC
- return index + 6;
- else
- return index + 4; // WIDE ...
- int index2 = (index & ~3) + 8;
- if (opcode == LOOKUPSWITCH) {
- int npairs = ByteArray.read32bit(code, index2);
- return index2 + npairs * 8 + 4;
- }
- else if (opcode == TABLESWITCH) {
- int low = ByteArray.read32bit(code, index2);
- int high = ByteArray.read32bit(code, index2 + 4);
- return index2 + (high - low + 1) * 4 + 8;
- }
- }
- catch (IndexOutOfBoundsException e) {
- }
-
- // opcode is UNUSED or an IndexOutOfBoundsException was thrown.
- throw new BadBytecode(opcode);
- }
-
- // methods for implementing insertGap().
-
- static class AlignmentException extends Exception {
-
- /** default serialVersionUID */
- private static final long serialVersionUID = 1L;}
-
- /**
- * insertGapCore0() inserts a gap (some NOPs).
- * It cannot handle a long code sequence more than 32K. All branch offsets must be
- * signed 16bits.
- *
- * If "where" is the beginning of a block statement and exclusive is false,
- * then the inserted gap is also included in the block statement.
- * "where" must indicate the first byte of an opcode.
- * The inserted gap is filled with NOP. gapLength may be extended to
- * a multiple of 4.
- *
- * This method was also called from CodeAttribute.LdcEntry.doit().
- *
- * @param where It must indicate the first byte of an opcode.
- */
- static byte[] insertGapCore0(byte[] code, int where, int gapLength,
- boolean exclusive, ExceptionTable etable, CodeAttribute ca)
- throws BadBytecode
- {
- if (gapLength <= 0)
- return code;
-
- try {
- return insertGapCore1(code, where, gapLength, exclusive, etable, ca);
- }
- catch (AlignmentException e) {
- try {
- return insertGapCore1(code, where, (gapLength + 3) & ~3,
- exclusive, etable, ca);
- }
- catch (AlignmentException e2) {
- throw new RuntimeException("fatal error?");
- }
- }
- }
-
- private static byte[] insertGapCore1(byte[] code, int where, int gapLength,
- boolean exclusive, ExceptionTable etable,
- CodeAttribute ca)
- throws BadBytecode, AlignmentException
- {
- int codeLength = code.length;
- byte[] newcode = new byte[codeLength + gapLength];
- insertGap2(code, where, gapLength, codeLength, newcode, exclusive);
- etable.shiftPc(where, gapLength, exclusive);
- LineNumberAttribute na
- = (LineNumberAttribute)ca.getAttribute(LineNumberAttribute.tag);
- if (na != null)
- na.shiftPc(where, gapLength, exclusive);
-
- LocalVariableAttribute va = (LocalVariableAttribute)ca.getAttribute(
- LocalVariableAttribute.tag);
- if (va != null)
- va.shiftPc(where, gapLength, exclusive);
-
- LocalVariableAttribute vta
- = (LocalVariableAttribute)ca.getAttribute(
- LocalVariableAttribute.typeTag);
- if (vta != null)
- vta.shiftPc(where, gapLength, exclusive);
-
- StackMapTable smt = (StackMapTable)ca.getAttribute(StackMapTable.tag);
- if (smt != null)
- smt.shiftPc(where, gapLength, exclusive);
-
- StackMap sm = (StackMap)ca.getAttribute(StackMap.tag);
- if (sm != null)
- sm.shiftPc(where, gapLength, exclusive);
-
- return newcode;
- }
-
- private static void insertGap2(byte[] code, int where, int gapLength,
- int endPos, byte[] newcode, boolean exclusive)
- throws BadBytecode, AlignmentException
- {
- int nextPos;
- int i = 0;
- int j = 0;
- for (; i < endPos; i = nextPos) {
- if (i == where) {
- int j2 = j + gapLength;
- while (j < j2)
- newcode[j++] = NOP;
- }
-
- nextPos = nextOpcode(code, i);
- int inst = code[i] & 0xff;
- // if<cond>, if_icmp<cond>, if_acmp<cond>, goto, jsr
- if ((153 <= inst && inst <= 168)
- || inst == IFNULL || inst == IFNONNULL) {
- /* 2bytes *signed* offset */
- int offset = (code[i + 1] << 8) | (code[i + 2] & 0xff);
- offset = newOffset(i, offset, where, gapLength, exclusive);
- newcode[j] = code[i];
- ByteArray.write16bit(offset, newcode, j + 1);
- j += 3;
- }
- else if (inst == GOTO_W || inst == JSR_W) {
- /* 4bytes offset */
- int offset = ByteArray.read32bit(code, i + 1);
- offset = newOffset(i, offset, where, gapLength, exclusive);
- newcode[j++] = code[i];
- ByteArray.write32bit(offset, newcode, j);
- j += 4;
- }
- else if (inst == TABLESWITCH) {
- if (i != j && (gapLength & 3) != 0)
- throw new AlignmentException();
-
- int i2 = (i & ~3) + 4; // 0-3 byte padding
- // IBM JVM 1.4.2 cannot run the following code:
- // int i0 = i;
- // while (i0 < i2)
- // newcode[j++] = code[i0++];
- // So extracting this code into an external method.
- // see JIRA JASSIST-74.
- j = copyGapBytes(newcode, j, code, i, i2);
-
- int defaultbyte = newOffset(i, ByteArray.read32bit(code, i2),
- where, gapLength, exclusive);
- ByteArray.write32bit(defaultbyte, newcode, j);
- int lowbyte = ByteArray.read32bit(code, i2 + 4);
- ByteArray.write32bit(lowbyte, newcode, j + 4);
- int highbyte = ByteArray.read32bit(code, i2 + 8);
- ByteArray.write32bit(highbyte, newcode, j + 8);
- j += 12;
- int i0 = i2 + 12;
- i2 = i0 + (highbyte - lowbyte + 1) * 4;
- while (i0 < i2) {
- int offset = newOffset(i, ByteArray.read32bit(code, i0),
- where, gapLength, exclusive);
- ByteArray.write32bit(offset, newcode, j);
- j += 4;
- i0 += 4;
- }
- }
- else if (inst == LOOKUPSWITCH) {
- if (i != j && (gapLength & 3) != 0)
- throw new AlignmentException();
-
- int i2 = (i & ~3) + 4; // 0-3 byte padding
-
- // IBM JVM 1.4.2 cannot run the following code:
- // int i0 = i;
- // while (i0 < i2)
- // newcode[j++] = code[i0++];
- // So extracting this code into an external method.
- // see JIRA JASSIST-74.
- j = copyGapBytes(newcode, j, code, i, i2);
-
- int defaultbyte = newOffset(i, ByteArray.read32bit(code, i2),
- where, gapLength, exclusive);
- ByteArray.write32bit(defaultbyte, newcode, j);
- int npairs = ByteArray.read32bit(code, i2 + 4);
- ByteArray.write32bit(npairs, newcode, j + 4);
- j += 8;
- int i0 = i2 + 8;
- i2 = i0 + npairs * 8;
- while (i0 < i2) {
- ByteArray.copy32bit(code, i0, newcode, j);
- int offset = newOffset(i,
- ByteArray.read32bit(code, i0 + 4),
- where, gapLength, exclusive);
- ByteArray.write32bit(offset, newcode, j + 4);
- j += 8;
- i0 += 8;
- }
- }
- else
- while (i < nextPos)
- newcode[j++] = code[i++];
- }
- }
-
-
- private static int copyGapBytes(byte[] newcode, int j, byte[] code, int i, int iEnd) {
- switch (iEnd - i) {
- case 4:
- newcode[j++] = code[i++];
- case 3:
- newcode[j++] = code[i++];
- case 2:
- newcode[j++] = code[i++];
- case 1:
- newcode[j++] = code[i++];
- default:
- }
-
- return j;
- }
-
- private static int newOffset(int i, int offset, int where,
- int gapLength, boolean exclusive) {
- int target = i + offset;
- if (i < where) {
- if (where < target || (exclusive && where == target))
- offset += gapLength;
- }
- else if (i == where) {
- // This code is different from the code in Branch#shiftOffset().
- // see JASSIST-124.
- if (target < where)
- offset -= gapLength;
- }
- else
- if (target < where || (!exclusive && where == target))
- offset -= gapLength;
-
- return offset;
- }
-
- static class Pointers {
- int cursor;
- int mark0, mark, mark2;
- ExceptionTable etable;
- LineNumberAttribute line;
- LocalVariableAttribute vars, types;
- StackMapTable stack;
- StackMap stack2;
-
- Pointers(int cur, int m, int m2, int m0, ExceptionTable et, CodeAttribute ca) {
- cursor = cur;
- mark = m;
- mark2 = m2;
- mark0 = m0;
- etable = et; // non null
- line = (LineNumberAttribute)ca.getAttribute(LineNumberAttribute.tag);
- vars = (LocalVariableAttribute)ca.getAttribute(LocalVariableAttribute.tag);
- types = (LocalVariableAttribute)ca.getAttribute(LocalVariableAttribute.typeTag);
- stack = (StackMapTable)ca.getAttribute(StackMapTable.tag);
- stack2 = (StackMap)ca.getAttribute(StackMap.tag);
- }
-
- void shiftPc(int where, int gapLength, boolean exclusive) throws BadBytecode {
- if (where < cursor || (where == cursor && exclusive))
- cursor += gapLength;
-
- if (where < mark || (where == mark && exclusive))
- mark += gapLength;
-
- if (where < mark2 || (where == mark2 && exclusive))
- mark2 += gapLength;
-
- if (where < mark0 || (where == mark0 && exclusive))
- mark0 += gapLength;
-
- etable.shiftPc(where, gapLength, exclusive);
- if (line != null)
- line.shiftPc(where, gapLength, exclusive);
-
- if (vars != null)
- vars.shiftPc(where, gapLength, exclusive);
-
- if (types != null)
- types.shiftPc(where, gapLength, exclusive);
-
- if (stack != null)
- stack.shiftPc(where, gapLength, exclusive);
-
- if (stack2 != null)
- stack2.shiftPc(where, gapLength, exclusive);
- }
-
- void shiftForSwitch(int where, int gapLength) throws BadBytecode {
- if (stack != null)
- stack.shiftForSwitch(where, gapLength);
-
- if (stack2 != null)
- stack2.shiftForSwitch(where, gapLength);
- }
- }
-
- /*
- * This method is called from CodeAttribute.LdcEntry.doit().
- */
- static byte[] changeLdcToLdcW(byte[] code, ExceptionTable etable,
- CodeAttribute ca, CodeAttribute.LdcEntry ldcs)
- throws BadBytecode
- {
- Pointers pointers = new Pointers(0, 0, 0, 0, etable, ca);
- List<Branch> jumps = makeJumpList(code, code.length, pointers);
- while (ldcs != null) {
- addLdcW(ldcs, jumps);
- ldcs = ldcs.next;
- }
-
- byte[] r = insertGap2w(code, 0, 0, false, jumps, pointers);
- return r;
- }
-
- private static void addLdcW(CodeAttribute.LdcEntry ldcs, List<Branch> jumps) {
- int where = ldcs.where;
- LdcW ldcw = new LdcW(where, ldcs.index);
- int s = jumps.size();
- for (int i = 0; i < s; i++)
- if (where < jumps.get(i).orgPos) {
- jumps.add(i, ldcw);
- return;
- }
-
- jumps.add(ldcw);
- }
-
- /*
- * insertGapCore0w() can handle a long code sequence more than 32K.
- * It guarantees that the length of the inserted gap (NOPs) is equal to
- * gapLength. No other NOPs except some NOPs following TABLESWITCH or
- * LOOKUPSWITCH will not be inserted.
- *
- * Note: currentPos might be moved.
- *
- * @param where It must indicate the first byte of an opcode.
- * @param newWhere It contains the updated index of the position where a gap
- * is inserted and the length of the gap.
- * It must not be null.
- */
- private byte[] insertGapCore0w(byte[] code, int where, int gapLength, boolean exclusive,
- ExceptionTable etable, CodeAttribute ca, Gap newWhere)
- throws BadBytecode
- {
- if (gapLength <= 0)
- return code;
-
- Pointers pointers = new Pointers(currentPos, mark, mark2, where, etable, ca);
- List<Branch> jumps = makeJumpList(code, code.length, pointers);
- byte[] r = insertGap2w(code, where, gapLength, exclusive, jumps, pointers);
- currentPos = pointers.cursor;
- mark = pointers.mark;
- mark2 = pointers.mark2;
- int where2 = pointers.mark0;
- if (where2 == currentPos && !exclusive)
- currentPos += gapLength;
-
- if (exclusive)
- where2 -= gapLength;
-
- newWhere.position = where2;
- newWhere.length = gapLength;
- return r;
- }
-
- private static byte[] insertGap2w(byte[] code, int where, int gapLength,
- boolean exclusive, List<Branch> jumps, Pointers ptrs)
- throws BadBytecode
- {
- if (gapLength > 0) {
- ptrs.shiftPc(where, gapLength, exclusive);
- for (Branch b:jumps)
- b.shift(where, gapLength, exclusive);
- }
-
- boolean unstable = true;
- do {
- while (unstable) {
- unstable = false;
- for (Branch b:jumps) {
- if (b.expanded()) {
- unstable = true;
- int p = b.pos;
- int delta = b.deltaSize();
- ptrs.shiftPc(p, delta, false);
- for (Branch bb:jumps)
- bb.shift(p, delta, false);
- }
- }
- }
-
- for (Branch b:jumps) {
- int diff = b.gapChanged();
- if (diff > 0) {
- unstable = true;
- int p = b.pos;
- ptrs.shiftPc(p, diff, false);
- for (Branch bb:jumps)
- bb.shift(p, diff, false);
- }
- }
- } while (unstable);
-
- return makeExapndedCode(code, jumps, where, gapLength);
- }
-
- private static List<Branch> makeJumpList(byte[] code, int endPos, Pointers ptrs)
- throws BadBytecode
- {
- List<Branch> jumps = new ArrayList<Branch>();
- int nextPos;
- for (int i = 0; i < endPos; i = nextPos) {
- nextPos = nextOpcode(code, i);
- int inst = code[i] & 0xff;
- // if<cond>, if_icmp<cond>, if_acmp<cond>, goto, jsr
- if ((153 <= inst && inst <= 168)
- || inst == IFNULL || inst == IFNONNULL) {
- /* 2bytes *signed* offset */
- int offset = (code[i + 1] << 8) | (code[i + 2] & 0xff);
- Branch b;
- if (inst == GOTO || inst == JSR)
- b = new Jump16(i, offset);
- else
- b = new If16(i, offset);
-
- jumps.add(b);
- }
- else if (inst == GOTO_W || inst == JSR_W) {
- /* 4bytes offset */
- int offset = ByteArray.read32bit(code, i + 1);
- jumps.add(new Jump32(i, offset));
- }
- else if (inst == TABLESWITCH) {
- int i2 = (i & ~3) + 4; // 0-3 byte padding
- int defaultbyte = ByteArray.read32bit(code, i2);
- int lowbyte = ByteArray.read32bit(code, i2 + 4);
- int highbyte = ByteArray.read32bit(code, i2 + 8);
- int i0 = i2 + 12;
- int size = highbyte - lowbyte + 1;
- int[] offsets = new int[size];
- for (int j = 0; j < size; j++) {
- offsets[j] = ByteArray.read32bit(code, i0);
- i0 += 4;
- }
-
- jumps.add(new Table(i, defaultbyte, lowbyte, highbyte, offsets, ptrs));
- }
- else if (inst == LOOKUPSWITCH) {
- int i2 = (i & ~3) + 4; // 0-3 byte padding
- int defaultbyte = ByteArray.read32bit(code, i2);
- int npairs = ByteArray.read32bit(code, i2 + 4);
- int i0 = i2 + 8;
- int[] matches = new int[npairs];
- int[] offsets = new int[npairs];
- for (int j = 0; j < npairs; j++) {
- matches[j] = ByteArray.read32bit(code, i0);
- offsets[j] = ByteArray.read32bit(code, i0 + 4);
- i0 += 8;
- }
-
- jumps.add(new Lookup(i, defaultbyte, matches, offsets, ptrs));
- }
- }
-
- return jumps;
- }
-
- private static byte[] makeExapndedCode(byte[] code, List<Branch> jumps,
- int where, int gapLength)
- throws BadBytecode
- {
- int n = jumps.size();
- int size = code.length + gapLength;
- for (Branch b:jumps)
- 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 = 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 = jumps.get(bindex);
- bpos = b.orgPos;
- }
- else {
- b = null;
- bpos = len;
- }
- }
- }
-
- return newcode;
- }
-
- static abstract class Branch {
- int pos, orgPos;
- Branch(int p) { pos = orgPos = p; }
- void shift(int where, int gapLength, boolean exclusive) {
- if (where < pos || (where == pos && exclusive))
- pos += gapLength;
- }
-
- static int shiftOffset(int i, int offset, int where,
- int gapLength, boolean exclusive) {
- int target = i + offset;
- if (i < where) {
- if (where < target || (exclusive && where == target))
- offset += gapLength;
- }
- else if (i == where) {
- // This code is different from the code in CodeIterator#newOffset().
- // see JASSIST-124.
- if (target < where && exclusive)
- offset -= gapLength;
- else if (where < target && !exclusive)
- offset += gapLength;
- }
- else
- if (target < where || (!exclusive && where == target))
- offset -= gapLength;
-
- return offset;
- }
-
- boolean expanded() { return false; }
- int gapChanged() { return 0; }
- int deltaSize() { return 0; } // newSize - oldSize
-
- // This returns the original instruction size.
- abstract int write(int srcPos, byte[] code, int destPos, byte[] newcode) throws BadBytecode;
- }
-
- /* used by changeLdcToLdcW() and CodeAttribute.LdcEntry.
- */
- static class LdcW extends Branch {
- int index;
- boolean state;
- LdcW(int p, int i) {
- super(p);
- index = i;
- state = true;
- }
-
- @Override
- boolean expanded() {
- if (state) {
- state = false;
- return true;
- }
- return false;
- }
-
- @Override
- int deltaSize() { return 1; }
-
- @Override
- int write(int srcPos, byte[] code, int destPos, byte[] newcode) {
- newcode[destPos] = LDC_W;
- ByteArray.write16bit(index, newcode, destPos + 1);
- return 2;
- }
- }
-
- static abstract class Branch16 extends Branch {
- int offset;
- int state;
- static final int BIT16 = 0;
- static final int EXPAND = 1;
- static final int BIT32 = 2;
-
- Branch16(int p, int off) {
- super(p);
- offset = off;
- state = BIT16;
- }
-
- @Override
- void shift(int where, int gapLength, boolean exclusive) {
- offset = shiftOffset(pos, offset, where, gapLength, exclusive);
- super.shift(where, gapLength, exclusive);
- if (state == BIT16)
- if (offset < Short.MIN_VALUE || Short.MAX_VALUE < offset)
- state = EXPAND;
- }
-
- @Override
- boolean expanded() {
- if (state == EXPAND) {
- state = BIT32;
- return true;
- }
- return false;
- }
-
- @Override
- abstract int deltaSize();
- abstract void write32(int src, byte[] code, int dest, byte[] newcode);
-
- @Override
- int write(int src, byte[] code, int dest, byte[] newcode) {
- if (state == BIT32)
- write32(src, code, dest, newcode);
- else {
- newcode[dest] = code[src];
- ByteArray.write16bit(offset, newcode, dest + 1);
- }
-
- return 3;
- }
- }
-
- // GOTO or JSR
- static class Jump16 extends Branch16 {
- Jump16(int p, int off) {
- super(p, off);
- }
-
- @Override
- int deltaSize() {
- return state == BIT32 ? 2 : 0;
- }
-
- @Override
- void write32(int src, byte[] code, int dest, byte[] newcode) {
- newcode[dest] = (byte)(((code[src] & 0xff) == GOTO) ? GOTO_W : JSR_W);
- ByteArray.write32bit(offset, newcode, dest + 1);
- }
- }
-
- // if<cond>, if_icmp<cond>, or if_acmp<cond>
- static class If16 extends Branch16 {
- If16(int p, int off) {
- super(p, off);
- }
-
- @Override
- int deltaSize() {
- return state == BIT32 ? 5 : 0;
- }
-
- @Override
- void write32(int src, byte[] code, int dest, byte[] newcode) {
- newcode[dest] = (byte)opcode(code[src] & 0xff);
- newcode[dest + 1] = 0;
- newcode[dest + 2] = 8; // branch_offset = 8
- newcode[dest + 3] = (byte)GOTO_W;
- ByteArray.write32bit(offset - 3, newcode, dest + 4);
- }
-
- int opcode(int op) {
- if (op == IFNULL)
- return IFNONNULL;
- else if (op == IFNONNULL)
- return IFNULL;
- if (((op - IFEQ) & 1) == 0)
- return op + 1;
- return op - 1;
- }
- }
-
- static class Jump32 extends Branch {
- int offset;
-
- Jump32(int p, int off) {
- super(p);
- offset = off;
- }
-
- @Override
- void shift(int where, int gapLength, boolean exclusive) {
- offset = shiftOffset(pos, offset, where, gapLength, exclusive);
- super.shift(where, gapLength, exclusive);
- }
-
- @Override
- int write(int src, byte[] code, int dest, byte[] newcode) {
- newcode[dest] = code[src];
- ByteArray.write32bit(offset, newcode, dest + 1);
- return 5;
- }
- }
-
- static abstract class Switcher extends Branch {
- int gap, defaultByte;
- int[] offsets;
- Pointers pointers;
-
- Switcher(int pos, int defaultByte, int[] offsets, Pointers ptrs) {
- super(pos);
- this.gap = 3 - (pos & 3);
- this.defaultByte = defaultByte;
- this.offsets = offsets;
- this.pointers = ptrs;
- }
-
- @Override
- void shift(int where, int gapLength, boolean exclusive) {
- int p = pos;
- defaultByte = shiftOffset(p, defaultByte, where, gapLength, exclusive);
- int num = offsets.length;
- for (int i = 0; i < num; i++)
- offsets[i] = shiftOffset(p, offsets[i], where, gapLength, exclusive);
-
- super.shift(where, gapLength, exclusive);
- }
-
- @Override
- int gapChanged() {
- int newGap = 3 - (pos & 3);
- if (newGap > gap) {
- int diff = newGap - gap;
- gap = newGap;
- return diff;
- }
-
- return 0;
- }
-
- @Override
- int deltaSize() {
- return gap - (3 - (orgPos & 3));
- }
-
- @Override
- int write(int src, byte[] code, int dest, byte[] newcode) throws BadBytecode {
- int padding = 3 - (pos & 3);
- int nops = gap - padding;
- int bytecodeSize = 5 + (3 - (orgPos & 3)) + tableSize();
- if (nops > 0)
- adjustOffsets(bytecodeSize, nops);
-
- newcode[dest++] = code[src];
- while (padding-- > 0)
- newcode[dest++] = 0;
-
- ByteArray.write32bit(defaultByte, newcode, dest);
- int size = write2(dest + 4, newcode);
- dest += size + 4;
- while (nops-- > 0)
- newcode[dest++] = NOP;
-
- return 5 + (3 - (orgPos & 3)) + size;
- }
-
- abstract int write2(int dest, byte[] newcode);
- abstract int tableSize();
-
- /* If the new bytecode size is shorter than the original, some NOPs
- * are appended after this branch instruction (tableswitch or
- * lookupswitch) to fill the gap.
- * This method changes a branch offset to point to the first NOP
- * if the offset originally points to the bytecode next to this
- * branch instruction. Otherwise, the bytecode would contain
- * dead code. It complicates the generation of StackMap and
- * StackMapTable.
- */
- void adjustOffsets(int size, int nops) throws BadBytecode {
- pointers.shiftForSwitch(pos + size, nops);
- if (defaultByte == size)
- defaultByte -= nops;
-
- for (int i = 0; i < offsets.length; i++)
- if (offsets[i] == size)
- offsets[i] -= nops;
- }
- }
-
- static class Table extends Switcher {
- int low, high;
-
- Table(int pos, int defaultByte, int low, int high, int[] offsets, Pointers ptrs) {
- super(pos, defaultByte, offsets, ptrs);
- this.low = low;
- this.high = high;
- }
-
- @Override
- int write2(int dest, byte[] newcode) {
- ByteArray.write32bit(low, newcode, dest);
- ByteArray.write32bit(high, newcode, dest + 4);
- int n = offsets.length;
- dest += 8;
- for (int i = 0; i < n; i++) {
- ByteArray.write32bit(offsets[i], newcode, dest);
- dest += 4;
- }
-
- return 8 + 4 * n;
- }
-
- @Override
- int tableSize() { return 8 + 4 * offsets.length; }
- }
-
- static class Lookup extends Switcher {
- int[] matches;
-
- Lookup(int pos, int defaultByte, int[] matches, int[] offsets, Pointers ptrs) {
- super(pos, defaultByte, offsets, ptrs);
- this.matches = matches;
- }
-
- @Override
- int write2(int dest, byte[] newcode) {
- int n = matches.length;
- ByteArray.write32bit(n, newcode, dest);
- dest += 4;
- for (int i = 0; i < n; i++) {
- ByteArray.write32bit(matches[i], newcode, dest);
- ByteArray.write32bit(offsets[i], newcode, dest + 4);
- dest += 8;
- }
-
- return 4 + 8 * n;
- }
-
- @Override
- int tableSize() { return 4 + 8 * matches.length; }
- }
- }
|