blob: 916d52d8b77a71bc248271124ec0978c686c09ce [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
yangguo@chromium.org56454712012-02-16 15:33:53 +0000294 FullCodeGenerator cgen(&masm, info);
295 cgen.Generate();
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());
yangguo@chromium.org56454712012-02-16 15:33:53 +0000407 ASSERT(Smi::IsValid(pc_and_state));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000408 BailoutEntry entry = { id, pc_and_state };
409#ifdef DEBUG
yangguo@chromium.org659ceec2012-01-26 07:37:54 +0000410 if (FLAG_enable_slow_asserts) {
411 // Assert that we don't have multiple bailout entries for the same node.
412 for (int i = 0; i < bailout_entries_.length(); i++) {
413 if (bailout_entries_.at(i).id == entry.id) {
414 AstPrinter printer;
415 PrintF("%s", printer.PrintProgram(info_->function()));
416 UNREACHABLE();
417 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000418 }
419 }
420#endif // DEBUG
421 bailout_entries_.Add(entry);
422}
423
424
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000425void FullCodeGenerator::RecordTypeFeedbackCell(
426 unsigned id, Handle<JSGlobalPropertyCell> cell) {
427 TypeFeedbackCellEntry entry = { id, cell };
428 type_feedback_cells_.Add(entry);
429}
430
431
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +0000432void FullCodeGenerator::RecordStackCheck(unsigned ast_id) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000433 // The pc offset does not need to be encoded and packed together with a
434 // state.
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +0000435 ASSERT(masm_->pc_offset() > 0);
436 BailoutEntry entry = { ast_id, static_cast<unsigned>(masm_->pc_offset()) };
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000437 stack_checks_.Add(entry);
438}
439
440
ricow@chromium.org65fae842010-08-25 15:26:24 +0000441bool FullCodeGenerator::ShouldInlineSmiCase(Token::Value op) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000442 // Inline smi case inside loops, but not division and modulo which
443 // are too complicated and take up too much space.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000444 if (op == Token::DIV ||op == Token::MOD) return false;
445 if (FLAG_always_inline_smi_code) return true;
446 return loop_depth_ > 0;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000447}
448
449
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000450void FullCodeGenerator::EffectContext::Plug(Register reg) const {
451}
452
453
454void FullCodeGenerator::AccumulatorValueContext::Plug(Register reg) const {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000455 __ Move(result_register(), reg);
456}
457
458
459void FullCodeGenerator::StackValueContext::Plug(Register reg) const {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000460 __ push(reg);
461}
462
463
464void FullCodeGenerator::TestContext::Plug(Register reg) const {
465 // For simplicity we always test the accumulator register.
466 __ Move(result_register(), reg);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000467 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000468 codegen()->DoTest(this);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000469}
470
471
472void FullCodeGenerator::EffectContext::PlugTOS() const {
473 __ Drop(1);
474}
475
476
477void FullCodeGenerator::AccumulatorValueContext::PlugTOS() const {
478 __ pop(result_register());
479}
480
481
482void FullCodeGenerator::StackValueContext::PlugTOS() const {
483}
484
485
486void FullCodeGenerator::TestContext::PlugTOS() const {
487 // For simplicity we always test the accumulator register.
488 __ pop(result_register());
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::PrepareTest(
495 Label* materialize_true,
496 Label* materialize_false,
497 Label** if_true,
498 Label** if_false,
499 Label** fall_through) const {
500 // In an effect context, the true and the false case branch to the
501 // same label.
502 *if_true = *if_false = *fall_through = materialize_true;
503}
504
505
506void FullCodeGenerator::AccumulatorValueContext::PrepareTest(
507 Label* materialize_true,
508 Label* materialize_false,
509 Label** if_true,
510 Label** if_false,
511 Label** fall_through) const {
512 *if_true = *fall_through = materialize_true;
513 *if_false = materialize_false;
514}
515
516
517void FullCodeGenerator::StackValueContext::PrepareTest(
518 Label* materialize_true,
519 Label* materialize_false,
520 Label** if_true,
521 Label** if_false,
522 Label** fall_through) const {
523 *if_true = *fall_through = materialize_true;
524 *if_false = materialize_false;
525}
526
527
528void FullCodeGenerator::TestContext::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 = true_label_;
535 *if_false = false_label_;
536 *fall_through = fall_through_;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000537}
538
539
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000540void FullCodeGenerator::DoTest(const TestContext* context) {
541 DoTest(context->condition(),
542 context->true_label(),
543 context->false_label(),
544 context->fall_through());
545}
546
547
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000548void FullCodeGenerator::VisitDeclarations(
549 ZoneList<Declaration*>* declarations) {
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000550 int save_global_count = global_count_;
551 global_count_ = 0;
552
553 AstVisitor::VisitDeclarations(declarations);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000554
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000555 // Batch declare global functions and variables.
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000556 if (global_count_ > 0) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000557 Handle<FixedArray> array =
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000558 isolate()->factory()->NewFixedArray(2 * global_count_, TENURED);
559 int length = declarations->length();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000560 for (int j = 0, i = 0; i < length; i++) {
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000561 VariableDeclaration* decl = declarations->at(i)->AsVariableDeclaration();
562 if (decl != NULL) {
563 Variable* var = decl->proxy()->var();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000564
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000565 if (var->IsUnallocated()) {
566 array->set(j++, *(var->name()));
567 if (decl->fun() == NULL) {
568 if (var->binding_needs_init()) {
569 // In case this binding needs initialization use the hole.
570 array->set_the_hole(j++);
571 } else {
572 array->set_undefined(j++);
573 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000574 } else {
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000575 Handle<SharedFunctionInfo> function =
576 Compiler::BuildFunctionInfo(decl->fun(), script());
577 // Check for stack-overflow exception.
578 if (function.is_null()) {
579 SetStackOverflow();
580 return;
581 }
582 array->set(j++, *function);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000583 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000584 }
585 }
586 }
587 // Invoke the platform-dependent code generator to do the actual
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000588 // declaration the global functions and variables.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000589 DeclareGlobals(array);
590 }
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000591
592 global_count_ = save_global_count;
593}
594
595
596void FullCodeGenerator::VisitVariableDeclaration(VariableDeclaration* decl) {
597 EmitDeclaration(decl->proxy(), decl->mode(), decl->fun());
598}
599
600
601void FullCodeGenerator::VisitModuleDeclaration(ModuleDeclaration* decl) {
602 // TODO(rossberg)
603}
604
605
606void FullCodeGenerator::VisitModuleLiteral(ModuleLiteral* module) {
607 // TODO(rossberg)
608}
609
610
611void FullCodeGenerator::VisitModuleVariable(ModuleVariable* module) {
612 // TODO(rossberg)
613}
614
615
616void FullCodeGenerator::VisitModulePath(ModulePath* module) {
617 // TODO(rossberg)
618}
619
620
621void FullCodeGenerator::VisitModuleUrl(ModuleUrl* decl) {
622 // TODO(rossberg)
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000623}
624
625
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000626int FullCodeGenerator::DeclareGlobalsFlags() {
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000627 ASSERT(DeclareGlobalsLanguageMode::is_valid(language_mode()));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000628 return DeclareGlobalsEvalFlag::encode(is_eval()) |
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000629 DeclareGlobalsNativeFlag::encode(is_native()) |
630 DeclareGlobalsLanguageMode::encode(language_mode());
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000631}
632
633
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000634void FullCodeGenerator::SetFunctionPosition(FunctionLiteral* fun) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000635 CodeGenerator::RecordPositions(masm_, fun->start_position());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000636}
637
638
639void FullCodeGenerator::SetReturnPosition(FunctionLiteral* fun) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000640 CodeGenerator::RecordPositions(masm_, fun->end_position() - 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000641}
642
643
644void FullCodeGenerator::SetStatementPosition(Statement* stmt) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000645#ifdef ENABLE_DEBUGGER_SUPPORT
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000646 if (!isolate()->debugger()->IsDebuggerActive()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000647 CodeGenerator::RecordPositions(masm_, stmt->statement_pos());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000648 } else {
649 // Check if the statement will be breakable without adding a debug break
650 // slot.
651 BreakableStatementChecker checker;
652 checker.Check(stmt);
653 // Record the statement position right here if the statement is not
654 // breakable. For breakable statements the actual recording of the
655 // position will be postponed to the breakable code (typically an IC).
656 bool position_recorded = CodeGenerator::RecordPositions(
657 masm_, stmt->statement_pos(), !checker.is_breakable());
658 // If the position recording did record a new position generate a debug
659 // break slot to make the statement breakable.
660 if (position_recorded) {
661 Debug::GenerateSlot(masm_);
662 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000663 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000664#else
665 CodeGenerator::RecordPositions(masm_, stmt->statement_pos());
666#endif
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000667}
668
669
670void FullCodeGenerator::SetExpressionPosition(Expression* expr, int pos) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000671#ifdef ENABLE_DEBUGGER_SUPPORT
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000672 if (!isolate()->debugger()->IsDebuggerActive()) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000673 CodeGenerator::RecordPositions(masm_, pos);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000674 } else {
675 // Check if the expression will be breakable without adding a debug break
676 // slot.
677 BreakableStatementChecker checker;
678 checker.Check(expr);
679 // Record a statement position right here if the expression is not
680 // breakable. For breakable expressions the actual recording of the
681 // position will be postponed to the breakable code (typically an IC).
682 // NOTE this will record a statement position for something which might
683 // not be a statement. As stepping in the debugger will only stop at
684 // statement positions this is used for e.g. the condition expression of
685 // a do while loop.
686 bool position_recorded = CodeGenerator::RecordPositions(
687 masm_, pos, !checker.is_breakable());
688 // If the position recording did record a new position generate a debug
689 // break slot to make the statement breakable.
690 if (position_recorded) {
691 Debug::GenerateSlot(masm_);
692 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000693 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000694#else
695 CodeGenerator::RecordPositions(masm_, pos);
696#endif
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000697}
698
699
700void FullCodeGenerator::SetStatementPosition(int pos) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000701 CodeGenerator::RecordPositions(masm_, pos);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000702}
703
704
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000705void FullCodeGenerator::SetSourcePosition(int pos) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000706 if (pos != RelocInfo::kNoPosition) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000707 masm_->positions_recorder()->RecordPosition(pos);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000708 }
709}
710
711
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000712// Lookup table for code generators for special runtime calls which are
713// generated inline.
714#define INLINE_FUNCTION_GENERATOR_ADDRESS(Name, argc, ressize) \
715 &FullCodeGenerator::Emit##Name,
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000716
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000717const FullCodeGenerator::InlineFunctionGenerator
718 FullCodeGenerator::kInlineFunctionGenerators[] = {
719 INLINE_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS)
720 INLINE_RUNTIME_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS)
721 };
722#undef INLINE_FUNCTION_GENERATOR_ADDRESS
723
724
725FullCodeGenerator::InlineFunctionGenerator
726 FullCodeGenerator::FindInlineFunctionGenerator(Runtime::FunctionId id) {
whesse@chromium.org023421e2010-12-21 12:19:12 +0000727 int lookup_index =
728 static_cast<int>(id) - static_cast<int>(Runtime::kFirstInlineFunction);
729 ASSERT(lookup_index >= 0);
730 ASSERT(static_cast<size_t>(lookup_index) <
731 ARRAY_SIZE(kInlineFunctionGenerators));
732 return kInlineFunctionGenerators[lookup_index];
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000733}
734
735
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000736void FullCodeGenerator::EmitInlineRuntimeCall(CallRuntime* expr) {
737 const Runtime::Function* function = expr->function();
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000738 ASSERT(function != NULL);
739 ASSERT(function->intrinsic_type == Runtime::INLINE);
740 InlineFunctionGenerator generator =
741 FindInlineFunctionGenerator(function->function_id);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000742 ((*this).*(generator))(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000743}
744
745
746void FullCodeGenerator::VisitBinaryOperation(BinaryOperation* expr) {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000747 switch (expr->op()) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000748 case Token::COMMA:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000749 return VisitComma(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000750 case Token::OR:
751 case Token::AND:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000752 return VisitLogicalExpression(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000753 default:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000754 return VisitArithmeticExpression(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000755 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000756}
757
758
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000759void FullCodeGenerator::VisitInDuplicateContext(Expression* expr) {
760 if (context()->IsEffect()) {
761 VisitForEffect(expr);
762 } else if (context()->IsAccumulatorValue()) {
763 VisitForAccumulatorValue(expr);
764 } else if (context()->IsStackValue()) {
765 VisitForStackValue(expr);
766 } else if (context()->IsTest()) {
767 const TestContext* test = TestContext::cast(context());
768 VisitForControl(expr, test->true_label(), test->false_label(),
769 test->fall_through());
770 }
771}
772
773
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000774void FullCodeGenerator::VisitComma(BinaryOperation* expr) {
775 Comment cmnt(masm_, "[ Comma");
776 VisitForEffect(expr->left());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000777 VisitInDuplicateContext(expr->right());
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000778}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000779
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000780
781void FullCodeGenerator::VisitLogicalExpression(BinaryOperation* expr) {
782 bool is_logical_and = expr->op() == Token::AND;
783 Comment cmnt(masm_, is_logical_and ? "[ Logical AND" : "[ Logical OR");
784 Expression* left = expr->left();
785 Expression* right = expr->right();
786 int right_id = expr->RightId();
787 Label done;
788
789 if (context()->IsTest()) {
790 Label eval_right;
791 const TestContext* test = TestContext::cast(context());
792 if (is_logical_and) {
793 VisitForControl(left, &eval_right, test->false_label(), &eval_right);
794 } else {
795 VisitForControl(left, test->true_label(), &eval_right, &eval_right);
796 }
797 PrepareForBailoutForId(right_id, NO_REGISTERS);
798 __ bind(&eval_right);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000799
800 } else if (context()->IsAccumulatorValue()) {
801 VisitForAccumulatorValue(left);
802 // We want the value in the accumulator for the test, and on the stack in
803 // case we need it.
804 __ push(result_register());
805 Label discard, restore;
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000806 if (is_logical_and) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000807 DoTest(left, &discard, &restore, &restore);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000808 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000809 DoTest(left, &restore, &discard, &restore);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000810 }
811 __ bind(&restore);
812 __ pop(result_register());
813 __ jmp(&done);
814 __ bind(&discard);
815 __ Drop(1);
816 PrepareForBailoutForId(right_id, NO_REGISTERS);
817
818 } else if (context()->IsStackValue()) {
819 VisitForAccumulatorValue(left);
820 // We want the value in the accumulator for the test, and on the stack in
821 // case we need it.
822 __ push(result_register());
823 Label discard;
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000824 if (is_logical_and) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000825 DoTest(left, &discard, &done, &discard);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000826 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000827 DoTest(left, &done, &discard, &discard);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000828 }
829 __ bind(&discard);
830 __ Drop(1);
831 PrepareForBailoutForId(right_id, NO_REGISTERS);
832
833 } else {
834 ASSERT(context()->IsEffect());
835 Label eval_right;
836 if (is_logical_and) {
837 VisitForControl(left, &eval_right, &done, &eval_right);
838 } else {
839 VisitForControl(left, &done, &eval_right, &eval_right);
840 }
841 PrepareForBailoutForId(right_id, NO_REGISTERS);
842 __ bind(&eval_right);
843 }
844
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000845 VisitInDuplicateContext(right);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000846 __ bind(&done);
847}
848
849
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000850void FullCodeGenerator::VisitArithmeticExpression(BinaryOperation* expr) {
851 Token::Value op = expr->op();
852 Comment cmnt(masm_, "[ ArithmeticExpression");
853 Expression* left = expr->left();
854 Expression* right = expr->right();
855 OverwriteMode mode =
856 left->ResultOverwriteAllowed()
857 ? OVERWRITE_LEFT
858 : (right->ResultOverwriteAllowed() ? OVERWRITE_RIGHT : NO_OVERWRITE);
859
860 VisitForStackValue(left);
861 VisitForAccumulatorValue(right);
862
863 SetSourcePosition(expr->position());
864 if (ShouldInlineSmiCase(op)) {
865 EmitInlineSmiBinaryOp(expr, op, mode, left, right);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000866 } else {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000867 EmitBinaryOp(expr, op, mode);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000868 }
869}
870
871
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000872void FullCodeGenerator::VisitBlock(Block* stmt) {
873 Comment cmnt(masm_, "[ Block");
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000874 NestedBlock nested_block(this, stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000875 SetStatementPosition(stmt);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000876
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000877 Scope* saved_scope = scope();
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000878 // Push a block context when entering a block with block scoped variables.
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000879 if (stmt->block_scope() != NULL) {
880 { Comment cmnt(masm_, "[ Extend block context");
881 scope_ = stmt->block_scope();
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000882 Handle<ScopeInfo> scope_info = scope_->GetScopeInfo();
883 int heap_slots = scope_info->ContextLength() - Context::MIN_CONTEXT_SLOTS;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000884 __ Push(scope_info);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000885 PushFunctionArgumentForContextAllocation();
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000886 if (heap_slots <= FastNewBlockContextStub::kMaximumSlots) {
887 FastNewBlockContextStub stub(heap_slots);
888 __ CallStub(&stub);
889 } else {
890 __ CallRuntime(Runtime::kPushBlockContext, 2);
891 }
892
893 // Replace the context stored in the frame.
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000894 StoreToFrameField(StandardFrameConstants::kContextOffset,
895 context_register());
896 }
897 { Comment cmnt(masm_, "[ Declarations");
898 VisitDeclarations(scope_->declarations());
899 }
900 }
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +0000901 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000902 VisitStatements(stmt->statements());
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000903 scope_ = saved_scope;
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000904 __ bind(nested_block.break_label());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000905 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000906
907 // Pop block context if necessary.
908 if (stmt->block_scope() != NULL) {
909 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
910 // Update local stack frame context field.
911 StoreToFrameField(StandardFrameConstants::kContextOffset,
912 context_register());
913 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000914}
915
916
917void FullCodeGenerator::VisitExpressionStatement(ExpressionStatement* stmt) {
918 Comment cmnt(masm_, "[ ExpressionStatement");
919 SetStatementPosition(stmt);
920 VisitForEffect(stmt->expression());
921}
922
923
924void FullCodeGenerator::VisitEmptyStatement(EmptyStatement* stmt) {
925 Comment cmnt(masm_, "[ EmptyStatement");
926 SetStatementPosition(stmt);
927}
928
929
930void FullCodeGenerator::VisitIfStatement(IfStatement* stmt) {
931 Comment cmnt(masm_, "[ IfStatement");
932 SetStatementPosition(stmt);
933 Label then_part, else_part, done;
934
ricow@chromium.org65fae842010-08-25 15:26:24 +0000935 if (stmt->HasElseStatement()) {
936 VisitForControl(stmt->condition(), &then_part, &else_part, &then_part);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000937 PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000938 __ bind(&then_part);
939 Visit(stmt->then_statement());
940 __ jmp(&done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000941
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000942 PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000943 __ bind(&else_part);
944 Visit(stmt->else_statement());
945 } else {
946 VisitForControl(stmt->condition(), &then_part, &done, &then_part);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000947 PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000948 __ bind(&then_part);
949 Visit(stmt->then_statement());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000950
951 PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000952 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000953 __ bind(&done);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000954 PrepareForBailoutForId(stmt->IfId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000955}
956
957
958void FullCodeGenerator::VisitContinueStatement(ContinueStatement* stmt) {
959 Comment cmnt(masm_, "[ ContinueStatement");
960 SetStatementPosition(stmt);
961 NestedStatement* current = nesting_stack_;
962 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000963 int context_length = 0;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000964 // When continuing, we clobber the unpredictable value in the accumulator
965 // with one that's safe for GC. If we hit an exit from the try block of
966 // try...finally on our way out, we will unconditionally preserve the
967 // accumulator on the stack.
968 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000969 while (!current->IsContinueTarget(stmt->target())) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000970 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000971 }
972 __ Drop(stack_depth);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000973 if (context_length > 0) {
974 while (context_length > 0) {
975 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
976 --context_length;
977 }
978 StoreToFrameField(StandardFrameConstants::kContextOffset,
979 context_register());
980 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000981
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000982 __ jmp(current->AsIteration()->continue_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000983}
984
985
986void FullCodeGenerator::VisitBreakStatement(BreakStatement* stmt) {
987 Comment cmnt(masm_, "[ BreakStatement");
988 SetStatementPosition(stmt);
989 NestedStatement* current = nesting_stack_;
990 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000991 int context_length = 0;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000992 // When breaking, we clobber the unpredictable value in the accumulator
993 // with one that's safe for GC. If we hit an exit from the try block of
994 // try...finally on our way out, we will unconditionally preserve the
995 // accumulator on the stack.
996 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000997 while (!current->IsBreakTarget(stmt->target())) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000998 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000999 }
1000 __ Drop(stack_depth);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001001 if (context_length > 0) {
1002 while (context_length > 0) {
1003 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1004 --context_length;
1005 }
1006 StoreToFrameField(StandardFrameConstants::kContextOffset,
1007 context_register());
1008 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001009
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001010 __ jmp(current->AsBreakable()->break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001011}
1012
1013
1014void FullCodeGenerator::VisitReturnStatement(ReturnStatement* stmt) {
1015 Comment cmnt(masm_, "[ ReturnStatement");
1016 SetStatementPosition(stmt);
1017 Expression* expr = stmt->expression();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001018 VisitForAccumulatorValue(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001019
1020 // Exit all nested statements.
1021 NestedStatement* current = nesting_stack_;
1022 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001023 int context_length = 0;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001024 while (current != NULL) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001025 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001026 }
1027 __ Drop(stack_depth);
1028
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001029 EmitReturnSequence();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001030}
1031
1032
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001033void FullCodeGenerator::VisitWithStatement(WithStatement* stmt) {
1034 Comment cmnt(masm_, "[ WithStatement");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001035 SetStatementPosition(stmt);
1036
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001037 VisitForStackValue(stmt->expression());
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00001038 PushFunctionArgumentForContextAllocation();
1039 __ CallRuntime(Runtime::kPushWithContext, 2);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001040 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001041
1042 { WithOrCatch body(this);
1043 Visit(stmt->statement());
1044 }
1045
1046 // Pop context.
1047 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1048 // Update local stack frame context field.
1049 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001050}
1051
1052
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001053void FullCodeGenerator::VisitDoWhileStatement(DoWhileStatement* stmt) {
1054 Comment cmnt(masm_, "[ DoWhileStatement");
1055 SetStatementPosition(stmt);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001056 Label body, stack_check;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001057
1058 Iteration loop_statement(this, stmt);
1059 increment_loop_depth();
1060
1061 __ bind(&body);
1062 Visit(stmt->body());
1063
ricow@chromium.org65fae842010-08-25 15:26:24 +00001064 // Record the position of the do while condition and make sure it is
1065 // possible to break on the condition.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001066 __ bind(loop_statement.continue_label());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001067 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001068 SetExpressionPosition(stmt->cond(), stmt->condition_position());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001069 VisitForControl(stmt->cond(),
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001070 &stack_check,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001071 loop_statement.break_label(),
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001072 &stack_check);
1073
1074 // Check stack before looping.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001075 PrepareForBailoutForId(stmt->BackEdgeId(), NO_REGISTERS);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001076 __ bind(&stack_check);
yangguo@chromium.org56454712012-02-16 15:33:53 +00001077 EmitStackCheck(stmt, &body);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001078 __ jmp(&body);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001079
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001080 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001081 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001082 decrement_loop_depth();
1083}
1084
1085
1086void FullCodeGenerator::VisitWhileStatement(WhileStatement* stmt) {
1087 Comment cmnt(masm_, "[ WhileStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001088 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001089
1090 Iteration loop_statement(this, stmt);
1091 increment_loop_depth();
1092
1093 // Emit the test at the bottom of the loop.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001094 __ jmp(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001095
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001096 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001097 __ bind(&body);
1098 Visit(stmt->body());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001099
1100 // Emit the statement position here as this is where the while
1101 // statement code starts.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001102 __ bind(loop_statement.continue_label());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001103 SetStatementPosition(stmt);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001104
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001105 // Check stack before looping.
yangguo@chromium.org56454712012-02-16 15:33:53 +00001106 EmitStackCheck(stmt, &body);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001107
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001108 __ bind(&test);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001109 VisitForControl(stmt->cond(),
1110 &body,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001111 loop_statement.break_label(),
1112 loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001113
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001114 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001115 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001116 decrement_loop_depth();
1117}
1118
1119
1120void FullCodeGenerator::VisitForStatement(ForStatement* stmt) {
1121 Comment cmnt(masm_, "[ ForStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001122 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001123
1124 Iteration loop_statement(this, stmt);
1125 if (stmt->init() != NULL) {
1126 Visit(stmt->init());
1127 }
1128
1129 increment_loop_depth();
1130 // Emit the test at the bottom of the loop (even if empty).
1131 __ jmp(&test);
1132
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001133 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001134 __ bind(&body);
1135 Visit(stmt->body());
1136
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001137 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001138 __ bind(loop_statement.continue_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001139 SetStatementPosition(stmt);
1140 if (stmt->next() != NULL) {
1141 Visit(stmt->next());
1142 }
1143
ricow@chromium.org65fae842010-08-25 15:26:24 +00001144 // Emit the statement position here as this is where the for
1145 // statement code starts.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001146 SetStatementPosition(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001147
1148 // Check stack before looping.
yangguo@chromium.org56454712012-02-16 15:33:53 +00001149 EmitStackCheck(stmt, &body);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001150
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001151 __ bind(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001152 if (stmt->cond() != NULL) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00001153 VisitForControl(stmt->cond(),
1154 &body,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001155 loop_statement.break_label(),
1156 loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001157 } else {
1158 __ jmp(&body);
1159 }
1160
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001161 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001162 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001163 decrement_loop_depth();
1164}
1165
1166
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001167void FullCodeGenerator::VisitTryCatchStatement(TryCatchStatement* stmt) {
1168 Comment cmnt(masm_, "[ TryCatchStatement");
1169 SetStatementPosition(stmt);
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001170 // The try block adds a handler to the exception handler chain before
1171 // entering, and removes it again when exiting normally. If an exception
1172 // is thrown during execution of the try block, the handler is consumed
1173 // and control is passed to the catch block with the exception in the
1174 // result register.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001175
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001176 Label try_entry, handler_entry, exit;
1177 __ jmp(&try_entry);
1178 __ bind(&handler_entry);
1179 handler_table()->set(stmt->index(), Smi::FromInt(handler_entry.pos()));
1180 // Exception handler code, the exception is in the result register.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001181 // Extend the context before executing the catch block.
1182 { Comment cmnt(masm_, "[ Extend catch context");
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001183 __ Push(stmt->variable()->name());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001184 __ push(result_register());
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00001185 PushFunctionArgumentForContextAllocation();
1186 __ CallRuntime(Runtime::kPushCatchContext, 3);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001187 StoreToFrameField(StandardFrameConstants::kContextOffset,
1188 context_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001189 }
1190
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001191 Scope* saved_scope = scope();
1192 scope_ = stmt->scope();
1193 ASSERT(scope_->declarations()->is_empty());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001194 { WithOrCatch catch_body(this);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001195 Visit(stmt->catch_block());
1196 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001197 // Restore the context.
1198 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1199 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001200 scope_ = saved_scope;
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001201 __ jmp(&exit);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001202
1203 // Try block code. Sets up the exception handler chain.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001204 __ bind(&try_entry);
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00001205 __ PushTryHandler(StackHandler::CATCH, stmt->index());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001206 { TryCatch try_body(this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001207 Visit(stmt->try_block());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001208 }
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001209 __ PopTryHandler();
1210 __ bind(&exit);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001211}
1212
1213
1214void FullCodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* stmt) {
1215 Comment cmnt(masm_, "[ TryFinallyStatement");
1216 SetStatementPosition(stmt);
1217 // Try finally is compiled by setting up a try-handler on the stack while
1218 // executing the try body, and removing it again afterwards.
1219 //
1220 // The try-finally construct can enter the finally block in three ways:
1221 // 1. By exiting the try-block normally. This removes the try-handler and
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001222 // calls the finally block code before continuing.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001223 // 2. By exiting the try-block with a function-local control flow transfer
1224 // (break/continue/return). The site of the, e.g., break removes the
1225 // try handler and calls the finally block code before continuing
1226 // its outward control transfer.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001227 // 3. By exiting the try-block with a thrown exception.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001228 // This can happen in nested function calls. It traverses the try-handler
1229 // chain and consumes the try-handler entry before jumping to the
1230 // handler code. The handler code then calls the finally-block before
1231 // rethrowing the exception.
1232 //
1233 // The finally block must assume a return address on top of the stack
1234 // (or in the link register on ARM chips) and a value (return value or
1235 // exception) in the result register (rax/eax/r0), both of which must
1236 // be preserved. The return address isn't GC-safe, so it should be
1237 // cooked before GC.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001238 Label try_entry, handler_entry, finally_entry;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001239
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001240 // Jump to try-handler setup and try-block code.
1241 __ jmp(&try_entry);
1242 __ bind(&handler_entry);
1243 handler_table()->set(stmt->index(), Smi::FromInt(handler_entry.pos()));
1244 // Exception handler code. This code is only executed when an exception
1245 // is thrown. The exception is in the result register, and must be
1246 // preserved by the finally block. Call the finally block and then
1247 // rethrow the exception if it returns.
1248 __ Call(&finally_entry);
1249 __ push(result_register());
1250 __ CallRuntime(Runtime::kReThrow, 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001251
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001252 // Finally block implementation.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001253 __ bind(&finally_entry);
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001254 EnterFinallyBlock();
1255 { Finally finally_body(this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001256 Visit(stmt->finally_block());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001257 }
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001258 ExitFinallyBlock(); // Return to the calling code.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001259
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001260 // Set up try handler.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001261 __ bind(&try_entry);
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00001262 __ PushTryHandler(StackHandler::FINALLY, stmt->index());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001263 { TryFinally try_body(this, &finally_entry);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001264 Visit(stmt->try_block());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001265 }
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001266 __ PopTryHandler();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001267 // Execute the finally block on the way out. Clobber the unpredictable
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001268 // value in the result register with one that's safe for GC because the
1269 // finally block will unconditionally preserve the result register on the
1270 // stack.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001271 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001272 __ Call(&finally_entry);
1273}
1274
1275
1276void FullCodeGenerator::VisitDebuggerStatement(DebuggerStatement* stmt) {
1277#ifdef ENABLE_DEBUGGER_SUPPORT
1278 Comment cmnt(masm_, "[ DebuggerStatement");
1279 SetStatementPosition(stmt);
1280
ager@chromium.org5c838252010-02-19 08:53:10 +00001281 __ DebugBreak();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001282 // Ignore the return value.
1283#endif
1284}
1285
1286
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001287void FullCodeGenerator::VisitConditional(Conditional* expr) {
1288 Comment cmnt(masm_, "[ Conditional");
1289 Label true_case, false_case, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001290 VisitForControl(expr->condition(), &true_case, &false_case, &true_case);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001291
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001292 PrepareForBailoutForId(expr->ThenId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001293 __ bind(&true_case);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001294 SetExpressionPosition(expr->then_expression(),
1295 expr->then_expression_position());
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001296 if (context()->IsTest()) {
1297 const TestContext* for_test = TestContext::cast(context());
1298 VisitForControl(expr->then_expression(),
1299 for_test->true_label(),
1300 for_test->false_label(),
1301 NULL);
1302 } else {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001303 VisitInDuplicateContext(expr->then_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001304 __ jmp(&done);
1305 }
1306
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001307 PrepareForBailoutForId(expr->ElseId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001308 __ bind(&false_case);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001309 SetExpressionPosition(expr->else_expression(),
1310 expr->else_expression_position());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001311 VisitInDuplicateContext(expr->else_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001312 // If control flow falls through Visit, merge it with true case here.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001313 if (!context()->IsTest()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001314 __ bind(&done);
1315 }
1316}
1317
1318
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001319void FullCodeGenerator::VisitLiteral(Literal* expr) {
1320 Comment cmnt(masm_, "[ Literal");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001321 context()->Plug(expr->handle());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001322}
1323
1324
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001325void FullCodeGenerator::VisitFunctionLiteral(FunctionLiteral* expr) {
1326 Comment cmnt(masm_, "[ FunctionLiteral");
1327
1328 // Build the function boilerplate and instantiate it.
1329 Handle<SharedFunctionInfo> function_info =
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001330 Compiler::BuildFunctionInfo(expr, script());
1331 if (function_info.is_null()) {
1332 SetStackOverflow();
1333 return;
1334 }
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001335 EmitNewClosure(function_info, expr->pretenure());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001336}
1337
1338
1339void FullCodeGenerator::VisitSharedFunctionInfoLiteral(
1340 SharedFunctionInfoLiteral* expr) {
1341 Comment cmnt(masm_, "[ SharedFunctionInfoLiteral");
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001342 EmitNewClosure(expr->shared_function_info(), false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001343}
1344
1345
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001346void FullCodeGenerator::VisitThrow(Throw* expr) {
1347 Comment cmnt(masm_, "[ Throw");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001348 VisitForStackValue(expr->exception());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001349 __ CallRuntime(Runtime::kThrow, 1);
1350 // Never returns here.
1351}
1352
1353
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001354FullCodeGenerator::NestedStatement* FullCodeGenerator::TryCatch::Exit(
1355 int* stack_depth,
1356 int* context_length) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001357 // The macros used here must preserve the result register.
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001358 __ Drop(*stack_depth);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001359 __ PopTryHandler();
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001360 *stack_depth = 0;
1361 return previous_;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001362}
1363
ricow@chromium.org65fae842010-08-25 15:26:24 +00001364
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001365bool FullCodeGenerator::TryLiteralCompare(CompareOperation* expr) {
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001366 Expression* sub_expr;
ager@chromium.org04921a82011-06-27 13:21:41 +00001367 Handle<String> check;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001368 if (expr->IsLiteralCompareTypeof(&sub_expr, &check)) {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001369 EmitLiteralCompareTypeof(expr, sub_expr, check);
ager@chromium.org04921a82011-06-27 13:21:41 +00001370 return true;
1371 }
1372
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001373 if (expr->IsLiteralCompareUndefined(&sub_expr)) {
1374 EmitLiteralCompareNil(expr, sub_expr, kUndefinedValue);
1375 return true;
1376 }
1377
1378 if (expr->IsLiteralCompareNull(&sub_expr)) {
1379 EmitLiteralCompareNil(expr, sub_expr, kNullValue);
ager@chromium.org04921a82011-06-27 13:21:41 +00001380 return true;
1381 }
1382
1383 return false;
1384}
1385
1386
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001387#undef __
1388
1389
1390} } // namespace v8::internal