blob: 49124497fba7dc5566a77950321e6858ff40f0df [file] [log] [blame]
Ben Murdochb8e0da22011-05-16 14:20:40 +01001// Copyright 2011 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,
43 int deoptimization_index)
44 : codegen_(codegen),
45 pointers_(pointers),
46 deoptimization_index_(deoptimization_index) { }
47 virtual ~SafepointGenerator() { }
48
Steve Block44f0eee2011-05-26 01:26:41 +010049 virtual void BeforeCall(int call_size) {
50 ASSERT(call_size >= 0);
51 // Ensure that we have enough space after the previous safepoint position
52 // for the generated code there.
53 int call_end = codegen_->masm()->pc_offset() + call_size;
54 int prev_jump_end =
55 codegen_->LastSafepointEnd() + Deoptimizer::patch_size();
56 if (call_end < prev_jump_end) {
57 int padding_size = prev_jump_end - call_end;
58 ASSERT_EQ(0, padding_size % Assembler::kInstrSize);
59 while (padding_size > 0) {
60 codegen_->masm()->nop();
61 padding_size -= Assembler::kInstrSize;
62 }
63 }
64 }
65
66 virtual void AfterCall() {
Ben Murdochb0fe1622011-05-05 13:52:32 +010067 codegen_->RecordSafepoint(pointers_, deoptimization_index_);
68 }
69
70 private:
71 LCodeGen* codegen_;
72 LPointerMap* pointers_;
73 int deoptimization_index_;
74};
75
76
77#define __ masm()->
78
79bool LCodeGen::GenerateCode() {
80 HPhase phase("Code generation", chunk());
81 ASSERT(is_unused());
82 status_ = GENERATING;
83 CpuFeatures::Scope scope1(VFP3);
84 CpuFeatures::Scope scope2(ARMv7);
85 return GeneratePrologue() &&
86 GenerateBody() &&
87 GenerateDeferredCode() &&
88 GenerateSafepointTable();
89}
90
91
92void LCodeGen::FinishCode(Handle<Code> code) {
93 ASSERT(is_done());
Steve Block053d10c2011-06-13 19:13:29 +010094 code->set_stack_slots(StackSlotCount());
Steve Block1e0659c2011-05-24 12:43:12 +010095 code->set_safepoint_table_offset(safepoints_.GetCodeOffset());
Ben Murdochb0fe1622011-05-05 13:52:32 +010096 PopulateDeoptimizationData(code);
Steve Block44f0eee2011-05-26 01:26:41 +010097 Deoptimizer::EnsureRelocSpaceForLazyDeoptimization(code);
Ben Murdochb0fe1622011-05-05 13:52:32 +010098}
99
100
101void LCodeGen::Abort(const char* format, ...) {
102 if (FLAG_trace_bailout) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100103 SmartPointer<char> name(info()->shared_info()->DebugName()->ToCString());
104 PrintF("Aborting LCodeGen in @\"%s\": ", *name);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100105 va_list arguments;
106 va_start(arguments, format);
107 OS::VPrint(format, arguments);
108 va_end(arguments);
109 PrintF("\n");
110 }
111 status_ = ABORTED;
112}
113
114
115void LCodeGen::Comment(const char* format, ...) {
116 if (!FLAG_code_comments) return;
117 char buffer[4 * KB];
118 StringBuilder builder(buffer, ARRAY_SIZE(buffer));
119 va_list arguments;
120 va_start(arguments, format);
121 builder.AddFormattedList(format, arguments);
122 va_end(arguments);
123
124 // Copy the string before recording it in the assembler to avoid
125 // issues when the stack allocated buffer goes out of scope.
126 size_t length = builder.position();
127 Vector<char> copy = Vector<char>::New(length + 1);
128 memcpy(copy.start(), builder.Finalize(), copy.length());
129 masm()->RecordComment(copy.start());
130}
131
132
133bool LCodeGen::GeneratePrologue() {
134 ASSERT(is_generating());
135
136#ifdef DEBUG
137 if (strlen(FLAG_stop_at) > 0 &&
138 info_->function()->name()->IsEqualTo(CStrVector(FLAG_stop_at))) {
139 __ stop("stop_at");
140 }
141#endif
142
143 // r1: Callee's JS function.
144 // cp: Callee's context.
145 // fp: Caller's frame pointer.
146 // lr: Caller's pc.
147
148 __ stm(db_w, sp, r1.bit() | cp.bit() | fp.bit() | lr.bit());
149 __ add(fp, sp, Operand(2 * kPointerSize)); // Adjust FP to point to saved FP.
150
151 // Reserve space for the stack slots needed by the code.
Steve Block053d10c2011-06-13 19:13:29 +0100152 int slots = StackSlotCount();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100153 if (slots > 0) {
154 if (FLAG_debug_code) {
155 __ mov(r0, Operand(slots));
156 __ mov(r2, Operand(kSlotsZapValue));
157 Label loop;
158 __ bind(&loop);
159 __ push(r2);
160 __ sub(r0, r0, Operand(1), SetCC);
161 __ b(ne, &loop);
162 } else {
163 __ sub(sp, sp, Operand(slots * kPointerSize));
164 }
165 }
166
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100167 // Possibly allocate a local context.
168 int heap_slots = scope()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
169 if (heap_slots > 0) {
170 Comment(";;; Allocate local context");
171 // Argument to NewContext is the function, which is in r1.
172 __ push(r1);
173 if (heap_slots <= FastNewContextStub::kMaximumSlots) {
174 FastNewContextStub stub(heap_slots);
175 __ CallStub(&stub);
176 } else {
177 __ CallRuntime(Runtime::kNewContext, 1);
178 }
179 RecordSafepoint(Safepoint::kNoDeoptimizationIndex);
180 // Context is returned in both r0 and cp. It replaces the context
181 // passed to us. It's saved in the stack and kept live in cp.
182 __ str(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
183 // Copy any necessary parameters into the context.
184 int num_parameters = scope()->num_parameters();
185 for (int i = 0; i < num_parameters; i++) {
186 Slot* slot = scope()->parameter(i)->AsSlot();
187 if (slot != NULL && slot->type() == Slot::CONTEXT) {
188 int parameter_offset = StandardFrameConstants::kCallerSPOffset +
189 (num_parameters - 1 - i) * kPointerSize;
190 // Load parameter from stack.
191 __ ldr(r0, MemOperand(fp, parameter_offset));
192 // Store it in the context.
193 __ mov(r1, Operand(Context::SlotOffset(slot->index())));
194 __ str(r0, MemOperand(cp, r1));
195 // Update the write barrier. This clobbers all involved
196 // registers, so we have to use two more registers to avoid
197 // clobbering cp.
198 __ mov(r2, Operand(cp));
199 __ RecordWrite(r2, Operand(r1), r3, r0);
200 }
201 }
202 Comment(";;; End allocate local context");
203 }
204
Ben Murdochb0fe1622011-05-05 13:52:32 +0100205 // Trace the call.
206 if (FLAG_trace) {
207 __ CallRuntime(Runtime::kTraceEnter, 0);
208 }
209 return !is_aborted();
210}
211
212
213bool LCodeGen::GenerateBody() {
214 ASSERT(is_generating());
215 bool emit_instructions = true;
216 for (current_instruction_ = 0;
217 !is_aborted() && current_instruction_ < instructions_->length();
218 current_instruction_++) {
219 LInstruction* instr = instructions_->at(current_instruction_);
220 if (instr->IsLabel()) {
221 LLabel* label = LLabel::cast(instr);
222 emit_instructions = !label->HasReplacement();
223 }
224
225 if (emit_instructions) {
226 Comment(";;; @%d: %s.", current_instruction_, instr->Mnemonic());
227 instr->CompileToNative(this);
228 }
229 }
230 return !is_aborted();
231}
232
233
234LInstruction* LCodeGen::GetNextInstruction() {
235 if (current_instruction_ < instructions_->length() - 1) {
236 return instructions_->at(current_instruction_ + 1);
237 } else {
238 return NULL;
239 }
240}
241
242
243bool LCodeGen::GenerateDeferredCode() {
244 ASSERT(is_generating());
245 for (int i = 0; !is_aborted() && i < deferred_.length(); i++) {
246 LDeferredCode* code = deferred_[i];
247 __ bind(code->entry());
248 code->Generate();
249 __ jmp(code->exit());
250 }
251
Ben Murdochb8e0da22011-05-16 14:20:40 +0100252 // Force constant pool emission at the end of deferred code to make
253 // sure that no constant pools are emitted after the official end of
254 // the instruction sequence.
255 masm()->CheckConstPool(true, false);
256
Ben Murdochb0fe1622011-05-05 13:52:32 +0100257 // Deferred code is the last part of the instruction sequence. Mark
258 // the generated code as done unless we bailed out.
259 if (!is_aborted()) status_ = DONE;
260 return !is_aborted();
261}
262
263
264bool LCodeGen::GenerateSafepointTable() {
265 ASSERT(is_done());
Steve Block053d10c2011-06-13 19:13:29 +0100266 safepoints_.Emit(masm(), StackSlotCount());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100267 return !is_aborted();
268}
269
270
271Register LCodeGen::ToRegister(int index) const {
272 return Register::FromAllocationIndex(index);
273}
274
275
276DoubleRegister LCodeGen::ToDoubleRegister(int index) const {
277 return DoubleRegister::FromAllocationIndex(index);
278}
279
280
281Register LCodeGen::ToRegister(LOperand* op) const {
282 ASSERT(op->IsRegister());
283 return ToRegister(op->index());
284}
285
286
287Register LCodeGen::EmitLoadRegister(LOperand* op, Register scratch) {
288 if (op->IsRegister()) {
289 return ToRegister(op->index());
290 } else if (op->IsConstantOperand()) {
291 __ mov(scratch, ToOperand(op));
292 return scratch;
293 } else if (op->IsStackSlot() || op->IsArgument()) {
294 __ ldr(scratch, ToMemOperand(op));
295 return scratch;
296 }
297 UNREACHABLE();
298 return scratch;
299}
300
301
302DoubleRegister LCodeGen::ToDoubleRegister(LOperand* op) const {
303 ASSERT(op->IsDoubleRegister());
304 return ToDoubleRegister(op->index());
305}
306
307
308DoubleRegister LCodeGen::EmitLoadDoubleRegister(LOperand* op,
309 SwVfpRegister flt_scratch,
310 DoubleRegister dbl_scratch) {
311 if (op->IsDoubleRegister()) {
312 return ToDoubleRegister(op->index());
313 } else if (op->IsConstantOperand()) {
314 LConstantOperand* const_op = LConstantOperand::cast(op);
315 Handle<Object> literal = chunk_->LookupLiteral(const_op);
316 Representation r = chunk_->LookupLiteralRepresentation(const_op);
317 if (r.IsInteger32()) {
318 ASSERT(literal->IsNumber());
319 __ mov(ip, Operand(static_cast<int32_t>(literal->Number())));
320 __ vmov(flt_scratch, ip);
321 __ vcvt_f64_s32(dbl_scratch, flt_scratch);
322 return dbl_scratch;
323 } else if (r.IsDouble()) {
324 Abort("unsupported double immediate");
325 } else if (r.IsTagged()) {
326 Abort("unsupported tagged immediate");
327 }
328 } else if (op->IsStackSlot() || op->IsArgument()) {
329 // TODO(regis): Why is vldr not taking a MemOperand?
330 // __ vldr(dbl_scratch, ToMemOperand(op));
331 MemOperand mem_op = ToMemOperand(op);
332 __ vldr(dbl_scratch, mem_op.rn(), mem_op.offset());
333 return dbl_scratch;
334 }
335 UNREACHABLE();
336 return dbl_scratch;
337}
338
339
340int LCodeGen::ToInteger32(LConstantOperand* op) const {
341 Handle<Object> value = chunk_->LookupLiteral(op);
342 ASSERT(chunk_->LookupLiteralRepresentation(op).IsInteger32());
343 ASSERT(static_cast<double>(static_cast<int32_t>(value->Number())) ==
344 value->Number());
345 return static_cast<int32_t>(value->Number());
346}
347
348
349Operand LCodeGen::ToOperand(LOperand* op) {
350 if (op->IsConstantOperand()) {
351 LConstantOperand* const_op = LConstantOperand::cast(op);
352 Handle<Object> literal = chunk_->LookupLiteral(const_op);
353 Representation r = chunk_->LookupLiteralRepresentation(const_op);
354 if (r.IsInteger32()) {
355 ASSERT(literal->IsNumber());
356 return Operand(static_cast<int32_t>(literal->Number()));
357 } else if (r.IsDouble()) {
358 Abort("ToOperand Unsupported double immediate.");
359 }
360 ASSERT(r.IsTagged());
361 return Operand(literal);
362 } else if (op->IsRegister()) {
363 return Operand(ToRegister(op));
364 } else if (op->IsDoubleRegister()) {
365 Abort("ToOperand IsDoubleRegister unimplemented");
366 return Operand(0);
367 }
368 // Stack slots not implemented, use ToMemOperand instead.
369 UNREACHABLE();
370 return Operand(0);
371}
372
373
374MemOperand LCodeGen::ToMemOperand(LOperand* op) const {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100375 ASSERT(!op->IsRegister());
376 ASSERT(!op->IsDoubleRegister());
377 ASSERT(op->IsStackSlot() || op->IsDoubleStackSlot());
378 int index = op->index();
379 if (index >= 0) {
380 // Local or spill slot. Skip the frame pointer, function, and
381 // context in the fixed part of the frame.
382 return MemOperand(fp, -(index + 3) * kPointerSize);
383 } else {
384 // Incoming parameter. Skip the return address.
385 return MemOperand(fp, -(index - 1) * kPointerSize);
386 }
387}
388
389
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100390MemOperand LCodeGen::ToHighMemOperand(LOperand* op) const {
391 ASSERT(op->IsDoubleStackSlot());
392 int index = op->index();
393 if (index >= 0) {
394 // Local or spill slot. Skip the frame pointer, function, context,
395 // and the first word of the double in the fixed part of the frame.
396 return MemOperand(fp, -(index + 3) * kPointerSize + kPointerSize);
397 } else {
398 // Incoming parameter. Skip the return address and the first word of
399 // the double.
400 return MemOperand(fp, -(index - 1) * kPointerSize + kPointerSize);
401 }
402}
403
404
Ben Murdochb8e0da22011-05-16 14:20:40 +0100405void LCodeGen::WriteTranslation(LEnvironment* environment,
406 Translation* translation) {
407 if (environment == NULL) return;
408
409 // The translation includes one command per value in the environment.
410 int translation_size = environment->values()->length();
411 // The output frame height does not include the parameters.
412 int height = translation_size - environment->parameter_count();
413
414 WriteTranslation(environment->outer(), translation);
415 int closure_id = DefineDeoptimizationLiteral(environment->closure());
416 translation->BeginFrame(environment->ast_id(), closure_id, height);
417 for (int i = 0; i < translation_size; ++i) {
418 LOperand* value = environment->values()->at(i);
419 // spilled_registers_ and spilled_double_registers_ are either
420 // both NULL or both set.
421 if (environment->spilled_registers() != NULL && value != NULL) {
422 if (value->IsRegister() &&
423 environment->spilled_registers()[value->index()] != NULL) {
424 translation->MarkDuplicate();
425 AddToTranslation(translation,
426 environment->spilled_registers()[value->index()],
427 environment->HasTaggedValueAt(i));
428 } else if (
429 value->IsDoubleRegister() &&
430 environment->spilled_double_registers()[value->index()] != NULL) {
431 translation->MarkDuplicate();
432 AddToTranslation(
433 translation,
434 environment->spilled_double_registers()[value->index()],
435 false);
436 }
437 }
438
439 AddToTranslation(translation, value, environment->HasTaggedValueAt(i));
440 }
441}
442
443
Ben Murdochb0fe1622011-05-05 13:52:32 +0100444void LCodeGen::AddToTranslation(Translation* translation,
445 LOperand* op,
446 bool is_tagged) {
447 if (op == NULL) {
448 // TODO(twuerthinger): Introduce marker operands to indicate that this value
449 // is not present and must be reconstructed from the deoptimizer. Currently
450 // this is only used for the arguments object.
451 translation->StoreArgumentsObject();
452 } else if (op->IsStackSlot()) {
453 if (is_tagged) {
454 translation->StoreStackSlot(op->index());
455 } else {
456 translation->StoreInt32StackSlot(op->index());
457 }
458 } else if (op->IsDoubleStackSlot()) {
459 translation->StoreDoubleStackSlot(op->index());
460 } else if (op->IsArgument()) {
461 ASSERT(is_tagged);
Steve Block053d10c2011-06-13 19:13:29 +0100462 int src_index = StackSlotCount() + op->index();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100463 translation->StoreStackSlot(src_index);
464 } else if (op->IsRegister()) {
465 Register reg = ToRegister(op);
466 if (is_tagged) {
467 translation->StoreRegister(reg);
468 } else {
469 translation->StoreInt32Register(reg);
470 }
471 } else if (op->IsDoubleRegister()) {
472 DoubleRegister reg = ToDoubleRegister(op);
473 translation->StoreDoubleRegister(reg);
474 } else if (op->IsConstantOperand()) {
475 Handle<Object> literal = chunk()->LookupLiteral(LConstantOperand::cast(op));
476 int src_index = DefineDeoptimizationLiteral(literal);
477 translation->StoreLiteral(src_index);
478 } else {
479 UNREACHABLE();
480 }
481}
482
483
484void LCodeGen::CallCode(Handle<Code> code,
485 RelocInfo::Mode mode,
486 LInstruction* instr) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100487 CallCodeGeneric(code, mode, instr, RECORD_SIMPLE_SAFEPOINT);
488}
489
490
491void LCodeGen::CallCodeGeneric(Handle<Code> code,
492 RelocInfo::Mode mode,
493 LInstruction* instr,
494 SafepointMode safepoint_mode) {
Steve Block1e0659c2011-05-24 12:43:12 +0100495 ASSERT(instr != NULL);
496 LPointerMap* pointers = instr->pointer_map();
497 RecordPosition(pointers->position());
498 __ Call(code, mode);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100499 RegisterLazyDeoptimization(instr, safepoint_mode);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100500}
501
502
Steve Block44f0eee2011-05-26 01:26:41 +0100503void LCodeGen::CallRuntime(const Runtime::Function* function,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100504 int num_arguments,
505 LInstruction* instr) {
506 ASSERT(instr != NULL);
507 LPointerMap* pointers = instr->pointer_map();
508 ASSERT(pointers != NULL);
509 RecordPosition(pointers->position());
510
511 __ CallRuntime(function, num_arguments);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100512 RegisterLazyDeoptimization(instr, RECORD_SIMPLE_SAFEPOINT);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100513}
514
515
Ben Murdoch8b112d22011-06-08 16:22:53 +0100516void LCodeGen::CallRuntimeFromDeferred(Runtime::FunctionId id,
517 int argc,
518 LInstruction* instr) {
519 __ CallRuntimeSaveDoubles(id);
520 RecordSafepointWithRegisters(
521 instr->pointer_map(), argc, Safepoint::kNoDeoptimizationIndex);
522}
523
524
525void LCodeGen::RegisterLazyDeoptimization(LInstruction* instr,
526 SafepointMode safepoint_mode) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100527 // Create the environment to bailout to. If the call has side effects
528 // execution has to continue after the call otherwise execution can continue
529 // from a previous bailout point repeating the call.
530 LEnvironment* deoptimization_environment;
531 if (instr->HasDeoptimizationEnvironment()) {
532 deoptimization_environment = instr->deoptimization_environment();
533 } else {
534 deoptimization_environment = instr->environment();
535 }
536
537 RegisterEnvironmentForDeoptimization(deoptimization_environment);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100538 if (safepoint_mode == RECORD_SIMPLE_SAFEPOINT) {
539 RecordSafepoint(instr->pointer_map(),
540 deoptimization_environment->deoptimization_index());
541 } else {
542 ASSERT(safepoint_mode == RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS);
543 RecordSafepointWithRegisters(
544 instr->pointer_map(),
545 0,
546 deoptimization_environment->deoptimization_index());
547 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100548}
549
550
551void LCodeGen::RegisterEnvironmentForDeoptimization(LEnvironment* environment) {
552 if (!environment->HasBeenRegistered()) {
553 // Physical stack frame layout:
554 // -x ............. -4 0 ..................................... y
555 // [incoming arguments] [spill slots] [pushed outgoing arguments]
556
557 // Layout of the environment:
558 // 0 ..................................................... size-1
559 // [parameters] [locals] [expression stack including arguments]
560
561 // Layout of the translation:
562 // 0 ........................................................ size - 1 + 4
563 // [expression stack including arguments] [locals] [4 words] [parameters]
564 // |>------------ translation_size ------------<|
565
566 int frame_count = 0;
567 for (LEnvironment* e = environment; e != NULL; e = e->outer()) {
568 ++frame_count;
569 }
570 Translation translation(&translations_, frame_count);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100571 WriteTranslation(environment, &translation);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100572 int deoptimization_index = deoptimizations_.length();
573 environment->Register(deoptimization_index, translation.index());
574 deoptimizations_.Add(environment);
575 }
576}
577
578
579void LCodeGen::DeoptimizeIf(Condition cc, LEnvironment* environment) {
580 RegisterEnvironmentForDeoptimization(environment);
581 ASSERT(environment->HasBeenRegistered());
582 int id = environment->deoptimization_index();
583 Address entry = Deoptimizer::GetDeoptimizationEntry(id, Deoptimizer::EAGER);
584 ASSERT(entry != NULL);
585 if (entry == NULL) {
586 Abort("bailout was not prepared");
587 return;
588 }
589
590 ASSERT(FLAG_deopt_every_n_times < 2); // Other values not supported on ARM.
591
592 if (FLAG_deopt_every_n_times == 1 &&
593 info_->shared_info()->opt_count() == id) {
594 __ Jump(entry, RelocInfo::RUNTIME_ENTRY);
595 return;
596 }
597
Steve Block1e0659c2011-05-24 12:43:12 +0100598 if (cc == al) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100599 if (FLAG_trap_on_deopt) __ stop("trap_on_deopt");
600 __ Jump(entry, RelocInfo::RUNTIME_ENTRY);
601 } else {
602 if (FLAG_trap_on_deopt) {
603 Label done;
604 __ b(&done, NegateCondition(cc));
605 __ stop("trap_on_deopt");
606 __ Jump(entry, RelocInfo::RUNTIME_ENTRY);
607 __ bind(&done);
608 } else {
609 __ Jump(entry, RelocInfo::RUNTIME_ENTRY, cc);
610 }
611 }
612}
613
614
615void LCodeGen::PopulateDeoptimizationData(Handle<Code> code) {
616 int length = deoptimizations_.length();
617 if (length == 0) return;
618 ASSERT(FLAG_deopt);
619 Handle<DeoptimizationInputData> data =
Steve Block44f0eee2011-05-26 01:26:41 +0100620 factory()->NewDeoptimizationInputData(length, TENURED);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100621
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100622 Handle<ByteArray> translations = translations_.CreateByteArray();
623 data->SetTranslationByteArray(*translations);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100624 data->SetInlinedFunctionCount(Smi::FromInt(inlined_function_count_));
625
626 Handle<FixedArray> literals =
Steve Block44f0eee2011-05-26 01:26:41 +0100627 factory()->NewFixedArray(deoptimization_literals_.length(), TENURED);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100628 for (int i = 0; i < deoptimization_literals_.length(); i++) {
629 literals->set(i, *deoptimization_literals_[i]);
630 }
631 data->SetLiteralArray(*literals);
632
633 data->SetOsrAstId(Smi::FromInt(info_->osr_ast_id()));
634 data->SetOsrPcOffset(Smi::FromInt(osr_pc_offset_));
635
636 // Populate the deoptimization entries.
637 for (int i = 0; i < length; i++) {
638 LEnvironment* env = deoptimizations_[i];
639 data->SetAstId(i, Smi::FromInt(env->ast_id()));
640 data->SetTranslationIndex(i, Smi::FromInt(env->translation_index()));
641 data->SetArgumentsStackHeight(i,
642 Smi::FromInt(env->arguments_stack_height()));
643 }
644 code->set_deoptimization_data(*data);
645}
646
647
648int LCodeGen::DefineDeoptimizationLiteral(Handle<Object> literal) {
649 int result = deoptimization_literals_.length();
650 for (int i = 0; i < deoptimization_literals_.length(); ++i) {
651 if (deoptimization_literals_[i].is_identical_to(literal)) return i;
652 }
653 deoptimization_literals_.Add(literal);
654 return result;
655}
656
657
658void LCodeGen::PopulateDeoptimizationLiteralsWithInlinedFunctions() {
659 ASSERT(deoptimization_literals_.length() == 0);
660
661 const ZoneList<Handle<JSFunction> >* inlined_closures =
662 chunk()->inlined_closures();
663
664 for (int i = 0, length = inlined_closures->length();
665 i < length;
666 i++) {
667 DefineDeoptimizationLiteral(inlined_closures->at(i));
668 }
669
670 inlined_function_count_ = deoptimization_literals_.length();
671}
672
673
Steve Block1e0659c2011-05-24 12:43:12 +0100674void LCodeGen::RecordSafepoint(
675 LPointerMap* pointers,
676 Safepoint::Kind kind,
677 int arguments,
678 int deoptimization_index) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100679 ASSERT(expected_safepoint_kind_ == kind);
680
Ben Murdochb0fe1622011-05-05 13:52:32 +0100681 const ZoneList<LOperand*>* operands = pointers->operands();
682 Safepoint safepoint = safepoints_.DefineSafepoint(masm(),
Steve Block1e0659c2011-05-24 12:43:12 +0100683 kind, arguments, deoptimization_index);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100684 for (int i = 0; i < operands->length(); i++) {
685 LOperand* pointer = operands->at(i);
686 if (pointer->IsStackSlot()) {
687 safepoint.DefinePointerSlot(pointer->index());
Steve Block1e0659c2011-05-24 12:43:12 +0100688 } else if (pointer->IsRegister() && (kind & Safepoint::kWithRegisters)) {
689 safepoint.DefinePointerRegister(ToRegister(pointer));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100690 }
691 }
Steve Block1e0659c2011-05-24 12:43:12 +0100692 if (kind & Safepoint::kWithRegisters) {
693 // Register cp always contains a pointer to the context.
694 safepoint.DefinePointerRegister(cp);
695 }
696}
697
698
699void LCodeGen::RecordSafepoint(LPointerMap* pointers,
700 int deoptimization_index) {
701 RecordSafepoint(pointers, Safepoint::kSimple, 0, deoptimization_index);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100702}
703
704
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100705void LCodeGen::RecordSafepoint(int deoptimization_index) {
706 LPointerMap empty_pointers(RelocInfo::kNoPosition);
707 RecordSafepoint(&empty_pointers, deoptimization_index);
708}
709
710
Ben Murdochb0fe1622011-05-05 13:52:32 +0100711void LCodeGen::RecordSafepointWithRegisters(LPointerMap* pointers,
712 int arguments,
713 int deoptimization_index) {
Steve Block1e0659c2011-05-24 12:43:12 +0100714 RecordSafepoint(pointers, Safepoint::kWithRegisters, arguments,
715 deoptimization_index);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100716}
717
718
Ben Murdochb8e0da22011-05-16 14:20:40 +0100719void LCodeGen::RecordSafepointWithRegistersAndDoubles(
720 LPointerMap* pointers,
721 int arguments,
722 int deoptimization_index) {
Steve Block1e0659c2011-05-24 12:43:12 +0100723 RecordSafepoint(pointers, Safepoint::kWithRegistersAndDoubles, arguments,
724 deoptimization_index);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100725}
726
727
Ben Murdochb0fe1622011-05-05 13:52:32 +0100728void LCodeGen::RecordPosition(int position) {
729 if (!FLAG_debug_info || position == RelocInfo::kNoPosition) return;
730 masm()->positions_recorder()->RecordPosition(position);
731}
732
733
734void LCodeGen::DoLabel(LLabel* label) {
735 if (label->is_loop_header()) {
736 Comment(";;; B%d - LOOP entry", label->block_id());
737 } else {
738 Comment(";;; B%d", label->block_id());
739 }
740 __ bind(label->label());
741 current_block_ = label->block_id();
742 LCodeGen::DoGap(label);
743}
744
745
746void LCodeGen::DoParallelMove(LParallelMove* move) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100747 resolver_.Resolve(move);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100748}
749
750
751void LCodeGen::DoGap(LGap* gap) {
752 for (int i = LGap::FIRST_INNER_POSITION;
753 i <= LGap::LAST_INNER_POSITION;
754 i++) {
755 LGap::InnerPosition inner_pos = static_cast<LGap::InnerPosition>(i);
756 LParallelMove* move = gap->GetParallelMove(inner_pos);
757 if (move != NULL) DoParallelMove(move);
758 }
759
760 LInstruction* next = GetNextInstruction();
761 if (next != NULL && next->IsLazyBailout()) {
762 int pc = masm()->pc_offset();
763 safepoints_.SetPcAfterGap(pc);
764 }
765}
766
767
768void LCodeGen::DoParameter(LParameter* instr) {
769 // Nothing to do.
770}
771
772
773void LCodeGen::DoCallStub(LCallStub* instr) {
Steve Block9fac8402011-05-12 15:51:54 +0100774 ASSERT(ToRegister(instr->result()).is(r0));
775 switch (instr->hydrogen()->major_key()) {
776 case CodeStub::RegExpConstructResult: {
777 RegExpConstructResultStub stub;
778 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
779 break;
780 }
781 case CodeStub::RegExpExec: {
782 RegExpExecStub stub;
783 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
784 break;
785 }
786 case CodeStub::SubString: {
787 SubStringStub stub;
788 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
789 break;
790 }
Steve Block9fac8402011-05-12 15:51:54 +0100791 case CodeStub::NumberToString: {
792 NumberToStringStub stub;
793 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
794 break;
795 }
796 case CodeStub::StringAdd: {
797 StringAddStub stub(NO_STRING_ADD_FLAGS);
798 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
799 break;
800 }
801 case CodeStub::StringCompare: {
802 StringCompareStub stub;
803 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
804 break;
805 }
806 case CodeStub::TranscendentalCache: {
Ben Murdochb8e0da22011-05-16 14:20:40 +0100807 __ ldr(r0, MemOperand(sp, 0));
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100808 TranscendentalCacheStub stub(instr->transcendental_type(),
809 TranscendentalCacheStub::TAGGED);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100810 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
Steve Block9fac8402011-05-12 15:51:54 +0100811 break;
812 }
813 default:
814 UNREACHABLE();
815 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100816}
817
818
819void LCodeGen::DoUnknownOSRValue(LUnknownOSRValue* instr) {
820 // Nothing to do.
821}
822
823
824void LCodeGen::DoModI(LModI* instr) {
Steve Block44f0eee2011-05-26 01:26:41 +0100825 if (instr->hydrogen()->HasPowerOf2Divisor()) {
826 Register dividend = ToRegister(instr->InputAt(0));
827
828 int32_t divisor =
829 HConstant::cast(instr->hydrogen()->right())->Integer32Value();
830
831 if (divisor < 0) divisor = -divisor;
832
833 Label positive_dividend, done;
834 __ cmp(dividend, Operand(0));
835 __ b(pl, &positive_dividend);
836 __ rsb(dividend, dividend, Operand(0));
837 __ and_(dividend, dividend, Operand(divisor - 1));
838 __ rsb(dividend, dividend, Operand(0), SetCC);
839 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
840 __ b(ne, &done);
841 DeoptimizeIf(al, instr->environment());
Ben Murdochb8e0da22011-05-16 14:20:40 +0100842 }
Steve Block44f0eee2011-05-26 01:26:41 +0100843 __ bind(&positive_dividend);
844 __ and_(dividend, dividend, Operand(divisor - 1));
845 __ bind(&done);
846 return;
847 }
848
Ben Murdochb8e0da22011-05-16 14:20:40 +0100849 // These registers hold untagged 32 bit values.
Steve Block1e0659c2011-05-24 12:43:12 +0100850 Register left = ToRegister(instr->InputAt(0));
851 Register right = ToRegister(instr->InputAt(1));
Ben Murdochb8e0da22011-05-16 14:20:40 +0100852 Register result = ToRegister(instr->result());
Ben Murdochb8e0da22011-05-16 14:20:40 +0100853
Steve Block44f0eee2011-05-26 01:26:41 +0100854 Register scratch = scratch0();
855 Register scratch2 = ToRegister(instr->TempAt(0));
856 DwVfpRegister dividend = ToDoubleRegister(instr->TempAt(1));
857 DwVfpRegister divisor = ToDoubleRegister(instr->TempAt(2));
858 DwVfpRegister quotient = double_scratch0();
859
860 ASSERT(result.is(left));
861
862 ASSERT(!dividend.is(divisor));
863 ASSERT(!dividend.is(quotient));
864 ASSERT(!divisor.is(quotient));
865 ASSERT(!scratch.is(left));
866 ASSERT(!scratch.is(right));
867 ASSERT(!scratch.is(result));
868
869 Label done, vfp_modulo, both_positive, right_negative;
870
Ben Murdochb8e0da22011-05-16 14:20:40 +0100871 // Check for x % 0.
872 if (instr->hydrogen()->CheckFlag(HValue::kCanBeDivByZero)) {
Steve Block44f0eee2011-05-26 01:26:41 +0100873 __ cmp(right, Operand(0));
874 DeoptimizeIf(eq, instr->environment());
Ben Murdochb8e0da22011-05-16 14:20:40 +0100875 }
876
Steve Block44f0eee2011-05-26 01:26:41 +0100877 // (0 % x) must yield 0 (if x is finite, which is the case here).
Steve Block1e0659c2011-05-24 12:43:12 +0100878 __ cmp(left, Operand(0));
Steve Block44f0eee2011-05-26 01:26:41 +0100879 __ b(eq, &done);
880 // Preload right in a vfp register.
881 __ vmov(divisor.low(), right);
882 __ b(lt, &vfp_modulo);
883
884 __ cmp(left, Operand(right));
885 __ b(lt, &done);
886
887 // Check for (positive) power of two on the right hand side.
888 __ JumpIfNotPowerOfTwoOrZeroAndNeg(right,
889 scratch,
890 &right_negative,
891 &both_positive);
892 // Perform modulo operation (scratch contains right - 1).
893 __ and_(result, scratch, Operand(left));
894 __ b(&done);
895
896 __ bind(&right_negative);
897 // Negate right. The sign of the divisor does not matter.
898 __ rsb(right, right, Operand(0));
899
900 __ bind(&both_positive);
901 const int kUnfolds = 3;
Steve Block1e0659c2011-05-24 12:43:12 +0100902 // If the right hand side is smaller than the (nonnegative)
Steve Block44f0eee2011-05-26 01:26:41 +0100903 // left hand side, the left hand side is the result.
904 // Else try a few subtractions of the left hand side.
Steve Block1e0659c2011-05-24 12:43:12 +0100905 __ mov(scratch, left);
906 for (int i = 0; i < kUnfolds; i++) {
907 // Check if the left hand side is less or equal than the
908 // the right hand side.
Steve Block44f0eee2011-05-26 01:26:41 +0100909 __ cmp(scratch, Operand(right));
Steve Block1e0659c2011-05-24 12:43:12 +0100910 __ mov(result, scratch, LeaveCC, lt);
911 __ b(lt, &done);
912 // If not, reduce the left hand side by the right hand
913 // side and check again.
914 if (i < kUnfolds - 1) __ sub(scratch, scratch, right);
915 }
916
Steve Block44f0eee2011-05-26 01:26:41 +0100917 __ bind(&vfp_modulo);
918 // Load the arguments in VFP registers.
919 // The divisor value is preloaded before. Be careful that 'right' is only live
920 // on entry.
921 __ vmov(dividend.low(), left);
922 // From here on don't use right as it may have been reallocated (for example
923 // to scratch2).
924 right = no_reg;
Steve Block1e0659c2011-05-24 12:43:12 +0100925
Steve Block44f0eee2011-05-26 01:26:41 +0100926 __ vcvt_f64_s32(dividend, dividend.low());
927 __ vcvt_f64_s32(divisor, divisor.low());
Ben Murdochb8e0da22011-05-16 14:20:40 +0100928
Steve Block44f0eee2011-05-26 01:26:41 +0100929 // We do not care about the sign of the divisor.
930 __ vabs(divisor, divisor);
931 // Compute the quotient and round it to a 32bit integer.
932 __ vdiv(quotient, dividend, divisor);
933 __ vcvt_s32_f64(quotient.low(), quotient);
934 __ vcvt_f64_s32(quotient, quotient.low());
Ben Murdochb8e0da22011-05-16 14:20:40 +0100935
Steve Block44f0eee2011-05-26 01:26:41 +0100936 // Compute the remainder in result.
937 DwVfpRegister double_scratch = dividend;
938 __ vmul(double_scratch, divisor, quotient);
939 __ vcvt_s32_f64(double_scratch.low(), double_scratch);
940 __ vmov(scratch, double_scratch.low());
Ben Murdochb8e0da22011-05-16 14:20:40 +0100941
Steve Block44f0eee2011-05-26 01:26:41 +0100942 if (!instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
943 __ sub(result, left, scratch);
944 } else {
945 Label ok;
946 // Check for -0.
947 __ sub(scratch2, left, scratch, SetCC);
948 __ b(ne, &ok);
949 __ cmp(left, Operand(0));
950 DeoptimizeIf(mi, instr->environment());
951 __ bind(&ok);
952 // Load the result and we are done.
953 __ mov(result, scratch2);
954 }
955
Ben Murdochb8e0da22011-05-16 14:20:40 +0100956 __ bind(&done);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100957}
958
959
960void LCodeGen::DoDivI(LDivI* instr) {
Ben Murdochb8e0da22011-05-16 14:20:40 +0100961 class DeferredDivI: public LDeferredCode {
962 public:
963 DeferredDivI(LCodeGen* codegen, LDivI* instr)
964 : LDeferredCode(codegen), instr_(instr) { }
965 virtual void Generate() {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100966 codegen()->DoDeferredBinaryOpStub(instr_, Token::DIV);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100967 }
968 private:
969 LDivI* instr_;
970 };
971
Steve Block1e0659c2011-05-24 12:43:12 +0100972 const Register left = ToRegister(instr->InputAt(0));
973 const Register right = ToRegister(instr->InputAt(1));
Ben Murdochb8e0da22011-05-16 14:20:40 +0100974 const Register scratch = scratch0();
975 const Register result = ToRegister(instr->result());
976
977 // Check for x / 0.
978 if (instr->hydrogen()->CheckFlag(HValue::kCanBeDivByZero)) {
Steve Block44f0eee2011-05-26 01:26:41 +0100979 __ cmp(right, Operand(0));
Ben Murdochb8e0da22011-05-16 14:20:40 +0100980 DeoptimizeIf(eq, instr->environment());
981 }
982
983 // Check for (0 / -x) that will produce negative zero.
984 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
985 Label left_not_zero;
Steve Block44f0eee2011-05-26 01:26:41 +0100986 __ cmp(left, Operand(0));
Ben Murdochb8e0da22011-05-16 14:20:40 +0100987 __ b(ne, &left_not_zero);
Steve Block44f0eee2011-05-26 01:26:41 +0100988 __ cmp(right, Operand(0));
Ben Murdochb8e0da22011-05-16 14:20:40 +0100989 DeoptimizeIf(mi, instr->environment());
990 __ bind(&left_not_zero);
991 }
992
993 // Check for (-kMinInt / -1).
994 if (instr->hydrogen()->CheckFlag(HValue::kCanOverflow)) {
995 Label left_not_min_int;
996 __ cmp(left, Operand(kMinInt));
997 __ b(ne, &left_not_min_int);
998 __ cmp(right, Operand(-1));
999 DeoptimizeIf(eq, instr->environment());
1000 __ bind(&left_not_min_int);
1001 }
1002
1003 Label done, deoptimize;
1004 // Test for a few common cases first.
1005 __ cmp(right, Operand(1));
1006 __ mov(result, left, LeaveCC, eq);
1007 __ b(eq, &done);
1008
1009 __ cmp(right, Operand(2));
1010 __ tst(left, Operand(1), eq);
1011 __ mov(result, Operand(left, ASR, 1), LeaveCC, eq);
1012 __ b(eq, &done);
1013
1014 __ cmp(right, Operand(4));
1015 __ tst(left, Operand(3), eq);
1016 __ mov(result, Operand(left, ASR, 2), LeaveCC, eq);
1017 __ b(eq, &done);
1018
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001019 // Call the stub. The numbers in r0 and r1 have
Ben Murdochb8e0da22011-05-16 14:20:40 +01001020 // to be tagged to Smis. If that is not possible, deoptimize.
1021 DeferredDivI* deferred = new DeferredDivI(this, instr);
1022
1023 __ TrySmiTag(left, &deoptimize, scratch);
1024 __ TrySmiTag(right, &deoptimize, scratch);
1025
1026 __ b(al, deferred->entry());
1027 __ bind(deferred->exit());
1028
1029 // If the result in r0 is a Smi, untag it, else deoptimize.
Steve Block1e0659c2011-05-24 12:43:12 +01001030 __ JumpIfNotSmi(result, &deoptimize);
Ben Murdochb8e0da22011-05-16 14:20:40 +01001031 __ SmiUntag(result);
1032 __ b(&done);
1033
1034 __ bind(&deoptimize);
1035 DeoptimizeIf(al, instr->environment());
1036 __ bind(&done);
1037}
1038
1039
Steve Block1e0659c2011-05-24 12:43:12 +01001040template<int T>
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001041void LCodeGen::DoDeferredBinaryOpStub(LTemplateInstruction<1, 2, T>* instr,
1042 Token::Value op) {
Steve Block1e0659c2011-05-24 12:43:12 +01001043 Register left = ToRegister(instr->InputAt(0));
1044 Register right = ToRegister(instr->InputAt(1));
Ben Murdochb8e0da22011-05-16 14:20:40 +01001045
Ben Murdoch8b112d22011-06-08 16:22:53 +01001046 PushSafepointRegistersScope scope(this, Safepoint::kWithRegistersAndDoubles);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001047 // Move left to r1 and right to r0 for the stub call.
1048 if (left.is(r1)) {
1049 __ Move(r0, right);
1050 } else if (left.is(r0) && right.is(r1)) {
1051 __ Swap(r0, r1, r2);
1052 } else if (left.is(r0)) {
1053 ASSERT(!right.is(r1));
1054 __ mov(r1, r0);
1055 __ mov(r0, right);
1056 } else {
1057 ASSERT(!left.is(r0) && !right.is(r0));
1058 __ mov(r0, right);
1059 __ mov(r1, left);
1060 }
1061 TypeRecordingBinaryOpStub stub(op, OVERWRITE_LEFT);
Ben Murdochb8e0da22011-05-16 14:20:40 +01001062 __ CallStub(&stub);
1063 RecordSafepointWithRegistersAndDoubles(instr->pointer_map(),
1064 0,
1065 Safepoint::kNoDeoptimizationIndex);
1066 // Overwrite the stored value of r0 with the result of the stub.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001067 __ StoreToSafepointRegistersAndDoublesSlot(r0, r0);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001068}
1069
1070
1071void LCodeGen::DoMulI(LMulI* instr) {
Steve Block9fac8402011-05-12 15:51:54 +01001072 Register scratch = scratch0();
Steve Block1e0659c2011-05-24 12:43:12 +01001073 Register left = ToRegister(instr->InputAt(0));
1074 Register right = EmitLoadRegister(instr->InputAt(1), scratch);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001075
1076 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero) &&
Steve Block1e0659c2011-05-24 12:43:12 +01001077 !instr->InputAt(1)->IsConstantOperand()) {
1078 __ orr(ToRegister(instr->TempAt(0)), left, right);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001079 }
1080
1081 if (instr->hydrogen()->CheckFlag(HValue::kCanOverflow)) {
1082 // scratch:left = left * right.
Steve Block1e0659c2011-05-24 12:43:12 +01001083 __ smull(left, scratch, left, right);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001084 __ mov(ip, Operand(left, ASR, 31));
1085 __ cmp(ip, Operand(scratch));
1086 DeoptimizeIf(ne, instr->environment());
1087 } else {
1088 __ mul(left, left, right);
1089 }
1090
1091 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
1092 // Bail out if the result is supposed to be negative zero.
1093 Label done;
Steve Block44f0eee2011-05-26 01:26:41 +01001094 __ cmp(left, Operand(0));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001095 __ b(ne, &done);
Steve Block1e0659c2011-05-24 12:43:12 +01001096 if (instr->InputAt(1)->IsConstantOperand()) {
1097 if (ToInteger32(LConstantOperand::cast(instr->InputAt(1))) <= 0) {
1098 DeoptimizeIf(al, instr->environment());
Ben Murdochb0fe1622011-05-05 13:52:32 +01001099 }
1100 } else {
1101 // Test the non-zero operand for negative sign.
Steve Block1e0659c2011-05-24 12:43:12 +01001102 __ cmp(ToRegister(instr->TempAt(0)), Operand(0));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001103 DeoptimizeIf(mi, instr->environment());
1104 }
1105 __ bind(&done);
1106 }
1107}
1108
1109
1110void LCodeGen::DoBitI(LBitI* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01001111 LOperand* left = instr->InputAt(0);
1112 LOperand* right = instr->InputAt(1);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001113 ASSERT(left->Equals(instr->result()));
1114 ASSERT(left->IsRegister());
1115 Register result = ToRegister(left);
Steve Block44f0eee2011-05-26 01:26:41 +01001116 Operand right_operand(no_reg);
1117
1118 if (right->IsStackSlot() || right->IsArgument()) {
1119 Register right_reg = EmitLoadRegister(right, ip);
1120 right_operand = Operand(right_reg);
1121 } else {
1122 ASSERT(right->IsRegister() || right->IsConstantOperand());
1123 right_operand = ToOperand(right);
1124 }
1125
Ben Murdochb0fe1622011-05-05 13:52:32 +01001126 switch (instr->op()) {
1127 case Token::BIT_AND:
Steve Block44f0eee2011-05-26 01:26:41 +01001128 __ and_(result, ToRegister(left), right_operand);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001129 break;
1130 case Token::BIT_OR:
Steve Block44f0eee2011-05-26 01:26:41 +01001131 __ orr(result, ToRegister(left), right_operand);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001132 break;
1133 case Token::BIT_XOR:
Steve Block44f0eee2011-05-26 01:26:41 +01001134 __ eor(result, ToRegister(left), right_operand);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001135 break;
1136 default:
1137 UNREACHABLE();
1138 break;
1139 }
1140}
1141
1142
1143void LCodeGen::DoShiftI(LShiftI* instr) {
Steve Block9fac8402011-05-12 15:51:54 +01001144 Register scratch = scratch0();
Steve Block1e0659c2011-05-24 12:43:12 +01001145 LOperand* left = instr->InputAt(0);
1146 LOperand* right = instr->InputAt(1);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001147 ASSERT(left->Equals(instr->result()));
1148 ASSERT(left->IsRegister());
1149 Register result = ToRegister(left);
1150 if (right->IsRegister()) {
1151 // Mask the right operand.
Steve Block9fac8402011-05-12 15:51:54 +01001152 __ and_(scratch, ToRegister(right), Operand(0x1F));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001153 switch (instr->op()) {
1154 case Token::SAR:
Steve Block9fac8402011-05-12 15:51:54 +01001155 __ mov(result, Operand(result, ASR, scratch));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001156 break;
1157 case Token::SHR:
1158 if (instr->can_deopt()) {
Steve Block9fac8402011-05-12 15:51:54 +01001159 __ mov(result, Operand(result, LSR, scratch), SetCC);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001160 DeoptimizeIf(mi, instr->environment());
1161 } else {
Steve Block9fac8402011-05-12 15:51:54 +01001162 __ mov(result, Operand(result, LSR, scratch));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001163 }
1164 break;
1165 case Token::SHL:
Steve Block9fac8402011-05-12 15:51:54 +01001166 __ mov(result, Operand(result, LSL, scratch));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001167 break;
1168 default:
1169 UNREACHABLE();
1170 break;
1171 }
1172 } else {
1173 int value = ToInteger32(LConstantOperand::cast(right));
1174 uint8_t shift_count = static_cast<uint8_t>(value & 0x1F);
1175 switch (instr->op()) {
1176 case Token::SAR:
1177 if (shift_count != 0) {
1178 __ mov(result, Operand(result, ASR, shift_count));
1179 }
1180 break;
1181 case Token::SHR:
1182 if (shift_count == 0 && instr->can_deopt()) {
1183 __ tst(result, Operand(0x80000000));
1184 DeoptimizeIf(ne, instr->environment());
1185 } else {
1186 __ mov(result, Operand(result, LSR, shift_count));
1187 }
1188 break;
1189 case Token::SHL:
1190 if (shift_count != 0) {
1191 __ mov(result, Operand(result, LSL, shift_count));
1192 }
1193 break;
1194 default:
1195 UNREACHABLE();
1196 break;
1197 }
1198 }
1199}
1200
1201
1202void LCodeGen::DoSubI(LSubI* instr) {
Steve Block44f0eee2011-05-26 01:26:41 +01001203 LOperand* left = instr->InputAt(0);
1204 LOperand* right = instr->InputAt(1);
1205 ASSERT(left->Equals(instr->result()));
1206 bool can_overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
1207 SBit set_cond = can_overflow ? SetCC : LeaveCC;
1208
1209 if (right->IsStackSlot() || right->IsArgument()) {
1210 Register right_reg = EmitLoadRegister(right, ip);
1211 __ sub(ToRegister(left), ToRegister(left), Operand(right_reg), set_cond);
1212 } else {
1213 ASSERT(right->IsRegister() || right->IsConstantOperand());
1214 __ sub(ToRegister(left), ToRegister(left), ToOperand(right), set_cond);
1215 }
1216
1217 if (can_overflow) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001218 DeoptimizeIf(vs, instr->environment());
1219 }
1220}
1221
1222
1223void LCodeGen::DoConstantI(LConstantI* instr) {
1224 ASSERT(instr->result()->IsRegister());
1225 __ mov(ToRegister(instr->result()), Operand(instr->value()));
1226}
1227
1228
1229void LCodeGen::DoConstantD(LConstantD* instr) {
Ben Murdochb8e0da22011-05-16 14:20:40 +01001230 ASSERT(instr->result()->IsDoubleRegister());
1231 DwVfpRegister result = ToDoubleRegister(instr->result());
1232 double v = instr->value();
1233 __ vmov(result, v);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001234}
1235
1236
1237void LCodeGen::DoConstantT(LConstantT* instr) {
1238 ASSERT(instr->result()->IsRegister());
1239 __ mov(ToRegister(instr->result()), Operand(instr->value()));
1240}
1241
1242
Steve Block9fac8402011-05-12 15:51:54 +01001243void LCodeGen::DoJSArrayLength(LJSArrayLength* instr) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001244 Register result = ToRegister(instr->result());
Steve Block1e0659c2011-05-24 12:43:12 +01001245 Register array = ToRegister(instr->InputAt(0));
Steve Block9fac8402011-05-12 15:51:54 +01001246 __ ldr(result, FieldMemOperand(array, JSArray::kLengthOffset));
1247}
Ben Murdochb0fe1622011-05-05 13:52:32 +01001248
Ben Murdochb0fe1622011-05-05 13:52:32 +01001249
Steve Block44f0eee2011-05-26 01:26:41 +01001250void LCodeGen::DoExternalArrayLength(LExternalArrayLength* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01001251 Register result = ToRegister(instr->result());
1252 Register array = ToRegister(instr->InputAt(0));
Steve Block44f0eee2011-05-26 01:26:41 +01001253 __ ldr(result, FieldMemOperand(array, ExternalArray::kLengthOffset));
Steve Block1e0659c2011-05-24 12:43:12 +01001254}
1255
1256
Steve Block9fac8402011-05-12 15:51:54 +01001257void LCodeGen::DoFixedArrayLength(LFixedArrayLength* instr) {
1258 Register result = ToRegister(instr->result());
Steve Block1e0659c2011-05-24 12:43:12 +01001259 Register array = ToRegister(instr->InputAt(0));
Steve Block9fac8402011-05-12 15:51:54 +01001260 __ ldr(result, FieldMemOperand(array, FixedArray::kLengthOffset));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001261}
1262
1263
1264void LCodeGen::DoValueOf(LValueOf* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01001265 Register input = ToRegister(instr->InputAt(0));
Ben Murdochb8e0da22011-05-16 14:20:40 +01001266 Register result = ToRegister(instr->result());
Steve Block1e0659c2011-05-24 12:43:12 +01001267 Register map = ToRegister(instr->TempAt(0));
Ben Murdochb8e0da22011-05-16 14:20:40 +01001268 ASSERT(input.is(result));
1269 Label done;
1270
1271 // If the object is a smi return the object.
1272 __ tst(input, Operand(kSmiTagMask));
1273 __ b(eq, &done);
1274
1275 // If the object is not a value type, return the object.
1276 __ CompareObjectType(input, map, map, JS_VALUE_TYPE);
1277 __ b(ne, &done);
1278 __ ldr(result, FieldMemOperand(input, JSValue::kValueOffset));
1279
1280 __ bind(&done);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001281}
1282
1283
1284void LCodeGen::DoBitNotI(LBitNotI* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01001285 LOperand* input = instr->InputAt(0);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001286 ASSERT(input->Equals(instr->result()));
1287 __ mvn(ToRegister(input), Operand(ToRegister(input)));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001288}
1289
1290
1291void LCodeGen::DoThrow(LThrow* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01001292 Register input_reg = EmitLoadRegister(instr->InputAt(0), ip);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001293 __ push(input_reg);
1294 CallRuntime(Runtime::kThrow, 1, instr);
1295
1296 if (FLAG_debug_code) {
1297 __ stop("Unreachable code.");
1298 }
1299}
1300
1301
1302void LCodeGen::DoAddI(LAddI* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01001303 LOperand* left = instr->InputAt(0);
1304 LOperand* right = instr->InputAt(1);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001305 ASSERT(left->Equals(instr->result()));
Steve Block44f0eee2011-05-26 01:26:41 +01001306 bool can_overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
1307 SBit set_cond = can_overflow ? SetCC : LeaveCC;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001308
Steve Block44f0eee2011-05-26 01:26:41 +01001309 if (right->IsStackSlot() || right->IsArgument()) {
1310 Register right_reg = EmitLoadRegister(right, ip);
1311 __ add(ToRegister(left), ToRegister(left), Operand(right_reg), set_cond);
1312 } else {
1313 ASSERT(right->IsRegister() || right->IsConstantOperand());
1314 __ add(ToRegister(left), ToRegister(left), ToOperand(right), set_cond);
1315 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01001316
Steve Block44f0eee2011-05-26 01:26:41 +01001317 if (can_overflow) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001318 DeoptimizeIf(vs, instr->environment());
1319 }
1320}
1321
1322
1323void LCodeGen::DoArithmeticD(LArithmeticD* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01001324 DoubleRegister left = ToDoubleRegister(instr->InputAt(0));
1325 DoubleRegister right = ToDoubleRegister(instr->InputAt(1));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001326 switch (instr->op()) {
1327 case Token::ADD:
1328 __ vadd(left, left, right);
1329 break;
1330 case Token::SUB:
1331 __ vsub(left, left, right);
1332 break;
1333 case Token::MUL:
1334 __ vmul(left, left, right);
1335 break;
1336 case Token::DIV:
1337 __ vdiv(left, left, right);
1338 break;
1339 case Token::MOD: {
Steve Block1e0659c2011-05-24 12:43:12 +01001340 // Save r0-r3 on the stack.
1341 __ stm(db_w, sp, r0.bit() | r1.bit() | r2.bit() | r3.bit());
1342
1343 __ PrepareCallCFunction(4, scratch0());
1344 __ vmov(r0, r1, left);
1345 __ vmov(r2, r3, right);
Steve Block44f0eee2011-05-26 01:26:41 +01001346 __ CallCFunction(
1347 ExternalReference::double_fp_operation(Token::MOD, isolate()), 4);
Steve Block1e0659c2011-05-24 12:43:12 +01001348 // Move the result in the double result register.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001349 __ GetCFunctionDoubleResult(ToDoubleRegister(instr->result()));
Steve Block1e0659c2011-05-24 12:43:12 +01001350
1351 // Restore r0-r3.
1352 __ ldm(ia_w, sp, r0.bit() | r1.bit() | r2.bit() | r3.bit());
Ben Murdochb0fe1622011-05-05 13:52:32 +01001353 break;
1354 }
1355 default:
1356 UNREACHABLE();
1357 break;
1358 }
1359}
1360
1361
1362void LCodeGen::DoArithmeticT(LArithmeticT* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01001363 ASSERT(ToRegister(instr->InputAt(0)).is(r1));
1364 ASSERT(ToRegister(instr->InputAt(1)).is(r0));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001365 ASSERT(ToRegister(instr->result()).is(r0));
1366
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001367 TypeRecordingBinaryOpStub stub(instr->op(), NO_OVERWRITE);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001368 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
1369}
1370
1371
1372int LCodeGen::GetNextEmittedBlock(int block) {
1373 for (int i = block + 1; i < graph()->blocks()->length(); ++i) {
1374 LLabel* label = chunk_->GetLabel(i);
1375 if (!label->HasReplacement()) return i;
1376 }
1377 return -1;
1378}
1379
1380
1381void LCodeGen::EmitBranch(int left_block, int right_block, Condition cc) {
1382 int next_block = GetNextEmittedBlock(current_block_);
1383 right_block = chunk_->LookupDestination(right_block);
1384 left_block = chunk_->LookupDestination(left_block);
1385
1386 if (right_block == left_block) {
1387 EmitGoto(left_block);
1388 } else if (left_block == next_block) {
1389 __ b(NegateCondition(cc), chunk_->GetAssemblyLabel(right_block));
1390 } else if (right_block == next_block) {
1391 __ b(cc, chunk_->GetAssemblyLabel(left_block));
1392 } else {
1393 __ b(cc, chunk_->GetAssemblyLabel(left_block));
1394 __ b(chunk_->GetAssemblyLabel(right_block));
1395 }
1396}
1397
1398
1399void LCodeGen::DoBranch(LBranch* instr) {
1400 int true_block = chunk_->LookupDestination(instr->true_block_id());
1401 int false_block = chunk_->LookupDestination(instr->false_block_id());
1402
1403 Representation r = instr->hydrogen()->representation();
1404 if (r.IsInteger32()) {
Steve Block1e0659c2011-05-24 12:43:12 +01001405 Register reg = ToRegister(instr->InputAt(0));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001406 __ cmp(reg, Operand(0));
Steve Block1e0659c2011-05-24 12:43:12 +01001407 EmitBranch(true_block, false_block, ne);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001408 } else if (r.IsDouble()) {
Steve Block1e0659c2011-05-24 12:43:12 +01001409 DoubleRegister reg = ToDoubleRegister(instr->InputAt(0));
Ben Murdoch086aeea2011-05-13 15:57:08 +01001410 Register scratch = scratch0();
1411
Ben Murdochb8e0da22011-05-16 14:20:40 +01001412 // Test the double value. Zero and NaN are false.
1413 __ VFPCompareAndLoadFlags(reg, 0.0, scratch);
1414 __ tst(scratch, Operand(kVFPZConditionFlagBit | kVFPVConditionFlagBit));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001415 EmitBranch(true_block, false_block, ne);
1416 } else {
1417 ASSERT(r.IsTagged());
Steve Block1e0659c2011-05-24 12:43:12 +01001418 Register reg = ToRegister(instr->InputAt(0));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001419 if (instr->hydrogen()->type().IsBoolean()) {
1420 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
1421 __ cmp(reg, ip);
1422 EmitBranch(true_block, false_block, eq);
1423 } else {
1424 Label* true_label = chunk_->GetAssemblyLabel(true_block);
1425 Label* false_label = chunk_->GetAssemblyLabel(false_block);
1426
1427 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
1428 __ cmp(reg, ip);
1429 __ b(eq, false_label);
1430 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
1431 __ cmp(reg, ip);
1432 __ b(eq, true_label);
1433 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
1434 __ cmp(reg, ip);
1435 __ b(eq, false_label);
1436 __ cmp(reg, Operand(0));
1437 __ b(eq, false_label);
1438 __ tst(reg, Operand(kSmiTagMask));
1439 __ b(eq, true_label);
1440
Ben Murdochb8e0da22011-05-16 14:20:40 +01001441 // Test double values. Zero and NaN are false.
Ben Murdochb0fe1622011-05-05 13:52:32 +01001442 Label call_stub;
1443 DoubleRegister dbl_scratch = d0;
Steve Block9fac8402011-05-12 15:51:54 +01001444 Register scratch = scratch0();
1445 __ ldr(scratch, FieldMemOperand(reg, HeapObject::kMapOffset));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001446 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
Steve Block9fac8402011-05-12 15:51:54 +01001447 __ cmp(scratch, Operand(ip));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001448 __ b(ne, &call_stub);
1449 __ sub(ip, reg, Operand(kHeapObjectTag));
1450 __ vldr(dbl_scratch, ip, HeapNumber::kValueOffset);
Ben Murdochb8e0da22011-05-16 14:20:40 +01001451 __ VFPCompareAndLoadFlags(dbl_scratch, 0.0, scratch);
1452 __ tst(scratch, Operand(kVFPZConditionFlagBit | kVFPVConditionFlagBit));
Ben Murdoch086aeea2011-05-13 15:57:08 +01001453 __ b(ne, false_label);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001454 __ b(true_label);
1455
1456 // The conversion stub doesn't cause garbage collections so it's
1457 // safe to not record a safepoint after the call.
1458 __ bind(&call_stub);
1459 ToBooleanStub stub(reg);
1460 RegList saved_regs = kJSCallerSaved | kCalleeSaved;
1461 __ stm(db_w, sp, saved_regs);
1462 __ CallStub(&stub);
1463 __ cmp(reg, Operand(0));
1464 __ ldm(ia_w, sp, saved_regs);
Steve Block1e0659c2011-05-24 12:43:12 +01001465 EmitBranch(true_block, false_block, ne);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001466 }
1467 }
1468}
1469
1470
1471void LCodeGen::EmitGoto(int block, LDeferredCode* deferred_stack_check) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001472 block = chunk_->LookupDestination(block);
1473 int next_block = GetNextEmittedBlock(current_block_);
1474 if (block != next_block) {
Ben Murdochb8e0da22011-05-16 14:20:40 +01001475 // Perform stack overflow check if this goto needs it before jumping.
1476 if (deferred_stack_check != NULL) {
1477 __ LoadRoot(ip, Heap::kStackLimitRootIndex);
1478 __ cmp(sp, Operand(ip));
1479 __ b(hs, chunk_->GetAssemblyLabel(block));
1480 __ jmp(deferred_stack_check->entry());
1481 deferred_stack_check->SetExit(chunk_->GetAssemblyLabel(block));
1482 } else {
1483 __ jmp(chunk_->GetAssemblyLabel(block));
1484 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01001485 }
1486}
1487
1488
1489void LCodeGen::DoDeferredStackCheck(LGoto* instr) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01001490 PushSafepointRegistersScope scope(this, Safepoint::kWithRegisters);
1491 CallRuntimeFromDeferred(Runtime::kStackGuard, 0, instr);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001492}
1493
1494
1495void LCodeGen::DoGoto(LGoto* instr) {
Ben Murdochb8e0da22011-05-16 14:20:40 +01001496 class DeferredStackCheck: public LDeferredCode {
1497 public:
1498 DeferredStackCheck(LCodeGen* codegen, LGoto* instr)
1499 : LDeferredCode(codegen), instr_(instr) { }
1500 virtual void Generate() { codegen()->DoDeferredStackCheck(instr_); }
1501 private:
1502 LGoto* instr_;
1503 };
1504
1505 DeferredStackCheck* deferred = NULL;
1506 if (instr->include_stack_check()) {
1507 deferred = new DeferredStackCheck(this, instr);
1508 }
1509 EmitGoto(instr->block_id(), deferred);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001510}
1511
1512
1513Condition LCodeGen::TokenToCondition(Token::Value op, bool is_unsigned) {
Steve Block1e0659c2011-05-24 12:43:12 +01001514 Condition cond = kNoCondition;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001515 switch (op) {
1516 case Token::EQ:
1517 case Token::EQ_STRICT:
1518 cond = eq;
1519 break;
1520 case Token::LT:
1521 cond = is_unsigned ? lo : lt;
1522 break;
1523 case Token::GT:
1524 cond = is_unsigned ? hi : gt;
1525 break;
1526 case Token::LTE:
1527 cond = is_unsigned ? ls : le;
1528 break;
1529 case Token::GTE:
1530 cond = is_unsigned ? hs : ge;
1531 break;
1532 case Token::IN:
1533 case Token::INSTANCEOF:
1534 default:
1535 UNREACHABLE();
1536 }
1537 return cond;
1538}
1539
1540
1541void LCodeGen::EmitCmpI(LOperand* left, LOperand* right) {
Steve Block1e0659c2011-05-24 12:43:12 +01001542 __ cmp(ToRegister(left), ToRegister(right));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001543}
1544
1545
1546void LCodeGen::DoCmpID(LCmpID* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01001547 LOperand* left = instr->InputAt(0);
1548 LOperand* right = instr->InputAt(1);
1549 LOperand* result = instr->result();
1550 Register scratch = scratch0();
1551
1552 Label unordered, done;
1553 if (instr->is_double()) {
1554 // Compare left and right as doubles and load the
1555 // resulting flags into the normal status register.
1556 __ VFPCompareAndSetFlags(ToDoubleRegister(left), ToDoubleRegister(right));
1557 // If a NaN is involved, i.e. the result is unordered (V set),
1558 // jump to unordered to return false.
1559 __ b(vs, &unordered);
1560 } else {
1561 EmitCmpI(left, right);
1562 }
1563
1564 Condition cc = TokenToCondition(instr->op(), instr->is_double());
1565 __ LoadRoot(ToRegister(result), Heap::kTrueValueRootIndex);
1566 __ b(cc, &done);
1567
1568 __ bind(&unordered);
1569 __ LoadRoot(ToRegister(result), Heap::kFalseValueRootIndex);
1570 __ bind(&done);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001571}
1572
1573
1574void LCodeGen::DoCmpIDAndBranch(LCmpIDAndBranch* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01001575 LOperand* left = instr->InputAt(0);
1576 LOperand* right = instr->InputAt(1);
1577 int false_block = chunk_->LookupDestination(instr->false_block_id());
1578 int true_block = chunk_->LookupDestination(instr->true_block_id());
1579
1580 if (instr->is_double()) {
1581 // Compare left and right as doubles and load the
1582 // resulting flags into the normal status register.
1583 __ VFPCompareAndSetFlags(ToDoubleRegister(left), ToDoubleRegister(right));
1584 // If a NaN is involved, i.e. the result is unordered (V set),
1585 // jump to false block label.
1586 __ b(vs, chunk_->GetAssemblyLabel(false_block));
1587 } else {
1588 EmitCmpI(left, right);
1589 }
1590
1591 Condition cc = TokenToCondition(instr->op(), instr->is_double());
1592 EmitBranch(true_block, false_block, cc);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001593}
1594
1595
1596void LCodeGen::DoCmpJSObjectEq(LCmpJSObjectEq* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01001597 Register left = ToRegister(instr->InputAt(0));
1598 Register right = ToRegister(instr->InputAt(1));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001599 Register result = ToRegister(instr->result());
1600
1601 __ cmp(left, Operand(right));
1602 __ LoadRoot(result, Heap::kTrueValueRootIndex, eq);
1603 __ LoadRoot(result, Heap::kFalseValueRootIndex, ne);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001604}
1605
1606
1607void LCodeGen::DoCmpJSObjectEqAndBranch(LCmpJSObjectEqAndBranch* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01001608 Register left = ToRegister(instr->InputAt(0));
1609 Register right = ToRegister(instr->InputAt(1));
1610 int false_block = chunk_->LookupDestination(instr->false_block_id());
1611 int true_block = chunk_->LookupDestination(instr->true_block_id());
1612
1613 __ cmp(left, Operand(right));
1614 EmitBranch(true_block, false_block, eq);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001615}
1616
1617
1618void LCodeGen::DoIsNull(LIsNull* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01001619 Register reg = ToRegister(instr->InputAt(0));
Steve Block9fac8402011-05-12 15:51:54 +01001620 Register result = ToRegister(instr->result());
1621
1622 __ LoadRoot(ip, Heap::kNullValueRootIndex);
1623 __ cmp(reg, ip);
1624 if (instr->is_strict()) {
1625 __ LoadRoot(result, Heap::kTrueValueRootIndex, eq);
1626 __ LoadRoot(result, Heap::kFalseValueRootIndex, ne);
1627 } else {
1628 Label true_value, false_value, done;
1629 __ b(eq, &true_value);
1630 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
1631 __ cmp(ip, reg);
1632 __ b(eq, &true_value);
1633 __ tst(reg, Operand(kSmiTagMask));
1634 __ b(eq, &false_value);
1635 // Check for undetectable objects by looking in the bit field in
1636 // the map. The object has already been smi checked.
1637 Register scratch = result;
1638 __ ldr(scratch, FieldMemOperand(reg, HeapObject::kMapOffset));
1639 __ ldrb(scratch, FieldMemOperand(scratch, Map::kBitFieldOffset));
1640 __ tst(scratch, Operand(1 << Map::kIsUndetectable));
1641 __ b(ne, &true_value);
1642 __ bind(&false_value);
1643 __ LoadRoot(result, Heap::kFalseValueRootIndex);
1644 __ jmp(&done);
1645 __ bind(&true_value);
1646 __ LoadRoot(result, Heap::kTrueValueRootIndex);
1647 __ bind(&done);
1648 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01001649}
1650
1651
1652void LCodeGen::DoIsNullAndBranch(LIsNullAndBranch* instr) {
Steve Block9fac8402011-05-12 15:51:54 +01001653 Register scratch = scratch0();
Steve Block1e0659c2011-05-24 12:43:12 +01001654 Register reg = ToRegister(instr->InputAt(0));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001655
1656 // TODO(fsc): If the expression is known to be a smi, then it's
1657 // definitely not null. Jump to the false block.
1658
1659 int true_block = chunk_->LookupDestination(instr->true_block_id());
1660 int false_block = chunk_->LookupDestination(instr->false_block_id());
1661
1662 __ LoadRoot(ip, Heap::kNullValueRootIndex);
1663 __ cmp(reg, ip);
1664 if (instr->is_strict()) {
1665 EmitBranch(true_block, false_block, eq);
1666 } else {
1667 Label* true_label = chunk_->GetAssemblyLabel(true_block);
1668 Label* false_label = chunk_->GetAssemblyLabel(false_block);
1669 __ b(eq, true_label);
1670 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
1671 __ cmp(reg, ip);
1672 __ b(eq, true_label);
1673 __ tst(reg, Operand(kSmiTagMask));
1674 __ b(eq, false_label);
1675 // Check for undetectable objects by looking in the bit field in
1676 // the map. The object has already been smi checked.
Ben Murdochb0fe1622011-05-05 13:52:32 +01001677 __ ldr(scratch, FieldMemOperand(reg, HeapObject::kMapOffset));
1678 __ ldrb(scratch, FieldMemOperand(scratch, Map::kBitFieldOffset));
1679 __ tst(scratch, Operand(1 << Map::kIsUndetectable));
1680 EmitBranch(true_block, false_block, ne);
1681 }
1682}
1683
1684
1685Condition LCodeGen::EmitIsObject(Register input,
1686 Register temp1,
1687 Register temp2,
1688 Label* is_not_object,
1689 Label* is_object) {
Steve Block1e0659c2011-05-24 12:43:12 +01001690 __ JumpIfSmi(input, is_not_object);
1691
1692 __ LoadRoot(temp1, Heap::kNullValueRootIndex);
1693 __ cmp(input, temp1);
1694 __ b(eq, is_object);
1695
1696 // Load map.
1697 __ ldr(temp1, FieldMemOperand(input, HeapObject::kMapOffset));
1698 // Undetectable objects behave like undefined.
1699 __ ldrb(temp2, FieldMemOperand(temp1, Map::kBitFieldOffset));
1700 __ tst(temp2, Operand(1 << Map::kIsUndetectable));
1701 __ b(ne, is_not_object);
1702
1703 // Load instance type and check that it is in object type range.
1704 __ ldrb(temp2, FieldMemOperand(temp1, Map::kInstanceTypeOffset));
1705 __ cmp(temp2, Operand(FIRST_JS_OBJECT_TYPE));
1706 __ b(lt, is_not_object);
1707 __ cmp(temp2, Operand(LAST_JS_OBJECT_TYPE));
1708 return le;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001709}
1710
1711
1712void LCodeGen::DoIsObject(LIsObject* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01001713 Register reg = ToRegister(instr->InputAt(0));
1714 Register result = ToRegister(instr->result());
1715 Register temp = scratch0();
1716 Label is_false, is_true, done;
1717
1718 Condition true_cond = EmitIsObject(reg, result, temp, &is_false, &is_true);
1719 __ b(true_cond, &is_true);
1720
1721 __ bind(&is_false);
1722 __ LoadRoot(result, Heap::kFalseValueRootIndex);
1723 __ b(&done);
1724
1725 __ bind(&is_true);
1726 __ LoadRoot(result, Heap::kTrueValueRootIndex);
1727
1728 __ bind(&done);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001729}
1730
1731
1732void LCodeGen::DoIsObjectAndBranch(LIsObjectAndBranch* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01001733 Register reg = ToRegister(instr->InputAt(0));
1734 Register temp1 = ToRegister(instr->TempAt(0));
1735 Register temp2 = scratch0();
1736
1737 int true_block = chunk_->LookupDestination(instr->true_block_id());
1738 int false_block = chunk_->LookupDestination(instr->false_block_id());
1739 Label* true_label = chunk_->GetAssemblyLabel(true_block);
1740 Label* false_label = chunk_->GetAssemblyLabel(false_block);
1741
1742 Condition true_cond =
1743 EmitIsObject(reg, temp1, temp2, false_label, true_label);
1744
1745 EmitBranch(true_block, false_block, true_cond);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001746}
1747
1748
1749void LCodeGen::DoIsSmi(LIsSmi* instr) {
1750 ASSERT(instr->hydrogen()->value()->representation().IsTagged());
1751 Register result = ToRegister(instr->result());
Steve Block1e0659c2011-05-24 12:43:12 +01001752 Register input_reg = EmitLoadRegister(instr->InputAt(0), ip);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001753 __ tst(input_reg, Operand(kSmiTagMask));
1754 __ LoadRoot(result, Heap::kTrueValueRootIndex);
1755 Label done;
1756 __ b(eq, &done);
1757 __ LoadRoot(result, Heap::kFalseValueRootIndex);
1758 __ bind(&done);
1759}
1760
1761
1762void LCodeGen::DoIsSmiAndBranch(LIsSmiAndBranch* instr) {
1763 int true_block = chunk_->LookupDestination(instr->true_block_id());
1764 int false_block = chunk_->LookupDestination(instr->false_block_id());
1765
Steve Block1e0659c2011-05-24 12:43:12 +01001766 Register input_reg = EmitLoadRegister(instr->InputAt(0), ip);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001767 __ tst(input_reg, Operand(kSmiTagMask));
1768 EmitBranch(true_block, false_block, eq);
1769}
1770
1771
Steve Block1e0659c2011-05-24 12:43:12 +01001772static InstanceType TestType(HHasInstanceType* instr) {
1773 InstanceType from = instr->from();
1774 InstanceType to = instr->to();
Ben Murdochb0fe1622011-05-05 13:52:32 +01001775 if (from == FIRST_TYPE) return to;
1776 ASSERT(from == to || to == LAST_TYPE);
1777 return from;
1778}
1779
1780
Steve Block1e0659c2011-05-24 12:43:12 +01001781static Condition BranchCondition(HHasInstanceType* instr) {
1782 InstanceType from = instr->from();
1783 InstanceType to = instr->to();
Ben Murdochb0fe1622011-05-05 13:52:32 +01001784 if (from == to) return eq;
1785 if (to == LAST_TYPE) return hs;
1786 if (from == FIRST_TYPE) return ls;
1787 UNREACHABLE();
1788 return eq;
1789}
1790
1791
1792void LCodeGen::DoHasInstanceType(LHasInstanceType* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01001793 Register input = ToRegister(instr->InputAt(0));
1794 Register result = ToRegister(instr->result());
1795
1796 ASSERT(instr->hydrogen()->value()->representation().IsTagged());
1797 Label done;
1798 __ tst(input, Operand(kSmiTagMask));
1799 __ LoadRoot(result, Heap::kFalseValueRootIndex, eq);
1800 __ b(eq, &done);
1801 __ CompareObjectType(input, result, result, TestType(instr->hydrogen()));
1802 Condition cond = BranchCondition(instr->hydrogen());
1803 __ LoadRoot(result, Heap::kTrueValueRootIndex, cond);
1804 __ LoadRoot(result, Heap::kFalseValueRootIndex, NegateCondition(cond));
1805 __ bind(&done);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001806}
1807
1808
1809void LCodeGen::DoHasInstanceTypeAndBranch(LHasInstanceTypeAndBranch* instr) {
Steve Block9fac8402011-05-12 15:51:54 +01001810 Register scratch = scratch0();
Steve Block1e0659c2011-05-24 12:43:12 +01001811 Register input = ToRegister(instr->InputAt(0));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001812
1813 int true_block = chunk_->LookupDestination(instr->true_block_id());
1814 int false_block = chunk_->LookupDestination(instr->false_block_id());
1815
1816 Label* false_label = chunk_->GetAssemblyLabel(false_block);
1817
1818 __ tst(input, Operand(kSmiTagMask));
1819 __ b(eq, false_label);
1820
Steve Block1e0659c2011-05-24 12:43:12 +01001821 __ CompareObjectType(input, scratch, scratch, TestType(instr->hydrogen()));
1822 EmitBranch(true_block, false_block, BranchCondition(instr->hydrogen()));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001823}
1824
1825
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001826void LCodeGen::DoGetCachedArrayIndex(LGetCachedArrayIndex* instr) {
1827 Register input = ToRegister(instr->InputAt(0));
1828 Register result = ToRegister(instr->result());
1829
1830 if (FLAG_debug_code) {
1831 __ AbortIfNotString(input);
1832 }
1833
1834 __ ldr(result, FieldMemOperand(input, String::kHashFieldOffset));
1835 __ IndexFromHash(result, result);
1836}
1837
1838
Ben Murdochb0fe1622011-05-05 13:52:32 +01001839void LCodeGen::DoHasCachedArrayIndex(LHasCachedArrayIndex* instr) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001840 Register input = ToRegister(instr->InputAt(0));
1841 Register result = ToRegister(instr->result());
1842 Register scratch = scratch0();
1843
1844 ASSERT(instr->hydrogen()->value()->representation().IsTagged());
1845 __ ldr(scratch,
1846 FieldMemOperand(input, String::kHashFieldOffset));
1847 __ tst(scratch, Operand(String::kContainsCachedArrayIndexMask));
1848 __ LoadRoot(result, Heap::kTrueValueRootIndex, eq);
1849 __ LoadRoot(result, Heap::kFalseValueRootIndex, ne);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001850}
1851
1852
1853void LCodeGen::DoHasCachedArrayIndexAndBranch(
1854 LHasCachedArrayIndexAndBranch* instr) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001855 Register input = ToRegister(instr->InputAt(0));
1856 Register scratch = scratch0();
1857
1858 int true_block = chunk_->LookupDestination(instr->true_block_id());
1859 int false_block = chunk_->LookupDestination(instr->false_block_id());
1860
1861 __ ldr(scratch,
1862 FieldMemOperand(input, String::kHashFieldOffset));
1863 __ tst(scratch, Operand(String::kContainsCachedArrayIndexMask));
1864 EmitBranch(true_block, false_block, eq);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001865}
1866
1867
Ben Murdochb8e0da22011-05-16 14:20:40 +01001868// Branches to a label or falls through with the answer in flags. Trashes
Ben Murdochb0fe1622011-05-05 13:52:32 +01001869// the temp registers, but not the input. Only input and temp2 may alias.
1870void LCodeGen::EmitClassOfTest(Label* is_true,
1871 Label* is_false,
1872 Handle<String>class_name,
1873 Register input,
1874 Register temp,
1875 Register temp2) {
Ben Murdochb8e0da22011-05-16 14:20:40 +01001876 ASSERT(!input.is(temp));
1877 ASSERT(!temp.is(temp2)); // But input and temp2 may be the same register.
1878 __ tst(input, Operand(kSmiTagMask));
1879 __ b(eq, is_false);
1880 __ CompareObjectType(input, temp, temp2, FIRST_JS_OBJECT_TYPE);
1881 __ b(lt, is_false);
1882
1883 // Map is now in temp.
1884 // Functions have class 'Function'.
1885 __ CompareInstanceType(temp, temp2, JS_FUNCTION_TYPE);
1886 if (class_name->IsEqualTo(CStrVector("Function"))) {
1887 __ b(eq, is_true);
1888 } else {
1889 __ b(eq, is_false);
1890 }
1891
1892 // Check if the constructor in the map is a function.
1893 __ ldr(temp, FieldMemOperand(temp, Map::kConstructorOffset));
1894
1895 // As long as JS_FUNCTION_TYPE is the last instance type and it is
1896 // right after LAST_JS_OBJECT_TYPE, we can avoid checking for
1897 // LAST_JS_OBJECT_TYPE.
1898 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
1899 ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
1900
1901 // Objects with a non-function constructor have class 'Object'.
1902 __ CompareObjectType(temp, temp2, temp2, JS_FUNCTION_TYPE);
1903 if (class_name->IsEqualTo(CStrVector("Object"))) {
1904 __ b(ne, is_true);
1905 } else {
1906 __ b(ne, is_false);
1907 }
1908
1909 // temp now contains the constructor function. Grab the
1910 // instance class name from there.
1911 __ ldr(temp, FieldMemOperand(temp, JSFunction::kSharedFunctionInfoOffset));
1912 __ ldr(temp, FieldMemOperand(temp,
1913 SharedFunctionInfo::kInstanceClassNameOffset));
1914 // The class name we are testing against is a symbol because it's a literal.
1915 // The name in the constructor is a symbol because of the way the context is
1916 // booted. This routine isn't expected to work for random API-created
1917 // classes and it doesn't have to because you can't access it with natives
1918 // syntax. Since both sides are symbols it is sufficient to use an identity
1919 // comparison.
1920 __ cmp(temp, Operand(class_name));
1921 // End with the answer in flags.
Ben Murdochb0fe1622011-05-05 13:52:32 +01001922}
1923
1924
1925void LCodeGen::DoClassOfTest(LClassOfTest* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01001926 Register input = ToRegister(instr->InputAt(0));
Ben Murdochb8e0da22011-05-16 14:20:40 +01001927 Register result = ToRegister(instr->result());
1928 ASSERT(input.is(result));
1929 Handle<String> class_name = instr->hydrogen()->class_name();
1930
1931 Label done, is_true, is_false;
1932
1933 EmitClassOfTest(&is_true, &is_false, class_name, input, scratch0(), input);
1934 __ b(ne, &is_false);
1935
1936 __ bind(&is_true);
1937 __ LoadRoot(result, Heap::kTrueValueRootIndex);
1938 __ jmp(&done);
1939
1940 __ bind(&is_false);
1941 __ LoadRoot(result, Heap::kFalseValueRootIndex);
1942 __ bind(&done);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001943}
1944
1945
1946void LCodeGen::DoClassOfTestAndBranch(LClassOfTestAndBranch* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01001947 Register input = ToRegister(instr->InputAt(0));
Ben Murdochb8e0da22011-05-16 14:20:40 +01001948 Register temp = scratch0();
Steve Block1e0659c2011-05-24 12:43:12 +01001949 Register temp2 = ToRegister(instr->TempAt(0));
Ben Murdochb8e0da22011-05-16 14:20:40 +01001950 Handle<String> class_name = instr->hydrogen()->class_name();
1951
1952 int true_block = chunk_->LookupDestination(instr->true_block_id());
1953 int false_block = chunk_->LookupDestination(instr->false_block_id());
1954
1955 Label* true_label = chunk_->GetAssemblyLabel(true_block);
1956 Label* false_label = chunk_->GetAssemblyLabel(false_block);
1957
1958 EmitClassOfTest(true_label, false_label, class_name, input, temp, temp2);
1959
1960 EmitBranch(true_block, false_block, eq);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001961}
1962
1963
1964void LCodeGen::DoCmpMapAndBranch(LCmpMapAndBranch* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01001965 Register reg = ToRegister(instr->InputAt(0));
1966 Register temp = ToRegister(instr->TempAt(0));
Steve Block9fac8402011-05-12 15:51:54 +01001967 int true_block = instr->true_block_id();
1968 int false_block = instr->false_block_id();
1969
1970 __ ldr(temp, FieldMemOperand(reg, HeapObject::kMapOffset));
1971 __ cmp(temp, Operand(instr->map()));
1972 EmitBranch(true_block, false_block, eq);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001973}
1974
1975
1976void LCodeGen::DoInstanceOf(LInstanceOf* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01001977 ASSERT(ToRegister(instr->InputAt(0)).is(r0)); // Object is in r0.
1978 ASSERT(ToRegister(instr->InputAt(1)).is(r1)); // Function is in r1.
Steve Block9fac8402011-05-12 15:51:54 +01001979
Ben Murdochb0fe1622011-05-05 13:52:32 +01001980 InstanceofStub stub(InstanceofStub::kArgsInRegisters);
1981 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
1982
Steve Block44f0eee2011-05-26 01:26:41 +01001983 __ cmp(r0, Operand(0));
1984 __ mov(r0, Operand(factory()->false_value()), LeaveCC, ne);
1985 __ mov(r0, Operand(factory()->true_value()), LeaveCC, eq);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001986}
1987
1988
1989void LCodeGen::DoInstanceOfAndBranch(LInstanceOfAndBranch* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01001990 ASSERT(ToRegister(instr->InputAt(0)).is(r0)); // Object is in r0.
1991 ASSERT(ToRegister(instr->InputAt(1)).is(r1)); // Function is in r1.
1992
1993 int true_block = chunk_->LookupDestination(instr->true_block_id());
1994 int false_block = chunk_->LookupDestination(instr->false_block_id());
1995
1996 InstanceofStub stub(InstanceofStub::kArgsInRegisters);
1997 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
Steve Block44f0eee2011-05-26 01:26:41 +01001998 __ cmp(r0, Operand(0));
Steve Block1e0659c2011-05-24 12:43:12 +01001999 EmitBranch(true_block, false_block, eq);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002000}
2001
2002
Ben Murdoch086aeea2011-05-13 15:57:08 +01002003void LCodeGen::DoInstanceOfKnownGlobal(LInstanceOfKnownGlobal* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01002004 class DeferredInstanceOfKnownGlobal: public LDeferredCode {
2005 public:
2006 DeferredInstanceOfKnownGlobal(LCodeGen* codegen,
2007 LInstanceOfKnownGlobal* instr)
2008 : LDeferredCode(codegen), instr_(instr) { }
2009 virtual void Generate() {
2010 codegen()->DoDeferredLInstanceOfKnownGlobal(instr_, &map_check_);
2011 }
2012
2013 Label* map_check() { return &map_check_; }
2014
2015 private:
2016 LInstanceOfKnownGlobal* instr_;
2017 Label map_check_;
2018 };
2019
2020 DeferredInstanceOfKnownGlobal* deferred;
2021 deferred = new DeferredInstanceOfKnownGlobal(this, instr);
2022
2023 Label done, false_result;
2024 Register object = ToRegister(instr->InputAt(0));
2025 Register temp = ToRegister(instr->TempAt(0));
2026 Register result = ToRegister(instr->result());
2027
2028 ASSERT(object.is(r0));
2029 ASSERT(result.is(r0));
2030
2031 // A Smi is not instance of anything.
2032 __ JumpIfSmi(object, &false_result);
2033
2034 // This is the inlined call site instanceof cache. The two occurences of the
2035 // hole value will be patched to the last map/result pair generated by the
2036 // instanceof stub.
2037 Label cache_miss;
2038 Register map = temp;
2039 __ ldr(map, FieldMemOperand(object, HeapObject::kMapOffset));
2040 __ bind(deferred->map_check()); // Label for calculating code patching.
2041 // We use Factory::the_hole_value() on purpose instead of loading from the
2042 // root array to force relocation to be able to later patch with
2043 // the cached map.
Steve Block44f0eee2011-05-26 01:26:41 +01002044 __ mov(ip, Operand(factory()->the_hole_value()));
Steve Block1e0659c2011-05-24 12:43:12 +01002045 __ cmp(map, Operand(ip));
2046 __ b(ne, &cache_miss);
2047 // We use Factory::the_hole_value() on purpose instead of loading from the
2048 // root array to force relocation to be able to later patch
2049 // with true or false.
Steve Block44f0eee2011-05-26 01:26:41 +01002050 __ mov(result, Operand(factory()->the_hole_value()));
Steve Block1e0659c2011-05-24 12:43:12 +01002051 __ b(&done);
2052
2053 // The inlined call site cache did not match. Check null and string before
2054 // calling the deferred code.
2055 __ bind(&cache_miss);
2056 // Null is not instance of anything.
2057 __ LoadRoot(ip, Heap::kNullValueRootIndex);
2058 __ cmp(object, Operand(ip));
2059 __ b(eq, &false_result);
2060
2061 // String values is not instance of anything.
2062 Condition is_string = masm_->IsObjectStringType(object, temp);
2063 __ b(is_string, &false_result);
2064
2065 // Go to the deferred code.
2066 __ b(deferred->entry());
2067
2068 __ bind(&false_result);
2069 __ LoadRoot(result, Heap::kFalseValueRootIndex);
2070
2071 // Here result has either true or false. Deferred code also produces true or
2072 // false object.
2073 __ bind(deferred->exit());
2074 __ bind(&done);
2075}
2076
2077
2078void LCodeGen::DoDeferredLInstanceOfKnownGlobal(LInstanceOfKnownGlobal* instr,
2079 Label* map_check) {
2080 Register result = ToRegister(instr->result());
2081 ASSERT(result.is(r0));
2082
2083 InstanceofStub::Flags flags = InstanceofStub::kNoFlags;
2084 flags = static_cast<InstanceofStub::Flags>(
2085 flags | InstanceofStub::kArgsInRegisters);
2086 flags = static_cast<InstanceofStub::Flags>(
2087 flags | InstanceofStub::kCallSiteInlineCheck);
2088 flags = static_cast<InstanceofStub::Flags>(
2089 flags | InstanceofStub::kReturnTrueFalseObject);
2090 InstanceofStub stub(flags);
2091
Ben Murdoch8b112d22011-06-08 16:22:53 +01002092 PushSafepointRegistersScope scope(this, Safepoint::kWithRegisters);
Steve Block1e0659c2011-05-24 12:43:12 +01002093
2094 // Get the temp register reserved by the instruction. This needs to be r4 as
2095 // its slot of the pushing of safepoint registers is used to communicate the
2096 // offset to the location of the map check.
2097 Register temp = ToRegister(instr->TempAt(0));
2098 ASSERT(temp.is(r4));
2099 __ mov(InstanceofStub::right(), Operand(instr->function()));
2100 static const int kAdditionalDelta = 4;
2101 int delta = masm_->InstructionsGeneratedSince(map_check) + kAdditionalDelta;
2102 Label before_push_delta;
2103 __ bind(&before_push_delta);
2104 __ BlockConstPoolFor(kAdditionalDelta);
2105 __ mov(temp, Operand(delta * kPointerSize));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002106 __ StoreToSafepointRegisterSlot(temp, temp);
Ben Murdoch8b112d22011-06-08 16:22:53 +01002107 CallCodeGeneric(stub.GetCode(),
2108 RelocInfo::CODE_TARGET,
2109 instr,
2110 RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS);
Steve Block1e0659c2011-05-24 12:43:12 +01002111 // Put the result value into the result register slot and
2112 // restore all registers.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002113 __ StoreToSafepointRegisterSlot(result, result);
Ben Murdoch086aeea2011-05-13 15:57:08 +01002114}
2115
Ben Murdochb0fe1622011-05-05 13:52:32 +01002116
2117static Condition ComputeCompareCondition(Token::Value op) {
2118 switch (op) {
2119 case Token::EQ_STRICT:
2120 case Token::EQ:
2121 return eq;
2122 case Token::LT:
2123 return lt;
2124 case Token::GT:
2125 return gt;
2126 case Token::LTE:
2127 return le;
2128 case Token::GTE:
2129 return ge;
2130 default:
2131 UNREACHABLE();
Steve Block1e0659c2011-05-24 12:43:12 +01002132 return kNoCondition;
Ben Murdochb0fe1622011-05-05 13:52:32 +01002133 }
2134}
2135
2136
2137void LCodeGen::DoCmpT(LCmpT* instr) {
2138 Token::Value op = instr->op();
2139
2140 Handle<Code> ic = CompareIC::GetUninitialized(op);
2141 CallCode(ic, RelocInfo::CODE_TARGET, instr);
Steve Block1e0659c2011-05-24 12:43:12 +01002142 __ cmp(r0, Operand(0)); // This instruction also signals no smi code inlined.
Ben Murdochb0fe1622011-05-05 13:52:32 +01002143
2144 Condition condition = ComputeCompareCondition(op);
2145 if (op == Token::GT || op == Token::LTE) {
2146 condition = ReverseCondition(condition);
2147 }
Ben Murdochb8e0da22011-05-16 14:20:40 +01002148 __ LoadRoot(ToRegister(instr->result()),
2149 Heap::kTrueValueRootIndex,
2150 condition);
2151 __ LoadRoot(ToRegister(instr->result()),
2152 Heap::kFalseValueRootIndex,
2153 NegateCondition(condition));
Ben Murdochb0fe1622011-05-05 13:52:32 +01002154}
2155
2156
2157void LCodeGen::DoCmpTAndBranch(LCmpTAndBranch* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01002158 Token::Value op = instr->op();
2159 int true_block = chunk_->LookupDestination(instr->true_block_id());
2160 int false_block = chunk_->LookupDestination(instr->false_block_id());
2161
2162 Handle<Code> ic = CompareIC::GetUninitialized(op);
2163 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2164
2165 // The compare stub expects compare condition and the input operands
2166 // reversed for GT and LTE.
2167 Condition condition = ComputeCompareCondition(op);
2168 if (op == Token::GT || op == Token::LTE) {
2169 condition = ReverseCondition(condition);
2170 }
2171 __ cmp(r0, Operand(0));
2172 EmitBranch(true_block, false_block, condition);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002173}
2174
2175
2176void LCodeGen::DoReturn(LReturn* instr) {
2177 if (FLAG_trace) {
2178 // Push the return value on the stack as the parameter.
2179 // Runtime::TraceExit returns its parameter in r0.
2180 __ push(r0);
2181 __ CallRuntime(Runtime::kTraceExit, 1);
2182 }
Steve Block053d10c2011-06-13 19:13:29 +01002183 int32_t sp_delta = (ParameterCount() + 1) * kPointerSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +01002184 __ mov(sp, fp);
2185 __ ldm(ia_w, sp, fp.bit() | lr.bit());
2186 __ add(sp, sp, Operand(sp_delta));
2187 __ Jump(lr);
2188}
2189
2190
Ben Murdoch8b112d22011-06-08 16:22:53 +01002191void LCodeGen::DoLoadGlobalCell(LLoadGlobalCell* instr) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01002192 Register result = ToRegister(instr->result());
2193 __ mov(ip, Operand(Handle<Object>(instr->hydrogen()->cell())));
2194 __ ldr(result, FieldMemOperand(ip, JSGlobalPropertyCell::kValueOffset));
2195 if (instr->hydrogen()->check_hole_value()) {
2196 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
2197 __ cmp(result, ip);
2198 DeoptimizeIf(eq, instr->environment());
2199 }
2200}
2201
2202
Ben Murdoch8b112d22011-06-08 16:22:53 +01002203void LCodeGen::DoLoadGlobalGeneric(LLoadGlobalGeneric* instr) {
2204 ASSERT(ToRegister(instr->global_object()).is(r0));
2205 ASSERT(ToRegister(instr->result()).is(r0));
2206
2207 __ mov(r2, Operand(instr->name()));
2208 RelocInfo::Mode mode = instr->for_typeof() ? RelocInfo::CODE_TARGET
2209 : RelocInfo::CODE_TARGET_CONTEXT;
2210 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
2211 CallCode(ic, mode, instr);
2212}
2213
2214
2215void LCodeGen::DoStoreGlobalCell(LStoreGlobalCell* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01002216 Register value = ToRegister(instr->InputAt(0));
2217 Register scratch = scratch0();
2218
2219 // Load the cell.
2220 __ mov(scratch, Operand(Handle<Object>(instr->hydrogen()->cell())));
2221
2222 // If the cell we are storing to contains the hole it could have
2223 // been deleted from the property dictionary. In that case, we need
2224 // to update the property details in the property dictionary to mark
2225 // it as no longer deleted.
2226 if (instr->hydrogen()->check_hole_value()) {
2227 Register scratch2 = ToRegister(instr->TempAt(0));
2228 __ ldr(scratch2,
2229 FieldMemOperand(scratch, JSGlobalPropertyCell::kValueOffset));
2230 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
2231 __ cmp(scratch2, ip);
2232 DeoptimizeIf(eq, instr->environment());
2233 }
2234
2235 // Store the value.
2236 __ str(value, FieldMemOperand(scratch, JSGlobalPropertyCell::kValueOffset));
Ben Murdochb0fe1622011-05-05 13:52:32 +01002237}
2238
2239
Ben Murdoch8b112d22011-06-08 16:22:53 +01002240void LCodeGen::DoStoreGlobalGeneric(LStoreGlobalGeneric* instr) {
2241 ASSERT(ToRegister(instr->global_object()).is(r1));
2242 ASSERT(ToRegister(instr->value()).is(r0));
2243
2244 __ mov(r2, Operand(instr->name()));
2245 Handle<Code> ic = instr->strict_mode()
2246 ? isolate()->builtins()->StoreIC_Initialize_Strict()
2247 : isolate()->builtins()->StoreIC_Initialize();
2248 CallCode(ic, RelocInfo::CODE_TARGET_CONTEXT, instr);
2249}
2250
2251
Ben Murdochb8e0da22011-05-16 14:20:40 +01002252void LCodeGen::DoLoadContextSlot(LLoadContextSlot* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01002253 Register context = ToRegister(instr->context());
Ben Murdochb8e0da22011-05-16 14:20:40 +01002254 Register result = ToRegister(instr->result());
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002255 __ ldr(result, ContextOperand(context, instr->slot_index()));
Ben Murdochb8e0da22011-05-16 14:20:40 +01002256}
2257
2258
Steve Block1e0659c2011-05-24 12:43:12 +01002259void LCodeGen::DoStoreContextSlot(LStoreContextSlot* instr) {
2260 Register context = ToRegister(instr->context());
2261 Register value = ToRegister(instr->value());
Steve Block1e0659c2011-05-24 12:43:12 +01002262 __ str(value, ContextOperand(context, instr->slot_index()));
2263 if (instr->needs_write_barrier()) {
2264 int offset = Context::SlotOffset(instr->slot_index());
2265 __ RecordWrite(context, Operand(offset), value, scratch0());
2266 }
2267}
2268
2269
Ben Murdochb0fe1622011-05-05 13:52:32 +01002270void LCodeGen::DoLoadNamedField(LLoadNamedField* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01002271 Register object = ToRegister(instr->InputAt(0));
Steve Block9fac8402011-05-12 15:51:54 +01002272 Register result = ToRegister(instr->result());
2273 if (instr->hydrogen()->is_in_object()) {
2274 __ ldr(result, FieldMemOperand(object, instr->hydrogen()->offset()));
2275 } else {
2276 __ ldr(result, FieldMemOperand(object, JSObject::kPropertiesOffset));
2277 __ ldr(result, FieldMemOperand(result, instr->hydrogen()->offset()));
2278 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01002279}
2280
2281
Steve Block44f0eee2011-05-26 01:26:41 +01002282void LCodeGen::EmitLoadField(Register result,
2283 Register object,
2284 Handle<Map> type,
2285 Handle<String> name) {
2286 LookupResult lookup;
2287 type->LookupInDescriptors(NULL, *name, &lookup);
2288 ASSERT(lookup.IsProperty() && lookup.type() == FIELD);
2289 int index = lookup.GetLocalFieldIndexFromMap(*type);
2290 int offset = index * kPointerSize;
2291 if (index < 0) {
2292 // Negative property indices are in-object properties, indexed
2293 // from the end of the fixed part of the object.
2294 __ ldr(result, FieldMemOperand(object, offset + type->instance_size()));
2295 } else {
2296 // Non-negative property indices are in the properties array.
2297 __ ldr(result, FieldMemOperand(object, JSObject::kPropertiesOffset));
2298 __ ldr(result, FieldMemOperand(result, offset + FixedArray::kHeaderSize));
2299 }
2300}
2301
2302
2303void LCodeGen::DoLoadNamedFieldPolymorphic(LLoadNamedFieldPolymorphic* instr) {
2304 Register object = ToRegister(instr->object());
2305 Register result = ToRegister(instr->result());
2306 Register scratch = scratch0();
2307 int map_count = instr->hydrogen()->types()->length();
2308 Handle<String> name = instr->hydrogen()->name();
2309 if (map_count == 0) {
2310 ASSERT(instr->hydrogen()->need_generic());
2311 __ mov(r2, Operand(name));
2312 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
2313 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2314 } else {
2315 Label done;
2316 __ ldr(scratch, FieldMemOperand(object, HeapObject::kMapOffset));
2317 for (int i = 0; i < map_count - 1; ++i) {
2318 Handle<Map> map = instr->hydrogen()->types()->at(i);
2319 Label next;
2320 __ cmp(scratch, Operand(map));
2321 __ b(ne, &next);
2322 EmitLoadField(result, object, map, name);
2323 __ b(&done);
2324 __ bind(&next);
2325 }
2326 Handle<Map> map = instr->hydrogen()->types()->last();
2327 __ cmp(scratch, Operand(map));
2328 if (instr->hydrogen()->need_generic()) {
2329 Label generic;
2330 __ b(ne, &generic);
2331 EmitLoadField(result, object, map, name);
2332 __ b(&done);
2333 __ bind(&generic);
2334 __ mov(r2, Operand(name));
2335 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
2336 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2337 } else {
2338 DeoptimizeIf(ne, instr->environment());
2339 EmitLoadField(result, object, map, name);
2340 }
2341 __ bind(&done);
2342 }
2343}
2344
2345
Ben Murdochb0fe1622011-05-05 13:52:32 +01002346void LCodeGen::DoLoadNamedGeneric(LLoadNamedGeneric* instr) {
2347 ASSERT(ToRegister(instr->object()).is(r0));
2348 ASSERT(ToRegister(instr->result()).is(r0));
2349
2350 // Name is always in r2.
2351 __ mov(r2, Operand(instr->name()));
Steve Block44f0eee2011-05-26 01:26:41 +01002352 Handle<Code> ic = isolate()->builtins()->LoadIC_Initialize();
Ben Murdochb0fe1622011-05-05 13:52:32 +01002353 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2354}
2355
2356
Steve Block9fac8402011-05-12 15:51:54 +01002357void LCodeGen::DoLoadFunctionPrototype(LLoadFunctionPrototype* instr) {
2358 Register scratch = scratch0();
2359 Register function = ToRegister(instr->function());
2360 Register result = ToRegister(instr->result());
2361
2362 // Check that the function really is a function. Load map into the
2363 // result register.
2364 __ CompareObjectType(function, result, scratch, JS_FUNCTION_TYPE);
2365 DeoptimizeIf(ne, instr->environment());
2366
2367 // Make sure that the function has an instance prototype.
2368 Label non_instance;
2369 __ ldrb(scratch, FieldMemOperand(result, Map::kBitFieldOffset));
2370 __ tst(scratch, Operand(1 << Map::kHasNonInstancePrototype));
2371 __ b(ne, &non_instance);
2372
2373 // Get the prototype or initial map from the function.
2374 __ ldr(result,
2375 FieldMemOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
2376
2377 // Check that the function has a prototype or an initial map.
2378 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
2379 __ cmp(result, ip);
2380 DeoptimizeIf(eq, instr->environment());
2381
2382 // If the function does not have an initial map, we're done.
2383 Label done;
2384 __ CompareObjectType(result, scratch, scratch, MAP_TYPE);
2385 __ b(ne, &done);
2386
2387 // Get the prototype from the initial map.
2388 __ ldr(result, FieldMemOperand(result, Map::kPrototypeOffset));
2389 __ jmp(&done);
2390
2391 // Non-instance prototype: Fetch prototype from constructor field
2392 // in initial map.
2393 __ bind(&non_instance);
2394 __ ldr(result, FieldMemOperand(result, Map::kConstructorOffset));
2395
2396 // All done.
2397 __ bind(&done);
2398}
2399
2400
Ben Murdochb0fe1622011-05-05 13:52:32 +01002401void LCodeGen::DoLoadElements(LLoadElements* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01002402 Register result = ToRegister(instr->result());
2403 Register input = ToRegister(instr->InputAt(0));
Ben Murdoch086aeea2011-05-13 15:57:08 +01002404 Register scratch = scratch0();
2405
Steve Block1e0659c2011-05-24 12:43:12 +01002406 __ ldr(result, FieldMemOperand(input, JSObject::kElementsOffset));
Ben Murdoch086aeea2011-05-13 15:57:08 +01002407 if (FLAG_debug_code) {
2408 Label done;
Steve Block1e0659c2011-05-24 12:43:12 +01002409 __ ldr(scratch, FieldMemOperand(result, HeapObject::kMapOffset));
Ben Murdoch086aeea2011-05-13 15:57:08 +01002410 __ LoadRoot(ip, Heap::kFixedArrayMapRootIndex);
2411 __ cmp(scratch, ip);
2412 __ b(eq, &done);
2413 __ LoadRoot(ip, Heap::kFixedCOWArrayMapRootIndex);
2414 __ cmp(scratch, ip);
Ben Murdoch8b112d22011-06-08 16:22:53 +01002415 __ b(eq, &done);
2416 __ ldr(scratch, FieldMemOperand(result, HeapObject::kMapOffset));
2417 __ ldrb(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
2418 __ sub(scratch, scratch, Operand(FIRST_EXTERNAL_ARRAY_TYPE));
2419 __ cmp(scratch, Operand(kExternalArrayTypeCount));
2420 __ Check(cc, "Check for fast elements failed.");
Ben Murdoch086aeea2011-05-13 15:57:08 +01002421 __ bind(&done);
2422 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01002423}
2424
2425
Steve Block44f0eee2011-05-26 01:26:41 +01002426void LCodeGen::DoLoadExternalArrayPointer(
2427 LLoadExternalArrayPointer* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01002428 Register to_reg = ToRegister(instr->result());
2429 Register from_reg = ToRegister(instr->InputAt(0));
Steve Block44f0eee2011-05-26 01:26:41 +01002430 __ ldr(to_reg, FieldMemOperand(from_reg,
2431 ExternalArray::kExternalPointerOffset));
Steve Block1e0659c2011-05-24 12:43:12 +01002432}
2433
2434
Ben Murdochb0fe1622011-05-05 13:52:32 +01002435void LCodeGen::DoAccessArgumentsAt(LAccessArgumentsAt* instr) {
Ben Murdoch086aeea2011-05-13 15:57:08 +01002436 Register arguments = ToRegister(instr->arguments());
2437 Register length = ToRegister(instr->length());
2438 Register index = ToRegister(instr->index());
2439 Register result = ToRegister(instr->result());
2440
2441 // Bailout index is not a valid argument index. Use unsigned check to get
2442 // negative check for free.
2443 __ sub(length, length, index, SetCC);
2444 DeoptimizeIf(ls, instr->environment());
2445
2446 // There are two words between the frame pointer and the last argument.
2447 // Subtracting from length accounts for one of them add one more.
2448 __ add(length, length, Operand(1));
2449 __ ldr(result, MemOperand(arguments, length, LSL, kPointerSizeLog2));
Ben Murdochb0fe1622011-05-05 13:52:32 +01002450}
2451
2452
2453void LCodeGen::DoLoadKeyedFastElement(LLoadKeyedFastElement* instr) {
Ben Murdoch086aeea2011-05-13 15:57:08 +01002454 Register elements = ToRegister(instr->elements());
2455 Register key = EmitLoadRegister(instr->key(), scratch0());
Ben Murdochb8e0da22011-05-16 14:20:40 +01002456 Register result = ToRegister(instr->result());
Ben Murdoch086aeea2011-05-13 15:57:08 +01002457 Register scratch = scratch0();
Ben Murdochb8e0da22011-05-16 14:20:40 +01002458 ASSERT(result.is(elements));
Ben Murdoch086aeea2011-05-13 15:57:08 +01002459
2460 // Load the result.
2461 __ add(scratch, elements, Operand(key, LSL, kPointerSizeLog2));
2462 __ ldr(result, FieldMemOperand(scratch, FixedArray::kHeaderSize));
2463
Ben Murdochb8e0da22011-05-16 14:20:40 +01002464 // Check for the hole value.
2465 __ LoadRoot(scratch, Heap::kTheHoleValueRootIndex);
2466 __ cmp(result, scratch);
2467 DeoptimizeIf(eq, instr->environment());
Ben Murdochb0fe1622011-05-05 13:52:32 +01002468}
2469
2470
Steve Block44f0eee2011-05-26 01:26:41 +01002471void LCodeGen::DoLoadKeyedSpecializedArrayElement(
2472 LLoadKeyedSpecializedArrayElement* instr) {
Steve Block44f0eee2011-05-26 01:26:41 +01002473 Register external_pointer = ToRegister(instr->external_pointer());
Steve Block1e0659c2011-05-24 12:43:12 +01002474 Register key = ToRegister(instr->key());
Ben Murdoch8b112d22011-06-08 16:22:53 +01002475 ExternalArrayType array_type = instr->array_type();
2476 if (array_type == kExternalFloatArray) {
2477 CpuFeatures::Scope scope(VFP3);
2478 DwVfpRegister result(ToDoubleRegister(instr->result()));
2479 __ add(scratch0(), external_pointer, Operand(key, LSL, 2));
2480 __ vldr(result.low(), scratch0(), 0);
2481 __ vcvt_f64_f32(result, result.low());
2482 } else {
2483 Register result(ToRegister(instr->result()));
2484 switch (array_type) {
2485 case kExternalByteArray:
2486 __ ldrsb(result, MemOperand(external_pointer, key));
2487 break;
2488 case kExternalUnsignedByteArray:
2489 case kExternalPixelArray:
2490 __ ldrb(result, MemOperand(external_pointer, key));
2491 break;
2492 case kExternalShortArray:
2493 __ ldrsh(result, MemOperand(external_pointer, key, LSL, 1));
2494 break;
2495 case kExternalUnsignedShortArray:
2496 __ ldrh(result, MemOperand(external_pointer, key, LSL, 1));
2497 break;
2498 case kExternalIntArray:
2499 __ ldr(result, MemOperand(external_pointer, key, LSL, 2));
2500 break;
2501 case kExternalUnsignedIntArray:
2502 __ ldr(result, MemOperand(external_pointer, key, LSL, 2));
2503 __ cmp(result, Operand(0x80000000));
2504 // TODO(danno): we could be more clever here, perhaps having a special
2505 // version of the stub that detects if the overflow case actually
2506 // happens, and generate code that returns a double rather than int.
2507 DeoptimizeIf(cs, instr->environment());
2508 break;
2509 case kExternalFloatArray:
2510 UNREACHABLE();
2511 break;
2512 }
2513 }
Steve Block1e0659c2011-05-24 12:43:12 +01002514}
2515
2516
Ben Murdochb0fe1622011-05-05 13:52:32 +01002517void LCodeGen::DoLoadKeyedGeneric(LLoadKeyedGeneric* instr) {
2518 ASSERT(ToRegister(instr->object()).is(r1));
2519 ASSERT(ToRegister(instr->key()).is(r0));
2520
Steve Block44f0eee2011-05-26 01:26:41 +01002521 Handle<Code> ic = isolate()->builtins()->KeyedLoadIC_Initialize();
Ben Murdochb0fe1622011-05-05 13:52:32 +01002522 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2523}
2524
2525
2526void LCodeGen::DoArgumentsElements(LArgumentsElements* instr) {
Ben Murdoch086aeea2011-05-13 15:57:08 +01002527 Register scratch = scratch0();
2528 Register result = ToRegister(instr->result());
2529
2530 // Check if the calling frame is an arguments adaptor frame.
2531 Label done, adapted;
2532 __ ldr(scratch, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2533 __ ldr(result, MemOperand(scratch, StandardFrameConstants::kContextOffset));
2534 __ cmp(result, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2535
2536 // Result is the frame pointer for the frame if not adapted and for the real
2537 // frame below the adaptor frame if adapted.
2538 __ mov(result, fp, LeaveCC, ne);
2539 __ mov(result, scratch, LeaveCC, eq);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002540}
2541
2542
2543void LCodeGen::DoArgumentsLength(LArgumentsLength* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01002544 Register elem = ToRegister(instr->InputAt(0));
Ben Murdoch086aeea2011-05-13 15:57:08 +01002545 Register result = ToRegister(instr->result());
2546
2547 Label done;
2548
2549 // If no arguments adaptor frame the number of arguments is fixed.
2550 __ cmp(fp, elem);
2551 __ mov(result, Operand(scope()->num_parameters()));
2552 __ b(eq, &done);
2553
2554 // Arguments adaptor frame present. Get argument length from there.
2555 __ ldr(result, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2556 __ ldr(result,
2557 MemOperand(result, ArgumentsAdaptorFrameConstants::kLengthOffset));
2558 __ SmiUntag(result);
2559
2560 // Argument length is in result register.
2561 __ bind(&done);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002562}
2563
2564
2565void LCodeGen::DoApplyArguments(LApplyArguments* instr) {
Ben Murdochb8e0da22011-05-16 14:20:40 +01002566 Register receiver = ToRegister(instr->receiver());
2567 Register function = ToRegister(instr->function());
Ben Murdochb8e0da22011-05-16 14:20:40 +01002568 Register length = ToRegister(instr->length());
2569 Register elements = ToRegister(instr->elements());
Steve Block1e0659c2011-05-24 12:43:12 +01002570 Register scratch = scratch0();
2571 ASSERT(receiver.is(r0)); // Used for parameter count.
2572 ASSERT(function.is(r1)); // Required by InvokeFunction.
2573 ASSERT(ToRegister(instr->result()).is(r0));
Ben Murdochb8e0da22011-05-16 14:20:40 +01002574
Steve Block1e0659c2011-05-24 12:43:12 +01002575 // If the receiver is null or undefined, we have to pass the global object
2576 // as a receiver.
2577 Label global_object, receiver_ok;
2578 __ LoadRoot(scratch, Heap::kNullValueRootIndex);
2579 __ cmp(receiver, scratch);
2580 __ b(eq, &global_object);
2581 __ LoadRoot(scratch, Heap::kUndefinedValueRootIndex);
2582 __ cmp(receiver, scratch);
2583 __ b(eq, &global_object);
2584
2585 // Deoptimize if the receiver is not a JS object.
2586 __ tst(receiver, Operand(kSmiTagMask));
2587 DeoptimizeIf(eq, instr->environment());
2588 __ CompareObjectType(receiver, scratch, scratch, FIRST_JS_OBJECT_TYPE);
2589 DeoptimizeIf(lo, instr->environment());
2590 __ jmp(&receiver_ok);
2591
2592 __ bind(&global_object);
2593 __ ldr(receiver, GlobalObjectOperand());
2594 __ bind(&receiver_ok);
Ben Murdochb8e0da22011-05-16 14:20:40 +01002595
2596 // Copy the arguments to this function possibly from the
2597 // adaptor frame below it.
2598 const uint32_t kArgumentsLimit = 1 * KB;
2599 __ cmp(length, Operand(kArgumentsLimit));
2600 DeoptimizeIf(hi, instr->environment());
2601
2602 // Push the receiver and use the register to keep the original
2603 // number of arguments.
2604 __ push(receiver);
2605 __ mov(receiver, length);
2606 // The arguments are at a one pointer size offset from elements.
2607 __ add(elements, elements, Operand(1 * kPointerSize));
2608
2609 // Loop through the arguments pushing them onto the execution
2610 // stack.
Steve Block1e0659c2011-05-24 12:43:12 +01002611 Label invoke, loop;
Ben Murdochb8e0da22011-05-16 14:20:40 +01002612 // length is a small non-negative integer, due to the test above.
Steve Block44f0eee2011-05-26 01:26:41 +01002613 __ cmp(length, Operand(0));
Ben Murdochb8e0da22011-05-16 14:20:40 +01002614 __ b(eq, &invoke);
2615 __ bind(&loop);
2616 __ ldr(scratch, MemOperand(elements, length, LSL, 2));
2617 __ push(scratch);
2618 __ sub(length, length, Operand(1), SetCC);
2619 __ b(ne, &loop);
2620
2621 __ bind(&invoke);
Steve Block1e0659c2011-05-24 12:43:12 +01002622 ASSERT(instr->HasPointerMap() && instr->HasDeoptimizationEnvironment());
2623 LPointerMap* pointers = instr->pointer_map();
2624 LEnvironment* env = instr->deoptimization_environment();
2625 RecordPosition(pointers->position());
2626 RegisterEnvironmentForDeoptimization(env);
Ben Murdochb8e0da22011-05-16 14:20:40 +01002627 SafepointGenerator safepoint_generator(this,
Steve Block1e0659c2011-05-24 12:43:12 +01002628 pointers,
2629 env->deoptimization_index());
2630 // The number of arguments is stored in receiver which is r0, as expected
2631 // by InvokeFunction.
2632 v8::internal::ParameterCount actual(receiver);
Ben Murdochb8e0da22011-05-16 14:20:40 +01002633 __ InvokeFunction(function, actual, CALL_FUNCTION, &safepoint_generator);
Steve Block1e0659c2011-05-24 12:43:12 +01002634 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
Ben Murdochb0fe1622011-05-05 13:52:32 +01002635}
2636
2637
2638void LCodeGen::DoPushArgument(LPushArgument* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01002639 LOperand* argument = instr->InputAt(0);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002640 if (argument->IsDoubleRegister() || argument->IsDoubleStackSlot()) {
2641 Abort("DoPushArgument not implemented for double type.");
2642 } else {
2643 Register argument_reg = EmitLoadRegister(argument, ip);
2644 __ push(argument_reg);
2645 }
2646}
2647
2648
Steve Block1e0659c2011-05-24 12:43:12 +01002649void LCodeGen::DoContext(LContext* instr) {
2650 Register result = ToRegister(instr->result());
2651 __ mov(result, cp);
2652}
2653
2654
2655void LCodeGen::DoOuterContext(LOuterContext* instr) {
2656 Register context = ToRegister(instr->context());
2657 Register result = ToRegister(instr->result());
2658 __ ldr(result,
2659 MemOperand(context, Context::SlotOffset(Context::CLOSURE_INDEX)));
2660 __ ldr(result, FieldMemOperand(result, JSFunction::kContextOffset));
2661}
2662
2663
Ben Murdochb0fe1622011-05-05 13:52:32 +01002664void LCodeGen::DoGlobalObject(LGlobalObject* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01002665 Register context = ToRegister(instr->context());
Ben Murdochb0fe1622011-05-05 13:52:32 +01002666 Register result = ToRegister(instr->result());
2667 __ ldr(result, ContextOperand(cp, Context::GLOBAL_INDEX));
2668}
2669
2670
2671void LCodeGen::DoGlobalReceiver(LGlobalReceiver* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01002672 Register global = ToRegister(instr->global());
Ben Murdochb0fe1622011-05-05 13:52:32 +01002673 Register result = ToRegister(instr->result());
Steve Block1e0659c2011-05-24 12:43:12 +01002674 __ ldr(result, FieldMemOperand(global, GlobalObject::kGlobalReceiverOffset));
Ben Murdochb0fe1622011-05-05 13:52:32 +01002675}
2676
2677
2678void LCodeGen::CallKnownFunction(Handle<JSFunction> function,
2679 int arity,
2680 LInstruction* instr) {
2681 // Change context if needed.
2682 bool change_context =
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002683 (info()->closure()->context() != function->context()) ||
Ben Murdochb0fe1622011-05-05 13:52:32 +01002684 scope()->contains_with() ||
2685 (scope()->num_heap_slots() > 0);
2686 if (change_context) {
2687 __ ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
2688 }
2689
2690 // Set r0 to arguments count if adaption is not needed. Assumes that r0
2691 // is available to write to at this point.
2692 if (!function->NeedsArgumentsAdaption()) {
2693 __ mov(r0, Operand(arity));
2694 }
2695
2696 LPointerMap* pointers = instr->pointer_map();
2697 RecordPosition(pointers->position());
2698
2699 // Invoke function.
2700 __ ldr(ip, FieldMemOperand(r1, JSFunction::kCodeEntryOffset));
2701 __ Call(ip);
2702
2703 // Setup deoptimization.
Ben Murdoch8b112d22011-06-08 16:22:53 +01002704 RegisterLazyDeoptimization(instr, RECORD_SIMPLE_SAFEPOINT);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002705
2706 // Restore context.
2707 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2708}
2709
2710
2711void LCodeGen::DoCallConstantFunction(LCallConstantFunction* instr) {
Steve Block9fac8402011-05-12 15:51:54 +01002712 ASSERT(ToRegister(instr->result()).is(r0));
2713 __ mov(r1, Operand(instr->function()));
2714 CallKnownFunction(instr->function(), instr->arity(), instr);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002715}
2716
2717
2718void LCodeGen::DoDeferredMathAbsTaggedHeapNumber(LUnaryMathOperation* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01002719 ASSERT(instr->InputAt(0)->Equals(instr->result()));
2720 Register input = ToRegister(instr->InputAt(0));
2721 Register scratch = scratch0();
2722
2723 // Deoptimize if not a heap number.
2724 __ ldr(scratch, FieldMemOperand(input, HeapObject::kMapOffset));
2725 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
2726 __ cmp(scratch, Operand(ip));
2727 DeoptimizeIf(ne, instr->environment());
2728
2729 Label done;
2730 Register exponent = scratch0();
2731 scratch = no_reg;
2732 __ ldr(exponent, FieldMemOperand(input, HeapNumber::kExponentOffset));
2733 // Check the sign of the argument. If the argument is positive, just
2734 // return it. We do not need to patch the stack since |input| and
2735 // |result| are the same register and |input| would be restored
2736 // unchanged by popping safepoint registers.
2737 __ tst(exponent, Operand(HeapNumber::kSignMask));
2738 __ b(eq, &done);
2739
2740 // Input is negative. Reverse its sign.
2741 // Preserve the value of all registers.
Ben Murdoch8b112d22011-06-08 16:22:53 +01002742 {
2743 PushSafepointRegistersScope scope(this, Safepoint::kWithRegisters);
Steve Block1e0659c2011-05-24 12:43:12 +01002744
Ben Murdoch8b112d22011-06-08 16:22:53 +01002745 // Registers were saved at the safepoint, so we can use
2746 // many scratch registers.
2747 Register tmp1 = input.is(r1) ? r0 : r1;
2748 Register tmp2 = input.is(r2) ? r0 : r2;
2749 Register tmp3 = input.is(r3) ? r0 : r3;
2750 Register tmp4 = input.is(r4) ? r0 : r4;
Steve Block1e0659c2011-05-24 12:43:12 +01002751
Ben Murdoch8b112d22011-06-08 16:22:53 +01002752 // exponent: floating point exponent value.
Steve Block1e0659c2011-05-24 12:43:12 +01002753
Ben Murdoch8b112d22011-06-08 16:22:53 +01002754 Label allocated, slow;
2755 __ LoadRoot(tmp4, Heap::kHeapNumberMapRootIndex);
2756 __ AllocateHeapNumber(tmp1, tmp2, tmp3, tmp4, &slow);
2757 __ b(&allocated);
Steve Block1e0659c2011-05-24 12:43:12 +01002758
Ben Murdoch8b112d22011-06-08 16:22:53 +01002759 // Slow case: Call the runtime system to do the number allocation.
2760 __ bind(&slow);
Steve Block1e0659c2011-05-24 12:43:12 +01002761
Ben Murdoch8b112d22011-06-08 16:22:53 +01002762 CallRuntimeFromDeferred(Runtime::kAllocateHeapNumber, 0, instr);
2763 // Set the pointer to the new heap number in tmp.
2764 if (!tmp1.is(r0)) __ mov(tmp1, Operand(r0));
2765 // Restore input_reg after call to runtime.
2766 __ LoadFromSafepointRegisterSlot(input, input);
2767 __ ldr(exponent, FieldMemOperand(input, HeapNumber::kExponentOffset));
Steve Block1e0659c2011-05-24 12:43:12 +01002768
Ben Murdoch8b112d22011-06-08 16:22:53 +01002769 __ bind(&allocated);
2770 // exponent: floating point exponent value.
2771 // tmp1: allocated heap number.
2772 __ bic(exponent, exponent, Operand(HeapNumber::kSignMask));
2773 __ str(exponent, FieldMemOperand(tmp1, HeapNumber::kExponentOffset));
2774 __ ldr(tmp2, FieldMemOperand(input, HeapNumber::kMantissaOffset));
2775 __ str(tmp2, FieldMemOperand(tmp1, HeapNumber::kMantissaOffset));
Steve Block1e0659c2011-05-24 12:43:12 +01002776
Ben Murdoch8b112d22011-06-08 16:22:53 +01002777 __ StoreToSafepointRegisterSlot(tmp1, input);
2778 }
Steve Block1e0659c2011-05-24 12:43:12 +01002779
2780 __ bind(&done);
2781}
2782
2783
2784void LCodeGen::EmitIntegerMathAbs(LUnaryMathOperation* instr) {
2785 Register input = ToRegister(instr->InputAt(0));
2786 __ cmp(input, Operand(0));
2787 // We can make rsb conditional because the previous cmp instruction
2788 // will clear the V (overflow) flag and rsb won't set this flag
2789 // if input is positive.
2790 __ rsb(input, input, Operand(0), SetCC, mi);
2791 // Deoptimize on overflow.
2792 DeoptimizeIf(vs, instr->environment());
Ben Murdochb0fe1622011-05-05 13:52:32 +01002793}
2794
2795
2796void LCodeGen::DoMathAbs(LUnaryMathOperation* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01002797 // Class for deferred case.
2798 class DeferredMathAbsTaggedHeapNumber: public LDeferredCode {
2799 public:
2800 DeferredMathAbsTaggedHeapNumber(LCodeGen* codegen,
2801 LUnaryMathOperation* instr)
2802 : LDeferredCode(codegen), instr_(instr) { }
2803 virtual void Generate() {
2804 codegen()->DoDeferredMathAbsTaggedHeapNumber(instr_);
2805 }
2806 private:
2807 LUnaryMathOperation* instr_;
2808 };
2809
2810 ASSERT(instr->InputAt(0)->Equals(instr->result()));
2811 Representation r = instr->hydrogen()->value()->representation();
2812 if (r.IsDouble()) {
2813 DwVfpRegister input = ToDoubleRegister(instr->InputAt(0));
2814 __ vabs(input, input);
2815 } else if (r.IsInteger32()) {
2816 EmitIntegerMathAbs(instr);
2817 } else {
2818 // Representation is tagged.
2819 DeferredMathAbsTaggedHeapNumber* deferred =
2820 new DeferredMathAbsTaggedHeapNumber(this, instr);
2821 Register input = ToRegister(instr->InputAt(0));
2822 // Smi check.
2823 __ JumpIfNotSmi(input, deferred->entry());
2824 // If smi, handle it directly.
2825 EmitIntegerMathAbs(instr);
2826 __ bind(deferred->exit());
2827 }
2828}
2829
2830
Ben Murdochb0fe1622011-05-05 13:52:32 +01002831void LCodeGen::DoMathFloor(LUnaryMathOperation* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01002832 DoubleRegister input = ToDoubleRegister(instr->InputAt(0));
Ben Murdochb8e0da22011-05-16 14:20:40 +01002833 Register result = ToRegister(instr->result());
Ben Murdochb8e0da22011-05-16 14:20:40 +01002834 SwVfpRegister single_scratch = double_scratch0().low();
Steve Block1e0659c2011-05-24 12:43:12 +01002835 Register scratch1 = scratch0();
2836 Register scratch2 = ToRegister(instr->TempAt(0));
Ben Murdochb8e0da22011-05-16 14:20:40 +01002837
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002838 __ EmitVFPTruncate(kRoundToMinusInf,
2839 single_scratch,
2840 input,
2841 scratch1,
2842 scratch2);
Ben Murdochb8e0da22011-05-16 14:20:40 +01002843 DeoptimizeIf(ne, instr->environment());
2844
2845 // Move the result back to general purpose register r0.
2846 __ vmov(result, single_scratch);
2847
Steve Block44f0eee2011-05-26 01:26:41 +01002848 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
2849 // Test for -0.
2850 Label done;
2851 __ cmp(result, Operand(0));
2852 __ b(ne, &done);
2853 __ vmov(scratch1, input.high());
2854 __ tst(scratch1, Operand(HeapNumber::kSignMask));
2855 DeoptimizeIf(ne, instr->environment());
2856 __ bind(&done);
2857 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01002858}
2859
2860
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002861void LCodeGen::DoMathRound(LUnaryMathOperation* instr) {
2862 DoubleRegister input = ToDoubleRegister(instr->InputAt(0));
2863 Register result = ToRegister(instr->result());
Steve Block053d10c2011-06-13 19:13:29 +01002864 Register scratch1 = scratch0();
2865 Register scratch2 = result;
2866 __ EmitVFPTruncate(kRoundToNearest,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002867 double_scratch0().low(),
2868 input,
2869 scratch1,
2870 scratch2);
2871 DeoptimizeIf(ne, instr->environment());
2872 __ vmov(result, double_scratch0().low());
2873
Steve Block44f0eee2011-05-26 01:26:41 +01002874 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
2875 // Test for -0.
Steve Block053d10c2011-06-13 19:13:29 +01002876 Label done;
Steve Block44f0eee2011-05-26 01:26:41 +01002877 __ cmp(result, Operand(0));
2878 __ b(ne, &done);
2879 __ vmov(scratch1, input.high());
2880 __ tst(scratch1, Operand(HeapNumber::kSignMask));
2881 DeoptimizeIf(ne, instr->environment());
Steve Block053d10c2011-06-13 19:13:29 +01002882 __ bind(&done);
Steve Block44f0eee2011-05-26 01:26:41 +01002883 }
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002884}
2885
2886
Ben Murdochb0fe1622011-05-05 13:52:32 +01002887void LCodeGen::DoMathSqrt(LUnaryMathOperation* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01002888 DoubleRegister input = ToDoubleRegister(instr->InputAt(0));
Ben Murdochb8e0da22011-05-16 14:20:40 +01002889 ASSERT(ToDoubleRegister(instr->result()).is(input));
2890 __ vsqrt(input, input);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002891}
2892
2893
Steve Block44f0eee2011-05-26 01:26:41 +01002894void LCodeGen::DoMathPowHalf(LUnaryMathOperation* instr) {
2895 DoubleRegister input = ToDoubleRegister(instr->InputAt(0));
2896 Register scratch = scratch0();
2897 SwVfpRegister single_scratch = double_scratch0().low();
2898 DoubleRegister double_scratch = double_scratch0();
2899 ASSERT(ToDoubleRegister(instr->result()).is(input));
2900
2901 // Add +0 to convert -0 to +0.
2902 __ mov(scratch, Operand(0));
2903 __ vmov(single_scratch, scratch);
2904 __ vcvt_f64_s32(double_scratch, single_scratch);
2905 __ vadd(input, input, double_scratch);
2906 __ vsqrt(input, input);
2907}
2908
2909
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002910void LCodeGen::DoPower(LPower* instr) {
2911 LOperand* left = instr->InputAt(0);
2912 LOperand* right = instr->InputAt(1);
2913 Register scratch = scratch0();
2914 DoubleRegister result_reg = ToDoubleRegister(instr->result());
2915 Representation exponent_type = instr->hydrogen()->right()->representation();
2916 if (exponent_type.IsDouble()) {
2917 // Prepare arguments and call C function.
2918 __ PrepareCallCFunction(4, scratch);
2919 __ vmov(r0, r1, ToDoubleRegister(left));
2920 __ vmov(r2, r3, ToDoubleRegister(right));
Steve Block44f0eee2011-05-26 01:26:41 +01002921 __ CallCFunction(
2922 ExternalReference::power_double_double_function(isolate()), 4);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002923 } else if (exponent_type.IsInteger32()) {
2924 ASSERT(ToRegister(right).is(r0));
2925 // Prepare arguments and call C function.
2926 __ PrepareCallCFunction(4, scratch);
2927 __ mov(r2, ToRegister(right));
2928 __ vmov(r0, r1, ToDoubleRegister(left));
Steve Block44f0eee2011-05-26 01:26:41 +01002929 __ CallCFunction(
2930 ExternalReference::power_double_int_function(isolate()), 4);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002931 } else {
2932 ASSERT(exponent_type.IsTagged());
2933 ASSERT(instr->hydrogen()->left()->representation().IsDouble());
2934
2935 Register right_reg = ToRegister(right);
2936
2937 // Check for smi on the right hand side.
2938 Label non_smi, call;
2939 __ JumpIfNotSmi(right_reg, &non_smi);
2940
2941 // Untag smi and convert it to a double.
2942 __ SmiUntag(right_reg);
2943 SwVfpRegister single_scratch = double_scratch0().low();
2944 __ vmov(single_scratch, right_reg);
2945 __ vcvt_f64_s32(result_reg, single_scratch);
2946 __ jmp(&call);
2947
2948 // Heap number map check.
2949 __ bind(&non_smi);
2950 __ ldr(scratch, FieldMemOperand(right_reg, HeapObject::kMapOffset));
2951 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
2952 __ cmp(scratch, Operand(ip));
2953 DeoptimizeIf(ne, instr->environment());
2954 int32_t value_offset = HeapNumber::kValueOffset - kHeapObjectTag;
2955 __ add(scratch, right_reg, Operand(value_offset));
2956 __ vldr(result_reg, scratch, 0);
2957
2958 // Prepare arguments and call C function.
2959 __ bind(&call);
2960 __ PrepareCallCFunction(4, scratch);
2961 __ vmov(r0, r1, ToDoubleRegister(left));
2962 __ vmov(r2, r3, result_reg);
Steve Block44f0eee2011-05-26 01:26:41 +01002963 __ CallCFunction(
2964 ExternalReference::power_double_double_function(isolate()), 4);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002965 }
2966 // Store the result in the result register.
2967 __ GetCFunctionDoubleResult(result_reg);
2968}
2969
2970
2971void LCodeGen::DoMathLog(LUnaryMathOperation* instr) {
2972 ASSERT(ToDoubleRegister(instr->result()).is(d2));
2973 TranscendentalCacheStub stub(TranscendentalCache::LOG,
2974 TranscendentalCacheStub::UNTAGGED);
2975 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
2976}
2977
2978
2979void LCodeGen::DoMathCos(LUnaryMathOperation* instr) {
2980 ASSERT(ToDoubleRegister(instr->result()).is(d2));
2981 TranscendentalCacheStub stub(TranscendentalCache::COS,
2982 TranscendentalCacheStub::UNTAGGED);
2983 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
2984}
2985
2986
2987void LCodeGen::DoMathSin(LUnaryMathOperation* instr) {
2988 ASSERT(ToDoubleRegister(instr->result()).is(d2));
2989 TranscendentalCacheStub stub(TranscendentalCache::SIN,
2990 TranscendentalCacheStub::UNTAGGED);
2991 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
2992}
2993
2994
Ben Murdochb0fe1622011-05-05 13:52:32 +01002995void LCodeGen::DoUnaryMathOperation(LUnaryMathOperation* instr) {
2996 switch (instr->op()) {
2997 case kMathAbs:
2998 DoMathAbs(instr);
2999 break;
3000 case kMathFloor:
3001 DoMathFloor(instr);
3002 break;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003003 case kMathRound:
3004 DoMathRound(instr);
3005 break;
Ben Murdochb0fe1622011-05-05 13:52:32 +01003006 case kMathSqrt:
3007 DoMathSqrt(instr);
3008 break;
Steve Block44f0eee2011-05-26 01:26:41 +01003009 case kMathPowHalf:
3010 DoMathPowHalf(instr);
3011 break;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003012 case kMathCos:
3013 DoMathCos(instr);
3014 break;
3015 case kMathSin:
3016 DoMathSin(instr);
3017 break;
3018 case kMathLog:
3019 DoMathLog(instr);
3020 break;
Ben Murdochb0fe1622011-05-05 13:52:32 +01003021 default:
3022 Abort("Unimplemented type of LUnaryMathOperation.");
3023 UNREACHABLE();
3024 }
3025}
3026
3027
3028void LCodeGen::DoCallKeyed(LCallKeyed* instr) {
Ben Murdoch086aeea2011-05-13 15:57:08 +01003029 ASSERT(ToRegister(instr->result()).is(r0));
3030
3031 int arity = instr->arity();
Steve Block44f0eee2011-05-26 01:26:41 +01003032 Handle<Code> ic =
3033 isolate()->stub_cache()->ComputeKeyedCallInitialize(arity, NOT_IN_LOOP);
Ben Murdoch086aeea2011-05-13 15:57:08 +01003034 CallCode(ic, RelocInfo::CODE_TARGET, instr);
3035 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
Ben Murdochb0fe1622011-05-05 13:52:32 +01003036}
3037
3038
3039void LCodeGen::DoCallNamed(LCallNamed* instr) {
3040 ASSERT(ToRegister(instr->result()).is(r0));
3041
3042 int arity = instr->arity();
Steve Block44f0eee2011-05-26 01:26:41 +01003043 Handle<Code> ic = isolate()->stub_cache()->ComputeCallInitialize(
3044 arity, NOT_IN_LOOP);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003045 __ mov(r2, Operand(instr->name()));
3046 CallCode(ic, RelocInfo::CODE_TARGET, instr);
3047 // Restore context register.
3048 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3049}
3050
3051
3052void LCodeGen::DoCallFunction(LCallFunction* instr) {
Steve Block9fac8402011-05-12 15:51:54 +01003053 ASSERT(ToRegister(instr->result()).is(r0));
3054
3055 int arity = instr->arity();
3056 CallFunctionStub stub(arity, NOT_IN_LOOP, RECEIVER_MIGHT_BE_VALUE);
3057 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
3058 __ Drop(1);
3059 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
Ben Murdochb0fe1622011-05-05 13:52:32 +01003060}
3061
3062
3063void LCodeGen::DoCallGlobal(LCallGlobal* instr) {
Ben Murdoch086aeea2011-05-13 15:57:08 +01003064 ASSERT(ToRegister(instr->result()).is(r0));
3065
3066 int arity = instr->arity();
Steve Block44f0eee2011-05-26 01:26:41 +01003067 Handle<Code> ic =
3068 isolate()->stub_cache()->ComputeCallInitialize(arity, NOT_IN_LOOP);
Ben Murdoch086aeea2011-05-13 15:57:08 +01003069 __ mov(r2, Operand(instr->name()));
3070 CallCode(ic, RelocInfo::CODE_TARGET_CONTEXT, instr);
3071 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
Ben Murdochb0fe1622011-05-05 13:52:32 +01003072}
3073
3074
3075void LCodeGen::DoCallKnownGlobal(LCallKnownGlobal* instr) {
3076 ASSERT(ToRegister(instr->result()).is(r0));
3077 __ mov(r1, Operand(instr->target()));
3078 CallKnownFunction(instr->target(), instr->arity(), instr);
3079}
3080
3081
3082void LCodeGen::DoCallNew(LCallNew* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01003083 ASSERT(ToRegister(instr->InputAt(0)).is(r1));
Ben Murdochb0fe1622011-05-05 13:52:32 +01003084 ASSERT(ToRegister(instr->result()).is(r0));
3085
Steve Block44f0eee2011-05-26 01:26:41 +01003086 Handle<Code> builtin = isolate()->builtins()->JSConstructCall();
Ben Murdochb0fe1622011-05-05 13:52:32 +01003087 __ mov(r0, Operand(instr->arity()));
3088 CallCode(builtin, RelocInfo::CONSTRUCT_CALL, instr);
3089}
3090
3091
3092void LCodeGen::DoCallRuntime(LCallRuntime* instr) {
3093 CallRuntime(instr->function(), instr->arity(), instr);
3094}
3095
3096
3097void LCodeGen::DoStoreNamedField(LStoreNamedField* instr) {
Ben Murdoch086aeea2011-05-13 15:57:08 +01003098 Register object = ToRegister(instr->object());
3099 Register value = ToRegister(instr->value());
3100 Register scratch = scratch0();
3101 int offset = instr->offset();
3102
3103 ASSERT(!object.is(value));
3104
3105 if (!instr->transition().is_null()) {
3106 __ mov(scratch, Operand(instr->transition()));
3107 __ str(scratch, FieldMemOperand(object, HeapObject::kMapOffset));
3108 }
3109
3110 // Do the store.
3111 if (instr->is_in_object()) {
3112 __ str(value, FieldMemOperand(object, offset));
3113 if (instr->needs_write_barrier()) {
3114 // Update the write barrier for the object for in-object properties.
3115 __ RecordWrite(object, Operand(offset), value, scratch);
3116 }
3117 } else {
3118 __ ldr(scratch, FieldMemOperand(object, JSObject::kPropertiesOffset));
3119 __ str(value, FieldMemOperand(scratch, offset));
3120 if (instr->needs_write_barrier()) {
3121 // Update the write barrier for the properties array.
3122 // object is used as a scratch register.
3123 __ RecordWrite(scratch, Operand(offset), value, object);
3124 }
3125 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01003126}
3127
3128
3129void LCodeGen::DoStoreNamedGeneric(LStoreNamedGeneric* instr) {
3130 ASSERT(ToRegister(instr->object()).is(r1));
3131 ASSERT(ToRegister(instr->value()).is(r0));
3132
3133 // Name is always in r2.
3134 __ mov(r2, Operand(instr->name()));
Ben Murdoch8b112d22011-06-08 16:22:53 +01003135 Handle<Code> ic = instr->strict_mode()
Steve Block44f0eee2011-05-26 01:26:41 +01003136 ? isolate()->builtins()->StoreIC_Initialize_Strict()
3137 : isolate()->builtins()->StoreIC_Initialize();
Ben Murdochb0fe1622011-05-05 13:52:32 +01003138 CallCode(ic, RelocInfo::CODE_TARGET, instr);
3139}
3140
3141
3142void LCodeGen::DoBoundsCheck(LBoundsCheck* instr) {
Ben Murdoch086aeea2011-05-13 15:57:08 +01003143 __ cmp(ToRegister(instr->index()), ToRegister(instr->length()));
Steve Block9fac8402011-05-12 15:51:54 +01003144 DeoptimizeIf(hs, instr->environment());
Ben Murdochb0fe1622011-05-05 13:52:32 +01003145}
3146
3147
3148void LCodeGen::DoStoreKeyedFastElement(LStoreKeyedFastElement* instr) {
Ben Murdoch086aeea2011-05-13 15:57:08 +01003149 Register value = ToRegister(instr->value());
3150 Register elements = ToRegister(instr->object());
3151 Register key = instr->key()->IsRegister() ? ToRegister(instr->key()) : no_reg;
3152 Register scratch = scratch0();
3153
3154 // Do the store.
3155 if (instr->key()->IsConstantOperand()) {
3156 ASSERT(!instr->hydrogen()->NeedsWriteBarrier());
3157 LConstantOperand* const_operand = LConstantOperand::cast(instr->key());
3158 int offset =
3159 ToInteger32(const_operand) * kPointerSize + FixedArray::kHeaderSize;
3160 __ str(value, FieldMemOperand(elements, offset));
3161 } else {
3162 __ add(scratch, elements, Operand(key, LSL, kPointerSizeLog2));
3163 __ str(value, FieldMemOperand(scratch, FixedArray::kHeaderSize));
3164 }
3165
3166 if (instr->hydrogen()->NeedsWriteBarrier()) {
3167 // Compute address of modified element and store it into key register.
3168 __ add(key, scratch, Operand(FixedArray::kHeaderSize));
3169 __ RecordWrite(elements, key, value);
3170 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01003171}
3172
3173
Steve Block44f0eee2011-05-26 01:26:41 +01003174void LCodeGen::DoStoreKeyedSpecializedArrayElement(
3175 LStoreKeyedSpecializedArrayElement* instr) {
Steve Block44f0eee2011-05-26 01:26:41 +01003176
3177 Register external_pointer = ToRegister(instr->external_pointer());
3178 Register key = ToRegister(instr->key());
Ben Murdoch8b112d22011-06-08 16:22:53 +01003179 ExternalArrayType array_type = instr->array_type();
3180 if (array_type == kExternalFloatArray) {
3181 CpuFeatures::Scope scope(VFP3);
3182 DwVfpRegister value(ToDoubleRegister(instr->value()));
3183 __ add(scratch0(), external_pointer, Operand(key, LSL, 2));
3184 __ vcvt_f32_f64(double_scratch0().low(), value);
3185 __ vstr(double_scratch0().low(), scratch0(), 0);
3186 } else {
3187 Register value(ToRegister(instr->value()));
3188 switch (array_type) {
3189 case kExternalPixelArray:
3190 // Clamp the value to [0..255].
3191 __ Usat(value, 8, Operand(value));
3192 __ strb(value, MemOperand(external_pointer, key));
3193 break;
3194 case kExternalByteArray:
3195 case kExternalUnsignedByteArray:
3196 __ strb(value, MemOperand(external_pointer, key));
3197 break;
3198 case kExternalShortArray:
3199 case kExternalUnsignedShortArray:
3200 __ strh(value, MemOperand(external_pointer, key, LSL, 1));
3201 break;
3202 case kExternalIntArray:
3203 case kExternalUnsignedIntArray:
3204 __ str(value, MemOperand(external_pointer, key, LSL, 2));
3205 break;
3206 case kExternalFloatArray:
3207 UNREACHABLE();
3208 break;
3209 }
3210 }
Steve Block44f0eee2011-05-26 01:26:41 +01003211}
3212
3213
Ben Murdochb0fe1622011-05-05 13:52:32 +01003214void LCodeGen::DoStoreKeyedGeneric(LStoreKeyedGeneric* instr) {
3215 ASSERT(ToRegister(instr->object()).is(r2));
3216 ASSERT(ToRegister(instr->key()).is(r1));
3217 ASSERT(ToRegister(instr->value()).is(r0));
3218
Ben Murdoch8b112d22011-06-08 16:22:53 +01003219 Handle<Code> ic = instr->strict_mode()
Steve Block44f0eee2011-05-26 01:26:41 +01003220 ? isolate()->builtins()->KeyedStoreIC_Initialize_Strict()
3221 : isolate()->builtins()->KeyedStoreIC_Initialize();
Ben Murdochb0fe1622011-05-05 13:52:32 +01003222 CallCode(ic, RelocInfo::CODE_TARGET, instr);
3223}
3224
3225
Steve Block1e0659c2011-05-24 12:43:12 +01003226void LCodeGen::DoStringCharCodeAt(LStringCharCodeAt* instr) {
3227 class DeferredStringCharCodeAt: public LDeferredCode {
3228 public:
3229 DeferredStringCharCodeAt(LCodeGen* codegen, LStringCharCodeAt* instr)
3230 : LDeferredCode(codegen), instr_(instr) { }
3231 virtual void Generate() { codegen()->DoDeferredStringCharCodeAt(instr_); }
3232 private:
3233 LStringCharCodeAt* instr_;
3234 };
3235
3236 Register scratch = scratch0();
3237 Register string = ToRegister(instr->string());
3238 Register index = no_reg;
3239 int const_index = -1;
3240 if (instr->index()->IsConstantOperand()) {
3241 const_index = ToInteger32(LConstantOperand::cast(instr->index()));
3242 STATIC_ASSERT(String::kMaxLength <= Smi::kMaxValue);
3243 if (!Smi::IsValid(const_index)) {
3244 // Guaranteed to be out of bounds because of the assert above.
3245 // So the bounds check that must dominate this instruction must
3246 // have deoptimized already.
3247 if (FLAG_debug_code) {
3248 __ Abort("StringCharCodeAt: out of bounds index.");
3249 }
3250 // No code needs to be generated.
3251 return;
3252 }
3253 } else {
3254 index = ToRegister(instr->index());
3255 }
3256 Register result = ToRegister(instr->result());
3257
3258 DeferredStringCharCodeAt* deferred =
3259 new DeferredStringCharCodeAt(this, instr);
3260
3261 Label flat_string, ascii_string, done;
3262
3263 // Fetch the instance type of the receiver into result register.
3264 __ ldr(result, FieldMemOperand(string, HeapObject::kMapOffset));
3265 __ ldrb(result, FieldMemOperand(result, Map::kInstanceTypeOffset));
3266
3267 // We need special handling for non-flat strings.
3268 STATIC_ASSERT(kSeqStringTag == 0);
3269 __ tst(result, Operand(kStringRepresentationMask));
3270 __ b(eq, &flat_string);
3271
3272 // Handle non-flat strings.
3273 __ tst(result, Operand(kIsConsStringMask));
3274 __ b(eq, deferred->entry());
3275
3276 // ConsString.
3277 // Check whether the right hand side is the empty string (i.e. if
3278 // this is really a flat string in a cons string). If that is not
3279 // the case we would rather go to the runtime system now to flatten
3280 // the string.
3281 __ ldr(scratch, FieldMemOperand(string, ConsString::kSecondOffset));
3282 __ LoadRoot(ip, Heap::kEmptyStringRootIndex);
3283 __ cmp(scratch, ip);
3284 __ b(ne, deferred->entry());
3285 // Get the first of the two strings and load its instance type.
3286 __ ldr(string, FieldMemOperand(string, ConsString::kFirstOffset));
3287 __ ldr(result, FieldMemOperand(string, HeapObject::kMapOffset));
3288 __ ldrb(result, FieldMemOperand(result, Map::kInstanceTypeOffset));
3289 // If the first cons component is also non-flat, then go to runtime.
3290 STATIC_ASSERT(kSeqStringTag == 0);
3291 __ tst(result, Operand(kStringRepresentationMask));
3292 __ b(ne, deferred->entry());
3293
3294 // Check for 1-byte or 2-byte string.
3295 __ bind(&flat_string);
3296 STATIC_ASSERT(kAsciiStringTag != 0);
3297 __ tst(result, Operand(kStringEncodingMask));
3298 __ b(ne, &ascii_string);
3299
3300 // 2-byte string.
3301 // Load the 2-byte character code into the result register.
3302 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
3303 if (instr->index()->IsConstantOperand()) {
3304 __ ldrh(result,
3305 FieldMemOperand(string,
3306 SeqTwoByteString::kHeaderSize + 2 * const_index));
3307 } else {
3308 __ add(scratch,
3309 string,
3310 Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
3311 __ ldrh(result, MemOperand(scratch, index, LSL, 1));
3312 }
3313 __ jmp(&done);
3314
3315 // ASCII string.
3316 // Load the byte into the result register.
3317 __ bind(&ascii_string);
3318 if (instr->index()->IsConstantOperand()) {
3319 __ ldrb(result, FieldMemOperand(string,
3320 SeqAsciiString::kHeaderSize + const_index));
3321 } else {
3322 __ add(scratch,
3323 string,
3324 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3325 __ ldrb(result, MemOperand(scratch, index));
3326 }
3327 __ bind(&done);
3328 __ bind(deferred->exit());
3329}
3330
3331
3332void LCodeGen::DoDeferredStringCharCodeAt(LStringCharCodeAt* instr) {
3333 Register string = ToRegister(instr->string());
3334 Register result = ToRegister(instr->result());
3335 Register scratch = scratch0();
3336
3337 // TODO(3095996): Get rid of this. For now, we need to make the
3338 // result register contain a valid pointer because it is already
3339 // contained in the register pointer map.
3340 __ mov(result, Operand(0));
3341
Ben Murdoch8b112d22011-06-08 16:22:53 +01003342 PushSafepointRegistersScope scope(this, Safepoint::kWithRegisters);
Steve Block1e0659c2011-05-24 12:43:12 +01003343 __ push(string);
3344 // Push the index as a smi. This is safe because of the checks in
3345 // DoStringCharCodeAt above.
3346 if (instr->index()->IsConstantOperand()) {
3347 int const_index = ToInteger32(LConstantOperand::cast(instr->index()));
3348 __ mov(scratch, Operand(Smi::FromInt(const_index)));
3349 __ push(scratch);
3350 } else {
3351 Register index = ToRegister(instr->index());
3352 __ SmiTag(index);
3353 __ push(index);
3354 }
Ben Murdoch8b112d22011-06-08 16:22:53 +01003355 CallRuntimeFromDeferred(Runtime::kStringCharCodeAt, 2, instr);
Steve Block1e0659c2011-05-24 12:43:12 +01003356 if (FLAG_debug_code) {
3357 __ AbortIfNotSmi(r0);
3358 }
3359 __ SmiUntag(r0);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003360 __ StoreToSafepointRegisterSlot(r0, result);
Steve Block1e0659c2011-05-24 12:43:12 +01003361}
3362
3363
Steve Block44f0eee2011-05-26 01:26:41 +01003364void LCodeGen::DoStringCharFromCode(LStringCharFromCode* instr) {
3365 class DeferredStringCharFromCode: public LDeferredCode {
3366 public:
3367 DeferredStringCharFromCode(LCodeGen* codegen, LStringCharFromCode* instr)
3368 : LDeferredCode(codegen), instr_(instr) { }
3369 virtual void Generate() { codegen()->DoDeferredStringCharFromCode(instr_); }
3370 private:
3371 LStringCharFromCode* instr_;
3372 };
3373
3374 DeferredStringCharFromCode* deferred =
3375 new DeferredStringCharFromCode(this, instr);
3376
3377 ASSERT(instr->hydrogen()->value()->representation().IsInteger32());
3378 Register char_code = ToRegister(instr->char_code());
3379 Register result = ToRegister(instr->result());
3380 ASSERT(!char_code.is(result));
3381
3382 __ cmp(char_code, Operand(String::kMaxAsciiCharCode));
3383 __ b(hi, deferred->entry());
3384 __ LoadRoot(result, Heap::kSingleCharacterStringCacheRootIndex);
3385 __ add(result, result, Operand(char_code, LSL, kPointerSizeLog2));
3386 __ ldr(result, FieldMemOperand(result, FixedArray::kHeaderSize));
3387 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
3388 __ cmp(result, ip);
3389 __ b(eq, deferred->entry());
3390 __ bind(deferred->exit());
3391}
3392
3393
3394void LCodeGen::DoDeferredStringCharFromCode(LStringCharFromCode* instr) {
3395 Register char_code = ToRegister(instr->char_code());
3396 Register result = ToRegister(instr->result());
3397
3398 // TODO(3095996): Get rid of this. For now, we need to make the
3399 // result register contain a valid pointer because it is already
3400 // contained in the register pointer map.
3401 __ mov(result, Operand(0));
3402
Ben Murdoch8b112d22011-06-08 16:22:53 +01003403 PushSafepointRegistersScope scope(this, Safepoint::kWithRegisters);
Steve Block44f0eee2011-05-26 01:26:41 +01003404 __ SmiTag(char_code);
3405 __ push(char_code);
Ben Murdoch8b112d22011-06-08 16:22:53 +01003406 CallRuntimeFromDeferred(Runtime::kCharFromCode, 1, instr);
Steve Block44f0eee2011-05-26 01:26:41 +01003407 __ StoreToSafepointRegisterSlot(r0, result);
Steve Block44f0eee2011-05-26 01:26:41 +01003408}
3409
3410
Steve Block1e0659c2011-05-24 12:43:12 +01003411void LCodeGen::DoStringLength(LStringLength* instr) {
3412 Register string = ToRegister(instr->InputAt(0));
3413 Register result = ToRegister(instr->result());
3414 __ ldr(result, FieldMemOperand(string, String::kLengthOffset));
3415}
3416
3417
Ben Murdochb0fe1622011-05-05 13:52:32 +01003418void LCodeGen::DoInteger32ToDouble(LInteger32ToDouble* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01003419 LOperand* input = instr->InputAt(0);
Ben Murdochb8e0da22011-05-16 14:20:40 +01003420 ASSERT(input->IsRegister() || input->IsStackSlot());
3421 LOperand* output = instr->result();
3422 ASSERT(output->IsDoubleRegister());
3423 SwVfpRegister single_scratch = double_scratch0().low();
3424 if (input->IsStackSlot()) {
3425 Register scratch = scratch0();
3426 __ ldr(scratch, ToMemOperand(input));
3427 __ vmov(single_scratch, scratch);
3428 } else {
3429 __ vmov(single_scratch, ToRegister(input));
3430 }
3431 __ vcvt_f64_s32(ToDoubleRegister(output), single_scratch);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003432}
3433
3434
3435void LCodeGen::DoNumberTagI(LNumberTagI* instr) {
3436 class DeferredNumberTagI: public LDeferredCode {
3437 public:
3438 DeferredNumberTagI(LCodeGen* codegen, LNumberTagI* instr)
3439 : LDeferredCode(codegen), instr_(instr) { }
3440 virtual void Generate() { codegen()->DoDeferredNumberTagI(instr_); }
3441 private:
3442 LNumberTagI* instr_;
3443 };
3444
Steve Block1e0659c2011-05-24 12:43:12 +01003445 LOperand* input = instr->InputAt(0);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003446 ASSERT(input->IsRegister() && input->Equals(instr->result()));
3447 Register reg = ToRegister(input);
3448
3449 DeferredNumberTagI* deferred = new DeferredNumberTagI(this, instr);
3450 __ SmiTag(reg, SetCC);
3451 __ b(vs, deferred->entry());
3452 __ bind(deferred->exit());
3453}
3454
3455
3456void LCodeGen::DoDeferredNumberTagI(LNumberTagI* instr) {
3457 Label slow;
Steve Block1e0659c2011-05-24 12:43:12 +01003458 Register reg = ToRegister(instr->InputAt(0));
Ben Murdochb0fe1622011-05-05 13:52:32 +01003459 DoubleRegister dbl_scratch = d0;
3460 SwVfpRegister flt_scratch = s0;
3461
3462 // Preserve the value of all registers.
Ben Murdoch8b112d22011-06-08 16:22:53 +01003463 PushSafepointRegistersScope scope(this, Safepoint::kWithRegisters);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003464
3465 // There was overflow, so bits 30 and 31 of the original integer
3466 // disagree. Try to allocate a heap number in new space and store
3467 // the value in there. If that fails, call the runtime system.
3468 Label done;
3469 __ SmiUntag(reg);
3470 __ eor(reg, reg, Operand(0x80000000));
3471 __ vmov(flt_scratch, reg);
3472 __ vcvt_f64_s32(dbl_scratch, flt_scratch);
3473 if (FLAG_inline_new) {
3474 __ LoadRoot(r6, Heap::kHeapNumberMapRootIndex);
3475 __ AllocateHeapNumber(r5, r3, r4, r6, &slow);
3476 if (!reg.is(r5)) __ mov(reg, r5);
3477 __ b(&done);
3478 }
3479
3480 // Slow case: Call the runtime system to do the number allocation.
3481 __ bind(&slow);
3482
3483 // TODO(3095996): Put a valid pointer value in the stack slot where the result
3484 // register is stored, as this register is in the pointer map, but contains an
3485 // integer value.
3486 __ mov(ip, Operand(0));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003487 __ StoreToSafepointRegisterSlot(ip, reg);
Ben Murdoch8b112d22011-06-08 16:22:53 +01003488 CallRuntimeFromDeferred(Runtime::kAllocateHeapNumber, 0, instr);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003489 if (!reg.is(r0)) __ mov(reg, r0);
3490
3491 // Done. Put the value in dbl_scratch into the value of the allocated heap
3492 // number.
3493 __ bind(&done);
3494 __ sub(ip, reg, Operand(kHeapObjectTag));
3495 __ vstr(dbl_scratch, ip, HeapNumber::kValueOffset);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003496 __ StoreToSafepointRegisterSlot(reg, reg);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003497}
3498
3499
3500void LCodeGen::DoNumberTagD(LNumberTagD* instr) {
3501 class DeferredNumberTagD: public LDeferredCode {
3502 public:
3503 DeferredNumberTagD(LCodeGen* codegen, LNumberTagD* instr)
3504 : LDeferredCode(codegen), instr_(instr) { }
3505 virtual void Generate() { codegen()->DoDeferredNumberTagD(instr_); }
3506 private:
3507 LNumberTagD* instr_;
3508 };
3509
Steve Block1e0659c2011-05-24 12:43:12 +01003510 DoubleRegister input_reg = ToDoubleRegister(instr->InputAt(0));
Steve Block9fac8402011-05-12 15:51:54 +01003511 Register scratch = scratch0();
Ben Murdochb0fe1622011-05-05 13:52:32 +01003512 Register reg = ToRegister(instr->result());
Steve Block1e0659c2011-05-24 12:43:12 +01003513 Register temp1 = ToRegister(instr->TempAt(0));
3514 Register temp2 = ToRegister(instr->TempAt(1));
Ben Murdochb0fe1622011-05-05 13:52:32 +01003515
3516 DeferredNumberTagD* deferred = new DeferredNumberTagD(this, instr);
3517 if (FLAG_inline_new) {
3518 __ LoadRoot(scratch, Heap::kHeapNumberMapRootIndex);
3519 __ AllocateHeapNumber(reg, temp1, temp2, scratch, deferred->entry());
3520 } else {
3521 __ jmp(deferred->entry());
3522 }
3523 __ bind(deferred->exit());
3524 __ sub(ip, reg, Operand(kHeapObjectTag));
3525 __ vstr(input_reg, ip, HeapNumber::kValueOffset);
3526}
3527
3528
3529void LCodeGen::DoDeferredNumberTagD(LNumberTagD* instr) {
3530 // TODO(3095996): Get rid of this. For now, we need to make the
3531 // result register contain a valid pointer because it is already
3532 // contained in the register pointer map.
3533 Register reg = ToRegister(instr->result());
3534 __ mov(reg, Operand(0));
3535
Ben Murdoch8b112d22011-06-08 16:22:53 +01003536 PushSafepointRegistersScope scope(this, Safepoint::kWithRegisters);
3537 CallRuntimeFromDeferred(Runtime::kAllocateHeapNumber, 0, instr);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003538 __ StoreToSafepointRegisterSlot(r0, reg);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003539}
3540
3541
3542void LCodeGen::DoSmiTag(LSmiTag* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01003543 LOperand* input = instr->InputAt(0);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003544 ASSERT(input->IsRegister() && input->Equals(instr->result()));
3545 ASSERT(!instr->hydrogen_value()->CheckFlag(HValue::kCanOverflow));
3546 __ SmiTag(ToRegister(input));
3547}
3548
3549
3550void LCodeGen::DoSmiUntag(LSmiUntag* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01003551 LOperand* input = instr->InputAt(0);
Ben Murdoch086aeea2011-05-13 15:57:08 +01003552 ASSERT(input->IsRegister() && input->Equals(instr->result()));
3553 if (instr->needs_check()) {
3554 __ tst(ToRegister(input), Operand(kSmiTagMask));
3555 DeoptimizeIf(ne, instr->environment());
3556 }
3557 __ SmiUntag(ToRegister(input));
Ben Murdochb0fe1622011-05-05 13:52:32 +01003558}
3559
3560
3561void LCodeGen::EmitNumberUntagD(Register input_reg,
3562 DoubleRegister result_reg,
3563 LEnvironment* env) {
Steve Block9fac8402011-05-12 15:51:54 +01003564 Register scratch = scratch0();
Ben Murdochb0fe1622011-05-05 13:52:32 +01003565 SwVfpRegister flt_scratch = s0;
3566 ASSERT(!result_reg.is(d0));
3567
3568 Label load_smi, heap_number, done;
3569
3570 // Smi check.
3571 __ tst(input_reg, Operand(kSmiTagMask));
3572 __ b(eq, &load_smi);
3573
3574 // Heap number map check.
Steve Block9fac8402011-05-12 15:51:54 +01003575 __ ldr(scratch, FieldMemOperand(input_reg, HeapObject::kMapOffset));
Ben Murdochb0fe1622011-05-05 13:52:32 +01003576 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
Steve Block9fac8402011-05-12 15:51:54 +01003577 __ cmp(scratch, Operand(ip));
Ben Murdochb0fe1622011-05-05 13:52:32 +01003578 __ b(eq, &heap_number);
3579
3580 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
3581 __ cmp(input_reg, Operand(ip));
3582 DeoptimizeIf(ne, env);
3583
3584 // Convert undefined to NaN.
3585 __ LoadRoot(ip, Heap::kNanValueRootIndex);
3586 __ sub(ip, ip, Operand(kHeapObjectTag));
3587 __ vldr(result_reg, ip, HeapNumber::kValueOffset);
3588 __ jmp(&done);
3589
3590 // Heap number to double register conversion.
3591 __ bind(&heap_number);
3592 __ sub(ip, input_reg, Operand(kHeapObjectTag));
3593 __ vldr(result_reg, ip, HeapNumber::kValueOffset);
3594 __ jmp(&done);
3595
3596 // Smi to double register conversion
3597 __ bind(&load_smi);
3598 __ SmiUntag(input_reg); // Untag smi before converting to float.
3599 __ vmov(flt_scratch, input_reg);
3600 __ vcvt_f64_s32(result_reg, flt_scratch);
3601 __ SmiTag(input_reg); // Retag smi.
3602 __ bind(&done);
3603}
3604
3605
3606class DeferredTaggedToI: public LDeferredCode {
3607 public:
3608 DeferredTaggedToI(LCodeGen* codegen, LTaggedToI* instr)
3609 : LDeferredCode(codegen), instr_(instr) { }
3610 virtual void Generate() { codegen()->DoDeferredTaggedToI(instr_); }
3611 private:
3612 LTaggedToI* instr_;
3613};
3614
3615
3616void LCodeGen::DoDeferredTaggedToI(LTaggedToI* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01003617 Register input_reg = ToRegister(instr->InputAt(0));
Steve Block44f0eee2011-05-26 01:26:41 +01003618 Register scratch1 = scratch0();
3619 Register scratch2 = ToRegister(instr->TempAt(0));
3620 DwVfpRegister double_scratch = double_scratch0();
3621 SwVfpRegister single_scratch = double_scratch.low();
3622
3623 ASSERT(!scratch1.is(input_reg) && !scratch1.is(scratch2));
3624 ASSERT(!scratch2.is(input_reg) && !scratch2.is(scratch1));
3625
3626 Label done;
Ben Murdochb0fe1622011-05-05 13:52:32 +01003627
3628 // Heap number map check.
Steve Block44f0eee2011-05-26 01:26:41 +01003629 __ ldr(scratch1, FieldMemOperand(input_reg, HeapObject::kMapOffset));
Ben Murdochb0fe1622011-05-05 13:52:32 +01003630 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
Steve Block44f0eee2011-05-26 01:26:41 +01003631 __ cmp(scratch1, Operand(ip));
Ben Murdochb0fe1622011-05-05 13:52:32 +01003632
3633 if (instr->truncating()) {
Steve Block44f0eee2011-05-26 01:26:41 +01003634 Register scratch3 = ToRegister(instr->TempAt(1));
3635 DwVfpRegister double_scratch2 = ToDoubleRegister(instr->TempAt(2));
3636 ASSERT(!scratch3.is(input_reg) &&
3637 !scratch3.is(scratch1) &&
3638 !scratch3.is(scratch2));
3639 // Performs a truncating conversion of a floating point number as used by
3640 // the JS bitwise operations.
Ben Murdochb0fe1622011-05-05 13:52:32 +01003641 Label heap_number;
3642 __ b(eq, &heap_number);
3643 // Check for undefined. Undefined is converted to zero for truncating
3644 // conversions.
3645 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
3646 __ cmp(input_reg, Operand(ip));
3647 DeoptimizeIf(ne, instr->environment());
3648 __ mov(input_reg, Operand(0));
3649 __ b(&done);
3650
3651 __ bind(&heap_number);
Steve Block44f0eee2011-05-26 01:26:41 +01003652 __ sub(scratch1, input_reg, Operand(kHeapObjectTag));
3653 __ vldr(double_scratch2, scratch1, HeapNumber::kValueOffset);
3654
3655 __ EmitECMATruncate(input_reg,
3656 double_scratch2,
3657 single_scratch,
3658 scratch1,
3659 scratch2,
3660 scratch3);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003661
3662 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01003663 CpuFeatures::Scope scope(VFP3);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003664 // Deoptimize if we don't have a heap number.
3665 DeoptimizeIf(ne, instr->environment());
3666
3667 __ sub(ip, input_reg, Operand(kHeapObjectTag));
Steve Block44f0eee2011-05-26 01:26:41 +01003668 __ vldr(double_scratch, ip, HeapNumber::kValueOffset);
3669 __ EmitVFPTruncate(kRoundToZero,
3670 single_scratch,
3671 double_scratch,
3672 scratch1,
3673 scratch2,
3674 kCheckForInexactConversion);
3675 DeoptimizeIf(ne, instr->environment());
3676 // Load the result.
3677 __ vmov(input_reg, single_scratch);
3678
Ben Murdochb0fe1622011-05-05 13:52:32 +01003679 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
Steve Block44f0eee2011-05-26 01:26:41 +01003680 __ cmp(input_reg, Operand(0));
Ben Murdochb0fe1622011-05-05 13:52:32 +01003681 __ b(ne, &done);
Steve Block44f0eee2011-05-26 01:26:41 +01003682 __ vmov(scratch1, double_scratch.high());
3683 __ tst(scratch1, Operand(HeapNumber::kSignMask));
Ben Murdochb0fe1622011-05-05 13:52:32 +01003684 DeoptimizeIf(ne, instr->environment());
3685 }
3686 }
3687 __ bind(&done);
3688}
3689
3690
3691void LCodeGen::DoTaggedToI(LTaggedToI* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01003692 LOperand* input = instr->InputAt(0);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003693 ASSERT(input->IsRegister());
3694 ASSERT(input->Equals(instr->result()));
3695
3696 Register input_reg = ToRegister(input);
3697
3698 DeferredTaggedToI* deferred = new DeferredTaggedToI(this, instr);
3699
3700 // Smi check.
3701 __ tst(input_reg, Operand(kSmiTagMask));
3702 __ b(ne, deferred->entry());
3703
3704 // Smi to int32 conversion
3705 __ SmiUntag(input_reg); // Untag smi.
3706
3707 __ bind(deferred->exit());
3708}
3709
3710
3711void LCodeGen::DoNumberUntagD(LNumberUntagD* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01003712 LOperand* input = instr->InputAt(0);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003713 ASSERT(input->IsRegister());
3714 LOperand* result = instr->result();
3715 ASSERT(result->IsDoubleRegister());
3716
3717 Register input_reg = ToRegister(input);
3718 DoubleRegister result_reg = ToDoubleRegister(result);
3719
3720 EmitNumberUntagD(input_reg, result_reg, instr->environment());
3721}
3722
3723
3724void LCodeGen::DoDoubleToI(LDoubleToI* instr) {
Steve Block44f0eee2011-05-26 01:26:41 +01003725 Register result_reg = ToRegister(instr->result());
Steve Block1e0659c2011-05-24 12:43:12 +01003726 Register scratch1 = scratch0();
3727 Register scratch2 = ToRegister(instr->TempAt(0));
Steve Block44f0eee2011-05-26 01:26:41 +01003728 DwVfpRegister double_input = ToDoubleRegister(instr->InputAt(0));
3729 DwVfpRegister double_scratch = double_scratch0();
3730 SwVfpRegister single_scratch = double_scratch0().low();
Steve Block1e0659c2011-05-24 12:43:12 +01003731
Steve Block44f0eee2011-05-26 01:26:41 +01003732 Label done;
Steve Block1e0659c2011-05-24 12:43:12 +01003733
Steve Block44f0eee2011-05-26 01:26:41 +01003734 if (instr->truncating()) {
3735 Register scratch3 = ToRegister(instr->TempAt(1));
3736 __ EmitECMATruncate(result_reg,
3737 double_input,
3738 single_scratch,
3739 scratch1,
3740 scratch2,
3741 scratch3);
3742 } else {
3743 VFPRoundingMode rounding_mode = kRoundToMinusInf;
3744 __ EmitVFPTruncate(rounding_mode,
3745 single_scratch,
3746 double_input,
3747 scratch1,
3748 scratch2,
3749 kCheckForInexactConversion);
3750 // Deoptimize if we had a vfp invalid exception,
3751 // including inexact operation.
Steve Block1e0659c2011-05-24 12:43:12 +01003752 DeoptimizeIf(ne, instr->environment());
Steve Block44f0eee2011-05-26 01:26:41 +01003753 // Retrieve the result.
3754 __ vmov(result_reg, single_scratch);
Steve Block1e0659c2011-05-24 12:43:12 +01003755 }
Steve Block44f0eee2011-05-26 01:26:41 +01003756 __ bind(&done);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003757}
3758
3759
3760void LCodeGen::DoCheckSmi(LCheckSmi* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01003761 LOperand* input = instr->InputAt(0);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003762 __ tst(ToRegister(input), Operand(kSmiTagMask));
Steve Block44f0eee2011-05-26 01:26:41 +01003763 DeoptimizeIf(ne, instr->environment());
3764}
3765
3766
3767void LCodeGen::DoCheckNonSmi(LCheckNonSmi* instr) {
3768 LOperand* input = instr->InputAt(0);
3769 __ tst(ToRegister(input), Operand(kSmiTagMask));
3770 DeoptimizeIf(eq, instr->environment());
Ben Murdochb0fe1622011-05-05 13:52:32 +01003771}
3772
3773
3774void LCodeGen::DoCheckInstanceType(LCheckInstanceType* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01003775 Register input = ToRegister(instr->InputAt(0));
Ben Murdoch086aeea2011-05-13 15:57:08 +01003776 Register scratch = scratch0();
3777 InstanceType first = instr->hydrogen()->first();
3778 InstanceType last = instr->hydrogen()->last();
3779
3780 __ ldr(scratch, FieldMemOperand(input, HeapObject::kMapOffset));
3781 __ ldrb(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
3782 __ cmp(scratch, Operand(first));
3783
3784 // If there is only one type in the interval check for equality.
3785 if (first == last) {
3786 DeoptimizeIf(ne, instr->environment());
3787 } else {
3788 DeoptimizeIf(lo, instr->environment());
3789 // Omit check for the last type.
3790 if (last != LAST_TYPE) {
3791 __ cmp(scratch, Operand(last));
3792 DeoptimizeIf(hi, instr->environment());
3793 }
3794 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01003795}
3796
3797
3798void LCodeGen::DoCheckFunction(LCheckFunction* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01003799 ASSERT(instr->InputAt(0)->IsRegister());
3800 Register reg = ToRegister(instr->InputAt(0));
Ben Murdochb0fe1622011-05-05 13:52:32 +01003801 __ cmp(reg, Operand(instr->hydrogen()->target()));
3802 DeoptimizeIf(ne, instr->environment());
3803}
3804
3805
3806void LCodeGen::DoCheckMap(LCheckMap* instr) {
Steve Block9fac8402011-05-12 15:51:54 +01003807 Register scratch = scratch0();
Steve Block1e0659c2011-05-24 12:43:12 +01003808 LOperand* input = instr->InputAt(0);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003809 ASSERT(input->IsRegister());
3810 Register reg = ToRegister(input);
Steve Block9fac8402011-05-12 15:51:54 +01003811 __ ldr(scratch, FieldMemOperand(reg, HeapObject::kMapOffset));
3812 __ cmp(scratch, Operand(instr->hydrogen()->map()));
Ben Murdochb0fe1622011-05-05 13:52:32 +01003813 DeoptimizeIf(ne, instr->environment());
3814}
3815
3816
Ben Murdochb8e0da22011-05-16 14:20:40 +01003817void LCodeGen::LoadHeapObject(Register result,
3818 Handle<HeapObject> object) {
Steve Block44f0eee2011-05-26 01:26:41 +01003819 if (heap()->InNewSpace(*object)) {
Steve Block9fac8402011-05-12 15:51:54 +01003820 Handle<JSGlobalPropertyCell> cell =
Steve Block44f0eee2011-05-26 01:26:41 +01003821 factory()->NewJSGlobalPropertyCell(object);
Steve Block9fac8402011-05-12 15:51:54 +01003822 __ mov(result, Operand(cell));
Ben Murdochb8e0da22011-05-16 14:20:40 +01003823 __ ldr(result, FieldMemOperand(result, JSGlobalPropertyCell::kValueOffset));
Steve Block9fac8402011-05-12 15:51:54 +01003824 } else {
Ben Murdochb8e0da22011-05-16 14:20:40 +01003825 __ mov(result, Operand(object));
Steve Block9fac8402011-05-12 15:51:54 +01003826 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01003827}
3828
3829
3830void LCodeGen::DoCheckPrototypeMaps(LCheckPrototypeMaps* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01003831 Register temp1 = ToRegister(instr->TempAt(0));
3832 Register temp2 = ToRegister(instr->TempAt(1));
Steve Block9fac8402011-05-12 15:51:54 +01003833
3834 Handle<JSObject> holder = instr->holder();
Ben Murdochb8e0da22011-05-16 14:20:40 +01003835 Handle<JSObject> current_prototype = instr->prototype();
Steve Block9fac8402011-05-12 15:51:54 +01003836
3837 // Load prototype object.
Ben Murdochb8e0da22011-05-16 14:20:40 +01003838 LoadHeapObject(temp1, current_prototype);
Steve Block9fac8402011-05-12 15:51:54 +01003839
3840 // Check prototype maps up to the holder.
3841 while (!current_prototype.is_identical_to(holder)) {
3842 __ ldr(temp2, FieldMemOperand(temp1, HeapObject::kMapOffset));
3843 __ cmp(temp2, Operand(Handle<Map>(current_prototype->map())));
3844 DeoptimizeIf(ne, instr->environment());
3845 current_prototype =
3846 Handle<JSObject>(JSObject::cast(current_prototype->GetPrototype()));
3847 // Load next prototype object.
Ben Murdochb8e0da22011-05-16 14:20:40 +01003848 LoadHeapObject(temp1, current_prototype);
Steve Block9fac8402011-05-12 15:51:54 +01003849 }
3850
3851 // Check the holder map.
3852 __ ldr(temp2, FieldMemOperand(temp1, HeapObject::kMapOffset));
3853 __ cmp(temp2, Operand(Handle<Map>(current_prototype->map())));
3854 DeoptimizeIf(ne, instr->environment());
Ben Murdochb0fe1622011-05-05 13:52:32 +01003855}
3856
3857
3858void LCodeGen::DoArrayLiteral(LArrayLiteral* instr) {
Steve Block9fac8402011-05-12 15:51:54 +01003859 __ ldr(r3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
3860 __ ldr(r3, FieldMemOperand(r3, JSFunction::kLiteralsOffset));
3861 __ mov(r2, Operand(Smi::FromInt(instr->hydrogen()->literal_index())));
3862 __ mov(r1, Operand(instr->hydrogen()->constant_elements()));
3863 __ Push(r3, r2, r1);
3864
3865 // Pick the right runtime function or stub to call.
3866 int length = instr->hydrogen()->length();
3867 if (instr->hydrogen()->IsCopyOnWrite()) {
3868 ASSERT(instr->hydrogen()->depth() == 1);
3869 FastCloneShallowArrayStub::Mode mode =
3870 FastCloneShallowArrayStub::COPY_ON_WRITE_ELEMENTS;
3871 FastCloneShallowArrayStub stub(mode, length);
3872 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
3873 } else if (instr->hydrogen()->depth() > 1) {
3874 CallRuntime(Runtime::kCreateArrayLiteral, 3, instr);
3875 } else if (length > FastCloneShallowArrayStub::kMaximumClonedLength) {
3876 CallRuntime(Runtime::kCreateArrayLiteralShallow, 3, instr);
3877 } else {
3878 FastCloneShallowArrayStub::Mode mode =
3879 FastCloneShallowArrayStub::CLONE_ELEMENTS;
3880 FastCloneShallowArrayStub stub(mode, length);
3881 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
3882 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01003883}
3884
3885
3886void LCodeGen::DoObjectLiteral(LObjectLiteral* instr) {
Steve Block9fac8402011-05-12 15:51:54 +01003887 __ ldr(r4, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
3888 __ ldr(r4, FieldMemOperand(r4, JSFunction::kLiteralsOffset));
3889 __ mov(r3, Operand(Smi::FromInt(instr->hydrogen()->literal_index())));
3890 __ mov(r2, Operand(instr->hydrogen()->constant_properties()));
3891 __ mov(r1, Operand(Smi::FromInt(instr->hydrogen()->fast_elements() ? 1 : 0)));
3892 __ Push(r4, r3, r2, r1);
3893
3894 // Pick the right runtime function to call.
3895 if (instr->hydrogen()->depth() > 1) {
3896 CallRuntime(Runtime::kCreateObjectLiteral, 4, instr);
3897 } else {
3898 CallRuntime(Runtime::kCreateObjectLiteralShallow, 4, instr);
3899 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01003900}
3901
3902
Steve Block44f0eee2011-05-26 01:26:41 +01003903void LCodeGen::DoToFastProperties(LToFastProperties* instr) {
3904 ASSERT(ToRegister(instr->InputAt(0)).is(r0));
3905 __ push(r0);
3906 CallRuntime(Runtime::kToFastProperties, 1, instr);
3907}
3908
3909
Ben Murdochb0fe1622011-05-05 13:52:32 +01003910void LCodeGen::DoRegExpLiteral(LRegExpLiteral* instr) {
Ben Murdoch086aeea2011-05-13 15:57:08 +01003911 Label materialized;
3912 // Registers will be used as follows:
3913 // r3 = JS function.
3914 // r7 = literals array.
3915 // r1 = regexp literal.
3916 // r0 = regexp literal clone.
3917 // r2 and r4-r6 are used as temporaries.
3918 __ ldr(r3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
3919 __ ldr(r7, FieldMemOperand(r3, JSFunction::kLiteralsOffset));
3920 int literal_offset = FixedArray::kHeaderSize +
3921 instr->hydrogen()->literal_index() * kPointerSize;
3922 __ ldr(r1, FieldMemOperand(r7, literal_offset));
3923 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
3924 __ cmp(r1, ip);
3925 __ b(ne, &materialized);
3926
3927 // Create regexp literal using runtime function
3928 // Result will be in r0.
3929 __ mov(r6, Operand(Smi::FromInt(instr->hydrogen()->literal_index())));
3930 __ mov(r5, Operand(instr->hydrogen()->pattern()));
3931 __ mov(r4, Operand(instr->hydrogen()->flags()));
3932 __ Push(r7, r6, r5, r4);
3933 CallRuntime(Runtime::kMaterializeRegExpLiteral, 4, instr);
3934 __ mov(r1, r0);
3935
3936 __ bind(&materialized);
3937 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
3938 Label allocated, runtime_allocate;
3939
3940 __ AllocateInNewSpace(size, r0, r2, r3, &runtime_allocate, TAG_OBJECT);
3941 __ jmp(&allocated);
3942
3943 __ bind(&runtime_allocate);
3944 __ mov(r0, Operand(Smi::FromInt(size)));
3945 __ Push(r1, r0);
3946 CallRuntime(Runtime::kAllocateInNewSpace, 1, instr);
3947 __ pop(r1);
3948
3949 __ bind(&allocated);
3950 // Copy the content into the newly allocated memory.
3951 // (Unroll copy loop once for better throughput).
3952 for (int i = 0; i < size - kPointerSize; i += 2 * kPointerSize) {
3953 __ ldr(r3, FieldMemOperand(r1, i));
3954 __ ldr(r2, FieldMemOperand(r1, i + kPointerSize));
3955 __ str(r3, FieldMemOperand(r0, i));
3956 __ str(r2, FieldMemOperand(r0, i + kPointerSize));
3957 }
3958 if ((size % (2 * kPointerSize)) != 0) {
3959 __ ldr(r3, FieldMemOperand(r1, size - kPointerSize));
3960 __ str(r3, FieldMemOperand(r0, size - kPointerSize));
3961 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01003962}
3963
3964
3965void LCodeGen::DoFunctionLiteral(LFunctionLiteral* instr) {
Ben Murdoch086aeea2011-05-13 15:57:08 +01003966 // Use the fast case closure allocation code that allocates in new
3967 // space for nested functions that don't need literals cloning.
3968 Handle<SharedFunctionInfo> shared_info = instr->shared_info();
Steve Block1e0659c2011-05-24 12:43:12 +01003969 bool pretenure = instr->hydrogen()->pretenure();
Steve Block44f0eee2011-05-26 01:26:41 +01003970 if (!pretenure && shared_info->num_literals() == 0) {
3971 FastNewClosureStub stub(
3972 shared_info->strict_mode() ? kStrictMode : kNonStrictMode);
Ben Murdoch086aeea2011-05-13 15:57:08 +01003973 __ mov(r1, Operand(shared_info));
3974 __ push(r1);
3975 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
3976 } else {
3977 __ mov(r2, Operand(shared_info));
3978 __ mov(r1, Operand(pretenure
Steve Block44f0eee2011-05-26 01:26:41 +01003979 ? factory()->true_value()
3980 : factory()->false_value()));
Ben Murdoch086aeea2011-05-13 15:57:08 +01003981 __ Push(cp, r2, r1);
3982 CallRuntime(Runtime::kNewClosure, 3, instr);
3983 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01003984}
3985
3986
3987void LCodeGen::DoTypeof(LTypeof* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01003988 Register input = ToRegister(instr->InputAt(0));
Ben Murdoch086aeea2011-05-13 15:57:08 +01003989 __ push(input);
3990 CallRuntime(Runtime::kTypeof, 1, instr);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003991}
3992
3993
3994void LCodeGen::DoTypeofIs(LTypeofIs* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01003995 Register input = ToRegister(instr->InputAt(0));
Ben Murdoch086aeea2011-05-13 15:57:08 +01003996 Register result = ToRegister(instr->result());
3997 Label true_label;
3998 Label false_label;
3999 Label done;
4000
4001 Condition final_branch_condition = EmitTypeofIs(&true_label,
4002 &false_label,
4003 input,
4004 instr->type_literal());
4005 __ b(final_branch_condition, &true_label);
4006 __ bind(&false_label);
4007 __ LoadRoot(result, Heap::kFalseValueRootIndex);
4008 __ b(&done);
4009
4010 __ bind(&true_label);
4011 __ LoadRoot(result, Heap::kTrueValueRootIndex);
4012
4013 __ bind(&done);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004014}
4015
4016
4017void LCodeGen::DoTypeofIsAndBranch(LTypeofIsAndBranch* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01004018 Register input = ToRegister(instr->InputAt(0));
Ben Murdochb0fe1622011-05-05 13:52:32 +01004019 int true_block = chunk_->LookupDestination(instr->true_block_id());
4020 int false_block = chunk_->LookupDestination(instr->false_block_id());
4021 Label* true_label = chunk_->GetAssemblyLabel(true_block);
4022 Label* false_label = chunk_->GetAssemblyLabel(false_block);
4023
4024 Condition final_branch_condition = EmitTypeofIs(true_label,
4025 false_label,
4026 input,
4027 instr->type_literal());
4028
4029 EmitBranch(true_block, false_block, final_branch_condition);
4030}
4031
4032
4033Condition LCodeGen::EmitTypeofIs(Label* true_label,
4034 Label* false_label,
4035 Register input,
4036 Handle<String> type_name) {
Steve Block1e0659c2011-05-24 12:43:12 +01004037 Condition final_branch_condition = kNoCondition;
Steve Block9fac8402011-05-12 15:51:54 +01004038 Register scratch = scratch0();
Steve Block44f0eee2011-05-26 01:26:41 +01004039 if (type_name->Equals(heap()->number_symbol())) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004040 __ JumpIfSmi(input, true_label);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004041 __ ldr(input, FieldMemOperand(input, HeapObject::kMapOffset));
4042 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
4043 __ cmp(input, Operand(ip));
4044 final_branch_condition = eq;
4045
Steve Block44f0eee2011-05-26 01:26:41 +01004046 } else if (type_name->Equals(heap()->string_symbol())) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004047 __ JumpIfSmi(input, false_label);
4048 __ CompareObjectType(input, input, scratch, FIRST_NONSTRING_TYPE);
4049 __ b(ge, false_label);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004050 __ ldrb(ip, FieldMemOperand(input, Map::kBitFieldOffset));
4051 __ tst(ip, Operand(1 << Map::kIsUndetectable));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004052 final_branch_condition = eq;
Ben Murdochb0fe1622011-05-05 13:52:32 +01004053
Steve Block44f0eee2011-05-26 01:26:41 +01004054 } else if (type_name->Equals(heap()->boolean_symbol())) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004055 __ CompareRoot(input, Heap::kTrueValueRootIndex);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004056 __ b(eq, true_label);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004057 __ CompareRoot(input, Heap::kFalseValueRootIndex);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004058 final_branch_condition = eq;
4059
Steve Block44f0eee2011-05-26 01:26:41 +01004060 } else if (type_name->Equals(heap()->undefined_symbol())) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004061 __ CompareRoot(input, Heap::kUndefinedValueRootIndex);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004062 __ b(eq, true_label);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004063 __ JumpIfSmi(input, false_label);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004064 // Check for undetectable objects => true.
4065 __ ldr(input, FieldMemOperand(input, HeapObject::kMapOffset));
4066 __ ldrb(ip, FieldMemOperand(input, Map::kBitFieldOffset));
4067 __ tst(ip, Operand(1 << Map::kIsUndetectable));
4068 final_branch_condition = ne;
4069
Steve Block44f0eee2011-05-26 01:26:41 +01004070 } else if (type_name->Equals(heap()->function_symbol())) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004071 __ JumpIfSmi(input, false_label);
4072 __ CompareObjectType(input, input, scratch, FIRST_FUNCTION_CLASS_TYPE);
4073 final_branch_condition = ge;
Ben Murdochb0fe1622011-05-05 13:52:32 +01004074
Steve Block44f0eee2011-05-26 01:26:41 +01004075 } else if (type_name->Equals(heap()->object_symbol())) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004076 __ JumpIfSmi(input, false_label);
4077 __ CompareRoot(input, Heap::kNullValueRootIndex);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004078 __ b(eq, true_label);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004079 __ CompareObjectType(input, input, scratch, FIRST_JS_OBJECT_TYPE);
4080 __ b(lo, false_label);
4081 __ CompareInstanceType(input, scratch, FIRST_FUNCTION_CLASS_TYPE);
4082 __ b(hs, false_label);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004083 // Check for undetectable objects => false.
4084 __ ldrb(ip, FieldMemOperand(input, Map::kBitFieldOffset));
4085 __ tst(ip, Operand(1 << Map::kIsUndetectable));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004086 final_branch_condition = eq;
Ben Murdochb0fe1622011-05-05 13:52:32 +01004087
4088 } else {
4089 final_branch_condition = ne;
4090 __ b(false_label);
4091 // A dead branch instruction will be generated after this point.
4092 }
4093
4094 return final_branch_condition;
4095}
4096
4097
Steve Block1e0659c2011-05-24 12:43:12 +01004098void LCodeGen::DoIsConstructCall(LIsConstructCall* instr) {
4099 Register result = ToRegister(instr->result());
4100 Label true_label;
4101 Label false_label;
4102 Label done;
4103
4104 EmitIsConstructCall(result, scratch0());
4105 __ b(eq, &true_label);
4106
4107 __ LoadRoot(result, Heap::kFalseValueRootIndex);
4108 __ b(&done);
4109
4110
4111 __ bind(&true_label);
4112 __ LoadRoot(result, Heap::kTrueValueRootIndex);
4113
4114 __ bind(&done);
4115}
4116
4117
4118void LCodeGen::DoIsConstructCallAndBranch(LIsConstructCallAndBranch* instr) {
4119 Register temp1 = ToRegister(instr->TempAt(0));
4120 int true_block = chunk_->LookupDestination(instr->true_block_id());
4121 int false_block = chunk_->LookupDestination(instr->false_block_id());
4122
4123 EmitIsConstructCall(temp1, scratch0());
4124 EmitBranch(true_block, false_block, eq);
4125}
4126
4127
4128void LCodeGen::EmitIsConstructCall(Register temp1, Register temp2) {
4129 ASSERT(!temp1.is(temp2));
4130 // Get the frame pointer for the calling frame.
4131 __ ldr(temp1, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
4132
4133 // Skip the arguments adaptor frame if it exists.
4134 Label check_frame_marker;
4135 __ ldr(temp2, MemOperand(temp1, StandardFrameConstants::kContextOffset));
4136 __ cmp(temp2, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
4137 __ b(ne, &check_frame_marker);
4138 __ ldr(temp1, MemOperand(temp1, StandardFrameConstants::kCallerFPOffset));
4139
4140 // Check the marker in the calling frame.
4141 __ bind(&check_frame_marker);
4142 __ ldr(temp1, MemOperand(temp1, StandardFrameConstants::kMarkerOffset));
4143 __ cmp(temp1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
4144}
4145
4146
Ben Murdochb0fe1622011-05-05 13:52:32 +01004147void LCodeGen::DoLazyBailout(LLazyBailout* instr) {
4148 // No code for lazy bailout instruction. Used to capture environment after a
4149 // call for populating the safepoint data with deoptimization data.
4150}
4151
4152
4153void LCodeGen::DoDeoptimize(LDeoptimize* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01004154 DeoptimizeIf(al, instr->environment());
Ben Murdochb0fe1622011-05-05 13:52:32 +01004155}
4156
4157
4158void LCodeGen::DoDeleteProperty(LDeleteProperty* instr) {
Ben Murdochb8e0da22011-05-16 14:20:40 +01004159 Register object = ToRegister(instr->object());
4160 Register key = ToRegister(instr->key());
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004161 Register strict = scratch0();
4162 __ mov(strict, Operand(Smi::FromInt(strict_mode_flag())));
4163 __ Push(object, key, strict);
Steve Block1e0659c2011-05-24 12:43:12 +01004164 ASSERT(instr->HasPointerMap() && instr->HasDeoptimizationEnvironment());
4165 LPointerMap* pointers = instr->pointer_map();
4166 LEnvironment* env = instr->deoptimization_environment();
4167 RecordPosition(pointers->position());
4168 RegisterEnvironmentForDeoptimization(env);
Ben Murdochb8e0da22011-05-16 14:20:40 +01004169 SafepointGenerator safepoint_generator(this,
Steve Block1e0659c2011-05-24 12:43:12 +01004170 pointers,
4171 env->deoptimization_index());
Ben Murdochb8e0da22011-05-16 14:20:40 +01004172 __ InvokeBuiltin(Builtins::DELETE, CALL_JS, &safepoint_generator);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004173}
4174
4175
4176void LCodeGen::DoStackCheck(LStackCheck* instr) {
4177 // Perform stack overflow check.
4178 Label ok;
4179 __ LoadRoot(ip, Heap::kStackLimitRootIndex);
4180 __ cmp(sp, Operand(ip));
4181 __ b(hs, &ok);
4182 StackCheckStub stub;
4183 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
4184 __ bind(&ok);
4185}
4186
4187
4188void LCodeGen::DoOsrEntry(LOsrEntry* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01004189 // This is a pseudo-instruction that ensures that the environment here is
4190 // properly registered for deoptimization and records the assembler's PC
4191 // offset.
4192 LEnvironment* environment = instr->environment();
4193 environment->SetSpilledRegisters(instr->SpilledRegisterArray(),
4194 instr->SpilledDoubleRegisterArray());
4195
4196 // If the environment were already registered, we would have no way of
4197 // backpatching it with the spill slot operands.
4198 ASSERT(!environment->HasBeenRegistered());
4199 RegisterEnvironmentForDeoptimization(environment);
4200 ASSERT(osr_pc_offset_ == -1);
4201 osr_pc_offset_ = masm()->pc_offset();
Ben Murdochb0fe1622011-05-05 13:52:32 +01004202}
4203
4204
4205#undef __
4206
4207} } // namespace v8::internal