blob: fc7b6899bcbffb29127b009fe5ffceff22ff3908 [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
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +000099void BreakableStatementChecker::VisitExitContextStatement(
100 ExitContextStatement* stmt) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000101}
102
103
104void BreakableStatementChecker::VisitSwitchStatement(SwitchStatement* stmt) {
105 // Switch statements breakable if the tag expression is.
106 Visit(stmt->tag());
107}
108
109
110void BreakableStatementChecker::VisitDoWhileStatement(DoWhileStatement* stmt) {
111 // Mark do while as breakable to avoid adding a break slot in front of it.
112 is_breakable_ = true;
113}
114
115
116void BreakableStatementChecker::VisitWhileStatement(WhileStatement* stmt) {
117 // Mark while statements breakable if the condition expression is.
118 Visit(stmt->cond());
119}
120
121
122void BreakableStatementChecker::VisitForStatement(ForStatement* stmt) {
123 // Mark for statements breakable if the condition expression is.
124 if (stmt->cond() != NULL) {
125 Visit(stmt->cond());
126 }
127}
128
129
130void BreakableStatementChecker::VisitForInStatement(ForInStatement* stmt) {
131 // Mark for in statements breakable if the enumerable expression is.
132 Visit(stmt->enumerable());
133}
134
135
136void BreakableStatementChecker::VisitTryCatchStatement(
137 TryCatchStatement* stmt) {
138 // Mark try catch as breakable to avoid adding a break slot in front of it.
139 is_breakable_ = true;
140}
141
142
143void BreakableStatementChecker::VisitTryFinallyStatement(
144 TryFinallyStatement* stmt) {
145 // Mark try finally as breakable to avoid adding a break slot in front of it.
146 is_breakable_ = true;
147}
148
149
150void BreakableStatementChecker::VisitDebuggerStatement(
151 DebuggerStatement* stmt) {
152 // The debugger statement is breakable.
153 is_breakable_ = true;
154}
155
156
157void BreakableStatementChecker::VisitFunctionLiteral(FunctionLiteral* expr) {
158}
159
160
161void BreakableStatementChecker::VisitSharedFunctionInfoLiteral(
162 SharedFunctionInfoLiteral* expr) {
163}
164
165
166void BreakableStatementChecker::VisitConditional(Conditional* expr) {
167}
168
169
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000170void BreakableStatementChecker::VisitVariableProxy(VariableProxy* expr) {
171}
172
173
174void BreakableStatementChecker::VisitLiteral(Literal* expr) {
175}
176
177
178void BreakableStatementChecker::VisitRegExpLiteral(RegExpLiteral* expr) {
179}
180
181
182void BreakableStatementChecker::VisitObjectLiteral(ObjectLiteral* expr) {
183}
184
185
186void BreakableStatementChecker::VisitArrayLiteral(ArrayLiteral* expr) {
187}
188
189
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000190void BreakableStatementChecker::VisitAssignment(Assignment* expr) {
191 // If assigning to a property (including a global property) the assignment is
192 // breakable.
193 Variable* var = expr->target()->AsVariableProxy()->AsVariable();
194 Property* prop = expr->target()->AsProperty();
195 if (prop != NULL || (var != NULL && var->is_global())) {
196 is_breakable_ = true;
197 return;
198 }
199
200 // Otherwise the assignment is breakable if the assigned value is.
201 Visit(expr->value());
202}
203
204
205void BreakableStatementChecker::VisitThrow(Throw* expr) {
206 // Throw is breakable if the expression is.
207 Visit(expr->exception());
208}
209
210
211void BreakableStatementChecker::VisitProperty(Property* expr) {
212 // Property load is breakable.
213 is_breakable_ = true;
214}
215
216
217void BreakableStatementChecker::VisitCall(Call* expr) {
218 // Function calls both through IC and call stub are breakable.
219 is_breakable_ = true;
220}
221
222
223void BreakableStatementChecker::VisitCallNew(CallNew* expr) {
224 // Function calls through new are breakable.
225 is_breakable_ = true;
226}
227
228
229void BreakableStatementChecker::VisitCallRuntime(CallRuntime* expr) {
230}
231
232
233void BreakableStatementChecker::VisitUnaryOperation(UnaryOperation* expr) {
234 Visit(expr->expression());
235}
236
237
238void BreakableStatementChecker::VisitCountOperation(CountOperation* expr) {
239 Visit(expr->expression());
240}
241
242
243void BreakableStatementChecker::VisitBinaryOperation(BinaryOperation* expr) {
244 Visit(expr->left());
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +0000245 if (expr->op() != Token::AND &&
246 expr->op() != Token::OR) {
247 Visit(expr->right());
248 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000249}
250
251
ricow@chromium.org65fae842010-08-25 15:26:24 +0000252void BreakableStatementChecker::VisitCompareToNull(CompareToNull* expr) {
253 Visit(expr->expression());
254}
255
256
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000257void BreakableStatementChecker::VisitCompareOperation(CompareOperation* expr) {
258 Visit(expr->left());
259 Visit(expr->right());
260}
261
262
263void BreakableStatementChecker::VisitThisFunction(ThisFunction* expr) {
264}
265
266
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000267#define __ ACCESS_MASM(masm())
268
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000269bool FullCodeGenerator::MakeCode(CompilationInfo* info) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000270 Isolate* isolate = info->isolate();
ager@chromium.org5c838252010-02-19 08:53:10 +0000271 Handle<Script> script = info->script();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000272 if (!script->IsUndefined() && !script->source()->IsUndefined()) {
273 int len = String::cast(script->source())->length();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000274 isolate->counters()->total_full_codegen_source_size()->Increment(len);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000275 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000276 if (FLAG_trace_codegen) {
277 PrintF("Full Compiler - ");
278 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000279 CodeGenerator::MakeCodePrologue(info);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000280 const int kInitialBufferSize = 4 * KB;
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000281 MacroAssembler masm(info->isolate(), NULL, kInitialBufferSize);
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000282#ifdef ENABLE_GDB_JIT_INTERFACE
283 masm.positions_recorder()->StartGDBJITLineInfoRecording();
284#endif
ager@chromium.org5c838252010-02-19 08:53:10 +0000285
286 FullCodeGenerator cgen(&masm);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000287 cgen.Generate(info);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000288 if (cgen.HasStackOverflow()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000289 ASSERT(!isolate->has_pending_exception());
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000290 return false;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000291 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000292 unsigned table_offset = cgen.EmitStackCheckTable();
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000293
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000294 Code::Flags flags = Code::ComputeFlags(Code::FUNCTION, NOT_IN_LOOP);
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000295 Handle<Code> code = CodeGenerator::MakeCodeEpilogue(&masm, flags, info);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000296 code->set_optimizable(info->IsOptimizable());
297 cgen.PopulateDeoptimizationData(code);
298 code->set_has_deoptimization_support(info->HasDeoptimizationSupport());
299 code->set_allow_osr_at_loop_nesting_level(0);
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000300 code->set_stack_check_table_offset(table_offset);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000301 CodeGenerator::PrintCode(code, info);
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000302 info->SetCode(code); // may be an empty handle.
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000303#ifdef ENABLE_GDB_JIT_INTERFACE
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000304 if (FLAG_gdbjit && !code.is_null()) {
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000305 GDBJITLineInfo* lineinfo =
306 masm.positions_recorder()->DetachGDBJITLineInfo();
307
308 GDBJIT(RegisterDetailedLineInfo(*code, lineinfo));
309 }
310#endif
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000311 return !code.is_null();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000312}
313
314
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000315unsigned FullCodeGenerator::EmitStackCheckTable() {
316 // The stack check table consists of a length (in number of entries)
317 // field, and then a sequence of entries. Each entry is a pair of AST id
318 // and code-relative pc offset.
319 masm()->Align(kIntSize);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000320 unsigned offset = masm()->pc_offset();
321 unsigned length = stack_checks_.length();
322 __ dd(length);
323 for (unsigned i = 0; i < length; ++i) {
324 __ dd(stack_checks_[i].id);
325 __ dd(stack_checks_[i].pc_and_state);
326 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000327 return offset;
328}
329
330
331void FullCodeGenerator::PopulateDeoptimizationData(Handle<Code> code) {
332 // Fill in the deoptimization information.
333 ASSERT(info_->HasDeoptimizationSupport() || bailout_entries_.is_empty());
334 if (!info_->HasDeoptimizationSupport()) return;
335 int length = bailout_entries_.length();
336 Handle<DeoptimizationOutputData> data =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000337 isolate()->factory()->
338 NewDeoptimizationOutputData(length, TENURED);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000339 for (int i = 0; i < length; i++) {
340 data->SetAstId(i, Smi::FromInt(bailout_entries_[i].id));
341 data->SetPcAndState(i, Smi::FromInt(bailout_entries_[i].pc_and_state));
342 }
343 code->set_deoptimization_data(*data);
344}
345
346
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000347void FullCodeGenerator::PrepareForBailout(Expression* node, State state) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000348 PrepareForBailoutForId(node->id(), state);
349}
350
351
352void FullCodeGenerator::RecordJSReturnSite(Call* call) {
353 // We record the offset of the function return so we can rebuild the frame
354 // if the function was inlined, i.e., this is the return address in the
355 // inlined function's frame.
356 //
357 // The state is ignored. We defensively set it to TOS_REG, which is the
358 // real state of the unoptimized code at the return site.
359 PrepareForBailoutForId(call->ReturnId(), TOS_REG);
360#ifdef DEBUG
361 // In debug builds, mark the return so we can verify that this function
362 // was called.
363 ASSERT(!call->return_is_recorded_);
364 call->return_is_recorded_ = true;
365#endif
366}
367
368
369void FullCodeGenerator::PrepareForBailoutForId(int id, State state) {
370 // There's no need to prepare this code for bailouts from already optimized
371 // code or code that can't be optimized.
372 if (!FLAG_deopt || !info_->HasDeoptimizationSupport()) return;
373 unsigned pc_and_state =
374 StateField::encode(state) | PcField::encode(masm_->pc_offset());
375 BailoutEntry entry = { id, pc_and_state };
376#ifdef DEBUG
377 // Assert that we don't have multiple bailout entries for the same node.
378 for (int i = 0; i < bailout_entries_.length(); i++) {
379 if (bailout_entries_.at(i).id == entry.id) {
380 AstPrinter printer;
381 PrintF("%s", printer.PrintProgram(info_->function()));
382 UNREACHABLE();
383 }
384 }
385#endif // DEBUG
386 bailout_entries_.Add(entry);
387}
388
389
390void FullCodeGenerator::RecordStackCheck(int ast_id) {
391 // The pc offset does not need to be encoded and packed together with a
392 // state.
393 BailoutEntry entry = { ast_id, masm_->pc_offset() };
394 stack_checks_.Add(entry);
395}
396
397
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000398int FullCodeGenerator::SlotOffset(Slot* slot) {
399 ASSERT(slot != NULL);
400 // Offset is negative because higher indexes are at lower addresses.
401 int offset = -slot->index() * kPointerSize;
402 // Adjust by a (parameter or local) base offset.
403 switch (slot->type()) {
404 case Slot::PARAMETER:
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000405 offset += (info_->scope()->num_parameters() + 1) * kPointerSize;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000406 break;
407 case Slot::LOCAL:
408 offset += JavaScriptFrameConstants::kLocal0Offset;
409 break;
410 case Slot::CONTEXT:
411 case Slot::LOOKUP:
412 UNREACHABLE();
413 }
414 return offset;
415}
416
417
ricow@chromium.org65fae842010-08-25 15:26:24 +0000418bool FullCodeGenerator::ShouldInlineSmiCase(Token::Value op) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000419 // Inline smi case inside loops, but not division and modulo which
420 // are too complicated and take up too much space.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000421 if (op == Token::DIV ||op == Token::MOD) return false;
422 if (FLAG_always_inline_smi_code) return true;
423 return loop_depth_ > 0;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000424}
425
426
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000427void FullCodeGenerator::EffectContext::Plug(Register reg) const {
428}
429
430
431void FullCodeGenerator::AccumulatorValueContext::Plug(Register reg) const {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000432 __ Move(result_register(), reg);
433}
434
435
436void FullCodeGenerator::StackValueContext::Plug(Register reg) const {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000437 __ push(reg);
vegorov@chromium.org7943d462011-08-01 11:41:52 +0000438 codegen()->increment_stack_height();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000439}
440
441
442void FullCodeGenerator::TestContext::Plug(Register reg) const {
443 // For simplicity we always test the accumulator register.
444 __ Move(result_register(), reg);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000445 codegen()->PrepareForBailoutBeforeSplit(TOS_REG, 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);
vegorov@chromium.org7943d462011-08-01 11:41:52 +0000452 codegen()->decrement_stack_height();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000453}
454
455
456void FullCodeGenerator::AccumulatorValueContext::PlugTOS() const {
457 __ pop(result_register());
vegorov@chromium.org7943d462011-08-01 11:41:52 +0000458 codegen()->decrement_stack_height();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000459}
460
461
462void FullCodeGenerator::StackValueContext::PlugTOS() const {
463}
464
465
466void FullCodeGenerator::TestContext::PlugTOS() const {
467 // For simplicity we always test the accumulator register.
468 __ pop(result_register());
vegorov@chromium.org7943d462011-08-01 11:41:52 +0000469 codegen()->decrement_stack_height();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000470 codegen()->PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000471 codegen()->DoTest(this);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000472}
473
474
475void FullCodeGenerator::EffectContext::PrepareTest(
476 Label* materialize_true,
477 Label* materialize_false,
478 Label** if_true,
479 Label** if_false,
480 Label** fall_through) const {
481 // In an effect context, the true and the false case branch to the
482 // same label.
483 *if_true = *if_false = *fall_through = materialize_true;
484}
485
486
487void FullCodeGenerator::AccumulatorValueContext::PrepareTest(
488 Label* materialize_true,
489 Label* materialize_false,
490 Label** if_true,
491 Label** if_false,
492 Label** fall_through) const {
493 *if_true = *fall_through = materialize_true;
494 *if_false = materialize_false;
495}
496
497
498void FullCodeGenerator::StackValueContext::PrepareTest(
499 Label* materialize_true,
500 Label* materialize_false,
501 Label** if_true,
502 Label** if_false,
503 Label** fall_through) const {
504 *if_true = *fall_through = materialize_true;
505 *if_false = materialize_false;
506}
507
508
509void FullCodeGenerator::TestContext::PrepareTest(
510 Label* materialize_true,
511 Label* materialize_false,
512 Label** if_true,
513 Label** if_false,
514 Label** fall_through) const {
515 *if_true = true_label_;
516 *if_false = false_label_;
517 *fall_through = fall_through_;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000518}
519
520
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000521void FullCodeGenerator::DoTest(const TestContext* context) {
522 DoTest(context->condition(),
523 context->true_label(),
524 context->false_label(),
525 context->fall_through());
526}
527
528
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000529void FullCodeGenerator::VisitDeclarations(
530 ZoneList<Declaration*>* declarations) {
531 int length = declarations->length();
532 int globals = 0;
533 for (int i = 0; i < length; i++) {
534 Declaration* decl = declarations->at(i);
535 Variable* var = decl->proxy()->var();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000536 Slot* slot = var->AsSlot();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000537
538 // If it was not possible to allocate the variable at compile
539 // time, we need to "declare" it at runtime to make sure it
540 // actually exists in the local context.
541 if ((slot != NULL && slot->type() == Slot::LOOKUP) || !var->is_global()) {
542 VisitDeclaration(decl);
543 } else {
544 // Count global variables and functions for later processing
545 globals++;
546 }
547 }
548
549 // Compute array of global variable and function declarations.
550 // Do nothing in case of no declared global functions or variables.
551 if (globals > 0) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000552 Handle<FixedArray> array =
553 isolate()->factory()->NewFixedArray(2 * globals, TENURED);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000554 for (int j = 0, i = 0; i < length; i++) {
555 Declaration* decl = declarations->at(i);
556 Variable* var = decl->proxy()->var();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000557 Slot* slot = var->AsSlot();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000558
559 if ((slot == NULL || slot->type() != Slot::LOOKUP) && var->is_global()) {
560 array->set(j++, *(var->name()));
561 if (decl->fun() == NULL) {
562 if (var->mode() == Variable::CONST) {
563 // In case this is const property use the hole.
564 array->set_the_hole(j++);
565 } else {
566 array->set_undefined(j++);
567 }
568 } else {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000569 Handle<SharedFunctionInfo> function =
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000570 Compiler::BuildFunctionInfo(decl->fun(), script());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000571 // Check for stack-overflow exception.
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000572 if (function.is_null()) {
573 SetStackOverflow();
574 return;
575 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000576 array->set(j++, *function);
577 }
578 }
579 }
580 // Invoke the platform-dependent code generator to do the actual
581 // declaration the global variables and functions.
582 DeclareGlobals(array);
583 }
584}
585
586
587void FullCodeGenerator::SetFunctionPosition(FunctionLiteral* fun) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000588 CodeGenerator::RecordPositions(masm_, fun->start_position());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000589}
590
591
592void FullCodeGenerator::SetReturnPosition(FunctionLiteral* fun) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000593 CodeGenerator::RecordPositions(masm_, fun->end_position() - 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000594}
595
596
597void FullCodeGenerator::SetStatementPosition(Statement* stmt) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000598#ifdef ENABLE_DEBUGGER_SUPPORT
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000599 if (!isolate()->debugger()->IsDebuggerActive()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000600 CodeGenerator::RecordPositions(masm_, stmt->statement_pos());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000601 } else {
602 // Check if the statement will be breakable without adding a debug break
603 // slot.
604 BreakableStatementChecker checker;
605 checker.Check(stmt);
606 // Record the statement position right here if the statement is not
607 // breakable. For breakable statements the actual recording of the
608 // position will be postponed to the breakable code (typically an IC).
609 bool position_recorded = CodeGenerator::RecordPositions(
610 masm_, stmt->statement_pos(), !checker.is_breakable());
611 // If the position recording did record a new position generate a debug
612 // break slot to make the statement breakable.
613 if (position_recorded) {
614 Debug::GenerateSlot(masm_);
615 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000616 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000617#else
618 CodeGenerator::RecordPositions(masm_, stmt->statement_pos());
619#endif
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000620}
621
622
623void FullCodeGenerator::SetExpressionPosition(Expression* expr, int pos) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000624#ifdef ENABLE_DEBUGGER_SUPPORT
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000625 if (!isolate()->debugger()->IsDebuggerActive()) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000626 CodeGenerator::RecordPositions(masm_, pos);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000627 } else {
628 // Check if the expression will be breakable without adding a debug break
629 // slot.
630 BreakableStatementChecker checker;
631 checker.Check(expr);
632 // Record a statement position right here if the expression is not
633 // breakable. For breakable expressions the actual recording of the
634 // position will be postponed to the breakable code (typically an IC).
635 // NOTE this will record a statement position for something which might
636 // not be a statement. As stepping in the debugger will only stop at
637 // statement positions this is used for e.g. the condition expression of
638 // a do while loop.
639 bool position_recorded = CodeGenerator::RecordPositions(
640 masm_, pos, !checker.is_breakable());
641 // If the position recording did record a new position generate a debug
642 // break slot to make the statement breakable.
643 if (position_recorded) {
644 Debug::GenerateSlot(masm_);
645 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000646 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000647#else
648 CodeGenerator::RecordPositions(masm_, pos);
649#endif
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000650}
651
652
653void FullCodeGenerator::SetStatementPosition(int pos) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000654 CodeGenerator::RecordPositions(masm_, pos);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000655}
656
657
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000658void FullCodeGenerator::SetSourcePosition(int pos) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000659 if (pos != RelocInfo::kNoPosition) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000660 masm_->positions_recorder()->RecordPosition(pos);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000661 }
662}
663
664
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000665// Lookup table for code generators for special runtime calls which are
666// generated inline.
667#define INLINE_FUNCTION_GENERATOR_ADDRESS(Name, argc, ressize) \
668 &FullCodeGenerator::Emit##Name,
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000669
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000670const FullCodeGenerator::InlineFunctionGenerator
671 FullCodeGenerator::kInlineFunctionGenerators[] = {
672 INLINE_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS)
673 INLINE_RUNTIME_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS)
674 };
675#undef INLINE_FUNCTION_GENERATOR_ADDRESS
676
677
678FullCodeGenerator::InlineFunctionGenerator
679 FullCodeGenerator::FindInlineFunctionGenerator(Runtime::FunctionId id) {
whesse@chromium.org023421e2010-12-21 12:19:12 +0000680 int lookup_index =
681 static_cast<int>(id) - static_cast<int>(Runtime::kFirstInlineFunction);
682 ASSERT(lookup_index >= 0);
683 ASSERT(static_cast<size_t>(lookup_index) <
684 ARRAY_SIZE(kInlineFunctionGenerators));
685 return kInlineFunctionGenerators[lookup_index];
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000686}
687
688
689void FullCodeGenerator::EmitInlineRuntimeCall(CallRuntime* node) {
690 ZoneList<Expression*>* args = node->arguments();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000691 const Runtime::Function* function = node->function();
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000692 ASSERT(function != NULL);
693 ASSERT(function->intrinsic_type == Runtime::INLINE);
694 InlineFunctionGenerator generator =
695 FindInlineFunctionGenerator(function->function_id);
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000696 ((*this).*(generator))(args);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000697}
698
699
700void FullCodeGenerator::VisitBinaryOperation(BinaryOperation* expr) {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000701 switch (expr->op()) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000702 case Token::COMMA:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000703 return VisitComma(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000704 case Token::OR:
705 case Token::AND:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000706 return VisitLogicalExpression(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000707 default:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000708 return VisitArithmeticExpression(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000709 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000710}
711
712
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000713void FullCodeGenerator::VisitComma(BinaryOperation* expr) {
714 Comment cmnt(masm_, "[ Comma");
715 VisitForEffect(expr->left());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000716 if (context()->IsTest()) ForwardBailoutToChild(expr);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000717 VisitInCurrentContext(expr->right());
718}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000719
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000720
721void FullCodeGenerator::VisitLogicalExpression(BinaryOperation* expr) {
722 bool is_logical_and = expr->op() == Token::AND;
723 Comment cmnt(masm_, is_logical_and ? "[ Logical AND" : "[ Logical OR");
724 Expression* left = expr->left();
725 Expression* right = expr->right();
726 int right_id = expr->RightId();
727 Label done;
728
729 if (context()->IsTest()) {
730 Label eval_right;
731 const TestContext* test = TestContext::cast(context());
732 if (is_logical_and) {
733 VisitForControl(left, &eval_right, test->false_label(), &eval_right);
734 } else {
735 VisitForControl(left, test->true_label(), &eval_right, &eval_right);
736 }
737 PrepareForBailoutForId(right_id, NO_REGISTERS);
738 __ bind(&eval_right);
739 ForwardBailoutToChild(expr);
740
741 } else if (context()->IsAccumulatorValue()) {
742 VisitForAccumulatorValue(left);
743 // We want the value in the accumulator for the test, and on the stack in
744 // case we need it.
745 __ push(result_register());
746 Label discard, restore;
747 PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
748 if (is_logical_and) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000749 DoTest(left, &discard, &restore, &restore);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000750 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000751 DoTest(left, &restore, &discard, &restore);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000752 }
753 __ bind(&restore);
754 __ pop(result_register());
755 __ jmp(&done);
756 __ bind(&discard);
757 __ Drop(1);
758 PrepareForBailoutForId(right_id, NO_REGISTERS);
759
760 } else if (context()->IsStackValue()) {
761 VisitForAccumulatorValue(left);
762 // We want the value in the accumulator for the test, and on the stack in
763 // case we need it.
764 __ push(result_register());
765 Label discard;
766 PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
767 if (is_logical_and) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000768 DoTest(left, &discard, &done, &discard);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000769 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000770 DoTest(left, &done, &discard, &discard);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000771 }
772 __ bind(&discard);
773 __ Drop(1);
774 PrepareForBailoutForId(right_id, NO_REGISTERS);
775
776 } else {
777 ASSERT(context()->IsEffect());
778 Label eval_right;
779 if (is_logical_and) {
780 VisitForControl(left, &eval_right, &done, &eval_right);
781 } else {
782 VisitForControl(left, &done, &eval_right, &eval_right);
783 }
784 PrepareForBailoutForId(right_id, NO_REGISTERS);
785 __ bind(&eval_right);
786 }
787
788 VisitInCurrentContext(right);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000789 __ bind(&done);
790}
791
792
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000793void FullCodeGenerator::VisitArithmeticExpression(BinaryOperation* expr) {
794 Token::Value op = expr->op();
795 Comment cmnt(masm_, "[ ArithmeticExpression");
796 Expression* left = expr->left();
797 Expression* right = expr->right();
798 OverwriteMode mode =
799 left->ResultOverwriteAllowed()
800 ? OVERWRITE_LEFT
801 : (right->ResultOverwriteAllowed() ? OVERWRITE_RIGHT : NO_OVERWRITE);
802
803 VisitForStackValue(left);
804 VisitForAccumulatorValue(right);
805
806 SetSourcePosition(expr->position());
807 if (ShouldInlineSmiCase(op)) {
808 EmitInlineSmiBinaryOp(expr, op, mode, left, right);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000809 } else {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000810 EmitBinaryOp(expr, op, mode);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000811 }
812}
813
814
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000815void FullCodeGenerator::ForwardBailoutToChild(Expression* expr) {
816 if (!info_->HasDeoptimizationSupport()) return;
817 ASSERT(context()->IsTest());
818 ASSERT(expr == forward_bailout_stack_->expr());
819 forward_bailout_pending_ = forward_bailout_stack_;
820}
821
822
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000823void FullCodeGenerator::VisitInCurrentContext(Expression* expr) {
824 if (context()->IsTest()) {
825 ForwardBailoutStack stack(expr, forward_bailout_pending_);
826 ForwardBailoutStack* saved = forward_bailout_stack_;
827 forward_bailout_pending_ = NULL;
828 forward_bailout_stack_ = &stack;
829 Visit(expr);
830 forward_bailout_stack_ = saved;
831 } else {
832 ASSERT(forward_bailout_pending_ == NULL);
833 Visit(expr);
834 State state = context()->IsAccumulatorValue() ? TOS_REG : NO_REGISTERS;
835 PrepareForBailout(expr, state);
836 // Forwarding bailouts to children is a one shot operation. It should have
837 // been processed at this point.
838 ASSERT(forward_bailout_pending_ == NULL);
839 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000840}
841
842
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000843void FullCodeGenerator::VisitBlock(Block* stmt) {
844 Comment cmnt(masm_, "[ Block");
845 Breakable nested_statement(this, stmt);
846 SetStatementPosition(stmt);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000847
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000848 Scope* saved_scope = scope();
849 if (stmt->block_scope() != NULL) {
850 { Comment cmnt(masm_, "[ Extend block context");
851 scope_ = stmt->block_scope();
852 __ Push(scope_->GetSerializedScopeInfo());
853 PushFunctionArgumentForContextAllocation();
854 __ CallRuntime(Runtime::kPushBlockContext, 2);
855 StoreToFrameField(StandardFrameConstants::kContextOffset,
856 context_register());
857 }
858 { Comment cmnt(masm_, "[ Declarations");
859 VisitDeclarations(scope_->declarations());
860 }
861 }
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +0000862 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000863 VisitStatements(stmt->statements());
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000864 scope_ = saved_scope;
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000865 __ bind(nested_statement.break_label());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000866 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000867}
868
869
870void FullCodeGenerator::VisitExpressionStatement(ExpressionStatement* stmt) {
871 Comment cmnt(masm_, "[ ExpressionStatement");
872 SetStatementPosition(stmt);
873 VisitForEffect(stmt->expression());
874}
875
876
877void FullCodeGenerator::VisitEmptyStatement(EmptyStatement* stmt) {
878 Comment cmnt(masm_, "[ EmptyStatement");
879 SetStatementPosition(stmt);
880}
881
882
883void FullCodeGenerator::VisitIfStatement(IfStatement* stmt) {
884 Comment cmnt(masm_, "[ IfStatement");
885 SetStatementPosition(stmt);
886 Label then_part, else_part, done;
887
ricow@chromium.org65fae842010-08-25 15:26:24 +0000888 if (stmt->HasElseStatement()) {
889 VisitForControl(stmt->condition(), &then_part, &else_part, &then_part);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000890 PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000891 __ bind(&then_part);
892 Visit(stmt->then_statement());
893 __ jmp(&done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000894
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000895 PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000896 __ bind(&else_part);
897 Visit(stmt->else_statement());
898 } else {
899 VisitForControl(stmt->condition(), &then_part, &done, &then_part);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000900 PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000901 __ bind(&then_part);
902 Visit(stmt->then_statement());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000903
904 PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000905 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000906 __ bind(&done);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000907 PrepareForBailoutForId(stmt->IfId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000908}
909
910
911void FullCodeGenerator::VisitContinueStatement(ContinueStatement* stmt) {
912 Comment cmnt(masm_, "[ ContinueStatement");
913 SetStatementPosition(stmt);
914 NestedStatement* current = nesting_stack_;
915 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000916 int context_length = 0;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000917 // When continuing, we clobber the unpredictable value in the accumulator
918 // with one that's safe for GC. If we hit an exit from the try block of
919 // try...finally on our way out, we will unconditionally preserve the
920 // accumulator on the stack.
921 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000922 while (!current->IsContinueTarget(stmt->target())) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000923 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000924 }
925 __ Drop(stack_depth);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000926 if (context_length > 0) {
927 while (context_length > 0) {
928 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
929 --context_length;
930 }
931 StoreToFrameField(StandardFrameConstants::kContextOffset,
932 context_register());
933 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000934
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000935 __ jmp(current->AsIteration()->continue_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000936}
937
938
939void FullCodeGenerator::VisitBreakStatement(BreakStatement* stmt) {
940 Comment cmnt(masm_, "[ BreakStatement");
941 SetStatementPosition(stmt);
942 NestedStatement* current = nesting_stack_;
943 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000944 int context_length = 0;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000945 // When breaking, we clobber the unpredictable value in the accumulator
946 // with one that's safe for GC. If we hit an exit from the try block of
947 // try...finally on our way out, we will unconditionally preserve the
948 // accumulator on the stack.
949 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000950 while (!current->IsBreakTarget(stmt->target())) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000951 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000952 }
953 __ Drop(stack_depth);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000954 if (context_length > 0) {
955 while (context_length > 0) {
956 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
957 --context_length;
958 }
959 StoreToFrameField(StandardFrameConstants::kContextOffset,
960 context_register());
961 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000962
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000963 __ jmp(current->AsBreakable()->break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000964}
965
966
967void FullCodeGenerator::VisitReturnStatement(ReturnStatement* stmt) {
968 Comment cmnt(masm_, "[ ReturnStatement");
969 SetStatementPosition(stmt);
970 Expression* expr = stmt->expression();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000971 VisitForAccumulatorValue(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000972
973 // Exit all nested statements.
974 NestedStatement* current = nesting_stack_;
975 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000976 int context_length = 0;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000977 while (current != NULL) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000978 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000979 }
980 __ Drop(stack_depth);
981
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000982 EmitReturnSequence();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000983}
984
985
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000986void FullCodeGenerator::VisitWithStatement(WithStatement* stmt) {
987 Comment cmnt(masm_, "[ WithStatement");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000988 SetStatementPosition(stmt);
989
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000990 VisitForStackValue(stmt->expression());
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000991 PushFunctionArgumentForContextAllocation();
992 __ CallRuntime(Runtime::kPushWithContext, 2);
vegorov@chromium.org7943d462011-08-01 11:41:52 +0000993 decrement_stack_height();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000994 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000995
996 { WithOrCatch body(this);
997 Visit(stmt->statement());
998 }
999
1000 // Pop context.
1001 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1002 // Update local stack frame context field.
1003 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001004}
1005
1006
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001007void FullCodeGenerator::VisitExitContextStatement(ExitContextStatement* stmt) {
1008 Comment cmnt(masm_, "[ ExitContextStatement");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001009 SetStatementPosition(stmt);
1010
1011 // Pop context.
1012 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1013 // Update local stack frame context field.
1014 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
1015}
1016
1017
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001018void FullCodeGenerator::VisitDoWhileStatement(DoWhileStatement* stmt) {
1019 Comment cmnt(masm_, "[ DoWhileStatement");
1020 SetStatementPosition(stmt);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001021 Label body, stack_check;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001022
1023 Iteration loop_statement(this, stmt);
1024 increment_loop_depth();
1025
1026 __ bind(&body);
1027 Visit(stmt->body());
1028
ricow@chromium.org65fae842010-08-25 15:26:24 +00001029 // Record the position of the do while condition and make sure it is
1030 // possible to break on the condition.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001031 __ bind(loop_statement.continue_label());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001032 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001033 SetExpressionPosition(stmt->cond(), stmt->condition_position());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001034 VisitForControl(stmt->cond(),
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001035 &stack_check,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001036 loop_statement.break_label(),
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001037 &stack_check);
1038
1039 // Check stack before looping.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001040 PrepareForBailoutForId(stmt->BackEdgeId(), NO_REGISTERS);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001041 __ bind(&stack_check);
1042 EmitStackCheck(stmt);
1043 __ jmp(&body);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001044
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001045 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001046 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001047 decrement_loop_depth();
1048}
1049
1050
1051void FullCodeGenerator::VisitWhileStatement(WhileStatement* stmt) {
1052 Comment cmnt(masm_, "[ WhileStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001053 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001054
1055 Iteration loop_statement(this, stmt);
1056 increment_loop_depth();
1057
1058 // Emit the test at the bottom of the loop.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001059 __ jmp(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001060
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001061 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001062 __ bind(&body);
1063 Visit(stmt->body());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001064
1065 // Emit the statement position here as this is where the while
1066 // statement code starts.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001067 __ bind(loop_statement.continue_label());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001068 SetStatementPosition(stmt);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001069
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001070 // Check stack before looping.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001071 EmitStackCheck(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001072
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001073 __ bind(&test);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001074 VisitForControl(stmt->cond(),
1075 &body,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001076 loop_statement.break_label(),
1077 loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001078
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001079 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001080 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001081 decrement_loop_depth();
1082}
1083
1084
1085void FullCodeGenerator::VisitForStatement(ForStatement* stmt) {
1086 Comment cmnt(masm_, "[ ForStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001087 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001088
1089 Iteration loop_statement(this, stmt);
1090 if (stmt->init() != NULL) {
1091 Visit(stmt->init());
1092 }
1093
1094 increment_loop_depth();
1095 // Emit the test at the bottom of the loop (even if empty).
1096 __ jmp(&test);
1097
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001098 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001099 __ bind(&body);
1100 Visit(stmt->body());
1101
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001102 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001103 __ bind(loop_statement.continue_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001104 SetStatementPosition(stmt);
1105 if (stmt->next() != NULL) {
1106 Visit(stmt->next());
1107 }
1108
ricow@chromium.org65fae842010-08-25 15:26:24 +00001109 // Emit the statement position here as this is where the for
1110 // statement code starts.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001111 SetStatementPosition(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001112
1113 // Check stack before looping.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001114 EmitStackCheck(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001115
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001116 __ bind(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001117 if (stmt->cond() != NULL) {
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 } else {
1123 __ jmp(&body);
1124 }
1125
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001126 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001127 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001128 decrement_loop_depth();
1129}
1130
1131
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001132void FullCodeGenerator::VisitTryCatchStatement(TryCatchStatement* stmt) {
1133 Comment cmnt(masm_, "[ TryCatchStatement");
1134 SetStatementPosition(stmt);
1135 // The try block adds a handler to the exception handler chain
1136 // before entering, and removes it again when exiting normally.
1137 // If an exception is thrown during execution of the try block,
1138 // control is passed to the handler, which also consumes the handler.
1139 // At this point, the exception is in a register, and store it in
1140 // the temporary local variable (prints as ".catch-var") before
1141 // executing the catch block. The catch block has been rewritten
1142 // to introduce a new scope to bind the catch variable and to remove
1143 // that scope again afterwards.
1144
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001145 Label try_handler_setup, done;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001146 __ Call(&try_handler_setup);
1147 // Try handler code, exception in result register.
1148
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001149 // Extend the context before executing the catch block.
1150 { Comment cmnt(masm_, "[ Extend catch context");
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001151 __ Push(stmt->variable()->name());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001152 __ push(result_register());
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00001153 PushFunctionArgumentForContextAllocation();
1154 __ CallRuntime(Runtime::kPushCatchContext, 3);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001155 StoreToFrameField(StandardFrameConstants::kContextOffset,
1156 context_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001157 }
1158
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001159 Scope* saved_scope = scope();
1160 scope_ = stmt->scope();
1161 ASSERT(scope_->declarations()->is_empty());
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001162 { WithOrCatch body(this);
1163 Visit(stmt->catch_block());
1164 }
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001165 scope_ = saved_scope;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001166 __ jmp(&done);
1167
1168 // Try block code. Sets up the exception handler chain.
1169 __ bind(&try_handler_setup);
1170 {
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001171 const int delta = StackHandlerConstants::kSize / kPointerSize;
1172 TryCatch try_block(this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001173 __ PushTryHandler(IN_JAVASCRIPT, TRY_CATCH_HANDLER);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001174 increment_stack_height(delta);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001175 Visit(stmt->try_block());
1176 __ PopTryHandler();
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001177 decrement_stack_height(delta);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001178 }
1179 __ bind(&done);
1180}
1181
1182
1183void FullCodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* stmt) {
1184 Comment cmnt(masm_, "[ TryFinallyStatement");
1185 SetStatementPosition(stmt);
1186 // Try finally is compiled by setting up a try-handler on the stack while
1187 // executing the try body, and removing it again afterwards.
1188 //
1189 // The try-finally construct can enter the finally block in three ways:
1190 // 1. By exiting the try-block normally. This removes the try-handler and
1191 // calls the finally block code before continuing.
1192 // 2. By exiting the try-block with a function-local control flow transfer
1193 // (break/continue/return). The site of the, e.g., break removes the
1194 // try handler and calls the finally block code before continuing
1195 // its outward control transfer.
1196 // 3. by exiting the try-block with a thrown exception.
1197 // This can happen in nested function calls. It traverses the try-handler
1198 // chain and consumes the try-handler entry before jumping to the
1199 // handler code. The handler code then calls the finally-block before
1200 // rethrowing the exception.
1201 //
1202 // The finally block must assume a return address on top of the stack
1203 // (or in the link register on ARM chips) and a value (return value or
1204 // exception) in the result register (rax/eax/r0), both of which must
1205 // be preserved. The return address isn't GC-safe, so it should be
1206 // cooked before GC.
1207 Label finally_entry;
1208 Label try_handler_setup;
vegorov@chromium.org7943d462011-08-01 11:41:52 +00001209 const int original_stack_height = stack_height();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001210
1211 // Setup the try-handler chain. Use a call to
1212 // Jump to try-handler setup and try-block code. Use call to put try-handler
1213 // address on stack.
1214 __ Call(&try_handler_setup);
1215 // Try handler code. Return address of call is pushed on handler stack.
1216 {
1217 // This code is only executed during stack-handler traversal when an
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001218 // exception is thrown. The exception is in the result register, which
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001219 // is retained by the finally block.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001220 // Call the finally block and then rethrow the exception if it returns.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001221 __ Call(&finally_entry);
1222 __ push(result_register());
1223 __ CallRuntime(Runtime::kReThrow, 1);
1224 }
1225
1226 __ bind(&finally_entry);
1227 {
1228 // Finally block implementation.
1229 Finally finally_block(this);
1230 EnterFinallyBlock();
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001231 set_stack_height(original_stack_height + Finally::kElementCount);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001232 Visit(stmt->finally_block());
1233 ExitFinallyBlock(); // Return to the calling code.
1234 }
1235
1236 __ bind(&try_handler_setup);
1237 {
1238 // Setup try handler (stack pointer registers).
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001239 const int delta = StackHandlerConstants::kSize / kPointerSize;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001240 TryFinally try_block(this, &finally_entry);
1241 __ PushTryHandler(IN_JAVASCRIPT, TRY_FINALLY_HANDLER);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001242 set_stack_height(original_stack_height + delta);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001243 Visit(stmt->try_block());
1244 __ PopTryHandler();
vegorov@chromium.org7943d462011-08-01 11:41:52 +00001245 set_stack_height(original_stack_height);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001246 }
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001247 // Execute the finally block on the way out. Clobber the unpredictable
1248 // value in the accumulator with one that's safe for GC. The finally
1249 // block will unconditionally preserve the accumulator on the stack.
1250 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001251 __ Call(&finally_entry);
1252}
1253
1254
1255void FullCodeGenerator::VisitDebuggerStatement(DebuggerStatement* stmt) {
1256#ifdef ENABLE_DEBUGGER_SUPPORT
1257 Comment cmnt(masm_, "[ DebuggerStatement");
1258 SetStatementPosition(stmt);
1259
ager@chromium.org5c838252010-02-19 08:53:10 +00001260 __ DebugBreak();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001261 // Ignore the return value.
1262#endif
1263}
1264
1265
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001266void FullCodeGenerator::VisitConditional(Conditional* expr) {
1267 Comment cmnt(masm_, "[ Conditional");
1268 Label true_case, false_case, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001269 VisitForControl(expr->condition(), &true_case, &false_case, &true_case);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001270
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001271 PrepareForBailoutForId(expr->ThenId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001272 __ bind(&true_case);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001273 SetExpressionPosition(expr->then_expression(),
1274 expr->then_expression_position());
vegorov@chromium.org7943d462011-08-01 11:41:52 +00001275 int start_stack_height = stack_height();
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001276 if (context()->IsTest()) {
1277 const TestContext* for_test = TestContext::cast(context());
1278 VisitForControl(expr->then_expression(),
1279 for_test->true_label(),
1280 for_test->false_label(),
1281 NULL);
1282 } else {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001283 VisitInCurrentContext(expr->then_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001284 __ jmp(&done);
1285 }
1286
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001287 PrepareForBailoutForId(expr->ElseId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001288 __ bind(&false_case);
vegorov@chromium.org7943d462011-08-01 11:41:52 +00001289 set_stack_height(start_stack_height);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001290 if (context()->IsTest()) ForwardBailoutToChild(expr);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001291 SetExpressionPosition(expr->else_expression(),
1292 expr->else_expression_position());
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001293 VisitInCurrentContext(expr->else_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001294 // If control flow falls through Visit, merge it with true case here.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001295 if (!context()->IsTest()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001296 __ bind(&done);
1297 }
1298}
1299
1300
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001301void FullCodeGenerator::VisitLiteral(Literal* expr) {
1302 Comment cmnt(masm_, "[ Literal");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001303 context()->Plug(expr->handle());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001304}
1305
1306
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001307void FullCodeGenerator::VisitFunctionLiteral(FunctionLiteral* expr) {
1308 Comment cmnt(masm_, "[ FunctionLiteral");
1309
1310 // Build the function boilerplate and instantiate it.
1311 Handle<SharedFunctionInfo> function_info =
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001312 Compiler::BuildFunctionInfo(expr, script());
1313 if (function_info.is_null()) {
1314 SetStackOverflow();
1315 return;
1316 }
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001317 EmitNewClosure(function_info, expr->pretenure());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001318}
1319
1320
1321void FullCodeGenerator::VisitSharedFunctionInfoLiteral(
1322 SharedFunctionInfoLiteral* expr) {
1323 Comment cmnt(masm_, "[ SharedFunctionInfoLiteral");
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001324 EmitNewClosure(expr->shared_function_info(), false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001325}
1326
1327
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001328void FullCodeGenerator::VisitThrow(Throw* expr) {
1329 Comment cmnt(masm_, "[ Throw");
vegorov@chromium.org7943d462011-08-01 11:41:52 +00001330 // Throw has no effect on the stack height or the current expression context.
1331 // Usually the expression context is null, because throw is a statement.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001332 VisitForStackValue(expr->exception());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001333 __ CallRuntime(Runtime::kThrow, 1);
vegorov@chromium.org7943d462011-08-01 11:41:52 +00001334 decrement_stack_height();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001335 // Never returns here.
1336}
1337
1338
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001339FullCodeGenerator::NestedStatement* FullCodeGenerator::TryFinally::Exit(
1340 int* stack_depth,
1341 int* context_length) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001342 // The macros used here must preserve the result register.
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001343 __ Drop(*stack_depth);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001344 __ PopTryHandler();
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001345 *stack_depth = 0;
1346
1347 Register context = FullCodeGenerator::context_register();
1348 while (*context_length > 0) {
1349 codegen_->LoadContextField(context, Context::PREVIOUS_INDEX);
1350 --(*context_length);
1351 }
1352
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001353 __ Call(finally_entry_);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001354 return previous_;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001355}
1356
1357
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001358FullCodeGenerator::NestedStatement* FullCodeGenerator::TryCatch::Exit(
1359 int* stack_depth,
1360 int* context_length) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001361 // The macros used here must preserve the result register.
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001362 __ Drop(*stack_depth);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001363 __ PopTryHandler();
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001364 *stack_depth = 0;
1365 return previous_;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001366}
1367
ricow@chromium.org65fae842010-08-25 15:26:24 +00001368
ager@chromium.org04921a82011-06-27 13:21:41 +00001369bool FullCodeGenerator::TryLiteralCompare(CompareOperation* compare,
1370 Label* if_true,
1371 Label* if_false,
1372 Label* fall_through) {
1373 Expression *expr;
1374 Handle<String> check;
1375 if (compare->IsLiteralCompareTypeof(&expr, &check)) {
1376 EmitLiteralCompareTypeof(expr, check, if_true, if_false, fall_through);
1377 return true;
1378 }
1379
1380 if (compare->IsLiteralCompareUndefined(&expr)) {
1381 EmitLiteralCompareUndefined(expr, if_true, if_false, fall_through);
1382 return true;
1383 }
1384
1385 return false;
1386}
1387
1388
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001389#undef __
1390
1391
1392} } // namespace v8::internal