blob: 58540f07bc663a6ef61f0fc7a55f2f66257d7419 [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) {
whesse@chromium.org023421e2010-12-21 12:19:12 +0000674 int lookup_index =
675 static_cast<int>(id) - static_cast<int>(Runtime::kFirstInlineFunction);
676 ASSERT(lookup_index >= 0);
677 ASSERT(static_cast<size_t>(lookup_index) <
678 ARRAY_SIZE(kInlineFunctionGenerators));
679 return kInlineFunctionGenerators[lookup_index];
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000680}
681
682
683void FullCodeGenerator::EmitInlineRuntimeCall(CallRuntime* node) {
684 ZoneList<Expression*>* args = node->arguments();
685 Handle<String> name = node->name();
686 Runtime::Function* function = node->function();
687 ASSERT(function != NULL);
688 ASSERT(function->intrinsic_type == Runtime::INLINE);
689 InlineFunctionGenerator generator =
690 FindInlineFunctionGenerator(function->function_id);
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000691 ((*this).*(generator))(args);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000692}
693
694
695void FullCodeGenerator::VisitBinaryOperation(BinaryOperation* expr) {
696 Comment cmnt(masm_, "[ BinaryOperation");
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000697 Token::Value op = expr->op();
698 Expression* left = expr->left();
699 Expression* right = expr->right();
ricow@chromium.org65fae842010-08-25 15:26:24 +0000700
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000701 OverwriteMode mode = NO_OVERWRITE;
702 if (left->ResultOverwriteAllowed()) {
703 mode = OVERWRITE_LEFT;
704 } else if (right->ResultOverwriteAllowed()) {
705 mode = OVERWRITE_RIGHT;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000706 }
707
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000708 switch (op) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000709 case Token::COMMA:
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000710 VisitForEffect(left);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000711 if (context()->IsTest()) ForwardBailoutToChild(expr);
712 context()->HandleExpression(right);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000713 break;
714
715 case Token::OR:
716 case Token::AND:
717 EmitLogicalOperation(expr);
718 break;
719
720 case Token::ADD:
721 case Token::SUB:
722 case Token::DIV:
723 case Token::MOD:
724 case Token::MUL:
725 case Token::BIT_OR:
726 case Token::BIT_AND:
727 case Token::BIT_XOR:
728 case Token::SHL:
729 case Token::SHR:
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000730 case Token::SAR: {
731 // Figure out if either of the operands is a constant.
732 ConstantOperand constant = ShouldInlineSmiCase(op)
733 ? GetConstantOperand(op, left, right)
734 : kNoConstants;
735
736 // Load only the operands that we need to materialize.
737 if (constant == kNoConstants) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000738 VisitForStackValue(left);
739 VisitForAccumulatorValue(right);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000740 } else if (constant == kRightConstant) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000741 VisitForAccumulatorValue(left);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000742 } else {
743 ASSERT(constant == kLeftConstant);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000744 VisitForAccumulatorValue(right);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000745 }
746
ricow@chromium.org65fae842010-08-25 15:26:24 +0000747 SetSourcePosition(expr->position());
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000748 if (ShouldInlineSmiCase(op)) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000749 EmitInlineSmiBinaryOp(expr, op, mode, left, right, constant);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000750 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000751 EmitBinaryOp(op, mode);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000752 }
ricow@chromium.org65fae842010-08-25 15:26:24 +0000753 break;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000754 }
ricow@chromium.org65fae842010-08-25 15:26:24 +0000755
756 default:
757 UNREACHABLE();
758 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000759}
760
761
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000762void FullCodeGenerator::EmitLogicalOperation(BinaryOperation* expr) {
763 Label eval_right, done;
764
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000765 context()->EmitLogicalLeft(expr, &eval_right, &done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000766
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000767 PrepareForBailoutForId(expr->RightId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000768 __ bind(&eval_right);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000769 if (context()->IsTest()) ForwardBailoutToChild(expr);
770 context()->HandleExpression(expr->right());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000771
772 __ bind(&done);
773}
774
775
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000776void FullCodeGenerator::EffectContext::EmitLogicalLeft(BinaryOperation* expr,
777 Label* eval_right,
778 Label* done) const {
779 if (expr->op() == Token::OR) {
780 codegen()->VisitForControl(expr->left(), done, eval_right, eval_right);
781 } else {
782 ASSERT(expr->op() == Token::AND);
783 codegen()->VisitForControl(expr->left(), eval_right, done, eval_right);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000784 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000785}
ricow@chromium.org65fae842010-08-25 15:26:24 +0000786
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000787
788void FullCodeGenerator::AccumulatorValueContext::EmitLogicalLeft(
789 BinaryOperation* expr,
790 Label* eval_right,
791 Label* done) const {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000792 HandleExpression(expr->left());
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000793 // We want the value in the accumulator for the test, and on the stack in case
794 // we need it.
795 __ push(result_register());
796 Label discard, restore;
797 if (expr->op() == Token::OR) {
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(&restore, &discard, &restore);
800 } else {
801 ASSERT(expr->op() == Token::AND);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000802 codegen()->PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000803 codegen()->DoTest(&discard, &restore, &restore);
804 }
805 __ bind(&restore);
806 __ pop(result_register());
807 __ jmp(done);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000808 __ bind(&discard);
809 __ Drop(1);
810}
811
812
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000813void FullCodeGenerator::StackValueContext::EmitLogicalLeft(
814 BinaryOperation* expr,
815 Label* eval_right,
816 Label* done) const {
817 codegen()->VisitForAccumulatorValue(expr->left());
818 // We want the value in the accumulator for the test, and on the stack in case
819 // we need it.
820 __ push(result_register());
821 Label discard;
822 if (expr->op() == Token::OR) {
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(done, &discard, &discard);
825 } else {
826 ASSERT(expr->op() == Token::AND);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000827 codegen()->PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000828 codegen()->DoTest(&discard, done, &discard);
829 }
830 __ bind(&discard);
831 __ Drop(1);
832}
833
834
835void FullCodeGenerator::TestContext::EmitLogicalLeft(BinaryOperation* expr,
836 Label* eval_right,
837 Label* done) const {
838 if (expr->op() == Token::OR) {
839 codegen()->VisitForControl(expr->left(),
840 true_label_, eval_right, eval_right);
841 } else {
842 ASSERT(expr->op() == Token::AND);
843 codegen()->VisitForControl(expr->left(),
844 eval_right, false_label_, eval_right);
845 }
846}
847
848
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000849void FullCodeGenerator::ForwardBailoutToChild(Expression* expr) {
850 if (!info_->HasDeoptimizationSupport()) return;
851 ASSERT(context()->IsTest());
852 ASSERT(expr == forward_bailout_stack_->expr());
853 forward_bailout_pending_ = forward_bailout_stack_;
854}
855
856
857void FullCodeGenerator::EffectContext::HandleExpression(
858 Expression* expr) const {
859 codegen()->HandleInNonTestContext(expr, NO_REGISTERS);
860}
861
862
863void FullCodeGenerator::AccumulatorValueContext::HandleExpression(
864 Expression* expr) const {
865 codegen()->HandleInNonTestContext(expr, TOS_REG);
866}
867
868
869void FullCodeGenerator::StackValueContext::HandleExpression(
870 Expression* expr) const {
871 codegen()->HandleInNonTestContext(expr, NO_REGISTERS);
872}
873
874
875void FullCodeGenerator::TestContext::HandleExpression(Expression* expr) const {
876 codegen()->VisitInTestContext(expr);
877}
878
879
880void FullCodeGenerator::HandleInNonTestContext(Expression* expr, State state) {
881 ASSERT(forward_bailout_pending_ == NULL);
882 AstVisitor::Visit(expr);
883 PrepareForBailout(expr, state);
884 // Forwarding bailouts to children is a one shot operation. It
885 // should have been processed at this point.
886 ASSERT(forward_bailout_pending_ == NULL);
887}
888
889
890void FullCodeGenerator::VisitInTestContext(Expression* expr) {
891 ForwardBailoutStack stack(expr, forward_bailout_pending_);
892 ForwardBailoutStack* saved = forward_bailout_stack_;
893 forward_bailout_pending_ = NULL;
894 forward_bailout_stack_ = &stack;
895 AstVisitor::Visit(expr);
896 forward_bailout_stack_ = saved;
897}
898
899
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000900void FullCodeGenerator::VisitBlock(Block* stmt) {
901 Comment cmnt(masm_, "[ Block");
902 Breakable nested_statement(this, stmt);
903 SetStatementPosition(stmt);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000904
905 PrepareForBailoutForId(stmt->EntryId(), TOS_REG);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000906 VisitStatements(stmt->statements());
907 __ bind(nested_statement.break_target());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000908 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000909}
910
911
912void FullCodeGenerator::VisitExpressionStatement(ExpressionStatement* stmt) {
913 Comment cmnt(masm_, "[ ExpressionStatement");
914 SetStatementPosition(stmt);
915 VisitForEffect(stmt->expression());
916}
917
918
919void FullCodeGenerator::VisitEmptyStatement(EmptyStatement* stmt) {
920 Comment cmnt(masm_, "[ EmptyStatement");
921 SetStatementPosition(stmt);
922}
923
924
925void FullCodeGenerator::VisitIfStatement(IfStatement* stmt) {
926 Comment cmnt(masm_, "[ IfStatement");
927 SetStatementPosition(stmt);
928 Label then_part, else_part, done;
929
ricow@chromium.org65fae842010-08-25 15:26:24 +0000930 if (stmt->HasElseStatement()) {
931 VisitForControl(stmt->condition(), &then_part, &else_part, &then_part);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000932 PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000933 __ bind(&then_part);
934 Visit(stmt->then_statement());
935 __ jmp(&done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000936
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000937 PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000938 __ bind(&else_part);
939 Visit(stmt->else_statement());
940 } else {
941 VisitForControl(stmt->condition(), &then_part, &done, &then_part);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000942 PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000943 __ bind(&then_part);
944 Visit(stmt->then_statement());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000945
946 PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000947 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000948 __ bind(&done);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000949 PrepareForBailoutForId(stmt->id(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000950}
951
952
953void FullCodeGenerator::VisitContinueStatement(ContinueStatement* stmt) {
954 Comment cmnt(masm_, "[ ContinueStatement");
955 SetStatementPosition(stmt);
956 NestedStatement* current = nesting_stack_;
957 int stack_depth = 0;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000958 // When continuing, we clobber the unpredictable value in the accumulator
959 // with one that's safe for GC. If we hit an exit from the try block of
960 // try...finally on our way out, we will unconditionally preserve the
961 // accumulator on the stack.
962 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000963 while (!current->IsContinueTarget(stmt->target())) {
964 stack_depth = current->Exit(stack_depth);
965 current = current->outer();
966 }
967 __ Drop(stack_depth);
968
969 Iteration* loop = current->AsIteration();
970 __ jmp(loop->continue_target());
971}
972
973
974void FullCodeGenerator::VisitBreakStatement(BreakStatement* stmt) {
975 Comment cmnt(masm_, "[ BreakStatement");
976 SetStatementPosition(stmt);
977 NestedStatement* current = nesting_stack_;
978 int stack_depth = 0;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000979 // When breaking, we clobber the unpredictable value in the accumulator
980 // with one that's safe for GC. If we hit an exit from the try block of
981 // try...finally on our way out, we will unconditionally preserve the
982 // accumulator on the stack.
983 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000984 while (!current->IsBreakTarget(stmt->target())) {
985 stack_depth = current->Exit(stack_depth);
986 current = current->outer();
987 }
988 __ Drop(stack_depth);
989
990 Breakable* target = current->AsBreakable();
991 __ jmp(target->break_target());
992}
993
994
995void FullCodeGenerator::VisitReturnStatement(ReturnStatement* stmt) {
996 Comment cmnt(masm_, "[ ReturnStatement");
997 SetStatementPosition(stmt);
998 Expression* expr = stmt->expression();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000999 VisitForAccumulatorValue(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001000
1001 // Exit all nested statements.
1002 NestedStatement* current = nesting_stack_;
1003 int stack_depth = 0;
1004 while (current != NULL) {
1005 stack_depth = current->Exit(stack_depth);
1006 current = current->outer();
1007 }
1008 __ Drop(stack_depth);
1009
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001010 EmitReturnSequence();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001011}
1012
1013
1014void FullCodeGenerator::VisitWithEnterStatement(WithEnterStatement* stmt) {
1015 Comment cmnt(masm_, "[ WithEnterStatement");
1016 SetStatementPosition(stmt);
1017
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001018 VisitForStackValue(stmt->expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001019 if (stmt->is_catch_block()) {
1020 __ CallRuntime(Runtime::kPushCatchContext, 1);
1021 } else {
1022 __ CallRuntime(Runtime::kPushContext, 1);
1023 }
1024 // Both runtime calls return the new context in both the context and the
1025 // result registers.
1026
1027 // Update local stack frame context field.
1028 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
1029}
1030
1031
1032void FullCodeGenerator::VisitWithExitStatement(WithExitStatement* stmt) {
1033 Comment cmnt(masm_, "[ WithExitStatement");
1034 SetStatementPosition(stmt);
1035
1036 // Pop context.
1037 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1038 // Update local stack frame context field.
1039 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
1040}
1041
1042
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001043void FullCodeGenerator::VisitDoWhileStatement(DoWhileStatement* stmt) {
1044 Comment cmnt(masm_, "[ DoWhileStatement");
1045 SetStatementPosition(stmt);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001046 Label body, stack_check;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001047
1048 Iteration loop_statement(this, stmt);
1049 increment_loop_depth();
1050
1051 __ bind(&body);
1052 Visit(stmt->body());
1053
ricow@chromium.org65fae842010-08-25 15:26:24 +00001054 // Record the position of the do while condition and make sure it is
1055 // possible to break on the condition.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001056 __ bind(loop_statement.continue_target());
1057 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001058 SetExpressionPosition(stmt->cond(), stmt->condition_position());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001059 VisitForControl(stmt->cond(),
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001060 &stack_check,
ricow@chromium.org65fae842010-08-25 15:26:24 +00001061 loop_statement.break_target(),
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001062 &stack_check);
1063
1064 // Check stack before looping.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001065 PrepareForBailoutForId(stmt->BackEdgeId(), NO_REGISTERS);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001066 __ bind(&stack_check);
1067 EmitStackCheck(stmt);
1068 __ jmp(&body);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001069
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001070 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001071 __ bind(loop_statement.break_target());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001072 decrement_loop_depth();
1073}
1074
1075
1076void FullCodeGenerator::VisitWhileStatement(WhileStatement* stmt) {
1077 Comment cmnt(masm_, "[ WhileStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001078 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001079
1080 Iteration loop_statement(this, stmt);
1081 increment_loop_depth();
1082
1083 // Emit the test at the bottom of the loop.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001084 __ jmp(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001085
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001086 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001087 __ bind(&body);
1088 Visit(stmt->body());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001089
1090 // Emit the statement position here as this is where the while
1091 // statement code starts.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001092 __ bind(loop_statement.continue_target());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001093 SetStatementPosition(stmt);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001094
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001095 // Check stack before looping.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001096 EmitStackCheck(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001097
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001098 __ bind(&test);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001099 VisitForControl(stmt->cond(),
1100 &body,
1101 loop_statement.break_target(),
1102 loop_statement.break_target());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001103
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001104 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001105 __ bind(loop_statement.break_target());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001106 decrement_loop_depth();
1107}
1108
1109
1110void FullCodeGenerator::VisitForStatement(ForStatement* stmt) {
1111 Comment cmnt(masm_, "[ ForStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001112 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001113
1114 Iteration loop_statement(this, stmt);
1115 if (stmt->init() != NULL) {
1116 Visit(stmt->init());
1117 }
1118
1119 increment_loop_depth();
1120 // Emit the test at the bottom of the loop (even if empty).
1121 __ jmp(&test);
1122
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001123 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001124 __ bind(&body);
1125 Visit(stmt->body());
1126
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001127 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001128 __ bind(loop_statement.continue_target());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001129 SetStatementPosition(stmt);
1130 if (stmt->next() != NULL) {
1131 Visit(stmt->next());
1132 }
1133
ricow@chromium.org65fae842010-08-25 15:26:24 +00001134 // Emit the statement position here as this is where the for
1135 // statement code starts.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001136 SetStatementPosition(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001137
1138 // Check stack before looping.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001139 EmitStackCheck(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001140
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001141 __ bind(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001142 if (stmt->cond() != NULL) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00001143 VisitForControl(stmt->cond(),
1144 &body,
1145 loop_statement.break_target(),
1146 loop_statement.break_target());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001147 } else {
1148 __ jmp(&body);
1149 }
1150
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001151 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001152 __ bind(loop_statement.break_target());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001153 decrement_loop_depth();
1154}
1155
1156
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001157void FullCodeGenerator::VisitTryCatchStatement(TryCatchStatement* stmt) {
1158 Comment cmnt(masm_, "[ TryCatchStatement");
1159 SetStatementPosition(stmt);
1160 // The try block adds a handler to the exception handler chain
1161 // before entering, and removes it again when exiting normally.
1162 // If an exception is thrown during execution of the try block,
1163 // control is passed to the handler, which also consumes the handler.
1164 // At this point, the exception is in a register, and store it in
1165 // the temporary local variable (prints as ".catch-var") before
1166 // executing the catch block. The catch block has been rewritten
1167 // to introduce a new scope to bind the catch variable and to remove
1168 // that scope again afterwards.
1169
1170 Label try_handler_setup, catch_entry, done;
1171 __ Call(&try_handler_setup);
1172 // Try handler code, exception in result register.
1173
1174 // Store exception in local .catch variable before executing catch block.
1175 {
1176 // The catch variable is *always* a variable proxy for a local variable.
1177 Variable* catch_var = stmt->catch_var()->AsVariableProxy()->AsVariable();
1178 ASSERT_NOT_NULL(catch_var);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001179 Slot* variable_slot = catch_var->AsSlot();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001180 ASSERT_NOT_NULL(variable_slot);
1181 ASSERT_EQ(Slot::LOCAL, variable_slot->type());
1182 StoreToFrameField(SlotOffset(variable_slot), result_register());
1183 }
1184
1185 Visit(stmt->catch_block());
1186 __ jmp(&done);
1187
1188 // Try block code. Sets up the exception handler chain.
1189 __ bind(&try_handler_setup);
1190 {
1191 TryCatch try_block(this, &catch_entry);
1192 __ PushTryHandler(IN_JAVASCRIPT, TRY_CATCH_HANDLER);
1193 Visit(stmt->try_block());
1194 __ PopTryHandler();
1195 }
1196 __ bind(&done);
1197}
1198
1199
1200void FullCodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* stmt) {
1201 Comment cmnt(masm_, "[ TryFinallyStatement");
1202 SetStatementPosition(stmt);
1203 // Try finally is compiled by setting up a try-handler on the stack while
1204 // executing the try body, and removing it again afterwards.
1205 //
1206 // The try-finally construct can enter the finally block in three ways:
1207 // 1. By exiting the try-block normally. This removes the try-handler and
1208 // calls the finally block code before continuing.
1209 // 2. By exiting the try-block with a function-local control flow transfer
1210 // (break/continue/return). The site of the, e.g., break removes the
1211 // try handler and calls the finally block code before continuing
1212 // its outward control transfer.
1213 // 3. by exiting the try-block with a thrown exception.
1214 // This can happen in nested function calls. It traverses the try-handler
1215 // chain and consumes the try-handler entry before jumping to the
1216 // handler code. The handler code then calls the finally-block before
1217 // rethrowing the exception.
1218 //
1219 // The finally block must assume a return address on top of the stack
1220 // (or in the link register on ARM chips) and a value (return value or
1221 // exception) in the result register (rax/eax/r0), both of which must
1222 // be preserved. The return address isn't GC-safe, so it should be
1223 // cooked before GC.
1224 Label finally_entry;
1225 Label try_handler_setup;
1226
1227 // Setup the try-handler chain. Use a call to
1228 // Jump to try-handler setup and try-block code. Use call to put try-handler
1229 // address on stack.
1230 __ Call(&try_handler_setup);
1231 // Try handler code. Return address of call is pushed on handler stack.
1232 {
1233 // This code is only executed during stack-handler traversal when an
1234 // exception is thrown. The execption is in the result register, which
1235 // is retained by the finally block.
1236 // Call the finally block and then rethrow the exception.
1237 __ Call(&finally_entry);
1238 __ push(result_register());
1239 __ CallRuntime(Runtime::kReThrow, 1);
1240 }
1241
1242 __ bind(&finally_entry);
1243 {
1244 // Finally block implementation.
1245 Finally finally_block(this);
1246 EnterFinallyBlock();
1247 Visit(stmt->finally_block());
1248 ExitFinallyBlock(); // Return to the calling code.
1249 }
1250
1251 __ bind(&try_handler_setup);
1252 {
1253 // Setup try handler (stack pointer registers).
1254 TryFinally try_block(this, &finally_entry);
1255 __ PushTryHandler(IN_JAVASCRIPT, TRY_FINALLY_HANDLER);
1256 Visit(stmt->try_block());
1257 __ PopTryHandler();
1258 }
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001259 // Execute the finally block on the way out. Clobber the unpredictable
1260 // value in the accumulator with one that's safe for GC. The finally
1261 // block will unconditionally preserve the accumulator on the stack.
1262 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001263 __ Call(&finally_entry);
1264}
1265
1266
1267void FullCodeGenerator::VisitDebuggerStatement(DebuggerStatement* stmt) {
1268#ifdef ENABLE_DEBUGGER_SUPPORT
1269 Comment cmnt(masm_, "[ DebuggerStatement");
1270 SetStatementPosition(stmt);
1271
ager@chromium.org5c838252010-02-19 08:53:10 +00001272 __ DebugBreak();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001273 // Ignore the return value.
1274#endif
1275}
1276
1277
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001278void FullCodeGenerator::VisitConditional(Conditional* expr) {
1279 Comment cmnt(masm_, "[ Conditional");
1280 Label true_case, false_case, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001281 VisitForControl(expr->condition(), &true_case, &false_case, &true_case);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001282
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001283 PrepareForBailoutForId(expr->ThenId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001284 __ bind(&true_case);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001285 SetExpressionPosition(expr->then_expression(),
1286 expr->then_expression_position());
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001287 if (context()->IsTest()) {
1288 const TestContext* for_test = TestContext::cast(context());
1289 VisitForControl(expr->then_expression(),
1290 for_test->true_label(),
1291 for_test->false_label(),
1292 NULL);
1293 } else {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001294 context()->HandleExpression(expr->then_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001295 __ jmp(&done);
1296 }
1297
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001298 PrepareForBailoutForId(expr->ElseId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001299 __ bind(&false_case);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001300 if (context()->IsTest()) ForwardBailoutToChild(expr);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001301 SetExpressionPosition(expr->else_expression(),
1302 expr->else_expression_position());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001303 context()->HandleExpression(expr->else_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001304 // If control flow falls through Visit, merge it with true case here.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001305 if (!context()->IsTest()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001306 __ bind(&done);
1307 }
1308}
1309
1310
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001311void FullCodeGenerator::VisitLiteral(Literal* expr) {
1312 Comment cmnt(masm_, "[ Literal");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001313 context()->Plug(expr->handle());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001314}
1315
1316
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001317void FullCodeGenerator::VisitFunctionLiteral(FunctionLiteral* expr) {
1318 Comment cmnt(masm_, "[ FunctionLiteral");
1319
1320 // Build the function boilerplate and instantiate it.
1321 Handle<SharedFunctionInfo> function_info =
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001322 Compiler::BuildFunctionInfo(expr, script());
1323 if (function_info.is_null()) {
1324 SetStackOverflow();
1325 return;
1326 }
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001327 EmitNewClosure(function_info, expr->pretenure());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001328}
1329
1330
1331void FullCodeGenerator::VisitSharedFunctionInfoLiteral(
1332 SharedFunctionInfoLiteral* expr) {
1333 Comment cmnt(masm_, "[ SharedFunctionInfoLiteral");
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001334 EmitNewClosure(expr->shared_function_info(), false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001335}
1336
1337
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001338void FullCodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* expr) {
1339 // Call runtime routine to allocate the catch extension object and
1340 // assign the exception value to the catch variable.
1341 Comment cmnt(masm_, "[ CatchExtensionObject");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001342 VisitForStackValue(expr->key());
1343 VisitForStackValue(expr->value());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001344 // Create catch extension object.
1345 __ CallRuntime(Runtime::kCreateCatchExtensionObject, 2);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001346 context()->Plug(result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001347}
1348
1349
1350void FullCodeGenerator::VisitThrow(Throw* expr) {
1351 Comment cmnt(masm_, "[ Throw");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001352 VisitForStackValue(expr->exception());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001353 __ CallRuntime(Runtime::kThrow, 1);
1354 // Never returns here.
1355}
1356
1357
ricow@chromium.org65fae842010-08-25 15:26:24 +00001358void FullCodeGenerator::VisitIncrementOperation(IncrementOperation* expr) {
1359 UNREACHABLE();
1360}
1361
1362
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001363int FullCodeGenerator::TryFinally::Exit(int stack_depth) {
1364 // The macros used here must preserve the result register.
1365 __ Drop(stack_depth);
1366 __ PopTryHandler();
1367 __ Call(finally_entry_);
1368 return 0;
1369}
1370
1371
1372int FullCodeGenerator::TryCatch::Exit(int stack_depth) {
1373 // The macros used here must preserve the result register.
1374 __ Drop(stack_depth);
1375 __ PopTryHandler();
1376 return 0;
1377}
1378
ricow@chromium.org65fae842010-08-25 15:26:24 +00001379
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001380#undef __
1381
1382
1383} } // namespace v8::internal