blob: e5375fc3ae8b4750b6c47a45346f08abb9c5c2bc [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"
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000038#include "stub-cache.h"
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000039
40namespace v8 {
41namespace internal {
42
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +000043void BreakableStatementChecker::Check(Statement* stmt) {
44 Visit(stmt);
45}
46
47
48void BreakableStatementChecker::Check(Expression* expr) {
49 Visit(expr);
50}
51
52
53void BreakableStatementChecker::VisitDeclaration(Declaration* decl) {
54}
55
56
57void BreakableStatementChecker::VisitBlock(Block* stmt) {
58}
59
60
61void BreakableStatementChecker::VisitExpressionStatement(
62 ExpressionStatement* stmt) {
63 // Check if expression is breakable.
64 Visit(stmt->expression());
65}
66
67
68void BreakableStatementChecker::VisitEmptyStatement(EmptyStatement* stmt) {
69}
70
71
72void BreakableStatementChecker::VisitIfStatement(IfStatement* stmt) {
73 // If the condition is breakable the if statement is breakable.
74 Visit(stmt->condition());
75}
76
77
78void BreakableStatementChecker::VisitContinueStatement(
79 ContinueStatement* stmt) {
80}
81
82
83void BreakableStatementChecker::VisitBreakStatement(BreakStatement* stmt) {
84}
85
86
87void BreakableStatementChecker::VisitReturnStatement(ReturnStatement* stmt) {
88 // Return is breakable if the expression is.
89 Visit(stmt->expression());
90}
91
92
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +000093void BreakableStatementChecker::VisitEnterWithContextStatement(
94 EnterWithContextStatement* 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);
320 masm()->RecordComment("[ Stack check table");
321 unsigned offset = masm()->pc_offset();
322 unsigned length = stack_checks_.length();
323 __ dd(length);
324 for (unsigned i = 0; i < length; ++i) {
325 __ dd(stack_checks_[i].id);
326 __ dd(stack_checks_[i].pc_and_state);
327 }
328 masm()->RecordComment("]");
329 return offset;
330}
331
332
333void FullCodeGenerator::PopulateDeoptimizationData(Handle<Code> code) {
334 // Fill in the deoptimization information.
335 ASSERT(info_->HasDeoptimizationSupport() || bailout_entries_.is_empty());
336 if (!info_->HasDeoptimizationSupport()) return;
337 int length = bailout_entries_.length();
338 Handle<DeoptimizationOutputData> data =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000339 isolate()->factory()->
340 NewDeoptimizationOutputData(length, TENURED);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000341 for (int i = 0; i < length; i++) {
342 data->SetAstId(i, Smi::FromInt(bailout_entries_[i].id));
343 data->SetPcAndState(i, Smi::FromInt(bailout_entries_[i].pc_and_state));
344 }
345 code->set_deoptimization_data(*data);
346}
347
348
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000349void FullCodeGenerator::PrepareForBailout(Expression* node, State state) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000350 PrepareForBailoutForId(node->id(), state);
351}
352
353
354void FullCodeGenerator::RecordJSReturnSite(Call* call) {
355 // We record the offset of the function return so we can rebuild the frame
356 // if the function was inlined, i.e., this is the return address in the
357 // inlined function's frame.
358 //
359 // The state is ignored. We defensively set it to TOS_REG, which is the
360 // real state of the unoptimized code at the return site.
361 PrepareForBailoutForId(call->ReturnId(), TOS_REG);
362#ifdef DEBUG
363 // In debug builds, mark the return so we can verify that this function
364 // was called.
365 ASSERT(!call->return_is_recorded_);
366 call->return_is_recorded_ = true;
367#endif
368}
369
370
371void FullCodeGenerator::PrepareForBailoutForId(int id, State state) {
372 // There's no need to prepare this code for bailouts from already optimized
373 // code or code that can't be optimized.
374 if (!FLAG_deopt || !info_->HasDeoptimizationSupport()) return;
375 unsigned pc_and_state =
376 StateField::encode(state) | PcField::encode(masm_->pc_offset());
377 BailoutEntry entry = { id, pc_and_state };
378#ifdef DEBUG
379 // Assert that we don't have multiple bailout entries for the same node.
380 for (int i = 0; i < bailout_entries_.length(); i++) {
381 if (bailout_entries_.at(i).id == entry.id) {
382 AstPrinter printer;
383 PrintF("%s", printer.PrintProgram(info_->function()));
384 UNREACHABLE();
385 }
386 }
387#endif // DEBUG
388 bailout_entries_.Add(entry);
389}
390
391
392void FullCodeGenerator::RecordStackCheck(int ast_id) {
393 // The pc offset does not need to be encoded and packed together with a
394 // state.
395 BailoutEntry entry = { ast_id, masm_->pc_offset() };
396 stack_checks_.Add(entry);
397}
398
399
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000400int FullCodeGenerator::SlotOffset(Slot* slot) {
401 ASSERT(slot != NULL);
402 // Offset is negative because higher indexes are at lower addresses.
403 int offset = -slot->index() * kPointerSize;
404 // Adjust by a (parameter or local) base offset.
405 switch (slot->type()) {
406 case Slot::PARAMETER:
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000407 offset += (info_->scope()->num_parameters() + 1) * kPointerSize;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000408 break;
409 case Slot::LOCAL:
410 offset += JavaScriptFrameConstants::kLocal0Offset;
411 break;
412 case Slot::CONTEXT:
413 case Slot::LOOKUP:
414 UNREACHABLE();
415 }
416 return offset;
417}
418
419
ricow@chromium.org65fae842010-08-25 15:26:24 +0000420bool FullCodeGenerator::ShouldInlineSmiCase(Token::Value op) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000421 // Inline smi case inside loops, but not division and modulo which
422 // are too complicated and take up too much space.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000423 if (op == Token::DIV ||op == Token::MOD) return false;
424 if (FLAG_always_inline_smi_code) return true;
425 return loop_depth_ > 0;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000426}
427
428
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000429void FullCodeGenerator::EffectContext::Plug(Register reg) const {
430}
431
432
433void FullCodeGenerator::AccumulatorValueContext::Plug(Register reg) const {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000434 __ Move(result_register(), reg);
435}
436
437
438void FullCodeGenerator::StackValueContext::Plug(Register reg) const {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000439 __ push(reg);
vegorov@chromium.org7943d462011-08-01 11:41:52 +0000440 codegen()->increment_stack_height();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000441}
442
443
444void FullCodeGenerator::TestContext::Plug(Register reg) const {
445 // For simplicity we always test the accumulator register.
446 __ Move(result_register(), reg);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000447 codegen()->PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000448 codegen()->DoTest(this);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000449}
450
451
452void FullCodeGenerator::EffectContext::PlugTOS() const {
453 __ Drop(1);
vegorov@chromium.org7943d462011-08-01 11:41:52 +0000454 codegen()->decrement_stack_height();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000455}
456
457
458void FullCodeGenerator::AccumulatorValueContext::PlugTOS() const {
459 __ pop(result_register());
vegorov@chromium.org7943d462011-08-01 11:41:52 +0000460 codegen()->decrement_stack_height();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000461}
462
463
464void FullCodeGenerator::StackValueContext::PlugTOS() const {
465}
466
467
468void FullCodeGenerator::TestContext::PlugTOS() const {
469 // For simplicity we always test the accumulator register.
470 __ pop(result_register());
vegorov@chromium.org7943d462011-08-01 11:41:52 +0000471 codegen()->decrement_stack_height();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000472 codegen()->PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000473 codegen()->DoTest(this);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000474}
475
476
477void FullCodeGenerator::EffectContext::PrepareTest(
478 Label* materialize_true,
479 Label* materialize_false,
480 Label** if_true,
481 Label** if_false,
482 Label** fall_through) const {
483 // In an effect context, the true and the false case branch to the
484 // same label.
485 *if_true = *if_false = *fall_through = materialize_true;
486}
487
488
489void FullCodeGenerator::AccumulatorValueContext::PrepareTest(
490 Label* materialize_true,
491 Label* materialize_false,
492 Label** if_true,
493 Label** if_false,
494 Label** fall_through) const {
495 *if_true = *fall_through = materialize_true;
496 *if_false = materialize_false;
497}
498
499
500void FullCodeGenerator::StackValueContext::PrepareTest(
501 Label* materialize_true,
502 Label* materialize_false,
503 Label** if_true,
504 Label** if_false,
505 Label** fall_through) const {
506 *if_true = *fall_through = materialize_true;
507 *if_false = materialize_false;
508}
509
510
511void FullCodeGenerator::TestContext::PrepareTest(
512 Label* materialize_true,
513 Label* materialize_false,
514 Label** if_true,
515 Label** if_false,
516 Label** fall_through) const {
517 *if_true = true_label_;
518 *if_false = false_label_;
519 *fall_through = fall_through_;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000520}
521
522
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000523void FullCodeGenerator::DoTest(const TestContext* context) {
524 DoTest(context->condition(),
525 context->true_label(),
526 context->false_label(),
527 context->fall_through());
528}
529
530
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000531void FullCodeGenerator::VisitDeclarations(
532 ZoneList<Declaration*>* declarations) {
533 int length = declarations->length();
534 int globals = 0;
535 for (int i = 0; i < length; i++) {
536 Declaration* decl = declarations->at(i);
537 Variable* var = decl->proxy()->var();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000538 Slot* slot = var->AsSlot();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000539
540 // If it was not possible to allocate the variable at compile
541 // time, we need to "declare" it at runtime to make sure it
542 // actually exists in the local context.
543 if ((slot != NULL && slot->type() == Slot::LOOKUP) || !var->is_global()) {
544 VisitDeclaration(decl);
545 } else {
546 // Count global variables and functions for later processing
547 globals++;
548 }
549 }
550
551 // Compute array of global variable and function declarations.
552 // Do nothing in case of no declared global functions or variables.
553 if (globals > 0) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000554 Handle<FixedArray> array =
555 isolate()->factory()->NewFixedArray(2 * globals, TENURED);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000556 for (int j = 0, i = 0; i < length; i++) {
557 Declaration* decl = declarations->at(i);
558 Variable* var = decl->proxy()->var();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000559 Slot* slot = var->AsSlot();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000560
561 if ((slot == NULL || slot->type() != Slot::LOOKUP) && var->is_global()) {
562 array->set(j++, *(var->name()));
563 if (decl->fun() == NULL) {
564 if (var->mode() == Variable::CONST) {
565 // In case this is const property use the hole.
566 array->set_the_hole(j++);
567 } else {
568 array->set_undefined(j++);
569 }
570 } else {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000571 Handle<SharedFunctionInfo> function =
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000572 Compiler::BuildFunctionInfo(decl->fun(), script());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000573 // Check for stack-overflow exception.
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000574 if (function.is_null()) {
575 SetStackOverflow();
576 return;
577 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000578 array->set(j++, *function);
579 }
580 }
581 }
582 // Invoke the platform-dependent code generator to do the actual
583 // declaration the global variables and functions.
584 DeclareGlobals(array);
585 }
586}
587
588
589void FullCodeGenerator::SetFunctionPosition(FunctionLiteral* fun) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000590 CodeGenerator::RecordPositions(masm_, fun->start_position());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000591}
592
593
594void FullCodeGenerator::SetReturnPosition(FunctionLiteral* fun) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000595 CodeGenerator::RecordPositions(masm_, fun->end_position() - 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000596}
597
598
599void FullCodeGenerator::SetStatementPosition(Statement* stmt) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000600#ifdef ENABLE_DEBUGGER_SUPPORT
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000601 if (!isolate()->debugger()->IsDebuggerActive()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000602 CodeGenerator::RecordPositions(masm_, stmt->statement_pos());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000603 } else {
604 // Check if the statement will be breakable without adding a debug break
605 // slot.
606 BreakableStatementChecker checker;
607 checker.Check(stmt);
608 // Record the statement position right here if the statement is not
609 // breakable. For breakable statements the actual recording of the
610 // position will be postponed to the breakable code (typically an IC).
611 bool position_recorded = CodeGenerator::RecordPositions(
612 masm_, stmt->statement_pos(), !checker.is_breakable());
613 // If the position recording did record a new position generate a debug
614 // break slot to make the statement breakable.
615 if (position_recorded) {
616 Debug::GenerateSlot(masm_);
617 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000618 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000619#else
620 CodeGenerator::RecordPositions(masm_, stmt->statement_pos());
621#endif
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000622}
623
624
625void FullCodeGenerator::SetExpressionPosition(Expression* expr, int pos) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000626#ifdef ENABLE_DEBUGGER_SUPPORT
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000627 if (!isolate()->debugger()->IsDebuggerActive()) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000628 CodeGenerator::RecordPositions(masm_, pos);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000629 } else {
630 // Check if the expression will be breakable without adding a debug break
631 // slot.
632 BreakableStatementChecker checker;
633 checker.Check(expr);
634 // Record a statement position right here if the expression is not
635 // breakable. For breakable expressions the actual recording of the
636 // position will be postponed to the breakable code (typically an IC).
637 // NOTE this will record a statement position for something which might
638 // not be a statement. As stepping in the debugger will only stop at
639 // statement positions this is used for e.g. the condition expression of
640 // a do while loop.
641 bool position_recorded = CodeGenerator::RecordPositions(
642 masm_, pos, !checker.is_breakable());
643 // If the position recording did record a new position generate a debug
644 // break slot to make the statement breakable.
645 if (position_recorded) {
646 Debug::GenerateSlot(masm_);
647 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000648 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000649#else
650 CodeGenerator::RecordPositions(masm_, pos);
651#endif
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000652}
653
654
655void FullCodeGenerator::SetStatementPosition(int pos) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000656 CodeGenerator::RecordPositions(masm_, pos);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000657}
658
659
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000660void FullCodeGenerator::SetSourcePosition(int pos) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000661 if (pos != RelocInfo::kNoPosition) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000662 masm_->positions_recorder()->RecordPosition(pos);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000663 }
664}
665
666
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000667// Lookup table for code generators for special runtime calls which are
668// generated inline.
669#define INLINE_FUNCTION_GENERATOR_ADDRESS(Name, argc, ressize) \
670 &FullCodeGenerator::Emit##Name,
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000671
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000672const FullCodeGenerator::InlineFunctionGenerator
673 FullCodeGenerator::kInlineFunctionGenerators[] = {
674 INLINE_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS)
675 INLINE_RUNTIME_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS)
676 };
677#undef INLINE_FUNCTION_GENERATOR_ADDRESS
678
679
680FullCodeGenerator::InlineFunctionGenerator
681 FullCodeGenerator::FindInlineFunctionGenerator(Runtime::FunctionId id) {
whesse@chromium.org023421e2010-12-21 12:19:12 +0000682 int lookup_index =
683 static_cast<int>(id) - static_cast<int>(Runtime::kFirstInlineFunction);
684 ASSERT(lookup_index >= 0);
685 ASSERT(static_cast<size_t>(lookup_index) <
686 ARRAY_SIZE(kInlineFunctionGenerators));
687 return kInlineFunctionGenerators[lookup_index];
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000688}
689
690
691void FullCodeGenerator::EmitInlineRuntimeCall(CallRuntime* node) {
692 ZoneList<Expression*>* args = node->arguments();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000693 const Runtime::Function* function = node->function();
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000694 ASSERT(function != NULL);
695 ASSERT(function->intrinsic_type == Runtime::INLINE);
696 InlineFunctionGenerator generator =
697 FindInlineFunctionGenerator(function->function_id);
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000698 ((*this).*(generator))(args);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000699}
700
701
702void FullCodeGenerator::VisitBinaryOperation(BinaryOperation* expr) {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000703 switch (expr->op()) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000704 case Token::COMMA:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000705 return VisitComma(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000706 case Token::OR:
707 case Token::AND:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000708 return VisitLogicalExpression(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000709 default:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000710 return VisitArithmeticExpression(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000711 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000712}
713
714
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000715void FullCodeGenerator::VisitComma(BinaryOperation* expr) {
716 Comment cmnt(masm_, "[ Comma");
717 VisitForEffect(expr->left());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000718 if (context()->IsTest()) ForwardBailoutToChild(expr);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000719 VisitInCurrentContext(expr->right());
720}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000721
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000722
723void FullCodeGenerator::VisitLogicalExpression(BinaryOperation* expr) {
724 bool is_logical_and = expr->op() == Token::AND;
725 Comment cmnt(masm_, is_logical_and ? "[ Logical AND" : "[ Logical OR");
726 Expression* left = expr->left();
727 Expression* right = expr->right();
728 int right_id = expr->RightId();
729 Label done;
730
731 if (context()->IsTest()) {
732 Label eval_right;
733 const TestContext* test = TestContext::cast(context());
734 if (is_logical_and) {
735 VisitForControl(left, &eval_right, test->false_label(), &eval_right);
736 } else {
737 VisitForControl(left, test->true_label(), &eval_right, &eval_right);
738 }
739 PrepareForBailoutForId(right_id, NO_REGISTERS);
740 __ bind(&eval_right);
741 ForwardBailoutToChild(expr);
742
743 } else if (context()->IsAccumulatorValue()) {
744 VisitForAccumulatorValue(left);
745 // We want the value in the accumulator for the test, and on the stack in
746 // case we need it.
747 __ push(result_register());
748 Label discard, restore;
749 PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
750 if (is_logical_and) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000751 DoTest(left, &discard, &restore, &restore);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000752 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000753 DoTest(left, &restore, &discard, &restore);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000754 }
755 __ bind(&restore);
756 __ pop(result_register());
757 __ jmp(&done);
758 __ bind(&discard);
759 __ Drop(1);
760 PrepareForBailoutForId(right_id, NO_REGISTERS);
761
762 } else if (context()->IsStackValue()) {
763 VisitForAccumulatorValue(left);
764 // We want the value in the accumulator for the test, and on the stack in
765 // case we need it.
766 __ push(result_register());
767 Label discard;
768 PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
769 if (is_logical_and) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000770 DoTest(left, &discard, &done, &discard);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000771 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000772 DoTest(left, &done, &discard, &discard);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000773 }
774 __ bind(&discard);
775 __ Drop(1);
776 PrepareForBailoutForId(right_id, NO_REGISTERS);
777
778 } else {
779 ASSERT(context()->IsEffect());
780 Label eval_right;
781 if (is_logical_and) {
782 VisitForControl(left, &eval_right, &done, &eval_right);
783 } else {
784 VisitForControl(left, &done, &eval_right, &eval_right);
785 }
786 PrepareForBailoutForId(right_id, NO_REGISTERS);
787 __ bind(&eval_right);
788 }
789
790 VisitInCurrentContext(right);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000791 __ bind(&done);
792}
793
794
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000795void FullCodeGenerator::VisitArithmeticExpression(BinaryOperation* expr) {
796 Token::Value op = expr->op();
797 Comment cmnt(masm_, "[ ArithmeticExpression");
798 Expression* left = expr->left();
799 Expression* right = expr->right();
800 OverwriteMode mode =
801 left->ResultOverwriteAllowed()
802 ? OVERWRITE_LEFT
803 : (right->ResultOverwriteAllowed() ? OVERWRITE_RIGHT : NO_OVERWRITE);
804
805 VisitForStackValue(left);
806 VisitForAccumulatorValue(right);
807
808 SetSourcePosition(expr->position());
809 if (ShouldInlineSmiCase(op)) {
810 EmitInlineSmiBinaryOp(expr, op, mode, left, right);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000811 } else {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000812 EmitBinaryOp(expr, op, mode);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000813 }
814}
815
816
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000817void FullCodeGenerator::ForwardBailoutToChild(Expression* expr) {
818 if (!info_->HasDeoptimizationSupport()) return;
819 ASSERT(context()->IsTest());
820 ASSERT(expr == forward_bailout_stack_->expr());
821 forward_bailout_pending_ = forward_bailout_stack_;
822}
823
824
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000825void FullCodeGenerator::VisitInCurrentContext(Expression* expr) {
826 if (context()->IsTest()) {
827 ForwardBailoutStack stack(expr, forward_bailout_pending_);
828 ForwardBailoutStack* saved = forward_bailout_stack_;
829 forward_bailout_pending_ = NULL;
830 forward_bailout_stack_ = &stack;
831 Visit(expr);
832 forward_bailout_stack_ = saved;
833 } else {
834 ASSERT(forward_bailout_pending_ == NULL);
835 Visit(expr);
836 State state = context()->IsAccumulatorValue() ? TOS_REG : NO_REGISTERS;
837 PrepareForBailout(expr, state);
838 // Forwarding bailouts to children is a one shot operation. It should have
839 // been processed at this point.
840 ASSERT(forward_bailout_pending_ == NULL);
841 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000842}
843
844
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000845void FullCodeGenerator::VisitBlock(Block* stmt) {
846 Comment cmnt(masm_, "[ Block");
847 Breakable nested_statement(this, stmt);
848 SetStatementPosition(stmt);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000849
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +0000850 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000851 VisitStatements(stmt->statements());
852 __ bind(nested_statement.break_target());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000853 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000854}
855
856
857void FullCodeGenerator::VisitExpressionStatement(ExpressionStatement* stmt) {
858 Comment cmnt(masm_, "[ ExpressionStatement");
859 SetStatementPosition(stmt);
860 VisitForEffect(stmt->expression());
861}
862
863
864void FullCodeGenerator::VisitEmptyStatement(EmptyStatement* stmt) {
865 Comment cmnt(masm_, "[ EmptyStatement");
866 SetStatementPosition(stmt);
867}
868
869
870void FullCodeGenerator::VisitIfStatement(IfStatement* stmt) {
871 Comment cmnt(masm_, "[ IfStatement");
872 SetStatementPosition(stmt);
873 Label then_part, else_part, done;
874
ricow@chromium.org65fae842010-08-25 15:26:24 +0000875 if (stmt->HasElseStatement()) {
876 VisitForControl(stmt->condition(), &then_part, &else_part, &then_part);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000877 PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000878 __ bind(&then_part);
879 Visit(stmt->then_statement());
880 __ jmp(&done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000881
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000882 PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000883 __ bind(&else_part);
884 Visit(stmt->else_statement());
885 } else {
886 VisitForControl(stmt->condition(), &then_part, &done, &then_part);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000887 PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000888 __ bind(&then_part);
889 Visit(stmt->then_statement());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000890
891 PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000892 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000893 __ bind(&done);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000894 PrepareForBailoutForId(stmt->IfId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000895}
896
897
898void FullCodeGenerator::VisitContinueStatement(ContinueStatement* stmt) {
899 Comment cmnt(masm_, "[ ContinueStatement");
900 SetStatementPosition(stmt);
901 NestedStatement* current = nesting_stack_;
902 int stack_depth = 0;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000903 // When continuing, we clobber the unpredictable value in the accumulator
904 // with one that's safe for GC. If we hit an exit from the try block of
905 // try...finally on our way out, we will unconditionally preserve the
906 // accumulator on the stack.
907 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000908 while (!current->IsContinueTarget(stmt->target())) {
909 stack_depth = current->Exit(stack_depth);
910 current = current->outer();
911 }
912 __ Drop(stack_depth);
913
914 Iteration* loop = current->AsIteration();
915 __ jmp(loop->continue_target());
916}
917
918
919void FullCodeGenerator::VisitBreakStatement(BreakStatement* stmt) {
920 Comment cmnt(masm_, "[ BreakStatement");
921 SetStatementPosition(stmt);
922 NestedStatement* current = nesting_stack_;
923 int stack_depth = 0;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000924 // When breaking, we clobber the unpredictable value in the accumulator
925 // with one that's safe for GC. If we hit an exit from the try block of
926 // try...finally on our way out, we will unconditionally preserve the
927 // accumulator on the stack.
928 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000929 while (!current->IsBreakTarget(stmt->target())) {
930 stack_depth = current->Exit(stack_depth);
931 current = current->outer();
932 }
933 __ Drop(stack_depth);
934
935 Breakable* target = current->AsBreakable();
936 __ jmp(target->break_target());
937}
938
939
940void FullCodeGenerator::VisitReturnStatement(ReturnStatement* stmt) {
941 Comment cmnt(masm_, "[ ReturnStatement");
942 SetStatementPosition(stmt);
943 Expression* expr = stmt->expression();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000944 VisitForAccumulatorValue(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000945
946 // Exit all nested statements.
947 NestedStatement* current = nesting_stack_;
948 int stack_depth = 0;
949 while (current != NULL) {
950 stack_depth = current->Exit(stack_depth);
951 current = current->outer();
952 }
953 __ Drop(stack_depth);
954
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000955 EmitReturnSequence();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000956}
957
958
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000959void FullCodeGenerator::VisitEnterWithContextStatement(
960 EnterWithContextStatement* stmt) {
961 Comment cmnt(masm_, "[ EnterWithContextStatement");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000962 SetStatementPosition(stmt);
963
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000964 VisitForStackValue(stmt->expression());
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000965 PushFunctionArgumentForContextAllocation();
966 __ CallRuntime(Runtime::kPushWithContext, 2);
vegorov@chromium.org7943d462011-08-01 11:41:52 +0000967 decrement_stack_height();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000968 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
969}
970
971
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000972void FullCodeGenerator::VisitExitContextStatement(ExitContextStatement* stmt) {
973 Comment cmnt(masm_, "[ ExitContextStatement");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000974 SetStatementPosition(stmt);
975
976 // Pop context.
977 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
978 // Update local stack frame context field.
979 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
980}
981
982
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000983void FullCodeGenerator::VisitDoWhileStatement(DoWhileStatement* stmt) {
984 Comment cmnt(masm_, "[ DoWhileStatement");
985 SetStatementPosition(stmt);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000986 Label body, stack_check;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000987
988 Iteration loop_statement(this, stmt);
989 increment_loop_depth();
990
991 __ bind(&body);
992 Visit(stmt->body());
993
ricow@chromium.org65fae842010-08-25 15:26:24 +0000994 // Record the position of the do while condition and make sure it is
995 // possible to break on the condition.
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000996 __ bind(loop_statement.continue_target());
997 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000998 SetExpressionPosition(stmt->cond(), stmt->condition_position());
ricow@chromium.org65fae842010-08-25 15:26:24 +0000999 VisitForControl(stmt->cond(),
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001000 &stack_check,
ricow@chromium.org65fae842010-08-25 15:26:24 +00001001 loop_statement.break_target(),
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001002 &stack_check);
1003
1004 // Check stack before looping.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001005 PrepareForBailoutForId(stmt->BackEdgeId(), NO_REGISTERS);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001006 __ bind(&stack_check);
1007 EmitStackCheck(stmt);
1008 __ jmp(&body);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001009
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001010 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001011 __ bind(loop_statement.break_target());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001012 decrement_loop_depth();
1013}
1014
1015
1016void FullCodeGenerator::VisitWhileStatement(WhileStatement* stmt) {
1017 Comment cmnt(masm_, "[ WhileStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001018 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001019
1020 Iteration loop_statement(this, stmt);
1021 increment_loop_depth();
1022
1023 // Emit the test at the bottom of the loop.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001024 __ jmp(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001025
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001026 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001027 __ bind(&body);
1028 Visit(stmt->body());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001029
1030 // Emit the statement position here as this is where the while
1031 // statement code starts.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001032 __ bind(loop_statement.continue_target());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001033 SetStatementPosition(stmt);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001034
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001035 // Check stack before looping.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001036 EmitStackCheck(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001037
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001038 __ bind(&test);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001039 VisitForControl(stmt->cond(),
1040 &body,
1041 loop_statement.break_target(),
1042 loop_statement.break_target());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001043
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001044 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001045 __ bind(loop_statement.break_target());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001046 decrement_loop_depth();
1047}
1048
1049
1050void FullCodeGenerator::VisitForStatement(ForStatement* stmt) {
1051 Comment cmnt(masm_, "[ ForStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001052 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001053
1054 Iteration loop_statement(this, stmt);
1055 if (stmt->init() != NULL) {
1056 Visit(stmt->init());
1057 }
1058
1059 increment_loop_depth();
1060 // Emit the test at the bottom of the loop (even if empty).
1061 __ jmp(&test);
1062
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());
1066
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001067 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001068 __ bind(loop_statement.continue_target());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001069 SetStatementPosition(stmt);
1070 if (stmt->next() != NULL) {
1071 Visit(stmt->next());
1072 }
1073
ricow@chromium.org65fae842010-08-25 15:26:24 +00001074 // Emit the statement position here as this is where the for
1075 // statement code starts.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001076 SetStatementPosition(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001077
1078 // Check stack before looping.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001079 EmitStackCheck(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001080
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001081 __ bind(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001082 if (stmt->cond() != NULL) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00001083 VisitForControl(stmt->cond(),
1084 &body,
1085 loop_statement.break_target(),
1086 loop_statement.break_target());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001087 } else {
1088 __ jmp(&body);
1089 }
1090
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001091 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001092 __ bind(loop_statement.break_target());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001093 decrement_loop_depth();
1094}
1095
1096
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001097void FullCodeGenerator::VisitTryCatchStatement(TryCatchStatement* stmt) {
1098 Comment cmnt(masm_, "[ TryCatchStatement");
1099 SetStatementPosition(stmt);
1100 // The try block adds a handler to the exception handler chain
1101 // before entering, and removes it again when exiting normally.
1102 // If an exception is thrown during execution of the try block,
1103 // control is passed to the handler, which also consumes the handler.
1104 // At this point, the exception is in a register, and store it in
1105 // the temporary local variable (prints as ".catch-var") before
1106 // executing the catch block. The catch block has been rewritten
1107 // to introduce a new scope to bind the catch variable and to remove
1108 // that scope again afterwards.
1109
1110 Label try_handler_setup, catch_entry, done;
1111 __ Call(&try_handler_setup);
1112 // Try handler code, exception in result register.
1113
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001114 // Extend the context before executing the catch block.
1115 { Comment cmnt(masm_, "[ Extend catch context");
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001116 __ Push(stmt->variable()->name());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001117 __ push(result_register());
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00001118 PushFunctionArgumentForContextAllocation();
1119 __ CallRuntime(Runtime::kPushCatchContext, 3);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001120 StoreToFrameField(StandardFrameConstants::kContextOffset,
1121 context_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001122 }
1123
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001124 Scope* saved_scope = scope();
1125 scope_ = stmt->scope();
1126 ASSERT(scope_->declarations()->is_empty());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001127 Visit(stmt->catch_block());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001128 scope_ = saved_scope;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001129 __ jmp(&done);
1130
1131 // Try block code. Sets up the exception handler chain.
1132 __ bind(&try_handler_setup);
1133 {
1134 TryCatch try_block(this, &catch_entry);
1135 __ PushTryHandler(IN_JAVASCRIPT, TRY_CATCH_HANDLER);
vegorov@chromium.org7943d462011-08-01 11:41:52 +00001136 increment_stack_height(StackHandlerConstants::kSize / kPointerSize);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001137 Visit(stmt->try_block());
1138 __ PopTryHandler();
vegorov@chromium.org7943d462011-08-01 11:41:52 +00001139 decrement_stack_height(StackHandlerConstants::kSize / kPointerSize);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001140 }
1141 __ bind(&done);
1142}
1143
1144
1145void FullCodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* stmt) {
1146 Comment cmnt(masm_, "[ TryFinallyStatement");
1147 SetStatementPosition(stmt);
1148 // Try finally is compiled by setting up a try-handler on the stack while
1149 // executing the try body, and removing it again afterwards.
1150 //
1151 // The try-finally construct can enter the finally block in three ways:
1152 // 1. By exiting the try-block normally. This removes the try-handler and
1153 // calls the finally block code before continuing.
1154 // 2. By exiting the try-block with a function-local control flow transfer
1155 // (break/continue/return). The site of the, e.g., break removes the
1156 // try handler and calls the finally block code before continuing
1157 // its outward control transfer.
1158 // 3. by exiting the try-block with a thrown exception.
1159 // This can happen in nested function calls. It traverses the try-handler
1160 // chain and consumes the try-handler entry before jumping to the
1161 // handler code. The handler code then calls the finally-block before
1162 // rethrowing the exception.
1163 //
1164 // The finally block must assume a return address on top of the stack
1165 // (or in the link register on ARM chips) and a value (return value or
1166 // exception) in the result register (rax/eax/r0), both of which must
1167 // be preserved. The return address isn't GC-safe, so it should be
1168 // cooked before GC.
1169 Label finally_entry;
1170 Label try_handler_setup;
vegorov@chromium.org7943d462011-08-01 11:41:52 +00001171 const int original_stack_height = stack_height();
1172 const int finally_block_stack_height = original_stack_height + 2;
1173 const int try_block_stack_height = original_stack_height + 4;
1174 STATIC_ASSERT(StackHandlerConstants::kSize / kPointerSize == 4);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001175
1176 // Setup the try-handler chain. Use a call to
1177 // Jump to try-handler setup and try-block code. Use call to put try-handler
1178 // address on stack.
1179 __ Call(&try_handler_setup);
1180 // Try handler code. Return address of call is pushed on handler stack.
1181 {
1182 // This code is only executed during stack-handler traversal when an
1183 // exception is thrown. The execption is in the result register, which
1184 // is retained by the finally block.
1185 // Call the finally block and then rethrow the exception.
1186 __ Call(&finally_entry);
1187 __ push(result_register());
1188 __ CallRuntime(Runtime::kReThrow, 1);
1189 }
1190
1191 __ bind(&finally_entry);
1192 {
1193 // Finally block implementation.
1194 Finally finally_block(this);
1195 EnterFinallyBlock();
vegorov@chromium.org7943d462011-08-01 11:41:52 +00001196 set_stack_height(finally_block_stack_height);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001197 Visit(stmt->finally_block());
1198 ExitFinallyBlock(); // Return to the calling code.
1199 }
1200
1201 __ bind(&try_handler_setup);
1202 {
1203 // Setup try handler (stack pointer registers).
1204 TryFinally try_block(this, &finally_entry);
1205 __ PushTryHandler(IN_JAVASCRIPT, TRY_FINALLY_HANDLER);
vegorov@chromium.org7943d462011-08-01 11:41:52 +00001206 set_stack_height(try_block_stack_height);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001207 Visit(stmt->try_block());
1208 __ PopTryHandler();
vegorov@chromium.org7943d462011-08-01 11:41:52 +00001209 set_stack_height(original_stack_height);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001210 }
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001211 // Execute the finally block on the way out. Clobber the unpredictable
1212 // value in the accumulator with one that's safe for GC. The finally
1213 // block will unconditionally preserve the accumulator on the stack.
1214 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001215 __ Call(&finally_entry);
1216}
1217
1218
1219void FullCodeGenerator::VisitDebuggerStatement(DebuggerStatement* stmt) {
1220#ifdef ENABLE_DEBUGGER_SUPPORT
1221 Comment cmnt(masm_, "[ DebuggerStatement");
1222 SetStatementPosition(stmt);
1223
ager@chromium.org5c838252010-02-19 08:53:10 +00001224 __ DebugBreak();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001225 // Ignore the return value.
1226#endif
1227}
1228
1229
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001230void FullCodeGenerator::VisitConditional(Conditional* expr) {
1231 Comment cmnt(masm_, "[ Conditional");
1232 Label true_case, false_case, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001233 VisitForControl(expr->condition(), &true_case, &false_case, &true_case);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001234
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001235 PrepareForBailoutForId(expr->ThenId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001236 __ bind(&true_case);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001237 SetExpressionPosition(expr->then_expression(),
1238 expr->then_expression_position());
vegorov@chromium.org7943d462011-08-01 11:41:52 +00001239 int start_stack_height = stack_height();
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001240 if (context()->IsTest()) {
1241 const TestContext* for_test = TestContext::cast(context());
1242 VisitForControl(expr->then_expression(),
1243 for_test->true_label(),
1244 for_test->false_label(),
1245 NULL);
1246 } else {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001247 VisitInCurrentContext(expr->then_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001248 __ jmp(&done);
1249 }
1250
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001251 PrepareForBailoutForId(expr->ElseId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001252 __ bind(&false_case);
vegorov@chromium.org7943d462011-08-01 11:41:52 +00001253 set_stack_height(start_stack_height);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001254 if (context()->IsTest()) ForwardBailoutToChild(expr);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001255 SetExpressionPosition(expr->else_expression(),
1256 expr->else_expression_position());
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001257 VisitInCurrentContext(expr->else_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001258 // If control flow falls through Visit, merge it with true case here.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001259 if (!context()->IsTest()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001260 __ bind(&done);
1261 }
1262}
1263
1264
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001265void FullCodeGenerator::VisitLiteral(Literal* expr) {
1266 Comment cmnt(masm_, "[ Literal");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001267 context()->Plug(expr->handle());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001268}
1269
1270
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001271void FullCodeGenerator::VisitFunctionLiteral(FunctionLiteral* expr) {
1272 Comment cmnt(masm_, "[ FunctionLiteral");
1273
1274 // Build the function boilerplate and instantiate it.
1275 Handle<SharedFunctionInfo> function_info =
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001276 Compiler::BuildFunctionInfo(expr, script());
1277 if (function_info.is_null()) {
1278 SetStackOverflow();
1279 return;
1280 }
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001281 EmitNewClosure(function_info, expr->pretenure());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001282}
1283
1284
1285void FullCodeGenerator::VisitSharedFunctionInfoLiteral(
1286 SharedFunctionInfoLiteral* expr) {
1287 Comment cmnt(masm_, "[ SharedFunctionInfoLiteral");
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001288 EmitNewClosure(expr->shared_function_info(), false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001289}
1290
1291
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001292void FullCodeGenerator::VisitThrow(Throw* expr) {
1293 Comment cmnt(masm_, "[ Throw");
vegorov@chromium.org7943d462011-08-01 11:41:52 +00001294 // Throw has no effect on the stack height or the current expression context.
1295 // Usually the expression context is null, because throw is a statement.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001296 VisitForStackValue(expr->exception());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001297 __ CallRuntime(Runtime::kThrow, 1);
vegorov@chromium.org7943d462011-08-01 11:41:52 +00001298 decrement_stack_height();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001299 // Never returns here.
1300}
1301
1302
1303int FullCodeGenerator::TryFinally::Exit(int stack_depth) {
1304 // The macros used here must preserve the result register.
1305 __ Drop(stack_depth);
1306 __ PopTryHandler();
1307 __ Call(finally_entry_);
1308 return 0;
1309}
1310
1311
1312int FullCodeGenerator::TryCatch::Exit(int stack_depth) {
1313 // The macros used here must preserve the result register.
1314 __ Drop(stack_depth);
1315 __ PopTryHandler();
1316 return 0;
1317}
1318
ricow@chromium.org65fae842010-08-25 15:26:24 +00001319
ager@chromium.org04921a82011-06-27 13:21:41 +00001320bool FullCodeGenerator::TryLiteralCompare(CompareOperation* compare,
1321 Label* if_true,
1322 Label* if_false,
1323 Label* fall_through) {
1324 Expression *expr;
1325 Handle<String> check;
1326 if (compare->IsLiteralCompareTypeof(&expr, &check)) {
1327 EmitLiteralCompareTypeof(expr, check, if_true, if_false, fall_through);
1328 return true;
1329 }
1330
1331 if (compare->IsLiteralCompareUndefined(&expr)) {
1332 EmitLiteralCompareUndefined(expr, if_true, if_false, fall_through);
1333 return true;
1334 }
1335
1336 return false;
1337}
1338
1339
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001340#undef __
1341
1342
1343} } // namespace v8::internal