blob: 0b91f544689ef60efe1dc5eb772bbd6fbb15ed67 [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);
440}
441
442
443void FullCodeGenerator::TestContext::Plug(Register reg) const {
444 // For simplicity we always test the accumulator register.
445 __ Move(result_register(), reg);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000446 codegen()->PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000447 codegen()->DoTest(this);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000448}
449
450
451void FullCodeGenerator::EffectContext::PlugTOS() const {
452 __ Drop(1);
453}
454
455
456void FullCodeGenerator::AccumulatorValueContext::PlugTOS() const {
457 __ pop(result_register());
458}
459
460
461void FullCodeGenerator::StackValueContext::PlugTOS() const {
462}
463
464
465void FullCodeGenerator::TestContext::PlugTOS() const {
466 // For simplicity we always test the accumulator register.
467 __ pop(result_register());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000468 codegen()->PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000469 codegen()->DoTest(this);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000470}
471
472
473void FullCodeGenerator::EffectContext::PrepareTest(
474 Label* materialize_true,
475 Label* materialize_false,
476 Label** if_true,
477 Label** if_false,
478 Label** fall_through) const {
479 // In an effect context, the true and the false case branch to the
480 // same label.
481 *if_true = *if_false = *fall_through = materialize_true;
482}
483
484
485void FullCodeGenerator::AccumulatorValueContext::PrepareTest(
486 Label* materialize_true,
487 Label* materialize_false,
488 Label** if_true,
489 Label** if_false,
490 Label** fall_through) const {
491 *if_true = *fall_through = materialize_true;
492 *if_false = materialize_false;
493}
494
495
496void FullCodeGenerator::StackValueContext::PrepareTest(
497 Label* materialize_true,
498 Label* materialize_false,
499 Label** if_true,
500 Label** if_false,
501 Label** fall_through) const {
502 *if_true = *fall_through = materialize_true;
503 *if_false = materialize_false;
504}
505
506
507void FullCodeGenerator::TestContext::PrepareTest(
508 Label* materialize_true,
509 Label* materialize_false,
510 Label** if_true,
511 Label** if_false,
512 Label** fall_through) const {
513 *if_true = true_label_;
514 *if_false = false_label_;
515 *fall_through = fall_through_;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000516}
517
518
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000519void FullCodeGenerator::DoTest(const TestContext* context) {
520 DoTest(context->condition(),
521 context->true_label(),
522 context->false_label(),
523 context->fall_through());
524}
525
526
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000527void FullCodeGenerator::VisitDeclarations(
528 ZoneList<Declaration*>* declarations) {
529 int length = declarations->length();
530 int globals = 0;
531 for (int i = 0; i < length; i++) {
532 Declaration* decl = declarations->at(i);
533 Variable* var = decl->proxy()->var();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000534 Slot* slot = var->AsSlot();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000535
536 // If it was not possible to allocate the variable at compile
537 // time, we need to "declare" it at runtime to make sure it
538 // actually exists in the local context.
539 if ((slot != NULL && slot->type() == Slot::LOOKUP) || !var->is_global()) {
540 VisitDeclaration(decl);
541 } else {
542 // Count global variables and functions for later processing
543 globals++;
544 }
545 }
546
547 // Compute array of global variable and function declarations.
548 // Do nothing in case of no declared global functions or variables.
549 if (globals > 0) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000550 Handle<FixedArray> array =
551 isolate()->factory()->NewFixedArray(2 * globals, TENURED);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000552 for (int j = 0, i = 0; i < length; i++) {
553 Declaration* decl = declarations->at(i);
554 Variable* var = decl->proxy()->var();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000555 Slot* slot = var->AsSlot();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000556
557 if ((slot == NULL || slot->type() != Slot::LOOKUP) && var->is_global()) {
558 array->set(j++, *(var->name()));
559 if (decl->fun() == NULL) {
560 if (var->mode() == Variable::CONST) {
561 // In case this is const property use the hole.
562 array->set_the_hole(j++);
563 } else {
564 array->set_undefined(j++);
565 }
566 } else {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000567 Handle<SharedFunctionInfo> function =
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000568 Compiler::BuildFunctionInfo(decl->fun(), script());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000569 // Check for stack-overflow exception.
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000570 if (function.is_null()) {
571 SetStackOverflow();
572 return;
573 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000574 array->set(j++, *function);
575 }
576 }
577 }
578 // Invoke the platform-dependent code generator to do the actual
579 // declaration the global variables and functions.
580 DeclareGlobals(array);
581 }
582}
583
584
585void FullCodeGenerator::SetFunctionPosition(FunctionLiteral* fun) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000586 CodeGenerator::RecordPositions(masm_, fun->start_position());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000587}
588
589
590void FullCodeGenerator::SetReturnPosition(FunctionLiteral* fun) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000591 CodeGenerator::RecordPositions(masm_, fun->end_position() - 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000592}
593
594
595void FullCodeGenerator::SetStatementPosition(Statement* stmt) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000596#ifdef ENABLE_DEBUGGER_SUPPORT
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000597 if (!isolate()->debugger()->IsDebuggerActive()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000598 CodeGenerator::RecordPositions(masm_, stmt->statement_pos());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000599 } else {
600 // Check if the statement will be breakable without adding a debug break
601 // slot.
602 BreakableStatementChecker checker;
603 checker.Check(stmt);
604 // Record the statement position right here if the statement is not
605 // breakable. For breakable statements the actual recording of the
606 // position will be postponed to the breakable code (typically an IC).
607 bool position_recorded = CodeGenerator::RecordPositions(
608 masm_, stmt->statement_pos(), !checker.is_breakable());
609 // If the position recording did record a new position generate a debug
610 // break slot to make the statement breakable.
611 if (position_recorded) {
612 Debug::GenerateSlot(masm_);
613 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000614 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000615#else
616 CodeGenerator::RecordPositions(masm_, stmt->statement_pos());
617#endif
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000618}
619
620
621void FullCodeGenerator::SetExpressionPosition(Expression* expr, int pos) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000622#ifdef ENABLE_DEBUGGER_SUPPORT
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000623 if (!isolate()->debugger()->IsDebuggerActive()) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000624 CodeGenerator::RecordPositions(masm_, pos);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000625 } else {
626 // Check if the expression will be breakable without adding a debug break
627 // slot.
628 BreakableStatementChecker checker;
629 checker.Check(expr);
630 // Record a statement position right here if the expression is not
631 // breakable. For breakable expressions the actual recording of the
632 // position will be postponed to the breakable code (typically an IC).
633 // NOTE this will record a statement position for something which might
634 // not be a statement. As stepping in the debugger will only stop at
635 // statement positions this is used for e.g. the condition expression of
636 // a do while loop.
637 bool position_recorded = CodeGenerator::RecordPositions(
638 masm_, pos, !checker.is_breakable());
639 // If the position recording did record a new position generate a debug
640 // break slot to make the statement breakable.
641 if (position_recorded) {
642 Debug::GenerateSlot(masm_);
643 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000644 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000645#else
646 CodeGenerator::RecordPositions(masm_, pos);
647#endif
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000648}
649
650
651void FullCodeGenerator::SetStatementPosition(int pos) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000652 CodeGenerator::RecordPositions(masm_, pos);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000653}
654
655
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000656void FullCodeGenerator::SetSourcePosition(int pos) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000657 if (pos != RelocInfo::kNoPosition) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000658 masm_->positions_recorder()->RecordPosition(pos);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000659 }
660}
661
662
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000663// Lookup table for code generators for special runtime calls which are
664// generated inline.
665#define INLINE_FUNCTION_GENERATOR_ADDRESS(Name, argc, ressize) \
666 &FullCodeGenerator::Emit##Name,
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000667
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000668const FullCodeGenerator::InlineFunctionGenerator
669 FullCodeGenerator::kInlineFunctionGenerators[] = {
670 INLINE_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS)
671 INLINE_RUNTIME_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS)
672 };
673#undef INLINE_FUNCTION_GENERATOR_ADDRESS
674
675
676FullCodeGenerator::InlineFunctionGenerator
677 FullCodeGenerator::FindInlineFunctionGenerator(Runtime::FunctionId id) {
whesse@chromium.org023421e2010-12-21 12:19:12 +0000678 int lookup_index =
679 static_cast<int>(id) - static_cast<int>(Runtime::kFirstInlineFunction);
680 ASSERT(lookup_index >= 0);
681 ASSERT(static_cast<size_t>(lookup_index) <
682 ARRAY_SIZE(kInlineFunctionGenerators));
683 return kInlineFunctionGenerators[lookup_index];
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000684}
685
686
687void FullCodeGenerator::EmitInlineRuntimeCall(CallRuntime* node) {
688 ZoneList<Expression*>* args = node->arguments();
689 Handle<String> name = node->name();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000690 const Runtime::Function* function = node->function();
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000691 ASSERT(function != NULL);
692 ASSERT(function->intrinsic_type == Runtime::INLINE);
693 InlineFunctionGenerator generator =
694 FindInlineFunctionGenerator(function->function_id);
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000695 ((*this).*(generator))(args);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000696}
697
698
699void FullCodeGenerator::VisitBinaryOperation(BinaryOperation* expr) {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000700 switch (expr->op()) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000701 case Token::COMMA:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000702 return VisitComma(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000703 case Token::OR:
704 case Token::AND:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000705 return VisitLogicalExpression(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000706 default:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000707 return VisitArithmeticExpression(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000708 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000709}
710
711
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000712void FullCodeGenerator::VisitComma(BinaryOperation* expr) {
713 Comment cmnt(masm_, "[ Comma");
714 VisitForEffect(expr->left());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000715 if (context()->IsTest()) ForwardBailoutToChild(expr);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000716 VisitInCurrentContext(expr->right());
717}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000718
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000719
720void FullCodeGenerator::VisitLogicalExpression(BinaryOperation* expr) {
721 bool is_logical_and = expr->op() == Token::AND;
722 Comment cmnt(masm_, is_logical_and ? "[ Logical AND" : "[ Logical OR");
723 Expression* left = expr->left();
724 Expression* right = expr->right();
725 int right_id = expr->RightId();
726 Label done;
727
728 if (context()->IsTest()) {
729 Label eval_right;
730 const TestContext* test = TestContext::cast(context());
731 if (is_logical_and) {
732 VisitForControl(left, &eval_right, test->false_label(), &eval_right);
733 } else {
734 VisitForControl(left, test->true_label(), &eval_right, &eval_right);
735 }
736 PrepareForBailoutForId(right_id, NO_REGISTERS);
737 __ bind(&eval_right);
738 ForwardBailoutToChild(expr);
739
740 } else if (context()->IsAccumulatorValue()) {
741 VisitForAccumulatorValue(left);
742 // We want the value in the accumulator for the test, and on the stack in
743 // case we need it.
744 __ push(result_register());
745 Label discard, restore;
746 PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
747 if (is_logical_and) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000748 DoTest(left, &discard, &restore, &restore);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000749 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000750 DoTest(left, &restore, &discard, &restore);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000751 }
752 __ bind(&restore);
753 __ pop(result_register());
754 __ jmp(&done);
755 __ bind(&discard);
756 __ Drop(1);
757 PrepareForBailoutForId(right_id, NO_REGISTERS);
758
759 } else if (context()->IsStackValue()) {
760 VisitForAccumulatorValue(left);
761 // We want the value in the accumulator for the test, and on the stack in
762 // case we need it.
763 __ push(result_register());
764 Label discard;
765 PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
766 if (is_logical_and) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000767 DoTest(left, &discard, &done, &discard);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000768 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000769 DoTest(left, &done, &discard, &discard);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000770 }
771 __ bind(&discard);
772 __ Drop(1);
773 PrepareForBailoutForId(right_id, NO_REGISTERS);
774
775 } else {
776 ASSERT(context()->IsEffect());
777 Label eval_right;
778 if (is_logical_and) {
779 VisitForControl(left, &eval_right, &done, &eval_right);
780 } else {
781 VisitForControl(left, &done, &eval_right, &eval_right);
782 }
783 PrepareForBailoutForId(right_id, NO_REGISTERS);
784 __ bind(&eval_right);
785 }
786
787 VisitInCurrentContext(right);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000788 __ bind(&done);
789}
790
791
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000792void FullCodeGenerator::VisitArithmeticExpression(BinaryOperation* expr) {
793 Token::Value op = expr->op();
794 Comment cmnt(masm_, "[ ArithmeticExpression");
795 Expression* left = expr->left();
796 Expression* right = expr->right();
797 OverwriteMode mode =
798 left->ResultOverwriteAllowed()
799 ? OVERWRITE_LEFT
800 : (right->ResultOverwriteAllowed() ? OVERWRITE_RIGHT : NO_OVERWRITE);
801
802 VisitForStackValue(left);
803 VisitForAccumulatorValue(right);
804
805 SetSourcePosition(expr->position());
806 if (ShouldInlineSmiCase(op)) {
807 EmitInlineSmiBinaryOp(expr, op, mode, left, right);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000808 } else {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000809 EmitBinaryOp(expr, op, mode);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000810 }
811}
812
813
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000814void FullCodeGenerator::ForwardBailoutToChild(Expression* expr) {
815 if (!info_->HasDeoptimizationSupport()) return;
816 ASSERT(context()->IsTest());
817 ASSERT(expr == forward_bailout_stack_->expr());
818 forward_bailout_pending_ = forward_bailout_stack_;
819}
820
821
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000822void FullCodeGenerator::VisitInCurrentContext(Expression* expr) {
823 if (context()->IsTest()) {
824 ForwardBailoutStack stack(expr, forward_bailout_pending_);
825 ForwardBailoutStack* saved = forward_bailout_stack_;
826 forward_bailout_pending_ = NULL;
827 forward_bailout_stack_ = &stack;
828 Visit(expr);
829 forward_bailout_stack_ = saved;
830 } else {
831 ASSERT(forward_bailout_pending_ == NULL);
832 Visit(expr);
833 State state = context()->IsAccumulatorValue() ? TOS_REG : NO_REGISTERS;
834 PrepareForBailout(expr, state);
835 // Forwarding bailouts to children is a one shot operation. It should have
836 // been processed at this point.
837 ASSERT(forward_bailout_pending_ == NULL);
838 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000839}
840
841
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000842void FullCodeGenerator::VisitBlock(Block* stmt) {
843 Comment cmnt(masm_, "[ Block");
844 Breakable nested_statement(this, stmt);
845 SetStatementPosition(stmt);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000846
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +0000847 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000848 VisitStatements(stmt->statements());
849 __ bind(nested_statement.break_target());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000850 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000851}
852
853
854void FullCodeGenerator::VisitExpressionStatement(ExpressionStatement* stmt) {
855 Comment cmnt(masm_, "[ ExpressionStatement");
856 SetStatementPosition(stmt);
857 VisitForEffect(stmt->expression());
858}
859
860
861void FullCodeGenerator::VisitEmptyStatement(EmptyStatement* stmt) {
862 Comment cmnt(masm_, "[ EmptyStatement");
863 SetStatementPosition(stmt);
864}
865
866
867void FullCodeGenerator::VisitIfStatement(IfStatement* stmt) {
868 Comment cmnt(masm_, "[ IfStatement");
869 SetStatementPosition(stmt);
870 Label then_part, else_part, done;
871
ricow@chromium.org65fae842010-08-25 15:26:24 +0000872 if (stmt->HasElseStatement()) {
873 VisitForControl(stmt->condition(), &then_part, &else_part, &then_part);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000874 PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000875 __ bind(&then_part);
876 Visit(stmt->then_statement());
877 __ jmp(&done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000878
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000879 PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000880 __ bind(&else_part);
881 Visit(stmt->else_statement());
882 } else {
883 VisitForControl(stmt->condition(), &then_part, &done, &then_part);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000884 PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000885 __ bind(&then_part);
886 Visit(stmt->then_statement());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000887
888 PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000889 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000890 __ bind(&done);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000891 PrepareForBailoutForId(stmt->IfId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000892}
893
894
895void FullCodeGenerator::VisitContinueStatement(ContinueStatement* stmt) {
896 Comment cmnt(masm_, "[ ContinueStatement");
897 SetStatementPosition(stmt);
898 NestedStatement* current = nesting_stack_;
899 int stack_depth = 0;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000900 // When continuing, we clobber the unpredictable value in the accumulator
901 // with one that's safe for GC. If we hit an exit from the try block of
902 // try...finally on our way out, we will unconditionally preserve the
903 // accumulator on the stack.
904 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000905 while (!current->IsContinueTarget(stmt->target())) {
906 stack_depth = current->Exit(stack_depth);
907 current = current->outer();
908 }
909 __ Drop(stack_depth);
910
911 Iteration* loop = current->AsIteration();
912 __ jmp(loop->continue_target());
913}
914
915
916void FullCodeGenerator::VisitBreakStatement(BreakStatement* stmt) {
917 Comment cmnt(masm_, "[ BreakStatement");
918 SetStatementPosition(stmt);
919 NestedStatement* current = nesting_stack_;
920 int stack_depth = 0;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000921 // When breaking, we clobber the unpredictable value in the accumulator
922 // with one that's safe for GC. If we hit an exit from the try block of
923 // try...finally on our way out, we will unconditionally preserve the
924 // accumulator on the stack.
925 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000926 while (!current->IsBreakTarget(stmt->target())) {
927 stack_depth = current->Exit(stack_depth);
928 current = current->outer();
929 }
930 __ Drop(stack_depth);
931
932 Breakable* target = current->AsBreakable();
933 __ jmp(target->break_target());
934}
935
936
937void FullCodeGenerator::VisitReturnStatement(ReturnStatement* stmt) {
938 Comment cmnt(masm_, "[ ReturnStatement");
939 SetStatementPosition(stmt);
940 Expression* expr = stmt->expression();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000941 VisitForAccumulatorValue(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000942
943 // Exit all nested statements.
944 NestedStatement* current = nesting_stack_;
945 int stack_depth = 0;
946 while (current != NULL) {
947 stack_depth = current->Exit(stack_depth);
948 current = current->outer();
949 }
950 __ Drop(stack_depth);
951
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000952 EmitReturnSequence();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000953}
954
955
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000956void FullCodeGenerator::VisitEnterWithContextStatement(
957 EnterWithContextStatement* stmt) {
958 Comment cmnt(masm_, "[ EnterWithContextStatement");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000959 SetStatementPosition(stmt);
960
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000961 VisitForStackValue(stmt->expression());
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000962 PushFunctionArgumentForContextAllocation();
963 __ CallRuntime(Runtime::kPushWithContext, 2);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000964 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
965}
966
967
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000968void FullCodeGenerator::VisitExitContextStatement(ExitContextStatement* stmt) {
969 Comment cmnt(masm_, "[ ExitContextStatement");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000970 SetStatementPosition(stmt);
971
972 // Pop context.
973 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
974 // Update local stack frame context field.
975 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
976}
977
978
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000979void FullCodeGenerator::VisitDoWhileStatement(DoWhileStatement* stmt) {
980 Comment cmnt(masm_, "[ DoWhileStatement");
981 SetStatementPosition(stmt);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000982 Label body, stack_check;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000983
984 Iteration loop_statement(this, stmt);
985 increment_loop_depth();
986
987 __ bind(&body);
988 Visit(stmt->body());
989
ricow@chromium.org65fae842010-08-25 15:26:24 +0000990 // Record the position of the do while condition and make sure it is
991 // possible to break on the condition.
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000992 __ bind(loop_statement.continue_target());
993 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000994 SetExpressionPosition(stmt->cond(), stmt->condition_position());
ricow@chromium.org65fae842010-08-25 15:26:24 +0000995 VisitForControl(stmt->cond(),
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000996 &stack_check,
ricow@chromium.org65fae842010-08-25 15:26:24 +0000997 loop_statement.break_target(),
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000998 &stack_check);
999
1000 // Check stack before looping.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001001 PrepareForBailoutForId(stmt->BackEdgeId(), NO_REGISTERS);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001002 __ bind(&stack_check);
1003 EmitStackCheck(stmt);
1004 __ jmp(&body);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001005
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001006 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001007 __ bind(loop_statement.break_target());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001008 decrement_loop_depth();
1009}
1010
1011
1012void FullCodeGenerator::VisitWhileStatement(WhileStatement* stmt) {
1013 Comment cmnt(masm_, "[ WhileStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001014 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001015
1016 Iteration loop_statement(this, stmt);
1017 increment_loop_depth();
1018
1019 // Emit the test at the bottom of the loop.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001020 __ jmp(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001021
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001022 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001023 __ bind(&body);
1024 Visit(stmt->body());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001025
1026 // Emit the statement position here as this is where the while
1027 // statement code starts.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001028 __ bind(loop_statement.continue_target());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001029 SetStatementPosition(stmt);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001030
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001031 // Check stack before looping.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001032 EmitStackCheck(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001033
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001034 __ bind(&test);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001035 VisitForControl(stmt->cond(),
1036 &body,
1037 loop_statement.break_target(),
1038 loop_statement.break_target());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001039
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001040 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001041 __ bind(loop_statement.break_target());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001042 decrement_loop_depth();
1043}
1044
1045
1046void FullCodeGenerator::VisitForStatement(ForStatement* stmt) {
1047 Comment cmnt(masm_, "[ ForStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001048 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001049
1050 Iteration loop_statement(this, stmt);
1051 if (stmt->init() != NULL) {
1052 Visit(stmt->init());
1053 }
1054
1055 increment_loop_depth();
1056 // Emit the test at the bottom of the loop (even if empty).
1057 __ jmp(&test);
1058
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001059 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001060 __ bind(&body);
1061 Visit(stmt->body());
1062
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001063 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001064 __ bind(loop_statement.continue_target());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001065 SetStatementPosition(stmt);
1066 if (stmt->next() != NULL) {
1067 Visit(stmt->next());
1068 }
1069
ricow@chromium.org65fae842010-08-25 15:26:24 +00001070 // Emit the statement position here as this is where the for
1071 // statement code starts.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001072 SetStatementPosition(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001073
1074 // Check stack before looping.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001075 EmitStackCheck(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001076
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001077 __ bind(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001078 if (stmt->cond() != NULL) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00001079 VisitForControl(stmt->cond(),
1080 &body,
1081 loop_statement.break_target(),
1082 loop_statement.break_target());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001083 } else {
1084 __ jmp(&body);
1085 }
1086
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001087 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001088 __ bind(loop_statement.break_target());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001089 decrement_loop_depth();
1090}
1091
1092
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001093void FullCodeGenerator::VisitTryCatchStatement(TryCatchStatement* stmt) {
1094 Comment cmnt(masm_, "[ TryCatchStatement");
1095 SetStatementPosition(stmt);
1096 // The try block adds a handler to the exception handler chain
1097 // before entering, and removes it again when exiting normally.
1098 // If an exception is thrown during execution of the try block,
1099 // control is passed to the handler, which also consumes the handler.
1100 // At this point, the exception is in a register, and store it in
1101 // the temporary local variable (prints as ".catch-var") before
1102 // executing the catch block. The catch block has been rewritten
1103 // to introduce a new scope to bind the catch variable and to remove
1104 // that scope again afterwards.
1105
1106 Label try_handler_setup, catch_entry, done;
1107 __ Call(&try_handler_setup);
1108 // Try handler code, exception in result register.
1109
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001110 // Extend the context before executing the catch block.
1111 { Comment cmnt(masm_, "[ Extend catch context");
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001112 __ Push(stmt->variable()->name());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001113 __ push(result_register());
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00001114 PushFunctionArgumentForContextAllocation();
1115 __ CallRuntime(Runtime::kPushCatchContext, 3);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001116 StoreToFrameField(StandardFrameConstants::kContextOffset,
1117 context_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001118 }
1119
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001120 Scope* saved_scope = scope();
1121 scope_ = stmt->scope();
1122 ASSERT(scope_->declarations()->is_empty());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001123 Visit(stmt->catch_block());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001124 scope_ = saved_scope;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001125 __ jmp(&done);
1126
1127 // Try block code. Sets up the exception handler chain.
1128 __ bind(&try_handler_setup);
1129 {
1130 TryCatch try_block(this, &catch_entry);
1131 __ PushTryHandler(IN_JAVASCRIPT, TRY_CATCH_HANDLER);
1132 Visit(stmt->try_block());
1133 __ PopTryHandler();
1134 }
1135 __ bind(&done);
1136}
1137
1138
1139void FullCodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* stmt) {
1140 Comment cmnt(masm_, "[ TryFinallyStatement");
1141 SetStatementPosition(stmt);
1142 // Try finally is compiled by setting up a try-handler on the stack while
1143 // executing the try body, and removing it again afterwards.
1144 //
1145 // The try-finally construct can enter the finally block in three ways:
1146 // 1. By exiting the try-block normally. This removes the try-handler and
1147 // calls the finally block code before continuing.
1148 // 2. By exiting the try-block with a function-local control flow transfer
1149 // (break/continue/return). The site of the, e.g., break removes the
1150 // try handler and calls the finally block code before continuing
1151 // its outward control transfer.
1152 // 3. by exiting the try-block with a thrown exception.
1153 // This can happen in nested function calls. It traverses the try-handler
1154 // chain and consumes the try-handler entry before jumping to the
1155 // handler code. The handler code then calls the finally-block before
1156 // rethrowing the exception.
1157 //
1158 // The finally block must assume a return address on top of the stack
1159 // (or in the link register on ARM chips) and a value (return value or
1160 // exception) in the result register (rax/eax/r0), both of which must
1161 // be preserved. The return address isn't GC-safe, so it should be
1162 // cooked before GC.
1163 Label finally_entry;
1164 Label try_handler_setup;
1165
1166 // Setup the try-handler chain. Use a call to
1167 // Jump to try-handler setup and try-block code. Use call to put try-handler
1168 // address on stack.
1169 __ Call(&try_handler_setup);
1170 // Try handler code. Return address of call is pushed on handler stack.
1171 {
1172 // This code is only executed during stack-handler traversal when an
1173 // exception is thrown. The execption is in the result register, which
1174 // is retained by the finally block.
1175 // Call the finally block and then rethrow the exception.
1176 __ Call(&finally_entry);
1177 __ push(result_register());
1178 __ CallRuntime(Runtime::kReThrow, 1);
1179 }
1180
1181 __ bind(&finally_entry);
1182 {
1183 // Finally block implementation.
1184 Finally finally_block(this);
1185 EnterFinallyBlock();
1186 Visit(stmt->finally_block());
1187 ExitFinallyBlock(); // Return to the calling code.
1188 }
1189
1190 __ bind(&try_handler_setup);
1191 {
1192 // Setup try handler (stack pointer registers).
1193 TryFinally try_block(this, &finally_entry);
1194 __ PushTryHandler(IN_JAVASCRIPT, TRY_FINALLY_HANDLER);
1195 Visit(stmt->try_block());
1196 __ PopTryHandler();
1197 }
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001198 // Execute the finally block on the way out. Clobber the unpredictable
1199 // value in the accumulator with one that's safe for GC. The finally
1200 // block will unconditionally preserve the accumulator on the stack.
1201 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001202 __ Call(&finally_entry);
1203}
1204
1205
1206void FullCodeGenerator::VisitDebuggerStatement(DebuggerStatement* stmt) {
1207#ifdef ENABLE_DEBUGGER_SUPPORT
1208 Comment cmnt(masm_, "[ DebuggerStatement");
1209 SetStatementPosition(stmt);
1210
ager@chromium.org5c838252010-02-19 08:53:10 +00001211 __ DebugBreak();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001212 // Ignore the return value.
1213#endif
1214}
1215
1216
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001217void FullCodeGenerator::VisitConditional(Conditional* expr) {
1218 Comment cmnt(masm_, "[ Conditional");
1219 Label true_case, false_case, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001220 VisitForControl(expr->condition(), &true_case, &false_case, &true_case);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001221
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001222 PrepareForBailoutForId(expr->ThenId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001223 __ bind(&true_case);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001224 SetExpressionPosition(expr->then_expression(),
1225 expr->then_expression_position());
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001226 if (context()->IsTest()) {
1227 const TestContext* for_test = TestContext::cast(context());
1228 VisitForControl(expr->then_expression(),
1229 for_test->true_label(),
1230 for_test->false_label(),
1231 NULL);
1232 } else {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001233 VisitInCurrentContext(expr->then_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001234 __ jmp(&done);
1235 }
1236
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001237 PrepareForBailoutForId(expr->ElseId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001238 __ bind(&false_case);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001239 if (context()->IsTest()) ForwardBailoutToChild(expr);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001240 SetExpressionPosition(expr->else_expression(),
1241 expr->else_expression_position());
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001242 VisitInCurrentContext(expr->else_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001243 // If control flow falls through Visit, merge it with true case here.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001244 if (!context()->IsTest()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001245 __ bind(&done);
1246 }
1247}
1248
1249
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001250void FullCodeGenerator::VisitLiteral(Literal* expr) {
1251 Comment cmnt(masm_, "[ Literal");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001252 context()->Plug(expr->handle());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001253}
1254
1255
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001256void FullCodeGenerator::VisitFunctionLiteral(FunctionLiteral* expr) {
1257 Comment cmnt(masm_, "[ FunctionLiteral");
1258
1259 // Build the function boilerplate and instantiate it.
1260 Handle<SharedFunctionInfo> function_info =
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001261 Compiler::BuildFunctionInfo(expr, script());
1262 if (function_info.is_null()) {
1263 SetStackOverflow();
1264 return;
1265 }
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001266 EmitNewClosure(function_info, expr->pretenure());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001267}
1268
1269
1270void FullCodeGenerator::VisitSharedFunctionInfoLiteral(
1271 SharedFunctionInfoLiteral* expr) {
1272 Comment cmnt(masm_, "[ SharedFunctionInfoLiteral");
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001273 EmitNewClosure(expr->shared_function_info(), false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001274}
1275
1276
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001277void FullCodeGenerator::VisitThrow(Throw* expr) {
1278 Comment cmnt(masm_, "[ Throw");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001279 VisitForStackValue(expr->exception());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001280 __ CallRuntime(Runtime::kThrow, 1);
1281 // Never returns here.
1282}
1283
1284
1285int FullCodeGenerator::TryFinally::Exit(int stack_depth) {
1286 // The macros used here must preserve the result register.
1287 __ Drop(stack_depth);
1288 __ PopTryHandler();
1289 __ Call(finally_entry_);
1290 return 0;
1291}
1292
1293
1294int FullCodeGenerator::TryCatch::Exit(int stack_depth) {
1295 // The macros used here must preserve the result register.
1296 __ Drop(stack_depth);
1297 __ PopTryHandler();
1298 return 0;
1299}
1300
ricow@chromium.org65fae842010-08-25 15:26:24 +00001301
ager@chromium.org04921a82011-06-27 13:21:41 +00001302bool FullCodeGenerator::TryLiteralCompare(CompareOperation* compare,
1303 Label* if_true,
1304 Label* if_false,
1305 Label* fall_through) {
1306 Expression *expr;
1307 Handle<String> check;
1308 if (compare->IsLiteralCompareTypeof(&expr, &check)) {
1309 EmitLiteralCompareTypeof(expr, check, if_true, if_false, fall_through);
1310 return true;
1311 }
1312
1313 if (compare->IsLiteralCompareUndefined(&expr)) {
1314 EmitLiteralCompareUndefined(expr, if_true, if_false, fall_through);
1315 return true;
1316 }
1317
1318 return false;
1319}
1320
1321
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001322#undef __
1323
1324
1325} } // namespace v8::internal