blob: d64f528e71c9a6d92221b2741df9c5cf4f82f7dd [file] [log] [blame]
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001// Copyright 2010 the V8 project authors. All rights reserved.
2// 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
28#include "ia32/lithium-codegen-ia32.h"
29#include "code-stubs.h"
30#include "stub-cache.h"
31
32namespace v8 {
33namespace internal {
34
35
36class SafepointGenerator : public PostCallGenerator {
37 public:
38 SafepointGenerator(LCodeGen* codegen,
39 LPointerMap* pointers,
40 int deoptimization_index)
41 : codegen_(codegen),
42 pointers_(pointers),
43 deoptimization_index_(deoptimization_index) { }
44 virtual ~SafepointGenerator() { }
45
46 virtual void Generate() {
47 codegen_->RecordSafepoint(pointers_, deoptimization_index_);
48 }
49
50 private:
51 LCodeGen* codegen_;
52 LPointerMap* pointers_;
53 int deoptimization_index_;
54};
55
56
57#define __ masm()->
58
59bool LCodeGen::GenerateCode() {
60 HPhase phase("Code generation", chunk());
61 ASSERT(is_unused());
62 status_ = GENERATING;
63 CpuFeatures::Scope scope(SSE2);
64 return GeneratePrologue() &&
65 GenerateBody() &&
66 GenerateDeferredCode() &&
67 GenerateSafepointTable();
68}
69
70
71void LCodeGen::FinishCode(Handle<Code> code) {
72 ASSERT(is_done());
73 code->set_stack_slots(StackSlotCount());
74 code->set_safepoint_table_start(safepoints_.GetCodeOffset());
75 PopulateDeoptimizationData(code);
76}
77
78
79void LCodeGen::Abort(const char* format, ...) {
80 if (FLAG_trace_bailout) {
81 SmartPointer<char> debug_name = graph()->debug_name()->ToCString();
82 PrintF("Aborting LCodeGen in @\"%s\": ", *debug_name);
83 va_list arguments;
84 va_start(arguments, format);
85 OS::VPrint(format, arguments);
86 va_end(arguments);
87 PrintF("\n");
88 }
89 status_ = ABORTED;
90}
91
92
93void LCodeGen::Comment(const char* format, ...) {
94 if (!FLAG_code_comments) return;
95 char buffer[4 * KB];
96 StringBuilder builder(buffer, ARRAY_SIZE(buffer));
97 va_list arguments;
98 va_start(arguments, format);
99 builder.AddFormattedList(format, arguments);
100 va_end(arguments);
101
102 // Copy the string before recording it in the assembler to avoid
103 // issues when the stack allocated buffer goes out of scope.
104 size_t length = builder.position();
105 Vector<char> copy = Vector<char>::New(length + 1);
106 memcpy(copy.start(), builder.Finalize(), copy.length());
107 masm()->RecordComment(copy.start());
108}
109
110
111bool LCodeGen::GeneratePrologue() {
112 ASSERT(is_generating());
113
114#ifdef DEBUG
115 if (strlen(FLAG_stop_at) > 0 &&
116 info_->function()->name()->IsEqualTo(CStrVector(FLAG_stop_at))) {
117 __ int3();
118 }
119#endif
120
121 __ push(ebp); // Caller's frame pointer.
122 __ mov(ebp, esp);
123 __ push(esi); // Callee's context.
124 __ push(edi); // Callee's JS function.
125
126 // Reserve space for the stack slots needed by the code.
127 int slots = StackSlotCount();
128 if (slots > 0) {
129 if (FLAG_debug_code) {
130 __ mov(Operand(eax), Immediate(slots));
131 Label loop;
132 __ bind(&loop);
133 __ push(Immediate(kSlotsZapValue));
134 __ dec(eax);
135 __ j(not_zero, &loop);
136 } else {
137 __ sub(Operand(esp), Immediate(slots * kPointerSize));
138 }
139 }
140
141 // Trace the call.
142 if (FLAG_trace) {
143 __ CallRuntime(Runtime::kTraceEnter, 0);
144 }
145 return !is_aborted();
146}
147
148
149bool LCodeGen::GenerateBody() {
150 ASSERT(is_generating());
151 bool emit_instructions = true;
152 for (current_instruction_ = 0;
153 !is_aborted() && current_instruction_ < instructions_->length();
154 current_instruction_++) {
155 LInstruction* instr = instructions_->at(current_instruction_);
156 if (instr->IsLabel()) {
157 LLabel* label = LLabel::cast(instr);
158 emit_instructions = !label->HasReplacement();
159 }
160
161 if (emit_instructions) {
162 Comment(";;; @%d: %s.", current_instruction_, instr->Mnemonic());
163 instr->CompileToNative(this);
164 }
165 }
166 return !is_aborted();
167}
168
169
170LInstruction* LCodeGen::GetNextInstruction() {
171 if (current_instruction_ < instructions_->length() - 1) {
172 return instructions_->at(current_instruction_ + 1);
173 } else {
174 return NULL;
175 }
176}
177
178
179bool LCodeGen::GenerateDeferredCode() {
180 ASSERT(is_generating());
181 for (int i = 0; !is_aborted() && i < deferred_.length(); i++) {
182 LDeferredCode* code = deferred_[i];
183 __ bind(code->entry());
184 code->Generate();
185 __ jmp(code->exit());
186 }
187
188 // Deferred code is the last part of the instruction sequence. Mark
189 // the generated code as done unless we bailed out.
190 if (!is_aborted()) status_ = DONE;
191 return !is_aborted();
192}
193
194
195bool LCodeGen::GenerateSafepointTable() {
196 ASSERT(is_done());
197 safepoints_.Emit(masm(), StackSlotCount());
198 return !is_aborted();
199}
200
201
202Register LCodeGen::ToRegister(int index) const {
203 return Register::FromAllocationIndex(index);
204}
205
206
207XMMRegister LCodeGen::ToDoubleRegister(int index) const {
208 return XMMRegister::FromAllocationIndex(index);
209}
210
211
212Register LCodeGen::ToRegister(LOperand* op) const {
213 ASSERT(op->IsRegister());
214 return ToRegister(op->index());
215}
216
217
218XMMRegister LCodeGen::ToDoubleRegister(LOperand* op) const {
219 ASSERT(op->IsDoubleRegister());
220 return ToDoubleRegister(op->index());
221}
222
223
224int LCodeGen::ToInteger32(LConstantOperand* op) const {
225 Handle<Object> value = chunk_->LookupLiteral(op);
226 ASSERT(chunk_->LookupLiteralRepresentation(op).IsInteger32());
227 ASSERT(static_cast<double>(static_cast<int32_t>(value->Number())) ==
228 value->Number());
229 return static_cast<int32_t>(value->Number());
230}
231
232
233Immediate LCodeGen::ToImmediate(LOperand* op) {
234 LConstantOperand* const_op = LConstantOperand::cast(op);
235 Handle<Object> literal = chunk_->LookupLiteral(const_op);
236 Representation r = chunk_->LookupLiteralRepresentation(const_op);
237 if (r.IsInteger32()) {
238 ASSERT(literal->IsNumber());
239 return Immediate(static_cast<int32_t>(literal->Number()));
240 } else if (r.IsDouble()) {
241 Abort("unsupported double immediate");
242 }
243 ASSERT(r.IsTagged());
244 return Immediate(literal);
245}
246
247
248Operand LCodeGen::ToOperand(LOperand* op) const {
249 if (op->IsRegister()) return Operand(ToRegister(op));
250 if (op->IsDoubleRegister()) return Operand(ToDoubleRegister(op));
251 ASSERT(op->IsStackSlot() || op->IsDoubleStackSlot());
252 int index = op->index();
253 if (index >= 0) {
254 // Local or spill slot. Skip the frame pointer, function, and
255 // context in the fixed part of the frame.
256 return Operand(ebp, -(index + 3) * kPointerSize);
257 } else {
258 // Incoming parameter. Skip the return address.
259 return Operand(ebp, -(index - 1) * kPointerSize);
260 }
261}
262
263
264void LCodeGen::AddToTranslation(Translation* translation,
265 LOperand* op,
266 bool is_tagged) {
267 if (op == NULL) {
268 // TODO(twuerthinger): Introduce marker operands to indicate that this value
269 // is not present and must be reconstructed from the deoptimizer. Currently
270 // this is only used for the arguments object.
271 translation->StoreArgumentsObject();
272 } else if (op->IsStackSlot()) {
273 if (is_tagged) {
274 translation->StoreStackSlot(op->index());
275 } else {
276 translation->StoreInt32StackSlot(op->index());
277 }
278 } else if (op->IsDoubleStackSlot()) {
279 translation->StoreDoubleStackSlot(op->index());
280 } else if (op->IsArgument()) {
281 ASSERT(is_tagged);
282 int src_index = StackSlotCount() + op->index();
283 translation->StoreStackSlot(src_index);
284 } else if (op->IsRegister()) {
285 Register reg = ToRegister(op);
286 if (is_tagged) {
287 translation->StoreRegister(reg);
288 } else {
289 translation->StoreInt32Register(reg);
290 }
291 } else if (op->IsDoubleRegister()) {
292 XMMRegister reg = ToDoubleRegister(op);
293 translation->StoreDoubleRegister(reg);
294 } else if (op->IsConstantOperand()) {
295 Handle<Object> literal = chunk()->LookupLiteral(LConstantOperand::cast(op));
296 int src_index = DefineDeoptimizationLiteral(literal);
297 translation->StoreLiteral(src_index);
298 } else {
299 UNREACHABLE();
300 }
301}
302
303
304void LCodeGen::CallCode(Handle<Code> code,
305 RelocInfo::Mode mode,
306 LInstruction* instr) {
307 if (instr != NULL) {
308 LPointerMap* pointers = instr->pointer_map();
309 RecordPosition(pointers->position());
310 __ call(code, mode);
311 RegisterLazyDeoptimization(instr);
312 } else {
313 LPointerMap no_pointers(0);
314 RecordPosition(no_pointers.position());
315 __ call(code, mode);
316 RecordSafepoint(&no_pointers, Safepoint::kNoDeoptimizationIndex);
317 }
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000318
319 // Signal that we don't inline smi code before these stubs in the
320 // optimizing code generator.
321 if (code->kind() == Code::TYPE_RECORDING_BINARY_OP_IC ||
322 code->kind() == Code::COMPARE_IC) {
323 __ nop();
324 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000325}
326
327
328void LCodeGen::CallRuntime(Runtime::Function* function,
329 int num_arguments,
330 LInstruction* instr) {
331 ASSERT(instr != NULL);
332 LPointerMap* pointers = instr->pointer_map();
333 ASSERT(pointers != NULL);
334 RecordPosition(pointers->position());
335
336 __ CallRuntime(function, num_arguments);
337 // Runtime calls to Throw are not supposed to ever return at the
338 // call site, so don't register lazy deoptimization for these. We do
339 // however have to record a safepoint since throwing exceptions can
340 // cause garbage collections.
341 // BUG(3243555): register a lazy deoptimization point at throw. We need
342 // it to be able to inline functions containing a throw statement.
343 if (!instr->IsThrow()) {
344 RegisterLazyDeoptimization(instr);
345 } else {
346 RecordSafepoint(instr->pointer_map(), Safepoint::kNoDeoptimizationIndex);
347 }
348}
349
350
351void LCodeGen::RegisterLazyDeoptimization(LInstruction* instr) {
352 // Create the environment to bailout to. If the call has side effects
353 // execution has to continue after the call otherwise execution can continue
354 // from a previous bailout point repeating the call.
355 LEnvironment* deoptimization_environment;
356 if (instr->HasDeoptimizationEnvironment()) {
357 deoptimization_environment = instr->deoptimization_environment();
358 } else {
359 deoptimization_environment = instr->environment();
360 }
361
362 RegisterEnvironmentForDeoptimization(deoptimization_environment);
363 RecordSafepoint(instr->pointer_map(),
364 deoptimization_environment->deoptimization_index());
365}
366
367
368void LCodeGen::RegisterEnvironmentForDeoptimization(LEnvironment* environment) {
369 if (!environment->HasBeenRegistered()) {
370 // Physical stack frame layout:
371 // -x ............. -4 0 ..................................... y
372 // [incoming arguments] [spill slots] [pushed outgoing arguments]
373
374 // Layout of the environment:
375 // 0 ..................................................... size-1
376 // [parameters] [locals] [expression stack including arguments]
377
378 // Layout of the translation:
379 // 0 ........................................................ size - 1 + 4
380 // [expression stack including arguments] [locals] [4 words] [parameters]
381 // |>------------ translation_size ------------<|
382
383 int frame_count = 0;
384 for (LEnvironment* e = environment; e != NULL; e = e->outer()) {
385 ++frame_count;
386 }
387 Translation translation(&translations_, frame_count);
388 environment->WriteTranslation(this, &translation);
389 int deoptimization_index = deoptimizations_.length();
390 environment->Register(deoptimization_index, translation.index());
391 deoptimizations_.Add(environment);
392 }
393}
394
395
396void LCodeGen::DeoptimizeIf(Condition cc, LEnvironment* environment) {
397 RegisterEnvironmentForDeoptimization(environment);
398 ASSERT(environment->HasBeenRegistered());
399 int id = environment->deoptimization_index();
400 Address entry = Deoptimizer::GetDeoptimizationEntry(id, Deoptimizer::EAGER);
401 ASSERT(entry != NULL);
402 if (entry == NULL) {
403 Abort("bailout was not prepared");
404 return;
405 }
406
407 if (FLAG_deopt_every_n_times != 0) {
408 Handle<SharedFunctionInfo> shared(info_->shared_info());
409 Label no_deopt;
410 __ pushfd();
411 __ push(eax);
412 __ push(ebx);
413 __ mov(ebx, shared);
414 __ mov(eax, FieldOperand(ebx, SharedFunctionInfo::kDeoptCounterOffset));
415 __ sub(Operand(eax), Immediate(Smi::FromInt(1)));
416 __ j(not_zero, &no_deopt);
417 if (FLAG_trap_on_deopt) __ int3();
418 __ mov(eax, Immediate(Smi::FromInt(FLAG_deopt_every_n_times)));
419 __ mov(FieldOperand(ebx, SharedFunctionInfo::kDeoptCounterOffset), eax);
420 __ pop(ebx);
421 __ pop(eax);
422 __ popfd();
423 __ jmp(entry, RelocInfo::RUNTIME_ENTRY);
424
425 __ bind(&no_deopt);
426 __ mov(FieldOperand(ebx, SharedFunctionInfo::kDeoptCounterOffset), eax);
427 __ pop(ebx);
428 __ pop(eax);
429 __ popfd();
430 }
431
432 if (cc == no_condition) {
433 if (FLAG_trap_on_deopt) __ int3();
434 __ jmp(entry, RelocInfo::RUNTIME_ENTRY);
435 } else {
436 if (FLAG_trap_on_deopt) {
437 NearLabel done;
438 __ j(NegateCondition(cc), &done);
439 __ int3();
440 __ jmp(entry, RelocInfo::RUNTIME_ENTRY);
441 __ bind(&done);
442 } else {
443 __ j(cc, entry, RelocInfo::RUNTIME_ENTRY, not_taken);
444 }
445 }
446}
447
448
449void LCodeGen::PopulateDeoptimizationData(Handle<Code> code) {
450 int length = deoptimizations_.length();
451 if (length == 0) return;
452 ASSERT(FLAG_deopt);
453 Handle<DeoptimizationInputData> data =
454 Factory::NewDeoptimizationInputData(length, TENURED);
455
456 data->SetTranslationByteArray(*translations_.CreateByteArray());
457 data->SetInlinedFunctionCount(Smi::FromInt(inlined_function_count_));
458
459 Handle<FixedArray> literals =
460 Factory::NewFixedArray(deoptimization_literals_.length(), TENURED);
461 for (int i = 0; i < deoptimization_literals_.length(); i++) {
462 literals->set(i, *deoptimization_literals_[i]);
463 }
464 data->SetLiteralArray(*literals);
465
466 data->SetOsrAstId(Smi::FromInt(info_->osr_ast_id()));
467 data->SetOsrPcOffset(Smi::FromInt(osr_pc_offset_));
468
469 // Populate the deoptimization entries.
470 for (int i = 0; i < length; i++) {
471 LEnvironment* env = deoptimizations_[i];
472 data->SetAstId(i, Smi::FromInt(env->ast_id()));
473 data->SetTranslationIndex(i, Smi::FromInt(env->translation_index()));
474 data->SetArgumentsStackHeight(i,
475 Smi::FromInt(env->arguments_stack_height()));
476 }
477 code->set_deoptimization_data(*data);
478}
479
480
481int LCodeGen::DefineDeoptimizationLiteral(Handle<Object> literal) {
482 int result = deoptimization_literals_.length();
483 for (int i = 0; i < deoptimization_literals_.length(); ++i) {
484 if (deoptimization_literals_[i].is_identical_to(literal)) return i;
485 }
486 deoptimization_literals_.Add(literal);
487 return result;
488}
489
490
491void LCodeGen::PopulateDeoptimizationLiteralsWithInlinedFunctions() {
492 ASSERT(deoptimization_literals_.length() == 0);
493
494 const ZoneList<Handle<JSFunction> >* inlined_closures =
495 chunk()->inlined_closures();
496
497 for (int i = 0, length = inlined_closures->length();
498 i < length;
499 i++) {
500 DefineDeoptimizationLiteral(inlined_closures->at(i));
501 }
502
503 inlined_function_count_ = deoptimization_literals_.length();
504}
505
506
507void LCodeGen::RecordSafepoint(LPointerMap* pointers,
508 int deoptimization_index) {
509 const ZoneList<LOperand*>* operands = pointers->operands();
510 Safepoint safepoint = safepoints_.DefineSafepoint(masm(),
511 deoptimization_index);
512 for (int i = 0; i < operands->length(); i++) {
513 LOperand* pointer = operands->at(i);
514 if (pointer->IsStackSlot()) {
515 safepoint.DefinePointerSlot(pointer->index());
516 }
517 }
518}
519
520
521void LCodeGen::RecordSafepointWithRegisters(LPointerMap* pointers,
522 int arguments,
523 int deoptimization_index) {
524 const ZoneList<LOperand*>* operands = pointers->operands();
525 Safepoint safepoint =
526 safepoints_.DefineSafepointWithRegisters(
527 masm(), arguments, deoptimization_index);
528 for (int i = 0; i < operands->length(); i++) {
529 LOperand* pointer = operands->at(i);
530 if (pointer->IsStackSlot()) {
531 safepoint.DefinePointerSlot(pointer->index());
532 } else if (pointer->IsRegister()) {
533 safepoint.DefinePointerRegister(ToRegister(pointer));
534 }
535 }
536 // Register esi always contains a pointer to the context.
537 safepoint.DefinePointerRegister(esi);
538}
539
540
541void LCodeGen::RecordPosition(int position) {
542 if (!FLAG_debug_info || position == RelocInfo::kNoPosition) return;
543 masm()->positions_recorder()->RecordPosition(position);
544}
545
546
547void LCodeGen::DoLabel(LLabel* label) {
548 if (label->is_loop_header()) {
549 Comment(";;; B%d - LOOP entry", label->block_id());
550 } else {
551 Comment(";;; B%d", label->block_id());
552 }
553 __ bind(label->label());
554 current_block_ = label->block_id();
555 LCodeGen::DoGap(label);
556}
557
558
559void LCodeGen::DoParallelMove(LParallelMove* move) {
560 // xmm0 must always be a scratch register.
561 XMMRegister xmm_scratch = xmm0;
562 LUnallocated marker_operand(LUnallocated::NONE);
563
564 Register cpu_scratch = esi;
565 bool destroys_cpu_scratch = false;
566
567 LGapResolver resolver(move->move_operands(), &marker_operand);
568 const ZoneList<LMoveOperands>* moves = resolver.ResolveInReverseOrder();
569 for (int i = moves->length() - 1; i >= 0; --i) {
570 LMoveOperands move = moves->at(i);
571 LOperand* from = move.from();
572 LOperand* to = move.to();
573 ASSERT(!from->IsDoubleRegister() ||
574 !ToDoubleRegister(from).is(xmm_scratch));
575 ASSERT(!to->IsDoubleRegister() || !ToDoubleRegister(to).is(xmm_scratch));
576 ASSERT(!from->IsRegister() || !ToRegister(from).is(cpu_scratch));
577 ASSERT(!to->IsRegister() || !ToRegister(to).is(cpu_scratch));
578 if (from->IsConstantOperand()) {
579 __ mov(ToOperand(to), ToImmediate(from));
580 } else if (from == &marker_operand) {
581 if (to->IsRegister() || to->IsStackSlot()) {
582 __ mov(ToOperand(to), cpu_scratch);
583 ASSERT(destroys_cpu_scratch);
584 } else {
585 ASSERT(to->IsDoubleRegister() || to->IsDoubleStackSlot());
586 __ movdbl(ToOperand(to), xmm_scratch);
587 }
588 } else if (to == &marker_operand) {
589 if (from->IsRegister() || from->IsStackSlot()) {
590 __ mov(cpu_scratch, ToOperand(from));
591 destroys_cpu_scratch = true;
592 } else {
593 ASSERT(from->IsDoubleRegister() || from->IsDoubleStackSlot());
594 __ movdbl(xmm_scratch, ToOperand(from));
595 }
596 } else if (from->IsRegister()) {
597 __ mov(ToOperand(to), ToRegister(from));
598 } else if (to->IsRegister()) {
599 __ mov(ToRegister(to), ToOperand(from));
600 } else if (from->IsStackSlot()) {
601 ASSERT(to->IsStackSlot());
602 __ push(eax);
603 __ mov(eax, ToOperand(from));
604 __ mov(ToOperand(to), eax);
605 __ pop(eax);
606 } else if (from->IsDoubleRegister()) {
607 __ movdbl(ToOperand(to), ToDoubleRegister(from));
608 } else if (to->IsDoubleRegister()) {
609 __ movdbl(ToDoubleRegister(to), ToOperand(from));
610 } else {
611 ASSERT(to->IsDoubleStackSlot() && from->IsDoubleStackSlot());
612 __ movdbl(xmm_scratch, ToOperand(from));
613 __ movdbl(ToOperand(to), xmm_scratch);
614 }
615 }
616
617 if (destroys_cpu_scratch) {
618 __ mov(cpu_scratch, Operand(ebp, -kPointerSize));
619 }
620}
621
622
623void LCodeGen::DoGap(LGap* gap) {
624 for (int i = LGap::FIRST_INNER_POSITION;
625 i <= LGap::LAST_INNER_POSITION;
626 i++) {
627 LGap::InnerPosition inner_pos = static_cast<LGap::InnerPosition>(i);
628 LParallelMove* move = gap->GetParallelMove(inner_pos);
629 if (move != NULL) DoParallelMove(move);
630 }
631
632 LInstruction* next = GetNextInstruction();
633 if (next != NULL && next->IsLazyBailout()) {
634 int pc = masm()->pc_offset();
635 safepoints_.SetPcAfterGap(pc);
636 }
637}
638
639
640void LCodeGen::DoParameter(LParameter* instr) {
641 // Nothing to do.
642}
643
644
645void LCodeGen::DoCallStub(LCallStub* instr) {
646 ASSERT(ToRegister(instr->result()).is(eax));
647 switch (instr->hydrogen()->major_key()) {
648 case CodeStub::RegExpConstructResult: {
649 RegExpConstructResultStub stub;
650 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
651 break;
652 }
653 case CodeStub::RegExpExec: {
654 RegExpExecStub stub;
655 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
656 break;
657 }
658 case CodeStub::SubString: {
659 SubStringStub stub;
660 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
661 break;
662 }
663 case CodeStub::StringCharAt: {
664 StringCharAtStub stub;
665 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
666 break;
667 }
668 case CodeStub::MathPow: {
669 MathPowStub stub;
670 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
671 break;
672 }
673 case CodeStub::NumberToString: {
674 NumberToStringStub stub;
675 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
676 break;
677 }
678 case CodeStub::StringAdd: {
679 StringAddStub stub(NO_STRING_ADD_FLAGS);
680 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
681 break;
682 }
683 case CodeStub::StringCompare: {
684 StringCompareStub stub;
685 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
686 break;
687 }
688 case CodeStub::TranscendentalCache: {
whesse@chromium.org023421e2010-12-21 12:19:12 +0000689 TranscendentalCacheStub stub(instr->transcendental_type(),
690 TranscendentalCacheStub::TAGGED);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000691 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
692 break;
693 }
694 default:
695 UNREACHABLE();
696 }
697}
698
699
700void LCodeGen::DoUnknownOSRValue(LUnknownOSRValue* instr) {
701 // Nothing to do.
702}
703
704
705void LCodeGen::DoModI(LModI* instr) {
706 LOperand* right = instr->right();
707 ASSERT(ToRegister(instr->result()).is(edx));
708 ASSERT(ToRegister(instr->left()).is(eax));
709 ASSERT(!ToRegister(instr->right()).is(eax));
710 ASSERT(!ToRegister(instr->right()).is(edx));
711
712 Register right_reg = ToRegister(right);
713
714 // Check for x % 0.
715 if (instr->hydrogen()->CheckFlag(HValue::kCanBeDivByZero)) {
716 __ test(right_reg, ToOperand(right));
717 DeoptimizeIf(zero, instr->environment());
718 }
719
720 // Sign extend to edx.
721 __ cdq();
722
723 // Check for (0 % -x) that will produce negative zero.
724 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
725 NearLabel positive_left;
726 NearLabel done;
727 __ test(eax, Operand(eax));
728 __ j(not_sign, &positive_left);
729 __ idiv(right_reg);
730
731 // Test the remainder for 0, because then the result would be -0.
732 __ test(edx, Operand(edx));
733 __ j(not_zero, &done);
734
735 DeoptimizeIf(no_condition, instr->environment());
736 __ bind(&positive_left);
737 __ idiv(right_reg);
738 __ bind(&done);
739 } else {
740 __ idiv(right_reg);
741 }
742}
743
744
745void LCodeGen::DoDivI(LDivI* instr) {
746 LOperand* right = instr->right();
747 ASSERT(ToRegister(instr->result()).is(eax));
748 ASSERT(ToRegister(instr->left()).is(eax));
749 ASSERT(!ToRegister(instr->right()).is(eax));
750 ASSERT(!ToRegister(instr->right()).is(edx));
751
752 Register left_reg = eax;
753
754 // Check for x / 0.
755 Register right_reg = ToRegister(right);
756 if (instr->hydrogen()->CheckFlag(HValue::kCanBeDivByZero)) {
757 __ test(right_reg, ToOperand(right));
758 DeoptimizeIf(zero, instr->environment());
759 }
760
761 // Check for (0 / -x) that will produce negative zero.
762 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
763 NearLabel left_not_zero;
764 __ test(left_reg, Operand(left_reg));
765 __ j(not_zero, &left_not_zero);
766 __ test(right_reg, ToOperand(right));
767 DeoptimizeIf(sign, instr->environment());
768 __ bind(&left_not_zero);
769 }
770
771 // Check for (-kMinInt / -1).
772 if (instr->hydrogen()->CheckFlag(HValue::kCanOverflow)) {
773 NearLabel left_not_min_int;
774 __ cmp(left_reg, kMinInt);
775 __ j(not_zero, &left_not_min_int);
776 __ cmp(right_reg, -1);
777 DeoptimizeIf(zero, instr->environment());
778 __ bind(&left_not_min_int);
779 }
780
781 // Sign extend to edx.
782 __ cdq();
783 __ idiv(right_reg);
784
785 // Deoptimize if remainder is not 0.
786 __ test(edx, Operand(edx));
787 DeoptimizeIf(not_zero, instr->environment());
788}
789
790
791void LCodeGen::DoMulI(LMulI* instr) {
792 Register left = ToRegister(instr->left());
793 LOperand* right = instr->right();
794
795 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
796 __ mov(ToRegister(instr->temp()), left);
797 }
798
799 if (right->IsConstantOperand()) {
800 __ imul(left, left, ToInteger32(LConstantOperand::cast(right)));
801 } else {
802 __ imul(left, ToOperand(right));
803 }
804
805 if (instr->hydrogen()->CheckFlag(HValue::kCanOverflow)) {
806 DeoptimizeIf(overflow, instr->environment());
807 }
808
809 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
810 // Bail out if the result is supposed to be negative zero.
811 NearLabel done;
812 __ test(left, Operand(left));
813 __ j(not_zero, &done);
814 if (right->IsConstantOperand()) {
815 if (ToInteger32(LConstantOperand::cast(right)) < 0) {
816 DeoptimizeIf(no_condition, instr->environment());
817 }
818 } else {
819 // Test the non-zero operand for negative sign.
820 __ or_(ToRegister(instr->temp()), ToOperand(right));
821 DeoptimizeIf(sign, instr->environment());
822 }
823 __ bind(&done);
824 }
825}
826
827
828void LCodeGen::DoBitI(LBitI* instr) {
829 LOperand* left = instr->left();
830 LOperand* right = instr->right();
831 ASSERT(left->Equals(instr->result()));
832 ASSERT(left->IsRegister());
833
834 if (right->IsConstantOperand()) {
835 int right_operand = ToInteger32(LConstantOperand::cast(right));
836 switch (instr->op()) {
837 case Token::BIT_AND:
838 __ and_(ToRegister(left), right_operand);
839 break;
840 case Token::BIT_OR:
841 __ or_(ToRegister(left), right_operand);
842 break;
843 case Token::BIT_XOR:
844 __ xor_(ToRegister(left), right_operand);
845 break;
846 default:
847 UNREACHABLE();
848 break;
849 }
850 } else {
851 switch (instr->op()) {
852 case Token::BIT_AND:
853 __ and_(ToRegister(left), ToOperand(right));
854 break;
855 case Token::BIT_OR:
856 __ or_(ToRegister(left), ToOperand(right));
857 break;
858 case Token::BIT_XOR:
859 __ xor_(ToRegister(left), ToOperand(right));
860 break;
861 default:
862 UNREACHABLE();
863 break;
864 }
865 }
866}
867
868
869void LCodeGen::DoShiftI(LShiftI* instr) {
870 LOperand* left = instr->left();
871 LOperand* right = instr->right();
872 ASSERT(left->Equals(instr->result()));
873 ASSERT(left->IsRegister());
874 if (right->IsRegister()) {
875 ASSERT(ToRegister(right).is(ecx));
876
877 switch (instr->op()) {
878 case Token::SAR:
879 __ sar_cl(ToRegister(left));
880 break;
881 case Token::SHR:
882 __ shr_cl(ToRegister(left));
883 if (instr->can_deopt()) {
884 __ test(ToRegister(left), Immediate(0x80000000));
885 DeoptimizeIf(not_zero, instr->environment());
886 }
887 break;
888 case Token::SHL:
889 __ shl_cl(ToRegister(left));
890 break;
891 default:
892 UNREACHABLE();
893 break;
894 }
895 } else {
896 int value = ToInteger32(LConstantOperand::cast(right));
897 uint8_t shift_count = static_cast<uint8_t>(value & 0x1F);
898 switch (instr->op()) {
899 case Token::SAR:
900 if (shift_count != 0) {
901 __ sar(ToRegister(left), shift_count);
902 }
903 break;
904 case Token::SHR:
905 if (shift_count == 0 && instr->can_deopt()) {
906 __ test(ToRegister(left), Immediate(0x80000000));
907 DeoptimizeIf(not_zero, instr->environment());
908 } else {
909 __ shr(ToRegister(left), shift_count);
910 }
911 break;
912 case Token::SHL:
913 if (shift_count != 0) {
914 __ shl(ToRegister(left), shift_count);
915 }
916 break;
917 default:
918 UNREACHABLE();
919 break;
920 }
921 }
922}
923
924
925void LCodeGen::DoSubI(LSubI* instr) {
926 LOperand* left = instr->left();
927 LOperand* right = instr->right();
928 ASSERT(left->Equals(instr->result()));
929
930 if (right->IsConstantOperand()) {
931 __ sub(ToOperand(left), ToImmediate(right));
932 } else {
933 __ sub(ToRegister(left), ToOperand(right));
934 }
935 if (instr->hydrogen()->CheckFlag(HValue::kCanOverflow)) {
936 DeoptimizeIf(overflow, instr->environment());
937 }
938}
939
940
941void LCodeGen::DoConstantI(LConstantI* instr) {
942 ASSERT(instr->result()->IsRegister());
943 __ mov(ToRegister(instr->result()), instr->value());
944}
945
946
947void LCodeGen::DoConstantD(LConstantD* instr) {
948 ASSERT(instr->result()->IsDoubleRegister());
949 XMMRegister res = ToDoubleRegister(instr->result());
950 double v = instr->value();
951 // Use xor to produce +0.0 in a fast and compact way, but avoid to
952 // do so if the constant is -0.0.
953 if (BitCast<uint64_t, double>(v) == 0) {
954 __ xorpd(res, res);
955 } else {
956 int32_t v_int32 = static_cast<int32_t>(v);
957 if (static_cast<double>(v_int32) == v) {
958 __ push_imm32(v_int32);
959 __ cvtsi2sd(res, Operand(esp, 0));
960 __ add(Operand(esp), Immediate(kPointerSize));
961 } else {
962 uint64_t int_val = BitCast<uint64_t, double>(v);
963 int32_t lower = static_cast<int32_t>(int_val);
964 int32_t upper = static_cast<int32_t>(int_val >> (kBitsPerInt));
965 __ push_imm32(upper);
966 __ push_imm32(lower);
967 __ movdbl(res, Operand(esp, 0));
968 __ add(Operand(esp), Immediate(2 * kPointerSize));
969 }
970 }
971}
972
973
974void LCodeGen::DoConstantT(LConstantT* instr) {
975 ASSERT(instr->result()->IsRegister());
976 __ mov(ToRegister(instr->result()), Immediate(instr->value()));
977}
978
979
980void LCodeGen::DoArrayLength(LArrayLength* instr) {
981 Register result = ToRegister(instr->result());
982
983 if (instr->hydrogen()->value()->IsLoadElements()) {
984 // We load the length directly from the elements array.
985 Register elements = ToRegister(instr->input());
986 __ mov(result, FieldOperand(elements, FixedArray::kLengthOffset));
987 } else {
988 // Check that the receiver really is an array.
989 Register array = ToRegister(instr->input());
990 Register temporary = ToRegister(instr->temporary());
991 __ CmpObjectType(array, JS_ARRAY_TYPE, temporary);
992 DeoptimizeIf(not_equal, instr->environment());
993
994 // Load length directly from the array.
995 __ mov(result, FieldOperand(array, JSArray::kLengthOffset));
996 }
997}
998
999
1000void LCodeGen::DoValueOf(LValueOf* instr) {
1001 Register input = ToRegister(instr->input());
1002 Register result = ToRegister(instr->result());
1003 Register map = ToRegister(instr->temporary());
1004 ASSERT(input.is(result));
1005 NearLabel done;
1006 // If the object is a smi return the object.
1007 __ test(input, Immediate(kSmiTagMask));
1008 __ j(zero, &done);
1009
1010 // If the object is not a value type, return the object.
1011 __ CmpObjectType(input, JS_VALUE_TYPE, map);
1012 __ j(not_equal, &done);
1013 __ mov(result, FieldOperand(input, JSValue::kValueOffset));
1014
1015 __ bind(&done);
1016}
1017
1018
1019void LCodeGen::DoBitNotI(LBitNotI* instr) {
1020 LOperand* input = instr->input();
1021 ASSERT(input->Equals(instr->result()));
1022 __ not_(ToRegister(input));
1023}
1024
1025
1026void LCodeGen::DoThrow(LThrow* instr) {
1027 __ push(ToOperand(instr->input()));
1028 CallRuntime(Runtime::kThrow, 1, instr);
1029
1030 if (FLAG_debug_code) {
1031 Comment("Unreachable code.");
1032 __ int3();
1033 }
1034}
1035
1036
1037void LCodeGen::DoAddI(LAddI* instr) {
1038 LOperand* left = instr->left();
1039 LOperand* right = instr->right();
1040 ASSERT(left->Equals(instr->result()));
1041
1042 if (right->IsConstantOperand()) {
1043 __ add(ToOperand(left), ToImmediate(right));
1044 } else {
1045 __ add(ToRegister(left), ToOperand(right));
1046 }
1047
1048 if (instr->hydrogen()->CheckFlag(HValue::kCanOverflow)) {
1049 DeoptimizeIf(overflow, instr->environment());
1050 }
1051}
1052
1053
1054void LCodeGen::DoArithmeticD(LArithmeticD* instr) {
1055 LOperand* left = instr->left();
1056 LOperand* right = instr->right();
1057 // Modulo uses a fixed result register.
1058 ASSERT(instr->op() == Token::MOD || left->Equals(instr->result()));
1059 switch (instr->op()) {
1060 case Token::ADD:
1061 __ addsd(ToDoubleRegister(left), ToDoubleRegister(right));
1062 break;
1063 case Token::SUB:
1064 __ subsd(ToDoubleRegister(left), ToDoubleRegister(right));
1065 break;
1066 case Token::MUL:
1067 __ mulsd(ToDoubleRegister(left), ToDoubleRegister(right));
1068 break;
1069 case Token::DIV:
1070 __ divsd(ToDoubleRegister(left), ToDoubleRegister(right));
1071 break;
1072 case Token::MOD: {
1073 // Pass two doubles as arguments on the stack.
1074 __ PrepareCallCFunction(4, eax);
1075 __ movdbl(Operand(esp, 0 * kDoubleSize), ToDoubleRegister(left));
1076 __ movdbl(Operand(esp, 1 * kDoubleSize), ToDoubleRegister(right));
1077 __ CallCFunction(ExternalReference::double_fp_operation(Token::MOD), 4);
1078
1079 // Return value is in st(0) on ia32.
1080 // Store it into the (fixed) result register.
1081 __ sub(Operand(esp), Immediate(kDoubleSize));
1082 __ fstp_d(Operand(esp, 0));
1083 __ movdbl(ToDoubleRegister(instr->result()), Operand(esp, 0));
1084 __ add(Operand(esp), Immediate(kDoubleSize));
1085 break;
1086 }
1087 default:
1088 UNREACHABLE();
1089 break;
1090 }
1091}
1092
1093
1094void LCodeGen::DoArithmeticT(LArithmeticT* instr) {
1095 ASSERT(ToRegister(instr->left()).is(edx));
1096 ASSERT(ToRegister(instr->right()).is(eax));
1097 ASSERT(ToRegister(instr->result()).is(eax));
1098
1099 TypeRecordingBinaryOpStub stub(instr->op(), NO_OVERWRITE);
1100 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
1101}
1102
1103
1104int LCodeGen::GetNextEmittedBlock(int block) {
1105 for (int i = block + 1; i < graph()->blocks()->length(); ++i) {
1106 LLabel* label = chunk_->GetLabel(i);
1107 if (!label->HasReplacement()) return i;
1108 }
1109 return -1;
1110}
1111
1112
1113void LCodeGen::EmitBranch(int left_block, int right_block, Condition cc) {
1114 int next_block = GetNextEmittedBlock(current_block_);
1115 right_block = chunk_->LookupDestination(right_block);
1116 left_block = chunk_->LookupDestination(left_block);
1117
1118 if (right_block == left_block) {
1119 EmitGoto(left_block);
1120 } else if (left_block == next_block) {
1121 __ j(NegateCondition(cc), chunk_->GetAssemblyLabel(right_block));
1122 } else if (right_block == next_block) {
1123 __ j(cc, chunk_->GetAssemblyLabel(left_block));
1124 } else {
1125 __ j(cc, chunk_->GetAssemblyLabel(left_block));
1126 __ jmp(chunk_->GetAssemblyLabel(right_block));
1127 }
1128}
1129
1130
1131void LCodeGen::DoBranch(LBranch* instr) {
1132 int true_block = chunk_->LookupDestination(instr->true_block_id());
1133 int false_block = chunk_->LookupDestination(instr->false_block_id());
1134
1135 Representation r = instr->hydrogen()->representation();
1136 if (r.IsInteger32()) {
1137 Register reg = ToRegister(instr->input());
1138 __ test(reg, Operand(reg));
1139 EmitBranch(true_block, false_block, not_zero);
1140 } else if (r.IsDouble()) {
1141 XMMRegister reg = ToDoubleRegister(instr->input());
1142 __ xorpd(xmm0, xmm0);
1143 __ ucomisd(reg, xmm0);
1144 EmitBranch(true_block, false_block, not_equal);
1145 } else {
1146 ASSERT(r.IsTagged());
1147 Register reg = ToRegister(instr->input());
1148 if (instr->hydrogen()->type().IsBoolean()) {
1149 __ cmp(reg, Factory::true_value());
1150 EmitBranch(true_block, false_block, equal);
1151 } else {
1152 Label* true_label = chunk_->GetAssemblyLabel(true_block);
1153 Label* false_label = chunk_->GetAssemblyLabel(false_block);
1154
1155 __ cmp(reg, Factory::undefined_value());
1156 __ j(equal, false_label);
1157 __ cmp(reg, Factory::true_value());
1158 __ j(equal, true_label);
1159 __ cmp(reg, Factory::false_value());
1160 __ j(equal, false_label);
1161 __ test(reg, Operand(reg));
1162 __ j(equal, false_label);
1163 __ test(reg, Immediate(kSmiTagMask));
1164 __ j(zero, true_label);
1165
1166 // Test for double values. Zero is false.
1167 NearLabel call_stub;
1168 __ cmp(FieldOperand(reg, HeapObject::kMapOffset),
1169 Factory::heap_number_map());
1170 __ j(not_equal, &call_stub);
1171 __ fldz();
1172 __ fld_d(FieldOperand(reg, HeapNumber::kValueOffset));
1173 __ FCmp();
1174 __ j(zero, false_label);
1175 __ jmp(true_label);
1176
1177 // The conversion stub doesn't cause garbage collections so it's
1178 // safe to not record a safepoint after the call.
1179 __ bind(&call_stub);
1180 ToBooleanStub stub;
1181 __ pushad();
1182 __ push(reg);
1183 __ CallStub(&stub);
1184 __ test(eax, Operand(eax));
1185 __ popad();
1186 EmitBranch(true_block, false_block, not_zero);
1187 }
1188 }
1189}
1190
1191
1192void LCodeGen::EmitGoto(int block, LDeferredCode* deferred_stack_check) {
1193 block = chunk_->LookupDestination(block);
1194 int next_block = GetNextEmittedBlock(current_block_);
1195 if (block != next_block) {
1196 // Perform stack overflow check if this goto needs it before jumping.
1197 if (deferred_stack_check != NULL) {
1198 ExternalReference stack_limit =
1199 ExternalReference::address_of_stack_limit();
1200 __ cmp(esp, Operand::StaticVariable(stack_limit));
1201 __ j(above_equal, chunk_->GetAssemblyLabel(block));
1202 __ jmp(deferred_stack_check->entry());
1203 deferred_stack_check->SetExit(chunk_->GetAssemblyLabel(block));
1204 } else {
1205 __ jmp(chunk_->GetAssemblyLabel(block));
1206 }
1207 }
1208}
1209
1210
1211void LCodeGen::DoDeferredStackCheck(LGoto* instr) {
1212 __ pushad();
1213 __ CallRuntimeSaveDoubles(Runtime::kStackGuard);
1214 RecordSafepointWithRegisters(
1215 instr->pointer_map(), 0, Safepoint::kNoDeoptimizationIndex);
1216 __ popad();
1217}
1218
1219void LCodeGen::DoGoto(LGoto* instr) {
1220 class DeferredStackCheck: public LDeferredCode {
1221 public:
1222 DeferredStackCheck(LCodeGen* codegen, LGoto* instr)
1223 : LDeferredCode(codegen), instr_(instr) { }
1224 virtual void Generate() { codegen()->DoDeferredStackCheck(instr_); }
1225 private:
1226 LGoto* instr_;
1227 };
1228
1229 DeferredStackCheck* deferred = NULL;
1230 if (instr->include_stack_check()) {
1231 deferred = new DeferredStackCheck(this, instr);
1232 }
1233 EmitGoto(instr->block_id(), deferred);
1234}
1235
1236
1237Condition LCodeGen::TokenToCondition(Token::Value op, bool is_unsigned) {
1238 Condition cond = no_condition;
1239 switch (op) {
1240 case Token::EQ:
1241 case Token::EQ_STRICT:
1242 cond = equal;
1243 break;
1244 case Token::LT:
1245 cond = is_unsigned ? below : less;
1246 break;
1247 case Token::GT:
1248 cond = is_unsigned ? above : greater;
1249 break;
1250 case Token::LTE:
1251 cond = is_unsigned ? below_equal : less_equal;
1252 break;
1253 case Token::GTE:
1254 cond = is_unsigned ? above_equal : greater_equal;
1255 break;
1256 case Token::IN:
1257 case Token::INSTANCEOF:
1258 default:
1259 UNREACHABLE();
1260 }
1261 return cond;
1262}
1263
1264
1265void LCodeGen::EmitCmpI(LOperand* left, LOperand* right) {
1266 if (right->IsConstantOperand()) {
1267 __ cmp(ToOperand(left), ToImmediate(right));
1268 } else {
1269 __ cmp(ToRegister(left), ToOperand(right));
1270 }
1271}
1272
1273
1274void LCodeGen::DoCmpID(LCmpID* instr) {
1275 LOperand* left = instr->left();
1276 LOperand* right = instr->right();
1277 LOperand* result = instr->result();
1278
1279 NearLabel unordered;
1280 if (instr->is_double()) {
1281 // Don't base result on EFLAGS when a NaN is involved. Instead
1282 // jump to the unordered case, which produces a false value.
1283 __ ucomisd(ToDoubleRegister(left), ToDoubleRegister(right));
1284 __ j(parity_even, &unordered, not_taken);
1285 } else {
1286 EmitCmpI(left, right);
1287 }
1288
1289 NearLabel done;
1290 Condition cc = TokenToCondition(instr->op(), instr->is_double());
1291 __ mov(ToRegister(result), Handle<Object>(Heap::true_value()));
1292 __ j(cc, &done);
1293
1294 __ bind(&unordered);
1295 __ mov(ToRegister(result), Handle<Object>(Heap::false_value()));
1296 __ bind(&done);
1297}
1298
1299
1300void LCodeGen::DoCmpIDAndBranch(LCmpIDAndBranch* instr) {
1301 LOperand* left = instr->left();
1302 LOperand* right = instr->right();
1303 int false_block = chunk_->LookupDestination(instr->false_block_id());
1304 int true_block = chunk_->LookupDestination(instr->true_block_id());
1305
1306 if (instr->is_double()) {
1307 // Don't base result on EFLAGS when a NaN is involved. Instead
1308 // jump to the false block.
1309 __ ucomisd(ToDoubleRegister(left), ToDoubleRegister(right));
1310 __ j(parity_even, chunk_->GetAssemblyLabel(false_block));
1311 } else {
1312 EmitCmpI(left, right);
1313 }
1314
1315 Condition cc = TokenToCondition(instr->op(), instr->is_double());
1316 EmitBranch(true_block, false_block, cc);
1317}
1318
1319
1320void LCodeGen::DoCmpJSObjectEq(LCmpJSObjectEq* instr) {
1321 Register left = ToRegister(instr->left());
1322 Register right = ToRegister(instr->right());
1323 Register result = ToRegister(instr->result());
1324
1325 __ cmp(left, Operand(right));
1326 __ mov(result, Handle<Object>(Heap::true_value()));
1327 NearLabel done;
1328 __ j(equal, &done);
1329 __ mov(result, Handle<Object>(Heap::false_value()));
1330 __ bind(&done);
1331}
1332
1333
1334void LCodeGen::DoCmpJSObjectEqAndBranch(LCmpJSObjectEqAndBranch* instr) {
1335 Register left = ToRegister(instr->left());
1336 Register right = ToRegister(instr->right());
1337 int false_block = chunk_->LookupDestination(instr->false_block_id());
1338 int true_block = chunk_->LookupDestination(instr->true_block_id());
1339
1340 __ cmp(left, Operand(right));
1341 EmitBranch(true_block, false_block, equal);
1342}
1343
1344
1345void LCodeGen::DoIsNull(LIsNull* instr) {
1346 Register reg = ToRegister(instr->input());
1347 Register result = ToRegister(instr->result());
1348
1349 // TODO(fsc): If the expression is known to be a smi, then it's
1350 // definitely not null. Materialize false.
1351
1352 __ cmp(reg, Factory::null_value());
1353 if (instr->is_strict()) {
1354 __ mov(result, Handle<Object>(Heap::true_value()));
1355 NearLabel done;
1356 __ j(equal, &done);
1357 __ mov(result, Handle<Object>(Heap::false_value()));
1358 __ bind(&done);
1359 } else {
1360 NearLabel true_value, false_value, done;
1361 __ j(equal, &true_value);
1362 __ cmp(reg, Factory::undefined_value());
1363 __ j(equal, &true_value);
1364 __ test(reg, Immediate(kSmiTagMask));
1365 __ j(zero, &false_value);
1366 // Check for undetectable objects by looking in the bit field in
1367 // the map. The object has already been smi checked.
1368 Register scratch = result;
1369 __ mov(scratch, FieldOperand(reg, HeapObject::kMapOffset));
1370 __ movzx_b(scratch, FieldOperand(scratch, Map::kBitFieldOffset));
1371 __ test(scratch, Immediate(1 << Map::kIsUndetectable));
1372 __ j(not_zero, &true_value);
1373 __ bind(&false_value);
1374 __ mov(result, Handle<Object>(Heap::false_value()));
1375 __ jmp(&done);
1376 __ bind(&true_value);
1377 __ mov(result, Handle<Object>(Heap::true_value()));
1378 __ bind(&done);
1379 }
1380}
1381
1382
1383void LCodeGen::DoIsNullAndBranch(LIsNullAndBranch* instr) {
1384 Register reg = ToRegister(instr->input());
1385
1386 // TODO(fsc): If the expression is known to be a smi, then it's
1387 // definitely not null. Jump to the false block.
1388
1389 int true_block = chunk_->LookupDestination(instr->true_block_id());
1390 int false_block = chunk_->LookupDestination(instr->false_block_id());
1391
1392 __ cmp(reg, Factory::null_value());
1393 if (instr->is_strict()) {
1394 EmitBranch(true_block, false_block, equal);
1395 } else {
1396 Label* true_label = chunk_->GetAssemblyLabel(true_block);
1397 Label* false_label = chunk_->GetAssemblyLabel(false_block);
1398 __ j(equal, true_label);
1399 __ cmp(reg, Factory::undefined_value());
1400 __ j(equal, true_label);
1401 __ test(reg, Immediate(kSmiTagMask));
1402 __ j(zero, false_label);
1403 // Check for undetectable objects by looking in the bit field in
1404 // the map. The object has already been smi checked.
1405 Register scratch = ToRegister(instr->temp());
1406 __ mov(scratch, FieldOperand(reg, HeapObject::kMapOffset));
1407 __ movzx_b(scratch, FieldOperand(scratch, Map::kBitFieldOffset));
1408 __ test(scratch, Immediate(1 << Map::kIsUndetectable));
1409 EmitBranch(true_block, false_block, not_zero);
1410 }
1411}
1412
1413
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001414Condition LCodeGen::EmitIsObject(Register input,
1415 Register temp1,
1416 Register temp2,
1417 Label* is_not_object,
1418 Label* is_object) {
1419 ASSERT(!input.is(temp1));
1420 ASSERT(!input.is(temp2));
1421 ASSERT(!temp1.is(temp2));
1422
1423 __ test(input, Immediate(kSmiTagMask));
1424 __ j(equal, is_not_object);
1425
1426 __ cmp(input, Factory::null_value());
1427 __ j(equal, is_object);
1428
1429 __ mov(temp1, FieldOperand(input, HeapObject::kMapOffset));
1430 // Undetectable objects behave like undefined.
1431 __ movzx_b(temp2, FieldOperand(temp1, Map::kBitFieldOffset));
1432 __ test(temp2, Immediate(1 << Map::kIsUndetectable));
1433 __ j(not_zero, is_not_object);
1434
1435 __ movzx_b(temp2, FieldOperand(temp1, Map::kInstanceTypeOffset));
1436 __ cmp(temp2, FIRST_JS_OBJECT_TYPE);
1437 __ j(below, is_not_object);
1438 __ cmp(temp2, LAST_JS_OBJECT_TYPE);
1439 return below_equal;
1440}
1441
1442
1443void LCodeGen::DoIsObject(LIsObject* instr) {
1444 Register reg = ToRegister(instr->input());
1445 Register result = ToRegister(instr->result());
1446 Register temp = ToRegister(instr->temp());
1447 Label is_false, is_true, done;
1448
1449 Condition true_cond = EmitIsObject(reg, result, temp, &is_false, &is_true);
1450 __ j(true_cond, &is_true);
1451
1452 __ bind(&is_false);
1453 __ mov(result, Handle<Object>(Heap::false_value()));
1454 __ jmp(&done);
1455
1456 __ bind(&is_true);
1457 __ mov(result, Handle<Object>(Heap::true_value()));
1458
1459 __ bind(&done);
1460}
1461
1462
1463void LCodeGen::DoIsObjectAndBranch(LIsObjectAndBranch* instr) {
1464 Register reg = ToRegister(instr->input());
1465 Register temp = ToRegister(instr->temp());
1466 Register temp2 = ToRegister(instr->temp2());
1467
1468 int true_block = chunk_->LookupDestination(instr->true_block_id());
1469 int false_block = chunk_->LookupDestination(instr->false_block_id());
1470 Label* true_label = chunk_->GetAssemblyLabel(true_block);
1471 Label* false_label = chunk_->GetAssemblyLabel(false_block);
1472
1473 Condition true_cond = EmitIsObject(reg, temp, temp2, false_label, true_label);
1474
1475 EmitBranch(true_block, false_block, true_cond);
1476}
1477
1478
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001479void LCodeGen::DoIsSmi(LIsSmi* instr) {
1480 Operand input = ToOperand(instr->input());
1481 Register result = ToRegister(instr->result());
1482
1483 ASSERT(instr->hydrogen()->value()->representation().IsTagged());
1484 __ test(input, Immediate(kSmiTagMask));
1485 __ mov(result, Handle<Object>(Heap::true_value()));
1486 NearLabel done;
1487 __ j(zero, &done);
1488 __ mov(result, Handle<Object>(Heap::false_value()));
1489 __ bind(&done);
1490}
1491
1492
1493void LCodeGen::DoIsSmiAndBranch(LIsSmiAndBranch* instr) {
1494 Operand input = ToOperand(instr->input());
1495
1496 int true_block = chunk_->LookupDestination(instr->true_block_id());
1497 int false_block = chunk_->LookupDestination(instr->false_block_id());
1498
1499 __ test(input, Immediate(kSmiTagMask));
1500 EmitBranch(true_block, false_block, zero);
1501}
1502
1503
1504InstanceType LHasInstanceType::TestType() {
1505 InstanceType from = hydrogen()->from();
1506 InstanceType to = hydrogen()->to();
1507 if (from == FIRST_TYPE) return to;
1508 ASSERT(from == to || to == LAST_TYPE);
1509 return from;
1510}
1511
1512
1513
1514Condition LHasInstanceType::BranchCondition() {
1515 InstanceType from = hydrogen()->from();
1516 InstanceType to = hydrogen()->to();
1517 if (from == to) return equal;
1518 if (to == LAST_TYPE) return above_equal;
1519 if (from == FIRST_TYPE) return below_equal;
1520 UNREACHABLE();
1521 return equal;
1522}
1523
1524
1525void LCodeGen::DoHasInstanceType(LHasInstanceType* instr) {
1526 Register input = ToRegister(instr->input());
1527 Register result = ToRegister(instr->result());
1528
1529 ASSERT(instr->hydrogen()->value()->representation().IsTagged());
1530 __ test(input, Immediate(kSmiTagMask));
1531 NearLabel done, is_false;
1532 __ j(zero, &is_false);
1533 __ CmpObjectType(input, instr->TestType(), result);
1534 __ j(NegateCondition(instr->BranchCondition()), &is_false);
1535 __ mov(result, Handle<Object>(Heap::true_value()));
1536 __ jmp(&done);
1537 __ bind(&is_false);
1538 __ mov(result, Handle<Object>(Heap::false_value()));
1539 __ bind(&done);
1540}
1541
1542
1543void LCodeGen::DoHasInstanceTypeAndBranch(LHasInstanceTypeAndBranch* instr) {
1544 Register input = ToRegister(instr->input());
1545 Register temp = ToRegister(instr->temp());
1546
1547 int true_block = chunk_->LookupDestination(instr->true_block_id());
1548 int false_block = chunk_->LookupDestination(instr->false_block_id());
1549
1550 Label* false_label = chunk_->GetAssemblyLabel(false_block);
1551
1552 __ test(input, Immediate(kSmiTagMask));
1553 __ j(zero, false_label);
1554
1555 __ CmpObjectType(input, instr->TestType(), temp);
1556 EmitBranch(true_block, false_block, instr->BranchCondition());
1557}
1558
1559
1560void LCodeGen::DoHasCachedArrayIndex(LHasCachedArrayIndex* instr) {
1561 Register input = ToRegister(instr->input());
1562 Register result = ToRegister(instr->result());
1563
1564 ASSERT(instr->hydrogen()->value()->representation().IsTagged());
1565 __ mov(result, Handle<Object>(Heap::true_value()));
1566 __ test(FieldOperand(input, String::kHashFieldOffset),
1567 Immediate(String::kContainsCachedArrayIndexMask));
1568 NearLabel done;
1569 __ j(not_zero, &done);
1570 __ mov(result, Handle<Object>(Heap::false_value()));
1571 __ bind(&done);
1572}
1573
1574
1575void LCodeGen::DoHasCachedArrayIndexAndBranch(
1576 LHasCachedArrayIndexAndBranch* instr) {
1577 Register input = ToRegister(instr->input());
1578
1579 int true_block = chunk_->LookupDestination(instr->true_block_id());
1580 int false_block = chunk_->LookupDestination(instr->false_block_id());
1581
1582 __ test(FieldOperand(input, String::kHashFieldOffset),
1583 Immediate(String::kContainsCachedArrayIndexMask));
1584 EmitBranch(true_block, false_block, not_equal);
1585}
1586
1587
1588// Branches to a label or falls through with the answer in the z flag. Trashes
1589// the temp registers, but not the input. Only input and temp2 may alias.
1590void LCodeGen::EmitClassOfTest(Label* is_true,
1591 Label* is_false,
1592 Handle<String>class_name,
1593 Register input,
1594 Register temp,
1595 Register temp2) {
1596 ASSERT(!input.is(temp));
1597 ASSERT(!temp.is(temp2)); // But input and temp2 may be the same register.
1598 __ test(input, Immediate(kSmiTagMask));
1599 __ j(zero, is_false);
1600 __ CmpObjectType(input, FIRST_JS_OBJECT_TYPE, temp);
1601 __ j(below, is_false);
1602
1603 // Map is now in temp.
1604 // Functions have class 'Function'.
1605 __ CmpInstanceType(temp, JS_FUNCTION_TYPE);
1606 if (class_name->IsEqualTo(CStrVector("Function"))) {
1607 __ j(equal, is_true);
1608 } else {
1609 __ j(equal, is_false);
1610 }
1611
1612 // Check if the constructor in the map is a function.
1613 __ mov(temp, FieldOperand(temp, Map::kConstructorOffset));
1614
1615 // As long as JS_FUNCTION_TYPE is the last instance type and it is
1616 // right after LAST_JS_OBJECT_TYPE, we can avoid checking for
1617 // LAST_JS_OBJECT_TYPE.
1618 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
1619 ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
1620
1621 // Objects with a non-function constructor have class 'Object'.
1622 __ CmpObjectType(temp, JS_FUNCTION_TYPE, temp2);
1623 if (class_name->IsEqualTo(CStrVector("Object"))) {
1624 __ j(not_equal, is_true);
1625 } else {
1626 __ j(not_equal, is_false);
1627 }
1628
1629 // temp now contains the constructor function. Grab the
1630 // instance class name from there.
1631 __ mov(temp, FieldOperand(temp, JSFunction::kSharedFunctionInfoOffset));
1632 __ mov(temp, FieldOperand(temp,
1633 SharedFunctionInfo::kInstanceClassNameOffset));
1634 // The class name we are testing against is a symbol because it's a literal.
1635 // The name in the constructor is a symbol because of the way the context is
1636 // booted. This routine isn't expected to work for random API-created
1637 // classes and it doesn't have to because you can't access it with natives
1638 // syntax. Since both sides are symbols it is sufficient to use an identity
1639 // comparison.
1640 __ cmp(temp, class_name);
1641 // End with the answer in the z flag.
1642}
1643
1644
1645void LCodeGen::DoClassOfTest(LClassOfTest* instr) {
1646 Register input = ToRegister(instr->input());
1647 Register result = ToRegister(instr->result());
1648 ASSERT(input.is(result));
1649 Register temp = ToRegister(instr->temporary());
1650 Handle<String> class_name = instr->hydrogen()->class_name();
1651 NearLabel done;
1652 Label is_true, is_false;
1653
1654 EmitClassOfTest(&is_true, &is_false, class_name, input, temp, input);
1655
1656 __ j(not_equal, &is_false);
1657
1658 __ bind(&is_true);
1659 __ mov(result, Handle<Object>(Heap::true_value()));
1660 __ jmp(&done);
1661
1662 __ bind(&is_false);
1663 __ mov(result, Handle<Object>(Heap::false_value()));
1664 __ bind(&done);
1665}
1666
1667
1668void LCodeGen::DoClassOfTestAndBranch(LClassOfTestAndBranch* instr) {
1669 Register input = ToRegister(instr->input());
1670 Register temp = ToRegister(instr->temporary());
1671 Register temp2 = ToRegister(instr->temporary2());
1672 if (input.is(temp)) {
1673 // Swap.
1674 Register swapper = temp;
1675 temp = temp2;
1676 temp2 = swapper;
1677 }
1678 Handle<String> class_name = instr->hydrogen()->class_name();
1679
1680 int true_block = chunk_->LookupDestination(instr->true_block_id());
1681 int false_block = chunk_->LookupDestination(instr->false_block_id());
1682
1683 Label* true_label = chunk_->GetAssemblyLabel(true_block);
1684 Label* false_label = chunk_->GetAssemblyLabel(false_block);
1685
1686 EmitClassOfTest(true_label, false_label, class_name, input, temp, temp2);
1687
1688 EmitBranch(true_block, false_block, equal);
1689}
1690
1691
1692void LCodeGen::DoCmpMapAndBranch(LCmpMapAndBranch* instr) {
1693 Register reg = ToRegister(instr->input());
1694 int true_block = instr->true_block_id();
1695 int false_block = instr->false_block_id();
1696
1697 __ cmp(FieldOperand(reg, HeapObject::kMapOffset), instr->map());
1698 EmitBranch(true_block, false_block, equal);
1699}
1700
1701
1702void LCodeGen::DoInstanceOf(LInstanceOf* instr) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001703 // Object and function are in fixed registers eax and edx.
1704 InstanceofStub stub(InstanceofStub::kArgsInRegisters);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001705 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
1706
1707 NearLabel true_value, done;
1708 __ test(eax, Operand(eax));
1709 __ j(zero, &true_value);
1710 __ mov(ToRegister(instr->result()), Factory::false_value());
1711 __ jmp(&done);
1712 __ bind(&true_value);
1713 __ mov(ToRegister(instr->result()), Factory::true_value());
1714 __ bind(&done);
1715}
1716
1717
1718void LCodeGen::DoInstanceOfAndBranch(LInstanceOfAndBranch* instr) {
1719 int true_block = chunk_->LookupDestination(instr->true_block_id());
1720 int false_block = chunk_->LookupDestination(instr->false_block_id());
1721
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001722 InstanceofStub stub(InstanceofStub::kArgsInRegisters);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001723 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
1724 __ test(eax, Operand(eax));
1725 EmitBranch(true_block, false_block, zero);
1726}
1727
1728
1729static Condition ComputeCompareCondition(Token::Value op) {
1730 switch (op) {
1731 case Token::EQ_STRICT:
1732 case Token::EQ:
1733 return equal;
1734 case Token::LT:
1735 return less;
1736 case Token::GT:
1737 return greater;
1738 case Token::LTE:
1739 return less_equal;
1740 case Token::GTE:
1741 return greater_equal;
1742 default:
1743 UNREACHABLE();
1744 return no_condition;
1745 }
1746}
1747
1748
1749void LCodeGen::DoCmpT(LCmpT* instr) {
1750 Token::Value op = instr->op();
1751
1752 Handle<Code> ic = CompareIC::GetUninitialized(op);
1753 CallCode(ic, RelocInfo::CODE_TARGET, instr);
1754
1755 Condition condition = ComputeCompareCondition(op);
1756 if (op == Token::GT || op == Token::LTE) {
1757 condition = ReverseCondition(condition);
1758 }
1759 NearLabel true_value, done;
1760 __ test(eax, Operand(eax));
1761 __ j(condition, &true_value);
1762 __ mov(ToRegister(instr->result()), Factory::false_value());
1763 __ jmp(&done);
1764 __ bind(&true_value);
1765 __ mov(ToRegister(instr->result()), Factory::true_value());
1766 __ bind(&done);
1767}
1768
1769
1770void LCodeGen::DoCmpTAndBranch(LCmpTAndBranch* instr) {
1771 Token::Value op = instr->op();
1772 int true_block = chunk_->LookupDestination(instr->true_block_id());
1773 int false_block = chunk_->LookupDestination(instr->false_block_id());
1774
1775 Handle<Code> ic = CompareIC::GetUninitialized(op);
1776 CallCode(ic, RelocInfo::CODE_TARGET, instr);
1777
1778 // The compare stub expects compare condition and the input operands
1779 // reversed for GT and LTE.
1780 Condition condition = ComputeCompareCondition(op);
1781 if (op == Token::GT || op == Token::LTE) {
1782 condition = ReverseCondition(condition);
1783 }
1784 __ test(eax, Operand(eax));
1785 EmitBranch(true_block, false_block, condition);
1786}
1787
1788
1789void LCodeGen::DoReturn(LReturn* instr) {
1790 if (FLAG_trace) {
1791 // Preserve the return value on the stack and rely on the runtime
1792 // call to return the value in the same register.
1793 __ push(eax);
1794 __ CallRuntime(Runtime::kTraceExit, 1);
1795 }
1796 __ mov(esp, ebp);
1797 __ pop(ebp);
1798 __ ret((ParameterCount() + 1) * kPointerSize);
1799}
1800
1801
1802void LCodeGen::DoLoadGlobal(LLoadGlobal* instr) {
1803 Register result = ToRegister(instr->result());
1804 __ mov(result, Operand::Cell(instr->hydrogen()->cell()));
1805 if (instr->hydrogen()->check_hole_value()) {
1806 __ cmp(result, Factory::the_hole_value());
1807 DeoptimizeIf(equal, instr->environment());
1808 }
1809}
1810
1811
1812void LCodeGen::DoStoreGlobal(LStoreGlobal* instr) {
1813 Register value = ToRegister(instr->input());
1814 __ mov(Operand::Cell(instr->hydrogen()->cell()), value);
1815}
1816
1817
1818void LCodeGen::DoLoadNamedField(LLoadNamedField* instr) {
1819 Register object = ToRegister(instr->input());
1820 Register result = ToRegister(instr->result());
1821 if (instr->hydrogen()->is_in_object()) {
1822 __ mov(result, FieldOperand(object, instr->hydrogen()->offset()));
1823 } else {
1824 __ mov(result, FieldOperand(object, JSObject::kPropertiesOffset));
1825 __ mov(result, FieldOperand(result, instr->hydrogen()->offset()));
1826 }
1827}
1828
1829
1830void LCodeGen::DoLoadNamedGeneric(LLoadNamedGeneric* instr) {
1831 ASSERT(ToRegister(instr->object()).is(eax));
1832 ASSERT(ToRegister(instr->result()).is(eax));
1833
1834 __ mov(ecx, instr->name());
1835 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
1836 CallCode(ic, RelocInfo::CODE_TARGET, instr);
1837}
1838
1839
1840void LCodeGen::DoLoadElements(LLoadElements* instr) {
1841 ASSERT(instr->result()->Equals(instr->input()));
1842 Register reg = ToRegister(instr->input());
1843 __ mov(reg, FieldOperand(reg, JSObject::kElementsOffset));
1844 if (FLAG_debug_code) {
1845 NearLabel done;
1846 __ cmp(FieldOperand(reg, HeapObject::kMapOffset),
1847 Immediate(Factory::fixed_array_map()));
1848 __ j(equal, &done);
1849 __ cmp(FieldOperand(reg, HeapObject::kMapOffset),
1850 Immediate(Factory::fixed_cow_array_map()));
1851 __ Check(equal, "Check for fast elements failed.");
1852 __ bind(&done);
1853 }
1854}
1855
1856
1857void LCodeGen::DoAccessArgumentsAt(LAccessArgumentsAt* instr) {
1858 Register arguments = ToRegister(instr->arguments());
1859 Register length = ToRegister(instr->length());
1860 Operand index = ToOperand(instr->index());
1861 Register result = ToRegister(instr->result());
1862
1863 __ sub(length, index);
1864 DeoptimizeIf(below_equal, instr->environment());
1865
1866 __ mov(result, Operand(arguments, length, times_4, kPointerSize));
1867}
1868
1869
1870void LCodeGen::DoLoadKeyedFastElement(LLoadKeyedFastElement* instr) {
1871 Register elements = ToRegister(instr->elements());
1872 Register key = ToRegister(instr->key());
1873 Register result;
1874 if (instr->load_result() != NULL) {
1875 result = ToRegister(instr->load_result());
1876 } else {
1877 result = ToRegister(instr->result());
1878 ASSERT(result.is(elements));
1879 }
1880
1881 // Load the result.
1882 __ mov(result, FieldOperand(elements, key, times_4, FixedArray::kHeaderSize));
1883
1884 Representation r = instr->hydrogen()->representation();
1885 if (r.IsInteger32()) {
1886 // Untag and check for smi.
1887 __ SmiUntag(result);
1888 DeoptimizeIf(carry, instr->environment());
1889 } else if (r.IsDouble()) {
1890 EmitNumberUntagD(result,
1891 ToDoubleRegister(instr->result()),
1892 instr->environment());
1893 } else {
1894 // Check for the hole value.
1895 ASSERT(r.IsTagged());
1896 __ cmp(result, Factory::the_hole_value());
1897 DeoptimizeIf(equal, instr->environment());
1898 }
1899}
1900
1901
1902void LCodeGen::DoLoadKeyedGeneric(LLoadKeyedGeneric* instr) {
1903 ASSERT(ToRegister(instr->object()).is(edx));
1904 ASSERT(ToRegister(instr->key()).is(eax));
1905
1906 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
1907 CallCode(ic, RelocInfo::CODE_TARGET, instr);
1908}
1909
1910
1911void LCodeGen::DoArgumentsElements(LArgumentsElements* instr) {
1912 Register result = ToRegister(instr->result());
1913
1914 // Check for arguments adapter frame.
1915 Label done, adapted;
1916 __ mov(result, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
1917 __ mov(result, Operand(result, StandardFrameConstants::kContextOffset));
1918 __ cmp(Operand(result),
1919 Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
1920 __ j(equal, &adapted);
1921
1922 // No arguments adaptor frame.
1923 __ mov(result, Operand(ebp));
1924 __ jmp(&done);
1925
1926 // Arguments adaptor frame present.
1927 __ bind(&adapted);
1928 __ mov(result, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
1929
1930 // Done. Pointer to topmost argument is in result.
1931 __ bind(&done);
1932}
1933
1934
1935void LCodeGen::DoArgumentsLength(LArgumentsLength* instr) {
1936 Operand elem = ToOperand(instr->input());
1937 Register result = ToRegister(instr->result());
1938
1939 Label done;
1940
1941 // No arguments adaptor frame. Number of arguments is fixed.
1942 __ cmp(ebp, elem);
1943 __ mov(result, Immediate(scope()->num_parameters()));
1944 __ j(equal, &done);
1945
1946 // Arguments adaptor frame present. Get argument length from there.
1947 __ mov(result, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
1948 __ mov(result, Operand(result,
1949 ArgumentsAdaptorFrameConstants::kLengthOffset));
1950 __ SmiUntag(result);
1951
1952 // Done. Argument length is in result register.
1953 __ bind(&done);
1954}
1955
1956
1957void LCodeGen::DoApplyArguments(LApplyArguments* instr) {
1958 Register receiver = ToRegister(instr->receiver());
1959 ASSERT(ToRegister(instr->function()).is(edi));
1960 ASSERT(ToRegister(instr->result()).is(eax));
1961
1962 // If the receiver is null or undefined, we have to pass the
1963 // global object as a receiver.
1964 NearLabel global_receiver, receiver_ok;
1965 __ cmp(receiver, Factory::null_value());
1966 __ j(equal, &global_receiver);
1967 __ cmp(receiver, Factory::undefined_value());
1968 __ j(not_equal, &receiver_ok);
1969 __ bind(&global_receiver);
1970 __ mov(receiver, GlobalObjectOperand());
1971 __ bind(&receiver_ok);
1972
1973 Register length = ToRegister(instr->length());
1974 Register elements = ToRegister(instr->elements());
1975
1976 Label invoke;
1977
1978 // Copy the arguments to this function possibly from the
1979 // adaptor frame below it.
1980 const uint32_t kArgumentsLimit = 1 * KB;
1981 __ cmp(length, kArgumentsLimit);
1982 DeoptimizeIf(above, instr->environment());
1983
1984 __ push(receiver);
1985 __ mov(receiver, length);
1986
1987 // Loop through the arguments pushing them onto the execution
1988 // stack.
1989 Label loop;
1990 // length is a small non-negative integer, due to the test above.
1991 __ test(length, Operand(length));
1992 __ j(zero, &invoke);
1993 __ bind(&loop);
1994 __ push(Operand(elements, length, times_pointer_size, 1 * kPointerSize));
1995 __ dec(length);
1996 __ j(not_zero, &loop);
1997
1998 // Invoke the function.
1999 __ bind(&invoke);
2000 ASSERT(receiver.is(eax));
2001 v8::internal::ParameterCount actual(eax);
2002 SafepointGenerator safepoint_generator(this,
2003 instr->pointer_map(),
2004 Safepoint::kNoDeoptimizationIndex);
2005 __ InvokeFunction(edi, actual, CALL_FUNCTION, &safepoint_generator);
2006}
2007
2008
2009void LCodeGen::DoPushArgument(LPushArgument* instr) {
2010 LOperand* argument = instr->input();
2011 if (argument->IsConstantOperand()) {
2012 __ push(ToImmediate(argument));
2013 } else {
2014 __ push(ToOperand(argument));
2015 }
2016}
2017
2018
2019void LCodeGen::DoGlobalObject(LGlobalObject* instr) {
2020 Register result = ToRegister(instr->result());
2021 __ mov(result, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
2022}
2023
2024
2025void LCodeGen::DoGlobalReceiver(LGlobalReceiver* instr) {
2026 Register result = ToRegister(instr->result());
2027 __ mov(result, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
2028 __ mov(result, FieldOperand(result, GlobalObject::kGlobalReceiverOffset));
2029}
2030
2031
2032void LCodeGen::CallKnownFunction(Handle<JSFunction> function,
2033 int arity,
2034 LInstruction* instr) {
2035 // Change context if needed.
2036 bool change_context =
2037 (graph()->info()->closure()->context() != function->context()) ||
2038 scope()->contains_with() ||
2039 (scope()->num_heap_slots() > 0);
2040 if (change_context) {
2041 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
2042 }
2043
2044 // Set eax to arguments count if adaption is not needed. Assumes that eax
2045 // is available to write to at this point.
2046 if (!function->NeedsArgumentsAdaption()) {
2047 __ mov(eax, arity);
2048 }
2049
2050 LPointerMap* pointers = instr->pointer_map();
2051 RecordPosition(pointers->position());
2052
2053 // Invoke function.
2054 if (*function == *graph()->info()->closure()) {
2055 __ CallSelf();
2056 } else {
2057 __ call(FieldOperand(edi, JSFunction::kCodeEntryOffset));
2058 }
2059
2060 // Setup deoptimization.
2061 RegisterLazyDeoptimization(instr);
2062
2063 // Restore context.
2064 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
2065}
2066
2067
2068void LCodeGen::DoCallConstantFunction(LCallConstantFunction* instr) {
2069 ASSERT(ToRegister(instr->result()).is(eax));
2070 __ mov(edi, instr->function());
2071 CallKnownFunction(instr->function(), instr->arity(), instr);
2072}
2073
2074
2075void LCodeGen::DoDeferredMathAbsTaggedHeapNumber(LUnaryMathOperation* instr) {
2076 Register input_reg = ToRegister(instr->input());
2077 __ cmp(FieldOperand(input_reg, HeapObject::kMapOffset),
2078 Factory::heap_number_map());
2079 DeoptimizeIf(not_equal, instr->environment());
2080
2081 Label done;
2082 Register tmp = input_reg.is(eax) ? ecx : eax;
2083 Register tmp2 = tmp.is(ecx) ? edx : input_reg.is(ecx) ? edx : ecx;
2084
2085 // Preserve the value of all registers.
2086 __ PushSafepointRegisters();
2087
2088 Label negative;
2089 __ mov(tmp, FieldOperand(input_reg, HeapNumber::kExponentOffset));
2090 // Check the sign of the argument. If the argument is positive,
2091 // just return it.
2092 __ test(tmp, Immediate(HeapNumber::kSignMask));
2093 __ j(not_zero, &negative);
2094 __ mov(tmp, input_reg);
2095 __ jmp(&done);
2096
2097 __ bind(&negative);
2098
2099 Label allocated, slow;
2100 __ AllocateHeapNumber(tmp, tmp2, no_reg, &slow);
2101 __ jmp(&allocated);
2102
2103 // Slow case: Call the runtime system to do the number allocation.
2104 __ bind(&slow);
2105
2106 __ CallRuntimeSaveDoubles(Runtime::kAllocateHeapNumber);
2107 RecordSafepointWithRegisters(
2108 instr->pointer_map(), 0, Safepoint::kNoDeoptimizationIndex);
2109 // Set the pointer to the new heap number in tmp.
2110 if (!tmp.is(eax)) __ mov(tmp, eax);
2111
2112 // Restore input_reg after call to runtime.
2113 __ mov(input_reg, Operand(esp, EspIndexForPushAll(input_reg) * kPointerSize));
2114
2115 __ bind(&allocated);
2116 __ mov(tmp2, FieldOperand(input_reg, HeapNumber::kExponentOffset));
2117 __ and_(tmp2, ~HeapNumber::kSignMask);
2118 __ mov(FieldOperand(tmp, HeapNumber::kExponentOffset), tmp2);
2119 __ mov(tmp2, FieldOperand(input_reg, HeapNumber::kMantissaOffset));
2120 __ mov(FieldOperand(tmp, HeapNumber::kMantissaOffset), tmp2);
2121
2122 __ bind(&done);
2123 __ mov(Operand(esp, EspIndexForPushAll(input_reg) * kPointerSize), tmp);
2124
2125 __ PopSafepointRegisters();
2126}
2127
2128
2129void LCodeGen::DoMathAbs(LUnaryMathOperation* instr) {
2130 // Class for deferred case.
2131 class DeferredMathAbsTaggedHeapNumber: public LDeferredCode {
2132 public:
2133 DeferredMathAbsTaggedHeapNumber(LCodeGen* codegen,
2134 LUnaryMathOperation* instr)
2135 : LDeferredCode(codegen), instr_(instr) { }
2136 virtual void Generate() {
2137 codegen()->DoDeferredMathAbsTaggedHeapNumber(instr_);
2138 }
2139 private:
2140 LUnaryMathOperation* instr_;
2141 };
2142
2143 ASSERT(instr->input()->Equals(instr->result()));
2144 Representation r = instr->hydrogen()->value()->representation();
2145
2146 if (r.IsDouble()) {
2147 XMMRegister scratch = xmm0;
2148 XMMRegister input_reg = ToDoubleRegister(instr->input());
2149 __ pxor(scratch, scratch);
2150 __ subsd(scratch, input_reg);
2151 __ pand(input_reg, scratch);
2152 } else if (r.IsInteger32()) {
2153 Register input_reg = ToRegister(instr->input());
2154 __ test(input_reg, Operand(input_reg));
2155 Label is_positive;
2156 __ j(not_sign, &is_positive);
2157 __ neg(input_reg);
2158 __ test(input_reg, Operand(input_reg));
2159 DeoptimizeIf(negative, instr->environment());
2160 __ bind(&is_positive);
2161 } else { // Tagged case.
2162 DeferredMathAbsTaggedHeapNumber* deferred =
2163 new DeferredMathAbsTaggedHeapNumber(this, instr);
2164 Label not_smi;
2165 Register input_reg = ToRegister(instr->input());
2166 // Smi check.
2167 __ test(input_reg, Immediate(kSmiTagMask));
2168 __ j(not_zero, deferred->entry());
2169 __ test(input_reg, Operand(input_reg));
2170 Label is_positive;
2171 __ j(not_sign, &is_positive);
2172 __ neg(input_reg);
2173
2174 __ test(input_reg, Operand(input_reg));
2175 DeoptimizeIf(negative, instr->environment());
2176
2177 __ bind(&is_positive);
2178 __ bind(deferred->exit());
2179 }
2180}
2181
2182
2183void LCodeGen::DoMathFloor(LUnaryMathOperation* instr) {
2184 XMMRegister xmm_scratch = xmm0;
2185 Register output_reg = ToRegister(instr->result());
2186 XMMRegister input_reg = ToDoubleRegister(instr->input());
2187 __ xorpd(xmm_scratch, xmm_scratch); // Zero the register.
2188 __ ucomisd(input_reg, xmm_scratch);
2189
2190 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
2191 DeoptimizeIf(below_equal, instr->environment());
2192 } else {
2193 DeoptimizeIf(below, instr->environment());
2194 }
2195
2196 // Use truncating instruction (OK because input is positive).
2197 __ cvttsd2si(output_reg, Operand(input_reg));
2198
2199 // Overflow is signalled with minint.
2200 __ cmp(output_reg, 0x80000000u);
2201 DeoptimizeIf(equal, instr->environment());
2202}
2203
2204
2205void LCodeGen::DoMathRound(LUnaryMathOperation* instr) {
2206 XMMRegister xmm_scratch = xmm0;
2207 Register output_reg = ToRegister(instr->result());
2208 XMMRegister input_reg = ToDoubleRegister(instr->input());
2209
2210 // xmm_scratch = 0.5
2211 ExternalReference one_half = ExternalReference::address_of_one_half();
2212 __ movdbl(xmm_scratch, Operand::StaticVariable(one_half));
2213
2214 // input = input + 0.5
2215 __ addsd(input_reg, xmm_scratch);
2216
2217 // We need to return -0 for the input range [-0.5, 0[, otherwise
2218 // compute Math.floor(value + 0.5).
2219 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
2220 __ ucomisd(input_reg, xmm_scratch);
2221 DeoptimizeIf(below_equal, instr->environment());
2222 } else {
2223 // If we don't need to bailout on -0, we check only bailout
2224 // on negative inputs.
2225 __ xorpd(xmm_scratch, xmm_scratch); // Zero the register.
2226 __ ucomisd(input_reg, xmm_scratch);
2227 DeoptimizeIf(below, instr->environment());
2228 }
2229
2230 // Compute Math.floor(value + 0.5).
2231 // Use truncating instruction (OK because input is positive).
2232 __ cvttsd2si(output_reg, Operand(input_reg));
2233
2234 // Overflow is signalled with minint.
2235 __ cmp(output_reg, 0x80000000u);
2236 DeoptimizeIf(equal, instr->environment());
2237}
2238
2239
2240void LCodeGen::DoMathSqrt(LUnaryMathOperation* instr) {
2241 XMMRegister input_reg = ToDoubleRegister(instr->input());
2242 ASSERT(ToDoubleRegister(instr->result()).is(input_reg));
2243 __ sqrtsd(input_reg, input_reg);
2244}
2245
2246
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002247void LCodeGen::DoMathPowHalf(LUnaryMathOperation* instr) {
2248 XMMRegister xmm_scratch = xmm0;
2249 XMMRegister input_reg = ToDoubleRegister(instr->input());
2250 ASSERT(ToDoubleRegister(instr->result()).is(input_reg));
2251 ExternalReference negative_infinity =
2252 ExternalReference::address_of_negative_infinity();
2253 __ movdbl(xmm_scratch, Operand::StaticVariable(negative_infinity));
2254 __ ucomisd(xmm_scratch, input_reg);
2255 DeoptimizeIf(equal, instr->environment());
2256 __ sqrtsd(input_reg, input_reg);
2257}
2258
2259
2260void LCodeGen::DoPower(LPower* instr) {
2261 LOperand* left = instr->left();
2262 LOperand* right = instr->right();
2263 DoubleRegister result_reg = ToDoubleRegister(instr->result());
2264 Representation exponent_type = instr->hydrogen()->right()->representation();
2265 if (exponent_type.IsDouble()) {
2266 // It is safe to use ebx directly since the instruction is marked
2267 // as a call.
2268 __ PrepareCallCFunction(4, ebx);
2269 __ movdbl(Operand(esp, 0 * kDoubleSize), ToDoubleRegister(left));
2270 __ movdbl(Operand(esp, 1 * kDoubleSize), ToDoubleRegister(right));
2271 __ CallCFunction(ExternalReference::power_double_double_function(), 4);
2272 } else if (exponent_type.IsInteger32()) {
2273 // It is safe to use ebx directly since the instruction is marked
2274 // as a call.
2275 ASSERT(!ToRegister(right).is(ebx));
2276 __ PrepareCallCFunction(4, ebx);
2277 __ movdbl(Operand(esp, 0 * kDoubleSize), ToDoubleRegister(left));
2278 __ mov(Operand(esp, 1 * kDoubleSize), ToRegister(right));
2279 __ CallCFunction(ExternalReference::power_double_int_function(), 4);
2280 } else {
2281 ASSERT(exponent_type.IsTagged());
2282 CpuFeatures::Scope scope(SSE2);
2283 Register right_reg = ToRegister(right);
2284
2285 Label non_smi, call;
2286 __ test(right_reg, Immediate(kSmiTagMask));
2287 __ j(not_zero, &non_smi);
2288 __ SmiUntag(right_reg);
2289 __ cvtsi2sd(result_reg, Operand(right_reg));
2290 __ jmp(&call);
2291
2292 __ bind(&non_smi);
2293 // It is safe to use ebx directly since the instruction is marked
2294 // as a call.
2295 ASSERT(!right_reg.is(ebx));
2296 __ CmpObjectType(right_reg, HEAP_NUMBER_TYPE , ebx);
2297 DeoptimizeIf(not_equal, instr->environment());
2298 __ movdbl(result_reg, FieldOperand(right_reg, HeapNumber::kValueOffset));
2299
2300 __ bind(&call);
2301 __ PrepareCallCFunction(4, ebx);
2302 __ movdbl(Operand(esp, 0 * kDoubleSize), ToDoubleRegister(left));
2303 __ movdbl(Operand(esp, 1 * kDoubleSize), result_reg);
2304 __ CallCFunction(ExternalReference::power_double_double_function(), 4);
2305 }
2306
2307 // Return value is in st(0) on ia32.
2308 // Store it into the (fixed) result register.
2309 __ sub(Operand(esp), Immediate(kDoubleSize));
2310 __ fstp_d(Operand(esp, 0));
2311 __ movdbl(result_reg, Operand(esp, 0));
2312 __ add(Operand(esp), Immediate(kDoubleSize));
2313}
2314
2315
2316void LCodeGen::DoMathLog(LUnaryMathOperation* instr) {
2317 ASSERT(ToDoubleRegister(instr->result()).is(xmm1));
whesse@chromium.org023421e2010-12-21 12:19:12 +00002318 TranscendentalCacheStub stub(TranscendentalCache::LOG,
2319 TranscendentalCacheStub::UNTAGGED);
2320 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
2321}
2322
2323
2324void LCodeGen::DoMathCos(LUnaryMathOperation* instr) {
2325 ASSERT(ToDoubleRegister(instr->result()).is(xmm1));
2326 TranscendentalCacheStub stub(TranscendentalCache::COS,
2327 TranscendentalCacheStub::UNTAGGED);
2328 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
2329}
2330
2331
2332void LCodeGen::DoMathSin(LUnaryMathOperation* instr) {
2333 ASSERT(ToDoubleRegister(instr->result()).is(xmm1));
2334 TranscendentalCacheStub stub(TranscendentalCache::SIN,
2335 TranscendentalCacheStub::UNTAGGED);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002336 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
2337}
2338
2339
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002340void LCodeGen::DoUnaryMathOperation(LUnaryMathOperation* instr) {
2341 switch (instr->op()) {
2342 case kMathAbs:
2343 DoMathAbs(instr);
2344 break;
2345 case kMathFloor:
2346 DoMathFloor(instr);
2347 break;
2348 case kMathRound:
2349 DoMathRound(instr);
2350 break;
2351 case kMathSqrt:
2352 DoMathSqrt(instr);
2353 break;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002354 case kMathPowHalf:
2355 DoMathPowHalf(instr);
2356 break;
whesse@chromium.org023421e2010-12-21 12:19:12 +00002357 case kMathCos:
2358 DoMathCos(instr);
2359 break;
2360 case kMathSin:
2361 DoMathSin(instr);
2362 break;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002363 case kMathLog:
2364 DoMathLog(instr);
2365 break;
2366
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002367 default:
2368 UNREACHABLE();
2369 }
2370}
2371
2372
2373void LCodeGen::DoCallKeyed(LCallKeyed* instr) {
2374 ASSERT(ToRegister(instr->result()).is(eax));
2375
2376 int arity = instr->arity();
2377 Handle<Code> ic = StubCache::ComputeKeyedCallInitialize(arity, NOT_IN_LOOP);
2378 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2379 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
2380}
2381
2382
2383void LCodeGen::DoCallNamed(LCallNamed* instr) {
2384 ASSERT(ToRegister(instr->result()).is(eax));
2385
2386 int arity = instr->arity();
2387 Handle<Code> ic = StubCache::ComputeCallInitialize(arity, NOT_IN_LOOP);
2388 __ mov(ecx, instr->name());
2389 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2390 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
2391}
2392
2393
2394void LCodeGen::DoCallFunction(LCallFunction* instr) {
2395 ASSERT(ToRegister(instr->result()).is(eax));
2396
2397 int arity = instr->arity();
2398 CallFunctionStub stub(arity, NOT_IN_LOOP, RECEIVER_MIGHT_BE_VALUE);
2399 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
2400 __ Drop(1);
2401 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
2402}
2403
2404
2405void LCodeGen::DoCallGlobal(LCallGlobal* instr) {
2406 ASSERT(ToRegister(instr->result()).is(eax));
2407
2408 int arity = instr->arity();
2409 Handle<Code> ic = StubCache::ComputeCallInitialize(arity, NOT_IN_LOOP);
2410 __ mov(ecx, instr->name());
2411 CallCode(ic, RelocInfo::CODE_TARGET_CONTEXT, instr);
2412 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
2413}
2414
2415
2416void LCodeGen::DoCallKnownGlobal(LCallKnownGlobal* instr) {
2417 ASSERT(ToRegister(instr->result()).is(eax));
2418 __ mov(edi, instr->target());
2419 CallKnownFunction(instr->target(), instr->arity(), instr);
2420}
2421
2422
2423void LCodeGen::DoCallNew(LCallNew* instr) {
2424 ASSERT(ToRegister(instr->input()).is(edi));
2425 ASSERT(ToRegister(instr->result()).is(eax));
2426
2427 Handle<Code> builtin(Builtins::builtin(Builtins::JSConstructCall));
2428 __ Set(eax, Immediate(instr->arity()));
2429 CallCode(builtin, RelocInfo::CONSTRUCT_CALL, instr);
2430}
2431
2432
2433void LCodeGen::DoCallRuntime(LCallRuntime* instr) {
2434 CallRuntime(instr->function(), instr->arity(), instr);
2435}
2436
2437
2438void LCodeGen::DoStoreNamedField(LStoreNamedField* instr) {
2439 Register object = ToRegister(instr->object());
2440 Register value = ToRegister(instr->value());
2441 int offset = instr->offset();
2442
2443 if (!instr->transition().is_null()) {
2444 __ mov(FieldOperand(object, HeapObject::kMapOffset), instr->transition());
2445 }
2446
2447 // Do the store.
2448 if (instr->is_in_object()) {
2449 __ mov(FieldOperand(object, offset), value);
2450 if (instr->needs_write_barrier()) {
2451 Register temp = ToRegister(instr->temp());
2452 // Update the write barrier for the object for in-object properties.
2453 __ RecordWrite(object, offset, value, temp);
2454 }
2455 } else {
2456 Register temp = ToRegister(instr->temp());
2457 __ mov(temp, FieldOperand(object, JSObject::kPropertiesOffset));
2458 __ mov(FieldOperand(temp, offset), value);
2459 if (instr->needs_write_barrier()) {
2460 // Update the write barrier for the properties array.
2461 // object is used as a scratch register.
2462 __ RecordWrite(temp, offset, value, object);
2463 }
2464 }
2465}
2466
2467
2468void LCodeGen::DoStoreNamedGeneric(LStoreNamedGeneric* instr) {
2469 ASSERT(ToRegister(instr->object()).is(edx));
2470 ASSERT(ToRegister(instr->value()).is(eax));
2471
2472 __ mov(ecx, instr->name());
2473 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
2474 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2475}
2476
2477
2478void LCodeGen::DoBoundsCheck(LBoundsCheck* instr) {
2479 __ cmp(ToRegister(instr->index()), ToOperand(instr->length()));
2480 DeoptimizeIf(above_equal, instr->environment());
2481}
2482
2483
2484void LCodeGen::DoStoreKeyedFastElement(LStoreKeyedFastElement* instr) {
2485 Register value = ToRegister(instr->value());
2486 Register elements = ToRegister(instr->object());
2487 Register key = instr->key()->IsRegister() ? ToRegister(instr->key()) : no_reg;
2488
2489 // Do the store.
2490 if (instr->key()->IsConstantOperand()) {
2491 ASSERT(!instr->hydrogen()->NeedsWriteBarrier());
2492 LConstantOperand* const_operand = LConstantOperand::cast(instr->key());
2493 int offset =
2494 ToInteger32(const_operand) * kPointerSize + FixedArray::kHeaderSize;
2495 __ mov(FieldOperand(elements, offset), value);
2496 } else {
2497 __ mov(FieldOperand(elements, key, times_4, FixedArray::kHeaderSize),
2498 value);
2499 }
2500
2501 // Update the write barrier unless we're certain that we're storing a smi.
2502 if (instr->hydrogen()->NeedsWriteBarrier()) {
2503 // Compute address of modified element and store it into key register.
2504 __ lea(key, FieldOperand(elements, key, times_4, FixedArray::kHeaderSize));
2505 __ RecordWrite(elements, key, value);
2506 }
2507}
2508
2509
2510void LCodeGen::DoStoreKeyedGeneric(LStoreKeyedGeneric* instr) {
2511 ASSERT(ToRegister(instr->object()).is(edx));
2512 ASSERT(ToRegister(instr->key()).is(ecx));
2513 ASSERT(ToRegister(instr->value()).is(eax));
2514
2515 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
2516 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2517}
2518
2519
2520void LCodeGen::DoInteger32ToDouble(LInteger32ToDouble* instr) {
2521 LOperand* input = instr->input();
2522 ASSERT(input->IsRegister() || input->IsStackSlot());
2523 LOperand* output = instr->result();
2524 ASSERT(output->IsDoubleRegister());
2525 __ cvtsi2sd(ToDoubleRegister(output), ToOperand(input));
2526}
2527
2528
2529void LCodeGen::DoNumberTagI(LNumberTagI* instr) {
2530 class DeferredNumberTagI: public LDeferredCode {
2531 public:
2532 DeferredNumberTagI(LCodeGen* codegen, LNumberTagI* instr)
2533 : LDeferredCode(codegen), instr_(instr) { }
2534 virtual void Generate() { codegen()->DoDeferredNumberTagI(instr_); }
2535 private:
2536 LNumberTagI* instr_;
2537 };
2538
2539 LOperand* input = instr->input();
2540 ASSERT(input->IsRegister() && input->Equals(instr->result()));
2541 Register reg = ToRegister(input);
2542
2543 DeferredNumberTagI* deferred = new DeferredNumberTagI(this, instr);
2544 __ SmiTag(reg);
2545 __ j(overflow, deferred->entry());
2546 __ bind(deferred->exit());
2547}
2548
2549
2550void LCodeGen::DoDeferredNumberTagI(LNumberTagI* instr) {
2551 Label slow;
2552 Register reg = ToRegister(instr->input());
2553 Register tmp = reg.is(eax) ? ecx : eax;
2554
2555 // Preserve the value of all registers.
2556 __ PushSafepointRegisters();
2557
2558 // There was overflow, so bits 30 and 31 of the original integer
2559 // disagree. Try to allocate a heap number in new space and store
2560 // the value in there. If that fails, call the runtime system.
2561 NearLabel done;
2562 __ SmiUntag(reg);
2563 __ xor_(reg, 0x80000000);
2564 __ cvtsi2sd(xmm0, Operand(reg));
2565 if (FLAG_inline_new) {
2566 __ AllocateHeapNumber(reg, tmp, no_reg, &slow);
2567 __ jmp(&done);
2568 }
2569
2570 // Slow case: Call the runtime system to do the number allocation.
2571 __ bind(&slow);
2572
2573 // TODO(3095996): Put a valid pointer value in the stack slot where the result
2574 // register is stored, as this register is in the pointer map, but contains an
2575 // integer value.
2576 __ mov(Operand(esp, EspIndexForPushAll(reg) * kPointerSize), Immediate(0));
2577
2578 __ CallRuntimeSaveDoubles(Runtime::kAllocateHeapNumber);
2579 RecordSafepointWithRegisters(
2580 instr->pointer_map(), 0, Safepoint::kNoDeoptimizationIndex);
2581 if (!reg.is(eax)) __ mov(reg, eax);
2582
2583 // Done. Put the value in xmm0 into the value of the allocated heap
2584 // number.
2585 __ bind(&done);
2586 __ movdbl(FieldOperand(reg, HeapNumber::kValueOffset), xmm0);
2587 __ mov(Operand(esp, EspIndexForPushAll(reg) * kPointerSize), reg);
2588 __ PopSafepointRegisters();
2589}
2590
2591
2592void LCodeGen::DoNumberTagD(LNumberTagD* instr) {
2593 class DeferredNumberTagD: public LDeferredCode {
2594 public:
2595 DeferredNumberTagD(LCodeGen* codegen, LNumberTagD* instr)
2596 : LDeferredCode(codegen), instr_(instr) { }
2597 virtual void Generate() { codegen()->DoDeferredNumberTagD(instr_); }
2598 private:
2599 LNumberTagD* instr_;
2600 };
2601
2602 XMMRegister input_reg = ToDoubleRegister(instr->input());
2603 Register reg = ToRegister(instr->result());
2604 Register tmp = ToRegister(instr->temp());
2605
2606 DeferredNumberTagD* deferred = new DeferredNumberTagD(this, instr);
2607 if (FLAG_inline_new) {
2608 __ AllocateHeapNumber(reg, tmp, no_reg, deferred->entry());
2609 } else {
2610 __ jmp(deferred->entry());
2611 }
2612 __ bind(deferred->exit());
2613 __ movdbl(FieldOperand(reg, HeapNumber::kValueOffset), input_reg);
2614}
2615
2616
2617void LCodeGen::DoDeferredNumberTagD(LNumberTagD* instr) {
2618 // TODO(3095996): Get rid of this. For now, we need to make the
2619 // result register contain a valid pointer because it is already
2620 // contained in the register pointer map.
2621 Register reg = ToRegister(instr->result());
2622 __ Set(reg, Immediate(0));
2623
2624 __ PushSafepointRegisters();
2625 __ CallRuntimeSaveDoubles(Runtime::kAllocateHeapNumber);
2626 RecordSafepointWithRegisters(
2627 instr->pointer_map(), 0, Safepoint::kNoDeoptimizationIndex);
2628 __ mov(Operand(esp, EspIndexForPushAll(reg) * kPointerSize), eax);
2629 __ PopSafepointRegisters();
2630}
2631
2632
2633void LCodeGen::DoSmiTag(LSmiTag* instr) {
2634 LOperand* input = instr->input();
2635 ASSERT(input->IsRegister() && input->Equals(instr->result()));
2636 ASSERT(!instr->hydrogen_value()->CheckFlag(HValue::kCanOverflow));
2637 __ SmiTag(ToRegister(input));
2638}
2639
2640
2641void LCodeGen::DoSmiUntag(LSmiUntag* instr) {
2642 LOperand* input = instr->input();
2643 ASSERT(input->IsRegister() && input->Equals(instr->result()));
2644 if (instr->needs_check()) {
2645 __ test(ToRegister(input), Immediate(kSmiTagMask));
2646 DeoptimizeIf(not_zero, instr->environment());
2647 }
2648 __ SmiUntag(ToRegister(input));
2649}
2650
2651
2652void LCodeGen::EmitNumberUntagD(Register input_reg,
2653 XMMRegister result_reg,
2654 LEnvironment* env) {
2655 NearLabel load_smi, heap_number, done;
2656
2657 // Smi check.
2658 __ test(input_reg, Immediate(kSmiTagMask));
2659 __ j(zero, &load_smi, not_taken);
2660
2661 // Heap number map check.
2662 __ cmp(FieldOperand(input_reg, HeapObject::kMapOffset),
2663 Factory::heap_number_map());
2664 __ j(equal, &heap_number);
2665
2666 __ cmp(input_reg, Factory::undefined_value());
2667 DeoptimizeIf(not_equal, env);
2668
2669 // Convert undefined to NaN.
2670 __ push(input_reg);
2671 __ mov(input_reg, Factory::nan_value());
2672 __ movdbl(result_reg, FieldOperand(input_reg, HeapNumber::kValueOffset));
2673 __ pop(input_reg);
2674 __ jmp(&done);
2675
2676 // Heap number to XMM conversion.
2677 __ bind(&heap_number);
2678 __ movdbl(result_reg, FieldOperand(input_reg, HeapNumber::kValueOffset));
2679 __ jmp(&done);
2680
2681 // Smi to XMM conversion
2682 __ bind(&load_smi);
2683 __ SmiUntag(input_reg); // Untag smi before converting to float.
2684 __ cvtsi2sd(result_reg, Operand(input_reg));
2685 __ SmiTag(input_reg); // Retag smi.
2686 __ bind(&done);
2687}
2688
2689
2690class DeferredTaggedToI: public LDeferredCode {
2691 public:
2692 DeferredTaggedToI(LCodeGen* codegen, LTaggedToI* instr)
2693 : LDeferredCode(codegen), instr_(instr) { }
2694 virtual void Generate() { codegen()->DoDeferredTaggedToI(instr_); }
2695 private:
2696 LTaggedToI* instr_;
2697};
2698
2699
2700void LCodeGen::DoDeferredTaggedToI(LTaggedToI* instr) {
2701 NearLabel done, heap_number;
2702 Register input_reg = ToRegister(instr->input());
2703
2704 // Heap number map check.
2705 __ cmp(FieldOperand(input_reg, HeapObject::kMapOffset),
2706 Factory::heap_number_map());
2707
2708 if (instr->truncating()) {
2709 __ j(equal, &heap_number);
2710 // Check for undefined. Undefined is converted to zero for truncating
2711 // conversions.
2712 __ cmp(input_reg, Factory::undefined_value());
2713 DeoptimizeIf(not_equal, instr->environment());
2714 __ mov(input_reg, 0);
2715 __ jmp(&done);
2716
2717 __ bind(&heap_number);
2718 if (CpuFeatures::IsSupported(SSE3)) {
2719 CpuFeatures::Scope scope(SSE3);
2720 NearLabel convert;
2721 // Use more powerful conversion when sse3 is available.
2722 // Load x87 register with heap number.
2723 __ fld_d(FieldOperand(input_reg, HeapNumber::kValueOffset));
2724 // Get exponent alone and check for too-big exponent.
2725 __ mov(input_reg, FieldOperand(input_reg, HeapNumber::kExponentOffset));
2726 __ and_(input_reg, HeapNumber::kExponentMask);
2727 const uint32_t kTooBigExponent =
2728 (HeapNumber::kExponentBias + 63) << HeapNumber::kExponentShift;
2729 __ cmp(Operand(input_reg), Immediate(kTooBigExponent));
2730 __ j(less, &convert);
2731 // Pop FPU stack before deoptimizing.
2732 __ ffree(0);
2733 __ fincstp();
2734 DeoptimizeIf(no_condition, instr->environment());
2735
2736 // Reserve space for 64 bit answer.
2737 __ bind(&convert);
2738 __ sub(Operand(esp), Immediate(kDoubleSize));
2739 // Do conversion, which cannot fail because we checked the exponent.
2740 __ fisttp_d(Operand(esp, 0));
2741 __ mov(input_reg, Operand(esp, 0)); // Low word of answer is the result.
2742 __ add(Operand(esp), Immediate(kDoubleSize));
2743 } else {
2744 NearLabel deopt;
2745 XMMRegister xmm_temp = ToDoubleRegister(instr->temp());
2746 __ movdbl(xmm0, FieldOperand(input_reg, HeapNumber::kValueOffset));
2747 __ cvttsd2si(input_reg, Operand(xmm0));
2748 __ cmp(input_reg, 0x80000000u);
2749 __ j(not_equal, &done);
2750 // Check if the input was 0x8000000 (kMinInt).
2751 // If no, then we got an overflow and we deoptimize.
2752 ExternalReference min_int = ExternalReference::address_of_min_int();
2753 __ movdbl(xmm_temp, Operand::StaticVariable(min_int));
2754 __ ucomisd(xmm_temp, xmm0);
2755 DeoptimizeIf(not_equal, instr->environment());
2756 DeoptimizeIf(parity_even, instr->environment()); // NaN.
2757 }
2758 } else {
2759 // Deoptimize if we don't have a heap number.
2760 DeoptimizeIf(not_equal, instr->environment());
2761
2762 XMMRegister xmm_temp = ToDoubleRegister(instr->temp());
2763 __ movdbl(xmm0, FieldOperand(input_reg, HeapNumber::kValueOffset));
2764 __ cvttsd2si(input_reg, Operand(xmm0));
2765 __ cvtsi2sd(xmm_temp, Operand(input_reg));
2766 __ ucomisd(xmm0, xmm_temp);
2767 DeoptimizeIf(not_equal, instr->environment());
2768 DeoptimizeIf(parity_even, instr->environment()); // NaN.
2769 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
2770 __ test(input_reg, Operand(input_reg));
2771 __ j(not_zero, &done);
2772 __ movmskpd(input_reg, xmm0);
2773 __ and_(input_reg, 1);
2774 DeoptimizeIf(not_zero, instr->environment());
2775 }
2776 }
2777 __ bind(&done);
2778}
2779
2780
2781void LCodeGen::DoTaggedToI(LTaggedToI* instr) {
2782 LOperand* input = instr->input();
2783 ASSERT(input->IsRegister());
2784 ASSERT(input->Equals(instr->result()));
2785
2786 Register input_reg = ToRegister(input);
2787
2788 DeferredTaggedToI* deferred = new DeferredTaggedToI(this, instr);
2789
2790 // Smi check.
2791 __ test(input_reg, Immediate(kSmiTagMask));
2792 __ j(not_zero, deferred->entry());
2793
2794 // Smi to int32 conversion
2795 __ SmiUntag(input_reg); // Untag smi.
2796
2797 __ bind(deferred->exit());
2798}
2799
2800
2801void LCodeGen::DoNumberUntagD(LNumberUntagD* instr) {
2802 LOperand* input = instr->input();
2803 ASSERT(input->IsRegister());
2804 LOperand* result = instr->result();
2805 ASSERT(result->IsDoubleRegister());
2806
2807 Register input_reg = ToRegister(input);
2808 XMMRegister result_reg = ToDoubleRegister(result);
2809
2810 EmitNumberUntagD(input_reg, result_reg, instr->environment());
2811}
2812
2813
2814void LCodeGen::DoDoubleToI(LDoubleToI* instr) {
2815 LOperand* input = instr->input();
2816 ASSERT(input->IsDoubleRegister());
2817 LOperand* result = instr->result();
2818 ASSERT(result->IsRegister());
2819
2820 XMMRegister input_reg = ToDoubleRegister(input);
2821 Register result_reg = ToRegister(result);
2822
2823 if (instr->truncating()) {
2824 // Performs a truncating conversion of a floating point number as used by
2825 // the JS bitwise operations.
2826 __ cvttsd2si(result_reg, Operand(input_reg));
2827 __ cmp(result_reg, 0x80000000u);
2828 if (CpuFeatures::IsSupported(SSE3)) {
2829 // This will deoptimize if the exponent of the input in out of range.
2830 CpuFeatures::Scope scope(SSE3);
2831 NearLabel convert, done;
2832 __ j(not_equal, &done);
2833 __ sub(Operand(esp), Immediate(kDoubleSize));
2834 __ movdbl(Operand(esp, 0), input_reg);
2835 // Get exponent alone and check for too-big exponent.
2836 __ mov(result_reg, Operand(esp, sizeof(int32_t)));
2837 __ and_(result_reg, HeapNumber::kExponentMask);
2838 const uint32_t kTooBigExponent =
2839 (HeapNumber::kExponentBias + 63) << HeapNumber::kExponentShift;
2840 __ cmp(Operand(result_reg), Immediate(kTooBigExponent));
2841 __ j(less, &convert);
2842 __ add(Operand(esp), Immediate(kDoubleSize));
2843 DeoptimizeIf(no_condition, instr->environment());
2844 __ bind(&convert);
2845 // Do conversion, which cannot fail because we checked the exponent.
2846 __ fld_d(Operand(esp, 0));
2847 __ fisttp_d(Operand(esp, 0));
2848 __ mov(result_reg, Operand(esp, 0)); // Low word of answer is the result.
2849 __ add(Operand(esp), Immediate(kDoubleSize));
2850 __ bind(&done);
2851 } else {
2852 // This will bail out if the input was not in the int32 range (or,
2853 // unfortunately, if the input was 0x80000000).
2854 DeoptimizeIf(equal, instr->environment());
2855 }
2856 } else {
2857 NearLabel done;
2858 __ cvttsd2si(result_reg, Operand(input_reg));
2859 __ cvtsi2sd(xmm0, Operand(result_reg));
2860 __ ucomisd(xmm0, input_reg);
2861 DeoptimizeIf(not_equal, instr->environment());
2862 DeoptimizeIf(parity_even, instr->environment()); // NaN.
2863 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
2864 // The integer converted back is equal to the original. We
2865 // only have to test if we got -0 as an input.
2866 __ test(result_reg, Operand(result_reg));
2867 __ j(not_zero, &done);
2868 __ movmskpd(result_reg, input_reg);
2869 // Bit 0 contains the sign of the double in input_reg.
2870 // If input was positive, we are ok and return 0, otherwise
2871 // deoptimize.
2872 __ and_(result_reg, 1);
2873 DeoptimizeIf(not_zero, instr->environment());
2874 }
2875 __ bind(&done);
2876 }
2877}
2878
2879
2880void LCodeGen::DoCheckSmi(LCheckSmi* instr) {
2881 LOperand* input = instr->input();
2882 ASSERT(input->IsRegister());
2883 __ test(ToRegister(input), Immediate(kSmiTagMask));
2884 DeoptimizeIf(instr->condition(), instr->environment());
2885}
2886
2887
2888void LCodeGen::DoCheckInstanceType(LCheckInstanceType* instr) {
2889 Register input = ToRegister(instr->input());
2890 Register temp = ToRegister(instr->temp());
2891 InstanceType first = instr->hydrogen()->first();
2892 InstanceType last = instr->hydrogen()->last();
2893
2894 __ test(input, Immediate(kSmiTagMask));
2895 DeoptimizeIf(zero, instr->environment());
2896
2897 __ mov(temp, FieldOperand(input, HeapObject::kMapOffset));
2898 __ cmpb(FieldOperand(temp, Map::kInstanceTypeOffset),
2899 static_cast<int8_t>(first));
2900
2901 // If there is only one type in the interval check for equality.
2902 if (first == last) {
2903 DeoptimizeIf(not_equal, instr->environment());
2904 } else {
2905 DeoptimizeIf(below, instr->environment());
2906 // Omit check for the last type.
2907 if (last != LAST_TYPE) {
2908 __ cmpb(FieldOperand(temp, Map::kInstanceTypeOffset),
2909 static_cast<int8_t>(last));
2910 DeoptimizeIf(above, instr->environment());
2911 }
2912 }
2913}
2914
2915
2916void LCodeGen::DoCheckFunction(LCheckFunction* instr) {
2917 ASSERT(instr->input()->IsRegister());
2918 Register reg = ToRegister(instr->input());
2919 __ cmp(reg, instr->hydrogen()->target());
2920 DeoptimizeIf(not_equal, instr->environment());
2921}
2922
2923
2924void LCodeGen::DoCheckMap(LCheckMap* instr) {
2925 LOperand* input = instr->input();
2926 ASSERT(input->IsRegister());
2927 Register reg = ToRegister(input);
2928 __ cmp(FieldOperand(reg, HeapObject::kMapOffset),
2929 instr->hydrogen()->map());
2930 DeoptimizeIf(not_equal, instr->environment());
2931}
2932
2933
2934void LCodeGen::LoadPrototype(Register result, Handle<JSObject> prototype) {
2935 if (Heap::InNewSpace(*prototype)) {
2936 Handle<JSGlobalPropertyCell> cell =
2937 Factory::NewJSGlobalPropertyCell(prototype);
2938 __ mov(result, Operand::Cell(cell));
2939 } else {
2940 __ mov(result, prototype);
2941 }
2942}
2943
2944
2945void LCodeGen::DoCheckPrototypeMaps(LCheckPrototypeMaps* instr) {
2946 Register reg = ToRegister(instr->temp());
2947
2948 Handle<JSObject> holder = instr->holder();
2949 Handle<Map> receiver_map = instr->receiver_map();
2950 Handle<JSObject> current_prototype(JSObject::cast(receiver_map->prototype()));
2951
2952 // Load prototype object.
2953 LoadPrototype(reg, current_prototype);
2954
2955 // Check prototype maps up to the holder.
2956 while (!current_prototype.is_identical_to(holder)) {
2957 __ cmp(FieldOperand(reg, HeapObject::kMapOffset),
2958 Handle<Map>(current_prototype->map()));
2959 DeoptimizeIf(not_equal, instr->environment());
2960 current_prototype =
2961 Handle<JSObject>(JSObject::cast(current_prototype->GetPrototype()));
2962 // Load next prototype object.
2963 LoadPrototype(reg, current_prototype);
2964 }
2965
2966 // Check the holder map.
2967 __ cmp(FieldOperand(reg, HeapObject::kMapOffset),
2968 Handle<Map>(current_prototype->map()));
2969 DeoptimizeIf(not_equal, instr->environment());
2970}
2971
2972
2973void LCodeGen::DoArrayLiteral(LArrayLiteral* instr) {
2974 // Setup the parameters to the stub/runtime call.
2975 __ mov(eax, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
2976 __ push(FieldOperand(eax, JSFunction::kLiteralsOffset));
2977 __ push(Immediate(Smi::FromInt(instr->hydrogen()->literal_index())));
2978 __ push(Immediate(instr->hydrogen()->constant_elements()));
2979
2980 // Pick the right runtime function or stub to call.
2981 int length = instr->hydrogen()->length();
2982 if (instr->hydrogen()->IsCopyOnWrite()) {
2983 ASSERT(instr->hydrogen()->depth() == 1);
2984 FastCloneShallowArrayStub::Mode mode =
2985 FastCloneShallowArrayStub::COPY_ON_WRITE_ELEMENTS;
2986 FastCloneShallowArrayStub stub(mode, length);
2987 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
2988 } else if (instr->hydrogen()->depth() > 1) {
2989 CallRuntime(Runtime::kCreateArrayLiteral, 3, instr);
2990 } else if (length > FastCloneShallowArrayStub::kMaximumClonedLength) {
2991 CallRuntime(Runtime::kCreateArrayLiteralShallow, 3, instr);
2992 } else {
2993 FastCloneShallowArrayStub::Mode mode =
2994 FastCloneShallowArrayStub::CLONE_ELEMENTS;
2995 FastCloneShallowArrayStub stub(mode, length);
2996 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
2997 }
2998}
2999
3000
3001void LCodeGen::DoObjectLiteral(LObjectLiteral* instr) {
3002 // Setup the parameters to the stub/runtime call.
3003 __ mov(eax, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
3004 __ push(FieldOperand(eax, JSFunction::kLiteralsOffset));
3005 __ push(Immediate(Smi::FromInt(instr->hydrogen()->literal_index())));
3006 __ push(Immediate(instr->hydrogen()->constant_properties()));
3007 __ push(Immediate(Smi::FromInt(instr->hydrogen()->fast_elements() ? 1 : 0)));
3008
3009 // Pick the right runtime function or stub to call.
3010 if (instr->hydrogen()->depth() > 1) {
3011 CallRuntime(Runtime::kCreateObjectLiteral, 4, instr);
3012 } else {
3013 CallRuntime(Runtime::kCreateObjectLiteralShallow, 4, instr);
3014 }
3015}
3016
3017
3018void LCodeGen::DoRegExpLiteral(LRegExpLiteral* instr) {
3019 NearLabel materialized;
3020 // Registers will be used as follows:
3021 // edi = JS function.
3022 // ecx = literals array.
3023 // ebx = regexp literal.
3024 // eax = regexp literal clone.
3025 __ mov(edi, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
3026 __ mov(ecx, FieldOperand(edi, JSFunction::kLiteralsOffset));
3027 int literal_offset = FixedArray::kHeaderSize +
3028 instr->hydrogen()->literal_index() * kPointerSize;
3029 __ mov(ebx, FieldOperand(ecx, literal_offset));
3030 __ cmp(ebx, Factory::undefined_value());
3031 __ j(not_equal, &materialized);
3032
3033 // Create regexp literal using runtime function
3034 // Result will be in eax.
3035 __ push(ecx);
3036 __ push(Immediate(Smi::FromInt(instr->hydrogen()->literal_index())));
3037 __ push(Immediate(instr->hydrogen()->pattern()));
3038 __ push(Immediate(instr->hydrogen()->flags()));
3039 CallRuntime(Runtime::kMaterializeRegExpLiteral, 4, instr);
3040 __ mov(ebx, eax);
3041
3042 __ bind(&materialized);
3043 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
3044 Label allocated, runtime_allocate;
3045 __ AllocateInNewSpace(size, eax, ecx, edx, &runtime_allocate, TAG_OBJECT);
3046 __ jmp(&allocated);
3047
3048 __ bind(&runtime_allocate);
3049 __ push(ebx);
3050 __ push(Immediate(Smi::FromInt(size)));
3051 CallRuntime(Runtime::kAllocateInNewSpace, 1, instr);
3052 __ pop(ebx);
3053
3054 __ bind(&allocated);
3055 // Copy the content into the newly allocated memory.
3056 // (Unroll copy loop once for better throughput).
3057 for (int i = 0; i < size - kPointerSize; i += 2 * kPointerSize) {
3058 __ mov(edx, FieldOperand(ebx, i));
3059 __ mov(ecx, FieldOperand(ebx, i + kPointerSize));
3060 __ mov(FieldOperand(eax, i), edx);
3061 __ mov(FieldOperand(eax, i + kPointerSize), ecx);
3062 }
3063 if ((size % (2 * kPointerSize)) != 0) {
3064 __ mov(edx, FieldOperand(ebx, size - kPointerSize));
3065 __ mov(FieldOperand(eax, size - kPointerSize), edx);
3066 }
3067}
3068
3069
3070void LCodeGen::DoFunctionLiteral(LFunctionLiteral* instr) {
3071 // Use the fast case closure allocation code that allocates in new
3072 // space for nested functions that don't need literals cloning.
3073 Handle<SharedFunctionInfo> shared_info = instr->shared_info();
3074 bool pretenure = !instr->hydrogen()->pretenure();
3075 if (shared_info->num_literals() == 0 && !pretenure) {
3076 FastNewClosureStub stub;
3077 __ push(Immediate(shared_info));
3078 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
3079 } else {
3080 __ push(esi);
3081 __ push(Immediate(shared_info));
3082 __ push(Immediate(pretenure
3083 ? Factory::true_value()
3084 : Factory::false_value()));
3085 CallRuntime(Runtime::kNewClosure, 3, instr);
3086 }
3087}
3088
3089
3090void LCodeGen::DoTypeof(LTypeof* instr) {
3091 LOperand* input = instr->input();
3092 if (input->IsConstantOperand()) {
3093 __ push(ToImmediate(input));
3094 } else {
3095 __ push(ToOperand(input));
3096 }
3097 CallRuntime(Runtime::kTypeof, 1, instr);
3098}
3099
3100
3101void LCodeGen::DoTypeofIs(LTypeofIs* instr) {
3102 Register input = ToRegister(instr->input());
3103 Register result = ToRegister(instr->result());
3104 Label true_label;
3105 Label false_label;
3106 NearLabel done;
3107
3108 Condition final_branch_condition = EmitTypeofIs(&true_label,
3109 &false_label,
3110 input,
3111 instr->type_literal());
3112 __ j(final_branch_condition, &true_label);
3113 __ bind(&false_label);
3114 __ mov(result, Handle<Object>(Heap::false_value()));
3115 __ jmp(&done);
3116
3117 __ bind(&true_label);
3118 __ mov(result, Handle<Object>(Heap::true_value()));
3119
3120 __ bind(&done);
3121}
3122
3123
3124void LCodeGen::DoTypeofIsAndBranch(LTypeofIsAndBranch* instr) {
3125 Register input = ToRegister(instr->input());
3126 int true_block = chunk_->LookupDestination(instr->true_block_id());
3127 int false_block = chunk_->LookupDestination(instr->false_block_id());
3128 Label* true_label = chunk_->GetAssemblyLabel(true_block);
3129 Label* false_label = chunk_->GetAssemblyLabel(false_block);
3130
3131 Condition final_branch_condition = EmitTypeofIs(true_label,
3132 false_label,
3133 input,
3134 instr->type_literal());
3135
3136 EmitBranch(true_block, false_block, final_branch_condition);
3137}
3138
3139
3140Condition LCodeGen::EmitTypeofIs(Label* true_label,
3141 Label* false_label,
3142 Register input,
3143 Handle<String> type_name) {
3144 Condition final_branch_condition = no_condition;
3145 if (type_name->Equals(Heap::number_symbol())) {
3146 __ test(input, Immediate(kSmiTagMask));
3147 __ j(zero, true_label);
3148 __ cmp(FieldOperand(input, HeapObject::kMapOffset),
3149 Factory::heap_number_map());
3150 final_branch_condition = equal;
3151
3152 } else if (type_name->Equals(Heap::string_symbol())) {
3153 __ test(input, Immediate(kSmiTagMask));
3154 __ j(zero, false_label);
3155 __ mov(input, FieldOperand(input, HeapObject::kMapOffset));
3156 __ test_b(FieldOperand(input, Map::kBitFieldOffset),
3157 1 << Map::kIsUndetectable);
3158 __ j(not_zero, false_label);
3159 __ CmpInstanceType(input, FIRST_NONSTRING_TYPE);
3160 final_branch_condition = below;
3161
3162 } else if (type_name->Equals(Heap::boolean_symbol())) {
3163 __ cmp(input, Handle<Object>(Heap::true_value()));
3164 __ j(equal, true_label);
3165 __ cmp(input, Handle<Object>(Heap::false_value()));
3166 final_branch_condition = equal;
3167
3168 } else if (type_name->Equals(Heap::undefined_symbol())) {
3169 __ cmp(input, Factory::undefined_value());
3170 __ j(equal, true_label);
3171 __ test(input, Immediate(kSmiTagMask));
3172 __ j(zero, false_label);
3173 // Check for undetectable objects => true.
3174 __ mov(input, FieldOperand(input, HeapObject::kMapOffset));
3175 __ test_b(FieldOperand(input, Map::kBitFieldOffset),
3176 1 << Map::kIsUndetectable);
3177 final_branch_condition = not_zero;
3178
3179 } else if (type_name->Equals(Heap::function_symbol())) {
3180 __ test(input, Immediate(kSmiTagMask));
3181 __ j(zero, false_label);
3182 __ CmpObjectType(input, JS_FUNCTION_TYPE, input);
3183 __ j(equal, true_label);
3184 // Regular expressions => 'function' (they are callable).
3185 __ CmpInstanceType(input, JS_REGEXP_TYPE);
3186 final_branch_condition = equal;
3187
3188 } else if (type_name->Equals(Heap::object_symbol())) {
3189 __ test(input, Immediate(kSmiTagMask));
3190 __ j(zero, false_label);
3191 __ cmp(input, Factory::null_value());
3192 __ j(equal, true_label);
3193 // Regular expressions => 'function', not 'object'.
3194 __ CmpObjectType(input, JS_REGEXP_TYPE, input);
3195 __ j(equal, false_label);
3196 // Check for undetectable objects => false.
3197 __ test_b(FieldOperand(input, Map::kBitFieldOffset),
3198 1 << Map::kIsUndetectable);
3199 __ j(not_zero, false_label);
3200 // Check for JS objects => true.
3201 __ CmpInstanceType(input, FIRST_JS_OBJECT_TYPE);
3202 __ j(below, false_label);
3203 __ CmpInstanceType(input, LAST_JS_OBJECT_TYPE);
3204 final_branch_condition = below_equal;
3205
3206 } else {
3207 final_branch_condition = not_equal;
3208 __ jmp(false_label);
3209 // A dead branch instruction will be generated after this point.
3210 }
3211
3212 return final_branch_condition;
3213}
3214
3215
3216void LCodeGen::DoLazyBailout(LLazyBailout* instr) {
3217 // No code for lazy bailout instruction. Used to capture environment after a
3218 // call for populating the safepoint data with deoptimization data.
3219}
3220
3221
3222void LCodeGen::DoDeoptimize(LDeoptimize* instr) {
3223 DeoptimizeIf(no_condition, instr->environment());
3224}
3225
3226
3227void LCodeGen::DoDeleteProperty(LDeleteProperty* instr) {
3228 LOperand* obj = instr->object();
3229 LOperand* key = instr->key();
3230 __ push(ToOperand(obj));
3231 if (key->IsConstantOperand()) {
3232 __ push(ToImmediate(key));
3233 } else {
3234 __ push(ToOperand(key));
3235 }
3236 RecordPosition(instr->pointer_map()->position());
3237 SafepointGenerator safepoint_generator(this,
3238 instr->pointer_map(),
3239 Safepoint::kNoDeoptimizationIndex);
3240 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION, &safepoint_generator);
3241}
3242
3243
3244void LCodeGen::DoStackCheck(LStackCheck* instr) {
3245 // Perform stack overflow check.
3246 NearLabel done;
3247 ExternalReference stack_limit = ExternalReference::address_of_stack_limit();
3248 __ cmp(esp, Operand::StaticVariable(stack_limit));
3249 __ j(above_equal, &done);
3250
3251 StackCheckStub stub;
3252 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
3253 __ bind(&done);
3254}
3255
3256
3257void LCodeGen::DoOsrEntry(LOsrEntry* instr) {
3258 // This is a pseudo-instruction that ensures that the environment here is
3259 // properly registered for deoptimization and records the assembler's PC
3260 // offset.
3261 LEnvironment* environment = instr->environment();
3262 environment->SetSpilledRegisters(instr->SpilledRegisterArray(),
3263 instr->SpilledDoubleRegisterArray());
3264
3265 // If the environment were already registered, we would have no way of
3266 // backpatching it with the spill slot operands.
3267 ASSERT(!environment->HasBeenRegistered());
3268 RegisterEnvironmentForDeoptimization(environment);
3269 ASSERT(osr_pc_offset_ == -1);
3270 osr_pc_offset_ = masm()->pc_offset();
3271}
3272
3273
3274#undef __
3275
3276} } // namespace v8::internal