blob: 8301c5e5de29e7724e4a159c7fe2fc47cf544a8b [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2012 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Andrei Popescu31002712010-02-23 13:46:05 +00004
5#ifndef V8_MIPS_CONSTANTS_H_
6#define V8_MIPS_CONSTANTS_H_
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007#include "src/globals.h"
Andrei Popescu31002712010-02-23 13:46:05 +00008// UNIMPLEMENTED_ macro for MIPS.
Steve Block44f0eee2011-05-26 01:26:41 +01009#ifdef DEBUG
Andrei Popescu31002712010-02-23 13:46:05 +000010#define UNIMPLEMENTED_MIPS() \
11 v8::internal::PrintF("%s, \tline %d: \tfunction %s not implemented. \n", \
12 __FILE__, __LINE__, __func__)
Steve Block44f0eee2011-05-26 01:26:41 +010013#else
14#define UNIMPLEMENTED_MIPS()
15#endif
16
Andrei Popescu31002712010-02-23 13:46:05 +000017#define UNSUPPORTED_MIPS() v8::internal::PrintF("Unsupported instruction.\n")
18
Ben Murdoch3ef787d2012-04-12 10:51:47 +010019enum ArchVariants {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000020 kMips32r1 = v8::internal::MIPSr1,
21 kMips32r2 = v8::internal::MIPSr2,
22 kMips32r6 = v8::internal::MIPSr6,
Ben Murdoch3ef787d2012-04-12 10:51:47 +010023 kLoongson
24};
Andrei Popescu31002712010-02-23 13:46:05 +000025
Steve Block44f0eee2011-05-26 01:26:41 +010026#ifdef _MIPS_ARCH_MIPS32R2
Ben Murdoch3ef787d2012-04-12 10:51:47 +010027 static const ArchVariants kArchVariant = kMips32r2;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000028#elif _MIPS_ARCH_MIPS32R6
29 static const ArchVariants kArchVariant = kMips32r6;
Ben Murdoch3ef787d2012-04-12 10:51:47 +010030#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 Murdochb8a8cc12014-11-26 15:28:44 +000034#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 Block44f0eee2011-05-26 01:26:41 +010039#else
Ben Murdoch3ef787d2012-04-12 10:51:47 +010040 static const ArchVariants kArchVariant = kMips32r1;
Steve Block44f0eee2011-05-26 01:26:41 +010041#endif
42
Ben Murdochb8a8cc12014-11-26 15:28:44 +000043enum 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
56enum 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)
Ben Murdoch097c5b22016-05-18 11:27:45 +010067#if defined(_MIPS_ARCH_MIPS32R2) || defined(_MIPS_ARCH_MIPS32R6)
68static const FpuMode kFpuMode = kFPXX;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000069#else
Ben Murdoch097c5b22016-05-18 11:27:45 +010070#error "FPXX is supported only on Mips32R2 and Mips32R6"
71#endif
72#else
73static const FpuMode kFpuMode = kFP32;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000074#endif
Steve Block44f0eee2011-05-26 01:26:41 +010075
Ben Murdoch257744e2011-11-30 15:57:28 +000076#if(defined(__mips_hard_float) && __mips_hard_float != 0)
77// Use floating-point coprocessor instructions. This flag is raised when
78// -mhard-float is passed to the compiler.
Ben Murdoch3ef787d2012-04-12 10:51:47 +010079const bool IsMipsSoftFloatABI = false;
Ben Murdoch257744e2011-11-30 15:57:28 +000080#elif(defined(__mips_soft_float) && __mips_soft_float != 0)
Ben Murdochb8a8cc12014-11-26 15:28:44 +000081// This flag is raised when -msoft-float is passed to the compiler.
82// Although FPU is a base requirement for v8, soft-float ABI is used
83// on soft-float systems with FPU kernel emulation.
Ben Murdoch3ef787d2012-04-12 10:51:47 +010084const bool IsMipsSoftFloatABI = true;
Ben Murdoch257744e2011-11-30 15:57:28 +000085#else
Ben Murdoch3ef787d2012-04-12 10:51:47 +010086const bool IsMipsSoftFloatABI = true;
Ben Murdoch257744e2011-11-30 15:57:28 +000087#endif
88
Ben Murdochb8a8cc12014-11-26 15:28:44 +000089#if defined(V8_TARGET_LITTLE_ENDIAN)
90const uint32_t kHoleNanUpper32Offset = 4;
91const uint32_t kHoleNanLower32Offset = 0;
92#elif defined(V8_TARGET_BIG_ENDIAN)
93const uint32_t kHoleNanUpper32Offset = 0;
94const uint32_t kHoleNanLower32Offset = 4;
95#else
96#error Unknown endianness
97#endif
98
Ben Murdoch097c5b22016-05-18 11:27:45 +010099#define IsFp64Mode() (kFpuMode == kFP64)
100#define IsFp32Mode() (kFpuMode == kFP32)
101#define IsFpxxMode() (kFpuMode == kFPXX)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000102
103#ifndef _MIPS_ARCH_MIPS32RX
104#define IsMipsArchVariant(check) \
105 (kArchVariant == check)
106#else
107#define IsMipsArchVariant(check) \
Paul Lind18a7ebb2014-12-17 22:29:32 -0800108 (CpuFeatures::IsSupported(static_cast<CpuFeature>(check)))
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000109#endif
110
Ben Murdochc5610432016-08-08 18:44:38 +0100111#if defined(V8_TARGET_LITTLE_ENDIAN)
112const uint32_t kMipsLwrOffset = 0;
113const uint32_t kMipsLwlOffset = 3;
114const uint32_t kMipsSwrOffset = 0;
115const uint32_t kMipsSwlOffset = 3;
116#elif defined(V8_TARGET_BIG_ENDIAN)
117const uint32_t kMipsLwrOffset = 3;
118const uint32_t kMipsLwlOffset = 0;
119const uint32_t kMipsSwrOffset = 3;
120const uint32_t kMipsSwlOffset = 0;
121#else
122#error Unknown endianness
123#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000124
125#define __STDC_FORMAT_MACROS
126#include <inttypes.h>
Ben Murdoch257744e2011-11-30 15:57:28 +0000127
Andrei Popescu31002712010-02-23 13:46:05 +0000128// Defines constants and accessor classes to assemble, disassemble and
129// simulate MIPS32 instructions.
130//
131// See: MIPS32 Architecture For Programmers
132// Volume II: The MIPS32 Instruction Set
133// Try www.cs.cornell.edu/courses/cs3410/2008fa/MIPS_Vol2.pdf.
134
Steve Block44f0eee2011-05-26 01:26:41 +0100135namespace v8 {
136namespace internal {
Andrei Popescu31002712010-02-23 13:46:05 +0000137
138// -----------------------------------------------------------------------------
Ben Murdoch257744e2011-11-30 15:57:28 +0000139// Registers and FPURegisters.
Andrei Popescu31002712010-02-23 13:46:05 +0000140
141// Number of general purpose registers.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100142const int kNumRegisters = 32;
143const int kInvalidRegister = -1;
Andrei Popescu31002712010-02-23 13:46:05 +0000144
145// Number of registers with HI, LO, and pc.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100146const int kNumSimuRegisters = 35;
Andrei Popescu31002712010-02-23 13:46:05 +0000147
148// In the simulator, the PC register is simulated as the 34th register.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100149const int kPCRegister = 34;
Andrei Popescu31002712010-02-23 13:46:05 +0000150
151// Number coprocessor registers.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100152const int kNumFPURegisters = 32;
153const int kInvalidFPURegister = -1;
Andrei Popescu31002712010-02-23 13:46:05 +0000154
Steve Block44f0eee2011-05-26 01:26:41 +0100155// FPU (coprocessor 1) control registers. Currently only FCSR is implemented.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100156const int kFCSRRegister = 31;
157const int kInvalidFPUControlRegister = -1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000158const uint32_t kFPUInvalidResult = static_cast<uint32_t>(1 << 31) - 1;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000159const int32_t kFPUInvalidResultNegative = static_cast<int32_t>(1 << 31);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000160const uint64_t kFPU64InvalidResult =
161 static_cast<uint64_t>(static_cast<uint64_t>(1) << 63) - 1;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000162const int64_t kFPU64InvalidResultNegative =
163 static_cast<int64_t>(static_cast<uint64_t>(1) << 63);
Steve Block44f0eee2011-05-26 01:26:41 +0100164
165// FCSR constants.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100166const uint32_t kFCSRInexactFlagBit = 2;
167const uint32_t kFCSRUnderflowFlagBit = 3;
168const uint32_t kFCSROverflowFlagBit = 4;
169const uint32_t kFCSRDivideByZeroFlagBit = 5;
170const uint32_t kFCSRInvalidOpFlagBit = 6;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000171const uint32_t kFCSRNaN2008FlagBit = 18;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000172
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100173const uint32_t kFCSRInexactFlagMask = 1 << kFCSRInexactFlagBit;
174const uint32_t kFCSRUnderflowFlagMask = 1 << kFCSRUnderflowFlagBit;
175const uint32_t kFCSROverflowFlagMask = 1 << kFCSROverflowFlagBit;
176const uint32_t kFCSRDivideByZeroFlagMask = 1 << kFCSRDivideByZeroFlagBit;
177const uint32_t kFCSRInvalidOpFlagMask = 1 << kFCSRInvalidOpFlagBit;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000178const uint32_t kFCSRNaN2008FlagMask = 1 << kFCSRNaN2008FlagBit;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000179
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100180const uint32_t kFCSRFlagMask =
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000181 kFCSRInexactFlagMask |
182 kFCSRUnderflowFlagMask |
183 kFCSROverflowFlagMask |
184 kFCSRDivideByZeroFlagMask |
185 kFCSRInvalidOpFlagMask;
186
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100187const uint32_t kFCSRExceptionFlagMask = kFCSRFlagMask ^ kFCSRInexactFlagMask;
Steve Block44f0eee2011-05-26 01:26:41 +0100188
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000189// 'pref' instruction hints
190const int32_t kPrefHintLoad = 0;
191const int32_t kPrefHintStore = 1;
192const int32_t kPrefHintLoadStreamed = 4;
193const int32_t kPrefHintStoreStreamed = 5;
194const int32_t kPrefHintLoadRetained = 6;
195const int32_t kPrefHintStoreRetained = 7;
196const int32_t kPrefHintWritebackInvalidate = 25;
197const int32_t kPrefHintPrepareForStore = 30;
198
Andrei Popescu31002712010-02-23 13:46:05 +0000199// Helper functions for converting between register numbers and names.
200class Registers {
201 public:
202 // Return the name of the register.
203 static const char* Name(int reg);
204
205 // Lookup the register number for the name provided.
206 static int Number(const char* name);
207
208 struct RegisterAlias {
209 int reg;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100210 const char* name;
Andrei Popescu31002712010-02-23 13:46:05 +0000211 };
212
213 static const int32_t kMaxValue = 0x7fffffff;
214 static const int32_t kMinValue = 0x80000000;
215
216 private:
Andrei Popescu31002712010-02-23 13:46:05 +0000217 static const char* names_[kNumSimuRegisters];
218 static const RegisterAlias aliases_[];
219};
220
221// Helper functions for converting between register numbers and names.
Steve Block44f0eee2011-05-26 01:26:41 +0100222class FPURegisters {
Andrei Popescu31002712010-02-23 13:46:05 +0000223 public:
224 // Return the name of the register.
225 static const char* Name(int reg);
226
227 // Lookup the register number for the name provided.
228 static int Number(const char* name);
229
230 struct RegisterAlias {
231 int creg;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100232 const char* name;
Andrei Popescu31002712010-02-23 13:46:05 +0000233 };
234
235 private:
Steve Block44f0eee2011-05-26 01:26:41 +0100236 static const char* names_[kNumFPURegisters];
Andrei Popescu31002712010-02-23 13:46:05 +0000237 static const RegisterAlias aliases_[];
238};
239
240
241// -----------------------------------------------------------------------------
242// Instructions encoding constants.
243
244// On MIPS all instructions are 32 bits.
245typedef int32_t Instr;
246
Andrei Popescu31002712010-02-23 13:46:05 +0000247// Special Software Interrupt codes when used in the presence of the MIPS
248// simulator.
249enum SoftwareInterruptCodes {
250 // Transition to C code.
251 call_rt_redirected = 0xfffff
252};
253
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000254// On MIPS Simulator breakpoints can have different codes:
255// - Breaks between 0 and kMaxWatchpointCode are treated as simple watchpoints,
256// the simulator will run through them and print the registers.
257// - Breaks between kMaxWatchpointCode and kMaxStopCode are treated as stop()
258// instructions (see Assembler::stop()).
259// - Breaks larger than kMaxStopCode are simple breaks, dropping you into the
260// debugger.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100261const uint32_t kMaxWatchpointCode = 31;
262const uint32_t kMaxStopCode = 127;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000263STATIC_ASSERT(kMaxWatchpointCode < kMaxStopCode);
264
265
Andrei Popescu31002712010-02-23 13:46:05 +0000266// ----- Fields offset and length.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100267const int kOpcodeShift = 26;
268const int kOpcodeBits = 6;
269const int kRsShift = 21;
270const int kRsBits = 5;
271const int kRtShift = 16;
272const int kRtBits = 5;
273const int kRdShift = 11;
274const int kRdBits = 5;
275const int kSaShift = 6;
276const int kSaBits = 5;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000277const int kLsaSaBits = 2;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100278const int kFunctionShift = 0;
279const int kFunctionBits = 6;
280const int kLuiShift = 16;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000281const int kBp2Shift = 6;
282const int kBp2Bits = 2;
Andrei Popescu31002712010-02-23 13:46:05 +0000283
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100284const int kImm16Shift = 0;
285const int kImm16Bits = 16;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000286const int kImm18Shift = 0;
287const int kImm18Bits = 18;
288const int kImm19Shift = 0;
289const int kImm19Bits = 19;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000290const int kImm21Shift = 0;
291const int kImm21Bits = 21;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100292const int kImm26Shift = 0;
293const int kImm26Bits = 26;
294const int kImm28Shift = 0;
295const int kImm28Bits = 28;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000296const int kImm32Shift = 0;
297const int kImm32Bits = 32;
Andrei Popescu31002712010-02-23 13:46:05 +0000298
Ben Murdoch589d6972011-11-30 16:04:58 +0000299// In branches and jumps immediate fields point to words, not bytes,
300// and are therefore shifted by 2.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100301const int kImmFieldShift = 2;
Ben Murdoch589d6972011-11-30 16:04:58 +0000302
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000303const int kFrBits = 5;
304const int kFrShift = 21;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100305const int kFsShift = 11;
306const int kFsBits = 5;
307const int kFtShift = 16;
308const int kFtBits = 5;
309const int kFdShift = 6;
310const int kFdBits = 5;
311const int kFCccShift = 8;
312const int kFCccBits = 3;
313const int kFBccShift = 18;
314const int kFBccBits = 3;
315const int kFBtrueShift = 16;
316const int kFBtrueBits = 1;
Andrei Popescu31002712010-02-23 13:46:05 +0000317
Ben Murdoch257744e2011-11-30 15:57:28 +0000318// ----- Miscellaneous useful masks.
Andrei Popescu31002712010-02-23 13:46:05 +0000319// Instruction bit masks.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000320const int kOpcodeMask = ((1 << kOpcodeBits) - 1) << kOpcodeShift;
321const int kImm16Mask = ((1 << kImm16Bits) - 1) << kImm16Shift;
322const int kImm18Mask = ((1 << kImm18Bits) - 1) << kImm18Shift;
323const int kImm19Mask = ((1 << kImm19Bits) - 1) << kImm19Shift;
324const int kImm21Mask = ((1 << kImm21Bits) - 1) << kImm21Shift;
325const int kImm26Mask = ((1 << kImm26Bits) - 1) << kImm26Shift;
326const int kImm28Mask = ((1 << kImm28Bits) - 1) << kImm28Shift;
327const int kRsFieldMask = ((1 << kRsBits) - 1) << kRsShift;
328const int kRtFieldMask = ((1 << kRtBits) - 1) << kRtShift;
329const int kRdFieldMask = ((1 << kRdBits) - 1) << kRdShift;
330const int kSaFieldMask = ((1 << kSaBits) - 1) << kSaShift;
331const int kFunctionFieldMask = ((1 << kFunctionBits) - 1) << kFunctionShift;
Andrei Popescu31002712010-02-23 13:46:05 +0000332// Misc masks.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000333const int kHiMask = 0xffff << 16;
334const int kLoMask = 0xffff;
335const int kSignMask = 0x80000000;
336const int kJumpAddrMask = (1 << (kImm26Bits + kImmFieldShift)) - 1;
Andrei Popescu31002712010-02-23 13:46:05 +0000337
338// ----- MIPS Opcodes and Function Fields.
339// We use this presentation to stay close to the table representation in
340// MIPS32 Architecture For Programmers, Volume II: The MIPS32 Instruction Set.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000341enum Opcode : uint32_t {
342 SPECIAL = 0U << kOpcodeShift,
343 REGIMM = 1U << kOpcodeShift,
Andrei Popescu31002712010-02-23 13:46:05 +0000344
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000345 J = ((0U << 3) + 2) << kOpcodeShift,
346 JAL = ((0U << 3) + 3) << kOpcodeShift,
347 BEQ = ((0U << 3) + 4) << kOpcodeShift,
348 BNE = ((0U << 3) + 5) << kOpcodeShift,
349 BLEZ = ((0U << 3) + 6) << kOpcodeShift,
350 BGTZ = ((0U << 3) + 7) << kOpcodeShift,
Andrei Popescu31002712010-02-23 13:46:05 +0000351
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000352 ADDI = ((1U << 3) + 0) << kOpcodeShift,
353 ADDIU = ((1U << 3) + 1) << kOpcodeShift,
354 SLTI = ((1U << 3) + 2) << kOpcodeShift,
355 SLTIU = ((1U << 3) + 3) << kOpcodeShift,
356 ANDI = ((1U << 3) + 4) << kOpcodeShift,
357 ORI = ((1U << 3) + 5) << kOpcodeShift,
358 XORI = ((1U << 3) + 6) << kOpcodeShift,
359 LUI = ((1U << 3) + 7) << kOpcodeShift, // LUI/AUI family.
Andrei Popescu31002712010-02-23 13:46:05 +0000360
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000361 BEQC = ((2U << 3) + 0) << kOpcodeShift,
362 COP1 = ((2U << 3) + 1) << kOpcodeShift, // Coprocessor 1 class.
363 BEQL = ((2U << 3) + 4) << kOpcodeShift,
364 BNEL = ((2U << 3) + 5) << kOpcodeShift,
365 BLEZL = ((2U << 3) + 6) << kOpcodeShift,
366 BGTZL = ((2U << 3) + 7) << kOpcodeShift,
Andrei Popescu31002712010-02-23 13:46:05 +0000367
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000368 DADDI = ((3U << 3) + 0) << kOpcodeShift, // This is also BNEC.
369 SPECIAL2 = ((3U << 3) + 4) << kOpcodeShift,
370 SPECIAL3 = ((3U << 3) + 7) << kOpcodeShift,
Andrei Popescu31002712010-02-23 13:46:05 +0000371
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000372 LB = ((4U << 3) + 0) << kOpcodeShift,
373 LH = ((4U << 3) + 1) << kOpcodeShift,
374 LWL = ((4U << 3) + 2) << kOpcodeShift,
375 LW = ((4U << 3) + 3) << kOpcodeShift,
376 LBU = ((4U << 3) + 4) << kOpcodeShift,
377 LHU = ((4U << 3) + 5) << kOpcodeShift,
378 LWR = ((4U << 3) + 6) << kOpcodeShift,
379 SB = ((5U << 3) + 0) << kOpcodeShift,
380 SH = ((5U << 3) + 1) << kOpcodeShift,
381 SWL = ((5U << 3) + 2) << kOpcodeShift,
382 SW = ((5U << 3) + 3) << kOpcodeShift,
383 SWR = ((5U << 3) + 6) << kOpcodeShift,
Andrei Popescu31002712010-02-23 13:46:05 +0000384
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000385 LWC1 = ((6U << 3) + 1) << kOpcodeShift,
386 BC = ((6U << 3) + 2) << kOpcodeShift,
387 LDC1 = ((6U << 3) + 5) << kOpcodeShift,
388 POP66 = ((6U << 3) + 6) << kOpcodeShift, // beqzc, jic
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000389
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000390 PREF = ((6U << 3) + 3) << kOpcodeShift,
Andrei Popescu31002712010-02-23 13:46:05 +0000391
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000392 SWC1 = ((7U << 3) + 1) << kOpcodeShift,
393 BALC = ((7U << 3) + 2) << kOpcodeShift,
394 PCREL = ((7U << 3) + 3) << kOpcodeShift,
395 SDC1 = ((7U << 3) + 5) << kOpcodeShift,
396 POP76 = ((7U << 3) + 6) << kOpcodeShift, // bnezc, jialc
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000397
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000398 COP1X = ((1U << 4) + 3) << kOpcodeShift,
399
400 // New r6 instruction.
401 POP06 = BLEZ, // bgeuc/bleuc, blezalc, bgezalc
402 POP07 = BGTZ, // bltuc/bgtuc, bgtzalc, bltzalc
403 POP10 = ADDI, // beqzalc, bovc, beqc
404 POP26 = BLEZL, // bgezc, blezc, bgec/blec
405 POP27 = BGTZL, // bgtzc, bltzc, bltc/bgtc
Ben Murdoch097c5b22016-05-18 11:27:45 +0100406 POP30 = DADDI, // bnezalc, bnvc, bnec
Andrei Popescu31002712010-02-23 13:46:05 +0000407};
408
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000409enum SecondaryField : uint32_t {
Andrei Popescu31002712010-02-23 13:46:05 +0000410 // SPECIAL Encoding of Function Field.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000411 SLL = ((0U << 3) + 0),
412 MOVCI = ((0U << 3) + 1),
413 SRL = ((0U << 3) + 2),
414 SRA = ((0U << 3) + 3),
415 SLLV = ((0U << 3) + 4),
416 LSA = ((0U << 3) + 5),
417 SRLV = ((0U << 3) + 6),
418 SRAV = ((0U << 3) + 7),
Andrei Popescu31002712010-02-23 13:46:05 +0000419
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000420 JR = ((1U << 3) + 0),
421 JALR = ((1U << 3) + 1),
422 MOVZ = ((1U << 3) + 2),
423 MOVN = ((1U << 3) + 3),
424 BREAK = ((1U << 3) + 5),
Ben Murdochc5610432016-08-08 18:44:38 +0100425 SYNC = ((1U << 3) + 7),
Andrei Popescu31002712010-02-23 13:46:05 +0000426
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000427 MFHI = ((2U << 3) + 0),
428 CLZ_R6 = ((2U << 3) + 0),
429 CLO_R6 = ((2U << 3) + 1),
430 MFLO = ((2U << 3) + 2),
Andrei Popescu31002712010-02-23 13:46:05 +0000431
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000432 MULT = ((3U << 3) + 0),
433 MULTU = ((3U << 3) + 1),
434 DIV = ((3U << 3) + 2),
435 DIVU = ((3U << 3) + 3),
Andrei Popescu31002712010-02-23 13:46:05 +0000436
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000437 ADD = ((4U << 3) + 0),
438 ADDU = ((4U << 3) + 1),
439 SUB = ((4U << 3) + 2),
440 SUBU = ((4U << 3) + 3),
441 AND = ((4U << 3) + 4),
442 OR = ((4U << 3) + 5),
443 XOR = ((4U << 3) + 6),
444 NOR = ((4U << 3) + 7),
Andrei Popescu31002712010-02-23 13:46:05 +0000445
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000446 SLT = ((5U << 3) + 2),
447 SLTU = ((5U << 3) + 3),
Andrei Popescu31002712010-02-23 13:46:05 +0000448
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000449 TGE = ((6U << 3) + 0),
450 TGEU = ((6U << 3) + 1),
451 TLT = ((6U << 3) + 2),
452 TLTU = ((6U << 3) + 3),
453 TEQ = ((6U << 3) + 4),
454 SELEQZ_S = ((6U << 3) + 5),
455 TNE = ((6U << 3) + 6),
456 SELNEZ_S = ((6U << 3) + 7),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000457
458 // Multiply integers in r6.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000459 MUL_MUH = ((3U << 3) + 0), // MUL, MUH.
460 MUL_MUH_U = ((3U << 3) + 1), // MUL_U, MUH_U.
461 RINT = ((3U << 3) + 2),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000462
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000463 MUL_OP = ((0U << 3) + 2),
464 MUH_OP = ((0U << 3) + 3),
465 DIV_OP = ((0U << 3) + 2),
466 MOD_OP = ((0U << 3) + 3),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000467
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000468 DIV_MOD = ((3U << 3) + 2),
469 DIV_MOD_U = ((3U << 3) + 3),
Andrei Popescu31002712010-02-23 13:46:05 +0000470
471 // SPECIAL2 Encoding of Function Field.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000472 MUL = ((0U << 3) + 2),
473 CLZ = ((4U << 3) + 0),
474 CLO = ((4U << 3) + 1),
Steve Block44f0eee2011-05-26 01:26:41 +0100475
476 // SPECIAL3 Encoding of Function Field.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000477 EXT = ((0U << 3) + 0),
478 INS = ((0U << 3) + 4),
479 BSHFL = ((4U << 3) + 0),
480
481 // SPECIAL3 Encoding of sa Field.
482 BITSWAP = ((0U << 3) + 0),
483 ALIGN = ((0U << 3) + 2),
484 WSBH = ((0U << 3) + 2),
485 SEB = ((2U << 3) + 0),
486 SEH = ((3U << 3) + 0),
Andrei Popescu31002712010-02-23 13:46:05 +0000487
488 // REGIMM encoding of rt Field.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000489 BLTZ = ((0U << 3) + 0) << 16,
490 BGEZ = ((0U << 3) + 1) << 16,
491 BLTZAL = ((2U << 3) + 0) << 16,
492 BGEZAL = ((2U << 3) + 1) << 16,
493 BGEZALL = ((2U << 3) + 3) << 16,
Andrei Popescu31002712010-02-23 13:46:05 +0000494
495 // COP1 Encoding of rs Field.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000496 MFC1 = ((0U << 3) + 0) << 21,
497 CFC1 = ((0U << 3) + 2) << 21,
498 MFHC1 = ((0U << 3) + 3) << 21,
499 MTC1 = ((0U << 3) + 4) << 21,
500 CTC1 = ((0U << 3) + 6) << 21,
501 MTHC1 = ((0U << 3) + 7) << 21,
502 BC1 = ((1U << 3) + 0) << 21,
503 S = ((2U << 3) + 0) << 21,
504 D = ((2U << 3) + 1) << 21,
505 W = ((2U << 3) + 4) << 21,
506 L = ((2U << 3) + 5) << 21,
507 PS = ((2U << 3) + 6) << 21,
Andrei Popescu31002712010-02-23 13:46:05 +0000508 // COP1 Encoding of Function Field When rs=S.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000509
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000510 ADD_S = ((0U << 3) + 0),
511 SUB_S = ((0U << 3) + 1),
512 MUL_S = ((0U << 3) + 2),
513 DIV_S = ((0U << 3) + 3),
514 ABS_S = ((0U << 3) + 5),
515 SQRT_S = ((0U << 3) + 4),
516 MOV_S = ((0U << 3) + 6),
517 NEG_S = ((0U << 3) + 7),
518 ROUND_L_S = ((1U << 3) + 0),
519 TRUNC_L_S = ((1U << 3) + 1),
520 CEIL_L_S = ((1U << 3) + 2),
521 FLOOR_L_S = ((1U << 3) + 3),
522 ROUND_W_S = ((1U << 3) + 4),
523 TRUNC_W_S = ((1U << 3) + 5),
524 CEIL_W_S = ((1U << 3) + 6),
525 FLOOR_W_S = ((1U << 3) + 7),
526 RECIP_S = ((2U << 3) + 5),
527 RSQRT_S = ((2U << 3) + 6),
528 CLASS_S = ((3U << 3) + 3),
529 CVT_D_S = ((4U << 3) + 1),
530 CVT_W_S = ((4U << 3) + 4),
531 CVT_L_S = ((4U << 3) + 5),
532 CVT_PS_S = ((4U << 3) + 6),
533
534 // COP1 Encoding of Function Field When rs=D.
535 ADD_D = ((0U << 3) + 0),
536 SUB_D = ((0U << 3) + 1),
537 MUL_D = ((0U << 3) + 2),
538 DIV_D = ((0U << 3) + 3),
539 SQRT_D = ((0U << 3) + 4),
540 ABS_D = ((0U << 3) + 5),
541 MOV_D = ((0U << 3) + 6),
542 NEG_D = ((0U << 3) + 7),
543 ROUND_L_D = ((1U << 3) + 0),
544 TRUNC_L_D = ((1U << 3) + 1),
545 CEIL_L_D = ((1U << 3) + 2),
546 FLOOR_L_D = ((1U << 3) + 3),
547 ROUND_W_D = ((1U << 3) + 4),
548 TRUNC_W_D = ((1U << 3) + 5),
549 CEIL_W_D = ((1U << 3) + 6),
550 FLOOR_W_D = ((1U << 3) + 7),
551 RECIP_D = ((2U << 3) + 5),
552 RSQRT_D = ((2U << 3) + 6),
553 CLASS_D = ((3U << 3) + 3),
554 MIN = ((3U << 3) + 4),
555 MINA = ((3U << 3) + 5),
556 MAX = ((3U << 3) + 6),
557 MAXA = ((3U << 3) + 7),
558 CVT_S_D = ((4U << 3) + 0),
559 CVT_W_D = ((4U << 3) + 4),
560 CVT_L_D = ((4U << 3) + 5),
561 C_F_D = ((6U << 3) + 0),
562 C_UN_D = ((6U << 3) + 1),
563 C_EQ_D = ((6U << 3) + 2),
564 C_UEQ_D = ((6U << 3) + 3),
565 C_OLT_D = ((6U << 3) + 4),
566 C_ULT_D = ((6U << 3) + 5),
567 C_OLE_D = ((6U << 3) + 6),
568 C_ULE_D = ((6U << 3) + 7),
569
570 // COP1 Encoding of Function Field When rs=W or L.
571 CVT_S_W = ((4U << 3) + 0),
572 CVT_D_W = ((4U << 3) + 1),
573 CVT_S_L = ((4U << 3) + 0),
574 CVT_D_L = ((4U << 3) + 1),
575 BC1EQZ = ((2U << 2) + 1) << 21,
576 BC1NEZ = ((3U << 2) + 1) << 21,
577 // COP1 CMP positive predicates Bit 5..4 = 00.
578 CMP_AF = ((0U << 3) + 0),
579 CMP_UN = ((0U << 3) + 1),
580 CMP_EQ = ((0U << 3) + 2),
581 CMP_UEQ = ((0U << 3) + 3),
582 CMP_LT = ((0U << 3) + 4),
583 CMP_ULT = ((0U << 3) + 5),
584 CMP_LE = ((0U << 3) + 6),
585 CMP_ULE = ((0U << 3) + 7),
586 CMP_SAF = ((1U << 3) + 0),
587 CMP_SUN = ((1U << 3) + 1),
588 CMP_SEQ = ((1U << 3) + 2),
589 CMP_SUEQ = ((1U << 3) + 3),
590 CMP_SSLT = ((1U << 3) + 4),
591 CMP_SSULT = ((1U << 3) + 5),
592 CMP_SLE = ((1U << 3) + 6),
593 CMP_SULE = ((1U << 3) + 7),
594 // COP1 CMP negative predicates Bit 5..4 = 01.
595 CMP_AT = ((2U << 3) + 0), // Reserved, not implemented.
596 CMP_OR = ((2U << 3) + 1),
597 CMP_UNE = ((2U << 3) + 2),
598 CMP_NE = ((2U << 3) + 3),
599 CMP_UGE = ((2U << 3) + 4), // Reserved, not implemented.
600 CMP_OGE = ((2U << 3) + 5), // Reserved, not implemented.
601 CMP_UGT = ((2U << 3) + 6), // Reserved, not implemented.
602 CMP_OGT = ((2U << 3) + 7), // Reserved, not implemented.
603 CMP_SAT = ((3U << 3) + 0), // Reserved, not implemented.
604 CMP_SOR = ((3U << 3) + 1),
605 CMP_SUNE = ((3U << 3) + 2),
606 CMP_SNE = ((3U << 3) + 3),
607 CMP_SUGE = ((3U << 3) + 4), // Reserved, not implemented.
608 CMP_SOGE = ((3U << 3) + 5), // Reserved, not implemented.
609 CMP_SUGT = ((3U << 3) + 6), // Reserved, not implemented.
610 CMP_SOGT = ((3U << 3) + 7), // Reserved, not implemented.
611
612 SEL = ((2U << 3) + 0),
613 MOVZ_C = ((2U << 3) + 2),
614 MOVN_C = ((2U << 3) + 3),
615 SELEQZ_C = ((2U << 3) + 4), // COP1 on FPR registers.
616 MOVF = ((2U << 3) + 1), // Function field for MOVT.fmt and MOVF.fmt
617 SELNEZ_C = ((2U << 3) + 7), // COP1 on FPR registers.
Andrei Popescu31002712010-02-23 13:46:05 +0000618 // COP1 Encoding of Function Field When rs=PS.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000619 // COP1X Encoding of Function Field.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000620 MADD_D = ((4U << 3) + 1),
Andrei Popescu31002712010-02-23 13:46:05 +0000621
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000622 // PCREL Encoding of rt Field.
623 ADDIUPC = ((0U << 2) + 0),
624 LWPC = ((0U << 2) + 1),
625 AUIPC = ((3U << 3) + 6),
626 ALUIPC = ((3U << 3) + 7),
627
628 // POP66 Encoding of rs Field.
629 JIC = ((0U << 5) + 0),
630
631 // POP76 Encoding of rs Field.
632 JIALC = ((0U << 5) + 0),
633
634 NULLSF = 0U
Andrei Popescu31002712010-02-23 13:46:05 +0000635};
636
Andrei Popescu31002712010-02-23 13:46:05 +0000637// ----- Emulated conditions.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000638// On MIPS we use this enum to abstract from conditional branch instructions.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000639// The 'U' prefix is used to specify unsigned comparisons.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000640// Opposite conditions must be paired as odd/even numbers
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000641// because 'NegateCondition' function flips LSB to negate condition.
Andrei Popescu31002712010-02-23 13:46:05 +0000642enum Condition {
643 // Any value < 0 is considered no_condition.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400644 kNoCondition = -1,
645 overflow = 0,
646 no_overflow = 1,
647 Uless = 2,
648 Ugreater_equal = 3,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000649 Uless_equal = 4,
650 Ugreater = 5,
651 equal = 6,
652 not_equal = 7, // Unordered or Not Equal.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400653 negative = 8,
654 positive = 9,
655 parity_even = 10,
656 parity_odd = 11,
657 less = 12,
Andrei Popescu31002712010-02-23 13:46:05 +0000658 greater_equal = 13,
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400659 less_equal = 14,
660 greater = 15,
661 ueq = 16, // Unordered or Equal.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000662 ogl = 17, // Ordered and Not Equal.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400663 cc_always = 18,
Andrei Popescu31002712010-02-23 13:46:05 +0000664
Ben Murdoch257744e2011-11-30 15:57:28 +0000665 // Aliases.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400666 carry = Uless,
667 not_carry = Ugreater_equal,
668 zero = equal,
669 eq = equal,
670 not_zero = not_equal,
671 ne = not_equal,
672 nz = not_equal,
673 sign = negative,
674 not_sign = positive,
675 mi = negative,
676 pl = positive,
677 hi = Ugreater,
678 ls = Uless_equal,
679 ge = greater_equal,
680 lt = less,
681 gt = greater,
682 le = less_equal,
683 hs = Ugreater_equal,
684 lo = Uless,
685 al = cc_always,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000686 ult = Uless,
687 uge = Ugreater_equal,
688 ule = Uless_equal,
689 ugt = Ugreater,
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400690 cc_default = kNoCondition
Andrei Popescu31002712010-02-23 13:46:05 +0000691};
692
Steve Block44f0eee2011-05-26 01:26:41 +0100693
694// Returns the equivalent of !cc.
695// Negation of the default kNoCondition (-1) results in a non-default
696// no_condition value (-2). As long as tests for no_condition check
697// for condition < 0, this will work as expected.
698inline Condition NegateCondition(Condition cc) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000699 DCHECK(cc != cc_always);
Steve Block44f0eee2011-05-26 01:26:41 +0100700 return static_cast<Condition>(cc ^ 1);
701}
702
703
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000704inline Condition NegateFpuCondition(Condition cc) {
705 DCHECK(cc != cc_always);
706 switch (cc) {
707 case ult:
708 return ge;
709 case ugt:
710 return le;
711 case uge:
712 return lt;
713 case ule:
714 return gt;
715 case lt:
716 return uge;
717 case gt:
718 return ule;
719 case ge:
720 return ult;
721 case le:
722 return ugt;
723 case eq:
724 return ne;
725 case ne:
726 return eq;
727 case ueq:
728 return ogl;
729 case ogl:
730 return ueq;
731 default:
732 return cc;
733 }
734}
735
736
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000737// Commute a condition such that {a cond b == b cond' a}.
738inline Condition CommuteCondition(Condition cc) {
Steve Block44f0eee2011-05-26 01:26:41 +0100739 switch (cc) {
740 case Uless:
741 return Ugreater;
742 case Ugreater:
743 return Uless;
744 case Ugreater_equal:
745 return Uless_equal;
746 case Uless_equal:
747 return Ugreater_equal;
748 case less:
749 return greater;
750 case greater:
751 return less;
752 case greater_equal:
753 return less_equal;
754 case less_equal:
755 return greater_equal;
756 default:
757 return cc;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000758 }
Steve Block44f0eee2011-05-26 01:26:41 +0100759}
760
761
Andrei Popescu31002712010-02-23 13:46:05 +0000762// ----- Coprocessor conditions.
763enum FPUCondition {
Ben Murdoch589d6972011-11-30 16:04:58 +0000764 kNoFPUCondition = -1,
765
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000766 F = 0x00, // False.
767 UN = 0x01, // Unordered.
768 EQ = 0x02, // Equal.
769 UEQ = 0x03, // Unordered or Equal.
770 OLT = 0x04, // Ordered or Less Than, on Mips release < 6.
771 LT = 0x04, // Ordered or Less Than, on Mips release >= 6.
772 ULT = 0x05, // Unordered or Less Than.
773 OLE = 0x06, // Ordered or Less Than or Equal, on Mips release < 6.
774 LE = 0x06, // Ordered or Less Than or Equal, on Mips release >= 6.
775 ULE = 0x07, // Unordered or Less Than or Equal.
776
777 // Following constants are available on Mips release >= 6 only.
778 ORD = 0x11, // Ordered, on Mips release >= 6.
779 UNE = 0x12, // Not equal, on Mips release >= 6.
780 NE = 0x13, // Ordered Greater Than or Less Than. on Mips >= 6 only.
Ben Murdoch589d6972011-11-30 16:04:58 +0000781};
782
783
784// FPU rounding modes.
785enum FPURoundingMode {
786 RN = 0 << 0, // Round to Nearest.
787 RZ = 1 << 0, // Round towards zero.
788 RP = 2 << 0, // Round towards Plus Infinity.
789 RM = 3 << 0, // Round towards Minus Infinity.
790
791 // Aliases.
792 kRoundToNearest = RN,
793 kRoundToZero = RZ,
794 kRoundToPlusInf = RP,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000795 kRoundToMinusInf = RM,
796
797 mode_round = RN,
798 mode_ceil = RP,
799 mode_floor = RM,
800 mode_trunc = RZ
Ben Murdoch589d6972011-11-30 16:04:58 +0000801};
802
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100803const uint32_t kFPURoundingModeMask = 3 << 0;
Ben Murdoch589d6972011-11-30 16:04:58 +0000804
805enum CheckForInexactConversion {
806 kCheckForInexactConversion,
807 kDontCheckForInexactConversion
Andrei Popescu31002712010-02-23 13:46:05 +0000808};
809
Ben Murdoch097c5b22016-05-18 11:27:45 +0100810enum class MaxMinKind : int { kMin = 0, kMax = 1 };
Andrei Popescu31002712010-02-23 13:46:05 +0000811
Steve Block44f0eee2011-05-26 01:26:41 +0100812// -----------------------------------------------------------------------------
813// Hints.
814
815// Branch hints are not used on the MIPS. They are defined so that they can
816// appear in shared function signatures, but will be ignored in MIPS
817// implementations.
818enum Hint {
819 no_hint = 0
820};
821
822
823inline Hint NegateHint(Hint hint) {
824 return no_hint;
825}
826
827
828// -----------------------------------------------------------------------------
829// Specific instructions, constants, and masks.
830// These constants are declared in assembler-mips.cc, as they use named
831// registers and other constants.
832
833// addiu(sp, sp, 4) aka Pop() operation or part of Pop(r)
834// operations as post-increment of sp.
835extern const Instr kPopInstruction;
836// addiu(sp, sp, -4) part of Push(r) operation as pre-decrement of sp.
837extern const Instr kPushInstruction;
838// sw(r, MemOperand(sp, 0))
839extern const Instr kPushRegPattern;
Ben Murdoch257744e2011-11-30 15:57:28 +0000840// lw(r, MemOperand(sp, 0))
Steve Block44f0eee2011-05-26 01:26:41 +0100841extern const Instr kPopRegPattern;
842extern const Instr kLwRegFpOffsetPattern;
843extern const Instr kSwRegFpOffsetPattern;
844extern const Instr kLwRegFpNegOffsetPattern;
845extern const Instr kSwRegFpNegOffsetPattern;
846// A mask for the Rt register for push, pop, lw, sw instructions.
847extern const Instr kRtMask;
848extern const Instr kLwSwInstrTypeMask;
849extern const Instr kLwSwInstrArgumentMask;
850extern const Instr kLwSwOffsetMask;
851
Andrei Popescu31002712010-02-23 13:46:05 +0000852// Break 0xfffff, reserved for redirected real time call.
853const Instr rtCallRedirInstr = SPECIAL | BREAK | call_rt_redirected << 6;
854// A nop instruction. (Encoding of sll 0 0 0).
855const Instr nopInstr = 0;
856
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000857static constexpr uint64_t OpcodeToBitNumber(Opcode opcode) {
858 return 1ULL << (static_cast<uint32_t>(opcode) >> kOpcodeShift);
859}
860
861
Andrei Popescu31002712010-02-23 13:46:05 +0000862class Instruction {
863 public:
864 enum {
Steve Block44f0eee2011-05-26 01:26:41 +0100865 kInstrSize = 4,
866 kInstrSizeLog2 = 2,
Andrei Popescu31002712010-02-23 13:46:05 +0000867 // On MIPS PC cannot actually be directly accessed. We behave as if PC was
Steve Block44f0eee2011-05-26 01:26:41 +0100868 // always the value of the current instruction being executed.
Andrei Popescu31002712010-02-23 13:46:05 +0000869 kPCReadOffset = 0
870 };
871
872 // Get the raw instruction bits.
873 inline Instr InstructionBits() const {
874 return *reinterpret_cast<const Instr*>(this);
875 }
876
877 // Set the raw instruction bits to value.
878 inline void SetInstructionBits(Instr value) {
879 *reinterpret_cast<Instr*>(this) = value;
880 }
881
882 // Read one particular bit out of the instruction bits.
883 inline int Bit(int nr) const {
884 return (InstructionBits() >> nr) & 1;
885 }
886
887 // Read a bit field out of the instruction bits.
888 inline int Bits(int hi, int lo) const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000889 return (InstructionBits() >> lo) & ((2U << (hi - lo)) - 1);
Andrei Popescu31002712010-02-23 13:46:05 +0000890 }
891
892 // Instruction type.
893 enum Type {
894 kRegisterType,
895 kImmediateType,
896 kJumpType,
897 kUnsupported = -1
898 };
899
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000900 enum TypeChecks { NORMAL, EXTRA };
Andrei Popescu31002712010-02-23 13:46:05 +0000901
902
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000903 static constexpr uint64_t kOpcodeImmediateTypeMask =
904 OpcodeToBitNumber(REGIMM) | OpcodeToBitNumber(BEQ) |
905 OpcodeToBitNumber(BNE) | OpcodeToBitNumber(BLEZ) |
906 OpcodeToBitNumber(BGTZ) | OpcodeToBitNumber(ADDI) |
907 OpcodeToBitNumber(DADDI) | OpcodeToBitNumber(ADDIU) |
908 OpcodeToBitNumber(SLTI) | OpcodeToBitNumber(SLTIU) |
909 OpcodeToBitNumber(ANDI) | OpcodeToBitNumber(ORI) |
910 OpcodeToBitNumber(XORI) | OpcodeToBitNumber(LUI) |
911 OpcodeToBitNumber(BEQL) | OpcodeToBitNumber(BNEL) |
912 OpcodeToBitNumber(BLEZL) | OpcodeToBitNumber(BGTZL) |
913 OpcodeToBitNumber(POP66) | OpcodeToBitNumber(POP76) |
914 OpcodeToBitNumber(LB) | OpcodeToBitNumber(LH) | OpcodeToBitNumber(LWL) |
915 OpcodeToBitNumber(LW) | OpcodeToBitNumber(LBU) | OpcodeToBitNumber(LHU) |
916 OpcodeToBitNumber(LWR) | OpcodeToBitNumber(SB) | OpcodeToBitNumber(SH) |
917 OpcodeToBitNumber(SWL) | OpcodeToBitNumber(SW) | OpcodeToBitNumber(SWR) |
918 OpcodeToBitNumber(LWC1) | OpcodeToBitNumber(LDC1) |
919 OpcodeToBitNumber(SWC1) | OpcodeToBitNumber(SDC1) |
920 OpcodeToBitNumber(PCREL) | OpcodeToBitNumber(BC) |
921 OpcodeToBitNumber(BALC);
922
923#define FunctionFieldToBitNumber(function) (1ULL << function)
924
925 static const uint64_t kFunctionFieldRegisterTypeMask =
926 FunctionFieldToBitNumber(JR) | FunctionFieldToBitNumber(JALR) |
927 FunctionFieldToBitNumber(BREAK) | FunctionFieldToBitNumber(SLL) |
928 FunctionFieldToBitNumber(SRL) | FunctionFieldToBitNumber(SRA) |
929 FunctionFieldToBitNumber(SLLV) | FunctionFieldToBitNumber(SRLV) |
930 FunctionFieldToBitNumber(SRAV) | FunctionFieldToBitNumber(LSA) |
931 FunctionFieldToBitNumber(MFHI) | FunctionFieldToBitNumber(MFLO) |
932 FunctionFieldToBitNumber(MULT) | FunctionFieldToBitNumber(MULTU) |
933 FunctionFieldToBitNumber(DIV) | FunctionFieldToBitNumber(DIVU) |
934 FunctionFieldToBitNumber(ADD) | FunctionFieldToBitNumber(ADDU) |
935 FunctionFieldToBitNumber(SUB) | FunctionFieldToBitNumber(SUBU) |
936 FunctionFieldToBitNumber(AND) | FunctionFieldToBitNumber(OR) |
937 FunctionFieldToBitNumber(XOR) | FunctionFieldToBitNumber(NOR) |
938 FunctionFieldToBitNumber(SLT) | FunctionFieldToBitNumber(SLTU) |
939 FunctionFieldToBitNumber(TGE) | FunctionFieldToBitNumber(TGEU) |
940 FunctionFieldToBitNumber(TLT) | FunctionFieldToBitNumber(TLTU) |
941 FunctionFieldToBitNumber(TEQ) | FunctionFieldToBitNumber(TNE) |
942 FunctionFieldToBitNumber(MOVZ) | FunctionFieldToBitNumber(MOVN) |
943 FunctionFieldToBitNumber(MOVCI) | FunctionFieldToBitNumber(SELEQZ_S) |
Ben Murdochc5610432016-08-08 18:44:38 +0100944 FunctionFieldToBitNumber(SELNEZ_S) | FunctionFieldToBitNumber(SYNC);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000945
946 // Get the encoding type of the instruction.
947 inline Type InstructionType(TypeChecks checks = NORMAL) const;
948
Andrei Popescu31002712010-02-23 13:46:05 +0000949 // Accessors for the different named fields used in the MIPS encoding.
Steve Block44f0eee2011-05-26 01:26:41 +0100950 inline Opcode OpcodeValue() const {
Andrei Popescu31002712010-02-23 13:46:05 +0000951 return static_cast<Opcode>(
952 Bits(kOpcodeShift + kOpcodeBits - 1, kOpcodeShift));
953 }
954
Steve Block44f0eee2011-05-26 01:26:41 +0100955 inline int RsValue() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000956 DCHECK(InstructionType() == kRegisterType ||
Andrei Popescu31002712010-02-23 13:46:05 +0000957 InstructionType() == kImmediateType);
958 return Bits(kRsShift + kRsBits - 1, kRsShift);
959 }
960
Steve Block44f0eee2011-05-26 01:26:41 +0100961 inline int RtValue() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000962 DCHECK(InstructionType() == kRegisterType ||
Andrei Popescu31002712010-02-23 13:46:05 +0000963 InstructionType() == kImmediateType);
964 return Bits(kRtShift + kRtBits - 1, kRtShift);
965 }
966
Steve Block44f0eee2011-05-26 01:26:41 +0100967 inline int RdValue() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000968 DCHECK(InstructionType() == kRegisterType);
Andrei Popescu31002712010-02-23 13:46:05 +0000969 return Bits(kRdShift + kRdBits - 1, kRdShift);
970 }
971
Steve Block44f0eee2011-05-26 01:26:41 +0100972 inline int SaValue() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000973 DCHECK(InstructionType() == kRegisterType);
Andrei Popescu31002712010-02-23 13:46:05 +0000974 return Bits(kSaShift + kSaBits - 1, kSaShift);
975 }
976
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000977 inline int LsaSaValue() const {
978 DCHECK(InstructionType() == kRegisterType);
979 return Bits(kSaShift + kLsaSaBits - 1, kSaShift);
980 }
981
Steve Block44f0eee2011-05-26 01:26:41 +0100982 inline int FunctionValue() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000983 DCHECK(InstructionType() == kRegisterType ||
Andrei Popescu31002712010-02-23 13:46:05 +0000984 InstructionType() == kImmediateType);
985 return Bits(kFunctionShift + kFunctionBits - 1, kFunctionShift);
986 }
987
Steve Block44f0eee2011-05-26 01:26:41 +0100988 inline int FdValue() const {
989 return Bits(kFdShift + kFdBits - 1, kFdShift);
Andrei Popescu31002712010-02-23 13:46:05 +0000990 }
991
Steve Block44f0eee2011-05-26 01:26:41 +0100992 inline int FsValue() const {
993 return Bits(kFsShift + kFsBits - 1, kFsShift);
994 }
995
996 inline int FtValue() const {
997 return Bits(kFtShift + kFtBits - 1, kFtShift);
998 }
999
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001000 inline int FrValue() const {
1001 return Bits(kFrShift + kFrBits -1, kFrShift);
1002 }
1003
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001004 inline int Bp2Value() const {
1005 DCHECK(InstructionType() == kRegisterType);
1006 return Bits(kBp2Shift + kBp2Bits - 1, kBp2Shift);
1007 }
1008
Steve Block44f0eee2011-05-26 01:26:41 +01001009 // Float Compare condition code instruction bits.
1010 inline int FCccValue() const {
1011 return Bits(kFCccShift + kFCccBits - 1, kFCccShift);
1012 }
1013
1014 // Float Branch condition code instruction bits.
1015 inline int FBccValue() const {
1016 return Bits(kFBccShift + kFBccBits - 1, kFBccShift);
1017 }
1018
1019 // Float Branch true/false instruction bit.
1020 inline int FBtrueValue() const {
1021 return Bits(kFBtrueShift + kFBtrueBits - 1, kFBtrueShift);
Andrei Popescu31002712010-02-23 13:46:05 +00001022 }
1023
1024 // Return the fields at their original place in the instruction encoding.
1025 inline Opcode OpcodeFieldRaw() const {
1026 return static_cast<Opcode>(InstructionBits() & kOpcodeMask);
1027 }
1028
1029 inline int RsFieldRaw() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001030 DCHECK(InstructionType() == kRegisterType ||
Andrei Popescu31002712010-02-23 13:46:05 +00001031 InstructionType() == kImmediateType);
1032 return InstructionBits() & kRsFieldMask;
1033 }
1034
Steve Block44f0eee2011-05-26 01:26:41 +01001035 // Same as above function, but safe to call within InstructionType().
1036 inline int RsFieldRawNoAssert() const {
1037 return InstructionBits() & kRsFieldMask;
1038 }
1039
Andrei Popescu31002712010-02-23 13:46:05 +00001040 inline int RtFieldRaw() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001041 DCHECK(InstructionType() == kRegisterType ||
Andrei Popescu31002712010-02-23 13:46:05 +00001042 InstructionType() == kImmediateType);
1043 return InstructionBits() & kRtFieldMask;
1044 }
1045
1046 inline int RdFieldRaw() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001047 DCHECK(InstructionType() == kRegisterType);
Andrei Popescu31002712010-02-23 13:46:05 +00001048 return InstructionBits() & kRdFieldMask;
1049 }
1050
1051 inline int SaFieldRaw() const {
Andrei Popescu31002712010-02-23 13:46:05 +00001052 return InstructionBits() & kSaFieldMask;
1053 }
1054
1055 inline int FunctionFieldRaw() const {
1056 return InstructionBits() & kFunctionFieldMask;
1057 }
1058
1059 // Get the secondary field according to the opcode.
Steve Block44f0eee2011-05-26 01:26:41 +01001060 inline int SecondaryValue() const {
Andrei Popescu31002712010-02-23 13:46:05 +00001061 Opcode op = OpcodeFieldRaw();
1062 switch (op) {
1063 case SPECIAL:
1064 case SPECIAL2:
Steve Block44f0eee2011-05-26 01:26:41 +01001065 return FunctionValue();
Andrei Popescu31002712010-02-23 13:46:05 +00001066 case COP1:
Steve Block44f0eee2011-05-26 01:26:41 +01001067 return RsValue();
Andrei Popescu31002712010-02-23 13:46:05 +00001068 case REGIMM:
Steve Block44f0eee2011-05-26 01:26:41 +01001069 return RtValue();
Andrei Popescu31002712010-02-23 13:46:05 +00001070 default:
1071 return NULLSF;
1072 }
1073 }
1074
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001075 inline int32_t ImmValue(int bits) const {
1076 DCHECK(InstructionType() == kImmediateType);
1077 return Bits(bits - 1, 0);
1078 }
1079
Steve Block44f0eee2011-05-26 01:26:41 +01001080 inline int32_t Imm16Value() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001081 DCHECK(InstructionType() == kImmediateType);
Andrei Popescu31002712010-02-23 13:46:05 +00001082 return Bits(kImm16Shift + kImm16Bits - 1, kImm16Shift);
1083 }
1084
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001085 inline int32_t Imm18Value() const {
1086 DCHECK(InstructionType() == kImmediateType);
1087 return Bits(kImm18Shift + kImm18Bits - 1, kImm18Shift);
1088 }
1089
1090 inline int32_t Imm19Value() const {
1091 DCHECK(InstructionType() == kImmediateType);
1092 return Bits(kImm19Shift + kImm19Bits - 1, kImm19Shift);
1093 }
1094
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001095 inline int32_t Imm21Value() const {
1096 DCHECK(InstructionType() == kImmediateType);
1097 return Bits(kImm21Shift + kImm21Bits - 1, kImm21Shift);
1098 }
1099
Steve Block44f0eee2011-05-26 01:26:41 +01001100 inline int32_t Imm26Value() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001101 DCHECK((InstructionType() == kJumpType) ||
1102 (InstructionType() == kImmediateType));
Ben Murdoch589d6972011-11-30 16:04:58 +00001103 return Bits(kImm26Shift + kImm26Bits - 1, kImm26Shift);
Andrei Popescu31002712010-02-23 13:46:05 +00001104 }
1105
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001106 static bool IsForbiddenAfterBranchInstr(Instr instr);
1107
1108 // Say if the instruction should not be used in a branch delay slot or
1109 // immediately after a compact branch.
1110 inline bool IsForbiddenAfterBranch() const {
1111 return IsForbiddenAfterBranchInstr(InstructionBits());
1112 }
1113
1114 inline bool IsForbiddenInBranchDelay() const {
1115 return IsForbiddenAfterBranch();
1116 }
1117
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001118 // Say if the instruction 'links'. e.g. jal, bal.
Steve Block44f0eee2011-05-26 01:26:41 +01001119 bool IsLinkingInstruction() const;
Andrei Popescu31002712010-02-23 13:46:05 +00001120 // Say if the instruction is a break or a trap.
Steve Block44f0eee2011-05-26 01:26:41 +01001121 bool IsTrap() const;
Andrei Popescu31002712010-02-23 13:46:05 +00001122
1123 // Instructions are read of out a code stream. The only way to get a
1124 // reference to an instruction is to convert a pointer. There is no way
1125 // to allocate or create instances of class Instruction.
1126 // Use the At(pc) function to create references to Instruction.
Ben Murdoch257744e2011-11-30 15:57:28 +00001127 static Instruction* At(byte* pc) {
Andrei Popescu31002712010-02-23 13:46:05 +00001128 return reinterpret_cast<Instruction*>(pc);
1129 }
1130
1131 private:
1132 // We need to prevent the creation of instances of class Instruction.
1133 DISALLOW_IMPLICIT_CONSTRUCTORS(Instruction);
1134};
1135
1136
1137// -----------------------------------------------------------------------------
1138// MIPS assembly various constants.
1139
Steve Block44f0eee2011-05-26 01:26:41 +01001140// C/C++ argument slots size.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001141const int kCArgSlotCount = 4;
1142const int kCArgsSlotsSize = kCArgSlotCount * Instruction::kInstrSize;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001143const int kInvalidStackOffset = -1;
Steve Block44f0eee2011-05-26 01:26:41 +01001144// JS argument slots size.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001145const int kJSArgsSlotsSize = 0 * Instruction::kInstrSize;
Steve Block44f0eee2011-05-26 01:26:41 +01001146// Assembly builtins argument slots size.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001147const int kBArgsSlotsSize = 0 * Instruction::kInstrSize;
Andrei Popescu31002712010-02-23 13:46:05 +00001148
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001149const int kBranchReturnOffset = 2 * Instruction::kInstrSize;
Andrei Popescu31002712010-02-23 13:46:05 +00001150
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001151
1152Instruction::Type Instruction::InstructionType(TypeChecks checks) const {
1153 if (checks == EXTRA) {
1154 if (OpcodeToBitNumber(OpcodeFieldRaw()) & kOpcodeImmediateTypeMask) {
1155 return kImmediateType;
1156 }
1157 }
1158 switch (OpcodeFieldRaw()) {
1159 case SPECIAL:
1160 if (checks == EXTRA) {
1161 if (FunctionFieldToBitNumber(FunctionFieldRaw()) &
1162 kFunctionFieldRegisterTypeMask) {
1163 return kRegisterType;
1164 } else {
1165 return kUnsupported;
1166 }
1167 } else {
1168 return kRegisterType;
1169 }
1170 break;
1171 case SPECIAL2:
1172 switch (FunctionFieldRaw()) {
1173 case MUL:
1174 case CLZ:
1175 return kRegisterType;
1176 default:
1177 return kUnsupported;
1178 }
1179 break;
1180 case SPECIAL3:
1181 switch (FunctionFieldRaw()) {
1182 case INS:
1183 case EXT:
1184 return kRegisterType;
1185 case BSHFL: {
1186 int sa = SaFieldRaw() >> kSaShift;
1187 switch (sa) {
1188 case BITSWAP:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001189 case WSBH:
1190 case SEB:
1191 case SEH:
Ben Murdoch61f157c2016-09-16 13:49:30 +01001192 return kRegisterType;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001193 }
1194 sa >>= kBp2Bits;
1195 switch (sa) {
1196 case ALIGN:
1197 return kRegisterType;
1198 default:
1199 return kUnsupported;
1200 }
1201 }
1202 default:
1203 return kUnsupported;
1204 }
1205 break;
1206 case COP1: // Coprocessor instructions.
1207 switch (RsFieldRawNoAssert()) {
1208 case BC1: // Branch on coprocessor condition.
1209 case BC1EQZ:
1210 case BC1NEZ:
1211 return kImmediateType;
1212 default:
1213 return kRegisterType;
1214 }
1215 break;
1216 case COP1X:
1217 return kRegisterType;
1218
1219 // 26 bits immediate type instructions. e.g.: j imm26.
1220 case J:
1221 case JAL:
1222 return kJumpType;
1223
1224 default:
1225 if (checks == NORMAL) {
1226 return kImmediateType;
1227 } else {
1228 return kUnsupported;
1229 }
1230 }
1231}
1232
1233#undef OpcodeToBitNumber
1234#undef FunctionFieldToBitNumber
1235} // namespace internal
1236} // namespace v8
Andrei Popescu31002712010-02-23 13:46:05 +00001237
1238#endif // #ifndef V8_MIPS_CONSTANTS_H_