Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(177)

Side by Side Diff: src/ia32/fast-codegen-ia32.cc

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

Powered by Google App Engine
This is Rietveld 408576698