1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef V8_PPC_CONSTANTS_PPC_H_
6 #define V8_PPC_CONSTANTS_PPC_H_
10 #include "src/base/logging.h"
11 #include "src/base/macros.h"
12 #include "src/globals.h"
17 // Number of registers
18 const int kNumRegisters = 32;
21 const int kNumFPDoubleRegisters = 32;
22 const int kNumFPRegisters = kNumFPDoubleRegisters;
24 const int kNoRegister = -1;
26 // Used in embedded constant pool builder - max reach in bits for
27 // various load instructions (one less due to unsigned)
28 const int kLoadPtrMaxReachBits = 15;
29 const int kLoadDoubleMaxReachBits = 15;
31 // sign-extend the least significant 16-bits of value <imm>
32 #define SIGN_EXT_IMM16(imm) ((static_cast<int>(imm) << 16) >> 16)
34 // sign-extend the least significant 26-bits of value <imm>
35 #define SIGN_EXT_IMM26(imm) ((static_cast<int>(imm) << 6) >> 6)
37 // -----------------------------------------------------------------------------
40 // Defines constants and accessor classes to assemble, disassemble and
41 // simulate PPC instructions.
43 // Section references in the code refer to the "PowerPC Microprocessor
44 // Family: The Programmer.s Reference Guide" from 10/95
45 // https://www-01.ibm.com/chips/techlib/techlib.nsf/techdocs/852569B20050FF778525699600741775/$file/prg.pdf
48 // Constants for specific fields are defined in their respective named enums.
49 // General constants are in an anonymous enum in class Instr.
54 ge = 2, // Greater or equal.
56 gt = 4, // Greater than.
57 le = 5, // Less then or equal
58 unordered = 6, // Floating-point unordered
60 overflow = 8, // Summary overflow
66 inline Condition NegateCondition(Condition cond) {
68 return static_cast<Condition>(cond ^ ne);
72 // Commute a condition such that {a cond b == b cond' a}.
73 inline Condition CommuteCondition(Condition cond) {
88 // -----------------------------------------------------------------------------
89 // Instructions encoding.
91 // Instr is merely used by the Assembler to distinguish 32bit integers
92 // representing instructions from usual 32 bit values.
93 // Instruction objects are pointers to 32bit values, and provide methods to
94 // access the various ISA fields.
95 typedef int32_t Instr;
97 // Opcodes as defined in section 4.2 table 34 (32bit PowerPC)
99 TWI = 3 << 26, // Trap Word Immediate
100 MULLI = 7 << 26, // Multiply Low Immediate
101 SUBFIC = 8 << 26, // Subtract from Immediate Carrying
102 CMPLI = 10 << 26, // Compare Logical Immediate
103 CMPI = 11 << 26, // Compare Immediate
104 ADDIC = 12 << 26, // Add Immediate Carrying
105 ADDICx = 13 << 26, // Add Immediate Carrying and Record
106 ADDI = 14 << 26, // Add Immediate
107 ADDIS = 15 << 26, // Add Immediate Shifted
108 BCX = 16 << 26, // Branch Conditional
109 SC = 17 << 26, // System Call
110 BX = 18 << 26, // Branch
111 EXT1 = 19 << 26, // Extended code set 1
112 RLWIMIX = 20 << 26, // Rotate Left Word Immediate then Mask Insert
113 RLWINMX = 21 << 26, // Rotate Left Word Immediate then AND with Mask
114 RLWNMX = 23 << 26, // Rotate Left Word then AND with Mask
115 ORI = 24 << 26, // OR Immediate
116 ORIS = 25 << 26, // OR Immediate Shifted
117 XORI = 26 << 26, // XOR Immediate
118 XORIS = 27 << 26, // XOR Immediate Shifted
119 ANDIx = 28 << 26, // AND Immediate
120 ANDISx = 29 << 26, // AND Immediate Shifted
121 EXT5 = 30 << 26, // Extended code set 5 - 64bit only
122 EXT2 = 31 << 26, // Extended code set 2
123 LWZ = 32 << 26, // Load Word and Zero
124 LWZU = 33 << 26, // Load Word with Zero Update
125 LBZ = 34 << 26, // Load Byte and Zero
126 LBZU = 35 << 26, // Load Byte and Zero with Update
127 STW = 36 << 26, // Store
128 STWU = 37 << 26, // Store Word with Update
129 STB = 38 << 26, // Store Byte
130 STBU = 39 << 26, // Store Byte with Update
131 LHZ = 40 << 26, // Load Half and Zero
132 LHZU = 41 << 26, // Load Half and Zero with Update
133 LHA = 42 << 26, // Load Half Algebraic
134 LHAU = 43 << 26, // Load Half Algebraic with Update
135 STH = 44 << 26, // Store Half
136 STHU = 45 << 26, // Store Half with Update
137 LMW = 46 << 26, // Load Multiple Word
138 STMW = 47 << 26, // Store Multiple Word
139 LFS = 48 << 26, // Load Floating-Point Single
140 LFSU = 49 << 26, // Load Floating-Point Single with Update
141 LFD = 50 << 26, // Load Floating-Point Double
142 LFDU = 51 << 26, // Load Floating-Point Double with Update
143 STFS = 52 << 26, // Store Floating-Point Single
144 STFSU = 53 << 26, // Store Floating-Point Single with Update
145 STFD = 54 << 26, // Store Floating-Point Double
146 STFDU = 55 << 26, // Store Floating-Point Double with Update
147 LD = 58 << 26, // Load Double Word
148 EXT3 = 59 << 26, // Extended code set 3
149 STD = 62 << 26, // Store Double Word (optionally with Update)
150 EXT4 = 63 << 26 // Extended code set 4
155 MCRF = 0 << 1, // Move Condition Register Field
156 BCLRX = 16 << 1, // Branch Conditional Link Register
157 CRNOR = 33 << 1, // Condition Register NOR)
158 RFI = 50 << 1, // Return from Interrupt
159 CRANDC = 129 << 1, // Condition Register AND with Complement
160 ISYNC = 150 << 1, // Instruction Synchronize
161 CRXOR = 193 << 1, // Condition Register XOR
162 CRNAND = 225 << 1, // Condition Register NAND
163 CRAND = 257 << 1, // Condition Register AND
164 CREQV = 289 << 1, // Condition Register Equivalent
165 CRORC = 417 << 1, // Condition Register OR with Complement
166 CROR = 449 << 1, // Condition Register OR
167 BCCTRX = 528 << 1 // Branch Conditional to Count Register
181 LWZX = 23 << 1, // load word zero w/ x-form
188 MFVSRD = 51 << 1, // Move From VSR Doubleword
191 LWZUX = 55 << 1, // load word zero w/ update x-form
196 LBZX = 87 << 1, // load byte zero w/ x-form
198 MFVSRWZ = 115 << 1, // Move From VSR Word And Zero
199 LBZUX = 119 << 1, // load byte zero w/ update x-form
204 STWX = 151 << 1, // store word w/ x-form
205 MTVSRD = 179 << 1, // Move To VSR Doubleword
207 STWUX = 183 << 1, // store word w/ update x-form
214 ADDZEX = 202 << 1, // Add to Zero Extended
218 MTVSRWA = 211 << 1, // Move To VSR Word Algebraic
219 STBX = 215 << 1, // store byte w/ x-form
220 MULLD = 233 << 1, // Multiply Low Double Word
221 MULLW = 235 << 1, // Multiply Low Word
222 MTVSRWZ = 243 << 1, // Move To VSR Word And Zero
223 STBUX = 247 << 1, // store byte w/ update x-form
224 ADDX = 266 << 1, // Add
225 LHZX = 279 << 1, // load half-word zero w/ x-form
226 LHZUX = 311 << 1, // load half-word zero w/ update x-form
227 LWAX = 341 << 1, // load word algebraic w/ x-form
228 LHAX = 343 << 1, // load half-word algebraic w/ x-form
229 LHAUX = 375 << 1, // load half-word algebraic w/ update x-form
230 XORX = 316 << 1, // Exclusive OR
231 MFSPR = 339 << 1, // Move from Special-Purpose-Register
232 STHX = 407 << 1, // store half-word w/ x-form
233 ORC = 412 << 1, // Or with Complement
234 STHUX = 439 << 1, // store half-word w/ update x-form
235 ORX = 444 << 1, // Or
236 DIVDU = 457 << 1, // Divide Double Word Unsigned
237 DIVWU = 459 << 1, // Divide Word Unsigned
238 MTSPR = 467 << 1, // Move to Special-Purpose-Register
239 DIVD = 489 << 1, // Divide Double Word
240 DIVW = 491 << 1, // Divide Word
242 // Below represent bits 10-1 (any value >= 512)
243 LFSX = 535 << 1, // load float-single w/ x-form
244 SRWX = 536 << 1, // Shift Right Word
245 SRDX = 539 << 1, // Shift Right Double Word
246 LFSUX = 567 << 1, // load float-single w/ update x-form
247 SYNC = 598 << 1, // Synchronize
248 LFDX = 599 << 1, // load float-double w/ x-form
249 LFDUX = 631 << 1, // load float-double w/ update X-form
250 STFSX = 663 << 1, // store float-single w/ x-form
251 STFSUX = 695 << 1, // store float-single w/ update x-form
252 STFDX = 727 << 1, // store float-double w/ x-form
253 STFDUX = 759 << 1, // store float-double w/ update x-form
254 SRAW = 792 << 1, // Shift Right Algebraic Word
255 SRAD = 794 << 1, // Shift Right Algebraic Double Word
256 SRAWIX = 824 << 1, // Shift Right Algebraic Word Immediate
257 SRADIX = 413 << 2, // Shift Right Algebraic Double Word Immediate
258 EXTSH = 922 << 1, // Extend Sign Halfword
259 EXTSB = 954 << 1, // Extend Sign Byte
260 ICBI = 982 << 1, // Instruction Cache Block Invalidate
261 EXTSW = 986 << 1 // Extend Sign Word
264 // Some use Bits 10-1 and other only 5-1 for the opcode
267 FDIV = 18 << 1, // Floating Divide
268 FSUB = 20 << 1, // Floating Subtract
269 FADD = 21 << 1, // Floating Add
270 FSQRT = 22 << 1, // Floating Square Root
271 FSEL = 23 << 1, // Floating Select
272 FMUL = 25 << 1, // Floating Multiply
273 FMSUB = 28 << 1, // Floating Multiply-Subtract
274 FMADD = 29 << 1, // Floating Multiply-Add
277 FCMPU = 0 << 1, // Floating Compare Unordered
278 FRSP = 12 << 1, // Floating-Point Rounding
279 FCTIW = 14 << 1, // Floating Convert to Integer Word X-form
280 FCTIWZ = 15 << 1, // Floating Convert to Integer Word with Round to Zero
281 FNEG = 40 << 1, // Floating Negate
282 MCRFS = 64 << 1, // Move to Condition Register from FPSCR
283 FMR = 72 << 1, // Floating Move Register
284 MTFSFI = 134 << 1, // Move to FPSCR Field Immediate
285 FABS = 264 << 1, // Floating Absolute Value
286 FRIN = 392 << 1, // Floating Round to Integer Nearest
287 FRIZ = 424 << 1, // Floating Round to Integer Toward Zero
288 FRIP = 456 << 1, // Floating Round to Integer Plus
289 FRIM = 488 << 1, // Floating Round to Integer Minus
290 MFFS = 583 << 1, // move from FPSCR x-form
291 MTFSF = 711 << 1, // move to FPSCR fields XFL-form
292 FCFID = 846 << 1, // Floating convert from integer doubleword
293 FCTID = 814 << 1, // Floating convert from integer doubleword
294 FCTIDZ = 815 << 1 // Floating convert from integer doubleword
299 RLDICL = 0 << 1, // Rotate Left Double Word Immediate then Clear Left
300 RLDICR = 2 << 1, // Rotate Left Double Word Immediate then Clear Right
301 RLDIC = 4 << 1, // Rotate Left Double Word Immediate then Clear
302 RLDIMI = 6 << 1, // Rotate Left Double Word Immediate then Mask Insert
304 RLDCL = 8 << 1, // Rotate Left Double Word then Clear Left
305 RLDCR = 9 << 1 // Rotate Left Double Word then Clear Right
308 // Instruction encoding bits and masks.
310 // Instruction encoding bit
335 // Instruction bit masks
336 kCondMask = 0x1F << 21,
337 kOff12Mask = (1 << 12) - 1,
338 kImm24Mask = (1 << 24) - 1,
339 kOff16Mask = (1 << 16) - 1,
340 kImm16Mask = (1 << 16) - 1,
341 kImm26Mask = (1 << 26) - 1,
342 kBOfieldMask = 0x1f << 21,
343 kOpcodeMask = 0x3f << 26,
344 kExt1OpcodeMask = 0x3ff << 1,
345 kExt2OpcodeMask = 0x3ff << 1,
346 kExt2OpcodeVariant2Mask = 0x1ff << 2,
347 kExt5OpcodeMask = 0x3 << 2,
348 kBOMask = 0x1f << 21,
349 kBIMask = 0x1F << 16,
357 // -----------------------------------------------------------------------------
358 // Addressing modes and instruction variants.
360 // Overflow Exception
362 SetOE = 1 << 10, // Set overflow exception
363 LeaveOE = 0 << 10 // No overflow exception
367 enum RCBit { // Bit 0
368 SetRC = 1, // LT,GT,EQ,SO
373 enum LKBit { // Bit 0
374 SetLK = 1, // Load effective address of next instruction
375 LeaveLK = 0 // No action
378 enum BOfield { // Bits 25-21
379 DCBNZF = 0 << 21, // Decrement CTR; branch if CTR != 0 and condition false
380 DCBEZF = 2 << 21, // Decrement CTR; branch if CTR == 0 and condition false
381 BF = 4 << 21, // Branch if condition false
382 DCBNZT = 8 << 21, // Decrement CTR; branch if CTR != 0 and condition true
383 DCBEZT = 10 << 21, // Decrement CTR; branch if CTR == 0 and condition true
384 BT = 12 << 21, // Branch if condition true
385 DCBNZ = 16 << 21, // Decrement CTR; branch if CTR != 0
386 DCBEZ = 18 << 21, // Decrement CTR; branch if CTR == 0
387 BA = 20 << 21 // Branch always
397 enum CRBit { CR_LT = 0, CR_GT = 1, CR_EQ = 2, CR_SO = 3, CR_FU = 3 };
401 // -----------------------------------------------------------------------------
402 // Supervisor Call (svc) specific support.
404 // Special Software Interrupt codes when used in the presence of the PPC
406 // svc (formerly swi) provides a 24bit immediate value. Use bits 22:0 for
407 // standard SoftwareInterrupCode. Bit 23 is reserved for the stop feature.
408 enum SoftwareInterruptCodes {
409 // transition to C code
410 kCallRtRedirected = 0x10,
412 kBreakpoint = 0x821008, // bits23-0 of 0x7d821008 = twge r2, r2
416 const uint32_t kStopCodeMask = kStopCode - 1;
417 const uint32_t kMaxStopCode = kStopCode - 1;
418 const int32_t kDefaultStopCode = -1;
420 // FP rounding modes.
421 enum FPRoundingMode {
422 RN = 0, // Round to Nearest.
423 RZ = 1, // Round towards zero.
424 RP = 2, // Round towards Plus Infinity.
425 RM = 3, // Round towards Minus Infinity.
428 kRoundToNearest = RN,
430 kRoundToPlusInf = RP,
431 kRoundToMinusInf = RM
434 const uint32_t kFPRoundingModeMask = 3;
436 enum CheckForInexactConversion {
437 kCheckForInexactConversion,
438 kDontCheckForInexactConversion
441 // -----------------------------------------------------------------------------
442 // Specific instructions, constants, and masks.
443 // These constants are declared in assembler-arm.cc, as they use named registers
444 // and other constants.
447 // add(sp, sp, 4) instruction (aka Pop())
448 extern const Instr kPopInstruction;
450 // str(r, MemOperand(sp, 4, NegPreIndex), al) instruction (aka push(r))
451 // register r is not encoded.
452 extern const Instr kPushRegPattern;
454 // ldr(r, MemOperand(sp, 4, PostIndex), al) instruction (aka pop(r))
455 // register r is not encoded.
456 extern const Instr kPopRegPattern;
458 // use TWI to indicate redirection call for simulation mode
459 const Instr rtCallRedirInstr = TWI;
461 // -----------------------------------------------------------------------------
462 // Instruction abstraction.
464 // The class Instruction enables access to individual fields defined in the PPC
465 // architecture instruction set encoding.
466 // Note that the Assembler uses typedef int32_t Instr.
468 // Example: Test whether the instruction at ptr does set the condition code
471 // bool InstructionSetsConditionCodes(byte* ptr) {
472 // Instruction* instr = Instruction::At(ptr);
473 // int type = instr->TypeValue();
474 // return ((type == 0) || (type == 1)) && instr->HasS();
479 enum { kInstrSize = 4, kInstrSizeLog2 = 2, kPCReadOffset = 8 };
481 // Helper macro to define static accessors.
482 // We use the cast to char* trick to bypass the strict anti-aliasing rules.
483 #define DECLARE_STATIC_TYPED_ACCESSOR(return_type, Name) \
484 static inline return_type Name(Instr instr) { \
485 char* temp = reinterpret_cast<char*>(&instr); \
486 return reinterpret_cast<Instruction*>(temp)->Name(); \
489 #define DECLARE_STATIC_ACCESSOR(Name) DECLARE_STATIC_TYPED_ACCESSOR(int, Name)
491 // Get the raw instruction bits.
492 inline Instr InstructionBits() const {
493 return *reinterpret_cast<const Instr*>(this);
496 // Set the raw instruction bits to value.
497 inline void SetInstructionBits(Instr value) {
498 *reinterpret_cast<Instr*>(this) = value;
501 // Read one particular bit out of the instruction bits.
502 inline int Bit(int nr) const { return (InstructionBits() >> nr) & 1; }
504 // Read a bit field's value out of the instruction bits.
505 inline int Bits(int hi, int lo) const {
506 return (InstructionBits() >> lo) & ((2 << (hi - lo)) - 1);
509 // Read a bit field out of the instruction bits.
510 inline int BitField(int hi, int lo) const {
511 return InstructionBits() & (((2 << (hi - lo)) - 1) << lo);
516 // Read one particular bit out of the instruction bits.
517 static inline int Bit(Instr instr, int nr) { return (instr >> nr) & 1; }
519 // Read the value of a bit field out of the instruction bits.
520 static inline int Bits(Instr instr, int hi, int lo) {
521 return (instr >> lo) & ((2 << (hi - lo)) - 1);
525 // Read a bit field out of the instruction bits.
526 static inline int BitField(Instr instr, int hi, int lo) {
527 return instr & (((2 << (hi - lo)) - 1) << lo);
530 inline int RSValue() const { return Bits(25, 21); }
531 inline int RTValue() const { return Bits(25, 21); }
532 inline int RAValue() const { return Bits(20, 16); }
533 DECLARE_STATIC_ACCESSOR(RAValue);
534 inline int RBValue() const { return Bits(15, 11); }
535 DECLARE_STATIC_ACCESSOR(RBValue);
536 inline int RCValue() const { return Bits(10, 6); }
537 DECLARE_STATIC_ACCESSOR(RCValue);
539 inline int OpcodeValue() const { return static_cast<Opcode>(Bits(31, 26)); }
540 inline Opcode OpcodeField() const {
541 return static_cast<Opcode>(BitField(24, 21));
544 // Fields used in Software interrupt instructions
545 inline SoftwareInterruptCodes SvcValue() const {
546 return static_cast<SoftwareInterruptCodes>(Bits(23, 0));
549 // Instructions are read of out a code stream. The only way to get a
550 // reference to an instruction is to convert a pointer. There is no way
551 // to allocate or create instances of class Instruction.
552 // Use the At(pc) function to create references to Instruction.
553 static Instruction* At(byte* pc) {
554 return reinterpret_cast<Instruction*>(pc);
559 // We need to prevent the creation of instances of class Instruction.
560 DISALLOW_IMPLICIT_CONSTRUCTORS(Instruction);
564 // Helper functions for converting between register numbers and names.
567 // Return the name of the register.
568 static const char* Name(int reg);
570 // Lookup the register number for the name provided.
571 static int Number(const char* name);
573 struct RegisterAlias {
579 static const char* names_[kNumRegisters];
580 static const RegisterAlias aliases_[];
583 // Helper functions for converting between FP register numbers and names.
586 // Return the name of the register.
587 static const char* Name(int reg);
589 // Lookup the register number for the name provided.
590 static int Number(const char* name);
593 static const char* names_[kNumFPRegisters];
596 } // namespace v8::internal
598 #endif // V8_PPC_CONSTANTS_PPC_H_