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