blob: 7282be157bf8b250cbf03a79f21dfead809451fd [file] [log] [blame]
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00001// Copyright 2011 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
54void BreakableStatementChecker::VisitDeclaration(Declaration* decl) {
55}
56
57
58void BreakableStatementChecker::VisitBlock(Block* stmt) {
59}
60
61
62void BreakableStatementChecker::VisitExpressionStatement(
63 ExpressionStatement* stmt) {
64 // Check if expression is breakable.
65 Visit(stmt->expression());
66}
67
68
69void BreakableStatementChecker::VisitEmptyStatement(EmptyStatement* stmt) {
70}
71
72
73void BreakableStatementChecker::VisitIfStatement(IfStatement* stmt) {
74 // If the condition is breakable the if statement is breakable.
75 Visit(stmt->condition());
76}
77
78
79void BreakableStatementChecker::VisitContinueStatement(
80 ContinueStatement* stmt) {
81}
82
83
84void BreakableStatementChecker::VisitBreakStatement(BreakStatement* stmt) {
85}
86
87
88void BreakableStatementChecker::VisitReturnStatement(ReturnStatement* stmt) {
89 // Return is breakable if the expression is.
90 Visit(stmt->expression());
91}
92
93
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +000094void BreakableStatementChecker::VisitWithStatement(WithStatement* stmt) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +000095 Visit(stmt->expression());
96}
97
98
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +000099void BreakableStatementChecker::VisitSwitchStatement(SwitchStatement* stmt) {
100 // Switch statements breakable if the tag expression is.
101 Visit(stmt->tag());
102}
103
104
105void BreakableStatementChecker::VisitDoWhileStatement(DoWhileStatement* stmt) {
106 // Mark do while as breakable to avoid adding a break slot in front of it.
107 is_breakable_ = true;
108}
109
110
111void BreakableStatementChecker::VisitWhileStatement(WhileStatement* stmt) {
112 // Mark while statements breakable if the condition expression is.
113 Visit(stmt->cond());
114}
115
116
117void BreakableStatementChecker::VisitForStatement(ForStatement* stmt) {
118 // Mark for statements breakable if the condition expression is.
119 if (stmt->cond() != NULL) {
120 Visit(stmt->cond());
121 }
122}
123
124
125void BreakableStatementChecker::VisitForInStatement(ForInStatement* stmt) {
126 // Mark for in statements breakable if the enumerable expression is.
127 Visit(stmt->enumerable());
128}
129
130
131void BreakableStatementChecker::VisitTryCatchStatement(
132 TryCatchStatement* stmt) {
133 // Mark try catch as breakable to avoid adding a break slot in front of it.
134 is_breakable_ = true;
135}
136
137
138void BreakableStatementChecker::VisitTryFinallyStatement(
139 TryFinallyStatement* stmt) {
140 // Mark try finally as breakable to avoid adding a break slot in front of it.
141 is_breakable_ = true;
142}
143
144
145void BreakableStatementChecker::VisitDebuggerStatement(
146 DebuggerStatement* stmt) {
147 // The debugger statement is breakable.
148 is_breakable_ = true;
149}
150
151
152void BreakableStatementChecker::VisitFunctionLiteral(FunctionLiteral* expr) {
153}
154
155
156void BreakableStatementChecker::VisitSharedFunctionInfoLiteral(
157 SharedFunctionInfoLiteral* expr) {
158}
159
160
161void BreakableStatementChecker::VisitConditional(Conditional* expr) {
162}
163
164
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000165void BreakableStatementChecker::VisitVariableProxy(VariableProxy* expr) {
166}
167
168
169void BreakableStatementChecker::VisitLiteral(Literal* expr) {
170}
171
172
173void BreakableStatementChecker::VisitRegExpLiteral(RegExpLiteral* expr) {
174}
175
176
177void BreakableStatementChecker::VisitObjectLiteral(ObjectLiteral* expr) {
178}
179
180
181void BreakableStatementChecker::VisitArrayLiteral(ArrayLiteral* expr) {
182}
183
184
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000185void BreakableStatementChecker::VisitAssignment(Assignment* expr) {
186 // If assigning to a property (including a global property) the assignment is
187 // breakable.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000188 VariableProxy* proxy = expr->target()->AsVariableProxy();
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000189 Property* prop = expr->target()->AsProperty();
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000190 if (prop != NULL || (proxy != NULL && proxy->var()->IsUnallocated())) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000191 is_breakable_ = true;
192 return;
193 }
194
195 // Otherwise the assignment is breakable if the assigned value is.
196 Visit(expr->value());
197}
198
199
200void BreakableStatementChecker::VisitThrow(Throw* expr) {
201 // Throw is breakable if the expression is.
202 Visit(expr->exception());
203}
204
205
206void BreakableStatementChecker::VisitProperty(Property* expr) {
207 // Property load is breakable.
208 is_breakable_ = true;
209}
210
211
212void BreakableStatementChecker::VisitCall(Call* expr) {
213 // Function calls both through IC and call stub are breakable.
214 is_breakable_ = true;
215}
216
217
218void BreakableStatementChecker::VisitCallNew(CallNew* expr) {
219 // Function calls through new are breakable.
220 is_breakable_ = true;
221}
222
223
224void BreakableStatementChecker::VisitCallRuntime(CallRuntime* expr) {
225}
226
227
228void BreakableStatementChecker::VisitUnaryOperation(UnaryOperation* expr) {
229 Visit(expr->expression());
230}
231
232
233void BreakableStatementChecker::VisitCountOperation(CountOperation* expr) {
234 Visit(expr->expression());
235}
236
237
238void BreakableStatementChecker::VisitBinaryOperation(BinaryOperation* expr) {
239 Visit(expr->left());
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +0000240 if (expr->op() != Token::AND &&
241 expr->op() != Token::OR) {
242 Visit(expr->right());
243 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000244}
245
246
247void BreakableStatementChecker::VisitCompareOperation(CompareOperation* expr) {
248 Visit(expr->left());
249 Visit(expr->right());
250}
251
252
253void BreakableStatementChecker::VisitThisFunction(ThisFunction* expr) {
254}
255
256
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000257#define __ ACCESS_MASM(masm())
258
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000259bool FullCodeGenerator::MakeCode(CompilationInfo* info) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000260 Isolate* isolate = info->isolate();
ager@chromium.org5c838252010-02-19 08:53:10 +0000261 Handle<Script> script = info->script();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000262 if (!script->IsUndefined() && !script->source()->IsUndefined()) {
263 int len = String::cast(script->source())->length();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000264 isolate->counters()->total_full_codegen_source_size()->Increment(len);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000265 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000266 if (FLAG_trace_codegen) {
267 PrintF("Full Compiler - ");
268 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000269 CodeGenerator::MakeCodePrologue(info);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000270 const int kInitialBufferSize = 4 * KB;
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000271 MacroAssembler masm(info->isolate(), NULL, kInitialBufferSize);
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000272#ifdef ENABLE_GDB_JIT_INTERFACE
273 masm.positions_recorder()->StartGDBJITLineInfoRecording();
274#endif
ager@chromium.org5c838252010-02-19 08:53:10 +0000275
276 FullCodeGenerator cgen(&masm);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000277 cgen.Generate(info);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000278 if (cgen.HasStackOverflow()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000279 ASSERT(!isolate->has_pending_exception());
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000280 return false;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000281 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000282 unsigned table_offset = cgen.EmitStackCheckTable();
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000283
lrn@chromium.org34e60782011-09-15 07:25:40 +0000284 Code::Flags flags = Code::ComputeFlags(Code::FUNCTION);
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000285 Handle<Code> code = CodeGenerator::MakeCodeEpilogue(&masm, flags, info);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000286 code->set_optimizable(info->IsOptimizable());
287 cgen.PopulateDeoptimizationData(code);
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000288 cgen.PopulateTypeFeedbackCells(code);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000289 code->set_has_deoptimization_support(info->HasDeoptimizationSupport());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +0000290 code->set_handler_table(*cgen.handler_table());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000291#ifdef ENABLE_DEBUGGER_SUPPORT
lrn@chromium.org34e60782011-09-15 07:25:40 +0000292 code->set_has_debug_break_slots(
293 info->isolate()->debugger()->IsDebuggerActive());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000294 code->set_compiled_optimizable(info->IsOptimizable());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000295#endif // ENABLE_DEBUGGER_SUPPORT
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000296 code->set_allow_osr_at_loop_nesting_level(0);
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000297 code->set_stack_check_table_offset(table_offset);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000298 CodeGenerator::PrintCode(code, info);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000299 info->SetCode(code); // May be an empty handle.
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000300#ifdef ENABLE_GDB_JIT_INTERFACE
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000301 if (FLAG_gdbjit && !code.is_null()) {
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000302 GDBJITLineInfo* lineinfo =
303 masm.positions_recorder()->DetachGDBJITLineInfo();
304
305 GDBJIT(RegisterDetailedLineInfo(*code, lineinfo));
306 }
307#endif
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000308 return !code.is_null();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000309}
310
311
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000312unsigned FullCodeGenerator::EmitStackCheckTable() {
313 // The stack check table consists of a length (in number of entries)
314 // field, and then a sequence of entries. Each entry is a pair of AST id
315 // and code-relative pc offset.
316 masm()->Align(kIntSize);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000317 unsigned offset = masm()->pc_offset();
318 unsigned length = stack_checks_.length();
319 __ dd(length);
320 for (unsigned i = 0; i < length; ++i) {
321 __ dd(stack_checks_[i].id);
322 __ dd(stack_checks_[i].pc_and_state);
323 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000324 return offset;
325}
326
327
328void FullCodeGenerator::PopulateDeoptimizationData(Handle<Code> code) {
329 // Fill in the deoptimization information.
330 ASSERT(info_->HasDeoptimizationSupport() || bailout_entries_.is_empty());
331 if (!info_->HasDeoptimizationSupport()) return;
332 int length = bailout_entries_.length();
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000333 Handle<DeoptimizationOutputData> data = isolate()->factory()->
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000334 NewDeoptimizationOutputData(length, TENURED);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000335 for (int i = 0; i < length; i++) {
336 data->SetAstId(i, Smi::FromInt(bailout_entries_[i].id));
337 data->SetPcAndState(i, Smi::FromInt(bailout_entries_[i].pc_and_state));
338 }
339 code->set_deoptimization_data(*data);
340}
341
342
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000343void FullCodeGenerator::PopulateTypeFeedbackCells(Handle<Code> code) {
344 if (type_feedback_cells_.is_empty()) return;
345 int length = type_feedback_cells_.length();
346 int array_size = TypeFeedbackCells::LengthOfFixedArray(length);
347 Handle<TypeFeedbackCells> cache = Handle<TypeFeedbackCells>::cast(
348 isolate()->factory()->NewFixedArray(array_size, TENURED));
349 for (int i = 0; i < length; i++) {
350 cache->SetAstId(i, Smi::FromInt(type_feedback_cells_[i].ast_id));
351 cache->SetCell(i, *type_feedback_cells_[i].cell);
352 }
353 code->set_type_feedback_cells(*cache);
354}
355
356
357
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000358void FullCodeGenerator::PrepareForBailout(Expression* node, State state) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000359 PrepareForBailoutForId(node->id(), state);
360}
361
362
363void FullCodeGenerator::RecordJSReturnSite(Call* call) {
364 // We record the offset of the function return so we can rebuild the frame
365 // if the function was inlined, i.e., this is the return address in the
366 // inlined function's frame.
367 //
368 // The state is ignored. We defensively set it to TOS_REG, which is the
369 // real state of the unoptimized code at the return site.
370 PrepareForBailoutForId(call->ReturnId(), TOS_REG);
371#ifdef DEBUG
372 // In debug builds, mark the return so we can verify that this function
373 // was called.
374 ASSERT(!call->return_is_recorded_);
375 call->return_is_recorded_ = true;
376#endif
377}
378
379
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +0000380void FullCodeGenerator::PrepareForBailoutForId(unsigned id, State state) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000381 // There's no need to prepare this code for bailouts from already optimized
382 // code or code that can't be optimized.
383 if (!FLAG_deopt || !info_->HasDeoptimizationSupport()) return;
384 unsigned pc_and_state =
385 StateField::encode(state) | PcField::encode(masm_->pc_offset());
386 BailoutEntry entry = { id, pc_and_state };
387#ifdef DEBUG
yangguo@chromium.org659ceec2012-01-26 07:37:54 +0000388 if (FLAG_enable_slow_asserts) {
389 // Assert that we don't have multiple bailout entries for the same node.
390 for (int i = 0; i < bailout_entries_.length(); i++) {
391 if (bailout_entries_.at(i).id == entry.id) {
392 AstPrinter printer;
393 PrintF("%s", printer.PrintProgram(info_->function()));
394 UNREACHABLE();
395 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000396 }
397 }
398#endif // DEBUG
399 bailout_entries_.Add(entry);
400}
401
402
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000403void FullCodeGenerator::RecordTypeFeedbackCell(
404 unsigned id, Handle<JSGlobalPropertyCell> cell) {
405 TypeFeedbackCellEntry entry = { id, cell };
406 type_feedback_cells_.Add(entry);
407}
408
409
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +0000410void FullCodeGenerator::RecordStackCheck(unsigned ast_id) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000411 // The pc offset does not need to be encoded and packed together with a
412 // state.
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +0000413 ASSERT(masm_->pc_offset() > 0);
414 BailoutEntry entry = { ast_id, static_cast<unsigned>(masm_->pc_offset()) };
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000415 stack_checks_.Add(entry);
416}
417
418
ricow@chromium.org65fae842010-08-25 15:26:24 +0000419bool FullCodeGenerator::ShouldInlineSmiCase(Token::Value op) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000420 // Inline smi case inside loops, but not division and modulo which
421 // are too complicated and take up too much space.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000422 if (op == Token::DIV ||op == Token::MOD) return false;
423 if (FLAG_always_inline_smi_code) return true;
424 return loop_depth_ > 0;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000425}
426
427
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000428void FullCodeGenerator::EffectContext::Plug(Register reg) const {
429}
430
431
432void FullCodeGenerator::AccumulatorValueContext::Plug(Register reg) const {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000433 __ Move(result_register(), reg);
434}
435
436
437void FullCodeGenerator::StackValueContext::Plug(Register reg) const {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000438 __ push(reg);
439}
440
441
442void FullCodeGenerator::TestContext::Plug(Register reg) const {
443 // For simplicity we always test the accumulator register.
444 __ Move(result_register(), reg);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000445 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000446 codegen()->DoTest(this);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000447}
448
449
450void FullCodeGenerator::EffectContext::PlugTOS() const {
451 __ Drop(1);
452}
453
454
455void FullCodeGenerator::AccumulatorValueContext::PlugTOS() const {
456 __ pop(result_register());
457}
458
459
460void FullCodeGenerator::StackValueContext::PlugTOS() const {
461}
462
463
464void FullCodeGenerator::TestContext::PlugTOS() const {
465 // For simplicity we always test the accumulator register.
466 __ pop(result_register());
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::PrepareTest(
473 Label* materialize_true,
474 Label* materialize_false,
475 Label** if_true,
476 Label** if_false,
477 Label** fall_through) const {
478 // In an effect context, the true and the false case branch to the
479 // same label.
480 *if_true = *if_false = *fall_through = materialize_true;
481}
482
483
484void FullCodeGenerator::AccumulatorValueContext::PrepareTest(
485 Label* materialize_true,
486 Label* materialize_false,
487 Label** if_true,
488 Label** if_false,
489 Label** fall_through) const {
490 *if_true = *fall_through = materialize_true;
491 *if_false = materialize_false;
492}
493
494
495void FullCodeGenerator::StackValueContext::PrepareTest(
496 Label* materialize_true,
497 Label* materialize_false,
498 Label** if_true,
499 Label** if_false,
500 Label** fall_through) const {
501 *if_true = *fall_through = materialize_true;
502 *if_false = materialize_false;
503}
504
505
506void FullCodeGenerator::TestContext::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 = true_label_;
513 *if_false = false_label_;
514 *fall_through = fall_through_;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000515}
516
517
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000518void FullCodeGenerator::DoTest(const TestContext* context) {
519 DoTest(context->condition(),
520 context->true_label(),
521 context->false_label(),
522 context->fall_through());
523}
524
525
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000526void FullCodeGenerator::VisitDeclarations(
527 ZoneList<Declaration*>* declarations) {
528 int length = declarations->length();
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000529 int global_count = 0;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000530 for (int i = 0; i < length; i++) {
531 Declaration* decl = declarations->at(i);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000532 EmitDeclaration(decl->proxy(), decl->mode(), decl->fun(), &global_count);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000533 }
534
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000535 // Batch declare global functions and variables.
536 if (global_count > 0) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000537 Handle<FixedArray> array =
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000538 isolate()->factory()->NewFixedArray(2 * global_count, TENURED);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000539 for (int j = 0, i = 0; i < length; i++) {
540 Declaration* decl = declarations->at(i);
541 Variable* var = decl->proxy()->var();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000542
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000543 if (var->IsUnallocated()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000544 array->set(j++, *(var->name()));
545 if (decl->fun() == NULL) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000546 if (var->binding_needs_init()) {
547 // In case this binding needs initialization use the hole.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000548 array->set_the_hole(j++);
549 } else {
550 array->set_undefined(j++);
551 }
552 } else {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000553 Handle<SharedFunctionInfo> function =
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000554 Compiler::BuildFunctionInfo(decl->fun(), script());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000555 // Check for stack-overflow exception.
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000556 if (function.is_null()) {
557 SetStackOverflow();
558 return;
559 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000560 array->set(j++, *function);
561 }
562 }
563 }
564 // Invoke the platform-dependent code generator to do the actual
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000565 // declaration the global functions and variables.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000566 DeclareGlobals(array);
567 }
568}
569
570
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000571int FullCodeGenerator::DeclareGlobalsFlags() {
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000572 ASSERT(DeclareGlobalsLanguageMode::is_valid(language_mode()));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000573 return DeclareGlobalsEvalFlag::encode(is_eval()) |
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000574 DeclareGlobalsNativeFlag::encode(is_native()) |
575 DeclareGlobalsLanguageMode::encode(language_mode());
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000576}
577
578
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000579void FullCodeGenerator::SetFunctionPosition(FunctionLiteral* fun) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000580 CodeGenerator::RecordPositions(masm_, fun->start_position());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000581}
582
583
584void FullCodeGenerator::SetReturnPosition(FunctionLiteral* fun) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000585 CodeGenerator::RecordPositions(masm_, fun->end_position() - 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000586}
587
588
589void FullCodeGenerator::SetStatementPosition(Statement* stmt) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000590#ifdef ENABLE_DEBUGGER_SUPPORT
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000591 if (!isolate()->debugger()->IsDebuggerActive()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000592 CodeGenerator::RecordPositions(masm_, stmt->statement_pos());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000593 } else {
594 // Check if the statement will be breakable without adding a debug break
595 // slot.
596 BreakableStatementChecker checker;
597 checker.Check(stmt);
598 // Record the statement position right here if the statement is not
599 // breakable. For breakable statements the actual recording of the
600 // position will be postponed to the breakable code (typically an IC).
601 bool position_recorded = CodeGenerator::RecordPositions(
602 masm_, stmt->statement_pos(), !checker.is_breakable());
603 // If the position recording did record a new position generate a debug
604 // break slot to make the statement breakable.
605 if (position_recorded) {
606 Debug::GenerateSlot(masm_);
607 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000608 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000609#else
610 CodeGenerator::RecordPositions(masm_, stmt->statement_pos());
611#endif
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000612}
613
614
615void FullCodeGenerator::SetExpressionPosition(Expression* expr, int pos) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000616#ifdef ENABLE_DEBUGGER_SUPPORT
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000617 if (!isolate()->debugger()->IsDebuggerActive()) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000618 CodeGenerator::RecordPositions(masm_, pos);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000619 } else {
620 // Check if the expression will be breakable without adding a debug break
621 // slot.
622 BreakableStatementChecker checker;
623 checker.Check(expr);
624 // Record a statement position right here if the expression is not
625 // breakable. For breakable expressions the actual recording of the
626 // position will be postponed to the breakable code (typically an IC).
627 // NOTE this will record a statement position for something which might
628 // not be a statement. As stepping in the debugger will only stop at
629 // statement positions this is used for e.g. the condition expression of
630 // a do while loop.
631 bool position_recorded = CodeGenerator::RecordPositions(
632 masm_, pos, !checker.is_breakable());
633 // If the position recording did record a new position generate a debug
634 // break slot to make the statement breakable.
635 if (position_recorded) {
636 Debug::GenerateSlot(masm_);
637 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000638 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000639#else
640 CodeGenerator::RecordPositions(masm_, pos);
641#endif
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000642}
643
644
645void FullCodeGenerator::SetStatementPosition(int pos) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000646 CodeGenerator::RecordPositions(masm_, pos);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000647}
648
649
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000650void FullCodeGenerator::SetSourcePosition(int pos) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000651 if (pos != RelocInfo::kNoPosition) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000652 masm_->positions_recorder()->RecordPosition(pos);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000653 }
654}
655
656
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000657// Lookup table for code generators for special runtime calls which are
658// generated inline.
659#define INLINE_FUNCTION_GENERATOR_ADDRESS(Name, argc, ressize) \
660 &FullCodeGenerator::Emit##Name,
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000661
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000662const FullCodeGenerator::InlineFunctionGenerator
663 FullCodeGenerator::kInlineFunctionGenerators[] = {
664 INLINE_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS)
665 INLINE_RUNTIME_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS)
666 };
667#undef INLINE_FUNCTION_GENERATOR_ADDRESS
668
669
670FullCodeGenerator::InlineFunctionGenerator
671 FullCodeGenerator::FindInlineFunctionGenerator(Runtime::FunctionId id) {
whesse@chromium.org023421e2010-12-21 12:19:12 +0000672 int lookup_index =
673 static_cast<int>(id) - static_cast<int>(Runtime::kFirstInlineFunction);
674 ASSERT(lookup_index >= 0);
675 ASSERT(static_cast<size_t>(lookup_index) <
676 ARRAY_SIZE(kInlineFunctionGenerators));
677 return kInlineFunctionGenerators[lookup_index];
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000678}
679
680
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000681void FullCodeGenerator::EmitInlineRuntimeCall(CallRuntime* expr) {
682 const Runtime::Function* function = expr->function();
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000683 ASSERT(function != NULL);
684 ASSERT(function->intrinsic_type == Runtime::INLINE);
685 InlineFunctionGenerator generator =
686 FindInlineFunctionGenerator(function->function_id);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000687 ((*this).*(generator))(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000688}
689
690
691void FullCodeGenerator::VisitBinaryOperation(BinaryOperation* expr) {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000692 switch (expr->op()) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000693 case Token::COMMA:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000694 return VisitComma(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000695 case Token::OR:
696 case Token::AND:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000697 return VisitLogicalExpression(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000698 default:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000699 return VisitArithmeticExpression(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000700 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000701}
702
703
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000704void FullCodeGenerator::VisitInDuplicateContext(Expression* expr) {
705 if (context()->IsEffect()) {
706 VisitForEffect(expr);
707 } else if (context()->IsAccumulatorValue()) {
708 VisitForAccumulatorValue(expr);
709 } else if (context()->IsStackValue()) {
710 VisitForStackValue(expr);
711 } else if (context()->IsTest()) {
712 const TestContext* test = TestContext::cast(context());
713 VisitForControl(expr, test->true_label(), test->false_label(),
714 test->fall_through());
715 }
716}
717
718
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000719void FullCodeGenerator::VisitComma(BinaryOperation* expr) {
720 Comment cmnt(masm_, "[ Comma");
721 VisitForEffect(expr->left());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000722 VisitInDuplicateContext(expr->right());
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000723}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000724
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000725
726void FullCodeGenerator::VisitLogicalExpression(BinaryOperation* expr) {
727 bool is_logical_and = expr->op() == Token::AND;
728 Comment cmnt(masm_, is_logical_and ? "[ Logical AND" : "[ Logical OR");
729 Expression* left = expr->left();
730 Expression* right = expr->right();
731 int right_id = expr->RightId();
732 Label done;
733
734 if (context()->IsTest()) {
735 Label eval_right;
736 const TestContext* test = TestContext::cast(context());
737 if (is_logical_and) {
738 VisitForControl(left, &eval_right, test->false_label(), &eval_right);
739 } else {
740 VisitForControl(left, test->true_label(), &eval_right, &eval_right);
741 }
742 PrepareForBailoutForId(right_id, NO_REGISTERS);
743 __ bind(&eval_right);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000744
745 } else if (context()->IsAccumulatorValue()) {
746 VisitForAccumulatorValue(left);
747 // We want the value in the accumulator for the test, and on the stack in
748 // case we need it.
749 __ push(result_register());
750 Label discard, restore;
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000751 if (is_logical_and) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000752 DoTest(left, &discard, &restore, &restore);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000753 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000754 DoTest(left, &restore, &discard, &restore);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000755 }
756 __ bind(&restore);
757 __ pop(result_register());
758 __ jmp(&done);
759 __ bind(&discard);
760 __ Drop(1);
761 PrepareForBailoutForId(right_id, NO_REGISTERS);
762
763 } else if (context()->IsStackValue()) {
764 VisitForAccumulatorValue(left);
765 // We want the value in the accumulator for the test, and on the stack in
766 // case we need it.
767 __ push(result_register());
768 Label discard;
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000769 if (is_logical_and) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000770 DoTest(left, &discard, &done, &discard);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000771 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000772 DoTest(left, &done, &discard, &discard);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000773 }
774 __ bind(&discard);
775 __ Drop(1);
776 PrepareForBailoutForId(right_id, NO_REGISTERS);
777
778 } else {
779 ASSERT(context()->IsEffect());
780 Label eval_right;
781 if (is_logical_and) {
782 VisitForControl(left, &eval_right, &done, &eval_right);
783 } else {
784 VisitForControl(left, &done, &eval_right, &eval_right);
785 }
786 PrepareForBailoutForId(right_id, NO_REGISTERS);
787 __ bind(&eval_right);
788 }
789
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000790 VisitInDuplicateContext(right);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000791 __ bind(&done);
792}
793
794
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000795void FullCodeGenerator::VisitArithmeticExpression(BinaryOperation* expr) {
796 Token::Value op = expr->op();
797 Comment cmnt(masm_, "[ ArithmeticExpression");
798 Expression* left = expr->left();
799 Expression* right = expr->right();
800 OverwriteMode mode =
801 left->ResultOverwriteAllowed()
802 ? OVERWRITE_LEFT
803 : (right->ResultOverwriteAllowed() ? OVERWRITE_RIGHT : NO_OVERWRITE);
804
805 VisitForStackValue(left);
806 VisitForAccumulatorValue(right);
807
808 SetSourcePosition(expr->position());
809 if (ShouldInlineSmiCase(op)) {
810 EmitInlineSmiBinaryOp(expr, op, mode, left, right);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000811 } else {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000812 EmitBinaryOp(expr, op, mode);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000813 }
814}
815
816
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000817void FullCodeGenerator::VisitBlock(Block* stmt) {
818 Comment cmnt(masm_, "[ Block");
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000819 NestedBlock nested_block(this, stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000820 SetStatementPosition(stmt);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000821
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000822 Scope* saved_scope = scope();
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000823 // Push a block context when entering a block with block scoped variables.
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000824 if (stmt->block_scope() != NULL) {
825 { Comment cmnt(masm_, "[ Extend block context");
826 scope_ = stmt->block_scope();
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000827 Handle<ScopeInfo> scope_info = scope_->GetScopeInfo();
828 int heap_slots = scope_info->ContextLength() - Context::MIN_CONTEXT_SLOTS;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000829 __ Push(scope_info);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000830 PushFunctionArgumentForContextAllocation();
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000831 if (heap_slots <= FastNewBlockContextStub::kMaximumSlots) {
832 FastNewBlockContextStub stub(heap_slots);
833 __ CallStub(&stub);
834 } else {
835 __ CallRuntime(Runtime::kPushBlockContext, 2);
836 }
837
838 // Replace the context stored in the frame.
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000839 StoreToFrameField(StandardFrameConstants::kContextOffset,
840 context_register());
841 }
842 { Comment cmnt(masm_, "[ Declarations");
843 VisitDeclarations(scope_->declarations());
844 }
845 }
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +0000846 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000847 VisitStatements(stmt->statements());
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000848 scope_ = saved_scope;
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000849 __ bind(nested_block.break_label());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000850 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000851
852 // Pop block context if necessary.
853 if (stmt->block_scope() != NULL) {
854 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
855 // Update local stack frame context field.
856 StoreToFrameField(StandardFrameConstants::kContextOffset,
857 context_register());
858 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000859}
860
861
862void FullCodeGenerator::VisitExpressionStatement(ExpressionStatement* stmt) {
863 Comment cmnt(masm_, "[ ExpressionStatement");
864 SetStatementPosition(stmt);
865 VisitForEffect(stmt->expression());
866}
867
868
869void FullCodeGenerator::VisitEmptyStatement(EmptyStatement* stmt) {
870 Comment cmnt(masm_, "[ EmptyStatement");
871 SetStatementPosition(stmt);
872}
873
874
875void FullCodeGenerator::VisitIfStatement(IfStatement* stmt) {
876 Comment cmnt(masm_, "[ IfStatement");
877 SetStatementPosition(stmt);
878 Label then_part, else_part, done;
879
ricow@chromium.org65fae842010-08-25 15:26:24 +0000880 if (stmt->HasElseStatement()) {
881 VisitForControl(stmt->condition(), &then_part, &else_part, &then_part);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000882 PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000883 __ bind(&then_part);
884 Visit(stmt->then_statement());
885 __ jmp(&done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000886
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000887 PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000888 __ bind(&else_part);
889 Visit(stmt->else_statement());
890 } else {
891 VisitForControl(stmt->condition(), &then_part, &done, &then_part);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000892 PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000893 __ bind(&then_part);
894 Visit(stmt->then_statement());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000895
896 PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000897 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000898 __ bind(&done);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000899 PrepareForBailoutForId(stmt->IfId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000900}
901
902
903void FullCodeGenerator::VisitContinueStatement(ContinueStatement* stmt) {
904 Comment cmnt(masm_, "[ ContinueStatement");
905 SetStatementPosition(stmt);
906 NestedStatement* current = nesting_stack_;
907 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000908 int context_length = 0;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000909 // When continuing, we clobber the unpredictable value in the accumulator
910 // with one that's safe for GC. If we hit an exit from the try block of
911 // try...finally on our way out, we will unconditionally preserve the
912 // accumulator on the stack.
913 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000914 while (!current->IsContinueTarget(stmt->target())) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000915 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000916 }
917 __ Drop(stack_depth);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000918 if (context_length > 0) {
919 while (context_length > 0) {
920 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
921 --context_length;
922 }
923 StoreToFrameField(StandardFrameConstants::kContextOffset,
924 context_register());
925 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000926
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000927 __ jmp(current->AsIteration()->continue_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000928}
929
930
931void FullCodeGenerator::VisitBreakStatement(BreakStatement* stmt) {
932 Comment cmnt(masm_, "[ BreakStatement");
933 SetStatementPosition(stmt);
934 NestedStatement* current = nesting_stack_;
935 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000936 int context_length = 0;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000937 // When breaking, we clobber the unpredictable value in the accumulator
938 // with one that's safe for GC. If we hit an exit from the try block of
939 // try...finally on our way out, we will unconditionally preserve the
940 // accumulator on the stack.
941 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000942 while (!current->IsBreakTarget(stmt->target())) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000943 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000944 }
945 __ Drop(stack_depth);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000946 if (context_length > 0) {
947 while (context_length > 0) {
948 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
949 --context_length;
950 }
951 StoreToFrameField(StandardFrameConstants::kContextOffset,
952 context_register());
953 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000954
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000955 __ jmp(current->AsBreakable()->break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000956}
957
958
959void FullCodeGenerator::VisitReturnStatement(ReturnStatement* stmt) {
960 Comment cmnt(masm_, "[ ReturnStatement");
961 SetStatementPosition(stmt);
962 Expression* expr = stmt->expression();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000963 VisitForAccumulatorValue(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000964
965 // Exit all nested statements.
966 NestedStatement* current = nesting_stack_;
967 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000968 int context_length = 0;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000969 while (current != NULL) {
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);
973
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000974 EmitReturnSequence();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000975}
976
977
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000978void FullCodeGenerator::VisitWithStatement(WithStatement* stmt) {
979 Comment cmnt(masm_, "[ WithStatement");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000980 SetStatementPosition(stmt);
981
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000982 VisitForStackValue(stmt->expression());
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000983 PushFunctionArgumentForContextAllocation();
984 __ CallRuntime(Runtime::kPushWithContext, 2);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000985 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000986
987 { WithOrCatch body(this);
988 Visit(stmt->statement());
989 }
990
991 // Pop context.
992 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
993 // Update local stack frame context field.
994 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000995}
996
997
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000998void FullCodeGenerator::VisitDoWhileStatement(DoWhileStatement* stmt) {
999 Comment cmnt(masm_, "[ DoWhileStatement");
1000 SetStatementPosition(stmt);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001001 Label body, stack_check;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001002
1003 Iteration loop_statement(this, stmt);
1004 increment_loop_depth();
1005
1006 __ bind(&body);
1007 Visit(stmt->body());
1008
ricow@chromium.org65fae842010-08-25 15:26:24 +00001009 // Record the position of the do while condition and make sure it is
1010 // possible to break on the condition.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001011 __ bind(loop_statement.continue_label());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001012 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001013 SetExpressionPosition(stmt->cond(), stmt->condition_position());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001014 VisitForControl(stmt->cond(),
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001015 &stack_check,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001016 loop_statement.break_label(),
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001017 &stack_check);
1018
1019 // Check stack before looping.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001020 PrepareForBailoutForId(stmt->BackEdgeId(), NO_REGISTERS);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001021 __ bind(&stack_check);
1022 EmitStackCheck(stmt);
1023 __ jmp(&body);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001024
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001025 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001026 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001027 decrement_loop_depth();
1028}
1029
1030
1031void FullCodeGenerator::VisitWhileStatement(WhileStatement* stmt) {
1032 Comment cmnt(masm_, "[ WhileStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001033 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001034
1035 Iteration loop_statement(this, stmt);
1036 increment_loop_depth();
1037
1038 // Emit the test at the bottom of the loop.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001039 __ jmp(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001040
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001041 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001042 __ bind(&body);
1043 Visit(stmt->body());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001044
1045 // Emit the statement position here as this is where the while
1046 // statement code starts.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001047 __ bind(loop_statement.continue_label());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001048 SetStatementPosition(stmt);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001049
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001050 // Check stack before looping.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001051 EmitStackCheck(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001052
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001053 __ bind(&test);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001054 VisitForControl(stmt->cond(),
1055 &body,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001056 loop_statement.break_label(),
1057 loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001058
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001059 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001060 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001061 decrement_loop_depth();
1062}
1063
1064
1065void FullCodeGenerator::VisitForStatement(ForStatement* stmt) {
1066 Comment cmnt(masm_, "[ ForStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001067 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001068
1069 Iteration loop_statement(this, stmt);
1070 if (stmt->init() != NULL) {
1071 Visit(stmt->init());
1072 }
1073
1074 increment_loop_depth();
1075 // Emit the test at the bottom of the loop (even if empty).
1076 __ jmp(&test);
1077
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001078 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001079 __ bind(&body);
1080 Visit(stmt->body());
1081
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001082 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001083 __ bind(loop_statement.continue_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001084 SetStatementPosition(stmt);
1085 if (stmt->next() != NULL) {
1086 Visit(stmt->next());
1087 }
1088
ricow@chromium.org65fae842010-08-25 15:26:24 +00001089 // Emit the statement position here as this is where the for
1090 // statement code starts.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001091 SetStatementPosition(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001092
1093 // Check stack before looping.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001094 EmitStackCheck(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001095
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001096 __ bind(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001097 if (stmt->cond() != NULL) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00001098 VisitForControl(stmt->cond(),
1099 &body,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001100 loop_statement.break_label(),
1101 loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001102 } else {
1103 __ jmp(&body);
1104 }
1105
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001106 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001107 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001108 decrement_loop_depth();
1109}
1110
1111
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001112void FullCodeGenerator::VisitTryCatchStatement(TryCatchStatement* stmt) {
1113 Comment cmnt(masm_, "[ TryCatchStatement");
1114 SetStatementPosition(stmt);
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001115 // The try block adds a handler to the exception handler chain before
1116 // entering, and removes it again when exiting normally. If an exception
1117 // is thrown during execution of the try block, the handler is consumed
1118 // and control is passed to the catch block with the exception in the
1119 // result register.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001120
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001121 Label try_entry, handler_entry, exit;
1122 __ jmp(&try_entry);
1123 __ bind(&handler_entry);
1124 handler_table()->set(stmt->index(), Smi::FromInt(handler_entry.pos()));
1125 // Exception handler code, the exception is in the result register.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001126 // Extend the context before executing the catch block.
1127 { Comment cmnt(masm_, "[ Extend catch context");
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001128 __ Push(stmt->variable()->name());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001129 __ push(result_register());
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00001130 PushFunctionArgumentForContextAllocation();
1131 __ CallRuntime(Runtime::kPushCatchContext, 3);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001132 StoreToFrameField(StandardFrameConstants::kContextOffset,
1133 context_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001134 }
1135
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001136 Scope* saved_scope = scope();
1137 scope_ = stmt->scope();
1138 ASSERT(scope_->declarations()->is_empty());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001139 { WithOrCatch catch_body(this);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001140 Visit(stmt->catch_block());
1141 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001142 // Restore the context.
1143 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1144 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001145 scope_ = saved_scope;
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001146 __ jmp(&exit);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001147
1148 // Try block code. Sets up the exception handler chain.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001149 __ bind(&try_entry);
1150 __ PushTryHandler(IN_JAVASCRIPT, TRY_CATCH_HANDLER, stmt->index());
1151 { TryCatch try_body(this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001152 Visit(stmt->try_block());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001153 }
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001154 __ PopTryHandler();
1155 __ bind(&exit);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001156}
1157
1158
1159void FullCodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* stmt) {
1160 Comment cmnt(masm_, "[ TryFinallyStatement");
1161 SetStatementPosition(stmt);
1162 // Try finally is compiled by setting up a try-handler on the stack while
1163 // executing the try body, and removing it again afterwards.
1164 //
1165 // The try-finally construct can enter the finally block in three ways:
1166 // 1. By exiting the try-block normally. This removes the try-handler and
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001167 // calls the finally block code before continuing.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001168 // 2. By exiting the try-block with a function-local control flow transfer
1169 // (break/continue/return). The site of the, e.g., break removes the
1170 // try handler and calls the finally block code before continuing
1171 // its outward control transfer.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001172 // 3. By exiting the try-block with a thrown exception.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001173 // This can happen in nested function calls. It traverses the try-handler
1174 // chain and consumes the try-handler entry before jumping to the
1175 // handler code. The handler code then calls the finally-block before
1176 // rethrowing the exception.
1177 //
1178 // The finally block must assume a return address on top of the stack
1179 // (or in the link register on ARM chips) and a value (return value or
1180 // exception) in the result register (rax/eax/r0), both of which must
1181 // be preserved. The return address isn't GC-safe, so it should be
1182 // cooked before GC.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001183 Label try_entry, handler_entry, finally_entry;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001184
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001185 // Jump to try-handler setup and try-block code.
1186 __ jmp(&try_entry);
1187 __ bind(&handler_entry);
1188 handler_table()->set(stmt->index(), Smi::FromInt(handler_entry.pos()));
1189 // Exception handler code. This code is only executed when an exception
1190 // is thrown. The exception is in the result register, and must be
1191 // preserved by the finally block. Call the finally block and then
1192 // rethrow the exception if it returns.
1193 __ Call(&finally_entry);
1194 __ push(result_register());
1195 __ CallRuntime(Runtime::kReThrow, 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001196
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001197 // Finally block implementation.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001198 __ bind(&finally_entry);
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001199 EnterFinallyBlock();
1200 { Finally finally_body(this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001201 Visit(stmt->finally_block());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001202 }
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001203 ExitFinallyBlock(); // Return to the calling code.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001204
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001205 // Set up try handler.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001206 __ bind(&try_entry);
1207 __ PushTryHandler(IN_JAVASCRIPT, TRY_FINALLY_HANDLER, stmt->index());
1208 { TryFinally try_body(this, &finally_entry);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001209 Visit(stmt->try_block());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001210 }
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001211 __ PopTryHandler();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001212 // Execute the finally block on the way out. Clobber the unpredictable
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001213 // value in the result register with one that's safe for GC because the
1214 // finally block will unconditionally preserve the result register on the
1215 // stack.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001216 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001217 __ Call(&finally_entry);
1218}
1219
1220
1221void FullCodeGenerator::VisitDebuggerStatement(DebuggerStatement* stmt) {
1222#ifdef ENABLE_DEBUGGER_SUPPORT
1223 Comment cmnt(masm_, "[ DebuggerStatement");
1224 SetStatementPosition(stmt);
1225
ager@chromium.org5c838252010-02-19 08:53:10 +00001226 __ DebugBreak();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001227 // Ignore the return value.
1228#endif
1229}
1230
1231
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001232void FullCodeGenerator::VisitConditional(Conditional* expr) {
1233 Comment cmnt(masm_, "[ Conditional");
1234 Label true_case, false_case, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001235 VisitForControl(expr->condition(), &true_case, &false_case, &true_case);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001236
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001237 PrepareForBailoutForId(expr->ThenId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001238 __ bind(&true_case);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001239 SetExpressionPosition(expr->then_expression(),
1240 expr->then_expression_position());
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001241 if (context()->IsTest()) {
1242 const TestContext* for_test = TestContext::cast(context());
1243 VisitForControl(expr->then_expression(),
1244 for_test->true_label(),
1245 for_test->false_label(),
1246 NULL);
1247 } else {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001248 VisitInDuplicateContext(expr->then_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001249 __ jmp(&done);
1250 }
1251
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001252 PrepareForBailoutForId(expr->ElseId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001253 __ bind(&false_case);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001254 SetExpressionPosition(expr->else_expression(),
1255 expr->else_expression_position());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001256 VisitInDuplicateContext(expr->else_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001257 // If control flow falls through Visit, merge it with true case here.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001258 if (!context()->IsTest()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001259 __ bind(&done);
1260 }
1261}
1262
1263
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001264void FullCodeGenerator::VisitLiteral(Literal* expr) {
1265 Comment cmnt(masm_, "[ Literal");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001266 context()->Plug(expr->handle());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001267}
1268
1269
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001270void FullCodeGenerator::VisitFunctionLiteral(FunctionLiteral* expr) {
1271 Comment cmnt(masm_, "[ FunctionLiteral");
1272
1273 // Build the function boilerplate and instantiate it.
1274 Handle<SharedFunctionInfo> function_info =
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001275 Compiler::BuildFunctionInfo(expr, script());
1276 if (function_info.is_null()) {
1277 SetStackOverflow();
1278 return;
1279 }
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001280 EmitNewClosure(function_info, expr->pretenure());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001281}
1282
1283
1284void FullCodeGenerator::VisitSharedFunctionInfoLiteral(
1285 SharedFunctionInfoLiteral* expr) {
1286 Comment cmnt(masm_, "[ SharedFunctionInfoLiteral");
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001287 EmitNewClosure(expr->shared_function_info(), false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001288}
1289
1290
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001291void FullCodeGenerator::VisitThrow(Throw* expr) {
1292 Comment cmnt(masm_, "[ Throw");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001293 VisitForStackValue(expr->exception());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001294 __ CallRuntime(Runtime::kThrow, 1);
1295 // Never returns here.
1296}
1297
1298
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001299FullCodeGenerator::NestedStatement* FullCodeGenerator::TryCatch::Exit(
1300 int* stack_depth,
1301 int* context_length) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001302 // The macros used here must preserve the result register.
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001303 __ Drop(*stack_depth);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001304 __ PopTryHandler();
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001305 *stack_depth = 0;
1306 return previous_;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001307}
1308
ricow@chromium.org65fae842010-08-25 15:26:24 +00001309
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001310bool FullCodeGenerator::TryLiteralCompare(CompareOperation* expr) {
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001311 Expression* sub_expr;
ager@chromium.org04921a82011-06-27 13:21:41 +00001312 Handle<String> check;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001313 if (expr->IsLiteralCompareTypeof(&sub_expr, &check)) {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001314 EmitLiteralCompareTypeof(expr, sub_expr, check);
ager@chromium.org04921a82011-06-27 13:21:41 +00001315 return true;
1316 }
1317
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001318 if (expr->IsLiteralCompareUndefined(&sub_expr)) {
1319 EmitLiteralCompareNil(expr, sub_expr, kUndefinedValue);
1320 return true;
1321 }
1322
1323 if (expr->IsLiteralCompareNull(&sub_expr)) {
1324 EmitLiteralCompareNil(expr, sub_expr, kNullValue);
ager@chromium.org04921a82011-06-27 13:21:41 +00001325 return true;
1326 }
1327
1328 return false;
1329}
1330
1331
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001332#undef __
1333
1334
1335} } // namespace v8::internal