blob: 76c8443e7c30cc422154eab202c61dba16e5941e [file] [log] [blame]
Ben Murdochc7cc0282012-03-05 14:35:55 +00001// Copyright 2012 the V8 project authors. All rights reserved.
Ben Murdochb0fe1622011-05-05 13:52:32 +01002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
Steve Block44f0eee2011-05-26 01:26:41 +010028#include "v8.h"
29
Ben Murdochb0fe1622011-05-05 13:52:32 +010030#include "arm/lithium-codegen-arm.h"
Ben Murdoche0cee9b2011-05-25 10:26:03 +010031#include "arm/lithium-gap-resolver-arm.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010032#include "code-stubs.h"
33#include "stub-cache.h"
34
35namespace v8 {
36namespace internal {
37
38
Steve Block44f0eee2011-05-26 01:26:41 +010039class SafepointGenerator : public CallWrapper {
Ben Murdochb0fe1622011-05-05 13:52:32 +010040 public:
41 SafepointGenerator(LCodeGen* codegen,
42 LPointerMap* pointers,
Ben Murdoch2b4ba112012-01-20 14:57:15 +000043 Safepoint::DeoptMode mode)
Ben Murdochb0fe1622011-05-05 13:52:32 +010044 : codegen_(codegen),
45 pointers_(pointers),
Ben Murdoch2b4ba112012-01-20 14:57:15 +000046 deopt_mode_(mode) { }
Ben Murdochb0fe1622011-05-05 13:52:32 +010047 virtual ~SafepointGenerator() { }
48
Ben Murdoch2b4ba112012-01-20 14:57:15 +000049 virtual void BeforeCall(int call_size) const { }
Steve Block44f0eee2011-05-26 01:26:41 +010050
Ben Murdoch257744e2011-11-30 15:57:28 +000051 virtual void AfterCall() const {
Ben Murdoch2b4ba112012-01-20 14:57:15 +000052 codegen_->RecordSafepoint(pointers_, deopt_mode_);
Ben Murdochb0fe1622011-05-05 13:52:32 +010053 }
54
55 private:
56 LCodeGen* codegen_;
57 LPointerMap* pointers_;
Ben Murdoch2b4ba112012-01-20 14:57:15 +000058 Safepoint::DeoptMode deopt_mode_;
Ben Murdochb0fe1622011-05-05 13:52:32 +010059};
60
61
62#define __ masm()->
63
64bool LCodeGen::GenerateCode() {
65 HPhase phase("Code generation", chunk());
66 ASSERT(is_unused());
67 status_ = GENERATING;
68 CpuFeatures::Scope scope1(VFP3);
69 CpuFeatures::Scope scope2(ARMv7);
Ben Murdoch592a9fc2012-03-05 11:04:45 +000070
71 CodeStub::GenerateFPStubs();
72
73 // Open a frame scope to indicate that there is a frame on the stack. The
74 // NONE indicates that the scope shouldn't actually generate code to set up
75 // the frame (that is done in GeneratePrologue).
76 FrameScope frame_scope(masm_, StackFrame::NONE);
77
Ben Murdochb0fe1622011-05-05 13:52:32 +010078 return GeneratePrologue() &&
79 GenerateBody() &&
80 GenerateDeferredCode() &&
Ben Murdoch257744e2011-11-30 15:57:28 +000081 GenerateDeoptJumpTable() &&
Ben Murdochb0fe1622011-05-05 13:52:32 +010082 GenerateSafepointTable();
83}
84
85
86void LCodeGen::FinishCode(Handle<Code> code) {
87 ASSERT(is_done());
Ben Murdoch257744e2011-11-30 15:57:28 +000088 code->set_stack_slots(GetStackSlotCount());
Steve Block1e0659c2011-05-24 12:43:12 +010089 code->set_safepoint_table_offset(safepoints_.GetCodeOffset());
Ben Murdochb0fe1622011-05-05 13:52:32 +010090 PopulateDeoptimizationData(code);
91}
92
93
94void LCodeGen::Abort(const char* format, ...) {
95 if (FLAG_trace_bailout) {
Ben Murdoch589d6972011-11-30 16:04:58 +000096 SmartArrayPointer<char> name(
97 info()->shared_info()->DebugName()->ToCString());
Ben Murdoche0cee9b2011-05-25 10:26:03 +010098 PrintF("Aborting LCodeGen in @\"%s\": ", *name);
Ben Murdochb0fe1622011-05-05 13:52:32 +010099 va_list arguments;
100 va_start(arguments, format);
101 OS::VPrint(format, arguments);
102 va_end(arguments);
103 PrintF("\n");
104 }
105 status_ = ABORTED;
106}
107
108
109void LCodeGen::Comment(const char* format, ...) {
110 if (!FLAG_code_comments) return;
111 char buffer[4 * KB];
112 StringBuilder builder(buffer, ARRAY_SIZE(buffer));
113 va_list arguments;
114 va_start(arguments, format);
115 builder.AddFormattedList(format, arguments);
116 va_end(arguments);
117
118 // Copy the string before recording it in the assembler to avoid
119 // issues when the stack allocated buffer goes out of scope.
120 size_t length = builder.position();
121 Vector<char> copy = Vector<char>::New(length + 1);
122 memcpy(copy.start(), builder.Finalize(), copy.length());
123 masm()->RecordComment(copy.start());
124}
125
126
127bool LCodeGen::GeneratePrologue() {
128 ASSERT(is_generating());
129
130#ifdef DEBUG
131 if (strlen(FLAG_stop_at) > 0 &&
132 info_->function()->name()->IsEqualTo(CStrVector(FLAG_stop_at))) {
133 __ stop("stop_at");
134 }
135#endif
136
137 // r1: Callee's JS function.
138 // cp: Callee's context.
139 // fp: Caller's frame pointer.
140 // lr: Caller's pc.
141
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000142 // Strict mode functions and builtins need to replace the receiver
143 // with undefined when called as functions (without an explicit
144 // receiver object). r5 is zero for method calls and non-zero for
145 // function calls.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000146 if (!info_->is_classic_mode() || info_->is_native()) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000147 Label ok;
148 __ cmp(r5, Operand(0));
149 __ b(eq, &ok);
150 int receiver_offset = scope()->num_parameters() * kPointerSize;
151 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
152 __ str(r2, MemOperand(sp, receiver_offset));
153 __ bind(&ok);
154 }
155
Ben Murdochb0fe1622011-05-05 13:52:32 +0100156 __ stm(db_w, sp, r1.bit() | cp.bit() | fp.bit() | lr.bit());
157 __ add(fp, sp, Operand(2 * kPointerSize)); // Adjust FP to point to saved FP.
158
159 // Reserve space for the stack slots needed by the code.
Ben Murdoch257744e2011-11-30 15:57:28 +0000160 int slots = GetStackSlotCount();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100161 if (slots > 0) {
162 if (FLAG_debug_code) {
163 __ mov(r0, Operand(slots));
164 __ mov(r2, Operand(kSlotsZapValue));
165 Label loop;
166 __ bind(&loop);
167 __ push(r2);
168 __ sub(r0, r0, Operand(1), SetCC);
169 __ b(ne, &loop);
170 } else {
171 __ sub(sp, sp, Operand(slots * kPointerSize));
172 }
173 }
174
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100175 // Possibly allocate a local context.
176 int heap_slots = scope()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
177 if (heap_slots > 0) {
178 Comment(";;; Allocate local context");
179 // Argument to NewContext is the function, which is in r1.
180 __ push(r1);
181 if (heap_slots <= FastNewContextStub::kMaximumSlots) {
182 FastNewContextStub stub(heap_slots);
183 __ CallStub(&stub);
184 } else {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000185 __ CallRuntime(Runtime::kNewFunctionContext, 1);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100186 }
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000187 RecordSafepoint(Safepoint::kNoLazyDeopt);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100188 // Context is returned in both r0 and cp. It replaces the context
189 // passed to us. It's saved in the stack and kept live in cp.
190 __ str(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
191 // Copy any necessary parameters into the context.
192 int num_parameters = scope()->num_parameters();
193 for (int i = 0; i < num_parameters; i++) {
Ben Murdoch589d6972011-11-30 16:04:58 +0000194 Variable* var = scope()->parameter(i);
195 if (var->IsContextSlot()) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100196 int parameter_offset = StandardFrameConstants::kCallerSPOffset +
197 (num_parameters - 1 - i) * kPointerSize;
198 // Load parameter from stack.
199 __ ldr(r0, MemOperand(fp, parameter_offset));
200 // Store it in the context.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000201 MemOperand target = ContextOperand(cp, var->index());
202 __ str(r0, target);
203 // Update the write barrier. This clobbers r3 and r0.
204 __ RecordWriteContextSlot(
205 cp, target.offset(), r0, r3, kLRHasBeenSaved, kSaveFPRegs);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100206 }
207 }
208 Comment(";;; End allocate local context");
209 }
210
Ben Murdochb0fe1622011-05-05 13:52:32 +0100211 // Trace the call.
212 if (FLAG_trace) {
213 __ CallRuntime(Runtime::kTraceEnter, 0);
214 }
215 return !is_aborted();
216}
217
218
219bool LCodeGen::GenerateBody() {
220 ASSERT(is_generating());
221 bool emit_instructions = true;
222 for (current_instruction_ = 0;
223 !is_aborted() && current_instruction_ < instructions_->length();
224 current_instruction_++) {
225 LInstruction* instr = instructions_->at(current_instruction_);
226 if (instr->IsLabel()) {
227 LLabel* label = LLabel::cast(instr);
228 emit_instructions = !label->HasReplacement();
229 }
230
231 if (emit_instructions) {
232 Comment(";;; @%d: %s.", current_instruction_, instr->Mnemonic());
233 instr->CompileToNative(this);
234 }
235 }
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000236 EnsureSpaceForLazyDeopt();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100237 return !is_aborted();
238}
239
240
Ben Murdochb0fe1622011-05-05 13:52:32 +0100241bool LCodeGen::GenerateDeferredCode() {
242 ASSERT(is_generating());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000243 if (deferred_.length() > 0) {
244 for (int i = 0; !is_aborted() && i < deferred_.length(); i++) {
245 LDeferredCode* code = deferred_[i];
246 __ bind(code->entry());
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000247 Comment(";;; Deferred code @%d: %s.",
248 code->instruction_index(),
249 code->instr()->Mnemonic());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000250 code->Generate();
251 __ jmp(code->exit());
252 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100253 }
254
Ben Murdoch257744e2011-11-30 15:57:28 +0000255 // Force constant pool emission at the end of the deferred code to make
256 // sure that no constant pools are emitted after.
Ben Murdochb8e0da22011-05-16 14:20:40 +0100257 masm()->CheckConstPool(true, false);
258
Ben Murdoch257744e2011-11-30 15:57:28 +0000259 return !is_aborted();
260}
261
262
263bool LCodeGen::GenerateDeoptJumpTable() {
264 // Check that the jump table is accessible from everywhere in the function
Ben Murdochc7cc0282012-03-05 14:35:55 +0000265 // code, i.e. that offsets to the table can be encoded in the 24bit signed
Ben Murdoch257744e2011-11-30 15:57:28 +0000266 // immediate of a branch instruction.
267 // To simplify we consider the code size from the first instruction to the
268 // end of the jump table. We also don't consider the pc load delta.
269 // Each entry in the jump table generates one instruction and inlines one
270 // 32bit data after it.
271 if (!is_int24((masm()->pc_offset() / Assembler::kInstrSize) +
272 deopt_jump_table_.length() * 2)) {
273 Abort("Generated code is too large");
274 }
275
276 // Block the constant pool emission during the jump table emission.
277 __ BlockConstPoolFor(deopt_jump_table_.length());
278 __ RecordComment("[ Deoptimisation jump table");
279 Label table_start;
280 __ bind(&table_start);
281 for (int i = 0; i < deopt_jump_table_.length(); i++) {
282 __ bind(&deopt_jump_table_[i].label);
283 __ ldr(pc, MemOperand(pc, Assembler::kInstrSize - Assembler::kPcLoadDelta));
284 __ dd(reinterpret_cast<uint32_t>(deopt_jump_table_[i].address));
285 }
286 ASSERT(masm()->InstructionsGeneratedSince(&table_start) ==
287 deopt_jump_table_.length() * 2);
288 __ RecordComment("]");
289
290 // The deoptimization jump table is the last part of the instruction
291 // sequence. Mark the generated code as done unless we bailed out.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100292 if (!is_aborted()) status_ = DONE;
293 return !is_aborted();
294}
295
296
297bool LCodeGen::GenerateSafepointTable() {
298 ASSERT(is_done());
Ben Murdoch257744e2011-11-30 15:57:28 +0000299 safepoints_.Emit(masm(), GetStackSlotCount());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100300 return !is_aborted();
301}
302
303
304Register LCodeGen::ToRegister(int index) const {
305 return Register::FromAllocationIndex(index);
306}
307
308
309DoubleRegister LCodeGen::ToDoubleRegister(int index) const {
310 return DoubleRegister::FromAllocationIndex(index);
311}
312
313
314Register LCodeGen::ToRegister(LOperand* op) const {
315 ASSERT(op->IsRegister());
316 return ToRegister(op->index());
317}
318
319
320Register LCodeGen::EmitLoadRegister(LOperand* op, Register scratch) {
321 if (op->IsRegister()) {
322 return ToRegister(op->index());
323 } else if (op->IsConstantOperand()) {
Ben Murdochc7cc0282012-03-05 14:35:55 +0000324 LConstantOperand* const_op = LConstantOperand::cast(op);
325 Handle<Object> literal = chunk_->LookupLiteral(const_op);
326 Representation r = chunk_->LookupLiteralRepresentation(const_op);
327 if (r.IsInteger32()) {
328 ASSERT(literal->IsNumber());
329 __ mov(scratch, Operand(static_cast<int32_t>(literal->Number())));
330 } else if (r.IsDouble()) {
331 Abort("EmitLoadRegister: Unsupported double immediate.");
332 } else {
333 ASSERT(r.IsTagged());
334 if (literal->IsSmi()) {
335 __ mov(scratch, Operand(literal));
336 } else {
337 __ LoadHeapObject(scratch, Handle<HeapObject>::cast(literal));
338 }
339 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100340 return scratch;
341 } else if (op->IsStackSlot() || op->IsArgument()) {
342 __ ldr(scratch, ToMemOperand(op));
343 return scratch;
344 }
345 UNREACHABLE();
346 return scratch;
347}
348
349
350DoubleRegister LCodeGen::ToDoubleRegister(LOperand* op) const {
351 ASSERT(op->IsDoubleRegister());
352 return ToDoubleRegister(op->index());
353}
354
355
356DoubleRegister LCodeGen::EmitLoadDoubleRegister(LOperand* op,
357 SwVfpRegister flt_scratch,
358 DoubleRegister dbl_scratch) {
359 if (op->IsDoubleRegister()) {
360 return ToDoubleRegister(op->index());
361 } else if (op->IsConstantOperand()) {
362 LConstantOperand* const_op = LConstantOperand::cast(op);
363 Handle<Object> literal = chunk_->LookupLiteral(const_op);
364 Representation r = chunk_->LookupLiteralRepresentation(const_op);
365 if (r.IsInteger32()) {
366 ASSERT(literal->IsNumber());
367 __ mov(ip, Operand(static_cast<int32_t>(literal->Number())));
368 __ vmov(flt_scratch, ip);
369 __ vcvt_f64_s32(dbl_scratch, flt_scratch);
370 return dbl_scratch;
371 } else if (r.IsDouble()) {
372 Abort("unsupported double immediate");
373 } else if (r.IsTagged()) {
374 Abort("unsupported tagged immediate");
375 }
376 } else if (op->IsStackSlot() || op->IsArgument()) {
377 // TODO(regis): Why is vldr not taking a MemOperand?
378 // __ vldr(dbl_scratch, ToMemOperand(op));
379 MemOperand mem_op = ToMemOperand(op);
380 __ vldr(dbl_scratch, mem_op.rn(), mem_op.offset());
381 return dbl_scratch;
382 }
383 UNREACHABLE();
384 return dbl_scratch;
385}
386
387
Ben Murdochc7cc0282012-03-05 14:35:55 +0000388Handle<Object> LCodeGen::ToHandle(LConstantOperand* op) const {
389 Handle<Object> literal = chunk_->LookupLiteral(op);
390 ASSERT(chunk_->LookupLiteralRepresentation(op).IsTagged());
391 return literal;
392}
393
394
395bool LCodeGen::IsInteger32(LConstantOperand* op) const {
396 return chunk_->LookupLiteralRepresentation(op).IsInteger32();
397}
398
399
Ben Murdochb0fe1622011-05-05 13:52:32 +0100400int LCodeGen::ToInteger32(LConstantOperand* op) const {
401 Handle<Object> value = chunk_->LookupLiteral(op);
402 ASSERT(chunk_->LookupLiteralRepresentation(op).IsInteger32());
403 ASSERT(static_cast<double>(static_cast<int32_t>(value->Number())) ==
404 value->Number());
405 return static_cast<int32_t>(value->Number());
406}
407
408
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000409double LCodeGen::ToDouble(LConstantOperand* op) const {
410 Handle<Object> value = chunk_->LookupLiteral(op);
411 return value->Number();
412}
413
414
Ben Murdochb0fe1622011-05-05 13:52:32 +0100415Operand LCodeGen::ToOperand(LOperand* op) {
416 if (op->IsConstantOperand()) {
417 LConstantOperand* const_op = LConstantOperand::cast(op);
418 Handle<Object> literal = chunk_->LookupLiteral(const_op);
419 Representation r = chunk_->LookupLiteralRepresentation(const_op);
420 if (r.IsInteger32()) {
421 ASSERT(literal->IsNumber());
422 return Operand(static_cast<int32_t>(literal->Number()));
423 } else if (r.IsDouble()) {
424 Abort("ToOperand Unsupported double immediate.");
425 }
426 ASSERT(r.IsTagged());
427 return Operand(literal);
428 } else if (op->IsRegister()) {
429 return Operand(ToRegister(op));
430 } else if (op->IsDoubleRegister()) {
431 Abort("ToOperand IsDoubleRegister unimplemented");
432 return Operand(0);
433 }
434 // Stack slots not implemented, use ToMemOperand instead.
435 UNREACHABLE();
436 return Operand(0);
437}
438
439
440MemOperand LCodeGen::ToMemOperand(LOperand* op) const {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100441 ASSERT(!op->IsRegister());
442 ASSERT(!op->IsDoubleRegister());
443 ASSERT(op->IsStackSlot() || op->IsDoubleStackSlot());
444 int index = op->index();
445 if (index >= 0) {
446 // Local or spill slot. Skip the frame pointer, function, and
447 // context in the fixed part of the frame.
448 return MemOperand(fp, -(index + 3) * kPointerSize);
449 } else {
450 // Incoming parameter. Skip the return address.
451 return MemOperand(fp, -(index - 1) * kPointerSize);
452 }
453}
454
455
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100456MemOperand LCodeGen::ToHighMemOperand(LOperand* op) const {
457 ASSERT(op->IsDoubleStackSlot());
458 int index = op->index();
459 if (index >= 0) {
460 // Local or spill slot. Skip the frame pointer, function, context,
461 // and the first word of the double in the fixed part of the frame.
462 return MemOperand(fp, -(index + 3) * kPointerSize + kPointerSize);
463 } else {
464 // Incoming parameter. Skip the return address and the first word of
465 // the double.
466 return MemOperand(fp, -(index - 1) * kPointerSize + kPointerSize);
467 }
468}
469
470
Ben Murdochb8e0da22011-05-16 14:20:40 +0100471void LCodeGen::WriteTranslation(LEnvironment* environment,
472 Translation* translation) {
473 if (environment == NULL) return;
474
475 // The translation includes one command per value in the environment.
476 int translation_size = environment->values()->length();
477 // The output frame height does not include the parameters.
478 int height = translation_size - environment->parameter_count();
479
480 WriteTranslation(environment->outer(), translation);
481 int closure_id = DefineDeoptimizationLiteral(environment->closure());
Ben Murdochc7cc0282012-03-05 14:35:55 +0000482 if (environment->is_arguments_adaptor()) {
483 translation->BeginArgumentsAdaptorFrame(closure_id, translation_size);
484 } else {
485 translation->BeginJSFrame(environment->ast_id(), closure_id, height);
486 }
Ben Murdochb8e0da22011-05-16 14:20:40 +0100487 for (int i = 0; i < translation_size; ++i) {
488 LOperand* value = environment->values()->at(i);
489 // spilled_registers_ and spilled_double_registers_ are either
490 // both NULL or both set.
491 if (environment->spilled_registers() != NULL && value != NULL) {
492 if (value->IsRegister() &&
493 environment->spilled_registers()[value->index()] != NULL) {
494 translation->MarkDuplicate();
495 AddToTranslation(translation,
496 environment->spilled_registers()[value->index()],
497 environment->HasTaggedValueAt(i));
498 } else if (
499 value->IsDoubleRegister() &&
500 environment->spilled_double_registers()[value->index()] != NULL) {
501 translation->MarkDuplicate();
502 AddToTranslation(
503 translation,
504 environment->spilled_double_registers()[value->index()],
505 false);
506 }
507 }
508
509 AddToTranslation(translation, value, environment->HasTaggedValueAt(i));
510 }
511}
512
513
Ben Murdochb0fe1622011-05-05 13:52:32 +0100514void LCodeGen::AddToTranslation(Translation* translation,
515 LOperand* op,
516 bool is_tagged) {
517 if (op == NULL) {
518 // TODO(twuerthinger): Introduce marker operands to indicate that this value
519 // is not present and must be reconstructed from the deoptimizer. Currently
520 // this is only used for the arguments object.
521 translation->StoreArgumentsObject();
522 } else if (op->IsStackSlot()) {
523 if (is_tagged) {
524 translation->StoreStackSlot(op->index());
525 } else {
526 translation->StoreInt32StackSlot(op->index());
527 }
528 } else if (op->IsDoubleStackSlot()) {
529 translation->StoreDoubleStackSlot(op->index());
530 } else if (op->IsArgument()) {
531 ASSERT(is_tagged);
Ben Murdoch257744e2011-11-30 15:57:28 +0000532 int src_index = GetStackSlotCount() + op->index();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100533 translation->StoreStackSlot(src_index);
534 } else if (op->IsRegister()) {
535 Register reg = ToRegister(op);
536 if (is_tagged) {
537 translation->StoreRegister(reg);
538 } else {
539 translation->StoreInt32Register(reg);
540 }
541 } else if (op->IsDoubleRegister()) {
542 DoubleRegister reg = ToDoubleRegister(op);
543 translation->StoreDoubleRegister(reg);
544 } else if (op->IsConstantOperand()) {
545 Handle<Object> literal = chunk()->LookupLiteral(LConstantOperand::cast(op));
546 int src_index = DefineDeoptimizationLiteral(literal);
547 translation->StoreLiteral(src_index);
548 } else {
549 UNREACHABLE();
550 }
551}
552
553
554void LCodeGen::CallCode(Handle<Code> code,
555 RelocInfo::Mode mode,
556 LInstruction* instr) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100557 CallCodeGeneric(code, mode, instr, RECORD_SIMPLE_SAFEPOINT);
558}
559
560
561void LCodeGen::CallCodeGeneric(Handle<Code> code,
562 RelocInfo::Mode mode,
563 LInstruction* instr,
564 SafepointMode safepoint_mode) {
Steve Block1e0659c2011-05-24 12:43:12 +0100565 ASSERT(instr != NULL);
566 LPointerMap* pointers = instr->pointer_map();
567 RecordPosition(pointers->position());
568 __ Call(code, mode);
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000569 RecordSafepointWithLazyDeopt(instr, safepoint_mode);
Ben Murdoch18a6f572011-07-25 17:16:09 +0100570
571 // Signal that we don't inline smi code before these stubs in the
572 // optimizing code generator.
Ben Murdoch257744e2011-11-30 15:57:28 +0000573 if (code->kind() == Code::BINARY_OP_IC ||
Ben Murdoch18a6f572011-07-25 17:16:09 +0100574 code->kind() == Code::COMPARE_IC) {
575 __ nop();
576 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100577}
578
579
Steve Block44f0eee2011-05-26 01:26:41 +0100580void LCodeGen::CallRuntime(const Runtime::Function* function,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100581 int num_arguments,
582 LInstruction* instr) {
583 ASSERT(instr != NULL);
584 LPointerMap* pointers = instr->pointer_map();
585 ASSERT(pointers != NULL);
586 RecordPosition(pointers->position());
587
588 __ CallRuntime(function, num_arguments);
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000589 RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100590}
591
592
Ben Murdoch8b112d22011-06-08 16:22:53 +0100593void LCodeGen::CallRuntimeFromDeferred(Runtime::FunctionId id,
594 int argc,
595 LInstruction* instr) {
596 __ CallRuntimeSaveDoubles(id);
597 RecordSafepointWithRegisters(
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000598 instr->pointer_map(), argc, Safepoint::kNoLazyDeopt);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100599}
600
601
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000602void LCodeGen::RegisterEnvironmentForDeoptimization(LEnvironment* environment,
603 Safepoint::DeoptMode mode) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100604 if (!environment->HasBeenRegistered()) {
605 // Physical stack frame layout:
606 // -x ............. -4 0 ..................................... y
607 // [incoming arguments] [spill slots] [pushed outgoing arguments]
608
609 // Layout of the environment:
610 // 0 ..................................................... size-1
611 // [parameters] [locals] [expression stack including arguments]
612
613 // Layout of the translation:
614 // 0 ........................................................ size - 1 + 4
615 // [expression stack including arguments] [locals] [4 words] [parameters]
616 // |>------------ translation_size ------------<|
617
618 int frame_count = 0;
Ben Murdochc7cc0282012-03-05 14:35:55 +0000619 int jsframe_count = 0;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100620 for (LEnvironment* e = environment; e != NULL; e = e->outer()) {
621 ++frame_count;
Ben Murdochc7cc0282012-03-05 14:35:55 +0000622 if (!e->is_arguments_adaptor()) {
623 ++jsframe_count;
624 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100625 }
Ben Murdochc7cc0282012-03-05 14:35:55 +0000626 Translation translation(&translations_, frame_count, jsframe_count);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100627 WriteTranslation(environment, &translation);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100628 int deoptimization_index = deoptimizations_.length();
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000629 int pc_offset = masm()->pc_offset();
630 environment->Register(deoptimization_index,
631 translation.index(),
632 (mode == Safepoint::kLazyDeopt) ? pc_offset : -1);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100633 deoptimizations_.Add(environment);
634 }
635}
636
637
638void LCodeGen::DeoptimizeIf(Condition cc, LEnvironment* environment) {
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000639 RegisterEnvironmentForDeoptimization(environment, Safepoint::kNoLazyDeopt);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100640 ASSERT(environment->HasBeenRegistered());
641 int id = environment->deoptimization_index();
642 Address entry = Deoptimizer::GetDeoptimizationEntry(id, Deoptimizer::EAGER);
643 ASSERT(entry != NULL);
644 if (entry == NULL) {
645 Abort("bailout was not prepared");
646 return;
647 }
648
649 ASSERT(FLAG_deopt_every_n_times < 2); // Other values not supported on ARM.
650
651 if (FLAG_deopt_every_n_times == 1 &&
652 info_->shared_info()->opt_count() == id) {
653 __ Jump(entry, RelocInfo::RUNTIME_ENTRY);
654 return;
655 }
656
Ben Murdoch257744e2011-11-30 15:57:28 +0000657 if (FLAG_trap_on_deopt) __ stop("trap_on_deopt", cc);
658
Steve Block1e0659c2011-05-24 12:43:12 +0100659 if (cc == al) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100660 __ Jump(entry, RelocInfo::RUNTIME_ENTRY);
661 } else {
Ben Murdoch257744e2011-11-30 15:57:28 +0000662 // We often have several deopts to the same entry, reuse the last
663 // jump entry if this is the case.
664 if (deopt_jump_table_.is_empty() ||
665 (deopt_jump_table_.last().address != entry)) {
666 deopt_jump_table_.Add(JumpTableEntry(entry));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100667 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000668 __ b(cc, &deopt_jump_table_.last().label);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100669 }
670}
671
672
673void LCodeGen::PopulateDeoptimizationData(Handle<Code> code) {
674 int length = deoptimizations_.length();
675 if (length == 0) return;
676 ASSERT(FLAG_deopt);
677 Handle<DeoptimizationInputData> data =
Steve Block44f0eee2011-05-26 01:26:41 +0100678 factory()->NewDeoptimizationInputData(length, TENURED);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100679
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100680 Handle<ByteArray> translations = translations_.CreateByteArray();
681 data->SetTranslationByteArray(*translations);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100682 data->SetInlinedFunctionCount(Smi::FromInt(inlined_function_count_));
683
684 Handle<FixedArray> literals =
Steve Block44f0eee2011-05-26 01:26:41 +0100685 factory()->NewFixedArray(deoptimization_literals_.length(), TENURED);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100686 for (int i = 0; i < deoptimization_literals_.length(); i++) {
687 literals->set(i, *deoptimization_literals_[i]);
688 }
689 data->SetLiteralArray(*literals);
690
691 data->SetOsrAstId(Smi::FromInt(info_->osr_ast_id()));
692 data->SetOsrPcOffset(Smi::FromInt(osr_pc_offset_));
693
694 // Populate the deoptimization entries.
695 for (int i = 0; i < length; i++) {
696 LEnvironment* env = deoptimizations_[i];
697 data->SetAstId(i, Smi::FromInt(env->ast_id()));
698 data->SetTranslationIndex(i, Smi::FromInt(env->translation_index()));
699 data->SetArgumentsStackHeight(i,
700 Smi::FromInt(env->arguments_stack_height()));
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000701 data->SetPc(i, Smi::FromInt(env->pc_offset()));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100702 }
703 code->set_deoptimization_data(*data);
704}
705
706
707int LCodeGen::DefineDeoptimizationLiteral(Handle<Object> literal) {
708 int result = deoptimization_literals_.length();
709 for (int i = 0; i < deoptimization_literals_.length(); ++i) {
710 if (deoptimization_literals_[i].is_identical_to(literal)) return i;
711 }
712 deoptimization_literals_.Add(literal);
713 return result;
714}
715
716
717void LCodeGen::PopulateDeoptimizationLiteralsWithInlinedFunctions() {
718 ASSERT(deoptimization_literals_.length() == 0);
719
720 const ZoneList<Handle<JSFunction> >* inlined_closures =
721 chunk()->inlined_closures();
722
723 for (int i = 0, length = inlined_closures->length();
724 i < length;
725 i++) {
726 DefineDeoptimizationLiteral(inlined_closures->at(i));
727 }
728
729 inlined_function_count_ = deoptimization_literals_.length();
730}
731
732
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000733void LCodeGen::RecordSafepointWithLazyDeopt(
734 LInstruction* instr, SafepointMode safepoint_mode) {
735 if (safepoint_mode == RECORD_SIMPLE_SAFEPOINT) {
736 RecordSafepoint(instr->pointer_map(), Safepoint::kLazyDeopt);
737 } else {
738 ASSERT(safepoint_mode == RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS);
739 RecordSafepointWithRegisters(
740 instr->pointer_map(), 0, Safepoint::kLazyDeopt);
741 }
742}
743
744
Steve Block1e0659c2011-05-24 12:43:12 +0100745void LCodeGen::RecordSafepoint(
746 LPointerMap* pointers,
747 Safepoint::Kind kind,
748 int arguments,
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000749 Safepoint::DeoptMode deopt_mode) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100750 ASSERT(expected_safepoint_kind_ == kind);
751
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000752 const ZoneList<LOperand*>* operands = pointers->GetNormalizedOperands();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100753 Safepoint safepoint = safepoints_.DefineSafepoint(masm(),
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000754 kind, arguments, deopt_mode);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100755 for (int i = 0; i < operands->length(); i++) {
756 LOperand* pointer = operands->at(i);
757 if (pointer->IsStackSlot()) {
758 safepoint.DefinePointerSlot(pointer->index());
Steve Block1e0659c2011-05-24 12:43:12 +0100759 } else if (pointer->IsRegister() && (kind & Safepoint::kWithRegisters)) {
760 safepoint.DefinePointerRegister(ToRegister(pointer));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100761 }
762 }
Steve Block1e0659c2011-05-24 12:43:12 +0100763 if (kind & Safepoint::kWithRegisters) {
764 // Register cp always contains a pointer to the context.
765 safepoint.DefinePointerRegister(cp);
766 }
767}
768
769
770void LCodeGen::RecordSafepoint(LPointerMap* pointers,
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000771 Safepoint::DeoptMode deopt_mode) {
772 RecordSafepoint(pointers, Safepoint::kSimple, 0, deopt_mode);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100773}
774
775
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000776void LCodeGen::RecordSafepoint(Safepoint::DeoptMode deopt_mode) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100777 LPointerMap empty_pointers(RelocInfo::kNoPosition);
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000778 RecordSafepoint(&empty_pointers, deopt_mode);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100779}
780
781
Ben Murdochb0fe1622011-05-05 13:52:32 +0100782void LCodeGen::RecordSafepointWithRegisters(LPointerMap* pointers,
783 int arguments,
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000784 Safepoint::DeoptMode deopt_mode) {
785 RecordSafepoint(
786 pointers, Safepoint::kWithRegisters, arguments, deopt_mode);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100787}
788
789
Ben Murdochb8e0da22011-05-16 14:20:40 +0100790void LCodeGen::RecordSafepointWithRegistersAndDoubles(
791 LPointerMap* pointers,
792 int arguments,
Ben Murdoch2b4ba112012-01-20 14:57:15 +0000793 Safepoint::DeoptMode deopt_mode) {
794 RecordSafepoint(
795 pointers, Safepoint::kWithRegistersAndDoubles, arguments, deopt_mode);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100796}
797
798
Ben Murdochb0fe1622011-05-05 13:52:32 +0100799void LCodeGen::RecordPosition(int position) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000800 if (position == RelocInfo::kNoPosition) return;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100801 masm()->positions_recorder()->RecordPosition(position);
802}
803
804
805void LCodeGen::DoLabel(LLabel* label) {
806 if (label->is_loop_header()) {
807 Comment(";;; B%d - LOOP entry", label->block_id());
808 } else {
809 Comment(";;; B%d", label->block_id());
810 }
811 __ bind(label->label());
812 current_block_ = label->block_id();
Ben Murdoch257744e2011-11-30 15:57:28 +0000813 DoGap(label);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100814}
815
816
817void LCodeGen::DoParallelMove(LParallelMove* move) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100818 resolver_.Resolve(move);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100819}
820
821
822void LCodeGen::DoGap(LGap* gap) {
823 for (int i = LGap::FIRST_INNER_POSITION;
824 i <= LGap::LAST_INNER_POSITION;
825 i++) {
826 LGap::InnerPosition inner_pos = static_cast<LGap::InnerPosition>(i);
827 LParallelMove* move = gap->GetParallelMove(inner_pos);
828 if (move != NULL) DoParallelMove(move);
829 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100830}
831
832
Ben Murdoch257744e2011-11-30 15:57:28 +0000833void LCodeGen::DoInstructionGap(LInstructionGap* instr) {
834 DoGap(instr);
835}
836
837
Ben Murdochb0fe1622011-05-05 13:52:32 +0100838void LCodeGen::DoParameter(LParameter* instr) {
839 // Nothing to do.
840}
841
842
843void LCodeGen::DoCallStub(LCallStub* instr) {
Steve Block9fac8402011-05-12 15:51:54 +0100844 ASSERT(ToRegister(instr->result()).is(r0));
845 switch (instr->hydrogen()->major_key()) {
846 case CodeStub::RegExpConstructResult: {
847 RegExpConstructResultStub stub;
848 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
849 break;
850 }
851 case CodeStub::RegExpExec: {
852 RegExpExecStub stub;
853 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
854 break;
855 }
856 case CodeStub::SubString: {
857 SubStringStub stub;
858 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
859 break;
860 }
Steve Block9fac8402011-05-12 15:51:54 +0100861 case CodeStub::NumberToString: {
862 NumberToStringStub stub;
863 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
864 break;
865 }
866 case CodeStub::StringAdd: {
867 StringAddStub stub(NO_STRING_ADD_FLAGS);
868 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
869 break;
870 }
871 case CodeStub::StringCompare: {
872 StringCompareStub stub;
873 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
874 break;
875 }
876 case CodeStub::TranscendentalCache: {
Ben Murdochb8e0da22011-05-16 14:20:40 +0100877 __ ldr(r0, MemOperand(sp, 0));
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100878 TranscendentalCacheStub stub(instr->transcendental_type(),
879 TranscendentalCacheStub::TAGGED);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100880 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
Steve Block9fac8402011-05-12 15:51:54 +0100881 break;
882 }
883 default:
884 UNREACHABLE();
885 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100886}
887
888
889void LCodeGen::DoUnknownOSRValue(LUnknownOSRValue* instr) {
890 // Nothing to do.
891}
892
893
894void LCodeGen::DoModI(LModI* instr) {
Steve Block44f0eee2011-05-26 01:26:41 +0100895 if (instr->hydrogen()->HasPowerOf2Divisor()) {
896 Register dividend = ToRegister(instr->InputAt(0));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000897 Register result = ToRegister(instr->result());
Steve Block44f0eee2011-05-26 01:26:41 +0100898
899 int32_t divisor =
900 HConstant::cast(instr->hydrogen()->right())->Integer32Value();
901
902 if (divisor < 0) divisor = -divisor;
903
904 Label positive_dividend, done;
905 __ cmp(dividend, Operand(0));
906 __ b(pl, &positive_dividend);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000907 __ rsb(result, dividend, Operand(0));
908 __ and_(result, result, Operand(divisor - 1), SetCC);
Steve Block44f0eee2011-05-26 01:26:41 +0100909 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000910 DeoptimizeIf(eq, instr->environment());
Ben Murdochb8e0da22011-05-16 14:20:40 +0100911 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000912 __ rsb(result, result, Operand(0));
913 __ b(&done);
Steve Block44f0eee2011-05-26 01:26:41 +0100914 __ bind(&positive_dividend);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000915 __ and_(result, dividend, Operand(divisor - 1));
Steve Block44f0eee2011-05-26 01:26:41 +0100916 __ bind(&done);
917 return;
918 }
919
Ben Murdochb8e0da22011-05-16 14:20:40 +0100920 // These registers hold untagged 32 bit values.
Steve Block1e0659c2011-05-24 12:43:12 +0100921 Register left = ToRegister(instr->InputAt(0));
922 Register right = ToRegister(instr->InputAt(1));
Ben Murdochb8e0da22011-05-16 14:20:40 +0100923 Register result = ToRegister(instr->result());
Ben Murdochb8e0da22011-05-16 14:20:40 +0100924
Steve Block44f0eee2011-05-26 01:26:41 +0100925 Register scratch = scratch0();
926 Register scratch2 = ToRegister(instr->TempAt(0));
927 DwVfpRegister dividend = ToDoubleRegister(instr->TempAt(1));
928 DwVfpRegister divisor = ToDoubleRegister(instr->TempAt(2));
929 DwVfpRegister quotient = double_scratch0();
930
Steve Block44f0eee2011-05-26 01:26:41 +0100931 ASSERT(!dividend.is(divisor));
932 ASSERT(!dividend.is(quotient));
933 ASSERT(!divisor.is(quotient));
934 ASSERT(!scratch.is(left));
935 ASSERT(!scratch.is(right));
936 ASSERT(!scratch.is(result));
937
938 Label done, vfp_modulo, both_positive, right_negative;
939
Ben Murdochb8e0da22011-05-16 14:20:40 +0100940 // Check for x % 0.
941 if (instr->hydrogen()->CheckFlag(HValue::kCanBeDivByZero)) {
Steve Block44f0eee2011-05-26 01:26:41 +0100942 __ cmp(right, Operand(0));
943 DeoptimizeIf(eq, instr->environment());
Ben Murdochb8e0da22011-05-16 14:20:40 +0100944 }
945
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000946 __ Move(result, left);
947
Steve Block44f0eee2011-05-26 01:26:41 +0100948 // (0 % x) must yield 0 (if x is finite, which is the case here).
Steve Block1e0659c2011-05-24 12:43:12 +0100949 __ cmp(left, Operand(0));
Steve Block44f0eee2011-05-26 01:26:41 +0100950 __ b(eq, &done);
951 // Preload right in a vfp register.
952 __ vmov(divisor.low(), right);
953 __ b(lt, &vfp_modulo);
954
955 __ cmp(left, Operand(right));
956 __ b(lt, &done);
957
958 // Check for (positive) power of two on the right hand side.
959 __ JumpIfNotPowerOfTwoOrZeroAndNeg(right,
960 scratch,
961 &right_negative,
962 &both_positive);
963 // Perform modulo operation (scratch contains right - 1).
964 __ and_(result, scratch, Operand(left));
965 __ b(&done);
966
967 __ bind(&right_negative);
968 // Negate right. The sign of the divisor does not matter.
969 __ rsb(right, right, Operand(0));
970
971 __ bind(&both_positive);
972 const int kUnfolds = 3;
Steve Block1e0659c2011-05-24 12:43:12 +0100973 // If the right hand side is smaller than the (nonnegative)
Steve Block44f0eee2011-05-26 01:26:41 +0100974 // left hand side, the left hand side is the result.
975 // Else try a few subtractions of the left hand side.
Steve Block1e0659c2011-05-24 12:43:12 +0100976 __ mov(scratch, left);
977 for (int i = 0; i < kUnfolds; i++) {
978 // Check if the left hand side is less or equal than the
979 // the right hand side.
Steve Block44f0eee2011-05-26 01:26:41 +0100980 __ cmp(scratch, Operand(right));
Steve Block1e0659c2011-05-24 12:43:12 +0100981 __ mov(result, scratch, LeaveCC, lt);
982 __ b(lt, &done);
983 // If not, reduce the left hand side by the right hand
984 // side and check again.
985 if (i < kUnfolds - 1) __ sub(scratch, scratch, right);
986 }
987
Steve Block44f0eee2011-05-26 01:26:41 +0100988 __ bind(&vfp_modulo);
989 // Load the arguments in VFP registers.
990 // The divisor value is preloaded before. Be careful that 'right' is only live
991 // on entry.
992 __ vmov(dividend.low(), left);
993 // From here on don't use right as it may have been reallocated (for example
994 // to scratch2).
995 right = no_reg;
Steve Block1e0659c2011-05-24 12:43:12 +0100996
Steve Block44f0eee2011-05-26 01:26:41 +0100997 __ vcvt_f64_s32(dividend, dividend.low());
998 __ vcvt_f64_s32(divisor, divisor.low());
Ben Murdochb8e0da22011-05-16 14:20:40 +0100999
Steve Block44f0eee2011-05-26 01:26:41 +01001000 // We do not care about the sign of the divisor.
1001 __ vabs(divisor, divisor);
1002 // Compute the quotient and round it to a 32bit integer.
1003 __ vdiv(quotient, dividend, divisor);
1004 __ vcvt_s32_f64(quotient.low(), quotient);
1005 __ vcvt_f64_s32(quotient, quotient.low());
Ben Murdochb8e0da22011-05-16 14:20:40 +01001006
Steve Block44f0eee2011-05-26 01:26:41 +01001007 // Compute the remainder in result.
1008 DwVfpRegister double_scratch = dividend;
1009 __ vmul(double_scratch, divisor, quotient);
1010 __ vcvt_s32_f64(double_scratch.low(), double_scratch);
1011 __ vmov(scratch, double_scratch.low());
Ben Murdochb8e0da22011-05-16 14:20:40 +01001012
Steve Block44f0eee2011-05-26 01:26:41 +01001013 if (!instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
1014 __ sub(result, left, scratch);
1015 } else {
1016 Label ok;
1017 // Check for -0.
1018 __ sub(scratch2, left, scratch, SetCC);
1019 __ b(ne, &ok);
1020 __ cmp(left, Operand(0));
1021 DeoptimizeIf(mi, instr->environment());
1022 __ bind(&ok);
1023 // Load the result and we are done.
1024 __ mov(result, scratch2);
1025 }
1026
Ben Murdochb8e0da22011-05-16 14:20:40 +01001027 __ bind(&done);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001028}
1029
1030
1031void LCodeGen::DoDivI(LDivI* instr) {
Ben Murdochb8e0da22011-05-16 14:20:40 +01001032 class DeferredDivI: public LDeferredCode {
1033 public:
1034 DeferredDivI(LCodeGen* codegen, LDivI* instr)
1035 : LDeferredCode(codegen), instr_(instr) { }
1036 virtual void Generate() {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001037 codegen()->DoDeferredBinaryOpStub(instr_, Token::DIV);
Ben Murdochb8e0da22011-05-16 14:20:40 +01001038 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001039 virtual LInstruction* instr() { return instr_; }
Ben Murdochb8e0da22011-05-16 14:20:40 +01001040 private:
1041 LDivI* instr_;
1042 };
1043
Steve Block1e0659c2011-05-24 12:43:12 +01001044 const Register left = ToRegister(instr->InputAt(0));
1045 const Register right = ToRegister(instr->InputAt(1));
Ben Murdochb8e0da22011-05-16 14:20:40 +01001046 const Register scratch = scratch0();
1047 const Register result = ToRegister(instr->result());
1048
1049 // Check for x / 0.
1050 if (instr->hydrogen()->CheckFlag(HValue::kCanBeDivByZero)) {
Steve Block44f0eee2011-05-26 01:26:41 +01001051 __ cmp(right, Operand(0));
Ben Murdochb8e0da22011-05-16 14:20:40 +01001052 DeoptimizeIf(eq, instr->environment());
1053 }
1054
1055 // Check for (0 / -x) that will produce negative zero.
1056 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
1057 Label left_not_zero;
Steve Block44f0eee2011-05-26 01:26:41 +01001058 __ cmp(left, Operand(0));
Ben Murdochb8e0da22011-05-16 14:20:40 +01001059 __ b(ne, &left_not_zero);
Steve Block44f0eee2011-05-26 01:26:41 +01001060 __ cmp(right, Operand(0));
Ben Murdochb8e0da22011-05-16 14:20:40 +01001061 DeoptimizeIf(mi, instr->environment());
1062 __ bind(&left_not_zero);
1063 }
1064
1065 // Check for (-kMinInt / -1).
1066 if (instr->hydrogen()->CheckFlag(HValue::kCanOverflow)) {
1067 Label left_not_min_int;
1068 __ cmp(left, Operand(kMinInt));
1069 __ b(ne, &left_not_min_int);
1070 __ cmp(right, Operand(-1));
1071 DeoptimizeIf(eq, instr->environment());
1072 __ bind(&left_not_min_int);
1073 }
1074
1075 Label done, deoptimize;
1076 // Test for a few common cases first.
1077 __ cmp(right, Operand(1));
1078 __ mov(result, left, LeaveCC, eq);
1079 __ b(eq, &done);
1080
1081 __ cmp(right, Operand(2));
1082 __ tst(left, Operand(1), eq);
1083 __ mov(result, Operand(left, ASR, 1), LeaveCC, eq);
1084 __ b(eq, &done);
1085
1086 __ cmp(right, Operand(4));
1087 __ tst(left, Operand(3), eq);
1088 __ mov(result, Operand(left, ASR, 2), LeaveCC, eq);
1089 __ b(eq, &done);
1090
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001091 // Call the stub. The numbers in r0 and r1 have
Ben Murdochb8e0da22011-05-16 14:20:40 +01001092 // to be tagged to Smis. If that is not possible, deoptimize.
1093 DeferredDivI* deferred = new DeferredDivI(this, instr);
1094
1095 __ TrySmiTag(left, &deoptimize, scratch);
1096 __ TrySmiTag(right, &deoptimize, scratch);
1097
1098 __ b(al, deferred->entry());
1099 __ bind(deferred->exit());
1100
1101 // If the result in r0 is a Smi, untag it, else deoptimize.
Steve Block1e0659c2011-05-24 12:43:12 +01001102 __ JumpIfNotSmi(result, &deoptimize);
Ben Murdochb8e0da22011-05-16 14:20:40 +01001103 __ SmiUntag(result);
1104 __ b(&done);
1105
1106 __ bind(&deoptimize);
1107 DeoptimizeIf(al, instr->environment());
1108 __ bind(&done);
1109}
1110
1111
Steve Block1e0659c2011-05-24 12:43:12 +01001112template<int T>
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001113void LCodeGen::DoDeferredBinaryOpStub(LTemplateInstruction<1, 2, T>* instr,
1114 Token::Value op) {
Steve Block1e0659c2011-05-24 12:43:12 +01001115 Register left = ToRegister(instr->InputAt(0));
1116 Register right = ToRegister(instr->InputAt(1));
Ben Murdochb8e0da22011-05-16 14:20:40 +01001117
Ben Murdoch8b112d22011-06-08 16:22:53 +01001118 PushSafepointRegistersScope scope(this, Safepoint::kWithRegistersAndDoubles);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001119 // Move left to r1 and right to r0 for the stub call.
1120 if (left.is(r1)) {
1121 __ Move(r0, right);
1122 } else if (left.is(r0) && right.is(r1)) {
1123 __ Swap(r0, r1, r2);
1124 } else if (left.is(r0)) {
1125 ASSERT(!right.is(r1));
1126 __ mov(r1, r0);
1127 __ mov(r0, right);
1128 } else {
1129 ASSERT(!left.is(r0) && !right.is(r0));
1130 __ mov(r0, right);
1131 __ mov(r1, left);
1132 }
Ben Murdoch257744e2011-11-30 15:57:28 +00001133 BinaryOpStub stub(op, OVERWRITE_LEFT);
Ben Murdochb8e0da22011-05-16 14:20:40 +01001134 __ CallStub(&stub);
1135 RecordSafepointWithRegistersAndDoubles(instr->pointer_map(),
1136 0,
Ben Murdoch2b4ba112012-01-20 14:57:15 +00001137 Safepoint::kNoLazyDeopt);
Ben Murdochb8e0da22011-05-16 14:20:40 +01001138 // Overwrite the stored value of r0 with the result of the stub.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001139 __ StoreToSafepointRegistersAndDoublesSlot(r0, r0);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001140}
1141
1142
1143void LCodeGen::DoMulI(LMulI* instr) {
Steve Block9fac8402011-05-12 15:51:54 +01001144 Register scratch = scratch0();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001145 Register result = ToRegister(instr->result());
1146 // Note that result may alias left.
Steve Block1e0659c2011-05-24 12:43:12 +01001147 Register left = ToRegister(instr->InputAt(0));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001148 LOperand* right_op = instr->InputAt(1);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001149
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001150 bool can_overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
1151 bool bailout_on_minus_zero =
1152 instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001153
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001154 if (right_op->IsConstantOperand() && !can_overflow) {
1155 // Use optimized code for specific constants.
1156 int32_t constant = ToInteger32(LConstantOperand::cast(right_op));
1157
1158 if (bailout_on_minus_zero && (constant < 0)) {
1159 // The case of a null constant will be handled separately.
1160 // If constant is negative and left is null, the result should be -0.
1161 __ cmp(left, Operand(0));
1162 DeoptimizeIf(eq, instr->environment());
1163 }
1164
1165 switch (constant) {
1166 case -1:
1167 __ rsb(result, left, Operand(0));
1168 break;
1169 case 0:
1170 if (bailout_on_minus_zero) {
1171 // If left is strictly negative and the constant is null, the
1172 // result is -0. Deoptimize if required, otherwise return 0.
1173 __ cmp(left, Operand(0));
1174 DeoptimizeIf(mi, instr->environment());
1175 }
1176 __ mov(result, Operand(0));
1177 break;
1178 case 1:
1179 __ Move(result, left);
1180 break;
1181 default:
1182 // Multiplying by powers of two and powers of two plus or minus
1183 // one can be done faster with shifted operands.
1184 // For other constants we emit standard code.
1185 int32_t mask = constant >> 31;
1186 uint32_t constant_abs = (constant + mask) ^ mask;
1187
1188 if (IsPowerOf2(constant_abs) ||
1189 IsPowerOf2(constant_abs - 1) ||
1190 IsPowerOf2(constant_abs + 1)) {
1191 if (IsPowerOf2(constant_abs)) {
1192 int32_t shift = WhichPowerOf2(constant_abs);
1193 __ mov(result, Operand(left, LSL, shift));
1194 } else if (IsPowerOf2(constant_abs - 1)) {
1195 int32_t shift = WhichPowerOf2(constant_abs - 1);
1196 __ add(result, left, Operand(left, LSL, shift));
1197 } else if (IsPowerOf2(constant_abs + 1)) {
1198 int32_t shift = WhichPowerOf2(constant_abs + 1);
1199 __ rsb(result, left, Operand(left, LSL, shift));
1200 }
1201
1202 // Correct the sign of the result is the constant is negative.
1203 if (constant < 0) __ rsb(result, result, Operand(0));
1204
1205 } else {
1206 // Generate standard code.
1207 __ mov(ip, Operand(constant));
1208 __ mul(result, left, ip);
1209 }
1210 }
1211
Ben Murdochb0fe1622011-05-05 13:52:32 +01001212 } else {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001213 Register right = EmitLoadRegister(right_op, scratch);
1214 if (bailout_on_minus_zero) {
1215 __ orr(ToRegister(instr->TempAt(0)), left, right);
1216 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01001217
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001218 if (can_overflow) {
1219 // scratch:result = left * right.
1220 __ smull(result, scratch, left, right);
1221 __ cmp(scratch, Operand(result, ASR, 31));
1222 DeoptimizeIf(ne, instr->environment());
Ben Murdochb0fe1622011-05-05 13:52:32 +01001223 } else {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001224 __ mul(result, left, right);
1225 }
1226
1227 if (bailout_on_minus_zero) {
1228 // Bail out if the result is supposed to be negative zero.
1229 Label done;
1230 __ cmp(result, Operand(0));
1231 __ b(ne, &done);
Steve Block1e0659c2011-05-24 12:43:12 +01001232 __ cmp(ToRegister(instr->TempAt(0)), Operand(0));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001233 DeoptimizeIf(mi, instr->environment());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001234 __ bind(&done);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001235 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01001236 }
1237}
1238
1239
1240void LCodeGen::DoBitI(LBitI* instr) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001241 LOperand* left_op = instr->InputAt(0);
1242 LOperand* right_op = instr->InputAt(1);
1243 ASSERT(left_op->IsRegister());
1244 Register left = ToRegister(left_op);
1245 Register result = ToRegister(instr->result());
1246 Operand right(no_reg);
Steve Block44f0eee2011-05-26 01:26:41 +01001247
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001248 if (right_op->IsStackSlot() || right_op->IsArgument()) {
1249 right = Operand(EmitLoadRegister(right_op, ip));
Steve Block44f0eee2011-05-26 01:26:41 +01001250 } else {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001251 ASSERT(right_op->IsRegister() || right_op->IsConstantOperand());
1252 right = ToOperand(right_op);
Steve Block44f0eee2011-05-26 01:26:41 +01001253 }
1254
Ben Murdochb0fe1622011-05-05 13:52:32 +01001255 switch (instr->op()) {
1256 case Token::BIT_AND:
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001257 __ and_(result, left, right);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001258 break;
1259 case Token::BIT_OR:
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001260 __ orr(result, left, right);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001261 break;
1262 case Token::BIT_XOR:
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001263 __ eor(result, left, right);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001264 break;
1265 default:
1266 UNREACHABLE();
1267 break;
1268 }
1269}
1270
1271
1272void LCodeGen::DoShiftI(LShiftI* instr) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001273 // Both 'left' and 'right' are "used at start" (see LCodeGen::DoShift), so
1274 // result may alias either of them.
1275 LOperand* right_op = instr->InputAt(1);
1276 Register left = ToRegister(instr->InputAt(0));
1277 Register result = ToRegister(instr->result());
Steve Block9fac8402011-05-12 15:51:54 +01001278 Register scratch = scratch0();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001279 if (right_op->IsRegister()) {
1280 // Mask the right_op operand.
1281 __ and_(scratch, ToRegister(right_op), Operand(0x1F));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001282 switch (instr->op()) {
1283 case Token::SAR:
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001284 __ mov(result, Operand(left, ASR, scratch));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001285 break;
1286 case Token::SHR:
1287 if (instr->can_deopt()) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001288 __ mov(result, Operand(left, LSR, scratch), SetCC);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001289 DeoptimizeIf(mi, instr->environment());
1290 } else {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001291 __ mov(result, Operand(left, LSR, scratch));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001292 }
1293 break;
1294 case Token::SHL:
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001295 __ mov(result, Operand(left, LSL, scratch));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001296 break;
1297 default:
1298 UNREACHABLE();
1299 break;
1300 }
1301 } else {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001302 // Mask the right_op operand.
1303 int value = ToInteger32(LConstantOperand::cast(right_op));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001304 uint8_t shift_count = static_cast<uint8_t>(value & 0x1F);
1305 switch (instr->op()) {
1306 case Token::SAR:
1307 if (shift_count != 0) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001308 __ mov(result, Operand(left, ASR, shift_count));
1309 } else {
1310 __ Move(result, left);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001311 }
1312 break;
1313 case Token::SHR:
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001314 if (shift_count != 0) {
1315 __ mov(result, Operand(left, LSR, shift_count));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001316 } else {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001317 if (instr->can_deopt()) {
1318 __ tst(left, Operand(0x80000000));
1319 DeoptimizeIf(ne, instr->environment());
1320 }
1321 __ Move(result, left);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001322 }
1323 break;
1324 case Token::SHL:
1325 if (shift_count != 0) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001326 __ mov(result, Operand(left, LSL, shift_count));
1327 } else {
1328 __ Move(result, left);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001329 }
1330 break;
1331 default:
1332 UNREACHABLE();
1333 break;
1334 }
1335 }
1336}
1337
1338
1339void LCodeGen::DoSubI(LSubI* instr) {
Steve Block44f0eee2011-05-26 01:26:41 +01001340 LOperand* left = instr->InputAt(0);
1341 LOperand* right = instr->InputAt(1);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001342 LOperand* result = instr->result();
Steve Block44f0eee2011-05-26 01:26:41 +01001343 bool can_overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
1344 SBit set_cond = can_overflow ? SetCC : LeaveCC;
1345
1346 if (right->IsStackSlot() || right->IsArgument()) {
1347 Register right_reg = EmitLoadRegister(right, ip);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001348 __ sub(ToRegister(result), ToRegister(left), Operand(right_reg), set_cond);
Steve Block44f0eee2011-05-26 01:26:41 +01001349 } else {
1350 ASSERT(right->IsRegister() || right->IsConstantOperand());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001351 __ sub(ToRegister(result), ToRegister(left), ToOperand(right), set_cond);
Steve Block44f0eee2011-05-26 01:26:41 +01001352 }
1353
1354 if (can_overflow) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001355 DeoptimizeIf(vs, instr->environment());
1356 }
1357}
1358
1359
1360void LCodeGen::DoConstantI(LConstantI* instr) {
1361 ASSERT(instr->result()->IsRegister());
1362 __ mov(ToRegister(instr->result()), Operand(instr->value()));
1363}
1364
1365
1366void LCodeGen::DoConstantD(LConstantD* instr) {
Ben Murdochb8e0da22011-05-16 14:20:40 +01001367 ASSERT(instr->result()->IsDoubleRegister());
1368 DwVfpRegister result = ToDoubleRegister(instr->result());
1369 double v = instr->value();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001370 __ Vmov(result, v);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001371}
1372
1373
1374void LCodeGen::DoConstantT(LConstantT* instr) {
Ben Murdochc7cc0282012-03-05 14:35:55 +00001375 Handle<Object> value = instr->value();
1376 if (value->IsSmi()) {
1377 __ mov(ToRegister(instr->result()), Operand(value));
1378 } else {
1379 __ LoadHeapObject(ToRegister(instr->result()),
1380 Handle<HeapObject>::cast(value));
1381 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01001382}
1383
1384
Steve Block9fac8402011-05-12 15:51:54 +01001385void LCodeGen::DoJSArrayLength(LJSArrayLength* instr) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001386 Register result = ToRegister(instr->result());
Steve Block1e0659c2011-05-24 12:43:12 +01001387 Register array = ToRegister(instr->InputAt(0));
Steve Block9fac8402011-05-12 15:51:54 +01001388 __ ldr(result, FieldMemOperand(array, JSArray::kLengthOffset));
1389}
Ben Murdochb0fe1622011-05-05 13:52:32 +01001390
Ben Murdochb0fe1622011-05-05 13:52:32 +01001391
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001392void LCodeGen::DoFixedArrayBaseLength(LFixedArrayBaseLength* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01001393 Register result = ToRegister(instr->result());
1394 Register array = ToRegister(instr->InputAt(0));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001395 __ ldr(result, FieldMemOperand(array, FixedArrayBase::kLengthOffset));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001396}
1397
1398
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001399void LCodeGen::DoElementsKind(LElementsKind* instr) {
1400 Register result = ToRegister(instr->result());
1401 Register input = ToRegister(instr->InputAt(0));
1402
1403 // Load map into |result|.
1404 __ ldr(result, FieldMemOperand(input, HeapObject::kMapOffset));
1405 // Load the map's "bit field 2" into |result|. We only need the first byte,
1406 // but the following bit field extraction takes care of that anyway.
1407 __ ldr(result, FieldMemOperand(result, Map::kBitField2Offset));
1408 // Retrieve elements_kind from bit field 2.
1409 __ ubfx(result, result, Map::kElementsKindShift, Map::kElementsKindBitCount);
1410}
1411
1412
Ben Murdochb0fe1622011-05-05 13:52:32 +01001413void LCodeGen::DoValueOf(LValueOf* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01001414 Register input = ToRegister(instr->InputAt(0));
Ben Murdochb8e0da22011-05-16 14:20:40 +01001415 Register result = ToRegister(instr->result());
Steve Block1e0659c2011-05-24 12:43:12 +01001416 Register map = ToRegister(instr->TempAt(0));
Ben Murdochb8e0da22011-05-16 14:20:40 +01001417 Label done;
1418
1419 // If the object is a smi return the object.
1420 __ tst(input, Operand(kSmiTagMask));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001421 __ Move(result, input, eq);
Ben Murdochb8e0da22011-05-16 14:20:40 +01001422 __ b(eq, &done);
1423
1424 // If the object is not a value type, return the object.
1425 __ CompareObjectType(input, map, map, JS_VALUE_TYPE);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001426 __ Move(result, input, ne);
Ben Murdochb8e0da22011-05-16 14:20:40 +01001427 __ b(ne, &done);
1428 __ ldr(result, FieldMemOperand(input, JSValue::kValueOffset));
1429
1430 __ bind(&done);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001431}
1432
1433
1434void LCodeGen::DoBitNotI(LBitNotI* instr) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001435 Register input = ToRegister(instr->InputAt(0));
1436 Register result = ToRegister(instr->result());
1437 __ mvn(result, Operand(input));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001438}
1439
1440
1441void LCodeGen::DoThrow(LThrow* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01001442 Register input_reg = EmitLoadRegister(instr->InputAt(0), ip);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001443 __ push(input_reg);
1444 CallRuntime(Runtime::kThrow, 1, instr);
1445
1446 if (FLAG_debug_code) {
1447 __ stop("Unreachable code.");
1448 }
1449}
1450
1451
1452void LCodeGen::DoAddI(LAddI* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01001453 LOperand* left = instr->InputAt(0);
1454 LOperand* right = instr->InputAt(1);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001455 LOperand* result = instr->result();
Steve Block44f0eee2011-05-26 01:26:41 +01001456 bool can_overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
1457 SBit set_cond = can_overflow ? SetCC : LeaveCC;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001458
Steve Block44f0eee2011-05-26 01:26:41 +01001459 if (right->IsStackSlot() || right->IsArgument()) {
1460 Register right_reg = EmitLoadRegister(right, ip);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001461 __ add(ToRegister(result), ToRegister(left), Operand(right_reg), set_cond);
Steve Block44f0eee2011-05-26 01:26:41 +01001462 } else {
1463 ASSERT(right->IsRegister() || right->IsConstantOperand());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001464 __ add(ToRegister(result), ToRegister(left), ToOperand(right), set_cond);
Steve Block44f0eee2011-05-26 01:26:41 +01001465 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01001466
Steve Block44f0eee2011-05-26 01:26:41 +01001467 if (can_overflow) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001468 DeoptimizeIf(vs, instr->environment());
1469 }
1470}
1471
1472
1473void LCodeGen::DoArithmeticD(LArithmeticD* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01001474 DoubleRegister left = ToDoubleRegister(instr->InputAt(0));
1475 DoubleRegister right = ToDoubleRegister(instr->InputAt(1));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001476 DoubleRegister result = ToDoubleRegister(instr->result());
Ben Murdochb0fe1622011-05-05 13:52:32 +01001477 switch (instr->op()) {
1478 case Token::ADD:
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001479 __ vadd(result, left, right);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001480 break;
1481 case Token::SUB:
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001482 __ vsub(result, left, right);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001483 break;
1484 case Token::MUL:
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001485 __ vmul(result, left, right);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001486 break;
1487 case Token::DIV:
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001488 __ vdiv(result, left, right);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001489 break;
1490 case Token::MOD: {
Steve Block1e0659c2011-05-24 12:43:12 +01001491 // Save r0-r3 on the stack.
1492 __ stm(db_w, sp, r0.bit() | r1.bit() | r2.bit() | r3.bit());
1493
Ben Murdoch257744e2011-11-30 15:57:28 +00001494 __ PrepareCallCFunction(0, 2, scratch0());
1495 __ SetCallCDoubleArguments(left, right);
Steve Block44f0eee2011-05-26 01:26:41 +01001496 __ CallCFunction(
Ben Murdoch257744e2011-11-30 15:57:28 +00001497 ExternalReference::double_fp_operation(Token::MOD, isolate()),
1498 0, 2);
Steve Block1e0659c2011-05-24 12:43:12 +01001499 // Move the result in the double result register.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001500 __ GetCFunctionDoubleResult(result);
Steve Block1e0659c2011-05-24 12:43:12 +01001501
1502 // Restore r0-r3.
1503 __ ldm(ia_w, sp, r0.bit() | r1.bit() | r2.bit() | r3.bit());
Ben Murdochb0fe1622011-05-05 13:52:32 +01001504 break;
1505 }
1506 default:
1507 UNREACHABLE();
1508 break;
1509 }
1510}
1511
1512
1513void LCodeGen::DoArithmeticT(LArithmeticT* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01001514 ASSERT(ToRegister(instr->InputAt(0)).is(r1));
1515 ASSERT(ToRegister(instr->InputAt(1)).is(r0));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001516 ASSERT(ToRegister(instr->result()).is(r0));
1517
Ben Murdoch257744e2011-11-30 15:57:28 +00001518 BinaryOpStub stub(instr->op(), NO_OVERWRITE);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001519 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
Ben Murdoch18a6f572011-07-25 17:16:09 +01001520 __ nop(); // Signals no inlined code.
Ben Murdochb0fe1622011-05-05 13:52:32 +01001521}
1522
1523
1524int LCodeGen::GetNextEmittedBlock(int block) {
1525 for (int i = block + 1; i < graph()->blocks()->length(); ++i) {
1526 LLabel* label = chunk_->GetLabel(i);
1527 if (!label->HasReplacement()) return i;
1528 }
1529 return -1;
1530}
1531
1532
1533void LCodeGen::EmitBranch(int left_block, int right_block, Condition cc) {
1534 int next_block = GetNextEmittedBlock(current_block_);
1535 right_block = chunk_->LookupDestination(right_block);
1536 left_block = chunk_->LookupDestination(left_block);
1537
1538 if (right_block == left_block) {
1539 EmitGoto(left_block);
1540 } else if (left_block == next_block) {
1541 __ b(NegateCondition(cc), chunk_->GetAssemblyLabel(right_block));
1542 } else if (right_block == next_block) {
1543 __ b(cc, chunk_->GetAssemblyLabel(left_block));
1544 } else {
1545 __ b(cc, chunk_->GetAssemblyLabel(left_block));
1546 __ b(chunk_->GetAssemblyLabel(right_block));
1547 }
1548}
1549
1550
1551void LCodeGen::DoBranch(LBranch* instr) {
1552 int true_block = chunk_->LookupDestination(instr->true_block_id());
1553 int false_block = chunk_->LookupDestination(instr->false_block_id());
1554
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001555 Representation r = instr->hydrogen()->value()->representation();
Ben Murdochb0fe1622011-05-05 13:52:32 +01001556 if (r.IsInteger32()) {
Steve Block1e0659c2011-05-24 12:43:12 +01001557 Register reg = ToRegister(instr->InputAt(0));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001558 __ cmp(reg, Operand(0));
Steve Block1e0659c2011-05-24 12:43:12 +01001559 EmitBranch(true_block, false_block, ne);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001560 } else if (r.IsDouble()) {
Steve Block1e0659c2011-05-24 12:43:12 +01001561 DoubleRegister reg = ToDoubleRegister(instr->InputAt(0));
Ben Murdoch086aeea2011-05-13 15:57:08 +01001562 Register scratch = scratch0();
1563
Ben Murdochb8e0da22011-05-16 14:20:40 +01001564 // Test the double value. Zero and NaN are false.
1565 __ VFPCompareAndLoadFlags(reg, 0.0, scratch);
1566 __ tst(scratch, Operand(kVFPZConditionFlagBit | kVFPVConditionFlagBit));
Ben Murdoch6d7cb002011-08-04 19:25:22 +01001567 EmitBranch(true_block, false_block, eq);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001568 } else {
1569 ASSERT(r.IsTagged());
Steve Block1e0659c2011-05-24 12:43:12 +01001570 Register reg = ToRegister(instr->InputAt(0));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001571 HType type = instr->hydrogen()->value()->type();
1572 if (type.IsBoolean()) {
1573 __ CompareRoot(reg, Heap::kTrueValueRootIndex);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001574 EmitBranch(true_block, false_block, eq);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001575 } else if (type.IsSmi()) {
1576 __ cmp(reg, Operand(0));
1577 EmitBranch(true_block, false_block, ne);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001578 } else {
1579 Label* true_label = chunk_->GetAssemblyLabel(true_block);
1580 Label* false_label = chunk_->GetAssemblyLabel(false_block);
1581
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001582 ToBooleanStub::Types expected = instr->hydrogen()->expected_input_types();
1583 // Avoid deopts in the case where we've never executed this path before.
1584 if (expected.IsEmpty()) expected = ToBooleanStub::all_types();
Ben Murdochb0fe1622011-05-05 13:52:32 +01001585
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001586 if (expected.Contains(ToBooleanStub::UNDEFINED)) {
1587 // undefined -> false.
1588 __ CompareRoot(reg, Heap::kUndefinedValueRootIndex);
1589 __ b(eq, false_label);
1590 }
1591 if (expected.Contains(ToBooleanStub::BOOLEAN)) {
1592 // Boolean -> its value.
1593 __ CompareRoot(reg, Heap::kTrueValueRootIndex);
1594 __ b(eq, true_label);
1595 __ CompareRoot(reg, Heap::kFalseValueRootIndex);
1596 __ b(eq, false_label);
1597 }
1598 if (expected.Contains(ToBooleanStub::NULL_TYPE)) {
1599 // 'null' -> false.
1600 __ CompareRoot(reg, Heap::kNullValueRootIndex);
1601 __ b(eq, false_label);
1602 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01001603
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001604 if (expected.Contains(ToBooleanStub::SMI)) {
1605 // Smis: 0 -> false, all other -> true.
1606 __ cmp(reg, Operand(0));
1607 __ b(eq, false_label);
1608 __ JumpIfSmi(reg, true_label);
1609 } else if (expected.NeedsMap()) {
1610 // If we need a map later and have a Smi -> deopt.
1611 __ tst(reg, Operand(kSmiTagMask));
1612 DeoptimizeIf(eq, instr->environment());
1613 }
1614
1615 const Register map = scratch0();
1616 if (expected.NeedsMap()) {
1617 __ ldr(map, FieldMemOperand(reg, HeapObject::kMapOffset));
1618
1619 if (expected.CanBeUndetectable()) {
1620 // Undetectable -> false.
1621 __ ldrb(ip, FieldMemOperand(map, Map::kBitFieldOffset));
1622 __ tst(ip, Operand(1 << Map::kIsUndetectable));
1623 __ b(ne, false_label);
1624 }
1625 }
1626
1627 if (expected.Contains(ToBooleanStub::SPEC_OBJECT)) {
1628 // spec object -> true.
1629 __ CompareInstanceType(map, ip, FIRST_SPEC_OBJECT_TYPE);
1630 __ b(ge, true_label);
1631 }
1632
1633 if (expected.Contains(ToBooleanStub::STRING)) {
1634 // String value -> false iff empty.
1635 Label not_string;
1636 __ CompareInstanceType(map, ip, FIRST_NONSTRING_TYPE);
1637 __ b(ge, &not_string);
1638 __ ldr(ip, FieldMemOperand(reg, String::kLengthOffset));
1639 __ cmp(ip, Operand(0));
1640 __ b(ne, true_label);
1641 __ b(false_label);
1642 __ bind(&not_string);
1643 }
1644
1645 if (expected.Contains(ToBooleanStub::HEAP_NUMBER)) {
1646 // heap number -> false iff +0, -0, or NaN.
1647 DoubleRegister dbl_scratch = double_scratch0();
1648 Label not_heap_number;
1649 __ CompareRoot(map, Heap::kHeapNumberMapRootIndex);
1650 __ b(ne, &not_heap_number);
1651 __ vldr(dbl_scratch, FieldMemOperand(reg, HeapNumber::kValueOffset));
1652 __ VFPCompareAndSetFlags(dbl_scratch, 0.0);
1653 __ b(vs, false_label); // NaN -> false.
1654 __ b(eq, false_label); // +0, -0 -> false.
1655 __ b(true_label);
1656 __ bind(&not_heap_number);
1657 }
1658
1659 // We've seen something for the first time -> deopt.
1660 DeoptimizeIf(al, instr->environment());
Ben Murdochb0fe1622011-05-05 13:52:32 +01001661 }
1662 }
1663}
1664
1665
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001666void LCodeGen::EmitGoto(int block) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001667 block = chunk_->LookupDestination(block);
1668 int next_block = GetNextEmittedBlock(current_block_);
1669 if (block != next_block) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001670 __ jmp(chunk_->GetAssemblyLabel(block));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001671 }
1672}
1673
1674
Ben Murdochb0fe1622011-05-05 13:52:32 +01001675void LCodeGen::DoGoto(LGoto* instr) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001676 EmitGoto(instr->block_id());
Ben Murdochb0fe1622011-05-05 13:52:32 +01001677}
1678
1679
1680Condition LCodeGen::TokenToCondition(Token::Value op, bool is_unsigned) {
Steve Block1e0659c2011-05-24 12:43:12 +01001681 Condition cond = kNoCondition;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001682 switch (op) {
1683 case Token::EQ:
1684 case Token::EQ_STRICT:
1685 cond = eq;
1686 break;
1687 case Token::LT:
1688 cond = is_unsigned ? lo : lt;
1689 break;
1690 case Token::GT:
1691 cond = is_unsigned ? hi : gt;
1692 break;
1693 case Token::LTE:
1694 cond = is_unsigned ? ls : le;
1695 break;
1696 case Token::GTE:
1697 cond = is_unsigned ? hs : ge;
1698 break;
1699 case Token::IN:
1700 case Token::INSTANCEOF:
1701 default:
1702 UNREACHABLE();
1703 }
1704 return cond;
1705}
1706
1707
Ben Murdochb0fe1622011-05-05 13:52:32 +01001708void LCodeGen::DoCmpIDAndBranch(LCmpIDAndBranch* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01001709 LOperand* left = instr->InputAt(0);
1710 LOperand* right = instr->InputAt(1);
1711 int false_block = chunk_->LookupDestination(instr->false_block_id());
1712 int true_block = chunk_->LookupDestination(instr->true_block_id());
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001713 Condition cond = TokenToCondition(instr->op(), false);
Steve Block1e0659c2011-05-24 12:43:12 +01001714
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001715 if (left->IsConstantOperand() && right->IsConstantOperand()) {
1716 // We can statically evaluate the comparison.
1717 double left_val = ToDouble(LConstantOperand::cast(left));
1718 double right_val = ToDouble(LConstantOperand::cast(right));
1719 int next_block =
1720 EvalComparison(instr->op(), left_val, right_val) ? true_block
1721 : false_block;
1722 EmitGoto(next_block);
Steve Block1e0659c2011-05-24 12:43:12 +01001723 } else {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001724 if (instr->is_double()) {
1725 // Compare left and right operands as doubles and load the
1726 // resulting flags into the normal status register.
1727 __ VFPCompareAndSetFlags(ToDoubleRegister(left), ToDoubleRegister(right));
1728 // If a NaN is involved, i.e. the result is unordered (V set),
1729 // jump to false block label.
1730 __ b(vs, chunk_->GetAssemblyLabel(false_block));
1731 } else {
1732 if (right->IsConstantOperand()) {
1733 __ cmp(ToRegister(left),
1734 Operand(ToInteger32(LConstantOperand::cast(right))));
1735 } else if (left->IsConstantOperand()) {
1736 __ cmp(ToRegister(right),
1737 Operand(ToInteger32(LConstantOperand::cast(left))));
1738 // We transposed the operands. Reverse the condition.
1739 cond = ReverseCondition(cond);
1740 } else {
1741 __ cmp(ToRegister(left), ToRegister(right));
1742 }
1743 }
1744 EmitBranch(true_block, false_block, cond);
Steve Block1e0659c2011-05-24 12:43:12 +01001745 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01001746}
1747
1748
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001749void LCodeGen::DoCmpObjectEqAndBranch(LCmpObjectEqAndBranch* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01001750 Register left = ToRegister(instr->InputAt(0));
1751 Register right = ToRegister(instr->InputAt(1));
1752 int false_block = chunk_->LookupDestination(instr->false_block_id());
1753 int true_block = chunk_->LookupDestination(instr->true_block_id());
1754
1755 __ cmp(left, Operand(right));
1756 EmitBranch(true_block, false_block, eq);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001757}
1758
1759
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001760void LCodeGen::DoCmpConstantEqAndBranch(LCmpConstantEqAndBranch* instr) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001761 Register left = ToRegister(instr->InputAt(0));
Ben Murdoch257744e2011-11-30 15:57:28 +00001762 int true_block = chunk_->LookupDestination(instr->true_block_id());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001763 int false_block = chunk_->LookupDestination(instr->false_block_id());
Ben Murdoch257744e2011-11-30 15:57:28 +00001764
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001765 __ cmp(left, Operand(instr->hydrogen()->right()));
Ben Murdoch257744e2011-11-30 15:57:28 +00001766 EmitBranch(true_block, false_block, eq);
1767}
1768
1769
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001770void LCodeGen::DoIsNilAndBranch(LIsNilAndBranch* instr) {
Steve Block9fac8402011-05-12 15:51:54 +01001771 Register scratch = scratch0();
Steve Block1e0659c2011-05-24 12:43:12 +01001772 Register reg = ToRegister(instr->InputAt(0));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001773 int false_block = chunk_->LookupDestination(instr->false_block_id());
1774
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001775 // If the expression is known to be untagged or a smi, then it's definitely
1776 // not null, and it can't be a an undetectable object.
1777 if (instr->hydrogen()->representation().IsSpecialization() ||
1778 instr->hydrogen()->type().IsSmi()) {
1779 EmitGoto(false_block);
1780 return;
1781 }
1782
1783 int true_block = chunk_->LookupDestination(instr->true_block_id());
1784 Heap::RootListIndex nil_value = instr->nil() == kNullValue ?
1785 Heap::kNullValueRootIndex :
1786 Heap::kUndefinedValueRootIndex;
1787 __ LoadRoot(ip, nil_value);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001788 __ cmp(reg, ip);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001789 if (instr->kind() == kStrictEquality) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001790 EmitBranch(true_block, false_block, eq);
1791 } else {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001792 Heap::RootListIndex other_nil_value = instr->nil() == kNullValue ?
1793 Heap::kUndefinedValueRootIndex :
1794 Heap::kNullValueRootIndex;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001795 Label* true_label = chunk_->GetAssemblyLabel(true_block);
1796 Label* false_label = chunk_->GetAssemblyLabel(false_block);
1797 __ b(eq, true_label);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001798 __ LoadRoot(ip, other_nil_value);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001799 __ cmp(reg, ip);
1800 __ b(eq, true_label);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001801 __ JumpIfSmi(reg, false_label);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001802 // Check for undetectable objects by looking in the bit field in
1803 // the map. The object has already been smi checked.
Ben Murdochb0fe1622011-05-05 13:52:32 +01001804 __ ldr(scratch, FieldMemOperand(reg, HeapObject::kMapOffset));
1805 __ ldrb(scratch, FieldMemOperand(scratch, Map::kBitFieldOffset));
1806 __ tst(scratch, Operand(1 << Map::kIsUndetectable));
1807 EmitBranch(true_block, false_block, ne);
1808 }
1809}
1810
1811
1812Condition LCodeGen::EmitIsObject(Register input,
1813 Register temp1,
Ben Murdochb0fe1622011-05-05 13:52:32 +01001814 Label* is_not_object,
1815 Label* is_object) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001816 Register temp2 = scratch0();
Steve Block1e0659c2011-05-24 12:43:12 +01001817 __ JumpIfSmi(input, is_not_object);
1818
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001819 __ LoadRoot(temp2, Heap::kNullValueRootIndex);
1820 __ cmp(input, temp2);
Steve Block1e0659c2011-05-24 12:43:12 +01001821 __ b(eq, is_object);
1822
1823 // Load map.
1824 __ ldr(temp1, FieldMemOperand(input, HeapObject::kMapOffset));
1825 // Undetectable objects behave like undefined.
1826 __ ldrb(temp2, FieldMemOperand(temp1, Map::kBitFieldOffset));
1827 __ tst(temp2, Operand(1 << Map::kIsUndetectable));
1828 __ b(ne, is_not_object);
1829
1830 // Load instance type and check that it is in object type range.
1831 __ ldrb(temp2, FieldMemOperand(temp1, Map::kInstanceTypeOffset));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001832 __ cmp(temp2, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
Steve Block1e0659c2011-05-24 12:43:12 +01001833 __ b(lt, is_not_object);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001834 __ cmp(temp2, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE));
Steve Block1e0659c2011-05-24 12:43:12 +01001835 return le;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001836}
1837
1838
Ben Murdochb0fe1622011-05-05 13:52:32 +01001839void LCodeGen::DoIsObjectAndBranch(LIsObjectAndBranch* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01001840 Register reg = ToRegister(instr->InputAt(0));
1841 Register temp1 = ToRegister(instr->TempAt(0));
Steve Block1e0659c2011-05-24 12:43:12 +01001842
1843 int true_block = chunk_->LookupDestination(instr->true_block_id());
1844 int false_block = chunk_->LookupDestination(instr->false_block_id());
1845 Label* true_label = chunk_->GetAssemblyLabel(true_block);
1846 Label* false_label = chunk_->GetAssemblyLabel(false_block);
1847
1848 Condition true_cond =
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001849 EmitIsObject(reg, temp1, false_label, true_label);
Steve Block1e0659c2011-05-24 12:43:12 +01001850
1851 EmitBranch(true_block, false_block, true_cond);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001852}
1853
1854
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001855Condition LCodeGen::EmitIsString(Register input,
1856 Register temp1,
1857 Label* is_not_string) {
1858 __ JumpIfSmi(input, is_not_string);
1859 __ CompareObjectType(input, temp1, temp1, FIRST_NONSTRING_TYPE);
1860
1861 return lt;
1862}
1863
1864
1865void LCodeGen::DoIsStringAndBranch(LIsStringAndBranch* instr) {
1866 Register reg = ToRegister(instr->InputAt(0));
1867 Register temp1 = ToRegister(instr->TempAt(0));
1868
1869 int true_block = chunk_->LookupDestination(instr->true_block_id());
1870 int false_block = chunk_->LookupDestination(instr->false_block_id());
1871 Label* false_label = chunk_->GetAssemblyLabel(false_block);
1872
1873 Condition true_cond =
1874 EmitIsString(reg, temp1, false_label);
1875
1876 EmitBranch(true_block, false_block, true_cond);
1877}
1878
1879
Ben Murdochb0fe1622011-05-05 13:52:32 +01001880void LCodeGen::DoIsSmiAndBranch(LIsSmiAndBranch* instr) {
1881 int true_block = chunk_->LookupDestination(instr->true_block_id());
1882 int false_block = chunk_->LookupDestination(instr->false_block_id());
1883
Steve Block1e0659c2011-05-24 12:43:12 +01001884 Register input_reg = EmitLoadRegister(instr->InputAt(0), ip);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001885 __ tst(input_reg, Operand(kSmiTagMask));
1886 EmitBranch(true_block, false_block, eq);
1887}
1888
1889
Ben Murdoch257744e2011-11-30 15:57:28 +00001890void LCodeGen::DoIsUndetectableAndBranch(LIsUndetectableAndBranch* instr) {
1891 Register input = ToRegister(instr->InputAt(0));
1892 Register temp = ToRegister(instr->TempAt(0));
1893
1894 int true_block = chunk_->LookupDestination(instr->true_block_id());
1895 int false_block = chunk_->LookupDestination(instr->false_block_id());
1896
1897 __ JumpIfSmi(input, chunk_->GetAssemblyLabel(false_block));
1898 __ ldr(temp, FieldMemOperand(input, HeapObject::kMapOffset));
1899 __ ldrb(temp, FieldMemOperand(temp, Map::kBitFieldOffset));
1900 __ tst(temp, Operand(1 << Map::kIsUndetectable));
1901 EmitBranch(true_block, false_block, ne);
1902}
1903
1904
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001905static Condition ComputeCompareCondition(Token::Value op) {
1906 switch (op) {
1907 case Token::EQ_STRICT:
1908 case Token::EQ:
1909 return eq;
1910 case Token::LT:
1911 return lt;
1912 case Token::GT:
1913 return gt;
1914 case Token::LTE:
1915 return le;
1916 case Token::GTE:
1917 return ge;
1918 default:
1919 UNREACHABLE();
1920 return kNoCondition;
1921 }
1922}
1923
1924
1925void LCodeGen::DoStringCompareAndBranch(LStringCompareAndBranch* instr) {
1926 Token::Value op = instr->op();
1927 int true_block = chunk_->LookupDestination(instr->true_block_id());
1928 int false_block = chunk_->LookupDestination(instr->false_block_id());
1929
1930 Handle<Code> ic = CompareIC::GetUninitialized(op);
1931 CallCode(ic, RelocInfo::CODE_TARGET, instr);
1932 __ cmp(r0, Operand(0)); // This instruction also signals no smi code inlined.
1933
1934 Condition condition = ComputeCompareCondition(op);
1935
1936 EmitBranch(true_block, false_block, condition);
1937}
1938
1939
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001940static InstanceType TestType(HHasInstanceTypeAndBranch* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01001941 InstanceType from = instr->from();
1942 InstanceType to = instr->to();
Ben Murdochb0fe1622011-05-05 13:52:32 +01001943 if (from == FIRST_TYPE) return to;
1944 ASSERT(from == to || to == LAST_TYPE);
1945 return from;
1946}
1947
1948
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001949static Condition BranchCondition(HHasInstanceTypeAndBranch* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01001950 InstanceType from = instr->from();
1951 InstanceType to = instr->to();
Ben Murdochb0fe1622011-05-05 13:52:32 +01001952 if (from == to) return eq;
1953 if (to == LAST_TYPE) return hs;
1954 if (from == FIRST_TYPE) return ls;
1955 UNREACHABLE();
1956 return eq;
1957}
1958
1959
Ben Murdochb0fe1622011-05-05 13:52:32 +01001960void LCodeGen::DoHasInstanceTypeAndBranch(LHasInstanceTypeAndBranch* instr) {
Steve Block9fac8402011-05-12 15:51:54 +01001961 Register scratch = scratch0();
Steve Block1e0659c2011-05-24 12:43:12 +01001962 Register input = ToRegister(instr->InputAt(0));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001963
1964 int true_block = chunk_->LookupDestination(instr->true_block_id());
1965 int false_block = chunk_->LookupDestination(instr->false_block_id());
1966
1967 Label* false_label = chunk_->GetAssemblyLabel(false_block);
1968
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001969 __ JumpIfSmi(input, false_label);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001970
Steve Block1e0659c2011-05-24 12:43:12 +01001971 __ CompareObjectType(input, scratch, scratch, TestType(instr->hydrogen()));
1972 EmitBranch(true_block, false_block, BranchCondition(instr->hydrogen()));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001973}
1974
1975
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001976void LCodeGen::DoGetCachedArrayIndex(LGetCachedArrayIndex* instr) {
1977 Register input = ToRegister(instr->InputAt(0));
1978 Register result = ToRegister(instr->result());
1979
1980 if (FLAG_debug_code) {
1981 __ AbortIfNotString(input);
1982 }
1983
1984 __ ldr(result, FieldMemOperand(input, String::kHashFieldOffset));
1985 __ IndexFromHash(result, result);
1986}
1987
1988
Ben Murdochb0fe1622011-05-05 13:52:32 +01001989void LCodeGen::DoHasCachedArrayIndexAndBranch(
1990 LHasCachedArrayIndexAndBranch* instr) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001991 Register input = ToRegister(instr->InputAt(0));
1992 Register scratch = scratch0();
1993
1994 int true_block = chunk_->LookupDestination(instr->true_block_id());
1995 int false_block = chunk_->LookupDestination(instr->false_block_id());
1996
1997 __ ldr(scratch,
1998 FieldMemOperand(input, String::kHashFieldOffset));
1999 __ tst(scratch, Operand(String::kContainsCachedArrayIndexMask));
2000 EmitBranch(true_block, false_block, eq);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002001}
2002
2003
Ben Murdochb8e0da22011-05-16 14:20:40 +01002004// Branches to a label or falls through with the answer in flags. Trashes
Ben Murdochc7cc0282012-03-05 14:35:55 +00002005// the temp registers, but not the input.
Ben Murdochb0fe1622011-05-05 13:52:32 +01002006void LCodeGen::EmitClassOfTest(Label* is_true,
2007 Label* is_false,
2008 Handle<String>class_name,
2009 Register input,
2010 Register temp,
2011 Register temp2) {
Ben Murdochb8e0da22011-05-16 14:20:40 +01002012 ASSERT(!input.is(temp));
Ben Murdochc7cc0282012-03-05 14:35:55 +00002013 ASSERT(!input.is(temp2));
2014 ASSERT(!temp.is(temp2));
2015
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002016 __ JumpIfSmi(input, is_false);
Ben Murdochb8e0da22011-05-16 14:20:40 +01002017
Ben Murdochb8e0da22011-05-16 14:20:40 +01002018 if (class_name->IsEqualTo(CStrVector("Function"))) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002019 // Assuming the following assertions, we can use the same compares to test
2020 // for both being a function type and being in the object type range.
2021 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
2022 STATIC_ASSERT(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2023 FIRST_SPEC_OBJECT_TYPE + 1);
2024 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2025 LAST_SPEC_OBJECT_TYPE - 1);
2026 STATIC_ASSERT(LAST_SPEC_OBJECT_TYPE == LAST_TYPE);
2027 __ CompareObjectType(input, temp, temp2, FIRST_SPEC_OBJECT_TYPE);
2028 __ b(lt, is_false);
2029 __ b(eq, is_true);
2030 __ cmp(temp2, Operand(LAST_SPEC_OBJECT_TYPE));
2031 __ b(eq, is_true);
Ben Murdochb8e0da22011-05-16 14:20:40 +01002032 } else {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002033 // Faster code path to avoid two compares: subtract lower bound from the
2034 // actual type and do a signed compare with the width of the type range.
2035 __ ldr(temp, FieldMemOperand(input, HeapObject::kMapOffset));
2036 __ ldrb(temp2, FieldMemOperand(temp, Map::kInstanceTypeOffset));
2037 __ sub(temp2, temp2, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
2038 __ cmp(temp2, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE -
2039 FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
2040 __ b(gt, is_false);
Ben Murdochb8e0da22011-05-16 14:20:40 +01002041 }
2042
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002043 // Now we are in the FIRST-LAST_NONCALLABLE_SPEC_OBJECT_TYPE range.
Ben Murdochb8e0da22011-05-16 14:20:40 +01002044 // Check if the constructor in the map is a function.
2045 __ ldr(temp, FieldMemOperand(temp, Map::kConstructorOffset));
2046
Ben Murdochb8e0da22011-05-16 14:20:40 +01002047 // Objects with a non-function constructor have class 'Object'.
2048 __ CompareObjectType(temp, temp2, temp2, JS_FUNCTION_TYPE);
2049 if (class_name->IsEqualTo(CStrVector("Object"))) {
2050 __ b(ne, is_true);
2051 } else {
2052 __ b(ne, is_false);
2053 }
2054
2055 // temp now contains the constructor function. Grab the
2056 // instance class name from there.
2057 __ ldr(temp, FieldMemOperand(temp, JSFunction::kSharedFunctionInfoOffset));
2058 __ ldr(temp, FieldMemOperand(temp,
2059 SharedFunctionInfo::kInstanceClassNameOffset));
2060 // The class name we are testing against is a symbol because it's a literal.
2061 // The name in the constructor is a symbol because of the way the context is
2062 // booted. This routine isn't expected to work for random API-created
2063 // classes and it doesn't have to because you can't access it with natives
2064 // syntax. Since both sides are symbols it is sufficient to use an identity
2065 // comparison.
2066 __ cmp(temp, Operand(class_name));
2067 // End with the answer in flags.
Ben Murdochb0fe1622011-05-05 13:52:32 +01002068}
2069
2070
Ben Murdochb0fe1622011-05-05 13:52:32 +01002071void LCodeGen::DoClassOfTestAndBranch(LClassOfTestAndBranch* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01002072 Register input = ToRegister(instr->InputAt(0));
Ben Murdochb8e0da22011-05-16 14:20:40 +01002073 Register temp = scratch0();
Steve Block1e0659c2011-05-24 12:43:12 +01002074 Register temp2 = ToRegister(instr->TempAt(0));
Ben Murdochb8e0da22011-05-16 14:20:40 +01002075 Handle<String> class_name = instr->hydrogen()->class_name();
2076
2077 int true_block = chunk_->LookupDestination(instr->true_block_id());
2078 int false_block = chunk_->LookupDestination(instr->false_block_id());
2079
2080 Label* true_label = chunk_->GetAssemblyLabel(true_block);
2081 Label* false_label = chunk_->GetAssemblyLabel(false_block);
2082
2083 EmitClassOfTest(true_label, false_label, class_name, input, temp, temp2);
2084
2085 EmitBranch(true_block, false_block, eq);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002086}
2087
2088
2089void LCodeGen::DoCmpMapAndBranch(LCmpMapAndBranch* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01002090 Register reg = ToRegister(instr->InputAt(0));
2091 Register temp = ToRegister(instr->TempAt(0));
Steve Block9fac8402011-05-12 15:51:54 +01002092 int true_block = instr->true_block_id();
2093 int false_block = instr->false_block_id();
2094
2095 __ ldr(temp, FieldMemOperand(reg, HeapObject::kMapOffset));
2096 __ cmp(temp, Operand(instr->map()));
2097 EmitBranch(true_block, false_block, eq);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002098}
2099
2100
2101void LCodeGen::DoInstanceOf(LInstanceOf* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01002102 ASSERT(ToRegister(instr->InputAt(0)).is(r0)); // Object is in r0.
2103 ASSERT(ToRegister(instr->InputAt(1)).is(r1)); // Function is in r1.
Steve Block9fac8402011-05-12 15:51:54 +01002104
Ben Murdochb0fe1622011-05-05 13:52:32 +01002105 InstanceofStub stub(InstanceofStub::kArgsInRegisters);
2106 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
2107
Steve Block44f0eee2011-05-26 01:26:41 +01002108 __ cmp(r0, Operand(0));
2109 __ mov(r0, Operand(factory()->false_value()), LeaveCC, ne);
2110 __ mov(r0, Operand(factory()->true_value()), LeaveCC, eq);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002111}
2112
2113
Ben Murdoch086aeea2011-05-13 15:57:08 +01002114void LCodeGen::DoInstanceOfKnownGlobal(LInstanceOfKnownGlobal* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01002115 class DeferredInstanceOfKnownGlobal: public LDeferredCode {
2116 public:
2117 DeferredInstanceOfKnownGlobal(LCodeGen* codegen,
2118 LInstanceOfKnownGlobal* instr)
2119 : LDeferredCode(codegen), instr_(instr) { }
2120 virtual void Generate() {
Ben Murdoch2b4ba112012-01-20 14:57:15 +00002121 codegen()->DoDeferredInstanceOfKnownGlobal(instr_, &map_check_);
Steve Block1e0659c2011-05-24 12:43:12 +01002122 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002123 virtual LInstruction* instr() { return instr_; }
Steve Block1e0659c2011-05-24 12:43:12 +01002124 Label* map_check() { return &map_check_; }
Steve Block1e0659c2011-05-24 12:43:12 +01002125 private:
2126 LInstanceOfKnownGlobal* instr_;
2127 Label map_check_;
2128 };
2129
2130 DeferredInstanceOfKnownGlobal* deferred;
2131 deferred = new DeferredInstanceOfKnownGlobal(this, instr);
2132
2133 Label done, false_result;
2134 Register object = ToRegister(instr->InputAt(0));
2135 Register temp = ToRegister(instr->TempAt(0));
2136 Register result = ToRegister(instr->result());
2137
2138 ASSERT(object.is(r0));
2139 ASSERT(result.is(r0));
2140
2141 // A Smi is not instance of anything.
2142 __ JumpIfSmi(object, &false_result);
2143
2144 // This is the inlined call site instanceof cache. The two occurences of the
2145 // hole value will be patched to the last map/result pair generated by the
2146 // instanceof stub.
2147 Label cache_miss;
2148 Register map = temp;
2149 __ ldr(map, FieldMemOperand(object, HeapObject::kMapOffset));
2150 __ bind(deferred->map_check()); // Label for calculating code patching.
2151 // We use Factory::the_hole_value() on purpose instead of loading from the
2152 // root array to force relocation to be able to later patch with
2153 // the cached map.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002154 Handle<JSGlobalPropertyCell> cell =
2155 factory()->NewJSGlobalPropertyCell(factory()->the_hole_value());
2156 __ mov(ip, Operand(Handle<Object>(cell)));
2157 __ ldr(ip, FieldMemOperand(ip, JSGlobalPropertyCell::kValueOffset));
Steve Block1e0659c2011-05-24 12:43:12 +01002158 __ cmp(map, Operand(ip));
2159 __ b(ne, &cache_miss);
2160 // We use Factory::the_hole_value() on purpose instead of loading from the
2161 // root array to force relocation to be able to later patch
2162 // with true or false.
Steve Block44f0eee2011-05-26 01:26:41 +01002163 __ mov(result, Operand(factory()->the_hole_value()));
Steve Block1e0659c2011-05-24 12:43:12 +01002164 __ b(&done);
2165
2166 // The inlined call site cache did not match. Check null and string before
2167 // calling the deferred code.
2168 __ bind(&cache_miss);
2169 // Null is not instance of anything.
2170 __ LoadRoot(ip, Heap::kNullValueRootIndex);
2171 __ cmp(object, Operand(ip));
2172 __ b(eq, &false_result);
2173
2174 // String values is not instance of anything.
2175 Condition is_string = masm_->IsObjectStringType(object, temp);
2176 __ b(is_string, &false_result);
2177
2178 // Go to the deferred code.
2179 __ b(deferred->entry());
2180
2181 __ bind(&false_result);
2182 __ LoadRoot(result, Heap::kFalseValueRootIndex);
2183
2184 // Here result has either true or false. Deferred code also produces true or
2185 // false object.
2186 __ bind(deferred->exit());
2187 __ bind(&done);
2188}
2189
2190
Ben Murdoch2b4ba112012-01-20 14:57:15 +00002191void LCodeGen::DoDeferredInstanceOfKnownGlobal(LInstanceOfKnownGlobal* instr,
2192 Label* map_check) {
Steve Block1e0659c2011-05-24 12:43:12 +01002193 Register result = ToRegister(instr->result());
2194 ASSERT(result.is(r0));
2195
2196 InstanceofStub::Flags flags = InstanceofStub::kNoFlags;
2197 flags = static_cast<InstanceofStub::Flags>(
2198 flags | InstanceofStub::kArgsInRegisters);
2199 flags = static_cast<InstanceofStub::Flags>(
2200 flags | InstanceofStub::kCallSiteInlineCheck);
2201 flags = static_cast<InstanceofStub::Flags>(
2202 flags | InstanceofStub::kReturnTrueFalseObject);
2203 InstanceofStub stub(flags);
2204
Ben Murdoch8b112d22011-06-08 16:22:53 +01002205 PushSafepointRegistersScope scope(this, Safepoint::kWithRegisters);
Steve Block1e0659c2011-05-24 12:43:12 +01002206
2207 // Get the temp register reserved by the instruction. This needs to be r4 as
2208 // its slot of the pushing of safepoint registers is used to communicate the
2209 // offset to the location of the map check.
2210 Register temp = ToRegister(instr->TempAt(0));
2211 ASSERT(temp.is(r4));
Ben Murdochc7cc0282012-03-05 14:35:55 +00002212 __ LoadHeapObject(InstanceofStub::right(), instr->function());
Steve Block1e0659c2011-05-24 12:43:12 +01002213 static const int kAdditionalDelta = 4;
2214 int delta = masm_->InstructionsGeneratedSince(map_check) + kAdditionalDelta;
2215 Label before_push_delta;
2216 __ bind(&before_push_delta);
2217 __ BlockConstPoolFor(kAdditionalDelta);
2218 __ mov(temp, Operand(delta * kPointerSize));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002219 __ StoreToSafepointRegisterSlot(temp, temp);
Ben Murdoch8b112d22011-06-08 16:22:53 +01002220 CallCodeGeneric(stub.GetCode(),
2221 RelocInfo::CODE_TARGET,
2222 instr,
2223 RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS);
Ben Murdoch2b4ba112012-01-20 14:57:15 +00002224 ASSERT(instr->HasDeoptimizationEnvironment());
2225 LEnvironment* env = instr->deoptimization_environment();
2226 safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
Steve Block1e0659c2011-05-24 12:43:12 +01002227 // Put the result value into the result register slot and
2228 // restore all registers.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002229 __ StoreToSafepointRegisterSlot(result, result);
Ben Murdoch086aeea2011-05-13 15:57:08 +01002230}
2231
Ben Murdochb0fe1622011-05-05 13:52:32 +01002232
Ben Murdochb0fe1622011-05-05 13:52:32 +01002233void LCodeGen::DoCmpT(LCmpT* instr) {
2234 Token::Value op = instr->op();
2235
2236 Handle<Code> ic = CompareIC::GetUninitialized(op);
2237 CallCode(ic, RelocInfo::CODE_TARGET, instr);
Steve Block1e0659c2011-05-24 12:43:12 +01002238 __ cmp(r0, Operand(0)); // This instruction also signals no smi code inlined.
Ben Murdochb0fe1622011-05-05 13:52:32 +01002239
2240 Condition condition = ComputeCompareCondition(op);
Ben Murdochb8e0da22011-05-16 14:20:40 +01002241 __ LoadRoot(ToRegister(instr->result()),
2242 Heap::kTrueValueRootIndex,
2243 condition);
2244 __ LoadRoot(ToRegister(instr->result()),
2245 Heap::kFalseValueRootIndex,
2246 NegateCondition(condition));
Ben Murdochb0fe1622011-05-05 13:52:32 +01002247}
2248
2249
Ben Murdochb0fe1622011-05-05 13:52:32 +01002250void LCodeGen::DoReturn(LReturn* instr) {
2251 if (FLAG_trace) {
2252 // Push the return value on the stack as the parameter.
2253 // Runtime::TraceExit returns its parameter in r0.
2254 __ push(r0);
2255 __ CallRuntime(Runtime::kTraceExit, 1);
2256 }
Ben Murdoch257744e2011-11-30 15:57:28 +00002257 int32_t sp_delta = (GetParameterCount() + 1) * kPointerSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +01002258 __ mov(sp, fp);
2259 __ ldm(ia_w, sp, fp.bit() | lr.bit());
2260 __ add(sp, sp, Operand(sp_delta));
2261 __ Jump(lr);
2262}
2263
2264
Ben Murdoch8b112d22011-06-08 16:22:53 +01002265void LCodeGen::DoLoadGlobalCell(LLoadGlobalCell* instr) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01002266 Register result = ToRegister(instr->result());
2267 __ mov(ip, Operand(Handle<Object>(instr->hydrogen()->cell())));
2268 __ ldr(result, FieldMemOperand(ip, JSGlobalPropertyCell::kValueOffset));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002269 if (instr->hydrogen()->RequiresHoleCheck()) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01002270 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
2271 __ cmp(result, ip);
2272 DeoptimizeIf(eq, instr->environment());
2273 }
2274}
2275
2276
Ben Murdoch8b112d22011-06-08 16:22:53 +01002277void LCodeGen::DoLoadGlobalGeneric(LLoadGlobalGeneric* instr) {
2278 ASSERT(ToRegister(instr->global_object()).is(r0));
2279 ASSERT(ToRegister(instr->result()).is(r0));
2280
2281 __ mov(r2, Operand(instr->name()));
2282 RelocInfo::Mode mode = instr->for_typeof() ? RelocInfo::CODE_TARGET
2283 : RelocInfo::CODE_TARGET_CONTEXT;
2284 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
2285 CallCode(ic, mode, instr);
2286}
2287
2288
2289void LCodeGen::DoStoreGlobalCell(LStoreGlobalCell* instr) {
Ben Murdochc7cc0282012-03-05 14:35:55 +00002290 Register value = ToRegister(instr->value());
2291 Register cell = scratch0();
Steve Block1e0659c2011-05-24 12:43:12 +01002292
2293 // Load the cell.
Ben Murdochc7cc0282012-03-05 14:35:55 +00002294 __ mov(cell, Operand(instr->hydrogen()->cell()));
Steve Block1e0659c2011-05-24 12:43:12 +01002295
2296 // If the cell we are storing to contains the hole it could have
2297 // been deleted from the property dictionary. In that case, we need
2298 // to update the property details in the property dictionary to mark
2299 // it as no longer deleted.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002300 if (instr->hydrogen()->RequiresHoleCheck()) {
Ben Murdochc7cc0282012-03-05 14:35:55 +00002301 // We use a temp to check the payload (CompareRoot might clobber ip).
2302 Register payload = ToRegister(instr->TempAt(0));
2303 __ ldr(payload, FieldMemOperand(cell, JSGlobalPropertyCell::kValueOffset));
2304 __ CompareRoot(payload, Heap::kTheHoleValueRootIndex);
Steve Block1e0659c2011-05-24 12:43:12 +01002305 DeoptimizeIf(eq, instr->environment());
2306 }
2307
2308 // Store the value.
Ben Murdochc7cc0282012-03-05 14:35:55 +00002309 __ str(value, FieldMemOperand(cell, JSGlobalPropertyCell::kValueOffset));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002310 // Cells are always rescanned, so no write barrier here.
Ben Murdochb0fe1622011-05-05 13:52:32 +01002311}
2312
2313
Ben Murdoch8b112d22011-06-08 16:22:53 +01002314void LCodeGen::DoStoreGlobalGeneric(LStoreGlobalGeneric* instr) {
2315 ASSERT(ToRegister(instr->global_object()).is(r1));
2316 ASSERT(ToRegister(instr->value()).is(r0));
2317
2318 __ mov(r2, Operand(instr->name()));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002319 Handle<Code> ic = (instr->strict_mode_flag() == kStrictMode)
Ben Murdoch8b112d22011-06-08 16:22:53 +01002320 ? isolate()->builtins()->StoreIC_Initialize_Strict()
2321 : isolate()->builtins()->StoreIC_Initialize();
2322 CallCode(ic, RelocInfo::CODE_TARGET_CONTEXT, instr);
2323}
2324
2325
Ben Murdochb8e0da22011-05-16 14:20:40 +01002326void LCodeGen::DoLoadContextSlot(LLoadContextSlot* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01002327 Register context = ToRegister(instr->context());
Ben Murdochb8e0da22011-05-16 14:20:40 +01002328 Register result = ToRegister(instr->result());
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002329 __ ldr(result, ContextOperand(context, instr->slot_index()));
Ben Murdochc7cc0282012-03-05 14:35:55 +00002330 if (instr->hydrogen()->RequiresHoleCheck()) {
2331 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
2332 __ cmp(result, ip);
2333 if (instr->hydrogen()->DeoptimizesOnHole()) {
2334 DeoptimizeIf(eq, instr->environment());
2335 } else {
2336 __ mov(result, Operand(factory()->undefined_value()), LeaveCC, eq);
2337 }
2338 }
Ben Murdochb8e0da22011-05-16 14:20:40 +01002339}
2340
2341
Steve Block1e0659c2011-05-24 12:43:12 +01002342void LCodeGen::DoStoreContextSlot(LStoreContextSlot* instr) {
2343 Register context = ToRegister(instr->context());
2344 Register value = ToRegister(instr->value());
Ben Murdochc7cc0282012-03-05 14:35:55 +00002345 Register scratch = scratch0();
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002346 MemOperand target = ContextOperand(context, instr->slot_index());
Ben Murdochc7cc0282012-03-05 14:35:55 +00002347
2348 Label skip_assignment;
2349
2350 if (instr->hydrogen()->RequiresHoleCheck()) {
2351 __ ldr(scratch, target);
2352 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
2353 __ cmp(scratch, ip);
2354 if (instr->hydrogen()->DeoptimizesOnHole()) {
2355 DeoptimizeIf(eq, instr->environment());
2356 } else {
2357 __ b(ne, &skip_assignment);
2358 }
2359 }
2360
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002361 __ str(value, target);
2362 if (instr->hydrogen()->NeedsWriteBarrier()) {
2363 HType type = instr->hydrogen()->value()->type();
2364 SmiCheck check_needed =
2365 type.IsHeapObject() ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
2366 __ RecordWriteContextSlot(context,
2367 target.offset(),
2368 value,
Ben Murdochc7cc0282012-03-05 14:35:55 +00002369 scratch,
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002370 kLRHasBeenSaved,
2371 kSaveFPRegs,
2372 EMIT_REMEMBERED_SET,
2373 check_needed);
Steve Block1e0659c2011-05-24 12:43:12 +01002374 }
Ben Murdochc7cc0282012-03-05 14:35:55 +00002375
2376 __ bind(&skip_assignment);
Steve Block1e0659c2011-05-24 12:43:12 +01002377}
2378
2379
Ben Murdochb0fe1622011-05-05 13:52:32 +01002380void LCodeGen::DoLoadNamedField(LLoadNamedField* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01002381 Register object = ToRegister(instr->InputAt(0));
Steve Block9fac8402011-05-12 15:51:54 +01002382 Register result = ToRegister(instr->result());
2383 if (instr->hydrogen()->is_in_object()) {
2384 __ ldr(result, FieldMemOperand(object, instr->hydrogen()->offset()));
2385 } else {
2386 __ ldr(result, FieldMemOperand(object, JSObject::kPropertiesOffset));
2387 __ ldr(result, FieldMemOperand(result, instr->hydrogen()->offset()));
2388 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01002389}
2390
2391
Ben Murdoch257744e2011-11-30 15:57:28 +00002392void LCodeGen::EmitLoadFieldOrConstantFunction(Register result,
2393 Register object,
2394 Handle<Map> type,
2395 Handle<String> name) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002396 LookupResult lookup(isolate());
Steve Block44f0eee2011-05-26 01:26:41 +01002397 type->LookupInDescriptors(NULL, *name, &lookup);
Ben Murdochc7cc0282012-03-05 14:35:55 +00002398 ASSERT(lookup.IsFound() &&
Ben Murdoch257744e2011-11-30 15:57:28 +00002399 (lookup.type() == FIELD || lookup.type() == CONSTANT_FUNCTION));
2400 if (lookup.type() == FIELD) {
2401 int index = lookup.GetLocalFieldIndexFromMap(*type);
2402 int offset = index * kPointerSize;
2403 if (index < 0) {
2404 // Negative property indices are in-object properties, indexed
2405 // from the end of the fixed part of the object.
2406 __ ldr(result, FieldMemOperand(object, offset + type->instance_size()));
2407 } else {
2408 // Non-negative property indices are in the properties array.
2409 __ ldr(result, FieldMemOperand(object, JSObject::kPropertiesOffset));
2410 __ ldr(result, FieldMemOperand(result, offset + FixedArray::kHeaderSize));
2411 }
Steve Block44f0eee2011-05-26 01:26:41 +01002412 } else {
Ben Murdoch257744e2011-11-30 15:57:28 +00002413 Handle<JSFunction> function(lookup.GetConstantFunctionFromMap(*type));
Ben Murdochc7cc0282012-03-05 14:35:55 +00002414 __ LoadHeapObject(result, function);
Steve Block44f0eee2011-05-26 01:26:41 +01002415 }
2416}
2417
2418
2419void LCodeGen::DoLoadNamedFieldPolymorphic(LLoadNamedFieldPolymorphic* instr) {
2420 Register object = ToRegister(instr->object());
2421 Register result = ToRegister(instr->result());
2422 Register scratch = scratch0();
2423 int map_count = instr->hydrogen()->types()->length();
2424 Handle<String> name = instr->hydrogen()->name();
2425 if (map_count == 0) {
2426 ASSERT(instr->hydrogen()->need_generic());
2427 __ mov(r2, Operand(name));
2428 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
2429 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2430 } else {
2431 Label done;
2432 __ ldr(scratch, FieldMemOperand(object, HeapObject::kMapOffset));
2433 for (int i = 0; i < map_count - 1; ++i) {
2434 Handle<Map> map = instr->hydrogen()->types()->at(i);
2435 Label next;
2436 __ cmp(scratch, Operand(map));
2437 __ b(ne, &next);
Ben Murdoch257744e2011-11-30 15:57:28 +00002438 EmitLoadFieldOrConstantFunction(result, object, map, name);
Steve Block44f0eee2011-05-26 01:26:41 +01002439 __ b(&done);
2440 __ bind(&next);
2441 }
2442 Handle<Map> map = instr->hydrogen()->types()->last();
2443 __ cmp(scratch, Operand(map));
2444 if (instr->hydrogen()->need_generic()) {
2445 Label generic;
2446 __ b(ne, &generic);
Ben Murdoch257744e2011-11-30 15:57:28 +00002447 EmitLoadFieldOrConstantFunction(result, object, map, name);
Steve Block44f0eee2011-05-26 01:26:41 +01002448 __ b(&done);
2449 __ bind(&generic);
2450 __ mov(r2, Operand(name));
2451 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
2452 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2453 } else {
2454 DeoptimizeIf(ne, instr->environment());
Ben Murdoch257744e2011-11-30 15:57:28 +00002455 EmitLoadFieldOrConstantFunction(result, object, map, name);
Steve Block44f0eee2011-05-26 01:26:41 +01002456 }
2457 __ bind(&done);
2458 }
2459}
2460
2461
Ben Murdochb0fe1622011-05-05 13:52:32 +01002462void LCodeGen::DoLoadNamedGeneric(LLoadNamedGeneric* instr) {
2463 ASSERT(ToRegister(instr->object()).is(r0));
2464 ASSERT(ToRegister(instr->result()).is(r0));
2465
2466 // Name is always in r2.
2467 __ mov(r2, Operand(instr->name()));
Steve Block44f0eee2011-05-26 01:26:41 +01002468 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
Ben Murdochb0fe1622011-05-05 13:52:32 +01002469 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2470}
2471
2472
Steve Block9fac8402011-05-12 15:51:54 +01002473void LCodeGen::DoLoadFunctionPrototype(LLoadFunctionPrototype* instr) {
2474 Register scratch = scratch0();
2475 Register function = ToRegister(instr->function());
2476 Register result = ToRegister(instr->result());
2477
2478 // Check that the function really is a function. Load map into the
2479 // result register.
2480 __ CompareObjectType(function, result, scratch, JS_FUNCTION_TYPE);
2481 DeoptimizeIf(ne, instr->environment());
2482
2483 // Make sure that the function has an instance prototype.
2484 Label non_instance;
2485 __ ldrb(scratch, FieldMemOperand(result, Map::kBitFieldOffset));
2486 __ tst(scratch, Operand(1 << Map::kHasNonInstancePrototype));
2487 __ b(ne, &non_instance);
2488
2489 // Get the prototype or initial map from the function.
2490 __ ldr(result,
2491 FieldMemOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
2492
2493 // Check that the function has a prototype or an initial map.
2494 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
2495 __ cmp(result, ip);
2496 DeoptimizeIf(eq, instr->environment());
2497
2498 // If the function does not have an initial map, we're done.
2499 Label done;
2500 __ CompareObjectType(result, scratch, scratch, MAP_TYPE);
2501 __ b(ne, &done);
2502
2503 // Get the prototype from the initial map.
2504 __ ldr(result, FieldMemOperand(result, Map::kPrototypeOffset));
2505 __ jmp(&done);
2506
2507 // Non-instance prototype: Fetch prototype from constructor field
2508 // in initial map.
2509 __ bind(&non_instance);
2510 __ ldr(result, FieldMemOperand(result, Map::kConstructorOffset));
2511
2512 // All done.
2513 __ bind(&done);
2514}
2515
2516
Ben Murdochb0fe1622011-05-05 13:52:32 +01002517void LCodeGen::DoLoadElements(LLoadElements* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01002518 Register result = ToRegister(instr->result());
2519 Register input = ToRegister(instr->InputAt(0));
Ben Murdoch086aeea2011-05-13 15:57:08 +01002520 Register scratch = scratch0();
2521
Steve Block1e0659c2011-05-24 12:43:12 +01002522 __ ldr(result, FieldMemOperand(input, JSObject::kElementsOffset));
Ben Murdoch086aeea2011-05-13 15:57:08 +01002523 if (FLAG_debug_code) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002524 Label done, fail;
Steve Block1e0659c2011-05-24 12:43:12 +01002525 __ ldr(scratch, FieldMemOperand(result, HeapObject::kMapOffset));
Ben Murdoch086aeea2011-05-13 15:57:08 +01002526 __ LoadRoot(ip, Heap::kFixedArrayMapRootIndex);
2527 __ cmp(scratch, ip);
2528 __ b(eq, &done);
2529 __ LoadRoot(ip, Heap::kFixedCOWArrayMapRootIndex);
2530 __ cmp(scratch, ip);
Ben Murdoch8b112d22011-06-08 16:22:53 +01002531 __ b(eq, &done);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002532 // |scratch| still contains |input|'s map.
2533 __ ldr(scratch, FieldMemOperand(scratch, Map::kBitField2Offset));
2534 __ ubfx(scratch, scratch, Map::kElementsKindShift,
2535 Map::kElementsKindBitCount);
Ben Murdoch589d6972011-11-30 16:04:58 +00002536 __ cmp(scratch, Operand(FAST_ELEMENTS));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002537 __ b(eq, &done);
Ben Murdoch589d6972011-11-30 16:04:58 +00002538 __ cmp(scratch, Operand(FIRST_EXTERNAL_ARRAY_ELEMENTS_KIND));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002539 __ b(lt, &fail);
Ben Murdoch589d6972011-11-30 16:04:58 +00002540 __ cmp(scratch, Operand(LAST_EXTERNAL_ARRAY_ELEMENTS_KIND));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002541 __ b(le, &done);
2542 __ bind(&fail);
2543 __ Abort("Check for fast or external elements failed.");
Ben Murdoch086aeea2011-05-13 15:57:08 +01002544 __ bind(&done);
2545 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01002546}
2547
2548
Steve Block44f0eee2011-05-26 01:26:41 +01002549void LCodeGen::DoLoadExternalArrayPointer(
2550 LLoadExternalArrayPointer* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01002551 Register to_reg = ToRegister(instr->result());
2552 Register from_reg = ToRegister(instr->InputAt(0));
Steve Block44f0eee2011-05-26 01:26:41 +01002553 __ ldr(to_reg, FieldMemOperand(from_reg,
2554 ExternalArray::kExternalPointerOffset));
Steve Block1e0659c2011-05-24 12:43:12 +01002555}
2556
2557
Ben Murdochb0fe1622011-05-05 13:52:32 +01002558void LCodeGen::DoAccessArgumentsAt(LAccessArgumentsAt* instr) {
Ben Murdoch086aeea2011-05-13 15:57:08 +01002559 Register arguments = ToRegister(instr->arguments());
2560 Register length = ToRegister(instr->length());
2561 Register index = ToRegister(instr->index());
2562 Register result = ToRegister(instr->result());
2563
2564 // Bailout index is not a valid argument index. Use unsigned check to get
2565 // negative check for free.
2566 __ sub(length, length, index, SetCC);
2567 DeoptimizeIf(ls, instr->environment());
2568
2569 // There are two words between the frame pointer and the last argument.
2570 // Subtracting from length accounts for one of them add one more.
2571 __ add(length, length, Operand(1));
2572 __ ldr(result, MemOperand(arguments, length, LSL, kPointerSizeLog2));
Ben Murdochb0fe1622011-05-05 13:52:32 +01002573}
2574
2575
2576void LCodeGen::DoLoadKeyedFastElement(LLoadKeyedFastElement* instr) {
Ben Murdoch086aeea2011-05-13 15:57:08 +01002577 Register elements = ToRegister(instr->elements());
2578 Register key = EmitLoadRegister(instr->key(), scratch0());
Ben Murdochb8e0da22011-05-16 14:20:40 +01002579 Register result = ToRegister(instr->result());
Ben Murdoch086aeea2011-05-13 15:57:08 +01002580 Register scratch = scratch0();
Ben Murdoch086aeea2011-05-13 15:57:08 +01002581
2582 // Load the result.
2583 __ add(scratch, elements, Operand(key, LSL, kPointerSizeLog2));
2584 __ ldr(result, FieldMemOperand(scratch, FixedArray::kHeaderSize));
2585
Ben Murdochb8e0da22011-05-16 14:20:40 +01002586 // Check for the hole value.
Ben Murdoch257744e2011-11-30 15:57:28 +00002587 if (instr->hydrogen()->RequiresHoleCheck()) {
2588 __ LoadRoot(scratch, Heap::kTheHoleValueRootIndex);
2589 __ cmp(result, scratch);
2590 DeoptimizeIf(eq, instr->environment());
2591 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01002592}
2593
2594
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002595void LCodeGen::DoLoadKeyedFastDoubleElement(
2596 LLoadKeyedFastDoubleElement* instr) {
2597 Register elements = ToRegister(instr->elements());
2598 bool key_is_constant = instr->key()->IsConstantOperand();
2599 Register key = no_reg;
2600 DwVfpRegister result = ToDoubleRegister(instr->result());
2601 Register scratch = scratch0();
2602
2603 int shift_size =
Ben Murdoch589d6972011-11-30 16:04:58 +00002604 ElementsKindToShiftSize(FAST_DOUBLE_ELEMENTS);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002605 int constant_key = 0;
2606 if (key_is_constant) {
2607 constant_key = ToInteger32(LConstantOperand::cast(instr->key()));
2608 if (constant_key & 0xF0000000) {
2609 Abort("array index constant value too big.");
2610 }
2611 } else {
2612 key = ToRegister(instr->key());
2613 }
2614
2615 Operand operand = key_is_constant
2616 ? Operand(constant_key * (1 << shift_size) +
2617 FixedDoubleArray::kHeaderSize - kHeapObjectTag)
2618 : Operand(key, LSL, shift_size);
2619 __ add(elements, elements, operand);
2620 if (!key_is_constant) {
2621 __ add(elements, elements,
2622 Operand(FixedDoubleArray::kHeaderSize - kHeapObjectTag));
2623 }
2624
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002625 __ ldr(scratch, MemOperand(elements, sizeof(kHoleNanLower32)));
2626 __ cmp(scratch, Operand(kHoleNanUpper32));
2627 DeoptimizeIf(eq, instr->environment());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002628
2629 __ vldr(result, elements, 0);
2630}
2631
2632
Steve Block44f0eee2011-05-26 01:26:41 +01002633void LCodeGen::DoLoadKeyedSpecializedArrayElement(
2634 LLoadKeyedSpecializedArrayElement* instr) {
Steve Block44f0eee2011-05-26 01:26:41 +01002635 Register external_pointer = ToRegister(instr->external_pointer());
Ben Murdoch257744e2011-11-30 15:57:28 +00002636 Register key = no_reg;
Ben Murdoch589d6972011-11-30 16:04:58 +00002637 ElementsKind elements_kind = instr->elements_kind();
Ben Murdoch257744e2011-11-30 15:57:28 +00002638 bool key_is_constant = instr->key()->IsConstantOperand();
2639 int constant_key = 0;
2640 if (key_is_constant) {
2641 constant_key = ToInteger32(LConstantOperand::cast(instr->key()));
2642 if (constant_key & 0xF0000000) {
2643 Abort("array index constant value too big.");
2644 }
2645 } else {
2646 key = ToRegister(instr->key());
2647 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002648 int shift_size = ElementsKindToShiftSize(elements_kind);
Ben Murdoch257744e2011-11-30 15:57:28 +00002649
Ben Murdoch589d6972011-11-30 16:04:58 +00002650 if (elements_kind == EXTERNAL_FLOAT_ELEMENTS ||
2651 elements_kind == EXTERNAL_DOUBLE_ELEMENTS) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01002652 CpuFeatures::Scope scope(VFP3);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002653 DwVfpRegister result = ToDoubleRegister(instr->result());
2654 Operand operand = key_is_constant
2655 ? Operand(constant_key * (1 << shift_size))
2656 : Operand(key, LSL, shift_size);
Ben Murdoch257744e2011-11-30 15:57:28 +00002657 __ add(scratch0(), external_pointer, operand);
Ben Murdoch589d6972011-11-30 16:04:58 +00002658 if (elements_kind == EXTERNAL_FLOAT_ELEMENTS) {
Ben Murdoch257744e2011-11-30 15:57:28 +00002659 __ vldr(result.low(), scratch0(), 0);
2660 __ vcvt_f64_f32(result, result.low());
Ben Murdoch589d6972011-11-30 16:04:58 +00002661 } else { // i.e. elements_kind == EXTERNAL_DOUBLE_ELEMENTS
Ben Murdoch257744e2011-11-30 15:57:28 +00002662 __ vldr(result, scratch0(), 0);
2663 }
Ben Murdoch8b112d22011-06-08 16:22:53 +01002664 } else {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002665 Register result = ToRegister(instr->result());
Ben Murdoch257744e2011-11-30 15:57:28 +00002666 MemOperand mem_operand(key_is_constant
2667 ? MemOperand(external_pointer, constant_key * (1 << shift_size))
2668 : MemOperand(external_pointer, key, LSL, shift_size));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002669 switch (elements_kind) {
Ben Murdoch589d6972011-11-30 16:04:58 +00002670 case EXTERNAL_BYTE_ELEMENTS:
Ben Murdoch257744e2011-11-30 15:57:28 +00002671 __ ldrsb(result, mem_operand);
Ben Murdoch8b112d22011-06-08 16:22:53 +01002672 break;
Ben Murdoch589d6972011-11-30 16:04:58 +00002673 case EXTERNAL_PIXEL_ELEMENTS:
2674 case EXTERNAL_UNSIGNED_BYTE_ELEMENTS:
Ben Murdoch257744e2011-11-30 15:57:28 +00002675 __ ldrb(result, mem_operand);
Ben Murdoch8b112d22011-06-08 16:22:53 +01002676 break;
Ben Murdoch589d6972011-11-30 16:04:58 +00002677 case EXTERNAL_SHORT_ELEMENTS:
Ben Murdoch257744e2011-11-30 15:57:28 +00002678 __ ldrsh(result, mem_operand);
Ben Murdoch8b112d22011-06-08 16:22:53 +01002679 break;
Ben Murdoch589d6972011-11-30 16:04:58 +00002680 case EXTERNAL_UNSIGNED_SHORT_ELEMENTS:
Ben Murdoch257744e2011-11-30 15:57:28 +00002681 __ ldrh(result, mem_operand);
Ben Murdoch8b112d22011-06-08 16:22:53 +01002682 break;
Ben Murdoch589d6972011-11-30 16:04:58 +00002683 case EXTERNAL_INT_ELEMENTS:
Ben Murdoch257744e2011-11-30 15:57:28 +00002684 __ ldr(result, mem_operand);
Ben Murdoch8b112d22011-06-08 16:22:53 +01002685 break;
Ben Murdoch589d6972011-11-30 16:04:58 +00002686 case EXTERNAL_UNSIGNED_INT_ELEMENTS:
Ben Murdoch257744e2011-11-30 15:57:28 +00002687 __ ldr(result, mem_operand);
Ben Murdoch8b112d22011-06-08 16:22:53 +01002688 __ cmp(result, Operand(0x80000000));
2689 // TODO(danno): we could be more clever here, perhaps having a special
2690 // version of the stub that detects if the overflow case actually
2691 // happens, and generate code that returns a double rather than int.
2692 DeoptimizeIf(cs, instr->environment());
2693 break;
Ben Murdoch589d6972011-11-30 16:04:58 +00002694 case EXTERNAL_FLOAT_ELEMENTS:
2695 case EXTERNAL_DOUBLE_ELEMENTS:
2696 case FAST_DOUBLE_ELEMENTS:
2697 case FAST_ELEMENTS:
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002698 case FAST_SMI_ONLY_ELEMENTS:
Ben Murdoch589d6972011-11-30 16:04:58 +00002699 case DICTIONARY_ELEMENTS:
2700 case NON_STRICT_ARGUMENTS_ELEMENTS:
Ben Murdoch8b112d22011-06-08 16:22:53 +01002701 UNREACHABLE();
2702 break;
2703 }
2704 }
Steve Block1e0659c2011-05-24 12:43:12 +01002705}
2706
2707
Ben Murdochb0fe1622011-05-05 13:52:32 +01002708void LCodeGen::DoLoadKeyedGeneric(LLoadKeyedGeneric* instr) {
2709 ASSERT(ToRegister(instr->object()).is(r1));
2710 ASSERT(ToRegister(instr->key()).is(r0));
2711
Steve Block44f0eee2011-05-26 01:26:41 +01002712 Handle<Code> ic = isolate()->builtins()->KeyedLoadIC_Initialize();
Ben Murdochb0fe1622011-05-05 13:52:32 +01002713 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2714}
2715
2716
2717void LCodeGen::DoArgumentsElements(LArgumentsElements* instr) {
Ben Murdoch086aeea2011-05-13 15:57:08 +01002718 Register scratch = scratch0();
2719 Register result = ToRegister(instr->result());
2720
2721 // Check if the calling frame is an arguments adaptor frame.
2722 Label done, adapted;
2723 __ ldr(scratch, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2724 __ ldr(result, MemOperand(scratch, StandardFrameConstants::kContextOffset));
2725 __ cmp(result, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2726
2727 // Result is the frame pointer for the frame if not adapted and for the real
2728 // frame below the adaptor frame if adapted.
2729 __ mov(result, fp, LeaveCC, ne);
2730 __ mov(result, scratch, LeaveCC, eq);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002731}
2732
2733
2734void LCodeGen::DoArgumentsLength(LArgumentsLength* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01002735 Register elem = ToRegister(instr->InputAt(0));
Ben Murdoch086aeea2011-05-13 15:57:08 +01002736 Register result = ToRegister(instr->result());
2737
2738 Label done;
2739
2740 // If no arguments adaptor frame the number of arguments is fixed.
2741 __ cmp(fp, elem);
2742 __ mov(result, Operand(scope()->num_parameters()));
2743 __ b(eq, &done);
2744
2745 // Arguments adaptor frame present. Get argument length from there.
2746 __ ldr(result, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2747 __ ldr(result,
2748 MemOperand(result, ArgumentsAdaptorFrameConstants::kLengthOffset));
2749 __ SmiUntag(result);
2750
2751 // Argument length is in result register.
2752 __ bind(&done);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002753}
2754
2755
2756void LCodeGen::DoApplyArguments(LApplyArguments* instr) {
Ben Murdochb8e0da22011-05-16 14:20:40 +01002757 Register receiver = ToRegister(instr->receiver());
2758 Register function = ToRegister(instr->function());
Ben Murdochb8e0da22011-05-16 14:20:40 +01002759 Register length = ToRegister(instr->length());
2760 Register elements = ToRegister(instr->elements());
Steve Block1e0659c2011-05-24 12:43:12 +01002761 Register scratch = scratch0();
2762 ASSERT(receiver.is(r0)); // Used for parameter count.
2763 ASSERT(function.is(r1)); // Required by InvokeFunction.
2764 ASSERT(ToRegister(instr->result()).is(r0));
Ben Murdochb8e0da22011-05-16 14:20:40 +01002765
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002766 // If the receiver is null or undefined, we have to pass the global
2767 // object as a receiver to normal functions. Values have to be
2768 // passed unchanged to builtins and strict-mode functions.
Steve Block1e0659c2011-05-24 12:43:12 +01002769 Label global_object, receiver_ok;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002770
2771 // Do not transform the receiver to object for strict mode
2772 // functions.
2773 __ ldr(scratch,
2774 FieldMemOperand(function, JSFunction::kSharedFunctionInfoOffset));
2775 __ ldr(scratch,
2776 FieldMemOperand(scratch, SharedFunctionInfo::kCompilerHintsOffset));
2777 __ tst(scratch,
2778 Operand(1 << (SharedFunctionInfo::kStrictModeFunction + kSmiTagSize)));
2779 __ b(ne, &receiver_ok);
2780
2781 // Do not transform the receiver to object for builtins.
2782 __ tst(scratch, Operand(1 << (SharedFunctionInfo::kNative + kSmiTagSize)));
2783 __ b(ne, &receiver_ok);
2784
2785 // Normal function. Replace undefined or null with global receiver.
Steve Block1e0659c2011-05-24 12:43:12 +01002786 __ LoadRoot(scratch, Heap::kNullValueRootIndex);
2787 __ cmp(receiver, scratch);
2788 __ b(eq, &global_object);
2789 __ LoadRoot(scratch, Heap::kUndefinedValueRootIndex);
2790 __ cmp(receiver, scratch);
2791 __ b(eq, &global_object);
2792
2793 // Deoptimize if the receiver is not a JS object.
2794 __ tst(receiver, Operand(kSmiTagMask));
2795 DeoptimizeIf(eq, instr->environment());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002796 __ CompareObjectType(receiver, scratch, scratch, FIRST_SPEC_OBJECT_TYPE);
2797 DeoptimizeIf(lt, instr->environment());
Steve Block1e0659c2011-05-24 12:43:12 +01002798 __ jmp(&receiver_ok);
2799
2800 __ bind(&global_object);
2801 __ ldr(receiver, GlobalObjectOperand());
Ben Murdoch257744e2011-11-30 15:57:28 +00002802 __ ldr(receiver,
2803 FieldMemOperand(receiver, JSGlobalObject::kGlobalReceiverOffset));
Steve Block1e0659c2011-05-24 12:43:12 +01002804 __ bind(&receiver_ok);
Ben Murdochb8e0da22011-05-16 14:20:40 +01002805
2806 // Copy the arguments to this function possibly from the
2807 // adaptor frame below it.
2808 const uint32_t kArgumentsLimit = 1 * KB;
2809 __ cmp(length, Operand(kArgumentsLimit));
2810 DeoptimizeIf(hi, instr->environment());
2811
2812 // Push the receiver and use the register to keep the original
2813 // number of arguments.
2814 __ push(receiver);
2815 __ mov(receiver, length);
2816 // The arguments are at a one pointer size offset from elements.
2817 __ add(elements, elements, Operand(1 * kPointerSize));
2818
2819 // Loop through the arguments pushing them onto the execution
2820 // stack.
Steve Block1e0659c2011-05-24 12:43:12 +01002821 Label invoke, loop;
Ben Murdochb8e0da22011-05-16 14:20:40 +01002822 // length is a small non-negative integer, due to the test above.
Steve Block44f0eee2011-05-26 01:26:41 +01002823 __ cmp(length, Operand(0));
Ben Murdochb8e0da22011-05-16 14:20:40 +01002824 __ b(eq, &invoke);
2825 __ bind(&loop);
2826 __ ldr(scratch, MemOperand(elements, length, LSL, 2));
2827 __ push(scratch);
2828 __ sub(length, length, Operand(1), SetCC);
2829 __ b(ne, &loop);
2830
2831 __ bind(&invoke);
Steve Block1e0659c2011-05-24 12:43:12 +01002832 ASSERT(instr->HasPointerMap() && instr->HasDeoptimizationEnvironment());
2833 LPointerMap* pointers = instr->pointer_map();
Steve Block1e0659c2011-05-24 12:43:12 +01002834 RecordPosition(pointers->position());
Ben Murdoch2b4ba112012-01-20 14:57:15 +00002835 SafepointGenerator safepoint_generator(
2836 this, pointers, Safepoint::kLazyDeopt);
Steve Block1e0659c2011-05-24 12:43:12 +01002837 // The number of arguments is stored in receiver which is r0, as expected
2838 // by InvokeFunction.
Ben Murdochc7cc0282012-03-05 14:35:55 +00002839 ParameterCount actual(receiver);
Ben Murdoch257744e2011-11-30 15:57:28 +00002840 __ InvokeFunction(function, actual, CALL_FUNCTION,
2841 safepoint_generator, CALL_AS_METHOD);
Steve Block1e0659c2011-05-24 12:43:12 +01002842 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
Ben Murdochb0fe1622011-05-05 13:52:32 +01002843}
2844
2845
2846void LCodeGen::DoPushArgument(LPushArgument* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01002847 LOperand* argument = instr->InputAt(0);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002848 if (argument->IsDoubleRegister() || argument->IsDoubleStackSlot()) {
2849 Abort("DoPushArgument not implemented for double type.");
2850 } else {
2851 Register argument_reg = EmitLoadRegister(argument, ip);
2852 __ push(argument_reg);
2853 }
2854}
2855
2856
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002857void LCodeGen::DoThisFunction(LThisFunction* instr) {
2858 Register result = ToRegister(instr->result());
Ben Murdochc7cc0282012-03-05 14:35:55 +00002859 __ LoadHeapObject(result, instr->hydrogen()->closure());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002860}
2861
2862
Steve Block1e0659c2011-05-24 12:43:12 +01002863void LCodeGen::DoContext(LContext* instr) {
2864 Register result = ToRegister(instr->result());
2865 __ mov(result, cp);
2866}
2867
2868
2869void LCodeGen::DoOuterContext(LOuterContext* instr) {
2870 Register context = ToRegister(instr->context());
2871 Register result = ToRegister(instr->result());
2872 __ ldr(result,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002873 MemOperand(context, Context::SlotOffset(Context::PREVIOUS_INDEX)));
Steve Block1e0659c2011-05-24 12:43:12 +01002874}
2875
2876
Ben Murdochb0fe1622011-05-05 13:52:32 +01002877void LCodeGen::DoGlobalObject(LGlobalObject* instr) {
2878 Register result = ToRegister(instr->result());
2879 __ ldr(result, ContextOperand(cp, Context::GLOBAL_INDEX));
2880}
2881
2882
2883void LCodeGen::DoGlobalReceiver(LGlobalReceiver* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01002884 Register global = ToRegister(instr->global());
Ben Murdochb0fe1622011-05-05 13:52:32 +01002885 Register result = ToRegister(instr->result());
Steve Block1e0659c2011-05-24 12:43:12 +01002886 __ ldr(result, FieldMemOperand(global, GlobalObject::kGlobalReceiverOffset));
Ben Murdochb0fe1622011-05-05 13:52:32 +01002887}
2888
2889
2890void LCodeGen::CallKnownFunction(Handle<JSFunction> function,
2891 int arity,
Ben Murdoch257744e2011-11-30 15:57:28 +00002892 LInstruction* instr,
2893 CallKind call_kind) {
Ben Murdochc7cc0282012-03-05 14:35:55 +00002894 bool can_invoke_directly = !function->NeedsArgumentsAdaption() ||
2895 function->shared()->formal_parameter_count() == arity;
Ben Murdochb0fe1622011-05-05 13:52:32 +01002896
2897 LPointerMap* pointers = instr->pointer_map();
2898 RecordPosition(pointers->position());
2899
Ben Murdochc7cc0282012-03-05 14:35:55 +00002900 if (can_invoke_directly) {
2901 __ LoadHeapObject(r1, function);
2902 // Change context if needed.
2903 bool change_context =
2904 (info()->closure()->context() != function->context()) ||
2905 scope()->contains_with() ||
2906 (scope()->num_heap_slots() > 0);
2907 if (change_context) {
2908 __ ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
2909 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01002910
Ben Murdochc7cc0282012-03-05 14:35:55 +00002911 // Set r0 to arguments count if adaption is not needed. Assumes that r0
2912 // is available to write to at this point.
2913 if (!function->NeedsArgumentsAdaption()) {
2914 __ mov(r0, Operand(arity));
2915 }
2916
2917 // Invoke function.
2918 __ SetCallKind(r5, call_kind);
2919 __ ldr(ip, FieldMemOperand(r1, JSFunction::kCodeEntryOffset));
2920 __ Call(ip);
2921
2922 // Set up deoptimization.
2923 RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT);
2924 } else {
2925 SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
2926 ParameterCount count(arity);
2927 __ InvokeFunction(function, count, CALL_FUNCTION, generator, call_kind);
2928 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01002929
2930 // Restore context.
2931 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2932}
2933
2934
2935void LCodeGen::DoCallConstantFunction(LCallConstantFunction* instr) {
Steve Block9fac8402011-05-12 15:51:54 +01002936 ASSERT(ToRegister(instr->result()).is(r0));
Ben Murdoch257744e2011-11-30 15:57:28 +00002937 CallKnownFunction(instr->function(),
2938 instr->arity(),
2939 instr,
2940 CALL_AS_METHOD);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002941}
2942
2943
2944void LCodeGen::DoDeferredMathAbsTaggedHeapNumber(LUnaryMathOperation* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01002945 Register input = ToRegister(instr->InputAt(0));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002946 Register result = ToRegister(instr->result());
Steve Block1e0659c2011-05-24 12:43:12 +01002947 Register scratch = scratch0();
2948
2949 // Deoptimize if not a heap number.
2950 __ ldr(scratch, FieldMemOperand(input, HeapObject::kMapOffset));
2951 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
2952 __ cmp(scratch, Operand(ip));
2953 DeoptimizeIf(ne, instr->environment());
2954
2955 Label done;
2956 Register exponent = scratch0();
2957 scratch = no_reg;
2958 __ ldr(exponent, FieldMemOperand(input, HeapNumber::kExponentOffset));
2959 // Check the sign of the argument. If the argument is positive, just
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002960 // return it.
Steve Block1e0659c2011-05-24 12:43:12 +01002961 __ tst(exponent, Operand(HeapNumber::kSignMask));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002962 // Move the input to the result if necessary.
2963 __ Move(result, input);
Steve Block1e0659c2011-05-24 12:43:12 +01002964 __ b(eq, &done);
2965
2966 // Input is negative. Reverse its sign.
2967 // Preserve the value of all registers.
Ben Murdoch8b112d22011-06-08 16:22:53 +01002968 {
2969 PushSafepointRegistersScope scope(this, Safepoint::kWithRegisters);
Steve Block1e0659c2011-05-24 12:43:12 +01002970
Ben Murdoch8b112d22011-06-08 16:22:53 +01002971 // Registers were saved at the safepoint, so we can use
2972 // many scratch registers.
2973 Register tmp1 = input.is(r1) ? r0 : r1;
2974 Register tmp2 = input.is(r2) ? r0 : r2;
2975 Register tmp3 = input.is(r3) ? r0 : r3;
2976 Register tmp4 = input.is(r4) ? r0 : r4;
Steve Block1e0659c2011-05-24 12:43:12 +01002977
Ben Murdoch8b112d22011-06-08 16:22:53 +01002978 // exponent: floating point exponent value.
Steve Block1e0659c2011-05-24 12:43:12 +01002979
Ben Murdoch8b112d22011-06-08 16:22:53 +01002980 Label allocated, slow;
2981 __ LoadRoot(tmp4, Heap::kHeapNumberMapRootIndex);
2982 __ AllocateHeapNumber(tmp1, tmp2, tmp3, tmp4, &slow);
2983 __ b(&allocated);
Steve Block1e0659c2011-05-24 12:43:12 +01002984
Ben Murdoch8b112d22011-06-08 16:22:53 +01002985 // Slow case: Call the runtime system to do the number allocation.
2986 __ bind(&slow);
Steve Block1e0659c2011-05-24 12:43:12 +01002987
Ben Murdoch8b112d22011-06-08 16:22:53 +01002988 CallRuntimeFromDeferred(Runtime::kAllocateHeapNumber, 0, instr);
2989 // Set the pointer to the new heap number in tmp.
2990 if (!tmp1.is(r0)) __ mov(tmp1, Operand(r0));
2991 // Restore input_reg after call to runtime.
2992 __ LoadFromSafepointRegisterSlot(input, input);
2993 __ ldr(exponent, FieldMemOperand(input, HeapNumber::kExponentOffset));
Steve Block1e0659c2011-05-24 12:43:12 +01002994
Ben Murdoch8b112d22011-06-08 16:22:53 +01002995 __ bind(&allocated);
2996 // exponent: floating point exponent value.
2997 // tmp1: allocated heap number.
2998 __ bic(exponent, exponent, Operand(HeapNumber::kSignMask));
2999 __ str(exponent, FieldMemOperand(tmp1, HeapNumber::kExponentOffset));
3000 __ ldr(tmp2, FieldMemOperand(input, HeapNumber::kMantissaOffset));
3001 __ str(tmp2, FieldMemOperand(tmp1, HeapNumber::kMantissaOffset));
Steve Block1e0659c2011-05-24 12:43:12 +01003002
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003003 __ StoreToSafepointRegisterSlot(tmp1, result);
Ben Murdoch8b112d22011-06-08 16:22:53 +01003004 }
Steve Block1e0659c2011-05-24 12:43:12 +01003005
3006 __ bind(&done);
3007}
3008
3009
3010void LCodeGen::EmitIntegerMathAbs(LUnaryMathOperation* instr) {
3011 Register input = ToRegister(instr->InputAt(0));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003012 Register result = ToRegister(instr->result());
Steve Block1e0659c2011-05-24 12:43:12 +01003013 __ cmp(input, Operand(0));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003014 __ Move(result, input, pl);
Steve Block1e0659c2011-05-24 12:43:12 +01003015 // We can make rsb conditional because the previous cmp instruction
3016 // will clear the V (overflow) flag and rsb won't set this flag
3017 // if input is positive.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003018 __ rsb(result, input, Operand(0), SetCC, mi);
Steve Block1e0659c2011-05-24 12:43:12 +01003019 // Deoptimize on overflow.
3020 DeoptimizeIf(vs, instr->environment());
Ben Murdochb0fe1622011-05-05 13:52:32 +01003021}
3022
3023
3024void LCodeGen::DoMathAbs(LUnaryMathOperation* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01003025 // Class for deferred case.
3026 class DeferredMathAbsTaggedHeapNumber: public LDeferredCode {
3027 public:
3028 DeferredMathAbsTaggedHeapNumber(LCodeGen* codegen,
3029 LUnaryMathOperation* instr)
3030 : LDeferredCode(codegen), instr_(instr) { }
3031 virtual void Generate() {
3032 codegen()->DoDeferredMathAbsTaggedHeapNumber(instr_);
3033 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003034 virtual LInstruction* instr() { return instr_; }
Steve Block1e0659c2011-05-24 12:43:12 +01003035 private:
3036 LUnaryMathOperation* instr_;
3037 };
3038
Steve Block1e0659c2011-05-24 12:43:12 +01003039 Representation r = instr->hydrogen()->value()->representation();
3040 if (r.IsDouble()) {
3041 DwVfpRegister input = ToDoubleRegister(instr->InputAt(0));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003042 DwVfpRegister result = ToDoubleRegister(instr->result());
3043 __ vabs(result, input);
Steve Block1e0659c2011-05-24 12:43:12 +01003044 } else if (r.IsInteger32()) {
3045 EmitIntegerMathAbs(instr);
3046 } else {
3047 // Representation is tagged.
3048 DeferredMathAbsTaggedHeapNumber* deferred =
3049 new DeferredMathAbsTaggedHeapNumber(this, instr);
3050 Register input = ToRegister(instr->InputAt(0));
3051 // Smi check.
3052 __ JumpIfNotSmi(input, deferred->entry());
3053 // If smi, handle it directly.
3054 EmitIntegerMathAbs(instr);
3055 __ bind(deferred->exit());
3056 }
3057}
3058
3059
Ben Murdochb0fe1622011-05-05 13:52:32 +01003060void LCodeGen::DoMathFloor(LUnaryMathOperation* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01003061 DoubleRegister input = ToDoubleRegister(instr->InputAt(0));
Ben Murdochb8e0da22011-05-16 14:20:40 +01003062 Register result = ToRegister(instr->result());
Ben Murdochb8e0da22011-05-16 14:20:40 +01003063 SwVfpRegister single_scratch = double_scratch0().low();
Steve Block1e0659c2011-05-24 12:43:12 +01003064 Register scratch1 = scratch0();
3065 Register scratch2 = ToRegister(instr->TempAt(0));
Ben Murdochb8e0da22011-05-16 14:20:40 +01003066
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003067 __ EmitVFPTruncate(kRoundToMinusInf,
3068 single_scratch,
3069 input,
3070 scratch1,
3071 scratch2);
Ben Murdochb8e0da22011-05-16 14:20:40 +01003072 DeoptimizeIf(ne, instr->environment());
3073
3074 // Move the result back to general purpose register r0.
3075 __ vmov(result, single_scratch);
3076
Steve Block44f0eee2011-05-26 01:26:41 +01003077 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3078 // Test for -0.
3079 Label done;
3080 __ cmp(result, Operand(0));
3081 __ b(ne, &done);
3082 __ vmov(scratch1, input.high());
3083 __ tst(scratch1, Operand(HeapNumber::kSignMask));
3084 DeoptimizeIf(ne, instr->environment());
3085 __ bind(&done);
3086 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01003087}
3088
3089
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003090void LCodeGen::DoMathRound(LUnaryMathOperation* instr) {
3091 DoubleRegister input = ToDoubleRegister(instr->InputAt(0));
3092 Register result = ToRegister(instr->result());
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003093 Register scratch = scratch0();
Ben Murdoch257744e2011-11-30 15:57:28 +00003094 Label done, check_sign_on_zero;
3095
3096 // Extract exponent bits.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003097 __ vmov(result, input.high());
3098 __ ubfx(scratch,
3099 result,
Ben Murdoch257744e2011-11-30 15:57:28 +00003100 HeapNumber::kExponentShift,
3101 HeapNumber::kExponentBits);
3102
3103 // If the number is in ]-0.5, +0.5[, the result is +/- 0.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003104 __ cmp(scratch, Operand(HeapNumber::kExponentBias - 2));
Ben Murdoch257744e2011-11-30 15:57:28 +00003105 __ mov(result, Operand(0), LeaveCC, le);
3106 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3107 __ b(le, &check_sign_on_zero);
3108 } else {
3109 __ b(le, &done);
3110 }
3111
3112 // The following conversion will not work with numbers
3113 // outside of ]-2^32, 2^32[.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003114 __ cmp(scratch, Operand(HeapNumber::kExponentBias + 32));
Ben Murdoch257744e2011-11-30 15:57:28 +00003115 DeoptimizeIf(ge, instr->environment());
3116
3117 // Save the original sign for later comparison.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003118 __ and_(scratch, result, Operand(HeapNumber::kSignMask));
Ben Murdoch257744e2011-11-30 15:57:28 +00003119
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003120 __ Vmov(double_scratch0(), 0.5);
Ben Murdoch692be652012-01-10 18:47:50 +00003121 __ vadd(double_scratch0(), input, double_scratch0());
Ben Murdoch257744e2011-11-30 15:57:28 +00003122
3123 // Check sign of the result: if the sign changed, the input
3124 // value was in ]0.5, 0[ and the result should be -0.
Ben Murdoch692be652012-01-10 18:47:50 +00003125 __ vmov(result, double_scratch0().high());
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003126 __ eor(result, result, Operand(scratch), SetCC);
Ben Murdoch257744e2011-11-30 15:57:28 +00003127 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3128 DeoptimizeIf(mi, instr->environment());
3129 } else {
3130 __ mov(result, Operand(0), LeaveCC, mi);
3131 __ b(mi, &done);
3132 }
3133
3134 __ EmitVFPTruncate(kRoundToMinusInf,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003135 double_scratch0().low(),
Ben Murdoch692be652012-01-10 18:47:50 +00003136 double_scratch0(),
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003137 result,
3138 scratch);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003139 DeoptimizeIf(ne, instr->environment());
3140 __ vmov(result, double_scratch0().low());
3141
Steve Block44f0eee2011-05-26 01:26:41 +01003142 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3143 // Test for -0.
Steve Block44f0eee2011-05-26 01:26:41 +01003144 __ cmp(result, Operand(0));
3145 __ b(ne, &done);
Ben Murdoch257744e2011-11-30 15:57:28 +00003146 __ bind(&check_sign_on_zero);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003147 __ vmov(scratch, input.high());
3148 __ tst(scratch, Operand(HeapNumber::kSignMask));
Steve Block44f0eee2011-05-26 01:26:41 +01003149 DeoptimizeIf(ne, instr->environment());
Steve Block44f0eee2011-05-26 01:26:41 +01003150 }
Ben Murdoch257744e2011-11-30 15:57:28 +00003151 __ bind(&done);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003152}
3153
3154
Ben Murdochb0fe1622011-05-05 13:52:32 +01003155void LCodeGen::DoMathSqrt(LUnaryMathOperation* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01003156 DoubleRegister input = ToDoubleRegister(instr->InputAt(0));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003157 DoubleRegister result = ToDoubleRegister(instr->result());
3158 __ vsqrt(result, input);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003159}
3160
3161
Steve Block44f0eee2011-05-26 01:26:41 +01003162void LCodeGen::DoMathPowHalf(LUnaryMathOperation* instr) {
3163 DoubleRegister input = ToDoubleRegister(instr->InputAt(0));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003164 DoubleRegister result = ToDoubleRegister(instr->result());
Ben Murdochc7cc0282012-03-05 14:35:55 +00003165 DoubleRegister temp = ToDoubleRegister(instr->TempAt(0));
3166
3167 // Note that according to ECMA-262 15.8.2.13:
3168 // Math.pow(-Infinity, 0.5) == Infinity
3169 // Math.sqrt(-Infinity) == NaN
3170 Label done;
3171 __ vmov(temp, -V8_INFINITY);
3172 __ VFPCompareAndSetFlags(input, temp);
3173 __ vneg(result, temp, eq);
3174 __ b(&done, eq);
3175
Steve Block44f0eee2011-05-26 01:26:41 +01003176 // Add +0 to convert -0 to +0.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003177 __ vadd(result, input, kDoubleRegZero);
3178 __ vsqrt(result, result);
Ben Murdochc7cc0282012-03-05 14:35:55 +00003179 __ bind(&done);
Steve Block44f0eee2011-05-26 01:26:41 +01003180}
3181
3182
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003183void LCodeGen::DoPower(LPower* instr) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003184 Representation exponent_type = instr->hydrogen()->right()->representation();
Ben Murdochc7cc0282012-03-05 14:35:55 +00003185 // Having marked this as a call, we can use any registers.
3186 // Just make sure that the input/output registers are the expected ones.
3187 ASSERT(!instr->InputAt(1)->IsDoubleRegister() ||
3188 ToDoubleRegister(instr->InputAt(1)).is(d2));
3189 ASSERT(!instr->InputAt(1)->IsRegister() ||
3190 ToRegister(instr->InputAt(1)).is(r2));
3191 ASSERT(ToDoubleRegister(instr->InputAt(0)).is(d1));
3192 ASSERT(ToDoubleRegister(instr->result()).is(d3));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003193
Ben Murdochc7cc0282012-03-05 14:35:55 +00003194 if (exponent_type.IsTagged()) {
3195 Label no_deopt;
3196 __ JumpIfSmi(r2, &no_deopt);
3197 __ ldr(r7, FieldMemOperand(r2, HeapObject::kMapOffset));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003198 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
Ben Murdochc7cc0282012-03-05 14:35:55 +00003199 __ cmp(r7, Operand(ip));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003200 DeoptimizeIf(ne, instr->environment());
Ben Murdochc7cc0282012-03-05 14:35:55 +00003201 __ bind(&no_deopt);
3202 MathPowStub stub(MathPowStub::TAGGED);
3203 __ CallStub(&stub);
3204 } else if (exponent_type.IsInteger32()) {
3205 MathPowStub stub(MathPowStub::INTEGER);
3206 __ CallStub(&stub);
3207 } else {
3208 ASSERT(exponent_type.IsDouble());
3209 MathPowStub stub(MathPowStub::DOUBLE);
3210 __ CallStub(&stub);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003211 }
Ben Murdochc7cc0282012-03-05 14:35:55 +00003212}
3213
3214
3215void LCodeGen::DoRandom(LRandom* instr) {
3216 // Having marked this instruction as a call we can use any
3217 // registers.
3218 ASSERT(ToDoubleRegister(instr->result()).is(d7));
3219 ASSERT(ToRegister(instr->InputAt(0)).is(r0));
3220
3221 __ PrepareCallCFunction(1, scratch0());
3222 __ ldr(r0, FieldMemOperand(r0, GlobalObject::kGlobalContextOffset));
3223 __ CallCFunction(ExternalReference::random_uint32_function(isolate()), 1);
3224
3225 // 0x41300000 is the top half of 1.0 x 2^20 as a double.
3226 // Create this constant using mov/orr to avoid PC relative load.
3227 __ mov(r1, Operand(0x41000000));
3228 __ orr(r1, r1, Operand(0x300000));
3229 // Move 0x41300000xxxxxxxx (x = random bits) to VFP.
3230 __ vmov(d7, r0, r1);
3231 // Move 0x4130000000000000 to VFP.
3232 __ mov(r0, Operand(0, RelocInfo::NONE));
3233 __ vmov(d8, r0, r1);
3234 // Subtract and store the result in the heap number.
3235 __ vsub(d7, d7, d8);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003236}
3237
3238
3239void LCodeGen::DoMathLog(LUnaryMathOperation* instr) {
3240 ASSERT(ToDoubleRegister(instr->result()).is(d2));
3241 TranscendentalCacheStub stub(TranscendentalCache::LOG,
3242 TranscendentalCacheStub::UNTAGGED);
3243 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
3244}
3245
3246
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003247void LCodeGen::DoMathTan(LUnaryMathOperation* instr) {
3248 ASSERT(ToDoubleRegister(instr->result()).is(d2));
3249 TranscendentalCacheStub stub(TranscendentalCache::TAN,
3250 TranscendentalCacheStub::UNTAGGED);
3251 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
3252}
3253
3254
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003255void LCodeGen::DoMathCos(LUnaryMathOperation* instr) {
3256 ASSERT(ToDoubleRegister(instr->result()).is(d2));
3257 TranscendentalCacheStub stub(TranscendentalCache::COS,
3258 TranscendentalCacheStub::UNTAGGED);
3259 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
3260}
3261
3262
3263void LCodeGen::DoMathSin(LUnaryMathOperation* instr) {
3264 ASSERT(ToDoubleRegister(instr->result()).is(d2));
3265 TranscendentalCacheStub stub(TranscendentalCache::SIN,
3266 TranscendentalCacheStub::UNTAGGED);
3267 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
3268}
3269
3270
Ben Murdochb0fe1622011-05-05 13:52:32 +01003271void LCodeGen::DoUnaryMathOperation(LUnaryMathOperation* instr) {
3272 switch (instr->op()) {
3273 case kMathAbs:
3274 DoMathAbs(instr);
3275 break;
3276 case kMathFloor:
3277 DoMathFloor(instr);
3278 break;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003279 case kMathRound:
3280 DoMathRound(instr);
3281 break;
Ben Murdochb0fe1622011-05-05 13:52:32 +01003282 case kMathSqrt:
3283 DoMathSqrt(instr);
3284 break;
Steve Block44f0eee2011-05-26 01:26:41 +01003285 case kMathPowHalf:
3286 DoMathPowHalf(instr);
3287 break;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003288 case kMathCos:
3289 DoMathCos(instr);
3290 break;
3291 case kMathSin:
3292 DoMathSin(instr);
3293 break;
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003294 case kMathTan:
3295 DoMathTan(instr);
3296 break;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003297 case kMathLog:
3298 DoMathLog(instr);
3299 break;
Ben Murdochb0fe1622011-05-05 13:52:32 +01003300 default:
3301 Abort("Unimplemented type of LUnaryMathOperation.");
3302 UNREACHABLE();
3303 }
3304}
3305
3306
Ben Murdoch257744e2011-11-30 15:57:28 +00003307void LCodeGen::DoInvokeFunction(LInvokeFunction* instr) {
3308 ASSERT(ToRegister(instr->function()).is(r1));
3309 ASSERT(instr->HasPointerMap());
3310 ASSERT(instr->HasDeoptimizationEnvironment());
3311 LPointerMap* pointers = instr->pointer_map();
Ben Murdoch257744e2011-11-30 15:57:28 +00003312 RecordPosition(pointers->position());
Ben Murdoch2b4ba112012-01-20 14:57:15 +00003313 SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
Ben Murdoch257744e2011-11-30 15:57:28 +00003314 ParameterCount count(instr->arity());
3315 __ InvokeFunction(r1, count, CALL_FUNCTION, generator, CALL_AS_METHOD);
3316 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3317}
3318
3319
Ben Murdochb0fe1622011-05-05 13:52:32 +01003320void LCodeGen::DoCallKeyed(LCallKeyed* instr) {
Ben Murdoch086aeea2011-05-13 15:57:08 +01003321 ASSERT(ToRegister(instr->result()).is(r0));
3322
3323 int arity = instr->arity();
Steve Block44f0eee2011-05-26 01:26:41 +01003324 Handle<Code> ic =
Ben Murdoch589d6972011-11-30 16:04:58 +00003325 isolate()->stub_cache()->ComputeKeyedCallInitialize(arity);
Ben Murdoch086aeea2011-05-13 15:57:08 +01003326 CallCode(ic, RelocInfo::CODE_TARGET, instr);
3327 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
Ben Murdochb0fe1622011-05-05 13:52:32 +01003328}
3329
3330
3331void LCodeGen::DoCallNamed(LCallNamed* instr) {
3332 ASSERT(ToRegister(instr->result()).is(r0));
3333
3334 int arity = instr->arity();
Ben Murdoch257744e2011-11-30 15:57:28 +00003335 RelocInfo::Mode mode = RelocInfo::CODE_TARGET;
3336 Handle<Code> ic =
Ben Murdoch589d6972011-11-30 16:04:58 +00003337 isolate()->stub_cache()->ComputeCallInitialize(arity, mode);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003338 __ mov(r2, Operand(instr->name()));
Ben Murdoch257744e2011-11-30 15:57:28 +00003339 CallCode(ic, mode, instr);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003340 // Restore context register.
3341 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3342}
3343
3344
3345void LCodeGen::DoCallFunction(LCallFunction* instr) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003346 ASSERT(ToRegister(instr->function()).is(r1));
Steve Block9fac8402011-05-12 15:51:54 +01003347 ASSERT(ToRegister(instr->result()).is(r0));
3348
3349 int arity = instr->arity();
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003350 CallFunctionStub stub(arity, NO_CALL_FUNCTION_FLAGS);
Steve Block9fac8402011-05-12 15:51:54 +01003351 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
Steve Block9fac8402011-05-12 15:51:54 +01003352 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
Ben Murdochb0fe1622011-05-05 13:52:32 +01003353}
3354
3355
3356void LCodeGen::DoCallGlobal(LCallGlobal* instr) {
Ben Murdoch086aeea2011-05-13 15:57:08 +01003357 ASSERT(ToRegister(instr->result()).is(r0));
3358
3359 int arity = instr->arity();
Ben Murdoch257744e2011-11-30 15:57:28 +00003360 RelocInfo::Mode mode = RelocInfo::CODE_TARGET_CONTEXT;
Steve Block44f0eee2011-05-26 01:26:41 +01003361 Handle<Code> ic =
Ben Murdoch589d6972011-11-30 16:04:58 +00003362 isolate()->stub_cache()->ComputeCallInitialize(arity, mode);
Ben Murdoch086aeea2011-05-13 15:57:08 +01003363 __ mov(r2, Operand(instr->name()));
Ben Murdoch257744e2011-11-30 15:57:28 +00003364 CallCode(ic, mode, instr);
Ben Murdoch086aeea2011-05-13 15:57:08 +01003365 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
Ben Murdochb0fe1622011-05-05 13:52:32 +01003366}
3367
3368
3369void LCodeGen::DoCallKnownGlobal(LCallKnownGlobal* instr) {
3370 ASSERT(ToRegister(instr->result()).is(r0));
Ben Murdoch257744e2011-11-30 15:57:28 +00003371 CallKnownFunction(instr->target(), instr->arity(), instr, CALL_AS_FUNCTION);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003372}
3373
3374
3375void LCodeGen::DoCallNew(LCallNew* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01003376 ASSERT(ToRegister(instr->InputAt(0)).is(r1));
Ben Murdochb0fe1622011-05-05 13:52:32 +01003377 ASSERT(ToRegister(instr->result()).is(r0));
3378
Steve Block44f0eee2011-05-26 01:26:41 +01003379 Handle<Code> builtin = isolate()->builtins()->JSConstructCall();
Ben Murdochb0fe1622011-05-05 13:52:32 +01003380 __ mov(r0, Operand(instr->arity()));
3381 CallCode(builtin, RelocInfo::CONSTRUCT_CALL, instr);
3382}
3383
3384
3385void LCodeGen::DoCallRuntime(LCallRuntime* instr) {
3386 CallRuntime(instr->function(), instr->arity(), instr);
3387}
3388
3389
3390void LCodeGen::DoStoreNamedField(LStoreNamedField* instr) {
Ben Murdoch086aeea2011-05-13 15:57:08 +01003391 Register object = ToRegister(instr->object());
3392 Register value = ToRegister(instr->value());
3393 Register scratch = scratch0();
3394 int offset = instr->offset();
3395
3396 ASSERT(!object.is(value));
3397
3398 if (!instr->transition().is_null()) {
3399 __ mov(scratch, Operand(instr->transition()));
3400 __ str(scratch, FieldMemOperand(object, HeapObject::kMapOffset));
3401 }
3402
3403 // Do the store.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003404 HType type = instr->hydrogen()->value()->type();
3405 SmiCheck check_needed =
3406 type.IsHeapObject() ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
Ben Murdoch086aeea2011-05-13 15:57:08 +01003407 if (instr->is_in_object()) {
3408 __ str(value, FieldMemOperand(object, offset));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003409 if (instr->hydrogen()->NeedsWriteBarrier()) {
Ben Murdoch086aeea2011-05-13 15:57:08 +01003410 // Update the write barrier for the object for in-object properties.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003411 __ RecordWriteField(object,
3412 offset,
3413 value,
3414 scratch,
3415 kLRHasBeenSaved,
3416 kSaveFPRegs,
3417 EMIT_REMEMBERED_SET,
3418 check_needed);
Ben Murdoch086aeea2011-05-13 15:57:08 +01003419 }
3420 } else {
3421 __ ldr(scratch, FieldMemOperand(object, JSObject::kPropertiesOffset));
3422 __ str(value, FieldMemOperand(scratch, offset));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003423 if (instr->hydrogen()->NeedsWriteBarrier()) {
Ben Murdoch086aeea2011-05-13 15:57:08 +01003424 // Update the write barrier for the properties array.
3425 // object is used as a scratch register.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003426 __ RecordWriteField(scratch,
3427 offset,
3428 value,
3429 object,
3430 kLRHasBeenSaved,
3431 kSaveFPRegs,
3432 EMIT_REMEMBERED_SET,
3433 check_needed);
Ben Murdoch086aeea2011-05-13 15:57:08 +01003434 }
3435 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01003436}
3437
3438
3439void LCodeGen::DoStoreNamedGeneric(LStoreNamedGeneric* instr) {
3440 ASSERT(ToRegister(instr->object()).is(r1));
3441 ASSERT(ToRegister(instr->value()).is(r0));
3442
3443 // Name is always in r2.
3444 __ mov(r2, Operand(instr->name()));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003445 Handle<Code> ic = (instr->strict_mode_flag() == kStrictMode)
Steve Block44f0eee2011-05-26 01:26:41 +01003446 ? isolate()->builtins()->StoreIC_Initialize_Strict()
3447 : isolate()->builtins()->StoreIC_Initialize();
Ben Murdochb0fe1622011-05-05 13:52:32 +01003448 CallCode(ic, RelocInfo::CODE_TARGET, instr);
3449}
3450
3451
3452void LCodeGen::DoBoundsCheck(LBoundsCheck* instr) {
Ben Murdoch086aeea2011-05-13 15:57:08 +01003453 __ cmp(ToRegister(instr->index()), ToRegister(instr->length()));
Steve Block9fac8402011-05-12 15:51:54 +01003454 DeoptimizeIf(hs, instr->environment());
Ben Murdochb0fe1622011-05-05 13:52:32 +01003455}
3456
3457
3458void LCodeGen::DoStoreKeyedFastElement(LStoreKeyedFastElement* instr) {
Ben Murdoch086aeea2011-05-13 15:57:08 +01003459 Register value = ToRegister(instr->value());
3460 Register elements = ToRegister(instr->object());
3461 Register key = instr->key()->IsRegister() ? ToRegister(instr->key()) : no_reg;
3462 Register scratch = scratch0();
3463
3464 // Do the store.
3465 if (instr->key()->IsConstantOperand()) {
3466 ASSERT(!instr->hydrogen()->NeedsWriteBarrier());
3467 LConstantOperand* const_operand = LConstantOperand::cast(instr->key());
3468 int offset =
3469 ToInteger32(const_operand) * kPointerSize + FixedArray::kHeaderSize;
3470 __ str(value, FieldMemOperand(elements, offset));
3471 } else {
3472 __ add(scratch, elements, Operand(key, LSL, kPointerSizeLog2));
3473 __ str(value, FieldMemOperand(scratch, FixedArray::kHeaderSize));
3474 }
3475
3476 if (instr->hydrogen()->NeedsWriteBarrier()) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003477 HType type = instr->hydrogen()->value()->type();
3478 SmiCheck check_needed =
3479 type.IsHeapObject() ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
Ben Murdoch086aeea2011-05-13 15:57:08 +01003480 // Compute address of modified element and store it into key register.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003481 __ add(key, scratch, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3482 __ RecordWrite(elements,
3483 key,
3484 value,
3485 kLRHasBeenSaved,
3486 kSaveFPRegs,
3487 EMIT_REMEMBERED_SET,
3488 check_needed);
Ben Murdoch086aeea2011-05-13 15:57:08 +01003489 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01003490}
3491
3492
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003493void LCodeGen::DoStoreKeyedFastDoubleElement(
3494 LStoreKeyedFastDoubleElement* instr) {
3495 DwVfpRegister value = ToDoubleRegister(instr->value());
3496 Register elements = ToRegister(instr->elements());
3497 Register key = no_reg;
3498 Register scratch = scratch0();
3499 bool key_is_constant = instr->key()->IsConstantOperand();
3500 int constant_key = 0;
3501 Label not_nan;
3502
3503 // Calculate the effective address of the slot in the array to store the
3504 // double value.
3505 if (key_is_constant) {
3506 constant_key = ToInteger32(LConstantOperand::cast(instr->key()));
3507 if (constant_key & 0xF0000000) {
3508 Abort("array index constant value too big.");
3509 }
3510 } else {
3511 key = ToRegister(instr->key());
3512 }
Ben Murdoch589d6972011-11-30 16:04:58 +00003513 int shift_size = ElementsKindToShiftSize(FAST_DOUBLE_ELEMENTS);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003514 Operand operand = key_is_constant
3515 ? Operand(constant_key * (1 << shift_size) +
3516 FixedDoubleArray::kHeaderSize - kHeapObjectTag)
3517 : Operand(key, LSL, shift_size);
3518 __ add(scratch, elements, operand);
3519 if (!key_is_constant) {
3520 __ add(scratch, scratch,
3521 Operand(FixedDoubleArray::kHeaderSize - kHeapObjectTag));
3522 }
3523
3524 // Check for NaN. All NaNs must be canonicalized.
3525 __ VFPCompareAndSetFlags(value, value);
3526
3527 // Only load canonical NaN if the comparison above set the overflow.
3528 __ Vmov(value, FixedDoubleArray::canonical_not_the_hole_nan_as_double(), vs);
3529
3530 __ bind(&not_nan);
3531 __ vstr(value, scratch, 0);
3532}
3533
3534
Steve Block44f0eee2011-05-26 01:26:41 +01003535void LCodeGen::DoStoreKeyedSpecializedArrayElement(
3536 LStoreKeyedSpecializedArrayElement* instr) {
Steve Block44f0eee2011-05-26 01:26:41 +01003537
3538 Register external_pointer = ToRegister(instr->external_pointer());
Ben Murdoch257744e2011-11-30 15:57:28 +00003539 Register key = no_reg;
Ben Murdoch589d6972011-11-30 16:04:58 +00003540 ElementsKind elements_kind = instr->elements_kind();
Ben Murdoch257744e2011-11-30 15:57:28 +00003541 bool key_is_constant = instr->key()->IsConstantOperand();
3542 int constant_key = 0;
3543 if (key_is_constant) {
3544 constant_key = ToInteger32(LConstantOperand::cast(instr->key()));
3545 if (constant_key & 0xF0000000) {
3546 Abort("array index constant value too big.");
3547 }
3548 } else {
3549 key = ToRegister(instr->key());
3550 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003551 int shift_size = ElementsKindToShiftSize(elements_kind);
Ben Murdoch257744e2011-11-30 15:57:28 +00003552
Ben Murdoch589d6972011-11-30 16:04:58 +00003553 if (elements_kind == EXTERNAL_FLOAT_ELEMENTS ||
3554 elements_kind == EXTERNAL_DOUBLE_ELEMENTS) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01003555 CpuFeatures::Scope scope(VFP3);
3556 DwVfpRegister value(ToDoubleRegister(instr->value()));
Ben Murdoch257744e2011-11-30 15:57:28 +00003557 Operand operand(key_is_constant ? Operand(constant_key * (1 << shift_size))
3558 : Operand(key, LSL, shift_size));
3559 __ add(scratch0(), external_pointer, operand);
Ben Murdoch589d6972011-11-30 16:04:58 +00003560 if (elements_kind == EXTERNAL_FLOAT_ELEMENTS) {
Ben Murdoch257744e2011-11-30 15:57:28 +00003561 __ vcvt_f32_f64(double_scratch0().low(), value);
3562 __ vstr(double_scratch0().low(), scratch0(), 0);
Ben Murdoch589d6972011-11-30 16:04:58 +00003563 } else { // i.e. elements_kind == EXTERNAL_DOUBLE_ELEMENTS
Ben Murdoch257744e2011-11-30 15:57:28 +00003564 __ vstr(value, scratch0(), 0);
3565 }
Ben Murdoch8b112d22011-06-08 16:22:53 +01003566 } else {
3567 Register value(ToRegister(instr->value()));
Ben Murdoch257744e2011-11-30 15:57:28 +00003568 MemOperand mem_operand(key_is_constant
3569 ? MemOperand(external_pointer, constant_key * (1 << shift_size))
3570 : MemOperand(external_pointer, key, LSL, shift_size));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003571 switch (elements_kind) {
Ben Murdoch589d6972011-11-30 16:04:58 +00003572 case EXTERNAL_PIXEL_ELEMENTS:
3573 case EXTERNAL_BYTE_ELEMENTS:
3574 case EXTERNAL_UNSIGNED_BYTE_ELEMENTS:
Ben Murdoch257744e2011-11-30 15:57:28 +00003575 __ strb(value, mem_operand);
Ben Murdoch8b112d22011-06-08 16:22:53 +01003576 break;
Ben Murdoch589d6972011-11-30 16:04:58 +00003577 case EXTERNAL_SHORT_ELEMENTS:
3578 case EXTERNAL_UNSIGNED_SHORT_ELEMENTS:
Ben Murdoch257744e2011-11-30 15:57:28 +00003579 __ strh(value, mem_operand);
Ben Murdoch8b112d22011-06-08 16:22:53 +01003580 break;
Ben Murdoch589d6972011-11-30 16:04:58 +00003581 case EXTERNAL_INT_ELEMENTS:
3582 case EXTERNAL_UNSIGNED_INT_ELEMENTS:
Ben Murdoch257744e2011-11-30 15:57:28 +00003583 __ str(value, mem_operand);
Ben Murdoch8b112d22011-06-08 16:22:53 +01003584 break;
Ben Murdoch589d6972011-11-30 16:04:58 +00003585 case EXTERNAL_FLOAT_ELEMENTS:
3586 case EXTERNAL_DOUBLE_ELEMENTS:
3587 case FAST_DOUBLE_ELEMENTS:
3588 case FAST_ELEMENTS:
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003589 case FAST_SMI_ONLY_ELEMENTS:
Ben Murdoch589d6972011-11-30 16:04:58 +00003590 case DICTIONARY_ELEMENTS:
3591 case NON_STRICT_ARGUMENTS_ELEMENTS:
Ben Murdoch8b112d22011-06-08 16:22:53 +01003592 UNREACHABLE();
3593 break;
3594 }
3595 }
Steve Block44f0eee2011-05-26 01:26:41 +01003596}
3597
3598
Ben Murdochb0fe1622011-05-05 13:52:32 +01003599void LCodeGen::DoStoreKeyedGeneric(LStoreKeyedGeneric* instr) {
3600 ASSERT(ToRegister(instr->object()).is(r2));
3601 ASSERT(ToRegister(instr->key()).is(r1));
3602 ASSERT(ToRegister(instr->value()).is(r0));
3603
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003604 Handle<Code> ic = (instr->strict_mode_flag() == kStrictMode)
Steve Block44f0eee2011-05-26 01:26:41 +01003605 ? isolate()->builtins()->KeyedStoreIC_Initialize_Strict()
3606 : isolate()->builtins()->KeyedStoreIC_Initialize();
Ben Murdochb0fe1622011-05-05 13:52:32 +01003607 CallCode(ic, RelocInfo::CODE_TARGET, instr);
3608}
3609
3610
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003611void LCodeGen::DoTransitionElementsKind(LTransitionElementsKind* instr) {
3612 Register object_reg = ToRegister(instr->object());
3613 Register new_map_reg = ToRegister(instr->new_map_reg());
3614 Register scratch = scratch0();
3615
3616 Handle<Map> from_map = instr->original_map();
3617 Handle<Map> to_map = instr->transitioned_map();
3618 ElementsKind from_kind = from_map->elements_kind();
3619 ElementsKind to_kind = to_map->elements_kind();
3620
3621 Label not_applicable;
3622 __ ldr(scratch, FieldMemOperand(object_reg, HeapObject::kMapOffset));
3623 __ cmp(scratch, Operand(from_map));
3624 __ b(ne, &not_applicable);
3625 __ mov(new_map_reg, Operand(to_map));
3626 if (from_kind == FAST_SMI_ONLY_ELEMENTS && to_kind == FAST_ELEMENTS) {
3627 __ str(new_map_reg, FieldMemOperand(object_reg, HeapObject::kMapOffset));
3628 // Write barrier.
3629 __ RecordWriteField(object_reg, HeapObject::kMapOffset, new_map_reg,
3630 scratch, kLRHasBeenSaved, kDontSaveFPRegs);
3631 } else if (from_kind == FAST_SMI_ONLY_ELEMENTS &&
3632 to_kind == FAST_DOUBLE_ELEMENTS) {
3633 Register fixed_object_reg = ToRegister(instr->temp_reg());
3634 ASSERT(fixed_object_reg.is(r2));
3635 ASSERT(new_map_reg.is(r3));
3636 __ mov(fixed_object_reg, object_reg);
3637 CallCode(isolate()->builtins()->TransitionElementsSmiToDouble(),
3638 RelocInfo::CODE_TARGET, instr);
3639 } else if (from_kind == FAST_DOUBLE_ELEMENTS && to_kind == FAST_ELEMENTS) {
3640 Register fixed_object_reg = ToRegister(instr->temp_reg());
3641 ASSERT(fixed_object_reg.is(r2));
3642 ASSERT(new_map_reg.is(r3));
3643 __ mov(fixed_object_reg, object_reg);
3644 CallCode(isolate()->builtins()->TransitionElementsDoubleToObject(),
3645 RelocInfo::CODE_TARGET, instr);
3646 } else {
3647 UNREACHABLE();
3648 }
3649 __ bind(&not_applicable);
3650}
3651
3652
Ben Murdoch257744e2011-11-30 15:57:28 +00003653void LCodeGen::DoStringAdd(LStringAdd* instr) {
3654 __ push(ToRegister(instr->left()));
3655 __ push(ToRegister(instr->right()));
3656 StringAddStub stub(NO_STRING_CHECK_IN_STUB);
3657 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
3658}
3659
3660
Steve Block1e0659c2011-05-24 12:43:12 +01003661void LCodeGen::DoStringCharCodeAt(LStringCharCodeAt* instr) {
3662 class DeferredStringCharCodeAt: public LDeferredCode {
3663 public:
3664 DeferredStringCharCodeAt(LCodeGen* codegen, LStringCharCodeAt* instr)
3665 : LDeferredCode(codegen), instr_(instr) { }
3666 virtual void Generate() { codegen()->DoDeferredStringCharCodeAt(instr_); }
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003667 virtual LInstruction* instr() { return instr_; }
Steve Block1e0659c2011-05-24 12:43:12 +01003668 private:
3669 LStringCharCodeAt* instr_;
3670 };
3671
Steve Block1e0659c2011-05-24 12:43:12 +01003672 DeferredStringCharCodeAt* deferred =
3673 new DeferredStringCharCodeAt(this, instr);
3674
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003675 StringCharLoadGenerator::Generate(masm(),
3676 ToRegister(instr->string()),
3677 ToRegister(instr->index()),
3678 ToRegister(instr->result()),
3679 deferred->entry());
Steve Block1e0659c2011-05-24 12:43:12 +01003680 __ bind(deferred->exit());
3681}
3682
3683
3684void LCodeGen::DoDeferredStringCharCodeAt(LStringCharCodeAt* instr) {
3685 Register string = ToRegister(instr->string());
3686 Register result = ToRegister(instr->result());
3687 Register scratch = scratch0();
3688
3689 // TODO(3095996): Get rid of this. For now, we need to make the
3690 // result register contain a valid pointer because it is already
3691 // contained in the register pointer map.
3692 __ mov(result, Operand(0));
3693
Ben Murdoch8b112d22011-06-08 16:22:53 +01003694 PushSafepointRegistersScope scope(this, Safepoint::kWithRegisters);
Steve Block1e0659c2011-05-24 12:43:12 +01003695 __ push(string);
3696 // Push the index as a smi. This is safe because of the checks in
3697 // DoStringCharCodeAt above.
3698 if (instr->index()->IsConstantOperand()) {
3699 int const_index = ToInteger32(LConstantOperand::cast(instr->index()));
3700 __ mov(scratch, Operand(Smi::FromInt(const_index)));
3701 __ push(scratch);
3702 } else {
3703 Register index = ToRegister(instr->index());
3704 __ SmiTag(index);
3705 __ push(index);
3706 }
Ben Murdoch8b112d22011-06-08 16:22:53 +01003707 CallRuntimeFromDeferred(Runtime::kStringCharCodeAt, 2, instr);
Steve Block1e0659c2011-05-24 12:43:12 +01003708 if (FLAG_debug_code) {
3709 __ AbortIfNotSmi(r0);
3710 }
3711 __ SmiUntag(r0);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003712 __ StoreToSafepointRegisterSlot(r0, result);
Steve Block1e0659c2011-05-24 12:43:12 +01003713}
3714
3715
Steve Block44f0eee2011-05-26 01:26:41 +01003716void LCodeGen::DoStringCharFromCode(LStringCharFromCode* instr) {
3717 class DeferredStringCharFromCode: public LDeferredCode {
3718 public:
3719 DeferredStringCharFromCode(LCodeGen* codegen, LStringCharFromCode* instr)
3720 : LDeferredCode(codegen), instr_(instr) { }
3721 virtual void Generate() { codegen()->DoDeferredStringCharFromCode(instr_); }
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003722 virtual LInstruction* instr() { return instr_; }
Steve Block44f0eee2011-05-26 01:26:41 +01003723 private:
3724 LStringCharFromCode* instr_;
3725 };
3726
3727 DeferredStringCharFromCode* deferred =
3728 new DeferredStringCharFromCode(this, instr);
3729
3730 ASSERT(instr->hydrogen()->value()->representation().IsInteger32());
3731 Register char_code = ToRegister(instr->char_code());
3732 Register result = ToRegister(instr->result());
3733 ASSERT(!char_code.is(result));
3734
3735 __ cmp(char_code, Operand(String::kMaxAsciiCharCode));
3736 __ b(hi, deferred->entry());
3737 __ LoadRoot(result, Heap::kSingleCharacterStringCacheRootIndex);
3738 __ add(result, result, Operand(char_code, LSL, kPointerSizeLog2));
3739 __ ldr(result, FieldMemOperand(result, FixedArray::kHeaderSize));
3740 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
3741 __ cmp(result, ip);
3742 __ b(eq, deferred->entry());
3743 __ bind(deferred->exit());
3744}
3745
3746
3747void LCodeGen::DoDeferredStringCharFromCode(LStringCharFromCode* instr) {
3748 Register char_code = ToRegister(instr->char_code());
3749 Register result = ToRegister(instr->result());
3750
3751 // TODO(3095996): Get rid of this. For now, we need to make the
3752 // result register contain a valid pointer because it is already
3753 // contained in the register pointer map.
3754 __ mov(result, Operand(0));
3755
Ben Murdoch8b112d22011-06-08 16:22:53 +01003756 PushSafepointRegistersScope scope(this, Safepoint::kWithRegisters);
Steve Block44f0eee2011-05-26 01:26:41 +01003757 __ SmiTag(char_code);
3758 __ push(char_code);
Ben Murdoch8b112d22011-06-08 16:22:53 +01003759 CallRuntimeFromDeferred(Runtime::kCharFromCode, 1, instr);
Steve Block44f0eee2011-05-26 01:26:41 +01003760 __ StoreToSafepointRegisterSlot(r0, result);
Steve Block44f0eee2011-05-26 01:26:41 +01003761}
3762
3763
Steve Block1e0659c2011-05-24 12:43:12 +01003764void LCodeGen::DoStringLength(LStringLength* instr) {
3765 Register string = ToRegister(instr->InputAt(0));
3766 Register result = ToRegister(instr->result());
3767 __ ldr(result, FieldMemOperand(string, String::kLengthOffset));
3768}
3769
3770
Ben Murdochb0fe1622011-05-05 13:52:32 +01003771void LCodeGen::DoInteger32ToDouble(LInteger32ToDouble* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01003772 LOperand* input = instr->InputAt(0);
Ben Murdochb8e0da22011-05-16 14:20:40 +01003773 ASSERT(input->IsRegister() || input->IsStackSlot());
3774 LOperand* output = instr->result();
3775 ASSERT(output->IsDoubleRegister());
3776 SwVfpRegister single_scratch = double_scratch0().low();
3777 if (input->IsStackSlot()) {
3778 Register scratch = scratch0();
3779 __ ldr(scratch, ToMemOperand(input));
3780 __ vmov(single_scratch, scratch);
3781 } else {
3782 __ vmov(single_scratch, ToRegister(input));
3783 }
3784 __ vcvt_f64_s32(ToDoubleRegister(output), single_scratch);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003785}
3786
3787
3788void LCodeGen::DoNumberTagI(LNumberTagI* instr) {
3789 class DeferredNumberTagI: public LDeferredCode {
3790 public:
3791 DeferredNumberTagI(LCodeGen* codegen, LNumberTagI* instr)
3792 : LDeferredCode(codegen), instr_(instr) { }
3793 virtual void Generate() { codegen()->DoDeferredNumberTagI(instr_); }
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003794 virtual LInstruction* instr() { return instr_; }
Ben Murdochb0fe1622011-05-05 13:52:32 +01003795 private:
3796 LNumberTagI* instr_;
3797 };
3798
Steve Block1e0659c2011-05-24 12:43:12 +01003799 LOperand* input = instr->InputAt(0);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003800 ASSERT(input->IsRegister() && input->Equals(instr->result()));
3801 Register reg = ToRegister(input);
3802
3803 DeferredNumberTagI* deferred = new DeferredNumberTagI(this, instr);
3804 __ SmiTag(reg, SetCC);
3805 __ b(vs, deferred->entry());
3806 __ bind(deferred->exit());
3807}
3808
3809
3810void LCodeGen::DoDeferredNumberTagI(LNumberTagI* instr) {
3811 Label slow;
Steve Block1e0659c2011-05-24 12:43:12 +01003812 Register reg = ToRegister(instr->InputAt(0));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003813 DoubleRegister dbl_scratch = double_scratch0();
3814 SwVfpRegister flt_scratch = dbl_scratch.low();
Ben Murdochb0fe1622011-05-05 13:52:32 +01003815
3816 // Preserve the value of all registers.
Ben Murdoch8b112d22011-06-08 16:22:53 +01003817 PushSafepointRegistersScope scope(this, Safepoint::kWithRegisters);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003818
3819 // There was overflow, so bits 30 and 31 of the original integer
3820 // disagree. Try to allocate a heap number in new space and store
3821 // the value in there. If that fails, call the runtime system.
3822 Label done;
3823 __ SmiUntag(reg);
3824 __ eor(reg, reg, Operand(0x80000000));
3825 __ vmov(flt_scratch, reg);
3826 __ vcvt_f64_s32(dbl_scratch, flt_scratch);
3827 if (FLAG_inline_new) {
3828 __ LoadRoot(r6, Heap::kHeapNumberMapRootIndex);
3829 __ AllocateHeapNumber(r5, r3, r4, r6, &slow);
3830 if (!reg.is(r5)) __ mov(reg, r5);
3831 __ b(&done);
3832 }
3833
3834 // Slow case: Call the runtime system to do the number allocation.
3835 __ bind(&slow);
3836
3837 // TODO(3095996): Put a valid pointer value in the stack slot where the result
3838 // register is stored, as this register is in the pointer map, but contains an
3839 // integer value.
3840 __ mov(ip, Operand(0));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003841 __ StoreToSafepointRegisterSlot(ip, reg);
Ben Murdoch8b112d22011-06-08 16:22:53 +01003842 CallRuntimeFromDeferred(Runtime::kAllocateHeapNumber, 0, instr);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003843 if (!reg.is(r0)) __ mov(reg, r0);
3844
3845 // Done. Put the value in dbl_scratch into the value of the allocated heap
3846 // number.
3847 __ bind(&done);
3848 __ sub(ip, reg, Operand(kHeapObjectTag));
3849 __ vstr(dbl_scratch, ip, HeapNumber::kValueOffset);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003850 __ StoreToSafepointRegisterSlot(reg, reg);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003851}
3852
3853
3854void LCodeGen::DoNumberTagD(LNumberTagD* instr) {
3855 class DeferredNumberTagD: public LDeferredCode {
3856 public:
3857 DeferredNumberTagD(LCodeGen* codegen, LNumberTagD* instr)
3858 : LDeferredCode(codegen), instr_(instr) { }
3859 virtual void Generate() { codegen()->DoDeferredNumberTagD(instr_); }
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003860 virtual LInstruction* instr() { return instr_; }
Ben Murdochb0fe1622011-05-05 13:52:32 +01003861 private:
3862 LNumberTagD* instr_;
3863 };
3864
Steve Block1e0659c2011-05-24 12:43:12 +01003865 DoubleRegister input_reg = ToDoubleRegister(instr->InputAt(0));
Steve Block9fac8402011-05-12 15:51:54 +01003866 Register scratch = scratch0();
Ben Murdochb0fe1622011-05-05 13:52:32 +01003867 Register reg = ToRegister(instr->result());
Steve Block1e0659c2011-05-24 12:43:12 +01003868 Register temp1 = ToRegister(instr->TempAt(0));
3869 Register temp2 = ToRegister(instr->TempAt(1));
Ben Murdochb0fe1622011-05-05 13:52:32 +01003870
3871 DeferredNumberTagD* deferred = new DeferredNumberTagD(this, instr);
3872 if (FLAG_inline_new) {
3873 __ LoadRoot(scratch, Heap::kHeapNumberMapRootIndex);
3874 __ AllocateHeapNumber(reg, temp1, temp2, scratch, deferred->entry());
3875 } else {
3876 __ jmp(deferred->entry());
3877 }
3878 __ bind(deferred->exit());
3879 __ sub(ip, reg, Operand(kHeapObjectTag));
3880 __ vstr(input_reg, ip, HeapNumber::kValueOffset);
3881}
3882
3883
3884void LCodeGen::DoDeferredNumberTagD(LNumberTagD* instr) {
3885 // TODO(3095996): Get rid of this. For now, we need to make the
3886 // result register contain a valid pointer because it is already
3887 // contained in the register pointer map.
3888 Register reg = ToRegister(instr->result());
3889 __ mov(reg, Operand(0));
3890
Ben Murdoch8b112d22011-06-08 16:22:53 +01003891 PushSafepointRegistersScope scope(this, Safepoint::kWithRegisters);
3892 CallRuntimeFromDeferred(Runtime::kAllocateHeapNumber, 0, instr);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003893 __ StoreToSafepointRegisterSlot(r0, reg);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003894}
3895
3896
3897void LCodeGen::DoSmiTag(LSmiTag* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01003898 LOperand* input = instr->InputAt(0);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003899 ASSERT(input->IsRegister() && input->Equals(instr->result()));
3900 ASSERT(!instr->hydrogen_value()->CheckFlag(HValue::kCanOverflow));
3901 __ SmiTag(ToRegister(input));
3902}
3903
3904
3905void LCodeGen::DoSmiUntag(LSmiUntag* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01003906 LOperand* input = instr->InputAt(0);
Ben Murdoch086aeea2011-05-13 15:57:08 +01003907 ASSERT(input->IsRegister() && input->Equals(instr->result()));
3908 if (instr->needs_check()) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003909 STATIC_ASSERT(kHeapObjectTag == 1);
Ben Murdoch257744e2011-11-30 15:57:28 +00003910 // If the input is a HeapObject, SmiUntag will set the carry flag.
3911 __ SmiUntag(ToRegister(input), SetCC);
3912 DeoptimizeIf(cs, instr->environment());
3913 } else {
3914 __ SmiUntag(ToRegister(input));
Ben Murdoch086aeea2011-05-13 15:57:08 +01003915 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01003916}
3917
3918
3919void LCodeGen::EmitNumberUntagD(Register input_reg,
3920 DoubleRegister result_reg,
Ben Murdoch7d3e7fc2011-07-12 16:37:06 +01003921 bool deoptimize_on_undefined,
Ben Murdochc7cc0282012-03-05 14:35:55 +00003922 bool deoptimize_on_minus_zero,
Ben Murdochb0fe1622011-05-05 13:52:32 +01003923 LEnvironment* env) {
Steve Block9fac8402011-05-12 15:51:54 +01003924 Register scratch = scratch0();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003925 SwVfpRegister flt_scratch = double_scratch0().low();
3926 ASSERT(!result_reg.is(double_scratch0()));
Ben Murdochb0fe1622011-05-05 13:52:32 +01003927
3928 Label load_smi, heap_number, done;
3929
3930 // Smi check.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003931 __ JumpIfSmi(input_reg, &load_smi);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003932
3933 // Heap number map check.
Steve Block9fac8402011-05-12 15:51:54 +01003934 __ ldr(scratch, FieldMemOperand(input_reg, HeapObject::kMapOffset));
Ben Murdochb0fe1622011-05-05 13:52:32 +01003935 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
Steve Block9fac8402011-05-12 15:51:54 +01003936 __ cmp(scratch, Operand(ip));
Ben Murdoch7d3e7fc2011-07-12 16:37:06 +01003937 if (deoptimize_on_undefined) {
3938 DeoptimizeIf(ne, env);
3939 } else {
3940 Label heap_number;
3941 __ b(eq, &heap_number);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003942
Ben Murdoch7d3e7fc2011-07-12 16:37:06 +01003943 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
3944 __ cmp(input_reg, Operand(ip));
3945 DeoptimizeIf(ne, env);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003946
Ben Murdoch7d3e7fc2011-07-12 16:37:06 +01003947 // Convert undefined to NaN.
3948 __ LoadRoot(ip, Heap::kNanValueRootIndex);
3949 __ sub(ip, ip, Operand(kHeapObjectTag));
3950 __ vldr(result_reg, ip, HeapNumber::kValueOffset);
3951 __ jmp(&done);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003952
Ben Murdoch7d3e7fc2011-07-12 16:37:06 +01003953 __ bind(&heap_number);
3954 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01003955 // Heap number to double register conversion.
Ben Murdochb0fe1622011-05-05 13:52:32 +01003956 __ sub(ip, input_reg, Operand(kHeapObjectTag));
3957 __ vldr(result_reg, ip, HeapNumber::kValueOffset);
Ben Murdochc7cc0282012-03-05 14:35:55 +00003958 if (deoptimize_on_minus_zero) {
3959 __ vmov(ip, result_reg.low());
3960 __ cmp(ip, Operand(0));
3961 __ b(ne, &done);
3962 __ vmov(ip, result_reg.high());
3963 __ cmp(ip, Operand(HeapNumber::kSignMask));
3964 DeoptimizeIf(eq, env);
3965 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01003966 __ jmp(&done);
3967
3968 // Smi to double register conversion
3969 __ bind(&load_smi);
3970 __ SmiUntag(input_reg); // Untag smi before converting to float.
3971 __ vmov(flt_scratch, input_reg);
3972 __ vcvt_f64_s32(result_reg, flt_scratch);
3973 __ SmiTag(input_reg); // Retag smi.
3974 __ bind(&done);
3975}
3976
3977
Ben Murdochb0fe1622011-05-05 13:52:32 +01003978void LCodeGen::DoDeferredTaggedToI(LTaggedToI* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01003979 Register input_reg = ToRegister(instr->InputAt(0));
Steve Block44f0eee2011-05-26 01:26:41 +01003980 Register scratch1 = scratch0();
3981 Register scratch2 = ToRegister(instr->TempAt(0));
3982 DwVfpRegister double_scratch = double_scratch0();
3983 SwVfpRegister single_scratch = double_scratch.low();
3984
3985 ASSERT(!scratch1.is(input_reg) && !scratch1.is(scratch2));
3986 ASSERT(!scratch2.is(input_reg) && !scratch2.is(scratch1));
3987
3988 Label done;
Ben Murdochb0fe1622011-05-05 13:52:32 +01003989
Ben Murdoch257744e2011-11-30 15:57:28 +00003990 // The input was optimistically untagged; revert it.
3991 // The carry flag is set when we reach this deferred code as we just executed
3992 // SmiUntag(heap_object, SetCC)
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003993 STATIC_ASSERT(kHeapObjectTag == 1);
Ben Murdoch257744e2011-11-30 15:57:28 +00003994 __ adc(input_reg, input_reg, Operand(input_reg));
3995
Ben Murdochb0fe1622011-05-05 13:52:32 +01003996 // Heap number map check.
Steve Block44f0eee2011-05-26 01:26:41 +01003997 __ ldr(scratch1, FieldMemOperand(input_reg, HeapObject::kMapOffset));
Ben Murdochb0fe1622011-05-05 13:52:32 +01003998 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
Steve Block44f0eee2011-05-26 01:26:41 +01003999 __ cmp(scratch1, Operand(ip));
Ben Murdochb0fe1622011-05-05 13:52:32 +01004000
4001 if (instr->truncating()) {
Steve Block44f0eee2011-05-26 01:26:41 +01004002 Register scratch3 = ToRegister(instr->TempAt(1));
4003 DwVfpRegister double_scratch2 = ToDoubleRegister(instr->TempAt(2));
4004 ASSERT(!scratch3.is(input_reg) &&
4005 !scratch3.is(scratch1) &&
4006 !scratch3.is(scratch2));
4007 // Performs a truncating conversion of a floating point number as used by
4008 // the JS bitwise operations.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004009 Label heap_number;
4010 __ b(eq, &heap_number);
4011 // Check for undefined. Undefined is converted to zero for truncating
4012 // conversions.
4013 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
4014 __ cmp(input_reg, Operand(ip));
4015 DeoptimizeIf(ne, instr->environment());
4016 __ mov(input_reg, Operand(0));
4017 __ b(&done);
4018
4019 __ bind(&heap_number);
Steve Block44f0eee2011-05-26 01:26:41 +01004020 __ sub(scratch1, input_reg, Operand(kHeapObjectTag));
4021 __ vldr(double_scratch2, scratch1, HeapNumber::kValueOffset);
4022
4023 __ EmitECMATruncate(input_reg,
4024 double_scratch2,
4025 single_scratch,
4026 scratch1,
4027 scratch2,
4028 scratch3);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004029
4030 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01004031 CpuFeatures::Scope scope(VFP3);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004032 // Deoptimize if we don't have a heap number.
4033 DeoptimizeIf(ne, instr->environment());
4034
4035 __ sub(ip, input_reg, Operand(kHeapObjectTag));
Steve Block44f0eee2011-05-26 01:26:41 +01004036 __ vldr(double_scratch, ip, HeapNumber::kValueOffset);
4037 __ EmitVFPTruncate(kRoundToZero,
4038 single_scratch,
4039 double_scratch,
4040 scratch1,
4041 scratch2,
4042 kCheckForInexactConversion);
4043 DeoptimizeIf(ne, instr->environment());
4044 // Load the result.
4045 __ vmov(input_reg, single_scratch);
4046
Ben Murdochb0fe1622011-05-05 13:52:32 +01004047 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
Steve Block44f0eee2011-05-26 01:26:41 +01004048 __ cmp(input_reg, Operand(0));
Ben Murdochb0fe1622011-05-05 13:52:32 +01004049 __ b(ne, &done);
Steve Block44f0eee2011-05-26 01:26:41 +01004050 __ vmov(scratch1, double_scratch.high());
4051 __ tst(scratch1, Operand(HeapNumber::kSignMask));
Ben Murdochb0fe1622011-05-05 13:52:32 +01004052 DeoptimizeIf(ne, instr->environment());
4053 }
4054 }
4055 __ bind(&done);
4056}
4057
4058
4059void LCodeGen::DoTaggedToI(LTaggedToI* instr) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004060 class DeferredTaggedToI: public LDeferredCode {
4061 public:
4062 DeferredTaggedToI(LCodeGen* codegen, LTaggedToI* instr)
4063 : LDeferredCode(codegen), instr_(instr) { }
4064 virtual void Generate() { codegen()->DoDeferredTaggedToI(instr_); }
4065 virtual LInstruction* instr() { return instr_; }
4066 private:
4067 LTaggedToI* instr_;
4068 };
4069
Steve Block1e0659c2011-05-24 12:43:12 +01004070 LOperand* input = instr->InputAt(0);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004071 ASSERT(input->IsRegister());
4072 ASSERT(input->Equals(instr->result()));
4073
4074 Register input_reg = ToRegister(input);
4075
4076 DeferredTaggedToI* deferred = new DeferredTaggedToI(this, instr);
4077
Ben Murdoch257744e2011-11-30 15:57:28 +00004078 // Optimistically untag the input.
4079 // If the input is a HeapObject, SmiUntag will set the carry flag.
4080 __ SmiUntag(input_reg, SetCC);
4081 // Branch to deferred code if the input was tagged.
4082 // The deferred code will take care of restoring the tag.
4083 __ b(cs, deferred->entry());
Ben Murdochb0fe1622011-05-05 13:52:32 +01004084 __ bind(deferred->exit());
4085}
4086
4087
4088void LCodeGen::DoNumberUntagD(LNumberUntagD* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01004089 LOperand* input = instr->InputAt(0);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004090 ASSERT(input->IsRegister());
4091 LOperand* result = instr->result();
4092 ASSERT(result->IsDoubleRegister());
4093
4094 Register input_reg = ToRegister(input);
4095 DoubleRegister result_reg = ToDoubleRegister(result);
4096
Ben Murdoch7d3e7fc2011-07-12 16:37:06 +01004097 EmitNumberUntagD(input_reg, result_reg,
4098 instr->hydrogen()->deoptimize_on_undefined(),
Ben Murdochc7cc0282012-03-05 14:35:55 +00004099 instr->hydrogen()->deoptimize_on_minus_zero(),
Ben Murdoch7d3e7fc2011-07-12 16:37:06 +01004100 instr->environment());
Ben Murdochb0fe1622011-05-05 13:52:32 +01004101}
4102
4103
4104void LCodeGen::DoDoubleToI(LDoubleToI* instr) {
Steve Block44f0eee2011-05-26 01:26:41 +01004105 Register result_reg = ToRegister(instr->result());
Steve Block1e0659c2011-05-24 12:43:12 +01004106 Register scratch1 = scratch0();
4107 Register scratch2 = ToRegister(instr->TempAt(0));
Steve Block44f0eee2011-05-26 01:26:41 +01004108 DwVfpRegister double_input = ToDoubleRegister(instr->InputAt(0));
Steve Block44f0eee2011-05-26 01:26:41 +01004109 SwVfpRegister single_scratch = double_scratch0().low();
Steve Block1e0659c2011-05-24 12:43:12 +01004110
Steve Block44f0eee2011-05-26 01:26:41 +01004111 Label done;
Steve Block1e0659c2011-05-24 12:43:12 +01004112
Steve Block44f0eee2011-05-26 01:26:41 +01004113 if (instr->truncating()) {
4114 Register scratch3 = ToRegister(instr->TempAt(1));
4115 __ EmitECMATruncate(result_reg,
4116 double_input,
4117 single_scratch,
4118 scratch1,
4119 scratch2,
4120 scratch3);
4121 } else {
4122 VFPRoundingMode rounding_mode = kRoundToMinusInf;
4123 __ EmitVFPTruncate(rounding_mode,
4124 single_scratch,
4125 double_input,
4126 scratch1,
4127 scratch2,
4128 kCheckForInexactConversion);
4129 // Deoptimize if we had a vfp invalid exception,
4130 // including inexact operation.
Steve Block1e0659c2011-05-24 12:43:12 +01004131 DeoptimizeIf(ne, instr->environment());
Steve Block44f0eee2011-05-26 01:26:41 +01004132 // Retrieve the result.
4133 __ vmov(result_reg, single_scratch);
Steve Block1e0659c2011-05-24 12:43:12 +01004134 }
Steve Block44f0eee2011-05-26 01:26:41 +01004135 __ bind(&done);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004136}
4137
4138
4139void LCodeGen::DoCheckSmi(LCheckSmi* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01004140 LOperand* input = instr->InputAt(0);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004141 __ tst(ToRegister(input), Operand(kSmiTagMask));
Steve Block44f0eee2011-05-26 01:26:41 +01004142 DeoptimizeIf(ne, instr->environment());
4143}
4144
4145
4146void LCodeGen::DoCheckNonSmi(LCheckNonSmi* instr) {
4147 LOperand* input = instr->InputAt(0);
4148 __ tst(ToRegister(input), Operand(kSmiTagMask));
4149 DeoptimizeIf(eq, instr->environment());
Ben Murdochb0fe1622011-05-05 13:52:32 +01004150}
4151
4152
4153void LCodeGen::DoCheckInstanceType(LCheckInstanceType* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01004154 Register input = ToRegister(instr->InputAt(0));
Ben Murdoch086aeea2011-05-13 15:57:08 +01004155 Register scratch = scratch0();
Ben Murdoch086aeea2011-05-13 15:57:08 +01004156
4157 __ ldr(scratch, FieldMemOperand(input, HeapObject::kMapOffset));
4158 __ ldrb(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
Ben Murdoch086aeea2011-05-13 15:57:08 +01004159
Ben Murdoch257744e2011-11-30 15:57:28 +00004160 if (instr->hydrogen()->is_interval_check()) {
4161 InstanceType first;
4162 InstanceType last;
4163 instr->hydrogen()->GetCheckInterval(&first, &last);
4164
4165 __ cmp(scratch, Operand(first));
4166
4167 // If there is only one type in the interval check for equality.
4168 if (first == last) {
4169 DeoptimizeIf(ne, instr->environment());
4170 } else {
4171 DeoptimizeIf(lo, instr->environment());
4172 // Omit check for the last type.
4173 if (last != LAST_TYPE) {
4174 __ cmp(scratch, Operand(last));
4175 DeoptimizeIf(hi, instr->environment());
4176 }
4177 }
Ben Murdoch086aeea2011-05-13 15:57:08 +01004178 } else {
Ben Murdoch257744e2011-11-30 15:57:28 +00004179 uint8_t mask;
4180 uint8_t tag;
4181 instr->hydrogen()->GetCheckMaskAndTag(&mask, &tag);
4182
4183 if (IsPowerOf2(mask)) {
4184 ASSERT(tag == 0 || IsPowerOf2(tag));
4185 __ tst(scratch, Operand(mask));
4186 DeoptimizeIf(tag == 0 ? ne : eq, instr->environment());
4187 } else {
4188 __ and_(scratch, scratch, Operand(mask));
4189 __ cmp(scratch, Operand(tag));
4190 DeoptimizeIf(ne, instr->environment());
Ben Murdoch086aeea2011-05-13 15:57:08 +01004191 }
4192 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01004193}
4194
4195
4196void LCodeGen::DoCheckFunction(LCheckFunction* instr) {
Ben Murdochc7cc0282012-03-05 14:35:55 +00004197 Register reg = ToRegister(instr->value());
4198 Handle<JSFunction> target = instr->hydrogen()->target();
4199 if (isolate()->heap()->InNewSpace(*target)) {
4200 Register reg = ToRegister(instr->value());
4201 Handle<JSGlobalPropertyCell> cell =
4202 isolate()->factory()->NewJSGlobalPropertyCell(target);
4203 __ mov(ip, Operand(Handle<Object>(cell)));
4204 __ ldr(ip, FieldMemOperand(ip, JSGlobalPropertyCell::kValueOffset));
4205 __ cmp(reg, ip);
4206 } else {
4207 __ cmp(reg, Operand(target));
4208 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01004209 DeoptimizeIf(ne, instr->environment());
4210}
4211
4212
Ben Murdochc7cc0282012-03-05 14:35:55 +00004213void LCodeGen::DoCheckMapCommon(Register reg,
4214 Register scratch,
4215 Handle<Map> map,
4216 CompareMapMode mode,
4217 LEnvironment* env) {
4218 Label success;
4219 __ CompareMap(reg, scratch, map, &success, mode);
4220 DeoptimizeIf(ne, env);
4221 __ bind(&success);
4222}
4223
4224
Ben Murdochb0fe1622011-05-05 13:52:32 +01004225void LCodeGen::DoCheckMap(LCheckMap* instr) {
Steve Block9fac8402011-05-12 15:51:54 +01004226 Register scratch = scratch0();
Steve Block1e0659c2011-05-24 12:43:12 +01004227 LOperand* input = instr->InputAt(0);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004228 ASSERT(input->IsRegister());
4229 Register reg = ToRegister(input);
Ben Murdochc7cc0282012-03-05 14:35:55 +00004230 Handle<Map> map = instr->hydrogen()->map();
4231 DoCheckMapCommon(reg, scratch, map, instr->hydrogen()->mode(),
4232 instr->environment());
Ben Murdochb0fe1622011-05-05 13:52:32 +01004233}
4234
4235
Ben Murdoch257744e2011-11-30 15:57:28 +00004236void LCodeGen::DoClampDToUint8(LClampDToUint8* instr) {
4237 DoubleRegister value_reg = ToDoubleRegister(instr->unclamped());
4238 Register result_reg = ToRegister(instr->result());
4239 DoubleRegister temp_reg = ToDoubleRegister(instr->TempAt(0));
4240 __ ClampDoubleToUint8(result_reg, value_reg, temp_reg);
4241}
4242
4243
4244void LCodeGen::DoClampIToUint8(LClampIToUint8* instr) {
4245 Register unclamped_reg = ToRegister(instr->unclamped());
4246 Register result_reg = ToRegister(instr->result());
4247 __ ClampUint8(result_reg, unclamped_reg);
4248}
4249
4250
4251void LCodeGen::DoClampTToUint8(LClampTToUint8* instr) {
4252 Register scratch = scratch0();
4253 Register input_reg = ToRegister(instr->unclamped());
4254 Register result_reg = ToRegister(instr->result());
4255 DoubleRegister temp_reg = ToDoubleRegister(instr->TempAt(0));
4256 Label is_smi, done, heap_number;
4257
4258 // Both smi and heap number cases are handled.
4259 __ JumpIfSmi(input_reg, &is_smi);
4260
4261 // Check for heap number
4262 __ ldr(scratch, FieldMemOperand(input_reg, HeapObject::kMapOffset));
4263 __ cmp(scratch, Operand(factory()->heap_number_map()));
4264 __ b(eq, &heap_number);
4265
4266 // Check for undefined. Undefined is converted to zero for clamping
4267 // conversions.
4268 __ cmp(input_reg, Operand(factory()->undefined_value()));
4269 DeoptimizeIf(ne, instr->environment());
Ben Murdoch69a99ed2011-11-30 16:03:39 +00004270 __ mov(result_reg, Operand(0));
Ben Murdoch257744e2011-11-30 15:57:28 +00004271 __ jmp(&done);
4272
4273 // Heap number
4274 __ bind(&heap_number);
4275 __ vldr(double_scratch0(), FieldMemOperand(input_reg,
4276 HeapNumber::kValueOffset));
4277 __ ClampDoubleToUint8(result_reg, double_scratch0(), temp_reg);
4278 __ jmp(&done);
4279
4280 // smi
4281 __ bind(&is_smi);
4282 __ SmiUntag(result_reg, input_reg);
4283 __ ClampUint8(result_reg, result_reg);
4284
4285 __ bind(&done);
4286}
4287
4288
Ben Murdochb0fe1622011-05-05 13:52:32 +01004289void LCodeGen::DoCheckPrototypeMaps(LCheckPrototypeMaps* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01004290 Register temp1 = ToRegister(instr->TempAt(0));
4291 Register temp2 = ToRegister(instr->TempAt(1));
Steve Block9fac8402011-05-12 15:51:54 +01004292
4293 Handle<JSObject> holder = instr->holder();
Ben Murdochb8e0da22011-05-16 14:20:40 +01004294 Handle<JSObject> current_prototype = instr->prototype();
Steve Block9fac8402011-05-12 15:51:54 +01004295
4296 // Load prototype object.
Ben Murdochc7cc0282012-03-05 14:35:55 +00004297 __ LoadHeapObject(temp1, current_prototype);
Steve Block9fac8402011-05-12 15:51:54 +01004298
4299 // Check prototype maps up to the holder.
4300 while (!current_prototype.is_identical_to(holder)) {
Ben Murdochc7cc0282012-03-05 14:35:55 +00004301 DoCheckMapCommon(temp1, temp2,
4302 Handle<Map>(current_prototype->map()),
4303 ALLOW_ELEMENT_TRANSITION_MAPS, instr->environment());
Steve Block9fac8402011-05-12 15:51:54 +01004304 current_prototype =
4305 Handle<JSObject>(JSObject::cast(current_prototype->GetPrototype()));
4306 // Load next prototype object.
Ben Murdochc7cc0282012-03-05 14:35:55 +00004307 __ LoadHeapObject(temp1, current_prototype);
Steve Block9fac8402011-05-12 15:51:54 +01004308 }
4309
4310 // Check the holder map.
Ben Murdochc7cc0282012-03-05 14:35:55 +00004311 DoCheckMapCommon(temp1, temp2,
4312 Handle<Map>(current_prototype->map()),
4313 ALLOW_ELEMENT_TRANSITION_MAPS, instr->environment());
Steve Block9fac8402011-05-12 15:51:54 +01004314 DeoptimizeIf(ne, instr->environment());
Ben Murdochb0fe1622011-05-05 13:52:32 +01004315}
4316
4317
4318void LCodeGen::DoArrayLiteral(LArrayLiteral* instr) {
Ben Murdochc7cc0282012-03-05 14:35:55 +00004319 Heap* heap = isolate()->heap();
4320 ElementsKind boilerplate_elements_kind =
4321 instr->hydrogen()->boilerplate_elements_kind();
4322
4323 // Deopt if the array literal boilerplate ElementsKind is of a type different
4324 // than the expected one. The check isn't necessary if the boilerplate has
4325 // already been converted to FAST_ELEMENTS.
4326 if (boilerplate_elements_kind != FAST_ELEMENTS) {
4327 __ LoadHeapObject(r1, instr->hydrogen()->boilerplate_object());
4328 // Load map into r2.
4329 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
4330 // Load the map's "bit field 2".
4331 __ ldrb(r2, FieldMemOperand(r2, Map::kBitField2Offset));
4332 // Retrieve elements_kind from bit field 2.
4333 __ ubfx(r2, r2, Map::kElementsKindShift, Map::kElementsKindBitCount);
4334 __ cmp(r2, Operand(boilerplate_elements_kind));
4335 DeoptimizeIf(ne, instr->environment());
4336 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004337
Steve Block9fac8402011-05-12 15:51:54 +01004338 __ ldr(r3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
4339 __ ldr(r3, FieldMemOperand(r3, JSFunction::kLiteralsOffset));
4340 __ mov(r2, Operand(Smi::FromInt(instr->hydrogen()->literal_index())));
Ben Murdochc7cc0282012-03-05 14:35:55 +00004341 // Boilerplate already exists, constant elements are never accessed.
4342 // Pass an empty fixed array.
4343 __ mov(r1, Operand(Handle<FixedArray>(heap->empty_fixed_array())));
Steve Block9fac8402011-05-12 15:51:54 +01004344 __ Push(r3, r2, r1);
4345
4346 // Pick the right runtime function or stub to call.
4347 int length = instr->hydrogen()->length();
4348 if (instr->hydrogen()->IsCopyOnWrite()) {
4349 ASSERT(instr->hydrogen()->depth() == 1);
4350 FastCloneShallowArrayStub::Mode mode =
4351 FastCloneShallowArrayStub::COPY_ON_WRITE_ELEMENTS;
4352 FastCloneShallowArrayStub stub(mode, length);
4353 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
4354 } else if (instr->hydrogen()->depth() > 1) {
4355 CallRuntime(Runtime::kCreateArrayLiteral, 3, instr);
4356 } else if (length > FastCloneShallowArrayStub::kMaximumClonedLength) {
4357 CallRuntime(Runtime::kCreateArrayLiteralShallow, 3, instr);
4358 } else {
4359 FastCloneShallowArrayStub::Mode mode =
Ben Murdochc7cc0282012-03-05 14:35:55 +00004360 boilerplate_elements_kind == FAST_DOUBLE_ELEMENTS
4361 ? FastCloneShallowArrayStub::CLONE_DOUBLE_ELEMENTS
4362 : FastCloneShallowArrayStub::CLONE_ELEMENTS;
Steve Block9fac8402011-05-12 15:51:54 +01004363 FastCloneShallowArrayStub stub(mode, length);
4364 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
4365 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01004366}
4367
4368
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004369void LCodeGen::EmitDeepCopy(Handle<JSObject> object,
4370 Register result,
4371 Register source,
4372 int* offset) {
4373 ASSERT(!source.is(r2));
4374 ASSERT(!result.is(r2));
4375
4376 // Increase the offset so that subsequent objects end up right after
4377 // this one.
4378 int current_offset = *offset;
4379 int size = object->map()->instance_size();
4380 *offset += size;
4381
4382 // Copy object header.
4383 ASSERT(object->properties()->length() == 0);
4384 ASSERT(object->elements()->length() == 0 ||
4385 object->elements()->map() == isolate()->heap()->fixed_cow_array_map());
4386 int inobject_properties = object->map()->inobject_properties();
4387 int header_size = size - inobject_properties * kPointerSize;
4388 for (int i = 0; i < header_size; i += kPointerSize) {
4389 __ ldr(r2, FieldMemOperand(source, i));
4390 __ str(r2, FieldMemOperand(result, current_offset + i));
4391 }
4392
4393 // Copy in-object properties.
4394 for (int i = 0; i < inobject_properties; i++) {
4395 int total_offset = current_offset + object->GetInObjectPropertyOffset(i);
4396 Handle<Object> value = Handle<Object>(object->InObjectPropertyAt(i));
4397 if (value->IsJSObject()) {
4398 Handle<JSObject> value_object = Handle<JSObject>::cast(value);
4399 __ add(r2, result, Operand(*offset));
4400 __ str(r2, FieldMemOperand(result, total_offset));
Ben Murdochc7cc0282012-03-05 14:35:55 +00004401 __ LoadHeapObject(source, value_object);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004402 EmitDeepCopy(value_object, result, source, offset);
4403 } else if (value->IsHeapObject()) {
Ben Murdochc7cc0282012-03-05 14:35:55 +00004404 __ LoadHeapObject(r2, Handle<HeapObject>::cast(value));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004405 __ str(r2, FieldMemOperand(result, total_offset));
4406 } else {
4407 __ mov(r2, Operand(value));
4408 __ str(r2, FieldMemOperand(result, total_offset));
4409 }
4410 }
4411}
4412
4413
4414void LCodeGen::DoObjectLiteralFast(LObjectLiteralFast* instr) {
4415 int size = instr->hydrogen()->total_size();
4416
4417 // Allocate all objects that are part of the literal in one big
4418 // allocation. This avoids multiple limit checks.
4419 Label allocated, runtime_allocate;
4420 __ AllocateInNewSpace(size, r0, r2, r3, &runtime_allocate, TAG_OBJECT);
4421 __ jmp(&allocated);
4422
4423 __ bind(&runtime_allocate);
4424 __ mov(r0, Operand(Smi::FromInt(size)));
4425 __ push(r0);
4426 CallRuntime(Runtime::kAllocateInNewSpace, 1, instr);
4427
4428 __ bind(&allocated);
4429 int offset = 0;
Ben Murdochc7cc0282012-03-05 14:35:55 +00004430 __ LoadHeapObject(r1, instr->hydrogen()->boilerplate());
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004431 EmitDeepCopy(instr->hydrogen()->boilerplate(), r0, r1, &offset);
4432 ASSERT_EQ(size, offset);
4433}
4434
4435
4436void LCodeGen::DoObjectLiteralGeneric(LObjectLiteralGeneric* instr) {
4437 Handle<FixedArray> constant_properties =
4438 instr->hydrogen()->constant_properties();
4439
Steve Block9fac8402011-05-12 15:51:54 +01004440 __ ldr(r4, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
4441 __ ldr(r4, FieldMemOperand(r4, JSFunction::kLiteralsOffset));
4442 __ mov(r3, Operand(Smi::FromInt(instr->hydrogen()->literal_index())));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004443 __ mov(r2, Operand(constant_properties));
4444 int flags = instr->hydrogen()->fast_elements()
4445 ? ObjectLiteral::kFastElements
4446 : ObjectLiteral::kNoFlags;
4447 __ mov(r1, Operand(Smi::FromInt(flags)));
Steve Block9fac8402011-05-12 15:51:54 +01004448 __ Push(r4, r3, r2, r1);
4449
4450 // Pick the right runtime function to call.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004451 int properties_count = constant_properties->length() / 2;
Steve Block9fac8402011-05-12 15:51:54 +01004452 if (instr->hydrogen()->depth() > 1) {
4453 CallRuntime(Runtime::kCreateObjectLiteral, 4, instr);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004454 } else if (flags != ObjectLiteral::kFastElements ||
4455 properties_count > FastCloneShallowObjectStub::kMaximumClonedProperties) {
Steve Block9fac8402011-05-12 15:51:54 +01004456 CallRuntime(Runtime::kCreateObjectLiteralShallow, 4, instr);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004457 } else {
4458 FastCloneShallowObjectStub stub(properties_count);
4459 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
Steve Block9fac8402011-05-12 15:51:54 +01004460 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01004461}
4462
4463
Steve Block44f0eee2011-05-26 01:26:41 +01004464void LCodeGen::DoToFastProperties(LToFastProperties* instr) {
4465 ASSERT(ToRegister(instr->InputAt(0)).is(r0));
4466 __ push(r0);
4467 CallRuntime(Runtime::kToFastProperties, 1, instr);
4468}
4469
4470
Ben Murdochb0fe1622011-05-05 13:52:32 +01004471void LCodeGen::DoRegExpLiteral(LRegExpLiteral* instr) {
Ben Murdoch086aeea2011-05-13 15:57:08 +01004472 Label materialized;
4473 // Registers will be used as follows:
4474 // r3 = JS function.
4475 // r7 = literals array.
4476 // r1 = regexp literal.
4477 // r0 = regexp literal clone.
4478 // r2 and r4-r6 are used as temporaries.
4479 __ ldr(r3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
4480 __ ldr(r7, FieldMemOperand(r3, JSFunction::kLiteralsOffset));
4481 int literal_offset = FixedArray::kHeaderSize +
4482 instr->hydrogen()->literal_index() * kPointerSize;
4483 __ ldr(r1, FieldMemOperand(r7, literal_offset));
4484 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
4485 __ cmp(r1, ip);
4486 __ b(ne, &materialized);
4487
4488 // Create regexp literal using runtime function
4489 // Result will be in r0.
4490 __ mov(r6, Operand(Smi::FromInt(instr->hydrogen()->literal_index())));
4491 __ mov(r5, Operand(instr->hydrogen()->pattern()));
4492 __ mov(r4, Operand(instr->hydrogen()->flags()));
4493 __ Push(r7, r6, r5, r4);
4494 CallRuntime(Runtime::kMaterializeRegExpLiteral, 4, instr);
4495 __ mov(r1, r0);
4496
4497 __ bind(&materialized);
4498 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
4499 Label allocated, runtime_allocate;
4500
4501 __ AllocateInNewSpace(size, r0, r2, r3, &runtime_allocate, TAG_OBJECT);
4502 __ jmp(&allocated);
4503
4504 __ bind(&runtime_allocate);
4505 __ mov(r0, Operand(Smi::FromInt(size)));
4506 __ Push(r1, r0);
4507 CallRuntime(Runtime::kAllocateInNewSpace, 1, instr);
4508 __ pop(r1);
4509
4510 __ bind(&allocated);
4511 // Copy the content into the newly allocated memory.
4512 // (Unroll copy loop once for better throughput).
4513 for (int i = 0; i < size - kPointerSize; i += 2 * kPointerSize) {
4514 __ ldr(r3, FieldMemOperand(r1, i));
4515 __ ldr(r2, FieldMemOperand(r1, i + kPointerSize));
4516 __ str(r3, FieldMemOperand(r0, i));
4517 __ str(r2, FieldMemOperand(r0, i + kPointerSize));
4518 }
4519 if ((size % (2 * kPointerSize)) != 0) {
4520 __ ldr(r3, FieldMemOperand(r1, size - kPointerSize));
4521 __ str(r3, FieldMemOperand(r0, size - kPointerSize));
4522 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01004523}
4524
4525
4526void LCodeGen::DoFunctionLiteral(LFunctionLiteral* instr) {
Ben Murdoch086aeea2011-05-13 15:57:08 +01004527 // Use the fast case closure allocation code that allocates in new
4528 // space for nested functions that don't need literals cloning.
4529 Handle<SharedFunctionInfo> shared_info = instr->shared_info();
Steve Block1e0659c2011-05-24 12:43:12 +01004530 bool pretenure = instr->hydrogen()->pretenure();
Steve Block44f0eee2011-05-26 01:26:41 +01004531 if (!pretenure && shared_info->num_literals() == 0) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004532 FastNewClosureStub stub(shared_info->language_mode());
Ben Murdoch086aeea2011-05-13 15:57:08 +01004533 __ mov(r1, Operand(shared_info));
4534 __ push(r1);
4535 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
4536 } else {
4537 __ mov(r2, Operand(shared_info));
4538 __ mov(r1, Operand(pretenure
Steve Block44f0eee2011-05-26 01:26:41 +01004539 ? factory()->true_value()
4540 : factory()->false_value()));
Ben Murdoch086aeea2011-05-13 15:57:08 +01004541 __ Push(cp, r2, r1);
4542 CallRuntime(Runtime::kNewClosure, 3, instr);
4543 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01004544}
4545
4546
4547void LCodeGen::DoTypeof(LTypeof* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01004548 Register input = ToRegister(instr->InputAt(0));
Ben Murdoch086aeea2011-05-13 15:57:08 +01004549 __ push(input);
4550 CallRuntime(Runtime::kTypeof, 1, instr);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004551}
4552
4553
Ben Murdochb0fe1622011-05-05 13:52:32 +01004554void LCodeGen::DoTypeofIsAndBranch(LTypeofIsAndBranch* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01004555 Register input = ToRegister(instr->InputAt(0));
Ben Murdochb0fe1622011-05-05 13:52:32 +01004556 int true_block = chunk_->LookupDestination(instr->true_block_id());
4557 int false_block = chunk_->LookupDestination(instr->false_block_id());
4558 Label* true_label = chunk_->GetAssemblyLabel(true_block);
4559 Label* false_label = chunk_->GetAssemblyLabel(false_block);
4560
4561 Condition final_branch_condition = EmitTypeofIs(true_label,
4562 false_label,
4563 input,
4564 instr->type_literal());
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004565 if (final_branch_condition != kNoCondition) {
4566 EmitBranch(true_block, false_block, final_branch_condition);
4567 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01004568}
4569
4570
4571Condition LCodeGen::EmitTypeofIs(Label* true_label,
4572 Label* false_label,
4573 Register input,
4574 Handle<String> type_name) {
Steve Block1e0659c2011-05-24 12:43:12 +01004575 Condition final_branch_condition = kNoCondition;
Steve Block9fac8402011-05-12 15:51:54 +01004576 Register scratch = scratch0();
Steve Block44f0eee2011-05-26 01:26:41 +01004577 if (type_name->Equals(heap()->number_symbol())) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004578 __ JumpIfSmi(input, true_label);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004579 __ ldr(input, FieldMemOperand(input, HeapObject::kMapOffset));
4580 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
4581 __ cmp(input, Operand(ip));
4582 final_branch_condition = eq;
4583
Steve Block44f0eee2011-05-26 01:26:41 +01004584 } else if (type_name->Equals(heap()->string_symbol())) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004585 __ JumpIfSmi(input, false_label);
4586 __ CompareObjectType(input, input, scratch, FIRST_NONSTRING_TYPE);
4587 __ b(ge, false_label);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004588 __ ldrb(ip, FieldMemOperand(input, Map::kBitFieldOffset));
4589 __ tst(ip, Operand(1 << Map::kIsUndetectable));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004590 final_branch_condition = eq;
Ben Murdochb0fe1622011-05-05 13:52:32 +01004591
Steve Block44f0eee2011-05-26 01:26:41 +01004592 } else if (type_name->Equals(heap()->boolean_symbol())) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004593 __ CompareRoot(input, Heap::kTrueValueRootIndex);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004594 __ b(eq, true_label);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004595 __ CompareRoot(input, Heap::kFalseValueRootIndex);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004596 final_branch_condition = eq;
4597
Ben Murdoch69a99ed2011-11-30 16:03:39 +00004598 } else if (FLAG_harmony_typeof && type_name->Equals(heap()->null_symbol())) {
4599 __ CompareRoot(input, Heap::kNullValueRootIndex);
4600 final_branch_condition = eq;
4601
Steve Block44f0eee2011-05-26 01:26:41 +01004602 } else if (type_name->Equals(heap()->undefined_symbol())) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004603 __ CompareRoot(input, Heap::kUndefinedValueRootIndex);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004604 __ b(eq, true_label);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004605 __ JumpIfSmi(input, false_label);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004606 // Check for undetectable objects => true.
4607 __ ldr(input, FieldMemOperand(input, HeapObject::kMapOffset));
4608 __ ldrb(ip, FieldMemOperand(input, Map::kBitFieldOffset));
4609 __ tst(ip, Operand(1 << Map::kIsUndetectable));
4610 final_branch_condition = ne;
4611
Steve Block44f0eee2011-05-26 01:26:41 +01004612 } else if (type_name->Equals(heap()->function_symbol())) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004613 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004614 __ JumpIfSmi(input, false_label);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004615 __ CompareObjectType(input, scratch, input, JS_FUNCTION_TYPE);
4616 __ b(eq, true_label);
4617 __ cmp(input, Operand(JS_FUNCTION_PROXY_TYPE));
4618 final_branch_condition = eq;
Ben Murdochb0fe1622011-05-05 13:52:32 +01004619
Steve Block44f0eee2011-05-26 01:26:41 +01004620 } else if (type_name->Equals(heap()->object_symbol())) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004621 __ JumpIfSmi(input, false_label);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00004622 if (!FLAG_harmony_typeof) {
4623 __ CompareRoot(input, Heap::kNullValueRootIndex);
4624 __ b(eq, true_label);
4625 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004626 __ CompareObjectType(input, input, scratch,
4627 FIRST_NONCALLABLE_SPEC_OBJECT_TYPE);
4628 __ b(lt, false_label);
4629 __ CompareInstanceType(input, scratch, LAST_NONCALLABLE_SPEC_OBJECT_TYPE);
4630 __ b(gt, false_label);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004631 // Check for undetectable objects => false.
4632 __ ldrb(ip, FieldMemOperand(input, Map::kBitFieldOffset));
4633 __ tst(ip, Operand(1 << Map::kIsUndetectable));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004634 final_branch_condition = eq;
Ben Murdochb0fe1622011-05-05 13:52:32 +01004635
4636 } else {
Ben Murdochb0fe1622011-05-05 13:52:32 +01004637 __ b(false_label);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004638 }
4639
4640 return final_branch_condition;
4641}
4642
4643
Steve Block1e0659c2011-05-24 12:43:12 +01004644void LCodeGen::DoIsConstructCallAndBranch(LIsConstructCallAndBranch* instr) {
4645 Register temp1 = ToRegister(instr->TempAt(0));
4646 int true_block = chunk_->LookupDestination(instr->true_block_id());
4647 int false_block = chunk_->LookupDestination(instr->false_block_id());
4648
4649 EmitIsConstructCall(temp1, scratch0());
4650 EmitBranch(true_block, false_block, eq);
4651}
4652
4653
4654void LCodeGen::EmitIsConstructCall(Register temp1, Register temp2) {
4655 ASSERT(!temp1.is(temp2));
4656 // Get the frame pointer for the calling frame.
4657 __ ldr(temp1, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
4658
4659 // Skip the arguments adaptor frame if it exists.
4660 Label check_frame_marker;
4661 __ ldr(temp2, MemOperand(temp1, StandardFrameConstants::kContextOffset));
4662 __ cmp(temp2, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
4663 __ b(ne, &check_frame_marker);
4664 __ ldr(temp1, MemOperand(temp1, StandardFrameConstants::kCallerFPOffset));
4665
4666 // Check the marker in the calling frame.
4667 __ bind(&check_frame_marker);
4668 __ ldr(temp1, MemOperand(temp1, StandardFrameConstants::kMarkerOffset));
4669 __ cmp(temp1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
4670}
4671
4672
Ben Murdoch2b4ba112012-01-20 14:57:15 +00004673void LCodeGen::EnsureSpaceForLazyDeopt() {
4674 // Ensure that we have enough space after the previous lazy-bailout
4675 // instruction for patching the code here.
4676 int current_pc = masm()->pc_offset();
4677 int patch_size = Deoptimizer::patch_size();
4678 if (current_pc < last_lazy_deopt_pc_ + patch_size) {
4679 int padding_size = last_lazy_deopt_pc_ + patch_size - current_pc;
4680 ASSERT_EQ(0, padding_size % Assembler::kInstrSize);
4681 while (padding_size > 0) {
4682 __ nop();
4683 padding_size -= Assembler::kInstrSize;
4684 }
4685 }
4686 last_lazy_deopt_pc_ = masm()->pc_offset();
4687}
4688
4689
Ben Murdochb0fe1622011-05-05 13:52:32 +01004690void LCodeGen::DoLazyBailout(LLazyBailout* instr) {
Ben Murdoch2b4ba112012-01-20 14:57:15 +00004691 EnsureSpaceForLazyDeopt();
4692 ASSERT(instr->HasEnvironment());
4693 LEnvironment* env = instr->environment();
4694 RegisterEnvironmentForDeoptimization(env, Safepoint::kLazyDeopt);
4695 safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
Ben Murdochb0fe1622011-05-05 13:52:32 +01004696}
4697
4698
4699void LCodeGen::DoDeoptimize(LDeoptimize* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01004700 DeoptimizeIf(al, instr->environment());
Ben Murdochb0fe1622011-05-05 13:52:32 +01004701}
4702
4703
4704void LCodeGen::DoDeleteProperty(LDeleteProperty* instr) {
Ben Murdochb8e0da22011-05-16 14:20:40 +01004705 Register object = ToRegister(instr->object());
4706 Register key = ToRegister(instr->key());
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004707 Register strict = scratch0();
4708 __ mov(strict, Operand(Smi::FromInt(strict_mode_flag())));
4709 __ Push(object, key, strict);
Steve Block1e0659c2011-05-24 12:43:12 +01004710 ASSERT(instr->HasPointerMap() && instr->HasDeoptimizationEnvironment());
4711 LPointerMap* pointers = instr->pointer_map();
Steve Block1e0659c2011-05-24 12:43:12 +01004712 RecordPosition(pointers->position());
Ben Murdoch2b4ba112012-01-20 14:57:15 +00004713 SafepointGenerator safepoint_generator(
4714 this, pointers, Safepoint::kLazyDeopt);
Ben Murdoch257744e2011-11-30 15:57:28 +00004715 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION, safepoint_generator);
4716}
4717
4718
4719void LCodeGen::DoIn(LIn* instr) {
4720 Register obj = ToRegister(instr->object());
4721 Register key = ToRegister(instr->key());
4722 __ Push(key, obj);
4723 ASSERT(instr->HasPointerMap() && instr->HasDeoptimizationEnvironment());
4724 LPointerMap* pointers = instr->pointer_map();
Ben Murdoch257744e2011-11-30 15:57:28 +00004725 RecordPosition(pointers->position());
Ben Murdoch2b4ba112012-01-20 14:57:15 +00004726 SafepointGenerator safepoint_generator(this, pointers, Safepoint::kLazyDeopt);
Ben Murdoch257744e2011-11-30 15:57:28 +00004727 __ InvokeBuiltin(Builtins::IN, CALL_FUNCTION, safepoint_generator);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004728}
4729
4730
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004731void LCodeGen::DoDeferredStackCheck(LStackCheck* instr) {
Ben Murdoch2b4ba112012-01-20 14:57:15 +00004732 PushSafepointRegistersScope scope(this, Safepoint::kWithRegisters);
4733 __ CallRuntimeSaveDoubles(Runtime::kStackGuard);
4734 RecordSafepointWithLazyDeopt(
4735 instr, RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS);
4736 ASSERT(instr->HasEnvironment());
4737 LEnvironment* env = instr->environment();
4738 safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004739}
4740
4741
Ben Murdochb0fe1622011-05-05 13:52:32 +01004742void LCodeGen::DoStackCheck(LStackCheck* instr) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004743 class DeferredStackCheck: public LDeferredCode {
4744 public:
4745 DeferredStackCheck(LCodeGen* codegen, LStackCheck* instr)
4746 : LDeferredCode(codegen), instr_(instr) { }
4747 virtual void Generate() { codegen()->DoDeferredStackCheck(instr_); }
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004748 virtual LInstruction* instr() { return instr_; }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004749 private:
4750 LStackCheck* instr_;
4751 };
4752
Ben Murdoch2b4ba112012-01-20 14:57:15 +00004753 ASSERT(instr->HasEnvironment());
4754 LEnvironment* env = instr->environment();
4755 // There is no LLazyBailout instruction for stack-checks. We have to
4756 // prepare for lazy deoptimization explicitly here.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004757 if (instr->hydrogen()->is_function_entry()) {
4758 // Perform stack overflow check.
4759 Label done;
4760 __ LoadRoot(ip, Heap::kStackLimitRootIndex);
4761 __ cmp(sp, Operand(ip));
4762 __ b(hs, &done);
4763 StackCheckStub stub;
4764 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
Ben Murdoch2b4ba112012-01-20 14:57:15 +00004765 EnsureSpaceForLazyDeopt();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004766 __ bind(&done);
Ben Murdoch2b4ba112012-01-20 14:57:15 +00004767 RegisterEnvironmentForDeoptimization(env, Safepoint::kLazyDeopt);
4768 safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004769 } else {
4770 ASSERT(instr->hydrogen()->is_backwards_branch());
4771 // Perform stack overflow check if this goto needs it before jumping.
4772 DeferredStackCheck* deferred_stack_check =
4773 new DeferredStackCheck(this, instr);
4774 __ LoadRoot(ip, Heap::kStackLimitRootIndex);
4775 __ cmp(sp, Operand(ip));
4776 __ b(lo, deferred_stack_check->entry());
Ben Murdoch2b4ba112012-01-20 14:57:15 +00004777 EnsureSpaceForLazyDeopt();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004778 __ bind(instr->done_label());
4779 deferred_stack_check->SetExit(instr->done_label());
Ben Murdoch2b4ba112012-01-20 14:57:15 +00004780 RegisterEnvironmentForDeoptimization(env, Safepoint::kLazyDeopt);
4781 // Don't record a deoptimization index for the safepoint here.
4782 // This will be done explicitly when emitting call and the safepoint in
4783 // the deferred code.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004784 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01004785}
4786
4787
4788void LCodeGen::DoOsrEntry(LOsrEntry* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01004789 // This is a pseudo-instruction that ensures that the environment here is
4790 // properly registered for deoptimization and records the assembler's PC
4791 // offset.
4792 LEnvironment* environment = instr->environment();
4793 environment->SetSpilledRegisters(instr->SpilledRegisterArray(),
4794 instr->SpilledDoubleRegisterArray());
4795
4796 // If the environment were already registered, we would have no way of
4797 // backpatching it with the spill slot operands.
4798 ASSERT(!environment->HasBeenRegistered());
Ben Murdoch2b4ba112012-01-20 14:57:15 +00004799 RegisterEnvironmentForDeoptimization(environment, Safepoint::kNoLazyDeopt);
Steve Block1e0659c2011-05-24 12:43:12 +01004800 ASSERT(osr_pc_offset_ == -1);
4801 osr_pc_offset_ = masm()->pc_offset();
Ben Murdochb0fe1622011-05-05 13:52:32 +01004802}
4803
4804
Ben Murdoch257744e2011-11-30 15:57:28 +00004805
4806
Ben Murdochb0fe1622011-05-05 13:52:32 +01004807#undef __
4808
4809} } // namespace v8::internal