blob: 6604e4140151a60e29b3a189305769120b314504 [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
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
216void BreakableStatementChecker::VisitProperty(Property* expr) {
217 // Property load is breakable.
218 is_breakable_ = true;
219}
220
221
222void BreakableStatementChecker::VisitCall(Call* expr) {
223 // Function calls both through IC and call stub are breakable.
224 is_breakable_ = true;
225}
226
227
228void BreakableStatementChecker::VisitCallNew(CallNew* expr) {
229 // Function calls through new are breakable.
230 is_breakable_ = true;
231}
232
233
234void BreakableStatementChecker::VisitCallRuntime(CallRuntime* expr) {
235}
236
237
238void BreakableStatementChecker::VisitUnaryOperation(UnaryOperation* expr) {
239 Visit(expr->expression());
240}
241
242
243void BreakableStatementChecker::VisitCountOperation(CountOperation* expr) {
244 Visit(expr->expression());
245}
246
247
248void BreakableStatementChecker::VisitBinaryOperation(BinaryOperation* expr) {
249 Visit(expr->left());
250 Visit(expr->right());
251}
252
253
ricow@chromium.org65fae842010-08-25 15:26:24 +0000254void BreakableStatementChecker::VisitCompareToNull(CompareToNull* expr) {
255 Visit(expr->expression());
256}
257
258
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000259void BreakableStatementChecker::VisitCompareOperation(CompareOperation* expr) {
260 Visit(expr->left());
261 Visit(expr->right());
262}
263
264
265void BreakableStatementChecker::VisitThisFunction(ThisFunction* expr) {
266}
267
268
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000269#define __ ACCESS_MASM(masm())
270
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000271bool FullCodeGenerator::MakeCode(CompilationInfo* info) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000272 Isolate* isolate = info->isolate();
ager@chromium.org5c838252010-02-19 08:53:10 +0000273 Handle<Script> script = info->script();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000274 if (!script->IsUndefined() && !script->source()->IsUndefined()) {
275 int len = String::cast(script->source())->length();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000276 isolate->counters()->total_full_codegen_source_size()->Increment(len);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000277 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000278 if (FLAG_trace_codegen) {
279 PrintF("Full Compiler - ");
280 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000281 CodeGenerator::MakeCodePrologue(info);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000282 const int kInitialBufferSize = 4 * KB;
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000283 MacroAssembler masm(info->isolate(), NULL, kInitialBufferSize);
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000284#ifdef ENABLE_GDB_JIT_INTERFACE
285 masm.positions_recorder()->StartGDBJITLineInfoRecording();
286#endif
ager@chromium.org5c838252010-02-19 08:53:10 +0000287
288 FullCodeGenerator cgen(&masm);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000289 cgen.Generate(info);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000290 if (cgen.HasStackOverflow()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000291 ASSERT(!isolate->has_pending_exception());
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000292 return false;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000293 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000294 unsigned table_offset = cgen.EmitStackCheckTable();
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000295
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000296 Code::Flags flags = Code::ComputeFlags(Code::FUNCTION, NOT_IN_LOOP);
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000297 Handle<Code> code = CodeGenerator::MakeCodeEpilogue(&masm, flags, info);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000298 code->set_optimizable(info->IsOptimizable());
299 cgen.PopulateDeoptimizationData(code);
300 code->set_has_deoptimization_support(info->HasDeoptimizationSupport());
301 code->set_allow_osr_at_loop_nesting_level(0);
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000302 code->set_stack_check_table_offset(table_offset);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000303 CodeGenerator::PrintCode(code, info);
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000304 info->SetCode(code); // may be an empty handle.
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000305#ifdef ENABLE_GDB_JIT_INTERFACE
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000306 if (FLAG_gdbjit && !code.is_null()) {
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000307 GDBJITLineInfo* lineinfo =
308 masm.positions_recorder()->DetachGDBJITLineInfo();
309
310 GDBJIT(RegisterDetailedLineInfo(*code, lineinfo));
311 }
312#endif
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000313 return !code.is_null();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000314}
315
316
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000317unsigned FullCodeGenerator::EmitStackCheckTable() {
318 // The stack check table consists of a length (in number of entries)
319 // field, and then a sequence of entries. Each entry is a pair of AST id
320 // and code-relative pc offset.
321 masm()->Align(kIntSize);
322 masm()->RecordComment("[ Stack check table");
323 unsigned offset = masm()->pc_offset();
324 unsigned length = stack_checks_.length();
325 __ dd(length);
326 for (unsigned i = 0; i < length; ++i) {
327 __ dd(stack_checks_[i].id);
328 __ dd(stack_checks_[i].pc_and_state);
329 }
330 masm()->RecordComment("]");
331 return offset;
332}
333
334
335void FullCodeGenerator::PopulateDeoptimizationData(Handle<Code> code) {
336 // Fill in the deoptimization information.
337 ASSERT(info_->HasDeoptimizationSupport() || bailout_entries_.is_empty());
338 if (!info_->HasDeoptimizationSupport()) return;
339 int length = bailout_entries_.length();
340 Handle<DeoptimizationOutputData> data =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000341 isolate()->factory()->
342 NewDeoptimizationOutputData(length, TENURED);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000343 for (int i = 0; i < length; i++) {
344 data->SetAstId(i, Smi::FromInt(bailout_entries_[i].id));
345 data->SetPcAndState(i, Smi::FromInt(bailout_entries_[i].pc_and_state));
346 }
347 code->set_deoptimization_data(*data);
348}
349
350
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000351void FullCodeGenerator::PrepareForBailout(Expression* node, State state) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000352 PrepareForBailoutForId(node->id(), state);
353}
354
355
356void FullCodeGenerator::RecordJSReturnSite(Call* call) {
357 // We record the offset of the function return so we can rebuild the frame
358 // if the function was inlined, i.e., this is the return address in the
359 // inlined function's frame.
360 //
361 // The state is ignored. We defensively set it to TOS_REG, which is the
362 // real state of the unoptimized code at the return site.
363 PrepareForBailoutForId(call->ReturnId(), TOS_REG);
364#ifdef DEBUG
365 // In debug builds, mark the return so we can verify that this function
366 // was called.
367 ASSERT(!call->return_is_recorded_);
368 call->return_is_recorded_ = true;
369#endif
370}
371
372
373void FullCodeGenerator::PrepareForBailoutForId(int id, State state) {
374 // There's no need to prepare this code for bailouts from already optimized
375 // code or code that can't be optimized.
376 if (!FLAG_deopt || !info_->HasDeoptimizationSupport()) return;
377 unsigned pc_and_state =
378 StateField::encode(state) | PcField::encode(masm_->pc_offset());
379 BailoutEntry entry = { id, pc_and_state };
380#ifdef DEBUG
381 // Assert that we don't have multiple bailout entries for the same node.
382 for (int i = 0; i < bailout_entries_.length(); i++) {
383 if (bailout_entries_.at(i).id == entry.id) {
384 AstPrinter printer;
385 PrintF("%s", printer.PrintProgram(info_->function()));
386 UNREACHABLE();
387 }
388 }
389#endif // DEBUG
390 bailout_entries_.Add(entry);
391}
392
393
394void FullCodeGenerator::RecordStackCheck(int ast_id) {
395 // The pc offset does not need to be encoded and packed together with a
396 // state.
397 BailoutEntry entry = { ast_id, masm_->pc_offset() };
398 stack_checks_.Add(entry);
399}
400
401
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000402int FullCodeGenerator::SlotOffset(Slot* slot) {
403 ASSERT(slot != NULL);
404 // Offset is negative because higher indexes are at lower addresses.
405 int offset = -slot->index() * kPointerSize;
406 // Adjust by a (parameter or local) base offset.
407 switch (slot->type()) {
408 case Slot::PARAMETER:
ager@chromium.org5c838252010-02-19 08:53:10 +0000409 offset += (scope()->num_parameters() + 1) * kPointerSize;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000410 break;
411 case Slot::LOCAL:
412 offset += JavaScriptFrameConstants::kLocal0Offset;
413 break;
414 case Slot::CONTEXT:
415 case Slot::LOOKUP:
416 UNREACHABLE();
417 }
418 return offset;
419}
420
421
ricow@chromium.org65fae842010-08-25 15:26:24 +0000422bool FullCodeGenerator::ShouldInlineSmiCase(Token::Value op) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000423 // Inline smi case inside loops, but not division and modulo which
424 // are too complicated and take up too much space.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000425 if (op == Token::DIV ||op == Token::MOD) return false;
426 if (FLAG_always_inline_smi_code) return true;
427 return loop_depth_ > 0;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000428}
429
430
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000431void FullCodeGenerator::EffectContext::Plug(Register reg) const {
432}
433
434
435void FullCodeGenerator::AccumulatorValueContext::Plug(Register reg) const {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000436 __ Move(result_register(), reg);
437}
438
439
440void FullCodeGenerator::StackValueContext::Plug(Register reg) const {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000441 __ push(reg);
442}
443
444
445void FullCodeGenerator::TestContext::Plug(Register reg) const {
446 // For simplicity we always test the accumulator register.
447 __ Move(result_register(), reg);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000448 codegen()->PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000449 codegen()->DoTest(true_label_, false_label_, fall_through_);
450}
451
452
453void FullCodeGenerator::EffectContext::PlugTOS() const {
454 __ Drop(1);
455}
456
457
458void FullCodeGenerator::AccumulatorValueContext::PlugTOS() const {
459 __ pop(result_register());
460}
461
462
463void FullCodeGenerator::StackValueContext::PlugTOS() const {
464}
465
466
467void FullCodeGenerator::TestContext::PlugTOS() const {
468 // For simplicity we always test the accumulator register.
469 __ pop(result_register());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000470 codegen()->PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000471 codegen()->DoTest(true_label_, false_label_, fall_through_);
472}
473
474
475void FullCodeGenerator::EffectContext::PrepareTest(
476 Label* materialize_true,
477 Label* materialize_false,
478 Label** if_true,
479 Label** if_false,
480 Label** fall_through) const {
481 // In an effect context, the true and the false case branch to the
482 // same label.
483 *if_true = *if_false = *fall_through = materialize_true;
484}
485
486
487void FullCodeGenerator::AccumulatorValueContext::PrepareTest(
488 Label* materialize_true,
489 Label* materialize_false,
490 Label** if_true,
491 Label** if_false,
492 Label** fall_through) const {
493 *if_true = *fall_through = materialize_true;
494 *if_false = materialize_false;
495}
496
497
498void FullCodeGenerator::StackValueContext::PrepareTest(
499 Label* materialize_true,
500 Label* materialize_false,
501 Label** if_true,
502 Label** if_false,
503 Label** fall_through) const {
504 *if_true = *fall_through = materialize_true;
505 *if_false = materialize_false;
506}
507
508
509void FullCodeGenerator::TestContext::PrepareTest(
510 Label* materialize_true,
511 Label* materialize_false,
512 Label** if_true,
513 Label** if_false,
514 Label** fall_through) const {
515 *if_true = true_label_;
516 *if_false = false_label_;
517 *fall_through = fall_through_;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000518}
519
520
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000521void FullCodeGenerator::VisitDeclarations(
522 ZoneList<Declaration*>* declarations) {
523 int length = declarations->length();
524 int globals = 0;
525 for (int i = 0; i < length; i++) {
526 Declaration* decl = declarations->at(i);
527 Variable* var = decl->proxy()->var();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000528 Slot* slot = var->AsSlot();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000529
530 // If it was not possible to allocate the variable at compile
531 // time, we need to "declare" it at runtime to make sure it
532 // actually exists in the local context.
533 if ((slot != NULL && slot->type() == Slot::LOOKUP) || !var->is_global()) {
534 VisitDeclaration(decl);
535 } else {
536 // Count global variables and functions for later processing
537 globals++;
538 }
539 }
540
541 // Compute array of global variable and function declarations.
542 // Do nothing in case of no declared global functions or variables.
543 if (globals > 0) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000544 Handle<FixedArray> array =
545 isolate()->factory()->NewFixedArray(2 * globals, TENURED);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000546 for (int j = 0, i = 0; i < length; i++) {
547 Declaration* decl = declarations->at(i);
548 Variable* var = decl->proxy()->var();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000549 Slot* slot = var->AsSlot();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000550
551 if ((slot == NULL || slot->type() != Slot::LOOKUP) && var->is_global()) {
552 array->set(j++, *(var->name()));
553 if (decl->fun() == NULL) {
554 if (var->mode() == Variable::CONST) {
555 // In case this is const property use the hole.
556 array->set_the_hole(j++);
557 } else {
558 array->set_undefined(j++);
559 }
560 } else {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000561 Handle<SharedFunctionInfo> function =
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000562 Compiler::BuildFunctionInfo(decl->fun(), script());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000563 // Check for stack-overflow exception.
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000564 if (function.is_null()) {
565 SetStackOverflow();
566 return;
567 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000568 array->set(j++, *function);
569 }
570 }
571 }
572 // Invoke the platform-dependent code generator to do the actual
573 // declaration the global variables and functions.
574 DeclareGlobals(array);
575 }
576}
577
578
579void FullCodeGenerator::SetFunctionPosition(FunctionLiteral* fun) {
580 if (FLAG_debug_info) {
581 CodeGenerator::RecordPositions(masm_, fun->start_position());
582 }
583}
584
585
586void FullCodeGenerator::SetReturnPosition(FunctionLiteral* fun) {
587 if (FLAG_debug_info) {
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000588 CodeGenerator::RecordPositions(masm_, fun->end_position() - 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000589 }
590}
591
592
593void FullCodeGenerator::SetStatementPosition(Statement* stmt) {
594 if (FLAG_debug_info) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000595#ifdef ENABLE_DEBUGGER_SUPPORT
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000596 if (!isolate()->debugger()->IsDebuggerActive()) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000597 CodeGenerator::RecordPositions(masm_, stmt->statement_pos());
598 } else {
599 // Check if the statement will be breakable without adding a debug break
600 // slot.
601 BreakableStatementChecker checker;
602 checker.Check(stmt);
603 // Record the statement position right here if the statement is not
604 // breakable. For breakable statements the actual recording of the
605 // position will be postponed to the breakable code (typically an IC).
606 bool position_recorded = CodeGenerator::RecordPositions(
607 masm_, stmt->statement_pos(), !checker.is_breakable());
608 // If the position recording did record a new position generate a debug
609 // break slot to make the statement breakable.
610 if (position_recorded) {
611 Debug::GenerateSlot(masm_);
612 }
613 }
614#else
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000615 CodeGenerator::RecordPositions(masm_, stmt->statement_pos());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000616#endif
617 }
618}
619
620
621void FullCodeGenerator::SetExpressionPosition(Expression* expr, int pos) {
622 if (FLAG_debug_info) {
623#ifdef ENABLE_DEBUGGER_SUPPORT
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000624 if (!isolate()->debugger()->IsDebuggerActive()) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000625 CodeGenerator::RecordPositions(masm_, pos);
626 } else {
627 // Check if the expression will be breakable without adding a debug break
628 // slot.
629 BreakableStatementChecker checker;
630 checker.Check(expr);
631 // Record a statement position right here if the expression is not
632 // breakable. For breakable expressions the actual recording of the
633 // position will be postponed to the breakable code (typically an IC).
634 // NOTE this will record a statement position for something which might
635 // not be a statement. As stepping in the debugger will only stop at
636 // statement positions this is used for e.g. the condition expression of
637 // a do while loop.
638 bool position_recorded = CodeGenerator::RecordPositions(
639 masm_, pos, !checker.is_breakable());
640 // If the position recording did record a new position generate a debug
641 // break slot to make the statement breakable.
642 if (position_recorded) {
643 Debug::GenerateSlot(masm_);
644 }
645 }
646#else
647 CodeGenerator::RecordPositions(masm_, pos);
648#endif
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000649 }
650}
651
652
653void FullCodeGenerator::SetStatementPosition(int pos) {
654 if (FLAG_debug_info) {
655 CodeGenerator::RecordPositions(masm_, pos);
656 }
657}
658
659
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000660void FullCodeGenerator::SetSourcePosition(int pos) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000661 if (FLAG_debug_info && pos != RelocInfo::kNoPosition) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000662 masm_->positions_recorder()->RecordPosition(pos);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000663 }
664}
665
666
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000667// Lookup table for code generators for special runtime calls which are
668// generated inline.
669#define INLINE_FUNCTION_GENERATOR_ADDRESS(Name, argc, ressize) \
670 &FullCodeGenerator::Emit##Name,
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000671
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000672const FullCodeGenerator::InlineFunctionGenerator
673 FullCodeGenerator::kInlineFunctionGenerators[] = {
674 INLINE_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS)
675 INLINE_RUNTIME_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS)
676 };
677#undef INLINE_FUNCTION_GENERATOR_ADDRESS
678
679
680FullCodeGenerator::InlineFunctionGenerator
681 FullCodeGenerator::FindInlineFunctionGenerator(Runtime::FunctionId id) {
whesse@chromium.org023421e2010-12-21 12:19:12 +0000682 int lookup_index =
683 static_cast<int>(id) - static_cast<int>(Runtime::kFirstInlineFunction);
684 ASSERT(lookup_index >= 0);
685 ASSERT(static_cast<size_t>(lookup_index) <
686 ARRAY_SIZE(kInlineFunctionGenerators));
687 return kInlineFunctionGenerators[lookup_index];
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000688}
689
690
691void FullCodeGenerator::EmitInlineRuntimeCall(CallRuntime* node) {
692 ZoneList<Expression*>* args = node->arguments();
693 Handle<String> name = node->name();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000694 const Runtime::Function* function = node->function();
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000695 ASSERT(function != NULL);
696 ASSERT(function->intrinsic_type == Runtime::INLINE);
697 InlineFunctionGenerator generator =
698 FindInlineFunctionGenerator(function->function_id);
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000699 ((*this).*(generator))(args);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000700}
701
702
703void FullCodeGenerator::VisitBinaryOperation(BinaryOperation* expr) {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000704 switch (expr->op()) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000705 case Token::COMMA:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000706 return VisitComma(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000707 case Token::OR:
708 case Token::AND:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000709 return VisitLogicalExpression(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000710 default:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000711 return VisitArithmeticExpression(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000712 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000713}
714
715
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000716void FullCodeGenerator::VisitComma(BinaryOperation* expr) {
717 Comment cmnt(masm_, "[ Comma");
718 VisitForEffect(expr->left());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000719 if (context()->IsTest()) ForwardBailoutToChild(expr);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000720 VisitInCurrentContext(expr->right());
721}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000722
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000723
724void FullCodeGenerator::VisitLogicalExpression(BinaryOperation* expr) {
725 bool is_logical_and = expr->op() == Token::AND;
726 Comment cmnt(masm_, is_logical_and ? "[ Logical AND" : "[ Logical OR");
727 Expression* left = expr->left();
728 Expression* right = expr->right();
729 int right_id = expr->RightId();
730 Label done;
731
732 if (context()->IsTest()) {
733 Label eval_right;
734 const TestContext* test = TestContext::cast(context());
735 if (is_logical_and) {
736 VisitForControl(left, &eval_right, test->false_label(), &eval_right);
737 } else {
738 VisitForControl(left, test->true_label(), &eval_right, &eval_right);
739 }
740 PrepareForBailoutForId(right_id, NO_REGISTERS);
741 __ bind(&eval_right);
742 ForwardBailoutToChild(expr);
743
744 } else if (context()->IsAccumulatorValue()) {
745 VisitForAccumulatorValue(left);
746 // We want the value in the accumulator for the test, and on the stack in
747 // case we need it.
748 __ push(result_register());
749 Label discard, restore;
750 PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
751 if (is_logical_and) {
752 DoTest(&discard, &restore, &restore);
753 } else {
754 DoTest(&restore, &discard, &restore);
755 }
756 __ bind(&restore);
757 __ pop(result_register());
758 __ jmp(&done);
759 __ bind(&discard);
760 __ Drop(1);
761 PrepareForBailoutForId(right_id, NO_REGISTERS);
762
763 } else if (context()->IsStackValue()) {
764 VisitForAccumulatorValue(left);
765 // We want the value in the accumulator for the test, and on the stack in
766 // case we need it.
767 __ push(result_register());
768 Label discard;
769 PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
770 if (is_logical_and) {
771 DoTest(&discard, &done, &discard);
772 } else {
773 DoTest(&done, &discard, &discard);
774 }
775 __ bind(&discard);
776 __ Drop(1);
777 PrepareForBailoutForId(right_id, NO_REGISTERS);
778
779 } else {
780 ASSERT(context()->IsEffect());
781 Label eval_right;
782 if (is_logical_and) {
783 VisitForControl(left, &eval_right, &done, &eval_right);
784 } else {
785 VisitForControl(left, &done, &eval_right, &eval_right);
786 }
787 PrepareForBailoutForId(right_id, NO_REGISTERS);
788 __ bind(&eval_right);
789 }
790
791 VisitInCurrentContext(right);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000792 __ bind(&done);
793}
794
795
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000796void FullCodeGenerator::VisitArithmeticExpression(BinaryOperation* expr) {
797 Token::Value op = expr->op();
798 Comment cmnt(masm_, "[ ArithmeticExpression");
799 Expression* left = expr->left();
800 Expression* right = expr->right();
801 OverwriteMode mode =
802 left->ResultOverwriteAllowed()
803 ? OVERWRITE_LEFT
804 : (right->ResultOverwriteAllowed() ? OVERWRITE_RIGHT : NO_OVERWRITE);
805
806 VisitForStackValue(left);
807 VisitForAccumulatorValue(right);
808
809 SetSourcePosition(expr->position());
810 if (ShouldInlineSmiCase(op)) {
811 EmitInlineSmiBinaryOp(expr, op, mode, left, right);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000812 } else {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000813 EmitBinaryOp(expr, op, mode);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000814 }
815}
816
817
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000818void FullCodeGenerator::ForwardBailoutToChild(Expression* expr) {
819 if (!info_->HasDeoptimizationSupport()) return;
820 ASSERT(context()->IsTest());
821 ASSERT(expr == forward_bailout_stack_->expr());
822 forward_bailout_pending_ = forward_bailout_stack_;
823}
824
825
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000826void FullCodeGenerator::VisitInCurrentContext(Expression* expr) {
827 if (context()->IsTest()) {
828 ForwardBailoutStack stack(expr, forward_bailout_pending_);
829 ForwardBailoutStack* saved = forward_bailout_stack_;
830 forward_bailout_pending_ = NULL;
831 forward_bailout_stack_ = &stack;
832 Visit(expr);
833 forward_bailout_stack_ = saved;
834 } else {
835 ASSERT(forward_bailout_pending_ == NULL);
836 Visit(expr);
837 State state = context()->IsAccumulatorValue() ? TOS_REG : NO_REGISTERS;
838 PrepareForBailout(expr, state);
839 // Forwarding bailouts to children is a one shot operation. It should have
840 // been processed at this point.
841 ASSERT(forward_bailout_pending_ == NULL);
842 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000843}
844
845
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000846void FullCodeGenerator::VisitBlock(Block* stmt) {
847 Comment cmnt(masm_, "[ Block");
848 Breakable nested_statement(this, stmt);
849 SetStatementPosition(stmt);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000850
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +0000851 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000852 VisitStatements(stmt->statements());
853 __ bind(nested_statement.break_target());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000854 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000855}
856
857
858void FullCodeGenerator::VisitExpressionStatement(ExpressionStatement* stmt) {
859 Comment cmnt(masm_, "[ ExpressionStatement");
860 SetStatementPosition(stmt);
861 VisitForEffect(stmt->expression());
862}
863
864
865void FullCodeGenerator::VisitEmptyStatement(EmptyStatement* stmt) {
866 Comment cmnt(masm_, "[ EmptyStatement");
867 SetStatementPosition(stmt);
868}
869
870
871void FullCodeGenerator::VisitIfStatement(IfStatement* stmt) {
872 Comment cmnt(masm_, "[ IfStatement");
873 SetStatementPosition(stmt);
874 Label then_part, else_part, done;
875
ricow@chromium.org65fae842010-08-25 15:26:24 +0000876 if (stmt->HasElseStatement()) {
877 VisitForControl(stmt->condition(), &then_part, &else_part, &then_part);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000878 PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000879 __ bind(&then_part);
880 Visit(stmt->then_statement());
881 __ jmp(&done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000882
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000883 PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000884 __ bind(&else_part);
885 Visit(stmt->else_statement());
886 } else {
887 VisitForControl(stmt->condition(), &then_part, &done, &then_part);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000888 PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000889 __ bind(&then_part);
890 Visit(stmt->then_statement());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000891
892 PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000893 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000894 __ bind(&done);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000895 PrepareForBailoutForId(stmt->IfId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000896}
897
898
899void FullCodeGenerator::VisitContinueStatement(ContinueStatement* stmt) {
900 Comment cmnt(masm_, "[ ContinueStatement");
901 SetStatementPosition(stmt);
902 NestedStatement* current = nesting_stack_;
903 int stack_depth = 0;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000904 // When continuing, we clobber the unpredictable value in the accumulator
905 // with one that's safe for GC. If we hit an exit from the try block of
906 // try...finally on our way out, we will unconditionally preserve the
907 // accumulator on the stack.
908 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000909 while (!current->IsContinueTarget(stmt->target())) {
910 stack_depth = current->Exit(stack_depth);
911 current = current->outer();
912 }
913 __ Drop(stack_depth);
914
915 Iteration* loop = current->AsIteration();
916 __ jmp(loop->continue_target());
917}
918
919
920void FullCodeGenerator::VisitBreakStatement(BreakStatement* stmt) {
921 Comment cmnt(masm_, "[ BreakStatement");
922 SetStatementPosition(stmt);
923 NestedStatement* current = nesting_stack_;
924 int stack_depth = 0;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000925 // When breaking, we clobber the unpredictable value in the accumulator
926 // with one that's safe for GC. If we hit an exit from the try block of
927 // try...finally on our way out, we will unconditionally preserve the
928 // accumulator on the stack.
929 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000930 while (!current->IsBreakTarget(stmt->target())) {
931 stack_depth = current->Exit(stack_depth);
932 current = current->outer();
933 }
934 __ Drop(stack_depth);
935
936 Breakable* target = current->AsBreakable();
937 __ jmp(target->break_target());
938}
939
940
941void FullCodeGenerator::VisitReturnStatement(ReturnStatement* stmt) {
942 Comment cmnt(masm_, "[ ReturnStatement");
943 SetStatementPosition(stmt);
944 Expression* expr = stmt->expression();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000945 VisitForAccumulatorValue(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000946
947 // Exit all nested statements.
948 NestedStatement* current = nesting_stack_;
949 int stack_depth = 0;
950 while (current != NULL) {
951 stack_depth = current->Exit(stack_depth);
952 current = current->outer();
953 }
954 __ Drop(stack_depth);
955
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000956 EmitReturnSequence();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000957}
958
959
960void FullCodeGenerator::VisitWithEnterStatement(WithEnterStatement* stmt) {
961 Comment cmnt(masm_, "[ WithEnterStatement");
962 SetStatementPosition(stmt);
963
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000964 VisitForStackValue(stmt->expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000965 if (stmt->is_catch_block()) {
966 __ CallRuntime(Runtime::kPushCatchContext, 1);
967 } else {
968 __ CallRuntime(Runtime::kPushContext, 1);
969 }
970 // Both runtime calls return the new context in both the context and the
971 // result registers.
972
973 // Update local stack frame context field.
974 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
975}
976
977
978void FullCodeGenerator::VisitWithExitStatement(WithExitStatement* stmt) {
979 Comment cmnt(masm_, "[ WithExitStatement");
980 SetStatementPosition(stmt);
981
982 // Pop context.
983 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
984 // Update local stack frame context field.
985 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
986}
987
988
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000989void FullCodeGenerator::VisitDoWhileStatement(DoWhileStatement* stmt) {
990 Comment cmnt(masm_, "[ DoWhileStatement");
991 SetStatementPosition(stmt);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000992 Label body, stack_check;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000993
994 Iteration loop_statement(this, stmt);
995 increment_loop_depth();
996
997 __ bind(&body);
998 Visit(stmt->body());
999
ricow@chromium.org65fae842010-08-25 15:26:24 +00001000 // Record the position of the do while condition and make sure it is
1001 // possible to break on the condition.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001002 __ bind(loop_statement.continue_target());
1003 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001004 SetExpressionPosition(stmt->cond(), stmt->condition_position());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001005 VisitForControl(stmt->cond(),
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001006 &stack_check,
ricow@chromium.org65fae842010-08-25 15:26:24 +00001007 loop_statement.break_target(),
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001008 &stack_check);
1009
1010 // Check stack before looping.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001011 PrepareForBailoutForId(stmt->BackEdgeId(), NO_REGISTERS);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001012 __ bind(&stack_check);
1013 EmitStackCheck(stmt);
1014 __ jmp(&body);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001015
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001016 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001017 __ bind(loop_statement.break_target());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001018 decrement_loop_depth();
1019}
1020
1021
1022void FullCodeGenerator::VisitWhileStatement(WhileStatement* stmt) {
1023 Comment cmnt(masm_, "[ WhileStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001024 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001025
1026 Iteration loop_statement(this, stmt);
1027 increment_loop_depth();
1028
1029 // Emit the test at the bottom of the loop.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001030 __ jmp(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001031
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001032 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001033 __ bind(&body);
1034 Visit(stmt->body());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001035
1036 // Emit the statement position here as this is where the while
1037 // statement code starts.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001038 __ bind(loop_statement.continue_target());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001039 SetStatementPosition(stmt);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001040
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001041 // Check stack before looping.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001042 EmitStackCheck(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001043
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001044 __ bind(&test);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001045 VisitForControl(stmt->cond(),
1046 &body,
1047 loop_statement.break_target(),
1048 loop_statement.break_target());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001049
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001050 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001051 __ bind(loop_statement.break_target());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001052 decrement_loop_depth();
1053}
1054
1055
1056void FullCodeGenerator::VisitForStatement(ForStatement* stmt) {
1057 Comment cmnt(masm_, "[ ForStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001058 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001059
1060 Iteration loop_statement(this, stmt);
1061 if (stmt->init() != NULL) {
1062 Visit(stmt->init());
1063 }
1064
1065 increment_loop_depth();
1066 // Emit the test at the bottom of the loop (even if empty).
1067 __ jmp(&test);
1068
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001069 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001070 __ bind(&body);
1071 Visit(stmt->body());
1072
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001073 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001074 __ bind(loop_statement.continue_target());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001075 SetStatementPosition(stmt);
1076 if (stmt->next() != NULL) {
1077 Visit(stmt->next());
1078 }
1079
ricow@chromium.org65fae842010-08-25 15:26:24 +00001080 // Emit the statement position here as this is where the for
1081 // statement code starts.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001082 SetStatementPosition(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001083
1084 // Check stack before looping.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001085 EmitStackCheck(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001086
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001087 __ bind(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001088 if (stmt->cond() != NULL) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00001089 VisitForControl(stmt->cond(),
1090 &body,
1091 loop_statement.break_target(),
1092 loop_statement.break_target());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001093 } else {
1094 __ jmp(&body);
1095 }
1096
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001097 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001098 __ bind(loop_statement.break_target());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001099 decrement_loop_depth();
1100}
1101
1102
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001103void FullCodeGenerator::VisitTryCatchStatement(TryCatchStatement* stmt) {
1104 Comment cmnt(masm_, "[ TryCatchStatement");
1105 SetStatementPosition(stmt);
1106 // The try block adds a handler to the exception handler chain
1107 // before entering, and removes it again when exiting normally.
1108 // If an exception is thrown during execution of the try block,
1109 // control is passed to the handler, which also consumes the handler.
1110 // At this point, the exception is in a register, and store it in
1111 // the temporary local variable (prints as ".catch-var") before
1112 // executing the catch block. The catch block has been rewritten
1113 // to introduce a new scope to bind the catch variable and to remove
1114 // that scope again afterwards.
1115
1116 Label try_handler_setup, catch_entry, done;
1117 __ Call(&try_handler_setup);
1118 // Try handler code, exception in result register.
1119
1120 // Store exception in local .catch variable before executing catch block.
1121 {
1122 // The catch variable is *always* a variable proxy for a local variable.
1123 Variable* catch_var = stmt->catch_var()->AsVariableProxy()->AsVariable();
1124 ASSERT_NOT_NULL(catch_var);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001125 Slot* variable_slot = catch_var->AsSlot();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001126 ASSERT_NOT_NULL(variable_slot);
1127 ASSERT_EQ(Slot::LOCAL, variable_slot->type());
1128 StoreToFrameField(SlotOffset(variable_slot), result_register());
1129 }
1130
1131 Visit(stmt->catch_block());
1132 __ jmp(&done);
1133
1134 // Try block code. Sets up the exception handler chain.
1135 __ bind(&try_handler_setup);
1136 {
1137 TryCatch try_block(this, &catch_entry);
1138 __ PushTryHandler(IN_JAVASCRIPT, TRY_CATCH_HANDLER);
1139 Visit(stmt->try_block());
1140 __ PopTryHandler();
1141 }
1142 __ bind(&done);
1143}
1144
1145
1146void FullCodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* stmt) {
1147 Comment cmnt(masm_, "[ TryFinallyStatement");
1148 SetStatementPosition(stmt);
1149 // Try finally is compiled by setting up a try-handler on the stack while
1150 // executing the try body, and removing it again afterwards.
1151 //
1152 // The try-finally construct can enter the finally block in three ways:
1153 // 1. By exiting the try-block normally. This removes the try-handler and
1154 // calls the finally block code before continuing.
1155 // 2. By exiting the try-block with a function-local control flow transfer
1156 // (break/continue/return). The site of the, e.g., break removes the
1157 // try handler and calls the finally block code before continuing
1158 // its outward control transfer.
1159 // 3. by exiting the try-block with a thrown exception.
1160 // This can happen in nested function calls. It traverses the try-handler
1161 // chain and consumes the try-handler entry before jumping to the
1162 // handler code. The handler code then calls the finally-block before
1163 // rethrowing the exception.
1164 //
1165 // The finally block must assume a return address on top of the stack
1166 // (or in the link register on ARM chips) and a value (return value or
1167 // exception) in the result register (rax/eax/r0), both of which must
1168 // be preserved. The return address isn't GC-safe, so it should be
1169 // cooked before GC.
1170 Label finally_entry;
1171 Label try_handler_setup;
1172
1173 // Setup the try-handler chain. Use a call to
1174 // Jump to try-handler setup and try-block code. Use call to put try-handler
1175 // address on stack.
1176 __ Call(&try_handler_setup);
1177 // Try handler code. Return address of call is pushed on handler stack.
1178 {
1179 // This code is only executed during stack-handler traversal when an
1180 // exception is thrown. The execption is in the result register, which
1181 // is retained by the finally block.
1182 // Call the finally block and then rethrow the exception.
1183 __ Call(&finally_entry);
1184 __ push(result_register());
1185 __ CallRuntime(Runtime::kReThrow, 1);
1186 }
1187
1188 __ bind(&finally_entry);
1189 {
1190 // Finally block implementation.
1191 Finally finally_block(this);
1192 EnterFinallyBlock();
1193 Visit(stmt->finally_block());
1194 ExitFinallyBlock(); // Return to the calling code.
1195 }
1196
1197 __ bind(&try_handler_setup);
1198 {
1199 // Setup try handler (stack pointer registers).
1200 TryFinally try_block(this, &finally_entry);
1201 __ PushTryHandler(IN_JAVASCRIPT, TRY_FINALLY_HANDLER);
1202 Visit(stmt->try_block());
1203 __ PopTryHandler();
1204 }
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001205 // Execute the finally block on the way out. Clobber the unpredictable
1206 // value in the accumulator with one that's safe for GC. The finally
1207 // block will unconditionally preserve the accumulator on the stack.
1208 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001209 __ Call(&finally_entry);
1210}
1211
1212
1213void FullCodeGenerator::VisitDebuggerStatement(DebuggerStatement* stmt) {
1214#ifdef ENABLE_DEBUGGER_SUPPORT
1215 Comment cmnt(masm_, "[ DebuggerStatement");
1216 SetStatementPosition(stmt);
1217
ager@chromium.org5c838252010-02-19 08:53:10 +00001218 __ DebugBreak();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001219 // Ignore the return value.
1220#endif
1221}
1222
1223
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001224void FullCodeGenerator::VisitConditional(Conditional* expr) {
1225 Comment cmnt(masm_, "[ Conditional");
1226 Label true_case, false_case, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001227 VisitForControl(expr->condition(), &true_case, &false_case, &true_case);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001228
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001229 PrepareForBailoutForId(expr->ThenId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001230 __ bind(&true_case);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001231 SetExpressionPosition(expr->then_expression(),
1232 expr->then_expression_position());
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001233 if (context()->IsTest()) {
1234 const TestContext* for_test = TestContext::cast(context());
1235 VisitForControl(expr->then_expression(),
1236 for_test->true_label(),
1237 for_test->false_label(),
1238 NULL);
1239 } else {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001240 VisitInCurrentContext(expr->then_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001241 __ jmp(&done);
1242 }
1243
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001244 PrepareForBailoutForId(expr->ElseId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001245 __ bind(&false_case);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001246 if (context()->IsTest()) ForwardBailoutToChild(expr);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001247 SetExpressionPosition(expr->else_expression(),
1248 expr->else_expression_position());
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001249 VisitInCurrentContext(expr->else_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001250 // If control flow falls through Visit, merge it with true case here.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001251 if (!context()->IsTest()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001252 __ bind(&done);
1253 }
1254}
1255
1256
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001257void FullCodeGenerator::VisitLiteral(Literal* expr) {
1258 Comment cmnt(masm_, "[ Literal");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001259 context()->Plug(expr->handle());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001260}
1261
1262
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001263void FullCodeGenerator::VisitFunctionLiteral(FunctionLiteral* expr) {
1264 Comment cmnt(masm_, "[ FunctionLiteral");
1265
1266 // Build the function boilerplate and instantiate it.
1267 Handle<SharedFunctionInfo> function_info =
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001268 Compiler::BuildFunctionInfo(expr, script());
1269 if (function_info.is_null()) {
1270 SetStackOverflow();
1271 return;
1272 }
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001273 EmitNewClosure(function_info, expr->pretenure());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001274}
1275
1276
1277void FullCodeGenerator::VisitSharedFunctionInfoLiteral(
1278 SharedFunctionInfoLiteral* expr) {
1279 Comment cmnt(masm_, "[ SharedFunctionInfoLiteral");
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001280 EmitNewClosure(expr->shared_function_info(), false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001281}
1282
1283
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001284void FullCodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* expr) {
1285 // Call runtime routine to allocate the catch extension object and
1286 // assign the exception value to the catch variable.
1287 Comment cmnt(masm_, "[ CatchExtensionObject");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001288 VisitForStackValue(expr->key());
1289 VisitForStackValue(expr->value());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001290 // Create catch extension object.
1291 __ CallRuntime(Runtime::kCreateCatchExtensionObject, 2);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001292 context()->Plug(result_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001293}
1294
1295
1296void FullCodeGenerator::VisitThrow(Throw* expr) {
1297 Comment cmnt(masm_, "[ Throw");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001298 VisitForStackValue(expr->exception());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001299 __ CallRuntime(Runtime::kThrow, 1);
1300 // Never returns here.
1301}
1302
1303
1304int FullCodeGenerator::TryFinally::Exit(int stack_depth) {
1305 // The macros used here must preserve the result register.
1306 __ Drop(stack_depth);
1307 __ PopTryHandler();
1308 __ Call(finally_entry_);
1309 return 0;
1310}
1311
1312
1313int FullCodeGenerator::TryCatch::Exit(int stack_depth) {
1314 // The macros used here must preserve the result register.
1315 __ Drop(stack_depth);
1316 __ PopTryHandler();
1317 return 0;
1318}
1319
ricow@chromium.org65fae842010-08-25 15:26:24 +00001320
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001321#undef __
1322
1323
1324} } // namespace v8::internal