blob: 0f44cf233ea9efc1497ff78c151ae08bf6b82ede [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
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000587int FullCodeGenerator::DeclareGlobalsFlags() {
588 int flags = 0;
589 if (is_eval()) flags |= kDeclareGlobalsEvalFlag;
590 if (is_strict_mode()) flags |= kDeclareGlobalsStrictModeFlag;
591 if (is_native()) flags |= kDeclareGlobalsNativeFlag;
592 return flags;
593}
594
595
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000596void FullCodeGenerator::SetFunctionPosition(FunctionLiteral* fun) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000597 CodeGenerator::RecordPositions(masm_, fun->start_position());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000598}
599
600
601void FullCodeGenerator::SetReturnPosition(FunctionLiteral* fun) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000602 CodeGenerator::RecordPositions(masm_, fun->end_position() - 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000603}
604
605
606void FullCodeGenerator::SetStatementPosition(Statement* stmt) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000607#ifdef ENABLE_DEBUGGER_SUPPORT
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000608 if (!isolate()->debugger()->IsDebuggerActive()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000609 CodeGenerator::RecordPositions(masm_, stmt->statement_pos());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000610 } else {
611 // Check if the statement will be breakable without adding a debug break
612 // slot.
613 BreakableStatementChecker checker;
614 checker.Check(stmt);
615 // Record the statement position right here if the statement is not
616 // breakable. For breakable statements the actual recording of the
617 // position will be postponed to the breakable code (typically an IC).
618 bool position_recorded = CodeGenerator::RecordPositions(
619 masm_, stmt->statement_pos(), !checker.is_breakable());
620 // If the position recording did record a new position generate a debug
621 // break slot to make the statement breakable.
622 if (position_recorded) {
623 Debug::GenerateSlot(masm_);
624 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000625 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000626#else
627 CodeGenerator::RecordPositions(masm_, stmt->statement_pos());
628#endif
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000629}
630
631
632void FullCodeGenerator::SetExpressionPosition(Expression* expr, int pos) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000633#ifdef ENABLE_DEBUGGER_SUPPORT
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000634 if (!isolate()->debugger()->IsDebuggerActive()) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000635 CodeGenerator::RecordPositions(masm_, pos);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000636 } else {
637 // Check if the expression will be breakable without adding a debug break
638 // slot.
639 BreakableStatementChecker checker;
640 checker.Check(expr);
641 // Record a statement position right here if the expression is not
642 // breakable. For breakable expressions the actual recording of the
643 // position will be postponed to the breakable code (typically an IC).
644 // NOTE this will record a statement position for something which might
645 // not be a statement. As stepping in the debugger will only stop at
646 // statement positions this is used for e.g. the condition expression of
647 // a do while loop.
648 bool position_recorded = CodeGenerator::RecordPositions(
649 masm_, pos, !checker.is_breakable());
650 // If the position recording did record a new position generate a debug
651 // break slot to make the statement breakable.
652 if (position_recorded) {
653 Debug::GenerateSlot(masm_);
654 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000655 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000656#else
657 CodeGenerator::RecordPositions(masm_, pos);
658#endif
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000659}
660
661
662void FullCodeGenerator::SetStatementPosition(int pos) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000663 CodeGenerator::RecordPositions(masm_, pos);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000664}
665
666
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000667void FullCodeGenerator::SetSourcePosition(int pos) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000668 if (pos != RelocInfo::kNoPosition) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000669 masm_->positions_recorder()->RecordPosition(pos);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000670 }
671}
672
673
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000674// Lookup table for code generators for special runtime calls which are
675// generated inline.
676#define INLINE_FUNCTION_GENERATOR_ADDRESS(Name, argc, ressize) \
677 &FullCodeGenerator::Emit##Name,
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000678
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000679const FullCodeGenerator::InlineFunctionGenerator
680 FullCodeGenerator::kInlineFunctionGenerators[] = {
681 INLINE_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS)
682 INLINE_RUNTIME_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS)
683 };
684#undef INLINE_FUNCTION_GENERATOR_ADDRESS
685
686
687FullCodeGenerator::InlineFunctionGenerator
688 FullCodeGenerator::FindInlineFunctionGenerator(Runtime::FunctionId id) {
whesse@chromium.org023421e2010-12-21 12:19:12 +0000689 int lookup_index =
690 static_cast<int>(id) - static_cast<int>(Runtime::kFirstInlineFunction);
691 ASSERT(lookup_index >= 0);
692 ASSERT(static_cast<size_t>(lookup_index) <
693 ARRAY_SIZE(kInlineFunctionGenerators));
694 return kInlineFunctionGenerators[lookup_index];
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000695}
696
697
698void FullCodeGenerator::EmitInlineRuntimeCall(CallRuntime* node) {
699 ZoneList<Expression*>* args = node->arguments();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000700 const Runtime::Function* function = node->function();
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000701 ASSERT(function != NULL);
702 ASSERT(function->intrinsic_type == Runtime::INLINE);
703 InlineFunctionGenerator generator =
704 FindInlineFunctionGenerator(function->function_id);
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000705 ((*this).*(generator))(args);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000706}
707
708
709void FullCodeGenerator::VisitBinaryOperation(BinaryOperation* expr) {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000710 switch (expr->op()) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000711 case Token::COMMA:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000712 return VisitComma(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000713 case Token::OR:
714 case Token::AND:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000715 return VisitLogicalExpression(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000716 default:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000717 return VisitArithmeticExpression(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000718 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000719}
720
721
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000722void FullCodeGenerator::VisitComma(BinaryOperation* expr) {
723 Comment cmnt(masm_, "[ Comma");
724 VisitForEffect(expr->left());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000725 if (context()->IsTest()) ForwardBailoutToChild(expr);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000726 VisitInCurrentContext(expr->right());
727}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000728
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000729
730void FullCodeGenerator::VisitLogicalExpression(BinaryOperation* expr) {
731 bool is_logical_and = expr->op() == Token::AND;
732 Comment cmnt(masm_, is_logical_and ? "[ Logical AND" : "[ Logical OR");
733 Expression* left = expr->left();
734 Expression* right = expr->right();
735 int right_id = expr->RightId();
736 Label done;
737
738 if (context()->IsTest()) {
739 Label eval_right;
740 const TestContext* test = TestContext::cast(context());
741 if (is_logical_and) {
742 VisitForControl(left, &eval_right, test->false_label(), &eval_right);
743 } else {
744 VisitForControl(left, test->true_label(), &eval_right, &eval_right);
745 }
746 PrepareForBailoutForId(right_id, NO_REGISTERS);
747 __ bind(&eval_right);
748 ForwardBailoutToChild(expr);
749
750 } else if (context()->IsAccumulatorValue()) {
751 VisitForAccumulatorValue(left);
752 // We want the value in the accumulator for the test, and on the stack in
753 // case we need it.
754 __ push(result_register());
755 Label discard, restore;
756 PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
757 if (is_logical_and) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000758 DoTest(left, &discard, &restore, &restore);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000759 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000760 DoTest(left, &restore, &discard, &restore);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000761 }
762 __ bind(&restore);
763 __ pop(result_register());
764 __ jmp(&done);
765 __ bind(&discard);
766 __ Drop(1);
767 PrepareForBailoutForId(right_id, NO_REGISTERS);
768
769 } else if (context()->IsStackValue()) {
770 VisitForAccumulatorValue(left);
771 // We want the value in the accumulator for the test, and on the stack in
772 // case we need it.
773 __ push(result_register());
774 Label discard;
775 PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
776 if (is_logical_and) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000777 DoTest(left, &discard, &done, &discard);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000778 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000779 DoTest(left, &done, &discard, &discard);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000780 }
781 __ bind(&discard);
782 __ Drop(1);
783 PrepareForBailoutForId(right_id, NO_REGISTERS);
784
785 } else {
786 ASSERT(context()->IsEffect());
787 Label eval_right;
788 if (is_logical_and) {
789 VisitForControl(left, &eval_right, &done, &eval_right);
790 } else {
791 VisitForControl(left, &done, &eval_right, &eval_right);
792 }
793 PrepareForBailoutForId(right_id, NO_REGISTERS);
794 __ bind(&eval_right);
795 }
796
797 VisitInCurrentContext(right);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000798 __ bind(&done);
799}
800
801
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000802void FullCodeGenerator::VisitArithmeticExpression(BinaryOperation* expr) {
803 Token::Value op = expr->op();
804 Comment cmnt(masm_, "[ ArithmeticExpression");
805 Expression* left = expr->left();
806 Expression* right = expr->right();
807 OverwriteMode mode =
808 left->ResultOverwriteAllowed()
809 ? OVERWRITE_LEFT
810 : (right->ResultOverwriteAllowed() ? OVERWRITE_RIGHT : NO_OVERWRITE);
811
812 VisitForStackValue(left);
813 VisitForAccumulatorValue(right);
814
815 SetSourcePosition(expr->position());
816 if (ShouldInlineSmiCase(op)) {
817 EmitInlineSmiBinaryOp(expr, op, mode, left, right);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000818 } else {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000819 EmitBinaryOp(expr, op, mode);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000820 }
821}
822
823
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000824void FullCodeGenerator::ForwardBailoutToChild(Expression* expr) {
825 if (!info_->HasDeoptimizationSupport()) return;
826 ASSERT(context()->IsTest());
827 ASSERT(expr == forward_bailout_stack_->expr());
828 forward_bailout_pending_ = forward_bailout_stack_;
829}
830
831
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000832void FullCodeGenerator::VisitInCurrentContext(Expression* expr) {
833 if (context()->IsTest()) {
834 ForwardBailoutStack stack(expr, forward_bailout_pending_);
835 ForwardBailoutStack* saved = forward_bailout_stack_;
836 forward_bailout_pending_ = NULL;
837 forward_bailout_stack_ = &stack;
838 Visit(expr);
839 forward_bailout_stack_ = saved;
840 } else {
841 ASSERT(forward_bailout_pending_ == NULL);
842 Visit(expr);
843 State state = context()->IsAccumulatorValue() ? TOS_REG : NO_REGISTERS;
844 PrepareForBailout(expr, state);
845 // Forwarding bailouts to children is a one shot operation. It should have
846 // been processed at this point.
847 ASSERT(forward_bailout_pending_ == NULL);
848 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000849}
850
851
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000852void FullCodeGenerator::VisitBlock(Block* stmt) {
853 Comment cmnt(masm_, "[ Block");
854 Breakable nested_statement(this, stmt);
855 SetStatementPosition(stmt);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000856
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000857 Scope* saved_scope = scope();
858 if (stmt->block_scope() != NULL) {
859 { Comment cmnt(masm_, "[ Extend block context");
860 scope_ = stmt->block_scope();
861 __ Push(scope_->GetSerializedScopeInfo());
862 PushFunctionArgumentForContextAllocation();
863 __ CallRuntime(Runtime::kPushBlockContext, 2);
864 StoreToFrameField(StandardFrameConstants::kContextOffset,
865 context_register());
866 }
867 { Comment cmnt(masm_, "[ Declarations");
868 VisitDeclarations(scope_->declarations());
869 }
870 }
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +0000871 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000872 VisitStatements(stmt->statements());
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000873 scope_ = saved_scope;
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000874 __ bind(nested_statement.break_label());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000875 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000876}
877
878
879void FullCodeGenerator::VisitExpressionStatement(ExpressionStatement* stmt) {
880 Comment cmnt(masm_, "[ ExpressionStatement");
881 SetStatementPosition(stmt);
882 VisitForEffect(stmt->expression());
883}
884
885
886void FullCodeGenerator::VisitEmptyStatement(EmptyStatement* stmt) {
887 Comment cmnt(masm_, "[ EmptyStatement");
888 SetStatementPosition(stmt);
889}
890
891
892void FullCodeGenerator::VisitIfStatement(IfStatement* stmt) {
893 Comment cmnt(masm_, "[ IfStatement");
894 SetStatementPosition(stmt);
895 Label then_part, else_part, done;
896
ricow@chromium.org65fae842010-08-25 15:26:24 +0000897 if (stmt->HasElseStatement()) {
898 VisitForControl(stmt->condition(), &then_part, &else_part, &then_part);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000899 PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000900 __ bind(&then_part);
901 Visit(stmt->then_statement());
902 __ jmp(&done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000903
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000904 PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000905 __ bind(&else_part);
906 Visit(stmt->else_statement());
907 } else {
908 VisitForControl(stmt->condition(), &then_part, &done, &then_part);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000909 PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000910 __ bind(&then_part);
911 Visit(stmt->then_statement());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000912
913 PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000914 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000915 __ bind(&done);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000916 PrepareForBailoutForId(stmt->IfId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000917}
918
919
920void FullCodeGenerator::VisitContinueStatement(ContinueStatement* stmt) {
921 Comment cmnt(masm_, "[ ContinueStatement");
922 SetStatementPosition(stmt);
923 NestedStatement* current = nesting_stack_;
924 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000925 int context_length = 0;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000926 // When continuing, we clobber the unpredictable value in the accumulator
927 // with one that's safe for GC. If we hit an exit from the try block of
928 // try...finally on our way out, we will unconditionally preserve the
929 // accumulator on the stack.
930 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000931 while (!current->IsContinueTarget(stmt->target())) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000932 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000933 }
934 __ Drop(stack_depth);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000935 if (context_length > 0) {
936 while (context_length > 0) {
937 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
938 --context_length;
939 }
940 StoreToFrameField(StandardFrameConstants::kContextOffset,
941 context_register());
942 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000943
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000944 __ jmp(current->AsIteration()->continue_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000945}
946
947
948void FullCodeGenerator::VisitBreakStatement(BreakStatement* stmt) {
949 Comment cmnt(masm_, "[ BreakStatement");
950 SetStatementPosition(stmt);
951 NestedStatement* current = nesting_stack_;
952 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000953 int context_length = 0;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000954 // When breaking, we clobber the unpredictable value in the accumulator
955 // with one that's safe for GC. If we hit an exit from the try block of
956 // try...finally on our way out, we will unconditionally preserve the
957 // accumulator on the stack.
958 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000959 while (!current->IsBreakTarget(stmt->target())) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000960 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000961 }
962 __ Drop(stack_depth);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000963 if (context_length > 0) {
964 while (context_length > 0) {
965 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
966 --context_length;
967 }
968 StoreToFrameField(StandardFrameConstants::kContextOffset,
969 context_register());
970 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000971
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000972 __ jmp(current->AsBreakable()->break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000973}
974
975
976void FullCodeGenerator::VisitReturnStatement(ReturnStatement* stmt) {
977 Comment cmnt(masm_, "[ ReturnStatement");
978 SetStatementPosition(stmt);
979 Expression* expr = stmt->expression();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000980 VisitForAccumulatorValue(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000981
982 // Exit all nested statements.
983 NestedStatement* current = nesting_stack_;
984 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000985 int context_length = 0;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000986 while (current != NULL) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000987 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000988 }
989 __ Drop(stack_depth);
990
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000991 EmitReturnSequence();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000992}
993
994
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000995void FullCodeGenerator::VisitWithStatement(WithStatement* stmt) {
996 Comment cmnt(masm_, "[ WithStatement");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000997 SetStatementPosition(stmt);
998
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000999 VisitForStackValue(stmt->expression());
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00001000 PushFunctionArgumentForContextAllocation();
1001 __ CallRuntime(Runtime::kPushWithContext, 2);
vegorov@chromium.org7943d462011-08-01 11:41:52 +00001002 decrement_stack_height();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001003 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001004
1005 { WithOrCatch body(this);
1006 Visit(stmt->statement());
1007 }
1008
1009 // Pop context.
1010 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1011 // Update local stack frame context field.
1012 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001013}
1014
1015
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001016void FullCodeGenerator::VisitExitContextStatement(ExitContextStatement* stmt) {
1017 Comment cmnt(masm_, "[ ExitContextStatement");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001018 SetStatementPosition(stmt);
1019
1020 // Pop context.
1021 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1022 // Update local stack frame context field.
1023 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
1024}
1025
1026
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001027void FullCodeGenerator::VisitDoWhileStatement(DoWhileStatement* stmt) {
1028 Comment cmnt(masm_, "[ DoWhileStatement");
1029 SetStatementPosition(stmt);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001030 Label body, stack_check;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001031
1032 Iteration loop_statement(this, stmt);
1033 increment_loop_depth();
1034
1035 __ bind(&body);
1036 Visit(stmt->body());
1037
ricow@chromium.org65fae842010-08-25 15:26:24 +00001038 // Record the position of the do while condition and make sure it is
1039 // possible to break on the condition.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001040 __ bind(loop_statement.continue_label());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001041 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001042 SetExpressionPosition(stmt->cond(), stmt->condition_position());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001043 VisitForControl(stmt->cond(),
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001044 &stack_check,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001045 loop_statement.break_label(),
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001046 &stack_check);
1047
1048 // Check stack before looping.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001049 PrepareForBailoutForId(stmt->BackEdgeId(), NO_REGISTERS);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001050 __ bind(&stack_check);
1051 EmitStackCheck(stmt);
1052 __ jmp(&body);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001053
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001054 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001055 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001056 decrement_loop_depth();
1057}
1058
1059
1060void FullCodeGenerator::VisitWhileStatement(WhileStatement* stmt) {
1061 Comment cmnt(masm_, "[ WhileStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001062 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001063
1064 Iteration loop_statement(this, stmt);
1065 increment_loop_depth();
1066
1067 // Emit the test at the bottom of the loop.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001068 __ jmp(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001069
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001070 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001071 __ bind(&body);
1072 Visit(stmt->body());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001073
1074 // Emit the statement position here as this is where the while
1075 // statement code starts.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001076 __ bind(loop_statement.continue_label());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001077 SetStatementPosition(stmt);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001078
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001079 // Check stack before looping.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001080 EmitStackCheck(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001081
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001082 __ bind(&test);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001083 VisitForControl(stmt->cond(),
1084 &body,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001085 loop_statement.break_label(),
1086 loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001087
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001088 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001089 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001090 decrement_loop_depth();
1091}
1092
1093
1094void FullCodeGenerator::VisitForStatement(ForStatement* stmt) {
1095 Comment cmnt(masm_, "[ ForStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001096 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001097
1098 Iteration loop_statement(this, stmt);
1099 if (stmt->init() != NULL) {
1100 Visit(stmt->init());
1101 }
1102
1103 increment_loop_depth();
1104 // Emit the test at the bottom of the loop (even if empty).
1105 __ jmp(&test);
1106
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001107 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001108 __ bind(&body);
1109 Visit(stmt->body());
1110
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001111 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001112 __ bind(loop_statement.continue_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001113 SetStatementPosition(stmt);
1114 if (stmt->next() != NULL) {
1115 Visit(stmt->next());
1116 }
1117
ricow@chromium.org65fae842010-08-25 15:26:24 +00001118 // Emit the statement position here as this is where the for
1119 // statement code starts.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001120 SetStatementPosition(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001121
1122 // Check stack before looping.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001123 EmitStackCheck(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001124
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001125 __ bind(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001126 if (stmt->cond() != NULL) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00001127 VisitForControl(stmt->cond(),
1128 &body,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001129 loop_statement.break_label(),
1130 loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001131 } else {
1132 __ jmp(&body);
1133 }
1134
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001135 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001136 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001137 decrement_loop_depth();
1138}
1139
1140
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001141void FullCodeGenerator::VisitTryCatchStatement(TryCatchStatement* stmt) {
1142 Comment cmnt(masm_, "[ TryCatchStatement");
1143 SetStatementPosition(stmt);
1144 // The try block adds a handler to the exception handler chain
1145 // before entering, and removes it again when exiting normally.
1146 // If an exception is thrown during execution of the try block,
1147 // control is passed to the handler, which also consumes the handler.
1148 // At this point, the exception is in a register, and store it in
1149 // the temporary local variable (prints as ".catch-var") before
1150 // executing the catch block. The catch block has been rewritten
1151 // to introduce a new scope to bind the catch variable and to remove
1152 // that scope again afterwards.
1153
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001154 Label try_handler_setup, done;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001155 __ Call(&try_handler_setup);
1156 // Try handler code, exception in result register.
1157
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001158 // Extend the context before executing the catch block.
1159 { Comment cmnt(masm_, "[ Extend catch context");
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001160 __ Push(stmt->variable()->name());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001161 __ push(result_register());
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00001162 PushFunctionArgumentForContextAllocation();
1163 __ CallRuntime(Runtime::kPushCatchContext, 3);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001164 StoreToFrameField(StandardFrameConstants::kContextOffset,
1165 context_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001166 }
1167
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001168 Scope* saved_scope = scope();
1169 scope_ = stmt->scope();
1170 ASSERT(scope_->declarations()->is_empty());
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001171 { WithOrCatch body(this);
1172 Visit(stmt->catch_block());
1173 }
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001174 scope_ = saved_scope;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001175 __ jmp(&done);
1176
1177 // Try block code. Sets up the exception handler chain.
1178 __ bind(&try_handler_setup);
1179 {
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001180 const int delta = StackHandlerConstants::kSize / kPointerSize;
1181 TryCatch try_block(this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001182 __ PushTryHandler(IN_JAVASCRIPT, TRY_CATCH_HANDLER);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001183 increment_stack_height(delta);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001184 Visit(stmt->try_block());
1185 __ PopTryHandler();
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001186 decrement_stack_height(delta);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001187 }
1188 __ bind(&done);
1189}
1190
1191
1192void FullCodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* stmt) {
1193 Comment cmnt(masm_, "[ TryFinallyStatement");
1194 SetStatementPosition(stmt);
1195 // Try finally is compiled by setting up a try-handler on the stack while
1196 // executing the try body, and removing it again afterwards.
1197 //
1198 // The try-finally construct can enter the finally block in three ways:
1199 // 1. By exiting the try-block normally. This removes the try-handler and
1200 // calls the finally block code before continuing.
1201 // 2. By exiting the try-block with a function-local control flow transfer
1202 // (break/continue/return). The site of the, e.g., break removes the
1203 // try handler and calls the finally block code before continuing
1204 // its outward control transfer.
1205 // 3. by exiting the try-block with a thrown exception.
1206 // This can happen in nested function calls. It traverses the try-handler
1207 // chain and consumes the try-handler entry before jumping to the
1208 // handler code. The handler code then calls the finally-block before
1209 // rethrowing the exception.
1210 //
1211 // The finally block must assume a return address on top of the stack
1212 // (or in the link register on ARM chips) and a value (return value or
1213 // exception) in the result register (rax/eax/r0), both of which must
1214 // be preserved. The return address isn't GC-safe, so it should be
1215 // cooked before GC.
1216 Label finally_entry;
1217 Label try_handler_setup;
vegorov@chromium.org7943d462011-08-01 11:41:52 +00001218 const int original_stack_height = stack_height();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001219
1220 // Setup the try-handler chain. Use a call to
1221 // Jump to try-handler setup and try-block code. Use call to put try-handler
1222 // address on stack.
1223 __ Call(&try_handler_setup);
1224 // Try handler code. Return address of call is pushed on handler stack.
1225 {
1226 // This code is only executed during stack-handler traversal when an
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001227 // exception is thrown. The exception is in the result register, which
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001228 // is retained by the finally block.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001229 // Call the finally block and then rethrow the exception if it returns.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001230 __ Call(&finally_entry);
1231 __ push(result_register());
1232 __ CallRuntime(Runtime::kReThrow, 1);
1233 }
1234
1235 __ bind(&finally_entry);
1236 {
1237 // Finally block implementation.
1238 Finally finally_block(this);
1239 EnterFinallyBlock();
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001240 set_stack_height(original_stack_height + Finally::kElementCount);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001241 Visit(stmt->finally_block());
1242 ExitFinallyBlock(); // Return to the calling code.
1243 }
1244
1245 __ bind(&try_handler_setup);
1246 {
1247 // Setup try handler (stack pointer registers).
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001248 const int delta = StackHandlerConstants::kSize / kPointerSize;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001249 TryFinally try_block(this, &finally_entry);
1250 __ PushTryHandler(IN_JAVASCRIPT, TRY_FINALLY_HANDLER);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001251 set_stack_height(original_stack_height + delta);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001252 Visit(stmt->try_block());
1253 __ PopTryHandler();
vegorov@chromium.org7943d462011-08-01 11:41:52 +00001254 set_stack_height(original_stack_height);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001255 }
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001256 // Execute the finally block on the way out. Clobber the unpredictable
1257 // value in the accumulator with one that's safe for GC. The finally
1258 // block will unconditionally preserve the accumulator on the stack.
1259 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001260 __ Call(&finally_entry);
1261}
1262
1263
1264void FullCodeGenerator::VisitDebuggerStatement(DebuggerStatement* stmt) {
1265#ifdef ENABLE_DEBUGGER_SUPPORT
1266 Comment cmnt(masm_, "[ DebuggerStatement");
1267 SetStatementPosition(stmt);
1268
ager@chromium.org5c838252010-02-19 08:53:10 +00001269 __ DebugBreak();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001270 // Ignore the return value.
1271#endif
1272}
1273
1274
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001275void FullCodeGenerator::VisitConditional(Conditional* expr) {
1276 Comment cmnt(masm_, "[ Conditional");
1277 Label true_case, false_case, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001278 VisitForControl(expr->condition(), &true_case, &false_case, &true_case);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001279
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001280 PrepareForBailoutForId(expr->ThenId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001281 __ bind(&true_case);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001282 SetExpressionPosition(expr->then_expression(),
1283 expr->then_expression_position());
vegorov@chromium.org7943d462011-08-01 11:41:52 +00001284 int start_stack_height = stack_height();
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001285 if (context()->IsTest()) {
1286 const TestContext* for_test = TestContext::cast(context());
1287 VisitForControl(expr->then_expression(),
1288 for_test->true_label(),
1289 for_test->false_label(),
1290 NULL);
1291 } else {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001292 VisitInCurrentContext(expr->then_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001293 __ jmp(&done);
1294 }
1295
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001296 PrepareForBailoutForId(expr->ElseId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001297 __ bind(&false_case);
vegorov@chromium.org7943d462011-08-01 11:41:52 +00001298 set_stack_height(start_stack_height);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001299 if (context()->IsTest()) ForwardBailoutToChild(expr);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001300 SetExpressionPosition(expr->else_expression(),
1301 expr->else_expression_position());
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001302 VisitInCurrentContext(expr->else_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001303 // If control flow falls through Visit, merge it with true case here.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001304 if (!context()->IsTest()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001305 __ bind(&done);
1306 }
1307}
1308
1309
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001310void FullCodeGenerator::VisitLiteral(Literal* expr) {
1311 Comment cmnt(masm_, "[ Literal");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001312 context()->Plug(expr->handle());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001313}
1314
1315
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001316void FullCodeGenerator::VisitFunctionLiteral(FunctionLiteral* expr) {
1317 Comment cmnt(masm_, "[ FunctionLiteral");
1318
1319 // Build the function boilerplate and instantiate it.
1320 Handle<SharedFunctionInfo> function_info =
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001321 Compiler::BuildFunctionInfo(expr, script());
1322 if (function_info.is_null()) {
1323 SetStackOverflow();
1324 return;
1325 }
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001326 EmitNewClosure(function_info, expr->pretenure());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001327}
1328
1329
1330void FullCodeGenerator::VisitSharedFunctionInfoLiteral(
1331 SharedFunctionInfoLiteral* expr) {
1332 Comment cmnt(masm_, "[ SharedFunctionInfoLiteral");
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001333 EmitNewClosure(expr->shared_function_info(), false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001334}
1335
1336
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001337void FullCodeGenerator::VisitThrow(Throw* expr) {
1338 Comment cmnt(masm_, "[ Throw");
vegorov@chromium.org7943d462011-08-01 11:41:52 +00001339 // Throw has no effect on the stack height or the current expression context.
1340 // Usually the expression context is null, because throw is a statement.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001341 VisitForStackValue(expr->exception());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001342 __ CallRuntime(Runtime::kThrow, 1);
vegorov@chromium.org7943d462011-08-01 11:41:52 +00001343 decrement_stack_height();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001344 // Never returns here.
1345}
1346
1347
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001348FullCodeGenerator::NestedStatement* FullCodeGenerator::TryFinally::Exit(
1349 int* stack_depth,
1350 int* context_length) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001351 // The macros used here must preserve the result register.
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001352 __ Drop(*stack_depth);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001353 __ PopTryHandler();
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001354 *stack_depth = 0;
1355
1356 Register context = FullCodeGenerator::context_register();
1357 while (*context_length > 0) {
1358 codegen_->LoadContextField(context, Context::PREVIOUS_INDEX);
1359 --(*context_length);
1360 }
1361
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001362 __ Call(finally_entry_);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001363 return previous_;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001364}
1365
1366
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001367FullCodeGenerator::NestedStatement* FullCodeGenerator::TryCatch::Exit(
1368 int* stack_depth,
1369 int* context_length) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001370 // The macros used here must preserve the result register.
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001371 __ Drop(*stack_depth);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001372 __ PopTryHandler();
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001373 *stack_depth = 0;
1374 return previous_;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001375}
1376
ricow@chromium.org65fae842010-08-25 15:26:24 +00001377
ager@chromium.org04921a82011-06-27 13:21:41 +00001378bool FullCodeGenerator::TryLiteralCompare(CompareOperation* compare,
1379 Label* if_true,
1380 Label* if_false,
1381 Label* fall_through) {
1382 Expression *expr;
1383 Handle<String> check;
1384 if (compare->IsLiteralCompareTypeof(&expr, &check)) {
1385 EmitLiteralCompareTypeof(expr, check, if_true, if_false, fall_through);
1386 return true;
1387 }
1388
1389 if (compare->IsLiteralCompareUndefined(&expr)) {
1390 EmitLiteralCompareUndefined(expr, if_true, if_false, fall_through);
1391 return true;
1392 }
1393
1394 return false;
1395}
1396
1397
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001398#undef __
1399
1400
1401} } // namespace v8::internal