Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1 | // Copyright 2013 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. |
| 4 | |
| 5 | #if V8_TARGET_ARCH_X64 |
| 6 | |
| 7 | #include "src/crankshaft/x64/lithium-codegen-x64.h" |
| 8 | |
| 9 | #include "src/base/bits.h" |
| 10 | #include "src/code-factory.h" |
| 11 | #include "src/code-stubs.h" |
| 12 | #include "src/crankshaft/hydrogen-osr.h" |
| 13 | #include "src/ic/ic.h" |
| 14 | #include "src/ic/stub-cache.h" |
| 15 | #include "src/profiler/cpu-profiler.h" |
| 16 | |
| 17 | namespace v8 { |
| 18 | namespace internal { |
| 19 | |
| 20 | |
| 21 | // When invoking builtins, we need to record the safepoint in the middle of |
| 22 | // the invoke instruction sequence generated by the macro assembler. |
| 23 | class SafepointGenerator final : public CallWrapper { |
| 24 | public: |
| 25 | SafepointGenerator(LCodeGen* codegen, |
| 26 | LPointerMap* pointers, |
| 27 | Safepoint::DeoptMode mode) |
| 28 | : codegen_(codegen), |
| 29 | pointers_(pointers), |
| 30 | deopt_mode_(mode) { } |
| 31 | virtual ~SafepointGenerator() {} |
| 32 | |
| 33 | void BeforeCall(int call_size) const override {} |
| 34 | |
| 35 | void AfterCall() const override { |
| 36 | codegen_->RecordSafepoint(pointers_, deopt_mode_); |
| 37 | } |
| 38 | |
| 39 | private: |
| 40 | LCodeGen* codegen_; |
| 41 | LPointerMap* pointers_; |
| 42 | Safepoint::DeoptMode deopt_mode_; |
| 43 | }; |
| 44 | |
| 45 | |
| 46 | #define __ masm()-> |
| 47 | |
| 48 | bool LCodeGen::GenerateCode() { |
| 49 | LPhase phase("Z_Code generation", chunk()); |
| 50 | DCHECK(is_unused()); |
| 51 | status_ = GENERATING; |
| 52 | |
| 53 | // Open a frame scope to indicate that there is a frame on the stack. The |
| 54 | // MANUAL indicates that the scope shouldn't actually generate code to set up |
| 55 | // the frame (that is done in GeneratePrologue). |
| 56 | FrameScope frame_scope(masm_, StackFrame::MANUAL); |
| 57 | |
| 58 | return GeneratePrologue() && |
| 59 | GenerateBody() && |
| 60 | GenerateDeferredCode() && |
| 61 | GenerateJumpTable() && |
| 62 | GenerateSafepointTable(); |
| 63 | } |
| 64 | |
| 65 | |
| 66 | void LCodeGen::FinishCode(Handle<Code> code) { |
| 67 | DCHECK(is_done()); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 68 | code->set_stack_slots(GetTotalFrameSlotCount()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 69 | code->set_safepoint_table_offset(safepoints_.GetCodeOffset()); |
| 70 | PopulateDeoptimizationData(code); |
| 71 | } |
| 72 | |
| 73 | |
| 74 | #ifdef _MSC_VER |
| 75 | void LCodeGen::MakeSureStackPagesMapped(int offset) { |
| 76 | const int kPageSize = 4 * KB; |
| 77 | for (offset -= kPageSize; offset > 0; offset -= kPageSize) { |
| 78 | __ movp(Operand(rsp, offset), rax); |
| 79 | } |
| 80 | } |
| 81 | #endif |
| 82 | |
| 83 | |
| 84 | void LCodeGen::SaveCallerDoubles() { |
| 85 | DCHECK(info()->saves_caller_doubles()); |
| 86 | DCHECK(NeedsEagerFrame()); |
| 87 | Comment(";;; Save clobbered callee double registers"); |
| 88 | int count = 0; |
| 89 | BitVector* doubles = chunk()->allocated_double_registers(); |
| 90 | BitVector::Iterator save_iterator(doubles); |
| 91 | while (!save_iterator.Done()) { |
| 92 | __ Movsd(MemOperand(rsp, count * kDoubleSize), |
| 93 | XMMRegister::from_code(save_iterator.Current())); |
| 94 | save_iterator.Advance(); |
| 95 | count++; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | |
| 100 | void LCodeGen::RestoreCallerDoubles() { |
| 101 | DCHECK(info()->saves_caller_doubles()); |
| 102 | DCHECK(NeedsEagerFrame()); |
| 103 | Comment(";;; Restore clobbered callee double registers"); |
| 104 | BitVector* doubles = chunk()->allocated_double_registers(); |
| 105 | BitVector::Iterator save_iterator(doubles); |
| 106 | int count = 0; |
| 107 | while (!save_iterator.Done()) { |
| 108 | __ Movsd(XMMRegister::from_code(save_iterator.Current()), |
| 109 | MemOperand(rsp, count * kDoubleSize)); |
| 110 | save_iterator.Advance(); |
| 111 | count++; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | |
| 116 | bool LCodeGen::GeneratePrologue() { |
| 117 | DCHECK(is_generating()); |
| 118 | |
| 119 | if (info()->IsOptimizing()) { |
| 120 | ProfileEntryHookStub::MaybeCallEntryHook(masm_); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | info()->set_prologue_offset(masm_->pc_offset()); |
| 124 | if (NeedsEagerFrame()) { |
| 125 | DCHECK(!frame_is_built_); |
| 126 | frame_is_built_ = true; |
| 127 | if (info()->IsStub()) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 128 | __ StubPrologue(StackFrame::STUB); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 129 | } else { |
| 130 | __ Prologue(info()->GeneratePreagedPrologue()); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // Reserve space for the stack slots needed by the code. |
| 135 | int slots = GetStackSlotCount(); |
| 136 | if (slots > 0) { |
| 137 | if (FLAG_debug_code) { |
| 138 | __ subp(rsp, Immediate(slots * kPointerSize)); |
| 139 | #ifdef _MSC_VER |
| 140 | MakeSureStackPagesMapped(slots * kPointerSize); |
| 141 | #endif |
| 142 | __ Push(rax); |
| 143 | __ Set(rax, slots); |
| 144 | __ Set(kScratchRegister, kSlotsZapValue); |
| 145 | Label loop; |
| 146 | __ bind(&loop); |
| 147 | __ movp(MemOperand(rsp, rax, times_pointer_size, 0), |
| 148 | kScratchRegister); |
| 149 | __ decl(rax); |
| 150 | __ j(not_zero, &loop); |
| 151 | __ Pop(rax); |
| 152 | } else { |
| 153 | __ subp(rsp, Immediate(slots * kPointerSize)); |
| 154 | #ifdef _MSC_VER |
| 155 | MakeSureStackPagesMapped(slots * kPointerSize); |
| 156 | #endif |
| 157 | } |
| 158 | |
| 159 | if (info()->saves_caller_doubles()) { |
| 160 | SaveCallerDoubles(); |
| 161 | } |
| 162 | } |
| 163 | return !is_aborted(); |
| 164 | } |
| 165 | |
| 166 | |
| 167 | void LCodeGen::DoPrologue(LPrologue* instr) { |
| 168 | Comment(";;; Prologue begin"); |
| 169 | |
| 170 | // Possibly allocate a local context. |
| 171 | if (info_->num_heap_slots() > 0) { |
| 172 | Comment(";;; Allocate local context"); |
| 173 | bool need_write_barrier = true; |
| 174 | // Argument to NewContext is the function, which is still in rdi. |
| 175 | int slots = info_->num_heap_slots() - Context::MIN_CONTEXT_SLOTS; |
| 176 | Safepoint::DeoptMode deopt_mode = Safepoint::kNoLazyDeopt; |
| 177 | if (info()->scope()->is_script_scope()) { |
| 178 | __ Push(rdi); |
| 179 | __ Push(info()->scope()->GetScopeInfo(info()->isolate())); |
| 180 | __ CallRuntime(Runtime::kNewScriptContext); |
| 181 | deopt_mode = Safepoint::kLazyDeopt; |
| 182 | } else if (slots <= FastNewContextStub::kMaximumSlots) { |
| 183 | FastNewContextStub stub(isolate(), slots); |
| 184 | __ CallStub(&stub); |
| 185 | // Result of FastNewContextStub is always in new space. |
| 186 | need_write_barrier = false; |
| 187 | } else { |
| 188 | __ Push(rdi); |
| 189 | __ CallRuntime(Runtime::kNewFunctionContext); |
| 190 | } |
| 191 | RecordSafepoint(deopt_mode); |
| 192 | |
| 193 | // Context is returned in rax. It replaces the context passed to us. |
| 194 | // It's saved in the stack and kept live in rsi. |
| 195 | __ movp(rsi, rax); |
| 196 | __ movp(Operand(rbp, StandardFrameConstants::kContextOffset), rax); |
| 197 | |
| 198 | // Copy any necessary parameters into the context. |
| 199 | int num_parameters = scope()->num_parameters(); |
| 200 | int first_parameter = scope()->has_this_declaration() ? -1 : 0; |
| 201 | for (int i = first_parameter; i < num_parameters; i++) { |
| 202 | Variable* var = (i == -1) ? scope()->receiver() : scope()->parameter(i); |
| 203 | if (var->IsContextSlot()) { |
| 204 | int parameter_offset = StandardFrameConstants::kCallerSPOffset + |
| 205 | (num_parameters - 1 - i) * kPointerSize; |
| 206 | // Load parameter from stack. |
| 207 | __ movp(rax, Operand(rbp, parameter_offset)); |
| 208 | // Store it in the context. |
| 209 | int context_offset = Context::SlotOffset(var->index()); |
| 210 | __ movp(Operand(rsi, context_offset), rax); |
| 211 | // Update the write barrier. This clobbers rax and rbx. |
| 212 | if (need_write_barrier) { |
| 213 | __ RecordWriteContextSlot(rsi, context_offset, rax, rbx, kSaveFPRegs); |
| 214 | } else if (FLAG_debug_code) { |
| 215 | Label done; |
| 216 | __ JumpIfInNewSpace(rsi, rax, &done, Label::kNear); |
| 217 | __ Abort(kExpectedNewSpaceObject); |
| 218 | __ bind(&done); |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | Comment(";;; End allocate local context"); |
| 223 | } |
| 224 | |
| 225 | Comment(";;; Prologue end"); |
| 226 | } |
| 227 | |
| 228 | |
| 229 | void LCodeGen::GenerateOsrPrologue() { |
| 230 | // Generate the OSR entry prologue at the first unknown OSR value, or if there |
| 231 | // are none, at the OSR entrypoint instruction. |
| 232 | if (osr_pc_offset_ >= 0) return; |
| 233 | |
| 234 | osr_pc_offset_ = masm()->pc_offset(); |
| 235 | |
| 236 | // Adjust the frame size, subsuming the unoptimized frame into the |
| 237 | // optimized frame. |
| 238 | int slots = GetStackSlotCount() - graph()->osr()->UnoptimizedFrameSlots(); |
| 239 | DCHECK(slots >= 0); |
| 240 | __ subp(rsp, Immediate(slots * kPointerSize)); |
| 241 | } |
| 242 | |
| 243 | |
| 244 | void LCodeGen::GenerateBodyInstructionPre(LInstruction* instr) { |
| 245 | if (instr->IsCall()) { |
| 246 | EnsureSpaceForLazyDeopt(Deoptimizer::patch_size()); |
| 247 | } |
| 248 | if (!instr->IsLazyBailout() && !instr->IsGap()) { |
| 249 | safepoints_.BumpLastLazySafepointIndex(); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | |
| 254 | void LCodeGen::GenerateBodyInstructionPost(LInstruction* instr) { |
| 255 | if (FLAG_debug_code && FLAG_enable_slow_asserts && instr->HasResult() && |
| 256 | instr->hydrogen_value()->representation().IsInteger32() && |
| 257 | instr->result()->IsRegister()) { |
| 258 | __ AssertZeroExtended(ToRegister(instr->result())); |
| 259 | } |
| 260 | |
| 261 | if (instr->HasResult() && instr->MustSignExtendResult(chunk())) { |
| 262 | // We sign extend the dehoisted key at the definition point when the pointer |
| 263 | // size is 64-bit. For x32 port, we sign extend the dehoisted key at the use |
| 264 | // points and MustSignExtendResult is always false. We can't use |
| 265 | // STATIC_ASSERT here as the pointer size is 32-bit for x32. |
| 266 | DCHECK(kPointerSize == kInt64Size); |
| 267 | if (instr->result()->IsRegister()) { |
| 268 | Register result_reg = ToRegister(instr->result()); |
| 269 | __ movsxlq(result_reg, result_reg); |
| 270 | } else { |
| 271 | // Sign extend the 32bit result in the stack slots. |
| 272 | DCHECK(instr->result()->IsStackSlot()); |
| 273 | Operand src = ToOperand(instr->result()); |
| 274 | __ movsxlq(kScratchRegister, src); |
| 275 | __ movq(src, kScratchRegister); |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | |
| 281 | bool LCodeGen::GenerateJumpTable() { |
| 282 | if (jump_table_.length() == 0) return !is_aborted(); |
| 283 | |
| 284 | Label needs_frame; |
| 285 | Comment(";;; -------------------- Jump table --------------------"); |
| 286 | for (int i = 0; i < jump_table_.length(); i++) { |
| 287 | Deoptimizer::JumpTableEntry* table_entry = &jump_table_[i]; |
| 288 | __ bind(&table_entry->label); |
| 289 | Address entry = table_entry->address; |
| 290 | DeoptComment(table_entry->deopt_info); |
| 291 | if (table_entry->needs_frame) { |
| 292 | DCHECK(!info()->saves_caller_doubles()); |
| 293 | __ Move(kScratchRegister, ExternalReference::ForDeoptEntry(entry)); |
| 294 | __ call(&needs_frame); |
| 295 | } else { |
| 296 | if (info()->saves_caller_doubles()) { |
| 297 | DCHECK(info()->IsStub()); |
| 298 | RestoreCallerDoubles(); |
| 299 | } |
| 300 | __ call(entry, RelocInfo::RUNTIME_ENTRY); |
| 301 | } |
| 302 | info()->LogDeoptCallPosition(masm()->pc_offset(), |
| 303 | table_entry->deopt_info.inlining_id); |
| 304 | } |
| 305 | |
| 306 | if (needs_frame.is_linked()) { |
| 307 | __ bind(&needs_frame); |
| 308 | /* stack layout |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 309 | 3: return address <-- rsp |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 310 | 2: garbage |
| 311 | 1: garbage |
| 312 | 0: garbage |
| 313 | */ |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 314 | // Reserve space for stub marker. |
| 315 | __ subp(rsp, Immediate(TypedFrameConstants::kFrameTypeSize)); |
| 316 | __ Push(MemOperand( |
| 317 | rsp, TypedFrameConstants::kFrameTypeSize)); // Copy return address. |
| 318 | __ Push(kScratchRegister); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 319 | |
| 320 | /* stack layout |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 321 | 3: return address |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 322 | 2: garbage |
| 323 | 1: return address |
| 324 | 0: entry address <-- rsp |
| 325 | */ |
| 326 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 327 | // Create a stack frame. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 328 | __ movp(MemOperand(rsp, 3 * kPointerSize), rbp); |
| 329 | __ leap(rbp, MemOperand(rsp, 3 * kPointerSize)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 330 | |
| 331 | // This variant of deopt can only be used with stubs. Since we don't |
| 332 | // have a function pointer to install in the stack frame that we're |
| 333 | // building, install a special marker there instead. |
| 334 | DCHECK(info()->IsStub()); |
| 335 | __ Move(MemOperand(rsp, 2 * kPointerSize), Smi::FromInt(StackFrame::STUB)); |
| 336 | |
| 337 | /* stack layout |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 338 | 3: old rbp |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 339 | 2: stub marker |
| 340 | 1: return address |
| 341 | 0: entry address <-- rsp |
| 342 | */ |
| 343 | __ ret(0); |
| 344 | } |
| 345 | |
| 346 | return !is_aborted(); |
| 347 | } |
| 348 | |
| 349 | |
| 350 | bool LCodeGen::GenerateDeferredCode() { |
| 351 | DCHECK(is_generating()); |
| 352 | if (deferred_.length() > 0) { |
| 353 | for (int i = 0; !is_aborted() && i < deferred_.length(); i++) { |
| 354 | LDeferredCode* code = deferred_[i]; |
| 355 | |
| 356 | HValue* value = |
| 357 | instructions_->at(code->instruction_index())->hydrogen_value(); |
| 358 | RecordAndWritePosition( |
| 359 | chunk()->graph()->SourcePositionToScriptPosition(value->position())); |
| 360 | |
| 361 | Comment(";;; <@%d,#%d> " |
| 362 | "-------------------- Deferred %s --------------------", |
| 363 | code->instruction_index(), |
| 364 | code->instr()->hydrogen_value()->id(), |
| 365 | code->instr()->Mnemonic()); |
| 366 | __ bind(code->entry()); |
| 367 | if (NeedsDeferredFrame()) { |
| 368 | Comment(";;; Build frame"); |
| 369 | DCHECK(!frame_is_built_); |
| 370 | DCHECK(info()->IsStub()); |
| 371 | frame_is_built_ = true; |
| 372 | // Build the frame in such a way that esi isn't trashed. |
| 373 | __ pushq(rbp); // Caller's frame pointer. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 374 | __ Push(Smi::FromInt(StackFrame::STUB)); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 375 | __ leap(rbp, Operand(rsp, TypedFrameConstants::kFixedFrameSizeFromFp)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 376 | Comment(";;; Deferred code"); |
| 377 | } |
| 378 | code->Generate(); |
| 379 | if (NeedsDeferredFrame()) { |
| 380 | __ bind(code->done()); |
| 381 | Comment(";;; Destroy frame"); |
| 382 | DCHECK(frame_is_built_); |
| 383 | frame_is_built_ = false; |
| 384 | __ movp(rsp, rbp); |
| 385 | __ popq(rbp); |
| 386 | } |
| 387 | __ jmp(code->exit()); |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | // Deferred code is the last part of the instruction sequence. Mark |
| 392 | // the generated code as done unless we bailed out. |
| 393 | if (!is_aborted()) status_ = DONE; |
| 394 | return !is_aborted(); |
| 395 | } |
| 396 | |
| 397 | |
| 398 | bool LCodeGen::GenerateSafepointTable() { |
| 399 | DCHECK(is_done()); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 400 | safepoints_.Emit(masm(), GetTotalFrameSlotCount()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 401 | return !is_aborted(); |
| 402 | } |
| 403 | |
| 404 | |
| 405 | Register LCodeGen::ToRegister(int index) const { |
| 406 | return Register::from_code(index); |
| 407 | } |
| 408 | |
| 409 | |
| 410 | XMMRegister LCodeGen::ToDoubleRegister(int index) const { |
| 411 | return XMMRegister::from_code(index); |
| 412 | } |
| 413 | |
| 414 | |
| 415 | Register LCodeGen::ToRegister(LOperand* op) const { |
| 416 | DCHECK(op->IsRegister()); |
| 417 | return ToRegister(op->index()); |
| 418 | } |
| 419 | |
| 420 | |
| 421 | XMMRegister LCodeGen::ToDoubleRegister(LOperand* op) const { |
| 422 | DCHECK(op->IsDoubleRegister()); |
| 423 | return ToDoubleRegister(op->index()); |
| 424 | } |
| 425 | |
| 426 | |
| 427 | bool LCodeGen::IsInteger32Constant(LConstantOperand* op) const { |
| 428 | return chunk_->LookupLiteralRepresentation(op).IsSmiOrInteger32(); |
| 429 | } |
| 430 | |
| 431 | |
| 432 | bool LCodeGen::IsExternalConstant(LConstantOperand* op) const { |
| 433 | return chunk_->LookupLiteralRepresentation(op).IsExternal(); |
| 434 | } |
| 435 | |
| 436 | |
| 437 | bool LCodeGen::IsDehoistedKeyConstant(LConstantOperand* op) const { |
| 438 | return op->IsConstantOperand() && |
| 439 | chunk_->IsDehoistedKey(chunk_->LookupConstant(op)); |
| 440 | } |
| 441 | |
| 442 | |
| 443 | bool LCodeGen::IsSmiConstant(LConstantOperand* op) const { |
| 444 | return chunk_->LookupLiteralRepresentation(op).IsSmi(); |
| 445 | } |
| 446 | |
| 447 | |
| 448 | int32_t LCodeGen::ToInteger32(LConstantOperand* op) const { |
| 449 | return ToRepresentation(op, Representation::Integer32()); |
| 450 | } |
| 451 | |
| 452 | |
| 453 | int32_t LCodeGen::ToRepresentation(LConstantOperand* op, |
| 454 | const Representation& r) const { |
| 455 | HConstant* constant = chunk_->LookupConstant(op); |
| 456 | int32_t value = constant->Integer32Value(); |
| 457 | if (r.IsInteger32()) return value; |
| 458 | DCHECK(SmiValuesAre31Bits() && r.IsSmiOrTagged()); |
| 459 | return static_cast<int32_t>(reinterpret_cast<intptr_t>(Smi::FromInt(value))); |
| 460 | } |
| 461 | |
| 462 | |
| 463 | Smi* LCodeGen::ToSmi(LConstantOperand* op) const { |
| 464 | HConstant* constant = chunk_->LookupConstant(op); |
| 465 | return Smi::FromInt(constant->Integer32Value()); |
| 466 | } |
| 467 | |
| 468 | |
| 469 | double LCodeGen::ToDouble(LConstantOperand* op) const { |
| 470 | HConstant* constant = chunk_->LookupConstant(op); |
| 471 | DCHECK(constant->HasDoubleValue()); |
| 472 | return constant->DoubleValue(); |
| 473 | } |
| 474 | |
| 475 | |
| 476 | ExternalReference LCodeGen::ToExternalReference(LConstantOperand* op) const { |
| 477 | HConstant* constant = chunk_->LookupConstant(op); |
| 478 | DCHECK(constant->HasExternalReferenceValue()); |
| 479 | return constant->ExternalReferenceValue(); |
| 480 | } |
| 481 | |
| 482 | |
| 483 | Handle<Object> LCodeGen::ToHandle(LConstantOperand* op) const { |
| 484 | HConstant* constant = chunk_->LookupConstant(op); |
| 485 | DCHECK(chunk_->LookupLiteralRepresentation(op).IsSmiOrTagged()); |
| 486 | return constant->handle(isolate()); |
| 487 | } |
| 488 | |
| 489 | |
| 490 | static int ArgumentsOffsetWithoutFrame(int index) { |
| 491 | DCHECK(index < 0); |
| 492 | return -(index + 1) * kPointerSize + kPCOnStackSize; |
| 493 | } |
| 494 | |
| 495 | |
| 496 | Operand LCodeGen::ToOperand(LOperand* op) const { |
| 497 | // Does not handle registers. In X64 assembler, plain registers are not |
| 498 | // representable as an Operand. |
| 499 | DCHECK(op->IsStackSlot() || op->IsDoubleStackSlot()); |
| 500 | if (NeedsEagerFrame()) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 501 | return Operand(rbp, FrameSlotToFPOffset(op->index())); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 502 | } else { |
| 503 | // Retrieve parameter without eager stack-frame relative to the |
| 504 | // stack-pointer. |
| 505 | return Operand(rsp, ArgumentsOffsetWithoutFrame(op->index())); |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | |
| 510 | void LCodeGen::WriteTranslation(LEnvironment* environment, |
| 511 | Translation* translation) { |
| 512 | if (environment == NULL) return; |
| 513 | |
| 514 | // The translation includes one command per value in the environment. |
| 515 | int translation_size = environment->translation_size(); |
| 516 | |
| 517 | WriteTranslation(environment->outer(), translation); |
| 518 | WriteTranslationFrame(environment, translation); |
| 519 | |
| 520 | int object_index = 0; |
| 521 | int dematerialized_index = 0; |
| 522 | for (int i = 0; i < translation_size; ++i) { |
| 523 | LOperand* value = environment->values()->at(i); |
| 524 | AddToTranslation( |
| 525 | environment, translation, value, environment->HasTaggedValueAt(i), |
| 526 | environment->HasUint32ValueAt(i), &object_index, &dematerialized_index); |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | |
| 531 | void LCodeGen::AddToTranslation(LEnvironment* environment, |
| 532 | Translation* translation, |
| 533 | LOperand* op, |
| 534 | bool is_tagged, |
| 535 | bool is_uint32, |
| 536 | int* object_index_pointer, |
| 537 | int* dematerialized_index_pointer) { |
| 538 | if (op == LEnvironment::materialization_marker()) { |
| 539 | int object_index = (*object_index_pointer)++; |
| 540 | if (environment->ObjectIsDuplicateAt(object_index)) { |
| 541 | int dupe_of = environment->ObjectDuplicateOfAt(object_index); |
| 542 | translation->DuplicateObject(dupe_of); |
| 543 | return; |
| 544 | } |
| 545 | int object_length = environment->ObjectLengthAt(object_index); |
| 546 | if (environment->ObjectIsArgumentsAt(object_index)) { |
| 547 | translation->BeginArgumentsObject(object_length); |
| 548 | } else { |
| 549 | translation->BeginCapturedObject(object_length); |
| 550 | } |
| 551 | int dematerialized_index = *dematerialized_index_pointer; |
| 552 | int env_offset = environment->translation_size() + dematerialized_index; |
| 553 | *dematerialized_index_pointer += object_length; |
| 554 | for (int i = 0; i < object_length; ++i) { |
| 555 | LOperand* value = environment->values()->at(env_offset + i); |
| 556 | AddToTranslation(environment, |
| 557 | translation, |
| 558 | value, |
| 559 | environment->HasTaggedValueAt(env_offset + i), |
| 560 | environment->HasUint32ValueAt(env_offset + i), |
| 561 | object_index_pointer, |
| 562 | dematerialized_index_pointer); |
| 563 | } |
| 564 | return; |
| 565 | } |
| 566 | |
| 567 | if (op->IsStackSlot()) { |
| 568 | int index = op->index(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 569 | if (is_tagged) { |
| 570 | translation->StoreStackSlot(index); |
| 571 | } else if (is_uint32) { |
| 572 | translation->StoreUint32StackSlot(index); |
| 573 | } else { |
| 574 | translation->StoreInt32StackSlot(index); |
| 575 | } |
| 576 | } else if (op->IsDoubleStackSlot()) { |
| 577 | int index = op->index(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 578 | translation->StoreDoubleStackSlot(index); |
| 579 | } else if (op->IsRegister()) { |
| 580 | Register reg = ToRegister(op); |
| 581 | if (is_tagged) { |
| 582 | translation->StoreRegister(reg); |
| 583 | } else if (is_uint32) { |
| 584 | translation->StoreUint32Register(reg); |
| 585 | } else { |
| 586 | translation->StoreInt32Register(reg); |
| 587 | } |
| 588 | } else if (op->IsDoubleRegister()) { |
| 589 | XMMRegister reg = ToDoubleRegister(op); |
| 590 | translation->StoreDoubleRegister(reg); |
| 591 | } else if (op->IsConstantOperand()) { |
| 592 | HConstant* constant = chunk()->LookupConstant(LConstantOperand::cast(op)); |
| 593 | int src_index = DefineDeoptimizationLiteral(constant->handle(isolate())); |
| 594 | translation->StoreLiteral(src_index); |
| 595 | } else { |
| 596 | UNREACHABLE(); |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | |
| 601 | void LCodeGen::CallCodeGeneric(Handle<Code> code, |
| 602 | RelocInfo::Mode mode, |
| 603 | LInstruction* instr, |
| 604 | SafepointMode safepoint_mode, |
| 605 | int argc) { |
| 606 | DCHECK(instr != NULL); |
| 607 | __ call(code, mode); |
| 608 | RecordSafepointWithLazyDeopt(instr, safepoint_mode, argc); |
| 609 | |
| 610 | // Signal that we don't inline smi code before these stubs in the |
| 611 | // optimizing code generator. |
| 612 | if (code->kind() == Code::BINARY_OP_IC || |
| 613 | code->kind() == Code::COMPARE_IC) { |
| 614 | __ nop(); |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | |
| 619 | void LCodeGen::CallCode(Handle<Code> code, |
| 620 | RelocInfo::Mode mode, |
| 621 | LInstruction* instr) { |
| 622 | CallCodeGeneric(code, mode, instr, RECORD_SIMPLE_SAFEPOINT, 0); |
| 623 | } |
| 624 | |
| 625 | |
| 626 | void LCodeGen::CallRuntime(const Runtime::Function* function, |
| 627 | int num_arguments, |
| 628 | LInstruction* instr, |
| 629 | SaveFPRegsMode save_doubles) { |
| 630 | DCHECK(instr != NULL); |
| 631 | DCHECK(instr->HasPointerMap()); |
| 632 | |
| 633 | __ CallRuntime(function, num_arguments, save_doubles); |
| 634 | |
| 635 | RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT, 0); |
| 636 | } |
| 637 | |
| 638 | |
| 639 | void LCodeGen::LoadContextFromDeferred(LOperand* context) { |
| 640 | if (context->IsRegister()) { |
| 641 | if (!ToRegister(context).is(rsi)) { |
| 642 | __ movp(rsi, ToRegister(context)); |
| 643 | } |
| 644 | } else if (context->IsStackSlot()) { |
| 645 | __ movp(rsi, ToOperand(context)); |
| 646 | } else if (context->IsConstantOperand()) { |
| 647 | HConstant* constant = |
| 648 | chunk_->LookupConstant(LConstantOperand::cast(context)); |
| 649 | __ Move(rsi, Handle<Object>::cast(constant->handle(isolate()))); |
| 650 | } else { |
| 651 | UNREACHABLE(); |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | |
| 656 | |
| 657 | void LCodeGen::CallRuntimeFromDeferred(Runtime::FunctionId id, |
| 658 | int argc, |
| 659 | LInstruction* instr, |
| 660 | LOperand* context) { |
| 661 | LoadContextFromDeferred(context); |
| 662 | |
| 663 | __ CallRuntimeSaveDoubles(id); |
| 664 | RecordSafepointWithRegisters( |
| 665 | instr->pointer_map(), argc, Safepoint::kNoLazyDeopt); |
| 666 | } |
| 667 | |
| 668 | |
| 669 | void LCodeGen::RegisterEnvironmentForDeoptimization(LEnvironment* environment, |
| 670 | Safepoint::DeoptMode mode) { |
| 671 | environment->set_has_been_used(); |
| 672 | if (!environment->HasBeenRegistered()) { |
| 673 | // Physical stack frame layout: |
| 674 | // -x ............. -4 0 ..................................... y |
| 675 | // [incoming arguments] [spill slots] [pushed outgoing arguments] |
| 676 | |
| 677 | // Layout of the environment: |
| 678 | // 0 ..................................................... size-1 |
| 679 | // [parameters] [locals] [expression stack including arguments] |
| 680 | |
| 681 | // Layout of the translation: |
| 682 | // 0 ........................................................ size - 1 + 4 |
| 683 | // [expression stack including arguments] [locals] [4 words] [parameters] |
| 684 | // |>------------ translation_size ------------<| |
| 685 | |
| 686 | int frame_count = 0; |
| 687 | int jsframe_count = 0; |
| 688 | for (LEnvironment* e = environment; e != NULL; e = e->outer()) { |
| 689 | ++frame_count; |
| 690 | if (e->frame_type() == JS_FUNCTION) { |
| 691 | ++jsframe_count; |
| 692 | } |
| 693 | } |
| 694 | Translation translation(&translations_, frame_count, jsframe_count, zone()); |
| 695 | WriteTranslation(environment, &translation); |
| 696 | int deoptimization_index = deoptimizations_.length(); |
| 697 | int pc_offset = masm()->pc_offset(); |
| 698 | environment->Register(deoptimization_index, |
| 699 | translation.index(), |
| 700 | (mode == Safepoint::kLazyDeopt) ? pc_offset : -1); |
| 701 | deoptimizations_.Add(environment, environment->zone()); |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | |
| 706 | void LCodeGen::DeoptimizeIf(Condition cc, LInstruction* instr, |
| 707 | Deoptimizer::DeoptReason deopt_reason, |
| 708 | Deoptimizer::BailoutType bailout_type) { |
| 709 | LEnvironment* environment = instr->environment(); |
| 710 | RegisterEnvironmentForDeoptimization(environment, Safepoint::kNoLazyDeopt); |
| 711 | DCHECK(environment->HasBeenRegistered()); |
| 712 | int id = environment->deoptimization_index(); |
| 713 | Address entry = |
| 714 | Deoptimizer::GetDeoptimizationEntry(isolate(), id, bailout_type); |
| 715 | if (entry == NULL) { |
| 716 | Abort(kBailoutWasNotPrepared); |
| 717 | return; |
| 718 | } |
| 719 | |
| 720 | if (DeoptEveryNTimes()) { |
| 721 | ExternalReference count = ExternalReference::stress_deopt_count(isolate()); |
| 722 | Label no_deopt; |
| 723 | __ pushfq(); |
| 724 | __ pushq(rax); |
| 725 | Operand count_operand = masm()->ExternalOperand(count, kScratchRegister); |
| 726 | __ movl(rax, count_operand); |
| 727 | __ subl(rax, Immediate(1)); |
| 728 | __ j(not_zero, &no_deopt, Label::kNear); |
| 729 | if (FLAG_trap_on_deopt) __ int3(); |
| 730 | __ movl(rax, Immediate(FLAG_deopt_every_n_times)); |
| 731 | __ movl(count_operand, rax); |
| 732 | __ popq(rax); |
| 733 | __ popfq(); |
| 734 | DCHECK(frame_is_built_); |
| 735 | __ call(entry, RelocInfo::RUNTIME_ENTRY); |
| 736 | __ bind(&no_deopt); |
| 737 | __ movl(count_operand, rax); |
| 738 | __ popq(rax); |
| 739 | __ popfq(); |
| 740 | } |
| 741 | |
| 742 | if (info()->ShouldTrapOnDeopt()) { |
| 743 | Label done; |
| 744 | if (cc != no_condition) { |
| 745 | __ j(NegateCondition(cc), &done, Label::kNear); |
| 746 | } |
| 747 | __ int3(); |
| 748 | __ bind(&done); |
| 749 | } |
| 750 | |
| 751 | Deoptimizer::DeoptInfo deopt_info = MakeDeoptInfo(instr, deopt_reason); |
| 752 | |
| 753 | DCHECK(info()->IsStub() || frame_is_built_); |
| 754 | // Go through jump table if we need to handle condition, build frame, or |
| 755 | // restore caller doubles. |
| 756 | if (cc == no_condition && frame_is_built_ && |
| 757 | !info()->saves_caller_doubles()) { |
| 758 | DeoptComment(deopt_info); |
| 759 | __ call(entry, RelocInfo::RUNTIME_ENTRY); |
| 760 | info()->LogDeoptCallPosition(masm()->pc_offset(), deopt_info.inlining_id); |
| 761 | } else { |
| 762 | Deoptimizer::JumpTableEntry table_entry(entry, deopt_info, bailout_type, |
| 763 | !frame_is_built_); |
| 764 | // We often have several deopts to the same entry, reuse the last |
| 765 | // jump entry if this is the case. |
| 766 | if (FLAG_trace_deopt || isolate()->cpu_profiler()->is_profiling() || |
| 767 | jump_table_.is_empty() || |
| 768 | !table_entry.IsEquivalentTo(jump_table_.last())) { |
| 769 | jump_table_.Add(table_entry, zone()); |
| 770 | } |
| 771 | if (cc == no_condition) { |
| 772 | __ jmp(&jump_table_.last().label); |
| 773 | } else { |
| 774 | __ j(cc, &jump_table_.last().label); |
| 775 | } |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | |
| 780 | void LCodeGen::DeoptimizeIf(Condition cc, LInstruction* instr, |
| 781 | Deoptimizer::DeoptReason deopt_reason) { |
| 782 | Deoptimizer::BailoutType bailout_type = info()->IsStub() |
| 783 | ? Deoptimizer::LAZY |
| 784 | : Deoptimizer::EAGER; |
| 785 | DeoptimizeIf(cc, instr, deopt_reason, bailout_type); |
| 786 | } |
| 787 | |
| 788 | |
| 789 | void LCodeGen::RecordSafepointWithLazyDeopt( |
| 790 | LInstruction* instr, SafepointMode safepoint_mode, int argc) { |
| 791 | if (safepoint_mode == RECORD_SIMPLE_SAFEPOINT) { |
| 792 | RecordSafepoint(instr->pointer_map(), Safepoint::kLazyDeopt); |
| 793 | } else { |
| 794 | DCHECK(safepoint_mode == RECORD_SAFEPOINT_WITH_REGISTERS); |
| 795 | RecordSafepointWithRegisters( |
| 796 | instr->pointer_map(), argc, Safepoint::kLazyDeopt); |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | |
| 801 | void LCodeGen::RecordSafepoint( |
| 802 | LPointerMap* pointers, |
| 803 | Safepoint::Kind kind, |
| 804 | int arguments, |
| 805 | Safepoint::DeoptMode deopt_mode) { |
| 806 | DCHECK(kind == expected_safepoint_kind_); |
| 807 | |
| 808 | const ZoneList<LOperand*>* operands = pointers->GetNormalizedOperands(); |
| 809 | |
| 810 | Safepoint safepoint = safepoints_.DefineSafepoint(masm(), |
| 811 | kind, arguments, deopt_mode); |
| 812 | for (int i = 0; i < operands->length(); i++) { |
| 813 | LOperand* pointer = operands->at(i); |
| 814 | if (pointer->IsStackSlot()) { |
| 815 | safepoint.DefinePointerSlot(pointer->index(), zone()); |
| 816 | } else if (pointer->IsRegister() && (kind & Safepoint::kWithRegisters)) { |
| 817 | safepoint.DefinePointerRegister(ToRegister(pointer), zone()); |
| 818 | } |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | |
| 823 | void LCodeGen::RecordSafepoint(LPointerMap* pointers, |
| 824 | Safepoint::DeoptMode deopt_mode) { |
| 825 | RecordSafepoint(pointers, Safepoint::kSimple, 0, deopt_mode); |
| 826 | } |
| 827 | |
| 828 | |
| 829 | void LCodeGen::RecordSafepoint(Safepoint::DeoptMode deopt_mode) { |
| 830 | LPointerMap empty_pointers(zone()); |
| 831 | RecordSafepoint(&empty_pointers, deopt_mode); |
| 832 | } |
| 833 | |
| 834 | |
| 835 | void LCodeGen::RecordSafepointWithRegisters(LPointerMap* pointers, |
| 836 | int arguments, |
| 837 | Safepoint::DeoptMode deopt_mode) { |
| 838 | RecordSafepoint(pointers, Safepoint::kWithRegisters, arguments, deopt_mode); |
| 839 | } |
| 840 | |
| 841 | |
| 842 | void LCodeGen::RecordAndWritePosition(int position) { |
| 843 | if (position == RelocInfo::kNoPosition) return; |
| 844 | masm()->positions_recorder()->RecordPosition(position); |
| 845 | masm()->positions_recorder()->WriteRecordedPositions(); |
| 846 | } |
| 847 | |
| 848 | |
| 849 | static const char* LabelType(LLabel* label) { |
| 850 | if (label->is_loop_header()) return " (loop header)"; |
| 851 | if (label->is_osr_entry()) return " (OSR entry)"; |
| 852 | return ""; |
| 853 | } |
| 854 | |
| 855 | |
| 856 | void LCodeGen::DoLabel(LLabel* label) { |
| 857 | Comment(";;; <@%d,#%d> -------------------- B%d%s --------------------", |
| 858 | current_instruction_, |
| 859 | label->hydrogen_value()->id(), |
| 860 | label->block_id(), |
| 861 | LabelType(label)); |
| 862 | __ bind(label->label()); |
| 863 | current_block_ = label->block_id(); |
| 864 | DoGap(label); |
| 865 | } |
| 866 | |
| 867 | |
| 868 | void LCodeGen::DoParallelMove(LParallelMove* move) { |
| 869 | resolver_.Resolve(move); |
| 870 | } |
| 871 | |
| 872 | |
| 873 | void LCodeGen::DoGap(LGap* gap) { |
| 874 | for (int i = LGap::FIRST_INNER_POSITION; |
| 875 | i <= LGap::LAST_INNER_POSITION; |
| 876 | i++) { |
| 877 | LGap::InnerPosition inner_pos = static_cast<LGap::InnerPosition>(i); |
| 878 | LParallelMove* move = gap->GetParallelMove(inner_pos); |
| 879 | if (move != NULL) DoParallelMove(move); |
| 880 | } |
| 881 | } |
| 882 | |
| 883 | |
| 884 | void LCodeGen::DoInstructionGap(LInstructionGap* instr) { |
| 885 | DoGap(instr); |
| 886 | } |
| 887 | |
| 888 | |
| 889 | void LCodeGen::DoParameter(LParameter* instr) { |
| 890 | // Nothing to do. |
| 891 | } |
| 892 | |
| 893 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 894 | void LCodeGen::DoUnknownOSRValue(LUnknownOSRValue* instr) { |
| 895 | GenerateOsrPrologue(); |
| 896 | } |
| 897 | |
| 898 | |
| 899 | void LCodeGen::DoModByPowerOf2I(LModByPowerOf2I* instr) { |
| 900 | Register dividend = ToRegister(instr->dividend()); |
| 901 | int32_t divisor = instr->divisor(); |
| 902 | DCHECK(dividend.is(ToRegister(instr->result()))); |
| 903 | |
| 904 | // Theoretically, a variation of the branch-free code for integer division by |
| 905 | // a power of 2 (calculating the remainder via an additional multiplication |
| 906 | // (which gets simplified to an 'and') and subtraction) should be faster, and |
| 907 | // this is exactly what GCC and clang emit. Nevertheless, benchmarks seem to |
| 908 | // indicate that positive dividends are heavily favored, so the branching |
| 909 | // version performs better. |
| 910 | HMod* hmod = instr->hydrogen(); |
| 911 | int32_t mask = divisor < 0 ? -(divisor + 1) : (divisor - 1); |
| 912 | Label dividend_is_not_negative, done; |
| 913 | if (hmod->CheckFlag(HValue::kLeftCanBeNegative)) { |
| 914 | __ testl(dividend, dividend); |
| 915 | __ j(not_sign, ÷nd_is_not_negative, Label::kNear); |
| 916 | // Note that this is correct even for kMinInt operands. |
| 917 | __ negl(dividend); |
| 918 | __ andl(dividend, Immediate(mask)); |
| 919 | __ negl(dividend); |
| 920 | if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) { |
| 921 | DeoptimizeIf(zero, instr, Deoptimizer::kMinusZero); |
| 922 | } |
| 923 | __ jmp(&done, Label::kNear); |
| 924 | } |
| 925 | |
| 926 | __ bind(÷nd_is_not_negative); |
| 927 | __ andl(dividend, Immediate(mask)); |
| 928 | __ bind(&done); |
| 929 | } |
| 930 | |
| 931 | |
| 932 | void LCodeGen::DoModByConstI(LModByConstI* instr) { |
| 933 | Register dividend = ToRegister(instr->dividend()); |
| 934 | int32_t divisor = instr->divisor(); |
| 935 | DCHECK(ToRegister(instr->result()).is(rax)); |
| 936 | |
| 937 | if (divisor == 0) { |
| 938 | DeoptimizeIf(no_condition, instr, Deoptimizer::kDivisionByZero); |
| 939 | return; |
| 940 | } |
| 941 | |
| 942 | __ TruncatingDiv(dividend, Abs(divisor)); |
| 943 | __ imull(rdx, rdx, Immediate(Abs(divisor))); |
| 944 | __ movl(rax, dividend); |
| 945 | __ subl(rax, rdx); |
| 946 | |
| 947 | // Check for negative zero. |
| 948 | HMod* hmod = instr->hydrogen(); |
| 949 | if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) { |
| 950 | Label remainder_not_zero; |
| 951 | __ j(not_zero, &remainder_not_zero, Label::kNear); |
| 952 | __ cmpl(dividend, Immediate(0)); |
| 953 | DeoptimizeIf(less, instr, Deoptimizer::kMinusZero); |
| 954 | __ bind(&remainder_not_zero); |
| 955 | } |
| 956 | } |
| 957 | |
| 958 | |
| 959 | void LCodeGen::DoModI(LModI* instr) { |
| 960 | HMod* hmod = instr->hydrogen(); |
| 961 | |
| 962 | Register left_reg = ToRegister(instr->left()); |
| 963 | DCHECK(left_reg.is(rax)); |
| 964 | Register right_reg = ToRegister(instr->right()); |
| 965 | DCHECK(!right_reg.is(rax)); |
| 966 | DCHECK(!right_reg.is(rdx)); |
| 967 | Register result_reg = ToRegister(instr->result()); |
| 968 | DCHECK(result_reg.is(rdx)); |
| 969 | |
| 970 | Label done; |
| 971 | // Check for x % 0, idiv would signal a divide error. We have to |
| 972 | // deopt in this case because we can't return a NaN. |
| 973 | if (hmod->CheckFlag(HValue::kCanBeDivByZero)) { |
| 974 | __ testl(right_reg, right_reg); |
| 975 | DeoptimizeIf(zero, instr, Deoptimizer::kDivisionByZero); |
| 976 | } |
| 977 | |
| 978 | // Check for kMinInt % -1, idiv would signal a divide error. We |
| 979 | // have to deopt if we care about -0, because we can't return that. |
| 980 | if (hmod->CheckFlag(HValue::kCanOverflow)) { |
| 981 | Label no_overflow_possible; |
| 982 | __ cmpl(left_reg, Immediate(kMinInt)); |
| 983 | __ j(not_zero, &no_overflow_possible, Label::kNear); |
| 984 | __ cmpl(right_reg, Immediate(-1)); |
| 985 | if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) { |
| 986 | DeoptimizeIf(equal, instr, Deoptimizer::kMinusZero); |
| 987 | } else { |
| 988 | __ j(not_equal, &no_overflow_possible, Label::kNear); |
| 989 | __ Set(result_reg, 0); |
| 990 | __ jmp(&done, Label::kNear); |
| 991 | } |
| 992 | __ bind(&no_overflow_possible); |
| 993 | } |
| 994 | |
| 995 | // Sign extend dividend in eax into edx:eax, since we are using only the low |
| 996 | // 32 bits of the values. |
| 997 | __ cdq(); |
| 998 | |
| 999 | // If we care about -0, test if the dividend is <0 and the result is 0. |
| 1000 | if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) { |
| 1001 | Label positive_left; |
| 1002 | __ testl(left_reg, left_reg); |
| 1003 | __ j(not_sign, &positive_left, Label::kNear); |
| 1004 | __ idivl(right_reg); |
| 1005 | __ testl(result_reg, result_reg); |
| 1006 | DeoptimizeIf(zero, instr, Deoptimizer::kMinusZero); |
| 1007 | __ jmp(&done, Label::kNear); |
| 1008 | __ bind(&positive_left); |
| 1009 | } |
| 1010 | __ idivl(right_reg); |
| 1011 | __ bind(&done); |
| 1012 | } |
| 1013 | |
| 1014 | |
| 1015 | void LCodeGen::DoFlooringDivByPowerOf2I(LFlooringDivByPowerOf2I* instr) { |
| 1016 | Register dividend = ToRegister(instr->dividend()); |
| 1017 | int32_t divisor = instr->divisor(); |
| 1018 | DCHECK(dividend.is(ToRegister(instr->result()))); |
| 1019 | |
| 1020 | // If the divisor is positive, things are easy: There can be no deopts and we |
| 1021 | // can simply do an arithmetic right shift. |
| 1022 | if (divisor == 1) return; |
| 1023 | int32_t shift = WhichPowerOf2Abs(divisor); |
| 1024 | if (divisor > 1) { |
| 1025 | __ sarl(dividend, Immediate(shift)); |
| 1026 | return; |
| 1027 | } |
| 1028 | |
| 1029 | // If the divisor is negative, we have to negate and handle edge cases. |
| 1030 | __ negl(dividend); |
| 1031 | if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) { |
| 1032 | DeoptimizeIf(zero, instr, Deoptimizer::kMinusZero); |
| 1033 | } |
| 1034 | |
| 1035 | // Dividing by -1 is basically negation, unless we overflow. |
| 1036 | if (divisor == -1) { |
| 1037 | if (instr->hydrogen()->CheckFlag(HValue::kLeftCanBeMinInt)) { |
| 1038 | DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow); |
| 1039 | } |
| 1040 | return; |
| 1041 | } |
| 1042 | |
| 1043 | // If the negation could not overflow, simply shifting is OK. |
| 1044 | if (!instr->hydrogen()->CheckFlag(HValue::kLeftCanBeMinInt)) { |
| 1045 | __ sarl(dividend, Immediate(shift)); |
| 1046 | return; |
| 1047 | } |
| 1048 | |
| 1049 | Label not_kmin_int, done; |
| 1050 | __ j(no_overflow, ¬_kmin_int, Label::kNear); |
| 1051 | __ movl(dividend, Immediate(kMinInt / divisor)); |
| 1052 | __ jmp(&done, Label::kNear); |
| 1053 | __ bind(¬_kmin_int); |
| 1054 | __ sarl(dividend, Immediate(shift)); |
| 1055 | __ bind(&done); |
| 1056 | } |
| 1057 | |
| 1058 | |
| 1059 | void LCodeGen::DoFlooringDivByConstI(LFlooringDivByConstI* instr) { |
| 1060 | Register dividend = ToRegister(instr->dividend()); |
| 1061 | int32_t divisor = instr->divisor(); |
| 1062 | DCHECK(ToRegister(instr->result()).is(rdx)); |
| 1063 | |
| 1064 | if (divisor == 0) { |
| 1065 | DeoptimizeIf(no_condition, instr, Deoptimizer::kDivisionByZero); |
| 1066 | return; |
| 1067 | } |
| 1068 | |
| 1069 | // Check for (0 / -x) that will produce negative zero. |
| 1070 | HMathFloorOfDiv* hdiv = instr->hydrogen(); |
| 1071 | if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) { |
| 1072 | __ testl(dividend, dividend); |
| 1073 | DeoptimizeIf(zero, instr, Deoptimizer::kMinusZero); |
| 1074 | } |
| 1075 | |
| 1076 | // Easy case: We need no dynamic check for the dividend and the flooring |
| 1077 | // division is the same as the truncating division. |
| 1078 | if ((divisor > 0 && !hdiv->CheckFlag(HValue::kLeftCanBeNegative)) || |
| 1079 | (divisor < 0 && !hdiv->CheckFlag(HValue::kLeftCanBePositive))) { |
| 1080 | __ TruncatingDiv(dividend, Abs(divisor)); |
| 1081 | if (divisor < 0) __ negl(rdx); |
| 1082 | return; |
| 1083 | } |
| 1084 | |
| 1085 | // In the general case we may need to adjust before and after the truncating |
| 1086 | // division to get a flooring division. |
| 1087 | Register temp = ToRegister(instr->temp3()); |
| 1088 | DCHECK(!temp.is(dividend) && !temp.is(rax) && !temp.is(rdx)); |
| 1089 | Label needs_adjustment, done; |
| 1090 | __ cmpl(dividend, Immediate(0)); |
| 1091 | __ j(divisor > 0 ? less : greater, &needs_adjustment, Label::kNear); |
| 1092 | __ TruncatingDiv(dividend, Abs(divisor)); |
| 1093 | if (divisor < 0) __ negl(rdx); |
| 1094 | __ jmp(&done, Label::kNear); |
| 1095 | __ bind(&needs_adjustment); |
| 1096 | __ leal(temp, Operand(dividend, divisor > 0 ? 1 : -1)); |
| 1097 | __ TruncatingDiv(temp, Abs(divisor)); |
| 1098 | if (divisor < 0) __ negl(rdx); |
| 1099 | __ decl(rdx); |
| 1100 | __ bind(&done); |
| 1101 | } |
| 1102 | |
| 1103 | |
| 1104 | // TODO(svenpanne) Refactor this to avoid code duplication with DoDivI. |
| 1105 | void LCodeGen::DoFlooringDivI(LFlooringDivI* instr) { |
| 1106 | HBinaryOperation* hdiv = instr->hydrogen(); |
| 1107 | Register dividend = ToRegister(instr->dividend()); |
| 1108 | Register divisor = ToRegister(instr->divisor()); |
| 1109 | Register remainder = ToRegister(instr->temp()); |
| 1110 | Register result = ToRegister(instr->result()); |
| 1111 | DCHECK(dividend.is(rax)); |
| 1112 | DCHECK(remainder.is(rdx)); |
| 1113 | DCHECK(result.is(rax)); |
| 1114 | DCHECK(!divisor.is(rax)); |
| 1115 | DCHECK(!divisor.is(rdx)); |
| 1116 | |
| 1117 | // Check for x / 0. |
| 1118 | if (hdiv->CheckFlag(HValue::kCanBeDivByZero)) { |
| 1119 | __ testl(divisor, divisor); |
| 1120 | DeoptimizeIf(zero, instr, Deoptimizer::kDivisionByZero); |
| 1121 | } |
| 1122 | |
| 1123 | // Check for (0 / -x) that will produce negative zero. |
| 1124 | if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero)) { |
| 1125 | Label dividend_not_zero; |
| 1126 | __ testl(dividend, dividend); |
| 1127 | __ j(not_zero, ÷nd_not_zero, Label::kNear); |
| 1128 | __ testl(divisor, divisor); |
| 1129 | DeoptimizeIf(sign, instr, Deoptimizer::kMinusZero); |
| 1130 | __ bind(÷nd_not_zero); |
| 1131 | } |
| 1132 | |
| 1133 | // Check for (kMinInt / -1). |
| 1134 | if (hdiv->CheckFlag(HValue::kCanOverflow)) { |
| 1135 | Label dividend_not_min_int; |
| 1136 | __ cmpl(dividend, Immediate(kMinInt)); |
| 1137 | __ j(not_zero, ÷nd_not_min_int, Label::kNear); |
| 1138 | __ cmpl(divisor, Immediate(-1)); |
| 1139 | DeoptimizeIf(zero, instr, Deoptimizer::kOverflow); |
| 1140 | __ bind(÷nd_not_min_int); |
| 1141 | } |
| 1142 | |
| 1143 | // Sign extend to rdx (= remainder). |
| 1144 | __ cdq(); |
| 1145 | __ idivl(divisor); |
| 1146 | |
| 1147 | Label done; |
| 1148 | __ testl(remainder, remainder); |
| 1149 | __ j(zero, &done, Label::kNear); |
| 1150 | __ xorl(remainder, divisor); |
| 1151 | __ sarl(remainder, Immediate(31)); |
| 1152 | __ addl(result, remainder); |
| 1153 | __ bind(&done); |
| 1154 | } |
| 1155 | |
| 1156 | |
| 1157 | void LCodeGen::DoDivByPowerOf2I(LDivByPowerOf2I* instr) { |
| 1158 | Register dividend = ToRegister(instr->dividend()); |
| 1159 | int32_t divisor = instr->divisor(); |
| 1160 | Register result = ToRegister(instr->result()); |
| 1161 | DCHECK(divisor == kMinInt || base::bits::IsPowerOfTwo32(Abs(divisor))); |
| 1162 | DCHECK(!result.is(dividend)); |
| 1163 | |
| 1164 | // Check for (0 / -x) that will produce negative zero. |
| 1165 | HDiv* hdiv = instr->hydrogen(); |
| 1166 | if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) { |
| 1167 | __ testl(dividend, dividend); |
| 1168 | DeoptimizeIf(zero, instr, Deoptimizer::kMinusZero); |
| 1169 | } |
| 1170 | // Check for (kMinInt / -1). |
| 1171 | if (hdiv->CheckFlag(HValue::kCanOverflow) && divisor == -1) { |
| 1172 | __ cmpl(dividend, Immediate(kMinInt)); |
| 1173 | DeoptimizeIf(zero, instr, Deoptimizer::kOverflow); |
| 1174 | } |
| 1175 | // Deoptimize if remainder will not be 0. |
| 1176 | if (!hdiv->CheckFlag(HInstruction::kAllUsesTruncatingToInt32) && |
| 1177 | divisor != 1 && divisor != -1) { |
| 1178 | int32_t mask = divisor < 0 ? -(divisor + 1) : (divisor - 1); |
| 1179 | __ testl(dividend, Immediate(mask)); |
| 1180 | DeoptimizeIf(not_zero, instr, Deoptimizer::kLostPrecision); |
| 1181 | } |
| 1182 | __ Move(result, dividend); |
| 1183 | int32_t shift = WhichPowerOf2Abs(divisor); |
| 1184 | if (shift > 0) { |
| 1185 | // The arithmetic shift is always OK, the 'if' is an optimization only. |
| 1186 | if (shift > 1) __ sarl(result, Immediate(31)); |
| 1187 | __ shrl(result, Immediate(32 - shift)); |
| 1188 | __ addl(result, dividend); |
| 1189 | __ sarl(result, Immediate(shift)); |
| 1190 | } |
| 1191 | if (divisor < 0) __ negl(result); |
| 1192 | } |
| 1193 | |
| 1194 | |
| 1195 | void LCodeGen::DoDivByConstI(LDivByConstI* instr) { |
| 1196 | Register dividend = ToRegister(instr->dividend()); |
| 1197 | int32_t divisor = instr->divisor(); |
| 1198 | DCHECK(ToRegister(instr->result()).is(rdx)); |
| 1199 | |
| 1200 | if (divisor == 0) { |
| 1201 | DeoptimizeIf(no_condition, instr, Deoptimizer::kDivisionByZero); |
| 1202 | return; |
| 1203 | } |
| 1204 | |
| 1205 | // Check for (0 / -x) that will produce negative zero. |
| 1206 | HDiv* hdiv = instr->hydrogen(); |
| 1207 | if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) { |
| 1208 | __ testl(dividend, dividend); |
| 1209 | DeoptimizeIf(zero, instr, Deoptimizer::kMinusZero); |
| 1210 | } |
| 1211 | |
| 1212 | __ TruncatingDiv(dividend, Abs(divisor)); |
| 1213 | if (divisor < 0) __ negl(rdx); |
| 1214 | |
| 1215 | if (!hdiv->CheckFlag(HInstruction::kAllUsesTruncatingToInt32)) { |
| 1216 | __ movl(rax, rdx); |
| 1217 | __ imull(rax, rax, Immediate(divisor)); |
| 1218 | __ subl(rax, dividend); |
| 1219 | DeoptimizeIf(not_equal, instr, Deoptimizer::kLostPrecision); |
| 1220 | } |
| 1221 | } |
| 1222 | |
| 1223 | |
| 1224 | // TODO(svenpanne) Refactor this to avoid code duplication with DoFlooringDivI. |
| 1225 | void LCodeGen::DoDivI(LDivI* instr) { |
| 1226 | HBinaryOperation* hdiv = instr->hydrogen(); |
| 1227 | Register dividend = ToRegister(instr->dividend()); |
| 1228 | Register divisor = ToRegister(instr->divisor()); |
| 1229 | Register remainder = ToRegister(instr->temp()); |
| 1230 | DCHECK(dividend.is(rax)); |
| 1231 | DCHECK(remainder.is(rdx)); |
| 1232 | DCHECK(ToRegister(instr->result()).is(rax)); |
| 1233 | DCHECK(!divisor.is(rax)); |
| 1234 | DCHECK(!divisor.is(rdx)); |
| 1235 | |
| 1236 | // Check for x / 0. |
| 1237 | if (hdiv->CheckFlag(HValue::kCanBeDivByZero)) { |
| 1238 | __ testl(divisor, divisor); |
| 1239 | DeoptimizeIf(zero, instr, Deoptimizer::kDivisionByZero); |
| 1240 | } |
| 1241 | |
| 1242 | // Check for (0 / -x) that will produce negative zero. |
| 1243 | if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero)) { |
| 1244 | Label dividend_not_zero; |
| 1245 | __ testl(dividend, dividend); |
| 1246 | __ j(not_zero, ÷nd_not_zero, Label::kNear); |
| 1247 | __ testl(divisor, divisor); |
| 1248 | DeoptimizeIf(sign, instr, Deoptimizer::kMinusZero); |
| 1249 | __ bind(÷nd_not_zero); |
| 1250 | } |
| 1251 | |
| 1252 | // Check for (kMinInt / -1). |
| 1253 | if (hdiv->CheckFlag(HValue::kCanOverflow)) { |
| 1254 | Label dividend_not_min_int; |
| 1255 | __ cmpl(dividend, Immediate(kMinInt)); |
| 1256 | __ j(not_zero, ÷nd_not_min_int, Label::kNear); |
| 1257 | __ cmpl(divisor, Immediate(-1)); |
| 1258 | DeoptimizeIf(zero, instr, Deoptimizer::kOverflow); |
| 1259 | __ bind(÷nd_not_min_int); |
| 1260 | } |
| 1261 | |
| 1262 | // Sign extend to rdx (= remainder). |
| 1263 | __ cdq(); |
| 1264 | __ idivl(divisor); |
| 1265 | |
| 1266 | if (!hdiv->CheckFlag(HValue::kAllUsesTruncatingToInt32)) { |
| 1267 | // Deoptimize if remainder is not 0. |
| 1268 | __ testl(remainder, remainder); |
| 1269 | DeoptimizeIf(not_zero, instr, Deoptimizer::kLostPrecision); |
| 1270 | } |
| 1271 | } |
| 1272 | |
| 1273 | |
| 1274 | void LCodeGen::DoMulI(LMulI* instr) { |
| 1275 | Register left = ToRegister(instr->left()); |
| 1276 | LOperand* right = instr->right(); |
| 1277 | |
| 1278 | if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) { |
| 1279 | if (instr->hydrogen_value()->representation().IsSmi()) { |
| 1280 | __ movp(kScratchRegister, left); |
| 1281 | } else { |
| 1282 | __ movl(kScratchRegister, left); |
| 1283 | } |
| 1284 | } |
| 1285 | |
| 1286 | bool can_overflow = |
| 1287 | instr->hydrogen()->CheckFlag(HValue::kCanOverflow); |
| 1288 | if (right->IsConstantOperand()) { |
| 1289 | int32_t right_value = ToInteger32(LConstantOperand::cast(right)); |
| 1290 | if (right_value == -1) { |
| 1291 | __ negl(left); |
| 1292 | } else if (right_value == 0) { |
| 1293 | __ xorl(left, left); |
| 1294 | } else if (right_value == 2) { |
| 1295 | __ addl(left, left); |
| 1296 | } else if (!can_overflow) { |
| 1297 | // If the multiplication is known to not overflow, we |
| 1298 | // can use operations that don't set the overflow flag |
| 1299 | // correctly. |
| 1300 | switch (right_value) { |
| 1301 | case 1: |
| 1302 | // Do nothing. |
| 1303 | break; |
| 1304 | case 3: |
| 1305 | __ leal(left, Operand(left, left, times_2, 0)); |
| 1306 | break; |
| 1307 | case 4: |
| 1308 | __ shll(left, Immediate(2)); |
| 1309 | break; |
| 1310 | case 5: |
| 1311 | __ leal(left, Operand(left, left, times_4, 0)); |
| 1312 | break; |
| 1313 | case 8: |
| 1314 | __ shll(left, Immediate(3)); |
| 1315 | break; |
| 1316 | case 9: |
| 1317 | __ leal(left, Operand(left, left, times_8, 0)); |
| 1318 | break; |
| 1319 | case 16: |
| 1320 | __ shll(left, Immediate(4)); |
| 1321 | break; |
| 1322 | default: |
| 1323 | __ imull(left, left, Immediate(right_value)); |
| 1324 | break; |
| 1325 | } |
| 1326 | } else { |
| 1327 | __ imull(left, left, Immediate(right_value)); |
| 1328 | } |
| 1329 | } else if (right->IsStackSlot()) { |
| 1330 | if (instr->hydrogen_value()->representation().IsSmi()) { |
| 1331 | __ SmiToInteger64(left, left); |
| 1332 | __ imulp(left, ToOperand(right)); |
| 1333 | } else { |
| 1334 | __ imull(left, ToOperand(right)); |
| 1335 | } |
| 1336 | } else { |
| 1337 | if (instr->hydrogen_value()->representation().IsSmi()) { |
| 1338 | __ SmiToInteger64(left, left); |
| 1339 | __ imulp(left, ToRegister(right)); |
| 1340 | } else { |
| 1341 | __ imull(left, ToRegister(right)); |
| 1342 | } |
| 1343 | } |
| 1344 | |
| 1345 | if (can_overflow) { |
| 1346 | DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow); |
| 1347 | } |
| 1348 | |
| 1349 | if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) { |
| 1350 | // Bail out if the result is supposed to be negative zero. |
| 1351 | Label done; |
| 1352 | if (instr->hydrogen_value()->representation().IsSmi()) { |
| 1353 | __ testp(left, left); |
| 1354 | } else { |
| 1355 | __ testl(left, left); |
| 1356 | } |
| 1357 | __ j(not_zero, &done, Label::kNear); |
| 1358 | if (right->IsConstantOperand()) { |
| 1359 | // Constant can't be represented as 32-bit Smi due to immediate size |
| 1360 | // limit. |
| 1361 | DCHECK(SmiValuesAre32Bits() |
| 1362 | ? !instr->hydrogen_value()->representation().IsSmi() |
| 1363 | : SmiValuesAre31Bits()); |
| 1364 | if (ToInteger32(LConstantOperand::cast(right)) < 0) { |
| 1365 | DeoptimizeIf(no_condition, instr, Deoptimizer::kMinusZero); |
| 1366 | } else if (ToInteger32(LConstantOperand::cast(right)) == 0) { |
| 1367 | __ cmpl(kScratchRegister, Immediate(0)); |
| 1368 | DeoptimizeIf(less, instr, Deoptimizer::kMinusZero); |
| 1369 | } |
| 1370 | } else if (right->IsStackSlot()) { |
| 1371 | if (instr->hydrogen_value()->representation().IsSmi()) { |
| 1372 | __ orp(kScratchRegister, ToOperand(right)); |
| 1373 | } else { |
| 1374 | __ orl(kScratchRegister, ToOperand(right)); |
| 1375 | } |
| 1376 | DeoptimizeIf(sign, instr, Deoptimizer::kMinusZero); |
| 1377 | } else { |
| 1378 | // Test the non-zero operand for negative sign. |
| 1379 | if (instr->hydrogen_value()->representation().IsSmi()) { |
| 1380 | __ orp(kScratchRegister, ToRegister(right)); |
| 1381 | } else { |
| 1382 | __ orl(kScratchRegister, ToRegister(right)); |
| 1383 | } |
| 1384 | DeoptimizeIf(sign, instr, Deoptimizer::kMinusZero); |
| 1385 | } |
| 1386 | __ bind(&done); |
| 1387 | } |
| 1388 | } |
| 1389 | |
| 1390 | |
| 1391 | void LCodeGen::DoBitI(LBitI* instr) { |
| 1392 | LOperand* left = instr->left(); |
| 1393 | LOperand* right = instr->right(); |
| 1394 | DCHECK(left->Equals(instr->result())); |
| 1395 | DCHECK(left->IsRegister()); |
| 1396 | |
| 1397 | if (right->IsConstantOperand()) { |
| 1398 | int32_t right_operand = |
| 1399 | ToRepresentation(LConstantOperand::cast(right), |
| 1400 | instr->hydrogen()->right()->representation()); |
| 1401 | switch (instr->op()) { |
| 1402 | case Token::BIT_AND: |
| 1403 | __ andl(ToRegister(left), Immediate(right_operand)); |
| 1404 | break; |
| 1405 | case Token::BIT_OR: |
| 1406 | __ orl(ToRegister(left), Immediate(right_operand)); |
| 1407 | break; |
| 1408 | case Token::BIT_XOR: |
| 1409 | if (right_operand == int32_t(~0)) { |
| 1410 | __ notl(ToRegister(left)); |
| 1411 | } else { |
| 1412 | __ xorl(ToRegister(left), Immediate(right_operand)); |
| 1413 | } |
| 1414 | break; |
| 1415 | default: |
| 1416 | UNREACHABLE(); |
| 1417 | break; |
| 1418 | } |
| 1419 | } else if (right->IsStackSlot()) { |
| 1420 | switch (instr->op()) { |
| 1421 | case Token::BIT_AND: |
| 1422 | if (instr->IsInteger32()) { |
| 1423 | __ andl(ToRegister(left), ToOperand(right)); |
| 1424 | } else { |
| 1425 | __ andp(ToRegister(left), ToOperand(right)); |
| 1426 | } |
| 1427 | break; |
| 1428 | case Token::BIT_OR: |
| 1429 | if (instr->IsInteger32()) { |
| 1430 | __ orl(ToRegister(left), ToOperand(right)); |
| 1431 | } else { |
| 1432 | __ orp(ToRegister(left), ToOperand(right)); |
| 1433 | } |
| 1434 | break; |
| 1435 | case Token::BIT_XOR: |
| 1436 | if (instr->IsInteger32()) { |
| 1437 | __ xorl(ToRegister(left), ToOperand(right)); |
| 1438 | } else { |
| 1439 | __ xorp(ToRegister(left), ToOperand(right)); |
| 1440 | } |
| 1441 | break; |
| 1442 | default: |
| 1443 | UNREACHABLE(); |
| 1444 | break; |
| 1445 | } |
| 1446 | } else { |
| 1447 | DCHECK(right->IsRegister()); |
| 1448 | switch (instr->op()) { |
| 1449 | case Token::BIT_AND: |
| 1450 | if (instr->IsInteger32()) { |
| 1451 | __ andl(ToRegister(left), ToRegister(right)); |
| 1452 | } else { |
| 1453 | __ andp(ToRegister(left), ToRegister(right)); |
| 1454 | } |
| 1455 | break; |
| 1456 | case Token::BIT_OR: |
| 1457 | if (instr->IsInteger32()) { |
| 1458 | __ orl(ToRegister(left), ToRegister(right)); |
| 1459 | } else { |
| 1460 | __ orp(ToRegister(left), ToRegister(right)); |
| 1461 | } |
| 1462 | break; |
| 1463 | case Token::BIT_XOR: |
| 1464 | if (instr->IsInteger32()) { |
| 1465 | __ xorl(ToRegister(left), ToRegister(right)); |
| 1466 | } else { |
| 1467 | __ xorp(ToRegister(left), ToRegister(right)); |
| 1468 | } |
| 1469 | break; |
| 1470 | default: |
| 1471 | UNREACHABLE(); |
| 1472 | break; |
| 1473 | } |
| 1474 | } |
| 1475 | } |
| 1476 | |
| 1477 | |
| 1478 | void LCodeGen::DoShiftI(LShiftI* instr) { |
| 1479 | LOperand* left = instr->left(); |
| 1480 | LOperand* right = instr->right(); |
| 1481 | DCHECK(left->Equals(instr->result())); |
| 1482 | DCHECK(left->IsRegister()); |
| 1483 | if (right->IsRegister()) { |
| 1484 | DCHECK(ToRegister(right).is(rcx)); |
| 1485 | |
| 1486 | switch (instr->op()) { |
| 1487 | case Token::ROR: |
| 1488 | __ rorl_cl(ToRegister(left)); |
| 1489 | break; |
| 1490 | case Token::SAR: |
| 1491 | __ sarl_cl(ToRegister(left)); |
| 1492 | break; |
| 1493 | case Token::SHR: |
| 1494 | __ shrl_cl(ToRegister(left)); |
| 1495 | if (instr->can_deopt()) { |
| 1496 | __ testl(ToRegister(left), ToRegister(left)); |
| 1497 | DeoptimizeIf(negative, instr, Deoptimizer::kNegativeValue); |
| 1498 | } |
| 1499 | break; |
| 1500 | case Token::SHL: |
| 1501 | __ shll_cl(ToRegister(left)); |
| 1502 | break; |
| 1503 | default: |
| 1504 | UNREACHABLE(); |
| 1505 | break; |
| 1506 | } |
| 1507 | } else { |
| 1508 | int32_t value = ToInteger32(LConstantOperand::cast(right)); |
| 1509 | uint8_t shift_count = static_cast<uint8_t>(value & 0x1F); |
| 1510 | switch (instr->op()) { |
| 1511 | case Token::ROR: |
| 1512 | if (shift_count != 0) { |
| 1513 | __ rorl(ToRegister(left), Immediate(shift_count)); |
| 1514 | } |
| 1515 | break; |
| 1516 | case Token::SAR: |
| 1517 | if (shift_count != 0) { |
| 1518 | __ sarl(ToRegister(left), Immediate(shift_count)); |
| 1519 | } |
| 1520 | break; |
| 1521 | case Token::SHR: |
| 1522 | if (shift_count != 0) { |
| 1523 | __ shrl(ToRegister(left), Immediate(shift_count)); |
| 1524 | } else if (instr->can_deopt()) { |
| 1525 | __ testl(ToRegister(left), ToRegister(left)); |
| 1526 | DeoptimizeIf(negative, instr, Deoptimizer::kNegativeValue); |
| 1527 | } |
| 1528 | break; |
| 1529 | case Token::SHL: |
| 1530 | if (shift_count != 0) { |
| 1531 | if (instr->hydrogen_value()->representation().IsSmi()) { |
| 1532 | if (SmiValuesAre32Bits()) { |
| 1533 | __ shlp(ToRegister(left), Immediate(shift_count)); |
| 1534 | } else { |
| 1535 | DCHECK(SmiValuesAre31Bits()); |
| 1536 | if (instr->can_deopt()) { |
| 1537 | if (shift_count != 1) { |
| 1538 | __ shll(ToRegister(left), Immediate(shift_count - 1)); |
| 1539 | } |
| 1540 | __ Integer32ToSmi(ToRegister(left), ToRegister(left)); |
| 1541 | DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow); |
| 1542 | } else { |
| 1543 | __ shll(ToRegister(left), Immediate(shift_count)); |
| 1544 | } |
| 1545 | } |
| 1546 | } else { |
| 1547 | __ shll(ToRegister(left), Immediate(shift_count)); |
| 1548 | } |
| 1549 | } |
| 1550 | break; |
| 1551 | default: |
| 1552 | UNREACHABLE(); |
| 1553 | break; |
| 1554 | } |
| 1555 | } |
| 1556 | } |
| 1557 | |
| 1558 | |
| 1559 | void LCodeGen::DoSubI(LSubI* instr) { |
| 1560 | LOperand* left = instr->left(); |
| 1561 | LOperand* right = instr->right(); |
| 1562 | DCHECK(left->Equals(instr->result())); |
| 1563 | |
| 1564 | if (right->IsConstantOperand()) { |
| 1565 | int32_t right_operand = |
| 1566 | ToRepresentation(LConstantOperand::cast(right), |
| 1567 | instr->hydrogen()->right()->representation()); |
| 1568 | __ subl(ToRegister(left), Immediate(right_operand)); |
| 1569 | } else if (right->IsRegister()) { |
| 1570 | if (instr->hydrogen_value()->representation().IsSmi()) { |
| 1571 | __ subp(ToRegister(left), ToRegister(right)); |
| 1572 | } else { |
| 1573 | __ subl(ToRegister(left), ToRegister(right)); |
| 1574 | } |
| 1575 | } else { |
| 1576 | if (instr->hydrogen_value()->representation().IsSmi()) { |
| 1577 | __ subp(ToRegister(left), ToOperand(right)); |
| 1578 | } else { |
| 1579 | __ subl(ToRegister(left), ToOperand(right)); |
| 1580 | } |
| 1581 | } |
| 1582 | |
| 1583 | if (instr->hydrogen()->CheckFlag(HValue::kCanOverflow)) { |
| 1584 | DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow); |
| 1585 | } |
| 1586 | } |
| 1587 | |
| 1588 | |
| 1589 | void LCodeGen::DoConstantI(LConstantI* instr) { |
| 1590 | Register dst = ToRegister(instr->result()); |
| 1591 | if (instr->value() == 0) { |
| 1592 | __ xorl(dst, dst); |
| 1593 | } else { |
| 1594 | __ movl(dst, Immediate(instr->value())); |
| 1595 | } |
| 1596 | } |
| 1597 | |
| 1598 | |
| 1599 | void LCodeGen::DoConstantS(LConstantS* instr) { |
| 1600 | __ Move(ToRegister(instr->result()), instr->value()); |
| 1601 | } |
| 1602 | |
| 1603 | |
| 1604 | void LCodeGen::DoConstantD(LConstantD* instr) { |
| 1605 | __ Move(ToDoubleRegister(instr->result()), instr->bits()); |
| 1606 | } |
| 1607 | |
| 1608 | |
| 1609 | void LCodeGen::DoConstantE(LConstantE* instr) { |
| 1610 | __ LoadAddress(ToRegister(instr->result()), instr->value()); |
| 1611 | } |
| 1612 | |
| 1613 | |
| 1614 | void LCodeGen::DoConstantT(LConstantT* instr) { |
| 1615 | Handle<Object> object = instr->value(isolate()); |
| 1616 | AllowDeferredHandleDereference smi_check; |
| 1617 | __ Move(ToRegister(instr->result()), object); |
| 1618 | } |
| 1619 | |
| 1620 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1621 | Operand LCodeGen::BuildSeqStringOperand(Register string, |
| 1622 | LOperand* index, |
| 1623 | String::Encoding encoding) { |
| 1624 | if (index->IsConstantOperand()) { |
| 1625 | int offset = ToInteger32(LConstantOperand::cast(index)); |
| 1626 | if (encoding == String::TWO_BYTE_ENCODING) { |
| 1627 | offset *= kUC16Size; |
| 1628 | } |
| 1629 | STATIC_ASSERT(kCharSize == 1); |
| 1630 | return FieldOperand(string, SeqString::kHeaderSize + offset); |
| 1631 | } |
| 1632 | return FieldOperand( |
| 1633 | string, ToRegister(index), |
| 1634 | encoding == String::ONE_BYTE_ENCODING ? times_1 : times_2, |
| 1635 | SeqString::kHeaderSize); |
| 1636 | } |
| 1637 | |
| 1638 | |
| 1639 | void LCodeGen::DoSeqStringGetChar(LSeqStringGetChar* instr) { |
| 1640 | String::Encoding encoding = instr->hydrogen()->encoding(); |
| 1641 | Register result = ToRegister(instr->result()); |
| 1642 | Register string = ToRegister(instr->string()); |
| 1643 | |
| 1644 | if (FLAG_debug_code) { |
| 1645 | __ Push(string); |
| 1646 | __ movp(string, FieldOperand(string, HeapObject::kMapOffset)); |
| 1647 | __ movzxbp(string, FieldOperand(string, Map::kInstanceTypeOffset)); |
| 1648 | |
| 1649 | __ andb(string, Immediate(kStringRepresentationMask | kStringEncodingMask)); |
| 1650 | static const uint32_t one_byte_seq_type = kSeqStringTag | kOneByteStringTag; |
| 1651 | static const uint32_t two_byte_seq_type = kSeqStringTag | kTwoByteStringTag; |
| 1652 | __ cmpp(string, Immediate(encoding == String::ONE_BYTE_ENCODING |
| 1653 | ? one_byte_seq_type : two_byte_seq_type)); |
| 1654 | __ Check(equal, kUnexpectedStringType); |
| 1655 | __ Pop(string); |
| 1656 | } |
| 1657 | |
| 1658 | Operand operand = BuildSeqStringOperand(string, instr->index(), encoding); |
| 1659 | if (encoding == String::ONE_BYTE_ENCODING) { |
| 1660 | __ movzxbl(result, operand); |
| 1661 | } else { |
| 1662 | __ movzxwl(result, operand); |
| 1663 | } |
| 1664 | } |
| 1665 | |
| 1666 | |
| 1667 | void LCodeGen::DoSeqStringSetChar(LSeqStringSetChar* instr) { |
| 1668 | String::Encoding encoding = instr->hydrogen()->encoding(); |
| 1669 | Register string = ToRegister(instr->string()); |
| 1670 | |
| 1671 | if (FLAG_debug_code) { |
| 1672 | Register value = ToRegister(instr->value()); |
| 1673 | Register index = ToRegister(instr->index()); |
| 1674 | static const uint32_t one_byte_seq_type = kSeqStringTag | kOneByteStringTag; |
| 1675 | static const uint32_t two_byte_seq_type = kSeqStringTag | kTwoByteStringTag; |
| 1676 | int encoding_mask = |
| 1677 | instr->hydrogen()->encoding() == String::ONE_BYTE_ENCODING |
| 1678 | ? one_byte_seq_type : two_byte_seq_type; |
| 1679 | __ EmitSeqStringSetCharCheck(string, index, value, encoding_mask); |
| 1680 | } |
| 1681 | |
| 1682 | Operand operand = BuildSeqStringOperand(string, instr->index(), encoding); |
| 1683 | if (instr->value()->IsConstantOperand()) { |
| 1684 | int value = ToInteger32(LConstantOperand::cast(instr->value())); |
| 1685 | DCHECK_LE(0, value); |
| 1686 | if (encoding == String::ONE_BYTE_ENCODING) { |
| 1687 | DCHECK_LE(value, String::kMaxOneByteCharCode); |
| 1688 | __ movb(operand, Immediate(value)); |
| 1689 | } else { |
| 1690 | DCHECK_LE(value, String::kMaxUtf16CodeUnit); |
| 1691 | __ movw(operand, Immediate(value)); |
| 1692 | } |
| 1693 | } else { |
| 1694 | Register value = ToRegister(instr->value()); |
| 1695 | if (encoding == String::ONE_BYTE_ENCODING) { |
| 1696 | __ movb(operand, value); |
| 1697 | } else { |
| 1698 | __ movw(operand, value); |
| 1699 | } |
| 1700 | } |
| 1701 | } |
| 1702 | |
| 1703 | |
| 1704 | void LCodeGen::DoAddI(LAddI* instr) { |
| 1705 | LOperand* left = instr->left(); |
| 1706 | LOperand* right = instr->right(); |
| 1707 | |
| 1708 | Representation target_rep = instr->hydrogen()->representation(); |
| 1709 | bool is_p = target_rep.IsSmi() || target_rep.IsExternal(); |
| 1710 | |
| 1711 | if (LAddI::UseLea(instr->hydrogen()) && !left->Equals(instr->result())) { |
| 1712 | if (right->IsConstantOperand()) { |
| 1713 | // No support for smi-immediates for 32-bit SMI. |
| 1714 | DCHECK(SmiValuesAre32Bits() ? !target_rep.IsSmi() : SmiValuesAre31Bits()); |
| 1715 | int32_t offset = |
| 1716 | ToRepresentation(LConstantOperand::cast(right), |
| 1717 | instr->hydrogen()->right()->representation()); |
| 1718 | if (is_p) { |
| 1719 | __ leap(ToRegister(instr->result()), |
| 1720 | MemOperand(ToRegister(left), offset)); |
| 1721 | } else { |
| 1722 | __ leal(ToRegister(instr->result()), |
| 1723 | MemOperand(ToRegister(left), offset)); |
| 1724 | } |
| 1725 | } else { |
| 1726 | Operand address(ToRegister(left), ToRegister(right), times_1, 0); |
| 1727 | if (is_p) { |
| 1728 | __ leap(ToRegister(instr->result()), address); |
| 1729 | } else { |
| 1730 | __ leal(ToRegister(instr->result()), address); |
| 1731 | } |
| 1732 | } |
| 1733 | } else { |
| 1734 | if (right->IsConstantOperand()) { |
| 1735 | // No support for smi-immediates for 32-bit SMI. |
| 1736 | DCHECK(SmiValuesAre32Bits() ? !target_rep.IsSmi() : SmiValuesAre31Bits()); |
| 1737 | int32_t right_operand = |
| 1738 | ToRepresentation(LConstantOperand::cast(right), |
| 1739 | instr->hydrogen()->right()->representation()); |
| 1740 | if (is_p) { |
| 1741 | __ addp(ToRegister(left), Immediate(right_operand)); |
| 1742 | } else { |
| 1743 | __ addl(ToRegister(left), Immediate(right_operand)); |
| 1744 | } |
| 1745 | } else if (right->IsRegister()) { |
| 1746 | if (is_p) { |
| 1747 | __ addp(ToRegister(left), ToRegister(right)); |
| 1748 | } else { |
| 1749 | __ addl(ToRegister(left), ToRegister(right)); |
| 1750 | } |
| 1751 | } else { |
| 1752 | if (is_p) { |
| 1753 | __ addp(ToRegister(left), ToOperand(right)); |
| 1754 | } else { |
| 1755 | __ addl(ToRegister(left), ToOperand(right)); |
| 1756 | } |
| 1757 | } |
| 1758 | if (instr->hydrogen()->CheckFlag(HValue::kCanOverflow)) { |
| 1759 | DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow); |
| 1760 | } |
| 1761 | } |
| 1762 | } |
| 1763 | |
| 1764 | |
| 1765 | void LCodeGen::DoMathMinMax(LMathMinMax* instr) { |
| 1766 | LOperand* left = instr->left(); |
| 1767 | LOperand* right = instr->right(); |
| 1768 | DCHECK(left->Equals(instr->result())); |
| 1769 | HMathMinMax::Operation operation = instr->hydrogen()->operation(); |
| 1770 | if (instr->hydrogen()->representation().IsSmiOrInteger32()) { |
| 1771 | Label return_left; |
| 1772 | Condition condition = (operation == HMathMinMax::kMathMin) |
| 1773 | ? less_equal |
| 1774 | : greater_equal; |
| 1775 | Register left_reg = ToRegister(left); |
| 1776 | if (right->IsConstantOperand()) { |
| 1777 | Immediate right_imm = Immediate( |
| 1778 | ToRepresentation(LConstantOperand::cast(right), |
| 1779 | instr->hydrogen()->right()->representation())); |
| 1780 | DCHECK(SmiValuesAre32Bits() |
| 1781 | ? !instr->hydrogen()->representation().IsSmi() |
| 1782 | : SmiValuesAre31Bits()); |
| 1783 | __ cmpl(left_reg, right_imm); |
| 1784 | __ j(condition, &return_left, Label::kNear); |
| 1785 | __ movp(left_reg, right_imm); |
| 1786 | } else if (right->IsRegister()) { |
| 1787 | Register right_reg = ToRegister(right); |
| 1788 | if (instr->hydrogen_value()->representation().IsSmi()) { |
| 1789 | __ cmpp(left_reg, right_reg); |
| 1790 | } else { |
| 1791 | __ cmpl(left_reg, right_reg); |
| 1792 | } |
| 1793 | __ j(condition, &return_left, Label::kNear); |
| 1794 | __ movp(left_reg, right_reg); |
| 1795 | } else { |
| 1796 | Operand right_op = ToOperand(right); |
| 1797 | if (instr->hydrogen_value()->representation().IsSmi()) { |
| 1798 | __ cmpp(left_reg, right_op); |
| 1799 | } else { |
| 1800 | __ cmpl(left_reg, right_op); |
| 1801 | } |
| 1802 | __ j(condition, &return_left, Label::kNear); |
| 1803 | __ movp(left_reg, right_op); |
| 1804 | } |
| 1805 | __ bind(&return_left); |
| 1806 | } else { |
| 1807 | DCHECK(instr->hydrogen()->representation().IsDouble()); |
| 1808 | Label not_nan, distinct, return_left, return_right; |
| 1809 | Condition condition = (operation == HMathMinMax::kMathMin) ? below : above; |
| 1810 | XMMRegister left_reg = ToDoubleRegister(left); |
| 1811 | XMMRegister right_reg = ToDoubleRegister(right); |
| 1812 | __ Ucomisd(left_reg, right_reg); |
| 1813 | __ j(parity_odd, ¬_nan, Label::kNear); // Both are not NaN. |
| 1814 | |
| 1815 | // One of the numbers is NaN. Find which one and return it. |
| 1816 | __ Ucomisd(left_reg, left_reg); |
| 1817 | __ j(parity_even, &return_left, Label::kNear); // left is NaN. |
| 1818 | __ jmp(&return_right, Label::kNear); // right is NaN. |
| 1819 | |
| 1820 | __ bind(¬_nan); |
| 1821 | __ j(not_equal, &distinct, Label::kNear); // left != right. |
| 1822 | |
| 1823 | // left == right |
| 1824 | XMMRegister xmm_scratch = double_scratch0(); |
| 1825 | __ Xorpd(xmm_scratch, xmm_scratch); |
| 1826 | __ Ucomisd(left_reg, xmm_scratch); |
| 1827 | __ j(not_equal, &return_left, Label::kNear); // left == right != 0. |
| 1828 | |
| 1829 | // At this point, both left and right are either +0 or -0. |
| 1830 | if (operation == HMathMinMax::kMathMin) { |
| 1831 | __ Orpd(left_reg, right_reg); |
| 1832 | } else { |
| 1833 | __ Andpd(left_reg, right_reg); |
| 1834 | } |
| 1835 | __ jmp(&return_left, Label::kNear); |
| 1836 | |
| 1837 | __ bind(&distinct); |
| 1838 | __ j(condition, &return_left, Label::kNear); |
| 1839 | |
| 1840 | __ bind(&return_right); |
| 1841 | __ Movapd(left_reg, right_reg); |
| 1842 | |
| 1843 | __ bind(&return_left); |
| 1844 | } |
| 1845 | } |
| 1846 | |
| 1847 | |
| 1848 | void LCodeGen::DoArithmeticD(LArithmeticD* instr) { |
| 1849 | XMMRegister left = ToDoubleRegister(instr->left()); |
| 1850 | XMMRegister right = ToDoubleRegister(instr->right()); |
| 1851 | XMMRegister result = ToDoubleRegister(instr->result()); |
| 1852 | switch (instr->op()) { |
| 1853 | case Token::ADD: |
| 1854 | if (CpuFeatures::IsSupported(AVX)) { |
| 1855 | CpuFeatureScope scope(masm(), AVX); |
| 1856 | __ vaddsd(result, left, right); |
| 1857 | } else { |
| 1858 | DCHECK(result.is(left)); |
| 1859 | __ addsd(left, right); |
| 1860 | } |
| 1861 | break; |
| 1862 | case Token::SUB: |
| 1863 | if (CpuFeatures::IsSupported(AVX)) { |
| 1864 | CpuFeatureScope scope(masm(), AVX); |
| 1865 | __ vsubsd(result, left, right); |
| 1866 | } else { |
| 1867 | DCHECK(result.is(left)); |
| 1868 | __ subsd(left, right); |
| 1869 | } |
| 1870 | break; |
| 1871 | case Token::MUL: |
| 1872 | if (CpuFeatures::IsSupported(AVX)) { |
| 1873 | CpuFeatureScope scope(masm(), AVX); |
| 1874 | __ vmulsd(result, left, right); |
| 1875 | } else { |
| 1876 | DCHECK(result.is(left)); |
| 1877 | __ mulsd(left, right); |
| 1878 | } |
| 1879 | break; |
| 1880 | case Token::DIV: |
| 1881 | if (CpuFeatures::IsSupported(AVX)) { |
| 1882 | CpuFeatureScope scope(masm(), AVX); |
| 1883 | __ vdivsd(result, left, right); |
| 1884 | } else { |
| 1885 | DCHECK(result.is(left)); |
| 1886 | __ divsd(left, right); |
| 1887 | } |
| 1888 | // Don't delete this mov. It may improve performance on some CPUs, |
| 1889 | // when there is a (v)mulsd depending on the result |
| 1890 | __ Movapd(result, result); |
| 1891 | break; |
| 1892 | case Token::MOD: { |
| 1893 | XMMRegister xmm_scratch = double_scratch0(); |
| 1894 | __ PrepareCallCFunction(2); |
| 1895 | __ Movapd(xmm_scratch, left); |
| 1896 | DCHECK(right.is(xmm1)); |
| 1897 | __ CallCFunction( |
| 1898 | ExternalReference::mod_two_doubles_operation(isolate()), 2); |
| 1899 | __ Movapd(result, xmm_scratch); |
| 1900 | break; |
| 1901 | } |
| 1902 | default: |
| 1903 | UNREACHABLE(); |
| 1904 | break; |
| 1905 | } |
| 1906 | } |
| 1907 | |
| 1908 | |
| 1909 | void LCodeGen::DoArithmeticT(LArithmeticT* instr) { |
| 1910 | DCHECK(ToRegister(instr->context()).is(rsi)); |
| 1911 | DCHECK(ToRegister(instr->left()).is(rdx)); |
| 1912 | DCHECK(ToRegister(instr->right()).is(rax)); |
| 1913 | DCHECK(ToRegister(instr->result()).is(rax)); |
| 1914 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1915 | Handle<Code> code = CodeFactory::BinaryOpIC(isolate(), instr->op()).code(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1916 | CallCode(code, RelocInfo::CODE_TARGET, instr); |
| 1917 | } |
| 1918 | |
| 1919 | |
| 1920 | template<class InstrType> |
| 1921 | void LCodeGen::EmitBranch(InstrType instr, Condition cc) { |
| 1922 | int left_block = instr->TrueDestination(chunk_); |
| 1923 | int right_block = instr->FalseDestination(chunk_); |
| 1924 | |
| 1925 | int next_block = GetNextEmittedBlock(); |
| 1926 | |
| 1927 | if (right_block == left_block || cc == no_condition) { |
| 1928 | EmitGoto(left_block); |
| 1929 | } else if (left_block == next_block) { |
| 1930 | __ j(NegateCondition(cc), chunk_->GetAssemblyLabel(right_block)); |
| 1931 | } else if (right_block == next_block) { |
| 1932 | __ j(cc, chunk_->GetAssemblyLabel(left_block)); |
| 1933 | } else { |
| 1934 | __ j(cc, chunk_->GetAssemblyLabel(left_block)); |
| 1935 | if (cc != always) { |
| 1936 | __ jmp(chunk_->GetAssemblyLabel(right_block)); |
| 1937 | } |
| 1938 | } |
| 1939 | } |
| 1940 | |
| 1941 | |
| 1942 | template <class InstrType> |
| 1943 | void LCodeGen::EmitTrueBranch(InstrType instr, Condition cc) { |
| 1944 | int true_block = instr->TrueDestination(chunk_); |
| 1945 | __ j(cc, chunk_->GetAssemblyLabel(true_block)); |
| 1946 | } |
| 1947 | |
| 1948 | |
| 1949 | template <class InstrType> |
| 1950 | void LCodeGen::EmitFalseBranch(InstrType instr, Condition cc) { |
| 1951 | int false_block = instr->FalseDestination(chunk_); |
| 1952 | __ j(cc, chunk_->GetAssemblyLabel(false_block)); |
| 1953 | } |
| 1954 | |
| 1955 | |
| 1956 | void LCodeGen::DoDebugBreak(LDebugBreak* instr) { |
| 1957 | __ int3(); |
| 1958 | } |
| 1959 | |
| 1960 | |
| 1961 | void LCodeGen::DoBranch(LBranch* instr) { |
| 1962 | Representation r = instr->hydrogen()->value()->representation(); |
| 1963 | if (r.IsInteger32()) { |
| 1964 | DCHECK(!info()->IsStub()); |
| 1965 | Register reg = ToRegister(instr->value()); |
| 1966 | __ testl(reg, reg); |
| 1967 | EmitBranch(instr, not_zero); |
| 1968 | } else if (r.IsSmi()) { |
| 1969 | DCHECK(!info()->IsStub()); |
| 1970 | Register reg = ToRegister(instr->value()); |
| 1971 | __ testp(reg, reg); |
| 1972 | EmitBranch(instr, not_zero); |
| 1973 | } else if (r.IsDouble()) { |
| 1974 | DCHECK(!info()->IsStub()); |
| 1975 | XMMRegister reg = ToDoubleRegister(instr->value()); |
| 1976 | XMMRegister xmm_scratch = double_scratch0(); |
| 1977 | __ Xorpd(xmm_scratch, xmm_scratch); |
| 1978 | __ Ucomisd(reg, xmm_scratch); |
| 1979 | EmitBranch(instr, not_equal); |
| 1980 | } else { |
| 1981 | DCHECK(r.IsTagged()); |
| 1982 | Register reg = ToRegister(instr->value()); |
| 1983 | HType type = instr->hydrogen()->value()->type(); |
| 1984 | if (type.IsBoolean()) { |
| 1985 | DCHECK(!info()->IsStub()); |
| 1986 | __ CompareRoot(reg, Heap::kTrueValueRootIndex); |
| 1987 | EmitBranch(instr, equal); |
| 1988 | } else if (type.IsSmi()) { |
| 1989 | DCHECK(!info()->IsStub()); |
| 1990 | __ SmiCompare(reg, Smi::FromInt(0)); |
| 1991 | EmitBranch(instr, not_equal); |
| 1992 | } else if (type.IsJSArray()) { |
| 1993 | DCHECK(!info()->IsStub()); |
| 1994 | EmitBranch(instr, no_condition); |
| 1995 | } else if (type.IsHeapNumber()) { |
| 1996 | DCHECK(!info()->IsStub()); |
| 1997 | XMMRegister xmm_scratch = double_scratch0(); |
| 1998 | __ Xorpd(xmm_scratch, xmm_scratch); |
| 1999 | __ Ucomisd(xmm_scratch, FieldOperand(reg, HeapNumber::kValueOffset)); |
| 2000 | EmitBranch(instr, not_equal); |
| 2001 | } else if (type.IsString()) { |
| 2002 | DCHECK(!info()->IsStub()); |
| 2003 | __ cmpp(FieldOperand(reg, String::kLengthOffset), Immediate(0)); |
| 2004 | EmitBranch(instr, not_equal); |
| 2005 | } else { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2006 | ToBooleanICStub::Types expected = |
| 2007 | instr->hydrogen()->expected_input_types(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2008 | // Avoid deopts in the case where we've never executed this path before. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2009 | if (expected.IsEmpty()) expected = ToBooleanICStub::Types::Generic(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2010 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2011 | if (expected.Contains(ToBooleanICStub::UNDEFINED)) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2012 | // undefined -> false. |
| 2013 | __ CompareRoot(reg, Heap::kUndefinedValueRootIndex); |
| 2014 | __ j(equal, instr->FalseLabel(chunk_)); |
| 2015 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2016 | if (expected.Contains(ToBooleanICStub::BOOLEAN)) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2017 | // true -> true. |
| 2018 | __ CompareRoot(reg, Heap::kTrueValueRootIndex); |
| 2019 | __ j(equal, instr->TrueLabel(chunk_)); |
| 2020 | // false -> false. |
| 2021 | __ CompareRoot(reg, Heap::kFalseValueRootIndex); |
| 2022 | __ j(equal, instr->FalseLabel(chunk_)); |
| 2023 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2024 | if (expected.Contains(ToBooleanICStub::NULL_TYPE)) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2025 | // 'null' -> false. |
| 2026 | __ CompareRoot(reg, Heap::kNullValueRootIndex); |
| 2027 | __ j(equal, instr->FalseLabel(chunk_)); |
| 2028 | } |
| 2029 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2030 | if (expected.Contains(ToBooleanICStub::SMI)) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2031 | // Smis: 0 -> false, all other -> true. |
| 2032 | __ Cmp(reg, Smi::FromInt(0)); |
| 2033 | __ j(equal, instr->FalseLabel(chunk_)); |
| 2034 | __ JumpIfSmi(reg, instr->TrueLabel(chunk_)); |
| 2035 | } else if (expected.NeedsMap()) { |
| 2036 | // If we need a map later and have a Smi -> deopt. |
| 2037 | __ testb(reg, Immediate(kSmiTagMask)); |
| 2038 | DeoptimizeIf(zero, instr, Deoptimizer::kSmi); |
| 2039 | } |
| 2040 | |
| 2041 | const Register map = kScratchRegister; |
| 2042 | if (expected.NeedsMap()) { |
| 2043 | __ movp(map, FieldOperand(reg, HeapObject::kMapOffset)); |
| 2044 | |
| 2045 | if (expected.CanBeUndetectable()) { |
| 2046 | // Undetectable -> false. |
| 2047 | __ testb(FieldOperand(map, Map::kBitFieldOffset), |
| 2048 | Immediate(1 << Map::kIsUndetectable)); |
| 2049 | __ j(not_zero, instr->FalseLabel(chunk_)); |
| 2050 | } |
| 2051 | } |
| 2052 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2053 | if (expected.Contains(ToBooleanICStub::SPEC_OBJECT)) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2054 | // spec object -> true. |
| 2055 | __ CmpInstanceType(map, FIRST_JS_RECEIVER_TYPE); |
| 2056 | __ j(above_equal, instr->TrueLabel(chunk_)); |
| 2057 | } |
| 2058 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2059 | if (expected.Contains(ToBooleanICStub::STRING)) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2060 | // String value -> false iff empty. |
| 2061 | Label not_string; |
| 2062 | __ CmpInstanceType(map, FIRST_NONSTRING_TYPE); |
| 2063 | __ j(above_equal, ¬_string, Label::kNear); |
| 2064 | __ cmpp(FieldOperand(reg, String::kLengthOffset), Immediate(0)); |
| 2065 | __ j(not_zero, instr->TrueLabel(chunk_)); |
| 2066 | __ jmp(instr->FalseLabel(chunk_)); |
| 2067 | __ bind(¬_string); |
| 2068 | } |
| 2069 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2070 | if (expected.Contains(ToBooleanICStub::SYMBOL)) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2071 | // Symbol value -> true. |
| 2072 | __ CmpInstanceType(map, SYMBOL_TYPE); |
| 2073 | __ j(equal, instr->TrueLabel(chunk_)); |
| 2074 | } |
| 2075 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2076 | if (expected.Contains(ToBooleanICStub::SIMD_VALUE)) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2077 | // SIMD value -> true. |
| 2078 | __ CmpInstanceType(map, SIMD128_VALUE_TYPE); |
| 2079 | __ j(equal, instr->TrueLabel(chunk_)); |
| 2080 | } |
| 2081 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2082 | if (expected.Contains(ToBooleanICStub::HEAP_NUMBER)) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2083 | // heap number -> false iff +0, -0, or NaN. |
| 2084 | Label not_heap_number; |
| 2085 | __ CompareRoot(map, Heap::kHeapNumberMapRootIndex); |
| 2086 | __ j(not_equal, ¬_heap_number, Label::kNear); |
| 2087 | XMMRegister xmm_scratch = double_scratch0(); |
| 2088 | __ Xorpd(xmm_scratch, xmm_scratch); |
| 2089 | __ Ucomisd(xmm_scratch, FieldOperand(reg, HeapNumber::kValueOffset)); |
| 2090 | __ j(zero, instr->FalseLabel(chunk_)); |
| 2091 | __ jmp(instr->TrueLabel(chunk_)); |
| 2092 | __ bind(¬_heap_number); |
| 2093 | } |
| 2094 | |
| 2095 | if (!expected.IsGeneric()) { |
| 2096 | // We've seen something for the first time -> deopt. |
| 2097 | // This can only happen if we are not generic already. |
| 2098 | DeoptimizeIf(no_condition, instr, Deoptimizer::kUnexpectedObject); |
| 2099 | } |
| 2100 | } |
| 2101 | } |
| 2102 | } |
| 2103 | |
| 2104 | |
| 2105 | void LCodeGen::EmitGoto(int block) { |
| 2106 | if (!IsNextEmittedBlock(block)) { |
| 2107 | __ jmp(chunk_->GetAssemblyLabel(chunk_->LookupDestination(block))); |
| 2108 | } |
| 2109 | } |
| 2110 | |
| 2111 | |
| 2112 | void LCodeGen::DoGoto(LGoto* instr) { |
| 2113 | EmitGoto(instr->block_id()); |
| 2114 | } |
| 2115 | |
| 2116 | |
| 2117 | inline Condition LCodeGen::TokenToCondition(Token::Value op, bool is_unsigned) { |
| 2118 | Condition cond = no_condition; |
| 2119 | switch (op) { |
| 2120 | case Token::EQ: |
| 2121 | case Token::EQ_STRICT: |
| 2122 | cond = equal; |
| 2123 | break; |
| 2124 | case Token::NE: |
| 2125 | case Token::NE_STRICT: |
| 2126 | cond = not_equal; |
| 2127 | break; |
| 2128 | case Token::LT: |
| 2129 | cond = is_unsigned ? below : less; |
| 2130 | break; |
| 2131 | case Token::GT: |
| 2132 | cond = is_unsigned ? above : greater; |
| 2133 | break; |
| 2134 | case Token::LTE: |
| 2135 | cond = is_unsigned ? below_equal : less_equal; |
| 2136 | break; |
| 2137 | case Token::GTE: |
| 2138 | cond = is_unsigned ? above_equal : greater_equal; |
| 2139 | break; |
| 2140 | case Token::IN: |
| 2141 | case Token::INSTANCEOF: |
| 2142 | default: |
| 2143 | UNREACHABLE(); |
| 2144 | } |
| 2145 | return cond; |
| 2146 | } |
| 2147 | |
| 2148 | |
| 2149 | void LCodeGen::DoCompareNumericAndBranch(LCompareNumericAndBranch* instr) { |
| 2150 | LOperand* left = instr->left(); |
| 2151 | LOperand* right = instr->right(); |
| 2152 | bool is_unsigned = |
| 2153 | instr->is_double() || |
| 2154 | instr->hydrogen()->left()->CheckFlag(HInstruction::kUint32) || |
| 2155 | instr->hydrogen()->right()->CheckFlag(HInstruction::kUint32); |
| 2156 | Condition cc = TokenToCondition(instr->op(), is_unsigned); |
| 2157 | |
| 2158 | if (left->IsConstantOperand() && right->IsConstantOperand()) { |
| 2159 | // We can statically evaluate the comparison. |
| 2160 | double left_val = ToDouble(LConstantOperand::cast(left)); |
| 2161 | double right_val = ToDouble(LConstantOperand::cast(right)); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2162 | int next_block = Token::EvalComparison(instr->op(), left_val, right_val) |
| 2163 | ? instr->TrueDestination(chunk_) |
| 2164 | : instr->FalseDestination(chunk_); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2165 | EmitGoto(next_block); |
| 2166 | } else { |
| 2167 | if (instr->is_double()) { |
| 2168 | // Don't base result on EFLAGS when a NaN is involved. Instead |
| 2169 | // jump to the false block. |
| 2170 | __ Ucomisd(ToDoubleRegister(left), ToDoubleRegister(right)); |
| 2171 | __ j(parity_even, instr->FalseLabel(chunk_)); |
| 2172 | } else { |
| 2173 | int32_t value; |
| 2174 | if (right->IsConstantOperand()) { |
| 2175 | value = ToInteger32(LConstantOperand::cast(right)); |
| 2176 | if (instr->hydrogen_value()->representation().IsSmi()) { |
| 2177 | __ Cmp(ToRegister(left), Smi::FromInt(value)); |
| 2178 | } else { |
| 2179 | __ cmpl(ToRegister(left), Immediate(value)); |
| 2180 | } |
| 2181 | } else if (left->IsConstantOperand()) { |
| 2182 | value = ToInteger32(LConstantOperand::cast(left)); |
| 2183 | if (instr->hydrogen_value()->representation().IsSmi()) { |
| 2184 | if (right->IsRegister()) { |
| 2185 | __ Cmp(ToRegister(right), Smi::FromInt(value)); |
| 2186 | } else { |
| 2187 | __ Cmp(ToOperand(right), Smi::FromInt(value)); |
| 2188 | } |
| 2189 | } else if (right->IsRegister()) { |
| 2190 | __ cmpl(ToRegister(right), Immediate(value)); |
| 2191 | } else { |
| 2192 | __ cmpl(ToOperand(right), Immediate(value)); |
| 2193 | } |
| 2194 | // We commuted the operands, so commute the condition. |
| 2195 | cc = CommuteCondition(cc); |
| 2196 | } else if (instr->hydrogen_value()->representation().IsSmi()) { |
| 2197 | if (right->IsRegister()) { |
| 2198 | __ cmpp(ToRegister(left), ToRegister(right)); |
| 2199 | } else { |
| 2200 | __ cmpp(ToRegister(left), ToOperand(right)); |
| 2201 | } |
| 2202 | } else { |
| 2203 | if (right->IsRegister()) { |
| 2204 | __ cmpl(ToRegister(left), ToRegister(right)); |
| 2205 | } else { |
| 2206 | __ cmpl(ToRegister(left), ToOperand(right)); |
| 2207 | } |
| 2208 | } |
| 2209 | } |
| 2210 | EmitBranch(instr, cc); |
| 2211 | } |
| 2212 | } |
| 2213 | |
| 2214 | |
| 2215 | void LCodeGen::DoCmpObjectEqAndBranch(LCmpObjectEqAndBranch* instr) { |
| 2216 | Register left = ToRegister(instr->left()); |
| 2217 | |
| 2218 | if (instr->right()->IsConstantOperand()) { |
| 2219 | Handle<Object> right = ToHandle(LConstantOperand::cast(instr->right())); |
| 2220 | __ Cmp(left, right); |
| 2221 | } else { |
| 2222 | Register right = ToRegister(instr->right()); |
| 2223 | __ cmpp(left, right); |
| 2224 | } |
| 2225 | EmitBranch(instr, equal); |
| 2226 | } |
| 2227 | |
| 2228 | |
| 2229 | void LCodeGen::DoCmpHoleAndBranch(LCmpHoleAndBranch* instr) { |
| 2230 | if (instr->hydrogen()->representation().IsTagged()) { |
| 2231 | Register input_reg = ToRegister(instr->object()); |
| 2232 | __ Cmp(input_reg, factory()->the_hole_value()); |
| 2233 | EmitBranch(instr, equal); |
| 2234 | return; |
| 2235 | } |
| 2236 | |
| 2237 | XMMRegister input_reg = ToDoubleRegister(instr->object()); |
| 2238 | __ Ucomisd(input_reg, input_reg); |
| 2239 | EmitFalseBranch(instr, parity_odd); |
| 2240 | |
| 2241 | __ subp(rsp, Immediate(kDoubleSize)); |
| 2242 | __ Movsd(MemOperand(rsp, 0), input_reg); |
| 2243 | __ addp(rsp, Immediate(kDoubleSize)); |
| 2244 | |
| 2245 | int offset = sizeof(kHoleNanUpper32); |
| 2246 | __ cmpl(MemOperand(rsp, -offset), Immediate(kHoleNanUpper32)); |
| 2247 | EmitBranch(instr, equal); |
| 2248 | } |
| 2249 | |
| 2250 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2251 | Condition LCodeGen::EmitIsString(Register input, |
| 2252 | Register temp1, |
| 2253 | Label* is_not_string, |
| 2254 | SmiCheck check_needed = INLINE_SMI_CHECK) { |
| 2255 | if (check_needed == INLINE_SMI_CHECK) { |
| 2256 | __ JumpIfSmi(input, is_not_string); |
| 2257 | } |
| 2258 | |
| 2259 | Condition cond = masm_->IsObjectStringType(input, temp1, temp1); |
| 2260 | |
| 2261 | return cond; |
| 2262 | } |
| 2263 | |
| 2264 | |
| 2265 | void LCodeGen::DoIsStringAndBranch(LIsStringAndBranch* instr) { |
| 2266 | Register reg = ToRegister(instr->value()); |
| 2267 | Register temp = ToRegister(instr->temp()); |
| 2268 | |
| 2269 | SmiCheck check_needed = |
| 2270 | instr->hydrogen()->value()->type().IsHeapObject() |
| 2271 | ? OMIT_SMI_CHECK : INLINE_SMI_CHECK; |
| 2272 | |
| 2273 | Condition true_cond = EmitIsString( |
| 2274 | reg, temp, instr->FalseLabel(chunk_), check_needed); |
| 2275 | |
| 2276 | EmitBranch(instr, true_cond); |
| 2277 | } |
| 2278 | |
| 2279 | |
| 2280 | void LCodeGen::DoIsSmiAndBranch(LIsSmiAndBranch* instr) { |
| 2281 | Condition is_smi; |
| 2282 | if (instr->value()->IsRegister()) { |
| 2283 | Register input = ToRegister(instr->value()); |
| 2284 | is_smi = masm()->CheckSmi(input); |
| 2285 | } else { |
| 2286 | Operand input = ToOperand(instr->value()); |
| 2287 | is_smi = masm()->CheckSmi(input); |
| 2288 | } |
| 2289 | EmitBranch(instr, is_smi); |
| 2290 | } |
| 2291 | |
| 2292 | |
| 2293 | void LCodeGen::DoIsUndetectableAndBranch(LIsUndetectableAndBranch* instr) { |
| 2294 | Register input = ToRegister(instr->value()); |
| 2295 | Register temp = ToRegister(instr->temp()); |
| 2296 | |
| 2297 | if (!instr->hydrogen()->value()->type().IsHeapObject()) { |
| 2298 | __ JumpIfSmi(input, instr->FalseLabel(chunk_)); |
| 2299 | } |
| 2300 | __ movp(temp, FieldOperand(input, HeapObject::kMapOffset)); |
| 2301 | __ testb(FieldOperand(temp, Map::kBitFieldOffset), |
| 2302 | Immediate(1 << Map::kIsUndetectable)); |
| 2303 | EmitBranch(instr, not_zero); |
| 2304 | } |
| 2305 | |
| 2306 | |
| 2307 | void LCodeGen::DoStringCompareAndBranch(LStringCompareAndBranch* instr) { |
| 2308 | DCHECK(ToRegister(instr->context()).is(rsi)); |
| 2309 | DCHECK(ToRegister(instr->left()).is(rdx)); |
| 2310 | DCHECK(ToRegister(instr->right()).is(rax)); |
| 2311 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2312 | Handle<Code> code = CodeFactory::StringCompare(isolate(), instr->op()).code(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2313 | CallCode(code, RelocInfo::CODE_TARGET, instr); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2314 | __ CompareRoot(rax, Heap::kTrueValueRootIndex); |
| 2315 | EmitBranch(instr, equal); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2316 | } |
| 2317 | |
| 2318 | |
| 2319 | static InstanceType TestType(HHasInstanceTypeAndBranch* instr) { |
| 2320 | InstanceType from = instr->from(); |
| 2321 | InstanceType to = instr->to(); |
| 2322 | if (from == FIRST_TYPE) return to; |
| 2323 | DCHECK(from == to || to == LAST_TYPE); |
| 2324 | return from; |
| 2325 | } |
| 2326 | |
| 2327 | |
| 2328 | static Condition BranchCondition(HHasInstanceTypeAndBranch* instr) { |
| 2329 | InstanceType from = instr->from(); |
| 2330 | InstanceType to = instr->to(); |
| 2331 | if (from == to) return equal; |
| 2332 | if (to == LAST_TYPE) return above_equal; |
| 2333 | if (from == FIRST_TYPE) return below_equal; |
| 2334 | UNREACHABLE(); |
| 2335 | return equal; |
| 2336 | } |
| 2337 | |
| 2338 | |
| 2339 | void LCodeGen::DoHasInstanceTypeAndBranch(LHasInstanceTypeAndBranch* instr) { |
| 2340 | Register input = ToRegister(instr->value()); |
| 2341 | |
| 2342 | if (!instr->hydrogen()->value()->type().IsHeapObject()) { |
| 2343 | __ JumpIfSmi(input, instr->FalseLabel(chunk_)); |
| 2344 | } |
| 2345 | |
| 2346 | __ CmpObjectType(input, TestType(instr->hydrogen()), kScratchRegister); |
| 2347 | EmitBranch(instr, BranchCondition(instr->hydrogen())); |
| 2348 | } |
| 2349 | |
| 2350 | |
| 2351 | void LCodeGen::DoGetCachedArrayIndex(LGetCachedArrayIndex* instr) { |
| 2352 | Register input = ToRegister(instr->value()); |
| 2353 | Register result = ToRegister(instr->result()); |
| 2354 | |
| 2355 | __ AssertString(input); |
| 2356 | |
| 2357 | __ movl(result, FieldOperand(input, String::kHashFieldOffset)); |
| 2358 | DCHECK(String::kHashShift >= kSmiTagSize); |
| 2359 | __ IndexFromHash(result, result); |
| 2360 | } |
| 2361 | |
| 2362 | |
| 2363 | void LCodeGen::DoHasCachedArrayIndexAndBranch( |
| 2364 | LHasCachedArrayIndexAndBranch* instr) { |
| 2365 | Register input = ToRegister(instr->value()); |
| 2366 | |
| 2367 | __ testl(FieldOperand(input, String::kHashFieldOffset), |
| 2368 | Immediate(String::kContainsCachedArrayIndexMask)); |
| 2369 | EmitBranch(instr, equal); |
| 2370 | } |
| 2371 | |
| 2372 | |
| 2373 | // Branches to a label or falls through with the answer in the z flag. |
| 2374 | // Trashes the temp register. |
| 2375 | void LCodeGen::EmitClassOfTest(Label* is_true, |
| 2376 | Label* is_false, |
| 2377 | Handle<String> class_name, |
| 2378 | Register input, |
| 2379 | Register temp, |
| 2380 | Register temp2) { |
| 2381 | DCHECK(!input.is(temp)); |
| 2382 | DCHECK(!input.is(temp2)); |
| 2383 | DCHECK(!temp.is(temp2)); |
| 2384 | |
| 2385 | __ JumpIfSmi(input, is_false); |
| 2386 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2387 | __ CmpObjectType(input, FIRST_FUNCTION_TYPE, temp); |
| 2388 | STATIC_ASSERT(LAST_FUNCTION_TYPE == LAST_TYPE); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2389 | if (String::Equals(isolate()->factory()->Function_string(), class_name)) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2390 | __ j(above_equal, is_true); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2391 | } else { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2392 | __ j(above_equal, is_false); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2393 | } |
| 2394 | |
| 2395 | // Check if the constructor in the map is a function. |
| 2396 | __ GetMapConstructor(temp, temp, kScratchRegister); |
| 2397 | |
| 2398 | // Objects with a non-function constructor have class 'Object'. |
| 2399 | __ CmpInstanceType(kScratchRegister, JS_FUNCTION_TYPE); |
| 2400 | if (String::Equals(class_name, isolate()->factory()->Object_string())) { |
| 2401 | __ j(not_equal, is_true); |
| 2402 | } else { |
| 2403 | __ j(not_equal, is_false); |
| 2404 | } |
| 2405 | |
| 2406 | // temp now contains the constructor function. Grab the |
| 2407 | // instance class name from there. |
| 2408 | __ movp(temp, FieldOperand(temp, JSFunction::kSharedFunctionInfoOffset)); |
| 2409 | __ movp(temp, FieldOperand(temp, |
| 2410 | SharedFunctionInfo::kInstanceClassNameOffset)); |
| 2411 | // The class name we are testing against is internalized since it's a literal. |
| 2412 | // The name in the constructor is internalized because of the way the context |
| 2413 | // is booted. This routine isn't expected to work for random API-created |
| 2414 | // classes and it doesn't have to because you can't access it with natives |
| 2415 | // syntax. Since both sides are internalized it is sufficient to use an |
| 2416 | // identity comparison. |
| 2417 | DCHECK(class_name->IsInternalizedString()); |
| 2418 | __ Cmp(temp, class_name); |
| 2419 | // End with the answer in the z flag. |
| 2420 | } |
| 2421 | |
| 2422 | |
| 2423 | void LCodeGen::DoClassOfTestAndBranch(LClassOfTestAndBranch* instr) { |
| 2424 | Register input = ToRegister(instr->value()); |
| 2425 | Register temp = ToRegister(instr->temp()); |
| 2426 | Register temp2 = ToRegister(instr->temp2()); |
| 2427 | Handle<String> class_name = instr->hydrogen()->class_name(); |
| 2428 | |
| 2429 | EmitClassOfTest(instr->TrueLabel(chunk_), instr->FalseLabel(chunk_), |
| 2430 | class_name, input, temp, temp2); |
| 2431 | |
| 2432 | EmitBranch(instr, equal); |
| 2433 | } |
| 2434 | |
| 2435 | |
| 2436 | void LCodeGen::DoCmpMapAndBranch(LCmpMapAndBranch* instr) { |
| 2437 | Register reg = ToRegister(instr->value()); |
| 2438 | |
| 2439 | __ Cmp(FieldOperand(reg, HeapObject::kMapOffset), instr->map()); |
| 2440 | EmitBranch(instr, equal); |
| 2441 | } |
| 2442 | |
| 2443 | |
| 2444 | void LCodeGen::DoInstanceOf(LInstanceOf* instr) { |
| 2445 | DCHECK(ToRegister(instr->context()).is(rsi)); |
| 2446 | DCHECK(ToRegister(instr->left()).is(InstanceOfDescriptor::LeftRegister())); |
| 2447 | DCHECK(ToRegister(instr->right()).is(InstanceOfDescriptor::RightRegister())); |
| 2448 | DCHECK(ToRegister(instr->result()).is(rax)); |
| 2449 | InstanceOfStub stub(isolate()); |
| 2450 | CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr); |
| 2451 | } |
| 2452 | |
| 2453 | |
| 2454 | void LCodeGen::DoHasInPrototypeChainAndBranch( |
| 2455 | LHasInPrototypeChainAndBranch* instr) { |
| 2456 | Register const object = ToRegister(instr->object()); |
| 2457 | Register const object_map = kScratchRegister; |
| 2458 | Register const object_prototype = object_map; |
| 2459 | Register const prototype = ToRegister(instr->prototype()); |
| 2460 | |
| 2461 | // The {object} must be a spec object. It's sufficient to know that {object} |
| 2462 | // is not a smi, since all other non-spec objects have {null} prototypes and |
| 2463 | // will be ruled out below. |
| 2464 | if (instr->hydrogen()->ObjectNeedsSmiCheck()) { |
| 2465 | Condition is_smi = __ CheckSmi(object); |
| 2466 | EmitFalseBranch(instr, is_smi); |
| 2467 | } |
| 2468 | |
| 2469 | // Loop through the {object}s prototype chain looking for the {prototype}. |
| 2470 | __ movp(object_map, FieldOperand(object, HeapObject::kMapOffset)); |
| 2471 | Label loop; |
| 2472 | __ bind(&loop); |
| 2473 | |
| 2474 | |
| 2475 | // Deoptimize if the object needs to be access checked. |
| 2476 | __ testb(FieldOperand(object_map, Map::kBitFieldOffset), |
| 2477 | Immediate(1 << Map::kIsAccessCheckNeeded)); |
| 2478 | DeoptimizeIf(not_zero, instr, Deoptimizer::kAccessCheck); |
| 2479 | // Deoptimize for proxies. |
| 2480 | __ CmpInstanceType(object_map, JS_PROXY_TYPE); |
| 2481 | DeoptimizeIf(equal, instr, Deoptimizer::kProxy); |
| 2482 | |
| 2483 | __ movp(object_prototype, FieldOperand(object_map, Map::kPrototypeOffset)); |
| 2484 | __ cmpp(object_prototype, prototype); |
| 2485 | EmitTrueBranch(instr, equal); |
| 2486 | __ CompareRoot(object_prototype, Heap::kNullValueRootIndex); |
| 2487 | EmitFalseBranch(instr, equal); |
| 2488 | __ movp(object_map, FieldOperand(object_prototype, HeapObject::kMapOffset)); |
| 2489 | __ jmp(&loop); |
| 2490 | } |
| 2491 | |
| 2492 | |
| 2493 | void LCodeGen::DoCmpT(LCmpT* instr) { |
| 2494 | DCHECK(ToRegister(instr->context()).is(rsi)); |
| 2495 | Token::Value op = instr->op(); |
| 2496 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2497 | Handle<Code> ic = CodeFactory::CompareIC(isolate(), op).code(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2498 | CallCode(ic, RelocInfo::CODE_TARGET, instr); |
| 2499 | |
| 2500 | Condition condition = TokenToCondition(op, false); |
| 2501 | Label true_value, done; |
| 2502 | __ testp(rax, rax); |
| 2503 | __ j(condition, &true_value, Label::kNear); |
| 2504 | __ LoadRoot(ToRegister(instr->result()), Heap::kFalseValueRootIndex); |
| 2505 | __ jmp(&done, Label::kNear); |
| 2506 | __ bind(&true_value); |
| 2507 | __ LoadRoot(ToRegister(instr->result()), Heap::kTrueValueRootIndex); |
| 2508 | __ bind(&done); |
| 2509 | } |
| 2510 | |
| 2511 | |
| 2512 | void LCodeGen::DoReturn(LReturn* instr) { |
| 2513 | if (FLAG_trace && info()->IsOptimizing()) { |
| 2514 | // Preserve the return value on the stack and rely on the runtime call |
| 2515 | // to return the value in the same register. We're leaving the code |
| 2516 | // managed by the register allocator and tearing down the frame, it's |
| 2517 | // safe to write to the context register. |
| 2518 | __ Push(rax); |
| 2519 | __ movp(rsi, Operand(rbp, StandardFrameConstants::kContextOffset)); |
| 2520 | __ CallRuntime(Runtime::kTraceExit); |
| 2521 | } |
| 2522 | if (info()->saves_caller_doubles()) { |
| 2523 | RestoreCallerDoubles(); |
| 2524 | } |
| 2525 | if (NeedsEagerFrame()) { |
| 2526 | __ movp(rsp, rbp); |
| 2527 | __ popq(rbp); |
| 2528 | } |
| 2529 | if (instr->has_constant_parameter_count()) { |
| 2530 | __ Ret((ToInteger32(instr->constant_parameter_count()) + 1) * kPointerSize, |
| 2531 | rcx); |
| 2532 | } else { |
| 2533 | DCHECK(info()->IsStub()); // Functions would need to drop one more value. |
| 2534 | Register reg = ToRegister(instr->parameter_count()); |
| 2535 | // The argument count parameter is a smi |
| 2536 | __ SmiToInteger32(reg, reg); |
| 2537 | Register return_addr_reg = reg.is(rcx) ? rbx : rcx; |
| 2538 | __ PopReturnAddressTo(return_addr_reg); |
| 2539 | __ shlp(reg, Immediate(kPointerSizeLog2)); |
| 2540 | __ addp(rsp, reg); |
| 2541 | __ jmp(return_addr_reg); |
| 2542 | } |
| 2543 | } |
| 2544 | |
| 2545 | |
| 2546 | template <class T> |
| 2547 | void LCodeGen::EmitVectorLoadICRegisters(T* instr) { |
| 2548 | Register vector_register = ToRegister(instr->temp_vector()); |
| 2549 | Register slot_register = LoadWithVectorDescriptor::SlotRegister(); |
| 2550 | DCHECK(vector_register.is(LoadWithVectorDescriptor::VectorRegister())); |
| 2551 | DCHECK(slot_register.is(rax)); |
| 2552 | |
| 2553 | AllowDeferredHandleDereference vector_structure_check; |
| 2554 | Handle<TypeFeedbackVector> vector = instr->hydrogen()->feedback_vector(); |
| 2555 | __ Move(vector_register, vector); |
| 2556 | // No need to allocate this register. |
| 2557 | FeedbackVectorSlot slot = instr->hydrogen()->slot(); |
| 2558 | int index = vector->GetIndex(slot); |
| 2559 | __ Move(slot_register, Smi::FromInt(index)); |
| 2560 | } |
| 2561 | |
| 2562 | |
| 2563 | template <class T> |
| 2564 | void LCodeGen::EmitVectorStoreICRegisters(T* instr) { |
| 2565 | Register vector_register = ToRegister(instr->temp_vector()); |
| 2566 | Register slot_register = ToRegister(instr->temp_slot()); |
| 2567 | |
| 2568 | AllowDeferredHandleDereference vector_structure_check; |
| 2569 | Handle<TypeFeedbackVector> vector = instr->hydrogen()->feedback_vector(); |
| 2570 | __ Move(vector_register, vector); |
| 2571 | FeedbackVectorSlot slot = instr->hydrogen()->slot(); |
| 2572 | int index = vector->GetIndex(slot); |
| 2573 | __ Move(slot_register, Smi::FromInt(index)); |
| 2574 | } |
| 2575 | |
| 2576 | |
| 2577 | void LCodeGen::DoLoadGlobalGeneric(LLoadGlobalGeneric* instr) { |
| 2578 | DCHECK(ToRegister(instr->context()).is(rsi)); |
| 2579 | DCHECK(ToRegister(instr->global_object()) |
| 2580 | .is(LoadDescriptor::ReceiverRegister())); |
| 2581 | DCHECK(ToRegister(instr->result()).is(rax)); |
| 2582 | |
| 2583 | __ Move(LoadDescriptor::NameRegister(), instr->name()); |
| 2584 | EmitVectorLoadICRegisters<LLoadGlobalGeneric>(instr); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2585 | Handle<Code> ic = CodeFactory::LoadICInOptimizedCode( |
| 2586 | isolate(), instr->typeof_mode(), PREMONOMORPHIC) |
| 2587 | .code(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2588 | CallCode(ic, RelocInfo::CODE_TARGET, instr); |
| 2589 | } |
| 2590 | |
| 2591 | |
| 2592 | void LCodeGen::DoLoadContextSlot(LLoadContextSlot* instr) { |
| 2593 | Register context = ToRegister(instr->context()); |
| 2594 | Register result = ToRegister(instr->result()); |
| 2595 | __ movp(result, ContextOperand(context, instr->slot_index())); |
| 2596 | if (instr->hydrogen()->RequiresHoleCheck()) { |
| 2597 | __ CompareRoot(result, Heap::kTheHoleValueRootIndex); |
| 2598 | if (instr->hydrogen()->DeoptimizesOnHole()) { |
| 2599 | DeoptimizeIf(equal, instr, Deoptimizer::kHole); |
| 2600 | } else { |
| 2601 | Label is_not_hole; |
| 2602 | __ j(not_equal, &is_not_hole, Label::kNear); |
| 2603 | __ LoadRoot(result, Heap::kUndefinedValueRootIndex); |
| 2604 | __ bind(&is_not_hole); |
| 2605 | } |
| 2606 | } |
| 2607 | } |
| 2608 | |
| 2609 | |
| 2610 | void LCodeGen::DoStoreContextSlot(LStoreContextSlot* instr) { |
| 2611 | Register context = ToRegister(instr->context()); |
| 2612 | Register value = ToRegister(instr->value()); |
| 2613 | |
| 2614 | Operand target = ContextOperand(context, instr->slot_index()); |
| 2615 | |
| 2616 | Label skip_assignment; |
| 2617 | if (instr->hydrogen()->RequiresHoleCheck()) { |
| 2618 | __ CompareRoot(target, Heap::kTheHoleValueRootIndex); |
| 2619 | if (instr->hydrogen()->DeoptimizesOnHole()) { |
| 2620 | DeoptimizeIf(equal, instr, Deoptimizer::kHole); |
| 2621 | } else { |
| 2622 | __ j(not_equal, &skip_assignment); |
| 2623 | } |
| 2624 | } |
| 2625 | __ movp(target, value); |
| 2626 | |
| 2627 | if (instr->hydrogen()->NeedsWriteBarrier()) { |
| 2628 | SmiCheck check_needed = |
| 2629 | instr->hydrogen()->value()->type().IsHeapObject() |
| 2630 | ? OMIT_SMI_CHECK : INLINE_SMI_CHECK; |
| 2631 | int offset = Context::SlotOffset(instr->slot_index()); |
| 2632 | Register scratch = ToRegister(instr->temp()); |
| 2633 | __ RecordWriteContextSlot(context, |
| 2634 | offset, |
| 2635 | value, |
| 2636 | scratch, |
| 2637 | kSaveFPRegs, |
| 2638 | EMIT_REMEMBERED_SET, |
| 2639 | check_needed); |
| 2640 | } |
| 2641 | |
| 2642 | __ bind(&skip_assignment); |
| 2643 | } |
| 2644 | |
| 2645 | |
| 2646 | void LCodeGen::DoLoadNamedField(LLoadNamedField* instr) { |
| 2647 | HObjectAccess access = instr->hydrogen()->access(); |
| 2648 | int offset = access.offset(); |
| 2649 | |
| 2650 | if (access.IsExternalMemory()) { |
| 2651 | Register result = ToRegister(instr->result()); |
| 2652 | if (instr->object()->IsConstantOperand()) { |
| 2653 | DCHECK(result.is(rax)); |
| 2654 | __ load_rax(ToExternalReference(LConstantOperand::cast(instr->object()))); |
| 2655 | } else { |
| 2656 | Register object = ToRegister(instr->object()); |
| 2657 | __ Load(result, MemOperand(object, offset), access.representation()); |
| 2658 | } |
| 2659 | return; |
| 2660 | } |
| 2661 | |
| 2662 | Register object = ToRegister(instr->object()); |
| 2663 | if (instr->hydrogen()->representation().IsDouble()) { |
| 2664 | DCHECK(access.IsInobject()); |
| 2665 | XMMRegister result = ToDoubleRegister(instr->result()); |
| 2666 | __ Movsd(result, FieldOperand(object, offset)); |
| 2667 | return; |
| 2668 | } |
| 2669 | |
| 2670 | Register result = ToRegister(instr->result()); |
| 2671 | if (!access.IsInobject()) { |
| 2672 | __ movp(result, FieldOperand(object, JSObject::kPropertiesOffset)); |
| 2673 | object = result; |
| 2674 | } |
| 2675 | |
| 2676 | Representation representation = access.representation(); |
| 2677 | if (representation.IsSmi() && SmiValuesAre32Bits() && |
| 2678 | instr->hydrogen()->representation().IsInteger32()) { |
| 2679 | if (FLAG_debug_code) { |
| 2680 | Register scratch = kScratchRegister; |
| 2681 | __ Load(scratch, FieldOperand(object, offset), representation); |
| 2682 | __ AssertSmi(scratch); |
| 2683 | } |
| 2684 | |
| 2685 | // Read int value directly from upper half of the smi. |
| 2686 | STATIC_ASSERT(kSmiTag == 0); |
| 2687 | DCHECK(kSmiTagSize + kSmiShiftSize == 32); |
| 2688 | offset += kPointerSize / 2; |
| 2689 | representation = Representation::Integer32(); |
| 2690 | } |
| 2691 | __ Load(result, FieldOperand(object, offset), representation); |
| 2692 | } |
| 2693 | |
| 2694 | |
| 2695 | void LCodeGen::DoLoadNamedGeneric(LLoadNamedGeneric* instr) { |
| 2696 | DCHECK(ToRegister(instr->context()).is(rsi)); |
| 2697 | DCHECK(ToRegister(instr->object()).is(LoadDescriptor::ReceiverRegister())); |
| 2698 | DCHECK(ToRegister(instr->result()).is(rax)); |
| 2699 | |
| 2700 | __ Move(LoadDescriptor::NameRegister(), instr->name()); |
| 2701 | EmitVectorLoadICRegisters<LLoadNamedGeneric>(instr); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2702 | Handle<Code> ic = CodeFactory::LoadICInOptimizedCode( |
| 2703 | isolate(), NOT_INSIDE_TYPEOF, |
| 2704 | instr->hydrogen()->initialization_state()) |
| 2705 | .code(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2706 | CallCode(ic, RelocInfo::CODE_TARGET, instr); |
| 2707 | } |
| 2708 | |
| 2709 | |
| 2710 | void LCodeGen::DoLoadFunctionPrototype(LLoadFunctionPrototype* instr) { |
| 2711 | Register function = ToRegister(instr->function()); |
| 2712 | Register result = ToRegister(instr->result()); |
| 2713 | |
| 2714 | // Get the prototype or initial map from the function. |
| 2715 | __ movp(result, |
| 2716 | FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset)); |
| 2717 | |
| 2718 | // Check that the function has a prototype or an initial map. |
| 2719 | __ CompareRoot(result, Heap::kTheHoleValueRootIndex); |
| 2720 | DeoptimizeIf(equal, instr, Deoptimizer::kHole); |
| 2721 | |
| 2722 | // If the function does not have an initial map, we're done. |
| 2723 | Label done; |
| 2724 | __ CmpObjectType(result, MAP_TYPE, kScratchRegister); |
| 2725 | __ j(not_equal, &done, Label::kNear); |
| 2726 | |
| 2727 | // Get the prototype from the initial map. |
| 2728 | __ movp(result, FieldOperand(result, Map::kPrototypeOffset)); |
| 2729 | |
| 2730 | // All done. |
| 2731 | __ bind(&done); |
| 2732 | } |
| 2733 | |
| 2734 | |
| 2735 | void LCodeGen::DoLoadRoot(LLoadRoot* instr) { |
| 2736 | Register result = ToRegister(instr->result()); |
| 2737 | __ LoadRoot(result, instr->index()); |
| 2738 | } |
| 2739 | |
| 2740 | |
| 2741 | void LCodeGen::DoAccessArgumentsAt(LAccessArgumentsAt* instr) { |
| 2742 | Register arguments = ToRegister(instr->arguments()); |
| 2743 | Register result = ToRegister(instr->result()); |
| 2744 | |
| 2745 | if (instr->length()->IsConstantOperand() && |
| 2746 | instr->index()->IsConstantOperand()) { |
| 2747 | int32_t const_index = ToInteger32(LConstantOperand::cast(instr->index())); |
| 2748 | int32_t const_length = ToInteger32(LConstantOperand::cast(instr->length())); |
| 2749 | if (const_index >= 0 && const_index < const_length) { |
| 2750 | StackArgumentsAccessor args(arguments, const_length, |
| 2751 | ARGUMENTS_DONT_CONTAIN_RECEIVER); |
| 2752 | __ movp(result, args.GetArgumentOperand(const_index)); |
| 2753 | } else if (FLAG_debug_code) { |
| 2754 | __ int3(); |
| 2755 | } |
| 2756 | } else { |
| 2757 | Register length = ToRegister(instr->length()); |
| 2758 | // There are two words between the frame pointer and the last argument. |
| 2759 | // Subtracting from length accounts for one of them add one more. |
| 2760 | if (instr->index()->IsRegister()) { |
| 2761 | __ subl(length, ToRegister(instr->index())); |
| 2762 | } else { |
| 2763 | __ subl(length, ToOperand(instr->index())); |
| 2764 | } |
| 2765 | StackArgumentsAccessor args(arguments, length, |
| 2766 | ARGUMENTS_DONT_CONTAIN_RECEIVER); |
| 2767 | __ movp(result, args.GetArgumentOperand(0)); |
| 2768 | } |
| 2769 | } |
| 2770 | |
| 2771 | |
| 2772 | void LCodeGen::DoLoadKeyedExternalArray(LLoadKeyed* instr) { |
| 2773 | ElementsKind elements_kind = instr->elements_kind(); |
| 2774 | LOperand* key = instr->key(); |
| 2775 | if (kPointerSize == kInt32Size && !key->IsConstantOperand()) { |
| 2776 | Register key_reg = ToRegister(key); |
| 2777 | Representation key_representation = |
| 2778 | instr->hydrogen()->key()->representation(); |
| 2779 | if (ExternalArrayOpRequiresTemp(key_representation, elements_kind)) { |
| 2780 | __ SmiToInteger64(key_reg, key_reg); |
| 2781 | } else if (instr->hydrogen()->IsDehoisted()) { |
| 2782 | // Sign extend key because it could be a 32 bit negative value |
| 2783 | // and the dehoisted address computation happens in 64 bits |
| 2784 | __ movsxlq(key_reg, key_reg); |
| 2785 | } |
| 2786 | } |
| 2787 | Operand operand(BuildFastArrayOperand( |
| 2788 | instr->elements(), |
| 2789 | key, |
| 2790 | instr->hydrogen()->key()->representation(), |
| 2791 | elements_kind, |
| 2792 | instr->base_offset())); |
| 2793 | |
| 2794 | if (elements_kind == FLOAT32_ELEMENTS) { |
| 2795 | XMMRegister result(ToDoubleRegister(instr->result())); |
| 2796 | __ Cvtss2sd(result, operand); |
| 2797 | } else if (elements_kind == FLOAT64_ELEMENTS) { |
| 2798 | __ Movsd(ToDoubleRegister(instr->result()), operand); |
| 2799 | } else { |
| 2800 | Register result(ToRegister(instr->result())); |
| 2801 | switch (elements_kind) { |
| 2802 | case INT8_ELEMENTS: |
| 2803 | __ movsxbl(result, operand); |
| 2804 | break; |
| 2805 | case UINT8_ELEMENTS: |
| 2806 | case UINT8_CLAMPED_ELEMENTS: |
| 2807 | __ movzxbl(result, operand); |
| 2808 | break; |
| 2809 | case INT16_ELEMENTS: |
| 2810 | __ movsxwl(result, operand); |
| 2811 | break; |
| 2812 | case UINT16_ELEMENTS: |
| 2813 | __ movzxwl(result, operand); |
| 2814 | break; |
| 2815 | case INT32_ELEMENTS: |
| 2816 | __ movl(result, operand); |
| 2817 | break; |
| 2818 | case UINT32_ELEMENTS: |
| 2819 | __ movl(result, operand); |
| 2820 | if (!instr->hydrogen()->CheckFlag(HInstruction::kUint32)) { |
| 2821 | __ testl(result, result); |
| 2822 | DeoptimizeIf(negative, instr, Deoptimizer::kNegativeValue); |
| 2823 | } |
| 2824 | break; |
| 2825 | case FLOAT32_ELEMENTS: |
| 2826 | case FLOAT64_ELEMENTS: |
| 2827 | case FAST_ELEMENTS: |
| 2828 | case FAST_SMI_ELEMENTS: |
| 2829 | case FAST_DOUBLE_ELEMENTS: |
| 2830 | case FAST_HOLEY_ELEMENTS: |
| 2831 | case FAST_HOLEY_SMI_ELEMENTS: |
| 2832 | case FAST_HOLEY_DOUBLE_ELEMENTS: |
| 2833 | case DICTIONARY_ELEMENTS: |
| 2834 | case FAST_SLOPPY_ARGUMENTS_ELEMENTS: |
| 2835 | case SLOW_SLOPPY_ARGUMENTS_ELEMENTS: |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2836 | case FAST_STRING_WRAPPER_ELEMENTS: |
| 2837 | case SLOW_STRING_WRAPPER_ELEMENTS: |
| 2838 | case NO_ELEMENTS: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2839 | UNREACHABLE(); |
| 2840 | break; |
| 2841 | } |
| 2842 | } |
| 2843 | } |
| 2844 | |
| 2845 | |
| 2846 | void LCodeGen::DoLoadKeyedFixedDoubleArray(LLoadKeyed* instr) { |
| 2847 | XMMRegister result(ToDoubleRegister(instr->result())); |
| 2848 | LOperand* key = instr->key(); |
| 2849 | if (kPointerSize == kInt32Size && !key->IsConstantOperand() && |
| 2850 | instr->hydrogen()->IsDehoisted()) { |
| 2851 | // Sign extend key because it could be a 32 bit negative value |
| 2852 | // and the dehoisted address computation happens in 64 bits |
| 2853 | __ movsxlq(ToRegister(key), ToRegister(key)); |
| 2854 | } |
| 2855 | if (instr->hydrogen()->RequiresHoleCheck()) { |
| 2856 | Operand hole_check_operand = BuildFastArrayOperand( |
| 2857 | instr->elements(), |
| 2858 | key, |
| 2859 | instr->hydrogen()->key()->representation(), |
| 2860 | FAST_DOUBLE_ELEMENTS, |
| 2861 | instr->base_offset() + sizeof(kHoleNanLower32)); |
| 2862 | __ cmpl(hole_check_operand, Immediate(kHoleNanUpper32)); |
| 2863 | DeoptimizeIf(equal, instr, Deoptimizer::kHole); |
| 2864 | } |
| 2865 | |
| 2866 | Operand double_load_operand = BuildFastArrayOperand( |
| 2867 | instr->elements(), |
| 2868 | key, |
| 2869 | instr->hydrogen()->key()->representation(), |
| 2870 | FAST_DOUBLE_ELEMENTS, |
| 2871 | instr->base_offset()); |
| 2872 | __ Movsd(result, double_load_operand); |
| 2873 | } |
| 2874 | |
| 2875 | |
| 2876 | void LCodeGen::DoLoadKeyedFixedArray(LLoadKeyed* instr) { |
| 2877 | HLoadKeyed* hinstr = instr->hydrogen(); |
| 2878 | Register result = ToRegister(instr->result()); |
| 2879 | LOperand* key = instr->key(); |
| 2880 | bool requires_hole_check = hinstr->RequiresHoleCheck(); |
| 2881 | Representation representation = hinstr->representation(); |
| 2882 | int offset = instr->base_offset(); |
| 2883 | |
| 2884 | if (kPointerSize == kInt32Size && !key->IsConstantOperand() && |
| 2885 | instr->hydrogen()->IsDehoisted()) { |
| 2886 | // Sign extend key because it could be a 32 bit negative value |
| 2887 | // and the dehoisted address computation happens in 64 bits |
| 2888 | __ movsxlq(ToRegister(key), ToRegister(key)); |
| 2889 | } |
| 2890 | if (representation.IsInteger32() && SmiValuesAre32Bits() && |
| 2891 | hinstr->elements_kind() == FAST_SMI_ELEMENTS) { |
| 2892 | DCHECK(!requires_hole_check); |
| 2893 | if (FLAG_debug_code) { |
| 2894 | Register scratch = kScratchRegister; |
| 2895 | __ Load(scratch, |
| 2896 | BuildFastArrayOperand(instr->elements(), |
| 2897 | key, |
| 2898 | instr->hydrogen()->key()->representation(), |
| 2899 | FAST_ELEMENTS, |
| 2900 | offset), |
| 2901 | Representation::Smi()); |
| 2902 | __ AssertSmi(scratch); |
| 2903 | } |
| 2904 | // Read int value directly from upper half of the smi. |
| 2905 | STATIC_ASSERT(kSmiTag == 0); |
| 2906 | DCHECK(kSmiTagSize + kSmiShiftSize == 32); |
| 2907 | offset += kPointerSize / 2; |
| 2908 | } |
| 2909 | |
| 2910 | __ Load(result, |
| 2911 | BuildFastArrayOperand(instr->elements(), key, |
| 2912 | instr->hydrogen()->key()->representation(), |
| 2913 | FAST_ELEMENTS, offset), |
| 2914 | representation); |
| 2915 | |
| 2916 | // Check for the hole value. |
| 2917 | if (requires_hole_check) { |
| 2918 | if (IsFastSmiElementsKind(hinstr->elements_kind())) { |
| 2919 | Condition smi = __ CheckSmi(result); |
| 2920 | DeoptimizeIf(NegateCondition(smi), instr, Deoptimizer::kNotASmi); |
| 2921 | } else { |
| 2922 | __ CompareRoot(result, Heap::kTheHoleValueRootIndex); |
| 2923 | DeoptimizeIf(equal, instr, Deoptimizer::kHole); |
| 2924 | } |
| 2925 | } else if (hinstr->hole_mode() == CONVERT_HOLE_TO_UNDEFINED) { |
| 2926 | DCHECK(hinstr->elements_kind() == FAST_HOLEY_ELEMENTS); |
| 2927 | Label done; |
| 2928 | __ CompareRoot(result, Heap::kTheHoleValueRootIndex); |
| 2929 | __ j(not_equal, &done); |
| 2930 | if (info()->IsStub()) { |
| 2931 | // A stub can safely convert the hole to undefined only if the array |
| 2932 | // protector cell contains (Smi) Isolate::kArrayProtectorValid. Otherwise |
| 2933 | // it needs to bail out. |
| 2934 | __ LoadRoot(result, Heap::kArrayProtectorRootIndex); |
| 2935 | __ Cmp(FieldOperand(result, Cell::kValueOffset), |
| 2936 | Smi::FromInt(Isolate::kArrayProtectorValid)); |
| 2937 | DeoptimizeIf(not_equal, instr, Deoptimizer::kHole); |
| 2938 | } |
| 2939 | __ Move(result, isolate()->factory()->undefined_value()); |
| 2940 | __ bind(&done); |
| 2941 | } |
| 2942 | } |
| 2943 | |
| 2944 | |
| 2945 | void LCodeGen::DoLoadKeyed(LLoadKeyed* instr) { |
| 2946 | if (instr->is_fixed_typed_array()) { |
| 2947 | DoLoadKeyedExternalArray(instr); |
| 2948 | } else if (instr->hydrogen()->representation().IsDouble()) { |
| 2949 | DoLoadKeyedFixedDoubleArray(instr); |
| 2950 | } else { |
| 2951 | DoLoadKeyedFixedArray(instr); |
| 2952 | } |
| 2953 | } |
| 2954 | |
| 2955 | |
| 2956 | Operand LCodeGen::BuildFastArrayOperand( |
| 2957 | LOperand* elements_pointer, |
| 2958 | LOperand* key, |
| 2959 | Representation key_representation, |
| 2960 | ElementsKind elements_kind, |
| 2961 | uint32_t offset) { |
| 2962 | Register elements_pointer_reg = ToRegister(elements_pointer); |
| 2963 | int shift_size = ElementsKindToShiftSize(elements_kind); |
| 2964 | if (key->IsConstantOperand()) { |
| 2965 | int32_t constant_value = ToInteger32(LConstantOperand::cast(key)); |
| 2966 | if (constant_value & 0xF0000000) { |
| 2967 | Abort(kArrayIndexConstantValueTooBig); |
| 2968 | } |
| 2969 | return Operand(elements_pointer_reg, |
| 2970 | (constant_value << shift_size) + offset); |
| 2971 | } else { |
| 2972 | // Guaranteed by ArrayInstructionInterface::KeyedAccessIndexRequirement(). |
| 2973 | DCHECK(key_representation.IsInteger32()); |
| 2974 | |
| 2975 | ScaleFactor scale_factor = static_cast<ScaleFactor>(shift_size); |
| 2976 | return Operand(elements_pointer_reg, |
| 2977 | ToRegister(key), |
| 2978 | scale_factor, |
| 2979 | offset); |
| 2980 | } |
| 2981 | } |
| 2982 | |
| 2983 | |
| 2984 | void LCodeGen::DoLoadKeyedGeneric(LLoadKeyedGeneric* instr) { |
| 2985 | DCHECK(ToRegister(instr->context()).is(rsi)); |
| 2986 | DCHECK(ToRegister(instr->object()).is(LoadDescriptor::ReceiverRegister())); |
| 2987 | DCHECK(ToRegister(instr->key()).is(LoadDescriptor::NameRegister())); |
| 2988 | |
| 2989 | if (instr->hydrogen()->HasVectorAndSlot()) { |
| 2990 | EmitVectorLoadICRegisters<LLoadKeyedGeneric>(instr); |
| 2991 | } |
| 2992 | |
| 2993 | Handle<Code> ic = CodeFactory::KeyedLoadICInOptimizedCode( |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2994 | isolate(), instr->hydrogen()->initialization_state()) |
| 2995 | .code(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2996 | CallCode(ic, RelocInfo::CODE_TARGET, instr); |
| 2997 | } |
| 2998 | |
| 2999 | |
| 3000 | void LCodeGen::DoArgumentsElements(LArgumentsElements* instr) { |
| 3001 | Register result = ToRegister(instr->result()); |
| 3002 | |
| 3003 | if (instr->hydrogen()->from_inlined()) { |
| 3004 | __ leap(result, Operand(rsp, -kFPOnStackSize + -kPCOnStackSize)); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3005 | } else if (instr->hydrogen()->arguments_adaptor()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3006 | // Check for arguments adapter frame. |
| 3007 | Label done, adapted; |
| 3008 | __ movp(result, Operand(rbp, StandardFrameConstants::kCallerFPOffset)); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3009 | __ Cmp(Operand(result, CommonFrameConstants::kContextOrFrameTypeOffset), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3010 | Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)); |
| 3011 | __ j(equal, &adapted, Label::kNear); |
| 3012 | |
| 3013 | // No arguments adaptor frame. |
| 3014 | __ movp(result, rbp); |
| 3015 | __ jmp(&done, Label::kNear); |
| 3016 | |
| 3017 | // Arguments adaptor frame present. |
| 3018 | __ bind(&adapted); |
| 3019 | __ movp(result, Operand(rbp, StandardFrameConstants::kCallerFPOffset)); |
| 3020 | |
| 3021 | // Result is the frame pointer for the frame if not adapted and for the real |
| 3022 | // frame below the adaptor frame if adapted. |
| 3023 | __ bind(&done); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3024 | } else { |
| 3025 | __ movp(result, rbp); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3026 | } |
| 3027 | } |
| 3028 | |
| 3029 | |
| 3030 | void LCodeGen::DoArgumentsLength(LArgumentsLength* instr) { |
| 3031 | Register result = ToRegister(instr->result()); |
| 3032 | |
| 3033 | Label done; |
| 3034 | |
| 3035 | // If no arguments adaptor frame the number of arguments is fixed. |
| 3036 | if (instr->elements()->IsRegister()) { |
| 3037 | __ cmpp(rbp, ToRegister(instr->elements())); |
| 3038 | } else { |
| 3039 | __ cmpp(rbp, ToOperand(instr->elements())); |
| 3040 | } |
| 3041 | __ movl(result, Immediate(scope()->num_parameters())); |
| 3042 | __ j(equal, &done, Label::kNear); |
| 3043 | |
| 3044 | // Arguments adaptor frame present. Get argument length from there. |
| 3045 | __ movp(result, Operand(rbp, StandardFrameConstants::kCallerFPOffset)); |
| 3046 | __ SmiToInteger32(result, |
| 3047 | Operand(result, |
| 3048 | ArgumentsAdaptorFrameConstants::kLengthOffset)); |
| 3049 | |
| 3050 | // Argument length is in result register. |
| 3051 | __ bind(&done); |
| 3052 | } |
| 3053 | |
| 3054 | |
| 3055 | void LCodeGen::DoWrapReceiver(LWrapReceiver* instr) { |
| 3056 | Register receiver = ToRegister(instr->receiver()); |
| 3057 | Register function = ToRegister(instr->function()); |
| 3058 | |
| 3059 | // If the receiver is null or undefined, we have to pass the global |
| 3060 | // object as a receiver to normal functions. Values have to be |
| 3061 | // passed unchanged to builtins and strict-mode functions. |
| 3062 | Label global_object, receiver_ok; |
| 3063 | Label::Distance dist = DeoptEveryNTimes() ? Label::kFar : Label::kNear; |
| 3064 | |
| 3065 | if (!instr->hydrogen()->known_function()) { |
| 3066 | // Do not transform the receiver to object for strict mode |
| 3067 | // functions. |
| 3068 | __ movp(kScratchRegister, |
| 3069 | FieldOperand(function, JSFunction::kSharedFunctionInfoOffset)); |
| 3070 | __ testb(FieldOperand(kScratchRegister, |
| 3071 | SharedFunctionInfo::kStrictModeByteOffset), |
| 3072 | Immediate(1 << SharedFunctionInfo::kStrictModeBitWithinByte)); |
| 3073 | __ j(not_equal, &receiver_ok, dist); |
| 3074 | |
| 3075 | // Do not transform the receiver to object for builtins. |
| 3076 | __ testb(FieldOperand(kScratchRegister, |
| 3077 | SharedFunctionInfo::kNativeByteOffset), |
| 3078 | Immediate(1 << SharedFunctionInfo::kNativeBitWithinByte)); |
| 3079 | __ j(not_equal, &receiver_ok, dist); |
| 3080 | } |
| 3081 | |
| 3082 | // Normal function. Replace undefined or null with global receiver. |
| 3083 | __ CompareRoot(receiver, Heap::kNullValueRootIndex); |
| 3084 | __ j(equal, &global_object, Label::kNear); |
| 3085 | __ CompareRoot(receiver, Heap::kUndefinedValueRootIndex); |
| 3086 | __ j(equal, &global_object, Label::kNear); |
| 3087 | |
| 3088 | // The receiver should be a JS object. |
| 3089 | Condition is_smi = __ CheckSmi(receiver); |
| 3090 | DeoptimizeIf(is_smi, instr, Deoptimizer::kSmi); |
| 3091 | __ CmpObjectType(receiver, FIRST_JS_RECEIVER_TYPE, kScratchRegister); |
| 3092 | DeoptimizeIf(below, instr, Deoptimizer::kNotAJavaScriptObject); |
| 3093 | |
| 3094 | __ jmp(&receiver_ok, Label::kNear); |
| 3095 | __ bind(&global_object); |
| 3096 | __ movp(receiver, FieldOperand(function, JSFunction::kContextOffset)); |
| 3097 | __ movp(receiver, ContextOperand(receiver, Context::NATIVE_CONTEXT_INDEX)); |
| 3098 | __ movp(receiver, ContextOperand(receiver, Context::GLOBAL_PROXY_INDEX)); |
| 3099 | |
| 3100 | __ bind(&receiver_ok); |
| 3101 | } |
| 3102 | |
| 3103 | |
| 3104 | void LCodeGen::DoApplyArguments(LApplyArguments* instr) { |
| 3105 | Register receiver = ToRegister(instr->receiver()); |
| 3106 | Register function = ToRegister(instr->function()); |
| 3107 | Register length = ToRegister(instr->length()); |
| 3108 | Register elements = ToRegister(instr->elements()); |
| 3109 | DCHECK(receiver.is(rax)); // Used for parameter count. |
| 3110 | DCHECK(function.is(rdi)); // Required by InvokeFunction. |
| 3111 | DCHECK(ToRegister(instr->result()).is(rax)); |
| 3112 | |
| 3113 | // Copy the arguments to this function possibly from the |
| 3114 | // adaptor frame below it. |
| 3115 | const uint32_t kArgumentsLimit = 1 * KB; |
| 3116 | __ cmpp(length, Immediate(kArgumentsLimit)); |
| 3117 | DeoptimizeIf(above, instr, Deoptimizer::kTooManyArguments); |
| 3118 | |
| 3119 | __ Push(receiver); |
| 3120 | __ movp(receiver, length); |
| 3121 | |
| 3122 | // Loop through the arguments pushing them onto the execution |
| 3123 | // stack. |
| 3124 | Label invoke, loop; |
| 3125 | // length is a small non-negative integer, due to the test above. |
| 3126 | __ testl(length, length); |
| 3127 | __ j(zero, &invoke, Label::kNear); |
| 3128 | __ bind(&loop); |
| 3129 | StackArgumentsAccessor args(elements, length, |
| 3130 | ARGUMENTS_DONT_CONTAIN_RECEIVER); |
| 3131 | __ Push(args.GetArgumentOperand(0)); |
| 3132 | __ decl(length); |
| 3133 | __ j(not_zero, &loop); |
| 3134 | |
| 3135 | // Invoke the function. |
| 3136 | __ bind(&invoke); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3137 | |
| 3138 | InvokeFlag flag = CALL_FUNCTION; |
| 3139 | if (instr->hydrogen()->tail_call_mode() == TailCallMode::kAllow) { |
| 3140 | DCHECK(!info()->saves_caller_doubles()); |
| 3141 | // TODO(ishell): drop current frame before pushing arguments to the stack. |
| 3142 | flag = JUMP_FUNCTION; |
| 3143 | ParameterCount actual(rax); |
| 3144 | // It is safe to use rbx, rcx and r8 as scratch registers here given that |
| 3145 | // 1) we are not going to return to caller function anyway, |
| 3146 | // 2) rbx (expected number of arguments) will be initialized below. |
| 3147 | PrepareForTailCall(actual, rbx, rcx, r8); |
| 3148 | } |
| 3149 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3150 | DCHECK(instr->HasPointerMap()); |
| 3151 | LPointerMap* pointers = instr->pointer_map(); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3152 | SafepointGenerator safepoint_generator(this, pointers, Safepoint::kLazyDeopt); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3153 | ParameterCount actual(rax); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3154 | __ InvokeFunction(function, no_reg, actual, flag, safepoint_generator); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3155 | } |
| 3156 | |
| 3157 | |
| 3158 | void LCodeGen::DoPushArgument(LPushArgument* instr) { |
| 3159 | LOperand* argument = instr->value(); |
| 3160 | EmitPushTaggedOperand(argument); |
| 3161 | } |
| 3162 | |
| 3163 | |
| 3164 | void LCodeGen::DoDrop(LDrop* instr) { |
| 3165 | __ Drop(instr->count()); |
| 3166 | } |
| 3167 | |
| 3168 | |
| 3169 | void LCodeGen::DoThisFunction(LThisFunction* instr) { |
| 3170 | Register result = ToRegister(instr->result()); |
| 3171 | __ movp(result, Operand(rbp, JavaScriptFrameConstants::kFunctionOffset)); |
| 3172 | } |
| 3173 | |
| 3174 | |
| 3175 | void LCodeGen::DoContext(LContext* instr) { |
| 3176 | Register result = ToRegister(instr->result()); |
| 3177 | if (info()->IsOptimizing()) { |
| 3178 | __ movp(result, Operand(rbp, StandardFrameConstants::kContextOffset)); |
| 3179 | } else { |
| 3180 | // If there is no frame, the context must be in rsi. |
| 3181 | DCHECK(result.is(rsi)); |
| 3182 | } |
| 3183 | } |
| 3184 | |
| 3185 | |
| 3186 | void LCodeGen::DoDeclareGlobals(LDeclareGlobals* instr) { |
| 3187 | DCHECK(ToRegister(instr->context()).is(rsi)); |
| 3188 | __ Push(instr->hydrogen()->pairs()); |
| 3189 | __ Push(Smi::FromInt(instr->hydrogen()->flags())); |
| 3190 | CallRuntime(Runtime::kDeclareGlobals, instr); |
| 3191 | } |
| 3192 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3193 | void LCodeGen::CallKnownFunction(Handle<JSFunction> function, |
| 3194 | int formal_parameter_count, int arity, |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3195 | bool is_tail_call, LInstruction* instr) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3196 | bool dont_adapt_arguments = |
| 3197 | formal_parameter_count == SharedFunctionInfo::kDontAdaptArgumentsSentinel; |
| 3198 | bool can_invoke_directly = |
| 3199 | dont_adapt_arguments || formal_parameter_count == arity; |
| 3200 | |
| 3201 | Register function_reg = rdi; |
| 3202 | LPointerMap* pointers = instr->pointer_map(); |
| 3203 | |
| 3204 | if (can_invoke_directly) { |
| 3205 | // Change context. |
| 3206 | __ movp(rsi, FieldOperand(function_reg, JSFunction::kContextOffset)); |
| 3207 | |
| 3208 | // Always initialize new target and number of actual arguments. |
| 3209 | __ LoadRoot(rdx, Heap::kUndefinedValueRootIndex); |
| 3210 | __ Set(rax, arity); |
| 3211 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3212 | bool is_self_call = function.is_identical_to(info()->closure()); |
| 3213 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3214 | // Invoke function. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3215 | if (is_self_call) { |
| 3216 | Handle<Code> self(reinterpret_cast<Code**>(__ CodeObject().location())); |
| 3217 | if (is_tail_call) { |
| 3218 | __ Jump(self, RelocInfo::CODE_TARGET); |
| 3219 | } else { |
| 3220 | __ Call(self, RelocInfo::CODE_TARGET); |
| 3221 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3222 | } else { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3223 | Operand target = FieldOperand(function_reg, JSFunction::kCodeEntryOffset); |
| 3224 | if (is_tail_call) { |
| 3225 | __ Jump(target); |
| 3226 | } else { |
| 3227 | __ Call(target); |
| 3228 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3229 | } |
| 3230 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3231 | if (!is_tail_call) { |
| 3232 | // Set up deoptimization. |
| 3233 | RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT, 0); |
| 3234 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3235 | } else { |
| 3236 | // We need to adapt arguments. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3237 | SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt); |
| 3238 | ParameterCount actual(arity); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3239 | ParameterCount expected(formal_parameter_count); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3240 | InvokeFlag flag = is_tail_call ? JUMP_FUNCTION : CALL_FUNCTION; |
| 3241 | __ InvokeFunction(function_reg, no_reg, expected, actual, flag, generator); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3242 | } |
| 3243 | } |
| 3244 | |
| 3245 | |
| 3246 | void LCodeGen::DoCallWithDescriptor(LCallWithDescriptor* instr) { |
| 3247 | DCHECK(ToRegister(instr->result()).is(rax)); |
| 3248 | |
| 3249 | if (instr->hydrogen()->IsTailCall()) { |
| 3250 | if (NeedsEagerFrame()) __ leave(); |
| 3251 | |
| 3252 | if (instr->target()->IsConstantOperand()) { |
| 3253 | LConstantOperand* target = LConstantOperand::cast(instr->target()); |
| 3254 | Handle<Code> code = Handle<Code>::cast(ToHandle(target)); |
| 3255 | __ jmp(code, RelocInfo::CODE_TARGET); |
| 3256 | } else { |
| 3257 | DCHECK(instr->target()->IsRegister()); |
| 3258 | Register target = ToRegister(instr->target()); |
| 3259 | __ addp(target, Immediate(Code::kHeaderSize - kHeapObjectTag)); |
| 3260 | __ jmp(target); |
| 3261 | } |
| 3262 | } else { |
| 3263 | LPointerMap* pointers = instr->pointer_map(); |
| 3264 | SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt); |
| 3265 | |
| 3266 | if (instr->target()->IsConstantOperand()) { |
| 3267 | LConstantOperand* target = LConstantOperand::cast(instr->target()); |
| 3268 | Handle<Code> code = Handle<Code>::cast(ToHandle(target)); |
| 3269 | generator.BeforeCall(__ CallSize(code)); |
| 3270 | __ call(code, RelocInfo::CODE_TARGET); |
| 3271 | } else { |
| 3272 | DCHECK(instr->target()->IsRegister()); |
| 3273 | Register target = ToRegister(instr->target()); |
| 3274 | generator.BeforeCall(__ CallSize(target)); |
| 3275 | __ addp(target, Immediate(Code::kHeaderSize - kHeapObjectTag)); |
| 3276 | __ call(target); |
| 3277 | } |
| 3278 | generator.AfterCall(); |
| 3279 | } |
| 3280 | } |
| 3281 | |
| 3282 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3283 | void LCodeGen::DoDeferredMathAbsTaggedHeapNumber(LMathAbs* instr) { |
| 3284 | Register input_reg = ToRegister(instr->value()); |
| 3285 | __ CompareRoot(FieldOperand(input_reg, HeapObject::kMapOffset), |
| 3286 | Heap::kHeapNumberMapRootIndex); |
| 3287 | DeoptimizeIf(not_equal, instr, Deoptimizer::kNotAHeapNumber); |
| 3288 | |
| 3289 | Label slow, allocated, done; |
| 3290 | Register tmp = input_reg.is(rax) ? rcx : rax; |
| 3291 | Register tmp2 = tmp.is(rcx) ? rdx : input_reg.is(rcx) ? rdx : rcx; |
| 3292 | |
| 3293 | // Preserve the value of all registers. |
| 3294 | PushSafepointRegistersScope scope(this); |
| 3295 | |
| 3296 | __ movl(tmp, FieldOperand(input_reg, HeapNumber::kExponentOffset)); |
| 3297 | // Check the sign of the argument. If the argument is positive, just |
| 3298 | // return it. We do not need to patch the stack since |input| and |
| 3299 | // |result| are the same register and |input| will be restored |
| 3300 | // unchanged by popping safepoint registers. |
| 3301 | __ testl(tmp, Immediate(HeapNumber::kSignMask)); |
| 3302 | __ j(zero, &done); |
| 3303 | |
| 3304 | __ AllocateHeapNumber(tmp, tmp2, &slow); |
| 3305 | __ jmp(&allocated, Label::kNear); |
| 3306 | |
| 3307 | // Slow case: Call the runtime system to do the number allocation. |
| 3308 | __ bind(&slow); |
| 3309 | CallRuntimeFromDeferred( |
| 3310 | Runtime::kAllocateHeapNumber, 0, instr, instr->context()); |
| 3311 | // Set the pointer to the new heap number in tmp. |
| 3312 | if (!tmp.is(rax)) __ movp(tmp, rax); |
| 3313 | // Restore input_reg after call to runtime. |
| 3314 | __ LoadFromSafepointRegisterSlot(input_reg, input_reg); |
| 3315 | |
| 3316 | __ bind(&allocated); |
| 3317 | __ movq(tmp2, FieldOperand(input_reg, HeapNumber::kValueOffset)); |
| 3318 | __ shlq(tmp2, Immediate(1)); |
| 3319 | __ shrq(tmp2, Immediate(1)); |
| 3320 | __ movq(FieldOperand(tmp, HeapNumber::kValueOffset), tmp2); |
| 3321 | __ StoreToSafepointRegisterSlot(input_reg, tmp); |
| 3322 | |
| 3323 | __ bind(&done); |
| 3324 | } |
| 3325 | |
| 3326 | |
| 3327 | void LCodeGen::EmitIntegerMathAbs(LMathAbs* instr) { |
| 3328 | Register input_reg = ToRegister(instr->value()); |
| 3329 | __ testl(input_reg, input_reg); |
| 3330 | Label is_positive; |
| 3331 | __ j(not_sign, &is_positive, Label::kNear); |
| 3332 | __ negl(input_reg); // Sets flags. |
| 3333 | DeoptimizeIf(negative, instr, Deoptimizer::kOverflow); |
| 3334 | __ bind(&is_positive); |
| 3335 | } |
| 3336 | |
| 3337 | |
| 3338 | void LCodeGen::EmitSmiMathAbs(LMathAbs* instr) { |
| 3339 | Register input_reg = ToRegister(instr->value()); |
| 3340 | __ testp(input_reg, input_reg); |
| 3341 | Label is_positive; |
| 3342 | __ j(not_sign, &is_positive, Label::kNear); |
| 3343 | __ negp(input_reg); // Sets flags. |
| 3344 | DeoptimizeIf(negative, instr, Deoptimizer::kOverflow); |
| 3345 | __ bind(&is_positive); |
| 3346 | } |
| 3347 | |
| 3348 | |
| 3349 | void LCodeGen::DoMathAbs(LMathAbs* instr) { |
| 3350 | // Class for deferred case. |
| 3351 | class DeferredMathAbsTaggedHeapNumber final : public LDeferredCode { |
| 3352 | public: |
| 3353 | DeferredMathAbsTaggedHeapNumber(LCodeGen* codegen, LMathAbs* instr) |
| 3354 | : LDeferredCode(codegen), instr_(instr) { } |
| 3355 | void Generate() override { |
| 3356 | codegen()->DoDeferredMathAbsTaggedHeapNumber(instr_); |
| 3357 | } |
| 3358 | LInstruction* instr() override { return instr_; } |
| 3359 | |
| 3360 | private: |
| 3361 | LMathAbs* instr_; |
| 3362 | }; |
| 3363 | |
| 3364 | DCHECK(instr->value()->Equals(instr->result())); |
| 3365 | Representation r = instr->hydrogen()->value()->representation(); |
| 3366 | |
| 3367 | if (r.IsDouble()) { |
| 3368 | XMMRegister scratch = double_scratch0(); |
| 3369 | XMMRegister input_reg = ToDoubleRegister(instr->value()); |
| 3370 | __ Xorpd(scratch, scratch); |
| 3371 | __ Subsd(scratch, input_reg); |
| 3372 | __ Andpd(input_reg, scratch); |
| 3373 | } else if (r.IsInteger32()) { |
| 3374 | EmitIntegerMathAbs(instr); |
| 3375 | } else if (r.IsSmi()) { |
| 3376 | EmitSmiMathAbs(instr); |
| 3377 | } else { // Tagged case. |
| 3378 | DeferredMathAbsTaggedHeapNumber* deferred = |
| 3379 | new(zone()) DeferredMathAbsTaggedHeapNumber(this, instr); |
| 3380 | Register input_reg = ToRegister(instr->value()); |
| 3381 | // Smi check. |
| 3382 | __ JumpIfNotSmi(input_reg, deferred->entry()); |
| 3383 | EmitSmiMathAbs(instr); |
| 3384 | __ bind(deferred->exit()); |
| 3385 | } |
| 3386 | } |
| 3387 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3388 | void LCodeGen::DoMathFloorD(LMathFloorD* instr) { |
| 3389 | XMMRegister output_reg = ToDoubleRegister(instr->result()); |
| 3390 | XMMRegister input_reg = ToDoubleRegister(instr->value()); |
| 3391 | CpuFeatureScope scope(masm(), SSE4_1); |
| 3392 | __ Roundsd(output_reg, input_reg, kRoundDown); |
| 3393 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3394 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3395 | void LCodeGen::DoMathFloorI(LMathFloorI* instr) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3396 | XMMRegister xmm_scratch = double_scratch0(); |
| 3397 | Register output_reg = ToRegister(instr->result()); |
| 3398 | XMMRegister input_reg = ToDoubleRegister(instr->value()); |
| 3399 | |
| 3400 | if (CpuFeatures::IsSupported(SSE4_1)) { |
| 3401 | CpuFeatureScope scope(masm(), SSE4_1); |
| 3402 | if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) { |
| 3403 | // Deoptimize if minus zero. |
| 3404 | __ Movq(output_reg, input_reg); |
| 3405 | __ subq(output_reg, Immediate(1)); |
| 3406 | DeoptimizeIf(overflow, instr, Deoptimizer::kMinusZero); |
| 3407 | } |
| 3408 | __ Roundsd(xmm_scratch, input_reg, kRoundDown); |
| 3409 | __ Cvttsd2si(output_reg, xmm_scratch); |
| 3410 | __ cmpl(output_reg, Immediate(0x1)); |
| 3411 | DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow); |
| 3412 | } else { |
| 3413 | Label negative_sign, done; |
| 3414 | // Deoptimize on unordered. |
| 3415 | __ Xorpd(xmm_scratch, xmm_scratch); // Zero the register. |
| 3416 | __ Ucomisd(input_reg, xmm_scratch); |
| 3417 | DeoptimizeIf(parity_even, instr, Deoptimizer::kNaN); |
| 3418 | __ j(below, &negative_sign, Label::kNear); |
| 3419 | |
| 3420 | if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) { |
| 3421 | // Check for negative zero. |
| 3422 | Label positive_sign; |
| 3423 | __ j(above, &positive_sign, Label::kNear); |
| 3424 | __ Movmskpd(output_reg, input_reg); |
| 3425 | __ testl(output_reg, Immediate(1)); |
| 3426 | DeoptimizeIf(not_zero, instr, Deoptimizer::kMinusZero); |
| 3427 | __ Set(output_reg, 0); |
| 3428 | __ jmp(&done); |
| 3429 | __ bind(&positive_sign); |
| 3430 | } |
| 3431 | |
| 3432 | // Use truncating instruction (OK because input is positive). |
| 3433 | __ Cvttsd2si(output_reg, input_reg); |
| 3434 | // Overflow is signalled with minint. |
| 3435 | __ cmpl(output_reg, Immediate(0x1)); |
| 3436 | DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow); |
| 3437 | __ jmp(&done, Label::kNear); |
| 3438 | |
| 3439 | // Non-zero negative reaches here. |
| 3440 | __ bind(&negative_sign); |
| 3441 | // Truncate, then compare and compensate. |
| 3442 | __ Cvttsd2si(output_reg, input_reg); |
| 3443 | __ Cvtlsi2sd(xmm_scratch, output_reg); |
| 3444 | __ Ucomisd(input_reg, xmm_scratch); |
| 3445 | __ j(equal, &done, Label::kNear); |
| 3446 | __ subl(output_reg, Immediate(1)); |
| 3447 | DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow); |
| 3448 | |
| 3449 | __ bind(&done); |
| 3450 | } |
| 3451 | } |
| 3452 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3453 | void LCodeGen::DoMathRoundD(LMathRoundD* instr) { |
| 3454 | XMMRegister xmm_scratch = double_scratch0(); |
| 3455 | XMMRegister output_reg = ToDoubleRegister(instr->result()); |
| 3456 | XMMRegister input_reg = ToDoubleRegister(instr->value()); |
| 3457 | CpuFeatureScope scope(masm(), SSE4_1); |
| 3458 | Label done; |
| 3459 | __ Roundsd(output_reg, input_reg, kRoundUp); |
| 3460 | __ Move(xmm_scratch, -0.5); |
| 3461 | __ Addsd(xmm_scratch, output_reg); |
| 3462 | __ Ucomisd(xmm_scratch, input_reg); |
| 3463 | __ j(below_equal, &done, Label::kNear); |
| 3464 | __ Move(xmm_scratch, 1.0); |
| 3465 | __ Subsd(output_reg, xmm_scratch); |
| 3466 | __ bind(&done); |
| 3467 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3468 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3469 | void LCodeGen::DoMathRoundI(LMathRoundI* instr) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3470 | const XMMRegister xmm_scratch = double_scratch0(); |
| 3471 | Register output_reg = ToRegister(instr->result()); |
| 3472 | XMMRegister input_reg = ToDoubleRegister(instr->value()); |
| 3473 | XMMRegister input_temp = ToDoubleRegister(instr->temp()); |
| 3474 | static int64_t one_half = V8_INT64_C(0x3FE0000000000000); // 0.5 |
| 3475 | static int64_t minus_one_half = V8_INT64_C(0xBFE0000000000000); // -0.5 |
| 3476 | |
| 3477 | Label done, round_to_zero, below_one_half; |
| 3478 | Label::Distance dist = DeoptEveryNTimes() ? Label::kFar : Label::kNear; |
| 3479 | __ movq(kScratchRegister, one_half); |
| 3480 | __ Movq(xmm_scratch, kScratchRegister); |
| 3481 | __ Ucomisd(xmm_scratch, input_reg); |
| 3482 | __ j(above, &below_one_half, Label::kNear); |
| 3483 | |
| 3484 | // CVTTSD2SI rounds towards zero, since 0.5 <= x, we use floor(0.5 + x). |
| 3485 | __ Addsd(xmm_scratch, input_reg); |
| 3486 | __ Cvttsd2si(output_reg, xmm_scratch); |
| 3487 | // Overflow is signalled with minint. |
| 3488 | __ cmpl(output_reg, Immediate(0x1)); |
| 3489 | DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow); |
| 3490 | __ jmp(&done, dist); |
| 3491 | |
| 3492 | __ bind(&below_one_half); |
| 3493 | __ movq(kScratchRegister, minus_one_half); |
| 3494 | __ Movq(xmm_scratch, kScratchRegister); |
| 3495 | __ Ucomisd(xmm_scratch, input_reg); |
| 3496 | __ j(below_equal, &round_to_zero, Label::kNear); |
| 3497 | |
| 3498 | // CVTTSD2SI rounds towards zero, we use ceil(x - (-0.5)) and then |
| 3499 | // compare and compensate. |
| 3500 | __ Movapd(input_temp, input_reg); // Do not alter input_reg. |
| 3501 | __ Subsd(input_temp, xmm_scratch); |
| 3502 | __ Cvttsd2si(output_reg, input_temp); |
| 3503 | // Catch minint due to overflow, and to prevent overflow when compensating. |
| 3504 | __ cmpl(output_reg, Immediate(0x1)); |
| 3505 | DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow); |
| 3506 | |
| 3507 | __ Cvtlsi2sd(xmm_scratch, output_reg); |
| 3508 | __ Ucomisd(xmm_scratch, input_temp); |
| 3509 | __ j(equal, &done, dist); |
| 3510 | __ subl(output_reg, Immediate(1)); |
| 3511 | // No overflow because we already ruled out minint. |
| 3512 | __ jmp(&done, dist); |
| 3513 | |
| 3514 | __ bind(&round_to_zero); |
| 3515 | // We return 0 for the input range [+0, 0.5[, or [-0.5, 0.5[ if |
| 3516 | // we can ignore the difference between a result of -0 and +0. |
| 3517 | if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) { |
| 3518 | __ Movq(output_reg, input_reg); |
| 3519 | __ testq(output_reg, output_reg); |
| 3520 | DeoptimizeIf(negative, instr, Deoptimizer::kMinusZero); |
| 3521 | } |
| 3522 | __ Set(output_reg, 0); |
| 3523 | __ bind(&done); |
| 3524 | } |
| 3525 | |
| 3526 | |
| 3527 | void LCodeGen::DoMathFround(LMathFround* instr) { |
| 3528 | XMMRegister input_reg = ToDoubleRegister(instr->value()); |
| 3529 | XMMRegister output_reg = ToDoubleRegister(instr->result()); |
| 3530 | __ Cvtsd2ss(output_reg, input_reg); |
| 3531 | __ Cvtss2sd(output_reg, output_reg); |
| 3532 | } |
| 3533 | |
| 3534 | |
| 3535 | void LCodeGen::DoMathSqrt(LMathSqrt* instr) { |
| 3536 | XMMRegister output = ToDoubleRegister(instr->result()); |
| 3537 | if (instr->value()->IsDoubleRegister()) { |
| 3538 | XMMRegister input = ToDoubleRegister(instr->value()); |
| 3539 | __ Sqrtsd(output, input); |
| 3540 | } else { |
| 3541 | Operand input = ToOperand(instr->value()); |
| 3542 | __ Sqrtsd(output, input); |
| 3543 | } |
| 3544 | } |
| 3545 | |
| 3546 | |
| 3547 | void LCodeGen::DoMathPowHalf(LMathPowHalf* instr) { |
| 3548 | XMMRegister xmm_scratch = double_scratch0(); |
| 3549 | XMMRegister input_reg = ToDoubleRegister(instr->value()); |
| 3550 | DCHECK(ToDoubleRegister(instr->result()).is(input_reg)); |
| 3551 | |
| 3552 | // Note that according to ECMA-262 15.8.2.13: |
| 3553 | // Math.pow(-Infinity, 0.5) == Infinity |
| 3554 | // Math.sqrt(-Infinity) == NaN |
| 3555 | Label done, sqrt; |
| 3556 | // Check base for -Infinity. According to IEEE-754, double-precision |
| 3557 | // -Infinity has the highest 12 bits set and the lowest 52 bits cleared. |
| 3558 | __ movq(kScratchRegister, V8_INT64_C(0xFFF0000000000000)); |
| 3559 | __ Movq(xmm_scratch, kScratchRegister); |
| 3560 | __ Ucomisd(xmm_scratch, input_reg); |
| 3561 | // Comparing -Infinity with NaN results in "unordered", which sets the |
| 3562 | // zero flag as if both were equal. However, it also sets the carry flag. |
| 3563 | __ j(not_equal, &sqrt, Label::kNear); |
| 3564 | __ j(carry, &sqrt, Label::kNear); |
| 3565 | // If input is -Infinity, return Infinity. |
| 3566 | __ Xorpd(input_reg, input_reg); |
| 3567 | __ Subsd(input_reg, xmm_scratch); |
| 3568 | __ jmp(&done, Label::kNear); |
| 3569 | |
| 3570 | // Square root. |
| 3571 | __ bind(&sqrt); |
| 3572 | __ Xorpd(xmm_scratch, xmm_scratch); |
| 3573 | __ Addsd(input_reg, xmm_scratch); // Convert -0 to +0. |
| 3574 | __ Sqrtsd(input_reg, input_reg); |
| 3575 | __ bind(&done); |
| 3576 | } |
| 3577 | |
| 3578 | |
| 3579 | void LCodeGen::DoPower(LPower* instr) { |
| 3580 | Representation exponent_type = instr->hydrogen()->right()->representation(); |
| 3581 | // Having marked this as a call, we can use any registers. |
| 3582 | // Just make sure that the input/output registers are the expected ones. |
| 3583 | |
| 3584 | Register tagged_exponent = MathPowTaggedDescriptor::exponent(); |
| 3585 | DCHECK(!instr->right()->IsRegister() || |
| 3586 | ToRegister(instr->right()).is(tagged_exponent)); |
| 3587 | DCHECK(!instr->right()->IsDoubleRegister() || |
| 3588 | ToDoubleRegister(instr->right()).is(xmm1)); |
| 3589 | DCHECK(ToDoubleRegister(instr->left()).is(xmm2)); |
| 3590 | DCHECK(ToDoubleRegister(instr->result()).is(xmm3)); |
| 3591 | |
| 3592 | if (exponent_type.IsSmi()) { |
| 3593 | MathPowStub stub(isolate(), MathPowStub::TAGGED); |
| 3594 | __ CallStub(&stub); |
| 3595 | } else if (exponent_type.IsTagged()) { |
| 3596 | Label no_deopt; |
| 3597 | __ JumpIfSmi(tagged_exponent, &no_deopt, Label::kNear); |
| 3598 | __ CmpObjectType(tagged_exponent, HEAP_NUMBER_TYPE, rcx); |
| 3599 | DeoptimizeIf(not_equal, instr, Deoptimizer::kNotAHeapNumber); |
| 3600 | __ bind(&no_deopt); |
| 3601 | MathPowStub stub(isolate(), MathPowStub::TAGGED); |
| 3602 | __ CallStub(&stub); |
| 3603 | } else if (exponent_type.IsInteger32()) { |
| 3604 | MathPowStub stub(isolate(), MathPowStub::INTEGER); |
| 3605 | __ CallStub(&stub); |
| 3606 | } else { |
| 3607 | DCHECK(exponent_type.IsDouble()); |
| 3608 | MathPowStub stub(isolate(), MathPowStub::DOUBLE); |
| 3609 | __ CallStub(&stub); |
| 3610 | } |
| 3611 | } |
| 3612 | |
| 3613 | |
| 3614 | void LCodeGen::DoMathExp(LMathExp* instr) { |
| 3615 | XMMRegister input = ToDoubleRegister(instr->value()); |
| 3616 | XMMRegister result = ToDoubleRegister(instr->result()); |
| 3617 | XMMRegister temp0 = double_scratch0(); |
| 3618 | Register temp1 = ToRegister(instr->temp1()); |
| 3619 | Register temp2 = ToRegister(instr->temp2()); |
| 3620 | |
| 3621 | MathExpGenerator::EmitMathExp(masm(), input, result, temp0, temp1, temp2); |
| 3622 | } |
| 3623 | |
| 3624 | |
| 3625 | void LCodeGen::DoMathLog(LMathLog* instr) { |
| 3626 | DCHECK(instr->value()->Equals(instr->result())); |
| 3627 | XMMRegister input_reg = ToDoubleRegister(instr->value()); |
| 3628 | XMMRegister xmm_scratch = double_scratch0(); |
| 3629 | Label positive, done, zero; |
| 3630 | __ Xorpd(xmm_scratch, xmm_scratch); |
| 3631 | __ Ucomisd(input_reg, xmm_scratch); |
| 3632 | __ j(above, &positive, Label::kNear); |
| 3633 | __ j(not_carry, &zero, Label::kNear); |
| 3634 | __ Pcmpeqd(input_reg, input_reg); |
| 3635 | __ jmp(&done, Label::kNear); |
| 3636 | __ bind(&zero); |
| 3637 | ExternalReference ninf = |
| 3638 | ExternalReference::address_of_negative_infinity(); |
| 3639 | Operand ninf_operand = masm()->ExternalOperand(ninf); |
| 3640 | __ Movsd(input_reg, ninf_operand); |
| 3641 | __ jmp(&done, Label::kNear); |
| 3642 | __ bind(&positive); |
| 3643 | __ fldln2(); |
| 3644 | __ subp(rsp, Immediate(kDoubleSize)); |
| 3645 | __ Movsd(Operand(rsp, 0), input_reg); |
| 3646 | __ fld_d(Operand(rsp, 0)); |
| 3647 | __ fyl2x(); |
| 3648 | __ fstp_d(Operand(rsp, 0)); |
| 3649 | __ Movsd(input_reg, Operand(rsp, 0)); |
| 3650 | __ addp(rsp, Immediate(kDoubleSize)); |
| 3651 | __ bind(&done); |
| 3652 | } |
| 3653 | |
| 3654 | |
| 3655 | void LCodeGen::DoMathClz32(LMathClz32* instr) { |
| 3656 | Register input = ToRegister(instr->value()); |
| 3657 | Register result = ToRegister(instr->result()); |
| 3658 | |
| 3659 | __ Lzcntl(result, input); |
| 3660 | } |
| 3661 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3662 | void LCodeGen::PrepareForTailCall(const ParameterCount& actual, |
| 3663 | Register scratch1, Register scratch2, |
| 3664 | Register scratch3) { |
| 3665 | #if DEBUG |
| 3666 | if (actual.is_reg()) { |
| 3667 | DCHECK(!AreAliased(actual.reg(), scratch1, scratch2, scratch3)); |
| 3668 | } else { |
| 3669 | DCHECK(!AreAliased(scratch1, scratch2, scratch3)); |
| 3670 | } |
| 3671 | #endif |
| 3672 | if (FLAG_code_comments) { |
| 3673 | if (actual.is_reg()) { |
| 3674 | Comment(";;; PrepareForTailCall, actual: %s {", actual.reg().ToString()); |
| 3675 | } else { |
| 3676 | Comment(";;; PrepareForTailCall, actual: %d {", actual.immediate()); |
| 3677 | } |
| 3678 | } |
| 3679 | |
| 3680 | // Check if next frame is an arguments adaptor frame. |
| 3681 | Register caller_args_count_reg = scratch1; |
| 3682 | Label no_arguments_adaptor, formal_parameter_count_loaded; |
| 3683 | __ movp(scratch2, Operand(rbp, StandardFrameConstants::kCallerFPOffset)); |
| 3684 | __ Cmp(Operand(scratch2, StandardFrameConstants::kContextOffset), |
| 3685 | Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)); |
| 3686 | __ j(not_equal, &no_arguments_adaptor, Label::kNear); |
| 3687 | |
| 3688 | // Drop current frame and load arguments count from arguments adaptor frame. |
| 3689 | __ movp(rbp, scratch2); |
| 3690 | __ SmiToInteger32( |
| 3691 | caller_args_count_reg, |
| 3692 | Operand(rbp, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
| 3693 | __ jmp(&formal_parameter_count_loaded, Label::kNear); |
| 3694 | |
| 3695 | __ bind(&no_arguments_adaptor); |
| 3696 | // Load caller's formal parameter count. |
| 3697 | __ movp(caller_args_count_reg, |
| 3698 | Immediate(info()->literal()->parameter_count())); |
| 3699 | |
| 3700 | __ bind(&formal_parameter_count_loaded); |
| 3701 | __ PrepareForTailCall(actual, caller_args_count_reg, scratch2, scratch3, |
| 3702 | ReturnAddressState::kNotOnStack); |
| 3703 | Comment(";;; }"); |
| 3704 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3705 | |
| 3706 | void LCodeGen::DoInvokeFunction(LInvokeFunction* instr) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3707 | HInvokeFunction* hinstr = instr->hydrogen(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3708 | DCHECK(ToRegister(instr->context()).is(rsi)); |
| 3709 | DCHECK(ToRegister(instr->function()).is(rdi)); |
| 3710 | DCHECK(instr->HasPointerMap()); |
| 3711 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3712 | bool is_tail_call = hinstr->tail_call_mode() == TailCallMode::kAllow; |
| 3713 | |
| 3714 | if (is_tail_call) { |
| 3715 | DCHECK(!info()->saves_caller_doubles()); |
| 3716 | ParameterCount actual(instr->arity()); |
| 3717 | // It is safe to use rbx, rcx and r8 as scratch registers here given that |
| 3718 | // 1) we are not going to return to caller function anyway, |
| 3719 | // 2) rbx (expected number of arguments) will be initialized below. |
| 3720 | PrepareForTailCall(actual, rbx, rcx, r8); |
| 3721 | } |
| 3722 | |
| 3723 | Handle<JSFunction> known_function = hinstr->known_function(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3724 | if (known_function.is_null()) { |
| 3725 | LPointerMap* pointers = instr->pointer_map(); |
| 3726 | SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3727 | ParameterCount actual(instr->arity()); |
| 3728 | InvokeFlag flag = is_tail_call ? JUMP_FUNCTION : CALL_FUNCTION; |
| 3729 | __ InvokeFunction(rdi, no_reg, actual, flag, generator); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3730 | } else { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3731 | CallKnownFunction(known_function, hinstr->formal_parameter_count(), |
| 3732 | instr->arity(), is_tail_call, instr); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3733 | } |
| 3734 | } |
| 3735 | |
| 3736 | |
| 3737 | void LCodeGen::DoCallNewArray(LCallNewArray* instr) { |
| 3738 | DCHECK(ToRegister(instr->context()).is(rsi)); |
| 3739 | DCHECK(ToRegister(instr->constructor()).is(rdi)); |
| 3740 | DCHECK(ToRegister(instr->result()).is(rax)); |
| 3741 | |
| 3742 | __ Set(rax, instr->arity()); |
| 3743 | if (instr->arity() == 1) { |
| 3744 | // We only need the allocation site for the case we have a length argument. |
| 3745 | // The case may bail out to the runtime, which will determine the correct |
| 3746 | // elements kind with the site. |
| 3747 | __ Move(rbx, instr->hydrogen()->site()); |
| 3748 | } else { |
| 3749 | __ LoadRoot(rbx, Heap::kUndefinedValueRootIndex); |
| 3750 | } |
| 3751 | |
| 3752 | ElementsKind kind = instr->hydrogen()->elements_kind(); |
| 3753 | AllocationSiteOverrideMode override_mode = |
| 3754 | (AllocationSite::GetMode(kind) == TRACK_ALLOCATION_SITE) |
| 3755 | ? DISABLE_ALLOCATION_SITES |
| 3756 | : DONT_OVERRIDE; |
| 3757 | |
| 3758 | if (instr->arity() == 0) { |
| 3759 | ArrayNoArgumentConstructorStub stub(isolate(), kind, override_mode); |
| 3760 | CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr); |
| 3761 | } else if (instr->arity() == 1) { |
| 3762 | Label done; |
| 3763 | if (IsFastPackedElementsKind(kind)) { |
| 3764 | Label packed_case; |
| 3765 | // We might need a change here |
| 3766 | // look at the first argument |
| 3767 | __ movp(rcx, Operand(rsp, 0)); |
| 3768 | __ testp(rcx, rcx); |
| 3769 | __ j(zero, &packed_case, Label::kNear); |
| 3770 | |
| 3771 | ElementsKind holey_kind = GetHoleyElementsKind(kind); |
| 3772 | ArraySingleArgumentConstructorStub stub(isolate(), |
| 3773 | holey_kind, |
| 3774 | override_mode); |
| 3775 | CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr); |
| 3776 | __ jmp(&done, Label::kNear); |
| 3777 | __ bind(&packed_case); |
| 3778 | } |
| 3779 | |
| 3780 | ArraySingleArgumentConstructorStub stub(isolate(), kind, override_mode); |
| 3781 | CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr); |
| 3782 | __ bind(&done); |
| 3783 | } else { |
| 3784 | ArrayNArgumentsConstructorStub stub(isolate(), kind, override_mode); |
| 3785 | CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr); |
| 3786 | } |
| 3787 | } |
| 3788 | |
| 3789 | |
| 3790 | void LCodeGen::DoCallRuntime(LCallRuntime* instr) { |
| 3791 | DCHECK(ToRegister(instr->context()).is(rsi)); |
| 3792 | CallRuntime(instr->function(), instr->arity(), instr, instr->save_doubles()); |
| 3793 | } |
| 3794 | |
| 3795 | |
| 3796 | void LCodeGen::DoStoreCodeEntry(LStoreCodeEntry* instr) { |
| 3797 | Register function = ToRegister(instr->function()); |
| 3798 | Register code_object = ToRegister(instr->code_object()); |
| 3799 | __ leap(code_object, FieldOperand(code_object, Code::kHeaderSize)); |
| 3800 | __ movp(FieldOperand(function, JSFunction::kCodeEntryOffset), code_object); |
| 3801 | } |
| 3802 | |
| 3803 | |
| 3804 | void LCodeGen::DoInnerAllocatedObject(LInnerAllocatedObject* instr) { |
| 3805 | Register result = ToRegister(instr->result()); |
| 3806 | Register base = ToRegister(instr->base_object()); |
| 3807 | if (instr->offset()->IsConstantOperand()) { |
| 3808 | LConstantOperand* offset = LConstantOperand::cast(instr->offset()); |
| 3809 | __ leap(result, Operand(base, ToInteger32(offset))); |
| 3810 | } else { |
| 3811 | Register offset = ToRegister(instr->offset()); |
| 3812 | __ leap(result, Operand(base, offset, times_1, 0)); |
| 3813 | } |
| 3814 | } |
| 3815 | |
| 3816 | |
| 3817 | void LCodeGen::DoStoreNamedField(LStoreNamedField* instr) { |
| 3818 | HStoreNamedField* hinstr = instr->hydrogen(); |
| 3819 | Representation representation = instr->representation(); |
| 3820 | |
| 3821 | HObjectAccess access = hinstr->access(); |
| 3822 | int offset = access.offset(); |
| 3823 | |
| 3824 | if (access.IsExternalMemory()) { |
| 3825 | DCHECK(!hinstr->NeedsWriteBarrier()); |
| 3826 | Register value = ToRegister(instr->value()); |
| 3827 | if (instr->object()->IsConstantOperand()) { |
| 3828 | DCHECK(value.is(rax)); |
| 3829 | LConstantOperand* object = LConstantOperand::cast(instr->object()); |
| 3830 | __ store_rax(ToExternalReference(object)); |
| 3831 | } else { |
| 3832 | Register object = ToRegister(instr->object()); |
| 3833 | __ Store(MemOperand(object, offset), value, representation); |
| 3834 | } |
| 3835 | return; |
| 3836 | } |
| 3837 | |
| 3838 | Register object = ToRegister(instr->object()); |
| 3839 | __ AssertNotSmi(object); |
| 3840 | |
| 3841 | DCHECK(!representation.IsSmi() || |
| 3842 | !instr->value()->IsConstantOperand() || |
| 3843 | IsInteger32Constant(LConstantOperand::cast(instr->value()))); |
| 3844 | if (!FLAG_unbox_double_fields && representation.IsDouble()) { |
| 3845 | DCHECK(access.IsInobject()); |
| 3846 | DCHECK(!hinstr->has_transition()); |
| 3847 | DCHECK(!hinstr->NeedsWriteBarrier()); |
| 3848 | XMMRegister value = ToDoubleRegister(instr->value()); |
| 3849 | __ Movsd(FieldOperand(object, offset), value); |
| 3850 | return; |
| 3851 | } |
| 3852 | |
| 3853 | if (hinstr->has_transition()) { |
| 3854 | Handle<Map> transition = hinstr->transition_map(); |
| 3855 | AddDeprecationDependency(transition); |
| 3856 | if (!hinstr->NeedsWriteBarrierForMap()) { |
| 3857 | __ Move(FieldOperand(object, HeapObject::kMapOffset), transition); |
| 3858 | } else { |
| 3859 | Register temp = ToRegister(instr->temp()); |
| 3860 | __ Move(kScratchRegister, transition); |
| 3861 | __ movp(FieldOperand(object, HeapObject::kMapOffset), kScratchRegister); |
| 3862 | // Update the write barrier for the map field. |
| 3863 | __ RecordWriteForMap(object, |
| 3864 | kScratchRegister, |
| 3865 | temp, |
| 3866 | kSaveFPRegs); |
| 3867 | } |
| 3868 | } |
| 3869 | |
| 3870 | // Do the store. |
| 3871 | Register write_register = object; |
| 3872 | if (!access.IsInobject()) { |
| 3873 | write_register = ToRegister(instr->temp()); |
| 3874 | __ movp(write_register, FieldOperand(object, JSObject::kPropertiesOffset)); |
| 3875 | } |
| 3876 | |
| 3877 | if (representation.IsSmi() && SmiValuesAre32Bits() && |
| 3878 | hinstr->value()->representation().IsInteger32()) { |
| 3879 | DCHECK(hinstr->store_mode() == STORE_TO_INITIALIZED_ENTRY); |
| 3880 | if (FLAG_debug_code) { |
| 3881 | Register scratch = kScratchRegister; |
| 3882 | __ Load(scratch, FieldOperand(write_register, offset), representation); |
| 3883 | __ AssertSmi(scratch); |
| 3884 | } |
| 3885 | // Store int value directly to upper half of the smi. |
| 3886 | STATIC_ASSERT(kSmiTag == 0); |
| 3887 | DCHECK(kSmiTagSize + kSmiShiftSize == 32); |
| 3888 | offset += kPointerSize / 2; |
| 3889 | representation = Representation::Integer32(); |
| 3890 | } |
| 3891 | |
| 3892 | Operand operand = FieldOperand(write_register, offset); |
| 3893 | |
| 3894 | if (FLAG_unbox_double_fields && representation.IsDouble()) { |
| 3895 | DCHECK(access.IsInobject()); |
| 3896 | XMMRegister value = ToDoubleRegister(instr->value()); |
| 3897 | __ Movsd(operand, value); |
| 3898 | |
| 3899 | } else if (instr->value()->IsRegister()) { |
| 3900 | Register value = ToRegister(instr->value()); |
| 3901 | __ Store(operand, value, representation); |
| 3902 | } else { |
| 3903 | LConstantOperand* operand_value = LConstantOperand::cast(instr->value()); |
| 3904 | if (IsInteger32Constant(operand_value)) { |
| 3905 | DCHECK(!hinstr->NeedsWriteBarrier()); |
| 3906 | int32_t value = ToInteger32(operand_value); |
| 3907 | if (representation.IsSmi()) { |
| 3908 | __ Move(operand, Smi::FromInt(value)); |
| 3909 | |
| 3910 | } else { |
| 3911 | __ movl(operand, Immediate(value)); |
| 3912 | } |
| 3913 | |
| 3914 | } else if (IsExternalConstant(operand_value)) { |
| 3915 | DCHECK(!hinstr->NeedsWriteBarrier()); |
| 3916 | ExternalReference ptr = ToExternalReference(operand_value); |
| 3917 | __ Move(kScratchRegister, ptr); |
| 3918 | __ movp(operand, kScratchRegister); |
| 3919 | } else { |
| 3920 | Handle<Object> handle_value = ToHandle(operand_value); |
| 3921 | DCHECK(!hinstr->NeedsWriteBarrier()); |
| 3922 | __ Move(operand, handle_value); |
| 3923 | } |
| 3924 | } |
| 3925 | |
| 3926 | if (hinstr->NeedsWriteBarrier()) { |
| 3927 | Register value = ToRegister(instr->value()); |
| 3928 | Register temp = access.IsInobject() ? ToRegister(instr->temp()) : object; |
| 3929 | // Update the write barrier for the object for in-object properties. |
| 3930 | __ RecordWriteField(write_register, |
| 3931 | offset, |
| 3932 | value, |
| 3933 | temp, |
| 3934 | kSaveFPRegs, |
| 3935 | EMIT_REMEMBERED_SET, |
| 3936 | hinstr->SmiCheckForWriteBarrier(), |
| 3937 | hinstr->PointersToHereCheckForValue()); |
| 3938 | } |
| 3939 | } |
| 3940 | |
| 3941 | |
| 3942 | void LCodeGen::DoStoreNamedGeneric(LStoreNamedGeneric* instr) { |
| 3943 | DCHECK(ToRegister(instr->context()).is(rsi)); |
| 3944 | DCHECK(ToRegister(instr->object()).is(StoreDescriptor::ReceiverRegister())); |
| 3945 | DCHECK(ToRegister(instr->value()).is(StoreDescriptor::ValueRegister())); |
| 3946 | |
| 3947 | if (instr->hydrogen()->HasVectorAndSlot()) { |
| 3948 | EmitVectorStoreICRegisters<LStoreNamedGeneric>(instr); |
| 3949 | } |
| 3950 | |
| 3951 | __ Move(StoreDescriptor::NameRegister(), instr->hydrogen()->name()); |
| 3952 | Handle<Code> ic = CodeFactory::StoreICInOptimizedCode( |
| 3953 | isolate(), instr->language_mode(), |
| 3954 | instr->hydrogen()->initialization_state()).code(); |
| 3955 | CallCode(ic, RelocInfo::CODE_TARGET, instr); |
| 3956 | } |
| 3957 | |
| 3958 | |
| 3959 | void LCodeGen::DoBoundsCheck(LBoundsCheck* instr) { |
| 3960 | Representation representation = instr->hydrogen()->length()->representation(); |
| 3961 | DCHECK(representation.Equals(instr->hydrogen()->index()->representation())); |
| 3962 | DCHECK(representation.IsSmiOrInteger32()); |
| 3963 | |
| 3964 | Condition cc = instr->hydrogen()->allow_equality() ? below : below_equal; |
| 3965 | if (instr->length()->IsConstantOperand()) { |
| 3966 | int32_t length = ToInteger32(LConstantOperand::cast(instr->length())); |
| 3967 | Register index = ToRegister(instr->index()); |
| 3968 | if (representation.IsSmi()) { |
| 3969 | __ Cmp(index, Smi::FromInt(length)); |
| 3970 | } else { |
| 3971 | __ cmpl(index, Immediate(length)); |
| 3972 | } |
| 3973 | cc = CommuteCondition(cc); |
| 3974 | } else if (instr->index()->IsConstantOperand()) { |
| 3975 | int32_t index = ToInteger32(LConstantOperand::cast(instr->index())); |
| 3976 | if (instr->length()->IsRegister()) { |
| 3977 | Register length = ToRegister(instr->length()); |
| 3978 | if (representation.IsSmi()) { |
| 3979 | __ Cmp(length, Smi::FromInt(index)); |
| 3980 | } else { |
| 3981 | __ cmpl(length, Immediate(index)); |
| 3982 | } |
| 3983 | } else { |
| 3984 | Operand length = ToOperand(instr->length()); |
| 3985 | if (representation.IsSmi()) { |
| 3986 | __ Cmp(length, Smi::FromInt(index)); |
| 3987 | } else { |
| 3988 | __ cmpl(length, Immediate(index)); |
| 3989 | } |
| 3990 | } |
| 3991 | } else { |
| 3992 | Register index = ToRegister(instr->index()); |
| 3993 | if (instr->length()->IsRegister()) { |
| 3994 | Register length = ToRegister(instr->length()); |
| 3995 | if (representation.IsSmi()) { |
| 3996 | __ cmpp(length, index); |
| 3997 | } else { |
| 3998 | __ cmpl(length, index); |
| 3999 | } |
| 4000 | } else { |
| 4001 | Operand length = ToOperand(instr->length()); |
| 4002 | if (representation.IsSmi()) { |
| 4003 | __ cmpp(length, index); |
| 4004 | } else { |
| 4005 | __ cmpl(length, index); |
| 4006 | } |
| 4007 | } |
| 4008 | } |
| 4009 | if (FLAG_debug_code && instr->hydrogen()->skip_check()) { |
| 4010 | Label done; |
| 4011 | __ j(NegateCondition(cc), &done, Label::kNear); |
| 4012 | __ int3(); |
| 4013 | __ bind(&done); |
| 4014 | } else { |
| 4015 | DeoptimizeIf(cc, instr, Deoptimizer::kOutOfBounds); |
| 4016 | } |
| 4017 | } |
| 4018 | |
| 4019 | |
| 4020 | void LCodeGen::DoStoreKeyedExternalArray(LStoreKeyed* instr) { |
| 4021 | ElementsKind elements_kind = instr->elements_kind(); |
| 4022 | LOperand* key = instr->key(); |
| 4023 | if (kPointerSize == kInt32Size && !key->IsConstantOperand()) { |
| 4024 | Register key_reg = ToRegister(key); |
| 4025 | Representation key_representation = |
| 4026 | instr->hydrogen()->key()->representation(); |
| 4027 | if (ExternalArrayOpRequiresTemp(key_representation, elements_kind)) { |
| 4028 | __ SmiToInteger64(key_reg, key_reg); |
| 4029 | } else if (instr->hydrogen()->IsDehoisted()) { |
| 4030 | // Sign extend key because it could be a 32 bit negative value |
| 4031 | // and the dehoisted address computation happens in 64 bits |
| 4032 | __ movsxlq(key_reg, key_reg); |
| 4033 | } |
| 4034 | } |
| 4035 | Operand operand(BuildFastArrayOperand( |
| 4036 | instr->elements(), |
| 4037 | key, |
| 4038 | instr->hydrogen()->key()->representation(), |
| 4039 | elements_kind, |
| 4040 | instr->base_offset())); |
| 4041 | |
| 4042 | if (elements_kind == FLOAT32_ELEMENTS) { |
| 4043 | XMMRegister value(ToDoubleRegister(instr->value())); |
| 4044 | __ Cvtsd2ss(value, value); |
| 4045 | __ Movss(operand, value); |
| 4046 | } else if (elements_kind == FLOAT64_ELEMENTS) { |
| 4047 | __ Movsd(operand, ToDoubleRegister(instr->value())); |
| 4048 | } else { |
| 4049 | Register value(ToRegister(instr->value())); |
| 4050 | switch (elements_kind) { |
| 4051 | case INT8_ELEMENTS: |
| 4052 | case UINT8_ELEMENTS: |
| 4053 | case UINT8_CLAMPED_ELEMENTS: |
| 4054 | __ movb(operand, value); |
| 4055 | break; |
| 4056 | case INT16_ELEMENTS: |
| 4057 | case UINT16_ELEMENTS: |
| 4058 | __ movw(operand, value); |
| 4059 | break; |
| 4060 | case INT32_ELEMENTS: |
| 4061 | case UINT32_ELEMENTS: |
| 4062 | __ movl(operand, value); |
| 4063 | break; |
| 4064 | case FLOAT32_ELEMENTS: |
| 4065 | case FLOAT64_ELEMENTS: |
| 4066 | case FAST_ELEMENTS: |
| 4067 | case FAST_SMI_ELEMENTS: |
| 4068 | case FAST_DOUBLE_ELEMENTS: |
| 4069 | case FAST_HOLEY_ELEMENTS: |
| 4070 | case FAST_HOLEY_SMI_ELEMENTS: |
| 4071 | case FAST_HOLEY_DOUBLE_ELEMENTS: |
| 4072 | case DICTIONARY_ELEMENTS: |
| 4073 | case FAST_SLOPPY_ARGUMENTS_ELEMENTS: |
| 4074 | case SLOW_SLOPPY_ARGUMENTS_ELEMENTS: |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 4075 | case FAST_STRING_WRAPPER_ELEMENTS: |
| 4076 | case SLOW_STRING_WRAPPER_ELEMENTS: |
| 4077 | case NO_ELEMENTS: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4078 | UNREACHABLE(); |
| 4079 | break; |
| 4080 | } |
| 4081 | } |
| 4082 | } |
| 4083 | |
| 4084 | |
| 4085 | void LCodeGen::DoStoreKeyedFixedDoubleArray(LStoreKeyed* instr) { |
| 4086 | XMMRegister value = ToDoubleRegister(instr->value()); |
| 4087 | LOperand* key = instr->key(); |
| 4088 | if (kPointerSize == kInt32Size && !key->IsConstantOperand() && |
| 4089 | instr->hydrogen()->IsDehoisted()) { |
| 4090 | // Sign extend key because it could be a 32 bit negative value |
| 4091 | // and the dehoisted address computation happens in 64 bits |
| 4092 | __ movsxlq(ToRegister(key), ToRegister(key)); |
| 4093 | } |
| 4094 | if (instr->NeedsCanonicalization()) { |
| 4095 | XMMRegister xmm_scratch = double_scratch0(); |
| 4096 | // Turn potential sNaN value into qNaN. |
| 4097 | __ Xorpd(xmm_scratch, xmm_scratch); |
| 4098 | __ Subsd(value, xmm_scratch); |
| 4099 | } |
| 4100 | |
| 4101 | Operand double_store_operand = BuildFastArrayOperand( |
| 4102 | instr->elements(), |
| 4103 | key, |
| 4104 | instr->hydrogen()->key()->representation(), |
| 4105 | FAST_DOUBLE_ELEMENTS, |
| 4106 | instr->base_offset()); |
| 4107 | |
| 4108 | __ Movsd(double_store_operand, value); |
| 4109 | } |
| 4110 | |
| 4111 | |
| 4112 | void LCodeGen::DoStoreKeyedFixedArray(LStoreKeyed* instr) { |
| 4113 | HStoreKeyed* hinstr = instr->hydrogen(); |
| 4114 | LOperand* key = instr->key(); |
| 4115 | int offset = instr->base_offset(); |
| 4116 | Representation representation = hinstr->value()->representation(); |
| 4117 | |
| 4118 | if (kPointerSize == kInt32Size && !key->IsConstantOperand() && |
| 4119 | instr->hydrogen()->IsDehoisted()) { |
| 4120 | // Sign extend key because it could be a 32 bit negative value |
| 4121 | // and the dehoisted address computation happens in 64 bits |
| 4122 | __ movsxlq(ToRegister(key), ToRegister(key)); |
| 4123 | } |
| 4124 | if (representation.IsInteger32() && SmiValuesAre32Bits()) { |
| 4125 | DCHECK(hinstr->store_mode() == STORE_TO_INITIALIZED_ENTRY); |
| 4126 | DCHECK(hinstr->elements_kind() == FAST_SMI_ELEMENTS); |
| 4127 | if (FLAG_debug_code) { |
| 4128 | Register scratch = kScratchRegister; |
| 4129 | __ Load(scratch, |
| 4130 | BuildFastArrayOperand(instr->elements(), |
| 4131 | key, |
| 4132 | instr->hydrogen()->key()->representation(), |
| 4133 | FAST_ELEMENTS, |
| 4134 | offset), |
| 4135 | Representation::Smi()); |
| 4136 | __ AssertSmi(scratch); |
| 4137 | } |
| 4138 | // Store int value directly to upper half of the smi. |
| 4139 | STATIC_ASSERT(kSmiTag == 0); |
| 4140 | DCHECK(kSmiTagSize + kSmiShiftSize == 32); |
| 4141 | offset += kPointerSize / 2; |
| 4142 | } |
| 4143 | |
| 4144 | Operand operand = |
| 4145 | BuildFastArrayOperand(instr->elements(), |
| 4146 | key, |
| 4147 | instr->hydrogen()->key()->representation(), |
| 4148 | FAST_ELEMENTS, |
| 4149 | offset); |
| 4150 | if (instr->value()->IsRegister()) { |
| 4151 | __ Store(operand, ToRegister(instr->value()), representation); |
| 4152 | } else { |
| 4153 | LConstantOperand* operand_value = LConstantOperand::cast(instr->value()); |
| 4154 | if (IsInteger32Constant(operand_value)) { |
| 4155 | int32_t value = ToInteger32(operand_value); |
| 4156 | if (representation.IsSmi()) { |
| 4157 | __ Move(operand, Smi::FromInt(value)); |
| 4158 | |
| 4159 | } else { |
| 4160 | __ movl(operand, Immediate(value)); |
| 4161 | } |
| 4162 | } else { |
| 4163 | Handle<Object> handle_value = ToHandle(operand_value); |
| 4164 | __ Move(operand, handle_value); |
| 4165 | } |
| 4166 | } |
| 4167 | |
| 4168 | if (hinstr->NeedsWriteBarrier()) { |
| 4169 | Register elements = ToRegister(instr->elements()); |
| 4170 | DCHECK(instr->value()->IsRegister()); |
| 4171 | Register value = ToRegister(instr->value()); |
| 4172 | DCHECK(!key->IsConstantOperand()); |
| 4173 | SmiCheck check_needed = hinstr->value()->type().IsHeapObject() |
| 4174 | ? OMIT_SMI_CHECK : INLINE_SMI_CHECK; |
| 4175 | // Compute address of modified element and store it into key register. |
| 4176 | Register key_reg(ToRegister(key)); |
| 4177 | __ leap(key_reg, operand); |
| 4178 | __ RecordWrite(elements, |
| 4179 | key_reg, |
| 4180 | value, |
| 4181 | kSaveFPRegs, |
| 4182 | EMIT_REMEMBERED_SET, |
| 4183 | check_needed, |
| 4184 | hinstr->PointersToHereCheckForValue()); |
| 4185 | } |
| 4186 | } |
| 4187 | |
| 4188 | |
| 4189 | void LCodeGen::DoStoreKeyed(LStoreKeyed* instr) { |
| 4190 | if (instr->is_fixed_typed_array()) { |
| 4191 | DoStoreKeyedExternalArray(instr); |
| 4192 | } else if (instr->hydrogen()->value()->representation().IsDouble()) { |
| 4193 | DoStoreKeyedFixedDoubleArray(instr); |
| 4194 | } else { |
| 4195 | DoStoreKeyedFixedArray(instr); |
| 4196 | } |
| 4197 | } |
| 4198 | |
| 4199 | |
| 4200 | void LCodeGen::DoStoreKeyedGeneric(LStoreKeyedGeneric* instr) { |
| 4201 | DCHECK(ToRegister(instr->context()).is(rsi)); |
| 4202 | DCHECK(ToRegister(instr->object()).is(StoreDescriptor::ReceiverRegister())); |
| 4203 | DCHECK(ToRegister(instr->key()).is(StoreDescriptor::NameRegister())); |
| 4204 | DCHECK(ToRegister(instr->value()).is(StoreDescriptor::ValueRegister())); |
| 4205 | |
| 4206 | if (instr->hydrogen()->HasVectorAndSlot()) { |
| 4207 | EmitVectorStoreICRegisters<LStoreKeyedGeneric>(instr); |
| 4208 | } |
| 4209 | |
| 4210 | Handle<Code> ic = CodeFactory::KeyedStoreICInOptimizedCode( |
| 4211 | isolate(), instr->language_mode(), |
| 4212 | instr->hydrogen()->initialization_state()).code(); |
| 4213 | CallCode(ic, RelocInfo::CODE_TARGET, instr); |
| 4214 | } |
| 4215 | |
| 4216 | |
| 4217 | void LCodeGen::DoMaybeGrowElements(LMaybeGrowElements* instr) { |
| 4218 | class DeferredMaybeGrowElements final : public LDeferredCode { |
| 4219 | public: |
| 4220 | DeferredMaybeGrowElements(LCodeGen* codegen, LMaybeGrowElements* instr) |
| 4221 | : LDeferredCode(codegen), instr_(instr) {} |
| 4222 | void Generate() override { codegen()->DoDeferredMaybeGrowElements(instr_); } |
| 4223 | LInstruction* instr() override { return instr_; } |
| 4224 | |
| 4225 | private: |
| 4226 | LMaybeGrowElements* instr_; |
| 4227 | }; |
| 4228 | |
| 4229 | Register result = rax; |
| 4230 | DeferredMaybeGrowElements* deferred = |
| 4231 | new (zone()) DeferredMaybeGrowElements(this, instr); |
| 4232 | LOperand* key = instr->key(); |
| 4233 | LOperand* current_capacity = instr->current_capacity(); |
| 4234 | |
| 4235 | DCHECK(instr->hydrogen()->key()->representation().IsInteger32()); |
| 4236 | DCHECK(instr->hydrogen()->current_capacity()->representation().IsInteger32()); |
| 4237 | DCHECK(key->IsConstantOperand() || key->IsRegister()); |
| 4238 | DCHECK(current_capacity->IsConstantOperand() || |
| 4239 | current_capacity->IsRegister()); |
| 4240 | |
| 4241 | if (key->IsConstantOperand() && current_capacity->IsConstantOperand()) { |
| 4242 | int32_t constant_key = ToInteger32(LConstantOperand::cast(key)); |
| 4243 | int32_t constant_capacity = |
| 4244 | ToInteger32(LConstantOperand::cast(current_capacity)); |
| 4245 | if (constant_key >= constant_capacity) { |
| 4246 | // Deferred case. |
| 4247 | __ jmp(deferred->entry()); |
| 4248 | } |
| 4249 | } else if (key->IsConstantOperand()) { |
| 4250 | int32_t constant_key = ToInteger32(LConstantOperand::cast(key)); |
| 4251 | __ cmpl(ToRegister(current_capacity), Immediate(constant_key)); |
| 4252 | __ j(less_equal, deferred->entry()); |
| 4253 | } else if (current_capacity->IsConstantOperand()) { |
| 4254 | int32_t constant_capacity = |
| 4255 | ToInteger32(LConstantOperand::cast(current_capacity)); |
| 4256 | __ cmpl(ToRegister(key), Immediate(constant_capacity)); |
| 4257 | __ j(greater_equal, deferred->entry()); |
| 4258 | } else { |
| 4259 | __ cmpl(ToRegister(key), ToRegister(current_capacity)); |
| 4260 | __ j(greater_equal, deferred->entry()); |
| 4261 | } |
| 4262 | |
| 4263 | if (instr->elements()->IsRegister()) { |
| 4264 | __ movp(result, ToRegister(instr->elements())); |
| 4265 | } else { |
| 4266 | __ movp(result, ToOperand(instr->elements())); |
| 4267 | } |
| 4268 | |
| 4269 | __ bind(deferred->exit()); |
| 4270 | } |
| 4271 | |
| 4272 | |
| 4273 | void LCodeGen::DoDeferredMaybeGrowElements(LMaybeGrowElements* instr) { |
| 4274 | // TODO(3095996): Get rid of this. For now, we need to make the |
| 4275 | // result register contain a valid pointer because it is already |
| 4276 | // contained in the register pointer map. |
| 4277 | Register result = rax; |
| 4278 | __ Move(result, Smi::FromInt(0)); |
| 4279 | |
| 4280 | // We have to call a stub. |
| 4281 | { |
| 4282 | PushSafepointRegistersScope scope(this); |
| 4283 | if (instr->object()->IsConstantOperand()) { |
| 4284 | LConstantOperand* constant_object = |
| 4285 | LConstantOperand::cast(instr->object()); |
| 4286 | if (IsSmiConstant(constant_object)) { |
| 4287 | Smi* immediate = ToSmi(constant_object); |
| 4288 | __ Move(result, immediate); |
| 4289 | } else { |
| 4290 | Handle<Object> handle_value = ToHandle(constant_object); |
| 4291 | __ Move(result, handle_value); |
| 4292 | } |
| 4293 | } else if (instr->object()->IsRegister()) { |
| 4294 | __ Move(result, ToRegister(instr->object())); |
| 4295 | } else { |
| 4296 | __ movp(result, ToOperand(instr->object())); |
| 4297 | } |
| 4298 | |
| 4299 | LOperand* key = instr->key(); |
| 4300 | if (key->IsConstantOperand()) { |
| 4301 | __ Move(rbx, ToSmi(LConstantOperand::cast(key))); |
| 4302 | } else { |
| 4303 | __ Move(rbx, ToRegister(key)); |
| 4304 | __ Integer32ToSmi(rbx, rbx); |
| 4305 | } |
| 4306 | |
| 4307 | GrowArrayElementsStub stub(isolate(), instr->hydrogen()->is_js_array(), |
| 4308 | instr->hydrogen()->kind()); |
| 4309 | __ CallStub(&stub); |
| 4310 | RecordSafepointWithLazyDeopt(instr, RECORD_SAFEPOINT_WITH_REGISTERS, 0); |
| 4311 | __ StoreToSafepointRegisterSlot(result, result); |
| 4312 | } |
| 4313 | |
| 4314 | // Deopt on smi, which means the elements array changed to dictionary mode. |
| 4315 | Condition is_smi = __ CheckSmi(result); |
| 4316 | DeoptimizeIf(is_smi, instr, Deoptimizer::kSmi); |
| 4317 | } |
| 4318 | |
| 4319 | |
| 4320 | void LCodeGen::DoTransitionElementsKind(LTransitionElementsKind* instr) { |
| 4321 | Register object_reg = ToRegister(instr->object()); |
| 4322 | |
| 4323 | Handle<Map> from_map = instr->original_map(); |
| 4324 | Handle<Map> to_map = instr->transitioned_map(); |
| 4325 | ElementsKind from_kind = instr->from_kind(); |
| 4326 | ElementsKind to_kind = instr->to_kind(); |
| 4327 | |
| 4328 | Label not_applicable; |
| 4329 | __ Cmp(FieldOperand(object_reg, HeapObject::kMapOffset), from_map); |
| 4330 | __ j(not_equal, ¬_applicable); |
| 4331 | if (IsSimpleMapChangeTransition(from_kind, to_kind)) { |
| 4332 | Register new_map_reg = ToRegister(instr->new_map_temp()); |
| 4333 | __ Move(new_map_reg, to_map, RelocInfo::EMBEDDED_OBJECT); |
| 4334 | __ movp(FieldOperand(object_reg, HeapObject::kMapOffset), new_map_reg); |
| 4335 | // Write barrier. |
| 4336 | __ RecordWriteForMap(object_reg, new_map_reg, ToRegister(instr->temp()), |
| 4337 | kDontSaveFPRegs); |
| 4338 | } else { |
| 4339 | DCHECK(object_reg.is(rax)); |
| 4340 | DCHECK(ToRegister(instr->context()).is(rsi)); |
| 4341 | PushSafepointRegistersScope scope(this); |
| 4342 | __ Move(rbx, to_map); |
| 4343 | bool is_js_array = from_map->instance_type() == JS_ARRAY_TYPE; |
| 4344 | TransitionElementsKindStub stub(isolate(), from_kind, to_kind, is_js_array); |
| 4345 | __ CallStub(&stub); |
| 4346 | RecordSafepointWithLazyDeopt(instr, RECORD_SAFEPOINT_WITH_REGISTERS, 0); |
| 4347 | } |
| 4348 | __ bind(¬_applicable); |
| 4349 | } |
| 4350 | |
| 4351 | |
| 4352 | void LCodeGen::DoTrapAllocationMemento(LTrapAllocationMemento* instr) { |
| 4353 | Register object = ToRegister(instr->object()); |
| 4354 | Register temp = ToRegister(instr->temp()); |
| 4355 | Label no_memento_found; |
| 4356 | __ TestJSArrayForAllocationMemento(object, temp, &no_memento_found); |
| 4357 | DeoptimizeIf(equal, instr, Deoptimizer::kMementoFound); |
| 4358 | __ bind(&no_memento_found); |
| 4359 | } |
| 4360 | |
| 4361 | |
| 4362 | void LCodeGen::DoStringAdd(LStringAdd* instr) { |
| 4363 | DCHECK(ToRegister(instr->context()).is(rsi)); |
| 4364 | DCHECK(ToRegister(instr->left()).is(rdx)); |
| 4365 | DCHECK(ToRegister(instr->right()).is(rax)); |
| 4366 | StringAddStub stub(isolate(), |
| 4367 | instr->hydrogen()->flags(), |
| 4368 | instr->hydrogen()->pretenure_flag()); |
| 4369 | CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr); |
| 4370 | } |
| 4371 | |
| 4372 | |
| 4373 | void LCodeGen::DoStringCharCodeAt(LStringCharCodeAt* instr) { |
| 4374 | class DeferredStringCharCodeAt final : public LDeferredCode { |
| 4375 | public: |
| 4376 | DeferredStringCharCodeAt(LCodeGen* codegen, LStringCharCodeAt* instr) |
| 4377 | : LDeferredCode(codegen), instr_(instr) { } |
| 4378 | void Generate() override { codegen()->DoDeferredStringCharCodeAt(instr_); } |
| 4379 | LInstruction* instr() override { return instr_; } |
| 4380 | |
| 4381 | private: |
| 4382 | LStringCharCodeAt* instr_; |
| 4383 | }; |
| 4384 | |
| 4385 | DeferredStringCharCodeAt* deferred = |
| 4386 | new(zone()) DeferredStringCharCodeAt(this, instr); |
| 4387 | |
| 4388 | StringCharLoadGenerator::Generate(masm(), |
| 4389 | ToRegister(instr->string()), |
| 4390 | ToRegister(instr->index()), |
| 4391 | ToRegister(instr->result()), |
| 4392 | deferred->entry()); |
| 4393 | __ bind(deferred->exit()); |
| 4394 | } |
| 4395 | |
| 4396 | |
| 4397 | void LCodeGen::DoDeferredStringCharCodeAt(LStringCharCodeAt* instr) { |
| 4398 | Register string = ToRegister(instr->string()); |
| 4399 | Register result = ToRegister(instr->result()); |
| 4400 | |
| 4401 | // TODO(3095996): Get rid of this. For now, we need to make the |
| 4402 | // result register contain a valid pointer because it is already |
| 4403 | // contained in the register pointer map. |
| 4404 | __ Set(result, 0); |
| 4405 | |
| 4406 | PushSafepointRegistersScope scope(this); |
| 4407 | __ Push(string); |
| 4408 | // Push the index as a smi. This is safe because of the checks in |
| 4409 | // DoStringCharCodeAt above. |
| 4410 | STATIC_ASSERT(String::kMaxLength <= Smi::kMaxValue); |
| 4411 | if (instr->index()->IsConstantOperand()) { |
| 4412 | int32_t const_index = ToInteger32(LConstantOperand::cast(instr->index())); |
| 4413 | __ Push(Smi::FromInt(const_index)); |
| 4414 | } else { |
| 4415 | Register index = ToRegister(instr->index()); |
| 4416 | __ Integer32ToSmi(index, index); |
| 4417 | __ Push(index); |
| 4418 | } |
| 4419 | CallRuntimeFromDeferred( |
| 4420 | Runtime::kStringCharCodeAtRT, 2, instr, instr->context()); |
| 4421 | __ AssertSmi(rax); |
| 4422 | __ SmiToInteger32(rax, rax); |
| 4423 | __ StoreToSafepointRegisterSlot(result, rax); |
| 4424 | } |
| 4425 | |
| 4426 | |
| 4427 | void LCodeGen::DoStringCharFromCode(LStringCharFromCode* instr) { |
| 4428 | class DeferredStringCharFromCode final : public LDeferredCode { |
| 4429 | public: |
| 4430 | DeferredStringCharFromCode(LCodeGen* codegen, LStringCharFromCode* instr) |
| 4431 | : LDeferredCode(codegen), instr_(instr) { } |
| 4432 | void Generate() override { |
| 4433 | codegen()->DoDeferredStringCharFromCode(instr_); |
| 4434 | } |
| 4435 | LInstruction* instr() override { return instr_; } |
| 4436 | |
| 4437 | private: |
| 4438 | LStringCharFromCode* instr_; |
| 4439 | }; |
| 4440 | |
| 4441 | DeferredStringCharFromCode* deferred = |
| 4442 | new(zone()) DeferredStringCharFromCode(this, instr); |
| 4443 | |
| 4444 | DCHECK(instr->hydrogen()->value()->representation().IsInteger32()); |
| 4445 | Register char_code = ToRegister(instr->char_code()); |
| 4446 | Register result = ToRegister(instr->result()); |
| 4447 | DCHECK(!char_code.is(result)); |
| 4448 | |
| 4449 | __ cmpl(char_code, Immediate(String::kMaxOneByteCharCode)); |
| 4450 | __ j(above, deferred->entry()); |
| 4451 | __ movsxlq(char_code, char_code); |
| 4452 | __ LoadRoot(result, Heap::kSingleCharacterStringCacheRootIndex); |
| 4453 | __ movp(result, FieldOperand(result, |
| 4454 | char_code, times_pointer_size, |
| 4455 | FixedArray::kHeaderSize)); |
| 4456 | __ CompareRoot(result, Heap::kUndefinedValueRootIndex); |
| 4457 | __ j(equal, deferred->entry()); |
| 4458 | __ bind(deferred->exit()); |
| 4459 | } |
| 4460 | |
| 4461 | |
| 4462 | void LCodeGen::DoDeferredStringCharFromCode(LStringCharFromCode* instr) { |
| 4463 | Register char_code = ToRegister(instr->char_code()); |
| 4464 | Register result = ToRegister(instr->result()); |
| 4465 | |
| 4466 | // TODO(3095996): Get rid of this. For now, we need to make the |
| 4467 | // result register contain a valid pointer because it is already |
| 4468 | // contained in the register pointer map. |
| 4469 | __ Set(result, 0); |
| 4470 | |
| 4471 | PushSafepointRegistersScope scope(this); |
| 4472 | __ Integer32ToSmi(char_code, char_code); |
| 4473 | __ Push(char_code); |
| 4474 | CallRuntimeFromDeferred(Runtime::kStringCharFromCode, 1, instr, |
| 4475 | instr->context()); |
| 4476 | __ StoreToSafepointRegisterSlot(result, rax); |
| 4477 | } |
| 4478 | |
| 4479 | |
| 4480 | void LCodeGen::DoInteger32ToDouble(LInteger32ToDouble* instr) { |
| 4481 | LOperand* input = instr->value(); |
| 4482 | DCHECK(input->IsRegister() || input->IsStackSlot()); |
| 4483 | LOperand* output = instr->result(); |
| 4484 | DCHECK(output->IsDoubleRegister()); |
| 4485 | if (input->IsRegister()) { |
| 4486 | __ Cvtlsi2sd(ToDoubleRegister(output), ToRegister(input)); |
| 4487 | } else { |
| 4488 | __ Cvtlsi2sd(ToDoubleRegister(output), ToOperand(input)); |
| 4489 | } |
| 4490 | } |
| 4491 | |
| 4492 | |
| 4493 | void LCodeGen::DoUint32ToDouble(LUint32ToDouble* instr) { |
| 4494 | LOperand* input = instr->value(); |
| 4495 | LOperand* output = instr->result(); |
| 4496 | |
| 4497 | __ LoadUint32(ToDoubleRegister(output), ToRegister(input)); |
| 4498 | } |
| 4499 | |
| 4500 | |
| 4501 | void LCodeGen::DoNumberTagI(LNumberTagI* instr) { |
| 4502 | class DeferredNumberTagI final : public LDeferredCode { |
| 4503 | public: |
| 4504 | DeferredNumberTagI(LCodeGen* codegen, LNumberTagI* instr) |
| 4505 | : LDeferredCode(codegen), instr_(instr) { } |
| 4506 | void Generate() override { |
| 4507 | codegen()->DoDeferredNumberTagIU(instr_, instr_->value(), instr_->temp1(), |
| 4508 | instr_->temp2(), SIGNED_INT32); |
| 4509 | } |
| 4510 | LInstruction* instr() override { return instr_; } |
| 4511 | |
| 4512 | private: |
| 4513 | LNumberTagI* instr_; |
| 4514 | }; |
| 4515 | |
| 4516 | LOperand* input = instr->value(); |
| 4517 | DCHECK(input->IsRegister() && input->Equals(instr->result())); |
| 4518 | Register reg = ToRegister(input); |
| 4519 | |
| 4520 | if (SmiValuesAre32Bits()) { |
| 4521 | __ Integer32ToSmi(reg, reg); |
| 4522 | } else { |
| 4523 | DCHECK(SmiValuesAre31Bits()); |
| 4524 | DeferredNumberTagI* deferred = new(zone()) DeferredNumberTagI(this, instr); |
| 4525 | __ Integer32ToSmi(reg, reg); |
| 4526 | __ j(overflow, deferred->entry()); |
| 4527 | __ bind(deferred->exit()); |
| 4528 | } |
| 4529 | } |
| 4530 | |
| 4531 | |
| 4532 | void LCodeGen::DoNumberTagU(LNumberTagU* instr) { |
| 4533 | class DeferredNumberTagU final : public LDeferredCode { |
| 4534 | public: |
| 4535 | DeferredNumberTagU(LCodeGen* codegen, LNumberTagU* instr) |
| 4536 | : LDeferredCode(codegen), instr_(instr) { } |
| 4537 | void Generate() override { |
| 4538 | codegen()->DoDeferredNumberTagIU(instr_, instr_->value(), instr_->temp1(), |
| 4539 | instr_->temp2(), UNSIGNED_INT32); |
| 4540 | } |
| 4541 | LInstruction* instr() override { return instr_; } |
| 4542 | |
| 4543 | private: |
| 4544 | LNumberTagU* instr_; |
| 4545 | }; |
| 4546 | |
| 4547 | LOperand* input = instr->value(); |
| 4548 | DCHECK(input->IsRegister() && input->Equals(instr->result())); |
| 4549 | Register reg = ToRegister(input); |
| 4550 | |
| 4551 | DeferredNumberTagU* deferred = new(zone()) DeferredNumberTagU(this, instr); |
| 4552 | __ cmpl(reg, Immediate(Smi::kMaxValue)); |
| 4553 | __ j(above, deferred->entry()); |
| 4554 | __ Integer32ToSmi(reg, reg); |
| 4555 | __ bind(deferred->exit()); |
| 4556 | } |
| 4557 | |
| 4558 | |
| 4559 | void LCodeGen::DoDeferredNumberTagIU(LInstruction* instr, |
| 4560 | LOperand* value, |
| 4561 | LOperand* temp1, |
| 4562 | LOperand* temp2, |
| 4563 | IntegerSignedness signedness) { |
| 4564 | Label done, slow; |
| 4565 | Register reg = ToRegister(value); |
| 4566 | Register tmp = ToRegister(temp1); |
| 4567 | XMMRegister temp_xmm = ToDoubleRegister(temp2); |
| 4568 | |
| 4569 | // Load value into temp_xmm which will be preserved across potential call to |
| 4570 | // runtime (MacroAssembler::EnterExitFrameEpilogue preserves only allocatable |
| 4571 | // XMM registers on x64). |
| 4572 | if (signedness == SIGNED_INT32) { |
| 4573 | DCHECK(SmiValuesAre31Bits()); |
| 4574 | // There was overflow, so bits 30 and 31 of the original integer |
| 4575 | // disagree. Try to allocate a heap number in new space and store |
| 4576 | // the value in there. If that fails, call the runtime system. |
| 4577 | __ SmiToInteger32(reg, reg); |
| 4578 | __ xorl(reg, Immediate(0x80000000)); |
| 4579 | __ Cvtlsi2sd(temp_xmm, reg); |
| 4580 | } else { |
| 4581 | DCHECK(signedness == UNSIGNED_INT32); |
| 4582 | __ LoadUint32(temp_xmm, reg); |
| 4583 | } |
| 4584 | |
| 4585 | if (FLAG_inline_new) { |
| 4586 | __ AllocateHeapNumber(reg, tmp, &slow); |
| 4587 | __ jmp(&done, kPointerSize == kInt64Size ? Label::kNear : Label::kFar); |
| 4588 | } |
| 4589 | |
| 4590 | // Slow case: Call the runtime system to do the number allocation. |
| 4591 | __ bind(&slow); |
| 4592 | { |
| 4593 | // Put a valid pointer value in the stack slot where the result |
| 4594 | // register is stored, as this register is in the pointer map, but contains |
| 4595 | // an integer value. |
| 4596 | __ Set(reg, 0); |
| 4597 | |
| 4598 | // Preserve the value of all registers. |
| 4599 | PushSafepointRegistersScope scope(this); |
| 4600 | |
| 4601 | // NumberTagIU uses the context from the frame, rather than |
| 4602 | // the environment's HContext or HInlinedContext value. |
| 4603 | // They only call Runtime::kAllocateHeapNumber. |
| 4604 | // The corresponding HChange instructions are added in a phase that does |
| 4605 | // not have easy access to the local context. |
| 4606 | __ movp(rsi, Operand(rbp, StandardFrameConstants::kContextOffset)); |
| 4607 | __ CallRuntimeSaveDoubles(Runtime::kAllocateHeapNumber); |
| 4608 | RecordSafepointWithRegisters( |
| 4609 | instr->pointer_map(), 0, Safepoint::kNoLazyDeopt); |
| 4610 | __ StoreToSafepointRegisterSlot(reg, rax); |
| 4611 | } |
| 4612 | |
| 4613 | // Done. Put the value in temp_xmm into the value of the allocated heap |
| 4614 | // number. |
| 4615 | __ bind(&done); |
| 4616 | __ Movsd(FieldOperand(reg, HeapNumber::kValueOffset), temp_xmm); |
| 4617 | } |
| 4618 | |
| 4619 | |
| 4620 | void LCodeGen::DoNumberTagD(LNumberTagD* instr) { |
| 4621 | class DeferredNumberTagD final : public LDeferredCode { |
| 4622 | public: |
| 4623 | DeferredNumberTagD(LCodeGen* codegen, LNumberTagD* instr) |
| 4624 | : LDeferredCode(codegen), instr_(instr) { } |
| 4625 | void Generate() override { codegen()->DoDeferredNumberTagD(instr_); } |
| 4626 | LInstruction* instr() override { return instr_; } |
| 4627 | |
| 4628 | private: |
| 4629 | LNumberTagD* instr_; |
| 4630 | }; |
| 4631 | |
| 4632 | XMMRegister input_reg = ToDoubleRegister(instr->value()); |
| 4633 | Register reg = ToRegister(instr->result()); |
| 4634 | Register tmp = ToRegister(instr->temp()); |
| 4635 | |
| 4636 | DeferredNumberTagD* deferred = new(zone()) DeferredNumberTagD(this, instr); |
| 4637 | if (FLAG_inline_new) { |
| 4638 | __ AllocateHeapNumber(reg, tmp, deferred->entry()); |
| 4639 | } else { |
| 4640 | __ jmp(deferred->entry()); |
| 4641 | } |
| 4642 | __ bind(deferred->exit()); |
| 4643 | __ Movsd(FieldOperand(reg, HeapNumber::kValueOffset), input_reg); |
| 4644 | } |
| 4645 | |
| 4646 | |
| 4647 | void LCodeGen::DoDeferredNumberTagD(LNumberTagD* instr) { |
| 4648 | // TODO(3095996): Get rid of this. For now, we need to make the |
| 4649 | // result register contain a valid pointer because it is already |
| 4650 | // contained in the register pointer map. |
| 4651 | Register reg = ToRegister(instr->result()); |
| 4652 | __ Move(reg, Smi::FromInt(0)); |
| 4653 | |
| 4654 | { |
| 4655 | PushSafepointRegistersScope scope(this); |
| 4656 | // NumberTagD uses the context from the frame, rather than |
| 4657 | // the environment's HContext or HInlinedContext value. |
| 4658 | // They only call Runtime::kAllocateHeapNumber. |
| 4659 | // The corresponding HChange instructions are added in a phase that does |
| 4660 | // not have easy access to the local context. |
| 4661 | __ movp(rsi, Operand(rbp, StandardFrameConstants::kContextOffset)); |
| 4662 | __ CallRuntimeSaveDoubles(Runtime::kAllocateHeapNumber); |
| 4663 | RecordSafepointWithRegisters( |
| 4664 | instr->pointer_map(), 0, Safepoint::kNoLazyDeopt); |
| 4665 | __ movp(kScratchRegister, rax); |
| 4666 | } |
| 4667 | __ movp(reg, kScratchRegister); |
| 4668 | } |
| 4669 | |
| 4670 | |
| 4671 | void LCodeGen::DoSmiTag(LSmiTag* instr) { |
| 4672 | HChange* hchange = instr->hydrogen(); |
| 4673 | Register input = ToRegister(instr->value()); |
| 4674 | Register output = ToRegister(instr->result()); |
| 4675 | if (hchange->CheckFlag(HValue::kCanOverflow) && |
| 4676 | hchange->value()->CheckFlag(HValue::kUint32)) { |
| 4677 | Condition is_smi = __ CheckUInteger32ValidSmiValue(input); |
| 4678 | DeoptimizeIf(NegateCondition(is_smi), instr, Deoptimizer::kOverflow); |
| 4679 | } |
| 4680 | __ Integer32ToSmi(output, input); |
| 4681 | if (hchange->CheckFlag(HValue::kCanOverflow) && |
| 4682 | !hchange->value()->CheckFlag(HValue::kUint32)) { |
| 4683 | DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow); |
| 4684 | } |
| 4685 | } |
| 4686 | |
| 4687 | |
| 4688 | void LCodeGen::DoSmiUntag(LSmiUntag* instr) { |
| 4689 | DCHECK(instr->value()->Equals(instr->result())); |
| 4690 | Register input = ToRegister(instr->value()); |
| 4691 | if (instr->needs_check()) { |
| 4692 | Condition is_smi = __ CheckSmi(input); |
| 4693 | DeoptimizeIf(NegateCondition(is_smi), instr, Deoptimizer::kNotASmi); |
| 4694 | } else { |
| 4695 | __ AssertSmi(input); |
| 4696 | } |
| 4697 | __ SmiToInteger32(input, input); |
| 4698 | } |
| 4699 | |
| 4700 | |
| 4701 | void LCodeGen::EmitNumberUntagD(LNumberUntagD* instr, Register input_reg, |
| 4702 | XMMRegister result_reg, NumberUntagDMode mode) { |
| 4703 | bool can_convert_undefined_to_nan = |
| 4704 | instr->hydrogen()->can_convert_undefined_to_nan(); |
| 4705 | bool deoptimize_on_minus_zero = instr->hydrogen()->deoptimize_on_minus_zero(); |
| 4706 | |
| 4707 | Label convert, load_smi, done; |
| 4708 | |
| 4709 | if (mode == NUMBER_CANDIDATE_IS_ANY_TAGGED) { |
| 4710 | // Smi check. |
| 4711 | __ JumpIfSmi(input_reg, &load_smi, Label::kNear); |
| 4712 | |
| 4713 | // Heap number map check. |
| 4714 | __ CompareRoot(FieldOperand(input_reg, HeapObject::kMapOffset), |
| 4715 | Heap::kHeapNumberMapRootIndex); |
| 4716 | |
| 4717 | // On x64 it is safe to load at heap number offset before evaluating the map |
| 4718 | // check, since all heap objects are at least two words long. |
| 4719 | __ Movsd(result_reg, FieldOperand(input_reg, HeapNumber::kValueOffset)); |
| 4720 | |
| 4721 | if (can_convert_undefined_to_nan) { |
| 4722 | __ j(not_equal, &convert, Label::kNear); |
| 4723 | } else { |
| 4724 | DeoptimizeIf(not_equal, instr, Deoptimizer::kNotAHeapNumber); |
| 4725 | } |
| 4726 | |
| 4727 | if (deoptimize_on_minus_zero) { |
| 4728 | XMMRegister xmm_scratch = double_scratch0(); |
| 4729 | __ Xorpd(xmm_scratch, xmm_scratch); |
| 4730 | __ Ucomisd(xmm_scratch, result_reg); |
| 4731 | __ j(not_equal, &done, Label::kNear); |
| 4732 | __ Movmskpd(kScratchRegister, result_reg); |
| 4733 | __ testl(kScratchRegister, Immediate(1)); |
| 4734 | DeoptimizeIf(not_zero, instr, Deoptimizer::kMinusZero); |
| 4735 | } |
| 4736 | __ jmp(&done, Label::kNear); |
| 4737 | |
| 4738 | if (can_convert_undefined_to_nan) { |
| 4739 | __ bind(&convert); |
| 4740 | |
| 4741 | // Convert undefined (and hole) to NaN. Compute NaN as 0/0. |
| 4742 | __ CompareRoot(input_reg, Heap::kUndefinedValueRootIndex); |
| 4743 | DeoptimizeIf(not_equal, instr, Deoptimizer::kNotAHeapNumberUndefined); |
| 4744 | |
| 4745 | __ Pcmpeqd(result_reg, result_reg); |
| 4746 | __ jmp(&done, Label::kNear); |
| 4747 | } |
| 4748 | } else { |
| 4749 | DCHECK(mode == NUMBER_CANDIDATE_IS_SMI); |
| 4750 | } |
| 4751 | |
| 4752 | // Smi to XMM conversion |
| 4753 | __ bind(&load_smi); |
| 4754 | __ SmiToInteger32(kScratchRegister, input_reg); |
| 4755 | __ Cvtlsi2sd(result_reg, kScratchRegister); |
| 4756 | __ bind(&done); |
| 4757 | } |
| 4758 | |
| 4759 | |
| 4760 | void LCodeGen::DoDeferredTaggedToI(LTaggedToI* instr, Label* done) { |
| 4761 | Register input_reg = ToRegister(instr->value()); |
| 4762 | |
| 4763 | if (instr->truncating()) { |
| 4764 | Label no_heap_number, check_bools, check_false; |
| 4765 | |
| 4766 | // Heap number map check. |
| 4767 | __ CompareRoot(FieldOperand(input_reg, HeapObject::kMapOffset), |
| 4768 | Heap::kHeapNumberMapRootIndex); |
| 4769 | __ j(not_equal, &no_heap_number, Label::kNear); |
| 4770 | __ TruncateHeapNumberToI(input_reg, input_reg); |
| 4771 | __ jmp(done); |
| 4772 | |
| 4773 | __ bind(&no_heap_number); |
| 4774 | // Check for Oddballs. Undefined/False is converted to zero and True to one |
| 4775 | // for truncating conversions. |
| 4776 | __ CompareRoot(input_reg, Heap::kUndefinedValueRootIndex); |
| 4777 | __ j(not_equal, &check_bools, Label::kNear); |
| 4778 | __ Set(input_reg, 0); |
| 4779 | __ jmp(done); |
| 4780 | |
| 4781 | __ bind(&check_bools); |
| 4782 | __ CompareRoot(input_reg, Heap::kTrueValueRootIndex); |
| 4783 | __ j(not_equal, &check_false, Label::kNear); |
| 4784 | __ Set(input_reg, 1); |
| 4785 | __ jmp(done); |
| 4786 | |
| 4787 | __ bind(&check_false); |
| 4788 | __ CompareRoot(input_reg, Heap::kFalseValueRootIndex); |
| 4789 | DeoptimizeIf(not_equal, instr, |
| 4790 | Deoptimizer::kNotAHeapNumberUndefinedBoolean); |
| 4791 | __ Set(input_reg, 0); |
| 4792 | } else { |
| 4793 | XMMRegister scratch = ToDoubleRegister(instr->temp()); |
| 4794 | DCHECK(!scratch.is(xmm0)); |
| 4795 | __ CompareRoot(FieldOperand(input_reg, HeapObject::kMapOffset), |
| 4796 | Heap::kHeapNumberMapRootIndex); |
| 4797 | DeoptimizeIf(not_equal, instr, Deoptimizer::kNotAHeapNumber); |
| 4798 | __ Movsd(xmm0, FieldOperand(input_reg, HeapNumber::kValueOffset)); |
| 4799 | __ Cvttsd2si(input_reg, xmm0); |
| 4800 | __ Cvtlsi2sd(scratch, input_reg); |
| 4801 | __ Ucomisd(xmm0, scratch); |
| 4802 | DeoptimizeIf(not_equal, instr, Deoptimizer::kLostPrecision); |
| 4803 | DeoptimizeIf(parity_even, instr, Deoptimizer::kNaN); |
| 4804 | if (instr->hydrogen()->GetMinusZeroMode() == FAIL_ON_MINUS_ZERO) { |
| 4805 | __ testl(input_reg, input_reg); |
| 4806 | __ j(not_zero, done); |
| 4807 | __ Movmskpd(input_reg, xmm0); |
| 4808 | __ andl(input_reg, Immediate(1)); |
| 4809 | DeoptimizeIf(not_zero, instr, Deoptimizer::kMinusZero); |
| 4810 | } |
| 4811 | } |
| 4812 | } |
| 4813 | |
| 4814 | |
| 4815 | void LCodeGen::DoTaggedToI(LTaggedToI* instr) { |
| 4816 | class DeferredTaggedToI final : public LDeferredCode { |
| 4817 | public: |
| 4818 | DeferredTaggedToI(LCodeGen* codegen, LTaggedToI* instr) |
| 4819 | : LDeferredCode(codegen), instr_(instr) { } |
| 4820 | void Generate() override { codegen()->DoDeferredTaggedToI(instr_, done()); } |
| 4821 | LInstruction* instr() override { return instr_; } |
| 4822 | |
| 4823 | private: |
| 4824 | LTaggedToI* instr_; |
| 4825 | }; |
| 4826 | |
| 4827 | LOperand* input = instr->value(); |
| 4828 | DCHECK(input->IsRegister()); |
| 4829 | DCHECK(input->Equals(instr->result())); |
| 4830 | Register input_reg = ToRegister(input); |
| 4831 | |
| 4832 | if (instr->hydrogen()->value()->representation().IsSmi()) { |
| 4833 | __ SmiToInteger32(input_reg, input_reg); |
| 4834 | } else { |
| 4835 | DeferredTaggedToI* deferred = new(zone()) DeferredTaggedToI(this, instr); |
| 4836 | __ JumpIfNotSmi(input_reg, deferred->entry()); |
| 4837 | __ SmiToInteger32(input_reg, input_reg); |
| 4838 | __ bind(deferred->exit()); |
| 4839 | } |
| 4840 | } |
| 4841 | |
| 4842 | |
| 4843 | void LCodeGen::DoNumberUntagD(LNumberUntagD* instr) { |
| 4844 | LOperand* input = instr->value(); |
| 4845 | DCHECK(input->IsRegister()); |
| 4846 | LOperand* result = instr->result(); |
| 4847 | DCHECK(result->IsDoubleRegister()); |
| 4848 | |
| 4849 | Register input_reg = ToRegister(input); |
| 4850 | XMMRegister result_reg = ToDoubleRegister(result); |
| 4851 | |
| 4852 | HValue* value = instr->hydrogen()->value(); |
| 4853 | NumberUntagDMode mode = value->representation().IsSmi() |
| 4854 | ? NUMBER_CANDIDATE_IS_SMI : NUMBER_CANDIDATE_IS_ANY_TAGGED; |
| 4855 | |
| 4856 | EmitNumberUntagD(instr, input_reg, result_reg, mode); |
| 4857 | } |
| 4858 | |
| 4859 | |
| 4860 | void LCodeGen::DoDoubleToI(LDoubleToI* instr) { |
| 4861 | LOperand* input = instr->value(); |
| 4862 | DCHECK(input->IsDoubleRegister()); |
| 4863 | LOperand* result = instr->result(); |
| 4864 | DCHECK(result->IsRegister()); |
| 4865 | |
| 4866 | XMMRegister input_reg = ToDoubleRegister(input); |
| 4867 | Register result_reg = ToRegister(result); |
| 4868 | |
| 4869 | if (instr->truncating()) { |
| 4870 | __ TruncateDoubleToI(result_reg, input_reg); |
| 4871 | } else { |
| 4872 | Label lost_precision, is_nan, minus_zero, done; |
| 4873 | XMMRegister xmm_scratch = double_scratch0(); |
| 4874 | Label::Distance dist = DeoptEveryNTimes() ? Label::kFar : Label::kNear; |
| 4875 | __ DoubleToI(result_reg, input_reg, xmm_scratch, |
| 4876 | instr->hydrogen()->GetMinusZeroMode(), &lost_precision, |
| 4877 | &is_nan, &minus_zero, dist); |
| 4878 | __ jmp(&done, dist); |
| 4879 | __ bind(&lost_precision); |
| 4880 | DeoptimizeIf(no_condition, instr, Deoptimizer::kLostPrecision); |
| 4881 | __ bind(&is_nan); |
| 4882 | DeoptimizeIf(no_condition, instr, Deoptimizer::kNaN); |
| 4883 | __ bind(&minus_zero); |
| 4884 | DeoptimizeIf(no_condition, instr, Deoptimizer::kMinusZero); |
| 4885 | __ bind(&done); |
| 4886 | } |
| 4887 | } |
| 4888 | |
| 4889 | |
| 4890 | void LCodeGen::DoDoubleToSmi(LDoubleToSmi* instr) { |
| 4891 | LOperand* input = instr->value(); |
| 4892 | DCHECK(input->IsDoubleRegister()); |
| 4893 | LOperand* result = instr->result(); |
| 4894 | DCHECK(result->IsRegister()); |
| 4895 | |
| 4896 | XMMRegister input_reg = ToDoubleRegister(input); |
| 4897 | Register result_reg = ToRegister(result); |
| 4898 | |
| 4899 | Label lost_precision, is_nan, minus_zero, done; |
| 4900 | XMMRegister xmm_scratch = double_scratch0(); |
| 4901 | Label::Distance dist = DeoptEveryNTimes() ? Label::kFar : Label::kNear; |
| 4902 | __ DoubleToI(result_reg, input_reg, xmm_scratch, |
| 4903 | instr->hydrogen()->GetMinusZeroMode(), &lost_precision, &is_nan, |
| 4904 | &minus_zero, dist); |
| 4905 | __ jmp(&done, dist); |
| 4906 | __ bind(&lost_precision); |
| 4907 | DeoptimizeIf(no_condition, instr, Deoptimizer::kLostPrecision); |
| 4908 | __ bind(&is_nan); |
| 4909 | DeoptimizeIf(no_condition, instr, Deoptimizer::kNaN); |
| 4910 | __ bind(&minus_zero); |
| 4911 | DeoptimizeIf(no_condition, instr, Deoptimizer::kMinusZero); |
| 4912 | __ bind(&done); |
| 4913 | __ Integer32ToSmi(result_reg, result_reg); |
| 4914 | DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow); |
| 4915 | } |
| 4916 | |
| 4917 | |
| 4918 | void LCodeGen::DoCheckSmi(LCheckSmi* instr) { |
| 4919 | LOperand* input = instr->value(); |
| 4920 | Condition cc = masm()->CheckSmi(ToRegister(input)); |
| 4921 | DeoptimizeIf(NegateCondition(cc), instr, Deoptimizer::kNotASmi); |
| 4922 | } |
| 4923 | |
| 4924 | |
| 4925 | void LCodeGen::DoCheckNonSmi(LCheckNonSmi* instr) { |
| 4926 | if (!instr->hydrogen()->value()->type().IsHeapObject()) { |
| 4927 | LOperand* input = instr->value(); |
| 4928 | Condition cc = masm()->CheckSmi(ToRegister(input)); |
| 4929 | DeoptimizeIf(cc, instr, Deoptimizer::kSmi); |
| 4930 | } |
| 4931 | } |
| 4932 | |
| 4933 | |
| 4934 | void LCodeGen::DoCheckArrayBufferNotNeutered( |
| 4935 | LCheckArrayBufferNotNeutered* instr) { |
| 4936 | Register view = ToRegister(instr->view()); |
| 4937 | |
| 4938 | __ movp(kScratchRegister, |
| 4939 | FieldOperand(view, JSArrayBufferView::kBufferOffset)); |
| 4940 | __ testb(FieldOperand(kScratchRegister, JSArrayBuffer::kBitFieldOffset), |
| 4941 | Immediate(1 << JSArrayBuffer::WasNeutered::kShift)); |
| 4942 | DeoptimizeIf(not_zero, instr, Deoptimizer::kOutOfBounds); |
| 4943 | } |
| 4944 | |
| 4945 | |
| 4946 | void LCodeGen::DoCheckInstanceType(LCheckInstanceType* instr) { |
| 4947 | Register input = ToRegister(instr->value()); |
| 4948 | |
| 4949 | __ movp(kScratchRegister, FieldOperand(input, HeapObject::kMapOffset)); |
| 4950 | |
| 4951 | if (instr->hydrogen()->is_interval_check()) { |
| 4952 | InstanceType first; |
| 4953 | InstanceType last; |
| 4954 | instr->hydrogen()->GetCheckInterval(&first, &last); |
| 4955 | |
| 4956 | __ cmpb(FieldOperand(kScratchRegister, Map::kInstanceTypeOffset), |
| 4957 | Immediate(static_cast<int8_t>(first))); |
| 4958 | |
| 4959 | // If there is only one type in the interval check for equality. |
| 4960 | if (first == last) { |
| 4961 | DeoptimizeIf(not_equal, instr, Deoptimizer::kWrongInstanceType); |
| 4962 | } else { |
| 4963 | DeoptimizeIf(below, instr, Deoptimizer::kWrongInstanceType); |
| 4964 | // Omit check for the last type. |
| 4965 | if (last != LAST_TYPE) { |
| 4966 | __ cmpb(FieldOperand(kScratchRegister, Map::kInstanceTypeOffset), |
| 4967 | Immediate(static_cast<int8_t>(last))); |
| 4968 | DeoptimizeIf(above, instr, Deoptimizer::kWrongInstanceType); |
| 4969 | } |
| 4970 | } |
| 4971 | } else { |
| 4972 | uint8_t mask; |
| 4973 | uint8_t tag; |
| 4974 | instr->hydrogen()->GetCheckMaskAndTag(&mask, &tag); |
| 4975 | |
| 4976 | if (base::bits::IsPowerOfTwo32(mask)) { |
| 4977 | DCHECK(tag == 0 || base::bits::IsPowerOfTwo32(tag)); |
| 4978 | __ testb(FieldOperand(kScratchRegister, Map::kInstanceTypeOffset), |
| 4979 | Immediate(mask)); |
| 4980 | DeoptimizeIf(tag == 0 ? not_zero : zero, instr, |
| 4981 | Deoptimizer::kWrongInstanceType); |
| 4982 | } else { |
| 4983 | __ movzxbl(kScratchRegister, |
| 4984 | FieldOperand(kScratchRegister, Map::kInstanceTypeOffset)); |
| 4985 | __ andb(kScratchRegister, Immediate(mask)); |
| 4986 | __ cmpb(kScratchRegister, Immediate(tag)); |
| 4987 | DeoptimizeIf(not_equal, instr, Deoptimizer::kWrongInstanceType); |
| 4988 | } |
| 4989 | } |
| 4990 | } |
| 4991 | |
| 4992 | |
| 4993 | void LCodeGen::DoCheckValue(LCheckValue* instr) { |
| 4994 | Register reg = ToRegister(instr->value()); |
| 4995 | __ Cmp(reg, instr->hydrogen()->object().handle()); |
| 4996 | DeoptimizeIf(not_equal, instr, Deoptimizer::kValueMismatch); |
| 4997 | } |
| 4998 | |
| 4999 | |
| 5000 | void LCodeGen::DoDeferredInstanceMigration(LCheckMaps* instr, Register object) { |
| 5001 | { |
| 5002 | PushSafepointRegistersScope scope(this); |
| 5003 | __ Push(object); |
| 5004 | __ Set(rsi, 0); |
| 5005 | __ CallRuntimeSaveDoubles(Runtime::kTryMigrateInstance); |
| 5006 | RecordSafepointWithRegisters( |
| 5007 | instr->pointer_map(), 1, Safepoint::kNoLazyDeopt); |
| 5008 | |
| 5009 | __ testp(rax, Immediate(kSmiTagMask)); |
| 5010 | } |
| 5011 | DeoptimizeIf(zero, instr, Deoptimizer::kInstanceMigrationFailed); |
| 5012 | } |
| 5013 | |
| 5014 | |
| 5015 | void LCodeGen::DoCheckMaps(LCheckMaps* instr) { |
| 5016 | class DeferredCheckMaps final : public LDeferredCode { |
| 5017 | public: |
| 5018 | DeferredCheckMaps(LCodeGen* codegen, LCheckMaps* instr, Register object) |
| 5019 | : LDeferredCode(codegen), instr_(instr), object_(object) { |
| 5020 | SetExit(check_maps()); |
| 5021 | } |
| 5022 | void Generate() override { |
| 5023 | codegen()->DoDeferredInstanceMigration(instr_, object_); |
| 5024 | } |
| 5025 | Label* check_maps() { return &check_maps_; } |
| 5026 | LInstruction* instr() override { return instr_; } |
| 5027 | |
| 5028 | private: |
| 5029 | LCheckMaps* instr_; |
| 5030 | Label check_maps_; |
| 5031 | Register object_; |
| 5032 | }; |
| 5033 | |
| 5034 | if (instr->hydrogen()->IsStabilityCheck()) { |
| 5035 | const UniqueSet<Map>* maps = instr->hydrogen()->maps(); |
| 5036 | for (int i = 0; i < maps->size(); ++i) { |
| 5037 | AddStabilityDependency(maps->at(i).handle()); |
| 5038 | } |
| 5039 | return; |
| 5040 | } |
| 5041 | |
| 5042 | LOperand* input = instr->value(); |
| 5043 | DCHECK(input->IsRegister()); |
| 5044 | Register reg = ToRegister(input); |
| 5045 | |
| 5046 | DeferredCheckMaps* deferred = NULL; |
| 5047 | if (instr->hydrogen()->HasMigrationTarget()) { |
| 5048 | deferred = new(zone()) DeferredCheckMaps(this, instr, reg); |
| 5049 | __ bind(deferred->check_maps()); |
| 5050 | } |
| 5051 | |
| 5052 | const UniqueSet<Map>* maps = instr->hydrogen()->maps(); |
| 5053 | Label success; |
| 5054 | for (int i = 0; i < maps->size() - 1; i++) { |
| 5055 | Handle<Map> map = maps->at(i).handle(); |
| 5056 | __ CompareMap(reg, map); |
| 5057 | __ j(equal, &success, Label::kNear); |
| 5058 | } |
| 5059 | |
| 5060 | Handle<Map> map = maps->at(maps->size() - 1).handle(); |
| 5061 | __ CompareMap(reg, map); |
| 5062 | if (instr->hydrogen()->HasMigrationTarget()) { |
| 5063 | __ j(not_equal, deferred->entry()); |
| 5064 | } else { |
| 5065 | DeoptimizeIf(not_equal, instr, Deoptimizer::kWrongMap); |
| 5066 | } |
| 5067 | |
| 5068 | __ bind(&success); |
| 5069 | } |
| 5070 | |
| 5071 | |
| 5072 | void LCodeGen::DoClampDToUint8(LClampDToUint8* instr) { |
| 5073 | XMMRegister value_reg = ToDoubleRegister(instr->unclamped()); |
| 5074 | XMMRegister xmm_scratch = double_scratch0(); |
| 5075 | Register result_reg = ToRegister(instr->result()); |
| 5076 | __ ClampDoubleToUint8(value_reg, xmm_scratch, result_reg); |
| 5077 | } |
| 5078 | |
| 5079 | |
| 5080 | void LCodeGen::DoClampIToUint8(LClampIToUint8* instr) { |
| 5081 | DCHECK(instr->unclamped()->Equals(instr->result())); |
| 5082 | Register value_reg = ToRegister(instr->result()); |
| 5083 | __ ClampUint8(value_reg); |
| 5084 | } |
| 5085 | |
| 5086 | |
| 5087 | void LCodeGen::DoClampTToUint8(LClampTToUint8* instr) { |
| 5088 | DCHECK(instr->unclamped()->Equals(instr->result())); |
| 5089 | Register input_reg = ToRegister(instr->unclamped()); |
| 5090 | XMMRegister temp_xmm_reg = ToDoubleRegister(instr->temp_xmm()); |
| 5091 | XMMRegister xmm_scratch = double_scratch0(); |
| 5092 | Label is_smi, done, heap_number; |
| 5093 | Label::Distance dist = DeoptEveryNTimes() ? Label::kFar : Label::kNear; |
| 5094 | __ JumpIfSmi(input_reg, &is_smi, dist); |
| 5095 | |
| 5096 | // Check for heap number |
| 5097 | __ Cmp(FieldOperand(input_reg, HeapObject::kMapOffset), |
| 5098 | factory()->heap_number_map()); |
| 5099 | __ j(equal, &heap_number, Label::kNear); |
| 5100 | |
| 5101 | // Check for undefined. Undefined is converted to zero for clamping |
| 5102 | // conversions. |
| 5103 | __ Cmp(input_reg, factory()->undefined_value()); |
| 5104 | DeoptimizeIf(not_equal, instr, Deoptimizer::kNotAHeapNumberUndefined); |
| 5105 | __ xorl(input_reg, input_reg); |
| 5106 | __ jmp(&done, Label::kNear); |
| 5107 | |
| 5108 | // Heap number |
| 5109 | __ bind(&heap_number); |
| 5110 | __ Movsd(xmm_scratch, FieldOperand(input_reg, HeapNumber::kValueOffset)); |
| 5111 | __ ClampDoubleToUint8(xmm_scratch, temp_xmm_reg, input_reg); |
| 5112 | __ jmp(&done, Label::kNear); |
| 5113 | |
| 5114 | // smi |
| 5115 | __ bind(&is_smi); |
| 5116 | __ SmiToInteger32(input_reg, input_reg); |
| 5117 | __ ClampUint8(input_reg); |
| 5118 | |
| 5119 | __ bind(&done); |
| 5120 | } |
| 5121 | |
| 5122 | |
| 5123 | void LCodeGen::DoDoubleBits(LDoubleBits* instr) { |
| 5124 | XMMRegister value_reg = ToDoubleRegister(instr->value()); |
| 5125 | Register result_reg = ToRegister(instr->result()); |
| 5126 | if (instr->hydrogen()->bits() == HDoubleBits::HIGH) { |
| 5127 | __ Movq(result_reg, value_reg); |
| 5128 | __ shrq(result_reg, Immediate(32)); |
| 5129 | } else { |
| 5130 | __ Movd(result_reg, value_reg); |
| 5131 | } |
| 5132 | } |
| 5133 | |
| 5134 | |
| 5135 | void LCodeGen::DoConstructDouble(LConstructDouble* instr) { |
| 5136 | Register hi_reg = ToRegister(instr->hi()); |
| 5137 | Register lo_reg = ToRegister(instr->lo()); |
| 5138 | XMMRegister result_reg = ToDoubleRegister(instr->result()); |
| 5139 | __ movl(kScratchRegister, hi_reg); |
| 5140 | __ shlq(kScratchRegister, Immediate(32)); |
| 5141 | __ orq(kScratchRegister, lo_reg); |
| 5142 | __ Movq(result_reg, kScratchRegister); |
| 5143 | } |
| 5144 | |
| 5145 | |
| 5146 | void LCodeGen::DoAllocate(LAllocate* instr) { |
| 5147 | class DeferredAllocate final : public LDeferredCode { |
| 5148 | public: |
| 5149 | DeferredAllocate(LCodeGen* codegen, LAllocate* instr) |
| 5150 | : LDeferredCode(codegen), instr_(instr) { } |
| 5151 | void Generate() override { codegen()->DoDeferredAllocate(instr_); } |
| 5152 | LInstruction* instr() override { return instr_; } |
| 5153 | |
| 5154 | private: |
| 5155 | LAllocate* instr_; |
| 5156 | }; |
| 5157 | |
| 5158 | DeferredAllocate* deferred = |
| 5159 | new(zone()) DeferredAllocate(this, instr); |
| 5160 | |
| 5161 | Register result = ToRegister(instr->result()); |
| 5162 | Register temp = ToRegister(instr->temp()); |
| 5163 | |
| 5164 | // Allocate memory for the object. |
| 5165 | AllocationFlags flags = TAG_OBJECT; |
| 5166 | if (instr->hydrogen()->MustAllocateDoubleAligned()) { |
| 5167 | flags = static_cast<AllocationFlags>(flags | DOUBLE_ALIGNMENT); |
| 5168 | } |
| 5169 | if (instr->hydrogen()->IsOldSpaceAllocation()) { |
| 5170 | DCHECK(!instr->hydrogen()->IsNewSpaceAllocation()); |
| 5171 | flags = static_cast<AllocationFlags>(flags | PRETENURE); |
| 5172 | } |
| 5173 | |
| 5174 | if (instr->size()->IsConstantOperand()) { |
| 5175 | int32_t size = ToInteger32(LConstantOperand::cast(instr->size())); |
| 5176 | CHECK(size <= Page::kMaxRegularHeapObjectSize); |
| 5177 | __ Allocate(size, result, temp, no_reg, deferred->entry(), flags); |
| 5178 | } else { |
| 5179 | Register size = ToRegister(instr->size()); |
| 5180 | __ Allocate(size, result, temp, no_reg, deferred->entry(), flags); |
| 5181 | } |
| 5182 | |
| 5183 | __ bind(deferred->exit()); |
| 5184 | |
| 5185 | if (instr->hydrogen()->MustPrefillWithFiller()) { |
| 5186 | if (instr->size()->IsConstantOperand()) { |
| 5187 | int32_t size = ToInteger32(LConstantOperand::cast(instr->size())); |
| 5188 | __ movl(temp, Immediate((size / kPointerSize) - 1)); |
| 5189 | } else { |
| 5190 | temp = ToRegister(instr->size()); |
| 5191 | __ sarp(temp, Immediate(kPointerSizeLog2)); |
| 5192 | __ decl(temp); |
| 5193 | } |
| 5194 | Label loop; |
| 5195 | __ bind(&loop); |
| 5196 | __ Move(FieldOperand(result, temp, times_pointer_size, 0), |
| 5197 | isolate()->factory()->one_pointer_filler_map()); |
| 5198 | __ decl(temp); |
| 5199 | __ j(not_zero, &loop); |
| 5200 | } |
| 5201 | } |
| 5202 | |
| 5203 | |
| 5204 | void LCodeGen::DoDeferredAllocate(LAllocate* instr) { |
| 5205 | Register result = ToRegister(instr->result()); |
| 5206 | |
| 5207 | // TODO(3095996): Get rid of this. For now, we need to make the |
| 5208 | // result register contain a valid pointer because it is already |
| 5209 | // contained in the register pointer map. |
| 5210 | __ Move(result, Smi::FromInt(0)); |
| 5211 | |
| 5212 | PushSafepointRegistersScope scope(this); |
| 5213 | if (instr->size()->IsRegister()) { |
| 5214 | Register size = ToRegister(instr->size()); |
| 5215 | DCHECK(!size.is(result)); |
| 5216 | __ Integer32ToSmi(size, size); |
| 5217 | __ Push(size); |
| 5218 | } else { |
| 5219 | int32_t size = ToInteger32(LConstantOperand::cast(instr->size())); |
| 5220 | __ Push(Smi::FromInt(size)); |
| 5221 | } |
| 5222 | |
| 5223 | int flags = 0; |
| 5224 | if (instr->hydrogen()->IsOldSpaceAllocation()) { |
| 5225 | DCHECK(!instr->hydrogen()->IsNewSpaceAllocation()); |
| 5226 | flags = AllocateTargetSpace::update(flags, OLD_SPACE); |
| 5227 | } else { |
| 5228 | flags = AllocateTargetSpace::update(flags, NEW_SPACE); |
| 5229 | } |
| 5230 | __ Push(Smi::FromInt(flags)); |
| 5231 | |
| 5232 | CallRuntimeFromDeferred( |
| 5233 | Runtime::kAllocateInTargetSpace, 2, instr, instr->context()); |
| 5234 | __ StoreToSafepointRegisterSlot(result, rax); |
| 5235 | } |
| 5236 | |
| 5237 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5238 | void LCodeGen::DoTypeof(LTypeof* instr) { |
| 5239 | DCHECK(ToRegister(instr->context()).is(rsi)); |
| 5240 | DCHECK(ToRegister(instr->value()).is(rbx)); |
| 5241 | Label end, do_call; |
| 5242 | Register value_register = ToRegister(instr->value()); |
| 5243 | __ JumpIfNotSmi(value_register, &do_call); |
| 5244 | __ Move(rax, isolate()->factory()->number_string()); |
| 5245 | __ jmp(&end); |
| 5246 | __ bind(&do_call); |
| 5247 | TypeofStub stub(isolate()); |
| 5248 | CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr); |
| 5249 | __ bind(&end); |
| 5250 | } |
| 5251 | |
| 5252 | |
| 5253 | void LCodeGen::EmitPushTaggedOperand(LOperand* operand) { |
| 5254 | DCHECK(!operand->IsDoubleRegister()); |
| 5255 | if (operand->IsConstantOperand()) { |
| 5256 | __ Push(ToHandle(LConstantOperand::cast(operand))); |
| 5257 | } else if (operand->IsRegister()) { |
| 5258 | __ Push(ToRegister(operand)); |
| 5259 | } else { |
| 5260 | __ Push(ToOperand(operand)); |
| 5261 | } |
| 5262 | } |
| 5263 | |
| 5264 | |
| 5265 | void LCodeGen::DoTypeofIsAndBranch(LTypeofIsAndBranch* instr) { |
| 5266 | Register input = ToRegister(instr->value()); |
| 5267 | Condition final_branch_condition = EmitTypeofIs(instr, input); |
| 5268 | if (final_branch_condition != no_condition) { |
| 5269 | EmitBranch(instr, final_branch_condition); |
| 5270 | } |
| 5271 | } |
| 5272 | |
| 5273 | |
| 5274 | Condition LCodeGen::EmitTypeofIs(LTypeofIsAndBranch* instr, Register input) { |
| 5275 | Label* true_label = instr->TrueLabel(chunk_); |
| 5276 | Label* false_label = instr->FalseLabel(chunk_); |
| 5277 | Handle<String> type_name = instr->type_literal(); |
| 5278 | int left_block = instr->TrueDestination(chunk_); |
| 5279 | int right_block = instr->FalseDestination(chunk_); |
| 5280 | int next_block = GetNextEmittedBlock(); |
| 5281 | |
| 5282 | Label::Distance true_distance = left_block == next_block ? Label::kNear |
| 5283 | : Label::kFar; |
| 5284 | Label::Distance false_distance = right_block == next_block ? Label::kNear |
| 5285 | : Label::kFar; |
| 5286 | Condition final_branch_condition = no_condition; |
| 5287 | Factory* factory = isolate()->factory(); |
| 5288 | if (String::Equals(type_name, factory->number_string())) { |
| 5289 | __ JumpIfSmi(input, true_label, true_distance); |
| 5290 | __ CompareRoot(FieldOperand(input, HeapObject::kMapOffset), |
| 5291 | Heap::kHeapNumberMapRootIndex); |
| 5292 | |
| 5293 | final_branch_condition = equal; |
| 5294 | |
| 5295 | } else if (String::Equals(type_name, factory->string_string())) { |
| 5296 | __ JumpIfSmi(input, false_label, false_distance); |
| 5297 | __ CmpObjectType(input, FIRST_NONSTRING_TYPE, input); |
| 5298 | final_branch_condition = below; |
| 5299 | |
| 5300 | } else if (String::Equals(type_name, factory->symbol_string())) { |
| 5301 | __ JumpIfSmi(input, false_label, false_distance); |
| 5302 | __ CmpObjectType(input, SYMBOL_TYPE, input); |
| 5303 | final_branch_condition = equal; |
| 5304 | |
| 5305 | } else if (String::Equals(type_name, factory->boolean_string())) { |
| 5306 | __ CompareRoot(input, Heap::kTrueValueRootIndex); |
| 5307 | __ j(equal, true_label, true_distance); |
| 5308 | __ CompareRoot(input, Heap::kFalseValueRootIndex); |
| 5309 | final_branch_condition = equal; |
| 5310 | |
| 5311 | } else if (String::Equals(type_name, factory->undefined_string())) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 5312 | __ CompareRoot(input, Heap::kNullValueRootIndex); |
| 5313 | __ j(equal, false_label, false_distance); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5314 | __ JumpIfSmi(input, false_label, false_distance); |
| 5315 | // Check for undetectable objects => true. |
| 5316 | __ movp(input, FieldOperand(input, HeapObject::kMapOffset)); |
| 5317 | __ testb(FieldOperand(input, Map::kBitFieldOffset), |
| 5318 | Immediate(1 << Map::kIsUndetectable)); |
| 5319 | final_branch_condition = not_zero; |
| 5320 | |
| 5321 | } else if (String::Equals(type_name, factory->function_string())) { |
| 5322 | __ JumpIfSmi(input, false_label, false_distance); |
| 5323 | // Check for callable and not undetectable objects => true. |
| 5324 | __ movp(input, FieldOperand(input, HeapObject::kMapOffset)); |
| 5325 | __ movzxbl(input, FieldOperand(input, Map::kBitFieldOffset)); |
| 5326 | __ andb(input, |
| 5327 | Immediate((1 << Map::kIsCallable) | (1 << Map::kIsUndetectable))); |
| 5328 | __ cmpb(input, Immediate(1 << Map::kIsCallable)); |
| 5329 | final_branch_condition = equal; |
| 5330 | |
| 5331 | } else if (String::Equals(type_name, factory->object_string())) { |
| 5332 | __ JumpIfSmi(input, false_label, false_distance); |
| 5333 | __ CompareRoot(input, Heap::kNullValueRootIndex); |
| 5334 | __ j(equal, true_label, true_distance); |
| 5335 | STATIC_ASSERT(LAST_JS_RECEIVER_TYPE == LAST_TYPE); |
| 5336 | __ CmpObjectType(input, FIRST_JS_RECEIVER_TYPE, input); |
| 5337 | __ j(below, false_label, false_distance); |
| 5338 | // Check for callable or undetectable objects => false. |
| 5339 | __ testb(FieldOperand(input, Map::kBitFieldOffset), |
| 5340 | Immediate((1 << Map::kIsCallable) | (1 << Map::kIsUndetectable))); |
| 5341 | final_branch_condition = zero; |
| 5342 | |
| 5343 | // clang-format off |
| 5344 | #define SIMD128_TYPE(TYPE, Type, type, lane_count, lane_type) \ |
| 5345 | } else if (String::Equals(type_name, factory->type##_string())) { \ |
| 5346 | __ JumpIfSmi(input, false_label, false_distance); \ |
| 5347 | __ CompareRoot(FieldOperand(input, HeapObject::kMapOffset), \ |
| 5348 | Heap::k##Type##MapRootIndex); \ |
| 5349 | final_branch_condition = equal; |
| 5350 | SIMD128_TYPES(SIMD128_TYPE) |
| 5351 | #undef SIMD128_TYPE |
| 5352 | // clang-format on |
| 5353 | |
| 5354 | } else { |
| 5355 | __ jmp(false_label, false_distance); |
| 5356 | } |
| 5357 | |
| 5358 | return final_branch_condition; |
| 5359 | } |
| 5360 | |
| 5361 | |
| 5362 | void LCodeGen::EnsureSpaceForLazyDeopt(int space_needed) { |
| 5363 | if (info()->ShouldEnsureSpaceForLazyDeopt()) { |
| 5364 | // Ensure that we have enough space after the previous lazy-bailout |
| 5365 | // instruction for patching the code here. |
| 5366 | int current_pc = masm()->pc_offset(); |
| 5367 | if (current_pc < last_lazy_deopt_pc_ + space_needed) { |
| 5368 | int padding_size = last_lazy_deopt_pc_ + space_needed - current_pc; |
| 5369 | __ Nop(padding_size); |
| 5370 | } |
| 5371 | } |
| 5372 | last_lazy_deopt_pc_ = masm()->pc_offset(); |
| 5373 | } |
| 5374 | |
| 5375 | |
| 5376 | void LCodeGen::DoLazyBailout(LLazyBailout* instr) { |
| 5377 | last_lazy_deopt_pc_ = masm()->pc_offset(); |
| 5378 | DCHECK(instr->HasEnvironment()); |
| 5379 | LEnvironment* env = instr->environment(); |
| 5380 | RegisterEnvironmentForDeoptimization(env, Safepoint::kLazyDeopt); |
| 5381 | safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index()); |
| 5382 | } |
| 5383 | |
| 5384 | |
| 5385 | void LCodeGen::DoDeoptimize(LDeoptimize* instr) { |
| 5386 | Deoptimizer::BailoutType type = instr->hydrogen()->type(); |
| 5387 | // TODO(danno): Stubs expect all deopts to be lazy for historical reasons (the |
| 5388 | // needed return address), even though the implementation of LAZY and EAGER is |
| 5389 | // now identical. When LAZY is eventually completely folded into EAGER, remove |
| 5390 | // the special case below. |
| 5391 | if (info()->IsStub() && type == Deoptimizer::EAGER) { |
| 5392 | type = Deoptimizer::LAZY; |
| 5393 | } |
| 5394 | DeoptimizeIf(no_condition, instr, instr->hydrogen()->reason(), type); |
| 5395 | } |
| 5396 | |
| 5397 | |
| 5398 | void LCodeGen::DoDummy(LDummy* instr) { |
| 5399 | // Nothing to see here, move on! |
| 5400 | } |
| 5401 | |
| 5402 | |
| 5403 | void LCodeGen::DoDummyUse(LDummyUse* instr) { |
| 5404 | // Nothing to see here, move on! |
| 5405 | } |
| 5406 | |
| 5407 | |
| 5408 | void LCodeGen::DoDeferredStackCheck(LStackCheck* instr) { |
| 5409 | PushSafepointRegistersScope scope(this); |
| 5410 | __ movp(rsi, Operand(rbp, StandardFrameConstants::kContextOffset)); |
| 5411 | __ CallRuntimeSaveDoubles(Runtime::kStackGuard); |
| 5412 | RecordSafepointWithLazyDeopt(instr, RECORD_SAFEPOINT_WITH_REGISTERS, 0); |
| 5413 | DCHECK(instr->HasEnvironment()); |
| 5414 | LEnvironment* env = instr->environment(); |
| 5415 | safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index()); |
| 5416 | } |
| 5417 | |
| 5418 | |
| 5419 | void LCodeGen::DoStackCheck(LStackCheck* instr) { |
| 5420 | class DeferredStackCheck final : public LDeferredCode { |
| 5421 | public: |
| 5422 | DeferredStackCheck(LCodeGen* codegen, LStackCheck* instr) |
| 5423 | : LDeferredCode(codegen), instr_(instr) { } |
| 5424 | void Generate() override { codegen()->DoDeferredStackCheck(instr_); } |
| 5425 | LInstruction* instr() override { return instr_; } |
| 5426 | |
| 5427 | private: |
| 5428 | LStackCheck* instr_; |
| 5429 | }; |
| 5430 | |
| 5431 | DCHECK(instr->HasEnvironment()); |
| 5432 | LEnvironment* env = instr->environment(); |
| 5433 | // There is no LLazyBailout instruction for stack-checks. We have to |
| 5434 | // prepare for lazy deoptimization explicitly here. |
| 5435 | if (instr->hydrogen()->is_function_entry()) { |
| 5436 | // Perform stack overflow check. |
| 5437 | Label done; |
| 5438 | __ CompareRoot(rsp, Heap::kStackLimitRootIndex); |
| 5439 | __ j(above_equal, &done, Label::kNear); |
| 5440 | |
| 5441 | DCHECK(instr->context()->IsRegister()); |
| 5442 | DCHECK(ToRegister(instr->context()).is(rsi)); |
| 5443 | CallCode(isolate()->builtins()->StackCheck(), |
| 5444 | RelocInfo::CODE_TARGET, |
| 5445 | instr); |
| 5446 | __ bind(&done); |
| 5447 | } else { |
| 5448 | DCHECK(instr->hydrogen()->is_backwards_branch()); |
| 5449 | // Perform stack overflow check if this goto needs it before jumping. |
| 5450 | DeferredStackCheck* deferred_stack_check = |
| 5451 | new(zone()) DeferredStackCheck(this, instr); |
| 5452 | __ CompareRoot(rsp, Heap::kStackLimitRootIndex); |
| 5453 | __ j(below, deferred_stack_check->entry()); |
| 5454 | EnsureSpaceForLazyDeopt(Deoptimizer::patch_size()); |
| 5455 | __ bind(instr->done_label()); |
| 5456 | deferred_stack_check->SetExit(instr->done_label()); |
| 5457 | RegisterEnvironmentForDeoptimization(env, Safepoint::kLazyDeopt); |
| 5458 | // Don't record a deoptimization index for the safepoint here. |
| 5459 | // This will be done explicitly when emitting call and the safepoint in |
| 5460 | // the deferred code. |
| 5461 | } |
| 5462 | } |
| 5463 | |
| 5464 | |
| 5465 | void LCodeGen::DoOsrEntry(LOsrEntry* instr) { |
| 5466 | // This is a pseudo-instruction that ensures that the environment here is |
| 5467 | // properly registered for deoptimization and records the assembler's PC |
| 5468 | // offset. |
| 5469 | LEnvironment* environment = instr->environment(); |
| 5470 | |
| 5471 | // If the environment were already registered, we would have no way of |
| 5472 | // backpatching it with the spill slot operands. |
| 5473 | DCHECK(!environment->HasBeenRegistered()); |
| 5474 | RegisterEnvironmentForDeoptimization(environment, Safepoint::kNoLazyDeopt); |
| 5475 | |
| 5476 | GenerateOsrPrologue(); |
| 5477 | } |
| 5478 | |
| 5479 | |
| 5480 | void LCodeGen::DoForInPrepareMap(LForInPrepareMap* instr) { |
| 5481 | DCHECK(ToRegister(instr->context()).is(rsi)); |
| 5482 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5483 | Label use_cache, call_runtime; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 5484 | __ CheckEnumCache(&call_runtime); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5485 | |
| 5486 | __ movp(rax, FieldOperand(rax, HeapObject::kMapOffset)); |
| 5487 | __ jmp(&use_cache, Label::kNear); |
| 5488 | |
| 5489 | // Get the set of properties to enumerate. |
| 5490 | __ bind(&call_runtime); |
| 5491 | __ Push(rax); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 5492 | CallRuntime(Runtime::kForInEnumerate, instr); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5493 | __ bind(&use_cache); |
| 5494 | } |
| 5495 | |
| 5496 | |
| 5497 | void LCodeGen::DoForInCacheArray(LForInCacheArray* instr) { |
| 5498 | Register map = ToRegister(instr->map()); |
| 5499 | Register result = ToRegister(instr->result()); |
| 5500 | Label load_cache, done; |
| 5501 | __ EnumLength(result, map); |
| 5502 | __ Cmp(result, Smi::FromInt(0)); |
| 5503 | __ j(not_equal, &load_cache, Label::kNear); |
| 5504 | __ LoadRoot(result, Heap::kEmptyFixedArrayRootIndex); |
| 5505 | __ jmp(&done, Label::kNear); |
| 5506 | __ bind(&load_cache); |
| 5507 | __ LoadInstanceDescriptors(map, result); |
| 5508 | __ movp(result, |
| 5509 | FieldOperand(result, DescriptorArray::kEnumCacheOffset)); |
| 5510 | __ movp(result, |
| 5511 | FieldOperand(result, FixedArray::SizeFor(instr->idx()))); |
| 5512 | __ bind(&done); |
| 5513 | Condition cc = masm()->CheckSmi(result); |
| 5514 | DeoptimizeIf(cc, instr, Deoptimizer::kNoCache); |
| 5515 | } |
| 5516 | |
| 5517 | |
| 5518 | void LCodeGen::DoCheckMapValue(LCheckMapValue* instr) { |
| 5519 | Register object = ToRegister(instr->value()); |
| 5520 | __ cmpp(ToRegister(instr->map()), |
| 5521 | FieldOperand(object, HeapObject::kMapOffset)); |
| 5522 | DeoptimizeIf(not_equal, instr, Deoptimizer::kWrongMap); |
| 5523 | } |
| 5524 | |
| 5525 | |
| 5526 | void LCodeGen::DoDeferredLoadMutableDouble(LLoadFieldByIndex* instr, |
| 5527 | Register object, |
| 5528 | Register index) { |
| 5529 | PushSafepointRegistersScope scope(this); |
| 5530 | __ Push(object); |
| 5531 | __ Push(index); |
| 5532 | __ xorp(rsi, rsi); |
| 5533 | __ CallRuntimeSaveDoubles(Runtime::kLoadMutableDouble); |
| 5534 | RecordSafepointWithRegisters( |
| 5535 | instr->pointer_map(), 2, Safepoint::kNoLazyDeopt); |
| 5536 | __ StoreToSafepointRegisterSlot(object, rax); |
| 5537 | } |
| 5538 | |
| 5539 | |
| 5540 | void LCodeGen::DoLoadFieldByIndex(LLoadFieldByIndex* instr) { |
| 5541 | class DeferredLoadMutableDouble final : public LDeferredCode { |
| 5542 | public: |
| 5543 | DeferredLoadMutableDouble(LCodeGen* codegen, |
| 5544 | LLoadFieldByIndex* instr, |
| 5545 | Register object, |
| 5546 | Register index) |
| 5547 | : LDeferredCode(codegen), |
| 5548 | instr_(instr), |
| 5549 | object_(object), |
| 5550 | index_(index) { |
| 5551 | } |
| 5552 | void Generate() override { |
| 5553 | codegen()->DoDeferredLoadMutableDouble(instr_, object_, index_); |
| 5554 | } |
| 5555 | LInstruction* instr() override { return instr_; } |
| 5556 | |
| 5557 | private: |
| 5558 | LLoadFieldByIndex* instr_; |
| 5559 | Register object_; |
| 5560 | Register index_; |
| 5561 | }; |
| 5562 | |
| 5563 | Register object = ToRegister(instr->object()); |
| 5564 | Register index = ToRegister(instr->index()); |
| 5565 | |
| 5566 | DeferredLoadMutableDouble* deferred; |
| 5567 | deferred = new(zone()) DeferredLoadMutableDouble(this, instr, object, index); |
| 5568 | |
| 5569 | Label out_of_object, done; |
| 5570 | __ Move(kScratchRegister, Smi::FromInt(1)); |
| 5571 | __ testp(index, kScratchRegister); |
| 5572 | __ j(not_zero, deferred->entry()); |
| 5573 | |
| 5574 | __ sarp(index, Immediate(1)); |
| 5575 | |
| 5576 | __ SmiToInteger32(index, index); |
| 5577 | __ cmpl(index, Immediate(0)); |
| 5578 | __ j(less, &out_of_object, Label::kNear); |
| 5579 | __ movp(object, FieldOperand(object, |
| 5580 | index, |
| 5581 | times_pointer_size, |
| 5582 | JSObject::kHeaderSize)); |
| 5583 | __ jmp(&done, Label::kNear); |
| 5584 | |
| 5585 | __ bind(&out_of_object); |
| 5586 | __ movp(object, FieldOperand(object, JSObject::kPropertiesOffset)); |
| 5587 | __ negl(index); |
| 5588 | // Index is now equal to out of object property index plus 1. |
| 5589 | __ movp(object, FieldOperand(object, |
| 5590 | index, |
| 5591 | times_pointer_size, |
| 5592 | FixedArray::kHeaderSize - kPointerSize)); |
| 5593 | __ bind(deferred->exit()); |
| 5594 | __ bind(&done); |
| 5595 | } |
| 5596 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5597 | #undef __ |
| 5598 | |
| 5599 | } // namespace internal |
| 5600 | } // namespace v8 |
| 5601 | |
| 5602 | #endif // V8_TARGET_ARCH_X64 |