blob: 49142515c7648f32be6e7d8e38208808d1656673 [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
111
112#define __STDC_FORMAT_MACROS
113#include <inttypes.h>
Ben Murdoch257744e2011-11-30 15:57:28 +0000114
Andrei Popescu31002712010-02-23 13:46:05 +0000115// 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 Block44f0eee2011-05-26 01:26:41 +0100122namespace v8 {
123namespace internal {
Andrei Popescu31002712010-02-23 13:46:05 +0000124
125// -----------------------------------------------------------------------------
Ben Murdoch257744e2011-11-30 15:57:28 +0000126// Registers and FPURegisters.
Andrei Popescu31002712010-02-23 13:46:05 +0000127
128// Number of general purpose registers.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100129const int kNumRegisters = 32;
130const int kInvalidRegister = -1;
Andrei Popescu31002712010-02-23 13:46:05 +0000131
132// Number of registers with HI, LO, and pc.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100133const int kNumSimuRegisters = 35;
Andrei Popescu31002712010-02-23 13:46:05 +0000134
135// In the simulator, the PC register is simulated as the 34th register.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100136const int kPCRegister = 34;
Andrei Popescu31002712010-02-23 13:46:05 +0000137
138// Number coprocessor registers.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100139const int kNumFPURegisters = 32;
140const int kInvalidFPURegister = -1;
Andrei Popescu31002712010-02-23 13:46:05 +0000141
Steve Block44f0eee2011-05-26 01:26:41 +0100142// FPU (coprocessor 1) control registers. Currently only FCSR is implemented.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100143const int kFCSRRegister = 31;
144const int kInvalidFPUControlRegister = -1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000145const uint32_t kFPUInvalidResult = static_cast<uint32_t>(1 << 31) - 1;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000146const int32_t kFPUInvalidResultNegative = static_cast<int32_t>(1 << 31);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000147const uint64_t kFPU64InvalidResult =
148 static_cast<uint64_t>(static_cast<uint64_t>(1) << 63) - 1;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000149const int64_t kFPU64InvalidResultNegative =
150 static_cast<int64_t>(static_cast<uint64_t>(1) << 63);
Steve Block44f0eee2011-05-26 01:26:41 +0100151
152// FCSR constants.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100153const uint32_t kFCSRInexactFlagBit = 2;
154const uint32_t kFCSRUnderflowFlagBit = 3;
155const uint32_t kFCSROverflowFlagBit = 4;
156const uint32_t kFCSRDivideByZeroFlagBit = 5;
157const uint32_t kFCSRInvalidOpFlagBit = 6;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000158const uint32_t kFCSRNaN2008FlagBit = 18;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000159
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100160const uint32_t kFCSRInexactFlagMask = 1 << kFCSRInexactFlagBit;
161const uint32_t kFCSRUnderflowFlagMask = 1 << kFCSRUnderflowFlagBit;
162const uint32_t kFCSROverflowFlagMask = 1 << kFCSROverflowFlagBit;
163const uint32_t kFCSRDivideByZeroFlagMask = 1 << kFCSRDivideByZeroFlagBit;
164const uint32_t kFCSRInvalidOpFlagMask = 1 << kFCSRInvalidOpFlagBit;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000165const uint32_t kFCSRNaN2008FlagMask = 1 << kFCSRNaN2008FlagBit;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000166
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100167const uint32_t kFCSRFlagMask =
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000168 kFCSRInexactFlagMask |
169 kFCSRUnderflowFlagMask |
170 kFCSROverflowFlagMask |
171 kFCSRDivideByZeroFlagMask |
172 kFCSRInvalidOpFlagMask;
173
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100174const uint32_t kFCSRExceptionFlagMask = kFCSRFlagMask ^ kFCSRInexactFlagMask;
Steve Block44f0eee2011-05-26 01:26:41 +0100175
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000176// 'pref' instruction hints
177const int32_t kPrefHintLoad = 0;
178const int32_t kPrefHintStore = 1;
179const int32_t kPrefHintLoadStreamed = 4;
180const int32_t kPrefHintStoreStreamed = 5;
181const int32_t kPrefHintLoadRetained = 6;
182const int32_t kPrefHintStoreRetained = 7;
183const int32_t kPrefHintWritebackInvalidate = 25;
184const int32_t kPrefHintPrepareForStore = 30;
185
Andrei Popescu31002712010-02-23 13:46:05 +0000186// Helper functions for converting between register numbers and names.
187class Registers {
188 public:
189 // Return the name of the register.
190 static const char* Name(int reg);
191
192 // Lookup the register number for the name provided.
193 static int Number(const char* name);
194
195 struct RegisterAlias {
196 int reg;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100197 const char* name;
Andrei Popescu31002712010-02-23 13:46:05 +0000198 };
199
200 static const int32_t kMaxValue = 0x7fffffff;
201 static const int32_t kMinValue = 0x80000000;
202
203 private:
Andrei Popescu31002712010-02-23 13:46:05 +0000204 static const char* names_[kNumSimuRegisters];
205 static const RegisterAlias aliases_[];
206};
207
208// Helper functions for converting between register numbers and names.
Steve Block44f0eee2011-05-26 01:26:41 +0100209class FPURegisters {
Andrei Popescu31002712010-02-23 13:46:05 +0000210 public:
211 // Return the name of the register.
212 static const char* Name(int reg);
213
214 // Lookup the register number for the name provided.
215 static int Number(const char* name);
216
217 struct RegisterAlias {
218 int creg;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100219 const char* name;
Andrei Popescu31002712010-02-23 13:46:05 +0000220 };
221
222 private:
Steve Block44f0eee2011-05-26 01:26:41 +0100223 static const char* names_[kNumFPURegisters];
Andrei Popescu31002712010-02-23 13:46:05 +0000224 static const RegisterAlias aliases_[];
225};
226
227
228// -----------------------------------------------------------------------------
229// Instructions encoding constants.
230
231// On MIPS all instructions are 32 bits.
232typedef int32_t Instr;
233
Andrei Popescu31002712010-02-23 13:46:05 +0000234// Special Software Interrupt codes when used in the presence of the MIPS
235// simulator.
236enum SoftwareInterruptCodes {
237 // Transition to C code.
238 call_rt_redirected = 0xfffff
239};
240
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000241// On MIPS Simulator breakpoints can have different codes:
242// - Breaks between 0 and kMaxWatchpointCode are treated as simple watchpoints,
243// the simulator will run through them and print the registers.
244// - Breaks between kMaxWatchpointCode and kMaxStopCode are treated as stop()
245// instructions (see Assembler::stop()).
246// - Breaks larger than kMaxStopCode are simple breaks, dropping you into the
247// debugger.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100248const uint32_t kMaxWatchpointCode = 31;
249const uint32_t kMaxStopCode = 127;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000250STATIC_ASSERT(kMaxWatchpointCode < kMaxStopCode);
251
252
Andrei Popescu31002712010-02-23 13:46:05 +0000253// ----- Fields offset and length.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100254const int kOpcodeShift = 26;
255const int kOpcodeBits = 6;
256const int kRsShift = 21;
257const int kRsBits = 5;
258const int kRtShift = 16;
259const int kRtBits = 5;
260const int kRdShift = 11;
261const int kRdBits = 5;
262const int kSaShift = 6;
263const int kSaBits = 5;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000264const int kLsaSaBits = 2;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100265const int kFunctionShift = 0;
266const int kFunctionBits = 6;
267const int kLuiShift = 16;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000268const int kBp2Shift = 6;
269const int kBp2Bits = 2;
Andrei Popescu31002712010-02-23 13:46:05 +0000270
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100271const int kImm16Shift = 0;
272const int kImm16Bits = 16;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000273const int kImm18Shift = 0;
274const int kImm18Bits = 18;
275const int kImm19Shift = 0;
276const int kImm19Bits = 19;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000277const int kImm21Shift = 0;
278const int kImm21Bits = 21;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100279const int kImm26Shift = 0;
280const int kImm26Bits = 26;
281const int kImm28Shift = 0;
282const int kImm28Bits = 28;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000283const int kImm32Shift = 0;
284const int kImm32Bits = 32;
Andrei Popescu31002712010-02-23 13:46:05 +0000285
Ben Murdoch589d6972011-11-30 16:04:58 +0000286// In branches and jumps immediate fields point to words, not bytes,
287// and are therefore shifted by 2.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100288const int kImmFieldShift = 2;
Ben Murdoch589d6972011-11-30 16:04:58 +0000289
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000290const int kFrBits = 5;
291const int kFrShift = 21;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100292const int kFsShift = 11;
293const int kFsBits = 5;
294const int kFtShift = 16;
295const int kFtBits = 5;
296const int kFdShift = 6;
297const int kFdBits = 5;
298const int kFCccShift = 8;
299const int kFCccBits = 3;
300const int kFBccShift = 18;
301const int kFBccBits = 3;
302const int kFBtrueShift = 16;
303const int kFBtrueBits = 1;
Andrei Popescu31002712010-02-23 13:46:05 +0000304
Ben Murdoch257744e2011-11-30 15:57:28 +0000305// ----- Miscellaneous useful masks.
Andrei Popescu31002712010-02-23 13:46:05 +0000306// Instruction bit masks.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000307const int kOpcodeMask = ((1 << kOpcodeBits) - 1) << kOpcodeShift;
308const int kImm16Mask = ((1 << kImm16Bits) - 1) << kImm16Shift;
309const int kImm18Mask = ((1 << kImm18Bits) - 1) << kImm18Shift;
310const int kImm19Mask = ((1 << kImm19Bits) - 1) << kImm19Shift;
311const int kImm21Mask = ((1 << kImm21Bits) - 1) << kImm21Shift;
312const int kImm26Mask = ((1 << kImm26Bits) - 1) << kImm26Shift;
313const int kImm28Mask = ((1 << kImm28Bits) - 1) << kImm28Shift;
314const int kRsFieldMask = ((1 << kRsBits) - 1) << kRsShift;
315const int kRtFieldMask = ((1 << kRtBits) - 1) << kRtShift;
316const int kRdFieldMask = ((1 << kRdBits) - 1) << kRdShift;
317const int kSaFieldMask = ((1 << kSaBits) - 1) << kSaShift;
318const int kFunctionFieldMask = ((1 << kFunctionBits) - 1) << kFunctionShift;
Andrei Popescu31002712010-02-23 13:46:05 +0000319// Misc masks.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000320const int kHiMask = 0xffff << 16;
321const int kLoMask = 0xffff;
322const int kSignMask = 0x80000000;
323const int kJumpAddrMask = (1 << (kImm26Bits + kImmFieldShift)) - 1;
Andrei Popescu31002712010-02-23 13:46:05 +0000324
325// ----- MIPS Opcodes and Function Fields.
326// We use this presentation to stay close to the table representation in
327// MIPS32 Architecture For Programmers, Volume II: The MIPS32 Instruction Set.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000328enum Opcode : uint32_t {
329 SPECIAL = 0U << kOpcodeShift,
330 REGIMM = 1U << kOpcodeShift,
Andrei Popescu31002712010-02-23 13:46:05 +0000331
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000332 J = ((0U << 3) + 2) << kOpcodeShift,
333 JAL = ((0U << 3) + 3) << kOpcodeShift,
334 BEQ = ((0U << 3) + 4) << kOpcodeShift,
335 BNE = ((0U << 3) + 5) << kOpcodeShift,
336 BLEZ = ((0U << 3) + 6) << kOpcodeShift,
337 BGTZ = ((0U << 3) + 7) << kOpcodeShift,
Andrei Popescu31002712010-02-23 13:46:05 +0000338
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000339 ADDI = ((1U << 3) + 0) << kOpcodeShift,
340 ADDIU = ((1U << 3) + 1) << kOpcodeShift,
341 SLTI = ((1U << 3) + 2) << kOpcodeShift,
342 SLTIU = ((1U << 3) + 3) << kOpcodeShift,
343 ANDI = ((1U << 3) + 4) << kOpcodeShift,
344 ORI = ((1U << 3) + 5) << kOpcodeShift,
345 XORI = ((1U << 3) + 6) << kOpcodeShift,
346 LUI = ((1U << 3) + 7) << kOpcodeShift, // LUI/AUI family.
Andrei Popescu31002712010-02-23 13:46:05 +0000347
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000348 BEQC = ((2U << 3) + 0) << kOpcodeShift,
349 COP1 = ((2U << 3) + 1) << kOpcodeShift, // Coprocessor 1 class.
350 BEQL = ((2U << 3) + 4) << kOpcodeShift,
351 BNEL = ((2U << 3) + 5) << kOpcodeShift,
352 BLEZL = ((2U << 3) + 6) << kOpcodeShift,
353 BGTZL = ((2U << 3) + 7) << kOpcodeShift,
Andrei Popescu31002712010-02-23 13:46:05 +0000354
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000355 DADDI = ((3U << 3) + 0) << kOpcodeShift, // This is also BNEC.
356 SPECIAL2 = ((3U << 3) + 4) << kOpcodeShift,
357 SPECIAL3 = ((3U << 3) + 7) << kOpcodeShift,
Andrei Popescu31002712010-02-23 13:46:05 +0000358
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000359 LB = ((4U << 3) + 0) << kOpcodeShift,
360 LH = ((4U << 3) + 1) << kOpcodeShift,
361 LWL = ((4U << 3) + 2) << kOpcodeShift,
362 LW = ((4U << 3) + 3) << kOpcodeShift,
363 LBU = ((4U << 3) + 4) << kOpcodeShift,
364 LHU = ((4U << 3) + 5) << kOpcodeShift,
365 LWR = ((4U << 3) + 6) << kOpcodeShift,
366 SB = ((5U << 3) + 0) << kOpcodeShift,
367 SH = ((5U << 3) + 1) << kOpcodeShift,
368 SWL = ((5U << 3) + 2) << kOpcodeShift,
369 SW = ((5U << 3) + 3) << kOpcodeShift,
370 SWR = ((5U << 3) + 6) << kOpcodeShift,
Andrei Popescu31002712010-02-23 13:46:05 +0000371
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000372 LWC1 = ((6U << 3) + 1) << kOpcodeShift,
373 BC = ((6U << 3) + 2) << kOpcodeShift,
374 LDC1 = ((6U << 3) + 5) << kOpcodeShift,
375 POP66 = ((6U << 3) + 6) << kOpcodeShift, // beqzc, jic
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000376
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000377 PREF = ((6U << 3) + 3) << kOpcodeShift,
Andrei Popescu31002712010-02-23 13:46:05 +0000378
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000379 SWC1 = ((7U << 3) + 1) << kOpcodeShift,
380 BALC = ((7U << 3) + 2) << kOpcodeShift,
381 PCREL = ((7U << 3) + 3) << kOpcodeShift,
382 SDC1 = ((7U << 3) + 5) << kOpcodeShift,
383 POP76 = ((7U << 3) + 6) << kOpcodeShift, // bnezc, jialc
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000384
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000385 COP1X = ((1U << 4) + 3) << kOpcodeShift,
386
387 // New r6 instruction.
388 POP06 = BLEZ, // bgeuc/bleuc, blezalc, bgezalc
389 POP07 = BGTZ, // bltuc/bgtuc, bgtzalc, bltzalc
390 POP10 = ADDI, // beqzalc, bovc, beqc
391 POP26 = BLEZL, // bgezc, blezc, bgec/blec
392 POP27 = BGTZL, // bgtzc, bltzc, bltc/bgtc
Ben Murdoch097c5b22016-05-18 11:27:45 +0100393 POP30 = DADDI, // bnezalc, bnvc, bnec
Andrei Popescu31002712010-02-23 13:46:05 +0000394};
395
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000396enum SecondaryField : uint32_t {
Andrei Popescu31002712010-02-23 13:46:05 +0000397 // SPECIAL Encoding of Function Field.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000398 SLL = ((0U << 3) + 0),
399 MOVCI = ((0U << 3) + 1),
400 SRL = ((0U << 3) + 2),
401 SRA = ((0U << 3) + 3),
402 SLLV = ((0U << 3) + 4),
403 LSA = ((0U << 3) + 5),
404 SRLV = ((0U << 3) + 6),
405 SRAV = ((0U << 3) + 7),
Andrei Popescu31002712010-02-23 13:46:05 +0000406
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000407 JR = ((1U << 3) + 0),
408 JALR = ((1U << 3) + 1),
409 MOVZ = ((1U << 3) + 2),
410 MOVN = ((1U << 3) + 3),
411 BREAK = ((1U << 3) + 5),
Andrei Popescu31002712010-02-23 13:46:05 +0000412
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000413 MFHI = ((2U << 3) + 0),
414 CLZ_R6 = ((2U << 3) + 0),
415 CLO_R6 = ((2U << 3) + 1),
416 MFLO = ((2U << 3) + 2),
Andrei Popescu31002712010-02-23 13:46:05 +0000417
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000418 MULT = ((3U << 3) + 0),
419 MULTU = ((3U << 3) + 1),
420 DIV = ((3U << 3) + 2),
421 DIVU = ((3U << 3) + 3),
Andrei Popescu31002712010-02-23 13:46:05 +0000422
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000423 ADD = ((4U << 3) + 0),
424 ADDU = ((4U << 3) + 1),
425 SUB = ((4U << 3) + 2),
426 SUBU = ((4U << 3) + 3),
427 AND = ((4U << 3) + 4),
428 OR = ((4U << 3) + 5),
429 XOR = ((4U << 3) + 6),
430 NOR = ((4U << 3) + 7),
Andrei Popescu31002712010-02-23 13:46:05 +0000431
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000432 SLT = ((5U << 3) + 2),
433 SLTU = ((5U << 3) + 3),
Andrei Popescu31002712010-02-23 13:46:05 +0000434
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000435 TGE = ((6U << 3) + 0),
436 TGEU = ((6U << 3) + 1),
437 TLT = ((6U << 3) + 2),
438 TLTU = ((6U << 3) + 3),
439 TEQ = ((6U << 3) + 4),
440 SELEQZ_S = ((6U << 3) + 5),
441 TNE = ((6U << 3) + 6),
442 SELNEZ_S = ((6U << 3) + 7),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000443
444 // Multiply integers in r6.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000445 MUL_MUH = ((3U << 3) + 0), // MUL, MUH.
446 MUL_MUH_U = ((3U << 3) + 1), // MUL_U, MUH_U.
447 RINT = ((3U << 3) + 2),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000448
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000449 MUL_OP = ((0U << 3) + 2),
450 MUH_OP = ((0U << 3) + 3),
451 DIV_OP = ((0U << 3) + 2),
452 MOD_OP = ((0U << 3) + 3),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000453
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000454 DIV_MOD = ((3U << 3) + 2),
455 DIV_MOD_U = ((3U << 3) + 3),
Andrei Popescu31002712010-02-23 13:46:05 +0000456
457 // SPECIAL2 Encoding of Function Field.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000458 MUL = ((0U << 3) + 2),
459 CLZ = ((4U << 3) + 0),
460 CLO = ((4U << 3) + 1),
Steve Block44f0eee2011-05-26 01:26:41 +0100461
462 // SPECIAL3 Encoding of Function Field.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000463 EXT = ((0U << 3) + 0),
464 INS = ((0U << 3) + 4),
465 BSHFL = ((4U << 3) + 0),
466
467 // SPECIAL3 Encoding of sa Field.
468 BITSWAP = ((0U << 3) + 0),
469 ALIGN = ((0U << 3) + 2),
470 WSBH = ((0U << 3) + 2),
471 SEB = ((2U << 3) + 0),
472 SEH = ((3U << 3) + 0),
Andrei Popescu31002712010-02-23 13:46:05 +0000473
474 // REGIMM encoding of rt Field.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000475 BLTZ = ((0U << 3) + 0) << 16,
476 BGEZ = ((0U << 3) + 1) << 16,
477 BLTZAL = ((2U << 3) + 0) << 16,
478 BGEZAL = ((2U << 3) + 1) << 16,
479 BGEZALL = ((2U << 3) + 3) << 16,
Andrei Popescu31002712010-02-23 13:46:05 +0000480
481 // COP1 Encoding of rs Field.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000482 MFC1 = ((0U << 3) + 0) << 21,
483 CFC1 = ((0U << 3) + 2) << 21,
484 MFHC1 = ((0U << 3) + 3) << 21,
485 MTC1 = ((0U << 3) + 4) << 21,
486 CTC1 = ((0U << 3) + 6) << 21,
487 MTHC1 = ((0U << 3) + 7) << 21,
488 BC1 = ((1U << 3) + 0) << 21,
489 S = ((2U << 3) + 0) << 21,
490 D = ((2U << 3) + 1) << 21,
491 W = ((2U << 3) + 4) << 21,
492 L = ((2U << 3) + 5) << 21,
493 PS = ((2U << 3) + 6) << 21,
Andrei Popescu31002712010-02-23 13:46:05 +0000494 // COP1 Encoding of Function Field When rs=S.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000495
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000496 ADD_S = ((0U << 3) + 0),
497 SUB_S = ((0U << 3) + 1),
498 MUL_S = ((0U << 3) + 2),
499 DIV_S = ((0U << 3) + 3),
500 ABS_S = ((0U << 3) + 5),
501 SQRT_S = ((0U << 3) + 4),
502 MOV_S = ((0U << 3) + 6),
503 NEG_S = ((0U << 3) + 7),
504 ROUND_L_S = ((1U << 3) + 0),
505 TRUNC_L_S = ((1U << 3) + 1),
506 CEIL_L_S = ((1U << 3) + 2),
507 FLOOR_L_S = ((1U << 3) + 3),
508 ROUND_W_S = ((1U << 3) + 4),
509 TRUNC_W_S = ((1U << 3) + 5),
510 CEIL_W_S = ((1U << 3) + 6),
511 FLOOR_W_S = ((1U << 3) + 7),
512 RECIP_S = ((2U << 3) + 5),
513 RSQRT_S = ((2U << 3) + 6),
514 CLASS_S = ((3U << 3) + 3),
515 CVT_D_S = ((4U << 3) + 1),
516 CVT_W_S = ((4U << 3) + 4),
517 CVT_L_S = ((4U << 3) + 5),
518 CVT_PS_S = ((4U << 3) + 6),
519
520 // COP1 Encoding of Function Field When rs=D.
521 ADD_D = ((0U << 3) + 0),
522 SUB_D = ((0U << 3) + 1),
523 MUL_D = ((0U << 3) + 2),
524 DIV_D = ((0U << 3) + 3),
525 SQRT_D = ((0U << 3) + 4),
526 ABS_D = ((0U << 3) + 5),
527 MOV_D = ((0U << 3) + 6),
528 NEG_D = ((0U << 3) + 7),
529 ROUND_L_D = ((1U << 3) + 0),
530 TRUNC_L_D = ((1U << 3) + 1),
531 CEIL_L_D = ((1U << 3) + 2),
532 FLOOR_L_D = ((1U << 3) + 3),
533 ROUND_W_D = ((1U << 3) + 4),
534 TRUNC_W_D = ((1U << 3) + 5),
535 CEIL_W_D = ((1U << 3) + 6),
536 FLOOR_W_D = ((1U << 3) + 7),
537 RECIP_D = ((2U << 3) + 5),
538 RSQRT_D = ((2U << 3) + 6),
539 CLASS_D = ((3U << 3) + 3),
540 MIN = ((3U << 3) + 4),
541 MINA = ((3U << 3) + 5),
542 MAX = ((3U << 3) + 6),
543 MAXA = ((3U << 3) + 7),
544 CVT_S_D = ((4U << 3) + 0),
545 CVT_W_D = ((4U << 3) + 4),
546 CVT_L_D = ((4U << 3) + 5),
547 C_F_D = ((6U << 3) + 0),
548 C_UN_D = ((6U << 3) + 1),
549 C_EQ_D = ((6U << 3) + 2),
550 C_UEQ_D = ((6U << 3) + 3),
551 C_OLT_D = ((6U << 3) + 4),
552 C_ULT_D = ((6U << 3) + 5),
553 C_OLE_D = ((6U << 3) + 6),
554 C_ULE_D = ((6U << 3) + 7),
555
556 // COP1 Encoding of Function Field When rs=W or L.
557 CVT_S_W = ((4U << 3) + 0),
558 CVT_D_W = ((4U << 3) + 1),
559 CVT_S_L = ((4U << 3) + 0),
560 CVT_D_L = ((4U << 3) + 1),
561 BC1EQZ = ((2U << 2) + 1) << 21,
562 BC1NEZ = ((3U << 2) + 1) << 21,
563 // COP1 CMP positive predicates Bit 5..4 = 00.
564 CMP_AF = ((0U << 3) + 0),
565 CMP_UN = ((0U << 3) + 1),
566 CMP_EQ = ((0U << 3) + 2),
567 CMP_UEQ = ((0U << 3) + 3),
568 CMP_LT = ((0U << 3) + 4),
569 CMP_ULT = ((0U << 3) + 5),
570 CMP_LE = ((0U << 3) + 6),
571 CMP_ULE = ((0U << 3) + 7),
572 CMP_SAF = ((1U << 3) + 0),
573 CMP_SUN = ((1U << 3) + 1),
574 CMP_SEQ = ((1U << 3) + 2),
575 CMP_SUEQ = ((1U << 3) + 3),
576 CMP_SSLT = ((1U << 3) + 4),
577 CMP_SSULT = ((1U << 3) + 5),
578 CMP_SLE = ((1U << 3) + 6),
579 CMP_SULE = ((1U << 3) + 7),
580 // COP1 CMP negative predicates Bit 5..4 = 01.
581 CMP_AT = ((2U << 3) + 0), // Reserved, not implemented.
582 CMP_OR = ((2U << 3) + 1),
583 CMP_UNE = ((2U << 3) + 2),
584 CMP_NE = ((2U << 3) + 3),
585 CMP_UGE = ((2U << 3) + 4), // Reserved, not implemented.
586 CMP_OGE = ((2U << 3) + 5), // Reserved, not implemented.
587 CMP_UGT = ((2U << 3) + 6), // Reserved, not implemented.
588 CMP_OGT = ((2U << 3) + 7), // Reserved, not implemented.
589 CMP_SAT = ((3U << 3) + 0), // Reserved, not implemented.
590 CMP_SOR = ((3U << 3) + 1),
591 CMP_SUNE = ((3U << 3) + 2),
592 CMP_SNE = ((3U << 3) + 3),
593 CMP_SUGE = ((3U << 3) + 4), // Reserved, not implemented.
594 CMP_SOGE = ((3U << 3) + 5), // Reserved, not implemented.
595 CMP_SUGT = ((3U << 3) + 6), // Reserved, not implemented.
596 CMP_SOGT = ((3U << 3) + 7), // Reserved, not implemented.
597
598 SEL = ((2U << 3) + 0),
599 MOVZ_C = ((2U << 3) + 2),
600 MOVN_C = ((2U << 3) + 3),
601 SELEQZ_C = ((2U << 3) + 4), // COP1 on FPR registers.
602 MOVF = ((2U << 3) + 1), // Function field for MOVT.fmt and MOVF.fmt
603 SELNEZ_C = ((2U << 3) + 7), // COP1 on FPR registers.
Andrei Popescu31002712010-02-23 13:46:05 +0000604 // COP1 Encoding of Function Field When rs=PS.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000605 // COP1X Encoding of Function Field.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000606 MADD_D = ((4U << 3) + 1),
Andrei Popescu31002712010-02-23 13:46:05 +0000607
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000608 // PCREL Encoding of rt Field.
609 ADDIUPC = ((0U << 2) + 0),
610 LWPC = ((0U << 2) + 1),
611 AUIPC = ((3U << 3) + 6),
612 ALUIPC = ((3U << 3) + 7),
613
614 // POP66 Encoding of rs Field.
615 JIC = ((0U << 5) + 0),
616
617 // POP76 Encoding of rs Field.
618 JIALC = ((0U << 5) + 0),
619
620 NULLSF = 0U
Andrei Popescu31002712010-02-23 13:46:05 +0000621};
622
623
624// ----- Emulated conditions.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000625// On MIPS we use this enum to abstract from conditional branch instructions.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000626// The 'U' prefix is used to specify unsigned comparisons.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000627// Opposite conditions must be paired as odd/even numbers
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000628// because 'NegateCondition' function flips LSB to negate condition.
Andrei Popescu31002712010-02-23 13:46:05 +0000629enum Condition {
630 // Any value < 0 is considered no_condition.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400631 kNoCondition = -1,
632 overflow = 0,
633 no_overflow = 1,
634 Uless = 2,
635 Ugreater_equal = 3,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000636 Uless_equal = 4,
637 Ugreater = 5,
638 equal = 6,
639 not_equal = 7, // Unordered or Not Equal.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400640 negative = 8,
641 positive = 9,
642 parity_even = 10,
643 parity_odd = 11,
644 less = 12,
Andrei Popescu31002712010-02-23 13:46:05 +0000645 greater_equal = 13,
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400646 less_equal = 14,
647 greater = 15,
648 ueq = 16, // Unordered or Equal.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000649 ogl = 17, // Ordered and Not Equal.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400650 cc_always = 18,
Andrei Popescu31002712010-02-23 13:46:05 +0000651
Ben Murdoch257744e2011-11-30 15:57:28 +0000652 // Aliases.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400653 carry = Uless,
654 not_carry = Ugreater_equal,
655 zero = equal,
656 eq = equal,
657 not_zero = not_equal,
658 ne = not_equal,
659 nz = not_equal,
660 sign = negative,
661 not_sign = positive,
662 mi = negative,
663 pl = positive,
664 hi = Ugreater,
665 ls = Uless_equal,
666 ge = greater_equal,
667 lt = less,
668 gt = greater,
669 le = less_equal,
670 hs = Ugreater_equal,
671 lo = Uless,
672 al = cc_always,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000673 ult = Uless,
674 uge = Ugreater_equal,
675 ule = Uless_equal,
676 ugt = Ugreater,
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400677 cc_default = kNoCondition
Andrei Popescu31002712010-02-23 13:46:05 +0000678};
679
Steve Block44f0eee2011-05-26 01:26:41 +0100680
681// Returns the equivalent of !cc.
682// Negation of the default kNoCondition (-1) results in a non-default
683// no_condition value (-2). As long as tests for no_condition check
684// for condition < 0, this will work as expected.
685inline Condition NegateCondition(Condition cc) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000686 DCHECK(cc != cc_always);
Steve Block44f0eee2011-05-26 01:26:41 +0100687 return static_cast<Condition>(cc ^ 1);
688}
689
690
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000691inline Condition NegateFpuCondition(Condition cc) {
692 DCHECK(cc != cc_always);
693 switch (cc) {
694 case ult:
695 return ge;
696 case ugt:
697 return le;
698 case uge:
699 return lt;
700 case ule:
701 return gt;
702 case lt:
703 return uge;
704 case gt:
705 return ule;
706 case ge:
707 return ult;
708 case le:
709 return ugt;
710 case eq:
711 return ne;
712 case ne:
713 return eq;
714 case ueq:
715 return ogl;
716 case ogl:
717 return ueq;
718 default:
719 return cc;
720 }
721}
722
723
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000724// Commute a condition such that {a cond b == b cond' a}.
725inline Condition CommuteCondition(Condition cc) {
Steve Block44f0eee2011-05-26 01:26:41 +0100726 switch (cc) {
727 case Uless:
728 return Ugreater;
729 case Ugreater:
730 return Uless;
731 case Ugreater_equal:
732 return Uless_equal;
733 case Uless_equal:
734 return Ugreater_equal;
735 case less:
736 return greater;
737 case greater:
738 return less;
739 case greater_equal:
740 return less_equal;
741 case less_equal:
742 return greater_equal;
743 default:
744 return cc;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000745 }
Steve Block44f0eee2011-05-26 01:26:41 +0100746}
747
748
Andrei Popescu31002712010-02-23 13:46:05 +0000749// ----- Coprocessor conditions.
750enum FPUCondition {
Ben Murdoch589d6972011-11-30 16:04:58 +0000751 kNoFPUCondition = -1,
752
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000753 F = 0x00, // False.
754 UN = 0x01, // Unordered.
755 EQ = 0x02, // Equal.
756 UEQ = 0x03, // Unordered or Equal.
757 OLT = 0x04, // Ordered or Less Than, on Mips release < 6.
758 LT = 0x04, // Ordered or Less Than, on Mips release >= 6.
759 ULT = 0x05, // Unordered or Less Than.
760 OLE = 0x06, // Ordered or Less Than or Equal, on Mips release < 6.
761 LE = 0x06, // Ordered or Less Than or Equal, on Mips release >= 6.
762 ULE = 0x07, // Unordered or Less Than or Equal.
763
764 // Following constants are available on Mips release >= 6 only.
765 ORD = 0x11, // Ordered, on Mips release >= 6.
766 UNE = 0x12, // Not equal, on Mips release >= 6.
767 NE = 0x13, // Ordered Greater Than or Less Than. on Mips >= 6 only.
Ben Murdoch589d6972011-11-30 16:04:58 +0000768};
769
770
771// FPU rounding modes.
772enum FPURoundingMode {
773 RN = 0 << 0, // Round to Nearest.
774 RZ = 1 << 0, // Round towards zero.
775 RP = 2 << 0, // Round towards Plus Infinity.
776 RM = 3 << 0, // Round towards Minus Infinity.
777
778 // Aliases.
779 kRoundToNearest = RN,
780 kRoundToZero = RZ,
781 kRoundToPlusInf = RP,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000782 kRoundToMinusInf = RM,
783
784 mode_round = RN,
785 mode_ceil = RP,
786 mode_floor = RM,
787 mode_trunc = RZ
Ben Murdoch589d6972011-11-30 16:04:58 +0000788};
789
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100790const uint32_t kFPURoundingModeMask = 3 << 0;
Ben Murdoch589d6972011-11-30 16:04:58 +0000791
792enum CheckForInexactConversion {
793 kCheckForInexactConversion,
794 kDontCheckForInexactConversion
Andrei Popescu31002712010-02-23 13:46:05 +0000795};
796
Ben Murdoch097c5b22016-05-18 11:27:45 +0100797enum class MaxMinKind : int { kMin = 0, kMax = 1 };
Andrei Popescu31002712010-02-23 13:46:05 +0000798
Steve Block44f0eee2011-05-26 01:26:41 +0100799// -----------------------------------------------------------------------------
800// Hints.
801
802// Branch hints are not used on the MIPS. They are defined so that they can
803// appear in shared function signatures, but will be ignored in MIPS
804// implementations.
805enum Hint {
806 no_hint = 0
807};
808
809
810inline Hint NegateHint(Hint hint) {
811 return no_hint;
812}
813
814
815// -----------------------------------------------------------------------------
816// Specific instructions, constants, and masks.
817// These constants are declared in assembler-mips.cc, as they use named
818// registers and other constants.
819
820// addiu(sp, sp, 4) aka Pop() operation or part of Pop(r)
821// operations as post-increment of sp.
822extern const Instr kPopInstruction;
823// addiu(sp, sp, -4) part of Push(r) operation as pre-decrement of sp.
824extern const Instr kPushInstruction;
825// sw(r, MemOperand(sp, 0))
826extern const Instr kPushRegPattern;
Ben Murdoch257744e2011-11-30 15:57:28 +0000827// lw(r, MemOperand(sp, 0))
Steve Block44f0eee2011-05-26 01:26:41 +0100828extern const Instr kPopRegPattern;
829extern const Instr kLwRegFpOffsetPattern;
830extern const Instr kSwRegFpOffsetPattern;
831extern const Instr kLwRegFpNegOffsetPattern;
832extern const Instr kSwRegFpNegOffsetPattern;
833// A mask for the Rt register for push, pop, lw, sw instructions.
834extern const Instr kRtMask;
835extern const Instr kLwSwInstrTypeMask;
836extern const Instr kLwSwInstrArgumentMask;
837extern const Instr kLwSwOffsetMask;
838
Andrei Popescu31002712010-02-23 13:46:05 +0000839// Break 0xfffff, reserved for redirected real time call.
840const Instr rtCallRedirInstr = SPECIAL | BREAK | call_rt_redirected << 6;
841// A nop instruction. (Encoding of sll 0 0 0).
842const Instr nopInstr = 0;
843
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000844static constexpr uint64_t OpcodeToBitNumber(Opcode opcode) {
845 return 1ULL << (static_cast<uint32_t>(opcode) >> kOpcodeShift);
846}
847
848
Andrei Popescu31002712010-02-23 13:46:05 +0000849class Instruction {
850 public:
851 enum {
Steve Block44f0eee2011-05-26 01:26:41 +0100852 kInstrSize = 4,
853 kInstrSizeLog2 = 2,
Andrei Popescu31002712010-02-23 13:46:05 +0000854 // On MIPS PC cannot actually be directly accessed. We behave as if PC was
Steve Block44f0eee2011-05-26 01:26:41 +0100855 // always the value of the current instruction being executed.
Andrei Popescu31002712010-02-23 13:46:05 +0000856 kPCReadOffset = 0
857 };
858
859 // Get the raw instruction bits.
860 inline Instr InstructionBits() const {
861 return *reinterpret_cast<const Instr*>(this);
862 }
863
864 // Set the raw instruction bits to value.
865 inline void SetInstructionBits(Instr value) {
866 *reinterpret_cast<Instr*>(this) = value;
867 }
868
869 // Read one particular bit out of the instruction bits.
870 inline int Bit(int nr) const {
871 return (InstructionBits() >> nr) & 1;
872 }
873
874 // Read a bit field out of the instruction bits.
875 inline int Bits(int hi, int lo) const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000876 return (InstructionBits() >> lo) & ((2U << (hi - lo)) - 1);
Andrei Popescu31002712010-02-23 13:46:05 +0000877 }
878
879 // Instruction type.
880 enum Type {
881 kRegisterType,
882 kImmediateType,
883 kJumpType,
884 kUnsupported = -1
885 };
886
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000887 enum TypeChecks { NORMAL, EXTRA };
Andrei Popescu31002712010-02-23 13:46:05 +0000888
889
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000890 static constexpr uint64_t kOpcodeImmediateTypeMask =
891 OpcodeToBitNumber(REGIMM) | OpcodeToBitNumber(BEQ) |
892 OpcodeToBitNumber(BNE) | OpcodeToBitNumber(BLEZ) |
893 OpcodeToBitNumber(BGTZ) | OpcodeToBitNumber(ADDI) |
894 OpcodeToBitNumber(DADDI) | OpcodeToBitNumber(ADDIU) |
895 OpcodeToBitNumber(SLTI) | OpcodeToBitNumber(SLTIU) |
896 OpcodeToBitNumber(ANDI) | OpcodeToBitNumber(ORI) |
897 OpcodeToBitNumber(XORI) | OpcodeToBitNumber(LUI) |
898 OpcodeToBitNumber(BEQL) | OpcodeToBitNumber(BNEL) |
899 OpcodeToBitNumber(BLEZL) | OpcodeToBitNumber(BGTZL) |
900 OpcodeToBitNumber(POP66) | OpcodeToBitNumber(POP76) |
901 OpcodeToBitNumber(LB) | OpcodeToBitNumber(LH) | OpcodeToBitNumber(LWL) |
902 OpcodeToBitNumber(LW) | OpcodeToBitNumber(LBU) | OpcodeToBitNumber(LHU) |
903 OpcodeToBitNumber(LWR) | OpcodeToBitNumber(SB) | OpcodeToBitNumber(SH) |
904 OpcodeToBitNumber(SWL) | OpcodeToBitNumber(SW) | OpcodeToBitNumber(SWR) |
905 OpcodeToBitNumber(LWC1) | OpcodeToBitNumber(LDC1) |
906 OpcodeToBitNumber(SWC1) | OpcodeToBitNumber(SDC1) |
907 OpcodeToBitNumber(PCREL) | OpcodeToBitNumber(BC) |
908 OpcodeToBitNumber(BALC);
909
910#define FunctionFieldToBitNumber(function) (1ULL << function)
911
912 static const uint64_t kFunctionFieldRegisterTypeMask =
913 FunctionFieldToBitNumber(JR) | FunctionFieldToBitNumber(JALR) |
914 FunctionFieldToBitNumber(BREAK) | FunctionFieldToBitNumber(SLL) |
915 FunctionFieldToBitNumber(SRL) | FunctionFieldToBitNumber(SRA) |
916 FunctionFieldToBitNumber(SLLV) | FunctionFieldToBitNumber(SRLV) |
917 FunctionFieldToBitNumber(SRAV) | FunctionFieldToBitNumber(LSA) |
918 FunctionFieldToBitNumber(MFHI) | FunctionFieldToBitNumber(MFLO) |
919 FunctionFieldToBitNumber(MULT) | FunctionFieldToBitNumber(MULTU) |
920 FunctionFieldToBitNumber(DIV) | FunctionFieldToBitNumber(DIVU) |
921 FunctionFieldToBitNumber(ADD) | FunctionFieldToBitNumber(ADDU) |
922 FunctionFieldToBitNumber(SUB) | FunctionFieldToBitNumber(SUBU) |
923 FunctionFieldToBitNumber(AND) | FunctionFieldToBitNumber(OR) |
924 FunctionFieldToBitNumber(XOR) | FunctionFieldToBitNumber(NOR) |
925 FunctionFieldToBitNumber(SLT) | FunctionFieldToBitNumber(SLTU) |
926 FunctionFieldToBitNumber(TGE) | FunctionFieldToBitNumber(TGEU) |
927 FunctionFieldToBitNumber(TLT) | FunctionFieldToBitNumber(TLTU) |
928 FunctionFieldToBitNumber(TEQ) | FunctionFieldToBitNumber(TNE) |
929 FunctionFieldToBitNumber(MOVZ) | FunctionFieldToBitNumber(MOVN) |
930 FunctionFieldToBitNumber(MOVCI) | FunctionFieldToBitNumber(SELEQZ_S) |
931 FunctionFieldToBitNumber(SELNEZ_S);
932
933
934 // Get the encoding type of the instruction.
935 inline Type InstructionType(TypeChecks checks = NORMAL) const;
936
Andrei Popescu31002712010-02-23 13:46:05 +0000937 // Accessors for the different named fields used in the MIPS encoding.
Steve Block44f0eee2011-05-26 01:26:41 +0100938 inline Opcode OpcodeValue() const {
Andrei Popescu31002712010-02-23 13:46:05 +0000939 return static_cast<Opcode>(
940 Bits(kOpcodeShift + kOpcodeBits - 1, kOpcodeShift));
941 }
942
Steve Block44f0eee2011-05-26 01:26:41 +0100943 inline int RsValue() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000944 DCHECK(InstructionType() == kRegisterType ||
Andrei Popescu31002712010-02-23 13:46:05 +0000945 InstructionType() == kImmediateType);
946 return Bits(kRsShift + kRsBits - 1, kRsShift);
947 }
948
Steve Block44f0eee2011-05-26 01:26:41 +0100949 inline int RtValue() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000950 DCHECK(InstructionType() == kRegisterType ||
Andrei Popescu31002712010-02-23 13:46:05 +0000951 InstructionType() == kImmediateType);
952 return Bits(kRtShift + kRtBits - 1, kRtShift);
953 }
954
Steve Block44f0eee2011-05-26 01:26:41 +0100955 inline int RdValue() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000956 DCHECK(InstructionType() == kRegisterType);
Andrei Popescu31002712010-02-23 13:46:05 +0000957 return Bits(kRdShift + kRdBits - 1, kRdShift);
958 }
959
Steve Block44f0eee2011-05-26 01:26:41 +0100960 inline int SaValue() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000961 DCHECK(InstructionType() == kRegisterType);
Andrei Popescu31002712010-02-23 13:46:05 +0000962 return Bits(kSaShift + kSaBits - 1, kSaShift);
963 }
964
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000965 inline int LsaSaValue() const {
966 DCHECK(InstructionType() == kRegisterType);
967 return Bits(kSaShift + kLsaSaBits - 1, kSaShift);
968 }
969
Steve Block44f0eee2011-05-26 01:26:41 +0100970 inline int FunctionValue() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000971 DCHECK(InstructionType() == kRegisterType ||
Andrei Popescu31002712010-02-23 13:46:05 +0000972 InstructionType() == kImmediateType);
973 return Bits(kFunctionShift + kFunctionBits - 1, kFunctionShift);
974 }
975
Steve Block44f0eee2011-05-26 01:26:41 +0100976 inline int FdValue() const {
977 return Bits(kFdShift + kFdBits - 1, kFdShift);
Andrei Popescu31002712010-02-23 13:46:05 +0000978 }
979
Steve Block44f0eee2011-05-26 01:26:41 +0100980 inline int FsValue() const {
981 return Bits(kFsShift + kFsBits - 1, kFsShift);
982 }
983
984 inline int FtValue() const {
985 return Bits(kFtShift + kFtBits - 1, kFtShift);
986 }
987
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000988 inline int FrValue() const {
989 return Bits(kFrShift + kFrBits -1, kFrShift);
990 }
991
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000992 inline int Bp2Value() const {
993 DCHECK(InstructionType() == kRegisterType);
994 return Bits(kBp2Shift + kBp2Bits - 1, kBp2Shift);
995 }
996
Steve Block44f0eee2011-05-26 01:26:41 +0100997 // Float Compare condition code instruction bits.
998 inline int FCccValue() const {
999 return Bits(kFCccShift + kFCccBits - 1, kFCccShift);
1000 }
1001
1002 // Float Branch condition code instruction bits.
1003 inline int FBccValue() const {
1004 return Bits(kFBccShift + kFBccBits - 1, kFBccShift);
1005 }
1006
1007 // Float Branch true/false instruction bit.
1008 inline int FBtrueValue() const {
1009 return Bits(kFBtrueShift + kFBtrueBits - 1, kFBtrueShift);
Andrei Popescu31002712010-02-23 13:46:05 +00001010 }
1011
1012 // Return the fields at their original place in the instruction encoding.
1013 inline Opcode OpcodeFieldRaw() const {
1014 return static_cast<Opcode>(InstructionBits() & kOpcodeMask);
1015 }
1016
1017 inline int RsFieldRaw() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001018 DCHECK(InstructionType() == kRegisterType ||
Andrei Popescu31002712010-02-23 13:46:05 +00001019 InstructionType() == kImmediateType);
1020 return InstructionBits() & kRsFieldMask;
1021 }
1022
Steve Block44f0eee2011-05-26 01:26:41 +01001023 // Same as above function, but safe to call within InstructionType().
1024 inline int RsFieldRawNoAssert() const {
1025 return InstructionBits() & kRsFieldMask;
1026 }
1027
Andrei Popescu31002712010-02-23 13:46:05 +00001028 inline int RtFieldRaw() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001029 DCHECK(InstructionType() == kRegisterType ||
Andrei Popescu31002712010-02-23 13:46:05 +00001030 InstructionType() == kImmediateType);
1031 return InstructionBits() & kRtFieldMask;
1032 }
1033
1034 inline int RdFieldRaw() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001035 DCHECK(InstructionType() == kRegisterType);
Andrei Popescu31002712010-02-23 13:46:05 +00001036 return InstructionBits() & kRdFieldMask;
1037 }
1038
1039 inline int SaFieldRaw() const {
Andrei Popescu31002712010-02-23 13:46:05 +00001040 return InstructionBits() & kSaFieldMask;
1041 }
1042
1043 inline int FunctionFieldRaw() const {
1044 return InstructionBits() & kFunctionFieldMask;
1045 }
1046
1047 // Get the secondary field according to the opcode.
Steve Block44f0eee2011-05-26 01:26:41 +01001048 inline int SecondaryValue() const {
Andrei Popescu31002712010-02-23 13:46:05 +00001049 Opcode op = OpcodeFieldRaw();
1050 switch (op) {
1051 case SPECIAL:
1052 case SPECIAL2:
Steve Block44f0eee2011-05-26 01:26:41 +01001053 return FunctionValue();
Andrei Popescu31002712010-02-23 13:46:05 +00001054 case COP1:
Steve Block44f0eee2011-05-26 01:26:41 +01001055 return RsValue();
Andrei Popescu31002712010-02-23 13:46:05 +00001056 case REGIMM:
Steve Block44f0eee2011-05-26 01:26:41 +01001057 return RtValue();
Andrei Popescu31002712010-02-23 13:46:05 +00001058 default:
1059 return NULLSF;
1060 }
1061 }
1062
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001063 inline int32_t ImmValue(int bits) const {
1064 DCHECK(InstructionType() == kImmediateType);
1065 return Bits(bits - 1, 0);
1066 }
1067
Steve Block44f0eee2011-05-26 01:26:41 +01001068 inline int32_t Imm16Value() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001069 DCHECK(InstructionType() == kImmediateType);
Andrei Popescu31002712010-02-23 13:46:05 +00001070 return Bits(kImm16Shift + kImm16Bits - 1, kImm16Shift);
1071 }
1072
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001073 inline int32_t Imm18Value() const {
1074 DCHECK(InstructionType() == kImmediateType);
1075 return Bits(kImm18Shift + kImm18Bits - 1, kImm18Shift);
1076 }
1077
1078 inline int32_t Imm19Value() const {
1079 DCHECK(InstructionType() == kImmediateType);
1080 return Bits(kImm19Shift + kImm19Bits - 1, kImm19Shift);
1081 }
1082
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001083 inline int32_t Imm21Value() const {
1084 DCHECK(InstructionType() == kImmediateType);
1085 return Bits(kImm21Shift + kImm21Bits - 1, kImm21Shift);
1086 }
1087
Steve Block44f0eee2011-05-26 01:26:41 +01001088 inline int32_t Imm26Value() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001089 DCHECK((InstructionType() == kJumpType) ||
1090 (InstructionType() == kImmediateType));
Ben Murdoch589d6972011-11-30 16:04:58 +00001091 return Bits(kImm26Shift + kImm26Bits - 1, kImm26Shift);
Andrei Popescu31002712010-02-23 13:46:05 +00001092 }
1093
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001094 static bool IsForbiddenAfterBranchInstr(Instr instr);
1095
1096 // Say if the instruction should not be used in a branch delay slot or
1097 // immediately after a compact branch.
1098 inline bool IsForbiddenAfterBranch() const {
1099 return IsForbiddenAfterBranchInstr(InstructionBits());
1100 }
1101
1102 inline bool IsForbiddenInBranchDelay() const {
1103 return IsForbiddenAfterBranch();
1104 }
1105
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001106 // Say if the instruction 'links'. e.g. jal, bal.
Steve Block44f0eee2011-05-26 01:26:41 +01001107 bool IsLinkingInstruction() const;
Andrei Popescu31002712010-02-23 13:46:05 +00001108 // Say if the instruction is a break or a trap.
Steve Block44f0eee2011-05-26 01:26:41 +01001109 bool IsTrap() const;
Andrei Popescu31002712010-02-23 13:46:05 +00001110
1111 // Instructions are read of out a code stream. The only way to get a
1112 // reference to an instruction is to convert a pointer. There is no way
1113 // to allocate or create instances of class Instruction.
1114 // Use the At(pc) function to create references to Instruction.
Ben Murdoch257744e2011-11-30 15:57:28 +00001115 static Instruction* At(byte* pc) {
Andrei Popescu31002712010-02-23 13:46:05 +00001116 return reinterpret_cast<Instruction*>(pc);
1117 }
1118
1119 private:
1120 // We need to prevent the creation of instances of class Instruction.
1121 DISALLOW_IMPLICIT_CONSTRUCTORS(Instruction);
1122};
1123
1124
1125// -----------------------------------------------------------------------------
1126// MIPS assembly various constants.
1127
Steve Block44f0eee2011-05-26 01:26:41 +01001128// C/C++ argument slots size.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001129const int kCArgSlotCount = 4;
1130const int kCArgsSlotsSize = kCArgSlotCount * Instruction::kInstrSize;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001131const int kInvalidStackOffset = -1;
Steve Block44f0eee2011-05-26 01:26:41 +01001132// JS argument slots size.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001133const int kJSArgsSlotsSize = 0 * Instruction::kInstrSize;
Steve Block44f0eee2011-05-26 01:26:41 +01001134// Assembly builtins argument slots size.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001135const int kBArgsSlotsSize = 0 * Instruction::kInstrSize;
Andrei Popescu31002712010-02-23 13:46:05 +00001136
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001137const int kBranchReturnOffset = 2 * Instruction::kInstrSize;
Andrei Popescu31002712010-02-23 13:46:05 +00001138
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001139
1140Instruction::Type Instruction::InstructionType(TypeChecks checks) const {
1141 if (checks == EXTRA) {
1142 if (OpcodeToBitNumber(OpcodeFieldRaw()) & kOpcodeImmediateTypeMask) {
1143 return kImmediateType;
1144 }
1145 }
1146 switch (OpcodeFieldRaw()) {
1147 case SPECIAL:
1148 if (checks == EXTRA) {
1149 if (FunctionFieldToBitNumber(FunctionFieldRaw()) &
1150 kFunctionFieldRegisterTypeMask) {
1151 return kRegisterType;
1152 } else {
1153 return kUnsupported;
1154 }
1155 } else {
1156 return kRegisterType;
1157 }
1158 break;
1159 case SPECIAL2:
1160 switch (FunctionFieldRaw()) {
1161 case MUL:
1162 case CLZ:
1163 return kRegisterType;
1164 default:
1165 return kUnsupported;
1166 }
1167 break;
1168 case SPECIAL3:
1169 switch (FunctionFieldRaw()) {
1170 case INS:
1171 case EXT:
1172 return kRegisterType;
1173 case BSHFL: {
1174 int sa = SaFieldRaw() >> kSaShift;
1175 switch (sa) {
1176 case BITSWAP:
1177 return kRegisterType;
1178 case WSBH:
1179 case SEB:
1180 case SEH:
1181 return kUnsupported;
1182 }
1183 sa >>= kBp2Bits;
1184 switch (sa) {
1185 case ALIGN:
1186 return kRegisterType;
1187 default:
1188 return kUnsupported;
1189 }
1190 }
1191 default:
1192 return kUnsupported;
1193 }
1194 break;
1195 case COP1: // Coprocessor instructions.
1196 switch (RsFieldRawNoAssert()) {
1197 case BC1: // Branch on coprocessor condition.
1198 case BC1EQZ:
1199 case BC1NEZ:
1200 return kImmediateType;
1201 default:
1202 return kRegisterType;
1203 }
1204 break;
1205 case COP1X:
1206 return kRegisterType;
1207
1208 // 26 bits immediate type instructions. e.g.: j imm26.
1209 case J:
1210 case JAL:
1211 return kJumpType;
1212
1213 default:
1214 if (checks == NORMAL) {
1215 return kImmediateType;
1216 } else {
1217 return kUnsupported;
1218 }
1219 }
1220}
1221
1222#undef OpcodeToBitNumber
1223#undef FunctionFieldToBitNumber
1224} // namespace internal
1225} // namespace v8
Andrei Popescu31002712010-02-23 13:46:05 +00001226
1227#endif // #ifndef V8_MIPS_CONSTANTS_H_