blob: a43f674c33641f1ae9682423494f2c76b9f744e1 [file] [log] [blame]
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00001// Copyright 2012 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"
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +000038#include "scopeinfo.h"
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +000039#include "snapshot.h"
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000040#include "stub-cache.h"
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000041
42namespace v8 {
43namespace internal {
44
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +000045void BreakableStatementChecker::Check(Statement* stmt) {
46 Visit(stmt);
47}
48
49
50void BreakableStatementChecker::Check(Expression* expr) {
51 Visit(expr);
52}
53
54
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +000055void BreakableStatementChecker::VisitVariableDeclaration(
56 VariableDeclaration* decl) {
57}
58
ulan@chromium.org812308e2012-02-29 15:58:45 +000059void BreakableStatementChecker::VisitFunctionDeclaration(
60 FunctionDeclaration* decl) {
61}
62
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +000063void BreakableStatementChecker::VisitModuleDeclaration(
64 ModuleDeclaration* decl) {
65}
66
ulan@chromium.org812308e2012-02-29 15:58:45 +000067void BreakableStatementChecker::VisitImportDeclaration(
68 ImportDeclaration* decl) {
69}
70
71void BreakableStatementChecker::VisitExportDeclaration(
72 ExportDeclaration* decl) {
73}
74
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +000075
76void BreakableStatementChecker::VisitModuleLiteral(ModuleLiteral* module) {
77}
78
79void BreakableStatementChecker::VisitModuleVariable(ModuleVariable* module) {
80}
81
82void BreakableStatementChecker::VisitModulePath(ModulePath* module) {
83}
84
85void BreakableStatementChecker::VisitModuleUrl(ModuleUrl* module) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +000086}
87
88
ulan@chromium.org8e8d8822012-11-23 14:36:46 +000089void BreakableStatementChecker::VisitModuleStatement(ModuleStatement* stmt) {
90}
91
92
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +000093void BreakableStatementChecker::VisitBlock(Block* stmt) {
94}
95
96
97void BreakableStatementChecker::VisitExpressionStatement(
98 ExpressionStatement* stmt) {
99 // Check if expression is breakable.
100 Visit(stmt->expression());
101}
102
103
104void BreakableStatementChecker::VisitEmptyStatement(EmptyStatement* stmt) {
105}
106
107
108void BreakableStatementChecker::VisitIfStatement(IfStatement* stmt) {
109 // If the condition is breakable the if statement is breakable.
110 Visit(stmt->condition());
111}
112
113
114void BreakableStatementChecker::VisitContinueStatement(
115 ContinueStatement* stmt) {
116}
117
118
119void BreakableStatementChecker::VisitBreakStatement(BreakStatement* stmt) {
120}
121
122
123void BreakableStatementChecker::VisitReturnStatement(ReturnStatement* stmt) {
124 // Return is breakable if the expression is.
125 Visit(stmt->expression());
126}
127
128
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000129void BreakableStatementChecker::VisitWithStatement(WithStatement* stmt) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000130 Visit(stmt->expression());
131}
132
133
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000134void BreakableStatementChecker::VisitSwitchStatement(SwitchStatement* stmt) {
135 // Switch statements breakable if the tag expression is.
136 Visit(stmt->tag());
137}
138
139
140void BreakableStatementChecker::VisitDoWhileStatement(DoWhileStatement* stmt) {
141 // Mark do while as breakable to avoid adding a break slot in front of it.
142 is_breakable_ = true;
143}
144
145
146void BreakableStatementChecker::VisitWhileStatement(WhileStatement* stmt) {
147 // Mark while statements breakable if the condition expression is.
148 Visit(stmt->cond());
149}
150
151
152void BreakableStatementChecker::VisitForStatement(ForStatement* stmt) {
153 // Mark for statements breakable if the condition expression is.
154 if (stmt->cond() != NULL) {
155 Visit(stmt->cond());
156 }
157}
158
159
160void BreakableStatementChecker::VisitForInStatement(ForInStatement* stmt) {
161 // Mark for in statements breakable if the enumerable expression is.
162 Visit(stmt->enumerable());
163}
164
165
166void BreakableStatementChecker::VisitTryCatchStatement(
167 TryCatchStatement* stmt) {
168 // Mark try catch as breakable to avoid adding a break slot in front of it.
169 is_breakable_ = true;
170}
171
172
173void BreakableStatementChecker::VisitTryFinallyStatement(
174 TryFinallyStatement* stmt) {
175 // Mark try finally as breakable to avoid adding a break slot in front of it.
176 is_breakable_ = true;
177}
178
179
180void BreakableStatementChecker::VisitDebuggerStatement(
181 DebuggerStatement* stmt) {
182 // The debugger statement is breakable.
183 is_breakable_ = true;
184}
185
186
187void BreakableStatementChecker::VisitFunctionLiteral(FunctionLiteral* expr) {
188}
189
190
191void BreakableStatementChecker::VisitSharedFunctionInfoLiteral(
192 SharedFunctionInfoLiteral* expr) {
193}
194
195
196void BreakableStatementChecker::VisitConditional(Conditional* expr) {
197}
198
199
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000200void BreakableStatementChecker::VisitVariableProxy(VariableProxy* expr) {
201}
202
203
204void BreakableStatementChecker::VisitLiteral(Literal* expr) {
205}
206
207
208void BreakableStatementChecker::VisitRegExpLiteral(RegExpLiteral* expr) {
209}
210
211
212void BreakableStatementChecker::VisitObjectLiteral(ObjectLiteral* expr) {
213}
214
215
216void BreakableStatementChecker::VisitArrayLiteral(ArrayLiteral* expr) {
217}
218
219
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000220void BreakableStatementChecker::VisitAssignment(Assignment* expr) {
221 // If assigning to a property (including a global property) the assignment is
222 // breakable.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000223 VariableProxy* proxy = expr->target()->AsVariableProxy();
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000224 Property* prop = expr->target()->AsProperty();
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000225 if (prop != NULL || (proxy != NULL && proxy->var()->IsUnallocated())) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000226 is_breakable_ = true;
227 return;
228 }
229
230 // Otherwise the assignment is breakable if the assigned value is.
231 Visit(expr->value());
232}
233
234
235void BreakableStatementChecker::VisitThrow(Throw* expr) {
236 // Throw is breakable if the expression is.
237 Visit(expr->exception());
238}
239
240
241void BreakableStatementChecker::VisitProperty(Property* expr) {
242 // Property load is breakable.
243 is_breakable_ = true;
244}
245
246
247void BreakableStatementChecker::VisitCall(Call* expr) {
248 // Function calls both through IC and call stub are breakable.
249 is_breakable_ = true;
250}
251
252
253void BreakableStatementChecker::VisitCallNew(CallNew* expr) {
254 // Function calls through new are breakable.
255 is_breakable_ = true;
256}
257
258
259void BreakableStatementChecker::VisitCallRuntime(CallRuntime* expr) {
260}
261
262
263void BreakableStatementChecker::VisitUnaryOperation(UnaryOperation* expr) {
264 Visit(expr->expression());
265}
266
267
268void BreakableStatementChecker::VisitCountOperation(CountOperation* expr) {
269 Visit(expr->expression());
270}
271
272
273void BreakableStatementChecker::VisitBinaryOperation(BinaryOperation* expr) {
274 Visit(expr->left());
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +0000275 if (expr->op() != Token::AND &&
276 expr->op() != Token::OR) {
277 Visit(expr->right());
278 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000279}
280
281
282void BreakableStatementChecker::VisitCompareOperation(CompareOperation* expr) {
283 Visit(expr->left());
284 Visit(expr->right());
285}
286
287
288void BreakableStatementChecker::VisitThisFunction(ThisFunction* expr) {
289}
290
291
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000292#define __ ACCESS_MASM(masm())
293
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000294bool FullCodeGenerator::MakeCode(CompilationInfo* info) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000295 Isolate* isolate = info->isolate();
ager@chromium.org5c838252010-02-19 08:53:10 +0000296 Handle<Script> script = info->script();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000297 if (!script->IsUndefined() && !script->source()->IsUndefined()) {
298 int len = String::cast(script->source())->length();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000299 isolate->counters()->total_full_codegen_source_size()->Increment(len);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000300 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000301 if (FLAG_trace_codegen) {
302 PrintF("Full Compiler - ");
303 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000304 CodeGenerator::MakeCodePrologue(info);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000305 const int kInitialBufferSize = 4 * KB;
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000306 MacroAssembler masm(info->isolate(), NULL, kInitialBufferSize);
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000307#ifdef ENABLE_GDB_JIT_INTERFACE
308 masm.positions_recorder()->StartGDBJITLineInfoRecording();
309#endif
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000310 LOG_CODE_EVENT(isolate,
311 CodeStartLinePosInfoRecordEvent(masm.positions_recorder()));
ager@chromium.org5c838252010-02-19 08:53:10 +0000312
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000313 FullCodeGenerator cgen(&masm, info);
yangguo@chromium.org56454712012-02-16 15:33:53 +0000314 cgen.Generate();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000315 if (cgen.HasStackOverflow()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000316 ASSERT(!isolate->has_pending_exception());
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000317 return false;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000318 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000319 unsigned table_offset = cgen.EmitStackCheckTable();
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000320
lrn@chromium.org34e60782011-09-15 07:25:40 +0000321 Code::Flags flags = Code::ComputeFlags(Code::FUNCTION);
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000322 Handle<Code> code = CodeGenerator::MakeCodeEpilogue(&masm, flags, info);
rossberg@chromium.org2c067b12012-03-19 11:01:52 +0000323 code->set_optimizable(info->IsOptimizable() &&
ulan@chromium.orgd6899c32012-05-18 14:12:25 +0000324 !info->function()->flags()->Contains(kDontOptimize) &&
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000325 info->function()->scope()->AllowsLazyCompilation());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000326 cgen.PopulateDeoptimizationData(code);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000327 cgen.PopulateTypeFeedbackInfo(code);
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000328 cgen.PopulateTypeFeedbackCells(code);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000329 code->set_has_deoptimization_support(info->HasDeoptimizationSupport());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +0000330 code->set_handler_table(*cgen.handler_table());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000331#ifdef ENABLE_DEBUGGER_SUPPORT
lrn@chromium.org34e60782011-09-15 07:25:40 +0000332 code->set_has_debug_break_slots(
333 info->isolate()->debugger()->IsDebuggerActive());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000334 code->set_compiled_optimizable(info->IsOptimizable());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000335#endif // ENABLE_DEBUGGER_SUPPORT
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000336 code->set_allow_osr_at_loop_nesting_level(0);
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000337 code->set_profiler_ticks(0);
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000338 code->set_stack_check_table_offset(table_offset);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000339 CodeGenerator::PrintCode(code, info);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000340 info->SetCode(code); // May be an empty handle.
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000341#ifdef ENABLE_GDB_JIT_INTERFACE
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000342 if (FLAG_gdbjit && !code.is_null()) {
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000343 GDBJITLineInfo* lineinfo =
344 masm.positions_recorder()->DetachGDBJITLineInfo();
345
346 GDBJIT(RegisterDetailedLineInfo(*code, lineinfo));
347 }
348#endif
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000349 if (!code.is_null()) {
350 void* line_info =
351 masm.positions_recorder()->DetachJITHandlerData();
352 LOG_CODE_EVENT(isolate, CodeEndLinePosInfoRecordEvent(*code, line_info));
353 }
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000354 return !code.is_null();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000355}
356
357
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000358unsigned FullCodeGenerator::EmitStackCheckTable() {
359 // The stack check table consists of a length (in number of entries)
360 // field, and then a sequence of entries. Each entry is a pair of AST id
361 // and code-relative pc offset.
362 masm()->Align(kIntSize);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000363 unsigned offset = masm()->pc_offset();
364 unsigned length = stack_checks_.length();
365 __ dd(length);
366 for (unsigned i = 0; i < length; ++i) {
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000367 __ dd(stack_checks_[i].id.ToInt());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000368 __ dd(stack_checks_[i].pc_and_state);
369 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000370 return offset;
371}
372
373
374void FullCodeGenerator::PopulateDeoptimizationData(Handle<Code> code) {
375 // Fill in the deoptimization information.
376 ASSERT(info_->HasDeoptimizationSupport() || bailout_entries_.is_empty());
377 if (!info_->HasDeoptimizationSupport()) return;
378 int length = bailout_entries_.length();
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000379 Handle<DeoptimizationOutputData> data = isolate()->factory()->
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000380 NewDeoptimizationOutputData(length, TENURED);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000381 for (int i = 0; i < length; i++) {
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000382 data->SetAstId(i, bailout_entries_[i].id);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000383 data->SetPcAndState(i, Smi::FromInt(bailout_entries_[i].pc_and_state));
384 }
385 code->set_deoptimization_data(*data);
386}
387
388
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000389void FullCodeGenerator::PopulateTypeFeedbackInfo(Handle<Code> code) {
390 Handle<TypeFeedbackInfo> info = isolate()->factory()->NewTypeFeedbackInfo();
391 info->set_ic_total_count(ic_total_count_);
yangguo@chromium.orga7d3df92012-02-27 11:46:55 +0000392 ASSERT(!isolate()->heap()->InNewSpace(*info));
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000393 code->set_type_feedback_info(*info);
394}
395
396
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +0000397void FullCodeGenerator::Initialize() {
398 // The generation of debug code must match between the snapshot code and the
399 // code that is generated later. This is assumed by the debugger when it is
400 // calculating PC offsets after generating a debug version of code. Therefore
401 // we disable the production of debug code in the full compiler if we are
402 // either generating a snapshot or we booted from a snapshot.
403 generate_debug_code_ = FLAG_debug_code &&
404 !Serializer::enabled() &&
405 !Snapshot::HaveASnapshotToStartFrom();
406 masm_->set_emit_debug_code(generate_debug_code_);
407 masm_->set_predictable_code_size(true);
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000408 InitializeAstVisitor();
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +0000409}
410
411
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000412void FullCodeGenerator::PopulateTypeFeedbackCells(Handle<Code> code) {
413 if (type_feedback_cells_.is_empty()) return;
414 int length = type_feedback_cells_.length();
415 int array_size = TypeFeedbackCells::LengthOfFixedArray(length);
416 Handle<TypeFeedbackCells> cache = Handle<TypeFeedbackCells>::cast(
417 isolate()->factory()->NewFixedArray(array_size, TENURED));
418 for (int i = 0; i < length; i++) {
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000419 cache->SetAstId(i, type_feedback_cells_[i].ast_id);
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000420 cache->SetCell(i, *type_feedback_cells_[i].cell);
421 }
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000422 TypeFeedbackInfo::cast(code->type_feedback_info())->set_type_feedback_cells(
423 *cache);
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000424}
425
426
427
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000428void FullCodeGenerator::PrepareForBailout(Expression* node, State state) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000429 PrepareForBailoutForId(node->id(), state);
430}
431
432
433void FullCodeGenerator::RecordJSReturnSite(Call* call) {
434 // We record the offset of the function return so we can rebuild the frame
435 // if the function was inlined, i.e., this is the return address in the
436 // inlined function's frame.
437 //
438 // The state is ignored. We defensively set it to TOS_REG, which is the
439 // real state of the unoptimized code at the return site.
440 PrepareForBailoutForId(call->ReturnId(), TOS_REG);
441#ifdef DEBUG
442 // In debug builds, mark the return so we can verify that this function
443 // was called.
444 ASSERT(!call->return_is_recorded_);
445 call->return_is_recorded_ = true;
446#endif
447}
448
449
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000450void FullCodeGenerator::PrepareForBailoutForId(BailoutId id, State state) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000451 // There's no need to prepare this code for bailouts from already optimized
452 // code or code that can't be optimized.
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000453 if (!info_->HasDeoptimizationSupport()) return;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000454 unsigned pc_and_state =
455 StateField::encode(state) | PcField::encode(masm_->pc_offset());
yangguo@chromium.org56454712012-02-16 15:33:53 +0000456 ASSERT(Smi::IsValid(pc_and_state));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000457 BailoutEntry entry = { id, pc_and_state };
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000458 ASSERT(!prepared_bailout_ids_.Contains(id.ToInt()));
459 prepared_bailout_ids_.Add(id.ToInt(), zone());
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000460 bailout_entries_.Add(entry, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000461}
462
463
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000464void FullCodeGenerator::RecordTypeFeedbackCell(
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000465 TypeFeedbackId id, Handle<JSGlobalPropertyCell> cell) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000466 TypeFeedbackCellEntry entry = { id, cell };
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000467 type_feedback_cells_.Add(entry, zone());
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000468}
469
470
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +0000471void FullCodeGenerator::RecordBackEdge(BailoutId ast_id) {
472 // The pc offset does not need to be encoded and packed together with a state.
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +0000473 ASSERT(masm_->pc_offset() > 0);
474 BailoutEntry entry = { ast_id, static_cast<unsigned>(masm_->pc_offset()) };
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000475 stack_checks_.Add(entry, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000476}
477
478
ricow@chromium.org65fae842010-08-25 15:26:24 +0000479bool FullCodeGenerator::ShouldInlineSmiCase(Token::Value op) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000480 // Inline smi case inside loops, but not division and modulo which
481 // are too complicated and take up too much space.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000482 if (op == Token::DIV ||op == Token::MOD) return false;
483 if (FLAG_always_inline_smi_code) return true;
484 return loop_depth_ > 0;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000485}
486
487
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000488void FullCodeGenerator::EffectContext::Plug(Register reg) const {
489}
490
491
492void FullCodeGenerator::AccumulatorValueContext::Plug(Register reg) const {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000493 __ Move(result_register(), reg);
494}
495
496
497void FullCodeGenerator::StackValueContext::Plug(Register reg) const {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000498 __ push(reg);
499}
500
501
502void FullCodeGenerator::TestContext::Plug(Register reg) const {
503 // For simplicity we always test the accumulator register.
504 __ Move(result_register(), reg);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000505 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000506 codegen()->DoTest(this);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000507}
508
509
510void FullCodeGenerator::EffectContext::PlugTOS() const {
511 __ Drop(1);
512}
513
514
515void FullCodeGenerator::AccumulatorValueContext::PlugTOS() const {
516 __ pop(result_register());
517}
518
519
520void FullCodeGenerator::StackValueContext::PlugTOS() const {
521}
522
523
524void FullCodeGenerator::TestContext::PlugTOS() const {
525 // For simplicity we always test the accumulator register.
526 __ pop(result_register());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000527 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000528 codegen()->DoTest(this);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000529}
530
531
532void FullCodeGenerator::EffectContext::PrepareTest(
533 Label* materialize_true,
534 Label* materialize_false,
535 Label** if_true,
536 Label** if_false,
537 Label** fall_through) const {
538 // In an effect context, the true and the false case branch to the
539 // same label.
540 *if_true = *if_false = *fall_through = materialize_true;
541}
542
543
544void FullCodeGenerator::AccumulatorValueContext::PrepareTest(
545 Label* materialize_true,
546 Label* materialize_false,
547 Label** if_true,
548 Label** if_false,
549 Label** fall_through) const {
550 *if_true = *fall_through = materialize_true;
551 *if_false = materialize_false;
552}
553
554
555void FullCodeGenerator::StackValueContext::PrepareTest(
556 Label* materialize_true,
557 Label* materialize_false,
558 Label** if_true,
559 Label** if_false,
560 Label** fall_through) const {
561 *if_true = *fall_through = materialize_true;
562 *if_false = materialize_false;
563}
564
565
566void FullCodeGenerator::TestContext::PrepareTest(
567 Label* materialize_true,
568 Label* materialize_false,
569 Label** if_true,
570 Label** if_false,
571 Label** fall_through) const {
572 *if_true = true_label_;
573 *if_false = false_label_;
574 *fall_through = fall_through_;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000575}
576
577
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000578void FullCodeGenerator::DoTest(const TestContext* context) {
579 DoTest(context->condition(),
580 context->true_label(),
581 context->false_label(),
582 context->fall_through());
583}
584
585
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000586void FullCodeGenerator::AllocateModules(ZoneList<Declaration*>* declarations) {
587 ASSERT(scope_->is_global_scope());
588
589 for (int i = 0; i < declarations->length(); i++) {
590 ModuleDeclaration* declaration = declarations->at(i)->AsModuleDeclaration();
591 if (declaration != NULL) {
592 ModuleLiteral* module = declaration->module()->AsModuleLiteral();
593 if (module != NULL) {
594 Comment cmnt(masm_, "[ Link nested modules");
595 Scope* scope = module->body()->scope();
596 Interface* interface = scope->interface();
597 ASSERT(interface->IsModule() && interface->IsFrozen());
598
599 interface->Allocate(scope->module_var()->index());
600
601 // Set up module context.
602 ASSERT(scope->interface()->Index() >= 0);
603 __ Push(Smi::FromInt(scope->interface()->Index()));
604 __ Push(scope->GetScopeInfo());
605 __ CallRuntime(Runtime::kPushModuleContext, 2);
606 StoreToFrameField(StandardFrameConstants::kContextOffset,
607 context_register());
608
609 AllocateModules(scope->declarations());
610
611 // Pop module context.
612 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
613 // Update local stack frame context field.
614 StoreToFrameField(StandardFrameConstants::kContextOffset,
615 context_register());
616 }
617 }
618 }
619}
620
621
622// Modules have their own local scope, represented by their own context.
623// Module instance objects have an accessor for every export that forwards
624// access to the respective slot from the module's context. (Exports that are
625// modules themselves, however, are simple data properties.)
626//
627// All modules have a _hosting_ scope/context, which (currently) is the
628// (innermost) enclosing global scope. To deal with recursion, nested modules
629// are hosted by the same scope as global ones.
630//
631// For every (global or nested) module literal, the hosting context has an
632// internal slot that points directly to the respective module context. This
633// enables quick access to (statically resolved) module members by 2-dimensional
634// access through the hosting context. For example,
635//
636// module A {
637// let x;
638// module B { let y; }
639// }
640// module C { let z; }
641//
642// allocates contexts as follows:
643//
644// [header| .A | .B | .C | A | C ] (global)
645// | | |
646// | | +-- [header| z ] (module)
647// | |
648// | +------- [header| y ] (module)
649// |
650// +------------ [header| x | B ] (module)
651//
652// Here, .A, .B, .C are the internal slots pointing to the hosted module
653// contexts, whereas A, B, C hold the actual instance objects (note that every
654// module context also points to the respective instance object through its
655// extension slot in the header).
656//
657// To deal with arbitrary recursion and aliases between modules,
658// they are created and initialized in several stages. Each stage applies to
659// all modules in the hosting global scope, including nested ones.
660//
661// 1. Allocate: for each module _literal_, allocate the module contexts and
662// respective instance object and wire them up. This happens in the
663// PushModuleContext runtime function, as generated by AllocateModules
664// (invoked by VisitDeclarations in the hosting scope).
665//
666// 2. Bind: for each module _declaration_ (i.e. literals as well as aliases),
667// assign the respective instance object to respective local variables. This
668// happens in VisitModuleDeclaration, and uses the instance objects created
669// in the previous stage.
670// For each module _literal_, this phase also constructs a module descriptor
671// for the next stage. This happens in VisitModuleLiteral.
672//
673// 3. Populate: invoke the DeclareModules runtime function to populate each
674// _instance_ object with accessors for it exports. This is generated by
675// DeclareModules (invoked by VisitDeclarations in the hosting scope again),
676// and uses the descriptors generated in the previous stage.
677//
678// 4. Initialize: execute the module bodies (and other code) in sequence. This
679// happens by the separate statements generated for module bodies. To reenter
680// the module scopes properly, the parser inserted ModuleStatements.
681
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000682void FullCodeGenerator::VisitDeclarations(
683 ZoneList<Declaration*>* declarations) {
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000684 Handle<FixedArray> saved_modules = modules_;
685 int saved_module_index = module_index_;
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000686 ZoneList<Handle<Object> >* saved_globals = globals_;
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000687 ZoneList<Handle<Object> > inner_globals(10, zone());
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000688 globals_ = &inner_globals;
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000689
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000690 if (scope_->num_modules() != 0) {
691 // This is a scope hosting modules. Allocate a descriptor array to pass
692 // to the runtime for initialization.
693 Comment cmnt(masm_, "[ Allocate modules");
694 ASSERT(scope_->is_global_scope());
695 modules_ =
696 isolate()->factory()->NewFixedArray(scope_->num_modules(), TENURED);
697 module_index_ = 0;
698
699 // Generate code for allocating all modules, including nested ones.
700 // The allocated contexts are stored in internal variables in this scope.
701 AllocateModules(declarations);
702 }
703
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000704 AstVisitor::VisitDeclarations(declarations);
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000705
706 if (scope_->num_modules() != 0) {
707 // Initialize modules from descriptor array.
708 ASSERT(module_index_ == modules_->length());
709 DeclareModules(modules_);
710 modules_ = saved_modules;
711 module_index_ = saved_module_index;
712 }
713
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000714 if (!globals_->is_empty()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000715 // Invoke the platform-dependent code generator to do the actual
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000716 // declaration of the global functions and variables.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000717 Handle<FixedArray> array =
718 isolate()->factory()->NewFixedArray(globals_->length(), TENURED);
719 for (int i = 0; i < globals_->length(); ++i)
720 array->set(i, *globals_->at(i));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000721 DeclareGlobals(array);
722 }
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000723
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000724 globals_ = saved_globals;
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000725}
726
727
728void FullCodeGenerator::VisitModuleLiteral(ModuleLiteral* module) {
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000729 Block* block = module->body();
730 Scope* saved_scope = scope();
731 scope_ = block->scope();
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000732 Interface* interface = scope_->interface();
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000733
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000734 Comment cmnt(masm_, "[ ModuleLiteral");
735 SetStatementPosition(block);
736
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000737 ASSERT(!modules_.is_null());
738 ASSERT(module_index_ < modules_->length());
739 int index = module_index_++;
740
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000741 // Set up module context.
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000742 ASSERT(interface->Index() >= 0);
743 __ Push(Smi::FromInt(interface->Index()));
744 __ Push(Smi::FromInt(0));
745 __ CallRuntime(Runtime::kPushModuleContext, 2);
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000746 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000747
748 {
749 Comment cmnt(masm_, "[ Declarations");
750 VisitDeclarations(scope_->declarations());
751 }
752
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000753 // Populate the module description.
754 Handle<ModuleInfo> description =
755 ModuleInfo::Create(isolate(), interface, scope_);
756 modules_->set(index, *description);
757
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000758 scope_ = saved_scope;
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000759 // Pop module context.
760 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
761 // Update local stack frame context field.
762 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000763}
764
765
766void FullCodeGenerator::VisitModuleVariable(ModuleVariable* module) {
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000767 // Nothing to do.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000768 // The instance object is resolved statically through the module's interface.
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000769}
770
771
772void FullCodeGenerator::VisitModulePath(ModulePath* module) {
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000773 // Nothing to do.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000774 // The instance object is resolved statically through the module's interface.
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000775}
776
777
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000778void FullCodeGenerator::VisitModuleUrl(ModuleUrl* module) {
779 // TODO(rossberg): dummy allocation for now.
780 Scope* scope = module->body()->scope();
781 Interface* interface = scope_->interface();
782
783 ASSERT(interface->IsModule() && interface->IsFrozen());
784 ASSERT(!modules_.is_null());
785 ASSERT(module_index_ < modules_->length());
786 interface->Allocate(scope->module_var()->index());
787 int index = module_index_++;
788
789 Handle<ModuleInfo> description =
790 ModuleInfo::Create(isolate(), interface, scope_);
791 modules_->set(index, *description);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000792}
793
794
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000795int FullCodeGenerator::DeclareGlobalsFlags() {
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000796 ASSERT(DeclareGlobalsLanguageMode::is_valid(language_mode()));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000797 return DeclareGlobalsEvalFlag::encode(is_eval()) |
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000798 DeclareGlobalsNativeFlag::encode(is_native()) |
799 DeclareGlobalsLanguageMode::encode(language_mode());
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000800}
801
802
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000803void FullCodeGenerator::SetFunctionPosition(FunctionLiteral* fun) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000804 CodeGenerator::RecordPositions(masm_, fun->start_position());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000805}
806
807
808void FullCodeGenerator::SetReturnPosition(FunctionLiteral* fun) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000809 CodeGenerator::RecordPositions(masm_, fun->end_position() - 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000810}
811
812
813void FullCodeGenerator::SetStatementPosition(Statement* stmt) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000814#ifdef ENABLE_DEBUGGER_SUPPORT
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000815 if (!isolate()->debugger()->IsDebuggerActive()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000816 CodeGenerator::RecordPositions(masm_, stmt->statement_pos());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000817 } else {
818 // Check if the statement will be breakable without adding a debug break
819 // slot.
820 BreakableStatementChecker checker;
821 checker.Check(stmt);
822 // Record the statement position right here if the statement is not
823 // breakable. For breakable statements the actual recording of the
824 // position will be postponed to the breakable code (typically an IC).
825 bool position_recorded = CodeGenerator::RecordPositions(
826 masm_, stmt->statement_pos(), !checker.is_breakable());
827 // If the position recording did record a new position generate a debug
828 // break slot to make the statement breakable.
829 if (position_recorded) {
830 Debug::GenerateSlot(masm_);
831 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000832 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000833#else
834 CodeGenerator::RecordPositions(masm_, stmt->statement_pos());
835#endif
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000836}
837
838
839void FullCodeGenerator::SetExpressionPosition(Expression* expr, int pos) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000840#ifdef ENABLE_DEBUGGER_SUPPORT
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000841 if (!isolate()->debugger()->IsDebuggerActive()) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000842 CodeGenerator::RecordPositions(masm_, pos);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000843 } else {
844 // Check if the expression will be breakable without adding a debug break
845 // slot.
846 BreakableStatementChecker checker;
847 checker.Check(expr);
848 // Record a statement position right here if the expression is not
849 // breakable. For breakable expressions the actual recording of the
850 // position will be postponed to the breakable code (typically an IC).
851 // NOTE this will record a statement position for something which might
852 // not be a statement. As stepping in the debugger will only stop at
853 // statement positions this is used for e.g. the condition expression of
854 // a do while loop.
855 bool position_recorded = CodeGenerator::RecordPositions(
856 masm_, pos, !checker.is_breakable());
857 // If the position recording did record a new position generate a debug
858 // break slot to make the statement breakable.
859 if (position_recorded) {
860 Debug::GenerateSlot(masm_);
861 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000862 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000863#else
864 CodeGenerator::RecordPositions(masm_, pos);
865#endif
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000866}
867
868
869void FullCodeGenerator::SetStatementPosition(int pos) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000870 CodeGenerator::RecordPositions(masm_, pos);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000871}
872
873
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000874void FullCodeGenerator::SetSourcePosition(int pos) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000875 if (pos != RelocInfo::kNoPosition) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000876 masm_->positions_recorder()->RecordPosition(pos);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000877 }
878}
879
880
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000881// Lookup table for code generators for special runtime calls which are
882// generated inline.
883#define INLINE_FUNCTION_GENERATOR_ADDRESS(Name, argc, ressize) \
884 &FullCodeGenerator::Emit##Name,
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000885
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000886const FullCodeGenerator::InlineFunctionGenerator
887 FullCodeGenerator::kInlineFunctionGenerators[] = {
888 INLINE_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS)
889 INLINE_RUNTIME_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS)
890 };
891#undef INLINE_FUNCTION_GENERATOR_ADDRESS
892
893
894FullCodeGenerator::InlineFunctionGenerator
895 FullCodeGenerator::FindInlineFunctionGenerator(Runtime::FunctionId id) {
whesse@chromium.org023421e2010-12-21 12:19:12 +0000896 int lookup_index =
897 static_cast<int>(id) - static_cast<int>(Runtime::kFirstInlineFunction);
898 ASSERT(lookup_index >= 0);
899 ASSERT(static_cast<size_t>(lookup_index) <
900 ARRAY_SIZE(kInlineFunctionGenerators));
901 return kInlineFunctionGenerators[lookup_index];
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000902}
903
904
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000905void FullCodeGenerator::EmitInlineRuntimeCall(CallRuntime* expr) {
906 const Runtime::Function* function = expr->function();
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000907 ASSERT(function != NULL);
908 ASSERT(function->intrinsic_type == Runtime::INLINE);
909 InlineFunctionGenerator generator =
910 FindInlineFunctionGenerator(function->function_id);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000911 ((*this).*(generator))(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000912}
913
914
915void FullCodeGenerator::VisitBinaryOperation(BinaryOperation* expr) {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000916 switch (expr->op()) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000917 case Token::COMMA:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000918 return VisitComma(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000919 case Token::OR:
920 case Token::AND:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000921 return VisitLogicalExpression(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000922 default:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000923 return VisitArithmeticExpression(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000924 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000925}
926
927
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000928void FullCodeGenerator::VisitInDuplicateContext(Expression* expr) {
929 if (context()->IsEffect()) {
930 VisitForEffect(expr);
931 } else if (context()->IsAccumulatorValue()) {
932 VisitForAccumulatorValue(expr);
933 } else if (context()->IsStackValue()) {
934 VisitForStackValue(expr);
935 } else if (context()->IsTest()) {
936 const TestContext* test = TestContext::cast(context());
937 VisitForControl(expr, test->true_label(), test->false_label(),
938 test->fall_through());
939 }
940}
941
942
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000943void FullCodeGenerator::VisitComma(BinaryOperation* expr) {
944 Comment cmnt(masm_, "[ Comma");
945 VisitForEffect(expr->left());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000946 VisitInDuplicateContext(expr->right());
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000947}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000948
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000949
950void FullCodeGenerator::VisitLogicalExpression(BinaryOperation* expr) {
951 bool is_logical_and = expr->op() == Token::AND;
952 Comment cmnt(masm_, is_logical_and ? "[ Logical AND" : "[ Logical OR");
953 Expression* left = expr->left();
954 Expression* right = expr->right();
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000955 BailoutId right_id = expr->RightId();
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000956 Label done;
957
958 if (context()->IsTest()) {
959 Label eval_right;
960 const TestContext* test = TestContext::cast(context());
961 if (is_logical_and) {
962 VisitForControl(left, &eval_right, test->false_label(), &eval_right);
963 } else {
964 VisitForControl(left, test->true_label(), &eval_right, &eval_right);
965 }
966 PrepareForBailoutForId(right_id, NO_REGISTERS);
967 __ bind(&eval_right);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000968
969 } else if (context()->IsAccumulatorValue()) {
970 VisitForAccumulatorValue(left);
971 // We want the value in the accumulator for the test, and on the stack in
972 // case we need it.
973 __ push(result_register());
974 Label discard, restore;
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000975 if (is_logical_and) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000976 DoTest(left, &discard, &restore, &restore);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000977 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000978 DoTest(left, &restore, &discard, &restore);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000979 }
980 __ bind(&restore);
981 __ pop(result_register());
982 __ jmp(&done);
983 __ bind(&discard);
984 __ Drop(1);
985 PrepareForBailoutForId(right_id, NO_REGISTERS);
986
987 } else if (context()->IsStackValue()) {
988 VisitForAccumulatorValue(left);
989 // We want the value in the accumulator for the test, and on the stack in
990 // case we need it.
991 __ push(result_register());
992 Label discard;
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000993 if (is_logical_and) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000994 DoTest(left, &discard, &done, &discard);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000995 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000996 DoTest(left, &done, &discard, &discard);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000997 }
998 __ bind(&discard);
999 __ Drop(1);
1000 PrepareForBailoutForId(right_id, NO_REGISTERS);
1001
1002 } else {
1003 ASSERT(context()->IsEffect());
1004 Label eval_right;
1005 if (is_logical_and) {
1006 VisitForControl(left, &eval_right, &done, &eval_right);
1007 } else {
1008 VisitForControl(left, &done, &eval_right, &eval_right);
1009 }
1010 PrepareForBailoutForId(right_id, NO_REGISTERS);
1011 __ bind(&eval_right);
1012 }
1013
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001014 VisitInDuplicateContext(right);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001015 __ bind(&done);
1016}
1017
1018
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001019void FullCodeGenerator::VisitArithmeticExpression(BinaryOperation* expr) {
1020 Token::Value op = expr->op();
1021 Comment cmnt(masm_, "[ ArithmeticExpression");
1022 Expression* left = expr->left();
1023 Expression* right = expr->right();
1024 OverwriteMode mode =
1025 left->ResultOverwriteAllowed()
1026 ? OVERWRITE_LEFT
1027 : (right->ResultOverwriteAllowed() ? OVERWRITE_RIGHT : NO_OVERWRITE);
1028
1029 VisitForStackValue(left);
1030 VisitForAccumulatorValue(right);
1031
1032 SetSourcePosition(expr->position());
1033 if (ShouldInlineSmiCase(op)) {
1034 EmitInlineSmiBinaryOp(expr, op, mode, left, right);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001035 } else {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001036 EmitBinaryOp(expr, op, mode);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001037 }
1038}
1039
1040
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001041void FullCodeGenerator::VisitBlock(Block* stmt) {
1042 Comment cmnt(masm_, "[ Block");
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001043 NestedBlock nested_block(this, stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001044 SetStatementPosition(stmt);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001045
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001046 Scope* saved_scope = scope();
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001047 // Push a block context when entering a block with block scoped variables.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00001048 if (stmt->scope() != NULL) {
danno@chromium.org81cac2b2012-07-10 11:28:27 +00001049 scope_ = stmt->scope();
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00001050 ASSERT(!scope_->is_module_scope());
1051 { Comment cmnt(masm_, "[ Extend block context");
1052 Handle<ScopeInfo> scope_info = scope_->GetScopeInfo();
1053 int heap_slots = scope_info->ContextLength() - Context::MIN_CONTEXT_SLOTS;
1054 __ Push(scope_info);
1055 PushFunctionArgumentForContextAllocation();
1056 if (heap_slots <= FastNewBlockContextStub::kMaximumSlots) {
1057 FastNewBlockContextStub stub(heap_slots);
1058 __ CallStub(&stub);
1059 } else {
1060 __ CallRuntime(Runtime::kPushBlockContext, 2);
1061 }
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001062
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00001063 // Replace the context stored in the frame.
1064 StoreToFrameField(StandardFrameConstants::kContextOffset,
1065 context_register());
1066 }
1067 { Comment cmnt(masm_, "[ Declarations");
1068 VisitDeclarations(scope_->declarations());
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001069 }
1070 }
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00001071
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00001072 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001073 VisitStatements(stmt->statements());
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001074 scope_ = saved_scope;
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001075 __ bind(nested_block.break_label());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001076 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001077
1078 // Pop block context if necessary.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00001079 if (stmt->scope() != NULL) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001080 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1081 // Update local stack frame context field.
1082 StoreToFrameField(StandardFrameConstants::kContextOffset,
1083 context_register());
1084 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001085}
1086
1087
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00001088void FullCodeGenerator::VisitModuleStatement(ModuleStatement* stmt) {
1089 Comment cmnt(masm_, "[ Module context");
1090
1091 __ Push(Smi::FromInt(stmt->proxy()->interface()->Index()));
1092 __ Push(Smi::FromInt(0));
1093 __ CallRuntime(Runtime::kPushModuleContext, 2);
1094 StoreToFrameField(
1095 StandardFrameConstants::kContextOffset, context_register());
1096
1097 Scope* saved_scope = scope_;
1098 scope_ = stmt->body()->scope();
1099 VisitStatements(stmt->body()->statements());
1100 scope_ = saved_scope;
1101 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1102 // Update local stack frame context field.
1103 StoreToFrameField(StandardFrameConstants::kContextOffset,
1104 context_register());
1105}
1106
1107
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001108void FullCodeGenerator::VisitExpressionStatement(ExpressionStatement* stmt) {
1109 Comment cmnt(masm_, "[ ExpressionStatement");
1110 SetStatementPosition(stmt);
1111 VisitForEffect(stmt->expression());
1112}
1113
1114
1115void FullCodeGenerator::VisitEmptyStatement(EmptyStatement* stmt) {
1116 Comment cmnt(masm_, "[ EmptyStatement");
1117 SetStatementPosition(stmt);
1118}
1119
1120
1121void FullCodeGenerator::VisitIfStatement(IfStatement* stmt) {
1122 Comment cmnt(masm_, "[ IfStatement");
1123 SetStatementPosition(stmt);
1124 Label then_part, else_part, done;
1125
ricow@chromium.org65fae842010-08-25 15:26:24 +00001126 if (stmt->HasElseStatement()) {
1127 VisitForControl(stmt->condition(), &then_part, &else_part, &then_part);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001128 PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001129 __ bind(&then_part);
1130 Visit(stmt->then_statement());
1131 __ jmp(&done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001132
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001133 PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001134 __ bind(&else_part);
1135 Visit(stmt->else_statement());
1136 } else {
1137 VisitForControl(stmt->condition(), &then_part, &done, &then_part);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001138 PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001139 __ bind(&then_part);
1140 Visit(stmt->then_statement());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001141
1142 PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001143 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001144 __ bind(&done);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001145 PrepareForBailoutForId(stmt->IfId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001146}
1147
1148
1149void FullCodeGenerator::VisitContinueStatement(ContinueStatement* stmt) {
1150 Comment cmnt(masm_, "[ ContinueStatement");
1151 SetStatementPosition(stmt);
1152 NestedStatement* current = nesting_stack_;
1153 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001154 int context_length = 0;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001155 // When continuing, we clobber the unpredictable value in the accumulator
1156 // with one that's safe for GC. If we hit an exit from the try block of
1157 // try...finally on our way out, we will unconditionally preserve the
1158 // accumulator on the stack.
1159 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001160 while (!current->IsContinueTarget(stmt->target())) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001161 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001162 }
1163 __ Drop(stack_depth);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001164 if (context_length > 0) {
1165 while (context_length > 0) {
1166 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1167 --context_length;
1168 }
1169 StoreToFrameField(StandardFrameConstants::kContextOffset,
1170 context_register());
1171 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001172
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001173 __ jmp(current->AsIteration()->continue_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001174}
1175
1176
1177void FullCodeGenerator::VisitBreakStatement(BreakStatement* stmt) {
1178 Comment cmnt(masm_, "[ BreakStatement");
1179 SetStatementPosition(stmt);
1180 NestedStatement* current = nesting_stack_;
1181 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001182 int context_length = 0;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001183 // When breaking, we clobber the unpredictable value in the accumulator
1184 // with one that's safe for GC. If we hit an exit from the try block of
1185 // try...finally on our way out, we will unconditionally preserve the
1186 // accumulator on the stack.
1187 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001188 while (!current->IsBreakTarget(stmt->target())) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001189 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001190 }
1191 __ Drop(stack_depth);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001192 if (context_length > 0) {
1193 while (context_length > 0) {
1194 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1195 --context_length;
1196 }
1197 StoreToFrameField(StandardFrameConstants::kContextOffset,
1198 context_register());
1199 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001200
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001201 __ jmp(current->AsBreakable()->break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001202}
1203
1204
1205void FullCodeGenerator::VisitReturnStatement(ReturnStatement* stmt) {
1206 Comment cmnt(masm_, "[ ReturnStatement");
1207 SetStatementPosition(stmt);
1208 Expression* expr = stmt->expression();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001209 VisitForAccumulatorValue(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001210
1211 // Exit all nested statements.
1212 NestedStatement* current = nesting_stack_;
1213 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001214 int context_length = 0;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001215 while (current != NULL) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001216 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001217 }
1218 __ Drop(stack_depth);
1219
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001220 EmitReturnSequence();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001221}
1222
1223
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001224void FullCodeGenerator::VisitWithStatement(WithStatement* stmt) {
1225 Comment cmnt(masm_, "[ WithStatement");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001226 SetStatementPosition(stmt);
1227
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001228 VisitForStackValue(stmt->expression());
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00001229 PushFunctionArgumentForContextAllocation();
1230 __ CallRuntime(Runtime::kPushWithContext, 2);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001231 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001232
1233 { WithOrCatch body(this);
1234 Visit(stmt->statement());
1235 }
1236
1237 // Pop context.
1238 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1239 // Update local stack frame context field.
1240 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001241}
1242
1243
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001244void FullCodeGenerator::VisitDoWhileStatement(DoWhileStatement* stmt) {
1245 Comment cmnt(masm_, "[ DoWhileStatement");
1246 SetStatementPosition(stmt);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001247 Label body, stack_check;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001248
1249 Iteration loop_statement(this, stmt);
1250 increment_loop_depth();
1251
1252 __ bind(&body);
1253 Visit(stmt->body());
1254
ricow@chromium.org65fae842010-08-25 15:26:24 +00001255 // Record the position of the do while condition and make sure it is
1256 // possible to break on the condition.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001257 __ bind(loop_statement.continue_label());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001258 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001259 SetExpressionPosition(stmt->cond(), stmt->condition_position());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001260 VisitForControl(stmt->cond(),
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001261 &stack_check,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001262 loop_statement.break_label(),
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001263 &stack_check);
1264
1265 // Check stack before looping.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001266 PrepareForBailoutForId(stmt->BackEdgeId(), NO_REGISTERS);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001267 __ bind(&stack_check);
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +00001268 EmitBackEdgeBookkeeping(stmt, &body);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001269 __ jmp(&body);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001270
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001271 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001272 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001273 decrement_loop_depth();
1274}
1275
1276
1277void FullCodeGenerator::VisitWhileStatement(WhileStatement* stmt) {
1278 Comment cmnt(masm_, "[ WhileStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001279 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001280
1281 Iteration loop_statement(this, stmt);
1282 increment_loop_depth();
1283
1284 // Emit the test at the bottom of the loop.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001285 __ jmp(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001286
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001287 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001288 __ bind(&body);
1289 Visit(stmt->body());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001290
1291 // Emit the statement position here as this is where the while
1292 // statement code starts.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001293 __ bind(loop_statement.continue_label());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001294 SetStatementPosition(stmt);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001295
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001296 // Check stack before looping.
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +00001297 EmitBackEdgeBookkeeping(stmt, &body);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001298
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001299 __ bind(&test);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001300 VisitForControl(stmt->cond(),
1301 &body,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001302 loop_statement.break_label(),
1303 loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001304
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001305 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001306 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001307 decrement_loop_depth();
1308}
1309
1310
1311void FullCodeGenerator::VisitForStatement(ForStatement* stmt) {
1312 Comment cmnt(masm_, "[ ForStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001313 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001314
1315 Iteration loop_statement(this, stmt);
erik.corry@gmail.combbceb572012-03-09 10:52:05 +00001316
1317 // Set statement position for a break slot before entering the for-body.
1318 SetStatementPosition(stmt);
1319
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001320 if (stmt->init() != NULL) {
1321 Visit(stmt->init());
1322 }
1323
1324 increment_loop_depth();
1325 // Emit the test at the bottom of the loop (even if empty).
1326 __ jmp(&test);
1327
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001328 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001329 __ bind(&body);
1330 Visit(stmt->body());
1331
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001332 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001333 __ bind(loop_statement.continue_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001334 if (stmt->next() != NULL) {
1335 Visit(stmt->next());
1336 }
1337
ricow@chromium.org65fae842010-08-25 15:26:24 +00001338 // Emit the statement position here as this is where the for
1339 // statement code starts.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001340 SetStatementPosition(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001341
1342 // Check stack before looping.
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +00001343 EmitBackEdgeBookkeeping(stmt, &body);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001344
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001345 __ bind(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001346 if (stmt->cond() != NULL) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00001347 VisitForControl(stmt->cond(),
1348 &body,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001349 loop_statement.break_label(),
1350 loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001351 } else {
1352 __ jmp(&body);
1353 }
1354
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001355 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001356 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001357 decrement_loop_depth();
1358}
1359
1360
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001361void FullCodeGenerator::VisitTryCatchStatement(TryCatchStatement* stmt) {
1362 Comment cmnt(masm_, "[ TryCatchStatement");
1363 SetStatementPosition(stmt);
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001364 // The try block adds a handler to the exception handler chain before
1365 // entering, and removes it again when exiting normally. If an exception
1366 // is thrown during execution of the try block, the handler is consumed
1367 // and control is passed to the catch block with the exception in the
1368 // result register.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001369
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001370 Label try_entry, handler_entry, exit;
1371 __ jmp(&try_entry);
1372 __ bind(&handler_entry);
1373 handler_table()->set(stmt->index(), Smi::FromInt(handler_entry.pos()));
1374 // Exception handler code, the exception is in the result register.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001375 // Extend the context before executing the catch block.
1376 { Comment cmnt(masm_, "[ Extend catch context");
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001377 __ Push(stmt->variable()->name());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001378 __ push(result_register());
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00001379 PushFunctionArgumentForContextAllocation();
1380 __ CallRuntime(Runtime::kPushCatchContext, 3);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001381 StoreToFrameField(StandardFrameConstants::kContextOffset,
1382 context_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001383 }
1384
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001385 Scope* saved_scope = scope();
1386 scope_ = stmt->scope();
1387 ASSERT(scope_->declarations()->is_empty());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001388 { WithOrCatch catch_body(this);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001389 Visit(stmt->catch_block());
1390 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001391 // Restore the context.
1392 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1393 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001394 scope_ = saved_scope;
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001395 __ jmp(&exit);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001396
1397 // Try block code. Sets up the exception handler chain.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001398 __ bind(&try_entry);
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00001399 __ PushTryHandler(StackHandler::CATCH, stmt->index());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001400 { TryCatch try_body(this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001401 Visit(stmt->try_block());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001402 }
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001403 __ PopTryHandler();
1404 __ bind(&exit);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001405}
1406
1407
1408void FullCodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* stmt) {
1409 Comment cmnt(masm_, "[ TryFinallyStatement");
1410 SetStatementPosition(stmt);
1411 // Try finally is compiled by setting up a try-handler on the stack while
1412 // executing the try body, and removing it again afterwards.
1413 //
1414 // The try-finally construct can enter the finally block in three ways:
1415 // 1. By exiting the try-block normally. This removes the try-handler and
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001416 // calls the finally block code before continuing.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001417 // 2. By exiting the try-block with a function-local control flow transfer
1418 // (break/continue/return). The site of the, e.g., break removes the
1419 // try handler and calls the finally block code before continuing
1420 // its outward control transfer.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001421 // 3. By exiting the try-block with a thrown exception.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001422 // This can happen in nested function calls. It traverses the try-handler
1423 // chain and consumes the try-handler entry before jumping to the
1424 // handler code. The handler code then calls the finally-block before
1425 // rethrowing the exception.
1426 //
1427 // The finally block must assume a return address on top of the stack
1428 // (or in the link register on ARM chips) and a value (return value or
1429 // exception) in the result register (rax/eax/r0), both of which must
1430 // be preserved. The return address isn't GC-safe, so it should be
1431 // cooked before GC.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001432 Label try_entry, handler_entry, finally_entry;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001433
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001434 // Jump to try-handler setup and try-block code.
1435 __ jmp(&try_entry);
1436 __ bind(&handler_entry);
1437 handler_table()->set(stmt->index(), Smi::FromInt(handler_entry.pos()));
1438 // Exception handler code. This code is only executed when an exception
1439 // is thrown. The exception is in the result register, and must be
1440 // preserved by the finally block. Call the finally block and then
1441 // rethrow the exception if it returns.
1442 __ Call(&finally_entry);
1443 __ push(result_register());
1444 __ CallRuntime(Runtime::kReThrow, 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001445
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001446 // Finally block implementation.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001447 __ bind(&finally_entry);
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001448 EnterFinallyBlock();
1449 { Finally finally_body(this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001450 Visit(stmt->finally_block());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001451 }
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001452 ExitFinallyBlock(); // Return to the calling code.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001453
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001454 // Set up try handler.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001455 __ bind(&try_entry);
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00001456 __ PushTryHandler(StackHandler::FINALLY, stmt->index());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001457 { TryFinally try_body(this, &finally_entry);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001458 Visit(stmt->try_block());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001459 }
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001460 __ PopTryHandler();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001461 // Execute the finally block on the way out. Clobber the unpredictable
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001462 // value in the result register with one that's safe for GC because the
1463 // finally block will unconditionally preserve the result register on the
1464 // stack.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001465 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001466 __ Call(&finally_entry);
1467}
1468
1469
1470void FullCodeGenerator::VisitDebuggerStatement(DebuggerStatement* stmt) {
1471#ifdef ENABLE_DEBUGGER_SUPPORT
1472 Comment cmnt(masm_, "[ DebuggerStatement");
1473 SetStatementPosition(stmt);
1474
ager@chromium.org5c838252010-02-19 08:53:10 +00001475 __ DebugBreak();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001476 // Ignore the return value.
1477#endif
1478}
1479
1480
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001481void FullCodeGenerator::VisitConditional(Conditional* expr) {
1482 Comment cmnt(masm_, "[ Conditional");
1483 Label true_case, false_case, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001484 VisitForControl(expr->condition(), &true_case, &false_case, &true_case);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001485
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001486 PrepareForBailoutForId(expr->ThenId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001487 __ bind(&true_case);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001488 SetExpressionPosition(expr->then_expression(),
1489 expr->then_expression_position());
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001490 if (context()->IsTest()) {
1491 const TestContext* for_test = TestContext::cast(context());
1492 VisitForControl(expr->then_expression(),
1493 for_test->true_label(),
1494 for_test->false_label(),
1495 NULL);
1496 } else {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001497 VisitInDuplicateContext(expr->then_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001498 __ jmp(&done);
1499 }
1500
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001501 PrepareForBailoutForId(expr->ElseId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001502 __ bind(&false_case);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001503 SetExpressionPosition(expr->else_expression(),
1504 expr->else_expression_position());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001505 VisitInDuplicateContext(expr->else_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001506 // If control flow falls through Visit, merge it with true case here.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001507 if (!context()->IsTest()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001508 __ bind(&done);
1509 }
1510}
1511
1512
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001513void FullCodeGenerator::VisitLiteral(Literal* expr) {
1514 Comment cmnt(masm_, "[ Literal");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001515 context()->Plug(expr->handle());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001516}
1517
1518
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001519void FullCodeGenerator::VisitFunctionLiteral(FunctionLiteral* expr) {
1520 Comment cmnt(masm_, "[ FunctionLiteral");
1521
1522 // Build the function boilerplate and instantiate it.
1523 Handle<SharedFunctionInfo> function_info =
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001524 Compiler::BuildFunctionInfo(expr, script());
1525 if (function_info.is_null()) {
1526 SetStackOverflow();
1527 return;
1528 }
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001529 EmitNewClosure(function_info, expr->pretenure());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001530}
1531
1532
1533void FullCodeGenerator::VisitSharedFunctionInfoLiteral(
1534 SharedFunctionInfoLiteral* expr) {
1535 Comment cmnt(masm_, "[ SharedFunctionInfoLiteral");
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001536 EmitNewClosure(expr->shared_function_info(), false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001537}
1538
1539
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001540void FullCodeGenerator::VisitThrow(Throw* expr) {
1541 Comment cmnt(masm_, "[ Throw");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001542 VisitForStackValue(expr->exception());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001543 __ CallRuntime(Runtime::kThrow, 1);
1544 // Never returns here.
1545}
1546
1547
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001548FullCodeGenerator::NestedStatement* FullCodeGenerator::TryCatch::Exit(
1549 int* stack_depth,
1550 int* context_length) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001551 // The macros used here must preserve the result register.
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001552 __ Drop(*stack_depth);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001553 __ PopTryHandler();
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001554 *stack_depth = 0;
1555 return previous_;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001556}
1557
ricow@chromium.org65fae842010-08-25 15:26:24 +00001558
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001559bool FullCodeGenerator::TryLiteralCompare(CompareOperation* expr) {
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001560 Expression* sub_expr;
ager@chromium.org04921a82011-06-27 13:21:41 +00001561 Handle<String> check;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001562 if (expr->IsLiteralCompareTypeof(&sub_expr, &check)) {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001563 EmitLiteralCompareTypeof(expr, sub_expr, check);
ager@chromium.org04921a82011-06-27 13:21:41 +00001564 return true;
1565 }
1566
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001567 if (expr->IsLiteralCompareUndefined(&sub_expr)) {
1568 EmitLiteralCompareNil(expr, sub_expr, kUndefinedValue);
1569 return true;
1570 }
1571
1572 if (expr->IsLiteralCompareNull(&sub_expr)) {
1573 EmitLiteralCompareNil(expr, sub_expr, kNullValue);
ager@chromium.org04921a82011-06-27 13:21:41 +00001574 return true;
1575 }
1576
1577 return false;
1578}
1579
1580
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001581#undef __
1582
1583
1584} } // namespace v8::internal