blob: d509cd5bb06be16eb1111eb347040573860c2f3a [file] [log] [blame]
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001// Copyright 2010 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
30#include "codegen-inl.h"
31#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
93void BreakableStatementChecker::VisitWithEnterStatement(
94 WithEnterStatement* stmt) {
95 Visit(stmt->expression());
96}
97
98
99void BreakableStatementChecker::VisitWithExitStatement(
100 WithExitStatement* stmt) {
101}
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
190void BreakableStatementChecker::VisitCatchExtensionObject(
191 CatchExtensionObject* expr) {
192}
193
194
195void BreakableStatementChecker::VisitAssignment(Assignment* expr) {
196 // If assigning to a property (including a global property) the assignment is
197 // breakable.
198 Variable* var = expr->target()->AsVariableProxy()->AsVariable();
199 Property* prop = expr->target()->AsProperty();
200 if (prop != NULL || (var != NULL && var->is_global())) {
201 is_breakable_ = true;
202 return;
203 }
204
205 // Otherwise the assignment is breakable if the assigned value is.
206 Visit(expr->value());
207}
208
209
210void BreakableStatementChecker::VisitThrow(Throw* expr) {
211 // Throw is breakable if the expression is.
212 Visit(expr->exception());
213}
214
215
ricow@chromium.org65fae842010-08-25 15:26:24 +0000216void BreakableStatementChecker::VisitIncrementOperation(
217 IncrementOperation* expr) {
218 UNREACHABLE();
219}
220
221
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000222void BreakableStatementChecker::VisitProperty(Property* expr) {
223 // Property load is breakable.
224 is_breakable_ = true;
225}
226
227
228void BreakableStatementChecker::VisitCall(Call* expr) {
229 // Function calls both through IC and call stub are breakable.
230 is_breakable_ = true;
231}
232
233
234void BreakableStatementChecker::VisitCallNew(CallNew* expr) {
235 // Function calls through new are breakable.
236 is_breakable_ = true;
237}
238
239
240void BreakableStatementChecker::VisitCallRuntime(CallRuntime* expr) {
241}
242
243
244void BreakableStatementChecker::VisitUnaryOperation(UnaryOperation* expr) {
245 Visit(expr->expression());
246}
247
248
249void BreakableStatementChecker::VisitCountOperation(CountOperation* expr) {
250 Visit(expr->expression());
251}
252
253
254void BreakableStatementChecker::VisitBinaryOperation(BinaryOperation* expr) {
255 Visit(expr->left());
256 Visit(expr->right());
257}
258
259
ricow@chromium.org65fae842010-08-25 15:26:24 +0000260void BreakableStatementChecker::VisitCompareToNull(CompareToNull* expr) {
261 Visit(expr->expression());
262}
263
264
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000265void BreakableStatementChecker::VisitCompareOperation(CompareOperation* expr) {
266 Visit(expr->left());
267 Visit(expr->right());
268}
269
270
271void BreakableStatementChecker::VisitThisFunction(ThisFunction* expr) {
272}
273
274
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000275#define __ ACCESS_MASM(masm())
276
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000277bool FullCodeGenerator::MakeCode(CompilationInfo* info) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000278 Isolate* isolate = info->isolate();
ager@chromium.org5c838252010-02-19 08:53:10 +0000279 Handle<Script> script = info->script();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000280 if (!script->IsUndefined() && !script->source()->IsUndefined()) {
281 int len = String::cast(script->source())->length();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000282 isolate->counters()->total_full_codegen_source_size()->Increment(len);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000283 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000284 if (FLAG_trace_codegen) {
285 PrintF("Full Compiler - ");
286 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000287 CodeGenerator::MakeCodePrologue(info);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000288 const int kInitialBufferSize = 4 * KB;
289 MacroAssembler masm(NULL, kInitialBufferSize);
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000290#ifdef ENABLE_GDB_JIT_INTERFACE
291 masm.positions_recorder()->StartGDBJITLineInfoRecording();
292#endif
ager@chromium.org5c838252010-02-19 08:53:10 +0000293
294 FullCodeGenerator cgen(&masm);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000295 cgen.Generate(info);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000296 if (cgen.HasStackOverflow()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000297 ASSERT(!isolate->has_pending_exception());
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000298 return false;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000299 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000300 unsigned table_offset = cgen.EmitStackCheckTable();
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000301
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000302 Code::Flags flags = Code::ComputeFlags(Code::FUNCTION, NOT_IN_LOOP);
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000303 Handle<Code> code = CodeGenerator::MakeCodeEpilogue(&masm, flags, info);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000304 code->set_optimizable(info->IsOptimizable());
305 cgen.PopulateDeoptimizationData(code);
306 code->set_has_deoptimization_support(info->HasDeoptimizationSupport());
307 code->set_allow_osr_at_loop_nesting_level(0);
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000308 code->set_stack_check_table_offset(table_offset);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000309 CodeGenerator::PrintCode(code, info);
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000310 info->SetCode(code); // may be an empty handle.
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000311#ifdef ENABLE_GDB_JIT_INTERFACE
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000312 if (FLAG_gdbjit && !code.is_null()) {
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000313 GDBJITLineInfo* lineinfo =
314 masm.positions_recorder()->DetachGDBJITLineInfo();
315
316 GDBJIT(RegisterDetailedLineInfo(*code, lineinfo));
317 }
318#endif
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000319 return !code.is_null();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000320}
321
322
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000323unsigned FullCodeGenerator::EmitStackCheckTable() {
324 // The stack check table consists of a length (in number of entries)
325 // field, and then a sequence of entries. Each entry is a pair of AST id
326 // and code-relative pc offset.
327 masm()->Align(kIntSize);
328 masm()->RecordComment("[ Stack check table");
329 unsigned offset = masm()->pc_offset();
330 unsigned length = stack_checks_.length();
331 __ dd(length);
332 for (unsigned i = 0; i < length; ++i) {
333 __ dd(stack_checks_[i].id);
334 __ dd(stack_checks_[i].pc_and_state);
335 }
336 masm()->RecordComment("]");
337 return offset;
338}
339
340
341void FullCodeGenerator::PopulateDeoptimizationData(Handle<Code> code) {
342 // Fill in the deoptimization information.
343 ASSERT(info_->HasDeoptimizationSupport() || bailout_entries_.is_empty());
344 if (!info_->HasDeoptimizationSupport()) return;
345 int length = bailout_entries_.length();
346 Handle<DeoptimizationOutputData> data =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000347 isolate()->factory()->
348 NewDeoptimizationOutputData(length, TENURED);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000349 for (int i = 0; i < length; i++) {
350 data->SetAstId(i, Smi::FromInt(bailout_entries_[i].id));
351 data->SetPcAndState(i, Smi::FromInt(bailout_entries_[i].pc_and_state));
352 }
353 code->set_deoptimization_data(*data);
354}
355
356
357void FullCodeGenerator::PrepareForBailout(AstNode* node, State state) {
358 PrepareForBailoutForId(node->id(), state);
359}
360
361
362void FullCodeGenerator::RecordJSReturnSite(Call* call) {
363 // We record the offset of the function return so we can rebuild the frame
364 // if the function was inlined, i.e., this is the return address in the
365 // inlined function's frame.
366 //
367 // The state is ignored. We defensively set it to TOS_REG, which is the
368 // real state of the unoptimized code at the return site.
369 PrepareForBailoutForId(call->ReturnId(), TOS_REG);
370#ifdef DEBUG
371 // In debug builds, mark the return so we can verify that this function
372 // was called.
373 ASSERT(!call->return_is_recorded_);
374 call->return_is_recorded_ = true;
375#endif
376}
377
378
379void FullCodeGenerator::PrepareForBailoutForId(int id, State state) {
380 // There's no need to prepare this code for bailouts from already optimized
381 // code or code that can't be optimized.
382 if (!FLAG_deopt || !info_->HasDeoptimizationSupport()) return;
383 unsigned pc_and_state =
384 StateField::encode(state) | PcField::encode(masm_->pc_offset());
385 BailoutEntry entry = { id, pc_and_state };
386#ifdef DEBUG
387 // Assert that we don't have multiple bailout entries for the same node.
388 for (int i = 0; i < bailout_entries_.length(); i++) {
389 if (bailout_entries_.at(i).id == entry.id) {
390 AstPrinter printer;
391 PrintF("%s", printer.PrintProgram(info_->function()));
392 UNREACHABLE();
393 }
394 }
395#endif // DEBUG
396 bailout_entries_.Add(entry);
397}
398
399
400void FullCodeGenerator::RecordStackCheck(int ast_id) {
401 // The pc offset does not need to be encoded and packed together with a
402 // state.
403 BailoutEntry entry = { ast_id, masm_->pc_offset() };
404 stack_checks_.Add(entry);
405}
406
407
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000408int FullCodeGenerator::SlotOffset(Slot* slot) {
409 ASSERT(slot != NULL);
410 // Offset is negative because higher indexes are at lower addresses.
411 int offset = -slot->index() * kPointerSize;
412 // Adjust by a (parameter or local) base offset.
413 switch (slot->type()) {
414 case Slot::PARAMETER:
ager@chromium.org5c838252010-02-19 08:53:10 +0000415 offset += (scope()->num_parameters() + 1) * kPointerSize;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000416 break;
417 case Slot::LOCAL:
418 offset += JavaScriptFrameConstants::kLocal0Offset;
419 break;
420 case Slot::CONTEXT:
421 case Slot::LOOKUP:
422 UNREACHABLE();
423 }
424 return offset;
425}
426
427
ricow@chromium.org65fae842010-08-25 15:26:24 +0000428bool FullCodeGenerator::ShouldInlineSmiCase(Token::Value op) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000429 // Inline smi case inside loops, but not division and modulo which
430 // are too complicated and take up too much space.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000431 if (op == Token::DIV ||op == Token::MOD) return false;
432 if (FLAG_always_inline_smi_code) return true;
433 return loop_depth_ > 0;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000434}
435
436
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000437void FullCodeGenerator::EffectContext::Plug(Register reg) const {
438}
439
440
441void FullCodeGenerator::AccumulatorValueContext::Plug(Register reg) const {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000442 __ Move(result_register(), reg);
443}
444
445
446void FullCodeGenerator::StackValueContext::Plug(Register reg) const {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000447 __ push(reg);
448}
449
450
451void FullCodeGenerator::TestContext::Plug(Register reg) const {
452 // For simplicity we always test the accumulator register.
453 __ Move(result_register(), reg);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000454 codegen()->PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000455 codegen()->DoTest(true_label_, false_label_, fall_through_);
456}
457
458
459void FullCodeGenerator::EffectContext::PlugTOS() const {
460 __ Drop(1);
461}
462
463
464void FullCodeGenerator::AccumulatorValueContext::PlugTOS() const {
465 __ pop(result_register());
466}
467
468
469void FullCodeGenerator::StackValueContext::PlugTOS() const {
470}
471
472
473void FullCodeGenerator::TestContext::PlugTOS() const {
474 // For simplicity we always test the accumulator register.
475 __ pop(result_register());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000476 codegen()->PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000477 codegen()->DoTest(true_label_, false_label_, fall_through_);
478}
479
480
481void FullCodeGenerator::EffectContext::PrepareTest(
482 Label* materialize_true,
483 Label* materialize_false,
484 Label** if_true,
485 Label** if_false,
486 Label** fall_through) const {
487 // In an effect context, the true and the false case branch to the
488 // same label.
489 *if_true = *if_false = *fall_through = materialize_true;
490}
491
492
493void FullCodeGenerator::AccumulatorValueContext::PrepareTest(
494 Label* materialize_true,
495 Label* materialize_false,
496 Label** if_true,
497 Label** if_false,
498 Label** fall_through) const {
499 *if_true = *fall_through = materialize_true;
500 *if_false = materialize_false;
501}
502
503
504void FullCodeGenerator::StackValueContext::PrepareTest(
505 Label* materialize_true,
506 Label* materialize_false,
507 Label** if_true,
508 Label** if_false,
509 Label** fall_through) const {
510 *if_true = *fall_through = materialize_true;
511 *if_false = materialize_false;
512}
513
514
515void FullCodeGenerator::TestContext::PrepareTest(
516 Label* materialize_true,
517 Label* materialize_false,
518 Label** if_true,
519 Label** if_false,
520 Label** fall_through) const {
521 *if_true = true_label_;
522 *if_false = false_label_;
523 *fall_through = fall_through_;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000524}
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) {
586 if (FLAG_debug_info) {
587 CodeGenerator::RecordPositions(masm_, fun->start_position());
588 }
589}
590
591
592void FullCodeGenerator::SetReturnPosition(FunctionLiteral* fun) {
593 if (FLAG_debug_info) {
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000594 CodeGenerator::RecordPositions(masm_, fun->end_position() - 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000595 }
596}
597
598
599void FullCodeGenerator::SetStatementPosition(Statement* stmt) {
600 if (FLAG_debug_info) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000601#ifdef ENABLE_DEBUGGER_SUPPORT
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000602 if (!isolate()->debugger()->IsDebuggerActive()) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000603 CodeGenerator::RecordPositions(masm_, stmt->statement_pos());
604 } else {
605 // Check if the statement will be breakable without adding a debug break
606 // slot.
607 BreakableStatementChecker checker;
608 checker.Check(stmt);
609 // Record the statement position right here if the statement is not
610 // breakable. For breakable statements the actual recording of the
611 // position will be postponed to the breakable code (typically an IC).
612 bool position_recorded = CodeGenerator::RecordPositions(
613 masm_, stmt->statement_pos(), !checker.is_breakable());
614 // If the position recording did record a new position generate a debug
615 // break slot to make the statement breakable.
616 if (position_recorded) {
617 Debug::GenerateSlot(masm_);
618 }
619 }
620#else
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000621 CodeGenerator::RecordPositions(masm_, stmt->statement_pos());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000622#endif
623 }
624}
625
626
627void FullCodeGenerator::SetExpressionPosition(Expression* expr, int pos) {
628 if (FLAG_debug_info) {
629#ifdef ENABLE_DEBUGGER_SUPPORT
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000630 if (!isolate()->debugger()->IsDebuggerActive()) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000631 CodeGenerator::RecordPositions(masm_, pos);
632 } else {
633 // Check if the expression will be breakable without adding a debug break
634 // slot.
635 BreakableStatementChecker checker;
636 checker.Check(expr);
637 // Record a statement position right here if the expression is not
638 // breakable. For breakable expressions the actual recording of the
639 // position will be postponed to the breakable code (typically an IC).
640 // NOTE this will record a statement position for something which might
641 // not be a statement. As stepping in the debugger will only stop at
642 // statement positions this is used for e.g. the condition expression of
643 // a do while loop.
644 bool position_recorded = CodeGenerator::RecordPositions(
645 masm_, pos, !checker.is_breakable());
646 // If the position recording did record a new position generate a debug
647 // break slot to make the statement breakable.
648 if (position_recorded) {
649 Debug::GenerateSlot(masm_);
650 }
651 }
652#else
653 CodeGenerator::RecordPositions(masm_, pos);
654#endif
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000655 }
656}
657
658
659void FullCodeGenerator::SetStatementPosition(int pos) {
660 if (FLAG_debug_info) {
661 CodeGenerator::RecordPositions(masm_, pos);
662 }
663}
664
665
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000666void FullCodeGenerator::SetSourcePosition(int pos) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000667 if (FLAG_debug_info && pos != RelocInfo::kNoPosition) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000668 masm_->positions_recorder()->RecordPosition(pos);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000669 }
670}
671
672
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000673// Lookup table for code generators for special runtime calls which are
674// generated inline.
675#define INLINE_FUNCTION_GENERATOR_ADDRESS(Name, argc, ressize) \
676 &FullCodeGenerator::Emit##Name,
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000677
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000678const FullCodeGenerator::InlineFunctionGenerator
679 FullCodeGenerator::kInlineFunctionGenerators[] = {
680 INLINE_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS)
681 INLINE_RUNTIME_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS)
682 };
683#undef INLINE_FUNCTION_GENERATOR_ADDRESS
684
685
686FullCodeGenerator::InlineFunctionGenerator
687 FullCodeGenerator::FindInlineFunctionGenerator(Runtime::FunctionId id) {
whesse@chromium.org023421e2010-12-21 12:19:12 +0000688 int lookup_index =
689 static_cast<int>(id) - static_cast<int>(Runtime::kFirstInlineFunction);
690 ASSERT(lookup_index >= 0);
691 ASSERT(static_cast<size_t>(lookup_index) <
692 ARRAY_SIZE(kInlineFunctionGenerators));
693 return kInlineFunctionGenerators[lookup_index];
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000694}
695
696
697void FullCodeGenerator::EmitInlineRuntimeCall(CallRuntime* node) {
698 ZoneList<Expression*>* args = node->arguments();
699 Handle<String> name = node->name();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000700 const Runtime::Function* function = node->function();
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000701 ASSERT(function != NULL);
702 ASSERT(function->intrinsic_type == Runtime::INLINE);
703 InlineFunctionGenerator generator =
704 FindInlineFunctionGenerator(function->function_id);
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000705 ((*this).*(generator))(args);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000706}
707
708
709void FullCodeGenerator::VisitBinaryOperation(BinaryOperation* expr) {
710 Comment cmnt(masm_, "[ BinaryOperation");
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000711 Token::Value op = expr->op();
712 Expression* left = expr->left();
713 Expression* right = expr->right();
ricow@chromium.org65fae842010-08-25 15:26:24 +0000714
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000715 OverwriteMode mode = NO_OVERWRITE;
716 if (left->ResultOverwriteAllowed()) {
717 mode = OVERWRITE_LEFT;
718 } else if (right->ResultOverwriteAllowed()) {
719 mode = OVERWRITE_RIGHT;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000720 }
721
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000722 switch (op) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000723 case Token::COMMA:
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000724 VisitForEffect(left);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000725 if (context()->IsTest()) ForwardBailoutToChild(expr);
726 context()->HandleExpression(right);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000727 break;
728
729 case Token::OR:
730 case Token::AND:
731 EmitLogicalOperation(expr);
732 break;
733
734 case Token::ADD:
735 case Token::SUB:
736 case Token::DIV:
737 case Token::MOD:
738 case Token::MUL:
739 case Token::BIT_OR:
740 case Token::BIT_AND:
741 case Token::BIT_XOR:
742 case Token::SHL:
743 case Token::SHR:
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000744 case Token::SAR: {
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000745 // Load both operands.
746 VisitForStackValue(left);
747 VisitForAccumulatorValue(right);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000748
ricow@chromium.org65fae842010-08-25 15:26:24 +0000749 SetSourcePosition(expr->position());
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000750 if (ShouldInlineSmiCase(op)) {
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000751 EmitInlineSmiBinaryOp(expr, op, mode, left, right);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000752 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000753 EmitBinaryOp(op, mode);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000754 }
ricow@chromium.org65fae842010-08-25 15:26:24 +0000755 break;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000756 }
ricow@chromium.org65fae842010-08-25 15:26:24 +0000757
758 default:
759 UNREACHABLE();
760 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000761}
762
763
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000764void FullCodeGenerator::EmitLogicalOperation(BinaryOperation* expr) {
765 Label eval_right, done;
766
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000767 context()->EmitLogicalLeft(expr, &eval_right, &done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000768
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000769 PrepareForBailoutForId(expr->RightId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000770 __ bind(&eval_right);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000771 if (context()->IsTest()) ForwardBailoutToChild(expr);
772 context()->HandleExpression(expr->right());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000773
774 __ bind(&done);
775}
776
777
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000778void FullCodeGenerator::EffectContext::EmitLogicalLeft(BinaryOperation* expr,
779 Label* eval_right,
780 Label* done) const {
781 if (expr->op() == Token::OR) {
782 codegen()->VisitForControl(expr->left(), done, eval_right, eval_right);
783 } else {
784 ASSERT(expr->op() == Token::AND);
785 codegen()->VisitForControl(expr->left(), eval_right, done, eval_right);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000786 }
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000787}
ricow@chromium.org65fae842010-08-25 15:26:24 +0000788
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000789
790void FullCodeGenerator::AccumulatorValueContext::EmitLogicalLeft(
791 BinaryOperation* expr,
792 Label* eval_right,
793 Label* done) const {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000794 HandleExpression(expr->left());
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000795 // We want the value in the accumulator for the test, and on the stack in case
796 // we need it.
797 __ push(result_register());
798 Label discard, restore;
799 if (expr->op() == Token::OR) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000800 codegen()->PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000801 codegen()->DoTest(&restore, &discard, &restore);
802 } else {
803 ASSERT(expr->op() == Token::AND);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000804 codegen()->PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000805 codegen()->DoTest(&discard, &restore, &restore);
806 }
807 __ bind(&restore);
808 __ pop(result_register());
809 __ jmp(done);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000810 __ bind(&discard);
811 __ Drop(1);
812}
813
814
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000815void FullCodeGenerator::StackValueContext::EmitLogicalLeft(
816 BinaryOperation* expr,
817 Label* eval_right,
818 Label* done) const {
819 codegen()->VisitForAccumulatorValue(expr->left());
820 // We want the value in the accumulator for the test, and on the stack in case
821 // we need it.
822 __ push(result_register());
823 Label discard;
824 if (expr->op() == Token::OR) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000825 codegen()->PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000826 codegen()->DoTest(done, &discard, &discard);
827 } else {
828 ASSERT(expr->op() == Token::AND);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000829 codegen()->PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000830 codegen()->DoTest(&discard, done, &discard);
831 }
832 __ bind(&discard);
833 __ Drop(1);
834}
835
836
837void FullCodeGenerator::TestContext::EmitLogicalLeft(BinaryOperation* expr,
838 Label* eval_right,
839 Label* done) const {
840 if (expr->op() == Token::OR) {
841 codegen()->VisitForControl(expr->left(),
842 true_label_, eval_right, eval_right);
843 } else {
844 ASSERT(expr->op() == Token::AND);
845 codegen()->VisitForControl(expr->left(),
846 eval_right, false_label_, eval_right);
847 }
848}
849
850
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000851void FullCodeGenerator::ForwardBailoutToChild(Expression* expr) {
852 if (!info_->HasDeoptimizationSupport()) return;
853 ASSERT(context()->IsTest());
854 ASSERT(expr == forward_bailout_stack_->expr());
855 forward_bailout_pending_ = forward_bailout_stack_;
856}
857
858
859void FullCodeGenerator::EffectContext::HandleExpression(
860 Expression* expr) const {
861 codegen()->HandleInNonTestContext(expr, NO_REGISTERS);
862}
863
864
865void FullCodeGenerator::AccumulatorValueContext::HandleExpression(
866 Expression* expr) const {
867 codegen()->HandleInNonTestContext(expr, TOS_REG);
868}
869
870
871void FullCodeGenerator::StackValueContext::HandleExpression(
872 Expression* expr) const {
873 codegen()->HandleInNonTestContext(expr, NO_REGISTERS);
874}
875
876
877void FullCodeGenerator::TestContext::HandleExpression(Expression* expr) const {
878 codegen()->VisitInTestContext(expr);
879}
880
881
882void FullCodeGenerator::HandleInNonTestContext(Expression* expr, State state) {
883 ASSERT(forward_bailout_pending_ == NULL);
884 AstVisitor::Visit(expr);
885 PrepareForBailout(expr, state);
886 // Forwarding bailouts to children is a one shot operation. It
887 // should have been processed at this point.
888 ASSERT(forward_bailout_pending_ == NULL);
889}
890
891
892void FullCodeGenerator::VisitInTestContext(Expression* expr) {
893 ForwardBailoutStack stack(expr, forward_bailout_pending_);
894 ForwardBailoutStack* saved = forward_bailout_stack_;
895 forward_bailout_pending_ = NULL;
896 forward_bailout_stack_ = &stack;
897 AstVisitor::Visit(expr);
898 forward_bailout_stack_ = saved;
899}
900
901
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000902void FullCodeGenerator::VisitBlock(Block* stmt) {
903 Comment cmnt(masm_, "[ Block");
904 Breakable nested_statement(this, stmt);
905 SetStatementPosition(stmt);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000906
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +0000907 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000908 VisitStatements(stmt->statements());
909 __ bind(nested_statement.break_target());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000910 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000911}
912
913
914void FullCodeGenerator::VisitExpressionStatement(ExpressionStatement* stmt) {
915 Comment cmnt(masm_, "[ ExpressionStatement");
916 SetStatementPosition(stmt);
917 VisitForEffect(stmt->expression());
918}
919
920
921void FullCodeGenerator::VisitEmptyStatement(EmptyStatement* stmt) {
922 Comment cmnt(masm_, "[ EmptyStatement");
923 SetStatementPosition(stmt);
924}
925
926
927void FullCodeGenerator::VisitIfStatement(IfStatement* stmt) {
928 Comment cmnt(masm_, "[ IfStatement");
929 SetStatementPosition(stmt);
930 Label then_part, else_part, done;
931
ricow@chromium.org65fae842010-08-25 15:26:24 +0000932 if (stmt->HasElseStatement()) {
933 VisitForControl(stmt->condition(), &then_part, &else_part, &then_part);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000934 PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000935 __ bind(&then_part);
936 Visit(stmt->then_statement());
937 __ jmp(&done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000938
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000939 PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000940 __ bind(&else_part);
941 Visit(stmt->else_statement());
942 } else {
943 VisitForControl(stmt->condition(), &then_part, &done, &then_part);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000944 PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000945 __ bind(&then_part);
946 Visit(stmt->then_statement());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000947
948 PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000949 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000950 __ bind(&done);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000951 PrepareForBailoutForId(stmt->id(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000952}
953
954
955void FullCodeGenerator::VisitContinueStatement(ContinueStatement* stmt) {
956 Comment cmnt(masm_, "[ ContinueStatement");
957 SetStatementPosition(stmt);
958 NestedStatement* current = nesting_stack_;
959 int stack_depth = 0;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000960 // When continuing, we clobber the unpredictable value in the accumulator
961 // with one that's safe for GC. If we hit an exit from the try block of
962 // try...finally on our way out, we will unconditionally preserve the
963 // accumulator on the stack.
964 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000965 while (!current->IsContinueTarget(stmt->target())) {
966 stack_depth = current->Exit(stack_depth);
967 current = current->outer();
968 }
969 __ Drop(stack_depth);
970
971 Iteration* loop = current->AsIteration();
972 __ jmp(loop->continue_target());
973}
974
975
976void FullCodeGenerator::VisitBreakStatement(BreakStatement* stmt) {
977 Comment cmnt(masm_, "[ BreakStatement");
978 SetStatementPosition(stmt);
979 NestedStatement* current = nesting_stack_;
980 int stack_depth = 0;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000981 // When breaking, we clobber the unpredictable value in the accumulator
982 // with one that's safe for GC. If we hit an exit from the try block of
983 // try...finally on our way out, we will unconditionally preserve the
984 // accumulator on the stack.
985 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000986 while (!current->IsBreakTarget(stmt->target())) {
987 stack_depth = current->Exit(stack_depth);
988 current = current->outer();
989 }
990 __ Drop(stack_depth);
991
992 Breakable* target = current->AsBreakable();
993 __ jmp(target->break_target());
994}
995
996
997void FullCodeGenerator::VisitReturnStatement(ReturnStatement* stmt) {
998 Comment cmnt(masm_, "[ ReturnStatement");
999 SetStatementPosition(stmt);
1000 Expression* expr = stmt->expression();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001001 VisitForAccumulatorValue(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001002
1003 // Exit all nested statements.
1004 NestedStatement* current = nesting_stack_;
1005 int stack_depth = 0;
1006 while (current != NULL) {
1007 stack_depth = current->Exit(stack_depth);
1008 current = current->outer();
1009 }
1010 __ Drop(stack_depth);
1011
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001012 EmitReturnSequence();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001013}
1014
1015
1016void FullCodeGenerator::VisitWithEnterStatement(WithEnterStatement* stmt) {
1017 Comment cmnt(masm_, "[ WithEnterStatement");
1018 SetStatementPosition(stmt);
1019
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001020 VisitForStackValue(stmt->expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001021 if (stmt->is_catch_block()) {
1022 __ CallRuntime(Runtime::kPushCatchContext, 1);
1023 } else {
1024 __ CallRuntime(Runtime::kPushContext, 1);
1025 }
1026 // Both runtime calls return the new context in both the context and the
1027 // result registers.
1028
1029 // Update local stack frame context field.
1030 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
1031}
1032
1033
1034void FullCodeGenerator::VisitWithExitStatement(WithExitStatement* stmt) {
1035 Comment cmnt(masm_, "[ WithExitStatement");
1036 SetStatementPosition(stmt);
1037
1038 // Pop context.
1039 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1040 // Update local stack frame context field.
1041 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
1042}
1043
1044
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001045void FullCodeGenerator::VisitDoWhileStatement(DoWhileStatement* stmt) {
1046 Comment cmnt(masm_, "[ DoWhileStatement");
1047 SetStatementPosition(stmt);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001048 Label body, stack_check;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001049
1050 Iteration loop_statement(this, stmt);
1051 increment_loop_depth();
1052
1053 __ bind(&body);
1054 Visit(stmt->body());
1055
ricow@chromium.org65fae842010-08-25 15:26:24 +00001056 // Record the position of the do while condition and make sure it is
1057 // possible to break on the condition.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001058 __ bind(loop_statement.continue_target());
1059 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001060 SetExpressionPosition(stmt->cond(), stmt->condition_position());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001061 VisitForControl(stmt->cond(),
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001062 &stack_check,
ricow@chromium.org65fae842010-08-25 15:26:24 +00001063 loop_statement.break_target(),
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001064 &stack_check);
1065
1066 // Check stack before looping.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001067 PrepareForBailoutForId(stmt->BackEdgeId(), NO_REGISTERS);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001068 __ bind(&stack_check);
1069 EmitStackCheck(stmt);
1070 __ jmp(&body);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001071
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001072 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001073 __ bind(loop_statement.break_target());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001074 decrement_loop_depth();
1075}
1076
1077
1078void FullCodeGenerator::VisitWhileStatement(WhileStatement* stmt) {
1079 Comment cmnt(masm_, "[ WhileStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001080 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001081
1082 Iteration loop_statement(this, stmt);
1083 increment_loop_depth();
1084
1085 // Emit the test at the bottom of the loop.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001086 __ jmp(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001087
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001088 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001089 __ bind(&body);
1090 Visit(stmt->body());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001091
1092 // Emit the statement position here as this is where the while
1093 // statement code starts.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001094 __ bind(loop_statement.continue_target());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001095 SetStatementPosition(stmt);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001096
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001097 // Check stack before looping.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001098 EmitStackCheck(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001099
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001100 __ bind(&test);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001101 VisitForControl(stmt->cond(),
1102 &body,
1103 loop_statement.break_target(),
1104 loop_statement.break_target());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001105
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001106 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001107 __ bind(loop_statement.break_target());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001108 decrement_loop_depth();
1109}
1110
1111
1112void FullCodeGenerator::VisitForStatement(ForStatement* stmt) {
1113 Comment cmnt(masm_, "[ ForStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001114 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001115
1116 Iteration loop_statement(this, stmt);
1117 if (stmt->init() != NULL) {
1118 Visit(stmt->init());
1119 }
1120
1121 increment_loop_depth();
1122 // Emit the test at the bottom of the loop (even if empty).
1123 __ jmp(&test);
1124
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001125 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001126 __ bind(&body);
1127 Visit(stmt->body());
1128
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001129 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001130 __ bind(loop_statement.continue_target());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001131 SetStatementPosition(stmt);
1132 if (stmt->next() != NULL) {
1133 Visit(stmt->next());
1134 }
1135
ricow@chromium.org65fae842010-08-25 15:26:24 +00001136 // Emit the statement position here as this is where the for
1137 // statement code starts.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001138 SetStatementPosition(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001139
1140 // Check stack before looping.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001141 EmitStackCheck(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001142
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001143 __ bind(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001144 if (stmt->cond() != NULL) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00001145 VisitForControl(stmt->cond(),
1146 &body,
1147 loop_statement.break_target(),
1148 loop_statement.break_target());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001149 } else {
1150 __ jmp(&body);
1151 }
1152
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001153 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001154 __ bind(loop_statement.break_target());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001155 decrement_loop_depth();
1156}
1157
1158
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001159void FullCodeGenerator::VisitTryCatchStatement(TryCatchStatement* stmt) {
1160 Comment cmnt(masm_, "[ TryCatchStatement");
1161 SetStatementPosition(stmt);
1162 // The try block adds a handler to the exception handler chain
1163 // before entering, and removes it again when exiting normally.
1164 // If an exception is thrown during execution of the try block,
1165 // control is passed to the handler, which also consumes the handler.
1166 // At this point, the exception is in a register, and store it in
1167 // the temporary local variable (prints as ".catch-var") before
1168 // executing the catch block. The catch block has been rewritten
1169 // to introduce a new scope to bind the catch variable and to remove
1170 // that scope again afterwards.
1171
1172 Label try_handler_setup, catch_entry, done;
1173 __ Call(&try_handler_setup);
1174 // Try handler code, exception in result register.
1175
1176 // Store exception in local .catch variable before executing catch block.
1177 {
1178 // The catch variable is *always* a variable proxy for a local variable.
1179 Variable* catch_var = stmt->catch_var()->AsVariableProxy()->AsVariable();
1180 ASSERT_NOT_NULL(catch_var);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001181 Slot* variable_slot = catch_var->AsSlot();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001182 ASSERT_NOT_NULL(variable_slot);
1183 ASSERT_EQ(Slot::LOCAL, variable_slot->type());
1184 StoreToFrameField(SlotOffset(variable_slot), result_register());
1185 }
1186
1187 Visit(stmt->catch_block());
1188 __ jmp(&done);
1189
1190 // Try block code. Sets up the exception handler chain.
1191 __ bind(&try_handler_setup);
1192 {
1193 TryCatch try_block(this, &catch_entry);
1194 __ PushTryHandler(IN_JAVASCRIPT, TRY_CATCH_HANDLER);
1195 Visit(stmt->try_block());
1196 __ PopTryHandler();
1197 }
1198 __ bind(&done);
1199}
1200
1201
1202void FullCodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* stmt) {
1203 Comment cmnt(masm_, "[ TryFinallyStatement");
1204 SetStatementPosition(stmt);
1205 // Try finally is compiled by setting up a try-handler on the stack while
1206 // executing the try body, and removing it again afterwards.
1207 //
1208 // The try-finally construct can enter the finally block in three ways:
1209 // 1. By exiting the try-block normally. This removes the try-handler and
1210 // calls the finally block code before continuing.
1211 // 2. By exiting the try-block with a function-local control flow transfer
1212 // (break/continue/return). The site of the, e.g., break removes the
1213 // try handler and calls the finally block code before continuing
1214 // its outward control transfer.
1215 // 3. by exiting the try-block with a thrown exception.
1216 // This can happen in nested function calls. It traverses the try-handler
1217 // chain and consumes the try-handler entry before jumping to the
1218 // handler code. The handler code then calls the finally-block before
1219 // rethrowing the exception.
1220 //
1221 // The finally block must assume a return address on top of the stack
1222 // (or in the link register on ARM chips) and a value (return value or
1223 // exception) in the result register (rax/eax/r0), both of which must
1224 // be preserved. The return address isn't GC-safe, so it should be
1225 // cooked before GC.
1226 Label finally_entry;
1227 Label try_handler_setup;
1228
1229 // Setup the try-handler chain. Use a call to
1230 // Jump to try-handler setup and try-block code. Use call to put try-handler
1231 // address on stack.
1232 __ Call(&try_handler_setup);
1233 // Try handler code. Return address of call is pushed on handler stack.
1234 {
1235 // This code is only executed during stack-handler traversal when an
1236 // exception is thrown. The execption is in the result register, which
1237 // is retained by the finally block.
1238 // Call the finally block and then rethrow the exception.
1239 __ Call(&finally_entry);
1240 __ push(result_register());
1241 __ CallRuntime(Runtime::kReThrow, 1);
1242 }
1243
1244 __ bind(&finally_entry);
1245 {
1246 // Finally block implementation.
1247 Finally finally_block(this);
1248 EnterFinallyBlock();
1249 Visit(stmt->finally_block());
1250 ExitFinallyBlock(); // Return to the calling code.
1251 }
1252
1253 __ bind(&try_handler_setup);
1254 {
1255 // Setup try handler (stack pointer registers).
1256 TryFinally try_block(this, &finally_entry);
1257 __ PushTryHandler(IN_JAVASCRIPT, TRY_FINALLY_HANDLER);
1258 Visit(stmt->try_block());
1259 __ PopTryHandler();
1260 }
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001261 // Execute the finally block on the way out. Clobber the unpredictable
1262 // value in the accumulator with one that's safe for GC. The finally
1263 // block will unconditionally preserve the accumulator on the stack.
1264 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001265 __ Call(&finally_entry);
1266}
1267
1268
1269void FullCodeGenerator::VisitDebuggerStatement(DebuggerStatement* stmt) {
1270#ifdef ENABLE_DEBUGGER_SUPPORT
1271 Comment cmnt(masm_, "[ DebuggerStatement");
1272 SetStatementPosition(stmt);
1273
ager@chromium.org5c838252010-02-19 08:53:10 +00001274 __ DebugBreak();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001275 // Ignore the return value.
1276#endif
1277}
1278
1279
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001280void FullCodeGenerator::VisitConditional(Conditional* expr) {
1281 Comment cmnt(masm_, "[ Conditional");
1282 Label true_case, false_case, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001283 VisitForControl(expr->condition(), &true_case, &false_case, &true_case);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001284
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001285 PrepareForBailoutForId(expr->ThenId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001286 __ bind(&true_case);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001287 SetExpressionPosition(expr->then_expression(),
1288 expr->then_expression_position());
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001289 if (context()->IsTest()) {
1290 const TestContext* for_test = TestContext::cast(context());
1291 VisitForControl(expr->then_expression(),
1292 for_test->true_label(),
1293 for_test->false_label(),
1294 NULL);
1295 } else {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001296 context()->HandleExpression(expr->then_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001297 __ jmp(&done);
1298 }
1299
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001300 PrepareForBailoutForId(expr->ElseId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001301 __ bind(&false_case);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001302 if (context()->IsTest()) ForwardBailoutToChild(expr);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001303 SetExpressionPosition(expr->else_expression(),
1304 expr->else_expression_position());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001305 context()->HandleExpression(expr->else_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001306 // If control flow falls through Visit, merge it with true case here.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001307 if (!context()->IsTest()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001308 __ bind(&done);
1309 }
1310}
1311
1312
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001313void FullCodeGenerator::VisitLiteral(Literal* expr) {
1314 Comment cmnt(masm_, "[ Literal");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001315 context()->Plug(expr->handle());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001316}
1317
1318
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001319void FullCodeGenerator::VisitFunctionLiteral(FunctionLiteral* expr) {
1320 Comment cmnt(masm_, "[ FunctionLiteral");
1321
1322 // Build the function boilerplate and instantiate it.
1323 Handle<SharedFunctionInfo> function_info =
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001324 Compiler::BuildFunctionInfo(expr, script());
1325 if (function_info.is_null()) {
1326 SetStackOverflow();
1327 return;
1328 }
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001329 EmitNewClosure(function_info, expr->pretenure());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001330}
1331
1332
1333void FullCodeGenerator::VisitSharedFunctionInfoLiteral(
1334 SharedFunctionInfoLiteral* expr) {
1335 Comment cmnt(masm_, "[ SharedFunctionInfoLiteral");
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001336 EmitNewClosure(expr->shared_function_info(), false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001337}
1338
1339
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001340void FullCodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* expr) {
1341 // Call runtime routine to allocate the catch extension object and
1342 // assign the exception value to the catch variable.
1343 Comment cmnt(masm_, "[ CatchExtensionObject");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001344 VisitForStackValue(expr->key());
1345 VisitForStackValue(expr->value());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001346 // Create catch extension object.
1347 __ CallRuntime(Runtime::kCreateCatchExtensionObject, 2);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001348 context()->Plug(result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001349}
1350
1351
1352void FullCodeGenerator::VisitThrow(Throw* expr) {
1353 Comment cmnt(masm_, "[ Throw");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001354 VisitForStackValue(expr->exception());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001355 __ CallRuntime(Runtime::kThrow, 1);
1356 // Never returns here.
1357}
1358
1359
ricow@chromium.org65fae842010-08-25 15:26:24 +00001360void FullCodeGenerator::VisitIncrementOperation(IncrementOperation* expr) {
1361 UNREACHABLE();
1362}
1363
1364
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001365int FullCodeGenerator::TryFinally::Exit(int stack_depth) {
1366 // The macros used here must preserve the result register.
1367 __ Drop(stack_depth);
1368 __ PopTryHandler();
1369 __ Call(finally_entry_);
1370 return 0;
1371}
1372
1373
1374int FullCodeGenerator::TryCatch::Exit(int stack_depth) {
1375 // The macros used here must preserve the result register.
1376 __ Drop(stack_depth);
1377 __ PopTryHandler();
1378 return 0;
1379}
1380
ricow@chromium.org65fae842010-08-25 15:26:24 +00001381
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001382#undef __
1383
1384
1385} } // namespace v8::internal