blob: 1c6a0b91b3bdfbe1adb343db2bad52c94aee52c2 [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 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000325 unsigned table_offset = cgen.EmitStackCheckTable();
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);
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000344 code->set_stack_check_table_offset(table_offset);
ulan@chromium.org6e196bf2013-03-13 09:38:22 +0000345 code->set_stack_check_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
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000365unsigned FullCodeGenerator::EmitStackCheckTable() {
366 // The stack check table consists of a length (in number of entries)
367 // 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();
371 unsigned length = stack_checks_.length();
372 __ dd(length);
373 for (unsigned i = 0; i < length; ++i) {
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000374 __ dd(stack_checks_[i].id.ToInt());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000375 __ dd(stack_checks_[i].pc_and_state);
376 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000377 return offset;
378}
379
380
381void FullCodeGenerator::PopulateDeoptimizationData(Handle<Code> code) {
382 // Fill in the deoptimization information.
383 ASSERT(info_->HasDeoptimizationSupport() || bailout_entries_.is_empty());
384 if (!info_->HasDeoptimizationSupport()) return;
385 int length = bailout_entries_.length();
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000386 Handle<DeoptimizationOutputData> data = isolate()->factory()->
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000387 NewDeoptimizationOutputData(length, TENURED);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000388 for (int i = 0; i < length; i++) {
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000389 data->SetAstId(i, bailout_entries_[i].id);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000390 data->SetPcAndState(i, Smi::FromInt(bailout_entries_[i].pc_and_state));
391 }
392 code->set_deoptimization_data(*data);
393}
394
395
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000396void FullCodeGenerator::PopulateTypeFeedbackInfo(Handle<Code> code) {
397 Handle<TypeFeedbackInfo> info = isolate()->factory()->NewTypeFeedbackInfo();
398 info->set_ic_total_count(ic_total_count_);
yangguo@chromium.orga7d3df92012-02-27 11:46:55 +0000399 ASSERT(!isolate()->heap()->InNewSpace(*info));
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000400 code->set_type_feedback_info(*info);
401}
402
403
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +0000404void FullCodeGenerator::Initialize() {
405 // The generation of debug code must match between the snapshot code and the
406 // code that is generated later. This is assumed by the debugger when it is
407 // calculating PC offsets after generating a debug version of code. Therefore
408 // we disable the production of debug code in the full compiler if we are
409 // either generating a snapshot or we booted from a snapshot.
410 generate_debug_code_ = FLAG_debug_code &&
411 !Serializer::enabled() &&
412 !Snapshot::HaveASnapshotToStartFrom();
413 masm_->set_emit_debug_code(generate_debug_code_);
414 masm_->set_predictable_code_size(true);
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000415 InitializeAstVisitor();
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +0000416}
417
418
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000419void FullCodeGenerator::PopulateTypeFeedbackCells(Handle<Code> code) {
420 if (type_feedback_cells_.is_empty()) return;
421 int length = type_feedback_cells_.length();
422 int array_size = TypeFeedbackCells::LengthOfFixedArray(length);
423 Handle<TypeFeedbackCells> cache = Handle<TypeFeedbackCells>::cast(
424 isolate()->factory()->NewFixedArray(array_size, TENURED));
425 for (int i = 0; i < length; i++) {
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000426 cache->SetAstId(i, type_feedback_cells_[i].ast_id);
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000427 cache->SetCell(i, *type_feedback_cells_[i].cell);
428 }
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000429 TypeFeedbackInfo::cast(code->type_feedback_info())->set_type_feedback_cells(
430 *cache);
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000431}
432
433
434
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000435void FullCodeGenerator::PrepareForBailout(Expression* node, State state) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000436 PrepareForBailoutForId(node->id(), state);
437}
438
439
440void FullCodeGenerator::RecordJSReturnSite(Call* call) {
441 // We record the offset of the function return so we can rebuild the frame
442 // if the function was inlined, i.e., this is the return address in the
443 // inlined function's frame.
444 //
445 // The state is ignored. We defensively set it to TOS_REG, which is the
446 // real state of the unoptimized code at the return site.
447 PrepareForBailoutForId(call->ReturnId(), TOS_REG);
448#ifdef DEBUG
449 // In debug builds, mark the return so we can verify that this function
450 // was called.
451 ASSERT(!call->return_is_recorded_);
452 call->return_is_recorded_ = true;
453#endif
454}
455
456
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000457void FullCodeGenerator::PrepareForBailoutForId(BailoutId id, State state) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000458 // There's no need to prepare this code for bailouts from already optimized
459 // code or code that can't be optimized.
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000460 if (!info_->HasDeoptimizationSupport()) return;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000461 unsigned pc_and_state =
462 StateField::encode(state) | PcField::encode(masm_->pc_offset());
yangguo@chromium.org56454712012-02-16 15:33:53 +0000463 ASSERT(Smi::IsValid(pc_and_state));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000464 BailoutEntry entry = { id, pc_and_state };
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000465 ASSERT(!prepared_bailout_ids_.Contains(id.ToInt()));
466 prepared_bailout_ids_.Add(id.ToInt(), zone());
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000467 bailout_entries_.Add(entry, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000468}
469
470
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000471void FullCodeGenerator::RecordTypeFeedbackCell(
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000472 TypeFeedbackId id, Handle<JSGlobalPropertyCell> cell) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000473 TypeFeedbackCellEntry entry = { id, cell };
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000474 type_feedback_cells_.Add(entry, zone());
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000475}
476
477
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +0000478void FullCodeGenerator::RecordBackEdge(BailoutId ast_id) {
479 // The pc offset does not need to be encoded and packed together with a state.
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +0000480 ASSERT(masm_->pc_offset() > 0);
481 BailoutEntry entry = { ast_id, static_cast<unsigned>(masm_->pc_offset()) };
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000482 stack_checks_.Add(entry, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000483}
484
485
ricow@chromium.org65fae842010-08-25 15:26:24 +0000486bool FullCodeGenerator::ShouldInlineSmiCase(Token::Value op) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000487 // Inline smi case inside loops, but not division and modulo which
488 // are too complicated and take up too much space.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000489 if (op == Token::DIV ||op == Token::MOD) return false;
490 if (FLAG_always_inline_smi_code) return true;
491 return loop_depth_ > 0;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000492}
493
494
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000495void FullCodeGenerator::EffectContext::Plug(Register reg) const {
496}
497
498
499void FullCodeGenerator::AccumulatorValueContext::Plug(Register reg) const {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000500 __ Move(result_register(), reg);
501}
502
503
504void FullCodeGenerator::StackValueContext::Plug(Register reg) const {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000505 __ push(reg);
506}
507
508
509void FullCodeGenerator::TestContext::Plug(Register reg) const {
510 // For simplicity we always test the accumulator register.
511 __ Move(result_register(), reg);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000512 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000513 codegen()->DoTest(this);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000514}
515
516
517void FullCodeGenerator::EffectContext::PlugTOS() const {
518 __ Drop(1);
519}
520
521
522void FullCodeGenerator::AccumulatorValueContext::PlugTOS() const {
523 __ pop(result_register());
524}
525
526
527void FullCodeGenerator::StackValueContext::PlugTOS() const {
528}
529
530
531void FullCodeGenerator::TestContext::PlugTOS() const {
532 // For simplicity we always test the accumulator register.
533 __ pop(result_register());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000534 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000535 codegen()->DoTest(this);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000536}
537
538
539void FullCodeGenerator::EffectContext::PrepareTest(
540 Label* materialize_true,
541 Label* materialize_false,
542 Label** if_true,
543 Label** if_false,
544 Label** fall_through) const {
545 // In an effect context, the true and the false case branch to the
546 // same label.
547 *if_true = *if_false = *fall_through = materialize_true;
548}
549
550
551void FullCodeGenerator::AccumulatorValueContext::PrepareTest(
552 Label* materialize_true,
553 Label* materialize_false,
554 Label** if_true,
555 Label** if_false,
556 Label** fall_through) const {
557 *if_true = *fall_through = materialize_true;
558 *if_false = materialize_false;
559}
560
561
562void FullCodeGenerator::StackValueContext::PrepareTest(
563 Label* materialize_true,
564 Label* materialize_false,
565 Label** if_true,
566 Label** if_false,
567 Label** fall_through) const {
568 *if_true = *fall_through = materialize_true;
569 *if_false = materialize_false;
570}
571
572
573void FullCodeGenerator::TestContext::PrepareTest(
574 Label* materialize_true,
575 Label* materialize_false,
576 Label** if_true,
577 Label** if_false,
578 Label** fall_through) const {
579 *if_true = true_label_;
580 *if_false = false_label_;
581 *fall_through = fall_through_;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000582}
583
584
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000585void FullCodeGenerator::DoTest(const TestContext* context) {
586 DoTest(context->condition(),
587 context->true_label(),
588 context->false_label(),
589 context->fall_through());
590}
591
592
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000593void FullCodeGenerator::AllocateModules(ZoneList<Declaration*>* declarations) {
594 ASSERT(scope_->is_global_scope());
595
596 for (int i = 0; i < declarations->length(); i++) {
597 ModuleDeclaration* declaration = declarations->at(i)->AsModuleDeclaration();
598 if (declaration != NULL) {
599 ModuleLiteral* module = declaration->module()->AsModuleLiteral();
600 if (module != NULL) {
601 Comment cmnt(masm_, "[ Link nested modules");
602 Scope* scope = module->body()->scope();
603 Interface* interface = scope->interface();
604 ASSERT(interface->IsModule() && interface->IsFrozen());
605
606 interface->Allocate(scope->module_var()->index());
607
608 // Set up module context.
609 ASSERT(scope->interface()->Index() >= 0);
610 __ Push(Smi::FromInt(scope->interface()->Index()));
611 __ Push(scope->GetScopeInfo());
612 __ CallRuntime(Runtime::kPushModuleContext, 2);
613 StoreToFrameField(StandardFrameConstants::kContextOffset,
614 context_register());
615
616 AllocateModules(scope->declarations());
617
618 // Pop module context.
619 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
620 // Update local stack frame context field.
621 StoreToFrameField(StandardFrameConstants::kContextOffset,
622 context_register());
623 }
624 }
625 }
626}
627
628
629// Modules have their own local scope, represented by their own context.
630// Module instance objects have an accessor for every export that forwards
631// access to the respective slot from the module's context. (Exports that are
632// modules themselves, however, are simple data properties.)
633//
634// All modules have a _hosting_ scope/context, which (currently) is the
635// (innermost) enclosing global scope. To deal with recursion, nested modules
636// are hosted by the same scope as global ones.
637//
638// For every (global or nested) module literal, the hosting context has an
639// internal slot that points directly to the respective module context. This
640// enables quick access to (statically resolved) module members by 2-dimensional
641// access through the hosting context. For example,
642//
643// module A {
644// let x;
645// module B { let y; }
646// }
647// module C { let z; }
648//
649// allocates contexts as follows:
650//
651// [header| .A | .B | .C | A | C ] (global)
652// | | |
653// | | +-- [header| z ] (module)
654// | |
655// | +------- [header| y ] (module)
656// |
657// +------------ [header| x | B ] (module)
658//
659// Here, .A, .B, .C are the internal slots pointing to the hosted module
660// contexts, whereas A, B, C hold the actual instance objects (note that every
661// module context also points to the respective instance object through its
662// extension slot in the header).
663//
664// To deal with arbitrary recursion and aliases between modules,
665// they are created and initialized in several stages. Each stage applies to
666// all modules in the hosting global scope, including nested ones.
667//
668// 1. Allocate: for each module _literal_, allocate the module contexts and
669// respective instance object and wire them up. This happens in the
670// PushModuleContext runtime function, as generated by AllocateModules
671// (invoked by VisitDeclarations in the hosting scope).
672//
673// 2. Bind: for each module _declaration_ (i.e. literals as well as aliases),
674// assign the respective instance object to respective local variables. This
675// happens in VisitModuleDeclaration, and uses the instance objects created
676// in the previous stage.
677// For each module _literal_, this phase also constructs a module descriptor
678// for the next stage. This happens in VisitModuleLiteral.
679//
680// 3. Populate: invoke the DeclareModules runtime function to populate each
681// _instance_ object with accessors for it exports. This is generated by
682// DeclareModules (invoked by VisitDeclarations in the hosting scope again),
683// and uses the descriptors generated in the previous stage.
684//
685// 4. Initialize: execute the module bodies (and other code) in sequence. This
686// happens by the separate statements generated for module bodies. To reenter
687// the module scopes properly, the parser inserted ModuleStatements.
688
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000689void FullCodeGenerator::VisitDeclarations(
690 ZoneList<Declaration*>* declarations) {
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000691 Handle<FixedArray> saved_modules = modules_;
692 int saved_module_index = module_index_;
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000693 ZoneList<Handle<Object> >* saved_globals = globals_;
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000694 ZoneList<Handle<Object> > inner_globals(10, zone());
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000695 globals_ = &inner_globals;
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000696
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000697 if (scope_->num_modules() != 0) {
698 // This is a scope hosting modules. Allocate a descriptor array to pass
699 // to the runtime for initialization.
700 Comment cmnt(masm_, "[ Allocate modules");
701 ASSERT(scope_->is_global_scope());
702 modules_ =
703 isolate()->factory()->NewFixedArray(scope_->num_modules(), TENURED);
704 module_index_ = 0;
705
706 // Generate code for allocating all modules, including nested ones.
707 // The allocated contexts are stored in internal variables in this scope.
708 AllocateModules(declarations);
709 }
710
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000711 AstVisitor::VisitDeclarations(declarations);
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000712
713 if (scope_->num_modules() != 0) {
714 // Initialize modules from descriptor array.
715 ASSERT(module_index_ == modules_->length());
716 DeclareModules(modules_);
717 modules_ = saved_modules;
718 module_index_ = saved_module_index;
719 }
720
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000721 if (!globals_->is_empty()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000722 // Invoke the platform-dependent code generator to do the actual
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000723 // declaration of the global functions and variables.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000724 Handle<FixedArray> array =
725 isolate()->factory()->NewFixedArray(globals_->length(), TENURED);
726 for (int i = 0; i < globals_->length(); ++i)
727 array->set(i, *globals_->at(i));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000728 DeclareGlobals(array);
729 }
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000730
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000731 globals_ = saved_globals;
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000732}
733
734
735void FullCodeGenerator::VisitModuleLiteral(ModuleLiteral* module) {
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000736 Block* block = module->body();
737 Scope* saved_scope = scope();
738 scope_ = block->scope();
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000739 Interface* interface = scope_->interface();
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000740
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000741 Comment cmnt(masm_, "[ ModuleLiteral");
742 SetStatementPosition(block);
743
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000744 ASSERT(!modules_.is_null());
745 ASSERT(module_index_ < modules_->length());
746 int index = module_index_++;
747
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000748 // Set up module context.
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000749 ASSERT(interface->Index() >= 0);
750 __ Push(Smi::FromInt(interface->Index()));
751 __ Push(Smi::FromInt(0));
752 __ CallRuntime(Runtime::kPushModuleContext, 2);
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000753 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000754
755 {
756 Comment cmnt(masm_, "[ Declarations");
757 VisitDeclarations(scope_->declarations());
758 }
759
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000760 // Populate the module description.
761 Handle<ModuleInfo> description =
762 ModuleInfo::Create(isolate(), interface, scope_);
763 modules_->set(index, *description);
764
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000765 scope_ = saved_scope;
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000766 // Pop module context.
767 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
768 // Update local stack frame context field.
769 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000770}
771
772
773void FullCodeGenerator::VisitModuleVariable(ModuleVariable* module) {
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000774 // Nothing to do.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000775 // The instance object is resolved statically through the module's interface.
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000776}
777
778
779void FullCodeGenerator::VisitModulePath(ModulePath* module) {
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000780 // Nothing to do.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000781 // The instance object is resolved statically through the module's interface.
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000782}
783
784
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000785void FullCodeGenerator::VisitModuleUrl(ModuleUrl* module) {
786 // TODO(rossberg): dummy allocation for now.
787 Scope* scope = module->body()->scope();
788 Interface* interface = scope_->interface();
789
790 ASSERT(interface->IsModule() && interface->IsFrozen());
791 ASSERT(!modules_.is_null());
792 ASSERT(module_index_ < modules_->length());
793 interface->Allocate(scope->module_var()->index());
794 int index = module_index_++;
795
796 Handle<ModuleInfo> description =
797 ModuleInfo::Create(isolate(), interface, scope_);
798 modules_->set(index, *description);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000799}
800
801
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000802int FullCodeGenerator::DeclareGlobalsFlags() {
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000803 ASSERT(DeclareGlobalsLanguageMode::is_valid(language_mode()));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000804 return DeclareGlobalsEvalFlag::encode(is_eval()) |
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000805 DeclareGlobalsNativeFlag::encode(is_native()) |
806 DeclareGlobalsLanguageMode::encode(language_mode());
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000807}
808
809
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000810void FullCodeGenerator::SetFunctionPosition(FunctionLiteral* fun) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000811 CodeGenerator::RecordPositions(masm_, fun->start_position());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000812}
813
814
815void FullCodeGenerator::SetReturnPosition(FunctionLiteral* fun) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000816 CodeGenerator::RecordPositions(masm_, fun->end_position() - 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000817}
818
819
820void FullCodeGenerator::SetStatementPosition(Statement* stmt) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000821#ifdef ENABLE_DEBUGGER_SUPPORT
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000822 if (!isolate()->debugger()->IsDebuggerActive()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000823 CodeGenerator::RecordPositions(masm_, stmt->statement_pos());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000824 } else {
825 // Check if the statement will be breakable without adding a debug break
826 // slot.
827 BreakableStatementChecker checker;
828 checker.Check(stmt);
829 // Record the statement position right here if the statement is not
830 // breakable. For breakable statements the actual recording of the
831 // position will be postponed to the breakable code (typically an IC).
832 bool position_recorded = CodeGenerator::RecordPositions(
833 masm_, stmt->statement_pos(), !checker.is_breakable());
834 // If the position recording did record a new position generate a debug
835 // break slot to make the statement breakable.
836 if (position_recorded) {
837 Debug::GenerateSlot(masm_);
838 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000839 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000840#else
841 CodeGenerator::RecordPositions(masm_, stmt->statement_pos());
842#endif
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000843}
844
845
846void FullCodeGenerator::SetExpressionPosition(Expression* expr, int pos) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000847#ifdef ENABLE_DEBUGGER_SUPPORT
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000848 if (!isolate()->debugger()->IsDebuggerActive()) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000849 CodeGenerator::RecordPositions(masm_, pos);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000850 } else {
851 // Check if the expression will be breakable without adding a debug break
852 // slot.
853 BreakableStatementChecker checker;
854 checker.Check(expr);
855 // Record a statement position right here if the expression is not
856 // breakable. For breakable expressions the actual recording of the
857 // position will be postponed to the breakable code (typically an IC).
858 // NOTE this will record a statement position for something which might
859 // not be a statement. As stepping in the debugger will only stop at
860 // statement positions this is used for e.g. the condition expression of
861 // a do while loop.
862 bool position_recorded = CodeGenerator::RecordPositions(
863 masm_, pos, !checker.is_breakable());
864 // If the position recording did record a new position generate a debug
865 // break slot to make the statement breakable.
866 if (position_recorded) {
867 Debug::GenerateSlot(masm_);
868 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000869 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000870#else
871 CodeGenerator::RecordPositions(masm_, pos);
872#endif
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000873}
874
875
876void FullCodeGenerator::SetStatementPosition(int pos) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000877 CodeGenerator::RecordPositions(masm_, pos);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000878}
879
880
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000881void FullCodeGenerator::SetSourcePosition(int pos) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000882 if (pos != RelocInfo::kNoPosition) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000883 masm_->positions_recorder()->RecordPosition(pos);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000884 }
885}
886
887
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000888// Lookup table for code generators for special runtime calls which are
889// generated inline.
890#define INLINE_FUNCTION_GENERATOR_ADDRESS(Name, argc, ressize) \
891 &FullCodeGenerator::Emit##Name,
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000892
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000893const FullCodeGenerator::InlineFunctionGenerator
894 FullCodeGenerator::kInlineFunctionGenerators[] = {
895 INLINE_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS)
896 INLINE_RUNTIME_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS)
897 };
898#undef INLINE_FUNCTION_GENERATOR_ADDRESS
899
900
901FullCodeGenerator::InlineFunctionGenerator
902 FullCodeGenerator::FindInlineFunctionGenerator(Runtime::FunctionId id) {
whesse@chromium.org023421e2010-12-21 12:19:12 +0000903 int lookup_index =
904 static_cast<int>(id) - static_cast<int>(Runtime::kFirstInlineFunction);
905 ASSERT(lookup_index >= 0);
906 ASSERT(static_cast<size_t>(lookup_index) <
907 ARRAY_SIZE(kInlineFunctionGenerators));
908 return kInlineFunctionGenerators[lookup_index];
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000909}
910
911
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000912void FullCodeGenerator::EmitInlineRuntimeCall(CallRuntime* expr) {
913 const Runtime::Function* function = expr->function();
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000914 ASSERT(function != NULL);
915 ASSERT(function->intrinsic_type == Runtime::INLINE);
916 InlineFunctionGenerator generator =
917 FindInlineFunctionGenerator(function->function_id);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000918 ((*this).*(generator))(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000919}
920
921
922void FullCodeGenerator::VisitBinaryOperation(BinaryOperation* expr) {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000923 switch (expr->op()) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000924 case Token::COMMA:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000925 return VisitComma(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000926 case Token::OR:
927 case Token::AND:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000928 return VisitLogicalExpression(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000929 default:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000930 return VisitArithmeticExpression(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000931 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000932}
933
934
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000935void FullCodeGenerator::VisitInDuplicateContext(Expression* expr) {
936 if (context()->IsEffect()) {
937 VisitForEffect(expr);
938 } else if (context()->IsAccumulatorValue()) {
939 VisitForAccumulatorValue(expr);
940 } else if (context()->IsStackValue()) {
941 VisitForStackValue(expr);
942 } else if (context()->IsTest()) {
943 const TestContext* test = TestContext::cast(context());
944 VisitForControl(expr, test->true_label(), test->false_label(),
945 test->fall_through());
946 }
947}
948
949
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000950void FullCodeGenerator::VisitComma(BinaryOperation* expr) {
951 Comment cmnt(masm_, "[ Comma");
952 VisitForEffect(expr->left());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000953 VisitInDuplicateContext(expr->right());
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000954}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000955
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000956
957void FullCodeGenerator::VisitLogicalExpression(BinaryOperation* expr) {
958 bool is_logical_and = expr->op() == Token::AND;
959 Comment cmnt(masm_, is_logical_and ? "[ Logical AND" : "[ Logical OR");
960 Expression* left = expr->left();
961 Expression* right = expr->right();
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000962 BailoutId right_id = expr->RightId();
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000963 Label done;
964
965 if (context()->IsTest()) {
966 Label eval_right;
967 const TestContext* test = TestContext::cast(context());
968 if (is_logical_and) {
969 VisitForControl(left, &eval_right, test->false_label(), &eval_right);
970 } else {
971 VisitForControl(left, test->true_label(), &eval_right, &eval_right);
972 }
973 PrepareForBailoutForId(right_id, NO_REGISTERS);
974 __ bind(&eval_right);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000975
976 } else if (context()->IsAccumulatorValue()) {
977 VisitForAccumulatorValue(left);
978 // We want the value in the accumulator for the test, and on the stack in
979 // case we need it.
980 __ push(result_register());
981 Label discard, restore;
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000982 if (is_logical_and) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000983 DoTest(left, &discard, &restore, &restore);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000984 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000985 DoTest(left, &restore, &discard, &restore);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000986 }
987 __ bind(&restore);
988 __ pop(result_register());
989 __ jmp(&done);
990 __ bind(&discard);
991 __ Drop(1);
992 PrepareForBailoutForId(right_id, NO_REGISTERS);
993
994 } else if (context()->IsStackValue()) {
995 VisitForAccumulatorValue(left);
996 // We want the value in the accumulator for the test, and on the stack in
997 // case we need it.
998 __ push(result_register());
999 Label discard;
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001000 if (is_logical_and) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001001 DoTest(left, &discard, &done, &discard);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001002 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001003 DoTest(left, &done, &discard, &discard);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001004 }
1005 __ bind(&discard);
1006 __ Drop(1);
1007 PrepareForBailoutForId(right_id, NO_REGISTERS);
1008
1009 } else {
1010 ASSERT(context()->IsEffect());
1011 Label eval_right;
1012 if (is_logical_and) {
1013 VisitForControl(left, &eval_right, &done, &eval_right);
1014 } else {
1015 VisitForControl(left, &done, &eval_right, &eval_right);
1016 }
1017 PrepareForBailoutForId(right_id, NO_REGISTERS);
1018 __ bind(&eval_right);
1019 }
1020
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001021 VisitInDuplicateContext(right);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001022 __ bind(&done);
1023}
1024
1025
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001026void FullCodeGenerator::VisitArithmeticExpression(BinaryOperation* expr) {
1027 Token::Value op = expr->op();
1028 Comment cmnt(masm_, "[ ArithmeticExpression");
1029 Expression* left = expr->left();
1030 Expression* right = expr->right();
1031 OverwriteMode mode =
1032 left->ResultOverwriteAllowed()
1033 ? OVERWRITE_LEFT
1034 : (right->ResultOverwriteAllowed() ? OVERWRITE_RIGHT : NO_OVERWRITE);
1035
1036 VisitForStackValue(left);
1037 VisitForAccumulatorValue(right);
1038
1039 SetSourcePosition(expr->position());
1040 if (ShouldInlineSmiCase(op)) {
1041 EmitInlineSmiBinaryOp(expr, op, mode, left, right);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001042 } else {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001043 EmitBinaryOp(expr, op, mode);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001044 }
1045}
1046
1047
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001048void FullCodeGenerator::VisitBlock(Block* stmt) {
1049 Comment cmnt(masm_, "[ Block");
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001050 NestedBlock nested_block(this, stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001051 SetStatementPosition(stmt);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001052
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001053 Scope* saved_scope = scope();
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001054 // Push a block context when entering a block with block scoped variables.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00001055 if (stmt->scope() != NULL) {
danno@chromium.org81cac2b2012-07-10 11:28:27 +00001056 scope_ = stmt->scope();
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00001057 ASSERT(!scope_->is_module_scope());
1058 { Comment cmnt(masm_, "[ Extend block context");
1059 Handle<ScopeInfo> scope_info = scope_->GetScopeInfo();
1060 int heap_slots = scope_info->ContextLength() - Context::MIN_CONTEXT_SLOTS;
1061 __ Push(scope_info);
1062 PushFunctionArgumentForContextAllocation();
1063 if (heap_slots <= FastNewBlockContextStub::kMaximumSlots) {
1064 FastNewBlockContextStub stub(heap_slots);
1065 __ CallStub(&stub);
1066 } else {
1067 __ CallRuntime(Runtime::kPushBlockContext, 2);
1068 }
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001069
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00001070 // Replace the context stored in the frame.
1071 StoreToFrameField(StandardFrameConstants::kContextOffset,
1072 context_register());
1073 }
1074 { Comment cmnt(masm_, "[ Declarations");
1075 VisitDeclarations(scope_->declarations());
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001076 }
1077 }
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00001078
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00001079 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001080 VisitStatements(stmt->statements());
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001081 scope_ = saved_scope;
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001082 __ bind(nested_block.break_label());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001083 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001084
1085 // Pop block context if necessary.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00001086 if (stmt->scope() != NULL) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001087 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1088 // Update local stack frame context field.
1089 StoreToFrameField(StandardFrameConstants::kContextOffset,
1090 context_register());
1091 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001092}
1093
1094
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00001095void FullCodeGenerator::VisitModuleStatement(ModuleStatement* stmt) {
1096 Comment cmnt(masm_, "[ Module context");
1097
1098 __ Push(Smi::FromInt(stmt->proxy()->interface()->Index()));
1099 __ Push(Smi::FromInt(0));
1100 __ CallRuntime(Runtime::kPushModuleContext, 2);
1101 StoreToFrameField(
1102 StandardFrameConstants::kContextOffset, context_register());
1103
1104 Scope* saved_scope = scope_;
1105 scope_ = stmt->body()->scope();
1106 VisitStatements(stmt->body()->statements());
1107 scope_ = saved_scope;
1108 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1109 // Update local stack frame context field.
1110 StoreToFrameField(StandardFrameConstants::kContextOffset,
1111 context_register());
1112}
1113
1114
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001115void FullCodeGenerator::VisitExpressionStatement(ExpressionStatement* stmt) {
1116 Comment cmnt(masm_, "[ ExpressionStatement");
1117 SetStatementPosition(stmt);
1118 VisitForEffect(stmt->expression());
1119}
1120
1121
1122void FullCodeGenerator::VisitEmptyStatement(EmptyStatement* stmt) {
1123 Comment cmnt(masm_, "[ EmptyStatement");
1124 SetStatementPosition(stmt);
1125}
1126
1127
1128void FullCodeGenerator::VisitIfStatement(IfStatement* stmt) {
1129 Comment cmnt(masm_, "[ IfStatement");
1130 SetStatementPosition(stmt);
1131 Label then_part, else_part, done;
1132
ricow@chromium.org65fae842010-08-25 15:26:24 +00001133 if (stmt->HasElseStatement()) {
1134 VisitForControl(stmt->condition(), &then_part, &else_part, &then_part);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001135 PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001136 __ bind(&then_part);
1137 Visit(stmt->then_statement());
1138 __ jmp(&done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001139
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001140 PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001141 __ bind(&else_part);
1142 Visit(stmt->else_statement());
1143 } else {
1144 VisitForControl(stmt->condition(), &then_part, &done, &then_part);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001145 PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001146 __ bind(&then_part);
1147 Visit(stmt->then_statement());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001148
1149 PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001150 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001151 __ bind(&done);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001152 PrepareForBailoutForId(stmt->IfId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001153}
1154
1155
1156void FullCodeGenerator::VisitContinueStatement(ContinueStatement* stmt) {
1157 Comment cmnt(masm_, "[ ContinueStatement");
1158 SetStatementPosition(stmt);
1159 NestedStatement* current = nesting_stack_;
1160 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001161 int context_length = 0;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001162 // When continuing, we clobber the unpredictable value in the accumulator
1163 // with one that's safe for GC. If we hit an exit from the try block of
1164 // try...finally on our way out, we will unconditionally preserve the
1165 // accumulator on the stack.
1166 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001167 while (!current->IsContinueTarget(stmt->target())) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001168 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001169 }
1170 __ Drop(stack_depth);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001171 if (context_length > 0) {
1172 while (context_length > 0) {
1173 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1174 --context_length;
1175 }
1176 StoreToFrameField(StandardFrameConstants::kContextOffset,
1177 context_register());
1178 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001179
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001180 __ jmp(current->AsIteration()->continue_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001181}
1182
1183
1184void FullCodeGenerator::VisitBreakStatement(BreakStatement* stmt) {
1185 Comment cmnt(masm_, "[ BreakStatement");
1186 SetStatementPosition(stmt);
1187 NestedStatement* current = nesting_stack_;
1188 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001189 int context_length = 0;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001190 // When breaking, we clobber the unpredictable value in the accumulator
1191 // with one that's safe for GC. If we hit an exit from the try block of
1192 // try...finally on our way out, we will unconditionally preserve the
1193 // accumulator on the stack.
1194 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001195 while (!current->IsBreakTarget(stmt->target())) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001196 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001197 }
1198 __ Drop(stack_depth);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001199 if (context_length > 0) {
1200 while (context_length > 0) {
1201 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1202 --context_length;
1203 }
1204 StoreToFrameField(StandardFrameConstants::kContextOffset,
1205 context_register());
1206 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001207
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001208 __ jmp(current->AsBreakable()->break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001209}
1210
1211
1212void FullCodeGenerator::VisitReturnStatement(ReturnStatement* stmt) {
1213 Comment cmnt(masm_, "[ ReturnStatement");
1214 SetStatementPosition(stmt);
1215 Expression* expr = stmt->expression();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001216 VisitForAccumulatorValue(expr);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001217
1218 // Exit all nested statements.
1219 NestedStatement* current = nesting_stack_;
1220 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001221 int context_length = 0;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001222 while (current != NULL) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001223 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001224 }
1225 __ Drop(stack_depth);
1226
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001227 EmitReturnSequence();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001228}
1229
1230
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001231void FullCodeGenerator::VisitWithStatement(WithStatement* stmt) {
1232 Comment cmnt(masm_, "[ WithStatement");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001233 SetStatementPosition(stmt);
1234
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001235 VisitForStackValue(stmt->expression());
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00001236 PushFunctionArgumentForContextAllocation();
1237 __ CallRuntime(Runtime::kPushWithContext, 2);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001238 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001239
1240 { WithOrCatch body(this);
1241 Visit(stmt->statement());
1242 }
1243
1244 // Pop context.
1245 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1246 // Update local stack frame context field.
1247 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001248}
1249
1250
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001251void FullCodeGenerator::VisitDoWhileStatement(DoWhileStatement* stmt) {
1252 Comment cmnt(masm_, "[ DoWhileStatement");
1253 SetStatementPosition(stmt);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001254 Label body, stack_check;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001255
1256 Iteration loop_statement(this, stmt);
1257 increment_loop_depth();
1258
1259 __ bind(&body);
1260 Visit(stmt->body());
1261
ricow@chromium.org65fae842010-08-25 15:26:24 +00001262 // Record the position of the do while condition and make sure it is
1263 // possible to break on the condition.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001264 __ bind(loop_statement.continue_label());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001265 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001266 SetExpressionPosition(stmt->cond(), stmt->condition_position());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001267 VisitForControl(stmt->cond(),
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001268 &stack_check,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001269 loop_statement.break_label(),
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001270 &stack_check);
1271
1272 // Check stack before looping.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001273 PrepareForBailoutForId(stmt->BackEdgeId(), NO_REGISTERS);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001274 __ bind(&stack_check);
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +00001275 EmitBackEdgeBookkeeping(stmt, &body);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001276 __ jmp(&body);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001277
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001278 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001279 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001280 decrement_loop_depth();
1281}
1282
1283
1284void FullCodeGenerator::VisitWhileStatement(WhileStatement* stmt) {
1285 Comment cmnt(masm_, "[ WhileStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001286 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001287
1288 Iteration loop_statement(this, stmt);
1289 increment_loop_depth();
1290
1291 // Emit the test at the bottom of the loop.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001292 __ jmp(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001293
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001294 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001295 __ bind(&body);
1296 Visit(stmt->body());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001297
1298 // Emit the statement position here as this is where the while
1299 // statement code starts.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001300 __ bind(loop_statement.continue_label());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001301 SetStatementPosition(stmt);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001302
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001303 // Check stack before looping.
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +00001304 EmitBackEdgeBookkeeping(stmt, &body);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001305
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001306 __ bind(&test);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001307 VisitForControl(stmt->cond(),
1308 &body,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001309 loop_statement.break_label(),
1310 loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001311
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001312 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001313 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001314 decrement_loop_depth();
1315}
1316
1317
1318void FullCodeGenerator::VisitForStatement(ForStatement* stmt) {
1319 Comment cmnt(masm_, "[ ForStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001320 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001321
1322 Iteration loop_statement(this, stmt);
erik.corry@gmail.combbceb572012-03-09 10:52:05 +00001323
1324 // Set statement position for a break slot before entering the for-body.
1325 SetStatementPosition(stmt);
1326
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001327 if (stmt->init() != NULL) {
1328 Visit(stmt->init());
1329 }
1330
1331 increment_loop_depth();
1332 // Emit the test at the bottom of the loop (even if empty).
1333 __ jmp(&test);
1334
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001335 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001336 __ bind(&body);
1337 Visit(stmt->body());
1338
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001339 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001340 __ bind(loop_statement.continue_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001341 if (stmt->next() != NULL) {
1342 Visit(stmt->next());
1343 }
1344
ricow@chromium.org65fae842010-08-25 15:26:24 +00001345 // Emit the statement position here as this is where the for
1346 // statement code starts.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001347 SetStatementPosition(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001348
1349 // Check stack before looping.
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +00001350 EmitBackEdgeBookkeeping(stmt, &body);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001351
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001352 __ bind(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001353 if (stmt->cond() != NULL) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00001354 VisitForControl(stmt->cond(),
1355 &body,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001356 loop_statement.break_label(),
1357 loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001358 } else {
1359 __ jmp(&body);
1360 }
1361
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001362 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001363 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001364 decrement_loop_depth();
1365}
1366
1367
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001368void FullCodeGenerator::VisitTryCatchStatement(TryCatchStatement* stmt) {
1369 Comment cmnt(masm_, "[ TryCatchStatement");
1370 SetStatementPosition(stmt);
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001371 // The try block adds a handler to the exception handler chain before
1372 // entering, and removes it again when exiting normally. If an exception
1373 // is thrown during execution of the try block, the handler is consumed
1374 // and control is passed to the catch block with the exception in the
1375 // result register.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001376
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001377 Label try_entry, handler_entry, exit;
1378 __ jmp(&try_entry);
1379 __ bind(&handler_entry);
1380 handler_table()->set(stmt->index(), Smi::FromInt(handler_entry.pos()));
1381 // Exception handler code, the exception is in the result register.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001382 // Extend the context before executing the catch block.
1383 { Comment cmnt(masm_, "[ Extend catch context");
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001384 __ Push(stmt->variable()->name());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001385 __ push(result_register());
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00001386 PushFunctionArgumentForContextAllocation();
1387 __ CallRuntime(Runtime::kPushCatchContext, 3);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001388 StoreToFrameField(StandardFrameConstants::kContextOffset,
1389 context_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001390 }
1391
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001392 Scope* saved_scope = scope();
1393 scope_ = stmt->scope();
1394 ASSERT(scope_->declarations()->is_empty());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001395 { WithOrCatch catch_body(this);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001396 Visit(stmt->catch_block());
1397 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001398 // Restore the context.
1399 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1400 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001401 scope_ = saved_scope;
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001402 __ jmp(&exit);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001403
1404 // Try block code. Sets up the exception handler chain.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001405 __ bind(&try_entry);
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00001406 __ PushTryHandler(StackHandler::CATCH, stmt->index());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001407 { TryCatch try_body(this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001408 Visit(stmt->try_block());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001409 }
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001410 __ PopTryHandler();
1411 __ bind(&exit);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001412}
1413
1414
1415void FullCodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* stmt) {
1416 Comment cmnt(masm_, "[ TryFinallyStatement");
1417 SetStatementPosition(stmt);
1418 // Try finally is compiled by setting up a try-handler on the stack while
1419 // executing the try body, and removing it again afterwards.
1420 //
1421 // The try-finally construct can enter the finally block in three ways:
1422 // 1. By exiting the try-block normally. This removes the try-handler and
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001423 // calls the finally block code before continuing.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001424 // 2. By exiting the try-block with a function-local control flow transfer
1425 // (break/continue/return). The site of the, e.g., break removes the
1426 // try handler and calls the finally block code before continuing
1427 // its outward control transfer.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001428 // 3. By exiting the try-block with a thrown exception.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001429 // This can happen in nested function calls. It traverses the try-handler
1430 // chain and consumes the try-handler entry before jumping to the
1431 // handler code. The handler code then calls the finally-block before
1432 // rethrowing the exception.
1433 //
1434 // The finally block must assume a return address on top of the stack
1435 // (or in the link register on ARM chips) and a value (return value or
1436 // exception) in the result register (rax/eax/r0), both of which must
1437 // be preserved. The return address isn't GC-safe, so it should be
1438 // cooked before GC.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001439 Label try_entry, handler_entry, finally_entry;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001440
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001441 // Jump to try-handler setup and try-block code.
1442 __ jmp(&try_entry);
1443 __ bind(&handler_entry);
1444 handler_table()->set(stmt->index(), Smi::FromInt(handler_entry.pos()));
1445 // Exception handler code. This code is only executed when an exception
1446 // is thrown. The exception is in the result register, and must be
1447 // preserved by the finally block. Call the finally block and then
1448 // rethrow the exception if it returns.
1449 __ Call(&finally_entry);
1450 __ push(result_register());
1451 __ CallRuntime(Runtime::kReThrow, 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001452
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001453 // Finally block implementation.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001454 __ bind(&finally_entry);
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001455 EnterFinallyBlock();
1456 { Finally finally_body(this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001457 Visit(stmt->finally_block());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001458 }
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001459 ExitFinallyBlock(); // Return to the calling code.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001460
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001461 // Set up try handler.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001462 __ bind(&try_entry);
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00001463 __ PushTryHandler(StackHandler::FINALLY, stmt->index());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001464 { TryFinally try_body(this, &finally_entry);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001465 Visit(stmt->try_block());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001466 }
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001467 __ PopTryHandler();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001468 // Execute the finally block on the way out. Clobber the unpredictable
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001469 // value in the result register with one that's safe for GC because the
1470 // finally block will unconditionally preserve the result register on the
1471 // stack.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001472 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001473 __ Call(&finally_entry);
1474}
1475
1476
1477void FullCodeGenerator::VisitDebuggerStatement(DebuggerStatement* stmt) {
1478#ifdef ENABLE_DEBUGGER_SUPPORT
1479 Comment cmnt(masm_, "[ DebuggerStatement");
1480 SetStatementPosition(stmt);
1481
ager@chromium.org5c838252010-02-19 08:53:10 +00001482 __ DebugBreak();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001483 // Ignore the return value.
1484#endif
1485}
1486
1487
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001488void FullCodeGenerator::VisitConditional(Conditional* expr) {
1489 Comment cmnt(masm_, "[ Conditional");
1490 Label true_case, false_case, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001491 VisitForControl(expr->condition(), &true_case, &false_case, &true_case);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001492
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001493 PrepareForBailoutForId(expr->ThenId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001494 __ bind(&true_case);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001495 SetExpressionPosition(expr->then_expression(),
1496 expr->then_expression_position());
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001497 if (context()->IsTest()) {
1498 const TestContext* for_test = TestContext::cast(context());
1499 VisitForControl(expr->then_expression(),
1500 for_test->true_label(),
1501 for_test->false_label(),
1502 NULL);
1503 } else {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001504 VisitInDuplicateContext(expr->then_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001505 __ jmp(&done);
1506 }
1507
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001508 PrepareForBailoutForId(expr->ElseId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001509 __ bind(&false_case);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001510 SetExpressionPosition(expr->else_expression(),
1511 expr->else_expression_position());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001512 VisitInDuplicateContext(expr->else_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001513 // If control flow falls through Visit, merge it with true case here.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001514 if (!context()->IsTest()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001515 __ bind(&done);
1516 }
1517}
1518
1519
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001520void FullCodeGenerator::VisitLiteral(Literal* expr) {
1521 Comment cmnt(masm_, "[ Literal");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001522 context()->Plug(expr->handle());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001523}
1524
1525
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001526void FullCodeGenerator::VisitFunctionLiteral(FunctionLiteral* expr) {
1527 Comment cmnt(masm_, "[ FunctionLiteral");
1528
1529 // Build the function boilerplate and instantiate it.
1530 Handle<SharedFunctionInfo> function_info =
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001531 Compiler::BuildFunctionInfo(expr, script());
1532 if (function_info.is_null()) {
1533 SetStackOverflow();
1534 return;
1535 }
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001536 EmitNewClosure(function_info, expr->pretenure());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001537}
1538
1539
1540void FullCodeGenerator::VisitSharedFunctionInfoLiteral(
1541 SharedFunctionInfoLiteral* expr) {
1542 Comment cmnt(masm_, "[ SharedFunctionInfoLiteral");
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001543 EmitNewClosure(expr->shared_function_info(), false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001544}
1545
1546
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00001547void FullCodeGenerator::VisitYield(Yield* expr) {
1548 if (expr->is_delegating_yield())
1549 UNIMPLEMENTED();
1550
1551 Comment cmnt(masm_, "[ Yield");
1552 VisitForAccumulatorValue(expr->expression());
1553 // TODO(wingo): Assert that the operand stack depth is 0, at least while
1554 // general yield expressions are unimplemented.
1555
1556 // TODO(wingo): What follows is as in VisitReturnStatement. Replace it with a
1557 // call to a builtin that will resume the generator.
1558 NestedStatement* current = nesting_stack_;
1559 int stack_depth = 0;
1560 int context_length = 0;
1561 while (current != NULL) {
1562 current = current->Exit(&stack_depth, &context_length);
1563 }
1564 __ Drop(stack_depth);
1565 EmitReturnSequence();
1566}
1567
1568
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001569void FullCodeGenerator::VisitThrow(Throw* expr) {
1570 Comment cmnt(masm_, "[ Throw");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001571 VisitForStackValue(expr->exception());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001572 __ CallRuntime(Runtime::kThrow, 1);
1573 // Never returns here.
1574}
1575
1576
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001577FullCodeGenerator::NestedStatement* FullCodeGenerator::TryCatch::Exit(
1578 int* stack_depth,
1579 int* context_length) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001580 // The macros used here must preserve the result register.
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001581 __ Drop(*stack_depth);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001582 __ PopTryHandler();
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001583 *stack_depth = 0;
1584 return previous_;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001585}
1586
ricow@chromium.org65fae842010-08-25 15:26:24 +00001587
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001588bool FullCodeGenerator::TryLiteralCompare(CompareOperation* expr) {
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001589 Expression* sub_expr;
ager@chromium.org04921a82011-06-27 13:21:41 +00001590 Handle<String> check;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001591 if (expr->IsLiteralCompareTypeof(&sub_expr, &check)) {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001592 EmitLiteralCompareTypeof(expr, sub_expr, check);
ager@chromium.org04921a82011-06-27 13:21:41 +00001593 return true;
1594 }
1595
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001596 if (expr->IsLiteralCompareUndefined(&sub_expr)) {
1597 EmitLiteralCompareNil(expr, sub_expr, kUndefinedValue);
1598 return true;
1599 }
1600
1601 if (expr->IsLiteralCompareNull(&sub_expr)) {
1602 EmitLiteralCompareNil(expr, sub_expr, kNullValue);
ager@chromium.org04921a82011-06-27 13:21:41 +00001603 return true;
1604 }
1605
1606 return false;
1607}
1608
1609
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001610#undef __
1611
1612
1613} } // namespace v8::internal