blob: c1350a14d971613b6c6c12f7668207cd99725d9c [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
danno@chromium.org1fd77d52013-06-07 16:01:45 +0000166void BreakableStatementChecker::VisitForOfStatement(ForOfStatement* stmt) {
167 // For-of is breakable because of the next() call.
168 is_breakable_ = true;
169}
170
171
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000172void BreakableStatementChecker::VisitTryCatchStatement(
173 TryCatchStatement* stmt) {
174 // Mark try catch as breakable to avoid adding a break slot in front of it.
175 is_breakable_ = true;
176}
177
178
179void BreakableStatementChecker::VisitTryFinallyStatement(
180 TryFinallyStatement* stmt) {
181 // Mark try finally as breakable to avoid adding a break slot in front of it.
182 is_breakable_ = true;
183}
184
185
186void BreakableStatementChecker::VisitDebuggerStatement(
187 DebuggerStatement* stmt) {
188 // The debugger statement is breakable.
189 is_breakable_ = true;
190}
191
192
193void BreakableStatementChecker::VisitFunctionLiteral(FunctionLiteral* expr) {
194}
195
196
197void BreakableStatementChecker::VisitSharedFunctionInfoLiteral(
198 SharedFunctionInfoLiteral* expr) {
199}
200
201
202void BreakableStatementChecker::VisitConditional(Conditional* expr) {
203}
204
205
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000206void BreakableStatementChecker::VisitVariableProxy(VariableProxy* expr) {
207}
208
209
210void BreakableStatementChecker::VisitLiteral(Literal* expr) {
211}
212
213
214void BreakableStatementChecker::VisitRegExpLiteral(RegExpLiteral* expr) {
215}
216
217
218void BreakableStatementChecker::VisitObjectLiteral(ObjectLiteral* expr) {
219}
220
221
222void BreakableStatementChecker::VisitArrayLiteral(ArrayLiteral* expr) {
223}
224
225
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000226void BreakableStatementChecker::VisitAssignment(Assignment* expr) {
227 // If assigning to a property (including a global property) the assignment is
228 // breakable.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000229 VariableProxy* proxy = expr->target()->AsVariableProxy();
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000230 Property* prop = expr->target()->AsProperty();
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000231 if (prop != NULL || (proxy != NULL && proxy->var()->IsUnallocated())) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000232 is_breakable_ = true;
233 return;
234 }
235
236 // Otherwise the assignment is breakable if the assigned value is.
237 Visit(expr->value());
238}
239
240
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000241void BreakableStatementChecker::VisitYield(Yield* expr) {
242 // Yield is breakable if the expression is.
243 Visit(expr->expression());
244}
245
246
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000247void BreakableStatementChecker::VisitThrow(Throw* expr) {
248 // Throw is breakable if the expression is.
249 Visit(expr->exception());
250}
251
252
253void BreakableStatementChecker::VisitProperty(Property* expr) {
254 // Property load is breakable.
255 is_breakable_ = true;
256}
257
258
259void BreakableStatementChecker::VisitCall(Call* expr) {
260 // Function calls both through IC and call stub are breakable.
261 is_breakable_ = true;
262}
263
264
265void BreakableStatementChecker::VisitCallNew(CallNew* expr) {
266 // Function calls through new are breakable.
267 is_breakable_ = true;
268}
269
270
271void BreakableStatementChecker::VisitCallRuntime(CallRuntime* expr) {
272}
273
274
275void BreakableStatementChecker::VisitUnaryOperation(UnaryOperation* expr) {
276 Visit(expr->expression());
277}
278
279
280void BreakableStatementChecker::VisitCountOperation(CountOperation* expr) {
281 Visit(expr->expression());
282}
283
284
285void BreakableStatementChecker::VisitBinaryOperation(BinaryOperation* expr) {
286 Visit(expr->left());
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +0000287 if (expr->op() != Token::AND &&
288 expr->op() != Token::OR) {
289 Visit(expr->right());
290 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000291}
292
293
294void BreakableStatementChecker::VisitCompareOperation(CompareOperation* expr) {
295 Visit(expr->left());
296 Visit(expr->right());
297}
298
299
300void BreakableStatementChecker::VisitThisFunction(ThisFunction* expr) {
301}
302
303
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000304#define __ ACCESS_MASM(masm())
305
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000306bool FullCodeGenerator::MakeCode(CompilationInfo* info) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000307 Isolate* isolate = info->isolate();
ager@chromium.org5c838252010-02-19 08:53:10 +0000308 Handle<Script> script = info->script();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000309 if (!script->IsUndefined() && !script->source()->IsUndefined()) {
310 int len = String::cast(script->source())->length();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000311 isolate->counters()->total_full_codegen_source_size()->Increment(len);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000312 }
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +0000313 CodeGenerator::MakeCodePrologue(info, "full");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000314 const int kInitialBufferSize = 4 * KB;
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000315 MacroAssembler masm(info->isolate(), NULL, kInitialBufferSize);
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000316#ifdef ENABLE_GDB_JIT_INTERFACE
317 masm.positions_recorder()->StartGDBJITLineInfoRecording();
318#endif
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000319 LOG_CODE_EVENT(isolate,
320 CodeStartLinePosInfoRecordEvent(masm.positions_recorder()));
ager@chromium.org5c838252010-02-19 08:53:10 +0000321
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000322 FullCodeGenerator cgen(&masm, info);
yangguo@chromium.org56454712012-02-16 15:33:53 +0000323 cgen.Generate();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000324 if (cgen.HasStackOverflow()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000325 ASSERT(!isolate->has_pending_exception());
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000326 return false;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000327 }
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000328 unsigned table_offset = cgen.EmitBackEdgeTable();
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000329
lrn@chromium.org34e60782011-09-15 07:25:40 +0000330 Code::Flags flags = Code::ComputeFlags(Code::FUNCTION);
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000331 Handle<Code> code = CodeGenerator::MakeCodeEpilogue(&masm, flags, info);
rossberg@chromium.org2c067b12012-03-19 11:01:52 +0000332 code->set_optimizable(info->IsOptimizable() &&
ulan@chromium.orgd6899c32012-05-18 14:12:25 +0000333 !info->function()->flags()->Contains(kDontOptimize) &&
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000334 info->function()->scope()->AllowsLazyCompilation());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000335 cgen.PopulateDeoptimizationData(code);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000336 cgen.PopulateTypeFeedbackInfo(code);
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000337 cgen.PopulateTypeFeedbackCells(code);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000338 code->set_has_deoptimization_support(info->HasDeoptimizationSupport());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +0000339 code->set_handler_table(*cgen.handler_table());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000340#ifdef ENABLE_DEBUGGER_SUPPORT
lrn@chromium.org34e60782011-09-15 07:25:40 +0000341 code->set_has_debug_break_slots(
342 info->isolate()->debugger()->IsDebuggerActive());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000343 code->set_compiled_optimizable(info->IsOptimizable());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000344#endif // ENABLE_DEBUGGER_SUPPORT
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000345 code->set_allow_osr_at_loop_nesting_level(0);
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000346 code->set_profiler_ticks(0);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000347 code->set_back_edge_table_offset(table_offset);
348 code->set_back_edges_patched_for_osr(false);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000349 CodeGenerator::PrintCode(code, info);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000350 info->SetCode(code); // May be an empty handle.
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000351#ifdef ENABLE_GDB_JIT_INTERFACE
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000352 if (FLAG_gdbjit && !code.is_null()) {
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000353 GDBJITLineInfo* lineinfo =
354 masm.positions_recorder()->DetachGDBJITLineInfo();
355
356 GDBJIT(RegisterDetailedLineInfo(*code, lineinfo));
357 }
358#endif
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000359 if (!code.is_null()) {
360 void* line_info =
361 masm.positions_recorder()->DetachJITHandlerData();
362 LOG_CODE_EVENT(isolate, CodeEndLinePosInfoRecordEvent(*code, line_info));
363 }
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000364 return !code.is_null();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000365}
366
367
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000368unsigned FullCodeGenerator::EmitBackEdgeTable() {
369 // The back edge table consists of a length (in number of entries)
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000370 // field, and then a sequence of entries. Each entry is a pair of AST id
371 // and code-relative pc offset.
372 masm()->Align(kIntSize);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000373 unsigned offset = masm()->pc_offset();
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000374 unsigned length = back_edges_.length();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000375 __ dd(length);
376 for (unsigned i = 0; i < length; ++i) {
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000377 __ dd(back_edges_[i].id.ToInt());
378 __ dd(back_edges_[i].pc);
379 __ db(back_edges_[i].loop_depth);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000380 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000381 return offset;
382}
383
384
385void FullCodeGenerator::PopulateDeoptimizationData(Handle<Code> code) {
386 // Fill in the deoptimization information.
387 ASSERT(info_->HasDeoptimizationSupport() || bailout_entries_.is_empty());
388 if (!info_->HasDeoptimizationSupport()) return;
389 int length = bailout_entries_.length();
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000390 Handle<DeoptimizationOutputData> data = isolate()->factory()->
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000391 NewDeoptimizationOutputData(length, TENURED);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000392 for (int i = 0; i < length; i++) {
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000393 data->SetAstId(i, bailout_entries_[i].id);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000394 data->SetPcAndState(i, Smi::FromInt(bailout_entries_[i].pc_and_state));
395 }
396 code->set_deoptimization_data(*data);
397}
398
399
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000400void FullCodeGenerator::PopulateTypeFeedbackInfo(Handle<Code> code) {
401 Handle<TypeFeedbackInfo> info = isolate()->factory()->NewTypeFeedbackInfo();
402 info->set_ic_total_count(ic_total_count_);
yangguo@chromium.orga7d3df92012-02-27 11:46:55 +0000403 ASSERT(!isolate()->heap()->InNewSpace(*info));
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000404 code->set_type_feedback_info(*info);
405}
406
407
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +0000408void FullCodeGenerator::Initialize() {
409 // The generation of debug code must match between the snapshot code and the
410 // code that is generated later. This is assumed by the debugger when it is
411 // calculating PC offsets after generating a debug version of code. Therefore
412 // we disable the production of debug code in the full compiler if we are
413 // either generating a snapshot or we booted from a snapshot.
414 generate_debug_code_ = FLAG_debug_code &&
415 !Serializer::enabled() &&
416 !Snapshot::HaveASnapshotToStartFrom();
417 masm_->set_emit_debug_code(generate_debug_code_);
418 masm_->set_predictable_code_size(true);
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000419 InitializeAstVisitor();
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +0000420}
421
422
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000423void FullCodeGenerator::PopulateTypeFeedbackCells(Handle<Code> code) {
424 if (type_feedback_cells_.is_empty()) return;
425 int length = type_feedback_cells_.length();
426 int array_size = TypeFeedbackCells::LengthOfFixedArray(length);
427 Handle<TypeFeedbackCells> cache = Handle<TypeFeedbackCells>::cast(
428 isolate()->factory()->NewFixedArray(array_size, TENURED));
429 for (int i = 0; i < length; i++) {
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000430 cache->SetAstId(i, type_feedback_cells_[i].ast_id);
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000431 cache->SetCell(i, *type_feedback_cells_[i].cell);
432 }
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000433 TypeFeedbackInfo::cast(code->type_feedback_info())->set_type_feedback_cells(
434 *cache);
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000435}
436
437
438
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000439void FullCodeGenerator::PrepareForBailout(Expression* node, State state) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000440 PrepareForBailoutForId(node->id(), state);
441}
442
443
444void FullCodeGenerator::RecordJSReturnSite(Call* call) {
445 // We record the offset of the function return so we can rebuild the frame
446 // if the function was inlined, i.e., this is the return address in the
447 // inlined function's frame.
448 //
449 // The state is ignored. We defensively set it to TOS_REG, which is the
450 // real state of the unoptimized code at the return site.
451 PrepareForBailoutForId(call->ReturnId(), TOS_REG);
452#ifdef DEBUG
453 // In debug builds, mark the return so we can verify that this function
454 // was called.
455 ASSERT(!call->return_is_recorded_);
456 call->return_is_recorded_ = true;
457#endif
458}
459
460
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000461void FullCodeGenerator::PrepareForBailoutForId(BailoutId id, State state) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000462 // There's no need to prepare this code for bailouts from already optimized
463 // code or code that can't be optimized.
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000464 if (!info_->HasDeoptimizationSupport()) return;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000465 unsigned pc_and_state =
466 StateField::encode(state) | PcField::encode(masm_->pc_offset());
yangguo@chromium.org56454712012-02-16 15:33:53 +0000467 ASSERT(Smi::IsValid(pc_and_state));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000468 BailoutEntry entry = { id, pc_and_state };
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000469 ASSERT(!prepared_bailout_ids_.Contains(id.ToInt()));
470 prepared_bailout_ids_.Add(id.ToInt(), zone());
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000471 bailout_entries_.Add(entry, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000472}
473
474
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000475void FullCodeGenerator::RecordTypeFeedbackCell(
danno@chromium.org41728482013-06-12 22:31:22 +0000476 TypeFeedbackId id, Handle<Cell> cell) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000477 TypeFeedbackCellEntry entry = { id, cell };
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000478 type_feedback_cells_.Add(entry, zone());
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000479}
480
481
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +0000482void FullCodeGenerator::RecordBackEdge(BailoutId ast_id) {
483 // The pc offset does not need to be encoded and packed together with a state.
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +0000484 ASSERT(masm_->pc_offset() > 0);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000485 ASSERT(loop_depth() > 0);
486 uint8_t depth = Min(loop_depth(), Code::kMaxLoopNestingMarker);
487 BackEdgeEntry entry =
488 { ast_id, static_cast<unsigned>(masm_->pc_offset()), depth };
489 back_edges_.Add(entry, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000490}
491
492
ricow@chromium.org65fae842010-08-25 15:26:24 +0000493bool FullCodeGenerator::ShouldInlineSmiCase(Token::Value op) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000494 // Inline smi case inside loops, but not division and modulo which
495 // are too complicated and take up too much space.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000496 if (op == Token::DIV ||op == Token::MOD) return false;
497 if (FLAG_always_inline_smi_code) return true;
498 return loop_depth_ > 0;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000499}
500
501
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000502void FullCodeGenerator::EffectContext::Plug(Register reg) const {
503}
504
505
506void FullCodeGenerator::AccumulatorValueContext::Plug(Register reg) const {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000507 __ Move(result_register(), reg);
508}
509
510
511void FullCodeGenerator::StackValueContext::Plug(Register reg) const {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000512 __ push(reg);
513}
514
515
516void FullCodeGenerator::TestContext::Plug(Register reg) const {
517 // For simplicity we always test the accumulator register.
518 __ Move(result_register(), reg);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000519 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000520 codegen()->DoTest(this);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000521}
522
523
524void FullCodeGenerator::EffectContext::PlugTOS() const {
525 __ Drop(1);
526}
527
528
529void FullCodeGenerator::AccumulatorValueContext::PlugTOS() const {
530 __ pop(result_register());
531}
532
533
534void FullCodeGenerator::StackValueContext::PlugTOS() const {
535}
536
537
538void FullCodeGenerator::TestContext::PlugTOS() const {
539 // For simplicity we always test the accumulator register.
540 __ pop(result_register());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000541 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000542 codegen()->DoTest(this);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000543}
544
545
546void FullCodeGenerator::EffectContext::PrepareTest(
547 Label* materialize_true,
548 Label* materialize_false,
549 Label** if_true,
550 Label** if_false,
551 Label** fall_through) const {
552 // In an effect context, the true and the false case branch to the
553 // same label.
554 *if_true = *if_false = *fall_through = materialize_true;
555}
556
557
558void FullCodeGenerator::AccumulatorValueContext::PrepareTest(
559 Label* materialize_true,
560 Label* materialize_false,
561 Label** if_true,
562 Label** if_false,
563 Label** fall_through) const {
564 *if_true = *fall_through = materialize_true;
565 *if_false = materialize_false;
566}
567
568
569void FullCodeGenerator::StackValueContext::PrepareTest(
570 Label* materialize_true,
571 Label* materialize_false,
572 Label** if_true,
573 Label** if_false,
574 Label** fall_through) const {
575 *if_true = *fall_through = materialize_true;
576 *if_false = materialize_false;
577}
578
579
580void FullCodeGenerator::TestContext::PrepareTest(
581 Label* materialize_true,
582 Label* materialize_false,
583 Label** if_true,
584 Label** if_false,
585 Label** fall_through) const {
586 *if_true = true_label_;
587 *if_false = false_label_;
588 *fall_through = fall_through_;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000589}
590
591
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000592void FullCodeGenerator::DoTest(const TestContext* context) {
593 DoTest(context->condition(),
594 context->true_label(),
595 context->false_label(),
596 context->fall_through());
597}
598
599
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000600void FullCodeGenerator::AllocateModules(ZoneList<Declaration*>* declarations) {
601 ASSERT(scope_->is_global_scope());
602
603 for (int i = 0; i < declarations->length(); i++) {
604 ModuleDeclaration* declaration = declarations->at(i)->AsModuleDeclaration();
605 if (declaration != NULL) {
606 ModuleLiteral* module = declaration->module()->AsModuleLiteral();
607 if (module != NULL) {
608 Comment cmnt(masm_, "[ Link nested modules");
609 Scope* scope = module->body()->scope();
610 Interface* interface = scope->interface();
611 ASSERT(interface->IsModule() && interface->IsFrozen());
612
613 interface->Allocate(scope->module_var()->index());
614
615 // Set up module context.
616 ASSERT(scope->interface()->Index() >= 0);
617 __ Push(Smi::FromInt(scope->interface()->Index()));
618 __ Push(scope->GetScopeInfo());
619 __ CallRuntime(Runtime::kPushModuleContext, 2);
620 StoreToFrameField(StandardFrameConstants::kContextOffset,
621 context_register());
622
623 AllocateModules(scope->declarations());
624
625 // Pop module context.
626 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
627 // Update local stack frame context field.
628 StoreToFrameField(StandardFrameConstants::kContextOffset,
629 context_register());
630 }
631 }
632 }
633}
634
635
636// Modules have their own local scope, represented by their own context.
637// Module instance objects have an accessor for every export that forwards
638// access to the respective slot from the module's context. (Exports that are
639// modules themselves, however, are simple data properties.)
640//
641// All modules have a _hosting_ scope/context, which (currently) is the
642// (innermost) enclosing global scope. To deal with recursion, nested modules
643// are hosted by the same scope as global ones.
644//
645// For every (global or nested) module literal, the hosting context has an
646// internal slot that points directly to the respective module context. This
647// enables quick access to (statically resolved) module members by 2-dimensional
648// access through the hosting context. For example,
649//
650// module A {
651// let x;
652// module B { let y; }
653// }
654// module C { let z; }
655//
656// allocates contexts as follows:
657//
658// [header| .A | .B | .C | A | C ] (global)
659// | | |
660// | | +-- [header| z ] (module)
661// | |
662// | +------- [header| y ] (module)
663// |
664// +------------ [header| x | B ] (module)
665//
666// Here, .A, .B, .C are the internal slots pointing to the hosted module
667// contexts, whereas A, B, C hold the actual instance objects (note that every
668// module context also points to the respective instance object through its
669// extension slot in the header).
670//
671// To deal with arbitrary recursion and aliases between modules,
672// they are created and initialized in several stages. Each stage applies to
673// all modules in the hosting global scope, including nested ones.
674//
675// 1. Allocate: for each module _literal_, allocate the module contexts and
676// respective instance object and wire them up. This happens in the
677// PushModuleContext runtime function, as generated by AllocateModules
678// (invoked by VisitDeclarations in the hosting scope).
679//
680// 2. Bind: for each module _declaration_ (i.e. literals as well as aliases),
681// assign the respective instance object to respective local variables. This
682// happens in VisitModuleDeclaration, and uses the instance objects created
683// in the previous stage.
684// For each module _literal_, this phase also constructs a module descriptor
685// for the next stage. This happens in VisitModuleLiteral.
686//
687// 3. Populate: invoke the DeclareModules runtime function to populate each
688// _instance_ object with accessors for it exports. This is generated by
689// DeclareModules (invoked by VisitDeclarations in the hosting scope again),
690// and uses the descriptors generated in the previous stage.
691//
692// 4. Initialize: execute the module bodies (and other code) in sequence. This
693// happens by the separate statements generated for module bodies. To reenter
694// the module scopes properly, the parser inserted ModuleStatements.
695
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000696void FullCodeGenerator::VisitDeclarations(
697 ZoneList<Declaration*>* declarations) {
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000698 Handle<FixedArray> saved_modules = modules_;
699 int saved_module_index = module_index_;
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000700 ZoneList<Handle<Object> >* saved_globals = globals_;
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000701 ZoneList<Handle<Object> > inner_globals(10, zone());
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000702 globals_ = &inner_globals;
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000703
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000704 if (scope_->num_modules() != 0) {
705 // This is a scope hosting modules. Allocate a descriptor array to pass
706 // to the runtime for initialization.
707 Comment cmnt(masm_, "[ Allocate modules");
708 ASSERT(scope_->is_global_scope());
709 modules_ =
710 isolate()->factory()->NewFixedArray(scope_->num_modules(), TENURED);
711 module_index_ = 0;
712
713 // Generate code for allocating all modules, including nested ones.
714 // The allocated contexts are stored in internal variables in this scope.
715 AllocateModules(declarations);
716 }
717
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000718 AstVisitor::VisitDeclarations(declarations);
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000719
720 if (scope_->num_modules() != 0) {
721 // Initialize modules from descriptor array.
722 ASSERT(module_index_ == modules_->length());
723 DeclareModules(modules_);
724 modules_ = saved_modules;
725 module_index_ = saved_module_index;
726 }
727
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000728 if (!globals_->is_empty()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000729 // Invoke the platform-dependent code generator to do the actual
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000730 // declaration of the global functions and variables.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000731 Handle<FixedArray> array =
732 isolate()->factory()->NewFixedArray(globals_->length(), TENURED);
733 for (int i = 0; i < globals_->length(); ++i)
734 array->set(i, *globals_->at(i));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000735 DeclareGlobals(array);
736 }
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000737
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000738 globals_ = saved_globals;
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000739}
740
741
742void FullCodeGenerator::VisitModuleLiteral(ModuleLiteral* module) {
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000743 Block* block = module->body();
744 Scope* saved_scope = scope();
745 scope_ = block->scope();
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000746 Interface* interface = scope_->interface();
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000747
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000748 Comment cmnt(masm_, "[ ModuleLiteral");
749 SetStatementPosition(block);
750
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000751 ASSERT(!modules_.is_null());
752 ASSERT(module_index_ < modules_->length());
753 int index = module_index_++;
754
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000755 // Set up module context.
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000756 ASSERT(interface->Index() >= 0);
757 __ Push(Smi::FromInt(interface->Index()));
758 __ Push(Smi::FromInt(0));
759 __ CallRuntime(Runtime::kPushModuleContext, 2);
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000760 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000761
762 {
763 Comment cmnt(masm_, "[ Declarations");
764 VisitDeclarations(scope_->declarations());
765 }
766
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000767 // Populate the module description.
768 Handle<ModuleInfo> description =
769 ModuleInfo::Create(isolate(), interface, scope_);
770 modules_->set(index, *description);
771
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000772 scope_ = saved_scope;
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000773 // Pop module context.
774 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
775 // Update local stack frame context field.
776 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000777}
778
779
780void FullCodeGenerator::VisitModuleVariable(ModuleVariable* 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
786void FullCodeGenerator::VisitModulePath(ModulePath* module) {
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000787 // Nothing to do.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000788 // The instance object is resolved statically through the module's interface.
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000789}
790
791
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000792void FullCodeGenerator::VisitModuleUrl(ModuleUrl* module) {
793 // TODO(rossberg): dummy allocation for now.
794 Scope* scope = module->body()->scope();
795 Interface* interface = scope_->interface();
796
797 ASSERT(interface->IsModule() && interface->IsFrozen());
798 ASSERT(!modules_.is_null());
799 ASSERT(module_index_ < modules_->length());
800 interface->Allocate(scope->module_var()->index());
801 int index = module_index_++;
802
803 Handle<ModuleInfo> description =
804 ModuleInfo::Create(isolate(), interface, scope_);
805 modules_->set(index, *description);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000806}
807
808
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000809int FullCodeGenerator::DeclareGlobalsFlags() {
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000810 ASSERT(DeclareGlobalsLanguageMode::is_valid(language_mode()));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000811 return DeclareGlobalsEvalFlag::encode(is_eval()) |
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000812 DeclareGlobalsNativeFlag::encode(is_native()) |
813 DeclareGlobalsLanguageMode::encode(language_mode());
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000814}
815
816
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000817void FullCodeGenerator::SetFunctionPosition(FunctionLiteral* fun) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000818 CodeGenerator::RecordPositions(masm_, fun->start_position());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000819}
820
821
822void FullCodeGenerator::SetReturnPosition(FunctionLiteral* fun) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000823 CodeGenerator::RecordPositions(masm_, fun->end_position() - 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000824}
825
826
827void FullCodeGenerator::SetStatementPosition(Statement* stmt) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000828#ifdef ENABLE_DEBUGGER_SUPPORT
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000829 if (!isolate()->debugger()->IsDebuggerActive()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000830 CodeGenerator::RecordPositions(masm_, stmt->statement_pos());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000831 } else {
832 // Check if the statement will be breakable without adding a debug break
833 // slot.
834 BreakableStatementChecker checker;
835 checker.Check(stmt);
836 // Record the statement position right here if the statement is not
837 // breakable. For breakable statements the actual recording of the
838 // position will be postponed to the breakable code (typically an IC).
839 bool position_recorded = CodeGenerator::RecordPositions(
840 masm_, stmt->statement_pos(), !checker.is_breakable());
841 // If the position recording did record a new position generate a debug
842 // break slot to make the statement breakable.
843 if (position_recorded) {
844 Debug::GenerateSlot(masm_);
845 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000846 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000847#else
848 CodeGenerator::RecordPositions(masm_, stmt->statement_pos());
849#endif
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000850}
851
852
853void FullCodeGenerator::SetExpressionPosition(Expression* expr, int pos) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000854#ifdef ENABLE_DEBUGGER_SUPPORT
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000855 if (!isolate()->debugger()->IsDebuggerActive()) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000856 CodeGenerator::RecordPositions(masm_, pos);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000857 } else {
858 // Check if the expression will be breakable without adding a debug break
859 // slot.
860 BreakableStatementChecker checker;
861 checker.Check(expr);
862 // Record a statement position right here if the expression is not
863 // breakable. For breakable expressions the actual recording of the
864 // position will be postponed to the breakable code (typically an IC).
865 // NOTE this will record a statement position for something which might
866 // not be a statement. As stepping in the debugger will only stop at
867 // statement positions this is used for e.g. the condition expression of
868 // a do while loop.
869 bool position_recorded = CodeGenerator::RecordPositions(
870 masm_, pos, !checker.is_breakable());
871 // If the position recording did record a new position generate a debug
872 // break slot to make the statement breakable.
873 if (position_recorded) {
874 Debug::GenerateSlot(masm_);
875 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000876 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000877#else
878 CodeGenerator::RecordPositions(masm_, pos);
879#endif
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000880}
881
882
883void FullCodeGenerator::SetStatementPosition(int pos) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000884 CodeGenerator::RecordPositions(masm_, pos);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000885}
886
887
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000888void FullCodeGenerator::SetSourcePosition(int pos) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000889 if (pos != RelocInfo::kNoPosition) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000890 masm_->positions_recorder()->RecordPosition(pos);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000891 }
892}
893
894
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000895// Lookup table for code generators for special runtime calls which are
896// generated inline.
897#define INLINE_FUNCTION_GENERATOR_ADDRESS(Name, argc, ressize) \
898 &FullCodeGenerator::Emit##Name,
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000899
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000900const FullCodeGenerator::InlineFunctionGenerator
901 FullCodeGenerator::kInlineFunctionGenerators[] = {
902 INLINE_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS)
903 INLINE_RUNTIME_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS)
904 };
905#undef INLINE_FUNCTION_GENERATOR_ADDRESS
906
907
908FullCodeGenerator::InlineFunctionGenerator
909 FullCodeGenerator::FindInlineFunctionGenerator(Runtime::FunctionId id) {
whesse@chromium.org023421e2010-12-21 12:19:12 +0000910 int lookup_index =
911 static_cast<int>(id) - static_cast<int>(Runtime::kFirstInlineFunction);
912 ASSERT(lookup_index >= 0);
913 ASSERT(static_cast<size_t>(lookup_index) <
914 ARRAY_SIZE(kInlineFunctionGenerators));
915 return kInlineFunctionGenerators[lookup_index];
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000916}
917
918
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000919void FullCodeGenerator::EmitInlineRuntimeCall(CallRuntime* expr) {
920 const Runtime::Function* function = expr->function();
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000921 ASSERT(function != NULL);
922 ASSERT(function->intrinsic_type == Runtime::INLINE);
923 InlineFunctionGenerator generator =
924 FindInlineFunctionGenerator(function->function_id);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000925 ((*this).*(generator))(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000926}
927
928
verwaest@chromium.org8a00e822013-06-10 15:11:22 +0000929void FullCodeGenerator::EmitGeneratorNext(CallRuntime* expr) {
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000930 ZoneList<Expression*>* args = expr->arguments();
931 ASSERT(args->length() == 2);
verwaest@chromium.org8a00e822013-06-10 15:11:22 +0000932 EmitGeneratorResume(args->at(0), args->at(1), JSGeneratorObject::NEXT);
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000933}
934
935
936void FullCodeGenerator::EmitGeneratorThrow(CallRuntime* expr) {
937 ZoneList<Expression*>* args = expr->arguments();
938 ASSERT(args->length() == 2);
939 EmitGeneratorResume(args->at(0), args->at(1), JSGeneratorObject::THROW);
940}
941
942
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +0000943void FullCodeGenerator::EmitDebugBreakInOptimizedCode(CallRuntime* expr) {
944 context()->Plug(handle(Smi::FromInt(0), isolate()));
945}
946
947
ricow@chromium.org65fae842010-08-25 15:26:24 +0000948void FullCodeGenerator::VisitBinaryOperation(BinaryOperation* expr) {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000949 switch (expr->op()) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000950 case Token::COMMA:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000951 return VisitComma(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000952 case Token::OR:
953 case Token::AND:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000954 return VisitLogicalExpression(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000955 default:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000956 return VisitArithmeticExpression(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000957 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000958}
959
960
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000961void FullCodeGenerator::VisitInDuplicateContext(Expression* expr) {
962 if (context()->IsEffect()) {
963 VisitForEffect(expr);
964 } else if (context()->IsAccumulatorValue()) {
965 VisitForAccumulatorValue(expr);
966 } else if (context()->IsStackValue()) {
967 VisitForStackValue(expr);
968 } else if (context()->IsTest()) {
969 const TestContext* test = TestContext::cast(context());
970 VisitForControl(expr, test->true_label(), test->false_label(),
971 test->fall_through());
972 }
973}
974
975
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000976void FullCodeGenerator::VisitComma(BinaryOperation* expr) {
977 Comment cmnt(masm_, "[ Comma");
978 VisitForEffect(expr->left());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000979 VisitInDuplicateContext(expr->right());
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000980}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000981
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000982
983void FullCodeGenerator::VisitLogicalExpression(BinaryOperation* expr) {
984 bool is_logical_and = expr->op() == Token::AND;
985 Comment cmnt(masm_, is_logical_and ? "[ Logical AND" : "[ Logical OR");
986 Expression* left = expr->left();
987 Expression* right = expr->right();
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000988 BailoutId right_id = expr->RightId();
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000989 Label done;
990
991 if (context()->IsTest()) {
992 Label eval_right;
993 const TestContext* test = TestContext::cast(context());
994 if (is_logical_and) {
995 VisitForControl(left, &eval_right, test->false_label(), &eval_right);
996 } else {
997 VisitForControl(left, test->true_label(), &eval_right, &eval_right);
998 }
999 PrepareForBailoutForId(right_id, NO_REGISTERS);
1000 __ bind(&eval_right);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001001
1002 } else if (context()->IsAccumulatorValue()) {
1003 VisitForAccumulatorValue(left);
1004 // We want the value in the accumulator for the test, and on the stack in
1005 // case we need it.
1006 __ push(result_register());
1007 Label discard, restore;
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001008 if (is_logical_and) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001009 DoTest(left, &discard, &restore, &restore);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001010 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001011 DoTest(left, &restore, &discard, &restore);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001012 }
1013 __ bind(&restore);
1014 __ pop(result_register());
1015 __ jmp(&done);
1016 __ bind(&discard);
1017 __ Drop(1);
1018 PrepareForBailoutForId(right_id, NO_REGISTERS);
1019
1020 } else if (context()->IsStackValue()) {
1021 VisitForAccumulatorValue(left);
1022 // We want the value in the accumulator for the test, and on the stack in
1023 // case we need it.
1024 __ push(result_register());
1025 Label discard;
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001026 if (is_logical_and) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001027 DoTest(left, &discard, &done, &discard);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001028 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001029 DoTest(left, &done, &discard, &discard);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001030 }
1031 __ bind(&discard);
1032 __ Drop(1);
1033 PrepareForBailoutForId(right_id, NO_REGISTERS);
1034
1035 } else {
1036 ASSERT(context()->IsEffect());
1037 Label eval_right;
1038 if (is_logical_and) {
1039 VisitForControl(left, &eval_right, &done, &eval_right);
1040 } else {
1041 VisitForControl(left, &done, &eval_right, &eval_right);
1042 }
1043 PrepareForBailoutForId(right_id, NO_REGISTERS);
1044 __ bind(&eval_right);
1045 }
1046
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001047 VisitInDuplicateContext(right);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001048 __ bind(&done);
1049}
1050
1051
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001052void FullCodeGenerator::VisitArithmeticExpression(BinaryOperation* expr) {
1053 Token::Value op = expr->op();
1054 Comment cmnt(masm_, "[ ArithmeticExpression");
1055 Expression* left = expr->left();
1056 Expression* right = expr->right();
1057 OverwriteMode mode =
1058 left->ResultOverwriteAllowed()
1059 ? OVERWRITE_LEFT
1060 : (right->ResultOverwriteAllowed() ? OVERWRITE_RIGHT : NO_OVERWRITE);
1061
1062 VisitForStackValue(left);
1063 VisitForAccumulatorValue(right);
1064
1065 SetSourcePosition(expr->position());
1066 if (ShouldInlineSmiCase(op)) {
1067 EmitInlineSmiBinaryOp(expr, op, mode, left, right);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001068 } else {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001069 EmitBinaryOp(expr, op, mode);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001070 }
1071}
1072
1073
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001074void FullCodeGenerator::VisitBlock(Block* stmt) {
1075 Comment cmnt(masm_, "[ Block");
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001076 NestedBlock nested_block(this, stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001077 SetStatementPosition(stmt);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001078
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001079 Scope* saved_scope = scope();
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001080 // Push a block context when entering a block with block scoped variables.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00001081 if (stmt->scope() != NULL) {
danno@chromium.org81cac2b2012-07-10 11:28:27 +00001082 scope_ = stmt->scope();
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00001083 ASSERT(!scope_->is_module_scope());
1084 { Comment cmnt(masm_, "[ Extend block context");
1085 Handle<ScopeInfo> scope_info = scope_->GetScopeInfo();
1086 int heap_slots = scope_info->ContextLength() - Context::MIN_CONTEXT_SLOTS;
1087 __ Push(scope_info);
1088 PushFunctionArgumentForContextAllocation();
1089 if (heap_slots <= FastNewBlockContextStub::kMaximumSlots) {
1090 FastNewBlockContextStub stub(heap_slots);
1091 __ CallStub(&stub);
1092 } else {
1093 __ CallRuntime(Runtime::kPushBlockContext, 2);
1094 }
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001095
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00001096 // Replace the context stored in the frame.
1097 StoreToFrameField(StandardFrameConstants::kContextOffset,
1098 context_register());
1099 }
1100 { Comment cmnt(masm_, "[ Declarations");
1101 VisitDeclarations(scope_->declarations());
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001102 }
1103 }
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00001104
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00001105 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001106 VisitStatements(stmt->statements());
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001107 scope_ = saved_scope;
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001108 __ bind(nested_block.break_label());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001109 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001110
1111 // Pop block context if necessary.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00001112 if (stmt->scope() != NULL) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001113 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1114 // Update local stack frame context field.
1115 StoreToFrameField(StandardFrameConstants::kContextOffset,
1116 context_register());
1117 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001118}
1119
1120
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00001121void FullCodeGenerator::VisitModuleStatement(ModuleStatement* stmt) {
1122 Comment cmnt(masm_, "[ Module context");
1123
1124 __ Push(Smi::FromInt(stmt->proxy()->interface()->Index()));
1125 __ Push(Smi::FromInt(0));
1126 __ CallRuntime(Runtime::kPushModuleContext, 2);
1127 StoreToFrameField(
1128 StandardFrameConstants::kContextOffset, context_register());
1129
1130 Scope* saved_scope = scope_;
1131 scope_ = stmt->body()->scope();
1132 VisitStatements(stmt->body()->statements());
1133 scope_ = saved_scope;
1134 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1135 // Update local stack frame context field.
1136 StoreToFrameField(StandardFrameConstants::kContextOffset,
1137 context_register());
1138}
1139
1140
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001141void FullCodeGenerator::VisitExpressionStatement(ExpressionStatement* stmt) {
1142 Comment cmnt(masm_, "[ ExpressionStatement");
1143 SetStatementPosition(stmt);
1144 VisitForEffect(stmt->expression());
1145}
1146
1147
1148void FullCodeGenerator::VisitEmptyStatement(EmptyStatement* stmt) {
1149 Comment cmnt(masm_, "[ EmptyStatement");
1150 SetStatementPosition(stmt);
1151}
1152
1153
1154void FullCodeGenerator::VisitIfStatement(IfStatement* stmt) {
1155 Comment cmnt(masm_, "[ IfStatement");
1156 SetStatementPosition(stmt);
1157 Label then_part, else_part, done;
1158
ricow@chromium.org65fae842010-08-25 15:26:24 +00001159 if (stmt->HasElseStatement()) {
1160 VisitForControl(stmt->condition(), &then_part, &else_part, &then_part);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001161 PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001162 __ bind(&then_part);
1163 Visit(stmt->then_statement());
1164 __ jmp(&done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001165
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001166 PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001167 __ bind(&else_part);
1168 Visit(stmt->else_statement());
1169 } else {
1170 VisitForControl(stmt->condition(), &then_part, &done, &then_part);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001171 PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001172 __ bind(&then_part);
1173 Visit(stmt->then_statement());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001174
1175 PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001176 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001177 __ bind(&done);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001178 PrepareForBailoutForId(stmt->IfId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001179}
1180
1181
1182void FullCodeGenerator::VisitContinueStatement(ContinueStatement* stmt) {
1183 Comment cmnt(masm_, "[ ContinueStatement");
1184 SetStatementPosition(stmt);
1185 NestedStatement* current = nesting_stack_;
1186 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001187 int context_length = 0;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001188 // When continuing, we clobber the unpredictable value in the accumulator
1189 // with one that's safe for GC. If we hit an exit from the try block of
1190 // try...finally on our way out, we will unconditionally preserve the
1191 // accumulator on the stack.
1192 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001193 while (!current->IsContinueTarget(stmt->target())) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001194 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001195 }
1196 __ Drop(stack_depth);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001197 if (context_length > 0) {
1198 while (context_length > 0) {
1199 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1200 --context_length;
1201 }
1202 StoreToFrameField(StandardFrameConstants::kContextOffset,
1203 context_register());
1204 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001205
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001206 __ jmp(current->AsIteration()->continue_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001207}
1208
1209
1210void FullCodeGenerator::VisitBreakStatement(BreakStatement* stmt) {
1211 Comment cmnt(masm_, "[ BreakStatement");
1212 SetStatementPosition(stmt);
1213 NestedStatement* current = nesting_stack_;
1214 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001215 int context_length = 0;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001216 // When breaking, we clobber the unpredictable value in the accumulator
1217 // with one that's safe for GC. If we hit an exit from the try block of
1218 // try...finally on our way out, we will unconditionally preserve the
1219 // accumulator on the stack.
1220 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001221 while (!current->IsBreakTarget(stmt->target())) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001222 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001223 }
1224 __ Drop(stack_depth);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001225 if (context_length > 0) {
1226 while (context_length > 0) {
1227 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1228 --context_length;
1229 }
1230 StoreToFrameField(StandardFrameConstants::kContextOffset,
1231 context_register());
1232 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001233
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001234 __ jmp(current->AsBreakable()->break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001235}
1236
1237
danno@chromium.org41728482013-06-12 22:31:22 +00001238void FullCodeGenerator::EmitUnwindBeforeReturn() {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001239 NestedStatement* current = nesting_stack_;
1240 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001241 int context_length = 0;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001242 while (current != NULL) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001243 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001244 }
1245 __ Drop(stack_depth);
danno@chromium.org41728482013-06-12 22:31:22 +00001246}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001247
danno@chromium.org41728482013-06-12 22:31:22 +00001248
1249void FullCodeGenerator::VisitReturnStatement(ReturnStatement* stmt) {
1250 Comment cmnt(masm_, "[ ReturnStatement");
1251 SetStatementPosition(stmt);
1252 Expression* expr = stmt->expression();
1253 VisitForAccumulatorValue(expr);
1254 EmitUnwindBeforeReturn();
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001255 EmitReturnSequence();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001256}
1257
1258
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001259void FullCodeGenerator::VisitWithStatement(WithStatement* stmt) {
1260 Comment cmnt(masm_, "[ WithStatement");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001261 SetStatementPosition(stmt);
1262
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001263 VisitForStackValue(stmt->expression());
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00001264 PushFunctionArgumentForContextAllocation();
1265 __ CallRuntime(Runtime::kPushWithContext, 2);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001266 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001267
danno@chromium.orgca29dd82013-04-26 11:59:48 +00001268 Scope* saved_scope = scope();
1269 scope_ = stmt->scope();
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001270 { WithOrCatch body(this);
1271 Visit(stmt->statement());
1272 }
danno@chromium.orgca29dd82013-04-26 11:59:48 +00001273 scope_ = saved_scope;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001274
1275 // Pop context.
1276 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1277 // Update local stack frame context field.
1278 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001279}
1280
1281
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001282void FullCodeGenerator::VisitDoWhileStatement(DoWhileStatement* stmt) {
1283 Comment cmnt(masm_, "[ DoWhileStatement");
1284 SetStatementPosition(stmt);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001285 Label body, book_keeping;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001286
1287 Iteration loop_statement(this, stmt);
1288 increment_loop_depth();
1289
1290 __ bind(&body);
1291 Visit(stmt->body());
1292
ricow@chromium.org65fae842010-08-25 15:26:24 +00001293 // Record the position of the do while condition and make sure it is
1294 // possible to break on the condition.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001295 __ bind(loop_statement.continue_label());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001296 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001297 SetExpressionPosition(stmt->cond(), stmt->condition_position());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001298 VisitForControl(stmt->cond(),
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001299 &book_keeping,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001300 loop_statement.break_label(),
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001301 &book_keeping);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001302
1303 // Check stack before looping.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001304 PrepareForBailoutForId(stmt->BackEdgeId(), NO_REGISTERS);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001305 __ bind(&book_keeping);
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +00001306 EmitBackEdgeBookkeeping(stmt, &body);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001307 __ jmp(&body);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001308
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001309 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001310 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001311 decrement_loop_depth();
1312}
1313
1314
1315void FullCodeGenerator::VisitWhileStatement(WhileStatement* stmt) {
1316 Comment cmnt(masm_, "[ WhileStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001317 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001318
1319 Iteration loop_statement(this, stmt);
1320 increment_loop_depth();
1321
1322 // Emit the test at the bottom of the loop.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001323 __ jmp(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001324
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001325 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001326 __ bind(&body);
1327 Visit(stmt->body());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001328
1329 // Emit the statement position here as this is where the while
1330 // statement code starts.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001331 __ bind(loop_statement.continue_label());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001332 SetStatementPosition(stmt);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001333
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001334 // Check stack before looping.
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +00001335 EmitBackEdgeBookkeeping(stmt, &body);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001336
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001337 __ bind(&test);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001338 VisitForControl(stmt->cond(),
1339 &body,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001340 loop_statement.break_label(),
1341 loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001342
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001343 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001344 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001345 decrement_loop_depth();
1346}
1347
1348
1349void FullCodeGenerator::VisitForStatement(ForStatement* stmt) {
1350 Comment cmnt(masm_, "[ ForStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001351 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001352
1353 Iteration loop_statement(this, stmt);
erik.corry@gmail.combbceb572012-03-09 10:52:05 +00001354
1355 // Set statement position for a break slot before entering the for-body.
1356 SetStatementPosition(stmt);
1357
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001358 if (stmt->init() != NULL) {
1359 Visit(stmt->init());
1360 }
1361
1362 increment_loop_depth();
1363 // Emit the test at the bottom of the loop (even if empty).
1364 __ jmp(&test);
1365
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001366 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001367 __ bind(&body);
1368 Visit(stmt->body());
1369
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001370 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001371 __ bind(loop_statement.continue_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001372 if (stmt->next() != NULL) {
1373 Visit(stmt->next());
1374 }
1375
ricow@chromium.org65fae842010-08-25 15:26:24 +00001376 // Emit the statement position here as this is where the for
1377 // statement code starts.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001378 SetStatementPosition(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001379
1380 // Check stack before looping.
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +00001381 EmitBackEdgeBookkeeping(stmt, &body);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001382
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001383 __ bind(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001384 if (stmt->cond() != NULL) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00001385 VisitForControl(stmt->cond(),
1386 &body,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001387 loop_statement.break_label(),
1388 loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001389 } else {
1390 __ jmp(&body);
1391 }
1392
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001393 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001394 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001395 decrement_loop_depth();
1396}
1397
1398
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001399void FullCodeGenerator::VisitTryCatchStatement(TryCatchStatement* stmt) {
1400 Comment cmnt(masm_, "[ TryCatchStatement");
1401 SetStatementPosition(stmt);
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001402 // The try block adds a handler to the exception handler chain before
1403 // entering, and removes it again when exiting normally. If an exception
1404 // is thrown during execution of the try block, the handler is consumed
1405 // and control is passed to the catch block with the exception in the
1406 // result register.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001407
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001408 Label try_entry, handler_entry, exit;
1409 __ jmp(&try_entry);
1410 __ bind(&handler_entry);
1411 handler_table()->set(stmt->index(), Smi::FromInt(handler_entry.pos()));
1412 // Exception handler code, the exception is in the result register.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001413 // Extend the context before executing the catch block.
1414 { Comment cmnt(masm_, "[ Extend catch context");
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001415 __ Push(stmt->variable()->name());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001416 __ push(result_register());
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00001417 PushFunctionArgumentForContextAllocation();
1418 __ CallRuntime(Runtime::kPushCatchContext, 3);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001419 StoreToFrameField(StandardFrameConstants::kContextOffset,
1420 context_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001421 }
1422
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001423 Scope* saved_scope = scope();
1424 scope_ = stmt->scope();
1425 ASSERT(scope_->declarations()->is_empty());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001426 { WithOrCatch catch_body(this);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001427 Visit(stmt->catch_block());
1428 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001429 // Restore the context.
1430 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1431 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001432 scope_ = saved_scope;
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001433 __ jmp(&exit);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001434
1435 // Try block code. Sets up the exception handler chain.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001436 __ bind(&try_entry);
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00001437 __ PushTryHandler(StackHandler::CATCH, stmt->index());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001438 { TryCatch try_body(this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001439 Visit(stmt->try_block());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001440 }
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001441 __ PopTryHandler();
1442 __ bind(&exit);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001443}
1444
1445
1446void FullCodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* stmt) {
1447 Comment cmnt(masm_, "[ TryFinallyStatement");
1448 SetStatementPosition(stmt);
1449 // Try finally is compiled by setting up a try-handler on the stack while
1450 // executing the try body, and removing it again afterwards.
1451 //
1452 // The try-finally construct can enter the finally block in three ways:
1453 // 1. By exiting the try-block normally. This removes the try-handler and
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001454 // calls the finally block code before continuing.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001455 // 2. By exiting the try-block with a function-local control flow transfer
1456 // (break/continue/return). The site of the, e.g., break removes the
1457 // try handler and calls the finally block code before continuing
1458 // its outward control transfer.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001459 // 3. By exiting the try-block with a thrown exception.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001460 // This can happen in nested function calls. It traverses the try-handler
1461 // chain and consumes the try-handler entry before jumping to the
1462 // handler code. The handler code then calls the finally-block before
1463 // rethrowing the exception.
1464 //
1465 // The finally block must assume a return address on top of the stack
1466 // (or in the link register on ARM chips) and a value (return value or
1467 // exception) in the result register (rax/eax/r0), both of which must
1468 // be preserved. The return address isn't GC-safe, so it should be
1469 // cooked before GC.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001470 Label try_entry, handler_entry, finally_entry;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001471
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001472 // Jump to try-handler setup and try-block code.
1473 __ jmp(&try_entry);
1474 __ bind(&handler_entry);
1475 handler_table()->set(stmt->index(), Smi::FromInt(handler_entry.pos()));
1476 // Exception handler code. This code is only executed when an exception
1477 // is thrown. The exception is in the result register, and must be
1478 // preserved by the finally block. Call the finally block and then
1479 // rethrow the exception if it returns.
1480 __ Call(&finally_entry);
1481 __ push(result_register());
1482 __ CallRuntime(Runtime::kReThrow, 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001483
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001484 // Finally block implementation.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001485 __ bind(&finally_entry);
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001486 EnterFinallyBlock();
1487 { Finally finally_body(this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001488 Visit(stmt->finally_block());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001489 }
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001490 ExitFinallyBlock(); // Return to the calling code.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001491
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001492 // Set up try handler.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001493 __ bind(&try_entry);
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00001494 __ PushTryHandler(StackHandler::FINALLY, stmt->index());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001495 { TryFinally try_body(this, &finally_entry);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001496 Visit(stmt->try_block());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001497 }
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001498 __ PopTryHandler();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001499 // Execute the finally block on the way out. Clobber the unpredictable
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001500 // value in the result register with one that's safe for GC because the
1501 // finally block will unconditionally preserve the result register on the
1502 // stack.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001503 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001504 __ Call(&finally_entry);
1505}
1506
1507
1508void FullCodeGenerator::VisitDebuggerStatement(DebuggerStatement* stmt) {
1509#ifdef ENABLE_DEBUGGER_SUPPORT
1510 Comment cmnt(masm_, "[ DebuggerStatement");
1511 SetStatementPosition(stmt);
1512
ager@chromium.org5c838252010-02-19 08:53:10 +00001513 __ DebugBreak();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001514 // Ignore the return value.
1515#endif
1516}
1517
1518
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001519void FullCodeGenerator::VisitConditional(Conditional* expr) {
1520 Comment cmnt(masm_, "[ Conditional");
1521 Label true_case, false_case, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001522 VisitForControl(expr->condition(), &true_case, &false_case, &true_case);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001523
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001524 PrepareForBailoutForId(expr->ThenId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001525 __ bind(&true_case);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001526 SetExpressionPosition(expr->then_expression(),
1527 expr->then_expression_position());
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001528 if (context()->IsTest()) {
1529 const TestContext* for_test = TestContext::cast(context());
1530 VisitForControl(expr->then_expression(),
1531 for_test->true_label(),
1532 for_test->false_label(),
1533 NULL);
1534 } else {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001535 VisitInDuplicateContext(expr->then_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001536 __ jmp(&done);
1537 }
1538
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001539 PrepareForBailoutForId(expr->ElseId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001540 __ bind(&false_case);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001541 SetExpressionPosition(expr->else_expression(),
1542 expr->else_expression_position());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001543 VisitInDuplicateContext(expr->else_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001544 // If control flow falls through Visit, merge it with true case here.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001545 if (!context()->IsTest()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001546 __ bind(&done);
1547 }
1548}
1549
1550
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001551void FullCodeGenerator::VisitLiteral(Literal* expr) {
1552 Comment cmnt(masm_, "[ Literal");
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00001553 context()->Plug(expr->value());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001554}
1555
1556
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001557void FullCodeGenerator::VisitFunctionLiteral(FunctionLiteral* expr) {
1558 Comment cmnt(masm_, "[ FunctionLiteral");
1559
1560 // Build the function boilerplate and instantiate it.
1561 Handle<SharedFunctionInfo> function_info =
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001562 Compiler::BuildFunctionInfo(expr, script());
1563 if (function_info.is_null()) {
1564 SetStackOverflow();
1565 return;
1566 }
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001567 EmitNewClosure(function_info, expr->pretenure());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001568}
1569
1570
1571void FullCodeGenerator::VisitSharedFunctionInfoLiteral(
1572 SharedFunctionInfoLiteral* expr) {
1573 Comment cmnt(masm_, "[ SharedFunctionInfoLiteral");
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001574 EmitNewClosure(expr->shared_function_info(), false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001575}
1576
1577
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001578void FullCodeGenerator::VisitThrow(Throw* expr) {
1579 Comment cmnt(masm_, "[ Throw");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001580 VisitForStackValue(expr->exception());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001581 __ CallRuntime(Runtime::kThrow, 1);
1582 // Never returns here.
1583}
1584
1585
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001586FullCodeGenerator::NestedStatement* FullCodeGenerator::TryCatch::Exit(
1587 int* stack_depth,
1588 int* context_length) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001589 // The macros used here must preserve the result register.
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001590 __ Drop(*stack_depth);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001591 __ PopTryHandler();
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001592 *stack_depth = 0;
1593 return previous_;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001594}
1595
ricow@chromium.org65fae842010-08-25 15:26:24 +00001596
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001597bool FullCodeGenerator::TryLiteralCompare(CompareOperation* expr) {
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001598 Expression* sub_expr;
ager@chromium.org04921a82011-06-27 13:21:41 +00001599 Handle<String> check;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001600 if (expr->IsLiteralCompareTypeof(&sub_expr, &check)) {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001601 EmitLiteralCompareTypeof(expr, sub_expr, check);
ager@chromium.org04921a82011-06-27 13:21:41 +00001602 return true;
1603 }
1604
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001605 if (expr->IsLiteralCompareUndefined(&sub_expr)) {
1606 EmitLiteralCompareNil(expr, sub_expr, kUndefinedValue);
1607 return true;
1608 }
1609
1610 if (expr->IsLiteralCompareNull(&sub_expr)) {
1611 EmitLiteralCompareNil(expr, sub_expr, kNullValue);
ager@chromium.org04921a82011-06-27 13:21:41 +00001612 return true;
1613 }
1614
1615 return false;
1616}
1617
1618
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001619#undef __
1620
1621
1622} } // namespace v8::internal