blob: 4eb10c7431dfd3e29045296a5f4517e6bcf2d119 [file] [log] [blame]
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001// Copyright 2010 the V8 project authors. All rights reserved.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002// 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"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000032#include "debug.h"
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000033#include "full-codegen.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000034#include "liveedit.h"
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +000035#include "macro-assembler.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000036#include "prettyprinter.h"
sgjesse@chromium.org833cdd72010-02-26 10:06:16 +000037#include "scopes.h"
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000038#include "stub-cache.h"
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000039
40namespace v8 {
41namespace internal {
42
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +000043void BreakableStatementChecker::Check(Statement* stmt) {
44 Visit(stmt);
45}
46
47
48void BreakableStatementChecker::Check(Expression* expr) {
49 Visit(expr);
50}
51
52
53void BreakableStatementChecker::VisitDeclaration(Declaration* decl) {
54}
55
56
57void BreakableStatementChecker::VisitBlock(Block* stmt) {
58}
59
60
61void BreakableStatementChecker::VisitExpressionStatement(
62 ExpressionStatement* stmt) {
63 // Check if expression is breakable.
64 Visit(stmt->expression());
65}
66
67
68void BreakableStatementChecker::VisitEmptyStatement(EmptyStatement* stmt) {
69}
70
71
72void BreakableStatementChecker::VisitIfStatement(IfStatement* stmt) {
73 // If the condition is breakable the if statement is breakable.
74 Visit(stmt->condition());
75}
76
77
78void BreakableStatementChecker::VisitContinueStatement(
79 ContinueStatement* stmt) {
80}
81
82
83void BreakableStatementChecker::VisitBreakStatement(BreakStatement* stmt) {
84}
85
86
87void BreakableStatementChecker::VisitReturnStatement(ReturnStatement* stmt) {
88 // Return is breakable if the expression is.
89 Visit(stmt->expression());
90}
91
92
93void BreakableStatementChecker::VisitWithEnterStatement(
94 WithEnterStatement* stmt) {
95 Visit(stmt->expression());
96}
97
98
99void BreakableStatementChecker::VisitWithExitStatement(
100 WithExitStatement* stmt) {
101}
102
103
104void BreakableStatementChecker::VisitSwitchStatement(SwitchStatement* stmt) {
105 // Switch statements breakable if the tag expression is.
106 Visit(stmt->tag());
107}
108
109
110void BreakableStatementChecker::VisitDoWhileStatement(DoWhileStatement* stmt) {
111 // Mark do while as breakable to avoid adding a break slot in front of it.
112 is_breakable_ = true;
113}
114
115
116void BreakableStatementChecker::VisitWhileStatement(WhileStatement* stmt) {
117 // Mark while statements breakable if the condition expression is.
118 Visit(stmt->cond());
119}
120
121
122void BreakableStatementChecker::VisitForStatement(ForStatement* stmt) {
123 // Mark for statements breakable if the condition expression is.
124 if (stmt->cond() != NULL) {
125 Visit(stmt->cond());
126 }
127}
128
129
130void BreakableStatementChecker::VisitForInStatement(ForInStatement* stmt) {
131 // Mark for in statements breakable if the enumerable expression is.
132 Visit(stmt->enumerable());
133}
134
135
136void BreakableStatementChecker::VisitTryCatchStatement(
137 TryCatchStatement* stmt) {
138 // Mark try catch as breakable to avoid adding a break slot in front of it.
139 is_breakable_ = true;
140}
141
142
143void BreakableStatementChecker::VisitTryFinallyStatement(
144 TryFinallyStatement* stmt) {
145 // Mark try finally as breakable to avoid adding a break slot in front of it.
146 is_breakable_ = true;
147}
148
149
150void BreakableStatementChecker::VisitDebuggerStatement(
151 DebuggerStatement* stmt) {
152 // The debugger statement is breakable.
153 is_breakable_ = true;
154}
155
156
157void BreakableStatementChecker::VisitFunctionLiteral(FunctionLiteral* expr) {
158}
159
160
161void BreakableStatementChecker::VisitSharedFunctionInfoLiteral(
162 SharedFunctionInfoLiteral* expr) {
163}
164
165
166void BreakableStatementChecker::VisitConditional(Conditional* expr) {
167}
168
169
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000170void BreakableStatementChecker::VisitVariableProxy(VariableProxy* expr) {
171}
172
173
174void BreakableStatementChecker::VisitLiteral(Literal* expr) {
175}
176
177
178void BreakableStatementChecker::VisitRegExpLiteral(RegExpLiteral* expr) {
179}
180
181
182void BreakableStatementChecker::VisitObjectLiteral(ObjectLiteral* expr) {
183}
184
185
186void BreakableStatementChecker::VisitArrayLiteral(ArrayLiteral* expr) {
187}
188
189
190void BreakableStatementChecker::VisitCatchExtensionObject(
191 CatchExtensionObject* expr) {
192}
193
194
195void BreakableStatementChecker::VisitAssignment(Assignment* expr) {
196 // If assigning to a property (including a global property) the assignment is
197 // breakable.
198 Variable* var = expr->target()->AsVariableProxy()->AsVariable();
199 Property* prop = expr->target()->AsProperty();
200 if (prop != NULL || (var != NULL && var->is_global())) {
201 is_breakable_ = true;
202 return;
203 }
204
205 // Otherwise the assignment is breakable if the assigned value is.
206 Visit(expr->value());
207}
208
209
210void BreakableStatementChecker::VisitThrow(Throw* expr) {
211 // Throw is breakable if the expression is.
212 Visit(expr->exception());
213}
214
215
ricow@chromium.org65fae842010-08-25 15:26:24 +0000216void BreakableStatementChecker::VisitIncrementOperation(
217 IncrementOperation* expr) {
218 UNREACHABLE();
219}
220
221
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000222void BreakableStatementChecker::VisitProperty(Property* expr) {
223 // Property load is breakable.
224 is_breakable_ = true;
225}
226
227
228void BreakableStatementChecker::VisitCall(Call* expr) {
229 // Function calls both through IC and call stub are breakable.
230 is_breakable_ = true;
231}
232
233
234void BreakableStatementChecker::VisitCallNew(CallNew* expr) {
235 // Function calls through new are breakable.
236 is_breakable_ = true;
237}
238
239
240void BreakableStatementChecker::VisitCallRuntime(CallRuntime* expr) {
241}
242
243
244void BreakableStatementChecker::VisitUnaryOperation(UnaryOperation* expr) {
245 Visit(expr->expression());
246}
247
248
249void BreakableStatementChecker::VisitCountOperation(CountOperation* expr) {
250 Visit(expr->expression());
251}
252
253
254void BreakableStatementChecker::VisitBinaryOperation(BinaryOperation* expr) {
255 Visit(expr->left());
256 Visit(expr->right());
257}
258
259
ricow@chromium.org65fae842010-08-25 15:26:24 +0000260void BreakableStatementChecker::VisitCompareToNull(CompareToNull* expr) {
261 Visit(expr->expression());
262}
263
264
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000265void BreakableStatementChecker::VisitCompareOperation(CompareOperation* expr) {
266 Visit(expr->left());
267 Visit(expr->right());
268}
269
270
271void BreakableStatementChecker::VisitThisFunction(ThisFunction* expr) {
272}
273
274
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000275#define __ ACCESS_MASM(masm())
276
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000277bool FullCodeGenerator::MakeCode(CompilationInfo* info) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000278 Handle<Script> script = info->script();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000279 if (!script->IsUndefined() && !script->source()->IsUndefined()) {
280 int len = String::cast(script->source())->length();
281 Counters::total_full_codegen_source_size.Increment(len);
282 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000283 if (FLAG_trace_codegen) {
284 PrintF("Full Compiler - ");
285 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000286 CodeGenerator::MakeCodePrologue(info);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000287 const int kInitialBufferSize = 4 * KB;
288 MacroAssembler masm(NULL, kInitialBufferSize);
ager@chromium.org5c838252010-02-19 08:53:10 +0000289
290 FullCodeGenerator cgen(&masm);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000291 cgen.Generate(info);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000292 if (cgen.HasStackOverflow()) {
293 ASSERT(!Top::has_pending_exception());
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000294 return false;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000295 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000296 unsigned table_offset = cgen.EmitStackCheckTable();
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000297
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000298 Code::Flags flags = Code::ComputeFlags(Code::FUNCTION, NOT_IN_LOOP);
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000299 Handle<Code> code = CodeGenerator::MakeCodeEpilogue(&masm, flags, info);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000300 code->set_optimizable(info->IsOptimizable());
301 cgen.PopulateDeoptimizationData(code);
302 code->set_has_deoptimization_support(info->HasDeoptimizationSupport());
303 code->set_allow_osr_at_loop_nesting_level(0);
304 code->set_stack_check_table_start(table_offset);
305 CodeGenerator::PrintCode(code, info);
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000306 info->SetCode(code); // may be an empty handle.
307 return !code.is_null();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000308}
309
310
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000311unsigned FullCodeGenerator::EmitStackCheckTable() {
312 // The stack check table consists of a length (in number of entries)
313 // field, and then a sequence of entries. Each entry is a pair of AST id
314 // and code-relative pc offset.
315 masm()->Align(kIntSize);
316 masm()->RecordComment("[ Stack check table");
317 unsigned offset = masm()->pc_offset();
318 unsigned length = stack_checks_.length();
319 __ dd(length);
320 for (unsigned i = 0; i < length; ++i) {
321 __ dd(stack_checks_[i].id);
322 __ dd(stack_checks_[i].pc_and_state);
323 }
324 masm()->RecordComment("]");
325 return offset;
326}
327
328
329void FullCodeGenerator::PopulateDeoptimizationData(Handle<Code> code) {
330 // Fill in the deoptimization information.
331 ASSERT(info_->HasDeoptimizationSupport() || bailout_entries_.is_empty());
332 if (!info_->HasDeoptimizationSupport()) return;
333 int length = bailout_entries_.length();
334 Handle<DeoptimizationOutputData> data =
335 Factory::NewDeoptimizationOutputData(length, TENURED);
336 for (int i = 0; i < length; i++) {
337 data->SetAstId(i, Smi::FromInt(bailout_entries_[i].id));
338 data->SetPcAndState(i, Smi::FromInt(bailout_entries_[i].pc_and_state));
339 }
340 code->set_deoptimization_data(*data);
341}
342
343
344void FullCodeGenerator::PrepareForBailout(AstNode* node, State state) {
345 PrepareForBailoutForId(node->id(), state);
346}
347
348
349void FullCodeGenerator::RecordJSReturnSite(Call* call) {
350 // We record the offset of the function return so we can rebuild the frame
351 // if the function was inlined, i.e., this is the return address in the
352 // inlined function's frame.
353 //
354 // The state is ignored. We defensively set it to TOS_REG, which is the
355 // real state of the unoptimized code at the return site.
356 PrepareForBailoutForId(call->ReturnId(), TOS_REG);
357#ifdef DEBUG
358 // In debug builds, mark the return so we can verify that this function
359 // was called.
360 ASSERT(!call->return_is_recorded_);
361 call->return_is_recorded_ = true;
362#endif
363}
364
365
366void FullCodeGenerator::PrepareForBailoutForId(int id, State state) {
367 // There's no need to prepare this code for bailouts from already optimized
368 // code or code that can't be optimized.
369 if (!FLAG_deopt || !info_->HasDeoptimizationSupport()) return;
370 unsigned pc_and_state =
371 StateField::encode(state) | PcField::encode(masm_->pc_offset());
372 BailoutEntry entry = { id, pc_and_state };
373#ifdef DEBUG
374 // Assert that we don't have multiple bailout entries for the same node.
375 for (int i = 0; i < bailout_entries_.length(); i++) {
376 if (bailout_entries_.at(i).id == entry.id) {
377 AstPrinter printer;
378 PrintF("%s", printer.PrintProgram(info_->function()));
379 UNREACHABLE();
380 }
381 }
382#endif // DEBUG
383 bailout_entries_.Add(entry);
384}
385
386
387void FullCodeGenerator::RecordStackCheck(int ast_id) {
388 // The pc offset does not need to be encoded and packed together with a
389 // state.
390 BailoutEntry entry = { ast_id, masm_->pc_offset() };
391 stack_checks_.Add(entry);
392}
393
394
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000395int FullCodeGenerator::SlotOffset(Slot* slot) {
396 ASSERT(slot != NULL);
397 // Offset is negative because higher indexes are at lower addresses.
398 int offset = -slot->index() * kPointerSize;
399 // Adjust by a (parameter or local) base offset.
400 switch (slot->type()) {
401 case Slot::PARAMETER:
ager@chromium.org5c838252010-02-19 08:53:10 +0000402 offset += (scope()->num_parameters() + 1) * kPointerSize;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000403 break;
404 case Slot::LOCAL:
405 offset += JavaScriptFrameConstants::kLocal0Offset;
406 break;
407 case Slot::CONTEXT:
408 case Slot::LOOKUP:
409 UNREACHABLE();
410 }
411 return offset;
412}
413
414
ricow@chromium.org65fae842010-08-25 15:26:24 +0000415bool FullCodeGenerator::ShouldInlineSmiCase(Token::Value op) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000416 // Inline smi case inside loops, but not division and modulo which
417 // are too complicated and take up too much space.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000418 if (op == Token::DIV ||op == Token::MOD) return false;
419 if (FLAG_always_inline_smi_code) return true;
420 return loop_depth_ > 0;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000421}
422
423
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000424void FullCodeGenerator::EffectContext::Plug(Register reg) const {
425}
426
427
428void FullCodeGenerator::AccumulatorValueContext::Plug(Register reg) const {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000429 __ Move(result_register(), reg);
430}
431
432
433void FullCodeGenerator::StackValueContext::Plug(Register reg) const {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000434 __ push(reg);
435}
436
437
438void FullCodeGenerator::TestContext::Plug(Register reg) const {
439 // For simplicity we always test the accumulator register.
440 __ Move(result_register(), reg);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000441 codegen()->PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000442 codegen()->DoTest(true_label_, false_label_, fall_through_);
443}
444
445
446void FullCodeGenerator::EffectContext::PlugTOS() const {
447 __ Drop(1);
448}
449
450
451void FullCodeGenerator::AccumulatorValueContext::PlugTOS() const {
452 __ pop(result_register());
453}
454
455
456void FullCodeGenerator::StackValueContext::PlugTOS() const {
457}
458
459
460void FullCodeGenerator::TestContext::PlugTOS() const {
461 // For simplicity we always test the accumulator register.
462 __ pop(result_register());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000463 codegen()->PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000464 codegen()->DoTest(true_label_, false_label_, fall_through_);
465}
466
467
468void FullCodeGenerator::EffectContext::PrepareTest(
469 Label* materialize_true,
470 Label* materialize_false,
471 Label** if_true,
472 Label** if_false,
473 Label** fall_through) const {
474 // In an effect context, the true and the false case branch to the
475 // same label.
476 *if_true = *if_false = *fall_through = materialize_true;
477}
478
479
480void FullCodeGenerator::AccumulatorValueContext::PrepareTest(
481 Label* materialize_true,
482 Label* materialize_false,
483 Label** if_true,
484 Label** if_false,
485 Label** fall_through) const {
486 *if_true = *fall_through = materialize_true;
487 *if_false = materialize_false;
488}
489
490
491void FullCodeGenerator::StackValueContext::PrepareTest(
492 Label* materialize_true,
493 Label* materialize_false,
494 Label** if_true,
495 Label** if_false,
496 Label** fall_through) const {
497 *if_true = *fall_through = materialize_true;
498 *if_false = materialize_false;
499}
500
501
502void FullCodeGenerator::TestContext::PrepareTest(
503 Label* materialize_true,
504 Label* materialize_false,
505 Label** if_true,
506 Label** if_false,
507 Label** fall_through) const {
508 *if_true = true_label_;
509 *if_false = false_label_;
510 *fall_through = fall_through_;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000511}
512
513
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000514void FullCodeGenerator::VisitDeclarations(
515 ZoneList<Declaration*>* declarations) {
516 int length = declarations->length();
517 int globals = 0;
518 for (int i = 0; i < length; i++) {
519 Declaration* decl = declarations->at(i);
520 Variable* var = decl->proxy()->var();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000521 Slot* slot = var->AsSlot();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000522
523 // If it was not possible to allocate the variable at compile
524 // time, we need to "declare" it at runtime to make sure it
525 // actually exists in the local context.
526 if ((slot != NULL && slot->type() == Slot::LOOKUP) || !var->is_global()) {
527 VisitDeclaration(decl);
528 } else {
529 // Count global variables and functions for later processing
530 globals++;
531 }
532 }
533
534 // Compute array of global variable and function declarations.
535 // Do nothing in case of no declared global functions or variables.
536 if (globals > 0) {
537 Handle<FixedArray> array = Factory::NewFixedArray(2 * globals, TENURED);
538 for (int j = 0, i = 0; i < length; i++) {
539 Declaration* decl = declarations->at(i);
540 Variable* var = decl->proxy()->var();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000541 Slot* slot = var->AsSlot();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000542
543 if ((slot == NULL || slot->type() != Slot::LOOKUP) && var->is_global()) {
544 array->set(j++, *(var->name()));
545 if (decl->fun() == NULL) {
546 if (var->mode() == Variable::CONST) {
547 // In case this is const property use the hole.
548 array->set_the_hole(j++);
549 } else {
550 array->set_undefined(j++);
551 }
552 } else {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000553 Handle<SharedFunctionInfo> function =
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000554 Compiler::BuildFunctionInfo(decl->fun(), script());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000555 // Check for stack-overflow exception.
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000556 if (function.is_null()) {
557 SetStackOverflow();
558 return;
559 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000560 array->set(j++, *function);
561 }
562 }
563 }
564 // Invoke the platform-dependent code generator to do the actual
565 // declaration the global variables and functions.
566 DeclareGlobals(array);
567 }
568}
569
570
571void FullCodeGenerator::SetFunctionPosition(FunctionLiteral* fun) {
572 if (FLAG_debug_info) {
573 CodeGenerator::RecordPositions(masm_, fun->start_position());
574 }
575}
576
577
578void FullCodeGenerator::SetReturnPosition(FunctionLiteral* fun) {
579 if (FLAG_debug_info) {
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000580 CodeGenerator::RecordPositions(masm_, fun->end_position() - 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000581 }
582}
583
584
585void FullCodeGenerator::SetStatementPosition(Statement* stmt) {
586 if (FLAG_debug_info) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000587#ifdef ENABLE_DEBUGGER_SUPPORT
588 if (!Debugger::IsDebuggerActive()) {
589 CodeGenerator::RecordPositions(masm_, stmt->statement_pos());
590 } else {
591 // Check if the statement will be breakable without adding a debug break
592 // slot.
593 BreakableStatementChecker checker;
594 checker.Check(stmt);
595 // Record the statement position right here if the statement is not
596 // breakable. For breakable statements the actual recording of the
597 // position will be postponed to the breakable code (typically an IC).
598 bool position_recorded = CodeGenerator::RecordPositions(
599 masm_, stmt->statement_pos(), !checker.is_breakable());
600 // If the position recording did record a new position generate a debug
601 // break slot to make the statement breakable.
602 if (position_recorded) {
603 Debug::GenerateSlot(masm_);
604 }
605 }
606#else
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000607 CodeGenerator::RecordPositions(masm_, stmt->statement_pos());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000608#endif
609 }
610}
611
612
613void FullCodeGenerator::SetExpressionPosition(Expression* expr, int pos) {
614 if (FLAG_debug_info) {
615#ifdef ENABLE_DEBUGGER_SUPPORT
616 if (!Debugger::IsDebuggerActive()) {
617 CodeGenerator::RecordPositions(masm_, pos);
618 } else {
619 // Check if the expression will be breakable without adding a debug break
620 // slot.
621 BreakableStatementChecker checker;
622 checker.Check(expr);
623 // Record a statement position right here if the expression is not
624 // breakable. For breakable expressions the actual recording of the
625 // position will be postponed to the breakable code (typically an IC).
626 // NOTE this will record a statement position for something which might
627 // not be a statement. As stepping in the debugger will only stop at
628 // statement positions this is used for e.g. the condition expression of
629 // a do while loop.
630 bool position_recorded = CodeGenerator::RecordPositions(
631 masm_, pos, !checker.is_breakable());
632 // If the position recording did record a new position generate a debug
633 // break slot to make the statement breakable.
634 if (position_recorded) {
635 Debug::GenerateSlot(masm_);
636 }
637 }
638#else
639 CodeGenerator::RecordPositions(masm_, pos);
640#endif
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000641 }
642}
643
644
645void FullCodeGenerator::SetStatementPosition(int pos) {
646 if (FLAG_debug_info) {
647 CodeGenerator::RecordPositions(masm_, pos);
648 }
649}
650
651
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000652void FullCodeGenerator::SetSourcePosition(int pos) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000653 if (FLAG_debug_info && pos != RelocInfo::kNoPosition) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000654 masm_->positions_recorder()->RecordPosition(pos);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000655 }
656}
657
658
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000659// Lookup table for code generators for special runtime calls which are
660// generated inline.
661#define INLINE_FUNCTION_GENERATOR_ADDRESS(Name, argc, ressize) \
662 &FullCodeGenerator::Emit##Name,
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000663
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000664const FullCodeGenerator::InlineFunctionGenerator
665 FullCodeGenerator::kInlineFunctionGenerators[] = {
666 INLINE_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS)
667 INLINE_RUNTIME_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS)
668 };
669#undef INLINE_FUNCTION_GENERATOR_ADDRESS
670
671
672FullCodeGenerator::InlineFunctionGenerator
673 FullCodeGenerator::FindInlineFunctionGenerator(Runtime::FunctionId id) {
674 return kInlineFunctionGenerators[
675 static_cast<int>(id) - static_cast<int>(Runtime::kFirstInlineFunction)];
676}
677
678
679void FullCodeGenerator::EmitInlineRuntimeCall(CallRuntime* node) {
680 ZoneList<Expression*>* args = node->arguments();
681 Handle<String> name = node->name();
682 Runtime::Function* function = node->function();
683 ASSERT(function != NULL);
684 ASSERT(function->intrinsic_type == Runtime::INLINE);
685 InlineFunctionGenerator generator =
686 FindInlineFunctionGenerator(function->function_id);
687 ASSERT(generator != NULL);
688 ((*this).*(generator))(args);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000689}
690
691
692void FullCodeGenerator::VisitBinaryOperation(BinaryOperation* expr) {
693 Comment cmnt(masm_, "[ BinaryOperation");
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000694 Token::Value op = expr->op();
695 Expression* left = expr->left();
696 Expression* right = expr->right();
ricow@chromium.org65fae842010-08-25 15:26:24 +0000697
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000698 OverwriteMode mode = NO_OVERWRITE;
699 if (left->ResultOverwriteAllowed()) {
700 mode = OVERWRITE_LEFT;
701 } else if (right->ResultOverwriteAllowed()) {
702 mode = OVERWRITE_RIGHT;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000703 }
704
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000705 switch (op) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000706 case Token::COMMA:
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000707 VisitForEffect(left);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000708 if (context()->IsTest()) ForwardBailoutToChild(expr);
709 context()->HandleExpression(right);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000710 break;
711
712 case Token::OR:
713 case Token::AND:
714 EmitLogicalOperation(expr);
715 break;
716
717 case Token::ADD:
718 case Token::SUB:
719 case Token::DIV:
720 case Token::MOD:
721 case Token::MUL:
722 case Token::BIT_OR:
723 case Token::BIT_AND:
724 case Token::BIT_XOR:
725 case Token::SHL:
726 case Token::SHR:
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000727 case Token::SAR: {
728 // Figure out if either of the operands is a constant.
729 ConstantOperand constant = ShouldInlineSmiCase(op)
730 ? GetConstantOperand(op, left, right)
731 : kNoConstants;
732
733 // Load only the operands that we need to materialize.
734 if (constant == kNoConstants) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000735 VisitForStackValue(left);
736 VisitForAccumulatorValue(right);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000737 } else if (constant == kRightConstant) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000738 VisitForAccumulatorValue(left);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000739 } else {
740 ASSERT(constant == kLeftConstant);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000741 VisitForAccumulatorValue(right);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000742 }
743
ricow@chromium.org65fae842010-08-25 15:26:24 +0000744 SetSourcePosition(expr->position());
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000745 if (ShouldInlineSmiCase(op)) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000746 EmitInlineSmiBinaryOp(expr, op, mode, left, right, constant);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000747 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000748 EmitBinaryOp(op, mode);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000749 }
ricow@chromium.org65fae842010-08-25 15:26:24 +0000750 break;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000751 }
ricow@chromium.org65fae842010-08-25 15:26:24 +0000752
753 default:
754 UNREACHABLE();
755 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000756}
757
758
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000759void FullCodeGenerator::EmitLogicalOperation(BinaryOperation* expr) {
760 Label eval_right, done;
761
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000762 context()->EmitLogicalLeft(expr, &eval_right, &done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000763
764 __ bind(&eval_right);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000765 if (context()->IsTest()) ForwardBailoutToChild(expr);
766 context()->HandleExpression(expr->right());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000767
768 __ bind(&done);
769}
770
771
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000772void FullCodeGenerator::EffectContext::EmitLogicalLeft(BinaryOperation* expr,
773 Label* eval_right,
774 Label* done) const {
775 if (expr->op() == Token::OR) {
776 codegen()->VisitForControl(expr->left(), done, eval_right, eval_right);
777 } else {
778 ASSERT(expr->op() == Token::AND);
779 codegen()->VisitForControl(expr->left(), eval_right, done, eval_right);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000780 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000781}
ricow@chromium.org65fae842010-08-25 15:26:24 +0000782
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000783
784void FullCodeGenerator::AccumulatorValueContext::EmitLogicalLeft(
785 BinaryOperation* expr,
786 Label* eval_right,
787 Label* done) const {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000788 HandleExpression(expr->left());
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000789 // We want the value in the accumulator for the test, and on the stack in case
790 // we need it.
791 __ push(result_register());
792 Label discard, restore;
793 if (expr->op() == Token::OR) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000794 codegen()->PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000795 codegen()->DoTest(&restore, &discard, &restore);
796 } else {
797 ASSERT(expr->op() == Token::AND);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000798 codegen()->PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000799 codegen()->DoTest(&discard, &restore, &restore);
800 }
801 __ bind(&restore);
802 __ pop(result_register());
803 __ jmp(done);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000804 __ bind(&discard);
805 __ Drop(1);
806}
807
808
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000809void FullCodeGenerator::StackValueContext::EmitLogicalLeft(
810 BinaryOperation* expr,
811 Label* eval_right,
812 Label* done) const {
813 codegen()->VisitForAccumulatorValue(expr->left());
814 // We want the value in the accumulator for the test, and on the stack in case
815 // we need it.
816 __ push(result_register());
817 Label discard;
818 if (expr->op() == Token::OR) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000819 codegen()->PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000820 codegen()->DoTest(done, &discard, &discard);
821 } else {
822 ASSERT(expr->op() == Token::AND);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000823 codegen()->PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000824 codegen()->DoTest(&discard, done, &discard);
825 }
826 __ bind(&discard);
827 __ Drop(1);
828}
829
830
831void FullCodeGenerator::TestContext::EmitLogicalLeft(BinaryOperation* expr,
832 Label* eval_right,
833 Label* done) const {
834 if (expr->op() == Token::OR) {
835 codegen()->VisitForControl(expr->left(),
836 true_label_, eval_right, eval_right);
837 } else {
838 ASSERT(expr->op() == Token::AND);
839 codegen()->VisitForControl(expr->left(),
840 eval_right, false_label_, eval_right);
841 }
842}
843
844
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000845void FullCodeGenerator::ForwardBailoutToChild(Expression* expr) {
846 if (!info_->HasDeoptimizationSupport()) return;
847 ASSERT(context()->IsTest());
848 ASSERT(expr == forward_bailout_stack_->expr());
849 forward_bailout_pending_ = forward_bailout_stack_;
850}
851
852
853void FullCodeGenerator::EffectContext::HandleExpression(
854 Expression* expr) const {
855 codegen()->HandleInNonTestContext(expr, NO_REGISTERS);
856}
857
858
859void FullCodeGenerator::AccumulatorValueContext::HandleExpression(
860 Expression* expr) const {
861 codegen()->HandleInNonTestContext(expr, TOS_REG);
862}
863
864
865void FullCodeGenerator::StackValueContext::HandleExpression(
866 Expression* expr) const {
867 codegen()->HandleInNonTestContext(expr, NO_REGISTERS);
868}
869
870
871void FullCodeGenerator::TestContext::HandleExpression(Expression* expr) const {
872 codegen()->VisitInTestContext(expr);
873}
874
875
876void FullCodeGenerator::HandleInNonTestContext(Expression* expr, State state) {
877 ASSERT(forward_bailout_pending_ == NULL);
878 AstVisitor::Visit(expr);
879 PrepareForBailout(expr, state);
880 // Forwarding bailouts to children is a one shot operation. It
881 // should have been processed at this point.
882 ASSERT(forward_bailout_pending_ == NULL);
883}
884
885
886void FullCodeGenerator::VisitInTestContext(Expression* expr) {
887 ForwardBailoutStack stack(expr, forward_bailout_pending_);
888 ForwardBailoutStack* saved = forward_bailout_stack_;
889 forward_bailout_pending_ = NULL;
890 forward_bailout_stack_ = &stack;
891 AstVisitor::Visit(expr);
892 forward_bailout_stack_ = saved;
893}
894
895
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000896void FullCodeGenerator::VisitBlock(Block* stmt) {
897 Comment cmnt(masm_, "[ Block");
898 Breakable nested_statement(this, stmt);
899 SetStatementPosition(stmt);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000900
901 PrepareForBailoutForId(stmt->EntryId(), TOS_REG);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000902 VisitStatements(stmt->statements());
903 __ bind(nested_statement.break_target());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000904 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000905}
906
907
908void FullCodeGenerator::VisitExpressionStatement(ExpressionStatement* stmt) {
909 Comment cmnt(masm_, "[ ExpressionStatement");
910 SetStatementPosition(stmt);
911 VisitForEffect(stmt->expression());
912}
913
914
915void FullCodeGenerator::VisitEmptyStatement(EmptyStatement* stmt) {
916 Comment cmnt(masm_, "[ EmptyStatement");
917 SetStatementPosition(stmt);
918}
919
920
921void FullCodeGenerator::VisitIfStatement(IfStatement* stmt) {
922 Comment cmnt(masm_, "[ IfStatement");
923 SetStatementPosition(stmt);
924 Label then_part, else_part, done;
925
ricow@chromium.org65fae842010-08-25 15:26:24 +0000926 if (stmt->HasElseStatement()) {
927 VisitForControl(stmt->condition(), &then_part, &else_part, &then_part);
928 __ bind(&then_part);
929 Visit(stmt->then_statement());
930 __ jmp(&done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000931
ricow@chromium.org65fae842010-08-25 15:26:24 +0000932 __ bind(&else_part);
933 Visit(stmt->else_statement());
934 } else {
935 VisitForControl(stmt->condition(), &then_part, &done, &then_part);
936 __ bind(&then_part);
937 Visit(stmt->then_statement());
938 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000939 __ bind(&done);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000940 PrepareForBailoutForId(stmt->id(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000941}
942
943
944void FullCodeGenerator::VisitContinueStatement(ContinueStatement* stmt) {
945 Comment cmnt(masm_, "[ ContinueStatement");
946 SetStatementPosition(stmt);
947 NestedStatement* current = nesting_stack_;
948 int stack_depth = 0;
949 while (!current->IsContinueTarget(stmt->target())) {
950 stack_depth = current->Exit(stack_depth);
951 current = current->outer();
952 }
953 __ Drop(stack_depth);
954
955 Iteration* loop = current->AsIteration();
956 __ jmp(loop->continue_target());
957}
958
959
960void FullCodeGenerator::VisitBreakStatement(BreakStatement* stmt) {
961 Comment cmnt(masm_, "[ BreakStatement");
962 SetStatementPosition(stmt);
963 NestedStatement* current = nesting_stack_;
964 int stack_depth = 0;
965 while (!current->IsBreakTarget(stmt->target())) {
966 stack_depth = current->Exit(stack_depth);
967 current = current->outer();
968 }
969 __ Drop(stack_depth);
970
971 Breakable* target = current->AsBreakable();
972 __ jmp(target->break_target());
973}
974
975
976void FullCodeGenerator::VisitReturnStatement(ReturnStatement* stmt) {
977 Comment cmnt(masm_, "[ ReturnStatement");
978 SetStatementPosition(stmt);
979 Expression* expr = stmt->expression();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000980 VisitForAccumulatorValue(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000981
982 // Exit all nested statements.
983 NestedStatement* current = nesting_stack_;
984 int stack_depth = 0;
985 while (current != NULL) {
986 stack_depth = current->Exit(stack_depth);
987 current = current->outer();
988 }
989 __ Drop(stack_depth);
990
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000991 EmitReturnSequence();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000992}
993
994
995void FullCodeGenerator::VisitWithEnterStatement(WithEnterStatement* stmt) {
996 Comment cmnt(masm_, "[ WithEnterStatement");
997 SetStatementPosition(stmt);
998
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000999 VisitForStackValue(stmt->expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001000 if (stmt->is_catch_block()) {
1001 __ CallRuntime(Runtime::kPushCatchContext, 1);
1002 } else {
1003 __ CallRuntime(Runtime::kPushContext, 1);
1004 }
1005 // Both runtime calls return the new context in both the context and the
1006 // result registers.
1007
1008 // Update local stack frame context field.
1009 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
1010}
1011
1012
1013void FullCodeGenerator::VisitWithExitStatement(WithExitStatement* stmt) {
1014 Comment cmnt(masm_, "[ WithExitStatement");
1015 SetStatementPosition(stmt);
1016
1017 // Pop context.
1018 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1019 // Update local stack frame context field.
1020 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
1021}
1022
1023
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001024void FullCodeGenerator::VisitDoWhileStatement(DoWhileStatement* stmt) {
1025 Comment cmnt(masm_, "[ DoWhileStatement");
1026 SetStatementPosition(stmt);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001027 Label body, stack_check;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001028
1029 Iteration loop_statement(this, stmt);
1030 increment_loop_depth();
1031
1032 __ bind(&body);
1033 Visit(stmt->body());
1034
ricow@chromium.org65fae842010-08-25 15:26:24 +00001035 // Record the position of the do while condition and make sure it is
1036 // possible to break on the condition.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001037 __ bind(loop_statement.continue_target());
1038 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001039 SetExpressionPosition(stmt->cond(), stmt->condition_position());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001040 VisitForControl(stmt->cond(),
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001041 &stack_check,
ricow@chromium.org65fae842010-08-25 15:26:24 +00001042 loop_statement.break_target(),
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001043 &stack_check);
1044
1045 // Check stack before looping.
1046 __ bind(&stack_check);
1047 EmitStackCheck(stmt);
1048 __ jmp(&body);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001049
ricow@chromium.org65fae842010-08-25 15:26:24 +00001050 __ bind(loop_statement.break_target());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001051 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001052 decrement_loop_depth();
1053}
1054
1055
1056void FullCodeGenerator::VisitWhileStatement(WhileStatement* stmt) {
1057 Comment cmnt(masm_, "[ WhileStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001058 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001059
1060 Iteration loop_statement(this, stmt);
1061 increment_loop_depth();
1062
1063 // Emit the test at the bottom of the loop.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001064 __ jmp(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001065
1066 __ bind(&body);
1067 Visit(stmt->body());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001068
1069 // Emit the statement position here as this is where the while
1070 // statement code starts.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001071 __ bind(loop_statement.continue_target());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001072 SetStatementPosition(stmt);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001073
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001074 // Check stack before looping.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001075 EmitStackCheck(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001076
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001077 __ bind(&test);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001078 VisitForControl(stmt->cond(),
1079 &body,
1080 loop_statement.break_target(),
1081 loop_statement.break_target());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001082
1083 __ bind(loop_statement.break_target());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001084 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001085 decrement_loop_depth();
1086}
1087
1088
1089void FullCodeGenerator::VisitForStatement(ForStatement* stmt) {
1090 Comment cmnt(masm_, "[ ForStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001091 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001092
1093 Iteration loop_statement(this, stmt);
1094 if (stmt->init() != NULL) {
1095 Visit(stmt->init());
1096 }
1097
1098 increment_loop_depth();
1099 // Emit the test at the bottom of the loop (even if empty).
1100 __ jmp(&test);
1101
1102 __ bind(&body);
1103 Visit(stmt->body());
1104
1105 __ bind(loop_statement.continue_target());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001106 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001107
1108 SetStatementPosition(stmt);
1109 if (stmt->next() != NULL) {
1110 Visit(stmt->next());
1111 }
1112
ricow@chromium.org65fae842010-08-25 15:26:24 +00001113 // Emit the statement position here as this is where the for
1114 // statement code starts.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001115 SetStatementPosition(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001116
1117 // Check stack before looping.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001118 EmitStackCheck(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001119
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001120 __ bind(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001121 if (stmt->cond() != NULL) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00001122 VisitForControl(stmt->cond(),
1123 &body,
1124 loop_statement.break_target(),
1125 loop_statement.break_target());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001126 } else {
1127 __ jmp(&body);
1128 }
1129
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001130 __ bind(loop_statement.break_target());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001131 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001132 decrement_loop_depth();
1133}
1134
1135
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001136void FullCodeGenerator::VisitTryCatchStatement(TryCatchStatement* stmt) {
1137 Comment cmnt(masm_, "[ TryCatchStatement");
1138 SetStatementPosition(stmt);
1139 // The try block adds a handler to the exception handler chain
1140 // before entering, and removes it again when exiting normally.
1141 // If an exception is thrown during execution of the try block,
1142 // control is passed to the handler, which also consumes the handler.
1143 // At this point, the exception is in a register, and store it in
1144 // the temporary local variable (prints as ".catch-var") before
1145 // executing the catch block. The catch block has been rewritten
1146 // to introduce a new scope to bind the catch variable and to remove
1147 // that scope again afterwards.
1148
1149 Label try_handler_setup, catch_entry, done;
1150 __ Call(&try_handler_setup);
1151 // Try handler code, exception in result register.
1152
1153 // Store exception in local .catch variable before executing catch block.
1154 {
1155 // The catch variable is *always* a variable proxy for a local variable.
1156 Variable* catch_var = stmt->catch_var()->AsVariableProxy()->AsVariable();
1157 ASSERT_NOT_NULL(catch_var);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001158 Slot* variable_slot = catch_var->AsSlot();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001159 ASSERT_NOT_NULL(variable_slot);
1160 ASSERT_EQ(Slot::LOCAL, variable_slot->type());
1161 StoreToFrameField(SlotOffset(variable_slot), result_register());
1162 }
1163
1164 Visit(stmt->catch_block());
1165 __ jmp(&done);
1166
1167 // Try block code. Sets up the exception handler chain.
1168 __ bind(&try_handler_setup);
1169 {
1170 TryCatch try_block(this, &catch_entry);
1171 __ PushTryHandler(IN_JAVASCRIPT, TRY_CATCH_HANDLER);
1172 Visit(stmt->try_block());
1173 __ PopTryHandler();
1174 }
1175 __ bind(&done);
1176}
1177
1178
1179void FullCodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* stmt) {
1180 Comment cmnt(masm_, "[ TryFinallyStatement");
1181 SetStatementPosition(stmt);
1182 // Try finally is compiled by setting up a try-handler on the stack while
1183 // executing the try body, and removing it again afterwards.
1184 //
1185 // The try-finally construct can enter the finally block in three ways:
1186 // 1. By exiting the try-block normally. This removes the try-handler and
1187 // calls the finally block code before continuing.
1188 // 2. By exiting the try-block with a function-local control flow transfer
1189 // (break/continue/return). The site of the, e.g., break removes the
1190 // try handler and calls the finally block code before continuing
1191 // its outward control transfer.
1192 // 3. by exiting the try-block with a thrown exception.
1193 // This can happen in nested function calls. It traverses the try-handler
1194 // chain and consumes the try-handler entry before jumping to the
1195 // handler code. The handler code then calls the finally-block before
1196 // rethrowing the exception.
1197 //
1198 // The finally block must assume a return address on top of the stack
1199 // (or in the link register on ARM chips) and a value (return value or
1200 // exception) in the result register (rax/eax/r0), both of which must
1201 // be preserved. The return address isn't GC-safe, so it should be
1202 // cooked before GC.
1203 Label finally_entry;
1204 Label try_handler_setup;
1205
1206 // Setup the try-handler chain. Use a call to
1207 // Jump to try-handler setup and try-block code. Use call to put try-handler
1208 // address on stack.
1209 __ Call(&try_handler_setup);
1210 // Try handler code. Return address of call is pushed on handler stack.
1211 {
1212 // This code is only executed during stack-handler traversal when an
1213 // exception is thrown. The execption is in the result register, which
1214 // is retained by the finally block.
1215 // Call the finally block and then rethrow the exception.
1216 __ Call(&finally_entry);
1217 __ push(result_register());
1218 __ CallRuntime(Runtime::kReThrow, 1);
1219 }
1220
1221 __ bind(&finally_entry);
1222 {
1223 // Finally block implementation.
1224 Finally finally_block(this);
1225 EnterFinallyBlock();
1226 Visit(stmt->finally_block());
1227 ExitFinallyBlock(); // Return to the calling code.
1228 }
1229
1230 __ bind(&try_handler_setup);
1231 {
1232 // Setup try handler (stack pointer registers).
1233 TryFinally try_block(this, &finally_entry);
1234 __ PushTryHandler(IN_JAVASCRIPT, TRY_FINALLY_HANDLER);
1235 Visit(stmt->try_block());
1236 __ PopTryHandler();
1237 }
1238 // Execute the finally block on the way out.
1239 __ Call(&finally_entry);
1240}
1241
1242
1243void FullCodeGenerator::VisitDebuggerStatement(DebuggerStatement* stmt) {
1244#ifdef ENABLE_DEBUGGER_SUPPORT
1245 Comment cmnt(masm_, "[ DebuggerStatement");
1246 SetStatementPosition(stmt);
1247
ager@chromium.org5c838252010-02-19 08:53:10 +00001248 __ DebugBreak();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001249 // Ignore the return value.
1250#endif
1251}
1252
1253
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001254void FullCodeGenerator::VisitConditional(Conditional* expr) {
1255 Comment cmnt(masm_, "[ Conditional");
1256 Label true_case, false_case, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001257 VisitForControl(expr->condition(), &true_case, &false_case, &true_case);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001258
1259 __ bind(&true_case);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001260 SetExpressionPosition(expr->then_expression(),
1261 expr->then_expression_position());
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001262 if (context()->IsTest()) {
1263 const TestContext* for_test = TestContext::cast(context());
1264 VisitForControl(expr->then_expression(),
1265 for_test->true_label(),
1266 for_test->false_label(),
1267 NULL);
1268 } else {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001269 context()->HandleExpression(expr->then_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001270 __ jmp(&done);
1271 }
1272
1273 __ bind(&false_case);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001274 if (context()->IsTest()) ForwardBailoutToChild(expr);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001275 SetExpressionPosition(expr->else_expression(),
1276 expr->else_expression_position());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001277 context()->HandleExpression(expr->else_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001278 // If control flow falls through Visit, merge it with true case here.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001279 if (!context()->IsTest()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001280 __ bind(&done);
1281 }
1282}
1283
1284
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001285void FullCodeGenerator::VisitLiteral(Literal* expr) {
1286 Comment cmnt(masm_, "[ Literal");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001287 context()->Plug(expr->handle());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001288}
1289
1290
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001291void FullCodeGenerator::VisitFunctionLiteral(FunctionLiteral* expr) {
1292 Comment cmnt(masm_, "[ FunctionLiteral");
1293
1294 // Build the function boilerplate and instantiate it.
1295 Handle<SharedFunctionInfo> function_info =
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001296 Compiler::BuildFunctionInfo(expr, script());
1297 if (function_info.is_null()) {
1298 SetStackOverflow();
1299 return;
1300 }
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001301 EmitNewClosure(function_info, expr->pretenure());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001302}
1303
1304
1305void FullCodeGenerator::VisitSharedFunctionInfoLiteral(
1306 SharedFunctionInfoLiteral* expr) {
1307 Comment cmnt(masm_, "[ SharedFunctionInfoLiteral");
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001308 EmitNewClosure(expr->shared_function_info(), false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001309}
1310
1311
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001312void FullCodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* expr) {
1313 // Call runtime routine to allocate the catch extension object and
1314 // assign the exception value to the catch variable.
1315 Comment cmnt(masm_, "[ CatchExtensionObject");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001316 VisitForStackValue(expr->key());
1317 VisitForStackValue(expr->value());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001318 // Create catch extension object.
1319 __ CallRuntime(Runtime::kCreateCatchExtensionObject, 2);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001320 context()->Plug(result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001321}
1322
1323
1324void FullCodeGenerator::VisitThrow(Throw* expr) {
1325 Comment cmnt(masm_, "[ Throw");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001326 VisitForStackValue(expr->exception());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001327 __ CallRuntime(Runtime::kThrow, 1);
1328 // Never returns here.
1329}
1330
1331
ricow@chromium.org65fae842010-08-25 15:26:24 +00001332void FullCodeGenerator::VisitIncrementOperation(IncrementOperation* expr) {
1333 UNREACHABLE();
1334}
1335
1336
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001337int FullCodeGenerator::TryFinally::Exit(int stack_depth) {
1338 // The macros used here must preserve the result register.
1339 __ Drop(stack_depth);
1340 __ PopTryHandler();
1341 __ Call(finally_entry_);
1342 return 0;
1343}
1344
1345
1346int FullCodeGenerator::TryCatch::Exit(int stack_depth) {
1347 // The macros used here must preserve the result register.
1348 __ Drop(stack_depth);
1349 __ PopTryHandler();
1350 return 0;
1351}
1352
ricow@chromium.org65fae842010-08-25 15:26:24 +00001353
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001354#undef __
1355
1356
1357} } // namespace v8::internal