blob: 5f3c1d2ceb949eba99f3b60c1c8dd50df216732c [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);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000316 code->set_optimizable(info->IsOptimizable());
yangguo@chromium.orga7d3df92012-02-27 11:46:55 +0000317 code->set_self_optimization_header(cgen.has_self_optimization_header_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000318 cgen.PopulateDeoptimizationData(code);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000319 cgen.PopulateTypeFeedbackInfo(code);
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000320 cgen.PopulateTypeFeedbackCells(code);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000321 code->set_has_deoptimization_support(info->HasDeoptimizationSupport());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +0000322 code->set_handler_table(*cgen.handler_table());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000323#ifdef ENABLE_DEBUGGER_SUPPORT
lrn@chromium.org34e60782011-09-15 07:25:40 +0000324 code->set_has_debug_break_slots(
325 info->isolate()->debugger()->IsDebuggerActive());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000326 code->set_compiled_optimizable(info->IsOptimizable());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000327#endif // ENABLE_DEBUGGER_SUPPORT
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000328 code->set_allow_osr_at_loop_nesting_level(0);
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000329 code->set_stack_check_table_offset(table_offset);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000330 CodeGenerator::PrintCode(code, info);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000331 info->SetCode(code); // May be an empty handle.
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000332 if (!code.is_null()) {
333 isolate->runtime_profiler()->NotifyCodeGenerated(code->instruction_size());
334 }
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000335#ifdef ENABLE_GDB_JIT_INTERFACE
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000336 if (FLAG_gdbjit && !code.is_null()) {
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000337 GDBJITLineInfo* lineinfo =
338 masm.positions_recorder()->DetachGDBJITLineInfo();
339
340 GDBJIT(RegisterDetailedLineInfo(*code, lineinfo));
341 }
342#endif
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000343 return !code.is_null();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000344}
345
346
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000347unsigned FullCodeGenerator::EmitStackCheckTable() {
348 // The stack check table consists of a length (in number of entries)
349 // field, and then a sequence of entries. Each entry is a pair of AST id
350 // and code-relative pc offset.
351 masm()->Align(kIntSize);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000352 unsigned offset = masm()->pc_offset();
353 unsigned length = stack_checks_.length();
354 __ dd(length);
355 for (unsigned i = 0; i < length; ++i) {
356 __ dd(stack_checks_[i].id);
357 __ dd(stack_checks_[i].pc_and_state);
358 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000359 return offset;
360}
361
362
363void FullCodeGenerator::PopulateDeoptimizationData(Handle<Code> code) {
364 // Fill in the deoptimization information.
365 ASSERT(info_->HasDeoptimizationSupport() || bailout_entries_.is_empty());
366 if (!info_->HasDeoptimizationSupport()) return;
367 int length = bailout_entries_.length();
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000368 Handle<DeoptimizationOutputData> data = isolate()->factory()->
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000369 NewDeoptimizationOutputData(length, TENURED);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000370 for (int i = 0; i < length; i++) {
371 data->SetAstId(i, Smi::FromInt(bailout_entries_[i].id));
372 data->SetPcAndState(i, Smi::FromInt(bailout_entries_[i].pc_and_state));
373 }
374 code->set_deoptimization_data(*data);
375}
376
377
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000378void FullCodeGenerator::PopulateTypeFeedbackInfo(Handle<Code> code) {
379 Handle<TypeFeedbackInfo> info = isolate()->factory()->NewTypeFeedbackInfo();
380 info->set_ic_total_count(ic_total_count_);
yangguo@chromium.orga7d3df92012-02-27 11:46:55 +0000381 ASSERT(!isolate()->heap()->InNewSpace(*info));
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000382 code->set_type_feedback_info(*info);
383}
384
385
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000386void FullCodeGenerator::PopulateTypeFeedbackCells(Handle<Code> code) {
387 if (type_feedback_cells_.is_empty()) return;
388 int length = type_feedback_cells_.length();
389 int array_size = TypeFeedbackCells::LengthOfFixedArray(length);
390 Handle<TypeFeedbackCells> cache = Handle<TypeFeedbackCells>::cast(
391 isolate()->factory()->NewFixedArray(array_size, TENURED));
392 for (int i = 0; i < length; i++) {
393 cache->SetAstId(i, Smi::FromInt(type_feedback_cells_[i].ast_id));
394 cache->SetCell(i, *type_feedback_cells_[i].cell);
395 }
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000396 TypeFeedbackInfo::cast(code->type_feedback_info())->set_type_feedback_cells(
397 *cache);
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000398}
399
400
401
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000402void FullCodeGenerator::PrepareForBailout(Expression* node, State state) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000403 PrepareForBailoutForId(node->id(), state);
404}
405
406
407void FullCodeGenerator::RecordJSReturnSite(Call* call) {
408 // We record the offset of the function return so we can rebuild the frame
409 // if the function was inlined, i.e., this is the return address in the
410 // inlined function's frame.
411 //
412 // The state is ignored. We defensively set it to TOS_REG, which is the
413 // real state of the unoptimized code at the return site.
414 PrepareForBailoutForId(call->ReturnId(), TOS_REG);
415#ifdef DEBUG
416 // In debug builds, mark the return so we can verify that this function
417 // was called.
418 ASSERT(!call->return_is_recorded_);
419 call->return_is_recorded_ = true;
420#endif
421}
422
423
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +0000424void FullCodeGenerator::PrepareForBailoutForId(unsigned id, State state) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000425 // There's no need to prepare this code for bailouts from already optimized
426 // code or code that can't be optimized.
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000427 if (!info_->HasDeoptimizationSupport()) return;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000428 unsigned pc_and_state =
429 StateField::encode(state) | PcField::encode(masm_->pc_offset());
yangguo@chromium.org56454712012-02-16 15:33:53 +0000430 ASSERT(Smi::IsValid(pc_and_state));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000431 BailoutEntry entry = { id, pc_and_state };
432#ifdef DEBUG
yangguo@chromium.org659ceec2012-01-26 07:37:54 +0000433 if (FLAG_enable_slow_asserts) {
434 // Assert that we don't have multiple bailout entries for the same node.
435 for (int i = 0; i < bailout_entries_.length(); i++) {
436 if (bailout_entries_.at(i).id == entry.id) {
437 AstPrinter printer;
438 PrintF("%s", printer.PrintProgram(info_->function()));
439 UNREACHABLE();
440 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000441 }
442 }
443#endif // DEBUG
444 bailout_entries_.Add(entry);
445}
446
447
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000448void FullCodeGenerator::RecordTypeFeedbackCell(
449 unsigned id, Handle<JSGlobalPropertyCell> cell) {
450 TypeFeedbackCellEntry entry = { id, cell };
451 type_feedback_cells_.Add(entry);
452}
453
454
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +0000455void FullCodeGenerator::RecordStackCheck(unsigned ast_id) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000456 // The pc offset does not need to be encoded and packed together with a
457 // state.
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +0000458 ASSERT(masm_->pc_offset() > 0);
459 BailoutEntry entry = { ast_id, static_cast<unsigned>(masm_->pc_offset()) };
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000460 stack_checks_.Add(entry);
461}
462
463
ricow@chromium.org65fae842010-08-25 15:26:24 +0000464bool FullCodeGenerator::ShouldInlineSmiCase(Token::Value op) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000465 // Inline smi case inside loops, but not division and modulo which
466 // are too complicated and take up too much space.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000467 if (op == Token::DIV ||op == Token::MOD) return false;
468 if (FLAG_always_inline_smi_code) return true;
469 return loop_depth_ > 0;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000470}
471
472
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000473void FullCodeGenerator::EffectContext::Plug(Register reg) const {
474}
475
476
477void FullCodeGenerator::AccumulatorValueContext::Plug(Register reg) const {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000478 __ Move(result_register(), reg);
479}
480
481
482void FullCodeGenerator::StackValueContext::Plug(Register reg) const {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000483 __ push(reg);
484}
485
486
487void FullCodeGenerator::TestContext::Plug(Register reg) const {
488 // For simplicity we always test the accumulator register.
489 __ Move(result_register(), reg);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000490 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000491 codegen()->DoTest(this);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000492}
493
494
495void FullCodeGenerator::EffectContext::PlugTOS() const {
496 __ Drop(1);
497}
498
499
500void FullCodeGenerator::AccumulatorValueContext::PlugTOS() const {
501 __ pop(result_register());
502}
503
504
505void FullCodeGenerator::StackValueContext::PlugTOS() const {
506}
507
508
509void FullCodeGenerator::TestContext::PlugTOS() const {
510 // For simplicity we always test the accumulator register.
511 __ pop(result_register());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000512 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000513 codegen()->DoTest(this);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000514}
515
516
517void FullCodeGenerator::EffectContext::PrepareTest(
518 Label* materialize_true,
519 Label* materialize_false,
520 Label** if_true,
521 Label** if_false,
522 Label** fall_through) const {
523 // In an effect context, the true and the false case branch to the
524 // same label.
525 *if_true = *if_false = *fall_through = materialize_true;
526}
527
528
529void FullCodeGenerator::AccumulatorValueContext::PrepareTest(
530 Label* materialize_true,
531 Label* materialize_false,
532 Label** if_true,
533 Label** if_false,
534 Label** fall_through) const {
535 *if_true = *fall_through = materialize_true;
536 *if_false = materialize_false;
537}
538
539
540void FullCodeGenerator::StackValueContext::PrepareTest(
541 Label* materialize_true,
542 Label* materialize_false,
543 Label** if_true,
544 Label** if_false,
545 Label** fall_through) const {
546 *if_true = *fall_through = materialize_true;
547 *if_false = materialize_false;
548}
549
550
551void FullCodeGenerator::TestContext::PrepareTest(
552 Label* materialize_true,
553 Label* materialize_false,
554 Label** if_true,
555 Label** if_false,
556 Label** fall_through) const {
557 *if_true = true_label_;
558 *if_false = false_label_;
559 *fall_through = fall_through_;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000560}
561
562
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000563void FullCodeGenerator::DoTest(const TestContext* context) {
564 DoTest(context->condition(),
565 context->true_label(),
566 context->false_label(),
567 context->fall_through());
568}
569
570
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000571void FullCodeGenerator::VisitDeclarations(
572 ZoneList<Declaration*>* declarations) {
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000573 int save_global_count = global_count_;
574 global_count_ = 0;
575
576 AstVisitor::VisitDeclarations(declarations);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000577
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000578 // Batch declare global functions and variables.
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000579 if (global_count_ > 0) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000580 Handle<FixedArray> array =
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000581 isolate()->factory()->NewFixedArray(2 * global_count_, TENURED);
582 int length = declarations->length();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000583 for (int j = 0, i = 0; i < length; i++) {
ulan@chromium.org812308e2012-02-29 15:58:45 +0000584 Declaration* decl = declarations->at(i);
585 Variable* var = decl->proxy()->var();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000586
ulan@chromium.org812308e2012-02-29 15:58:45 +0000587 if (var->IsUnallocated()) {
588 array->set(j++, *(var->name()));
589 FunctionDeclaration* fun_decl = decl->AsFunctionDeclaration();
590 if (fun_decl == NULL) {
591 if (var->binding_needs_init()) {
592 // In case this binding needs initialization use the hole.
593 array->set_the_hole(j++);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000594 } else {
ulan@chromium.org812308e2012-02-29 15:58:45 +0000595 array->set_undefined(j++);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000596 }
ulan@chromium.org812308e2012-02-29 15:58:45 +0000597 } else {
598 Handle<SharedFunctionInfo> function =
599 Compiler::BuildFunctionInfo(fun_decl->fun(), script());
600 // Check for stack-overflow exception.
601 if (function.is_null()) {
602 SetStackOverflow();
603 return;
604 }
605 array->set(j++, *function);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000606 }
607 }
608 }
609 // Invoke the platform-dependent code generator to do the actual
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000610 // declaration the global functions and variables.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000611 DeclareGlobals(array);
612 }
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000613
614 global_count_ = save_global_count;
615}
616
617
618void FullCodeGenerator::VisitVariableDeclaration(VariableDeclaration* decl) {
ulan@chromium.org812308e2012-02-29 15:58:45 +0000619 EmitDeclaration(decl->proxy(), decl->mode(), NULL);
620}
621
622
623void FullCodeGenerator::VisitFunctionDeclaration(FunctionDeclaration* decl) {
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000624 EmitDeclaration(decl->proxy(), decl->mode(), decl->fun());
625}
626
627
628void FullCodeGenerator::VisitModuleDeclaration(ModuleDeclaration* decl) {
ulan@chromium.org812308e2012-02-29 15:58:45 +0000629 EmitDeclaration(decl->proxy(), decl->mode(), NULL);
630}
631
632
633void FullCodeGenerator::VisitImportDeclaration(ImportDeclaration* decl) {
634 EmitDeclaration(decl->proxy(), decl->mode(), NULL);
635}
636
637
638void FullCodeGenerator::VisitExportDeclaration(ExportDeclaration* decl) {
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000639 // TODO(rossberg)
640}
641
642
643void FullCodeGenerator::VisitModuleLiteral(ModuleLiteral* module) {
644 // TODO(rossberg)
645}
646
647
648void FullCodeGenerator::VisitModuleVariable(ModuleVariable* module) {
649 // TODO(rossberg)
650}
651
652
653void FullCodeGenerator::VisitModulePath(ModulePath* module) {
654 // TODO(rossberg)
655}
656
657
658void FullCodeGenerator::VisitModuleUrl(ModuleUrl* decl) {
659 // TODO(rossberg)
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000660}
661
662
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000663int FullCodeGenerator::DeclareGlobalsFlags() {
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000664 ASSERT(DeclareGlobalsLanguageMode::is_valid(language_mode()));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000665 return DeclareGlobalsEvalFlag::encode(is_eval()) |
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000666 DeclareGlobalsNativeFlag::encode(is_native()) |
667 DeclareGlobalsLanguageMode::encode(language_mode());
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000668}
669
670
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000671void FullCodeGenerator::SetFunctionPosition(FunctionLiteral* fun) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000672 CodeGenerator::RecordPositions(masm_, fun->start_position());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000673}
674
675
676void FullCodeGenerator::SetReturnPosition(FunctionLiteral* fun) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000677 CodeGenerator::RecordPositions(masm_, fun->end_position() - 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000678}
679
680
681void FullCodeGenerator::SetStatementPosition(Statement* stmt) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000682#ifdef ENABLE_DEBUGGER_SUPPORT
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000683 if (!isolate()->debugger()->IsDebuggerActive()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000684 CodeGenerator::RecordPositions(masm_, stmt->statement_pos());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000685 } else {
686 // Check if the statement will be breakable without adding a debug break
687 // slot.
688 BreakableStatementChecker checker;
689 checker.Check(stmt);
690 // Record the statement position right here if the statement is not
691 // breakable. For breakable statements the actual recording of the
692 // position will be postponed to the breakable code (typically an IC).
693 bool position_recorded = CodeGenerator::RecordPositions(
694 masm_, stmt->statement_pos(), !checker.is_breakable());
695 // If the position recording did record a new position generate a debug
696 // break slot to make the statement breakable.
697 if (position_recorded) {
698 Debug::GenerateSlot(masm_);
699 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000700 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000701#else
702 CodeGenerator::RecordPositions(masm_, stmt->statement_pos());
703#endif
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000704}
705
706
707void FullCodeGenerator::SetExpressionPosition(Expression* expr, int pos) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000708#ifdef ENABLE_DEBUGGER_SUPPORT
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000709 if (!isolate()->debugger()->IsDebuggerActive()) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000710 CodeGenerator::RecordPositions(masm_, pos);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000711 } else {
712 // Check if the expression will be breakable without adding a debug break
713 // slot.
714 BreakableStatementChecker checker;
715 checker.Check(expr);
716 // Record a statement position right here if the expression is not
717 // breakable. For breakable expressions the actual recording of the
718 // position will be postponed to the breakable code (typically an IC).
719 // NOTE this will record a statement position for something which might
720 // not be a statement. As stepping in the debugger will only stop at
721 // statement positions this is used for e.g. the condition expression of
722 // a do while loop.
723 bool position_recorded = CodeGenerator::RecordPositions(
724 masm_, pos, !checker.is_breakable());
725 // If the position recording did record a new position generate a debug
726 // break slot to make the statement breakable.
727 if (position_recorded) {
728 Debug::GenerateSlot(masm_);
729 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000730 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000731#else
732 CodeGenerator::RecordPositions(masm_, pos);
733#endif
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000734}
735
736
737void FullCodeGenerator::SetStatementPosition(int pos) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000738 CodeGenerator::RecordPositions(masm_, pos);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000739}
740
741
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000742void FullCodeGenerator::SetSourcePosition(int pos) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000743 if (pos != RelocInfo::kNoPosition) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000744 masm_->positions_recorder()->RecordPosition(pos);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000745 }
746}
747
748
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000749// Lookup table for code generators for special runtime calls which are
750// generated inline.
751#define INLINE_FUNCTION_GENERATOR_ADDRESS(Name, argc, ressize) \
752 &FullCodeGenerator::Emit##Name,
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000753
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000754const FullCodeGenerator::InlineFunctionGenerator
755 FullCodeGenerator::kInlineFunctionGenerators[] = {
756 INLINE_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS)
757 INLINE_RUNTIME_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS)
758 };
759#undef INLINE_FUNCTION_GENERATOR_ADDRESS
760
761
762FullCodeGenerator::InlineFunctionGenerator
763 FullCodeGenerator::FindInlineFunctionGenerator(Runtime::FunctionId id) {
whesse@chromium.org023421e2010-12-21 12:19:12 +0000764 int lookup_index =
765 static_cast<int>(id) - static_cast<int>(Runtime::kFirstInlineFunction);
766 ASSERT(lookup_index >= 0);
767 ASSERT(static_cast<size_t>(lookup_index) <
768 ARRAY_SIZE(kInlineFunctionGenerators));
769 return kInlineFunctionGenerators[lookup_index];
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000770}
771
772
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000773void FullCodeGenerator::EmitInlineRuntimeCall(CallRuntime* expr) {
774 const Runtime::Function* function = expr->function();
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000775 ASSERT(function != NULL);
776 ASSERT(function->intrinsic_type == Runtime::INLINE);
777 InlineFunctionGenerator generator =
778 FindInlineFunctionGenerator(function->function_id);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000779 ((*this).*(generator))(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000780}
781
782
783void FullCodeGenerator::VisitBinaryOperation(BinaryOperation* expr) {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000784 switch (expr->op()) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000785 case Token::COMMA:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000786 return VisitComma(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000787 case Token::OR:
788 case Token::AND:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000789 return VisitLogicalExpression(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000790 default:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000791 return VisitArithmeticExpression(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000792 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000793}
794
795
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000796void FullCodeGenerator::VisitInDuplicateContext(Expression* expr) {
797 if (context()->IsEffect()) {
798 VisitForEffect(expr);
799 } else if (context()->IsAccumulatorValue()) {
800 VisitForAccumulatorValue(expr);
801 } else if (context()->IsStackValue()) {
802 VisitForStackValue(expr);
803 } else if (context()->IsTest()) {
804 const TestContext* test = TestContext::cast(context());
805 VisitForControl(expr, test->true_label(), test->false_label(),
806 test->fall_through());
807 }
808}
809
810
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000811void FullCodeGenerator::VisitComma(BinaryOperation* expr) {
812 Comment cmnt(masm_, "[ Comma");
813 VisitForEffect(expr->left());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000814 VisitInDuplicateContext(expr->right());
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000815}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000816
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000817
818void FullCodeGenerator::VisitLogicalExpression(BinaryOperation* expr) {
819 bool is_logical_and = expr->op() == Token::AND;
820 Comment cmnt(masm_, is_logical_and ? "[ Logical AND" : "[ Logical OR");
821 Expression* left = expr->left();
822 Expression* right = expr->right();
823 int right_id = expr->RightId();
824 Label done;
825
826 if (context()->IsTest()) {
827 Label eval_right;
828 const TestContext* test = TestContext::cast(context());
829 if (is_logical_and) {
830 VisitForControl(left, &eval_right, test->false_label(), &eval_right);
831 } else {
832 VisitForControl(left, test->true_label(), &eval_right, &eval_right);
833 }
834 PrepareForBailoutForId(right_id, NO_REGISTERS);
835 __ bind(&eval_right);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000836
837 } else if (context()->IsAccumulatorValue()) {
838 VisitForAccumulatorValue(left);
839 // We want the value in the accumulator for the test, and on the stack in
840 // case we need it.
841 __ push(result_register());
842 Label discard, restore;
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000843 if (is_logical_and) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000844 DoTest(left, &discard, &restore, &restore);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000845 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000846 DoTest(left, &restore, &discard, &restore);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000847 }
848 __ bind(&restore);
849 __ pop(result_register());
850 __ jmp(&done);
851 __ bind(&discard);
852 __ Drop(1);
853 PrepareForBailoutForId(right_id, NO_REGISTERS);
854
855 } else if (context()->IsStackValue()) {
856 VisitForAccumulatorValue(left);
857 // We want the value in the accumulator for the test, and on the stack in
858 // case we need it.
859 __ push(result_register());
860 Label discard;
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000861 if (is_logical_and) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000862 DoTest(left, &discard, &done, &discard);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000863 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000864 DoTest(left, &done, &discard, &discard);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000865 }
866 __ bind(&discard);
867 __ Drop(1);
868 PrepareForBailoutForId(right_id, NO_REGISTERS);
869
870 } else {
871 ASSERT(context()->IsEffect());
872 Label eval_right;
873 if (is_logical_and) {
874 VisitForControl(left, &eval_right, &done, &eval_right);
875 } else {
876 VisitForControl(left, &done, &eval_right, &eval_right);
877 }
878 PrepareForBailoutForId(right_id, NO_REGISTERS);
879 __ bind(&eval_right);
880 }
881
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000882 VisitInDuplicateContext(right);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000883 __ bind(&done);
884}
885
886
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000887void FullCodeGenerator::VisitArithmeticExpression(BinaryOperation* expr) {
888 Token::Value op = expr->op();
889 Comment cmnt(masm_, "[ ArithmeticExpression");
890 Expression* left = expr->left();
891 Expression* right = expr->right();
892 OverwriteMode mode =
893 left->ResultOverwriteAllowed()
894 ? OVERWRITE_LEFT
895 : (right->ResultOverwriteAllowed() ? OVERWRITE_RIGHT : NO_OVERWRITE);
896
897 VisitForStackValue(left);
898 VisitForAccumulatorValue(right);
899
900 SetSourcePosition(expr->position());
901 if (ShouldInlineSmiCase(op)) {
902 EmitInlineSmiBinaryOp(expr, op, mode, left, right);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000903 } else {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000904 EmitBinaryOp(expr, op, mode);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000905 }
906}
907
908
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000909void FullCodeGenerator::VisitBlock(Block* stmt) {
910 Comment cmnt(masm_, "[ Block");
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000911 NestedBlock nested_block(this, stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000912 SetStatementPosition(stmt);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000913
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000914 Scope* saved_scope = scope();
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000915 // Push a block context when entering a block with block scoped variables.
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000916 if (stmt->block_scope() != NULL) {
917 { Comment cmnt(masm_, "[ Extend block context");
918 scope_ = stmt->block_scope();
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000919 Handle<ScopeInfo> scope_info = scope_->GetScopeInfo();
920 int heap_slots = scope_info->ContextLength() - Context::MIN_CONTEXT_SLOTS;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000921 __ Push(scope_info);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000922 PushFunctionArgumentForContextAllocation();
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000923 if (heap_slots <= FastNewBlockContextStub::kMaximumSlots) {
924 FastNewBlockContextStub stub(heap_slots);
925 __ CallStub(&stub);
926 } else {
927 __ CallRuntime(Runtime::kPushBlockContext, 2);
928 }
929
930 // Replace the context stored in the frame.
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000931 StoreToFrameField(StandardFrameConstants::kContextOffset,
932 context_register());
933 }
934 { Comment cmnt(masm_, "[ Declarations");
935 VisitDeclarations(scope_->declarations());
936 }
937 }
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +0000938 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000939 VisitStatements(stmt->statements());
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000940 scope_ = saved_scope;
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000941 __ bind(nested_block.break_label());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000942 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000943
944 // Pop block context if necessary.
945 if (stmt->block_scope() != NULL) {
946 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
947 // Update local stack frame context field.
948 StoreToFrameField(StandardFrameConstants::kContextOffset,
949 context_register());
950 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000951}
952
953
954void FullCodeGenerator::VisitExpressionStatement(ExpressionStatement* stmt) {
955 Comment cmnt(masm_, "[ ExpressionStatement");
956 SetStatementPosition(stmt);
957 VisitForEffect(stmt->expression());
958}
959
960
961void FullCodeGenerator::VisitEmptyStatement(EmptyStatement* stmt) {
962 Comment cmnt(masm_, "[ EmptyStatement");
963 SetStatementPosition(stmt);
964}
965
966
967void FullCodeGenerator::VisitIfStatement(IfStatement* stmt) {
968 Comment cmnt(masm_, "[ IfStatement");
969 SetStatementPosition(stmt);
970 Label then_part, else_part, done;
971
ricow@chromium.org65fae842010-08-25 15:26:24 +0000972 if (stmt->HasElseStatement()) {
973 VisitForControl(stmt->condition(), &then_part, &else_part, &then_part);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000974 PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000975 __ bind(&then_part);
976 Visit(stmt->then_statement());
977 __ jmp(&done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000978
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000979 PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000980 __ bind(&else_part);
981 Visit(stmt->else_statement());
982 } else {
983 VisitForControl(stmt->condition(), &then_part, &done, &then_part);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000984 PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000985 __ bind(&then_part);
986 Visit(stmt->then_statement());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000987
988 PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000989 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000990 __ bind(&done);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000991 PrepareForBailoutForId(stmt->IfId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000992}
993
994
995void FullCodeGenerator::VisitContinueStatement(ContinueStatement* stmt) {
996 Comment cmnt(masm_, "[ ContinueStatement");
997 SetStatementPosition(stmt);
998 NestedStatement* current = nesting_stack_;
999 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001000 int context_length = 0;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001001 // When continuing, we clobber the unpredictable value in the accumulator
1002 // with one that's safe for GC. If we hit an exit from the try block of
1003 // try...finally on our way out, we will unconditionally preserve the
1004 // accumulator on the stack.
1005 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001006 while (!current->IsContinueTarget(stmt->target())) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001007 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001008 }
1009 __ Drop(stack_depth);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001010 if (context_length > 0) {
1011 while (context_length > 0) {
1012 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1013 --context_length;
1014 }
1015 StoreToFrameField(StandardFrameConstants::kContextOffset,
1016 context_register());
1017 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001018
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001019 __ jmp(current->AsIteration()->continue_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001020}
1021
1022
1023void FullCodeGenerator::VisitBreakStatement(BreakStatement* stmt) {
1024 Comment cmnt(masm_, "[ BreakStatement");
1025 SetStatementPosition(stmt);
1026 NestedStatement* current = nesting_stack_;
1027 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001028 int context_length = 0;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001029 // When breaking, we clobber the unpredictable value in the accumulator
1030 // with one that's safe for GC. If we hit an exit from the try block of
1031 // try...finally on our way out, we will unconditionally preserve the
1032 // accumulator on the stack.
1033 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001034 while (!current->IsBreakTarget(stmt->target())) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001035 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001036 }
1037 __ Drop(stack_depth);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001038 if (context_length > 0) {
1039 while (context_length > 0) {
1040 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1041 --context_length;
1042 }
1043 StoreToFrameField(StandardFrameConstants::kContextOffset,
1044 context_register());
1045 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001046
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001047 __ jmp(current->AsBreakable()->break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001048}
1049
1050
1051void FullCodeGenerator::VisitReturnStatement(ReturnStatement* stmt) {
1052 Comment cmnt(masm_, "[ ReturnStatement");
1053 SetStatementPosition(stmt);
1054 Expression* expr = stmt->expression();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001055 VisitForAccumulatorValue(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001056
1057 // Exit all nested statements.
1058 NestedStatement* current = nesting_stack_;
1059 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001060 int context_length = 0;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001061 while (current != NULL) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001062 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001063 }
1064 __ Drop(stack_depth);
1065
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001066 EmitReturnSequence();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001067}
1068
1069
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001070void FullCodeGenerator::VisitWithStatement(WithStatement* stmt) {
1071 Comment cmnt(masm_, "[ WithStatement");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001072 SetStatementPosition(stmt);
1073
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001074 VisitForStackValue(stmt->expression());
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00001075 PushFunctionArgumentForContextAllocation();
1076 __ CallRuntime(Runtime::kPushWithContext, 2);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001077 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001078
1079 { WithOrCatch body(this);
1080 Visit(stmt->statement());
1081 }
1082
1083 // Pop context.
1084 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1085 // Update local stack frame context field.
1086 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001087}
1088
1089
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001090void FullCodeGenerator::VisitDoWhileStatement(DoWhileStatement* stmt) {
1091 Comment cmnt(masm_, "[ DoWhileStatement");
1092 SetStatementPosition(stmt);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001093 Label body, stack_check;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001094
1095 Iteration loop_statement(this, stmt);
1096 increment_loop_depth();
1097
1098 __ bind(&body);
1099 Visit(stmt->body());
1100
ricow@chromium.org65fae842010-08-25 15:26:24 +00001101 // Record the position of the do while condition and make sure it is
1102 // possible to break on the condition.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001103 __ bind(loop_statement.continue_label());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001104 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001105 SetExpressionPosition(stmt->cond(), stmt->condition_position());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001106 VisitForControl(stmt->cond(),
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001107 &stack_check,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001108 loop_statement.break_label(),
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001109 &stack_check);
1110
1111 // Check stack before looping.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001112 PrepareForBailoutForId(stmt->BackEdgeId(), NO_REGISTERS);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001113 __ bind(&stack_check);
yangguo@chromium.org56454712012-02-16 15:33:53 +00001114 EmitStackCheck(stmt, &body);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001115 __ jmp(&body);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001116
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001117 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001118 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001119 decrement_loop_depth();
1120}
1121
1122
1123void FullCodeGenerator::VisitWhileStatement(WhileStatement* stmt) {
1124 Comment cmnt(masm_, "[ WhileStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001125 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001126
1127 Iteration loop_statement(this, stmt);
1128 increment_loop_depth();
1129
1130 // Emit the test at the bottom of the loop.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001131 __ jmp(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001132
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001133 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001134 __ bind(&body);
1135 Visit(stmt->body());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001136
1137 // Emit the statement position here as this is where the while
1138 // statement code starts.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001139 __ bind(loop_statement.continue_label());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001140 SetStatementPosition(stmt);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001141
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001142 // Check stack before looping.
yangguo@chromium.org56454712012-02-16 15:33:53 +00001143 EmitStackCheck(stmt, &body);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001144
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001145 __ bind(&test);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001146 VisitForControl(stmt->cond(),
1147 &body,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001148 loop_statement.break_label(),
1149 loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001150
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001151 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001152 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001153 decrement_loop_depth();
1154}
1155
1156
1157void FullCodeGenerator::VisitForStatement(ForStatement* stmt) {
1158 Comment cmnt(masm_, "[ ForStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001159 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001160
1161 Iteration loop_statement(this, stmt);
1162 if (stmt->init() != NULL) {
1163 Visit(stmt->init());
1164 }
1165
1166 increment_loop_depth();
1167 // Emit the test at the bottom of the loop (even if empty).
1168 __ jmp(&test);
1169
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001170 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001171 __ bind(&body);
1172 Visit(stmt->body());
1173
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001174 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001175 __ bind(loop_statement.continue_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001176 SetStatementPosition(stmt);
1177 if (stmt->next() != NULL) {
1178 Visit(stmt->next());
1179 }
1180
ricow@chromium.org65fae842010-08-25 15:26:24 +00001181 // Emit the statement position here as this is where the for
1182 // statement code starts.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001183 SetStatementPosition(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001184
1185 // Check stack before looping.
yangguo@chromium.org56454712012-02-16 15:33:53 +00001186 EmitStackCheck(stmt, &body);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001187
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001188 __ bind(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001189 if (stmt->cond() != NULL) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00001190 VisitForControl(stmt->cond(),
1191 &body,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001192 loop_statement.break_label(),
1193 loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001194 } else {
1195 __ jmp(&body);
1196 }
1197
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001198 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001199 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001200 decrement_loop_depth();
1201}
1202
1203
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001204void FullCodeGenerator::VisitTryCatchStatement(TryCatchStatement* stmt) {
1205 Comment cmnt(masm_, "[ TryCatchStatement");
1206 SetStatementPosition(stmt);
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001207 // The try block adds a handler to the exception handler chain before
1208 // entering, and removes it again when exiting normally. If an exception
1209 // is thrown during execution of the try block, the handler is consumed
1210 // and control is passed to the catch block with the exception in the
1211 // result register.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001212
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001213 Label try_entry, handler_entry, exit;
1214 __ jmp(&try_entry);
1215 __ bind(&handler_entry);
1216 handler_table()->set(stmt->index(), Smi::FromInt(handler_entry.pos()));
1217 // Exception handler code, the exception is in the result register.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001218 // Extend the context before executing the catch block.
1219 { Comment cmnt(masm_, "[ Extend catch context");
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001220 __ Push(stmt->variable()->name());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001221 __ push(result_register());
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00001222 PushFunctionArgumentForContextAllocation();
1223 __ CallRuntime(Runtime::kPushCatchContext, 3);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001224 StoreToFrameField(StandardFrameConstants::kContextOffset,
1225 context_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001226 }
1227
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001228 Scope* saved_scope = scope();
1229 scope_ = stmt->scope();
1230 ASSERT(scope_->declarations()->is_empty());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001231 { WithOrCatch catch_body(this);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001232 Visit(stmt->catch_block());
1233 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001234 // Restore the context.
1235 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1236 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001237 scope_ = saved_scope;
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001238 __ jmp(&exit);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001239
1240 // Try block code. Sets up the exception handler chain.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001241 __ bind(&try_entry);
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00001242 __ PushTryHandler(StackHandler::CATCH, stmt->index());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001243 { TryCatch try_body(this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001244 Visit(stmt->try_block());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001245 }
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001246 __ PopTryHandler();
1247 __ bind(&exit);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001248}
1249
1250
1251void FullCodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* stmt) {
1252 Comment cmnt(masm_, "[ TryFinallyStatement");
1253 SetStatementPosition(stmt);
1254 // Try finally is compiled by setting up a try-handler on the stack while
1255 // executing the try body, and removing it again afterwards.
1256 //
1257 // The try-finally construct can enter the finally block in three ways:
1258 // 1. By exiting the try-block normally. This removes the try-handler and
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001259 // calls the finally block code before continuing.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001260 // 2. By exiting the try-block with a function-local control flow transfer
1261 // (break/continue/return). The site of the, e.g., break removes the
1262 // try handler and calls the finally block code before continuing
1263 // its outward control transfer.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001264 // 3. By exiting the try-block with a thrown exception.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001265 // This can happen in nested function calls. It traverses the try-handler
1266 // chain and consumes the try-handler entry before jumping to the
1267 // handler code. The handler code then calls the finally-block before
1268 // rethrowing the exception.
1269 //
1270 // The finally block must assume a return address on top of the stack
1271 // (or in the link register on ARM chips) and a value (return value or
1272 // exception) in the result register (rax/eax/r0), both of which must
1273 // be preserved. The return address isn't GC-safe, so it should be
1274 // cooked before GC.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001275 Label try_entry, handler_entry, finally_entry;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001276
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001277 // Jump to try-handler setup and try-block code.
1278 __ jmp(&try_entry);
1279 __ bind(&handler_entry);
1280 handler_table()->set(stmt->index(), Smi::FromInt(handler_entry.pos()));
1281 // Exception handler code. This code is only executed when an exception
1282 // is thrown. The exception is in the result register, and must be
1283 // preserved by the finally block. Call the finally block and then
1284 // rethrow the exception if it returns.
1285 __ Call(&finally_entry);
1286 __ push(result_register());
1287 __ CallRuntime(Runtime::kReThrow, 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001288
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001289 // Finally block implementation.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001290 __ bind(&finally_entry);
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001291 EnterFinallyBlock();
1292 { Finally finally_body(this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001293 Visit(stmt->finally_block());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001294 }
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001295 ExitFinallyBlock(); // Return to the calling code.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001296
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001297 // Set up try handler.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001298 __ bind(&try_entry);
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00001299 __ PushTryHandler(StackHandler::FINALLY, stmt->index());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001300 { TryFinally try_body(this, &finally_entry);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001301 Visit(stmt->try_block());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001302 }
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001303 __ PopTryHandler();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001304 // Execute the finally block on the way out. Clobber the unpredictable
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001305 // value in the result register with one that's safe for GC because the
1306 // finally block will unconditionally preserve the result register on the
1307 // stack.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001308 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001309 __ Call(&finally_entry);
1310}
1311
1312
1313void FullCodeGenerator::VisitDebuggerStatement(DebuggerStatement* stmt) {
1314#ifdef ENABLE_DEBUGGER_SUPPORT
1315 Comment cmnt(masm_, "[ DebuggerStatement");
1316 SetStatementPosition(stmt);
1317
ager@chromium.org5c838252010-02-19 08:53:10 +00001318 __ DebugBreak();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001319 // Ignore the return value.
1320#endif
1321}
1322
1323
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001324void FullCodeGenerator::VisitConditional(Conditional* expr) {
1325 Comment cmnt(masm_, "[ Conditional");
1326 Label true_case, false_case, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001327 VisitForControl(expr->condition(), &true_case, &false_case, &true_case);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001328
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001329 PrepareForBailoutForId(expr->ThenId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001330 __ bind(&true_case);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001331 SetExpressionPosition(expr->then_expression(),
1332 expr->then_expression_position());
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001333 if (context()->IsTest()) {
1334 const TestContext* for_test = TestContext::cast(context());
1335 VisitForControl(expr->then_expression(),
1336 for_test->true_label(),
1337 for_test->false_label(),
1338 NULL);
1339 } else {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001340 VisitInDuplicateContext(expr->then_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001341 __ jmp(&done);
1342 }
1343
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001344 PrepareForBailoutForId(expr->ElseId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001345 __ bind(&false_case);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001346 SetExpressionPosition(expr->else_expression(),
1347 expr->else_expression_position());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001348 VisitInDuplicateContext(expr->else_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001349 // If control flow falls through Visit, merge it with true case here.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001350 if (!context()->IsTest()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001351 __ bind(&done);
1352 }
1353}
1354
1355
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001356void FullCodeGenerator::VisitLiteral(Literal* expr) {
1357 Comment cmnt(masm_, "[ Literal");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001358 context()->Plug(expr->handle());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001359}
1360
1361
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001362void FullCodeGenerator::VisitFunctionLiteral(FunctionLiteral* expr) {
1363 Comment cmnt(masm_, "[ FunctionLiteral");
1364
1365 // Build the function boilerplate and instantiate it.
1366 Handle<SharedFunctionInfo> function_info =
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001367 Compiler::BuildFunctionInfo(expr, script());
1368 if (function_info.is_null()) {
1369 SetStackOverflow();
1370 return;
1371 }
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001372 EmitNewClosure(function_info, expr->pretenure());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001373}
1374
1375
1376void FullCodeGenerator::VisitSharedFunctionInfoLiteral(
1377 SharedFunctionInfoLiteral* expr) {
1378 Comment cmnt(masm_, "[ SharedFunctionInfoLiteral");
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001379 EmitNewClosure(expr->shared_function_info(), false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001380}
1381
1382
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001383void FullCodeGenerator::VisitThrow(Throw* expr) {
1384 Comment cmnt(masm_, "[ Throw");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001385 VisitForStackValue(expr->exception());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001386 __ CallRuntime(Runtime::kThrow, 1);
1387 // Never returns here.
1388}
1389
1390
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001391FullCodeGenerator::NestedStatement* FullCodeGenerator::TryCatch::Exit(
1392 int* stack_depth,
1393 int* context_length) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001394 // The macros used here must preserve the result register.
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001395 __ Drop(*stack_depth);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001396 __ PopTryHandler();
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001397 *stack_depth = 0;
1398 return previous_;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001399}
1400
ricow@chromium.org65fae842010-08-25 15:26:24 +00001401
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001402bool FullCodeGenerator::TryLiteralCompare(CompareOperation* expr) {
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001403 Expression* sub_expr;
ager@chromium.org04921a82011-06-27 13:21:41 +00001404 Handle<String> check;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001405 if (expr->IsLiteralCompareTypeof(&sub_expr, &check)) {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001406 EmitLiteralCompareTypeof(expr, sub_expr, check);
ager@chromium.org04921a82011-06-27 13:21:41 +00001407 return true;
1408 }
1409
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001410 if (expr->IsLiteralCompareUndefined(&sub_expr)) {
1411 EmitLiteralCompareNil(expr, sub_expr, kUndefinedValue);
1412 return true;
1413 }
1414
1415 if (expr->IsLiteralCompareNull(&sub_expr)) {
1416 EmitLiteralCompareNil(expr, sub_expr, kNullValue);
ager@chromium.org04921a82011-06-27 13:21:41 +00001417 return true;
1418 }
1419
1420 return false;
1421}
1422
1423
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001424#undef __
1425
1426
1427} } // namespace v8::internal