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