blob: a06ed7344802219b7a3f3887160d54940b380132 [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2012 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005#include "src/v8.h"
Steve Block44f0eee2011-05-26 01:26:41 +01006
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007#include "src/arm/lithium-codegen-arm.h"
8#include "src/arm/lithium-gap-resolver-arm.h"
9#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"
Ben Murdochb0fe1622011-05-05 13:52:32 +010015
16namespace v8 {
17namespace internal {
18
19
Ben Murdochb8a8cc12014-11-26 15:28:44 +000020class SafepointGenerator FINAL : public CallWrapper {
Ben Murdochb0fe1622011-05-05 13:52:32 +010021 public:
22 SafepointGenerator(LCodeGen* codegen,
23 LPointerMap* pointers,
Ben Murdoch2b4ba112012-01-20 14:57:15 +000024 Safepoint::DeoptMode mode)
Ben Murdochb0fe1622011-05-05 13:52:32 +010025 : codegen_(codegen),
26 pointers_(pointers),
Ben Murdoch2b4ba112012-01-20 14:57:15 +000027 deopt_mode_(mode) { }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000028 virtual ~SafepointGenerator() {}
Ben Murdochb0fe1622011-05-05 13:52:32 +010029
Ben Murdochb8a8cc12014-11-26 15:28:44 +000030 virtual void BeforeCall(int call_size) const OVERRIDE {}
Steve Block44f0eee2011-05-26 01:26:41 +010031
Ben Murdochb8a8cc12014-11-26 15:28:44 +000032 virtual void AfterCall() const OVERRIDE {
Ben Murdoch2b4ba112012-01-20 14:57:15 +000033 codegen_->RecordSafepoint(pointers_, deopt_mode_);
Ben Murdochb0fe1622011-05-05 13:52:32 +010034 }
35
36 private:
37 LCodeGen* codegen_;
38 LPointerMap* pointers_;
Ben Murdoch2b4ba112012-01-20 14:57:15 +000039 Safepoint::DeoptMode deopt_mode_;
Ben Murdochb0fe1622011-05-05 13:52:32 +010040};
41
42
43#define __ masm()->
44
45bool LCodeGen::GenerateCode() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000046 LPhase phase("Z_Code generation", chunk());
47 DCHECK(is_unused());
Ben Murdochb0fe1622011-05-05 13:52:32 +010048 status_ = GENERATING;
Ben Murdoch3ef787d2012-04-12 10:51:47 +010049
50 // Open a frame scope to indicate that there is a frame on the stack. The
51 // NONE indicates that the scope shouldn't actually generate code to set up
52 // the frame (that is done in GeneratePrologue).
53 FrameScope frame_scope(masm_, StackFrame::NONE);
54
Ben Murdochb8a8cc12014-11-26 15:28:44 +000055 return GeneratePrologue() && GenerateBody() && GenerateDeferredCode() &&
56 GenerateJumpTable() && GenerateSafepointTable();
Ben Murdochb0fe1622011-05-05 13:52:32 +010057}
58
59
60void LCodeGen::FinishCode(Handle<Code> code) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000061 DCHECK(is_done());
Ben Murdoch257744e2011-11-30 15:57:28 +000062 code->set_stack_slots(GetStackSlotCount());
Steve Block1e0659c2011-05-24 12:43:12 +010063 code->set_safepoint_table_offset(safepoints_.GetCodeOffset());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000064 if (code->is_optimized_code()) RegisterWeakObjectsInOptimizedCode(code);
Ben Murdochb0fe1622011-05-05 13:52:32 +010065 PopulateDeoptimizationData(code);
66}
67
68
Ben Murdochb8a8cc12014-11-26 15:28:44 +000069void LCodeGen::SaveCallerDoubles() {
70 DCHECK(info()->saves_caller_doubles());
71 DCHECK(NeedsEagerFrame());
72 Comment(";;; Save clobbered callee double registers");
73 int count = 0;
74 BitVector* doubles = chunk()->allocated_double_registers();
75 BitVector::Iterator save_iterator(doubles);
76 while (!save_iterator.Done()) {
77 __ vstr(DwVfpRegister::FromAllocationIndex(save_iterator.Current()),
78 MemOperand(sp, count * kDoubleSize));
79 save_iterator.Advance();
80 count++;
Ben Murdochb0fe1622011-05-05 13:52:32 +010081 }
Ben Murdochb0fe1622011-05-05 13:52:32 +010082}
83
84
Ben Murdochb8a8cc12014-11-26 15:28:44 +000085void LCodeGen::RestoreCallerDoubles() {
86 DCHECK(info()->saves_caller_doubles());
87 DCHECK(NeedsEagerFrame());
88 Comment(";;; Restore clobbered callee double registers");
89 BitVector* doubles = chunk()->allocated_double_registers();
90 BitVector::Iterator save_iterator(doubles);
91 int count = 0;
92 while (!save_iterator.Done()) {
93 __ vldr(DwVfpRegister::FromAllocationIndex(save_iterator.Current()),
94 MemOperand(sp, count * kDoubleSize));
95 save_iterator.Advance();
96 count++;
97 }
Ben Murdochb0fe1622011-05-05 13:52:32 +010098}
99
100
101bool LCodeGen::GeneratePrologue() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000102 DCHECK(is_generating());
103
104 if (info()->IsOptimizing()) {
105 ProfileEntryHookStub::MaybeCallEntryHook(masm_);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100106
107#ifdef DEBUG
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000108 if (strlen(FLAG_stop_at) > 0 &&
109 info_->function()->name()->IsUtf8EqualTo(CStrVector(FLAG_stop_at))) {
110 __ stop("stop_at");
111 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100112#endif
113
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000114 // r1: Callee's JS function.
115 // cp: Callee's context.
116 // pp: Callee's constant pool pointer (if FLAG_enable_ool_constant_pool)
117 // fp: Caller's frame pointer.
118 // lr: Caller's pc.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100119
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000120 // Sloppy mode functions and builtins need to replace the receiver with the
121 // global proxy when called as functions (without an explicit receiver
122 // object).
123 if (info_->this_has_uses() &&
124 info_->strict_mode() == SLOPPY &&
125 !info_->is_native()) {
126 Label ok;
127 int receiver_offset = info_->scope()->num_parameters() * kPointerSize;
128 __ ldr(r2, MemOperand(sp, receiver_offset));
129 __ CompareRoot(r2, Heap::kUndefinedValueRootIndex);
130 __ b(ne, &ok);
131
132 __ ldr(r2, GlobalObjectOperand());
133 __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalProxyOffset));
134
135 __ str(r2, MemOperand(sp, receiver_offset));
136
137 __ bind(&ok);
138 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000139 }
140
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000141 info()->set_prologue_offset(masm_->pc_offset());
142 if (NeedsEagerFrame()) {
143 if (info()->IsStub()) {
144 __ StubPrologue();
145 } else {
146 __ Prologue(info()->IsCodePreAgingActive());
147 }
148 frame_is_built_ = true;
149 info_->AddNoFrameRange(0, masm_->pc_offset());
150 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100151
152 // Reserve space for the stack slots needed by the code.
Ben Murdoch257744e2011-11-30 15:57:28 +0000153 int slots = GetStackSlotCount();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100154 if (slots > 0) {
155 if (FLAG_debug_code) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000156 __ sub(sp, sp, Operand(slots * kPointerSize));
157 __ push(r0);
158 __ push(r1);
159 __ add(r0, sp, Operand(slots * kPointerSize));
160 __ mov(r1, Operand(kSlotsZapValue));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100161 Label loop;
162 __ bind(&loop);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000163 __ sub(r0, r0, Operand(kPointerSize));
164 __ str(r1, MemOperand(r0, 2 * kPointerSize));
165 __ cmp(r0, sp);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100166 __ b(ne, &loop);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000167 __ pop(r1);
168 __ pop(r0);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100169 } else {
170 __ sub(sp, sp, Operand(slots * kPointerSize));
171 }
172 }
173
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000174 if (info()->saves_caller_doubles()) {
175 SaveCallerDoubles();
176 }
177
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100178 // Possibly allocate a local context.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000179 int heap_slots = info()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100180 if (heap_slots > 0) {
181 Comment(";;; Allocate local context");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000182 bool need_write_barrier = true;
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100183 // Argument to NewContext is the function, which is in r1.
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100184 if (heap_slots <= FastNewContextStub::kMaximumSlots) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000185 FastNewContextStub stub(isolate(), heap_slots);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100186 __ CallStub(&stub);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000187 // Result of FastNewContextStub is always in new space.
188 need_write_barrier = false;
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100189 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000190 __ push(r1);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000191 __ CallRuntime(Runtime::kNewFunctionContext, 1);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100192 }
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000193 RecordSafepoint(Safepoint::kNoLazyDeopt);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100194 // Context is returned in both r0 and cp. It replaces the context
195 // passed to us. It's saved in the stack and kept live in cp.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000196 __ mov(cp, r0);
197 __ str(r0, MemOperand(fp, StandardFrameConstants::kContextOffset));
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100198 // Copy any necessary parameters into the context.
199 int num_parameters = scope()->num_parameters();
200 for (int i = 0; i < num_parameters; i++) {
Ben Murdoch589d6972011-11-30 16:04:58 +0000201 Variable* var = scope()->parameter(i);
202 if (var->IsContextSlot()) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100203 int parameter_offset = StandardFrameConstants::kCallerSPOffset +
204 (num_parameters - 1 - i) * kPointerSize;
205 // Load parameter from stack.
206 __ ldr(r0, MemOperand(fp, parameter_offset));
207 // Store it in the context.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100208 MemOperand target = ContextOperand(cp, var->index());
209 __ str(r0, target);
210 // Update the write barrier. This clobbers r3 and r0.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000211 if (need_write_barrier) {
212 __ RecordWriteContextSlot(
213 cp,
214 target.offset(),
215 r0,
216 r3,
217 GetLinkRegisterState(),
218 kSaveFPRegs);
219 } else if (FLAG_debug_code) {
220 Label done;
221 __ JumpIfInNewSpace(cp, r0, &done);
222 __ Abort(kExpectedNewSpaceObject);
223 __ bind(&done);
224 }
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100225 }
226 }
227 Comment(";;; End allocate local context");
228 }
229
Ben Murdochb0fe1622011-05-05 13:52:32 +0100230 // Trace the call.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000231 if (FLAG_trace && info()->IsOptimizing()) {
232 // We have not executed any compiled code yet, so cp still holds the
233 // incoming context.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100234 __ CallRuntime(Runtime::kTraceEnter, 0);
235 }
236 return !is_aborted();
237}
238
239
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000240void LCodeGen::GenerateOsrPrologue() {
241 // Generate the OSR entry prologue at the first unknown OSR value, or if there
242 // are none, at the OSR entrypoint instruction.
243 if (osr_pc_offset_ >= 0) return;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100244
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000245 osr_pc_offset_ = masm()->pc_offset();
246
247 // Adjust the frame size, subsuming the unoptimized frame into the
248 // optimized frame.
249 int slots = GetStackSlotCount() - graph()->osr()->UnoptimizedFrameSlots();
250 DCHECK(slots >= 0);
251 __ sub(sp, sp, Operand(slots * kPointerSize));
252}
253
254
255void LCodeGen::GenerateBodyInstructionPre(LInstruction* instr) {
256 if (instr->IsCall()) {
257 EnsureSpaceForLazyDeopt(Deoptimizer::patch_size());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100258 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000259 if (!instr->IsLazyBailout() && !instr->IsGap()) {
260 safepoints_.BumpLastLazySafepointIndex();
261 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100262}
263
264
Ben Murdochb0fe1622011-05-05 13:52:32 +0100265bool LCodeGen::GenerateDeferredCode() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000266 DCHECK(is_generating());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000267 if (deferred_.length() > 0) {
268 for (int i = 0; !is_aborted() && i < deferred_.length(); i++) {
269 LDeferredCode* code = deferred_[i];
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000270
271 HValue* value =
272 instructions_->at(code->instruction_index())->hydrogen_value();
273 RecordAndWritePosition(
274 chunk()->graph()->SourcePositionToScriptPosition(value->position()));
275
276 Comment(";;; <@%d,#%d> "
277 "-------------------- Deferred %s --------------------",
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100278 code->instruction_index(),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000279 code->instr()->hydrogen_value()->id(),
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100280 code->instr()->Mnemonic());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000281 __ bind(code->entry());
282 if (NeedsDeferredFrame()) {
283 Comment(";;; Build frame");
284 DCHECK(!frame_is_built_);
285 DCHECK(info()->IsStub());
286 frame_is_built_ = true;
287 __ PushFixedFrame();
288 __ mov(scratch0(), Operand(Smi::FromInt(StackFrame::STUB)));
289 __ push(scratch0());
290 __ add(fp, sp, Operand(StandardFrameConstants::kFixedFrameSizeFromFp));
291 Comment(";;; Deferred code");
292 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000293 code->Generate();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000294 if (NeedsDeferredFrame()) {
295 Comment(";;; Destroy frame");
296 DCHECK(frame_is_built_);
297 __ pop(ip);
298 __ PopFixedFrame();
299 frame_is_built_ = false;
300 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000301 __ jmp(code->exit());
302 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100303 }
304
Ben Murdoch257744e2011-11-30 15:57:28 +0000305 // Force constant pool emission at the end of the deferred code to make
306 // sure that no constant pools are emitted after.
Ben Murdochb8e0da22011-05-16 14:20:40 +0100307 masm()->CheckConstPool(true, false);
308
Ben Murdoch257744e2011-11-30 15:57:28 +0000309 return !is_aborted();
310}
311
312
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000313bool LCodeGen::GenerateJumpTable() {
Ben Murdoch257744e2011-11-30 15:57:28 +0000314 // Check that the jump table is accessible from everywhere in the function
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100315 // code, i.e. that offsets to the table can be encoded in the 24bit signed
Ben Murdoch257744e2011-11-30 15:57:28 +0000316 // immediate of a branch instruction.
317 // To simplify we consider the code size from the first instruction to the
318 // end of the jump table. We also don't consider the pc load delta.
319 // Each entry in the jump table generates one instruction and inlines one
320 // 32bit data after it.
321 if (!is_int24((masm()->pc_offset() / Assembler::kInstrSize) +
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000322 jump_table_.length() * 7)) {
323 Abort(kGeneratedCodeIsTooLarge);
Ben Murdoch257744e2011-11-30 15:57:28 +0000324 }
325
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000326 if (jump_table_.length() > 0) {
327 Label needs_frame, call_deopt_entry;
328
329 Comment(";;; -------------------- Jump table --------------------");
330 Address base = jump_table_[0].address;
331
332 Register entry_offset = scratch0();
333
334 int length = jump_table_.length();
335 for (int i = 0; i < length; i++) {
336 Deoptimizer::JumpTableEntry* table_entry = &jump_table_[i];
337 __ bind(&table_entry->label);
338
339 DCHECK_EQ(jump_table_[0].bailout_type, table_entry->bailout_type);
340 Address entry = table_entry->address;
341 DeoptComment(table_entry->reason);
342
343 // Second-level deopt table entries are contiguous and small, so instead
344 // of loading the full, absolute address of each one, load an immediate
345 // offset which will be added to the base address later.
346 __ mov(entry_offset, Operand(entry - base));
347
348 if (table_entry->needs_frame) {
349 DCHECK(!info()->saves_caller_doubles());
350 if (needs_frame.is_bound()) {
351 __ b(&needs_frame);
352 } else {
353 __ bind(&needs_frame);
354 Comment(";;; call deopt with frame");
355 __ PushFixedFrame();
356 // This variant of deopt can only be used with stubs. Since we don't
357 // have a function pointer to install in the stack frame that we're
358 // building, install a special marker there instead.
359 DCHECK(info()->IsStub());
360 __ mov(ip, Operand(Smi::FromInt(StackFrame::STUB)));
361 __ push(ip);
362 __ add(fp, sp,
363 Operand(StandardFrameConstants::kFixedFrameSizeFromFp));
364 __ bind(&call_deopt_entry);
365 // Add the base address to the offset previously loaded in
366 // entry_offset.
367 __ add(entry_offset, entry_offset,
368 Operand(ExternalReference::ForDeoptEntry(base)));
369 __ blx(entry_offset);
370 }
371
372 masm()->CheckConstPool(false, false);
373 } else {
374 // The last entry can fall through into `call_deopt_entry`, avoiding a
375 // branch.
376 bool need_branch = ((i + 1) != length) || call_deopt_entry.is_bound();
377
378 if (need_branch) __ b(&call_deopt_entry);
379
380 masm()->CheckConstPool(false, !need_branch);
381 }
382 }
383
384 if (!call_deopt_entry.is_bound()) {
385 Comment(";;; call deopt");
386 __ bind(&call_deopt_entry);
387
388 if (info()->saves_caller_doubles()) {
389 DCHECK(info()->IsStub());
390 RestoreCallerDoubles();
391 }
392
393 // Add the base address to the offset previously loaded in entry_offset.
394 __ add(entry_offset, entry_offset,
395 Operand(ExternalReference::ForDeoptEntry(base)));
396 __ blx(entry_offset);
397 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000398 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000399
400 // Force constant pool emission at the end of the deopt jump table to make
401 // sure that no constant pools are emitted after.
402 masm()->CheckConstPool(true, false);
Ben Murdoch257744e2011-11-30 15:57:28 +0000403
404 // The deoptimization jump table is the last part of the instruction
405 // sequence. Mark the generated code as done unless we bailed out.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100406 if (!is_aborted()) status_ = DONE;
407 return !is_aborted();
408}
409
410
411bool LCodeGen::GenerateSafepointTable() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000412 DCHECK(is_done());
Ben Murdoch257744e2011-11-30 15:57:28 +0000413 safepoints_.Emit(masm(), GetStackSlotCount());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100414 return !is_aborted();
415}
416
417
418Register LCodeGen::ToRegister(int index) const {
419 return Register::FromAllocationIndex(index);
420}
421
422
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000423DwVfpRegister LCodeGen::ToDoubleRegister(int index) const {
424 return DwVfpRegister::FromAllocationIndex(index);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100425}
426
427
428Register LCodeGen::ToRegister(LOperand* op) const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000429 DCHECK(op->IsRegister());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100430 return ToRegister(op->index());
431}
432
433
434Register LCodeGen::EmitLoadRegister(LOperand* op, Register scratch) {
435 if (op->IsRegister()) {
436 return ToRegister(op->index());
437 } else if (op->IsConstantOperand()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100438 LConstantOperand* const_op = LConstantOperand::cast(op);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000439 HConstant* constant = chunk_->LookupConstant(const_op);
440 Handle<Object> literal = constant->handle(isolate());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100441 Representation r = chunk_->LookupLiteralRepresentation(const_op);
442 if (r.IsInteger32()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000443 DCHECK(literal->IsNumber());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100444 __ mov(scratch, Operand(static_cast<int32_t>(literal->Number())));
445 } else if (r.IsDouble()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000446 Abort(kEmitLoadRegisterUnsupportedDoubleImmediate);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100447 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000448 DCHECK(r.IsSmiOrTagged());
449 __ Move(scratch, literal);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100450 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100451 return scratch;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000452 } else if (op->IsStackSlot()) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100453 __ ldr(scratch, ToMemOperand(op));
454 return scratch;
455 }
456 UNREACHABLE();
457 return scratch;
458}
459
460
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000461DwVfpRegister LCodeGen::ToDoubleRegister(LOperand* op) const {
462 DCHECK(op->IsDoubleRegister());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100463 return ToDoubleRegister(op->index());
464}
465
466
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000467DwVfpRegister LCodeGen::EmitLoadDoubleRegister(LOperand* op,
468 SwVfpRegister flt_scratch,
469 DwVfpRegister dbl_scratch) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100470 if (op->IsDoubleRegister()) {
471 return ToDoubleRegister(op->index());
472 } else if (op->IsConstantOperand()) {
473 LConstantOperand* const_op = LConstantOperand::cast(op);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000474 HConstant* constant = chunk_->LookupConstant(const_op);
475 Handle<Object> literal = constant->handle(isolate());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100476 Representation r = chunk_->LookupLiteralRepresentation(const_op);
477 if (r.IsInteger32()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000478 DCHECK(literal->IsNumber());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100479 __ mov(ip, Operand(static_cast<int32_t>(literal->Number())));
480 __ vmov(flt_scratch, ip);
481 __ vcvt_f64_s32(dbl_scratch, flt_scratch);
482 return dbl_scratch;
483 } else if (r.IsDouble()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000484 Abort(kUnsupportedDoubleImmediate);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100485 } else if (r.IsTagged()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000486 Abort(kUnsupportedTaggedImmediate);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100487 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000488 } else if (op->IsStackSlot()) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100489 // TODO(regis): Why is vldr not taking a MemOperand?
490 // __ vldr(dbl_scratch, ToMemOperand(op));
491 MemOperand mem_op = ToMemOperand(op);
492 __ vldr(dbl_scratch, mem_op.rn(), mem_op.offset());
493 return dbl_scratch;
494 }
495 UNREACHABLE();
496 return dbl_scratch;
497}
498
499
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100500Handle<Object> LCodeGen::ToHandle(LConstantOperand* op) const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000501 HConstant* constant = chunk_->LookupConstant(op);
502 DCHECK(chunk_->LookupLiteralRepresentation(op).IsSmiOrTagged());
503 return constant->handle(isolate());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100504}
505
506
507bool LCodeGen::IsInteger32(LConstantOperand* op) const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000508 return chunk_->LookupLiteralRepresentation(op).IsSmiOrInteger32();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100509}
510
511
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000512bool LCodeGen::IsSmi(LConstantOperand* op) const {
513 return chunk_->LookupLiteralRepresentation(op).IsSmi();
514}
515
516
517int32_t LCodeGen::ToInteger32(LConstantOperand* op) const {
518 return ToRepresentation(op, Representation::Integer32());
519}
520
521
522int32_t LCodeGen::ToRepresentation(LConstantOperand* op,
523 const Representation& r) const {
524 HConstant* constant = chunk_->LookupConstant(op);
525 int32_t value = constant->Integer32Value();
526 if (r.IsInteger32()) return value;
527 DCHECK(r.IsSmiOrTagged());
528 return reinterpret_cast<int32_t>(Smi::FromInt(value));
529}
530
531
532Smi* LCodeGen::ToSmi(LConstantOperand* op) const {
533 HConstant* constant = chunk_->LookupConstant(op);
534 return Smi::FromInt(constant->Integer32Value());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100535}
536
537
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100538double LCodeGen::ToDouble(LConstantOperand* op) const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000539 HConstant* constant = chunk_->LookupConstant(op);
540 DCHECK(constant->HasDoubleValue());
541 return constant->DoubleValue();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100542}
543
544
Ben Murdochb0fe1622011-05-05 13:52:32 +0100545Operand LCodeGen::ToOperand(LOperand* op) {
546 if (op->IsConstantOperand()) {
547 LConstantOperand* const_op = LConstantOperand::cast(op);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000548 HConstant* constant = chunk()->LookupConstant(const_op);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100549 Representation r = chunk_->LookupLiteralRepresentation(const_op);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000550 if (r.IsSmi()) {
551 DCHECK(constant->HasSmiValue());
552 return Operand(Smi::FromInt(constant->Integer32Value()));
553 } else if (r.IsInteger32()) {
554 DCHECK(constant->HasInteger32Value());
555 return Operand(constant->Integer32Value());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100556 } else if (r.IsDouble()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000557 Abort(kToOperandUnsupportedDoubleImmediate);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100558 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000559 DCHECK(r.IsTagged());
560 return Operand(constant->handle(isolate()));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100561 } else if (op->IsRegister()) {
562 return Operand(ToRegister(op));
563 } else if (op->IsDoubleRegister()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000564 Abort(kToOperandIsDoubleRegisterUnimplemented);
565 return Operand::Zero();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100566 }
567 // Stack slots not implemented, use ToMemOperand instead.
568 UNREACHABLE();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000569 return Operand::Zero();
570}
571
572
573static int ArgumentsOffsetWithoutFrame(int index) {
574 DCHECK(index < 0);
575 return -(index + 1) * kPointerSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100576}
577
578
579MemOperand LCodeGen::ToMemOperand(LOperand* op) const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000580 DCHECK(!op->IsRegister());
581 DCHECK(!op->IsDoubleRegister());
582 DCHECK(op->IsStackSlot() || op->IsDoubleStackSlot());
583 if (NeedsEagerFrame()) {
584 return MemOperand(fp, StackSlotOffset(op->index()));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100585 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000586 // Retrieve parameter without eager stack-frame relative to the
587 // stack-pointer.
588 return MemOperand(sp, ArgumentsOffsetWithoutFrame(op->index()));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100589 }
590}
591
592
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100593MemOperand LCodeGen::ToHighMemOperand(LOperand* op) const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000594 DCHECK(op->IsDoubleStackSlot());
595 if (NeedsEagerFrame()) {
596 return MemOperand(fp, StackSlotOffset(op->index()) + kPointerSize);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100597 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000598 // Retrieve parameter without eager stack-frame relative to the
599 // stack-pointer.
600 return MemOperand(
601 sp, ArgumentsOffsetWithoutFrame(op->index()) + kPointerSize);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100602 }
603}
604
605
Ben Murdochb8e0da22011-05-16 14:20:40 +0100606void LCodeGen::WriteTranslation(LEnvironment* environment,
607 Translation* translation) {
608 if (environment == NULL) return;
609
610 // The translation includes one command per value in the environment.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000611 int translation_size = environment->translation_size();
Ben Murdochb8e0da22011-05-16 14:20:40 +0100612 // The output frame height does not include the parameters.
613 int height = translation_size - environment->parameter_count();
614
615 WriteTranslation(environment->outer(), translation);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000616 bool has_closure_id = !info()->closure().is_null() &&
617 !info()->closure().is_identical_to(environment->closure());
618 int closure_id = has_closure_id
619 ? DefineDeoptimizationLiteral(environment->closure())
620 : Translation::kSelfLiteralId;
621
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100622 switch (environment->frame_type()) {
623 case JS_FUNCTION:
624 translation->BeginJSFrame(environment->ast_id(), closure_id, height);
625 break;
626 case JS_CONSTRUCT:
627 translation->BeginConstructStubFrame(closure_id, translation_size);
628 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000629 case JS_GETTER:
630 DCHECK(translation_size == 1);
631 DCHECK(height == 0);
632 translation->BeginGetterStubFrame(closure_id);
633 break;
634 case JS_SETTER:
635 DCHECK(translation_size == 2);
636 DCHECK(height == 0);
637 translation->BeginSetterStubFrame(closure_id);
638 break;
639 case STUB:
640 translation->BeginCompiledStubFrame();
641 break;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100642 case ARGUMENTS_ADAPTOR:
643 translation->BeginArgumentsAdaptorFrame(closure_id, translation_size);
644 break;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100645 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000646
647 int object_index = 0;
648 int dematerialized_index = 0;
Ben Murdochb8e0da22011-05-16 14:20:40 +0100649 for (int i = 0; i < translation_size; ++i) {
650 LOperand* value = environment->values()->at(i);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000651 AddToTranslation(environment,
652 translation,
653 value,
654 environment->HasTaggedValueAt(i),
655 environment->HasUint32ValueAt(i),
656 &object_index,
657 &dematerialized_index);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100658 }
659}
660
661
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000662void LCodeGen::AddToTranslation(LEnvironment* environment,
663 Translation* translation,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100664 LOperand* op,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000665 bool is_tagged,
666 bool is_uint32,
667 int* object_index_pointer,
668 int* dematerialized_index_pointer) {
669 if (op == LEnvironment::materialization_marker()) {
670 int object_index = (*object_index_pointer)++;
671 if (environment->ObjectIsDuplicateAt(object_index)) {
672 int dupe_of = environment->ObjectDuplicateOfAt(object_index);
673 translation->DuplicateObject(dupe_of);
674 return;
675 }
676 int object_length = environment->ObjectLengthAt(object_index);
677 if (environment->ObjectIsArgumentsAt(object_index)) {
678 translation->BeginArgumentsObject(object_length);
679 } else {
680 translation->BeginCapturedObject(object_length);
681 }
682 int dematerialized_index = *dematerialized_index_pointer;
683 int env_offset = environment->translation_size() + dematerialized_index;
684 *dematerialized_index_pointer += object_length;
685 for (int i = 0; i < object_length; ++i) {
686 LOperand* value = environment->values()->at(env_offset + i);
687 AddToTranslation(environment,
688 translation,
689 value,
690 environment->HasTaggedValueAt(env_offset + i),
691 environment->HasUint32ValueAt(env_offset + i),
692 object_index_pointer,
693 dematerialized_index_pointer);
694 }
695 return;
696 }
697
698 if (op->IsStackSlot()) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100699 if (is_tagged) {
700 translation->StoreStackSlot(op->index());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000701 } else if (is_uint32) {
702 translation->StoreUint32StackSlot(op->index());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100703 } else {
704 translation->StoreInt32StackSlot(op->index());
705 }
706 } else if (op->IsDoubleStackSlot()) {
707 translation->StoreDoubleStackSlot(op->index());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100708 } else if (op->IsRegister()) {
709 Register reg = ToRegister(op);
710 if (is_tagged) {
711 translation->StoreRegister(reg);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000712 } else if (is_uint32) {
713 translation->StoreUint32Register(reg);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100714 } else {
715 translation->StoreInt32Register(reg);
716 }
717 } else if (op->IsDoubleRegister()) {
718 DoubleRegister reg = ToDoubleRegister(op);
719 translation->StoreDoubleRegister(reg);
720 } else if (op->IsConstantOperand()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000721 HConstant* constant = chunk()->LookupConstant(LConstantOperand::cast(op));
722 int src_index = DefineDeoptimizationLiteral(constant->handle(isolate()));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100723 translation->StoreLiteral(src_index);
724 } else {
725 UNREACHABLE();
726 }
727}
728
729
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000730int LCodeGen::CallCodeSize(Handle<Code> code, RelocInfo::Mode mode) {
731 int size = masm()->CallSize(code, mode);
732 if (code->kind() == Code::BINARY_OP_IC ||
733 code->kind() == Code::COMPARE_IC) {
734 size += Assembler::kInstrSize; // extra nop() added in CallCodeGeneric.
735 }
736 return size;
737}
738
739
Ben Murdochb0fe1622011-05-05 13:52:32 +0100740void LCodeGen::CallCode(Handle<Code> code,
741 RelocInfo::Mode mode,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000742 LInstruction* instr,
743 TargetAddressStorageMode storage_mode) {
744 CallCodeGeneric(code, mode, instr, RECORD_SIMPLE_SAFEPOINT, storage_mode);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100745}
746
747
748void LCodeGen::CallCodeGeneric(Handle<Code> code,
749 RelocInfo::Mode mode,
750 LInstruction* instr,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000751 SafepointMode safepoint_mode,
752 TargetAddressStorageMode storage_mode) {
753 DCHECK(instr != NULL);
754 // Block literal pool emission to ensure nop indicating no inlined smi code
755 // is in the correct position.
756 Assembler::BlockConstPoolScope block_const_pool(masm());
757 __ Call(code, mode, TypeFeedbackId::None(), al, storage_mode);
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000758 RecordSafepointWithLazyDeopt(instr, safepoint_mode);
Ben Murdoch18a6f572011-07-25 17:16:09 +0100759
760 // Signal that we don't inline smi code before these stubs in the
761 // optimizing code generator.
Ben Murdoch257744e2011-11-30 15:57:28 +0000762 if (code->kind() == Code::BINARY_OP_IC ||
Ben Murdoch18a6f572011-07-25 17:16:09 +0100763 code->kind() == Code::COMPARE_IC) {
764 __ nop();
765 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100766}
767
768
Steve Block44f0eee2011-05-26 01:26:41 +0100769void LCodeGen::CallRuntime(const Runtime::Function* function,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100770 int num_arguments,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000771 LInstruction* instr,
772 SaveFPRegsMode save_doubles) {
773 DCHECK(instr != NULL);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100774
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000775 __ CallRuntime(function, num_arguments, save_doubles);
776
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000777 RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100778}
779
780
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000781void LCodeGen::LoadContextFromDeferred(LOperand* context) {
782 if (context->IsRegister()) {
783 __ Move(cp, ToRegister(context));
784 } else if (context->IsStackSlot()) {
785 __ ldr(cp, ToMemOperand(context));
786 } else if (context->IsConstantOperand()) {
787 HConstant* constant =
788 chunk_->LookupConstant(LConstantOperand::cast(context));
789 __ Move(cp, Handle<Object>::cast(constant->handle(isolate())));
790 } else {
791 UNREACHABLE();
792 }
793}
794
795
Ben Murdoch8b112d22011-06-08 16:22:53 +0100796void LCodeGen::CallRuntimeFromDeferred(Runtime::FunctionId id,
797 int argc,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000798 LInstruction* instr,
799 LOperand* context) {
800 LoadContextFromDeferred(context);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100801 __ CallRuntimeSaveDoubles(id);
802 RecordSafepointWithRegisters(
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000803 instr->pointer_map(), argc, Safepoint::kNoLazyDeopt);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100804}
805
806
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000807void LCodeGen::RegisterEnvironmentForDeoptimization(LEnvironment* environment,
808 Safepoint::DeoptMode mode) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000809 environment->set_has_been_used();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100810 if (!environment->HasBeenRegistered()) {
811 // Physical stack frame layout:
812 // -x ............. -4 0 ..................................... y
813 // [incoming arguments] [spill slots] [pushed outgoing arguments]
814
815 // Layout of the environment:
816 // 0 ..................................................... size-1
817 // [parameters] [locals] [expression stack including arguments]
818
819 // Layout of the translation:
820 // 0 ........................................................ size - 1 + 4
821 // [expression stack including arguments] [locals] [4 words] [parameters]
822 // |>------------ translation_size ------------<|
823
824 int frame_count = 0;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100825 int jsframe_count = 0;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100826 for (LEnvironment* e = environment; e != NULL; e = e->outer()) {
827 ++frame_count;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100828 if (e->frame_type() == JS_FUNCTION) {
829 ++jsframe_count;
830 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100831 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000832 Translation translation(&translations_, frame_count, jsframe_count, zone());
Ben Murdochb8e0da22011-05-16 14:20:40 +0100833 WriteTranslation(environment, &translation);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100834 int deoptimization_index = deoptimizations_.length();
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000835 int pc_offset = masm()->pc_offset();
836 environment->Register(deoptimization_index,
837 translation.index(),
838 (mode == Safepoint::kLazyDeopt) ? pc_offset : -1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000839 deoptimizations_.Add(environment, zone());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100840 }
841}
842
843
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000844void LCodeGen::DeoptimizeIf(Condition condition, LInstruction* instr,
845 const char* detail,
846 Deoptimizer::BailoutType bailout_type) {
847 LEnvironment* environment = instr->environment();
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000848 RegisterEnvironmentForDeoptimization(environment, Safepoint::kNoLazyDeopt);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000849 DCHECK(environment->HasBeenRegistered());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100850 int id = environment->deoptimization_index();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000851 DCHECK(info()->IsOptimizing() || info()->IsStub());
852 Address entry =
853 Deoptimizer::GetDeoptimizationEntry(isolate(), id, bailout_type);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100854 if (entry == NULL) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000855 Abort(kBailoutWasNotPrepared);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100856 return;
857 }
858
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000859 if (FLAG_deopt_every_n_times != 0 && !info()->IsStub()) {
860 Register scratch = scratch0();
861 ExternalReference count = ExternalReference::stress_deopt_count(isolate());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100862
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000863 // Store the condition on the stack if necessary
864 if (condition != al) {
865 __ mov(scratch, Operand::Zero(), LeaveCC, NegateCondition(condition));
866 __ mov(scratch, Operand(1), LeaveCC, condition);
867 __ push(scratch);
868 }
869
870 __ push(r1);
871 __ mov(scratch, Operand(count));
872 __ ldr(r1, MemOperand(scratch));
873 __ sub(r1, r1, Operand(1), SetCC);
874 __ mov(r1, Operand(FLAG_deopt_every_n_times), LeaveCC, eq);
875 __ str(r1, MemOperand(scratch));
876 __ pop(r1);
877
878 if (condition != al) {
879 // Clean up the stack before the deoptimizer call
880 __ pop(scratch);
881 }
882
883 __ Call(entry, RelocInfo::RUNTIME_ENTRY, eq);
884
885 // 'Restore' the condition in a slightly hacky way. (It would be better
886 // to use 'msr' and 'mrs' instructions here, but they are not supported by
887 // our ARM simulator).
888 if (condition != al) {
889 condition = ne;
890 __ cmp(scratch, Operand::Zero());
891 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100892 }
893
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000894 if (info()->ShouldTrapOnDeopt()) {
895 __ stop("trap_on_deopt", condition);
896 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000897
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000898 Deoptimizer::Reason reason(instr->hydrogen_value()->position().raw(),
899 instr->Mnemonic(), detail);
900 DCHECK(info()->IsStub() || frame_is_built_);
901 // Go through jump table if we need to handle condition, build frame, or
902 // restore caller doubles.
903 if (condition == al && frame_is_built_ &&
904 !info()->saves_caller_doubles()) {
905 DeoptComment(reason);
906 __ Call(entry, RelocInfo::RUNTIME_ENTRY);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100907 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000908 Deoptimizer::JumpTableEntry table_entry(entry, reason, bailout_type,
909 !frame_is_built_);
Ben Murdoch257744e2011-11-30 15:57:28 +0000910 // We often have several deopts to the same entry, reuse the last
911 // jump entry if this is the case.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000912 if (jump_table_.is_empty() ||
913 !table_entry.IsEquivalentTo(jump_table_.last())) {
914 jump_table_.Add(table_entry, zone());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100915 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000916 __ b(condition, &jump_table_.last().label);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100917 }
918}
919
920
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000921void LCodeGen::DeoptimizeIf(Condition condition, LInstruction* instr,
922 const char* detail) {
923 Deoptimizer::BailoutType bailout_type = info()->IsStub()
924 ? Deoptimizer::LAZY
925 : Deoptimizer::EAGER;
926 DeoptimizeIf(condition, instr, detail, bailout_type);
927}
928
929
Ben Murdochb0fe1622011-05-05 13:52:32 +0100930void LCodeGen::PopulateDeoptimizationData(Handle<Code> code) {
931 int length = deoptimizations_.length();
932 if (length == 0) return;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100933 Handle<DeoptimizationInputData> data =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000934 DeoptimizationInputData::New(isolate(), length, TENURED);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100935
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000936 Handle<ByteArray> translations =
937 translations_.CreateByteArray(isolate()->factory());
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100938 data->SetTranslationByteArray(*translations);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100939 data->SetInlinedFunctionCount(Smi::FromInt(inlined_function_count_));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000940 data->SetOptimizationId(Smi::FromInt(info_->optimization_id()));
941 if (info_->IsOptimizing()) {
942 // Reference to shared function info does not change between phases.
943 AllowDeferredHandleDereference allow_handle_dereference;
944 data->SetSharedFunctionInfo(*info_->shared_info());
945 } else {
946 data->SetSharedFunctionInfo(Smi::FromInt(0));
947 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100948
949 Handle<FixedArray> literals =
Steve Block44f0eee2011-05-26 01:26:41 +0100950 factory()->NewFixedArray(deoptimization_literals_.length(), TENURED);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000951 { AllowDeferredHandleDereference copy_handles;
952 for (int i = 0; i < deoptimization_literals_.length(); i++) {
953 literals->set(i, *deoptimization_literals_[i]);
954 }
955 data->SetLiteralArray(*literals);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100956 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100957
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000958 data->SetOsrAstId(Smi::FromInt(info_->osr_ast_id().ToInt()));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100959 data->SetOsrPcOffset(Smi::FromInt(osr_pc_offset_));
960
961 // Populate the deoptimization entries.
962 for (int i = 0; i < length; i++) {
963 LEnvironment* env = deoptimizations_[i];
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000964 data->SetAstId(i, env->ast_id());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100965 data->SetTranslationIndex(i, Smi::FromInt(env->translation_index()));
966 data->SetArgumentsStackHeight(i,
967 Smi::FromInt(env->arguments_stack_height()));
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000968 data->SetPc(i, Smi::FromInt(env->pc_offset()));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100969 }
970 code->set_deoptimization_data(*data);
971}
972
973
974int LCodeGen::DefineDeoptimizationLiteral(Handle<Object> literal) {
975 int result = deoptimization_literals_.length();
976 for (int i = 0; i < deoptimization_literals_.length(); ++i) {
977 if (deoptimization_literals_[i].is_identical_to(literal)) return i;
978 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000979 deoptimization_literals_.Add(literal, zone());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100980 return result;
981}
982
983
984void LCodeGen::PopulateDeoptimizationLiteralsWithInlinedFunctions() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000985 DCHECK(deoptimization_literals_.length() == 0);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100986
987 const ZoneList<Handle<JSFunction> >* inlined_closures =
988 chunk()->inlined_closures();
989
990 for (int i = 0, length = inlined_closures->length();
991 i < length;
992 i++) {
993 DefineDeoptimizationLiteral(inlined_closures->at(i));
994 }
995
996 inlined_function_count_ = deoptimization_literals_.length();
997}
998
999
Ben Murdoch2b4ba112012-01-20 14:57:15 +00001000void LCodeGen::RecordSafepointWithLazyDeopt(
1001 LInstruction* instr, SafepointMode safepoint_mode) {
1002 if (safepoint_mode == RECORD_SIMPLE_SAFEPOINT) {
1003 RecordSafepoint(instr->pointer_map(), Safepoint::kLazyDeopt);
1004 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001005 DCHECK(safepoint_mode == RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS);
Ben Murdoch2b4ba112012-01-20 14:57:15 +00001006 RecordSafepointWithRegisters(
1007 instr->pointer_map(), 0, Safepoint::kLazyDeopt);
1008 }
1009}
1010
1011
Steve Block1e0659c2011-05-24 12:43:12 +01001012void LCodeGen::RecordSafepoint(
1013 LPointerMap* pointers,
1014 Safepoint::Kind kind,
1015 int arguments,
Ben Murdoch2b4ba112012-01-20 14:57:15 +00001016 Safepoint::DeoptMode deopt_mode) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001017 DCHECK(expected_safepoint_kind_ == kind);
Ben Murdoch8b112d22011-06-08 16:22:53 +01001018
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001019 const ZoneList<LOperand*>* operands = pointers->GetNormalizedOperands();
Ben Murdochb0fe1622011-05-05 13:52:32 +01001020 Safepoint safepoint = safepoints_.DefineSafepoint(masm(),
Ben Murdoch2b4ba112012-01-20 14:57:15 +00001021 kind, arguments, deopt_mode);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001022 for (int i = 0; i < operands->length(); i++) {
1023 LOperand* pointer = operands->at(i);
1024 if (pointer->IsStackSlot()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001025 safepoint.DefinePointerSlot(pointer->index(), zone());
Steve Block1e0659c2011-05-24 12:43:12 +01001026 } else if (pointer->IsRegister() && (kind & Safepoint::kWithRegisters)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001027 safepoint.DefinePointerRegister(ToRegister(pointer), zone());
Ben Murdochb0fe1622011-05-05 13:52:32 +01001028 }
1029 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001030 if (FLAG_enable_ool_constant_pool && (kind & Safepoint::kWithRegisters)) {
1031 // Register pp always contains a pointer to the constant pool.
1032 safepoint.DefinePointerRegister(pp, zone());
Steve Block1e0659c2011-05-24 12:43:12 +01001033 }
1034}
1035
1036
1037void LCodeGen::RecordSafepoint(LPointerMap* pointers,
Ben Murdoch2b4ba112012-01-20 14:57:15 +00001038 Safepoint::DeoptMode deopt_mode) {
1039 RecordSafepoint(pointers, Safepoint::kSimple, 0, deopt_mode);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001040}
1041
1042
Ben Murdoch2b4ba112012-01-20 14:57:15 +00001043void LCodeGen::RecordSafepoint(Safepoint::DeoptMode deopt_mode) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001044 LPointerMap empty_pointers(zone());
Ben Murdoch2b4ba112012-01-20 14:57:15 +00001045 RecordSafepoint(&empty_pointers, deopt_mode);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001046}
1047
1048
Ben Murdochb0fe1622011-05-05 13:52:32 +01001049void LCodeGen::RecordSafepointWithRegisters(LPointerMap* pointers,
1050 int arguments,
Ben Murdoch2b4ba112012-01-20 14:57:15 +00001051 Safepoint::DeoptMode deopt_mode) {
1052 RecordSafepoint(
1053 pointers, Safepoint::kWithRegisters, arguments, deopt_mode);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001054}
1055
1056
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001057void LCodeGen::RecordAndWritePosition(int position) {
1058 if (position == RelocInfo::kNoPosition) return;
1059 masm()->positions_recorder()->RecordPosition(position);
1060 masm()->positions_recorder()->WriteRecordedPositions();
Ben Murdochb8e0da22011-05-16 14:20:40 +01001061}
1062
1063
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001064static const char* LabelType(LLabel* label) {
1065 if (label->is_loop_header()) return " (loop header)";
1066 if (label->is_osr_entry()) return " (OSR entry)";
1067 return "";
Ben Murdochb0fe1622011-05-05 13:52:32 +01001068}
1069
1070
1071void LCodeGen::DoLabel(LLabel* label) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001072 Comment(";;; <@%d,#%d> -------------------- B%d%s --------------------",
1073 current_instruction_,
1074 label->hydrogen_value()->id(),
1075 label->block_id(),
1076 LabelType(label));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001077 __ bind(label->label());
1078 current_block_ = label->block_id();
Ben Murdoch257744e2011-11-30 15:57:28 +00001079 DoGap(label);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001080}
1081
1082
1083void LCodeGen::DoParallelMove(LParallelMove* move) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001084 resolver_.Resolve(move);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001085}
1086
1087
1088void LCodeGen::DoGap(LGap* gap) {
1089 for (int i = LGap::FIRST_INNER_POSITION;
1090 i <= LGap::LAST_INNER_POSITION;
1091 i++) {
1092 LGap::InnerPosition inner_pos = static_cast<LGap::InnerPosition>(i);
1093 LParallelMove* move = gap->GetParallelMove(inner_pos);
1094 if (move != NULL) DoParallelMove(move);
1095 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01001096}
1097
1098
Ben Murdoch257744e2011-11-30 15:57:28 +00001099void LCodeGen::DoInstructionGap(LInstructionGap* instr) {
1100 DoGap(instr);
1101}
1102
1103
Ben Murdochb0fe1622011-05-05 13:52:32 +01001104void LCodeGen::DoParameter(LParameter* instr) {
1105 // Nothing to do.
1106}
1107
1108
1109void LCodeGen::DoCallStub(LCallStub* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001110 DCHECK(ToRegister(instr->context()).is(cp));
1111 DCHECK(ToRegister(instr->result()).is(r0));
Steve Block9fac8402011-05-12 15:51:54 +01001112 switch (instr->hydrogen()->major_key()) {
Steve Block9fac8402011-05-12 15:51:54 +01001113 case CodeStub::RegExpExec: {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001114 RegExpExecStub stub(isolate());
Steve Block9fac8402011-05-12 15:51:54 +01001115 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
1116 break;
1117 }
1118 case CodeStub::SubString: {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001119 SubStringStub stub(isolate());
Steve Block9fac8402011-05-12 15:51:54 +01001120 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
1121 break;
1122 }
1123 case CodeStub::StringCompare: {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001124 StringCompareStub stub(isolate());
Ben Murdochb8e0da22011-05-16 14:20:40 +01001125 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
Steve Block9fac8402011-05-12 15:51:54 +01001126 break;
1127 }
1128 default:
1129 UNREACHABLE();
1130 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01001131}
1132
1133
1134void LCodeGen::DoUnknownOSRValue(LUnknownOSRValue* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001135 GenerateOsrPrologue();
1136}
1137
1138
1139void LCodeGen::DoModByPowerOf2I(LModByPowerOf2I* instr) {
1140 Register dividend = ToRegister(instr->dividend());
1141 int32_t divisor = instr->divisor();
1142 DCHECK(dividend.is(ToRegister(instr->result())));
1143
1144 // Theoretically, a variation of the branch-free code for integer division by
1145 // a power of 2 (calculating the remainder via an additional multiplication
1146 // (which gets simplified to an 'and') and subtraction) should be faster, and
1147 // this is exactly what GCC and clang emit. Nevertheless, benchmarks seem to
1148 // indicate that positive dividends are heavily favored, so the branching
1149 // version performs better.
1150 HMod* hmod = instr->hydrogen();
1151 int32_t mask = divisor < 0 ? -(divisor + 1) : (divisor - 1);
1152 Label dividend_is_not_negative, done;
1153 if (hmod->CheckFlag(HValue::kLeftCanBeNegative)) {
1154 __ cmp(dividend, Operand::Zero());
1155 __ b(pl, &dividend_is_not_negative);
1156 // Note that this is correct even for kMinInt operands.
1157 __ rsb(dividend, dividend, Operand::Zero());
1158 __ and_(dividend, dividend, Operand(mask));
1159 __ rsb(dividend, dividend, Operand::Zero(), SetCC);
1160 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1161 DeoptimizeIf(eq, instr);
1162 }
1163 __ b(&done);
1164 }
1165
1166 __ bind(&dividend_is_not_negative);
1167 __ and_(dividend, dividend, Operand(mask));
1168 __ bind(&done);
1169}
1170
1171
1172void LCodeGen::DoModByConstI(LModByConstI* instr) {
1173 Register dividend = ToRegister(instr->dividend());
1174 int32_t divisor = instr->divisor();
1175 Register result = ToRegister(instr->result());
1176 DCHECK(!dividend.is(result));
1177
1178 if (divisor == 0) {
1179 DeoptimizeIf(al, instr);
1180 return;
1181 }
1182
1183 __ TruncatingDiv(result, dividend, Abs(divisor));
1184 __ mov(ip, Operand(Abs(divisor)));
1185 __ smull(result, ip, result, ip);
1186 __ sub(result, dividend, result, SetCC);
1187
1188 // Check for negative zero.
1189 HMod* hmod = instr->hydrogen();
1190 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1191 Label remainder_not_zero;
1192 __ b(ne, &remainder_not_zero);
1193 __ cmp(dividend, Operand::Zero());
1194 DeoptimizeIf(lt, instr);
1195 __ bind(&remainder_not_zero);
1196 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01001197}
1198
1199
1200void LCodeGen::DoModI(LModI* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001201 HMod* hmod = instr->hydrogen();
1202 if (CpuFeatures::IsSupported(SUDIV)) {
1203 CpuFeatureScope scope(masm(), SUDIV);
Steve Block44f0eee2011-05-26 01:26:41 +01001204
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001205 Register left_reg = ToRegister(instr->left());
1206 Register right_reg = ToRegister(instr->right());
1207 Register result_reg = ToRegister(instr->result());
Steve Block44f0eee2011-05-26 01:26:41 +01001208
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001209 Label done;
1210 // Check for x % 0, sdiv might signal an exception. We have to deopt in this
1211 // case because we can't return a NaN.
1212 if (hmod->CheckFlag(HValue::kCanBeDivByZero)) {
1213 __ cmp(right_reg, Operand::Zero());
1214 DeoptimizeIf(eq, instr);
Ben Murdochb8e0da22011-05-16 14:20:40 +01001215 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001216
1217 // Check for kMinInt % -1, sdiv will return kMinInt, which is not what we
1218 // want. We have to deopt if we care about -0, because we can't return that.
1219 if (hmod->CheckFlag(HValue::kCanOverflow)) {
1220 Label no_overflow_possible;
1221 __ cmp(left_reg, Operand(kMinInt));
1222 __ b(ne, &no_overflow_possible);
1223 __ cmp(right_reg, Operand(-1));
1224 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1225 DeoptimizeIf(eq, instr);
1226 } else {
1227 __ b(ne, &no_overflow_possible);
1228 __ mov(result_reg, Operand::Zero());
1229 __ jmp(&done);
1230 }
1231 __ bind(&no_overflow_possible);
1232 }
1233
1234 // For 'r3 = r1 % r2' we can have the following ARM code:
1235 // sdiv r3, r1, r2
1236 // mls r3, r3, r2, r1
1237
1238 __ sdiv(result_reg, left_reg, right_reg);
1239 __ Mls(result_reg, result_reg, right_reg, left_reg);
1240
1241 // If we care about -0, test if the dividend is <0 and the result is 0.
1242 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1243 __ cmp(result_reg, Operand::Zero());
1244 __ b(ne, &done);
1245 __ cmp(left_reg, Operand::Zero());
1246 DeoptimizeIf(lt, instr);
1247 }
Steve Block44f0eee2011-05-26 01:26:41 +01001248 __ bind(&done);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001249
1250 } else {
1251 // General case, without any SDIV support.
1252 Register left_reg = ToRegister(instr->left());
1253 Register right_reg = ToRegister(instr->right());
1254 Register result_reg = ToRegister(instr->result());
1255 Register scratch = scratch0();
1256 DCHECK(!scratch.is(left_reg));
1257 DCHECK(!scratch.is(right_reg));
1258 DCHECK(!scratch.is(result_reg));
1259 DwVfpRegister dividend = ToDoubleRegister(instr->temp());
1260 DwVfpRegister divisor = ToDoubleRegister(instr->temp2());
1261 DCHECK(!divisor.is(dividend));
1262 LowDwVfpRegister quotient = double_scratch0();
1263 DCHECK(!quotient.is(dividend));
1264 DCHECK(!quotient.is(divisor));
1265
1266 Label done;
1267 // Check for x % 0, we have to deopt in this case because we can't return a
1268 // NaN.
1269 if (hmod->CheckFlag(HValue::kCanBeDivByZero)) {
1270 __ cmp(right_reg, Operand::Zero());
1271 DeoptimizeIf(eq, instr);
1272 }
1273
1274 __ Move(result_reg, left_reg);
1275 // Load the arguments in VFP registers. The divisor value is preloaded
1276 // before. Be careful that 'right_reg' is only live on entry.
1277 // TODO(svenpanne) The last comments seems to be wrong nowadays.
1278 __ vmov(double_scratch0().low(), left_reg);
1279 __ vcvt_f64_s32(dividend, double_scratch0().low());
1280 __ vmov(double_scratch0().low(), right_reg);
1281 __ vcvt_f64_s32(divisor, double_scratch0().low());
1282
1283 // We do not care about the sign of the divisor. Note that we still handle
1284 // the kMinInt % -1 case correctly, though.
1285 __ vabs(divisor, divisor);
1286 // Compute the quotient and round it to a 32bit integer.
1287 __ vdiv(quotient, dividend, divisor);
1288 __ vcvt_s32_f64(quotient.low(), quotient);
1289 __ vcvt_f64_s32(quotient, quotient.low());
1290
1291 // Compute the remainder in result.
1292 __ vmul(double_scratch0(), divisor, quotient);
1293 __ vcvt_s32_f64(double_scratch0().low(), double_scratch0());
1294 __ vmov(scratch, double_scratch0().low());
1295 __ sub(result_reg, left_reg, scratch, SetCC);
1296
1297 // If we care about -0, test if the dividend is <0 and the result is 0.
1298 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1299 __ b(ne, &done);
1300 __ cmp(left_reg, Operand::Zero());
1301 DeoptimizeIf(mi, instr);
1302 }
1303 __ bind(&done);
1304 }
1305}
1306
1307
1308void LCodeGen::DoDivByPowerOf2I(LDivByPowerOf2I* instr) {
1309 Register dividend = ToRegister(instr->dividend());
1310 int32_t divisor = instr->divisor();
1311 Register result = ToRegister(instr->result());
1312 DCHECK(divisor == kMinInt || base::bits::IsPowerOfTwo32(Abs(divisor)));
1313 DCHECK(!result.is(dividend));
1314
1315 // Check for (0 / -x) that will produce negative zero.
1316 HDiv* hdiv = instr->hydrogen();
1317 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) {
1318 __ cmp(dividend, Operand::Zero());
1319 DeoptimizeIf(eq, instr);
1320 }
1321 // Check for (kMinInt / -1).
1322 if (hdiv->CheckFlag(HValue::kCanOverflow) && divisor == -1) {
1323 __ cmp(dividend, Operand(kMinInt));
1324 DeoptimizeIf(eq, instr);
1325 }
1326 // Deoptimize if remainder will not be 0.
1327 if (!hdiv->CheckFlag(HInstruction::kAllUsesTruncatingToInt32) &&
1328 divisor != 1 && divisor != -1) {
1329 int32_t mask = divisor < 0 ? -(divisor + 1) : (divisor - 1);
1330 __ tst(dividend, Operand(mask));
1331 DeoptimizeIf(ne, instr);
1332 }
1333
1334 if (divisor == -1) { // Nice shortcut, not needed for correctness.
1335 __ rsb(result, dividend, Operand(0));
1336 return;
1337 }
1338 int32_t shift = WhichPowerOf2Abs(divisor);
1339 if (shift == 0) {
1340 __ mov(result, dividend);
1341 } else if (shift == 1) {
1342 __ add(result, dividend, Operand(dividend, LSR, 31));
1343 } else {
1344 __ mov(result, Operand(dividend, ASR, 31));
1345 __ add(result, dividend, Operand(result, LSR, 32 - shift));
1346 }
1347 if (shift > 0) __ mov(result, Operand(result, ASR, shift));
1348 if (divisor < 0) __ rsb(result, result, Operand(0));
1349}
1350
1351
1352void LCodeGen::DoDivByConstI(LDivByConstI* instr) {
1353 Register dividend = ToRegister(instr->dividend());
1354 int32_t divisor = instr->divisor();
1355 Register result = ToRegister(instr->result());
1356 DCHECK(!dividend.is(result));
1357
1358 if (divisor == 0) {
1359 DeoptimizeIf(al, instr);
Steve Block44f0eee2011-05-26 01:26:41 +01001360 return;
1361 }
1362
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001363 // Check for (0 / -x) that will produce negative zero.
1364 HDiv* hdiv = instr->hydrogen();
1365 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) {
1366 __ cmp(dividend, Operand::Zero());
1367 DeoptimizeIf(eq, instr);
Ben Murdochb8e0da22011-05-16 14:20:40 +01001368 }
1369
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001370 __ TruncatingDiv(result, dividend, Abs(divisor));
1371 if (divisor < 0) __ rsb(result, result, Operand::Zero());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001372
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001373 if (!hdiv->CheckFlag(HInstruction::kAllUsesTruncatingToInt32)) {
1374 __ mov(ip, Operand(divisor));
1375 __ smull(scratch0(), ip, result, ip);
1376 __ sub(scratch0(), scratch0(), dividend, SetCC);
1377 DeoptimizeIf(ne, instr);
Steve Block1e0659c2011-05-24 12:43:12 +01001378 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01001379}
1380
1381
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001382// TODO(svenpanne) Refactor this to avoid code duplication with DoFlooringDivI.
Ben Murdochb0fe1622011-05-05 13:52:32 +01001383void LCodeGen::DoDivI(LDivI* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001384 HBinaryOperation* hdiv = instr->hydrogen();
1385 Register dividend = ToRegister(instr->dividend());
1386 Register divisor = ToRegister(instr->divisor());
1387 Register result = ToRegister(instr->result());
Ben Murdochb8e0da22011-05-16 14:20:40 +01001388
1389 // Check for x / 0.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001390 if (hdiv->CheckFlag(HValue::kCanBeDivByZero)) {
1391 __ cmp(divisor, Operand::Zero());
1392 DeoptimizeIf(eq, instr);
Ben Murdochb8e0da22011-05-16 14:20:40 +01001393 }
1394
1395 // Check for (0 / -x) that will produce negative zero.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001396 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero)) {
1397 Label positive;
1398 if (!instr->hydrogen_value()->CheckFlag(HValue::kCanBeDivByZero)) {
1399 // Do the test only if it hadn't be done above.
1400 __ cmp(divisor, Operand::Zero());
1401 }
1402 __ b(pl, &positive);
1403 __ cmp(dividend, Operand::Zero());
1404 DeoptimizeIf(eq, instr);
1405 __ bind(&positive);
1406 }
1407
1408 // Check for (kMinInt / -1).
1409 if (hdiv->CheckFlag(HValue::kCanOverflow) &&
1410 (!CpuFeatures::IsSupported(SUDIV) ||
1411 !hdiv->CheckFlag(HValue::kAllUsesTruncatingToInt32))) {
1412 // We don't need to check for overflow when truncating with sdiv
1413 // support because, on ARM, sdiv kMinInt, -1 -> kMinInt.
1414 __ cmp(dividend, Operand(kMinInt));
1415 __ cmp(divisor, Operand(-1), eq);
1416 DeoptimizeIf(eq, instr);
1417 }
1418
1419 if (CpuFeatures::IsSupported(SUDIV)) {
1420 CpuFeatureScope scope(masm(), SUDIV);
1421 __ sdiv(result, dividend, divisor);
1422 } else {
1423 DoubleRegister vleft = ToDoubleRegister(instr->temp());
1424 DoubleRegister vright = double_scratch0();
1425 __ vmov(double_scratch0().low(), dividend);
1426 __ vcvt_f64_s32(vleft, double_scratch0().low());
1427 __ vmov(double_scratch0().low(), divisor);
1428 __ vcvt_f64_s32(vright, double_scratch0().low());
1429 __ vdiv(vleft, vleft, vright); // vleft now contains the result.
1430 __ vcvt_s32_f64(double_scratch0().low(), vleft);
1431 __ vmov(result, double_scratch0().low());
1432 }
1433
1434 if (!hdiv->CheckFlag(HValue::kAllUsesTruncatingToInt32)) {
1435 // Compute remainder and deopt if it's not zero.
1436 Register remainder = scratch0();
1437 __ Mls(remainder, result, divisor, dividend);
1438 __ cmp(remainder, Operand::Zero());
1439 DeoptimizeIf(ne, instr);
1440 }
1441}
1442
1443
1444void LCodeGen::DoMultiplyAddD(LMultiplyAddD* instr) {
1445 DwVfpRegister addend = ToDoubleRegister(instr->addend());
1446 DwVfpRegister multiplier = ToDoubleRegister(instr->multiplier());
1447 DwVfpRegister multiplicand = ToDoubleRegister(instr->multiplicand());
1448
1449 // This is computed in-place.
1450 DCHECK(addend.is(ToDoubleRegister(instr->result())));
1451
1452 __ vmla(addend, multiplier, multiplicand);
1453}
1454
1455
1456void LCodeGen::DoMultiplySubD(LMultiplySubD* instr) {
1457 DwVfpRegister minuend = ToDoubleRegister(instr->minuend());
1458 DwVfpRegister multiplier = ToDoubleRegister(instr->multiplier());
1459 DwVfpRegister multiplicand = ToDoubleRegister(instr->multiplicand());
1460
1461 // This is computed in-place.
1462 DCHECK(minuend.is(ToDoubleRegister(instr->result())));
1463
1464 __ vmls(minuend, multiplier, multiplicand);
1465}
1466
1467
1468void LCodeGen::DoFlooringDivByPowerOf2I(LFlooringDivByPowerOf2I* instr) {
1469 Register dividend = ToRegister(instr->dividend());
1470 Register result = ToRegister(instr->result());
1471 int32_t divisor = instr->divisor();
1472
1473 // If the divisor is 1, return the dividend.
1474 if (divisor == 1) {
1475 __ Move(result, dividend);
1476 return;
1477 }
1478
1479 // If the divisor is positive, things are easy: There can be no deopts and we
1480 // can simply do an arithmetic right shift.
1481 int32_t shift = WhichPowerOf2Abs(divisor);
1482 if (divisor > 1) {
1483 __ mov(result, Operand(dividend, ASR, shift));
1484 return;
1485 }
1486
1487 // If the divisor is negative, we have to negate and handle edge cases.
1488 __ rsb(result, dividend, Operand::Zero(), SetCC);
Ben Murdochb8e0da22011-05-16 14:20:40 +01001489 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001490 DeoptimizeIf(eq, instr);
Ben Murdochb8e0da22011-05-16 14:20:40 +01001491 }
1492
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001493 // Dividing by -1 is basically negation, unless we overflow.
1494 if (divisor == -1) {
1495 if (instr->hydrogen()->CheckFlag(HValue::kLeftCanBeMinInt)) {
1496 DeoptimizeIf(vs, instr);
1497 }
1498 return;
Ben Murdochb8e0da22011-05-16 14:20:40 +01001499 }
1500
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001501 // If the negation could not overflow, simply shifting is OK.
1502 if (!instr->hydrogen()->CheckFlag(HValue::kLeftCanBeMinInt)) {
1503 __ mov(result, Operand(result, ASR, shift));
1504 return;
1505 }
Ben Murdochb8e0da22011-05-16 14:20:40 +01001506
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001507 __ mov(result, Operand(kMinInt / divisor), LeaveCC, vs);
1508 __ mov(result, Operand(result, ASR, shift), LeaveCC, vc);
1509}
Ben Murdochb8e0da22011-05-16 14:20:40 +01001510
Ben Murdochb8e0da22011-05-16 14:20:40 +01001511
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001512void LCodeGen::DoFlooringDivByConstI(LFlooringDivByConstI* instr) {
1513 Register dividend = ToRegister(instr->dividend());
1514 int32_t divisor = instr->divisor();
1515 Register result = ToRegister(instr->result());
1516 DCHECK(!dividend.is(result));
Ben Murdochb8e0da22011-05-16 14:20:40 +01001517
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001518 if (divisor == 0) {
1519 DeoptimizeIf(al, instr);
1520 return;
1521 }
Ben Murdochb8e0da22011-05-16 14:20:40 +01001522
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001523 // Check for (0 / -x) that will produce negative zero.
1524 HMathFloorOfDiv* hdiv = instr->hydrogen();
1525 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) {
1526 __ cmp(dividend, Operand::Zero());
1527 DeoptimizeIf(eq, instr);
1528 }
Ben Murdochb8e0da22011-05-16 14:20:40 +01001529
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001530 // Easy case: We need no dynamic check for the dividend and the flooring
1531 // division is the same as the truncating division.
1532 if ((divisor > 0 && !hdiv->CheckFlag(HValue::kLeftCanBeNegative)) ||
1533 (divisor < 0 && !hdiv->CheckFlag(HValue::kLeftCanBePositive))) {
1534 __ TruncatingDiv(result, dividend, Abs(divisor));
1535 if (divisor < 0) __ rsb(result, result, Operand::Zero());
1536 return;
1537 }
Ben Murdochb8e0da22011-05-16 14:20:40 +01001538
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001539 // In the general case we may need to adjust before and after the truncating
1540 // division to get a flooring division.
1541 Register temp = ToRegister(instr->temp());
1542 DCHECK(!temp.is(dividend) && !temp.is(result));
1543 Label needs_adjustment, done;
1544 __ cmp(dividend, Operand::Zero());
1545 __ b(divisor > 0 ? lt : gt, &needs_adjustment);
1546 __ TruncatingDiv(result, dividend, Abs(divisor));
1547 if (divisor < 0) __ rsb(result, result, Operand::Zero());
1548 __ jmp(&done);
1549 __ bind(&needs_adjustment);
1550 __ add(temp, dividend, Operand(divisor > 0 ? 1 : -1));
1551 __ TruncatingDiv(result, temp, Abs(divisor));
1552 if (divisor < 0) __ rsb(result, result, Operand::Zero());
1553 __ sub(result, result, Operand(1));
Ben Murdochb8e0da22011-05-16 14:20:40 +01001554 __ bind(&done);
1555}
1556
1557
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001558// TODO(svenpanne) Refactor this to avoid code duplication with DoDivI.
1559void LCodeGen::DoFlooringDivI(LFlooringDivI* instr) {
1560 HBinaryOperation* hdiv = instr->hydrogen();
1561 Register left = ToRegister(instr->dividend());
1562 Register right = ToRegister(instr->divisor());
1563 Register result = ToRegister(instr->result());
Ben Murdochb8e0da22011-05-16 14:20:40 +01001564
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001565 // Check for x / 0.
1566 if (hdiv->CheckFlag(HValue::kCanBeDivByZero)) {
1567 __ cmp(right, Operand::Zero());
1568 DeoptimizeIf(eq, instr);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001569 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001570
1571 // Check for (0 / -x) that will produce negative zero.
1572 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero)) {
1573 Label positive;
1574 if (!instr->hydrogen_value()->CheckFlag(HValue::kCanBeDivByZero)) {
1575 // Do the test only if it hadn't be done above.
1576 __ cmp(right, Operand::Zero());
1577 }
1578 __ b(pl, &positive);
1579 __ cmp(left, Operand::Zero());
1580 DeoptimizeIf(eq, instr);
1581 __ bind(&positive);
1582 }
1583
1584 // Check for (kMinInt / -1).
1585 if (hdiv->CheckFlag(HValue::kCanOverflow) &&
1586 (!CpuFeatures::IsSupported(SUDIV) ||
1587 !hdiv->CheckFlag(HValue::kAllUsesTruncatingToInt32))) {
1588 // We don't need to check for overflow when truncating with sdiv
1589 // support because, on ARM, sdiv kMinInt, -1 -> kMinInt.
1590 __ cmp(left, Operand(kMinInt));
1591 __ cmp(right, Operand(-1), eq);
1592 DeoptimizeIf(eq, instr);
1593 }
1594
1595 if (CpuFeatures::IsSupported(SUDIV)) {
1596 CpuFeatureScope scope(masm(), SUDIV);
1597 __ sdiv(result, left, right);
1598 } else {
1599 DoubleRegister vleft = ToDoubleRegister(instr->temp());
1600 DoubleRegister vright = double_scratch0();
1601 __ vmov(double_scratch0().low(), left);
1602 __ vcvt_f64_s32(vleft, double_scratch0().low());
1603 __ vmov(double_scratch0().low(), right);
1604 __ vcvt_f64_s32(vright, double_scratch0().low());
1605 __ vdiv(vleft, vleft, vright); // vleft now contains the result.
1606 __ vcvt_s32_f64(double_scratch0().low(), vleft);
1607 __ vmov(result, double_scratch0().low());
1608 }
1609
1610 Label done;
1611 Register remainder = scratch0();
1612 __ Mls(remainder, result, right, left);
1613 __ cmp(remainder, Operand::Zero());
1614 __ b(eq, &done);
1615 __ eor(remainder, remainder, Operand(right));
1616 __ add(result, result, Operand(remainder, ASR, 31));
1617 __ bind(&done);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001618}
1619
1620
1621void LCodeGen::DoMulI(LMulI* instr) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001622 Register result = ToRegister(instr->result());
1623 // Note that result may alias left.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001624 Register left = ToRegister(instr->left());
1625 LOperand* right_op = instr->right();
Ben Murdochb0fe1622011-05-05 13:52:32 +01001626
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001627 bool bailout_on_minus_zero =
1628 instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001629 bool overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001630
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001631 if (right_op->IsConstantOperand()) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001632 int32_t constant = ToInteger32(LConstantOperand::cast(right_op));
1633
1634 if (bailout_on_minus_zero && (constant < 0)) {
1635 // The case of a null constant will be handled separately.
1636 // If constant is negative and left is null, the result should be -0.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001637 __ cmp(left, Operand::Zero());
1638 DeoptimizeIf(eq, instr);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001639 }
1640
1641 switch (constant) {
1642 case -1:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001643 if (overflow) {
1644 __ rsb(result, left, Operand::Zero(), SetCC);
1645 DeoptimizeIf(vs, instr);
1646 } else {
1647 __ rsb(result, left, Operand::Zero());
1648 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001649 break;
1650 case 0:
1651 if (bailout_on_minus_zero) {
1652 // If left is strictly negative and the constant is null, the
1653 // result is -0. Deoptimize if required, otherwise return 0.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001654 __ cmp(left, Operand::Zero());
1655 DeoptimizeIf(mi, instr);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001656 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001657 __ mov(result, Operand::Zero());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001658 break;
1659 case 1:
1660 __ Move(result, left);
1661 break;
1662 default:
1663 // Multiplying by powers of two and powers of two plus or minus
1664 // one can be done faster with shifted operands.
1665 // For other constants we emit standard code.
1666 int32_t mask = constant >> 31;
1667 uint32_t constant_abs = (constant + mask) ^ mask;
1668
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001669 if (base::bits::IsPowerOfTwo32(constant_abs)) {
1670 int32_t shift = WhichPowerOf2(constant_abs);
1671 __ mov(result, Operand(left, LSL, shift));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001672 // Correct the sign of the result is the constant is negative.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001673 if (constant < 0) __ rsb(result, result, Operand::Zero());
1674 } else if (base::bits::IsPowerOfTwo32(constant_abs - 1)) {
1675 int32_t shift = WhichPowerOf2(constant_abs - 1);
1676 __ add(result, left, Operand(left, LSL, shift));
1677 // Correct the sign of the result is the constant is negative.
1678 if (constant < 0) __ rsb(result, result, Operand::Zero());
1679 } else if (base::bits::IsPowerOfTwo32(constant_abs + 1)) {
1680 int32_t shift = WhichPowerOf2(constant_abs + 1);
1681 __ rsb(result, left, Operand(left, LSL, shift));
1682 // Correct the sign of the result is the constant is negative.
1683 if (constant < 0) __ rsb(result, result, Operand::Zero());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001684 } else {
1685 // Generate standard code.
1686 __ mov(ip, Operand(constant));
1687 __ mul(result, left, ip);
1688 }
1689 }
1690
Ben Murdochb0fe1622011-05-05 13:52:32 +01001691 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001692 DCHECK(right_op->IsRegister());
1693 Register right = ToRegister(right_op);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001694
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001695 if (overflow) {
1696 Register scratch = scratch0();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001697 // scratch:result = left * right.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001698 if (instr->hydrogen()->representation().IsSmi()) {
1699 __ SmiUntag(result, left);
1700 __ smull(result, scratch, result, right);
1701 } else {
1702 __ smull(result, scratch, left, right);
1703 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001704 __ cmp(scratch, Operand(result, ASR, 31));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001705 DeoptimizeIf(ne, instr);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001706 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001707 if (instr->hydrogen()->representation().IsSmi()) {
1708 __ SmiUntag(result, left);
1709 __ mul(result, result, right);
1710 } else {
1711 __ mul(result, left, right);
1712 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001713 }
1714
1715 if (bailout_on_minus_zero) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001716 Label done;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001717 __ teq(left, Operand(right));
1718 __ b(pl, &done);
1719 // Bail out if the result is minus zero.
1720 __ cmp(result, Operand::Zero());
1721 DeoptimizeIf(eq, instr);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001722 __ bind(&done);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001723 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01001724 }
1725}
1726
1727
1728void LCodeGen::DoBitI(LBitI* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001729 LOperand* left_op = instr->left();
1730 LOperand* right_op = instr->right();
1731 DCHECK(left_op->IsRegister());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001732 Register left = ToRegister(left_op);
1733 Register result = ToRegister(instr->result());
1734 Operand right(no_reg);
Steve Block44f0eee2011-05-26 01:26:41 +01001735
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001736 if (right_op->IsStackSlot()) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001737 right = Operand(EmitLoadRegister(right_op, ip));
Steve Block44f0eee2011-05-26 01:26:41 +01001738 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001739 DCHECK(right_op->IsRegister() || right_op->IsConstantOperand());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001740 right = ToOperand(right_op);
Steve Block44f0eee2011-05-26 01:26:41 +01001741 }
1742
Ben Murdochb0fe1622011-05-05 13:52:32 +01001743 switch (instr->op()) {
1744 case Token::BIT_AND:
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001745 __ and_(result, left, right);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001746 break;
1747 case Token::BIT_OR:
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001748 __ orr(result, left, right);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001749 break;
1750 case Token::BIT_XOR:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001751 if (right_op->IsConstantOperand() && right.immediate() == int32_t(~0)) {
1752 __ mvn(result, Operand(left));
1753 } else {
1754 __ eor(result, left, right);
1755 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01001756 break;
1757 default:
1758 UNREACHABLE();
1759 break;
1760 }
1761}
1762
1763
1764void LCodeGen::DoShiftI(LShiftI* instr) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001765 // Both 'left' and 'right' are "used at start" (see LCodeGen::DoShift), so
1766 // result may alias either of them.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001767 LOperand* right_op = instr->right();
1768 Register left = ToRegister(instr->left());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001769 Register result = ToRegister(instr->result());
Steve Block9fac8402011-05-12 15:51:54 +01001770 Register scratch = scratch0();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001771 if (right_op->IsRegister()) {
1772 // Mask the right_op operand.
1773 __ and_(scratch, ToRegister(right_op), Operand(0x1F));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001774 switch (instr->op()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001775 case Token::ROR:
1776 __ mov(result, Operand(left, ROR, scratch));
1777 break;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001778 case Token::SAR:
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001779 __ mov(result, Operand(left, ASR, scratch));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001780 break;
1781 case Token::SHR:
1782 if (instr->can_deopt()) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001783 __ mov(result, Operand(left, LSR, scratch), SetCC);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001784 DeoptimizeIf(mi, instr);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001785 } else {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001786 __ mov(result, Operand(left, LSR, scratch));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001787 }
1788 break;
1789 case Token::SHL:
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001790 __ mov(result, Operand(left, LSL, scratch));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001791 break;
1792 default:
1793 UNREACHABLE();
1794 break;
1795 }
1796 } else {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001797 // Mask the right_op operand.
1798 int value = ToInteger32(LConstantOperand::cast(right_op));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001799 uint8_t shift_count = static_cast<uint8_t>(value & 0x1F);
1800 switch (instr->op()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001801 case Token::ROR:
1802 if (shift_count != 0) {
1803 __ mov(result, Operand(left, ROR, shift_count));
1804 } else {
1805 __ Move(result, left);
1806 }
1807 break;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001808 case Token::SAR:
1809 if (shift_count != 0) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001810 __ mov(result, Operand(left, ASR, shift_count));
1811 } else {
1812 __ Move(result, left);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001813 }
1814 break;
1815 case Token::SHR:
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001816 if (shift_count != 0) {
1817 __ mov(result, Operand(left, LSR, shift_count));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001818 } else {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001819 if (instr->can_deopt()) {
1820 __ tst(left, Operand(0x80000000));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001821 DeoptimizeIf(ne, instr);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001822 }
1823 __ Move(result, left);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001824 }
1825 break;
1826 case Token::SHL:
1827 if (shift_count != 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001828 if (instr->hydrogen_value()->representation().IsSmi() &&
1829 instr->can_deopt()) {
1830 if (shift_count != 1) {
1831 __ mov(result, Operand(left, LSL, shift_count - 1));
1832 __ SmiTag(result, result, SetCC);
1833 } else {
1834 __ SmiTag(result, left, SetCC);
1835 }
1836 DeoptimizeIf(vs, instr);
1837 } else {
1838 __ mov(result, Operand(left, LSL, shift_count));
1839 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001840 } else {
1841 __ Move(result, left);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001842 }
1843 break;
1844 default:
1845 UNREACHABLE();
1846 break;
1847 }
1848 }
1849}
1850
1851
1852void LCodeGen::DoSubI(LSubI* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001853 LOperand* left = instr->left();
1854 LOperand* right = instr->right();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001855 LOperand* result = instr->result();
Steve Block44f0eee2011-05-26 01:26:41 +01001856 bool can_overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
1857 SBit set_cond = can_overflow ? SetCC : LeaveCC;
1858
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001859 if (right->IsStackSlot()) {
Steve Block44f0eee2011-05-26 01:26:41 +01001860 Register right_reg = EmitLoadRegister(right, ip);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001861 __ sub(ToRegister(result), ToRegister(left), Operand(right_reg), set_cond);
Steve Block44f0eee2011-05-26 01:26:41 +01001862 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001863 DCHECK(right->IsRegister() || right->IsConstantOperand());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001864 __ sub(ToRegister(result), ToRegister(left), ToOperand(right), set_cond);
Steve Block44f0eee2011-05-26 01:26:41 +01001865 }
1866
1867 if (can_overflow) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001868 DeoptimizeIf(vs, instr);
1869 }
1870}
1871
1872
1873void LCodeGen::DoRSubI(LRSubI* instr) {
1874 LOperand* left = instr->left();
1875 LOperand* right = instr->right();
1876 LOperand* result = instr->result();
1877 bool can_overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
1878 SBit set_cond = can_overflow ? SetCC : LeaveCC;
1879
1880 if (right->IsStackSlot()) {
1881 Register right_reg = EmitLoadRegister(right, ip);
1882 __ rsb(ToRegister(result), ToRegister(left), Operand(right_reg), set_cond);
1883 } else {
1884 DCHECK(right->IsRegister() || right->IsConstantOperand());
1885 __ rsb(ToRegister(result), ToRegister(left), ToOperand(right), set_cond);
1886 }
1887
1888 if (can_overflow) {
1889 DeoptimizeIf(vs, instr);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001890 }
1891}
1892
1893
1894void LCodeGen::DoConstantI(LConstantI* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001895 __ mov(ToRegister(instr->result()), Operand(instr->value()));
1896}
1897
1898
1899void LCodeGen::DoConstantS(LConstantS* instr) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001900 __ mov(ToRegister(instr->result()), Operand(instr->value()));
1901}
1902
1903
1904void LCodeGen::DoConstantD(LConstantD* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001905 DCHECK(instr->result()->IsDoubleRegister());
Ben Murdochb8e0da22011-05-16 14:20:40 +01001906 DwVfpRegister result = ToDoubleRegister(instr->result());
1907 double v = instr->value();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001908 __ Vmov(result, v, scratch0());
1909}
1910
1911
1912void LCodeGen::DoConstantE(LConstantE* instr) {
1913 __ mov(ToRegister(instr->result()), Operand(instr->value()));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001914}
1915
1916
1917void LCodeGen::DoConstantT(LConstantT* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001918 Handle<Object> object = instr->value(isolate());
1919 AllowDeferredHandleDereference smi_check;
1920 __ Move(ToRegister(instr->result()), object);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001921}
1922
1923
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001924void LCodeGen::DoMapEnumLength(LMapEnumLength* instr) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001925 Register result = ToRegister(instr->result());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001926 Register map = ToRegister(instr->value());
1927 __ EnumLength(result, map);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001928}
1929
1930
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001931void LCodeGen::DoDateField(LDateField* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001932 Register object = ToRegister(instr->date());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001933 Register result = ToRegister(instr->result());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001934 Register scratch = ToRegister(instr->temp());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001935 Smi* index = instr->index();
1936 Label runtime, done;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001937 DCHECK(object.is(result));
1938 DCHECK(object.is(r0));
1939 DCHECK(!scratch.is(scratch0()));
1940 DCHECK(!scratch.is(object));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001941
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001942 __ SmiTst(object);
1943 DeoptimizeIf(eq, instr);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001944 __ CompareObjectType(object, scratch, scratch, JS_DATE_TYPE);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001945 DeoptimizeIf(ne, instr);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001946
1947 if (index->value() == 0) {
1948 __ ldr(result, FieldMemOperand(object, JSDate::kValueOffset));
1949 } else {
1950 if (index->value() < JSDate::kFirstUncachedField) {
1951 ExternalReference stamp = ExternalReference::date_cache_stamp(isolate());
1952 __ mov(scratch, Operand(stamp));
1953 __ ldr(scratch, MemOperand(scratch));
1954 __ ldr(scratch0(), FieldMemOperand(object, JSDate::kCacheStampOffset));
1955 __ cmp(scratch, scratch0());
1956 __ b(ne, &runtime);
1957 __ ldr(result, FieldMemOperand(object, JSDate::kValueOffset +
1958 kPointerSize * index->value()));
1959 __ jmp(&done);
1960 }
1961 __ bind(&runtime);
1962 __ PrepareCallCFunction(2, scratch);
1963 __ mov(r1, Operand(index));
1964 __ CallCFunction(ExternalReference::get_date_field_function(isolate()), 2);
1965 __ bind(&done);
1966 }
1967}
1968
1969
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001970MemOperand LCodeGen::BuildSeqStringOperand(Register string,
1971 LOperand* index,
1972 String::Encoding encoding) {
1973 if (index->IsConstantOperand()) {
1974 int offset = ToInteger32(LConstantOperand::cast(index));
1975 if (encoding == String::TWO_BYTE_ENCODING) {
1976 offset *= kUC16Size;
1977 }
1978 STATIC_ASSERT(kCharSize == 1);
1979 return FieldMemOperand(string, SeqString::kHeaderSize + offset);
1980 }
1981 Register scratch = scratch0();
1982 DCHECK(!scratch.is(string));
1983 DCHECK(!scratch.is(ToRegister(index)));
1984 if (encoding == String::ONE_BYTE_ENCODING) {
1985 __ add(scratch, string, Operand(ToRegister(index)));
1986 } else {
1987 STATIC_ASSERT(kUC16Size == 2);
1988 __ add(scratch, string, Operand(ToRegister(index), LSL, 1));
1989 }
1990 return FieldMemOperand(scratch, SeqString::kHeaderSize);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001991}
1992
1993
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001994void LCodeGen::DoSeqStringGetChar(LSeqStringGetChar* instr) {
1995 String::Encoding encoding = instr->hydrogen()->encoding();
1996 Register string = ToRegister(instr->string());
1997 Register result = ToRegister(instr->result());
Ben Murdochb0fe1622011-05-05 13:52:32 +01001998
1999 if (FLAG_debug_code) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002000 Register scratch = scratch0();
2001 __ ldr(scratch, FieldMemOperand(string, HeapObject::kMapOffset));
2002 __ ldrb(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
2003
2004 __ and_(scratch, scratch,
2005 Operand(kStringRepresentationMask | kStringEncodingMask));
2006 static const uint32_t one_byte_seq_type = kSeqStringTag | kOneByteStringTag;
2007 static const uint32_t two_byte_seq_type = kSeqStringTag | kTwoByteStringTag;
2008 __ cmp(scratch, Operand(encoding == String::ONE_BYTE_ENCODING
2009 ? one_byte_seq_type : two_byte_seq_type));
2010 __ Check(eq, kUnexpectedStringType);
2011 }
2012
2013 MemOperand operand = BuildSeqStringOperand(string, instr->index(), encoding);
2014 if (encoding == String::ONE_BYTE_ENCODING) {
2015 __ ldrb(result, operand);
2016 } else {
2017 __ ldrh(result, operand);
2018 }
2019}
2020
2021
2022void LCodeGen::DoSeqStringSetChar(LSeqStringSetChar* instr) {
2023 String::Encoding encoding = instr->hydrogen()->encoding();
2024 Register string = ToRegister(instr->string());
2025 Register value = ToRegister(instr->value());
2026
2027 if (FLAG_debug_code) {
2028 Register index = ToRegister(instr->index());
2029 static const uint32_t one_byte_seq_type = kSeqStringTag | kOneByteStringTag;
2030 static const uint32_t two_byte_seq_type = kSeqStringTag | kTwoByteStringTag;
2031 int encoding_mask =
2032 instr->hydrogen()->encoding() == String::ONE_BYTE_ENCODING
2033 ? one_byte_seq_type : two_byte_seq_type;
2034 __ EmitSeqStringSetCharCheck(string, index, value, encoding_mask);
2035 }
2036
2037 MemOperand operand = BuildSeqStringOperand(string, instr->index(), encoding);
2038 if (encoding == String::ONE_BYTE_ENCODING) {
2039 __ strb(value, operand);
2040 } else {
2041 __ strh(value, operand);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002042 }
2043}
2044
2045
2046void LCodeGen::DoAddI(LAddI* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002047 LOperand* left = instr->left();
2048 LOperand* right = instr->right();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002049 LOperand* result = instr->result();
Steve Block44f0eee2011-05-26 01:26:41 +01002050 bool can_overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
2051 SBit set_cond = can_overflow ? SetCC : LeaveCC;
Ben Murdochb0fe1622011-05-05 13:52:32 +01002052
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002053 if (right->IsStackSlot()) {
Steve Block44f0eee2011-05-26 01:26:41 +01002054 Register right_reg = EmitLoadRegister(right, ip);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002055 __ add(ToRegister(result), ToRegister(left), Operand(right_reg), set_cond);
Steve Block44f0eee2011-05-26 01:26:41 +01002056 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002057 DCHECK(right->IsRegister() || right->IsConstantOperand());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002058 __ add(ToRegister(result), ToRegister(left), ToOperand(right), set_cond);
Steve Block44f0eee2011-05-26 01:26:41 +01002059 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01002060
Steve Block44f0eee2011-05-26 01:26:41 +01002061 if (can_overflow) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002062 DeoptimizeIf(vs, instr);
2063 }
2064}
2065
2066
2067void LCodeGen::DoMathMinMax(LMathMinMax* instr) {
2068 LOperand* left = instr->left();
2069 LOperand* right = instr->right();
2070 HMathMinMax::Operation operation = instr->hydrogen()->operation();
2071 if (instr->hydrogen()->representation().IsSmiOrInteger32()) {
2072 Condition condition = (operation == HMathMinMax::kMathMin) ? le : ge;
2073 Register left_reg = ToRegister(left);
2074 Operand right_op = (right->IsRegister() || right->IsConstantOperand())
2075 ? ToOperand(right)
2076 : Operand(EmitLoadRegister(right, ip));
2077 Register result_reg = ToRegister(instr->result());
2078 __ cmp(left_reg, right_op);
2079 __ Move(result_reg, left_reg, condition);
2080 __ mov(result_reg, right_op, LeaveCC, NegateCondition(condition));
2081 } else {
2082 DCHECK(instr->hydrogen()->representation().IsDouble());
2083 DwVfpRegister left_reg = ToDoubleRegister(left);
2084 DwVfpRegister right_reg = ToDoubleRegister(right);
2085 DwVfpRegister result_reg = ToDoubleRegister(instr->result());
2086 Label result_is_nan, return_left, return_right, check_zero, done;
2087 __ VFPCompareAndSetFlags(left_reg, right_reg);
2088 if (operation == HMathMinMax::kMathMin) {
2089 __ b(mi, &return_left);
2090 __ b(gt, &return_right);
2091 } else {
2092 __ b(mi, &return_right);
2093 __ b(gt, &return_left);
2094 }
2095 __ b(vs, &result_is_nan);
2096 // Left equals right => check for -0.
2097 __ VFPCompareAndSetFlags(left_reg, 0.0);
2098 if (left_reg.is(result_reg) || right_reg.is(result_reg)) {
2099 __ b(ne, &done); // left == right != 0.
2100 } else {
2101 __ b(ne, &return_left); // left == right != 0.
2102 }
2103 // At this point, both left and right are either 0 or -0.
2104 if (operation == HMathMinMax::kMathMin) {
2105 // We could use a single 'vorr' instruction here if we had NEON support.
2106 __ vneg(left_reg, left_reg);
2107 __ vsub(result_reg, left_reg, right_reg);
2108 __ vneg(result_reg, result_reg);
2109 } else {
2110 // Since we operate on +0 and/or -0, vadd and vand have the same effect;
2111 // the decision for vadd is easy because vand is a NEON instruction.
2112 __ vadd(result_reg, left_reg, right_reg);
2113 }
2114 __ b(&done);
2115
2116 __ bind(&result_is_nan);
2117 __ vadd(result_reg, left_reg, right_reg);
2118 __ b(&done);
2119
2120 __ bind(&return_right);
2121 __ Move(result_reg, right_reg);
2122 if (!left_reg.is(result_reg)) {
2123 __ b(&done);
2124 }
2125
2126 __ bind(&return_left);
2127 __ Move(result_reg, left_reg);
2128
2129 __ bind(&done);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002130 }
2131}
2132
2133
2134void LCodeGen::DoArithmeticD(LArithmeticD* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002135 DwVfpRegister left = ToDoubleRegister(instr->left());
2136 DwVfpRegister right = ToDoubleRegister(instr->right());
2137 DwVfpRegister result = ToDoubleRegister(instr->result());
Ben Murdochb0fe1622011-05-05 13:52:32 +01002138 switch (instr->op()) {
2139 case Token::ADD:
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002140 __ vadd(result, left, right);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002141 break;
2142 case Token::SUB:
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002143 __ vsub(result, left, right);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002144 break;
2145 case Token::MUL:
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002146 __ vmul(result, left, right);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002147 break;
2148 case Token::DIV:
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002149 __ vdiv(result, left, right);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002150 break;
2151 case Token::MOD: {
Ben Murdoch257744e2011-11-30 15:57:28 +00002152 __ PrepareCallCFunction(0, 2, scratch0());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002153 __ MovToFloatParameters(left, right);
Steve Block44f0eee2011-05-26 01:26:41 +01002154 __ CallCFunction(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002155 ExternalReference::mod_two_doubles_operation(isolate()),
Ben Murdoch257744e2011-11-30 15:57:28 +00002156 0, 2);
Steve Block1e0659c2011-05-24 12:43:12 +01002157 // Move the result in the double result register.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002158 __ MovFromFloatResult(result);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002159 break;
2160 }
2161 default:
2162 UNREACHABLE();
2163 break;
2164 }
2165}
2166
2167
2168void LCodeGen::DoArithmeticT(LArithmeticT* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002169 DCHECK(ToRegister(instr->context()).is(cp));
2170 DCHECK(ToRegister(instr->left()).is(r1));
2171 DCHECK(ToRegister(instr->right()).is(r0));
2172 DCHECK(ToRegister(instr->result()).is(r0));
Ben Murdochb0fe1622011-05-05 13:52:32 +01002173
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002174 Handle<Code> code =
2175 CodeFactory::BinaryOpIC(isolate(), instr->op(), NO_OVERWRITE).code();
2176 // Block literal pool emission to ensure nop indicating no inlined smi code
2177 // is in the correct position.
2178 Assembler::BlockConstPoolScope block_const_pool(masm());
2179 CallCode(code, RelocInfo::CODE_TARGET, instr);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002180}
2181
2182
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002183template<class InstrType>
2184void LCodeGen::EmitBranch(InstrType instr, Condition condition) {
2185 int left_block = instr->TrueDestination(chunk_);
2186 int right_block = instr->FalseDestination(chunk_);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002187
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002188 int next_block = GetNextEmittedBlock();
Ben Murdochb0fe1622011-05-05 13:52:32 +01002189
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002190 if (right_block == left_block || condition == al) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01002191 EmitGoto(left_block);
2192 } else if (left_block == next_block) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002193 __ b(NegateCondition(condition), chunk_->GetAssemblyLabel(right_block));
Ben Murdochb0fe1622011-05-05 13:52:32 +01002194 } else if (right_block == next_block) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002195 __ b(condition, chunk_->GetAssemblyLabel(left_block));
Ben Murdochb0fe1622011-05-05 13:52:32 +01002196 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002197 __ b(condition, chunk_->GetAssemblyLabel(left_block));
Ben Murdochb0fe1622011-05-05 13:52:32 +01002198 __ b(chunk_->GetAssemblyLabel(right_block));
2199 }
2200}
2201
2202
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002203template<class InstrType>
2204void LCodeGen::EmitFalseBranch(InstrType instr, Condition condition) {
2205 int false_block = instr->FalseDestination(chunk_);
2206 __ b(condition, chunk_->GetAssemblyLabel(false_block));
2207}
2208
2209
2210void LCodeGen::DoDebugBreak(LDebugBreak* instr) {
2211 __ stop("LBreak");
2212}
2213
2214
Ben Murdochb0fe1622011-05-05 13:52:32 +01002215void LCodeGen::DoBranch(LBranch* instr) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002216 Representation r = instr->hydrogen()->value()->representation();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002217 if (r.IsInteger32() || r.IsSmi()) {
2218 DCHECK(!info()->IsStub());
2219 Register reg = ToRegister(instr->value());
2220 __ cmp(reg, Operand::Zero());
2221 EmitBranch(instr, ne);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002222 } else if (r.IsDouble()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002223 DCHECK(!info()->IsStub());
2224 DwVfpRegister reg = ToDoubleRegister(instr->value());
Ben Murdochb8e0da22011-05-16 14:20:40 +01002225 // Test the double value. Zero and NaN are false.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002226 __ VFPCompareAndSetFlags(reg, 0.0);
2227 __ cmp(r0, r0, vs); // If NaN, set the Z flag. (NaN -> false)
2228 EmitBranch(instr, ne);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002229 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002230 DCHECK(r.IsTagged());
2231 Register reg = ToRegister(instr->value());
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002232 HType type = instr->hydrogen()->value()->type();
2233 if (type.IsBoolean()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002234 DCHECK(!info()->IsStub());
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002235 __ CompareRoot(reg, Heap::kTrueValueRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002236 EmitBranch(instr, eq);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002237 } else if (type.IsSmi()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002238 DCHECK(!info()->IsStub());
2239 __ cmp(reg, Operand::Zero());
2240 EmitBranch(instr, ne);
2241 } else if (type.IsJSArray()) {
2242 DCHECK(!info()->IsStub());
2243 EmitBranch(instr, al);
2244 } else if (type.IsHeapNumber()) {
2245 DCHECK(!info()->IsStub());
2246 DwVfpRegister dbl_scratch = double_scratch0();
2247 __ vldr(dbl_scratch, FieldMemOperand(reg, HeapNumber::kValueOffset));
2248 // Test the double value. Zero and NaN are false.
2249 __ VFPCompareAndSetFlags(dbl_scratch, 0.0);
2250 __ cmp(r0, r0, vs); // If NaN, set the Z flag. (NaN)
2251 EmitBranch(instr, ne);
2252 } else if (type.IsString()) {
2253 DCHECK(!info()->IsStub());
2254 __ ldr(ip, FieldMemOperand(reg, String::kLengthOffset));
2255 __ cmp(ip, Operand::Zero());
2256 EmitBranch(instr, ne);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002257 } else {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002258 ToBooleanStub::Types expected = instr->hydrogen()->expected_input_types();
2259 // Avoid deopts in the case where we've never executed this path before.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002260 if (expected.IsEmpty()) expected = ToBooleanStub::Types::Generic();
Ben Murdochb0fe1622011-05-05 13:52:32 +01002261
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002262 if (expected.Contains(ToBooleanStub::UNDEFINED)) {
2263 // undefined -> false.
2264 __ CompareRoot(reg, Heap::kUndefinedValueRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002265 __ b(eq, instr->FalseLabel(chunk_));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002266 }
2267 if (expected.Contains(ToBooleanStub::BOOLEAN)) {
2268 // Boolean -> its value.
2269 __ CompareRoot(reg, Heap::kTrueValueRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002270 __ b(eq, instr->TrueLabel(chunk_));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002271 __ CompareRoot(reg, Heap::kFalseValueRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002272 __ b(eq, instr->FalseLabel(chunk_));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002273 }
2274 if (expected.Contains(ToBooleanStub::NULL_TYPE)) {
2275 // 'null' -> false.
2276 __ CompareRoot(reg, Heap::kNullValueRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002277 __ b(eq, instr->FalseLabel(chunk_));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002278 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01002279
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002280 if (expected.Contains(ToBooleanStub::SMI)) {
2281 // Smis: 0 -> false, all other -> true.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002282 __ cmp(reg, Operand::Zero());
2283 __ b(eq, instr->FalseLabel(chunk_));
2284 __ JumpIfSmi(reg, instr->TrueLabel(chunk_));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002285 } else if (expected.NeedsMap()) {
2286 // If we need a map later and have a Smi -> deopt.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002287 __ SmiTst(reg);
2288 DeoptimizeIf(eq, instr);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002289 }
2290
2291 const Register map = scratch0();
2292 if (expected.NeedsMap()) {
2293 __ ldr(map, FieldMemOperand(reg, HeapObject::kMapOffset));
2294
2295 if (expected.CanBeUndetectable()) {
2296 // Undetectable -> false.
2297 __ ldrb(ip, FieldMemOperand(map, Map::kBitFieldOffset));
2298 __ tst(ip, Operand(1 << Map::kIsUndetectable));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002299 __ b(ne, instr->FalseLabel(chunk_));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002300 }
2301 }
2302
2303 if (expected.Contains(ToBooleanStub::SPEC_OBJECT)) {
2304 // spec object -> true.
2305 __ CompareInstanceType(map, ip, FIRST_SPEC_OBJECT_TYPE);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002306 __ b(ge, instr->TrueLabel(chunk_));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002307 }
2308
2309 if (expected.Contains(ToBooleanStub::STRING)) {
2310 // String value -> false iff empty.
2311 Label not_string;
2312 __ CompareInstanceType(map, ip, FIRST_NONSTRING_TYPE);
2313 __ b(ge, &not_string);
2314 __ ldr(ip, FieldMemOperand(reg, String::kLengthOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002315 __ cmp(ip, Operand::Zero());
2316 __ b(ne, instr->TrueLabel(chunk_));
2317 __ b(instr->FalseLabel(chunk_));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002318 __ bind(&not_string);
2319 }
2320
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002321 if (expected.Contains(ToBooleanStub::SYMBOL)) {
2322 // Symbol value -> true.
2323 __ CompareInstanceType(map, ip, SYMBOL_TYPE);
2324 __ b(eq, instr->TrueLabel(chunk_));
2325 }
2326
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002327 if (expected.Contains(ToBooleanStub::HEAP_NUMBER)) {
2328 // heap number -> false iff +0, -0, or NaN.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002329 DwVfpRegister dbl_scratch = double_scratch0();
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002330 Label not_heap_number;
2331 __ CompareRoot(map, Heap::kHeapNumberMapRootIndex);
2332 __ b(ne, &not_heap_number);
2333 __ vldr(dbl_scratch, FieldMemOperand(reg, HeapNumber::kValueOffset));
2334 __ VFPCompareAndSetFlags(dbl_scratch, 0.0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002335 __ cmp(r0, r0, vs); // NaN -> false.
2336 __ b(eq, instr->FalseLabel(chunk_)); // +0, -0 -> false.
2337 __ b(instr->TrueLabel(chunk_));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002338 __ bind(&not_heap_number);
2339 }
2340
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002341 if (!expected.IsGeneric()) {
2342 // We've seen something for the first time -> deopt.
2343 // This can only happen if we are not generic already.
2344 DeoptimizeIf(al, instr);
2345 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01002346 }
2347 }
2348}
2349
2350
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002351void LCodeGen::EmitGoto(int block) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002352 if (!IsNextEmittedBlock(block)) {
2353 __ jmp(chunk_->GetAssemblyLabel(LookupDestination(block)));
Ben Murdochb0fe1622011-05-05 13:52:32 +01002354 }
2355}
2356
2357
Ben Murdochb0fe1622011-05-05 13:52:32 +01002358void LCodeGen::DoGoto(LGoto* instr) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002359 EmitGoto(instr->block_id());
Ben Murdochb0fe1622011-05-05 13:52:32 +01002360}
2361
2362
2363Condition LCodeGen::TokenToCondition(Token::Value op, bool is_unsigned) {
Steve Block1e0659c2011-05-24 12:43:12 +01002364 Condition cond = kNoCondition;
Ben Murdochb0fe1622011-05-05 13:52:32 +01002365 switch (op) {
2366 case Token::EQ:
2367 case Token::EQ_STRICT:
2368 cond = eq;
2369 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002370 case Token::NE:
2371 case Token::NE_STRICT:
2372 cond = ne;
2373 break;
Ben Murdochb0fe1622011-05-05 13:52:32 +01002374 case Token::LT:
2375 cond = is_unsigned ? lo : lt;
2376 break;
2377 case Token::GT:
2378 cond = is_unsigned ? hi : gt;
2379 break;
2380 case Token::LTE:
2381 cond = is_unsigned ? ls : le;
2382 break;
2383 case Token::GTE:
2384 cond = is_unsigned ? hs : ge;
2385 break;
2386 case Token::IN:
2387 case Token::INSTANCEOF:
2388 default:
2389 UNREACHABLE();
2390 }
2391 return cond;
2392}
2393
2394
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002395void LCodeGen::DoCompareNumericAndBranch(LCompareNumericAndBranch* instr) {
2396 LOperand* left = instr->left();
2397 LOperand* right = instr->right();
2398 bool is_unsigned =
2399 instr->hydrogen()->left()->CheckFlag(HInstruction::kUint32) ||
2400 instr->hydrogen()->right()->CheckFlag(HInstruction::kUint32);
2401 Condition cond = TokenToCondition(instr->op(), is_unsigned);
Steve Block1e0659c2011-05-24 12:43:12 +01002402
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002403 if (left->IsConstantOperand() && right->IsConstantOperand()) {
2404 // We can statically evaluate the comparison.
2405 double left_val = ToDouble(LConstantOperand::cast(left));
2406 double right_val = ToDouble(LConstantOperand::cast(right));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002407 int next_block = EvalComparison(instr->op(), left_val, right_val) ?
2408 instr->TrueDestination(chunk_) : instr->FalseDestination(chunk_);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002409 EmitGoto(next_block);
Steve Block1e0659c2011-05-24 12:43:12 +01002410 } else {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002411 if (instr->is_double()) {
2412 // Compare left and right operands as doubles and load the
2413 // resulting flags into the normal status register.
2414 __ VFPCompareAndSetFlags(ToDoubleRegister(left), ToDoubleRegister(right));
2415 // If a NaN is involved, i.e. the result is unordered (V set),
2416 // jump to false block label.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002417 __ b(vs, instr->FalseLabel(chunk_));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002418 } else {
2419 if (right->IsConstantOperand()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002420 int32_t value = ToInteger32(LConstantOperand::cast(right));
2421 if (instr->hydrogen_value()->representation().IsSmi()) {
2422 __ cmp(ToRegister(left), Operand(Smi::FromInt(value)));
2423 } else {
2424 __ cmp(ToRegister(left), Operand(value));
2425 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002426 } else if (left->IsConstantOperand()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002427 int32_t value = ToInteger32(LConstantOperand::cast(left));
2428 if (instr->hydrogen_value()->representation().IsSmi()) {
2429 __ cmp(ToRegister(right), Operand(Smi::FromInt(value)));
2430 } else {
2431 __ cmp(ToRegister(right), Operand(value));
2432 }
2433 // We commuted the operands, so commute the condition.
2434 cond = CommuteCondition(cond);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002435 } else {
2436 __ cmp(ToRegister(left), ToRegister(right));
2437 }
2438 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002439 EmitBranch(instr, cond);
Steve Block1e0659c2011-05-24 12:43:12 +01002440 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01002441}
2442
2443
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002444void LCodeGen::DoCmpObjectEqAndBranch(LCmpObjectEqAndBranch* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002445 Register left = ToRegister(instr->left());
2446 Register right = ToRegister(instr->right());
Steve Block1e0659c2011-05-24 12:43:12 +01002447
2448 __ cmp(left, Operand(right));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002449 EmitBranch(instr, eq);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002450}
2451
2452
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002453void LCodeGen::DoCmpHoleAndBranch(LCmpHoleAndBranch* instr) {
2454 if (instr->hydrogen()->representation().IsTagged()) {
2455 Register input_reg = ToRegister(instr->object());
2456 __ mov(ip, Operand(factory()->the_hole_value()));
2457 __ cmp(input_reg, ip);
2458 EmitBranch(instr, eq);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002459 return;
2460 }
2461
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002462 DwVfpRegister input_reg = ToDoubleRegister(instr->object());
2463 __ VFPCompareAndSetFlags(input_reg, input_reg);
2464 EmitFalseBranch(instr, vc);
2465
2466 Register scratch = scratch0();
2467 __ VmovHigh(scratch, input_reg);
2468 __ cmp(scratch, Operand(kHoleNanUpper32));
2469 EmitBranch(instr, eq);
2470}
2471
2472
2473void LCodeGen::DoCompareMinusZeroAndBranch(LCompareMinusZeroAndBranch* instr) {
2474 Representation rep = instr->hydrogen()->value()->representation();
2475 DCHECK(!rep.IsInteger32());
2476 Register scratch = ToRegister(instr->temp());
2477
2478 if (rep.IsDouble()) {
2479 DwVfpRegister value = ToDoubleRegister(instr->value());
2480 __ VFPCompareAndSetFlags(value, 0.0);
2481 EmitFalseBranch(instr, ne);
2482 __ VmovHigh(scratch, value);
2483 __ cmp(scratch, Operand(0x80000000));
Ben Murdochb0fe1622011-05-05 13:52:32 +01002484 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002485 Register value = ToRegister(instr->value());
2486 __ CheckMap(value,
2487 scratch,
2488 Heap::kHeapNumberMapRootIndex,
2489 instr->FalseLabel(chunk()),
2490 DO_SMI_CHECK);
2491 __ ldr(scratch, FieldMemOperand(value, HeapNumber::kExponentOffset));
2492 __ ldr(ip, FieldMemOperand(value, HeapNumber::kMantissaOffset));
2493 __ cmp(scratch, Operand(0x80000000));
2494 __ cmp(ip, Operand(0x00000000), eq);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002495 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002496 EmitBranch(instr, eq);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002497}
2498
2499
2500Condition LCodeGen::EmitIsObject(Register input,
2501 Register temp1,
Ben Murdochb0fe1622011-05-05 13:52:32 +01002502 Label* is_not_object,
2503 Label* is_object) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002504 Register temp2 = scratch0();
Steve Block1e0659c2011-05-24 12:43:12 +01002505 __ JumpIfSmi(input, is_not_object);
2506
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002507 __ LoadRoot(temp2, Heap::kNullValueRootIndex);
2508 __ cmp(input, temp2);
Steve Block1e0659c2011-05-24 12:43:12 +01002509 __ b(eq, is_object);
2510
2511 // Load map.
2512 __ ldr(temp1, FieldMemOperand(input, HeapObject::kMapOffset));
2513 // Undetectable objects behave like undefined.
2514 __ ldrb(temp2, FieldMemOperand(temp1, Map::kBitFieldOffset));
2515 __ tst(temp2, Operand(1 << Map::kIsUndetectable));
2516 __ b(ne, is_not_object);
2517
2518 // Load instance type and check that it is in object type range.
2519 __ ldrb(temp2, FieldMemOperand(temp1, Map::kInstanceTypeOffset));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002520 __ cmp(temp2, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
Steve Block1e0659c2011-05-24 12:43:12 +01002521 __ b(lt, is_not_object);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002522 __ cmp(temp2, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE));
Steve Block1e0659c2011-05-24 12:43:12 +01002523 return le;
Ben Murdochb0fe1622011-05-05 13:52:32 +01002524}
2525
2526
Ben Murdochb0fe1622011-05-05 13:52:32 +01002527void LCodeGen::DoIsObjectAndBranch(LIsObjectAndBranch* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002528 Register reg = ToRegister(instr->value());
2529 Register temp1 = ToRegister(instr->temp());
Steve Block1e0659c2011-05-24 12:43:12 +01002530
2531 Condition true_cond =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002532 EmitIsObject(reg, temp1,
2533 instr->FalseLabel(chunk_), instr->TrueLabel(chunk_));
Steve Block1e0659c2011-05-24 12:43:12 +01002534
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002535 EmitBranch(instr, true_cond);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002536}
2537
2538
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002539Condition LCodeGen::EmitIsString(Register input,
2540 Register temp1,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002541 Label* is_not_string,
2542 SmiCheck check_needed = INLINE_SMI_CHECK) {
2543 if (check_needed == INLINE_SMI_CHECK) {
2544 __ JumpIfSmi(input, is_not_string);
2545 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002546 __ CompareObjectType(input, temp1, temp1, FIRST_NONSTRING_TYPE);
2547
2548 return lt;
2549}
2550
2551
2552void LCodeGen::DoIsStringAndBranch(LIsStringAndBranch* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002553 Register reg = ToRegister(instr->value());
2554 Register temp1 = ToRegister(instr->temp());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002555
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002556 SmiCheck check_needed =
2557 instr->hydrogen()->value()->type().IsHeapObject()
2558 ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002559 Condition true_cond =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002560 EmitIsString(reg, temp1, instr->FalseLabel(chunk_), check_needed);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002561
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002562 EmitBranch(instr, true_cond);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002563}
2564
2565
Ben Murdochb0fe1622011-05-05 13:52:32 +01002566void LCodeGen::DoIsSmiAndBranch(LIsSmiAndBranch* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002567 Register input_reg = EmitLoadRegister(instr->value(), ip);
2568 __ SmiTst(input_reg);
2569 EmitBranch(instr, eq);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002570}
2571
2572
Ben Murdoch257744e2011-11-30 15:57:28 +00002573void LCodeGen::DoIsUndetectableAndBranch(LIsUndetectableAndBranch* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002574 Register input = ToRegister(instr->value());
2575 Register temp = ToRegister(instr->temp());
Ben Murdoch257744e2011-11-30 15:57:28 +00002576
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002577 if (!instr->hydrogen()->value()->type().IsHeapObject()) {
2578 __ JumpIfSmi(input, instr->FalseLabel(chunk_));
2579 }
Ben Murdoch257744e2011-11-30 15:57:28 +00002580 __ ldr(temp, FieldMemOperand(input, HeapObject::kMapOffset));
2581 __ ldrb(temp, FieldMemOperand(temp, Map::kBitFieldOffset));
2582 __ tst(temp, Operand(1 << Map::kIsUndetectable));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002583 EmitBranch(instr, ne);
Ben Murdoch257744e2011-11-30 15:57:28 +00002584}
2585
2586
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002587static Condition ComputeCompareCondition(Token::Value op) {
2588 switch (op) {
2589 case Token::EQ_STRICT:
2590 case Token::EQ:
2591 return eq;
2592 case Token::LT:
2593 return lt;
2594 case Token::GT:
2595 return gt;
2596 case Token::LTE:
2597 return le;
2598 case Token::GTE:
2599 return ge;
2600 default:
2601 UNREACHABLE();
2602 return kNoCondition;
2603 }
2604}
2605
2606
2607void LCodeGen::DoStringCompareAndBranch(LStringCompareAndBranch* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002608 DCHECK(ToRegister(instr->context()).is(cp));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002609 Token::Value op = instr->op();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002610
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002611 Handle<Code> ic = CodeFactory::CompareIC(isolate(), op).code();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002612 CallCode(ic, RelocInfo::CODE_TARGET, instr);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002613 // This instruction also signals no smi code inlined.
2614 __ cmp(r0, Operand::Zero());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002615
2616 Condition condition = ComputeCompareCondition(op);
2617
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002618 EmitBranch(instr, condition);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002619}
2620
2621
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002622static InstanceType TestType(HHasInstanceTypeAndBranch* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01002623 InstanceType from = instr->from();
2624 InstanceType to = instr->to();
Ben Murdochb0fe1622011-05-05 13:52:32 +01002625 if (from == FIRST_TYPE) return to;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002626 DCHECK(from == to || to == LAST_TYPE);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002627 return from;
2628}
2629
2630
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002631static Condition BranchCondition(HHasInstanceTypeAndBranch* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01002632 InstanceType from = instr->from();
2633 InstanceType to = instr->to();
Ben Murdochb0fe1622011-05-05 13:52:32 +01002634 if (from == to) return eq;
2635 if (to == LAST_TYPE) return hs;
2636 if (from == FIRST_TYPE) return ls;
2637 UNREACHABLE();
2638 return eq;
2639}
2640
2641
Ben Murdochb0fe1622011-05-05 13:52:32 +01002642void LCodeGen::DoHasInstanceTypeAndBranch(LHasInstanceTypeAndBranch* instr) {
Steve Block9fac8402011-05-12 15:51:54 +01002643 Register scratch = scratch0();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002644 Register input = ToRegister(instr->value());
Ben Murdochb0fe1622011-05-05 13:52:32 +01002645
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002646 if (!instr->hydrogen()->value()->type().IsHeapObject()) {
2647 __ JumpIfSmi(input, instr->FalseLabel(chunk_));
2648 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01002649
Steve Block1e0659c2011-05-24 12:43:12 +01002650 __ CompareObjectType(input, scratch, scratch, TestType(instr->hydrogen()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002651 EmitBranch(instr, BranchCondition(instr->hydrogen()));
Ben Murdochb0fe1622011-05-05 13:52:32 +01002652}
2653
2654
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002655void LCodeGen::DoGetCachedArrayIndex(LGetCachedArrayIndex* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002656 Register input = ToRegister(instr->value());
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002657 Register result = ToRegister(instr->result());
2658
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002659 __ AssertString(input);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002660
2661 __ ldr(result, FieldMemOperand(input, String::kHashFieldOffset));
2662 __ IndexFromHash(result, result);
2663}
2664
2665
Ben Murdochb0fe1622011-05-05 13:52:32 +01002666void LCodeGen::DoHasCachedArrayIndexAndBranch(
2667 LHasCachedArrayIndexAndBranch* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002668 Register input = ToRegister(instr->value());
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002669 Register scratch = scratch0();
2670
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002671 __ ldr(scratch,
2672 FieldMemOperand(input, String::kHashFieldOffset));
2673 __ tst(scratch, Operand(String::kContainsCachedArrayIndexMask));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002674 EmitBranch(instr, eq);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002675}
2676
2677
Ben Murdochb8e0da22011-05-16 14:20:40 +01002678// Branches to a label or falls through with the answer in flags. Trashes
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002679// the temp registers, but not the input.
Ben Murdochb0fe1622011-05-05 13:52:32 +01002680void LCodeGen::EmitClassOfTest(Label* is_true,
2681 Label* is_false,
2682 Handle<String>class_name,
2683 Register input,
2684 Register temp,
2685 Register temp2) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002686 DCHECK(!input.is(temp));
2687 DCHECK(!input.is(temp2));
2688 DCHECK(!temp.is(temp2));
Ben Murdochb8e0da22011-05-16 14:20:40 +01002689
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002690 __ JumpIfSmi(input, is_false);
2691
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002692 if (String::Equals(isolate()->factory()->Function_string(), class_name)) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002693 // Assuming the following assertions, we can use the same compares to test
2694 // for both being a function type and being in the object type range.
2695 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
2696 STATIC_ASSERT(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2697 FIRST_SPEC_OBJECT_TYPE + 1);
2698 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2699 LAST_SPEC_OBJECT_TYPE - 1);
2700 STATIC_ASSERT(LAST_SPEC_OBJECT_TYPE == LAST_TYPE);
2701 __ CompareObjectType(input, temp, temp2, FIRST_SPEC_OBJECT_TYPE);
2702 __ b(lt, is_false);
2703 __ b(eq, is_true);
2704 __ cmp(temp2, Operand(LAST_SPEC_OBJECT_TYPE));
2705 __ b(eq, is_true);
Ben Murdochb8e0da22011-05-16 14:20:40 +01002706 } else {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002707 // Faster code path to avoid two compares: subtract lower bound from the
2708 // actual type and do a signed compare with the width of the type range.
2709 __ ldr(temp, FieldMemOperand(input, HeapObject::kMapOffset));
2710 __ ldrb(temp2, FieldMemOperand(temp, Map::kInstanceTypeOffset));
2711 __ sub(temp2, temp2, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
2712 __ cmp(temp2, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE -
2713 FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
2714 __ b(gt, is_false);
Ben Murdochb8e0da22011-05-16 14:20:40 +01002715 }
2716
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002717 // Now we are in the FIRST-LAST_NONCALLABLE_SPEC_OBJECT_TYPE range.
Ben Murdochb8e0da22011-05-16 14:20:40 +01002718 // Check if the constructor in the map is a function.
2719 __ ldr(temp, FieldMemOperand(temp, Map::kConstructorOffset));
2720
Ben Murdochb8e0da22011-05-16 14:20:40 +01002721 // Objects with a non-function constructor have class 'Object'.
2722 __ CompareObjectType(temp, temp2, temp2, JS_FUNCTION_TYPE);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002723 if (class_name->IsOneByteEqualTo(STATIC_CHAR_VECTOR("Object"))) {
Ben Murdochb8e0da22011-05-16 14:20:40 +01002724 __ b(ne, is_true);
2725 } else {
2726 __ b(ne, is_false);
2727 }
2728
2729 // temp now contains the constructor function. Grab the
2730 // instance class name from there.
2731 __ ldr(temp, FieldMemOperand(temp, JSFunction::kSharedFunctionInfoOffset));
2732 __ ldr(temp, FieldMemOperand(temp,
2733 SharedFunctionInfo::kInstanceClassNameOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002734 // The class name we are testing against is internalized since it's a literal.
2735 // The name in the constructor is internalized because of the way the context
2736 // is booted. This routine isn't expected to work for random API-created
Ben Murdochb8e0da22011-05-16 14:20:40 +01002737 // classes and it doesn't have to because you can't access it with natives
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002738 // syntax. Since both sides are internalized it is sufficient to use an
2739 // identity comparison.
Ben Murdochb8e0da22011-05-16 14:20:40 +01002740 __ cmp(temp, Operand(class_name));
2741 // End with the answer in flags.
Ben Murdochb0fe1622011-05-05 13:52:32 +01002742}
2743
2744
Ben Murdochb0fe1622011-05-05 13:52:32 +01002745void LCodeGen::DoClassOfTestAndBranch(LClassOfTestAndBranch* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002746 Register input = ToRegister(instr->value());
Ben Murdochb8e0da22011-05-16 14:20:40 +01002747 Register temp = scratch0();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002748 Register temp2 = ToRegister(instr->temp());
Ben Murdochb8e0da22011-05-16 14:20:40 +01002749 Handle<String> class_name = instr->hydrogen()->class_name();
2750
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002751 EmitClassOfTest(instr->TrueLabel(chunk_), instr->FalseLabel(chunk_),
2752 class_name, input, temp, temp2);
Ben Murdochb8e0da22011-05-16 14:20:40 +01002753
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002754 EmitBranch(instr, eq);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002755}
2756
2757
2758void LCodeGen::DoCmpMapAndBranch(LCmpMapAndBranch* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002759 Register reg = ToRegister(instr->value());
2760 Register temp = ToRegister(instr->temp());
Steve Block9fac8402011-05-12 15:51:54 +01002761
2762 __ ldr(temp, FieldMemOperand(reg, HeapObject::kMapOffset));
2763 __ cmp(temp, Operand(instr->map()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002764 EmitBranch(instr, eq);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002765}
2766
2767
2768void LCodeGen::DoInstanceOf(LInstanceOf* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002769 DCHECK(ToRegister(instr->context()).is(cp));
2770 DCHECK(ToRegister(instr->left()).is(r0)); // Object is in r0.
2771 DCHECK(ToRegister(instr->right()).is(r1)); // Function is in r1.
Steve Block9fac8402011-05-12 15:51:54 +01002772
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002773 InstanceofStub stub(isolate(), InstanceofStub::kArgsInRegisters);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002774 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
2775
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002776 __ cmp(r0, Operand::Zero());
Steve Block44f0eee2011-05-26 01:26:41 +01002777 __ mov(r0, Operand(factory()->false_value()), LeaveCC, ne);
2778 __ mov(r0, Operand(factory()->true_value()), LeaveCC, eq);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002779}
2780
2781
Ben Murdoch086aeea2011-05-13 15:57:08 +01002782void LCodeGen::DoInstanceOfKnownGlobal(LInstanceOfKnownGlobal* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002783 class DeferredInstanceOfKnownGlobal FINAL : public LDeferredCode {
Steve Block1e0659c2011-05-24 12:43:12 +01002784 public:
2785 DeferredInstanceOfKnownGlobal(LCodeGen* codegen,
2786 LInstanceOfKnownGlobal* instr)
2787 : LDeferredCode(codegen), instr_(instr) { }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002788 virtual void Generate() OVERRIDE {
2789 codegen()->DoDeferredInstanceOfKnownGlobal(instr_, &map_check_,
2790 &load_bool_);
Steve Block1e0659c2011-05-24 12:43:12 +01002791 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002792 virtual LInstruction* instr() OVERRIDE { return instr_; }
Steve Block1e0659c2011-05-24 12:43:12 +01002793 Label* map_check() { return &map_check_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002794 Label* load_bool() { return &load_bool_; }
2795
Steve Block1e0659c2011-05-24 12:43:12 +01002796 private:
2797 LInstanceOfKnownGlobal* instr_;
2798 Label map_check_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002799 Label load_bool_;
Steve Block1e0659c2011-05-24 12:43:12 +01002800 };
2801
2802 DeferredInstanceOfKnownGlobal* deferred;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002803 deferred = new(zone()) DeferredInstanceOfKnownGlobal(this, instr);
Steve Block1e0659c2011-05-24 12:43:12 +01002804
2805 Label done, false_result;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002806 Register object = ToRegister(instr->value());
2807 Register temp = ToRegister(instr->temp());
Steve Block1e0659c2011-05-24 12:43:12 +01002808 Register result = ToRegister(instr->result());
2809
Steve Block1e0659c2011-05-24 12:43:12 +01002810 // A Smi is not instance of anything.
2811 __ JumpIfSmi(object, &false_result);
2812
2813 // This is the inlined call site instanceof cache. The two occurences of the
2814 // hole value will be patched to the last map/result pair generated by the
2815 // instanceof stub.
2816 Label cache_miss;
2817 Register map = temp;
2818 __ ldr(map, FieldMemOperand(object, HeapObject::kMapOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002819 {
2820 // Block constant pool emission to ensure the positions of instructions are
2821 // as expected by the patcher. See InstanceofStub::Generate().
2822 Assembler::BlockConstPoolScope block_const_pool(masm());
2823 __ bind(deferred->map_check()); // Label for calculating code patching.
2824 // We use Factory::the_hole_value() on purpose instead of loading from the
2825 // root array to force relocation to be able to later patch with
2826 // the cached map.
2827 Handle<Cell> cell = factory()->NewCell(factory()->the_hole_value());
2828 __ mov(ip, Operand(Handle<Object>(cell)));
2829 __ ldr(ip, FieldMemOperand(ip, PropertyCell::kValueOffset));
2830 __ cmp(map, Operand(ip));
2831 __ b(ne, &cache_miss);
2832 __ bind(deferred->load_bool()); // Label for calculating code patching.
2833 // We use Factory::the_hole_value() on purpose instead of loading from the
2834 // root array to force relocation to be able to later patch
2835 // with true or false.
2836 __ mov(result, Operand(factory()->the_hole_value()));
2837 }
Steve Block1e0659c2011-05-24 12:43:12 +01002838 __ b(&done);
2839
2840 // The inlined call site cache did not match. Check null and string before
2841 // calling the deferred code.
2842 __ bind(&cache_miss);
2843 // Null is not instance of anything.
2844 __ LoadRoot(ip, Heap::kNullValueRootIndex);
2845 __ cmp(object, Operand(ip));
2846 __ b(eq, &false_result);
2847
2848 // String values is not instance of anything.
2849 Condition is_string = masm_->IsObjectStringType(object, temp);
2850 __ b(is_string, &false_result);
2851
2852 // Go to the deferred code.
2853 __ b(deferred->entry());
2854
2855 __ bind(&false_result);
2856 __ LoadRoot(result, Heap::kFalseValueRootIndex);
2857
2858 // Here result has either true or false. Deferred code also produces true or
2859 // false object.
2860 __ bind(deferred->exit());
2861 __ bind(&done);
2862}
2863
2864
Ben Murdoch2b4ba112012-01-20 14:57:15 +00002865void LCodeGen::DoDeferredInstanceOfKnownGlobal(LInstanceOfKnownGlobal* instr,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002866 Label* map_check,
2867 Label* bool_load) {
Steve Block1e0659c2011-05-24 12:43:12 +01002868 InstanceofStub::Flags flags = InstanceofStub::kNoFlags;
2869 flags = static_cast<InstanceofStub::Flags>(
2870 flags | InstanceofStub::kArgsInRegisters);
2871 flags = static_cast<InstanceofStub::Flags>(
2872 flags | InstanceofStub::kCallSiteInlineCheck);
2873 flags = static_cast<InstanceofStub::Flags>(
2874 flags | InstanceofStub::kReturnTrueFalseObject);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002875 InstanceofStub stub(isolate(), flags);
Steve Block1e0659c2011-05-24 12:43:12 +01002876
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002877 PushSafepointRegistersScope scope(this);
2878 LoadContextFromDeferred(instr->context());
Steve Block1e0659c2011-05-24 12:43:12 +01002879
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002880 __ Move(InstanceofStub::right(), instr->function());
2881
2882 int call_size = CallCodeSize(stub.GetCode(), RelocInfo::CODE_TARGET);
2883 int additional_delta = (call_size / Assembler::kInstrSize) + 4;
2884 // Make sure that code size is predicable, since we use specific constants
2885 // offsets in the code to find embedded values..
2886 PredictableCodeSizeScope predictable(
2887 masm_, (additional_delta + 1) * Assembler::kInstrSize);
2888 // Make sure we don't emit any additional entries in the constant pool before
2889 // the call to ensure that the CallCodeSize() calculated the correct number of
2890 // instructions for the constant pool load.
2891 {
2892 ConstantPoolUnavailableScope constant_pool_unavailable(masm_);
2893 int map_check_delta =
2894 masm_->InstructionsGeneratedSince(map_check) + additional_delta;
2895 int bool_load_delta =
2896 masm_->InstructionsGeneratedSince(bool_load) + additional_delta;
2897 Label before_push_delta;
2898 __ bind(&before_push_delta);
2899 __ BlockConstPoolFor(additional_delta);
2900 // r5 is used to communicate the offset to the location of the map check.
2901 __ mov(r5, Operand(map_check_delta * kPointerSize));
2902 // r6 is used to communicate the offset to the location of the bool load.
2903 __ mov(r6, Operand(bool_load_delta * kPointerSize));
2904 // The mov above can generate one or two instructions. The delta was
2905 // computed for two instructions, so we need to pad here in case of one
2906 // instruction.
2907 while (masm_->InstructionsGeneratedSince(&before_push_delta) != 4) {
2908 __ nop();
2909 }
2910 }
Ben Murdoch8b112d22011-06-08 16:22:53 +01002911 CallCodeGeneric(stub.GetCode(),
2912 RelocInfo::CODE_TARGET,
2913 instr,
2914 RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002915 LEnvironment* env = instr->GetDeferredLazyDeoptimizationEnvironment();
Ben Murdoch2b4ba112012-01-20 14:57:15 +00002916 safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002917 // Put the result value (r0) into the result register slot and
Steve Block1e0659c2011-05-24 12:43:12 +01002918 // restore all registers.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002919 __ StoreToSafepointRegisterSlot(r0, ToRegister(instr->result()));
Ben Murdoch086aeea2011-05-13 15:57:08 +01002920}
2921
Ben Murdochb0fe1622011-05-05 13:52:32 +01002922
Ben Murdochb0fe1622011-05-05 13:52:32 +01002923void LCodeGen::DoCmpT(LCmpT* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002924 DCHECK(ToRegister(instr->context()).is(cp));
Ben Murdochb0fe1622011-05-05 13:52:32 +01002925 Token::Value op = instr->op();
2926
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002927 Handle<Code> ic = CodeFactory::CompareIC(isolate(), op).code();
Ben Murdochb0fe1622011-05-05 13:52:32 +01002928 CallCode(ic, RelocInfo::CODE_TARGET, instr);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002929 // This instruction also signals no smi code inlined.
2930 __ cmp(r0, Operand::Zero());
Ben Murdochb0fe1622011-05-05 13:52:32 +01002931
2932 Condition condition = ComputeCompareCondition(op);
Ben Murdochb8e0da22011-05-16 14:20:40 +01002933 __ LoadRoot(ToRegister(instr->result()),
2934 Heap::kTrueValueRootIndex,
2935 condition);
2936 __ LoadRoot(ToRegister(instr->result()),
2937 Heap::kFalseValueRootIndex,
2938 NegateCondition(condition));
Ben Murdochb0fe1622011-05-05 13:52:32 +01002939}
2940
2941
Ben Murdochb0fe1622011-05-05 13:52:32 +01002942void LCodeGen::DoReturn(LReturn* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002943 if (FLAG_trace && info()->IsOptimizing()) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01002944 // Push the return value on the stack as the parameter.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002945 // Runtime::TraceExit returns its parameter in r0. We're leaving the code
2946 // managed by the register allocator and tearing down the frame, it's
2947 // safe to write to the context register.
Ben Murdochb0fe1622011-05-05 13:52:32 +01002948 __ push(r0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002949 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
Ben Murdochb0fe1622011-05-05 13:52:32 +01002950 __ CallRuntime(Runtime::kTraceExit, 1);
2951 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002952 if (info()->saves_caller_doubles()) {
2953 RestoreCallerDoubles();
2954 }
2955 int no_frame_start = -1;
2956 if (NeedsEagerFrame()) {
2957 no_frame_start = masm_->LeaveFrame(StackFrame::JAVA_SCRIPT);
2958 }
2959 { ConstantPoolUnavailableScope constant_pool_unavailable(masm());
2960 if (instr->has_constant_parameter_count()) {
2961 int parameter_count = ToInteger32(instr->constant_parameter_count());
2962 int32_t sp_delta = (parameter_count + 1) * kPointerSize;
2963 if (sp_delta != 0) {
2964 __ add(sp, sp, Operand(sp_delta));
2965 }
2966 } else {
2967 Register reg = ToRegister(instr->parameter_count());
2968 // The argument count parameter is a smi
2969 __ SmiUntag(reg);
2970 __ add(sp, sp, Operand(reg, LSL, kPointerSizeLog2));
2971 }
2972
2973 __ Jump(lr);
2974
2975 if (no_frame_start != -1) {
2976 info_->AddNoFrameRange(no_frame_start, masm_->pc_offset());
2977 }
2978 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01002979}
2980
2981
Ben Murdoch8b112d22011-06-08 16:22:53 +01002982void LCodeGen::DoLoadGlobalCell(LLoadGlobalCell* instr) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01002983 Register result = ToRegister(instr->result());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002984 __ mov(ip, Operand(Handle<Object>(instr->hydrogen()->cell().handle())));
2985 __ ldr(result, FieldMemOperand(ip, Cell::kValueOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002986 if (instr->hydrogen()->RequiresHoleCheck()) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01002987 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
2988 __ cmp(result, ip);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002989 DeoptimizeIf(eq, instr);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002990 }
2991}
2992
2993
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002994template <class T>
2995void LCodeGen::EmitVectorLoadICRegisters(T* instr) {
2996 DCHECK(FLAG_vector_ics);
2997 Register vector = ToRegister(instr->temp_vector());
2998 DCHECK(vector.is(VectorLoadICDescriptor::VectorRegister()));
2999 __ Move(vector, instr->hydrogen()->feedback_vector());
3000 // No need to allocate this register.
3001 DCHECK(VectorLoadICDescriptor::SlotRegister().is(r0));
3002 __ mov(VectorLoadICDescriptor::SlotRegister(),
3003 Operand(Smi::FromInt(instr->hydrogen()->slot())));
3004}
Ben Murdoch8b112d22011-06-08 16:22:53 +01003005
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003006
3007void LCodeGen::DoLoadGlobalGeneric(LLoadGlobalGeneric* instr) {
3008 DCHECK(ToRegister(instr->context()).is(cp));
3009 DCHECK(ToRegister(instr->global_object())
3010 .is(LoadDescriptor::ReceiverRegister()));
3011 DCHECK(ToRegister(instr->result()).is(r0));
3012
3013 __ mov(LoadDescriptor::NameRegister(), Operand(instr->name()));
3014 if (FLAG_vector_ics) {
3015 EmitVectorLoadICRegisters<LLoadGlobalGeneric>(instr);
3016 }
3017 ContextualMode mode = instr->for_typeof() ? NOT_CONTEXTUAL : CONTEXTUAL;
3018 Handle<Code> ic = CodeFactory::LoadIC(isolate(), mode).code();
3019 CallCode(ic, RelocInfo::CODE_TARGET, instr);
Ben Murdoch8b112d22011-06-08 16:22:53 +01003020}
3021
3022
3023void LCodeGen::DoStoreGlobalCell(LStoreGlobalCell* instr) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003024 Register value = ToRegister(instr->value());
3025 Register cell = scratch0();
Steve Block1e0659c2011-05-24 12:43:12 +01003026
3027 // Load the cell.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003028 __ mov(cell, Operand(instr->hydrogen()->cell().handle()));
Steve Block1e0659c2011-05-24 12:43:12 +01003029
3030 // If the cell we are storing to contains the hole it could have
3031 // been deleted from the property dictionary. In that case, we need
3032 // to update the property details in the property dictionary to mark
3033 // it as no longer deleted.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003034 if (instr->hydrogen()->RequiresHoleCheck()) {
3035 // We use a temp to check the payload (CompareRoot might clobber ip).
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003036 Register payload = ToRegister(instr->temp());
3037 __ ldr(payload, FieldMemOperand(cell, Cell::kValueOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003038 __ CompareRoot(payload, Heap::kTheHoleValueRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003039 DeoptimizeIf(eq, instr);
Steve Block1e0659c2011-05-24 12:43:12 +01003040 }
3041
3042 // Store the value.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003043 __ str(value, FieldMemOperand(cell, Cell::kValueOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003044 // Cells are always rescanned, so no write barrier here.
Ben Murdochb0fe1622011-05-05 13:52:32 +01003045}
3046
3047
Ben Murdochb8e0da22011-05-16 14:20:40 +01003048void LCodeGen::DoLoadContextSlot(LLoadContextSlot* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01003049 Register context = ToRegister(instr->context());
Ben Murdochb8e0da22011-05-16 14:20:40 +01003050 Register result = ToRegister(instr->result());
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003051 __ ldr(result, ContextOperand(context, instr->slot_index()));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003052 if (instr->hydrogen()->RequiresHoleCheck()) {
3053 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
3054 __ cmp(result, ip);
3055 if (instr->hydrogen()->DeoptimizesOnHole()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003056 DeoptimizeIf(eq, instr);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003057 } else {
3058 __ mov(result, Operand(factory()->undefined_value()), LeaveCC, eq);
3059 }
3060 }
Ben Murdochb8e0da22011-05-16 14:20:40 +01003061}
3062
3063
Steve Block1e0659c2011-05-24 12:43:12 +01003064void LCodeGen::DoStoreContextSlot(LStoreContextSlot* instr) {
3065 Register context = ToRegister(instr->context());
3066 Register value = ToRegister(instr->value());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003067 Register scratch = scratch0();
3068 MemOperand target = ContextOperand(context, instr->slot_index());
3069
3070 Label skip_assignment;
3071
3072 if (instr->hydrogen()->RequiresHoleCheck()) {
3073 __ ldr(scratch, target);
3074 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
3075 __ cmp(scratch, ip);
3076 if (instr->hydrogen()->DeoptimizesOnHole()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003077 DeoptimizeIf(eq, instr);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003078 } else {
3079 __ b(ne, &skip_assignment);
3080 }
Ben Murdochc7cc0282012-03-05 14:35:55 +00003081 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003082
3083 __ str(value, target);
3084 if (instr->hydrogen()->NeedsWriteBarrier()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003085 SmiCheck check_needed =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003086 instr->hydrogen()->value()->type().IsHeapObject()
3087 ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003088 __ RecordWriteContextSlot(context,
3089 target.offset(),
3090 value,
3091 scratch,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003092 GetLinkRegisterState(),
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003093 kSaveFPRegs,
3094 EMIT_REMEMBERED_SET,
3095 check_needed);
3096 }
3097
3098 __ bind(&skip_assignment);
Steve Block1e0659c2011-05-24 12:43:12 +01003099}
3100
3101
Ben Murdochb0fe1622011-05-05 13:52:32 +01003102void LCodeGen::DoLoadNamedField(LLoadNamedField* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003103 HObjectAccess access = instr->hydrogen()->access();
3104 int offset = access.offset();
Steve Block44f0eee2011-05-26 01:26:41 +01003105 Register object = ToRegister(instr->object());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003106
3107 if (access.IsExternalMemory()) {
3108 Register result = ToRegister(instr->result());
3109 MemOperand operand = MemOperand(object, offset);
3110 __ Load(result, operand, access.representation());
3111 return;
Steve Block44f0eee2011-05-26 01:26:41 +01003112 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003113
3114 if (instr->hydrogen()->representation().IsDouble()) {
3115 DwVfpRegister result = ToDoubleRegister(instr->result());
3116 __ vldr(result, FieldMemOperand(object, offset));
3117 return;
3118 }
3119
3120 Register result = ToRegister(instr->result());
3121 if (!access.IsInobject()) {
3122 __ ldr(result, FieldMemOperand(object, JSObject::kPropertiesOffset));
3123 object = result;
3124 }
3125 MemOperand operand = FieldMemOperand(object, offset);
3126 __ Load(result, operand, access.representation());
Steve Block44f0eee2011-05-26 01:26:41 +01003127}
3128
3129
Ben Murdochb0fe1622011-05-05 13:52:32 +01003130void LCodeGen::DoLoadNamedGeneric(LLoadNamedGeneric* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003131 DCHECK(ToRegister(instr->context()).is(cp));
3132 DCHECK(ToRegister(instr->object()).is(LoadDescriptor::ReceiverRegister()));
3133 DCHECK(ToRegister(instr->result()).is(r0));
Ben Murdochb0fe1622011-05-05 13:52:32 +01003134
3135 // Name is always in r2.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003136 __ mov(LoadDescriptor::NameRegister(), Operand(instr->name()));
3137 if (FLAG_vector_ics) {
3138 EmitVectorLoadICRegisters<LLoadNamedGeneric>(instr);
3139 }
3140 Handle<Code> ic = CodeFactory::LoadIC(isolate(), NOT_CONTEXTUAL).code();
3141 CallCode(ic, RelocInfo::CODE_TARGET, instr, NEVER_INLINE_TARGET_ADDRESS);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003142}
3143
3144
Steve Block9fac8402011-05-12 15:51:54 +01003145void LCodeGen::DoLoadFunctionPrototype(LLoadFunctionPrototype* instr) {
3146 Register scratch = scratch0();
3147 Register function = ToRegister(instr->function());
3148 Register result = ToRegister(instr->result());
3149
Steve Block9fac8402011-05-12 15:51:54 +01003150 // Get the prototype or initial map from the function.
3151 __ ldr(result,
3152 FieldMemOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
3153
3154 // Check that the function has a prototype or an initial map.
3155 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
3156 __ cmp(result, ip);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003157 DeoptimizeIf(eq, instr);
Steve Block9fac8402011-05-12 15:51:54 +01003158
3159 // If the function does not have an initial map, we're done.
3160 Label done;
3161 __ CompareObjectType(result, scratch, scratch, MAP_TYPE);
3162 __ b(ne, &done);
3163
3164 // Get the prototype from the initial map.
3165 __ ldr(result, FieldMemOperand(result, Map::kPrototypeOffset));
Steve Block9fac8402011-05-12 15:51:54 +01003166
3167 // All done.
3168 __ bind(&done);
3169}
3170
3171
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003172void LCodeGen::DoLoadRoot(LLoadRoot* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01003173 Register result = ToRegister(instr->result());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003174 __ LoadRoot(result, instr->index());
Steve Block1e0659c2011-05-24 12:43:12 +01003175}
3176
3177
Ben Murdochb0fe1622011-05-05 13:52:32 +01003178void LCodeGen::DoAccessArgumentsAt(LAccessArgumentsAt* instr) {
Ben Murdoch086aeea2011-05-13 15:57:08 +01003179 Register arguments = ToRegister(instr->arguments());
Ben Murdoch086aeea2011-05-13 15:57:08 +01003180 Register result = ToRegister(instr->result());
Ben Murdoch086aeea2011-05-13 15:57:08 +01003181 // There are two words between the frame pointer and the last argument.
3182 // Subtracting from length accounts for one of them add one more.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003183 if (instr->length()->IsConstantOperand()) {
3184 int const_length = ToInteger32(LConstantOperand::cast(instr->length()));
3185 if (instr->index()->IsConstantOperand()) {
3186 int const_index = ToInteger32(LConstantOperand::cast(instr->index()));
3187 int index = (const_length - const_index) + 1;
3188 __ ldr(result, MemOperand(arguments, index * kPointerSize));
3189 } else {
3190 Register index = ToRegister(instr->index());
3191 __ rsb(result, index, Operand(const_length + 1));
3192 __ ldr(result, MemOperand(arguments, result, LSL, kPointerSizeLog2));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003193 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003194 } else if (instr->index()->IsConstantOperand()) {
3195 Register length = ToRegister(instr->length());
3196 int const_index = ToInteger32(LConstantOperand::cast(instr->index()));
3197 int loc = const_index - 1;
3198 if (loc != 0) {
3199 __ sub(result, length, Operand(loc));
3200 __ ldr(result, MemOperand(arguments, result, LSL, kPointerSizeLog2));
3201 } else {
3202 __ ldr(result, MemOperand(arguments, length, LSL, kPointerSizeLog2));
3203 }
3204 } else {
3205 Register length = ToRegister(instr->length());
3206 Register index = ToRegister(instr->index());
3207 __ sub(result, length, index);
3208 __ add(result, result, Operand(1));
3209 __ ldr(result, MemOperand(arguments, result, LSL, kPointerSizeLog2));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003210 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003211}
3212
3213
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003214void LCodeGen::DoLoadKeyedExternalArray(LLoadKeyed* instr) {
3215 Register external_pointer = ToRegister(instr->elements());
Ben Murdoch257744e2011-11-30 15:57:28 +00003216 Register key = no_reg;
Ben Murdoch589d6972011-11-30 16:04:58 +00003217 ElementsKind elements_kind = instr->elements_kind();
Ben Murdoch257744e2011-11-30 15:57:28 +00003218 bool key_is_constant = instr->key()->IsConstantOperand();
3219 int constant_key = 0;
3220 if (key_is_constant) {
3221 constant_key = ToInteger32(LConstantOperand::cast(instr->key()));
3222 if (constant_key & 0xF0000000) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003223 Abort(kArrayIndexConstantValueTooBig);
Ben Murdoch257744e2011-11-30 15:57:28 +00003224 }
3225 } else {
3226 key = ToRegister(instr->key());
3227 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003228 int element_size_shift = ElementsKindToShiftSize(elements_kind);
3229 int shift_size = (instr->hydrogen()->key()->representation().IsSmi())
3230 ? (element_size_shift - kSmiTagSize) : element_size_shift;
3231 int base_offset = instr->base_offset();
Ben Murdoch257744e2011-11-30 15:57:28 +00003232
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003233 if (elements_kind == EXTERNAL_FLOAT32_ELEMENTS ||
3234 elements_kind == FLOAT32_ELEMENTS ||
3235 elements_kind == EXTERNAL_FLOAT64_ELEMENTS ||
3236 elements_kind == FLOAT64_ELEMENTS) {
3237 int base_offset = instr->base_offset();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003238 DwVfpRegister result = ToDoubleRegister(instr->result());
3239 Operand operand = key_is_constant
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003240 ? Operand(constant_key << element_size_shift)
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003241 : Operand(key, LSL, shift_size);
Ben Murdoch257744e2011-11-30 15:57:28 +00003242 __ add(scratch0(), external_pointer, operand);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003243 if (elements_kind == EXTERNAL_FLOAT32_ELEMENTS ||
3244 elements_kind == FLOAT32_ELEMENTS) {
3245 __ vldr(double_scratch0().low(), scratch0(), base_offset);
3246 __ vcvt_f64_f32(result, double_scratch0().low());
Ben Murdoch589d6972011-11-30 16:04:58 +00003247 } else { // i.e. elements_kind == EXTERNAL_DOUBLE_ELEMENTS
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003248 __ vldr(result, scratch0(), base_offset);
Ben Murdoch257744e2011-11-30 15:57:28 +00003249 }
Ben Murdoch8b112d22011-06-08 16:22:53 +01003250 } else {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003251 Register result = ToRegister(instr->result());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003252 MemOperand mem_operand = PrepareKeyedOperand(
3253 key, external_pointer, key_is_constant, constant_key,
3254 element_size_shift, shift_size, base_offset);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003255 switch (elements_kind) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003256 case EXTERNAL_INT8_ELEMENTS:
3257 case INT8_ELEMENTS:
Ben Murdoch257744e2011-11-30 15:57:28 +00003258 __ ldrsb(result, mem_operand);
Ben Murdoch8b112d22011-06-08 16:22:53 +01003259 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003260 case EXTERNAL_UINT8_CLAMPED_ELEMENTS:
3261 case EXTERNAL_UINT8_ELEMENTS:
3262 case UINT8_ELEMENTS:
3263 case UINT8_CLAMPED_ELEMENTS:
Ben Murdoch257744e2011-11-30 15:57:28 +00003264 __ ldrb(result, mem_operand);
Ben Murdoch8b112d22011-06-08 16:22:53 +01003265 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003266 case EXTERNAL_INT16_ELEMENTS:
3267 case INT16_ELEMENTS:
Ben Murdoch257744e2011-11-30 15:57:28 +00003268 __ ldrsh(result, mem_operand);
Ben Murdoch8b112d22011-06-08 16:22:53 +01003269 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003270 case EXTERNAL_UINT16_ELEMENTS:
3271 case UINT16_ELEMENTS:
Ben Murdoch257744e2011-11-30 15:57:28 +00003272 __ ldrh(result, mem_operand);
Ben Murdoch8b112d22011-06-08 16:22:53 +01003273 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003274 case EXTERNAL_INT32_ELEMENTS:
3275 case INT32_ELEMENTS:
Ben Murdoch257744e2011-11-30 15:57:28 +00003276 __ ldr(result, mem_operand);
Ben Murdoch8b112d22011-06-08 16:22:53 +01003277 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003278 case EXTERNAL_UINT32_ELEMENTS:
3279 case UINT32_ELEMENTS:
Ben Murdoch257744e2011-11-30 15:57:28 +00003280 __ ldr(result, mem_operand);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003281 if (!instr->hydrogen()->CheckFlag(HInstruction::kUint32)) {
3282 __ cmp(result, Operand(0x80000000));
3283 DeoptimizeIf(cs, instr);
3284 }
Ben Murdoch8b112d22011-06-08 16:22:53 +01003285 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003286 case FLOAT32_ELEMENTS:
3287 case FLOAT64_ELEMENTS:
3288 case EXTERNAL_FLOAT32_ELEMENTS:
3289 case EXTERNAL_FLOAT64_ELEMENTS:
3290 case FAST_HOLEY_DOUBLE_ELEMENTS:
3291 case FAST_HOLEY_ELEMENTS:
3292 case FAST_HOLEY_SMI_ELEMENTS:
Ben Murdoch589d6972011-11-30 16:04:58 +00003293 case FAST_DOUBLE_ELEMENTS:
3294 case FAST_ELEMENTS:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003295 case FAST_SMI_ELEMENTS:
Ben Murdoch589d6972011-11-30 16:04:58 +00003296 case DICTIONARY_ELEMENTS:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003297 case SLOPPY_ARGUMENTS_ELEMENTS:
Ben Murdoch8b112d22011-06-08 16:22:53 +01003298 UNREACHABLE();
3299 break;
3300 }
3301 }
Steve Block1e0659c2011-05-24 12:43:12 +01003302}
3303
3304
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003305void LCodeGen::DoLoadKeyedFixedDoubleArray(LLoadKeyed* instr) {
3306 Register elements = ToRegister(instr->elements());
3307 bool key_is_constant = instr->key()->IsConstantOperand();
3308 Register key = no_reg;
3309 DwVfpRegister result = ToDoubleRegister(instr->result());
3310 Register scratch = scratch0();
Ben Murdochb0fe1622011-05-05 13:52:32 +01003311
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003312 int element_size_shift = ElementsKindToShiftSize(FAST_DOUBLE_ELEMENTS);
3313
3314 int base_offset = instr->base_offset();
3315 if (key_is_constant) {
3316 int constant_key = ToInteger32(LConstantOperand::cast(instr->key()));
3317 if (constant_key & 0xF0000000) {
3318 Abort(kArrayIndexConstantValueTooBig);
3319 }
3320 base_offset += constant_key * kDoubleSize;
3321 }
3322 __ add(scratch, elements, Operand(base_offset));
3323
3324 if (!key_is_constant) {
3325 key = ToRegister(instr->key());
3326 int shift_size = (instr->hydrogen()->key()->representation().IsSmi())
3327 ? (element_size_shift - kSmiTagSize) : element_size_shift;
3328 __ add(scratch, scratch, Operand(key, LSL, shift_size));
3329 }
3330
3331 __ vldr(result, scratch, 0);
3332
3333 if (instr->hydrogen()->RequiresHoleCheck()) {
3334 __ ldr(scratch, MemOperand(scratch, sizeof(kHoleNanLower32)));
3335 __ cmp(scratch, Operand(kHoleNanUpper32));
3336 DeoptimizeIf(eq, instr);
3337 }
3338}
3339
3340
3341void LCodeGen::DoLoadKeyedFixedArray(LLoadKeyed* instr) {
3342 Register elements = ToRegister(instr->elements());
3343 Register result = ToRegister(instr->result());
3344 Register scratch = scratch0();
3345 Register store_base = scratch;
3346 int offset = instr->base_offset();
3347
3348 if (instr->key()->IsConstantOperand()) {
3349 LConstantOperand* const_operand = LConstantOperand::cast(instr->key());
3350 offset += ToInteger32(const_operand) * kPointerSize;
3351 store_base = elements;
3352 } else {
3353 Register key = ToRegister(instr->key());
3354 // Even though the HLoadKeyed instruction forces the input
3355 // representation for the key to be an integer, the input gets replaced
3356 // during bound check elimination with the index argument to the bounds
3357 // check, which can be tagged, so that case must be handled here, too.
3358 if (instr->hydrogen()->key()->representation().IsSmi()) {
3359 __ add(scratch, elements, Operand::PointerOffsetFromSmiKey(key));
3360 } else {
3361 __ add(scratch, elements, Operand(key, LSL, kPointerSizeLog2));
3362 }
3363 }
3364 __ ldr(result, MemOperand(store_base, offset));
3365
3366 // Check for the hole value.
3367 if (instr->hydrogen()->RequiresHoleCheck()) {
3368 if (IsFastSmiElementsKind(instr->hydrogen()->elements_kind())) {
3369 __ SmiTst(result);
3370 DeoptimizeIf(ne, instr);
3371 } else {
3372 __ LoadRoot(scratch, Heap::kTheHoleValueRootIndex);
3373 __ cmp(result, scratch);
3374 DeoptimizeIf(eq, instr);
3375 }
3376 }
3377}
3378
3379
3380void LCodeGen::DoLoadKeyed(LLoadKeyed* instr) {
3381 if (instr->is_typed_elements()) {
3382 DoLoadKeyedExternalArray(instr);
3383 } else if (instr->hydrogen()->representation().IsDouble()) {
3384 DoLoadKeyedFixedDoubleArray(instr);
3385 } else {
3386 DoLoadKeyedFixedArray(instr);
3387 }
3388}
3389
3390
3391MemOperand LCodeGen::PrepareKeyedOperand(Register key,
3392 Register base,
3393 bool key_is_constant,
3394 int constant_key,
3395 int element_size,
3396 int shift_size,
3397 int base_offset) {
3398 if (key_is_constant) {
3399 return MemOperand(base, (constant_key << element_size) + base_offset);
3400 }
3401
3402 if (base_offset == 0) {
3403 if (shift_size >= 0) {
3404 return MemOperand(base, key, LSL, shift_size);
3405 } else {
3406 DCHECK_EQ(-1, shift_size);
3407 return MemOperand(base, key, LSR, 1);
3408 }
3409 }
3410
3411 if (shift_size >= 0) {
3412 __ add(scratch0(), base, Operand(key, LSL, shift_size));
3413 return MemOperand(scratch0(), base_offset);
3414 } else {
3415 DCHECK_EQ(-1, shift_size);
3416 __ add(scratch0(), base, Operand(key, ASR, 1));
3417 return MemOperand(scratch0(), base_offset);
3418 }
3419}
3420
3421
3422void LCodeGen::DoLoadKeyedGeneric(LLoadKeyedGeneric* instr) {
3423 DCHECK(ToRegister(instr->context()).is(cp));
3424 DCHECK(ToRegister(instr->object()).is(LoadDescriptor::ReceiverRegister()));
3425 DCHECK(ToRegister(instr->key()).is(LoadDescriptor::NameRegister()));
3426
3427 if (FLAG_vector_ics) {
3428 EmitVectorLoadICRegisters<LLoadKeyedGeneric>(instr);
3429 }
3430
3431 Handle<Code> ic = CodeFactory::KeyedLoadIC(isolate()).code();
3432 CallCode(ic, RelocInfo::CODE_TARGET, instr, NEVER_INLINE_TARGET_ADDRESS);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003433}
3434
3435
3436void LCodeGen::DoArgumentsElements(LArgumentsElements* instr) {
Ben Murdoch086aeea2011-05-13 15:57:08 +01003437 Register scratch = scratch0();
3438 Register result = ToRegister(instr->result());
3439
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003440 if (instr->hydrogen()->from_inlined()) {
3441 __ sub(result, sp, Operand(2 * kPointerSize));
3442 } else {
3443 // Check if the calling frame is an arguments adaptor frame.
3444 Label done, adapted;
3445 __ ldr(scratch, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3446 __ ldr(result, MemOperand(scratch, StandardFrameConstants::kContextOffset));
3447 __ cmp(result, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
Ben Murdoch086aeea2011-05-13 15:57:08 +01003448
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003449 // Result is the frame pointer for the frame if not adapted and for the real
3450 // frame below the adaptor frame if adapted.
3451 __ mov(result, fp, LeaveCC, ne);
3452 __ mov(result, scratch, LeaveCC, eq);
3453 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01003454}
3455
3456
3457void LCodeGen::DoArgumentsLength(LArgumentsLength* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003458 Register elem = ToRegister(instr->elements());
Ben Murdoch086aeea2011-05-13 15:57:08 +01003459 Register result = ToRegister(instr->result());
3460
3461 Label done;
3462
3463 // If no arguments adaptor frame the number of arguments is fixed.
3464 __ cmp(fp, elem);
3465 __ mov(result, Operand(scope()->num_parameters()));
3466 __ b(eq, &done);
3467
3468 // Arguments adaptor frame present. Get argument length from there.
3469 __ ldr(result, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3470 __ ldr(result,
3471 MemOperand(result, ArgumentsAdaptorFrameConstants::kLengthOffset));
3472 __ SmiUntag(result);
3473
3474 // Argument length is in result register.
3475 __ bind(&done);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003476}
3477
3478
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003479void LCodeGen::DoWrapReceiver(LWrapReceiver* instr) {
Ben Murdochb8e0da22011-05-16 14:20:40 +01003480 Register receiver = ToRegister(instr->receiver());
3481 Register function = ToRegister(instr->function());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003482 Register result = ToRegister(instr->result());
Steve Block1e0659c2011-05-24 12:43:12 +01003483 Register scratch = scratch0();
Ben Murdochb8e0da22011-05-16 14:20:40 +01003484
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003485 // If the receiver is null or undefined, we have to pass the global
3486 // object as a receiver to normal functions. Values have to be
3487 // passed unchanged to builtins and strict-mode functions.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003488 Label global_object, result_in_receiver;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003489
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003490 if (!instr->hydrogen()->known_function()) {
3491 // Do not transform the receiver to object for strict mode
3492 // functions.
3493 __ ldr(scratch,
3494 FieldMemOperand(function, JSFunction::kSharedFunctionInfoOffset));
3495 __ ldr(scratch,
3496 FieldMemOperand(scratch, SharedFunctionInfo::kCompilerHintsOffset));
3497 int mask = 1 << (SharedFunctionInfo::kStrictModeFunction + kSmiTagSize);
3498 __ tst(scratch, Operand(mask));
3499 __ b(ne, &result_in_receiver);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003500
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003501 // Do not transform the receiver to object for builtins.
3502 __ tst(scratch, Operand(1 << (SharedFunctionInfo::kNative + kSmiTagSize)));
3503 __ b(ne, &result_in_receiver);
3504 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003505
3506 // Normal function. Replace undefined or null with global receiver.
Steve Block1e0659c2011-05-24 12:43:12 +01003507 __ LoadRoot(scratch, Heap::kNullValueRootIndex);
3508 __ cmp(receiver, scratch);
3509 __ b(eq, &global_object);
3510 __ LoadRoot(scratch, Heap::kUndefinedValueRootIndex);
3511 __ cmp(receiver, scratch);
3512 __ b(eq, &global_object);
3513
3514 // Deoptimize if the receiver is not a JS object.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003515 __ SmiTst(receiver);
3516 DeoptimizeIf(eq, instr);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003517 __ CompareObjectType(receiver, scratch, scratch, FIRST_SPEC_OBJECT_TYPE);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003518 DeoptimizeIf(lt, instr);
Steve Block1e0659c2011-05-24 12:43:12 +01003519
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003520 __ b(&result_in_receiver);
Steve Block1e0659c2011-05-24 12:43:12 +01003521 __ bind(&global_object);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003522 __ ldr(result, FieldMemOperand(function, JSFunction::kContextOffset));
3523 __ ldr(result,
3524 ContextOperand(result, Context::GLOBAL_OBJECT_INDEX));
3525 __ ldr(result, FieldMemOperand(result, GlobalObject::kGlobalProxyOffset));
3526
3527 if (result.is(receiver)) {
3528 __ bind(&result_in_receiver);
3529 } else {
3530 Label result_ok;
3531 __ b(&result_ok);
3532 __ bind(&result_in_receiver);
3533 __ mov(result, receiver);
3534 __ bind(&result_ok);
3535 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003536}
3537
3538
3539void LCodeGen::DoApplyArguments(LApplyArguments* instr) {
3540 Register receiver = ToRegister(instr->receiver());
3541 Register function = ToRegister(instr->function());
3542 Register length = ToRegister(instr->length());
3543 Register elements = ToRegister(instr->elements());
3544 Register scratch = scratch0();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003545 DCHECK(receiver.is(r0)); // Used for parameter count.
3546 DCHECK(function.is(r1)); // Required by InvokeFunction.
3547 DCHECK(ToRegister(instr->result()).is(r0));
Ben Murdochb8e0da22011-05-16 14:20:40 +01003548
3549 // Copy the arguments to this function possibly from the
3550 // adaptor frame below it.
3551 const uint32_t kArgumentsLimit = 1 * KB;
3552 __ cmp(length, Operand(kArgumentsLimit));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003553 DeoptimizeIf(hi, instr);
Ben Murdochb8e0da22011-05-16 14:20:40 +01003554
3555 // Push the receiver and use the register to keep the original
3556 // number of arguments.
3557 __ push(receiver);
3558 __ mov(receiver, length);
3559 // The arguments are at a one pointer size offset from elements.
3560 __ add(elements, elements, Operand(1 * kPointerSize));
3561
3562 // Loop through the arguments pushing them onto the execution
3563 // stack.
Steve Block1e0659c2011-05-24 12:43:12 +01003564 Label invoke, loop;
Ben Murdochb8e0da22011-05-16 14:20:40 +01003565 // length is a small non-negative integer, due to the test above.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003566 __ cmp(length, Operand::Zero());
Ben Murdochb8e0da22011-05-16 14:20:40 +01003567 __ b(eq, &invoke);
3568 __ bind(&loop);
3569 __ ldr(scratch, MemOperand(elements, length, LSL, 2));
3570 __ push(scratch);
3571 __ sub(length, length, Operand(1), SetCC);
3572 __ b(ne, &loop);
3573
3574 __ bind(&invoke);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003575 DCHECK(instr->HasPointerMap());
Steve Block1e0659c2011-05-24 12:43:12 +01003576 LPointerMap* pointers = instr->pointer_map();
Ben Murdoch2b4ba112012-01-20 14:57:15 +00003577 SafepointGenerator safepoint_generator(
3578 this, pointers, Safepoint::kLazyDeopt);
Steve Block1e0659c2011-05-24 12:43:12 +01003579 // The number of arguments is stored in receiver which is r0, as expected
3580 // by InvokeFunction.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003581 ParameterCount actual(receiver);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003582 __ InvokeFunction(function, actual, CALL_FUNCTION, safepoint_generator);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003583}
3584
3585
3586void LCodeGen::DoPushArgument(LPushArgument* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003587 LOperand* argument = instr->value();
Ben Murdochb0fe1622011-05-05 13:52:32 +01003588 if (argument->IsDoubleRegister() || argument->IsDoubleStackSlot()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003589 Abort(kDoPushArgumentNotImplementedForDoubleType);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003590 } else {
3591 Register argument_reg = EmitLoadRegister(argument, ip);
3592 __ push(argument_reg);
3593 }
3594}
3595
3596
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003597void LCodeGen::DoDrop(LDrop* instr) {
3598 __ Drop(instr->count());
3599}
3600
3601
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003602void LCodeGen::DoThisFunction(LThisFunction* instr) {
3603 Register result = ToRegister(instr->result());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003604 __ ldr(result, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003605}
3606
3607
Steve Block1e0659c2011-05-24 12:43:12 +01003608void LCodeGen::DoContext(LContext* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003609 // If there is a non-return use, the context must be moved to a register.
Steve Block1e0659c2011-05-24 12:43:12 +01003610 Register result = ToRegister(instr->result());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003611 if (info()->IsOptimizing()) {
3612 __ ldr(result, MemOperand(fp, StandardFrameConstants::kContextOffset));
3613 } else {
3614 // If there is no frame, the context must be in cp.
3615 DCHECK(result.is(cp));
3616 }
Steve Block1e0659c2011-05-24 12:43:12 +01003617}
3618
3619
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003620void LCodeGen::DoDeclareGlobals(LDeclareGlobals* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003621 DCHECK(ToRegister(instr->context()).is(cp));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003622 __ push(cp); // The context is the first argument.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003623 __ Move(scratch0(), instr->hydrogen()->pairs());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003624 __ push(scratch0());
3625 __ mov(scratch0(), Operand(Smi::FromInt(instr->hydrogen()->flags())));
3626 __ push(scratch0());
3627 CallRuntime(Runtime::kDeclareGlobals, 3, instr);
3628}
3629
3630
Ben Murdochb0fe1622011-05-05 13:52:32 +01003631void LCodeGen::CallKnownFunction(Handle<JSFunction> function,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003632 int formal_parameter_count,
Ben Murdochb0fe1622011-05-05 13:52:32 +01003633 int arity,
Ben Murdoch257744e2011-11-30 15:57:28 +00003634 LInstruction* instr,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003635 R1State r1_state) {
3636 bool dont_adapt_arguments =
3637 formal_parameter_count == SharedFunctionInfo::kDontAdaptArgumentsSentinel;
3638 bool can_invoke_directly =
3639 dont_adapt_arguments || formal_parameter_count == arity;
Ben Murdochb0fe1622011-05-05 13:52:32 +01003640
3641 LPointerMap* pointers = instr->pointer_map();
Ben Murdochb0fe1622011-05-05 13:52:32 +01003642
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003643 if (can_invoke_directly) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003644 if (r1_state == R1_UNINITIALIZED) {
3645 __ Move(r1, function);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003646 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01003647
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003648 // Change context.
3649 __ ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
3650
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003651 // Set r0 to arguments count if adaption is not needed. Assumes that r0
3652 // is available to write to at this point.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003653 if (dont_adapt_arguments) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003654 __ mov(r0, Operand(arity));
3655 }
3656
3657 // Invoke function.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003658 __ ldr(ip, FieldMemOperand(r1, JSFunction::kCodeEntryOffset));
3659 __ Call(ip);
3660
3661 // Set up deoptimization.
3662 RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT);
3663 } else {
3664 SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
3665 ParameterCount count(arity);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003666 ParameterCount expected(formal_parameter_count);
3667 __ InvokeFunction(function, expected, count, CALL_FUNCTION, generator);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003668 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01003669}
3670
3671
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003672void LCodeGen::DoDeferredMathAbsTaggedHeapNumber(LMathAbs* instr) {
3673 DCHECK(instr->context() != NULL);
3674 DCHECK(ToRegister(instr->context()).is(cp));
3675 Register input = ToRegister(instr->value());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003676 Register result = ToRegister(instr->result());
Steve Block1e0659c2011-05-24 12:43:12 +01003677 Register scratch = scratch0();
3678
3679 // Deoptimize if not a heap number.
3680 __ ldr(scratch, FieldMemOperand(input, HeapObject::kMapOffset));
3681 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
3682 __ cmp(scratch, Operand(ip));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003683 DeoptimizeIf(ne, instr);
Steve Block1e0659c2011-05-24 12:43:12 +01003684
3685 Label done;
3686 Register exponent = scratch0();
3687 scratch = no_reg;
3688 __ ldr(exponent, FieldMemOperand(input, HeapNumber::kExponentOffset));
3689 // Check the sign of the argument. If the argument is positive, just
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003690 // return it.
Steve Block1e0659c2011-05-24 12:43:12 +01003691 __ tst(exponent, Operand(HeapNumber::kSignMask));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003692 // Move the input to the result if necessary.
3693 __ Move(result, input);
Steve Block1e0659c2011-05-24 12:43:12 +01003694 __ b(eq, &done);
3695
3696 // Input is negative. Reverse its sign.
3697 // Preserve the value of all registers.
Ben Murdoch8b112d22011-06-08 16:22:53 +01003698 {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003699 PushSafepointRegistersScope scope(this);
Steve Block1e0659c2011-05-24 12:43:12 +01003700
Ben Murdoch8b112d22011-06-08 16:22:53 +01003701 // Registers were saved at the safepoint, so we can use
3702 // many scratch registers.
3703 Register tmp1 = input.is(r1) ? r0 : r1;
3704 Register tmp2 = input.is(r2) ? r0 : r2;
3705 Register tmp3 = input.is(r3) ? r0 : r3;
3706 Register tmp4 = input.is(r4) ? r0 : r4;
Steve Block1e0659c2011-05-24 12:43:12 +01003707
Ben Murdoch8b112d22011-06-08 16:22:53 +01003708 // exponent: floating point exponent value.
Steve Block1e0659c2011-05-24 12:43:12 +01003709
Ben Murdoch8b112d22011-06-08 16:22:53 +01003710 Label allocated, slow;
3711 __ LoadRoot(tmp4, Heap::kHeapNumberMapRootIndex);
3712 __ AllocateHeapNumber(tmp1, tmp2, tmp3, tmp4, &slow);
3713 __ b(&allocated);
Steve Block1e0659c2011-05-24 12:43:12 +01003714
Ben Murdoch8b112d22011-06-08 16:22:53 +01003715 // Slow case: Call the runtime system to do the number allocation.
3716 __ bind(&slow);
Steve Block1e0659c2011-05-24 12:43:12 +01003717
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003718 CallRuntimeFromDeferred(Runtime::kAllocateHeapNumber, 0, instr,
3719 instr->context());
Ben Murdoch8b112d22011-06-08 16:22:53 +01003720 // Set the pointer to the new heap number in tmp.
3721 if (!tmp1.is(r0)) __ mov(tmp1, Operand(r0));
3722 // Restore input_reg after call to runtime.
3723 __ LoadFromSafepointRegisterSlot(input, input);
3724 __ ldr(exponent, FieldMemOperand(input, HeapNumber::kExponentOffset));
Steve Block1e0659c2011-05-24 12:43:12 +01003725
Ben Murdoch8b112d22011-06-08 16:22:53 +01003726 __ bind(&allocated);
3727 // exponent: floating point exponent value.
3728 // tmp1: allocated heap number.
3729 __ bic(exponent, exponent, Operand(HeapNumber::kSignMask));
3730 __ str(exponent, FieldMemOperand(tmp1, HeapNumber::kExponentOffset));
3731 __ ldr(tmp2, FieldMemOperand(input, HeapNumber::kMantissaOffset));
3732 __ str(tmp2, FieldMemOperand(tmp1, HeapNumber::kMantissaOffset));
Steve Block1e0659c2011-05-24 12:43:12 +01003733
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003734 __ StoreToSafepointRegisterSlot(tmp1, result);
Ben Murdoch8b112d22011-06-08 16:22:53 +01003735 }
Steve Block1e0659c2011-05-24 12:43:12 +01003736
3737 __ bind(&done);
3738}
3739
3740
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003741void LCodeGen::EmitIntegerMathAbs(LMathAbs* instr) {
3742 Register input = ToRegister(instr->value());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003743 Register result = ToRegister(instr->result());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003744 __ cmp(input, Operand::Zero());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003745 __ Move(result, input, pl);
Steve Block1e0659c2011-05-24 12:43:12 +01003746 // We can make rsb conditional because the previous cmp instruction
3747 // will clear the V (overflow) flag and rsb won't set this flag
3748 // if input is positive.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003749 __ rsb(result, input, Operand::Zero(), SetCC, mi);
Steve Block1e0659c2011-05-24 12:43:12 +01003750 // Deoptimize on overflow.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003751 DeoptimizeIf(vs, instr);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003752}
3753
3754
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003755void LCodeGen::DoMathAbs(LMathAbs* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01003756 // Class for deferred case.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003757 class DeferredMathAbsTaggedHeapNumber FINAL : public LDeferredCode {
Steve Block1e0659c2011-05-24 12:43:12 +01003758 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003759 DeferredMathAbsTaggedHeapNumber(LCodeGen* codegen, LMathAbs* instr)
Steve Block1e0659c2011-05-24 12:43:12 +01003760 : LDeferredCode(codegen), instr_(instr) { }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003761 virtual void Generate() OVERRIDE {
Steve Block1e0659c2011-05-24 12:43:12 +01003762 codegen()->DoDeferredMathAbsTaggedHeapNumber(instr_);
3763 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003764 virtual LInstruction* instr() OVERRIDE { return instr_; }
Steve Block1e0659c2011-05-24 12:43:12 +01003765 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003766 LMathAbs* instr_;
Steve Block1e0659c2011-05-24 12:43:12 +01003767 };
3768
Steve Block1e0659c2011-05-24 12:43:12 +01003769 Representation r = instr->hydrogen()->value()->representation();
3770 if (r.IsDouble()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003771 DwVfpRegister input = ToDoubleRegister(instr->value());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003772 DwVfpRegister result = ToDoubleRegister(instr->result());
3773 __ vabs(result, input);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003774 } else if (r.IsSmiOrInteger32()) {
Steve Block1e0659c2011-05-24 12:43:12 +01003775 EmitIntegerMathAbs(instr);
3776 } else {
3777 // Representation is tagged.
3778 DeferredMathAbsTaggedHeapNumber* deferred =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003779 new(zone()) DeferredMathAbsTaggedHeapNumber(this, instr);
3780 Register input = ToRegister(instr->value());
Steve Block1e0659c2011-05-24 12:43:12 +01003781 // Smi check.
3782 __ JumpIfNotSmi(input, deferred->entry());
3783 // If smi, handle it directly.
3784 EmitIntegerMathAbs(instr);
3785 __ bind(deferred->exit());
3786 }
3787}
3788
3789
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003790void LCodeGen::DoMathFloor(LMathFloor* instr) {
3791 DwVfpRegister input = ToDoubleRegister(instr->value());
Ben Murdochb8e0da22011-05-16 14:20:40 +01003792 Register result = ToRegister(instr->result());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003793 Register input_high = scratch0();
3794 Label done, exact;
Ben Murdochb8e0da22011-05-16 14:20:40 +01003795
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003796 __ TryInt32Floor(result, input, input_high, double_scratch0(), &done, &exact);
3797 DeoptimizeIf(al, instr);
Ben Murdochb8e0da22011-05-16 14:20:40 +01003798
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003799 __ bind(&exact);
Steve Block44f0eee2011-05-26 01:26:41 +01003800 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3801 // Test for -0.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003802 __ cmp(result, Operand::Zero());
Steve Block44f0eee2011-05-26 01:26:41 +01003803 __ b(ne, &done);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003804 __ cmp(input_high, Operand::Zero());
3805 DeoptimizeIf(mi, instr);
Steve Block44f0eee2011-05-26 01:26:41 +01003806 }
Ben Murdoch257744e2011-11-30 15:57:28 +00003807 __ bind(&done);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003808}
3809
3810
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003811void LCodeGen::DoMathRound(LMathRound* instr) {
3812 DwVfpRegister input = ToDoubleRegister(instr->value());
3813 Register result = ToRegister(instr->result());
3814 DwVfpRegister double_scratch1 = ToDoubleRegister(instr->temp());
3815 DwVfpRegister input_plus_dot_five = double_scratch1;
3816 Register input_high = scratch0();
3817 DwVfpRegister dot_five = double_scratch0();
3818 Label convert, done;
3819
3820 __ Vmov(dot_five, 0.5, scratch0());
3821 __ vabs(double_scratch1, input);
3822 __ VFPCompareAndSetFlags(double_scratch1, dot_five);
3823 // If input is in [-0.5, -0], the result is -0.
3824 // If input is in [+0, +0.5[, the result is +0.
3825 // If the input is +0.5, the result is 1.
3826 __ b(hi, &convert); // Out of [-0.5, +0.5].
3827 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3828 __ VmovHigh(input_high, input);
3829 __ cmp(input_high, Operand::Zero());
3830 DeoptimizeIf(mi, instr); // [-0.5, -0].
3831 }
3832 __ VFPCompareAndSetFlags(input, dot_five);
3833 __ mov(result, Operand(1), LeaveCC, eq); // +0.5.
3834 // Remaining cases: [+0, +0.5[ or [-0.5, +0.5[, depending on
3835 // flag kBailoutOnMinusZero.
3836 __ mov(result, Operand::Zero(), LeaveCC, ne);
3837 __ b(&done);
3838
3839 __ bind(&convert);
3840 __ vadd(input_plus_dot_five, input, dot_five);
3841 // Reuse dot_five (double_scratch0) as we no longer need this value.
3842 __ TryInt32Floor(result, input_plus_dot_five, input_high, double_scratch0(),
3843 &done, &done);
3844 DeoptimizeIf(al, instr);
3845 __ bind(&done);
3846}
3847
3848
3849void LCodeGen::DoMathFround(LMathFround* instr) {
3850 DwVfpRegister input_reg = ToDoubleRegister(instr->value());
3851 DwVfpRegister output_reg = ToDoubleRegister(instr->result());
3852 LowDwVfpRegister scratch = double_scratch0();
3853 __ vcvt_f32_f64(scratch.low(), input_reg);
3854 __ vcvt_f64_f32(output_reg, scratch.low());
3855}
3856
3857
3858void LCodeGen::DoMathSqrt(LMathSqrt* instr) {
3859 DwVfpRegister input = ToDoubleRegister(instr->value());
3860 DwVfpRegister result = ToDoubleRegister(instr->result());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003861 __ vsqrt(result, input);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003862}
3863
3864
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003865void LCodeGen::DoMathPowHalf(LMathPowHalf* instr) {
3866 DwVfpRegister input = ToDoubleRegister(instr->value());
3867 DwVfpRegister result = ToDoubleRegister(instr->result());
3868 DwVfpRegister temp = double_scratch0();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003869
3870 // Note that according to ECMA-262 15.8.2.13:
3871 // Math.pow(-Infinity, 0.5) == Infinity
3872 // Math.sqrt(-Infinity) == NaN
3873 Label done;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003874 __ vmov(temp, -V8_INFINITY, scratch0());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003875 __ VFPCompareAndSetFlags(input, temp);
3876 __ vneg(result, temp, eq);
3877 __ b(&done, eq);
3878
Steve Block44f0eee2011-05-26 01:26:41 +01003879 // Add +0 to convert -0 to +0.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003880 __ vadd(result, input, kDoubleRegZero);
3881 __ vsqrt(result, result);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003882 __ bind(&done);
Steve Block44f0eee2011-05-26 01:26:41 +01003883}
3884
3885
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003886void LCodeGen::DoPower(LPower* instr) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003887 Representation exponent_type = instr->hydrogen()->right()->representation();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003888 // Having marked this as a call, we can use any registers.
3889 // Just make sure that the input/output registers are the expected ones.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003890 Register tagged_exponent = MathPowTaggedDescriptor::exponent();
3891 DCHECK(!instr->right()->IsDoubleRegister() ||
3892 ToDoubleRegister(instr->right()).is(d1));
3893 DCHECK(!instr->right()->IsRegister() ||
3894 ToRegister(instr->right()).is(tagged_exponent));
3895 DCHECK(ToDoubleRegister(instr->left()).is(d0));
3896 DCHECK(ToDoubleRegister(instr->result()).is(d2));
Ben Murdoch85b71792012-04-11 18:30:58 +01003897
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003898 if (exponent_type.IsSmi()) {
3899 MathPowStub stub(isolate(), MathPowStub::TAGGED);
3900 __ CallStub(&stub);
3901 } else if (exponent_type.IsTagged()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003902 Label no_deopt;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003903 __ JumpIfSmi(tagged_exponent, &no_deopt);
3904 DCHECK(!r6.is(tagged_exponent));
3905 __ ldr(r6, FieldMemOperand(tagged_exponent, HeapObject::kMapOffset));
Ben Murdoch85b71792012-04-11 18:30:58 +01003906 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003907 __ cmp(r6, Operand(ip));
3908 DeoptimizeIf(ne, instr);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003909 __ bind(&no_deopt);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003910 MathPowStub stub(isolate(), MathPowStub::TAGGED);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003911 __ CallStub(&stub);
3912 } else if (exponent_type.IsInteger32()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003913 MathPowStub stub(isolate(), MathPowStub::INTEGER);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003914 __ CallStub(&stub);
3915 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003916 DCHECK(exponent_type.IsDouble());
3917 MathPowStub stub(isolate(), MathPowStub::DOUBLE);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003918 __ CallStub(&stub);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003919 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003920}
3921
3922
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003923void LCodeGen::DoMathExp(LMathExp* instr) {
3924 DwVfpRegister input = ToDoubleRegister(instr->value());
3925 DwVfpRegister result = ToDoubleRegister(instr->result());
3926 DwVfpRegister double_scratch1 = ToDoubleRegister(instr->double_temp());
3927 DwVfpRegister double_scratch2 = double_scratch0();
3928 Register temp1 = ToRegister(instr->temp1());
3929 Register temp2 = ToRegister(instr->temp2());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003930
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003931 MathExpGenerator::EmitMathExp(
3932 masm(), input, result, double_scratch1, double_scratch2,
3933 temp1, temp2, scratch0());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003934}
3935
3936
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003937void LCodeGen::DoMathLog(LMathLog* instr) {
3938 __ PrepareCallCFunction(0, 1, scratch0());
3939 __ MovToFloatParameter(ToDoubleRegister(instr->value()));
3940 __ CallCFunction(ExternalReference::math_log_double_function(isolate()),
3941 0, 1);
3942 __ MovFromFloatResult(ToDoubleRegister(instr->result()));
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +01003943}
3944
3945
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003946void LCodeGen::DoMathClz32(LMathClz32* instr) {
3947 Register input = ToRegister(instr->value());
3948 Register result = ToRegister(instr->result());
3949 __ clz(result, input);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003950}
3951
3952
Ben Murdoch257744e2011-11-30 15:57:28 +00003953void LCodeGen::DoInvokeFunction(LInvokeFunction* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003954 DCHECK(ToRegister(instr->context()).is(cp));
3955 DCHECK(ToRegister(instr->function()).is(r1));
3956 DCHECK(instr->HasPointerMap());
3957
3958 Handle<JSFunction> known_function = instr->hydrogen()->known_function();
3959 if (known_function.is_null()) {
3960 LPointerMap* pointers = instr->pointer_map();
3961 SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
3962 ParameterCount count(instr->arity());
3963 __ InvokeFunction(r1, count, CALL_FUNCTION, generator);
3964 } else {
3965 CallKnownFunction(known_function,
3966 instr->hydrogen()->formal_parameter_count(),
3967 instr->arity(),
3968 instr,
3969 R1_CONTAINS_TARGET);
3970 }
3971}
3972
3973
3974void LCodeGen::DoTailCallThroughMegamorphicCache(
3975 LTailCallThroughMegamorphicCache* instr) {
3976 Register receiver = ToRegister(instr->receiver());
3977 Register name = ToRegister(instr->name());
3978 DCHECK(receiver.is(LoadDescriptor::ReceiverRegister()));
3979 DCHECK(name.is(LoadDescriptor::NameRegister()));
3980 DCHECK(receiver.is(r1));
3981 DCHECK(name.is(r2));
3982
3983 Register scratch = r3;
3984 Register extra = r4;
3985 Register extra2 = r5;
3986 Register extra3 = r6;
3987
3988 // Important for the tail-call.
3989 bool must_teardown_frame = NeedsEagerFrame();
3990
3991 // The probe will tail call to a handler if found.
3992 isolate()->stub_cache()->GenerateProbe(masm(), instr->hydrogen()->flags(),
3993 must_teardown_frame, receiver, name,
3994 scratch, extra, extra2, extra3);
3995
3996 // Tail call to miss if we ended up here.
3997 if (must_teardown_frame) __ LeaveFrame(StackFrame::INTERNAL);
3998 LoadIC::GenerateMiss(masm());
3999}
4000
4001
4002void LCodeGen::DoCallWithDescriptor(LCallWithDescriptor* instr) {
4003 DCHECK(ToRegister(instr->result()).is(r0));
4004
Ben Murdoch257744e2011-11-30 15:57:28 +00004005 LPointerMap* pointers = instr->pointer_map();
Ben Murdoch2b4ba112012-01-20 14:57:15 +00004006 SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004007
4008 if (instr->target()->IsConstantOperand()) {
4009 LConstantOperand* target = LConstantOperand::cast(instr->target());
4010 Handle<Code> code = Handle<Code>::cast(ToHandle(target));
4011 generator.BeforeCall(__ CallSize(code, RelocInfo::CODE_TARGET));
4012 PlatformInterfaceDescriptor* call_descriptor =
4013 instr->descriptor().platform_specific_descriptor();
4014 __ Call(code, RelocInfo::CODE_TARGET, TypeFeedbackId::None(), al,
4015 call_descriptor->storage_mode());
4016 } else {
4017 DCHECK(instr->target()->IsRegister());
4018 Register target = ToRegister(instr->target());
4019 generator.BeforeCall(__ CallSize(target));
4020 // Make sure we don't emit any additional entries in the constant pool
4021 // before the call to ensure that the CallCodeSize() calculated the correct
4022 // number of instructions for the constant pool load.
4023 {
4024 ConstantPoolUnavailableScope constant_pool_unavailable(masm_);
4025 __ add(target, target, Operand(Code::kHeaderSize - kHeapObjectTag));
4026 }
4027 __ Call(target);
4028 }
4029 generator.AfterCall();
Ben Murdoch257744e2011-11-30 15:57:28 +00004030}
4031
4032
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004033void LCodeGen::DoCallJSFunction(LCallJSFunction* instr) {
4034 DCHECK(ToRegister(instr->function()).is(r1));
4035 DCHECK(ToRegister(instr->result()).is(r0));
Ben Murdoch086aeea2011-05-13 15:57:08 +01004036
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004037 if (instr->hydrogen()->pass_argument_count()) {
4038 __ mov(r0, Operand(instr->arity()));
4039 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01004040
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004041 // Change context.
4042 __ ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
Ben Murdochb0fe1622011-05-05 13:52:32 +01004043
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004044 // Load the code entry address
4045 __ ldr(ip, FieldMemOperand(r1, JSFunction::kCodeEntryOffset));
4046 __ Call(ip);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004047
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004048 RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004049}
4050
4051
4052void LCodeGen::DoCallFunction(LCallFunction* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004053 DCHECK(ToRegister(instr->context()).is(cp));
4054 DCHECK(ToRegister(instr->function()).is(r1));
4055 DCHECK(ToRegister(instr->result()).is(r0));
Steve Block9fac8402011-05-12 15:51:54 +01004056
4057 int arity = instr->arity();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004058 CallFunctionStub stub(isolate(), arity, instr->hydrogen()->function_flags());
Steve Block9fac8402011-05-12 15:51:54 +01004059 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004060}
4061
4062
4063void LCodeGen::DoCallNew(LCallNew* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004064 DCHECK(ToRegister(instr->context()).is(cp));
4065 DCHECK(ToRegister(instr->constructor()).is(r1));
4066 DCHECK(ToRegister(instr->result()).is(r0));
Ben Murdochb0fe1622011-05-05 13:52:32 +01004067
Ben Murdochb0fe1622011-05-05 13:52:32 +01004068 __ mov(r0, Operand(instr->arity()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004069 // No cell in r2 for construct type feedback in optimized code
4070 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
4071 CallConstructStub stub(isolate(), NO_CALL_CONSTRUCTOR_FLAGS);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004072 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004073}
4074
4075
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004076void LCodeGen::DoCallNewArray(LCallNewArray* instr) {
4077 DCHECK(ToRegister(instr->context()).is(cp));
4078 DCHECK(ToRegister(instr->constructor()).is(r1));
4079 DCHECK(ToRegister(instr->result()).is(r0));
4080
4081 __ mov(r0, Operand(instr->arity()));
4082 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
4083 ElementsKind kind = instr->hydrogen()->elements_kind();
4084 AllocationSiteOverrideMode override_mode =
4085 (AllocationSite::GetMode(kind) == TRACK_ALLOCATION_SITE)
4086 ? DISABLE_ALLOCATION_SITES
4087 : DONT_OVERRIDE;
4088
4089 if (instr->arity() == 0) {
4090 ArrayNoArgumentConstructorStub stub(isolate(), kind, override_mode);
4091 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
4092 } else if (instr->arity() == 1) {
4093 Label done;
4094 if (IsFastPackedElementsKind(kind)) {
4095 Label packed_case;
4096 // We might need a change here
4097 // look at the first argument
4098 __ ldr(r5, MemOperand(sp, 0));
4099 __ cmp(r5, Operand::Zero());
4100 __ b(eq, &packed_case);
4101
4102 ElementsKind holey_kind = GetHoleyElementsKind(kind);
4103 ArraySingleArgumentConstructorStub stub(isolate(),
4104 holey_kind,
4105 override_mode);
4106 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
4107 __ jmp(&done);
4108 __ bind(&packed_case);
4109 }
4110
4111 ArraySingleArgumentConstructorStub stub(isolate(), kind, override_mode);
4112 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
4113 __ bind(&done);
4114 } else {
4115 ArrayNArgumentsConstructorStub stub(isolate(), kind, override_mode);
4116 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
4117 }
4118}
4119
4120
Ben Murdochb0fe1622011-05-05 13:52:32 +01004121void LCodeGen::DoCallRuntime(LCallRuntime* instr) {
4122 CallRuntime(instr->function(), instr->arity(), instr);
4123}
4124
4125
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004126void LCodeGen::DoStoreCodeEntry(LStoreCodeEntry* instr) {
4127 Register function = ToRegister(instr->function());
4128 Register code_object = ToRegister(instr->code_object());
4129 __ add(code_object, code_object, Operand(Code::kHeaderSize - kHeapObjectTag));
4130 __ str(code_object,
4131 FieldMemOperand(function, JSFunction::kCodeEntryOffset));
4132}
4133
4134
4135void LCodeGen::DoInnerAllocatedObject(LInnerAllocatedObject* instr) {
4136 Register result = ToRegister(instr->result());
4137 Register base = ToRegister(instr->base_object());
4138 if (instr->offset()->IsConstantOperand()) {
4139 LConstantOperand* offset = LConstantOperand::cast(instr->offset());
4140 __ add(result, base, Operand(ToInteger32(offset)));
4141 } else {
4142 Register offset = ToRegister(instr->offset());
4143 __ add(result, base, offset);
4144 }
4145}
4146
4147
Ben Murdochb0fe1622011-05-05 13:52:32 +01004148void LCodeGen::DoStoreNamedField(LStoreNamedField* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004149 Representation representation = instr->representation();
4150
Ben Murdoch086aeea2011-05-13 15:57:08 +01004151 Register object = ToRegister(instr->object());
Ben Murdoch086aeea2011-05-13 15:57:08 +01004152 Register scratch = scratch0();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004153 HObjectAccess access = instr->hydrogen()->access();
4154 int offset = access.offset();
Ben Murdoch086aeea2011-05-13 15:57:08 +01004155
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004156 if (access.IsExternalMemory()) {
4157 Register value = ToRegister(instr->value());
4158 MemOperand operand = MemOperand(object, offset);
4159 __ Store(value, operand, representation);
4160 return;
4161 }
Ben Murdoch086aeea2011-05-13 15:57:08 +01004162
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004163 __ AssertNotSmi(object);
4164
4165 DCHECK(!representation.IsSmi() ||
4166 !instr->value()->IsConstantOperand() ||
4167 IsSmi(LConstantOperand::cast(instr->value())));
4168 if (representation.IsDouble()) {
4169 DCHECK(access.IsInobject());
4170 DCHECK(!instr->hydrogen()->has_transition());
4171 DCHECK(!instr->hydrogen()->NeedsWriteBarrier());
4172 DwVfpRegister value = ToDoubleRegister(instr->value());
4173 __ vstr(value, FieldMemOperand(object, offset));
4174 return;
4175 }
4176
4177 if (instr->hydrogen()->has_transition()) {
4178 Handle<Map> transition = instr->hydrogen()->transition_map();
4179 AddDeprecationDependency(transition);
4180 __ mov(scratch, Operand(transition));
Ben Murdoch086aeea2011-05-13 15:57:08 +01004181 __ str(scratch, FieldMemOperand(object, HeapObject::kMapOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004182 if (instr->hydrogen()->NeedsWriteBarrierForMap()) {
4183 Register temp = ToRegister(instr->temp());
4184 // Update the write barrier for the map field.
4185 __ RecordWriteForMap(object,
4186 scratch,
4187 temp,
4188 GetLinkRegisterState(),
4189 kSaveFPRegs);
4190 }
Ben Murdoch086aeea2011-05-13 15:57:08 +01004191 }
4192
4193 // Do the store.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004194 Register value = ToRegister(instr->value());
4195 if (access.IsInobject()) {
4196 MemOperand operand = FieldMemOperand(object, offset);
4197 __ Store(value, operand, representation);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004198 if (instr->hydrogen()->NeedsWriteBarrier()) {
Ben Murdoch086aeea2011-05-13 15:57:08 +01004199 // Update the write barrier for the object for in-object properties.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004200 __ RecordWriteField(object,
4201 offset,
4202 value,
4203 scratch,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004204 GetLinkRegisterState(),
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004205 kSaveFPRegs,
4206 EMIT_REMEMBERED_SET,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004207 instr->hydrogen()->SmiCheckForWriteBarrier(),
4208 instr->hydrogen()->PointersToHereCheckForValue());
Ben Murdoch086aeea2011-05-13 15:57:08 +01004209 }
4210 } else {
4211 __ ldr(scratch, FieldMemOperand(object, JSObject::kPropertiesOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004212 MemOperand operand = FieldMemOperand(scratch, offset);
4213 __ Store(value, operand, representation);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004214 if (instr->hydrogen()->NeedsWriteBarrier()) {
Ben Murdoch086aeea2011-05-13 15:57:08 +01004215 // Update the write barrier for the properties array.
4216 // object is used as a scratch register.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004217 __ RecordWriteField(scratch,
4218 offset,
4219 value,
4220 object,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004221 GetLinkRegisterState(),
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004222 kSaveFPRegs,
4223 EMIT_REMEMBERED_SET,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004224 instr->hydrogen()->SmiCheckForWriteBarrier(),
4225 instr->hydrogen()->PointersToHereCheckForValue());
Ben Murdoch086aeea2011-05-13 15:57:08 +01004226 }
4227 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01004228}
4229
4230
4231void LCodeGen::DoStoreNamedGeneric(LStoreNamedGeneric* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004232 DCHECK(ToRegister(instr->context()).is(cp));
4233 DCHECK(ToRegister(instr->object()).is(StoreDescriptor::ReceiverRegister()));
4234 DCHECK(ToRegister(instr->value()).is(StoreDescriptor::ValueRegister()));
Ben Murdochb0fe1622011-05-05 13:52:32 +01004235
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004236 __ mov(StoreDescriptor::NameRegister(), Operand(instr->name()));
4237 Handle<Code> ic = StoreIC::initialize_stub(isolate(), instr->strict_mode());
4238 CallCode(ic, RelocInfo::CODE_TARGET, instr, NEVER_INLINE_TARGET_ADDRESS);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004239}
4240
4241
4242void LCodeGen::DoBoundsCheck(LBoundsCheck* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004243 Condition cc = instr->hydrogen()->allow_equality() ? hi : hs;
4244 if (instr->index()->IsConstantOperand()) {
4245 Operand index = ToOperand(instr->index());
4246 Register length = ToRegister(instr->length());
4247 __ cmp(length, index);
4248 cc = CommuteCondition(cc);
Ben Murdoch086aeea2011-05-13 15:57:08 +01004249 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004250 Register index = ToRegister(instr->index());
4251 Operand length = ToOperand(instr->length());
4252 __ cmp(index, length);
Ben Murdoch086aeea2011-05-13 15:57:08 +01004253 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004254 if (FLAG_debug_code && instr->hydrogen()->skip_check()) {
4255 Label done;
4256 __ b(NegateCondition(cc), &done);
4257 __ stop("eliminated bounds check failed");
4258 __ bind(&done);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004259 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004260 DeoptimizeIf(cc, instr);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004261 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004262}
4263
4264
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004265void LCodeGen::DoStoreKeyedExternalArray(LStoreKeyed* instr) {
4266 Register external_pointer = ToRegister(instr->elements());
Ben Murdoch257744e2011-11-30 15:57:28 +00004267 Register key = no_reg;
Ben Murdoch589d6972011-11-30 16:04:58 +00004268 ElementsKind elements_kind = instr->elements_kind();
Ben Murdoch257744e2011-11-30 15:57:28 +00004269 bool key_is_constant = instr->key()->IsConstantOperand();
4270 int constant_key = 0;
4271 if (key_is_constant) {
4272 constant_key = ToInteger32(LConstantOperand::cast(instr->key()));
4273 if (constant_key & 0xF0000000) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004274 Abort(kArrayIndexConstantValueTooBig);
Ben Murdoch257744e2011-11-30 15:57:28 +00004275 }
4276 } else {
4277 key = ToRegister(instr->key());
4278 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004279 int element_size_shift = ElementsKindToShiftSize(elements_kind);
4280 int shift_size = (instr->hydrogen()->key()->representation().IsSmi())
4281 ? (element_size_shift - kSmiTagSize) : element_size_shift;
4282 int base_offset = instr->base_offset();
Ben Murdoch257744e2011-11-30 15:57:28 +00004283
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004284 if (elements_kind == EXTERNAL_FLOAT32_ELEMENTS ||
4285 elements_kind == FLOAT32_ELEMENTS ||
4286 elements_kind == EXTERNAL_FLOAT64_ELEMENTS ||
4287 elements_kind == FLOAT64_ELEMENTS) {
4288 Register address = scratch0();
Ben Murdoch8b112d22011-06-08 16:22:53 +01004289 DwVfpRegister value(ToDoubleRegister(instr->value()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004290 if (key_is_constant) {
4291 if (constant_key != 0) {
4292 __ add(address, external_pointer,
4293 Operand(constant_key << element_size_shift));
4294 } else {
4295 address = external_pointer;
4296 }
4297 } else {
4298 __ add(address, external_pointer, Operand(key, LSL, shift_size));
4299 }
4300 if (elements_kind == EXTERNAL_FLOAT32_ELEMENTS ||
4301 elements_kind == FLOAT32_ELEMENTS) {
Ben Murdoch257744e2011-11-30 15:57:28 +00004302 __ vcvt_f32_f64(double_scratch0().low(), value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004303 __ vstr(double_scratch0().low(), address, base_offset);
4304 } else { // Storing doubles, not floats.
4305 __ vstr(value, address, base_offset);
Ben Murdoch257744e2011-11-30 15:57:28 +00004306 }
Ben Murdoch8b112d22011-06-08 16:22:53 +01004307 } else {
4308 Register value(ToRegister(instr->value()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004309 MemOperand mem_operand = PrepareKeyedOperand(
4310 key, external_pointer, key_is_constant, constant_key,
4311 element_size_shift, shift_size,
4312 base_offset);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004313 switch (elements_kind) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004314 case EXTERNAL_UINT8_CLAMPED_ELEMENTS:
4315 case EXTERNAL_INT8_ELEMENTS:
4316 case EXTERNAL_UINT8_ELEMENTS:
4317 case UINT8_ELEMENTS:
4318 case UINT8_CLAMPED_ELEMENTS:
4319 case INT8_ELEMENTS:
Ben Murdoch257744e2011-11-30 15:57:28 +00004320 __ strb(value, mem_operand);
Ben Murdoch8b112d22011-06-08 16:22:53 +01004321 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004322 case EXTERNAL_INT16_ELEMENTS:
4323 case EXTERNAL_UINT16_ELEMENTS:
4324 case INT16_ELEMENTS:
4325 case UINT16_ELEMENTS:
Ben Murdoch257744e2011-11-30 15:57:28 +00004326 __ strh(value, mem_operand);
Ben Murdoch8b112d22011-06-08 16:22:53 +01004327 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004328 case EXTERNAL_INT32_ELEMENTS:
4329 case EXTERNAL_UINT32_ELEMENTS:
4330 case INT32_ELEMENTS:
4331 case UINT32_ELEMENTS:
Ben Murdoch257744e2011-11-30 15:57:28 +00004332 __ str(value, mem_operand);
Ben Murdoch8b112d22011-06-08 16:22:53 +01004333 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004334 case FLOAT32_ELEMENTS:
4335 case FLOAT64_ELEMENTS:
4336 case EXTERNAL_FLOAT32_ELEMENTS:
4337 case EXTERNAL_FLOAT64_ELEMENTS:
Ben Murdoch589d6972011-11-30 16:04:58 +00004338 case FAST_DOUBLE_ELEMENTS:
4339 case FAST_ELEMENTS:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004340 case FAST_SMI_ELEMENTS:
4341 case FAST_HOLEY_DOUBLE_ELEMENTS:
4342 case FAST_HOLEY_ELEMENTS:
4343 case FAST_HOLEY_SMI_ELEMENTS:
Ben Murdoch589d6972011-11-30 16:04:58 +00004344 case DICTIONARY_ELEMENTS:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004345 case SLOPPY_ARGUMENTS_ELEMENTS:
Ben Murdoch8b112d22011-06-08 16:22:53 +01004346 UNREACHABLE();
4347 break;
4348 }
4349 }
Steve Block44f0eee2011-05-26 01:26:41 +01004350}
4351
4352
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004353void LCodeGen::DoStoreKeyedFixedDoubleArray(LStoreKeyed* instr) {
4354 DwVfpRegister value = ToDoubleRegister(instr->value());
4355 Register elements = ToRegister(instr->elements());
4356 Register scratch = scratch0();
4357 DwVfpRegister double_scratch = double_scratch0();
4358 bool key_is_constant = instr->key()->IsConstantOperand();
4359 int base_offset = instr->base_offset();
Ben Murdochb0fe1622011-05-05 13:52:32 +01004360
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004361 // Calculate the effective address of the slot in the array to store the
4362 // double value.
4363 int element_size_shift = ElementsKindToShiftSize(FAST_DOUBLE_ELEMENTS);
4364 if (key_is_constant) {
4365 int constant_key = ToInteger32(LConstantOperand::cast(instr->key()));
4366 if (constant_key & 0xF0000000) {
4367 Abort(kArrayIndexConstantValueTooBig);
4368 }
4369 __ add(scratch, elements,
4370 Operand((constant_key << element_size_shift) + base_offset));
4371 } else {
4372 int shift_size = (instr->hydrogen()->key()->representation().IsSmi())
4373 ? (element_size_shift - kSmiTagSize) : element_size_shift;
4374 __ add(scratch, elements, Operand(base_offset));
4375 __ add(scratch, scratch,
4376 Operand(ToRegister(instr->key()), LSL, shift_size));
4377 }
4378
4379 if (instr->NeedsCanonicalization()) {
4380 // Force a canonical NaN.
4381 if (masm()->emit_debug_code()) {
4382 __ vmrs(ip);
4383 __ tst(ip, Operand(kVFPDefaultNaNModeControlBit));
4384 __ Assert(ne, kDefaultNaNModeNotSet);
4385 }
4386 __ VFPCanonicalizeNaN(double_scratch, value);
4387 __ vstr(double_scratch, scratch, 0);
4388 } else {
4389 __ vstr(value, scratch, 0);
4390 }
4391}
4392
4393
4394void LCodeGen::DoStoreKeyedFixedArray(LStoreKeyed* instr) {
4395 Register value = ToRegister(instr->value());
4396 Register elements = ToRegister(instr->elements());
4397 Register key = instr->key()->IsRegister() ? ToRegister(instr->key())
4398 : no_reg;
4399 Register scratch = scratch0();
4400 Register store_base = scratch;
4401 int offset = instr->base_offset();
4402
4403 // Do the store.
4404 if (instr->key()->IsConstantOperand()) {
4405 DCHECK(!instr->hydrogen()->NeedsWriteBarrier());
4406 LConstantOperand* const_operand = LConstantOperand::cast(instr->key());
4407 offset += ToInteger32(const_operand) * kPointerSize;
4408 store_base = elements;
4409 } else {
4410 // Even though the HLoadKeyed instruction forces the input
4411 // representation for the key to be an integer, the input gets replaced
4412 // during bound check elimination with the index argument to the bounds
4413 // check, which can be tagged, so that case must be handled here, too.
4414 if (instr->hydrogen()->key()->representation().IsSmi()) {
4415 __ add(scratch, elements, Operand::PointerOffsetFromSmiKey(key));
4416 } else {
4417 __ add(scratch, elements, Operand(key, LSL, kPointerSizeLog2));
4418 }
4419 }
4420 __ str(value, MemOperand(store_base, offset));
4421
4422 if (instr->hydrogen()->NeedsWriteBarrier()) {
4423 SmiCheck check_needed =
4424 instr->hydrogen()->value()->type().IsHeapObject()
4425 ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
4426 // Compute address of modified element and store it into key register.
4427 __ add(key, store_base, Operand(offset));
4428 __ RecordWrite(elements,
4429 key,
4430 value,
4431 GetLinkRegisterState(),
4432 kSaveFPRegs,
4433 EMIT_REMEMBERED_SET,
4434 check_needed,
4435 instr->hydrogen()->PointersToHereCheckForValue());
4436 }
4437}
4438
4439
4440void LCodeGen::DoStoreKeyed(LStoreKeyed* instr) {
4441 // By cases: external, fast double
4442 if (instr->is_typed_elements()) {
4443 DoStoreKeyedExternalArray(instr);
4444 } else if (instr->hydrogen()->value()->representation().IsDouble()) {
4445 DoStoreKeyedFixedDoubleArray(instr);
4446 } else {
4447 DoStoreKeyedFixedArray(instr);
4448 }
4449}
4450
4451
4452void LCodeGen::DoStoreKeyedGeneric(LStoreKeyedGeneric* instr) {
4453 DCHECK(ToRegister(instr->context()).is(cp));
4454 DCHECK(ToRegister(instr->object()).is(StoreDescriptor::ReceiverRegister()));
4455 DCHECK(ToRegister(instr->key()).is(StoreDescriptor::NameRegister()));
4456 DCHECK(ToRegister(instr->value()).is(StoreDescriptor::ValueRegister()));
4457
4458 Handle<Code> ic =
4459 CodeFactory::KeyedStoreIC(isolate(), instr->strict_mode()).code();
4460 CallCode(ic, RelocInfo::CODE_TARGET, instr, NEVER_INLINE_TARGET_ADDRESS);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004461}
4462
4463
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004464void LCodeGen::DoTransitionElementsKind(LTransitionElementsKind* instr) {
4465 Register object_reg = ToRegister(instr->object());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004466 Register scratch = scratch0();
4467
4468 Handle<Map> from_map = instr->original_map();
4469 Handle<Map> to_map = instr->transitioned_map();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004470 ElementsKind from_kind = instr->from_kind();
4471 ElementsKind to_kind = instr->to_kind();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004472
4473 Label not_applicable;
4474 __ ldr(scratch, FieldMemOperand(object_reg, HeapObject::kMapOffset));
4475 __ cmp(scratch, Operand(from_map));
4476 __ b(ne, &not_applicable);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004477
4478 if (IsSimpleMapChangeTransition(from_kind, to_kind)) {
4479 Register new_map_reg = ToRegister(instr->new_map_temp());
4480 __ mov(new_map_reg, Operand(to_map));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004481 __ str(new_map_reg, FieldMemOperand(object_reg, HeapObject::kMapOffset));
4482 // Write barrier.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004483 __ RecordWriteForMap(object_reg,
4484 new_map_reg,
4485 scratch,
4486 GetLinkRegisterState(),
4487 kDontSaveFPRegs);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004488 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004489 DCHECK(ToRegister(instr->context()).is(cp));
4490 DCHECK(object_reg.is(r0));
4491 PushSafepointRegistersScope scope(this);
4492 __ Move(r1, to_map);
4493 bool is_js_array = from_map->instance_type() == JS_ARRAY_TYPE;
4494 TransitionElementsKindStub stub(isolate(), from_kind, to_kind, is_js_array);
4495 __ CallStub(&stub);
4496 RecordSafepointWithRegisters(
4497 instr->pointer_map(), 0, Safepoint::kLazyDeopt);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004498 }
4499 __ bind(&not_applicable);
4500}
4501
4502
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004503void LCodeGen::DoTrapAllocationMemento(LTrapAllocationMemento* instr) {
4504 Register object = ToRegister(instr->object());
4505 Register temp = ToRegister(instr->temp());
4506 Label no_memento_found;
4507 __ TestJSArrayForAllocationMemento(object, temp, &no_memento_found);
4508 DeoptimizeIf(eq, instr);
4509 __ bind(&no_memento_found);
4510}
4511
4512
Ben Murdoch257744e2011-11-30 15:57:28 +00004513void LCodeGen::DoStringAdd(LStringAdd* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004514 DCHECK(ToRegister(instr->context()).is(cp));
4515 DCHECK(ToRegister(instr->left()).is(r1));
4516 DCHECK(ToRegister(instr->right()).is(r0));
4517 StringAddStub stub(isolate(),
4518 instr->hydrogen()->flags(),
4519 instr->hydrogen()->pretenure_flag());
Ben Murdoch257744e2011-11-30 15:57:28 +00004520 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
4521}
4522
4523
Steve Block1e0659c2011-05-24 12:43:12 +01004524void LCodeGen::DoStringCharCodeAt(LStringCharCodeAt* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004525 class DeferredStringCharCodeAt FINAL : public LDeferredCode {
Steve Block1e0659c2011-05-24 12:43:12 +01004526 public:
4527 DeferredStringCharCodeAt(LCodeGen* codegen, LStringCharCodeAt* instr)
4528 : LDeferredCode(codegen), instr_(instr) { }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004529 virtual void Generate() OVERRIDE {
4530 codegen()->DoDeferredStringCharCodeAt(instr_);
4531 }
4532 virtual LInstruction* instr() OVERRIDE { return instr_; }
Steve Block1e0659c2011-05-24 12:43:12 +01004533 private:
4534 LStringCharCodeAt* instr_;
4535 };
4536
Steve Block1e0659c2011-05-24 12:43:12 +01004537 DeferredStringCharCodeAt* deferred =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004538 new(zone()) DeferredStringCharCodeAt(this, instr);
Steve Block1e0659c2011-05-24 12:43:12 +01004539
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004540 StringCharLoadGenerator::Generate(masm(),
4541 ToRegister(instr->string()),
4542 ToRegister(instr->index()),
4543 ToRegister(instr->result()),
4544 deferred->entry());
Steve Block1e0659c2011-05-24 12:43:12 +01004545 __ bind(deferred->exit());
4546}
4547
4548
4549void LCodeGen::DoDeferredStringCharCodeAt(LStringCharCodeAt* instr) {
4550 Register string = ToRegister(instr->string());
4551 Register result = ToRegister(instr->result());
4552 Register scratch = scratch0();
4553
4554 // TODO(3095996): Get rid of this. For now, we need to make the
4555 // result register contain a valid pointer because it is already
4556 // contained in the register pointer map.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004557 __ mov(result, Operand::Zero());
Steve Block1e0659c2011-05-24 12:43:12 +01004558
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004559 PushSafepointRegistersScope scope(this);
Steve Block1e0659c2011-05-24 12:43:12 +01004560 __ push(string);
4561 // Push the index as a smi. This is safe because of the checks in
4562 // DoStringCharCodeAt above.
4563 if (instr->index()->IsConstantOperand()) {
4564 int const_index = ToInteger32(LConstantOperand::cast(instr->index()));
4565 __ mov(scratch, Operand(Smi::FromInt(const_index)));
4566 __ push(scratch);
4567 } else {
4568 Register index = ToRegister(instr->index());
4569 __ SmiTag(index);
4570 __ push(index);
4571 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004572 CallRuntimeFromDeferred(Runtime::kStringCharCodeAtRT, 2, instr,
4573 instr->context());
4574 __ AssertSmi(r0);
Steve Block1e0659c2011-05-24 12:43:12 +01004575 __ SmiUntag(r0);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004576 __ StoreToSafepointRegisterSlot(r0, result);
Steve Block1e0659c2011-05-24 12:43:12 +01004577}
4578
4579
Steve Block44f0eee2011-05-26 01:26:41 +01004580void LCodeGen::DoStringCharFromCode(LStringCharFromCode* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004581 class DeferredStringCharFromCode FINAL : public LDeferredCode {
Steve Block44f0eee2011-05-26 01:26:41 +01004582 public:
4583 DeferredStringCharFromCode(LCodeGen* codegen, LStringCharFromCode* instr)
4584 : LDeferredCode(codegen), instr_(instr) { }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004585 virtual void Generate() OVERRIDE {
4586 codegen()->DoDeferredStringCharFromCode(instr_);
4587 }
4588 virtual LInstruction* instr() OVERRIDE { return instr_; }
Steve Block44f0eee2011-05-26 01:26:41 +01004589 private:
4590 LStringCharFromCode* instr_;
4591 };
4592
4593 DeferredStringCharFromCode* deferred =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004594 new(zone()) DeferredStringCharFromCode(this, instr);
Steve Block44f0eee2011-05-26 01:26:41 +01004595
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004596 DCHECK(instr->hydrogen()->value()->representation().IsInteger32());
Steve Block44f0eee2011-05-26 01:26:41 +01004597 Register char_code = ToRegister(instr->char_code());
4598 Register result = ToRegister(instr->result());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004599 DCHECK(!char_code.is(result));
Steve Block44f0eee2011-05-26 01:26:41 +01004600
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004601 __ cmp(char_code, Operand(String::kMaxOneByteCharCode));
Steve Block44f0eee2011-05-26 01:26:41 +01004602 __ b(hi, deferred->entry());
4603 __ LoadRoot(result, Heap::kSingleCharacterStringCacheRootIndex);
4604 __ add(result, result, Operand(char_code, LSL, kPointerSizeLog2));
4605 __ ldr(result, FieldMemOperand(result, FixedArray::kHeaderSize));
4606 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
4607 __ cmp(result, ip);
4608 __ b(eq, deferred->entry());
4609 __ bind(deferred->exit());
4610}
4611
4612
4613void LCodeGen::DoDeferredStringCharFromCode(LStringCharFromCode* instr) {
4614 Register char_code = ToRegister(instr->char_code());
4615 Register result = ToRegister(instr->result());
4616
4617 // TODO(3095996): Get rid of this. For now, we need to make the
4618 // result register contain a valid pointer because it is already
4619 // contained in the register pointer map.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004620 __ mov(result, Operand::Zero());
Steve Block44f0eee2011-05-26 01:26:41 +01004621
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004622 PushSafepointRegistersScope scope(this);
Steve Block44f0eee2011-05-26 01:26:41 +01004623 __ SmiTag(char_code);
4624 __ push(char_code);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004625 CallRuntimeFromDeferred(Runtime::kCharFromCode, 1, instr, instr->context());
Steve Block44f0eee2011-05-26 01:26:41 +01004626 __ StoreToSafepointRegisterSlot(r0, result);
Steve Block44f0eee2011-05-26 01:26:41 +01004627}
4628
4629
Ben Murdochb0fe1622011-05-05 13:52:32 +01004630void LCodeGen::DoInteger32ToDouble(LInteger32ToDouble* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004631 LOperand* input = instr->value();
4632 DCHECK(input->IsRegister() || input->IsStackSlot());
Ben Murdochb8e0da22011-05-16 14:20:40 +01004633 LOperand* output = instr->result();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004634 DCHECK(output->IsDoubleRegister());
Ben Murdochb8e0da22011-05-16 14:20:40 +01004635 SwVfpRegister single_scratch = double_scratch0().low();
4636 if (input->IsStackSlot()) {
4637 Register scratch = scratch0();
4638 __ ldr(scratch, ToMemOperand(input));
4639 __ vmov(single_scratch, scratch);
4640 } else {
4641 __ vmov(single_scratch, ToRegister(input));
4642 }
4643 __ vcvt_f64_s32(ToDoubleRegister(output), single_scratch);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004644}
4645
4646
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004647void LCodeGen::DoUint32ToDouble(LUint32ToDouble* instr) {
4648 LOperand* input = instr->value();
4649 LOperand* output = instr->result();
4650
4651 SwVfpRegister flt_scratch = double_scratch0().low();
4652 __ vmov(flt_scratch, ToRegister(input));
4653 __ vcvt_f64_u32(ToDoubleRegister(output), flt_scratch);
4654}
4655
4656
Ben Murdochb0fe1622011-05-05 13:52:32 +01004657void LCodeGen::DoNumberTagI(LNumberTagI* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004658 class DeferredNumberTagI FINAL : public LDeferredCode {
Ben Murdochb0fe1622011-05-05 13:52:32 +01004659 public:
4660 DeferredNumberTagI(LCodeGen* codegen, LNumberTagI* instr)
4661 : LDeferredCode(codegen), instr_(instr) { }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004662 virtual void Generate() OVERRIDE {
4663 codegen()->DoDeferredNumberTagIU(instr_,
4664 instr_->value(),
4665 instr_->temp1(),
4666 instr_->temp2(),
4667 SIGNED_INT32);
4668 }
4669 virtual LInstruction* instr() OVERRIDE { return instr_; }
Ben Murdochb0fe1622011-05-05 13:52:32 +01004670 private:
4671 LNumberTagI* instr_;
4672 };
4673
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004674 Register src = ToRegister(instr->value());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004675 Register dst = ToRegister(instr->result());
Ben Murdochb0fe1622011-05-05 13:52:32 +01004676
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004677 DeferredNumberTagI* deferred = new(zone()) DeferredNumberTagI(this, instr);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004678 __ SmiTag(dst, src, SetCC);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004679 __ b(vs, deferred->entry());
4680 __ bind(deferred->exit());
4681}
4682
4683
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004684void LCodeGen::DoNumberTagU(LNumberTagU* instr) {
4685 class DeferredNumberTagU FINAL : public LDeferredCode {
4686 public:
4687 DeferredNumberTagU(LCodeGen* codegen, LNumberTagU* instr)
4688 : LDeferredCode(codegen), instr_(instr) { }
4689 virtual void Generate() OVERRIDE {
4690 codegen()->DoDeferredNumberTagIU(instr_,
4691 instr_->value(),
4692 instr_->temp1(),
4693 instr_->temp2(),
4694 UNSIGNED_INT32);
4695 }
4696 virtual LInstruction* instr() OVERRIDE { return instr_; }
4697 private:
4698 LNumberTagU* instr_;
4699 };
4700
4701 Register input = ToRegister(instr->value());
4702 Register result = ToRegister(instr->result());
4703
4704 DeferredNumberTagU* deferred = new(zone()) DeferredNumberTagU(this, instr);
4705 __ cmp(input, Operand(Smi::kMaxValue));
4706 __ b(hi, deferred->entry());
4707 __ SmiTag(result, input);
4708 __ bind(deferred->exit());
4709}
4710
4711
4712void LCodeGen::DoDeferredNumberTagIU(LInstruction* instr,
4713 LOperand* value,
4714 LOperand* temp1,
4715 LOperand* temp2,
4716 IntegerSignedness signedness) {
4717 Label done, slow;
4718 Register src = ToRegister(value);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004719 Register dst = ToRegister(instr->result());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004720 Register tmp1 = scratch0();
4721 Register tmp2 = ToRegister(temp1);
4722 Register tmp3 = ToRegister(temp2);
4723 LowDwVfpRegister dbl_scratch = double_scratch0();
Ben Murdochb0fe1622011-05-05 13:52:32 +01004724
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004725 if (signedness == SIGNED_INT32) {
4726 // There was overflow, so bits 30 and 31 of the original integer
4727 // disagree. Try to allocate a heap number in new space and store
4728 // the value in there. If that fails, call the runtime system.
4729 if (dst.is(src)) {
4730 __ SmiUntag(src, dst);
4731 __ eor(src, src, Operand(0x80000000));
4732 }
4733 __ vmov(dbl_scratch.low(), src);
4734 __ vcvt_f64_s32(dbl_scratch, dbl_scratch.low());
4735 } else {
4736 __ vmov(dbl_scratch.low(), src);
4737 __ vcvt_f64_u32(dbl_scratch, dbl_scratch.low());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004738 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004739
Ben Murdochb0fe1622011-05-05 13:52:32 +01004740 if (FLAG_inline_new) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004741 __ LoadRoot(tmp3, Heap::kHeapNumberMapRootIndex);
4742 __ AllocateHeapNumber(dst, tmp1, tmp2, tmp3, &slow, DONT_TAG_RESULT);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004743 __ b(&done);
4744 }
4745
4746 // Slow case: Call the runtime system to do the number allocation.
4747 __ bind(&slow);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004748 {
4749 // TODO(3095996): Put a valid pointer value in the stack slot where the
4750 // result register is stored, as this register is in the pointer map, but
4751 // contains an integer value.
4752 __ mov(dst, Operand::Zero());
Ben Murdochb0fe1622011-05-05 13:52:32 +01004753
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004754 // Preserve the value of all registers.
4755 PushSafepointRegistersScope scope(this);
4756
4757 // NumberTagI and NumberTagD use the context from the frame, rather than
4758 // the environment's HContext or HInlinedContext value.
4759 // They only call Runtime::kAllocateHeapNumber.
4760 // The corresponding HChange instructions are added in a phase that does
4761 // not have easy access to the local context.
4762 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4763 __ CallRuntimeSaveDoubles(Runtime::kAllocateHeapNumber);
4764 RecordSafepointWithRegisters(
4765 instr->pointer_map(), 0, Safepoint::kNoLazyDeopt);
4766 __ sub(r0, r0, Operand(kHeapObjectTag));
4767 __ StoreToSafepointRegisterSlot(r0, dst);
4768 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01004769
4770 // Done. Put the value in dbl_scratch into the value of the allocated heap
4771 // number.
4772 __ bind(&done);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004773 __ vstr(dbl_scratch, dst, HeapNumber::kValueOffset);
4774 __ add(dst, dst, Operand(kHeapObjectTag));
Ben Murdochb0fe1622011-05-05 13:52:32 +01004775}
4776
4777
4778void LCodeGen::DoNumberTagD(LNumberTagD* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004779 class DeferredNumberTagD FINAL : public LDeferredCode {
Ben Murdochb0fe1622011-05-05 13:52:32 +01004780 public:
4781 DeferredNumberTagD(LCodeGen* codegen, LNumberTagD* instr)
4782 : LDeferredCode(codegen), instr_(instr) { }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004783 virtual void Generate() OVERRIDE {
4784 codegen()->DoDeferredNumberTagD(instr_);
4785 }
4786 virtual LInstruction* instr() OVERRIDE { return instr_; }
Ben Murdochb0fe1622011-05-05 13:52:32 +01004787 private:
4788 LNumberTagD* instr_;
4789 };
4790
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004791 DwVfpRegister input_reg = ToDoubleRegister(instr->value());
Steve Block9fac8402011-05-12 15:51:54 +01004792 Register scratch = scratch0();
Ben Murdochb0fe1622011-05-05 13:52:32 +01004793 Register reg = ToRegister(instr->result());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004794 Register temp1 = ToRegister(instr->temp());
4795 Register temp2 = ToRegister(instr->temp2());
Ben Murdochb0fe1622011-05-05 13:52:32 +01004796
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004797 DeferredNumberTagD* deferred = new(zone()) DeferredNumberTagD(this, instr);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004798 if (FLAG_inline_new) {
4799 __ LoadRoot(scratch, Heap::kHeapNumberMapRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004800 // We want the untagged address first for performance
4801 __ AllocateHeapNumber(reg, temp1, temp2, scratch, deferred->entry(),
4802 DONT_TAG_RESULT);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004803 } else {
4804 __ jmp(deferred->entry());
4805 }
4806 __ bind(deferred->exit());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004807 __ vstr(input_reg, reg, HeapNumber::kValueOffset);
4808 // Now that we have finished with the object's real address tag it
4809 __ add(reg, reg, Operand(kHeapObjectTag));
Ben Murdochb0fe1622011-05-05 13:52:32 +01004810}
4811
4812
4813void LCodeGen::DoDeferredNumberTagD(LNumberTagD* instr) {
4814 // TODO(3095996): Get rid of this. For now, we need to make the
4815 // result register contain a valid pointer because it is already
4816 // contained in the register pointer map.
4817 Register reg = ToRegister(instr->result());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004818 __ mov(reg, Operand::Zero());
Ben Murdochb0fe1622011-05-05 13:52:32 +01004819
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004820 PushSafepointRegistersScope scope(this);
4821 // NumberTagI and NumberTagD use the context from the frame, rather than
4822 // the environment's HContext or HInlinedContext value.
4823 // They only call Runtime::kAllocateHeapNumber.
4824 // The corresponding HChange instructions are added in a phase that does
4825 // not have easy access to the local context.
4826 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4827 __ CallRuntimeSaveDoubles(Runtime::kAllocateHeapNumber);
4828 RecordSafepointWithRegisters(
4829 instr->pointer_map(), 0, Safepoint::kNoLazyDeopt);
4830 __ sub(r0, r0, Operand(kHeapObjectTag));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004831 __ StoreToSafepointRegisterSlot(r0, reg);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004832}
4833
4834
4835void LCodeGen::DoSmiTag(LSmiTag* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004836 HChange* hchange = instr->hydrogen();
4837 Register input = ToRegister(instr->value());
4838 Register output = ToRegister(instr->result());
4839 if (hchange->CheckFlag(HValue::kCanOverflow) &&
4840 hchange->value()->CheckFlag(HValue::kUint32)) {
4841 __ tst(input, Operand(0xc0000000));
4842 DeoptimizeIf(ne, instr);
4843 }
4844 if (hchange->CheckFlag(HValue::kCanOverflow) &&
4845 !hchange->value()->CheckFlag(HValue::kUint32)) {
4846 __ SmiTag(output, input, SetCC);
4847 DeoptimizeIf(vs, instr);
4848 } else {
4849 __ SmiTag(output, input);
4850 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01004851}
4852
4853
4854void LCodeGen::DoSmiUntag(LSmiUntag* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004855 Register input = ToRegister(instr->value());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004856 Register result = ToRegister(instr->result());
Ben Murdoch086aeea2011-05-13 15:57:08 +01004857 if (instr->needs_check()) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00004858 STATIC_ASSERT(kHeapObjectTag == 1);
Ben Murdoch257744e2011-11-30 15:57:28 +00004859 // If the input is a HeapObject, SmiUntag will set the carry flag.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004860 __ SmiUntag(result, input, SetCC);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004861 DeoptimizeIf(cs, instr);
Ben Murdoch257744e2011-11-30 15:57:28 +00004862 } else {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004863 __ SmiUntag(result, input);
Ben Murdoch086aeea2011-05-13 15:57:08 +01004864 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01004865}
4866
4867
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004868void LCodeGen::EmitNumberUntagD(LNumberUntagD* instr, Register input_reg,
4869 DwVfpRegister result_reg,
4870 NumberUntagDMode mode) {
4871 bool can_convert_undefined_to_nan =
4872 instr->hydrogen()->can_convert_undefined_to_nan();
4873 bool deoptimize_on_minus_zero = instr->hydrogen()->deoptimize_on_minus_zero();
4874
Steve Block9fac8402011-05-12 15:51:54 +01004875 Register scratch = scratch0();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004876 SwVfpRegister flt_scratch = double_scratch0().low();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004877 DCHECK(!result_reg.is(double_scratch0()));
4878 Label convert, load_smi, done;
4879 if (mode == NUMBER_CANDIDATE_IS_ANY_TAGGED) {
4880 // Smi check.
4881 __ UntagAndJumpIfSmi(scratch, input_reg, &load_smi);
4882 // Heap number map check.
4883 __ ldr(scratch, FieldMemOperand(input_reg, HeapObject::kMapOffset));
4884 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
4885 __ cmp(scratch, Operand(ip));
4886 if (can_convert_undefined_to_nan) {
4887 __ b(ne, &convert);
4888 } else {
4889 DeoptimizeIf(ne, instr);
4890 }
4891 // load heap number
4892 __ vldr(result_reg, input_reg, HeapNumber::kValueOffset - kHeapObjectTag);
4893 if (deoptimize_on_minus_zero) {
4894 __ VmovLow(scratch, result_reg);
4895 __ cmp(scratch, Operand::Zero());
4896 __ b(ne, &done);
4897 __ VmovHigh(scratch, result_reg);
4898 __ cmp(scratch, Operand(HeapNumber::kSignMask));
4899 DeoptimizeIf(eq, instr);
4900 }
Ben Murdoch7d3e7fc2011-07-12 16:37:06 +01004901 __ jmp(&done);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004902 if (can_convert_undefined_to_nan) {
4903 __ bind(&convert);
4904 // Convert undefined (and hole) to NaN.
4905 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
4906 __ cmp(input_reg, Operand(ip));
4907 DeoptimizeIf(ne, instr);
4908 __ LoadRoot(scratch, Heap::kNanValueRootIndex);
4909 __ vldr(result_reg, scratch, HeapNumber::kValueOffset - kHeapObjectTag);
4910 __ jmp(&done);
4911 }
4912 } else {
4913 __ SmiUntag(scratch, input_reg);
4914 DCHECK(mode == NUMBER_CANDIDATE_IS_SMI);
Ben Murdoch7d3e7fc2011-07-12 16:37:06 +01004915 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01004916 // Smi to double register conversion
4917 __ bind(&load_smi);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004918 // scratch: untagged value of input_reg
4919 __ vmov(flt_scratch, scratch);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004920 __ vcvt_f64_s32(result_reg, flt_scratch);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004921 __ bind(&done);
4922}
4923
4924
Ben Murdochb0fe1622011-05-05 13:52:32 +01004925void LCodeGen::DoDeferredTaggedToI(LTaggedToI* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004926 Register input_reg = ToRegister(instr->value());
Steve Block44f0eee2011-05-26 01:26:41 +01004927 Register scratch1 = scratch0();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004928 Register scratch2 = ToRegister(instr->temp());
4929 LowDwVfpRegister double_scratch = double_scratch0();
4930 DwVfpRegister double_scratch2 = ToDoubleRegister(instr->temp2());
Steve Block44f0eee2011-05-26 01:26:41 +01004931
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004932 DCHECK(!scratch1.is(input_reg) && !scratch1.is(scratch2));
4933 DCHECK(!scratch2.is(input_reg) && !scratch2.is(scratch1));
Steve Block44f0eee2011-05-26 01:26:41 +01004934
4935 Label done;
Ben Murdochb0fe1622011-05-05 13:52:32 +01004936
Ben Murdoch257744e2011-11-30 15:57:28 +00004937 // The input was optimistically untagged; revert it.
4938 // The carry flag is set when we reach this deferred code as we just executed
4939 // SmiUntag(heap_object, SetCC)
Ben Murdoch69a99ed2011-11-30 16:03:39 +00004940 STATIC_ASSERT(kHeapObjectTag == 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004941 __ adc(scratch2, input_reg, Operand(input_reg));
Ben Murdoch257744e2011-11-30 15:57:28 +00004942
Ben Murdochb0fe1622011-05-05 13:52:32 +01004943 // Heap number map check.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004944 __ ldr(scratch1, FieldMemOperand(scratch2, HeapObject::kMapOffset));
Ben Murdochb0fe1622011-05-05 13:52:32 +01004945 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
Steve Block44f0eee2011-05-26 01:26:41 +01004946 __ cmp(scratch1, Operand(ip));
Ben Murdochb0fe1622011-05-05 13:52:32 +01004947
4948 if (instr->truncating()) {
Steve Block44f0eee2011-05-26 01:26:41 +01004949 // Performs a truncating conversion of a floating point number as used by
4950 // the JS bitwise operations.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004951 Label no_heap_number, check_bools, check_false;
4952 __ b(ne, &no_heap_number);
4953 __ TruncateHeapNumberToI(input_reg, scratch2);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004954 __ b(&done);
4955
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004956 // Check for Oddballs. Undefined/False is converted to zero and True to one
4957 // for truncating conversions.
4958 __ bind(&no_heap_number);
4959 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
4960 __ cmp(scratch2, Operand(ip));
4961 __ b(ne, &check_bools);
4962 __ mov(input_reg, Operand::Zero());
4963 __ b(&done);
Steve Block44f0eee2011-05-26 01:26:41 +01004964
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004965 __ bind(&check_bools);
4966 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
4967 __ cmp(scratch2, Operand(ip));
4968 __ b(ne, &check_false);
4969 __ mov(input_reg, Operand(1));
4970 __ b(&done);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004971
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004972 __ bind(&check_false);
4973 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
4974 __ cmp(scratch2, Operand(ip));
4975 DeoptimizeIf(ne, instr, "cannot truncate");
4976 __ mov(input_reg, Operand::Zero());
Ben Murdochb0fe1622011-05-05 13:52:32 +01004977 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004978 DeoptimizeIf(ne, instr, "not a heap number");
Ben Murdochb0fe1622011-05-05 13:52:32 +01004979
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004980 __ sub(ip, scratch2, Operand(kHeapObjectTag));
4981 __ vldr(double_scratch2, ip, HeapNumber::kValueOffset);
4982 __ TryDoubleToInt32Exact(input_reg, double_scratch2, double_scratch);
4983 DeoptimizeIf(ne, instr, "lost precision or NaN");
Steve Block44f0eee2011-05-26 01:26:41 +01004984
Ben Murdochb0fe1622011-05-05 13:52:32 +01004985 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004986 __ cmp(input_reg, Operand::Zero());
Ben Murdochb0fe1622011-05-05 13:52:32 +01004987 __ b(ne, &done);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004988 __ VmovHigh(scratch1, double_scratch2);
Steve Block44f0eee2011-05-26 01:26:41 +01004989 __ tst(scratch1, Operand(HeapNumber::kSignMask));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004990 DeoptimizeIf(ne, instr, "minus zero");
Ben Murdochb0fe1622011-05-05 13:52:32 +01004991 }
4992 }
4993 __ bind(&done);
4994}
4995
4996
4997void LCodeGen::DoTaggedToI(LTaggedToI* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004998 class DeferredTaggedToI FINAL : public LDeferredCode {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004999 public:
5000 DeferredTaggedToI(LCodeGen* codegen, LTaggedToI* instr)
5001 : LDeferredCode(codegen), instr_(instr) { }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005002 virtual void Generate() OVERRIDE {
5003 codegen()->DoDeferredTaggedToI(instr_);
5004 }
5005 virtual LInstruction* instr() OVERRIDE { return instr_; }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005006 private:
5007 LTaggedToI* instr_;
5008 };
5009
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005010 LOperand* input = instr->value();
5011 DCHECK(input->IsRegister());
5012 DCHECK(input->Equals(instr->result()));
Ben Murdochb0fe1622011-05-05 13:52:32 +01005013
5014 Register input_reg = ToRegister(input);
5015
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005016 if (instr->hydrogen()->value()->representation().IsSmi()) {
5017 __ SmiUntag(input_reg);
5018 } else {
5019 DeferredTaggedToI* deferred = new(zone()) DeferredTaggedToI(this, instr);
Ben Murdochb0fe1622011-05-05 13:52:32 +01005020
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005021 // Optimistically untag the input.
5022 // If the input is a HeapObject, SmiUntag will set the carry flag.
5023 __ SmiUntag(input_reg, SetCC);
5024 // Branch to deferred code if the input was tagged.
5025 // The deferred code will take care of restoring the tag.
5026 __ b(cs, deferred->entry());
5027 __ bind(deferred->exit());
5028 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01005029}
5030
5031
5032void LCodeGen::DoNumberUntagD(LNumberUntagD* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005033 LOperand* input = instr->value();
5034 DCHECK(input->IsRegister());
Ben Murdochb0fe1622011-05-05 13:52:32 +01005035 LOperand* result = instr->result();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005036 DCHECK(result->IsDoubleRegister());
Ben Murdochb0fe1622011-05-05 13:52:32 +01005037
5038 Register input_reg = ToRegister(input);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005039 DwVfpRegister result_reg = ToDoubleRegister(result);
Ben Murdochb0fe1622011-05-05 13:52:32 +01005040
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005041 HValue* value = instr->hydrogen()->value();
5042 NumberUntagDMode mode = value->representation().IsSmi()
5043 ? NUMBER_CANDIDATE_IS_SMI : NUMBER_CANDIDATE_IS_ANY_TAGGED;
5044
5045 EmitNumberUntagD(instr, input_reg, result_reg, mode);
Ben Murdochb0fe1622011-05-05 13:52:32 +01005046}
5047
5048
5049void LCodeGen::DoDoubleToI(LDoubleToI* instr) {
Steve Block44f0eee2011-05-26 01:26:41 +01005050 Register result_reg = ToRegister(instr->result());
Steve Block1e0659c2011-05-24 12:43:12 +01005051 Register scratch1 = scratch0();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005052 DwVfpRegister double_input = ToDoubleRegister(instr->value());
5053 LowDwVfpRegister double_scratch = double_scratch0();
Steve Block1e0659c2011-05-24 12:43:12 +01005054
Steve Block44f0eee2011-05-26 01:26:41 +01005055 if (instr->truncating()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005056 __ TruncateDoubleToI(result_reg, double_input);
Steve Block44f0eee2011-05-26 01:26:41 +01005057 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005058 __ TryDoubleToInt32Exact(result_reg, double_input, double_scratch);
5059 // Deoptimize if the input wasn't a int32 (inside a double).
5060 DeoptimizeIf(ne, instr);
5061 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
5062 Label done;
5063 __ cmp(result_reg, Operand::Zero());
5064 __ b(ne, &done);
5065 __ VmovHigh(scratch1, double_input);
5066 __ tst(scratch1, Operand(HeapNumber::kSignMask));
5067 DeoptimizeIf(ne, instr);
5068 __ bind(&done);
5069 }
Steve Block1e0659c2011-05-24 12:43:12 +01005070 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005071}
5072
5073
5074void LCodeGen::DoDoubleToSmi(LDoubleToSmi* instr) {
5075 Register result_reg = ToRegister(instr->result());
5076 Register scratch1 = scratch0();
5077 DwVfpRegister double_input = ToDoubleRegister(instr->value());
5078 LowDwVfpRegister double_scratch = double_scratch0();
5079
5080 if (instr->truncating()) {
5081 __ TruncateDoubleToI(result_reg, double_input);
5082 } else {
5083 __ TryDoubleToInt32Exact(result_reg, double_input, double_scratch);
5084 // Deoptimize if the input wasn't a int32 (inside a double).
5085 DeoptimizeIf(ne, instr);
5086 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
5087 Label done;
5088 __ cmp(result_reg, Operand::Zero());
5089 __ b(ne, &done);
5090 __ VmovHigh(scratch1, double_input);
5091 __ tst(scratch1, Operand(HeapNumber::kSignMask));
5092 DeoptimizeIf(ne, instr);
5093 __ bind(&done);
5094 }
5095 }
5096 __ SmiTag(result_reg, SetCC);
5097 DeoptimizeIf(vs, instr);
Ben Murdochb0fe1622011-05-05 13:52:32 +01005098}
5099
5100
5101void LCodeGen::DoCheckSmi(LCheckSmi* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005102 LOperand* input = instr->value();
5103 __ SmiTst(ToRegister(input));
5104 DeoptimizeIf(ne, instr);
Steve Block44f0eee2011-05-26 01:26:41 +01005105}
5106
5107
5108void LCodeGen::DoCheckNonSmi(LCheckNonSmi* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005109 if (!instr->hydrogen()->value()->type().IsHeapObject()) {
5110 LOperand* input = instr->value();
5111 __ SmiTst(ToRegister(input));
5112 DeoptimizeIf(eq, instr);
5113 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01005114}
5115
5116
5117void LCodeGen::DoCheckInstanceType(LCheckInstanceType* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005118 Register input = ToRegister(instr->value());
Ben Murdoch086aeea2011-05-13 15:57:08 +01005119 Register scratch = scratch0();
Ben Murdoch086aeea2011-05-13 15:57:08 +01005120
5121 __ ldr(scratch, FieldMemOperand(input, HeapObject::kMapOffset));
5122 __ ldrb(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
Ben Murdoch086aeea2011-05-13 15:57:08 +01005123
Ben Murdoch257744e2011-11-30 15:57:28 +00005124 if (instr->hydrogen()->is_interval_check()) {
5125 InstanceType first;
5126 InstanceType last;
5127 instr->hydrogen()->GetCheckInterval(&first, &last);
5128
5129 __ cmp(scratch, Operand(first));
5130
5131 // If there is only one type in the interval check for equality.
5132 if (first == last) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005133 DeoptimizeIf(ne, instr);
Ben Murdoch257744e2011-11-30 15:57:28 +00005134 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005135 DeoptimizeIf(lo, instr);
Ben Murdoch257744e2011-11-30 15:57:28 +00005136 // Omit check for the last type.
5137 if (last != LAST_TYPE) {
5138 __ cmp(scratch, Operand(last));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005139 DeoptimizeIf(hi, instr);
Ben Murdoch257744e2011-11-30 15:57:28 +00005140 }
5141 }
Ben Murdoch086aeea2011-05-13 15:57:08 +01005142 } else {
Ben Murdoch257744e2011-11-30 15:57:28 +00005143 uint8_t mask;
5144 uint8_t tag;
5145 instr->hydrogen()->GetCheckMaskAndTag(&mask, &tag);
5146
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005147 if (base::bits::IsPowerOfTwo32(mask)) {
5148 DCHECK(tag == 0 || base::bits::IsPowerOfTwo32(tag));
Ben Murdoch257744e2011-11-30 15:57:28 +00005149 __ tst(scratch, Operand(mask));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005150 DeoptimizeIf(tag == 0 ? ne : eq, instr);
Ben Murdoch257744e2011-11-30 15:57:28 +00005151 } else {
5152 __ and_(scratch, scratch, Operand(mask));
5153 __ cmp(scratch, Operand(tag));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005154 DeoptimizeIf(ne, instr);
Ben Murdoch086aeea2011-05-13 15:57:08 +01005155 }
5156 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01005157}
5158
5159
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005160void LCodeGen::DoCheckValue(LCheckValue* instr) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005161 Register reg = ToRegister(instr->value());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005162 Handle<HeapObject> object = instr->hydrogen()->object().handle();
5163 AllowDeferredHandleDereference smi_check;
5164 if (isolate()->heap()->InNewSpace(*object)) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005165 Register reg = ToRegister(instr->value());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005166 Handle<Cell> cell = isolate()->factory()->NewCell(object);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005167 __ mov(ip, Operand(Handle<Object>(cell)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005168 __ ldr(ip, FieldMemOperand(ip, Cell::kValueOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005169 __ cmp(reg, ip);
5170 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005171 __ cmp(reg, Operand(object));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005172 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005173 DeoptimizeIf(ne, instr);
Ben Murdochb0fe1622011-05-05 13:52:32 +01005174}
5175
5176
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005177void LCodeGen::DoDeferredInstanceMigration(LCheckMaps* instr, Register object) {
5178 {
5179 PushSafepointRegistersScope scope(this);
5180 __ push(object);
5181 __ mov(cp, Operand::Zero());
5182 __ CallRuntimeSaveDoubles(Runtime::kTryMigrateInstance);
5183 RecordSafepointWithRegisters(
5184 instr->pointer_map(), 1, Safepoint::kNoLazyDeopt);
5185 __ StoreToSafepointRegisterSlot(r0, scratch0());
5186 }
5187 __ tst(scratch0(), Operand(kSmiTagMask));
5188 DeoptimizeIf(eq, instr);
5189}
5190
5191
5192void LCodeGen::DoCheckMaps(LCheckMaps* instr) {
5193 class DeferredCheckMaps FINAL : public LDeferredCode {
5194 public:
5195 DeferredCheckMaps(LCodeGen* codegen, LCheckMaps* instr, Register object)
5196 : LDeferredCode(codegen), instr_(instr), object_(object) {
5197 SetExit(check_maps());
5198 }
5199 virtual void Generate() OVERRIDE {
5200 codegen()->DoDeferredInstanceMigration(instr_, object_);
5201 }
5202 Label* check_maps() { return &check_maps_; }
5203 virtual LInstruction* instr() OVERRIDE { return instr_; }
5204 private:
5205 LCheckMaps* instr_;
5206 Label check_maps_;
5207 Register object_;
5208 };
5209
5210 if (instr->hydrogen()->IsStabilityCheck()) {
5211 const UniqueSet<Map>* maps = instr->hydrogen()->maps();
5212 for (int i = 0; i < maps->size(); ++i) {
5213 AddStabilityDependency(maps->at(i).handle());
5214 }
5215 return;
5216 }
5217
5218 Register map_reg = scratch0();
5219
5220 LOperand* input = instr->value();
5221 DCHECK(input->IsRegister());
5222 Register reg = ToRegister(input);
5223
5224 __ ldr(map_reg, FieldMemOperand(reg, HeapObject::kMapOffset));
5225
5226 DeferredCheckMaps* deferred = NULL;
5227 if (instr->hydrogen()->HasMigrationTarget()) {
5228 deferred = new(zone()) DeferredCheckMaps(this, instr, reg);
5229 __ bind(deferred->check_maps());
5230 }
5231
5232 const UniqueSet<Map>* maps = instr->hydrogen()->maps();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005233 Label success;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005234 for (int i = 0; i < maps->size() - 1; i++) {
5235 Handle<Map> map = maps->at(i).handle();
5236 __ CompareMap(map_reg, map, &success);
5237 __ b(eq, &success);
5238 }
5239
5240 Handle<Map> map = maps->at(maps->size() - 1).handle();
5241 __ CompareMap(map_reg, map, &success);
5242 if (instr->hydrogen()->HasMigrationTarget()) {
5243 __ b(ne, deferred->entry());
5244 } else {
5245 DeoptimizeIf(ne, instr);
5246 }
5247
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005248 __ bind(&success);
5249}
5250
5251
Ben Murdoch257744e2011-11-30 15:57:28 +00005252void LCodeGen::DoClampDToUint8(LClampDToUint8* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005253 DwVfpRegister value_reg = ToDoubleRegister(instr->unclamped());
Ben Murdoch257744e2011-11-30 15:57:28 +00005254 Register result_reg = ToRegister(instr->result());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005255 __ ClampDoubleToUint8(result_reg, value_reg, double_scratch0());
Ben Murdoch257744e2011-11-30 15:57:28 +00005256}
5257
5258
5259void LCodeGen::DoClampIToUint8(LClampIToUint8* instr) {
5260 Register unclamped_reg = ToRegister(instr->unclamped());
5261 Register result_reg = ToRegister(instr->result());
5262 __ ClampUint8(result_reg, unclamped_reg);
5263}
5264
5265
5266void LCodeGen::DoClampTToUint8(LClampTToUint8* instr) {
5267 Register scratch = scratch0();
5268 Register input_reg = ToRegister(instr->unclamped());
5269 Register result_reg = ToRegister(instr->result());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005270 DwVfpRegister temp_reg = ToDoubleRegister(instr->temp());
Ben Murdoch257744e2011-11-30 15:57:28 +00005271 Label is_smi, done, heap_number;
5272
5273 // Both smi and heap number cases are handled.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005274 __ UntagAndJumpIfSmi(result_reg, input_reg, &is_smi);
Ben Murdoch257744e2011-11-30 15:57:28 +00005275
5276 // Check for heap number
5277 __ ldr(scratch, FieldMemOperand(input_reg, HeapObject::kMapOffset));
5278 __ cmp(scratch, Operand(factory()->heap_number_map()));
5279 __ b(eq, &heap_number);
5280
5281 // Check for undefined. Undefined is converted to zero for clamping
5282 // conversions.
5283 __ cmp(input_reg, Operand(factory()->undefined_value()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005284 DeoptimizeIf(ne, instr);
5285 __ mov(result_reg, Operand::Zero());
Ben Murdoch257744e2011-11-30 15:57:28 +00005286 __ jmp(&done);
5287
5288 // Heap number
5289 __ bind(&heap_number);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005290 __ vldr(temp_reg, FieldMemOperand(input_reg, HeapNumber::kValueOffset));
5291 __ ClampDoubleToUint8(result_reg, temp_reg, double_scratch0());
Ben Murdoch257744e2011-11-30 15:57:28 +00005292 __ jmp(&done);
5293
5294 // smi
5295 __ bind(&is_smi);
Ben Murdoch257744e2011-11-30 15:57:28 +00005296 __ ClampUint8(result_reg, result_reg);
5297
5298 __ bind(&done);
5299}
5300
5301
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005302void LCodeGen::DoDoubleBits(LDoubleBits* instr) {
5303 DwVfpRegister value_reg = ToDoubleRegister(instr->value());
5304 Register result_reg = ToRegister(instr->result());
5305 if (instr->hydrogen()->bits() == HDoubleBits::HIGH) {
5306 __ VmovHigh(result_reg, value_reg);
5307 } else {
5308 __ VmovLow(result_reg, value_reg);
Steve Block9fac8402011-05-12 15:51:54 +01005309 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01005310}
5311
5312
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005313void LCodeGen::DoConstructDouble(LConstructDouble* instr) {
5314 Register hi_reg = ToRegister(instr->hi());
5315 Register lo_reg = ToRegister(instr->lo());
5316 DwVfpRegister result_reg = ToDoubleRegister(instr->result());
5317 __ VmovHigh(result_reg, hi_reg);
5318 __ VmovLow(result_reg, lo_reg);
5319}
5320
5321
5322void LCodeGen::DoAllocate(LAllocate* instr) {
5323 class DeferredAllocate FINAL : public LDeferredCode {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005324 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005325 DeferredAllocate(LCodeGen* codegen, LAllocate* instr)
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005326 : LDeferredCode(codegen), instr_(instr) { }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005327 virtual void Generate() OVERRIDE {
5328 codegen()->DoDeferredAllocate(instr_);
5329 }
5330 virtual LInstruction* instr() OVERRIDE { return instr_; }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005331 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005332 LAllocate* instr_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005333 };
5334
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005335 DeferredAllocate* deferred =
5336 new(zone()) DeferredAllocate(this, instr);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005337
5338 Register result = ToRegister(instr->result());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005339 Register scratch = ToRegister(instr->temp1());
5340 Register scratch2 = ToRegister(instr->temp2());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005341
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005342 // Allocate memory for the object.
5343 AllocationFlags flags = TAG_OBJECT;
5344 if (instr->hydrogen()->MustAllocateDoubleAligned()) {
5345 flags = static_cast<AllocationFlags>(flags | DOUBLE_ALIGNMENT);
5346 }
5347 if (instr->hydrogen()->IsOldPointerSpaceAllocation()) {
5348 DCHECK(!instr->hydrogen()->IsOldDataSpaceAllocation());
5349 DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
5350 flags = static_cast<AllocationFlags>(flags | PRETENURE_OLD_POINTER_SPACE);
5351 } else if (instr->hydrogen()->IsOldDataSpaceAllocation()) {
5352 DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
5353 flags = static_cast<AllocationFlags>(flags | PRETENURE_OLD_DATA_SPACE);
5354 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005355
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005356 if (instr->size()->IsConstantOperand()) {
5357 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
5358 if (size <= Page::kMaxRegularHeapObjectSize) {
5359 __ Allocate(size, result, scratch, scratch2, deferred->entry(), flags);
5360 } else {
5361 __ jmp(deferred->entry());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005362 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005363 } else {
5364 Register size = ToRegister(instr->size());
5365 __ Allocate(size, result, scratch, scratch2, deferred->entry(), flags);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005366 }
5367
5368 __ bind(deferred->exit());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005369
5370 if (instr->hydrogen()->MustPrefillWithFiller()) {
5371 STATIC_ASSERT(kHeapObjectTag == 1);
5372 if (instr->size()->IsConstantOperand()) {
5373 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
5374 __ mov(scratch, Operand(size - kHeapObjectTag));
5375 } else {
5376 __ sub(scratch, ToRegister(instr->size()), Operand(kHeapObjectTag));
5377 }
5378 __ mov(scratch2, Operand(isolate()->factory()->one_pointer_filler_map()));
5379 Label loop;
5380 __ bind(&loop);
5381 __ sub(scratch, scratch, Operand(kPointerSize), SetCC);
5382 __ str(scratch2, MemOperand(result, scratch));
5383 __ b(ge, &loop);
5384 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005385}
5386
5387
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005388void LCodeGen::DoDeferredAllocate(LAllocate* instr) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005389 Register result = ToRegister(instr->result());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005390
5391 // TODO(3095996): Get rid of this. For now, we need to make the
5392 // result register contain a valid pointer because it is already
5393 // contained in the register pointer map.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005394 __ mov(result, Operand(Smi::FromInt(0)));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005395
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005396 PushSafepointRegistersScope scope(this);
5397 if (instr->size()->IsRegister()) {
5398 Register size = ToRegister(instr->size());
5399 DCHECK(!size.is(result));
5400 __ SmiTag(size);
5401 __ push(size);
5402 } else {
5403 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
5404 if (size >= 0 && size <= Smi::kMaxValue) {
5405 __ Push(Smi::FromInt(size));
5406 } else {
5407 // We should never get here at runtime => abort
5408 __ stop("invalid allocation size");
5409 return;
5410 }
5411 }
5412
5413 int flags = AllocateDoubleAlignFlag::encode(
5414 instr->hydrogen()->MustAllocateDoubleAligned());
5415 if (instr->hydrogen()->IsOldPointerSpaceAllocation()) {
5416 DCHECK(!instr->hydrogen()->IsOldDataSpaceAllocation());
5417 DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
5418 flags = AllocateTargetSpace::update(flags, OLD_POINTER_SPACE);
5419 } else if (instr->hydrogen()->IsOldDataSpaceAllocation()) {
5420 DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
5421 flags = AllocateTargetSpace::update(flags, OLD_DATA_SPACE);
5422 } else {
5423 flags = AllocateTargetSpace::update(flags, NEW_SPACE);
5424 }
5425 __ Push(Smi::FromInt(flags));
5426
5427 CallRuntimeFromDeferred(
5428 Runtime::kAllocateInTargetSpace, 2, instr, instr->context());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005429 __ StoreToSafepointRegisterSlot(r0, result);
5430}
5431
5432
Steve Block44f0eee2011-05-26 01:26:41 +01005433void LCodeGen::DoToFastProperties(LToFastProperties* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005434 DCHECK(ToRegister(instr->value()).is(r0));
Steve Block44f0eee2011-05-26 01:26:41 +01005435 __ push(r0);
5436 CallRuntime(Runtime::kToFastProperties, 1, instr);
5437}
5438
5439
Ben Murdochb0fe1622011-05-05 13:52:32 +01005440void LCodeGen::DoRegExpLiteral(LRegExpLiteral* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005441 DCHECK(ToRegister(instr->context()).is(cp));
Ben Murdoch086aeea2011-05-13 15:57:08 +01005442 Label materialized;
5443 // Registers will be used as follows:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005444 // r6 = literals array.
Ben Murdoch086aeea2011-05-13 15:57:08 +01005445 // r1 = regexp literal.
5446 // r0 = regexp literal clone.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005447 // r2-5 are used as temporaries.
5448 int literal_offset =
5449 FixedArray::OffsetOfElementAt(instr->hydrogen()->literal_index());
5450 __ Move(r6, instr->hydrogen()->literals());
5451 __ ldr(r1, FieldMemOperand(r6, literal_offset));
Ben Murdoch086aeea2011-05-13 15:57:08 +01005452 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
5453 __ cmp(r1, ip);
5454 __ b(ne, &materialized);
5455
5456 // Create regexp literal using runtime function
5457 // Result will be in r0.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005458 __ mov(r5, Operand(Smi::FromInt(instr->hydrogen()->literal_index())));
5459 __ mov(r4, Operand(instr->hydrogen()->pattern()));
5460 __ mov(r3, Operand(instr->hydrogen()->flags()));
5461 __ Push(r6, r5, r4, r3);
Ben Murdoch086aeea2011-05-13 15:57:08 +01005462 CallRuntime(Runtime::kMaterializeRegExpLiteral, 4, instr);
5463 __ mov(r1, r0);
5464
5465 __ bind(&materialized);
5466 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
5467 Label allocated, runtime_allocate;
5468
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005469 __ Allocate(size, r0, r2, r3, &runtime_allocate, TAG_OBJECT);
Ben Murdoch086aeea2011-05-13 15:57:08 +01005470 __ jmp(&allocated);
5471
5472 __ bind(&runtime_allocate);
5473 __ mov(r0, Operand(Smi::FromInt(size)));
5474 __ Push(r1, r0);
5475 CallRuntime(Runtime::kAllocateInNewSpace, 1, instr);
5476 __ pop(r1);
5477
5478 __ bind(&allocated);
5479 // Copy the content into the newly allocated memory.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005480 __ CopyFields(r0, r1, double_scratch0(), size / kPointerSize);
Ben Murdochb0fe1622011-05-05 13:52:32 +01005481}
5482
5483
5484void LCodeGen::DoFunctionLiteral(LFunctionLiteral* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005485 DCHECK(ToRegister(instr->context()).is(cp));
Ben Murdoch086aeea2011-05-13 15:57:08 +01005486 // Use the fast case closure allocation code that allocates in new
5487 // space for nested functions that don't need literals cloning.
Steve Block1e0659c2011-05-24 12:43:12 +01005488 bool pretenure = instr->hydrogen()->pretenure();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005489 if (!pretenure && instr->hydrogen()->has_no_literals()) {
5490 FastNewClosureStub stub(isolate(), instr->hydrogen()->strict_mode(),
5491 instr->hydrogen()->kind());
5492 __ mov(r2, Operand(instr->hydrogen()->shared_info()));
Ben Murdoch086aeea2011-05-13 15:57:08 +01005493 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
5494 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005495 __ mov(r2, Operand(instr->hydrogen()->shared_info()));
5496 __ mov(r1, Operand(pretenure ? factory()->true_value()
5497 : factory()->false_value()));
Ben Murdoch086aeea2011-05-13 15:57:08 +01005498 __ Push(cp, r2, r1);
5499 CallRuntime(Runtime::kNewClosure, 3, instr);
5500 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01005501}
5502
5503
5504void LCodeGen::DoTypeof(LTypeof* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005505 Register input = ToRegister(instr->value());
Ben Murdoch086aeea2011-05-13 15:57:08 +01005506 __ push(input);
5507 CallRuntime(Runtime::kTypeof, 1, instr);
Ben Murdochb0fe1622011-05-05 13:52:32 +01005508}
5509
5510
Ben Murdochb0fe1622011-05-05 13:52:32 +01005511void LCodeGen::DoTypeofIsAndBranch(LTypeofIsAndBranch* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005512 Register input = ToRegister(instr->value());
Ben Murdochb0fe1622011-05-05 13:52:32 +01005513
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005514 Condition final_branch_condition = EmitTypeofIs(instr->TrueLabel(chunk_),
5515 instr->FalseLabel(chunk_),
Ben Murdochb0fe1622011-05-05 13:52:32 +01005516 input,
5517 instr->type_literal());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005518 if (final_branch_condition != kNoCondition) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005519 EmitBranch(instr, final_branch_condition);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005520 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01005521}
5522
5523
5524Condition LCodeGen::EmitTypeofIs(Label* true_label,
5525 Label* false_label,
5526 Register input,
5527 Handle<String> type_name) {
Steve Block1e0659c2011-05-24 12:43:12 +01005528 Condition final_branch_condition = kNoCondition;
Steve Block9fac8402011-05-12 15:51:54 +01005529 Register scratch = scratch0();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005530 Factory* factory = isolate()->factory();
5531 if (String::Equals(type_name, factory->number_string())) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005532 __ JumpIfSmi(input, true_label);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005533 __ ldr(scratch, FieldMemOperand(input, HeapObject::kMapOffset));
5534 __ CompareRoot(scratch, Heap::kHeapNumberMapRootIndex);
Ben Murdochb0fe1622011-05-05 13:52:32 +01005535 final_branch_condition = eq;
5536
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005537 } else if (String::Equals(type_name, factory->string_string())) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005538 __ JumpIfSmi(input, false_label);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005539 __ CompareObjectType(input, scratch, no_reg, FIRST_NONSTRING_TYPE);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005540 __ b(ge, false_label);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005541 __ ldrb(scratch, FieldMemOperand(scratch, Map::kBitFieldOffset));
5542 __ tst(scratch, Operand(1 << Map::kIsUndetectable));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005543 final_branch_condition = eq;
Ben Murdochb0fe1622011-05-05 13:52:32 +01005544
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005545 } else if (String::Equals(type_name, factory->symbol_string())) {
5546 __ JumpIfSmi(input, false_label);
5547 __ CompareObjectType(input, scratch, no_reg, SYMBOL_TYPE);
5548 final_branch_condition = eq;
5549
5550 } else if (String::Equals(type_name, factory->boolean_string())) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005551 __ CompareRoot(input, Heap::kTrueValueRootIndex);
Ben Murdochb0fe1622011-05-05 13:52:32 +01005552 __ b(eq, true_label);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005553 __ CompareRoot(input, Heap::kFalseValueRootIndex);
Ben Murdochb0fe1622011-05-05 13:52:32 +01005554 final_branch_condition = eq;
5555
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005556 } else if (String::Equals(type_name, factory->undefined_string())) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005557 __ CompareRoot(input, Heap::kUndefinedValueRootIndex);
Ben Murdochb0fe1622011-05-05 13:52:32 +01005558 __ b(eq, true_label);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005559 __ JumpIfSmi(input, false_label);
Ben Murdochb0fe1622011-05-05 13:52:32 +01005560 // Check for undetectable objects => true.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005561 __ ldr(scratch, FieldMemOperand(input, HeapObject::kMapOffset));
5562 __ ldrb(scratch, FieldMemOperand(scratch, Map::kBitFieldOffset));
5563 __ tst(scratch, Operand(1 << Map::kIsUndetectable));
Ben Murdochb0fe1622011-05-05 13:52:32 +01005564 final_branch_condition = ne;
5565
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005566 } else if (String::Equals(type_name, factory->function_string())) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005567 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005568 Register type_reg = scratch;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005569 __ JumpIfSmi(input, false_label);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005570 __ CompareObjectType(input, scratch, type_reg, JS_FUNCTION_TYPE);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005571 __ b(eq, true_label);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005572 __ cmp(type_reg, Operand(JS_FUNCTION_PROXY_TYPE));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005573 final_branch_condition = eq;
Ben Murdochb0fe1622011-05-05 13:52:32 +01005574
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005575 } else if (String::Equals(type_name, factory->object_string())) {
5576 Register map = scratch;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005577 __ JumpIfSmi(input, false_label);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005578 __ CompareRoot(input, Heap::kNullValueRootIndex);
5579 __ b(eq, true_label);
5580 __ CheckObjectTypeRange(input,
5581 map,
5582 FIRST_NONCALLABLE_SPEC_OBJECT_TYPE,
5583 LAST_NONCALLABLE_SPEC_OBJECT_TYPE,
5584 false_label);
Ben Murdochb0fe1622011-05-05 13:52:32 +01005585 // Check for undetectable objects => false.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005586 __ ldrb(scratch, FieldMemOperand(map, Map::kBitFieldOffset));
5587 __ tst(scratch, Operand(1 << Map::kIsUndetectable));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005588 final_branch_condition = eq;
Ben Murdochb0fe1622011-05-05 13:52:32 +01005589
5590 } else {
Ben Murdochb0fe1622011-05-05 13:52:32 +01005591 __ b(false_label);
Ben Murdochb0fe1622011-05-05 13:52:32 +01005592 }
5593
5594 return final_branch_condition;
5595}
5596
5597
Steve Block1e0659c2011-05-24 12:43:12 +01005598void LCodeGen::DoIsConstructCallAndBranch(LIsConstructCallAndBranch* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005599 Register temp1 = ToRegister(instr->temp());
Steve Block1e0659c2011-05-24 12:43:12 +01005600
5601 EmitIsConstructCall(temp1, scratch0());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005602 EmitBranch(instr, eq);
Steve Block1e0659c2011-05-24 12:43:12 +01005603}
5604
5605
5606void LCodeGen::EmitIsConstructCall(Register temp1, Register temp2) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005607 DCHECK(!temp1.is(temp2));
Steve Block1e0659c2011-05-24 12:43:12 +01005608 // Get the frame pointer for the calling frame.
5609 __ ldr(temp1, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
5610
5611 // Skip the arguments adaptor frame if it exists.
Steve Block1e0659c2011-05-24 12:43:12 +01005612 __ ldr(temp2, MemOperand(temp1, StandardFrameConstants::kContextOffset));
5613 __ cmp(temp2, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005614 __ ldr(temp1, MemOperand(temp1, StandardFrameConstants::kCallerFPOffset), eq);
Steve Block1e0659c2011-05-24 12:43:12 +01005615
5616 // Check the marker in the calling frame.
Steve Block1e0659c2011-05-24 12:43:12 +01005617 __ ldr(temp1, MemOperand(temp1, StandardFrameConstants::kMarkerOffset));
5618 __ cmp(temp1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
5619}
5620
5621
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005622void LCodeGen::EnsureSpaceForLazyDeopt(int space_needed) {
5623 if (!info()->IsStub()) {
5624 // Ensure that we have enough space after the previous lazy-bailout
5625 // instruction for patching the code here.
5626 int current_pc = masm()->pc_offset();
5627 if (current_pc < last_lazy_deopt_pc_ + space_needed) {
5628 // Block literal pool emission for duration of padding.
5629 Assembler::BlockConstPoolScope block_const_pool(masm());
5630 int padding_size = last_lazy_deopt_pc_ + space_needed - current_pc;
5631 DCHECK_EQ(0, padding_size % Assembler::kInstrSize);
5632 while (padding_size > 0) {
5633 __ nop();
5634 padding_size -= Assembler::kInstrSize;
5635 }
Ben Murdoch2b4ba112012-01-20 14:57:15 +00005636 }
5637 }
5638 last_lazy_deopt_pc_ = masm()->pc_offset();
5639}
5640
5641
Ben Murdochb0fe1622011-05-05 13:52:32 +01005642void LCodeGen::DoLazyBailout(LLazyBailout* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005643 last_lazy_deopt_pc_ = masm()->pc_offset();
5644 DCHECK(instr->HasEnvironment());
Ben Murdoch2b4ba112012-01-20 14:57:15 +00005645 LEnvironment* env = instr->environment();
5646 RegisterEnvironmentForDeoptimization(env, Safepoint::kLazyDeopt);
5647 safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
Ben Murdochb0fe1622011-05-05 13:52:32 +01005648}
5649
5650
5651void LCodeGen::DoDeoptimize(LDeoptimize* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005652 Deoptimizer::BailoutType type = instr->hydrogen()->type();
5653 // TODO(danno): Stubs expect all deopts to be lazy for historical reasons (the
5654 // needed return address), even though the implementation of LAZY and EAGER is
5655 // now identical. When LAZY is eventually completely folded into EAGER, remove
5656 // the special case below.
5657 if (info()->IsStub() && type == Deoptimizer::EAGER) {
5658 type = Deoptimizer::LAZY;
5659 }
5660
5661 DeoptimizeIf(al, instr, instr->hydrogen()->reason(), type);
Ben Murdochb0fe1622011-05-05 13:52:32 +01005662}
5663
5664
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005665void LCodeGen::DoDummy(LDummy* instr) {
5666 // Nothing to see here, move on!
Ben Murdoch257744e2011-11-30 15:57:28 +00005667}
5668
5669
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005670void LCodeGen::DoDummyUse(LDummyUse* instr) {
5671 // Nothing to see here, move on!
Ben Murdochb0fe1622011-05-05 13:52:32 +01005672}
5673
5674
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005675void LCodeGen::DoDeferredStackCheck(LStackCheck* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005676 PushSafepointRegistersScope scope(this);
5677 LoadContextFromDeferred(instr->context());
Ben Murdoch2b4ba112012-01-20 14:57:15 +00005678 __ CallRuntimeSaveDoubles(Runtime::kStackGuard);
5679 RecordSafepointWithLazyDeopt(
5680 instr, RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005681 DCHECK(instr->HasEnvironment());
Ben Murdoch2b4ba112012-01-20 14:57:15 +00005682 LEnvironment* env = instr->environment();
5683 safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005684}
5685
5686
Ben Murdochb0fe1622011-05-05 13:52:32 +01005687void LCodeGen::DoStackCheck(LStackCheck* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005688 class DeferredStackCheck FINAL : public LDeferredCode {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005689 public:
5690 DeferredStackCheck(LCodeGen* codegen, LStackCheck* instr)
5691 : LDeferredCode(codegen), instr_(instr) { }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005692 virtual void Generate() OVERRIDE {
5693 codegen()->DoDeferredStackCheck(instr_);
5694 }
5695 virtual LInstruction* instr() OVERRIDE { return instr_; }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005696 private:
5697 LStackCheck* instr_;
5698 };
5699
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005700 DCHECK(instr->HasEnvironment());
Ben Murdoch2b4ba112012-01-20 14:57:15 +00005701 LEnvironment* env = instr->environment();
5702 // There is no LLazyBailout instruction for stack-checks. We have to
5703 // prepare for lazy deoptimization explicitly here.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005704 if (instr->hydrogen()->is_function_entry()) {
5705 // Perform stack overflow check.
5706 Label done;
5707 __ LoadRoot(ip, Heap::kStackLimitRootIndex);
5708 __ cmp(sp, Operand(ip));
5709 __ b(hs, &done);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005710 Handle<Code> stack_check = isolate()->builtins()->StackCheck();
5711 PredictableCodeSizeScope predictable(masm(),
5712 CallCodeSize(stack_check, RelocInfo::CODE_TARGET));
5713 DCHECK(instr->context()->IsRegister());
5714 DCHECK(ToRegister(instr->context()).is(cp));
5715 CallCode(stack_check, RelocInfo::CODE_TARGET, instr);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005716 __ bind(&done);
5717 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005718 DCHECK(instr->hydrogen()->is_backwards_branch());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005719 // Perform stack overflow check if this goto needs it before jumping.
5720 DeferredStackCheck* deferred_stack_check =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005721 new(zone()) DeferredStackCheck(this, instr);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005722 __ LoadRoot(ip, Heap::kStackLimitRootIndex);
5723 __ cmp(sp, Operand(ip));
5724 __ b(lo, deferred_stack_check->entry());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005725 EnsureSpaceForLazyDeopt(Deoptimizer::patch_size());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005726 __ bind(instr->done_label());
5727 deferred_stack_check->SetExit(instr->done_label());
Ben Murdoch2b4ba112012-01-20 14:57:15 +00005728 RegisterEnvironmentForDeoptimization(env, Safepoint::kLazyDeopt);
5729 // Don't record a deoptimization index for the safepoint here.
5730 // This will be done explicitly when emitting call and the safepoint in
5731 // the deferred code.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005732 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01005733}
5734
5735
5736void LCodeGen::DoOsrEntry(LOsrEntry* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01005737 // This is a pseudo-instruction that ensures that the environment here is
5738 // properly registered for deoptimization and records the assembler's PC
5739 // offset.
5740 LEnvironment* environment = instr->environment();
Steve Block1e0659c2011-05-24 12:43:12 +01005741
5742 // If the environment were already registered, we would have no way of
5743 // backpatching it with the spill slot operands.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005744 DCHECK(!environment->HasBeenRegistered());
Ben Murdoch2b4ba112012-01-20 14:57:15 +00005745 RegisterEnvironmentForDeoptimization(environment, Safepoint::kNoLazyDeopt);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005746
5747 GenerateOsrPrologue();
Ben Murdochb0fe1622011-05-05 13:52:32 +01005748}
5749
5750
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005751void LCodeGen::DoForInPrepareMap(LForInPrepareMap* instr) {
5752 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
5753 __ cmp(r0, ip);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005754 DeoptimizeIf(eq, instr);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005755
5756 Register null_value = r5;
5757 __ LoadRoot(null_value, Heap::kNullValueRootIndex);
5758 __ cmp(r0, null_value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005759 DeoptimizeIf(eq, instr);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005760
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005761 __ SmiTst(r0);
5762 DeoptimizeIf(eq, instr);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005763
5764 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
5765 __ CompareObjectType(r0, r1, r1, LAST_JS_PROXY_TYPE);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005766 DeoptimizeIf(le, instr);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005767
5768 Label use_cache, call_runtime;
5769 __ CheckEnumCache(null_value, &call_runtime);
5770
5771 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
5772 __ b(&use_cache);
5773
5774 // Get the set of properties to enumerate.
5775 __ bind(&call_runtime);
5776 __ push(r0);
5777 CallRuntime(Runtime::kGetPropertyNamesFast, 1, instr);
5778
5779 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
5780 __ LoadRoot(ip, Heap::kMetaMapRootIndex);
5781 __ cmp(r1, ip);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005782 DeoptimizeIf(ne, instr);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005783 __ bind(&use_cache);
5784}
5785
5786
5787void LCodeGen::DoForInCacheArray(LForInCacheArray* instr) {
5788 Register map = ToRegister(instr->map());
5789 Register result = ToRegister(instr->result());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005790 Label load_cache, done;
5791 __ EnumLength(result, map);
5792 __ cmp(result, Operand(Smi::FromInt(0)));
5793 __ b(ne, &load_cache);
5794 __ mov(result, Operand(isolate()->factory()->empty_fixed_array()));
5795 __ jmp(&done);
5796
5797 __ bind(&load_cache);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005798 __ LoadInstanceDescriptors(map, result);
5799 __ ldr(result,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005800 FieldMemOperand(result, DescriptorArray::kEnumCacheOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005801 __ ldr(result,
5802 FieldMemOperand(result, FixedArray::SizeFor(instr->idx())));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005803 __ cmp(result, Operand::Zero());
5804 DeoptimizeIf(eq, instr);
5805
5806 __ bind(&done);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005807}
5808
5809
5810void LCodeGen::DoCheckMapValue(LCheckMapValue* instr) {
5811 Register object = ToRegister(instr->value());
5812 Register map = ToRegister(instr->map());
5813 __ ldr(scratch0(), FieldMemOperand(object, HeapObject::kMapOffset));
5814 __ cmp(map, scratch0());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005815 DeoptimizeIf(ne, instr);
5816}
5817
5818
5819void LCodeGen::DoDeferredLoadMutableDouble(LLoadFieldByIndex* instr,
5820 Register result,
5821 Register object,
5822 Register index) {
5823 PushSafepointRegistersScope scope(this);
5824 __ Push(object);
5825 __ Push(index);
5826 __ mov(cp, Operand::Zero());
5827 __ CallRuntimeSaveDoubles(Runtime::kLoadMutableDouble);
5828 RecordSafepointWithRegisters(
5829 instr->pointer_map(), 2, Safepoint::kNoLazyDeopt);
5830 __ StoreToSafepointRegisterSlot(r0, result);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005831}
5832
5833
5834void LCodeGen::DoLoadFieldByIndex(LLoadFieldByIndex* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005835 class DeferredLoadMutableDouble FINAL : public LDeferredCode {
5836 public:
5837 DeferredLoadMutableDouble(LCodeGen* codegen,
5838 LLoadFieldByIndex* instr,
5839 Register result,
5840 Register object,
5841 Register index)
5842 : LDeferredCode(codegen),
5843 instr_(instr),
5844 result_(result),
5845 object_(object),
5846 index_(index) {
5847 }
5848 virtual void Generate() OVERRIDE {
5849 codegen()->DoDeferredLoadMutableDouble(instr_, result_, object_, index_);
5850 }
5851 virtual LInstruction* instr() OVERRIDE { return instr_; }
5852 private:
5853 LLoadFieldByIndex* instr_;
5854 Register result_;
5855 Register object_;
5856 Register index_;
5857 };
5858
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005859 Register object = ToRegister(instr->object());
5860 Register index = ToRegister(instr->index());
5861 Register result = ToRegister(instr->result());
5862 Register scratch = scratch0();
5863
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005864 DeferredLoadMutableDouble* deferred;
5865 deferred = new(zone()) DeferredLoadMutableDouble(
5866 this, instr, result, object, index);
5867
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005868 Label out_of_object, done;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005869
5870 __ tst(index, Operand(Smi::FromInt(1)));
5871 __ b(ne, deferred->entry());
5872 __ mov(index, Operand(index, ASR, 1));
5873
5874 __ cmp(index, Operand::Zero());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005875 __ b(lt, &out_of_object);
5876
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005877 __ add(scratch, object, Operand::PointerOffsetFromSmiKey(index));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005878 __ ldr(result, FieldMemOperand(scratch, JSObject::kHeaderSize));
5879
5880 __ b(&done);
5881
5882 __ bind(&out_of_object);
5883 __ ldr(result, FieldMemOperand(object, JSObject::kPropertiesOffset));
5884 // Index is equal to negated out of object property index plus 1.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005885 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize < kPointerSizeLog2);
5886 __ sub(scratch, result, Operand::PointerOffsetFromSmiKey(index));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005887 __ ldr(result, FieldMemOperand(scratch,
5888 FixedArray::kHeaderSize - kPointerSize));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005889 __ bind(deferred->exit());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005890 __ bind(&done);
5891}
Ben Murdoch257744e2011-11-30 15:57:28 +00005892
5893
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005894void LCodeGen::DoStoreFrameContext(LStoreFrameContext* instr) {
5895 Register context = ToRegister(instr->context());
5896 __ str(context, MemOperand(fp, StandardFrameConstants::kContextOffset));
5897}
5898
5899
5900void LCodeGen::DoAllocateBlockContext(LAllocateBlockContext* instr) {
5901 Handle<ScopeInfo> scope_info = instr->scope_info();
5902 __ Push(scope_info);
5903 __ push(ToRegister(instr->function()));
5904 CallRuntime(Runtime::kPushBlockContext, 2, instr);
5905 RecordSafepoint(Safepoint::kNoLazyDeopt);
5906}
5907
5908
Ben Murdochb0fe1622011-05-05 13:52:32 +01005909#undef __
5910
5911} } // namespace v8::internal