blob: 5ffebfb53b758ad08325b0b46a6e8cb3b84cf55e [file] [log] [blame]
Leon Clarked91b9f72010-01-27 17:25:45 +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 "full-codegen.h"
Kristian Monsen80d68ea2010-09-08 11:05:35 +010033#include "macro-assembler.h"
Steve Block6ded16b2010-05-10 14:33:55 +010034#include "scopes.h"
Leon Clarked91b9f72010-01-27 17:25:45 +000035#include "stub-cache.h"
36#include "debug.h"
Andrei Popescu402d9372010-02-26 13:31:12 +000037#include "liveedit.h"
Leon Clarked91b9f72010-01-27 17:25:45 +000038
39namespace v8 {
40namespace internal {
41
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010042void BreakableStatementChecker::Check(Statement* stmt) {
43 Visit(stmt);
44}
45
46
47void BreakableStatementChecker::Check(Expression* expr) {
48 Visit(expr);
49}
50
51
52void BreakableStatementChecker::VisitDeclaration(Declaration* decl) {
53}
54
55
56void BreakableStatementChecker::VisitBlock(Block* stmt) {
57}
58
59
60void BreakableStatementChecker::VisitExpressionStatement(
61 ExpressionStatement* stmt) {
62 // Check if expression is breakable.
63 Visit(stmt->expression());
64}
65
66
67void BreakableStatementChecker::VisitEmptyStatement(EmptyStatement* stmt) {
68}
69
70
71void BreakableStatementChecker::VisitIfStatement(IfStatement* stmt) {
72 // If the condition is breakable the if statement is breakable.
73 Visit(stmt->condition());
74}
75
76
77void BreakableStatementChecker::VisitContinueStatement(
78 ContinueStatement* stmt) {
79}
80
81
82void BreakableStatementChecker::VisitBreakStatement(BreakStatement* stmt) {
83}
84
85
86void BreakableStatementChecker::VisitReturnStatement(ReturnStatement* stmt) {
87 // Return is breakable if the expression is.
88 Visit(stmt->expression());
89}
90
91
92void BreakableStatementChecker::VisitWithEnterStatement(
93 WithEnterStatement* stmt) {
94 Visit(stmt->expression());
95}
96
97
98void BreakableStatementChecker::VisitWithExitStatement(
99 WithExitStatement* stmt) {
100}
101
102
103void BreakableStatementChecker::VisitSwitchStatement(SwitchStatement* stmt) {
104 // Switch statements breakable if the tag expression is.
105 Visit(stmt->tag());
106}
107
108
109void BreakableStatementChecker::VisitDoWhileStatement(DoWhileStatement* stmt) {
110 // Mark do while as breakable to avoid adding a break slot in front of it.
111 is_breakable_ = true;
112}
113
114
115void BreakableStatementChecker::VisitWhileStatement(WhileStatement* stmt) {
116 // Mark while statements breakable if the condition expression is.
117 Visit(stmt->cond());
118}
119
120
121void BreakableStatementChecker::VisitForStatement(ForStatement* stmt) {
122 // Mark for statements breakable if the condition expression is.
123 if (stmt->cond() != NULL) {
124 Visit(stmt->cond());
125 }
126}
127
128
129void BreakableStatementChecker::VisitForInStatement(ForInStatement* stmt) {
130 // Mark for in statements breakable if the enumerable expression is.
131 Visit(stmt->enumerable());
132}
133
134
135void BreakableStatementChecker::VisitTryCatchStatement(
136 TryCatchStatement* stmt) {
137 // Mark try catch as breakable to avoid adding a break slot in front of it.
138 is_breakable_ = true;
139}
140
141
142void BreakableStatementChecker::VisitTryFinallyStatement(
143 TryFinallyStatement* stmt) {
144 // Mark try finally as breakable to avoid adding a break slot in front of it.
145 is_breakable_ = true;
146}
147
148
149void BreakableStatementChecker::VisitDebuggerStatement(
150 DebuggerStatement* stmt) {
151 // The debugger statement is breakable.
152 is_breakable_ = true;
153}
154
155
156void BreakableStatementChecker::VisitFunctionLiteral(FunctionLiteral* expr) {
157}
158
159
160void BreakableStatementChecker::VisitSharedFunctionInfoLiteral(
161 SharedFunctionInfoLiteral* expr) {
162}
163
164
165void BreakableStatementChecker::VisitConditional(Conditional* expr) {
166}
167
168
169void BreakableStatementChecker::VisitSlot(Slot* expr) {
170}
171
172
173void BreakableStatementChecker::VisitVariableProxy(VariableProxy* expr) {
174}
175
176
177void BreakableStatementChecker::VisitLiteral(Literal* expr) {
178}
179
180
181void BreakableStatementChecker::VisitRegExpLiteral(RegExpLiteral* expr) {
182}
183
184
185void BreakableStatementChecker::VisitObjectLiteral(ObjectLiteral* expr) {
186}
187
188
189void BreakableStatementChecker::VisitArrayLiteral(ArrayLiteral* expr) {
190}
191
192
193void BreakableStatementChecker::VisitCatchExtensionObject(
194 CatchExtensionObject* expr) {
195}
196
197
198void BreakableStatementChecker::VisitAssignment(Assignment* expr) {
199 // If assigning to a property (including a global property) the assignment is
200 // breakable.
201 Variable* var = expr->target()->AsVariableProxy()->AsVariable();
202 Property* prop = expr->target()->AsProperty();
203 if (prop != NULL || (var != NULL && var->is_global())) {
204 is_breakable_ = true;
205 return;
206 }
207
208 // Otherwise the assignment is breakable if the assigned value is.
209 Visit(expr->value());
210}
211
212
213void BreakableStatementChecker::VisitThrow(Throw* expr) {
214 // Throw is breakable if the expression is.
215 Visit(expr->exception());
216}
217
218
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100219void BreakableStatementChecker::VisitIncrementOperation(
220 IncrementOperation* expr) {
221 UNREACHABLE();
222}
223
224
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100225void BreakableStatementChecker::VisitProperty(Property* expr) {
226 // Property load is breakable.
227 is_breakable_ = true;
228}
229
230
231void BreakableStatementChecker::VisitCall(Call* expr) {
232 // Function calls both through IC and call stub are breakable.
233 is_breakable_ = true;
234}
235
236
237void BreakableStatementChecker::VisitCallNew(CallNew* expr) {
238 // Function calls through new are breakable.
239 is_breakable_ = true;
240}
241
242
243void BreakableStatementChecker::VisitCallRuntime(CallRuntime* expr) {
244}
245
246
247void BreakableStatementChecker::VisitUnaryOperation(UnaryOperation* expr) {
248 Visit(expr->expression());
249}
250
251
252void BreakableStatementChecker::VisitCountOperation(CountOperation* expr) {
253 Visit(expr->expression());
254}
255
256
257void BreakableStatementChecker::VisitBinaryOperation(BinaryOperation* expr) {
258 Visit(expr->left());
259 Visit(expr->right());
260}
261
262
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100263void BreakableStatementChecker::VisitCompareToNull(CompareToNull* expr) {
264 Visit(expr->expression());
265}
266
267
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100268void BreakableStatementChecker::VisitCompareOperation(CompareOperation* expr) {
269 Visit(expr->left());
270 Visit(expr->right());
271}
272
273
274void BreakableStatementChecker::VisitThisFunction(ThisFunction* expr) {
275}
276
277
Leon Clarked91b9f72010-01-27 17:25:45 +0000278#define __ ACCESS_MASM(masm())
279
Andrei Popescu31002712010-02-23 13:46:05 +0000280Handle<Code> FullCodeGenerator::MakeCode(CompilationInfo* info) {
281 Handle<Script> script = info->script();
Leon Clarked91b9f72010-01-27 17:25:45 +0000282 if (!script->IsUndefined() && !script->source()->IsUndefined()) {
283 int len = String::cast(script->source())->length();
284 Counters::total_full_codegen_source_size.Increment(len);
285 }
Andrei Popescu31002712010-02-23 13:46:05 +0000286 CodeGenerator::MakeCodePrologue(info);
Leon Clarked91b9f72010-01-27 17:25:45 +0000287 const int kInitialBufferSize = 4 * KB;
288 MacroAssembler masm(NULL, kInitialBufferSize);
Andrei Popescu402d9372010-02-26 13:31:12 +0000289
Andrei Popescu31002712010-02-23 13:46:05 +0000290 FullCodeGenerator cgen(&masm);
Iain Merrick75681382010-08-19 15:07:18 +0100291 cgen.Generate(info);
Leon Clarked91b9f72010-01-27 17:25:45 +0000292 if (cgen.HasStackOverflow()) {
293 ASSERT(!Top::has_pending_exception());
294 return Handle<Code>::null();
295 }
296 Code::Flags flags = Code::ComputeFlags(Code::FUNCTION, NOT_IN_LOOP);
Steve Block6ded16b2010-05-10 14:33:55 +0100297 return CodeGenerator::MakeCodeEpilogue(&masm, flags, info);
Leon Clarked91b9f72010-01-27 17:25:45 +0000298}
299
300
301int FullCodeGenerator::SlotOffset(Slot* slot) {
302 ASSERT(slot != NULL);
303 // Offset is negative because higher indexes are at lower addresses.
304 int offset = -slot->index() * kPointerSize;
305 // Adjust by a (parameter or local) base offset.
306 switch (slot->type()) {
307 case Slot::PARAMETER:
Andrei Popescu31002712010-02-23 13:46:05 +0000308 offset += (scope()->num_parameters() + 1) * kPointerSize;
Leon Clarked91b9f72010-01-27 17:25:45 +0000309 break;
310 case Slot::LOCAL:
311 offset += JavaScriptFrameConstants::kLocal0Offset;
312 break;
313 case Slot::CONTEXT:
314 case Slot::LOOKUP:
315 UNREACHABLE();
316 }
317 return offset;
318}
319
320
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100321bool FullCodeGenerator::ShouldInlineSmiCase(Token::Value op) {
322 // TODO(kasperl): Once the compare stub allows leaving out the
323 // inlined smi case, we should get rid of this check.
324 if (Token::IsCompareOp(op)) return true;
325 // TODO(kasperl): Once the unary bit not stub allows leaving out
326 // the inlined smi case, we should get rid of this check.
327 if (op == Token::BIT_NOT) return true;
328 // Inline smi case inside loops, but not division and modulo which
329 // are too complicated and take up too much space.
330 return (op != Token::DIV) && (op != Token::MOD) && (loop_depth_ > 0);
331}
332
333
334void FullCodeGenerator::PrepareTest(Label* materialize_true,
335 Label* materialize_false,
336 Label** if_true,
337 Label** if_false,
338 Label** fall_through) {
339 switch (context_) {
340 case Expression::kUninitialized:
341 UNREACHABLE();
342 break;
343 case Expression::kEffect:
344 // In an effect context, the true and the false case branch to the
345 // same label.
346 *if_true = *if_false = *fall_through = materialize_true;
347 break;
348 case Expression::kValue:
349 *if_true = *fall_through = materialize_true;
350 *if_false = materialize_false;
351 break;
352 case Expression::kTest:
353 *if_true = true_label_;
354 *if_false = false_label_;
355 *fall_through = fall_through_;
356 break;
357 }
358}
359
360
Leon Clarked91b9f72010-01-27 17:25:45 +0000361void FullCodeGenerator::VisitDeclarations(
362 ZoneList<Declaration*>* declarations) {
363 int length = declarations->length();
364 int globals = 0;
365 for (int i = 0; i < length; i++) {
366 Declaration* decl = declarations->at(i);
367 Variable* var = decl->proxy()->var();
368 Slot* slot = var->slot();
369
370 // If it was not possible to allocate the variable at compile
371 // time, we need to "declare" it at runtime to make sure it
372 // actually exists in the local context.
373 if ((slot != NULL && slot->type() == Slot::LOOKUP) || !var->is_global()) {
374 VisitDeclaration(decl);
375 } else {
376 // Count global variables and functions for later processing
377 globals++;
378 }
379 }
380
381 // Compute array of global variable and function declarations.
382 // Do nothing in case of no declared global functions or variables.
383 if (globals > 0) {
384 Handle<FixedArray> array = Factory::NewFixedArray(2 * globals, TENURED);
385 for (int j = 0, i = 0; i < length; i++) {
386 Declaration* decl = declarations->at(i);
387 Variable* var = decl->proxy()->var();
388 Slot* slot = var->slot();
389
390 if ((slot == NULL || slot->type() != Slot::LOOKUP) && var->is_global()) {
391 array->set(j++, *(var->name()));
392 if (decl->fun() == NULL) {
393 if (var->mode() == Variable::CONST) {
394 // In case this is const property use the hole.
395 array->set_the_hole(j++);
396 } else {
397 array->set_undefined(j++);
398 }
399 } else {
Steve Block6ded16b2010-05-10 14:33:55 +0100400 Handle<SharedFunctionInfo> function =
401 Compiler::BuildFunctionInfo(decl->fun(), script(), this);
Leon Clarked91b9f72010-01-27 17:25:45 +0000402 // Check for stack-overflow exception.
403 if (HasStackOverflow()) return;
404 array->set(j++, *function);
405 }
406 }
407 }
408 // Invoke the platform-dependent code generator to do the actual
409 // declaration the global variables and functions.
410 DeclareGlobals(array);
411 }
412}
413
414
415void FullCodeGenerator::SetFunctionPosition(FunctionLiteral* fun) {
416 if (FLAG_debug_info) {
417 CodeGenerator::RecordPositions(masm_, fun->start_position());
418 }
419}
420
421
422void FullCodeGenerator::SetReturnPosition(FunctionLiteral* fun) {
423 if (FLAG_debug_info) {
Ben Murdochbb769b22010-08-11 14:56:33 +0100424 CodeGenerator::RecordPositions(masm_, fun->end_position() - 1);
Leon Clarked91b9f72010-01-27 17:25:45 +0000425 }
426}
427
428
429void FullCodeGenerator::SetStatementPosition(Statement* stmt) {
430 if (FLAG_debug_info) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100431#ifdef ENABLE_DEBUGGER_SUPPORT
432 if (!Debugger::IsDebuggerActive()) {
433 CodeGenerator::RecordPositions(masm_, stmt->statement_pos());
434 } else {
435 // Check if the statement will be breakable without adding a debug break
436 // slot.
437 BreakableStatementChecker checker;
438 checker.Check(stmt);
439 // Record the statement position right here if the statement is not
440 // breakable. For breakable statements the actual recording of the
441 // position will be postponed to the breakable code (typically an IC).
442 bool position_recorded = CodeGenerator::RecordPositions(
443 masm_, stmt->statement_pos(), !checker.is_breakable());
444 // If the position recording did record a new position generate a debug
445 // break slot to make the statement breakable.
446 if (position_recorded) {
447 Debug::GenerateSlot(masm_);
448 }
449 }
450#else
Leon Clarked91b9f72010-01-27 17:25:45 +0000451 CodeGenerator::RecordPositions(masm_, stmt->statement_pos());
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100452#endif
453 }
454}
455
456
457void FullCodeGenerator::SetExpressionPosition(Expression* expr, int pos) {
458 if (FLAG_debug_info) {
459#ifdef ENABLE_DEBUGGER_SUPPORT
460 if (!Debugger::IsDebuggerActive()) {
461 CodeGenerator::RecordPositions(masm_, pos);
462 } else {
463 // Check if the expression will be breakable without adding a debug break
464 // slot.
465 BreakableStatementChecker checker;
466 checker.Check(expr);
467 // Record a statement position right here if the expression is not
468 // breakable. For breakable expressions the actual recording of the
469 // position will be postponed to the breakable code (typically an IC).
470 // NOTE this will record a statement position for something which might
471 // not be a statement. As stepping in the debugger will only stop at
472 // statement positions this is used for e.g. the condition expression of
473 // a do while loop.
474 bool position_recorded = CodeGenerator::RecordPositions(
475 masm_, pos, !checker.is_breakable());
476 // If the position recording did record a new position generate a debug
477 // break slot to make the statement breakable.
478 if (position_recorded) {
479 Debug::GenerateSlot(masm_);
480 }
481 }
482#else
483 CodeGenerator::RecordPositions(masm_, pos);
484#endif
Leon Clarked91b9f72010-01-27 17:25:45 +0000485 }
486}
487
488
489void FullCodeGenerator::SetStatementPosition(int pos) {
490 if (FLAG_debug_info) {
491 CodeGenerator::RecordPositions(masm_, pos);
492 }
493}
494
495
496void FullCodeGenerator::SetSourcePosition(int pos) {
497 if (FLAG_debug_info && pos != RelocInfo::kNoPosition) {
498 masm_->RecordPosition(pos);
499 }
500}
501
502
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100503void FullCodeGenerator::EmitInlineRuntimeCall(CallRuntime* expr) {
504 Handle<String> name = expr->name();
Steve Block791712a2010-08-27 10:21:07 +0100505 SmartPointer<char> cstring = name->ToCString();
506
507#define CHECK_EMIT_INLINE_CALL(name, x, y) \
508 if (strcmp("_"#name, *cstring) == 0) { \
509 Emit##name(expr->arguments()); \
510 return; \
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100511 }
Steve Block791712a2010-08-27 10:21:07 +0100512 INLINE_RUNTIME_FUNCTION_LIST(CHECK_EMIT_INLINE_CALL)
Steve Block791712a2010-08-27 10:21:07 +0100513#undef CHECK_EMIT_INLINE_CALL
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100514 UNREACHABLE();
515}
516
517
518void FullCodeGenerator::VisitBinaryOperation(BinaryOperation* expr) {
519 Comment cmnt(masm_, "[ BinaryOperation");
520 Token::Value op = expr->op();
521 Expression* left = expr->left();
522 Expression* right = expr->right();
523
524 OverwriteMode mode = NO_OVERWRITE;
525 if (left->ResultOverwriteAllowed()) {
526 mode = OVERWRITE_LEFT;
527 } else if (right->ResultOverwriteAllowed()) {
528 mode = OVERWRITE_RIGHT;
529 }
530
531 switch (op) {
532 case Token::COMMA:
533 VisitForEffect(left);
534 Visit(right);
535 break;
536
537 case Token::OR:
538 case Token::AND:
539 EmitLogicalOperation(expr);
540 break;
541
542 case Token::ADD:
543 case Token::SUB:
544 case Token::DIV:
545 case Token::MOD:
546 case Token::MUL:
547 case Token::BIT_OR:
548 case Token::BIT_AND:
549 case Token::BIT_XOR:
550 case Token::SHL:
551 case Token::SHR:
552 case Token::SAR: {
553 // Figure out if either of the operands is a constant.
554 ConstantOperand constant = ShouldInlineSmiCase(op)
555 ? GetConstantOperand(op, left, right)
556 : kNoConstants;
557
558 // Load only the operands that we need to materialize.
559 if (constant == kNoConstants) {
560 VisitForValue(left, kStack);
561 VisitForValue(right, kAccumulator);
562 } else if (constant == kRightConstant) {
563 VisitForValue(left, kAccumulator);
564 } else {
565 ASSERT(constant == kLeftConstant);
566 VisitForValue(right, kAccumulator);
567 }
568
569 SetSourcePosition(expr->position());
570 if (ShouldInlineSmiCase(op)) {
571 EmitInlineSmiBinaryOp(expr, op, context_, mode, left, right, constant);
572 } else {
573 EmitBinaryOp(op, context_, mode);
574 }
575 break;
576 }
577
578 default:
579 UNREACHABLE();
580 }
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100581}
582
583
Leon Clarked91b9f72010-01-27 17:25:45 +0000584void FullCodeGenerator::EmitLogicalOperation(BinaryOperation* expr) {
585 Label eval_right, done;
586
587 // Set up the appropriate context for the left subexpression based
588 // on the operation and our own context. Initially assume we can
589 // inherit both true and false labels from our context.
590 if (expr->op() == Token::OR) {
591 switch (context_) {
592 case Expression::kUninitialized:
593 UNREACHABLE();
594 case Expression::kEffect:
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100595 VisitForControl(expr->left(), &done, &eval_right, &eval_right);
Leon Clarked91b9f72010-01-27 17:25:45 +0000596 break;
597 case Expression::kValue:
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100598 VisitLogicalForValue(expr->left(), expr->op(), location_, &done);
Leon Clarked91b9f72010-01-27 17:25:45 +0000599 break;
600 case Expression::kTest:
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100601 VisitForControl(expr->left(), true_label_, &eval_right, &eval_right);
Leon Clarked91b9f72010-01-27 17:25:45 +0000602 break;
603 }
604 } else {
605 ASSERT_EQ(Token::AND, expr->op());
606 switch (context_) {
607 case Expression::kUninitialized:
608 UNREACHABLE();
609 case Expression::kEffect:
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100610 VisitForControl(expr->left(), &eval_right, &done, &eval_right);
Leon Clarked91b9f72010-01-27 17:25:45 +0000611 break;
612 case Expression::kValue:
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100613 VisitLogicalForValue(expr->left(), expr->op(), location_, &done);
Leon Clarked91b9f72010-01-27 17:25:45 +0000614 break;
615 case Expression::kTest:
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100616 VisitForControl(expr->left(), &eval_right, false_label_, &eval_right);
Leon Clarked91b9f72010-01-27 17:25:45 +0000617 break;
618 }
619 }
620
621 __ bind(&eval_right);
622 Visit(expr->right());
623
624 __ bind(&done);
625}
626
627
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100628void FullCodeGenerator::VisitLogicalForValue(Expression* expr,
629 Token::Value op,
630 Location where,
631 Label* done) {
632 ASSERT(op == Token::AND || op == Token::OR);
633 VisitForValue(expr, kAccumulator);
634 __ push(result_register());
635
636 Label discard;
637 switch (where) {
638 case kAccumulator: {
639 Label restore;
640 if (op == Token::OR) {
641 DoTest(&restore, &discard, &restore);
642 } else {
643 DoTest(&discard, &restore, &restore);
644 }
645 __ bind(&restore);
646 __ pop(result_register());
647 __ jmp(done);
648 break;
649 }
650 case kStack: {
651 if (op == Token::OR) {
652 DoTest(done, &discard, &discard);
653 } else {
654 DoTest(&discard, done, &discard);
655 }
656 break;
657 }
658 }
659
660 __ bind(&discard);
661 __ Drop(1);
662}
663
664
Leon Clarked91b9f72010-01-27 17:25:45 +0000665void FullCodeGenerator::VisitBlock(Block* stmt) {
666 Comment cmnt(masm_, "[ Block");
667 Breakable nested_statement(this, stmt);
668 SetStatementPosition(stmt);
669 VisitStatements(stmt->statements());
670 __ bind(nested_statement.break_target());
671}
672
673
674void FullCodeGenerator::VisitExpressionStatement(ExpressionStatement* stmt) {
675 Comment cmnt(masm_, "[ ExpressionStatement");
676 SetStatementPosition(stmt);
677 VisitForEffect(stmt->expression());
678}
679
680
681void FullCodeGenerator::VisitEmptyStatement(EmptyStatement* stmt) {
682 Comment cmnt(masm_, "[ EmptyStatement");
683 SetStatementPosition(stmt);
684}
685
686
687void FullCodeGenerator::VisitIfStatement(IfStatement* stmt) {
688 Comment cmnt(masm_, "[ IfStatement");
689 SetStatementPosition(stmt);
690 Label then_part, else_part, done;
691
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100692 if (stmt->HasElseStatement()) {
693 VisitForControl(stmt->condition(), &then_part, &else_part, &then_part);
694 __ bind(&then_part);
695 Visit(stmt->then_statement());
696 __ jmp(&done);
Leon Clarked91b9f72010-01-27 17:25:45 +0000697
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100698 __ bind(&else_part);
699 Visit(stmt->else_statement());
700 } else {
701 VisitForControl(stmt->condition(), &then_part, &done, &then_part);
702 __ bind(&then_part);
703 Visit(stmt->then_statement());
704 }
Leon Clarked91b9f72010-01-27 17:25:45 +0000705 __ bind(&done);
706}
707
708
709void FullCodeGenerator::VisitContinueStatement(ContinueStatement* stmt) {
710 Comment cmnt(masm_, "[ ContinueStatement");
711 SetStatementPosition(stmt);
712 NestedStatement* current = nesting_stack_;
713 int stack_depth = 0;
714 while (!current->IsContinueTarget(stmt->target())) {
715 stack_depth = current->Exit(stack_depth);
716 current = current->outer();
717 }
718 __ Drop(stack_depth);
719
720 Iteration* loop = current->AsIteration();
721 __ jmp(loop->continue_target());
722}
723
724
725void FullCodeGenerator::VisitBreakStatement(BreakStatement* stmt) {
726 Comment cmnt(masm_, "[ BreakStatement");
727 SetStatementPosition(stmt);
728 NestedStatement* current = nesting_stack_;
729 int stack_depth = 0;
730 while (!current->IsBreakTarget(stmt->target())) {
731 stack_depth = current->Exit(stack_depth);
732 current = current->outer();
733 }
734 __ Drop(stack_depth);
735
736 Breakable* target = current->AsBreakable();
737 __ jmp(target->break_target());
738}
739
740
741void FullCodeGenerator::VisitReturnStatement(ReturnStatement* stmt) {
742 Comment cmnt(masm_, "[ ReturnStatement");
743 SetStatementPosition(stmt);
744 Expression* expr = stmt->expression();
745 VisitForValue(expr, kAccumulator);
746
747 // Exit all nested statements.
748 NestedStatement* current = nesting_stack_;
749 int stack_depth = 0;
750 while (current != NULL) {
751 stack_depth = current->Exit(stack_depth);
752 current = current->outer();
753 }
754 __ Drop(stack_depth);
755
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100756 EmitReturnSequence();
Leon Clarked91b9f72010-01-27 17:25:45 +0000757}
758
759
760void FullCodeGenerator::VisitWithEnterStatement(WithEnterStatement* stmt) {
761 Comment cmnt(masm_, "[ WithEnterStatement");
762 SetStatementPosition(stmt);
763
764 VisitForValue(stmt->expression(), kStack);
765 if (stmt->is_catch_block()) {
766 __ CallRuntime(Runtime::kPushCatchContext, 1);
767 } else {
768 __ CallRuntime(Runtime::kPushContext, 1);
769 }
770 // Both runtime calls return the new context in both the context and the
771 // result registers.
772
773 // Update local stack frame context field.
774 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
775}
776
777
778void FullCodeGenerator::VisitWithExitStatement(WithExitStatement* stmt) {
779 Comment cmnt(masm_, "[ WithExitStatement");
780 SetStatementPosition(stmt);
781
782 // Pop context.
783 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
784 // Update local stack frame context field.
785 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
786}
787
788
Leon Clarked91b9f72010-01-27 17:25:45 +0000789void FullCodeGenerator::VisitDoWhileStatement(DoWhileStatement* stmt) {
790 Comment cmnt(masm_, "[ DoWhileStatement");
791 SetStatementPosition(stmt);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100792 Label body, stack_limit_hit, stack_check_success, done;
Leon Clarked91b9f72010-01-27 17:25:45 +0000793
794 Iteration loop_statement(this, stmt);
795 increment_loop_depth();
796
797 __ bind(&body);
798 Visit(stmt->body());
799
800 // Check stack before looping.
801 __ StackLimitCheck(&stack_limit_hit);
802 __ bind(&stack_check_success);
803
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100804 // Record the position of the do while condition and make sure it is
805 // possible to break on the condition.
Leon Clarked91b9f72010-01-27 17:25:45 +0000806 __ bind(loop_statement.continue_target());
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100807 SetExpressionPosition(stmt->cond(), stmt->condition_position());
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100808 VisitForControl(stmt->cond(),
809 &body,
810 loop_statement.break_target(),
811 loop_statement.break_target());
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100812
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100813 __ bind(loop_statement.break_target());
814 __ jmp(&done);
Leon Clarked91b9f72010-01-27 17:25:45 +0000815
816 __ bind(&stack_limit_hit);
817 StackCheckStub stack_stub;
818 __ CallStub(&stack_stub);
819 __ jmp(&stack_check_success);
820
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100821 __ bind(&done);
Leon Clarked91b9f72010-01-27 17:25:45 +0000822 decrement_loop_depth();
823}
824
825
826void FullCodeGenerator::VisitWhileStatement(WhileStatement* stmt) {
827 Comment cmnt(masm_, "[ WhileStatement");
Iain Merrick9ac36c92010-09-13 15:29:50 +0100828 Label body, stack_limit_hit, stack_check_success, done;
Leon Clarked91b9f72010-01-27 17:25:45 +0000829
830 Iteration loop_statement(this, stmt);
831 increment_loop_depth();
832
833 // Emit the test at the bottom of the loop.
834 __ jmp(loop_statement.continue_target());
835
836 __ bind(&body);
837 Visit(stmt->body());
Leon Clarked91b9f72010-01-27 17:25:45 +0000838 __ bind(loop_statement.continue_target());
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100839
840 // Emit the statement position here as this is where the while
841 // statement code starts.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100842 SetStatementPosition(stmt);
Leon Clarkef7060e22010-06-03 12:02:55 +0100843
Leon Clarked91b9f72010-01-27 17:25:45 +0000844 // Check stack before looping.
845 __ StackLimitCheck(&stack_limit_hit);
846 __ bind(&stack_check_success);
847
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100848 VisitForControl(stmt->cond(),
849 &body,
850 loop_statement.break_target(),
851 loop_statement.break_target());
Leon Clarked91b9f72010-01-27 17:25:45 +0000852
853 __ bind(loop_statement.break_target());
Iain Merrick9ac36c92010-09-13 15:29:50 +0100854 __ jmp(&done);
855
856 __ bind(&stack_limit_hit);
857 StackCheckStub stack_stub;
858 __ CallStub(&stack_stub);
859 __ jmp(&stack_check_success);
860
861 __ bind(&done);
Leon Clarked91b9f72010-01-27 17:25:45 +0000862 decrement_loop_depth();
863}
864
865
866void FullCodeGenerator::VisitForStatement(ForStatement* stmt) {
867 Comment cmnt(masm_, "[ ForStatement");
Leon Clarked91b9f72010-01-27 17:25:45 +0000868 Label test, body, stack_limit_hit, stack_check_success;
869
870 Iteration loop_statement(this, stmt);
871 if (stmt->init() != NULL) {
872 Visit(stmt->init());
873 }
874
875 increment_loop_depth();
876 // Emit the test at the bottom of the loop (even if empty).
877 __ jmp(&test);
878
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100879 __ bind(&stack_limit_hit);
880 StackCheckStub stack_stub;
881 __ CallStub(&stack_stub);
882 __ jmp(&stack_check_success);
883
Leon Clarked91b9f72010-01-27 17:25:45 +0000884 __ bind(&body);
885 Visit(stmt->body());
886
887 __ bind(loop_statement.continue_target());
888
889 SetStatementPosition(stmt);
890 if (stmt->next() != NULL) {
891 Visit(stmt->next());
892 }
893
894 __ bind(&test);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100895 // Emit the statement position here as this is where the for
896 // statement code starts.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100897 SetStatementPosition(stmt);
Leon Clarked91b9f72010-01-27 17:25:45 +0000898
899 // Check stack before looping.
900 __ StackLimitCheck(&stack_limit_hit);
901 __ bind(&stack_check_success);
902
903 if (stmt->cond() != NULL) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100904 VisitForControl(stmt->cond(),
905 &body,
906 loop_statement.break_target(),
907 loop_statement.break_target());
Leon Clarked91b9f72010-01-27 17:25:45 +0000908 } else {
909 __ jmp(&body);
910 }
911
Leon Clarked91b9f72010-01-27 17:25:45 +0000912 __ bind(loop_statement.break_target());
913 decrement_loop_depth();
914}
915
916
Leon Clarked91b9f72010-01-27 17:25:45 +0000917void FullCodeGenerator::VisitTryCatchStatement(TryCatchStatement* stmt) {
918 Comment cmnt(masm_, "[ TryCatchStatement");
919 SetStatementPosition(stmt);
920 // The try block adds a handler to the exception handler chain
921 // before entering, and removes it again when exiting normally.
922 // If an exception is thrown during execution of the try block,
923 // control is passed to the handler, which also consumes the handler.
924 // At this point, the exception is in a register, and store it in
925 // the temporary local variable (prints as ".catch-var") before
926 // executing the catch block. The catch block has been rewritten
927 // to introduce a new scope to bind the catch variable and to remove
928 // that scope again afterwards.
929
930 Label try_handler_setup, catch_entry, done;
931 __ Call(&try_handler_setup);
932 // Try handler code, exception in result register.
933
934 // Store exception in local .catch variable before executing catch block.
935 {
936 // The catch variable is *always* a variable proxy for a local variable.
937 Variable* catch_var = stmt->catch_var()->AsVariableProxy()->AsVariable();
938 ASSERT_NOT_NULL(catch_var);
939 Slot* variable_slot = catch_var->slot();
940 ASSERT_NOT_NULL(variable_slot);
941 ASSERT_EQ(Slot::LOCAL, variable_slot->type());
942 StoreToFrameField(SlotOffset(variable_slot), result_register());
943 }
944
945 Visit(stmt->catch_block());
946 __ jmp(&done);
947
948 // Try block code. Sets up the exception handler chain.
949 __ bind(&try_handler_setup);
950 {
951 TryCatch try_block(this, &catch_entry);
952 __ PushTryHandler(IN_JAVASCRIPT, TRY_CATCH_HANDLER);
953 Visit(stmt->try_block());
954 __ PopTryHandler();
955 }
956 __ bind(&done);
957}
958
959
960void FullCodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* stmt) {
961 Comment cmnt(masm_, "[ TryFinallyStatement");
962 SetStatementPosition(stmt);
963 // Try finally is compiled by setting up a try-handler on the stack while
964 // executing the try body, and removing it again afterwards.
965 //
966 // The try-finally construct can enter the finally block in three ways:
967 // 1. By exiting the try-block normally. This removes the try-handler and
968 // calls the finally block code before continuing.
969 // 2. By exiting the try-block with a function-local control flow transfer
970 // (break/continue/return). The site of the, e.g., break removes the
971 // try handler and calls the finally block code before continuing
972 // its outward control transfer.
973 // 3. by exiting the try-block with a thrown exception.
974 // This can happen in nested function calls. It traverses the try-handler
975 // chain and consumes the try-handler entry before jumping to the
976 // handler code. The handler code then calls the finally-block before
977 // rethrowing the exception.
978 //
979 // The finally block must assume a return address on top of the stack
980 // (or in the link register on ARM chips) and a value (return value or
981 // exception) in the result register (rax/eax/r0), both of which must
982 // be preserved. The return address isn't GC-safe, so it should be
983 // cooked before GC.
984 Label finally_entry;
985 Label try_handler_setup;
986
987 // Setup the try-handler chain. Use a call to
988 // Jump to try-handler setup and try-block code. Use call to put try-handler
989 // address on stack.
990 __ Call(&try_handler_setup);
991 // Try handler code. Return address of call is pushed on handler stack.
992 {
993 // This code is only executed during stack-handler traversal when an
994 // exception is thrown. The execption is in the result register, which
995 // is retained by the finally block.
996 // Call the finally block and then rethrow the exception.
997 __ Call(&finally_entry);
998 __ push(result_register());
999 __ CallRuntime(Runtime::kReThrow, 1);
1000 }
1001
1002 __ bind(&finally_entry);
1003 {
1004 // Finally block implementation.
1005 Finally finally_block(this);
1006 EnterFinallyBlock();
1007 Visit(stmt->finally_block());
1008 ExitFinallyBlock(); // Return to the calling code.
1009 }
1010
1011 __ bind(&try_handler_setup);
1012 {
1013 // Setup try handler (stack pointer registers).
1014 TryFinally try_block(this, &finally_entry);
1015 __ PushTryHandler(IN_JAVASCRIPT, TRY_FINALLY_HANDLER);
1016 Visit(stmt->try_block());
1017 __ PopTryHandler();
1018 }
1019 // Execute the finally block on the way out.
1020 __ Call(&finally_entry);
1021}
1022
1023
1024void FullCodeGenerator::VisitDebuggerStatement(DebuggerStatement* stmt) {
1025#ifdef ENABLE_DEBUGGER_SUPPORT
1026 Comment cmnt(masm_, "[ DebuggerStatement");
1027 SetStatementPosition(stmt);
Leon Clarke4515c472010-02-03 11:58:03 +00001028
Andrei Popescu402d9372010-02-26 13:31:12 +00001029 __ DebugBreak();
Leon Clarked91b9f72010-01-27 17:25:45 +00001030 // Ignore the return value.
1031#endif
1032}
1033
1034
Leon Clarked91b9f72010-01-27 17:25:45 +00001035void FullCodeGenerator::VisitConditional(Conditional* expr) {
1036 Comment cmnt(masm_, "[ Conditional");
1037 Label true_case, false_case, done;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001038 VisitForControl(expr->condition(), &true_case, &false_case, &true_case);
Leon Clarked91b9f72010-01-27 17:25:45 +00001039
1040 __ bind(&true_case);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001041 SetExpressionPosition(expr->then_expression(),
1042 expr->then_expression_position());
Leon Clarked91b9f72010-01-27 17:25:45 +00001043 Visit(expr->then_expression());
1044 // If control flow falls through Visit, jump to done.
1045 if (context_ == Expression::kEffect || context_ == Expression::kValue) {
1046 __ jmp(&done);
1047 }
1048
1049 __ bind(&false_case);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001050 SetExpressionPosition(expr->else_expression(),
1051 expr->else_expression_position());
Leon Clarked91b9f72010-01-27 17:25:45 +00001052 Visit(expr->else_expression());
1053 // If control flow falls through Visit, merge it with true case here.
1054 if (context_ == Expression::kEffect || context_ == Expression::kValue) {
1055 __ bind(&done);
1056 }
1057}
1058
1059
1060void FullCodeGenerator::VisitSlot(Slot* expr) {
1061 // Slots do not appear directly in the AST.
1062 UNREACHABLE();
1063}
1064
1065
1066void FullCodeGenerator::VisitLiteral(Literal* expr) {
1067 Comment cmnt(masm_, "[ Literal");
1068 Apply(context_, expr);
1069}
1070
1071
Leon Clarkef7060e22010-06-03 12:02:55 +01001072void FullCodeGenerator::VisitFunctionLiteral(FunctionLiteral* expr) {
1073 Comment cmnt(masm_, "[ FunctionLiteral");
1074
1075 // Build the function boilerplate and instantiate it.
1076 Handle<SharedFunctionInfo> function_info =
1077 Compiler::BuildFunctionInfo(expr, script(), this);
1078 if (HasStackOverflow()) return;
1079 EmitNewClosure(function_info);
1080}
1081
1082
1083void FullCodeGenerator::VisitSharedFunctionInfoLiteral(
1084 SharedFunctionInfoLiteral* expr) {
1085 Comment cmnt(masm_, "[ SharedFunctionInfoLiteral");
1086 EmitNewClosure(expr->shared_function_info());
1087}
1088
1089
Leon Clarked91b9f72010-01-27 17:25:45 +00001090void FullCodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* expr) {
1091 // Call runtime routine to allocate the catch extension object and
1092 // assign the exception value to the catch variable.
1093 Comment cmnt(masm_, "[ CatchExtensionObject");
1094 VisitForValue(expr->key(), kStack);
1095 VisitForValue(expr->value(), kStack);
1096 // Create catch extension object.
1097 __ CallRuntime(Runtime::kCreateCatchExtensionObject, 2);
1098 Apply(context_, result_register());
1099}
1100
1101
1102void FullCodeGenerator::VisitThrow(Throw* expr) {
1103 Comment cmnt(masm_, "[ Throw");
1104 VisitForValue(expr->exception(), kStack);
1105 __ CallRuntime(Runtime::kThrow, 1);
1106 // Never returns here.
1107}
1108
1109
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001110void FullCodeGenerator::VisitIncrementOperation(IncrementOperation* expr) {
1111 UNREACHABLE();
1112}
1113
1114
Leon Clarked91b9f72010-01-27 17:25:45 +00001115int FullCodeGenerator::TryFinally::Exit(int stack_depth) {
1116 // The macros used here must preserve the result register.
1117 __ Drop(stack_depth);
1118 __ PopTryHandler();
1119 __ Call(finally_entry_);
1120 return 0;
1121}
1122
1123
1124int FullCodeGenerator::TryCatch::Exit(int stack_depth) {
1125 // The macros used here must preserve the result register.
1126 __ Drop(stack_depth);
1127 __ PopTryHandler();
1128 return 0;
1129}
1130
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001131
1132void FullCodeGenerator::EmitRegExpCloneResult(ZoneList<Expression*>* args) {
1133 ASSERT(args->length() == 1);
1134 VisitForValue(args->at(0), kStack);
1135 __ CallRuntime(Runtime::kRegExpCloneResult, 1);
1136 Apply(context_, result_register());
1137}
1138
Leon Clarked91b9f72010-01-27 17:25:45 +00001139#undef __
1140
1141
1142} } // namespace v8::internal