1917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul/* 2917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Copyright (C) 2007 The Android Open Source Project 3917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * 4917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Licensed under the Apache License, Version 2.0 (the "License"); 5917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * you may not use this file except in compliance with the License. 6917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * You may obtain a copy of the License at 7917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * 8917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * http://www.apache.org/licenses/LICENSE-2.0 9917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * 10917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Unless required by applicable law or agreed to in writing, software 11917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * distributed under the License is distributed on an "AS IS" BASIS, 12917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * See the License for the specific language governing permissions and 14917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * limitations under the License. 15917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */ 16917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 17917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulpackage com.android.dexgen.dex.code; 18917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 19917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulimport com.android.dexgen.rop.code.RegisterSpecList; 20917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulimport com.android.dexgen.rop.code.SourcePosition; 21917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 22917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul/** 23917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Instruction which has a single branch target. 24917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */ 25917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulpublic final class TargetInsn extends FixedSizeInsn { 26917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** {@code non-null;} the branch target */ 27917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul private CodeAddress target; 28917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 29917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** 30917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Constructs an instance. The output address of this instance is initially 31917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * unknown ({@code -1}), and the target is initially 32917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * {@code null}. 33917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * 34917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * @param opcode the opcode; one of the constants from {@link Dops} 35917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * @param position {@code non-null;} source position 36917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * @param registers {@code non-null;} register list, including a 37917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * result register if appropriate (that is, registers may be either 38917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * ins or outs) 39917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * @param target {@code non-null;} the branch target 40917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */ 41917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul public TargetInsn(Dop opcode, SourcePosition position, 42917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul RegisterSpecList registers, CodeAddress target) { 43917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul super(opcode, position, registers); 44917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 45917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul if (target == null) { 46917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul throw new NullPointerException("target == null"); 47917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 48917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 49917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul this.target = target; 50917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 51917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 52917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** {@inheritDoc} */ 53917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul @Override 54917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul public DalvInsn withOpcode(Dop opcode) { 55917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul return new TargetInsn(opcode, getPosition(), getRegisters(), target); 56917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 57917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 58917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** {@inheritDoc} */ 59917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul @Override 60917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul public DalvInsn withRegisters(RegisterSpecList registers) { 61917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul return new TargetInsn(getOpcode(), getPosition(), registers, target); 62917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 63917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 64917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** 65917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Returns an instance that is just like this one, except that its 66917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * opcode has the opposite sense (as a test; e.g. a 67917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * {@code lt} test becomes a {@code ge}), and its branch 68917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * target is replaced by the one given, and all set-once values 69917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * associated with the class (such as its address) are reset. 70917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * 71917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * @param target {@code non-null;} the new branch target 72917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * @return {@code non-null;} an appropriately-constructed instance 73917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */ 74917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul public TargetInsn withNewTargetAndReversed(CodeAddress target) { 75917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul Dop opcode = getOpcode().getOppositeTest(); 76917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 77917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul return new TargetInsn(opcode, getPosition(), getRegisters(), target); 78917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 79917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 80917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** 81917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Gets the unique branch target of this instruction. 82917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * 83917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * @return {@code non-null;} the branch target 84917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */ 85917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul public CodeAddress getTarget() { 86917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul return target; 87917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 88917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 89917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** 90917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Gets the target address of this instruction. This is only valid 91917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * to call if the target instruction has been assigned an address, 92917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * and it is merely a convenient shorthand for 93917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * {@code getTarget().getAddress()}. 94917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * 95917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * @return {@code >= 0;} the target address 96917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */ 97917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul public int getTargetAddress() { 98917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul return target.getAddress(); 99917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 100917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 101917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** 102917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Gets the branch offset of this instruction. This is only valid to 103917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * call if both this and the target instruction each has been assigned 104917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * an address, and it is merely a convenient shorthand for 105917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * {@code getTargetAddress() - getAddress()}. 106917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * 107917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * @return the branch offset 108917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */ 109917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul public int getTargetOffset() { 110917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul return target.getAddress() - getAddress(); 111917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 112917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 113917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** 114917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Returns whether the target offset is known. 115917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * 116917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * @return {@code true} if the target offset is known or 117917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * {@code false} if not 118917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */ 119917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul public boolean hasTargetOffset() { 120917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul return hasAddress() && target.hasAddress(); 121917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 122917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 123917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** {@inheritDoc} */ 124917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul @Override 125917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul protected String argString() { 126917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul if (target == null) { 127917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul return "????"; 128917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 129917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 130917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul return target.identifierString(); 131917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 132917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul} 133