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