blob: 165d007571f227460fcc3ea2ab53b4f3e6475e96 [file] [log] [blame]
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00001// Copyright 2012 the V8 project authors. All rights reserved.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
karlklose@chromium.org44bc7082011-04-11 12:33:05 +000030#include "codegen.h"
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000031#include "compiler.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000032#include "debug.h"
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000033#include "full-codegen.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000034#include "liveedit.h"
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +000035#include "macro-assembler.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000036#include "prettyprinter.h"
sgjesse@chromium.org833cdd72010-02-26 10:06:16 +000037#include "scopes.h"
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +000038#include "scopeinfo.h"
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +000039#include "snapshot.h"
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000040#include "stub-cache.h"
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000041
42namespace v8 {
43namespace internal {
44
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +000045void BreakableStatementChecker::Check(Statement* stmt) {
46 Visit(stmt);
47}
48
49
50void BreakableStatementChecker::Check(Expression* expr) {
51 Visit(expr);
52}
53
54
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +000055void BreakableStatementChecker::VisitVariableDeclaration(
56 VariableDeclaration* decl) {
57}
58
ulan@chromium.org812308e2012-02-29 15:58:45 +000059void BreakableStatementChecker::VisitFunctionDeclaration(
60 FunctionDeclaration* decl) {
61}
62
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +000063void BreakableStatementChecker::VisitModuleDeclaration(
64 ModuleDeclaration* decl) {
65}
66
ulan@chromium.org812308e2012-02-29 15:58:45 +000067void BreakableStatementChecker::VisitImportDeclaration(
68 ImportDeclaration* decl) {
69}
70
71void BreakableStatementChecker::VisitExportDeclaration(
72 ExportDeclaration* decl) {
73}
74
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +000075
76void BreakableStatementChecker::VisitModuleLiteral(ModuleLiteral* module) {
77}
78
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +000079
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +000080void BreakableStatementChecker::VisitModuleVariable(ModuleVariable* module) {
81}
82
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +000083
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +000084void BreakableStatementChecker::VisitModulePath(ModulePath* module) {
85}
86
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +000087
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +000088void BreakableStatementChecker::VisitModuleUrl(ModuleUrl* module) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +000089}
90
91
ulan@chromium.org8e8d8822012-11-23 14:36:46 +000092void BreakableStatementChecker::VisitModuleStatement(ModuleStatement* stmt) {
93}
94
95
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +000096void BreakableStatementChecker::VisitBlock(Block* stmt) {
97}
98
99
100void BreakableStatementChecker::VisitExpressionStatement(
101 ExpressionStatement* stmt) {
102 // Check if expression is breakable.
103 Visit(stmt->expression());
104}
105
106
107void BreakableStatementChecker::VisitEmptyStatement(EmptyStatement* stmt) {
108}
109
110
111void BreakableStatementChecker::VisitIfStatement(IfStatement* stmt) {
112 // If the condition is breakable the if statement is breakable.
113 Visit(stmt->condition());
114}
115
116
117void BreakableStatementChecker::VisitContinueStatement(
118 ContinueStatement* stmt) {
119}
120
121
122void BreakableStatementChecker::VisitBreakStatement(BreakStatement* stmt) {
123}
124
125
126void BreakableStatementChecker::VisitReturnStatement(ReturnStatement* stmt) {
127 // Return is breakable if the expression is.
128 Visit(stmt->expression());
129}
130
131
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000132void BreakableStatementChecker::VisitWithStatement(WithStatement* stmt) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000133 Visit(stmt->expression());
134}
135
136
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000137void BreakableStatementChecker::VisitSwitchStatement(SwitchStatement* stmt) {
138 // Switch statements breakable if the tag expression is.
139 Visit(stmt->tag());
140}
141
142
143void BreakableStatementChecker::VisitDoWhileStatement(DoWhileStatement* stmt) {
144 // Mark do while as breakable to avoid adding a break slot in front of it.
145 is_breakable_ = true;
146}
147
148
149void BreakableStatementChecker::VisitWhileStatement(WhileStatement* stmt) {
150 // Mark while statements breakable if the condition expression is.
151 Visit(stmt->cond());
152}
153
154
155void BreakableStatementChecker::VisitForStatement(ForStatement* stmt) {
156 // Mark for statements breakable if the condition expression is.
157 if (stmt->cond() != NULL) {
158 Visit(stmt->cond());
159 }
160}
161
162
163void BreakableStatementChecker::VisitForInStatement(ForInStatement* stmt) {
164 // Mark for in statements breakable if the enumerable expression is.
165 Visit(stmt->enumerable());
166}
167
168
danno@chromium.org1fd77d52013-06-07 16:01:45 +0000169void BreakableStatementChecker::VisitForOfStatement(ForOfStatement* stmt) {
170 // For-of is breakable because of the next() call.
171 is_breakable_ = true;
172}
173
174
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000175void BreakableStatementChecker::VisitTryCatchStatement(
176 TryCatchStatement* stmt) {
177 // Mark try catch as breakable to avoid adding a break slot in front of it.
178 is_breakable_ = true;
179}
180
181
182void BreakableStatementChecker::VisitTryFinallyStatement(
183 TryFinallyStatement* stmt) {
184 // Mark try finally as breakable to avoid adding a break slot in front of it.
185 is_breakable_ = true;
186}
187
188
189void BreakableStatementChecker::VisitDebuggerStatement(
190 DebuggerStatement* stmt) {
191 // The debugger statement is breakable.
192 is_breakable_ = true;
193}
194
195
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +0000196void BreakableStatementChecker::VisitCaseClause(CaseClause* clause) {
197}
198
199
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000200void BreakableStatementChecker::VisitFunctionLiteral(FunctionLiteral* expr) {
201}
202
203
jkummerow@chromium.orgfb7a7c42013-10-02 11:41:02 +0000204void BreakableStatementChecker::VisitNativeFunctionLiteral(
205 NativeFunctionLiteral* expr) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000206}
207
208
209void BreakableStatementChecker::VisitConditional(Conditional* expr) {
210}
211
212
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000213void BreakableStatementChecker::VisitVariableProxy(VariableProxy* expr) {
214}
215
216
217void BreakableStatementChecker::VisitLiteral(Literal* expr) {
218}
219
220
221void BreakableStatementChecker::VisitRegExpLiteral(RegExpLiteral* expr) {
222}
223
224
225void BreakableStatementChecker::VisitObjectLiteral(ObjectLiteral* expr) {
226}
227
228
229void BreakableStatementChecker::VisitArrayLiteral(ArrayLiteral* expr) {
230}
231
232
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000233void BreakableStatementChecker::VisitAssignment(Assignment* expr) {
234 // If assigning to a property (including a global property) the assignment is
235 // breakable.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000236 VariableProxy* proxy = expr->target()->AsVariableProxy();
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000237 Property* prop = expr->target()->AsProperty();
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000238 if (prop != NULL || (proxy != NULL && proxy->var()->IsUnallocated())) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000239 is_breakable_ = true;
240 return;
241 }
242
243 // Otherwise the assignment is breakable if the assigned value is.
244 Visit(expr->value());
245}
246
247
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000248void BreakableStatementChecker::VisitYield(Yield* expr) {
249 // Yield is breakable if the expression is.
250 Visit(expr->expression());
251}
252
253
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000254void BreakableStatementChecker::VisitThrow(Throw* expr) {
255 // Throw is breakable if the expression is.
256 Visit(expr->exception());
257}
258
259
260void BreakableStatementChecker::VisitProperty(Property* expr) {
261 // Property load is breakable.
262 is_breakable_ = true;
263}
264
265
266void BreakableStatementChecker::VisitCall(Call* expr) {
267 // Function calls both through IC and call stub are breakable.
268 is_breakable_ = true;
269}
270
271
272void BreakableStatementChecker::VisitCallNew(CallNew* expr) {
273 // Function calls through new are breakable.
274 is_breakable_ = true;
275}
276
277
278void BreakableStatementChecker::VisitCallRuntime(CallRuntime* expr) {
279}
280
281
282void BreakableStatementChecker::VisitUnaryOperation(UnaryOperation* expr) {
283 Visit(expr->expression());
284}
285
286
287void BreakableStatementChecker::VisitCountOperation(CountOperation* expr) {
288 Visit(expr->expression());
289}
290
291
292void BreakableStatementChecker::VisitBinaryOperation(BinaryOperation* expr) {
293 Visit(expr->left());
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +0000294 if (expr->op() != Token::AND &&
295 expr->op() != Token::OR) {
296 Visit(expr->right());
297 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000298}
299
300
301void BreakableStatementChecker::VisitCompareOperation(CompareOperation* expr) {
302 Visit(expr->left());
303 Visit(expr->right());
304}
305
306
307void BreakableStatementChecker::VisitThisFunction(ThisFunction* expr) {
308}
309
310
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000311#define __ ACCESS_MASM(masm())
312
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000313bool FullCodeGenerator::MakeCode(CompilationInfo* info) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000314 Isolate* isolate = info->isolate();
yangguo@chromium.org49546742013-12-23 16:17:49 +0000315
316 Logger::TimerEventScope timer(
317 isolate, Logger::TimerEventScope::v8_compile_full_code);
318
ager@chromium.org5c838252010-02-19 08:53:10 +0000319 Handle<Script> script = info->script();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000320 if (!script->IsUndefined() && !script->source()->IsUndefined()) {
321 int len = String::cast(script->source())->length();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000322 isolate->counters()->total_full_codegen_source_size()->Increment(len);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000323 }
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +0000324 CodeGenerator::MakeCodePrologue(info, "full");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000325 const int kInitialBufferSize = 4 * KB;
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000326 MacroAssembler masm(info->isolate(), NULL, kInitialBufferSize);
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000327#ifdef ENABLE_GDB_JIT_INTERFACE
328 masm.positions_recorder()->StartGDBJITLineInfoRecording();
329#endif
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000330 LOG_CODE_EVENT(isolate,
331 CodeStartLinePosInfoRecordEvent(masm.positions_recorder()));
ager@chromium.org5c838252010-02-19 08:53:10 +0000332
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000333 FullCodeGenerator cgen(&masm, info);
yangguo@chromium.org56454712012-02-16 15:33:53 +0000334 cgen.Generate();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000335 if (cgen.HasStackOverflow()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000336 ASSERT(!isolate->has_pending_exception());
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000337 return false;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000338 }
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000339 unsigned table_offset = cgen.EmitBackEdgeTable();
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000340
lrn@chromium.org34e60782011-09-15 07:25:40 +0000341 Code::Flags flags = Code::ComputeFlags(Code::FUNCTION);
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000342 Handle<Code> code = CodeGenerator::MakeCodeEpilogue(&masm, flags, info);
rossberg@chromium.org2c067b12012-03-19 11:01:52 +0000343 code->set_optimizable(info->IsOptimizable() &&
jkummerow@chromium.org2c9426b2013-09-05 16:31:13 +0000344 !info->function()->dont_optimize() &&
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000345 info->function()->scope()->AllowsLazyCompilation());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000346 cgen.PopulateDeoptimizationData(code);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000347 cgen.PopulateTypeFeedbackInfo(code);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000348 code->set_has_deoptimization_support(info->HasDeoptimizationSupport());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +0000349 code->set_handler_table(*cgen.handler_table());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000350#ifdef ENABLE_DEBUGGER_SUPPORT
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000351 code->set_compiled_optimizable(info->IsOptimizable());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000352#endif // ENABLE_DEBUGGER_SUPPORT
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000353 code->set_allow_osr_at_loop_nesting_level(0);
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000354 code->set_profiler_ticks(0);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000355 code->set_back_edge_table_offset(table_offset);
356 code->set_back_edges_patched_for_osr(false);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000357 CodeGenerator::PrintCode(code, info);
rossberg@chromium.orgebeba022013-08-19 09:36:44 +0000358 info->SetCode(code);
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000359#ifdef ENABLE_GDB_JIT_INTERFACE
rossberg@chromium.orgebeba022013-08-19 09:36:44 +0000360 if (FLAG_gdbjit) {
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000361 GDBJITLineInfo* lineinfo =
362 masm.positions_recorder()->DetachGDBJITLineInfo();
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000363 GDBJIT(RegisterDetailedLineInfo(*code, lineinfo));
364 }
365#endif
rossberg@chromium.orgebeba022013-08-19 09:36:44 +0000366 void* line_info = masm.positions_recorder()->DetachJITHandlerData();
367 LOG_CODE_EVENT(isolate, CodeEndLinePosInfoRecordEvent(*code, line_info));
368 return true;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000369}
370
371
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000372unsigned FullCodeGenerator::EmitBackEdgeTable() {
373 // The back edge table consists of a length (in number of entries)
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000374 // field, and then a sequence of entries. Each entry is a pair of AST id
375 // and code-relative pc offset.
376 masm()->Align(kIntSize);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000377 unsigned offset = masm()->pc_offset();
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000378 unsigned length = back_edges_.length();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000379 __ dd(length);
380 for (unsigned i = 0; i < length; ++i) {
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000381 __ dd(back_edges_[i].id.ToInt());
382 __ dd(back_edges_[i].pc);
jkummerow@chromium.orgba72ec82013-07-22 09:21:20 +0000383 __ dd(back_edges_[i].loop_depth);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000384 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000385 return offset;
386}
387
388
389void FullCodeGenerator::PopulateDeoptimizationData(Handle<Code> code) {
390 // Fill in the deoptimization information.
391 ASSERT(info_->HasDeoptimizationSupport() || bailout_entries_.is_empty());
392 if (!info_->HasDeoptimizationSupport()) return;
393 int length = bailout_entries_.length();
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000394 Handle<DeoptimizationOutputData> data = isolate()->factory()->
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000395 NewDeoptimizationOutputData(length, TENURED);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000396 for (int i = 0; i < length; i++) {
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000397 data->SetAstId(i, bailout_entries_[i].id);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000398 data->SetPcAndState(i, Smi::FromInt(bailout_entries_[i].pc_and_state));
399 }
400 code->set_deoptimization_data(*data);
401}
402
403
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000404void FullCodeGenerator::PopulateTypeFeedbackInfo(Handle<Code> code) {
405 Handle<TypeFeedbackInfo> info = isolate()->factory()->NewTypeFeedbackInfo();
406 info->set_ic_total_count(ic_total_count_);
yangguo@chromium.orga7d3df92012-02-27 11:46:55 +0000407 ASSERT(!isolate()->heap()->InNewSpace(*info));
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000408 code->set_type_feedback_info(*info);
409}
410
411
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +0000412void FullCodeGenerator::Initialize() {
413 // The generation of debug code must match between the snapshot code and the
414 // code that is generated later. This is assumed by the debugger when it is
415 // calculating PC offsets after generating a debug version of code. Therefore
416 // we disable the production of debug code in the full compiler if we are
417 // either generating a snapshot or we booted from a snapshot.
418 generate_debug_code_ = FLAG_debug_code &&
419 !Serializer::enabled() &&
420 !Snapshot::HaveASnapshotToStartFrom();
421 masm_->set_emit_debug_code(generate_debug_code_);
422 masm_->set_predictable_code_size(true);
machenbach@chromium.org6d26cbb2014-01-22 10:50:56 +0000423 InitializeAstVisitor(info_->zone());
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +0000424}
425
426
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000427void FullCodeGenerator::PrepareForBailout(Expression* node, State state) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000428 PrepareForBailoutForId(node->id(), state);
429}
430
431
machenbach@chromium.orga03ba1e2014-02-01 08:54:43 +0000432void FullCodeGenerator::CallLoadIC(ContextualMode contextual_mode,
433 TypeFeedbackId id) {
434 ExtraICState extra_state = LoadIC::ComputeExtraICState(contextual_mode);
435 Handle<Code> ic = LoadIC::initialize_stub(isolate(), extra_state);
titzer@chromium.orgf5a24542014-03-04 09:06:17 +0000436 CallIC(ic, id);
ulan@chromium.org9cbaabd2014-01-08 10:55:36 +0000437}
438
439
titzer@chromium.orgf5a24542014-03-04 09:06:17 +0000440void FullCodeGenerator::CallStoreIC(TypeFeedbackId id) {
machenbach@chromium.org43c51e52014-01-20 07:57:28 +0000441 Handle<Code> ic = StoreIC::initialize_stub(isolate(), strict_mode());
titzer@chromium.orgf5a24542014-03-04 09:06:17 +0000442 CallIC(ic, id);
ulan@chromium.org9cbaabd2014-01-08 10:55:36 +0000443}
444
445
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000446void FullCodeGenerator::RecordJSReturnSite(Call* call) {
447 // We record the offset of the function return so we can rebuild the frame
448 // if the function was inlined, i.e., this is the return address in the
449 // inlined function's frame.
450 //
451 // The state is ignored. We defensively set it to TOS_REG, which is the
452 // real state of the unoptimized code at the return site.
453 PrepareForBailoutForId(call->ReturnId(), TOS_REG);
454#ifdef DEBUG
455 // In debug builds, mark the return so we can verify that this function
456 // was called.
457 ASSERT(!call->return_is_recorded_);
458 call->return_is_recorded_ = true;
459#endif
460}
461
462
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000463void FullCodeGenerator::PrepareForBailoutForId(BailoutId id, State state) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000464 // There's no need to prepare this code for bailouts from already optimized
465 // code or code that can't be optimized.
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000466 if (!info_->HasDeoptimizationSupport()) return;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000467 unsigned pc_and_state =
468 StateField::encode(state) | PcField::encode(masm_->pc_offset());
yangguo@chromium.org56454712012-02-16 15:33:53 +0000469 ASSERT(Smi::IsValid(pc_and_state));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000470 BailoutEntry entry = { id, pc_and_state };
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000471 ASSERT(!prepared_bailout_ids_.Contains(id.ToInt()));
472 prepared_bailout_ids_.Add(id.ToInt(), zone());
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000473 bailout_entries_.Add(entry, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000474}
475
476
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +0000477void FullCodeGenerator::RecordBackEdge(BailoutId ast_id) {
478 // The pc offset does not need to be encoded and packed together with a state.
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +0000479 ASSERT(masm_->pc_offset() > 0);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000480 ASSERT(loop_depth() > 0);
481 uint8_t depth = Min(loop_depth(), Code::kMaxLoopNestingMarker);
482 BackEdgeEntry entry =
483 { ast_id, static_cast<unsigned>(masm_->pc_offset()), depth };
484 back_edges_.Add(entry, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000485}
486
487
ricow@chromium.org65fae842010-08-25 15:26:24 +0000488bool FullCodeGenerator::ShouldInlineSmiCase(Token::Value op) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000489 // Inline smi case inside loops, but not division and modulo which
490 // are too complicated and take up too much space.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000491 if (op == Token::DIV ||op == Token::MOD) return false;
492 if (FLAG_always_inline_smi_code) return true;
493 return loop_depth_ > 0;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000494}
495
496
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000497void FullCodeGenerator::EffectContext::Plug(Register reg) const {
498}
499
500
501void FullCodeGenerator::AccumulatorValueContext::Plug(Register reg) const {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000502 __ Move(result_register(), reg);
503}
504
505
506void FullCodeGenerator::StackValueContext::Plug(Register reg) const {
danno@chromium.org59400602013-08-13 17:09:37 +0000507 __ Push(reg);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000508}
509
510
511void FullCodeGenerator::TestContext::Plug(Register reg) const {
512 // For simplicity we always test the accumulator register.
513 __ Move(result_register(), reg);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000514 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000515 codegen()->DoTest(this);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000516}
517
518
519void FullCodeGenerator::EffectContext::PlugTOS() const {
520 __ Drop(1);
521}
522
523
524void FullCodeGenerator::AccumulatorValueContext::PlugTOS() const {
danno@chromium.org59400602013-08-13 17:09:37 +0000525 __ Pop(result_register());
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000526}
527
528
529void FullCodeGenerator::StackValueContext::PlugTOS() const {
530}
531
532
533void FullCodeGenerator::TestContext::PlugTOS() const {
534 // For simplicity we always test the accumulator register.
danno@chromium.org59400602013-08-13 17:09:37 +0000535 __ Pop(result_register());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000536 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000537 codegen()->DoTest(this);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000538}
539
540
541void FullCodeGenerator::EffectContext::PrepareTest(
542 Label* materialize_true,
543 Label* materialize_false,
544 Label** if_true,
545 Label** if_false,
546 Label** fall_through) const {
547 // In an effect context, the true and the false case branch to the
548 // same label.
549 *if_true = *if_false = *fall_through = materialize_true;
550}
551
552
553void FullCodeGenerator::AccumulatorValueContext::PrepareTest(
554 Label* materialize_true,
555 Label* materialize_false,
556 Label** if_true,
557 Label** if_false,
558 Label** fall_through) const {
559 *if_true = *fall_through = materialize_true;
560 *if_false = materialize_false;
561}
562
563
564void FullCodeGenerator::StackValueContext::PrepareTest(
565 Label* materialize_true,
566 Label* materialize_false,
567 Label** if_true,
568 Label** if_false,
569 Label** fall_through) const {
570 *if_true = *fall_through = materialize_true;
571 *if_false = materialize_false;
572}
573
574
575void FullCodeGenerator::TestContext::PrepareTest(
576 Label* materialize_true,
577 Label* materialize_false,
578 Label** if_true,
579 Label** if_false,
580 Label** fall_through) const {
581 *if_true = true_label_;
582 *if_false = false_label_;
583 *fall_through = fall_through_;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000584}
585
586
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000587void FullCodeGenerator::DoTest(const TestContext* context) {
588 DoTest(context->condition(),
589 context->true_label(),
590 context->false_label(),
591 context->fall_through());
592}
593
594
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000595void FullCodeGenerator::AllocateModules(ZoneList<Declaration*>* declarations) {
596 ASSERT(scope_->is_global_scope());
597
598 for (int i = 0; i < declarations->length(); i++) {
599 ModuleDeclaration* declaration = declarations->at(i)->AsModuleDeclaration();
600 if (declaration != NULL) {
601 ModuleLiteral* module = declaration->module()->AsModuleLiteral();
602 if (module != NULL) {
603 Comment cmnt(masm_, "[ Link nested modules");
604 Scope* scope = module->body()->scope();
605 Interface* interface = scope->interface();
606 ASSERT(interface->IsModule() && interface->IsFrozen());
607
608 interface->Allocate(scope->module_var()->index());
609
610 // Set up module context.
611 ASSERT(scope->interface()->Index() >= 0);
612 __ Push(Smi::FromInt(scope->interface()->Index()));
613 __ Push(scope->GetScopeInfo());
614 __ CallRuntime(Runtime::kPushModuleContext, 2);
615 StoreToFrameField(StandardFrameConstants::kContextOffset,
616 context_register());
617
618 AllocateModules(scope->declarations());
619
620 // Pop module context.
621 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
622 // Update local stack frame context field.
623 StoreToFrameField(StandardFrameConstants::kContextOffset,
624 context_register());
625 }
626 }
627 }
628}
629
630
631// Modules have their own local scope, represented by their own context.
632// Module instance objects have an accessor for every export that forwards
633// access to the respective slot from the module's context. (Exports that are
634// modules themselves, however, are simple data properties.)
635//
636// All modules have a _hosting_ scope/context, which (currently) is the
637// (innermost) enclosing global scope. To deal with recursion, nested modules
638// are hosted by the same scope as global ones.
639//
640// For every (global or nested) module literal, the hosting context has an
641// internal slot that points directly to the respective module context. This
642// enables quick access to (statically resolved) module members by 2-dimensional
643// access through the hosting context. For example,
644//
645// module A {
646// let x;
647// module B { let y; }
648// }
649// module C { let z; }
650//
651// allocates contexts as follows:
652//
653// [header| .A | .B | .C | A | C ] (global)
654// | | |
655// | | +-- [header| z ] (module)
656// | |
657// | +------- [header| y ] (module)
658// |
659// +------------ [header| x | B ] (module)
660//
661// Here, .A, .B, .C are the internal slots pointing to the hosted module
662// contexts, whereas A, B, C hold the actual instance objects (note that every
663// module context also points to the respective instance object through its
664// extension slot in the header).
665//
666// To deal with arbitrary recursion and aliases between modules,
667// they are created and initialized in several stages. Each stage applies to
668// all modules in the hosting global scope, including nested ones.
669//
670// 1. Allocate: for each module _literal_, allocate the module contexts and
671// respective instance object and wire them up. This happens in the
672// PushModuleContext runtime function, as generated by AllocateModules
673// (invoked by VisitDeclarations in the hosting scope).
674//
675// 2. Bind: for each module _declaration_ (i.e. literals as well as aliases),
676// assign the respective instance object to respective local variables. This
677// happens in VisitModuleDeclaration, and uses the instance objects created
678// in the previous stage.
679// For each module _literal_, this phase also constructs a module descriptor
680// for the next stage. This happens in VisitModuleLiteral.
681//
682// 3. Populate: invoke the DeclareModules runtime function to populate each
683// _instance_ object with accessors for it exports. This is generated by
684// DeclareModules (invoked by VisitDeclarations in the hosting scope again),
685// and uses the descriptors generated in the previous stage.
686//
687// 4. Initialize: execute the module bodies (and other code) in sequence. This
688// happens by the separate statements generated for module bodies. To reenter
689// the module scopes properly, the parser inserted ModuleStatements.
690
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000691void FullCodeGenerator::VisitDeclarations(
692 ZoneList<Declaration*>* declarations) {
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000693 Handle<FixedArray> saved_modules = modules_;
694 int saved_module_index = module_index_;
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000695 ZoneList<Handle<Object> >* saved_globals = globals_;
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000696 ZoneList<Handle<Object> > inner_globals(10, zone());
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000697 globals_ = &inner_globals;
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000698
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000699 if (scope_->num_modules() != 0) {
700 // This is a scope hosting modules. Allocate a descriptor array to pass
701 // to the runtime for initialization.
702 Comment cmnt(masm_, "[ Allocate modules");
703 ASSERT(scope_->is_global_scope());
704 modules_ =
705 isolate()->factory()->NewFixedArray(scope_->num_modules(), TENURED);
706 module_index_ = 0;
707
708 // Generate code for allocating all modules, including nested ones.
709 // The allocated contexts are stored in internal variables in this scope.
710 AllocateModules(declarations);
711 }
712
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000713 AstVisitor::VisitDeclarations(declarations);
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000714
715 if (scope_->num_modules() != 0) {
716 // Initialize modules from descriptor array.
717 ASSERT(module_index_ == modules_->length());
718 DeclareModules(modules_);
719 modules_ = saved_modules;
720 module_index_ = saved_module_index;
721 }
722
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000723 if (!globals_->is_empty()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000724 // Invoke the platform-dependent code generator to do the actual
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000725 // declaration of the global functions and variables.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000726 Handle<FixedArray> array =
727 isolate()->factory()->NewFixedArray(globals_->length(), TENURED);
728 for (int i = 0; i < globals_->length(); ++i)
729 array->set(i, *globals_->at(i));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000730 DeclareGlobals(array);
731 }
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000732
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000733 globals_ = saved_globals;
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000734}
735
736
737void FullCodeGenerator::VisitModuleLiteral(ModuleLiteral* module) {
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000738 Block* block = module->body();
739 Scope* saved_scope = scope();
740 scope_ = block->scope();
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000741 Interface* interface = scope_->interface();
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000742
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000743 Comment cmnt(masm_, "[ ModuleLiteral");
744 SetStatementPosition(block);
745
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000746 ASSERT(!modules_.is_null());
747 ASSERT(module_index_ < modules_->length());
748 int index = module_index_++;
749
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000750 // Set up module context.
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000751 ASSERT(interface->Index() >= 0);
752 __ Push(Smi::FromInt(interface->Index()));
753 __ Push(Smi::FromInt(0));
754 __ CallRuntime(Runtime::kPushModuleContext, 2);
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000755 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000756
757 {
758 Comment cmnt(masm_, "[ Declarations");
759 VisitDeclarations(scope_->declarations());
760 }
761
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000762 // Populate the module description.
763 Handle<ModuleInfo> description =
764 ModuleInfo::Create(isolate(), interface, scope_);
765 modules_->set(index, *description);
766
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000767 scope_ = saved_scope;
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000768 // Pop module context.
769 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
770 // Update local stack frame context field.
771 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000772}
773
774
775void FullCodeGenerator::VisitModuleVariable(ModuleVariable* module) {
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000776 // Nothing to do.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000777 // The instance object is resolved statically through the module's interface.
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000778}
779
780
781void FullCodeGenerator::VisitModulePath(ModulePath* module) {
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000782 // Nothing to do.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000783 // The instance object is resolved statically through the module's interface.
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000784}
785
786
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000787void FullCodeGenerator::VisitModuleUrl(ModuleUrl* module) {
788 // TODO(rossberg): dummy allocation for now.
789 Scope* scope = module->body()->scope();
790 Interface* interface = scope_->interface();
791
792 ASSERT(interface->IsModule() && interface->IsFrozen());
793 ASSERT(!modules_.is_null());
794 ASSERT(module_index_ < modules_->length());
795 interface->Allocate(scope->module_var()->index());
796 int index = module_index_++;
797
798 Handle<ModuleInfo> description =
799 ModuleInfo::Create(isolate(), interface, scope_);
800 modules_->set(index, *description);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000801}
802
803
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000804int FullCodeGenerator::DeclareGlobalsFlags() {
dslomov@chromium.org486536d2014-03-12 13:09:18 +0000805 ASSERT(DeclareGlobalsStrictMode::is_valid(strict_mode()));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000806 return DeclareGlobalsEvalFlag::encode(is_eval()) |
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000807 DeclareGlobalsNativeFlag::encode(is_native()) |
dslomov@chromium.org486536d2014-03-12 13:09:18 +0000808 DeclareGlobalsStrictMode::encode(strict_mode());
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000809}
810
811
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000812void FullCodeGenerator::SetFunctionPosition(FunctionLiteral* fun) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000813 CodeGenerator::RecordPositions(masm_, fun->start_position());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000814}
815
816
817void FullCodeGenerator::SetReturnPosition(FunctionLiteral* fun) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000818 CodeGenerator::RecordPositions(masm_, fun->end_position() - 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000819}
820
821
822void FullCodeGenerator::SetStatementPosition(Statement* stmt) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000823#ifdef ENABLE_DEBUGGER_SUPPORT
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000824 if (!isolate()->debugger()->IsDebuggerActive()) {
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +0000825 CodeGenerator::RecordPositions(masm_, stmt->position());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000826 } else {
827 // Check if the statement will be breakable without adding a debug break
828 // slot.
machenbach@chromium.org6d26cbb2014-01-22 10:50:56 +0000829 BreakableStatementChecker checker(zone());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000830 checker.Check(stmt);
831 // Record the statement position right here if the statement is not
832 // breakable. For breakable statements the actual recording of the
833 // position will be postponed to the breakable code (typically an IC).
834 bool position_recorded = CodeGenerator::RecordPositions(
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +0000835 masm_, stmt->position(), !checker.is_breakable());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000836 // If the position recording did record a new position generate a debug
837 // break slot to make the statement breakable.
838 if (position_recorded) {
839 Debug::GenerateSlot(masm_);
840 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000841 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000842#else
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +0000843 CodeGenerator::RecordPositions(masm_, stmt->position());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000844#endif
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000845}
846
847
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +0000848void FullCodeGenerator::SetExpressionPosition(Expression* expr) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000849#ifdef ENABLE_DEBUGGER_SUPPORT
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000850 if (!isolate()->debugger()->IsDebuggerActive()) {
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +0000851 CodeGenerator::RecordPositions(masm_, expr->position());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000852 } else {
853 // Check if the expression will be breakable without adding a debug break
854 // slot.
machenbach@chromium.org6d26cbb2014-01-22 10:50:56 +0000855 BreakableStatementChecker checker(zone());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000856 checker.Check(expr);
857 // Record a statement position right here if the expression is not
858 // breakable. For breakable expressions the actual recording of the
859 // position will be postponed to the breakable code (typically an IC).
860 // NOTE this will record a statement position for something which might
861 // not be a statement. As stepping in the debugger will only stop at
862 // statement positions this is used for e.g. the condition expression of
863 // a do while loop.
864 bool position_recorded = CodeGenerator::RecordPositions(
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +0000865 masm_, expr->position(), !checker.is_breakable());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000866 // If the position recording did record a new position generate a debug
867 // break slot to make the statement breakable.
868 if (position_recorded) {
869 Debug::GenerateSlot(masm_);
870 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000871 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000872#else
873 CodeGenerator::RecordPositions(masm_, pos);
874#endif
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000875}
876
877
878void FullCodeGenerator::SetStatementPosition(int pos) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000879 CodeGenerator::RecordPositions(masm_, pos);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000880}
881
882
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000883void FullCodeGenerator::SetSourcePosition(int pos) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000884 if (pos != RelocInfo::kNoPosition) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000885 masm_->positions_recorder()->RecordPosition(pos);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000886 }
887}
888
889
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000890// Lookup table for code generators for special runtime calls which are
891// generated inline.
892#define INLINE_FUNCTION_GENERATOR_ADDRESS(Name, argc, ressize) \
893 &FullCodeGenerator::Emit##Name,
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000894
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000895const FullCodeGenerator::InlineFunctionGenerator
896 FullCodeGenerator::kInlineFunctionGenerators[] = {
897 INLINE_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS)
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000898 };
899#undef INLINE_FUNCTION_GENERATOR_ADDRESS
900
901
902FullCodeGenerator::InlineFunctionGenerator
903 FullCodeGenerator::FindInlineFunctionGenerator(Runtime::FunctionId id) {
whesse@chromium.org023421e2010-12-21 12:19:12 +0000904 int lookup_index =
905 static_cast<int>(id) - static_cast<int>(Runtime::kFirstInlineFunction);
906 ASSERT(lookup_index >= 0);
907 ASSERT(static_cast<size_t>(lookup_index) <
908 ARRAY_SIZE(kInlineFunctionGenerators));
909 return kInlineFunctionGenerators[lookup_index];
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000910}
911
912
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000913void FullCodeGenerator::EmitInlineRuntimeCall(CallRuntime* expr) {
914 const Runtime::Function* function = expr->function();
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000915 ASSERT(function != NULL);
916 ASSERT(function->intrinsic_type == Runtime::INLINE);
917 InlineFunctionGenerator generator =
918 FindInlineFunctionGenerator(function->function_id);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000919 ((*this).*(generator))(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000920}
921
922
verwaest@chromium.org8a00e822013-06-10 15:11:22 +0000923void FullCodeGenerator::EmitGeneratorNext(CallRuntime* expr) {
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000924 ZoneList<Expression*>* args = expr->arguments();
925 ASSERT(args->length() == 2);
verwaest@chromium.org8a00e822013-06-10 15:11:22 +0000926 EmitGeneratorResume(args->at(0), args->at(1), JSGeneratorObject::NEXT);
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000927}
928
929
930void FullCodeGenerator::EmitGeneratorThrow(CallRuntime* expr) {
931 ZoneList<Expression*>* args = expr->arguments();
932 ASSERT(args->length() == 2);
933 EmitGeneratorResume(args->at(0), args->at(1), JSGeneratorObject::THROW);
934}
935
936
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +0000937void FullCodeGenerator::EmitDebugBreakInOptimizedCode(CallRuntime* expr) {
938 context()->Plug(handle(Smi::FromInt(0), isolate()));
939}
940
941
machenbach@chromium.orgca2f2042014-03-10 10:03:12 +0000942void FullCodeGenerator::EmitDoubleHi(CallRuntime* expr) {
943 ZoneList<Expression*>* args = expr->arguments();
944 ASSERT(args->length() == 1);
945 VisitForStackValue(args->at(0));
946 masm()->CallRuntime(Runtime::kDoubleHi, 1);
947 context()->Plug(result_register());
948}
949
950
951void FullCodeGenerator::EmitDoubleLo(CallRuntime* expr) {
952 ZoneList<Expression*>* args = expr->arguments();
953 ASSERT(args->length() == 1);
954 VisitForStackValue(args->at(0));
955 masm()->CallRuntime(Runtime::kDoubleLo, 1);
956 context()->Plug(result_register());
957}
958
959
960void FullCodeGenerator::EmitConstructDouble(CallRuntime* expr) {
961 ZoneList<Expression*>* args = expr->arguments();
962 ASSERT(args->length() == 2);
963 VisitForStackValue(args->at(0));
964 VisitForStackValue(args->at(1));
965 masm()->CallRuntime(Runtime::kConstructDouble, 2);
966 context()->Plug(result_register());
967}
968
969
ricow@chromium.org65fae842010-08-25 15:26:24 +0000970void FullCodeGenerator::VisitBinaryOperation(BinaryOperation* expr) {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000971 switch (expr->op()) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000972 case Token::COMMA:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000973 return VisitComma(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000974 case Token::OR:
975 case Token::AND:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000976 return VisitLogicalExpression(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000977 default:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000978 return VisitArithmeticExpression(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000979 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000980}
981
982
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000983void FullCodeGenerator::VisitInDuplicateContext(Expression* expr) {
984 if (context()->IsEffect()) {
985 VisitForEffect(expr);
986 } else if (context()->IsAccumulatorValue()) {
987 VisitForAccumulatorValue(expr);
988 } else if (context()->IsStackValue()) {
989 VisitForStackValue(expr);
990 } else if (context()->IsTest()) {
991 const TestContext* test = TestContext::cast(context());
992 VisitForControl(expr, test->true_label(), test->false_label(),
993 test->fall_through());
994 }
995}
996
997
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000998void FullCodeGenerator::VisitComma(BinaryOperation* expr) {
999 Comment cmnt(masm_, "[ Comma");
1000 VisitForEffect(expr->left());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001001 VisitInDuplicateContext(expr->right());
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001002}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001003
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001004
1005void FullCodeGenerator::VisitLogicalExpression(BinaryOperation* expr) {
1006 bool is_logical_and = expr->op() == Token::AND;
1007 Comment cmnt(masm_, is_logical_and ? "[ Logical AND" : "[ Logical OR");
1008 Expression* left = expr->left();
1009 Expression* right = expr->right();
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001010 BailoutId right_id = expr->RightId();
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001011 Label done;
1012
1013 if (context()->IsTest()) {
1014 Label eval_right;
1015 const TestContext* test = TestContext::cast(context());
1016 if (is_logical_and) {
1017 VisitForControl(left, &eval_right, test->false_label(), &eval_right);
1018 } else {
1019 VisitForControl(left, test->true_label(), &eval_right, &eval_right);
1020 }
1021 PrepareForBailoutForId(right_id, NO_REGISTERS);
1022 __ bind(&eval_right);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001023
1024 } else if (context()->IsAccumulatorValue()) {
1025 VisitForAccumulatorValue(left);
1026 // We want the value in the accumulator for the test, and on the stack in
1027 // case we need it.
danno@chromium.org59400602013-08-13 17:09:37 +00001028 __ Push(result_register());
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001029 Label discard, restore;
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001030 if (is_logical_and) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001031 DoTest(left, &discard, &restore, &restore);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001032 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001033 DoTest(left, &restore, &discard, &restore);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001034 }
1035 __ bind(&restore);
danno@chromium.org59400602013-08-13 17:09:37 +00001036 __ Pop(result_register());
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001037 __ jmp(&done);
1038 __ bind(&discard);
1039 __ Drop(1);
1040 PrepareForBailoutForId(right_id, NO_REGISTERS);
1041
1042 } else if (context()->IsStackValue()) {
1043 VisitForAccumulatorValue(left);
1044 // We want the value in the accumulator for the test, and on the stack in
1045 // case we need it.
danno@chromium.org59400602013-08-13 17:09:37 +00001046 __ Push(result_register());
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001047 Label discard;
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001048 if (is_logical_and) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001049 DoTest(left, &discard, &done, &discard);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001050 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001051 DoTest(left, &done, &discard, &discard);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001052 }
1053 __ bind(&discard);
1054 __ Drop(1);
1055 PrepareForBailoutForId(right_id, NO_REGISTERS);
1056
1057 } else {
1058 ASSERT(context()->IsEffect());
1059 Label eval_right;
1060 if (is_logical_and) {
1061 VisitForControl(left, &eval_right, &done, &eval_right);
1062 } else {
1063 VisitForControl(left, &done, &eval_right, &eval_right);
1064 }
1065 PrepareForBailoutForId(right_id, NO_REGISTERS);
1066 __ bind(&eval_right);
1067 }
1068
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001069 VisitInDuplicateContext(right);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001070 __ bind(&done);
1071}
1072
1073
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001074void FullCodeGenerator::VisitArithmeticExpression(BinaryOperation* expr) {
1075 Token::Value op = expr->op();
1076 Comment cmnt(masm_, "[ ArithmeticExpression");
1077 Expression* left = expr->left();
1078 Expression* right = expr->right();
1079 OverwriteMode mode =
1080 left->ResultOverwriteAllowed()
1081 ? OVERWRITE_LEFT
1082 : (right->ResultOverwriteAllowed() ? OVERWRITE_RIGHT : NO_OVERWRITE);
1083
1084 VisitForStackValue(left);
1085 VisitForAccumulatorValue(right);
1086
1087 SetSourcePosition(expr->position());
1088 if (ShouldInlineSmiCase(op)) {
1089 EmitInlineSmiBinaryOp(expr, op, mode, left, right);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001090 } else {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001091 EmitBinaryOp(expr, op, mode);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001092 }
1093}
1094
1095
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001096void FullCodeGenerator::VisitBlock(Block* stmt) {
1097 Comment cmnt(masm_, "[ Block");
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001098 NestedBlock nested_block(this, stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001099 SetStatementPosition(stmt);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001100
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001101 Scope* saved_scope = scope();
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001102 // Push a block context when entering a block with block scoped variables.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00001103 if (stmt->scope() != NULL) {
danno@chromium.org81cac2b2012-07-10 11:28:27 +00001104 scope_ = stmt->scope();
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00001105 ASSERT(!scope_->is_module_scope());
1106 { Comment cmnt(masm_, "[ Extend block context");
machenbach@chromium.org05150ab2014-01-29 08:13:29 +00001107 __ Push(scope_->GetScopeInfo());
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00001108 PushFunctionArgumentForContextAllocation();
machenbach@chromium.org05150ab2014-01-29 08:13:29 +00001109 __ CallRuntime(Runtime::kPushBlockContext, 2);
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001110
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00001111 // Replace the context stored in the frame.
1112 StoreToFrameField(StandardFrameConstants::kContextOffset,
1113 context_register());
1114 }
1115 { Comment cmnt(masm_, "[ Declarations");
1116 VisitDeclarations(scope_->declarations());
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001117 }
1118 }
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00001119
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00001120 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001121 VisitStatements(stmt->statements());
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001122 scope_ = saved_scope;
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001123 __ bind(nested_block.break_label());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001124 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001125
1126 // Pop block context if necessary.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00001127 if (stmt->scope() != NULL) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001128 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1129 // Update local stack frame context field.
1130 StoreToFrameField(StandardFrameConstants::kContextOffset,
1131 context_register());
1132 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001133}
1134
1135
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00001136void FullCodeGenerator::VisitModuleStatement(ModuleStatement* stmt) {
1137 Comment cmnt(masm_, "[ Module context");
1138
1139 __ Push(Smi::FromInt(stmt->proxy()->interface()->Index()));
1140 __ Push(Smi::FromInt(0));
1141 __ CallRuntime(Runtime::kPushModuleContext, 2);
1142 StoreToFrameField(
1143 StandardFrameConstants::kContextOffset, context_register());
1144
1145 Scope* saved_scope = scope_;
1146 scope_ = stmt->body()->scope();
1147 VisitStatements(stmt->body()->statements());
1148 scope_ = saved_scope;
1149 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1150 // Update local stack frame context field.
1151 StoreToFrameField(StandardFrameConstants::kContextOffset,
1152 context_register());
1153}
1154
1155
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001156void FullCodeGenerator::VisitExpressionStatement(ExpressionStatement* stmt) {
1157 Comment cmnt(masm_, "[ ExpressionStatement");
1158 SetStatementPosition(stmt);
1159 VisitForEffect(stmt->expression());
1160}
1161
1162
1163void FullCodeGenerator::VisitEmptyStatement(EmptyStatement* stmt) {
1164 Comment cmnt(masm_, "[ EmptyStatement");
1165 SetStatementPosition(stmt);
1166}
1167
1168
1169void FullCodeGenerator::VisitIfStatement(IfStatement* stmt) {
1170 Comment cmnt(masm_, "[ IfStatement");
1171 SetStatementPosition(stmt);
1172 Label then_part, else_part, done;
1173
ricow@chromium.org65fae842010-08-25 15:26:24 +00001174 if (stmt->HasElseStatement()) {
1175 VisitForControl(stmt->condition(), &then_part, &else_part, &then_part);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001176 PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001177 __ bind(&then_part);
1178 Visit(stmt->then_statement());
1179 __ jmp(&done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001180
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001181 PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001182 __ bind(&else_part);
1183 Visit(stmt->else_statement());
1184 } else {
1185 VisitForControl(stmt->condition(), &then_part, &done, &then_part);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001186 PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001187 __ bind(&then_part);
1188 Visit(stmt->then_statement());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001189
1190 PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001191 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001192 __ bind(&done);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001193 PrepareForBailoutForId(stmt->IfId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001194}
1195
1196
1197void FullCodeGenerator::VisitContinueStatement(ContinueStatement* stmt) {
1198 Comment cmnt(masm_, "[ ContinueStatement");
1199 SetStatementPosition(stmt);
1200 NestedStatement* current = nesting_stack_;
1201 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001202 int context_length = 0;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001203 // When continuing, we clobber the unpredictable value in the accumulator
1204 // with one that's safe for GC. If we hit an exit from the try block of
1205 // try...finally on our way out, we will unconditionally preserve the
1206 // accumulator on the stack.
1207 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001208 while (!current->IsContinueTarget(stmt->target())) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001209 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001210 }
1211 __ Drop(stack_depth);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001212 if (context_length > 0) {
1213 while (context_length > 0) {
1214 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1215 --context_length;
1216 }
1217 StoreToFrameField(StandardFrameConstants::kContextOffset,
1218 context_register());
1219 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001220
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001221 __ jmp(current->AsIteration()->continue_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001222}
1223
1224
1225void FullCodeGenerator::VisitBreakStatement(BreakStatement* stmt) {
1226 Comment cmnt(masm_, "[ BreakStatement");
1227 SetStatementPosition(stmt);
1228 NestedStatement* current = nesting_stack_;
1229 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001230 int context_length = 0;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001231 // When breaking, we clobber the unpredictable value in the accumulator
1232 // with one that's safe for GC. If we hit an exit from the try block of
1233 // try...finally on our way out, we will unconditionally preserve the
1234 // accumulator on the stack.
1235 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001236 while (!current->IsBreakTarget(stmt->target())) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001237 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001238 }
1239 __ Drop(stack_depth);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001240 if (context_length > 0) {
1241 while (context_length > 0) {
1242 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1243 --context_length;
1244 }
1245 StoreToFrameField(StandardFrameConstants::kContextOffset,
1246 context_register());
1247 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001248
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001249 __ jmp(current->AsBreakable()->break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001250}
1251
1252
danno@chromium.org41728482013-06-12 22:31:22 +00001253void FullCodeGenerator::EmitUnwindBeforeReturn() {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001254 NestedStatement* current = nesting_stack_;
1255 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001256 int context_length = 0;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001257 while (current != NULL) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001258 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001259 }
1260 __ Drop(stack_depth);
danno@chromium.org41728482013-06-12 22:31:22 +00001261}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001262
danno@chromium.org41728482013-06-12 22:31:22 +00001263
1264void FullCodeGenerator::VisitReturnStatement(ReturnStatement* stmt) {
1265 Comment cmnt(masm_, "[ ReturnStatement");
1266 SetStatementPosition(stmt);
1267 Expression* expr = stmt->expression();
1268 VisitForAccumulatorValue(expr);
1269 EmitUnwindBeforeReturn();
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001270 EmitReturnSequence();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001271}
1272
1273
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001274void FullCodeGenerator::VisitWithStatement(WithStatement* stmt) {
1275 Comment cmnt(masm_, "[ WithStatement");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001276 SetStatementPosition(stmt);
1277
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001278 VisitForStackValue(stmt->expression());
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00001279 PushFunctionArgumentForContextAllocation();
1280 __ CallRuntime(Runtime::kPushWithContext, 2);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001281 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001282
danno@chromium.orgca29dd82013-04-26 11:59:48 +00001283 Scope* saved_scope = scope();
1284 scope_ = stmt->scope();
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001285 { WithOrCatch body(this);
1286 Visit(stmt->statement());
1287 }
danno@chromium.orgca29dd82013-04-26 11:59:48 +00001288 scope_ = saved_scope;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001289
1290 // Pop context.
1291 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1292 // Update local stack frame context field.
1293 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001294}
1295
1296
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001297void FullCodeGenerator::VisitDoWhileStatement(DoWhileStatement* stmt) {
1298 Comment cmnt(masm_, "[ DoWhileStatement");
1299 SetStatementPosition(stmt);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001300 Label body, book_keeping;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001301
1302 Iteration loop_statement(this, stmt);
1303 increment_loop_depth();
1304
1305 __ bind(&body);
1306 Visit(stmt->body());
1307
ricow@chromium.org65fae842010-08-25 15:26:24 +00001308 // Record the position of the do while condition and make sure it is
1309 // possible to break on the condition.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001310 __ bind(loop_statement.continue_label());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001311 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +00001312 SetExpressionPosition(stmt->cond());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001313 VisitForControl(stmt->cond(),
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001314 &book_keeping,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001315 loop_statement.break_label(),
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001316 &book_keeping);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001317
1318 // Check stack before looping.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001319 PrepareForBailoutForId(stmt->BackEdgeId(), NO_REGISTERS);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001320 __ bind(&book_keeping);
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +00001321 EmitBackEdgeBookkeeping(stmt, &body);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001322 __ jmp(&body);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001323
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001324 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001325 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001326 decrement_loop_depth();
1327}
1328
1329
1330void FullCodeGenerator::VisitWhileStatement(WhileStatement* stmt) {
1331 Comment cmnt(masm_, "[ WhileStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001332 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001333
1334 Iteration loop_statement(this, stmt);
1335 increment_loop_depth();
1336
1337 // Emit the test at the bottom of the loop.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001338 __ jmp(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001339
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001340 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001341 __ bind(&body);
1342 Visit(stmt->body());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001343
1344 // Emit the statement position here as this is where the while
1345 // statement code starts.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001346 __ bind(loop_statement.continue_label());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001347 SetStatementPosition(stmt);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001348
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001349 // 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);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001353 VisitForControl(stmt->cond(),
1354 &body,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001355 loop_statement.break_label(),
1356 loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001357
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001358 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001359 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001360 decrement_loop_depth();
1361}
1362
1363
1364void FullCodeGenerator::VisitForStatement(ForStatement* stmt) {
1365 Comment cmnt(masm_, "[ ForStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001366 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001367
1368 Iteration loop_statement(this, stmt);
erik.corry@gmail.combbceb572012-03-09 10:52:05 +00001369
1370 // Set statement position for a break slot before entering the for-body.
1371 SetStatementPosition(stmt);
1372
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001373 if (stmt->init() != NULL) {
1374 Visit(stmt->init());
1375 }
1376
1377 increment_loop_depth();
1378 // Emit the test at the bottom of the loop (even if empty).
1379 __ jmp(&test);
1380
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001381 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001382 __ bind(&body);
1383 Visit(stmt->body());
1384
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001385 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001386 __ bind(loop_statement.continue_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001387 if (stmt->next() != NULL) {
1388 Visit(stmt->next());
1389 }
1390
ricow@chromium.org65fae842010-08-25 15:26:24 +00001391 // Emit the statement position here as this is where the for
1392 // statement code starts.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001393 SetStatementPosition(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001394
1395 // Check stack before looping.
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +00001396 EmitBackEdgeBookkeeping(stmt, &body);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001397
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001398 __ bind(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001399 if (stmt->cond() != NULL) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00001400 VisitForControl(stmt->cond(),
1401 &body,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001402 loop_statement.break_label(),
1403 loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001404 } else {
1405 __ jmp(&body);
1406 }
1407
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001408 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001409 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001410 decrement_loop_depth();
1411}
1412
1413
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001414void FullCodeGenerator::VisitTryCatchStatement(TryCatchStatement* stmt) {
1415 Comment cmnt(masm_, "[ TryCatchStatement");
1416 SetStatementPosition(stmt);
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001417 // The try block adds a handler to the exception handler chain before
1418 // entering, and removes it again when exiting normally. If an exception
1419 // is thrown during execution of the try block, the handler is consumed
1420 // and control is passed to the catch block with the exception in the
1421 // result register.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001422
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001423 Label try_entry, handler_entry, exit;
1424 __ jmp(&try_entry);
1425 __ bind(&handler_entry);
1426 handler_table()->set(stmt->index(), Smi::FromInt(handler_entry.pos()));
1427 // Exception handler code, the exception is in the result register.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001428 // Extend the context before executing the catch block.
1429 { Comment cmnt(masm_, "[ Extend catch context");
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001430 __ Push(stmt->variable()->name());
danno@chromium.org59400602013-08-13 17:09:37 +00001431 __ Push(result_register());
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00001432 PushFunctionArgumentForContextAllocation();
1433 __ CallRuntime(Runtime::kPushCatchContext, 3);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001434 StoreToFrameField(StandardFrameConstants::kContextOffset,
1435 context_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001436 }
1437
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001438 Scope* saved_scope = scope();
1439 scope_ = stmt->scope();
1440 ASSERT(scope_->declarations()->is_empty());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001441 { WithOrCatch catch_body(this);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001442 Visit(stmt->catch_block());
1443 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001444 // Restore the context.
1445 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1446 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001447 scope_ = saved_scope;
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001448 __ jmp(&exit);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001449
1450 // Try block code. Sets up the exception handler chain.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001451 __ bind(&try_entry);
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00001452 __ PushTryHandler(StackHandler::CATCH, stmt->index());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001453 { TryCatch try_body(this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001454 Visit(stmt->try_block());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001455 }
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001456 __ PopTryHandler();
1457 __ bind(&exit);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001458}
1459
1460
1461void FullCodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* stmt) {
1462 Comment cmnt(masm_, "[ TryFinallyStatement");
1463 SetStatementPosition(stmt);
1464 // Try finally is compiled by setting up a try-handler on the stack while
1465 // executing the try body, and removing it again afterwards.
1466 //
1467 // The try-finally construct can enter the finally block in three ways:
1468 // 1. By exiting the try-block normally. This removes the try-handler and
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001469 // calls the finally block code before continuing.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001470 // 2. By exiting the try-block with a function-local control flow transfer
1471 // (break/continue/return). The site of the, e.g., break removes the
1472 // try handler and calls the finally block code before continuing
1473 // its outward control transfer.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001474 // 3. By exiting the try-block with a thrown exception.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001475 // This can happen in nested function calls. It traverses the try-handler
1476 // chain and consumes the try-handler entry before jumping to the
1477 // handler code. The handler code then calls the finally-block before
1478 // rethrowing the exception.
1479 //
1480 // The finally block must assume a return address on top of the stack
1481 // (or in the link register on ARM chips) and a value (return value or
1482 // exception) in the result register (rax/eax/r0), both of which must
1483 // be preserved. The return address isn't GC-safe, so it should be
1484 // cooked before GC.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001485 Label try_entry, handler_entry, finally_entry;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001486
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001487 // Jump to try-handler setup and try-block code.
1488 __ jmp(&try_entry);
1489 __ bind(&handler_entry);
1490 handler_table()->set(stmt->index(), Smi::FromInt(handler_entry.pos()));
1491 // Exception handler code. This code is only executed when an exception
1492 // is thrown. The exception is in the result register, and must be
1493 // preserved by the finally block. Call the finally block and then
1494 // rethrow the exception if it returns.
1495 __ Call(&finally_entry);
danno@chromium.org59400602013-08-13 17:09:37 +00001496 __ Push(result_register());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001497 __ CallRuntime(Runtime::kReThrow, 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001498
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001499 // Finally block implementation.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001500 __ bind(&finally_entry);
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001501 EnterFinallyBlock();
1502 { Finally finally_body(this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001503 Visit(stmt->finally_block());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001504 }
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001505 ExitFinallyBlock(); // Return to the calling code.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001506
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001507 // Set up try handler.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001508 __ bind(&try_entry);
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00001509 __ PushTryHandler(StackHandler::FINALLY, stmt->index());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001510 { TryFinally try_body(this, &finally_entry);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001511 Visit(stmt->try_block());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001512 }
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001513 __ PopTryHandler();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001514 // Execute the finally block on the way out. Clobber the unpredictable
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001515 // value in the result register with one that's safe for GC because the
1516 // finally block will unconditionally preserve the result register on the
1517 // stack.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001518 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001519 __ Call(&finally_entry);
1520}
1521
1522
1523void FullCodeGenerator::VisitDebuggerStatement(DebuggerStatement* stmt) {
1524#ifdef ENABLE_DEBUGGER_SUPPORT
1525 Comment cmnt(masm_, "[ DebuggerStatement");
1526 SetStatementPosition(stmt);
1527
ager@chromium.org5c838252010-02-19 08:53:10 +00001528 __ DebugBreak();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001529 // Ignore the return value.
1530#endif
1531}
1532
1533
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +00001534void FullCodeGenerator::VisitCaseClause(CaseClause* clause) {
1535 UNREACHABLE();
1536}
1537
1538
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001539void FullCodeGenerator::VisitConditional(Conditional* expr) {
1540 Comment cmnt(masm_, "[ Conditional");
1541 Label true_case, false_case, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001542 VisitForControl(expr->condition(), &true_case, &false_case, &true_case);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001543
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001544 PrepareForBailoutForId(expr->ThenId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001545 __ bind(&true_case);
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +00001546 SetExpressionPosition(expr->then_expression());
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001547 if (context()->IsTest()) {
1548 const TestContext* for_test = TestContext::cast(context());
1549 VisitForControl(expr->then_expression(),
1550 for_test->true_label(),
1551 for_test->false_label(),
1552 NULL);
1553 } else {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001554 VisitInDuplicateContext(expr->then_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001555 __ jmp(&done);
1556 }
1557
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001558 PrepareForBailoutForId(expr->ElseId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001559 __ bind(&false_case);
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +00001560 SetExpressionPosition(expr->else_expression());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001561 VisitInDuplicateContext(expr->else_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001562 // If control flow falls through Visit, merge it with true case here.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001563 if (!context()->IsTest()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001564 __ bind(&done);
1565 }
1566}
1567
1568
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001569void FullCodeGenerator::VisitLiteral(Literal* expr) {
1570 Comment cmnt(masm_, "[ Literal");
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00001571 context()->Plug(expr->value());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001572}
1573
1574
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001575void FullCodeGenerator::VisitFunctionLiteral(FunctionLiteral* expr) {
1576 Comment cmnt(masm_, "[ FunctionLiteral");
1577
1578 // Build the function boilerplate and instantiate it.
1579 Handle<SharedFunctionInfo> function_info =
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001580 Compiler::BuildFunctionInfo(expr, script());
1581 if (function_info.is_null()) {
1582 SetStackOverflow();
1583 return;
1584 }
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001585 EmitNewClosure(function_info, expr->pretenure());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001586}
1587
1588
jkummerow@chromium.orgfb7a7c42013-10-02 11:41:02 +00001589void FullCodeGenerator::VisitNativeFunctionLiteral(
1590 NativeFunctionLiteral* expr) {
1591 Comment cmnt(masm_, "[ NativeFunctionLiteral");
1592
1593 // Compute the function template for the native function.
1594 Handle<String> name = expr->name();
1595 v8::Handle<v8::FunctionTemplate> fun_template =
machenbach@chromium.org37be4082013-11-26 13:50:38 +00001596 expr->extension()->GetNativeFunctionTemplate(
1597 reinterpret_cast<v8::Isolate*>(isolate()), v8::Utils::ToLocal(name));
jkummerow@chromium.orgfb7a7c42013-10-02 11:41:02 +00001598 ASSERT(!fun_template.IsEmpty());
1599
1600 // Instantiate the function and create a shared function info from it.
1601 Handle<JSFunction> fun = Utils::OpenHandle(*fun_template->GetFunction());
1602 const int literals = fun->NumberOfLiterals();
1603 Handle<Code> code = Handle<Code>(fun->shared()->code());
1604 Handle<Code> construct_stub = Handle<Code>(fun->shared()->construct_stub());
1605 bool is_generator = false;
1606 Handle<SharedFunctionInfo> shared =
1607 isolate()->factory()->NewSharedFunctionInfo(name, literals, is_generator,
machenbach@chromium.orgca2f2042014-03-10 10:03:12 +00001608 code, Handle<ScopeInfo>(fun->shared()->scope_info()),
1609 Handle<FixedArray>(fun->shared()->feedback_vector()));
jkummerow@chromium.orgfb7a7c42013-10-02 11:41:02 +00001610 shared->set_construct_stub(*construct_stub);
1611
1612 // Copy the function data to the shared function info.
1613 shared->set_function_data(fun->shared()->function_data());
1614 int parameters = fun->shared()->formal_parameter_count();
1615 shared->set_formal_parameter_count(parameters);
1616
1617 EmitNewClosure(shared, false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001618}
1619
1620
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001621void FullCodeGenerator::VisitThrow(Throw* expr) {
1622 Comment cmnt(masm_, "[ Throw");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001623 VisitForStackValue(expr->exception());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001624 __ CallRuntime(Runtime::kThrow, 1);
1625 // Never returns here.
1626}
1627
1628
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001629FullCodeGenerator::NestedStatement* FullCodeGenerator::TryCatch::Exit(
1630 int* stack_depth,
1631 int* context_length) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001632 // The macros used here must preserve the result register.
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001633 __ Drop(*stack_depth);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001634 __ PopTryHandler();
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001635 *stack_depth = 0;
1636 return previous_;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001637}
1638
ricow@chromium.org65fae842010-08-25 15:26:24 +00001639
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001640bool FullCodeGenerator::TryLiteralCompare(CompareOperation* expr) {
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001641 Expression* sub_expr;
ager@chromium.org04921a82011-06-27 13:21:41 +00001642 Handle<String> check;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001643 if (expr->IsLiteralCompareTypeof(&sub_expr, &check)) {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001644 EmitLiteralCompareTypeof(expr, sub_expr, check);
ager@chromium.org04921a82011-06-27 13:21:41 +00001645 return true;
1646 }
1647
jkummerow@chromium.org96a3c512013-07-18 17:02:47 +00001648 if (expr->IsLiteralCompareUndefined(&sub_expr, isolate())) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001649 EmitLiteralCompareNil(expr, sub_expr, kUndefinedValue);
1650 return true;
1651 }
1652
1653 if (expr->IsLiteralCompareNull(&sub_expr)) {
1654 EmitLiteralCompareNil(expr, sub_expr, kNullValue);
ager@chromium.org04921a82011-06-27 13:21:41 +00001655 return true;
1656 }
1657
1658 return false;
1659}
1660
1661
yangguo@chromium.org49546742013-12-23 16:17:49 +00001662void BackEdgeTable::Patch(Isolate* isolate, Code* unoptimized) {
machenbach@chromium.org528ce022013-09-23 14:09:36 +00001663 DisallowHeapAllocation no_gc;
machenbach@chromium.org8e36b5b2013-09-26 07:36:30 +00001664 Code* patch = isolate->builtins()->builtin(Builtins::kOnStackReplacement);
machenbach@chromium.org528ce022013-09-23 14:09:36 +00001665
1666 // Iterate over the back edge table and patch every interrupt
1667 // call to an unconditional call to the replacement code.
1668 int loop_nesting_level = unoptimized->allow_osr_at_loop_nesting_level();
1669
1670 BackEdgeTable back_edges(unoptimized, &no_gc);
1671 for (uint32_t i = 0; i < back_edges.length(); i++) {
1672 if (static_cast<int>(back_edges.loop_depth(i)) == loop_nesting_level) {
1673 ASSERT_EQ(INTERRUPT, GetBackEdgeState(isolate,
1674 unoptimized,
1675 back_edges.pc(i)));
machenbach@chromium.org8e36b5b2013-09-26 07:36:30 +00001676 PatchAt(unoptimized, back_edges.pc(i), ON_STACK_REPLACEMENT, patch);
machenbach@chromium.org528ce022013-09-23 14:09:36 +00001677 }
1678 }
1679
1680 unoptimized->set_back_edges_patched_for_osr(true);
1681 ASSERT(Verify(isolate, unoptimized, loop_nesting_level));
1682}
1683
1684
yangguo@chromium.org49546742013-12-23 16:17:49 +00001685void BackEdgeTable::Revert(Isolate* isolate, Code* unoptimized) {
machenbach@chromium.org528ce022013-09-23 14:09:36 +00001686 DisallowHeapAllocation no_gc;
machenbach@chromium.org8e36b5b2013-09-26 07:36:30 +00001687 Code* patch = isolate->builtins()->builtin(Builtins::kInterruptCheck);
machenbach@chromium.org528ce022013-09-23 14:09:36 +00001688
1689 // Iterate over the back edge table and revert the patched interrupt calls.
1690 ASSERT(unoptimized->back_edges_patched_for_osr());
1691 int loop_nesting_level = unoptimized->allow_osr_at_loop_nesting_level();
1692
1693 BackEdgeTable back_edges(unoptimized, &no_gc);
1694 for (uint32_t i = 0; i < back_edges.length(); i++) {
1695 if (static_cast<int>(back_edges.loop_depth(i)) <= loop_nesting_level) {
machenbach@chromium.org8e36b5b2013-09-26 07:36:30 +00001696 ASSERT_NE(INTERRUPT, GetBackEdgeState(isolate,
1697 unoptimized,
1698 back_edges.pc(i)));
1699 PatchAt(unoptimized, back_edges.pc(i), INTERRUPT, patch);
machenbach@chromium.org528ce022013-09-23 14:09:36 +00001700 }
1701 }
1702
1703 unoptimized->set_back_edges_patched_for_osr(false);
1704 unoptimized->set_allow_osr_at_loop_nesting_level(0);
1705 // Assert that none of the back edges are patched anymore.
1706 ASSERT(Verify(isolate, unoptimized, -1));
1707}
1708
1709
yangguo@chromium.org49546742013-12-23 16:17:49 +00001710void BackEdgeTable::AddStackCheck(Handle<Code> code, uint32_t pc_offset) {
machenbach@chromium.org8e36b5b2013-09-26 07:36:30 +00001711 DisallowHeapAllocation no_gc;
yangguo@chromium.org49546742013-12-23 16:17:49 +00001712 Isolate* isolate = code->GetIsolate();
1713 Address pc = code->instruction_start() + pc_offset;
machenbach@chromium.org8e36b5b2013-09-26 07:36:30 +00001714 Code* patch = isolate->builtins()->builtin(Builtins::kOsrAfterStackCheck);
yangguo@chromium.org49546742013-12-23 16:17:49 +00001715 PatchAt(*code, pc, OSR_AFTER_STACK_CHECK, patch);
machenbach@chromium.org8e36b5b2013-09-26 07:36:30 +00001716}
1717
1718
yangguo@chromium.org49546742013-12-23 16:17:49 +00001719void BackEdgeTable::RemoveStackCheck(Handle<Code> code, uint32_t pc_offset) {
machenbach@chromium.org8e36b5b2013-09-26 07:36:30 +00001720 DisallowHeapAllocation no_gc;
yangguo@chromium.org49546742013-12-23 16:17:49 +00001721 Isolate* isolate = code->GetIsolate();
1722 Address pc = code->instruction_start() + pc_offset;
1723
1724 if (OSR_AFTER_STACK_CHECK == GetBackEdgeState(isolate, *code, pc)) {
machenbach@chromium.org8e36b5b2013-09-26 07:36:30 +00001725 Code* patch = isolate->builtins()->builtin(Builtins::kOnStackReplacement);
yangguo@chromium.org49546742013-12-23 16:17:49 +00001726 PatchAt(*code, pc, ON_STACK_REPLACEMENT, patch);
machenbach@chromium.org8e36b5b2013-09-26 07:36:30 +00001727 }
1728}
1729
1730
machenbach@chromium.org528ce022013-09-23 14:09:36 +00001731#ifdef DEBUG
1732bool BackEdgeTable::Verify(Isolate* isolate,
1733 Code* unoptimized,
1734 int loop_nesting_level) {
1735 DisallowHeapAllocation no_gc;
1736 BackEdgeTable back_edges(unoptimized, &no_gc);
1737 for (uint32_t i = 0; i < back_edges.length(); i++) {
1738 uint32_t loop_depth = back_edges.loop_depth(i);
1739 CHECK_LE(static_cast<int>(loop_depth), Code::kMaxLoopNestingMarker);
1740 // Assert that all back edges for shallower loops (and only those)
1741 // have already been patched.
1742 CHECK_EQ((static_cast<int>(loop_depth) <= loop_nesting_level),
1743 GetBackEdgeState(isolate,
1744 unoptimized,
1745 back_edges.pc(i)) != INTERRUPT);
1746 }
1747 return true;
1748}
1749#endif // DEBUG
1750
1751
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001752#undef __
1753
1754
1755} } // namespace v8::internal