blob: 5c5ba6b2566e8d4598b8c3ac253829e00b42d105 [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
58void BreakableStatementChecker::VisitModuleDeclaration(
59 ModuleDeclaration* decl) {
60}
61
62
63void BreakableStatementChecker::VisitModuleLiteral(ModuleLiteral* module) {
64}
65
66void BreakableStatementChecker::VisitModuleVariable(ModuleVariable* module) {
67}
68
69void BreakableStatementChecker::VisitModulePath(ModulePath* module) {
70}
71
72void BreakableStatementChecker::VisitModuleUrl(ModuleUrl* module) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +000073}
74
75
76void BreakableStatementChecker::VisitBlock(Block* stmt) {
77}
78
79
80void BreakableStatementChecker::VisitExpressionStatement(
81 ExpressionStatement* stmt) {
82 // Check if expression is breakable.
83 Visit(stmt->expression());
84}
85
86
87void BreakableStatementChecker::VisitEmptyStatement(EmptyStatement* stmt) {
88}
89
90
91void BreakableStatementChecker::VisitIfStatement(IfStatement* stmt) {
92 // If the condition is breakable the if statement is breakable.
93 Visit(stmt->condition());
94}
95
96
97void BreakableStatementChecker::VisitContinueStatement(
98 ContinueStatement* stmt) {
99}
100
101
102void BreakableStatementChecker::VisitBreakStatement(BreakStatement* stmt) {
103}
104
105
106void BreakableStatementChecker::VisitReturnStatement(ReturnStatement* stmt) {
107 // Return is breakable if the expression is.
108 Visit(stmt->expression());
109}
110
111
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000112void BreakableStatementChecker::VisitWithStatement(WithStatement* stmt) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000113 Visit(stmt->expression());
114}
115
116
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000117void BreakableStatementChecker::VisitSwitchStatement(SwitchStatement* stmt) {
118 // Switch statements breakable if the tag expression is.
119 Visit(stmt->tag());
120}
121
122
123void BreakableStatementChecker::VisitDoWhileStatement(DoWhileStatement* stmt) {
124 // Mark do while as breakable to avoid adding a break slot in front of it.
125 is_breakable_ = true;
126}
127
128
129void BreakableStatementChecker::VisitWhileStatement(WhileStatement* stmt) {
130 // Mark while statements breakable if the condition expression is.
131 Visit(stmt->cond());
132}
133
134
135void BreakableStatementChecker::VisitForStatement(ForStatement* stmt) {
136 // Mark for statements breakable if the condition expression is.
137 if (stmt->cond() != NULL) {
138 Visit(stmt->cond());
139 }
140}
141
142
143void BreakableStatementChecker::VisitForInStatement(ForInStatement* stmt) {
144 // Mark for in statements breakable if the enumerable expression is.
145 Visit(stmt->enumerable());
146}
147
148
149void BreakableStatementChecker::VisitTryCatchStatement(
150 TryCatchStatement* stmt) {
151 // Mark try catch as breakable to avoid adding a break slot in front of it.
152 is_breakable_ = true;
153}
154
155
156void BreakableStatementChecker::VisitTryFinallyStatement(
157 TryFinallyStatement* stmt) {
158 // Mark try finally as breakable to avoid adding a break slot in front of it.
159 is_breakable_ = true;
160}
161
162
163void BreakableStatementChecker::VisitDebuggerStatement(
164 DebuggerStatement* stmt) {
165 // The debugger statement is breakable.
166 is_breakable_ = true;
167}
168
169
170void BreakableStatementChecker::VisitFunctionLiteral(FunctionLiteral* expr) {
171}
172
173
174void BreakableStatementChecker::VisitSharedFunctionInfoLiteral(
175 SharedFunctionInfoLiteral* expr) {
176}
177
178
179void BreakableStatementChecker::VisitConditional(Conditional* expr) {
180}
181
182
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000183void BreakableStatementChecker::VisitVariableProxy(VariableProxy* expr) {
184}
185
186
187void BreakableStatementChecker::VisitLiteral(Literal* expr) {
188}
189
190
191void BreakableStatementChecker::VisitRegExpLiteral(RegExpLiteral* expr) {
192}
193
194
195void BreakableStatementChecker::VisitObjectLiteral(ObjectLiteral* expr) {
196}
197
198
199void BreakableStatementChecker::VisitArrayLiteral(ArrayLiteral* expr) {
200}
201
202
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000203void BreakableStatementChecker::VisitAssignment(Assignment* expr) {
204 // If assigning to a property (including a global property) the assignment is
205 // breakable.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000206 VariableProxy* proxy = expr->target()->AsVariableProxy();
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000207 Property* prop = expr->target()->AsProperty();
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000208 if (prop != NULL || (proxy != NULL && proxy->var()->IsUnallocated())) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000209 is_breakable_ = true;
210 return;
211 }
212
213 // Otherwise the assignment is breakable if the assigned value is.
214 Visit(expr->value());
215}
216
217
218void BreakableStatementChecker::VisitThrow(Throw* expr) {
219 // Throw is breakable if the expression is.
220 Visit(expr->exception());
221}
222
223
224void BreakableStatementChecker::VisitProperty(Property* expr) {
225 // Property load is breakable.
226 is_breakable_ = true;
227}
228
229
230void BreakableStatementChecker::VisitCall(Call* expr) {
231 // Function calls both through IC and call stub are breakable.
232 is_breakable_ = true;
233}
234
235
236void BreakableStatementChecker::VisitCallNew(CallNew* expr) {
237 // Function calls through new are breakable.
238 is_breakable_ = true;
239}
240
241
242void BreakableStatementChecker::VisitCallRuntime(CallRuntime* expr) {
243}
244
245
246void BreakableStatementChecker::VisitUnaryOperation(UnaryOperation* expr) {
247 Visit(expr->expression());
248}
249
250
251void BreakableStatementChecker::VisitCountOperation(CountOperation* expr) {
252 Visit(expr->expression());
253}
254
255
256void BreakableStatementChecker::VisitBinaryOperation(BinaryOperation* expr) {
257 Visit(expr->left());
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +0000258 if (expr->op() != Token::AND &&
259 expr->op() != Token::OR) {
260 Visit(expr->right());
261 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000262}
263
264
265void BreakableStatementChecker::VisitCompareOperation(CompareOperation* expr) {
266 Visit(expr->left());
267 Visit(expr->right());
268}
269
270
271void BreakableStatementChecker::VisitThisFunction(ThisFunction* expr) {
272}
273
274
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000275#define __ ACCESS_MASM(masm())
276
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000277bool FullCodeGenerator::MakeCode(CompilationInfo* info) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000278 Isolate* isolate = info->isolate();
ager@chromium.org5c838252010-02-19 08:53:10 +0000279 Handle<Script> script = info->script();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000280 if (!script->IsUndefined() && !script->source()->IsUndefined()) {
281 int len = String::cast(script->source())->length();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000282 isolate->counters()->total_full_codegen_source_size()->Increment(len);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000283 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000284 if (FLAG_trace_codegen) {
285 PrintF("Full Compiler - ");
286 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000287 CodeGenerator::MakeCodePrologue(info);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000288 const int kInitialBufferSize = 4 * KB;
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000289 MacroAssembler masm(info->isolate(), NULL, kInitialBufferSize);
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000290#ifdef ENABLE_GDB_JIT_INTERFACE
291 masm.positions_recorder()->StartGDBJITLineInfoRecording();
292#endif
ager@chromium.org5c838252010-02-19 08:53:10 +0000293
294 FullCodeGenerator cgen(&masm);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000295 cgen.Generate(info);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000296 if (cgen.HasStackOverflow()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000297 ASSERT(!isolate->has_pending_exception());
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000298 return false;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000299 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000300 unsigned table_offset = cgen.EmitStackCheckTable();
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000301
lrn@chromium.org34e60782011-09-15 07:25:40 +0000302 Code::Flags flags = Code::ComputeFlags(Code::FUNCTION);
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000303 Handle<Code> code = CodeGenerator::MakeCodeEpilogue(&masm, flags, info);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000304 code->set_optimizable(info->IsOptimizable());
305 cgen.PopulateDeoptimizationData(code);
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000306 cgen.PopulateTypeFeedbackCells(code);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000307 code->set_has_deoptimization_support(info->HasDeoptimizationSupport());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +0000308 code->set_handler_table(*cgen.handler_table());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000309#ifdef ENABLE_DEBUGGER_SUPPORT
lrn@chromium.org34e60782011-09-15 07:25:40 +0000310 code->set_has_debug_break_slots(
311 info->isolate()->debugger()->IsDebuggerActive());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000312 code->set_compiled_optimizable(info->IsOptimizable());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000313#endif // ENABLE_DEBUGGER_SUPPORT
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000314 code->set_allow_osr_at_loop_nesting_level(0);
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000315 code->set_stack_check_table_offset(table_offset);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000316 CodeGenerator::PrintCode(code, info);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000317 info->SetCode(code); // May be an empty handle.
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000318 if (!code.is_null()) {
319 isolate->runtime_profiler()->NotifyCodeGenerated(code->instruction_size());
320 }
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000321#ifdef ENABLE_GDB_JIT_INTERFACE
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000322 if (FLAG_gdbjit && !code.is_null()) {
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000323 GDBJITLineInfo* lineinfo =
324 masm.positions_recorder()->DetachGDBJITLineInfo();
325
326 GDBJIT(RegisterDetailedLineInfo(*code, lineinfo));
327 }
328#endif
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000329 return !code.is_null();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000330}
331
332
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000333unsigned FullCodeGenerator::EmitStackCheckTable() {
334 // The stack check table consists of a length (in number of entries)
335 // field, and then a sequence of entries. Each entry is a pair of AST id
336 // and code-relative pc offset.
337 masm()->Align(kIntSize);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000338 unsigned offset = masm()->pc_offset();
339 unsigned length = stack_checks_.length();
340 __ dd(length);
341 for (unsigned i = 0; i < length; ++i) {
342 __ dd(stack_checks_[i].id);
343 __ dd(stack_checks_[i].pc_and_state);
344 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000345 return offset;
346}
347
348
349void FullCodeGenerator::PopulateDeoptimizationData(Handle<Code> code) {
350 // Fill in the deoptimization information.
351 ASSERT(info_->HasDeoptimizationSupport() || bailout_entries_.is_empty());
352 if (!info_->HasDeoptimizationSupport()) return;
353 int length = bailout_entries_.length();
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000354 Handle<DeoptimizationOutputData> data = isolate()->factory()->
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000355 NewDeoptimizationOutputData(length, TENURED);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000356 for (int i = 0; i < length; i++) {
357 data->SetAstId(i, Smi::FromInt(bailout_entries_[i].id));
358 data->SetPcAndState(i, Smi::FromInt(bailout_entries_[i].pc_and_state));
359 }
360 code->set_deoptimization_data(*data);
361}
362
363
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000364void FullCodeGenerator::PopulateTypeFeedbackCells(Handle<Code> code) {
365 if (type_feedback_cells_.is_empty()) return;
366 int length = type_feedback_cells_.length();
367 int array_size = TypeFeedbackCells::LengthOfFixedArray(length);
368 Handle<TypeFeedbackCells> cache = Handle<TypeFeedbackCells>::cast(
369 isolate()->factory()->NewFixedArray(array_size, TENURED));
370 for (int i = 0; i < length; i++) {
371 cache->SetAstId(i, Smi::FromInt(type_feedback_cells_[i].ast_id));
372 cache->SetCell(i, *type_feedback_cells_[i].cell);
373 }
374 code->set_type_feedback_cells(*cache);
375}
376
377
378
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000379void FullCodeGenerator::PrepareForBailout(Expression* node, State state) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000380 PrepareForBailoutForId(node->id(), state);
381}
382
383
384void FullCodeGenerator::RecordJSReturnSite(Call* call) {
385 // We record the offset of the function return so we can rebuild the frame
386 // if the function was inlined, i.e., this is the return address in the
387 // inlined function's frame.
388 //
389 // The state is ignored. We defensively set it to TOS_REG, which is the
390 // real state of the unoptimized code at the return site.
391 PrepareForBailoutForId(call->ReturnId(), TOS_REG);
392#ifdef DEBUG
393 // In debug builds, mark the return so we can verify that this function
394 // was called.
395 ASSERT(!call->return_is_recorded_);
396 call->return_is_recorded_ = true;
397#endif
398}
399
400
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +0000401void FullCodeGenerator::PrepareForBailoutForId(unsigned id, State state) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000402 // There's no need to prepare this code for bailouts from already optimized
403 // code or code that can't be optimized.
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000404 if (!info_->HasDeoptimizationSupport()) return;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000405 unsigned pc_and_state =
406 StateField::encode(state) | PcField::encode(masm_->pc_offset());
407 BailoutEntry entry = { id, pc_and_state };
408#ifdef DEBUG
yangguo@chromium.org659ceec2012-01-26 07:37:54 +0000409 if (FLAG_enable_slow_asserts) {
410 // Assert that we don't have multiple bailout entries for the same node.
411 for (int i = 0; i < bailout_entries_.length(); i++) {
412 if (bailout_entries_.at(i).id == entry.id) {
413 AstPrinter printer;
414 PrintF("%s", printer.PrintProgram(info_->function()));
415 UNREACHABLE();
416 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000417 }
418 }
419#endif // DEBUG
420 bailout_entries_.Add(entry);
421}
422
423
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000424void FullCodeGenerator::RecordTypeFeedbackCell(
425 unsigned id, Handle<JSGlobalPropertyCell> cell) {
426 TypeFeedbackCellEntry entry = { id, cell };
427 type_feedback_cells_.Add(entry);
428}
429
430
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +0000431void FullCodeGenerator::RecordStackCheck(unsigned ast_id) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000432 // The pc offset does not need to be encoded and packed together with a
433 // state.
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +0000434 ASSERT(masm_->pc_offset() > 0);
435 BailoutEntry entry = { ast_id, static_cast<unsigned>(masm_->pc_offset()) };
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000436 stack_checks_.Add(entry);
437}
438
439
ricow@chromium.org65fae842010-08-25 15:26:24 +0000440bool FullCodeGenerator::ShouldInlineSmiCase(Token::Value op) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000441 // Inline smi case inside loops, but not division and modulo which
442 // are too complicated and take up too much space.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000443 if (op == Token::DIV ||op == Token::MOD) return false;
444 if (FLAG_always_inline_smi_code) return true;
445 return loop_depth_ > 0;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000446}
447
448
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000449void FullCodeGenerator::EffectContext::Plug(Register reg) const {
450}
451
452
453void FullCodeGenerator::AccumulatorValueContext::Plug(Register reg) const {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000454 __ Move(result_register(), reg);
455}
456
457
458void FullCodeGenerator::StackValueContext::Plug(Register reg) const {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000459 __ push(reg);
460}
461
462
463void FullCodeGenerator::TestContext::Plug(Register reg) const {
464 // For simplicity we always test the accumulator register.
465 __ Move(result_register(), reg);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000466 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000467 codegen()->DoTest(this);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000468}
469
470
471void FullCodeGenerator::EffectContext::PlugTOS() const {
472 __ Drop(1);
473}
474
475
476void FullCodeGenerator::AccumulatorValueContext::PlugTOS() const {
477 __ pop(result_register());
478}
479
480
481void FullCodeGenerator::StackValueContext::PlugTOS() const {
482}
483
484
485void FullCodeGenerator::TestContext::PlugTOS() const {
486 // For simplicity we always test the accumulator register.
487 __ pop(result_register());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000488 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000489 codegen()->DoTest(this);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000490}
491
492
493void FullCodeGenerator::EffectContext::PrepareTest(
494 Label* materialize_true,
495 Label* materialize_false,
496 Label** if_true,
497 Label** if_false,
498 Label** fall_through) const {
499 // In an effect context, the true and the false case branch to the
500 // same label.
501 *if_true = *if_false = *fall_through = materialize_true;
502}
503
504
505void FullCodeGenerator::AccumulatorValueContext::PrepareTest(
506 Label* materialize_true,
507 Label* materialize_false,
508 Label** if_true,
509 Label** if_false,
510 Label** fall_through) const {
511 *if_true = *fall_through = materialize_true;
512 *if_false = materialize_false;
513}
514
515
516void FullCodeGenerator::StackValueContext::PrepareTest(
517 Label* materialize_true,
518 Label* materialize_false,
519 Label** if_true,
520 Label** if_false,
521 Label** fall_through) const {
522 *if_true = *fall_through = materialize_true;
523 *if_false = materialize_false;
524}
525
526
527void FullCodeGenerator::TestContext::PrepareTest(
528 Label* materialize_true,
529 Label* materialize_false,
530 Label** if_true,
531 Label** if_false,
532 Label** fall_through) const {
533 *if_true = true_label_;
534 *if_false = false_label_;
535 *fall_through = fall_through_;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000536}
537
538
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000539void FullCodeGenerator::DoTest(const TestContext* context) {
540 DoTest(context->condition(),
541 context->true_label(),
542 context->false_label(),
543 context->fall_through());
544}
545
546
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000547void FullCodeGenerator::VisitDeclarations(
548 ZoneList<Declaration*>* declarations) {
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000549 int save_global_count = global_count_;
550 global_count_ = 0;
551
552 AstVisitor::VisitDeclarations(declarations);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000553
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000554 // Batch declare global functions and variables.
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000555 if (global_count_ > 0) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000556 Handle<FixedArray> array =
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000557 isolate()->factory()->NewFixedArray(2 * global_count_, TENURED);
558 int length = declarations->length();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000559 for (int j = 0, i = 0; i < length; i++) {
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000560 VariableDeclaration* decl = declarations->at(i)->AsVariableDeclaration();
561 if (decl != NULL) {
562 Variable* var = decl->proxy()->var();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000563
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000564 if (var->IsUnallocated()) {
565 array->set(j++, *(var->name()));
566 if (decl->fun() == NULL) {
567 if (var->binding_needs_init()) {
568 // In case this binding needs initialization use the hole.
569 array->set_the_hole(j++);
570 } else {
571 array->set_undefined(j++);
572 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000573 } else {
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000574 Handle<SharedFunctionInfo> function =
575 Compiler::BuildFunctionInfo(decl->fun(), script());
576 // Check for stack-overflow exception.
577 if (function.is_null()) {
578 SetStackOverflow();
579 return;
580 }
581 array->set(j++, *function);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000582 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000583 }
584 }
585 }
586 // Invoke the platform-dependent code generator to do the actual
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000587 // declaration the global functions and variables.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000588 DeclareGlobals(array);
589 }
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000590
591 global_count_ = save_global_count;
592}
593
594
595void FullCodeGenerator::VisitVariableDeclaration(VariableDeclaration* decl) {
596 EmitDeclaration(decl->proxy(), decl->mode(), decl->fun());
597}
598
599
600void FullCodeGenerator::VisitModuleDeclaration(ModuleDeclaration* decl) {
601 // TODO(rossberg)
602}
603
604
605void FullCodeGenerator::VisitModuleLiteral(ModuleLiteral* module) {
606 // TODO(rossberg)
607}
608
609
610void FullCodeGenerator::VisitModuleVariable(ModuleVariable* module) {
611 // TODO(rossberg)
612}
613
614
615void FullCodeGenerator::VisitModulePath(ModulePath* module) {
616 // TODO(rossberg)
617}
618
619
620void FullCodeGenerator::VisitModuleUrl(ModuleUrl* decl) {
621 // TODO(rossberg)
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000622}
623
624
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000625int FullCodeGenerator::DeclareGlobalsFlags() {
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000626 ASSERT(DeclareGlobalsLanguageMode::is_valid(language_mode()));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000627 return DeclareGlobalsEvalFlag::encode(is_eval()) |
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000628 DeclareGlobalsNativeFlag::encode(is_native()) |
629 DeclareGlobalsLanguageMode::encode(language_mode());
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000630}
631
632
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000633void FullCodeGenerator::SetFunctionPosition(FunctionLiteral* fun) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000634 CodeGenerator::RecordPositions(masm_, fun->start_position());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000635}
636
637
638void FullCodeGenerator::SetReturnPosition(FunctionLiteral* fun) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000639 CodeGenerator::RecordPositions(masm_, fun->end_position() - 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000640}
641
642
643void FullCodeGenerator::SetStatementPosition(Statement* stmt) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000644#ifdef ENABLE_DEBUGGER_SUPPORT
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000645 if (!isolate()->debugger()->IsDebuggerActive()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000646 CodeGenerator::RecordPositions(masm_, stmt->statement_pos());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000647 } else {
648 // Check if the statement will be breakable without adding a debug break
649 // slot.
650 BreakableStatementChecker checker;
651 checker.Check(stmt);
652 // Record the statement position right here if the statement is not
653 // breakable. For breakable statements the actual recording of the
654 // position will be postponed to the breakable code (typically an IC).
655 bool position_recorded = CodeGenerator::RecordPositions(
656 masm_, stmt->statement_pos(), !checker.is_breakable());
657 // If the position recording did record a new position generate a debug
658 // break slot to make the statement breakable.
659 if (position_recorded) {
660 Debug::GenerateSlot(masm_);
661 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000662 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000663#else
664 CodeGenerator::RecordPositions(masm_, stmt->statement_pos());
665#endif
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000666}
667
668
669void FullCodeGenerator::SetExpressionPosition(Expression* expr, int pos) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000670#ifdef ENABLE_DEBUGGER_SUPPORT
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000671 if (!isolate()->debugger()->IsDebuggerActive()) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000672 CodeGenerator::RecordPositions(masm_, pos);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000673 } else {
674 // Check if the expression will be breakable without adding a debug break
675 // slot.
676 BreakableStatementChecker checker;
677 checker.Check(expr);
678 // Record a statement position right here if the expression is not
679 // breakable. For breakable expressions the actual recording of the
680 // position will be postponed to the breakable code (typically an IC).
681 // NOTE this will record a statement position for something which might
682 // not be a statement. As stepping in the debugger will only stop at
683 // statement positions this is used for e.g. the condition expression of
684 // a do while loop.
685 bool position_recorded = CodeGenerator::RecordPositions(
686 masm_, pos, !checker.is_breakable());
687 // If the position recording did record a new position generate a debug
688 // break slot to make the statement breakable.
689 if (position_recorded) {
690 Debug::GenerateSlot(masm_);
691 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000692 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000693#else
694 CodeGenerator::RecordPositions(masm_, pos);
695#endif
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000696}
697
698
699void FullCodeGenerator::SetStatementPosition(int pos) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000700 CodeGenerator::RecordPositions(masm_, pos);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000701}
702
703
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000704void FullCodeGenerator::SetSourcePosition(int pos) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000705 if (pos != RelocInfo::kNoPosition) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000706 masm_->positions_recorder()->RecordPosition(pos);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000707 }
708}
709
710
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000711// Lookup table for code generators for special runtime calls which are
712// generated inline.
713#define INLINE_FUNCTION_GENERATOR_ADDRESS(Name, argc, ressize) \
714 &FullCodeGenerator::Emit##Name,
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000715
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000716const FullCodeGenerator::InlineFunctionGenerator
717 FullCodeGenerator::kInlineFunctionGenerators[] = {
718 INLINE_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS)
719 INLINE_RUNTIME_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS)
720 };
721#undef INLINE_FUNCTION_GENERATOR_ADDRESS
722
723
724FullCodeGenerator::InlineFunctionGenerator
725 FullCodeGenerator::FindInlineFunctionGenerator(Runtime::FunctionId id) {
whesse@chromium.org023421e2010-12-21 12:19:12 +0000726 int lookup_index =
727 static_cast<int>(id) - static_cast<int>(Runtime::kFirstInlineFunction);
728 ASSERT(lookup_index >= 0);
729 ASSERT(static_cast<size_t>(lookup_index) <
730 ARRAY_SIZE(kInlineFunctionGenerators));
731 return kInlineFunctionGenerators[lookup_index];
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000732}
733
734
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000735void FullCodeGenerator::EmitInlineRuntimeCall(CallRuntime* expr) {
736 const Runtime::Function* function = expr->function();
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000737 ASSERT(function != NULL);
738 ASSERT(function->intrinsic_type == Runtime::INLINE);
739 InlineFunctionGenerator generator =
740 FindInlineFunctionGenerator(function->function_id);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000741 ((*this).*(generator))(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000742}
743
744
745void FullCodeGenerator::VisitBinaryOperation(BinaryOperation* expr) {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000746 switch (expr->op()) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000747 case Token::COMMA:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000748 return VisitComma(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000749 case Token::OR:
750 case Token::AND:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000751 return VisitLogicalExpression(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000752 default:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000753 return VisitArithmeticExpression(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000754 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000755}
756
757
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000758void FullCodeGenerator::VisitInDuplicateContext(Expression* expr) {
759 if (context()->IsEffect()) {
760 VisitForEffect(expr);
761 } else if (context()->IsAccumulatorValue()) {
762 VisitForAccumulatorValue(expr);
763 } else if (context()->IsStackValue()) {
764 VisitForStackValue(expr);
765 } else if (context()->IsTest()) {
766 const TestContext* test = TestContext::cast(context());
767 VisitForControl(expr, test->true_label(), test->false_label(),
768 test->fall_through());
769 }
770}
771
772
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000773void FullCodeGenerator::VisitComma(BinaryOperation* expr) {
774 Comment cmnt(masm_, "[ Comma");
775 VisitForEffect(expr->left());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000776 VisitInDuplicateContext(expr->right());
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000777}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000778
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000779
780void FullCodeGenerator::VisitLogicalExpression(BinaryOperation* expr) {
781 bool is_logical_and = expr->op() == Token::AND;
782 Comment cmnt(masm_, is_logical_and ? "[ Logical AND" : "[ Logical OR");
783 Expression* left = expr->left();
784 Expression* right = expr->right();
785 int right_id = expr->RightId();
786 Label done;
787
788 if (context()->IsTest()) {
789 Label eval_right;
790 const TestContext* test = TestContext::cast(context());
791 if (is_logical_and) {
792 VisitForControl(left, &eval_right, test->false_label(), &eval_right);
793 } else {
794 VisitForControl(left, test->true_label(), &eval_right, &eval_right);
795 }
796 PrepareForBailoutForId(right_id, NO_REGISTERS);
797 __ bind(&eval_right);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000798
799 } else if (context()->IsAccumulatorValue()) {
800 VisitForAccumulatorValue(left);
801 // We want the value in the accumulator for the test, and on the stack in
802 // case we need it.
803 __ push(result_register());
804 Label discard, restore;
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000805 if (is_logical_and) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000806 DoTest(left, &discard, &restore, &restore);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000807 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000808 DoTest(left, &restore, &discard, &restore);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000809 }
810 __ bind(&restore);
811 __ pop(result_register());
812 __ jmp(&done);
813 __ bind(&discard);
814 __ Drop(1);
815 PrepareForBailoutForId(right_id, NO_REGISTERS);
816
817 } else if (context()->IsStackValue()) {
818 VisitForAccumulatorValue(left);
819 // We want the value in the accumulator for the test, and on the stack in
820 // case we need it.
821 __ push(result_register());
822 Label discard;
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000823 if (is_logical_and) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000824 DoTest(left, &discard, &done, &discard);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000825 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000826 DoTest(left, &done, &discard, &discard);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000827 }
828 __ bind(&discard);
829 __ Drop(1);
830 PrepareForBailoutForId(right_id, NO_REGISTERS);
831
832 } else {
833 ASSERT(context()->IsEffect());
834 Label eval_right;
835 if (is_logical_and) {
836 VisitForControl(left, &eval_right, &done, &eval_right);
837 } else {
838 VisitForControl(left, &done, &eval_right, &eval_right);
839 }
840 PrepareForBailoutForId(right_id, NO_REGISTERS);
841 __ bind(&eval_right);
842 }
843
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000844 VisitInDuplicateContext(right);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000845 __ bind(&done);
846}
847
848
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000849void FullCodeGenerator::VisitArithmeticExpression(BinaryOperation* expr) {
850 Token::Value op = expr->op();
851 Comment cmnt(masm_, "[ ArithmeticExpression");
852 Expression* left = expr->left();
853 Expression* right = expr->right();
854 OverwriteMode mode =
855 left->ResultOverwriteAllowed()
856 ? OVERWRITE_LEFT
857 : (right->ResultOverwriteAllowed() ? OVERWRITE_RIGHT : NO_OVERWRITE);
858
859 VisitForStackValue(left);
860 VisitForAccumulatorValue(right);
861
862 SetSourcePosition(expr->position());
863 if (ShouldInlineSmiCase(op)) {
864 EmitInlineSmiBinaryOp(expr, op, mode, left, right);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000865 } else {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000866 EmitBinaryOp(expr, op, mode);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000867 }
868}
869
870
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000871void FullCodeGenerator::VisitBlock(Block* stmt) {
872 Comment cmnt(masm_, "[ Block");
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000873 NestedBlock nested_block(this, stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000874 SetStatementPosition(stmt);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000875
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000876 Scope* saved_scope = scope();
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000877 // Push a block context when entering a block with block scoped variables.
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000878 if (stmt->block_scope() != NULL) {
879 { Comment cmnt(masm_, "[ Extend block context");
880 scope_ = stmt->block_scope();
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000881 Handle<ScopeInfo> scope_info = scope_->GetScopeInfo();
882 int heap_slots = scope_info->ContextLength() - Context::MIN_CONTEXT_SLOTS;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000883 __ Push(scope_info);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000884 PushFunctionArgumentForContextAllocation();
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000885 if (heap_slots <= FastNewBlockContextStub::kMaximumSlots) {
886 FastNewBlockContextStub stub(heap_slots);
887 __ CallStub(&stub);
888 } else {
889 __ CallRuntime(Runtime::kPushBlockContext, 2);
890 }
891
892 // Replace the context stored in the frame.
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000893 StoreToFrameField(StandardFrameConstants::kContextOffset,
894 context_register());
895 }
896 { Comment cmnt(masm_, "[ Declarations");
897 VisitDeclarations(scope_->declarations());
898 }
899 }
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +0000900 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000901 VisitStatements(stmt->statements());
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000902 scope_ = saved_scope;
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000903 __ bind(nested_block.break_label());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000904 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000905
906 // Pop block context if necessary.
907 if (stmt->block_scope() != NULL) {
908 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
909 // Update local stack frame context field.
910 StoreToFrameField(StandardFrameConstants::kContextOffset,
911 context_register());
912 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000913}
914
915
916void FullCodeGenerator::VisitExpressionStatement(ExpressionStatement* stmt) {
917 Comment cmnt(masm_, "[ ExpressionStatement");
918 SetStatementPosition(stmt);
919 VisitForEffect(stmt->expression());
920}
921
922
923void FullCodeGenerator::VisitEmptyStatement(EmptyStatement* stmt) {
924 Comment cmnt(masm_, "[ EmptyStatement");
925 SetStatementPosition(stmt);
926}
927
928
929void FullCodeGenerator::VisitIfStatement(IfStatement* stmt) {
930 Comment cmnt(masm_, "[ IfStatement");
931 SetStatementPosition(stmt);
932 Label then_part, else_part, done;
933
ricow@chromium.org65fae842010-08-25 15:26:24 +0000934 if (stmt->HasElseStatement()) {
935 VisitForControl(stmt->condition(), &then_part, &else_part, &then_part);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000936 PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000937 __ bind(&then_part);
938 Visit(stmt->then_statement());
939 __ jmp(&done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000940
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000941 PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000942 __ bind(&else_part);
943 Visit(stmt->else_statement());
944 } else {
945 VisitForControl(stmt->condition(), &then_part, &done, &then_part);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000946 PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000947 __ bind(&then_part);
948 Visit(stmt->then_statement());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000949
950 PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000951 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000952 __ bind(&done);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000953 PrepareForBailoutForId(stmt->IfId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000954}
955
956
957void FullCodeGenerator::VisitContinueStatement(ContinueStatement* stmt) {
958 Comment cmnt(masm_, "[ ContinueStatement");
959 SetStatementPosition(stmt);
960 NestedStatement* current = nesting_stack_;
961 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000962 int context_length = 0;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000963 // When continuing, we clobber the unpredictable value in the accumulator
964 // with one that's safe for GC. If we hit an exit from the try block of
965 // try...finally on our way out, we will unconditionally preserve the
966 // accumulator on the stack.
967 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000968 while (!current->IsContinueTarget(stmt->target())) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000969 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000970 }
971 __ Drop(stack_depth);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000972 if (context_length > 0) {
973 while (context_length > 0) {
974 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
975 --context_length;
976 }
977 StoreToFrameField(StandardFrameConstants::kContextOffset,
978 context_register());
979 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000980
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000981 __ jmp(current->AsIteration()->continue_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000982}
983
984
985void FullCodeGenerator::VisitBreakStatement(BreakStatement* stmt) {
986 Comment cmnt(masm_, "[ BreakStatement");
987 SetStatementPosition(stmt);
988 NestedStatement* current = nesting_stack_;
989 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000990 int context_length = 0;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000991 // When breaking, we clobber the unpredictable value in the accumulator
992 // with one that's safe for GC. If we hit an exit from the try block of
993 // try...finally on our way out, we will unconditionally preserve the
994 // accumulator on the stack.
995 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000996 while (!current->IsBreakTarget(stmt->target())) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000997 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000998 }
999 __ Drop(stack_depth);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001000 if (context_length > 0) {
1001 while (context_length > 0) {
1002 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1003 --context_length;
1004 }
1005 StoreToFrameField(StandardFrameConstants::kContextOffset,
1006 context_register());
1007 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001008
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001009 __ jmp(current->AsBreakable()->break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001010}
1011
1012
1013void FullCodeGenerator::VisitReturnStatement(ReturnStatement* stmt) {
1014 Comment cmnt(masm_, "[ ReturnStatement");
1015 SetStatementPosition(stmt);
1016 Expression* expr = stmt->expression();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001017 VisitForAccumulatorValue(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001018
1019 // Exit all nested statements.
1020 NestedStatement* current = nesting_stack_;
1021 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001022 int context_length = 0;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001023 while (current != NULL) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001024 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001025 }
1026 __ Drop(stack_depth);
1027
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001028 EmitReturnSequence();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001029}
1030
1031
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001032void FullCodeGenerator::VisitWithStatement(WithStatement* stmt) {
1033 Comment cmnt(masm_, "[ WithStatement");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001034 SetStatementPosition(stmt);
1035
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001036 VisitForStackValue(stmt->expression());
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00001037 PushFunctionArgumentForContextAllocation();
1038 __ CallRuntime(Runtime::kPushWithContext, 2);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001039 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001040
1041 { WithOrCatch body(this);
1042 Visit(stmt->statement());
1043 }
1044
1045 // Pop context.
1046 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1047 // Update local stack frame context field.
1048 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001049}
1050
1051
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001052void FullCodeGenerator::VisitDoWhileStatement(DoWhileStatement* stmt) {
1053 Comment cmnt(masm_, "[ DoWhileStatement");
1054 SetStatementPosition(stmt);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001055 Label body, stack_check;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001056
1057 Iteration loop_statement(this, stmt);
1058 increment_loop_depth();
1059
1060 __ bind(&body);
1061 Visit(stmt->body());
1062
ricow@chromium.org65fae842010-08-25 15:26:24 +00001063 // Record the position of the do while condition and make sure it is
1064 // possible to break on the condition.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001065 __ bind(loop_statement.continue_label());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001066 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001067 SetExpressionPosition(stmt->cond(), stmt->condition_position());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001068 VisitForControl(stmt->cond(),
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001069 &stack_check,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001070 loop_statement.break_label(),
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001071 &stack_check);
1072
1073 // Check stack before looping.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001074 PrepareForBailoutForId(stmt->BackEdgeId(), NO_REGISTERS);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001075 __ bind(&stack_check);
1076 EmitStackCheck(stmt);
1077 __ jmp(&body);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001078
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001079 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001080 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001081 decrement_loop_depth();
1082}
1083
1084
1085void FullCodeGenerator::VisitWhileStatement(WhileStatement* stmt) {
1086 Comment cmnt(masm_, "[ WhileStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001087 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001088
1089 Iteration loop_statement(this, stmt);
1090 increment_loop_depth();
1091
1092 // Emit the test at the bottom of the loop.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001093 __ jmp(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001094
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001095 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001096 __ bind(&body);
1097 Visit(stmt->body());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001098
1099 // Emit the statement position here as this is where the while
1100 // statement code starts.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001101 __ bind(loop_statement.continue_label());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001102 SetStatementPosition(stmt);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001103
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001104 // Check stack before looping.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001105 EmitStackCheck(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001106
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001107 __ bind(&test);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001108 VisitForControl(stmt->cond(),
1109 &body,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001110 loop_statement.break_label(),
1111 loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001112
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001113 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001114 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001115 decrement_loop_depth();
1116}
1117
1118
1119void FullCodeGenerator::VisitForStatement(ForStatement* stmt) {
1120 Comment cmnt(masm_, "[ ForStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001121 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001122
1123 Iteration loop_statement(this, stmt);
1124 if (stmt->init() != NULL) {
1125 Visit(stmt->init());
1126 }
1127
1128 increment_loop_depth();
1129 // Emit the test at the bottom of the loop (even if empty).
1130 __ jmp(&test);
1131
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001132 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001133 __ bind(&body);
1134 Visit(stmt->body());
1135
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001136 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001137 __ bind(loop_statement.continue_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001138 SetStatementPosition(stmt);
1139 if (stmt->next() != NULL) {
1140 Visit(stmt->next());
1141 }
1142
ricow@chromium.org65fae842010-08-25 15:26:24 +00001143 // Emit the statement position here as this is where the for
1144 // statement code starts.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001145 SetStatementPosition(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001146
1147 // Check stack before looping.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001148 EmitStackCheck(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001149
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001150 __ bind(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001151 if (stmt->cond() != NULL) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00001152 VisitForControl(stmt->cond(),
1153 &body,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001154 loop_statement.break_label(),
1155 loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001156 } else {
1157 __ jmp(&body);
1158 }
1159
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001160 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001161 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001162 decrement_loop_depth();
1163}
1164
1165
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001166void FullCodeGenerator::VisitTryCatchStatement(TryCatchStatement* stmt) {
1167 Comment cmnt(masm_, "[ TryCatchStatement");
1168 SetStatementPosition(stmt);
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001169 // The try block adds a handler to the exception handler chain before
1170 // entering, and removes it again when exiting normally. If an exception
1171 // is thrown during execution of the try block, the handler is consumed
1172 // and control is passed to the catch block with the exception in the
1173 // result register.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001174
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001175 Label try_entry, handler_entry, exit;
1176 __ jmp(&try_entry);
1177 __ bind(&handler_entry);
1178 handler_table()->set(stmt->index(), Smi::FromInt(handler_entry.pos()));
1179 // Exception handler code, the exception is in the result register.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001180 // Extend the context before executing the catch block.
1181 { Comment cmnt(masm_, "[ Extend catch context");
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001182 __ Push(stmt->variable()->name());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001183 __ push(result_register());
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00001184 PushFunctionArgumentForContextAllocation();
1185 __ CallRuntime(Runtime::kPushCatchContext, 3);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001186 StoreToFrameField(StandardFrameConstants::kContextOffset,
1187 context_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001188 }
1189
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001190 Scope* saved_scope = scope();
1191 scope_ = stmt->scope();
1192 ASSERT(scope_->declarations()->is_empty());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001193 { WithOrCatch catch_body(this);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001194 Visit(stmt->catch_block());
1195 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001196 // Restore the context.
1197 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1198 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001199 scope_ = saved_scope;
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001200 __ jmp(&exit);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001201
1202 // Try block code. Sets up the exception handler chain.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001203 __ bind(&try_entry);
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00001204 __ PushTryHandler(StackHandler::CATCH, stmt->index());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001205 { TryCatch try_body(this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001206 Visit(stmt->try_block());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001207 }
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001208 __ PopTryHandler();
1209 __ bind(&exit);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001210}
1211
1212
1213void FullCodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* stmt) {
1214 Comment cmnt(masm_, "[ TryFinallyStatement");
1215 SetStatementPosition(stmt);
1216 // Try finally is compiled by setting up a try-handler on the stack while
1217 // executing the try body, and removing it again afterwards.
1218 //
1219 // The try-finally construct can enter the finally block in three ways:
1220 // 1. By exiting the try-block normally. This removes the try-handler and
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001221 // calls the finally block code before continuing.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001222 // 2. By exiting the try-block with a function-local control flow transfer
1223 // (break/continue/return). The site of the, e.g., break removes the
1224 // try handler and calls the finally block code before continuing
1225 // its outward control transfer.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001226 // 3. By exiting the try-block with a thrown exception.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001227 // This can happen in nested function calls. It traverses the try-handler
1228 // chain and consumes the try-handler entry before jumping to the
1229 // handler code. The handler code then calls the finally-block before
1230 // rethrowing the exception.
1231 //
1232 // The finally block must assume a return address on top of the stack
1233 // (or in the link register on ARM chips) and a value (return value or
1234 // exception) in the result register (rax/eax/r0), both of which must
1235 // be preserved. The return address isn't GC-safe, so it should be
1236 // cooked before GC.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001237 Label try_entry, handler_entry, finally_entry;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001238
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001239 // Jump to try-handler setup and try-block code.
1240 __ jmp(&try_entry);
1241 __ bind(&handler_entry);
1242 handler_table()->set(stmt->index(), Smi::FromInt(handler_entry.pos()));
1243 // Exception handler code. This code is only executed when an exception
1244 // is thrown. The exception is in the result register, and must be
1245 // preserved by the finally block. Call the finally block and then
1246 // rethrow the exception if it returns.
1247 __ Call(&finally_entry);
1248 __ push(result_register());
1249 __ CallRuntime(Runtime::kReThrow, 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001250
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001251 // Finally block implementation.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001252 __ bind(&finally_entry);
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001253 EnterFinallyBlock();
1254 { Finally finally_body(this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001255 Visit(stmt->finally_block());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001256 }
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001257 ExitFinallyBlock(); // Return to the calling code.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001258
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001259 // Set up try handler.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001260 __ bind(&try_entry);
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00001261 __ PushTryHandler(StackHandler::FINALLY, stmt->index());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001262 { TryFinally try_body(this, &finally_entry);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001263 Visit(stmt->try_block());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001264 }
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001265 __ PopTryHandler();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001266 // Execute the finally block on the way out. Clobber the unpredictable
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001267 // value in the result register with one that's safe for GC because the
1268 // finally block will unconditionally preserve the result register on the
1269 // stack.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001270 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001271 __ Call(&finally_entry);
1272}
1273
1274
1275void FullCodeGenerator::VisitDebuggerStatement(DebuggerStatement* stmt) {
1276#ifdef ENABLE_DEBUGGER_SUPPORT
1277 Comment cmnt(masm_, "[ DebuggerStatement");
1278 SetStatementPosition(stmt);
1279
ager@chromium.org5c838252010-02-19 08:53:10 +00001280 __ DebugBreak();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001281 // Ignore the return value.
1282#endif
1283}
1284
1285
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001286void FullCodeGenerator::VisitConditional(Conditional* expr) {
1287 Comment cmnt(masm_, "[ Conditional");
1288 Label true_case, false_case, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001289 VisitForControl(expr->condition(), &true_case, &false_case, &true_case);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001290
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001291 PrepareForBailoutForId(expr->ThenId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001292 __ bind(&true_case);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001293 SetExpressionPosition(expr->then_expression(),
1294 expr->then_expression_position());
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001295 if (context()->IsTest()) {
1296 const TestContext* for_test = TestContext::cast(context());
1297 VisitForControl(expr->then_expression(),
1298 for_test->true_label(),
1299 for_test->false_label(),
1300 NULL);
1301 } else {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001302 VisitInDuplicateContext(expr->then_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001303 __ jmp(&done);
1304 }
1305
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001306 PrepareForBailoutForId(expr->ElseId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001307 __ bind(&false_case);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001308 SetExpressionPosition(expr->else_expression(),
1309 expr->else_expression_position());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001310 VisitInDuplicateContext(expr->else_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001311 // If control flow falls through Visit, merge it with true case here.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001312 if (!context()->IsTest()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001313 __ bind(&done);
1314 }
1315}
1316
1317
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001318void FullCodeGenerator::VisitLiteral(Literal* expr) {
1319 Comment cmnt(masm_, "[ Literal");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001320 context()->Plug(expr->handle());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001321}
1322
1323
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001324void FullCodeGenerator::VisitFunctionLiteral(FunctionLiteral* expr) {
1325 Comment cmnt(masm_, "[ FunctionLiteral");
1326
1327 // Build the function boilerplate and instantiate it.
1328 Handle<SharedFunctionInfo> function_info =
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001329 Compiler::BuildFunctionInfo(expr, script());
1330 if (function_info.is_null()) {
1331 SetStackOverflow();
1332 return;
1333 }
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001334 EmitNewClosure(function_info, expr->pretenure());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001335}
1336
1337
1338void FullCodeGenerator::VisitSharedFunctionInfoLiteral(
1339 SharedFunctionInfoLiteral* expr) {
1340 Comment cmnt(masm_, "[ SharedFunctionInfoLiteral");
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001341 EmitNewClosure(expr->shared_function_info(), false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001342}
1343
1344
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001345void FullCodeGenerator::VisitThrow(Throw* expr) {
1346 Comment cmnt(masm_, "[ Throw");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001347 VisitForStackValue(expr->exception());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001348 __ CallRuntime(Runtime::kThrow, 1);
1349 // Never returns here.
1350}
1351
1352
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001353FullCodeGenerator::NestedStatement* FullCodeGenerator::TryCatch::Exit(
1354 int* stack_depth,
1355 int* context_length) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001356 // The macros used here must preserve the result register.
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001357 __ Drop(*stack_depth);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001358 __ PopTryHandler();
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001359 *stack_depth = 0;
1360 return previous_;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001361}
1362
ricow@chromium.org65fae842010-08-25 15:26:24 +00001363
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001364bool FullCodeGenerator::TryLiteralCompare(CompareOperation* expr) {
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001365 Expression* sub_expr;
ager@chromium.org04921a82011-06-27 13:21:41 +00001366 Handle<String> check;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001367 if (expr->IsLiteralCompareTypeof(&sub_expr, &check)) {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001368 EmitLiteralCompareTypeof(expr, sub_expr, check);
ager@chromium.org04921a82011-06-27 13:21:41 +00001369 return true;
1370 }
1371
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001372 if (expr->IsLiteralCompareUndefined(&sub_expr)) {
1373 EmitLiteralCompareNil(expr, sub_expr, kUndefinedValue);
1374 return true;
1375 }
1376
1377 if (expr->IsLiteralCompareNull(&sub_expr)) {
1378 EmitLiteralCompareNil(expr, sub_expr, kNullValue);
ager@chromium.org04921a82011-06-27 13:21:41 +00001379 return true;
1380 }
1381
1382 return false;
1383}
1384
1385
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001386#undef __
1387
1388
1389} } // namespace v8::internal