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