RegisterFormatter.java revision 4b72225e9d81201838f387171a68a832486903f9
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.util.IndentingWriter; 32import org.jf.baksmali.baksmali; 33import org.jf.dexlib.CodeItem; 34import org.jf.dexlib.Util.AccessFlags; 35 36import java.io.IOException; 37 38/** 39 * This class contains the logic used for formatting registers 40 */ 41public class RegisterFormatter { 42 43 /** 44 * Write out the register range value used by Format3rc. If baksmali.noParameterRegisters is true, it will always 45 * output the registers in the v<n> format. But if false, then it will check if *both* registers are parameter 46 * registers, and if so, use the p<n> format for both. If only the last register is a parameter register, it will 47 * use the v<n> format for both, otherwise it would be confusing to have something like {v20 .. p1} 48 * @param writer the <code>IndentingWriter</code> to write to 49 * @param codeItem the <code>CodeItem</code> that the register is from 50 * @param startRegister the first register in the range 51 * @param lastRegister the last register in the range 52 */ 53 public static void writeRegisterRange(IndentingWriter writer, CodeItem codeItem, int startRegister, 54 int lastRegister) throws IOException { 55 assert lastRegister >= startRegister; 56 57 if (!baksmali.noParameterRegisters) { 58 int parameterRegisterCount = codeItem.getParent().method.getPrototype().getParameterRegisterCount() 59 + (((codeItem.getParent().accessFlags & AccessFlags.STATIC.getValue())==0)?1:0); 60 int registerCount = codeItem.getRegisterCount(); 61 62 assert startRegister <= lastRegister; 63 64 if (startRegister >= registerCount - parameterRegisterCount) { 65 writer.write("{p"); 66 writer.printIntAsDec(startRegister - (registerCount - parameterRegisterCount)); 67 writer.write(" .. p"); 68 writer.printIntAsDec(lastRegister - (registerCount - parameterRegisterCount)); 69 writer.write('}'); 70 return; 71 } 72 } 73 writer.write("{v"); 74 writer.printIntAsDec(startRegister); 75 writer.write(" .. v"); 76 writer.printIntAsDec(lastRegister); 77 writer.write('}'); 78 } 79 80 /** 81 * Writes a register with the appropriate format. If baksmali.noParameterRegisters is true, then it will always 82 * output a register in the v<n> format. If false, then it determines if the register is a parameter register, 83 * and if so, formats it in the p<n> format instead. 84 * 85 * @param writer the <code>IndentingWriter</code> to write to 86 * @param codeItem the <code>CodeItem</code> that the register is from 87 * @param register the register number 88 */ 89 public static void writeTo(IndentingWriter writer, CodeItem codeItem, int register) throws IOException { 90 if (!baksmali.noParameterRegisters) { 91 int parameterRegisterCount = codeItem.getParent().method.getPrototype().getParameterRegisterCount() 92 + (((codeItem.getParent().accessFlags & AccessFlags.STATIC.getValue())==0)?1:0); 93 int registerCount = codeItem.getRegisterCount(); 94 if (register >= registerCount - parameterRegisterCount) { 95 writer.write('p'); 96 writer.printIntAsDec((register - (registerCount - parameterRegisterCount))); 97 return; 98 } 99 } 100 writer.write('v'); 101 writer.printIntAsDec(register); 102 } 103} 104