blob: 252fb9257a9bfdfcabd2ce77b541de233502b8f3 [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);
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000289#ifdef ENABLE_GDB_JIT_INTERFACE
290 masm.positions_recorder()->StartGDBJITLineInfoRecording();
291#endif
ager@chromium.org5c838252010-02-19 08:53:10 +0000292
293 FullCodeGenerator cgen(&masm);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000294 cgen.Generate(info);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000295 if (cgen.HasStackOverflow()) {
296 ASSERT(!Top::has_pending_exception());
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000297 return false;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000298 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000299 unsigned table_offset = cgen.EmitStackCheckTable();
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000300
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000301 Code::Flags flags = Code::ComputeFlags(Code::FUNCTION, NOT_IN_LOOP);
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000302 Handle<Code> code = CodeGenerator::MakeCodeEpilogue(&masm, flags, info);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000303 code->set_optimizable(info->IsOptimizable());
304 cgen.PopulateDeoptimizationData(code);
305 code->set_has_deoptimization_support(info->HasDeoptimizationSupport());
306 code->set_allow_osr_at_loop_nesting_level(0);
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000307 code->set_stack_check_table_offset(table_offset);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000308 CodeGenerator::PrintCode(code, info);
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000309 info->SetCode(code); // may be an empty handle.
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000310#ifdef ENABLE_GDB_JIT_INTERFACE
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000311 if (FLAG_gdbjit && !code.is_null()) {
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000312 GDBJITLineInfo* lineinfo =
313 masm.positions_recorder()->DetachGDBJITLineInfo();
314
315 GDBJIT(RegisterDetailedLineInfo(*code, lineinfo));
316 }
317#endif
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000318 return !code.is_null();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000319}
320
321
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000322unsigned FullCodeGenerator::EmitStackCheckTable() {
323 // The stack check table consists of a length (in number of entries)
324 // field, and then a sequence of entries. Each entry is a pair of AST id
325 // and code-relative pc offset.
326 masm()->Align(kIntSize);
327 masm()->RecordComment("[ Stack check table");
328 unsigned offset = masm()->pc_offset();
329 unsigned length = stack_checks_.length();
330 __ dd(length);
331 for (unsigned i = 0; i < length; ++i) {
332 __ dd(stack_checks_[i].id);
333 __ dd(stack_checks_[i].pc_and_state);
334 }
335 masm()->RecordComment("]");
336 return offset;
337}
338
339
340void FullCodeGenerator::PopulateDeoptimizationData(Handle<Code> code) {
341 // Fill in the deoptimization information.
342 ASSERT(info_->HasDeoptimizationSupport() || bailout_entries_.is_empty());
343 if (!info_->HasDeoptimizationSupport()) return;
344 int length = bailout_entries_.length();
345 Handle<DeoptimizationOutputData> data =
346 Factory::NewDeoptimizationOutputData(length, TENURED);
347 for (int i = 0; i < length; i++) {
348 data->SetAstId(i, Smi::FromInt(bailout_entries_[i].id));
349 data->SetPcAndState(i, Smi::FromInt(bailout_entries_[i].pc_and_state));
350 }
351 code->set_deoptimization_data(*data);
352}
353
354
355void FullCodeGenerator::PrepareForBailout(AstNode* node, State state) {
356 PrepareForBailoutForId(node->id(), state);
357}
358
359
360void FullCodeGenerator::RecordJSReturnSite(Call* call) {
361 // We record the offset of the function return so we can rebuild the frame
362 // if the function was inlined, i.e., this is the return address in the
363 // inlined function's frame.
364 //
365 // The state is ignored. We defensively set it to TOS_REG, which is the
366 // real state of the unoptimized code at the return site.
367 PrepareForBailoutForId(call->ReturnId(), TOS_REG);
368#ifdef DEBUG
369 // In debug builds, mark the return so we can verify that this function
370 // was called.
371 ASSERT(!call->return_is_recorded_);
372 call->return_is_recorded_ = true;
373#endif
374}
375
376
377void FullCodeGenerator::PrepareForBailoutForId(int id, State state) {
378 // There's no need to prepare this code for bailouts from already optimized
379 // code or code that can't be optimized.
380 if (!FLAG_deopt || !info_->HasDeoptimizationSupport()) return;
381 unsigned pc_and_state =
382 StateField::encode(state) | PcField::encode(masm_->pc_offset());
383 BailoutEntry entry = { id, pc_and_state };
384#ifdef DEBUG
385 // Assert that we don't have multiple bailout entries for the same node.
386 for (int i = 0; i < bailout_entries_.length(); i++) {
387 if (bailout_entries_.at(i).id == entry.id) {
388 AstPrinter printer;
389 PrintF("%s", printer.PrintProgram(info_->function()));
390 UNREACHABLE();
391 }
392 }
393#endif // DEBUG
394 bailout_entries_.Add(entry);
395}
396
397
398void FullCodeGenerator::RecordStackCheck(int ast_id) {
399 // The pc offset does not need to be encoded and packed together with a
400 // state.
401 BailoutEntry entry = { ast_id, masm_->pc_offset() };
402 stack_checks_.Add(entry);
403}
404
405
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000406int FullCodeGenerator::SlotOffset(Slot* slot) {
407 ASSERT(slot != NULL);
408 // Offset is negative because higher indexes are at lower addresses.
409 int offset = -slot->index() * kPointerSize;
410 // Adjust by a (parameter or local) base offset.
411 switch (slot->type()) {
412 case Slot::PARAMETER:
ager@chromium.org5c838252010-02-19 08:53:10 +0000413 offset += (scope()->num_parameters() + 1) * kPointerSize;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000414 break;
415 case Slot::LOCAL:
416 offset += JavaScriptFrameConstants::kLocal0Offset;
417 break;
418 case Slot::CONTEXT:
419 case Slot::LOOKUP:
420 UNREACHABLE();
421 }
422 return offset;
423}
424
425
ricow@chromium.org65fae842010-08-25 15:26:24 +0000426bool FullCodeGenerator::ShouldInlineSmiCase(Token::Value op) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000427 // Inline smi case inside loops, but not division and modulo which
428 // are too complicated and take up too much space.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000429 if (op == Token::DIV ||op == Token::MOD) return false;
430 if (FLAG_always_inline_smi_code) return true;
431 return loop_depth_ > 0;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000432}
433
434
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000435void FullCodeGenerator::EffectContext::Plug(Register reg) const {
436}
437
438
439void FullCodeGenerator::AccumulatorValueContext::Plug(Register reg) const {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000440 __ Move(result_register(), reg);
441}
442
443
444void FullCodeGenerator::StackValueContext::Plug(Register reg) const {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000445 __ push(reg);
446}
447
448
449void FullCodeGenerator::TestContext::Plug(Register reg) const {
450 // For simplicity we always test the accumulator register.
451 __ Move(result_register(), reg);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000452 codegen()->PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000453 codegen()->DoTest(true_label_, false_label_, fall_through_);
454}
455
456
457void FullCodeGenerator::EffectContext::PlugTOS() const {
458 __ Drop(1);
459}
460
461
462void FullCodeGenerator::AccumulatorValueContext::PlugTOS() const {
463 __ pop(result_register());
464}
465
466
467void FullCodeGenerator::StackValueContext::PlugTOS() const {
468}
469
470
471void FullCodeGenerator::TestContext::PlugTOS() const {
472 // For simplicity we always test the accumulator register.
473 __ pop(result_register());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000474 codegen()->PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000475 codegen()->DoTest(true_label_, false_label_, fall_through_);
476}
477
478
479void FullCodeGenerator::EffectContext::PrepareTest(
480 Label* materialize_true,
481 Label* materialize_false,
482 Label** if_true,
483 Label** if_false,
484 Label** fall_through) const {
485 // In an effect context, the true and the false case branch to the
486 // same label.
487 *if_true = *if_false = *fall_through = materialize_true;
488}
489
490
491void FullCodeGenerator::AccumulatorValueContext::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::StackValueContext::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 = *fall_through = materialize_true;
509 *if_false = materialize_false;
510}
511
512
513void FullCodeGenerator::TestContext::PrepareTest(
514 Label* materialize_true,
515 Label* materialize_false,
516 Label** if_true,
517 Label** if_false,
518 Label** fall_through) const {
519 *if_true = true_label_;
520 *if_false = false_label_;
521 *fall_through = fall_through_;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000522}
523
524
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000525void FullCodeGenerator::VisitDeclarations(
526 ZoneList<Declaration*>* declarations) {
527 int length = declarations->length();
528 int globals = 0;
529 for (int i = 0; i < length; i++) {
530 Declaration* decl = declarations->at(i);
531 Variable* var = decl->proxy()->var();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000532 Slot* slot = var->AsSlot();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000533
534 // If it was not possible to allocate the variable at compile
535 // time, we need to "declare" it at runtime to make sure it
536 // actually exists in the local context.
537 if ((slot != NULL && slot->type() == Slot::LOOKUP) || !var->is_global()) {
538 VisitDeclaration(decl);
539 } else {
540 // Count global variables and functions for later processing
541 globals++;
542 }
543 }
544
545 // Compute array of global variable and function declarations.
546 // Do nothing in case of no declared global functions or variables.
547 if (globals > 0) {
548 Handle<FixedArray> array = Factory::NewFixedArray(2 * globals, TENURED);
549 for (int j = 0, i = 0; i < length; i++) {
550 Declaration* decl = declarations->at(i);
551 Variable* var = decl->proxy()->var();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000552 Slot* slot = var->AsSlot();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000553
554 if ((slot == NULL || slot->type() != Slot::LOOKUP) && var->is_global()) {
555 array->set(j++, *(var->name()));
556 if (decl->fun() == NULL) {
557 if (var->mode() == Variable::CONST) {
558 // In case this is const property use the hole.
559 array->set_the_hole(j++);
560 } else {
561 array->set_undefined(j++);
562 }
563 } else {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000564 Handle<SharedFunctionInfo> function =
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000565 Compiler::BuildFunctionInfo(decl->fun(), script());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000566 // Check for stack-overflow exception.
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000567 if (function.is_null()) {
568 SetStackOverflow();
569 return;
570 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000571 array->set(j++, *function);
572 }
573 }
574 }
575 // Invoke the platform-dependent code generator to do the actual
576 // declaration the global variables and functions.
577 DeclareGlobals(array);
578 }
579}
580
581
582void FullCodeGenerator::SetFunctionPosition(FunctionLiteral* fun) {
583 if (FLAG_debug_info) {
584 CodeGenerator::RecordPositions(masm_, fun->start_position());
585 }
586}
587
588
589void FullCodeGenerator::SetReturnPosition(FunctionLiteral* fun) {
590 if (FLAG_debug_info) {
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000591 CodeGenerator::RecordPositions(masm_, fun->end_position() - 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000592 }
593}
594
595
596void FullCodeGenerator::SetStatementPosition(Statement* stmt) {
597 if (FLAG_debug_info) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000598#ifdef ENABLE_DEBUGGER_SUPPORT
599 if (!Debugger::IsDebuggerActive()) {
600 CodeGenerator::RecordPositions(masm_, stmt->statement_pos());
601 } else {
602 // Check if the statement will be breakable without adding a debug break
603 // slot.
604 BreakableStatementChecker checker;
605 checker.Check(stmt);
606 // Record the statement position right here if the statement is not
607 // breakable. For breakable statements the actual recording of the
608 // position will be postponed to the breakable code (typically an IC).
609 bool position_recorded = CodeGenerator::RecordPositions(
610 masm_, stmt->statement_pos(), !checker.is_breakable());
611 // If the position recording did record a new position generate a debug
612 // break slot to make the statement breakable.
613 if (position_recorded) {
614 Debug::GenerateSlot(masm_);
615 }
616 }
617#else
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000618 CodeGenerator::RecordPositions(masm_, stmt->statement_pos());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000619#endif
620 }
621}
622
623
624void FullCodeGenerator::SetExpressionPosition(Expression* expr, int pos) {
625 if (FLAG_debug_info) {
626#ifdef ENABLE_DEBUGGER_SUPPORT
627 if (!Debugger::IsDebuggerActive()) {
628 CodeGenerator::RecordPositions(masm_, pos);
629 } else {
630 // Check if the expression will be breakable without adding a debug break
631 // slot.
632 BreakableStatementChecker checker;
633 checker.Check(expr);
634 // Record a statement position right here if the expression is not
635 // breakable. For breakable expressions the actual recording of the
636 // position will be postponed to the breakable code (typically an IC).
637 // NOTE this will record a statement position for something which might
638 // not be a statement. As stepping in the debugger will only stop at
639 // statement positions this is used for e.g. the condition expression of
640 // a do while loop.
641 bool position_recorded = CodeGenerator::RecordPositions(
642 masm_, pos, !checker.is_breakable());
643 // If the position recording did record a new position generate a debug
644 // break slot to make the statement breakable.
645 if (position_recorded) {
646 Debug::GenerateSlot(masm_);
647 }
648 }
649#else
650 CodeGenerator::RecordPositions(masm_, pos);
651#endif
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000652 }
653}
654
655
656void FullCodeGenerator::SetStatementPosition(int pos) {
657 if (FLAG_debug_info) {
658 CodeGenerator::RecordPositions(masm_, pos);
659 }
660}
661
662
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000663void FullCodeGenerator::SetSourcePosition(int pos) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000664 if (FLAG_debug_info && pos != RelocInfo::kNoPosition) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000665 masm_->positions_recorder()->RecordPosition(pos);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000666 }
667}
668
669
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000670// Lookup table for code generators for special runtime calls which are
671// generated inline.
672#define INLINE_FUNCTION_GENERATOR_ADDRESS(Name, argc, ressize) \
673 &FullCodeGenerator::Emit##Name,
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000674
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000675const FullCodeGenerator::InlineFunctionGenerator
676 FullCodeGenerator::kInlineFunctionGenerators[] = {
677 INLINE_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS)
678 INLINE_RUNTIME_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS)
679 };
680#undef INLINE_FUNCTION_GENERATOR_ADDRESS
681
682
683FullCodeGenerator::InlineFunctionGenerator
684 FullCodeGenerator::FindInlineFunctionGenerator(Runtime::FunctionId id) {
whesse@chromium.org023421e2010-12-21 12:19:12 +0000685 int lookup_index =
686 static_cast<int>(id) - static_cast<int>(Runtime::kFirstInlineFunction);
687 ASSERT(lookup_index >= 0);
688 ASSERT(static_cast<size_t>(lookup_index) <
689 ARRAY_SIZE(kInlineFunctionGenerators));
690 return kInlineFunctionGenerators[lookup_index];
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000691}
692
693
694void FullCodeGenerator::EmitInlineRuntimeCall(CallRuntime* node) {
695 ZoneList<Expression*>* args = node->arguments();
696 Handle<String> name = node->name();
697 Runtime::Function* function = node->function();
698 ASSERT(function != NULL);
699 ASSERT(function->intrinsic_type == Runtime::INLINE);
700 InlineFunctionGenerator generator =
701 FindInlineFunctionGenerator(function->function_id);
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000702 ((*this).*(generator))(args);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000703}
704
705
706void FullCodeGenerator::VisitBinaryOperation(BinaryOperation* expr) {
707 Comment cmnt(masm_, "[ BinaryOperation");
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000708 Token::Value op = expr->op();
709 Expression* left = expr->left();
710 Expression* right = expr->right();
ricow@chromium.org65fae842010-08-25 15:26:24 +0000711
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000712 OverwriteMode mode = NO_OVERWRITE;
713 if (left->ResultOverwriteAllowed()) {
714 mode = OVERWRITE_LEFT;
715 } else if (right->ResultOverwriteAllowed()) {
716 mode = OVERWRITE_RIGHT;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000717 }
718
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000719 switch (op) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000720 case Token::COMMA:
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000721 VisitForEffect(left);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000722 if (context()->IsTest()) ForwardBailoutToChild(expr);
723 context()->HandleExpression(right);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000724 break;
725
726 case Token::OR:
727 case Token::AND:
728 EmitLogicalOperation(expr);
729 break;
730
731 case Token::ADD:
732 case Token::SUB:
733 case Token::DIV:
734 case Token::MOD:
735 case Token::MUL:
736 case Token::BIT_OR:
737 case Token::BIT_AND:
738 case Token::BIT_XOR:
739 case Token::SHL:
740 case Token::SHR:
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000741 case Token::SAR: {
742 // Figure out if either of the operands is a constant.
743 ConstantOperand constant = ShouldInlineSmiCase(op)
744 ? GetConstantOperand(op, left, right)
745 : kNoConstants;
746
747 // Load only the operands that we need to materialize.
748 if (constant == kNoConstants) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000749 VisitForStackValue(left);
750 VisitForAccumulatorValue(right);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000751 } else if (constant == kRightConstant) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000752 VisitForAccumulatorValue(left);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000753 } else {
754 ASSERT(constant == kLeftConstant);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000755 VisitForAccumulatorValue(right);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000756 }
757
ricow@chromium.org65fae842010-08-25 15:26:24 +0000758 SetSourcePosition(expr->position());
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000759 if (ShouldInlineSmiCase(op)) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000760 EmitInlineSmiBinaryOp(expr, op, mode, left, right, constant);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000761 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000762 EmitBinaryOp(op, mode);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000763 }
ricow@chromium.org65fae842010-08-25 15:26:24 +0000764 break;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000765 }
ricow@chromium.org65fae842010-08-25 15:26:24 +0000766
767 default:
768 UNREACHABLE();
769 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000770}
771
772
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000773void FullCodeGenerator::EmitLogicalOperation(BinaryOperation* expr) {
774 Label eval_right, done;
775
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000776 context()->EmitLogicalLeft(expr, &eval_right, &done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000777
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000778 PrepareForBailoutForId(expr->RightId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000779 __ bind(&eval_right);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000780 if (context()->IsTest()) ForwardBailoutToChild(expr);
781 context()->HandleExpression(expr->right());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000782
783 __ bind(&done);
784}
785
786
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000787void FullCodeGenerator::EffectContext::EmitLogicalLeft(BinaryOperation* expr,
788 Label* eval_right,
789 Label* done) const {
790 if (expr->op() == Token::OR) {
791 codegen()->VisitForControl(expr->left(), done, eval_right, eval_right);
792 } else {
793 ASSERT(expr->op() == Token::AND);
794 codegen()->VisitForControl(expr->left(), eval_right, done, eval_right);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000795 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000796}
ricow@chromium.org65fae842010-08-25 15:26:24 +0000797
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000798
799void FullCodeGenerator::AccumulatorValueContext::EmitLogicalLeft(
800 BinaryOperation* expr,
801 Label* eval_right,
802 Label* done) const {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000803 HandleExpression(expr->left());
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000804 // We want the value in the accumulator for the test, and on the stack in case
805 // we need it.
806 __ push(result_register());
807 Label discard, restore;
808 if (expr->op() == Token::OR) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000809 codegen()->PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000810 codegen()->DoTest(&restore, &discard, &restore);
811 } else {
812 ASSERT(expr->op() == Token::AND);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000813 codegen()->PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000814 codegen()->DoTest(&discard, &restore, &restore);
815 }
816 __ bind(&restore);
817 __ pop(result_register());
818 __ jmp(done);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000819 __ bind(&discard);
820 __ Drop(1);
821}
822
823
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000824void FullCodeGenerator::StackValueContext::EmitLogicalLeft(
825 BinaryOperation* expr,
826 Label* eval_right,
827 Label* done) const {
828 codegen()->VisitForAccumulatorValue(expr->left());
829 // We want the value in the accumulator for the test, and on the stack in case
830 // we need it.
831 __ push(result_register());
832 Label discard;
833 if (expr->op() == Token::OR) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000834 codegen()->PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000835 codegen()->DoTest(done, &discard, &discard);
836 } else {
837 ASSERT(expr->op() == Token::AND);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000838 codegen()->PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000839 codegen()->DoTest(&discard, done, &discard);
840 }
841 __ bind(&discard);
842 __ Drop(1);
843}
844
845
846void FullCodeGenerator::TestContext::EmitLogicalLeft(BinaryOperation* expr,
847 Label* eval_right,
848 Label* done) const {
849 if (expr->op() == Token::OR) {
850 codegen()->VisitForControl(expr->left(),
851 true_label_, eval_right, eval_right);
852 } else {
853 ASSERT(expr->op() == Token::AND);
854 codegen()->VisitForControl(expr->left(),
855 eval_right, false_label_, eval_right);
856 }
857}
858
859
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000860void FullCodeGenerator::ForwardBailoutToChild(Expression* expr) {
861 if (!info_->HasDeoptimizationSupport()) return;
862 ASSERT(context()->IsTest());
863 ASSERT(expr == forward_bailout_stack_->expr());
864 forward_bailout_pending_ = forward_bailout_stack_;
865}
866
867
868void FullCodeGenerator::EffectContext::HandleExpression(
869 Expression* expr) const {
870 codegen()->HandleInNonTestContext(expr, NO_REGISTERS);
871}
872
873
874void FullCodeGenerator::AccumulatorValueContext::HandleExpression(
875 Expression* expr) const {
876 codegen()->HandleInNonTestContext(expr, TOS_REG);
877}
878
879
880void FullCodeGenerator::StackValueContext::HandleExpression(
881 Expression* expr) const {
882 codegen()->HandleInNonTestContext(expr, NO_REGISTERS);
883}
884
885
886void FullCodeGenerator::TestContext::HandleExpression(Expression* expr) const {
887 codegen()->VisitInTestContext(expr);
888}
889
890
891void FullCodeGenerator::HandleInNonTestContext(Expression* expr, State state) {
892 ASSERT(forward_bailout_pending_ == NULL);
893 AstVisitor::Visit(expr);
894 PrepareForBailout(expr, state);
895 // Forwarding bailouts to children is a one shot operation. It
896 // should have been processed at this point.
897 ASSERT(forward_bailout_pending_ == NULL);
898}
899
900
901void FullCodeGenerator::VisitInTestContext(Expression* expr) {
902 ForwardBailoutStack stack(expr, forward_bailout_pending_);
903 ForwardBailoutStack* saved = forward_bailout_stack_;
904 forward_bailout_pending_ = NULL;
905 forward_bailout_stack_ = &stack;
906 AstVisitor::Visit(expr);
907 forward_bailout_stack_ = saved;
908}
909
910
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000911void FullCodeGenerator::VisitBlock(Block* stmt) {
912 Comment cmnt(masm_, "[ Block");
913 Breakable nested_statement(this, stmt);
914 SetStatementPosition(stmt);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000915
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +0000916 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000917 VisitStatements(stmt->statements());
918 __ bind(nested_statement.break_target());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000919 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000920}
921
922
923void FullCodeGenerator::VisitExpressionStatement(ExpressionStatement* stmt) {
924 Comment cmnt(masm_, "[ ExpressionStatement");
925 SetStatementPosition(stmt);
926 VisitForEffect(stmt->expression());
927}
928
929
930void FullCodeGenerator::VisitEmptyStatement(EmptyStatement* stmt) {
931 Comment cmnt(masm_, "[ EmptyStatement");
932 SetStatementPosition(stmt);
933}
934
935
936void FullCodeGenerator::VisitIfStatement(IfStatement* stmt) {
937 Comment cmnt(masm_, "[ IfStatement");
938 SetStatementPosition(stmt);
939 Label then_part, else_part, done;
940
ricow@chromium.org65fae842010-08-25 15:26:24 +0000941 if (stmt->HasElseStatement()) {
942 VisitForControl(stmt->condition(), &then_part, &else_part, &then_part);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000943 PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000944 __ bind(&then_part);
945 Visit(stmt->then_statement());
946 __ jmp(&done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000947
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000948 PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000949 __ bind(&else_part);
950 Visit(stmt->else_statement());
951 } else {
952 VisitForControl(stmt->condition(), &then_part, &done, &then_part);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000953 PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000954 __ bind(&then_part);
955 Visit(stmt->then_statement());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000956
957 PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000958 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000959 __ bind(&done);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000960 PrepareForBailoutForId(stmt->id(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000961}
962
963
964void FullCodeGenerator::VisitContinueStatement(ContinueStatement* stmt) {
965 Comment cmnt(masm_, "[ ContinueStatement");
966 SetStatementPosition(stmt);
967 NestedStatement* current = nesting_stack_;
968 int stack_depth = 0;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000969 // When continuing, we clobber the unpredictable value in the accumulator
970 // with one that's safe for GC. If we hit an exit from the try block of
971 // try...finally on our way out, we will unconditionally preserve the
972 // accumulator on the stack.
973 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000974 while (!current->IsContinueTarget(stmt->target())) {
975 stack_depth = current->Exit(stack_depth);
976 current = current->outer();
977 }
978 __ Drop(stack_depth);
979
980 Iteration* loop = current->AsIteration();
981 __ jmp(loop->continue_target());
982}
983
984
985void FullCodeGenerator::VisitBreakStatement(BreakStatement* stmt) {
986 Comment cmnt(masm_, "[ BreakStatement");
987 SetStatementPosition(stmt);
988 NestedStatement* current = nesting_stack_;
989 int stack_depth = 0;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000990 // When breaking, we clobber the unpredictable value in the accumulator
991 // with one that's safe for GC. If we hit an exit from the try block of
992 // try...finally on our way out, we will unconditionally preserve the
993 // accumulator on the stack.
994 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000995 while (!current->IsBreakTarget(stmt->target())) {
996 stack_depth = current->Exit(stack_depth);
997 current = current->outer();
998 }
999 __ Drop(stack_depth);
1000
1001 Breakable* target = current->AsBreakable();
1002 __ jmp(target->break_target());
1003}
1004
1005
1006void FullCodeGenerator::VisitReturnStatement(ReturnStatement* stmt) {
1007 Comment cmnt(masm_, "[ ReturnStatement");
1008 SetStatementPosition(stmt);
1009 Expression* expr = stmt->expression();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001010 VisitForAccumulatorValue(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001011
1012 // Exit all nested statements.
1013 NestedStatement* current = nesting_stack_;
1014 int stack_depth = 0;
1015 while (current != NULL) {
1016 stack_depth = current->Exit(stack_depth);
1017 current = current->outer();
1018 }
1019 __ Drop(stack_depth);
1020
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001021 EmitReturnSequence();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001022}
1023
1024
1025void FullCodeGenerator::VisitWithEnterStatement(WithEnterStatement* stmt) {
1026 Comment cmnt(masm_, "[ WithEnterStatement");
1027 SetStatementPosition(stmt);
1028
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001029 VisitForStackValue(stmt->expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001030 if (stmt->is_catch_block()) {
1031 __ CallRuntime(Runtime::kPushCatchContext, 1);
1032 } else {
1033 __ CallRuntime(Runtime::kPushContext, 1);
1034 }
1035 // Both runtime calls return the new context in both the context and the
1036 // result registers.
1037
1038 // Update local stack frame context field.
1039 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
1040}
1041
1042
1043void FullCodeGenerator::VisitWithExitStatement(WithExitStatement* stmt) {
1044 Comment cmnt(masm_, "[ WithExitStatement");
1045 SetStatementPosition(stmt);
1046
1047 // Pop context.
1048 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1049 // Update local stack frame context field.
1050 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
1051}
1052
1053
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001054void FullCodeGenerator::VisitDoWhileStatement(DoWhileStatement* stmt) {
1055 Comment cmnt(masm_, "[ DoWhileStatement");
1056 SetStatementPosition(stmt);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001057 Label body, stack_check;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001058
1059 Iteration loop_statement(this, stmt);
1060 increment_loop_depth();
1061
1062 __ bind(&body);
1063 Visit(stmt->body());
1064
ricow@chromium.org65fae842010-08-25 15:26:24 +00001065 // Record the position of the do while condition and make sure it is
1066 // possible to break on the condition.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001067 __ bind(loop_statement.continue_target());
1068 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001069 SetExpressionPosition(stmt->cond(), stmt->condition_position());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001070 VisitForControl(stmt->cond(),
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001071 &stack_check,
ricow@chromium.org65fae842010-08-25 15:26:24 +00001072 loop_statement.break_target(),
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001073 &stack_check);
1074
1075 // Check stack before looping.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001076 PrepareForBailoutForId(stmt->BackEdgeId(), NO_REGISTERS);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001077 __ bind(&stack_check);
1078 EmitStackCheck(stmt);
1079 __ jmp(&body);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001080
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001081 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001082 __ bind(loop_statement.break_target());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001083 decrement_loop_depth();
1084}
1085
1086
1087void FullCodeGenerator::VisitWhileStatement(WhileStatement* stmt) {
1088 Comment cmnt(masm_, "[ WhileStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001089 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001090
1091 Iteration loop_statement(this, stmt);
1092 increment_loop_depth();
1093
1094 // Emit the test at the bottom of the loop.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001095 __ jmp(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001096
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001097 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001098 __ bind(&body);
1099 Visit(stmt->body());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001100
1101 // Emit the statement position here as this is where the while
1102 // statement code starts.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001103 __ bind(loop_statement.continue_target());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001104 SetStatementPosition(stmt);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001105
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001106 // Check stack before looping.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001107 EmitStackCheck(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001108
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001109 __ bind(&test);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001110 VisitForControl(stmt->cond(),
1111 &body,
1112 loop_statement.break_target(),
1113 loop_statement.break_target());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001114
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001115 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001116 __ bind(loop_statement.break_target());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001117 decrement_loop_depth();
1118}
1119
1120
1121void FullCodeGenerator::VisitForStatement(ForStatement* stmt) {
1122 Comment cmnt(masm_, "[ ForStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001123 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001124
1125 Iteration loop_statement(this, stmt);
1126 if (stmt->init() != NULL) {
1127 Visit(stmt->init());
1128 }
1129
1130 increment_loop_depth();
1131 // Emit the test at the bottom of the loop (even if empty).
1132 __ jmp(&test);
1133
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001134 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001135 __ bind(&body);
1136 Visit(stmt->body());
1137
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001138 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001139 __ bind(loop_statement.continue_target());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001140 SetStatementPosition(stmt);
1141 if (stmt->next() != NULL) {
1142 Visit(stmt->next());
1143 }
1144
ricow@chromium.org65fae842010-08-25 15:26:24 +00001145 // Emit the statement position here as this is where the for
1146 // statement code starts.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001147 SetStatementPosition(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001148
1149 // Check stack before looping.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001150 EmitStackCheck(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001151
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001152 __ bind(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001153 if (stmt->cond() != NULL) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00001154 VisitForControl(stmt->cond(),
1155 &body,
1156 loop_statement.break_target(),
1157 loop_statement.break_target());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001158 } else {
1159 __ jmp(&body);
1160 }
1161
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001162 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001163 __ bind(loop_statement.break_target());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001164 decrement_loop_depth();
1165}
1166
1167
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001168void FullCodeGenerator::VisitTryCatchStatement(TryCatchStatement* stmt) {
1169 Comment cmnt(masm_, "[ TryCatchStatement");
1170 SetStatementPosition(stmt);
1171 // The try block adds a handler to the exception handler chain
1172 // before entering, and removes it again when exiting normally.
1173 // If an exception is thrown during execution of the try block,
1174 // control is passed to the handler, which also consumes the handler.
1175 // At this point, the exception is in a register, and store it in
1176 // the temporary local variable (prints as ".catch-var") before
1177 // executing the catch block. The catch block has been rewritten
1178 // to introduce a new scope to bind the catch variable and to remove
1179 // that scope again afterwards.
1180
1181 Label try_handler_setup, catch_entry, done;
1182 __ Call(&try_handler_setup);
1183 // Try handler code, exception in result register.
1184
1185 // Store exception in local .catch variable before executing catch block.
1186 {
1187 // The catch variable is *always* a variable proxy for a local variable.
1188 Variable* catch_var = stmt->catch_var()->AsVariableProxy()->AsVariable();
1189 ASSERT_NOT_NULL(catch_var);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001190 Slot* variable_slot = catch_var->AsSlot();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001191 ASSERT_NOT_NULL(variable_slot);
1192 ASSERT_EQ(Slot::LOCAL, variable_slot->type());
1193 StoreToFrameField(SlotOffset(variable_slot), result_register());
1194 }
1195
1196 Visit(stmt->catch_block());
1197 __ jmp(&done);
1198
1199 // Try block code. Sets up the exception handler chain.
1200 __ bind(&try_handler_setup);
1201 {
1202 TryCatch try_block(this, &catch_entry);
1203 __ PushTryHandler(IN_JAVASCRIPT, TRY_CATCH_HANDLER);
1204 Visit(stmt->try_block());
1205 __ PopTryHandler();
1206 }
1207 __ bind(&done);
1208}
1209
1210
1211void FullCodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* stmt) {
1212 Comment cmnt(masm_, "[ TryFinallyStatement");
1213 SetStatementPosition(stmt);
1214 // Try finally is compiled by setting up a try-handler on the stack while
1215 // executing the try body, and removing it again afterwards.
1216 //
1217 // The try-finally construct can enter the finally block in three ways:
1218 // 1. By exiting the try-block normally. This removes the try-handler and
1219 // calls the finally block code before continuing.
1220 // 2. By exiting the try-block with a function-local control flow transfer
1221 // (break/continue/return). The site of the, e.g., break removes the
1222 // try handler and calls the finally block code before continuing
1223 // its outward control transfer.
1224 // 3. by exiting the try-block with a thrown exception.
1225 // This can happen in nested function calls. It traverses the try-handler
1226 // chain and consumes the try-handler entry before jumping to the
1227 // handler code. The handler code then calls the finally-block before
1228 // rethrowing the exception.
1229 //
1230 // The finally block must assume a return address on top of the stack
1231 // (or in the link register on ARM chips) and a value (return value or
1232 // exception) in the result register (rax/eax/r0), both of which must
1233 // be preserved. The return address isn't GC-safe, so it should be
1234 // cooked before GC.
1235 Label finally_entry;
1236 Label try_handler_setup;
1237
1238 // Setup the try-handler chain. Use a call to
1239 // Jump to try-handler setup and try-block code. Use call to put try-handler
1240 // address on stack.
1241 __ Call(&try_handler_setup);
1242 // Try handler code. Return address of call is pushed on handler stack.
1243 {
1244 // This code is only executed during stack-handler traversal when an
1245 // exception is thrown. The execption is in the result register, which
1246 // is retained by the finally block.
1247 // Call the finally block and then rethrow the exception.
1248 __ Call(&finally_entry);
1249 __ push(result_register());
1250 __ CallRuntime(Runtime::kReThrow, 1);
1251 }
1252
1253 __ bind(&finally_entry);
1254 {
1255 // Finally block implementation.
1256 Finally finally_block(this);
1257 EnterFinallyBlock();
1258 Visit(stmt->finally_block());
1259 ExitFinallyBlock(); // Return to the calling code.
1260 }
1261
1262 __ bind(&try_handler_setup);
1263 {
1264 // Setup try handler (stack pointer registers).
1265 TryFinally try_block(this, &finally_entry);
1266 __ PushTryHandler(IN_JAVASCRIPT, TRY_FINALLY_HANDLER);
1267 Visit(stmt->try_block());
1268 __ PopTryHandler();
1269 }
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001270 // Execute the finally block on the way out. Clobber the unpredictable
1271 // value in the accumulator with one that's safe for GC. The finally
1272 // block will unconditionally preserve the accumulator on the stack.
1273 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001274 __ Call(&finally_entry);
1275}
1276
1277
1278void FullCodeGenerator::VisitDebuggerStatement(DebuggerStatement* stmt) {
1279#ifdef ENABLE_DEBUGGER_SUPPORT
1280 Comment cmnt(masm_, "[ DebuggerStatement");
1281 SetStatementPosition(stmt);
1282
ager@chromium.org5c838252010-02-19 08:53:10 +00001283 __ DebugBreak();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001284 // Ignore the return value.
1285#endif
1286}
1287
1288
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001289void FullCodeGenerator::VisitConditional(Conditional* expr) {
1290 Comment cmnt(masm_, "[ Conditional");
1291 Label true_case, false_case, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001292 VisitForControl(expr->condition(), &true_case, &false_case, &true_case);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001293
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001294 PrepareForBailoutForId(expr->ThenId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001295 __ bind(&true_case);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001296 SetExpressionPosition(expr->then_expression(),
1297 expr->then_expression_position());
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001298 if (context()->IsTest()) {
1299 const TestContext* for_test = TestContext::cast(context());
1300 VisitForControl(expr->then_expression(),
1301 for_test->true_label(),
1302 for_test->false_label(),
1303 NULL);
1304 } else {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001305 context()->HandleExpression(expr->then_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001306 __ jmp(&done);
1307 }
1308
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001309 PrepareForBailoutForId(expr->ElseId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001310 __ bind(&false_case);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001311 if (context()->IsTest()) ForwardBailoutToChild(expr);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001312 SetExpressionPosition(expr->else_expression(),
1313 expr->else_expression_position());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001314 context()->HandleExpression(expr->else_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001315 // If control flow falls through Visit, merge it with true case here.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001316 if (!context()->IsTest()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001317 __ bind(&done);
1318 }
1319}
1320
1321
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001322void FullCodeGenerator::VisitLiteral(Literal* expr) {
1323 Comment cmnt(masm_, "[ Literal");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001324 context()->Plug(expr->handle());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001325}
1326
1327
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001328void FullCodeGenerator::VisitFunctionLiteral(FunctionLiteral* expr) {
1329 Comment cmnt(masm_, "[ FunctionLiteral");
1330
1331 // Build the function boilerplate and instantiate it.
1332 Handle<SharedFunctionInfo> function_info =
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001333 Compiler::BuildFunctionInfo(expr, script());
1334 if (function_info.is_null()) {
1335 SetStackOverflow();
1336 return;
1337 }
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001338 EmitNewClosure(function_info, expr->pretenure());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001339}
1340
1341
1342void FullCodeGenerator::VisitSharedFunctionInfoLiteral(
1343 SharedFunctionInfoLiteral* expr) {
1344 Comment cmnt(masm_, "[ SharedFunctionInfoLiteral");
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001345 EmitNewClosure(expr->shared_function_info(), false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001346}
1347
1348
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001349void FullCodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* expr) {
1350 // Call runtime routine to allocate the catch extension object and
1351 // assign the exception value to the catch variable.
1352 Comment cmnt(masm_, "[ CatchExtensionObject");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001353 VisitForStackValue(expr->key());
1354 VisitForStackValue(expr->value());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001355 // Create catch extension object.
1356 __ CallRuntime(Runtime::kCreateCatchExtensionObject, 2);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001357 context()->Plug(result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001358}
1359
1360
1361void FullCodeGenerator::VisitThrow(Throw* expr) {
1362 Comment cmnt(masm_, "[ Throw");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001363 VisitForStackValue(expr->exception());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001364 __ CallRuntime(Runtime::kThrow, 1);
1365 // Never returns here.
1366}
1367
1368
ricow@chromium.org65fae842010-08-25 15:26:24 +00001369void FullCodeGenerator::VisitIncrementOperation(IncrementOperation* expr) {
1370 UNREACHABLE();
1371}
1372
1373
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001374int FullCodeGenerator::TryFinally::Exit(int stack_depth) {
1375 // The macros used here must preserve the result register.
1376 __ Drop(stack_depth);
1377 __ PopTryHandler();
1378 __ Call(finally_entry_);
1379 return 0;
1380}
1381
1382
1383int FullCodeGenerator::TryCatch::Exit(int stack_depth) {
1384 // The macros used here must preserve the result register.
1385 __ Drop(stack_depth);
1386 __ PopTryHandler();
1387 return 0;
1388}
1389
ricow@chromium.org65fae842010-08-25 15:26:24 +00001390
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001391#undef __
1392
1393
1394} } // namespace v8::internal