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