blob: 9353ab515617006609013744cb2035b04d38eae1 [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());
245 Visit(expr->right());
246}
247
248
ricow@chromium.org65fae842010-08-25 15:26:24 +0000249void BreakableStatementChecker::VisitCompareToNull(CompareToNull* expr) {
250 Visit(expr->expression());
251}
252
253
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000254void BreakableStatementChecker::VisitCompareOperation(CompareOperation* expr) {
255 Visit(expr->left());
256 Visit(expr->right());
257}
258
259
260void BreakableStatementChecker::VisitThisFunction(ThisFunction* expr) {
261}
262
263
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000264#define __ ACCESS_MASM(masm())
265
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000266bool FullCodeGenerator::MakeCode(CompilationInfo* info) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000267 Isolate* isolate = info->isolate();
ager@chromium.org5c838252010-02-19 08:53:10 +0000268 Handle<Script> script = info->script();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000269 if (!script->IsUndefined() && !script->source()->IsUndefined()) {
270 int len = String::cast(script->source())->length();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000271 isolate->counters()->total_full_codegen_source_size()->Increment(len);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000272 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000273 if (FLAG_trace_codegen) {
274 PrintF("Full Compiler - ");
275 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000276 CodeGenerator::MakeCodePrologue(info);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000277 const int kInitialBufferSize = 4 * KB;
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000278 MacroAssembler masm(info->isolate(), NULL, kInitialBufferSize);
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000279#ifdef ENABLE_GDB_JIT_INTERFACE
280 masm.positions_recorder()->StartGDBJITLineInfoRecording();
281#endif
ager@chromium.org5c838252010-02-19 08:53:10 +0000282
283 FullCodeGenerator cgen(&masm);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000284 cgen.Generate(info);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000285 if (cgen.HasStackOverflow()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000286 ASSERT(!isolate->has_pending_exception());
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000287 return false;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000288 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000289 unsigned table_offset = cgen.EmitStackCheckTable();
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000290
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000291 Code::Flags flags = Code::ComputeFlags(Code::FUNCTION, NOT_IN_LOOP);
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000292 Handle<Code> code = CodeGenerator::MakeCodeEpilogue(&masm, flags, info);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000293 code->set_optimizable(info->IsOptimizable());
294 cgen.PopulateDeoptimizationData(code);
295 code->set_has_deoptimization_support(info->HasDeoptimizationSupport());
296 code->set_allow_osr_at_loop_nesting_level(0);
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000297 code->set_stack_check_table_offset(table_offset);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000298 CodeGenerator::PrintCode(code, info);
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000299 info->SetCode(code); // may be an empty handle.
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000300#ifdef ENABLE_GDB_JIT_INTERFACE
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000301 if (FLAG_gdbjit && !code.is_null()) {
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000302 GDBJITLineInfo* lineinfo =
303 masm.positions_recorder()->DetachGDBJITLineInfo();
304
305 GDBJIT(RegisterDetailedLineInfo(*code, lineinfo));
306 }
307#endif
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000308 return !code.is_null();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000309}
310
311
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000312unsigned FullCodeGenerator::EmitStackCheckTable() {
313 // The stack check table consists of a length (in number of entries)
314 // field, and then a sequence of entries. Each entry is a pair of AST id
315 // and code-relative pc offset.
316 masm()->Align(kIntSize);
317 masm()->RecordComment("[ Stack check table");
318 unsigned offset = masm()->pc_offset();
319 unsigned length = stack_checks_.length();
320 __ dd(length);
321 for (unsigned i = 0; i < length; ++i) {
322 __ dd(stack_checks_[i].id);
323 __ dd(stack_checks_[i].pc_and_state);
324 }
325 masm()->RecordComment("]");
326 return offset;
327}
328
329
330void FullCodeGenerator::PopulateDeoptimizationData(Handle<Code> code) {
331 // Fill in the deoptimization information.
332 ASSERT(info_->HasDeoptimizationSupport() || bailout_entries_.is_empty());
333 if (!info_->HasDeoptimizationSupport()) return;
334 int length = bailout_entries_.length();
335 Handle<DeoptimizationOutputData> data =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000336 isolate()->factory()->
337 NewDeoptimizationOutputData(length, TENURED);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000338 for (int i = 0; i < length; i++) {
339 data->SetAstId(i, Smi::FromInt(bailout_entries_[i].id));
340 data->SetPcAndState(i, Smi::FromInt(bailout_entries_[i].pc_and_state));
341 }
342 code->set_deoptimization_data(*data);
343}
344
345
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000346void FullCodeGenerator::PrepareForBailout(Expression* node, State state) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000347 PrepareForBailoutForId(node->id(), state);
348}
349
350
351void FullCodeGenerator::RecordJSReturnSite(Call* call) {
352 // We record the offset of the function return so we can rebuild the frame
353 // if the function was inlined, i.e., this is the return address in the
354 // inlined function's frame.
355 //
356 // The state is ignored. We defensively set it to TOS_REG, which is the
357 // real state of the unoptimized code at the return site.
358 PrepareForBailoutForId(call->ReturnId(), TOS_REG);
359#ifdef DEBUG
360 // In debug builds, mark the return so we can verify that this function
361 // was called.
362 ASSERT(!call->return_is_recorded_);
363 call->return_is_recorded_ = true;
364#endif
365}
366
367
368void FullCodeGenerator::PrepareForBailoutForId(int id, State state) {
369 // There's no need to prepare this code for bailouts from already optimized
370 // code or code that can't be optimized.
371 if (!FLAG_deopt || !info_->HasDeoptimizationSupport()) return;
372 unsigned pc_and_state =
373 StateField::encode(state) | PcField::encode(masm_->pc_offset());
374 BailoutEntry entry = { id, pc_and_state };
375#ifdef DEBUG
376 // Assert that we don't have multiple bailout entries for the same node.
377 for (int i = 0; i < bailout_entries_.length(); i++) {
378 if (bailout_entries_.at(i).id == entry.id) {
379 AstPrinter printer;
380 PrintF("%s", printer.PrintProgram(info_->function()));
381 UNREACHABLE();
382 }
383 }
384#endif // DEBUG
385 bailout_entries_.Add(entry);
386}
387
388
389void FullCodeGenerator::RecordStackCheck(int ast_id) {
390 // The pc offset does not need to be encoded and packed together with a
391 // state.
392 BailoutEntry entry = { ast_id, masm_->pc_offset() };
393 stack_checks_.Add(entry);
394}
395
396
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000397int FullCodeGenerator::SlotOffset(Slot* slot) {
398 ASSERT(slot != NULL);
399 // Offset is negative because higher indexes are at lower addresses.
400 int offset = -slot->index() * kPointerSize;
401 // Adjust by a (parameter or local) base offset.
402 switch (slot->type()) {
403 case Slot::PARAMETER:
ager@chromium.org5c838252010-02-19 08:53:10 +0000404 offset += (scope()->num_parameters() + 1) * kPointerSize;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000405 break;
406 case Slot::LOCAL:
407 offset += JavaScriptFrameConstants::kLocal0Offset;
408 break;
409 case Slot::CONTEXT:
410 case Slot::LOOKUP:
411 UNREACHABLE();
412 }
413 return offset;
414}
415
416
ricow@chromium.org65fae842010-08-25 15:26:24 +0000417bool FullCodeGenerator::ShouldInlineSmiCase(Token::Value op) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000418 // Inline smi case inside loops, but not division and modulo which
419 // are too complicated and take up too much space.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000420 if (op == Token::DIV ||op == Token::MOD) return false;
421 if (FLAG_always_inline_smi_code) return true;
422 return loop_depth_ > 0;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000423}
424
425
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000426void FullCodeGenerator::EffectContext::Plug(Register reg) const {
427}
428
429
430void FullCodeGenerator::AccumulatorValueContext::Plug(Register reg) const {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000431 __ Move(result_register(), reg);
432}
433
434
435void FullCodeGenerator::StackValueContext::Plug(Register reg) const {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000436 __ push(reg);
437}
438
439
440void FullCodeGenerator::TestContext::Plug(Register reg) const {
441 // For simplicity we always test the accumulator register.
442 __ Move(result_register(), reg);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000443 codegen()->PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000444 codegen()->DoTest(this);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000445}
446
447
448void FullCodeGenerator::EffectContext::PlugTOS() const {
449 __ Drop(1);
450}
451
452
453void FullCodeGenerator::AccumulatorValueContext::PlugTOS() const {
454 __ pop(result_register());
455}
456
457
458void FullCodeGenerator::StackValueContext::PlugTOS() const {
459}
460
461
462void FullCodeGenerator::TestContext::PlugTOS() const {
463 // For simplicity we always test the accumulator register.
464 __ pop(result_register());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000465 codegen()->PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000466 codegen()->DoTest(this);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000467}
468
469
470void FullCodeGenerator::EffectContext::PrepareTest(
471 Label* materialize_true,
472 Label* materialize_false,
473 Label** if_true,
474 Label** if_false,
475 Label** fall_through) const {
476 // In an effect context, the true and the false case branch to the
477 // same label.
478 *if_true = *if_false = *fall_through = materialize_true;
479}
480
481
482void FullCodeGenerator::AccumulatorValueContext::PrepareTest(
483 Label* materialize_true,
484 Label* materialize_false,
485 Label** if_true,
486 Label** if_false,
487 Label** fall_through) const {
488 *if_true = *fall_through = materialize_true;
489 *if_false = materialize_false;
490}
491
492
493void FullCodeGenerator::StackValueContext::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::TestContext::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 = true_label_;
511 *if_false = false_label_;
512 *fall_through = fall_through_;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000513}
514
515
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000516void FullCodeGenerator::DoTest(const TestContext* context) {
517 DoTest(context->condition(),
518 context->true_label(),
519 context->false_label(),
520 context->fall_through());
521}
522
523
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000524void FullCodeGenerator::VisitDeclarations(
525 ZoneList<Declaration*>* declarations) {
526 int length = declarations->length();
527 int globals = 0;
528 for (int i = 0; i < length; i++) {
529 Declaration* decl = declarations->at(i);
530 Variable* var = decl->proxy()->var();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000531 Slot* slot = var->AsSlot();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000532
533 // If it was not possible to allocate the variable at compile
534 // time, we need to "declare" it at runtime to make sure it
535 // actually exists in the local context.
536 if ((slot != NULL && slot->type() == Slot::LOOKUP) || !var->is_global()) {
537 VisitDeclaration(decl);
538 } else {
539 // Count global variables and functions for later processing
540 globals++;
541 }
542 }
543
544 // Compute array of global variable and function declarations.
545 // Do nothing in case of no declared global functions or variables.
546 if (globals > 0) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000547 Handle<FixedArray> array =
548 isolate()->factory()->NewFixedArray(2 * globals, TENURED);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000549 for (int j = 0, i = 0; i < length; i++) {
550 Declaration* decl = declarations->at(i);
551 Variable* var = decl->proxy()->var();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000552 Slot* slot = var->AsSlot();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000553
554 if ((slot == NULL || slot->type() != Slot::LOOKUP) && var->is_global()) {
555 array->set(j++, *(var->name()));
556 if (decl->fun() == NULL) {
557 if (var->mode() == Variable::CONST) {
558 // In case this is const property use the hole.
559 array->set_the_hole(j++);
560 } else {
561 array->set_undefined(j++);
562 }
563 } else {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000564 Handle<SharedFunctionInfo> function =
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000565 Compiler::BuildFunctionInfo(decl->fun(), script());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000566 // Check for stack-overflow exception.
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000567 if (function.is_null()) {
568 SetStackOverflow();
569 return;
570 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000571 array->set(j++, *function);
572 }
573 }
574 }
575 // Invoke the platform-dependent code generator to do the actual
576 // declaration the global variables and functions.
577 DeclareGlobals(array);
578 }
579}
580
581
582void FullCodeGenerator::SetFunctionPosition(FunctionLiteral* fun) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000583 CodeGenerator::RecordPositions(masm_, fun->start_position());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000584}
585
586
587void FullCodeGenerator::SetReturnPosition(FunctionLiteral* fun) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000588 CodeGenerator::RecordPositions(masm_, fun->end_position() - 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000589}
590
591
592void FullCodeGenerator::SetStatementPosition(Statement* stmt) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000593#ifdef ENABLE_DEBUGGER_SUPPORT
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000594 if (!isolate()->debugger()->IsDebuggerActive()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000595 CodeGenerator::RecordPositions(masm_, stmt->statement_pos());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000596 } else {
597 // Check if the statement will be breakable without adding a debug break
598 // slot.
599 BreakableStatementChecker checker;
600 checker.Check(stmt);
601 // Record the statement position right here if the statement is not
602 // breakable. For breakable statements the actual recording of the
603 // position will be postponed to the breakable code (typically an IC).
604 bool position_recorded = CodeGenerator::RecordPositions(
605 masm_, stmt->statement_pos(), !checker.is_breakable());
606 // If the position recording did record a new position generate a debug
607 // break slot to make the statement breakable.
608 if (position_recorded) {
609 Debug::GenerateSlot(masm_);
610 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000611 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000612#else
613 CodeGenerator::RecordPositions(masm_, stmt->statement_pos());
614#endif
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000615}
616
617
618void FullCodeGenerator::SetExpressionPosition(Expression* expr, int pos) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000619#ifdef ENABLE_DEBUGGER_SUPPORT
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000620 if (!isolate()->debugger()->IsDebuggerActive()) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000621 CodeGenerator::RecordPositions(masm_, pos);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000622 } else {
623 // Check if the expression will be breakable without adding a debug break
624 // slot.
625 BreakableStatementChecker checker;
626 checker.Check(expr);
627 // Record a statement position right here if the expression is not
628 // breakable. For breakable expressions the actual recording of the
629 // position will be postponed to the breakable code (typically an IC).
630 // NOTE this will record a statement position for something which might
631 // not be a statement. As stepping in the debugger will only stop at
632 // statement positions this is used for e.g. the condition expression of
633 // a do while loop.
634 bool position_recorded = CodeGenerator::RecordPositions(
635 masm_, pos, !checker.is_breakable());
636 // If the position recording did record a new position generate a debug
637 // break slot to make the statement breakable.
638 if (position_recorded) {
639 Debug::GenerateSlot(masm_);
640 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000641 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000642#else
643 CodeGenerator::RecordPositions(masm_, pos);
644#endif
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000645}
646
647
648void FullCodeGenerator::SetStatementPosition(int pos) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000649 CodeGenerator::RecordPositions(masm_, pos);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000650}
651
652
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000653void FullCodeGenerator::SetSourcePosition(int pos) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000654 if (pos != RelocInfo::kNoPosition) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000655 masm_->positions_recorder()->RecordPosition(pos);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000656 }
657}
658
659
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000660// Lookup table for code generators for special runtime calls which are
661// generated inline.
662#define INLINE_FUNCTION_GENERATOR_ADDRESS(Name, argc, ressize) \
663 &FullCodeGenerator::Emit##Name,
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000664
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000665const FullCodeGenerator::InlineFunctionGenerator
666 FullCodeGenerator::kInlineFunctionGenerators[] = {
667 INLINE_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS)
668 INLINE_RUNTIME_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS)
669 };
670#undef INLINE_FUNCTION_GENERATOR_ADDRESS
671
672
673FullCodeGenerator::InlineFunctionGenerator
674 FullCodeGenerator::FindInlineFunctionGenerator(Runtime::FunctionId id) {
whesse@chromium.org023421e2010-12-21 12:19:12 +0000675 int lookup_index =
676 static_cast<int>(id) - static_cast<int>(Runtime::kFirstInlineFunction);
677 ASSERT(lookup_index >= 0);
678 ASSERT(static_cast<size_t>(lookup_index) <
679 ARRAY_SIZE(kInlineFunctionGenerators));
680 return kInlineFunctionGenerators[lookup_index];
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000681}
682
683
684void FullCodeGenerator::EmitInlineRuntimeCall(CallRuntime* node) {
685 ZoneList<Expression*>* args = node->arguments();
686 Handle<String> name = node->name();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000687 const Runtime::Function* function = node->function();
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000688 ASSERT(function != NULL);
689 ASSERT(function->intrinsic_type == Runtime::INLINE);
690 InlineFunctionGenerator generator =
691 FindInlineFunctionGenerator(function->function_id);
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000692 ((*this).*(generator))(args);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000693}
694
695
696void FullCodeGenerator::VisitBinaryOperation(BinaryOperation* expr) {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000697 switch (expr->op()) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000698 case Token::COMMA:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000699 return VisitComma(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000700 case Token::OR:
701 case Token::AND:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000702 return VisitLogicalExpression(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000703 default:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000704 return VisitArithmeticExpression(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000705 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000706}
707
708
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000709void FullCodeGenerator::VisitComma(BinaryOperation* expr) {
710 Comment cmnt(masm_, "[ Comma");
711 VisitForEffect(expr->left());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000712 if (context()->IsTest()) ForwardBailoutToChild(expr);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000713 VisitInCurrentContext(expr->right());
714}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000715
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000716
717void FullCodeGenerator::VisitLogicalExpression(BinaryOperation* expr) {
718 bool is_logical_and = expr->op() == Token::AND;
719 Comment cmnt(masm_, is_logical_and ? "[ Logical AND" : "[ Logical OR");
720 Expression* left = expr->left();
721 Expression* right = expr->right();
722 int right_id = expr->RightId();
723 Label done;
724
725 if (context()->IsTest()) {
726 Label eval_right;
727 const TestContext* test = TestContext::cast(context());
728 if (is_logical_and) {
729 VisitForControl(left, &eval_right, test->false_label(), &eval_right);
730 } else {
731 VisitForControl(left, test->true_label(), &eval_right, &eval_right);
732 }
733 PrepareForBailoutForId(right_id, NO_REGISTERS);
734 __ bind(&eval_right);
735 ForwardBailoutToChild(expr);
736
737 } else if (context()->IsAccumulatorValue()) {
738 VisitForAccumulatorValue(left);
739 // We want the value in the accumulator for the test, and on the stack in
740 // case we need it.
741 __ push(result_register());
742 Label discard, restore;
743 PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
744 if (is_logical_and) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000745 DoTest(left, &discard, &restore, &restore);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000746 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000747 DoTest(left, &restore, &discard, &restore);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000748 }
749 __ bind(&restore);
750 __ pop(result_register());
751 __ jmp(&done);
752 __ bind(&discard);
753 __ Drop(1);
754 PrepareForBailoutForId(right_id, NO_REGISTERS);
755
756 } else if (context()->IsStackValue()) {
757 VisitForAccumulatorValue(left);
758 // We want the value in the accumulator for the test, and on the stack in
759 // case we need it.
760 __ push(result_register());
761 Label discard;
762 PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
763 if (is_logical_and) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000764 DoTest(left, &discard, &done, &discard);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000765 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000766 DoTest(left, &done, &discard, &discard);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000767 }
768 __ bind(&discard);
769 __ Drop(1);
770 PrepareForBailoutForId(right_id, NO_REGISTERS);
771
772 } else {
773 ASSERT(context()->IsEffect());
774 Label eval_right;
775 if (is_logical_and) {
776 VisitForControl(left, &eval_right, &done, &eval_right);
777 } else {
778 VisitForControl(left, &done, &eval_right, &eval_right);
779 }
780 PrepareForBailoutForId(right_id, NO_REGISTERS);
781 __ bind(&eval_right);
782 }
783
784 VisitInCurrentContext(right);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000785 __ bind(&done);
786}
787
788
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000789void FullCodeGenerator::VisitArithmeticExpression(BinaryOperation* expr) {
790 Token::Value op = expr->op();
791 Comment cmnt(masm_, "[ ArithmeticExpression");
792 Expression* left = expr->left();
793 Expression* right = expr->right();
794 OverwriteMode mode =
795 left->ResultOverwriteAllowed()
796 ? OVERWRITE_LEFT
797 : (right->ResultOverwriteAllowed() ? OVERWRITE_RIGHT : NO_OVERWRITE);
798
799 VisitForStackValue(left);
800 VisitForAccumulatorValue(right);
801
802 SetSourcePosition(expr->position());
803 if (ShouldInlineSmiCase(op)) {
804 EmitInlineSmiBinaryOp(expr, op, mode, left, right);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000805 } else {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000806 EmitBinaryOp(expr, op, mode);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000807 }
808}
809
810
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000811void FullCodeGenerator::ForwardBailoutToChild(Expression* expr) {
812 if (!info_->HasDeoptimizationSupport()) return;
813 ASSERT(context()->IsTest());
814 ASSERT(expr == forward_bailout_stack_->expr());
815 forward_bailout_pending_ = forward_bailout_stack_;
816}
817
818
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000819void FullCodeGenerator::VisitInCurrentContext(Expression* expr) {
820 if (context()->IsTest()) {
821 ForwardBailoutStack stack(expr, forward_bailout_pending_);
822 ForwardBailoutStack* saved = forward_bailout_stack_;
823 forward_bailout_pending_ = NULL;
824 forward_bailout_stack_ = &stack;
825 Visit(expr);
826 forward_bailout_stack_ = saved;
827 } else {
828 ASSERT(forward_bailout_pending_ == NULL);
829 Visit(expr);
830 State state = context()->IsAccumulatorValue() ? TOS_REG : NO_REGISTERS;
831 PrepareForBailout(expr, state);
832 // Forwarding bailouts to children is a one shot operation. It should have
833 // been processed at this point.
834 ASSERT(forward_bailout_pending_ == NULL);
835 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000836}
837
838
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000839void FullCodeGenerator::VisitBlock(Block* stmt) {
840 Comment cmnt(masm_, "[ Block");
841 Breakable nested_statement(this, stmt);
842 SetStatementPosition(stmt);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000843
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +0000844 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000845 VisitStatements(stmt->statements());
846 __ bind(nested_statement.break_target());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000847 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000848}
849
850
851void FullCodeGenerator::VisitExpressionStatement(ExpressionStatement* stmt) {
852 Comment cmnt(masm_, "[ ExpressionStatement");
853 SetStatementPosition(stmt);
854 VisitForEffect(stmt->expression());
855}
856
857
858void FullCodeGenerator::VisitEmptyStatement(EmptyStatement* stmt) {
859 Comment cmnt(masm_, "[ EmptyStatement");
860 SetStatementPosition(stmt);
861}
862
863
864void FullCodeGenerator::VisitIfStatement(IfStatement* stmt) {
865 Comment cmnt(masm_, "[ IfStatement");
866 SetStatementPosition(stmt);
867 Label then_part, else_part, done;
868
ricow@chromium.org65fae842010-08-25 15:26:24 +0000869 if (stmt->HasElseStatement()) {
870 VisitForControl(stmt->condition(), &then_part, &else_part, &then_part);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000871 PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000872 __ bind(&then_part);
873 Visit(stmt->then_statement());
874 __ jmp(&done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000875
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000876 PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000877 __ bind(&else_part);
878 Visit(stmt->else_statement());
879 } else {
880 VisitForControl(stmt->condition(), &then_part, &done, &then_part);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000881 PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000882 __ bind(&then_part);
883 Visit(stmt->then_statement());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000884
885 PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000886 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000887 __ bind(&done);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000888 PrepareForBailoutForId(stmt->IfId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000889}
890
891
892void FullCodeGenerator::VisitContinueStatement(ContinueStatement* stmt) {
893 Comment cmnt(masm_, "[ ContinueStatement");
894 SetStatementPosition(stmt);
895 NestedStatement* current = nesting_stack_;
896 int stack_depth = 0;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000897 // When continuing, we clobber the unpredictable value in the accumulator
898 // with one that's safe for GC. If we hit an exit from the try block of
899 // try...finally on our way out, we will unconditionally preserve the
900 // accumulator on the stack.
901 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000902 while (!current->IsContinueTarget(stmt->target())) {
903 stack_depth = current->Exit(stack_depth);
904 current = current->outer();
905 }
906 __ Drop(stack_depth);
907
908 Iteration* loop = current->AsIteration();
909 __ jmp(loop->continue_target());
910}
911
912
913void FullCodeGenerator::VisitBreakStatement(BreakStatement* stmt) {
914 Comment cmnt(masm_, "[ BreakStatement");
915 SetStatementPosition(stmt);
916 NestedStatement* current = nesting_stack_;
917 int stack_depth = 0;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000918 // When breaking, we clobber the unpredictable value in the accumulator
919 // with one that's safe for GC. If we hit an exit from the try block of
920 // try...finally on our way out, we will unconditionally preserve the
921 // accumulator on the stack.
922 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000923 while (!current->IsBreakTarget(stmt->target())) {
924 stack_depth = current->Exit(stack_depth);
925 current = current->outer();
926 }
927 __ Drop(stack_depth);
928
929 Breakable* target = current->AsBreakable();
930 __ jmp(target->break_target());
931}
932
933
934void FullCodeGenerator::VisitReturnStatement(ReturnStatement* stmt) {
935 Comment cmnt(masm_, "[ ReturnStatement");
936 SetStatementPosition(stmt);
937 Expression* expr = stmt->expression();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000938 VisitForAccumulatorValue(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000939
940 // Exit all nested statements.
941 NestedStatement* current = nesting_stack_;
942 int stack_depth = 0;
943 while (current != NULL) {
944 stack_depth = current->Exit(stack_depth);
945 current = current->outer();
946 }
947 __ Drop(stack_depth);
948
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000949 EmitReturnSequence();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000950}
951
952
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000953void FullCodeGenerator::VisitEnterWithContextStatement(
954 EnterWithContextStatement* stmt) {
955 Comment cmnt(masm_, "[ EnterWithContextStatement");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000956 SetStatementPosition(stmt);
957
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000958 VisitForStackValue(stmt->expression());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000959 __ CallRuntime(Runtime::kPushWithContext, 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000960 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
961}
962
963
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000964void FullCodeGenerator::VisitExitContextStatement(ExitContextStatement* stmt) {
965 Comment cmnt(masm_, "[ ExitContextStatement");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000966 SetStatementPosition(stmt);
967
968 // Pop context.
969 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
970 // Update local stack frame context field.
971 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
972}
973
974
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000975void FullCodeGenerator::VisitDoWhileStatement(DoWhileStatement* stmt) {
976 Comment cmnt(masm_, "[ DoWhileStatement");
977 SetStatementPosition(stmt);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000978 Label body, stack_check;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000979
980 Iteration loop_statement(this, stmt);
981 increment_loop_depth();
982
983 __ bind(&body);
984 Visit(stmt->body());
985
ricow@chromium.org65fae842010-08-25 15:26:24 +0000986 // Record the position of the do while condition and make sure it is
987 // possible to break on the condition.
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000988 __ bind(loop_statement.continue_target());
989 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000990 SetExpressionPosition(stmt->cond(), stmt->condition_position());
ricow@chromium.org65fae842010-08-25 15:26:24 +0000991 VisitForControl(stmt->cond(),
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000992 &stack_check,
ricow@chromium.org65fae842010-08-25 15:26:24 +0000993 loop_statement.break_target(),
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000994 &stack_check);
995
996 // Check stack before looping.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000997 PrepareForBailoutForId(stmt->BackEdgeId(), NO_REGISTERS);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000998 __ bind(&stack_check);
999 EmitStackCheck(stmt);
1000 __ jmp(&body);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001001
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001002 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001003 __ bind(loop_statement.break_target());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001004 decrement_loop_depth();
1005}
1006
1007
1008void FullCodeGenerator::VisitWhileStatement(WhileStatement* stmt) {
1009 Comment cmnt(masm_, "[ WhileStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001010 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001011
1012 Iteration loop_statement(this, stmt);
1013 increment_loop_depth();
1014
1015 // Emit the test at the bottom of the loop.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001016 __ jmp(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001017
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001018 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001019 __ bind(&body);
1020 Visit(stmt->body());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001021
1022 // Emit the statement position here as this is where the while
1023 // statement code starts.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001024 __ bind(loop_statement.continue_target());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001025 SetStatementPosition(stmt);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001026
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001027 // Check stack before looping.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001028 EmitStackCheck(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001029
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001030 __ bind(&test);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001031 VisitForControl(stmt->cond(),
1032 &body,
1033 loop_statement.break_target(),
1034 loop_statement.break_target());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001035
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001036 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001037 __ bind(loop_statement.break_target());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001038 decrement_loop_depth();
1039}
1040
1041
1042void FullCodeGenerator::VisitForStatement(ForStatement* stmt) {
1043 Comment cmnt(masm_, "[ ForStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001044 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001045
1046 Iteration loop_statement(this, stmt);
1047 if (stmt->init() != NULL) {
1048 Visit(stmt->init());
1049 }
1050
1051 increment_loop_depth();
1052 // Emit the test at the bottom of the loop (even if empty).
1053 __ jmp(&test);
1054
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001055 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001056 __ bind(&body);
1057 Visit(stmt->body());
1058
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001059 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001060 __ bind(loop_statement.continue_target());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001061 SetStatementPosition(stmt);
1062 if (stmt->next() != NULL) {
1063 Visit(stmt->next());
1064 }
1065
ricow@chromium.org65fae842010-08-25 15:26:24 +00001066 // Emit the statement position here as this is where the for
1067 // statement code starts.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001068 SetStatementPosition(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001069
1070 // Check stack before looping.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001071 EmitStackCheck(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001072
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001073 __ bind(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001074 if (stmt->cond() != NULL) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00001075 VisitForControl(stmt->cond(),
1076 &body,
1077 loop_statement.break_target(),
1078 loop_statement.break_target());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001079 } else {
1080 __ jmp(&body);
1081 }
1082
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001083 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001084 __ bind(loop_statement.break_target());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001085 decrement_loop_depth();
1086}
1087
1088
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001089void FullCodeGenerator::VisitTryCatchStatement(TryCatchStatement* stmt) {
1090 Comment cmnt(masm_, "[ TryCatchStatement");
1091 SetStatementPosition(stmt);
1092 // The try block adds a handler to the exception handler chain
1093 // before entering, and removes it again when exiting normally.
1094 // If an exception is thrown during execution of the try block,
1095 // control is passed to the handler, which also consumes the handler.
1096 // At this point, the exception is in a register, and store it in
1097 // the temporary local variable (prints as ".catch-var") before
1098 // executing the catch block. The catch block has been rewritten
1099 // to introduce a new scope to bind the catch variable and to remove
1100 // that scope again afterwards.
1101
1102 Label try_handler_setup, catch_entry, done;
1103 __ Call(&try_handler_setup);
1104 // Try handler code, exception in result register.
1105
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001106 // Extend the context before executing the catch block.
1107 { Comment cmnt(masm_, "[ Extend catch context");
1108 __ Push(stmt->name());
1109 __ push(result_register());
1110 __ CallRuntime(Runtime::kPushCatchContext, 2);
1111 StoreToFrameField(StandardFrameConstants::kContextOffset,
1112 context_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001113 }
1114
1115 Visit(stmt->catch_block());
1116 __ jmp(&done);
1117
1118 // Try block code. Sets up the exception handler chain.
1119 __ bind(&try_handler_setup);
1120 {
1121 TryCatch try_block(this, &catch_entry);
1122 __ PushTryHandler(IN_JAVASCRIPT, TRY_CATCH_HANDLER);
1123 Visit(stmt->try_block());
1124 __ PopTryHandler();
1125 }
1126 __ bind(&done);
1127}
1128
1129
1130void FullCodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* stmt) {
1131 Comment cmnt(masm_, "[ TryFinallyStatement");
1132 SetStatementPosition(stmt);
1133 // Try finally is compiled by setting up a try-handler on the stack while
1134 // executing the try body, and removing it again afterwards.
1135 //
1136 // The try-finally construct can enter the finally block in three ways:
1137 // 1. By exiting the try-block normally. This removes the try-handler and
1138 // calls the finally block code before continuing.
1139 // 2. By exiting the try-block with a function-local control flow transfer
1140 // (break/continue/return). The site of the, e.g., break removes the
1141 // try handler and calls the finally block code before continuing
1142 // its outward control transfer.
1143 // 3. by exiting the try-block with a thrown exception.
1144 // This can happen in nested function calls. It traverses the try-handler
1145 // chain and consumes the try-handler entry before jumping to the
1146 // handler code. The handler code then calls the finally-block before
1147 // rethrowing the exception.
1148 //
1149 // The finally block must assume a return address on top of the stack
1150 // (or in the link register on ARM chips) and a value (return value or
1151 // exception) in the result register (rax/eax/r0), both of which must
1152 // be preserved. The return address isn't GC-safe, so it should be
1153 // cooked before GC.
1154 Label finally_entry;
1155 Label try_handler_setup;
1156
1157 // Setup the try-handler chain. Use a call to
1158 // Jump to try-handler setup and try-block code. Use call to put try-handler
1159 // address on stack.
1160 __ Call(&try_handler_setup);
1161 // Try handler code. Return address of call is pushed on handler stack.
1162 {
1163 // This code is only executed during stack-handler traversal when an
1164 // exception is thrown. The execption is in the result register, which
1165 // is retained by the finally block.
1166 // Call the finally block and then rethrow the exception.
1167 __ Call(&finally_entry);
1168 __ push(result_register());
1169 __ CallRuntime(Runtime::kReThrow, 1);
1170 }
1171
1172 __ bind(&finally_entry);
1173 {
1174 // Finally block implementation.
1175 Finally finally_block(this);
1176 EnterFinallyBlock();
1177 Visit(stmt->finally_block());
1178 ExitFinallyBlock(); // Return to the calling code.
1179 }
1180
1181 __ bind(&try_handler_setup);
1182 {
1183 // Setup try handler (stack pointer registers).
1184 TryFinally try_block(this, &finally_entry);
1185 __ PushTryHandler(IN_JAVASCRIPT, TRY_FINALLY_HANDLER);
1186 Visit(stmt->try_block());
1187 __ PopTryHandler();
1188 }
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001189 // Execute the finally block on the way out. Clobber the unpredictable
1190 // value in the accumulator with one that's safe for GC. The finally
1191 // block will unconditionally preserve the accumulator on the stack.
1192 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001193 __ Call(&finally_entry);
1194}
1195
1196
1197void FullCodeGenerator::VisitDebuggerStatement(DebuggerStatement* stmt) {
1198#ifdef ENABLE_DEBUGGER_SUPPORT
1199 Comment cmnt(masm_, "[ DebuggerStatement");
1200 SetStatementPosition(stmt);
1201
ager@chromium.org5c838252010-02-19 08:53:10 +00001202 __ DebugBreak();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001203 // Ignore the return value.
1204#endif
1205}
1206
1207
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001208void FullCodeGenerator::VisitConditional(Conditional* expr) {
1209 Comment cmnt(masm_, "[ Conditional");
1210 Label true_case, false_case, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001211 VisitForControl(expr->condition(), &true_case, &false_case, &true_case);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001212
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001213 PrepareForBailoutForId(expr->ThenId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001214 __ bind(&true_case);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001215 SetExpressionPosition(expr->then_expression(),
1216 expr->then_expression_position());
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001217 if (context()->IsTest()) {
1218 const TestContext* for_test = TestContext::cast(context());
1219 VisitForControl(expr->then_expression(),
1220 for_test->true_label(),
1221 for_test->false_label(),
1222 NULL);
1223 } else {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001224 VisitInCurrentContext(expr->then_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001225 __ jmp(&done);
1226 }
1227
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001228 PrepareForBailoutForId(expr->ElseId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001229 __ bind(&false_case);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001230 if (context()->IsTest()) ForwardBailoutToChild(expr);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001231 SetExpressionPosition(expr->else_expression(),
1232 expr->else_expression_position());
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001233 VisitInCurrentContext(expr->else_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001234 // If control flow falls through Visit, merge it with true case here.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001235 if (!context()->IsTest()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001236 __ bind(&done);
1237 }
1238}
1239
1240
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001241void FullCodeGenerator::VisitLiteral(Literal* expr) {
1242 Comment cmnt(masm_, "[ Literal");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001243 context()->Plug(expr->handle());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001244}
1245
1246
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001247void FullCodeGenerator::VisitFunctionLiteral(FunctionLiteral* expr) {
1248 Comment cmnt(masm_, "[ FunctionLiteral");
1249
1250 // Build the function boilerplate and instantiate it.
1251 Handle<SharedFunctionInfo> function_info =
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001252 Compiler::BuildFunctionInfo(expr, script());
1253 if (function_info.is_null()) {
1254 SetStackOverflow();
1255 return;
1256 }
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001257 EmitNewClosure(function_info, expr->pretenure());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001258}
1259
1260
1261void FullCodeGenerator::VisitSharedFunctionInfoLiteral(
1262 SharedFunctionInfoLiteral* expr) {
1263 Comment cmnt(masm_, "[ SharedFunctionInfoLiteral");
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001264 EmitNewClosure(expr->shared_function_info(), false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001265}
1266
1267
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001268void FullCodeGenerator::VisitThrow(Throw* expr) {
1269 Comment cmnt(masm_, "[ Throw");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001270 VisitForStackValue(expr->exception());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001271 __ CallRuntime(Runtime::kThrow, 1);
1272 // Never returns here.
1273}
1274
1275
1276int FullCodeGenerator::TryFinally::Exit(int stack_depth) {
1277 // The macros used here must preserve the result register.
1278 __ Drop(stack_depth);
1279 __ PopTryHandler();
1280 __ Call(finally_entry_);
1281 return 0;
1282}
1283
1284
1285int FullCodeGenerator::TryCatch::Exit(int stack_depth) {
1286 // The macros used here must preserve the result register.
1287 __ Drop(stack_depth);
1288 __ PopTryHandler();
1289 return 0;
1290}
1291
ricow@chromium.org65fae842010-08-25 15:26:24 +00001292
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001293#undef __
1294
1295
1296} } // namespace v8::internal