Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 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 | |
Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 19 | #include <ostream> |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 20 | #include <sstream> |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 21 | |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 22 | #include "base/logging.h" |
Elliott Hughes | e222ee0 | 2012-12-13 14:41:43 -0800 | [diff] [blame] | 23 | #include "base/stringprintf.h" |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 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)); |
Goran Jakovljevic | 403e0d5 | 2015-04-08 16:26:05 +0200 | [diff] [blame] | 47 | static const uint32_t kSpecial0Mask = (0x3f << kOpcodeShift); |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 48 | static const uint32_t kSpecial2Mask = (0x3f << kOpcodeShift); |
Chris Larsen | 2fadd7b | 2015-08-14 14:56:10 -0700 | [diff] [blame] | 49 | static const uint32_t kSpecial3Mask = (0x3f << kOpcodeShift); |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 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 |
Alexey Frunze | 92d9060 | 2015-12-18 18:16:36 -0800 | [diff] [blame] | 59 | { kRTypeMask | (0x1f << 21), 2, "srl", "DTA", }, |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 60 | { kRTypeMask, 3, "sra", "DTA", }, |
Chris Larsen | 7d4152f | 2015-10-19 15:17:16 -0700 | [diff] [blame] | 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", }, |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 65 | { kRTypeMask, 8, "jr", "S", }, |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 66 | { kRTypeMask | (0x1f << 11), 9 | (31 << 11), "jalr", "S", }, // rd = 31 is implicit. |
Goran Jakovljevic | 403e0d5 | 2015-04-08 16:26:05 +0200 | [diff] [blame] | 67 | { kRTypeMask | (0x1f << 11), 9, "jr", "S", }, // rd = 0 is implicit. |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 68 | { kRTypeMask, 9, "jalr", "DS", }, // General case. |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 69 | { kRTypeMask | (0x1f << 6), 10, "movz", "DST", }, |
| 70 | { kRTypeMask | (0x1f << 6), 11, "movn", "DST", }, |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 71 | { kRTypeMask, 12, "syscall", "", }, // TODO: code |
| 72 | { kRTypeMask, 13, "break", "", }, // TODO: code |
| 73 | { kRTypeMask, 15, "sync", "", }, // TODO: type |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 74 | { kRTypeMask, 16, "mfhi", "D", }, |
| 75 | { kRTypeMask, 17, "mthi", "S", }, |
| 76 | { kRTypeMask, 18, "mflo", "D", }, |
| 77 | { kRTypeMask, 19, "mtlo", "S", }, |
Chris Larsen | 7d4152f | 2015-10-19 15:17:16 -0700 | [diff] [blame] | 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", }, |
Douglas Leung | 027f0ff | 2015-02-27 19:05:03 -0800 | [diff] [blame] | 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", }, |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 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", }, |
Goran Jakovljevic | 403e0d5 | 2015-04-08 16:26:05 +0200 | [diff] [blame] | 100 | { kRTypeMask, 45, "daddu", "DST", }, |
| 101 | { kRTypeMask, 46, "dsub", "DST", }, |
| 102 | { kRTypeMask, 47, "dsubu", "DST", }, |
| 103 | // TODO: tge[u], tlt[u], teg, tne |
Chris Larsen | 7d4152f | 2015-10-19 15:17:16 -0700 | [diff] [blame] | 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", }, |
Goran Jakovljevic | 403e0d5 | 2015-04-08 16:26:05 +0200 | [diff] [blame] | 112 | |
| 113 | // SPECIAL0 |
Alexey Frunze | cd7b0ee | 2015-12-03 16:46:38 -0800 | [diff] [blame] | 114 | { kSpecial0Mask | 0x307ff, 1, "movf", "DSc" }, |
| 115 | { kSpecial0Mask | 0x307ff, 0x10001, "movt", "DSc" }, |
Goran Jakovljevic | 403e0d5 | 2015-04-08 16:26:05 +0200 | [diff] [blame] | 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" }, |
Chris Larsen | 2fadd7b | 2015-08-14 14:56:10 -0700 | [diff] [blame] | 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" }, |
Goran Jakovljevic | 403e0d5 | 2015-04-08 16:26:05 +0200 | [diff] [blame] | 139 | // TODO: sdbbp |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 140 | |
| 141 | // SPECIAL2 |
| 142 | { kSpecial2Mask | 0x7ff, (28 << kOpcodeShift) | 2, "mul", "DST" }, |
| 143 | { kSpecial2Mask | 0x7ff, (28 << kOpcodeShift) | 32, "clz", "DS" }, |
Chris Larsen | e384547 | 2015-11-18 12:27:15 -0800 | [diff] [blame] | 144 | { kSpecial2Mask | 0x7ff, (28 << kOpcodeShift) | 33, "clo", "DS" }, |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 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" }, |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 150 | { kSpecial2Mask | 0x3f, (28 << kOpcodeShift) | 0x3f, "sdbbp", "" }, // TODO: code |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 151 | |
Chris Larsen | 2fadd7b | 2015-08-14 14:56:10 -0700 | [diff] [blame] | 152 | // SPECIAL3 |
Alexey Frunze | 5c7aed3 | 2015-11-25 19:41:54 -0800 | [diff] [blame] | 153 | { kSpecial3Mask | 0x3f, (31 << kOpcodeShift), "ext", "TSAZ", }, |
Chris Larsen | 2fadd7b | 2015-08-14 14:56:10 -0700 | [diff] [blame] | 154 | { kSpecial3Mask | 0x3f, (31 << kOpcodeShift) | 3, "dext", "TSAZ", }, |
Alexey Frunze | 5c7aed3 | 2015-11-25 19:41:54 -0800 | [diff] [blame] | 155 | { kSpecial3Mask | 0x3f, (31 << kOpcodeShift) | 4, "ins", "TSAz", }, |
Chris Larsen | e384547 | 2015-11-18 12:27:15 -0800 | [diff] [blame] | 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", }, |
Chris Larsen | 2fadd7b | 2015-08-14 14:56:10 -0700 | [diff] [blame] | 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 | |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 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" }, |
Alexey Frunze | 4dda337 | 2015-06-01 18:31:49 -0700 | [diff] [blame] | 202 | { kITypeMask, 6 << kOpcodeShift, "bgeuc", "STB" }, |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 203 | { kITypeMask | (0x1f << 16), 7 << kOpcodeShift | (0 << 16), "bgtz", "SB" }, |
Alexey Frunze | 4dda337 | 2015-06-01 18:31:49 -0700 | [diff] [blame] | 204 | { kITypeMask, 7 << kOpcodeShift, "bltuc", "STB" }, |
Goran Jakovljevic | 403e0d5 | 2015-04-08 16:26:05 +0200 | [diff] [blame] | 205 | { kITypeMask | (0x1f << 16), 1 << kOpcodeShift | (6 << 16), "dahi", "Si", }, |
| 206 | { kITypeMask | (0x1f << 16), 1 << kOpcodeShift | (30 << 16), "dati", "Si", }, |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 207 | |
| 208 | { 0xffff0000, (4 << kOpcodeShift), "b", "B" }, |
| 209 | { 0xffff0000, (1 << kOpcodeShift) | (17 << 16), "bal", "B" }, |
| 210 | |
Alexey Frunze | 4dda337 | 2015-06-01 18:31:49 -0700 | [diff] [blame] | 211 | { kITypeMask, 8 << kOpcodeShift, "beqc", "STB" }, |
| 212 | |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 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", }, |
Goran Jakovljevic | 403e0d5 | 2015-04-08 16:26:05 +0200 | [diff] [blame] | 219 | { kITypeMask, 14 << kOpcodeShift, "xori", "TSi", }, |
| 220 | { kITypeMask | (0x1f << 21), 15 << kOpcodeShift, "lui", "TI", }, |
| 221 | { kITypeMask, 15 << kOpcodeShift, "aui", "TSI", }, |
Alexey Frunze | 4dda337 | 2015-06-01 18:31:49 -0700 | [diff] [blame] | 222 | |
Alexey Frunze | cd7b0ee | 2015-12-03 16:46:38 -0800 | [diff] [blame] | 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 | |
Alexey Frunze | 4dda337 | 2015-06-01 18:31:49 -0700 | [diff] [blame] | 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 | |
Goran Jakovljevic | 403e0d5 | 2015-04-08 16:26:05 +0200 | [diff] [blame] | 304 | { kITypeMask, 25 << kOpcodeShift, "daddiu", "TSi", }, |
| 305 | { kITypeMask, 29 << kOpcodeShift, "daui", "TSi", }, |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 306 | |
Brian Carlstrom | d74e41b | 2013-03-24 23:47:01 -0700 | [diff] [blame] | 307 | { kITypeMask, 32u << kOpcodeShift, "lb", "TO", }, |
| 308 | { kITypeMask, 33u << kOpcodeShift, "lh", "TO", }, |
| 309 | { kITypeMask, 35u << kOpcodeShift, "lw", "TO", }, |
| 310 | { kITypeMask, 36u << kOpcodeShift, "lbu", "TO", }, |
| 311 | { kITypeMask, 37u << kOpcodeShift, "lhu", "TO", }, |
Goran Jakovljevic | 403e0d5 | 2015-04-08 16:26:05 +0200 | [diff] [blame] | 312 | { kITypeMask, 39u << kOpcodeShift, "lwu", "TO", }, |
Brian Carlstrom | d74e41b | 2013-03-24 23:47:01 -0700 | [diff] [blame] | 313 | { kITypeMask, 40u << kOpcodeShift, "sb", "TO", }, |
| 314 | { kITypeMask, 41u << kOpcodeShift, "sh", "TO", }, |
| 315 | { kITypeMask, 43u << kOpcodeShift, "sw", "TO", }, |
| 316 | { kITypeMask, 49u << kOpcodeShift, "lwc1", "tO", }, |
Goran Jakovljevic | 8c434dc | 2015-08-26 14:39:44 +0200 | [diff] [blame] | 317 | { kJTypeMask, 50u << kOpcodeShift, "bc", "P" }, |
Andreas Gampe | 8d36591 | 2015-01-13 11:32:32 -0800 | [diff] [blame] | 318 | { kITypeMask, 53u << kOpcodeShift, "ldc1", "tO", }, |
Alexey Frunze | 4dda337 | 2015-06-01 18:31:49 -0700 | [diff] [blame] | 319 | { kITypeMask | (0x1f << 21), 54u << kOpcodeShift, "jic", "Ti" }, |
| 320 | { kITypeMask | (1 << 21), (54u << kOpcodeShift) | (1 << 21), "beqzc", "Sb" }, // TODO: de-dup? |
| 321 | { kITypeMask | (1 << 22), (54u << kOpcodeShift) | (1 << 22), "beqzc", "Sb" }, |
| 322 | { kITypeMask | (1 << 23), (54u << kOpcodeShift) | (1 << 23), "beqzc", "Sb" }, |
| 323 | { kITypeMask | (1 << 24), (54u << kOpcodeShift) | (1 << 24), "beqzc", "Sb" }, |
| 324 | { kITypeMask | (1 << 25), (54u << kOpcodeShift) | (1 << 25), "beqzc", "Sb" }, |
Goran Jakovljevic | 403e0d5 | 2015-04-08 16:26:05 +0200 | [diff] [blame] | 325 | { kITypeMask, 55u << kOpcodeShift, "ld", "TO", }, |
Brian Carlstrom | d74e41b | 2013-03-24 23:47:01 -0700 | [diff] [blame] | 326 | { kITypeMask, 57u << kOpcodeShift, "swc1", "tO", }, |
Alexey Frunze | 4dda337 | 2015-06-01 18:31:49 -0700 | [diff] [blame] | 327 | { kITypeMask | (0x1f << 16), (59u << kOpcodeShift) | (30 << 16), "auipc", "Si" }, |
Goran Jakovljevic | 8c434dc | 2015-08-26 14:39:44 +0200 | [diff] [blame] | 328 | { kITypeMask | (0x3 << 19), (59u << kOpcodeShift) | (0 << 19), "addiupc", "Sp" }, |
Andreas Gampe | 8d36591 | 2015-01-13 11:32:32 -0800 | [diff] [blame] | 329 | { kITypeMask, 61u << kOpcodeShift, "sdc1", "tO", }, |
Alexey Frunze | 4dda337 | 2015-06-01 18:31:49 -0700 | [diff] [blame] | 330 | { kITypeMask | (0x1f << 21), 62u << kOpcodeShift, "jialc", "Ti" }, |
| 331 | { kITypeMask | (1 << 21), (62u << kOpcodeShift) | (1 << 21), "bnezc", "Sb" }, // TODO: de-dup? |
| 332 | { kITypeMask | (1 << 22), (62u << kOpcodeShift) | (1 << 22), "bnezc", "Sb" }, |
| 333 | { kITypeMask | (1 << 23), (62u << kOpcodeShift) | (1 << 23), "bnezc", "Sb" }, |
| 334 | { kITypeMask | (1 << 24), (62u << kOpcodeShift) | (1 << 24), "bnezc", "Sb" }, |
| 335 | { kITypeMask | (1 << 25), (62u << kOpcodeShift) | (1 << 25), "bnezc", "Sb" }, |
Goran Jakovljevic | 403e0d5 | 2015-04-08 16:26:05 +0200 | [diff] [blame] | 336 | { kITypeMask, 63u << kOpcodeShift, "sd", "TO", }, |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 337 | |
| 338 | // Floating point. |
Goran Jakovljevic | 403e0d5 | 2015-04-08 16:26:05 +0200 | [diff] [blame] | 339 | { kFpMask | (0x1f << 21), kCop1 | (0x00 << 21), "mfc1", "Td" }, |
| 340 | { kFpMask | (0x1f << 21), kCop1 | (0x01 << 21), "dmfc1", "Td" }, |
| 341 | { kFpMask | (0x1f << 21), kCop1 | (0x03 << 21), "mfhc1", "Td" }, |
| 342 | { kFpMask | (0x1f << 21), kCop1 | (0x04 << 21), "mtc1", "Td" }, |
| 343 | { kFpMask | (0x1f << 21), kCop1 | (0x05 << 21), "dmtc1", "Td" }, |
| 344 | { kFpMask | (0x1f << 21), kCop1 | (0x07 << 21), "mthc1", "Td" }, |
Alexey Frunze | cd7b0ee | 2015-12-03 16:46:38 -0800 | [diff] [blame] | 345 | { kFpMask | (0x1f << 21), kCop1 | (0x14 << 21) | 1, "cmp.un.s", "adt" }, |
| 346 | { kFpMask | (0x1f << 21), kCop1 | (0x14 << 21) | 2, "cmp.eq.s", "adt" }, |
| 347 | { kFpMask | (0x1f << 21), kCop1 | (0x14 << 21) | 3, "cmp.ueq.s", "adt" }, |
| 348 | { kFpMask | (0x1f << 21), kCop1 | (0x14 << 21) | 4, "cmp.lt.s", "adt" }, |
| 349 | { kFpMask | (0x1f << 21), kCop1 | (0x14 << 21) | 5, "cmp.ult.s", "adt" }, |
| 350 | { kFpMask | (0x1f << 21), kCop1 | (0x14 << 21) | 6, "cmp.le.s", "adt" }, |
| 351 | { kFpMask | (0x1f << 21), kCop1 | (0x14 << 21) | 7, "cmp.ule.s", "adt" }, |
| 352 | { kFpMask | (0x1f << 21), kCop1 | (0x14 << 21) | 17, "cmp.or.s", "adt" }, |
| 353 | { kFpMask | (0x1f << 21), kCop1 | (0x14 << 21) | 18, "cmp.une.s", "adt" }, |
| 354 | { kFpMask | (0x1f << 21), kCop1 | (0x14 << 21) | 19, "cmp.ne.s", "adt" }, |
| 355 | { kFpMask | (0x1f << 21), kCop1 | (0x15 << 21) | 1, "cmp.un.d", "adt" }, |
| 356 | { kFpMask | (0x1f << 21), kCop1 | (0x15 << 21) | 2, "cmp.eq.d", "adt" }, |
| 357 | { kFpMask | (0x1f << 21), kCop1 | (0x15 << 21) | 3, "cmp.ueq.d", "adt" }, |
| 358 | { kFpMask | (0x1f << 21), kCop1 | (0x15 << 21) | 4, "cmp.lt.d", "adt" }, |
| 359 | { kFpMask | (0x1f << 21), kCop1 | (0x15 << 21) | 5, "cmp.ult.d", "adt" }, |
| 360 | { kFpMask | (0x1f << 21), kCop1 | (0x15 << 21) | 6, "cmp.le.d", "adt" }, |
| 361 | { kFpMask | (0x1f << 21), kCop1 | (0x15 << 21) | 7, "cmp.ule.d", "adt" }, |
| 362 | { kFpMask | (0x1f << 21), kCop1 | (0x15 << 21) | 17, "cmp.or.d", "adt" }, |
| 363 | { kFpMask | (0x1f << 21), kCop1 | (0x15 << 21) | 18, "cmp.une.d", "adt" }, |
| 364 | { kFpMask | (0x1f << 21), kCop1 | (0x15 << 21) | 19, "cmp.ne.d", "adt" }, |
Douglas Leung | 1cd2790 | 2015-02-13 16:55:57 -0800 | [diff] [blame] | 365 | { kFpMask | (0x10 << 21), kCop1 | (0x10 << 21) | 0, "add", "fadt" }, |
| 366 | { kFpMask | (0x10 << 21), kCop1 | (0x10 << 21) | 1, "sub", "fadt" }, |
| 367 | { kFpMask | (0x10 << 21), kCop1 | (0x10 << 21) | 2, "mul", "fadt" }, |
| 368 | { kFpMask | (0x10 << 21), kCop1 | (0x10 << 21) | 3, "div", "fadt" }, |
| 369 | { kFpMask | (0x10 << 21), kCop1 | (0x10 << 21) | 4, "sqrt", "fad" }, |
| 370 | { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 5, "abs", "fad" }, |
| 371 | { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 6, "mov", "fad" }, |
| 372 | { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 7, "neg", "fad" }, |
| 373 | { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 8, "round.l", "fad" }, |
| 374 | { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 9, "trunc.l", "fad" }, |
| 375 | { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 10, "ceil.l", "fad" }, |
| 376 | { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 11, "floor.l", "fad" }, |
| 377 | { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 12, "round.w", "fad" }, |
| 378 | { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 13, "trunc.w", "fad" }, |
| 379 | { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 14, "ceil.w", "fad" }, |
| 380 | { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 15, "floor.w", "fad" }, |
Chris Larsen | 2fadd7b | 2015-08-14 14:56:10 -0700 | [diff] [blame] | 381 | { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 26, "rint", "fad" }, |
| 382 | { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 27, "class", "fad" }, |
Douglas Leung | 1cd2790 | 2015-02-13 16:55:57 -0800 | [diff] [blame] | 383 | { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 32, "cvt.s", "fad" }, |
| 384 | { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 33, "cvt.d", "fad" }, |
| 385 | { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 36, "cvt.w", "fad" }, |
| 386 | { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 37, "cvt.l", "fad" }, |
| 387 | { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 38, "cvt.ps", "fad" }, |
Alexey Frunze | cd7b0ee | 2015-12-03 16:46:38 -0800 | [diff] [blame] | 388 | { kFpMask | (0x10 << 21), kCop1 | (0x10 << 21) | 49, "c.un", "fCdt" }, |
| 389 | { kFpMask | (0x10 << 21), kCop1 | (0x10 << 21) | 50, "c.eq", "fCdt" }, |
| 390 | { kFpMask | (0x10 << 21), kCop1 | (0x10 << 21) | 51, "c.ueq", "fCdt" }, |
| 391 | { kFpMask | (0x10 << 21), kCop1 | (0x10 << 21) | 52, "c.olt", "fCdt" }, |
| 392 | { kFpMask | (0x10 << 21), kCop1 | (0x10 << 21) | 53, "c.ult", "fCdt" }, |
| 393 | { kFpMask | (0x10 << 21), kCop1 | (0x10 << 21) | 54, "c.ole", "fCdt" }, |
| 394 | { kFpMask | (0x10 << 21), kCop1 | (0x10 << 21) | 55, "c.ule", "fCdt" }, |
Chris Larsen | 2fadd7b | 2015-08-14 14:56:10 -0700 | [diff] [blame] | 395 | { kFpMask, kCop1 | 0x10, "sel", "fadt" }, |
| 396 | { kFpMask, kCop1 | 0x1e, "max", "fadt" }, |
| 397 | { kFpMask, kCop1 | 0x1c, "min", "fadt" }, |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 398 | }; |
| 399 | |
| 400 | static uint32_t ReadU32(const uint8_t* ptr) { |
jeffhao | 4f8f04a | 2012-10-02 18:10:35 -0700 | [diff] [blame] | 401 | // We only support little-endian MIPS. |
| 402 | return ptr[0] | (ptr[1] << 8) | (ptr[2] << 16) | (ptr[3] << 24); |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 403 | } |
| 404 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 405 | size_t DisassemblerMips::Dump(std::ostream& os, const uint8_t* instr_ptr) { |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 406 | uint32_t instruction = ReadU32(instr_ptr); |
| 407 | |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 408 | uint32_t rs = (instruction >> 21) & 0x1f; // I-type, R-type. |
| 409 | uint32_t rt = (instruction >> 16) & 0x1f; // I-type, R-type. |
| 410 | uint32_t rd = (instruction >> 11) & 0x1f; // R-type. |
| 411 | uint32_t sa = (instruction >> 6) & 0x1f; // R-type. |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 412 | |
| 413 | std::string opcode; |
| 414 | std::ostringstream args; |
| 415 | |
| 416 | // TODO: remove this! |
| 417 | uint32_t op = (instruction >> 26) & 0x3f; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 418 | uint32_t function = (instruction & 0x3f); // R-type. |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 419 | opcode = StringPrintf("op=%d fn=%d", op, function); |
| 420 | |
| 421 | for (size_t i = 0; i < arraysize(gMipsInstructions); ++i) { |
| 422 | if (gMipsInstructions[i].Matches(instruction)) { |
| 423 | opcode = gMipsInstructions[i].name; |
| 424 | for (const char* args_fmt = gMipsInstructions[i].args_fmt; *args_fmt; ++args_fmt) { |
| 425 | switch (*args_fmt) { |
Alexey Frunze | 5c7aed3 | 2015-11-25 19:41:54 -0800 | [diff] [blame] | 426 | case 'A': // sa (shift amount or [d]ins/[d]ext position). |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 427 | args << sa; |
| 428 | break; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 429 | case 'B': // Branch offset. |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 430 | { |
| 431 | int32_t offset = static_cast<int16_t>(instruction & 0xffff); |
| 432 | offset <<= 2; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 433 | offset += 4; // Delay slot. |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 434 | args << FormatInstructionPointer(instr_ptr + offset) |
| 435 | << StringPrintf(" ; %+d", offset); |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 436 | } |
| 437 | break; |
Alexey Frunze | 4dda337 | 2015-06-01 18:31:49 -0700 | [diff] [blame] | 438 | case 'b': // 21-bit branch offset. |
| 439 | { |
| 440 | int32_t offset = (instruction & 0x1fffff) - ((instruction & 0x100000) << 1); |
| 441 | offset <<= 2; |
| 442 | offset += 4; // Delay slot. |
| 443 | args << FormatInstructionPointer(instr_ptr + offset) |
| 444 | << StringPrintf(" ; %+d", offset); |
| 445 | } |
| 446 | break; |
Alexey Frunze | cd7b0ee | 2015-12-03 16:46:38 -0800 | [diff] [blame] | 447 | case 'C': // Floating-point condition code flag in c.<cond>.fmt. |
| 448 | args << "cc" << (sa >> 2); |
| 449 | break; |
| 450 | case 'c': // Floating-point condition code flag in bc1f/bc1t and movf/movt. |
| 451 | args << "cc" << (rt >> 2); |
| 452 | break; |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 453 | case 'D': args << 'r' << rd; break; |
| 454 | case 'd': args << 'f' << rd; break; |
Douglas Leung | 1cd2790 | 2015-02-13 16:55:57 -0800 | [diff] [blame] | 455 | case 'a': args << 'f' << sa; break; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 456 | case 'f': // Floating point "fmt". |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 457 | { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 458 | size_t fmt = (instruction >> 21) & 0x7; // TODO: other fmts? |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 459 | switch (fmt) { |
| 460 | case 0: opcode += ".s"; break; |
| 461 | case 1: opcode += ".d"; break; |
| 462 | case 4: opcode += ".w"; break; |
| 463 | case 5: opcode += ".l"; break; |
| 464 | case 6: opcode += ".ps"; break; |
| 465 | default: opcode += ".?"; break; |
| 466 | } |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 467 | continue; // No ", ". |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 468 | } |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 469 | case 'I': // Upper 16-bit immediate. |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 470 | args << reinterpret_cast<void*>((instruction & 0xffff) << 16); |
| 471 | break; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 472 | case 'i': // Sign-extended lower 16-bit immediate. |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 473 | args << static_cast<int16_t>(instruction & 0xffff); |
| 474 | break; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 475 | case 'L': // Jump label. |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 476 | { |
| 477 | // TODO: is this right? |
| 478 | uint32_t instr_index = (instruction & 0x1ffffff); |
| 479 | uint32_t target = (instr_index << 2); |
| 480 | target |= (reinterpret_cast<uintptr_t>(instr_ptr + 4) & 0xf0000000); |
| 481 | args << reinterpret_cast<void*>(target); |
| 482 | } |
| 483 | break; |
Chris Larsen | 2fadd7b | 2015-08-14 14:56:10 -0700 | [diff] [blame] | 484 | case 'l': // 9-bit signed offset |
| 485 | { |
| 486 | int32_t offset = static_cast<int16_t>(instruction) >> 7; |
| 487 | args << StringPrintf("%+d(r%d)", offset, rs); |
| 488 | } |
| 489 | break; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 490 | case 'O': // +x(rs) |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 491 | { |
| 492 | int32_t offset = static_cast<int16_t>(instruction & 0xffff); |
| 493 | args << StringPrintf("%+d(r%d)", offset, rs); |
| 494 | if (rs == 17) { |
| 495 | args << " ; "; |
Goran Jakovljevic | 403e0d5 | 2015-04-08 16:26:05 +0200 | [diff] [blame] | 496 | if (is64bit_) { |
| 497 | Thread::DumpThreadOffset<8>(args, offset); |
| 498 | } else { |
| 499 | Thread::DumpThreadOffset<4>(args, offset); |
| 500 | } |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 501 | } |
| 502 | } |
| 503 | break; |
Goran Jakovljevic | 8c434dc | 2015-08-26 14:39:44 +0200 | [diff] [blame] | 504 | case 'P': // 26-bit offset in bc. |
| 505 | { |
| 506 | int32_t offset = (instruction & 0x3ffffff) - ((instruction & 0x2000000) << 1); |
| 507 | offset <<= 2; |
| 508 | offset += 4; |
| 509 | args << FormatInstructionPointer(instr_ptr + offset); |
| 510 | args << StringPrintf(" ; %+d", offset); |
| 511 | } |
| 512 | break; |
| 513 | case 'p': // 19-bit offset in addiupc. |
| 514 | { |
| 515 | int32_t offset = (instruction & 0x7ffff) - ((instruction & 0x40000) << 1); |
| 516 | args << offset << " ; move r" << rs << ", "; |
| 517 | args << FormatInstructionPointer(instr_ptr + (offset << 2)); |
| 518 | } |
| 519 | break; |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 520 | case 'S': args << 'r' << rs; break; |
| 521 | case 's': args << 'f' << rs; break; |
| 522 | case 'T': args << 'r' << rt; break; |
| 523 | case 't': args << 'f' << rt; break; |
Alexey Frunze | 5c7aed3 | 2015-11-25 19:41:54 -0800 | [diff] [blame] | 524 | case 'Z': args << (rd + 1); break; // sz ([d]ext size). |
| 525 | case 'z': args << (rd - sa + 1); break; // sz ([d]ins size). |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 526 | } |
| 527 | if (*(args_fmt + 1)) { |
| 528 | args << ", "; |
| 529 | } |
| 530 | } |
| 531 | break; |
| 532 | } |
| 533 | } |
| 534 | |
Alexey Frunze | 4dda337 | 2015-06-01 18:31:49 -0700 | [diff] [blame] | 535 | // Special cases for sequences of: |
| 536 | // pc-relative +/- 2GB branch: |
| 537 | // auipc reg, imm |
| 538 | // jic reg, imm |
| 539 | // pc-relative +/- 2GB branch and link: |
| 540 | // auipc reg, imm |
| 541 | // daddiu reg, reg, imm |
| 542 | // jialc reg, 0 |
| 543 | if (((op == 0x36 && rs == 0 && rt != 0) || // jic |
| 544 | (op == 0x19 && rs == rt && rt != 0)) && // daddiu |
| 545 | last_ptr_ && (intptr_t)instr_ptr - (intptr_t)last_ptr_ == 4 && |
| 546 | (last_instr_ & 0xFC1F0000) == 0xEC1E0000 && // auipc |
| 547 | ((last_instr_ >> 21) & 0x1F) == rt) { |
| 548 | uint32_t offset = (last_instr_ << 16) | (instruction & 0xFFFF); |
| 549 | offset -= (offset & 0x8000) << 1; |
| 550 | offset -= 4; |
| 551 | if (op == 0x36) { |
| 552 | args << " ; b "; |
| 553 | } else { |
| 554 | args << " ; move r" << rt << ", "; |
| 555 | } |
| 556 | args << FormatInstructionPointer(instr_ptr + (int32_t)offset); |
| 557 | args << StringPrintf(" ; %+d", (int32_t)offset); |
| 558 | } |
| 559 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 560 | os << FormatInstructionPointer(instr_ptr) |
| 561 | << StringPrintf(": %08x\t%-7s ", instruction, opcode.c_str()) |
| 562 | << args.str() << '\n'; |
Alexey Frunze | 4dda337 | 2015-06-01 18:31:49 -0700 | [diff] [blame] | 563 | last_ptr_ = instr_ptr; |
| 564 | last_instr_ = instruction; |
Ian Rogers | b23a772 | 2012-10-09 16:54:26 -0700 | [diff] [blame] | 565 | return 4; |
| 566 | } |
| 567 | |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 568 | void DisassemblerMips::Dump(std::ostream& os, const uint8_t* begin, const uint8_t* end) { |
| 569 | for (const uint8_t* cur = begin; cur < end; cur += 4) { |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 570 | Dump(os, cur); |
Elliott Hughes | 60454e8 | 2012-04-25 12:56:03 -0700 | [diff] [blame] | 571 | } |
| 572 | } |
| 573 | |
| 574 | } // namespace mips |
| 575 | } // namespace art |