Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 4 | |
| 5 | #ifndef V8_MIPS_CONSTANTS_H_ |
| 6 | #define V8_MIPS_CONSTANTS_H_ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7 | #include "src/globals.h" |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 8 | // UNIMPLEMENTED_ macro for MIPS. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9 | #ifdef DEBUG |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 10 | #define UNIMPLEMENTED_MIPS() \ |
| 11 | v8::internal::PrintF("%s, \tline %d: \tfunction %s not implemented. \n", \ |
| 12 | __FILE__, __LINE__, __func__) |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13 | #else |
| 14 | #define UNIMPLEMENTED_MIPS() |
| 15 | #endif |
| 16 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 17 | #define UNSUPPORTED_MIPS() v8::internal::PrintF("Unsupported instruction.\n") |
| 18 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 19 | enum ArchVariants { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 20 | kMips32r1 = v8::internal::MIPSr1, |
| 21 | kMips32r2 = v8::internal::MIPSr2, |
| 22 | kMips32r6 = v8::internal::MIPSr6, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 23 | kLoongson |
| 24 | }; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 25 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 26 | #ifdef _MIPS_ARCH_MIPS32R2 |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 27 | static const ArchVariants kArchVariant = kMips32r2; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 28 | #elif _MIPS_ARCH_MIPS32R6 |
| 29 | static const ArchVariants kArchVariant = kMips32r6; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 30 | #elif _MIPS_ARCH_LOONGSON |
| 31 | // The loongson flag refers to the LOONGSON architectures based on MIPS-III, |
| 32 | // which predates (and is a subset of) the mips32r2 and r1 architectures. |
| 33 | static const ArchVariants kArchVariant = kLoongson; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 34 | #elif _MIPS_ARCH_MIPS32RX |
| 35 | // This flags referred to compatibility mode that creates universal code that |
| 36 | // can run on any MIPS32 architecture revision. The dynamically generated code |
| 37 | // by v8 is specialized for the MIPS host detected in runtime probing. |
| 38 | static const ArchVariants kArchVariant = kMips32r1; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 39 | #else |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 40 | static const ArchVariants kArchVariant = kMips32r1; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 41 | #endif |
| 42 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 43 | enum Endianness { |
| 44 | kLittle, |
| 45 | kBig |
| 46 | }; |
| 47 | |
| 48 | #if defined(V8_TARGET_LITTLE_ENDIAN) |
| 49 | static const Endianness kArchEndian = kLittle; |
| 50 | #elif defined(V8_TARGET_BIG_ENDIAN) |
| 51 | static const Endianness kArchEndian = kBig; |
| 52 | #else |
| 53 | #error Unknown endianness |
| 54 | #endif |
| 55 | |
| 56 | enum FpuMode { |
| 57 | kFP32, |
| 58 | kFP64, |
| 59 | kFPXX |
| 60 | }; |
| 61 | |
| 62 | #if defined(FPU_MODE_FP32) |
| 63 | static const FpuMode kFpuMode = kFP32; |
| 64 | #elif defined(FPU_MODE_FP64) |
| 65 | static const FpuMode kFpuMode = kFP64; |
| 66 | #elif defined(FPU_MODE_FPXX) |
| 67 | static const FpuMode kFpuMode = kFPXX; |
| 68 | #else |
| 69 | static const FpuMode kFpuMode = kFP32; |
| 70 | #endif |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 71 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 72 | #if(defined(__mips_hard_float) && __mips_hard_float != 0) |
| 73 | // Use floating-point coprocessor instructions. This flag is raised when |
| 74 | // -mhard-float is passed to the compiler. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 75 | const bool IsMipsSoftFloatABI = false; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 76 | #elif(defined(__mips_soft_float) && __mips_soft_float != 0) |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 77 | // This flag is raised when -msoft-float is passed to the compiler. |
| 78 | // Although FPU is a base requirement for v8, soft-float ABI is used |
| 79 | // on soft-float systems with FPU kernel emulation. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 80 | const bool IsMipsSoftFloatABI = true; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 81 | #else |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 82 | const bool IsMipsSoftFloatABI = true; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 83 | #endif |
| 84 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 85 | #if defined(V8_TARGET_LITTLE_ENDIAN) |
| 86 | const uint32_t kHoleNanUpper32Offset = 4; |
| 87 | const uint32_t kHoleNanLower32Offset = 0; |
| 88 | #elif defined(V8_TARGET_BIG_ENDIAN) |
| 89 | const uint32_t kHoleNanUpper32Offset = 0; |
| 90 | const uint32_t kHoleNanLower32Offset = 4; |
| 91 | #else |
| 92 | #error Unknown endianness |
| 93 | #endif |
| 94 | |
| 95 | #ifndef FPU_MODE_FPXX |
| 96 | #define IsFp64Mode() \ |
| 97 | (kFpuMode == kFP64) |
| 98 | #else |
| 99 | #define IsFp64Mode() \ |
| 100 | (CpuFeatures::IsSupported(FP64FPU)) |
| 101 | #endif |
| 102 | |
| 103 | #ifndef _MIPS_ARCH_MIPS32RX |
| 104 | #define IsMipsArchVariant(check) \ |
| 105 | (kArchVariant == check) |
| 106 | #else |
| 107 | #define IsMipsArchVariant(check) \ |
Paul Lind | 18a7ebb | 2014-12-17 22:29:32 -0800 | [diff] [blame^] | 108 | (CpuFeatures::IsSupported(static_cast<CpuFeature>(check))) |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 109 | #endif |
| 110 | |
| 111 | |
| 112 | #define __STDC_FORMAT_MACROS |
| 113 | #include <inttypes.h> |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 114 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 115 | // Defines constants and accessor classes to assemble, disassemble and |
| 116 | // simulate MIPS32 instructions. |
| 117 | // |
| 118 | // See: MIPS32 Architecture For Programmers |
| 119 | // Volume II: The MIPS32 Instruction Set |
| 120 | // Try www.cs.cornell.edu/courses/cs3410/2008fa/MIPS_Vol2.pdf. |
| 121 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 122 | namespace v8 { |
| 123 | namespace internal { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 124 | |
| 125 | // ----------------------------------------------------------------------------- |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 126 | // Registers and FPURegisters. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 127 | |
| 128 | // Number of general purpose registers. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 129 | const int kNumRegisters = 32; |
| 130 | const int kInvalidRegister = -1; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 131 | |
| 132 | // Number of registers with HI, LO, and pc. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 133 | const int kNumSimuRegisters = 35; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 134 | |
| 135 | // In the simulator, the PC register is simulated as the 34th register. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 136 | const int kPCRegister = 34; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 137 | |
| 138 | // Number coprocessor registers. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 139 | const int kNumFPURegisters = 32; |
| 140 | const int kInvalidFPURegister = -1; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 141 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 142 | // FPU (coprocessor 1) control registers. Currently only FCSR is implemented. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 143 | const int kFCSRRegister = 31; |
| 144 | const int kInvalidFPUControlRegister = -1; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 145 | const uint32_t kFPUInvalidResult = static_cast<uint32_t>(1 << 31) - 1; |
| 146 | const uint64_t kFPU64InvalidResult = |
| 147 | static_cast<uint64_t>(static_cast<uint64_t>(1) << 63) - 1; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 148 | |
| 149 | // FCSR constants. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 150 | const uint32_t kFCSRInexactFlagBit = 2; |
| 151 | const uint32_t kFCSRUnderflowFlagBit = 3; |
| 152 | const uint32_t kFCSROverflowFlagBit = 4; |
| 153 | const uint32_t kFCSRDivideByZeroFlagBit = 5; |
| 154 | const uint32_t kFCSRInvalidOpFlagBit = 6; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 155 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 156 | const uint32_t kFCSRInexactFlagMask = 1 << kFCSRInexactFlagBit; |
| 157 | const uint32_t kFCSRUnderflowFlagMask = 1 << kFCSRUnderflowFlagBit; |
| 158 | const uint32_t kFCSROverflowFlagMask = 1 << kFCSROverflowFlagBit; |
| 159 | const uint32_t kFCSRDivideByZeroFlagMask = 1 << kFCSRDivideByZeroFlagBit; |
| 160 | const uint32_t kFCSRInvalidOpFlagMask = 1 << kFCSRInvalidOpFlagBit; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 161 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 162 | const uint32_t kFCSRFlagMask = |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 163 | kFCSRInexactFlagMask | |
| 164 | kFCSRUnderflowFlagMask | |
| 165 | kFCSROverflowFlagMask | |
| 166 | kFCSRDivideByZeroFlagMask | |
| 167 | kFCSRInvalidOpFlagMask; |
| 168 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 169 | const uint32_t kFCSRExceptionFlagMask = kFCSRFlagMask ^ kFCSRInexactFlagMask; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 170 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 171 | // 'pref' instruction hints |
| 172 | const int32_t kPrefHintLoad = 0; |
| 173 | const int32_t kPrefHintStore = 1; |
| 174 | const int32_t kPrefHintLoadStreamed = 4; |
| 175 | const int32_t kPrefHintStoreStreamed = 5; |
| 176 | const int32_t kPrefHintLoadRetained = 6; |
| 177 | const int32_t kPrefHintStoreRetained = 7; |
| 178 | const int32_t kPrefHintWritebackInvalidate = 25; |
| 179 | const int32_t kPrefHintPrepareForStore = 30; |
| 180 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 181 | // Helper functions for converting between register numbers and names. |
| 182 | class Registers { |
| 183 | public: |
| 184 | // Return the name of the register. |
| 185 | static const char* Name(int reg); |
| 186 | |
| 187 | // Lookup the register number for the name provided. |
| 188 | static int Number(const char* name); |
| 189 | |
| 190 | struct RegisterAlias { |
| 191 | int reg; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 192 | const char* name; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 193 | }; |
| 194 | |
| 195 | static const int32_t kMaxValue = 0x7fffffff; |
| 196 | static const int32_t kMinValue = 0x80000000; |
| 197 | |
| 198 | private: |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 199 | static const char* names_[kNumSimuRegisters]; |
| 200 | static const RegisterAlias aliases_[]; |
| 201 | }; |
| 202 | |
| 203 | // Helper functions for converting between register numbers and names. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 204 | class FPURegisters { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 205 | public: |
| 206 | // Return the name of the register. |
| 207 | static const char* Name(int reg); |
| 208 | |
| 209 | // Lookup the register number for the name provided. |
| 210 | static int Number(const char* name); |
| 211 | |
| 212 | struct RegisterAlias { |
| 213 | int creg; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 214 | const char* name; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 215 | }; |
| 216 | |
| 217 | private: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 218 | static const char* names_[kNumFPURegisters]; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 219 | static const RegisterAlias aliases_[]; |
| 220 | }; |
| 221 | |
| 222 | |
| 223 | // ----------------------------------------------------------------------------- |
| 224 | // Instructions encoding constants. |
| 225 | |
| 226 | // On MIPS all instructions are 32 bits. |
| 227 | typedef int32_t Instr; |
| 228 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 229 | // Special Software Interrupt codes when used in the presence of the MIPS |
| 230 | // simulator. |
| 231 | enum SoftwareInterruptCodes { |
| 232 | // Transition to C code. |
| 233 | call_rt_redirected = 0xfffff |
| 234 | }; |
| 235 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 236 | // On MIPS Simulator breakpoints can have different codes: |
| 237 | // - Breaks between 0 and kMaxWatchpointCode are treated as simple watchpoints, |
| 238 | // the simulator will run through them and print the registers. |
| 239 | // - Breaks between kMaxWatchpointCode and kMaxStopCode are treated as stop() |
| 240 | // instructions (see Assembler::stop()). |
| 241 | // - Breaks larger than kMaxStopCode are simple breaks, dropping you into the |
| 242 | // debugger. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 243 | const uint32_t kMaxWatchpointCode = 31; |
| 244 | const uint32_t kMaxStopCode = 127; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 245 | STATIC_ASSERT(kMaxWatchpointCode < kMaxStopCode); |
| 246 | |
| 247 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 248 | // ----- Fields offset and length. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 249 | const int kOpcodeShift = 26; |
| 250 | const int kOpcodeBits = 6; |
| 251 | const int kRsShift = 21; |
| 252 | const int kRsBits = 5; |
| 253 | const int kRtShift = 16; |
| 254 | const int kRtBits = 5; |
| 255 | const int kRdShift = 11; |
| 256 | const int kRdBits = 5; |
| 257 | const int kSaShift = 6; |
| 258 | const int kSaBits = 5; |
| 259 | const int kFunctionShift = 0; |
| 260 | const int kFunctionBits = 6; |
| 261 | const int kLuiShift = 16; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 262 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 263 | const int kImm16Shift = 0; |
| 264 | const int kImm16Bits = 16; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 265 | const int kImm21Shift = 0; |
| 266 | const int kImm21Bits = 21; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 267 | const int kImm26Shift = 0; |
| 268 | const int kImm26Bits = 26; |
| 269 | const int kImm28Shift = 0; |
| 270 | const int kImm28Bits = 28; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 271 | const int kImm32Shift = 0; |
| 272 | const int kImm32Bits = 32; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 273 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 274 | // In branches and jumps immediate fields point to words, not bytes, |
| 275 | // and are therefore shifted by 2. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 276 | const int kImmFieldShift = 2; |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 277 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 278 | const int kFrBits = 5; |
| 279 | const int kFrShift = 21; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 280 | const int kFsShift = 11; |
| 281 | const int kFsBits = 5; |
| 282 | const int kFtShift = 16; |
| 283 | const int kFtBits = 5; |
| 284 | const int kFdShift = 6; |
| 285 | const int kFdBits = 5; |
| 286 | const int kFCccShift = 8; |
| 287 | const int kFCccBits = 3; |
| 288 | const int kFBccShift = 18; |
| 289 | const int kFBccBits = 3; |
| 290 | const int kFBtrueShift = 16; |
| 291 | const int kFBtrueBits = 1; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 292 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 293 | // ----- Miscellaneous useful masks. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 294 | // Instruction bit masks. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 295 | const int kOpcodeMask = ((1 << kOpcodeBits) - 1) << kOpcodeShift; |
| 296 | const int kImm16Mask = ((1 << kImm16Bits) - 1) << kImm16Shift; |
| 297 | const int kImm26Mask = ((1 << kImm26Bits) - 1) << kImm26Shift; |
| 298 | const int kImm28Mask = ((1 << kImm28Bits) - 1) << kImm28Shift; |
| 299 | const int kRsFieldMask = ((1 << kRsBits) - 1) << kRsShift; |
| 300 | const int kRtFieldMask = ((1 << kRtBits) - 1) << kRtShift; |
| 301 | const int kRdFieldMask = ((1 << kRdBits) - 1) << kRdShift; |
| 302 | const int kSaFieldMask = ((1 << kSaBits) - 1) << kSaShift; |
| 303 | const int kFunctionFieldMask = ((1 << kFunctionBits) - 1) << kFunctionShift; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 304 | // Misc masks. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 305 | const int kHiMask = 0xffff << 16; |
| 306 | const int kLoMask = 0xffff; |
| 307 | const int kSignMask = 0x80000000; |
| 308 | const int kJumpAddrMask = (1 << (kImm26Bits + kImmFieldShift)) - 1; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 309 | |
| 310 | // ----- MIPS Opcodes and Function Fields. |
| 311 | // We use this presentation to stay close to the table representation in |
| 312 | // MIPS32 Architecture For Programmers, Volume II: The MIPS32 Instruction Set. |
| 313 | enum Opcode { |
| 314 | SPECIAL = 0 << kOpcodeShift, |
| 315 | REGIMM = 1 << kOpcodeShift, |
| 316 | |
| 317 | J = ((0 << 3) + 2) << kOpcodeShift, |
| 318 | JAL = ((0 << 3) + 3) << kOpcodeShift, |
| 319 | BEQ = ((0 << 3) + 4) << kOpcodeShift, |
| 320 | BNE = ((0 << 3) + 5) << kOpcodeShift, |
| 321 | BLEZ = ((0 << 3) + 6) << kOpcodeShift, |
| 322 | BGTZ = ((0 << 3) + 7) << kOpcodeShift, |
| 323 | |
| 324 | ADDI = ((1 << 3) + 0) << kOpcodeShift, |
| 325 | ADDIU = ((1 << 3) + 1) << kOpcodeShift, |
| 326 | SLTI = ((1 << 3) + 2) << kOpcodeShift, |
| 327 | SLTIU = ((1 << 3) + 3) << kOpcodeShift, |
| 328 | ANDI = ((1 << 3) + 4) << kOpcodeShift, |
| 329 | ORI = ((1 << 3) + 5) << kOpcodeShift, |
| 330 | XORI = ((1 << 3) + 6) << kOpcodeShift, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 331 | LUI = ((1 << 3) + 7) << kOpcodeShift, // LUI/AUI family. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 332 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 333 | BEQC = ((2 << 3) + 0) << kOpcodeShift, |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 334 | COP1 = ((2 << 3) + 1) << kOpcodeShift, // Coprocessor 1 class. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 335 | BEQL = ((2 << 3) + 4) << kOpcodeShift, |
| 336 | BNEL = ((2 << 3) + 5) << kOpcodeShift, |
| 337 | BLEZL = ((2 << 3) + 6) << kOpcodeShift, |
| 338 | BGTZL = ((2 << 3) + 7) << kOpcodeShift, |
| 339 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 340 | DADDI = ((3 << 3) + 0) << kOpcodeShift, // This is also BNEC. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 341 | SPECIAL2 = ((3 << 3) + 4) << kOpcodeShift, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 342 | SPECIAL3 = ((3 << 3) + 7) << kOpcodeShift, |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 343 | |
| 344 | LB = ((4 << 3) + 0) << kOpcodeShift, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 345 | LH = ((4 << 3) + 1) << kOpcodeShift, |
| 346 | LWL = ((4 << 3) + 2) << kOpcodeShift, |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 347 | LW = ((4 << 3) + 3) << kOpcodeShift, |
| 348 | LBU = ((4 << 3) + 4) << kOpcodeShift, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 349 | LHU = ((4 << 3) + 5) << kOpcodeShift, |
| 350 | LWR = ((4 << 3) + 6) << kOpcodeShift, |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 351 | SB = ((5 << 3) + 0) << kOpcodeShift, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 352 | SH = ((5 << 3) + 1) << kOpcodeShift, |
| 353 | SWL = ((5 << 3) + 2) << kOpcodeShift, |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 354 | SW = ((5 << 3) + 3) << kOpcodeShift, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 355 | SWR = ((5 << 3) + 6) << kOpcodeShift, |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 356 | |
| 357 | LWC1 = ((6 << 3) + 1) << kOpcodeShift, |
| 358 | LDC1 = ((6 << 3) + 5) << kOpcodeShift, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 359 | BEQZC = ((6 << 3) + 6) << kOpcodeShift, |
| 360 | |
| 361 | PREF = ((6 << 3) + 3) << kOpcodeShift, |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 362 | |
| 363 | SWC1 = ((7 << 3) + 1) << kOpcodeShift, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 364 | SDC1 = ((7 << 3) + 5) << kOpcodeShift, |
| 365 | BNEZC = ((7 << 3) + 6) << kOpcodeShift, |
| 366 | |
| 367 | COP1X = ((1 << 4) + 3) << kOpcodeShift |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 368 | }; |
| 369 | |
| 370 | enum SecondaryField { |
| 371 | // SPECIAL Encoding of Function Field. |
| 372 | SLL = ((0 << 3) + 0), |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 373 | MOVCI = ((0 << 3) + 1), |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 374 | SRL = ((0 << 3) + 2), |
| 375 | SRA = ((0 << 3) + 3), |
| 376 | SLLV = ((0 << 3) + 4), |
| 377 | SRLV = ((0 << 3) + 6), |
| 378 | SRAV = ((0 << 3) + 7), |
| 379 | |
| 380 | JR = ((1 << 3) + 0), |
| 381 | JALR = ((1 << 3) + 1), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 382 | MOVZ = ((1 << 3) + 2), |
| 383 | MOVN = ((1 << 3) + 3), |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 384 | BREAK = ((1 << 3) + 5), |
| 385 | |
| 386 | MFHI = ((2 << 3) + 0), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 387 | CLZ_R6 = ((2 << 3) + 0), |
| 388 | CLO_R6 = ((2 << 3) + 1), |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 389 | MFLO = ((2 << 3) + 2), |
| 390 | |
| 391 | MULT = ((3 << 3) + 0), |
| 392 | MULTU = ((3 << 3) + 1), |
| 393 | DIV = ((3 << 3) + 2), |
| 394 | DIVU = ((3 << 3) + 3), |
| 395 | |
| 396 | ADD = ((4 << 3) + 0), |
| 397 | ADDU = ((4 << 3) + 1), |
| 398 | SUB = ((4 << 3) + 2), |
| 399 | SUBU = ((4 << 3) + 3), |
| 400 | AND = ((4 << 3) + 4), |
| 401 | OR = ((4 << 3) + 5), |
| 402 | XOR = ((4 << 3) + 6), |
| 403 | NOR = ((4 << 3) + 7), |
| 404 | |
| 405 | SLT = ((5 << 3) + 2), |
| 406 | SLTU = ((5 << 3) + 3), |
| 407 | |
| 408 | TGE = ((6 << 3) + 0), |
| 409 | TGEU = ((6 << 3) + 1), |
| 410 | TLT = ((6 << 3) + 2), |
| 411 | TLTU = ((6 << 3) + 3), |
| 412 | TEQ = ((6 << 3) + 4), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 413 | SELEQZ_S = ((6 << 3) + 5), |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 414 | TNE = ((6 << 3) + 6), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 415 | SELNEZ_S = ((6 << 3) + 7), |
| 416 | |
| 417 | // Multiply integers in r6. |
| 418 | MUL_MUH = ((3 << 3) + 0), // MUL, MUH. |
| 419 | MUL_MUH_U = ((3 << 3) + 1), // MUL_U, MUH_U. |
| 420 | |
| 421 | MUL_OP = ((0 << 3) + 2), |
| 422 | MUH_OP = ((0 << 3) + 3), |
| 423 | DIV_OP = ((0 << 3) + 2), |
| 424 | MOD_OP = ((0 << 3) + 3), |
| 425 | |
| 426 | DIV_MOD = ((3 << 3) + 2), |
| 427 | DIV_MOD_U = ((3 << 3) + 3), |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 428 | |
| 429 | // SPECIAL2 Encoding of Function Field. |
| 430 | MUL = ((0 << 3) + 2), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 431 | CLZ = ((4 << 3) + 0), |
| 432 | CLO = ((4 << 3) + 1), |
| 433 | |
| 434 | // SPECIAL3 Encoding of Function Field. |
| 435 | EXT = ((0 << 3) + 0), |
| 436 | INS = ((0 << 3) + 4), |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 437 | |
| 438 | // REGIMM encoding of rt Field. |
| 439 | BLTZ = ((0 << 3) + 0) << 16, |
| 440 | BGEZ = ((0 << 3) + 1) << 16, |
| 441 | BLTZAL = ((2 << 3) + 0) << 16, |
| 442 | BGEZAL = ((2 << 3) + 1) << 16, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 443 | BGEZALL = ((2 << 3) + 3) << 16, |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 444 | |
| 445 | // COP1 Encoding of rs Field. |
| 446 | MFC1 = ((0 << 3) + 0) << 21, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 447 | CFC1 = ((0 << 3) + 2) << 21, |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 448 | MFHC1 = ((0 << 3) + 3) << 21, |
| 449 | MTC1 = ((0 << 3) + 4) << 21, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 450 | CTC1 = ((0 << 3) + 6) << 21, |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 451 | MTHC1 = ((0 << 3) + 7) << 21, |
| 452 | BC1 = ((1 << 3) + 0) << 21, |
| 453 | S = ((2 << 3) + 0) << 21, |
| 454 | D = ((2 << 3) + 1) << 21, |
| 455 | W = ((2 << 3) + 4) << 21, |
| 456 | L = ((2 << 3) + 5) << 21, |
| 457 | PS = ((2 << 3) + 6) << 21, |
| 458 | // COP1 Encoding of Function Field When rs=S. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 459 | ROUND_L_S = ((1 << 3) + 0), |
| 460 | TRUNC_L_S = ((1 << 3) + 1), |
| 461 | CEIL_L_S = ((1 << 3) + 2), |
| 462 | FLOOR_L_S = ((1 << 3) + 3), |
| 463 | ROUND_W_S = ((1 << 3) + 4), |
| 464 | TRUNC_W_S = ((1 << 3) + 5), |
| 465 | CEIL_W_S = ((1 << 3) + 6), |
| 466 | FLOOR_W_S = ((1 << 3) + 7), |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 467 | CVT_D_S = ((4 << 3) + 1), |
| 468 | CVT_W_S = ((4 << 3) + 4), |
| 469 | CVT_L_S = ((4 << 3) + 5), |
| 470 | CVT_PS_S = ((4 << 3) + 6), |
| 471 | // COP1 Encoding of Function Field When rs=D. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 472 | ADD_D = ((0 << 3) + 0), |
| 473 | SUB_D = ((0 << 3) + 1), |
| 474 | MUL_D = ((0 << 3) + 2), |
| 475 | DIV_D = ((0 << 3) + 3), |
| 476 | SQRT_D = ((0 << 3) + 4), |
| 477 | ABS_D = ((0 << 3) + 5), |
| 478 | MOV_D = ((0 << 3) + 6), |
| 479 | NEG_D = ((0 << 3) + 7), |
| 480 | ROUND_L_D = ((1 << 3) + 0), |
| 481 | TRUNC_L_D = ((1 << 3) + 1), |
| 482 | CEIL_L_D = ((1 << 3) + 2), |
| 483 | FLOOR_L_D = ((1 << 3) + 3), |
| 484 | ROUND_W_D = ((1 << 3) + 4), |
| 485 | TRUNC_W_D = ((1 << 3) + 5), |
| 486 | CEIL_W_D = ((1 << 3) + 6), |
| 487 | FLOOR_W_D = ((1 << 3) + 7), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 488 | MIN = ((3 << 3) + 4), |
| 489 | MINA = ((3 << 3) + 5), |
| 490 | MAX = ((3 << 3) + 6), |
| 491 | MAXA = ((3 << 3) + 7), |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 492 | CVT_S_D = ((4 << 3) + 0), |
| 493 | CVT_W_D = ((4 << 3) + 4), |
| 494 | CVT_L_D = ((4 << 3) + 5), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 495 | C_F_D = ((6 << 3) + 0), |
| 496 | C_UN_D = ((6 << 3) + 1), |
| 497 | C_EQ_D = ((6 << 3) + 2), |
| 498 | C_UEQ_D = ((6 << 3) + 3), |
| 499 | C_OLT_D = ((6 << 3) + 4), |
| 500 | C_ULT_D = ((6 << 3) + 5), |
| 501 | C_OLE_D = ((6 << 3) + 6), |
| 502 | C_ULE_D = ((6 << 3) + 7), |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 503 | // COP1 Encoding of Function Field When rs=W or L. |
| 504 | CVT_S_W = ((4 << 3) + 0), |
| 505 | CVT_D_W = ((4 << 3) + 1), |
| 506 | CVT_S_L = ((4 << 3) + 0), |
| 507 | CVT_D_L = ((4 << 3) + 1), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 508 | BC1EQZ = ((2 << 2) + 1) << 21, |
| 509 | BC1NEZ = ((3 << 2) + 1) << 21, |
| 510 | // COP1 CMP positive predicates Bit 5..4 = 00. |
| 511 | CMP_AF = ((0 << 3) + 0), |
| 512 | CMP_UN = ((0 << 3) + 1), |
| 513 | CMP_EQ = ((0 << 3) + 2), |
| 514 | CMP_UEQ = ((0 << 3) + 3), |
| 515 | CMP_LT = ((0 << 3) + 4), |
| 516 | CMP_ULT = ((0 << 3) + 5), |
| 517 | CMP_LE = ((0 << 3) + 6), |
| 518 | CMP_ULE = ((0 << 3) + 7), |
| 519 | CMP_SAF = ((1 << 3) + 0), |
| 520 | CMP_SUN = ((1 << 3) + 1), |
| 521 | CMP_SEQ = ((1 << 3) + 2), |
| 522 | CMP_SUEQ = ((1 << 3) + 3), |
| 523 | CMP_SSLT = ((1 << 3) + 4), |
| 524 | CMP_SSULT = ((1 << 3) + 5), |
| 525 | CMP_SLE = ((1 << 3) + 6), |
| 526 | CMP_SULE = ((1 << 3) + 7), |
| 527 | // COP1 CMP negative predicates Bit 5..4 = 01. |
| 528 | CMP_AT = ((2 << 3) + 0), // Reserved, not implemented. |
| 529 | CMP_OR = ((2 << 3) + 1), |
| 530 | CMP_UNE = ((2 << 3) + 2), |
| 531 | CMP_NE = ((2 << 3) + 3), |
| 532 | CMP_UGE = ((2 << 3) + 4), // Reserved, not implemented. |
| 533 | CMP_OGE = ((2 << 3) + 5), // Reserved, not implemented. |
| 534 | CMP_UGT = ((2 << 3) + 6), // Reserved, not implemented. |
| 535 | CMP_OGT = ((2 << 3) + 7), // Reserved, not implemented. |
| 536 | CMP_SAT = ((3 << 3) + 0), // Reserved, not implemented. |
| 537 | CMP_SOR = ((3 << 3) + 1), |
| 538 | CMP_SUNE = ((3 << 3) + 2), |
| 539 | CMP_SNE = ((3 << 3) + 3), |
| 540 | CMP_SUGE = ((3 << 3) + 4), // Reserved, not implemented. |
| 541 | CMP_SOGE = ((3 << 3) + 5), // Reserved, not implemented. |
| 542 | CMP_SUGT = ((3 << 3) + 6), // Reserved, not implemented. |
| 543 | CMP_SOGT = ((3 << 3) + 7), // Reserved, not implemented. |
| 544 | |
| 545 | SEL = ((2 << 3) + 0), |
| 546 | SELEQZ_C = ((2 << 3) + 4), // COP1 on FPR registers. |
| 547 | SELNEZ_C = ((2 << 3) + 7), // COP1 on FPR registers. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 548 | // COP1 Encoding of Function Field When rs=PS. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 549 | // COP1X Encoding of Function Field. |
| 550 | MADD_D = ((4 << 3) + 1), |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 551 | |
| 552 | NULLSF = 0 |
| 553 | }; |
| 554 | |
| 555 | |
| 556 | // ----- Emulated conditions. |
| 557 | // On MIPS we use this enum to abstract from conditionnal branch instructions. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 558 | // The 'U' prefix is used to specify unsigned comparisons. |
| 559 | // Oppposite conditions must be paired as odd/even numbers |
| 560 | // because 'NegateCondition' function flips LSB to negate condition. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 561 | enum Condition { |
| 562 | // Any value < 0 is considered no_condition. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 563 | kNoCondition = -1, |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 564 | |
| 565 | overflow = 0, |
| 566 | no_overflow = 1, |
| 567 | Uless = 2, |
| 568 | Ugreater_equal= 3, |
| 569 | equal = 4, |
| 570 | not_equal = 5, |
| 571 | Uless_equal = 6, |
| 572 | Ugreater = 7, |
| 573 | negative = 8, |
| 574 | positive = 9, |
| 575 | parity_even = 10, |
| 576 | parity_odd = 11, |
| 577 | less = 12, |
| 578 | greater_equal = 13, |
| 579 | less_equal = 14, |
| 580 | greater = 15, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 581 | ueq = 16, // Unordered or Equal. |
| 582 | nue = 17, // Not (Unordered or Equal). |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 583 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 584 | cc_always = 18, |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 585 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 586 | // Aliases. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 587 | carry = Uless, |
| 588 | not_carry = Ugreater_equal, |
| 589 | zero = equal, |
| 590 | eq = equal, |
| 591 | not_zero = not_equal, |
| 592 | ne = not_equal, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 593 | nz = not_equal, |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 594 | sign = negative, |
| 595 | not_sign = positive, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 596 | mi = negative, |
| 597 | pl = positive, |
| 598 | hi = Ugreater, |
| 599 | ls = Uless_equal, |
| 600 | ge = greater_equal, |
| 601 | lt = less, |
| 602 | gt = greater, |
| 603 | le = less_equal, |
| 604 | hs = Ugreater_equal, |
| 605 | lo = Uless, |
| 606 | al = cc_always, |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 607 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 608 | cc_default = kNoCondition |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 609 | }; |
| 610 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 611 | |
| 612 | // Returns the equivalent of !cc. |
| 613 | // Negation of the default kNoCondition (-1) results in a non-default |
| 614 | // no_condition value (-2). As long as tests for no_condition check |
| 615 | // for condition < 0, this will work as expected. |
| 616 | inline Condition NegateCondition(Condition cc) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 617 | DCHECK(cc != cc_always); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 618 | return static_cast<Condition>(cc ^ 1); |
| 619 | } |
| 620 | |
| 621 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 622 | // Commute a condition such that {a cond b == b cond' a}. |
| 623 | inline Condition CommuteCondition(Condition cc) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 624 | switch (cc) { |
| 625 | case Uless: |
| 626 | return Ugreater; |
| 627 | case Ugreater: |
| 628 | return Uless; |
| 629 | case Ugreater_equal: |
| 630 | return Uless_equal; |
| 631 | case Uless_equal: |
| 632 | return Ugreater_equal; |
| 633 | case less: |
| 634 | return greater; |
| 635 | case greater: |
| 636 | return less; |
| 637 | case greater_equal: |
| 638 | return less_equal; |
| 639 | case less_equal: |
| 640 | return greater_equal; |
| 641 | default: |
| 642 | return cc; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 643 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 647 | // ----- Coprocessor conditions. |
| 648 | enum FPUCondition { |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 649 | kNoFPUCondition = -1, |
| 650 | |
| 651 | F = 0, // False. |
| 652 | UN = 1, // Unordered. |
| 653 | EQ = 2, // Equal. |
| 654 | UEQ = 3, // Unordered or Equal. |
| 655 | OLT = 4, // Ordered or Less Than. |
| 656 | ULT = 5, // Unordered or Less Than. |
| 657 | OLE = 6, // Ordered or Less Than or Equal. |
| 658 | ULE = 7 // Unordered or Less Than or Equal. |
| 659 | }; |
| 660 | |
| 661 | |
| 662 | // FPU rounding modes. |
| 663 | enum FPURoundingMode { |
| 664 | RN = 0 << 0, // Round to Nearest. |
| 665 | RZ = 1 << 0, // Round towards zero. |
| 666 | RP = 2 << 0, // Round towards Plus Infinity. |
| 667 | RM = 3 << 0, // Round towards Minus Infinity. |
| 668 | |
| 669 | // Aliases. |
| 670 | kRoundToNearest = RN, |
| 671 | kRoundToZero = RZ, |
| 672 | kRoundToPlusInf = RP, |
| 673 | kRoundToMinusInf = RM |
| 674 | }; |
| 675 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 676 | const uint32_t kFPURoundingModeMask = 3 << 0; |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 677 | |
| 678 | enum CheckForInexactConversion { |
| 679 | kCheckForInexactConversion, |
| 680 | kDontCheckForInexactConversion |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 681 | }; |
| 682 | |
| 683 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 684 | // ----------------------------------------------------------------------------- |
| 685 | // Hints. |
| 686 | |
| 687 | // Branch hints are not used on the MIPS. They are defined so that they can |
| 688 | // appear in shared function signatures, but will be ignored in MIPS |
| 689 | // implementations. |
| 690 | enum Hint { |
| 691 | no_hint = 0 |
| 692 | }; |
| 693 | |
| 694 | |
| 695 | inline Hint NegateHint(Hint hint) { |
| 696 | return no_hint; |
| 697 | } |
| 698 | |
| 699 | |
| 700 | // ----------------------------------------------------------------------------- |
| 701 | // Specific instructions, constants, and masks. |
| 702 | // These constants are declared in assembler-mips.cc, as they use named |
| 703 | // registers and other constants. |
| 704 | |
| 705 | // addiu(sp, sp, 4) aka Pop() operation or part of Pop(r) |
| 706 | // operations as post-increment of sp. |
| 707 | extern const Instr kPopInstruction; |
| 708 | // addiu(sp, sp, -4) part of Push(r) operation as pre-decrement of sp. |
| 709 | extern const Instr kPushInstruction; |
| 710 | // sw(r, MemOperand(sp, 0)) |
| 711 | extern const Instr kPushRegPattern; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 712 | // lw(r, MemOperand(sp, 0)) |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 713 | extern const Instr kPopRegPattern; |
| 714 | extern const Instr kLwRegFpOffsetPattern; |
| 715 | extern const Instr kSwRegFpOffsetPattern; |
| 716 | extern const Instr kLwRegFpNegOffsetPattern; |
| 717 | extern const Instr kSwRegFpNegOffsetPattern; |
| 718 | // A mask for the Rt register for push, pop, lw, sw instructions. |
| 719 | extern const Instr kRtMask; |
| 720 | extern const Instr kLwSwInstrTypeMask; |
| 721 | extern const Instr kLwSwInstrArgumentMask; |
| 722 | extern const Instr kLwSwOffsetMask; |
| 723 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 724 | // Break 0xfffff, reserved for redirected real time call. |
| 725 | const Instr rtCallRedirInstr = SPECIAL | BREAK | call_rt_redirected << 6; |
| 726 | // A nop instruction. (Encoding of sll 0 0 0). |
| 727 | const Instr nopInstr = 0; |
| 728 | |
| 729 | class Instruction { |
| 730 | public: |
| 731 | enum { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 732 | kInstrSize = 4, |
| 733 | kInstrSizeLog2 = 2, |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 734 | // On MIPS PC cannot actually be directly accessed. We behave as if PC was |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 735 | // always the value of the current instruction being executed. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 736 | kPCReadOffset = 0 |
| 737 | }; |
| 738 | |
| 739 | // Get the raw instruction bits. |
| 740 | inline Instr InstructionBits() const { |
| 741 | return *reinterpret_cast<const Instr*>(this); |
| 742 | } |
| 743 | |
| 744 | // Set the raw instruction bits to value. |
| 745 | inline void SetInstructionBits(Instr value) { |
| 746 | *reinterpret_cast<Instr*>(this) = value; |
| 747 | } |
| 748 | |
| 749 | // Read one particular bit out of the instruction bits. |
| 750 | inline int Bit(int nr) const { |
| 751 | return (InstructionBits() >> nr) & 1; |
| 752 | } |
| 753 | |
| 754 | // Read a bit field out of the instruction bits. |
| 755 | inline int Bits(int hi, int lo) const { |
| 756 | return (InstructionBits() >> lo) & ((2 << (hi - lo)) - 1); |
| 757 | } |
| 758 | |
| 759 | // Instruction type. |
| 760 | enum Type { |
| 761 | kRegisterType, |
| 762 | kImmediateType, |
| 763 | kJumpType, |
| 764 | kUnsupported = -1 |
| 765 | }; |
| 766 | |
| 767 | // Get the encoding type of the instruction. |
| 768 | Type InstructionType() const; |
| 769 | |
| 770 | |
| 771 | // Accessors for the different named fields used in the MIPS encoding. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 772 | inline Opcode OpcodeValue() const { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 773 | return static_cast<Opcode>( |
| 774 | Bits(kOpcodeShift + kOpcodeBits - 1, kOpcodeShift)); |
| 775 | } |
| 776 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 777 | inline int RsValue() const { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 778 | DCHECK(InstructionType() == kRegisterType || |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 779 | InstructionType() == kImmediateType); |
| 780 | return Bits(kRsShift + kRsBits - 1, kRsShift); |
| 781 | } |
| 782 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 783 | inline int RtValue() const { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 784 | DCHECK(InstructionType() == kRegisterType || |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 785 | InstructionType() == kImmediateType); |
| 786 | return Bits(kRtShift + kRtBits - 1, kRtShift); |
| 787 | } |
| 788 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 789 | inline int RdValue() const { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 790 | DCHECK(InstructionType() == kRegisterType); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 791 | return Bits(kRdShift + kRdBits - 1, kRdShift); |
| 792 | } |
| 793 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 794 | inline int SaValue() const { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 795 | DCHECK(InstructionType() == kRegisterType); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 796 | return Bits(kSaShift + kSaBits - 1, kSaShift); |
| 797 | } |
| 798 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 799 | inline int FunctionValue() const { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 800 | DCHECK(InstructionType() == kRegisterType || |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 801 | InstructionType() == kImmediateType); |
| 802 | return Bits(kFunctionShift + kFunctionBits - 1, kFunctionShift); |
| 803 | } |
| 804 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 805 | inline int FdValue() const { |
| 806 | return Bits(kFdShift + kFdBits - 1, kFdShift); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 807 | } |
| 808 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 809 | inline int FsValue() const { |
| 810 | return Bits(kFsShift + kFsBits - 1, kFsShift); |
| 811 | } |
| 812 | |
| 813 | inline int FtValue() const { |
| 814 | return Bits(kFtShift + kFtBits - 1, kFtShift); |
| 815 | } |
| 816 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 817 | inline int FrValue() const { |
| 818 | return Bits(kFrShift + kFrBits -1, kFrShift); |
| 819 | } |
| 820 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 821 | // Float Compare condition code instruction bits. |
| 822 | inline int FCccValue() const { |
| 823 | return Bits(kFCccShift + kFCccBits - 1, kFCccShift); |
| 824 | } |
| 825 | |
| 826 | // Float Branch condition code instruction bits. |
| 827 | inline int FBccValue() const { |
| 828 | return Bits(kFBccShift + kFBccBits - 1, kFBccShift); |
| 829 | } |
| 830 | |
| 831 | // Float Branch true/false instruction bit. |
| 832 | inline int FBtrueValue() const { |
| 833 | return Bits(kFBtrueShift + kFBtrueBits - 1, kFBtrueShift); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | // Return the fields at their original place in the instruction encoding. |
| 837 | inline Opcode OpcodeFieldRaw() const { |
| 838 | return static_cast<Opcode>(InstructionBits() & kOpcodeMask); |
| 839 | } |
| 840 | |
| 841 | inline int RsFieldRaw() const { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 842 | DCHECK(InstructionType() == kRegisterType || |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 843 | InstructionType() == kImmediateType); |
| 844 | return InstructionBits() & kRsFieldMask; |
| 845 | } |
| 846 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 847 | // Same as above function, but safe to call within InstructionType(). |
| 848 | inline int RsFieldRawNoAssert() const { |
| 849 | return InstructionBits() & kRsFieldMask; |
| 850 | } |
| 851 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 852 | inline int RtFieldRaw() const { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 853 | DCHECK(InstructionType() == kRegisterType || |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 854 | InstructionType() == kImmediateType); |
| 855 | return InstructionBits() & kRtFieldMask; |
| 856 | } |
| 857 | |
| 858 | inline int RdFieldRaw() const { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 859 | DCHECK(InstructionType() == kRegisterType); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 860 | return InstructionBits() & kRdFieldMask; |
| 861 | } |
| 862 | |
| 863 | inline int SaFieldRaw() const { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 864 | DCHECK(InstructionType() == kRegisterType); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 865 | return InstructionBits() & kSaFieldMask; |
| 866 | } |
| 867 | |
| 868 | inline int FunctionFieldRaw() const { |
| 869 | return InstructionBits() & kFunctionFieldMask; |
| 870 | } |
| 871 | |
| 872 | // Get the secondary field according to the opcode. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 873 | inline int SecondaryValue() const { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 874 | Opcode op = OpcodeFieldRaw(); |
| 875 | switch (op) { |
| 876 | case SPECIAL: |
| 877 | case SPECIAL2: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 878 | return FunctionValue(); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 879 | case COP1: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 880 | return RsValue(); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 881 | case REGIMM: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 882 | return RtValue(); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 883 | default: |
| 884 | return NULLSF; |
| 885 | } |
| 886 | } |
| 887 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 888 | inline int32_t Imm16Value() const { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 889 | DCHECK(InstructionType() == kImmediateType); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 890 | return Bits(kImm16Shift + kImm16Bits - 1, kImm16Shift); |
| 891 | } |
| 892 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 893 | inline int32_t Imm21Value() const { |
| 894 | DCHECK(InstructionType() == kImmediateType); |
| 895 | return Bits(kImm21Shift + kImm21Bits - 1, kImm21Shift); |
| 896 | } |
| 897 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 898 | inline int32_t Imm26Value() const { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 899 | DCHECK(InstructionType() == kJumpType); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 900 | return Bits(kImm26Shift + kImm26Bits - 1, kImm26Shift); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | // Say if the instruction should not be used in a branch delay slot. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 904 | bool IsForbiddenInBranchDelay() const; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 905 | // Say if the instruction 'links'. e.g. jal, bal. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 906 | bool IsLinkingInstruction() const; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 907 | // Say if the instruction is a break or a trap. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 908 | bool IsTrap() const; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 909 | |
| 910 | // Instructions are read of out a code stream. The only way to get a |
| 911 | // reference to an instruction is to convert a pointer. There is no way |
| 912 | // to allocate or create instances of class Instruction. |
| 913 | // Use the At(pc) function to create references to Instruction. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 914 | static Instruction* At(byte* pc) { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 915 | return reinterpret_cast<Instruction*>(pc); |
| 916 | } |
| 917 | |
| 918 | private: |
| 919 | // We need to prevent the creation of instances of class Instruction. |
| 920 | DISALLOW_IMPLICIT_CONSTRUCTORS(Instruction); |
| 921 | }; |
| 922 | |
| 923 | |
| 924 | // ----------------------------------------------------------------------------- |
| 925 | // MIPS assembly various constants. |
| 926 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 927 | // C/C++ argument slots size. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 928 | const int kCArgSlotCount = 4; |
| 929 | const int kCArgsSlotsSize = kCArgSlotCount * Instruction::kInstrSize; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 930 | // JS argument slots size. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 931 | const int kJSArgsSlotsSize = 0 * Instruction::kInstrSize; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 932 | // Assembly builtins argument slots size. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 933 | const int kBArgsSlotsSize = 0 * Instruction::kInstrSize; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 934 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 935 | const int kBranchReturnOffset = 2 * Instruction::kInstrSize; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 936 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 937 | } } // namespace v8::internal |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 938 | |
| 939 | #endif // #ifndef V8_MIPS_CONSTANTS_H_ |