RegisterFormatter.java revision 5867263eb588f4671400895d1e6b01c01535061b
11e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)/*
21e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * [The "BSD licence"]
31e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * Copyright (c) 2009 Ben Gruver
41e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * All rights reserved.
51e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) *
61e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * Redistribution and use in source and binary forms, with or without
71e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * modification, are permitted provided that the following conditions
85f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) * are met:
95f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) * 1. Redistributions of source code must retain the above copyright
105f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) *    notice, this list of conditions and the following disclaimer.
115f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) * 2. Redistributions in binary form must reproduce the above copyright
125f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) *    notice, this list of conditions and the following disclaimer in the
135f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) *    documentation and/or other materials provided with the distribution.
145f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) * 3. The name of the author may not be used to endorse or promote products
155f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) *    derived from this software without specific prior written permission.
166e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) *
17f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
185f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
211e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
221e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
235f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
245f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
251e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
265f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
271e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) */
28f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
29f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)package org.jf.baksmali.Adaptors;
301e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
315f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)import org.jf.dexlib.CodeItem;
325f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)import org.jf.dexlib.Util.AccessFlags;
335f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)import org.jf.baksmali.baksmali;
345f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
355f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)/**
365f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) * This class contains the logic used for formatting registers
37f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) */
381e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)public class RegisterFormatter {
391e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
401e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    /**
41f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)     * This method is used (only) by format 3rc (the format that uses a range of regsiters like {v1 .. v10}) to format
42a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)     * it's registers. If both registers are parameter registers, they will be formatted as such, otherwise they will
43a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)     * both be formatted as normal registers
441e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)     * @param codeItem
451e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)     * @param startRegister
46a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)     * @param lastRegister
471e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)     * @return an array of 2 strings containing the formatted registers
481e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)     */
491e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    public static String[] formatFormat3rcRegisters(CodeItem codeItem, int startRegister, int lastRegister) {
501e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        if (!baksmali.noParameterRegisters) {
511e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)            int parameterRegisterCount = codeItem.getParent().method.getPrototype().getParameterRegisterCount()
52f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                + (((codeItem.getParent().accessFlags & AccessFlags.STATIC.getValue())==0)?1:0);
531e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)            int registerCount = codeItem.getRegisterCount();
541e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
555f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)            assert startRegister <= lastRegister;
566e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
575f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)            if (startRegister >= registerCount - parameterRegisterCount) {
586e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                return new String[] {"p" + (startRegister - (registerCount - parameterRegisterCount)),
596e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                                     "p" + (lastRegister - (registerCount - parameterRegisterCount))};
606e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)            }
616e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)        }
625f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        return new String[] {"v" + startRegister,
635f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                             "v" + lastRegister};
645f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    }
655f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
665f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    /**
675f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)     * Formats a register with the appropriate format - with either the normal v<n> format or the p<n> parameter format.
685f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)     *
695f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)     * It uses the register and parameter information from the give <code>CodeItem</code> to determine if the given
705f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)     * register is a normal or parameter register.
715f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)     * @param codeItem
725f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)     * @param register
735f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)     * @return The formatted register
745f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)     */
751e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    public static String formatRegister(CodeItem codeItem, int register) {
765f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        if (!baksmali.noParameterRegisters) {
775f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)            int parameterRegisterCount = codeItem.getParent().method.getPrototype().getParameterRegisterCount()
785f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                + (((codeItem.getParent().accessFlags & AccessFlags.STATIC.getValue())==0)?1:0);
795f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)            int registerCount = codeItem.getRegisterCount();
805f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)            if (register >= registerCount - parameterRegisterCount) {
815f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                return "p" + (register - (registerCount - parameterRegisterCount));
821e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)            }
831e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        }
841e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        return "v" + register;
851e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    }
861e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
871e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)