blob: 96307a302d5ccfc81671d6590c9d0dec861f8264 [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
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000764 PrepareForBailoutForId(expr->RightId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000765 __ bind(&eval_right);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000766 if (context()->IsTest()) ForwardBailoutToChild(expr);
767 context()->HandleExpression(expr->right());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000768
769 __ bind(&done);
770}
771
772
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000773void FullCodeGenerator::EffectContext::EmitLogicalLeft(BinaryOperation* expr,
774 Label* eval_right,
775 Label* done) const {
776 if (expr->op() == Token::OR) {
777 codegen()->VisitForControl(expr->left(), done, eval_right, eval_right);
778 } else {
779 ASSERT(expr->op() == Token::AND);
780 codegen()->VisitForControl(expr->left(), eval_right, done, eval_right);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000781 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000782}
ricow@chromium.org65fae842010-08-25 15:26:24 +0000783
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000784
785void FullCodeGenerator::AccumulatorValueContext::EmitLogicalLeft(
786 BinaryOperation* expr,
787 Label* eval_right,
788 Label* done) const {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000789 HandleExpression(expr->left());
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000790 // We want the value in the accumulator for the test, and on the stack in case
791 // we need it.
792 __ push(result_register());
793 Label discard, restore;
794 if (expr->op() == Token::OR) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000795 codegen()->PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000796 codegen()->DoTest(&restore, &discard, &restore);
797 } else {
798 ASSERT(expr->op() == Token::AND);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000799 codegen()->PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000800 codegen()->DoTest(&discard, &restore, &restore);
801 }
802 __ bind(&restore);
803 __ pop(result_register());
804 __ jmp(done);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000805 __ bind(&discard);
806 __ Drop(1);
807}
808
809
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000810void FullCodeGenerator::StackValueContext::EmitLogicalLeft(
811 BinaryOperation* expr,
812 Label* eval_right,
813 Label* done) const {
814 codegen()->VisitForAccumulatorValue(expr->left());
815 // We want the value in the accumulator for the test, and on the stack in case
816 // we need it.
817 __ push(result_register());
818 Label discard;
819 if (expr->op() == Token::OR) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000820 codegen()->PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000821 codegen()->DoTest(done, &discard, &discard);
822 } else {
823 ASSERT(expr->op() == Token::AND);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000824 codegen()->PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000825 codegen()->DoTest(&discard, done, &discard);
826 }
827 __ bind(&discard);
828 __ Drop(1);
829}
830
831
832void FullCodeGenerator::TestContext::EmitLogicalLeft(BinaryOperation* expr,
833 Label* eval_right,
834 Label* done) const {
835 if (expr->op() == Token::OR) {
836 codegen()->VisitForControl(expr->left(),
837 true_label_, eval_right, eval_right);
838 } else {
839 ASSERT(expr->op() == Token::AND);
840 codegen()->VisitForControl(expr->left(),
841 eval_right, false_label_, eval_right);
842 }
843}
844
845
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000846void FullCodeGenerator::ForwardBailoutToChild(Expression* expr) {
847 if (!info_->HasDeoptimizationSupport()) return;
848 ASSERT(context()->IsTest());
849 ASSERT(expr == forward_bailout_stack_->expr());
850 forward_bailout_pending_ = forward_bailout_stack_;
851}
852
853
854void FullCodeGenerator::EffectContext::HandleExpression(
855 Expression* expr) const {
856 codegen()->HandleInNonTestContext(expr, NO_REGISTERS);
857}
858
859
860void FullCodeGenerator::AccumulatorValueContext::HandleExpression(
861 Expression* expr) const {
862 codegen()->HandleInNonTestContext(expr, TOS_REG);
863}
864
865
866void FullCodeGenerator::StackValueContext::HandleExpression(
867 Expression* expr) const {
868 codegen()->HandleInNonTestContext(expr, NO_REGISTERS);
869}
870
871
872void FullCodeGenerator::TestContext::HandleExpression(Expression* expr) const {
873 codegen()->VisitInTestContext(expr);
874}
875
876
877void FullCodeGenerator::HandleInNonTestContext(Expression* expr, State state) {
878 ASSERT(forward_bailout_pending_ == NULL);
879 AstVisitor::Visit(expr);
880 PrepareForBailout(expr, state);
881 // Forwarding bailouts to children is a one shot operation. It
882 // should have been processed at this point.
883 ASSERT(forward_bailout_pending_ == NULL);
884}
885
886
887void FullCodeGenerator::VisitInTestContext(Expression* expr) {
888 ForwardBailoutStack stack(expr, forward_bailout_pending_);
889 ForwardBailoutStack* saved = forward_bailout_stack_;
890 forward_bailout_pending_ = NULL;
891 forward_bailout_stack_ = &stack;
892 AstVisitor::Visit(expr);
893 forward_bailout_stack_ = saved;
894}
895
896
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000897void FullCodeGenerator::VisitBlock(Block* stmt) {
898 Comment cmnt(masm_, "[ Block");
899 Breakable nested_statement(this, stmt);
900 SetStatementPosition(stmt);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000901
902 PrepareForBailoutForId(stmt->EntryId(), TOS_REG);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000903 VisitStatements(stmt->statements());
904 __ bind(nested_statement.break_target());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000905 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000906}
907
908
909void FullCodeGenerator::VisitExpressionStatement(ExpressionStatement* stmt) {
910 Comment cmnt(masm_, "[ ExpressionStatement");
911 SetStatementPosition(stmt);
912 VisitForEffect(stmt->expression());
913}
914
915
916void FullCodeGenerator::VisitEmptyStatement(EmptyStatement* stmt) {
917 Comment cmnt(masm_, "[ EmptyStatement");
918 SetStatementPosition(stmt);
919}
920
921
922void FullCodeGenerator::VisitIfStatement(IfStatement* stmt) {
923 Comment cmnt(masm_, "[ IfStatement");
924 SetStatementPosition(stmt);
925 Label then_part, else_part, done;
926
ricow@chromium.org65fae842010-08-25 15:26:24 +0000927 if (stmt->HasElseStatement()) {
928 VisitForControl(stmt->condition(), &then_part, &else_part, &then_part);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000929 PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000930 __ bind(&then_part);
931 Visit(stmt->then_statement());
932 __ jmp(&done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000933
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000934 PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000935 __ bind(&else_part);
936 Visit(stmt->else_statement());
937 } else {
938 VisitForControl(stmt->condition(), &then_part, &done, &then_part);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000939 PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000940 __ bind(&then_part);
941 Visit(stmt->then_statement());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000942
943 PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000944 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000945 __ bind(&done);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000946 PrepareForBailoutForId(stmt->id(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000947}
948
949
950void FullCodeGenerator::VisitContinueStatement(ContinueStatement* stmt) {
951 Comment cmnt(masm_, "[ ContinueStatement");
952 SetStatementPosition(stmt);
953 NestedStatement* current = nesting_stack_;
954 int stack_depth = 0;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000955 // When continuing, we clobber the unpredictable value in the accumulator
956 // with one that's safe for GC. If we hit an exit from the try block of
957 // try...finally on our way out, we will unconditionally preserve the
958 // accumulator on the stack.
959 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000960 while (!current->IsContinueTarget(stmt->target())) {
961 stack_depth = current->Exit(stack_depth);
962 current = current->outer();
963 }
964 __ Drop(stack_depth);
965
966 Iteration* loop = current->AsIteration();
967 __ jmp(loop->continue_target());
968}
969
970
971void FullCodeGenerator::VisitBreakStatement(BreakStatement* stmt) {
972 Comment cmnt(masm_, "[ BreakStatement");
973 SetStatementPosition(stmt);
974 NestedStatement* current = nesting_stack_;
975 int stack_depth = 0;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000976 // When breaking, we clobber the unpredictable value in the accumulator
977 // with one that's safe for GC. If we hit an exit from the try block of
978 // try...finally on our way out, we will unconditionally preserve the
979 // accumulator on the stack.
980 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000981 while (!current->IsBreakTarget(stmt->target())) {
982 stack_depth = current->Exit(stack_depth);
983 current = current->outer();
984 }
985 __ Drop(stack_depth);
986
987 Breakable* target = current->AsBreakable();
988 __ jmp(target->break_target());
989}
990
991
992void FullCodeGenerator::VisitReturnStatement(ReturnStatement* stmt) {
993 Comment cmnt(masm_, "[ ReturnStatement");
994 SetStatementPosition(stmt);
995 Expression* expr = stmt->expression();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000996 VisitForAccumulatorValue(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000997
998 // Exit all nested statements.
999 NestedStatement* current = nesting_stack_;
1000 int stack_depth = 0;
1001 while (current != NULL) {
1002 stack_depth = current->Exit(stack_depth);
1003 current = current->outer();
1004 }
1005 __ Drop(stack_depth);
1006
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001007 EmitReturnSequence();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001008}
1009
1010
1011void FullCodeGenerator::VisitWithEnterStatement(WithEnterStatement* stmt) {
1012 Comment cmnt(masm_, "[ WithEnterStatement");
1013 SetStatementPosition(stmt);
1014
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001015 VisitForStackValue(stmt->expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001016 if (stmt->is_catch_block()) {
1017 __ CallRuntime(Runtime::kPushCatchContext, 1);
1018 } else {
1019 __ CallRuntime(Runtime::kPushContext, 1);
1020 }
1021 // Both runtime calls return the new context in both the context and the
1022 // result registers.
1023
1024 // Update local stack frame context field.
1025 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
1026}
1027
1028
1029void FullCodeGenerator::VisitWithExitStatement(WithExitStatement* stmt) {
1030 Comment cmnt(masm_, "[ WithExitStatement");
1031 SetStatementPosition(stmt);
1032
1033 // Pop context.
1034 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1035 // Update local stack frame context field.
1036 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
1037}
1038
1039
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001040void FullCodeGenerator::VisitDoWhileStatement(DoWhileStatement* stmt) {
1041 Comment cmnt(masm_, "[ DoWhileStatement");
1042 SetStatementPosition(stmt);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001043 Label body, stack_check;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001044
1045 Iteration loop_statement(this, stmt);
1046 increment_loop_depth();
1047
1048 __ bind(&body);
1049 Visit(stmt->body());
1050
ricow@chromium.org65fae842010-08-25 15:26:24 +00001051 // Record the position of the do while condition and make sure it is
1052 // possible to break on the condition.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001053 __ bind(loop_statement.continue_target());
1054 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001055 SetExpressionPosition(stmt->cond(), stmt->condition_position());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001056 VisitForControl(stmt->cond(),
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001057 &stack_check,
ricow@chromium.org65fae842010-08-25 15:26:24 +00001058 loop_statement.break_target(),
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001059 &stack_check);
1060
1061 // Check stack before looping.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001062 PrepareForBailoutForId(stmt->BackEdgeId(), NO_REGISTERS);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001063 __ bind(&stack_check);
1064 EmitStackCheck(stmt);
1065 __ jmp(&body);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001066
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001067 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001068 __ bind(loop_statement.break_target());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001069 decrement_loop_depth();
1070}
1071
1072
1073void FullCodeGenerator::VisitWhileStatement(WhileStatement* stmt) {
1074 Comment cmnt(masm_, "[ WhileStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001075 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001076
1077 Iteration loop_statement(this, stmt);
1078 increment_loop_depth();
1079
1080 // Emit the test at the bottom of the loop.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001081 __ jmp(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001082
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001083 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001084 __ bind(&body);
1085 Visit(stmt->body());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001086
1087 // Emit the statement position here as this is where the while
1088 // statement code starts.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001089 __ bind(loop_statement.continue_target());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001090 SetStatementPosition(stmt);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001091
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001092 // Check stack before looping.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001093 EmitStackCheck(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001094
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001095 __ bind(&test);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001096 VisitForControl(stmt->cond(),
1097 &body,
1098 loop_statement.break_target(),
1099 loop_statement.break_target());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001100
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001101 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001102 __ bind(loop_statement.break_target());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001103 decrement_loop_depth();
1104}
1105
1106
1107void FullCodeGenerator::VisitForStatement(ForStatement* stmt) {
1108 Comment cmnt(masm_, "[ ForStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001109 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001110
1111 Iteration loop_statement(this, stmt);
1112 if (stmt->init() != NULL) {
1113 Visit(stmt->init());
1114 }
1115
1116 increment_loop_depth();
1117 // Emit the test at the bottom of the loop (even if empty).
1118 __ jmp(&test);
1119
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001120 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001121 __ bind(&body);
1122 Visit(stmt->body());
1123
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001124 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001125 __ bind(loop_statement.continue_target());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001126 SetStatementPosition(stmt);
1127 if (stmt->next() != NULL) {
1128 Visit(stmt->next());
1129 }
1130
ricow@chromium.org65fae842010-08-25 15:26:24 +00001131 // Emit the statement position here as this is where the for
1132 // statement code starts.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001133 SetStatementPosition(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001134
1135 // Check stack before looping.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001136 EmitStackCheck(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001137
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001138 __ bind(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001139 if (stmt->cond() != NULL) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00001140 VisitForControl(stmt->cond(),
1141 &body,
1142 loop_statement.break_target(),
1143 loop_statement.break_target());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001144 } else {
1145 __ jmp(&body);
1146 }
1147
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001148 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001149 __ bind(loop_statement.break_target());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001150 decrement_loop_depth();
1151}
1152
1153
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001154void FullCodeGenerator::VisitTryCatchStatement(TryCatchStatement* stmt) {
1155 Comment cmnt(masm_, "[ TryCatchStatement");
1156 SetStatementPosition(stmt);
1157 // The try block adds a handler to the exception handler chain
1158 // before entering, and removes it again when exiting normally.
1159 // If an exception is thrown during execution of the try block,
1160 // control is passed to the handler, which also consumes the handler.
1161 // At this point, the exception is in a register, and store it in
1162 // the temporary local variable (prints as ".catch-var") before
1163 // executing the catch block. The catch block has been rewritten
1164 // to introduce a new scope to bind the catch variable and to remove
1165 // that scope again afterwards.
1166
1167 Label try_handler_setup, catch_entry, done;
1168 __ Call(&try_handler_setup);
1169 // Try handler code, exception in result register.
1170
1171 // Store exception in local .catch variable before executing catch block.
1172 {
1173 // The catch variable is *always* a variable proxy for a local variable.
1174 Variable* catch_var = stmt->catch_var()->AsVariableProxy()->AsVariable();
1175 ASSERT_NOT_NULL(catch_var);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001176 Slot* variable_slot = catch_var->AsSlot();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001177 ASSERT_NOT_NULL(variable_slot);
1178 ASSERT_EQ(Slot::LOCAL, variable_slot->type());
1179 StoreToFrameField(SlotOffset(variable_slot), result_register());
1180 }
1181
1182 Visit(stmt->catch_block());
1183 __ jmp(&done);
1184
1185 // Try block code. Sets up the exception handler chain.
1186 __ bind(&try_handler_setup);
1187 {
1188 TryCatch try_block(this, &catch_entry);
1189 __ PushTryHandler(IN_JAVASCRIPT, TRY_CATCH_HANDLER);
1190 Visit(stmt->try_block());
1191 __ PopTryHandler();
1192 }
1193 __ bind(&done);
1194}
1195
1196
1197void FullCodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* stmt) {
1198 Comment cmnt(masm_, "[ TryFinallyStatement");
1199 SetStatementPosition(stmt);
1200 // Try finally is compiled by setting up a try-handler on the stack while
1201 // executing the try body, and removing it again afterwards.
1202 //
1203 // The try-finally construct can enter the finally block in three ways:
1204 // 1. By exiting the try-block normally. This removes the try-handler and
1205 // calls the finally block code before continuing.
1206 // 2. By exiting the try-block with a function-local control flow transfer
1207 // (break/continue/return). The site of the, e.g., break removes the
1208 // try handler and calls the finally block code before continuing
1209 // its outward control transfer.
1210 // 3. by exiting the try-block with a thrown exception.
1211 // This can happen in nested function calls. It traverses the try-handler
1212 // chain and consumes the try-handler entry before jumping to the
1213 // handler code. The handler code then calls the finally-block before
1214 // rethrowing the exception.
1215 //
1216 // The finally block must assume a return address on top of the stack
1217 // (or in the link register on ARM chips) and a value (return value or
1218 // exception) in the result register (rax/eax/r0), both of which must
1219 // be preserved. The return address isn't GC-safe, so it should be
1220 // cooked before GC.
1221 Label finally_entry;
1222 Label try_handler_setup;
1223
1224 // Setup the try-handler chain. Use a call to
1225 // Jump to try-handler setup and try-block code. Use call to put try-handler
1226 // address on stack.
1227 __ Call(&try_handler_setup);
1228 // Try handler code. Return address of call is pushed on handler stack.
1229 {
1230 // This code is only executed during stack-handler traversal when an
1231 // exception is thrown. The execption is in the result register, which
1232 // is retained by the finally block.
1233 // Call the finally block and then rethrow the exception.
1234 __ Call(&finally_entry);
1235 __ push(result_register());
1236 __ CallRuntime(Runtime::kReThrow, 1);
1237 }
1238
1239 __ bind(&finally_entry);
1240 {
1241 // Finally block implementation.
1242 Finally finally_block(this);
1243 EnterFinallyBlock();
1244 Visit(stmt->finally_block());
1245 ExitFinallyBlock(); // Return to the calling code.
1246 }
1247
1248 __ bind(&try_handler_setup);
1249 {
1250 // Setup try handler (stack pointer registers).
1251 TryFinally try_block(this, &finally_entry);
1252 __ PushTryHandler(IN_JAVASCRIPT, TRY_FINALLY_HANDLER);
1253 Visit(stmt->try_block());
1254 __ PopTryHandler();
1255 }
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001256 // Execute the finally block on the way out. Clobber the unpredictable
1257 // value in the accumulator with one that's safe for GC. The finally
1258 // block will unconditionally preserve the accumulator on the stack.
1259 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001260 __ Call(&finally_entry);
1261}
1262
1263
1264void FullCodeGenerator::VisitDebuggerStatement(DebuggerStatement* stmt) {
1265#ifdef ENABLE_DEBUGGER_SUPPORT
1266 Comment cmnt(masm_, "[ DebuggerStatement");
1267 SetStatementPosition(stmt);
1268
ager@chromium.org5c838252010-02-19 08:53:10 +00001269 __ DebugBreak();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001270 // Ignore the return value.
1271#endif
1272}
1273
1274
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001275void FullCodeGenerator::VisitConditional(Conditional* expr) {
1276 Comment cmnt(masm_, "[ Conditional");
1277 Label true_case, false_case, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001278 VisitForControl(expr->condition(), &true_case, &false_case, &true_case);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001279
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001280 PrepareForBailoutForId(expr->ThenId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001281 __ bind(&true_case);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001282 SetExpressionPosition(expr->then_expression(),
1283 expr->then_expression_position());
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001284 if (context()->IsTest()) {
1285 const TestContext* for_test = TestContext::cast(context());
1286 VisitForControl(expr->then_expression(),
1287 for_test->true_label(),
1288 for_test->false_label(),
1289 NULL);
1290 } else {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001291 context()->HandleExpression(expr->then_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001292 __ jmp(&done);
1293 }
1294
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001295 PrepareForBailoutForId(expr->ElseId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001296 __ bind(&false_case);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001297 if (context()->IsTest()) ForwardBailoutToChild(expr);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001298 SetExpressionPosition(expr->else_expression(),
1299 expr->else_expression_position());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001300 context()->HandleExpression(expr->else_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001301 // If control flow falls through Visit, merge it with true case here.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001302 if (!context()->IsTest()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001303 __ bind(&done);
1304 }
1305}
1306
1307
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001308void FullCodeGenerator::VisitLiteral(Literal* expr) {
1309 Comment cmnt(masm_, "[ Literal");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001310 context()->Plug(expr->handle());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001311}
1312
1313
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001314void FullCodeGenerator::VisitFunctionLiteral(FunctionLiteral* expr) {
1315 Comment cmnt(masm_, "[ FunctionLiteral");
1316
1317 // Build the function boilerplate and instantiate it.
1318 Handle<SharedFunctionInfo> function_info =
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001319 Compiler::BuildFunctionInfo(expr, script());
1320 if (function_info.is_null()) {
1321 SetStackOverflow();
1322 return;
1323 }
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001324 EmitNewClosure(function_info, expr->pretenure());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001325}
1326
1327
1328void FullCodeGenerator::VisitSharedFunctionInfoLiteral(
1329 SharedFunctionInfoLiteral* expr) {
1330 Comment cmnt(masm_, "[ SharedFunctionInfoLiteral");
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001331 EmitNewClosure(expr->shared_function_info(), false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001332}
1333
1334
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001335void FullCodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* expr) {
1336 // Call runtime routine to allocate the catch extension object and
1337 // assign the exception value to the catch variable.
1338 Comment cmnt(masm_, "[ CatchExtensionObject");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001339 VisitForStackValue(expr->key());
1340 VisitForStackValue(expr->value());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001341 // Create catch extension object.
1342 __ CallRuntime(Runtime::kCreateCatchExtensionObject, 2);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001343 context()->Plug(result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001344}
1345
1346
1347void FullCodeGenerator::VisitThrow(Throw* expr) {
1348 Comment cmnt(masm_, "[ Throw");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001349 VisitForStackValue(expr->exception());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001350 __ CallRuntime(Runtime::kThrow, 1);
1351 // Never returns here.
1352}
1353
1354
ricow@chromium.org65fae842010-08-25 15:26:24 +00001355void FullCodeGenerator::VisitIncrementOperation(IncrementOperation* expr) {
1356 UNREACHABLE();
1357}
1358
1359
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001360int FullCodeGenerator::TryFinally::Exit(int stack_depth) {
1361 // The macros used here must preserve the result register.
1362 __ Drop(stack_depth);
1363 __ PopTryHandler();
1364 __ Call(finally_entry_);
1365 return 0;
1366}
1367
1368
1369int FullCodeGenerator::TryCatch::Exit(int stack_depth) {
1370 // The macros used here must preserve the result register.
1371 __ Drop(stack_depth);
1372 __ PopTryHandler();
1373 return 0;
1374}
1375
ricow@chromium.org65fae842010-08-25 15:26:24 +00001376
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001377#undef __
1378
1379
1380} } // namespace v8::internal