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