RegisterFormatter.java revision 00fc68adf2e39aeb9fed35293f2576bbe729ec4b
1/* 2 * [The "BSD licence"] 3 * Copyright (c) 2010 Ben Gruver (JesusFreke) 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 29package org.jf.baksmali.Adaptors; 30 31import org.jf.dexlib.CodeItem; 32import org.jf.dexlib.Util.AccessFlags; 33import org.jf.baksmali.baksmali; 34 35/** 36 * This class contains the logic used for formatting registers 37 */ 38public class RegisterFormatter { 39 40 /** 41 * This method is used (only) by format 3rc (the format that uses a range of regsiters like {v1 .. v10}) to format 42 * it's registers. If both registers are parameter registers, they will be formatted as such, otherwise they will 43 * both be formatted as normal registers 44 * @param codeItem 45 * @param startRegister 46 * @param lastRegister 47 * @return an array of 2 strings containing the formatted registers 48 */ 49 public static String[] formatFormat3rcRegisters(CodeItem codeItem, int startRegister, int lastRegister) { 50 if (!baksmali.noParameterRegisters) { 51 int parameterRegisterCount = codeItem.getParent().method.getPrototype().getParameterRegisterCount() 52 + (((codeItem.getParent().accessFlags & AccessFlags.STATIC.getValue())==0)?1:0); 53 int registerCount = codeItem.getRegisterCount(); 54 55 assert startRegister <= lastRegister; 56 57 if (startRegister >= registerCount - parameterRegisterCount) { 58 return new String[] {"p" + (startRegister - (registerCount - parameterRegisterCount)), 59 "p" + (lastRegister - (registerCount - parameterRegisterCount))}; 60 } 61 } 62 return new String[] {"v" + startRegister, 63 "v" + lastRegister}; 64 } 65 66 /** 67 * Formats a register with the appropriate format - with either the normal v<n> format or the p<n> parameter format. 68 * 69 * It uses the register and parameter information from the give <code>CodeItem</code> to determine if the given 70 * register is a normal or parameter register. 71 * @param codeItem 72 * @param register 73 * @return The formatted register 74 */ 75 public static String formatRegister(CodeItem codeItem, int register) { 76 if (!baksmali.noParameterRegisters) { 77 int parameterRegisterCount = codeItem.getParent().method.getPrototype().getParameterRegisterCount() 78 + (((codeItem.getParent().accessFlags & AccessFlags.STATIC.getValue())==0)?1:0); 79 int registerCount = codeItem.getRegisterCount(); 80 if (register >= registerCount - parameterRegisterCount) { 81 return "p" + (register - (registerCount - parameterRegisterCount)); 82 } 83 } 84 return "v" + register; 85 } 86} 87