blob: 732a8fe7d0aadc3a0a9417efe3333a829431339a [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;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000865 __ bind(nested_statement.break_target());
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
935 Iteration* loop = current->AsIteration();
936 __ jmp(loop->continue_target());
937}
938
939
940void FullCodeGenerator::VisitBreakStatement(BreakStatement* stmt) {
941 Comment cmnt(masm_, "[ BreakStatement");
942 SetStatementPosition(stmt);
943 NestedStatement* current = nesting_stack_;
944 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000945 int context_length = 0;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000946 // When breaking, we clobber the unpredictable value in the accumulator
947 // with one that's safe for GC. If we hit an exit from the try block of
948 // try...finally on our way out, we will unconditionally preserve the
949 // accumulator on the stack.
950 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000951 while (!current->IsBreakTarget(stmt->target())) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000952 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000953 }
954 __ Drop(stack_depth);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000955 if (context_length > 0) {
956 while (context_length > 0) {
957 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
958 --context_length;
959 }
960 StoreToFrameField(StandardFrameConstants::kContextOffset,
961 context_register());
962 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000963
964 Breakable* target = current->AsBreakable();
965 __ jmp(target->break_target());
966}
967
968
969void FullCodeGenerator::VisitReturnStatement(ReturnStatement* stmt) {
970 Comment cmnt(masm_, "[ ReturnStatement");
971 SetStatementPosition(stmt);
972 Expression* expr = stmt->expression();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000973 VisitForAccumulatorValue(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000974
975 // Exit all nested statements.
976 NestedStatement* current = nesting_stack_;
977 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000978 int context_length = 0;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000979 while (current != NULL) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000980 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000981 }
982 __ Drop(stack_depth);
983
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000984 EmitReturnSequence();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000985}
986
987
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000988void FullCodeGenerator::VisitWithStatement(WithStatement* stmt) {
989 Comment cmnt(masm_, "[ WithStatement");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000990 SetStatementPosition(stmt);
991
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000992 VisitForStackValue(stmt->expression());
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000993 PushFunctionArgumentForContextAllocation();
994 __ CallRuntime(Runtime::kPushWithContext, 2);
vegorov@chromium.org7943d462011-08-01 11:41:52 +0000995 decrement_stack_height();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000996 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000997
998 { WithOrCatch body(this);
999 Visit(stmt->statement());
1000 }
1001
1002 // Pop context.
1003 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1004 // Update local stack frame context field.
1005 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001006}
1007
1008
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001009void FullCodeGenerator::VisitExitContextStatement(ExitContextStatement* stmt) {
1010 Comment cmnt(masm_, "[ ExitContextStatement");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001011 SetStatementPosition(stmt);
1012
1013 // Pop context.
1014 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1015 // Update local stack frame context field.
1016 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
1017}
1018
1019
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001020void FullCodeGenerator::VisitDoWhileStatement(DoWhileStatement* stmt) {
1021 Comment cmnt(masm_, "[ DoWhileStatement");
1022 SetStatementPosition(stmt);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001023 Label body, stack_check;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001024
1025 Iteration loop_statement(this, stmt);
1026 increment_loop_depth();
1027
1028 __ bind(&body);
1029 Visit(stmt->body());
1030
ricow@chromium.org65fae842010-08-25 15:26:24 +00001031 // Record the position of the do while condition and make sure it is
1032 // possible to break on the condition.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001033 __ bind(loop_statement.continue_target());
1034 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001035 SetExpressionPosition(stmt->cond(), stmt->condition_position());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001036 VisitForControl(stmt->cond(),
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001037 &stack_check,
ricow@chromium.org65fae842010-08-25 15:26:24 +00001038 loop_statement.break_target(),
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001039 &stack_check);
1040
1041 // Check stack before looping.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001042 PrepareForBailoutForId(stmt->BackEdgeId(), NO_REGISTERS);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001043 __ bind(&stack_check);
1044 EmitStackCheck(stmt);
1045 __ jmp(&body);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001046
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001047 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001048 __ bind(loop_statement.break_target());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001049 decrement_loop_depth();
1050}
1051
1052
1053void FullCodeGenerator::VisitWhileStatement(WhileStatement* stmt) {
1054 Comment cmnt(masm_, "[ WhileStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001055 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001056
1057 Iteration loop_statement(this, stmt);
1058 increment_loop_depth();
1059
1060 // Emit the test at the bottom of the loop.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001061 __ jmp(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001062
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001063 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001064 __ bind(&body);
1065 Visit(stmt->body());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001066
1067 // Emit the statement position here as this is where the while
1068 // statement code starts.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001069 __ bind(loop_statement.continue_target());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001070 SetStatementPosition(stmt);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001071
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001072 // Check stack before looping.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001073 EmitStackCheck(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001074
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001075 __ bind(&test);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001076 VisitForControl(stmt->cond(),
1077 &body,
1078 loop_statement.break_target(),
1079 loop_statement.break_target());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001080
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001081 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001082 __ bind(loop_statement.break_target());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001083 decrement_loop_depth();
1084}
1085
1086
1087void FullCodeGenerator::VisitForStatement(ForStatement* stmt) {
1088 Comment cmnt(masm_, "[ ForStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001089 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001090
1091 Iteration loop_statement(this, stmt);
1092 if (stmt->init() != NULL) {
1093 Visit(stmt->init());
1094 }
1095
1096 increment_loop_depth();
1097 // Emit the test at the bottom of the loop (even if empty).
1098 __ jmp(&test);
1099
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001100 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001101 __ bind(&body);
1102 Visit(stmt->body());
1103
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001104 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001105 __ bind(loop_statement.continue_target());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001106 SetStatementPosition(stmt);
1107 if (stmt->next() != NULL) {
1108 Visit(stmt->next());
1109 }
1110
ricow@chromium.org65fae842010-08-25 15:26:24 +00001111 // Emit the statement position here as this is where the for
1112 // statement code starts.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001113 SetStatementPosition(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001114
1115 // Check stack before looping.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001116 EmitStackCheck(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001117
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001118 __ bind(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001119 if (stmt->cond() != NULL) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00001120 VisitForControl(stmt->cond(),
1121 &body,
1122 loop_statement.break_target(),
1123 loop_statement.break_target());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001124 } else {
1125 __ jmp(&body);
1126 }
1127
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001128 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001129 __ bind(loop_statement.break_target());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001130 decrement_loop_depth();
1131}
1132
1133
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001134void FullCodeGenerator::VisitTryCatchStatement(TryCatchStatement* stmt) {
1135 Comment cmnt(masm_, "[ TryCatchStatement");
1136 SetStatementPosition(stmt);
1137 // The try block adds a handler to the exception handler chain
1138 // before entering, and removes it again when exiting normally.
1139 // If an exception is thrown during execution of the try block,
1140 // control is passed to the handler, which also consumes the handler.
1141 // At this point, the exception is in a register, and store it in
1142 // the temporary local variable (prints as ".catch-var") before
1143 // executing the catch block. The catch block has been rewritten
1144 // to introduce a new scope to bind the catch variable and to remove
1145 // that scope again afterwards.
1146
1147 Label try_handler_setup, catch_entry, done;
1148 __ Call(&try_handler_setup);
1149 // Try handler code, exception in result register.
1150
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001151 // Extend the context before executing the catch block.
1152 { Comment cmnt(masm_, "[ Extend catch context");
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001153 __ Push(stmt->variable()->name());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001154 __ push(result_register());
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00001155 PushFunctionArgumentForContextAllocation();
1156 __ CallRuntime(Runtime::kPushCatchContext, 3);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001157 StoreToFrameField(StandardFrameConstants::kContextOffset,
1158 context_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001159 }
1160
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001161 Scope* saved_scope = scope();
1162 scope_ = stmt->scope();
1163 ASSERT(scope_->declarations()->is_empty());
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001164 { WithOrCatch body(this);
1165 Visit(stmt->catch_block());
1166 }
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001167 scope_ = saved_scope;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001168 __ jmp(&done);
1169
1170 // Try block code. Sets up the exception handler chain.
1171 __ bind(&try_handler_setup);
1172 {
1173 TryCatch try_block(this, &catch_entry);
1174 __ PushTryHandler(IN_JAVASCRIPT, TRY_CATCH_HANDLER);
vegorov@chromium.org7943d462011-08-01 11:41:52 +00001175 increment_stack_height(StackHandlerConstants::kSize / kPointerSize);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001176 Visit(stmt->try_block());
1177 __ PopTryHandler();
vegorov@chromium.org7943d462011-08-01 11:41:52 +00001178 decrement_stack_height(StackHandlerConstants::kSize / kPointerSize);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001179 }
1180 __ bind(&done);
1181}
1182
1183
1184void FullCodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* stmt) {
1185 Comment cmnt(masm_, "[ TryFinallyStatement");
1186 SetStatementPosition(stmt);
1187 // Try finally is compiled by setting up a try-handler on the stack while
1188 // executing the try body, and removing it again afterwards.
1189 //
1190 // The try-finally construct can enter the finally block in three ways:
1191 // 1. By exiting the try-block normally. This removes the try-handler and
1192 // calls the finally block code before continuing.
1193 // 2. By exiting the try-block with a function-local control flow transfer
1194 // (break/continue/return). The site of the, e.g., break removes the
1195 // try handler and calls the finally block code before continuing
1196 // its outward control transfer.
1197 // 3. by exiting the try-block with a thrown exception.
1198 // This can happen in nested function calls. It traverses the try-handler
1199 // chain and consumes the try-handler entry before jumping to the
1200 // handler code. The handler code then calls the finally-block before
1201 // rethrowing the exception.
1202 //
1203 // The finally block must assume a return address on top of the stack
1204 // (or in the link register on ARM chips) and a value (return value or
1205 // exception) in the result register (rax/eax/r0), both of which must
1206 // be preserved. The return address isn't GC-safe, so it should be
1207 // cooked before GC.
1208 Label finally_entry;
1209 Label try_handler_setup;
vegorov@chromium.org7943d462011-08-01 11:41:52 +00001210 const int original_stack_height = stack_height();
1211 const int finally_block_stack_height = original_stack_height + 2;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001212 const int try_block_stack_height = original_stack_height + 5;
1213 STATIC_ASSERT(StackHandlerConstants::kSize == 5 * kPointerSize);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001214
1215 // Setup the try-handler chain. Use a call to
1216 // Jump to try-handler setup and try-block code. Use call to put try-handler
1217 // address on stack.
1218 __ Call(&try_handler_setup);
1219 // Try handler code. Return address of call is pushed on handler stack.
1220 {
1221 // This code is only executed during stack-handler traversal when an
1222 // exception is thrown. The execption is in the result register, which
1223 // is retained by the finally block.
1224 // Call the finally block and then rethrow the exception.
1225 __ Call(&finally_entry);
1226 __ push(result_register());
1227 __ CallRuntime(Runtime::kReThrow, 1);
1228 }
1229
1230 __ bind(&finally_entry);
1231 {
1232 // Finally block implementation.
1233 Finally finally_block(this);
1234 EnterFinallyBlock();
vegorov@chromium.org7943d462011-08-01 11:41:52 +00001235 set_stack_height(finally_block_stack_height);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001236 Visit(stmt->finally_block());
1237 ExitFinallyBlock(); // Return to the calling code.
1238 }
1239
1240 __ bind(&try_handler_setup);
1241 {
1242 // Setup try handler (stack pointer registers).
1243 TryFinally try_block(this, &finally_entry);
1244 __ PushTryHandler(IN_JAVASCRIPT, TRY_FINALLY_HANDLER);
vegorov@chromium.org7943d462011-08-01 11:41:52 +00001245 set_stack_height(try_block_stack_height);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001246 Visit(stmt->try_block());
1247 __ PopTryHandler();
vegorov@chromium.org7943d462011-08-01 11:41:52 +00001248 set_stack_height(original_stack_height);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001249 }
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001250 // Execute the finally block on the way out. Clobber the unpredictable
1251 // value in the accumulator with one that's safe for GC. The finally
1252 // block will unconditionally preserve the accumulator on the stack.
1253 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001254 __ Call(&finally_entry);
1255}
1256
1257
1258void FullCodeGenerator::VisitDebuggerStatement(DebuggerStatement* stmt) {
1259#ifdef ENABLE_DEBUGGER_SUPPORT
1260 Comment cmnt(masm_, "[ DebuggerStatement");
1261 SetStatementPosition(stmt);
1262
ager@chromium.org5c838252010-02-19 08:53:10 +00001263 __ DebugBreak();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001264 // Ignore the return value.
1265#endif
1266}
1267
1268
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001269void FullCodeGenerator::VisitConditional(Conditional* expr) {
1270 Comment cmnt(masm_, "[ Conditional");
1271 Label true_case, false_case, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001272 VisitForControl(expr->condition(), &true_case, &false_case, &true_case);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001273
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001274 PrepareForBailoutForId(expr->ThenId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001275 __ bind(&true_case);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001276 SetExpressionPosition(expr->then_expression(),
1277 expr->then_expression_position());
vegorov@chromium.org7943d462011-08-01 11:41:52 +00001278 int start_stack_height = stack_height();
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001279 if (context()->IsTest()) {
1280 const TestContext* for_test = TestContext::cast(context());
1281 VisitForControl(expr->then_expression(),
1282 for_test->true_label(),
1283 for_test->false_label(),
1284 NULL);
1285 } else {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001286 VisitInCurrentContext(expr->then_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001287 __ jmp(&done);
1288 }
1289
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001290 PrepareForBailoutForId(expr->ElseId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001291 __ bind(&false_case);
vegorov@chromium.org7943d462011-08-01 11:41:52 +00001292 set_stack_height(start_stack_height);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001293 if (context()->IsTest()) ForwardBailoutToChild(expr);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001294 SetExpressionPosition(expr->else_expression(),
1295 expr->else_expression_position());
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001296 VisitInCurrentContext(expr->else_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001297 // If control flow falls through Visit, merge it with true case here.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001298 if (!context()->IsTest()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001299 __ bind(&done);
1300 }
1301}
1302
1303
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001304void FullCodeGenerator::VisitLiteral(Literal* expr) {
1305 Comment cmnt(masm_, "[ Literal");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001306 context()->Plug(expr->handle());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001307}
1308
1309
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001310void FullCodeGenerator::VisitFunctionLiteral(FunctionLiteral* expr) {
1311 Comment cmnt(masm_, "[ FunctionLiteral");
1312
1313 // Build the function boilerplate and instantiate it.
1314 Handle<SharedFunctionInfo> function_info =
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001315 Compiler::BuildFunctionInfo(expr, script());
1316 if (function_info.is_null()) {
1317 SetStackOverflow();
1318 return;
1319 }
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001320 EmitNewClosure(function_info, expr->pretenure());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001321}
1322
1323
1324void FullCodeGenerator::VisitSharedFunctionInfoLiteral(
1325 SharedFunctionInfoLiteral* expr) {
1326 Comment cmnt(masm_, "[ SharedFunctionInfoLiteral");
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001327 EmitNewClosure(expr->shared_function_info(), false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001328}
1329
1330
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001331void FullCodeGenerator::VisitThrow(Throw* expr) {
1332 Comment cmnt(masm_, "[ Throw");
vegorov@chromium.org7943d462011-08-01 11:41:52 +00001333 // Throw has no effect on the stack height or the current expression context.
1334 // Usually the expression context is null, because throw is a statement.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001335 VisitForStackValue(expr->exception());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001336 __ CallRuntime(Runtime::kThrow, 1);
vegorov@chromium.org7943d462011-08-01 11:41:52 +00001337 decrement_stack_height();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001338 // Never returns here.
1339}
1340
1341
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001342FullCodeGenerator::NestedStatement* FullCodeGenerator::TryFinally::Exit(
1343 int* stack_depth,
1344 int* context_length) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001345 // The macros used here must preserve the result register.
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001346 __ Drop(*stack_depth);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001347 __ PopTryHandler();
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001348 *stack_depth = 0;
1349
1350 Register context = FullCodeGenerator::context_register();
1351 while (*context_length > 0) {
1352 codegen_->LoadContextField(context, Context::PREVIOUS_INDEX);
1353 --(*context_length);
1354 }
1355
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001356 __ Call(finally_entry_);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001357 return previous_;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001358}
1359
1360
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001361FullCodeGenerator::NestedStatement* FullCodeGenerator::TryCatch::Exit(
1362 int* stack_depth,
1363 int* context_length) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001364 // The macros used here must preserve the result register.
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001365 __ Drop(*stack_depth);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001366 __ PopTryHandler();
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001367 *stack_depth = 0;
1368 return previous_;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001369}
1370
ricow@chromium.org65fae842010-08-25 15:26:24 +00001371
ager@chromium.org04921a82011-06-27 13:21:41 +00001372bool FullCodeGenerator::TryLiteralCompare(CompareOperation* compare,
1373 Label* if_true,
1374 Label* if_false,
1375 Label* fall_through) {
1376 Expression *expr;
1377 Handle<String> check;
1378 if (compare->IsLiteralCompareTypeof(&expr, &check)) {
1379 EmitLiteralCompareTypeof(expr, check, if_true, if_false, fall_through);
1380 return true;
1381 }
1382
1383 if (compare->IsLiteralCompareUndefined(&expr)) {
1384 EmitLiteralCompareUndefined(expr, if_true, if_false, fall_through);
1385 return true;
1386 }
1387
1388 return false;
1389}
1390
1391
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001392#undef __
1393
1394
1395} } // namespace v8::internal