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