Home | History | Annotate | Download | only in disassembler
      1 /*
      2  * Copyright (C) 2012 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #include "disassembler_mips.h"
     18 
     19 #include <ostream>
     20 #include <sstream>
     21 
     22 #include "base/logging.h"
     23 #include "base/stringprintf.h"
     24 #include "thread.h"
     25 
     26 namespace art {
     27 namespace mips {
     28 
     29 struct MipsInstruction {
     30   uint32_t mask;
     31   uint32_t value;
     32   const char* name;
     33   const char* args_fmt;
     34 
     35   bool Matches(uint32_t instruction) const {
     36     return (instruction & mask) == value;
     37   }
     38 };
     39 
     40 static const uint32_t kOpcodeShift = 26;
     41 
     42 static const uint32_t kCop1 = (17 << kOpcodeShift);
     43 
     44 static const uint32_t kITypeMask = (0x3f << kOpcodeShift);
     45 static const uint32_t kJTypeMask = (0x3f << kOpcodeShift);
     46 static const uint32_t kRTypeMask = ((0x3f << kOpcodeShift) | (0x3f));
     47 static const uint32_t kSpecial0Mask = (0x3f << kOpcodeShift);
     48 static const uint32_t kSpecial2Mask = (0x3f << kOpcodeShift);
     49 static const uint32_t kSpecial3Mask = (0x3f << kOpcodeShift);
     50 static const uint32_t kFpMask = kRTypeMask;
     51 
     52 static const MipsInstruction gMipsInstructions[] = {
     53   // "sll r0, r0, 0" is the canonical "nop", used in delay slots.
     54   { 0xffffffff, 0, "nop", "" },
     55 
     56   // R-type instructions.
     57   { kRTypeMask, 0, "sll", "DTA", },
     58   // 0, 1, movci
     59   { kRTypeMask | (0x1f << 21), 2, "srl", "DTA", },
     60   { kRTypeMask, 3, "sra", "DTA", },
     61   { kRTypeMask | (0x1f << 6), 4, "sllv", "DTS", },
     62   { kRTypeMask | (0x1f << 6), 6, "srlv", "DTS", },
     63   { kRTypeMask | (0x1f << 6), (1 << 6) | 6, "rotrv", "DTS", },
     64   { kRTypeMask | (0x1f << 6), 7, "srav", "DTS", },
     65   { kRTypeMask, 8, "jr", "S", },
     66   { kRTypeMask | (0x1f << 11), 9 | (31 << 11), "jalr", "S", },  // rd = 31 is implicit.
     67   { kRTypeMask | (0x1f << 11), 9, "jr", "S", },  // rd = 0 is implicit.
     68   { kRTypeMask, 9, "jalr", "DS", },  // General case.
     69   { kRTypeMask | (0x1f << 6), 10, "movz", "DST", },
     70   { kRTypeMask | (0x1f << 6), 11, "movn", "DST", },
     71   { kRTypeMask, 12, "syscall", "", },  // TODO: code
     72   { kRTypeMask, 13, "break", "", },  // TODO: code
     73   { kRTypeMask, 15, "sync", "", },  // TODO: type
     74   { kRTypeMask, 16, "mfhi", "D", },
     75   { kRTypeMask, 17, "mthi", "S", },
     76   { kRTypeMask, 18, "mflo", "D", },
     77   { kRTypeMask, 19, "mtlo", "S", },
     78   { kRTypeMask | (0x1f << 6), 20, "dsllv", "DTS", },
     79   { kRTypeMask | (0x1f << 6), 22, "dsrlv", "DTS", },
     80   { kRTypeMask | (0x1f << 6), (1 << 6) | 22, "drotrv", "DTS", },
     81   { kRTypeMask | (0x1f << 6), 23, "dsrav", "DTS", },
     82   { kRTypeMask | (0x1f << 6), 24, "mult", "ST", },
     83   { kRTypeMask | (0x1f << 6), 25, "multu", "ST", },
     84   { kRTypeMask | (0x1f << 6), 26, "div", "ST", },
     85   { kRTypeMask | (0x1f << 6), 27, "divu", "ST", },
     86   { kRTypeMask | (0x1f << 6), 24 + (2 << 6), "mul", "DST", },
     87   { kRTypeMask | (0x1f << 6), 24 + (3 << 6), "muh", "DST", },
     88   { kRTypeMask | (0x1f << 6), 26 + (2 << 6), "div", "DST", },
     89   { kRTypeMask | (0x1f << 6), 26 + (3 << 6), "mod", "DST", },
     90   { kRTypeMask, 32, "add", "DST", },
     91   { kRTypeMask, 33, "addu", "DST", },
     92   { kRTypeMask, 34, "sub", "DST", },
     93   { kRTypeMask, 35, "subu", "DST", },
     94   { kRTypeMask, 36, "and", "DST", },
     95   { kRTypeMask, 37, "or", "DST", },
     96   { kRTypeMask, 38, "xor", "DST", },
     97   { kRTypeMask, 39, "nor", "DST", },
     98   { kRTypeMask, 42, "slt", "DST", },
     99   { kRTypeMask, 43, "sltu", "DST", },
    100   { kRTypeMask, 45, "daddu", "DST", },
    101   { kRTypeMask, 46, "dsub", "DST", },
    102   { kRTypeMask, 47, "dsubu", "DST", },
    103   // TODO: tge[u], tlt[u], teg, tne
    104   { kRTypeMask | (0x1f << 21), 56, "dsll", "DTA", },
    105   { kRTypeMask | (0x1f << 21), 58, "dsrl", "DTA", },
    106   { kRTypeMask | (0x1f << 21), (1 << 21) | 58, "drotr", "DTA", },
    107   { kRTypeMask | (0x1f << 21), 59, "dsra", "DTA", },
    108   { kRTypeMask | (0x1f << 21), 60, "dsll32", "DTA", },
    109   { kRTypeMask | (0x1f << 21), 62, "dsrl32", "DTA", },
    110   { kRTypeMask | (0x1f << 21), (1 << 21) | 62, "drotr32", "DTA", },
    111   { kRTypeMask | (0x1f << 21), 63, "dsra32", "DTA", },
    112 
    113   // SPECIAL0
    114   { kSpecial0Mask | 0x307ff, 1, "movf", "DSc" },
    115   { kSpecial0Mask | 0x307ff, 0x10001, "movt", "DSc" },
    116   { kSpecial0Mask | 0x7ff, (2 << 6) | 24, "mul", "DST" },
    117   { kSpecial0Mask | 0x7ff, (3 << 6) | 24, "muh", "DST" },
    118   { kSpecial0Mask | 0x7ff, (2 << 6) | 25, "mulu", "DST" },
    119   { kSpecial0Mask | 0x7ff, (3 << 6) | 25, "muhu", "DST" },
    120   { kSpecial0Mask | 0x7ff, (2 << 6) | 26, "div", "DST" },
    121   { kSpecial0Mask | 0x7ff, (3 << 6) | 26, "mod", "DST" },
    122   { kSpecial0Mask | 0x7ff, (2 << 6) | 27, "divu", "DST" },
    123   { kSpecial0Mask | 0x7ff, (3 << 6) | 27, "modu", "DST" },
    124   { kSpecial0Mask | 0x7ff, (2 << 6) | 28, "dmul", "DST" },
    125   { kSpecial0Mask | 0x7ff, (3 << 6) | 28, "dmuh", "DST" },
    126   { kSpecial0Mask | 0x7ff, (2 << 6) | 29, "dmulu", "DST" },
    127   { kSpecial0Mask | 0x7ff, (3 << 6) | 29, "dmuhu", "DST" },
    128   { kSpecial0Mask | 0x7ff, (2 << 6) | 30, "ddiv", "DST" },
    129   { kSpecial0Mask | 0x7ff, (3 << 6) | 30, "dmod", "DST" },
    130   { kSpecial0Mask | 0x7ff, (2 << 6) | 31, "ddivu", "DST" },
    131   { kSpecial0Mask | 0x7ff, (3 << 6) | 31, "dmodu", "DST" },
    132   { kSpecial0Mask | 0x7ff, (0 << 6) | 53, "seleqz", "DST" },
    133   { kSpecial0Mask | 0x7ff, (0 << 6) | 55, "selnez", "DST" },
    134   { kSpecial0Mask | (0x1f << 21) | 0x3f, (1 << 21) | 2, "rotr", "DTA", },
    135   { kSpecial0Mask | (0x1f << 16) | 0x7ff, (0x01 << 6) | 0x10, "clz", "DS" },
    136   { kSpecial0Mask | (0x1f << 16) | 0x7ff, (0x01 << 6) | 0x11, "clo", "DS" },
    137   { kSpecial0Mask | (0x1f << 16) | 0x7ff, (0x01 << 6) | 0x12, "dclz", "DS" },
    138   { kSpecial0Mask | (0x1f << 16) | 0x7ff, (0x01 << 6) | 0x13, "dclo", "DS" },
    139   // TODO: sdbbp
    140 
    141   // SPECIAL2
    142   { kSpecial2Mask | 0x7ff, (28 << kOpcodeShift) | 2, "mul", "DST" },
    143   { kSpecial2Mask | 0x7ff, (28 << kOpcodeShift) | 32, "clz", "DS" },
    144   { kSpecial2Mask | 0x7ff, (28 << kOpcodeShift) | 33, "clo", "DS" },
    145   { kSpecial2Mask | 0xffff, (28 << kOpcodeShift) | 0, "madd", "ST" },
    146   { kSpecial2Mask | 0xffff, (28 << kOpcodeShift) | 1, "maddu", "ST" },
    147   { kSpecial2Mask | 0xffff, (28 << kOpcodeShift) | 2, "mul", "DST" },
    148   { kSpecial2Mask | 0xffff, (28 << kOpcodeShift) | 4, "msub", "ST" },
    149   { kSpecial2Mask | 0xffff, (28 << kOpcodeShift) | 5, "msubu", "ST" },
    150   { kSpecial2Mask | 0x3f, (28 << kOpcodeShift) | 0x3f, "sdbbp", "" },  // TODO: code
    151 
    152   // SPECIAL3
    153   { kSpecial3Mask | 0x3f, (31 << kOpcodeShift), "ext", "TSAZ", },
    154   { kSpecial3Mask | 0x3f, (31 << kOpcodeShift) | 3, "dext", "TSAZ", },
    155   { kSpecial3Mask | 0x3f, (31 << kOpcodeShift) | 4, "ins", "TSAz", },
    156   { kSpecial3Mask | (0x1f << 21) | (0x1f << 6) | 0x3f,
    157     (31 << kOpcodeShift) | (16 << 6) | 32,
    158     "seb",
    159     "DT", },
    160   { kSpecial3Mask | (0x1f << 21) | (0x1f << 6) | 0x3f,
    161     (31 << kOpcodeShift) | (24 << 6) | 32,
    162     "seh",
    163     "DT", },
    164   { kSpecial3Mask | (0x1f << 21) | (0x1f << 6) | 0x3f,
    165     (31 << kOpcodeShift) | 32,
    166     "bitswap",
    167     "DT", },
    168   { kSpecial3Mask | (0x1f << 21) | (0x1f << 6) | 0x3f,
    169     (31 << kOpcodeShift) | 36,
    170     "dbitswap",
    171     "DT", },
    172   { kSpecial3Mask | (0x1f << 21) | (0x1f << 6) | 0x3f,
    173     (31 << kOpcodeShift) | (2 << 6) | 36,
    174     "dsbh",
    175     "DT", },
    176   { kSpecial3Mask | (0x1f << 21) | (0x1f << 6) | 0x3f,
    177     (31 << kOpcodeShift) | (5 << 6) | 36,
    178     "dshd",
    179     "DT", },
    180   { kSpecial3Mask | (0x1f << 21) | (0x1f << 6) | 0x3f,
    181     (31 << kOpcodeShift) | (2 << 6) | 32,
    182     "wsbh",
    183     "DT", },
    184   { kSpecial3Mask | 0x7f, (31 << kOpcodeShift) | 0x26, "sc", "Tl", },
    185   { kSpecial3Mask | 0x7f, (31 << kOpcodeShift) | 0x27, "scd", "Tl", },
    186   { kSpecial3Mask | 0x7f, (31 << kOpcodeShift) | 0x36, "ll", "Tl", },
    187   { kSpecial3Mask | 0x7f, (31 << kOpcodeShift) | 0x37, "lld", "Tl", },
    188 
    189   // J-type instructions.
    190   { kJTypeMask, 2 << kOpcodeShift, "j", "L" },
    191   { kJTypeMask, 3 << kOpcodeShift, "jal", "L" },
    192 
    193   // I-type instructions.
    194   { kITypeMask, 4 << kOpcodeShift, "beq", "STB" },
    195   { kITypeMask, 5 << kOpcodeShift, "bne", "STB" },
    196   { kITypeMask | (0x1f << 16), 1 << kOpcodeShift | (1 << 16), "bgez", "SB" },
    197   { kITypeMask | (0x1f << 16), 1 << kOpcodeShift | (0 << 16), "bltz", "SB" },
    198   { kITypeMask | (0x1f << 16), 1 << kOpcodeShift | (2 << 16), "bltzl", "SB" },
    199   { kITypeMask | (0x1f << 16), 1 << kOpcodeShift | (16 << 16), "bltzal", "SB" },
    200   { kITypeMask | (0x1f << 16), 1 << kOpcodeShift | (18 << 16), "bltzall", "SB" },
    201   { kITypeMask | (0x1f << 16), 6 << kOpcodeShift | (0 << 16), "blez", "SB" },
    202   { kITypeMask, 6 << kOpcodeShift, "bgeuc", "STB" },
    203   { kITypeMask | (0x1f << 16), 7 << kOpcodeShift | (0 << 16), "bgtz", "SB" },
    204   { kITypeMask, 7 << kOpcodeShift, "bltuc", "STB" },
    205   { kITypeMask | (0x1f << 16), 1 << kOpcodeShift | (6 << 16), "dahi", "Si", },
    206   { kITypeMask | (0x1f << 16), 1 << kOpcodeShift | (30 << 16), "dati", "Si", },
    207 
    208   { 0xffff0000, (4 << kOpcodeShift), "b", "B" },
    209   { 0xffff0000, (1 << kOpcodeShift) | (17 << 16), "bal", "B" },
    210 
    211   { kITypeMask, 8 << kOpcodeShift, "beqc", "STB" },
    212 
    213   { kITypeMask, 8 << kOpcodeShift, "addi", "TSi", },
    214   { kITypeMask, 9 << kOpcodeShift, "addiu", "TSi", },
    215   { kITypeMask, 10 << kOpcodeShift, "slti", "TSi", },
    216   { kITypeMask, 11 << kOpcodeShift, "sltiu", "TSi", },
    217   { kITypeMask, 12 << kOpcodeShift, "andi", "TSi", },
    218   { kITypeMask, 13 << kOpcodeShift, "ori", "TSi", },
    219   { kITypeMask, 14 << kOpcodeShift, "xori", "TSi", },
    220   { kITypeMask | (0x1f << 21), 15 << kOpcodeShift, "lui", "TI", },
    221   { kITypeMask, 15 << kOpcodeShift, "aui", "TSI", },
    222 
    223   { kITypeMask | (0x3e3 << 16), (17 << kOpcodeShift) | (8 << 21), "bc1f", "cB" },
    224   { kITypeMask | (0x3e3 << 16), (17 << kOpcodeShift) | (8 << 21) | (1 << 16), "bc1t", "cB" },
    225   { kITypeMask | (0x1f << 21), (17 << kOpcodeShift) | (9 << 21), "bc1eqz", "tB" },
    226   { kITypeMask | (0x1f << 21), (17 << kOpcodeShift) | (13 << 21), "bc1nez", "tB" },
    227 
    228   { kITypeMask | (0x1f << 21), 22 << kOpcodeShift, "blezc", "TB" },
    229 
    230   // TODO: de-dup
    231   { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (1  << 21) | (1  << 16), "bgezc", "TB" },
    232   { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (2  << 21) | (2  << 16), "bgezc", "TB" },
    233   { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (3  << 21) | (3  << 16), "bgezc", "TB" },
    234   { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (4  << 21) | (4  << 16), "bgezc", "TB" },
    235   { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (5  << 21) | (5  << 16), "bgezc", "TB" },
    236   { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (6  << 21) | (6  << 16), "bgezc", "TB" },
    237   { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (7  << 21) | (7  << 16), "bgezc", "TB" },
    238   { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (8  << 21) | (8  << 16), "bgezc", "TB" },
    239   { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (9  << 21) | (9  << 16), "bgezc", "TB" },
    240   { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (10 << 21) | (10 << 16), "bgezc", "TB" },
    241   { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (11 << 21) | (11 << 16), "bgezc", "TB" },
    242   { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (12 << 21) | (12 << 16), "bgezc", "TB" },
    243   { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (13 << 21) | (13 << 16), "bgezc", "TB" },
    244   { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (14 << 21) | (14 << 16), "bgezc", "TB" },
    245   { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (15 << 21) | (15 << 16), "bgezc", "TB" },
    246   { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (16 << 21) | (16 << 16), "bgezc", "TB" },
    247   { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (17 << 21) | (17 << 16), "bgezc", "TB" },
    248   { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (18 << 21) | (18 << 16), "bgezc", "TB" },
    249   { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (19 << 21) | (19 << 16), "bgezc", "TB" },
    250   { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (20 << 21) | (20 << 16), "bgezc", "TB" },
    251   { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (21 << 21) | (21 << 16), "bgezc", "TB" },
    252   { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (22 << 21) | (22 << 16), "bgezc", "TB" },
    253   { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (23 << 21) | (23 << 16), "bgezc", "TB" },
    254   { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (24 << 21) | (24 << 16), "bgezc", "TB" },
    255   { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (25 << 21) | (25 << 16), "bgezc", "TB" },
    256   { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (26 << 21) | (26 << 16), "bgezc", "TB" },
    257   { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (27 << 21) | (27 << 16), "bgezc", "TB" },
    258   { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (28 << 21) | (28 << 16), "bgezc", "TB" },
    259   { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (29 << 21) | (29 << 16), "bgezc", "TB" },
    260   { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (30 << 21) | (30 << 16), "bgezc", "TB" },
    261   { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (31 << 21) | (31 << 16), "bgezc", "TB" },
    262 
    263   { kITypeMask, 22 << kOpcodeShift, "bgec", "STB" },
    264 
    265   { kITypeMask | (0x1f << 21), 23 << kOpcodeShift, "bgtzc", "TB" },
    266 
    267   // TODO: de-dup
    268   { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (1  << 21) | (1  << 16), "bltzc", "TB" },
    269   { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (2  << 21) | (2  << 16), "bltzc", "TB" },
    270   { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (3  << 21) | (3  << 16), "bltzc", "TB" },
    271   { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (4  << 21) | (4  << 16), "bltzc", "TB" },
    272   { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (5  << 21) | (5  << 16), "bltzc", "TB" },
    273   { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (6  << 21) | (6  << 16), "bltzc", "TB" },
    274   { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (7  << 21) | (7  << 16), "bltzc", "TB" },
    275   { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (8  << 21) | (8  << 16), "bltzc", "TB" },
    276   { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (9  << 21) | (9  << 16), "bltzc", "TB" },
    277   { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (10 << 21) | (10 << 16), "bltzc", "TB" },
    278   { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (11 << 21) | (11 << 16), "bltzc", "TB" },
    279   { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (12 << 21) | (12 << 16), "bltzc", "TB" },
    280   { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (13 << 21) | (13 << 16), "bltzc", "TB" },
    281   { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (14 << 21) | (14 << 16), "bltzc", "TB" },
    282   { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (15 << 21) | (15 << 16), "bltzc", "TB" },
    283   { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (16 << 21) | (16 << 16), "bltzc", "TB" },
    284   { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (17 << 21) | (17 << 16), "bltzc", "TB" },
    285   { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (18 << 21) | (18 << 16), "bltzc", "TB" },
    286   { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (19 << 21) | (19 << 16), "bltzc", "TB" },
    287   { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (20 << 21) | (20 << 16), "bltzc", "TB" },
    288   { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (21 << 21) | (21 << 16), "bltzc", "TB" },
    289   { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (22 << 21) | (22 << 16), "bltzc", "TB" },
    290   { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (23 << 21) | (23 << 16), "bltzc", "TB" },
    291   { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (24 << 21) | (24 << 16), "bltzc", "TB" },
    292   { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (25 << 21) | (25 << 16), "bltzc", "TB" },
    293   { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (26 << 21) | (26 << 16), "bltzc", "TB" },
    294   { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (27 << 21) | (27 << 16), "bltzc", "TB" },
    295   { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (28 << 21) | (28 << 16), "bltzc", "TB" },
    296   { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (29 << 21) | (29 << 16), "bltzc", "TB" },
    297   { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (30 << 21) | (30 << 16), "bltzc", "TB" },
    298   { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (31 << 21) | (31 << 16), "bltzc", "TB" },
    299 
    300   { kITypeMask, 23 << kOpcodeShift, "bltc", "STB" },
    301 
    302   { kITypeMask, 24 << kOpcodeShift, "bnec", "STB" },
    303 
    304   { kITypeMask, 25 << kOpcodeShift, "daddiu", "TSi", },
    305   { kITypeMask, 29 << kOpcodeShift, "daui", "TSi", },
    306 
    307   { kITypeMask, 32u << kOpcodeShift, "lb", "TO", },
    308   { kITypeMask, 33u << kOpcodeShift, "lh", "TO", },
    309   { kITypeMask, 34u << kOpcodeShift, "lwl", "TO", },
    310   { kITypeMask, 35u << kOpcodeShift, "lw", "TO", },
    311   { kITypeMask, 36u << kOpcodeShift, "lbu", "TO", },
    312   { kITypeMask, 37u << kOpcodeShift, "lhu", "TO", },
    313   { kITypeMask, 38u << kOpcodeShift, "lwr", "TO", },
    314   { kITypeMask, 39u << kOpcodeShift, "lwu", "TO", },
    315   { kITypeMask, 40u << kOpcodeShift, "sb", "TO", },
    316   { kITypeMask, 41u << kOpcodeShift, "sh", "TO", },
    317   { kITypeMask, 42u << kOpcodeShift, "swl", "TO", },
    318   { kITypeMask, 43u << kOpcodeShift, "sw", "TO", },
    319   { kITypeMask, 46u << kOpcodeShift, "swr", "TO", },
    320   { kITypeMask, 48u << kOpcodeShift, "ll", "TO", },
    321   { kITypeMask, 49u << kOpcodeShift, "lwc1", "tO", },
    322   { kJTypeMask, 50u << kOpcodeShift, "bc", "P" },
    323   { kITypeMask, 53u << kOpcodeShift, "ldc1", "tO", },
    324   { kITypeMask | (0x1f << 21), 54u << kOpcodeShift, "jic", "Ti" },
    325   { kITypeMask | (1 << 21), (54u << kOpcodeShift) | (1 << 21), "beqzc", "Sb" },  // TODO: de-dup?
    326   { kITypeMask | (1 << 22), (54u << kOpcodeShift) | (1 << 22), "beqzc", "Sb" },
    327   { kITypeMask | (1 << 23), (54u << kOpcodeShift) | (1 << 23), "beqzc", "Sb" },
    328   { kITypeMask | (1 << 24), (54u << kOpcodeShift) | (1 << 24), "beqzc", "Sb" },
    329   { kITypeMask | (1 << 25), (54u << kOpcodeShift) | (1 << 25), "beqzc", "Sb" },
    330   { kITypeMask, 55u << kOpcodeShift, "ld", "TO", },
    331   { kITypeMask, 56u << kOpcodeShift, "sc", "TO", },
    332   { kITypeMask, 57u << kOpcodeShift, "swc1", "tO", },
    333   { kITypeMask | (0x1f << 16), (59u << kOpcodeShift) | (30 << 16), "auipc", "Si" },
    334   { kITypeMask | (0x3 << 19), (59u << kOpcodeShift) | (0 << 19), "addiupc", "Sp" },
    335   { kITypeMask, 61u << kOpcodeShift, "sdc1", "tO", },
    336   { kITypeMask | (0x1f << 21), 62u << kOpcodeShift, "jialc", "Ti" },
    337   { kITypeMask | (1 << 21), (62u << kOpcodeShift) | (1 << 21), "bnezc", "Sb" },  // TODO: de-dup?
    338   { kITypeMask | (1 << 22), (62u << kOpcodeShift) | (1 << 22), "bnezc", "Sb" },
    339   { kITypeMask | (1 << 23), (62u << kOpcodeShift) | (1 << 23), "bnezc", "Sb" },
    340   { kITypeMask | (1 << 24), (62u << kOpcodeShift) | (1 << 24), "bnezc", "Sb" },
    341   { kITypeMask | (1 << 25), (62u << kOpcodeShift) | (1 << 25), "bnezc", "Sb" },
    342   { kITypeMask, 63u << kOpcodeShift, "sd", "TO", },
    343 
    344   // Floating point.
    345   { kFpMask | (0x1f << 21), kCop1 | (0x00 << 21), "mfc1", "Td" },
    346   { kFpMask | (0x1f << 21), kCop1 | (0x01 << 21), "dmfc1", "Td" },
    347   { kFpMask | (0x1f << 21), kCop1 | (0x03 << 21), "mfhc1", "Td" },
    348   { kFpMask | (0x1f << 21), kCop1 | (0x04 << 21), "mtc1", "Td" },
    349   { kFpMask | (0x1f << 21), kCop1 | (0x05 << 21), "dmtc1", "Td" },
    350   { kFpMask | (0x1f << 21), kCop1 | (0x07 << 21), "mthc1", "Td" },
    351   { kFpMask | (0x1f << 21), kCop1 | (0x14 << 21) | 1, "cmp.un.s", "adt" },
    352   { kFpMask | (0x1f << 21), kCop1 | (0x14 << 21) | 2, "cmp.eq.s", "adt" },
    353   { kFpMask | (0x1f << 21), kCop1 | (0x14 << 21) | 3, "cmp.ueq.s", "adt" },
    354   { kFpMask | (0x1f << 21), kCop1 | (0x14 << 21) | 4, "cmp.lt.s", "adt" },
    355   { kFpMask | (0x1f << 21), kCop1 | (0x14 << 21) | 5, "cmp.ult.s", "adt" },
    356   { kFpMask | (0x1f << 21), kCop1 | (0x14 << 21) | 6, "cmp.le.s", "adt" },
    357   { kFpMask | (0x1f << 21), kCop1 | (0x14 << 21) | 7, "cmp.ule.s", "adt" },
    358   { kFpMask | (0x1f << 21), kCop1 | (0x14 << 21) | 17, "cmp.or.s", "adt" },
    359   { kFpMask | (0x1f << 21), kCop1 | (0x14 << 21) | 18, "cmp.une.s", "adt" },
    360   { kFpMask | (0x1f << 21), kCop1 | (0x14 << 21) | 19, "cmp.ne.s", "adt" },
    361   { kFpMask | (0x1f << 21), kCop1 | (0x15 << 21) | 1, "cmp.un.d", "adt" },
    362   { kFpMask | (0x1f << 21), kCop1 | (0x15 << 21) | 2, "cmp.eq.d", "adt" },
    363   { kFpMask | (0x1f << 21), kCop1 | (0x15 << 21) | 3, "cmp.ueq.d", "adt" },
    364   { kFpMask | (0x1f << 21), kCop1 | (0x15 << 21) | 4, "cmp.lt.d", "adt" },
    365   { kFpMask | (0x1f << 21), kCop1 | (0x15 << 21) | 5, "cmp.ult.d", "adt" },
    366   { kFpMask | (0x1f << 21), kCop1 | (0x15 << 21) | 6, "cmp.le.d", "adt" },
    367   { kFpMask | (0x1f << 21), kCop1 | (0x15 << 21) | 7, "cmp.ule.d", "adt" },
    368   { kFpMask | (0x1f << 21), kCop1 | (0x15 << 21) | 17, "cmp.or.d", "adt" },
    369   { kFpMask | (0x1f << 21), kCop1 | (0x15 << 21) | 18, "cmp.une.d", "adt" },
    370   { kFpMask | (0x1f << 21), kCop1 | (0x15 << 21) | 19, "cmp.ne.d", "adt" },
    371   { kFpMask | (0x10 << 21), kCop1 | (0x10 << 21) | 0, "add", "fadt" },
    372   { kFpMask | (0x10 << 21), kCop1 | (0x10 << 21) | 1, "sub", "fadt" },
    373   { kFpMask | (0x10 << 21), kCop1 | (0x10 << 21) | 2, "mul", "fadt" },
    374   { kFpMask | (0x10 << 21), kCop1 | (0x10 << 21) | 3, "div", "fadt" },
    375   { kFpMask | (0x10 << 21), kCop1 | (0x10 << 21) | 4, "sqrt", "fad" },
    376   { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 5, "abs", "fad" },
    377   { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 6, "mov", "fad" },
    378   { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 7, "neg", "fad" },
    379   { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 8, "round.l", "fad" },
    380   { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 9, "trunc.l", "fad" },
    381   { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 10, "ceil.l", "fad" },
    382   { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 11, "floor.l", "fad" },
    383   { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 12, "round.w", "fad" },
    384   { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 13, "trunc.w", "fad" },
    385   { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 14, "ceil.w", "fad" },
    386   { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 15, "floor.w", "fad" },
    387   { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 26, "rint", "fad" },
    388   { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 27, "class", "fad" },
    389   { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 32, "cvt.s", "fad" },
    390   { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 33, "cvt.d", "fad" },
    391   { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 36, "cvt.w", "fad" },
    392   { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 37, "cvt.l", "fad" },
    393   { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 38, "cvt.ps", "fad" },
    394   { kFpMask | (0x10 << 21), kCop1 | (0x10 << 21) | 49, "c.un", "fCdt" },
    395   { kFpMask | (0x10 << 21), kCop1 | (0x10 << 21) | 50, "c.eq", "fCdt" },
    396   { kFpMask | (0x10 << 21), kCop1 | (0x10 << 21) | 51, "c.ueq", "fCdt" },
    397   { kFpMask | (0x10 << 21), kCop1 | (0x10 << 21) | 52, "c.olt", "fCdt" },
    398   { kFpMask | (0x10 << 21), kCop1 | (0x10 << 21) | 53, "c.ult", "fCdt" },
    399   { kFpMask | (0x10 << 21), kCop1 | (0x10 << 21) | 54, "c.ole", "fCdt" },
    400   { kFpMask | (0x10 << 21), kCop1 | (0x10 << 21) | 55, "c.ule", "fCdt" },
    401   { kFpMask, kCop1 | 0x10, "sel", "fadt" },
    402   { kFpMask, kCop1 | 0x1e, "max", "fadt" },
    403   { kFpMask, kCop1 | 0x1c, "min", "fadt" },
    404 };
    405 
    406 static uint32_t ReadU32(const uint8_t* ptr) {
    407   // We only support little-endian MIPS.
    408   return ptr[0] | (ptr[1] << 8) | (ptr[2] << 16) | (ptr[3] << 24);
    409 }
    410 
    411 size_t DisassemblerMips::Dump(std::ostream& os, const uint8_t* instr_ptr) {
    412   uint32_t instruction = ReadU32(instr_ptr);
    413 
    414   uint32_t rs = (instruction >> 21) & 0x1f;  // I-type, R-type.
    415   uint32_t rt = (instruction >> 16) & 0x1f;  // I-type, R-type.
    416   uint32_t rd = (instruction >> 11) & 0x1f;  // R-type.
    417   uint32_t sa = (instruction >>  6) & 0x1f;  // R-type.
    418 
    419   std::string opcode;
    420   std::ostringstream args;
    421 
    422   // TODO: remove this!
    423   uint32_t op = (instruction >> 26) & 0x3f;
    424   uint32_t function = (instruction & 0x3f);  // R-type.
    425   opcode = StringPrintf("op=%d fn=%d", op, function);
    426 
    427   for (size_t i = 0; i < arraysize(gMipsInstructions); ++i) {
    428     if (gMipsInstructions[i].Matches(instruction)) {
    429       opcode = gMipsInstructions[i].name;
    430       for (const char* args_fmt = gMipsInstructions[i].args_fmt; *args_fmt; ++args_fmt) {
    431         switch (*args_fmt) {
    432           case 'A':  // sa (shift amount or [d]ins/[d]ext position).
    433             args << sa;
    434             break;
    435           case 'B':  // Branch offset.
    436             {
    437               int32_t offset = static_cast<int16_t>(instruction & 0xffff);
    438               offset <<= 2;
    439               offset += 4;  // Delay slot.
    440               args << FormatInstructionPointer(instr_ptr + offset)
    441                    << StringPrintf("  ; %+d", offset);
    442             }
    443             break;
    444           case 'b':  // 21-bit branch offset.
    445             {
    446               int32_t offset = (instruction & 0x1fffff) - ((instruction & 0x100000) << 1);
    447               offset <<= 2;
    448               offset += 4;  // Delay slot.
    449               args << FormatInstructionPointer(instr_ptr + offset)
    450                    << StringPrintf("  ; %+d", offset);
    451             }
    452             break;
    453           case 'C':  // Floating-point condition code flag in c.<cond>.fmt.
    454             args << "cc" << (sa >> 2);
    455             break;
    456           case 'c':  // Floating-point condition code flag in bc1f/bc1t and movf/movt.
    457             args << "cc" << (rt >> 2);
    458             break;
    459           case 'D': args << 'r' << rd; break;
    460           case 'd': args << 'f' << rd; break;
    461           case 'a': args << 'f' << sa; break;
    462           case 'f':  // Floating point "fmt".
    463             {
    464               size_t fmt = (instruction >> 21) & 0x7;  // TODO: other fmts?
    465               switch (fmt) {
    466                 case 0: opcode += ".s"; break;
    467                 case 1: opcode += ".d"; break;
    468                 case 4: opcode += ".w"; break;
    469                 case 5: opcode += ".l"; break;
    470                 case 6: opcode += ".ps"; break;
    471                 default: opcode += ".?"; break;
    472               }
    473               continue;  // No ", ".
    474             }
    475           case 'I':  // Upper 16-bit immediate.
    476             args << reinterpret_cast<void*>((instruction & 0xffff) << 16);
    477             break;
    478           case 'i':  // Sign-extended lower 16-bit immediate.
    479             args << static_cast<int16_t>(instruction & 0xffff);
    480             break;
    481           case 'L':  // Jump label.
    482             {
    483               // TODO: is this right?
    484               uint32_t instr_index = (instruction & 0x1ffffff);
    485               uint32_t target = (instr_index << 2);
    486               target |= (reinterpret_cast<uintptr_t>(instr_ptr + 4) & 0xf0000000);
    487               args << reinterpret_cast<void*>(target);
    488             }
    489             break;
    490           case 'l':  // 9-bit signed offset
    491             {
    492               int32_t offset = static_cast<int16_t>(instruction) >> 7;
    493               args << StringPrintf("%+d(r%d)", offset, rs);
    494             }
    495             break;
    496           case 'O':  // +x(rs)
    497             {
    498               int32_t offset = static_cast<int16_t>(instruction & 0xffff);
    499               args << StringPrintf("%+d(r%d)", offset, rs);
    500               if (rs == 17) {
    501                 args << "  ; ";
    502                 if (is64bit_) {
    503                   Thread::DumpThreadOffset<8>(args, offset);
    504                 } else {
    505                   Thread::DumpThreadOffset<4>(args, offset);
    506                 }
    507               }
    508             }
    509             break;
    510           case 'P':  // 26-bit offset in bc.
    511             {
    512               int32_t offset = (instruction & 0x3ffffff) - ((instruction & 0x2000000) << 1);
    513               offset <<= 2;
    514               offset += 4;
    515               args << FormatInstructionPointer(instr_ptr + offset);
    516               args << StringPrintf("  ; %+d", offset);
    517             }
    518             break;
    519           case 'p':  // 19-bit offset in addiupc.
    520             {
    521               int32_t offset = (instruction & 0x7ffff) - ((instruction & 0x40000) << 1);
    522               args << offset << "  ; move r" << rs << ", ";
    523               args << FormatInstructionPointer(instr_ptr + (offset << 2));
    524             }
    525             break;
    526           case 'S': args << 'r' << rs; break;
    527           case 's': args << 'f' << rs; break;
    528           case 'T': args << 'r' << rt; break;
    529           case 't': args << 'f' << rt; break;
    530           case 'Z': args << (rd + 1); break;  // sz ([d]ext size).
    531           case 'z': args << (rd - sa + 1); break;  // sz ([d]ins size).
    532         }
    533         if (*(args_fmt + 1)) {
    534           args << ", ";
    535         }
    536       }
    537       break;
    538     }
    539   }
    540 
    541   // Special cases for sequences of:
    542   //   pc-relative +/- 2GB branch:
    543   //     auipc  reg, imm
    544   //     jic    reg, imm
    545   //   pc-relative +/- 2GB branch and link:
    546   //     auipc  reg, imm
    547   //     daddiu reg, reg, imm
    548   //     jialc  reg, 0
    549   if (((op == 0x36 && rs == 0 && rt != 0) ||  // jic
    550        (op == 0x19 && rs == rt && rt != 0)) &&  // daddiu
    551       last_ptr_ && (intptr_t)instr_ptr - (intptr_t)last_ptr_ == 4 &&
    552       (last_instr_ & 0xFC1F0000) == 0xEC1E0000 &&  // auipc
    553       ((last_instr_ >> 21) & 0x1F) == rt) {
    554     uint32_t offset = (last_instr_ << 16) | (instruction & 0xFFFF);
    555     offset -= (offset & 0x8000) << 1;
    556     offset -= 4;
    557     if (op == 0x36) {
    558       args << "  ; b ";
    559     } else {
    560       args << "  ; move r" << rt << ", ";
    561     }
    562     args << FormatInstructionPointer(instr_ptr + (int32_t)offset);
    563     args << StringPrintf("  ; %+d", (int32_t)offset);
    564   }
    565 
    566   os << FormatInstructionPointer(instr_ptr)
    567      << StringPrintf(": %08x\t%-7s ", instruction, opcode.c_str())
    568      << args.str() << '\n';
    569   last_ptr_ = instr_ptr;
    570   last_instr_ = instruction;
    571   return 4;
    572 }
    573 
    574 void DisassemblerMips::Dump(std::ostream& os, const uint8_t* begin, const uint8_t* end) {
    575   for (const uint8_t* cur = begin; cur < end; cur += 4) {
    576     Dump(os, cur);
    577   }
    578 }
    579 
    580 }  // namespace mips
    581 }  // namespace art
    582