1 // Copyright (c) 1994-2006 Sun Microsystems Inc. 2 // All Rights Reserved. 3 // 4 // Redistribution and use in source and binary forms, with or without 5 // modification, are permitted provided that the following conditions are 6 // met: 7 // 8 // - Redistributions of source code must retain the above copyright notice, 9 // this list of conditions and the following disclaimer. 10 // 11 // - Redistribution in binary form must reproduce the above copyright 12 // notice, this list of conditions and the following disclaimer in the 13 // documentation and/or other materials provided with the distribution. 14 // 15 // - Neither the name of Sun Microsystems or the names of contributors may 16 // be used to endorse or promote products derived from this software without 17 // specific prior written permission. 18 // 19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 20 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 21 // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 23 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 24 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 25 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 26 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 27 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 28 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 31 // The original source code covered by the above license above has been 32 // modified significantly by Google Inc. 33 // Copyright 2012 the V8 project authors. All rights reserved. 34 35 36 #ifndef V8_MIPS_ASSEMBLER_MIPS_H_ 37 #define V8_MIPS_ASSEMBLER_MIPS_H_ 38 39 #include <stdio.h> 40 41 #include <set> 42 43 #include "src/assembler.h" 44 #include "src/mips64/constants-mips64.h" 45 46 namespace v8 { 47 namespace internal { 48 49 // clang-format off 50 #define GENERAL_REGISTERS(V) \ 51 V(zero_reg) V(at) V(v0) V(v1) V(a0) V(a1) V(a2) V(a3) \ 52 V(a4) V(a5) V(a6) V(a7) V(t0) V(t1) V(t2) V(t3) \ 53 V(s0) V(s1) V(s2) V(s3) V(s4) V(s5) V(s6) V(s7) V(t8) V(t9) \ 54 V(k0) V(k1) V(gp) V(sp) V(fp) V(ra) 55 56 #define ALLOCATABLE_GENERAL_REGISTERS(V) \ 57 V(v0) V(v1) V(a0) V(a1) V(a2) V(a3) \ 58 V(a4) V(a5) V(a6) V(a7) V(t0) V(t1) V(t2) V(s7) 59 60 #define DOUBLE_REGISTERS(V) \ 61 V(f0) V(f1) V(f2) V(f3) V(f4) V(f5) V(f6) V(f7) \ 62 V(f8) V(f9) V(f10) V(f11) V(f12) V(f13) V(f14) V(f15) \ 63 V(f16) V(f17) V(f18) V(f19) V(f20) V(f21) V(f22) V(f23) \ 64 V(f24) V(f25) V(f26) V(f27) V(f28) V(f29) V(f30) V(f31) 65 66 #define FLOAT_REGISTERS DOUBLE_REGISTERS 67 #define SIMD128_REGISTERS DOUBLE_REGISTERS 68 69 #define ALLOCATABLE_DOUBLE_REGISTERS(V) \ 70 V(f0) V(f2) V(f4) V(f6) V(f8) V(f10) V(f12) V(f14) \ 71 V(f16) V(f18) V(f20) V(f22) V(f24) V(f26) 72 // clang-format on 73 74 // CPU Registers. 75 // 76 // 1) We would prefer to use an enum, but enum values are assignment- 77 // compatible with int, which has caused code-generation bugs. 78 // 79 // 2) We would prefer to use a class instead of a struct but we don't like 80 // the register initialization to depend on the particular initialization 81 // order (which appears to be different on OS X, Linux, and Windows for the 82 // installed versions of C++ we tried). Using a struct permits C-style 83 // "initialization". Also, the Register objects cannot be const as this 84 // forces initialization stubs in MSVC, making us dependent on initialization 85 // order. 86 // 87 // 3) By not using an enum, we are possibly preventing the compiler from 88 // doing certain constant folds, which may significantly reduce the 89 // code generated for some assembly instructions (because they boil down 90 // to a few constants). If this is a problem, we could change the code 91 // such that we use an enum in optimized mode, and the struct in debug 92 // mode. This way we get the compile-time error checking in debug mode 93 // and best performance in optimized code. 94 95 96 // ----------------------------------------------------------------------------- 97 // Implementation of Register and FPURegister. 98 99 struct Register { 100 static const int kCpRegister = 23; // cp (s7) is the 23rd register. 101 102 #if defined(V8_TARGET_LITTLE_ENDIAN) 103 static const int kMantissaOffset = 0; 104 static const int kExponentOffset = 4; 105 #elif defined(V8_TARGET_BIG_ENDIAN) 106 static const int kMantissaOffset = 4; 107 static const int kExponentOffset = 0; 108 #else 109 #error Unknown endianness 110 #endif 111 112 enum Code { 113 #define REGISTER_CODE(R) kCode_##R, 114 GENERAL_REGISTERS(REGISTER_CODE) 115 #undef REGISTER_CODE 116 kAfterLast, 117 kCode_no_reg = -1 118 }; 119 120 static const int kNumRegisters = Code::kAfterLast; 121 from_codeRegister122 static Register from_code(int code) { 123 DCHECK(code >= 0); 124 DCHECK(code < kNumRegisters); 125 Register r = { code }; 126 return r; 127 } 128 is_validRegister129 bool is_valid() const { return 0 <= reg_code && reg_code < kNumRegisters; } isRegister130 bool is(Register reg) const { return reg_code == reg.reg_code; } codeRegister131 int code() const { 132 DCHECK(is_valid()); 133 return reg_code; 134 } bitRegister135 int bit() const { 136 DCHECK(is_valid()); 137 return 1 << reg_code; 138 } 139 140 // Unfortunately we can't make this private in a struct. 141 int reg_code; 142 }; 143 144 // s7: context register 145 // s3: lithium scratch 146 // s4: lithium scratch2 147 #define DECLARE_REGISTER(R) const Register R = {Register::kCode_##R}; 148 GENERAL_REGISTERS(DECLARE_REGISTER) 149 #undef DECLARE_REGISTER 150 const Register no_reg = {Register::kCode_no_reg}; 151 152 153 int ToNumber(Register reg); 154 155 Register ToRegister(int num); 156 157 static const bool kSimpleFPAliasing = true; 158 159 // Coprocessor register. 160 struct FPURegister { 161 enum Code { 162 #define REGISTER_CODE(R) kCode_##R, 163 DOUBLE_REGISTERS(REGISTER_CODE) 164 #undef REGISTER_CODE 165 kAfterLast, 166 kCode_no_reg = -1 167 }; 168 169 static const int kMaxNumRegisters = Code::kAfterLast; 170 171 inline static int NumRegisters(); 172 173 // TODO(plind): Warning, inconsistent numbering here. kNumFPURegisters refers 174 // to number of 32-bit FPU regs, but kNumAllocatableRegisters refers to 175 // number of Double regs (64-bit regs, or FPU-reg-pairs). 176 is_validFPURegister177 bool is_valid() const { return 0 <= reg_code && reg_code < kMaxNumRegisters; } isFPURegister178 bool is(FPURegister reg) const { return reg_code == reg.reg_code; } lowFPURegister179 FPURegister low() const { 180 // TODO(plind): Create DCHECK for FR=0 mode. This usage suspect for FR=1. 181 // Find low reg of a Double-reg pair, which is the reg itself. 182 DCHECK(reg_code % 2 == 0); // Specified Double reg must be even. 183 FPURegister reg; 184 reg.reg_code = reg_code; 185 DCHECK(reg.is_valid()); 186 return reg; 187 } highFPURegister188 FPURegister high() const { 189 // TODO(plind): Create DCHECK for FR=0 mode. This usage illegal in FR=1. 190 // Find high reg of a Doubel-reg pair, which is reg + 1. 191 DCHECK(reg_code % 2 == 0); // Specified Double reg must be even. 192 FPURegister reg; 193 reg.reg_code = reg_code + 1; 194 DCHECK(reg.is_valid()); 195 return reg; 196 } 197 codeFPURegister198 int code() const { 199 DCHECK(is_valid()); 200 return reg_code; 201 } bitFPURegister202 int bit() const { 203 DCHECK(is_valid()); 204 return 1 << reg_code; 205 } 206 from_codeFPURegister207 static FPURegister from_code(int code) { 208 FPURegister r = {code}; 209 return r; 210 } setcodeFPURegister211 void setcode(int f) { 212 reg_code = f; 213 DCHECK(is_valid()); 214 } 215 // Unfortunately we can't make this private in a struct. 216 int reg_code; 217 }; 218 219 // A few double registers are reserved: one as a scratch register and one to 220 // hold 0.0. 221 // f28: 0.0 222 // f30: scratch register. 223 224 // V8 now supports the O32 ABI, and the FPU Registers are organized as 32 225 // 32-bit registers, f0 through f31. When used as 'double' they are used 226 // in pairs, starting with the even numbered register. So a double operation 227 // on f0 really uses f0 and f1. 228 // (Modern mips hardware also supports 32 64-bit registers, via setting 229 // (privileged) Status Register FR bit to 1. This is used by the N32 ABI, 230 // but it is not in common use. Someday we will want to support this in v8.) 231 232 // For O32 ABI, Floats and Doubles refer to same set of 32 32-bit registers. 233 typedef FPURegister FloatRegister; 234 235 typedef FPURegister DoubleRegister; 236 237 // TODO(mips64) Define SIMD registers. 238 typedef FPURegister Simd128Register; 239 240 const DoubleRegister no_freg = {-1}; 241 242 const DoubleRegister f0 = {0}; // Return value in hard float mode. 243 const DoubleRegister f1 = {1}; 244 const DoubleRegister f2 = {2}; 245 const DoubleRegister f3 = {3}; 246 const DoubleRegister f4 = {4}; 247 const DoubleRegister f5 = {5}; 248 const DoubleRegister f6 = {6}; 249 const DoubleRegister f7 = {7}; 250 const DoubleRegister f8 = {8}; 251 const DoubleRegister f9 = {9}; 252 const DoubleRegister f10 = {10}; 253 const DoubleRegister f11 = {11}; 254 const DoubleRegister f12 = {12}; // Arg 0 in hard float mode. 255 const DoubleRegister f13 = {13}; 256 const DoubleRegister f14 = {14}; // Arg 1 in hard float mode. 257 const DoubleRegister f15 = {15}; 258 const DoubleRegister f16 = {16}; 259 const DoubleRegister f17 = {17}; 260 const DoubleRegister f18 = {18}; 261 const DoubleRegister f19 = {19}; 262 const DoubleRegister f20 = {20}; 263 const DoubleRegister f21 = {21}; 264 const DoubleRegister f22 = {22}; 265 const DoubleRegister f23 = {23}; 266 const DoubleRegister f24 = {24}; 267 const DoubleRegister f25 = {25}; 268 const DoubleRegister f26 = {26}; 269 const DoubleRegister f27 = {27}; 270 const DoubleRegister f28 = {28}; 271 const DoubleRegister f29 = {29}; 272 const DoubleRegister f30 = {30}; 273 const DoubleRegister f31 = {31}; 274 275 // Register aliases. 276 // cp is assumed to be a callee saved register. 277 // Defined using #define instead of "static const Register&" because Clang 278 // complains otherwise when a compilation unit that includes this header 279 // doesn't use the variables. 280 #define kRootRegister s6 281 #define cp s7 282 #define kLithiumScratchReg s3 283 #define kLithiumScratchReg2 s4 284 #define kLithiumScratchDouble f30 285 #define kDoubleRegZero f28 286 // Used on mips64r6 for compare operations. 287 // We use the last non-callee saved odd register for N64 ABI 288 #define kDoubleCompareReg f23 289 290 // FPU (coprocessor 1) control registers. 291 // Currently only FCSR (#31) is implemented. 292 struct FPUControlRegister { is_validFPUControlRegister293 bool is_valid() const { return reg_code == kFCSRRegister; } isFPUControlRegister294 bool is(FPUControlRegister creg) const { return reg_code == creg.reg_code; } codeFPUControlRegister295 int code() const { 296 DCHECK(is_valid()); 297 return reg_code; 298 } bitFPUControlRegister299 int bit() const { 300 DCHECK(is_valid()); 301 return 1 << reg_code; 302 } setcodeFPUControlRegister303 void setcode(int f) { 304 reg_code = f; 305 DCHECK(is_valid()); 306 } 307 // Unfortunately we can't make this private in a struct. 308 int reg_code; 309 }; 310 311 const FPUControlRegister no_fpucreg = { kInvalidFPUControlRegister }; 312 const FPUControlRegister FCSR = { kFCSRRegister }; 313 314 // ----------------------------------------------------------------------------- 315 // Machine instruction Operands. 316 const int kSmiShift = kSmiTagSize + kSmiShiftSize; 317 const uint64_t kSmiShiftMask = (1UL << kSmiShift) - 1; 318 // Class Operand represents a shifter operand in data processing instructions. 319 class Operand BASE_EMBEDDED { 320 public: 321 // Immediate. 322 INLINE(explicit Operand(int64_t immediate, 323 RelocInfo::Mode rmode = RelocInfo::NONE64)); 324 INLINE(explicit Operand(const ExternalReference& f)); 325 INLINE(explicit Operand(const char* s)); 326 INLINE(explicit Operand(Object** opp)); 327 INLINE(explicit Operand(Context** cpp)); 328 explicit Operand(Handle<Object> handle); 329 INLINE(explicit Operand(Smi* value)); 330 331 // Register. 332 INLINE(explicit Operand(Register rm)); 333 334 // Return true if this is a register operand. 335 INLINE(bool is_reg() const); 336 immediate()337 inline int64_t immediate() const { 338 DCHECK(!is_reg()); 339 return imm64_; 340 } 341 rm()342 Register rm() const { return rm_; } 343 344 private: 345 Register rm_; 346 int64_t imm64_; // Valid if rm_ == no_reg. 347 RelocInfo::Mode rmode_; 348 349 friend class Assembler; 350 friend class MacroAssembler; 351 }; 352 353 354 // On MIPS we have only one adressing mode with base_reg + offset. 355 // Class MemOperand represents a memory operand in load and store instructions. 356 class MemOperand : public Operand { 357 public: 358 // Immediate value attached to offset. 359 enum OffsetAddend { 360 offset_minus_one = -1, 361 offset_zero = 0 362 }; 363 364 explicit MemOperand(Register rn, int32_t offset = 0); 365 explicit MemOperand(Register rn, int32_t unit, int32_t multiplier, 366 OffsetAddend offset_addend = offset_zero); offset()367 int32_t offset() const { return offset_; } 368 OffsetIsInt16Encodable()369 bool OffsetIsInt16Encodable() const { 370 return is_int16(offset_); 371 } 372 373 private: 374 int32_t offset_; 375 376 friend class Assembler; 377 }; 378 379 380 class Assembler : public AssemblerBase { 381 public: 382 // Create an assembler. Instructions and relocation information are emitted 383 // into a buffer, with the instructions starting from the beginning and the 384 // relocation information starting from the end of the buffer. See CodeDesc 385 // for a detailed comment on the layout (globals.h). 386 // 387 // If the provided buffer is NULL, the assembler allocates and grows its own 388 // buffer, and buffer_size determines the initial buffer size. The buffer is 389 // owned by the assembler and deallocated upon destruction of the assembler. 390 // 391 // If the provided buffer is not NULL, the assembler uses the provided buffer 392 // for code generation and assumes its size to be buffer_size. If the buffer 393 // is too small, a fatal error occurs. No deallocation of the buffer is done 394 // upon destruction of the assembler. 395 Assembler(Isolate* isolate, void* buffer, int buffer_size); ~Assembler()396 virtual ~Assembler() { } 397 398 // GetCode emits any pending (non-emitted) code and fills the descriptor 399 // desc. GetCode() is idempotent; it returns the same result if no other 400 // Assembler functions are invoked in between GetCode() calls. 401 void GetCode(CodeDesc* desc); 402 403 // Label operations & relative jumps (PPUM Appendix D). 404 // 405 // Takes a branch opcode (cc) and a label (L) and generates 406 // either a backward branch or a forward branch and links it 407 // to the label fixup chain. Usage: 408 // 409 // Label L; // unbound label 410 // j(cc, &L); // forward branch to unbound label 411 // bind(&L); // bind label to the current pc 412 // j(cc, &L); // backward branch to bound label 413 // bind(&L); // illegal: a label may be bound only once 414 // 415 // Note: The same Label can be used for forward and backward branches 416 // but it may be bound only once. 417 void bind(Label* L); // Binds an unbound label L to current code position. 418 419 enum OffsetSize : int { kOffset26 = 26, kOffset21 = 21, kOffset16 = 16 }; 420 421 // Determines if Label is bound and near enough so that branch instruction 422 // can be used to reach it, instead of jump instruction. 423 bool is_near(Label* L); 424 bool is_near(Label* L, OffsetSize bits); 425 bool is_near_branch(Label* L); is_near_pre_r6(Label * L)426 inline bool is_near_pre_r6(Label* L) { 427 DCHECK(!(kArchVariant == kMips64r6)); 428 return pc_offset() - L->pos() < kMaxBranchOffset - 4 * kInstrSize; 429 } is_near_r6(Label * L)430 inline bool is_near_r6(Label* L) { 431 DCHECK(kArchVariant == kMips64r6); 432 return pc_offset() - L->pos() < kMaxCompactBranchOffset - 4 * kInstrSize; 433 } 434 435 int BranchOffset(Instr instr); 436 437 // Returns the branch offset to the given label from the current code 438 // position. Links the label to the current position if it is still unbound. 439 // Manages the jump elimination optimization if the second parameter is true. 440 int32_t branch_offset_helper(Label* L, OffsetSize bits); branch_offset(Label * L)441 inline int32_t branch_offset(Label* L) { 442 return branch_offset_helper(L, OffsetSize::kOffset16); 443 } branch_offset21(Label * L)444 inline int32_t branch_offset21(Label* L) { 445 return branch_offset_helper(L, OffsetSize::kOffset21); 446 } branch_offset26(Label * L)447 inline int32_t branch_offset26(Label* L) { 448 return branch_offset_helper(L, OffsetSize::kOffset26); 449 } shifted_branch_offset(Label * L)450 inline int32_t shifted_branch_offset(Label* L) { 451 return branch_offset(L) >> 2; 452 } shifted_branch_offset21(Label * L)453 inline int32_t shifted_branch_offset21(Label* L) { 454 return branch_offset21(L) >> 2; 455 } shifted_branch_offset26(Label * L)456 inline int32_t shifted_branch_offset26(Label* L) { 457 return branch_offset26(L) >> 2; 458 } 459 uint64_t jump_address(Label* L); 460 uint64_t jump_offset(Label* L); 461 462 // Puts a labels target address at the given position. 463 // The high 8 bits are set to zero. 464 void label_at_put(Label* L, int at_offset); 465 466 // Read/Modify the code target address in the branch/call instruction at pc. 467 static Address target_address_at(Address pc); 468 static void set_target_address_at( 469 Isolate* isolate, Address pc, Address target, 470 ICacheFlushMode icache_flush_mode = FLUSH_ICACHE_IF_NEEDED); 471 // On MIPS there is no Constant Pool so we skip that parameter. INLINE(static Address target_address_at (Address pc,Address constant_pool))472 INLINE(static Address target_address_at(Address pc, Address constant_pool)) { 473 return target_address_at(pc); 474 } INLINE(static void set_target_address_at (Isolate * isolate,Address pc,Address constant_pool,Address target,ICacheFlushMode icache_flush_mode=FLUSH_ICACHE_IF_NEEDED))475 INLINE(static void set_target_address_at( 476 Isolate* isolate, Address pc, Address constant_pool, Address target, 477 ICacheFlushMode icache_flush_mode = FLUSH_ICACHE_IF_NEEDED)) { 478 set_target_address_at(isolate, pc, target, icache_flush_mode); 479 } INLINE(static Address target_address_at (Address pc,Code * code))480 INLINE(static Address target_address_at(Address pc, Code* code)) { 481 Address constant_pool = code ? code->constant_pool() : NULL; 482 return target_address_at(pc, constant_pool); 483 } INLINE(static void set_target_address_at (Isolate * isolate,Address pc,Code * code,Address target,ICacheFlushMode icache_flush_mode=FLUSH_ICACHE_IF_NEEDED))484 INLINE(static void set_target_address_at( 485 Isolate* isolate, Address pc, Code* code, Address target, 486 ICacheFlushMode icache_flush_mode = FLUSH_ICACHE_IF_NEEDED)) { 487 Address constant_pool = code ? code->constant_pool() : NULL; 488 set_target_address_at(isolate, pc, constant_pool, target, 489 icache_flush_mode); 490 } 491 492 // Return the code target address at a call site from the return address 493 // of that call in the instruction stream. 494 inline static Address target_address_from_return_address(Address pc); 495 496 static void JumpLabelToJumpRegister(Address pc); 497 498 static void QuietNaN(HeapObject* nan); 499 500 // This sets the branch destination (which gets loaded at the call address). 501 // This is for calls and branches within generated code. The serializer 502 // has already deserialized the lui/ori instructions etc. deserialization_set_special_target_at(Isolate * isolate,Address instruction_payload,Code * code,Address target)503 inline static void deserialization_set_special_target_at( 504 Isolate* isolate, Address instruction_payload, Code* code, 505 Address target) { 506 set_target_address_at( 507 isolate, 508 instruction_payload - kInstructionsFor64BitConstant * kInstrSize, code, 509 target); 510 } 511 512 // This sets the internal reference at the pc. 513 inline static void deserialization_set_target_internal_reference_at( 514 Isolate* isolate, Address pc, Address target, 515 RelocInfo::Mode mode = RelocInfo::INTERNAL_REFERENCE); 516 517 // Size of an instruction. 518 static const int kInstrSize = sizeof(Instr); 519 520 // Difference between address of current opcode and target address offset. 521 static const int kBranchPCOffset = 4; 522 523 // Here we are patching the address in the LUI/ORI instruction pair. 524 // These values are used in the serialization process and must be zero for 525 // MIPS platform, as Code, Embedded Object or External-reference pointers 526 // are split across two consecutive instructions and don't exist separately 527 // in the code, so the serializer should not step forwards in memory after 528 // a target is resolved and written. 529 static const int kSpecialTargetSize = 0; 530 531 // Number of consecutive instructions used to store 32bit/64bit constant. 532 // This constant was used in RelocInfo::target_address_address() function 533 // to tell serializer address of the instruction that follows 534 // LUI/ORI instruction pair. 535 static const int kInstructionsFor32BitConstant = 2; 536 static const int kInstructionsFor64BitConstant = 4; 537 538 // Distance between the instruction referring to the address of the call 539 // target and the return address. 540 #ifdef _MIPS_ARCH_MIPS64R6 541 static const int kCallTargetAddressOffset = 5 * kInstrSize; 542 #else 543 static const int kCallTargetAddressOffset = 6 * kInstrSize; 544 #endif 545 546 // Distance between start of patched debug break slot and the emitted address 547 // to jump to. 548 static const int kPatchDebugBreakSlotAddressOffset = 6 * kInstrSize; 549 550 // Difference between address of current opcode and value read from pc 551 // register. 552 static const int kPcLoadDelta = 4; 553 554 #ifdef _MIPS_ARCH_MIPS64R6 555 static const int kDebugBreakSlotInstructions = 5; 556 #else 557 static const int kDebugBreakSlotInstructions = 6; 558 #endif 559 static const int kDebugBreakSlotLength = 560 kDebugBreakSlotInstructions * kInstrSize; 561 562 563 // --------------------------------------------------------------------------- 564 // Code generation. 565 566 // Insert the smallest number of nop instructions 567 // possible to align the pc offset to a multiple 568 // of m. m must be a power of 2 (>= 4). 569 void Align(int m); 570 // Insert the smallest number of zero bytes possible to align the pc offset 571 // to a mulitple of m. m must be a power of 2 (>= 2). 572 void DataAlign(int m); 573 // Aligns code to something that's optimal for a jump target for the platform. 574 void CodeTargetAlign(); 575 576 // Different nop operations are used by the code generator to detect certain 577 // states of the generated code. 578 enum NopMarkerTypes { 579 NON_MARKING_NOP = 0, 580 DEBUG_BREAK_NOP, 581 // IC markers. 582 PROPERTY_ACCESS_INLINED, 583 PROPERTY_ACCESS_INLINED_CONTEXT, 584 PROPERTY_ACCESS_INLINED_CONTEXT_DONT_DELETE, 585 // Helper values. 586 LAST_CODE_MARKER, 587 FIRST_IC_MARKER = PROPERTY_ACCESS_INLINED, 588 // Code aging 589 CODE_AGE_MARKER_NOP = 6, 590 CODE_AGE_SEQUENCE_NOP 591 }; 592 593 // Type == 0 is the default non-marking nop. For mips this is a 594 // sll(zero_reg, zero_reg, 0). We use rt_reg == at for non-zero 595 // marking, to avoid conflict with ssnop and ehb instructions. 596 void nop(unsigned int type = 0) { 597 DCHECK(type < 32); 598 Register nop_rt_reg = (type == 0) ? zero_reg : at; 599 sll(zero_reg, nop_rt_reg, type, true); 600 } 601 602 603 // --------Branch-and-jump-instructions---------- 604 // We don't use likely variant of instructions. 605 void b(int16_t offset); b(Label * L)606 inline void b(Label* L) { b(shifted_branch_offset(L)); } 607 void bal(int16_t offset); bal(Label * L)608 inline void bal(Label* L) { bal(shifted_branch_offset(L)); } 609 void bc(int32_t offset); bc(Label * L)610 inline void bc(Label* L) { bc(shifted_branch_offset26(L)); } 611 void balc(int32_t offset); balc(Label * L)612 inline void balc(Label* L) { balc(shifted_branch_offset26(L)); } 613 614 void beq(Register rs, Register rt, int16_t offset); beq(Register rs,Register rt,Label * L)615 inline void beq(Register rs, Register rt, Label* L) { 616 beq(rs, rt, shifted_branch_offset(L)); 617 } 618 void bgez(Register rs, int16_t offset); 619 void bgezc(Register rt, int16_t offset); bgezc(Register rt,Label * L)620 inline void bgezc(Register rt, Label* L) { 621 bgezc(rt, shifted_branch_offset(L)); 622 } 623 void bgeuc(Register rs, Register rt, int16_t offset); bgeuc(Register rs,Register rt,Label * L)624 inline void bgeuc(Register rs, Register rt, Label* L) { 625 bgeuc(rs, rt, shifted_branch_offset(L)); 626 } 627 void bgec(Register rs, Register rt, int16_t offset); bgec(Register rs,Register rt,Label * L)628 inline void bgec(Register rs, Register rt, Label* L) { 629 bgec(rs, rt, shifted_branch_offset(L)); 630 } 631 void bgezal(Register rs, int16_t offset); 632 void bgezalc(Register rt, int16_t offset); bgezalc(Register rt,Label * L)633 inline void bgezalc(Register rt, Label* L) { 634 bgezalc(rt, shifted_branch_offset(L)); 635 } 636 void bgezall(Register rs, int16_t offset); bgezall(Register rs,Label * L)637 inline void bgezall(Register rs, Label* L) { 638 bgezall(rs, branch_offset(L) >> 2); 639 } 640 void bgtz(Register rs, int16_t offset); 641 void bgtzc(Register rt, int16_t offset); bgtzc(Register rt,Label * L)642 inline void bgtzc(Register rt, Label* L) { 643 bgtzc(rt, shifted_branch_offset(L)); 644 } 645 void blez(Register rs, int16_t offset); 646 void blezc(Register rt, int16_t offset); blezc(Register rt,Label * L)647 inline void blezc(Register rt, Label* L) { 648 blezc(rt, shifted_branch_offset(L)); 649 } 650 void bltz(Register rs, int16_t offset); 651 void bltzc(Register rt, int16_t offset); bltzc(Register rt,Label * L)652 inline void bltzc(Register rt, Label* L) { 653 bltzc(rt, shifted_branch_offset(L)); 654 } 655 void bltuc(Register rs, Register rt, int16_t offset); bltuc(Register rs,Register rt,Label * L)656 inline void bltuc(Register rs, Register rt, Label* L) { 657 bltuc(rs, rt, shifted_branch_offset(L)); 658 } 659 void bltc(Register rs, Register rt, int16_t offset); bltc(Register rs,Register rt,Label * L)660 inline void bltc(Register rs, Register rt, Label* L) { 661 bltc(rs, rt, shifted_branch_offset(L)); 662 } 663 void bltzal(Register rs, int16_t offset); 664 void blezalc(Register rt, int16_t offset); blezalc(Register rt,Label * L)665 inline void blezalc(Register rt, Label* L) { 666 blezalc(rt, shifted_branch_offset(L)); 667 } 668 void bltzalc(Register rt, int16_t offset); bltzalc(Register rt,Label * L)669 inline void bltzalc(Register rt, Label* L) { 670 bltzalc(rt, shifted_branch_offset(L)); 671 } 672 void bgtzalc(Register rt, int16_t offset); bgtzalc(Register rt,Label * L)673 inline void bgtzalc(Register rt, Label* L) { 674 bgtzalc(rt, shifted_branch_offset(L)); 675 } 676 void beqzalc(Register rt, int16_t offset); beqzalc(Register rt,Label * L)677 inline void beqzalc(Register rt, Label* L) { 678 beqzalc(rt, shifted_branch_offset(L)); 679 } 680 void beqc(Register rs, Register rt, int16_t offset); beqc(Register rs,Register rt,Label * L)681 inline void beqc(Register rs, Register rt, Label* L) { 682 beqc(rs, rt, shifted_branch_offset(L)); 683 } 684 void beqzc(Register rs, int32_t offset); beqzc(Register rs,Label * L)685 inline void beqzc(Register rs, Label* L) { 686 beqzc(rs, shifted_branch_offset21(L)); 687 } 688 void bnezalc(Register rt, int16_t offset); bnezalc(Register rt,Label * L)689 inline void bnezalc(Register rt, Label* L) { 690 bnezalc(rt, shifted_branch_offset(L)); 691 } 692 void bnec(Register rs, Register rt, int16_t offset); bnec(Register rs,Register rt,Label * L)693 inline void bnec(Register rs, Register rt, Label* L) { 694 bnec(rs, rt, shifted_branch_offset(L)); 695 } 696 void bnezc(Register rt, int32_t offset); bnezc(Register rt,Label * L)697 inline void bnezc(Register rt, Label* L) { 698 bnezc(rt, shifted_branch_offset21(L)); 699 } 700 void bne(Register rs, Register rt, int16_t offset); bne(Register rs,Register rt,Label * L)701 inline void bne(Register rs, Register rt, Label* L) { 702 bne(rs, rt, shifted_branch_offset(L)); 703 } 704 void bovc(Register rs, Register rt, int16_t offset); bovc(Register rs,Register rt,Label * L)705 inline void bovc(Register rs, Register rt, Label* L) { 706 bovc(rs, rt, shifted_branch_offset(L)); 707 } 708 void bnvc(Register rs, Register rt, int16_t offset); bnvc(Register rs,Register rt,Label * L)709 inline void bnvc(Register rs, Register rt, Label* L) { 710 bnvc(rs, rt, shifted_branch_offset(L)); 711 } 712 713 // Never use the int16_t b(l)cond version with a branch offset 714 // instead of using the Label* version. 715 716 // Jump targets must be in the current 256 MB-aligned region. i.e. 28 bits. 717 void j(int64_t target); 718 void jal(int64_t target); 719 void j(Label* target); 720 void jal(Label* target); 721 void jalr(Register rs, Register rd = ra); 722 void jr(Register target); 723 void jic(Register rt, int16_t offset); 724 void jialc(Register rt, int16_t offset); 725 726 727 // -------Data-processing-instructions--------- 728 729 // Arithmetic. 730 void addu(Register rd, Register rs, Register rt); 731 void subu(Register rd, Register rs, Register rt); 732 733 void div(Register rs, Register rt); 734 void divu(Register rs, Register rt); 735 void ddiv(Register rs, Register rt); 736 void ddivu(Register rs, Register rt); 737 void div(Register rd, Register rs, Register rt); 738 void divu(Register rd, Register rs, Register rt); 739 void ddiv(Register rd, Register rs, Register rt); 740 void ddivu(Register rd, Register rs, Register rt); 741 void mod(Register rd, Register rs, Register rt); 742 void modu(Register rd, Register rs, Register rt); 743 void dmod(Register rd, Register rs, Register rt); 744 void dmodu(Register rd, Register rs, Register rt); 745 746 void mul(Register rd, Register rs, Register rt); 747 void muh(Register rd, Register rs, Register rt); 748 void mulu(Register rd, Register rs, Register rt); 749 void muhu(Register rd, Register rs, Register rt); 750 void mult(Register rs, Register rt); 751 void multu(Register rs, Register rt); 752 void dmul(Register rd, Register rs, Register rt); 753 void dmuh(Register rd, Register rs, Register rt); 754 void dmulu(Register rd, Register rs, Register rt); 755 void dmuhu(Register rd, Register rs, Register rt); 756 void daddu(Register rd, Register rs, Register rt); 757 void dsubu(Register rd, Register rs, Register rt); 758 void dmult(Register rs, Register rt); 759 void dmultu(Register rs, Register rt); 760 761 void addiu(Register rd, Register rs, int32_t j); 762 void daddiu(Register rd, Register rs, int32_t j); 763 764 // Logical. 765 void and_(Register rd, Register rs, Register rt); 766 void or_(Register rd, Register rs, Register rt); 767 void xor_(Register rd, Register rs, Register rt); 768 void nor(Register rd, Register rs, Register rt); 769 770 void andi(Register rd, Register rs, int32_t j); 771 void ori(Register rd, Register rs, int32_t j); 772 void xori(Register rd, Register rs, int32_t j); 773 void lui(Register rd, int32_t j); 774 void aui(Register rt, Register rs, int32_t j); 775 void daui(Register rt, Register rs, int32_t j); 776 void dahi(Register rs, int32_t j); 777 void dati(Register rs, int32_t j); 778 779 // Shifts. 780 // Please note: sll(zero_reg, zero_reg, x) instructions are reserved as nop 781 // and may cause problems in normal code. coming_from_nop makes sure this 782 // doesn't happen. 783 void sll(Register rd, Register rt, uint16_t sa, bool coming_from_nop = false); 784 void sllv(Register rd, Register rt, Register rs); 785 void srl(Register rd, Register rt, uint16_t sa); 786 void srlv(Register rd, Register rt, Register rs); 787 void sra(Register rt, Register rd, uint16_t sa); 788 void srav(Register rt, Register rd, Register rs); 789 void rotr(Register rd, Register rt, uint16_t sa); 790 void rotrv(Register rd, Register rt, Register rs); 791 void dsll(Register rd, Register rt, uint16_t sa); 792 void dsllv(Register rd, Register rt, Register rs); 793 void dsrl(Register rd, Register rt, uint16_t sa); 794 void dsrlv(Register rd, Register rt, Register rs); 795 void drotr(Register rd, Register rt, uint16_t sa); 796 void drotr32(Register rd, Register rt, uint16_t sa); 797 void drotrv(Register rd, Register rt, Register rs); 798 void dsra(Register rt, Register rd, uint16_t sa); 799 void dsrav(Register rd, Register rt, Register rs); 800 void dsll32(Register rt, Register rd, uint16_t sa); 801 void dsrl32(Register rt, Register rd, uint16_t sa); 802 void dsra32(Register rt, Register rd, uint16_t sa); 803 804 // ------------Memory-instructions------------- 805 806 void lb(Register rd, const MemOperand& rs); 807 void lbu(Register rd, const MemOperand& rs); 808 void lh(Register rd, const MemOperand& rs); 809 void lhu(Register rd, const MemOperand& rs); 810 void lw(Register rd, const MemOperand& rs); 811 void lwu(Register rd, const MemOperand& rs); 812 void lwl(Register rd, const MemOperand& rs); 813 void lwr(Register rd, const MemOperand& rs); 814 void sb(Register rd, const MemOperand& rs); 815 void sh(Register rd, const MemOperand& rs); 816 void sw(Register rd, const MemOperand& rs); 817 void swl(Register rd, const MemOperand& rs); 818 void swr(Register rd, const MemOperand& rs); 819 void ldl(Register rd, const MemOperand& rs); 820 void ldr(Register rd, const MemOperand& rs); 821 void sdl(Register rd, const MemOperand& rs); 822 void sdr(Register rd, const MemOperand& rs); 823 void ld(Register rd, const MemOperand& rs); 824 void sd(Register rd, const MemOperand& rs); 825 826 827 // ---------PC-Relative-instructions----------- 828 829 void addiupc(Register rs, int32_t imm19); 830 void lwpc(Register rs, int32_t offset19); 831 void lwupc(Register rs, int32_t offset19); 832 void ldpc(Register rs, int32_t offset18); 833 void auipc(Register rs, int16_t imm16); 834 void aluipc(Register rs, int16_t imm16); 835 836 837 // ----------------Prefetch-------------------- 838 839 void pref(int32_t hint, const MemOperand& rs); 840 841 842 // -------------Misc-instructions-------------- 843 844 // Break / Trap instructions. 845 void break_(uint32_t code, bool break_as_stop = false); 846 void stop(const char* msg, uint32_t code = kMaxStopCode); 847 void tge(Register rs, Register rt, uint16_t code); 848 void tgeu(Register rs, Register rt, uint16_t code); 849 void tlt(Register rs, Register rt, uint16_t code); 850 void tltu(Register rs, Register rt, uint16_t code); 851 void teq(Register rs, Register rt, uint16_t code); 852 void tne(Register rs, Register rt, uint16_t code); 853 854 // Memory barrier instruction. 855 void sync(); 856 857 // Move from HI/LO register. 858 void mfhi(Register rd); 859 void mflo(Register rd); 860 861 // Set on less than. 862 void slt(Register rd, Register rs, Register rt); 863 void sltu(Register rd, Register rs, Register rt); 864 void slti(Register rd, Register rs, int32_t j); 865 void sltiu(Register rd, Register rs, int32_t j); 866 867 // Conditional move. 868 void movz(Register rd, Register rs, Register rt); 869 void movn(Register rd, Register rs, Register rt); 870 void movt(Register rd, Register rs, uint16_t cc = 0); 871 void movf(Register rd, Register rs, uint16_t cc = 0); 872 873 void sel(SecondaryField fmt, FPURegister fd, FPURegister fs, FPURegister ft); 874 void sel_s(FPURegister fd, FPURegister fs, FPURegister ft); 875 void sel_d(FPURegister fd, FPURegister fs, FPURegister ft); 876 void seleqz(Register rd, Register rs, Register rt); 877 void seleqz(SecondaryField fmt, FPURegister fd, FPURegister fs, 878 FPURegister ft); 879 void selnez(Register rs, Register rt, Register rd); 880 void selnez(SecondaryField fmt, FPURegister fd, FPURegister fs, 881 FPURegister ft); 882 void seleqz_d(FPURegister fd, FPURegister fs, FPURegister ft); 883 void seleqz_s(FPURegister fd, FPURegister fs, FPURegister ft); 884 void selnez_d(FPURegister fd, FPURegister fs, FPURegister ft); 885 void selnez_s(FPURegister fd, FPURegister fs, FPURegister ft); 886 887 void movz_s(FPURegister fd, FPURegister fs, Register rt); 888 void movz_d(FPURegister fd, FPURegister fs, Register rt); 889 void movt_s(FPURegister fd, FPURegister fs, uint16_t cc = 0); 890 void movt_d(FPURegister fd, FPURegister fs, uint16_t cc = 0); 891 void movf_s(FPURegister fd, FPURegister fs, uint16_t cc = 0); 892 void movf_d(FPURegister fd, FPURegister fs, uint16_t cc = 0); 893 void movn_s(FPURegister fd, FPURegister fs, Register rt); 894 void movn_d(FPURegister fd, FPURegister fs, Register rt); 895 // Bit twiddling. 896 void clz(Register rd, Register rs); 897 void dclz(Register rd, Register rs); 898 void ins_(Register rt, Register rs, uint16_t pos, uint16_t size); 899 void ext_(Register rt, Register rs, uint16_t pos, uint16_t size); 900 void dext_(Register rt, Register rs, uint16_t pos, uint16_t size); 901 void dextm(Register rt, Register rs, uint16_t pos, uint16_t size); 902 void dextu(Register rt, Register rs, uint16_t pos, uint16_t size); 903 void dins_(Register rt, Register rs, uint16_t pos, uint16_t size); 904 void bitswap(Register rd, Register rt); 905 void dbitswap(Register rd, Register rt); 906 void align(Register rd, Register rs, Register rt, uint8_t bp); 907 void dalign(Register rd, Register rs, Register rt, uint8_t bp); 908 909 void wsbh(Register rd, Register rt); 910 void dsbh(Register rd, Register rt); 911 void dshd(Register rd, Register rt); 912 void seh(Register rd, Register rt); 913 void seb(Register rd, Register rt); 914 915 // --------Coprocessor-instructions---------------- 916 917 // Load, store, and move. 918 void lwc1(FPURegister fd, const MemOperand& src); 919 void ldc1(FPURegister fd, const MemOperand& src); 920 921 void swc1(FPURegister fs, const MemOperand& dst); 922 void sdc1(FPURegister fs, const MemOperand& dst); 923 924 void mtc1(Register rt, FPURegister fs); 925 void mthc1(Register rt, FPURegister fs); 926 void dmtc1(Register rt, FPURegister fs); 927 928 void mfc1(Register rt, FPURegister fs); 929 void mfhc1(Register rt, FPURegister fs); 930 void dmfc1(Register rt, FPURegister fs); 931 932 void ctc1(Register rt, FPUControlRegister fs); 933 void cfc1(Register rt, FPUControlRegister fs); 934 935 // Arithmetic. 936 void add_s(FPURegister fd, FPURegister fs, FPURegister ft); 937 void add_d(FPURegister fd, FPURegister fs, FPURegister ft); 938 void sub_s(FPURegister fd, FPURegister fs, FPURegister ft); 939 void sub_d(FPURegister fd, FPURegister fs, FPURegister ft); 940 void mul_s(FPURegister fd, FPURegister fs, FPURegister ft); 941 void mul_d(FPURegister fd, FPURegister fs, FPURegister ft); 942 void madd_s(FPURegister fd, FPURegister fr, FPURegister fs, FPURegister ft); 943 void madd_d(FPURegister fd, FPURegister fr, FPURegister fs, FPURegister ft); 944 void msub_s(FPURegister fd, FPURegister fr, FPURegister fs, FPURegister ft); 945 void msub_d(FPURegister fd, FPURegister fr, FPURegister fs, FPURegister ft); 946 void maddf_s(FPURegister fd, FPURegister fs, FPURegister ft); 947 void maddf_d(FPURegister fd, FPURegister fs, FPURegister ft); 948 void msubf_s(FPURegister fd, FPURegister fs, FPURegister ft); 949 void msubf_d(FPURegister fd, FPURegister fs, FPURegister ft); 950 void div_s(FPURegister fd, FPURegister fs, FPURegister ft); 951 void div_d(FPURegister fd, FPURegister fs, FPURegister ft); 952 void abs_s(FPURegister fd, FPURegister fs); 953 void abs_d(FPURegister fd, FPURegister fs); 954 void mov_d(FPURegister fd, FPURegister fs); 955 void mov_s(FPURegister fd, FPURegister fs); 956 void neg_s(FPURegister fd, FPURegister fs); 957 void neg_d(FPURegister fd, FPURegister fs); 958 void sqrt_s(FPURegister fd, FPURegister fs); 959 void sqrt_d(FPURegister fd, FPURegister fs); 960 void rsqrt_s(FPURegister fd, FPURegister fs); 961 void rsqrt_d(FPURegister fd, FPURegister fs); 962 void recip_d(FPURegister fd, FPURegister fs); 963 void recip_s(FPURegister fd, FPURegister fs); 964 965 // Conversion. 966 void cvt_w_s(FPURegister fd, FPURegister fs); 967 void cvt_w_d(FPURegister fd, FPURegister fs); 968 void trunc_w_s(FPURegister fd, FPURegister fs); 969 void trunc_w_d(FPURegister fd, FPURegister fs); 970 void round_w_s(FPURegister fd, FPURegister fs); 971 void round_w_d(FPURegister fd, FPURegister fs); 972 void floor_w_s(FPURegister fd, FPURegister fs); 973 void floor_w_d(FPURegister fd, FPURegister fs); 974 void ceil_w_s(FPURegister fd, FPURegister fs); 975 void ceil_w_d(FPURegister fd, FPURegister fs); 976 void rint_s(FPURegister fd, FPURegister fs); 977 void rint_d(FPURegister fd, FPURegister fs); 978 void rint(SecondaryField fmt, FPURegister fd, FPURegister fs); 979 980 981 void cvt_l_s(FPURegister fd, FPURegister fs); 982 void cvt_l_d(FPURegister fd, FPURegister fs); 983 void trunc_l_s(FPURegister fd, FPURegister fs); 984 void trunc_l_d(FPURegister fd, FPURegister fs); 985 void round_l_s(FPURegister fd, FPURegister fs); 986 void round_l_d(FPURegister fd, FPURegister fs); 987 void floor_l_s(FPURegister fd, FPURegister fs); 988 void floor_l_d(FPURegister fd, FPURegister fs); 989 void ceil_l_s(FPURegister fd, FPURegister fs); 990 void ceil_l_d(FPURegister fd, FPURegister fs); 991 992 void class_s(FPURegister fd, FPURegister fs); 993 void class_d(FPURegister fd, FPURegister fs); 994 995 void min(SecondaryField fmt, FPURegister fd, FPURegister fs, FPURegister ft); 996 void mina(SecondaryField fmt, FPURegister fd, FPURegister fs, FPURegister ft); 997 void max(SecondaryField fmt, FPURegister fd, FPURegister fs, FPURegister ft); 998 void maxa(SecondaryField fmt, FPURegister fd, FPURegister fs, FPURegister ft); 999 void min_s(FPURegister fd, FPURegister fs, FPURegister ft); 1000 void min_d(FPURegister fd, FPURegister fs, FPURegister ft); 1001 void max_s(FPURegister fd, FPURegister fs, FPURegister ft); 1002 void max_d(FPURegister fd, FPURegister fs, FPURegister ft); 1003 void mina_s(FPURegister fd, FPURegister fs, FPURegister ft); 1004 void mina_d(FPURegister fd, FPURegister fs, FPURegister ft); 1005 void maxa_s(FPURegister fd, FPURegister fs, FPURegister ft); 1006 void maxa_d(FPURegister fd, FPURegister fs, FPURegister ft); 1007 1008 void cvt_s_w(FPURegister fd, FPURegister fs); 1009 void cvt_s_l(FPURegister fd, FPURegister fs); 1010 void cvt_s_d(FPURegister fd, FPURegister fs); 1011 1012 void cvt_d_w(FPURegister fd, FPURegister fs); 1013 void cvt_d_l(FPURegister fd, FPURegister fs); 1014 void cvt_d_s(FPURegister fd, FPURegister fs); 1015 1016 // Conditions and branches for MIPSr6. 1017 void cmp(FPUCondition cond, SecondaryField fmt, 1018 FPURegister fd, FPURegister ft, FPURegister fs); 1019 void cmp_s(FPUCondition cond, FPURegister fd, FPURegister fs, FPURegister ft); 1020 void cmp_d(FPUCondition cond, FPURegister fd, FPURegister fs, FPURegister ft); 1021 1022 void bc1eqz(int16_t offset, FPURegister ft); bc1eqz(Label * L,FPURegister ft)1023 inline void bc1eqz(Label* L, FPURegister ft) { 1024 bc1eqz(shifted_branch_offset(L), ft); 1025 } 1026 void bc1nez(int16_t offset, FPURegister ft); bc1nez(Label * L,FPURegister ft)1027 inline void bc1nez(Label* L, FPURegister ft) { 1028 bc1nez(shifted_branch_offset(L), ft); 1029 } 1030 1031 // Conditions and branches for non MIPSr6. 1032 void c(FPUCondition cond, SecondaryField fmt, 1033 FPURegister ft, FPURegister fs, uint16_t cc = 0); 1034 void c_s(FPUCondition cond, FPURegister ft, FPURegister fs, uint16_t cc = 0); 1035 void c_d(FPUCondition cond, FPURegister ft, FPURegister fs, uint16_t cc = 0); 1036 1037 void bc1f(int16_t offset, uint16_t cc = 0); 1038 inline void bc1f(Label* L, uint16_t cc = 0) { 1039 bc1f(shifted_branch_offset(L), cc); 1040 } 1041 void bc1t(int16_t offset, uint16_t cc = 0); 1042 inline void bc1t(Label* L, uint16_t cc = 0) { 1043 bc1t(shifted_branch_offset(L), cc); 1044 } 1045 void fcmp(FPURegister src1, const double src2, FPUCondition cond); 1046 1047 // Check the code size generated from label to here. SizeOfCodeGeneratedSince(Label * label)1048 int SizeOfCodeGeneratedSince(Label* label) { 1049 return pc_offset() - label->pos(); 1050 } 1051 1052 // Check the number of instructions generated from label to here. InstructionsGeneratedSince(Label * label)1053 int InstructionsGeneratedSince(Label* label) { 1054 return SizeOfCodeGeneratedSince(label) / kInstrSize; 1055 } 1056 1057 // Class for scoping postponing the trampoline pool generation. 1058 class BlockTrampolinePoolScope { 1059 public: BlockTrampolinePoolScope(Assembler * assem)1060 explicit BlockTrampolinePoolScope(Assembler* assem) : assem_(assem) { 1061 assem_->StartBlockTrampolinePool(); 1062 } ~BlockTrampolinePoolScope()1063 ~BlockTrampolinePoolScope() { 1064 assem_->EndBlockTrampolinePool(); 1065 } 1066 1067 private: 1068 Assembler* assem_; 1069 1070 DISALLOW_IMPLICIT_CONSTRUCTORS(BlockTrampolinePoolScope); 1071 }; 1072 1073 // Class for postponing the assembly buffer growth. Typically used for 1074 // sequences of instructions that must be emitted as a unit, before 1075 // buffer growth (and relocation) can occur. 1076 // This blocking scope is not nestable. 1077 class BlockGrowBufferScope { 1078 public: BlockGrowBufferScope(Assembler * assem)1079 explicit BlockGrowBufferScope(Assembler* assem) : assem_(assem) { 1080 assem_->StartBlockGrowBuffer(); 1081 } ~BlockGrowBufferScope()1082 ~BlockGrowBufferScope() { 1083 assem_->EndBlockGrowBuffer(); 1084 } 1085 1086 private: 1087 Assembler* assem_; 1088 1089 DISALLOW_IMPLICIT_CONSTRUCTORS(BlockGrowBufferScope); 1090 }; 1091 1092 // Debugging. 1093 1094 // Mark generator continuation. 1095 void RecordGeneratorContinuation(); 1096 1097 // Mark address of a debug break slot. 1098 void RecordDebugBreakSlot(RelocInfo::Mode mode); 1099 1100 // Record the AST id of the CallIC being compiled, so that it can be placed 1101 // in the relocation information. SetRecordedAstId(TypeFeedbackId ast_id)1102 void SetRecordedAstId(TypeFeedbackId ast_id) { 1103 DCHECK(recorded_ast_id_.IsNone()); 1104 recorded_ast_id_ = ast_id; 1105 } 1106 RecordedAstId()1107 TypeFeedbackId RecordedAstId() { 1108 DCHECK(!recorded_ast_id_.IsNone()); 1109 return recorded_ast_id_; 1110 } 1111 ClearRecordedAstId()1112 void ClearRecordedAstId() { recorded_ast_id_ = TypeFeedbackId::None(); } 1113 1114 // Record a comment relocation entry that can be used by a disassembler. 1115 // Use --code-comments to enable. 1116 void RecordComment(const char* msg); 1117 1118 // Record a deoptimization reason that can be used by a log or cpu profiler. 1119 // Use --trace-deopt to enable. 1120 void RecordDeoptReason(DeoptimizeReason reason, SourcePosition position, 1121 int id); 1122 1123 static int RelocateInternalReference(RelocInfo::Mode rmode, byte* pc, 1124 intptr_t pc_delta); 1125 1126 // Writes a single byte or word of data in the code stream. Used for 1127 // inline tables, e.g., jump-tables. 1128 void db(uint8_t data); 1129 void dd(uint32_t data); 1130 void dq(uint64_t data); dp(uintptr_t data)1131 void dp(uintptr_t data) { dq(data); } 1132 void dd(Label* label); 1133 1134 // Postpone the generation of the trampoline pool for the specified number of 1135 // instructions. 1136 void BlockTrampolinePoolFor(int instructions); 1137 1138 // Check if there is less than kGap bytes available in the buffer. 1139 // If this is the case, we need to grow the buffer before emitting 1140 // an instruction or relocation information. overflow()1141 inline bool overflow() const { return pc_ >= reloc_info_writer.pos() - kGap; } 1142 1143 // Get the number of bytes available in the buffer. available_space()1144 inline intptr_t available_space() const { 1145 return reloc_info_writer.pos() - pc_; 1146 } 1147 1148 // Read/patch instructions. instr_at(byte * pc)1149 static Instr instr_at(byte* pc) { return *reinterpret_cast<Instr*>(pc); } instr_at_put(byte * pc,Instr instr)1150 static void instr_at_put(byte* pc, Instr instr) { 1151 *reinterpret_cast<Instr*>(pc) = instr; 1152 } instr_at(int pos)1153 Instr instr_at(int pos) { return *reinterpret_cast<Instr*>(buffer_ + pos); } instr_at_put(int pos,Instr instr)1154 void instr_at_put(int pos, Instr instr) { 1155 *reinterpret_cast<Instr*>(buffer_ + pos) = instr; 1156 } 1157 1158 // Check if an instruction is a branch of some kind. 1159 static bool IsBranch(Instr instr); 1160 static bool IsBc(Instr instr); 1161 static bool IsBzc(Instr instr); 1162 1163 static bool IsBeq(Instr instr); 1164 static bool IsBne(Instr instr); 1165 static bool IsBeqzc(Instr instr); 1166 static bool IsBnezc(Instr instr); 1167 static bool IsBeqc(Instr instr); 1168 static bool IsBnec(Instr instr); 1169 1170 1171 static bool IsJump(Instr instr); 1172 static bool IsJ(Instr instr); 1173 static bool IsLui(Instr instr); 1174 static bool IsOri(Instr instr); 1175 1176 static bool IsJal(Instr instr); 1177 static bool IsJr(Instr instr); 1178 static bool IsJalr(Instr instr); 1179 1180 static bool IsNop(Instr instr, unsigned int type); 1181 static bool IsPop(Instr instr); 1182 static bool IsPush(Instr instr); 1183 static bool IsLwRegFpOffset(Instr instr); 1184 static bool IsSwRegFpOffset(Instr instr); 1185 static bool IsLwRegFpNegOffset(Instr instr); 1186 static bool IsSwRegFpNegOffset(Instr instr); 1187 1188 static Register GetRtReg(Instr instr); 1189 static Register GetRsReg(Instr instr); 1190 static Register GetRdReg(Instr instr); 1191 1192 static uint32_t GetRt(Instr instr); 1193 static uint32_t GetRtField(Instr instr); 1194 static uint32_t GetRs(Instr instr); 1195 static uint32_t GetRsField(Instr instr); 1196 static uint32_t GetRd(Instr instr); 1197 static uint32_t GetRdField(Instr instr); 1198 static uint32_t GetSa(Instr instr); 1199 static uint32_t GetSaField(Instr instr); 1200 static uint32_t GetOpcodeField(Instr instr); 1201 static uint32_t GetFunction(Instr instr); 1202 static uint32_t GetFunctionField(Instr instr); 1203 static uint32_t GetImmediate16(Instr instr); 1204 static uint32_t GetLabelConst(Instr instr); 1205 1206 static int32_t GetBranchOffset(Instr instr); 1207 static bool IsLw(Instr instr); 1208 static int16_t GetLwOffset(Instr instr); 1209 static Instr SetLwOffset(Instr instr, int16_t offset); 1210 1211 static bool IsSw(Instr instr); 1212 static Instr SetSwOffset(Instr instr, int16_t offset); 1213 static bool IsAddImmediate(Instr instr); 1214 static Instr SetAddImmediateOffset(Instr instr, int16_t offset); 1215 1216 static bool IsAndImmediate(Instr instr); 1217 static bool IsEmittedConstant(Instr instr); 1218 1219 void CheckTrampolinePool(); 1220 PatchConstantPoolAccessInstruction(int pc_offset,int offset,ConstantPoolEntry::Access access,ConstantPoolEntry::Type type)1221 void PatchConstantPoolAccessInstruction(int pc_offset, int offset, 1222 ConstantPoolEntry::Access access, 1223 ConstantPoolEntry::Type type) { 1224 // No embedded constant pool support. 1225 UNREACHABLE(); 1226 } 1227 IsPrevInstrCompactBranch()1228 bool IsPrevInstrCompactBranch() { return prev_instr_compact_branch_; } 1229 UnboundLabelsCount()1230 inline int UnboundLabelsCount() { return unbound_labels_count_; } 1231 1232 protected: 1233 // Load Scaled Address instructions. 1234 void lsa(Register rd, Register rt, Register rs, uint8_t sa); 1235 void dlsa(Register rd, Register rt, Register rs, uint8_t sa); 1236 1237 // Helpers. 1238 void LoadRegPlusOffsetToAt(const MemOperand& src); 1239 1240 // Relocation for a type-recording IC has the AST id added to it. This 1241 // member variable is a way to pass the information from the call site to 1242 // the relocation info. 1243 TypeFeedbackId recorded_ast_id_; 1244 1245 inline static void set_target_internal_reference_encoded_at(Address pc, 1246 Address target); 1247 buffer_space()1248 int64_t buffer_space() const { return reloc_info_writer.pos() - pc_; } 1249 1250 // Decode branch instruction at pos and return branch target pos. 1251 int target_at(int pos, bool is_internal); 1252 1253 // Patch branch instruction at pos to branch to given branch target pos. 1254 void target_at_put(int pos, int target_pos, bool is_internal); 1255 1256 // Say if we need to relocate with this mode. 1257 bool MustUseReg(RelocInfo::Mode rmode); 1258 1259 // Record reloc info for current pc_. 1260 void RecordRelocInfo(RelocInfo::Mode rmode, intptr_t data = 0); 1261 1262 // Block the emission of the trampoline pool before pc_offset. BlockTrampolinePoolBefore(int pc_offset)1263 void BlockTrampolinePoolBefore(int pc_offset) { 1264 if (no_trampoline_pool_before_ < pc_offset) 1265 no_trampoline_pool_before_ = pc_offset; 1266 } 1267 StartBlockTrampolinePool()1268 void StartBlockTrampolinePool() { 1269 trampoline_pool_blocked_nesting_++; 1270 } 1271 EndBlockTrampolinePool()1272 void EndBlockTrampolinePool() { 1273 trampoline_pool_blocked_nesting_--; 1274 } 1275 is_trampoline_pool_blocked()1276 bool is_trampoline_pool_blocked() const { 1277 return trampoline_pool_blocked_nesting_ > 0; 1278 } 1279 has_exception()1280 bool has_exception() const { 1281 return internal_trampoline_exception_; 1282 } 1283 1284 void DoubleAsTwoUInt32(double d, uint32_t* lo, uint32_t* hi); 1285 is_trampoline_emitted()1286 bool is_trampoline_emitted() const { 1287 return trampoline_emitted_; 1288 } 1289 1290 // Temporarily block automatic assembly buffer growth. StartBlockGrowBuffer()1291 void StartBlockGrowBuffer() { 1292 DCHECK(!block_buffer_growth_); 1293 block_buffer_growth_ = true; 1294 } 1295 EndBlockGrowBuffer()1296 void EndBlockGrowBuffer() { 1297 DCHECK(block_buffer_growth_); 1298 block_buffer_growth_ = false; 1299 } 1300 is_buffer_growth_blocked()1301 bool is_buffer_growth_blocked() const { 1302 return block_buffer_growth_; 1303 } 1304 EmitForbiddenSlotInstruction()1305 void EmitForbiddenSlotInstruction() { 1306 if (IsPrevInstrCompactBranch()) { 1307 nop(); 1308 } 1309 } 1310 1311 inline void CheckTrampolinePoolQuick(int extra_instructions = 0); 1312 1313 private: 1314 // Buffer size and constant pool distance are checked together at regular 1315 // intervals of kBufferCheckInterval emitted bytes. 1316 static const int kBufferCheckInterval = 1*KB/2; 1317 1318 // Code generation. 1319 // The relocation writer's position is at least kGap bytes below the end of 1320 // the generated instructions. This is so that multi-instruction sequences do 1321 // not have to check for overflow. The same is true for writes of large 1322 // relocation info entries. 1323 static const int kGap = 32; 1324 1325 1326 // Repeated checking whether the trampoline pool should be emitted is rather 1327 // expensive. By default we only check again once a number of instructions 1328 // has been generated. 1329 static const int kCheckConstIntervalInst = 32; 1330 static const int kCheckConstInterval = kCheckConstIntervalInst * kInstrSize; 1331 1332 int next_buffer_check_; // pc offset of next buffer check. 1333 1334 // Emission of the trampoline pool may be blocked in some code sequences. 1335 int trampoline_pool_blocked_nesting_; // Block emission if this is not zero. 1336 int no_trampoline_pool_before_; // Block emission before this pc offset. 1337 1338 // Keep track of the last emitted pool to guarantee a maximal distance. 1339 int last_trampoline_pool_end_; // pc offset of the end of the last pool. 1340 1341 // Automatic growth of the assembly buffer may be blocked for some sequences. 1342 bool block_buffer_growth_; // Block growth when true. 1343 1344 // Relocation information generation. 1345 // Each relocation is encoded as a variable size value. 1346 static const int kMaxRelocSize = RelocInfoWriter::kMaxSize; 1347 RelocInfoWriter reloc_info_writer; 1348 1349 // The bound position, before this we cannot do instruction elimination. 1350 int last_bound_pos_; 1351 1352 // Readable constants for compact branch handling in emit() 1353 enum class CompactBranchType : bool { NO = false, COMPACT_BRANCH = true }; 1354 1355 // Code emission. 1356 inline void CheckBuffer(); 1357 void GrowBuffer(); 1358 inline void emit(Instr x, 1359 CompactBranchType is_compact_branch = CompactBranchType::NO); 1360 inline void emit(uint64_t x); 1361 inline void CheckForEmitInForbiddenSlot(); 1362 template <typename T> 1363 inline void EmitHelper(T x); 1364 inline void EmitHelper(Instr x, CompactBranchType is_compact_branch); 1365 1366 // Instruction generation. 1367 // We have 3 different kind of encoding layout on MIPS. 1368 // However due to many different types of objects encoded in the same fields 1369 // we have quite a few aliases for each mode. 1370 // Using the same structure to refer to Register and FPURegister would spare a 1371 // few aliases, but mixing both does not look clean to me. 1372 // Anyway we could surely implement this differently. 1373 1374 void GenInstrRegister(Opcode opcode, 1375 Register rs, 1376 Register rt, 1377 Register rd, 1378 uint16_t sa = 0, 1379 SecondaryField func = NULLSF); 1380 1381 void GenInstrRegister(Opcode opcode, 1382 Register rs, 1383 Register rt, 1384 uint16_t msb, 1385 uint16_t lsb, 1386 SecondaryField func); 1387 1388 void GenInstrRegister(Opcode opcode, 1389 SecondaryField fmt, 1390 FPURegister ft, 1391 FPURegister fs, 1392 FPURegister fd, 1393 SecondaryField func = NULLSF); 1394 1395 void GenInstrRegister(Opcode opcode, 1396 FPURegister fr, 1397 FPURegister ft, 1398 FPURegister fs, 1399 FPURegister fd, 1400 SecondaryField func = NULLSF); 1401 1402 void GenInstrRegister(Opcode opcode, 1403 SecondaryField fmt, 1404 Register rt, 1405 FPURegister fs, 1406 FPURegister fd, 1407 SecondaryField func = NULLSF); 1408 1409 void GenInstrRegister(Opcode opcode, 1410 SecondaryField fmt, 1411 Register rt, 1412 FPUControlRegister fs, 1413 SecondaryField func = NULLSF); 1414 1415 1416 void GenInstrImmediate( 1417 Opcode opcode, Register rs, Register rt, int32_t j, 1418 CompactBranchType is_compact_branch = CompactBranchType::NO); 1419 void GenInstrImmediate( 1420 Opcode opcode, Register rs, SecondaryField SF, int32_t j, 1421 CompactBranchType is_compact_branch = CompactBranchType::NO); 1422 void GenInstrImmediate( 1423 Opcode opcode, Register r1, FPURegister r2, int32_t j, 1424 CompactBranchType is_compact_branch = CompactBranchType::NO); 1425 void GenInstrImmediate( 1426 Opcode opcode, Register rs, int32_t offset21, 1427 CompactBranchType is_compact_branch = CompactBranchType::NO); 1428 void GenInstrImmediate(Opcode opcode, Register rs, uint32_t offset21); 1429 void GenInstrImmediate( 1430 Opcode opcode, int32_t offset26, 1431 CompactBranchType is_compact_branch = CompactBranchType::NO); 1432 1433 void GenInstrJump(Opcode opcode, 1434 uint32_t address); 1435 1436 // Labels. 1437 void print(Label* L); 1438 void bind_to(Label* L, int pos); 1439 void next(Label* L, bool is_internal); 1440 1441 // One trampoline consists of: 1442 // - space for trampoline slots, 1443 // - space for labels. 1444 // 1445 // Space for trampoline slots is equal to slot_count * 2 * kInstrSize. 1446 // Space for trampoline slots preceeds space for labels. Each label is of one 1447 // instruction size, so total amount for labels is equal to 1448 // label_count * kInstrSize. 1449 class Trampoline { 1450 public: Trampoline()1451 Trampoline() { 1452 start_ = 0; 1453 next_slot_ = 0; 1454 free_slot_count_ = 0; 1455 end_ = 0; 1456 } Trampoline(int start,int slot_count)1457 Trampoline(int start, int slot_count) { 1458 start_ = start; 1459 next_slot_ = start; 1460 free_slot_count_ = slot_count; 1461 end_ = start + slot_count * kTrampolineSlotsSize; 1462 } start()1463 int start() { 1464 return start_; 1465 } end()1466 int end() { 1467 return end_; 1468 } take_slot()1469 int take_slot() { 1470 int trampoline_slot = kInvalidSlotPos; 1471 if (free_slot_count_ <= 0) { 1472 // We have run out of space on trampolines. 1473 // Make sure we fail in debug mode, so we become aware of each case 1474 // when this happens. 1475 DCHECK(0); 1476 // Internal exception will be caught. 1477 } else { 1478 trampoline_slot = next_slot_; 1479 free_slot_count_--; 1480 next_slot_ += kTrampolineSlotsSize; 1481 } 1482 return trampoline_slot; 1483 } 1484 1485 private: 1486 int start_; 1487 int end_; 1488 int next_slot_; 1489 int free_slot_count_; 1490 }; 1491 1492 int32_t get_trampoline_entry(int32_t pos); 1493 int unbound_labels_count_; 1494 // After trampoline is emitted, long branches are used in generated code for 1495 // the forward branches whose target offsets could be beyond reach of branch 1496 // instruction. We use this information to trigger different mode of 1497 // branch instruction generation, where we use jump instructions rather 1498 // than regular branch instructions. 1499 bool trampoline_emitted_; 1500 static const int kTrampolineSlotsSize = 2 * kInstrSize; 1501 static const int kMaxBranchOffset = (1 << (18 - 1)) - 1; 1502 static const int kMaxCompactBranchOffset = (1 << (28 - 1)) - 1; 1503 static const int kInvalidSlotPos = -1; 1504 1505 // Internal reference positions, required for unbounded internal reference 1506 // labels. 1507 std::set<int64_t> internal_reference_positions_; 1508 EmittedCompactBranchInstruction()1509 void EmittedCompactBranchInstruction() { prev_instr_compact_branch_ = true; } ClearCompactBranchState()1510 void ClearCompactBranchState() { prev_instr_compact_branch_ = false; } 1511 bool prev_instr_compact_branch_ = false; 1512 1513 Trampoline trampoline_; 1514 bool internal_trampoline_exception_; 1515 1516 friend class RegExpMacroAssemblerMIPS; 1517 friend class RelocInfo; 1518 friend class CodePatcher; 1519 friend class BlockTrampolinePoolScope; 1520 friend class EnsureSpace; 1521 }; 1522 1523 1524 class EnsureSpace BASE_EMBEDDED { 1525 public: EnsureSpace(Assembler * assembler)1526 explicit EnsureSpace(Assembler* assembler) { 1527 assembler->CheckBuffer(); 1528 } 1529 }; 1530 1531 } // namespace internal 1532 } // namespace v8 1533 1534 #endif // V8_ARM_ASSEMBLER_MIPS_H_ 1535