blob: 8327501b6f8d7e4ecb6d28e66e939b9c62b34889 [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)
67 static const FpuMode kFpuMode = kFPXX;
68#else
69 static const FpuMode kFpuMode = kFP32;
70#endif
Steve Block44f0eee2011-05-26 01:26:41 +010071
Ben Murdoch257744e2011-11-30 15:57:28 +000072#if(defined(__mips_hard_float) && __mips_hard_float != 0)
73// Use floating-point coprocessor instructions. This flag is raised when
74// -mhard-float is passed to the compiler.
Ben Murdoch3ef787d2012-04-12 10:51:47 +010075const bool IsMipsSoftFloatABI = false;
Ben Murdoch257744e2011-11-30 15:57:28 +000076#elif(defined(__mips_soft_float) && __mips_soft_float != 0)
Ben Murdochb8a8cc12014-11-26 15:28:44 +000077// This flag is raised when -msoft-float is passed to the compiler.
78// Although FPU is a base requirement for v8, soft-float ABI is used
79// on soft-float systems with FPU kernel emulation.
Ben Murdoch3ef787d2012-04-12 10:51:47 +010080const bool IsMipsSoftFloatABI = true;
Ben Murdoch257744e2011-11-30 15:57:28 +000081#else
Ben Murdoch3ef787d2012-04-12 10:51:47 +010082const bool IsMipsSoftFloatABI = true;
Ben Murdoch257744e2011-11-30 15:57:28 +000083#endif
84
Ben Murdochb8a8cc12014-11-26 15:28:44 +000085#if defined(V8_TARGET_LITTLE_ENDIAN)
86const uint32_t kHoleNanUpper32Offset = 4;
87const uint32_t kHoleNanLower32Offset = 0;
88#elif defined(V8_TARGET_BIG_ENDIAN)
89const uint32_t kHoleNanUpper32Offset = 0;
90const uint32_t kHoleNanLower32Offset = 4;
91#else
92#error Unknown endianness
93#endif
94
95#ifndef FPU_MODE_FPXX
96#define IsFp64Mode() \
97 (kFpuMode == kFP64)
98#else
99#define IsFp64Mode() \
100 (CpuFeatures::IsSupported(FP64FPU))
101#endif
102
103#ifndef _MIPS_ARCH_MIPS32RX
104#define IsMipsArchVariant(check) \
105 (kArchVariant == check)
106#else
107#define IsMipsArchVariant(check) \
Paul 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
393 POP30 = DADDI, // bnezalc, bvnc, 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
797
Steve Block44f0eee2011-05-26 01:26:41 +0100798// -----------------------------------------------------------------------------
799// Hints.
800
801// Branch hints are not used on the MIPS. They are defined so that they can
802// appear in shared function signatures, but will be ignored in MIPS
803// implementations.
804enum Hint {
805 no_hint = 0
806};
807
808
809inline Hint NegateHint(Hint hint) {
810 return no_hint;
811}
812
813
814// -----------------------------------------------------------------------------
815// Specific instructions, constants, and masks.
816// These constants are declared in assembler-mips.cc, as they use named
817// registers and other constants.
818
819// addiu(sp, sp, 4) aka Pop() operation or part of Pop(r)
820// operations as post-increment of sp.
821extern const Instr kPopInstruction;
822// addiu(sp, sp, -4) part of Push(r) operation as pre-decrement of sp.
823extern const Instr kPushInstruction;
824// sw(r, MemOperand(sp, 0))
825extern const Instr kPushRegPattern;
Ben Murdoch257744e2011-11-30 15:57:28 +0000826// lw(r, MemOperand(sp, 0))
Steve Block44f0eee2011-05-26 01:26:41 +0100827extern const Instr kPopRegPattern;
828extern const Instr kLwRegFpOffsetPattern;
829extern const Instr kSwRegFpOffsetPattern;
830extern const Instr kLwRegFpNegOffsetPattern;
831extern const Instr kSwRegFpNegOffsetPattern;
832// A mask for the Rt register for push, pop, lw, sw instructions.
833extern const Instr kRtMask;
834extern const Instr kLwSwInstrTypeMask;
835extern const Instr kLwSwInstrArgumentMask;
836extern const Instr kLwSwOffsetMask;
837
Andrei Popescu31002712010-02-23 13:46:05 +0000838// Break 0xfffff, reserved for redirected real time call.
839const Instr rtCallRedirInstr = SPECIAL | BREAK | call_rt_redirected << 6;
840// A nop instruction. (Encoding of sll 0 0 0).
841const Instr nopInstr = 0;
842
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000843static constexpr uint64_t OpcodeToBitNumber(Opcode opcode) {
844 return 1ULL << (static_cast<uint32_t>(opcode) >> kOpcodeShift);
845}
846
847
Andrei Popescu31002712010-02-23 13:46:05 +0000848class Instruction {
849 public:
850 enum {
Steve Block44f0eee2011-05-26 01:26:41 +0100851 kInstrSize = 4,
852 kInstrSizeLog2 = 2,
Andrei Popescu31002712010-02-23 13:46:05 +0000853 // On MIPS PC cannot actually be directly accessed. We behave as if PC was
Steve Block44f0eee2011-05-26 01:26:41 +0100854 // always the value of the current instruction being executed.
Andrei Popescu31002712010-02-23 13:46:05 +0000855 kPCReadOffset = 0
856 };
857
858 // Get the raw instruction bits.
859 inline Instr InstructionBits() const {
860 return *reinterpret_cast<const Instr*>(this);
861 }
862
863 // Set the raw instruction bits to value.
864 inline void SetInstructionBits(Instr value) {
865 *reinterpret_cast<Instr*>(this) = value;
866 }
867
868 // Read one particular bit out of the instruction bits.
869 inline int Bit(int nr) const {
870 return (InstructionBits() >> nr) & 1;
871 }
872
873 // Read a bit field out of the instruction bits.
874 inline int Bits(int hi, int lo) const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000875 return (InstructionBits() >> lo) & ((2U << (hi - lo)) - 1);
Andrei Popescu31002712010-02-23 13:46:05 +0000876 }
877
878 // Instruction type.
879 enum Type {
880 kRegisterType,
881 kImmediateType,
882 kJumpType,
883 kUnsupported = -1
884 };
885
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000886 enum TypeChecks { NORMAL, EXTRA };
Andrei Popescu31002712010-02-23 13:46:05 +0000887
888
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000889 static constexpr uint64_t kOpcodeImmediateTypeMask =
890 OpcodeToBitNumber(REGIMM) | OpcodeToBitNumber(BEQ) |
891 OpcodeToBitNumber(BNE) | OpcodeToBitNumber(BLEZ) |
892 OpcodeToBitNumber(BGTZ) | OpcodeToBitNumber(ADDI) |
893 OpcodeToBitNumber(DADDI) | OpcodeToBitNumber(ADDIU) |
894 OpcodeToBitNumber(SLTI) | OpcodeToBitNumber(SLTIU) |
895 OpcodeToBitNumber(ANDI) | OpcodeToBitNumber(ORI) |
896 OpcodeToBitNumber(XORI) | OpcodeToBitNumber(LUI) |
897 OpcodeToBitNumber(BEQL) | OpcodeToBitNumber(BNEL) |
898 OpcodeToBitNumber(BLEZL) | OpcodeToBitNumber(BGTZL) |
899 OpcodeToBitNumber(POP66) | OpcodeToBitNumber(POP76) |
900 OpcodeToBitNumber(LB) | OpcodeToBitNumber(LH) | OpcodeToBitNumber(LWL) |
901 OpcodeToBitNumber(LW) | OpcodeToBitNumber(LBU) | OpcodeToBitNumber(LHU) |
902 OpcodeToBitNumber(LWR) | OpcodeToBitNumber(SB) | OpcodeToBitNumber(SH) |
903 OpcodeToBitNumber(SWL) | OpcodeToBitNumber(SW) | OpcodeToBitNumber(SWR) |
904 OpcodeToBitNumber(LWC1) | OpcodeToBitNumber(LDC1) |
905 OpcodeToBitNumber(SWC1) | OpcodeToBitNumber(SDC1) |
906 OpcodeToBitNumber(PCREL) | OpcodeToBitNumber(BC) |
907 OpcodeToBitNumber(BALC);
908
909#define FunctionFieldToBitNumber(function) (1ULL << function)
910
911 static const uint64_t kFunctionFieldRegisterTypeMask =
912 FunctionFieldToBitNumber(JR) | FunctionFieldToBitNumber(JALR) |
913 FunctionFieldToBitNumber(BREAK) | FunctionFieldToBitNumber(SLL) |
914 FunctionFieldToBitNumber(SRL) | FunctionFieldToBitNumber(SRA) |
915 FunctionFieldToBitNumber(SLLV) | FunctionFieldToBitNumber(SRLV) |
916 FunctionFieldToBitNumber(SRAV) | FunctionFieldToBitNumber(LSA) |
917 FunctionFieldToBitNumber(MFHI) | FunctionFieldToBitNumber(MFLO) |
918 FunctionFieldToBitNumber(MULT) | FunctionFieldToBitNumber(MULTU) |
919 FunctionFieldToBitNumber(DIV) | FunctionFieldToBitNumber(DIVU) |
920 FunctionFieldToBitNumber(ADD) | FunctionFieldToBitNumber(ADDU) |
921 FunctionFieldToBitNumber(SUB) | FunctionFieldToBitNumber(SUBU) |
922 FunctionFieldToBitNumber(AND) | FunctionFieldToBitNumber(OR) |
923 FunctionFieldToBitNumber(XOR) | FunctionFieldToBitNumber(NOR) |
924 FunctionFieldToBitNumber(SLT) | FunctionFieldToBitNumber(SLTU) |
925 FunctionFieldToBitNumber(TGE) | FunctionFieldToBitNumber(TGEU) |
926 FunctionFieldToBitNumber(TLT) | FunctionFieldToBitNumber(TLTU) |
927 FunctionFieldToBitNumber(TEQ) | FunctionFieldToBitNumber(TNE) |
928 FunctionFieldToBitNumber(MOVZ) | FunctionFieldToBitNumber(MOVN) |
929 FunctionFieldToBitNumber(MOVCI) | FunctionFieldToBitNumber(SELEQZ_S) |
930 FunctionFieldToBitNumber(SELNEZ_S);
931
932
933 // Get the encoding type of the instruction.
934 inline Type InstructionType(TypeChecks checks = NORMAL) const;
935
Andrei Popescu31002712010-02-23 13:46:05 +0000936 // Accessors for the different named fields used in the MIPS encoding.
Steve Block44f0eee2011-05-26 01:26:41 +0100937 inline Opcode OpcodeValue() const {
Andrei Popescu31002712010-02-23 13:46:05 +0000938 return static_cast<Opcode>(
939 Bits(kOpcodeShift + kOpcodeBits - 1, kOpcodeShift));
940 }
941
Steve Block44f0eee2011-05-26 01:26:41 +0100942 inline int RsValue() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000943 DCHECK(InstructionType() == kRegisterType ||
Andrei Popescu31002712010-02-23 13:46:05 +0000944 InstructionType() == kImmediateType);
945 return Bits(kRsShift + kRsBits - 1, kRsShift);
946 }
947
Steve Block44f0eee2011-05-26 01:26:41 +0100948 inline int RtValue() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000949 DCHECK(InstructionType() == kRegisterType ||
Andrei Popescu31002712010-02-23 13:46:05 +0000950 InstructionType() == kImmediateType);
951 return Bits(kRtShift + kRtBits - 1, kRtShift);
952 }
953
Steve Block44f0eee2011-05-26 01:26:41 +0100954 inline int RdValue() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000955 DCHECK(InstructionType() == kRegisterType);
Andrei Popescu31002712010-02-23 13:46:05 +0000956 return Bits(kRdShift + kRdBits - 1, kRdShift);
957 }
958
Steve Block44f0eee2011-05-26 01:26:41 +0100959 inline int SaValue() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000960 DCHECK(InstructionType() == kRegisterType);
Andrei Popescu31002712010-02-23 13:46:05 +0000961 return Bits(kSaShift + kSaBits - 1, kSaShift);
962 }
963
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000964 inline int LsaSaValue() const {
965 DCHECK(InstructionType() == kRegisterType);
966 return Bits(kSaShift + kLsaSaBits - 1, kSaShift);
967 }
968
Steve Block44f0eee2011-05-26 01:26:41 +0100969 inline int FunctionValue() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000970 DCHECK(InstructionType() == kRegisterType ||
Andrei Popescu31002712010-02-23 13:46:05 +0000971 InstructionType() == kImmediateType);
972 return Bits(kFunctionShift + kFunctionBits - 1, kFunctionShift);
973 }
974
Steve Block44f0eee2011-05-26 01:26:41 +0100975 inline int FdValue() const {
976 return Bits(kFdShift + kFdBits - 1, kFdShift);
Andrei Popescu31002712010-02-23 13:46:05 +0000977 }
978
Steve Block44f0eee2011-05-26 01:26:41 +0100979 inline int FsValue() const {
980 return Bits(kFsShift + kFsBits - 1, kFsShift);
981 }
982
983 inline int FtValue() const {
984 return Bits(kFtShift + kFtBits - 1, kFtShift);
985 }
986
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000987 inline int FrValue() const {
988 return Bits(kFrShift + kFrBits -1, kFrShift);
989 }
990
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000991 inline int Bp2Value() const {
992 DCHECK(InstructionType() == kRegisterType);
993 return Bits(kBp2Shift + kBp2Bits - 1, kBp2Shift);
994 }
995
Steve Block44f0eee2011-05-26 01:26:41 +0100996 // Float Compare condition code instruction bits.
997 inline int FCccValue() const {
998 return Bits(kFCccShift + kFCccBits - 1, kFCccShift);
999 }
1000
1001 // Float Branch condition code instruction bits.
1002 inline int FBccValue() const {
1003 return Bits(kFBccShift + kFBccBits - 1, kFBccShift);
1004 }
1005
1006 // Float Branch true/false instruction bit.
1007 inline int FBtrueValue() const {
1008 return Bits(kFBtrueShift + kFBtrueBits - 1, kFBtrueShift);
Andrei Popescu31002712010-02-23 13:46:05 +00001009 }
1010
1011 // Return the fields at their original place in the instruction encoding.
1012 inline Opcode OpcodeFieldRaw() const {
1013 return static_cast<Opcode>(InstructionBits() & kOpcodeMask);
1014 }
1015
1016 inline int RsFieldRaw() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001017 DCHECK(InstructionType() == kRegisterType ||
Andrei Popescu31002712010-02-23 13:46:05 +00001018 InstructionType() == kImmediateType);
1019 return InstructionBits() & kRsFieldMask;
1020 }
1021
Steve Block44f0eee2011-05-26 01:26:41 +01001022 // Same as above function, but safe to call within InstructionType().
1023 inline int RsFieldRawNoAssert() const {
1024 return InstructionBits() & kRsFieldMask;
1025 }
1026
Andrei Popescu31002712010-02-23 13:46:05 +00001027 inline int RtFieldRaw() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001028 DCHECK(InstructionType() == kRegisterType ||
Andrei Popescu31002712010-02-23 13:46:05 +00001029 InstructionType() == kImmediateType);
1030 return InstructionBits() & kRtFieldMask;
1031 }
1032
1033 inline int RdFieldRaw() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001034 DCHECK(InstructionType() == kRegisterType);
Andrei Popescu31002712010-02-23 13:46:05 +00001035 return InstructionBits() & kRdFieldMask;
1036 }
1037
1038 inline int SaFieldRaw() const {
Andrei Popescu31002712010-02-23 13:46:05 +00001039 return InstructionBits() & kSaFieldMask;
1040 }
1041
1042 inline int FunctionFieldRaw() const {
1043 return InstructionBits() & kFunctionFieldMask;
1044 }
1045
1046 // Get the secondary field according to the opcode.
Steve Block44f0eee2011-05-26 01:26:41 +01001047 inline int SecondaryValue() const {
Andrei Popescu31002712010-02-23 13:46:05 +00001048 Opcode op = OpcodeFieldRaw();
1049 switch (op) {
1050 case SPECIAL:
1051 case SPECIAL2:
Steve Block44f0eee2011-05-26 01:26:41 +01001052 return FunctionValue();
Andrei Popescu31002712010-02-23 13:46:05 +00001053 case COP1:
Steve Block44f0eee2011-05-26 01:26:41 +01001054 return RsValue();
Andrei Popescu31002712010-02-23 13:46:05 +00001055 case REGIMM:
Steve Block44f0eee2011-05-26 01:26:41 +01001056 return RtValue();
Andrei Popescu31002712010-02-23 13:46:05 +00001057 default:
1058 return NULLSF;
1059 }
1060 }
1061
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001062 inline int32_t ImmValue(int bits) const {
1063 DCHECK(InstructionType() == kImmediateType);
1064 return Bits(bits - 1, 0);
1065 }
1066
Steve Block44f0eee2011-05-26 01:26:41 +01001067 inline int32_t Imm16Value() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001068 DCHECK(InstructionType() == kImmediateType);
Andrei Popescu31002712010-02-23 13:46:05 +00001069 return Bits(kImm16Shift + kImm16Bits - 1, kImm16Shift);
1070 }
1071
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001072 inline int32_t Imm18Value() const {
1073 DCHECK(InstructionType() == kImmediateType);
1074 return Bits(kImm18Shift + kImm18Bits - 1, kImm18Shift);
1075 }
1076
1077 inline int32_t Imm19Value() const {
1078 DCHECK(InstructionType() == kImmediateType);
1079 return Bits(kImm19Shift + kImm19Bits - 1, kImm19Shift);
1080 }
1081
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001082 inline int32_t Imm21Value() const {
1083 DCHECK(InstructionType() == kImmediateType);
1084 return Bits(kImm21Shift + kImm21Bits - 1, kImm21Shift);
1085 }
1086
Steve Block44f0eee2011-05-26 01:26:41 +01001087 inline int32_t Imm26Value() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001088 DCHECK((InstructionType() == kJumpType) ||
1089 (InstructionType() == kImmediateType));
Ben Murdoch589d6972011-11-30 16:04:58 +00001090 return Bits(kImm26Shift + kImm26Bits - 1, kImm26Shift);
Andrei Popescu31002712010-02-23 13:46:05 +00001091 }
1092
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001093 static bool IsForbiddenAfterBranchInstr(Instr instr);
1094
1095 // Say if the instruction should not be used in a branch delay slot or
1096 // immediately after a compact branch.
1097 inline bool IsForbiddenAfterBranch() const {
1098 return IsForbiddenAfterBranchInstr(InstructionBits());
1099 }
1100
1101 inline bool IsForbiddenInBranchDelay() const {
1102 return IsForbiddenAfterBranch();
1103 }
1104
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001105 // Say if the instruction 'links'. e.g. jal, bal.
Steve Block44f0eee2011-05-26 01:26:41 +01001106 bool IsLinkingInstruction() const;
Andrei Popescu31002712010-02-23 13:46:05 +00001107 // Say if the instruction is a break or a trap.
Steve Block44f0eee2011-05-26 01:26:41 +01001108 bool IsTrap() const;
Andrei Popescu31002712010-02-23 13:46:05 +00001109
1110 // Instructions are read of out a code stream. The only way to get a
1111 // reference to an instruction is to convert a pointer. There is no way
1112 // to allocate or create instances of class Instruction.
1113 // Use the At(pc) function to create references to Instruction.
Ben Murdoch257744e2011-11-30 15:57:28 +00001114 static Instruction* At(byte* pc) {
Andrei Popescu31002712010-02-23 13:46:05 +00001115 return reinterpret_cast<Instruction*>(pc);
1116 }
1117
1118 private:
1119 // We need to prevent the creation of instances of class Instruction.
1120 DISALLOW_IMPLICIT_CONSTRUCTORS(Instruction);
1121};
1122
1123
1124// -----------------------------------------------------------------------------
1125// MIPS assembly various constants.
1126
Steve Block44f0eee2011-05-26 01:26:41 +01001127// C/C++ argument slots size.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001128const int kCArgSlotCount = 4;
1129const int kCArgsSlotsSize = kCArgSlotCount * Instruction::kInstrSize;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001130const int kInvalidStackOffset = -1;
Steve Block44f0eee2011-05-26 01:26:41 +01001131// JS argument slots size.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001132const int kJSArgsSlotsSize = 0 * Instruction::kInstrSize;
Steve Block44f0eee2011-05-26 01:26:41 +01001133// Assembly builtins argument slots size.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001134const int kBArgsSlotsSize = 0 * Instruction::kInstrSize;
Andrei Popescu31002712010-02-23 13:46:05 +00001135
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001136const int kBranchReturnOffset = 2 * Instruction::kInstrSize;
Andrei Popescu31002712010-02-23 13:46:05 +00001137
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001138
1139Instruction::Type Instruction::InstructionType(TypeChecks checks) const {
1140 if (checks == EXTRA) {
1141 if (OpcodeToBitNumber(OpcodeFieldRaw()) & kOpcodeImmediateTypeMask) {
1142 return kImmediateType;
1143 }
1144 }
1145 switch (OpcodeFieldRaw()) {
1146 case SPECIAL:
1147 if (checks == EXTRA) {
1148 if (FunctionFieldToBitNumber(FunctionFieldRaw()) &
1149 kFunctionFieldRegisterTypeMask) {
1150 return kRegisterType;
1151 } else {
1152 return kUnsupported;
1153 }
1154 } else {
1155 return kRegisterType;
1156 }
1157 break;
1158 case SPECIAL2:
1159 switch (FunctionFieldRaw()) {
1160 case MUL:
1161 case CLZ:
1162 return kRegisterType;
1163 default:
1164 return kUnsupported;
1165 }
1166 break;
1167 case SPECIAL3:
1168 switch (FunctionFieldRaw()) {
1169 case INS:
1170 case EXT:
1171 return kRegisterType;
1172 case BSHFL: {
1173 int sa = SaFieldRaw() >> kSaShift;
1174 switch (sa) {
1175 case BITSWAP:
1176 return kRegisterType;
1177 case WSBH:
1178 case SEB:
1179 case SEH:
1180 return kUnsupported;
1181 }
1182 sa >>= kBp2Bits;
1183 switch (sa) {
1184 case ALIGN:
1185 return kRegisterType;
1186 default:
1187 return kUnsupported;
1188 }
1189 }
1190 default:
1191 return kUnsupported;
1192 }
1193 break;
1194 case COP1: // Coprocessor instructions.
1195 switch (RsFieldRawNoAssert()) {
1196 case BC1: // Branch on coprocessor condition.
1197 case BC1EQZ:
1198 case BC1NEZ:
1199 return kImmediateType;
1200 default:
1201 return kRegisterType;
1202 }
1203 break;
1204 case COP1X:
1205 return kRegisterType;
1206
1207 // 26 bits immediate type instructions. e.g.: j imm26.
1208 case J:
1209 case JAL:
1210 return kJumpType;
1211
1212 default:
1213 if (checks == NORMAL) {
1214 return kImmediateType;
1215 } else {
1216 return kUnsupported;
1217 }
1218 }
1219}
1220
1221#undef OpcodeToBitNumber
1222#undef FunctionFieldToBitNumber
1223} // namespace internal
1224} // namespace v8
Andrei Popescu31002712010-02-23 13:46:05 +00001225
1226#endif // #ifndef V8_MIPS_CONSTANTS_H_