blob: 10f2bb8cdd27a7dbeb80c86f549e83ccac6bc0ae [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2013 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.
Ben Murdochb8e0da22011-05-16 14:20:40 +01004
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005#include "src/v8.h"
Ben Murdochb8e0da22011-05-16 14:20:40 +01006
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007#if V8_TARGET_ARCH_X64
Ben Murdochb8e0da22011-05-16 14:20:40 +01008
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009#include "src/base/bits.h"
10#include "src/code-factory.h"
11#include "src/code-stubs.h"
12#include "src/hydrogen-osr.h"
13#include "src/ic/ic.h"
14#include "src/ic/stub-cache.h"
15#include "src/x64/lithium-codegen-x64.h"
Ben Murdochb8e0da22011-05-16 14:20:40 +010016
17namespace v8 {
18namespace internal {
19
20
Ben Murdoche0cee9b2011-05-25 10:26:03 +010021// When invoking builtins, we need to record the safepoint in the middle of
22// the invoke instruction sequence generated by the macro assembler.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000023class SafepointGenerator FINAL : public CallWrapper {
Ben Murdoche0cee9b2011-05-25 10:26:03 +010024 public:
25 SafepointGenerator(LCodeGen* codegen,
26 LPointerMap* pointers,
Ben Murdoch2b4ba112012-01-20 14:57:15 +000027 Safepoint::DeoptMode mode)
Ben Murdoche0cee9b2011-05-25 10:26:03 +010028 : codegen_(codegen),
29 pointers_(pointers),
Ben Murdoch2b4ba112012-01-20 14:57:15 +000030 deopt_mode_(mode) { }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000031 virtual ~SafepointGenerator() {}
Ben Murdoche0cee9b2011-05-25 10:26:03 +010032
Emily Bernierd0a1eb72015-03-24 16:35:39 -040033 void BeforeCall(int call_size) const OVERRIDE {}
Steve Block44f0eee2011-05-26 01:26:41 +010034
Emily Bernierd0a1eb72015-03-24 16:35:39 -040035 void AfterCall() const OVERRIDE {
Ben Murdoch2b4ba112012-01-20 14:57:15 +000036 codegen_->RecordSafepoint(pointers_, deopt_mode_);
Ben Murdoche0cee9b2011-05-25 10:26:03 +010037 }
38
39 private:
Ben Murdoche0cee9b2011-05-25 10:26:03 +010040 LCodeGen* codegen_;
41 LPointerMap* pointers_;
Ben Murdoch2b4ba112012-01-20 14:57:15 +000042 Safepoint::DeoptMode deopt_mode_;
Ben Murdoche0cee9b2011-05-25 10:26:03 +010043};
44
45
Ben Murdochb8e0da22011-05-16 14:20:40 +010046#define __ masm()->
47
48bool LCodeGen::GenerateCode() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000049 LPhase phase("Z_Code generation", chunk());
50 DCHECK(is_unused());
Ben Murdochb8e0da22011-05-16 14:20:40 +010051 status_ = GENERATING;
Ben Murdoch3ef787d2012-04-12 10:51:47 +010052
53 // Open a frame scope to indicate that there is a frame on the stack. The
54 // MANUAL indicates that the scope shouldn't actually generate code to set up
55 // the frame (that is done in GeneratePrologue).
56 FrameScope frame_scope(masm_, StackFrame::MANUAL);
57
Ben Murdochb8e0da22011-05-16 14:20:40 +010058 return GeneratePrologue() &&
59 GenerateBody() &&
60 GenerateDeferredCode() &&
Ben Murdoche0cee9b2011-05-25 10:26:03 +010061 GenerateJumpTable() &&
Ben Murdochb8e0da22011-05-16 14:20:40 +010062 GenerateSafepointTable();
63}
64
65
66void LCodeGen::FinishCode(Handle<Code> code) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000067 DCHECK(is_done());
Ben Murdoch257744e2011-11-30 15:57:28 +000068 code->set_stack_slots(GetStackSlotCount());
Steve Block1e0659c2011-05-24 12:43:12 +010069 code->set_safepoint_table_offset(safepoints_.GetCodeOffset());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000070 if (code->is_optimized_code()) RegisterWeakObjectsInOptimizedCode(code);
Ben Murdochb8e0da22011-05-16 14:20:40 +010071 PopulateDeoptimizationData(code);
72}
73
74
Ben Murdochb8a8cc12014-11-26 15:28:44 +000075#ifdef _MSC_VER
76void LCodeGen::MakeSureStackPagesMapped(int offset) {
77 const int kPageSize = 4 * KB;
78 for (offset -= kPageSize; offset > 0; offset -= kPageSize) {
79 __ movp(Operand(rsp, offset), rax);
Ben Murdochb8e0da22011-05-16 14:20:40 +010080 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000081}
82#endif
83
84
85void LCodeGen::SaveCallerDoubles() {
86 DCHECK(info()->saves_caller_doubles());
87 DCHECK(NeedsEagerFrame());
88 Comment(";;; Save clobbered callee double registers");
89 int count = 0;
90 BitVector* doubles = chunk()->allocated_double_registers();
91 BitVector::Iterator save_iterator(doubles);
92 while (!save_iterator.Done()) {
93 __ movsd(MemOperand(rsp, count * kDoubleSize),
94 XMMRegister::FromAllocationIndex(save_iterator.Current()));
95 save_iterator.Advance();
96 count++;
97 }
Ben Murdochb8e0da22011-05-16 14:20:40 +010098}
99
100
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000101void LCodeGen::RestoreCallerDoubles() {
102 DCHECK(info()->saves_caller_doubles());
103 DCHECK(NeedsEagerFrame());
104 Comment(";;; Restore clobbered callee double registers");
105 BitVector* doubles = chunk()->allocated_double_registers();
106 BitVector::Iterator save_iterator(doubles);
107 int count = 0;
108 while (!save_iterator.Done()) {
109 __ movsd(XMMRegister::FromAllocationIndex(save_iterator.Current()),
110 MemOperand(rsp, count * kDoubleSize));
111 save_iterator.Advance();
112 count++;
113 }
Ben Murdochb8e0da22011-05-16 14:20:40 +0100114}
115
116
117bool LCodeGen::GeneratePrologue() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000118 DCHECK(is_generating());
119
120 if (info()->IsOptimizing()) {
121 ProfileEntryHookStub::MaybeCallEntryHook(masm_);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100122
123#ifdef DEBUG
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000124 if (strlen(FLAG_stop_at) > 0 &&
125 info_->function()->name()->IsUtf8EqualTo(CStrVector(FLAG_stop_at))) {
126 __ int3();
127 }
Ben Murdochb8e0da22011-05-16 14:20:40 +0100128#endif
129
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000130 // Sloppy mode functions need to replace the receiver with the global proxy
131 // when called as functions (without an explicit receiver object).
132 if (info_->this_has_uses() &&
133 info_->strict_mode() == SLOPPY &&
134 !info_->is_native()) {
135 Label ok;
136 StackArgumentsAccessor args(rsp, scope()->num_parameters());
137 __ movp(rcx, args.GetReceiverOperand());
138
139 __ CompareRoot(rcx, Heap::kUndefinedValueRootIndex);
140 __ j(not_equal, &ok, Label::kNear);
141
142 __ movp(rcx, GlobalObjectOperand());
143 __ movp(rcx, FieldOperand(rcx, GlobalObject::kGlobalProxyOffset));
144
145 __ movp(args.GetReceiverOperand(), rcx);
146
147 __ bind(&ok);
148 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000149 }
150
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000151 info()->set_prologue_offset(masm_->pc_offset());
152 if (NeedsEagerFrame()) {
153 DCHECK(!frame_is_built_);
154 frame_is_built_ = true;
155 if (info()->IsStub()) {
156 __ StubPrologue();
157 } else {
158 __ Prologue(info()->IsCodePreAgingActive());
159 }
160 info()->AddNoFrameRange(0, masm_->pc_offset());
161 }
Ben Murdochb8e0da22011-05-16 14:20:40 +0100162
163 // Reserve space for the stack slots needed by the code.
Ben Murdoch257744e2011-11-30 15:57:28 +0000164 int slots = GetStackSlotCount();
Ben Murdochb8e0da22011-05-16 14:20:40 +0100165 if (slots > 0) {
166 if (FLAG_debug_code) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000167 __ subp(rsp, Immediate(slots * kPointerSize));
168#ifdef _MSC_VER
169 MakeSureStackPagesMapped(slots * kPointerSize);
170#endif
171 __ Push(rax);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100172 __ Set(rax, slots);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000173 __ Set(kScratchRegister, kSlotsZapValue);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100174 Label loop;
175 __ bind(&loop);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000176 __ movp(MemOperand(rsp, rax, times_pointer_size, 0),
177 kScratchRegister);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100178 __ decl(rax);
179 __ j(not_zero, &loop);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000180 __ Pop(rax);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100181 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000182 __ subp(rsp, Immediate(slots * kPointerSize));
Ben Murdochb8e0da22011-05-16 14:20:40 +0100183#ifdef _MSC_VER
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000184 MakeSureStackPagesMapped(slots * kPointerSize);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100185#endif
186 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000187
188 if (info()->saves_caller_doubles()) {
189 SaveCallerDoubles();
190 }
Ben Murdochb8e0da22011-05-16 14:20:40 +0100191 }
192
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100193 // Possibly allocate a local context.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000194 int heap_slots = info_->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100195 if (heap_slots > 0) {
196 Comment(";;; Allocate local context");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000197 bool need_write_barrier = true;
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100198 // Argument to NewContext is the function, which is still in rdi.
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100199 if (heap_slots <= FastNewContextStub::kMaximumSlots) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000200 FastNewContextStub stub(isolate(), heap_slots);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100201 __ CallStub(&stub);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000202 // Result of FastNewContextStub is always in new space.
203 need_write_barrier = false;
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100204 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000205 __ Push(rdi);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000206 __ CallRuntime(Runtime::kNewFunctionContext, 1);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100207 }
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000208 RecordSafepoint(Safepoint::kNoLazyDeopt);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000209 // Context is returned in rax. It replaces the context passed to us.
210 // It's saved in the stack and kept live in rsi.
211 __ movp(rsi, rax);
212 __ movp(Operand(rbp, StandardFrameConstants::kContextOffset), rax);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100213
214 // Copy any necessary parameters into the context.
215 int num_parameters = scope()->num_parameters();
216 for (int i = 0; i < num_parameters; i++) {
Ben Murdoch589d6972011-11-30 16:04:58 +0000217 Variable* var = scope()->parameter(i);
218 if (var->IsContextSlot()) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100219 int parameter_offset = StandardFrameConstants::kCallerSPOffset +
220 (num_parameters - 1 - i) * kPointerSize;
221 // Load parameter from stack.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000222 __ movp(rax, Operand(rbp, parameter_offset));
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100223 // Store it in the context.
Ben Murdoch589d6972011-11-30 16:04:58 +0000224 int context_offset = Context::SlotOffset(var->index());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000225 __ movp(Operand(rsi, context_offset), rax);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100226 // Update the write barrier. This clobbers rax and rbx.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000227 if (need_write_barrier) {
228 __ RecordWriteContextSlot(rsi, context_offset, rax, rbx, kSaveFPRegs);
229 } else if (FLAG_debug_code) {
230 Label done;
231 __ JumpIfInNewSpace(rsi, rax, &done, Label::kNear);
232 __ Abort(kExpectedNewSpaceObject);
233 __ bind(&done);
234 }
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100235 }
236 }
237 Comment(";;; End allocate local context");
238 }
239
Ben Murdochb8e0da22011-05-16 14:20:40 +0100240 // Trace the call.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000241 if (FLAG_trace && info()->IsOptimizing()) {
Ben Murdochb8e0da22011-05-16 14:20:40 +0100242 __ CallRuntime(Runtime::kTraceEnter, 0);
243 }
244 return !is_aborted();
245}
246
247
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000248void LCodeGen::GenerateOsrPrologue() {
249 // Generate the OSR entry prologue at the first unknown OSR value, or if there
250 // are none, at the OSR entrypoint instruction.
251 if (osr_pc_offset_ >= 0) return;
Ben Murdochb8e0da22011-05-16 14:20:40 +0100252
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000253 osr_pc_offset_ = masm()->pc_offset();
254
255 // Adjust the frame size, subsuming the unoptimized frame into the
256 // optimized frame.
257 int slots = GetStackSlotCount() - graph()->osr()->UnoptimizedFrameSlots();
258 DCHECK(slots >= 0);
259 __ subp(rsp, Immediate(slots * kPointerSize));
260}
261
262
263void LCodeGen::GenerateBodyInstructionPre(LInstruction* instr) {
264 if (instr->IsCall()) {
265 EnsureSpaceForLazyDeopt(Deoptimizer::patch_size());
266 }
267 if (!instr->IsLazyBailout() && !instr->IsGap()) {
268 safepoints_.BumpLastLazySafepointIndex();
269 }
270}
271
272
273void LCodeGen::GenerateBodyInstructionPost(LInstruction* instr) {
274 if (FLAG_debug_code && FLAG_enable_slow_asserts && instr->HasResult() &&
275 instr->hydrogen_value()->representation().IsInteger32() &&
276 instr->result()->IsRegister()) {
277 __ AssertZeroExtended(ToRegister(instr->result()));
278 }
279
280 if (instr->HasResult() && instr->MustSignExtendResult(chunk())) {
281 // We sign extend the dehoisted key at the definition point when the pointer
282 // size is 64-bit. For x32 port, we sign extend the dehoisted key at the use
283 // points and MustSignExtendResult is always false. We can't use
284 // STATIC_ASSERT here as the pointer size is 32-bit for x32.
285 DCHECK(kPointerSize == kInt64Size);
286 if (instr->result()->IsRegister()) {
287 Register result_reg = ToRegister(instr->result());
288 __ movsxlq(result_reg, result_reg);
289 } else {
290 // Sign extend the 32bit result in the stack slots.
291 DCHECK(instr->result()->IsStackSlot());
292 Operand src = ToOperand(instr->result());
293 __ movsxlq(kScratchRegister, src);
294 __ movq(src, kScratchRegister);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100295 }
296 }
Ben Murdochb8e0da22011-05-16 14:20:40 +0100297}
298
299
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100300bool LCodeGen::GenerateJumpTable() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000301 Label needs_frame;
302 if (jump_table_.length() > 0) {
303 Comment(";;; -------------------- Jump table --------------------");
304 }
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100305 for (int i = 0; i < jump_table_.length(); i++) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000306 Deoptimizer::JumpTableEntry* table_entry = &jump_table_[i];
307 __ bind(&table_entry->label);
308 Address entry = table_entry->address;
309 DeoptComment(table_entry->reason);
310 if (table_entry->needs_frame) {
311 DCHECK(!info()->saves_caller_doubles());
312 __ Move(kScratchRegister, ExternalReference::ForDeoptEntry(entry));
313 if (needs_frame.is_bound()) {
314 __ jmp(&needs_frame);
315 } else {
316 __ bind(&needs_frame);
317 __ movp(rsi, MemOperand(rbp, StandardFrameConstants::kContextOffset));
318 __ pushq(rbp);
319 __ movp(rbp, rsp);
320 __ Push(rsi);
321 // This variant of deopt can only be used with stubs. Since we don't
322 // have a function pointer to install in the stack frame that we're
323 // building, install a special marker there instead.
324 DCHECK(info()->IsStub());
325 __ Move(rsi, Smi::FromInt(StackFrame::STUB));
326 __ Push(rsi);
327 __ movp(rsi, MemOperand(rsp, kPointerSize));
328 __ call(kScratchRegister);
329 }
330 } else {
331 if (info()->saves_caller_doubles()) {
332 DCHECK(info()->IsStub());
333 RestoreCallerDoubles();
334 }
335 __ call(entry, RelocInfo::RUNTIME_ENTRY);
336 }
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100337 }
338 return !is_aborted();
339}
340
341
Ben Murdochb8e0da22011-05-16 14:20:40 +0100342bool LCodeGen::GenerateDeferredCode() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000343 DCHECK(is_generating());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000344 if (deferred_.length() > 0) {
345 for (int i = 0; !is_aborted() && i < deferred_.length(); i++) {
346 LDeferredCode* code = deferred_[i];
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000347
348 HValue* value =
349 instructions_->at(code->instruction_index())->hydrogen_value();
350 RecordAndWritePosition(
351 chunk()->graph()->SourcePositionToScriptPosition(value->position()));
352
353 Comment(";;; <@%d,#%d> "
354 "-------------------- Deferred %s --------------------",
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100355 code->instruction_index(),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000356 code->instr()->hydrogen_value()->id(),
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100357 code->instr()->Mnemonic());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000358 __ bind(code->entry());
359 if (NeedsDeferredFrame()) {
360 Comment(";;; Build frame");
361 DCHECK(!frame_is_built_);
362 DCHECK(info()->IsStub());
363 frame_is_built_ = true;
364 // Build the frame in such a way that esi isn't trashed.
365 __ pushq(rbp); // Caller's frame pointer.
366 __ Push(Operand(rbp, StandardFrameConstants::kContextOffset));
367 __ Push(Smi::FromInt(StackFrame::STUB));
368 __ leap(rbp, Operand(rsp, 2 * kPointerSize));
369 Comment(";;; Deferred code");
370 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000371 code->Generate();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000372 if (NeedsDeferredFrame()) {
373 __ bind(code->done());
374 Comment(";;; Destroy frame");
375 DCHECK(frame_is_built_);
376 frame_is_built_ = false;
377 __ movp(rsp, rbp);
378 __ popq(rbp);
379 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000380 __ jmp(code->exit());
381 }
Ben Murdochb8e0da22011-05-16 14:20:40 +0100382 }
383
384 // Deferred code is the last part of the instruction sequence. Mark
385 // the generated code as done unless we bailed out.
386 if (!is_aborted()) status_ = DONE;
387 return !is_aborted();
388}
389
390
391bool LCodeGen::GenerateSafepointTable() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000392 DCHECK(is_done());
Ben Murdoch257744e2011-11-30 15:57:28 +0000393 safepoints_.Emit(masm(), GetStackSlotCount());
Ben Murdochb8e0da22011-05-16 14:20:40 +0100394 return !is_aborted();
395}
396
397
398Register LCodeGen::ToRegister(int index) const {
399 return Register::FromAllocationIndex(index);
400}
401
402
403XMMRegister LCodeGen::ToDoubleRegister(int index) const {
404 return XMMRegister::FromAllocationIndex(index);
405}
406
407
408Register LCodeGen::ToRegister(LOperand* op) const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000409 DCHECK(op->IsRegister());
Ben Murdochb8e0da22011-05-16 14:20:40 +0100410 return ToRegister(op->index());
411}
412
413
414XMMRegister LCodeGen::ToDoubleRegister(LOperand* op) const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000415 DCHECK(op->IsDoubleRegister());
Ben Murdochb8e0da22011-05-16 14:20:40 +0100416 return ToDoubleRegister(op->index());
417}
418
419
420bool LCodeGen::IsInteger32Constant(LConstantOperand* op) const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000421 return chunk_->LookupLiteralRepresentation(op).IsSmiOrInteger32();
Ben Murdochb8e0da22011-05-16 14:20:40 +0100422}
423
424
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000425bool LCodeGen::IsDehoistedKeyConstant(LConstantOperand* op) const {
Ben Murdochb8e0da22011-05-16 14:20:40 +0100426 return op->IsConstantOperand() &&
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000427 chunk_->IsDehoistedKey(chunk_->LookupConstant(op));
Ben Murdochb8e0da22011-05-16 14:20:40 +0100428}
429
430
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000431bool LCodeGen::IsSmiConstant(LConstantOperand* op) const {
432 return chunk_->LookupLiteralRepresentation(op).IsSmi();
433}
434
435
436int32_t LCodeGen::ToInteger32(LConstantOperand* op) const {
437 return ToRepresentation(op, Representation::Integer32());
438}
439
440
441int32_t LCodeGen::ToRepresentation(LConstantOperand* op,
442 const Representation& r) const {
443 HConstant* constant = chunk_->LookupConstant(op);
444 int32_t value = constant->Integer32Value();
445 if (r.IsInteger32()) return value;
446 DCHECK(SmiValuesAre31Bits() && r.IsSmiOrTagged());
447 return static_cast<int32_t>(reinterpret_cast<intptr_t>(Smi::FromInt(value)));
448}
449
450
451Smi* LCodeGen::ToSmi(LConstantOperand* op) const {
452 HConstant* constant = chunk_->LookupConstant(op);
453 return Smi::FromInt(constant->Integer32Value());
Ben Murdochb8e0da22011-05-16 14:20:40 +0100454}
455
456
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100457double LCodeGen::ToDouble(LConstantOperand* op) const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000458 HConstant* constant = chunk_->LookupConstant(op);
459 DCHECK(constant->HasDoubleValue());
460 return constant->DoubleValue();
461}
462
463
464ExternalReference LCodeGen::ToExternalReference(LConstantOperand* op) const {
465 HConstant* constant = chunk_->LookupConstant(op);
466 DCHECK(constant->HasExternalReferenceValue());
467 return constant->ExternalReferenceValue();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100468}
469
470
Ben Murdochb8e0da22011-05-16 14:20:40 +0100471Handle<Object> LCodeGen::ToHandle(LConstantOperand* op) const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000472 HConstant* constant = chunk_->LookupConstant(op);
473 DCHECK(chunk_->LookupLiteralRepresentation(op).IsSmiOrTagged());
474 return constant->handle(isolate());
475}
476
477
478static int ArgumentsOffsetWithoutFrame(int index) {
479 DCHECK(index < 0);
480 return -(index + 1) * kPointerSize + kPCOnStackSize;
Ben Murdochb8e0da22011-05-16 14:20:40 +0100481}
482
483
484Operand LCodeGen::ToOperand(LOperand* op) const {
485 // Does not handle registers. In X64 assembler, plain registers are not
486 // representable as an Operand.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000487 DCHECK(op->IsStackSlot() || op->IsDoubleStackSlot());
488 if (NeedsEagerFrame()) {
489 return Operand(rbp, StackSlotOffset(op->index()));
Ben Murdochb8e0da22011-05-16 14:20:40 +0100490 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000491 // Retrieve parameter without eager stack-frame relative to the
492 // stack-pointer.
493 return Operand(rsp, ArgumentsOffsetWithoutFrame(op->index()));
Ben Murdochb8e0da22011-05-16 14:20:40 +0100494 }
495}
496
497
498void LCodeGen::WriteTranslation(LEnvironment* environment,
499 Translation* translation) {
500 if (environment == NULL) return;
501
502 // The translation includes one command per value in the environment.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000503 int translation_size = environment->translation_size();
Ben Murdochb8e0da22011-05-16 14:20:40 +0100504 // The output frame height does not include the parameters.
505 int height = translation_size - environment->parameter_count();
506
507 WriteTranslation(environment->outer(), translation);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000508 bool has_closure_id = !info()->closure().is_null() &&
509 !info()->closure().is_identical_to(environment->closure());
510 int closure_id = has_closure_id
511 ? DefineDeoptimizationLiteral(environment->closure())
512 : Translation::kSelfLiteralId;
513
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100514 switch (environment->frame_type()) {
515 case JS_FUNCTION:
516 translation->BeginJSFrame(environment->ast_id(), closure_id, height);
517 break;
518 case JS_CONSTRUCT:
519 translation->BeginConstructStubFrame(closure_id, translation_size);
520 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000521 case JS_GETTER:
522 DCHECK(translation_size == 1);
523 DCHECK(height == 0);
524 translation->BeginGetterStubFrame(closure_id);
525 break;
526 case JS_SETTER:
527 DCHECK(translation_size == 2);
528 DCHECK(height == 0);
529 translation->BeginSetterStubFrame(closure_id);
530 break;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100531 case ARGUMENTS_ADAPTOR:
532 translation->BeginArgumentsAdaptorFrame(closure_id, translation_size);
533 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000534 case STUB:
535 translation->BeginCompiledStubFrame();
536 break;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100537 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000538
539 int object_index = 0;
540 int dematerialized_index = 0;
Ben Murdochb8e0da22011-05-16 14:20:40 +0100541 for (int i = 0; i < translation_size; ++i) {
542 LOperand* value = environment->values()->at(i);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000543 AddToTranslation(environment,
544 translation,
545 value,
546 environment->HasTaggedValueAt(i),
547 environment->HasUint32ValueAt(i),
548 &object_index,
549 &dematerialized_index);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100550 }
551}
552
553
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000554void LCodeGen::AddToTranslation(LEnvironment* environment,
555 Translation* translation,
Ben Murdochb8e0da22011-05-16 14:20:40 +0100556 LOperand* op,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000557 bool is_tagged,
558 bool is_uint32,
559 int* object_index_pointer,
560 int* dematerialized_index_pointer) {
561 if (op == LEnvironment::materialization_marker()) {
562 int object_index = (*object_index_pointer)++;
563 if (environment->ObjectIsDuplicateAt(object_index)) {
564 int dupe_of = environment->ObjectDuplicateOfAt(object_index);
565 translation->DuplicateObject(dupe_of);
566 return;
567 }
568 int object_length = environment->ObjectLengthAt(object_index);
569 if (environment->ObjectIsArgumentsAt(object_index)) {
570 translation->BeginArgumentsObject(object_length);
571 } else {
572 translation->BeginCapturedObject(object_length);
573 }
574 int dematerialized_index = *dematerialized_index_pointer;
575 int env_offset = environment->translation_size() + dematerialized_index;
576 *dematerialized_index_pointer += object_length;
577 for (int i = 0; i < object_length; ++i) {
578 LOperand* value = environment->values()->at(env_offset + i);
579 AddToTranslation(environment,
580 translation,
581 value,
582 environment->HasTaggedValueAt(env_offset + i),
583 environment->HasUint32ValueAt(env_offset + i),
584 object_index_pointer,
585 dematerialized_index_pointer);
586 }
587 return;
588 }
589
590 if (op->IsStackSlot()) {
Ben Murdochb8e0da22011-05-16 14:20:40 +0100591 if (is_tagged) {
592 translation->StoreStackSlot(op->index());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000593 } else if (is_uint32) {
594 translation->StoreUint32StackSlot(op->index());
Ben Murdochb8e0da22011-05-16 14:20:40 +0100595 } else {
596 translation->StoreInt32StackSlot(op->index());
597 }
598 } else if (op->IsDoubleStackSlot()) {
599 translation->StoreDoubleStackSlot(op->index());
Ben Murdochb8e0da22011-05-16 14:20:40 +0100600 } else if (op->IsRegister()) {
601 Register reg = ToRegister(op);
602 if (is_tagged) {
603 translation->StoreRegister(reg);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000604 } else if (is_uint32) {
605 translation->StoreUint32Register(reg);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100606 } else {
607 translation->StoreInt32Register(reg);
608 }
609 } else if (op->IsDoubleRegister()) {
610 XMMRegister reg = ToDoubleRegister(op);
611 translation->StoreDoubleRegister(reg);
612 } else if (op->IsConstantOperand()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000613 HConstant* constant = chunk()->LookupConstant(LConstantOperand::cast(op));
614 int src_index = DefineDeoptimizationLiteral(constant->handle(isolate()));
Ben Murdochb8e0da22011-05-16 14:20:40 +0100615 translation->StoreLiteral(src_index);
616 } else {
617 UNREACHABLE();
618 }
619}
620
621
Ben Murdoch8b112d22011-06-08 16:22:53 +0100622void LCodeGen::CallCodeGeneric(Handle<Code> code,
623 RelocInfo::Mode mode,
624 LInstruction* instr,
625 SafepointMode safepoint_mode,
626 int argc) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000627 DCHECK(instr != NULL);
Steve Block1e0659c2011-05-24 12:43:12 +0100628 __ call(code, mode);
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000629 RecordSafepointWithLazyDeopt(instr, safepoint_mode, argc);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100630
631 // Signal that we don't inline smi code before these stubs in the
632 // optimizing code generator.
Ben Murdoch257744e2011-11-30 15:57:28 +0000633 if (code->kind() == Code::BINARY_OP_IC ||
Ben Murdochb8e0da22011-05-16 14:20:40 +0100634 code->kind() == Code::COMPARE_IC) {
635 __ nop();
636 }
637}
638
639
Ben Murdoch8b112d22011-06-08 16:22:53 +0100640void LCodeGen::CallCode(Handle<Code> code,
641 RelocInfo::Mode mode,
642 LInstruction* instr) {
643 CallCodeGeneric(code, mode, instr, RECORD_SIMPLE_SAFEPOINT, 0);
644}
645
646
Steve Block44f0eee2011-05-26 01:26:41 +0100647void LCodeGen::CallRuntime(const Runtime::Function* function,
Ben Murdochb8e0da22011-05-16 14:20:40 +0100648 int num_arguments,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000649 LInstruction* instr,
650 SaveFPRegsMode save_doubles) {
651 DCHECK(instr != NULL);
652 DCHECK(instr->HasPointerMap());
Steve Block1e0659c2011-05-24 12:43:12 +0100653
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000654 __ CallRuntime(function, num_arguments, save_doubles);
655
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000656 RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT, 0);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100657}
658
659
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000660void LCodeGen::LoadContextFromDeferred(LOperand* context) {
661 if (context->IsRegister()) {
662 if (!ToRegister(context).is(rsi)) {
663 __ movp(rsi, ToRegister(context));
664 }
665 } else if (context->IsStackSlot()) {
666 __ movp(rsi, ToOperand(context));
667 } else if (context->IsConstantOperand()) {
668 HConstant* constant =
669 chunk_->LookupConstant(LConstantOperand::cast(context));
670 __ Move(rsi, Handle<Object>::cast(constant->handle(isolate())));
671 } else {
672 UNREACHABLE();
673 }
674}
675
676
677
Ben Murdoch8b112d22011-06-08 16:22:53 +0100678void LCodeGen::CallRuntimeFromDeferred(Runtime::FunctionId id,
679 int argc,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000680 LInstruction* instr,
681 LOperand* context) {
682 LoadContextFromDeferred(context);
683
Ben Murdoch8b112d22011-06-08 16:22:53 +0100684 __ CallRuntimeSaveDoubles(id);
685 RecordSafepointWithRegisters(
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000686 instr->pointer_map(), argc, Safepoint::kNoLazyDeopt);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100687}
688
689
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000690void LCodeGen::RegisterEnvironmentForDeoptimization(LEnvironment* environment,
691 Safepoint::DeoptMode mode) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000692 environment->set_has_been_used();
Ben Murdochb8e0da22011-05-16 14:20:40 +0100693 if (!environment->HasBeenRegistered()) {
694 // Physical stack frame layout:
695 // -x ............. -4 0 ..................................... y
696 // [incoming arguments] [spill slots] [pushed outgoing arguments]
697
698 // Layout of the environment:
699 // 0 ..................................................... size-1
700 // [parameters] [locals] [expression stack including arguments]
701
702 // Layout of the translation:
703 // 0 ........................................................ size - 1 + 4
704 // [expression stack including arguments] [locals] [4 words] [parameters]
705 // |>------------ translation_size ------------<|
706
707 int frame_count = 0;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100708 int jsframe_count = 0;
Ben Murdochb8e0da22011-05-16 14:20:40 +0100709 for (LEnvironment* e = environment; e != NULL; e = e->outer()) {
710 ++frame_count;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100711 if (e->frame_type() == JS_FUNCTION) {
712 ++jsframe_count;
713 }
Ben Murdochb8e0da22011-05-16 14:20:40 +0100714 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000715 Translation translation(&translations_, frame_count, jsframe_count, zone());
Ben Murdochb8e0da22011-05-16 14:20:40 +0100716 WriteTranslation(environment, &translation);
717 int deoptimization_index = deoptimizations_.length();
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000718 int pc_offset = masm()->pc_offset();
719 environment->Register(deoptimization_index,
720 translation.index(),
721 (mode == Safepoint::kLazyDeopt) ? pc_offset : -1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000722 deoptimizations_.Add(environment, environment->zone());
Ben Murdochb8e0da22011-05-16 14:20:40 +0100723 }
724}
725
726
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000727void LCodeGen::DeoptimizeIf(Condition cc, LInstruction* instr,
728 const char* detail,
729 Deoptimizer::BailoutType bailout_type) {
730 LEnvironment* environment = instr->environment();
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000731 RegisterEnvironmentForDeoptimization(environment, Safepoint::kNoLazyDeopt);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000732 DCHECK(environment->HasBeenRegistered());
Steve Block1e0659c2011-05-24 12:43:12 +0100733 int id = environment->deoptimization_index();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000734 DCHECK(info()->IsOptimizing() || info()->IsStub());
735 Address entry =
736 Deoptimizer::GetDeoptimizationEntry(isolate(), id, bailout_type);
Steve Block1e0659c2011-05-24 12:43:12 +0100737 if (entry == NULL) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000738 Abort(kBailoutWasNotPrepared);
Steve Block1e0659c2011-05-24 12:43:12 +0100739 return;
740 }
741
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000742 if (DeoptEveryNTimes()) {
743 ExternalReference count = ExternalReference::stress_deopt_count(isolate());
744 Label no_deopt;
745 __ pushfq();
746 __ pushq(rax);
747 Operand count_operand = masm()->ExternalOperand(count, kScratchRegister);
748 __ movl(rax, count_operand);
749 __ subl(rax, Immediate(1));
750 __ j(not_zero, &no_deopt, Label::kNear);
751 if (FLAG_trap_on_deopt) __ int3();
752 __ movl(rax, Immediate(FLAG_deopt_every_n_times));
753 __ movl(count_operand, rax);
754 __ popq(rax);
755 __ popfq();
756 DCHECK(frame_is_built_);
757 __ call(entry, RelocInfo::RUNTIME_ENTRY);
758 __ bind(&no_deopt);
759 __ movl(count_operand, rax);
760 __ popq(rax);
761 __ popfq();
762 }
763
764 if (info()->ShouldTrapOnDeopt()) {
765 Label done;
766 if (cc != no_condition) {
767 __ j(NegateCondition(cc), &done, Label::kNear);
768 }
769 __ int3();
770 __ bind(&done);
771 }
772
773 Deoptimizer::Reason reason(instr->hydrogen_value()->position().raw(),
774 instr->Mnemonic(), detail);
775 DCHECK(info()->IsStub() || frame_is_built_);
776 // Go through jump table if we need to handle condition, build frame, or
777 // restore caller doubles.
778 if (cc == no_condition && frame_is_built_ &&
779 !info()->saves_caller_doubles()) {
780 DeoptComment(reason);
781 __ call(entry, RelocInfo::RUNTIME_ENTRY);
Steve Block1e0659c2011-05-24 12:43:12 +0100782 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000783 Deoptimizer::JumpTableEntry table_entry(entry, reason, bailout_type,
784 !frame_is_built_);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100785 // We often have several deopts to the same entry, reuse the last
786 // jump entry if this is the case.
Steve Block44f0eee2011-05-26 01:26:41 +0100787 if (jump_table_.is_empty() ||
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000788 !table_entry.IsEquivalentTo(jump_table_.last())) {
789 jump_table_.Add(table_entry, zone());
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100790 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000791 if (cc == no_condition) {
792 __ jmp(&jump_table_.last().label);
793 } else {
794 __ j(cc, &jump_table_.last().label);
795 }
Steve Block1e0659c2011-05-24 12:43:12 +0100796 }
Ben Murdochb8e0da22011-05-16 14:20:40 +0100797}
798
799
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000800void LCodeGen::DeoptimizeIf(Condition cc, LInstruction* instr,
801 const char* detail) {
802 Deoptimizer::BailoutType bailout_type = info()->IsStub()
803 ? Deoptimizer::LAZY
804 : Deoptimizer::EAGER;
805 DeoptimizeIf(cc, instr, detail, bailout_type);
806}
807
808
Ben Murdochb8e0da22011-05-16 14:20:40 +0100809void LCodeGen::PopulateDeoptimizationData(Handle<Code> code) {
810 int length = deoptimizations_.length();
811 if (length == 0) return;
Ben Murdochb8e0da22011-05-16 14:20:40 +0100812 Handle<DeoptimizationInputData> data =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000813 DeoptimizationInputData::New(isolate(), length, TENURED);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100814
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000815 Handle<ByteArray> translations =
816 translations_.CreateByteArray(isolate()->factory());
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100817 data->SetTranslationByteArray(*translations);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100818 data->SetInlinedFunctionCount(Smi::FromInt(inlined_function_count_));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000819 data->SetOptimizationId(Smi::FromInt(info_->optimization_id()));
820 if (info_->IsOptimizing()) {
821 // Reference to shared function info does not change between phases.
822 AllowDeferredHandleDereference allow_handle_dereference;
823 data->SetSharedFunctionInfo(*info_->shared_info());
824 } else {
825 data->SetSharedFunctionInfo(Smi::FromInt(0));
826 }
Ben Murdochb8e0da22011-05-16 14:20:40 +0100827
828 Handle<FixedArray> literals =
Steve Block44f0eee2011-05-26 01:26:41 +0100829 factory()->NewFixedArray(deoptimization_literals_.length(), TENURED);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000830 { AllowDeferredHandleDereference copy_handles;
831 for (int i = 0; i < deoptimization_literals_.length(); i++) {
832 literals->set(i, *deoptimization_literals_[i]);
833 }
834 data->SetLiteralArray(*literals);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100835 }
Ben Murdochb8e0da22011-05-16 14:20:40 +0100836
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000837 data->SetOsrAstId(Smi::FromInt(info_->osr_ast_id().ToInt()));
Ben Murdochb8e0da22011-05-16 14:20:40 +0100838 data->SetOsrPcOffset(Smi::FromInt(osr_pc_offset_));
839
840 // Populate the deoptimization entries.
841 for (int i = 0; i < length; i++) {
842 LEnvironment* env = deoptimizations_[i];
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000843 data->SetAstId(i, env->ast_id());
Ben Murdochb8e0da22011-05-16 14:20:40 +0100844 data->SetTranslationIndex(i, Smi::FromInt(env->translation_index()));
845 data->SetArgumentsStackHeight(i,
846 Smi::FromInt(env->arguments_stack_height()));
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000847 data->SetPc(i, Smi::FromInt(env->pc_offset()));
Ben Murdochb8e0da22011-05-16 14:20:40 +0100848 }
849 code->set_deoptimization_data(*data);
850}
851
852
853int LCodeGen::DefineDeoptimizationLiteral(Handle<Object> literal) {
854 int result = deoptimization_literals_.length();
855 for (int i = 0; i < deoptimization_literals_.length(); ++i) {
856 if (deoptimization_literals_[i].is_identical_to(literal)) return i;
857 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000858 deoptimization_literals_.Add(literal, zone());
Ben Murdochb8e0da22011-05-16 14:20:40 +0100859 return result;
860}
861
862
863void LCodeGen::PopulateDeoptimizationLiteralsWithInlinedFunctions() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000864 DCHECK(deoptimization_literals_.length() == 0);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100865
866 const ZoneList<Handle<JSFunction> >* inlined_closures =
867 chunk()->inlined_closures();
868
869 for (int i = 0, length = inlined_closures->length();
870 i < length;
871 i++) {
872 DefineDeoptimizationLiteral(inlined_closures->at(i));
873 }
874
875 inlined_function_count_ = deoptimization_literals_.length();
876}
877
878
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000879void LCodeGen::RecordSafepointWithLazyDeopt(
880 LInstruction* instr, SafepointMode safepoint_mode, int argc) {
881 if (safepoint_mode == RECORD_SIMPLE_SAFEPOINT) {
882 RecordSafepoint(instr->pointer_map(), Safepoint::kLazyDeopt);
883 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000884 DCHECK(safepoint_mode == RECORD_SAFEPOINT_WITH_REGISTERS);
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000885 RecordSafepointWithRegisters(
886 instr->pointer_map(), argc, Safepoint::kLazyDeopt);
887 }
888}
889
890
Steve Block1e0659c2011-05-24 12:43:12 +0100891void LCodeGen::RecordSafepoint(
892 LPointerMap* pointers,
893 Safepoint::Kind kind,
894 int arguments,
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000895 Safepoint::DeoptMode deopt_mode) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000896 DCHECK(kind == expected_safepoint_kind_);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100897
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100898 const ZoneList<LOperand*>* operands = pointers->GetNormalizedOperands();
Steve Block1e0659c2011-05-24 12:43:12 +0100899
Ben Murdochb8e0da22011-05-16 14:20:40 +0100900 Safepoint safepoint = safepoints_.DefineSafepoint(masm(),
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000901 kind, arguments, deopt_mode);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100902 for (int i = 0; i < operands->length(); i++) {
903 LOperand* pointer = operands->at(i);
904 if (pointer->IsStackSlot()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000905 safepoint.DefinePointerSlot(pointer->index(), zone());
Steve Block1e0659c2011-05-24 12:43:12 +0100906 } else if (pointer->IsRegister() && (kind & Safepoint::kWithRegisters)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000907 safepoint.DefinePointerRegister(ToRegister(pointer), zone());
Ben Murdochb8e0da22011-05-16 14:20:40 +0100908 }
909 }
Steve Block1e0659c2011-05-24 12:43:12 +0100910}
911
912
913void LCodeGen::RecordSafepoint(LPointerMap* pointers,
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000914 Safepoint::DeoptMode deopt_mode) {
915 RecordSafepoint(pointers, Safepoint::kSimple, 0, deopt_mode);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100916}
917
918
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000919void LCodeGen::RecordSafepoint(Safepoint::DeoptMode deopt_mode) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000920 LPointerMap empty_pointers(zone());
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000921 RecordSafepoint(&empty_pointers, deopt_mode);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100922}
923
924
Ben Murdochb8e0da22011-05-16 14:20:40 +0100925void LCodeGen::RecordSafepointWithRegisters(LPointerMap* pointers,
926 int arguments,
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000927 Safepoint::DeoptMode deopt_mode) {
928 RecordSafepoint(pointers, Safepoint::kWithRegisters, arguments, deopt_mode);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100929}
930
931
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000932void LCodeGen::RecordAndWritePosition(int position) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000933 if (position == RelocInfo::kNoPosition) return;
Ben Murdochb8e0da22011-05-16 14:20:40 +0100934 masm()->positions_recorder()->RecordPosition(position);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000935 masm()->positions_recorder()->WriteRecordedPositions();
936}
937
938
939static const char* LabelType(LLabel* label) {
940 if (label->is_loop_header()) return " (loop header)";
941 if (label->is_osr_entry()) return " (OSR entry)";
942 return "";
Ben Murdochb8e0da22011-05-16 14:20:40 +0100943}
944
945
946void LCodeGen::DoLabel(LLabel* label) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000947 Comment(";;; <@%d,#%d> -------------------- B%d%s --------------------",
948 current_instruction_,
949 label->hydrogen_value()->id(),
950 label->block_id(),
951 LabelType(label));
Ben Murdochb8e0da22011-05-16 14:20:40 +0100952 __ bind(label->label());
953 current_block_ = label->block_id();
Ben Murdoch257744e2011-11-30 15:57:28 +0000954 DoGap(label);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100955}
956
957
958void LCodeGen::DoParallelMove(LParallelMove* move) {
Steve Block1e0659c2011-05-24 12:43:12 +0100959 resolver_.Resolve(move);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100960}
961
962
963void LCodeGen::DoGap(LGap* gap) {
964 for (int i = LGap::FIRST_INNER_POSITION;
965 i <= LGap::LAST_INNER_POSITION;
966 i++) {
967 LGap::InnerPosition inner_pos = static_cast<LGap::InnerPosition>(i);
968 LParallelMove* move = gap->GetParallelMove(inner_pos);
969 if (move != NULL) DoParallelMove(move);
970 }
Ben Murdochb8e0da22011-05-16 14:20:40 +0100971}
972
973
Ben Murdoch257744e2011-11-30 15:57:28 +0000974void LCodeGen::DoInstructionGap(LInstructionGap* instr) {
975 DoGap(instr);
976}
977
978
Ben Murdochb8e0da22011-05-16 14:20:40 +0100979void LCodeGen::DoParameter(LParameter* instr) {
980 // Nothing to do.
981}
982
983
984void LCodeGen::DoCallStub(LCallStub* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000985 DCHECK(ToRegister(instr->context()).is(rsi));
986 DCHECK(ToRegister(instr->result()).is(rax));
Steve Block1e0659c2011-05-24 12:43:12 +0100987 switch (instr->hydrogen()->major_key()) {
Steve Block1e0659c2011-05-24 12:43:12 +0100988 case CodeStub::RegExpExec: {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000989 RegExpExecStub stub(isolate());
Steve Block1e0659c2011-05-24 12:43:12 +0100990 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
991 break;
992 }
993 case CodeStub::SubString: {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000994 SubStringStub stub(isolate());
Steve Block1e0659c2011-05-24 12:43:12 +0100995 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
996 break;
997 }
998 case CodeStub::StringCompare: {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000999 StringCompareStub stub(isolate());
Steve Block1e0659c2011-05-24 12:43:12 +01001000 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
1001 break;
1002 }
1003 default:
1004 UNREACHABLE();
1005 }
Ben Murdochb8e0da22011-05-16 14:20:40 +01001006}
1007
1008
1009void LCodeGen::DoUnknownOSRValue(LUnknownOSRValue* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001010 GenerateOsrPrologue();
1011}
1012
1013
1014void LCodeGen::DoModByPowerOf2I(LModByPowerOf2I* instr) {
1015 Register dividend = ToRegister(instr->dividend());
1016 int32_t divisor = instr->divisor();
1017 DCHECK(dividend.is(ToRegister(instr->result())));
1018
1019 // Theoretically, a variation of the branch-free code for integer division by
1020 // a power of 2 (calculating the remainder via an additional multiplication
1021 // (which gets simplified to an 'and') and subtraction) should be faster, and
1022 // this is exactly what GCC and clang emit. Nevertheless, benchmarks seem to
1023 // indicate that positive dividends are heavily favored, so the branching
1024 // version performs better.
1025 HMod* hmod = instr->hydrogen();
1026 int32_t mask = divisor < 0 ? -(divisor + 1) : (divisor - 1);
1027 Label dividend_is_not_negative, done;
1028 if (hmod->CheckFlag(HValue::kLeftCanBeNegative)) {
1029 __ testl(dividend, dividend);
1030 __ j(not_sign, &dividend_is_not_negative, Label::kNear);
1031 // Note that this is correct even for kMinInt operands.
1032 __ negl(dividend);
1033 __ andl(dividend, Immediate(mask));
1034 __ negl(dividend);
1035 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1036 DeoptimizeIf(zero, instr, "minus zero");
1037 }
1038 __ jmp(&done, Label::kNear);
1039 }
1040
1041 __ bind(&dividend_is_not_negative);
1042 __ andl(dividend, Immediate(mask));
1043 __ bind(&done);
1044}
1045
1046
1047void LCodeGen::DoModByConstI(LModByConstI* instr) {
1048 Register dividend = ToRegister(instr->dividend());
1049 int32_t divisor = instr->divisor();
1050 DCHECK(ToRegister(instr->result()).is(rax));
1051
1052 if (divisor == 0) {
1053 DeoptimizeIf(no_condition, instr, "division by zero");
1054 return;
1055 }
1056
1057 __ TruncatingDiv(dividend, Abs(divisor));
1058 __ imull(rdx, rdx, Immediate(Abs(divisor)));
1059 __ movl(rax, dividend);
1060 __ subl(rax, rdx);
1061
1062 // Check for negative zero.
1063 HMod* hmod = instr->hydrogen();
1064 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1065 Label remainder_not_zero;
1066 __ j(not_zero, &remainder_not_zero, Label::kNear);
1067 __ cmpl(dividend, Immediate(0));
1068 DeoptimizeIf(less, instr, "minus zero");
1069 __ bind(&remainder_not_zero);
1070 }
Ben Murdochb8e0da22011-05-16 14:20:40 +01001071}
1072
1073
1074void LCodeGen::DoModI(LModI* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001075 HMod* hmod = instr->hydrogen();
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001076
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001077 Register left_reg = ToRegister(instr->left());
1078 DCHECK(left_reg.is(rax));
1079 Register right_reg = ToRegister(instr->right());
1080 DCHECK(!right_reg.is(rax));
1081 DCHECK(!right_reg.is(rdx));
1082 Register result_reg = ToRegister(instr->result());
1083 DCHECK(result_reg.is(rdx));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001084
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001085 Label done;
1086 // Check for x % 0, idiv would signal a divide error. We have to
1087 // deopt in this case because we can't return a NaN.
1088 if (hmod->CheckFlag(HValue::kCanBeDivByZero)) {
1089 __ testl(right_reg, right_reg);
1090 DeoptimizeIf(zero, instr, "division by zero");
1091 }
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001092
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001093 // Check for kMinInt % -1, idiv would signal a divide error. We
1094 // have to deopt if we care about -0, because we can't return that.
1095 if (hmod->CheckFlag(HValue::kCanOverflow)) {
1096 Label no_overflow_possible;
1097 __ cmpl(left_reg, Immediate(kMinInt));
1098 __ j(not_zero, &no_overflow_possible, Label::kNear);
1099 __ cmpl(right_reg, Immediate(-1));
1100 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1101 DeoptimizeIf(equal, instr, "minus zero");
Ben Murdoch7d3e7fc2011-07-12 16:37:06 +01001102 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001103 __ j(not_equal, &no_overflow_possible, Label::kNear);
1104 __ Set(result_reg, 0);
Ben Murdoch257744e2011-11-30 15:57:28 +00001105 __ jmp(&done, Label::kNear);
Steve Block44f0eee2011-05-26 01:26:41 +01001106 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001107 __ bind(&no_overflow_possible);
1108 }
Steve Block44f0eee2011-05-26 01:26:41 +01001109
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001110 // Sign extend dividend in eax into edx:eax, since we are using only the low
1111 // 32 bits of the values.
1112 __ cdq();
Steve Block44f0eee2011-05-26 01:26:41 +01001113
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001114 // If we care about -0, test if the dividend is <0 and the result is 0.
1115 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1116 Label positive_left;
Ben Murdoch257744e2011-11-30 15:57:28 +00001117 __ testl(left_reg, left_reg);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001118 __ j(not_sign, &positive_left, Label::kNear);
1119 __ idivl(right_reg);
1120 __ testl(result_reg, result_reg);
1121 DeoptimizeIf(zero, instr, "minus zero");
Ben Murdoch257744e2011-11-30 15:57:28 +00001122 __ jmp(&done, Label::kNear);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001123 __ bind(&positive_left);
1124 }
1125 __ idivl(right_reg);
1126 __ bind(&done);
1127}
Ben Murdoch257744e2011-11-30 15:57:28 +00001128
Ben Murdoch257744e2011-11-30 15:57:28 +00001129
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001130void LCodeGen::DoFlooringDivByPowerOf2I(LFlooringDivByPowerOf2I* instr) {
1131 Register dividend = ToRegister(instr->dividend());
1132 int32_t divisor = instr->divisor();
1133 DCHECK(dividend.is(ToRegister(instr->result())));
1134
1135 // If the divisor is positive, things are easy: There can be no deopts and we
1136 // can simply do an arithmetic right shift.
1137 if (divisor == 1) return;
1138 int32_t shift = WhichPowerOf2Abs(divisor);
1139 if (divisor > 1) {
1140 __ sarl(dividend, Immediate(shift));
1141 return;
1142 }
1143
1144 // If the divisor is negative, we have to negate and handle edge cases.
1145 __ negl(dividend);
1146 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
1147 DeoptimizeIf(zero, instr, "minus zero");
1148 }
1149
1150 // Dividing by -1 is basically negation, unless we overflow.
1151 if (divisor == -1) {
1152 if (instr->hydrogen()->CheckFlag(HValue::kLeftCanBeMinInt)) {
1153 DeoptimizeIf(overflow, instr, "overflow");
1154 }
1155 return;
1156 }
1157
1158 // If the negation could not overflow, simply shifting is OK.
1159 if (!instr->hydrogen()->CheckFlag(HValue::kLeftCanBeMinInt)) {
1160 __ sarl(dividend, Immediate(shift));
1161 return;
1162 }
1163
1164 Label not_kmin_int, done;
1165 __ j(no_overflow, &not_kmin_int, Label::kNear);
1166 __ movl(dividend, Immediate(kMinInt / divisor));
1167 __ jmp(&done, Label::kNear);
1168 __ bind(&not_kmin_int);
1169 __ sarl(dividend, Immediate(shift));
1170 __ bind(&done);
1171}
1172
1173
1174void LCodeGen::DoFlooringDivByConstI(LFlooringDivByConstI* instr) {
1175 Register dividend = ToRegister(instr->dividend());
1176 int32_t divisor = instr->divisor();
1177 DCHECK(ToRegister(instr->result()).is(rdx));
1178
1179 if (divisor == 0) {
1180 DeoptimizeIf(no_condition, instr, "division by zero");
1181 return;
1182 }
1183
1184 // Check for (0 / -x) that will produce negative zero.
1185 HMathFloorOfDiv* hdiv = instr->hydrogen();
1186 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) {
1187 __ testl(dividend, dividend);
1188 DeoptimizeIf(zero, instr, "minus zero");
1189 }
1190
1191 // Easy case: We need no dynamic check for the dividend and the flooring
1192 // division is the same as the truncating division.
1193 if ((divisor > 0 && !hdiv->CheckFlag(HValue::kLeftCanBeNegative)) ||
1194 (divisor < 0 && !hdiv->CheckFlag(HValue::kLeftCanBePositive))) {
1195 __ TruncatingDiv(dividend, Abs(divisor));
1196 if (divisor < 0) __ negl(rdx);
1197 return;
1198 }
1199
1200 // In the general case we may need to adjust before and after the truncating
1201 // division to get a flooring division.
1202 Register temp = ToRegister(instr->temp3());
1203 DCHECK(!temp.is(dividend) && !temp.is(rax) && !temp.is(rdx));
1204 Label needs_adjustment, done;
1205 __ cmpl(dividend, Immediate(0));
1206 __ j(divisor > 0 ? less : greater, &needs_adjustment, Label::kNear);
1207 __ TruncatingDiv(dividend, Abs(divisor));
1208 if (divisor < 0) __ negl(rdx);
1209 __ jmp(&done, Label::kNear);
1210 __ bind(&needs_adjustment);
1211 __ leal(temp, Operand(dividend, divisor > 0 ? 1 : -1));
1212 __ TruncatingDiv(temp, Abs(divisor));
1213 if (divisor < 0) __ negl(rdx);
1214 __ decl(rdx);
1215 __ bind(&done);
1216}
1217
1218
1219// TODO(svenpanne) Refactor this to avoid code duplication with DoDivI.
1220void LCodeGen::DoFlooringDivI(LFlooringDivI* instr) {
1221 HBinaryOperation* hdiv = instr->hydrogen();
1222 Register dividend = ToRegister(instr->dividend());
1223 Register divisor = ToRegister(instr->divisor());
1224 Register remainder = ToRegister(instr->temp());
1225 Register result = ToRegister(instr->result());
1226 DCHECK(dividend.is(rax));
1227 DCHECK(remainder.is(rdx));
1228 DCHECK(result.is(rax));
1229 DCHECK(!divisor.is(rax));
1230 DCHECK(!divisor.is(rdx));
1231
1232 // Check for x / 0.
1233 if (hdiv->CheckFlag(HValue::kCanBeDivByZero)) {
1234 __ testl(divisor, divisor);
1235 DeoptimizeIf(zero, instr, "division by zero");
1236 }
1237
1238 // Check for (0 / -x) that will produce negative zero.
1239 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero)) {
1240 Label dividend_not_zero;
1241 __ testl(dividend, dividend);
1242 __ j(not_zero, &dividend_not_zero, Label::kNear);
1243 __ testl(divisor, divisor);
1244 DeoptimizeIf(sign, instr, "minus zero");
1245 __ bind(&dividend_not_zero);
1246 }
1247
1248 // Check for (kMinInt / -1).
1249 if (hdiv->CheckFlag(HValue::kCanOverflow)) {
1250 Label dividend_not_min_int;
1251 __ cmpl(dividend, Immediate(kMinInt));
1252 __ j(not_zero, &dividend_not_min_int, Label::kNear);
1253 __ cmpl(divisor, Immediate(-1));
1254 DeoptimizeIf(zero, instr, "overflow");
1255 __ bind(&dividend_not_min_int);
1256 }
1257
1258 // Sign extend to rdx (= remainder).
1259 __ cdq();
1260 __ idivl(divisor);
1261
1262 Label done;
1263 __ testl(remainder, remainder);
1264 __ j(zero, &done, Label::kNear);
1265 __ xorl(remainder, divisor);
1266 __ sarl(remainder, Immediate(31));
1267 __ addl(result, remainder);
1268 __ bind(&done);
1269}
1270
1271
1272void LCodeGen::DoDivByPowerOf2I(LDivByPowerOf2I* instr) {
1273 Register dividend = ToRegister(instr->dividend());
1274 int32_t divisor = instr->divisor();
1275 Register result = ToRegister(instr->result());
1276 DCHECK(divisor == kMinInt || base::bits::IsPowerOfTwo32(Abs(divisor)));
1277 DCHECK(!result.is(dividend));
1278
1279 // Check for (0 / -x) that will produce negative zero.
1280 HDiv* hdiv = instr->hydrogen();
1281 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) {
1282 __ testl(dividend, dividend);
1283 DeoptimizeIf(zero, instr, "minus zero");
1284 }
1285 // Check for (kMinInt / -1).
1286 if (hdiv->CheckFlag(HValue::kCanOverflow) && divisor == -1) {
1287 __ cmpl(dividend, Immediate(kMinInt));
1288 DeoptimizeIf(zero, instr, "overflow");
1289 }
1290 // Deoptimize if remainder will not be 0.
1291 if (!hdiv->CheckFlag(HInstruction::kAllUsesTruncatingToInt32) &&
1292 divisor != 1 && divisor != -1) {
1293 int32_t mask = divisor < 0 ? -(divisor + 1) : (divisor - 1);
1294 __ testl(dividend, Immediate(mask));
1295 DeoptimizeIf(not_zero, instr, "lost precision");
1296 }
1297 __ Move(result, dividend);
1298 int32_t shift = WhichPowerOf2Abs(divisor);
1299 if (shift > 0) {
1300 // The arithmetic shift is always OK, the 'if' is an optimization only.
1301 if (shift > 1) __ sarl(result, Immediate(31));
1302 __ shrl(result, Immediate(32 - shift));
1303 __ addl(result, dividend);
1304 __ sarl(result, Immediate(shift));
1305 }
1306 if (divisor < 0) __ negl(result);
1307}
1308
1309
1310void LCodeGen::DoDivByConstI(LDivByConstI* instr) {
1311 Register dividend = ToRegister(instr->dividend());
1312 int32_t divisor = instr->divisor();
1313 DCHECK(ToRegister(instr->result()).is(rdx));
1314
1315 if (divisor == 0) {
1316 DeoptimizeIf(no_condition, instr, "division by zero");
1317 return;
1318 }
1319
1320 // Check for (0 / -x) that will produce negative zero.
1321 HDiv* hdiv = instr->hydrogen();
1322 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) {
1323 __ testl(dividend, dividend);
1324 DeoptimizeIf(zero, instr, "minus zero");
1325 }
1326
1327 __ TruncatingDiv(dividend, Abs(divisor));
1328 if (divisor < 0) __ negl(rdx);
1329
1330 if (!hdiv->CheckFlag(HInstruction::kAllUsesTruncatingToInt32)) {
1331 __ movl(rax, rdx);
1332 __ imull(rax, rax, Immediate(divisor));
1333 __ subl(rax, dividend);
1334 DeoptimizeIf(not_equal, instr, "lost precision");
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001335 }
Ben Murdochb8e0da22011-05-16 14:20:40 +01001336}
1337
1338
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001339// TODO(svenpanne) Refactor this to avoid code duplication with DoFlooringDivI.
Ben Murdochb8e0da22011-05-16 14:20:40 +01001340void LCodeGen::DoDivI(LDivI* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001341 HBinaryOperation* hdiv = instr->hydrogen();
1342 Register dividend = ToRegister(instr->dividend());
1343 Register divisor = ToRegister(instr->divisor());
1344 Register remainder = ToRegister(instr->temp());
1345 DCHECK(dividend.is(rax));
1346 DCHECK(remainder.is(rdx));
1347 DCHECK(ToRegister(instr->result()).is(rax));
1348 DCHECK(!divisor.is(rax));
1349 DCHECK(!divisor.is(rdx));
Steve Block1e0659c2011-05-24 12:43:12 +01001350
1351 // Check for x / 0.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001352 if (hdiv->CheckFlag(HValue::kCanBeDivByZero)) {
1353 __ testl(divisor, divisor);
1354 DeoptimizeIf(zero, instr, "division by zero");
Steve Block1e0659c2011-05-24 12:43:12 +01001355 }
1356
1357 // Check for (0 / -x) that will produce negative zero.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001358 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero)) {
1359 Label dividend_not_zero;
1360 __ testl(dividend, dividend);
1361 __ j(not_zero, &dividend_not_zero, Label::kNear);
1362 __ testl(divisor, divisor);
1363 DeoptimizeIf(sign, instr, "minus zero");
1364 __ bind(&dividend_not_zero);
Steve Block1e0659c2011-05-24 12:43:12 +01001365 }
1366
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001367 // Check for (kMinInt / -1).
1368 if (hdiv->CheckFlag(HValue::kCanOverflow)) {
1369 Label dividend_not_min_int;
1370 __ cmpl(dividend, Immediate(kMinInt));
1371 __ j(not_zero, &dividend_not_min_int, Label::kNear);
1372 __ cmpl(divisor, Immediate(-1));
1373 DeoptimizeIf(zero, instr, "overflow");
1374 __ bind(&dividend_not_min_int);
Steve Block1e0659c2011-05-24 12:43:12 +01001375 }
1376
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001377 // Sign extend to rdx (= remainder).
Steve Block1e0659c2011-05-24 12:43:12 +01001378 __ cdq();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001379 __ idivl(divisor);
Steve Block1e0659c2011-05-24 12:43:12 +01001380
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001381 if (!hdiv->CheckFlag(HValue::kAllUsesTruncatingToInt32)) {
1382 // Deoptimize if remainder is not 0.
1383 __ testl(remainder, remainder);
1384 DeoptimizeIf(not_zero, instr, "lost precision");
1385 }
Steve Block1e0659c2011-05-24 12:43:12 +01001386}
Ben Murdochb8e0da22011-05-16 14:20:40 +01001387
1388
1389void LCodeGen::DoMulI(LMulI* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001390 Register left = ToRegister(instr->left());
1391 LOperand* right = instr->right();
Steve Block1e0659c2011-05-24 12:43:12 +01001392
1393 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001394 if (instr->hydrogen_value()->representation().IsSmi()) {
1395 __ movp(kScratchRegister, left);
1396 } else {
1397 __ movl(kScratchRegister, left);
1398 }
Steve Block1e0659c2011-05-24 12:43:12 +01001399 }
1400
Steve Block44f0eee2011-05-26 01:26:41 +01001401 bool can_overflow =
1402 instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
Steve Block1e0659c2011-05-24 12:43:12 +01001403 if (right->IsConstantOperand()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001404 int32_t right_value = ToInteger32(LConstantOperand::cast(right));
Steve Block44f0eee2011-05-26 01:26:41 +01001405 if (right_value == -1) {
1406 __ negl(left);
1407 } else if (right_value == 0) {
1408 __ xorl(left, left);
1409 } else if (right_value == 2) {
1410 __ addl(left, left);
1411 } else if (!can_overflow) {
1412 // If the multiplication is known to not overflow, we
1413 // can use operations that don't set the overflow flag
1414 // correctly.
1415 switch (right_value) {
1416 case 1:
1417 // Do nothing.
1418 break;
1419 case 3:
1420 __ leal(left, Operand(left, left, times_2, 0));
1421 break;
1422 case 4:
1423 __ shll(left, Immediate(2));
1424 break;
1425 case 5:
1426 __ leal(left, Operand(left, left, times_4, 0));
1427 break;
1428 case 8:
1429 __ shll(left, Immediate(3));
1430 break;
1431 case 9:
1432 __ leal(left, Operand(left, left, times_8, 0));
1433 break;
1434 case 16:
1435 __ shll(left, Immediate(4));
1436 break;
1437 default:
1438 __ imull(left, left, Immediate(right_value));
1439 break;
1440 }
1441 } else {
1442 __ imull(left, left, Immediate(right_value));
1443 }
Steve Block1e0659c2011-05-24 12:43:12 +01001444 } else if (right->IsStackSlot()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001445 if (instr->hydrogen_value()->representation().IsSmi()) {
1446 __ SmiToInteger64(left, left);
1447 __ imulp(left, ToOperand(right));
1448 } else {
1449 __ imull(left, ToOperand(right));
1450 }
Steve Block1e0659c2011-05-24 12:43:12 +01001451 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001452 if (instr->hydrogen_value()->representation().IsSmi()) {
1453 __ SmiToInteger64(left, left);
1454 __ imulp(left, ToRegister(right));
1455 } else {
1456 __ imull(left, ToRegister(right));
1457 }
Steve Block1e0659c2011-05-24 12:43:12 +01001458 }
1459
Steve Block44f0eee2011-05-26 01:26:41 +01001460 if (can_overflow) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001461 DeoptimizeIf(overflow, instr, "overflow");
Steve Block1e0659c2011-05-24 12:43:12 +01001462 }
1463
1464 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
1465 // Bail out if the result is supposed to be negative zero.
Ben Murdoch257744e2011-11-30 15:57:28 +00001466 Label done;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001467 if (instr->hydrogen_value()->representation().IsSmi()) {
1468 __ testp(left, left);
1469 } else {
1470 __ testl(left, left);
1471 }
Ben Murdoch257744e2011-11-30 15:57:28 +00001472 __ j(not_zero, &done, Label::kNear);
Steve Block1e0659c2011-05-24 12:43:12 +01001473 if (right->IsConstantOperand()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001474 // Constant can't be represented as 32-bit Smi due to immediate size
1475 // limit.
1476 DCHECK(SmiValuesAre32Bits()
1477 ? !instr->hydrogen_value()->representation().IsSmi()
1478 : SmiValuesAre31Bits());
1479 if (ToInteger32(LConstantOperand::cast(right)) < 0) {
1480 DeoptimizeIf(no_condition, instr, "minus zero");
1481 } else if (ToInteger32(LConstantOperand::cast(right)) == 0) {
1482 __ cmpl(kScratchRegister, Immediate(0));
1483 DeoptimizeIf(less, instr, "minus zero");
Steve Block1e0659c2011-05-24 12:43:12 +01001484 }
1485 } else if (right->IsStackSlot()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001486 if (instr->hydrogen_value()->representation().IsSmi()) {
1487 __ orp(kScratchRegister, ToOperand(right));
1488 } else {
1489 __ orl(kScratchRegister, ToOperand(right));
1490 }
1491 DeoptimizeIf(sign, instr, "minus zero");
Steve Block1e0659c2011-05-24 12:43:12 +01001492 } else {
1493 // Test the non-zero operand for negative sign.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001494 if (instr->hydrogen_value()->representation().IsSmi()) {
1495 __ orp(kScratchRegister, ToRegister(right));
1496 } else {
1497 __ orl(kScratchRegister, ToRegister(right));
1498 }
1499 DeoptimizeIf(sign, instr, "minus zero");
Steve Block1e0659c2011-05-24 12:43:12 +01001500 }
1501 __ bind(&done);
1502 }
1503}
Ben Murdochb8e0da22011-05-16 14:20:40 +01001504
1505
1506void LCodeGen::DoBitI(LBitI* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001507 LOperand* left = instr->left();
1508 LOperand* right = instr->right();
1509 DCHECK(left->Equals(instr->result()));
1510 DCHECK(left->IsRegister());
Steve Block1e0659c2011-05-24 12:43:12 +01001511
1512 if (right->IsConstantOperand()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001513 int32_t right_operand =
1514 ToRepresentation(LConstantOperand::cast(right),
1515 instr->hydrogen()->right()->representation());
Steve Block1e0659c2011-05-24 12:43:12 +01001516 switch (instr->op()) {
1517 case Token::BIT_AND:
1518 __ andl(ToRegister(left), Immediate(right_operand));
1519 break;
1520 case Token::BIT_OR:
1521 __ orl(ToRegister(left), Immediate(right_operand));
1522 break;
1523 case Token::BIT_XOR:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001524 if (right_operand == int32_t(~0)) {
1525 __ notl(ToRegister(left));
1526 } else {
1527 __ xorl(ToRegister(left), Immediate(right_operand));
1528 }
Steve Block1e0659c2011-05-24 12:43:12 +01001529 break;
1530 default:
1531 UNREACHABLE();
1532 break;
1533 }
1534 } else if (right->IsStackSlot()) {
1535 switch (instr->op()) {
1536 case Token::BIT_AND:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001537 if (instr->IsInteger32()) {
1538 __ andl(ToRegister(left), ToOperand(right));
1539 } else {
1540 __ andp(ToRegister(left), ToOperand(right));
1541 }
Steve Block1e0659c2011-05-24 12:43:12 +01001542 break;
1543 case Token::BIT_OR:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001544 if (instr->IsInteger32()) {
1545 __ orl(ToRegister(left), ToOperand(right));
1546 } else {
1547 __ orp(ToRegister(left), ToOperand(right));
1548 }
Steve Block1e0659c2011-05-24 12:43:12 +01001549 break;
1550 case Token::BIT_XOR:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001551 if (instr->IsInteger32()) {
1552 __ xorl(ToRegister(left), ToOperand(right));
1553 } else {
1554 __ xorp(ToRegister(left), ToOperand(right));
1555 }
Steve Block1e0659c2011-05-24 12:43:12 +01001556 break;
1557 default:
1558 UNREACHABLE();
1559 break;
1560 }
1561 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001562 DCHECK(right->IsRegister());
Steve Block1e0659c2011-05-24 12:43:12 +01001563 switch (instr->op()) {
1564 case Token::BIT_AND:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001565 if (instr->IsInteger32()) {
1566 __ andl(ToRegister(left), ToRegister(right));
1567 } else {
1568 __ andp(ToRegister(left), ToRegister(right));
1569 }
Steve Block1e0659c2011-05-24 12:43:12 +01001570 break;
1571 case Token::BIT_OR:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001572 if (instr->IsInteger32()) {
1573 __ orl(ToRegister(left), ToRegister(right));
1574 } else {
1575 __ orp(ToRegister(left), ToRegister(right));
1576 }
Steve Block1e0659c2011-05-24 12:43:12 +01001577 break;
1578 case Token::BIT_XOR:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001579 if (instr->IsInteger32()) {
1580 __ xorl(ToRegister(left), ToRegister(right));
1581 } else {
1582 __ xorp(ToRegister(left), ToRegister(right));
1583 }
Steve Block1e0659c2011-05-24 12:43:12 +01001584 break;
1585 default:
1586 UNREACHABLE();
1587 break;
1588 }
1589 }
1590}
Ben Murdochb8e0da22011-05-16 14:20:40 +01001591
1592
1593void LCodeGen::DoShiftI(LShiftI* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001594 LOperand* left = instr->left();
1595 LOperand* right = instr->right();
1596 DCHECK(left->Equals(instr->result()));
1597 DCHECK(left->IsRegister());
Steve Block1e0659c2011-05-24 12:43:12 +01001598 if (right->IsRegister()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001599 DCHECK(ToRegister(right).is(rcx));
Steve Block1e0659c2011-05-24 12:43:12 +01001600
1601 switch (instr->op()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001602 case Token::ROR:
1603 __ rorl_cl(ToRegister(left));
1604 break;
Steve Block1e0659c2011-05-24 12:43:12 +01001605 case Token::SAR:
1606 __ sarl_cl(ToRegister(left));
1607 break;
1608 case Token::SHR:
1609 __ shrl_cl(ToRegister(left));
1610 if (instr->can_deopt()) {
1611 __ testl(ToRegister(left), ToRegister(left));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001612 DeoptimizeIf(negative, instr, "negative value");
Steve Block1e0659c2011-05-24 12:43:12 +01001613 }
1614 break;
1615 case Token::SHL:
1616 __ shll_cl(ToRegister(left));
1617 break;
1618 default:
1619 UNREACHABLE();
1620 break;
1621 }
1622 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001623 int32_t value = ToInteger32(LConstantOperand::cast(right));
Steve Block1e0659c2011-05-24 12:43:12 +01001624 uint8_t shift_count = static_cast<uint8_t>(value & 0x1F);
1625 switch (instr->op()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001626 case Token::ROR:
1627 if (shift_count != 0) {
1628 __ rorl(ToRegister(left), Immediate(shift_count));
1629 }
1630 break;
Steve Block1e0659c2011-05-24 12:43:12 +01001631 case Token::SAR:
1632 if (shift_count != 0) {
1633 __ sarl(ToRegister(left), Immediate(shift_count));
1634 }
1635 break;
1636 case Token::SHR:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001637 if (shift_count != 0) {
Steve Block1e0659c2011-05-24 12:43:12 +01001638 __ shrl(ToRegister(left), Immediate(shift_count));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001639 } else if (instr->can_deopt()) {
1640 __ testl(ToRegister(left), ToRegister(left));
1641 DeoptimizeIf(negative, instr, "negative value");
Steve Block1e0659c2011-05-24 12:43:12 +01001642 }
1643 break;
1644 case Token::SHL:
1645 if (shift_count != 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001646 if (instr->hydrogen_value()->representation().IsSmi()) {
1647 if (SmiValuesAre32Bits()) {
1648 __ shlp(ToRegister(left), Immediate(shift_count));
1649 } else {
1650 DCHECK(SmiValuesAre31Bits());
1651 if (instr->can_deopt()) {
1652 if (shift_count != 1) {
1653 __ shll(ToRegister(left), Immediate(shift_count - 1));
1654 }
1655 __ Integer32ToSmi(ToRegister(left), ToRegister(left));
1656 DeoptimizeIf(overflow, instr, "overflow");
1657 } else {
1658 __ shll(ToRegister(left), Immediate(shift_count));
1659 }
1660 }
1661 } else {
1662 __ shll(ToRegister(left), Immediate(shift_count));
1663 }
Steve Block1e0659c2011-05-24 12:43:12 +01001664 }
1665 break;
1666 default:
1667 UNREACHABLE();
1668 break;
1669 }
1670 }
Ben Murdochb8e0da22011-05-16 14:20:40 +01001671}
1672
1673
1674void LCodeGen::DoSubI(LSubI* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001675 LOperand* left = instr->left();
1676 LOperand* right = instr->right();
1677 DCHECK(left->Equals(instr->result()));
Steve Block1e0659c2011-05-24 12:43:12 +01001678
1679 if (right->IsConstantOperand()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001680 int32_t right_operand =
1681 ToRepresentation(LConstantOperand::cast(right),
1682 instr->hydrogen()->right()->representation());
1683 __ subl(ToRegister(left), Immediate(right_operand));
Steve Block1e0659c2011-05-24 12:43:12 +01001684 } else if (right->IsRegister()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001685 if (instr->hydrogen_value()->representation().IsSmi()) {
1686 __ subp(ToRegister(left), ToRegister(right));
1687 } else {
1688 __ subl(ToRegister(left), ToRegister(right));
1689 }
Steve Block1e0659c2011-05-24 12:43:12 +01001690 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001691 if (instr->hydrogen_value()->representation().IsSmi()) {
1692 __ subp(ToRegister(left), ToOperand(right));
1693 } else {
1694 __ subl(ToRegister(left), ToOperand(right));
1695 }
Steve Block1e0659c2011-05-24 12:43:12 +01001696 }
1697
1698 if (instr->hydrogen()->CheckFlag(HValue::kCanOverflow)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001699 DeoptimizeIf(overflow, instr, "overflow");
Steve Block1e0659c2011-05-24 12:43:12 +01001700 }
Ben Murdochb8e0da22011-05-16 14:20:40 +01001701}
1702
1703
1704void LCodeGen::DoConstantI(LConstantI* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001705 Register dst = ToRegister(instr->result());
1706 if (instr->value() == 0) {
1707 __ xorl(dst, dst);
1708 } else {
1709 __ movl(dst, Immediate(instr->value()));
1710 }
1711}
1712
1713
1714void LCodeGen::DoConstantS(LConstantS* instr) {
1715 __ Move(ToRegister(instr->result()), instr->value());
Ben Murdochb8e0da22011-05-16 14:20:40 +01001716}
1717
1718
1719void LCodeGen::DoConstantD(LConstantD* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001720 DCHECK(instr->result()->IsDoubleRegister());
Ben Murdochb8e0da22011-05-16 14:20:40 +01001721 XMMRegister res = ToDoubleRegister(instr->result());
1722 double v = instr->value();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001723 uint64_t int_val = bit_cast<uint64_t, double>(v);
Ben Murdochb8e0da22011-05-16 14:20:40 +01001724 // Use xor to produce +0.0 in a fast and compact way, but avoid to
1725 // do so if the constant is -0.0.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001726 if (int_val == 0) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001727 __ xorps(res, res);
Ben Murdochb8e0da22011-05-16 14:20:40 +01001728 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001729 Register tmp = ToRegister(instr->temp());
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001730 __ Set(tmp, int_val);
1731 __ movq(res, tmp);
Ben Murdochb8e0da22011-05-16 14:20:40 +01001732 }
1733}
1734
1735
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001736void LCodeGen::DoConstantE(LConstantE* instr) {
1737 __ LoadAddress(ToRegister(instr->result()), instr->value());
1738}
1739
1740
Ben Murdochb8e0da22011-05-16 14:20:40 +01001741void LCodeGen::DoConstantT(LConstantT* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001742 Handle<Object> object = instr->value(isolate());
1743 AllowDeferredHandleDereference smi_check;
1744 __ Move(ToRegister(instr->result()), object);
Ben Murdochb8e0da22011-05-16 14:20:40 +01001745}
1746
1747
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001748void LCodeGen::DoMapEnumLength(LMapEnumLength* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01001749 Register result = ToRegister(instr->result());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001750 Register map = ToRegister(instr->value());
1751 __ EnumLength(result, map);
Ben Murdochb8e0da22011-05-16 14:20:40 +01001752}
1753
1754
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001755void LCodeGen::DoDateField(LDateField* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001756 Register object = ToRegister(instr->date());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001757 Register result = ToRegister(instr->result());
1758 Smi* index = instr->index();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001759 Label runtime, done, not_date_object;
1760 DCHECK(object.is(result));
1761 DCHECK(object.is(rax));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001762
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001763 Condition cc = masm()->CheckSmi(object);
1764 DeoptimizeIf(cc, instr, "Smi");
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001765 __ CmpObjectType(object, JS_DATE_TYPE, kScratchRegister);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001766 DeoptimizeIf(not_equal, instr, "not a date object");
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001767
1768 if (index->value() == 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001769 __ movp(result, FieldOperand(object, JSDate::kValueOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001770 } else {
1771 if (index->value() < JSDate::kFirstUncachedField) {
1772 ExternalReference stamp = ExternalReference::date_cache_stamp(isolate());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001773 Operand stamp_operand = __ ExternalOperand(stamp);
1774 __ movp(kScratchRegister, stamp_operand);
1775 __ cmpp(kScratchRegister, FieldOperand(object,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001776 JSDate::kCacheStampOffset));
1777 __ j(not_equal, &runtime, Label::kNear);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001778 __ movp(result, FieldOperand(object, JSDate::kValueOffset +
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001779 kPointerSize * index->value()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001780 __ jmp(&done, Label::kNear);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001781 }
1782 __ bind(&runtime);
1783 __ PrepareCallCFunction(2);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001784 __ movp(arg_reg_1, object);
1785 __ Move(arg_reg_2, index, Assembler::RelocInfoNone());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001786 __ CallCFunction(ExternalReference::get_date_field_function(isolate()), 2);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001787 __ bind(&done);
1788 }
1789}
1790
1791
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001792Operand LCodeGen::BuildSeqStringOperand(Register string,
1793 LOperand* index,
1794 String::Encoding encoding) {
1795 if (index->IsConstantOperand()) {
1796 int offset = ToInteger32(LConstantOperand::cast(index));
1797 if (encoding == String::TWO_BYTE_ENCODING) {
1798 offset *= kUC16Size;
1799 }
1800 STATIC_ASSERT(kCharSize == 1);
1801 return FieldOperand(string, SeqString::kHeaderSize + offset);
1802 }
1803 return FieldOperand(
1804 string, ToRegister(index),
1805 encoding == String::ONE_BYTE_ENCODING ? times_1 : times_2,
1806 SeqString::kHeaderSize);
Ben Murdochb8e0da22011-05-16 14:20:40 +01001807}
1808
1809
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001810void LCodeGen::DoSeqStringGetChar(LSeqStringGetChar* instr) {
1811 String::Encoding encoding = instr->hydrogen()->encoding();
1812 Register result = ToRegister(instr->result());
1813 Register string = ToRegister(instr->string());
Steve Block1e0659c2011-05-24 12:43:12 +01001814
1815 if (FLAG_debug_code) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001816 __ Push(string);
1817 __ movp(string, FieldOperand(string, HeapObject::kMapOffset));
1818 __ movzxbp(string, FieldOperand(string, Map::kInstanceTypeOffset));
1819
1820 __ andb(string, Immediate(kStringRepresentationMask | kStringEncodingMask));
1821 static const uint32_t one_byte_seq_type = kSeqStringTag | kOneByteStringTag;
1822 static const uint32_t two_byte_seq_type = kSeqStringTag | kTwoByteStringTag;
1823 __ cmpp(string, Immediate(encoding == String::ONE_BYTE_ENCODING
1824 ? one_byte_seq_type : two_byte_seq_type));
1825 __ Check(equal, kUnexpectedStringType);
1826 __ Pop(string);
1827 }
1828
1829 Operand operand = BuildSeqStringOperand(string, instr->index(), encoding);
1830 if (encoding == String::ONE_BYTE_ENCODING) {
1831 __ movzxbl(result, operand);
1832 } else {
1833 __ movzxwl(result, operand);
1834 }
1835}
1836
1837
1838void LCodeGen::DoSeqStringSetChar(LSeqStringSetChar* instr) {
1839 String::Encoding encoding = instr->hydrogen()->encoding();
1840 Register string = ToRegister(instr->string());
1841
1842 if (FLAG_debug_code) {
1843 Register value = ToRegister(instr->value());
1844 Register index = ToRegister(instr->index());
1845 static const uint32_t one_byte_seq_type = kSeqStringTag | kOneByteStringTag;
1846 static const uint32_t two_byte_seq_type = kSeqStringTag | kTwoByteStringTag;
1847 int encoding_mask =
1848 instr->hydrogen()->encoding() == String::ONE_BYTE_ENCODING
1849 ? one_byte_seq_type : two_byte_seq_type;
1850 __ EmitSeqStringSetCharCheck(string, index, value, encoding_mask);
1851 }
1852
1853 Operand operand = BuildSeqStringOperand(string, instr->index(), encoding);
1854 if (instr->value()->IsConstantOperand()) {
1855 int value = ToInteger32(LConstantOperand::cast(instr->value()));
1856 DCHECK_LE(0, value);
1857 if (encoding == String::ONE_BYTE_ENCODING) {
1858 DCHECK_LE(value, String::kMaxOneByteCharCode);
1859 __ movb(operand, Immediate(value));
1860 } else {
1861 DCHECK_LE(value, String::kMaxUtf16CodeUnit);
1862 __ movw(operand, Immediate(value));
1863 }
1864 } else {
1865 Register value = ToRegister(instr->value());
1866 if (encoding == String::ONE_BYTE_ENCODING) {
1867 __ movb(operand, value);
1868 } else {
1869 __ movw(operand, value);
1870 }
Steve Block1e0659c2011-05-24 12:43:12 +01001871 }
Ben Murdochb8e0da22011-05-16 14:20:40 +01001872}
1873
1874
1875void LCodeGen::DoAddI(LAddI* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001876 LOperand* left = instr->left();
1877 LOperand* right = instr->right();
Ben Murdochb8e0da22011-05-16 14:20:40 +01001878
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001879 Representation target_rep = instr->hydrogen()->representation();
1880 bool is_p = target_rep.IsSmi() || target_rep.IsExternal();
1881
1882 if (LAddI::UseLea(instr->hydrogen()) && !left->Equals(instr->result())) {
1883 if (right->IsConstantOperand()) {
1884 // No support for smi-immediates for 32-bit SMI.
1885 DCHECK(SmiValuesAre32Bits() ? !target_rep.IsSmi() : SmiValuesAre31Bits());
1886 int32_t offset =
1887 ToRepresentation(LConstantOperand::cast(right),
1888 instr->hydrogen()->right()->representation());
1889 if (is_p) {
1890 __ leap(ToRegister(instr->result()),
1891 MemOperand(ToRegister(left), offset));
1892 } else {
1893 __ leal(ToRegister(instr->result()),
1894 MemOperand(ToRegister(left), offset));
1895 }
1896 } else {
1897 Operand address(ToRegister(left), ToRegister(right), times_1, 0);
1898 if (is_p) {
1899 __ leap(ToRegister(instr->result()), address);
1900 } else {
1901 __ leal(ToRegister(instr->result()), address);
1902 }
1903 }
Ben Murdochb8e0da22011-05-16 14:20:40 +01001904 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001905 if (right->IsConstantOperand()) {
1906 // No support for smi-immediates for 32-bit SMI.
1907 DCHECK(SmiValuesAre32Bits() ? !target_rep.IsSmi() : SmiValuesAre31Bits());
1908 int32_t right_operand =
1909 ToRepresentation(LConstantOperand::cast(right),
1910 instr->hydrogen()->right()->representation());
1911 if (is_p) {
1912 __ addp(ToRegister(left), Immediate(right_operand));
1913 } else {
1914 __ addl(ToRegister(left), Immediate(right_operand));
1915 }
1916 } else if (right->IsRegister()) {
1917 if (is_p) {
1918 __ addp(ToRegister(left), ToRegister(right));
1919 } else {
1920 __ addl(ToRegister(left), ToRegister(right));
1921 }
1922 } else {
1923 if (is_p) {
1924 __ addp(ToRegister(left), ToOperand(right));
1925 } else {
1926 __ addl(ToRegister(left), ToOperand(right));
1927 }
1928 }
1929 if (instr->hydrogen()->CheckFlag(HValue::kCanOverflow)) {
1930 DeoptimizeIf(overflow, instr, "overflow");
1931 }
Ben Murdochb8e0da22011-05-16 14:20:40 +01001932 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001933}
Ben Murdochb8e0da22011-05-16 14:20:40 +01001934
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001935
1936void LCodeGen::DoMathMinMax(LMathMinMax* instr) {
1937 LOperand* left = instr->left();
1938 LOperand* right = instr->right();
1939 DCHECK(left->Equals(instr->result()));
1940 HMathMinMax::Operation operation = instr->hydrogen()->operation();
1941 if (instr->hydrogen()->representation().IsSmiOrInteger32()) {
1942 Label return_left;
1943 Condition condition = (operation == HMathMinMax::kMathMin)
1944 ? less_equal
1945 : greater_equal;
1946 Register left_reg = ToRegister(left);
1947 if (right->IsConstantOperand()) {
1948 Immediate right_imm = Immediate(
1949 ToRepresentation(LConstantOperand::cast(right),
1950 instr->hydrogen()->right()->representation()));
1951 DCHECK(SmiValuesAre32Bits()
1952 ? !instr->hydrogen()->representation().IsSmi()
1953 : SmiValuesAre31Bits());
1954 __ cmpl(left_reg, right_imm);
1955 __ j(condition, &return_left, Label::kNear);
1956 __ movp(left_reg, right_imm);
1957 } else if (right->IsRegister()) {
1958 Register right_reg = ToRegister(right);
1959 if (instr->hydrogen_value()->representation().IsSmi()) {
1960 __ cmpp(left_reg, right_reg);
1961 } else {
1962 __ cmpl(left_reg, right_reg);
1963 }
1964 __ j(condition, &return_left, Label::kNear);
1965 __ movp(left_reg, right_reg);
1966 } else {
1967 Operand right_op = ToOperand(right);
1968 if (instr->hydrogen_value()->representation().IsSmi()) {
1969 __ cmpp(left_reg, right_op);
1970 } else {
1971 __ cmpl(left_reg, right_op);
1972 }
1973 __ j(condition, &return_left, Label::kNear);
1974 __ movp(left_reg, right_op);
1975 }
1976 __ bind(&return_left);
1977 } else {
1978 DCHECK(instr->hydrogen()->representation().IsDouble());
1979 Label check_nan_left, check_zero, return_left, return_right;
1980 Condition condition = (operation == HMathMinMax::kMathMin) ? below : above;
1981 XMMRegister left_reg = ToDoubleRegister(left);
1982 XMMRegister right_reg = ToDoubleRegister(right);
1983 __ ucomisd(left_reg, right_reg);
1984 __ j(parity_even, &check_nan_left, Label::kNear); // At least one NaN.
1985 __ j(equal, &check_zero, Label::kNear); // left == right.
1986 __ j(condition, &return_left, Label::kNear);
1987 __ jmp(&return_right, Label::kNear);
1988
1989 __ bind(&check_zero);
1990 XMMRegister xmm_scratch = double_scratch0();
1991 __ xorps(xmm_scratch, xmm_scratch);
1992 __ ucomisd(left_reg, xmm_scratch);
1993 __ j(not_equal, &return_left, Label::kNear); // left == right != 0.
1994 // At this point, both left and right are either 0 or -0.
1995 if (operation == HMathMinMax::kMathMin) {
1996 __ orps(left_reg, right_reg);
1997 } else {
1998 // Since we operate on +0 and/or -0, addsd and andsd have the same effect.
1999 __ addsd(left_reg, right_reg);
2000 }
2001 __ jmp(&return_left, Label::kNear);
2002
2003 __ bind(&check_nan_left);
2004 __ ucomisd(left_reg, left_reg); // NaN check.
2005 __ j(parity_even, &return_left, Label::kNear);
2006 __ bind(&return_right);
2007 __ movaps(left_reg, right_reg);
2008
2009 __ bind(&return_left);
Ben Murdochb8e0da22011-05-16 14:20:40 +01002010 }
2011}
2012
2013
2014void LCodeGen::DoArithmeticD(LArithmeticD* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002015 XMMRegister left = ToDoubleRegister(instr->left());
2016 XMMRegister right = ToDoubleRegister(instr->right());
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002017 XMMRegister result = ToDoubleRegister(instr->result());
2018 // All operations except MOD are computed in-place.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002019 DCHECK(instr->op() == Token::MOD || left.is(result));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002020 switch (instr->op()) {
2021 case Token::ADD:
2022 __ addsd(left, right);
2023 break;
2024 case Token::SUB:
2025 __ subsd(left, right);
2026 break;
2027 case Token::MUL:
2028 __ mulsd(left, right);
2029 break;
2030 case Token::DIV:
2031 __ divsd(left, right);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002032 // Don't delete this mov. It may improve performance on some CPUs,
2033 // when there is a mulsd depending on the result
2034 __ movaps(left, left);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002035 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002036 case Token::MOD: {
2037 XMMRegister xmm_scratch = double_scratch0();
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002038 __ PrepareCallCFunction(2);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002039 __ movaps(xmm_scratch, left);
2040 DCHECK(right.is(xmm1));
Steve Block44f0eee2011-05-26 01:26:41 +01002041 __ CallCFunction(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002042 ExternalReference::mod_two_doubles_operation(isolate()), 2);
2043 __ movaps(result, xmm_scratch);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002044 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002045 }
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002046 default:
2047 UNREACHABLE();
2048 break;
2049 }
Ben Murdochb8e0da22011-05-16 14:20:40 +01002050}
2051
2052
2053void LCodeGen::DoArithmeticT(LArithmeticT* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002054 DCHECK(ToRegister(instr->context()).is(rsi));
2055 DCHECK(ToRegister(instr->left()).is(rdx));
2056 DCHECK(ToRegister(instr->right()).is(rax));
2057 DCHECK(ToRegister(instr->result()).is(rax));
Ben Murdochb8e0da22011-05-16 14:20:40 +01002058
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002059 Handle<Code> code =
2060 CodeFactory::BinaryOpIC(isolate(), instr->op(), NO_OVERWRITE).code();
2061 CallCode(code, RelocInfo::CODE_TARGET, instr);
Ben Murdochb8e0da22011-05-16 14:20:40 +01002062}
2063
2064
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002065template<class InstrType>
2066void LCodeGen::EmitBranch(InstrType instr, Condition cc) {
2067 int left_block = instr->TrueDestination(chunk_);
2068 int right_block = instr->FalseDestination(chunk_);
Ben Murdochb8e0da22011-05-16 14:20:40 +01002069
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002070 int next_block = GetNextEmittedBlock();
Ben Murdochb8e0da22011-05-16 14:20:40 +01002071
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002072 if (right_block == left_block || cc == no_condition) {
Steve Block1e0659c2011-05-24 12:43:12 +01002073 EmitGoto(left_block);
2074 } else if (left_block == next_block) {
2075 __ j(NegateCondition(cc), chunk_->GetAssemblyLabel(right_block));
2076 } else if (right_block == next_block) {
2077 __ j(cc, chunk_->GetAssemblyLabel(left_block));
2078 } else {
2079 __ j(cc, chunk_->GetAssemblyLabel(left_block));
2080 if (cc != always) {
2081 __ jmp(chunk_->GetAssemblyLabel(right_block));
2082 }
2083 }
Ben Murdochb8e0da22011-05-16 14:20:40 +01002084}
2085
2086
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002087template<class InstrType>
2088void LCodeGen::EmitFalseBranch(InstrType instr, Condition cc) {
2089 int false_block = instr->FalseDestination(chunk_);
2090 __ j(cc, chunk_->GetAssemblyLabel(false_block));
2091}
Steve Block1e0659c2011-05-24 12:43:12 +01002092
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002093
2094void LCodeGen::DoDebugBreak(LDebugBreak* instr) {
2095 __ int3();
2096}
2097
2098
2099void LCodeGen::DoBranch(LBranch* instr) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002100 Representation r = instr->hydrogen()->value()->representation();
Steve Block1e0659c2011-05-24 12:43:12 +01002101 if (r.IsInteger32()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002102 DCHECK(!info()->IsStub());
2103 Register reg = ToRegister(instr->value());
Steve Block1e0659c2011-05-24 12:43:12 +01002104 __ testl(reg, reg);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002105 EmitBranch(instr, not_zero);
2106 } else if (r.IsSmi()) {
2107 DCHECK(!info()->IsStub());
2108 Register reg = ToRegister(instr->value());
2109 __ testp(reg, reg);
2110 EmitBranch(instr, not_zero);
Steve Block1e0659c2011-05-24 12:43:12 +01002111 } else if (r.IsDouble()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002112 DCHECK(!info()->IsStub());
2113 XMMRegister reg = ToDoubleRegister(instr->value());
2114 XMMRegister xmm_scratch = double_scratch0();
2115 __ xorps(xmm_scratch, xmm_scratch);
2116 __ ucomisd(reg, xmm_scratch);
2117 EmitBranch(instr, not_equal);
Steve Block1e0659c2011-05-24 12:43:12 +01002118 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002119 DCHECK(r.IsTagged());
2120 Register reg = ToRegister(instr->value());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002121 HType type = instr->hydrogen()->value()->type();
Steve Block1e0659c2011-05-24 12:43:12 +01002122 if (type.IsBoolean()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002123 DCHECK(!info()->IsStub());
Steve Block44f0eee2011-05-26 01:26:41 +01002124 __ CompareRoot(reg, Heap::kTrueValueRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002125 EmitBranch(instr, equal);
Steve Block1e0659c2011-05-24 12:43:12 +01002126 } else if (type.IsSmi()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002127 DCHECK(!info()->IsStub());
Steve Block1e0659c2011-05-24 12:43:12 +01002128 __ SmiCompare(reg, Smi::FromInt(0));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002129 EmitBranch(instr, not_equal);
2130 } else if (type.IsJSArray()) {
2131 DCHECK(!info()->IsStub());
2132 EmitBranch(instr, no_condition);
2133 } else if (type.IsHeapNumber()) {
2134 DCHECK(!info()->IsStub());
2135 XMMRegister xmm_scratch = double_scratch0();
2136 __ xorps(xmm_scratch, xmm_scratch);
2137 __ ucomisd(xmm_scratch, FieldOperand(reg, HeapNumber::kValueOffset));
2138 EmitBranch(instr, not_equal);
2139 } else if (type.IsString()) {
2140 DCHECK(!info()->IsStub());
2141 __ cmpp(FieldOperand(reg, String::kLengthOffset), Immediate(0));
2142 EmitBranch(instr, not_equal);
Steve Block1e0659c2011-05-24 12:43:12 +01002143 } else {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002144 ToBooleanStub::Types expected = instr->hydrogen()->expected_input_types();
2145 // Avoid deopts in the case where we've never executed this path before.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002146 if (expected.IsEmpty()) expected = ToBooleanStub::Types::Generic();
Steve Block1e0659c2011-05-24 12:43:12 +01002147
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002148 if (expected.Contains(ToBooleanStub::UNDEFINED)) {
2149 // undefined -> false.
2150 __ CompareRoot(reg, Heap::kUndefinedValueRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002151 __ j(equal, instr->FalseLabel(chunk_));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002152 }
2153 if (expected.Contains(ToBooleanStub::BOOLEAN)) {
2154 // true -> true.
2155 __ CompareRoot(reg, Heap::kTrueValueRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002156 __ j(equal, instr->TrueLabel(chunk_));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002157 // false -> false.
2158 __ CompareRoot(reg, Heap::kFalseValueRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002159 __ j(equal, instr->FalseLabel(chunk_));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002160 }
2161 if (expected.Contains(ToBooleanStub::NULL_TYPE)) {
2162 // 'null' -> false.
2163 __ CompareRoot(reg, Heap::kNullValueRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002164 __ j(equal, instr->FalseLabel(chunk_));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002165 }
Steve Block1e0659c2011-05-24 12:43:12 +01002166
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002167 if (expected.Contains(ToBooleanStub::SMI)) {
2168 // Smis: 0 -> false, all other -> true.
2169 __ Cmp(reg, Smi::FromInt(0));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002170 __ j(equal, instr->FalseLabel(chunk_));
2171 __ JumpIfSmi(reg, instr->TrueLabel(chunk_));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002172 } else if (expected.NeedsMap()) {
2173 // If we need a map later and have a Smi -> deopt.
2174 __ testb(reg, Immediate(kSmiTagMask));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002175 DeoptimizeIf(zero, instr, "Smi");
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002176 }
Steve Block1e0659c2011-05-24 12:43:12 +01002177
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002178 const Register map = kScratchRegister;
2179 if (expected.NeedsMap()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002180 __ movp(map, FieldOperand(reg, HeapObject::kMapOffset));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002181
2182 if (expected.CanBeUndetectable()) {
2183 // Undetectable -> false.
2184 __ testb(FieldOperand(map, Map::kBitFieldOffset),
2185 Immediate(1 << Map::kIsUndetectable));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002186 __ j(not_zero, instr->FalseLabel(chunk_));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002187 }
2188 }
2189
2190 if (expected.Contains(ToBooleanStub::SPEC_OBJECT)) {
2191 // spec object -> true.
2192 __ CmpInstanceType(map, FIRST_SPEC_OBJECT_TYPE);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002193 __ j(above_equal, instr->TrueLabel(chunk_));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002194 }
2195
2196 if (expected.Contains(ToBooleanStub::STRING)) {
2197 // String value -> false iff empty.
2198 Label not_string;
2199 __ CmpInstanceType(map, FIRST_NONSTRING_TYPE);
2200 __ j(above_equal, &not_string, Label::kNear);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002201 __ cmpp(FieldOperand(reg, String::kLengthOffset), Immediate(0));
2202 __ j(not_zero, instr->TrueLabel(chunk_));
2203 __ jmp(instr->FalseLabel(chunk_));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002204 __ bind(&not_string);
2205 }
2206
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002207 if (expected.Contains(ToBooleanStub::SYMBOL)) {
2208 // Symbol value -> true.
2209 __ CmpInstanceType(map, SYMBOL_TYPE);
2210 __ j(equal, instr->TrueLabel(chunk_));
2211 }
2212
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002213 if (expected.Contains(ToBooleanStub::HEAP_NUMBER)) {
2214 // heap number -> false iff +0, -0, or NaN.
2215 Label not_heap_number;
2216 __ CompareRoot(map, Heap::kHeapNumberMapRootIndex);
2217 __ j(not_equal, &not_heap_number, Label::kNear);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002218 XMMRegister xmm_scratch = double_scratch0();
2219 __ xorps(xmm_scratch, xmm_scratch);
2220 __ ucomisd(xmm_scratch, FieldOperand(reg, HeapNumber::kValueOffset));
2221 __ j(zero, instr->FalseLabel(chunk_));
2222 __ jmp(instr->TrueLabel(chunk_));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002223 __ bind(&not_heap_number);
2224 }
2225
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002226 if (!expected.IsGeneric()) {
2227 // We've seen something for the first time -> deopt.
2228 // This can only happen if we are not generic already.
2229 DeoptimizeIf(no_condition, instr, "unexpected object");
2230 }
Steve Block1e0659c2011-05-24 12:43:12 +01002231 }
2232 }
Ben Murdochb8e0da22011-05-16 14:20:40 +01002233}
2234
2235
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002236void LCodeGen::EmitGoto(int block) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002237 if (!IsNextEmittedBlock(block)) {
2238 __ jmp(chunk_->GetAssemblyLabel(chunk_->LookupDestination(block)));
Ben Murdochb8e0da22011-05-16 14:20:40 +01002239 }
2240}
2241
2242
Ben Murdochb8e0da22011-05-16 14:20:40 +01002243void LCodeGen::DoGoto(LGoto* instr) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002244 EmitGoto(instr->block_id());
Ben Murdochb8e0da22011-05-16 14:20:40 +01002245}
2246
2247
Steve Block1e0659c2011-05-24 12:43:12 +01002248inline Condition LCodeGen::TokenToCondition(Token::Value op, bool is_unsigned) {
Ben Murdochb8e0da22011-05-16 14:20:40 +01002249 Condition cond = no_condition;
2250 switch (op) {
2251 case Token::EQ:
2252 case Token::EQ_STRICT:
2253 cond = equal;
2254 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002255 case Token::NE:
2256 case Token::NE_STRICT:
2257 cond = not_equal;
2258 break;
Ben Murdochb8e0da22011-05-16 14:20:40 +01002259 case Token::LT:
2260 cond = is_unsigned ? below : less;
2261 break;
2262 case Token::GT:
2263 cond = is_unsigned ? above : greater;
2264 break;
2265 case Token::LTE:
2266 cond = is_unsigned ? below_equal : less_equal;
2267 break;
2268 case Token::GTE:
2269 cond = is_unsigned ? above_equal : greater_equal;
2270 break;
2271 case Token::IN:
2272 case Token::INSTANCEOF:
2273 default:
2274 UNREACHABLE();
2275 }
2276 return cond;
2277}
2278
2279
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002280void LCodeGen::DoCompareNumericAndBranch(LCompareNumericAndBranch* instr) {
2281 LOperand* left = instr->left();
2282 LOperand* right = instr->right();
2283 bool is_unsigned =
2284 instr->is_double() ||
2285 instr->hydrogen()->left()->CheckFlag(HInstruction::kUint32) ||
2286 instr->hydrogen()->right()->CheckFlag(HInstruction::kUint32);
2287 Condition cc = TokenToCondition(instr->op(), is_unsigned);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002288
2289 if (left->IsConstantOperand() && right->IsConstantOperand()) {
2290 // We can statically evaluate the comparison.
2291 double left_val = ToDouble(LConstantOperand::cast(left));
2292 double right_val = ToDouble(LConstantOperand::cast(right));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002293 int next_block = EvalComparison(instr->op(), left_val, right_val) ?
2294 instr->TrueDestination(chunk_) : instr->FalseDestination(chunk_);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002295 EmitGoto(next_block);
2296 } else {
2297 if (instr->is_double()) {
2298 // Don't base result on EFLAGS when a NaN is involved. Instead
2299 // jump to the false block.
2300 __ ucomisd(ToDoubleRegister(left), ToDoubleRegister(right));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002301 __ j(parity_even, instr->FalseLabel(chunk_));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002302 } else {
2303 int32_t value;
2304 if (right->IsConstantOperand()) {
2305 value = ToInteger32(LConstantOperand::cast(right));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002306 if (instr->hydrogen_value()->representation().IsSmi()) {
2307 __ Cmp(ToRegister(left), Smi::FromInt(value));
2308 } else {
2309 __ cmpl(ToRegister(left), Immediate(value));
2310 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002311 } else if (left->IsConstantOperand()) {
2312 value = ToInteger32(LConstantOperand::cast(left));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002313 if (instr->hydrogen_value()->representation().IsSmi()) {
2314 if (right->IsRegister()) {
2315 __ Cmp(ToRegister(right), Smi::FromInt(value));
2316 } else {
2317 __ Cmp(ToOperand(right), Smi::FromInt(value));
2318 }
2319 } else if (right->IsRegister()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002320 __ cmpl(ToRegister(right), Immediate(value));
2321 } else {
2322 __ cmpl(ToOperand(right), Immediate(value));
2323 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002324 // We commuted the operands, so commute the condition.
2325 cc = CommuteCondition(cc);
2326 } else if (instr->hydrogen_value()->representation().IsSmi()) {
2327 if (right->IsRegister()) {
2328 __ cmpp(ToRegister(left), ToRegister(right));
2329 } else {
2330 __ cmpp(ToRegister(left), ToOperand(right));
2331 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002332 } else {
2333 if (right->IsRegister()) {
2334 __ cmpl(ToRegister(left), ToRegister(right));
2335 } else {
2336 __ cmpl(ToRegister(left), ToOperand(right));
2337 }
2338 }
2339 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002340 EmitBranch(instr, cc);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002341 }
Ben Murdochb8e0da22011-05-16 14:20:40 +01002342}
2343
2344
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002345void LCodeGen::DoCmpObjectEqAndBranch(LCmpObjectEqAndBranch* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002346 Register left = ToRegister(instr->left());
Steve Block1e0659c2011-05-24 12:43:12 +01002347
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002348 if (instr->right()->IsConstantOperand()) {
2349 Handle<Object> right = ToHandle(LConstantOperand::cast(instr->right()));
2350 __ Cmp(left, right);
2351 } else {
2352 Register right = ToRegister(instr->right());
2353 __ cmpp(left, right);
2354 }
2355 EmitBranch(instr, equal);
Ben Murdochb8e0da22011-05-16 14:20:40 +01002356}
2357
2358
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002359void LCodeGen::DoCmpHoleAndBranch(LCmpHoleAndBranch* instr) {
2360 if (instr->hydrogen()->representation().IsTagged()) {
2361 Register input_reg = ToRegister(instr->object());
2362 __ Cmp(input_reg, factory()->the_hole_value());
2363 EmitBranch(instr, equal);
Steve Block1e0659c2011-05-24 12:43:12 +01002364 return;
2365 }
2366
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002367 XMMRegister input_reg = ToDoubleRegister(instr->object());
2368 __ ucomisd(input_reg, input_reg);
2369 EmitFalseBranch(instr, parity_odd);
2370
2371 __ subp(rsp, Immediate(kDoubleSize));
2372 __ movsd(MemOperand(rsp, 0), input_reg);
2373 __ addp(rsp, Immediate(kDoubleSize));
2374
2375 int offset = sizeof(kHoleNanUpper32);
2376 __ cmpl(MemOperand(rsp, -offset), Immediate(kHoleNanUpper32));
2377 EmitBranch(instr, equal);
2378}
2379
2380
2381void LCodeGen::DoCompareMinusZeroAndBranch(LCompareMinusZeroAndBranch* instr) {
2382 Representation rep = instr->hydrogen()->value()->representation();
2383 DCHECK(!rep.IsInteger32());
2384
2385 if (rep.IsDouble()) {
2386 XMMRegister value = ToDoubleRegister(instr->value());
2387 XMMRegister xmm_scratch = double_scratch0();
2388 __ xorps(xmm_scratch, xmm_scratch);
2389 __ ucomisd(xmm_scratch, value);
2390 EmitFalseBranch(instr, not_equal);
2391 __ movmskpd(kScratchRegister, value);
2392 __ testl(kScratchRegister, Immediate(1));
2393 EmitBranch(instr, not_zero);
Steve Block1e0659c2011-05-24 12:43:12 +01002394 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002395 Register value = ToRegister(instr->value());
2396 Handle<Map> map = masm()->isolate()->factory()->heap_number_map();
2397 __ CheckMap(value, map, instr->FalseLabel(chunk()), DO_SMI_CHECK);
2398 __ cmpl(FieldOperand(value, HeapNumber::kExponentOffset),
2399 Immediate(0x1));
2400 EmitFalseBranch(instr, no_overflow);
2401 __ cmpl(FieldOperand(value, HeapNumber::kMantissaOffset),
2402 Immediate(0x00000000));
2403 EmitBranch(instr, equal);
Steve Block1e0659c2011-05-24 12:43:12 +01002404 }
Ben Murdochb8e0da22011-05-16 14:20:40 +01002405}
2406
2407
2408Condition LCodeGen::EmitIsObject(Register input,
Ben Murdochb8e0da22011-05-16 14:20:40 +01002409 Label* is_not_object,
2410 Label* is_object) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002411 DCHECK(!input.is(kScratchRegister));
Steve Block1e0659c2011-05-24 12:43:12 +01002412
2413 __ JumpIfSmi(input, is_not_object);
2414
2415 __ CompareRoot(input, Heap::kNullValueRootIndex);
2416 __ j(equal, is_object);
2417
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002418 __ movp(kScratchRegister, FieldOperand(input, HeapObject::kMapOffset));
Steve Block1e0659c2011-05-24 12:43:12 +01002419 // Undetectable objects behave like undefined.
2420 __ testb(FieldOperand(kScratchRegister, Map::kBitFieldOffset),
2421 Immediate(1 << Map::kIsUndetectable));
2422 __ j(not_zero, is_not_object);
2423
2424 __ movzxbl(kScratchRegister,
2425 FieldOperand(kScratchRegister, Map::kInstanceTypeOffset));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002426 __ cmpb(kScratchRegister, Immediate(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
Steve Block1e0659c2011-05-24 12:43:12 +01002427 __ j(below, is_not_object);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002428 __ cmpb(kScratchRegister, Immediate(LAST_NONCALLABLE_SPEC_OBJECT_TYPE));
Ben Murdochb8e0da22011-05-16 14:20:40 +01002429 return below_equal;
2430}
2431
2432
Ben Murdochb8e0da22011-05-16 14:20:40 +01002433void LCodeGen::DoIsObjectAndBranch(LIsObjectAndBranch* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002434 Register reg = ToRegister(instr->value());
Steve Block1e0659c2011-05-24 12:43:12 +01002435
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002436 Condition true_cond = EmitIsObject(
2437 reg, instr->FalseLabel(chunk_), instr->TrueLabel(chunk_));
Steve Block1e0659c2011-05-24 12:43:12 +01002438
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002439 EmitBranch(instr, true_cond);
Ben Murdochb8e0da22011-05-16 14:20:40 +01002440}
2441
2442
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002443Condition LCodeGen::EmitIsString(Register input,
2444 Register temp1,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002445 Label* is_not_string,
2446 SmiCheck check_needed = INLINE_SMI_CHECK) {
2447 if (check_needed == INLINE_SMI_CHECK) {
2448 __ JumpIfSmi(input, is_not_string);
2449 }
2450
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002451 Condition cond = masm_->IsObjectStringType(input, temp1, temp1);
2452
2453 return cond;
2454}
2455
2456
2457void LCodeGen::DoIsStringAndBranch(LIsStringAndBranch* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002458 Register reg = ToRegister(instr->value());
2459 Register temp = ToRegister(instr->temp());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002460
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002461 SmiCheck check_needed =
2462 instr->hydrogen()->value()->type().IsHeapObject()
2463 ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002464
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002465 Condition true_cond = EmitIsString(
2466 reg, temp, instr->FalseLabel(chunk_), check_needed);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002467
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002468 EmitBranch(instr, true_cond);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002469}
2470
2471
Ben Murdochb8e0da22011-05-16 14:20:40 +01002472void LCodeGen::DoIsSmiAndBranch(LIsSmiAndBranch* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01002473 Condition is_smi;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002474 if (instr->value()->IsRegister()) {
2475 Register input = ToRegister(instr->value());
Steve Block1e0659c2011-05-24 12:43:12 +01002476 is_smi = masm()->CheckSmi(input);
2477 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002478 Operand input = ToOperand(instr->value());
Steve Block1e0659c2011-05-24 12:43:12 +01002479 is_smi = masm()->CheckSmi(input);
2480 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002481 EmitBranch(instr, is_smi);
Steve Block1e0659c2011-05-24 12:43:12 +01002482}
2483
2484
Ben Murdoch257744e2011-11-30 15:57:28 +00002485void LCodeGen::DoIsUndetectableAndBranch(LIsUndetectableAndBranch* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002486 Register input = ToRegister(instr->value());
2487 Register temp = ToRegister(instr->temp());
Ben Murdoch257744e2011-11-30 15:57:28 +00002488
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002489 if (!instr->hydrogen()->value()->type().IsHeapObject()) {
2490 __ JumpIfSmi(input, instr->FalseLabel(chunk_));
2491 }
2492 __ movp(temp, FieldOperand(input, HeapObject::kMapOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +00002493 __ testb(FieldOperand(temp, Map::kBitFieldOffset),
2494 Immediate(1 << Map::kIsUndetectable));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002495 EmitBranch(instr, not_zero);
Ben Murdoch257744e2011-11-30 15:57:28 +00002496}
2497
2498
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002499void LCodeGen::DoStringCompareAndBranch(LStringCompareAndBranch* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002500 DCHECK(ToRegister(instr->context()).is(rsi));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002501 Token::Value op = instr->op();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002502
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002503 Handle<Code> ic = CodeFactory::CompareIC(isolate(), op).code();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002504 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2505
2506 Condition condition = TokenToCondition(op, false);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002507 __ testp(rax, rax);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002508
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002509 EmitBranch(instr, condition);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002510}
2511
2512
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002513static InstanceType TestType(HHasInstanceTypeAndBranch* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01002514 InstanceType from = instr->from();
2515 InstanceType to = instr->to();
2516 if (from == FIRST_TYPE) return to;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002517 DCHECK(from == to || to == LAST_TYPE);
Steve Block1e0659c2011-05-24 12:43:12 +01002518 return from;
2519}
2520
2521
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002522static Condition BranchCondition(HHasInstanceTypeAndBranch* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01002523 InstanceType from = instr->from();
2524 InstanceType to = instr->to();
2525 if (from == to) return equal;
2526 if (to == LAST_TYPE) return above_equal;
2527 if (from == FIRST_TYPE) return below_equal;
2528 UNREACHABLE();
2529 return equal;
Ben Murdochb8e0da22011-05-16 14:20:40 +01002530}
2531
2532
Ben Murdochb8e0da22011-05-16 14:20:40 +01002533void LCodeGen::DoHasInstanceTypeAndBranch(LHasInstanceTypeAndBranch* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002534 Register input = ToRegister(instr->value());
Steve Block1e0659c2011-05-24 12:43:12 +01002535
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002536 if (!instr->hydrogen()->value()->type().IsHeapObject()) {
2537 __ JumpIfSmi(input, instr->FalseLabel(chunk_));
2538 }
Steve Block1e0659c2011-05-24 12:43:12 +01002539
2540 __ CmpObjectType(input, TestType(instr->hydrogen()), kScratchRegister);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002541 EmitBranch(instr, BranchCondition(instr->hydrogen()));
Ben Murdochb8e0da22011-05-16 14:20:40 +01002542}
2543
2544
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002545void LCodeGen::DoGetCachedArrayIndex(LGetCachedArrayIndex* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002546 Register input = ToRegister(instr->value());
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002547 Register result = ToRegister(instr->result());
2548
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002549 __ AssertString(input);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002550
2551 __ movl(result, FieldOperand(input, String::kHashFieldOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002552 DCHECK(String::kHashShift >= kSmiTagSize);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002553 __ IndexFromHash(result, result);
2554}
2555
2556
Ben Murdochb8e0da22011-05-16 14:20:40 +01002557void LCodeGen::DoHasCachedArrayIndexAndBranch(
2558 LHasCachedArrayIndexAndBranch* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002559 Register input = ToRegister(instr->value());
Steve Block1e0659c2011-05-24 12:43:12 +01002560
2561 __ testl(FieldOperand(input, String::kHashFieldOffset),
2562 Immediate(String::kContainsCachedArrayIndexMask));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002563 EmitBranch(instr, equal);
Ben Murdochb8e0da22011-05-16 14:20:40 +01002564}
2565
2566
Steve Block1e0659c2011-05-24 12:43:12 +01002567// Branches to a label or falls through with the answer in the z flag.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002568// Trashes the temp register.
Ben Murdochb8e0da22011-05-16 14:20:40 +01002569void LCodeGen::EmitClassOfTest(Label* is_true,
2570 Label* is_false,
Steve Block1e0659c2011-05-24 12:43:12 +01002571 Handle<String> class_name,
Ben Murdochb8e0da22011-05-16 14:20:40 +01002572 Register input,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002573 Register temp,
2574 Register temp2) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002575 DCHECK(!input.is(temp));
2576 DCHECK(!input.is(temp2));
2577 DCHECK(!temp.is(temp2));
Steve Block1e0659c2011-05-24 12:43:12 +01002578
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002579 __ JumpIfSmi(input, is_false);
2580
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002581 if (String::Equals(isolate()->factory()->Function_string(), class_name)) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002582 // Assuming the following assertions, we can use the same compares to test
2583 // for both being a function type and being in the object type range.
2584 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
2585 STATIC_ASSERT(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2586 FIRST_SPEC_OBJECT_TYPE + 1);
2587 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2588 LAST_SPEC_OBJECT_TYPE - 1);
2589 STATIC_ASSERT(LAST_SPEC_OBJECT_TYPE == LAST_TYPE);
2590 __ CmpObjectType(input, FIRST_SPEC_OBJECT_TYPE, temp);
2591 __ j(below, is_false);
2592 __ j(equal, is_true);
2593 __ CmpInstanceType(temp, LAST_SPEC_OBJECT_TYPE);
2594 __ j(equal, is_true);
Steve Block1e0659c2011-05-24 12:43:12 +01002595 } else {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002596 // Faster code path to avoid two compares: subtract lower bound from the
2597 // actual type and do a signed compare with the width of the type range.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002598 __ movp(temp, FieldOperand(input, HeapObject::kMapOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002599 __ movzxbl(temp2, FieldOperand(temp, Map::kInstanceTypeOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002600 __ subp(temp2, Immediate(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
2601 __ cmpp(temp2, Immediate(LAST_NONCALLABLE_SPEC_OBJECT_TYPE -
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002602 FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
2603 __ j(above, is_false);
Steve Block1e0659c2011-05-24 12:43:12 +01002604 }
2605
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002606 // Now we are in the FIRST-LAST_NONCALLABLE_SPEC_OBJECT_TYPE range.
Steve Block1e0659c2011-05-24 12:43:12 +01002607 // Check if the constructor in the map is a function.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002608 __ movp(temp, FieldOperand(temp, Map::kConstructorOffset));
Steve Block1e0659c2011-05-24 12:43:12 +01002609
Steve Block1e0659c2011-05-24 12:43:12 +01002610 // Objects with a non-function constructor have class 'Object'.
2611 __ CmpObjectType(temp, JS_FUNCTION_TYPE, kScratchRegister);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002612 if (String::Equals(class_name, isolate()->factory()->Object_string())) {
Steve Block1e0659c2011-05-24 12:43:12 +01002613 __ j(not_equal, is_true);
2614 } else {
2615 __ j(not_equal, is_false);
2616 }
2617
2618 // temp now contains the constructor function. Grab the
2619 // instance class name from there.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002620 __ movp(temp, FieldOperand(temp, JSFunction::kSharedFunctionInfoOffset));
2621 __ movp(temp, FieldOperand(temp,
Steve Block1e0659c2011-05-24 12:43:12 +01002622 SharedFunctionInfo::kInstanceClassNameOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002623 // The class name we are testing against is internalized since it's a literal.
2624 // The name in the constructor is internalized because of the way the context
2625 // is booted. This routine isn't expected to work for random API-created
Steve Block1e0659c2011-05-24 12:43:12 +01002626 // classes and it doesn't have to because you can't access it with natives
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002627 // syntax. Since both sides are internalized it is sufficient to use an
2628 // identity comparison.
2629 DCHECK(class_name->IsInternalizedString());
Steve Block1e0659c2011-05-24 12:43:12 +01002630 __ Cmp(temp, class_name);
2631 // End with the answer in the z flag.
Ben Murdochb8e0da22011-05-16 14:20:40 +01002632}
2633
2634
Ben Murdochb8e0da22011-05-16 14:20:40 +01002635void LCodeGen::DoClassOfTestAndBranch(LClassOfTestAndBranch* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002636 Register input = ToRegister(instr->value());
2637 Register temp = ToRegister(instr->temp());
2638 Register temp2 = ToRegister(instr->temp2());
Steve Block1e0659c2011-05-24 12:43:12 +01002639 Handle<String> class_name = instr->hydrogen()->class_name();
2640
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002641 EmitClassOfTest(instr->TrueLabel(chunk_), instr->FalseLabel(chunk_),
2642 class_name, input, temp, temp2);
Steve Block1e0659c2011-05-24 12:43:12 +01002643
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002644 EmitBranch(instr, equal);
Ben Murdochb8e0da22011-05-16 14:20:40 +01002645}
2646
2647
2648void LCodeGen::DoCmpMapAndBranch(LCmpMapAndBranch* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002649 Register reg = ToRegister(instr->value());
Steve Block1e0659c2011-05-24 12:43:12 +01002650
2651 __ Cmp(FieldOperand(reg, HeapObject::kMapOffset), instr->map());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002652 EmitBranch(instr, equal);
Ben Murdochb8e0da22011-05-16 14:20:40 +01002653}
2654
2655
2656void LCodeGen::DoInstanceOf(LInstanceOf* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002657 DCHECK(ToRegister(instr->context()).is(rsi));
2658 InstanceofStub stub(isolate(), InstanceofStub::kNoFlags);
2659 __ Push(ToRegister(instr->left()));
2660 __ Push(ToRegister(instr->right()));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002661 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
Ben Murdoch257744e2011-11-30 15:57:28 +00002662 Label true_value, done;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002663 __ testp(rax, rax);
Ben Murdoch257744e2011-11-30 15:57:28 +00002664 __ j(zero, &true_value, Label::kNear);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002665 __ LoadRoot(ToRegister(instr->result()), Heap::kFalseValueRootIndex);
Ben Murdoch257744e2011-11-30 15:57:28 +00002666 __ jmp(&done, Label::kNear);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002667 __ bind(&true_value);
2668 __ LoadRoot(ToRegister(instr->result()), Heap::kTrueValueRootIndex);
2669 __ bind(&done);
Ben Murdochb8e0da22011-05-16 14:20:40 +01002670}
2671
2672
Ben Murdochb8e0da22011-05-16 14:20:40 +01002673void LCodeGen::DoInstanceOfKnownGlobal(LInstanceOfKnownGlobal* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002674 class DeferredInstanceOfKnownGlobal FINAL : public LDeferredCode {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002675 public:
2676 DeferredInstanceOfKnownGlobal(LCodeGen* codegen,
2677 LInstanceOfKnownGlobal* instr)
2678 : LDeferredCode(codegen), instr_(instr) { }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002679 void Generate() OVERRIDE {
Ben Murdoch2b4ba112012-01-20 14:57:15 +00002680 codegen()->DoDeferredInstanceOfKnownGlobal(instr_, &map_check_);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002681 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002682 LInstruction* instr() OVERRIDE { return instr_; }
Steve Block44f0eee2011-05-26 01:26:41 +01002683 Label* map_check() { return &map_check_; }
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002684 private:
2685 LInstanceOfKnownGlobal* instr_;
Steve Block44f0eee2011-05-26 01:26:41 +01002686 Label map_check_;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002687 };
2688
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002689 DCHECK(ToRegister(instr->context()).is(rsi));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002690 DeferredInstanceOfKnownGlobal* deferred;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002691 deferred = new(zone()) DeferredInstanceOfKnownGlobal(this, instr);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002692
Steve Block44f0eee2011-05-26 01:26:41 +01002693 Label done, false_result;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002694 Register object = ToRegister(instr->value());
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002695
2696 // A Smi is not an instance of anything.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002697 __ JumpIfSmi(object, &false_result, Label::kNear);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002698
Steve Block44f0eee2011-05-26 01:26:41 +01002699 // This is the inlined call site instanceof cache. The two occurences of the
2700 // hole value will be patched to the last map/result pair generated by the
2701 // instanceof stub.
Ben Murdoch257744e2011-11-30 15:57:28 +00002702 Label cache_miss;
Steve Block44f0eee2011-05-26 01:26:41 +01002703 // Use a temp register to avoid memory operands with variable lengths.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002704 Register map = ToRegister(instr->temp());
2705 __ movp(map, FieldOperand(object, HeapObject::kMapOffset));
Steve Block44f0eee2011-05-26 01:26:41 +01002706 __ bind(deferred->map_check()); // Label for calculating code patching.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002707 Handle<Cell> cache_cell = factory()->NewCell(factory()->the_hole_value());
2708 __ Move(kScratchRegister, cache_cell, RelocInfo::CELL);
2709 __ cmpp(map, Operand(kScratchRegister, 0));
Ben Murdoch257744e2011-11-30 15:57:28 +00002710 __ j(not_equal, &cache_miss, Label::kNear);
Steve Block44f0eee2011-05-26 01:26:41 +01002711 // Patched to load either true or false.
2712 __ LoadRoot(ToRegister(instr->result()), Heap::kTheHoleValueRootIndex);
2713#ifdef DEBUG
2714 // Check that the code size between patch label and patch sites is invariant.
2715 Label end_of_patched_code;
2716 __ bind(&end_of_patched_code);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002717 DCHECK(true);
Steve Block44f0eee2011-05-26 01:26:41 +01002718#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002719 __ jmp(&done, Label::kNear);
Steve Block44f0eee2011-05-26 01:26:41 +01002720
2721 // The inlined call site cache did not match. Check for null and string
2722 // before calling the deferred code.
2723 __ bind(&cache_miss); // Null is not an instance of anything.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002724 __ CompareRoot(object, Heap::kNullValueRootIndex);
Ben Murdoch257744e2011-11-30 15:57:28 +00002725 __ j(equal, &false_result, Label::kNear);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002726
2727 // String values are not instances of anything.
2728 __ JumpIfNotString(object, kScratchRegister, deferred->entry());
2729
2730 __ bind(&false_result);
2731 __ LoadRoot(ToRegister(instr->result()), Heap::kFalseValueRootIndex);
2732
2733 __ bind(deferred->exit());
Steve Block44f0eee2011-05-26 01:26:41 +01002734 __ bind(&done);
Ben Murdochb8e0da22011-05-16 14:20:40 +01002735}
2736
2737
Ben Murdoch2b4ba112012-01-20 14:57:15 +00002738void LCodeGen::DoDeferredInstanceOfKnownGlobal(LInstanceOfKnownGlobal* instr,
2739 Label* map_check) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01002740 {
2741 PushSafepointRegistersScope scope(this);
2742 InstanceofStub::Flags flags = static_cast<InstanceofStub::Flags>(
2743 InstanceofStub::kNoFlags | InstanceofStub::kCallSiteInlineCheck);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002744 InstanceofStub stub(isolate(), flags);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002745
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002746 __ Push(ToRegister(instr->value()));
2747 __ Push(instr->function());
Ben Murdoch8b112d22011-06-08 16:22:53 +01002748
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002749 static const int kAdditionalDelta = kPointerSize == kInt64Size ? 10 : 16;
Ben Murdoch8b112d22011-06-08 16:22:53 +01002750 int delta =
2751 masm_->SizeOfCodeGeneratedSince(map_check) + kAdditionalDelta;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002752 DCHECK(delta >= 0);
2753 __ PushImm32(delta);
Ben Murdoch8b112d22011-06-08 16:22:53 +01002754
2755 // We are pushing three values on the stack but recording a
2756 // safepoint with two arguments because stub is going to
2757 // remove the third argument from the stack before jumping
2758 // to instanceof builtin on the slow path.
2759 CallCodeGeneric(stub.GetCode(),
2760 RelocInfo::CODE_TARGET,
2761 instr,
2762 RECORD_SAFEPOINT_WITH_REGISTERS,
2763 2);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002764 DCHECK(delta == masm_->SizeOfCodeGeneratedSince(map_check));
2765 LEnvironment* env = instr->GetDeferredLazyDeoptimizationEnvironment();
Ben Murdoch2b4ba112012-01-20 14:57:15 +00002766 safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
Ben Murdoch8b112d22011-06-08 16:22:53 +01002767 // Move result to a register that survives the end of the
2768 // PushSafepointRegisterScope.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002769 __ movp(kScratchRegister, rax);
Ben Murdoch8b112d22011-06-08 16:22:53 +01002770 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002771 __ testp(kScratchRegister, kScratchRegister);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002772 Label load_false;
2773 Label done;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002774 __ j(not_zero, &load_false, Label::kNear);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002775 __ LoadRoot(rax, Heap::kTrueValueRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002776 __ jmp(&done, Label::kNear);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002777 __ bind(&load_false);
2778 __ LoadRoot(rax, Heap::kFalseValueRootIndex);
2779 __ bind(&done);
Ben Murdochb8e0da22011-05-16 14:20:40 +01002780}
2781
2782
2783void LCodeGen::DoCmpT(LCmpT* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002784 DCHECK(ToRegister(instr->context()).is(rsi));
Steve Block1e0659c2011-05-24 12:43:12 +01002785 Token::Value op = instr->op();
2786
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002787 Handle<Code> ic = CodeFactory::CompareIC(isolate(), op).code();
Steve Block1e0659c2011-05-24 12:43:12 +01002788 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2789
2790 Condition condition = TokenToCondition(op, false);
Ben Murdoch257744e2011-11-30 15:57:28 +00002791 Label true_value, done;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002792 __ testp(rax, rax);
Ben Murdoch257744e2011-11-30 15:57:28 +00002793 __ j(condition, &true_value, Label::kNear);
Steve Block1e0659c2011-05-24 12:43:12 +01002794 __ LoadRoot(ToRegister(instr->result()), Heap::kFalseValueRootIndex);
Ben Murdoch257744e2011-11-30 15:57:28 +00002795 __ jmp(&done, Label::kNear);
Steve Block1e0659c2011-05-24 12:43:12 +01002796 __ bind(&true_value);
2797 __ LoadRoot(ToRegister(instr->result()), Heap::kTrueValueRootIndex);
2798 __ bind(&done);
Ben Murdochb8e0da22011-05-16 14:20:40 +01002799}
2800
2801
Ben Murdochb8e0da22011-05-16 14:20:40 +01002802void LCodeGen::DoReturn(LReturn* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002803 if (FLAG_trace && info()->IsOptimizing()) {
2804 // Preserve the return value on the stack and rely on the runtime call
2805 // to return the value in the same register. We're leaving the code
2806 // managed by the register allocator and tearing down the frame, it's
2807 // safe to write to the context register.
2808 __ Push(rax);
2809 __ movp(rsi, Operand(rbp, StandardFrameConstants::kContextOffset));
Ben Murdochb8e0da22011-05-16 14:20:40 +01002810 __ CallRuntime(Runtime::kTraceExit, 1);
2811 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002812 if (info()->saves_caller_doubles()) {
2813 RestoreCallerDoubles();
2814 }
2815 int no_frame_start = -1;
2816 if (NeedsEagerFrame()) {
2817 __ movp(rsp, rbp);
2818 __ popq(rbp);
2819 no_frame_start = masm_->pc_offset();
2820 }
2821 if (instr->has_constant_parameter_count()) {
2822 __ Ret((ToInteger32(instr->constant_parameter_count()) + 1) * kPointerSize,
2823 rcx);
2824 } else {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002825 DCHECK(info()->IsStub()); // Functions would need to drop one more value.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002826 Register reg = ToRegister(instr->parameter_count());
2827 // The argument count parameter is a smi
2828 __ SmiToInteger32(reg, reg);
2829 Register return_addr_reg = reg.is(rcx) ? rbx : rcx;
2830 __ PopReturnAddressTo(return_addr_reg);
2831 __ shlp(reg, Immediate(kPointerSizeLog2));
2832 __ addp(rsp, reg);
2833 __ jmp(return_addr_reg);
2834 }
2835 if (no_frame_start != -1) {
2836 info_->AddNoFrameRange(no_frame_start, masm_->pc_offset());
2837 }
Ben Murdochb8e0da22011-05-16 14:20:40 +01002838}
2839
2840
Ben Murdoch8b112d22011-06-08 16:22:53 +01002841void LCodeGen::DoLoadGlobalCell(LLoadGlobalCell* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01002842 Register result = ToRegister(instr->result());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002843 __ LoadGlobalCell(result, instr->hydrogen()->cell().handle());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002844 if (instr->hydrogen()->RequiresHoleCheck()) {
Steve Block1e0659c2011-05-24 12:43:12 +01002845 __ CompareRoot(result, Heap::kTheHoleValueRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002846 DeoptimizeIf(equal, instr, "hole");
Steve Block1e0659c2011-05-24 12:43:12 +01002847 }
Ben Murdochb8e0da22011-05-16 14:20:40 +01002848}
2849
2850
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002851template <class T>
2852void LCodeGen::EmitVectorLoadICRegisters(T* instr) {
2853 DCHECK(FLAG_vector_ics);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002854 Register vector_register = ToRegister(instr->temp_vector());
2855 Register slot_register = VectorLoadICDescriptor::SlotRegister();
2856 DCHECK(vector_register.is(VectorLoadICDescriptor::VectorRegister()));
2857 DCHECK(slot_register.is(rax));
2858
2859 AllowDeferredHandleDereference vector_structure_check;
2860 Handle<TypeFeedbackVector> vector = instr->hydrogen()->feedback_vector();
2861 __ Move(vector_register, vector);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002862 // No need to allocate this register.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002863 FeedbackVectorICSlot slot = instr->hydrogen()->slot();
2864 int index = vector->GetIndex(slot);
2865 __ Move(slot_register, Smi::FromInt(index));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002866}
Ben Murdoch8b112d22011-06-08 16:22:53 +01002867
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002868
2869void LCodeGen::DoLoadGlobalGeneric(LLoadGlobalGeneric* instr) {
2870 DCHECK(ToRegister(instr->context()).is(rsi));
2871 DCHECK(ToRegister(instr->global_object())
2872 .is(LoadDescriptor::ReceiverRegister()));
2873 DCHECK(ToRegister(instr->result()).is(rax));
2874
2875 __ Move(LoadDescriptor::NameRegister(), instr->name());
2876 if (FLAG_vector_ics) {
2877 EmitVectorLoadICRegisters<LLoadGlobalGeneric>(instr);
2878 }
2879 ContextualMode mode = instr->for_typeof() ? NOT_CONTEXTUAL : CONTEXTUAL;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002880 Handle<Code> ic = CodeFactory::LoadICInOptimizedCode(isolate(), mode).code();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002881 CallCode(ic, RelocInfo::CODE_TARGET, instr);
Ben Murdoch8b112d22011-06-08 16:22:53 +01002882}
2883
2884
2885void LCodeGen::DoStoreGlobalCell(LStoreGlobalCell* instr) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002886 Register value = ToRegister(instr->value());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002887 Handle<Cell> cell_handle = instr->hydrogen()->cell().handle();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002888
Steve Block1e0659c2011-05-24 12:43:12 +01002889 // If the cell we are storing to contains the hole it could have
2890 // been deleted from the property dictionary. In that case, we need
2891 // to update the property details in the property dictionary to mark
2892 // it as no longer deleted. We deoptimize in that case.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002893 if (instr->hydrogen()->RequiresHoleCheck()) {
2894 // We have a temp because CompareRoot might clobber kScratchRegister.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002895 Register cell = ToRegister(instr->temp());
2896 DCHECK(!value.is(cell));
2897 __ Move(cell, cell_handle, RelocInfo::CELL);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002898 __ CompareRoot(Operand(cell, 0), Heap::kTheHoleValueRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002899 DeoptimizeIf(equal, instr, "hole");
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002900 // Store the value.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002901 __ movp(Operand(cell, 0), value);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002902 } else {
2903 // Store the value.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002904 __ Move(kScratchRegister, cell_handle, RelocInfo::CELL);
2905 __ movp(Operand(kScratchRegister, 0), value);
Steve Block1e0659c2011-05-24 12:43:12 +01002906 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002907 // Cells are always rescanned, so no write barrier here.
Ben Murdochb8e0da22011-05-16 14:20:40 +01002908}
2909
2910
2911void LCodeGen::DoLoadContextSlot(LLoadContextSlot* instr) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002912 Register context = ToRegister(instr->context());
2913 Register result = ToRegister(instr->result());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002914 __ movp(result, ContextOperand(context, instr->slot_index()));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002915 if (instr->hydrogen()->RequiresHoleCheck()) {
2916 __ CompareRoot(result, Heap::kTheHoleValueRootIndex);
2917 if (instr->hydrogen()->DeoptimizesOnHole()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002918 DeoptimizeIf(equal, instr, "hole");
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002919 } else {
2920 Label is_not_hole;
2921 __ j(not_equal, &is_not_hole, Label::kNear);
2922 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
2923 __ bind(&is_not_hole);
2924 }
2925 }
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002926}
2927
2928
2929void LCodeGen::DoStoreContextSlot(LStoreContextSlot* instr) {
2930 Register context = ToRegister(instr->context());
2931 Register value = ToRegister(instr->value());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002932
2933 Operand target = ContextOperand(context, instr->slot_index());
2934
2935 Label skip_assignment;
2936 if (instr->hydrogen()->RequiresHoleCheck()) {
2937 __ CompareRoot(target, Heap::kTheHoleValueRootIndex);
2938 if (instr->hydrogen()->DeoptimizesOnHole()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002939 DeoptimizeIf(equal, instr, "hole");
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002940 } else {
2941 __ j(not_equal, &skip_assignment);
2942 }
2943 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002944 __ movp(target, value);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002945
2946 if (instr->hydrogen()->NeedsWriteBarrier()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002947 SmiCheck check_needed =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002948 instr->hydrogen()->value()->type().IsHeapObject()
2949 ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002950 int offset = Context::SlotOffset(instr->slot_index());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002951 Register scratch = ToRegister(instr->temp());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002952 __ RecordWriteContextSlot(context,
2953 offset,
2954 value,
2955 scratch,
2956 kSaveFPRegs,
2957 EMIT_REMEMBERED_SET,
2958 check_needed);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002959 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002960
2961 __ bind(&skip_assignment);
Ben Murdochb8e0da22011-05-16 14:20:40 +01002962}
2963
2964
2965void LCodeGen::DoLoadNamedField(LLoadNamedField* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002966 HObjectAccess access = instr->hydrogen()->access();
2967 int offset = access.offset();
Ben Murdochb8e0da22011-05-16 14:20:40 +01002968
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002969 if (access.IsExternalMemory()) {
2970 Register result = ToRegister(instr->result());
2971 if (instr->object()->IsConstantOperand()) {
2972 DCHECK(result.is(rax));
2973 __ load_rax(ToExternalReference(LConstantOperand::cast(instr->object())));
Ben Murdoch257744e2011-11-30 15:57:28 +00002974 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002975 Register object = ToRegister(instr->object());
2976 __ Load(result, MemOperand(object, offset), access.representation());
Ben Murdoch257744e2011-11-30 15:57:28 +00002977 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002978 return;
Steve Block44f0eee2011-05-26 01:26:41 +01002979 }
Steve Block44f0eee2011-05-26 01:26:41 +01002980
Steve Block44f0eee2011-05-26 01:26:41 +01002981 Register object = ToRegister(instr->object());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002982 if (instr->hydrogen()->representation().IsDouble()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002983 DCHECK(access.IsInobject());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002984 XMMRegister result = ToDoubleRegister(instr->result());
2985 __ movsd(result, FieldOperand(object, offset));
2986 return;
Steve Block44f0eee2011-05-26 01:26:41 +01002987 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002988
2989 Register result = ToRegister(instr->result());
2990 if (!access.IsInobject()) {
2991 __ movp(result, FieldOperand(object, JSObject::kPropertiesOffset));
2992 object = result;
2993 }
2994
2995 Representation representation = access.representation();
2996 if (representation.IsSmi() && SmiValuesAre32Bits() &&
2997 instr->hydrogen()->representation().IsInteger32()) {
2998 if (FLAG_debug_code) {
2999 Register scratch = kScratchRegister;
3000 __ Load(scratch, FieldOperand(object, offset), representation);
3001 __ AssertSmi(scratch);
3002 }
3003
3004 // Read int value directly from upper half of the smi.
3005 STATIC_ASSERT(kSmiTag == 0);
3006 DCHECK(kSmiTagSize + kSmiShiftSize == 32);
3007 offset += kPointerSize / 2;
3008 representation = Representation::Integer32();
3009 }
3010 __ Load(result, FieldOperand(object, offset), representation);
Steve Block44f0eee2011-05-26 01:26:41 +01003011}
3012
3013
Ben Murdochb8e0da22011-05-16 14:20:40 +01003014void LCodeGen::DoLoadNamedGeneric(LLoadNamedGeneric* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003015 DCHECK(ToRegister(instr->context()).is(rsi));
3016 DCHECK(ToRegister(instr->object()).is(LoadDescriptor::ReceiverRegister()));
3017 DCHECK(ToRegister(instr->result()).is(rax));
Steve Block1e0659c2011-05-24 12:43:12 +01003018
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003019 __ Move(LoadDescriptor::NameRegister(), instr->name());
3020 if (FLAG_vector_ics) {
3021 EmitVectorLoadICRegisters<LLoadNamedGeneric>(instr);
3022 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003023 Handle<Code> ic =
3024 CodeFactory::LoadICInOptimizedCode(isolate(), NOT_CONTEXTUAL).code();
Steve Block1e0659c2011-05-24 12:43:12 +01003025 CallCode(ic, RelocInfo::CODE_TARGET, instr);
Ben Murdochb8e0da22011-05-16 14:20:40 +01003026}
3027
3028
3029void LCodeGen::DoLoadFunctionPrototype(LLoadFunctionPrototype* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01003030 Register function = ToRegister(instr->function());
3031 Register result = ToRegister(instr->result());
3032
Steve Block1e0659c2011-05-24 12:43:12 +01003033 // Get the prototype or initial map from the function.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003034 __ movp(result,
Steve Block1e0659c2011-05-24 12:43:12 +01003035 FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
3036
3037 // Check that the function has a prototype or an initial map.
3038 __ CompareRoot(result, Heap::kTheHoleValueRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003039 DeoptimizeIf(equal, instr, "hole");
Steve Block1e0659c2011-05-24 12:43:12 +01003040
3041 // If the function does not have an initial map, we're done.
Ben Murdoch257744e2011-11-30 15:57:28 +00003042 Label done;
Steve Block1e0659c2011-05-24 12:43:12 +01003043 __ CmpObjectType(result, MAP_TYPE, kScratchRegister);
Ben Murdoch257744e2011-11-30 15:57:28 +00003044 __ j(not_equal, &done, Label::kNear);
Steve Block1e0659c2011-05-24 12:43:12 +01003045
3046 // Get the prototype from the initial map.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003047 __ movp(result, FieldOperand(result, Map::kPrototypeOffset));
Steve Block1e0659c2011-05-24 12:43:12 +01003048
3049 // All done.
3050 __ bind(&done);
Ben Murdochb8e0da22011-05-16 14:20:40 +01003051}
3052
3053
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003054void LCodeGen::DoLoadRoot(LLoadRoot* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01003055 Register result = ToRegister(instr->result());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003056 __ LoadRoot(result, instr->index());
Ben Murdochb8e0da22011-05-16 14:20:40 +01003057}
3058
3059
3060void LCodeGen::DoAccessArgumentsAt(LAccessArgumentsAt* instr) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003061 Register arguments = ToRegister(instr->arguments());
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003062 Register result = ToRegister(instr->result());
3063
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003064 if (instr->length()->IsConstantOperand() &&
3065 instr->index()->IsConstantOperand()) {
3066 int32_t const_index = ToInteger32(LConstantOperand::cast(instr->index()));
3067 int32_t const_length = ToInteger32(LConstantOperand::cast(instr->length()));
3068 if (const_index >= 0 && const_index < const_length) {
3069 StackArgumentsAccessor args(arguments, const_length,
3070 ARGUMENTS_DONT_CONTAIN_RECEIVER);
3071 __ movp(result, args.GetArgumentOperand(const_index));
3072 } else if (FLAG_debug_code) {
3073 __ int3();
Ben Murdoch257744e2011-11-30 15:57:28 +00003074 }
Ben Murdoch257744e2011-11-30 15:57:28 +00003075 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003076 Register length = ToRegister(instr->length());
3077 // There are two words between the frame pointer and the last argument.
3078 // Subtracting from length accounts for one of them add one more.
3079 if (instr->index()->IsRegister()) {
3080 __ subl(length, ToRegister(instr->index()));
3081 } else {
3082 __ subl(length, ToOperand(instr->index()));
3083 }
3084 StackArgumentsAccessor args(arguments, length,
3085 ARGUMENTS_DONT_CONTAIN_RECEIVER);
3086 __ movp(result, args.GetArgumentOperand(0));
Ben Murdoch257744e2011-11-30 15:57:28 +00003087 }
Steve Block1e0659c2011-05-24 12:43:12 +01003088}
3089
3090
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003091void LCodeGen::DoLoadKeyedExternalArray(LLoadKeyed* instr) {
Ben Murdoch589d6972011-11-30 16:04:58 +00003092 ElementsKind elements_kind = instr->elements_kind();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003093 LOperand* key = instr->key();
3094 if (kPointerSize == kInt32Size && !key->IsConstantOperand()) {
3095 Register key_reg = ToRegister(key);
3096 Representation key_representation =
3097 instr->hydrogen()->key()->representation();
3098 if (ExternalArrayOpRequiresTemp(key_representation, elements_kind)) {
3099 __ SmiToInteger64(key_reg, key_reg);
3100 } else if (instr->hydrogen()->IsDehoisted()) {
3101 // Sign extend key because it could be a 32 bit negative value
3102 // and the dehoisted address computation happens in 64 bits
3103 __ movsxlq(key_reg, key_reg);
3104 }
3105 }
3106 Operand operand(BuildFastArrayOperand(
3107 instr->elements(),
3108 key,
3109 instr->hydrogen()->key()->representation(),
3110 elements_kind,
3111 instr->base_offset()));
3112
3113 if (elements_kind == EXTERNAL_FLOAT32_ELEMENTS ||
3114 elements_kind == FLOAT32_ELEMENTS) {
Steve Block44f0eee2011-05-26 01:26:41 +01003115 XMMRegister result(ToDoubleRegister(instr->result()));
Ben Murdoch257744e2011-11-30 15:57:28 +00003116 __ movss(result, operand);
Steve Block44f0eee2011-05-26 01:26:41 +01003117 __ cvtss2sd(result, result);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003118 } else if (elements_kind == EXTERNAL_FLOAT64_ELEMENTS ||
3119 elements_kind == FLOAT64_ELEMENTS) {
Ben Murdoch257744e2011-11-30 15:57:28 +00003120 __ movsd(ToDoubleRegister(instr->result()), operand);
Steve Block44f0eee2011-05-26 01:26:41 +01003121 } else {
3122 Register result(ToRegister(instr->result()));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003123 switch (elements_kind) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003124 case EXTERNAL_INT8_ELEMENTS:
3125 case INT8_ELEMENTS:
3126 __ movsxbl(result, operand);
Steve Block44f0eee2011-05-26 01:26:41 +01003127 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003128 case EXTERNAL_UINT8_ELEMENTS:
3129 case EXTERNAL_UINT8_CLAMPED_ELEMENTS:
3130 case UINT8_ELEMENTS:
3131 case UINT8_CLAMPED_ELEMENTS:
3132 __ movzxbl(result, operand);
Steve Block44f0eee2011-05-26 01:26:41 +01003133 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003134 case EXTERNAL_INT16_ELEMENTS:
3135 case INT16_ELEMENTS:
3136 __ movsxwl(result, operand);
Steve Block44f0eee2011-05-26 01:26:41 +01003137 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003138 case EXTERNAL_UINT16_ELEMENTS:
3139 case UINT16_ELEMENTS:
3140 __ movzxwl(result, operand);
Steve Block44f0eee2011-05-26 01:26:41 +01003141 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003142 case EXTERNAL_INT32_ELEMENTS:
3143 case INT32_ELEMENTS:
Ben Murdoch257744e2011-11-30 15:57:28 +00003144 __ movl(result, operand);
Steve Block44f0eee2011-05-26 01:26:41 +01003145 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003146 case EXTERNAL_UINT32_ELEMENTS:
3147 case UINT32_ELEMENTS:
3148 __ movl(result, operand);
3149 if (!instr->hydrogen()->CheckFlag(HInstruction::kUint32)) {
3150 __ testl(result, result);
3151 DeoptimizeIf(negative, instr, "negative value");
3152 }
3153 break;
3154 case EXTERNAL_FLOAT32_ELEMENTS:
3155 case EXTERNAL_FLOAT64_ELEMENTS:
3156 case FLOAT32_ELEMENTS:
3157 case FLOAT64_ELEMENTS:
Ben Murdoch589d6972011-11-30 16:04:58 +00003158 case FAST_ELEMENTS:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003159 case FAST_SMI_ELEMENTS:
Ben Murdoch589d6972011-11-30 16:04:58 +00003160 case FAST_DOUBLE_ELEMENTS:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003161 case FAST_HOLEY_ELEMENTS:
3162 case FAST_HOLEY_SMI_ELEMENTS:
3163 case FAST_HOLEY_DOUBLE_ELEMENTS:
Ben Murdoch589d6972011-11-30 16:04:58 +00003164 case DICTIONARY_ELEMENTS:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003165 case SLOPPY_ARGUMENTS_ELEMENTS:
Steve Block44f0eee2011-05-26 01:26:41 +01003166 UNREACHABLE();
3167 break;
3168 }
3169 }
Ben Murdochb8e0da22011-05-16 14:20:40 +01003170}
3171
3172
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003173void LCodeGen::DoLoadKeyedFixedDoubleArray(LLoadKeyed* instr) {
3174 XMMRegister result(ToDoubleRegister(instr->result()));
3175 LOperand* key = instr->key();
3176 if (kPointerSize == kInt32Size && !key->IsConstantOperand() &&
3177 instr->hydrogen()->IsDehoisted()) {
3178 // Sign extend key because it could be a 32 bit negative value
3179 // and the dehoisted address computation happens in 64 bits
3180 __ movsxlq(ToRegister(key), ToRegister(key));
3181 }
3182 if (instr->hydrogen()->RequiresHoleCheck()) {
3183 Operand hole_check_operand = BuildFastArrayOperand(
3184 instr->elements(),
3185 key,
3186 instr->hydrogen()->key()->representation(),
3187 FAST_DOUBLE_ELEMENTS,
3188 instr->base_offset() + sizeof(kHoleNanLower32));
3189 __ cmpl(hole_check_operand, Immediate(kHoleNanUpper32));
3190 DeoptimizeIf(equal, instr, "hole");
3191 }
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003192
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003193 Operand double_load_operand = BuildFastArrayOperand(
3194 instr->elements(),
3195 key,
3196 instr->hydrogen()->key()->representation(),
3197 FAST_DOUBLE_ELEMENTS,
3198 instr->base_offset());
3199 __ movsd(result, double_load_operand);
3200}
3201
3202
3203void LCodeGen::DoLoadKeyedFixedArray(LLoadKeyed* instr) {
3204 HLoadKeyed* hinstr = instr->hydrogen();
3205 Register result = ToRegister(instr->result());
3206 LOperand* key = instr->key();
3207 bool requires_hole_check = hinstr->RequiresHoleCheck();
3208 Representation representation = hinstr->representation();
3209 int offset = instr->base_offset();
3210
3211 if (kPointerSize == kInt32Size && !key->IsConstantOperand() &&
3212 instr->hydrogen()->IsDehoisted()) {
3213 // Sign extend key because it could be a 32 bit negative value
3214 // and the dehoisted address computation happens in 64 bits
3215 __ movsxlq(ToRegister(key), ToRegister(key));
3216 }
3217 if (representation.IsInteger32() && SmiValuesAre32Bits() &&
3218 hinstr->elements_kind() == FAST_SMI_ELEMENTS) {
3219 DCHECK(!requires_hole_check);
3220 if (FLAG_debug_code) {
3221 Register scratch = kScratchRegister;
3222 __ Load(scratch,
3223 BuildFastArrayOperand(instr->elements(),
3224 key,
3225 instr->hydrogen()->key()->representation(),
3226 FAST_ELEMENTS,
3227 offset),
3228 Representation::Smi());
3229 __ AssertSmi(scratch);
3230 }
3231 // Read int value directly from upper half of the smi.
3232 STATIC_ASSERT(kSmiTag == 0);
3233 DCHECK(kSmiTagSize + kSmiShiftSize == 32);
3234 offset += kPointerSize / 2;
3235 }
3236
3237 __ Load(result,
3238 BuildFastArrayOperand(instr->elements(), key,
3239 instr->hydrogen()->key()->representation(),
3240 FAST_ELEMENTS, offset),
3241 representation);
3242
3243 // Check for the hole value.
3244 if (requires_hole_check) {
3245 if (IsFastSmiElementsKind(hinstr->elements_kind())) {
3246 Condition smi = __ CheckSmi(result);
3247 DeoptimizeIf(NegateCondition(smi), instr, "not a Smi");
3248 } else {
3249 __ CompareRoot(result, Heap::kTheHoleValueRootIndex);
3250 DeoptimizeIf(equal, instr, "hole");
3251 }
3252 }
3253}
3254
3255
3256void LCodeGen::DoLoadKeyed(LLoadKeyed* instr) {
3257 if (instr->is_typed_elements()) {
3258 DoLoadKeyedExternalArray(instr);
3259 } else if (instr->hydrogen()->representation().IsDouble()) {
3260 DoLoadKeyedFixedDoubleArray(instr);
3261 } else {
3262 DoLoadKeyedFixedArray(instr);
3263 }
3264}
3265
3266
3267Operand LCodeGen::BuildFastArrayOperand(
3268 LOperand* elements_pointer,
3269 LOperand* key,
3270 Representation key_representation,
3271 ElementsKind elements_kind,
3272 uint32_t offset) {
3273 Register elements_pointer_reg = ToRegister(elements_pointer);
3274 int shift_size = ElementsKindToShiftSize(elements_kind);
3275 if (key->IsConstantOperand()) {
3276 int32_t constant_value = ToInteger32(LConstantOperand::cast(key));
3277 if (constant_value & 0xF0000000) {
3278 Abort(kArrayIndexConstantValueTooBig);
3279 }
3280 return Operand(elements_pointer_reg,
3281 (constant_value << shift_size) + offset);
3282 } else {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003283 // Guaranteed by ArrayInstructionInterface::KeyedAccessIndexRequirement().
3284 DCHECK(key_representation.IsInteger32());
3285
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003286 ScaleFactor scale_factor = static_cast<ScaleFactor>(shift_size);
3287 return Operand(elements_pointer_reg,
3288 ToRegister(key),
3289 scale_factor,
3290 offset);
3291 }
3292}
3293
3294
3295void LCodeGen::DoLoadKeyedGeneric(LLoadKeyedGeneric* instr) {
3296 DCHECK(ToRegister(instr->context()).is(rsi));
3297 DCHECK(ToRegister(instr->object()).is(LoadDescriptor::ReceiverRegister()));
3298 DCHECK(ToRegister(instr->key()).is(LoadDescriptor::NameRegister()));
3299
3300 if (FLAG_vector_ics) {
3301 EmitVectorLoadICRegisters<LLoadKeyedGeneric>(instr);
3302 }
3303
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003304 Handle<Code> ic = CodeFactory::KeyedLoadICInOptimizedCode(isolate()).code();
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003305 CallCode(ic, RelocInfo::CODE_TARGET, instr);
Ben Murdochb8e0da22011-05-16 14:20:40 +01003306}
3307
3308
3309void LCodeGen::DoArgumentsElements(LArgumentsElements* instr) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003310 Register result = ToRegister(instr->result());
3311
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003312 if (instr->hydrogen()->from_inlined()) {
3313 __ leap(result, Operand(rsp, -kFPOnStackSize + -kPCOnStackSize));
3314 } else {
3315 // Check for arguments adapter frame.
3316 Label done, adapted;
3317 __ movp(result, Operand(rbp, StandardFrameConstants::kCallerFPOffset));
3318 __ Cmp(Operand(result, StandardFrameConstants::kContextOffset),
3319 Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
3320 __ j(equal, &adapted, Label::kNear);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003321
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003322 // No arguments adaptor frame.
3323 __ movp(result, rbp);
3324 __ jmp(&done, Label::kNear);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003325
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003326 // Arguments adaptor frame present.
3327 __ bind(&adapted);
3328 __ movp(result, Operand(rbp, StandardFrameConstants::kCallerFPOffset));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003329
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003330 // Result is the frame pointer for the frame if not adapted and for the real
3331 // frame below the adaptor frame if adapted.
3332 __ bind(&done);
3333 }
Ben Murdochb8e0da22011-05-16 14:20:40 +01003334}
3335
3336
3337void LCodeGen::DoArgumentsLength(LArgumentsLength* instr) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003338 Register result = ToRegister(instr->result());
3339
Ben Murdoch257744e2011-11-30 15:57:28 +00003340 Label done;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003341
3342 // If no arguments adaptor frame the number of arguments is fixed.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003343 if (instr->elements()->IsRegister()) {
3344 __ cmpp(rbp, ToRegister(instr->elements()));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003345 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003346 __ cmpp(rbp, ToOperand(instr->elements()));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003347 }
Ben Murdoch8b112d22011-06-08 16:22:53 +01003348 __ movl(result, Immediate(scope()->num_parameters()));
Ben Murdoch257744e2011-11-30 15:57:28 +00003349 __ j(equal, &done, Label::kNear);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003350
3351 // Arguments adaptor frame present. Get argument length from there.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003352 __ movp(result, Operand(rbp, StandardFrameConstants::kCallerFPOffset));
Ben Murdoch8b112d22011-06-08 16:22:53 +01003353 __ SmiToInteger32(result,
3354 Operand(result,
3355 ArgumentsAdaptorFrameConstants::kLengthOffset));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003356
3357 // Argument length is in result register.
3358 __ bind(&done);
Ben Murdochb8e0da22011-05-16 14:20:40 +01003359}
3360
3361
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003362void LCodeGen::DoWrapReceiver(LWrapReceiver* instr) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003363 Register receiver = ToRegister(instr->receiver());
3364 Register function = ToRegister(instr->function());
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003365
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003366 // If the receiver is null or undefined, we have to pass the global
3367 // object as a receiver to normal functions. Values have to be
3368 // passed unchanged to builtins and strict-mode functions.
Ben Murdoch257744e2011-11-30 15:57:28 +00003369 Label global_object, receiver_ok;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003370 Label::Distance dist = DeoptEveryNTimes() ? Label::kFar : Label::kNear;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003371
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003372 if (!instr->hydrogen()->known_function()) {
3373 // Do not transform the receiver to object for strict mode
3374 // functions.
3375 __ movp(kScratchRegister,
3376 FieldOperand(function, JSFunction::kSharedFunctionInfoOffset));
3377 __ testb(FieldOperand(kScratchRegister,
3378 SharedFunctionInfo::kStrictModeByteOffset),
3379 Immediate(1 << SharedFunctionInfo::kStrictModeBitWithinByte));
3380 __ j(not_equal, &receiver_ok, dist);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003381
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003382 // Do not transform the receiver to object for builtins.
3383 __ testb(FieldOperand(kScratchRegister,
3384 SharedFunctionInfo::kNativeByteOffset),
3385 Immediate(1 << SharedFunctionInfo::kNativeBitWithinByte));
3386 __ j(not_equal, &receiver_ok, dist);
3387 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003388
3389 // Normal function. Replace undefined or null with global receiver.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003390 __ CompareRoot(receiver, Heap::kNullValueRootIndex);
Ben Murdoch257744e2011-11-30 15:57:28 +00003391 __ j(equal, &global_object, Label::kNear);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003392 __ CompareRoot(receiver, Heap::kUndefinedValueRootIndex);
Ben Murdoch257744e2011-11-30 15:57:28 +00003393 __ j(equal, &global_object, Label::kNear);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003394
3395 // The receiver should be a JS object.
3396 Condition is_smi = __ CheckSmi(receiver);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003397 DeoptimizeIf(is_smi, instr, "Smi");
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003398 __ CmpObjectType(receiver, FIRST_SPEC_OBJECT_TYPE, kScratchRegister);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003399 DeoptimizeIf(below, instr, "not a JavaScript object");
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003400
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003401 __ jmp(&receiver_ok, Label::kNear);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003402 __ bind(&global_object);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003403 __ movp(receiver, FieldOperand(function, JSFunction::kContextOffset));
3404 __ movp(receiver,
3405 Operand(receiver,
3406 Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
3407 __ movp(receiver, FieldOperand(receiver, GlobalObject::kGlobalProxyOffset));
3408
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003409 __ bind(&receiver_ok);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003410}
3411
3412
3413void LCodeGen::DoApplyArguments(LApplyArguments* instr) {
3414 Register receiver = ToRegister(instr->receiver());
3415 Register function = ToRegister(instr->function());
3416 Register length = ToRegister(instr->length());
3417 Register elements = ToRegister(instr->elements());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003418 DCHECK(receiver.is(rax)); // Used for parameter count.
3419 DCHECK(function.is(rdi)); // Required by InvokeFunction.
3420 DCHECK(ToRegister(instr->result()).is(rax));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003421
3422 // Copy the arguments to this function possibly from the
3423 // adaptor frame below it.
3424 const uint32_t kArgumentsLimit = 1 * KB;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003425 __ cmpp(length, Immediate(kArgumentsLimit));
3426 DeoptimizeIf(above, instr, "too many arguments");
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003427
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003428 __ Push(receiver);
3429 __ movp(receiver, length);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003430
3431 // Loop through the arguments pushing them onto the execution
3432 // stack.
Ben Murdoch257744e2011-11-30 15:57:28 +00003433 Label invoke, loop;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003434 // length is a small non-negative integer, due to the test above.
3435 __ testl(length, length);
Ben Murdoch257744e2011-11-30 15:57:28 +00003436 __ j(zero, &invoke, Label::kNear);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003437 __ bind(&loop);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003438 StackArgumentsAccessor args(elements, length,
3439 ARGUMENTS_DONT_CONTAIN_RECEIVER);
3440 __ Push(args.GetArgumentOperand(0));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003441 __ decl(length);
3442 __ j(not_zero, &loop);
3443
3444 // Invoke the function.
3445 __ bind(&invoke);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003446 DCHECK(instr->HasPointerMap());
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003447 LPointerMap* pointers = instr->pointer_map();
Ben Murdoch2b4ba112012-01-20 14:57:15 +00003448 SafepointGenerator safepoint_generator(
3449 this, pointers, Safepoint::kLazyDeopt);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003450 ParameterCount actual(rax);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003451 __ InvokeFunction(function, actual, CALL_FUNCTION, safepoint_generator);
Ben Murdochb8e0da22011-05-16 14:20:40 +01003452}
3453
3454
3455void LCodeGen::DoPushArgument(LPushArgument* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003456 LOperand* argument = instr->value();
Ben Murdoch257744e2011-11-30 15:57:28 +00003457 EmitPushTaggedOperand(argument);
Steve Block1e0659c2011-05-24 12:43:12 +01003458}
3459
3460
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003461void LCodeGen::DoDrop(LDrop* instr) {
3462 __ Drop(instr->count());
3463}
3464
3465
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003466void LCodeGen::DoThisFunction(LThisFunction* instr) {
3467 Register result = ToRegister(instr->result());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003468 __ movp(result, Operand(rbp, JavaScriptFrameConstants::kFunctionOffset));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003469}
3470
3471
Steve Block1e0659c2011-05-24 12:43:12 +01003472void LCodeGen::DoContext(LContext* instr) {
3473 Register result = ToRegister(instr->result());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003474 if (info()->IsOptimizing()) {
3475 __ movp(result, Operand(rbp, StandardFrameConstants::kContextOffset));
3476 } else {
3477 // If there is no frame, the context must be in rsi.
3478 DCHECK(result.is(rsi));
3479 }
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003480}
3481
3482
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003483void LCodeGen::DoDeclareGlobals(LDeclareGlobals* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003484 DCHECK(ToRegister(instr->context()).is(rsi));
3485 __ Push(rsi); // The context is the first argument.
3486 __ Push(instr->hydrogen()->pairs());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003487 __ Push(Smi::FromInt(instr->hydrogen()->flags()));
3488 CallRuntime(Runtime::kDeclareGlobals, 3, instr);
3489}
3490
3491
Ben Murdochb8e0da22011-05-16 14:20:40 +01003492void LCodeGen::CallKnownFunction(Handle<JSFunction> function,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003493 int formal_parameter_count,
Ben Murdochb8e0da22011-05-16 14:20:40 +01003494 int arity,
Ben Murdoch257744e2011-11-30 15:57:28 +00003495 LInstruction* instr,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003496 RDIState rdi_state) {
3497 bool dont_adapt_arguments =
3498 formal_parameter_count == SharedFunctionInfo::kDontAdaptArgumentsSentinel;
3499 bool can_invoke_directly =
3500 dont_adapt_arguments || formal_parameter_count == arity;
Steve Block1e0659c2011-05-24 12:43:12 +01003501
3502 LPointerMap* pointers = instr->pointer_map();
Steve Block1e0659c2011-05-24 12:43:12 +01003503
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003504 if (can_invoke_directly) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003505 if (rdi_state == RDI_UNINITIALIZED) {
3506 __ Move(rdi, function);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003507 }
3508
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003509 // Change context.
3510 __ movp(rsi, FieldOperand(rdi, JSFunction::kContextOffset));
3511
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003512 // Set rax to arguments count if adaption is not needed. Assumes that rax
3513 // is available to write to at this point.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003514 if (dont_adapt_arguments) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003515 __ Set(rax, arity);
3516 }
3517
3518 // Invoke function.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003519 if (function.is_identical_to(info()->closure())) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003520 __ CallSelf();
3521 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003522 __ Call(FieldOperand(rdi, JSFunction::kCodeEntryOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003523 }
3524
3525 // Set up deoptimization.
3526 RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT, 0);
3527 } else {
3528 // We need to adapt arguments.
3529 SafepointGenerator generator(
3530 this, pointers, Safepoint::kLazyDeopt);
3531 ParameterCount count(arity);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003532 ParameterCount expected(formal_parameter_count);
3533 __ InvokeFunction(function, expected, count, CALL_FUNCTION, generator);
3534 }
3535}
3536
3537
3538void LCodeGen::DoTailCallThroughMegamorphicCache(
3539 LTailCallThroughMegamorphicCache* instr) {
3540 Register receiver = ToRegister(instr->receiver());
3541 Register name = ToRegister(instr->name());
3542 DCHECK(receiver.is(LoadDescriptor::ReceiverRegister()));
3543 DCHECK(name.is(LoadDescriptor::NameRegister()));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003544 Register scratch = rdi;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003545 DCHECK(!scratch.is(receiver) && !scratch.is(name));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003546 DCHECK(!FLAG_vector_ics ||
3547 !AreAliased(ToRegister(instr->slot()), ToRegister(instr->vector()),
3548 scratch));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003549
3550 // Important for the tail-call.
3551 bool must_teardown_frame = NeedsEagerFrame();
3552
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003553 if (!instr->hydrogen()->is_just_miss()) {
3554 // The probe will tail call to a handler if found.
3555 DCHECK(!instr->hydrogen()->is_keyed_load());
3556 isolate()->stub_cache()->GenerateProbe(
3557 masm(), Code::LOAD_IC, instr->hydrogen()->flags(), must_teardown_frame,
3558 receiver, name, scratch, no_reg);
3559 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003560
3561 // Tail call to miss if we ended up here.
3562 if (must_teardown_frame) __ leave();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003563 if (instr->hydrogen()->is_keyed_load()) {
3564 KeyedLoadIC::GenerateMiss(masm());
3565 } else {
3566 LoadIC::GenerateMiss(masm());
3567 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003568}
3569
3570
3571void LCodeGen::DoCallWithDescriptor(LCallWithDescriptor* instr) {
3572 DCHECK(ToRegister(instr->result()).is(rax));
3573
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003574 if (instr->hydrogen()->IsTailCall()) {
3575 if (NeedsEagerFrame()) __ leave();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003576
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003577 if (instr->target()->IsConstantOperand()) {
3578 LConstantOperand* target = LConstantOperand::cast(instr->target());
3579 Handle<Code> code = Handle<Code>::cast(ToHandle(target));
3580 __ jmp(code, RelocInfo::CODE_TARGET);
3581 } else {
3582 DCHECK(instr->target()->IsRegister());
3583 Register target = ToRegister(instr->target());
3584 __ addp(target, Immediate(Code::kHeaderSize - kHeapObjectTag));
3585 __ jmp(target);
3586 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003587 } else {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003588 LPointerMap* pointers = instr->pointer_map();
3589 SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
3590
3591 if (instr->target()->IsConstantOperand()) {
3592 LConstantOperand* target = LConstantOperand::cast(instr->target());
3593 Handle<Code> code = Handle<Code>::cast(ToHandle(target));
3594 generator.BeforeCall(__ CallSize(code));
3595 __ call(code, RelocInfo::CODE_TARGET);
3596 } else {
3597 DCHECK(instr->target()->IsRegister());
3598 Register target = ToRegister(instr->target());
3599 generator.BeforeCall(__ CallSize(target));
3600 __ addp(target, Immediate(Code::kHeaderSize - kHeapObjectTag));
3601 __ call(target);
3602 }
3603 generator.AfterCall();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003604 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003605}
3606
3607
3608void LCodeGen::DoCallJSFunction(LCallJSFunction* instr) {
3609 DCHECK(ToRegister(instr->function()).is(rdi));
3610 DCHECK(ToRegister(instr->result()).is(rax));
3611
3612 if (instr->hydrogen()->pass_argument_count()) {
3613 __ Set(rax, instr->arity());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003614 }
Ben Murdoch85b71792012-04-11 18:30:58 +01003615
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003616 // Change context.
3617 __ movp(rsi, FieldOperand(rdi, JSFunction::kContextOffset));
3618
3619 LPointerMap* pointers = instr->pointer_map();
3620 SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
3621
3622 bool is_self_call = false;
3623 if (instr->hydrogen()->function()->IsConstant()) {
3624 Handle<JSFunction> jsfun = Handle<JSFunction>::null();
3625 HConstant* fun_const = HConstant::cast(instr->hydrogen()->function());
3626 jsfun = Handle<JSFunction>::cast(fun_const->handle(isolate()));
3627 is_self_call = jsfun.is_identical_to(info()->closure());
3628 }
3629
3630 if (is_self_call) {
3631 __ CallSelf();
3632 } else {
3633 Operand target = FieldOperand(rdi, JSFunction::kCodeEntryOffset);
3634 generator.BeforeCall(__ CallSize(target));
3635 __ Call(target);
3636 }
3637 generator.AfterCall();
Ben Murdochb8e0da22011-05-16 14:20:40 +01003638}
3639
3640
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003641void LCodeGen::DoDeferredMathAbsTaggedHeapNumber(LMathAbs* instr) {
3642 Register input_reg = ToRegister(instr->value());
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003643 __ CompareRoot(FieldOperand(input_reg, HeapObject::kMapOffset),
3644 Heap::kHeapNumberMapRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003645 DeoptimizeIf(not_equal, instr, "not a heap number");
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003646
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003647 Label slow, allocated, done;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003648 Register tmp = input_reg.is(rax) ? rcx : rax;
3649 Register tmp2 = tmp.is(rcx) ? rdx : input_reg.is(rcx) ? rdx : rcx;
3650
3651 // Preserve the value of all registers.
Ben Murdoch8b112d22011-06-08 16:22:53 +01003652 PushSafepointRegistersScope scope(this);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003653
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003654 __ movl(tmp, FieldOperand(input_reg, HeapNumber::kExponentOffset));
3655 // Check the sign of the argument. If the argument is positive, just
3656 // return it. We do not need to patch the stack since |input| and
3657 // |result| are the same register and |input| will be restored
3658 // unchanged by popping safepoint registers.
3659 __ testl(tmp, Immediate(HeapNumber::kSignMask));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003660 __ j(zero, &done);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003661
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003662 __ AllocateHeapNumber(tmp, tmp2, &slow);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003663 __ jmp(&allocated, Label::kNear);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003664
3665 // Slow case: Call the runtime system to do the number allocation.
3666 __ bind(&slow);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003667 CallRuntimeFromDeferred(
3668 Runtime::kAllocateHeapNumber, 0, instr, instr->context());
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003669 // Set the pointer to the new heap number in tmp.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003670 if (!tmp.is(rax)) __ movp(tmp, rax);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003671 // Restore input_reg after call to runtime.
3672 __ LoadFromSafepointRegisterSlot(input_reg, input_reg);
3673
3674 __ bind(&allocated);
3675 __ movq(tmp2, FieldOperand(input_reg, HeapNumber::kValueOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003676 __ shlq(tmp2, Immediate(1));
3677 __ shrq(tmp2, Immediate(1));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003678 __ movq(FieldOperand(tmp, HeapNumber::kValueOffset), tmp2);
3679 __ StoreToSafepointRegisterSlot(input_reg, tmp);
3680
3681 __ bind(&done);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003682}
3683
3684
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003685void LCodeGen::EmitIntegerMathAbs(LMathAbs* instr) {
3686 Register input_reg = ToRegister(instr->value());
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003687 __ testl(input_reg, input_reg);
3688 Label is_positive;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003689 __ j(not_sign, &is_positive, Label::kNear);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003690 __ negl(input_reg); // Sets flags.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003691 DeoptimizeIf(negative, instr, "overflow");
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003692 __ bind(&is_positive);
Ben Murdochb8e0da22011-05-16 14:20:40 +01003693}
3694
3695
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003696void LCodeGen::EmitSmiMathAbs(LMathAbs* instr) {
3697 Register input_reg = ToRegister(instr->value());
3698 __ testp(input_reg, input_reg);
3699 Label is_positive;
3700 __ j(not_sign, &is_positive, Label::kNear);
3701 __ negp(input_reg); // Sets flags.
3702 DeoptimizeIf(negative, instr, "overflow");
3703 __ bind(&is_positive);
3704}
3705
3706
3707void LCodeGen::DoMathAbs(LMathAbs* instr) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003708 // Class for deferred case.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003709 class DeferredMathAbsTaggedHeapNumber FINAL : public LDeferredCode {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003710 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003711 DeferredMathAbsTaggedHeapNumber(LCodeGen* codegen, LMathAbs* instr)
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003712 : LDeferredCode(codegen), instr_(instr) { }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003713 void Generate() OVERRIDE {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003714 codegen()->DoDeferredMathAbsTaggedHeapNumber(instr_);
3715 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003716 LInstruction* instr() OVERRIDE { return instr_; }
3717
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003718 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003719 LMathAbs* instr_;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003720 };
3721
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003722 DCHECK(instr->value()->Equals(instr->result()));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003723 Representation r = instr->hydrogen()->value()->representation();
3724
3725 if (r.IsDouble()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003726 XMMRegister scratch = double_scratch0();
3727 XMMRegister input_reg = ToDoubleRegister(instr->value());
Ben Murdoch257744e2011-11-30 15:57:28 +00003728 __ xorps(scratch, scratch);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003729 __ subsd(scratch, input_reg);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003730 __ andps(input_reg, scratch);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003731 } else if (r.IsInteger32()) {
3732 EmitIntegerMathAbs(instr);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003733 } else if (r.IsSmi()) {
3734 EmitSmiMathAbs(instr);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003735 } else { // Tagged case.
3736 DeferredMathAbsTaggedHeapNumber* deferred =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003737 new(zone()) DeferredMathAbsTaggedHeapNumber(this, instr);
3738 Register input_reg = ToRegister(instr->value());
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003739 // Smi check.
3740 __ JumpIfNotSmi(input_reg, deferred->entry());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003741 EmitSmiMathAbs(instr);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003742 __ bind(deferred->exit());
3743 }
Ben Murdochb8e0da22011-05-16 14:20:40 +01003744}
3745
3746
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003747void LCodeGen::DoMathFloor(LMathFloor* instr) {
3748 XMMRegister xmm_scratch = double_scratch0();
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003749 Register output_reg = ToRegister(instr->result());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003750 XMMRegister input_reg = ToDoubleRegister(instr->value());
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003751
Ben Murdoch257744e2011-11-30 15:57:28 +00003752 if (CpuFeatures::IsSupported(SSE4_1)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003753 CpuFeatureScope scope(masm(), SSE4_1);
Ben Murdoch257744e2011-11-30 15:57:28 +00003754 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3755 // Deoptimize if minus zero.
3756 __ movq(output_reg, input_reg);
3757 __ subq(output_reg, Immediate(1));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003758 DeoptimizeIf(overflow, instr, "minus zero");
Ben Murdoch257744e2011-11-30 15:57:28 +00003759 }
3760 __ roundsd(xmm_scratch, input_reg, Assembler::kRoundDown);
3761 __ cvttsd2si(output_reg, xmm_scratch);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003762 __ cmpl(output_reg, Immediate(0x1));
3763 DeoptimizeIf(overflow, instr, "overflow");
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003764 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003765 Label negative_sign, done;
3766 // Deoptimize on unordered.
Ben Murdoch257744e2011-11-30 15:57:28 +00003767 __ xorps(xmm_scratch, xmm_scratch); // Zero the register.
3768 __ ucomisd(input_reg, xmm_scratch);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003769 DeoptimizeIf(parity_even, instr, "NaN");
3770 __ j(below, &negative_sign, Label::kNear);
3771
Ben Murdoch257744e2011-11-30 15:57:28 +00003772 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003773 // Check for negative zero.
3774 Label positive_sign;
3775 __ j(above, &positive_sign, Label::kNear);
3776 __ movmskpd(output_reg, input_reg);
3777 __ testq(output_reg, Immediate(1));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003778 DeoptimizeIf(not_zero, instr, "minus zero");
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003779 __ Set(output_reg, 0);
3780 __ jmp(&done);
3781 __ bind(&positive_sign);
Ben Murdoch257744e2011-11-30 15:57:28 +00003782 }
3783
3784 // Use truncating instruction (OK because input is positive).
3785 __ cvttsd2si(output_reg, input_reg);
Ben Murdoch257744e2011-11-30 15:57:28 +00003786 // Overflow is signalled with minint.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003787 __ cmpl(output_reg, Immediate(0x1));
3788 DeoptimizeIf(overflow, instr, "overflow");
3789 __ jmp(&done, Label::kNear);
3790
3791 // Non-zero negative reaches here.
3792 __ bind(&negative_sign);
3793 // Truncate, then compare and compensate.
3794 __ cvttsd2si(output_reg, input_reg);
3795 __ Cvtlsi2sd(xmm_scratch, output_reg);
3796 __ ucomisd(input_reg, xmm_scratch);
3797 __ j(equal, &done, Label::kNear);
3798 __ subl(output_reg, Immediate(1));
3799 DeoptimizeIf(overflow, instr, "overflow");
3800
3801 __ bind(&done);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003802 }
Ben Murdochb8e0da22011-05-16 14:20:40 +01003803}
3804
3805
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003806void LCodeGen::DoMathRound(LMathRound* instr) {
3807 const XMMRegister xmm_scratch = double_scratch0();
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003808 Register output_reg = ToRegister(instr->result());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003809 XMMRegister input_reg = ToDoubleRegister(instr->value());
3810 XMMRegister input_temp = ToDoubleRegister(instr->temp());
3811 static int64_t one_half = V8_INT64_C(0x3FE0000000000000); // 0.5
3812 static int64_t minus_one_half = V8_INT64_C(0xBFE0000000000000); // -0.5
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003813
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003814 Label done, round_to_zero, below_one_half;
3815 Label::Distance dist = DeoptEveryNTimes() ? Label::kFar : Label::kNear;
3816 __ movq(kScratchRegister, one_half);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003817 __ movq(xmm_scratch, kScratchRegister);
Ben Murdoch257744e2011-11-30 15:57:28 +00003818 __ ucomisd(xmm_scratch, input_reg);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003819 __ j(above, &below_one_half, Label::kNear);
3820
3821 // CVTTSD2SI rounds towards zero, since 0.5 <= x, we use floor(0.5 + x).
Ben Murdoch692be652012-01-10 18:47:50 +00003822 __ addsd(xmm_scratch, input_reg);
Ben Murdoch692be652012-01-10 18:47:50 +00003823 __ cvttsd2si(output_reg, xmm_scratch);
Steve Block053d10c2011-06-13 19:13:29 +01003824 // Overflow is signalled with minint.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003825 __ cmpl(output_reg, Immediate(0x1));
3826 DeoptimizeIf(overflow, instr, "overflow");
3827 __ jmp(&done, dist);
Ben Murdoch257744e2011-11-30 15:57:28 +00003828
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003829 __ bind(&below_one_half);
3830 __ movq(kScratchRegister, minus_one_half);
3831 __ movq(xmm_scratch, kScratchRegister);
3832 __ ucomisd(xmm_scratch, input_reg);
3833 __ j(below_equal, &round_to_zero, Label::kNear);
3834
3835 // CVTTSD2SI rounds towards zero, we use ceil(x - (-0.5)) and then
3836 // compare and compensate.
3837 __ movq(input_temp, input_reg); // Do not alter input_reg.
3838 __ subsd(input_temp, xmm_scratch);
3839 __ cvttsd2si(output_reg, input_temp);
3840 // Catch minint due to overflow, and to prevent overflow when compensating.
3841 __ cmpl(output_reg, Immediate(0x1));
3842 DeoptimizeIf(overflow, instr, "overflow");
3843
3844 __ Cvtlsi2sd(xmm_scratch, output_reg);
3845 __ ucomisd(xmm_scratch, input_temp);
3846 __ j(equal, &done, dist);
3847 __ subl(output_reg, Immediate(1));
3848 // No overflow because we already ruled out minint.
3849 __ jmp(&done, dist);
3850
3851 __ bind(&round_to_zero);
3852 // We return 0 for the input range [+0, 0.5[, or [-0.5, 0.5[ if
3853 // we can ignore the difference between a result of -0 and +0.
Ben Murdoch257744e2011-11-30 15:57:28 +00003854 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
Ben Murdoch257744e2011-11-30 15:57:28 +00003855 __ movq(output_reg, input_reg);
3856 __ testq(output_reg, output_reg);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003857 DeoptimizeIf(negative, instr, "minus zero");
Ben Murdoch257744e2011-11-30 15:57:28 +00003858 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003859 __ Set(output_reg, 0);
Ben Murdoch257744e2011-11-30 15:57:28 +00003860 __ bind(&done);
Ben Murdochb8e0da22011-05-16 14:20:40 +01003861}
3862
3863
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003864void LCodeGen::DoMathFround(LMathFround* instr) {
3865 XMMRegister input_reg = ToDoubleRegister(instr->value());
3866 XMMRegister output_reg = ToDoubleRegister(instr->result());
3867 __ cvtsd2ss(output_reg, input_reg);
3868 __ cvtss2sd(output_reg, output_reg);
Ben Murdochb8e0da22011-05-16 14:20:40 +01003869}
3870
3871
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003872void LCodeGen::DoMathSqrt(LMathSqrt* instr) {
3873 XMMRegister output = ToDoubleRegister(instr->result());
3874 if (instr->value()->IsDoubleRegister()) {
3875 XMMRegister input = ToDoubleRegister(instr->value());
3876 __ sqrtsd(output, input);
3877 } else {
3878 Operand input = ToOperand(instr->value());
3879 __ sqrtsd(output, input);
3880 }
3881}
3882
3883
3884void LCodeGen::DoMathPowHalf(LMathPowHalf* instr) {
3885 XMMRegister xmm_scratch = double_scratch0();
3886 XMMRegister input_reg = ToDoubleRegister(instr->value());
3887 DCHECK(ToDoubleRegister(instr->result()).is(input_reg));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003888
3889 // Note that according to ECMA-262 15.8.2.13:
3890 // Math.pow(-Infinity, 0.5) == Infinity
3891 // Math.sqrt(-Infinity) == NaN
3892 Label done, sqrt;
3893 // Check base for -Infinity. According to IEEE-754, double-precision
3894 // -Infinity has the highest 12 bits set and the lowest 52 bits cleared.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003895 __ movq(kScratchRegister, V8_INT64_C(0xFFF0000000000000));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003896 __ movq(xmm_scratch, kScratchRegister);
3897 __ ucomisd(xmm_scratch, input_reg);
3898 // Comparing -Infinity with NaN results in "unordered", which sets the
3899 // zero flag as if both were equal. However, it also sets the carry flag.
3900 __ j(not_equal, &sqrt, Label::kNear);
3901 __ j(carry, &sqrt, Label::kNear);
3902 // If input is -Infinity, return Infinity.
3903 __ xorps(input_reg, input_reg);
3904 __ subsd(input_reg, xmm_scratch);
3905 __ jmp(&done, Label::kNear);
3906
3907 // Square root.
3908 __ bind(&sqrt);
Ben Murdoch257744e2011-11-30 15:57:28 +00003909 __ xorps(xmm_scratch, xmm_scratch);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003910 __ addsd(input_reg, xmm_scratch); // Convert -0 to +0.
3911 __ sqrtsd(input_reg, input_reg);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003912 __ bind(&done);
Ben Murdochb8e0da22011-05-16 14:20:40 +01003913}
3914
3915
3916void LCodeGen::DoPower(LPower* instr) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003917 Representation exponent_type = instr->hydrogen()->right()->representation();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003918 // Having marked this as a call, we can use any registers.
3919 // Just make sure that the input/output registers are the expected ones.
3920
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003921 Register tagged_exponent = MathPowTaggedDescriptor::exponent();
3922 DCHECK(!instr->right()->IsRegister() ||
3923 ToRegister(instr->right()).is(tagged_exponent));
3924 DCHECK(!instr->right()->IsDoubleRegister() ||
3925 ToDoubleRegister(instr->right()).is(xmm1));
3926 DCHECK(ToDoubleRegister(instr->left()).is(xmm2));
3927 DCHECK(ToDoubleRegister(instr->result()).is(xmm3));
Ben Murdochc7cc0282012-03-05 14:35:55 +00003928
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003929 if (exponent_type.IsSmi()) {
3930 MathPowStub stub(isolate(), MathPowStub::TAGGED);
3931 __ CallStub(&stub);
3932 } else if (exponent_type.IsTagged()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003933 Label no_deopt;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003934 __ JumpIfSmi(tagged_exponent, &no_deopt, Label::kNear);
3935 __ CmpObjectType(tagged_exponent, HEAP_NUMBER_TYPE, rcx);
3936 DeoptimizeIf(not_equal, instr, "not a heap number");
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003937 __ bind(&no_deopt);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003938 MathPowStub stub(isolate(), MathPowStub::TAGGED);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003939 __ CallStub(&stub);
3940 } else if (exponent_type.IsInteger32()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003941 MathPowStub stub(isolate(), MathPowStub::INTEGER);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003942 __ CallStub(&stub);
3943 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003944 DCHECK(exponent_type.IsDouble());
3945 MathPowStub stub(isolate(), MathPowStub::DOUBLE);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003946 __ CallStub(&stub);
Ben Murdoch85b71792012-04-11 18:30:58 +01003947 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003948}
3949
3950
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003951void LCodeGen::DoMathExp(LMathExp* instr) {
3952 XMMRegister input = ToDoubleRegister(instr->value());
3953 XMMRegister result = ToDoubleRegister(instr->result());
3954 XMMRegister temp0 = double_scratch0();
3955 Register temp1 = ToRegister(instr->temp1());
3956 Register temp2 = ToRegister(instr->temp2());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003957
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003958 MathExpGenerator::EmitMathExp(masm(), input, result, temp0, temp1, temp2);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003959}
3960
3961
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003962void LCodeGen::DoMathLog(LMathLog* instr) {
3963 DCHECK(instr->value()->Equals(instr->result()));
3964 XMMRegister input_reg = ToDoubleRegister(instr->value());
3965 XMMRegister xmm_scratch = double_scratch0();
3966 Label positive, done, zero;
3967 __ xorps(xmm_scratch, xmm_scratch);
3968 __ ucomisd(input_reg, xmm_scratch);
3969 __ j(above, &positive, Label::kNear);
3970 __ j(not_carry, &zero, Label::kNear);
3971 ExternalReference nan =
3972 ExternalReference::address_of_canonical_non_hole_nan();
3973 Operand nan_operand = masm()->ExternalOperand(nan);
3974 __ movsd(input_reg, nan_operand);
3975 __ jmp(&done, Label::kNear);
3976 __ bind(&zero);
3977 ExternalReference ninf =
3978 ExternalReference::address_of_negative_infinity();
3979 Operand ninf_operand = masm()->ExternalOperand(ninf);
3980 __ movsd(input_reg, ninf_operand);
3981 __ jmp(&done, Label::kNear);
3982 __ bind(&positive);
3983 __ fldln2();
3984 __ subp(rsp, Immediate(kDoubleSize));
3985 __ movsd(Operand(rsp, 0), input_reg);
3986 __ fld_d(Operand(rsp, 0));
3987 __ fyl2x();
3988 __ fstp_d(Operand(rsp, 0));
3989 __ movsd(input_reg, Operand(rsp, 0));
3990 __ addp(rsp, Immediate(kDoubleSize));
3991 __ bind(&done);
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +01003992}
3993
3994
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003995void LCodeGen::DoMathClz32(LMathClz32* instr) {
3996 Register input = ToRegister(instr->value());
3997 Register result = ToRegister(instr->result());
3998 Label not_zero_input;
3999 __ bsrl(result, input);
Ben Murdochb8e0da22011-05-16 14:20:40 +01004000
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004001 __ j(not_zero, &not_zero_input);
4002 __ Set(result, 63); // 63^31 == 32
Ben Murdochb8e0da22011-05-16 14:20:40 +01004003
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004004 __ bind(&not_zero_input);
4005 __ xorl(result, Immediate(31)); // for x in [0..31], 31^x == 31-x.
Ben Murdochb8e0da22011-05-16 14:20:40 +01004006}
4007
4008
Ben Murdoch257744e2011-11-30 15:57:28 +00004009void LCodeGen::DoInvokeFunction(LInvokeFunction* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004010 DCHECK(ToRegister(instr->context()).is(rsi));
4011 DCHECK(ToRegister(instr->function()).is(rdi));
4012 DCHECK(instr->HasPointerMap());
Ben Murdoch257744e2011-11-30 15:57:28 +00004013
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004014 Handle<JSFunction> known_function = instr->hydrogen()->known_function();
4015 if (known_function.is_null()) {
4016 LPointerMap* pointers = instr->pointer_map();
4017 SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
4018 ParameterCount count(instr->arity());
4019 __ InvokeFunction(rdi, count, CALL_FUNCTION, generator);
4020 } else {
4021 CallKnownFunction(known_function,
4022 instr->hydrogen()->formal_parameter_count(),
4023 instr->arity(),
4024 instr,
4025 RDI_CONTAINS_TARGET);
4026 }
Ben Murdochb8e0da22011-05-16 14:20:40 +01004027}
4028
4029
4030void LCodeGen::DoCallFunction(LCallFunction* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004031 DCHECK(ToRegister(instr->context()).is(rsi));
4032 DCHECK(ToRegister(instr->function()).is(rdi));
4033 DCHECK(ToRegister(instr->result()).is(rax));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004034
4035 int arity = instr->arity();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004036 CallFunctionStub stub(isolate(), arity, instr->hydrogen()->function_flags());
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004037 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
Ben Murdochb8e0da22011-05-16 14:20:40 +01004038}
4039
4040
4041void LCodeGen::DoCallNew(LCallNew* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004042 DCHECK(ToRegister(instr->context()).is(rsi));
4043 DCHECK(ToRegister(instr->constructor()).is(rdi));
4044 DCHECK(ToRegister(instr->result()).is(rax));
Steve Block1e0659c2011-05-24 12:43:12 +01004045
Steve Block1e0659c2011-05-24 12:43:12 +01004046 __ Set(rax, instr->arity());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004047 // No cell in ebx for construct type feedback in optimized code
4048 __ LoadRoot(rbx, Heap::kUndefinedValueRootIndex);
4049 CallConstructStub stub(isolate(), NO_CALL_CONSTRUCTOR_FLAGS);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004050 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
Ben Murdochb8e0da22011-05-16 14:20:40 +01004051}
4052
4053
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004054void LCodeGen::DoCallNewArray(LCallNewArray* instr) {
4055 DCHECK(ToRegister(instr->context()).is(rsi));
4056 DCHECK(ToRegister(instr->constructor()).is(rdi));
4057 DCHECK(ToRegister(instr->result()).is(rax));
4058
4059 __ Set(rax, instr->arity());
4060 __ LoadRoot(rbx, Heap::kUndefinedValueRootIndex);
4061 ElementsKind kind = instr->hydrogen()->elements_kind();
4062 AllocationSiteOverrideMode override_mode =
4063 (AllocationSite::GetMode(kind) == TRACK_ALLOCATION_SITE)
4064 ? DISABLE_ALLOCATION_SITES
4065 : DONT_OVERRIDE;
4066
4067 if (instr->arity() == 0) {
4068 ArrayNoArgumentConstructorStub stub(isolate(), kind, override_mode);
4069 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
4070 } else if (instr->arity() == 1) {
4071 Label done;
4072 if (IsFastPackedElementsKind(kind)) {
4073 Label packed_case;
4074 // We might need a change here
4075 // look at the first argument
4076 __ movp(rcx, Operand(rsp, 0));
4077 __ testp(rcx, rcx);
4078 __ j(zero, &packed_case, Label::kNear);
4079
4080 ElementsKind holey_kind = GetHoleyElementsKind(kind);
4081 ArraySingleArgumentConstructorStub stub(isolate(),
4082 holey_kind,
4083 override_mode);
4084 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
4085 __ jmp(&done, Label::kNear);
4086 __ bind(&packed_case);
4087 }
4088
4089 ArraySingleArgumentConstructorStub stub(isolate(), kind, override_mode);
4090 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
4091 __ bind(&done);
4092 } else {
4093 ArrayNArgumentsConstructorStub stub(isolate(), kind, override_mode);
4094 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
4095 }
4096}
4097
4098
Ben Murdochb8e0da22011-05-16 14:20:40 +01004099void LCodeGen::DoCallRuntime(LCallRuntime* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004100 DCHECK(ToRegister(instr->context()).is(rsi));
4101 CallRuntime(instr->function(), instr->arity(), instr, instr->save_doubles());
4102}
4103
4104
4105void LCodeGen::DoStoreCodeEntry(LStoreCodeEntry* instr) {
4106 Register function = ToRegister(instr->function());
4107 Register code_object = ToRegister(instr->code_object());
4108 __ leap(code_object, FieldOperand(code_object, Code::kHeaderSize));
4109 __ movp(FieldOperand(function, JSFunction::kCodeEntryOffset), code_object);
4110}
4111
4112
4113void LCodeGen::DoInnerAllocatedObject(LInnerAllocatedObject* instr) {
4114 Register result = ToRegister(instr->result());
4115 Register base = ToRegister(instr->base_object());
4116 if (instr->offset()->IsConstantOperand()) {
4117 LConstantOperand* offset = LConstantOperand::cast(instr->offset());
4118 __ leap(result, Operand(base, ToInteger32(offset)));
4119 } else {
4120 Register offset = ToRegister(instr->offset());
4121 __ leap(result, Operand(base, offset, times_1, 0));
4122 }
Ben Murdochb8e0da22011-05-16 14:20:40 +01004123}
4124
4125
4126void LCodeGen::DoStoreNamedField(LStoreNamedField* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004127 HStoreNamedField* hinstr = instr->hydrogen();
4128 Representation representation = instr->representation();
Steve Block1e0659c2011-05-24 12:43:12 +01004129
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004130 HObjectAccess access = hinstr->access();
4131 int offset = access.offset();
4132
4133 if (access.IsExternalMemory()) {
4134 DCHECK(!hinstr->NeedsWriteBarrier());
4135 Register value = ToRegister(instr->value());
4136 if (instr->object()->IsConstantOperand()) {
4137 DCHECK(value.is(rax));
4138 LConstantOperand* object = LConstantOperand::cast(instr->object());
4139 __ store_rax(ToExternalReference(object));
4140 } else {
4141 Register object = ToRegister(instr->object());
4142 __ Store(MemOperand(object, offset), value, representation);
4143 }
4144 return;
4145 }
4146
4147 Register object = ToRegister(instr->object());
4148 __ AssertNotSmi(object);
4149
4150 DCHECK(!representation.IsSmi() ||
4151 !instr->value()->IsConstantOperand() ||
4152 IsInteger32Constant(LConstantOperand::cast(instr->value())));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004153 if (!FLAG_unbox_double_fields && representation.IsDouble()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004154 DCHECK(access.IsInobject());
4155 DCHECK(!hinstr->has_transition());
4156 DCHECK(!hinstr->NeedsWriteBarrier());
4157 XMMRegister value = ToDoubleRegister(instr->value());
4158 __ movsd(FieldOperand(object, offset), value);
4159 return;
4160 }
4161
4162 if (hinstr->has_transition()) {
4163 Handle<Map> transition = hinstr->transition_map();
4164 AddDeprecationDependency(transition);
4165 if (!hinstr->NeedsWriteBarrierForMap()) {
4166 __ Move(FieldOperand(object, HeapObject::kMapOffset), transition);
4167 } else {
4168 Register temp = ToRegister(instr->temp());
4169 __ Move(kScratchRegister, transition);
4170 __ movp(FieldOperand(object, HeapObject::kMapOffset), kScratchRegister);
4171 // Update the write barrier for the map field.
4172 __ RecordWriteForMap(object,
4173 kScratchRegister,
4174 temp,
4175 kSaveFPRegs);
4176 }
Steve Block1e0659c2011-05-24 12:43:12 +01004177 }
4178
4179 // Do the store.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004180 Register write_register = object;
4181 if (!access.IsInobject()) {
4182 write_register = ToRegister(instr->temp());
4183 __ movp(write_register, FieldOperand(object, JSObject::kPropertiesOffset));
4184 }
4185
4186 if (representation.IsSmi() && SmiValuesAre32Bits() &&
4187 hinstr->value()->representation().IsInteger32()) {
4188 DCHECK(hinstr->store_mode() == STORE_TO_INITIALIZED_ENTRY);
4189 if (FLAG_debug_code) {
4190 Register scratch = kScratchRegister;
4191 __ Load(scratch, FieldOperand(write_register, offset), representation);
4192 __ AssertSmi(scratch);
Steve Block1e0659c2011-05-24 12:43:12 +01004193 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004194 // Store int value directly to upper half of the smi.
4195 STATIC_ASSERT(kSmiTag == 0);
4196 DCHECK(kSmiTagSize + kSmiShiftSize == 32);
4197 offset += kPointerSize / 2;
4198 representation = Representation::Integer32();
4199 }
4200
4201 Operand operand = FieldOperand(write_register, offset);
4202
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004203 if (FLAG_unbox_double_fields && representation.IsDouble()) {
4204 DCHECK(access.IsInobject());
4205 XMMRegister value = ToDoubleRegister(instr->value());
4206 __ movsd(operand, value);
4207
4208 } else if (instr->value()->IsRegister()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004209 Register value = ToRegister(instr->value());
4210 __ Store(operand, value, representation);
Steve Block1e0659c2011-05-24 12:43:12 +01004211 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004212 LConstantOperand* operand_value = LConstantOperand::cast(instr->value());
4213 if (IsInteger32Constant(operand_value)) {
4214 DCHECK(!hinstr->NeedsWriteBarrier());
4215 int32_t value = ToInteger32(operand_value);
4216 if (representation.IsSmi()) {
4217 __ Move(operand, Smi::FromInt(value));
4218
4219 } else {
4220 __ movl(operand, Immediate(value));
4221 }
4222
4223 } else {
4224 Handle<Object> handle_value = ToHandle(operand_value);
4225 DCHECK(!hinstr->NeedsWriteBarrier());
4226 __ Move(operand, handle_value);
Steve Block1e0659c2011-05-24 12:43:12 +01004227 }
4228 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004229
4230 if (hinstr->NeedsWriteBarrier()) {
4231 Register value = ToRegister(instr->value());
4232 Register temp = access.IsInobject() ? ToRegister(instr->temp()) : object;
4233 // Update the write barrier for the object for in-object properties.
4234 __ RecordWriteField(write_register,
4235 offset,
4236 value,
4237 temp,
4238 kSaveFPRegs,
4239 EMIT_REMEMBERED_SET,
4240 hinstr->SmiCheckForWriteBarrier(),
4241 hinstr->PointersToHereCheckForValue());
4242 }
Ben Murdochb8e0da22011-05-16 14:20:40 +01004243}
4244
4245
4246void LCodeGen::DoStoreNamedGeneric(LStoreNamedGeneric* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004247 DCHECK(ToRegister(instr->context()).is(rsi));
4248 DCHECK(ToRegister(instr->object()).is(StoreDescriptor::ReceiverRegister()));
4249 DCHECK(ToRegister(instr->value()).is(StoreDescriptor::ValueRegister()));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004250
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004251 __ Move(StoreDescriptor::NameRegister(), instr->hydrogen()->name());
4252 Handle<Code> ic = StoreIC::initialize_stub(isolate(), instr->strict_mode());
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004253 CallCode(ic, RelocInfo::CODE_TARGET, instr);
4254}
4255
4256
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004257void LCodeGen::DoBoundsCheck(LBoundsCheck* instr) {
4258 Representation representation = instr->hydrogen()->length()->representation();
4259 DCHECK(representation.Equals(instr->hydrogen()->index()->representation()));
4260 DCHECK(representation.IsSmiOrInteger32());
4261
4262 Condition cc = instr->hydrogen()->allow_equality() ? below : below_equal;
4263 if (instr->length()->IsConstantOperand()) {
4264 int32_t length = ToInteger32(LConstantOperand::cast(instr->length()));
4265 Register index = ToRegister(instr->index());
4266 if (representation.IsSmi()) {
4267 __ Cmp(index, Smi::FromInt(length));
4268 } else {
4269 __ cmpl(index, Immediate(length));
4270 }
4271 cc = CommuteCondition(cc);
4272 } else if (instr->index()->IsConstantOperand()) {
4273 int32_t index = ToInteger32(LConstantOperand::cast(instr->index()));
4274 if (instr->length()->IsRegister()) {
4275 Register length = ToRegister(instr->length());
4276 if (representation.IsSmi()) {
4277 __ Cmp(length, Smi::FromInt(index));
4278 } else {
4279 __ cmpl(length, Immediate(index));
4280 }
4281 } else {
4282 Operand length = ToOperand(instr->length());
4283 if (representation.IsSmi()) {
4284 __ Cmp(length, Smi::FromInt(index));
4285 } else {
4286 __ cmpl(length, Immediate(index));
4287 }
4288 }
4289 } else {
4290 Register index = ToRegister(instr->index());
4291 if (instr->length()->IsRegister()) {
4292 Register length = ToRegister(instr->length());
4293 if (representation.IsSmi()) {
4294 __ cmpp(length, index);
4295 } else {
4296 __ cmpl(length, index);
4297 }
4298 } else {
4299 Operand length = ToOperand(instr->length());
4300 if (representation.IsSmi()) {
4301 __ cmpp(length, index);
4302 } else {
4303 __ cmpl(length, index);
4304 }
4305 }
4306 }
4307 if (FLAG_debug_code && instr->hydrogen()->skip_check()) {
4308 Label done;
4309 __ j(NegateCondition(cc), &done, Label::kNear);
4310 __ int3();
4311 __ bind(&done);
4312 } else {
4313 DeoptimizeIf(cc, instr, "out of bounds");
4314 }
4315}
4316
4317
4318void LCodeGen::DoStoreKeyedExternalArray(LStoreKeyed* instr) {
Ben Murdoch589d6972011-11-30 16:04:58 +00004319 ElementsKind elements_kind = instr->elements_kind();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004320 LOperand* key = instr->key();
4321 if (kPointerSize == kInt32Size && !key->IsConstantOperand()) {
4322 Register key_reg = ToRegister(key);
4323 Representation key_representation =
4324 instr->hydrogen()->key()->representation();
4325 if (ExternalArrayOpRequiresTemp(key_representation, elements_kind)) {
4326 __ SmiToInteger64(key_reg, key_reg);
4327 } else if (instr->hydrogen()->IsDehoisted()) {
4328 // Sign extend key because it could be a 32 bit negative value
4329 // and the dehoisted address computation happens in 64 bits
4330 __ movsxlq(key_reg, key_reg);
4331 }
4332 }
4333 Operand operand(BuildFastArrayOperand(
4334 instr->elements(),
4335 key,
4336 instr->hydrogen()->key()->representation(),
4337 elements_kind,
4338 instr->base_offset()));
4339
4340 if (elements_kind == EXTERNAL_FLOAT32_ELEMENTS ||
4341 elements_kind == FLOAT32_ELEMENTS) {
Steve Block44f0eee2011-05-26 01:26:41 +01004342 XMMRegister value(ToDoubleRegister(instr->value()));
4343 __ cvtsd2ss(value, value);
Ben Murdoch257744e2011-11-30 15:57:28 +00004344 __ movss(operand, value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004345 } else if (elements_kind == EXTERNAL_FLOAT64_ELEMENTS ||
4346 elements_kind == FLOAT64_ELEMENTS) {
Ben Murdoch257744e2011-11-30 15:57:28 +00004347 __ movsd(operand, ToDoubleRegister(instr->value()));
Steve Block44f0eee2011-05-26 01:26:41 +01004348 } else {
4349 Register value(ToRegister(instr->value()));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004350 switch (elements_kind) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004351 case EXTERNAL_UINT8_CLAMPED_ELEMENTS:
4352 case EXTERNAL_INT8_ELEMENTS:
4353 case EXTERNAL_UINT8_ELEMENTS:
4354 case INT8_ELEMENTS:
4355 case UINT8_ELEMENTS:
4356 case UINT8_CLAMPED_ELEMENTS:
Ben Murdoch257744e2011-11-30 15:57:28 +00004357 __ movb(operand, value);
Steve Block44f0eee2011-05-26 01:26:41 +01004358 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004359 case EXTERNAL_INT16_ELEMENTS:
4360 case EXTERNAL_UINT16_ELEMENTS:
4361 case INT16_ELEMENTS:
4362 case UINT16_ELEMENTS:
Ben Murdoch257744e2011-11-30 15:57:28 +00004363 __ movw(operand, value);
Steve Block44f0eee2011-05-26 01:26:41 +01004364 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004365 case EXTERNAL_INT32_ELEMENTS:
4366 case EXTERNAL_UINT32_ELEMENTS:
4367 case INT32_ELEMENTS:
4368 case UINT32_ELEMENTS:
Ben Murdoch257744e2011-11-30 15:57:28 +00004369 __ movl(operand, value);
Steve Block44f0eee2011-05-26 01:26:41 +01004370 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004371 case EXTERNAL_FLOAT32_ELEMENTS:
4372 case EXTERNAL_FLOAT64_ELEMENTS:
4373 case FLOAT32_ELEMENTS:
4374 case FLOAT64_ELEMENTS:
Ben Murdoch589d6972011-11-30 16:04:58 +00004375 case FAST_ELEMENTS:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004376 case FAST_SMI_ELEMENTS:
Ben Murdoch589d6972011-11-30 16:04:58 +00004377 case FAST_DOUBLE_ELEMENTS:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004378 case FAST_HOLEY_ELEMENTS:
4379 case FAST_HOLEY_SMI_ELEMENTS:
4380 case FAST_HOLEY_DOUBLE_ELEMENTS:
Ben Murdoch589d6972011-11-30 16:04:58 +00004381 case DICTIONARY_ELEMENTS:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004382 case SLOPPY_ARGUMENTS_ELEMENTS:
Steve Block44f0eee2011-05-26 01:26:41 +01004383 UNREACHABLE();
4384 break;
4385 }
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004386 }
Ben Murdochb8e0da22011-05-16 14:20:40 +01004387}
4388
4389
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004390void LCodeGen::DoStoreKeyedFixedDoubleArray(LStoreKeyed* instr) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004391 XMMRegister value = ToDoubleRegister(instr->value());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004392 LOperand* key = instr->key();
4393 if (kPointerSize == kInt32Size && !key->IsConstantOperand() &&
4394 instr->hydrogen()->IsDehoisted()) {
4395 // Sign extend key because it could be a 32 bit negative value
4396 // and the dehoisted address computation happens in 64 bits
4397 __ movsxlq(ToRegister(key), ToRegister(key));
4398 }
4399 if (instr->NeedsCanonicalization()) {
4400 Label have_value;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004401
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004402 __ ucomisd(value, value);
4403 __ j(parity_odd, &have_value, Label::kNear); // NaN.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004404
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004405 __ Set(kScratchRegister,
4406 bit_cast<uint64_t>(
4407 FixedDoubleArray::canonical_not_the_hole_nan_as_double()));
4408 __ movq(value, kScratchRegister);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004409
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004410 __ bind(&have_value);
4411 }
4412
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004413 Operand double_store_operand = BuildFastArrayOperand(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004414 instr->elements(),
4415 key,
4416 instr->hydrogen()->key()->representation(),
4417 FAST_DOUBLE_ELEMENTS,
4418 instr->base_offset());
4419
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004420 __ movsd(double_store_operand, value);
4421}
4422
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004423
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004424void LCodeGen::DoStoreKeyedFixedArray(LStoreKeyed* instr) {
4425 HStoreKeyed* hinstr = instr->hydrogen();
4426 LOperand* key = instr->key();
4427 int offset = instr->base_offset();
4428 Representation representation = hinstr->value()->representation();
4429
4430 if (kPointerSize == kInt32Size && !key->IsConstantOperand() &&
4431 instr->hydrogen()->IsDehoisted()) {
4432 // Sign extend key because it could be a 32 bit negative value
4433 // and the dehoisted address computation happens in 64 bits
4434 __ movsxlq(ToRegister(key), ToRegister(key));
4435 }
4436 if (representation.IsInteger32() && SmiValuesAre32Bits()) {
4437 DCHECK(hinstr->store_mode() == STORE_TO_INITIALIZED_ENTRY);
4438 DCHECK(hinstr->elements_kind() == FAST_SMI_ELEMENTS);
4439 if (FLAG_debug_code) {
4440 Register scratch = kScratchRegister;
4441 __ Load(scratch,
4442 BuildFastArrayOperand(instr->elements(),
4443 key,
4444 instr->hydrogen()->key()->representation(),
4445 FAST_ELEMENTS,
4446 offset),
4447 Representation::Smi());
4448 __ AssertSmi(scratch);
4449 }
4450 // Store int value directly to upper half of the smi.
4451 STATIC_ASSERT(kSmiTag == 0);
4452 DCHECK(kSmiTagSize + kSmiShiftSize == 32);
4453 offset += kPointerSize / 2;
4454 }
4455
4456 Operand operand =
4457 BuildFastArrayOperand(instr->elements(),
4458 key,
4459 instr->hydrogen()->key()->representation(),
4460 FAST_ELEMENTS,
4461 offset);
4462 if (instr->value()->IsRegister()) {
4463 __ Store(operand, ToRegister(instr->value()), representation);
4464 } else {
4465 LConstantOperand* operand_value = LConstantOperand::cast(instr->value());
4466 if (IsInteger32Constant(operand_value)) {
4467 int32_t value = ToInteger32(operand_value);
4468 if (representation.IsSmi()) {
4469 __ Move(operand, Smi::FromInt(value));
4470
4471 } else {
4472 __ movl(operand, Immediate(value));
4473 }
4474 } else {
4475 Handle<Object> handle_value = ToHandle(operand_value);
4476 __ Move(operand, handle_value);
4477 }
4478 }
4479
4480 if (hinstr->NeedsWriteBarrier()) {
4481 Register elements = ToRegister(instr->elements());
4482 DCHECK(instr->value()->IsRegister());
4483 Register value = ToRegister(instr->value());
4484 DCHECK(!key->IsConstantOperand());
4485 SmiCheck check_needed = hinstr->value()->type().IsHeapObject()
4486 ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
4487 // Compute address of modified element and store it into key register.
4488 Register key_reg(ToRegister(key));
4489 __ leap(key_reg, operand);
4490 __ RecordWrite(elements,
4491 key_reg,
4492 value,
4493 kSaveFPRegs,
4494 EMIT_REMEMBERED_SET,
4495 check_needed,
4496 hinstr->PointersToHereCheckForValue());
4497 }
4498}
4499
4500
4501void LCodeGen::DoStoreKeyed(LStoreKeyed* instr) {
4502 if (instr->is_typed_elements()) {
4503 DoStoreKeyedExternalArray(instr);
4504 } else if (instr->hydrogen()->value()->representation().IsDouble()) {
4505 DoStoreKeyedFixedDoubleArray(instr);
4506 } else {
4507 DoStoreKeyedFixedArray(instr);
4508 }
4509}
4510
4511
4512void LCodeGen::DoStoreKeyedGeneric(LStoreKeyedGeneric* instr) {
4513 DCHECK(ToRegister(instr->context()).is(rsi));
4514 DCHECK(ToRegister(instr->object()).is(StoreDescriptor::ReceiverRegister()));
4515 DCHECK(ToRegister(instr->key()).is(StoreDescriptor::NameRegister()));
4516 DCHECK(ToRegister(instr->value()).is(StoreDescriptor::ValueRegister()));
4517
4518 Handle<Code> ic =
4519 CodeFactory::KeyedStoreIC(isolate(), instr->strict_mode()).code();
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004520 CallCode(ic, RelocInfo::CODE_TARGET, instr);
4521}
4522
4523
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004524void LCodeGen::DoTransitionElementsKind(LTransitionElementsKind* instr) {
4525 Register object_reg = ToRegister(instr->object());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004526
4527 Handle<Map> from_map = instr->original_map();
4528 Handle<Map> to_map = instr->transitioned_map();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004529 ElementsKind from_kind = instr->from_kind();
4530 ElementsKind to_kind = instr->to_kind();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004531
4532 Label not_applicable;
4533 __ Cmp(FieldOperand(object_reg, HeapObject::kMapOffset), from_map);
4534 __ j(not_equal, &not_applicable);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004535 if (IsSimpleMapChangeTransition(from_kind, to_kind)) {
4536 Register new_map_reg = ToRegister(instr->new_map_temp());
4537 __ Move(new_map_reg, to_map, RelocInfo::EMBEDDED_OBJECT);
4538 __ movp(FieldOperand(object_reg, HeapObject::kMapOffset), new_map_reg);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004539 // Write barrier.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004540 __ RecordWriteForMap(object_reg, new_map_reg, ToRegister(instr->temp()),
4541 kDontSaveFPRegs);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004542 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004543 DCHECK(object_reg.is(rax));
4544 DCHECK(ToRegister(instr->context()).is(rsi));
4545 PushSafepointRegistersScope scope(this);
4546 __ Move(rbx, to_map);
4547 bool is_js_array = from_map->instance_type() == JS_ARRAY_TYPE;
4548 TransitionElementsKindStub stub(isolate(), from_kind, to_kind, is_js_array);
4549 __ CallStub(&stub);
4550 RecordSafepointWithLazyDeopt(instr, RECORD_SAFEPOINT_WITH_REGISTERS, 0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004551 }
4552 __ bind(&not_applicable);
4553}
4554
4555
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004556void LCodeGen::DoTrapAllocationMemento(LTrapAllocationMemento* instr) {
4557 Register object = ToRegister(instr->object());
4558 Register temp = ToRegister(instr->temp());
4559 Label no_memento_found;
4560 __ TestJSArrayForAllocationMemento(object, temp, &no_memento_found);
4561 DeoptimizeIf(equal, instr, "memento found");
4562 __ bind(&no_memento_found);
4563}
4564
4565
Ben Murdoch257744e2011-11-30 15:57:28 +00004566void LCodeGen::DoStringAdd(LStringAdd* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004567 DCHECK(ToRegister(instr->context()).is(rsi));
4568 DCHECK(ToRegister(instr->left()).is(rdx));
4569 DCHECK(ToRegister(instr->right()).is(rax));
4570 StringAddStub stub(isolate(),
4571 instr->hydrogen()->flags(),
4572 instr->hydrogen()->pretenure_flag());
Ben Murdoch257744e2011-11-30 15:57:28 +00004573 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
4574}
4575
4576
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004577void LCodeGen::DoStringCharCodeAt(LStringCharCodeAt* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004578 class DeferredStringCharCodeAt FINAL : public LDeferredCode {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004579 public:
4580 DeferredStringCharCodeAt(LCodeGen* codegen, LStringCharCodeAt* instr)
4581 : LDeferredCode(codegen), instr_(instr) { }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004582 void Generate() OVERRIDE { codegen()->DoDeferredStringCharCodeAt(instr_); }
4583 LInstruction* instr() OVERRIDE { return instr_; }
4584
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004585 private:
4586 LStringCharCodeAt* instr_;
4587 };
4588
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004589 DeferredStringCharCodeAt* deferred =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004590 new(zone()) DeferredStringCharCodeAt(this, instr);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004591
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004592 StringCharLoadGenerator::Generate(masm(),
4593 ToRegister(instr->string()),
4594 ToRegister(instr->index()),
4595 ToRegister(instr->result()),
4596 deferred->entry());
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004597 __ bind(deferred->exit());
4598}
4599
4600
4601void LCodeGen::DoDeferredStringCharCodeAt(LStringCharCodeAt* instr) {
4602 Register string = ToRegister(instr->string());
4603 Register result = ToRegister(instr->result());
4604
4605 // TODO(3095996): Get rid of this. For now, we need to make the
4606 // result register contain a valid pointer because it is already
4607 // contained in the register pointer map.
4608 __ Set(result, 0);
4609
Ben Murdoch8b112d22011-06-08 16:22:53 +01004610 PushSafepointRegistersScope scope(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004611 __ Push(string);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004612 // Push the index as a smi. This is safe because of the checks in
4613 // DoStringCharCodeAt above.
4614 STATIC_ASSERT(String::kMaxLength <= Smi::kMaxValue);
4615 if (instr->index()->IsConstantOperand()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004616 int32_t const_index = ToInteger32(LConstantOperand::cast(instr->index()));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004617 __ Push(Smi::FromInt(const_index));
4618 } else {
4619 Register index = ToRegister(instr->index());
4620 __ Integer32ToSmi(index, index);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004621 __ Push(index);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004622 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004623 CallRuntimeFromDeferred(
4624 Runtime::kStringCharCodeAtRT, 2, instr, instr->context());
4625 __ AssertSmi(rax);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004626 __ SmiToInteger32(rax, rax);
4627 __ StoreToSafepointRegisterSlot(result, rax);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004628}
4629
4630
Steve Block44f0eee2011-05-26 01:26:41 +01004631void LCodeGen::DoStringCharFromCode(LStringCharFromCode* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004632 class DeferredStringCharFromCode FINAL : public LDeferredCode {
Steve Block44f0eee2011-05-26 01:26:41 +01004633 public:
4634 DeferredStringCharFromCode(LCodeGen* codegen, LStringCharFromCode* instr)
4635 : LDeferredCode(codegen), instr_(instr) { }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004636 void Generate() OVERRIDE {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004637 codegen()->DoDeferredStringCharFromCode(instr_);
4638 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004639 LInstruction* instr() OVERRIDE { return instr_; }
4640
Steve Block44f0eee2011-05-26 01:26:41 +01004641 private:
4642 LStringCharFromCode* instr_;
4643 };
4644
4645 DeferredStringCharFromCode* deferred =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004646 new(zone()) DeferredStringCharFromCode(this, instr);
Steve Block44f0eee2011-05-26 01:26:41 +01004647
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004648 DCHECK(instr->hydrogen()->value()->representation().IsInteger32());
Steve Block44f0eee2011-05-26 01:26:41 +01004649 Register char_code = ToRegister(instr->char_code());
4650 Register result = ToRegister(instr->result());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004651 DCHECK(!char_code.is(result));
Steve Block44f0eee2011-05-26 01:26:41 +01004652
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004653 __ cmpl(char_code, Immediate(String::kMaxOneByteCharCode));
Steve Block44f0eee2011-05-26 01:26:41 +01004654 __ j(above, deferred->entry());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004655 __ movsxlq(char_code, char_code);
Steve Block44f0eee2011-05-26 01:26:41 +01004656 __ LoadRoot(result, Heap::kSingleCharacterStringCacheRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004657 __ movp(result, FieldOperand(result,
Steve Block44f0eee2011-05-26 01:26:41 +01004658 char_code, times_pointer_size,
4659 FixedArray::kHeaderSize));
4660 __ CompareRoot(result, Heap::kUndefinedValueRootIndex);
4661 __ j(equal, deferred->entry());
4662 __ bind(deferred->exit());
4663}
4664
4665
4666void LCodeGen::DoDeferredStringCharFromCode(LStringCharFromCode* instr) {
4667 Register char_code = ToRegister(instr->char_code());
4668 Register result = ToRegister(instr->result());
4669
4670 // TODO(3095996): Get rid of this. For now, we need to make the
4671 // result register contain a valid pointer because it is already
4672 // contained in the register pointer map.
4673 __ Set(result, 0);
4674
Ben Murdoch8b112d22011-06-08 16:22:53 +01004675 PushSafepointRegistersScope scope(this);
Steve Block44f0eee2011-05-26 01:26:41 +01004676 __ Integer32ToSmi(char_code, char_code);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004677 __ Push(char_code);
4678 CallRuntimeFromDeferred(Runtime::kCharFromCode, 1, instr, instr->context());
Steve Block44f0eee2011-05-26 01:26:41 +01004679 __ StoreToSafepointRegisterSlot(result, rax);
Steve Block44f0eee2011-05-26 01:26:41 +01004680}
4681
4682
Ben Murdochb8e0da22011-05-16 14:20:40 +01004683void LCodeGen::DoInteger32ToDouble(LInteger32ToDouble* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004684 LOperand* input = instr->value();
4685 DCHECK(input->IsRegister() || input->IsStackSlot());
Steve Block1e0659c2011-05-24 12:43:12 +01004686 LOperand* output = instr->result();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004687 DCHECK(output->IsDoubleRegister());
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004688 if (input->IsRegister()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004689 __ Cvtlsi2sd(ToDoubleRegister(output), ToRegister(input));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004690 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004691 __ Cvtlsi2sd(ToDoubleRegister(output), ToOperand(input));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004692 }
Ben Murdochb8e0da22011-05-16 14:20:40 +01004693}
4694
4695
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004696void LCodeGen::DoUint32ToDouble(LUint32ToDouble* instr) {
4697 LOperand* input = instr->value();
4698 LOperand* output = instr->result();
4699
4700 __ LoadUint32(ToDoubleRegister(output), ToRegister(input));
4701}
4702
4703
Ben Murdochb8e0da22011-05-16 14:20:40 +01004704void LCodeGen::DoNumberTagI(LNumberTagI* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004705 class DeferredNumberTagI FINAL : public LDeferredCode {
4706 public:
4707 DeferredNumberTagI(LCodeGen* codegen, LNumberTagI* instr)
4708 : LDeferredCode(codegen), instr_(instr) { }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004709 void Generate() OVERRIDE {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004710 codegen()->DoDeferredNumberTagIU(instr_, instr_->value(), instr_->temp1(),
4711 instr_->temp2(), SIGNED_INT32);
4712 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004713 LInstruction* instr() OVERRIDE { return instr_; }
4714
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004715 private:
4716 LNumberTagI* instr_;
4717 };
4718
4719 LOperand* input = instr->value();
4720 DCHECK(input->IsRegister() && input->Equals(instr->result()));
Steve Block1e0659c2011-05-24 12:43:12 +01004721 Register reg = ToRegister(input);
Ben Murdochb8e0da22011-05-16 14:20:40 +01004722
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004723 if (SmiValuesAre32Bits()) {
4724 __ Integer32ToSmi(reg, reg);
4725 } else {
4726 DCHECK(SmiValuesAre31Bits());
4727 DeferredNumberTagI* deferred = new(zone()) DeferredNumberTagI(this, instr);
4728 __ Integer32ToSmi(reg, reg);
4729 __ j(overflow, deferred->entry());
4730 __ bind(deferred->exit());
4731 }
4732}
4733
4734
4735void LCodeGen::DoNumberTagU(LNumberTagU* instr) {
4736 class DeferredNumberTagU FINAL : public LDeferredCode {
4737 public:
4738 DeferredNumberTagU(LCodeGen* codegen, LNumberTagU* instr)
4739 : LDeferredCode(codegen), instr_(instr) { }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004740 void Generate() OVERRIDE {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004741 codegen()->DoDeferredNumberTagIU(instr_, instr_->value(), instr_->temp1(),
4742 instr_->temp2(), UNSIGNED_INT32);
4743 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004744 LInstruction* instr() OVERRIDE { return instr_; }
4745
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004746 private:
4747 LNumberTagU* instr_;
4748 };
4749
4750 LOperand* input = instr->value();
4751 DCHECK(input->IsRegister() && input->Equals(instr->result()));
4752 Register reg = ToRegister(input);
4753
4754 DeferredNumberTagU* deferred = new(zone()) DeferredNumberTagU(this, instr);
4755 __ cmpl(reg, Immediate(Smi::kMaxValue));
4756 __ j(above, deferred->entry());
Steve Block1e0659c2011-05-24 12:43:12 +01004757 __ Integer32ToSmi(reg, reg);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004758 __ bind(deferred->exit());
4759}
4760
4761
4762void LCodeGen::DoDeferredNumberTagIU(LInstruction* instr,
4763 LOperand* value,
4764 LOperand* temp1,
4765 LOperand* temp2,
4766 IntegerSignedness signedness) {
4767 Label done, slow;
4768 Register reg = ToRegister(value);
4769 Register tmp = ToRegister(temp1);
4770 XMMRegister temp_xmm = ToDoubleRegister(temp2);
4771
4772 // Load value into temp_xmm which will be preserved across potential call to
4773 // runtime (MacroAssembler::EnterExitFrameEpilogue preserves only allocatable
4774 // XMM registers on x64).
4775 if (signedness == SIGNED_INT32) {
4776 DCHECK(SmiValuesAre31Bits());
4777 // There was overflow, so bits 30 and 31 of the original integer
4778 // disagree. Try to allocate a heap number in new space and store
4779 // the value in there. If that fails, call the runtime system.
4780 __ SmiToInteger32(reg, reg);
4781 __ xorl(reg, Immediate(0x80000000));
4782 __ cvtlsi2sd(temp_xmm, reg);
4783 } else {
4784 DCHECK(signedness == UNSIGNED_INT32);
4785 __ LoadUint32(temp_xmm, reg);
4786 }
4787
4788 if (FLAG_inline_new) {
4789 __ AllocateHeapNumber(reg, tmp, &slow);
4790 __ jmp(&done, kPointerSize == kInt64Size ? Label::kNear : Label::kFar);
4791 }
4792
4793 // Slow case: Call the runtime system to do the number allocation.
4794 __ bind(&slow);
4795 {
4796 // Put a valid pointer value in the stack slot where the result
4797 // register is stored, as this register is in the pointer map, but contains
4798 // an integer value.
4799 __ Set(reg, 0);
4800
4801 // Preserve the value of all registers.
4802 PushSafepointRegistersScope scope(this);
4803
4804 // NumberTagIU uses the context from the frame, rather than
4805 // the environment's HContext or HInlinedContext value.
4806 // They only call Runtime::kAllocateHeapNumber.
4807 // The corresponding HChange instructions are added in a phase that does
4808 // not have easy access to the local context.
4809 __ movp(rsi, Operand(rbp, StandardFrameConstants::kContextOffset));
4810 __ CallRuntimeSaveDoubles(Runtime::kAllocateHeapNumber);
4811 RecordSafepointWithRegisters(
4812 instr->pointer_map(), 0, Safepoint::kNoLazyDeopt);
4813 __ StoreToSafepointRegisterSlot(reg, rax);
4814 }
4815
4816 // Done. Put the value in temp_xmm into the value of the allocated heap
4817 // number.
4818 __ bind(&done);
4819 __ movsd(FieldOperand(reg, HeapNumber::kValueOffset), temp_xmm);
Ben Murdochb8e0da22011-05-16 14:20:40 +01004820}
4821
4822
4823void LCodeGen::DoNumberTagD(LNumberTagD* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004824 class DeferredNumberTagD FINAL : public LDeferredCode {
Steve Block1e0659c2011-05-24 12:43:12 +01004825 public:
4826 DeferredNumberTagD(LCodeGen* codegen, LNumberTagD* instr)
4827 : LDeferredCode(codegen), instr_(instr) { }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004828 void Generate() OVERRIDE { codegen()->DoDeferredNumberTagD(instr_); }
4829 LInstruction* instr() OVERRIDE { return instr_; }
4830
Steve Block1e0659c2011-05-24 12:43:12 +01004831 private:
4832 LNumberTagD* instr_;
4833 };
4834
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004835 XMMRegister input_reg = ToDoubleRegister(instr->value());
Steve Block1e0659c2011-05-24 12:43:12 +01004836 Register reg = ToRegister(instr->result());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004837 Register tmp = ToRegister(instr->temp());
Steve Block1e0659c2011-05-24 12:43:12 +01004838
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004839 DeferredNumberTagD* deferred = new(zone()) DeferredNumberTagD(this, instr);
Steve Block1e0659c2011-05-24 12:43:12 +01004840 if (FLAG_inline_new) {
4841 __ AllocateHeapNumber(reg, tmp, deferred->entry());
4842 } else {
4843 __ jmp(deferred->entry());
4844 }
4845 __ bind(deferred->exit());
4846 __ movsd(FieldOperand(reg, HeapNumber::kValueOffset), input_reg);
Ben Murdochb8e0da22011-05-16 14:20:40 +01004847}
4848
4849
4850void LCodeGen::DoDeferredNumberTagD(LNumberTagD* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01004851 // TODO(3095996): Get rid of this. For now, we need to make the
4852 // result register contain a valid pointer because it is already
4853 // contained in the register pointer map.
4854 Register reg = ToRegister(instr->result());
4855 __ Move(reg, Smi::FromInt(0));
4856
Ben Murdoch8b112d22011-06-08 16:22:53 +01004857 {
4858 PushSafepointRegistersScope scope(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004859 // NumberTagD uses the context from the frame, rather than
4860 // the environment's HContext or HInlinedContext value.
4861 // They only call Runtime::kAllocateHeapNumber.
4862 // The corresponding HChange instructions are added in a phase that does
4863 // not have easy access to the local context.
4864 __ movp(rsi, Operand(rbp, StandardFrameConstants::kContextOffset));
4865 __ CallRuntimeSaveDoubles(Runtime::kAllocateHeapNumber);
4866 RecordSafepointWithRegisters(
4867 instr->pointer_map(), 0, Safepoint::kNoLazyDeopt);
4868 __ movp(kScratchRegister, rax);
Ben Murdoch8b112d22011-06-08 16:22:53 +01004869 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004870 __ movp(reg, kScratchRegister);
Ben Murdochb8e0da22011-05-16 14:20:40 +01004871}
4872
4873
4874void LCodeGen::DoSmiTag(LSmiTag* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004875 HChange* hchange = instr->hydrogen();
4876 Register input = ToRegister(instr->value());
4877 Register output = ToRegister(instr->result());
4878 if (hchange->CheckFlag(HValue::kCanOverflow) &&
4879 hchange->value()->CheckFlag(HValue::kUint32)) {
4880 Condition is_smi = __ CheckUInteger32ValidSmiValue(input);
4881 DeoptimizeIf(NegateCondition(is_smi), instr, "overflow");
4882 }
4883 __ Integer32ToSmi(output, input);
4884 if (hchange->CheckFlag(HValue::kCanOverflow) &&
4885 !hchange->value()->CheckFlag(HValue::kUint32)) {
4886 DeoptimizeIf(overflow, instr, "overflow");
4887 }
Ben Murdochb8e0da22011-05-16 14:20:40 +01004888}
4889
4890
4891void LCodeGen::DoSmiUntag(LSmiUntag* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004892 DCHECK(instr->value()->Equals(instr->result()));
4893 Register input = ToRegister(instr->value());
Steve Block1e0659c2011-05-24 12:43:12 +01004894 if (instr->needs_check()) {
4895 Condition is_smi = __ CheckSmi(input);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004896 DeoptimizeIf(NegateCondition(is_smi), instr, "not a Smi");
4897 } else {
4898 __ AssertSmi(input);
Steve Block1e0659c2011-05-24 12:43:12 +01004899 }
4900 __ SmiToInteger32(input, input);
Ben Murdochb8e0da22011-05-16 14:20:40 +01004901}
4902
4903
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004904void LCodeGen::EmitNumberUntagD(LNumberUntagD* instr, Register input_reg,
4905 XMMRegister result_reg, NumberUntagDMode mode) {
4906 bool can_convert_undefined_to_nan =
4907 instr->hydrogen()->can_convert_undefined_to_nan();
4908 bool deoptimize_on_minus_zero = instr->hydrogen()->deoptimize_on_minus_zero();
Steve Block1e0659c2011-05-24 12:43:12 +01004909
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004910 Label convert, load_smi, done;
Steve Block1e0659c2011-05-24 12:43:12 +01004911
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004912 if (mode == NUMBER_CANDIDATE_IS_ANY_TAGGED) {
4913 // Smi check.
4914 __ JumpIfSmi(input_reg, &load_smi, Label::kNear);
Ben Murdoch257744e2011-11-30 15:57:28 +00004915
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004916 // Heap number map check.
4917 __ CompareRoot(FieldOperand(input_reg, HeapObject::kMapOffset),
4918 Heap::kHeapNumberMapRootIndex);
Steve Block1e0659c2011-05-24 12:43:12 +01004919
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004920 // On x64 it is safe to load at heap number offset before evaluating the map
4921 // check, since all heap objects are at least two words long.
4922 __ movsd(result_reg, FieldOperand(input_reg, HeapNumber::kValueOffset));
4923
4924 if (can_convert_undefined_to_nan) {
4925 __ j(not_equal, &convert, Label::kNear);
4926 } else {
4927 DeoptimizeIf(not_equal, instr, "not a heap number");
4928 }
4929
4930 if (deoptimize_on_minus_zero) {
4931 XMMRegister xmm_scratch = double_scratch0();
4932 __ xorps(xmm_scratch, xmm_scratch);
4933 __ ucomisd(xmm_scratch, result_reg);
4934 __ j(not_equal, &done, Label::kNear);
4935 __ movmskpd(kScratchRegister, result_reg);
4936 __ testq(kScratchRegister, Immediate(1));
4937 DeoptimizeIf(not_zero, instr, "minus zero");
4938 }
Ben Murdoch257744e2011-11-30 15:57:28 +00004939 __ jmp(&done, Label::kNear);
Steve Block1e0659c2011-05-24 12:43:12 +01004940
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004941 if (can_convert_undefined_to_nan) {
4942 __ bind(&convert);
4943
4944 // Convert undefined (and hole) to NaN. Compute NaN as 0/0.
4945 __ CompareRoot(input_reg, Heap::kUndefinedValueRootIndex);
4946 DeoptimizeIf(not_equal, instr, "not a heap number/undefined");
4947
4948 __ xorps(result_reg, result_reg);
4949 __ divsd(result_reg, result_reg);
4950 __ jmp(&done, Label::kNear);
4951 }
4952 } else {
4953 DCHECK(mode == NUMBER_CANDIDATE_IS_SMI);
Ben Murdoch7d3e7fc2011-07-12 16:37:06 +01004954 }
Steve Block1e0659c2011-05-24 12:43:12 +01004955
4956 // Smi to XMM conversion
4957 __ bind(&load_smi);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004958 __ SmiToInteger32(kScratchRegister, input_reg);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004959 __ Cvtlsi2sd(result_reg, kScratchRegister);
Steve Block1e0659c2011-05-24 12:43:12 +01004960 __ bind(&done);
Ben Murdochb8e0da22011-05-16 14:20:40 +01004961}
4962
4963
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004964void LCodeGen::DoDeferredTaggedToI(LTaggedToI* instr, Label* done) {
4965 Register input_reg = ToRegister(instr->value());
Steve Block1e0659c2011-05-24 12:43:12 +01004966
4967 if (instr->truncating()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004968 Label no_heap_number, check_bools, check_false;
4969
4970 // Heap number map check.
4971 __ CompareRoot(FieldOperand(input_reg, HeapObject::kMapOffset),
4972 Heap::kHeapNumberMapRootIndex);
4973 __ j(not_equal, &no_heap_number, Label::kNear);
4974 __ TruncateHeapNumberToI(input_reg, input_reg);
4975 __ jmp(done);
4976
4977 __ bind(&no_heap_number);
4978 // Check for Oddballs. Undefined/False is converted to zero and True to one
4979 // for truncating conversions.
Steve Block1e0659c2011-05-24 12:43:12 +01004980 __ CompareRoot(input_reg, Heap::kUndefinedValueRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004981 __ j(not_equal, &check_bools, Label::kNear);
Ben Murdoch8b112d22011-06-08 16:22:53 +01004982 __ Set(input_reg, 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004983 __ jmp(done);
Steve Block1e0659c2011-05-24 12:43:12 +01004984
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004985 __ bind(&check_bools);
4986 __ CompareRoot(input_reg, Heap::kTrueValueRootIndex);
4987 __ j(not_equal, &check_false, Label::kNear);
4988 __ Set(input_reg, 1);
4989 __ jmp(done);
Steve Block1e0659c2011-05-24 12:43:12 +01004990
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004991 __ bind(&check_false);
4992 __ CompareRoot(input_reg, Heap::kFalseValueRootIndex);
4993 DeoptimizeIf(not_equal, instr, "not a heap number/undefined/true/false");
4994 __ Set(input_reg, 0);
Steve Block1e0659c2011-05-24 12:43:12 +01004995 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004996 XMMRegister scratch = ToDoubleRegister(instr->temp());
4997 DCHECK(!scratch.is(xmm0));
4998 __ CompareRoot(FieldOperand(input_reg, HeapObject::kMapOffset),
4999 Heap::kHeapNumberMapRootIndex);
5000 DeoptimizeIf(not_equal, instr, "not a heap number");
Steve Block1e0659c2011-05-24 12:43:12 +01005001 __ movsd(xmm0, FieldOperand(input_reg, HeapNumber::kValueOffset));
5002 __ cvttsd2si(input_reg, xmm0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005003 __ Cvtlsi2sd(scratch, input_reg);
5004 __ ucomisd(xmm0, scratch);
5005 DeoptimizeIf(not_equal, instr, "lost precision");
5006 DeoptimizeIf(parity_even, instr, "NaN");
5007 if (instr->hydrogen()->GetMinusZeroMode() == FAIL_ON_MINUS_ZERO) {
Steve Block1e0659c2011-05-24 12:43:12 +01005008 __ testl(input_reg, input_reg);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005009 __ j(not_zero, done);
Steve Block1e0659c2011-05-24 12:43:12 +01005010 __ movmskpd(input_reg, xmm0);
5011 __ andl(input_reg, Immediate(1));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005012 DeoptimizeIf(not_zero, instr, "minus zero");
Steve Block1e0659c2011-05-24 12:43:12 +01005013 }
5014 }
Ben Murdochb8e0da22011-05-16 14:20:40 +01005015}
5016
5017
5018void LCodeGen::DoTaggedToI(LTaggedToI* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005019 class DeferredTaggedToI FINAL : public LDeferredCode {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005020 public:
5021 DeferredTaggedToI(LCodeGen* codegen, LTaggedToI* instr)
5022 : LDeferredCode(codegen), instr_(instr) { }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005023 void Generate() OVERRIDE { codegen()->DoDeferredTaggedToI(instr_, done()); }
5024 LInstruction* instr() OVERRIDE { return instr_; }
5025
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005026 private:
5027 LTaggedToI* instr_;
5028 };
5029
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005030 LOperand* input = instr->value();
5031 DCHECK(input->IsRegister());
5032 DCHECK(input->Equals(instr->result()));
Steve Block1e0659c2011-05-24 12:43:12 +01005033 Register input_reg = ToRegister(input);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005034
5035 if (instr->hydrogen()->value()->representation().IsSmi()) {
5036 __ SmiToInteger32(input_reg, input_reg);
5037 } else {
5038 DeferredTaggedToI* deferred = new(zone()) DeferredTaggedToI(this, instr);
5039 __ JumpIfNotSmi(input_reg, deferred->entry());
5040 __ SmiToInteger32(input_reg, input_reg);
5041 __ bind(deferred->exit());
5042 }
Ben Murdochb8e0da22011-05-16 14:20:40 +01005043}
5044
5045
5046void LCodeGen::DoNumberUntagD(LNumberUntagD* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005047 LOperand* input = instr->value();
5048 DCHECK(input->IsRegister());
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005049 LOperand* result = instr->result();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005050 DCHECK(result->IsDoubleRegister());
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005051
5052 Register input_reg = ToRegister(input);
5053 XMMRegister result_reg = ToDoubleRegister(result);
5054
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005055 HValue* value = instr->hydrogen()->value();
5056 NumberUntagDMode mode = value->representation().IsSmi()
5057 ? NUMBER_CANDIDATE_IS_SMI : NUMBER_CANDIDATE_IS_ANY_TAGGED;
5058
5059 EmitNumberUntagD(instr, input_reg, result_reg, mode);
Ben Murdochb8e0da22011-05-16 14:20:40 +01005060}
5061
5062
5063void LCodeGen::DoDoubleToI(LDoubleToI* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005064 LOperand* input = instr->value();
5065 DCHECK(input->IsDoubleRegister());
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005066 LOperand* result = instr->result();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005067 DCHECK(result->IsRegister());
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005068
5069 XMMRegister input_reg = ToDoubleRegister(input);
5070 Register result_reg = ToRegister(result);
5071
5072 if (instr->truncating()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005073 __ TruncateDoubleToI(result_reg, input_reg);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005074 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005075 Label lost_precision, is_nan, minus_zero, done;
5076 XMMRegister xmm_scratch = double_scratch0();
5077 Label::Distance dist = DeoptEveryNTimes() ? Label::kFar : Label::kNear;
5078 __ DoubleToI(result_reg, input_reg, xmm_scratch,
5079 instr->hydrogen()->GetMinusZeroMode(), &lost_precision,
5080 &is_nan, &minus_zero, dist);
5081 __ jmp(&done, dist);
5082 __ bind(&lost_precision);
5083 DeoptimizeIf(no_condition, instr, "lost precision");
5084 __ bind(&is_nan);
5085 DeoptimizeIf(no_condition, instr, "NaN");
5086 __ bind(&minus_zero);
5087 DeoptimizeIf(no_condition, instr, "minus zero");
5088 __ bind(&done);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005089 }
Ben Murdochb8e0da22011-05-16 14:20:40 +01005090}
5091
5092
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005093void LCodeGen::DoDoubleToSmi(LDoubleToSmi* instr) {
5094 LOperand* input = instr->value();
5095 DCHECK(input->IsDoubleRegister());
5096 LOperand* result = instr->result();
5097 DCHECK(result->IsRegister());
5098
5099 XMMRegister input_reg = ToDoubleRegister(input);
5100 Register result_reg = ToRegister(result);
5101
5102 Label lost_precision, is_nan, minus_zero, done;
5103 XMMRegister xmm_scratch = double_scratch0();
5104 Label::Distance dist = DeoptEveryNTimes() ? Label::kFar : Label::kNear;
5105 __ DoubleToI(result_reg, input_reg, xmm_scratch,
5106 instr->hydrogen()->GetMinusZeroMode(), &lost_precision, &is_nan,
5107 &minus_zero, dist);
5108 __ jmp(&done, dist);
5109 __ bind(&lost_precision);
5110 DeoptimizeIf(no_condition, instr, "lost precision");
5111 __ bind(&is_nan);
5112 DeoptimizeIf(no_condition, instr, "NaN");
5113 __ bind(&minus_zero);
5114 DeoptimizeIf(no_condition, instr, "minus zero");
5115 __ bind(&done);
5116 __ Integer32ToSmi(result_reg, result_reg);
5117 DeoptimizeIf(overflow, instr, "overflow");
5118}
5119
5120
Ben Murdochb8e0da22011-05-16 14:20:40 +01005121void LCodeGen::DoCheckSmi(LCheckSmi* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005122 LOperand* input = instr->value();
Steve Block1e0659c2011-05-24 12:43:12 +01005123 Condition cc = masm()->CheckSmi(ToRegister(input));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005124 DeoptimizeIf(NegateCondition(cc), instr, "not a Smi");
Steve Block44f0eee2011-05-26 01:26:41 +01005125}
5126
5127
5128void LCodeGen::DoCheckNonSmi(LCheckNonSmi* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005129 if (!instr->hydrogen()->value()->type().IsHeapObject()) {
5130 LOperand* input = instr->value();
5131 Condition cc = masm()->CheckSmi(ToRegister(input));
5132 DeoptimizeIf(cc, instr, "Smi");
5133 }
Ben Murdochb8e0da22011-05-16 14:20:40 +01005134}
5135
5136
5137void LCodeGen::DoCheckInstanceType(LCheckInstanceType* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005138 Register input = ToRegister(instr->value());
Steve Block1e0659c2011-05-24 12:43:12 +01005139
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005140 __ movp(kScratchRegister, FieldOperand(input, HeapObject::kMapOffset));
Steve Block1e0659c2011-05-24 12:43:12 +01005141
Ben Murdoch257744e2011-11-30 15:57:28 +00005142 if (instr->hydrogen()->is_interval_check()) {
5143 InstanceType first;
5144 InstanceType last;
5145 instr->hydrogen()->GetCheckInterval(&first, &last);
5146
Steve Block1e0659c2011-05-24 12:43:12 +01005147 __ cmpb(FieldOperand(kScratchRegister, Map::kInstanceTypeOffset),
5148 Immediate(static_cast<int8_t>(first)));
Ben Murdoch257744e2011-11-30 15:57:28 +00005149
5150 // If there is only one type in the interval check for equality.
5151 if (first == last) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005152 DeoptimizeIf(not_equal, instr, "wrong instance type");
Ben Murdoch257744e2011-11-30 15:57:28 +00005153 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005154 DeoptimizeIf(below, instr, "wrong instance type");
Ben Murdoch257744e2011-11-30 15:57:28 +00005155 // Omit check for the last type.
5156 if (last != LAST_TYPE) {
5157 __ cmpb(FieldOperand(kScratchRegister, Map::kInstanceTypeOffset),
5158 Immediate(static_cast<int8_t>(last)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005159 DeoptimizeIf(above, instr, "wrong instance type");
Ben Murdoch257744e2011-11-30 15:57:28 +00005160 }
5161 }
Steve Block1e0659c2011-05-24 12:43:12 +01005162 } else {
Ben Murdoch257744e2011-11-30 15:57:28 +00005163 uint8_t mask;
5164 uint8_t tag;
5165 instr->hydrogen()->GetCheckMaskAndTag(&mask, &tag);
5166
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005167 if (base::bits::IsPowerOfTwo32(mask)) {
5168 DCHECK(tag == 0 || base::bits::IsPowerOfTwo32(tag));
Ben Murdoch257744e2011-11-30 15:57:28 +00005169 __ testb(FieldOperand(kScratchRegister, Map::kInstanceTypeOffset),
5170 Immediate(mask));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005171 DeoptimizeIf(tag == 0 ? not_zero : zero, instr, "wrong instance type");
Ben Murdoch257744e2011-11-30 15:57:28 +00005172 } else {
5173 __ movzxbl(kScratchRegister,
5174 FieldOperand(kScratchRegister, Map::kInstanceTypeOffset));
5175 __ andb(kScratchRegister, Immediate(mask));
5176 __ cmpb(kScratchRegister, Immediate(tag));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005177 DeoptimizeIf(not_equal, instr, "wrong instance type");
Steve Block1e0659c2011-05-24 12:43:12 +01005178 }
5179 }
Ben Murdochb8e0da22011-05-16 14:20:40 +01005180}
5181
5182
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005183void LCodeGen::DoCheckValue(LCheckValue* instr) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005184 Register reg = ToRegister(instr->value());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005185 __ Cmp(reg, instr->hydrogen()->object().handle());
5186 DeoptimizeIf(not_equal, instr, "value mismatch");
5187}
5188
5189
5190void LCodeGen::DoDeferredInstanceMigration(LCheckMaps* instr, Register object) {
5191 {
5192 PushSafepointRegistersScope scope(this);
5193 __ Push(object);
5194 __ Set(rsi, 0);
5195 __ CallRuntimeSaveDoubles(Runtime::kTryMigrateInstance);
5196 RecordSafepointWithRegisters(
5197 instr->pointer_map(), 1, Safepoint::kNoLazyDeopt);
5198
5199 __ testp(rax, Immediate(kSmiTagMask));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005200 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005201 DeoptimizeIf(zero, instr, "instance migration failed");
Ben Murdochb8e0da22011-05-16 14:20:40 +01005202}
5203
5204
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005205void LCodeGen::DoCheckMaps(LCheckMaps* instr) {
5206 class DeferredCheckMaps FINAL : public LDeferredCode {
5207 public:
5208 DeferredCheckMaps(LCodeGen* codegen, LCheckMaps* instr, Register object)
5209 : LDeferredCode(codegen), instr_(instr), object_(object) {
5210 SetExit(check_maps());
5211 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005212 void Generate() OVERRIDE {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005213 codegen()->DoDeferredInstanceMigration(instr_, object_);
5214 }
5215 Label* check_maps() { return &check_maps_; }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005216 LInstruction* instr() OVERRIDE { return instr_; }
5217
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005218 private:
5219 LCheckMaps* instr_;
5220 Label check_maps_;
5221 Register object_;
5222 };
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005223
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005224 if (instr->hydrogen()->IsStabilityCheck()) {
5225 const UniqueSet<Map>* maps = instr->hydrogen()->maps();
5226 for (int i = 0; i < maps->size(); ++i) {
5227 AddStabilityDependency(maps->at(i).handle());
5228 }
5229 return;
5230 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005231
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005232 LOperand* input = instr->value();
5233 DCHECK(input->IsRegister());
Steve Block1e0659c2011-05-24 12:43:12 +01005234 Register reg = ToRegister(input);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005235
5236 DeferredCheckMaps* deferred = NULL;
5237 if (instr->hydrogen()->HasMigrationTarget()) {
5238 deferred = new(zone()) DeferredCheckMaps(this, instr, reg);
5239 __ bind(deferred->check_maps());
5240 }
5241
5242 const UniqueSet<Map>* maps = instr->hydrogen()->maps();
5243 Label success;
5244 for (int i = 0; i < maps->size() - 1; i++) {
5245 Handle<Map> map = maps->at(i).handle();
5246 __ CompareMap(reg, map);
5247 __ j(equal, &success, Label::kNear);
5248 }
5249
5250 Handle<Map> map = maps->at(maps->size() - 1).handle();
5251 __ CompareMap(reg, map);
5252 if (instr->hydrogen()->HasMigrationTarget()) {
5253 __ j(not_equal, deferred->entry());
5254 } else {
5255 DeoptimizeIf(not_equal, instr, "wrong map");
5256 }
5257
5258 __ bind(&success);
Ben Murdochb8e0da22011-05-16 14:20:40 +01005259}
5260
5261
Ben Murdoch257744e2011-11-30 15:57:28 +00005262void LCodeGen::DoClampDToUint8(LClampDToUint8* instr) {
5263 XMMRegister value_reg = ToDoubleRegister(instr->unclamped());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005264 XMMRegister xmm_scratch = double_scratch0();
Ben Murdoch257744e2011-11-30 15:57:28 +00005265 Register result_reg = ToRegister(instr->result());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005266 __ ClampDoubleToUint8(value_reg, xmm_scratch, result_reg);
Ben Murdoch257744e2011-11-30 15:57:28 +00005267}
5268
5269
5270void LCodeGen::DoClampIToUint8(LClampIToUint8* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005271 DCHECK(instr->unclamped()->Equals(instr->result()));
Ben Murdoch257744e2011-11-30 15:57:28 +00005272 Register value_reg = ToRegister(instr->result());
5273 __ ClampUint8(value_reg);
5274}
5275
5276
5277void LCodeGen::DoClampTToUint8(LClampTToUint8* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005278 DCHECK(instr->unclamped()->Equals(instr->result()));
Ben Murdoch257744e2011-11-30 15:57:28 +00005279 Register input_reg = ToRegister(instr->unclamped());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005280 XMMRegister temp_xmm_reg = ToDoubleRegister(instr->temp_xmm());
5281 XMMRegister xmm_scratch = double_scratch0();
Ben Murdoch257744e2011-11-30 15:57:28 +00005282 Label is_smi, done, heap_number;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005283 Label::Distance dist = DeoptEveryNTimes() ? Label::kFar : Label::kNear;
5284 __ JumpIfSmi(input_reg, &is_smi, dist);
Ben Murdoch257744e2011-11-30 15:57:28 +00005285
5286 // Check for heap number
5287 __ Cmp(FieldOperand(input_reg, HeapObject::kMapOffset),
5288 factory()->heap_number_map());
5289 __ j(equal, &heap_number, Label::kNear);
5290
5291 // Check for undefined. Undefined is converted to zero for clamping
5292 // conversions.
5293 __ Cmp(input_reg, factory()->undefined_value());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005294 DeoptimizeIf(not_equal, instr, "not a heap number/undefined");
5295 __ xorl(input_reg, input_reg);
Ben Murdoch257744e2011-11-30 15:57:28 +00005296 __ jmp(&done, Label::kNear);
5297
5298 // Heap number
5299 __ bind(&heap_number);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005300 __ movsd(xmm_scratch, FieldOperand(input_reg, HeapNumber::kValueOffset));
5301 __ ClampDoubleToUint8(xmm_scratch, temp_xmm_reg, input_reg);
Ben Murdoch257744e2011-11-30 15:57:28 +00005302 __ jmp(&done, Label::kNear);
5303
5304 // smi
5305 __ bind(&is_smi);
5306 __ SmiToInteger32(input_reg, input_reg);
5307 __ ClampUint8(input_reg);
5308
5309 __ bind(&done);
5310}
5311
5312
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005313void LCodeGen::DoDoubleBits(LDoubleBits* instr) {
5314 XMMRegister value_reg = ToDoubleRegister(instr->value());
5315 Register result_reg = ToRegister(instr->result());
5316 if (instr->hydrogen()->bits() == HDoubleBits::HIGH) {
5317 __ movq(result_reg, value_reg);
5318 __ shrq(result_reg, Immediate(32));
5319 } else {
5320 __ movd(result_reg, value_reg);
Steve Block1e0659c2011-05-24 12:43:12 +01005321 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005322}
5323
5324
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005325void LCodeGen::DoConstructDouble(LConstructDouble* instr) {
5326 Register hi_reg = ToRegister(instr->hi());
5327 Register lo_reg = ToRegister(instr->lo());
5328 XMMRegister result_reg = ToDoubleRegister(instr->result());
5329 XMMRegister xmm_scratch = double_scratch0();
5330 __ movd(result_reg, hi_reg);
5331 __ psllq(result_reg, 32);
5332 __ movd(xmm_scratch, lo_reg);
5333 __ orps(result_reg, xmm_scratch);
5334}
5335
5336
5337void LCodeGen::DoAllocate(LAllocate* instr) {
5338 class DeferredAllocate FINAL : public LDeferredCode {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005339 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005340 DeferredAllocate(LCodeGen* codegen, LAllocate* instr)
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005341 : LDeferredCode(codegen), instr_(instr) { }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005342 void Generate() OVERRIDE { codegen()->DoDeferredAllocate(instr_); }
5343 LInstruction* instr() OVERRIDE { return instr_; }
5344
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005345 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005346 LAllocate* instr_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005347 };
5348
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005349 DeferredAllocate* deferred =
5350 new(zone()) DeferredAllocate(this, instr);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005351
5352 Register result = ToRegister(instr->result());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005353 Register temp = ToRegister(instr->temp());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005354
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005355 // Allocate memory for the object.
5356 AllocationFlags flags = TAG_OBJECT;
5357 if (instr->hydrogen()->MustAllocateDoubleAligned()) {
5358 flags = static_cast<AllocationFlags>(flags | DOUBLE_ALIGNMENT);
5359 }
5360 if (instr->hydrogen()->IsOldPointerSpaceAllocation()) {
5361 DCHECK(!instr->hydrogen()->IsOldDataSpaceAllocation());
5362 DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
5363 flags = static_cast<AllocationFlags>(flags | PRETENURE_OLD_POINTER_SPACE);
5364 } else if (instr->hydrogen()->IsOldDataSpaceAllocation()) {
5365 DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
5366 flags = static_cast<AllocationFlags>(flags | PRETENURE_OLD_DATA_SPACE);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005367 }
5368
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005369 if (instr->size()->IsConstantOperand()) {
5370 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
5371 if (size <= Page::kMaxRegularHeapObjectSize) {
5372 __ Allocate(size, result, temp, no_reg, deferred->entry(), flags);
5373 } else {
5374 __ jmp(deferred->entry());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005375 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005376 } else {
5377 Register size = ToRegister(instr->size());
5378 __ Allocate(size, result, temp, no_reg, deferred->entry(), flags);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005379 }
5380
5381 __ bind(deferred->exit());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005382
5383 if (instr->hydrogen()->MustPrefillWithFiller()) {
5384 if (instr->size()->IsConstantOperand()) {
5385 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
5386 __ movl(temp, Immediate((size / kPointerSize) - 1));
5387 } else {
5388 temp = ToRegister(instr->size());
5389 __ sarp(temp, Immediate(kPointerSizeLog2));
5390 __ decl(temp);
5391 }
5392 Label loop;
5393 __ bind(&loop);
5394 __ Move(FieldOperand(result, temp, times_pointer_size, 0),
5395 isolate()->factory()->one_pointer_filler_map());
5396 __ decl(temp);
5397 __ j(not_zero, &loop);
5398 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005399}
5400
5401
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005402void LCodeGen::DoDeferredAllocate(LAllocate* instr) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005403 Register result = ToRegister(instr->result());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005404
5405 // TODO(3095996): Get rid of this. For now, we need to make the
5406 // result register contain a valid pointer because it is already
5407 // contained in the register pointer map.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005408 __ Move(result, Smi::FromInt(0));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005409
5410 PushSafepointRegistersScope scope(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005411 if (instr->size()->IsRegister()) {
5412 Register size = ToRegister(instr->size());
5413 DCHECK(!size.is(result));
5414 __ Integer32ToSmi(size, size);
5415 __ Push(size);
5416 } else {
5417 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
5418 __ Push(Smi::FromInt(size));
5419 }
5420
5421 int flags = 0;
5422 if (instr->hydrogen()->IsOldPointerSpaceAllocation()) {
5423 DCHECK(!instr->hydrogen()->IsOldDataSpaceAllocation());
5424 DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
5425 flags = AllocateTargetSpace::update(flags, OLD_POINTER_SPACE);
5426 } else if (instr->hydrogen()->IsOldDataSpaceAllocation()) {
5427 DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
5428 flags = AllocateTargetSpace::update(flags, OLD_DATA_SPACE);
5429 } else {
5430 flags = AllocateTargetSpace::update(flags, NEW_SPACE);
5431 }
5432 __ Push(Smi::FromInt(flags));
5433
5434 CallRuntimeFromDeferred(
5435 Runtime::kAllocateInTargetSpace, 2, instr, instr->context());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005436 __ StoreToSafepointRegisterSlot(result, rax);
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +01005437}
5438
5439
Steve Block44f0eee2011-05-26 01:26:41 +01005440void LCodeGen::DoToFastProperties(LToFastProperties* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005441 DCHECK(ToRegister(instr->value()).is(rax));
5442 __ Push(rax);
Steve Block44f0eee2011-05-26 01:26:41 +01005443 CallRuntime(Runtime::kToFastProperties, 1, instr);
5444}
5445
5446
Ben Murdochb8e0da22011-05-16 14:20:40 +01005447void LCodeGen::DoRegExpLiteral(LRegExpLiteral* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005448 DCHECK(ToRegister(instr->context()).is(rsi));
Ben Murdoch257744e2011-11-30 15:57:28 +00005449 Label materialized;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005450 // Registers will be used as follows:
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005451 // rcx = literals array.
5452 // rbx = regexp literal.
5453 // rax = regexp literal clone.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005454 int literal_offset =
5455 FixedArray::OffsetOfElementAt(instr->hydrogen()->literal_index());
5456 __ Move(rcx, instr->hydrogen()->literals());
5457 __ movp(rbx, FieldOperand(rcx, literal_offset));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005458 __ CompareRoot(rbx, Heap::kUndefinedValueRootIndex);
Ben Murdoch257744e2011-11-30 15:57:28 +00005459 __ j(not_equal, &materialized, Label::kNear);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005460
5461 // Create regexp literal using runtime function
5462 // Result will be in rax.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005463 __ Push(rcx);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005464 __ Push(Smi::FromInt(instr->hydrogen()->literal_index()));
5465 __ Push(instr->hydrogen()->pattern());
5466 __ Push(instr->hydrogen()->flags());
5467 CallRuntime(Runtime::kMaterializeRegExpLiteral, 4, instr);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005468 __ movp(rbx, rax);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005469
5470 __ bind(&materialized);
5471 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
5472 Label allocated, runtime_allocate;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005473 __ Allocate(size, rax, rcx, rdx, &runtime_allocate, TAG_OBJECT);
5474 __ jmp(&allocated, Label::kNear);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005475
5476 __ bind(&runtime_allocate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005477 __ Push(rbx);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005478 __ Push(Smi::FromInt(size));
5479 CallRuntime(Runtime::kAllocateInNewSpace, 1, instr);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005480 __ Pop(rbx);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005481
5482 __ bind(&allocated);
5483 // Copy the content into the newly allocated memory.
5484 // (Unroll copy loop once for better throughput).
5485 for (int i = 0; i < size - kPointerSize; i += 2 * kPointerSize) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005486 __ movp(rdx, FieldOperand(rbx, i));
5487 __ movp(rcx, FieldOperand(rbx, i + kPointerSize));
5488 __ movp(FieldOperand(rax, i), rdx);
5489 __ movp(FieldOperand(rax, i + kPointerSize), rcx);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005490 }
5491 if ((size % (2 * kPointerSize)) != 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005492 __ movp(rdx, FieldOperand(rbx, size - kPointerSize));
5493 __ movp(FieldOperand(rax, size - kPointerSize), rdx);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005494 }
Ben Murdochb8e0da22011-05-16 14:20:40 +01005495}
5496
5497
5498void LCodeGen::DoFunctionLiteral(LFunctionLiteral* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005499 DCHECK(ToRegister(instr->context()).is(rsi));
Steve Block1e0659c2011-05-24 12:43:12 +01005500 // Use the fast case closure allocation code that allocates in new
5501 // space for nested functions that don't need literals cloning.
Steve Block1e0659c2011-05-24 12:43:12 +01005502 bool pretenure = instr->hydrogen()->pretenure();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005503 if (!pretenure && instr->hydrogen()->has_no_literals()) {
5504 FastNewClosureStub stub(isolate(), instr->hydrogen()->strict_mode(),
5505 instr->hydrogen()->kind());
5506 __ Move(rbx, instr->hydrogen()->shared_info());
Steve Block1e0659c2011-05-24 12:43:12 +01005507 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
5508 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005509 __ Push(rsi);
5510 __ Push(instr->hydrogen()->shared_info());
5511 __ PushRoot(pretenure ? Heap::kTrueValueRootIndex :
5512 Heap::kFalseValueRootIndex);
Steve Block1e0659c2011-05-24 12:43:12 +01005513 CallRuntime(Runtime::kNewClosure, 3, instr);
5514 }
Ben Murdochb8e0da22011-05-16 14:20:40 +01005515}
5516
5517
5518void LCodeGen::DoTypeof(LTypeof* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005519 DCHECK(ToRegister(instr->context()).is(rsi));
5520 LOperand* input = instr->value();
Ben Murdoch257744e2011-11-30 15:57:28 +00005521 EmitPushTaggedOperand(input);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005522 CallRuntime(Runtime::kTypeof, 1, instr);
Ben Murdochb8e0da22011-05-16 14:20:40 +01005523}
5524
5525
Ben Murdoch257744e2011-11-30 15:57:28 +00005526void LCodeGen::EmitPushTaggedOperand(LOperand* operand) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005527 DCHECK(!operand->IsDoubleRegister());
Ben Murdoch257744e2011-11-30 15:57:28 +00005528 if (operand->IsConstantOperand()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005529 __ Push(ToHandle(LConstantOperand::cast(operand)));
Ben Murdoch257744e2011-11-30 15:57:28 +00005530 } else if (operand->IsRegister()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005531 __ Push(ToRegister(operand));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005532 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005533 __ Push(ToOperand(operand));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005534 }
5535}
5536
5537
5538void LCodeGen::DoTypeofIsAndBranch(LTypeofIsAndBranch* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005539 Register input = ToRegister(instr->value());
5540 Condition final_branch_condition = EmitTypeofIs(instr, input);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005541 if (final_branch_condition != no_condition) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005542 EmitBranch(instr, final_branch_condition);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005543 }
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005544}
5545
5546
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005547Condition LCodeGen::EmitTypeofIs(LTypeofIsAndBranch* instr, Register input) {
5548 Label* true_label = instr->TrueLabel(chunk_);
5549 Label* false_label = instr->FalseLabel(chunk_);
5550 Handle<String> type_name = instr->type_literal();
5551 int left_block = instr->TrueDestination(chunk_);
5552 int right_block = instr->FalseDestination(chunk_);
5553 int next_block = GetNextEmittedBlock();
5554
5555 Label::Distance true_distance = left_block == next_block ? Label::kNear
5556 : Label::kFar;
5557 Label::Distance false_distance = right_block == next_block ? Label::kNear
5558 : Label::kFar;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005559 Condition final_branch_condition = no_condition;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005560 Factory* factory = isolate()->factory();
5561 if (String::Equals(type_name, factory->number_string())) {
5562 __ JumpIfSmi(input, true_label, true_distance);
Steve Block44f0eee2011-05-26 01:26:41 +01005563 __ CompareRoot(FieldOperand(input, HeapObject::kMapOffset),
5564 Heap::kHeapNumberMapRootIndex);
5565
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005566 final_branch_condition = equal;
5567
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005568 } else if (String::Equals(type_name, factory->string_string())) {
5569 __ JumpIfSmi(input, false_label, false_distance);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005570 __ CmpObjectType(input, FIRST_NONSTRING_TYPE, input);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005571 __ j(above_equal, false_label, false_distance);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005572 __ testb(FieldOperand(input, Map::kBitFieldOffset),
5573 Immediate(1 << Map::kIsUndetectable));
5574 final_branch_condition = zero;
5575
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005576 } else if (String::Equals(type_name, factory->symbol_string())) {
5577 __ JumpIfSmi(input, false_label, false_distance);
5578 __ CmpObjectType(input, SYMBOL_TYPE, input);
5579 final_branch_condition = equal;
5580
5581 } else if (String::Equals(type_name, factory->boolean_string())) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005582 __ CompareRoot(input, Heap::kTrueValueRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005583 __ j(equal, true_label, true_distance);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005584 __ CompareRoot(input, Heap::kFalseValueRootIndex);
5585 final_branch_condition = equal;
5586
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005587 } else if (String::Equals(type_name, factory->undefined_string())) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005588 __ CompareRoot(input, Heap::kUndefinedValueRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005589 __ j(equal, true_label, true_distance);
5590 __ JumpIfSmi(input, false_label, false_distance);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005591 // Check for undetectable objects => true.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005592 __ movp(input, FieldOperand(input, HeapObject::kMapOffset));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005593 __ testb(FieldOperand(input, Map::kBitFieldOffset),
5594 Immediate(1 << Map::kIsUndetectable));
5595 final_branch_condition = not_zero;
5596
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005597 } else if (String::Equals(type_name, factory->function_string())) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005598 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005599 __ JumpIfSmi(input, false_label, false_distance);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005600 __ CmpObjectType(input, JS_FUNCTION_TYPE, input);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005601 __ j(equal, true_label, true_distance);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005602 __ CmpInstanceType(input, JS_FUNCTION_PROXY_TYPE);
5603 final_branch_condition = equal;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005604
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005605 } else if (String::Equals(type_name, factory->object_string())) {
5606 __ JumpIfSmi(input, false_label, false_distance);
5607 __ CompareRoot(input, Heap::kNullValueRootIndex);
5608 __ j(equal, true_label, true_distance);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005609 __ CmpObjectType(input, FIRST_NONCALLABLE_SPEC_OBJECT_TYPE, input);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005610 __ j(below, false_label, false_distance);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005611 __ CmpInstanceType(input, LAST_NONCALLABLE_SPEC_OBJECT_TYPE);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005612 __ j(above, false_label, false_distance);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005613 // Check for undetectable objects => false.
5614 __ testb(FieldOperand(input, Map::kBitFieldOffset),
5615 Immediate(1 << Map::kIsUndetectable));
5616 final_branch_condition = zero;
5617
5618 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005619 __ jmp(false_label, false_distance);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005620 }
5621
5622 return final_branch_condition;
Ben Murdochb8e0da22011-05-16 14:20:40 +01005623}
5624
5625
Steve Block1e0659c2011-05-24 12:43:12 +01005626void LCodeGen::DoIsConstructCallAndBranch(LIsConstructCallAndBranch* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005627 Register temp = ToRegister(instr->temp());
Steve Block1e0659c2011-05-24 12:43:12 +01005628
5629 EmitIsConstructCall(temp);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005630 EmitBranch(instr, equal);
Steve Block1e0659c2011-05-24 12:43:12 +01005631}
5632
5633
5634void LCodeGen::EmitIsConstructCall(Register temp) {
5635 // Get the frame pointer for the calling frame.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005636 __ movp(temp, Operand(rbp, StandardFrameConstants::kCallerFPOffset));
Steve Block1e0659c2011-05-24 12:43:12 +01005637
5638 // Skip the arguments adaptor frame if it exists.
Ben Murdoch257744e2011-11-30 15:57:28 +00005639 Label check_frame_marker;
Steve Block44f0eee2011-05-26 01:26:41 +01005640 __ Cmp(Operand(temp, StandardFrameConstants::kContextOffset),
5641 Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
Ben Murdoch257744e2011-11-30 15:57:28 +00005642 __ j(not_equal, &check_frame_marker, Label::kNear);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005643 __ movp(temp, Operand(temp, StandardFrameConstants::kCallerFPOffset));
Steve Block1e0659c2011-05-24 12:43:12 +01005644
5645 // Check the marker in the calling frame.
5646 __ bind(&check_frame_marker);
Steve Block44f0eee2011-05-26 01:26:41 +01005647 __ Cmp(Operand(temp, StandardFrameConstants::kMarkerOffset),
5648 Smi::FromInt(StackFrame::CONSTRUCT));
Steve Block1e0659c2011-05-24 12:43:12 +01005649}
5650
5651
Ben Murdoch2b4ba112012-01-20 14:57:15 +00005652void LCodeGen::EnsureSpaceForLazyDeopt(int space_needed) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005653 if (!info()->IsStub()) {
5654 // Ensure that we have enough space after the previous lazy-bailout
5655 // instruction for patching the code here.
5656 int current_pc = masm()->pc_offset();
5657 if (current_pc < last_lazy_deopt_pc_ + space_needed) {
5658 int padding_size = last_lazy_deopt_pc_ + space_needed - current_pc;
5659 __ Nop(padding_size);
5660 }
Ben Murdoch2b4ba112012-01-20 14:57:15 +00005661 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005662 last_lazy_deopt_pc_ = masm()->pc_offset();
Ben Murdoch2b4ba112012-01-20 14:57:15 +00005663}
5664
5665
Ben Murdochb8e0da22011-05-16 14:20:40 +01005666void LCodeGen::DoLazyBailout(LLazyBailout* instr) {
Ben Murdoch2b4ba112012-01-20 14:57:15 +00005667 last_lazy_deopt_pc_ = masm()->pc_offset();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005668 DCHECK(instr->HasEnvironment());
Ben Murdoch2b4ba112012-01-20 14:57:15 +00005669 LEnvironment* env = instr->environment();
5670 RegisterEnvironmentForDeoptimization(env, Safepoint::kLazyDeopt);
5671 safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
Ben Murdochb8e0da22011-05-16 14:20:40 +01005672}
5673
5674
5675void LCodeGen::DoDeoptimize(LDeoptimize* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005676 Deoptimizer::BailoutType type = instr->hydrogen()->type();
5677 // TODO(danno): Stubs expect all deopts to be lazy for historical reasons (the
5678 // needed return address), even though the implementation of LAZY and EAGER is
5679 // now identical. When LAZY is eventually completely folded into EAGER, remove
5680 // the special case below.
5681 if (info()->IsStub() && type == Deoptimizer::EAGER) {
5682 type = Deoptimizer::LAZY;
5683 }
5684 DeoptimizeIf(no_condition, instr, instr->hydrogen()->reason(), type);
Ben Murdochb8e0da22011-05-16 14:20:40 +01005685}
5686
5687
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005688void LCodeGen::DoDummy(LDummy* instr) {
5689 // Nothing to see here, move on!
Ben Murdoch257744e2011-11-30 15:57:28 +00005690}
5691
5692
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005693void LCodeGen::DoDummyUse(LDummyUse* instr) {
5694 // Nothing to see here, move on!
Ben Murdochb8e0da22011-05-16 14:20:40 +01005695}
5696
5697
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005698void LCodeGen::DoDeferredStackCheck(LStackCheck* instr) {
Ben Murdoch2b4ba112012-01-20 14:57:15 +00005699 PushSafepointRegistersScope scope(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005700 __ movp(rsi, Operand(rbp, StandardFrameConstants::kContextOffset));
Ben Murdoch2b4ba112012-01-20 14:57:15 +00005701 __ CallRuntimeSaveDoubles(Runtime::kStackGuard);
5702 RecordSafepointWithLazyDeopt(instr, RECORD_SAFEPOINT_WITH_REGISTERS, 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005703 DCHECK(instr->HasEnvironment());
Ben Murdoch2b4ba112012-01-20 14:57:15 +00005704 LEnvironment* env = instr->environment();
5705 safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005706}
5707
5708
5709void LCodeGen::DoStackCheck(LStackCheck* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005710 class DeferredStackCheck FINAL : public LDeferredCode {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005711 public:
5712 DeferredStackCheck(LCodeGen* codegen, LStackCheck* instr)
5713 : LDeferredCode(codegen), instr_(instr) { }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005714 void Generate() OVERRIDE { codegen()->DoDeferredStackCheck(instr_); }
5715 LInstruction* instr() OVERRIDE { return instr_; }
5716
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005717 private:
5718 LStackCheck* instr_;
5719 };
5720
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005721 DCHECK(instr->HasEnvironment());
Ben Murdoch2b4ba112012-01-20 14:57:15 +00005722 LEnvironment* env = instr->environment();
5723 // There is no LLazyBailout instruction for stack-checks. We have to
5724 // prepare for lazy deoptimization explicitly here.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005725 if (instr->hydrogen()->is_function_entry()) {
5726 // Perform stack overflow check.
5727 Label done;
5728 __ CompareRoot(rsp, Heap::kStackLimitRootIndex);
5729 __ j(above_equal, &done, Label::kNear);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005730
5731 DCHECK(instr->context()->IsRegister());
5732 DCHECK(ToRegister(instr->context()).is(rsi));
5733 CallCode(isolate()->builtins()->StackCheck(),
5734 RelocInfo::CODE_TARGET,
5735 instr);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005736 __ bind(&done);
5737 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005738 DCHECK(instr->hydrogen()->is_backwards_branch());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005739 // Perform stack overflow check if this goto needs it before jumping.
5740 DeferredStackCheck* deferred_stack_check =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005741 new(zone()) DeferredStackCheck(this, instr);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005742 __ CompareRoot(rsp, Heap::kStackLimitRootIndex);
5743 __ j(below, deferred_stack_check->entry());
Ben Murdoch2b4ba112012-01-20 14:57:15 +00005744 EnsureSpaceForLazyDeopt(Deoptimizer::patch_size());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005745 __ bind(instr->done_label());
5746 deferred_stack_check->SetExit(instr->done_label());
Ben Murdoch2b4ba112012-01-20 14:57:15 +00005747 RegisterEnvironmentForDeoptimization(env, Safepoint::kLazyDeopt);
5748 // Don't record a deoptimization index for the safepoint here.
5749 // This will be done explicitly when emitting call and the safepoint in
5750 // the deferred code.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005751 }
Ben Murdochb8e0da22011-05-16 14:20:40 +01005752}
5753
5754
5755void LCodeGen::DoOsrEntry(LOsrEntry* instr) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005756 // This is a pseudo-instruction that ensures that the environment here is
5757 // properly registered for deoptimization and records the assembler's PC
5758 // offset.
5759 LEnvironment* environment = instr->environment();
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005760
5761 // If the environment were already registered, we would have no way of
5762 // backpatching it with the spill slot operands.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005763 DCHECK(!environment->HasBeenRegistered());
Ben Murdoch2b4ba112012-01-20 14:57:15 +00005764 RegisterEnvironmentForDeoptimization(environment, Safepoint::kNoLazyDeopt);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005765
5766 GenerateOsrPrologue();
Ben Murdochb8e0da22011-05-16 14:20:40 +01005767}
5768
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005769
5770void LCodeGen::DoForInPrepareMap(LForInPrepareMap* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005771 DCHECK(ToRegister(instr->context()).is(rsi));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005772 __ CompareRoot(rax, Heap::kUndefinedValueRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005773 DeoptimizeIf(equal, instr, "undefined");
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005774
5775 Register null_value = rdi;
5776 __ LoadRoot(null_value, Heap::kNullValueRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005777 __ cmpp(rax, null_value);
5778 DeoptimizeIf(equal, instr, "null");
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005779
5780 Condition cc = masm()->CheckSmi(rax);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005781 DeoptimizeIf(cc, instr, "Smi");
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005782
5783 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
5784 __ CmpObjectType(rax, LAST_JS_PROXY_TYPE, rcx);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005785 DeoptimizeIf(below_equal, instr, "wrong instance type");
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005786
5787 Label use_cache, call_runtime;
5788 __ CheckEnumCache(null_value, &call_runtime);
5789
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005790 __ movp(rax, FieldOperand(rax, HeapObject::kMapOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005791 __ jmp(&use_cache, Label::kNear);
5792
5793 // Get the set of properties to enumerate.
5794 __ bind(&call_runtime);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005795 __ Push(rax);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005796 CallRuntime(Runtime::kGetPropertyNamesFast, 1, instr);
5797
5798 __ CompareRoot(FieldOperand(rax, HeapObject::kMapOffset),
5799 Heap::kMetaMapRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005800 DeoptimizeIf(not_equal, instr, "wrong map");
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005801 __ bind(&use_cache);
5802}
5803
5804
5805void LCodeGen::DoForInCacheArray(LForInCacheArray* instr) {
5806 Register map = ToRegister(instr->map());
5807 Register result = ToRegister(instr->result());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005808 Label load_cache, done;
5809 __ EnumLength(result, map);
5810 __ Cmp(result, Smi::FromInt(0));
5811 __ j(not_equal, &load_cache, Label::kNear);
5812 __ LoadRoot(result, Heap::kEmptyFixedArrayRootIndex);
5813 __ jmp(&done, Label::kNear);
5814 __ bind(&load_cache);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005815 __ LoadInstanceDescriptors(map, result);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005816 __ movp(result,
5817 FieldOperand(result, DescriptorArray::kEnumCacheOffset));
5818 __ movp(result,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005819 FieldOperand(result, FixedArray::SizeFor(instr->idx())));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005820 __ bind(&done);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005821 Condition cc = masm()->CheckSmi(result);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005822 DeoptimizeIf(cc, instr, "no cache");
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005823}
5824
5825
5826void LCodeGen::DoCheckMapValue(LCheckMapValue* instr) {
5827 Register object = ToRegister(instr->value());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005828 __ cmpp(ToRegister(instr->map()),
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005829 FieldOperand(object, HeapObject::kMapOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005830 DeoptimizeIf(not_equal, instr, "wrong map");
5831}
5832
5833
5834void LCodeGen::DoDeferredLoadMutableDouble(LLoadFieldByIndex* instr,
5835 Register object,
5836 Register index) {
5837 PushSafepointRegistersScope scope(this);
5838 __ Push(object);
5839 __ Push(index);
5840 __ xorp(rsi, rsi);
5841 __ CallRuntimeSaveDoubles(Runtime::kLoadMutableDouble);
5842 RecordSafepointWithRegisters(
5843 instr->pointer_map(), 2, Safepoint::kNoLazyDeopt);
5844 __ StoreToSafepointRegisterSlot(object, rax);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005845}
5846
5847
5848void LCodeGen::DoLoadFieldByIndex(LLoadFieldByIndex* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005849 class DeferredLoadMutableDouble FINAL : public LDeferredCode {
5850 public:
5851 DeferredLoadMutableDouble(LCodeGen* codegen,
5852 LLoadFieldByIndex* instr,
5853 Register object,
5854 Register index)
5855 : LDeferredCode(codegen),
5856 instr_(instr),
5857 object_(object),
5858 index_(index) {
5859 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005860 void Generate() OVERRIDE {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005861 codegen()->DoDeferredLoadMutableDouble(instr_, object_, index_);
5862 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005863 LInstruction* instr() OVERRIDE { return instr_; }
5864
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005865 private:
5866 LLoadFieldByIndex* instr_;
5867 Register object_;
5868 Register index_;
5869 };
5870
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005871 Register object = ToRegister(instr->object());
5872 Register index = ToRegister(instr->index());
5873
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005874 DeferredLoadMutableDouble* deferred;
5875 deferred = new(zone()) DeferredLoadMutableDouble(this, instr, object, index);
5876
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005877 Label out_of_object, done;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005878 __ Move(kScratchRegister, Smi::FromInt(1));
5879 __ testp(index, kScratchRegister);
5880 __ j(not_zero, deferred->entry());
5881
5882 __ sarp(index, Immediate(1));
5883
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005884 __ SmiToInteger32(index, index);
5885 __ cmpl(index, Immediate(0));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005886 __ j(less, &out_of_object, Label::kNear);
5887 __ movp(object, FieldOperand(object,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005888 index,
5889 times_pointer_size,
5890 JSObject::kHeaderSize));
5891 __ jmp(&done, Label::kNear);
5892
5893 __ bind(&out_of_object);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005894 __ movp(object, FieldOperand(object, JSObject::kPropertiesOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005895 __ negl(index);
5896 // Index is now equal to out of object property index plus 1.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005897 __ movp(object, FieldOperand(object,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005898 index,
5899 times_pointer_size,
5900 FixedArray::kHeaderSize - kPointerSize));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005901 __ bind(deferred->exit());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005902 __ bind(&done);
5903}
5904
5905
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005906void LCodeGen::DoStoreFrameContext(LStoreFrameContext* instr) {
5907 Register context = ToRegister(instr->context());
5908 __ movp(Operand(rbp, StandardFrameConstants::kContextOffset), context);
5909}
5910
5911
5912void LCodeGen::DoAllocateBlockContext(LAllocateBlockContext* instr) {
5913 Handle<ScopeInfo> scope_info = instr->scope_info();
5914 __ Push(scope_info);
5915 __ Push(ToRegister(instr->function()));
5916 CallRuntime(Runtime::kPushBlockContext, 2, instr);
5917 RecordSafepoint(Safepoint::kNoLazyDeopt);
5918}
5919
5920
Ben Murdochb8e0da22011-05-16 14:20:40 +01005921#undef __
5922
5923} } // namespace v8::internal
5924
5925#endif // V8_TARGET_ARCH_X64