blob: 3d10e96b53b1c64460b758833c60b5d59d6d42d8 [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);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000306 cgen.PopulateTypeFeedbackInfo(code);
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000307 cgen.PopulateTypeFeedbackCells(code);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000308 code->set_has_deoptimization_support(info->HasDeoptimizationSupport());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +0000309 code->set_handler_table(*cgen.handler_table());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000310#ifdef ENABLE_DEBUGGER_SUPPORT
lrn@chromium.org34e60782011-09-15 07:25:40 +0000311 code->set_has_debug_break_slots(
312 info->isolate()->debugger()->IsDebuggerActive());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000313 code->set_compiled_optimizable(info->IsOptimizable());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000314#endif // ENABLE_DEBUGGER_SUPPORT
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000315 code->set_allow_osr_at_loop_nesting_level(0);
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000316 code->set_stack_check_table_offset(table_offset);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000317 CodeGenerator::PrintCode(code, info);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000318 info->SetCode(code); // May be an empty handle.
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000319 if (!code.is_null()) {
320 isolate->runtime_profiler()->NotifyCodeGenerated(code->instruction_size());
321 }
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000322#ifdef ENABLE_GDB_JIT_INTERFACE
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000323 if (FLAG_gdbjit && !code.is_null()) {
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000324 GDBJITLineInfo* lineinfo =
325 masm.positions_recorder()->DetachGDBJITLineInfo();
326
327 GDBJIT(RegisterDetailedLineInfo(*code, lineinfo));
328 }
329#endif
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000330 return !code.is_null();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000331}
332
333
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000334unsigned FullCodeGenerator::EmitStackCheckTable() {
335 // The stack check table consists of a length (in number of entries)
336 // field, and then a sequence of entries. Each entry is a pair of AST id
337 // and code-relative pc offset.
338 masm()->Align(kIntSize);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000339 unsigned offset = masm()->pc_offset();
340 unsigned length = stack_checks_.length();
341 __ dd(length);
342 for (unsigned i = 0; i < length; ++i) {
343 __ dd(stack_checks_[i].id);
344 __ dd(stack_checks_[i].pc_and_state);
345 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000346 return offset;
347}
348
349
350void FullCodeGenerator::PopulateDeoptimizationData(Handle<Code> code) {
351 // Fill in the deoptimization information.
352 ASSERT(info_->HasDeoptimizationSupport() || bailout_entries_.is_empty());
353 if (!info_->HasDeoptimizationSupport()) return;
354 int length = bailout_entries_.length();
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000355 Handle<DeoptimizationOutputData> data = isolate()->factory()->
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000356 NewDeoptimizationOutputData(length, TENURED);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000357 for (int i = 0; i < length; i++) {
358 data->SetAstId(i, Smi::FromInt(bailout_entries_[i].id));
359 data->SetPcAndState(i, Smi::FromInt(bailout_entries_[i].pc_and_state));
360 }
361 code->set_deoptimization_data(*data);
362}
363
364
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000365void FullCodeGenerator::PopulateTypeFeedbackInfo(Handle<Code> code) {
366 Handle<TypeFeedbackInfo> info = isolate()->factory()->NewTypeFeedbackInfo();
367 info->set_ic_total_count(ic_total_count_);
368 code->set_type_feedback_info(*info);
369}
370
371
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000372void FullCodeGenerator::PopulateTypeFeedbackCells(Handle<Code> code) {
373 if (type_feedback_cells_.is_empty()) return;
374 int length = type_feedback_cells_.length();
375 int array_size = TypeFeedbackCells::LengthOfFixedArray(length);
376 Handle<TypeFeedbackCells> cache = Handle<TypeFeedbackCells>::cast(
377 isolate()->factory()->NewFixedArray(array_size, TENURED));
378 for (int i = 0; i < length; i++) {
379 cache->SetAstId(i, Smi::FromInt(type_feedback_cells_[i].ast_id));
380 cache->SetCell(i, *type_feedback_cells_[i].cell);
381 }
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000382 TypeFeedbackInfo::cast(code->type_feedback_info())->set_type_feedback_cells(
383 *cache);
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000384}
385
386
387
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000388void FullCodeGenerator::PrepareForBailout(Expression* node, State state) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000389 PrepareForBailoutForId(node->id(), state);
390}
391
392
393void FullCodeGenerator::RecordJSReturnSite(Call* call) {
394 // We record the offset of the function return so we can rebuild the frame
395 // if the function was inlined, i.e., this is the return address in the
396 // inlined function's frame.
397 //
398 // The state is ignored. We defensively set it to TOS_REG, which is the
399 // real state of the unoptimized code at the return site.
400 PrepareForBailoutForId(call->ReturnId(), TOS_REG);
401#ifdef DEBUG
402 // In debug builds, mark the return so we can verify that this function
403 // was called.
404 ASSERT(!call->return_is_recorded_);
405 call->return_is_recorded_ = true;
406#endif
407}
408
409
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +0000410void FullCodeGenerator::PrepareForBailoutForId(unsigned id, State state) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000411 // There's no need to prepare this code for bailouts from already optimized
412 // code or code that can't be optimized.
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000413 if (!info_->HasDeoptimizationSupport()) return;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000414 unsigned pc_and_state =
415 StateField::encode(state) | PcField::encode(masm_->pc_offset());
yangguo@chromium.org56454712012-02-16 15:33:53 +0000416 ASSERT(Smi::IsValid(pc_and_state));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000417 BailoutEntry entry = { id, pc_and_state };
418#ifdef DEBUG
yangguo@chromium.org659ceec2012-01-26 07:37:54 +0000419 if (FLAG_enable_slow_asserts) {
420 // Assert that we don't have multiple bailout entries for the same node.
421 for (int i = 0; i < bailout_entries_.length(); i++) {
422 if (bailout_entries_.at(i).id == entry.id) {
423 AstPrinter printer;
424 PrintF("%s", printer.PrintProgram(info_->function()));
425 UNREACHABLE();
426 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000427 }
428 }
429#endif // DEBUG
430 bailout_entries_.Add(entry);
431}
432
433
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000434void FullCodeGenerator::RecordTypeFeedbackCell(
435 unsigned id, Handle<JSGlobalPropertyCell> cell) {
436 TypeFeedbackCellEntry entry = { id, cell };
437 type_feedback_cells_.Add(entry);
438}
439
440
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +0000441void FullCodeGenerator::RecordStackCheck(unsigned ast_id) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000442 // The pc offset does not need to be encoded and packed together with a
443 // state.
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +0000444 ASSERT(masm_->pc_offset() > 0);
445 BailoutEntry entry = { ast_id, static_cast<unsigned>(masm_->pc_offset()) };
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000446 stack_checks_.Add(entry);
447}
448
449
ricow@chromium.org65fae842010-08-25 15:26:24 +0000450bool FullCodeGenerator::ShouldInlineSmiCase(Token::Value op) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000451 // Inline smi case inside loops, but not division and modulo which
452 // are too complicated and take up too much space.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000453 if (op == Token::DIV ||op == Token::MOD) return false;
454 if (FLAG_always_inline_smi_code) return true;
455 return loop_depth_ > 0;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000456}
457
458
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000459void FullCodeGenerator::EffectContext::Plug(Register reg) const {
460}
461
462
463void FullCodeGenerator::AccumulatorValueContext::Plug(Register reg) const {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000464 __ Move(result_register(), reg);
465}
466
467
468void FullCodeGenerator::StackValueContext::Plug(Register reg) const {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000469 __ push(reg);
470}
471
472
473void FullCodeGenerator::TestContext::Plug(Register reg) const {
474 // For simplicity we always test the accumulator register.
475 __ Move(result_register(), reg);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000476 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000477 codegen()->DoTest(this);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000478}
479
480
481void FullCodeGenerator::EffectContext::PlugTOS() const {
482 __ Drop(1);
483}
484
485
486void FullCodeGenerator::AccumulatorValueContext::PlugTOS() const {
487 __ pop(result_register());
488}
489
490
491void FullCodeGenerator::StackValueContext::PlugTOS() const {
492}
493
494
495void FullCodeGenerator::TestContext::PlugTOS() const {
496 // For simplicity we always test the accumulator register.
497 __ pop(result_register());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000498 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000499 codegen()->DoTest(this);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000500}
501
502
503void FullCodeGenerator::EffectContext::PrepareTest(
504 Label* materialize_true,
505 Label* materialize_false,
506 Label** if_true,
507 Label** if_false,
508 Label** fall_through) const {
509 // In an effect context, the true and the false case branch to the
510 // same label.
511 *if_true = *if_false = *fall_through = materialize_true;
512}
513
514
515void FullCodeGenerator::AccumulatorValueContext::PrepareTest(
516 Label* materialize_true,
517 Label* materialize_false,
518 Label** if_true,
519 Label** if_false,
520 Label** fall_through) const {
521 *if_true = *fall_through = materialize_true;
522 *if_false = materialize_false;
523}
524
525
526void FullCodeGenerator::StackValueContext::PrepareTest(
527 Label* materialize_true,
528 Label* materialize_false,
529 Label** if_true,
530 Label** if_false,
531 Label** fall_through) const {
532 *if_true = *fall_through = materialize_true;
533 *if_false = materialize_false;
534}
535
536
537void FullCodeGenerator::TestContext::PrepareTest(
538 Label* materialize_true,
539 Label* materialize_false,
540 Label** if_true,
541 Label** if_false,
542 Label** fall_through) const {
543 *if_true = true_label_;
544 *if_false = false_label_;
545 *fall_through = fall_through_;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000546}
547
548
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000549void FullCodeGenerator::DoTest(const TestContext* context) {
550 DoTest(context->condition(),
551 context->true_label(),
552 context->false_label(),
553 context->fall_through());
554}
555
556
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000557void FullCodeGenerator::VisitDeclarations(
558 ZoneList<Declaration*>* declarations) {
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000559 int save_global_count = global_count_;
560 global_count_ = 0;
561
562 AstVisitor::VisitDeclarations(declarations);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000563
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000564 // Batch declare global functions and variables.
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000565 if (global_count_ > 0) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000566 Handle<FixedArray> array =
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000567 isolate()->factory()->NewFixedArray(2 * global_count_, TENURED);
568 int length = declarations->length();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000569 for (int j = 0, i = 0; i < length; i++) {
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000570 VariableDeclaration* decl = declarations->at(i)->AsVariableDeclaration();
571 if (decl != NULL) {
572 Variable* var = decl->proxy()->var();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000573
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000574 if (var->IsUnallocated()) {
575 array->set(j++, *(var->name()));
576 if (decl->fun() == NULL) {
577 if (var->binding_needs_init()) {
578 // In case this binding needs initialization use the hole.
579 array->set_the_hole(j++);
580 } else {
581 array->set_undefined(j++);
582 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000583 } else {
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000584 Handle<SharedFunctionInfo> function =
585 Compiler::BuildFunctionInfo(decl->fun(), script());
586 // Check for stack-overflow exception.
587 if (function.is_null()) {
588 SetStackOverflow();
589 return;
590 }
591 array->set(j++, *function);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000592 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000593 }
594 }
595 }
596 // Invoke the platform-dependent code generator to do the actual
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000597 // declaration the global functions and variables.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000598 DeclareGlobals(array);
599 }
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000600
601 global_count_ = save_global_count;
602}
603
604
605void FullCodeGenerator::VisitVariableDeclaration(VariableDeclaration* decl) {
606 EmitDeclaration(decl->proxy(), decl->mode(), decl->fun());
607}
608
609
610void FullCodeGenerator::VisitModuleDeclaration(ModuleDeclaration* decl) {
611 // TODO(rossberg)
612}
613
614
615void FullCodeGenerator::VisitModuleLiteral(ModuleLiteral* module) {
616 // TODO(rossberg)
617}
618
619
620void FullCodeGenerator::VisitModuleVariable(ModuleVariable* module) {
621 // TODO(rossberg)
622}
623
624
625void FullCodeGenerator::VisitModulePath(ModulePath* module) {
626 // TODO(rossberg)
627}
628
629
630void FullCodeGenerator::VisitModuleUrl(ModuleUrl* decl) {
631 // TODO(rossberg)
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000632}
633
634
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000635int FullCodeGenerator::DeclareGlobalsFlags() {
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000636 ASSERT(DeclareGlobalsLanguageMode::is_valid(language_mode()));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000637 return DeclareGlobalsEvalFlag::encode(is_eval()) |
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000638 DeclareGlobalsNativeFlag::encode(is_native()) |
639 DeclareGlobalsLanguageMode::encode(language_mode());
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000640}
641
642
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000643void FullCodeGenerator::SetFunctionPosition(FunctionLiteral* fun) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000644 CodeGenerator::RecordPositions(masm_, fun->start_position());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000645}
646
647
648void FullCodeGenerator::SetReturnPosition(FunctionLiteral* fun) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000649 CodeGenerator::RecordPositions(masm_, fun->end_position() - 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000650}
651
652
653void FullCodeGenerator::SetStatementPosition(Statement* stmt) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000654#ifdef ENABLE_DEBUGGER_SUPPORT
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000655 if (!isolate()->debugger()->IsDebuggerActive()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000656 CodeGenerator::RecordPositions(masm_, stmt->statement_pos());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000657 } else {
658 // Check if the statement will be breakable without adding a debug break
659 // slot.
660 BreakableStatementChecker checker;
661 checker.Check(stmt);
662 // Record the statement position right here if the statement is not
663 // breakable. For breakable statements the actual recording of the
664 // position will be postponed to the breakable code (typically an IC).
665 bool position_recorded = CodeGenerator::RecordPositions(
666 masm_, stmt->statement_pos(), !checker.is_breakable());
667 // If the position recording did record a new position generate a debug
668 // break slot to make the statement breakable.
669 if (position_recorded) {
670 Debug::GenerateSlot(masm_);
671 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000672 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000673#else
674 CodeGenerator::RecordPositions(masm_, stmt->statement_pos());
675#endif
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000676}
677
678
679void FullCodeGenerator::SetExpressionPosition(Expression* expr, int pos) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000680#ifdef ENABLE_DEBUGGER_SUPPORT
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000681 if (!isolate()->debugger()->IsDebuggerActive()) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000682 CodeGenerator::RecordPositions(masm_, pos);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000683 } else {
684 // Check if the expression will be breakable without adding a debug break
685 // slot.
686 BreakableStatementChecker checker;
687 checker.Check(expr);
688 // Record a statement position right here if the expression is not
689 // breakable. For breakable expressions the actual recording of the
690 // position will be postponed to the breakable code (typically an IC).
691 // NOTE this will record a statement position for something which might
692 // not be a statement. As stepping in the debugger will only stop at
693 // statement positions this is used for e.g. the condition expression of
694 // a do while loop.
695 bool position_recorded = CodeGenerator::RecordPositions(
696 masm_, pos, !checker.is_breakable());
697 // If the position recording did record a new position generate a debug
698 // break slot to make the statement breakable.
699 if (position_recorded) {
700 Debug::GenerateSlot(masm_);
701 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000702 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000703#else
704 CodeGenerator::RecordPositions(masm_, pos);
705#endif
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000706}
707
708
709void FullCodeGenerator::SetStatementPosition(int pos) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000710 CodeGenerator::RecordPositions(masm_, pos);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000711}
712
713
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000714void FullCodeGenerator::SetSourcePosition(int pos) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000715 if (pos != RelocInfo::kNoPosition) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000716 masm_->positions_recorder()->RecordPosition(pos);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000717 }
718}
719
720
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000721// Lookup table for code generators for special runtime calls which are
722// generated inline.
723#define INLINE_FUNCTION_GENERATOR_ADDRESS(Name, argc, ressize) \
724 &FullCodeGenerator::Emit##Name,
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000725
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000726const FullCodeGenerator::InlineFunctionGenerator
727 FullCodeGenerator::kInlineFunctionGenerators[] = {
728 INLINE_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS)
729 INLINE_RUNTIME_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS)
730 };
731#undef INLINE_FUNCTION_GENERATOR_ADDRESS
732
733
734FullCodeGenerator::InlineFunctionGenerator
735 FullCodeGenerator::FindInlineFunctionGenerator(Runtime::FunctionId id) {
whesse@chromium.org023421e2010-12-21 12:19:12 +0000736 int lookup_index =
737 static_cast<int>(id) - static_cast<int>(Runtime::kFirstInlineFunction);
738 ASSERT(lookup_index >= 0);
739 ASSERT(static_cast<size_t>(lookup_index) <
740 ARRAY_SIZE(kInlineFunctionGenerators));
741 return kInlineFunctionGenerators[lookup_index];
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000742}
743
744
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000745void FullCodeGenerator::EmitInlineRuntimeCall(CallRuntime* expr) {
746 const Runtime::Function* function = expr->function();
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000747 ASSERT(function != NULL);
748 ASSERT(function->intrinsic_type == Runtime::INLINE);
749 InlineFunctionGenerator generator =
750 FindInlineFunctionGenerator(function->function_id);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000751 ((*this).*(generator))(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000752}
753
754
755void FullCodeGenerator::VisitBinaryOperation(BinaryOperation* expr) {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000756 switch (expr->op()) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000757 case Token::COMMA:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000758 return VisitComma(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000759 case Token::OR:
760 case Token::AND:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000761 return VisitLogicalExpression(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000762 default:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000763 return VisitArithmeticExpression(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000764 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000765}
766
767
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000768void FullCodeGenerator::VisitInDuplicateContext(Expression* expr) {
769 if (context()->IsEffect()) {
770 VisitForEffect(expr);
771 } else if (context()->IsAccumulatorValue()) {
772 VisitForAccumulatorValue(expr);
773 } else if (context()->IsStackValue()) {
774 VisitForStackValue(expr);
775 } else if (context()->IsTest()) {
776 const TestContext* test = TestContext::cast(context());
777 VisitForControl(expr, test->true_label(), test->false_label(),
778 test->fall_through());
779 }
780}
781
782
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000783void FullCodeGenerator::VisitComma(BinaryOperation* expr) {
784 Comment cmnt(masm_, "[ Comma");
785 VisitForEffect(expr->left());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000786 VisitInDuplicateContext(expr->right());
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000787}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000788
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000789
790void FullCodeGenerator::VisitLogicalExpression(BinaryOperation* expr) {
791 bool is_logical_and = expr->op() == Token::AND;
792 Comment cmnt(masm_, is_logical_and ? "[ Logical AND" : "[ Logical OR");
793 Expression* left = expr->left();
794 Expression* right = expr->right();
795 int right_id = expr->RightId();
796 Label done;
797
798 if (context()->IsTest()) {
799 Label eval_right;
800 const TestContext* test = TestContext::cast(context());
801 if (is_logical_and) {
802 VisitForControl(left, &eval_right, test->false_label(), &eval_right);
803 } else {
804 VisitForControl(left, test->true_label(), &eval_right, &eval_right);
805 }
806 PrepareForBailoutForId(right_id, NO_REGISTERS);
807 __ bind(&eval_right);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000808
809 } else if (context()->IsAccumulatorValue()) {
810 VisitForAccumulatorValue(left);
811 // We want the value in the accumulator for the test, and on the stack in
812 // case we need it.
813 __ push(result_register());
814 Label discard, restore;
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000815 if (is_logical_and) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000816 DoTest(left, &discard, &restore, &restore);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000817 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000818 DoTest(left, &restore, &discard, &restore);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000819 }
820 __ bind(&restore);
821 __ pop(result_register());
822 __ jmp(&done);
823 __ bind(&discard);
824 __ Drop(1);
825 PrepareForBailoutForId(right_id, NO_REGISTERS);
826
827 } else if (context()->IsStackValue()) {
828 VisitForAccumulatorValue(left);
829 // We want the value in the accumulator for the test, and on the stack in
830 // case we need it.
831 __ push(result_register());
832 Label discard;
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000833 if (is_logical_and) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000834 DoTest(left, &discard, &done, &discard);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000835 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000836 DoTest(left, &done, &discard, &discard);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000837 }
838 __ bind(&discard);
839 __ Drop(1);
840 PrepareForBailoutForId(right_id, NO_REGISTERS);
841
842 } else {
843 ASSERT(context()->IsEffect());
844 Label eval_right;
845 if (is_logical_and) {
846 VisitForControl(left, &eval_right, &done, &eval_right);
847 } else {
848 VisitForControl(left, &done, &eval_right, &eval_right);
849 }
850 PrepareForBailoutForId(right_id, NO_REGISTERS);
851 __ bind(&eval_right);
852 }
853
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000854 VisitInDuplicateContext(right);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000855 __ bind(&done);
856}
857
858
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000859void FullCodeGenerator::VisitArithmeticExpression(BinaryOperation* expr) {
860 Token::Value op = expr->op();
861 Comment cmnt(masm_, "[ ArithmeticExpression");
862 Expression* left = expr->left();
863 Expression* right = expr->right();
864 OverwriteMode mode =
865 left->ResultOverwriteAllowed()
866 ? OVERWRITE_LEFT
867 : (right->ResultOverwriteAllowed() ? OVERWRITE_RIGHT : NO_OVERWRITE);
868
869 VisitForStackValue(left);
870 VisitForAccumulatorValue(right);
871
872 SetSourcePosition(expr->position());
873 if (ShouldInlineSmiCase(op)) {
874 EmitInlineSmiBinaryOp(expr, op, mode, left, right);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000875 } else {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000876 EmitBinaryOp(expr, op, mode);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000877 }
878}
879
880
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000881void FullCodeGenerator::VisitBlock(Block* stmt) {
882 Comment cmnt(masm_, "[ Block");
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000883 NestedBlock nested_block(this, stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000884 SetStatementPosition(stmt);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000885
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000886 Scope* saved_scope = scope();
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000887 // Push a block context when entering a block with block scoped variables.
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000888 if (stmt->block_scope() != NULL) {
889 { Comment cmnt(masm_, "[ Extend block context");
890 scope_ = stmt->block_scope();
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000891 Handle<ScopeInfo> scope_info = scope_->GetScopeInfo();
892 int heap_slots = scope_info->ContextLength() - Context::MIN_CONTEXT_SLOTS;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000893 __ Push(scope_info);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000894 PushFunctionArgumentForContextAllocation();
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000895 if (heap_slots <= FastNewBlockContextStub::kMaximumSlots) {
896 FastNewBlockContextStub stub(heap_slots);
897 __ CallStub(&stub);
898 } else {
899 __ CallRuntime(Runtime::kPushBlockContext, 2);
900 }
901
902 // Replace the context stored in the frame.
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000903 StoreToFrameField(StandardFrameConstants::kContextOffset,
904 context_register());
905 }
906 { Comment cmnt(masm_, "[ Declarations");
907 VisitDeclarations(scope_->declarations());
908 }
909 }
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +0000910 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000911 VisitStatements(stmt->statements());
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000912 scope_ = saved_scope;
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000913 __ bind(nested_block.break_label());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000914 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000915
916 // Pop block context if necessary.
917 if (stmt->block_scope() != NULL) {
918 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
919 // Update local stack frame context field.
920 StoreToFrameField(StandardFrameConstants::kContextOffset,
921 context_register());
922 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000923}
924
925
926void FullCodeGenerator::VisitExpressionStatement(ExpressionStatement* stmt) {
927 Comment cmnt(masm_, "[ ExpressionStatement");
928 SetStatementPosition(stmt);
929 VisitForEffect(stmt->expression());
930}
931
932
933void FullCodeGenerator::VisitEmptyStatement(EmptyStatement* stmt) {
934 Comment cmnt(masm_, "[ EmptyStatement");
935 SetStatementPosition(stmt);
936}
937
938
939void FullCodeGenerator::VisitIfStatement(IfStatement* stmt) {
940 Comment cmnt(masm_, "[ IfStatement");
941 SetStatementPosition(stmt);
942 Label then_part, else_part, done;
943
ricow@chromium.org65fae842010-08-25 15:26:24 +0000944 if (stmt->HasElseStatement()) {
945 VisitForControl(stmt->condition(), &then_part, &else_part, &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());
949 __ jmp(&done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000950
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000951 PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000952 __ bind(&else_part);
953 Visit(stmt->else_statement());
954 } else {
955 VisitForControl(stmt->condition(), &then_part, &done, &then_part);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000956 PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000957 __ bind(&then_part);
958 Visit(stmt->then_statement());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000959
960 PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000961 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000962 __ bind(&done);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000963 PrepareForBailoutForId(stmt->IfId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000964}
965
966
967void FullCodeGenerator::VisitContinueStatement(ContinueStatement* stmt) {
968 Comment cmnt(masm_, "[ ContinueStatement");
969 SetStatementPosition(stmt);
970 NestedStatement* current = nesting_stack_;
971 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000972 int context_length = 0;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000973 // When continuing, we clobber the unpredictable value in the accumulator
974 // with one that's safe for GC. If we hit an exit from the try block of
975 // try...finally on our way out, we will unconditionally preserve the
976 // accumulator on the stack.
977 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000978 while (!current->IsContinueTarget(stmt->target())) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000979 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000980 }
981 __ Drop(stack_depth);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000982 if (context_length > 0) {
983 while (context_length > 0) {
984 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
985 --context_length;
986 }
987 StoreToFrameField(StandardFrameConstants::kContextOffset,
988 context_register());
989 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000990
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000991 __ jmp(current->AsIteration()->continue_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000992}
993
994
995void FullCodeGenerator::VisitBreakStatement(BreakStatement* stmt) {
996 Comment cmnt(masm_, "[ BreakStatement");
997 SetStatementPosition(stmt);
998 NestedStatement* current = nesting_stack_;
999 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001000 int context_length = 0;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001001 // When breaking, we clobber the unpredictable value in the accumulator
1002 // with one that's safe for GC. If we hit an exit from the try block of
1003 // try...finally on our way out, we will unconditionally preserve the
1004 // accumulator on the stack.
1005 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001006 while (!current->IsBreakTarget(stmt->target())) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001007 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001008 }
1009 __ Drop(stack_depth);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001010 if (context_length > 0) {
1011 while (context_length > 0) {
1012 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1013 --context_length;
1014 }
1015 StoreToFrameField(StandardFrameConstants::kContextOffset,
1016 context_register());
1017 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001018
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001019 __ jmp(current->AsBreakable()->break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001020}
1021
1022
1023void FullCodeGenerator::VisitReturnStatement(ReturnStatement* stmt) {
1024 Comment cmnt(masm_, "[ ReturnStatement");
1025 SetStatementPosition(stmt);
1026 Expression* expr = stmt->expression();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001027 VisitForAccumulatorValue(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001028
1029 // Exit all nested statements.
1030 NestedStatement* current = nesting_stack_;
1031 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001032 int context_length = 0;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001033 while (current != NULL) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001034 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001035 }
1036 __ Drop(stack_depth);
1037
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001038 EmitReturnSequence();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001039}
1040
1041
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001042void FullCodeGenerator::VisitWithStatement(WithStatement* stmt) {
1043 Comment cmnt(masm_, "[ WithStatement");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001044 SetStatementPosition(stmt);
1045
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001046 VisitForStackValue(stmt->expression());
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00001047 PushFunctionArgumentForContextAllocation();
1048 __ CallRuntime(Runtime::kPushWithContext, 2);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001049 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001050
1051 { WithOrCatch body(this);
1052 Visit(stmt->statement());
1053 }
1054
1055 // Pop context.
1056 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1057 // Update local stack frame context field.
1058 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001059}
1060
1061
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001062void FullCodeGenerator::VisitDoWhileStatement(DoWhileStatement* stmt) {
1063 Comment cmnt(masm_, "[ DoWhileStatement");
1064 SetStatementPosition(stmt);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001065 Label body, stack_check;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001066
1067 Iteration loop_statement(this, stmt);
1068 increment_loop_depth();
1069
1070 __ bind(&body);
1071 Visit(stmt->body());
1072
ricow@chromium.org65fae842010-08-25 15:26:24 +00001073 // Record the position of the do while condition and make sure it is
1074 // possible to break on the condition.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001075 __ bind(loop_statement.continue_label());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001076 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001077 SetExpressionPosition(stmt->cond(), stmt->condition_position());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001078 VisitForControl(stmt->cond(),
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001079 &stack_check,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001080 loop_statement.break_label(),
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001081 &stack_check);
1082
1083 // Check stack before looping.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001084 PrepareForBailoutForId(stmt->BackEdgeId(), NO_REGISTERS);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001085 __ bind(&stack_check);
yangguo@chromium.org56454712012-02-16 15:33:53 +00001086 EmitStackCheck(stmt, &body);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001087 __ jmp(&body);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001088
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001089 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001090 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001091 decrement_loop_depth();
1092}
1093
1094
1095void FullCodeGenerator::VisitWhileStatement(WhileStatement* stmt) {
1096 Comment cmnt(masm_, "[ WhileStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001097 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001098
1099 Iteration loop_statement(this, stmt);
1100 increment_loop_depth();
1101
1102 // Emit the test at the bottom of the loop.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001103 __ jmp(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001104
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001105 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001106 __ bind(&body);
1107 Visit(stmt->body());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001108
1109 // Emit the statement position here as this is where the while
1110 // statement code starts.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001111 __ bind(loop_statement.continue_label());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001112 SetStatementPosition(stmt);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001113
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001114 // Check stack before looping.
yangguo@chromium.org56454712012-02-16 15:33:53 +00001115 EmitStackCheck(stmt, &body);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001116
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001117 __ bind(&test);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001118 VisitForControl(stmt->cond(),
1119 &body,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001120 loop_statement.break_label(),
1121 loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001122
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001123 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001124 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001125 decrement_loop_depth();
1126}
1127
1128
1129void FullCodeGenerator::VisitForStatement(ForStatement* stmt) {
1130 Comment cmnt(masm_, "[ ForStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001131 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001132
1133 Iteration loop_statement(this, stmt);
1134 if (stmt->init() != NULL) {
1135 Visit(stmt->init());
1136 }
1137
1138 increment_loop_depth();
1139 // Emit the test at the bottom of the loop (even if empty).
1140 __ jmp(&test);
1141
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001142 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001143 __ bind(&body);
1144 Visit(stmt->body());
1145
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001146 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001147 __ bind(loop_statement.continue_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001148 SetStatementPosition(stmt);
1149 if (stmt->next() != NULL) {
1150 Visit(stmt->next());
1151 }
1152
ricow@chromium.org65fae842010-08-25 15:26:24 +00001153 // Emit the statement position here as this is where the for
1154 // statement code starts.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001155 SetStatementPosition(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001156
1157 // Check stack before looping.
yangguo@chromium.org56454712012-02-16 15:33:53 +00001158 EmitStackCheck(stmt, &body);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001159
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001160 __ bind(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001161 if (stmt->cond() != NULL) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00001162 VisitForControl(stmt->cond(),
1163 &body,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001164 loop_statement.break_label(),
1165 loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001166 } else {
1167 __ jmp(&body);
1168 }
1169
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001170 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001171 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001172 decrement_loop_depth();
1173}
1174
1175
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001176void FullCodeGenerator::VisitTryCatchStatement(TryCatchStatement* stmt) {
1177 Comment cmnt(masm_, "[ TryCatchStatement");
1178 SetStatementPosition(stmt);
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001179 // The try block adds a handler to the exception handler chain before
1180 // entering, and removes it again when exiting normally. If an exception
1181 // is thrown during execution of the try block, the handler is consumed
1182 // and control is passed to the catch block with the exception in the
1183 // result register.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001184
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001185 Label try_entry, handler_entry, exit;
1186 __ jmp(&try_entry);
1187 __ bind(&handler_entry);
1188 handler_table()->set(stmt->index(), Smi::FromInt(handler_entry.pos()));
1189 // Exception handler code, the exception is in the result register.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001190 // Extend the context before executing the catch block.
1191 { Comment cmnt(masm_, "[ Extend catch context");
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001192 __ Push(stmt->variable()->name());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001193 __ push(result_register());
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00001194 PushFunctionArgumentForContextAllocation();
1195 __ CallRuntime(Runtime::kPushCatchContext, 3);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001196 StoreToFrameField(StandardFrameConstants::kContextOffset,
1197 context_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001198 }
1199
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001200 Scope* saved_scope = scope();
1201 scope_ = stmt->scope();
1202 ASSERT(scope_->declarations()->is_empty());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001203 { WithOrCatch catch_body(this);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001204 Visit(stmt->catch_block());
1205 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001206 // Restore the context.
1207 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1208 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001209 scope_ = saved_scope;
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001210 __ jmp(&exit);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001211
1212 // Try block code. Sets up the exception handler chain.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001213 __ bind(&try_entry);
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00001214 __ PushTryHandler(StackHandler::CATCH, stmt->index());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001215 { TryCatch try_body(this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001216 Visit(stmt->try_block());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001217 }
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001218 __ PopTryHandler();
1219 __ bind(&exit);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001220}
1221
1222
1223void FullCodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* stmt) {
1224 Comment cmnt(masm_, "[ TryFinallyStatement");
1225 SetStatementPosition(stmt);
1226 // Try finally is compiled by setting up a try-handler on the stack while
1227 // executing the try body, and removing it again afterwards.
1228 //
1229 // The try-finally construct can enter the finally block in three ways:
1230 // 1. By exiting the try-block normally. This removes the try-handler and
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001231 // calls the finally block code before continuing.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001232 // 2. By exiting the try-block with a function-local control flow transfer
1233 // (break/continue/return). The site of the, e.g., break removes the
1234 // try handler and calls the finally block code before continuing
1235 // its outward control transfer.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001236 // 3. By exiting the try-block with a thrown exception.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001237 // This can happen in nested function calls. It traverses the try-handler
1238 // chain and consumes the try-handler entry before jumping to the
1239 // handler code. The handler code then calls the finally-block before
1240 // rethrowing the exception.
1241 //
1242 // The finally block must assume a return address on top of the stack
1243 // (or in the link register on ARM chips) and a value (return value or
1244 // exception) in the result register (rax/eax/r0), both of which must
1245 // be preserved. The return address isn't GC-safe, so it should be
1246 // cooked before GC.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001247 Label try_entry, handler_entry, finally_entry;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001248
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001249 // Jump to try-handler setup and try-block code.
1250 __ jmp(&try_entry);
1251 __ bind(&handler_entry);
1252 handler_table()->set(stmt->index(), Smi::FromInt(handler_entry.pos()));
1253 // Exception handler code. This code is only executed when an exception
1254 // is thrown. The exception is in the result register, and must be
1255 // preserved by the finally block. Call the finally block and then
1256 // rethrow the exception if it returns.
1257 __ Call(&finally_entry);
1258 __ push(result_register());
1259 __ CallRuntime(Runtime::kReThrow, 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001260
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001261 // Finally block implementation.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001262 __ bind(&finally_entry);
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001263 EnterFinallyBlock();
1264 { Finally finally_body(this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001265 Visit(stmt->finally_block());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001266 }
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001267 ExitFinallyBlock(); // Return to the calling code.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001268
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001269 // Set up try handler.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001270 __ bind(&try_entry);
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00001271 __ PushTryHandler(StackHandler::FINALLY, stmt->index());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001272 { TryFinally try_body(this, &finally_entry);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001273 Visit(stmt->try_block());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001274 }
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001275 __ PopTryHandler();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001276 // Execute the finally block on the way out. Clobber the unpredictable
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001277 // value in the result register with one that's safe for GC because the
1278 // finally block will unconditionally preserve the result register on the
1279 // stack.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001280 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001281 __ Call(&finally_entry);
1282}
1283
1284
1285void FullCodeGenerator::VisitDebuggerStatement(DebuggerStatement* stmt) {
1286#ifdef ENABLE_DEBUGGER_SUPPORT
1287 Comment cmnt(masm_, "[ DebuggerStatement");
1288 SetStatementPosition(stmt);
1289
ager@chromium.org5c838252010-02-19 08:53:10 +00001290 __ DebugBreak();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001291 // Ignore the return value.
1292#endif
1293}
1294
1295
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001296void FullCodeGenerator::VisitConditional(Conditional* expr) {
1297 Comment cmnt(masm_, "[ Conditional");
1298 Label true_case, false_case, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001299 VisitForControl(expr->condition(), &true_case, &false_case, &true_case);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001300
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001301 PrepareForBailoutForId(expr->ThenId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001302 __ bind(&true_case);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001303 SetExpressionPosition(expr->then_expression(),
1304 expr->then_expression_position());
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001305 if (context()->IsTest()) {
1306 const TestContext* for_test = TestContext::cast(context());
1307 VisitForControl(expr->then_expression(),
1308 for_test->true_label(),
1309 for_test->false_label(),
1310 NULL);
1311 } else {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001312 VisitInDuplicateContext(expr->then_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001313 __ jmp(&done);
1314 }
1315
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001316 PrepareForBailoutForId(expr->ElseId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001317 __ bind(&false_case);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001318 SetExpressionPosition(expr->else_expression(),
1319 expr->else_expression_position());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001320 VisitInDuplicateContext(expr->else_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001321 // If control flow falls through Visit, merge it with true case here.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001322 if (!context()->IsTest()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001323 __ bind(&done);
1324 }
1325}
1326
1327
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001328void FullCodeGenerator::VisitLiteral(Literal* expr) {
1329 Comment cmnt(masm_, "[ Literal");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001330 context()->Plug(expr->handle());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001331}
1332
1333
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001334void FullCodeGenerator::VisitFunctionLiteral(FunctionLiteral* expr) {
1335 Comment cmnt(masm_, "[ FunctionLiteral");
1336
1337 // Build the function boilerplate and instantiate it.
1338 Handle<SharedFunctionInfo> function_info =
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001339 Compiler::BuildFunctionInfo(expr, script());
1340 if (function_info.is_null()) {
1341 SetStackOverflow();
1342 return;
1343 }
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001344 EmitNewClosure(function_info, expr->pretenure());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001345}
1346
1347
1348void FullCodeGenerator::VisitSharedFunctionInfoLiteral(
1349 SharedFunctionInfoLiteral* expr) {
1350 Comment cmnt(masm_, "[ SharedFunctionInfoLiteral");
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001351 EmitNewClosure(expr->shared_function_info(), false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001352}
1353
1354
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001355void FullCodeGenerator::VisitThrow(Throw* expr) {
1356 Comment cmnt(masm_, "[ Throw");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001357 VisitForStackValue(expr->exception());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001358 __ CallRuntime(Runtime::kThrow, 1);
1359 // Never returns here.
1360}
1361
1362
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001363FullCodeGenerator::NestedStatement* FullCodeGenerator::TryCatch::Exit(
1364 int* stack_depth,
1365 int* context_length) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001366 // The macros used here must preserve the result register.
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001367 __ Drop(*stack_depth);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001368 __ PopTryHandler();
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001369 *stack_depth = 0;
1370 return previous_;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001371}
1372
ricow@chromium.org65fae842010-08-25 15:26:24 +00001373
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001374bool FullCodeGenerator::TryLiteralCompare(CompareOperation* expr) {
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001375 Expression* sub_expr;
ager@chromium.org04921a82011-06-27 13:21:41 +00001376 Handle<String> check;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001377 if (expr->IsLiteralCompareTypeof(&sub_expr, &check)) {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001378 EmitLiteralCompareTypeof(expr, sub_expr, check);
ager@chromium.org04921a82011-06-27 13:21:41 +00001379 return true;
1380 }
1381
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001382 if (expr->IsLiteralCompareUndefined(&sub_expr)) {
1383 EmitLiteralCompareNil(expr, sub_expr, kUndefinedValue);
1384 return true;
1385 }
1386
1387 if (expr->IsLiteralCompareNull(&sub_expr)) {
1388 EmitLiteralCompareNil(expr, sub_expr, kNullValue);
ager@chromium.org04921a82011-06-27 13:21:41 +00001389 return true;
1390 }
1391
1392 return false;
1393}
1394
1395
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001396#undef __
1397
1398
1399} } // namespace v8::internal