1 /* 2 * [The "BSD licence"] 3 * Copyright (c) 2010 Ben Gruver 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 package org.jf.baksmali.Adaptors; 30 31 import org.jf.util.IndentingWriter; 32 import org.jf.baksmali.baksmali; 33 import org.jf.baksmali.main; 34 import org.jf.dexlib.ClassDataItem; 35 import org.jf.dexlib.Code.Analysis.AnalyzedInstruction; 36 import org.jf.dexlib.Code.Analysis.MethodAnalyzer; 37 import org.jf.dexlib.Code.Analysis.RegisterType; 38 39 import java.io.IOException; 40 import java.util.BitSet; 41 42 public class PostInstructionRegisterInfoMethodItem extends MethodItem { 43 private final AnalyzedInstruction analyzedInstruction; 44 private final MethodAnalyzer methodAnalyzer; 45 46 public PostInstructionRegisterInfoMethodItem(AnalyzedInstruction analyzedInstruction, MethodAnalyzer methodAnalyzer, 47 int codeAddress) { 48 super(codeAddress); 49 this.analyzedInstruction = analyzedInstruction; 50 this.methodAnalyzer = methodAnalyzer; 51 } 52 53 @Override 54 public double getSortOrder() { 55 return 100.1; 56 } 57 58 @Override 59 public boolean writeTo(IndentingWriter writer) throws IOException { 60 int registerInfo = baksmali.registerInfo; 61 int registerCount = analyzedInstruction.getRegisterCount(); 62 BitSet registers = new BitSet(registerCount); 63 64 if ((registerInfo & main.ALL) != 0) { 65 registers.set(0, registerCount); 66 } else { 67 if ((registerInfo & main.ALLPOST) != 0) { 68 registers.set(0, registerCount); 69 } else if ((registerInfo & main.DEST) != 0) { 70 addDestRegs(registers, registerCount); 71 } 72 } 73 74 return writeRegisterInfo(writer, registers); 75 } 76 77 private void addDestRegs(BitSet printPostRegister, int registerCount) { 78 for (int registerNum=0; registerNum<registerCount; registerNum++) { 79 if (analyzedInstruction.getPreInstructionRegisterType(registerNum) != 80 analyzedInstruction.getPostInstructionRegisterType(registerNum)) { 81 printPostRegister.set(registerNum); 82 } 83 } 84 } 85 86 private boolean writeRegisterInfo(IndentingWriter writer, BitSet registers) throws IOException { 87 ClassDataItem.EncodedMethod encodedMethod = methodAnalyzer.getMethod(); 88 89 int registerNum = registers.nextSetBit(0); 90 if (registerNum < 0) { 91 return false; 92 } 93 94 writer.write('#'); 95 for (; registerNum >= 0; registerNum = registers.nextSetBit(registerNum + 1)) { 96 97 RegisterType registerType = analyzedInstruction.getPostInstructionRegisterType(registerNum); 98 99 RegisterFormatter.writeTo(writer, encodedMethod.codeItem, registerNum); 100 writer.write('='); 101 102 if (registerType == null) { 103 writer.write("null"); 104 } else { 105 registerType.writeTo(writer); 106 } 107 writer.write(';'); 108 } 109 return true; 110 } 111 } 112