blob: 8846107acf8e90db9e631ca24347f3d5c5d60d57 [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
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +000079
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +000080void BreakableStatementChecker::VisitModuleVariable(ModuleVariable* module) {
81}
82
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +000083
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +000084void BreakableStatementChecker::VisitModulePath(ModulePath* module) {
85}
86
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +000087
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +000088void BreakableStatementChecker::VisitModuleUrl(ModuleUrl* module) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +000089}
90
91
ulan@chromium.org8e8d8822012-11-23 14:36:46 +000092void BreakableStatementChecker::VisitModuleStatement(ModuleStatement* stmt) {
93}
94
95
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +000096void BreakableStatementChecker::VisitBlock(Block* stmt) {
97}
98
99
100void BreakableStatementChecker::VisitExpressionStatement(
101 ExpressionStatement* stmt) {
102 // Check if expression is breakable.
103 Visit(stmt->expression());
104}
105
106
107void BreakableStatementChecker::VisitEmptyStatement(EmptyStatement* stmt) {
108}
109
110
111void BreakableStatementChecker::VisitIfStatement(IfStatement* stmt) {
112 // If the condition is breakable the if statement is breakable.
113 Visit(stmt->condition());
114}
115
116
117void BreakableStatementChecker::VisitContinueStatement(
118 ContinueStatement* stmt) {
119}
120
121
122void BreakableStatementChecker::VisitBreakStatement(BreakStatement* stmt) {
123}
124
125
126void BreakableStatementChecker::VisitReturnStatement(ReturnStatement* stmt) {
127 // Return is breakable if the expression is.
128 Visit(stmt->expression());
129}
130
131
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000132void BreakableStatementChecker::VisitWithStatement(WithStatement* stmt) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000133 Visit(stmt->expression());
134}
135
136
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000137void BreakableStatementChecker::VisitSwitchStatement(SwitchStatement* stmt) {
138 // Switch statements breakable if the tag expression is.
139 Visit(stmt->tag());
140}
141
142
143void BreakableStatementChecker::VisitDoWhileStatement(DoWhileStatement* stmt) {
144 // Mark do while as breakable to avoid adding a break slot in front of it.
145 is_breakable_ = true;
146}
147
148
149void BreakableStatementChecker::VisitWhileStatement(WhileStatement* stmt) {
150 // Mark while statements breakable if the condition expression is.
151 Visit(stmt->cond());
152}
153
154
155void BreakableStatementChecker::VisitForStatement(ForStatement* stmt) {
156 // Mark for statements breakable if the condition expression is.
157 if (stmt->cond() != NULL) {
158 Visit(stmt->cond());
159 }
160}
161
162
163void BreakableStatementChecker::VisitForInStatement(ForInStatement* stmt) {
164 // Mark for in statements breakable if the enumerable expression is.
165 Visit(stmt->enumerable());
166}
167
168
danno@chromium.org1fd77d52013-06-07 16:01:45 +0000169void BreakableStatementChecker::VisitForOfStatement(ForOfStatement* stmt) {
170 // For-of is breakable because of the next() call.
171 is_breakable_ = true;
172}
173
174
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000175void BreakableStatementChecker::VisitTryCatchStatement(
176 TryCatchStatement* stmt) {
177 // Mark try catch as breakable to avoid adding a break slot in front of it.
178 is_breakable_ = true;
179}
180
181
182void BreakableStatementChecker::VisitTryFinallyStatement(
183 TryFinallyStatement* stmt) {
184 // Mark try finally as breakable to avoid adding a break slot in front of it.
185 is_breakable_ = true;
186}
187
188
189void BreakableStatementChecker::VisitDebuggerStatement(
190 DebuggerStatement* stmt) {
191 // The debugger statement is breakable.
192 is_breakable_ = true;
193}
194
195
196void BreakableStatementChecker::VisitFunctionLiteral(FunctionLiteral* expr) {
197}
198
199
200void BreakableStatementChecker::VisitSharedFunctionInfoLiteral(
201 SharedFunctionInfoLiteral* expr) {
202}
203
204
205void BreakableStatementChecker::VisitConditional(Conditional* expr) {
206}
207
208
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000209void BreakableStatementChecker::VisitVariableProxy(VariableProxy* expr) {
210}
211
212
213void BreakableStatementChecker::VisitLiteral(Literal* expr) {
214}
215
216
217void BreakableStatementChecker::VisitRegExpLiteral(RegExpLiteral* expr) {
218}
219
220
221void BreakableStatementChecker::VisitObjectLiteral(ObjectLiteral* expr) {
222}
223
224
225void BreakableStatementChecker::VisitArrayLiteral(ArrayLiteral* expr) {
226}
227
228
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000229void BreakableStatementChecker::VisitAssignment(Assignment* expr) {
230 // If assigning to a property (including a global property) the assignment is
231 // breakable.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000232 VariableProxy* proxy = expr->target()->AsVariableProxy();
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000233 Property* prop = expr->target()->AsProperty();
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000234 if (prop != NULL || (proxy != NULL && proxy->var()->IsUnallocated())) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000235 is_breakable_ = true;
236 return;
237 }
238
239 // Otherwise the assignment is breakable if the assigned value is.
240 Visit(expr->value());
241}
242
243
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000244void BreakableStatementChecker::VisitYield(Yield* expr) {
245 // Yield is breakable if the expression is.
246 Visit(expr->expression());
247}
248
249
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000250void BreakableStatementChecker::VisitThrow(Throw* expr) {
251 // Throw is breakable if the expression is.
252 Visit(expr->exception());
253}
254
255
256void BreakableStatementChecker::VisitProperty(Property* expr) {
257 // Property load is breakable.
258 is_breakable_ = true;
259}
260
261
262void BreakableStatementChecker::VisitCall(Call* expr) {
263 // Function calls both through IC and call stub are breakable.
264 is_breakable_ = true;
265}
266
267
268void BreakableStatementChecker::VisitCallNew(CallNew* expr) {
269 // Function calls through new are breakable.
270 is_breakable_ = true;
271}
272
273
274void BreakableStatementChecker::VisitCallRuntime(CallRuntime* expr) {
275}
276
277
278void BreakableStatementChecker::VisitUnaryOperation(UnaryOperation* expr) {
279 Visit(expr->expression());
280}
281
282
283void BreakableStatementChecker::VisitCountOperation(CountOperation* expr) {
284 Visit(expr->expression());
285}
286
287
288void BreakableStatementChecker::VisitBinaryOperation(BinaryOperation* expr) {
289 Visit(expr->left());
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +0000290 if (expr->op() != Token::AND &&
291 expr->op() != Token::OR) {
292 Visit(expr->right());
293 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000294}
295
296
297void BreakableStatementChecker::VisitCompareOperation(CompareOperation* expr) {
298 Visit(expr->left());
299 Visit(expr->right());
300}
301
302
303void BreakableStatementChecker::VisitThisFunction(ThisFunction* expr) {
304}
305
306
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000307#define __ ACCESS_MASM(masm())
308
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000309bool FullCodeGenerator::MakeCode(CompilationInfo* info) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000310 Isolate* isolate = info->isolate();
ager@chromium.org5c838252010-02-19 08:53:10 +0000311 Handle<Script> script = info->script();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000312 if (!script->IsUndefined() && !script->source()->IsUndefined()) {
313 int len = String::cast(script->source())->length();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000314 isolate->counters()->total_full_codegen_source_size()->Increment(len);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000315 }
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +0000316 CodeGenerator::MakeCodePrologue(info, "full");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000317 const int kInitialBufferSize = 4 * KB;
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000318 MacroAssembler masm(info->isolate(), NULL, kInitialBufferSize);
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000319#ifdef ENABLE_GDB_JIT_INTERFACE
320 masm.positions_recorder()->StartGDBJITLineInfoRecording();
321#endif
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000322 LOG_CODE_EVENT(isolate,
323 CodeStartLinePosInfoRecordEvent(masm.positions_recorder()));
ager@chromium.org5c838252010-02-19 08:53:10 +0000324
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000325 FullCodeGenerator cgen(&masm, info);
yangguo@chromium.org56454712012-02-16 15:33:53 +0000326 cgen.Generate();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000327 if (cgen.HasStackOverflow()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000328 ASSERT(!isolate->has_pending_exception());
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000329 return false;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000330 }
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000331 unsigned table_offset = cgen.EmitBackEdgeTable();
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000332
lrn@chromium.org34e60782011-09-15 07:25:40 +0000333 Code::Flags flags = Code::ComputeFlags(Code::FUNCTION);
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000334 Handle<Code> code = CodeGenerator::MakeCodeEpilogue(&masm, flags, info);
rossberg@chromium.org2c067b12012-03-19 11:01:52 +0000335 code->set_optimizable(info->IsOptimizable() &&
ulan@chromium.orgd6899c32012-05-18 14:12:25 +0000336 !info->function()->flags()->Contains(kDontOptimize) &&
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000337 info->function()->scope()->AllowsLazyCompilation());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000338 cgen.PopulateDeoptimizationData(code);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000339 cgen.PopulateTypeFeedbackInfo(code);
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000340 cgen.PopulateTypeFeedbackCells(code);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000341 code->set_has_deoptimization_support(info->HasDeoptimizationSupport());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +0000342 code->set_handler_table(*cgen.handler_table());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000343#ifdef ENABLE_DEBUGGER_SUPPORT
lrn@chromium.org34e60782011-09-15 07:25:40 +0000344 code->set_has_debug_break_slots(
345 info->isolate()->debugger()->IsDebuggerActive());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000346 code->set_compiled_optimizable(info->IsOptimizable());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000347#endif // ENABLE_DEBUGGER_SUPPORT
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000348 code->set_allow_osr_at_loop_nesting_level(0);
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000349 code->set_profiler_ticks(0);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000350 code->set_back_edge_table_offset(table_offset);
351 code->set_back_edges_patched_for_osr(false);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000352 CodeGenerator::PrintCode(code, info);
rossberg@chromium.orgebeba022013-08-19 09:36:44 +0000353 info->SetCode(code);
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000354#ifdef ENABLE_GDB_JIT_INTERFACE
rossberg@chromium.orgebeba022013-08-19 09:36:44 +0000355 if (FLAG_gdbjit) {
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000356 GDBJITLineInfo* lineinfo =
357 masm.positions_recorder()->DetachGDBJITLineInfo();
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000358 GDBJIT(RegisterDetailedLineInfo(*code, lineinfo));
359 }
360#endif
rossberg@chromium.orgebeba022013-08-19 09:36:44 +0000361 void* line_info = masm.positions_recorder()->DetachJITHandlerData();
362 LOG_CODE_EVENT(isolate, CodeEndLinePosInfoRecordEvent(*code, line_info));
363 return true;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000364}
365
366
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000367unsigned FullCodeGenerator::EmitBackEdgeTable() {
368 // The back edge table consists of a length (in number of entries)
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000369 // field, and then a sequence of entries. Each entry is a pair of AST id
370 // and code-relative pc offset.
371 masm()->Align(kIntSize);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000372 unsigned offset = masm()->pc_offset();
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000373 unsigned length = back_edges_.length();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000374 __ dd(length);
375 for (unsigned i = 0; i < length; ++i) {
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000376 __ dd(back_edges_[i].id.ToInt());
377 __ dd(back_edges_[i].pc);
jkummerow@chromium.orgba72ec82013-07-22 09:21:20 +0000378 __ dd(back_edges_[i].loop_depth);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000379 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000380 return offset;
381}
382
383
384void FullCodeGenerator::PopulateDeoptimizationData(Handle<Code> code) {
385 // Fill in the deoptimization information.
386 ASSERT(info_->HasDeoptimizationSupport() || bailout_entries_.is_empty());
387 if (!info_->HasDeoptimizationSupport()) return;
388 int length = bailout_entries_.length();
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000389 Handle<DeoptimizationOutputData> data = isolate()->factory()->
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000390 NewDeoptimizationOutputData(length, TENURED);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000391 for (int i = 0; i < length; i++) {
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000392 data->SetAstId(i, bailout_entries_[i].id);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000393 data->SetPcAndState(i, Smi::FromInt(bailout_entries_[i].pc_and_state));
394 }
395 code->set_deoptimization_data(*data);
396}
397
398
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000399void FullCodeGenerator::PopulateTypeFeedbackInfo(Handle<Code> code) {
400 Handle<TypeFeedbackInfo> info = isolate()->factory()->NewTypeFeedbackInfo();
401 info->set_ic_total_count(ic_total_count_);
yangguo@chromium.orga7d3df92012-02-27 11:46:55 +0000402 ASSERT(!isolate()->heap()->InNewSpace(*info));
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000403 code->set_type_feedback_info(*info);
404}
405
406
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +0000407void FullCodeGenerator::Initialize() {
408 // The generation of debug code must match between the snapshot code and the
409 // code that is generated later. This is assumed by the debugger when it is
410 // calculating PC offsets after generating a debug version of code. Therefore
411 // we disable the production of debug code in the full compiler if we are
412 // either generating a snapshot or we booted from a snapshot.
413 generate_debug_code_ = FLAG_debug_code &&
414 !Serializer::enabled() &&
415 !Snapshot::HaveASnapshotToStartFrom();
416 masm_->set_emit_debug_code(generate_debug_code_);
417 masm_->set_predictable_code_size(true);
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000418 InitializeAstVisitor();
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +0000419}
420
421
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000422void FullCodeGenerator::PopulateTypeFeedbackCells(Handle<Code> code) {
423 if (type_feedback_cells_.is_empty()) return;
424 int length = type_feedback_cells_.length();
425 int array_size = TypeFeedbackCells::LengthOfFixedArray(length);
426 Handle<TypeFeedbackCells> cache = Handle<TypeFeedbackCells>::cast(
427 isolate()->factory()->NewFixedArray(array_size, TENURED));
428 for (int i = 0; i < length; i++) {
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000429 cache->SetAstId(i, type_feedback_cells_[i].ast_id);
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000430 cache->SetCell(i, *type_feedback_cells_[i].cell);
431 }
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000432 TypeFeedbackInfo::cast(code->type_feedback_info())->set_type_feedback_cells(
433 *cache);
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000434}
435
436
437
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000438void FullCodeGenerator::PrepareForBailout(Expression* node, State state) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000439 PrepareForBailoutForId(node->id(), state);
440}
441
442
443void FullCodeGenerator::RecordJSReturnSite(Call* call) {
444 // We record the offset of the function return so we can rebuild the frame
445 // if the function was inlined, i.e., this is the return address in the
446 // inlined function's frame.
447 //
448 // The state is ignored. We defensively set it to TOS_REG, which is the
449 // real state of the unoptimized code at the return site.
450 PrepareForBailoutForId(call->ReturnId(), TOS_REG);
451#ifdef DEBUG
452 // In debug builds, mark the return so we can verify that this function
453 // was called.
454 ASSERT(!call->return_is_recorded_);
455 call->return_is_recorded_ = true;
456#endif
457}
458
459
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000460void FullCodeGenerator::PrepareForBailoutForId(BailoutId id, State state) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000461 // There's no need to prepare this code for bailouts from already optimized
462 // code or code that can't be optimized.
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000463 if (!info_->HasDeoptimizationSupport()) return;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000464 unsigned pc_and_state =
465 StateField::encode(state) | PcField::encode(masm_->pc_offset());
yangguo@chromium.org56454712012-02-16 15:33:53 +0000466 ASSERT(Smi::IsValid(pc_and_state));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000467 BailoutEntry entry = { id, pc_and_state };
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000468 ASSERT(!prepared_bailout_ids_.Contains(id.ToInt()));
469 prepared_bailout_ids_.Add(id.ToInt(), zone());
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000470 bailout_entries_.Add(entry, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000471}
472
473
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000474void FullCodeGenerator::RecordTypeFeedbackCell(
danno@chromium.org41728482013-06-12 22:31:22 +0000475 TypeFeedbackId id, Handle<Cell> cell) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000476 TypeFeedbackCellEntry entry = { id, cell };
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000477 type_feedback_cells_.Add(entry, zone());
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000478}
479
480
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +0000481void FullCodeGenerator::RecordBackEdge(BailoutId ast_id) {
482 // The pc offset does not need to be encoded and packed together with a state.
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +0000483 ASSERT(masm_->pc_offset() > 0);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000484 ASSERT(loop_depth() > 0);
485 uint8_t depth = Min(loop_depth(), Code::kMaxLoopNestingMarker);
486 BackEdgeEntry entry =
487 { ast_id, static_cast<unsigned>(masm_->pc_offset()), depth };
488 back_edges_.Add(entry, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000489}
490
491
ricow@chromium.org65fae842010-08-25 15:26:24 +0000492bool FullCodeGenerator::ShouldInlineSmiCase(Token::Value op) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000493 // Inline smi case inside loops, but not division and modulo which
494 // are too complicated and take up too much space.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000495 if (op == Token::DIV ||op == Token::MOD) return false;
496 if (FLAG_always_inline_smi_code) return true;
497 return loop_depth_ > 0;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000498}
499
500
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000501void FullCodeGenerator::EffectContext::Plug(Register reg) const {
502}
503
504
505void FullCodeGenerator::AccumulatorValueContext::Plug(Register reg) const {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000506 __ Move(result_register(), reg);
507}
508
509
510void FullCodeGenerator::StackValueContext::Plug(Register reg) const {
danno@chromium.org59400602013-08-13 17:09:37 +0000511 __ Push(reg);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000512}
513
514
515void FullCodeGenerator::TestContext::Plug(Register reg) const {
516 // For simplicity we always test the accumulator register.
517 __ Move(result_register(), reg);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000518 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000519 codegen()->DoTest(this);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000520}
521
522
523void FullCodeGenerator::EffectContext::PlugTOS() const {
524 __ Drop(1);
525}
526
527
528void FullCodeGenerator::AccumulatorValueContext::PlugTOS() const {
danno@chromium.org59400602013-08-13 17:09:37 +0000529 __ Pop(result_register());
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000530}
531
532
533void FullCodeGenerator::StackValueContext::PlugTOS() const {
534}
535
536
537void FullCodeGenerator::TestContext::PlugTOS() const {
538 // For simplicity we always test the accumulator register.
danno@chromium.org59400602013-08-13 17:09:37 +0000539 __ Pop(result_register());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000540 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000541 codegen()->DoTest(this);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000542}
543
544
545void FullCodeGenerator::EffectContext::PrepareTest(
546 Label* materialize_true,
547 Label* materialize_false,
548 Label** if_true,
549 Label** if_false,
550 Label** fall_through) const {
551 // In an effect context, the true and the false case branch to the
552 // same label.
553 *if_true = *if_false = *fall_through = materialize_true;
554}
555
556
557void FullCodeGenerator::AccumulatorValueContext::PrepareTest(
558 Label* materialize_true,
559 Label* materialize_false,
560 Label** if_true,
561 Label** if_false,
562 Label** fall_through) const {
563 *if_true = *fall_through = materialize_true;
564 *if_false = materialize_false;
565}
566
567
568void FullCodeGenerator::StackValueContext::PrepareTest(
569 Label* materialize_true,
570 Label* materialize_false,
571 Label** if_true,
572 Label** if_false,
573 Label** fall_through) const {
574 *if_true = *fall_through = materialize_true;
575 *if_false = materialize_false;
576}
577
578
579void FullCodeGenerator::TestContext::PrepareTest(
580 Label* materialize_true,
581 Label* materialize_false,
582 Label** if_true,
583 Label** if_false,
584 Label** fall_through) const {
585 *if_true = true_label_;
586 *if_false = false_label_;
587 *fall_through = fall_through_;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000588}
589
590
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000591void FullCodeGenerator::DoTest(const TestContext* context) {
592 DoTest(context->condition(),
593 context->true_label(),
594 context->false_label(),
595 context->fall_through());
596}
597
598
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000599void FullCodeGenerator::AllocateModules(ZoneList<Declaration*>* declarations) {
600 ASSERT(scope_->is_global_scope());
601
602 for (int i = 0; i < declarations->length(); i++) {
603 ModuleDeclaration* declaration = declarations->at(i)->AsModuleDeclaration();
604 if (declaration != NULL) {
605 ModuleLiteral* module = declaration->module()->AsModuleLiteral();
606 if (module != NULL) {
607 Comment cmnt(masm_, "[ Link nested modules");
608 Scope* scope = module->body()->scope();
609 Interface* interface = scope->interface();
610 ASSERT(interface->IsModule() && interface->IsFrozen());
611
612 interface->Allocate(scope->module_var()->index());
613
614 // Set up module context.
615 ASSERT(scope->interface()->Index() >= 0);
616 __ Push(Smi::FromInt(scope->interface()->Index()));
617 __ Push(scope->GetScopeInfo());
618 __ CallRuntime(Runtime::kPushModuleContext, 2);
619 StoreToFrameField(StandardFrameConstants::kContextOffset,
620 context_register());
621
622 AllocateModules(scope->declarations());
623
624 // Pop module context.
625 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
626 // Update local stack frame context field.
627 StoreToFrameField(StandardFrameConstants::kContextOffset,
628 context_register());
629 }
630 }
631 }
632}
633
634
635// Modules have their own local scope, represented by their own context.
636// Module instance objects have an accessor for every export that forwards
637// access to the respective slot from the module's context. (Exports that are
638// modules themselves, however, are simple data properties.)
639//
640// All modules have a _hosting_ scope/context, which (currently) is the
641// (innermost) enclosing global scope. To deal with recursion, nested modules
642// are hosted by the same scope as global ones.
643//
644// For every (global or nested) module literal, the hosting context has an
645// internal slot that points directly to the respective module context. This
646// enables quick access to (statically resolved) module members by 2-dimensional
647// access through the hosting context. For example,
648//
649// module A {
650// let x;
651// module B { let y; }
652// }
653// module C { let z; }
654//
655// allocates contexts as follows:
656//
657// [header| .A | .B | .C | A | C ] (global)
658// | | |
659// | | +-- [header| z ] (module)
660// | |
661// | +------- [header| y ] (module)
662// |
663// +------------ [header| x | B ] (module)
664//
665// Here, .A, .B, .C are the internal slots pointing to the hosted module
666// contexts, whereas A, B, C hold the actual instance objects (note that every
667// module context also points to the respective instance object through its
668// extension slot in the header).
669//
670// To deal with arbitrary recursion and aliases between modules,
671// they are created and initialized in several stages. Each stage applies to
672// all modules in the hosting global scope, including nested ones.
673//
674// 1. Allocate: for each module _literal_, allocate the module contexts and
675// respective instance object and wire them up. This happens in the
676// PushModuleContext runtime function, as generated by AllocateModules
677// (invoked by VisitDeclarations in the hosting scope).
678//
679// 2. Bind: for each module _declaration_ (i.e. literals as well as aliases),
680// assign the respective instance object to respective local variables. This
681// happens in VisitModuleDeclaration, and uses the instance objects created
682// in the previous stage.
683// For each module _literal_, this phase also constructs a module descriptor
684// for the next stage. This happens in VisitModuleLiteral.
685//
686// 3. Populate: invoke the DeclareModules runtime function to populate each
687// _instance_ object with accessors for it exports. This is generated by
688// DeclareModules (invoked by VisitDeclarations in the hosting scope again),
689// and uses the descriptors generated in the previous stage.
690//
691// 4. Initialize: execute the module bodies (and other code) in sequence. This
692// happens by the separate statements generated for module bodies. To reenter
693// the module scopes properly, the parser inserted ModuleStatements.
694
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000695void FullCodeGenerator::VisitDeclarations(
696 ZoneList<Declaration*>* declarations) {
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000697 Handle<FixedArray> saved_modules = modules_;
698 int saved_module_index = module_index_;
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000699 ZoneList<Handle<Object> >* saved_globals = globals_;
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000700 ZoneList<Handle<Object> > inner_globals(10, zone());
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000701 globals_ = &inner_globals;
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000702
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000703 if (scope_->num_modules() != 0) {
704 // This is a scope hosting modules. Allocate a descriptor array to pass
705 // to the runtime for initialization.
706 Comment cmnt(masm_, "[ Allocate modules");
707 ASSERT(scope_->is_global_scope());
708 modules_ =
709 isolate()->factory()->NewFixedArray(scope_->num_modules(), TENURED);
710 module_index_ = 0;
711
712 // Generate code for allocating all modules, including nested ones.
713 // The allocated contexts are stored in internal variables in this scope.
714 AllocateModules(declarations);
715 }
716
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000717 AstVisitor::VisitDeclarations(declarations);
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000718
719 if (scope_->num_modules() != 0) {
720 // Initialize modules from descriptor array.
721 ASSERT(module_index_ == modules_->length());
722 DeclareModules(modules_);
723 modules_ = saved_modules;
724 module_index_ = saved_module_index;
725 }
726
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000727 if (!globals_->is_empty()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000728 // Invoke the platform-dependent code generator to do the actual
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000729 // declaration of the global functions and variables.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000730 Handle<FixedArray> array =
731 isolate()->factory()->NewFixedArray(globals_->length(), TENURED);
732 for (int i = 0; i < globals_->length(); ++i)
733 array->set(i, *globals_->at(i));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000734 DeclareGlobals(array);
735 }
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000736
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000737 globals_ = saved_globals;
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000738}
739
740
741void FullCodeGenerator::VisitModuleLiteral(ModuleLiteral* module) {
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000742 Block* block = module->body();
743 Scope* saved_scope = scope();
744 scope_ = block->scope();
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000745 Interface* interface = scope_->interface();
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000746
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000747 Comment cmnt(masm_, "[ ModuleLiteral");
748 SetStatementPosition(block);
749
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000750 ASSERT(!modules_.is_null());
751 ASSERT(module_index_ < modules_->length());
752 int index = module_index_++;
753
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000754 // Set up module context.
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000755 ASSERT(interface->Index() >= 0);
756 __ Push(Smi::FromInt(interface->Index()));
757 __ Push(Smi::FromInt(0));
758 __ CallRuntime(Runtime::kPushModuleContext, 2);
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000759 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000760
761 {
762 Comment cmnt(masm_, "[ Declarations");
763 VisitDeclarations(scope_->declarations());
764 }
765
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000766 // Populate the module description.
767 Handle<ModuleInfo> description =
768 ModuleInfo::Create(isolate(), interface, scope_);
769 modules_->set(index, *description);
770
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000771 scope_ = saved_scope;
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000772 // Pop module context.
773 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
774 // Update local stack frame context field.
775 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000776}
777
778
779void FullCodeGenerator::VisitModuleVariable(ModuleVariable* module) {
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000780 // Nothing to do.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000781 // The instance object is resolved statically through the module's interface.
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000782}
783
784
785void FullCodeGenerator::VisitModulePath(ModulePath* module) {
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000786 // Nothing to do.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000787 // The instance object is resolved statically through the module's interface.
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000788}
789
790
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000791void FullCodeGenerator::VisitModuleUrl(ModuleUrl* module) {
792 // TODO(rossberg): dummy allocation for now.
793 Scope* scope = module->body()->scope();
794 Interface* interface = scope_->interface();
795
796 ASSERT(interface->IsModule() && interface->IsFrozen());
797 ASSERT(!modules_.is_null());
798 ASSERT(module_index_ < modules_->length());
799 interface->Allocate(scope->module_var()->index());
800 int index = module_index_++;
801
802 Handle<ModuleInfo> description =
803 ModuleInfo::Create(isolate(), interface, scope_);
804 modules_->set(index, *description);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000805}
806
807
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000808int FullCodeGenerator::DeclareGlobalsFlags() {
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000809 ASSERT(DeclareGlobalsLanguageMode::is_valid(language_mode()));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000810 return DeclareGlobalsEvalFlag::encode(is_eval()) |
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000811 DeclareGlobalsNativeFlag::encode(is_native()) |
812 DeclareGlobalsLanguageMode::encode(language_mode());
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000813}
814
815
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000816void FullCodeGenerator::SetFunctionPosition(FunctionLiteral* fun) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000817 CodeGenerator::RecordPositions(masm_, fun->start_position());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000818}
819
820
821void FullCodeGenerator::SetReturnPosition(FunctionLiteral* fun) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000822 CodeGenerator::RecordPositions(masm_, fun->end_position() - 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000823}
824
825
826void FullCodeGenerator::SetStatementPosition(Statement* stmt) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000827#ifdef ENABLE_DEBUGGER_SUPPORT
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000828 if (!isolate()->debugger()->IsDebuggerActive()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000829 CodeGenerator::RecordPositions(masm_, stmt->statement_pos());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000830 } else {
831 // Check if the statement will be breakable without adding a debug break
832 // slot.
833 BreakableStatementChecker checker;
834 checker.Check(stmt);
835 // Record the statement position right here if the statement is not
836 // breakable. For breakable statements the actual recording of the
837 // position will be postponed to the breakable code (typically an IC).
838 bool position_recorded = CodeGenerator::RecordPositions(
839 masm_, stmt->statement_pos(), !checker.is_breakable());
840 // If the position recording did record a new position generate a debug
841 // break slot to make the statement breakable.
842 if (position_recorded) {
843 Debug::GenerateSlot(masm_);
844 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000845 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000846#else
847 CodeGenerator::RecordPositions(masm_, stmt->statement_pos());
848#endif
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000849}
850
851
852void FullCodeGenerator::SetExpressionPosition(Expression* expr, int pos) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000853#ifdef ENABLE_DEBUGGER_SUPPORT
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000854 if (!isolate()->debugger()->IsDebuggerActive()) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000855 CodeGenerator::RecordPositions(masm_, pos);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000856 } else {
857 // Check if the expression will be breakable without adding a debug break
858 // slot.
859 BreakableStatementChecker checker;
860 checker.Check(expr);
861 // Record a statement position right here if the expression is not
862 // breakable. For breakable expressions the actual recording of the
863 // position will be postponed to the breakable code (typically an IC).
864 // NOTE this will record a statement position for something which might
865 // not be a statement. As stepping in the debugger will only stop at
866 // statement positions this is used for e.g. the condition expression of
867 // a do while loop.
868 bool position_recorded = CodeGenerator::RecordPositions(
869 masm_, pos, !checker.is_breakable());
870 // If the position recording did record a new position generate a debug
871 // break slot to make the statement breakable.
872 if (position_recorded) {
873 Debug::GenerateSlot(masm_);
874 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000875 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000876#else
877 CodeGenerator::RecordPositions(masm_, pos);
878#endif
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000879}
880
881
882void FullCodeGenerator::SetStatementPosition(int pos) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000883 CodeGenerator::RecordPositions(masm_, pos);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000884}
885
886
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000887void FullCodeGenerator::SetSourcePosition(int pos) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000888 if (pos != RelocInfo::kNoPosition) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000889 masm_->positions_recorder()->RecordPosition(pos);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000890 }
891}
892
893
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000894// Lookup table for code generators for special runtime calls which are
895// generated inline.
896#define INLINE_FUNCTION_GENERATOR_ADDRESS(Name, argc, ressize) \
897 &FullCodeGenerator::Emit##Name,
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000898
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000899const FullCodeGenerator::InlineFunctionGenerator
900 FullCodeGenerator::kInlineFunctionGenerators[] = {
901 INLINE_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS)
902 INLINE_RUNTIME_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS)
903 };
904#undef INLINE_FUNCTION_GENERATOR_ADDRESS
905
906
907FullCodeGenerator::InlineFunctionGenerator
908 FullCodeGenerator::FindInlineFunctionGenerator(Runtime::FunctionId id) {
whesse@chromium.org023421e2010-12-21 12:19:12 +0000909 int lookup_index =
910 static_cast<int>(id) - static_cast<int>(Runtime::kFirstInlineFunction);
911 ASSERT(lookup_index >= 0);
912 ASSERT(static_cast<size_t>(lookup_index) <
913 ARRAY_SIZE(kInlineFunctionGenerators));
914 return kInlineFunctionGenerators[lookup_index];
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000915}
916
917
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000918void FullCodeGenerator::EmitInlineRuntimeCall(CallRuntime* expr) {
919 const Runtime::Function* function = expr->function();
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000920 ASSERT(function != NULL);
921 ASSERT(function->intrinsic_type == Runtime::INLINE);
922 InlineFunctionGenerator generator =
923 FindInlineFunctionGenerator(function->function_id);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000924 ((*this).*(generator))(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000925}
926
927
verwaest@chromium.org8a00e822013-06-10 15:11:22 +0000928void FullCodeGenerator::EmitGeneratorNext(CallRuntime* expr) {
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000929 ZoneList<Expression*>* args = expr->arguments();
930 ASSERT(args->length() == 2);
verwaest@chromium.org8a00e822013-06-10 15:11:22 +0000931 EmitGeneratorResume(args->at(0), args->at(1), JSGeneratorObject::NEXT);
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000932}
933
934
935void FullCodeGenerator::EmitGeneratorThrow(CallRuntime* expr) {
936 ZoneList<Expression*>* args = expr->arguments();
937 ASSERT(args->length() == 2);
938 EmitGeneratorResume(args->at(0), args->at(1), JSGeneratorObject::THROW);
939}
940
941
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +0000942void FullCodeGenerator::EmitDebugBreakInOptimizedCode(CallRuntime* expr) {
943 context()->Plug(handle(Smi::FromInt(0), isolate()));
944}
945
946
ricow@chromium.org65fae842010-08-25 15:26:24 +0000947void FullCodeGenerator::VisitBinaryOperation(BinaryOperation* expr) {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000948 switch (expr->op()) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000949 case Token::COMMA:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000950 return VisitComma(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000951 case Token::OR:
952 case Token::AND:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000953 return VisitLogicalExpression(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000954 default:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000955 return VisitArithmeticExpression(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000956 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000957}
958
959
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000960void FullCodeGenerator::VisitInDuplicateContext(Expression* expr) {
961 if (context()->IsEffect()) {
962 VisitForEffect(expr);
963 } else if (context()->IsAccumulatorValue()) {
964 VisitForAccumulatorValue(expr);
965 } else if (context()->IsStackValue()) {
966 VisitForStackValue(expr);
967 } else if (context()->IsTest()) {
968 const TestContext* test = TestContext::cast(context());
969 VisitForControl(expr, test->true_label(), test->false_label(),
970 test->fall_through());
971 }
972}
973
974
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000975void FullCodeGenerator::VisitComma(BinaryOperation* expr) {
976 Comment cmnt(masm_, "[ Comma");
977 VisitForEffect(expr->left());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000978 VisitInDuplicateContext(expr->right());
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000979}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000980
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000981
982void FullCodeGenerator::VisitLogicalExpression(BinaryOperation* expr) {
983 bool is_logical_and = expr->op() == Token::AND;
984 Comment cmnt(masm_, is_logical_and ? "[ Logical AND" : "[ Logical OR");
985 Expression* left = expr->left();
986 Expression* right = expr->right();
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000987 BailoutId right_id = expr->RightId();
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000988 Label done;
989
990 if (context()->IsTest()) {
991 Label eval_right;
992 const TestContext* test = TestContext::cast(context());
993 if (is_logical_and) {
994 VisitForControl(left, &eval_right, test->false_label(), &eval_right);
995 } else {
996 VisitForControl(left, test->true_label(), &eval_right, &eval_right);
997 }
998 PrepareForBailoutForId(right_id, NO_REGISTERS);
999 __ bind(&eval_right);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001000
1001 } else if (context()->IsAccumulatorValue()) {
1002 VisitForAccumulatorValue(left);
1003 // We want the value in the accumulator for the test, and on the stack in
1004 // case we need it.
danno@chromium.org59400602013-08-13 17:09:37 +00001005 __ Push(result_register());
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001006 Label discard, restore;
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001007 if (is_logical_and) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001008 DoTest(left, &discard, &restore, &restore);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001009 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001010 DoTest(left, &restore, &discard, &restore);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001011 }
1012 __ bind(&restore);
danno@chromium.org59400602013-08-13 17:09:37 +00001013 __ Pop(result_register());
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001014 __ jmp(&done);
1015 __ bind(&discard);
1016 __ Drop(1);
1017 PrepareForBailoutForId(right_id, NO_REGISTERS);
1018
1019 } else if (context()->IsStackValue()) {
1020 VisitForAccumulatorValue(left);
1021 // We want the value in the accumulator for the test, and on the stack in
1022 // case we need it.
danno@chromium.org59400602013-08-13 17:09:37 +00001023 __ Push(result_register());
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001024 Label discard;
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001025 if (is_logical_and) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001026 DoTest(left, &discard, &done, &discard);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001027 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001028 DoTest(left, &done, &discard, &discard);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001029 }
1030 __ bind(&discard);
1031 __ Drop(1);
1032 PrepareForBailoutForId(right_id, NO_REGISTERS);
1033
1034 } else {
1035 ASSERT(context()->IsEffect());
1036 Label eval_right;
1037 if (is_logical_and) {
1038 VisitForControl(left, &eval_right, &done, &eval_right);
1039 } else {
1040 VisitForControl(left, &done, &eval_right, &eval_right);
1041 }
1042 PrepareForBailoutForId(right_id, NO_REGISTERS);
1043 __ bind(&eval_right);
1044 }
1045
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001046 VisitInDuplicateContext(right);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001047 __ bind(&done);
1048}
1049
1050
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001051void FullCodeGenerator::VisitArithmeticExpression(BinaryOperation* expr) {
1052 Token::Value op = expr->op();
1053 Comment cmnt(masm_, "[ ArithmeticExpression");
1054 Expression* left = expr->left();
1055 Expression* right = expr->right();
1056 OverwriteMode mode =
1057 left->ResultOverwriteAllowed()
1058 ? OVERWRITE_LEFT
1059 : (right->ResultOverwriteAllowed() ? OVERWRITE_RIGHT : NO_OVERWRITE);
1060
1061 VisitForStackValue(left);
1062 VisitForAccumulatorValue(right);
1063
1064 SetSourcePosition(expr->position());
1065 if (ShouldInlineSmiCase(op)) {
1066 EmitInlineSmiBinaryOp(expr, op, mode, left, right);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001067 } else {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001068 EmitBinaryOp(expr, op, mode);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001069 }
1070}
1071
1072
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001073void FullCodeGenerator::VisitBlock(Block* stmt) {
1074 Comment cmnt(masm_, "[ Block");
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001075 NestedBlock nested_block(this, stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001076 SetStatementPosition(stmt);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001077
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001078 Scope* saved_scope = scope();
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001079 // Push a block context when entering a block with block scoped variables.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00001080 if (stmt->scope() != NULL) {
danno@chromium.org81cac2b2012-07-10 11:28:27 +00001081 scope_ = stmt->scope();
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00001082 ASSERT(!scope_->is_module_scope());
1083 { Comment cmnt(masm_, "[ Extend block context");
1084 Handle<ScopeInfo> scope_info = scope_->GetScopeInfo();
1085 int heap_slots = scope_info->ContextLength() - Context::MIN_CONTEXT_SLOTS;
1086 __ Push(scope_info);
1087 PushFunctionArgumentForContextAllocation();
1088 if (heap_slots <= FastNewBlockContextStub::kMaximumSlots) {
1089 FastNewBlockContextStub stub(heap_slots);
1090 __ CallStub(&stub);
1091 } else {
1092 __ CallRuntime(Runtime::kPushBlockContext, 2);
1093 }
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001094
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00001095 // Replace the context stored in the frame.
1096 StoreToFrameField(StandardFrameConstants::kContextOffset,
1097 context_register());
1098 }
1099 { Comment cmnt(masm_, "[ Declarations");
1100 VisitDeclarations(scope_->declarations());
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001101 }
1102 }
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00001103
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00001104 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001105 VisitStatements(stmt->statements());
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001106 scope_ = saved_scope;
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001107 __ bind(nested_block.break_label());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001108 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001109
1110 // Pop block context if necessary.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00001111 if (stmt->scope() != NULL) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001112 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1113 // Update local stack frame context field.
1114 StoreToFrameField(StandardFrameConstants::kContextOffset,
1115 context_register());
1116 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001117}
1118
1119
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00001120void FullCodeGenerator::VisitModuleStatement(ModuleStatement* stmt) {
1121 Comment cmnt(masm_, "[ Module context");
1122
1123 __ Push(Smi::FromInt(stmt->proxy()->interface()->Index()));
1124 __ Push(Smi::FromInt(0));
1125 __ CallRuntime(Runtime::kPushModuleContext, 2);
1126 StoreToFrameField(
1127 StandardFrameConstants::kContextOffset, context_register());
1128
1129 Scope* saved_scope = scope_;
1130 scope_ = stmt->body()->scope();
1131 VisitStatements(stmt->body()->statements());
1132 scope_ = saved_scope;
1133 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1134 // Update local stack frame context field.
1135 StoreToFrameField(StandardFrameConstants::kContextOffset,
1136 context_register());
1137}
1138
1139
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001140void FullCodeGenerator::VisitExpressionStatement(ExpressionStatement* stmt) {
1141 Comment cmnt(masm_, "[ ExpressionStatement");
1142 SetStatementPosition(stmt);
1143 VisitForEffect(stmt->expression());
1144}
1145
1146
1147void FullCodeGenerator::VisitEmptyStatement(EmptyStatement* stmt) {
1148 Comment cmnt(masm_, "[ EmptyStatement");
1149 SetStatementPosition(stmt);
1150}
1151
1152
1153void FullCodeGenerator::VisitIfStatement(IfStatement* stmt) {
1154 Comment cmnt(masm_, "[ IfStatement");
1155 SetStatementPosition(stmt);
1156 Label then_part, else_part, done;
1157
ricow@chromium.org65fae842010-08-25 15:26:24 +00001158 if (stmt->HasElseStatement()) {
1159 VisitForControl(stmt->condition(), &then_part, &else_part, &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());
1163 __ jmp(&done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001164
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001165 PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001166 __ bind(&else_part);
1167 Visit(stmt->else_statement());
1168 } else {
1169 VisitForControl(stmt->condition(), &then_part, &done, &then_part);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001170 PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001171 __ bind(&then_part);
1172 Visit(stmt->then_statement());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001173
1174 PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001175 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001176 __ bind(&done);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001177 PrepareForBailoutForId(stmt->IfId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001178}
1179
1180
1181void FullCodeGenerator::VisitContinueStatement(ContinueStatement* stmt) {
1182 Comment cmnt(masm_, "[ ContinueStatement");
1183 SetStatementPosition(stmt);
1184 NestedStatement* current = nesting_stack_;
1185 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001186 int context_length = 0;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001187 // When continuing, we clobber the unpredictable value in the accumulator
1188 // with one that's safe for GC. If we hit an exit from the try block of
1189 // try...finally on our way out, we will unconditionally preserve the
1190 // accumulator on the stack.
1191 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001192 while (!current->IsContinueTarget(stmt->target())) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001193 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001194 }
1195 __ Drop(stack_depth);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001196 if (context_length > 0) {
1197 while (context_length > 0) {
1198 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1199 --context_length;
1200 }
1201 StoreToFrameField(StandardFrameConstants::kContextOffset,
1202 context_register());
1203 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001204
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001205 __ jmp(current->AsIteration()->continue_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001206}
1207
1208
1209void FullCodeGenerator::VisitBreakStatement(BreakStatement* stmt) {
1210 Comment cmnt(masm_, "[ BreakStatement");
1211 SetStatementPosition(stmt);
1212 NestedStatement* current = nesting_stack_;
1213 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001214 int context_length = 0;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001215 // When breaking, we clobber the unpredictable value in the accumulator
1216 // with one that's safe for GC. If we hit an exit from the try block of
1217 // try...finally on our way out, we will unconditionally preserve the
1218 // accumulator on the stack.
1219 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001220 while (!current->IsBreakTarget(stmt->target())) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001221 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001222 }
1223 __ Drop(stack_depth);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001224 if (context_length > 0) {
1225 while (context_length > 0) {
1226 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1227 --context_length;
1228 }
1229 StoreToFrameField(StandardFrameConstants::kContextOffset,
1230 context_register());
1231 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001232
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001233 __ jmp(current->AsBreakable()->break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001234}
1235
1236
danno@chromium.org41728482013-06-12 22:31:22 +00001237void FullCodeGenerator::EmitUnwindBeforeReturn() {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001238 NestedStatement* current = nesting_stack_;
1239 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001240 int context_length = 0;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001241 while (current != NULL) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001242 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001243 }
1244 __ Drop(stack_depth);
danno@chromium.org41728482013-06-12 22:31:22 +00001245}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001246
danno@chromium.org41728482013-06-12 22:31:22 +00001247
1248void FullCodeGenerator::VisitReturnStatement(ReturnStatement* stmt) {
1249 Comment cmnt(masm_, "[ ReturnStatement");
1250 SetStatementPosition(stmt);
1251 Expression* expr = stmt->expression();
1252 VisitForAccumulatorValue(expr);
1253 EmitUnwindBeforeReturn();
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001254 EmitReturnSequence();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001255}
1256
1257
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001258void FullCodeGenerator::VisitWithStatement(WithStatement* stmt) {
1259 Comment cmnt(masm_, "[ WithStatement");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001260 SetStatementPosition(stmt);
1261
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001262 VisitForStackValue(stmt->expression());
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00001263 PushFunctionArgumentForContextAllocation();
1264 __ CallRuntime(Runtime::kPushWithContext, 2);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001265 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001266
danno@chromium.orgca29dd82013-04-26 11:59:48 +00001267 Scope* saved_scope = scope();
1268 scope_ = stmt->scope();
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001269 { WithOrCatch body(this);
1270 Visit(stmt->statement());
1271 }
danno@chromium.orgca29dd82013-04-26 11:59:48 +00001272 scope_ = saved_scope;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001273
1274 // Pop context.
1275 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1276 // Update local stack frame context field.
1277 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001278}
1279
1280
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001281void FullCodeGenerator::VisitDoWhileStatement(DoWhileStatement* stmt) {
1282 Comment cmnt(masm_, "[ DoWhileStatement");
1283 SetStatementPosition(stmt);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001284 Label body, book_keeping;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001285
1286 Iteration loop_statement(this, stmt);
1287 increment_loop_depth();
1288
1289 __ bind(&body);
1290 Visit(stmt->body());
1291
ricow@chromium.org65fae842010-08-25 15:26:24 +00001292 // Record the position of the do while condition and make sure it is
1293 // possible to break on the condition.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001294 __ bind(loop_statement.continue_label());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001295 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001296 SetExpressionPosition(stmt->cond(), stmt->condition_position());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001297 VisitForControl(stmt->cond(),
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001298 &book_keeping,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001299 loop_statement.break_label(),
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001300 &book_keeping);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001301
1302 // Check stack before looping.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001303 PrepareForBailoutForId(stmt->BackEdgeId(), NO_REGISTERS);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001304 __ bind(&book_keeping);
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +00001305 EmitBackEdgeBookkeeping(stmt, &body);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001306 __ jmp(&body);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001307
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001308 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001309 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001310 decrement_loop_depth();
1311}
1312
1313
1314void FullCodeGenerator::VisitWhileStatement(WhileStatement* stmt) {
1315 Comment cmnt(masm_, "[ WhileStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001316 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001317
1318 Iteration loop_statement(this, stmt);
1319 increment_loop_depth();
1320
1321 // Emit the test at the bottom of the loop.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001322 __ jmp(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001323
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001324 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001325 __ bind(&body);
1326 Visit(stmt->body());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001327
1328 // Emit the statement position here as this is where the while
1329 // statement code starts.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001330 __ bind(loop_statement.continue_label());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001331 SetStatementPosition(stmt);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001332
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001333 // Check stack before looping.
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +00001334 EmitBackEdgeBookkeeping(stmt, &body);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001335
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001336 __ bind(&test);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001337 VisitForControl(stmt->cond(),
1338 &body,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001339 loop_statement.break_label(),
1340 loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001341
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001342 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001343 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001344 decrement_loop_depth();
1345}
1346
1347
1348void FullCodeGenerator::VisitForStatement(ForStatement* stmt) {
1349 Comment cmnt(masm_, "[ ForStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001350 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001351
1352 Iteration loop_statement(this, stmt);
erik.corry@gmail.combbceb572012-03-09 10:52:05 +00001353
1354 // Set statement position for a break slot before entering the for-body.
1355 SetStatementPosition(stmt);
1356
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001357 if (stmt->init() != NULL) {
1358 Visit(stmt->init());
1359 }
1360
1361 increment_loop_depth();
1362 // Emit the test at the bottom of the loop (even if empty).
1363 __ jmp(&test);
1364
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001365 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001366 __ bind(&body);
1367 Visit(stmt->body());
1368
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001369 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001370 __ bind(loop_statement.continue_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001371 if (stmt->next() != NULL) {
1372 Visit(stmt->next());
1373 }
1374
ricow@chromium.org65fae842010-08-25 15:26:24 +00001375 // Emit the statement position here as this is where the for
1376 // statement code starts.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001377 SetStatementPosition(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001378
1379 // Check stack before looping.
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +00001380 EmitBackEdgeBookkeeping(stmt, &body);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001381
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001382 __ bind(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001383 if (stmt->cond() != NULL) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00001384 VisitForControl(stmt->cond(),
1385 &body,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001386 loop_statement.break_label(),
1387 loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001388 } else {
1389 __ jmp(&body);
1390 }
1391
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001392 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001393 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001394 decrement_loop_depth();
1395}
1396
1397
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001398void FullCodeGenerator::VisitTryCatchStatement(TryCatchStatement* stmt) {
1399 Comment cmnt(masm_, "[ TryCatchStatement");
1400 SetStatementPosition(stmt);
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001401 // The try block adds a handler to the exception handler chain before
1402 // entering, and removes it again when exiting normally. If an exception
1403 // is thrown during execution of the try block, the handler is consumed
1404 // and control is passed to the catch block with the exception in the
1405 // result register.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001406
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001407 Label try_entry, handler_entry, exit;
1408 __ jmp(&try_entry);
1409 __ bind(&handler_entry);
1410 handler_table()->set(stmt->index(), Smi::FromInt(handler_entry.pos()));
1411 // Exception handler code, the exception is in the result register.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001412 // Extend the context before executing the catch block.
1413 { Comment cmnt(masm_, "[ Extend catch context");
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001414 __ Push(stmt->variable()->name());
danno@chromium.org59400602013-08-13 17:09:37 +00001415 __ Push(result_register());
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00001416 PushFunctionArgumentForContextAllocation();
1417 __ CallRuntime(Runtime::kPushCatchContext, 3);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001418 StoreToFrameField(StandardFrameConstants::kContextOffset,
1419 context_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001420 }
1421
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001422 Scope* saved_scope = scope();
1423 scope_ = stmt->scope();
1424 ASSERT(scope_->declarations()->is_empty());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001425 { WithOrCatch catch_body(this);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001426 Visit(stmt->catch_block());
1427 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001428 // Restore the context.
1429 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1430 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001431 scope_ = saved_scope;
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001432 __ jmp(&exit);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001433
1434 // Try block code. Sets up the exception handler chain.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001435 __ bind(&try_entry);
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00001436 __ PushTryHandler(StackHandler::CATCH, stmt->index());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001437 { TryCatch try_body(this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001438 Visit(stmt->try_block());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001439 }
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001440 __ PopTryHandler();
1441 __ bind(&exit);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001442}
1443
1444
1445void FullCodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* stmt) {
1446 Comment cmnt(masm_, "[ TryFinallyStatement");
1447 SetStatementPosition(stmt);
1448 // Try finally is compiled by setting up a try-handler on the stack while
1449 // executing the try body, and removing it again afterwards.
1450 //
1451 // The try-finally construct can enter the finally block in three ways:
1452 // 1. By exiting the try-block normally. This removes the try-handler and
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001453 // calls the finally block code before continuing.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001454 // 2. By exiting the try-block with a function-local control flow transfer
1455 // (break/continue/return). The site of the, e.g., break removes the
1456 // try handler and calls the finally block code before continuing
1457 // its outward control transfer.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001458 // 3. By exiting the try-block with a thrown exception.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001459 // This can happen in nested function calls. It traverses the try-handler
1460 // chain and consumes the try-handler entry before jumping to the
1461 // handler code. The handler code then calls the finally-block before
1462 // rethrowing the exception.
1463 //
1464 // The finally block must assume a return address on top of the stack
1465 // (or in the link register on ARM chips) and a value (return value or
1466 // exception) in the result register (rax/eax/r0), both of which must
1467 // be preserved. The return address isn't GC-safe, so it should be
1468 // cooked before GC.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001469 Label try_entry, handler_entry, finally_entry;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001470
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001471 // Jump to try-handler setup and try-block code.
1472 __ jmp(&try_entry);
1473 __ bind(&handler_entry);
1474 handler_table()->set(stmt->index(), Smi::FromInt(handler_entry.pos()));
1475 // Exception handler code. This code is only executed when an exception
1476 // is thrown. The exception is in the result register, and must be
1477 // preserved by the finally block. Call the finally block and then
1478 // rethrow the exception if it returns.
1479 __ Call(&finally_entry);
danno@chromium.org59400602013-08-13 17:09:37 +00001480 __ Push(result_register());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001481 __ CallRuntime(Runtime::kReThrow, 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001482
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001483 // Finally block implementation.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001484 __ bind(&finally_entry);
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001485 EnterFinallyBlock();
1486 { Finally finally_body(this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001487 Visit(stmt->finally_block());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001488 }
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001489 ExitFinallyBlock(); // Return to the calling code.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001490
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001491 // Set up try handler.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001492 __ bind(&try_entry);
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00001493 __ PushTryHandler(StackHandler::FINALLY, stmt->index());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001494 { TryFinally try_body(this, &finally_entry);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001495 Visit(stmt->try_block());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001496 }
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001497 __ PopTryHandler();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001498 // Execute the finally block on the way out. Clobber the unpredictable
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001499 // value in the result register with one that's safe for GC because the
1500 // finally block will unconditionally preserve the result register on the
1501 // stack.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001502 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001503 __ Call(&finally_entry);
1504}
1505
1506
1507void FullCodeGenerator::VisitDebuggerStatement(DebuggerStatement* stmt) {
1508#ifdef ENABLE_DEBUGGER_SUPPORT
1509 Comment cmnt(masm_, "[ DebuggerStatement");
1510 SetStatementPosition(stmt);
1511
ager@chromium.org5c838252010-02-19 08:53:10 +00001512 __ DebugBreak();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001513 // Ignore the return value.
1514#endif
1515}
1516
1517
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001518void FullCodeGenerator::VisitConditional(Conditional* expr) {
1519 Comment cmnt(masm_, "[ Conditional");
1520 Label true_case, false_case, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001521 VisitForControl(expr->condition(), &true_case, &false_case, &true_case);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001522
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001523 PrepareForBailoutForId(expr->ThenId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001524 __ bind(&true_case);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001525 SetExpressionPosition(expr->then_expression(),
1526 expr->then_expression_position());
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001527 if (context()->IsTest()) {
1528 const TestContext* for_test = TestContext::cast(context());
1529 VisitForControl(expr->then_expression(),
1530 for_test->true_label(),
1531 for_test->false_label(),
1532 NULL);
1533 } else {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001534 VisitInDuplicateContext(expr->then_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001535 __ jmp(&done);
1536 }
1537
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001538 PrepareForBailoutForId(expr->ElseId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001539 __ bind(&false_case);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001540 SetExpressionPosition(expr->else_expression(),
1541 expr->else_expression_position());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001542 VisitInDuplicateContext(expr->else_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001543 // If control flow falls through Visit, merge it with true case here.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001544 if (!context()->IsTest()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001545 __ bind(&done);
1546 }
1547}
1548
1549
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001550void FullCodeGenerator::VisitLiteral(Literal* expr) {
1551 Comment cmnt(masm_, "[ Literal");
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00001552 context()->Plug(expr->value());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001553}
1554
1555
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001556void FullCodeGenerator::VisitFunctionLiteral(FunctionLiteral* expr) {
1557 Comment cmnt(masm_, "[ FunctionLiteral");
1558
1559 // Build the function boilerplate and instantiate it.
1560 Handle<SharedFunctionInfo> function_info =
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001561 Compiler::BuildFunctionInfo(expr, script());
1562 if (function_info.is_null()) {
1563 SetStackOverflow();
1564 return;
1565 }
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001566 EmitNewClosure(function_info, expr->pretenure());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001567}
1568
1569
1570void FullCodeGenerator::VisitSharedFunctionInfoLiteral(
1571 SharedFunctionInfoLiteral* expr) {
1572 Comment cmnt(masm_, "[ SharedFunctionInfoLiteral");
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001573 EmitNewClosure(expr->shared_function_info(), false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001574}
1575
1576
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001577void FullCodeGenerator::VisitThrow(Throw* expr) {
1578 Comment cmnt(masm_, "[ Throw");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001579 VisitForStackValue(expr->exception());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001580 __ CallRuntime(Runtime::kThrow, 1);
1581 // Never returns here.
1582}
1583
1584
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001585FullCodeGenerator::NestedStatement* FullCodeGenerator::TryCatch::Exit(
1586 int* stack_depth,
1587 int* context_length) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001588 // The macros used here must preserve the result register.
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001589 __ Drop(*stack_depth);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001590 __ PopTryHandler();
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001591 *stack_depth = 0;
1592 return previous_;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001593}
1594
ricow@chromium.org65fae842010-08-25 15:26:24 +00001595
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001596bool FullCodeGenerator::TryLiteralCompare(CompareOperation* expr) {
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001597 Expression* sub_expr;
ager@chromium.org04921a82011-06-27 13:21:41 +00001598 Handle<String> check;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001599 if (expr->IsLiteralCompareTypeof(&sub_expr, &check)) {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001600 EmitLiteralCompareTypeof(expr, sub_expr, check);
ager@chromium.org04921a82011-06-27 13:21:41 +00001601 return true;
1602 }
1603
jkummerow@chromium.org96a3c512013-07-18 17:02:47 +00001604 if (expr->IsLiteralCompareUndefined(&sub_expr, isolate())) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001605 EmitLiteralCompareNil(expr, sub_expr, kUndefinedValue);
1606 return true;
1607 }
1608
1609 if (expr->IsLiteralCompareNull(&sub_expr)) {
1610 EmitLiteralCompareNil(expr, sub_expr, kNullValue);
ager@chromium.org04921a82011-06-27 13:21:41 +00001611 return true;
1612 }
1613
1614 return false;
1615}
1616
1617
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001618#undef __
1619
1620
1621} } // namespace v8::internal