blob: d963979ad867f0a8bc2d6c649af00383bed442da [file] [log] [blame]
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00001// Copyright 2012 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
karlklose@chromium.org44bc7082011-04-11 12:33:05 +000030#include "codegen.h"
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000031#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"
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +000038#include "scopeinfo.h"
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000039#include "stub-cache.h"
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000040
41namespace v8 {
42namespace internal {
43
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +000044void BreakableStatementChecker::Check(Statement* stmt) {
45 Visit(stmt);
46}
47
48
49void BreakableStatementChecker::Check(Expression* expr) {
50 Visit(expr);
51}
52
53
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +000054void BreakableStatementChecker::VisitVariableDeclaration(
55 VariableDeclaration* decl) {
56}
57
ulan@chromium.org812308e2012-02-29 15:58:45 +000058void BreakableStatementChecker::VisitFunctionDeclaration(
59 FunctionDeclaration* decl) {
60}
61
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +000062void BreakableStatementChecker::VisitModuleDeclaration(
63 ModuleDeclaration* decl) {
64}
65
ulan@chromium.org812308e2012-02-29 15:58:45 +000066void BreakableStatementChecker::VisitImportDeclaration(
67 ImportDeclaration* decl) {
68}
69
70void BreakableStatementChecker::VisitExportDeclaration(
71 ExportDeclaration* decl) {
72}
73
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +000074
75void BreakableStatementChecker::VisitModuleLiteral(ModuleLiteral* module) {
76}
77
78void BreakableStatementChecker::VisitModuleVariable(ModuleVariable* module) {
79}
80
81void BreakableStatementChecker::VisitModulePath(ModulePath* module) {
82}
83
84void BreakableStatementChecker::VisitModuleUrl(ModuleUrl* module) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +000085}
86
87
88void BreakableStatementChecker::VisitBlock(Block* stmt) {
89}
90
91
92void BreakableStatementChecker::VisitExpressionStatement(
93 ExpressionStatement* stmt) {
94 // Check if expression is breakable.
95 Visit(stmt->expression());
96}
97
98
99void BreakableStatementChecker::VisitEmptyStatement(EmptyStatement* stmt) {
100}
101
102
103void BreakableStatementChecker::VisitIfStatement(IfStatement* stmt) {
104 // If the condition is breakable the if statement is breakable.
105 Visit(stmt->condition());
106}
107
108
109void BreakableStatementChecker::VisitContinueStatement(
110 ContinueStatement* stmt) {
111}
112
113
114void BreakableStatementChecker::VisitBreakStatement(BreakStatement* stmt) {
115}
116
117
118void BreakableStatementChecker::VisitReturnStatement(ReturnStatement* stmt) {
119 // Return is breakable if the expression is.
120 Visit(stmt->expression());
121}
122
123
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000124void BreakableStatementChecker::VisitWithStatement(WithStatement* stmt) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000125 Visit(stmt->expression());
126}
127
128
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000129void BreakableStatementChecker::VisitSwitchStatement(SwitchStatement* stmt) {
130 // Switch statements breakable if the tag expression is.
131 Visit(stmt->tag());
132}
133
134
135void BreakableStatementChecker::VisitDoWhileStatement(DoWhileStatement* stmt) {
136 // Mark do while as breakable to avoid adding a break slot in front of it.
137 is_breakable_ = true;
138}
139
140
141void BreakableStatementChecker::VisitWhileStatement(WhileStatement* stmt) {
142 // Mark while statements breakable if the condition expression is.
143 Visit(stmt->cond());
144}
145
146
147void BreakableStatementChecker::VisitForStatement(ForStatement* stmt) {
148 // Mark for statements breakable if the condition expression is.
149 if (stmt->cond() != NULL) {
150 Visit(stmt->cond());
151 }
152}
153
154
155void BreakableStatementChecker::VisitForInStatement(ForInStatement* stmt) {
156 // Mark for in statements breakable if the enumerable expression is.
157 Visit(stmt->enumerable());
158}
159
160
161void BreakableStatementChecker::VisitTryCatchStatement(
162 TryCatchStatement* stmt) {
163 // Mark try catch as breakable to avoid adding a break slot in front of it.
164 is_breakable_ = true;
165}
166
167
168void BreakableStatementChecker::VisitTryFinallyStatement(
169 TryFinallyStatement* stmt) {
170 // Mark try finally as breakable to avoid adding a break slot in front of it.
171 is_breakable_ = true;
172}
173
174
175void BreakableStatementChecker::VisitDebuggerStatement(
176 DebuggerStatement* stmt) {
177 // The debugger statement is breakable.
178 is_breakable_ = true;
179}
180
181
182void BreakableStatementChecker::VisitFunctionLiteral(FunctionLiteral* expr) {
183}
184
185
186void BreakableStatementChecker::VisitSharedFunctionInfoLiteral(
187 SharedFunctionInfoLiteral* expr) {
188}
189
190
191void BreakableStatementChecker::VisitConditional(Conditional* expr) {
192}
193
194
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000195void BreakableStatementChecker::VisitVariableProxy(VariableProxy* expr) {
196}
197
198
199void BreakableStatementChecker::VisitLiteral(Literal* expr) {
200}
201
202
203void BreakableStatementChecker::VisitRegExpLiteral(RegExpLiteral* expr) {
204}
205
206
207void BreakableStatementChecker::VisitObjectLiteral(ObjectLiteral* expr) {
208}
209
210
211void BreakableStatementChecker::VisitArrayLiteral(ArrayLiteral* expr) {
212}
213
214
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000215void BreakableStatementChecker::VisitAssignment(Assignment* expr) {
216 // If assigning to a property (including a global property) the assignment is
217 // breakable.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000218 VariableProxy* proxy = expr->target()->AsVariableProxy();
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000219 Property* prop = expr->target()->AsProperty();
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000220 if (prop != NULL || (proxy != NULL && proxy->var()->IsUnallocated())) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000221 is_breakable_ = true;
222 return;
223 }
224
225 // Otherwise the assignment is breakable if the assigned value is.
226 Visit(expr->value());
227}
228
229
230void BreakableStatementChecker::VisitThrow(Throw* expr) {
231 // Throw is breakable if the expression is.
232 Visit(expr->exception());
233}
234
235
236void BreakableStatementChecker::VisitProperty(Property* expr) {
237 // Property load is breakable.
238 is_breakable_ = true;
239}
240
241
242void BreakableStatementChecker::VisitCall(Call* expr) {
243 // Function calls both through IC and call stub are breakable.
244 is_breakable_ = true;
245}
246
247
248void BreakableStatementChecker::VisitCallNew(CallNew* expr) {
249 // Function calls through new are breakable.
250 is_breakable_ = true;
251}
252
253
254void BreakableStatementChecker::VisitCallRuntime(CallRuntime* expr) {
255}
256
257
258void BreakableStatementChecker::VisitUnaryOperation(UnaryOperation* expr) {
259 Visit(expr->expression());
260}
261
262
263void BreakableStatementChecker::VisitCountOperation(CountOperation* expr) {
264 Visit(expr->expression());
265}
266
267
268void BreakableStatementChecker::VisitBinaryOperation(BinaryOperation* expr) {
269 Visit(expr->left());
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +0000270 if (expr->op() != Token::AND &&
271 expr->op() != Token::OR) {
272 Visit(expr->right());
273 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000274}
275
276
277void BreakableStatementChecker::VisitCompareOperation(CompareOperation* expr) {
278 Visit(expr->left());
279 Visit(expr->right());
280}
281
282
283void BreakableStatementChecker::VisitThisFunction(ThisFunction* expr) {
284}
285
286
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000287#define __ ACCESS_MASM(masm())
288
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000289bool FullCodeGenerator::MakeCode(CompilationInfo* info) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000290 Isolate* isolate = info->isolate();
ager@chromium.org5c838252010-02-19 08:53:10 +0000291 Handle<Script> script = info->script();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000292 if (!script->IsUndefined() && !script->source()->IsUndefined()) {
293 int len = String::cast(script->source())->length();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000294 isolate->counters()->total_full_codegen_source_size()->Increment(len);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000295 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000296 if (FLAG_trace_codegen) {
297 PrintF("Full Compiler - ");
298 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000299 CodeGenerator::MakeCodePrologue(info);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000300 const int kInitialBufferSize = 4 * KB;
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000301 MacroAssembler masm(info->isolate(), NULL, kInitialBufferSize);
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000302#ifdef ENABLE_GDB_JIT_INTERFACE
303 masm.positions_recorder()->StartGDBJITLineInfoRecording();
304#endif
ager@chromium.org5c838252010-02-19 08:53:10 +0000305
yangguo@chromium.org56454712012-02-16 15:33:53 +0000306 FullCodeGenerator cgen(&masm, info);
307 cgen.Generate();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000308 if (cgen.HasStackOverflow()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000309 ASSERT(!isolate->has_pending_exception());
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000310 return false;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000311 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000312 unsigned table_offset = cgen.EmitStackCheckTable();
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000313
lrn@chromium.org34e60782011-09-15 07:25:40 +0000314 Code::Flags flags = Code::ComputeFlags(Code::FUNCTION);
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000315 Handle<Code> code = CodeGenerator::MakeCodeEpilogue(&masm, flags, info);
rossberg@chromium.org2c067b12012-03-19 11:01:52 +0000316 code->set_optimizable(info->IsOptimizable() &&
317 !info->function()->flags()->Contains(kDontOptimize));
yangguo@chromium.orga7d3df92012-02-27 11:46:55 +0000318 code->set_self_optimization_header(cgen.has_self_optimization_header_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000319 cgen.PopulateDeoptimizationData(code);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000320 cgen.PopulateTypeFeedbackInfo(code);
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000321 cgen.PopulateTypeFeedbackCells(code);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000322 code->set_has_deoptimization_support(info->HasDeoptimizationSupport());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +0000323 code->set_handler_table(*cgen.handler_table());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000324#ifdef ENABLE_DEBUGGER_SUPPORT
lrn@chromium.org34e60782011-09-15 07:25:40 +0000325 code->set_has_debug_break_slots(
326 info->isolate()->debugger()->IsDebuggerActive());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000327 code->set_compiled_optimizable(info->IsOptimizable());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000328#endif // ENABLE_DEBUGGER_SUPPORT
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000329 code->set_allow_osr_at_loop_nesting_level(0);
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000330 code->set_stack_check_table_offset(table_offset);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000331 CodeGenerator::PrintCode(code, info);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000332 info->SetCode(code); // May be an empty handle.
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000333 if (!code.is_null()) {
334 isolate->runtime_profiler()->NotifyCodeGenerated(code->instruction_size());
335 }
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000336#ifdef ENABLE_GDB_JIT_INTERFACE
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000337 if (FLAG_gdbjit && !code.is_null()) {
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000338 GDBJITLineInfo* lineinfo =
339 masm.positions_recorder()->DetachGDBJITLineInfo();
340
341 GDBJIT(RegisterDetailedLineInfo(*code, lineinfo));
342 }
343#endif
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000344 return !code.is_null();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000345}
346
347
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000348unsigned FullCodeGenerator::EmitStackCheckTable() {
349 // The stack check table consists of a length (in number of entries)
350 // field, and then a sequence of entries. Each entry is a pair of AST id
351 // and code-relative pc offset.
352 masm()->Align(kIntSize);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000353 unsigned offset = masm()->pc_offset();
354 unsigned length = stack_checks_.length();
355 __ dd(length);
356 for (unsigned i = 0; i < length; ++i) {
357 __ dd(stack_checks_[i].id);
358 __ dd(stack_checks_[i].pc_and_state);
359 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000360 return offset;
361}
362
363
364void FullCodeGenerator::PopulateDeoptimizationData(Handle<Code> code) {
365 // Fill in the deoptimization information.
366 ASSERT(info_->HasDeoptimizationSupport() || bailout_entries_.is_empty());
367 if (!info_->HasDeoptimizationSupport()) return;
368 int length = bailout_entries_.length();
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000369 Handle<DeoptimizationOutputData> data = isolate()->factory()->
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000370 NewDeoptimizationOutputData(length, TENURED);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000371 for (int i = 0; i < length; i++) {
372 data->SetAstId(i, Smi::FromInt(bailout_entries_[i].id));
373 data->SetPcAndState(i, Smi::FromInt(bailout_entries_[i].pc_and_state));
374 }
375 code->set_deoptimization_data(*data);
376}
377
378
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000379void FullCodeGenerator::PopulateTypeFeedbackInfo(Handle<Code> code) {
380 Handle<TypeFeedbackInfo> info = isolate()->factory()->NewTypeFeedbackInfo();
381 info->set_ic_total_count(ic_total_count_);
yangguo@chromium.orga7d3df92012-02-27 11:46:55 +0000382 ASSERT(!isolate()->heap()->InNewSpace(*info));
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000383 code->set_type_feedback_info(*info);
384}
385
386
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000387void FullCodeGenerator::PopulateTypeFeedbackCells(Handle<Code> code) {
388 if (type_feedback_cells_.is_empty()) return;
389 int length = type_feedback_cells_.length();
390 int array_size = TypeFeedbackCells::LengthOfFixedArray(length);
391 Handle<TypeFeedbackCells> cache = Handle<TypeFeedbackCells>::cast(
392 isolate()->factory()->NewFixedArray(array_size, TENURED));
393 for (int i = 0; i < length; i++) {
394 cache->SetAstId(i, Smi::FromInt(type_feedback_cells_[i].ast_id));
395 cache->SetCell(i, *type_feedback_cells_[i].cell);
396 }
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000397 TypeFeedbackInfo::cast(code->type_feedback_info())->set_type_feedback_cells(
398 *cache);
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000399}
400
401
402
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000403void FullCodeGenerator::PrepareForBailout(Expression* node, State state) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000404 PrepareForBailoutForId(node->id(), state);
405}
406
407
408void FullCodeGenerator::RecordJSReturnSite(Call* call) {
409 // We record the offset of the function return so we can rebuild the frame
410 // if the function was inlined, i.e., this is the return address in the
411 // inlined function's frame.
412 //
413 // The state is ignored. We defensively set it to TOS_REG, which is the
414 // real state of the unoptimized code at the return site.
415 PrepareForBailoutForId(call->ReturnId(), TOS_REG);
416#ifdef DEBUG
417 // In debug builds, mark the return so we can verify that this function
418 // was called.
419 ASSERT(!call->return_is_recorded_);
420 call->return_is_recorded_ = true;
421#endif
422}
423
424
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +0000425void FullCodeGenerator::PrepareForBailoutForId(unsigned id, State state) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000426 // There's no need to prepare this code for bailouts from already optimized
427 // code or code that can't be optimized.
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000428 if (!info_->HasDeoptimizationSupport()) return;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000429 unsigned pc_and_state =
430 StateField::encode(state) | PcField::encode(masm_->pc_offset());
yangguo@chromium.org56454712012-02-16 15:33:53 +0000431 ASSERT(Smi::IsValid(pc_and_state));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000432 BailoutEntry entry = { id, pc_and_state };
433#ifdef DEBUG
yangguo@chromium.org659ceec2012-01-26 07:37:54 +0000434 if (FLAG_enable_slow_asserts) {
435 // Assert that we don't have multiple bailout entries for the same node.
436 for (int i = 0; i < bailout_entries_.length(); i++) {
437 if (bailout_entries_.at(i).id == entry.id) {
438 AstPrinter printer;
439 PrintF("%s", printer.PrintProgram(info_->function()));
440 UNREACHABLE();
441 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000442 }
443 }
444#endif // DEBUG
445 bailout_entries_.Add(entry);
446}
447
448
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000449void FullCodeGenerator::RecordTypeFeedbackCell(
450 unsigned id, Handle<JSGlobalPropertyCell> cell) {
451 TypeFeedbackCellEntry entry = { id, cell };
452 type_feedback_cells_.Add(entry);
453}
454
455
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +0000456void FullCodeGenerator::RecordStackCheck(unsigned ast_id) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000457 // The pc offset does not need to be encoded and packed together with a
458 // state.
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +0000459 ASSERT(masm_->pc_offset() > 0);
460 BailoutEntry entry = { ast_id, static_cast<unsigned>(masm_->pc_offset()) };
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000461 stack_checks_.Add(entry);
462}
463
464
ricow@chromium.org65fae842010-08-25 15:26:24 +0000465bool FullCodeGenerator::ShouldInlineSmiCase(Token::Value op) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000466 // Inline smi case inside loops, but not division and modulo which
467 // are too complicated and take up too much space.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000468 if (op == Token::DIV ||op == Token::MOD) return false;
469 if (FLAG_always_inline_smi_code) return true;
470 return loop_depth_ > 0;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000471}
472
473
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000474void FullCodeGenerator::EffectContext::Plug(Register reg) const {
475}
476
477
478void FullCodeGenerator::AccumulatorValueContext::Plug(Register reg) const {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000479 __ Move(result_register(), reg);
480}
481
482
483void FullCodeGenerator::StackValueContext::Plug(Register reg) const {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000484 __ push(reg);
485}
486
487
488void FullCodeGenerator::TestContext::Plug(Register reg) const {
489 // For simplicity we always test the accumulator register.
490 __ Move(result_register(), reg);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000491 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000492 codegen()->DoTest(this);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000493}
494
495
496void FullCodeGenerator::EffectContext::PlugTOS() const {
497 __ Drop(1);
498}
499
500
501void FullCodeGenerator::AccumulatorValueContext::PlugTOS() const {
502 __ pop(result_register());
503}
504
505
506void FullCodeGenerator::StackValueContext::PlugTOS() const {
507}
508
509
510void FullCodeGenerator::TestContext::PlugTOS() const {
511 // For simplicity we always test the accumulator register.
512 __ pop(result_register());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000513 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000514 codegen()->DoTest(this);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000515}
516
517
518void FullCodeGenerator::EffectContext::PrepareTest(
519 Label* materialize_true,
520 Label* materialize_false,
521 Label** if_true,
522 Label** if_false,
523 Label** fall_through) const {
524 // In an effect context, the true and the false case branch to the
525 // same label.
526 *if_true = *if_false = *fall_through = materialize_true;
527}
528
529
530void FullCodeGenerator::AccumulatorValueContext::PrepareTest(
531 Label* materialize_true,
532 Label* materialize_false,
533 Label** if_true,
534 Label** if_false,
535 Label** fall_through) const {
536 *if_true = *fall_through = materialize_true;
537 *if_false = materialize_false;
538}
539
540
541void FullCodeGenerator::StackValueContext::PrepareTest(
542 Label* materialize_true,
543 Label* materialize_false,
544 Label** if_true,
545 Label** if_false,
546 Label** fall_through) const {
547 *if_true = *fall_through = materialize_true;
548 *if_false = materialize_false;
549}
550
551
552void FullCodeGenerator::TestContext::PrepareTest(
553 Label* materialize_true,
554 Label* materialize_false,
555 Label** if_true,
556 Label** if_false,
557 Label** fall_through) const {
558 *if_true = true_label_;
559 *if_false = false_label_;
560 *fall_through = fall_through_;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000561}
562
563
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000564void FullCodeGenerator::DoTest(const TestContext* context) {
565 DoTest(context->condition(),
566 context->true_label(),
567 context->false_label(),
568 context->fall_through());
569}
570
571
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000572void FullCodeGenerator::VisitDeclarations(
573 ZoneList<Declaration*>* declarations) {
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000574 int save_global_count = global_count_;
575 global_count_ = 0;
576
577 AstVisitor::VisitDeclarations(declarations);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000578
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000579 // Batch declare global functions and variables.
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000580 if (global_count_ > 0) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000581 Handle<FixedArray> array =
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000582 isolate()->factory()->NewFixedArray(2 * global_count_, TENURED);
583 int length = declarations->length();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000584 for (int j = 0, i = 0; i < length; i++) {
ulan@chromium.org812308e2012-02-29 15:58:45 +0000585 Declaration* decl = declarations->at(i);
586 Variable* var = decl->proxy()->var();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000587
ulan@chromium.org812308e2012-02-29 15:58:45 +0000588 if (var->IsUnallocated()) {
589 array->set(j++, *(var->name()));
590 FunctionDeclaration* fun_decl = decl->AsFunctionDeclaration();
591 if (fun_decl == NULL) {
592 if (var->binding_needs_init()) {
593 // In case this binding needs initialization use the hole.
594 array->set_the_hole(j++);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000595 } else {
ulan@chromium.org812308e2012-02-29 15:58:45 +0000596 array->set_undefined(j++);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000597 }
ulan@chromium.org812308e2012-02-29 15:58:45 +0000598 } else {
599 Handle<SharedFunctionInfo> function =
600 Compiler::BuildFunctionInfo(fun_decl->fun(), script());
601 // Check for stack-overflow exception.
602 if (function.is_null()) {
603 SetStackOverflow();
604 return;
605 }
606 array->set(j++, *function);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000607 }
608 }
609 }
610 // Invoke the platform-dependent code generator to do the actual
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000611 // declaration the global functions and variables.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000612 DeclareGlobals(array);
613 }
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000614
615 global_count_ = save_global_count;
616}
617
618
619void FullCodeGenerator::VisitVariableDeclaration(VariableDeclaration* decl) {
ulan@chromium.org812308e2012-02-29 15:58:45 +0000620 EmitDeclaration(decl->proxy(), decl->mode(), NULL);
621}
622
623
624void FullCodeGenerator::VisitFunctionDeclaration(FunctionDeclaration* decl) {
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000625 EmitDeclaration(decl->proxy(), decl->mode(), decl->fun());
626}
627
628
629void FullCodeGenerator::VisitModuleDeclaration(ModuleDeclaration* decl) {
ulan@chromium.org812308e2012-02-29 15:58:45 +0000630 EmitDeclaration(decl->proxy(), decl->mode(), NULL);
631}
632
633
634void FullCodeGenerator::VisitImportDeclaration(ImportDeclaration* decl) {
635 EmitDeclaration(decl->proxy(), decl->mode(), NULL);
636}
637
638
639void FullCodeGenerator::VisitExportDeclaration(ExportDeclaration* decl) {
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000640 // TODO(rossberg)
641}
642
643
644void FullCodeGenerator::VisitModuleLiteral(ModuleLiteral* module) {
645 // TODO(rossberg)
646}
647
648
649void FullCodeGenerator::VisitModuleVariable(ModuleVariable* module) {
650 // TODO(rossberg)
651}
652
653
654void FullCodeGenerator::VisitModulePath(ModulePath* module) {
655 // TODO(rossberg)
656}
657
658
659void FullCodeGenerator::VisitModuleUrl(ModuleUrl* decl) {
660 // TODO(rossberg)
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000661}
662
663
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000664int FullCodeGenerator::DeclareGlobalsFlags() {
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000665 ASSERT(DeclareGlobalsLanguageMode::is_valid(language_mode()));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000666 return DeclareGlobalsEvalFlag::encode(is_eval()) |
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000667 DeclareGlobalsNativeFlag::encode(is_native()) |
668 DeclareGlobalsLanguageMode::encode(language_mode());
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000669}
670
671
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000672void FullCodeGenerator::SetFunctionPosition(FunctionLiteral* fun) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000673 CodeGenerator::RecordPositions(masm_, fun->start_position());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000674}
675
676
677void FullCodeGenerator::SetReturnPosition(FunctionLiteral* fun) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000678 CodeGenerator::RecordPositions(masm_, fun->end_position() - 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000679}
680
681
682void FullCodeGenerator::SetStatementPosition(Statement* stmt) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000683#ifdef ENABLE_DEBUGGER_SUPPORT
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000684 if (!isolate()->debugger()->IsDebuggerActive()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000685 CodeGenerator::RecordPositions(masm_, stmt->statement_pos());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000686 } else {
687 // Check if the statement will be breakable without adding a debug break
688 // slot.
689 BreakableStatementChecker checker;
690 checker.Check(stmt);
691 // Record the statement position right here if the statement is not
692 // breakable. For breakable statements the actual recording of the
693 // position will be postponed to the breakable code (typically an IC).
694 bool position_recorded = CodeGenerator::RecordPositions(
695 masm_, stmt->statement_pos(), !checker.is_breakable());
696 // If the position recording did record a new position generate a debug
697 // break slot to make the statement breakable.
698 if (position_recorded) {
699 Debug::GenerateSlot(masm_);
700 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000701 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000702#else
703 CodeGenerator::RecordPositions(masm_, stmt->statement_pos());
704#endif
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000705}
706
707
708void FullCodeGenerator::SetExpressionPosition(Expression* expr, int pos) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000709#ifdef ENABLE_DEBUGGER_SUPPORT
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000710 if (!isolate()->debugger()->IsDebuggerActive()) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000711 CodeGenerator::RecordPositions(masm_, pos);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000712 } else {
713 // Check if the expression will be breakable without adding a debug break
714 // slot.
715 BreakableStatementChecker checker;
716 checker.Check(expr);
717 // Record a statement position right here if the expression is not
718 // breakable. For breakable expressions the actual recording of the
719 // position will be postponed to the breakable code (typically an IC).
720 // NOTE this will record a statement position for something which might
721 // not be a statement. As stepping in the debugger will only stop at
722 // statement positions this is used for e.g. the condition expression of
723 // a do while loop.
724 bool position_recorded = CodeGenerator::RecordPositions(
725 masm_, pos, !checker.is_breakable());
726 // If the position recording did record a new position generate a debug
727 // break slot to make the statement breakable.
728 if (position_recorded) {
729 Debug::GenerateSlot(masm_);
730 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000731 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000732#else
733 CodeGenerator::RecordPositions(masm_, pos);
734#endif
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000735}
736
737
738void FullCodeGenerator::SetStatementPosition(int pos) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000739 CodeGenerator::RecordPositions(masm_, pos);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000740}
741
742
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000743void FullCodeGenerator::SetSourcePosition(int pos) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000744 if (pos != RelocInfo::kNoPosition) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000745 masm_->positions_recorder()->RecordPosition(pos);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000746 }
747}
748
749
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000750// Lookup table for code generators for special runtime calls which are
751// generated inline.
752#define INLINE_FUNCTION_GENERATOR_ADDRESS(Name, argc, ressize) \
753 &FullCodeGenerator::Emit##Name,
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000754
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000755const FullCodeGenerator::InlineFunctionGenerator
756 FullCodeGenerator::kInlineFunctionGenerators[] = {
757 INLINE_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS)
758 INLINE_RUNTIME_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS)
759 };
760#undef INLINE_FUNCTION_GENERATOR_ADDRESS
761
762
763FullCodeGenerator::InlineFunctionGenerator
764 FullCodeGenerator::FindInlineFunctionGenerator(Runtime::FunctionId id) {
whesse@chromium.org023421e2010-12-21 12:19:12 +0000765 int lookup_index =
766 static_cast<int>(id) - static_cast<int>(Runtime::kFirstInlineFunction);
767 ASSERT(lookup_index >= 0);
768 ASSERT(static_cast<size_t>(lookup_index) <
769 ARRAY_SIZE(kInlineFunctionGenerators));
770 return kInlineFunctionGenerators[lookup_index];
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000771}
772
773
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000774void FullCodeGenerator::EmitInlineRuntimeCall(CallRuntime* expr) {
775 const Runtime::Function* function = expr->function();
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000776 ASSERT(function != NULL);
777 ASSERT(function->intrinsic_type == Runtime::INLINE);
778 InlineFunctionGenerator generator =
779 FindInlineFunctionGenerator(function->function_id);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000780 ((*this).*(generator))(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000781}
782
783
784void FullCodeGenerator::VisitBinaryOperation(BinaryOperation* expr) {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000785 switch (expr->op()) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000786 case Token::COMMA:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000787 return VisitComma(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000788 case Token::OR:
789 case Token::AND:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000790 return VisitLogicalExpression(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000791 default:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000792 return VisitArithmeticExpression(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000793 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000794}
795
796
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000797void FullCodeGenerator::VisitInDuplicateContext(Expression* expr) {
798 if (context()->IsEffect()) {
799 VisitForEffect(expr);
800 } else if (context()->IsAccumulatorValue()) {
801 VisitForAccumulatorValue(expr);
802 } else if (context()->IsStackValue()) {
803 VisitForStackValue(expr);
804 } else if (context()->IsTest()) {
805 const TestContext* test = TestContext::cast(context());
806 VisitForControl(expr, test->true_label(), test->false_label(),
807 test->fall_through());
808 }
809}
810
811
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000812void FullCodeGenerator::VisitComma(BinaryOperation* expr) {
813 Comment cmnt(masm_, "[ Comma");
814 VisitForEffect(expr->left());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000815 VisitInDuplicateContext(expr->right());
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000816}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000817
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000818
819void FullCodeGenerator::VisitLogicalExpression(BinaryOperation* expr) {
820 bool is_logical_and = expr->op() == Token::AND;
821 Comment cmnt(masm_, is_logical_and ? "[ Logical AND" : "[ Logical OR");
822 Expression* left = expr->left();
823 Expression* right = expr->right();
824 int right_id = expr->RightId();
825 Label done;
826
827 if (context()->IsTest()) {
828 Label eval_right;
829 const TestContext* test = TestContext::cast(context());
830 if (is_logical_and) {
831 VisitForControl(left, &eval_right, test->false_label(), &eval_right);
832 } else {
833 VisitForControl(left, test->true_label(), &eval_right, &eval_right);
834 }
835 PrepareForBailoutForId(right_id, NO_REGISTERS);
836 __ bind(&eval_right);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000837
838 } else if (context()->IsAccumulatorValue()) {
839 VisitForAccumulatorValue(left);
840 // We want the value in the accumulator for the test, and on the stack in
841 // case we need it.
842 __ push(result_register());
843 Label discard, restore;
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000844 if (is_logical_and) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000845 DoTest(left, &discard, &restore, &restore);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000846 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000847 DoTest(left, &restore, &discard, &restore);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000848 }
849 __ bind(&restore);
850 __ pop(result_register());
851 __ jmp(&done);
852 __ bind(&discard);
853 __ Drop(1);
854 PrepareForBailoutForId(right_id, NO_REGISTERS);
855
856 } else if (context()->IsStackValue()) {
857 VisitForAccumulatorValue(left);
858 // We want the value in the accumulator for the test, and on the stack in
859 // case we need it.
860 __ push(result_register());
861 Label discard;
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000862 if (is_logical_and) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000863 DoTest(left, &discard, &done, &discard);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000864 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000865 DoTest(left, &done, &discard, &discard);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000866 }
867 __ bind(&discard);
868 __ Drop(1);
869 PrepareForBailoutForId(right_id, NO_REGISTERS);
870
871 } else {
872 ASSERT(context()->IsEffect());
873 Label eval_right;
874 if (is_logical_and) {
875 VisitForControl(left, &eval_right, &done, &eval_right);
876 } else {
877 VisitForControl(left, &done, &eval_right, &eval_right);
878 }
879 PrepareForBailoutForId(right_id, NO_REGISTERS);
880 __ bind(&eval_right);
881 }
882
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000883 VisitInDuplicateContext(right);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000884 __ bind(&done);
885}
886
887
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000888void FullCodeGenerator::VisitArithmeticExpression(BinaryOperation* expr) {
889 Token::Value op = expr->op();
890 Comment cmnt(masm_, "[ ArithmeticExpression");
891 Expression* left = expr->left();
892 Expression* right = expr->right();
893 OverwriteMode mode =
894 left->ResultOverwriteAllowed()
895 ? OVERWRITE_LEFT
896 : (right->ResultOverwriteAllowed() ? OVERWRITE_RIGHT : NO_OVERWRITE);
897
898 VisitForStackValue(left);
899 VisitForAccumulatorValue(right);
900
901 SetSourcePosition(expr->position());
902 if (ShouldInlineSmiCase(op)) {
903 EmitInlineSmiBinaryOp(expr, op, mode, left, right);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000904 } else {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000905 EmitBinaryOp(expr, op, mode);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000906 }
907}
908
909
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000910void FullCodeGenerator::VisitBlock(Block* stmt) {
911 Comment cmnt(masm_, "[ Block");
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000912 NestedBlock nested_block(this, stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000913 SetStatementPosition(stmt);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000914
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000915 Scope* saved_scope = scope();
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000916 // Push a block context when entering a block with block scoped variables.
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000917 if (stmt->block_scope() != NULL) {
918 { Comment cmnt(masm_, "[ Extend block context");
919 scope_ = stmt->block_scope();
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000920 Handle<ScopeInfo> scope_info = scope_->GetScopeInfo();
921 int heap_slots = scope_info->ContextLength() - Context::MIN_CONTEXT_SLOTS;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000922 __ Push(scope_info);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000923 PushFunctionArgumentForContextAllocation();
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000924 if (heap_slots <= FastNewBlockContextStub::kMaximumSlots) {
925 FastNewBlockContextStub stub(heap_slots);
926 __ CallStub(&stub);
927 } else {
928 __ CallRuntime(Runtime::kPushBlockContext, 2);
929 }
930
931 // Replace the context stored in the frame.
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000932 StoreToFrameField(StandardFrameConstants::kContextOffset,
933 context_register());
934 }
935 { Comment cmnt(masm_, "[ Declarations");
936 VisitDeclarations(scope_->declarations());
937 }
938 }
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +0000939 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000940 VisitStatements(stmt->statements());
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000941 scope_ = saved_scope;
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000942 __ bind(nested_block.break_label());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000943 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000944
945 // Pop block context if necessary.
946 if (stmt->block_scope() != NULL) {
947 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
948 // Update local stack frame context field.
949 StoreToFrameField(StandardFrameConstants::kContextOffset,
950 context_register());
951 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000952}
953
954
955void FullCodeGenerator::VisitExpressionStatement(ExpressionStatement* stmt) {
956 Comment cmnt(masm_, "[ ExpressionStatement");
957 SetStatementPosition(stmt);
958 VisitForEffect(stmt->expression());
959}
960
961
962void FullCodeGenerator::VisitEmptyStatement(EmptyStatement* stmt) {
963 Comment cmnt(masm_, "[ EmptyStatement");
964 SetStatementPosition(stmt);
965}
966
967
968void FullCodeGenerator::VisitIfStatement(IfStatement* stmt) {
969 Comment cmnt(masm_, "[ IfStatement");
970 SetStatementPosition(stmt);
971 Label then_part, else_part, done;
972
ricow@chromium.org65fae842010-08-25 15:26:24 +0000973 if (stmt->HasElseStatement()) {
974 VisitForControl(stmt->condition(), &then_part, &else_part, &then_part);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000975 PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000976 __ bind(&then_part);
977 Visit(stmt->then_statement());
978 __ jmp(&done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000979
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000980 PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000981 __ bind(&else_part);
982 Visit(stmt->else_statement());
983 } else {
984 VisitForControl(stmt->condition(), &then_part, &done, &then_part);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000985 PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000986 __ bind(&then_part);
987 Visit(stmt->then_statement());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000988
989 PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000990 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000991 __ bind(&done);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000992 PrepareForBailoutForId(stmt->IfId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000993}
994
995
996void FullCodeGenerator::VisitContinueStatement(ContinueStatement* stmt) {
997 Comment cmnt(masm_, "[ ContinueStatement");
998 SetStatementPosition(stmt);
999 NestedStatement* current = nesting_stack_;
1000 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001001 int context_length = 0;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001002 // When continuing, we clobber the unpredictable value in the accumulator
1003 // with one that's safe for GC. If we hit an exit from the try block of
1004 // try...finally on our way out, we will unconditionally preserve the
1005 // accumulator on the stack.
1006 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001007 while (!current->IsContinueTarget(stmt->target())) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001008 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001009 }
1010 __ Drop(stack_depth);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001011 if (context_length > 0) {
1012 while (context_length > 0) {
1013 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1014 --context_length;
1015 }
1016 StoreToFrameField(StandardFrameConstants::kContextOffset,
1017 context_register());
1018 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001019
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001020 __ jmp(current->AsIteration()->continue_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001021}
1022
1023
1024void FullCodeGenerator::VisitBreakStatement(BreakStatement* stmt) {
1025 Comment cmnt(masm_, "[ BreakStatement");
1026 SetStatementPosition(stmt);
1027 NestedStatement* current = nesting_stack_;
1028 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001029 int context_length = 0;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001030 // When breaking, we clobber the unpredictable value in the accumulator
1031 // with one that's safe for GC. If we hit an exit from the try block of
1032 // try...finally on our way out, we will unconditionally preserve the
1033 // accumulator on the stack.
1034 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001035 while (!current->IsBreakTarget(stmt->target())) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001036 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001037 }
1038 __ Drop(stack_depth);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001039 if (context_length > 0) {
1040 while (context_length > 0) {
1041 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1042 --context_length;
1043 }
1044 StoreToFrameField(StandardFrameConstants::kContextOffset,
1045 context_register());
1046 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001047
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001048 __ jmp(current->AsBreakable()->break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001049}
1050
1051
1052void FullCodeGenerator::VisitReturnStatement(ReturnStatement* stmt) {
1053 Comment cmnt(masm_, "[ ReturnStatement");
1054 SetStatementPosition(stmt);
1055 Expression* expr = stmt->expression();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001056 VisitForAccumulatorValue(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001057
1058 // Exit all nested statements.
1059 NestedStatement* current = nesting_stack_;
1060 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001061 int context_length = 0;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001062 while (current != NULL) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001063 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001064 }
1065 __ Drop(stack_depth);
1066
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001067 EmitReturnSequence();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001068}
1069
1070
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001071void FullCodeGenerator::VisitWithStatement(WithStatement* stmt) {
1072 Comment cmnt(masm_, "[ WithStatement");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001073 SetStatementPosition(stmt);
1074
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001075 VisitForStackValue(stmt->expression());
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00001076 PushFunctionArgumentForContextAllocation();
1077 __ CallRuntime(Runtime::kPushWithContext, 2);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001078 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001079
1080 { WithOrCatch body(this);
1081 Visit(stmt->statement());
1082 }
1083
1084 // Pop context.
1085 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1086 // Update local stack frame context field.
1087 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001088}
1089
1090
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001091void FullCodeGenerator::VisitDoWhileStatement(DoWhileStatement* stmt) {
1092 Comment cmnt(masm_, "[ DoWhileStatement");
1093 SetStatementPosition(stmt);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001094 Label body, stack_check;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001095
1096 Iteration loop_statement(this, stmt);
1097 increment_loop_depth();
1098
1099 __ bind(&body);
1100 Visit(stmt->body());
1101
ricow@chromium.org65fae842010-08-25 15:26:24 +00001102 // Record the position of the do while condition and make sure it is
1103 // possible to break on the condition.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001104 __ bind(loop_statement.continue_label());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001105 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001106 SetExpressionPosition(stmt->cond(), stmt->condition_position());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001107 VisitForControl(stmt->cond(),
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001108 &stack_check,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001109 loop_statement.break_label(),
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001110 &stack_check);
1111
1112 // Check stack before looping.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001113 PrepareForBailoutForId(stmt->BackEdgeId(), NO_REGISTERS);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001114 __ bind(&stack_check);
yangguo@chromium.org56454712012-02-16 15:33:53 +00001115 EmitStackCheck(stmt, &body);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001116 __ jmp(&body);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001117
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001118 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001119 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001120 decrement_loop_depth();
1121}
1122
1123
1124void FullCodeGenerator::VisitWhileStatement(WhileStatement* stmt) {
1125 Comment cmnt(masm_, "[ WhileStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001126 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001127
1128 Iteration loop_statement(this, stmt);
1129 increment_loop_depth();
1130
1131 // Emit the test at the bottom of the loop.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001132 __ jmp(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001133
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());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001137
1138 // Emit the statement position here as this is where the while
1139 // statement code starts.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001140 __ bind(loop_statement.continue_label());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001141 SetStatementPosition(stmt);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001142
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001143 // Check stack before looping.
yangguo@chromium.org56454712012-02-16 15:33:53 +00001144 EmitStackCheck(stmt, &body);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001145
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001146 __ bind(&test);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001147 VisitForControl(stmt->cond(),
1148 &body,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001149 loop_statement.break_label(),
1150 loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001151
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001152 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001153 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001154 decrement_loop_depth();
1155}
1156
1157
1158void FullCodeGenerator::VisitForStatement(ForStatement* stmt) {
1159 Comment cmnt(masm_, "[ ForStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001160 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001161
1162 Iteration loop_statement(this, stmt);
erik.corry@gmail.combbceb572012-03-09 10:52:05 +00001163
1164 // Set statement position for a break slot before entering the for-body.
1165 SetStatementPosition(stmt);
1166
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001167 if (stmt->init() != NULL) {
1168 Visit(stmt->init());
1169 }
1170
1171 increment_loop_depth();
1172 // Emit the test at the bottom of the loop (even if empty).
1173 __ jmp(&test);
1174
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001175 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001176 __ bind(&body);
1177 Visit(stmt->body());
1178
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001179 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001180 __ bind(loop_statement.continue_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001181 if (stmt->next() != NULL) {
1182 Visit(stmt->next());
1183 }
1184
ricow@chromium.org65fae842010-08-25 15:26:24 +00001185 // Emit the statement position here as this is where the for
1186 // statement code starts.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001187 SetStatementPosition(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001188
1189 // Check stack before looping.
yangguo@chromium.org56454712012-02-16 15:33:53 +00001190 EmitStackCheck(stmt, &body);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001191
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001192 __ bind(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001193 if (stmt->cond() != NULL) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00001194 VisitForControl(stmt->cond(),
1195 &body,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001196 loop_statement.break_label(),
1197 loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001198 } else {
1199 __ jmp(&body);
1200 }
1201
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001202 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001203 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001204 decrement_loop_depth();
1205}
1206
1207
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001208void FullCodeGenerator::VisitTryCatchStatement(TryCatchStatement* stmt) {
1209 Comment cmnt(masm_, "[ TryCatchStatement");
1210 SetStatementPosition(stmt);
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001211 // The try block adds a handler to the exception handler chain before
1212 // entering, and removes it again when exiting normally. If an exception
1213 // is thrown during execution of the try block, the handler is consumed
1214 // and control is passed to the catch block with the exception in the
1215 // result register.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001216
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001217 Label try_entry, handler_entry, exit;
1218 __ jmp(&try_entry);
1219 __ bind(&handler_entry);
1220 handler_table()->set(stmt->index(), Smi::FromInt(handler_entry.pos()));
1221 // Exception handler code, the exception is in the result register.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001222 // Extend the context before executing the catch block.
1223 { Comment cmnt(masm_, "[ Extend catch context");
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001224 __ Push(stmt->variable()->name());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001225 __ push(result_register());
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00001226 PushFunctionArgumentForContextAllocation();
1227 __ CallRuntime(Runtime::kPushCatchContext, 3);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001228 StoreToFrameField(StandardFrameConstants::kContextOffset,
1229 context_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001230 }
1231
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001232 Scope* saved_scope = scope();
1233 scope_ = stmt->scope();
1234 ASSERT(scope_->declarations()->is_empty());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001235 { WithOrCatch catch_body(this);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001236 Visit(stmt->catch_block());
1237 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001238 // Restore the context.
1239 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1240 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001241 scope_ = saved_scope;
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001242 __ jmp(&exit);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001243
1244 // Try block code. Sets up the exception handler chain.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001245 __ bind(&try_entry);
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00001246 __ PushTryHandler(StackHandler::CATCH, stmt->index());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001247 { TryCatch try_body(this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001248 Visit(stmt->try_block());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001249 }
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001250 __ PopTryHandler();
1251 __ bind(&exit);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001252}
1253
1254
1255void FullCodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* stmt) {
1256 Comment cmnt(masm_, "[ TryFinallyStatement");
1257 SetStatementPosition(stmt);
1258 // Try finally is compiled by setting up a try-handler on the stack while
1259 // executing the try body, and removing it again afterwards.
1260 //
1261 // The try-finally construct can enter the finally block in three ways:
1262 // 1. By exiting the try-block normally. This removes the try-handler and
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001263 // calls the finally block code before continuing.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001264 // 2. By exiting the try-block with a function-local control flow transfer
1265 // (break/continue/return). The site of the, e.g., break removes the
1266 // try handler and calls the finally block code before continuing
1267 // its outward control transfer.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001268 // 3. By exiting the try-block with a thrown exception.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001269 // This can happen in nested function calls. It traverses the try-handler
1270 // chain and consumes the try-handler entry before jumping to the
1271 // handler code. The handler code then calls the finally-block before
1272 // rethrowing the exception.
1273 //
1274 // The finally block must assume a return address on top of the stack
1275 // (or in the link register on ARM chips) and a value (return value or
1276 // exception) in the result register (rax/eax/r0), both of which must
1277 // be preserved. The return address isn't GC-safe, so it should be
1278 // cooked before GC.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001279 Label try_entry, handler_entry, finally_entry;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001280
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001281 // Jump to try-handler setup and try-block code.
1282 __ jmp(&try_entry);
1283 __ bind(&handler_entry);
1284 handler_table()->set(stmt->index(), Smi::FromInt(handler_entry.pos()));
1285 // Exception handler code. This code is only executed when an exception
1286 // is thrown. The exception is in the result register, and must be
1287 // preserved by the finally block. Call the finally block and then
1288 // rethrow the exception if it returns.
1289 __ Call(&finally_entry);
1290 __ push(result_register());
1291 __ CallRuntime(Runtime::kReThrow, 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001292
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001293 // Finally block implementation.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001294 __ bind(&finally_entry);
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001295 EnterFinallyBlock();
1296 { Finally finally_body(this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001297 Visit(stmt->finally_block());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001298 }
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001299 ExitFinallyBlock(); // Return to the calling code.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001300
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001301 // Set up try handler.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001302 __ bind(&try_entry);
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00001303 __ PushTryHandler(StackHandler::FINALLY, stmt->index());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001304 { TryFinally try_body(this, &finally_entry);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001305 Visit(stmt->try_block());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001306 }
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001307 __ PopTryHandler();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001308 // Execute the finally block on the way out. Clobber the unpredictable
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001309 // value in the result register with one that's safe for GC because the
1310 // finally block will unconditionally preserve the result register on the
1311 // stack.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001312 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001313 __ Call(&finally_entry);
1314}
1315
1316
1317void FullCodeGenerator::VisitDebuggerStatement(DebuggerStatement* stmt) {
1318#ifdef ENABLE_DEBUGGER_SUPPORT
1319 Comment cmnt(masm_, "[ DebuggerStatement");
1320 SetStatementPosition(stmt);
1321
ager@chromium.org5c838252010-02-19 08:53:10 +00001322 __ DebugBreak();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001323 // Ignore the return value.
1324#endif
1325}
1326
1327
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001328void FullCodeGenerator::VisitConditional(Conditional* expr) {
1329 Comment cmnt(masm_, "[ Conditional");
1330 Label true_case, false_case, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001331 VisitForControl(expr->condition(), &true_case, &false_case, &true_case);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001332
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001333 PrepareForBailoutForId(expr->ThenId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001334 __ bind(&true_case);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001335 SetExpressionPosition(expr->then_expression(),
1336 expr->then_expression_position());
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001337 if (context()->IsTest()) {
1338 const TestContext* for_test = TestContext::cast(context());
1339 VisitForControl(expr->then_expression(),
1340 for_test->true_label(),
1341 for_test->false_label(),
1342 NULL);
1343 } else {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001344 VisitInDuplicateContext(expr->then_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001345 __ jmp(&done);
1346 }
1347
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001348 PrepareForBailoutForId(expr->ElseId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001349 __ bind(&false_case);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001350 SetExpressionPosition(expr->else_expression(),
1351 expr->else_expression_position());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001352 VisitInDuplicateContext(expr->else_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001353 // If control flow falls through Visit, merge it with true case here.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001354 if (!context()->IsTest()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001355 __ bind(&done);
1356 }
1357}
1358
1359
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001360void FullCodeGenerator::VisitLiteral(Literal* expr) {
1361 Comment cmnt(masm_, "[ Literal");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001362 context()->Plug(expr->handle());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001363}
1364
1365
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001366void FullCodeGenerator::VisitFunctionLiteral(FunctionLiteral* expr) {
1367 Comment cmnt(masm_, "[ FunctionLiteral");
1368
1369 // Build the function boilerplate and instantiate it.
1370 Handle<SharedFunctionInfo> function_info =
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001371 Compiler::BuildFunctionInfo(expr, script());
1372 if (function_info.is_null()) {
1373 SetStackOverflow();
1374 return;
1375 }
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001376 EmitNewClosure(function_info, expr->pretenure());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001377}
1378
1379
1380void FullCodeGenerator::VisitSharedFunctionInfoLiteral(
1381 SharedFunctionInfoLiteral* expr) {
1382 Comment cmnt(masm_, "[ SharedFunctionInfoLiteral");
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001383 EmitNewClosure(expr->shared_function_info(), false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001384}
1385
1386
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001387void FullCodeGenerator::VisitThrow(Throw* expr) {
1388 Comment cmnt(masm_, "[ Throw");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001389 VisitForStackValue(expr->exception());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001390 __ CallRuntime(Runtime::kThrow, 1);
1391 // Never returns here.
1392}
1393
1394
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001395FullCodeGenerator::NestedStatement* FullCodeGenerator::TryCatch::Exit(
1396 int* stack_depth,
1397 int* context_length) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001398 // The macros used here must preserve the result register.
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001399 __ Drop(*stack_depth);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001400 __ PopTryHandler();
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001401 *stack_depth = 0;
1402 return previous_;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001403}
1404
ricow@chromium.org65fae842010-08-25 15:26:24 +00001405
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001406bool FullCodeGenerator::TryLiteralCompare(CompareOperation* expr) {
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001407 Expression* sub_expr;
ager@chromium.org04921a82011-06-27 13:21:41 +00001408 Handle<String> check;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001409 if (expr->IsLiteralCompareTypeof(&sub_expr, &check)) {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001410 EmitLiteralCompareTypeof(expr, sub_expr, check);
ager@chromium.org04921a82011-06-27 13:21:41 +00001411 return true;
1412 }
1413
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001414 if (expr->IsLiteralCompareUndefined(&sub_expr)) {
1415 EmitLiteralCompareNil(expr, sub_expr, kUndefinedValue);
1416 return true;
1417 }
1418
1419 if (expr->IsLiteralCompareNull(&sub_expr)) {
1420 EmitLiteralCompareNil(expr, sub_expr, kNullValue);
ager@chromium.org04921a82011-06-27 13:21:41 +00001421 return true;
1422 }
1423
1424 return false;
1425}
1426
1427
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001428#undef __
1429
1430
1431} } // namespace v8::internal