blob: e59dc512df2db8fe9c9b742494552b6dc7d12485 [file] [log] [blame]
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001// Copyright 2009 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 "v8.h"
29
30#include "codegen-inl.h"
31#include "compiler.h"
32#include "debug.h"
33#include "full-codegen.h"
34#include "parser.h"
sgjesse@chromium.org833cdd72010-02-26 10:06:16 +000035#include "scopes.h"
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000036
37namespace v8 {
38namespace internal {
39
40#define __ ACCESS_MASM(masm_)
41
42// Generate code for a JS function. On entry to the function the receiver
43// and arguments have been pushed on the stack left to right, with the
44// return address on top of them. The actual argument count matches the
45// formal parameter count expected by the function.
46//
47// The live registers are:
48// o edi: the JS function object being called (ie, ourselves)
49// o esi: our context
50// o ebp: our caller's frame pointer
51// o esp: stack pointer (pointing to return address)
52//
53// The function builds a JS frame. Please see JavaScriptFrameConstants in
54// frames-ia32.h for its layout.
ager@chromium.org5c838252010-02-19 08:53:10 +000055void FullCodeGenerator::Generate(CompilationInfo* info, Mode mode) {
56 ASSERT(info_ == NULL);
57 info_ = info;
58 SetFunctionPosition(function());
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000059 Comment cmnt(masm_, "[ function compiled by full code generator");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000060
61 if (mode == PRIMARY) {
62 __ push(ebp); // Caller's frame pointer.
63 __ mov(ebp, esp);
64 __ push(esi); // Callee's context.
65 __ push(edi); // Callee's JS Function.
66
67 { Comment cmnt(masm_, "[ Allocate locals");
ager@chromium.org5c838252010-02-19 08:53:10 +000068 int locals_count = scope()->num_stack_slots();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000069 if (locals_count == 1) {
70 __ push(Immediate(Factory::undefined_value()));
71 } else if (locals_count > 1) {
72 __ mov(eax, Immediate(Factory::undefined_value()));
73 for (int i = 0; i < locals_count; i++) {
74 __ push(eax);
75 }
76 }
77 }
78
79 bool function_in_register = true;
80
81 // Possibly allocate a local context.
ager@chromium.org5c838252010-02-19 08:53:10 +000082 if (scope()->num_heap_slots() > 0) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000083 Comment cmnt(masm_, "[ Allocate local context");
84 // Argument to NewContext is the function, which is still in edi.
85 __ push(edi);
86 __ CallRuntime(Runtime::kNewContext, 1);
87 function_in_register = false;
88 // Context is returned in both eax and esi. It replaces the context
89 // passed to us. It's saved in the stack and kept live in esi.
90 __ mov(Operand(ebp, StandardFrameConstants::kContextOffset), esi);
91
92 // Copy parameters into context if necessary.
ager@chromium.org5c838252010-02-19 08:53:10 +000093 int num_parameters = scope()->num_parameters();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000094 for (int i = 0; i < num_parameters; i++) {
ager@chromium.org5c838252010-02-19 08:53:10 +000095 Slot* slot = scope()->parameter(i)->slot();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000096 if (slot != NULL && slot->type() == Slot::CONTEXT) {
97 int parameter_offset = StandardFrameConstants::kCallerSPOffset +
98 (num_parameters - 1 - i) * kPointerSize;
99 // Load parameter from stack.
100 __ mov(eax, Operand(ebp, parameter_offset));
101 // Store it in the context.
102 int context_offset = Context::SlotOffset(slot->index());
103 __ mov(Operand(esi, context_offset), eax);
104 // Update the write barrier. This clobbers all involved
105 // registers, so we have use a third register to avoid
106 // clobbering esi.
107 __ mov(ecx, esi);
108 __ RecordWrite(ecx, context_offset, eax, ebx);
109 }
110 }
111 }
112
ager@chromium.org5c838252010-02-19 08:53:10 +0000113 Variable* arguments = scope()->arguments()->AsVariable();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000114 if (arguments != NULL) {
115 // Function uses arguments object.
116 Comment cmnt(masm_, "[ Allocate arguments object");
117 if (function_in_register) {
118 __ push(edi);
119 } else {
120 __ push(Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
121 }
122 // Receiver is just before the parameters on the caller's stack.
ager@chromium.org5c838252010-02-19 08:53:10 +0000123 int offset = scope()->num_parameters() * kPointerSize;
124 __ lea(edx,
125 Operand(ebp, StandardFrameConstants::kCallerSPOffset + offset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000126 __ push(edx);
ager@chromium.org5c838252010-02-19 08:53:10 +0000127 __ push(Immediate(Smi::FromInt(scope()->num_parameters())));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000128 // Arguments to ArgumentsAccessStub:
129 // function, receiver address, parameter count.
130 // The stub will rewrite receiver and parameter count if the previous
131 // stack frame was an arguments adapter frame.
132 ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT);
133 __ CallStub(&stub);
134 __ mov(ecx, eax); // Duplicate result.
135 Move(arguments->slot(), eax, ebx, edx);
136 Slot* dot_arguments_slot =
ager@chromium.org5c838252010-02-19 08:53:10 +0000137 scope()->arguments_shadow()->AsVariable()->slot();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000138 Move(dot_arguments_slot, ecx, ebx, edx);
139 }
140 }
141
142 { Comment cmnt(masm_, "[ Declarations");
ager@chromium.org5c838252010-02-19 08:53:10 +0000143 VisitDeclarations(scope()->declarations());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000144 }
145
146 { Comment cmnt(masm_, "[ Stack check");
147 Label ok;
148 ExternalReference stack_limit =
149 ExternalReference::address_of_stack_limit();
150 __ cmp(esp, Operand::StaticVariable(stack_limit));
151 __ j(above_equal, &ok, taken);
152 StackCheckStub stub;
153 __ CallStub(&stub);
154 __ bind(&ok);
155 }
156
157 if (FLAG_trace) {
158 __ CallRuntime(Runtime::kTraceEnter, 0);
159 }
160
161 { Comment cmnt(masm_, "[ Body");
162 ASSERT(loop_depth() == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +0000163 VisitStatements(function()->body());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000164 ASSERT(loop_depth() == 0);
165 }
166
167 { Comment cmnt(masm_, "[ return <undefined>;");
168 // Emit a 'return undefined' in case control fell off the end of the body.
169 __ mov(eax, Factory::undefined_value());
ager@chromium.org5c838252010-02-19 08:53:10 +0000170 EmitReturnSequence(function()->end_position());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000171 }
172}
173
174
175void FullCodeGenerator::EmitReturnSequence(int position) {
176 Comment cmnt(masm_, "[ Return sequence");
177 if (return_label_.is_bound()) {
178 __ jmp(&return_label_);
179 } else {
180 // Common return label
181 __ bind(&return_label_);
182 if (FLAG_trace) {
183 __ push(eax);
184 __ CallRuntime(Runtime::kTraceExit, 1);
185 }
186#ifdef DEBUG
187 // Add a label for checking the size of the code used for returning.
188 Label check_exit_codesize;
189 masm_->bind(&check_exit_codesize);
190#endif
191 CodeGenerator::RecordPositions(masm_, position);
192 __ RecordJSReturn();
193 // Do not use the leave instruction here because it is too short to
194 // patch with the code required by the debugger.
195 __ mov(esp, ebp);
196 __ pop(ebp);
ager@chromium.org5c838252010-02-19 08:53:10 +0000197 __ ret((scope()->num_parameters() + 1) * kPointerSize);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000198#ifdef ENABLE_DEBUGGER_SUPPORT
199 // Check that the size of the code used for returning matches what is
200 // expected by the debugger.
201 ASSERT_EQ(Assembler::kJSReturnSequenceLength,
202 masm_->SizeOfCodeGeneratedSince(&check_exit_codesize));
203#endif
204 }
205}
206
207
208void FullCodeGenerator::Apply(Expression::Context context, Register reg) {
209 switch (context) {
210 case Expression::kUninitialized:
211 UNREACHABLE();
212
213 case Expression::kEffect:
214 // Nothing to do.
215 break;
216
217 case Expression::kValue:
218 // Move value into place.
219 switch (location_) {
220 case kAccumulator:
221 if (!reg.is(result_register())) __ mov(result_register(), reg);
222 break;
223 case kStack:
224 __ push(reg);
225 break;
226 }
227 break;
228
229 case Expression::kTest:
230 // For simplicity we always test the accumulator register.
231 if (!reg.is(result_register())) __ mov(result_register(), reg);
232 DoTest(context);
233 break;
234
235 case Expression::kValueTest:
236 case Expression::kTestValue:
237 if (!reg.is(result_register())) __ mov(result_register(), reg);
238 switch (location_) {
239 case kAccumulator:
240 break;
241 case kStack:
242 __ push(result_register());
243 break;
244 }
245 DoTest(context);
246 break;
247 }
248}
249
250
251void FullCodeGenerator::Apply(Expression::Context context, Slot* slot) {
252 switch (context) {
253 case Expression::kUninitialized:
254 UNREACHABLE();
255 case Expression::kEffect:
256 // Nothing to do.
257 break;
258 case Expression::kValue: {
259 MemOperand slot_operand = EmitSlotSearch(slot, result_register());
260 switch (location_) {
261 case kAccumulator:
262 __ mov(result_register(), slot_operand);
263 break;
264 case kStack:
265 // Memory operands can be pushed directly.
266 __ push(slot_operand);
267 break;
268 }
269 break;
270 }
271
272 case Expression::kTest:
273 // For simplicity we always test the accumulator register.
274 Move(result_register(), slot);
275 DoTest(context);
276 break;
277
278 case Expression::kValueTest:
279 case Expression::kTestValue:
280 Move(result_register(), slot);
281 switch (location_) {
282 case kAccumulator:
283 break;
284 case kStack:
285 __ push(result_register());
286 break;
287 }
288 DoTest(context);
289 break;
290 }
291}
292
293
294void FullCodeGenerator::Apply(Expression::Context context, Literal* lit) {
295 switch (context) {
296 case Expression::kUninitialized:
297 UNREACHABLE();
298 case Expression::kEffect:
299 // Nothing to do.
300 break;
301 case Expression::kValue:
302 switch (location_) {
303 case kAccumulator:
304 __ mov(result_register(), lit->handle());
305 break;
306 case kStack:
307 // Immediates can be pushed directly.
308 __ push(Immediate(lit->handle()));
309 break;
310 }
311 break;
312
313 case Expression::kTest:
314 // For simplicity we always test the accumulator register.
315 __ mov(result_register(), lit->handle());
316 DoTest(context);
317 break;
318
319 case Expression::kValueTest:
320 case Expression::kTestValue:
321 __ mov(result_register(), lit->handle());
322 switch (location_) {
323 case kAccumulator:
324 break;
325 case kStack:
326 __ push(result_register());
327 break;
328 }
329 DoTest(context);
330 break;
331 }
332}
333
334
335void FullCodeGenerator::ApplyTOS(Expression::Context context) {
336 switch (context) {
337 case Expression::kUninitialized:
338 UNREACHABLE();
339
340 case Expression::kEffect:
341 __ Drop(1);
342 break;
343
344 case Expression::kValue:
345 switch (location_) {
346 case kAccumulator:
347 __ pop(result_register());
348 break;
349 case kStack:
350 break;
351 }
352 break;
353
354 case Expression::kTest:
355 // For simplicity we always test the accumulator register.
356 __ pop(result_register());
357 DoTest(context);
358 break;
359
360 case Expression::kValueTest:
361 case Expression::kTestValue:
362 switch (location_) {
363 case kAccumulator:
364 __ pop(result_register());
365 break;
366 case kStack:
367 __ mov(result_register(), Operand(esp, 0));
368 break;
369 }
370 DoTest(context);
371 break;
372 }
373}
374
375
376void FullCodeGenerator::DropAndApply(int count,
377 Expression::Context context,
378 Register reg) {
379 ASSERT(count > 0);
380 ASSERT(!reg.is(esp));
381 switch (context) {
382 case Expression::kUninitialized:
383 UNREACHABLE();
384
385 case Expression::kEffect:
386 __ Drop(count);
387 break;
388
389 case Expression::kValue:
390 switch (location_) {
391 case kAccumulator:
392 __ Drop(count);
393 if (!reg.is(result_register())) __ mov(result_register(), reg);
394 break;
395 case kStack:
396 if (count > 1) __ Drop(count - 1);
397 __ mov(Operand(esp, 0), reg);
398 break;
399 }
400 break;
401
402 case Expression::kTest:
403 // For simplicity we always test the accumulator register.
404 __ Drop(count);
405 if (!reg.is(result_register())) __ mov(result_register(), reg);
406 DoTest(context);
407 break;
408
409 case Expression::kValueTest:
410 case Expression::kTestValue:
411 switch (location_) {
412 case kAccumulator:
413 __ Drop(count);
414 if (!reg.is(result_register())) __ mov(result_register(), reg);
415 break;
416 case kStack:
417 if (count > 1) __ Drop(count - 1);
418 __ mov(result_register(), reg);
419 __ mov(Operand(esp, 0), result_register());
420 break;
421 }
422 DoTest(context);
423 break;
424 }
425}
426
427
428void FullCodeGenerator::Apply(Expression::Context context,
429 Label* materialize_true,
430 Label* materialize_false) {
431 switch (context) {
432 case Expression::kUninitialized:
433
434 case Expression::kEffect:
435 ASSERT_EQ(materialize_true, materialize_false);
436 __ bind(materialize_true);
437 break;
438
439 case Expression::kValue: {
440 Label done;
441 switch (location_) {
442 case kAccumulator:
443 __ bind(materialize_true);
444 __ mov(result_register(), Factory::true_value());
445 __ jmp(&done);
446 __ bind(materialize_false);
447 __ mov(result_register(), Factory::false_value());
448 break;
449 case kStack:
450 __ bind(materialize_true);
451 __ push(Immediate(Factory::true_value()));
452 __ jmp(&done);
453 __ bind(materialize_false);
454 __ push(Immediate(Factory::false_value()));
455 break;
456 }
457 __ bind(&done);
458 break;
459 }
460
461 case Expression::kTest:
462 break;
463
464 case Expression::kValueTest:
465 __ bind(materialize_true);
466 switch (location_) {
467 case kAccumulator:
468 __ mov(result_register(), Factory::true_value());
469 break;
470 case kStack:
471 __ push(Immediate(Factory::true_value()));
472 break;
473 }
474 __ jmp(true_label_);
475 break;
476
477 case Expression::kTestValue:
478 __ bind(materialize_false);
479 switch (location_) {
480 case kAccumulator:
481 __ mov(result_register(), Factory::false_value());
482 break;
483 case kStack:
484 __ push(Immediate(Factory::false_value()));
485 break;
486 }
487 __ jmp(false_label_);
488 break;
489 }
490}
491
492
493void FullCodeGenerator::DoTest(Expression::Context context) {
494 // The value to test is in the accumulator. If the value might be needed
495 // on the stack (value/test and test/value contexts with a stack location
496 // desired), then the value is already duplicated on the stack.
497 ASSERT_NE(NULL, true_label_);
498 ASSERT_NE(NULL, false_label_);
499
500 // In value/test and test/value expression contexts with stack as the
501 // desired location, there is already an extra value on the stack. Use a
502 // label to discard it if unneeded.
503 Label discard;
504 Label* if_true = true_label_;
505 Label* if_false = false_label_;
506 switch (context) {
507 case Expression::kUninitialized:
508 case Expression::kEffect:
509 case Expression::kValue:
510 UNREACHABLE();
511 case Expression::kTest:
512 break;
513 case Expression::kValueTest:
514 switch (location_) {
515 case kAccumulator:
516 break;
517 case kStack:
518 if_false = &discard;
519 break;
520 }
521 break;
522 case Expression::kTestValue:
523 switch (location_) {
524 case kAccumulator:
525 break;
526 case kStack:
527 if_true = &discard;
528 break;
529 }
530 break;
531 }
532
533 // Emit the inlined tests assumed by the stub.
534 __ cmp(result_register(), Factory::undefined_value());
535 __ j(equal, if_false);
536 __ cmp(result_register(), Factory::true_value());
537 __ j(equal, if_true);
538 __ cmp(result_register(), Factory::false_value());
539 __ j(equal, if_false);
540 ASSERT_EQ(0, kSmiTag);
541 __ test(result_register(), Operand(result_register()));
542 __ j(zero, if_false);
543 __ test(result_register(), Immediate(kSmiTagMask));
544 __ j(zero, if_true);
545
546 // Save a copy of the value if it may be needed and isn't already saved.
547 switch (context) {
548 case Expression::kUninitialized:
549 case Expression::kEffect:
550 case Expression::kValue:
551 UNREACHABLE();
552 case Expression::kTest:
553 break;
554 case Expression::kValueTest:
555 switch (location_) {
556 case kAccumulator:
557 __ push(result_register());
558 break;
559 case kStack:
560 break;
561 }
562 break;
563 case Expression::kTestValue:
564 switch (location_) {
565 case kAccumulator:
566 __ push(result_register());
567 break;
568 case kStack:
569 break;
570 }
571 break;
572 }
573
574 // Call the ToBoolean stub for all other cases.
575 ToBooleanStub stub;
576 __ push(result_register());
577 __ CallStub(&stub);
578 __ test(eax, Operand(eax));
579
580 // The stub returns nonzero for true. Complete based on the context.
581 switch (context) {
582 case Expression::kUninitialized:
583 case Expression::kEffect:
584 case Expression::kValue:
585 UNREACHABLE();
586
587 case Expression::kTest:
588 __ j(not_zero, true_label_);
589 __ jmp(false_label_);
590 break;
591
592 case Expression::kValueTest:
593 switch (location_) {
594 case kAccumulator:
595 __ j(zero, &discard);
596 __ pop(result_register());
597 __ jmp(true_label_);
598 break;
599 case kStack:
600 __ j(not_zero, true_label_);
601 break;
602 }
603 __ bind(&discard);
604 __ Drop(1);
605 __ jmp(false_label_);
606 break;
607
608 case Expression::kTestValue:
609 switch (location_) {
610 case kAccumulator:
611 __ j(not_zero, &discard);
612 __ pop(result_register());
613 __ jmp(false_label_);
614 break;
615 case kStack:
616 __ j(zero, false_label_);
617 break;
618 }
619 __ bind(&discard);
620 __ Drop(1);
621 __ jmp(true_label_);
622 break;
623 }
624}
625
626
627MemOperand FullCodeGenerator::EmitSlotSearch(Slot* slot, Register scratch) {
628 switch (slot->type()) {
629 case Slot::PARAMETER:
630 case Slot::LOCAL:
631 return Operand(ebp, SlotOffset(slot));
632 case Slot::CONTEXT: {
633 int context_chain_length =
ager@chromium.org5c838252010-02-19 08:53:10 +0000634 scope()->ContextChainLength(slot->var()->scope());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000635 __ LoadContext(scratch, context_chain_length);
636 return CodeGenerator::ContextOperand(scratch, slot->index());
637 }
638 case Slot::LOOKUP:
639 UNREACHABLE();
640 }
641 UNREACHABLE();
642 return Operand(eax, 0);
643}
644
645
646void FullCodeGenerator::Move(Register destination, Slot* source) {
647 MemOperand location = EmitSlotSearch(source, destination);
648 __ mov(destination, location);
649}
650
651
652void FullCodeGenerator::Move(Slot* dst,
653 Register src,
654 Register scratch1,
655 Register scratch2) {
656 ASSERT(dst->type() != Slot::LOOKUP); // Not yet implemented.
657 ASSERT(!scratch1.is(src) && !scratch2.is(src));
658 MemOperand location = EmitSlotSearch(dst, scratch1);
659 __ mov(location, src);
660 // Emit the write barrier code if the location is in the heap.
661 if (dst->type() == Slot::CONTEXT) {
662 int offset = FixedArray::kHeaderSize + dst->index() * kPointerSize;
663 __ RecordWrite(scratch1, offset, src, scratch2);
664 }
665}
666
667
668void FullCodeGenerator::VisitDeclaration(Declaration* decl) {
669 Comment cmnt(masm_, "[ Declaration");
670 Variable* var = decl->proxy()->var();
671 ASSERT(var != NULL); // Must have been resolved.
672 Slot* slot = var->slot();
673 Property* prop = var->AsProperty();
674
675 if (slot != NULL) {
676 switch (slot->type()) {
677 case Slot::PARAMETER:
678 case Slot::LOCAL:
679 if (decl->mode() == Variable::CONST) {
680 __ mov(Operand(ebp, SlotOffset(slot)),
681 Immediate(Factory::the_hole_value()));
682 } else if (decl->fun() != NULL) {
683 VisitForValue(decl->fun(), kAccumulator);
684 __ mov(Operand(ebp, SlotOffset(slot)), result_register());
685 }
686 break;
687
688 case Slot::CONTEXT:
689 // We bypass the general EmitSlotSearch because we know more about
690 // this specific context.
691
692 // The variable in the decl always resides in the current context.
ager@chromium.org5c838252010-02-19 08:53:10 +0000693 ASSERT_EQ(0, scope()->ContextChainLength(var->scope()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000694 if (FLAG_debug_code) {
695 // Check if we have the correct context pointer.
696 __ mov(ebx,
697 CodeGenerator::ContextOperand(esi, Context::FCONTEXT_INDEX));
698 __ cmp(ebx, Operand(esi));
699 __ Check(equal, "Unexpected declaration in current context.");
700 }
701 if (decl->mode() == Variable::CONST) {
702 __ mov(eax, Immediate(Factory::the_hole_value()));
703 __ mov(CodeGenerator::ContextOperand(esi, slot->index()), eax);
704 // No write barrier since the hole value is in old space.
705 } else if (decl->fun() != NULL) {
706 VisitForValue(decl->fun(), kAccumulator);
707 __ mov(CodeGenerator::ContextOperand(esi, slot->index()),
708 result_register());
709 int offset = Context::SlotOffset(slot->index());
710 __ mov(ebx, esi);
711 __ RecordWrite(ebx, offset, result_register(), ecx);
712 }
713 break;
714
715 case Slot::LOOKUP: {
716 __ push(esi);
717 __ push(Immediate(var->name()));
718 // Declaration nodes are always introduced in one of two modes.
719 ASSERT(decl->mode() == Variable::VAR ||
720 decl->mode() == Variable::CONST);
721 PropertyAttributes attr =
722 (decl->mode() == Variable::VAR) ? NONE : READ_ONLY;
723 __ push(Immediate(Smi::FromInt(attr)));
724 // Push initial value, if any.
725 // Note: For variables we must not push an initial value (such as
726 // 'undefined') because we may have a (legal) redeclaration and we
727 // must not destroy the current value.
728 if (decl->mode() == Variable::CONST) {
729 __ push(Immediate(Factory::the_hole_value()));
730 } else if (decl->fun() != NULL) {
731 VisitForValue(decl->fun(), kStack);
732 } else {
733 __ push(Immediate(Smi::FromInt(0))); // No initial value!
734 }
735 __ CallRuntime(Runtime::kDeclareContextSlot, 4);
736 break;
737 }
738 }
739
740 } else if (prop != NULL) {
741 if (decl->fun() != NULL || decl->mode() == Variable::CONST) {
742 // We are declaring a function or constant that rewrites to a
743 // property. Use (keyed) IC to set the initial value.
744 VisitForValue(prop->obj(), kStack);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000745 if (decl->fun() != NULL) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000746 VisitForValue(prop->key(), kStack);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000747 VisitForValue(decl->fun(), kAccumulator);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000748 __ pop(ecx);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000749 } else {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000750 VisitForValue(prop->key(), kAccumulator);
751 __ mov(ecx, result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000752 __ mov(result_register(), Factory::the_hole_value());
753 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000754 __ pop(edx);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000755
756 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
757 __ call(ic, RelocInfo::CODE_TARGET);
758 // Absence of a test eax instruction following the call
759 // indicates that none of the load was inlined.
760 __ nop();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000761 }
762 }
763}
764
765
766void FullCodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
767 // Call the runtime to declare the globals.
768 __ push(esi); // The context is the first argument.
769 __ push(Immediate(pairs));
ager@chromium.org5c838252010-02-19 08:53:10 +0000770 __ push(Immediate(Smi::FromInt(is_eval() ? 1 : 0)));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000771 __ CallRuntime(Runtime::kDeclareGlobals, 3);
772 // Return value is ignored.
773}
774
775
776void FullCodeGenerator::VisitFunctionLiteral(FunctionLiteral* expr) {
777 Comment cmnt(masm_, "[ FunctionLiteral");
778
779 // Build the function boilerplate and instantiate it.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000780 Handle<SharedFunctionInfo> function_info =
781 Compiler::BuildFunctionInfo(expr, script(), this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000782 if (HasStackOverflow()) return;
783
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000784 // Create a new closure.
785 __ push(esi);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000786 __ push(Immediate(function_info));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000787 __ CallRuntime(Runtime::kNewClosure, 2);
788 Apply(context_, eax);
789}
790
791
792void FullCodeGenerator::VisitVariableProxy(VariableProxy* expr) {
793 Comment cmnt(masm_, "[ VariableProxy");
794 EmitVariableLoad(expr->var(), context_);
795}
796
797
798void FullCodeGenerator::EmitVariableLoad(Variable* var,
799 Expression::Context context) {
800 // Four cases: non-this global variables, lookup slots, all other
801 // types of slots, and parameters that rewrite to explicit property
802 // accesses on the arguments object.
803 Slot* slot = var->slot();
804 Property* property = var->AsProperty();
805
806 if (var->is_global() && !var->is_this()) {
807 Comment cmnt(masm_, "Global variable");
808 // Use inline caching. Variable name is passed in ecx and the global
809 // object on the stack.
ager@chromium.org5c838252010-02-19 08:53:10 +0000810 __ mov(eax, CodeGenerator::GlobalObject());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000811 __ mov(ecx, var->name());
812 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
813 __ call(ic, RelocInfo::CODE_TARGET_CONTEXT);
814 // By emitting a nop we make sure that we do not have a test eax
815 // instruction after the call it is treated specially by the LoadIC code
816 // Remember that the assembler may choose to do peephole optimization
817 // (eg, push/pop elimination).
818 __ nop();
ager@chromium.org5c838252010-02-19 08:53:10 +0000819 Apply(context, eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000820
821 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
822 Comment cmnt(masm_, "Lookup slot");
823 __ push(esi); // Context.
824 __ push(Immediate(var->name()));
825 __ CallRuntime(Runtime::kLoadContextSlot, 2);
826 Apply(context, eax);
827
828 } else if (slot != NULL) {
829 Comment cmnt(masm_, (slot->type() == Slot::CONTEXT)
830 ? "Context slot"
831 : "Stack slot");
832 Apply(context, slot);
833
834 } else {
835 Comment cmnt(masm_, "Rewritten parameter");
836 ASSERT_NOT_NULL(property);
837 // Rewritten parameter accesses are of the form "slot[literal]".
838
839 // Assert that the object is in a slot.
840 Variable* object_var = property->obj()->AsVariableProxy()->AsVariable();
841 ASSERT_NOT_NULL(object_var);
842 Slot* object_slot = object_var->slot();
843 ASSERT_NOT_NULL(object_slot);
844
845 // Load the object.
846 MemOperand object_loc = EmitSlotSearch(object_slot, eax);
ager@chromium.org5c838252010-02-19 08:53:10 +0000847 __ mov(edx, object_loc);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000848
849 // Assert that the key is a smi.
850 Literal* key_literal = property->key()->AsLiteral();
851 ASSERT_NOT_NULL(key_literal);
852 ASSERT(key_literal->handle()->IsSmi());
853
854 // Load the key.
ager@chromium.org5c838252010-02-19 08:53:10 +0000855 __ mov(eax, Immediate(key_literal->handle()));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000856
857 // Do a keyed property load.
858 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
859 __ call(ic, RelocInfo::CODE_TARGET);
860 // Notice: We must not have a "test eax, ..." instruction after the
861 // call. It is treated specially by the LoadIC code.
862 __ nop();
863 // Drop key and object left on the stack by IC.
ager@chromium.org5c838252010-02-19 08:53:10 +0000864 Apply(context, eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000865 }
866}
867
868
869void FullCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) {
870 Comment cmnt(masm_, "[ RegExpLiteral");
871 Label done;
872 // Registers will be used as follows:
873 // edi = JS function.
874 // ebx = literals array.
875 // eax = regexp literal.
876 __ mov(edi, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
877 __ mov(ebx, FieldOperand(edi, JSFunction::kLiteralsOffset));
878 int literal_offset =
879 FixedArray::kHeaderSize + expr->literal_index() * kPointerSize;
880 __ mov(eax, FieldOperand(ebx, literal_offset));
881 __ cmp(eax, Factory::undefined_value());
882 __ j(not_equal, &done);
883 // Create regexp literal using runtime function
884 // Result will be in eax.
885 __ push(ebx);
886 __ push(Immediate(Smi::FromInt(expr->literal_index())));
887 __ push(Immediate(expr->pattern()));
888 __ push(Immediate(expr->flags()));
889 __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
890 // Label done:
891 __ bind(&done);
892 Apply(context_, eax);
893}
894
895
896void FullCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
897 Comment cmnt(masm_, "[ ObjectLiteral");
898 __ mov(edi, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
899 __ push(FieldOperand(edi, JSFunction::kLiteralsOffset));
900 __ push(Immediate(Smi::FromInt(expr->literal_index())));
901 __ push(Immediate(expr->constant_properties()));
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000902 __ push(Immediate(Smi::FromInt(expr->fast_elements() ? 1 : 0)));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000903 if (expr->depth() > 1) {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000904 __ CallRuntime(Runtime::kCreateObjectLiteral, 4);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000905 } else {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000906 __ CallRuntime(Runtime::kCreateObjectLiteralShallow, 4);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000907 }
908
909 // If result_saved is true the result is on top of the stack. If
910 // result_saved is false the result is in eax.
911 bool result_saved = false;
912
913 for (int i = 0; i < expr->properties()->length(); i++) {
914 ObjectLiteral::Property* property = expr->properties()->at(i);
915 if (property->IsCompileTimeValue()) continue;
916
917 Literal* key = property->key();
918 Expression* value = property->value();
919 if (!result_saved) {
920 __ push(eax); // Save result on the stack
921 result_saved = true;
922 }
923 switch (property->kind()) {
924 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
925 ASSERT(!CompileTimeValue::IsCompileTimeValue(value));
926 // Fall through.
927 case ObjectLiteral::Property::COMPUTED:
928 if (key->handle()->IsSymbol()) {
929 VisitForValue(value, kAccumulator);
930 __ mov(ecx, Immediate(key->handle()));
931 __ mov(edx, Operand(esp, 0));
932 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
933 __ call(ic, RelocInfo::CODE_TARGET);
934 __ nop();
935 break;
936 }
937 // Fall through.
938 case ObjectLiteral::Property::PROTOTYPE:
939 __ push(Operand(esp, 0)); // Duplicate receiver.
940 VisitForValue(key, kStack);
941 VisitForValue(value, kStack);
942 __ CallRuntime(Runtime::kSetProperty, 3);
943 break;
944 case ObjectLiteral::Property::SETTER:
945 case ObjectLiteral::Property::GETTER:
946 __ push(Operand(esp, 0)); // Duplicate receiver.
947 VisitForValue(key, kStack);
948 __ push(Immediate(property->kind() == ObjectLiteral::Property::SETTER ?
949 Smi::FromInt(1) :
950 Smi::FromInt(0)));
951 VisitForValue(value, kStack);
952 __ CallRuntime(Runtime::kDefineAccessor, 4);
953 break;
954 default: UNREACHABLE();
955 }
956 }
957
958 if (result_saved) {
959 ApplyTOS(context_);
960 } else {
961 Apply(context_, eax);
962 }
963}
964
965
966void FullCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
967 Comment cmnt(masm_, "[ ArrayLiteral");
968 __ mov(ebx, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
969 __ push(FieldOperand(ebx, JSFunction::kLiteralsOffset));
970 __ push(Immediate(Smi::FromInt(expr->literal_index())));
971 __ push(Immediate(expr->constant_elements()));
972 if (expr->depth() > 1) {
973 __ CallRuntime(Runtime::kCreateArrayLiteral, 3);
974 } else {
975 __ CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
976 }
977
978 bool result_saved = false; // Is the result saved to the stack?
979
980 // Emit code to evaluate all the non-constant subexpressions and to store
981 // them into the newly cloned array.
982 ZoneList<Expression*>* subexprs = expr->values();
983 for (int i = 0, len = subexprs->length(); i < len; i++) {
984 Expression* subexpr = subexprs->at(i);
985 // If the subexpression is a literal or a simple materialized literal it
986 // is already set in the cloned array.
987 if (subexpr->AsLiteral() != NULL ||
988 CompileTimeValue::IsCompileTimeValue(subexpr)) {
989 continue;
990 }
991
992 if (!result_saved) {
993 __ push(eax);
994 result_saved = true;
995 }
996 VisitForValue(subexpr, kAccumulator);
997
998 // Store the subexpression value in the array's elements.
999 __ mov(ebx, Operand(esp, 0)); // Copy of array literal.
1000 __ mov(ebx, FieldOperand(ebx, JSObject::kElementsOffset));
1001 int offset = FixedArray::kHeaderSize + (i * kPointerSize);
1002 __ mov(FieldOperand(ebx, offset), result_register());
1003
1004 // Update the write barrier for the array store.
1005 __ RecordWrite(ebx, offset, result_register(), ecx);
1006 }
1007
1008 if (result_saved) {
1009 ApplyTOS(context_);
1010 } else {
1011 Apply(context_, eax);
1012 }
1013}
1014
1015
ager@chromium.org5c838252010-02-19 08:53:10 +00001016void FullCodeGenerator::VisitAssignment(Assignment* expr) {
1017 Comment cmnt(masm_, "[ Assignment");
1018 ASSERT(expr->op() != Token::INIT_CONST);
1019 // Left-hand side can only be a property, a global or a (parameter or local)
1020 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
1021 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1022 LhsKind assign_type = VARIABLE;
1023 Property* prop = expr->target()->AsProperty();
1024 if (prop != NULL) {
1025 assign_type =
1026 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
1027 }
1028
1029 // Evaluate LHS expression.
1030 switch (assign_type) {
1031 case VARIABLE:
1032 // Nothing to do here.
1033 break;
1034 case NAMED_PROPERTY:
1035 if (expr->is_compound()) {
1036 // We need the receiver both on the stack and in the accumulator.
1037 VisitForValue(prop->obj(), kAccumulator);
1038 __ push(result_register());
1039 } else {
1040 VisitForValue(prop->obj(), kStack);
1041 }
1042 break;
1043 case KEYED_PROPERTY:
1044 if (expr->is_compound()) {
1045 VisitForValue(prop->obj(), kStack);
1046 VisitForValue(prop->key(), kAccumulator);
1047 __ mov(edx, Operand(esp, 0));
1048 __ push(eax);
1049 } else {
1050 VisitForValue(prop->obj(), kStack);
1051 VisitForValue(prop->key(), kStack);
1052 }
1053 break;
1054 }
1055
1056 // If we have a compound assignment: Get value of LHS expression and
1057 // store in on top of the stack.
1058 if (expr->is_compound()) {
1059 Location saved_location = location_;
1060 location_ = kStack;
1061 switch (assign_type) {
1062 case VARIABLE:
1063 EmitVariableLoad(expr->target()->AsVariableProxy()->var(),
1064 Expression::kValue);
1065 break;
1066 case NAMED_PROPERTY:
1067 EmitNamedPropertyLoad(prop);
1068 __ push(result_register());
1069 break;
1070 case KEYED_PROPERTY:
1071 EmitKeyedPropertyLoad(prop);
1072 __ push(result_register());
1073 break;
1074 }
1075 location_ = saved_location;
1076 }
1077
1078 // Evaluate RHS expression.
1079 Expression* rhs = expr->value();
1080 VisitForValue(rhs, kAccumulator);
1081
1082 // If we have a compound assignment: Apply operator.
1083 if (expr->is_compound()) {
1084 Location saved_location = location_;
1085 location_ = kAccumulator;
1086 EmitBinaryOp(expr->binary_op(), Expression::kValue);
1087 location_ = saved_location;
1088 }
1089
1090 // Record source position before possible IC call.
1091 SetSourcePosition(expr->position());
1092
1093 // Store the value.
1094 switch (assign_type) {
1095 case VARIABLE:
1096 EmitVariableAssignment(expr->target()->AsVariableProxy()->var(),
1097 context_);
1098 break;
1099 case NAMED_PROPERTY:
1100 EmitNamedPropertyAssignment(expr);
1101 break;
1102 case KEYED_PROPERTY:
1103 EmitKeyedPropertyAssignment(expr);
1104 break;
1105 }
1106}
1107
1108
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001109void FullCodeGenerator::EmitNamedPropertyLoad(Property* prop) {
1110 SetSourcePosition(prop->position());
1111 Literal* key = prop->key()->AsLiteral();
1112 __ mov(ecx, Immediate(key->handle()));
1113 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
1114 __ call(ic, RelocInfo::CODE_TARGET);
1115 __ nop();
1116}
1117
1118
1119void FullCodeGenerator::EmitKeyedPropertyLoad(Property* prop) {
1120 SetSourcePosition(prop->position());
1121 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
1122 __ call(ic, RelocInfo::CODE_TARGET);
1123 __ nop();
1124}
1125
1126
1127void FullCodeGenerator::EmitBinaryOp(Token::Value op,
1128 Expression::Context context) {
1129 __ push(result_register());
1130 GenericBinaryOpStub stub(op,
1131 NO_OVERWRITE,
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001132 NO_GENERIC_BINARY_FLAGS,
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00001133 TypeInfo::Unknown());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001134 __ CallStub(&stub);
1135 Apply(context, eax);
1136}
1137
1138
1139void FullCodeGenerator::EmitVariableAssignment(Variable* var,
1140 Expression::Context context) {
1141 // Three main cases: global variables, lookup slots, and all other
1142 // types of slots. Left-hand-side parameters that rewrite to
1143 // explicit property accesses do not reach here.
1144 ASSERT(var != NULL);
1145 ASSERT(var->is_global() || var->slot() != NULL);
1146
1147 Slot* slot = var->slot();
1148 if (var->is_global()) {
1149 ASSERT(!var->is_this());
1150 // Assignment to a global variable. Use inline caching for the
1151 // assignment. Right-hand-side value is passed in eax, variable name in
1152 // ecx, and the global object on the stack.
1153 __ mov(ecx, var->name());
1154 __ mov(edx, CodeGenerator::GlobalObject());
1155 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
1156 __ call(ic, RelocInfo::CODE_TARGET);
1157 __ nop();
1158 Apply(context, eax);
1159
1160 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
1161 __ push(result_register()); // Value.
1162 __ push(esi); // Context.
1163 __ push(Immediate(var->name()));
1164 __ CallRuntime(Runtime::kStoreContextSlot, 3);
1165 Apply(context, eax);
1166
1167 } else if (slot != NULL) {
1168 switch (slot->type()) {
1169 case Slot::LOCAL:
1170 case Slot::PARAMETER:
1171 __ mov(Operand(ebp, SlotOffset(slot)), result_register());
1172 break;
1173
1174 case Slot::CONTEXT: {
1175 MemOperand target = EmitSlotSearch(slot, ecx);
1176 __ mov(target, result_register());
1177
1178 // RecordWrite may destroy all its register arguments.
1179 __ mov(edx, result_register());
1180 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
1181 __ RecordWrite(ecx, offset, edx, ebx);
1182 break;
1183 }
1184
1185 case Slot::LOOKUP:
1186 UNREACHABLE();
1187 break;
1188 }
1189 Apply(context, result_register());
1190
1191 } else {
1192 // Variables rewritten as properties are not treated as variables in
1193 // assignments.
1194 UNREACHABLE();
1195 }
1196}
1197
1198
1199void FullCodeGenerator::EmitNamedPropertyAssignment(Assignment* expr) {
1200 // Assignment to a property, using a named store IC.
1201 Property* prop = expr->target()->AsProperty();
1202 ASSERT(prop != NULL);
1203 ASSERT(prop->key()->AsLiteral() != NULL);
1204
1205 // If the assignment starts a block of assignments to the same object,
1206 // change to slow case to avoid the quadratic behavior of repeatedly
1207 // adding fast properties.
1208 if (expr->starts_initialization_block()) {
1209 __ push(result_register());
1210 __ push(Operand(esp, kPointerSize)); // Receiver is now under value.
1211 __ CallRuntime(Runtime::kToSlowProperties, 1);
1212 __ pop(result_register());
1213 }
1214
1215 // Record source code position before IC call.
1216 SetSourcePosition(expr->position());
1217 __ mov(ecx, prop->key()->AsLiteral()->handle());
1218 if (expr->ends_initialization_block()) {
1219 __ mov(edx, Operand(esp, 0));
1220 } else {
1221 __ pop(edx);
1222 }
1223 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
1224 __ call(ic, RelocInfo::CODE_TARGET);
1225 __ nop();
1226
1227 // If the assignment ends an initialization block, revert to fast case.
1228 if (expr->ends_initialization_block()) {
1229 __ push(eax); // Result of assignment, saved even if not needed.
1230 __ push(Operand(esp, kPointerSize)); // Receiver is under value.
1231 __ CallRuntime(Runtime::kToFastProperties, 1);
1232 __ pop(eax);
1233 DropAndApply(1, context_, eax);
1234 } else {
1235 Apply(context_, eax);
1236 }
1237}
1238
1239
1240void FullCodeGenerator::EmitKeyedPropertyAssignment(Assignment* expr) {
1241 // Assignment to a property, using a keyed store IC.
1242
1243 // If the assignment starts a block of assignments to the same object,
1244 // change to slow case to avoid the quadratic behavior of repeatedly
1245 // adding fast properties.
1246 if (expr->starts_initialization_block()) {
1247 __ push(result_register());
1248 // Receiver is now under the key and value.
1249 __ push(Operand(esp, 2 * kPointerSize));
1250 __ CallRuntime(Runtime::kToSlowProperties, 1);
1251 __ pop(result_register());
1252 }
1253
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001254 __ pop(ecx);
1255 if (expr->ends_initialization_block()) {
1256 __ mov(edx, Operand(esp, 0)); // Leave receiver on the stack for later.
1257 } else {
1258 __ pop(edx);
1259 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001260 // Record source code position before IC call.
1261 SetSourcePosition(expr->position());
1262 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
1263 __ call(ic, RelocInfo::CODE_TARGET);
1264 // This nop signals to the IC that there is no inlined code at the call
1265 // site for it to patch.
1266 __ nop();
1267
1268 // If the assignment ends an initialization block, revert to fast case.
1269 if (expr->ends_initialization_block()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001270 __ pop(edx);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001271 __ push(eax); // Result of assignment, saved even if not needed.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001272 __ push(edx);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001273 __ CallRuntime(Runtime::kToFastProperties, 1);
1274 __ pop(eax);
1275 }
1276
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001277 Apply(context_, eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001278}
1279
1280
1281void FullCodeGenerator::VisitProperty(Property* expr) {
1282 Comment cmnt(masm_, "[ Property");
1283 Expression* key = expr->key();
1284
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001285 if (key->IsPropertyName()) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001286 VisitForValue(expr->obj(), kAccumulator);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001287 EmitNamedPropertyLoad(expr);
ager@chromium.org5c838252010-02-19 08:53:10 +00001288 Apply(context_, eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001289 } else {
ager@chromium.org5c838252010-02-19 08:53:10 +00001290 VisitForValue(expr->obj(), kStack);
1291 VisitForValue(expr->key(), kAccumulator);
1292 __ pop(edx);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001293 EmitKeyedPropertyLoad(expr);
ager@chromium.org5c838252010-02-19 08:53:10 +00001294 Apply(context_, eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001295 }
1296}
1297
1298
1299void FullCodeGenerator::EmitCallWithIC(Call* expr,
1300 Handle<Object> name,
1301 RelocInfo::Mode mode) {
1302 // Code common for calls using the IC.
1303 ZoneList<Expression*>* args = expr->arguments();
1304 int arg_count = args->length();
1305 for (int i = 0; i < arg_count; i++) {
1306 VisitForValue(args->at(i), kStack);
1307 }
1308 __ Set(ecx, Immediate(name));
1309 // Record source position of the IC call.
1310 SetSourcePosition(expr->position());
1311 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1312 Handle<Code> ic = CodeGenerator::ComputeCallInitialize(arg_count, in_loop);
1313 __ call(ic, mode);
1314 // Restore context register.
1315 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
1316 Apply(context_, eax);
1317}
1318
1319
1320void FullCodeGenerator::EmitCallWithStub(Call* expr) {
1321 // Code common for calls using the call stub.
1322 ZoneList<Expression*>* args = expr->arguments();
1323 int arg_count = args->length();
1324 for (int i = 0; i < arg_count; i++) {
1325 VisitForValue(args->at(i), kStack);
1326 }
1327 // Record source position for debugger.
1328 SetSourcePosition(expr->position());
1329 CallFunctionStub stub(arg_count, NOT_IN_LOOP, RECEIVER_MIGHT_BE_VALUE);
1330 __ CallStub(&stub);
1331 // Restore context register.
1332 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
1333 DropAndApply(1, context_, eax);
1334}
1335
1336
1337void FullCodeGenerator::VisitCall(Call* expr) {
1338 Comment cmnt(masm_, "[ Call");
1339 Expression* fun = expr->expression();
1340 Variable* var = fun->AsVariableProxy()->AsVariable();
1341
1342 if (var != NULL && var->is_possibly_eval()) {
1343 // Call to the identifier 'eval'.
1344 UNREACHABLE();
1345 } else if (var != NULL && !var->is_this() && var->is_global()) {
1346 // Push global object as receiver for the call IC.
1347 __ push(CodeGenerator::GlobalObject());
1348 EmitCallWithIC(expr, var->name(), RelocInfo::CODE_TARGET_CONTEXT);
1349 } else if (var != NULL && var->slot() != NULL &&
1350 var->slot()->type() == Slot::LOOKUP) {
1351 // Call to a lookup slot.
1352 UNREACHABLE();
1353 } else if (fun->AsProperty() != NULL) {
1354 // Call to an object property.
1355 Property* prop = fun->AsProperty();
1356 Literal* key = prop->key()->AsLiteral();
1357 if (key != NULL && key->handle()->IsSymbol()) {
1358 // Call to a named property, use call IC.
1359 VisitForValue(prop->obj(), kStack);
1360 EmitCallWithIC(expr, key->handle(), RelocInfo::CODE_TARGET);
1361 } else {
1362 // Call to a keyed property, use keyed load IC followed by function
1363 // call.
1364 VisitForValue(prop->obj(), kStack);
ager@chromium.org5c838252010-02-19 08:53:10 +00001365 VisitForValue(prop->key(), kAccumulator);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001366 // Record source code position for IC call.
1367 SetSourcePosition(prop->position());
ager@chromium.org5c838252010-02-19 08:53:10 +00001368 if (prop->is_synthetic()) {
1369 __ pop(edx); // We do not need to keep the receiver.
1370 } else {
1371 __ mov(edx, Operand(esp, 0)); // Keep receiver, to call function on.
1372 }
1373
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001374 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
1375 __ call(ic, RelocInfo::CODE_TARGET);
1376 // By emitting a nop we make sure that we do not have a "test eax,..."
1377 // instruction after the call it is treated specially by the LoadIC code.
1378 __ nop();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001379 if (prop->is_synthetic()) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001380 // Push result (function).
1381 __ push(eax);
1382 // Push Global receiver.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001383 __ mov(ecx, CodeGenerator::GlobalObject());
1384 __ push(FieldOperand(ecx, GlobalObject::kGlobalReceiverOffset));
1385 } else {
ager@chromium.org5c838252010-02-19 08:53:10 +00001386 // Pop receiver.
1387 __ pop(ebx);
1388 // Push result (function).
1389 __ push(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001390 __ push(ebx);
1391 }
1392 EmitCallWithStub(expr);
1393 }
1394 } else {
1395 // Call to some other expression. If the expression is an anonymous
1396 // function literal not called in a loop, mark it as one that should
1397 // also use the full code generator.
1398 FunctionLiteral* lit = fun->AsFunctionLiteral();
1399 if (lit != NULL &&
1400 lit->name()->Equals(Heap::empty_string()) &&
1401 loop_depth() == 0) {
1402 lit->set_try_full_codegen(true);
1403 }
1404 VisitForValue(fun, kStack);
1405 // Load global receiver object.
1406 __ mov(ebx, CodeGenerator::GlobalObject());
1407 __ push(FieldOperand(ebx, GlobalObject::kGlobalReceiverOffset));
1408 // Emit function call.
1409 EmitCallWithStub(expr);
1410 }
1411}
1412
1413
1414void FullCodeGenerator::VisitCallNew(CallNew* expr) {
1415 Comment cmnt(masm_, "[ CallNew");
1416 // According to ECMA-262, section 11.2.2, page 44, the function
1417 // expression in new calls must be evaluated before the
1418 // arguments.
1419 // Push function on the stack.
1420 VisitForValue(expr->expression(), kStack);
1421
1422 // Push global object (receiver).
1423 __ push(CodeGenerator::GlobalObject());
1424
1425 // Push the arguments ("left-to-right") on the stack.
1426 ZoneList<Expression*>* args = expr->arguments();
1427 int arg_count = args->length();
1428 for (int i = 0; i < arg_count; i++) {
1429 VisitForValue(args->at(i), kStack);
1430 }
1431
1432 // Call the construct call builtin that handles allocation and
1433 // constructor invocation.
1434 SetSourcePosition(expr->position());
1435
1436 // Load function, arg_count into edi and eax.
1437 __ Set(eax, Immediate(arg_count));
1438 // Function is in esp[arg_count + 1].
1439 __ mov(edi, Operand(esp, eax, times_pointer_size, kPointerSize));
1440
1441 Handle<Code> construct_builtin(Builtins::builtin(Builtins::JSConstructCall));
1442 __ call(construct_builtin, RelocInfo::CONSTRUCT_CALL);
1443
1444 // Replace function on TOS with result in eax, or pop it.
1445 DropAndApply(1, context_, eax);
1446}
1447
1448
1449void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
1450 Comment cmnt(masm_, "[ CallRuntime");
1451 ZoneList<Expression*>* args = expr->arguments();
1452
1453 if (expr->is_jsruntime()) {
1454 // Prepare for calling JS runtime function.
1455 __ mov(eax, CodeGenerator::GlobalObject());
1456 __ push(FieldOperand(eax, GlobalObject::kBuiltinsOffset));
1457 }
1458
1459 // Push the arguments ("left-to-right").
1460 int arg_count = args->length();
1461 for (int i = 0; i < arg_count; i++) {
1462 VisitForValue(args->at(i), kStack);
1463 }
1464
1465 if (expr->is_jsruntime()) {
1466 // Call the JS runtime function via a call IC.
1467 __ Set(ecx, Immediate(expr->name()));
1468 InLoopFlag in_loop = (loop_depth() > 0) ? IN_LOOP : NOT_IN_LOOP;
1469 Handle<Code> ic = CodeGenerator::ComputeCallInitialize(arg_count, in_loop);
1470 __ call(ic, RelocInfo::CODE_TARGET);
1471 // Restore context register.
1472 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
1473 } else {
1474 // Call the C runtime function.
1475 __ CallRuntime(expr->function(), arg_count);
1476 }
1477 Apply(context_, eax);
1478}
1479
1480
1481void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
1482 switch (expr->op()) {
1483 case Token::VOID: {
1484 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
1485 VisitForEffect(expr->expression());
1486 switch (context_) {
1487 case Expression::kUninitialized:
1488 UNREACHABLE();
1489 break;
1490 case Expression::kEffect:
1491 break;
1492 case Expression::kValue:
1493 switch (location_) {
1494 case kAccumulator:
1495 __ mov(result_register(), Factory::undefined_value());
1496 break;
1497 case kStack:
1498 __ push(Immediate(Factory::undefined_value()));
1499 break;
1500 }
1501 break;
1502 case Expression::kTestValue:
1503 // Value is false so it's needed.
1504 switch (location_) {
1505 case kAccumulator:
1506 __ mov(result_register(), Factory::undefined_value());
1507 break;
1508 case kStack:
1509 __ push(Immediate(Factory::undefined_value()));
1510 break;
1511 }
1512 // Fall through.
1513 case Expression::kTest:
1514 case Expression::kValueTest:
1515 __ jmp(false_label_);
1516 break;
1517 }
1518 break;
1519 }
1520
1521 case Token::NOT: {
1522 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
1523 Label materialize_true, materialize_false, done;
1524 // Initially assume a pure test context. Notice that the labels are
1525 // swapped.
1526 Label* if_true = false_label_;
1527 Label* if_false = true_label_;
1528 switch (context_) {
1529 case Expression::kUninitialized:
1530 UNREACHABLE();
1531 break;
1532 case Expression::kEffect:
1533 if_true = &done;
1534 if_false = &done;
1535 break;
1536 case Expression::kValue:
1537 if_true = &materialize_false;
1538 if_false = &materialize_true;
1539 break;
1540 case Expression::kTest:
1541 break;
1542 case Expression::kValueTest:
1543 if_false = &materialize_true;
1544 break;
1545 case Expression::kTestValue:
1546 if_true = &materialize_false;
1547 break;
1548 }
1549 VisitForControl(expr->expression(), if_true, if_false);
1550 Apply(context_, if_false, if_true); // Labels swapped.
1551 break;
1552 }
1553
1554 case Token::TYPEOF: {
1555 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
1556 VariableProxy* proxy = expr->expression()->AsVariableProxy();
1557 if (proxy != NULL &&
1558 !proxy->var()->is_this() &&
1559 proxy->var()->is_global()) {
1560 Comment cmnt(masm_, "Global variable");
ager@chromium.org5c838252010-02-19 08:53:10 +00001561 __ mov(eax, CodeGenerator::GlobalObject());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001562 __ mov(ecx, Immediate(proxy->name()));
1563 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
1564 // Use a regular load, not a contextual load, to avoid a reference
1565 // error.
1566 __ call(ic, RelocInfo::CODE_TARGET);
ager@chromium.org5c838252010-02-19 08:53:10 +00001567 __ push(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001568 } else if (proxy != NULL &&
1569 proxy->var()->slot() != NULL &&
1570 proxy->var()->slot()->type() == Slot::LOOKUP) {
1571 __ push(esi);
1572 __ push(Immediate(proxy->name()));
1573 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
1574 __ push(eax);
1575 } else {
1576 // This expression cannot throw a reference error at the top level.
1577 VisitForValue(expr->expression(), kStack);
1578 }
1579
1580 __ CallRuntime(Runtime::kTypeof, 1);
1581 Apply(context_, eax);
1582 break;
1583 }
1584
1585 case Token::ADD: {
1586 Comment cmt(masm_, "[ UnaryOperation (ADD)");
1587 VisitForValue(expr->expression(), kAccumulator);
1588 Label no_conversion;
1589 __ test(result_register(), Immediate(kSmiTagMask));
1590 __ j(zero, &no_conversion);
1591 __ push(result_register());
1592 __ InvokeBuiltin(Builtins::TO_NUMBER, CALL_FUNCTION);
1593 __ bind(&no_conversion);
1594 Apply(context_, result_register());
1595 break;
1596 }
1597
1598 case Token::SUB: {
1599 Comment cmt(masm_, "[ UnaryOperation (SUB)");
1600 bool overwrite =
1601 (expr->expression()->AsBinaryOperation() != NULL &&
1602 expr->expression()->AsBinaryOperation()->ResultOverwriteAllowed());
1603 GenericUnaryOpStub stub(Token::SUB, overwrite);
1604 // GenericUnaryOpStub expects the argument to be in the
1605 // accumulator register eax.
1606 VisitForValue(expr->expression(), kAccumulator);
1607 __ CallStub(&stub);
1608 Apply(context_, eax);
1609 break;
1610 }
1611
1612 case Token::BIT_NOT: {
1613 Comment cmt(masm_, "[ UnaryOperation (BIT_NOT)");
1614 bool overwrite =
1615 (expr->expression()->AsBinaryOperation() != NULL &&
1616 expr->expression()->AsBinaryOperation()->ResultOverwriteAllowed());
1617 GenericUnaryOpStub stub(Token::BIT_NOT, overwrite);
1618 // GenericUnaryOpStub expects the argument to be in the
1619 // accumulator register eax.
1620 VisitForValue(expr->expression(), kAccumulator);
1621 // Avoid calling the stub for Smis.
1622 Label smi, done;
1623 __ test(result_register(), Immediate(kSmiTagMask));
1624 __ j(zero, &smi);
1625 // Non-smi: call stub leaving result in accumulator register.
1626 __ CallStub(&stub);
1627 __ jmp(&done);
1628 // Perform operation directly on Smis.
1629 __ bind(&smi);
1630 __ not_(result_register());
1631 __ and_(result_register(), ~kSmiTagMask); // Remove inverted smi-tag.
1632 __ bind(&done);
1633 Apply(context_, result_register());
1634 break;
1635 }
1636
1637 default:
1638 UNREACHABLE();
1639 }
1640}
1641
1642
1643void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
1644 Comment cmnt(masm_, "[ CountOperation");
1645
1646 // Expression can only be a property, a global or a (parameter or local)
1647 // slot. Variables with rewrite to .arguments are treated as KEYED_PROPERTY.
1648 enum LhsKind { VARIABLE, NAMED_PROPERTY, KEYED_PROPERTY };
1649 LhsKind assign_type = VARIABLE;
1650 Property* prop = expr->expression()->AsProperty();
1651 // In case of a property we use the uninitialized expression context
1652 // of the key to detect a named property.
1653 if (prop != NULL) {
1654 assign_type =
1655 (prop->key()->IsPropertyName()) ? NAMED_PROPERTY : KEYED_PROPERTY;
1656 }
1657
1658 // Evaluate expression and get value.
1659 if (assign_type == VARIABLE) {
1660 ASSERT(expr->expression()->AsVariableProxy()->var() != NULL);
1661 Location saved_location = location_;
1662 location_ = kAccumulator;
1663 EmitVariableLoad(expr->expression()->AsVariableProxy()->var(),
1664 Expression::kValue);
1665 location_ = saved_location;
1666 } else {
1667 // Reserve space for result of postfix operation.
1668 if (expr->is_postfix() && context_ != Expression::kEffect) {
1669 __ push(Immediate(Smi::FromInt(0)));
1670 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001671 if (assign_type == NAMED_PROPERTY) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001672 // Put the object both on the stack and in the accumulator.
1673 VisitForValue(prop->obj(), kAccumulator);
1674 __ push(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001675 EmitNamedPropertyLoad(prop);
1676 } else {
ager@chromium.org5c838252010-02-19 08:53:10 +00001677 VisitForValue(prop->obj(), kStack);
1678 VisitForValue(prop->key(), kAccumulator);
1679 __ mov(edx, Operand(esp, 0));
1680 __ push(eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001681 EmitKeyedPropertyLoad(prop);
1682 }
1683 }
1684
1685 // Call ToNumber only if operand is not a smi.
1686 Label no_conversion;
1687 __ test(eax, Immediate(kSmiTagMask));
1688 __ j(zero, &no_conversion);
1689 __ push(eax);
1690 __ InvokeBuiltin(Builtins::TO_NUMBER, CALL_FUNCTION);
1691 __ bind(&no_conversion);
1692
1693 // Save result for postfix expressions.
1694 if (expr->is_postfix()) {
1695 switch (context_) {
1696 case Expression::kUninitialized:
1697 UNREACHABLE();
1698 case Expression::kEffect:
1699 // Do not save result.
1700 break;
1701 case Expression::kValue:
1702 case Expression::kTest:
1703 case Expression::kValueTest:
1704 case Expression::kTestValue:
1705 // Save the result on the stack. If we have a named or keyed property
1706 // we store the result under the receiver that is currently on top
1707 // of the stack.
1708 switch (assign_type) {
1709 case VARIABLE:
1710 __ push(eax);
1711 break;
1712 case NAMED_PROPERTY:
1713 __ mov(Operand(esp, kPointerSize), eax);
1714 break;
1715 case KEYED_PROPERTY:
1716 __ mov(Operand(esp, 2 * kPointerSize), eax);
1717 break;
1718 }
1719 break;
1720 }
1721 }
1722
1723 // Inline smi case if we are in a loop.
1724 Label stub_call, done;
1725 if (loop_depth() > 0) {
1726 if (expr->op() == Token::INC) {
1727 __ add(Operand(eax), Immediate(Smi::FromInt(1)));
1728 } else {
1729 __ sub(Operand(eax), Immediate(Smi::FromInt(1)));
1730 }
1731 __ j(overflow, &stub_call);
1732 // We could eliminate this smi check if we split the code at
1733 // the first smi check before calling ToNumber.
1734 __ test(eax, Immediate(kSmiTagMask));
1735 __ j(zero, &done);
1736 __ bind(&stub_call);
1737 // Call stub. Undo operation first.
1738 if (expr->op() == Token::INC) {
1739 __ sub(Operand(eax), Immediate(Smi::FromInt(1)));
1740 } else {
1741 __ add(Operand(eax), Immediate(Smi::FromInt(1)));
1742 }
1743 }
1744 // Call stub for +1/-1.
1745 GenericBinaryOpStub stub(expr->binary_op(),
1746 NO_OVERWRITE,
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001747 NO_GENERIC_BINARY_FLAGS,
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00001748 TypeInfo::Unknown());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001749 stub.GenerateCall(masm(), eax, Smi::FromInt(1));
1750 __ bind(&done);
1751
1752 // Store the value returned in eax.
1753 switch (assign_type) {
1754 case VARIABLE:
1755 if (expr->is_postfix()) {
1756 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
1757 Expression::kEffect);
1758 // For all contexts except kEffect: We have the result on
1759 // top of the stack.
1760 if (context_ != Expression::kEffect) {
1761 ApplyTOS(context_);
1762 }
1763 } else {
1764 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
1765 context_);
1766 }
1767 break;
1768 case NAMED_PROPERTY: {
1769 __ mov(ecx, prop->key()->AsLiteral()->handle());
1770 __ pop(edx);
1771 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
1772 __ call(ic, RelocInfo::CODE_TARGET);
1773 // This nop signals to the IC that there is no inlined code at the call
1774 // site for it to patch.
1775 __ nop();
1776 if (expr->is_postfix()) {
1777 if (context_ != Expression::kEffect) {
1778 ApplyTOS(context_);
1779 }
1780 } else {
1781 Apply(context_, eax);
1782 }
1783 break;
1784 }
1785 case KEYED_PROPERTY: {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001786 __ pop(ecx);
1787 __ pop(edx);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001788 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
1789 __ call(ic, RelocInfo::CODE_TARGET);
1790 // This nop signals to the IC that there is no inlined code at the call
1791 // site for it to patch.
1792 __ nop();
1793 if (expr->is_postfix()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001794 // Result is on the stack
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001795 if (context_ != Expression::kEffect) {
1796 ApplyTOS(context_);
1797 }
1798 } else {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001799 Apply(context_, eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001800 }
1801 break;
1802 }
1803 }
1804}
1805
1806
1807void FullCodeGenerator::VisitBinaryOperation(BinaryOperation* expr) {
1808 Comment cmnt(masm_, "[ BinaryOperation");
1809 switch (expr->op()) {
1810 case Token::COMMA:
1811 VisitForEffect(expr->left());
1812 Visit(expr->right());
1813 break;
1814
1815 case Token::OR:
1816 case Token::AND:
1817 EmitLogicalOperation(expr);
1818 break;
1819
1820 case Token::ADD:
1821 case Token::SUB:
1822 case Token::DIV:
1823 case Token::MOD:
1824 case Token::MUL:
1825 case Token::BIT_OR:
1826 case Token::BIT_AND:
1827 case Token::BIT_XOR:
1828 case Token::SHL:
1829 case Token::SHR:
1830 case Token::SAR:
1831 VisitForValue(expr->left(), kStack);
1832 VisitForValue(expr->right(), kAccumulator);
1833 EmitBinaryOp(expr->op(), context_);
1834 break;
1835
1836 default:
1837 UNREACHABLE();
1838 }
1839}
1840
1841
1842void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
1843 Comment cmnt(masm_, "[ CompareOperation");
1844
1845 // Always perform the comparison for its control flow. Pack the result
1846 // into the expression's context after the comparison is performed.
1847 Label materialize_true, materialize_false, done;
1848 // Initially assume we are in a test context.
1849 Label* if_true = true_label_;
1850 Label* if_false = false_label_;
1851 switch (context_) {
1852 case Expression::kUninitialized:
1853 UNREACHABLE();
1854 break;
1855 case Expression::kEffect:
1856 if_true = &done;
1857 if_false = &done;
1858 break;
1859 case Expression::kValue:
1860 if_true = &materialize_true;
1861 if_false = &materialize_false;
1862 break;
1863 case Expression::kTest:
1864 break;
1865 case Expression::kValueTest:
1866 if_true = &materialize_true;
1867 break;
1868 case Expression::kTestValue:
1869 if_false = &materialize_false;
1870 break;
1871 }
1872
1873 VisitForValue(expr->left(), kStack);
1874 switch (expr->op()) {
1875 case Token::IN:
1876 VisitForValue(expr->right(), kStack);
1877 __ InvokeBuiltin(Builtins::IN, CALL_FUNCTION);
1878 __ cmp(eax, Factory::true_value());
1879 __ j(equal, if_true);
1880 __ jmp(if_false);
1881 break;
1882
1883 case Token::INSTANCEOF: {
1884 VisitForValue(expr->right(), kStack);
1885 InstanceofStub stub;
1886 __ CallStub(&stub);
1887 __ test(eax, Operand(eax));
1888 __ j(zero, if_true); // The stub returns 0 for true.
1889 __ jmp(if_false);
1890 break;
1891 }
1892
1893 default: {
1894 VisitForValue(expr->right(), kAccumulator);
1895 Condition cc = no_condition;
1896 bool strict = false;
1897 switch (expr->op()) {
1898 case Token::EQ_STRICT:
1899 strict = true;
1900 // Fall through
1901 case Token::EQ:
1902 cc = equal;
1903 __ pop(edx);
1904 break;
1905 case Token::LT:
1906 cc = less;
1907 __ pop(edx);
1908 break;
1909 case Token::GT:
1910 // Reverse left and right sizes to obtain ECMA-262 conversion order.
1911 cc = less;
1912 __ mov(edx, result_register());
1913 __ pop(eax);
1914 break;
1915 case Token::LTE:
1916 // Reverse left and right sizes to obtain ECMA-262 conversion order.
1917 cc = greater_equal;
1918 __ mov(edx, result_register());
1919 __ pop(eax);
1920 break;
1921 case Token::GTE:
1922 cc = greater_equal;
1923 __ pop(edx);
1924 break;
1925 case Token::IN:
1926 case Token::INSTANCEOF:
1927 default:
1928 UNREACHABLE();
1929 }
1930
1931 // The comparison stub expects the smi vs. smi case to be handled
1932 // before it is called.
1933 Label slow_case;
1934 __ mov(ecx, Operand(edx));
1935 __ or_(ecx, Operand(eax));
1936 __ test(ecx, Immediate(kSmiTagMask));
1937 __ j(not_zero, &slow_case, not_taken);
1938 __ cmp(edx, Operand(eax));
1939 __ j(cc, if_true);
1940 __ jmp(if_false);
1941
1942 __ bind(&slow_case);
1943 CompareStub stub(cc, strict);
1944 __ CallStub(&stub);
1945 __ test(eax, Operand(eax));
1946 __ j(cc, if_true);
1947 __ jmp(if_false);
1948 }
1949 }
1950
1951 // Convert the result of the comparison into one expected for this
1952 // expression's context.
1953 Apply(context_, if_true, if_false);
1954}
1955
1956
1957void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
1958 __ mov(eax, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
1959 Apply(context_, eax);
1960}
1961
1962
1963Register FullCodeGenerator::result_register() { return eax; }
1964
1965
1966Register FullCodeGenerator::context_register() { return esi; }
1967
1968
1969void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
1970 ASSERT_EQ(POINTER_SIZE_ALIGN(frame_offset), frame_offset);
1971 __ mov(Operand(ebp, frame_offset), value);
1972}
1973
1974
1975void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
1976 __ mov(dst, CodeGenerator::ContextOperand(esi, context_index));
1977}
1978
1979
1980// ----------------------------------------------------------------------------
1981// Non-local control flow support.
1982
1983void FullCodeGenerator::EnterFinallyBlock() {
1984 // Cook return address on top of stack (smi encoded Code* delta)
1985 ASSERT(!result_register().is(edx));
1986 __ mov(edx, Operand(esp, 0));
1987 __ sub(Operand(edx), Immediate(masm_->CodeObject()));
1988 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
1989 ASSERT_EQ(0, kSmiTag);
1990 __ add(edx, Operand(edx)); // Convert to smi.
1991 __ mov(Operand(esp, 0), edx);
1992 // Store result register while executing finally block.
1993 __ push(result_register());
1994}
1995
1996
1997void FullCodeGenerator::ExitFinallyBlock() {
1998 ASSERT(!result_register().is(edx));
1999 // Restore result register from stack.
2000 __ pop(result_register());
2001 // Uncook return address.
2002 __ mov(edx, Operand(esp, 0));
2003 __ sar(edx, 1); // Convert smi to int.
2004 __ add(Operand(edx), Immediate(masm_->CodeObject()));
2005 __ mov(Operand(esp, 0), edx);
2006 // And return.
2007 __ ret(0);
2008}
2009
2010
2011#undef __
2012
2013} } // namespace v8::internal