blob: f7a063aed8b9c1e65ae27178ad5abad0760b37d2 [file] [log] [blame]
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001// Copyright 2009 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
30#include "codegen-inl.h"
31#include "compiler.h"
32#include "full-codegen.h"
sgjesse@chromium.org833cdd72010-02-26 10:06:16 +000033#include "scopes.h"
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000034#include "stub-cache.h"
35#include "debug.h"
ager@chromium.org5c838252010-02-19 08:53:10 +000036#include "liveedit.h"
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000037
38namespace v8 {
39namespace internal {
40
41#define BAILOUT(reason) \
42 do { \
43 if (FLAG_trace_bailout) { \
44 PrintF("%s\n", reason); \
45 } \
46 has_supported_syntax_ = false; \
47 return; \
48 } while (false)
49
50
51#define CHECK_BAILOUT \
52 do { \
53 if (!has_supported_syntax_) return; \
54 } while (false)
55
56
57void FullCodeGenSyntaxChecker::Check(FunctionLiteral* fun) {
58 Scope* scope = fun->scope();
59 VisitDeclarations(scope->declarations());
60 CHECK_BAILOUT;
61
62 VisitStatements(fun->body());
63}
64
65
66void FullCodeGenSyntaxChecker::VisitDeclarations(
67 ZoneList<Declaration*>* decls) {
68 for (int i = 0; i < decls->length(); i++) {
69 Visit(decls->at(i));
70 CHECK_BAILOUT;
71 }
72}
73
74
75void FullCodeGenSyntaxChecker::VisitStatements(ZoneList<Statement*>* stmts) {
76 for (int i = 0, len = stmts->length(); i < len; i++) {
77 Visit(stmts->at(i));
78 CHECK_BAILOUT;
79 }
80}
81
82
83void FullCodeGenSyntaxChecker::VisitDeclaration(Declaration* decl) {
84 Property* prop = decl->proxy()->AsProperty();
85 if (prop != NULL) {
86 Visit(prop->obj());
87 Visit(prop->key());
88 }
89
90 if (decl->fun() != NULL) {
91 Visit(decl->fun());
92 }
93}
94
95
96void FullCodeGenSyntaxChecker::VisitBlock(Block* stmt) {
97 VisitStatements(stmt->statements());
98}
99
100
101void FullCodeGenSyntaxChecker::VisitExpressionStatement(
102 ExpressionStatement* stmt) {
103 Visit(stmt->expression());
104}
105
106
107void FullCodeGenSyntaxChecker::VisitEmptyStatement(EmptyStatement* stmt) {
108 // Supported.
109}
110
111
112void FullCodeGenSyntaxChecker::VisitIfStatement(IfStatement* stmt) {
113 Visit(stmt->condition());
114 CHECK_BAILOUT;
115 Visit(stmt->then_statement());
116 CHECK_BAILOUT;
117 Visit(stmt->else_statement());
118}
119
120
121void FullCodeGenSyntaxChecker::VisitContinueStatement(ContinueStatement* stmt) {
122 // Supported.
123}
124
125
126void FullCodeGenSyntaxChecker::VisitBreakStatement(BreakStatement* stmt) {
127 // Supported.
128}
129
130
131void FullCodeGenSyntaxChecker::VisitReturnStatement(ReturnStatement* stmt) {
132 Visit(stmt->expression());
133}
134
135
136void FullCodeGenSyntaxChecker::VisitWithEnterStatement(
137 WithEnterStatement* stmt) {
138 Visit(stmt->expression());
139}
140
141
142void FullCodeGenSyntaxChecker::VisitWithExitStatement(WithExitStatement* stmt) {
143 // Supported.
144}
145
146
147void FullCodeGenSyntaxChecker::VisitSwitchStatement(SwitchStatement* stmt) {
148 BAILOUT("SwitchStatement");
149}
150
151
152void FullCodeGenSyntaxChecker::VisitDoWhileStatement(DoWhileStatement* stmt) {
153 Visit(stmt->cond());
154 CHECK_BAILOUT;
155 Visit(stmt->body());
156}
157
158
159void FullCodeGenSyntaxChecker::VisitWhileStatement(WhileStatement* stmt) {
160 Visit(stmt->cond());
161 CHECK_BAILOUT;
162 Visit(stmt->body());
163}
164
165
166void FullCodeGenSyntaxChecker::VisitForStatement(ForStatement* stmt) {
167 if (!FLAG_always_full_compiler) BAILOUT("ForStatement");
168 if (stmt->init() != NULL) {
169 Visit(stmt->init());
170 CHECK_BAILOUT;
171 }
172 if (stmt->cond() != NULL) {
173 Visit(stmt->cond());
174 CHECK_BAILOUT;
175 }
176 Visit(stmt->body());
177 if (stmt->next() != NULL) {
178 CHECK_BAILOUT;
179 Visit(stmt->next());
180 }
181}
182
183
184void FullCodeGenSyntaxChecker::VisitForInStatement(ForInStatement* stmt) {
185 BAILOUT("ForInStatement");
186}
187
188
189void FullCodeGenSyntaxChecker::VisitTryCatchStatement(TryCatchStatement* stmt) {
190 Visit(stmt->try_block());
191 CHECK_BAILOUT;
192 Visit(stmt->catch_block());
193}
194
195
196void FullCodeGenSyntaxChecker::VisitTryFinallyStatement(
197 TryFinallyStatement* stmt) {
198 Visit(stmt->try_block());
199 CHECK_BAILOUT;
200 Visit(stmt->finally_block());
201}
202
203
204void FullCodeGenSyntaxChecker::VisitDebuggerStatement(
205 DebuggerStatement* stmt) {
206 // Supported.
207}
208
209
210void FullCodeGenSyntaxChecker::VisitFunctionLiteral(FunctionLiteral* expr) {
211 // Supported.
212}
213
214
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000215void FullCodeGenSyntaxChecker::VisitSharedFunctionInfoLiteral(
216 SharedFunctionInfoLiteral* expr) {
217 BAILOUT("SharedFunctionInfoLiteral");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000218}
219
220
221void FullCodeGenSyntaxChecker::VisitConditional(Conditional* expr) {
222 Visit(expr->condition());
223 CHECK_BAILOUT;
224 Visit(expr->then_expression());
225 CHECK_BAILOUT;
226 Visit(expr->else_expression());
227}
228
229
230void FullCodeGenSyntaxChecker::VisitSlot(Slot* expr) {
231 UNREACHABLE();
232}
233
234
235void FullCodeGenSyntaxChecker::VisitVariableProxy(VariableProxy* expr) {
236 // Supported.
237}
238
239
240void FullCodeGenSyntaxChecker::VisitLiteral(Literal* expr) {
241 // Supported.
242}
243
244
245void FullCodeGenSyntaxChecker::VisitRegExpLiteral(RegExpLiteral* expr) {
246 // Supported.
247}
248
249
250void FullCodeGenSyntaxChecker::VisitObjectLiteral(ObjectLiteral* expr) {
251 ZoneList<ObjectLiteral::Property*>* properties = expr->properties();
252
253 for (int i = 0, len = properties->length(); i < len; i++) {
254 ObjectLiteral::Property* property = properties->at(i);
255 if (property->IsCompileTimeValue()) continue;
256 Visit(property->key());
257 CHECK_BAILOUT;
258 Visit(property->value());
259 CHECK_BAILOUT;
260 }
261}
262
263
264void FullCodeGenSyntaxChecker::VisitArrayLiteral(ArrayLiteral* expr) {
265 ZoneList<Expression*>* subexprs = expr->values();
266 for (int i = 0, len = subexprs->length(); i < len; i++) {
267 Expression* subexpr = subexprs->at(i);
268 if (subexpr->AsLiteral() != NULL) continue;
269 if (CompileTimeValue::IsCompileTimeValue(subexpr)) continue;
270 Visit(subexpr);
271 CHECK_BAILOUT;
272 }
273}
274
275
276void FullCodeGenSyntaxChecker::VisitCatchExtensionObject(
277 CatchExtensionObject* expr) {
278 Visit(expr->key());
279 CHECK_BAILOUT;
280 Visit(expr->value());
281}
282
283
284void FullCodeGenSyntaxChecker::VisitAssignment(Assignment* expr) {
285 Token::Value op = expr->op();
286 if (op == Token::INIT_CONST) BAILOUT("initialize constant");
287
288 Variable* var = expr->target()->AsVariableProxy()->AsVariable();
289 Property* prop = expr->target()->AsProperty();
290 ASSERT(var == NULL || prop == NULL);
291 if (var != NULL) {
292 if (var->mode() == Variable::CONST) BAILOUT("Assignment to const");
293 // All other variables are supported.
294 } else if (prop != NULL) {
295 Visit(prop->obj());
296 CHECK_BAILOUT;
297 Visit(prop->key());
298 CHECK_BAILOUT;
299 } else {
300 // This is a throw reference error.
301 BAILOUT("non-variable/non-property assignment");
302 }
303
304 Visit(expr->value());
305}
306
307
308void FullCodeGenSyntaxChecker::VisitThrow(Throw* expr) {
309 Visit(expr->exception());
310}
311
312
313void FullCodeGenSyntaxChecker::VisitProperty(Property* expr) {
314 Visit(expr->obj());
315 CHECK_BAILOUT;
316 Visit(expr->key());
317}
318
319
320void FullCodeGenSyntaxChecker::VisitCall(Call* expr) {
321 Expression* fun = expr->expression();
322 ZoneList<Expression*>* args = expr->arguments();
323 Variable* var = fun->AsVariableProxy()->AsVariable();
324
325 // Check for supported calls
326 if (var != NULL && var->is_possibly_eval()) {
327 BAILOUT("call to the identifier 'eval'");
328 } else if (var != NULL && !var->is_this() && var->is_global()) {
329 // Calls to global variables are supported.
330 } else if (var != NULL && var->slot() != NULL &&
331 var->slot()->type() == Slot::LOOKUP) {
332 BAILOUT("call to a lookup slot");
333 } else if (fun->AsProperty() != NULL) {
334 Property* prop = fun->AsProperty();
335 Visit(prop->obj());
336 CHECK_BAILOUT;
337 Visit(prop->key());
338 CHECK_BAILOUT;
339 } else {
340 // Otherwise the call is supported if the function expression is.
341 Visit(fun);
342 }
343 // Check all arguments to the call.
344 for (int i = 0; i < args->length(); i++) {
345 Visit(args->at(i));
346 CHECK_BAILOUT;
347 }
348}
349
350
351void FullCodeGenSyntaxChecker::VisitCallNew(CallNew* expr) {
352 Visit(expr->expression());
353 CHECK_BAILOUT;
354 ZoneList<Expression*>* args = expr->arguments();
355 // Check all arguments to the call
356 for (int i = 0; i < args->length(); i++) {
357 Visit(args->at(i));
358 CHECK_BAILOUT;
359 }
360}
361
362
363void FullCodeGenSyntaxChecker::VisitCallRuntime(CallRuntime* expr) {
364 // Check for inline runtime call
365 if (expr->name()->Get(0) == '_' &&
366 CodeGenerator::FindInlineRuntimeLUT(expr->name()) != NULL) {
367 BAILOUT("inlined runtime call");
368 }
369 // Check all arguments to the call. (Relies on TEMP meaning STACK.)
370 for (int i = 0; i < expr->arguments()->length(); i++) {
371 Visit(expr->arguments()->at(i));
372 CHECK_BAILOUT;
373 }
374}
375
376
377void FullCodeGenSyntaxChecker::VisitUnaryOperation(UnaryOperation* expr) {
378 switch (expr->op()) {
379 case Token::ADD:
380 case Token::BIT_NOT:
381 case Token::NOT:
382 case Token::SUB:
383 case Token::TYPEOF:
384 case Token::VOID:
385 Visit(expr->expression());
386 break;
387 case Token::DELETE:
388 BAILOUT("UnaryOperation: DELETE");
389 default:
390 UNREACHABLE();
391 }
392}
393
394
395void FullCodeGenSyntaxChecker::VisitCountOperation(CountOperation* expr) {
396 Variable* var = expr->expression()->AsVariableProxy()->AsVariable();
397 Property* prop = expr->expression()->AsProperty();
398 ASSERT(var == NULL || prop == NULL);
399 if (var != NULL) {
400 // All global variables are supported.
401 if (!var->is_global()) {
402 ASSERT(var->slot() != NULL);
403 Slot::Type type = var->slot()->type();
404 if (type == Slot::LOOKUP) {
405 BAILOUT("CountOperation with lookup slot");
406 }
407 }
408 } else if (prop != NULL) {
409 Visit(prop->obj());
410 CHECK_BAILOUT;
411 Visit(prop->key());
412 CHECK_BAILOUT;
413 } else {
414 // This is a throw reference error.
415 BAILOUT("CountOperation non-variable/non-property expression");
416 }
417}
418
419
420void FullCodeGenSyntaxChecker::VisitBinaryOperation(BinaryOperation* expr) {
421 Visit(expr->left());
422 CHECK_BAILOUT;
423 Visit(expr->right());
424}
425
426
427void FullCodeGenSyntaxChecker::VisitCompareOperation(CompareOperation* expr) {
428 Visit(expr->left());
429 CHECK_BAILOUT;
430 Visit(expr->right());
431}
432
433
434void FullCodeGenSyntaxChecker::VisitThisFunction(ThisFunction* expr) {
435 // Supported.
436}
437
438#undef BAILOUT
439#undef CHECK_BAILOUT
440
441
442#define __ ACCESS_MASM(masm())
443
ager@chromium.org5c838252010-02-19 08:53:10 +0000444Handle<Code> FullCodeGenerator::MakeCode(CompilationInfo* info) {
445 Handle<Script> script = info->script();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000446 if (!script->IsUndefined() && !script->source()->IsUndefined()) {
447 int len = String::cast(script->source())->length();
448 Counters::total_full_codegen_source_size.Increment(len);
449 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000450 CodeGenerator::MakeCodePrologue(info);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000451 const int kInitialBufferSize = 4 * KB;
452 MacroAssembler masm(NULL, kInitialBufferSize);
ager@chromium.org5c838252010-02-19 08:53:10 +0000453
454 FullCodeGenerator cgen(&masm);
455 cgen.Generate(info, PRIMARY);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000456 if (cgen.HasStackOverflow()) {
457 ASSERT(!Top::has_pending_exception());
458 return Handle<Code>::null();
459 }
460 Code::Flags flags = Code::ComputeFlags(Code::FUNCTION, NOT_IN_LOOP);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000461 return CodeGenerator::MakeCodeEpilogue(&masm, flags, info);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000462}
463
464
465int FullCodeGenerator::SlotOffset(Slot* slot) {
466 ASSERT(slot != NULL);
467 // Offset is negative because higher indexes are at lower addresses.
468 int offset = -slot->index() * kPointerSize;
469 // Adjust by a (parameter or local) base offset.
470 switch (slot->type()) {
471 case Slot::PARAMETER:
ager@chromium.org5c838252010-02-19 08:53:10 +0000472 offset += (scope()->num_parameters() + 1) * kPointerSize;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000473 break;
474 case Slot::LOCAL:
475 offset += JavaScriptFrameConstants::kLocal0Offset;
476 break;
477 case Slot::CONTEXT:
478 case Slot::LOOKUP:
479 UNREACHABLE();
480 }
481 return offset;
482}
483
484
485void FullCodeGenerator::VisitDeclarations(
486 ZoneList<Declaration*>* declarations) {
487 int length = declarations->length();
488 int globals = 0;
489 for (int i = 0; i < length; i++) {
490 Declaration* decl = declarations->at(i);
491 Variable* var = decl->proxy()->var();
492 Slot* slot = var->slot();
493
494 // If it was not possible to allocate the variable at compile
495 // time, we need to "declare" it at runtime to make sure it
496 // actually exists in the local context.
497 if ((slot != NULL && slot->type() == Slot::LOOKUP) || !var->is_global()) {
498 VisitDeclaration(decl);
499 } else {
500 // Count global variables and functions for later processing
501 globals++;
502 }
503 }
504
505 // Compute array of global variable and function declarations.
506 // Do nothing in case of no declared global functions or variables.
507 if (globals > 0) {
508 Handle<FixedArray> array = Factory::NewFixedArray(2 * globals, TENURED);
509 for (int j = 0, i = 0; i < length; i++) {
510 Declaration* decl = declarations->at(i);
511 Variable* var = decl->proxy()->var();
512 Slot* slot = var->slot();
513
514 if ((slot == NULL || slot->type() != Slot::LOOKUP) && var->is_global()) {
515 array->set(j++, *(var->name()));
516 if (decl->fun() == NULL) {
517 if (var->mode() == Variable::CONST) {
518 // In case this is const property use the hole.
519 array->set_the_hole(j++);
520 } else {
521 array->set_undefined(j++);
522 }
523 } else {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000524 Handle<SharedFunctionInfo> function =
525 Compiler::BuildFunctionInfo(decl->fun(), script(), this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000526 // Check for stack-overflow exception.
527 if (HasStackOverflow()) return;
528 array->set(j++, *function);
529 }
530 }
531 }
532 // Invoke the platform-dependent code generator to do the actual
533 // declaration the global variables and functions.
534 DeclareGlobals(array);
535 }
536}
537
538
539void FullCodeGenerator::SetFunctionPosition(FunctionLiteral* fun) {
540 if (FLAG_debug_info) {
541 CodeGenerator::RecordPositions(masm_, fun->start_position());
542 }
543}
544
545
546void FullCodeGenerator::SetReturnPosition(FunctionLiteral* fun) {
547 if (FLAG_debug_info) {
548 CodeGenerator::RecordPositions(masm_, fun->end_position());
549 }
550}
551
552
553void FullCodeGenerator::SetStatementPosition(Statement* stmt) {
554 if (FLAG_debug_info) {
555 CodeGenerator::RecordPositions(masm_, stmt->statement_pos());
556 }
557}
558
559
560void FullCodeGenerator::SetStatementPosition(int pos) {
561 if (FLAG_debug_info) {
562 CodeGenerator::RecordPositions(masm_, pos);
563 }
564}
565
566
567void FullCodeGenerator::SetSourcePosition(int pos) {
568 if (FLAG_debug_info && pos != RelocInfo::kNoPosition) {
569 masm_->RecordPosition(pos);
570 }
571}
572
573
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000574void FullCodeGenerator::EmitInlineRuntimeCall(CallRuntime* expr) {
575 Handle<String> name = expr->name();
576 if (strcmp("_IsSmi", *name->ToCString()) == 0) {
577 EmitIsSmi(expr->arguments());
578 } else if (strcmp("_IsNonNegativeSmi", *name->ToCString()) == 0) {
579 EmitIsNonNegativeSmi(expr->arguments());
580 } else if (strcmp("_IsObject", *name->ToCString()) == 0) {
581 EmitIsObject(expr->arguments());
582 } else if (strcmp("_IsUndetectableObject", *name->ToCString()) == 0) {
583 EmitIsUndetectableObject(expr->arguments());
584 } else if (strcmp("_IsFunction", *name->ToCString()) == 0) {
585 EmitIsFunction(expr->arguments());
586 } else if (strcmp("_IsArray", *name->ToCString()) == 0) {
587 EmitIsArray(expr->arguments());
588 } else if (strcmp("_IsRegExp", *name->ToCString()) == 0) {
589 EmitIsRegExp(expr->arguments());
590 } else if (strcmp("_IsConstructCall", *name->ToCString()) == 0) {
591 EmitIsConstructCall(expr->arguments());
592 } else if (strcmp("_ObjectEquals", *name->ToCString()) == 0) {
593 EmitObjectEquals(expr->arguments());
594 } else if (strcmp("_Arguments", *name->ToCString()) == 0) {
595 EmitArguments(expr->arguments());
596 } else if (strcmp("_ArgumentsLength", *name->ToCString()) == 0) {
597 EmitArgumentsLength(expr->arguments());
598 } else if (strcmp("_ClassOf", *name->ToCString()) == 0) {
599 EmitClassOf(expr->arguments());
600 } else if (strcmp("_Log", *name->ToCString()) == 0) {
601 EmitLog(expr->arguments());
602 } else if (strcmp("_RandomHeapNumber", *name->ToCString()) == 0) {
603 EmitRandomHeapNumber(expr->arguments());
604 } else if (strcmp("_SubString", *name->ToCString()) == 0) {
605 EmitSubString(expr->arguments());
606 } else if (strcmp("_RegExpExec", *name->ToCString()) == 0) {
607 EmitRegExpExec(expr->arguments());
608 } else if (strcmp("_ValueOf", *name->ToCString()) == 0) {
609 EmitValueOf(expr->arguments());
610 } else if (strcmp("_SetValueOf", *name->ToCString()) == 0) {
611 EmitSetValueOf(expr->arguments());
612 } else if (strcmp("_NumberToString", *name->ToCString()) == 0) {
613 EmitNumberToString(expr->arguments());
614 } else if (strcmp("_StringCharFromCode", *name->ToCString()) == 0) {
615 EmitStringCharFromCode(expr->arguments());
616 } else if (strcmp("_StringCharCodeAt", *name->ToCString()) == 0) {
617 EmitStringCharCodeAt(expr->arguments());
618 } else if (strcmp("_StringCharAt", *name->ToCString()) == 0) {
619 EmitStringCharAt(expr->arguments());
620 } else if (strcmp("_StringAdd", *name->ToCString()) == 0) {
621 EmitStringAdd(expr->arguments());
622 } else if (strcmp("_StringCompare", *name->ToCString()) == 0) {
623 EmitStringCompare(expr->arguments());
624 } else if (strcmp("_MathPow", *name->ToCString()) == 0) {
625 EmitMathPow(expr->arguments());
626 } else if (strcmp("_MathSin", *name->ToCString()) == 0) {
627 EmitMathSin(expr->arguments());
628 } else if (strcmp("_MathCos", *name->ToCString()) == 0) {
629 EmitMathCos(expr->arguments());
630 } else if (strcmp("_MathSqrt", *name->ToCString()) == 0) {
631 EmitMathSqrt(expr->arguments());
632 } else if (strcmp("_CallFunction", *name->ToCString()) == 0) {
633 EmitCallFunction(expr->arguments());
634 } else if (strcmp("_RegExpConstructResult", *name->ToCString()) == 0) {
635 EmitRegExpConstructResult(expr->arguments());
636 } else if (strcmp("_SwapElements", *name->ToCString()) == 0) {
637 EmitSwapElements(expr->arguments());
638 } else if (strcmp("_GetFromCache", *name->ToCString()) == 0) {
639 EmitGetFromCache(expr->arguments());
640 } else {
641 UNREACHABLE();
642 }
643}
644
645
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000646void FullCodeGenerator::EmitLogicalOperation(BinaryOperation* expr) {
647 Label eval_right, done;
648
649 // Set up the appropriate context for the left subexpression based
650 // on the operation and our own context. Initially assume we can
651 // inherit both true and false labels from our context.
652 if (expr->op() == Token::OR) {
653 switch (context_) {
654 case Expression::kUninitialized:
655 UNREACHABLE();
656 case Expression::kEffect:
657 VisitForControl(expr->left(), &done, &eval_right);
658 break;
659 case Expression::kValue:
660 VisitForValueControl(expr->left(),
661 location_,
662 &done,
663 &eval_right);
664 break;
665 case Expression::kTest:
666 VisitForControl(expr->left(), true_label_, &eval_right);
667 break;
668 case Expression::kValueTest:
669 VisitForValueControl(expr->left(),
670 location_,
671 true_label_,
672 &eval_right);
673 break;
674 case Expression::kTestValue:
675 VisitForControl(expr->left(), true_label_, &eval_right);
676 break;
677 }
678 } else {
679 ASSERT_EQ(Token::AND, expr->op());
680 switch (context_) {
681 case Expression::kUninitialized:
682 UNREACHABLE();
683 case Expression::kEffect:
684 VisitForControl(expr->left(), &eval_right, &done);
685 break;
686 case Expression::kValue:
687 VisitForControlValue(expr->left(),
688 location_,
689 &eval_right,
690 &done);
691 break;
692 case Expression::kTest:
693 VisitForControl(expr->left(), &eval_right, false_label_);
694 break;
695 case Expression::kValueTest:
696 VisitForControl(expr->left(), &eval_right, false_label_);
697 break;
698 case Expression::kTestValue:
699 VisitForControlValue(expr->left(),
700 location_,
701 &eval_right,
702 false_label_);
703 break;
704 }
705 }
706
707 __ bind(&eval_right);
708 Visit(expr->right());
709
710 __ bind(&done);
711}
712
713
714void FullCodeGenerator::VisitBlock(Block* stmt) {
715 Comment cmnt(masm_, "[ Block");
716 Breakable nested_statement(this, stmt);
717 SetStatementPosition(stmt);
718 VisitStatements(stmt->statements());
719 __ bind(nested_statement.break_target());
720}
721
722
723void FullCodeGenerator::VisitExpressionStatement(ExpressionStatement* stmt) {
724 Comment cmnt(masm_, "[ ExpressionStatement");
725 SetStatementPosition(stmt);
726 VisitForEffect(stmt->expression());
727}
728
729
730void FullCodeGenerator::VisitEmptyStatement(EmptyStatement* stmt) {
731 Comment cmnt(masm_, "[ EmptyStatement");
732 SetStatementPosition(stmt);
733}
734
735
736void FullCodeGenerator::VisitIfStatement(IfStatement* stmt) {
737 Comment cmnt(masm_, "[ IfStatement");
738 SetStatementPosition(stmt);
739 Label then_part, else_part, done;
740
741 // Do not worry about optimizing for empty then or else bodies.
742 VisitForControl(stmt->condition(), &then_part, &else_part);
743
744 __ bind(&then_part);
745 Visit(stmt->then_statement());
746 __ jmp(&done);
747
748 __ bind(&else_part);
749 Visit(stmt->else_statement());
750
751 __ bind(&done);
752}
753
754
755void FullCodeGenerator::VisitContinueStatement(ContinueStatement* stmt) {
756 Comment cmnt(masm_, "[ ContinueStatement");
757 SetStatementPosition(stmt);
758 NestedStatement* current = nesting_stack_;
759 int stack_depth = 0;
760 while (!current->IsContinueTarget(stmt->target())) {
761 stack_depth = current->Exit(stack_depth);
762 current = current->outer();
763 }
764 __ Drop(stack_depth);
765
766 Iteration* loop = current->AsIteration();
767 __ jmp(loop->continue_target());
768}
769
770
771void FullCodeGenerator::VisitBreakStatement(BreakStatement* stmt) {
772 Comment cmnt(masm_, "[ BreakStatement");
773 SetStatementPosition(stmt);
774 NestedStatement* current = nesting_stack_;
775 int stack_depth = 0;
776 while (!current->IsBreakTarget(stmt->target())) {
777 stack_depth = current->Exit(stack_depth);
778 current = current->outer();
779 }
780 __ Drop(stack_depth);
781
782 Breakable* target = current->AsBreakable();
783 __ jmp(target->break_target());
784}
785
786
787void FullCodeGenerator::VisitReturnStatement(ReturnStatement* stmt) {
788 Comment cmnt(masm_, "[ ReturnStatement");
789 SetStatementPosition(stmt);
790 Expression* expr = stmt->expression();
791 VisitForValue(expr, kAccumulator);
792
793 // Exit all nested statements.
794 NestedStatement* current = nesting_stack_;
795 int stack_depth = 0;
796 while (current != NULL) {
797 stack_depth = current->Exit(stack_depth);
798 current = current->outer();
799 }
800 __ Drop(stack_depth);
801
802 EmitReturnSequence(stmt->statement_pos());
803}
804
805
806void FullCodeGenerator::VisitWithEnterStatement(WithEnterStatement* stmt) {
807 Comment cmnt(masm_, "[ WithEnterStatement");
808 SetStatementPosition(stmt);
809
810 VisitForValue(stmt->expression(), kStack);
811 if (stmt->is_catch_block()) {
812 __ CallRuntime(Runtime::kPushCatchContext, 1);
813 } else {
814 __ CallRuntime(Runtime::kPushContext, 1);
815 }
816 // Both runtime calls return the new context in both the context and the
817 // result registers.
818
819 // Update local stack frame context field.
820 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
821}
822
823
824void FullCodeGenerator::VisitWithExitStatement(WithExitStatement* stmt) {
825 Comment cmnt(masm_, "[ WithExitStatement");
826 SetStatementPosition(stmt);
827
828 // Pop context.
829 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
830 // Update local stack frame context field.
831 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
832}
833
834
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000835void FullCodeGenerator::VisitDoWhileStatement(DoWhileStatement* stmt) {
836 Comment cmnt(masm_, "[ DoWhileStatement");
837 SetStatementPosition(stmt);
838 Label body, stack_limit_hit, stack_check_success;
839
840 Iteration loop_statement(this, stmt);
841 increment_loop_depth();
842
843 __ bind(&body);
844 Visit(stmt->body());
845
846 // Check stack before looping.
847 __ StackLimitCheck(&stack_limit_hit);
848 __ bind(&stack_check_success);
849
850 __ bind(loop_statement.continue_target());
851 SetStatementPosition(stmt->condition_position());
852 VisitForControl(stmt->cond(), &body, loop_statement.break_target());
853
854 __ bind(&stack_limit_hit);
855 StackCheckStub stack_stub;
856 __ CallStub(&stack_stub);
857 __ jmp(&stack_check_success);
858
859 __ bind(loop_statement.break_target());
860
861 decrement_loop_depth();
862}
863
864
865void FullCodeGenerator::VisitWhileStatement(WhileStatement* stmt) {
866 Comment cmnt(masm_, "[ WhileStatement");
867 SetStatementPosition(stmt);
868 Label body, stack_limit_hit, stack_check_success;
869
870 Iteration loop_statement(this, stmt);
871 increment_loop_depth();
872
873 // Emit the test at the bottom of the loop.
874 __ jmp(loop_statement.continue_target());
875
876 __ bind(&body);
877 Visit(stmt->body());
878
879 __ bind(loop_statement.continue_target());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000880
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000881 // Check stack before looping.
882 __ StackLimitCheck(&stack_limit_hit);
883 __ bind(&stack_check_success);
884
885 VisitForControl(stmt->cond(), &body, loop_statement.break_target());
886
887 __ bind(&stack_limit_hit);
888 StackCheckStub stack_stub;
889 __ CallStub(&stack_stub);
890 __ jmp(&stack_check_success);
891
892 __ bind(loop_statement.break_target());
893 decrement_loop_depth();
894}
895
896
897void FullCodeGenerator::VisitForStatement(ForStatement* stmt) {
898 Comment cmnt(masm_, "[ ForStatement");
899 SetStatementPosition(stmt);
900 Label test, body, stack_limit_hit, stack_check_success;
901
902 Iteration loop_statement(this, stmt);
903 if (stmt->init() != NULL) {
904 Visit(stmt->init());
905 }
906
907 increment_loop_depth();
908 // Emit the test at the bottom of the loop (even if empty).
909 __ jmp(&test);
910
911 __ bind(&body);
912 Visit(stmt->body());
913
914 __ bind(loop_statement.continue_target());
915
916 SetStatementPosition(stmt);
917 if (stmt->next() != NULL) {
918 Visit(stmt->next());
919 }
920
921 __ bind(&test);
922
923 // Check stack before looping.
924 __ StackLimitCheck(&stack_limit_hit);
925 __ bind(&stack_check_success);
926
927 if (stmt->cond() != NULL) {
928 VisitForControl(stmt->cond(), &body, loop_statement.break_target());
929 } else {
930 __ jmp(&body);
931 }
932
933 __ bind(&stack_limit_hit);
934 StackCheckStub stack_stub;
935 __ CallStub(&stack_stub);
936 __ jmp(&stack_check_success);
937
938 __ bind(loop_statement.break_target());
939 decrement_loop_depth();
940}
941
942
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000943void FullCodeGenerator::VisitTryCatchStatement(TryCatchStatement* stmt) {
944 Comment cmnt(masm_, "[ TryCatchStatement");
945 SetStatementPosition(stmt);
946 // The try block adds a handler to the exception handler chain
947 // before entering, and removes it again when exiting normally.
948 // If an exception is thrown during execution of the try block,
949 // control is passed to the handler, which also consumes the handler.
950 // At this point, the exception is in a register, and store it in
951 // the temporary local variable (prints as ".catch-var") before
952 // executing the catch block. The catch block has been rewritten
953 // to introduce a new scope to bind the catch variable and to remove
954 // that scope again afterwards.
955
956 Label try_handler_setup, catch_entry, done;
957 __ Call(&try_handler_setup);
958 // Try handler code, exception in result register.
959
960 // Store exception in local .catch variable before executing catch block.
961 {
962 // The catch variable is *always* a variable proxy for a local variable.
963 Variable* catch_var = stmt->catch_var()->AsVariableProxy()->AsVariable();
964 ASSERT_NOT_NULL(catch_var);
965 Slot* variable_slot = catch_var->slot();
966 ASSERT_NOT_NULL(variable_slot);
967 ASSERT_EQ(Slot::LOCAL, variable_slot->type());
968 StoreToFrameField(SlotOffset(variable_slot), result_register());
969 }
970
971 Visit(stmt->catch_block());
972 __ jmp(&done);
973
974 // Try block code. Sets up the exception handler chain.
975 __ bind(&try_handler_setup);
976 {
977 TryCatch try_block(this, &catch_entry);
978 __ PushTryHandler(IN_JAVASCRIPT, TRY_CATCH_HANDLER);
979 Visit(stmt->try_block());
980 __ PopTryHandler();
981 }
982 __ bind(&done);
983}
984
985
986void FullCodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* stmt) {
987 Comment cmnt(masm_, "[ TryFinallyStatement");
988 SetStatementPosition(stmt);
989 // Try finally is compiled by setting up a try-handler on the stack while
990 // executing the try body, and removing it again afterwards.
991 //
992 // The try-finally construct can enter the finally block in three ways:
993 // 1. By exiting the try-block normally. This removes the try-handler and
994 // calls the finally block code before continuing.
995 // 2. By exiting the try-block with a function-local control flow transfer
996 // (break/continue/return). The site of the, e.g., break removes the
997 // try handler and calls the finally block code before continuing
998 // its outward control transfer.
999 // 3. by exiting the try-block with a thrown exception.
1000 // This can happen in nested function calls. It traverses the try-handler
1001 // chain and consumes the try-handler entry before jumping to the
1002 // handler code. The handler code then calls the finally-block before
1003 // rethrowing the exception.
1004 //
1005 // The finally block must assume a return address on top of the stack
1006 // (or in the link register on ARM chips) and a value (return value or
1007 // exception) in the result register (rax/eax/r0), both of which must
1008 // be preserved. The return address isn't GC-safe, so it should be
1009 // cooked before GC.
1010 Label finally_entry;
1011 Label try_handler_setup;
1012
1013 // Setup the try-handler chain. Use a call to
1014 // Jump to try-handler setup and try-block code. Use call to put try-handler
1015 // address on stack.
1016 __ Call(&try_handler_setup);
1017 // Try handler code. Return address of call is pushed on handler stack.
1018 {
1019 // This code is only executed during stack-handler traversal when an
1020 // exception is thrown. The execption is in the result register, which
1021 // is retained by the finally block.
1022 // Call the finally block and then rethrow the exception.
1023 __ Call(&finally_entry);
1024 __ push(result_register());
1025 __ CallRuntime(Runtime::kReThrow, 1);
1026 }
1027
1028 __ bind(&finally_entry);
1029 {
1030 // Finally block implementation.
1031 Finally finally_block(this);
1032 EnterFinallyBlock();
1033 Visit(stmt->finally_block());
1034 ExitFinallyBlock(); // Return to the calling code.
1035 }
1036
1037 __ bind(&try_handler_setup);
1038 {
1039 // Setup try handler (stack pointer registers).
1040 TryFinally try_block(this, &finally_entry);
1041 __ PushTryHandler(IN_JAVASCRIPT, TRY_FINALLY_HANDLER);
1042 Visit(stmt->try_block());
1043 __ PopTryHandler();
1044 }
1045 // Execute the finally block on the way out.
1046 __ Call(&finally_entry);
1047}
1048
1049
1050void FullCodeGenerator::VisitDebuggerStatement(DebuggerStatement* stmt) {
1051#ifdef ENABLE_DEBUGGER_SUPPORT
1052 Comment cmnt(masm_, "[ DebuggerStatement");
1053 SetStatementPosition(stmt);
1054
ager@chromium.org5c838252010-02-19 08:53:10 +00001055 __ DebugBreak();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001056 // Ignore the return value.
1057#endif
1058}
1059
1060
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001061void FullCodeGenerator::VisitConditional(Conditional* expr) {
1062 Comment cmnt(masm_, "[ Conditional");
1063 Label true_case, false_case, done;
1064 VisitForControl(expr->condition(), &true_case, &false_case);
1065
1066 __ bind(&true_case);
1067 Visit(expr->then_expression());
1068 // If control flow falls through Visit, jump to done.
1069 if (context_ == Expression::kEffect || context_ == Expression::kValue) {
1070 __ jmp(&done);
1071 }
1072
1073 __ bind(&false_case);
1074 Visit(expr->else_expression());
1075 // If control flow falls through Visit, merge it with true case here.
1076 if (context_ == Expression::kEffect || context_ == Expression::kValue) {
1077 __ bind(&done);
1078 }
1079}
1080
1081
1082void FullCodeGenerator::VisitSlot(Slot* expr) {
1083 // Slots do not appear directly in the AST.
1084 UNREACHABLE();
1085}
1086
1087
1088void FullCodeGenerator::VisitLiteral(Literal* expr) {
1089 Comment cmnt(masm_, "[ Literal");
1090 Apply(context_, expr);
1091}
1092
1093
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001094void FullCodeGenerator::VisitFunctionLiteral(FunctionLiteral* expr) {
1095 Comment cmnt(masm_, "[ FunctionLiteral");
1096
1097 // Build the function boilerplate and instantiate it.
1098 Handle<SharedFunctionInfo> function_info =
1099 Compiler::BuildFunctionInfo(expr, script(), this);
1100 if (HasStackOverflow()) return;
1101 EmitNewClosure(function_info);
1102}
1103
1104
1105void FullCodeGenerator::VisitSharedFunctionInfoLiteral(
1106 SharedFunctionInfoLiteral* expr) {
1107 Comment cmnt(masm_, "[ SharedFunctionInfoLiteral");
1108 EmitNewClosure(expr->shared_function_info());
1109}
1110
1111
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001112void FullCodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* expr) {
1113 // Call runtime routine to allocate the catch extension object and
1114 // assign the exception value to the catch variable.
1115 Comment cmnt(masm_, "[ CatchExtensionObject");
1116 VisitForValue(expr->key(), kStack);
1117 VisitForValue(expr->value(), kStack);
1118 // Create catch extension object.
1119 __ CallRuntime(Runtime::kCreateCatchExtensionObject, 2);
1120 Apply(context_, result_register());
1121}
1122
1123
1124void FullCodeGenerator::VisitThrow(Throw* expr) {
1125 Comment cmnt(masm_, "[ Throw");
1126 VisitForValue(expr->exception(), kStack);
1127 __ CallRuntime(Runtime::kThrow, 1);
1128 // Never returns here.
1129}
1130
1131
1132int FullCodeGenerator::TryFinally::Exit(int stack_depth) {
1133 // The macros used here must preserve the result register.
1134 __ Drop(stack_depth);
1135 __ PopTryHandler();
1136 __ Call(finally_entry_);
1137 return 0;
1138}
1139
1140
1141int FullCodeGenerator::TryCatch::Exit(int stack_depth) {
1142 // The macros used here must preserve the result register.
1143 __ Drop(stack_depth);
1144 __ PopTryHandler();
1145 return 0;
1146}
1147
1148#undef __
1149
1150
1151} } // namespace v8::internal