blob: 2170b8421275710e3c4329e4ed5723b608d058f6 [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
machenbach@chromium.org8545d492014-03-17 09:28:03 +0000389void FullCodeGenerator::InitializeFeedbackVector() {
390 int length = info_->function()->slot_count();
391 feedback_vector_ = isolate()->factory()->NewFixedArray(length, TENURED);
392 Handle<Object> sentinel = TypeFeedbackInfo::UninitializedSentinel(isolate());
393 // Ensure that it's safe to set without using a write barrier.
394 ASSERT_EQ(isolate()->heap()->uninitialized_symbol(), *sentinel);
395 for (int i = 0; i < length; i++) {
396 feedback_vector_->set(i, *sentinel, SKIP_WRITE_BARRIER);
397 }
398}
399
400
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000401void FullCodeGenerator::PopulateDeoptimizationData(Handle<Code> code) {
402 // Fill in the deoptimization information.
403 ASSERT(info_->HasDeoptimizationSupport() || bailout_entries_.is_empty());
404 if (!info_->HasDeoptimizationSupport()) return;
405 int length = bailout_entries_.length();
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000406 Handle<DeoptimizationOutputData> data = isolate()->factory()->
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000407 NewDeoptimizationOutputData(length, TENURED);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000408 for (int i = 0; i < length; i++) {
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000409 data->SetAstId(i, bailout_entries_[i].id);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000410 data->SetPcAndState(i, Smi::FromInt(bailout_entries_[i].pc_and_state));
411 }
412 code->set_deoptimization_data(*data);
413}
414
415
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000416void FullCodeGenerator::PopulateTypeFeedbackInfo(Handle<Code> code) {
417 Handle<TypeFeedbackInfo> info = isolate()->factory()->NewTypeFeedbackInfo();
418 info->set_ic_total_count(ic_total_count_);
machenbach@chromium.org8545d492014-03-17 09:28:03 +0000419 info->set_feedback_vector(*FeedbackVector());
yangguo@chromium.orga7d3df92012-02-27 11:46:55 +0000420 ASSERT(!isolate()->heap()->InNewSpace(*info));
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000421 code->set_type_feedback_info(*info);
422}
423
424
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +0000425void FullCodeGenerator::Initialize() {
426 // The generation of debug code must match between the snapshot code and the
427 // code that is generated later. This is assumed by the debugger when it is
428 // calculating PC offsets after generating a debug version of code. Therefore
429 // we disable the production of debug code in the full compiler if we are
430 // either generating a snapshot or we booted from a snapshot.
431 generate_debug_code_ = FLAG_debug_code &&
432 !Serializer::enabled() &&
433 !Snapshot::HaveASnapshotToStartFrom();
434 masm_->set_emit_debug_code(generate_debug_code_);
435 masm_->set_predictable_code_size(true);
machenbach@chromium.org6d26cbb2014-01-22 10:50:56 +0000436 InitializeAstVisitor(info_->zone());
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +0000437}
438
439
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000440void FullCodeGenerator::PrepareForBailout(Expression* node, State state) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000441 PrepareForBailoutForId(node->id(), state);
442}
443
444
machenbach@chromium.orga03ba1e2014-02-01 08:54:43 +0000445void FullCodeGenerator::CallLoadIC(ContextualMode contextual_mode,
446 TypeFeedbackId id) {
447 ExtraICState extra_state = LoadIC::ComputeExtraICState(contextual_mode);
448 Handle<Code> ic = LoadIC::initialize_stub(isolate(), extra_state);
titzer@chromium.orgf5a24542014-03-04 09:06:17 +0000449 CallIC(ic, id);
ulan@chromium.org9cbaabd2014-01-08 10:55:36 +0000450}
451
452
titzer@chromium.orgf5a24542014-03-04 09:06:17 +0000453void FullCodeGenerator::CallStoreIC(TypeFeedbackId id) {
machenbach@chromium.org43c51e52014-01-20 07:57:28 +0000454 Handle<Code> ic = StoreIC::initialize_stub(isolate(), strict_mode());
titzer@chromium.orgf5a24542014-03-04 09:06:17 +0000455 CallIC(ic, id);
ulan@chromium.org9cbaabd2014-01-08 10:55:36 +0000456}
457
458
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000459void FullCodeGenerator::RecordJSReturnSite(Call* call) {
460 // We record the offset of the function return so we can rebuild the frame
461 // if the function was inlined, i.e., this is the return address in the
462 // inlined function's frame.
463 //
464 // The state is ignored. We defensively set it to TOS_REG, which is the
465 // real state of the unoptimized code at the return site.
466 PrepareForBailoutForId(call->ReturnId(), TOS_REG);
467#ifdef DEBUG
468 // In debug builds, mark the return so we can verify that this function
469 // was called.
470 ASSERT(!call->return_is_recorded_);
471 call->return_is_recorded_ = true;
472#endif
473}
474
475
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000476void FullCodeGenerator::PrepareForBailoutForId(BailoutId id, State state) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000477 // There's no need to prepare this code for bailouts from already optimized
478 // code or code that can't be optimized.
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000479 if (!info_->HasDeoptimizationSupport()) return;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000480 unsigned pc_and_state =
481 StateField::encode(state) | PcField::encode(masm_->pc_offset());
yangguo@chromium.org56454712012-02-16 15:33:53 +0000482 ASSERT(Smi::IsValid(pc_and_state));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000483 BailoutEntry entry = { id, pc_and_state };
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000484 ASSERT(!prepared_bailout_ids_.Contains(id.ToInt()));
485 prepared_bailout_ids_.Add(id.ToInt(), zone());
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000486 bailout_entries_.Add(entry, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000487}
488
489
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +0000490void FullCodeGenerator::RecordBackEdge(BailoutId ast_id) {
491 // The pc offset does not need to be encoded and packed together with a state.
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +0000492 ASSERT(masm_->pc_offset() > 0);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000493 ASSERT(loop_depth() > 0);
494 uint8_t depth = Min(loop_depth(), Code::kMaxLoopNestingMarker);
495 BackEdgeEntry entry =
496 { ast_id, static_cast<unsigned>(masm_->pc_offset()), depth };
497 back_edges_.Add(entry, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000498}
499
500
ricow@chromium.org65fae842010-08-25 15:26:24 +0000501bool FullCodeGenerator::ShouldInlineSmiCase(Token::Value op) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000502 // Inline smi case inside loops, but not division and modulo which
503 // are too complicated and take up too much space.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000504 if (op == Token::DIV ||op == Token::MOD) return false;
505 if (FLAG_always_inline_smi_code) return true;
506 return loop_depth_ > 0;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000507}
508
509
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000510void FullCodeGenerator::EffectContext::Plug(Register reg) const {
511}
512
513
514void FullCodeGenerator::AccumulatorValueContext::Plug(Register reg) const {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000515 __ Move(result_register(), reg);
516}
517
518
519void FullCodeGenerator::StackValueContext::Plug(Register reg) const {
danno@chromium.org59400602013-08-13 17:09:37 +0000520 __ Push(reg);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000521}
522
523
524void FullCodeGenerator::TestContext::Plug(Register reg) const {
525 // For simplicity we always test the accumulator register.
526 __ Move(result_register(), reg);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000527 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000528 codegen()->DoTest(this);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000529}
530
531
532void FullCodeGenerator::EffectContext::PlugTOS() const {
533 __ Drop(1);
534}
535
536
537void FullCodeGenerator::AccumulatorValueContext::PlugTOS() const {
danno@chromium.org59400602013-08-13 17:09:37 +0000538 __ Pop(result_register());
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000539}
540
541
542void FullCodeGenerator::StackValueContext::PlugTOS() const {
543}
544
545
546void FullCodeGenerator::TestContext::PlugTOS() const {
547 // For simplicity we always test the accumulator register.
danno@chromium.org59400602013-08-13 17:09:37 +0000548 __ Pop(result_register());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000549 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000550 codegen()->DoTest(this);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000551}
552
553
554void FullCodeGenerator::EffectContext::PrepareTest(
555 Label* materialize_true,
556 Label* materialize_false,
557 Label** if_true,
558 Label** if_false,
559 Label** fall_through) const {
560 // In an effect context, the true and the false case branch to the
561 // same label.
562 *if_true = *if_false = *fall_through = materialize_true;
563}
564
565
566void FullCodeGenerator::AccumulatorValueContext::PrepareTest(
567 Label* materialize_true,
568 Label* materialize_false,
569 Label** if_true,
570 Label** if_false,
571 Label** fall_through) const {
572 *if_true = *fall_through = materialize_true;
573 *if_false = materialize_false;
574}
575
576
577void FullCodeGenerator::StackValueContext::PrepareTest(
578 Label* materialize_true,
579 Label* materialize_false,
580 Label** if_true,
581 Label** if_false,
582 Label** fall_through) const {
583 *if_true = *fall_through = materialize_true;
584 *if_false = materialize_false;
585}
586
587
588void FullCodeGenerator::TestContext::PrepareTest(
589 Label* materialize_true,
590 Label* materialize_false,
591 Label** if_true,
592 Label** if_false,
593 Label** fall_through) const {
594 *if_true = true_label_;
595 *if_false = false_label_;
596 *fall_through = fall_through_;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000597}
598
599
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000600void FullCodeGenerator::DoTest(const TestContext* context) {
601 DoTest(context->condition(),
602 context->true_label(),
603 context->false_label(),
604 context->fall_through());
605}
606
607
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000608void FullCodeGenerator::AllocateModules(ZoneList<Declaration*>* declarations) {
609 ASSERT(scope_->is_global_scope());
610
611 for (int i = 0; i < declarations->length(); i++) {
612 ModuleDeclaration* declaration = declarations->at(i)->AsModuleDeclaration();
613 if (declaration != NULL) {
614 ModuleLiteral* module = declaration->module()->AsModuleLiteral();
615 if (module != NULL) {
616 Comment cmnt(masm_, "[ Link nested modules");
617 Scope* scope = module->body()->scope();
618 Interface* interface = scope->interface();
619 ASSERT(interface->IsModule() && interface->IsFrozen());
620
621 interface->Allocate(scope->module_var()->index());
622
623 // Set up module context.
624 ASSERT(scope->interface()->Index() >= 0);
625 __ Push(Smi::FromInt(scope->interface()->Index()));
626 __ Push(scope->GetScopeInfo());
627 __ CallRuntime(Runtime::kPushModuleContext, 2);
628 StoreToFrameField(StandardFrameConstants::kContextOffset,
629 context_register());
630
631 AllocateModules(scope->declarations());
632
633 // Pop module context.
634 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
635 // Update local stack frame context field.
636 StoreToFrameField(StandardFrameConstants::kContextOffset,
637 context_register());
638 }
639 }
640 }
641}
642
643
644// Modules have their own local scope, represented by their own context.
645// Module instance objects have an accessor for every export that forwards
646// access to the respective slot from the module's context. (Exports that are
647// modules themselves, however, are simple data properties.)
648//
649// All modules have a _hosting_ scope/context, which (currently) is the
650// (innermost) enclosing global scope. To deal with recursion, nested modules
651// are hosted by the same scope as global ones.
652//
653// For every (global or nested) module literal, the hosting context has an
654// internal slot that points directly to the respective module context. This
655// enables quick access to (statically resolved) module members by 2-dimensional
656// access through the hosting context. For example,
657//
658// module A {
659// let x;
660// module B { let y; }
661// }
662// module C { let z; }
663//
664// allocates contexts as follows:
665//
666// [header| .A | .B | .C | A | C ] (global)
667// | | |
668// | | +-- [header| z ] (module)
669// | |
670// | +------- [header| y ] (module)
671// |
672// +------------ [header| x | B ] (module)
673//
674// Here, .A, .B, .C are the internal slots pointing to the hosted module
675// contexts, whereas A, B, C hold the actual instance objects (note that every
676// module context also points to the respective instance object through its
677// extension slot in the header).
678//
679// To deal with arbitrary recursion and aliases between modules,
680// they are created and initialized in several stages. Each stage applies to
681// all modules in the hosting global scope, including nested ones.
682//
683// 1. Allocate: for each module _literal_, allocate the module contexts and
684// respective instance object and wire them up. This happens in the
685// PushModuleContext runtime function, as generated by AllocateModules
686// (invoked by VisitDeclarations in the hosting scope).
687//
688// 2. Bind: for each module _declaration_ (i.e. literals as well as aliases),
689// assign the respective instance object to respective local variables. This
690// happens in VisitModuleDeclaration, and uses the instance objects created
691// in the previous stage.
692// For each module _literal_, this phase also constructs a module descriptor
693// for the next stage. This happens in VisitModuleLiteral.
694//
695// 3. Populate: invoke the DeclareModules runtime function to populate each
696// _instance_ object with accessors for it exports. This is generated by
697// DeclareModules (invoked by VisitDeclarations in the hosting scope again),
698// and uses the descriptors generated in the previous stage.
699//
700// 4. Initialize: execute the module bodies (and other code) in sequence. This
701// happens by the separate statements generated for module bodies. To reenter
702// the module scopes properly, the parser inserted ModuleStatements.
703
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000704void FullCodeGenerator::VisitDeclarations(
705 ZoneList<Declaration*>* declarations) {
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000706 Handle<FixedArray> saved_modules = modules_;
707 int saved_module_index = module_index_;
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000708 ZoneList<Handle<Object> >* saved_globals = globals_;
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000709 ZoneList<Handle<Object> > inner_globals(10, zone());
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000710 globals_ = &inner_globals;
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000711
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000712 if (scope_->num_modules() != 0) {
713 // This is a scope hosting modules. Allocate a descriptor array to pass
714 // to the runtime for initialization.
715 Comment cmnt(masm_, "[ Allocate modules");
716 ASSERT(scope_->is_global_scope());
717 modules_ =
718 isolate()->factory()->NewFixedArray(scope_->num_modules(), TENURED);
719 module_index_ = 0;
720
721 // Generate code for allocating all modules, including nested ones.
722 // The allocated contexts are stored in internal variables in this scope.
723 AllocateModules(declarations);
724 }
725
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000726 AstVisitor::VisitDeclarations(declarations);
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000727
728 if (scope_->num_modules() != 0) {
729 // Initialize modules from descriptor array.
730 ASSERT(module_index_ == modules_->length());
731 DeclareModules(modules_);
732 modules_ = saved_modules;
733 module_index_ = saved_module_index;
734 }
735
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000736 if (!globals_->is_empty()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000737 // Invoke the platform-dependent code generator to do the actual
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000738 // declaration of the global functions and variables.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000739 Handle<FixedArray> array =
740 isolate()->factory()->NewFixedArray(globals_->length(), TENURED);
741 for (int i = 0; i < globals_->length(); ++i)
742 array->set(i, *globals_->at(i));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000743 DeclareGlobals(array);
744 }
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000745
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000746 globals_ = saved_globals;
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000747}
748
749
750void FullCodeGenerator::VisitModuleLiteral(ModuleLiteral* module) {
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000751 Block* block = module->body();
752 Scope* saved_scope = scope();
753 scope_ = block->scope();
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000754 Interface* interface = scope_->interface();
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000755
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000756 Comment cmnt(masm_, "[ ModuleLiteral");
757 SetStatementPosition(block);
758
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000759 ASSERT(!modules_.is_null());
760 ASSERT(module_index_ < modules_->length());
761 int index = module_index_++;
762
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000763 // Set up module context.
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000764 ASSERT(interface->Index() >= 0);
765 __ Push(Smi::FromInt(interface->Index()));
766 __ Push(Smi::FromInt(0));
767 __ CallRuntime(Runtime::kPushModuleContext, 2);
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000768 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000769
770 {
771 Comment cmnt(masm_, "[ Declarations");
772 VisitDeclarations(scope_->declarations());
773 }
774
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000775 // Populate the module description.
776 Handle<ModuleInfo> description =
777 ModuleInfo::Create(isolate(), interface, scope_);
778 modules_->set(index, *description);
779
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000780 scope_ = saved_scope;
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000781 // Pop module context.
782 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
783 // Update local stack frame context field.
784 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000785}
786
787
788void FullCodeGenerator::VisitModuleVariable(ModuleVariable* module) {
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000789 // Nothing to do.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000790 // The instance object is resolved statically through the module's interface.
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000791}
792
793
794void FullCodeGenerator::VisitModulePath(ModulePath* module) {
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000795 // Nothing to do.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000796 // The instance object is resolved statically through the module's interface.
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000797}
798
799
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000800void FullCodeGenerator::VisitModuleUrl(ModuleUrl* module) {
801 // TODO(rossberg): dummy allocation for now.
802 Scope* scope = module->body()->scope();
803 Interface* interface = scope_->interface();
804
805 ASSERT(interface->IsModule() && interface->IsFrozen());
806 ASSERT(!modules_.is_null());
807 ASSERT(module_index_ < modules_->length());
808 interface->Allocate(scope->module_var()->index());
809 int index = module_index_++;
810
811 Handle<ModuleInfo> description =
812 ModuleInfo::Create(isolate(), interface, scope_);
813 modules_->set(index, *description);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000814}
815
816
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000817int FullCodeGenerator::DeclareGlobalsFlags() {
dslomov@chromium.org486536d2014-03-12 13:09:18 +0000818 ASSERT(DeclareGlobalsStrictMode::is_valid(strict_mode()));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000819 return DeclareGlobalsEvalFlag::encode(is_eval()) |
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000820 DeclareGlobalsNativeFlag::encode(is_native()) |
dslomov@chromium.org486536d2014-03-12 13:09:18 +0000821 DeclareGlobalsStrictMode::encode(strict_mode());
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000822}
823
824
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000825void FullCodeGenerator::SetFunctionPosition(FunctionLiteral* fun) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000826 CodeGenerator::RecordPositions(masm_, fun->start_position());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000827}
828
829
830void FullCodeGenerator::SetReturnPosition(FunctionLiteral* fun) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000831 CodeGenerator::RecordPositions(masm_, fun->end_position() - 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000832}
833
834
835void FullCodeGenerator::SetStatementPosition(Statement* stmt) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000836#ifdef ENABLE_DEBUGGER_SUPPORT
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000837 if (!isolate()->debugger()->IsDebuggerActive()) {
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +0000838 CodeGenerator::RecordPositions(masm_, stmt->position());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000839 } else {
840 // Check if the statement will be breakable without adding a debug break
841 // slot.
machenbach@chromium.org6d26cbb2014-01-22 10:50:56 +0000842 BreakableStatementChecker checker(zone());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000843 checker.Check(stmt);
844 // Record the statement position right here if the statement is not
845 // breakable. For breakable statements the actual recording of the
846 // position will be postponed to the breakable code (typically an IC).
847 bool position_recorded = CodeGenerator::RecordPositions(
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +0000848 masm_, stmt->position(), !checker.is_breakable());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000849 // If the position recording did record a new position generate a debug
850 // break slot to make the statement breakable.
851 if (position_recorded) {
852 Debug::GenerateSlot(masm_);
853 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000854 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000855#else
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +0000856 CodeGenerator::RecordPositions(masm_, stmt->position());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000857#endif
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000858}
859
860
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +0000861void FullCodeGenerator::SetExpressionPosition(Expression* expr) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000862#ifdef ENABLE_DEBUGGER_SUPPORT
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000863 if (!isolate()->debugger()->IsDebuggerActive()) {
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +0000864 CodeGenerator::RecordPositions(masm_, expr->position());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000865 } else {
866 // Check if the expression will be breakable without adding a debug break
867 // slot.
machenbach@chromium.org6d26cbb2014-01-22 10:50:56 +0000868 BreakableStatementChecker checker(zone());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000869 checker.Check(expr);
870 // Record a statement position right here if the expression is not
871 // breakable. For breakable expressions the actual recording of the
872 // position will be postponed to the breakable code (typically an IC).
873 // NOTE this will record a statement position for something which might
874 // not be a statement. As stepping in the debugger will only stop at
875 // statement positions this is used for e.g. the condition expression of
876 // a do while loop.
877 bool position_recorded = CodeGenerator::RecordPositions(
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +0000878 masm_, expr->position(), !checker.is_breakable());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000879 // If the position recording did record a new position generate a debug
880 // break slot to make the statement breakable.
881 if (position_recorded) {
882 Debug::GenerateSlot(masm_);
883 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000884 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000885#else
886 CodeGenerator::RecordPositions(masm_, pos);
887#endif
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000888}
889
890
891void FullCodeGenerator::SetStatementPosition(int pos) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000892 CodeGenerator::RecordPositions(masm_, pos);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000893}
894
895
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000896void FullCodeGenerator::SetSourcePosition(int pos) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000897 if (pos != RelocInfo::kNoPosition) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000898 masm_->positions_recorder()->RecordPosition(pos);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000899 }
900}
901
902
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000903// Lookup table for code generators for special runtime calls which are
904// generated inline.
905#define INLINE_FUNCTION_GENERATOR_ADDRESS(Name, argc, ressize) \
906 &FullCodeGenerator::Emit##Name,
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000907
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000908const FullCodeGenerator::InlineFunctionGenerator
909 FullCodeGenerator::kInlineFunctionGenerators[] = {
910 INLINE_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS)
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000911 };
912#undef INLINE_FUNCTION_GENERATOR_ADDRESS
913
914
915FullCodeGenerator::InlineFunctionGenerator
916 FullCodeGenerator::FindInlineFunctionGenerator(Runtime::FunctionId id) {
whesse@chromium.org023421e2010-12-21 12:19:12 +0000917 int lookup_index =
918 static_cast<int>(id) - static_cast<int>(Runtime::kFirstInlineFunction);
919 ASSERT(lookup_index >= 0);
920 ASSERT(static_cast<size_t>(lookup_index) <
921 ARRAY_SIZE(kInlineFunctionGenerators));
922 return kInlineFunctionGenerators[lookup_index];
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000923}
924
925
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000926void FullCodeGenerator::EmitInlineRuntimeCall(CallRuntime* expr) {
927 const Runtime::Function* function = expr->function();
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000928 ASSERT(function != NULL);
929 ASSERT(function->intrinsic_type == Runtime::INLINE);
930 InlineFunctionGenerator generator =
931 FindInlineFunctionGenerator(function->function_id);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000932 ((*this).*(generator))(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000933}
934
935
verwaest@chromium.org8a00e822013-06-10 15:11:22 +0000936void FullCodeGenerator::EmitGeneratorNext(CallRuntime* expr) {
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000937 ZoneList<Expression*>* args = expr->arguments();
938 ASSERT(args->length() == 2);
verwaest@chromium.org8a00e822013-06-10 15:11:22 +0000939 EmitGeneratorResume(args->at(0), args->at(1), JSGeneratorObject::NEXT);
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000940}
941
942
943void FullCodeGenerator::EmitGeneratorThrow(CallRuntime* expr) {
944 ZoneList<Expression*>* args = expr->arguments();
945 ASSERT(args->length() == 2);
946 EmitGeneratorResume(args->at(0), args->at(1), JSGeneratorObject::THROW);
947}
948
949
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +0000950void FullCodeGenerator::EmitDebugBreakInOptimizedCode(CallRuntime* expr) {
951 context()->Plug(handle(Smi::FromInt(0), isolate()));
952}
953
954
machenbach@chromium.orgca2f2042014-03-10 10:03:12 +0000955void FullCodeGenerator::EmitDoubleHi(CallRuntime* expr) {
956 ZoneList<Expression*>* args = expr->arguments();
957 ASSERT(args->length() == 1);
958 VisitForStackValue(args->at(0));
959 masm()->CallRuntime(Runtime::kDoubleHi, 1);
960 context()->Plug(result_register());
961}
962
963
964void FullCodeGenerator::EmitDoubleLo(CallRuntime* expr) {
965 ZoneList<Expression*>* args = expr->arguments();
966 ASSERT(args->length() == 1);
967 VisitForStackValue(args->at(0));
968 masm()->CallRuntime(Runtime::kDoubleLo, 1);
969 context()->Plug(result_register());
970}
971
972
973void FullCodeGenerator::EmitConstructDouble(CallRuntime* expr) {
974 ZoneList<Expression*>* args = expr->arguments();
975 ASSERT(args->length() == 2);
976 VisitForStackValue(args->at(0));
977 VisitForStackValue(args->at(1));
978 masm()->CallRuntime(Runtime::kConstructDouble, 2);
979 context()->Plug(result_register());
980}
981
982
ricow@chromium.org65fae842010-08-25 15:26:24 +0000983void FullCodeGenerator::VisitBinaryOperation(BinaryOperation* expr) {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000984 switch (expr->op()) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000985 case Token::COMMA:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000986 return VisitComma(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000987 case Token::OR:
988 case Token::AND:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000989 return VisitLogicalExpression(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000990 default:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000991 return VisitArithmeticExpression(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000992 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000993}
994
995
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000996void FullCodeGenerator::VisitInDuplicateContext(Expression* expr) {
997 if (context()->IsEffect()) {
998 VisitForEffect(expr);
999 } else if (context()->IsAccumulatorValue()) {
1000 VisitForAccumulatorValue(expr);
1001 } else if (context()->IsStackValue()) {
1002 VisitForStackValue(expr);
1003 } else if (context()->IsTest()) {
1004 const TestContext* test = TestContext::cast(context());
1005 VisitForControl(expr, test->true_label(), test->false_label(),
1006 test->fall_through());
1007 }
1008}
1009
1010
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001011void FullCodeGenerator::VisitComma(BinaryOperation* expr) {
1012 Comment cmnt(masm_, "[ Comma");
1013 VisitForEffect(expr->left());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001014 VisitInDuplicateContext(expr->right());
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001015}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001016
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001017
1018void FullCodeGenerator::VisitLogicalExpression(BinaryOperation* expr) {
1019 bool is_logical_and = expr->op() == Token::AND;
1020 Comment cmnt(masm_, is_logical_and ? "[ Logical AND" : "[ Logical OR");
1021 Expression* left = expr->left();
1022 Expression* right = expr->right();
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001023 BailoutId right_id = expr->RightId();
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001024 Label done;
1025
1026 if (context()->IsTest()) {
1027 Label eval_right;
1028 const TestContext* test = TestContext::cast(context());
1029 if (is_logical_and) {
1030 VisitForControl(left, &eval_right, test->false_label(), &eval_right);
1031 } else {
1032 VisitForControl(left, test->true_label(), &eval_right, &eval_right);
1033 }
1034 PrepareForBailoutForId(right_id, NO_REGISTERS);
1035 __ bind(&eval_right);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001036
1037 } else if (context()->IsAccumulatorValue()) {
1038 VisitForAccumulatorValue(left);
1039 // We want the value in the accumulator for the test, and on the stack in
1040 // case we need it.
danno@chromium.org59400602013-08-13 17:09:37 +00001041 __ Push(result_register());
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001042 Label discard, restore;
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001043 if (is_logical_and) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001044 DoTest(left, &discard, &restore, &restore);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001045 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001046 DoTest(left, &restore, &discard, &restore);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001047 }
1048 __ bind(&restore);
danno@chromium.org59400602013-08-13 17:09:37 +00001049 __ Pop(result_register());
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001050 __ jmp(&done);
1051 __ bind(&discard);
1052 __ Drop(1);
1053 PrepareForBailoutForId(right_id, NO_REGISTERS);
1054
1055 } else if (context()->IsStackValue()) {
1056 VisitForAccumulatorValue(left);
1057 // We want the value in the accumulator for the test, and on the stack in
1058 // case we need it.
danno@chromium.org59400602013-08-13 17:09:37 +00001059 __ Push(result_register());
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001060 Label discard;
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001061 if (is_logical_and) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001062 DoTest(left, &discard, &done, &discard);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001063 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001064 DoTest(left, &done, &discard, &discard);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001065 }
1066 __ bind(&discard);
1067 __ Drop(1);
1068 PrepareForBailoutForId(right_id, NO_REGISTERS);
1069
1070 } else {
1071 ASSERT(context()->IsEffect());
1072 Label eval_right;
1073 if (is_logical_and) {
1074 VisitForControl(left, &eval_right, &done, &eval_right);
1075 } else {
1076 VisitForControl(left, &done, &eval_right, &eval_right);
1077 }
1078 PrepareForBailoutForId(right_id, NO_REGISTERS);
1079 __ bind(&eval_right);
1080 }
1081
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001082 VisitInDuplicateContext(right);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001083 __ bind(&done);
1084}
1085
1086
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001087void FullCodeGenerator::VisitArithmeticExpression(BinaryOperation* expr) {
1088 Token::Value op = expr->op();
1089 Comment cmnt(masm_, "[ ArithmeticExpression");
1090 Expression* left = expr->left();
1091 Expression* right = expr->right();
1092 OverwriteMode mode =
1093 left->ResultOverwriteAllowed()
1094 ? OVERWRITE_LEFT
1095 : (right->ResultOverwriteAllowed() ? OVERWRITE_RIGHT : NO_OVERWRITE);
1096
1097 VisitForStackValue(left);
1098 VisitForAccumulatorValue(right);
1099
1100 SetSourcePosition(expr->position());
1101 if (ShouldInlineSmiCase(op)) {
1102 EmitInlineSmiBinaryOp(expr, op, mode, left, right);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001103 } else {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001104 EmitBinaryOp(expr, op, mode);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001105 }
1106}
1107
1108
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001109void FullCodeGenerator::VisitBlock(Block* stmt) {
1110 Comment cmnt(masm_, "[ Block");
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001111 NestedBlock nested_block(this, stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001112 SetStatementPosition(stmt);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001113
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001114 Scope* saved_scope = scope();
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001115 // Push a block context when entering a block with block scoped variables.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00001116 if (stmt->scope() != NULL) {
danno@chromium.org81cac2b2012-07-10 11:28:27 +00001117 scope_ = stmt->scope();
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00001118 ASSERT(!scope_->is_module_scope());
1119 { Comment cmnt(masm_, "[ Extend block context");
machenbach@chromium.org05150ab2014-01-29 08:13:29 +00001120 __ Push(scope_->GetScopeInfo());
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00001121 PushFunctionArgumentForContextAllocation();
machenbach@chromium.org05150ab2014-01-29 08:13:29 +00001122 __ CallRuntime(Runtime::kPushBlockContext, 2);
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001123
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00001124 // Replace the context stored in the frame.
1125 StoreToFrameField(StandardFrameConstants::kContextOffset,
1126 context_register());
1127 }
1128 { Comment cmnt(masm_, "[ Declarations");
1129 VisitDeclarations(scope_->declarations());
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001130 }
1131 }
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00001132
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00001133 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001134 VisitStatements(stmt->statements());
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001135 scope_ = saved_scope;
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001136 __ bind(nested_block.break_label());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001137 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001138
1139 // Pop block context if necessary.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00001140 if (stmt->scope() != NULL) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001141 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1142 // Update local stack frame context field.
1143 StoreToFrameField(StandardFrameConstants::kContextOffset,
1144 context_register());
1145 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001146}
1147
1148
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00001149void FullCodeGenerator::VisitModuleStatement(ModuleStatement* stmt) {
1150 Comment cmnt(masm_, "[ Module context");
1151
1152 __ Push(Smi::FromInt(stmt->proxy()->interface()->Index()));
1153 __ Push(Smi::FromInt(0));
1154 __ CallRuntime(Runtime::kPushModuleContext, 2);
1155 StoreToFrameField(
1156 StandardFrameConstants::kContextOffset, context_register());
1157
1158 Scope* saved_scope = scope_;
1159 scope_ = stmt->body()->scope();
1160 VisitStatements(stmt->body()->statements());
1161 scope_ = saved_scope;
1162 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1163 // Update local stack frame context field.
1164 StoreToFrameField(StandardFrameConstants::kContextOffset,
1165 context_register());
1166}
1167
1168
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001169void FullCodeGenerator::VisitExpressionStatement(ExpressionStatement* stmt) {
1170 Comment cmnt(masm_, "[ ExpressionStatement");
1171 SetStatementPosition(stmt);
1172 VisitForEffect(stmt->expression());
1173}
1174
1175
1176void FullCodeGenerator::VisitEmptyStatement(EmptyStatement* stmt) {
1177 Comment cmnt(masm_, "[ EmptyStatement");
1178 SetStatementPosition(stmt);
1179}
1180
1181
1182void FullCodeGenerator::VisitIfStatement(IfStatement* stmt) {
1183 Comment cmnt(masm_, "[ IfStatement");
1184 SetStatementPosition(stmt);
1185 Label then_part, else_part, done;
1186
ricow@chromium.org65fae842010-08-25 15:26:24 +00001187 if (stmt->HasElseStatement()) {
1188 VisitForControl(stmt->condition(), &then_part, &else_part, &then_part);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001189 PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001190 __ bind(&then_part);
1191 Visit(stmt->then_statement());
1192 __ jmp(&done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001193
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001194 PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001195 __ bind(&else_part);
1196 Visit(stmt->else_statement());
1197 } else {
1198 VisitForControl(stmt->condition(), &then_part, &done, &then_part);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001199 PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001200 __ bind(&then_part);
1201 Visit(stmt->then_statement());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001202
1203 PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001204 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001205 __ bind(&done);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001206 PrepareForBailoutForId(stmt->IfId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001207}
1208
1209
1210void FullCodeGenerator::VisitContinueStatement(ContinueStatement* stmt) {
1211 Comment cmnt(masm_, "[ ContinueStatement");
1212 SetStatementPosition(stmt);
1213 NestedStatement* current = nesting_stack_;
1214 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001215 int context_length = 0;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001216 // When continuing, we clobber the unpredictable value in the accumulator
1217 // with one that's safe for GC. If we hit an exit from the try block of
1218 // try...finally on our way out, we will unconditionally preserve the
1219 // accumulator on the stack.
1220 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001221 while (!current->IsContinueTarget(stmt->target())) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001222 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001223 }
1224 __ Drop(stack_depth);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001225 if (context_length > 0) {
1226 while (context_length > 0) {
1227 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1228 --context_length;
1229 }
1230 StoreToFrameField(StandardFrameConstants::kContextOffset,
1231 context_register());
1232 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001233
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001234 __ jmp(current->AsIteration()->continue_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001235}
1236
1237
1238void FullCodeGenerator::VisitBreakStatement(BreakStatement* stmt) {
1239 Comment cmnt(masm_, "[ BreakStatement");
1240 SetStatementPosition(stmt);
1241 NestedStatement* current = nesting_stack_;
1242 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001243 int context_length = 0;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001244 // When breaking, we clobber the unpredictable value in the accumulator
1245 // with one that's safe for GC. If we hit an exit from the try block of
1246 // try...finally on our way out, we will unconditionally preserve the
1247 // accumulator on the stack.
1248 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001249 while (!current->IsBreakTarget(stmt->target())) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001250 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001251 }
1252 __ Drop(stack_depth);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001253 if (context_length > 0) {
1254 while (context_length > 0) {
1255 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1256 --context_length;
1257 }
1258 StoreToFrameField(StandardFrameConstants::kContextOffset,
1259 context_register());
1260 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001261
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001262 __ jmp(current->AsBreakable()->break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001263}
1264
1265
danno@chromium.org41728482013-06-12 22:31:22 +00001266void FullCodeGenerator::EmitUnwindBeforeReturn() {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001267 NestedStatement* current = nesting_stack_;
1268 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001269 int context_length = 0;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001270 while (current != NULL) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001271 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001272 }
1273 __ Drop(stack_depth);
danno@chromium.org41728482013-06-12 22:31:22 +00001274}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001275
danno@chromium.org41728482013-06-12 22:31:22 +00001276
1277void FullCodeGenerator::VisitReturnStatement(ReturnStatement* stmt) {
1278 Comment cmnt(masm_, "[ ReturnStatement");
1279 SetStatementPosition(stmt);
1280 Expression* expr = stmt->expression();
1281 VisitForAccumulatorValue(expr);
1282 EmitUnwindBeforeReturn();
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001283 EmitReturnSequence();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001284}
1285
1286
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001287void FullCodeGenerator::VisitWithStatement(WithStatement* stmt) {
1288 Comment cmnt(masm_, "[ WithStatement");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001289 SetStatementPosition(stmt);
1290
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001291 VisitForStackValue(stmt->expression());
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00001292 PushFunctionArgumentForContextAllocation();
1293 __ CallRuntime(Runtime::kPushWithContext, 2);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001294 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001295
danno@chromium.orgca29dd82013-04-26 11:59:48 +00001296 Scope* saved_scope = scope();
1297 scope_ = stmt->scope();
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001298 { WithOrCatch body(this);
1299 Visit(stmt->statement());
1300 }
danno@chromium.orgca29dd82013-04-26 11:59:48 +00001301 scope_ = saved_scope;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001302
1303 // Pop context.
1304 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1305 // Update local stack frame context field.
1306 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001307}
1308
1309
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001310void FullCodeGenerator::VisitDoWhileStatement(DoWhileStatement* stmt) {
1311 Comment cmnt(masm_, "[ DoWhileStatement");
1312 SetStatementPosition(stmt);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001313 Label body, book_keeping;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001314
1315 Iteration loop_statement(this, stmt);
1316 increment_loop_depth();
1317
1318 __ bind(&body);
1319 Visit(stmt->body());
1320
ricow@chromium.org65fae842010-08-25 15:26:24 +00001321 // Record the position of the do while condition and make sure it is
1322 // possible to break on the condition.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001323 __ bind(loop_statement.continue_label());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001324 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +00001325 SetExpressionPosition(stmt->cond());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001326 VisitForControl(stmt->cond(),
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001327 &book_keeping,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001328 loop_statement.break_label(),
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001329 &book_keeping);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001330
1331 // Check stack before looping.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001332 PrepareForBailoutForId(stmt->BackEdgeId(), NO_REGISTERS);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001333 __ bind(&book_keeping);
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +00001334 EmitBackEdgeBookkeeping(stmt, &body);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001335 __ jmp(&body);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001336
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001337 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001338 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001339 decrement_loop_depth();
1340}
1341
1342
1343void FullCodeGenerator::VisitWhileStatement(WhileStatement* stmt) {
1344 Comment cmnt(masm_, "[ WhileStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001345 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001346
1347 Iteration loop_statement(this, stmt);
1348 increment_loop_depth();
1349
1350 // Emit the test at the bottom of the loop.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001351 __ jmp(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001352
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001353 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001354 __ bind(&body);
1355 Visit(stmt->body());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001356
1357 // Emit the statement position here as this is where the while
1358 // statement code starts.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001359 __ bind(loop_statement.continue_label());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001360 SetStatementPosition(stmt);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001361
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001362 // Check stack before looping.
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +00001363 EmitBackEdgeBookkeeping(stmt, &body);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001364
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001365 __ bind(&test);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001366 VisitForControl(stmt->cond(),
1367 &body,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001368 loop_statement.break_label(),
1369 loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001370
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001371 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001372 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001373 decrement_loop_depth();
1374}
1375
1376
1377void FullCodeGenerator::VisitForStatement(ForStatement* stmt) {
1378 Comment cmnt(masm_, "[ ForStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001379 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001380
1381 Iteration loop_statement(this, stmt);
erik.corry@gmail.combbceb572012-03-09 10:52:05 +00001382
1383 // Set statement position for a break slot before entering the for-body.
1384 SetStatementPosition(stmt);
1385
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001386 if (stmt->init() != NULL) {
1387 Visit(stmt->init());
1388 }
1389
1390 increment_loop_depth();
1391 // Emit the test at the bottom of the loop (even if empty).
1392 __ jmp(&test);
1393
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001394 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001395 __ bind(&body);
1396 Visit(stmt->body());
1397
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001398 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001399 __ bind(loop_statement.continue_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001400 if (stmt->next() != NULL) {
1401 Visit(stmt->next());
1402 }
1403
ricow@chromium.org65fae842010-08-25 15:26:24 +00001404 // Emit the statement position here as this is where the for
1405 // statement code starts.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001406 SetStatementPosition(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001407
1408 // Check stack before looping.
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +00001409 EmitBackEdgeBookkeeping(stmt, &body);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001410
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001411 __ bind(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001412 if (stmt->cond() != NULL) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00001413 VisitForControl(stmt->cond(),
1414 &body,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001415 loop_statement.break_label(),
1416 loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001417 } else {
1418 __ jmp(&body);
1419 }
1420
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001421 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001422 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001423 decrement_loop_depth();
1424}
1425
1426
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001427void FullCodeGenerator::VisitTryCatchStatement(TryCatchStatement* stmt) {
1428 Comment cmnt(masm_, "[ TryCatchStatement");
1429 SetStatementPosition(stmt);
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001430 // The try block adds a handler to the exception handler chain before
1431 // entering, and removes it again when exiting normally. If an exception
1432 // is thrown during execution of the try block, the handler is consumed
1433 // and control is passed to the catch block with the exception in the
1434 // result register.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001435
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001436 Label try_entry, handler_entry, exit;
1437 __ jmp(&try_entry);
1438 __ bind(&handler_entry);
1439 handler_table()->set(stmt->index(), Smi::FromInt(handler_entry.pos()));
1440 // Exception handler code, the exception is in the result register.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001441 // Extend the context before executing the catch block.
1442 { Comment cmnt(masm_, "[ Extend catch context");
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001443 __ Push(stmt->variable()->name());
danno@chromium.org59400602013-08-13 17:09:37 +00001444 __ Push(result_register());
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00001445 PushFunctionArgumentForContextAllocation();
1446 __ CallRuntime(Runtime::kPushCatchContext, 3);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001447 StoreToFrameField(StandardFrameConstants::kContextOffset,
1448 context_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001449 }
1450
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001451 Scope* saved_scope = scope();
1452 scope_ = stmt->scope();
1453 ASSERT(scope_->declarations()->is_empty());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001454 { WithOrCatch catch_body(this);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001455 Visit(stmt->catch_block());
1456 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001457 // Restore the context.
1458 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1459 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001460 scope_ = saved_scope;
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001461 __ jmp(&exit);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001462
1463 // Try block code. Sets up the exception handler chain.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001464 __ bind(&try_entry);
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00001465 __ PushTryHandler(StackHandler::CATCH, stmt->index());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001466 { TryCatch try_body(this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001467 Visit(stmt->try_block());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001468 }
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001469 __ PopTryHandler();
1470 __ bind(&exit);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001471}
1472
1473
1474void FullCodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* stmt) {
1475 Comment cmnt(masm_, "[ TryFinallyStatement");
1476 SetStatementPosition(stmt);
1477 // Try finally is compiled by setting up a try-handler on the stack while
1478 // executing the try body, and removing it again afterwards.
1479 //
1480 // The try-finally construct can enter the finally block in three ways:
1481 // 1. By exiting the try-block normally. This removes the try-handler and
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001482 // calls the finally block code before continuing.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001483 // 2. By exiting the try-block with a function-local control flow transfer
1484 // (break/continue/return). The site of the, e.g., break removes the
1485 // try handler and calls the finally block code before continuing
1486 // its outward control transfer.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001487 // 3. By exiting the try-block with a thrown exception.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001488 // This can happen in nested function calls. It traverses the try-handler
1489 // chain and consumes the try-handler entry before jumping to the
1490 // handler code. The handler code then calls the finally-block before
1491 // rethrowing the exception.
1492 //
1493 // The finally block must assume a return address on top of the stack
1494 // (or in the link register on ARM chips) and a value (return value or
1495 // exception) in the result register (rax/eax/r0), both of which must
1496 // be preserved. The return address isn't GC-safe, so it should be
1497 // cooked before GC.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001498 Label try_entry, handler_entry, finally_entry;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001499
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001500 // Jump to try-handler setup and try-block code.
1501 __ jmp(&try_entry);
1502 __ bind(&handler_entry);
1503 handler_table()->set(stmt->index(), Smi::FromInt(handler_entry.pos()));
1504 // Exception handler code. This code is only executed when an exception
1505 // is thrown. The exception is in the result register, and must be
1506 // preserved by the finally block. Call the finally block and then
1507 // rethrow the exception if it returns.
1508 __ Call(&finally_entry);
danno@chromium.org59400602013-08-13 17:09:37 +00001509 __ Push(result_register());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001510 __ CallRuntime(Runtime::kReThrow, 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001511
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001512 // Finally block implementation.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001513 __ bind(&finally_entry);
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001514 EnterFinallyBlock();
1515 { Finally finally_body(this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001516 Visit(stmt->finally_block());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001517 }
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001518 ExitFinallyBlock(); // Return to the calling code.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001519
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001520 // Set up try handler.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001521 __ bind(&try_entry);
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00001522 __ PushTryHandler(StackHandler::FINALLY, stmt->index());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001523 { TryFinally try_body(this, &finally_entry);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001524 Visit(stmt->try_block());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001525 }
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001526 __ PopTryHandler();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001527 // Execute the finally block on the way out. Clobber the unpredictable
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001528 // value in the result register with one that's safe for GC because the
1529 // finally block will unconditionally preserve the result register on the
1530 // stack.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001531 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001532 __ Call(&finally_entry);
1533}
1534
1535
1536void FullCodeGenerator::VisitDebuggerStatement(DebuggerStatement* stmt) {
1537#ifdef ENABLE_DEBUGGER_SUPPORT
1538 Comment cmnt(masm_, "[ DebuggerStatement");
1539 SetStatementPosition(stmt);
1540
ager@chromium.org5c838252010-02-19 08:53:10 +00001541 __ DebugBreak();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001542 // Ignore the return value.
1543#endif
1544}
1545
1546
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +00001547void FullCodeGenerator::VisitCaseClause(CaseClause* clause) {
1548 UNREACHABLE();
1549}
1550
1551
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001552void FullCodeGenerator::VisitConditional(Conditional* expr) {
1553 Comment cmnt(masm_, "[ Conditional");
1554 Label true_case, false_case, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001555 VisitForControl(expr->condition(), &true_case, &false_case, &true_case);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001556
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001557 PrepareForBailoutForId(expr->ThenId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001558 __ bind(&true_case);
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +00001559 SetExpressionPosition(expr->then_expression());
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001560 if (context()->IsTest()) {
1561 const TestContext* for_test = TestContext::cast(context());
1562 VisitForControl(expr->then_expression(),
1563 for_test->true_label(),
1564 for_test->false_label(),
1565 NULL);
1566 } else {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001567 VisitInDuplicateContext(expr->then_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001568 __ jmp(&done);
1569 }
1570
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001571 PrepareForBailoutForId(expr->ElseId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001572 __ bind(&false_case);
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +00001573 SetExpressionPosition(expr->else_expression());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001574 VisitInDuplicateContext(expr->else_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001575 // If control flow falls through Visit, merge it with true case here.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001576 if (!context()->IsTest()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001577 __ bind(&done);
1578 }
1579}
1580
1581
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001582void FullCodeGenerator::VisitLiteral(Literal* expr) {
1583 Comment cmnt(masm_, "[ Literal");
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00001584 context()->Plug(expr->value());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001585}
1586
1587
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001588void FullCodeGenerator::VisitFunctionLiteral(FunctionLiteral* expr) {
1589 Comment cmnt(masm_, "[ FunctionLiteral");
1590
1591 // Build the function boilerplate and instantiate it.
1592 Handle<SharedFunctionInfo> function_info =
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001593 Compiler::BuildFunctionInfo(expr, script());
1594 if (function_info.is_null()) {
1595 SetStackOverflow();
1596 return;
1597 }
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001598 EmitNewClosure(function_info, expr->pretenure());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001599}
1600
1601
jkummerow@chromium.orgfb7a7c42013-10-02 11:41:02 +00001602void FullCodeGenerator::VisitNativeFunctionLiteral(
1603 NativeFunctionLiteral* expr) {
1604 Comment cmnt(masm_, "[ NativeFunctionLiteral");
1605
1606 // Compute the function template for the native function.
1607 Handle<String> name = expr->name();
1608 v8::Handle<v8::FunctionTemplate> fun_template =
machenbach@chromium.org37be4082013-11-26 13:50:38 +00001609 expr->extension()->GetNativeFunctionTemplate(
1610 reinterpret_cast<v8::Isolate*>(isolate()), v8::Utils::ToLocal(name));
jkummerow@chromium.orgfb7a7c42013-10-02 11:41:02 +00001611 ASSERT(!fun_template.IsEmpty());
1612
1613 // Instantiate the function and create a shared function info from it.
1614 Handle<JSFunction> fun = Utils::OpenHandle(*fun_template->GetFunction());
1615 const int literals = fun->NumberOfLiterals();
1616 Handle<Code> code = Handle<Code>(fun->shared()->code());
1617 Handle<Code> construct_stub = Handle<Code>(fun->shared()->construct_stub());
1618 bool is_generator = false;
1619 Handle<SharedFunctionInfo> shared =
1620 isolate()->factory()->NewSharedFunctionInfo(name, literals, is_generator,
machenbach@chromium.org8545d492014-03-17 09:28:03 +00001621 code, Handle<ScopeInfo>(fun->shared()->scope_info()));
jkummerow@chromium.orgfb7a7c42013-10-02 11:41:02 +00001622 shared->set_construct_stub(*construct_stub);
1623
1624 // Copy the function data to the shared function info.
1625 shared->set_function_data(fun->shared()->function_data());
1626 int parameters = fun->shared()->formal_parameter_count();
1627 shared->set_formal_parameter_count(parameters);
1628
1629 EmitNewClosure(shared, false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001630}
1631
1632
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001633void FullCodeGenerator::VisitThrow(Throw* expr) {
1634 Comment cmnt(masm_, "[ Throw");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001635 VisitForStackValue(expr->exception());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001636 __ CallRuntime(Runtime::kThrow, 1);
1637 // Never returns here.
1638}
1639
1640
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001641FullCodeGenerator::NestedStatement* FullCodeGenerator::TryCatch::Exit(
1642 int* stack_depth,
1643 int* context_length) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001644 // The macros used here must preserve the result register.
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001645 __ Drop(*stack_depth);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001646 __ PopTryHandler();
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001647 *stack_depth = 0;
1648 return previous_;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001649}
1650
ricow@chromium.org65fae842010-08-25 15:26:24 +00001651
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001652bool FullCodeGenerator::TryLiteralCompare(CompareOperation* expr) {
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001653 Expression* sub_expr;
ager@chromium.org04921a82011-06-27 13:21:41 +00001654 Handle<String> check;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001655 if (expr->IsLiteralCompareTypeof(&sub_expr, &check)) {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001656 EmitLiteralCompareTypeof(expr, sub_expr, check);
ager@chromium.org04921a82011-06-27 13:21:41 +00001657 return true;
1658 }
1659
jkummerow@chromium.org96a3c512013-07-18 17:02:47 +00001660 if (expr->IsLiteralCompareUndefined(&sub_expr, isolate())) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001661 EmitLiteralCompareNil(expr, sub_expr, kUndefinedValue);
1662 return true;
1663 }
1664
1665 if (expr->IsLiteralCompareNull(&sub_expr)) {
1666 EmitLiteralCompareNil(expr, sub_expr, kNullValue);
ager@chromium.org04921a82011-06-27 13:21:41 +00001667 return true;
1668 }
1669
1670 return false;
1671}
1672
1673
yangguo@chromium.org49546742013-12-23 16:17:49 +00001674void BackEdgeTable::Patch(Isolate* isolate, Code* unoptimized) {
machenbach@chromium.org528ce022013-09-23 14:09:36 +00001675 DisallowHeapAllocation no_gc;
machenbach@chromium.org8e36b5b2013-09-26 07:36:30 +00001676 Code* patch = isolate->builtins()->builtin(Builtins::kOnStackReplacement);
machenbach@chromium.org528ce022013-09-23 14:09:36 +00001677
1678 // Iterate over the back edge table and patch every interrupt
1679 // call to an unconditional call to the replacement code.
1680 int loop_nesting_level = unoptimized->allow_osr_at_loop_nesting_level();
1681
1682 BackEdgeTable back_edges(unoptimized, &no_gc);
1683 for (uint32_t i = 0; i < back_edges.length(); i++) {
1684 if (static_cast<int>(back_edges.loop_depth(i)) == loop_nesting_level) {
1685 ASSERT_EQ(INTERRUPT, GetBackEdgeState(isolate,
1686 unoptimized,
1687 back_edges.pc(i)));
machenbach@chromium.org8e36b5b2013-09-26 07:36:30 +00001688 PatchAt(unoptimized, back_edges.pc(i), ON_STACK_REPLACEMENT, patch);
machenbach@chromium.org528ce022013-09-23 14:09:36 +00001689 }
1690 }
1691
1692 unoptimized->set_back_edges_patched_for_osr(true);
1693 ASSERT(Verify(isolate, unoptimized, loop_nesting_level));
1694}
1695
1696
yangguo@chromium.org49546742013-12-23 16:17:49 +00001697void BackEdgeTable::Revert(Isolate* isolate, Code* unoptimized) {
machenbach@chromium.org528ce022013-09-23 14:09:36 +00001698 DisallowHeapAllocation no_gc;
machenbach@chromium.org8e36b5b2013-09-26 07:36:30 +00001699 Code* patch = isolate->builtins()->builtin(Builtins::kInterruptCheck);
machenbach@chromium.org528ce022013-09-23 14:09:36 +00001700
1701 // Iterate over the back edge table and revert the patched interrupt calls.
1702 ASSERT(unoptimized->back_edges_patched_for_osr());
1703 int loop_nesting_level = unoptimized->allow_osr_at_loop_nesting_level();
1704
1705 BackEdgeTable back_edges(unoptimized, &no_gc);
1706 for (uint32_t i = 0; i < back_edges.length(); i++) {
1707 if (static_cast<int>(back_edges.loop_depth(i)) <= loop_nesting_level) {
machenbach@chromium.org8e36b5b2013-09-26 07:36:30 +00001708 ASSERT_NE(INTERRUPT, GetBackEdgeState(isolate,
1709 unoptimized,
1710 back_edges.pc(i)));
1711 PatchAt(unoptimized, back_edges.pc(i), INTERRUPT, patch);
machenbach@chromium.org528ce022013-09-23 14:09:36 +00001712 }
1713 }
1714
1715 unoptimized->set_back_edges_patched_for_osr(false);
1716 unoptimized->set_allow_osr_at_loop_nesting_level(0);
1717 // Assert that none of the back edges are patched anymore.
1718 ASSERT(Verify(isolate, unoptimized, -1));
1719}
1720
1721
yangguo@chromium.org49546742013-12-23 16:17:49 +00001722void BackEdgeTable::AddStackCheck(Handle<Code> code, uint32_t pc_offset) {
machenbach@chromium.org8e36b5b2013-09-26 07:36:30 +00001723 DisallowHeapAllocation no_gc;
yangguo@chromium.org49546742013-12-23 16:17:49 +00001724 Isolate* isolate = code->GetIsolate();
1725 Address pc = code->instruction_start() + pc_offset;
machenbach@chromium.org8e36b5b2013-09-26 07:36:30 +00001726 Code* patch = isolate->builtins()->builtin(Builtins::kOsrAfterStackCheck);
yangguo@chromium.org49546742013-12-23 16:17:49 +00001727 PatchAt(*code, pc, OSR_AFTER_STACK_CHECK, patch);
machenbach@chromium.org8e36b5b2013-09-26 07:36:30 +00001728}
1729
1730
yangguo@chromium.org49546742013-12-23 16:17:49 +00001731void BackEdgeTable::RemoveStackCheck(Handle<Code> code, uint32_t pc_offset) {
machenbach@chromium.org8e36b5b2013-09-26 07:36:30 +00001732 DisallowHeapAllocation no_gc;
yangguo@chromium.org49546742013-12-23 16:17:49 +00001733 Isolate* isolate = code->GetIsolate();
1734 Address pc = code->instruction_start() + pc_offset;
1735
1736 if (OSR_AFTER_STACK_CHECK == GetBackEdgeState(isolate, *code, pc)) {
machenbach@chromium.org8e36b5b2013-09-26 07:36:30 +00001737 Code* patch = isolate->builtins()->builtin(Builtins::kOnStackReplacement);
yangguo@chromium.org49546742013-12-23 16:17:49 +00001738 PatchAt(*code, pc, ON_STACK_REPLACEMENT, patch);
machenbach@chromium.org8e36b5b2013-09-26 07:36:30 +00001739 }
1740}
1741
1742
machenbach@chromium.org528ce022013-09-23 14:09:36 +00001743#ifdef DEBUG
1744bool BackEdgeTable::Verify(Isolate* isolate,
1745 Code* unoptimized,
1746 int loop_nesting_level) {
1747 DisallowHeapAllocation no_gc;
1748 BackEdgeTable back_edges(unoptimized, &no_gc);
1749 for (uint32_t i = 0; i < back_edges.length(); i++) {
1750 uint32_t loop_depth = back_edges.loop_depth(i);
1751 CHECK_LE(static_cast<int>(loop_depth), Code::kMaxLoopNestingMarker);
1752 // Assert that all back edges for shallower loops (and only those)
1753 // have already been patched.
1754 CHECK_EQ((static_cast<int>(loop_depth) <= loop_nesting_level),
1755 GetBackEdgeState(isolate,
1756 unoptimized,
1757 back_edges.pc(i)) != INTERRUPT);
1758 }
1759 return true;
1760}
1761#endif // DEBUG
1762
1763
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001764#undef __
1765
1766
1767} } // namespace v8::internal