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 29 package org.jf.baksmali.Adaptors; 30 31 import org.jf.baksmali.BaksmaliOptions; 32 import org.jf.util.IndentingWriter; 33 34 import javax.annotation.Nonnull; 35 import java.io.IOException; 36 37 /** 38 * This class contains the logic used for formatting registers 39 */ 40 public class RegisterFormatter { 41 @Nonnull public final BaksmaliOptions options; 42 public final int registerCount; 43 public final int parameterRegisterCount; 44 RegisterFormatter(@onnull BaksmaliOptions options, int registerCount, int parameterRegisterCount)45 public RegisterFormatter(@Nonnull BaksmaliOptions options, int registerCount, int parameterRegisterCount) { 46 this.options = options; 47 this.registerCount = registerCount; 48 this.parameterRegisterCount = parameterRegisterCount; 49 } 50 51 /** 52 * Write out the register range value used by Format3rc. If baksmali.noParameterRegisters is true, it will always 53 * output the registers in the v<n> format. But if false, then it will check if *both* registers are parameter 54 * registers, and if so, use the p<n> format for both. If only the last register is a parameter register, it will 55 * use the v<n> format for both, otherwise it would be confusing to have something like {v20 .. p1} 56 * @param writer the <code>IndentingWriter</code> to write to 57 * @param startRegister the first register in the range 58 * @param lastRegister the last register in the range 59 */ writeRegisterRange(IndentingWriter writer, int startRegister, int lastRegister)60 public void writeRegisterRange(IndentingWriter writer, int startRegister, int lastRegister) throws IOException { 61 if (options.parameterRegisters) { 62 assert startRegister <= lastRegister; 63 64 if (startRegister >= registerCount - parameterRegisterCount) { 65 writer.write("{p"); 66 writer.printSignedIntAsDec(startRegister - (registerCount - parameterRegisterCount)); 67 writer.write(" .. p"); 68 writer.printSignedIntAsDec(lastRegister - (registerCount - parameterRegisterCount)); 69 writer.write('}'); 70 return; 71 } 72 } 73 writer.write("{v"); 74 writer.printSignedIntAsDec(startRegister); 75 writer.write(" .. v"); 76 writer.printSignedIntAsDec(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 register the register number 87 */ writeTo(IndentingWriter writer, int register)88 public void writeTo(IndentingWriter writer, int register) throws IOException { 89 if (options.parameterRegisters) { 90 if (register >= registerCount - parameterRegisterCount) { 91 writer.write('p'); 92 writer.printSignedIntAsDec((register - (registerCount - parameterRegisterCount))); 93 return; 94 } 95 } 96 writer.write('v'); 97 writer.printSignedIntAsDec(register); 98 } 99 } 100