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