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