blob: 72d08358455863a8686a788f2ef8d033464959b1 [file] [log] [blame]
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00001// Copyright 2012 the V8 project authors. All rights reserved.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
karlklose@chromium.org44bc7082011-04-11 12:33:05 +000030#include "codegen.h"
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000031#include "compiler.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000032#include "debug.h"
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000033#include "full-codegen.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000034#include "liveedit.h"
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +000035#include "macro-assembler.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000036#include "prettyprinter.h"
sgjesse@chromium.org833cdd72010-02-26 10:06:16 +000037#include "scopes.h"
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +000038#include "scopeinfo.h"
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +000039#include "snapshot.h"
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000040#include "stub-cache.h"
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000041
42namespace v8 {
43namespace internal {
44
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +000045void BreakableStatementChecker::Check(Statement* stmt) {
46 Visit(stmt);
47}
48
49
50void BreakableStatementChecker::Check(Expression* expr) {
51 Visit(expr);
52}
53
54
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +000055void BreakableStatementChecker::VisitVariableDeclaration(
56 VariableDeclaration* decl) {
57}
58
ulan@chromium.org812308e2012-02-29 15:58:45 +000059void BreakableStatementChecker::VisitFunctionDeclaration(
60 FunctionDeclaration* decl) {
61}
62
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +000063void BreakableStatementChecker::VisitModuleDeclaration(
64 ModuleDeclaration* decl) {
65}
66
ulan@chromium.org812308e2012-02-29 15:58:45 +000067void BreakableStatementChecker::VisitImportDeclaration(
68 ImportDeclaration* decl) {
69}
70
71void BreakableStatementChecker::VisitExportDeclaration(
72 ExportDeclaration* decl) {
73}
74
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +000075
76void BreakableStatementChecker::VisitModuleLiteral(ModuleLiteral* module) {
77}
78
79void BreakableStatementChecker::VisitModuleVariable(ModuleVariable* module) {
80}
81
82void BreakableStatementChecker::VisitModulePath(ModulePath* module) {
83}
84
85void BreakableStatementChecker::VisitModuleUrl(ModuleUrl* module) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +000086}
87
88
ulan@chromium.org8e8d8822012-11-23 14:36:46 +000089void BreakableStatementChecker::VisitModuleStatement(ModuleStatement* stmt) {
90}
91
92
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +000093void BreakableStatementChecker::VisitBlock(Block* stmt) {
94}
95
96
97void BreakableStatementChecker::VisitExpressionStatement(
98 ExpressionStatement* stmt) {
99 // Check if expression is breakable.
100 Visit(stmt->expression());
101}
102
103
104void BreakableStatementChecker::VisitEmptyStatement(EmptyStatement* stmt) {
105}
106
107
108void BreakableStatementChecker::VisitIfStatement(IfStatement* stmt) {
109 // If the condition is breakable the if statement is breakable.
110 Visit(stmt->condition());
111}
112
113
114void BreakableStatementChecker::VisitContinueStatement(
115 ContinueStatement* stmt) {
116}
117
118
119void BreakableStatementChecker::VisitBreakStatement(BreakStatement* stmt) {
120}
121
122
123void BreakableStatementChecker::VisitReturnStatement(ReturnStatement* stmt) {
124 // Return is breakable if the expression is.
125 Visit(stmt->expression());
126}
127
128
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000129void BreakableStatementChecker::VisitWithStatement(WithStatement* stmt) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000130 Visit(stmt->expression());
131}
132
133
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000134void BreakableStatementChecker::VisitSwitchStatement(SwitchStatement* stmt) {
135 // Switch statements breakable if the tag expression is.
136 Visit(stmt->tag());
137}
138
139
140void BreakableStatementChecker::VisitDoWhileStatement(DoWhileStatement* stmt) {
141 // Mark do while as breakable to avoid adding a break slot in front of it.
142 is_breakable_ = true;
143}
144
145
146void BreakableStatementChecker::VisitWhileStatement(WhileStatement* stmt) {
147 // Mark while statements breakable if the condition expression is.
148 Visit(stmt->cond());
149}
150
151
152void BreakableStatementChecker::VisitForStatement(ForStatement* stmt) {
153 // Mark for statements breakable if the condition expression is.
154 if (stmt->cond() != NULL) {
155 Visit(stmt->cond());
156 }
157}
158
159
160void BreakableStatementChecker::VisitForInStatement(ForInStatement* stmt) {
161 // Mark for in statements breakable if the enumerable expression is.
162 Visit(stmt->enumerable());
163}
164
165
166void BreakableStatementChecker::VisitTryCatchStatement(
167 TryCatchStatement* stmt) {
168 // Mark try catch as breakable to avoid adding a break slot in front of it.
169 is_breakable_ = true;
170}
171
172
173void BreakableStatementChecker::VisitTryFinallyStatement(
174 TryFinallyStatement* stmt) {
175 // Mark try finally as breakable to avoid adding a break slot in front of it.
176 is_breakable_ = true;
177}
178
179
180void BreakableStatementChecker::VisitDebuggerStatement(
181 DebuggerStatement* stmt) {
182 // The debugger statement is breakable.
183 is_breakable_ = true;
184}
185
186
187void BreakableStatementChecker::VisitFunctionLiteral(FunctionLiteral* expr) {
188}
189
190
191void BreakableStatementChecker::VisitSharedFunctionInfoLiteral(
192 SharedFunctionInfoLiteral* expr) {
193}
194
195
196void BreakableStatementChecker::VisitConditional(Conditional* expr) {
197}
198
199
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000200void BreakableStatementChecker::VisitVariableProxy(VariableProxy* expr) {
201}
202
203
204void BreakableStatementChecker::VisitLiteral(Literal* expr) {
205}
206
207
208void BreakableStatementChecker::VisitRegExpLiteral(RegExpLiteral* expr) {
209}
210
211
212void BreakableStatementChecker::VisitObjectLiteral(ObjectLiteral* expr) {
213}
214
215
216void BreakableStatementChecker::VisitArrayLiteral(ArrayLiteral* expr) {
217}
218
219
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000220void BreakableStatementChecker::VisitAssignment(Assignment* expr) {
221 // If assigning to a property (including a global property) the assignment is
222 // breakable.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000223 VariableProxy* proxy = expr->target()->AsVariableProxy();
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000224 Property* prop = expr->target()->AsProperty();
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000225 if (prop != NULL || (proxy != NULL && proxy->var()->IsUnallocated())) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000226 is_breakable_ = true;
227 return;
228 }
229
230 // Otherwise the assignment is breakable if the assigned value is.
231 Visit(expr->value());
232}
233
234
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000235void BreakableStatementChecker::VisitYield(Yield* expr) {
236 // Yield is breakable if the expression is.
237 Visit(expr->expression());
238}
239
240
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000241void BreakableStatementChecker::VisitThrow(Throw* expr) {
242 // Throw is breakable if the expression is.
243 Visit(expr->exception());
244}
245
246
247void BreakableStatementChecker::VisitProperty(Property* expr) {
248 // Property load is breakable.
249 is_breakable_ = true;
250}
251
252
253void BreakableStatementChecker::VisitCall(Call* expr) {
254 // Function calls both through IC and call stub are breakable.
255 is_breakable_ = true;
256}
257
258
259void BreakableStatementChecker::VisitCallNew(CallNew* expr) {
260 // Function calls through new are breakable.
261 is_breakable_ = true;
262}
263
264
265void BreakableStatementChecker::VisitCallRuntime(CallRuntime* expr) {
266}
267
268
269void BreakableStatementChecker::VisitUnaryOperation(UnaryOperation* expr) {
270 Visit(expr->expression());
271}
272
273
274void BreakableStatementChecker::VisitCountOperation(CountOperation* expr) {
275 Visit(expr->expression());
276}
277
278
279void BreakableStatementChecker::VisitBinaryOperation(BinaryOperation* expr) {
280 Visit(expr->left());
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +0000281 if (expr->op() != Token::AND &&
282 expr->op() != Token::OR) {
283 Visit(expr->right());
284 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000285}
286
287
288void BreakableStatementChecker::VisitCompareOperation(CompareOperation* expr) {
289 Visit(expr->left());
290 Visit(expr->right());
291}
292
293
294void BreakableStatementChecker::VisitThisFunction(ThisFunction* expr) {
295}
296
297
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000298#define __ ACCESS_MASM(masm())
299
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000300bool FullCodeGenerator::MakeCode(CompilationInfo* info) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000301 Isolate* isolate = info->isolate();
ager@chromium.org5c838252010-02-19 08:53:10 +0000302 Handle<Script> script = info->script();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000303 if (!script->IsUndefined() && !script->source()->IsUndefined()) {
304 int len = String::cast(script->source())->length();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000305 isolate->counters()->total_full_codegen_source_size()->Increment(len);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000306 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000307 if (FLAG_trace_codegen) {
308 PrintF("Full Compiler - ");
309 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000310 CodeGenerator::MakeCodePrologue(info);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000311 const int kInitialBufferSize = 4 * KB;
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000312 MacroAssembler masm(info->isolate(), NULL, kInitialBufferSize);
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000313#ifdef ENABLE_GDB_JIT_INTERFACE
314 masm.positions_recorder()->StartGDBJITLineInfoRecording();
315#endif
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000316 LOG_CODE_EVENT(isolate,
317 CodeStartLinePosInfoRecordEvent(masm.positions_recorder()));
ager@chromium.org5c838252010-02-19 08:53:10 +0000318
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000319 FullCodeGenerator cgen(&masm, info);
yangguo@chromium.org56454712012-02-16 15:33:53 +0000320 cgen.Generate();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000321 if (cgen.HasStackOverflow()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000322 ASSERT(!isolate->has_pending_exception());
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000323 return false;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000324 }
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000325 unsigned table_offset = cgen.EmitBackEdgeTable();
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000326
lrn@chromium.org34e60782011-09-15 07:25:40 +0000327 Code::Flags flags = Code::ComputeFlags(Code::FUNCTION);
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000328 Handle<Code> code = CodeGenerator::MakeCodeEpilogue(&masm, flags, info);
rossberg@chromium.org2c067b12012-03-19 11:01:52 +0000329 code->set_optimizable(info->IsOptimizable() &&
ulan@chromium.orgd6899c32012-05-18 14:12:25 +0000330 !info->function()->flags()->Contains(kDontOptimize) &&
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000331 info->function()->scope()->AllowsLazyCompilation());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000332 cgen.PopulateDeoptimizationData(code);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000333 cgen.PopulateTypeFeedbackInfo(code);
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000334 cgen.PopulateTypeFeedbackCells(code);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000335 code->set_has_deoptimization_support(info->HasDeoptimizationSupport());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +0000336 code->set_handler_table(*cgen.handler_table());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000337#ifdef ENABLE_DEBUGGER_SUPPORT
lrn@chromium.org34e60782011-09-15 07:25:40 +0000338 code->set_has_debug_break_slots(
339 info->isolate()->debugger()->IsDebuggerActive());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000340 code->set_compiled_optimizable(info->IsOptimizable());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000341#endif // ENABLE_DEBUGGER_SUPPORT
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000342 code->set_allow_osr_at_loop_nesting_level(0);
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000343 code->set_profiler_ticks(0);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000344 code->set_back_edge_table_offset(table_offset);
345 code->set_back_edges_patched_for_osr(false);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000346 CodeGenerator::PrintCode(code, info);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000347 info->SetCode(code); // May be an empty handle.
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000348#ifdef ENABLE_GDB_JIT_INTERFACE
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000349 if (FLAG_gdbjit && !code.is_null()) {
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000350 GDBJITLineInfo* lineinfo =
351 masm.positions_recorder()->DetachGDBJITLineInfo();
352
353 GDBJIT(RegisterDetailedLineInfo(*code, lineinfo));
354 }
355#endif
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000356 if (!code.is_null()) {
357 void* line_info =
358 masm.positions_recorder()->DetachJITHandlerData();
359 LOG_CODE_EVENT(isolate, CodeEndLinePosInfoRecordEvent(*code, line_info));
360 }
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000361 return !code.is_null();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000362}
363
364
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000365unsigned FullCodeGenerator::EmitBackEdgeTable() {
366 // The back edge table consists of a length (in number of entries)
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000367 // field, and then a sequence of entries. Each entry is a pair of AST id
368 // and code-relative pc offset.
369 masm()->Align(kIntSize);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000370 unsigned offset = masm()->pc_offset();
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000371 unsigned length = back_edges_.length();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000372 __ dd(length);
373 for (unsigned i = 0; i < length; ++i) {
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000374 __ dd(back_edges_[i].id.ToInt());
375 __ dd(back_edges_[i].pc);
376 __ db(back_edges_[i].loop_depth);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000377 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000378 return offset;
379}
380
381
382void FullCodeGenerator::PopulateDeoptimizationData(Handle<Code> code) {
383 // Fill in the deoptimization information.
384 ASSERT(info_->HasDeoptimizationSupport() || bailout_entries_.is_empty());
385 if (!info_->HasDeoptimizationSupport()) return;
386 int length = bailout_entries_.length();
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000387 Handle<DeoptimizationOutputData> data = isolate()->factory()->
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000388 NewDeoptimizationOutputData(length, TENURED);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000389 for (int i = 0; i < length; i++) {
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000390 data->SetAstId(i, bailout_entries_[i].id);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000391 data->SetPcAndState(i, Smi::FromInt(bailout_entries_[i].pc_and_state));
392 }
393 code->set_deoptimization_data(*data);
394}
395
396
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000397void FullCodeGenerator::PopulateTypeFeedbackInfo(Handle<Code> code) {
398 Handle<TypeFeedbackInfo> info = isolate()->factory()->NewTypeFeedbackInfo();
399 info->set_ic_total_count(ic_total_count_);
yangguo@chromium.orga7d3df92012-02-27 11:46:55 +0000400 ASSERT(!isolate()->heap()->InNewSpace(*info));
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000401 code->set_type_feedback_info(*info);
402}
403
404
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +0000405void FullCodeGenerator::Initialize() {
406 // The generation of debug code must match between the snapshot code and the
407 // code that is generated later. This is assumed by the debugger when it is
408 // calculating PC offsets after generating a debug version of code. Therefore
409 // we disable the production of debug code in the full compiler if we are
410 // either generating a snapshot or we booted from a snapshot.
411 generate_debug_code_ = FLAG_debug_code &&
412 !Serializer::enabled() &&
413 !Snapshot::HaveASnapshotToStartFrom();
414 masm_->set_emit_debug_code(generate_debug_code_);
415 masm_->set_predictable_code_size(true);
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000416 InitializeAstVisitor();
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +0000417}
418
419
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000420void FullCodeGenerator::PopulateTypeFeedbackCells(Handle<Code> code) {
421 if (type_feedback_cells_.is_empty()) return;
422 int length = type_feedback_cells_.length();
423 int array_size = TypeFeedbackCells::LengthOfFixedArray(length);
424 Handle<TypeFeedbackCells> cache = Handle<TypeFeedbackCells>::cast(
425 isolate()->factory()->NewFixedArray(array_size, TENURED));
426 for (int i = 0; i < length; i++) {
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000427 cache->SetAstId(i, type_feedback_cells_[i].ast_id);
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000428 cache->SetCell(i, *type_feedback_cells_[i].cell);
429 }
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000430 TypeFeedbackInfo::cast(code->type_feedback_info())->set_type_feedback_cells(
431 *cache);
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000432}
433
434
435
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000436void FullCodeGenerator::PrepareForBailout(Expression* node, State state) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000437 PrepareForBailoutForId(node->id(), state);
438}
439
440
441void FullCodeGenerator::RecordJSReturnSite(Call* call) {
442 // We record the offset of the function return so we can rebuild the frame
443 // if the function was inlined, i.e., this is the return address in the
444 // inlined function's frame.
445 //
446 // The state is ignored. We defensively set it to TOS_REG, which is the
447 // real state of the unoptimized code at the return site.
448 PrepareForBailoutForId(call->ReturnId(), TOS_REG);
449#ifdef DEBUG
450 // In debug builds, mark the return so we can verify that this function
451 // was called.
452 ASSERT(!call->return_is_recorded_);
453 call->return_is_recorded_ = true;
454#endif
455}
456
457
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000458void FullCodeGenerator::PrepareForBailoutForId(BailoutId id, State state) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000459 // There's no need to prepare this code for bailouts from already optimized
460 // code or code that can't be optimized.
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000461 if (!info_->HasDeoptimizationSupport()) return;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000462 unsigned pc_and_state =
463 StateField::encode(state) | PcField::encode(masm_->pc_offset());
yangguo@chromium.org56454712012-02-16 15:33:53 +0000464 ASSERT(Smi::IsValid(pc_and_state));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000465 BailoutEntry entry = { id, pc_and_state };
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000466 ASSERT(!prepared_bailout_ids_.Contains(id.ToInt()));
467 prepared_bailout_ids_.Add(id.ToInt(), zone());
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000468 bailout_entries_.Add(entry, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000469}
470
471
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000472void FullCodeGenerator::RecordTypeFeedbackCell(
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000473 TypeFeedbackId id, Handle<JSGlobalPropertyCell> cell) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000474 TypeFeedbackCellEntry entry = { id, cell };
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000475 type_feedback_cells_.Add(entry, zone());
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000476}
477
478
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +0000479void FullCodeGenerator::RecordBackEdge(BailoutId ast_id) {
480 // The pc offset does not need to be encoded and packed together with a state.
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +0000481 ASSERT(masm_->pc_offset() > 0);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000482 ASSERT(loop_depth() > 0);
483 uint8_t depth = Min(loop_depth(), Code::kMaxLoopNestingMarker);
484 BackEdgeEntry entry =
485 { ast_id, static_cast<unsigned>(masm_->pc_offset()), depth };
486 back_edges_.Add(entry, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000487}
488
489
ricow@chromium.org65fae842010-08-25 15:26:24 +0000490bool FullCodeGenerator::ShouldInlineSmiCase(Token::Value op) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000491 // Inline smi case inside loops, but not division and modulo which
492 // are too complicated and take up too much space.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000493 if (op == Token::DIV ||op == Token::MOD) return false;
494 if (FLAG_always_inline_smi_code) return true;
495 return loop_depth_ > 0;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000496}
497
498
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000499void FullCodeGenerator::EffectContext::Plug(Register reg) const {
500}
501
502
503void FullCodeGenerator::AccumulatorValueContext::Plug(Register reg) const {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000504 __ Move(result_register(), reg);
505}
506
507
508void FullCodeGenerator::StackValueContext::Plug(Register reg) const {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000509 __ push(reg);
510}
511
512
513void FullCodeGenerator::TestContext::Plug(Register reg) const {
514 // For simplicity we always test the accumulator register.
515 __ Move(result_register(), reg);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000516 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000517 codegen()->DoTest(this);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000518}
519
520
521void FullCodeGenerator::EffectContext::PlugTOS() const {
522 __ Drop(1);
523}
524
525
526void FullCodeGenerator::AccumulatorValueContext::PlugTOS() const {
527 __ pop(result_register());
528}
529
530
531void FullCodeGenerator::StackValueContext::PlugTOS() const {
532}
533
534
535void FullCodeGenerator::TestContext::PlugTOS() const {
536 // For simplicity we always test the accumulator register.
537 __ pop(result_register());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000538 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000539 codegen()->DoTest(this);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000540}
541
542
543void FullCodeGenerator::EffectContext::PrepareTest(
544 Label* materialize_true,
545 Label* materialize_false,
546 Label** if_true,
547 Label** if_false,
548 Label** fall_through) const {
549 // In an effect context, the true and the false case branch to the
550 // same label.
551 *if_true = *if_false = *fall_through = materialize_true;
552}
553
554
555void FullCodeGenerator::AccumulatorValueContext::PrepareTest(
556 Label* materialize_true,
557 Label* materialize_false,
558 Label** if_true,
559 Label** if_false,
560 Label** fall_through) const {
561 *if_true = *fall_through = materialize_true;
562 *if_false = materialize_false;
563}
564
565
566void FullCodeGenerator::StackValueContext::PrepareTest(
567 Label* materialize_true,
568 Label* materialize_false,
569 Label** if_true,
570 Label** if_false,
571 Label** fall_through) const {
572 *if_true = *fall_through = materialize_true;
573 *if_false = materialize_false;
574}
575
576
577void FullCodeGenerator::TestContext::PrepareTest(
578 Label* materialize_true,
579 Label* materialize_false,
580 Label** if_true,
581 Label** if_false,
582 Label** fall_through) const {
583 *if_true = true_label_;
584 *if_false = false_label_;
585 *fall_through = fall_through_;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000586}
587
588
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000589void FullCodeGenerator::DoTest(const TestContext* context) {
590 DoTest(context->condition(),
591 context->true_label(),
592 context->false_label(),
593 context->fall_through());
594}
595
596
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000597void FullCodeGenerator::AllocateModules(ZoneList<Declaration*>* declarations) {
598 ASSERT(scope_->is_global_scope());
599
600 for (int i = 0; i < declarations->length(); i++) {
601 ModuleDeclaration* declaration = declarations->at(i)->AsModuleDeclaration();
602 if (declaration != NULL) {
603 ModuleLiteral* module = declaration->module()->AsModuleLiteral();
604 if (module != NULL) {
605 Comment cmnt(masm_, "[ Link nested modules");
606 Scope* scope = module->body()->scope();
607 Interface* interface = scope->interface();
608 ASSERT(interface->IsModule() && interface->IsFrozen());
609
610 interface->Allocate(scope->module_var()->index());
611
612 // Set up module context.
613 ASSERT(scope->interface()->Index() >= 0);
614 __ Push(Smi::FromInt(scope->interface()->Index()));
615 __ Push(scope->GetScopeInfo());
616 __ CallRuntime(Runtime::kPushModuleContext, 2);
617 StoreToFrameField(StandardFrameConstants::kContextOffset,
618 context_register());
619
620 AllocateModules(scope->declarations());
621
622 // Pop module context.
623 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
624 // Update local stack frame context field.
625 StoreToFrameField(StandardFrameConstants::kContextOffset,
626 context_register());
627 }
628 }
629 }
630}
631
632
633// Modules have their own local scope, represented by their own context.
634// Module instance objects have an accessor for every export that forwards
635// access to the respective slot from the module's context. (Exports that are
636// modules themselves, however, are simple data properties.)
637//
638// All modules have a _hosting_ scope/context, which (currently) is the
639// (innermost) enclosing global scope. To deal with recursion, nested modules
640// are hosted by the same scope as global ones.
641//
642// For every (global or nested) module literal, the hosting context has an
643// internal slot that points directly to the respective module context. This
644// enables quick access to (statically resolved) module members by 2-dimensional
645// access through the hosting context. For example,
646//
647// module A {
648// let x;
649// module B { let y; }
650// }
651// module C { let z; }
652//
653// allocates contexts as follows:
654//
655// [header| .A | .B | .C | A | C ] (global)
656// | | |
657// | | +-- [header| z ] (module)
658// | |
659// | +------- [header| y ] (module)
660// |
661// +------------ [header| x | B ] (module)
662//
663// Here, .A, .B, .C are the internal slots pointing to the hosted module
664// contexts, whereas A, B, C hold the actual instance objects (note that every
665// module context also points to the respective instance object through its
666// extension slot in the header).
667//
668// To deal with arbitrary recursion and aliases between modules,
669// they are created and initialized in several stages. Each stage applies to
670// all modules in the hosting global scope, including nested ones.
671//
672// 1. Allocate: for each module _literal_, allocate the module contexts and
673// respective instance object and wire them up. This happens in the
674// PushModuleContext runtime function, as generated by AllocateModules
675// (invoked by VisitDeclarations in the hosting scope).
676//
677// 2. Bind: for each module _declaration_ (i.e. literals as well as aliases),
678// assign the respective instance object to respective local variables. This
679// happens in VisitModuleDeclaration, and uses the instance objects created
680// in the previous stage.
681// For each module _literal_, this phase also constructs a module descriptor
682// for the next stage. This happens in VisitModuleLiteral.
683//
684// 3. Populate: invoke the DeclareModules runtime function to populate each
685// _instance_ object with accessors for it exports. This is generated by
686// DeclareModules (invoked by VisitDeclarations in the hosting scope again),
687// and uses the descriptors generated in the previous stage.
688//
689// 4. Initialize: execute the module bodies (and other code) in sequence. This
690// happens by the separate statements generated for module bodies. To reenter
691// the module scopes properly, the parser inserted ModuleStatements.
692
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000693void FullCodeGenerator::VisitDeclarations(
694 ZoneList<Declaration*>* declarations) {
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000695 Handle<FixedArray> saved_modules = modules_;
696 int saved_module_index = module_index_;
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000697 ZoneList<Handle<Object> >* saved_globals = globals_;
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000698 ZoneList<Handle<Object> > inner_globals(10, zone());
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000699 globals_ = &inner_globals;
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000700
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000701 if (scope_->num_modules() != 0) {
702 // This is a scope hosting modules. Allocate a descriptor array to pass
703 // to the runtime for initialization.
704 Comment cmnt(masm_, "[ Allocate modules");
705 ASSERT(scope_->is_global_scope());
706 modules_ =
707 isolate()->factory()->NewFixedArray(scope_->num_modules(), TENURED);
708 module_index_ = 0;
709
710 // Generate code for allocating all modules, including nested ones.
711 // The allocated contexts are stored in internal variables in this scope.
712 AllocateModules(declarations);
713 }
714
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000715 AstVisitor::VisitDeclarations(declarations);
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000716
717 if (scope_->num_modules() != 0) {
718 // Initialize modules from descriptor array.
719 ASSERT(module_index_ == modules_->length());
720 DeclareModules(modules_);
721 modules_ = saved_modules;
722 module_index_ = saved_module_index;
723 }
724
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000725 if (!globals_->is_empty()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000726 // Invoke the platform-dependent code generator to do the actual
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000727 // declaration of the global functions and variables.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000728 Handle<FixedArray> array =
729 isolate()->factory()->NewFixedArray(globals_->length(), TENURED);
730 for (int i = 0; i < globals_->length(); ++i)
731 array->set(i, *globals_->at(i));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000732 DeclareGlobals(array);
733 }
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000734
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000735 globals_ = saved_globals;
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000736}
737
738
739void FullCodeGenerator::VisitModuleLiteral(ModuleLiteral* module) {
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000740 Block* block = module->body();
741 Scope* saved_scope = scope();
742 scope_ = block->scope();
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000743 Interface* interface = scope_->interface();
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000744
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000745 Comment cmnt(masm_, "[ ModuleLiteral");
746 SetStatementPosition(block);
747
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000748 ASSERT(!modules_.is_null());
749 ASSERT(module_index_ < modules_->length());
750 int index = module_index_++;
751
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000752 // Set up module context.
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000753 ASSERT(interface->Index() >= 0);
754 __ Push(Smi::FromInt(interface->Index()));
755 __ Push(Smi::FromInt(0));
756 __ CallRuntime(Runtime::kPushModuleContext, 2);
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000757 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000758
759 {
760 Comment cmnt(masm_, "[ Declarations");
761 VisitDeclarations(scope_->declarations());
762 }
763
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000764 // Populate the module description.
765 Handle<ModuleInfo> description =
766 ModuleInfo::Create(isolate(), interface, scope_);
767 modules_->set(index, *description);
768
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000769 scope_ = saved_scope;
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000770 // Pop module context.
771 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
772 // Update local stack frame context field.
773 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000774}
775
776
777void FullCodeGenerator::VisitModuleVariable(ModuleVariable* module) {
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000778 // Nothing to do.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000779 // The instance object is resolved statically through the module's interface.
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000780}
781
782
783void FullCodeGenerator::VisitModulePath(ModulePath* module) {
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000784 // Nothing to do.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000785 // The instance object is resolved statically through the module's interface.
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000786}
787
788
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000789void FullCodeGenerator::VisitModuleUrl(ModuleUrl* module) {
790 // TODO(rossberg): dummy allocation for now.
791 Scope* scope = module->body()->scope();
792 Interface* interface = scope_->interface();
793
794 ASSERT(interface->IsModule() && interface->IsFrozen());
795 ASSERT(!modules_.is_null());
796 ASSERT(module_index_ < modules_->length());
797 interface->Allocate(scope->module_var()->index());
798 int index = module_index_++;
799
800 Handle<ModuleInfo> description =
801 ModuleInfo::Create(isolate(), interface, scope_);
802 modules_->set(index, *description);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000803}
804
805
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000806int FullCodeGenerator::DeclareGlobalsFlags() {
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000807 ASSERT(DeclareGlobalsLanguageMode::is_valid(language_mode()));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000808 return DeclareGlobalsEvalFlag::encode(is_eval()) |
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000809 DeclareGlobalsNativeFlag::encode(is_native()) |
810 DeclareGlobalsLanguageMode::encode(language_mode());
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000811}
812
813
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000814void FullCodeGenerator::SetFunctionPosition(FunctionLiteral* fun) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000815 CodeGenerator::RecordPositions(masm_, fun->start_position());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000816}
817
818
819void FullCodeGenerator::SetReturnPosition(FunctionLiteral* fun) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000820 CodeGenerator::RecordPositions(masm_, fun->end_position() - 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000821}
822
823
824void FullCodeGenerator::SetStatementPosition(Statement* stmt) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000825#ifdef ENABLE_DEBUGGER_SUPPORT
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000826 if (!isolate()->debugger()->IsDebuggerActive()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000827 CodeGenerator::RecordPositions(masm_, stmt->statement_pos());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000828 } else {
829 // Check if the statement will be breakable without adding a debug break
830 // slot.
831 BreakableStatementChecker checker;
832 checker.Check(stmt);
833 // Record the statement position right here if the statement is not
834 // breakable. For breakable statements the actual recording of the
835 // position will be postponed to the breakable code (typically an IC).
836 bool position_recorded = CodeGenerator::RecordPositions(
837 masm_, stmt->statement_pos(), !checker.is_breakable());
838 // If the position recording did record a new position generate a debug
839 // break slot to make the statement breakable.
840 if (position_recorded) {
841 Debug::GenerateSlot(masm_);
842 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000843 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000844#else
845 CodeGenerator::RecordPositions(masm_, stmt->statement_pos());
846#endif
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000847}
848
849
850void FullCodeGenerator::SetExpressionPosition(Expression* expr, int pos) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000851#ifdef ENABLE_DEBUGGER_SUPPORT
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000852 if (!isolate()->debugger()->IsDebuggerActive()) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000853 CodeGenerator::RecordPositions(masm_, pos);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000854 } else {
855 // Check if the expression will be breakable without adding a debug break
856 // slot.
857 BreakableStatementChecker checker;
858 checker.Check(expr);
859 // Record a statement position right here if the expression is not
860 // breakable. For breakable expressions the actual recording of the
861 // position will be postponed to the breakable code (typically an IC).
862 // NOTE this will record a statement position for something which might
863 // not be a statement. As stepping in the debugger will only stop at
864 // statement positions this is used for e.g. the condition expression of
865 // a do while loop.
866 bool position_recorded = CodeGenerator::RecordPositions(
867 masm_, pos, !checker.is_breakable());
868 // If the position recording did record a new position generate a debug
869 // break slot to make the statement breakable.
870 if (position_recorded) {
871 Debug::GenerateSlot(masm_);
872 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000873 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000874#else
875 CodeGenerator::RecordPositions(masm_, pos);
876#endif
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000877}
878
879
880void FullCodeGenerator::SetStatementPosition(int pos) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000881 CodeGenerator::RecordPositions(masm_, pos);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000882}
883
884
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000885void FullCodeGenerator::SetSourcePosition(int pos) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000886 if (pos != RelocInfo::kNoPosition) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000887 masm_->positions_recorder()->RecordPosition(pos);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000888 }
889}
890
891
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000892// Lookup table for code generators for special runtime calls which are
893// generated inline.
894#define INLINE_FUNCTION_GENERATOR_ADDRESS(Name, argc, ressize) \
895 &FullCodeGenerator::Emit##Name,
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000896
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000897const FullCodeGenerator::InlineFunctionGenerator
898 FullCodeGenerator::kInlineFunctionGenerators[] = {
899 INLINE_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS)
900 INLINE_RUNTIME_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS)
901 };
902#undef INLINE_FUNCTION_GENERATOR_ADDRESS
903
904
905FullCodeGenerator::InlineFunctionGenerator
906 FullCodeGenerator::FindInlineFunctionGenerator(Runtime::FunctionId id) {
whesse@chromium.org023421e2010-12-21 12:19:12 +0000907 int lookup_index =
908 static_cast<int>(id) - static_cast<int>(Runtime::kFirstInlineFunction);
909 ASSERT(lookup_index >= 0);
910 ASSERT(static_cast<size_t>(lookup_index) <
911 ARRAY_SIZE(kInlineFunctionGenerators));
912 return kInlineFunctionGenerators[lookup_index];
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000913}
914
915
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000916void FullCodeGenerator::EmitInlineRuntimeCall(CallRuntime* expr) {
917 const Runtime::Function* function = expr->function();
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000918 ASSERT(function != NULL);
919 ASSERT(function->intrinsic_type == Runtime::INLINE);
920 InlineFunctionGenerator generator =
921 FindInlineFunctionGenerator(function->function_id);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000922 ((*this).*(generator))(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000923}
924
925
926void FullCodeGenerator::VisitBinaryOperation(BinaryOperation* expr) {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000927 switch (expr->op()) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000928 case Token::COMMA:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000929 return VisitComma(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000930 case Token::OR:
931 case Token::AND:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000932 return VisitLogicalExpression(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000933 default:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000934 return VisitArithmeticExpression(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000935 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000936}
937
938
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000939void FullCodeGenerator::VisitInDuplicateContext(Expression* expr) {
940 if (context()->IsEffect()) {
941 VisitForEffect(expr);
942 } else if (context()->IsAccumulatorValue()) {
943 VisitForAccumulatorValue(expr);
944 } else if (context()->IsStackValue()) {
945 VisitForStackValue(expr);
946 } else if (context()->IsTest()) {
947 const TestContext* test = TestContext::cast(context());
948 VisitForControl(expr, test->true_label(), test->false_label(),
949 test->fall_through());
950 }
951}
952
953
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000954void FullCodeGenerator::VisitComma(BinaryOperation* expr) {
955 Comment cmnt(masm_, "[ Comma");
956 VisitForEffect(expr->left());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000957 VisitInDuplicateContext(expr->right());
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000958}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000959
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000960
961void FullCodeGenerator::VisitLogicalExpression(BinaryOperation* expr) {
962 bool is_logical_and = expr->op() == Token::AND;
963 Comment cmnt(masm_, is_logical_and ? "[ Logical AND" : "[ Logical OR");
964 Expression* left = expr->left();
965 Expression* right = expr->right();
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000966 BailoutId right_id = expr->RightId();
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000967 Label done;
968
969 if (context()->IsTest()) {
970 Label eval_right;
971 const TestContext* test = TestContext::cast(context());
972 if (is_logical_and) {
973 VisitForControl(left, &eval_right, test->false_label(), &eval_right);
974 } else {
975 VisitForControl(left, test->true_label(), &eval_right, &eval_right);
976 }
977 PrepareForBailoutForId(right_id, NO_REGISTERS);
978 __ bind(&eval_right);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000979
980 } else if (context()->IsAccumulatorValue()) {
981 VisitForAccumulatorValue(left);
982 // We want the value in the accumulator for the test, and on the stack in
983 // case we need it.
984 __ push(result_register());
985 Label discard, restore;
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000986 if (is_logical_and) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000987 DoTest(left, &discard, &restore, &restore);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000988 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000989 DoTest(left, &restore, &discard, &restore);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000990 }
991 __ bind(&restore);
992 __ pop(result_register());
993 __ jmp(&done);
994 __ bind(&discard);
995 __ Drop(1);
996 PrepareForBailoutForId(right_id, NO_REGISTERS);
997
998 } else if (context()->IsStackValue()) {
999 VisitForAccumulatorValue(left);
1000 // We want the value in the accumulator for the test, and on the stack in
1001 // case we need it.
1002 __ push(result_register());
1003 Label discard;
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001004 if (is_logical_and) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001005 DoTest(left, &discard, &done, &discard);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001006 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001007 DoTest(left, &done, &discard, &discard);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001008 }
1009 __ bind(&discard);
1010 __ Drop(1);
1011 PrepareForBailoutForId(right_id, NO_REGISTERS);
1012
1013 } else {
1014 ASSERT(context()->IsEffect());
1015 Label eval_right;
1016 if (is_logical_and) {
1017 VisitForControl(left, &eval_right, &done, &eval_right);
1018 } else {
1019 VisitForControl(left, &done, &eval_right, &eval_right);
1020 }
1021 PrepareForBailoutForId(right_id, NO_REGISTERS);
1022 __ bind(&eval_right);
1023 }
1024
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001025 VisitInDuplicateContext(right);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001026 __ bind(&done);
1027}
1028
1029
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001030void FullCodeGenerator::VisitArithmeticExpression(BinaryOperation* expr) {
1031 Token::Value op = expr->op();
1032 Comment cmnt(masm_, "[ ArithmeticExpression");
1033 Expression* left = expr->left();
1034 Expression* right = expr->right();
1035 OverwriteMode mode =
1036 left->ResultOverwriteAllowed()
1037 ? OVERWRITE_LEFT
1038 : (right->ResultOverwriteAllowed() ? OVERWRITE_RIGHT : NO_OVERWRITE);
1039
1040 VisitForStackValue(left);
1041 VisitForAccumulatorValue(right);
1042
1043 SetSourcePosition(expr->position());
1044 if (ShouldInlineSmiCase(op)) {
1045 EmitInlineSmiBinaryOp(expr, op, mode, left, right);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001046 } else {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001047 EmitBinaryOp(expr, op, mode);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001048 }
1049}
1050
1051
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001052void FullCodeGenerator::VisitBlock(Block* stmt) {
1053 Comment cmnt(masm_, "[ Block");
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001054 NestedBlock nested_block(this, stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001055 SetStatementPosition(stmt);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001056
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001057 Scope* saved_scope = scope();
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001058 // Push a block context when entering a block with block scoped variables.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00001059 if (stmt->scope() != NULL) {
danno@chromium.org81cac2b2012-07-10 11:28:27 +00001060 scope_ = stmt->scope();
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00001061 ASSERT(!scope_->is_module_scope());
1062 { Comment cmnt(masm_, "[ Extend block context");
1063 Handle<ScopeInfo> scope_info = scope_->GetScopeInfo();
1064 int heap_slots = scope_info->ContextLength() - Context::MIN_CONTEXT_SLOTS;
1065 __ Push(scope_info);
1066 PushFunctionArgumentForContextAllocation();
1067 if (heap_slots <= FastNewBlockContextStub::kMaximumSlots) {
1068 FastNewBlockContextStub stub(heap_slots);
1069 __ CallStub(&stub);
1070 } else {
1071 __ CallRuntime(Runtime::kPushBlockContext, 2);
1072 }
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001073
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00001074 // Replace the context stored in the frame.
1075 StoreToFrameField(StandardFrameConstants::kContextOffset,
1076 context_register());
1077 }
1078 { Comment cmnt(masm_, "[ Declarations");
1079 VisitDeclarations(scope_->declarations());
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001080 }
1081 }
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00001082
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00001083 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001084 VisitStatements(stmt->statements());
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001085 scope_ = saved_scope;
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001086 __ bind(nested_block.break_label());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001087 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001088
1089 // Pop block context if necessary.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00001090 if (stmt->scope() != NULL) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001091 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1092 // Update local stack frame context field.
1093 StoreToFrameField(StandardFrameConstants::kContextOffset,
1094 context_register());
1095 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001096}
1097
1098
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00001099void FullCodeGenerator::VisitModuleStatement(ModuleStatement* stmt) {
1100 Comment cmnt(masm_, "[ Module context");
1101
1102 __ Push(Smi::FromInt(stmt->proxy()->interface()->Index()));
1103 __ Push(Smi::FromInt(0));
1104 __ CallRuntime(Runtime::kPushModuleContext, 2);
1105 StoreToFrameField(
1106 StandardFrameConstants::kContextOffset, context_register());
1107
1108 Scope* saved_scope = scope_;
1109 scope_ = stmt->body()->scope();
1110 VisitStatements(stmt->body()->statements());
1111 scope_ = saved_scope;
1112 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1113 // Update local stack frame context field.
1114 StoreToFrameField(StandardFrameConstants::kContextOffset,
1115 context_register());
1116}
1117
1118
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001119void FullCodeGenerator::VisitExpressionStatement(ExpressionStatement* stmt) {
1120 Comment cmnt(masm_, "[ ExpressionStatement");
1121 SetStatementPosition(stmt);
1122 VisitForEffect(stmt->expression());
1123}
1124
1125
1126void FullCodeGenerator::VisitEmptyStatement(EmptyStatement* stmt) {
1127 Comment cmnt(masm_, "[ EmptyStatement");
1128 SetStatementPosition(stmt);
1129}
1130
1131
1132void FullCodeGenerator::VisitIfStatement(IfStatement* stmt) {
1133 Comment cmnt(masm_, "[ IfStatement");
1134 SetStatementPosition(stmt);
1135 Label then_part, else_part, done;
1136
ricow@chromium.org65fae842010-08-25 15:26:24 +00001137 if (stmt->HasElseStatement()) {
1138 VisitForControl(stmt->condition(), &then_part, &else_part, &then_part);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001139 PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001140 __ bind(&then_part);
1141 Visit(stmt->then_statement());
1142 __ jmp(&done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001143
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001144 PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001145 __ bind(&else_part);
1146 Visit(stmt->else_statement());
1147 } else {
1148 VisitForControl(stmt->condition(), &then_part, &done, &then_part);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001149 PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001150 __ bind(&then_part);
1151 Visit(stmt->then_statement());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001152
1153 PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001154 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001155 __ bind(&done);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001156 PrepareForBailoutForId(stmt->IfId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001157}
1158
1159
1160void FullCodeGenerator::VisitContinueStatement(ContinueStatement* stmt) {
1161 Comment cmnt(masm_, "[ ContinueStatement");
1162 SetStatementPosition(stmt);
1163 NestedStatement* current = nesting_stack_;
1164 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001165 int context_length = 0;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001166 // When continuing, we clobber the unpredictable value in the accumulator
1167 // with one that's safe for GC. If we hit an exit from the try block of
1168 // try...finally on our way out, we will unconditionally preserve the
1169 // accumulator on the stack.
1170 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001171 while (!current->IsContinueTarget(stmt->target())) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001172 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001173 }
1174 __ Drop(stack_depth);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001175 if (context_length > 0) {
1176 while (context_length > 0) {
1177 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1178 --context_length;
1179 }
1180 StoreToFrameField(StandardFrameConstants::kContextOffset,
1181 context_register());
1182 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001183
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001184 __ jmp(current->AsIteration()->continue_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001185}
1186
1187
1188void FullCodeGenerator::VisitBreakStatement(BreakStatement* stmt) {
1189 Comment cmnt(masm_, "[ BreakStatement");
1190 SetStatementPosition(stmt);
1191 NestedStatement* current = nesting_stack_;
1192 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001193 int context_length = 0;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001194 // When breaking, we clobber the unpredictable value in the accumulator
1195 // with one that's safe for GC. If we hit an exit from the try block of
1196 // try...finally on our way out, we will unconditionally preserve the
1197 // accumulator on the stack.
1198 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001199 while (!current->IsBreakTarget(stmt->target())) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001200 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001201 }
1202 __ Drop(stack_depth);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001203 if (context_length > 0) {
1204 while (context_length > 0) {
1205 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1206 --context_length;
1207 }
1208 StoreToFrameField(StandardFrameConstants::kContextOffset,
1209 context_register());
1210 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001211
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001212 __ jmp(current->AsBreakable()->break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001213}
1214
1215
1216void FullCodeGenerator::VisitReturnStatement(ReturnStatement* stmt) {
1217 Comment cmnt(masm_, "[ ReturnStatement");
1218 SetStatementPosition(stmt);
1219 Expression* expr = stmt->expression();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001220 VisitForAccumulatorValue(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001221
1222 // Exit all nested statements.
1223 NestedStatement* current = nesting_stack_;
1224 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001225 int context_length = 0;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001226 while (current != NULL) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001227 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001228 }
1229 __ Drop(stack_depth);
1230
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001231 EmitReturnSequence();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001232}
1233
1234
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001235void FullCodeGenerator::VisitWithStatement(WithStatement* stmt) {
1236 Comment cmnt(masm_, "[ WithStatement");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001237 SetStatementPosition(stmt);
1238
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001239 VisitForStackValue(stmt->expression());
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00001240 PushFunctionArgumentForContextAllocation();
1241 __ CallRuntime(Runtime::kPushWithContext, 2);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001242 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001243
1244 { WithOrCatch body(this);
1245 Visit(stmt->statement());
1246 }
1247
1248 // Pop context.
1249 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1250 // Update local stack frame context field.
1251 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001252}
1253
1254
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001255void FullCodeGenerator::VisitDoWhileStatement(DoWhileStatement* stmt) {
1256 Comment cmnt(masm_, "[ DoWhileStatement");
1257 SetStatementPosition(stmt);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001258 Label body, book_keeping;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001259
1260 Iteration loop_statement(this, stmt);
1261 increment_loop_depth();
1262
1263 __ bind(&body);
1264 Visit(stmt->body());
1265
ricow@chromium.org65fae842010-08-25 15:26:24 +00001266 // Record the position of the do while condition and make sure it is
1267 // possible to break on the condition.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001268 __ bind(loop_statement.continue_label());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001269 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001270 SetExpressionPosition(stmt->cond(), stmt->condition_position());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001271 VisitForControl(stmt->cond(),
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001272 &book_keeping,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001273 loop_statement.break_label(),
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001274 &book_keeping);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001275
1276 // Check stack before looping.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001277 PrepareForBailoutForId(stmt->BackEdgeId(), NO_REGISTERS);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001278 __ bind(&book_keeping);
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +00001279 EmitBackEdgeBookkeeping(stmt, &body);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001280 __ jmp(&body);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001281
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001282 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001283 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001284 decrement_loop_depth();
1285}
1286
1287
1288void FullCodeGenerator::VisitWhileStatement(WhileStatement* stmt) {
1289 Comment cmnt(masm_, "[ WhileStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001290 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001291
1292 Iteration loop_statement(this, stmt);
1293 increment_loop_depth();
1294
1295 // Emit the test at the bottom of the loop.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001296 __ jmp(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001297
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001298 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001299 __ bind(&body);
1300 Visit(stmt->body());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001301
1302 // Emit the statement position here as this is where the while
1303 // statement code starts.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001304 __ bind(loop_statement.continue_label());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001305 SetStatementPosition(stmt);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001306
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001307 // Check stack before looping.
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +00001308 EmitBackEdgeBookkeeping(stmt, &body);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001309
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001310 __ bind(&test);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001311 VisitForControl(stmt->cond(),
1312 &body,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001313 loop_statement.break_label(),
1314 loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001315
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001316 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001317 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001318 decrement_loop_depth();
1319}
1320
1321
1322void FullCodeGenerator::VisitForStatement(ForStatement* stmt) {
1323 Comment cmnt(masm_, "[ ForStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001324 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001325
1326 Iteration loop_statement(this, stmt);
erik.corry@gmail.combbceb572012-03-09 10:52:05 +00001327
1328 // Set statement position for a break slot before entering the for-body.
1329 SetStatementPosition(stmt);
1330
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001331 if (stmt->init() != NULL) {
1332 Visit(stmt->init());
1333 }
1334
1335 increment_loop_depth();
1336 // Emit the test at the bottom of the loop (even if empty).
1337 __ jmp(&test);
1338
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001339 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001340 __ bind(&body);
1341 Visit(stmt->body());
1342
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001343 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001344 __ bind(loop_statement.continue_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001345 if (stmt->next() != NULL) {
1346 Visit(stmt->next());
1347 }
1348
ricow@chromium.org65fae842010-08-25 15:26:24 +00001349 // Emit the statement position here as this is where the for
1350 // statement code starts.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001351 SetStatementPosition(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001352
1353 // Check stack before looping.
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +00001354 EmitBackEdgeBookkeeping(stmt, &body);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001355
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001356 __ bind(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001357 if (stmt->cond() != NULL) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00001358 VisitForControl(stmt->cond(),
1359 &body,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001360 loop_statement.break_label(),
1361 loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001362 } else {
1363 __ jmp(&body);
1364 }
1365
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001366 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001367 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001368 decrement_loop_depth();
1369}
1370
1371
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001372void FullCodeGenerator::VisitTryCatchStatement(TryCatchStatement* stmt) {
1373 Comment cmnt(masm_, "[ TryCatchStatement");
1374 SetStatementPosition(stmt);
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001375 // The try block adds a handler to the exception handler chain before
1376 // entering, and removes it again when exiting normally. If an exception
1377 // is thrown during execution of the try block, the handler is consumed
1378 // and control is passed to the catch block with the exception in the
1379 // result register.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001380
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001381 Label try_entry, handler_entry, exit;
1382 __ jmp(&try_entry);
1383 __ bind(&handler_entry);
1384 handler_table()->set(stmt->index(), Smi::FromInt(handler_entry.pos()));
1385 // Exception handler code, the exception is in the result register.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001386 // Extend the context before executing the catch block.
1387 { Comment cmnt(masm_, "[ Extend catch context");
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001388 __ Push(stmt->variable()->name());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001389 __ push(result_register());
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00001390 PushFunctionArgumentForContextAllocation();
1391 __ CallRuntime(Runtime::kPushCatchContext, 3);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001392 StoreToFrameField(StandardFrameConstants::kContextOffset,
1393 context_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001394 }
1395
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001396 Scope* saved_scope = scope();
1397 scope_ = stmt->scope();
1398 ASSERT(scope_->declarations()->is_empty());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001399 { WithOrCatch catch_body(this);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001400 Visit(stmt->catch_block());
1401 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001402 // Restore the context.
1403 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1404 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001405 scope_ = saved_scope;
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001406 __ jmp(&exit);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001407
1408 // Try block code. Sets up the exception handler chain.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001409 __ bind(&try_entry);
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00001410 __ PushTryHandler(StackHandler::CATCH, stmt->index());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001411 { TryCatch try_body(this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001412 Visit(stmt->try_block());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001413 }
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001414 __ PopTryHandler();
1415 __ bind(&exit);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001416}
1417
1418
1419void FullCodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* stmt) {
1420 Comment cmnt(masm_, "[ TryFinallyStatement");
1421 SetStatementPosition(stmt);
1422 // Try finally is compiled by setting up a try-handler on the stack while
1423 // executing the try body, and removing it again afterwards.
1424 //
1425 // The try-finally construct can enter the finally block in three ways:
1426 // 1. By exiting the try-block normally. This removes the try-handler and
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001427 // calls the finally block code before continuing.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001428 // 2. By exiting the try-block with a function-local control flow transfer
1429 // (break/continue/return). The site of the, e.g., break removes the
1430 // try handler and calls the finally block code before continuing
1431 // its outward control transfer.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001432 // 3. By exiting the try-block with a thrown exception.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001433 // This can happen in nested function calls. It traverses the try-handler
1434 // chain and consumes the try-handler entry before jumping to the
1435 // handler code. The handler code then calls the finally-block before
1436 // rethrowing the exception.
1437 //
1438 // The finally block must assume a return address on top of the stack
1439 // (or in the link register on ARM chips) and a value (return value or
1440 // exception) in the result register (rax/eax/r0), both of which must
1441 // be preserved. The return address isn't GC-safe, so it should be
1442 // cooked before GC.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001443 Label try_entry, handler_entry, finally_entry;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001444
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001445 // Jump to try-handler setup and try-block code.
1446 __ jmp(&try_entry);
1447 __ bind(&handler_entry);
1448 handler_table()->set(stmt->index(), Smi::FromInt(handler_entry.pos()));
1449 // Exception handler code. This code is only executed when an exception
1450 // is thrown. The exception is in the result register, and must be
1451 // preserved by the finally block. Call the finally block and then
1452 // rethrow the exception if it returns.
1453 __ Call(&finally_entry);
1454 __ push(result_register());
1455 __ CallRuntime(Runtime::kReThrow, 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001456
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001457 // Finally block implementation.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001458 __ bind(&finally_entry);
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001459 EnterFinallyBlock();
1460 { Finally finally_body(this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001461 Visit(stmt->finally_block());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001462 }
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001463 ExitFinallyBlock(); // Return to the calling code.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001464
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001465 // Set up try handler.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001466 __ bind(&try_entry);
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00001467 __ PushTryHandler(StackHandler::FINALLY, stmt->index());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001468 { TryFinally try_body(this, &finally_entry);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001469 Visit(stmt->try_block());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001470 }
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001471 __ PopTryHandler();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001472 // Execute the finally block on the way out. Clobber the unpredictable
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001473 // value in the result register with one that's safe for GC because the
1474 // finally block will unconditionally preserve the result register on the
1475 // stack.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001476 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001477 __ Call(&finally_entry);
1478}
1479
1480
1481void FullCodeGenerator::VisitDebuggerStatement(DebuggerStatement* stmt) {
1482#ifdef ENABLE_DEBUGGER_SUPPORT
1483 Comment cmnt(masm_, "[ DebuggerStatement");
1484 SetStatementPosition(stmt);
1485
ager@chromium.org5c838252010-02-19 08:53:10 +00001486 __ DebugBreak();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001487 // Ignore the return value.
1488#endif
1489}
1490
1491
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001492void FullCodeGenerator::VisitConditional(Conditional* expr) {
1493 Comment cmnt(masm_, "[ Conditional");
1494 Label true_case, false_case, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001495 VisitForControl(expr->condition(), &true_case, &false_case, &true_case);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001496
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001497 PrepareForBailoutForId(expr->ThenId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001498 __ bind(&true_case);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001499 SetExpressionPosition(expr->then_expression(),
1500 expr->then_expression_position());
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001501 if (context()->IsTest()) {
1502 const TestContext* for_test = TestContext::cast(context());
1503 VisitForControl(expr->then_expression(),
1504 for_test->true_label(),
1505 for_test->false_label(),
1506 NULL);
1507 } else {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001508 VisitInDuplicateContext(expr->then_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001509 __ jmp(&done);
1510 }
1511
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001512 PrepareForBailoutForId(expr->ElseId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001513 __ bind(&false_case);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001514 SetExpressionPosition(expr->else_expression(),
1515 expr->else_expression_position());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001516 VisitInDuplicateContext(expr->else_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001517 // If control flow falls through Visit, merge it with true case here.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001518 if (!context()->IsTest()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001519 __ bind(&done);
1520 }
1521}
1522
1523
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001524void FullCodeGenerator::VisitLiteral(Literal* expr) {
1525 Comment cmnt(masm_, "[ Literal");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001526 context()->Plug(expr->handle());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001527}
1528
1529
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001530void FullCodeGenerator::VisitFunctionLiteral(FunctionLiteral* expr) {
1531 Comment cmnt(masm_, "[ FunctionLiteral");
1532
1533 // Build the function boilerplate and instantiate it.
1534 Handle<SharedFunctionInfo> function_info =
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001535 Compiler::BuildFunctionInfo(expr, script());
1536 if (function_info.is_null()) {
1537 SetStackOverflow();
1538 return;
1539 }
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001540 EmitNewClosure(function_info, expr->pretenure());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001541}
1542
1543
1544void FullCodeGenerator::VisitSharedFunctionInfoLiteral(
1545 SharedFunctionInfoLiteral* expr) {
1546 Comment cmnt(masm_, "[ SharedFunctionInfoLiteral");
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001547 EmitNewClosure(expr->shared_function_info(), false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001548}
1549
1550
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00001551void FullCodeGenerator::VisitYield(Yield* expr) {
1552 if (expr->is_delegating_yield())
1553 UNIMPLEMENTED();
1554
1555 Comment cmnt(masm_, "[ Yield");
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001556 // TODO(wingo): Actually update the iterator state.
1557 VisitForEffect(expr->generator_object());
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00001558 VisitForAccumulatorValue(expr->expression());
1559 // TODO(wingo): Assert that the operand stack depth is 0, at least while
1560 // general yield expressions are unimplemented.
1561
1562 // TODO(wingo): What follows is as in VisitReturnStatement. Replace it with a
1563 // call to a builtin that will resume the generator.
1564 NestedStatement* current = nesting_stack_;
1565 int stack_depth = 0;
1566 int context_length = 0;
1567 while (current != NULL) {
1568 current = current->Exit(&stack_depth, &context_length);
1569 }
1570 __ Drop(stack_depth);
1571 EmitReturnSequence();
1572}
1573
1574
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001575void FullCodeGenerator::VisitThrow(Throw* expr) {
1576 Comment cmnt(masm_, "[ Throw");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001577 VisitForStackValue(expr->exception());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001578 __ CallRuntime(Runtime::kThrow, 1);
1579 // Never returns here.
1580}
1581
1582
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001583FullCodeGenerator::NestedStatement* FullCodeGenerator::TryCatch::Exit(
1584 int* stack_depth,
1585 int* context_length) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001586 // The macros used here must preserve the result register.
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001587 __ Drop(*stack_depth);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001588 __ PopTryHandler();
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001589 *stack_depth = 0;
1590 return previous_;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001591}
1592
ricow@chromium.org65fae842010-08-25 15:26:24 +00001593
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001594bool FullCodeGenerator::TryLiteralCompare(CompareOperation* expr) {
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001595 Expression* sub_expr;
ager@chromium.org04921a82011-06-27 13:21:41 +00001596 Handle<String> check;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001597 if (expr->IsLiteralCompareTypeof(&sub_expr, &check)) {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001598 EmitLiteralCompareTypeof(expr, sub_expr, check);
ager@chromium.org04921a82011-06-27 13:21:41 +00001599 return true;
1600 }
1601
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001602 if (expr->IsLiteralCompareUndefined(&sub_expr)) {
1603 EmitLiteralCompareNil(expr, sub_expr, kUndefinedValue);
1604 return true;
1605 }
1606
1607 if (expr->IsLiteralCompareNull(&sub_expr)) {
1608 EmitLiteralCompareNil(expr, sub_expr, kNullValue);
ager@chromium.org04921a82011-06-27 13:21:41 +00001609 return true;
1610 }
1611
1612 return false;
1613}
1614
1615
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001616#undef __
1617
1618
1619} } // namespace v8::internal