blob: ad2a9947f3d89a1f5363258d059151b2def093df [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
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000235void BreakableStatementChecker::VisitYield(Yield* expr) {
236 // Yield is breakable if the expression is.
237 Visit(expr->expression());
238}
239
240
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000241void BreakableStatementChecker::VisitThrow(Throw* expr) {
242 // Throw is breakable if the expression is.
243 Visit(expr->exception());
244}
245
246
247void BreakableStatementChecker::VisitProperty(Property* expr) {
248 // Property load is breakable.
249 is_breakable_ = true;
250}
251
252
253void BreakableStatementChecker::VisitCall(Call* expr) {
254 // Function calls both through IC and call stub are breakable.
255 is_breakable_ = true;
256}
257
258
259void BreakableStatementChecker::VisitCallNew(CallNew* expr) {
260 // Function calls through new are breakable.
261 is_breakable_ = true;
262}
263
264
265void BreakableStatementChecker::VisitCallRuntime(CallRuntime* expr) {
266}
267
268
269void BreakableStatementChecker::VisitUnaryOperation(UnaryOperation* expr) {
270 Visit(expr->expression());
271}
272
273
274void BreakableStatementChecker::VisitCountOperation(CountOperation* expr) {
275 Visit(expr->expression());
276}
277
278
279void BreakableStatementChecker::VisitBinaryOperation(BinaryOperation* expr) {
280 Visit(expr->left());
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +0000281 if (expr->op() != Token::AND &&
282 expr->op() != Token::OR) {
283 Visit(expr->right());
284 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000285}
286
287
288void BreakableStatementChecker::VisitCompareOperation(CompareOperation* expr) {
289 Visit(expr->left());
290 Visit(expr->right());
291}
292
293
294void BreakableStatementChecker::VisitThisFunction(ThisFunction* expr) {
295}
296
297
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000298#define __ ACCESS_MASM(masm())
299
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000300bool FullCodeGenerator::MakeCode(CompilationInfo* info) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000301 Isolate* isolate = info->isolate();
ager@chromium.org5c838252010-02-19 08:53:10 +0000302 Handle<Script> script = info->script();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000303 if (!script->IsUndefined() && !script->source()->IsUndefined()) {
304 int len = String::cast(script->source())->length();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000305 isolate->counters()->total_full_codegen_source_size()->Increment(len);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000306 }
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +0000307 CodeGenerator::MakeCodePrologue(info, "full");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000308 const int kInitialBufferSize = 4 * KB;
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000309 MacroAssembler masm(info->isolate(), NULL, kInitialBufferSize);
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000310#ifdef ENABLE_GDB_JIT_INTERFACE
311 masm.positions_recorder()->StartGDBJITLineInfoRecording();
312#endif
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000313 LOG_CODE_EVENT(isolate,
314 CodeStartLinePosInfoRecordEvent(masm.positions_recorder()));
ager@chromium.org5c838252010-02-19 08:53:10 +0000315
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000316 FullCodeGenerator cgen(&masm, info);
yangguo@chromium.org56454712012-02-16 15:33:53 +0000317 cgen.Generate();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000318 if (cgen.HasStackOverflow()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000319 ASSERT(!isolate->has_pending_exception());
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000320 return false;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000321 }
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000322 unsigned table_offset = cgen.EmitBackEdgeTable();
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000323
lrn@chromium.org34e60782011-09-15 07:25:40 +0000324 Code::Flags flags = Code::ComputeFlags(Code::FUNCTION);
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000325 Handle<Code> code = CodeGenerator::MakeCodeEpilogue(&masm, flags, info);
rossberg@chromium.org2c067b12012-03-19 11:01:52 +0000326 code->set_optimizable(info->IsOptimizable() &&
ulan@chromium.orgd6899c32012-05-18 14:12:25 +0000327 !info->function()->flags()->Contains(kDontOptimize) &&
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000328 info->function()->scope()->AllowsLazyCompilation());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000329 cgen.PopulateDeoptimizationData(code);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000330 cgen.PopulateTypeFeedbackInfo(code);
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000331 cgen.PopulateTypeFeedbackCells(code);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000332 code->set_has_deoptimization_support(info->HasDeoptimizationSupport());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +0000333 code->set_handler_table(*cgen.handler_table());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000334#ifdef ENABLE_DEBUGGER_SUPPORT
lrn@chromium.org34e60782011-09-15 07:25:40 +0000335 code->set_has_debug_break_slots(
336 info->isolate()->debugger()->IsDebuggerActive());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000337 code->set_compiled_optimizable(info->IsOptimizable());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000338#endif // ENABLE_DEBUGGER_SUPPORT
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000339 code->set_allow_osr_at_loop_nesting_level(0);
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000340 code->set_profiler_ticks(0);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000341 code->set_back_edge_table_offset(table_offset);
342 code->set_back_edges_patched_for_osr(false);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000343 CodeGenerator::PrintCode(code, info);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000344 info->SetCode(code); // May be an empty handle.
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000345#ifdef ENABLE_GDB_JIT_INTERFACE
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000346 if (FLAG_gdbjit && !code.is_null()) {
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000347 GDBJITLineInfo* lineinfo =
348 masm.positions_recorder()->DetachGDBJITLineInfo();
349
350 GDBJIT(RegisterDetailedLineInfo(*code, lineinfo));
351 }
352#endif
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000353 if (!code.is_null()) {
354 void* line_info =
355 masm.positions_recorder()->DetachJITHandlerData();
356 LOG_CODE_EVENT(isolate, CodeEndLinePosInfoRecordEvent(*code, line_info));
357 }
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000358 return !code.is_null();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000359}
360
361
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000362unsigned FullCodeGenerator::EmitBackEdgeTable() {
363 // The back edge table consists of a length (in number of entries)
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000364 // field, and then a sequence of entries. Each entry is a pair of AST id
365 // and code-relative pc offset.
366 masm()->Align(kIntSize);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000367 unsigned offset = masm()->pc_offset();
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000368 unsigned length = back_edges_.length();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000369 __ dd(length);
370 for (unsigned i = 0; i < length; ++i) {
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000371 __ dd(back_edges_[i].id.ToInt());
372 __ dd(back_edges_[i].pc);
373 __ db(back_edges_[i].loop_depth);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000374 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000375 return offset;
376}
377
378
379void FullCodeGenerator::PopulateDeoptimizationData(Handle<Code> code) {
380 // Fill in the deoptimization information.
381 ASSERT(info_->HasDeoptimizationSupport() || bailout_entries_.is_empty());
382 if (!info_->HasDeoptimizationSupport()) return;
383 int length = bailout_entries_.length();
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000384 Handle<DeoptimizationOutputData> data = isolate()->factory()->
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000385 NewDeoptimizationOutputData(length, TENURED);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000386 for (int i = 0; i < length; i++) {
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000387 data->SetAstId(i, bailout_entries_[i].id);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000388 data->SetPcAndState(i, Smi::FromInt(bailout_entries_[i].pc_and_state));
389 }
390 code->set_deoptimization_data(*data);
391}
392
393
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000394void FullCodeGenerator::PopulateTypeFeedbackInfo(Handle<Code> code) {
395 Handle<TypeFeedbackInfo> info = isolate()->factory()->NewTypeFeedbackInfo();
396 info->set_ic_total_count(ic_total_count_);
yangguo@chromium.orga7d3df92012-02-27 11:46:55 +0000397 ASSERT(!isolate()->heap()->InNewSpace(*info));
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000398 code->set_type_feedback_info(*info);
399}
400
401
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +0000402void FullCodeGenerator::Initialize() {
403 // The generation of debug code must match between the snapshot code and the
404 // code that is generated later. This is assumed by the debugger when it is
405 // calculating PC offsets after generating a debug version of code. Therefore
406 // we disable the production of debug code in the full compiler if we are
407 // either generating a snapshot or we booted from a snapshot.
408 generate_debug_code_ = FLAG_debug_code &&
409 !Serializer::enabled() &&
410 !Snapshot::HaveASnapshotToStartFrom();
411 masm_->set_emit_debug_code(generate_debug_code_);
412 masm_->set_predictable_code_size(true);
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000413 InitializeAstVisitor();
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +0000414}
415
416
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000417void FullCodeGenerator::PopulateTypeFeedbackCells(Handle<Code> code) {
418 if (type_feedback_cells_.is_empty()) return;
419 int length = type_feedback_cells_.length();
420 int array_size = TypeFeedbackCells::LengthOfFixedArray(length);
421 Handle<TypeFeedbackCells> cache = Handle<TypeFeedbackCells>::cast(
422 isolate()->factory()->NewFixedArray(array_size, TENURED));
423 for (int i = 0; i < length; i++) {
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000424 cache->SetAstId(i, type_feedback_cells_[i].ast_id);
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000425 cache->SetCell(i, *type_feedback_cells_[i].cell);
426 }
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000427 TypeFeedbackInfo::cast(code->type_feedback_info())->set_type_feedback_cells(
428 *cache);
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000429}
430
431
432
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000433void FullCodeGenerator::PrepareForBailout(Expression* node, State state) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000434 PrepareForBailoutForId(node->id(), state);
435}
436
437
438void FullCodeGenerator::RecordJSReturnSite(Call* call) {
439 // We record the offset of the function return so we can rebuild the frame
440 // if the function was inlined, i.e., this is the return address in the
441 // inlined function's frame.
442 //
443 // The state is ignored. We defensively set it to TOS_REG, which is the
444 // real state of the unoptimized code at the return site.
445 PrepareForBailoutForId(call->ReturnId(), TOS_REG);
446#ifdef DEBUG
447 // In debug builds, mark the return so we can verify that this function
448 // was called.
449 ASSERT(!call->return_is_recorded_);
450 call->return_is_recorded_ = true;
451#endif
452}
453
454
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000455void FullCodeGenerator::PrepareForBailoutForId(BailoutId id, State state) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000456 // There's no need to prepare this code for bailouts from already optimized
457 // code or code that can't be optimized.
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000458 if (!info_->HasDeoptimizationSupport()) return;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000459 unsigned pc_and_state =
460 StateField::encode(state) | PcField::encode(masm_->pc_offset());
yangguo@chromium.org56454712012-02-16 15:33:53 +0000461 ASSERT(Smi::IsValid(pc_and_state));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000462 BailoutEntry entry = { id, pc_and_state };
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000463 ASSERT(!prepared_bailout_ids_.Contains(id.ToInt()));
464 prepared_bailout_ids_.Add(id.ToInt(), zone());
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000465 bailout_entries_.Add(entry, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000466}
467
468
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000469void FullCodeGenerator::RecordTypeFeedbackCell(
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000470 TypeFeedbackId id, Handle<JSGlobalPropertyCell> cell) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000471 TypeFeedbackCellEntry entry = { id, cell };
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000472 type_feedback_cells_.Add(entry, zone());
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000473}
474
475
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +0000476void FullCodeGenerator::RecordBackEdge(BailoutId ast_id) {
477 // The pc offset does not need to be encoded and packed together with a state.
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +0000478 ASSERT(masm_->pc_offset() > 0);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000479 ASSERT(loop_depth() > 0);
480 uint8_t depth = Min(loop_depth(), Code::kMaxLoopNestingMarker);
481 BackEdgeEntry entry =
482 { ast_id, static_cast<unsigned>(masm_->pc_offset()), depth };
483 back_edges_.Add(entry, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000484}
485
486
ricow@chromium.org65fae842010-08-25 15:26:24 +0000487bool FullCodeGenerator::ShouldInlineSmiCase(Token::Value op) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000488 // Inline smi case inside loops, but not division and modulo which
489 // are too complicated and take up too much space.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000490 if (op == Token::DIV ||op == Token::MOD) return false;
491 if (FLAG_always_inline_smi_code) return true;
492 return loop_depth_ > 0;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000493}
494
495
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000496void FullCodeGenerator::EffectContext::Plug(Register reg) const {
497}
498
499
500void FullCodeGenerator::AccumulatorValueContext::Plug(Register reg) const {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000501 __ Move(result_register(), reg);
502}
503
504
505void FullCodeGenerator::StackValueContext::Plug(Register reg) const {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000506 __ push(reg);
507}
508
509
510void FullCodeGenerator::TestContext::Plug(Register reg) const {
511 // For simplicity we always test the accumulator register.
512 __ Move(result_register(), reg);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000513 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000514 codegen()->DoTest(this);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000515}
516
517
518void FullCodeGenerator::EffectContext::PlugTOS() const {
519 __ Drop(1);
520}
521
522
523void FullCodeGenerator::AccumulatorValueContext::PlugTOS() const {
524 __ pop(result_register());
525}
526
527
528void FullCodeGenerator::StackValueContext::PlugTOS() const {
529}
530
531
532void FullCodeGenerator::TestContext::PlugTOS() const {
533 // For simplicity we always test the accumulator register.
534 __ pop(result_register());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000535 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000536 codegen()->DoTest(this);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000537}
538
539
540void FullCodeGenerator::EffectContext::PrepareTest(
541 Label* materialize_true,
542 Label* materialize_false,
543 Label** if_true,
544 Label** if_false,
545 Label** fall_through) const {
546 // In an effect context, the true and the false case branch to the
547 // same label.
548 *if_true = *if_false = *fall_through = materialize_true;
549}
550
551
552void FullCodeGenerator::AccumulatorValueContext::PrepareTest(
553 Label* materialize_true,
554 Label* materialize_false,
555 Label** if_true,
556 Label** if_false,
557 Label** fall_through) const {
558 *if_true = *fall_through = materialize_true;
559 *if_false = materialize_false;
560}
561
562
563void FullCodeGenerator::StackValueContext::PrepareTest(
564 Label* materialize_true,
565 Label* materialize_false,
566 Label** if_true,
567 Label** if_false,
568 Label** fall_through) const {
569 *if_true = *fall_through = materialize_true;
570 *if_false = materialize_false;
571}
572
573
574void FullCodeGenerator::TestContext::PrepareTest(
575 Label* materialize_true,
576 Label* materialize_false,
577 Label** if_true,
578 Label** if_false,
579 Label** fall_through) const {
580 *if_true = true_label_;
581 *if_false = false_label_;
582 *fall_through = fall_through_;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000583}
584
585
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000586void FullCodeGenerator::DoTest(const TestContext* context) {
587 DoTest(context->condition(),
588 context->true_label(),
589 context->false_label(),
590 context->fall_through());
591}
592
593
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000594void FullCodeGenerator::AllocateModules(ZoneList<Declaration*>* declarations) {
595 ASSERT(scope_->is_global_scope());
596
597 for (int i = 0; i < declarations->length(); i++) {
598 ModuleDeclaration* declaration = declarations->at(i)->AsModuleDeclaration();
599 if (declaration != NULL) {
600 ModuleLiteral* module = declaration->module()->AsModuleLiteral();
601 if (module != NULL) {
602 Comment cmnt(masm_, "[ Link nested modules");
603 Scope* scope = module->body()->scope();
604 Interface* interface = scope->interface();
605 ASSERT(interface->IsModule() && interface->IsFrozen());
606
607 interface->Allocate(scope->module_var()->index());
608
609 // Set up module context.
610 ASSERT(scope->interface()->Index() >= 0);
611 __ Push(Smi::FromInt(scope->interface()->Index()));
612 __ Push(scope->GetScopeInfo());
613 __ CallRuntime(Runtime::kPushModuleContext, 2);
614 StoreToFrameField(StandardFrameConstants::kContextOffset,
615 context_register());
616
617 AllocateModules(scope->declarations());
618
619 // Pop module context.
620 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
621 // Update local stack frame context field.
622 StoreToFrameField(StandardFrameConstants::kContextOffset,
623 context_register());
624 }
625 }
626 }
627}
628
629
630// Modules have their own local scope, represented by their own context.
631// Module instance objects have an accessor for every export that forwards
632// access to the respective slot from the module's context. (Exports that are
633// modules themselves, however, are simple data properties.)
634//
635// All modules have a _hosting_ scope/context, which (currently) is the
636// (innermost) enclosing global scope. To deal with recursion, nested modules
637// are hosted by the same scope as global ones.
638//
639// For every (global or nested) module literal, the hosting context has an
640// internal slot that points directly to the respective module context. This
641// enables quick access to (statically resolved) module members by 2-dimensional
642// access through the hosting context. For example,
643//
644// module A {
645// let x;
646// module B { let y; }
647// }
648// module C { let z; }
649//
650// allocates contexts as follows:
651//
652// [header| .A | .B | .C | A | C ] (global)
653// | | |
654// | | +-- [header| z ] (module)
655// | |
656// | +------- [header| y ] (module)
657// |
658// +------------ [header| x | B ] (module)
659//
660// Here, .A, .B, .C are the internal slots pointing to the hosted module
661// contexts, whereas A, B, C hold the actual instance objects (note that every
662// module context also points to the respective instance object through its
663// extension slot in the header).
664//
665// To deal with arbitrary recursion and aliases between modules,
666// they are created and initialized in several stages. Each stage applies to
667// all modules in the hosting global scope, including nested ones.
668//
669// 1. Allocate: for each module _literal_, allocate the module contexts and
670// respective instance object and wire them up. This happens in the
671// PushModuleContext runtime function, as generated by AllocateModules
672// (invoked by VisitDeclarations in the hosting scope).
673//
674// 2. Bind: for each module _declaration_ (i.e. literals as well as aliases),
675// assign the respective instance object to respective local variables. This
676// happens in VisitModuleDeclaration, and uses the instance objects created
677// in the previous stage.
678// For each module _literal_, this phase also constructs a module descriptor
679// for the next stage. This happens in VisitModuleLiteral.
680//
681// 3. Populate: invoke the DeclareModules runtime function to populate each
682// _instance_ object with accessors for it exports. This is generated by
683// DeclareModules (invoked by VisitDeclarations in the hosting scope again),
684// and uses the descriptors generated in the previous stage.
685//
686// 4. Initialize: execute the module bodies (and other code) in sequence. This
687// happens by the separate statements generated for module bodies. To reenter
688// the module scopes properly, the parser inserted ModuleStatements.
689
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000690void FullCodeGenerator::VisitDeclarations(
691 ZoneList<Declaration*>* declarations) {
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000692 Handle<FixedArray> saved_modules = modules_;
693 int saved_module_index = module_index_;
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000694 ZoneList<Handle<Object> >* saved_globals = globals_;
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000695 ZoneList<Handle<Object> > inner_globals(10, zone());
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000696 globals_ = &inner_globals;
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000697
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000698 if (scope_->num_modules() != 0) {
699 // This is a scope hosting modules. Allocate a descriptor array to pass
700 // to the runtime for initialization.
701 Comment cmnt(masm_, "[ Allocate modules");
702 ASSERT(scope_->is_global_scope());
703 modules_ =
704 isolate()->factory()->NewFixedArray(scope_->num_modules(), TENURED);
705 module_index_ = 0;
706
707 // Generate code for allocating all modules, including nested ones.
708 // The allocated contexts are stored in internal variables in this scope.
709 AllocateModules(declarations);
710 }
711
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000712 AstVisitor::VisitDeclarations(declarations);
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000713
714 if (scope_->num_modules() != 0) {
715 // Initialize modules from descriptor array.
716 ASSERT(module_index_ == modules_->length());
717 DeclareModules(modules_);
718 modules_ = saved_modules;
719 module_index_ = saved_module_index;
720 }
721
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000722 if (!globals_->is_empty()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000723 // Invoke the platform-dependent code generator to do the actual
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000724 // declaration of the global functions and variables.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000725 Handle<FixedArray> array =
726 isolate()->factory()->NewFixedArray(globals_->length(), TENURED);
727 for (int i = 0; i < globals_->length(); ++i)
728 array->set(i, *globals_->at(i));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000729 DeclareGlobals(array);
730 }
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000731
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000732 globals_ = saved_globals;
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000733}
734
735
736void FullCodeGenerator::VisitModuleLiteral(ModuleLiteral* module) {
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000737 Block* block = module->body();
738 Scope* saved_scope = scope();
739 scope_ = block->scope();
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000740 Interface* interface = scope_->interface();
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000741
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000742 Comment cmnt(masm_, "[ ModuleLiteral");
743 SetStatementPosition(block);
744
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000745 ASSERT(!modules_.is_null());
746 ASSERT(module_index_ < modules_->length());
747 int index = module_index_++;
748
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000749 // Set up module context.
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000750 ASSERT(interface->Index() >= 0);
751 __ Push(Smi::FromInt(interface->Index()));
752 __ Push(Smi::FromInt(0));
753 __ CallRuntime(Runtime::kPushModuleContext, 2);
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000754 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000755
756 {
757 Comment cmnt(masm_, "[ Declarations");
758 VisitDeclarations(scope_->declarations());
759 }
760
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000761 // Populate the module description.
762 Handle<ModuleInfo> description =
763 ModuleInfo::Create(isolate(), interface, scope_);
764 modules_->set(index, *description);
765
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000766 scope_ = saved_scope;
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000767 // Pop module context.
768 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
769 // Update local stack frame context field.
770 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000771}
772
773
774void FullCodeGenerator::VisitModuleVariable(ModuleVariable* module) {
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000775 // Nothing to do.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000776 // The instance object is resolved statically through the module's interface.
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000777}
778
779
780void FullCodeGenerator::VisitModulePath(ModulePath* module) {
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000781 // Nothing to do.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000782 // The instance object is resolved statically through the module's interface.
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000783}
784
785
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000786void FullCodeGenerator::VisitModuleUrl(ModuleUrl* module) {
787 // TODO(rossberg): dummy allocation for now.
788 Scope* scope = module->body()->scope();
789 Interface* interface = scope_->interface();
790
791 ASSERT(interface->IsModule() && interface->IsFrozen());
792 ASSERT(!modules_.is_null());
793 ASSERT(module_index_ < modules_->length());
794 interface->Allocate(scope->module_var()->index());
795 int index = module_index_++;
796
797 Handle<ModuleInfo> description =
798 ModuleInfo::Create(isolate(), interface, scope_);
799 modules_->set(index, *description);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000800}
801
802
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000803int FullCodeGenerator::DeclareGlobalsFlags() {
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000804 ASSERT(DeclareGlobalsLanguageMode::is_valid(language_mode()));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000805 return DeclareGlobalsEvalFlag::encode(is_eval()) |
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000806 DeclareGlobalsNativeFlag::encode(is_native()) |
807 DeclareGlobalsLanguageMode::encode(language_mode());
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000808}
809
810
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000811void FullCodeGenerator::SetFunctionPosition(FunctionLiteral* fun) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000812 CodeGenerator::RecordPositions(masm_, fun->start_position());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000813}
814
815
816void FullCodeGenerator::SetReturnPosition(FunctionLiteral* fun) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000817 CodeGenerator::RecordPositions(masm_, fun->end_position() - 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000818}
819
820
821void FullCodeGenerator::SetStatementPosition(Statement* stmt) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000822#ifdef ENABLE_DEBUGGER_SUPPORT
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000823 if (!isolate()->debugger()->IsDebuggerActive()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000824 CodeGenerator::RecordPositions(masm_, stmt->statement_pos());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000825 } else {
826 // Check if the statement will be breakable without adding a debug break
827 // slot.
828 BreakableStatementChecker checker;
829 checker.Check(stmt);
830 // Record the statement position right here if the statement is not
831 // breakable. For breakable statements the actual recording of the
832 // position will be postponed to the breakable code (typically an IC).
833 bool position_recorded = CodeGenerator::RecordPositions(
834 masm_, stmt->statement_pos(), !checker.is_breakable());
835 // If the position recording did record a new position generate a debug
836 // break slot to make the statement breakable.
837 if (position_recorded) {
838 Debug::GenerateSlot(masm_);
839 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000840 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000841#else
842 CodeGenerator::RecordPositions(masm_, stmt->statement_pos());
843#endif
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000844}
845
846
847void FullCodeGenerator::SetExpressionPosition(Expression* expr, int pos) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000848#ifdef ENABLE_DEBUGGER_SUPPORT
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000849 if (!isolate()->debugger()->IsDebuggerActive()) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000850 CodeGenerator::RecordPositions(masm_, pos);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000851 } else {
852 // Check if the expression will be breakable without adding a debug break
853 // slot.
854 BreakableStatementChecker checker;
855 checker.Check(expr);
856 // Record a statement position right here if the expression is not
857 // breakable. For breakable expressions the actual recording of the
858 // position will be postponed to the breakable code (typically an IC).
859 // NOTE this will record a statement position for something which might
860 // not be a statement. As stepping in the debugger will only stop at
861 // statement positions this is used for e.g. the condition expression of
862 // a do while loop.
863 bool position_recorded = CodeGenerator::RecordPositions(
864 masm_, pos, !checker.is_breakable());
865 // If the position recording did record a new position generate a debug
866 // break slot to make the statement breakable.
867 if (position_recorded) {
868 Debug::GenerateSlot(masm_);
869 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000870 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000871#else
872 CodeGenerator::RecordPositions(masm_, pos);
873#endif
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000874}
875
876
877void FullCodeGenerator::SetStatementPosition(int pos) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000878 CodeGenerator::RecordPositions(masm_, pos);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000879}
880
881
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000882void FullCodeGenerator::SetSourcePosition(int pos) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000883 if (pos != RelocInfo::kNoPosition) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000884 masm_->positions_recorder()->RecordPosition(pos);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000885 }
886}
887
888
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000889// Lookup table for code generators for special runtime calls which are
890// generated inline.
891#define INLINE_FUNCTION_GENERATOR_ADDRESS(Name, argc, ressize) \
892 &FullCodeGenerator::Emit##Name,
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000893
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000894const FullCodeGenerator::InlineFunctionGenerator
895 FullCodeGenerator::kInlineFunctionGenerators[] = {
896 INLINE_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS)
897 INLINE_RUNTIME_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS)
898 };
899#undef INLINE_FUNCTION_GENERATOR_ADDRESS
900
901
902FullCodeGenerator::InlineFunctionGenerator
903 FullCodeGenerator::FindInlineFunctionGenerator(Runtime::FunctionId id) {
whesse@chromium.org023421e2010-12-21 12:19:12 +0000904 int lookup_index =
905 static_cast<int>(id) - static_cast<int>(Runtime::kFirstInlineFunction);
906 ASSERT(lookup_index >= 0);
907 ASSERT(static_cast<size_t>(lookup_index) <
908 ARRAY_SIZE(kInlineFunctionGenerators));
909 return kInlineFunctionGenerators[lookup_index];
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000910}
911
912
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000913void FullCodeGenerator::EmitInlineRuntimeCall(CallRuntime* expr) {
914 const Runtime::Function* function = expr->function();
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000915 ASSERT(function != NULL);
916 ASSERT(function->intrinsic_type == Runtime::INLINE);
917 InlineFunctionGenerator generator =
918 FindInlineFunctionGenerator(function->function_id);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000919 ((*this).*(generator))(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000920}
921
922
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000923void FullCodeGenerator::EmitGeneratorSend(CallRuntime* expr) {
924 ZoneList<Expression*>* args = expr->arguments();
925 ASSERT(args->length() == 2);
926 EmitGeneratorResume(args->at(0), args->at(1), JSGeneratorObject::SEND);
927}
928
929
930void FullCodeGenerator::EmitGeneratorThrow(CallRuntime* expr) {
931 ZoneList<Expression*>* args = expr->arguments();
932 ASSERT(args->length() == 2);
933 EmitGeneratorResume(args->at(0), args->at(1), JSGeneratorObject::THROW);
934}
935
936
ricow@chromium.org65fae842010-08-25 15:26:24 +0000937void FullCodeGenerator::VisitBinaryOperation(BinaryOperation* expr) {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000938 switch (expr->op()) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000939 case Token::COMMA:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000940 return VisitComma(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000941 case Token::OR:
942 case Token::AND:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000943 return VisitLogicalExpression(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000944 default:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000945 return VisitArithmeticExpression(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000946 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000947}
948
949
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000950void FullCodeGenerator::VisitInDuplicateContext(Expression* expr) {
951 if (context()->IsEffect()) {
952 VisitForEffect(expr);
953 } else if (context()->IsAccumulatorValue()) {
954 VisitForAccumulatorValue(expr);
955 } else if (context()->IsStackValue()) {
956 VisitForStackValue(expr);
957 } else if (context()->IsTest()) {
958 const TestContext* test = TestContext::cast(context());
959 VisitForControl(expr, test->true_label(), test->false_label(),
960 test->fall_through());
961 }
962}
963
964
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000965void FullCodeGenerator::VisitComma(BinaryOperation* expr) {
966 Comment cmnt(masm_, "[ Comma");
967 VisitForEffect(expr->left());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000968 VisitInDuplicateContext(expr->right());
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000969}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000970
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000971
972void FullCodeGenerator::VisitLogicalExpression(BinaryOperation* expr) {
973 bool is_logical_and = expr->op() == Token::AND;
974 Comment cmnt(masm_, is_logical_and ? "[ Logical AND" : "[ Logical OR");
975 Expression* left = expr->left();
976 Expression* right = expr->right();
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000977 BailoutId right_id = expr->RightId();
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000978 Label done;
979
980 if (context()->IsTest()) {
981 Label eval_right;
982 const TestContext* test = TestContext::cast(context());
983 if (is_logical_and) {
984 VisitForControl(left, &eval_right, test->false_label(), &eval_right);
985 } else {
986 VisitForControl(left, test->true_label(), &eval_right, &eval_right);
987 }
988 PrepareForBailoutForId(right_id, NO_REGISTERS);
989 __ bind(&eval_right);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000990
991 } else if (context()->IsAccumulatorValue()) {
992 VisitForAccumulatorValue(left);
993 // We want the value in the accumulator for the test, and on the stack in
994 // case we need it.
995 __ push(result_register());
996 Label discard, restore;
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000997 if (is_logical_and) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000998 DoTest(left, &discard, &restore, &restore);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000999 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001000 DoTest(left, &restore, &discard, &restore);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001001 }
1002 __ bind(&restore);
1003 __ pop(result_register());
1004 __ jmp(&done);
1005 __ bind(&discard);
1006 __ Drop(1);
1007 PrepareForBailoutForId(right_id, NO_REGISTERS);
1008
1009 } else if (context()->IsStackValue()) {
1010 VisitForAccumulatorValue(left);
1011 // We want the value in the accumulator for the test, and on the stack in
1012 // case we need it.
1013 __ push(result_register());
1014 Label discard;
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001015 if (is_logical_and) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001016 DoTest(left, &discard, &done, &discard);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001017 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001018 DoTest(left, &done, &discard, &discard);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001019 }
1020 __ bind(&discard);
1021 __ Drop(1);
1022 PrepareForBailoutForId(right_id, NO_REGISTERS);
1023
1024 } else {
1025 ASSERT(context()->IsEffect());
1026 Label eval_right;
1027 if (is_logical_and) {
1028 VisitForControl(left, &eval_right, &done, &eval_right);
1029 } else {
1030 VisitForControl(left, &done, &eval_right, &eval_right);
1031 }
1032 PrepareForBailoutForId(right_id, NO_REGISTERS);
1033 __ bind(&eval_right);
1034 }
1035
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001036 VisitInDuplicateContext(right);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001037 __ bind(&done);
1038}
1039
1040
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001041void FullCodeGenerator::VisitArithmeticExpression(BinaryOperation* expr) {
1042 Token::Value op = expr->op();
1043 Comment cmnt(masm_, "[ ArithmeticExpression");
1044 Expression* left = expr->left();
1045 Expression* right = expr->right();
1046 OverwriteMode mode =
1047 left->ResultOverwriteAllowed()
1048 ? OVERWRITE_LEFT
1049 : (right->ResultOverwriteAllowed() ? OVERWRITE_RIGHT : NO_OVERWRITE);
1050
1051 VisitForStackValue(left);
1052 VisitForAccumulatorValue(right);
1053
1054 SetSourcePosition(expr->position());
1055 if (ShouldInlineSmiCase(op)) {
1056 EmitInlineSmiBinaryOp(expr, op, mode, left, right);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001057 } else {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001058 EmitBinaryOp(expr, op, mode);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001059 }
1060}
1061
1062
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001063void FullCodeGenerator::VisitBlock(Block* stmt) {
1064 Comment cmnt(masm_, "[ Block");
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001065 NestedBlock nested_block(this, stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001066 SetStatementPosition(stmt);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001067
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001068 Scope* saved_scope = scope();
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001069 // Push a block context when entering a block with block scoped variables.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00001070 if (stmt->scope() != NULL) {
danno@chromium.org81cac2b2012-07-10 11:28:27 +00001071 scope_ = stmt->scope();
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00001072 ASSERT(!scope_->is_module_scope());
1073 { Comment cmnt(masm_, "[ Extend block context");
1074 Handle<ScopeInfo> scope_info = scope_->GetScopeInfo();
1075 int heap_slots = scope_info->ContextLength() - Context::MIN_CONTEXT_SLOTS;
1076 __ Push(scope_info);
1077 PushFunctionArgumentForContextAllocation();
1078 if (heap_slots <= FastNewBlockContextStub::kMaximumSlots) {
1079 FastNewBlockContextStub stub(heap_slots);
1080 __ CallStub(&stub);
1081 } else {
1082 __ CallRuntime(Runtime::kPushBlockContext, 2);
1083 }
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001084
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00001085 // Replace the context stored in the frame.
1086 StoreToFrameField(StandardFrameConstants::kContextOffset,
1087 context_register());
1088 }
1089 { Comment cmnt(masm_, "[ Declarations");
1090 VisitDeclarations(scope_->declarations());
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001091 }
1092 }
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00001093
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00001094 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001095 VisitStatements(stmt->statements());
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001096 scope_ = saved_scope;
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001097 __ bind(nested_block.break_label());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001098 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001099
1100 // Pop block context if necessary.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00001101 if (stmt->scope() != NULL) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001102 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1103 // Update local stack frame context field.
1104 StoreToFrameField(StandardFrameConstants::kContextOffset,
1105 context_register());
1106 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001107}
1108
1109
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00001110void FullCodeGenerator::VisitModuleStatement(ModuleStatement* stmt) {
1111 Comment cmnt(masm_, "[ Module context");
1112
1113 __ Push(Smi::FromInt(stmt->proxy()->interface()->Index()));
1114 __ Push(Smi::FromInt(0));
1115 __ CallRuntime(Runtime::kPushModuleContext, 2);
1116 StoreToFrameField(
1117 StandardFrameConstants::kContextOffset, context_register());
1118
1119 Scope* saved_scope = scope_;
1120 scope_ = stmt->body()->scope();
1121 VisitStatements(stmt->body()->statements());
1122 scope_ = saved_scope;
1123 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1124 // Update local stack frame context field.
1125 StoreToFrameField(StandardFrameConstants::kContextOffset,
1126 context_register());
1127}
1128
1129
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001130void FullCodeGenerator::VisitExpressionStatement(ExpressionStatement* stmt) {
1131 Comment cmnt(masm_, "[ ExpressionStatement");
1132 SetStatementPosition(stmt);
1133 VisitForEffect(stmt->expression());
1134}
1135
1136
1137void FullCodeGenerator::VisitEmptyStatement(EmptyStatement* stmt) {
1138 Comment cmnt(masm_, "[ EmptyStatement");
1139 SetStatementPosition(stmt);
1140}
1141
1142
1143void FullCodeGenerator::VisitIfStatement(IfStatement* stmt) {
1144 Comment cmnt(masm_, "[ IfStatement");
1145 SetStatementPosition(stmt);
1146 Label then_part, else_part, done;
1147
ricow@chromium.org65fae842010-08-25 15:26:24 +00001148 if (stmt->HasElseStatement()) {
1149 VisitForControl(stmt->condition(), &then_part, &else_part, &then_part);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001150 PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001151 __ bind(&then_part);
1152 Visit(stmt->then_statement());
1153 __ jmp(&done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001154
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001155 PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001156 __ bind(&else_part);
1157 Visit(stmt->else_statement());
1158 } else {
1159 VisitForControl(stmt->condition(), &then_part, &done, &then_part);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001160 PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001161 __ bind(&then_part);
1162 Visit(stmt->then_statement());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001163
1164 PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001165 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001166 __ bind(&done);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001167 PrepareForBailoutForId(stmt->IfId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001168}
1169
1170
1171void FullCodeGenerator::VisitContinueStatement(ContinueStatement* stmt) {
1172 Comment cmnt(masm_, "[ ContinueStatement");
1173 SetStatementPosition(stmt);
1174 NestedStatement* current = nesting_stack_;
1175 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001176 int context_length = 0;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001177 // When continuing, we clobber the unpredictable value in the accumulator
1178 // with one that's safe for GC. If we hit an exit from the try block of
1179 // try...finally on our way out, we will unconditionally preserve the
1180 // accumulator on the stack.
1181 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001182 while (!current->IsContinueTarget(stmt->target())) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001183 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001184 }
1185 __ Drop(stack_depth);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001186 if (context_length > 0) {
1187 while (context_length > 0) {
1188 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1189 --context_length;
1190 }
1191 StoreToFrameField(StandardFrameConstants::kContextOffset,
1192 context_register());
1193 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001194
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001195 __ jmp(current->AsIteration()->continue_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001196}
1197
1198
1199void FullCodeGenerator::VisitBreakStatement(BreakStatement* stmt) {
1200 Comment cmnt(masm_, "[ BreakStatement");
1201 SetStatementPosition(stmt);
1202 NestedStatement* current = nesting_stack_;
1203 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001204 int context_length = 0;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001205 // When breaking, we clobber the unpredictable value in the accumulator
1206 // with one that's safe for GC. If we hit an exit from the try block of
1207 // try...finally on our way out, we will unconditionally preserve the
1208 // accumulator on the stack.
1209 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001210 while (!current->IsBreakTarget(stmt->target())) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001211 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001212 }
1213 __ Drop(stack_depth);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001214 if (context_length > 0) {
1215 while (context_length > 0) {
1216 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1217 --context_length;
1218 }
1219 StoreToFrameField(StandardFrameConstants::kContextOffset,
1220 context_register());
1221 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001222
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001223 __ jmp(current->AsBreakable()->break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001224}
1225
1226
1227void FullCodeGenerator::VisitReturnStatement(ReturnStatement* stmt) {
1228 Comment cmnt(masm_, "[ ReturnStatement");
1229 SetStatementPosition(stmt);
1230 Expression* expr = stmt->expression();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001231 VisitForAccumulatorValue(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001232
1233 // Exit all nested statements.
1234 NestedStatement* current = nesting_stack_;
1235 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001236 int context_length = 0;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001237 while (current != NULL) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001238 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001239 }
1240 __ Drop(stack_depth);
1241
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001242 EmitReturnSequence();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001243}
1244
1245
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001246void FullCodeGenerator::VisitWithStatement(WithStatement* stmt) {
1247 Comment cmnt(masm_, "[ WithStatement");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001248 SetStatementPosition(stmt);
1249
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001250 VisitForStackValue(stmt->expression());
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00001251 PushFunctionArgumentForContextAllocation();
1252 __ CallRuntime(Runtime::kPushWithContext, 2);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001253 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001254
danno@chromium.orgca29dd82013-04-26 11:59:48 +00001255 Scope* saved_scope = scope();
1256 scope_ = stmt->scope();
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001257 { WithOrCatch body(this);
1258 Visit(stmt->statement());
1259 }
danno@chromium.orgca29dd82013-04-26 11:59:48 +00001260 scope_ = saved_scope;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001261
1262 // Pop context.
1263 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1264 // Update local stack frame context field.
1265 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001266}
1267
1268
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001269void FullCodeGenerator::VisitDoWhileStatement(DoWhileStatement* stmt) {
1270 Comment cmnt(masm_, "[ DoWhileStatement");
1271 SetStatementPosition(stmt);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001272 Label body, book_keeping;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001273
1274 Iteration loop_statement(this, stmt);
1275 increment_loop_depth();
1276
1277 __ bind(&body);
1278 Visit(stmt->body());
1279
ricow@chromium.org65fae842010-08-25 15:26:24 +00001280 // Record the position of the do while condition and make sure it is
1281 // possible to break on the condition.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001282 __ bind(loop_statement.continue_label());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001283 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001284 SetExpressionPosition(stmt->cond(), stmt->condition_position());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001285 VisitForControl(stmt->cond(),
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001286 &book_keeping,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001287 loop_statement.break_label(),
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001288 &book_keeping);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001289
1290 // Check stack before looping.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001291 PrepareForBailoutForId(stmt->BackEdgeId(), NO_REGISTERS);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001292 __ bind(&book_keeping);
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +00001293 EmitBackEdgeBookkeeping(stmt, &body);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001294 __ jmp(&body);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001295
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001296 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001297 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001298 decrement_loop_depth();
1299}
1300
1301
1302void FullCodeGenerator::VisitWhileStatement(WhileStatement* stmt) {
1303 Comment cmnt(masm_, "[ WhileStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001304 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001305
1306 Iteration loop_statement(this, stmt);
1307 increment_loop_depth();
1308
1309 // Emit the test at the bottom of the loop.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001310 __ jmp(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001311
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001312 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001313 __ bind(&body);
1314 Visit(stmt->body());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001315
1316 // Emit the statement position here as this is where the while
1317 // statement code starts.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001318 __ bind(loop_statement.continue_label());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001319 SetStatementPosition(stmt);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001320
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001321 // Check stack before looping.
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +00001322 EmitBackEdgeBookkeeping(stmt, &body);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001323
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001324 __ bind(&test);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001325 VisitForControl(stmt->cond(),
1326 &body,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001327 loop_statement.break_label(),
1328 loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001329
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001330 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001331 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001332 decrement_loop_depth();
1333}
1334
1335
1336void FullCodeGenerator::VisitForStatement(ForStatement* stmt) {
1337 Comment cmnt(masm_, "[ ForStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001338 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001339
1340 Iteration loop_statement(this, stmt);
erik.corry@gmail.combbceb572012-03-09 10:52:05 +00001341
1342 // Set statement position for a break slot before entering the for-body.
1343 SetStatementPosition(stmt);
1344
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001345 if (stmt->init() != NULL) {
1346 Visit(stmt->init());
1347 }
1348
1349 increment_loop_depth();
1350 // Emit the test at the bottom of the loop (even if empty).
1351 __ jmp(&test);
1352
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001353 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001354 __ bind(&body);
1355 Visit(stmt->body());
1356
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001357 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001358 __ bind(loop_statement.continue_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001359 if (stmt->next() != NULL) {
1360 Visit(stmt->next());
1361 }
1362
ricow@chromium.org65fae842010-08-25 15:26:24 +00001363 // Emit the statement position here as this is where the for
1364 // statement code starts.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001365 SetStatementPosition(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001366
1367 // Check stack before looping.
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +00001368 EmitBackEdgeBookkeeping(stmt, &body);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001369
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001370 __ bind(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001371 if (stmt->cond() != NULL) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00001372 VisitForControl(stmt->cond(),
1373 &body,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001374 loop_statement.break_label(),
1375 loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001376 } else {
1377 __ jmp(&body);
1378 }
1379
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001380 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001381 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001382 decrement_loop_depth();
1383}
1384
1385
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001386void FullCodeGenerator::VisitTryCatchStatement(TryCatchStatement* stmt) {
1387 Comment cmnt(masm_, "[ TryCatchStatement");
1388 SetStatementPosition(stmt);
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001389 // The try block adds a handler to the exception handler chain before
1390 // entering, and removes it again when exiting normally. If an exception
1391 // is thrown during execution of the try block, the handler is consumed
1392 // and control is passed to the catch block with the exception in the
1393 // result register.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001394
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001395 Label try_entry, handler_entry, exit;
1396 __ jmp(&try_entry);
1397 __ bind(&handler_entry);
1398 handler_table()->set(stmt->index(), Smi::FromInt(handler_entry.pos()));
1399 // Exception handler code, the exception is in the result register.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001400 // Extend the context before executing the catch block.
1401 { Comment cmnt(masm_, "[ Extend catch context");
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001402 __ Push(stmt->variable()->name());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001403 __ push(result_register());
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00001404 PushFunctionArgumentForContextAllocation();
1405 __ CallRuntime(Runtime::kPushCatchContext, 3);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001406 StoreToFrameField(StandardFrameConstants::kContextOffset,
1407 context_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001408 }
1409
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001410 Scope* saved_scope = scope();
1411 scope_ = stmt->scope();
1412 ASSERT(scope_->declarations()->is_empty());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001413 { WithOrCatch catch_body(this);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001414 Visit(stmt->catch_block());
1415 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001416 // Restore the context.
1417 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1418 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001419 scope_ = saved_scope;
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001420 __ jmp(&exit);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001421
1422 // Try block code. Sets up the exception handler chain.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001423 __ bind(&try_entry);
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00001424 __ PushTryHandler(StackHandler::CATCH, stmt->index());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001425 { TryCatch try_body(this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001426 Visit(stmt->try_block());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001427 }
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001428 __ PopTryHandler();
1429 __ bind(&exit);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001430}
1431
1432
1433void FullCodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* stmt) {
1434 Comment cmnt(masm_, "[ TryFinallyStatement");
1435 SetStatementPosition(stmt);
1436 // Try finally is compiled by setting up a try-handler on the stack while
1437 // executing the try body, and removing it again afterwards.
1438 //
1439 // The try-finally construct can enter the finally block in three ways:
1440 // 1. By exiting the try-block normally. This removes the try-handler and
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001441 // calls the finally block code before continuing.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001442 // 2. By exiting the try-block with a function-local control flow transfer
1443 // (break/continue/return). The site of the, e.g., break removes the
1444 // try handler and calls the finally block code before continuing
1445 // its outward control transfer.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001446 // 3. By exiting the try-block with a thrown exception.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001447 // This can happen in nested function calls. It traverses the try-handler
1448 // chain and consumes the try-handler entry before jumping to the
1449 // handler code. The handler code then calls the finally-block before
1450 // rethrowing the exception.
1451 //
1452 // The finally block must assume a return address on top of the stack
1453 // (or in the link register on ARM chips) and a value (return value or
1454 // exception) in the result register (rax/eax/r0), both of which must
1455 // be preserved. The return address isn't GC-safe, so it should be
1456 // cooked before GC.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001457 Label try_entry, handler_entry, finally_entry;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001458
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001459 // Jump to try-handler setup and try-block code.
1460 __ jmp(&try_entry);
1461 __ bind(&handler_entry);
1462 handler_table()->set(stmt->index(), Smi::FromInt(handler_entry.pos()));
1463 // Exception handler code. This code is only executed when an exception
1464 // is thrown. The exception is in the result register, and must be
1465 // preserved by the finally block. Call the finally block and then
1466 // rethrow the exception if it returns.
1467 __ Call(&finally_entry);
1468 __ push(result_register());
1469 __ CallRuntime(Runtime::kReThrow, 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001470
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001471 // Finally block implementation.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001472 __ bind(&finally_entry);
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001473 EnterFinallyBlock();
1474 { Finally finally_body(this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001475 Visit(stmt->finally_block());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001476 }
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001477 ExitFinallyBlock(); // Return to the calling code.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001478
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001479 // Set up try handler.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001480 __ bind(&try_entry);
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00001481 __ PushTryHandler(StackHandler::FINALLY, stmt->index());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001482 { TryFinally try_body(this, &finally_entry);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001483 Visit(stmt->try_block());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001484 }
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001485 __ PopTryHandler();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001486 // Execute the finally block on the way out. Clobber the unpredictable
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001487 // value in the result register with one that's safe for GC because the
1488 // finally block will unconditionally preserve the result register on the
1489 // stack.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001490 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001491 __ Call(&finally_entry);
1492}
1493
1494
1495void FullCodeGenerator::VisitDebuggerStatement(DebuggerStatement* stmt) {
1496#ifdef ENABLE_DEBUGGER_SUPPORT
1497 Comment cmnt(masm_, "[ DebuggerStatement");
1498 SetStatementPosition(stmt);
1499
ager@chromium.org5c838252010-02-19 08:53:10 +00001500 __ DebugBreak();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001501 // Ignore the return value.
1502#endif
1503}
1504
1505
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001506void FullCodeGenerator::VisitConditional(Conditional* expr) {
1507 Comment cmnt(masm_, "[ Conditional");
1508 Label true_case, false_case, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001509 VisitForControl(expr->condition(), &true_case, &false_case, &true_case);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001510
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001511 PrepareForBailoutForId(expr->ThenId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001512 __ bind(&true_case);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001513 SetExpressionPosition(expr->then_expression(),
1514 expr->then_expression_position());
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001515 if (context()->IsTest()) {
1516 const TestContext* for_test = TestContext::cast(context());
1517 VisitForControl(expr->then_expression(),
1518 for_test->true_label(),
1519 for_test->false_label(),
1520 NULL);
1521 } else {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001522 VisitInDuplicateContext(expr->then_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001523 __ jmp(&done);
1524 }
1525
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001526 PrepareForBailoutForId(expr->ElseId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001527 __ bind(&false_case);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001528 SetExpressionPosition(expr->else_expression(),
1529 expr->else_expression_position());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001530 VisitInDuplicateContext(expr->else_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001531 // If control flow falls through Visit, merge it with true case here.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001532 if (!context()->IsTest()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001533 __ bind(&done);
1534 }
1535}
1536
1537
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001538void FullCodeGenerator::VisitLiteral(Literal* expr) {
1539 Comment cmnt(masm_, "[ Literal");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001540 context()->Plug(expr->handle());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001541}
1542
1543
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001544void FullCodeGenerator::VisitFunctionLiteral(FunctionLiteral* expr) {
1545 Comment cmnt(masm_, "[ FunctionLiteral");
1546
1547 // Build the function boilerplate and instantiate it.
1548 Handle<SharedFunctionInfo> function_info =
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001549 Compiler::BuildFunctionInfo(expr, script());
1550 if (function_info.is_null()) {
1551 SetStackOverflow();
1552 return;
1553 }
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001554 EmitNewClosure(function_info, expr->pretenure());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001555}
1556
1557
1558void FullCodeGenerator::VisitSharedFunctionInfoLiteral(
1559 SharedFunctionInfoLiteral* expr) {
1560 Comment cmnt(masm_, "[ SharedFunctionInfoLiteral");
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001561 EmitNewClosure(expr->shared_function_info(), false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001562}
1563
1564
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001565void FullCodeGenerator::VisitThrow(Throw* expr) {
1566 Comment cmnt(masm_, "[ Throw");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001567 VisitForStackValue(expr->exception());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001568 __ CallRuntime(Runtime::kThrow, 1);
1569 // Never returns here.
1570}
1571
1572
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001573FullCodeGenerator::NestedStatement* FullCodeGenerator::TryCatch::Exit(
1574 int* stack_depth,
1575 int* context_length) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001576 // The macros used here must preserve the result register.
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001577 __ Drop(*stack_depth);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001578 __ PopTryHandler();
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001579 *stack_depth = 0;
1580 return previous_;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001581}
1582
ricow@chromium.org65fae842010-08-25 15:26:24 +00001583
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001584bool FullCodeGenerator::TryLiteralCompare(CompareOperation* expr) {
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001585 Expression* sub_expr;
ager@chromium.org04921a82011-06-27 13:21:41 +00001586 Handle<String> check;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001587 if (expr->IsLiteralCompareTypeof(&sub_expr, &check)) {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001588 EmitLiteralCompareTypeof(expr, sub_expr, check);
ager@chromium.org04921a82011-06-27 13:21:41 +00001589 return true;
1590 }
1591
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001592 if (expr->IsLiteralCompareUndefined(&sub_expr)) {
1593 EmitLiteralCompareNil(expr, sub_expr, kUndefinedValue);
1594 return true;
1595 }
1596
1597 if (expr->IsLiteralCompareNull(&sub_expr)) {
1598 EmitLiteralCompareNil(expr, sub_expr, kNullValue);
ager@chromium.org04921a82011-06-27 13:21:41 +00001599 return true;
1600 }
1601
1602 return false;
1603}
1604
1605
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001606#undef __
1607
1608
1609} } // namespace v8::internal