blob: 9592e0afa21c45aec364adb1e1557fae8f4d0551 [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"
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +000039#include "snapshot.h"
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000040#include "stub-cache.h"
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000041
42namespace v8 {
43namespace internal {
44
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +000045void BreakableStatementChecker::Check(Statement* stmt) {
46 Visit(stmt);
47}
48
49
50void BreakableStatementChecker::Check(Expression* expr) {
51 Visit(expr);
52}
53
54
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +000055void BreakableStatementChecker::VisitVariableDeclaration(
56 VariableDeclaration* decl) {
57}
58
ulan@chromium.org812308e2012-02-29 15:58:45 +000059void BreakableStatementChecker::VisitFunctionDeclaration(
60 FunctionDeclaration* decl) {
61}
62
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +000063void BreakableStatementChecker::VisitModuleDeclaration(
64 ModuleDeclaration* decl) {
65}
66
ulan@chromium.org812308e2012-02-29 15:58:45 +000067void BreakableStatementChecker::VisitImportDeclaration(
68 ImportDeclaration* decl) {
69}
70
71void BreakableStatementChecker::VisitExportDeclaration(
72 ExportDeclaration* decl) {
73}
74
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +000075
76void BreakableStatementChecker::VisitModuleLiteral(ModuleLiteral* module) {
77}
78
79void BreakableStatementChecker::VisitModuleVariable(ModuleVariable* module) {
80}
81
82void BreakableStatementChecker::VisitModulePath(ModulePath* module) {
83}
84
85void BreakableStatementChecker::VisitModuleUrl(ModuleUrl* module) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +000086}
87
88
89void BreakableStatementChecker::VisitBlock(Block* stmt) {
90}
91
92
93void BreakableStatementChecker::VisitExpressionStatement(
94 ExpressionStatement* stmt) {
95 // Check if expression is breakable.
96 Visit(stmt->expression());
97}
98
99
100void BreakableStatementChecker::VisitEmptyStatement(EmptyStatement* stmt) {
101}
102
103
104void BreakableStatementChecker::VisitIfStatement(IfStatement* stmt) {
105 // If the condition is breakable the if statement is breakable.
106 Visit(stmt->condition());
107}
108
109
110void BreakableStatementChecker::VisitContinueStatement(
111 ContinueStatement* stmt) {
112}
113
114
115void BreakableStatementChecker::VisitBreakStatement(BreakStatement* stmt) {
116}
117
118
119void BreakableStatementChecker::VisitReturnStatement(ReturnStatement* stmt) {
120 // Return is breakable if the expression is.
121 Visit(stmt->expression());
122}
123
124
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000125void BreakableStatementChecker::VisitWithStatement(WithStatement* stmt) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000126 Visit(stmt->expression());
127}
128
129
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000130void BreakableStatementChecker::VisitSwitchStatement(SwitchStatement* stmt) {
131 // Switch statements breakable if the tag expression is.
132 Visit(stmt->tag());
133}
134
135
136void BreakableStatementChecker::VisitDoWhileStatement(DoWhileStatement* stmt) {
137 // Mark do while as breakable to avoid adding a break slot in front of it.
138 is_breakable_ = true;
139}
140
141
142void BreakableStatementChecker::VisitWhileStatement(WhileStatement* stmt) {
143 // Mark while statements breakable if the condition expression is.
144 Visit(stmt->cond());
145}
146
147
148void BreakableStatementChecker::VisitForStatement(ForStatement* stmt) {
149 // Mark for statements breakable if the condition expression is.
150 if (stmt->cond() != NULL) {
151 Visit(stmt->cond());
152 }
153}
154
155
156void BreakableStatementChecker::VisitForInStatement(ForInStatement* stmt) {
157 // Mark for in statements breakable if the enumerable expression is.
158 Visit(stmt->enumerable());
159}
160
161
162void BreakableStatementChecker::VisitTryCatchStatement(
163 TryCatchStatement* stmt) {
164 // Mark try catch as breakable to avoid adding a break slot in front of it.
165 is_breakable_ = true;
166}
167
168
169void BreakableStatementChecker::VisitTryFinallyStatement(
170 TryFinallyStatement* stmt) {
171 // Mark try finally as breakable to avoid adding a break slot in front of it.
172 is_breakable_ = true;
173}
174
175
176void BreakableStatementChecker::VisitDebuggerStatement(
177 DebuggerStatement* stmt) {
178 // The debugger statement is breakable.
179 is_breakable_ = true;
180}
181
182
183void BreakableStatementChecker::VisitFunctionLiteral(FunctionLiteral* expr) {
184}
185
186
187void BreakableStatementChecker::VisitSharedFunctionInfoLiteral(
188 SharedFunctionInfoLiteral* expr) {
189}
190
191
192void BreakableStatementChecker::VisitConditional(Conditional* expr) {
193}
194
195
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000196void BreakableStatementChecker::VisitVariableProxy(VariableProxy* expr) {
197}
198
199
200void BreakableStatementChecker::VisitLiteral(Literal* expr) {
201}
202
203
204void BreakableStatementChecker::VisitRegExpLiteral(RegExpLiteral* expr) {
205}
206
207
208void BreakableStatementChecker::VisitObjectLiteral(ObjectLiteral* expr) {
209}
210
211
212void BreakableStatementChecker::VisitArrayLiteral(ArrayLiteral* expr) {
213}
214
215
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000216void BreakableStatementChecker::VisitAssignment(Assignment* expr) {
217 // If assigning to a property (including a global property) the assignment is
218 // breakable.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000219 VariableProxy* proxy = expr->target()->AsVariableProxy();
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000220 Property* prop = expr->target()->AsProperty();
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000221 if (prop != NULL || (proxy != NULL && proxy->var()->IsUnallocated())) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000222 is_breakable_ = true;
223 return;
224 }
225
226 // Otherwise the assignment is breakable if the assigned value is.
227 Visit(expr->value());
228}
229
230
231void BreakableStatementChecker::VisitThrow(Throw* expr) {
232 // Throw is breakable if the expression is.
233 Visit(expr->exception());
234}
235
236
237void BreakableStatementChecker::VisitProperty(Property* expr) {
238 // Property load is breakable.
239 is_breakable_ = true;
240}
241
242
243void BreakableStatementChecker::VisitCall(Call* expr) {
244 // Function calls both through IC and call stub are breakable.
245 is_breakable_ = true;
246}
247
248
249void BreakableStatementChecker::VisitCallNew(CallNew* expr) {
250 // Function calls through new are breakable.
251 is_breakable_ = true;
252}
253
254
255void BreakableStatementChecker::VisitCallRuntime(CallRuntime* expr) {
256}
257
258
259void BreakableStatementChecker::VisitUnaryOperation(UnaryOperation* expr) {
260 Visit(expr->expression());
261}
262
263
264void BreakableStatementChecker::VisitCountOperation(CountOperation* expr) {
265 Visit(expr->expression());
266}
267
268
269void BreakableStatementChecker::VisitBinaryOperation(BinaryOperation* expr) {
270 Visit(expr->left());
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +0000271 if (expr->op() != Token::AND &&
272 expr->op() != Token::OR) {
273 Visit(expr->right());
274 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000275}
276
277
278void BreakableStatementChecker::VisitCompareOperation(CompareOperation* expr) {
279 Visit(expr->left());
280 Visit(expr->right());
281}
282
283
284void BreakableStatementChecker::VisitThisFunction(ThisFunction* expr) {
285}
286
287
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000288#define __ ACCESS_MASM(masm())
289
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000290bool FullCodeGenerator::MakeCode(CompilationInfo* info) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000291 Isolate* isolate = info->isolate();
ager@chromium.org5c838252010-02-19 08:53:10 +0000292 Handle<Script> script = info->script();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000293 if (!script->IsUndefined() && !script->source()->IsUndefined()) {
294 int len = String::cast(script->source())->length();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000295 isolate->counters()->total_full_codegen_source_size()->Increment(len);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000296 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000297 if (FLAG_trace_codegen) {
298 PrintF("Full Compiler - ");
299 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000300 CodeGenerator::MakeCodePrologue(info);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000301 const int kInitialBufferSize = 4 * KB;
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000302 MacroAssembler masm(info->isolate(), NULL, kInitialBufferSize);
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000303#ifdef ENABLE_GDB_JIT_INTERFACE
304 masm.positions_recorder()->StartGDBJITLineInfoRecording();
305#endif
ager@chromium.org5c838252010-02-19 08:53:10 +0000306
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000307 FullCodeGenerator cgen(&masm, info);
yangguo@chromium.org56454712012-02-16 15:33:53 +0000308 cgen.Generate();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000309 if (cgen.HasStackOverflow()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000310 ASSERT(!isolate->has_pending_exception());
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000311 return false;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000312 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000313 unsigned table_offset = cgen.EmitStackCheckTable();
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000314
lrn@chromium.org34e60782011-09-15 07:25:40 +0000315 Code::Flags flags = Code::ComputeFlags(Code::FUNCTION);
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000316 Handle<Code> code = CodeGenerator::MakeCodeEpilogue(&masm, flags, info);
rossberg@chromium.org2c067b12012-03-19 11:01:52 +0000317 code->set_optimizable(info->IsOptimizable() &&
ulan@chromium.orgd6899c32012-05-18 14:12:25 +0000318 !info->function()->flags()->Contains(kDontOptimize) &&
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000319 info->function()->scope()->AllowsLazyCompilation());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000320 cgen.PopulateDeoptimizationData(code);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000321 cgen.PopulateTypeFeedbackInfo(code);
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000322 cgen.PopulateTypeFeedbackCells(code);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000323 code->set_has_deoptimization_support(info->HasDeoptimizationSupport());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +0000324 code->set_handler_table(*cgen.handler_table());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000325#ifdef ENABLE_DEBUGGER_SUPPORT
lrn@chromium.org34e60782011-09-15 07:25:40 +0000326 code->set_has_debug_break_slots(
327 info->isolate()->debugger()->IsDebuggerActive());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000328 code->set_compiled_optimizable(info->IsOptimizable());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000329#endif // ENABLE_DEBUGGER_SUPPORT
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000330 code->set_allow_osr_at_loop_nesting_level(0);
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000331 code->set_profiler_ticks(0);
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000332 code->set_stack_check_table_offset(table_offset);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000333 CodeGenerator::PrintCode(code, info);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000334 info->SetCode(code); // May be an empty handle.
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) {
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000356 __ dd(stack_checks_[i].id.ToInt());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000357 __ 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++) {
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000371 data->SetAstId(i, bailout_entries_[i].id);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000372 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
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +0000386void FullCodeGenerator::Initialize() {
387 // The generation of debug code must match between the snapshot code and the
388 // code that is generated later. This is assumed by the debugger when it is
389 // calculating PC offsets after generating a debug version of code. Therefore
390 // we disable the production of debug code in the full compiler if we are
391 // either generating a snapshot or we booted from a snapshot.
392 generate_debug_code_ = FLAG_debug_code &&
393 !Serializer::enabled() &&
394 !Snapshot::HaveASnapshotToStartFrom();
395 masm_->set_emit_debug_code(generate_debug_code_);
396 masm_->set_predictable_code_size(true);
397}
398
399
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000400void FullCodeGenerator::PopulateTypeFeedbackCells(Handle<Code> code) {
401 if (type_feedback_cells_.is_empty()) return;
402 int length = type_feedback_cells_.length();
403 int array_size = TypeFeedbackCells::LengthOfFixedArray(length);
404 Handle<TypeFeedbackCells> cache = Handle<TypeFeedbackCells>::cast(
405 isolate()->factory()->NewFixedArray(array_size, TENURED));
406 for (int i = 0; i < length; i++) {
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000407 cache->SetAstId(i, type_feedback_cells_[i].ast_id);
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000408 cache->SetCell(i, *type_feedback_cells_[i].cell);
409 }
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000410 TypeFeedbackInfo::cast(code->type_feedback_info())->set_type_feedback_cells(
411 *cache);
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000412}
413
414
415
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000416void FullCodeGenerator::PrepareForBailout(Expression* node, State state) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000417 PrepareForBailoutForId(node->id(), state);
418}
419
420
421void FullCodeGenerator::RecordJSReturnSite(Call* call) {
422 // We record the offset of the function return so we can rebuild the frame
423 // if the function was inlined, i.e., this is the return address in the
424 // inlined function's frame.
425 //
426 // The state is ignored. We defensively set it to TOS_REG, which is the
427 // real state of the unoptimized code at the return site.
428 PrepareForBailoutForId(call->ReturnId(), TOS_REG);
429#ifdef DEBUG
430 // In debug builds, mark the return so we can verify that this function
431 // was called.
432 ASSERT(!call->return_is_recorded_);
433 call->return_is_recorded_ = true;
434#endif
435}
436
437
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000438void FullCodeGenerator::PrepareForBailoutForId(BailoutId id, State state) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000439 // There's no need to prepare this code for bailouts from already optimized
440 // code or code that can't be optimized.
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000441 if (!info_->HasDeoptimizationSupport()) return;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000442 unsigned pc_and_state =
443 StateField::encode(state) | PcField::encode(masm_->pc_offset());
yangguo@chromium.org56454712012-02-16 15:33:53 +0000444 ASSERT(Smi::IsValid(pc_and_state));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000445 BailoutEntry entry = { id, pc_and_state };
446#ifdef DEBUG
yangguo@chromium.org659ceec2012-01-26 07:37:54 +0000447 if (FLAG_enable_slow_asserts) {
448 // Assert that we don't have multiple bailout entries for the same node.
449 for (int i = 0; i < bailout_entries_.length(); i++) {
450 if (bailout_entries_.at(i).id == entry.id) {
451 AstPrinter printer;
452 PrintF("%s", printer.PrintProgram(info_->function()));
453 UNREACHABLE();
454 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000455 }
456 }
457#endif // DEBUG
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000458 bailout_entries_.Add(entry, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000459}
460
461
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000462void FullCodeGenerator::RecordTypeFeedbackCell(
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000463 TypeFeedbackId id, Handle<JSGlobalPropertyCell> cell) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000464 TypeFeedbackCellEntry entry = { id, cell };
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000465 type_feedback_cells_.Add(entry, zone());
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000466}
467
468
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000469void FullCodeGenerator::RecordStackCheck(BailoutId ast_id) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000470 // The pc offset does not need to be encoded and packed together with a
471 // state.
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +0000472 ASSERT(masm_->pc_offset() > 0);
473 BailoutEntry entry = { ast_id, static_cast<unsigned>(masm_->pc_offset()) };
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000474 stack_checks_.Add(entry, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000475}
476
477
ricow@chromium.org65fae842010-08-25 15:26:24 +0000478bool FullCodeGenerator::ShouldInlineSmiCase(Token::Value op) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000479 // Inline smi case inside loops, but not division and modulo which
480 // are too complicated and take up too much space.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000481 if (op == Token::DIV ||op == Token::MOD) return false;
482 if (FLAG_always_inline_smi_code) return true;
483 return loop_depth_ > 0;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000484}
485
486
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000487void FullCodeGenerator::EffectContext::Plug(Register reg) const {
488}
489
490
491void FullCodeGenerator::AccumulatorValueContext::Plug(Register reg) const {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000492 __ Move(result_register(), reg);
493}
494
495
496void FullCodeGenerator::StackValueContext::Plug(Register reg) const {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000497 __ push(reg);
498}
499
500
501void FullCodeGenerator::TestContext::Plug(Register reg) const {
502 // For simplicity we always test the accumulator register.
503 __ Move(result_register(), reg);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000504 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000505 codegen()->DoTest(this);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000506}
507
508
509void FullCodeGenerator::EffectContext::PlugTOS() const {
510 __ Drop(1);
511}
512
513
514void FullCodeGenerator::AccumulatorValueContext::PlugTOS() const {
515 __ pop(result_register());
516}
517
518
519void FullCodeGenerator::StackValueContext::PlugTOS() const {
520}
521
522
523void FullCodeGenerator::TestContext::PlugTOS() const {
524 // For simplicity we always test the accumulator register.
525 __ pop(result_register());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000526 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000527 codegen()->DoTest(this);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000528}
529
530
531void FullCodeGenerator::EffectContext::PrepareTest(
532 Label* materialize_true,
533 Label* materialize_false,
534 Label** if_true,
535 Label** if_false,
536 Label** fall_through) const {
537 // In an effect context, the true and the false case branch to the
538 // same label.
539 *if_true = *if_false = *fall_through = materialize_true;
540}
541
542
543void FullCodeGenerator::AccumulatorValueContext::PrepareTest(
544 Label* materialize_true,
545 Label* materialize_false,
546 Label** if_true,
547 Label** if_false,
548 Label** fall_through) const {
549 *if_true = *fall_through = materialize_true;
550 *if_false = materialize_false;
551}
552
553
554void FullCodeGenerator::StackValueContext::PrepareTest(
555 Label* materialize_true,
556 Label* materialize_false,
557 Label** if_true,
558 Label** if_false,
559 Label** fall_through) const {
560 *if_true = *fall_through = materialize_true;
561 *if_false = materialize_false;
562}
563
564
565void FullCodeGenerator::TestContext::PrepareTest(
566 Label* materialize_true,
567 Label* materialize_false,
568 Label** if_true,
569 Label** if_false,
570 Label** fall_through) const {
571 *if_true = true_label_;
572 *if_false = false_label_;
573 *fall_through = fall_through_;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000574}
575
576
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000577void FullCodeGenerator::DoTest(const TestContext* context) {
578 DoTest(context->condition(),
579 context->true_label(),
580 context->false_label(),
581 context->fall_through());
582}
583
584
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000585void FullCodeGenerator::VisitDeclarations(
586 ZoneList<Declaration*>* declarations) {
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000587 ZoneList<Handle<Object> >* saved_globals = globals_;
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000588 ZoneList<Handle<Object> > inner_globals(10, zone());
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000589 globals_ = &inner_globals;
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000590
591 AstVisitor::VisitDeclarations(declarations);
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000592 if (!globals_->is_empty()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000593 // Invoke the platform-dependent code generator to do the actual
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000594 // declaration the global functions and variables.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000595 Handle<FixedArray> array =
596 isolate()->factory()->NewFixedArray(globals_->length(), TENURED);
597 for (int i = 0; i < globals_->length(); ++i)
598 array->set(i, *globals_->at(i));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000599 DeclareGlobals(array);
600 }
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000601
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000602 globals_ = saved_globals;
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000603}
604
605
606void FullCodeGenerator::VisitModuleLiteral(ModuleLiteral* module) {
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000607 // Allocate a module context statically.
608 Block* block = module->body();
609 Scope* saved_scope = scope();
610 scope_ = block->scope();
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000611 Interface* interface = module->interface();
612 Handle<JSModule> instance = interface->Instance();
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000613
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000614 Comment cmnt(masm_, "[ ModuleLiteral");
615 SetStatementPosition(block);
616
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000617 // Set up module context.
618 __ Push(instance);
619 __ CallRuntime(Runtime::kPushModuleContext, 1);
620 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000621
622 {
623 Comment cmnt(masm_, "[ Declarations");
624 VisitDeclarations(scope_->declarations());
625 }
626
627 scope_ = saved_scope;
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000628 // Pop module context.
629 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
630 // Update local stack frame context field.
631 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000632}
633
634
635void FullCodeGenerator::VisitModuleVariable(ModuleVariable* module) {
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000636 // Nothing to do.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000637 // The instance object is resolved statically through the module's interface.
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000638}
639
640
641void FullCodeGenerator::VisitModulePath(ModulePath* module) {
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000642 // Nothing to do.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000643 // The instance object is resolved statically through the module's interface.
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000644}
645
646
647void FullCodeGenerator::VisitModuleUrl(ModuleUrl* decl) {
648 // TODO(rossberg)
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000649}
650
651
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000652int FullCodeGenerator::DeclareGlobalsFlags() {
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000653 ASSERT(DeclareGlobalsLanguageMode::is_valid(language_mode()));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000654 return DeclareGlobalsEvalFlag::encode(is_eval()) |
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000655 DeclareGlobalsNativeFlag::encode(is_native()) |
656 DeclareGlobalsLanguageMode::encode(language_mode());
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000657}
658
659
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000660void FullCodeGenerator::SetFunctionPosition(FunctionLiteral* fun) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000661 CodeGenerator::RecordPositions(masm_, fun->start_position());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000662}
663
664
665void FullCodeGenerator::SetReturnPosition(FunctionLiteral* fun) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000666 CodeGenerator::RecordPositions(masm_, fun->end_position() - 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000667}
668
669
670void FullCodeGenerator::SetStatementPosition(Statement* stmt) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000671#ifdef ENABLE_DEBUGGER_SUPPORT
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000672 if (!isolate()->debugger()->IsDebuggerActive()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000673 CodeGenerator::RecordPositions(masm_, stmt->statement_pos());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000674 } else {
675 // Check if the statement will be breakable without adding a debug break
676 // slot.
677 BreakableStatementChecker checker;
678 checker.Check(stmt);
679 // Record the statement position right here if the statement is not
680 // breakable. For breakable statements the actual recording of the
681 // position will be postponed to the breakable code (typically an IC).
682 bool position_recorded = CodeGenerator::RecordPositions(
683 masm_, stmt->statement_pos(), !checker.is_breakable());
684 // If the position recording did record a new position generate a debug
685 // break slot to make the statement breakable.
686 if (position_recorded) {
687 Debug::GenerateSlot(masm_);
688 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000689 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000690#else
691 CodeGenerator::RecordPositions(masm_, stmt->statement_pos());
692#endif
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000693}
694
695
696void FullCodeGenerator::SetExpressionPosition(Expression* expr, int pos) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000697#ifdef ENABLE_DEBUGGER_SUPPORT
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000698 if (!isolate()->debugger()->IsDebuggerActive()) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000699 CodeGenerator::RecordPositions(masm_, pos);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000700 } else {
701 // Check if the expression will be breakable without adding a debug break
702 // slot.
703 BreakableStatementChecker checker;
704 checker.Check(expr);
705 // Record a statement position right here if the expression is not
706 // breakable. For breakable expressions the actual recording of the
707 // position will be postponed to the breakable code (typically an IC).
708 // NOTE this will record a statement position for something which might
709 // not be a statement. As stepping in the debugger will only stop at
710 // statement positions this is used for e.g. the condition expression of
711 // a do while loop.
712 bool position_recorded = CodeGenerator::RecordPositions(
713 masm_, pos, !checker.is_breakable());
714 // If the position recording did record a new position generate a debug
715 // break slot to make the statement breakable.
716 if (position_recorded) {
717 Debug::GenerateSlot(masm_);
718 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000719 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000720#else
721 CodeGenerator::RecordPositions(masm_, pos);
722#endif
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000723}
724
725
726void FullCodeGenerator::SetStatementPosition(int pos) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000727 CodeGenerator::RecordPositions(masm_, pos);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000728}
729
730
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000731void FullCodeGenerator::SetSourcePosition(int pos) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000732 if (pos != RelocInfo::kNoPosition) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000733 masm_->positions_recorder()->RecordPosition(pos);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000734 }
735}
736
737
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000738// Lookup table for code generators for special runtime calls which are
739// generated inline.
740#define INLINE_FUNCTION_GENERATOR_ADDRESS(Name, argc, ressize) \
741 &FullCodeGenerator::Emit##Name,
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000742
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000743const FullCodeGenerator::InlineFunctionGenerator
744 FullCodeGenerator::kInlineFunctionGenerators[] = {
745 INLINE_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS)
746 INLINE_RUNTIME_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS)
747 };
748#undef INLINE_FUNCTION_GENERATOR_ADDRESS
749
750
751FullCodeGenerator::InlineFunctionGenerator
752 FullCodeGenerator::FindInlineFunctionGenerator(Runtime::FunctionId id) {
whesse@chromium.org023421e2010-12-21 12:19:12 +0000753 int lookup_index =
754 static_cast<int>(id) - static_cast<int>(Runtime::kFirstInlineFunction);
755 ASSERT(lookup_index >= 0);
756 ASSERT(static_cast<size_t>(lookup_index) <
757 ARRAY_SIZE(kInlineFunctionGenerators));
758 return kInlineFunctionGenerators[lookup_index];
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000759}
760
761
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000762void FullCodeGenerator::EmitInlineRuntimeCall(CallRuntime* expr) {
763 const Runtime::Function* function = expr->function();
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000764 ASSERT(function != NULL);
765 ASSERT(function->intrinsic_type == Runtime::INLINE);
766 InlineFunctionGenerator generator =
767 FindInlineFunctionGenerator(function->function_id);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000768 ((*this).*(generator))(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000769}
770
771
772void FullCodeGenerator::VisitBinaryOperation(BinaryOperation* expr) {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000773 switch (expr->op()) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000774 case Token::COMMA:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000775 return VisitComma(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000776 case Token::OR:
777 case Token::AND:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000778 return VisitLogicalExpression(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000779 default:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000780 return VisitArithmeticExpression(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000781 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000782}
783
784
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000785void FullCodeGenerator::VisitInDuplicateContext(Expression* expr) {
786 if (context()->IsEffect()) {
787 VisitForEffect(expr);
788 } else if (context()->IsAccumulatorValue()) {
789 VisitForAccumulatorValue(expr);
790 } else if (context()->IsStackValue()) {
791 VisitForStackValue(expr);
792 } else if (context()->IsTest()) {
793 const TestContext* test = TestContext::cast(context());
794 VisitForControl(expr, test->true_label(), test->false_label(),
795 test->fall_through());
796 }
797}
798
799
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000800void FullCodeGenerator::VisitComma(BinaryOperation* expr) {
801 Comment cmnt(masm_, "[ Comma");
802 VisitForEffect(expr->left());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000803 VisitInDuplicateContext(expr->right());
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000804}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000805
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000806
807void FullCodeGenerator::VisitLogicalExpression(BinaryOperation* expr) {
808 bool is_logical_and = expr->op() == Token::AND;
809 Comment cmnt(masm_, is_logical_and ? "[ Logical AND" : "[ Logical OR");
810 Expression* left = expr->left();
811 Expression* right = expr->right();
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000812 BailoutId right_id = expr->RightId();
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000813 Label done;
814
815 if (context()->IsTest()) {
816 Label eval_right;
817 const TestContext* test = TestContext::cast(context());
818 if (is_logical_and) {
819 VisitForControl(left, &eval_right, test->false_label(), &eval_right);
820 } else {
821 VisitForControl(left, test->true_label(), &eval_right, &eval_right);
822 }
823 PrepareForBailoutForId(right_id, NO_REGISTERS);
824 __ bind(&eval_right);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000825
826 } else if (context()->IsAccumulatorValue()) {
827 VisitForAccumulatorValue(left);
828 // We want the value in the accumulator for the test, and on the stack in
829 // case we need it.
830 __ push(result_register());
831 Label discard, restore;
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000832 if (is_logical_and) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000833 DoTest(left, &discard, &restore, &restore);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000834 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000835 DoTest(left, &restore, &discard, &restore);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000836 }
837 __ bind(&restore);
838 __ pop(result_register());
839 __ jmp(&done);
840 __ bind(&discard);
841 __ Drop(1);
842 PrepareForBailoutForId(right_id, NO_REGISTERS);
843
844 } else if (context()->IsStackValue()) {
845 VisitForAccumulatorValue(left);
846 // We want the value in the accumulator for the test, and on the stack in
847 // case we need it.
848 __ push(result_register());
849 Label discard;
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000850 if (is_logical_and) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000851 DoTest(left, &discard, &done, &discard);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000852 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000853 DoTest(left, &done, &discard, &discard);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000854 }
855 __ bind(&discard);
856 __ Drop(1);
857 PrepareForBailoutForId(right_id, NO_REGISTERS);
858
859 } else {
860 ASSERT(context()->IsEffect());
861 Label eval_right;
862 if (is_logical_and) {
863 VisitForControl(left, &eval_right, &done, &eval_right);
864 } else {
865 VisitForControl(left, &done, &eval_right, &eval_right);
866 }
867 PrepareForBailoutForId(right_id, NO_REGISTERS);
868 __ bind(&eval_right);
869 }
870
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000871 VisitInDuplicateContext(right);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000872 __ bind(&done);
873}
874
875
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000876void FullCodeGenerator::VisitArithmeticExpression(BinaryOperation* expr) {
877 Token::Value op = expr->op();
878 Comment cmnt(masm_, "[ ArithmeticExpression");
879 Expression* left = expr->left();
880 Expression* right = expr->right();
881 OverwriteMode mode =
882 left->ResultOverwriteAllowed()
883 ? OVERWRITE_LEFT
884 : (right->ResultOverwriteAllowed() ? OVERWRITE_RIGHT : NO_OVERWRITE);
885
886 VisitForStackValue(left);
887 VisitForAccumulatorValue(right);
888
889 SetSourcePosition(expr->position());
890 if (ShouldInlineSmiCase(op)) {
891 EmitInlineSmiBinaryOp(expr, op, mode, left, right);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000892 } else {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000893 EmitBinaryOp(expr, op, mode);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000894 }
895}
896
897
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000898void FullCodeGenerator::VisitBlock(Block* stmt) {
899 Comment cmnt(masm_, "[ Block");
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000900 NestedBlock nested_block(this, stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000901 SetStatementPosition(stmt);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000902
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000903 Scope* saved_scope = scope();
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000904 // Push a block context when entering a block with block scoped variables.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000905 if (stmt->scope() != NULL) {
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000906 scope_ = stmt->scope();
907 if (scope_->is_module_scope()) {
908 // If this block is a module body, then we have already allocated and
909 // initialized the declarations earlier. Just push the context.
910 ASSERT(!scope_->interface()->Instance().is_null());
911 __ Push(scope_->interface()->Instance());
912 __ CallRuntime(Runtime::kPushModuleContext, 1);
913 StoreToFrameField(
914 StandardFrameConstants::kContextOffset, context_register());
915 } else {
916 { Comment cmnt(masm_, "[ Extend block context");
917 Handle<ScopeInfo> scope_info = scope_->GetScopeInfo();
918 int heap_slots =
919 scope_info->ContextLength() - Context::MIN_CONTEXT_SLOTS;
920 __ Push(scope_info);
921 PushFunctionArgumentForContextAllocation();
922 if (heap_slots <= FastNewBlockContextStub::kMaximumSlots) {
923 FastNewBlockContextStub stub(heap_slots);
924 __ CallStub(&stub);
925 } else {
926 __ CallRuntime(Runtime::kPushBlockContext, 2);
927 }
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000928
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000929 // Replace the context stored in the frame.
930 StoreToFrameField(StandardFrameConstants::kContextOffset,
931 context_register());
932 }
933 { Comment cmnt(masm_, "[ Declarations");
934 VisitDeclarations(scope_->declarations());
935 }
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000936 }
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.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000945 if (stmt->scope() != NULL) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000946 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);
erik.corry@gmail.combbceb572012-03-09 10:52:05 +00001162
1163 // Set statement position for a break slot before entering the for-body.
1164 SetStatementPosition(stmt);
1165
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001166 if (stmt->init() != NULL) {
1167 Visit(stmt->init());
1168 }
1169
1170 increment_loop_depth();
1171 // Emit the test at the bottom of the loop (even if empty).
1172 __ jmp(&test);
1173
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001174 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001175 __ bind(&body);
1176 Visit(stmt->body());
1177
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001178 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001179 __ bind(loop_statement.continue_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001180 if (stmt->next() != NULL) {
1181 Visit(stmt->next());
1182 }
1183
ricow@chromium.org65fae842010-08-25 15:26:24 +00001184 // Emit the statement position here as this is where the for
1185 // statement code starts.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001186 SetStatementPosition(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001187
1188 // Check stack before looping.
yangguo@chromium.org56454712012-02-16 15:33:53 +00001189 EmitStackCheck(stmt, &body);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001190
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001191 __ bind(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001192 if (stmt->cond() != NULL) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00001193 VisitForControl(stmt->cond(),
1194 &body,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001195 loop_statement.break_label(),
1196 loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001197 } else {
1198 __ jmp(&body);
1199 }
1200
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001201 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001202 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001203 decrement_loop_depth();
1204}
1205
1206
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001207void FullCodeGenerator::VisitTryCatchStatement(TryCatchStatement* stmt) {
1208 Comment cmnt(masm_, "[ TryCatchStatement");
1209 SetStatementPosition(stmt);
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001210 // The try block adds a handler to the exception handler chain before
1211 // entering, and removes it again when exiting normally. If an exception
1212 // is thrown during execution of the try block, the handler is consumed
1213 // and control is passed to the catch block with the exception in the
1214 // result register.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001215
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001216 Label try_entry, handler_entry, exit;
1217 __ jmp(&try_entry);
1218 __ bind(&handler_entry);
1219 handler_table()->set(stmt->index(), Smi::FromInt(handler_entry.pos()));
1220 // Exception handler code, the exception is in the result register.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001221 // Extend the context before executing the catch block.
1222 { Comment cmnt(masm_, "[ Extend catch context");
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001223 __ Push(stmt->variable()->name());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001224 __ push(result_register());
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00001225 PushFunctionArgumentForContextAllocation();
1226 __ CallRuntime(Runtime::kPushCatchContext, 3);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001227 StoreToFrameField(StandardFrameConstants::kContextOffset,
1228 context_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001229 }
1230
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001231 Scope* saved_scope = scope();
1232 scope_ = stmt->scope();
1233 ASSERT(scope_->declarations()->is_empty());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001234 { WithOrCatch catch_body(this);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001235 Visit(stmt->catch_block());
1236 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001237 // Restore the context.
1238 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1239 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001240 scope_ = saved_scope;
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001241 __ jmp(&exit);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001242
1243 // Try block code. Sets up the exception handler chain.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001244 __ bind(&try_entry);
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00001245 __ PushTryHandler(StackHandler::CATCH, stmt->index());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001246 { TryCatch try_body(this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001247 Visit(stmt->try_block());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001248 }
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001249 __ PopTryHandler();
1250 __ bind(&exit);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001251}
1252
1253
1254void FullCodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* stmt) {
1255 Comment cmnt(masm_, "[ TryFinallyStatement");
1256 SetStatementPosition(stmt);
1257 // Try finally is compiled by setting up a try-handler on the stack while
1258 // executing the try body, and removing it again afterwards.
1259 //
1260 // The try-finally construct can enter the finally block in three ways:
1261 // 1. By exiting the try-block normally. This removes the try-handler and
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001262 // calls the finally block code before continuing.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001263 // 2. By exiting the try-block with a function-local control flow transfer
1264 // (break/continue/return). The site of the, e.g., break removes the
1265 // try handler and calls the finally block code before continuing
1266 // its outward control transfer.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001267 // 3. By exiting the try-block with a thrown exception.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001268 // This can happen in nested function calls. It traverses the try-handler
1269 // chain and consumes the try-handler entry before jumping to the
1270 // handler code. The handler code then calls the finally-block before
1271 // rethrowing the exception.
1272 //
1273 // The finally block must assume a return address on top of the stack
1274 // (or in the link register on ARM chips) and a value (return value or
1275 // exception) in the result register (rax/eax/r0), both of which must
1276 // be preserved. The return address isn't GC-safe, so it should be
1277 // cooked before GC.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001278 Label try_entry, handler_entry, finally_entry;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001279
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001280 // Jump to try-handler setup and try-block code.
1281 __ jmp(&try_entry);
1282 __ bind(&handler_entry);
1283 handler_table()->set(stmt->index(), Smi::FromInt(handler_entry.pos()));
1284 // Exception handler code. This code is only executed when an exception
1285 // is thrown. The exception is in the result register, and must be
1286 // preserved by the finally block. Call the finally block and then
1287 // rethrow the exception if it returns.
1288 __ Call(&finally_entry);
1289 __ push(result_register());
1290 __ CallRuntime(Runtime::kReThrow, 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001291
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001292 // Finally block implementation.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001293 __ bind(&finally_entry);
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001294 EnterFinallyBlock();
1295 { Finally finally_body(this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001296 Visit(stmt->finally_block());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001297 }
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001298 ExitFinallyBlock(); // Return to the calling code.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001299
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001300 // Set up try handler.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001301 __ bind(&try_entry);
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00001302 __ PushTryHandler(StackHandler::FINALLY, stmt->index());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001303 { TryFinally try_body(this, &finally_entry);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001304 Visit(stmt->try_block());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001305 }
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001306 __ PopTryHandler();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001307 // Execute the finally block on the way out. Clobber the unpredictable
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001308 // value in the result register with one that's safe for GC because the
1309 // finally block will unconditionally preserve the result register on the
1310 // stack.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001311 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001312 __ Call(&finally_entry);
1313}
1314
1315
1316void FullCodeGenerator::VisitDebuggerStatement(DebuggerStatement* stmt) {
1317#ifdef ENABLE_DEBUGGER_SUPPORT
1318 Comment cmnt(masm_, "[ DebuggerStatement");
1319 SetStatementPosition(stmt);
1320
ager@chromium.org5c838252010-02-19 08:53:10 +00001321 __ DebugBreak();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001322 // Ignore the return value.
1323#endif
1324}
1325
1326
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001327void FullCodeGenerator::VisitConditional(Conditional* expr) {
1328 Comment cmnt(masm_, "[ Conditional");
1329 Label true_case, false_case, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001330 VisitForControl(expr->condition(), &true_case, &false_case, &true_case);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001331
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001332 PrepareForBailoutForId(expr->ThenId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001333 __ bind(&true_case);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001334 SetExpressionPosition(expr->then_expression(),
1335 expr->then_expression_position());
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001336 if (context()->IsTest()) {
1337 const TestContext* for_test = TestContext::cast(context());
1338 VisitForControl(expr->then_expression(),
1339 for_test->true_label(),
1340 for_test->false_label(),
1341 NULL);
1342 } else {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001343 VisitInDuplicateContext(expr->then_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001344 __ jmp(&done);
1345 }
1346
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001347 PrepareForBailoutForId(expr->ElseId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001348 __ bind(&false_case);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001349 SetExpressionPosition(expr->else_expression(),
1350 expr->else_expression_position());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001351 VisitInDuplicateContext(expr->else_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001352 // If control flow falls through Visit, merge it with true case here.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001353 if (!context()->IsTest()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001354 __ bind(&done);
1355 }
1356}
1357
1358
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001359void FullCodeGenerator::VisitLiteral(Literal* expr) {
1360 Comment cmnt(masm_, "[ Literal");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001361 context()->Plug(expr->handle());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001362}
1363
1364
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001365void FullCodeGenerator::VisitFunctionLiteral(FunctionLiteral* expr) {
1366 Comment cmnt(masm_, "[ FunctionLiteral");
1367
1368 // Build the function boilerplate and instantiate it.
1369 Handle<SharedFunctionInfo> function_info =
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001370 Compiler::BuildFunctionInfo(expr, script());
1371 if (function_info.is_null()) {
1372 SetStackOverflow();
1373 return;
1374 }
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001375 EmitNewClosure(function_info, expr->pretenure());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001376}
1377
1378
1379void FullCodeGenerator::VisitSharedFunctionInfoLiteral(
1380 SharedFunctionInfoLiteral* expr) {
1381 Comment cmnt(masm_, "[ SharedFunctionInfoLiteral");
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001382 EmitNewClosure(expr->shared_function_info(), false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001383}
1384
1385
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001386void FullCodeGenerator::VisitThrow(Throw* expr) {
1387 Comment cmnt(masm_, "[ Throw");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001388 VisitForStackValue(expr->exception());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001389 __ CallRuntime(Runtime::kThrow, 1);
1390 // Never returns here.
1391}
1392
1393
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001394FullCodeGenerator::NestedStatement* FullCodeGenerator::TryCatch::Exit(
1395 int* stack_depth,
1396 int* context_length) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001397 // The macros used here must preserve the result register.
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001398 __ Drop(*stack_depth);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001399 __ PopTryHandler();
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001400 *stack_depth = 0;
1401 return previous_;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001402}
1403
ricow@chromium.org65fae842010-08-25 15:26:24 +00001404
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001405bool FullCodeGenerator::TryLiteralCompare(CompareOperation* expr) {
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001406 Expression* sub_expr;
ager@chromium.org04921a82011-06-27 13:21:41 +00001407 Handle<String> check;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001408 if (expr->IsLiteralCompareTypeof(&sub_expr, &check)) {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001409 EmitLiteralCompareTypeof(expr, sub_expr, check);
ager@chromium.org04921a82011-06-27 13:21:41 +00001410 return true;
1411 }
1412
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001413 if (expr->IsLiteralCompareUndefined(&sub_expr)) {
1414 EmitLiteralCompareNil(expr, sub_expr, kUndefinedValue);
1415 return true;
1416 }
1417
1418 if (expr->IsLiteralCompareNull(&sub_expr)) {
1419 EmitLiteralCompareNil(expr, sub_expr, kNullValue);
ager@chromium.org04921a82011-06-27 13:21:41 +00001420 return true;
1421 }
1422
1423 return false;
1424}
1425
1426
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001427#undef __
1428
1429
1430} } // namespace v8::internal