blob: 9569ac8b8099f628e410c09e0e00e2741e5feaa3 [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());
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000943 __ Set(ToRegister(instr->result()), Immediate(instr->value()));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000944}
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());
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000976 __ Set(ToRegister(instr->result()), Immediate(instr->value()));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000977}
978
979
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000980void LCodeGen::DoJSArrayLength(LJSArrayLength* instr) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000981 Register result = ToRegister(instr->result());
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000982 Register array = ToRegister(instr->input());
983 __ mov(result, FieldOperand(array, JSArray::kLengthOffset));
984}
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000985
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000986
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000987void LCodeGen::DoFixedArrayLength(LFixedArrayLength* instr) {
988 Register result = ToRegister(instr->result());
989 Register array = ToRegister(instr->input());
990 __ mov(result, FieldOperand(array, FixedArray::kLengthOffset));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000991}
992
993
994void LCodeGen::DoValueOf(LValueOf* instr) {
995 Register input = ToRegister(instr->input());
996 Register result = ToRegister(instr->result());
997 Register map = ToRegister(instr->temporary());
998 ASSERT(input.is(result));
999 NearLabel done;
1000 // If the object is a smi return the object.
1001 __ test(input, Immediate(kSmiTagMask));
1002 __ j(zero, &done);
1003
1004 // If the object is not a value type, return the object.
1005 __ CmpObjectType(input, JS_VALUE_TYPE, map);
1006 __ j(not_equal, &done);
1007 __ mov(result, FieldOperand(input, JSValue::kValueOffset));
1008
1009 __ bind(&done);
1010}
1011
1012
1013void LCodeGen::DoBitNotI(LBitNotI* instr) {
1014 LOperand* input = instr->input();
1015 ASSERT(input->Equals(instr->result()));
1016 __ not_(ToRegister(input));
1017}
1018
1019
1020void LCodeGen::DoThrow(LThrow* instr) {
1021 __ push(ToOperand(instr->input()));
1022 CallRuntime(Runtime::kThrow, 1, instr);
1023
1024 if (FLAG_debug_code) {
1025 Comment("Unreachable code.");
1026 __ int3();
1027 }
1028}
1029
1030
1031void LCodeGen::DoAddI(LAddI* instr) {
1032 LOperand* left = instr->left();
1033 LOperand* right = instr->right();
1034 ASSERT(left->Equals(instr->result()));
1035
1036 if (right->IsConstantOperand()) {
1037 __ add(ToOperand(left), ToImmediate(right));
1038 } else {
1039 __ add(ToRegister(left), ToOperand(right));
1040 }
1041
1042 if (instr->hydrogen()->CheckFlag(HValue::kCanOverflow)) {
1043 DeoptimizeIf(overflow, instr->environment());
1044 }
1045}
1046
1047
1048void LCodeGen::DoArithmeticD(LArithmeticD* instr) {
1049 LOperand* left = instr->left();
1050 LOperand* right = instr->right();
1051 // Modulo uses a fixed result register.
1052 ASSERT(instr->op() == Token::MOD || left->Equals(instr->result()));
1053 switch (instr->op()) {
1054 case Token::ADD:
1055 __ addsd(ToDoubleRegister(left), ToDoubleRegister(right));
1056 break;
1057 case Token::SUB:
1058 __ subsd(ToDoubleRegister(left), ToDoubleRegister(right));
1059 break;
1060 case Token::MUL:
1061 __ mulsd(ToDoubleRegister(left), ToDoubleRegister(right));
1062 break;
1063 case Token::DIV:
1064 __ divsd(ToDoubleRegister(left), ToDoubleRegister(right));
1065 break;
1066 case Token::MOD: {
1067 // Pass two doubles as arguments on the stack.
1068 __ PrepareCallCFunction(4, eax);
1069 __ movdbl(Operand(esp, 0 * kDoubleSize), ToDoubleRegister(left));
1070 __ movdbl(Operand(esp, 1 * kDoubleSize), ToDoubleRegister(right));
1071 __ CallCFunction(ExternalReference::double_fp_operation(Token::MOD), 4);
1072
1073 // Return value is in st(0) on ia32.
1074 // Store it into the (fixed) result register.
1075 __ sub(Operand(esp), Immediate(kDoubleSize));
1076 __ fstp_d(Operand(esp, 0));
1077 __ movdbl(ToDoubleRegister(instr->result()), Operand(esp, 0));
1078 __ add(Operand(esp), Immediate(kDoubleSize));
1079 break;
1080 }
1081 default:
1082 UNREACHABLE();
1083 break;
1084 }
1085}
1086
1087
1088void LCodeGen::DoArithmeticT(LArithmeticT* instr) {
1089 ASSERT(ToRegister(instr->left()).is(edx));
1090 ASSERT(ToRegister(instr->right()).is(eax));
1091 ASSERT(ToRegister(instr->result()).is(eax));
1092
1093 TypeRecordingBinaryOpStub stub(instr->op(), NO_OVERWRITE);
1094 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
1095}
1096
1097
1098int LCodeGen::GetNextEmittedBlock(int block) {
1099 for (int i = block + 1; i < graph()->blocks()->length(); ++i) {
1100 LLabel* label = chunk_->GetLabel(i);
1101 if (!label->HasReplacement()) return i;
1102 }
1103 return -1;
1104}
1105
1106
1107void LCodeGen::EmitBranch(int left_block, int right_block, Condition cc) {
1108 int next_block = GetNextEmittedBlock(current_block_);
1109 right_block = chunk_->LookupDestination(right_block);
1110 left_block = chunk_->LookupDestination(left_block);
1111
1112 if (right_block == left_block) {
1113 EmitGoto(left_block);
1114 } else if (left_block == next_block) {
1115 __ j(NegateCondition(cc), chunk_->GetAssemblyLabel(right_block));
1116 } else if (right_block == next_block) {
1117 __ j(cc, chunk_->GetAssemblyLabel(left_block));
1118 } else {
1119 __ j(cc, chunk_->GetAssemblyLabel(left_block));
1120 __ jmp(chunk_->GetAssemblyLabel(right_block));
1121 }
1122}
1123
1124
1125void LCodeGen::DoBranch(LBranch* instr) {
1126 int true_block = chunk_->LookupDestination(instr->true_block_id());
1127 int false_block = chunk_->LookupDestination(instr->false_block_id());
1128
1129 Representation r = instr->hydrogen()->representation();
1130 if (r.IsInteger32()) {
1131 Register reg = ToRegister(instr->input());
1132 __ test(reg, Operand(reg));
1133 EmitBranch(true_block, false_block, not_zero);
1134 } else if (r.IsDouble()) {
1135 XMMRegister reg = ToDoubleRegister(instr->input());
1136 __ xorpd(xmm0, xmm0);
1137 __ ucomisd(reg, xmm0);
1138 EmitBranch(true_block, false_block, not_equal);
1139 } else {
1140 ASSERT(r.IsTagged());
1141 Register reg = ToRegister(instr->input());
1142 if (instr->hydrogen()->type().IsBoolean()) {
1143 __ cmp(reg, Factory::true_value());
1144 EmitBranch(true_block, false_block, equal);
1145 } else {
1146 Label* true_label = chunk_->GetAssemblyLabel(true_block);
1147 Label* false_label = chunk_->GetAssemblyLabel(false_block);
1148
1149 __ cmp(reg, Factory::undefined_value());
1150 __ j(equal, false_label);
1151 __ cmp(reg, Factory::true_value());
1152 __ j(equal, true_label);
1153 __ cmp(reg, Factory::false_value());
1154 __ j(equal, false_label);
1155 __ test(reg, Operand(reg));
1156 __ j(equal, false_label);
1157 __ test(reg, Immediate(kSmiTagMask));
1158 __ j(zero, true_label);
1159
1160 // Test for double values. Zero is false.
1161 NearLabel call_stub;
1162 __ cmp(FieldOperand(reg, HeapObject::kMapOffset),
1163 Factory::heap_number_map());
1164 __ j(not_equal, &call_stub);
1165 __ fldz();
1166 __ fld_d(FieldOperand(reg, HeapNumber::kValueOffset));
1167 __ FCmp();
1168 __ j(zero, false_label);
1169 __ jmp(true_label);
1170
1171 // The conversion stub doesn't cause garbage collections so it's
1172 // safe to not record a safepoint after the call.
1173 __ bind(&call_stub);
1174 ToBooleanStub stub;
1175 __ pushad();
1176 __ push(reg);
1177 __ CallStub(&stub);
1178 __ test(eax, Operand(eax));
1179 __ popad();
1180 EmitBranch(true_block, false_block, not_zero);
1181 }
1182 }
1183}
1184
1185
1186void LCodeGen::EmitGoto(int block, LDeferredCode* deferred_stack_check) {
1187 block = chunk_->LookupDestination(block);
1188 int next_block = GetNextEmittedBlock(current_block_);
1189 if (block != next_block) {
1190 // Perform stack overflow check if this goto needs it before jumping.
1191 if (deferred_stack_check != NULL) {
1192 ExternalReference stack_limit =
1193 ExternalReference::address_of_stack_limit();
1194 __ cmp(esp, Operand::StaticVariable(stack_limit));
1195 __ j(above_equal, chunk_->GetAssemblyLabel(block));
1196 __ jmp(deferred_stack_check->entry());
1197 deferred_stack_check->SetExit(chunk_->GetAssemblyLabel(block));
1198 } else {
1199 __ jmp(chunk_->GetAssemblyLabel(block));
1200 }
1201 }
1202}
1203
1204
1205void LCodeGen::DoDeferredStackCheck(LGoto* instr) {
1206 __ pushad();
1207 __ CallRuntimeSaveDoubles(Runtime::kStackGuard);
1208 RecordSafepointWithRegisters(
1209 instr->pointer_map(), 0, Safepoint::kNoDeoptimizationIndex);
1210 __ popad();
1211}
1212
1213void LCodeGen::DoGoto(LGoto* instr) {
1214 class DeferredStackCheck: public LDeferredCode {
1215 public:
1216 DeferredStackCheck(LCodeGen* codegen, LGoto* instr)
1217 : LDeferredCode(codegen), instr_(instr) { }
1218 virtual void Generate() { codegen()->DoDeferredStackCheck(instr_); }
1219 private:
1220 LGoto* instr_;
1221 };
1222
1223 DeferredStackCheck* deferred = NULL;
1224 if (instr->include_stack_check()) {
1225 deferred = new DeferredStackCheck(this, instr);
1226 }
1227 EmitGoto(instr->block_id(), deferred);
1228}
1229
1230
1231Condition LCodeGen::TokenToCondition(Token::Value op, bool is_unsigned) {
1232 Condition cond = no_condition;
1233 switch (op) {
1234 case Token::EQ:
1235 case Token::EQ_STRICT:
1236 cond = equal;
1237 break;
1238 case Token::LT:
1239 cond = is_unsigned ? below : less;
1240 break;
1241 case Token::GT:
1242 cond = is_unsigned ? above : greater;
1243 break;
1244 case Token::LTE:
1245 cond = is_unsigned ? below_equal : less_equal;
1246 break;
1247 case Token::GTE:
1248 cond = is_unsigned ? above_equal : greater_equal;
1249 break;
1250 case Token::IN:
1251 case Token::INSTANCEOF:
1252 default:
1253 UNREACHABLE();
1254 }
1255 return cond;
1256}
1257
1258
1259void LCodeGen::EmitCmpI(LOperand* left, LOperand* right) {
1260 if (right->IsConstantOperand()) {
1261 __ cmp(ToOperand(left), ToImmediate(right));
1262 } else {
1263 __ cmp(ToRegister(left), ToOperand(right));
1264 }
1265}
1266
1267
1268void LCodeGen::DoCmpID(LCmpID* instr) {
1269 LOperand* left = instr->left();
1270 LOperand* right = instr->right();
1271 LOperand* result = instr->result();
1272
1273 NearLabel unordered;
1274 if (instr->is_double()) {
1275 // Don't base result on EFLAGS when a NaN is involved. Instead
1276 // jump to the unordered case, which produces a false value.
1277 __ ucomisd(ToDoubleRegister(left), ToDoubleRegister(right));
1278 __ j(parity_even, &unordered, not_taken);
1279 } else {
1280 EmitCmpI(left, right);
1281 }
1282
1283 NearLabel done;
1284 Condition cc = TokenToCondition(instr->op(), instr->is_double());
1285 __ mov(ToRegister(result), Handle<Object>(Heap::true_value()));
1286 __ j(cc, &done);
1287
1288 __ bind(&unordered);
1289 __ mov(ToRegister(result), Handle<Object>(Heap::false_value()));
1290 __ bind(&done);
1291}
1292
1293
1294void LCodeGen::DoCmpIDAndBranch(LCmpIDAndBranch* instr) {
1295 LOperand* left = instr->left();
1296 LOperand* right = instr->right();
1297 int false_block = chunk_->LookupDestination(instr->false_block_id());
1298 int true_block = chunk_->LookupDestination(instr->true_block_id());
1299
1300 if (instr->is_double()) {
1301 // Don't base result on EFLAGS when a NaN is involved. Instead
1302 // jump to the false block.
1303 __ ucomisd(ToDoubleRegister(left), ToDoubleRegister(right));
1304 __ j(parity_even, chunk_->GetAssemblyLabel(false_block));
1305 } else {
1306 EmitCmpI(left, right);
1307 }
1308
1309 Condition cc = TokenToCondition(instr->op(), instr->is_double());
1310 EmitBranch(true_block, false_block, cc);
1311}
1312
1313
1314void LCodeGen::DoCmpJSObjectEq(LCmpJSObjectEq* instr) {
1315 Register left = ToRegister(instr->left());
1316 Register right = ToRegister(instr->right());
1317 Register result = ToRegister(instr->result());
1318
1319 __ cmp(left, Operand(right));
1320 __ mov(result, Handle<Object>(Heap::true_value()));
1321 NearLabel done;
1322 __ j(equal, &done);
1323 __ mov(result, Handle<Object>(Heap::false_value()));
1324 __ bind(&done);
1325}
1326
1327
1328void LCodeGen::DoCmpJSObjectEqAndBranch(LCmpJSObjectEqAndBranch* instr) {
1329 Register left = ToRegister(instr->left());
1330 Register right = ToRegister(instr->right());
1331 int false_block = chunk_->LookupDestination(instr->false_block_id());
1332 int true_block = chunk_->LookupDestination(instr->true_block_id());
1333
1334 __ cmp(left, Operand(right));
1335 EmitBranch(true_block, false_block, equal);
1336}
1337
1338
1339void LCodeGen::DoIsNull(LIsNull* instr) {
1340 Register reg = ToRegister(instr->input());
1341 Register result = ToRegister(instr->result());
1342
1343 // TODO(fsc): If the expression is known to be a smi, then it's
1344 // definitely not null. Materialize false.
1345
1346 __ cmp(reg, Factory::null_value());
1347 if (instr->is_strict()) {
1348 __ mov(result, Handle<Object>(Heap::true_value()));
1349 NearLabel done;
1350 __ j(equal, &done);
1351 __ mov(result, Handle<Object>(Heap::false_value()));
1352 __ bind(&done);
1353 } else {
1354 NearLabel true_value, false_value, done;
1355 __ j(equal, &true_value);
1356 __ cmp(reg, Factory::undefined_value());
1357 __ j(equal, &true_value);
1358 __ test(reg, Immediate(kSmiTagMask));
1359 __ j(zero, &false_value);
1360 // Check for undetectable objects by looking in the bit field in
1361 // the map. The object has already been smi checked.
1362 Register scratch = result;
1363 __ mov(scratch, FieldOperand(reg, HeapObject::kMapOffset));
1364 __ movzx_b(scratch, FieldOperand(scratch, Map::kBitFieldOffset));
1365 __ test(scratch, Immediate(1 << Map::kIsUndetectable));
1366 __ j(not_zero, &true_value);
1367 __ bind(&false_value);
1368 __ mov(result, Handle<Object>(Heap::false_value()));
1369 __ jmp(&done);
1370 __ bind(&true_value);
1371 __ mov(result, Handle<Object>(Heap::true_value()));
1372 __ bind(&done);
1373 }
1374}
1375
1376
1377void LCodeGen::DoIsNullAndBranch(LIsNullAndBranch* instr) {
1378 Register reg = ToRegister(instr->input());
1379
1380 // TODO(fsc): If the expression is known to be a smi, then it's
1381 // definitely not null. Jump to the false block.
1382
1383 int true_block = chunk_->LookupDestination(instr->true_block_id());
1384 int false_block = chunk_->LookupDestination(instr->false_block_id());
1385
1386 __ cmp(reg, Factory::null_value());
1387 if (instr->is_strict()) {
1388 EmitBranch(true_block, false_block, equal);
1389 } else {
1390 Label* true_label = chunk_->GetAssemblyLabel(true_block);
1391 Label* false_label = chunk_->GetAssemblyLabel(false_block);
1392 __ j(equal, true_label);
1393 __ cmp(reg, Factory::undefined_value());
1394 __ j(equal, true_label);
1395 __ test(reg, Immediate(kSmiTagMask));
1396 __ j(zero, false_label);
1397 // Check for undetectable objects by looking in the bit field in
1398 // the map. The object has already been smi checked.
1399 Register scratch = ToRegister(instr->temp());
1400 __ mov(scratch, FieldOperand(reg, HeapObject::kMapOffset));
1401 __ movzx_b(scratch, FieldOperand(scratch, Map::kBitFieldOffset));
1402 __ test(scratch, Immediate(1 << Map::kIsUndetectable));
1403 EmitBranch(true_block, false_block, not_zero);
1404 }
1405}
1406
1407
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001408Condition LCodeGen::EmitIsObject(Register input,
1409 Register temp1,
1410 Register temp2,
1411 Label* is_not_object,
1412 Label* is_object) {
1413 ASSERT(!input.is(temp1));
1414 ASSERT(!input.is(temp2));
1415 ASSERT(!temp1.is(temp2));
1416
1417 __ test(input, Immediate(kSmiTagMask));
1418 __ j(equal, is_not_object);
1419
1420 __ cmp(input, Factory::null_value());
1421 __ j(equal, is_object);
1422
1423 __ mov(temp1, FieldOperand(input, HeapObject::kMapOffset));
1424 // Undetectable objects behave like undefined.
1425 __ movzx_b(temp2, FieldOperand(temp1, Map::kBitFieldOffset));
1426 __ test(temp2, Immediate(1 << Map::kIsUndetectable));
1427 __ j(not_zero, is_not_object);
1428
1429 __ movzx_b(temp2, FieldOperand(temp1, Map::kInstanceTypeOffset));
1430 __ cmp(temp2, FIRST_JS_OBJECT_TYPE);
1431 __ j(below, is_not_object);
1432 __ cmp(temp2, LAST_JS_OBJECT_TYPE);
1433 return below_equal;
1434}
1435
1436
1437void LCodeGen::DoIsObject(LIsObject* instr) {
1438 Register reg = ToRegister(instr->input());
1439 Register result = ToRegister(instr->result());
1440 Register temp = ToRegister(instr->temp());
1441 Label is_false, is_true, done;
1442
1443 Condition true_cond = EmitIsObject(reg, result, temp, &is_false, &is_true);
1444 __ j(true_cond, &is_true);
1445
1446 __ bind(&is_false);
1447 __ mov(result, Handle<Object>(Heap::false_value()));
1448 __ jmp(&done);
1449
1450 __ bind(&is_true);
1451 __ mov(result, Handle<Object>(Heap::true_value()));
1452
1453 __ bind(&done);
1454}
1455
1456
1457void LCodeGen::DoIsObjectAndBranch(LIsObjectAndBranch* instr) {
1458 Register reg = ToRegister(instr->input());
1459 Register temp = ToRegister(instr->temp());
1460 Register temp2 = ToRegister(instr->temp2());
1461
1462 int true_block = chunk_->LookupDestination(instr->true_block_id());
1463 int false_block = chunk_->LookupDestination(instr->false_block_id());
1464 Label* true_label = chunk_->GetAssemblyLabel(true_block);
1465 Label* false_label = chunk_->GetAssemblyLabel(false_block);
1466
1467 Condition true_cond = EmitIsObject(reg, temp, temp2, false_label, true_label);
1468
1469 EmitBranch(true_block, false_block, true_cond);
1470}
1471
1472
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001473void LCodeGen::DoIsSmi(LIsSmi* instr) {
1474 Operand input = ToOperand(instr->input());
1475 Register result = ToRegister(instr->result());
1476
1477 ASSERT(instr->hydrogen()->value()->representation().IsTagged());
1478 __ test(input, Immediate(kSmiTagMask));
1479 __ mov(result, Handle<Object>(Heap::true_value()));
1480 NearLabel done;
1481 __ j(zero, &done);
1482 __ mov(result, Handle<Object>(Heap::false_value()));
1483 __ bind(&done);
1484}
1485
1486
1487void LCodeGen::DoIsSmiAndBranch(LIsSmiAndBranch* instr) {
1488 Operand input = ToOperand(instr->input());
1489
1490 int true_block = chunk_->LookupDestination(instr->true_block_id());
1491 int false_block = chunk_->LookupDestination(instr->false_block_id());
1492
1493 __ test(input, Immediate(kSmiTagMask));
1494 EmitBranch(true_block, false_block, zero);
1495}
1496
1497
1498InstanceType LHasInstanceType::TestType() {
1499 InstanceType from = hydrogen()->from();
1500 InstanceType to = hydrogen()->to();
1501 if (from == FIRST_TYPE) return to;
1502 ASSERT(from == to || to == LAST_TYPE);
1503 return from;
1504}
1505
1506
1507
1508Condition LHasInstanceType::BranchCondition() {
1509 InstanceType from = hydrogen()->from();
1510 InstanceType to = hydrogen()->to();
1511 if (from == to) return equal;
1512 if (to == LAST_TYPE) return above_equal;
1513 if (from == FIRST_TYPE) return below_equal;
1514 UNREACHABLE();
1515 return equal;
1516}
1517
1518
1519void LCodeGen::DoHasInstanceType(LHasInstanceType* instr) {
1520 Register input = ToRegister(instr->input());
1521 Register result = ToRegister(instr->result());
1522
1523 ASSERT(instr->hydrogen()->value()->representation().IsTagged());
1524 __ test(input, Immediate(kSmiTagMask));
1525 NearLabel done, is_false;
1526 __ j(zero, &is_false);
1527 __ CmpObjectType(input, instr->TestType(), result);
1528 __ j(NegateCondition(instr->BranchCondition()), &is_false);
1529 __ mov(result, Handle<Object>(Heap::true_value()));
1530 __ jmp(&done);
1531 __ bind(&is_false);
1532 __ mov(result, Handle<Object>(Heap::false_value()));
1533 __ bind(&done);
1534}
1535
1536
1537void LCodeGen::DoHasInstanceTypeAndBranch(LHasInstanceTypeAndBranch* instr) {
1538 Register input = ToRegister(instr->input());
1539 Register temp = ToRegister(instr->temp());
1540
1541 int true_block = chunk_->LookupDestination(instr->true_block_id());
1542 int false_block = chunk_->LookupDestination(instr->false_block_id());
1543
1544 Label* false_label = chunk_->GetAssemblyLabel(false_block);
1545
1546 __ test(input, Immediate(kSmiTagMask));
1547 __ j(zero, false_label);
1548
1549 __ CmpObjectType(input, instr->TestType(), temp);
1550 EmitBranch(true_block, false_block, instr->BranchCondition());
1551}
1552
1553
1554void LCodeGen::DoHasCachedArrayIndex(LHasCachedArrayIndex* instr) {
1555 Register input = ToRegister(instr->input());
1556 Register result = ToRegister(instr->result());
1557
1558 ASSERT(instr->hydrogen()->value()->representation().IsTagged());
1559 __ mov(result, Handle<Object>(Heap::true_value()));
1560 __ test(FieldOperand(input, String::kHashFieldOffset),
1561 Immediate(String::kContainsCachedArrayIndexMask));
1562 NearLabel done;
1563 __ j(not_zero, &done);
1564 __ mov(result, Handle<Object>(Heap::false_value()));
1565 __ bind(&done);
1566}
1567
1568
1569void LCodeGen::DoHasCachedArrayIndexAndBranch(
1570 LHasCachedArrayIndexAndBranch* instr) {
1571 Register input = ToRegister(instr->input());
1572
1573 int true_block = chunk_->LookupDestination(instr->true_block_id());
1574 int false_block = chunk_->LookupDestination(instr->false_block_id());
1575
1576 __ test(FieldOperand(input, String::kHashFieldOffset),
1577 Immediate(String::kContainsCachedArrayIndexMask));
1578 EmitBranch(true_block, false_block, not_equal);
1579}
1580
1581
1582// Branches to a label or falls through with the answer in the z flag. Trashes
1583// the temp registers, but not the input. Only input and temp2 may alias.
1584void LCodeGen::EmitClassOfTest(Label* is_true,
1585 Label* is_false,
1586 Handle<String>class_name,
1587 Register input,
1588 Register temp,
1589 Register temp2) {
1590 ASSERT(!input.is(temp));
1591 ASSERT(!temp.is(temp2)); // But input and temp2 may be the same register.
1592 __ test(input, Immediate(kSmiTagMask));
1593 __ j(zero, is_false);
1594 __ CmpObjectType(input, FIRST_JS_OBJECT_TYPE, temp);
1595 __ j(below, is_false);
1596
1597 // Map is now in temp.
1598 // Functions have class 'Function'.
1599 __ CmpInstanceType(temp, JS_FUNCTION_TYPE);
1600 if (class_name->IsEqualTo(CStrVector("Function"))) {
1601 __ j(equal, is_true);
1602 } else {
1603 __ j(equal, is_false);
1604 }
1605
1606 // Check if the constructor in the map is a function.
1607 __ mov(temp, FieldOperand(temp, Map::kConstructorOffset));
1608
1609 // As long as JS_FUNCTION_TYPE is the last instance type and it is
1610 // right after LAST_JS_OBJECT_TYPE, we can avoid checking for
1611 // LAST_JS_OBJECT_TYPE.
1612 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
1613 ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
1614
1615 // Objects with a non-function constructor have class 'Object'.
1616 __ CmpObjectType(temp, JS_FUNCTION_TYPE, temp2);
1617 if (class_name->IsEqualTo(CStrVector("Object"))) {
1618 __ j(not_equal, is_true);
1619 } else {
1620 __ j(not_equal, is_false);
1621 }
1622
1623 // temp now contains the constructor function. Grab the
1624 // instance class name from there.
1625 __ mov(temp, FieldOperand(temp, JSFunction::kSharedFunctionInfoOffset));
1626 __ mov(temp, FieldOperand(temp,
1627 SharedFunctionInfo::kInstanceClassNameOffset));
1628 // The class name we are testing against is a symbol because it's a literal.
1629 // The name in the constructor is a symbol because of the way the context is
1630 // booted. This routine isn't expected to work for random API-created
1631 // classes and it doesn't have to because you can't access it with natives
1632 // syntax. Since both sides are symbols it is sufficient to use an identity
1633 // comparison.
1634 __ cmp(temp, class_name);
1635 // End with the answer in the z flag.
1636}
1637
1638
1639void LCodeGen::DoClassOfTest(LClassOfTest* instr) {
1640 Register input = ToRegister(instr->input());
1641 Register result = ToRegister(instr->result());
1642 ASSERT(input.is(result));
1643 Register temp = ToRegister(instr->temporary());
1644 Handle<String> class_name = instr->hydrogen()->class_name();
1645 NearLabel done;
1646 Label is_true, is_false;
1647
1648 EmitClassOfTest(&is_true, &is_false, class_name, input, temp, input);
1649
1650 __ j(not_equal, &is_false);
1651
1652 __ bind(&is_true);
1653 __ mov(result, Handle<Object>(Heap::true_value()));
1654 __ jmp(&done);
1655
1656 __ bind(&is_false);
1657 __ mov(result, Handle<Object>(Heap::false_value()));
1658 __ bind(&done);
1659}
1660
1661
1662void LCodeGen::DoClassOfTestAndBranch(LClassOfTestAndBranch* instr) {
1663 Register input = ToRegister(instr->input());
1664 Register temp = ToRegister(instr->temporary());
1665 Register temp2 = ToRegister(instr->temporary2());
1666 if (input.is(temp)) {
1667 // Swap.
1668 Register swapper = temp;
1669 temp = temp2;
1670 temp2 = swapper;
1671 }
1672 Handle<String> class_name = instr->hydrogen()->class_name();
1673
1674 int true_block = chunk_->LookupDestination(instr->true_block_id());
1675 int false_block = chunk_->LookupDestination(instr->false_block_id());
1676
1677 Label* true_label = chunk_->GetAssemblyLabel(true_block);
1678 Label* false_label = chunk_->GetAssemblyLabel(false_block);
1679
1680 EmitClassOfTest(true_label, false_label, class_name, input, temp, temp2);
1681
1682 EmitBranch(true_block, false_block, equal);
1683}
1684
1685
1686void LCodeGen::DoCmpMapAndBranch(LCmpMapAndBranch* instr) {
1687 Register reg = ToRegister(instr->input());
1688 int true_block = instr->true_block_id();
1689 int false_block = instr->false_block_id();
1690
1691 __ cmp(FieldOperand(reg, HeapObject::kMapOffset), instr->map());
1692 EmitBranch(true_block, false_block, equal);
1693}
1694
1695
1696void LCodeGen::DoInstanceOf(LInstanceOf* instr) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001697 // Object and function are in fixed registers eax and edx.
1698 InstanceofStub stub(InstanceofStub::kArgsInRegisters);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001699 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
1700
1701 NearLabel true_value, done;
1702 __ test(eax, Operand(eax));
1703 __ j(zero, &true_value);
1704 __ mov(ToRegister(instr->result()), Factory::false_value());
1705 __ jmp(&done);
1706 __ bind(&true_value);
1707 __ mov(ToRegister(instr->result()), Factory::true_value());
1708 __ bind(&done);
1709}
1710
1711
1712void LCodeGen::DoInstanceOfAndBranch(LInstanceOfAndBranch* instr) {
1713 int true_block = chunk_->LookupDestination(instr->true_block_id());
1714 int false_block = chunk_->LookupDestination(instr->false_block_id());
1715
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001716 InstanceofStub stub(InstanceofStub::kArgsInRegisters);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001717 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
1718 __ test(eax, Operand(eax));
1719 EmitBranch(true_block, false_block, zero);
1720}
1721
1722
1723static Condition ComputeCompareCondition(Token::Value op) {
1724 switch (op) {
1725 case Token::EQ_STRICT:
1726 case Token::EQ:
1727 return equal;
1728 case Token::LT:
1729 return less;
1730 case Token::GT:
1731 return greater;
1732 case Token::LTE:
1733 return less_equal;
1734 case Token::GTE:
1735 return greater_equal;
1736 default:
1737 UNREACHABLE();
1738 return no_condition;
1739 }
1740}
1741
1742
1743void LCodeGen::DoCmpT(LCmpT* instr) {
1744 Token::Value op = instr->op();
1745
1746 Handle<Code> ic = CompareIC::GetUninitialized(op);
1747 CallCode(ic, RelocInfo::CODE_TARGET, instr);
1748
1749 Condition condition = ComputeCompareCondition(op);
1750 if (op == Token::GT || op == Token::LTE) {
1751 condition = ReverseCondition(condition);
1752 }
1753 NearLabel true_value, done;
1754 __ test(eax, Operand(eax));
1755 __ j(condition, &true_value);
1756 __ mov(ToRegister(instr->result()), Factory::false_value());
1757 __ jmp(&done);
1758 __ bind(&true_value);
1759 __ mov(ToRegister(instr->result()), Factory::true_value());
1760 __ bind(&done);
1761}
1762
1763
1764void LCodeGen::DoCmpTAndBranch(LCmpTAndBranch* instr) {
1765 Token::Value op = instr->op();
1766 int true_block = chunk_->LookupDestination(instr->true_block_id());
1767 int false_block = chunk_->LookupDestination(instr->false_block_id());
1768
1769 Handle<Code> ic = CompareIC::GetUninitialized(op);
1770 CallCode(ic, RelocInfo::CODE_TARGET, instr);
1771
1772 // The compare stub expects compare condition and the input operands
1773 // reversed for GT and LTE.
1774 Condition condition = ComputeCompareCondition(op);
1775 if (op == Token::GT || op == Token::LTE) {
1776 condition = ReverseCondition(condition);
1777 }
1778 __ test(eax, Operand(eax));
1779 EmitBranch(true_block, false_block, condition);
1780}
1781
1782
1783void LCodeGen::DoReturn(LReturn* instr) {
1784 if (FLAG_trace) {
1785 // Preserve the return value on the stack and rely on the runtime
1786 // call to return the value in the same register.
1787 __ push(eax);
1788 __ CallRuntime(Runtime::kTraceExit, 1);
1789 }
1790 __ mov(esp, ebp);
1791 __ pop(ebp);
1792 __ ret((ParameterCount() + 1) * kPointerSize);
1793}
1794
1795
1796void LCodeGen::DoLoadGlobal(LLoadGlobal* instr) {
1797 Register result = ToRegister(instr->result());
1798 __ mov(result, Operand::Cell(instr->hydrogen()->cell()));
1799 if (instr->hydrogen()->check_hole_value()) {
1800 __ cmp(result, Factory::the_hole_value());
1801 DeoptimizeIf(equal, instr->environment());
1802 }
1803}
1804
1805
1806void LCodeGen::DoStoreGlobal(LStoreGlobal* instr) {
1807 Register value = ToRegister(instr->input());
1808 __ mov(Operand::Cell(instr->hydrogen()->cell()), value);
1809}
1810
1811
1812void LCodeGen::DoLoadNamedField(LLoadNamedField* instr) {
1813 Register object = ToRegister(instr->input());
1814 Register result = ToRegister(instr->result());
1815 if (instr->hydrogen()->is_in_object()) {
1816 __ mov(result, FieldOperand(object, instr->hydrogen()->offset()));
1817 } else {
1818 __ mov(result, FieldOperand(object, JSObject::kPropertiesOffset));
1819 __ mov(result, FieldOperand(result, instr->hydrogen()->offset()));
1820 }
1821}
1822
1823
1824void LCodeGen::DoLoadNamedGeneric(LLoadNamedGeneric* instr) {
1825 ASSERT(ToRegister(instr->object()).is(eax));
1826 ASSERT(ToRegister(instr->result()).is(eax));
1827
1828 __ mov(ecx, instr->name());
1829 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
1830 CallCode(ic, RelocInfo::CODE_TARGET, instr);
1831}
1832
1833
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +00001834void LCodeGen::DoLoadFunctionPrototype(LLoadFunctionPrototype* instr) {
1835 Register function = ToRegister(instr->function());
1836 Register temp = ToRegister(instr->temporary());
1837 Register result = ToRegister(instr->result());
1838
1839 // Check that the function really is a function.
1840 __ CmpObjectType(function, JS_FUNCTION_TYPE, result);
1841 DeoptimizeIf(not_equal, instr->environment());
1842
1843 // Check whether the function has an instance prototype.
1844 NearLabel non_instance;
1845 __ test_b(FieldOperand(result, Map::kBitFieldOffset),
1846 1 << Map::kHasNonInstancePrototype);
1847 __ j(not_zero, &non_instance);
1848
1849 // Get the prototype or initial map from the function.
1850 __ mov(result,
1851 FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
1852
1853 // Check that the function has a prototype or an initial map.
1854 __ cmp(Operand(result), Immediate(Factory::the_hole_value()));
1855 DeoptimizeIf(equal, instr->environment());
1856
1857 // If the function does not have an initial map, we're done.
1858 NearLabel done;
1859 __ CmpObjectType(result, MAP_TYPE, temp);
1860 __ j(not_equal, &done);
1861
1862 // Get the prototype from the initial map.
1863 __ mov(result, FieldOperand(result, Map::kPrototypeOffset));
1864 __ jmp(&done);
1865
1866 // Non-instance prototype: Fetch prototype from constructor field
1867 // in the function's map.
1868 __ bind(&non_instance);
1869 __ mov(result, FieldOperand(result, Map::kConstructorOffset));
1870
1871 // All done.
1872 __ bind(&done);
1873}
1874
1875
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001876void LCodeGen::DoLoadElements(LLoadElements* instr) {
1877 ASSERT(instr->result()->Equals(instr->input()));
1878 Register reg = ToRegister(instr->input());
1879 __ mov(reg, FieldOperand(reg, JSObject::kElementsOffset));
1880 if (FLAG_debug_code) {
1881 NearLabel done;
1882 __ cmp(FieldOperand(reg, HeapObject::kMapOffset),
1883 Immediate(Factory::fixed_array_map()));
1884 __ j(equal, &done);
1885 __ cmp(FieldOperand(reg, HeapObject::kMapOffset),
1886 Immediate(Factory::fixed_cow_array_map()));
1887 __ Check(equal, "Check for fast elements failed.");
1888 __ bind(&done);
1889 }
1890}
1891
1892
1893void LCodeGen::DoAccessArgumentsAt(LAccessArgumentsAt* instr) {
1894 Register arguments = ToRegister(instr->arguments());
1895 Register length = ToRegister(instr->length());
1896 Operand index = ToOperand(instr->index());
1897 Register result = ToRegister(instr->result());
1898
1899 __ sub(length, index);
1900 DeoptimizeIf(below_equal, instr->environment());
1901
1902 __ mov(result, Operand(arguments, length, times_4, kPointerSize));
1903}
1904
1905
1906void LCodeGen::DoLoadKeyedFastElement(LLoadKeyedFastElement* instr) {
1907 Register elements = ToRegister(instr->elements());
1908 Register key = ToRegister(instr->key());
1909 Register result;
1910 if (instr->load_result() != NULL) {
1911 result = ToRegister(instr->load_result());
1912 } else {
1913 result = ToRegister(instr->result());
1914 ASSERT(result.is(elements));
1915 }
1916
1917 // Load the result.
1918 __ mov(result, FieldOperand(elements, key, times_4, FixedArray::kHeaderSize));
1919
1920 Representation r = instr->hydrogen()->representation();
1921 if (r.IsInteger32()) {
1922 // Untag and check for smi.
1923 __ SmiUntag(result);
1924 DeoptimizeIf(carry, instr->environment());
1925 } else if (r.IsDouble()) {
1926 EmitNumberUntagD(result,
1927 ToDoubleRegister(instr->result()),
1928 instr->environment());
1929 } else {
1930 // Check for the hole value.
1931 ASSERT(r.IsTagged());
1932 __ cmp(result, Factory::the_hole_value());
1933 DeoptimizeIf(equal, instr->environment());
1934 }
1935}
1936
1937
1938void LCodeGen::DoLoadKeyedGeneric(LLoadKeyedGeneric* instr) {
1939 ASSERT(ToRegister(instr->object()).is(edx));
1940 ASSERT(ToRegister(instr->key()).is(eax));
1941
1942 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
1943 CallCode(ic, RelocInfo::CODE_TARGET, instr);
1944}
1945
1946
1947void LCodeGen::DoArgumentsElements(LArgumentsElements* instr) {
1948 Register result = ToRegister(instr->result());
1949
1950 // Check for arguments adapter frame.
1951 Label done, adapted;
1952 __ mov(result, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
1953 __ mov(result, Operand(result, StandardFrameConstants::kContextOffset));
1954 __ cmp(Operand(result),
1955 Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
1956 __ j(equal, &adapted);
1957
1958 // No arguments adaptor frame.
1959 __ mov(result, Operand(ebp));
1960 __ jmp(&done);
1961
1962 // Arguments adaptor frame present.
1963 __ bind(&adapted);
1964 __ mov(result, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
1965
1966 // Done. Pointer to topmost argument is in result.
1967 __ bind(&done);
1968}
1969
1970
1971void LCodeGen::DoArgumentsLength(LArgumentsLength* instr) {
1972 Operand elem = ToOperand(instr->input());
1973 Register result = ToRegister(instr->result());
1974
1975 Label done;
1976
1977 // No arguments adaptor frame. Number of arguments is fixed.
1978 __ cmp(ebp, elem);
1979 __ mov(result, Immediate(scope()->num_parameters()));
1980 __ j(equal, &done);
1981
1982 // Arguments adaptor frame present. Get argument length from there.
1983 __ mov(result, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
1984 __ mov(result, Operand(result,
1985 ArgumentsAdaptorFrameConstants::kLengthOffset));
1986 __ SmiUntag(result);
1987
1988 // Done. Argument length is in result register.
1989 __ bind(&done);
1990}
1991
1992
1993void LCodeGen::DoApplyArguments(LApplyArguments* instr) {
1994 Register receiver = ToRegister(instr->receiver());
1995 ASSERT(ToRegister(instr->function()).is(edi));
1996 ASSERT(ToRegister(instr->result()).is(eax));
1997
1998 // If the receiver is null or undefined, we have to pass the
1999 // global object as a receiver.
2000 NearLabel global_receiver, receiver_ok;
2001 __ cmp(receiver, Factory::null_value());
2002 __ j(equal, &global_receiver);
2003 __ cmp(receiver, Factory::undefined_value());
2004 __ j(not_equal, &receiver_ok);
2005 __ bind(&global_receiver);
2006 __ mov(receiver, GlobalObjectOperand());
2007 __ bind(&receiver_ok);
2008
2009 Register length = ToRegister(instr->length());
2010 Register elements = ToRegister(instr->elements());
2011
2012 Label invoke;
2013
2014 // Copy the arguments to this function possibly from the
2015 // adaptor frame below it.
2016 const uint32_t kArgumentsLimit = 1 * KB;
2017 __ cmp(length, kArgumentsLimit);
2018 DeoptimizeIf(above, instr->environment());
2019
2020 __ push(receiver);
2021 __ mov(receiver, length);
2022
2023 // Loop through the arguments pushing them onto the execution
2024 // stack.
2025 Label loop;
2026 // length is a small non-negative integer, due to the test above.
2027 __ test(length, Operand(length));
2028 __ j(zero, &invoke);
2029 __ bind(&loop);
2030 __ push(Operand(elements, length, times_pointer_size, 1 * kPointerSize));
2031 __ dec(length);
2032 __ j(not_zero, &loop);
2033
2034 // Invoke the function.
2035 __ bind(&invoke);
2036 ASSERT(receiver.is(eax));
2037 v8::internal::ParameterCount actual(eax);
2038 SafepointGenerator safepoint_generator(this,
2039 instr->pointer_map(),
2040 Safepoint::kNoDeoptimizationIndex);
2041 __ InvokeFunction(edi, actual, CALL_FUNCTION, &safepoint_generator);
2042}
2043
2044
2045void LCodeGen::DoPushArgument(LPushArgument* instr) {
2046 LOperand* argument = instr->input();
2047 if (argument->IsConstantOperand()) {
2048 __ push(ToImmediate(argument));
2049 } else {
2050 __ push(ToOperand(argument));
2051 }
2052}
2053
2054
2055void LCodeGen::DoGlobalObject(LGlobalObject* instr) {
2056 Register result = ToRegister(instr->result());
2057 __ mov(result, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
2058}
2059
2060
2061void LCodeGen::DoGlobalReceiver(LGlobalReceiver* instr) {
2062 Register result = ToRegister(instr->result());
2063 __ mov(result, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
2064 __ mov(result, FieldOperand(result, GlobalObject::kGlobalReceiverOffset));
2065}
2066
2067
2068void LCodeGen::CallKnownFunction(Handle<JSFunction> function,
2069 int arity,
2070 LInstruction* instr) {
2071 // Change context if needed.
2072 bool change_context =
2073 (graph()->info()->closure()->context() != function->context()) ||
2074 scope()->contains_with() ||
2075 (scope()->num_heap_slots() > 0);
2076 if (change_context) {
2077 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
2078 }
2079
2080 // Set eax to arguments count if adaption is not needed. Assumes that eax
2081 // is available to write to at this point.
2082 if (!function->NeedsArgumentsAdaption()) {
2083 __ mov(eax, arity);
2084 }
2085
2086 LPointerMap* pointers = instr->pointer_map();
2087 RecordPosition(pointers->position());
2088
2089 // Invoke function.
2090 if (*function == *graph()->info()->closure()) {
2091 __ CallSelf();
2092 } else {
2093 __ call(FieldOperand(edi, JSFunction::kCodeEntryOffset));
2094 }
2095
2096 // Setup deoptimization.
2097 RegisterLazyDeoptimization(instr);
2098
2099 // Restore context.
2100 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
2101}
2102
2103
2104void LCodeGen::DoCallConstantFunction(LCallConstantFunction* instr) {
2105 ASSERT(ToRegister(instr->result()).is(eax));
2106 __ mov(edi, instr->function());
2107 CallKnownFunction(instr->function(), instr->arity(), instr);
2108}
2109
2110
2111void LCodeGen::DoDeferredMathAbsTaggedHeapNumber(LUnaryMathOperation* instr) {
2112 Register input_reg = ToRegister(instr->input());
2113 __ cmp(FieldOperand(input_reg, HeapObject::kMapOffset),
2114 Factory::heap_number_map());
2115 DeoptimizeIf(not_equal, instr->environment());
2116
2117 Label done;
2118 Register tmp = input_reg.is(eax) ? ecx : eax;
2119 Register tmp2 = tmp.is(ecx) ? edx : input_reg.is(ecx) ? edx : ecx;
2120
2121 // Preserve the value of all registers.
2122 __ PushSafepointRegisters();
2123
2124 Label negative;
2125 __ mov(tmp, FieldOperand(input_reg, HeapNumber::kExponentOffset));
2126 // Check the sign of the argument. If the argument is positive,
2127 // just return it.
2128 __ test(tmp, Immediate(HeapNumber::kSignMask));
2129 __ j(not_zero, &negative);
2130 __ mov(tmp, input_reg);
2131 __ jmp(&done);
2132
2133 __ bind(&negative);
2134
2135 Label allocated, slow;
2136 __ AllocateHeapNumber(tmp, tmp2, no_reg, &slow);
2137 __ jmp(&allocated);
2138
2139 // Slow case: Call the runtime system to do the number allocation.
2140 __ bind(&slow);
2141
2142 __ CallRuntimeSaveDoubles(Runtime::kAllocateHeapNumber);
2143 RecordSafepointWithRegisters(
2144 instr->pointer_map(), 0, Safepoint::kNoDeoptimizationIndex);
2145 // Set the pointer to the new heap number in tmp.
2146 if (!tmp.is(eax)) __ mov(tmp, eax);
2147
2148 // Restore input_reg after call to runtime.
2149 __ mov(input_reg, Operand(esp, EspIndexForPushAll(input_reg) * kPointerSize));
2150
2151 __ bind(&allocated);
2152 __ mov(tmp2, FieldOperand(input_reg, HeapNumber::kExponentOffset));
2153 __ and_(tmp2, ~HeapNumber::kSignMask);
2154 __ mov(FieldOperand(tmp, HeapNumber::kExponentOffset), tmp2);
2155 __ mov(tmp2, FieldOperand(input_reg, HeapNumber::kMantissaOffset));
2156 __ mov(FieldOperand(tmp, HeapNumber::kMantissaOffset), tmp2);
2157
2158 __ bind(&done);
2159 __ mov(Operand(esp, EspIndexForPushAll(input_reg) * kPointerSize), tmp);
2160
2161 __ PopSafepointRegisters();
2162}
2163
2164
2165void LCodeGen::DoMathAbs(LUnaryMathOperation* instr) {
2166 // Class for deferred case.
2167 class DeferredMathAbsTaggedHeapNumber: public LDeferredCode {
2168 public:
2169 DeferredMathAbsTaggedHeapNumber(LCodeGen* codegen,
2170 LUnaryMathOperation* instr)
2171 : LDeferredCode(codegen), instr_(instr) { }
2172 virtual void Generate() {
2173 codegen()->DoDeferredMathAbsTaggedHeapNumber(instr_);
2174 }
2175 private:
2176 LUnaryMathOperation* instr_;
2177 };
2178
2179 ASSERT(instr->input()->Equals(instr->result()));
2180 Representation r = instr->hydrogen()->value()->representation();
2181
2182 if (r.IsDouble()) {
2183 XMMRegister scratch = xmm0;
2184 XMMRegister input_reg = ToDoubleRegister(instr->input());
2185 __ pxor(scratch, scratch);
2186 __ subsd(scratch, input_reg);
2187 __ pand(input_reg, scratch);
2188 } else if (r.IsInteger32()) {
2189 Register input_reg = ToRegister(instr->input());
2190 __ test(input_reg, Operand(input_reg));
2191 Label is_positive;
2192 __ j(not_sign, &is_positive);
2193 __ neg(input_reg);
2194 __ test(input_reg, Operand(input_reg));
2195 DeoptimizeIf(negative, instr->environment());
2196 __ bind(&is_positive);
2197 } else { // Tagged case.
2198 DeferredMathAbsTaggedHeapNumber* deferred =
2199 new DeferredMathAbsTaggedHeapNumber(this, instr);
2200 Label not_smi;
2201 Register input_reg = ToRegister(instr->input());
2202 // Smi check.
2203 __ test(input_reg, Immediate(kSmiTagMask));
2204 __ j(not_zero, deferred->entry());
2205 __ test(input_reg, Operand(input_reg));
2206 Label is_positive;
2207 __ j(not_sign, &is_positive);
2208 __ neg(input_reg);
2209
2210 __ test(input_reg, Operand(input_reg));
2211 DeoptimizeIf(negative, instr->environment());
2212
2213 __ bind(&is_positive);
2214 __ bind(deferred->exit());
2215 }
2216}
2217
2218
2219void LCodeGen::DoMathFloor(LUnaryMathOperation* instr) {
2220 XMMRegister xmm_scratch = xmm0;
2221 Register output_reg = ToRegister(instr->result());
2222 XMMRegister input_reg = ToDoubleRegister(instr->input());
2223 __ xorpd(xmm_scratch, xmm_scratch); // Zero the register.
2224 __ ucomisd(input_reg, xmm_scratch);
2225
2226 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
2227 DeoptimizeIf(below_equal, instr->environment());
2228 } else {
2229 DeoptimizeIf(below, instr->environment());
2230 }
2231
2232 // Use truncating instruction (OK because input is positive).
2233 __ cvttsd2si(output_reg, Operand(input_reg));
2234
2235 // Overflow is signalled with minint.
2236 __ cmp(output_reg, 0x80000000u);
2237 DeoptimizeIf(equal, instr->environment());
2238}
2239
2240
2241void LCodeGen::DoMathRound(LUnaryMathOperation* instr) {
2242 XMMRegister xmm_scratch = xmm0;
2243 Register output_reg = ToRegister(instr->result());
2244 XMMRegister input_reg = ToDoubleRegister(instr->input());
2245
2246 // xmm_scratch = 0.5
2247 ExternalReference one_half = ExternalReference::address_of_one_half();
2248 __ movdbl(xmm_scratch, Operand::StaticVariable(one_half));
2249
2250 // input = input + 0.5
2251 __ addsd(input_reg, xmm_scratch);
2252
2253 // We need to return -0 for the input range [-0.5, 0[, otherwise
2254 // compute Math.floor(value + 0.5).
2255 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
2256 __ ucomisd(input_reg, xmm_scratch);
2257 DeoptimizeIf(below_equal, instr->environment());
2258 } else {
2259 // If we don't need to bailout on -0, we check only bailout
2260 // on negative inputs.
2261 __ xorpd(xmm_scratch, xmm_scratch); // Zero the register.
2262 __ ucomisd(input_reg, xmm_scratch);
2263 DeoptimizeIf(below, instr->environment());
2264 }
2265
2266 // Compute Math.floor(value + 0.5).
2267 // Use truncating instruction (OK because input is positive).
2268 __ cvttsd2si(output_reg, Operand(input_reg));
2269
2270 // Overflow is signalled with minint.
2271 __ cmp(output_reg, 0x80000000u);
2272 DeoptimizeIf(equal, instr->environment());
2273}
2274
2275
2276void LCodeGen::DoMathSqrt(LUnaryMathOperation* instr) {
2277 XMMRegister input_reg = ToDoubleRegister(instr->input());
2278 ASSERT(ToDoubleRegister(instr->result()).is(input_reg));
2279 __ sqrtsd(input_reg, input_reg);
2280}
2281
2282
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002283void LCodeGen::DoMathPowHalf(LUnaryMathOperation* instr) {
2284 XMMRegister xmm_scratch = xmm0;
2285 XMMRegister input_reg = ToDoubleRegister(instr->input());
2286 ASSERT(ToDoubleRegister(instr->result()).is(input_reg));
2287 ExternalReference negative_infinity =
2288 ExternalReference::address_of_negative_infinity();
2289 __ movdbl(xmm_scratch, Operand::StaticVariable(negative_infinity));
2290 __ ucomisd(xmm_scratch, input_reg);
2291 DeoptimizeIf(equal, instr->environment());
2292 __ sqrtsd(input_reg, input_reg);
2293}
2294
2295
2296void LCodeGen::DoPower(LPower* instr) {
2297 LOperand* left = instr->left();
2298 LOperand* right = instr->right();
2299 DoubleRegister result_reg = ToDoubleRegister(instr->result());
2300 Representation exponent_type = instr->hydrogen()->right()->representation();
2301 if (exponent_type.IsDouble()) {
2302 // It is safe to use ebx directly since the instruction is marked
2303 // as a call.
2304 __ PrepareCallCFunction(4, ebx);
2305 __ movdbl(Operand(esp, 0 * kDoubleSize), ToDoubleRegister(left));
2306 __ movdbl(Operand(esp, 1 * kDoubleSize), ToDoubleRegister(right));
2307 __ CallCFunction(ExternalReference::power_double_double_function(), 4);
2308 } else if (exponent_type.IsInteger32()) {
2309 // It is safe to use ebx directly since the instruction is marked
2310 // as a call.
2311 ASSERT(!ToRegister(right).is(ebx));
2312 __ PrepareCallCFunction(4, ebx);
2313 __ movdbl(Operand(esp, 0 * kDoubleSize), ToDoubleRegister(left));
2314 __ mov(Operand(esp, 1 * kDoubleSize), ToRegister(right));
2315 __ CallCFunction(ExternalReference::power_double_int_function(), 4);
2316 } else {
2317 ASSERT(exponent_type.IsTagged());
2318 CpuFeatures::Scope scope(SSE2);
2319 Register right_reg = ToRegister(right);
2320
2321 Label non_smi, call;
2322 __ test(right_reg, Immediate(kSmiTagMask));
2323 __ j(not_zero, &non_smi);
2324 __ SmiUntag(right_reg);
2325 __ cvtsi2sd(result_reg, Operand(right_reg));
2326 __ jmp(&call);
2327
2328 __ bind(&non_smi);
2329 // It is safe to use ebx directly since the instruction is marked
2330 // as a call.
2331 ASSERT(!right_reg.is(ebx));
2332 __ CmpObjectType(right_reg, HEAP_NUMBER_TYPE , ebx);
2333 DeoptimizeIf(not_equal, instr->environment());
2334 __ movdbl(result_reg, FieldOperand(right_reg, HeapNumber::kValueOffset));
2335
2336 __ bind(&call);
2337 __ PrepareCallCFunction(4, ebx);
2338 __ movdbl(Operand(esp, 0 * kDoubleSize), ToDoubleRegister(left));
2339 __ movdbl(Operand(esp, 1 * kDoubleSize), result_reg);
2340 __ CallCFunction(ExternalReference::power_double_double_function(), 4);
2341 }
2342
2343 // Return value is in st(0) on ia32.
2344 // Store it into the (fixed) result register.
2345 __ sub(Operand(esp), Immediate(kDoubleSize));
2346 __ fstp_d(Operand(esp, 0));
2347 __ movdbl(result_reg, Operand(esp, 0));
2348 __ add(Operand(esp), Immediate(kDoubleSize));
2349}
2350
2351
2352void LCodeGen::DoMathLog(LUnaryMathOperation* instr) {
2353 ASSERT(ToDoubleRegister(instr->result()).is(xmm1));
whesse@chromium.org023421e2010-12-21 12:19:12 +00002354 TranscendentalCacheStub stub(TranscendentalCache::LOG,
2355 TranscendentalCacheStub::UNTAGGED);
2356 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
2357}
2358
2359
2360void LCodeGen::DoMathCos(LUnaryMathOperation* instr) {
2361 ASSERT(ToDoubleRegister(instr->result()).is(xmm1));
2362 TranscendentalCacheStub stub(TranscendentalCache::COS,
2363 TranscendentalCacheStub::UNTAGGED);
2364 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
2365}
2366
2367
2368void LCodeGen::DoMathSin(LUnaryMathOperation* instr) {
2369 ASSERT(ToDoubleRegister(instr->result()).is(xmm1));
2370 TranscendentalCacheStub stub(TranscendentalCache::SIN,
2371 TranscendentalCacheStub::UNTAGGED);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002372 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
2373}
2374
2375
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002376void LCodeGen::DoUnaryMathOperation(LUnaryMathOperation* instr) {
2377 switch (instr->op()) {
2378 case kMathAbs:
2379 DoMathAbs(instr);
2380 break;
2381 case kMathFloor:
2382 DoMathFloor(instr);
2383 break;
2384 case kMathRound:
2385 DoMathRound(instr);
2386 break;
2387 case kMathSqrt:
2388 DoMathSqrt(instr);
2389 break;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002390 case kMathPowHalf:
2391 DoMathPowHalf(instr);
2392 break;
whesse@chromium.org023421e2010-12-21 12:19:12 +00002393 case kMathCos:
2394 DoMathCos(instr);
2395 break;
2396 case kMathSin:
2397 DoMathSin(instr);
2398 break;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002399 case kMathLog:
2400 DoMathLog(instr);
2401 break;
2402
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002403 default:
2404 UNREACHABLE();
2405 }
2406}
2407
2408
2409void LCodeGen::DoCallKeyed(LCallKeyed* instr) {
2410 ASSERT(ToRegister(instr->result()).is(eax));
2411
2412 int arity = instr->arity();
2413 Handle<Code> ic = StubCache::ComputeKeyedCallInitialize(arity, NOT_IN_LOOP);
2414 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2415 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
2416}
2417
2418
2419void LCodeGen::DoCallNamed(LCallNamed* instr) {
2420 ASSERT(ToRegister(instr->result()).is(eax));
2421
2422 int arity = instr->arity();
2423 Handle<Code> ic = StubCache::ComputeCallInitialize(arity, NOT_IN_LOOP);
2424 __ mov(ecx, instr->name());
2425 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2426 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
2427}
2428
2429
2430void LCodeGen::DoCallFunction(LCallFunction* instr) {
2431 ASSERT(ToRegister(instr->result()).is(eax));
2432
2433 int arity = instr->arity();
2434 CallFunctionStub stub(arity, NOT_IN_LOOP, RECEIVER_MIGHT_BE_VALUE);
2435 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
2436 __ Drop(1);
2437 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
2438}
2439
2440
2441void LCodeGen::DoCallGlobal(LCallGlobal* instr) {
2442 ASSERT(ToRegister(instr->result()).is(eax));
2443
2444 int arity = instr->arity();
2445 Handle<Code> ic = StubCache::ComputeCallInitialize(arity, NOT_IN_LOOP);
2446 __ mov(ecx, instr->name());
2447 CallCode(ic, RelocInfo::CODE_TARGET_CONTEXT, instr);
2448 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
2449}
2450
2451
2452void LCodeGen::DoCallKnownGlobal(LCallKnownGlobal* instr) {
2453 ASSERT(ToRegister(instr->result()).is(eax));
2454 __ mov(edi, instr->target());
2455 CallKnownFunction(instr->target(), instr->arity(), instr);
2456}
2457
2458
2459void LCodeGen::DoCallNew(LCallNew* instr) {
2460 ASSERT(ToRegister(instr->input()).is(edi));
2461 ASSERT(ToRegister(instr->result()).is(eax));
2462
2463 Handle<Code> builtin(Builtins::builtin(Builtins::JSConstructCall));
2464 __ Set(eax, Immediate(instr->arity()));
2465 CallCode(builtin, RelocInfo::CONSTRUCT_CALL, instr);
2466}
2467
2468
2469void LCodeGen::DoCallRuntime(LCallRuntime* instr) {
2470 CallRuntime(instr->function(), instr->arity(), instr);
2471}
2472
2473
2474void LCodeGen::DoStoreNamedField(LStoreNamedField* instr) {
2475 Register object = ToRegister(instr->object());
2476 Register value = ToRegister(instr->value());
2477 int offset = instr->offset();
2478
2479 if (!instr->transition().is_null()) {
2480 __ mov(FieldOperand(object, HeapObject::kMapOffset), instr->transition());
2481 }
2482
2483 // Do the store.
2484 if (instr->is_in_object()) {
2485 __ mov(FieldOperand(object, offset), value);
2486 if (instr->needs_write_barrier()) {
2487 Register temp = ToRegister(instr->temp());
2488 // Update the write barrier for the object for in-object properties.
2489 __ RecordWrite(object, offset, value, temp);
2490 }
2491 } else {
2492 Register temp = ToRegister(instr->temp());
2493 __ mov(temp, FieldOperand(object, JSObject::kPropertiesOffset));
2494 __ mov(FieldOperand(temp, offset), value);
2495 if (instr->needs_write_barrier()) {
2496 // Update the write barrier for the properties array.
2497 // object is used as a scratch register.
2498 __ RecordWrite(temp, offset, value, object);
2499 }
2500 }
2501}
2502
2503
2504void LCodeGen::DoStoreNamedGeneric(LStoreNamedGeneric* instr) {
2505 ASSERT(ToRegister(instr->object()).is(edx));
2506 ASSERT(ToRegister(instr->value()).is(eax));
2507
2508 __ mov(ecx, instr->name());
2509 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
2510 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2511}
2512
2513
2514void LCodeGen::DoBoundsCheck(LBoundsCheck* instr) {
2515 __ cmp(ToRegister(instr->index()), ToOperand(instr->length()));
2516 DeoptimizeIf(above_equal, instr->environment());
2517}
2518
2519
2520void LCodeGen::DoStoreKeyedFastElement(LStoreKeyedFastElement* instr) {
2521 Register value = ToRegister(instr->value());
2522 Register elements = ToRegister(instr->object());
2523 Register key = instr->key()->IsRegister() ? ToRegister(instr->key()) : no_reg;
2524
2525 // Do the store.
2526 if (instr->key()->IsConstantOperand()) {
2527 ASSERT(!instr->hydrogen()->NeedsWriteBarrier());
2528 LConstantOperand* const_operand = LConstantOperand::cast(instr->key());
2529 int offset =
2530 ToInteger32(const_operand) * kPointerSize + FixedArray::kHeaderSize;
2531 __ mov(FieldOperand(elements, offset), value);
2532 } else {
2533 __ mov(FieldOperand(elements, key, times_4, FixedArray::kHeaderSize),
2534 value);
2535 }
2536
2537 // Update the write barrier unless we're certain that we're storing a smi.
2538 if (instr->hydrogen()->NeedsWriteBarrier()) {
2539 // Compute address of modified element and store it into key register.
2540 __ lea(key, FieldOperand(elements, key, times_4, FixedArray::kHeaderSize));
2541 __ RecordWrite(elements, key, value);
2542 }
2543}
2544
2545
2546void LCodeGen::DoStoreKeyedGeneric(LStoreKeyedGeneric* instr) {
2547 ASSERT(ToRegister(instr->object()).is(edx));
2548 ASSERT(ToRegister(instr->key()).is(ecx));
2549 ASSERT(ToRegister(instr->value()).is(eax));
2550
2551 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
2552 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2553}
2554
2555
2556void LCodeGen::DoInteger32ToDouble(LInteger32ToDouble* instr) {
2557 LOperand* input = instr->input();
2558 ASSERT(input->IsRegister() || input->IsStackSlot());
2559 LOperand* output = instr->result();
2560 ASSERT(output->IsDoubleRegister());
2561 __ cvtsi2sd(ToDoubleRegister(output), ToOperand(input));
2562}
2563
2564
2565void LCodeGen::DoNumberTagI(LNumberTagI* instr) {
2566 class DeferredNumberTagI: public LDeferredCode {
2567 public:
2568 DeferredNumberTagI(LCodeGen* codegen, LNumberTagI* instr)
2569 : LDeferredCode(codegen), instr_(instr) { }
2570 virtual void Generate() { codegen()->DoDeferredNumberTagI(instr_); }
2571 private:
2572 LNumberTagI* instr_;
2573 };
2574
2575 LOperand* input = instr->input();
2576 ASSERT(input->IsRegister() && input->Equals(instr->result()));
2577 Register reg = ToRegister(input);
2578
2579 DeferredNumberTagI* deferred = new DeferredNumberTagI(this, instr);
2580 __ SmiTag(reg);
2581 __ j(overflow, deferred->entry());
2582 __ bind(deferred->exit());
2583}
2584
2585
2586void LCodeGen::DoDeferredNumberTagI(LNumberTagI* instr) {
2587 Label slow;
2588 Register reg = ToRegister(instr->input());
2589 Register tmp = reg.is(eax) ? ecx : eax;
2590
2591 // Preserve the value of all registers.
2592 __ PushSafepointRegisters();
2593
2594 // There was overflow, so bits 30 and 31 of the original integer
2595 // disagree. Try to allocate a heap number in new space and store
2596 // the value in there. If that fails, call the runtime system.
2597 NearLabel done;
2598 __ SmiUntag(reg);
2599 __ xor_(reg, 0x80000000);
2600 __ cvtsi2sd(xmm0, Operand(reg));
2601 if (FLAG_inline_new) {
2602 __ AllocateHeapNumber(reg, tmp, no_reg, &slow);
2603 __ jmp(&done);
2604 }
2605
2606 // Slow case: Call the runtime system to do the number allocation.
2607 __ bind(&slow);
2608
2609 // TODO(3095996): Put a valid pointer value in the stack slot where the result
2610 // register is stored, as this register is in the pointer map, but contains an
2611 // integer value.
2612 __ mov(Operand(esp, EspIndexForPushAll(reg) * kPointerSize), Immediate(0));
2613
2614 __ CallRuntimeSaveDoubles(Runtime::kAllocateHeapNumber);
2615 RecordSafepointWithRegisters(
2616 instr->pointer_map(), 0, Safepoint::kNoDeoptimizationIndex);
2617 if (!reg.is(eax)) __ mov(reg, eax);
2618
2619 // Done. Put the value in xmm0 into the value of the allocated heap
2620 // number.
2621 __ bind(&done);
2622 __ movdbl(FieldOperand(reg, HeapNumber::kValueOffset), xmm0);
2623 __ mov(Operand(esp, EspIndexForPushAll(reg) * kPointerSize), reg);
2624 __ PopSafepointRegisters();
2625}
2626
2627
2628void LCodeGen::DoNumberTagD(LNumberTagD* instr) {
2629 class DeferredNumberTagD: public LDeferredCode {
2630 public:
2631 DeferredNumberTagD(LCodeGen* codegen, LNumberTagD* instr)
2632 : LDeferredCode(codegen), instr_(instr) { }
2633 virtual void Generate() { codegen()->DoDeferredNumberTagD(instr_); }
2634 private:
2635 LNumberTagD* instr_;
2636 };
2637
2638 XMMRegister input_reg = ToDoubleRegister(instr->input());
2639 Register reg = ToRegister(instr->result());
2640 Register tmp = ToRegister(instr->temp());
2641
2642 DeferredNumberTagD* deferred = new DeferredNumberTagD(this, instr);
2643 if (FLAG_inline_new) {
2644 __ AllocateHeapNumber(reg, tmp, no_reg, deferred->entry());
2645 } else {
2646 __ jmp(deferred->entry());
2647 }
2648 __ bind(deferred->exit());
2649 __ movdbl(FieldOperand(reg, HeapNumber::kValueOffset), input_reg);
2650}
2651
2652
2653void LCodeGen::DoDeferredNumberTagD(LNumberTagD* instr) {
2654 // TODO(3095996): Get rid of this. For now, we need to make the
2655 // result register contain a valid pointer because it is already
2656 // contained in the register pointer map.
2657 Register reg = ToRegister(instr->result());
2658 __ Set(reg, Immediate(0));
2659
2660 __ PushSafepointRegisters();
2661 __ CallRuntimeSaveDoubles(Runtime::kAllocateHeapNumber);
2662 RecordSafepointWithRegisters(
2663 instr->pointer_map(), 0, Safepoint::kNoDeoptimizationIndex);
2664 __ mov(Operand(esp, EspIndexForPushAll(reg) * kPointerSize), eax);
2665 __ PopSafepointRegisters();
2666}
2667
2668
2669void LCodeGen::DoSmiTag(LSmiTag* instr) {
2670 LOperand* input = instr->input();
2671 ASSERT(input->IsRegister() && input->Equals(instr->result()));
2672 ASSERT(!instr->hydrogen_value()->CheckFlag(HValue::kCanOverflow));
2673 __ SmiTag(ToRegister(input));
2674}
2675
2676
2677void LCodeGen::DoSmiUntag(LSmiUntag* instr) {
2678 LOperand* input = instr->input();
2679 ASSERT(input->IsRegister() && input->Equals(instr->result()));
2680 if (instr->needs_check()) {
2681 __ test(ToRegister(input), Immediate(kSmiTagMask));
2682 DeoptimizeIf(not_zero, instr->environment());
2683 }
2684 __ SmiUntag(ToRegister(input));
2685}
2686
2687
2688void LCodeGen::EmitNumberUntagD(Register input_reg,
2689 XMMRegister result_reg,
2690 LEnvironment* env) {
2691 NearLabel load_smi, heap_number, done;
2692
2693 // Smi check.
2694 __ test(input_reg, Immediate(kSmiTagMask));
2695 __ j(zero, &load_smi, not_taken);
2696
2697 // Heap number map check.
2698 __ cmp(FieldOperand(input_reg, HeapObject::kMapOffset),
2699 Factory::heap_number_map());
2700 __ j(equal, &heap_number);
2701
2702 __ cmp(input_reg, Factory::undefined_value());
2703 DeoptimizeIf(not_equal, env);
2704
2705 // Convert undefined to NaN.
2706 __ push(input_reg);
2707 __ mov(input_reg, Factory::nan_value());
2708 __ movdbl(result_reg, FieldOperand(input_reg, HeapNumber::kValueOffset));
2709 __ pop(input_reg);
2710 __ jmp(&done);
2711
2712 // Heap number to XMM conversion.
2713 __ bind(&heap_number);
2714 __ movdbl(result_reg, FieldOperand(input_reg, HeapNumber::kValueOffset));
2715 __ jmp(&done);
2716
2717 // Smi to XMM conversion
2718 __ bind(&load_smi);
2719 __ SmiUntag(input_reg); // Untag smi before converting to float.
2720 __ cvtsi2sd(result_reg, Operand(input_reg));
2721 __ SmiTag(input_reg); // Retag smi.
2722 __ bind(&done);
2723}
2724
2725
2726class DeferredTaggedToI: public LDeferredCode {
2727 public:
2728 DeferredTaggedToI(LCodeGen* codegen, LTaggedToI* instr)
2729 : LDeferredCode(codegen), instr_(instr) { }
2730 virtual void Generate() { codegen()->DoDeferredTaggedToI(instr_); }
2731 private:
2732 LTaggedToI* instr_;
2733};
2734
2735
2736void LCodeGen::DoDeferredTaggedToI(LTaggedToI* instr) {
2737 NearLabel done, heap_number;
2738 Register input_reg = ToRegister(instr->input());
2739
2740 // Heap number map check.
2741 __ cmp(FieldOperand(input_reg, HeapObject::kMapOffset),
2742 Factory::heap_number_map());
2743
2744 if (instr->truncating()) {
2745 __ j(equal, &heap_number);
2746 // Check for undefined. Undefined is converted to zero for truncating
2747 // conversions.
2748 __ cmp(input_reg, Factory::undefined_value());
2749 DeoptimizeIf(not_equal, instr->environment());
2750 __ mov(input_reg, 0);
2751 __ jmp(&done);
2752
2753 __ bind(&heap_number);
2754 if (CpuFeatures::IsSupported(SSE3)) {
2755 CpuFeatures::Scope scope(SSE3);
2756 NearLabel convert;
2757 // Use more powerful conversion when sse3 is available.
2758 // Load x87 register with heap number.
2759 __ fld_d(FieldOperand(input_reg, HeapNumber::kValueOffset));
2760 // Get exponent alone and check for too-big exponent.
2761 __ mov(input_reg, FieldOperand(input_reg, HeapNumber::kExponentOffset));
2762 __ and_(input_reg, HeapNumber::kExponentMask);
2763 const uint32_t kTooBigExponent =
2764 (HeapNumber::kExponentBias + 63) << HeapNumber::kExponentShift;
2765 __ cmp(Operand(input_reg), Immediate(kTooBigExponent));
2766 __ j(less, &convert);
2767 // Pop FPU stack before deoptimizing.
2768 __ ffree(0);
2769 __ fincstp();
2770 DeoptimizeIf(no_condition, instr->environment());
2771
2772 // Reserve space for 64 bit answer.
2773 __ bind(&convert);
2774 __ sub(Operand(esp), Immediate(kDoubleSize));
2775 // Do conversion, which cannot fail because we checked the exponent.
2776 __ fisttp_d(Operand(esp, 0));
2777 __ mov(input_reg, Operand(esp, 0)); // Low word of answer is the result.
2778 __ add(Operand(esp), Immediate(kDoubleSize));
2779 } else {
2780 NearLabel deopt;
2781 XMMRegister xmm_temp = ToDoubleRegister(instr->temp());
2782 __ movdbl(xmm0, FieldOperand(input_reg, HeapNumber::kValueOffset));
2783 __ cvttsd2si(input_reg, Operand(xmm0));
2784 __ cmp(input_reg, 0x80000000u);
2785 __ j(not_equal, &done);
2786 // Check if the input was 0x8000000 (kMinInt).
2787 // If no, then we got an overflow and we deoptimize.
2788 ExternalReference min_int = ExternalReference::address_of_min_int();
2789 __ movdbl(xmm_temp, Operand::StaticVariable(min_int));
2790 __ ucomisd(xmm_temp, xmm0);
2791 DeoptimizeIf(not_equal, instr->environment());
2792 DeoptimizeIf(parity_even, instr->environment()); // NaN.
2793 }
2794 } else {
2795 // Deoptimize if we don't have a heap number.
2796 DeoptimizeIf(not_equal, instr->environment());
2797
2798 XMMRegister xmm_temp = ToDoubleRegister(instr->temp());
2799 __ movdbl(xmm0, FieldOperand(input_reg, HeapNumber::kValueOffset));
2800 __ cvttsd2si(input_reg, Operand(xmm0));
2801 __ cvtsi2sd(xmm_temp, Operand(input_reg));
2802 __ ucomisd(xmm0, xmm_temp);
2803 DeoptimizeIf(not_equal, instr->environment());
2804 DeoptimizeIf(parity_even, instr->environment()); // NaN.
2805 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
2806 __ test(input_reg, Operand(input_reg));
2807 __ j(not_zero, &done);
2808 __ movmskpd(input_reg, xmm0);
2809 __ and_(input_reg, 1);
2810 DeoptimizeIf(not_zero, instr->environment());
2811 }
2812 }
2813 __ bind(&done);
2814}
2815
2816
2817void LCodeGen::DoTaggedToI(LTaggedToI* instr) {
2818 LOperand* input = instr->input();
2819 ASSERT(input->IsRegister());
2820 ASSERT(input->Equals(instr->result()));
2821
2822 Register input_reg = ToRegister(input);
2823
2824 DeferredTaggedToI* deferred = new DeferredTaggedToI(this, instr);
2825
2826 // Smi check.
2827 __ test(input_reg, Immediate(kSmiTagMask));
2828 __ j(not_zero, deferred->entry());
2829
2830 // Smi to int32 conversion
2831 __ SmiUntag(input_reg); // Untag smi.
2832
2833 __ bind(deferred->exit());
2834}
2835
2836
2837void LCodeGen::DoNumberUntagD(LNumberUntagD* instr) {
2838 LOperand* input = instr->input();
2839 ASSERT(input->IsRegister());
2840 LOperand* result = instr->result();
2841 ASSERT(result->IsDoubleRegister());
2842
2843 Register input_reg = ToRegister(input);
2844 XMMRegister result_reg = ToDoubleRegister(result);
2845
2846 EmitNumberUntagD(input_reg, result_reg, instr->environment());
2847}
2848
2849
2850void LCodeGen::DoDoubleToI(LDoubleToI* instr) {
2851 LOperand* input = instr->input();
2852 ASSERT(input->IsDoubleRegister());
2853 LOperand* result = instr->result();
2854 ASSERT(result->IsRegister());
2855
2856 XMMRegister input_reg = ToDoubleRegister(input);
2857 Register result_reg = ToRegister(result);
2858
2859 if (instr->truncating()) {
2860 // Performs a truncating conversion of a floating point number as used by
2861 // the JS bitwise operations.
2862 __ cvttsd2si(result_reg, Operand(input_reg));
2863 __ cmp(result_reg, 0x80000000u);
2864 if (CpuFeatures::IsSupported(SSE3)) {
2865 // This will deoptimize if the exponent of the input in out of range.
2866 CpuFeatures::Scope scope(SSE3);
2867 NearLabel convert, done;
2868 __ j(not_equal, &done);
2869 __ sub(Operand(esp), Immediate(kDoubleSize));
2870 __ movdbl(Operand(esp, 0), input_reg);
2871 // Get exponent alone and check for too-big exponent.
2872 __ mov(result_reg, Operand(esp, sizeof(int32_t)));
2873 __ and_(result_reg, HeapNumber::kExponentMask);
2874 const uint32_t kTooBigExponent =
2875 (HeapNumber::kExponentBias + 63) << HeapNumber::kExponentShift;
2876 __ cmp(Operand(result_reg), Immediate(kTooBigExponent));
2877 __ j(less, &convert);
2878 __ add(Operand(esp), Immediate(kDoubleSize));
2879 DeoptimizeIf(no_condition, instr->environment());
2880 __ bind(&convert);
2881 // Do conversion, which cannot fail because we checked the exponent.
2882 __ fld_d(Operand(esp, 0));
2883 __ fisttp_d(Operand(esp, 0));
2884 __ mov(result_reg, Operand(esp, 0)); // Low word of answer is the result.
2885 __ add(Operand(esp), Immediate(kDoubleSize));
2886 __ bind(&done);
2887 } else {
2888 // This will bail out if the input was not in the int32 range (or,
2889 // unfortunately, if the input was 0x80000000).
2890 DeoptimizeIf(equal, instr->environment());
2891 }
2892 } else {
2893 NearLabel done;
2894 __ cvttsd2si(result_reg, Operand(input_reg));
2895 __ cvtsi2sd(xmm0, Operand(result_reg));
2896 __ ucomisd(xmm0, input_reg);
2897 DeoptimizeIf(not_equal, instr->environment());
2898 DeoptimizeIf(parity_even, instr->environment()); // NaN.
2899 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
2900 // The integer converted back is equal to the original. We
2901 // only have to test if we got -0 as an input.
2902 __ test(result_reg, Operand(result_reg));
2903 __ j(not_zero, &done);
2904 __ movmskpd(result_reg, input_reg);
2905 // Bit 0 contains the sign of the double in input_reg.
2906 // If input was positive, we are ok and return 0, otherwise
2907 // deoptimize.
2908 __ and_(result_reg, 1);
2909 DeoptimizeIf(not_zero, instr->environment());
2910 }
2911 __ bind(&done);
2912 }
2913}
2914
2915
2916void LCodeGen::DoCheckSmi(LCheckSmi* instr) {
2917 LOperand* input = instr->input();
2918 ASSERT(input->IsRegister());
2919 __ test(ToRegister(input), Immediate(kSmiTagMask));
2920 DeoptimizeIf(instr->condition(), instr->environment());
2921}
2922
2923
2924void LCodeGen::DoCheckInstanceType(LCheckInstanceType* instr) {
2925 Register input = ToRegister(instr->input());
2926 Register temp = ToRegister(instr->temp());
2927 InstanceType first = instr->hydrogen()->first();
2928 InstanceType last = instr->hydrogen()->last();
2929
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002930 __ mov(temp, FieldOperand(input, HeapObject::kMapOffset));
2931 __ cmpb(FieldOperand(temp, Map::kInstanceTypeOffset),
2932 static_cast<int8_t>(first));
2933
2934 // If there is only one type in the interval check for equality.
2935 if (first == last) {
2936 DeoptimizeIf(not_equal, instr->environment());
2937 } else {
2938 DeoptimizeIf(below, instr->environment());
2939 // Omit check for the last type.
2940 if (last != LAST_TYPE) {
2941 __ cmpb(FieldOperand(temp, Map::kInstanceTypeOffset),
2942 static_cast<int8_t>(last));
2943 DeoptimizeIf(above, instr->environment());
2944 }
2945 }
2946}
2947
2948
2949void LCodeGen::DoCheckFunction(LCheckFunction* instr) {
2950 ASSERT(instr->input()->IsRegister());
2951 Register reg = ToRegister(instr->input());
2952 __ cmp(reg, instr->hydrogen()->target());
2953 DeoptimizeIf(not_equal, instr->environment());
2954}
2955
2956
2957void LCodeGen::DoCheckMap(LCheckMap* instr) {
2958 LOperand* input = instr->input();
2959 ASSERT(input->IsRegister());
2960 Register reg = ToRegister(input);
2961 __ cmp(FieldOperand(reg, HeapObject::kMapOffset),
2962 instr->hydrogen()->map());
2963 DeoptimizeIf(not_equal, instr->environment());
2964}
2965
2966
2967void LCodeGen::LoadPrototype(Register result, Handle<JSObject> prototype) {
2968 if (Heap::InNewSpace(*prototype)) {
2969 Handle<JSGlobalPropertyCell> cell =
2970 Factory::NewJSGlobalPropertyCell(prototype);
2971 __ mov(result, Operand::Cell(cell));
2972 } else {
2973 __ mov(result, prototype);
2974 }
2975}
2976
2977
2978void LCodeGen::DoCheckPrototypeMaps(LCheckPrototypeMaps* instr) {
2979 Register reg = ToRegister(instr->temp());
2980
2981 Handle<JSObject> holder = instr->holder();
2982 Handle<Map> receiver_map = instr->receiver_map();
2983 Handle<JSObject> current_prototype(JSObject::cast(receiver_map->prototype()));
2984
2985 // Load prototype object.
2986 LoadPrototype(reg, current_prototype);
2987
2988 // Check prototype maps up to the holder.
2989 while (!current_prototype.is_identical_to(holder)) {
2990 __ cmp(FieldOperand(reg, HeapObject::kMapOffset),
2991 Handle<Map>(current_prototype->map()));
2992 DeoptimizeIf(not_equal, instr->environment());
2993 current_prototype =
2994 Handle<JSObject>(JSObject::cast(current_prototype->GetPrototype()));
2995 // Load next prototype object.
2996 LoadPrototype(reg, current_prototype);
2997 }
2998
2999 // Check the holder map.
3000 __ cmp(FieldOperand(reg, HeapObject::kMapOffset),
3001 Handle<Map>(current_prototype->map()));
3002 DeoptimizeIf(not_equal, instr->environment());
3003}
3004
3005
3006void LCodeGen::DoArrayLiteral(LArrayLiteral* instr) {
3007 // Setup the parameters to the stub/runtime call.
3008 __ mov(eax, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
3009 __ push(FieldOperand(eax, JSFunction::kLiteralsOffset));
3010 __ push(Immediate(Smi::FromInt(instr->hydrogen()->literal_index())));
3011 __ push(Immediate(instr->hydrogen()->constant_elements()));
3012
3013 // Pick the right runtime function or stub to call.
3014 int length = instr->hydrogen()->length();
3015 if (instr->hydrogen()->IsCopyOnWrite()) {
3016 ASSERT(instr->hydrogen()->depth() == 1);
3017 FastCloneShallowArrayStub::Mode mode =
3018 FastCloneShallowArrayStub::COPY_ON_WRITE_ELEMENTS;
3019 FastCloneShallowArrayStub stub(mode, length);
3020 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
3021 } else if (instr->hydrogen()->depth() > 1) {
3022 CallRuntime(Runtime::kCreateArrayLiteral, 3, instr);
3023 } else if (length > FastCloneShallowArrayStub::kMaximumClonedLength) {
3024 CallRuntime(Runtime::kCreateArrayLiteralShallow, 3, instr);
3025 } else {
3026 FastCloneShallowArrayStub::Mode mode =
3027 FastCloneShallowArrayStub::CLONE_ELEMENTS;
3028 FastCloneShallowArrayStub stub(mode, length);
3029 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
3030 }
3031}
3032
3033
3034void LCodeGen::DoObjectLiteral(LObjectLiteral* instr) {
3035 // Setup the parameters to the stub/runtime call.
3036 __ mov(eax, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
3037 __ push(FieldOperand(eax, JSFunction::kLiteralsOffset));
3038 __ push(Immediate(Smi::FromInt(instr->hydrogen()->literal_index())));
3039 __ push(Immediate(instr->hydrogen()->constant_properties()));
3040 __ push(Immediate(Smi::FromInt(instr->hydrogen()->fast_elements() ? 1 : 0)));
3041
3042 // Pick the right runtime function or stub to call.
3043 if (instr->hydrogen()->depth() > 1) {
3044 CallRuntime(Runtime::kCreateObjectLiteral, 4, instr);
3045 } else {
3046 CallRuntime(Runtime::kCreateObjectLiteralShallow, 4, instr);
3047 }
3048}
3049
3050
3051void LCodeGen::DoRegExpLiteral(LRegExpLiteral* instr) {
3052 NearLabel materialized;
3053 // Registers will be used as follows:
3054 // edi = JS function.
3055 // ecx = literals array.
3056 // ebx = regexp literal.
3057 // eax = regexp literal clone.
3058 __ mov(edi, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
3059 __ mov(ecx, FieldOperand(edi, JSFunction::kLiteralsOffset));
3060 int literal_offset = FixedArray::kHeaderSize +
3061 instr->hydrogen()->literal_index() * kPointerSize;
3062 __ mov(ebx, FieldOperand(ecx, literal_offset));
3063 __ cmp(ebx, Factory::undefined_value());
3064 __ j(not_equal, &materialized);
3065
3066 // Create regexp literal using runtime function
3067 // Result will be in eax.
3068 __ push(ecx);
3069 __ push(Immediate(Smi::FromInt(instr->hydrogen()->literal_index())));
3070 __ push(Immediate(instr->hydrogen()->pattern()));
3071 __ push(Immediate(instr->hydrogen()->flags()));
3072 CallRuntime(Runtime::kMaterializeRegExpLiteral, 4, instr);
3073 __ mov(ebx, eax);
3074
3075 __ bind(&materialized);
3076 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
3077 Label allocated, runtime_allocate;
3078 __ AllocateInNewSpace(size, eax, ecx, edx, &runtime_allocate, TAG_OBJECT);
3079 __ jmp(&allocated);
3080
3081 __ bind(&runtime_allocate);
3082 __ push(ebx);
3083 __ push(Immediate(Smi::FromInt(size)));
3084 CallRuntime(Runtime::kAllocateInNewSpace, 1, instr);
3085 __ pop(ebx);
3086
3087 __ bind(&allocated);
3088 // Copy the content into the newly allocated memory.
3089 // (Unroll copy loop once for better throughput).
3090 for (int i = 0; i < size - kPointerSize; i += 2 * kPointerSize) {
3091 __ mov(edx, FieldOperand(ebx, i));
3092 __ mov(ecx, FieldOperand(ebx, i + kPointerSize));
3093 __ mov(FieldOperand(eax, i), edx);
3094 __ mov(FieldOperand(eax, i + kPointerSize), ecx);
3095 }
3096 if ((size % (2 * kPointerSize)) != 0) {
3097 __ mov(edx, FieldOperand(ebx, size - kPointerSize));
3098 __ mov(FieldOperand(eax, size - kPointerSize), edx);
3099 }
3100}
3101
3102
3103void LCodeGen::DoFunctionLiteral(LFunctionLiteral* instr) {
3104 // Use the fast case closure allocation code that allocates in new
3105 // space for nested functions that don't need literals cloning.
3106 Handle<SharedFunctionInfo> shared_info = instr->shared_info();
3107 bool pretenure = !instr->hydrogen()->pretenure();
3108 if (shared_info->num_literals() == 0 && !pretenure) {
3109 FastNewClosureStub stub;
3110 __ push(Immediate(shared_info));
3111 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
3112 } else {
3113 __ push(esi);
3114 __ push(Immediate(shared_info));
3115 __ push(Immediate(pretenure
3116 ? Factory::true_value()
3117 : Factory::false_value()));
3118 CallRuntime(Runtime::kNewClosure, 3, instr);
3119 }
3120}
3121
3122
3123void LCodeGen::DoTypeof(LTypeof* instr) {
3124 LOperand* input = instr->input();
3125 if (input->IsConstantOperand()) {
3126 __ push(ToImmediate(input));
3127 } else {
3128 __ push(ToOperand(input));
3129 }
3130 CallRuntime(Runtime::kTypeof, 1, instr);
3131}
3132
3133
3134void LCodeGen::DoTypeofIs(LTypeofIs* instr) {
3135 Register input = ToRegister(instr->input());
3136 Register result = ToRegister(instr->result());
3137 Label true_label;
3138 Label false_label;
3139 NearLabel done;
3140
3141 Condition final_branch_condition = EmitTypeofIs(&true_label,
3142 &false_label,
3143 input,
3144 instr->type_literal());
3145 __ j(final_branch_condition, &true_label);
3146 __ bind(&false_label);
3147 __ mov(result, Handle<Object>(Heap::false_value()));
3148 __ jmp(&done);
3149
3150 __ bind(&true_label);
3151 __ mov(result, Handle<Object>(Heap::true_value()));
3152
3153 __ bind(&done);
3154}
3155
3156
3157void LCodeGen::DoTypeofIsAndBranch(LTypeofIsAndBranch* instr) {
3158 Register input = ToRegister(instr->input());
3159 int true_block = chunk_->LookupDestination(instr->true_block_id());
3160 int false_block = chunk_->LookupDestination(instr->false_block_id());
3161 Label* true_label = chunk_->GetAssemblyLabel(true_block);
3162 Label* false_label = chunk_->GetAssemblyLabel(false_block);
3163
3164 Condition final_branch_condition = EmitTypeofIs(true_label,
3165 false_label,
3166 input,
3167 instr->type_literal());
3168
3169 EmitBranch(true_block, false_block, final_branch_condition);
3170}
3171
3172
3173Condition LCodeGen::EmitTypeofIs(Label* true_label,
3174 Label* false_label,
3175 Register input,
3176 Handle<String> type_name) {
3177 Condition final_branch_condition = no_condition;
3178 if (type_name->Equals(Heap::number_symbol())) {
3179 __ test(input, Immediate(kSmiTagMask));
3180 __ j(zero, true_label);
3181 __ cmp(FieldOperand(input, HeapObject::kMapOffset),
3182 Factory::heap_number_map());
3183 final_branch_condition = equal;
3184
3185 } else if (type_name->Equals(Heap::string_symbol())) {
3186 __ test(input, Immediate(kSmiTagMask));
3187 __ j(zero, false_label);
3188 __ mov(input, FieldOperand(input, HeapObject::kMapOffset));
3189 __ test_b(FieldOperand(input, Map::kBitFieldOffset),
3190 1 << Map::kIsUndetectable);
3191 __ j(not_zero, false_label);
3192 __ CmpInstanceType(input, FIRST_NONSTRING_TYPE);
3193 final_branch_condition = below;
3194
3195 } else if (type_name->Equals(Heap::boolean_symbol())) {
3196 __ cmp(input, Handle<Object>(Heap::true_value()));
3197 __ j(equal, true_label);
3198 __ cmp(input, Handle<Object>(Heap::false_value()));
3199 final_branch_condition = equal;
3200
3201 } else if (type_name->Equals(Heap::undefined_symbol())) {
3202 __ cmp(input, Factory::undefined_value());
3203 __ j(equal, true_label);
3204 __ test(input, Immediate(kSmiTagMask));
3205 __ j(zero, false_label);
3206 // Check for undetectable objects => true.
3207 __ mov(input, FieldOperand(input, HeapObject::kMapOffset));
3208 __ test_b(FieldOperand(input, Map::kBitFieldOffset),
3209 1 << Map::kIsUndetectable);
3210 final_branch_condition = not_zero;
3211
3212 } else if (type_name->Equals(Heap::function_symbol())) {
3213 __ test(input, Immediate(kSmiTagMask));
3214 __ j(zero, false_label);
3215 __ CmpObjectType(input, JS_FUNCTION_TYPE, input);
3216 __ j(equal, true_label);
3217 // Regular expressions => 'function' (they are callable).
3218 __ CmpInstanceType(input, JS_REGEXP_TYPE);
3219 final_branch_condition = equal;
3220
3221 } else if (type_name->Equals(Heap::object_symbol())) {
3222 __ test(input, Immediate(kSmiTagMask));
3223 __ j(zero, false_label);
3224 __ cmp(input, Factory::null_value());
3225 __ j(equal, true_label);
3226 // Regular expressions => 'function', not 'object'.
3227 __ CmpObjectType(input, JS_REGEXP_TYPE, input);
3228 __ j(equal, false_label);
3229 // Check for undetectable objects => false.
3230 __ test_b(FieldOperand(input, Map::kBitFieldOffset),
3231 1 << Map::kIsUndetectable);
3232 __ j(not_zero, false_label);
3233 // Check for JS objects => true.
3234 __ CmpInstanceType(input, FIRST_JS_OBJECT_TYPE);
3235 __ j(below, false_label);
3236 __ CmpInstanceType(input, LAST_JS_OBJECT_TYPE);
3237 final_branch_condition = below_equal;
3238
3239 } else {
3240 final_branch_condition = not_equal;
3241 __ jmp(false_label);
3242 // A dead branch instruction will be generated after this point.
3243 }
3244
3245 return final_branch_condition;
3246}
3247
3248
3249void LCodeGen::DoLazyBailout(LLazyBailout* instr) {
3250 // No code for lazy bailout instruction. Used to capture environment after a
3251 // call for populating the safepoint data with deoptimization data.
3252}
3253
3254
3255void LCodeGen::DoDeoptimize(LDeoptimize* instr) {
3256 DeoptimizeIf(no_condition, instr->environment());
3257}
3258
3259
3260void LCodeGen::DoDeleteProperty(LDeleteProperty* instr) {
3261 LOperand* obj = instr->object();
3262 LOperand* key = instr->key();
3263 __ push(ToOperand(obj));
3264 if (key->IsConstantOperand()) {
3265 __ push(ToImmediate(key));
3266 } else {
3267 __ push(ToOperand(key));
3268 }
3269 RecordPosition(instr->pointer_map()->position());
3270 SafepointGenerator safepoint_generator(this,
3271 instr->pointer_map(),
3272 Safepoint::kNoDeoptimizationIndex);
3273 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION, &safepoint_generator);
3274}
3275
3276
3277void LCodeGen::DoStackCheck(LStackCheck* instr) {
3278 // Perform stack overflow check.
3279 NearLabel done;
3280 ExternalReference stack_limit = ExternalReference::address_of_stack_limit();
3281 __ cmp(esp, Operand::StaticVariable(stack_limit));
3282 __ j(above_equal, &done);
3283
3284 StackCheckStub stub;
3285 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
3286 __ bind(&done);
3287}
3288
3289
3290void LCodeGen::DoOsrEntry(LOsrEntry* instr) {
3291 // This is a pseudo-instruction that ensures that the environment here is
3292 // properly registered for deoptimization and records the assembler's PC
3293 // offset.
3294 LEnvironment* environment = instr->environment();
3295 environment->SetSpilledRegisters(instr->SpilledRegisterArray(),
3296 instr->SpilledDoubleRegisterArray());
3297
3298 // If the environment were already registered, we would have no way of
3299 // backpatching it with the spill slot operands.
3300 ASSERT(!environment->HasBeenRegistered());
3301 RegisterEnvironmentForDeoptimization(environment);
3302 ASSERT(osr_pc_offset_ == -1);
3303 osr_pc_offset_ = masm()->pc_offset();
3304}
3305
3306
3307#undef __
3308
3309} } // namespace v8::internal