blob: 4da4e531ee809da84ad3902ba5ee20b2cbe878bd [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
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000306 FullCodeGenerator cgen(&masm, info, isolate->zone());
yangguo@chromium.org56454712012-02-16 15:33:53 +0000307 cgen.Generate();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000308 if (cgen.HasStackOverflow()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000309 ASSERT(!isolate->has_pending_exception());
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000310 return false;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000311 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000312 unsigned table_offset = cgen.EmitStackCheckTable();
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000313
lrn@chromium.org34e60782011-09-15 07:25:40 +0000314 Code::Flags flags = Code::ComputeFlags(Code::FUNCTION);
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000315 Handle<Code> code = CodeGenerator::MakeCodeEpilogue(&masm, flags, info);
rossberg@chromium.org2c067b12012-03-19 11:01:52 +0000316 code->set_optimizable(info->IsOptimizable() &&
ulan@chromium.orgd6899c32012-05-18 14:12:25 +0000317 !info->function()->flags()->Contains(kDontOptimize) &&
318 info->function()->scope()->AllowsLazyRecompilation());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000319 cgen.PopulateDeoptimizationData(code);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000320 cgen.PopulateTypeFeedbackInfo(code);
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000321 cgen.PopulateTypeFeedbackCells(code);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000322 code->set_has_deoptimization_support(info->HasDeoptimizationSupport());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +0000323 code->set_handler_table(*cgen.handler_table());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000324#ifdef ENABLE_DEBUGGER_SUPPORT
lrn@chromium.org34e60782011-09-15 07:25:40 +0000325 code->set_has_debug_break_slots(
326 info->isolate()->debugger()->IsDebuggerActive());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000327 code->set_compiled_optimizable(info->IsOptimizable());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000328#endif // ENABLE_DEBUGGER_SUPPORT
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000329 code->set_allow_osr_at_loop_nesting_level(0);
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000330 code->set_profiler_ticks(0);
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000331 code->set_stack_check_table_offset(table_offset);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000332 CodeGenerator::PrintCode(code, info);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000333 info->SetCode(code); // May be an empty handle.
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000334#ifdef ENABLE_GDB_JIT_INTERFACE
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000335 if (FLAG_gdbjit && !code.is_null()) {
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000336 GDBJITLineInfo* lineinfo =
337 masm.positions_recorder()->DetachGDBJITLineInfo();
338
339 GDBJIT(RegisterDetailedLineInfo(*code, lineinfo));
340 }
341#endif
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000342 return !code.is_null();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000343}
344
345
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000346unsigned FullCodeGenerator::EmitStackCheckTable() {
347 // The stack check table consists of a length (in number of entries)
348 // field, and then a sequence of entries. Each entry is a pair of AST id
349 // and code-relative pc offset.
350 masm()->Align(kIntSize);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000351 unsigned offset = masm()->pc_offset();
352 unsigned length = stack_checks_.length();
353 __ dd(length);
354 for (unsigned i = 0; i < length; ++i) {
355 __ dd(stack_checks_[i].id);
356 __ dd(stack_checks_[i].pc_and_state);
357 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000358 return offset;
359}
360
361
362void FullCodeGenerator::PopulateDeoptimizationData(Handle<Code> code) {
363 // Fill in the deoptimization information.
364 ASSERT(info_->HasDeoptimizationSupport() || bailout_entries_.is_empty());
365 if (!info_->HasDeoptimizationSupport()) return;
366 int length = bailout_entries_.length();
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000367 Handle<DeoptimizationOutputData> data = isolate()->factory()->
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000368 NewDeoptimizationOutputData(length, TENURED);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000369 for (int i = 0; i < length; i++) {
370 data->SetAstId(i, Smi::FromInt(bailout_entries_[i].id));
371 data->SetPcAndState(i, Smi::FromInt(bailout_entries_[i].pc_and_state));
372 }
373 code->set_deoptimization_data(*data);
374}
375
376
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000377void FullCodeGenerator::PopulateTypeFeedbackInfo(Handle<Code> code) {
378 Handle<TypeFeedbackInfo> info = isolate()->factory()->NewTypeFeedbackInfo();
379 info->set_ic_total_count(ic_total_count_);
yangguo@chromium.orga7d3df92012-02-27 11:46:55 +0000380 ASSERT(!isolate()->heap()->InNewSpace(*info));
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000381 code->set_type_feedback_info(*info);
382}
383
384
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000385void FullCodeGenerator::PopulateTypeFeedbackCells(Handle<Code> code) {
386 if (type_feedback_cells_.is_empty()) return;
387 int length = type_feedback_cells_.length();
388 int array_size = TypeFeedbackCells::LengthOfFixedArray(length);
389 Handle<TypeFeedbackCells> cache = Handle<TypeFeedbackCells>::cast(
390 isolate()->factory()->NewFixedArray(array_size, TENURED));
391 for (int i = 0; i < length; i++) {
392 cache->SetAstId(i, Smi::FromInt(type_feedback_cells_[i].ast_id));
393 cache->SetCell(i, *type_feedback_cells_[i].cell);
394 }
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000395 TypeFeedbackInfo::cast(code->type_feedback_info())->set_type_feedback_cells(
396 *cache);
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000397}
398
399
400
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000401void FullCodeGenerator::PrepareForBailout(Expression* node, State state) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000402 PrepareForBailoutForId(node->id(), state);
403}
404
405
406void FullCodeGenerator::RecordJSReturnSite(Call* call) {
407 // We record the offset of the function return so we can rebuild the frame
408 // if the function was inlined, i.e., this is the return address in the
409 // inlined function's frame.
410 //
411 // The state is ignored. We defensively set it to TOS_REG, which is the
412 // real state of the unoptimized code at the return site.
413 PrepareForBailoutForId(call->ReturnId(), TOS_REG);
414#ifdef DEBUG
415 // In debug builds, mark the return so we can verify that this function
416 // was called.
417 ASSERT(!call->return_is_recorded_);
418 call->return_is_recorded_ = true;
419#endif
420}
421
422
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +0000423void FullCodeGenerator::PrepareForBailoutForId(unsigned id, State state) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000424 // There's no need to prepare this code for bailouts from already optimized
425 // code or code that can't be optimized.
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000426 if (!info_->HasDeoptimizationSupport()) return;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000427 unsigned pc_and_state =
428 StateField::encode(state) | PcField::encode(masm_->pc_offset());
yangguo@chromium.org56454712012-02-16 15:33:53 +0000429 ASSERT(Smi::IsValid(pc_and_state));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000430 BailoutEntry entry = { id, pc_and_state };
431#ifdef DEBUG
yangguo@chromium.org659ceec2012-01-26 07:37:54 +0000432 if (FLAG_enable_slow_asserts) {
433 // Assert that we don't have multiple bailout entries for the same node.
434 for (int i = 0; i < bailout_entries_.length(); i++) {
435 if (bailout_entries_.at(i).id == entry.id) {
436 AstPrinter printer;
437 PrintF("%s", printer.PrintProgram(info_->function()));
438 UNREACHABLE();
439 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000440 }
441 }
442#endif // DEBUG
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000443 bailout_entries_.Add(entry, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000444}
445
446
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000447void FullCodeGenerator::RecordTypeFeedbackCell(
448 unsigned id, Handle<JSGlobalPropertyCell> cell) {
449 TypeFeedbackCellEntry entry = { id, cell };
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000450 type_feedback_cells_.Add(entry, zone());
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000451}
452
453
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +0000454void FullCodeGenerator::RecordStackCheck(unsigned ast_id) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000455 // The pc offset does not need to be encoded and packed together with a
456 // state.
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +0000457 ASSERT(masm_->pc_offset() > 0);
458 BailoutEntry entry = { ast_id, static_cast<unsigned>(masm_->pc_offset()) };
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000459 stack_checks_.Add(entry, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000460}
461
462
ricow@chromium.org65fae842010-08-25 15:26:24 +0000463bool FullCodeGenerator::ShouldInlineSmiCase(Token::Value op) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000464 // Inline smi case inside loops, but not division and modulo which
465 // are too complicated and take up too much space.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000466 if (op == Token::DIV ||op == Token::MOD) return false;
467 if (FLAG_always_inline_smi_code) return true;
468 return loop_depth_ > 0;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000469}
470
471
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000472void FullCodeGenerator::EffectContext::Plug(Register reg) const {
473}
474
475
476void FullCodeGenerator::AccumulatorValueContext::Plug(Register reg) const {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000477 __ Move(result_register(), reg);
478}
479
480
481void FullCodeGenerator::StackValueContext::Plug(Register reg) const {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000482 __ push(reg);
483}
484
485
486void FullCodeGenerator::TestContext::Plug(Register reg) const {
487 // For simplicity we always test the accumulator register.
488 __ Move(result_register(), reg);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000489 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000490 codegen()->DoTest(this);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000491}
492
493
494void FullCodeGenerator::EffectContext::PlugTOS() const {
495 __ Drop(1);
496}
497
498
499void FullCodeGenerator::AccumulatorValueContext::PlugTOS() const {
500 __ pop(result_register());
501}
502
503
504void FullCodeGenerator::StackValueContext::PlugTOS() const {
505}
506
507
508void FullCodeGenerator::TestContext::PlugTOS() const {
509 // For simplicity we always test the accumulator register.
510 __ pop(result_register());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000511 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000512 codegen()->DoTest(this);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000513}
514
515
516void FullCodeGenerator::EffectContext::PrepareTest(
517 Label* materialize_true,
518 Label* materialize_false,
519 Label** if_true,
520 Label** if_false,
521 Label** fall_through) const {
522 // In an effect context, the true and the false case branch to the
523 // same label.
524 *if_true = *if_false = *fall_through = materialize_true;
525}
526
527
528void FullCodeGenerator::AccumulatorValueContext::PrepareTest(
529 Label* materialize_true,
530 Label* materialize_false,
531 Label** if_true,
532 Label** if_false,
533 Label** fall_through) const {
534 *if_true = *fall_through = materialize_true;
535 *if_false = materialize_false;
536}
537
538
539void FullCodeGenerator::StackValueContext::PrepareTest(
540 Label* materialize_true,
541 Label* materialize_false,
542 Label** if_true,
543 Label** if_false,
544 Label** fall_through) const {
545 *if_true = *fall_through = materialize_true;
546 *if_false = materialize_false;
547}
548
549
550void FullCodeGenerator::TestContext::PrepareTest(
551 Label* materialize_true,
552 Label* materialize_false,
553 Label** if_true,
554 Label** if_false,
555 Label** fall_through) const {
556 *if_true = true_label_;
557 *if_false = false_label_;
558 *fall_through = fall_through_;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000559}
560
561
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000562void FullCodeGenerator::DoTest(const TestContext* context) {
563 DoTest(context->condition(),
564 context->true_label(),
565 context->false_label(),
566 context->fall_through());
567}
568
569
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000570void FullCodeGenerator::VisitDeclarations(
571 ZoneList<Declaration*>* declarations) {
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000572 ZoneList<Handle<Object> >* saved_globals = globals_;
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000573 ZoneList<Handle<Object> > inner_globals(10, zone());
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000574 globals_ = &inner_globals;
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000575
576 AstVisitor::VisitDeclarations(declarations);
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000577 if (!globals_->is_empty()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000578 // Invoke the platform-dependent code generator to do the actual
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000579 // declaration the global functions and variables.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000580 Handle<FixedArray> array =
581 isolate()->factory()->NewFixedArray(globals_->length(), TENURED);
582 for (int i = 0; i < globals_->length(); ++i)
583 array->set(i, *globals_->at(i));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000584 DeclareGlobals(array);
585 }
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000586
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000587 globals_ = saved_globals;
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000588}
589
590
591void FullCodeGenerator::VisitModuleLiteral(ModuleLiteral* module) {
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000592 Handle<JSModule> instance = module->interface()->Instance();
593 ASSERT(!instance.is_null());
594
595 // Allocate a module context statically.
596 Block* block = module->body();
597 Scope* saved_scope = scope();
598 scope_ = block->scope();
599 Handle<ScopeInfo> scope_info = scope_->GetScopeInfo();
600
601 // Generate code for module creation and linking.
602 Comment cmnt(masm_, "[ ModuleLiteral");
603 SetStatementPosition(block);
604
605 if (scope_info->HasContext()) {
606 // Set up module context.
607 __ Push(scope_info);
608 __ Push(instance);
609 __ CallRuntime(Runtime::kPushModuleContext, 2);
610 StoreToFrameField(
611 StandardFrameConstants::kContextOffset, context_register());
612 }
613
614 {
615 Comment cmnt(masm_, "[ Declarations");
616 VisitDeclarations(scope_->declarations());
617 }
618
619 scope_ = saved_scope;
620 if (scope_info->HasContext()) {
621 // Pop module context.
622 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
623 // Update local stack frame context field.
624 StoreToFrameField(
625 StandardFrameConstants::kContextOffset, context_register());
626 }
627
628 // Populate module instance object.
629 const PropertyAttributes attr =
630 static_cast<PropertyAttributes>(READ_ONLY | DONT_DELETE | DONT_ENUM);
631 for (Interface::Iterator it = module->interface()->iterator();
632 !it.done(); it.Advance()) {
633 if (it.interface()->IsModule()) {
634 Handle<Object> value = it.interface()->Instance();
635 ASSERT(!value.is_null());
636 JSReceiver::SetProperty(instance, it.name(), value, attr, kStrictMode);
637 } else {
638 // TODO(rossberg): set proper getters instead of undefined...
639 // instance->DefineAccessor(*it.name(), ACCESSOR_GETTER, *getter, attr);
640 Handle<Object> value(isolate()->heap()->undefined_value());
641 JSReceiver::SetProperty(instance, it.name(), value, attr, kStrictMode);
642 }
643 }
644 USE(instance->PreventExtensions());
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000645}
646
647
648void FullCodeGenerator::VisitModuleVariable(ModuleVariable* module) {
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000649 // Noting to do.
650 // The instance object is resolved statically through the module's interface.
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000651}
652
653
654void FullCodeGenerator::VisitModulePath(ModulePath* module) {
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000655 // Noting to do.
656 // The instance object is resolved statically through the module's interface.
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000657}
658
659
660void FullCodeGenerator::VisitModuleUrl(ModuleUrl* decl) {
661 // TODO(rossberg)
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000662}
663
664
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000665int FullCodeGenerator::DeclareGlobalsFlags() {
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000666 ASSERT(DeclareGlobalsLanguageMode::is_valid(language_mode()));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000667 return DeclareGlobalsEvalFlag::encode(is_eval()) |
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000668 DeclareGlobalsNativeFlag::encode(is_native()) |
669 DeclareGlobalsLanguageMode::encode(language_mode());
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000670}
671
672
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000673void FullCodeGenerator::SetFunctionPosition(FunctionLiteral* fun) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000674 CodeGenerator::RecordPositions(masm_, fun->start_position());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000675}
676
677
678void FullCodeGenerator::SetReturnPosition(FunctionLiteral* fun) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000679 CodeGenerator::RecordPositions(masm_, fun->end_position() - 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000680}
681
682
683void FullCodeGenerator::SetStatementPosition(Statement* stmt) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000684#ifdef ENABLE_DEBUGGER_SUPPORT
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000685 if (!isolate()->debugger()->IsDebuggerActive()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000686 CodeGenerator::RecordPositions(masm_, stmt->statement_pos());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000687 } else {
688 // Check if the statement will be breakable without adding a debug break
689 // slot.
690 BreakableStatementChecker checker;
691 checker.Check(stmt);
692 // Record the statement position right here if the statement is not
693 // breakable. For breakable statements the actual recording of the
694 // position will be postponed to the breakable code (typically an IC).
695 bool position_recorded = CodeGenerator::RecordPositions(
696 masm_, stmt->statement_pos(), !checker.is_breakable());
697 // If the position recording did record a new position generate a debug
698 // break slot to make the statement breakable.
699 if (position_recorded) {
700 Debug::GenerateSlot(masm_);
701 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000702 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000703#else
704 CodeGenerator::RecordPositions(masm_, stmt->statement_pos());
705#endif
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000706}
707
708
709void FullCodeGenerator::SetExpressionPosition(Expression* expr, int pos) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000710#ifdef ENABLE_DEBUGGER_SUPPORT
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000711 if (!isolate()->debugger()->IsDebuggerActive()) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000712 CodeGenerator::RecordPositions(masm_, pos);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000713 } else {
714 // Check if the expression will be breakable without adding a debug break
715 // slot.
716 BreakableStatementChecker checker;
717 checker.Check(expr);
718 // Record a statement position right here if the expression is not
719 // breakable. For breakable expressions the actual recording of the
720 // position will be postponed to the breakable code (typically an IC).
721 // NOTE this will record a statement position for something which might
722 // not be a statement. As stepping in the debugger will only stop at
723 // statement positions this is used for e.g. the condition expression of
724 // a do while loop.
725 bool position_recorded = CodeGenerator::RecordPositions(
726 masm_, pos, !checker.is_breakable());
727 // If the position recording did record a new position generate a debug
728 // break slot to make the statement breakable.
729 if (position_recorded) {
730 Debug::GenerateSlot(masm_);
731 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000732 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000733#else
734 CodeGenerator::RecordPositions(masm_, pos);
735#endif
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000736}
737
738
739void FullCodeGenerator::SetStatementPosition(int pos) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000740 CodeGenerator::RecordPositions(masm_, pos);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000741}
742
743
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000744void FullCodeGenerator::SetSourcePosition(int pos) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000745 if (pos != RelocInfo::kNoPosition) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000746 masm_->positions_recorder()->RecordPosition(pos);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000747 }
748}
749
750
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000751// Lookup table for code generators for special runtime calls which are
752// generated inline.
753#define INLINE_FUNCTION_GENERATOR_ADDRESS(Name, argc, ressize) \
754 &FullCodeGenerator::Emit##Name,
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000755
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000756const FullCodeGenerator::InlineFunctionGenerator
757 FullCodeGenerator::kInlineFunctionGenerators[] = {
758 INLINE_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS)
759 INLINE_RUNTIME_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS)
760 };
761#undef INLINE_FUNCTION_GENERATOR_ADDRESS
762
763
764FullCodeGenerator::InlineFunctionGenerator
765 FullCodeGenerator::FindInlineFunctionGenerator(Runtime::FunctionId id) {
whesse@chromium.org023421e2010-12-21 12:19:12 +0000766 int lookup_index =
767 static_cast<int>(id) - static_cast<int>(Runtime::kFirstInlineFunction);
768 ASSERT(lookup_index >= 0);
769 ASSERT(static_cast<size_t>(lookup_index) <
770 ARRAY_SIZE(kInlineFunctionGenerators));
771 return kInlineFunctionGenerators[lookup_index];
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000772}
773
774
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000775void FullCodeGenerator::EmitInlineRuntimeCall(CallRuntime* expr) {
776 const Runtime::Function* function = expr->function();
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000777 ASSERT(function != NULL);
778 ASSERT(function->intrinsic_type == Runtime::INLINE);
779 InlineFunctionGenerator generator =
780 FindInlineFunctionGenerator(function->function_id);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000781 ((*this).*(generator))(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000782}
783
784
785void FullCodeGenerator::VisitBinaryOperation(BinaryOperation* expr) {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000786 switch (expr->op()) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000787 case Token::COMMA:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000788 return VisitComma(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000789 case Token::OR:
790 case Token::AND:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000791 return VisitLogicalExpression(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000792 default:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000793 return VisitArithmeticExpression(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000794 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000795}
796
797
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000798void FullCodeGenerator::VisitInDuplicateContext(Expression* expr) {
799 if (context()->IsEffect()) {
800 VisitForEffect(expr);
801 } else if (context()->IsAccumulatorValue()) {
802 VisitForAccumulatorValue(expr);
803 } else if (context()->IsStackValue()) {
804 VisitForStackValue(expr);
805 } else if (context()->IsTest()) {
806 const TestContext* test = TestContext::cast(context());
807 VisitForControl(expr, test->true_label(), test->false_label(),
808 test->fall_through());
809 }
810}
811
812
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000813void FullCodeGenerator::VisitComma(BinaryOperation* expr) {
814 Comment cmnt(masm_, "[ Comma");
815 VisitForEffect(expr->left());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000816 VisitInDuplicateContext(expr->right());
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000817}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000818
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000819
820void FullCodeGenerator::VisitLogicalExpression(BinaryOperation* expr) {
821 bool is_logical_and = expr->op() == Token::AND;
822 Comment cmnt(masm_, is_logical_and ? "[ Logical AND" : "[ Logical OR");
823 Expression* left = expr->left();
824 Expression* right = expr->right();
825 int right_id = expr->RightId();
826 Label done;
827
828 if (context()->IsTest()) {
829 Label eval_right;
830 const TestContext* test = TestContext::cast(context());
831 if (is_logical_and) {
832 VisitForControl(left, &eval_right, test->false_label(), &eval_right);
833 } else {
834 VisitForControl(left, test->true_label(), &eval_right, &eval_right);
835 }
836 PrepareForBailoutForId(right_id, NO_REGISTERS);
837 __ bind(&eval_right);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000838
839 } else if (context()->IsAccumulatorValue()) {
840 VisitForAccumulatorValue(left);
841 // We want the value in the accumulator for the test, and on the stack in
842 // case we need it.
843 __ push(result_register());
844 Label discard, restore;
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000845 if (is_logical_and) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000846 DoTest(left, &discard, &restore, &restore);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000847 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000848 DoTest(left, &restore, &discard, &restore);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000849 }
850 __ bind(&restore);
851 __ pop(result_register());
852 __ jmp(&done);
853 __ bind(&discard);
854 __ Drop(1);
855 PrepareForBailoutForId(right_id, NO_REGISTERS);
856
857 } else if (context()->IsStackValue()) {
858 VisitForAccumulatorValue(left);
859 // We want the value in the accumulator for the test, and on the stack in
860 // case we need it.
861 __ push(result_register());
862 Label discard;
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000863 if (is_logical_and) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000864 DoTest(left, &discard, &done, &discard);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000865 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000866 DoTest(left, &done, &discard, &discard);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000867 }
868 __ bind(&discard);
869 __ Drop(1);
870 PrepareForBailoutForId(right_id, NO_REGISTERS);
871
872 } else {
873 ASSERT(context()->IsEffect());
874 Label eval_right;
875 if (is_logical_and) {
876 VisitForControl(left, &eval_right, &done, &eval_right);
877 } else {
878 VisitForControl(left, &done, &eval_right, &eval_right);
879 }
880 PrepareForBailoutForId(right_id, NO_REGISTERS);
881 __ bind(&eval_right);
882 }
883
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000884 VisitInDuplicateContext(right);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000885 __ bind(&done);
886}
887
888
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000889void FullCodeGenerator::VisitArithmeticExpression(BinaryOperation* expr) {
890 Token::Value op = expr->op();
891 Comment cmnt(masm_, "[ ArithmeticExpression");
892 Expression* left = expr->left();
893 Expression* right = expr->right();
894 OverwriteMode mode =
895 left->ResultOverwriteAllowed()
896 ? OVERWRITE_LEFT
897 : (right->ResultOverwriteAllowed() ? OVERWRITE_RIGHT : NO_OVERWRITE);
898
899 VisitForStackValue(left);
900 VisitForAccumulatorValue(right);
901
902 SetSourcePosition(expr->position());
903 if (ShouldInlineSmiCase(op)) {
904 EmitInlineSmiBinaryOp(expr, op, mode, left, right);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000905 } else {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000906 EmitBinaryOp(expr, op, mode);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000907 }
908}
909
910
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000911void FullCodeGenerator::VisitBlock(Block* stmt) {
912 Comment cmnt(masm_, "[ Block");
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000913 NestedBlock nested_block(this, stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000914 SetStatementPosition(stmt);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000915
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000916 Scope* saved_scope = scope();
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000917 // Push a block context when entering a block with block scoped variables.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000918 if (stmt->scope() != NULL) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000919 { Comment cmnt(masm_, "[ Extend block context");
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000920 scope_ = stmt->scope();
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000921 Handle<ScopeInfo> scope_info = scope_->GetScopeInfo();
922 int heap_slots = scope_info->ContextLength() - Context::MIN_CONTEXT_SLOTS;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000923 __ Push(scope_info);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000924 PushFunctionArgumentForContextAllocation();
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000925 if (heap_slots <= FastNewBlockContextStub::kMaximumSlots) {
926 FastNewBlockContextStub stub(heap_slots);
927 __ CallStub(&stub);
928 } else {
929 __ CallRuntime(Runtime::kPushBlockContext, 2);
930 }
931
932 // Replace the context stored in the frame.
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000933 StoreToFrameField(StandardFrameConstants::kContextOffset,
934 context_register());
935 }
936 { Comment cmnt(masm_, "[ Declarations");
937 VisitDeclarations(scope_->declarations());
938 }
939 }
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +0000940 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000941 VisitStatements(stmt->statements());
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000942 scope_ = saved_scope;
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000943 __ bind(nested_block.break_label());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000944 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000945
946 // Pop block context if necessary.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000947 if (stmt->scope() != NULL) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000948 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
949 // Update local stack frame context field.
950 StoreToFrameField(StandardFrameConstants::kContextOffset,
951 context_register());
952 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000953}
954
955
956void FullCodeGenerator::VisitExpressionStatement(ExpressionStatement* stmt) {
957 Comment cmnt(masm_, "[ ExpressionStatement");
958 SetStatementPosition(stmt);
959 VisitForEffect(stmt->expression());
960}
961
962
963void FullCodeGenerator::VisitEmptyStatement(EmptyStatement* stmt) {
964 Comment cmnt(masm_, "[ EmptyStatement");
965 SetStatementPosition(stmt);
966}
967
968
969void FullCodeGenerator::VisitIfStatement(IfStatement* stmt) {
970 Comment cmnt(masm_, "[ IfStatement");
971 SetStatementPosition(stmt);
972 Label then_part, else_part, done;
973
ricow@chromium.org65fae842010-08-25 15:26:24 +0000974 if (stmt->HasElseStatement()) {
975 VisitForControl(stmt->condition(), &then_part, &else_part, &then_part);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000976 PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000977 __ bind(&then_part);
978 Visit(stmt->then_statement());
979 __ jmp(&done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000980
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000981 PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000982 __ bind(&else_part);
983 Visit(stmt->else_statement());
984 } else {
985 VisitForControl(stmt->condition(), &then_part, &done, &then_part);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000986 PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000987 __ bind(&then_part);
988 Visit(stmt->then_statement());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000989
990 PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000991 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000992 __ bind(&done);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000993 PrepareForBailoutForId(stmt->IfId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000994}
995
996
997void FullCodeGenerator::VisitContinueStatement(ContinueStatement* stmt) {
998 Comment cmnt(masm_, "[ ContinueStatement");
999 SetStatementPosition(stmt);
1000 NestedStatement* current = nesting_stack_;
1001 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001002 int context_length = 0;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001003 // When continuing, we clobber the unpredictable value in the accumulator
1004 // with one that's safe for GC. If we hit an exit from the try block of
1005 // try...finally on our way out, we will unconditionally preserve the
1006 // accumulator on the stack.
1007 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001008 while (!current->IsContinueTarget(stmt->target())) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001009 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001010 }
1011 __ Drop(stack_depth);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001012 if (context_length > 0) {
1013 while (context_length > 0) {
1014 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1015 --context_length;
1016 }
1017 StoreToFrameField(StandardFrameConstants::kContextOffset,
1018 context_register());
1019 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001020
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001021 __ jmp(current->AsIteration()->continue_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001022}
1023
1024
1025void FullCodeGenerator::VisitBreakStatement(BreakStatement* stmt) {
1026 Comment cmnt(masm_, "[ BreakStatement");
1027 SetStatementPosition(stmt);
1028 NestedStatement* current = nesting_stack_;
1029 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001030 int context_length = 0;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001031 // When breaking, we clobber the unpredictable value in the accumulator
1032 // with one that's safe for GC. If we hit an exit from the try block of
1033 // try...finally on our way out, we will unconditionally preserve the
1034 // accumulator on the stack.
1035 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001036 while (!current->IsBreakTarget(stmt->target())) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001037 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001038 }
1039 __ Drop(stack_depth);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001040 if (context_length > 0) {
1041 while (context_length > 0) {
1042 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1043 --context_length;
1044 }
1045 StoreToFrameField(StandardFrameConstants::kContextOffset,
1046 context_register());
1047 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001048
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001049 __ jmp(current->AsBreakable()->break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001050}
1051
1052
1053void FullCodeGenerator::VisitReturnStatement(ReturnStatement* stmt) {
1054 Comment cmnt(masm_, "[ ReturnStatement");
1055 SetStatementPosition(stmt);
1056 Expression* expr = stmt->expression();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001057 VisitForAccumulatorValue(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001058
1059 // Exit all nested statements.
1060 NestedStatement* current = nesting_stack_;
1061 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001062 int context_length = 0;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001063 while (current != NULL) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001064 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001065 }
1066 __ Drop(stack_depth);
1067
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001068 EmitReturnSequence();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001069}
1070
1071
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001072void FullCodeGenerator::VisitWithStatement(WithStatement* stmt) {
1073 Comment cmnt(masm_, "[ WithStatement");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001074 SetStatementPosition(stmt);
1075
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001076 VisitForStackValue(stmt->expression());
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00001077 PushFunctionArgumentForContextAllocation();
1078 __ CallRuntime(Runtime::kPushWithContext, 2);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001079 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001080
1081 { WithOrCatch body(this);
1082 Visit(stmt->statement());
1083 }
1084
1085 // Pop context.
1086 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1087 // Update local stack frame context field.
1088 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001089}
1090
1091
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001092void FullCodeGenerator::VisitDoWhileStatement(DoWhileStatement* stmt) {
1093 Comment cmnt(masm_, "[ DoWhileStatement");
1094 SetStatementPosition(stmt);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001095 Label body, stack_check;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001096
1097 Iteration loop_statement(this, stmt);
1098 increment_loop_depth();
1099
1100 __ bind(&body);
1101 Visit(stmt->body());
1102
ricow@chromium.org65fae842010-08-25 15:26:24 +00001103 // Record the position of the do while condition and make sure it is
1104 // possible to break on the condition.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001105 __ bind(loop_statement.continue_label());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001106 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001107 SetExpressionPosition(stmt->cond(), stmt->condition_position());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001108 VisitForControl(stmt->cond(),
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001109 &stack_check,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001110 loop_statement.break_label(),
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001111 &stack_check);
1112
1113 // Check stack before looping.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001114 PrepareForBailoutForId(stmt->BackEdgeId(), NO_REGISTERS);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001115 __ bind(&stack_check);
yangguo@chromium.org56454712012-02-16 15:33:53 +00001116 EmitStackCheck(stmt, &body);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001117 __ jmp(&body);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001118
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001119 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001120 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001121 decrement_loop_depth();
1122}
1123
1124
1125void FullCodeGenerator::VisitWhileStatement(WhileStatement* stmt) {
1126 Comment cmnt(masm_, "[ WhileStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001127 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001128
1129 Iteration loop_statement(this, stmt);
1130 increment_loop_depth();
1131
1132 // Emit the test at the bottom of the loop.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001133 __ jmp(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001134
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001135 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001136 __ bind(&body);
1137 Visit(stmt->body());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001138
1139 // Emit the statement position here as this is where the while
1140 // statement code starts.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001141 __ bind(loop_statement.continue_label());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001142 SetStatementPosition(stmt);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001143
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001144 // Check stack before looping.
yangguo@chromium.org56454712012-02-16 15:33:53 +00001145 EmitStackCheck(stmt, &body);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001146
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001147 __ bind(&test);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001148 VisitForControl(stmt->cond(),
1149 &body,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001150 loop_statement.break_label(),
1151 loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001152
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001153 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001154 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001155 decrement_loop_depth();
1156}
1157
1158
1159void FullCodeGenerator::VisitForStatement(ForStatement* stmt) {
1160 Comment cmnt(masm_, "[ ForStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001161 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001162
1163 Iteration loop_statement(this, stmt);
erik.corry@gmail.combbceb572012-03-09 10:52:05 +00001164
1165 // Set statement position for a break slot before entering the for-body.
1166 SetStatementPosition(stmt);
1167
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001168 if (stmt->init() != NULL) {
1169 Visit(stmt->init());
1170 }
1171
1172 increment_loop_depth();
1173 // Emit the test at the bottom of the loop (even if empty).
1174 __ jmp(&test);
1175
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001176 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001177 __ bind(&body);
1178 Visit(stmt->body());
1179
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001180 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001181 __ bind(loop_statement.continue_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001182 if (stmt->next() != NULL) {
1183 Visit(stmt->next());
1184 }
1185
ricow@chromium.org65fae842010-08-25 15:26:24 +00001186 // Emit the statement position here as this is where the for
1187 // statement code starts.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001188 SetStatementPosition(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001189
1190 // Check stack before looping.
yangguo@chromium.org56454712012-02-16 15:33:53 +00001191 EmitStackCheck(stmt, &body);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001192
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001193 __ bind(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001194 if (stmt->cond() != NULL) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00001195 VisitForControl(stmt->cond(),
1196 &body,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001197 loop_statement.break_label(),
1198 loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001199 } else {
1200 __ jmp(&body);
1201 }
1202
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001203 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001204 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001205 decrement_loop_depth();
1206}
1207
1208
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001209void FullCodeGenerator::VisitTryCatchStatement(TryCatchStatement* stmt) {
1210 Comment cmnt(masm_, "[ TryCatchStatement");
1211 SetStatementPosition(stmt);
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001212 // The try block adds a handler to the exception handler chain before
1213 // entering, and removes it again when exiting normally. If an exception
1214 // is thrown during execution of the try block, the handler is consumed
1215 // and control is passed to the catch block with the exception in the
1216 // result register.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001217
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001218 Label try_entry, handler_entry, exit;
1219 __ jmp(&try_entry);
1220 __ bind(&handler_entry);
1221 handler_table()->set(stmt->index(), Smi::FromInt(handler_entry.pos()));
1222 // Exception handler code, the exception is in the result register.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001223 // Extend the context before executing the catch block.
1224 { Comment cmnt(masm_, "[ Extend catch context");
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001225 __ Push(stmt->variable()->name());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001226 __ push(result_register());
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00001227 PushFunctionArgumentForContextAllocation();
1228 __ CallRuntime(Runtime::kPushCatchContext, 3);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001229 StoreToFrameField(StandardFrameConstants::kContextOffset,
1230 context_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001231 }
1232
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001233 Scope* saved_scope = scope();
1234 scope_ = stmt->scope();
1235 ASSERT(scope_->declarations()->is_empty());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001236 { WithOrCatch catch_body(this);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001237 Visit(stmt->catch_block());
1238 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001239 // Restore the context.
1240 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1241 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001242 scope_ = saved_scope;
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001243 __ jmp(&exit);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001244
1245 // Try block code. Sets up the exception handler chain.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001246 __ bind(&try_entry);
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00001247 __ PushTryHandler(StackHandler::CATCH, stmt->index());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001248 { TryCatch try_body(this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001249 Visit(stmt->try_block());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001250 }
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001251 __ PopTryHandler();
1252 __ bind(&exit);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001253}
1254
1255
1256void FullCodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* stmt) {
1257 Comment cmnt(masm_, "[ TryFinallyStatement");
1258 SetStatementPosition(stmt);
1259 // Try finally is compiled by setting up a try-handler on the stack while
1260 // executing the try body, and removing it again afterwards.
1261 //
1262 // The try-finally construct can enter the finally block in three ways:
1263 // 1. By exiting the try-block normally. This removes the try-handler and
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001264 // calls the finally block code before continuing.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001265 // 2. By exiting the try-block with a function-local control flow transfer
1266 // (break/continue/return). The site of the, e.g., break removes the
1267 // try handler and calls the finally block code before continuing
1268 // its outward control transfer.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001269 // 3. By exiting the try-block with a thrown exception.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001270 // This can happen in nested function calls. It traverses the try-handler
1271 // chain and consumes the try-handler entry before jumping to the
1272 // handler code. The handler code then calls the finally-block before
1273 // rethrowing the exception.
1274 //
1275 // The finally block must assume a return address on top of the stack
1276 // (or in the link register on ARM chips) and a value (return value or
1277 // exception) in the result register (rax/eax/r0), both of which must
1278 // be preserved. The return address isn't GC-safe, so it should be
1279 // cooked before GC.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001280 Label try_entry, handler_entry, finally_entry;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001281
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001282 // Jump to try-handler setup and try-block code.
1283 __ jmp(&try_entry);
1284 __ bind(&handler_entry);
1285 handler_table()->set(stmt->index(), Smi::FromInt(handler_entry.pos()));
1286 // Exception handler code. This code is only executed when an exception
1287 // is thrown. The exception is in the result register, and must be
1288 // preserved by the finally block. Call the finally block and then
1289 // rethrow the exception if it returns.
1290 __ Call(&finally_entry);
1291 __ push(result_register());
1292 __ CallRuntime(Runtime::kReThrow, 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001293
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001294 // Finally block implementation.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001295 __ bind(&finally_entry);
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001296 EnterFinallyBlock();
1297 { Finally finally_body(this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001298 Visit(stmt->finally_block());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001299 }
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001300 ExitFinallyBlock(); // Return to the calling code.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001301
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001302 // Set up try handler.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001303 __ bind(&try_entry);
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00001304 __ PushTryHandler(StackHandler::FINALLY, stmt->index());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001305 { TryFinally try_body(this, &finally_entry);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001306 Visit(stmt->try_block());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001307 }
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001308 __ PopTryHandler();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001309 // Execute the finally block on the way out. Clobber the unpredictable
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001310 // value in the result register with one that's safe for GC because the
1311 // finally block will unconditionally preserve the result register on the
1312 // stack.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001313 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001314 __ Call(&finally_entry);
1315}
1316
1317
1318void FullCodeGenerator::VisitDebuggerStatement(DebuggerStatement* stmt) {
1319#ifdef ENABLE_DEBUGGER_SUPPORT
1320 Comment cmnt(masm_, "[ DebuggerStatement");
1321 SetStatementPosition(stmt);
1322
ager@chromium.org5c838252010-02-19 08:53:10 +00001323 __ DebugBreak();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001324 // Ignore the return value.
1325#endif
1326}
1327
1328
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001329void FullCodeGenerator::VisitConditional(Conditional* expr) {
1330 Comment cmnt(masm_, "[ Conditional");
1331 Label true_case, false_case, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001332 VisitForControl(expr->condition(), &true_case, &false_case, &true_case);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001333
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001334 PrepareForBailoutForId(expr->ThenId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001335 __ bind(&true_case);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001336 SetExpressionPosition(expr->then_expression(),
1337 expr->then_expression_position());
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001338 if (context()->IsTest()) {
1339 const TestContext* for_test = TestContext::cast(context());
1340 VisitForControl(expr->then_expression(),
1341 for_test->true_label(),
1342 for_test->false_label(),
1343 NULL);
1344 } else {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001345 VisitInDuplicateContext(expr->then_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001346 __ jmp(&done);
1347 }
1348
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001349 PrepareForBailoutForId(expr->ElseId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001350 __ bind(&false_case);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001351 SetExpressionPosition(expr->else_expression(),
1352 expr->else_expression_position());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001353 VisitInDuplicateContext(expr->else_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001354 // If control flow falls through Visit, merge it with true case here.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001355 if (!context()->IsTest()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001356 __ bind(&done);
1357 }
1358}
1359
1360
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001361void FullCodeGenerator::VisitLiteral(Literal* expr) {
1362 Comment cmnt(masm_, "[ Literal");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001363 context()->Plug(expr->handle());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001364}
1365
1366
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001367void FullCodeGenerator::VisitFunctionLiteral(FunctionLiteral* expr) {
1368 Comment cmnt(masm_, "[ FunctionLiteral");
1369
1370 // Build the function boilerplate and instantiate it.
1371 Handle<SharedFunctionInfo> function_info =
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001372 Compiler::BuildFunctionInfo(expr, script());
1373 if (function_info.is_null()) {
1374 SetStackOverflow();
1375 return;
1376 }
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001377 EmitNewClosure(function_info, expr->pretenure());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001378}
1379
1380
1381void FullCodeGenerator::VisitSharedFunctionInfoLiteral(
1382 SharedFunctionInfoLiteral* expr) {
1383 Comment cmnt(masm_, "[ SharedFunctionInfoLiteral");
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001384 EmitNewClosure(expr->shared_function_info(), false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001385}
1386
1387
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001388void FullCodeGenerator::VisitThrow(Throw* expr) {
1389 Comment cmnt(masm_, "[ Throw");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001390 VisitForStackValue(expr->exception());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001391 __ CallRuntime(Runtime::kThrow, 1);
1392 // Never returns here.
1393}
1394
1395
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001396FullCodeGenerator::NestedStatement* FullCodeGenerator::TryCatch::Exit(
1397 int* stack_depth,
1398 int* context_length) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001399 // The macros used here must preserve the result register.
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001400 __ Drop(*stack_depth);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001401 __ PopTryHandler();
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001402 *stack_depth = 0;
1403 return previous_;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001404}
1405
ricow@chromium.org65fae842010-08-25 15:26:24 +00001406
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001407bool FullCodeGenerator::TryLiteralCompare(CompareOperation* expr) {
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001408 Expression* sub_expr;
ager@chromium.org04921a82011-06-27 13:21:41 +00001409 Handle<String> check;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001410 if (expr->IsLiteralCompareTypeof(&sub_expr, &check)) {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001411 EmitLiteralCompareTypeof(expr, sub_expr, check);
ager@chromium.org04921a82011-06-27 13:21:41 +00001412 return true;
1413 }
1414
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001415 if (expr->IsLiteralCompareUndefined(&sub_expr)) {
1416 EmitLiteralCompareNil(expr, sub_expr, kUndefinedValue);
1417 return true;
1418 }
1419
1420 if (expr->IsLiteralCompareNull(&sub_expr)) {
1421 EmitLiteralCompareNil(expr, sub_expr, kNullValue);
ager@chromium.org04921a82011-06-27 13:21:41 +00001422 return true;
1423 }
1424
1425 return false;
1426}
1427
1428
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001429#undef __
1430
1431
1432} } // namespace v8::internal