blob: f87cf88ce019b3534da4a4ab4ca9692150e3a9ab [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();
ager@chromium.org5c838252010-02-19 08:53:10 +0000315 Handle<Script> script = info->script();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000316 if (!script->IsUndefined() && !script->source()->IsUndefined()) {
317 int len = String::cast(script->source())->length();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000318 isolate->counters()->total_full_codegen_source_size()->Increment(len);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000319 }
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +0000320 CodeGenerator::MakeCodePrologue(info, "full");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000321 const int kInitialBufferSize = 4 * KB;
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000322 MacroAssembler masm(info->isolate(), NULL, kInitialBufferSize);
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000323#ifdef ENABLE_GDB_JIT_INTERFACE
324 masm.positions_recorder()->StartGDBJITLineInfoRecording();
325#endif
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000326 LOG_CODE_EVENT(isolate,
327 CodeStartLinePosInfoRecordEvent(masm.positions_recorder()));
ager@chromium.org5c838252010-02-19 08:53:10 +0000328
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000329 FullCodeGenerator cgen(&masm, info);
yangguo@chromium.org56454712012-02-16 15:33:53 +0000330 cgen.Generate();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000331 if (cgen.HasStackOverflow()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000332 ASSERT(!isolate->has_pending_exception());
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000333 return false;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000334 }
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000335 unsigned table_offset = cgen.EmitBackEdgeTable();
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000336
lrn@chromium.org34e60782011-09-15 07:25:40 +0000337 Code::Flags flags = Code::ComputeFlags(Code::FUNCTION);
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000338 Handle<Code> code = CodeGenerator::MakeCodeEpilogue(&masm, flags, info);
rossberg@chromium.org2c067b12012-03-19 11:01:52 +0000339 code->set_optimizable(info->IsOptimizable() &&
jkummerow@chromium.org2c9426b2013-09-05 16:31:13 +0000340 !info->function()->dont_optimize() &&
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000341 info->function()->scope()->AllowsLazyCompilation());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000342 cgen.PopulateDeoptimizationData(code);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000343 cgen.PopulateTypeFeedbackInfo(code);
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000344 cgen.PopulateTypeFeedbackCells(code);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000345 code->set_has_deoptimization_support(info->HasDeoptimizationSupport());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +0000346 code->set_handler_table(*cgen.handler_table());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000347#ifdef ENABLE_DEBUGGER_SUPPORT
lrn@chromium.org34e60782011-09-15 07:25:40 +0000348 code->set_has_debug_break_slots(
349 info->isolate()->debugger()->IsDebuggerActive());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000350 code->set_compiled_optimizable(info->IsOptimizable());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000351#endif // ENABLE_DEBUGGER_SUPPORT
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000352 code->set_allow_osr_at_loop_nesting_level(0);
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000353 code->set_profiler_ticks(0);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000354 code->set_back_edge_table_offset(table_offset);
355 code->set_back_edges_patched_for_osr(false);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000356 CodeGenerator::PrintCode(code, info);
rossberg@chromium.orgebeba022013-08-19 09:36:44 +0000357 info->SetCode(code);
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000358#ifdef ENABLE_GDB_JIT_INTERFACE
rossberg@chromium.orgebeba022013-08-19 09:36:44 +0000359 if (FLAG_gdbjit) {
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000360 GDBJITLineInfo* lineinfo =
361 masm.positions_recorder()->DetachGDBJITLineInfo();
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000362 GDBJIT(RegisterDetailedLineInfo(*code, lineinfo));
363 }
364#endif
rossberg@chromium.orgebeba022013-08-19 09:36:44 +0000365 void* line_info = masm.positions_recorder()->DetachJITHandlerData();
366 LOG_CODE_EVENT(isolate, CodeEndLinePosInfoRecordEvent(*code, line_info));
367 return true;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000368}
369
370
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000371unsigned FullCodeGenerator::EmitBackEdgeTable() {
372 // The back edge table consists of a length (in number of entries)
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000373 // field, and then a sequence of entries. Each entry is a pair of AST id
374 // and code-relative pc offset.
375 masm()->Align(kIntSize);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000376 unsigned offset = masm()->pc_offset();
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000377 unsigned length = back_edges_.length();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000378 __ dd(length);
379 for (unsigned i = 0; i < length; ++i) {
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000380 __ dd(back_edges_[i].id.ToInt());
381 __ dd(back_edges_[i].pc);
jkummerow@chromium.orgba72ec82013-07-22 09:21:20 +0000382 __ dd(back_edges_[i].loop_depth);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000383 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000384 return offset;
385}
386
387
388void FullCodeGenerator::PopulateDeoptimizationData(Handle<Code> code) {
389 // Fill in the deoptimization information.
390 ASSERT(info_->HasDeoptimizationSupport() || bailout_entries_.is_empty());
391 if (!info_->HasDeoptimizationSupport()) return;
392 int length = bailout_entries_.length();
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000393 Handle<DeoptimizationOutputData> data = isolate()->factory()->
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000394 NewDeoptimizationOutputData(length, TENURED);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000395 for (int i = 0; i < length; i++) {
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000396 data->SetAstId(i, bailout_entries_[i].id);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000397 data->SetPcAndState(i, Smi::FromInt(bailout_entries_[i].pc_and_state));
398 }
399 code->set_deoptimization_data(*data);
400}
401
402
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000403void FullCodeGenerator::PopulateTypeFeedbackInfo(Handle<Code> code) {
404 Handle<TypeFeedbackInfo> info = isolate()->factory()->NewTypeFeedbackInfo();
405 info->set_ic_total_count(ic_total_count_);
yangguo@chromium.orga7d3df92012-02-27 11:46:55 +0000406 ASSERT(!isolate()->heap()->InNewSpace(*info));
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000407 code->set_type_feedback_info(*info);
408}
409
410
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +0000411void FullCodeGenerator::Initialize() {
412 // The generation of debug code must match between the snapshot code and the
413 // code that is generated later. This is assumed by the debugger when it is
414 // calculating PC offsets after generating a debug version of code. Therefore
415 // we disable the production of debug code in the full compiler if we are
416 // either generating a snapshot or we booted from a snapshot.
417 generate_debug_code_ = FLAG_debug_code &&
418 !Serializer::enabled() &&
419 !Snapshot::HaveASnapshotToStartFrom();
420 masm_->set_emit_debug_code(generate_debug_code_);
421 masm_->set_predictable_code_size(true);
jkummerow@chromium.org8fa5bd92013-09-02 11:45:09 +0000422 InitializeAstVisitor(info_->isolate());
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +0000423}
424
425
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000426void FullCodeGenerator::PopulateTypeFeedbackCells(Handle<Code> code) {
427 if (type_feedback_cells_.is_empty()) return;
428 int length = type_feedback_cells_.length();
429 int array_size = TypeFeedbackCells::LengthOfFixedArray(length);
430 Handle<TypeFeedbackCells> cache = Handle<TypeFeedbackCells>::cast(
431 isolate()->factory()->NewFixedArray(array_size, TENURED));
432 for (int i = 0; i < length; i++) {
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000433 cache->SetAstId(i, type_feedback_cells_[i].ast_id);
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000434 cache->SetCell(i, *type_feedback_cells_[i].cell);
435 }
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000436 TypeFeedbackInfo::cast(code->type_feedback_info())->set_type_feedback_cells(
437 *cache);
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000438}
439
440
441
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000442void FullCodeGenerator::PrepareForBailout(Expression* node, State state) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000443 PrepareForBailoutForId(node->id(), state);
444}
445
446
447void FullCodeGenerator::RecordJSReturnSite(Call* call) {
448 // We record the offset of the function return so we can rebuild the frame
449 // if the function was inlined, i.e., this is the return address in the
450 // inlined function's frame.
451 //
452 // The state is ignored. We defensively set it to TOS_REG, which is the
453 // real state of the unoptimized code at the return site.
454 PrepareForBailoutForId(call->ReturnId(), TOS_REG);
455#ifdef DEBUG
456 // In debug builds, mark the return so we can verify that this function
457 // was called.
458 ASSERT(!call->return_is_recorded_);
459 call->return_is_recorded_ = true;
460#endif
461}
462
463
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000464void FullCodeGenerator::PrepareForBailoutForId(BailoutId id, State state) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000465 // There's no need to prepare this code for bailouts from already optimized
466 // code or code that can't be optimized.
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000467 if (!info_->HasDeoptimizationSupport()) return;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000468 unsigned pc_and_state =
469 StateField::encode(state) | PcField::encode(masm_->pc_offset());
yangguo@chromium.org56454712012-02-16 15:33:53 +0000470 ASSERT(Smi::IsValid(pc_and_state));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000471 BailoutEntry entry = { id, pc_and_state };
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000472 ASSERT(!prepared_bailout_ids_.Contains(id.ToInt()));
473 prepared_bailout_ids_.Add(id.ToInt(), zone());
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000474 bailout_entries_.Add(entry, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000475}
476
477
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000478void FullCodeGenerator::RecordTypeFeedbackCell(
danno@chromium.org41728482013-06-12 22:31:22 +0000479 TypeFeedbackId id, Handle<Cell> cell) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000480 TypeFeedbackCellEntry entry = { id, cell };
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000481 type_feedback_cells_.Add(entry, zone());
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000482}
483
484
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +0000485void FullCodeGenerator::RecordBackEdge(BailoutId ast_id) {
486 // The pc offset does not need to be encoded and packed together with a state.
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +0000487 ASSERT(masm_->pc_offset() > 0);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000488 ASSERT(loop_depth() > 0);
489 uint8_t depth = Min(loop_depth(), Code::kMaxLoopNestingMarker);
490 BackEdgeEntry entry =
491 { ast_id, static_cast<unsigned>(masm_->pc_offset()), depth };
492 back_edges_.Add(entry, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000493}
494
495
ricow@chromium.org65fae842010-08-25 15:26:24 +0000496bool FullCodeGenerator::ShouldInlineSmiCase(Token::Value op) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000497 // Inline smi case inside loops, but not division and modulo which
498 // are too complicated and take up too much space.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000499 if (op == Token::DIV ||op == Token::MOD) return false;
500 if (FLAG_always_inline_smi_code) return true;
501 return loop_depth_ > 0;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000502}
503
504
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000505void FullCodeGenerator::EffectContext::Plug(Register reg) const {
506}
507
508
509void FullCodeGenerator::AccumulatorValueContext::Plug(Register reg) const {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000510 __ Move(result_register(), reg);
511}
512
513
514void FullCodeGenerator::StackValueContext::Plug(Register reg) const {
danno@chromium.org59400602013-08-13 17:09:37 +0000515 __ Push(reg);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000516}
517
518
519void FullCodeGenerator::TestContext::Plug(Register reg) const {
520 // For simplicity we always test the accumulator register.
521 __ Move(result_register(), reg);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000522 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000523 codegen()->DoTest(this);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000524}
525
526
527void FullCodeGenerator::EffectContext::PlugTOS() const {
528 __ Drop(1);
529}
530
531
532void FullCodeGenerator::AccumulatorValueContext::PlugTOS() const {
danno@chromium.org59400602013-08-13 17:09:37 +0000533 __ Pop(result_register());
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000534}
535
536
537void FullCodeGenerator::StackValueContext::PlugTOS() const {
538}
539
540
541void FullCodeGenerator::TestContext::PlugTOS() const {
542 // For simplicity we always test the accumulator register.
danno@chromium.org59400602013-08-13 17:09:37 +0000543 __ Pop(result_register());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000544 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000545 codegen()->DoTest(this);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000546}
547
548
549void FullCodeGenerator::EffectContext::PrepareTest(
550 Label* materialize_true,
551 Label* materialize_false,
552 Label** if_true,
553 Label** if_false,
554 Label** fall_through) const {
555 // In an effect context, the true and the false case branch to the
556 // same label.
557 *if_true = *if_false = *fall_through = materialize_true;
558}
559
560
561void FullCodeGenerator::AccumulatorValueContext::PrepareTest(
562 Label* materialize_true,
563 Label* materialize_false,
564 Label** if_true,
565 Label** if_false,
566 Label** fall_through) const {
567 *if_true = *fall_through = materialize_true;
568 *if_false = materialize_false;
569}
570
571
572void FullCodeGenerator::StackValueContext::PrepareTest(
573 Label* materialize_true,
574 Label* materialize_false,
575 Label** if_true,
576 Label** if_false,
577 Label** fall_through) const {
578 *if_true = *fall_through = materialize_true;
579 *if_false = materialize_false;
580}
581
582
583void FullCodeGenerator::TestContext::PrepareTest(
584 Label* materialize_true,
585 Label* materialize_false,
586 Label** if_true,
587 Label** if_false,
588 Label** fall_through) const {
589 *if_true = true_label_;
590 *if_false = false_label_;
591 *fall_through = fall_through_;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000592}
593
594
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000595void FullCodeGenerator::DoTest(const TestContext* context) {
596 DoTest(context->condition(),
597 context->true_label(),
598 context->false_label(),
599 context->fall_through());
600}
601
602
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000603void FullCodeGenerator::AllocateModules(ZoneList<Declaration*>* declarations) {
604 ASSERT(scope_->is_global_scope());
605
606 for (int i = 0; i < declarations->length(); i++) {
607 ModuleDeclaration* declaration = declarations->at(i)->AsModuleDeclaration();
608 if (declaration != NULL) {
609 ModuleLiteral* module = declaration->module()->AsModuleLiteral();
610 if (module != NULL) {
611 Comment cmnt(masm_, "[ Link nested modules");
612 Scope* scope = module->body()->scope();
613 Interface* interface = scope->interface();
614 ASSERT(interface->IsModule() && interface->IsFrozen());
615
616 interface->Allocate(scope->module_var()->index());
617
618 // Set up module context.
619 ASSERT(scope->interface()->Index() >= 0);
620 __ Push(Smi::FromInt(scope->interface()->Index()));
621 __ Push(scope->GetScopeInfo());
622 __ CallRuntime(Runtime::kPushModuleContext, 2);
623 StoreToFrameField(StandardFrameConstants::kContextOffset,
624 context_register());
625
626 AllocateModules(scope->declarations());
627
628 // Pop module context.
629 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
630 // Update local stack frame context field.
631 StoreToFrameField(StandardFrameConstants::kContextOffset,
632 context_register());
633 }
634 }
635 }
636}
637
638
639// Modules have their own local scope, represented by their own context.
640// Module instance objects have an accessor for every export that forwards
641// access to the respective slot from the module's context. (Exports that are
642// modules themselves, however, are simple data properties.)
643//
644// All modules have a _hosting_ scope/context, which (currently) is the
645// (innermost) enclosing global scope. To deal with recursion, nested modules
646// are hosted by the same scope as global ones.
647//
648// For every (global or nested) module literal, the hosting context has an
649// internal slot that points directly to the respective module context. This
650// enables quick access to (statically resolved) module members by 2-dimensional
651// access through the hosting context. For example,
652//
653// module A {
654// let x;
655// module B { let y; }
656// }
657// module C { let z; }
658//
659// allocates contexts as follows:
660//
661// [header| .A | .B | .C | A | C ] (global)
662// | | |
663// | | +-- [header| z ] (module)
664// | |
665// | +------- [header| y ] (module)
666// |
667// +------------ [header| x | B ] (module)
668//
669// Here, .A, .B, .C are the internal slots pointing to the hosted module
670// contexts, whereas A, B, C hold the actual instance objects (note that every
671// module context also points to the respective instance object through its
672// extension slot in the header).
673//
674// To deal with arbitrary recursion and aliases between modules,
675// they are created and initialized in several stages. Each stage applies to
676// all modules in the hosting global scope, including nested ones.
677//
678// 1. Allocate: for each module _literal_, allocate the module contexts and
679// respective instance object and wire them up. This happens in the
680// PushModuleContext runtime function, as generated by AllocateModules
681// (invoked by VisitDeclarations in the hosting scope).
682//
683// 2. Bind: for each module _declaration_ (i.e. literals as well as aliases),
684// assign the respective instance object to respective local variables. This
685// happens in VisitModuleDeclaration, and uses the instance objects created
686// in the previous stage.
687// For each module _literal_, this phase also constructs a module descriptor
688// for the next stage. This happens in VisitModuleLiteral.
689//
690// 3. Populate: invoke the DeclareModules runtime function to populate each
691// _instance_ object with accessors for it exports. This is generated by
692// DeclareModules (invoked by VisitDeclarations in the hosting scope again),
693// and uses the descriptors generated in the previous stage.
694//
695// 4. Initialize: execute the module bodies (and other code) in sequence. This
696// happens by the separate statements generated for module bodies. To reenter
697// the module scopes properly, the parser inserted ModuleStatements.
698
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000699void FullCodeGenerator::VisitDeclarations(
700 ZoneList<Declaration*>* declarations) {
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000701 Handle<FixedArray> saved_modules = modules_;
702 int saved_module_index = module_index_;
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000703 ZoneList<Handle<Object> >* saved_globals = globals_;
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000704 ZoneList<Handle<Object> > inner_globals(10, zone());
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000705 globals_ = &inner_globals;
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000706
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000707 if (scope_->num_modules() != 0) {
708 // This is a scope hosting modules. Allocate a descriptor array to pass
709 // to the runtime for initialization.
710 Comment cmnt(masm_, "[ Allocate modules");
711 ASSERT(scope_->is_global_scope());
712 modules_ =
713 isolate()->factory()->NewFixedArray(scope_->num_modules(), TENURED);
714 module_index_ = 0;
715
716 // Generate code for allocating all modules, including nested ones.
717 // The allocated contexts are stored in internal variables in this scope.
718 AllocateModules(declarations);
719 }
720
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000721 AstVisitor::VisitDeclarations(declarations);
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000722
723 if (scope_->num_modules() != 0) {
724 // Initialize modules from descriptor array.
725 ASSERT(module_index_ == modules_->length());
726 DeclareModules(modules_);
727 modules_ = saved_modules;
728 module_index_ = saved_module_index;
729 }
730
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000731 if (!globals_->is_empty()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000732 // Invoke the platform-dependent code generator to do the actual
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000733 // declaration of the global functions and variables.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000734 Handle<FixedArray> array =
735 isolate()->factory()->NewFixedArray(globals_->length(), TENURED);
736 for (int i = 0; i < globals_->length(); ++i)
737 array->set(i, *globals_->at(i));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000738 DeclareGlobals(array);
739 }
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000740
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000741 globals_ = saved_globals;
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000742}
743
744
745void FullCodeGenerator::VisitModuleLiteral(ModuleLiteral* module) {
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000746 Block* block = module->body();
747 Scope* saved_scope = scope();
748 scope_ = block->scope();
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000749 Interface* interface = scope_->interface();
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000750
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000751 Comment cmnt(masm_, "[ ModuleLiteral");
752 SetStatementPosition(block);
753
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000754 ASSERT(!modules_.is_null());
755 ASSERT(module_index_ < modules_->length());
756 int index = module_index_++;
757
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000758 // Set up module context.
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000759 ASSERT(interface->Index() >= 0);
760 __ Push(Smi::FromInt(interface->Index()));
761 __ Push(Smi::FromInt(0));
762 __ CallRuntime(Runtime::kPushModuleContext, 2);
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000763 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000764
765 {
766 Comment cmnt(masm_, "[ Declarations");
767 VisitDeclarations(scope_->declarations());
768 }
769
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000770 // Populate the module description.
771 Handle<ModuleInfo> description =
772 ModuleInfo::Create(isolate(), interface, scope_);
773 modules_->set(index, *description);
774
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000775 scope_ = saved_scope;
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000776 // Pop module context.
777 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
778 // Update local stack frame context field.
779 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000780}
781
782
783void FullCodeGenerator::VisitModuleVariable(ModuleVariable* module) {
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000784 // Nothing to do.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000785 // The instance object is resolved statically through the module's interface.
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000786}
787
788
789void FullCodeGenerator::VisitModulePath(ModulePath* module) {
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000790 // Nothing to do.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000791 // The instance object is resolved statically through the module's interface.
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000792}
793
794
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000795void FullCodeGenerator::VisitModuleUrl(ModuleUrl* module) {
796 // TODO(rossberg): dummy allocation for now.
797 Scope* scope = module->body()->scope();
798 Interface* interface = scope_->interface();
799
800 ASSERT(interface->IsModule() && interface->IsFrozen());
801 ASSERT(!modules_.is_null());
802 ASSERT(module_index_ < modules_->length());
803 interface->Allocate(scope->module_var()->index());
804 int index = module_index_++;
805
806 Handle<ModuleInfo> description =
807 ModuleInfo::Create(isolate(), interface, scope_);
808 modules_->set(index, *description);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000809}
810
811
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000812int FullCodeGenerator::DeclareGlobalsFlags() {
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000813 ASSERT(DeclareGlobalsLanguageMode::is_valid(language_mode()));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000814 return DeclareGlobalsEvalFlag::encode(is_eval()) |
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000815 DeclareGlobalsNativeFlag::encode(is_native()) |
816 DeclareGlobalsLanguageMode::encode(language_mode());
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000817}
818
819
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000820void FullCodeGenerator::SetFunctionPosition(FunctionLiteral* fun) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000821 CodeGenerator::RecordPositions(masm_, fun->start_position());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000822}
823
824
825void FullCodeGenerator::SetReturnPosition(FunctionLiteral* fun) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000826 CodeGenerator::RecordPositions(masm_, fun->end_position() - 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000827}
828
829
830void FullCodeGenerator::SetStatementPosition(Statement* stmt) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000831#ifdef ENABLE_DEBUGGER_SUPPORT
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000832 if (!isolate()->debugger()->IsDebuggerActive()) {
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +0000833 CodeGenerator::RecordPositions(masm_, stmt->position());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000834 } else {
835 // Check if the statement will be breakable without adding a debug break
836 // slot.
jkummerow@chromium.org8fa5bd92013-09-02 11:45:09 +0000837 BreakableStatementChecker checker(isolate());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000838 checker.Check(stmt);
839 // Record the statement position right here if the statement is not
840 // breakable. For breakable statements the actual recording of the
841 // position will be postponed to the breakable code (typically an IC).
842 bool position_recorded = CodeGenerator::RecordPositions(
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +0000843 masm_, stmt->position(), !checker.is_breakable());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000844 // If the position recording did record a new position generate a debug
845 // break slot to make the statement breakable.
846 if (position_recorded) {
847 Debug::GenerateSlot(masm_);
848 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000849 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000850#else
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +0000851 CodeGenerator::RecordPositions(masm_, stmt->position());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000852#endif
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000853}
854
855
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +0000856void FullCodeGenerator::SetExpressionPosition(Expression* expr) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000857#ifdef ENABLE_DEBUGGER_SUPPORT
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000858 if (!isolate()->debugger()->IsDebuggerActive()) {
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +0000859 CodeGenerator::RecordPositions(masm_, expr->position());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000860 } else {
861 // Check if the expression will be breakable without adding a debug break
862 // slot.
jkummerow@chromium.org8fa5bd92013-09-02 11:45:09 +0000863 BreakableStatementChecker checker(isolate());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000864 checker.Check(expr);
865 // Record a statement position right here if the expression is not
866 // breakable. For breakable expressions the actual recording of the
867 // position will be postponed to the breakable code (typically an IC).
868 // NOTE this will record a statement position for something which might
869 // not be a statement. As stepping in the debugger will only stop at
870 // statement positions this is used for e.g. the condition expression of
871 // a do while loop.
872 bool position_recorded = CodeGenerator::RecordPositions(
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +0000873 masm_, expr->position(), !checker.is_breakable());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000874 // If the position recording did record a new position generate a debug
875 // break slot to make the statement breakable.
876 if (position_recorded) {
877 Debug::GenerateSlot(masm_);
878 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000879 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000880#else
881 CodeGenerator::RecordPositions(masm_, pos);
882#endif
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000883}
884
885
886void FullCodeGenerator::SetStatementPosition(int pos) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000887 CodeGenerator::RecordPositions(masm_, pos);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000888}
889
890
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000891void FullCodeGenerator::SetSourcePosition(int pos) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000892 if (pos != RelocInfo::kNoPosition) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000893 masm_->positions_recorder()->RecordPosition(pos);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000894 }
895}
896
897
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000898// Lookup table for code generators for special runtime calls which are
899// generated inline.
900#define INLINE_FUNCTION_GENERATOR_ADDRESS(Name, argc, ressize) \
901 &FullCodeGenerator::Emit##Name,
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000902
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000903const FullCodeGenerator::InlineFunctionGenerator
904 FullCodeGenerator::kInlineFunctionGenerators[] = {
905 INLINE_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS)
906 INLINE_RUNTIME_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS)
907 };
908#undef INLINE_FUNCTION_GENERATOR_ADDRESS
909
910
911FullCodeGenerator::InlineFunctionGenerator
912 FullCodeGenerator::FindInlineFunctionGenerator(Runtime::FunctionId id) {
whesse@chromium.org023421e2010-12-21 12:19:12 +0000913 int lookup_index =
914 static_cast<int>(id) - static_cast<int>(Runtime::kFirstInlineFunction);
915 ASSERT(lookup_index >= 0);
916 ASSERT(static_cast<size_t>(lookup_index) <
917 ARRAY_SIZE(kInlineFunctionGenerators));
918 return kInlineFunctionGenerators[lookup_index];
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000919}
920
921
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000922void FullCodeGenerator::EmitInlineRuntimeCall(CallRuntime* expr) {
923 const Runtime::Function* function = expr->function();
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000924 ASSERT(function != NULL);
925 ASSERT(function->intrinsic_type == Runtime::INLINE);
926 InlineFunctionGenerator generator =
927 FindInlineFunctionGenerator(function->function_id);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000928 ((*this).*(generator))(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000929}
930
931
verwaest@chromium.org8a00e822013-06-10 15:11:22 +0000932void FullCodeGenerator::EmitGeneratorNext(CallRuntime* expr) {
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000933 ZoneList<Expression*>* args = expr->arguments();
934 ASSERT(args->length() == 2);
verwaest@chromium.org8a00e822013-06-10 15:11:22 +0000935 EmitGeneratorResume(args->at(0), args->at(1), JSGeneratorObject::NEXT);
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000936}
937
938
939void FullCodeGenerator::EmitGeneratorThrow(CallRuntime* expr) {
940 ZoneList<Expression*>* args = expr->arguments();
941 ASSERT(args->length() == 2);
942 EmitGeneratorResume(args->at(0), args->at(1), JSGeneratorObject::THROW);
943}
944
945
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +0000946void FullCodeGenerator::EmitDebugBreakInOptimizedCode(CallRuntime* expr) {
947 context()->Plug(handle(Smi::FromInt(0), isolate()));
948}
949
950
ricow@chromium.org65fae842010-08-25 15:26:24 +0000951void FullCodeGenerator::VisitBinaryOperation(BinaryOperation* expr) {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000952 switch (expr->op()) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000953 case Token::COMMA:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000954 return VisitComma(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000955 case Token::OR:
956 case Token::AND:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000957 return VisitLogicalExpression(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000958 default:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000959 return VisitArithmeticExpression(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000960 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000961}
962
963
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000964void FullCodeGenerator::VisitInDuplicateContext(Expression* expr) {
965 if (context()->IsEffect()) {
966 VisitForEffect(expr);
967 } else if (context()->IsAccumulatorValue()) {
968 VisitForAccumulatorValue(expr);
969 } else if (context()->IsStackValue()) {
970 VisitForStackValue(expr);
971 } else if (context()->IsTest()) {
972 const TestContext* test = TestContext::cast(context());
973 VisitForControl(expr, test->true_label(), test->false_label(),
974 test->fall_through());
975 }
976}
977
978
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000979void FullCodeGenerator::VisitComma(BinaryOperation* expr) {
980 Comment cmnt(masm_, "[ Comma");
981 VisitForEffect(expr->left());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000982 VisitInDuplicateContext(expr->right());
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000983}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000984
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000985
986void FullCodeGenerator::VisitLogicalExpression(BinaryOperation* expr) {
987 bool is_logical_and = expr->op() == Token::AND;
988 Comment cmnt(masm_, is_logical_and ? "[ Logical AND" : "[ Logical OR");
989 Expression* left = expr->left();
990 Expression* right = expr->right();
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000991 BailoutId right_id = expr->RightId();
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000992 Label done;
993
994 if (context()->IsTest()) {
995 Label eval_right;
996 const TestContext* test = TestContext::cast(context());
997 if (is_logical_and) {
998 VisitForControl(left, &eval_right, test->false_label(), &eval_right);
999 } else {
1000 VisitForControl(left, test->true_label(), &eval_right, &eval_right);
1001 }
1002 PrepareForBailoutForId(right_id, NO_REGISTERS);
1003 __ bind(&eval_right);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001004
1005 } else if (context()->IsAccumulatorValue()) {
1006 VisitForAccumulatorValue(left);
1007 // We want the value in the accumulator for the test, and on the stack in
1008 // case we need it.
danno@chromium.org59400602013-08-13 17:09:37 +00001009 __ Push(result_register());
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001010 Label discard, restore;
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001011 if (is_logical_and) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001012 DoTest(left, &discard, &restore, &restore);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001013 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001014 DoTest(left, &restore, &discard, &restore);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001015 }
1016 __ bind(&restore);
danno@chromium.org59400602013-08-13 17:09:37 +00001017 __ Pop(result_register());
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001018 __ jmp(&done);
1019 __ bind(&discard);
1020 __ Drop(1);
1021 PrepareForBailoutForId(right_id, NO_REGISTERS);
1022
1023 } else if (context()->IsStackValue()) {
1024 VisitForAccumulatorValue(left);
1025 // We want the value in the accumulator for the test, and on the stack in
1026 // case we need it.
danno@chromium.org59400602013-08-13 17:09:37 +00001027 __ Push(result_register());
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001028 Label discard;
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001029 if (is_logical_and) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001030 DoTest(left, &discard, &done, &discard);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001031 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001032 DoTest(left, &done, &discard, &discard);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001033 }
1034 __ bind(&discard);
1035 __ Drop(1);
1036 PrepareForBailoutForId(right_id, NO_REGISTERS);
1037
1038 } else {
1039 ASSERT(context()->IsEffect());
1040 Label eval_right;
1041 if (is_logical_and) {
1042 VisitForControl(left, &eval_right, &done, &eval_right);
1043 } else {
1044 VisitForControl(left, &done, &eval_right, &eval_right);
1045 }
1046 PrepareForBailoutForId(right_id, NO_REGISTERS);
1047 __ bind(&eval_right);
1048 }
1049
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001050 VisitInDuplicateContext(right);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001051 __ bind(&done);
1052}
1053
1054
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001055void FullCodeGenerator::VisitArithmeticExpression(BinaryOperation* expr) {
1056 Token::Value op = expr->op();
1057 Comment cmnt(masm_, "[ ArithmeticExpression");
1058 Expression* left = expr->left();
1059 Expression* right = expr->right();
1060 OverwriteMode mode =
1061 left->ResultOverwriteAllowed()
1062 ? OVERWRITE_LEFT
1063 : (right->ResultOverwriteAllowed() ? OVERWRITE_RIGHT : NO_OVERWRITE);
1064
1065 VisitForStackValue(left);
1066 VisitForAccumulatorValue(right);
1067
1068 SetSourcePosition(expr->position());
1069 if (ShouldInlineSmiCase(op)) {
1070 EmitInlineSmiBinaryOp(expr, op, mode, left, right);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001071 } else {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001072 EmitBinaryOp(expr, op, mode);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001073 }
1074}
1075
1076
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001077void FullCodeGenerator::VisitBlock(Block* stmt) {
1078 Comment cmnt(masm_, "[ Block");
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001079 NestedBlock nested_block(this, stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001080 SetStatementPosition(stmt);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001081
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001082 Scope* saved_scope = scope();
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001083 // Push a block context when entering a block with block scoped variables.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00001084 if (stmt->scope() != NULL) {
danno@chromium.org81cac2b2012-07-10 11:28:27 +00001085 scope_ = stmt->scope();
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00001086 ASSERT(!scope_->is_module_scope());
1087 { Comment cmnt(masm_, "[ Extend block context");
1088 Handle<ScopeInfo> scope_info = scope_->GetScopeInfo();
1089 int heap_slots = scope_info->ContextLength() - Context::MIN_CONTEXT_SLOTS;
1090 __ Push(scope_info);
1091 PushFunctionArgumentForContextAllocation();
1092 if (heap_slots <= FastNewBlockContextStub::kMaximumSlots) {
1093 FastNewBlockContextStub stub(heap_slots);
1094 __ CallStub(&stub);
1095 } else {
1096 __ CallRuntime(Runtime::kPushBlockContext, 2);
1097 }
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001098
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00001099 // Replace the context stored in the frame.
1100 StoreToFrameField(StandardFrameConstants::kContextOffset,
1101 context_register());
1102 }
1103 { Comment cmnt(masm_, "[ Declarations");
1104 VisitDeclarations(scope_->declarations());
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001105 }
1106 }
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00001107
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00001108 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001109 VisitStatements(stmt->statements());
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001110 scope_ = saved_scope;
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001111 __ bind(nested_block.break_label());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001112 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001113
1114 // Pop block context if necessary.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00001115 if (stmt->scope() != NULL) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001116 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1117 // Update local stack frame context field.
1118 StoreToFrameField(StandardFrameConstants::kContextOffset,
1119 context_register());
1120 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001121}
1122
1123
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00001124void FullCodeGenerator::VisitModuleStatement(ModuleStatement* stmt) {
1125 Comment cmnt(masm_, "[ Module context");
1126
1127 __ Push(Smi::FromInt(stmt->proxy()->interface()->Index()));
1128 __ Push(Smi::FromInt(0));
1129 __ CallRuntime(Runtime::kPushModuleContext, 2);
1130 StoreToFrameField(
1131 StandardFrameConstants::kContextOffset, context_register());
1132
1133 Scope* saved_scope = scope_;
1134 scope_ = stmt->body()->scope();
1135 VisitStatements(stmt->body()->statements());
1136 scope_ = saved_scope;
1137 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1138 // Update local stack frame context field.
1139 StoreToFrameField(StandardFrameConstants::kContextOffset,
1140 context_register());
1141}
1142
1143
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001144void FullCodeGenerator::VisitExpressionStatement(ExpressionStatement* stmt) {
1145 Comment cmnt(masm_, "[ ExpressionStatement");
1146 SetStatementPosition(stmt);
1147 VisitForEffect(stmt->expression());
1148}
1149
1150
1151void FullCodeGenerator::VisitEmptyStatement(EmptyStatement* stmt) {
1152 Comment cmnt(masm_, "[ EmptyStatement");
1153 SetStatementPosition(stmt);
1154}
1155
1156
1157void FullCodeGenerator::VisitIfStatement(IfStatement* stmt) {
1158 Comment cmnt(masm_, "[ IfStatement");
1159 SetStatementPosition(stmt);
1160 Label then_part, else_part, done;
1161
ricow@chromium.org65fae842010-08-25 15:26:24 +00001162 if (stmt->HasElseStatement()) {
1163 VisitForControl(stmt->condition(), &then_part, &else_part, &then_part);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001164 PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001165 __ bind(&then_part);
1166 Visit(stmt->then_statement());
1167 __ jmp(&done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001168
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001169 PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001170 __ bind(&else_part);
1171 Visit(stmt->else_statement());
1172 } else {
1173 VisitForControl(stmt->condition(), &then_part, &done, &then_part);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001174 PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001175 __ bind(&then_part);
1176 Visit(stmt->then_statement());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001177
1178 PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001179 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001180 __ bind(&done);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001181 PrepareForBailoutForId(stmt->IfId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001182}
1183
1184
1185void FullCodeGenerator::VisitContinueStatement(ContinueStatement* stmt) {
1186 Comment cmnt(masm_, "[ ContinueStatement");
1187 SetStatementPosition(stmt);
1188 NestedStatement* current = nesting_stack_;
1189 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001190 int context_length = 0;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001191 // When continuing, we clobber the unpredictable value in the accumulator
1192 // with one that's safe for GC. If we hit an exit from the try block of
1193 // try...finally on our way out, we will unconditionally preserve the
1194 // accumulator on the stack.
1195 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001196 while (!current->IsContinueTarget(stmt->target())) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001197 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001198 }
1199 __ Drop(stack_depth);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001200 if (context_length > 0) {
1201 while (context_length > 0) {
1202 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1203 --context_length;
1204 }
1205 StoreToFrameField(StandardFrameConstants::kContextOffset,
1206 context_register());
1207 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001208
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001209 __ jmp(current->AsIteration()->continue_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001210}
1211
1212
1213void FullCodeGenerator::VisitBreakStatement(BreakStatement* stmt) {
1214 Comment cmnt(masm_, "[ BreakStatement");
1215 SetStatementPosition(stmt);
1216 NestedStatement* current = nesting_stack_;
1217 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001218 int context_length = 0;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001219 // When breaking, we clobber the unpredictable value in the accumulator
1220 // with one that's safe for GC. If we hit an exit from the try block of
1221 // try...finally on our way out, we will unconditionally preserve the
1222 // accumulator on the stack.
1223 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001224 while (!current->IsBreakTarget(stmt->target())) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001225 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001226 }
1227 __ Drop(stack_depth);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001228 if (context_length > 0) {
1229 while (context_length > 0) {
1230 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1231 --context_length;
1232 }
1233 StoreToFrameField(StandardFrameConstants::kContextOffset,
1234 context_register());
1235 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001236
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001237 __ jmp(current->AsBreakable()->break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001238}
1239
1240
danno@chromium.org41728482013-06-12 22:31:22 +00001241void FullCodeGenerator::EmitUnwindBeforeReturn() {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001242 NestedStatement* current = nesting_stack_;
1243 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001244 int context_length = 0;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001245 while (current != NULL) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001246 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001247 }
1248 __ Drop(stack_depth);
danno@chromium.org41728482013-06-12 22:31:22 +00001249}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001250
danno@chromium.org41728482013-06-12 22:31:22 +00001251
1252void FullCodeGenerator::VisitReturnStatement(ReturnStatement* stmt) {
1253 Comment cmnt(masm_, "[ ReturnStatement");
1254 SetStatementPosition(stmt);
1255 Expression* expr = stmt->expression();
1256 VisitForAccumulatorValue(expr);
1257 EmitUnwindBeforeReturn();
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001258 EmitReturnSequence();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001259}
1260
1261
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001262void FullCodeGenerator::VisitWithStatement(WithStatement* stmt) {
1263 Comment cmnt(masm_, "[ WithStatement");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001264 SetStatementPosition(stmt);
1265
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001266 VisitForStackValue(stmt->expression());
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00001267 PushFunctionArgumentForContextAllocation();
1268 __ CallRuntime(Runtime::kPushWithContext, 2);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001269 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001270
danno@chromium.orgca29dd82013-04-26 11:59:48 +00001271 Scope* saved_scope = scope();
1272 scope_ = stmt->scope();
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001273 { WithOrCatch body(this);
1274 Visit(stmt->statement());
1275 }
danno@chromium.orgca29dd82013-04-26 11:59:48 +00001276 scope_ = saved_scope;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001277
1278 // Pop context.
1279 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1280 // Update local stack frame context field.
1281 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001282}
1283
1284
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001285void FullCodeGenerator::VisitDoWhileStatement(DoWhileStatement* stmt) {
1286 Comment cmnt(masm_, "[ DoWhileStatement");
1287 SetStatementPosition(stmt);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001288 Label body, book_keeping;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001289
1290 Iteration loop_statement(this, stmt);
1291 increment_loop_depth();
1292
1293 __ bind(&body);
1294 Visit(stmt->body());
1295
ricow@chromium.org65fae842010-08-25 15:26:24 +00001296 // Record the position of the do while condition and make sure it is
1297 // possible to break on the condition.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001298 __ bind(loop_statement.continue_label());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001299 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +00001300 SetExpressionPosition(stmt->cond());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001301 VisitForControl(stmt->cond(),
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001302 &book_keeping,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001303 loop_statement.break_label(),
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001304 &book_keeping);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001305
1306 // Check stack before looping.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001307 PrepareForBailoutForId(stmt->BackEdgeId(), NO_REGISTERS);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001308 __ bind(&book_keeping);
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +00001309 EmitBackEdgeBookkeeping(stmt, &body);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001310 __ jmp(&body);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001311
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001312 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001313 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001314 decrement_loop_depth();
1315}
1316
1317
1318void FullCodeGenerator::VisitWhileStatement(WhileStatement* stmt) {
1319 Comment cmnt(masm_, "[ WhileStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001320 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001321
1322 Iteration loop_statement(this, stmt);
1323 increment_loop_depth();
1324
1325 // Emit the test at the bottom of the loop.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001326 __ jmp(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001327
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001328 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001329 __ bind(&body);
1330 Visit(stmt->body());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001331
1332 // Emit the statement position here as this is where the while
1333 // statement code starts.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001334 __ bind(loop_statement.continue_label());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001335 SetStatementPosition(stmt);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001336
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001337 // Check stack before looping.
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +00001338 EmitBackEdgeBookkeeping(stmt, &body);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001339
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001340 __ bind(&test);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001341 VisitForControl(stmt->cond(),
1342 &body,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001343 loop_statement.break_label(),
1344 loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001345
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001346 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001347 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001348 decrement_loop_depth();
1349}
1350
1351
1352void FullCodeGenerator::VisitForStatement(ForStatement* stmt) {
1353 Comment cmnt(masm_, "[ ForStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001354 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001355
1356 Iteration loop_statement(this, stmt);
erik.corry@gmail.combbceb572012-03-09 10:52:05 +00001357
1358 // Set statement position for a break slot before entering the for-body.
1359 SetStatementPosition(stmt);
1360
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001361 if (stmt->init() != NULL) {
1362 Visit(stmt->init());
1363 }
1364
1365 increment_loop_depth();
1366 // Emit the test at the bottom of the loop (even if empty).
1367 __ jmp(&test);
1368
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001369 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001370 __ bind(&body);
1371 Visit(stmt->body());
1372
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001373 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001374 __ bind(loop_statement.continue_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001375 if (stmt->next() != NULL) {
1376 Visit(stmt->next());
1377 }
1378
ricow@chromium.org65fae842010-08-25 15:26:24 +00001379 // Emit the statement position here as this is where the for
1380 // statement code starts.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001381 SetStatementPosition(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001382
1383 // Check stack before looping.
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +00001384 EmitBackEdgeBookkeeping(stmt, &body);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001385
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001386 __ bind(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001387 if (stmt->cond() != NULL) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00001388 VisitForControl(stmt->cond(),
1389 &body,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001390 loop_statement.break_label(),
1391 loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001392 } else {
1393 __ jmp(&body);
1394 }
1395
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001396 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001397 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001398 decrement_loop_depth();
1399}
1400
1401
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001402void FullCodeGenerator::VisitTryCatchStatement(TryCatchStatement* stmt) {
1403 Comment cmnt(masm_, "[ TryCatchStatement");
1404 SetStatementPosition(stmt);
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001405 // The try block adds a handler to the exception handler chain before
1406 // entering, and removes it again when exiting normally. If an exception
1407 // is thrown during execution of the try block, the handler is consumed
1408 // and control is passed to the catch block with the exception in the
1409 // result register.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001410
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001411 Label try_entry, handler_entry, exit;
1412 __ jmp(&try_entry);
1413 __ bind(&handler_entry);
1414 handler_table()->set(stmt->index(), Smi::FromInt(handler_entry.pos()));
1415 // Exception handler code, the exception is in the result register.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001416 // Extend the context before executing the catch block.
1417 { Comment cmnt(masm_, "[ Extend catch context");
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001418 __ Push(stmt->variable()->name());
danno@chromium.org59400602013-08-13 17:09:37 +00001419 __ Push(result_register());
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00001420 PushFunctionArgumentForContextAllocation();
1421 __ CallRuntime(Runtime::kPushCatchContext, 3);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001422 StoreToFrameField(StandardFrameConstants::kContextOffset,
1423 context_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001424 }
1425
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001426 Scope* saved_scope = scope();
1427 scope_ = stmt->scope();
1428 ASSERT(scope_->declarations()->is_empty());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001429 { WithOrCatch catch_body(this);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001430 Visit(stmt->catch_block());
1431 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001432 // Restore the context.
1433 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1434 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001435 scope_ = saved_scope;
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001436 __ jmp(&exit);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001437
1438 // Try block code. Sets up the exception handler chain.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001439 __ bind(&try_entry);
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00001440 __ PushTryHandler(StackHandler::CATCH, stmt->index());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001441 { TryCatch try_body(this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001442 Visit(stmt->try_block());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001443 }
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001444 __ PopTryHandler();
1445 __ bind(&exit);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001446}
1447
1448
1449void FullCodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* stmt) {
1450 Comment cmnt(masm_, "[ TryFinallyStatement");
1451 SetStatementPosition(stmt);
1452 // Try finally is compiled by setting up a try-handler on the stack while
1453 // executing the try body, and removing it again afterwards.
1454 //
1455 // The try-finally construct can enter the finally block in three ways:
1456 // 1. By exiting the try-block normally. This removes the try-handler and
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001457 // calls the finally block code before continuing.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001458 // 2. By exiting the try-block with a function-local control flow transfer
1459 // (break/continue/return). The site of the, e.g., break removes the
1460 // try handler and calls the finally block code before continuing
1461 // its outward control transfer.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001462 // 3. By exiting the try-block with a thrown exception.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001463 // This can happen in nested function calls. It traverses the try-handler
1464 // chain and consumes the try-handler entry before jumping to the
1465 // handler code. The handler code then calls the finally-block before
1466 // rethrowing the exception.
1467 //
1468 // The finally block must assume a return address on top of the stack
1469 // (or in the link register on ARM chips) and a value (return value or
1470 // exception) in the result register (rax/eax/r0), both of which must
1471 // be preserved. The return address isn't GC-safe, so it should be
1472 // cooked before GC.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001473 Label try_entry, handler_entry, finally_entry;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001474
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001475 // Jump to try-handler setup and try-block code.
1476 __ jmp(&try_entry);
1477 __ bind(&handler_entry);
1478 handler_table()->set(stmt->index(), Smi::FromInt(handler_entry.pos()));
1479 // Exception handler code. This code is only executed when an exception
1480 // is thrown. The exception is in the result register, and must be
1481 // preserved by the finally block. Call the finally block and then
1482 // rethrow the exception if it returns.
1483 __ Call(&finally_entry);
danno@chromium.org59400602013-08-13 17:09:37 +00001484 __ Push(result_register());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001485 __ CallRuntime(Runtime::kReThrow, 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001486
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001487 // Finally block implementation.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001488 __ bind(&finally_entry);
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001489 EnterFinallyBlock();
1490 { Finally finally_body(this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001491 Visit(stmt->finally_block());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001492 }
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001493 ExitFinallyBlock(); // Return to the calling code.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001494
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001495 // Set up try handler.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001496 __ bind(&try_entry);
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00001497 __ PushTryHandler(StackHandler::FINALLY, stmt->index());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001498 { TryFinally try_body(this, &finally_entry);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001499 Visit(stmt->try_block());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001500 }
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001501 __ PopTryHandler();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001502 // Execute the finally block on the way out. Clobber the unpredictable
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001503 // value in the result register with one that's safe for GC because the
1504 // finally block will unconditionally preserve the result register on the
1505 // stack.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001506 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001507 __ Call(&finally_entry);
1508}
1509
1510
1511void FullCodeGenerator::VisitDebuggerStatement(DebuggerStatement* stmt) {
1512#ifdef ENABLE_DEBUGGER_SUPPORT
1513 Comment cmnt(masm_, "[ DebuggerStatement");
1514 SetStatementPosition(stmt);
1515
ager@chromium.org5c838252010-02-19 08:53:10 +00001516 __ DebugBreak();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001517 // Ignore the return value.
1518#endif
1519}
1520
1521
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +00001522void FullCodeGenerator::VisitCaseClause(CaseClause* clause) {
1523 UNREACHABLE();
1524}
1525
1526
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001527void FullCodeGenerator::VisitConditional(Conditional* expr) {
1528 Comment cmnt(masm_, "[ Conditional");
1529 Label true_case, false_case, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001530 VisitForControl(expr->condition(), &true_case, &false_case, &true_case);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001531
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001532 PrepareForBailoutForId(expr->ThenId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001533 __ bind(&true_case);
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +00001534 SetExpressionPosition(expr->then_expression());
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001535 if (context()->IsTest()) {
1536 const TestContext* for_test = TestContext::cast(context());
1537 VisitForControl(expr->then_expression(),
1538 for_test->true_label(),
1539 for_test->false_label(),
1540 NULL);
1541 } else {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001542 VisitInDuplicateContext(expr->then_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001543 __ jmp(&done);
1544 }
1545
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001546 PrepareForBailoutForId(expr->ElseId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001547 __ bind(&false_case);
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +00001548 SetExpressionPosition(expr->else_expression());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001549 VisitInDuplicateContext(expr->else_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001550 // If control flow falls through Visit, merge it with true case here.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001551 if (!context()->IsTest()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001552 __ bind(&done);
1553 }
1554}
1555
1556
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001557void FullCodeGenerator::VisitLiteral(Literal* expr) {
1558 Comment cmnt(masm_, "[ Literal");
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00001559 context()->Plug(expr->value());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001560}
1561
1562
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001563void FullCodeGenerator::VisitFunctionLiteral(FunctionLiteral* expr) {
1564 Comment cmnt(masm_, "[ FunctionLiteral");
1565
1566 // Build the function boilerplate and instantiate it.
1567 Handle<SharedFunctionInfo> function_info =
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001568 Compiler::BuildFunctionInfo(expr, script());
1569 if (function_info.is_null()) {
1570 SetStackOverflow();
1571 return;
1572 }
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001573 EmitNewClosure(function_info, expr->pretenure());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001574}
1575
1576
jkummerow@chromium.orgfb7a7c42013-10-02 11:41:02 +00001577void FullCodeGenerator::VisitNativeFunctionLiteral(
1578 NativeFunctionLiteral* expr) {
1579 Comment cmnt(masm_, "[ NativeFunctionLiteral");
1580
1581 // Compute the function template for the native function.
1582 Handle<String> name = expr->name();
1583 v8::Handle<v8::FunctionTemplate> fun_template =
1584 expr->extension()->GetNativeFunction(v8::Utils::ToLocal(name));
1585 ASSERT(!fun_template.IsEmpty());
1586
1587 // Instantiate the function and create a shared function info from it.
1588 Handle<JSFunction> fun = Utils::OpenHandle(*fun_template->GetFunction());
1589 const int literals = fun->NumberOfLiterals();
1590 Handle<Code> code = Handle<Code>(fun->shared()->code());
1591 Handle<Code> construct_stub = Handle<Code>(fun->shared()->construct_stub());
1592 bool is_generator = false;
1593 Handle<SharedFunctionInfo> shared =
1594 isolate()->factory()->NewSharedFunctionInfo(name, literals, is_generator,
1595 code, Handle<ScopeInfo>(fun->shared()->scope_info()));
1596 shared->set_construct_stub(*construct_stub);
1597
1598 // Copy the function data to the shared function info.
1599 shared->set_function_data(fun->shared()->function_data());
1600 int parameters = fun->shared()->formal_parameter_count();
1601 shared->set_formal_parameter_count(parameters);
1602
1603 EmitNewClosure(shared, false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001604}
1605
1606
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001607void FullCodeGenerator::VisitThrow(Throw* expr) {
1608 Comment cmnt(masm_, "[ Throw");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001609 VisitForStackValue(expr->exception());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001610 __ CallRuntime(Runtime::kThrow, 1);
1611 // Never returns here.
1612}
1613
1614
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001615FullCodeGenerator::NestedStatement* FullCodeGenerator::TryCatch::Exit(
1616 int* stack_depth,
1617 int* context_length) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001618 // The macros used here must preserve the result register.
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001619 __ Drop(*stack_depth);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001620 __ PopTryHandler();
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001621 *stack_depth = 0;
1622 return previous_;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001623}
1624
ricow@chromium.org65fae842010-08-25 15:26:24 +00001625
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001626bool FullCodeGenerator::TryLiteralCompare(CompareOperation* expr) {
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001627 Expression* sub_expr;
ager@chromium.org04921a82011-06-27 13:21:41 +00001628 Handle<String> check;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001629 if (expr->IsLiteralCompareTypeof(&sub_expr, &check)) {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001630 EmitLiteralCompareTypeof(expr, sub_expr, check);
ager@chromium.org04921a82011-06-27 13:21:41 +00001631 return true;
1632 }
1633
jkummerow@chromium.org96a3c512013-07-18 17:02:47 +00001634 if (expr->IsLiteralCompareUndefined(&sub_expr, isolate())) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001635 EmitLiteralCompareNil(expr, sub_expr, kUndefinedValue);
1636 return true;
1637 }
1638
1639 if (expr->IsLiteralCompareNull(&sub_expr)) {
1640 EmitLiteralCompareNil(expr, sub_expr, kNullValue);
ager@chromium.org04921a82011-06-27 13:21:41 +00001641 return true;
1642 }
1643
1644 return false;
1645}
1646
1647
machenbach@chromium.org528ce022013-09-23 14:09:36 +00001648void BackEdgeTable::Patch(Isolate* isolate,
1649 Code* unoptimized) {
1650 DisallowHeapAllocation no_gc;
machenbach@chromium.org8e36b5b2013-09-26 07:36:30 +00001651 Code* patch = isolate->builtins()->builtin(Builtins::kOnStackReplacement);
machenbach@chromium.org528ce022013-09-23 14:09:36 +00001652
1653 // Iterate over the back edge table and patch every interrupt
1654 // call to an unconditional call to the replacement code.
1655 int loop_nesting_level = unoptimized->allow_osr_at_loop_nesting_level();
1656
1657 BackEdgeTable back_edges(unoptimized, &no_gc);
1658 for (uint32_t i = 0; i < back_edges.length(); i++) {
1659 if (static_cast<int>(back_edges.loop_depth(i)) == loop_nesting_level) {
1660 ASSERT_EQ(INTERRUPT, GetBackEdgeState(isolate,
1661 unoptimized,
1662 back_edges.pc(i)));
machenbach@chromium.org8e36b5b2013-09-26 07:36:30 +00001663 PatchAt(unoptimized, back_edges.pc(i), ON_STACK_REPLACEMENT, patch);
machenbach@chromium.org528ce022013-09-23 14:09:36 +00001664 }
1665 }
1666
1667 unoptimized->set_back_edges_patched_for_osr(true);
1668 ASSERT(Verify(isolate, unoptimized, loop_nesting_level));
1669}
1670
1671
1672void BackEdgeTable::Revert(Isolate* isolate,
1673 Code* unoptimized) {
1674 DisallowHeapAllocation no_gc;
machenbach@chromium.org8e36b5b2013-09-26 07:36:30 +00001675 Code* patch = isolate->builtins()->builtin(Builtins::kInterruptCheck);
machenbach@chromium.org528ce022013-09-23 14:09:36 +00001676
1677 // Iterate over the back edge table and revert the patched interrupt calls.
1678 ASSERT(unoptimized->back_edges_patched_for_osr());
1679 int loop_nesting_level = unoptimized->allow_osr_at_loop_nesting_level();
1680
1681 BackEdgeTable back_edges(unoptimized, &no_gc);
1682 for (uint32_t i = 0; i < back_edges.length(); i++) {
1683 if (static_cast<int>(back_edges.loop_depth(i)) <= loop_nesting_level) {
machenbach@chromium.org8e36b5b2013-09-26 07:36:30 +00001684 ASSERT_NE(INTERRUPT, GetBackEdgeState(isolate,
1685 unoptimized,
1686 back_edges.pc(i)));
1687 PatchAt(unoptimized, back_edges.pc(i), INTERRUPT, patch);
machenbach@chromium.org528ce022013-09-23 14:09:36 +00001688 }
1689 }
1690
1691 unoptimized->set_back_edges_patched_for_osr(false);
1692 unoptimized->set_allow_osr_at_loop_nesting_level(0);
1693 // Assert that none of the back edges are patched anymore.
1694 ASSERT(Verify(isolate, unoptimized, -1));
1695}
1696
1697
machenbach@chromium.org8e36b5b2013-09-26 07:36:30 +00001698void BackEdgeTable::AddStackCheck(CompilationInfo* info) {
1699 DisallowHeapAllocation no_gc;
1700 Isolate* isolate = info->isolate();
1701 Code* code = info->shared_info()->code();
1702 Address pc = code->instruction_start() + info->osr_pc_offset();
1703 ASSERT_EQ(ON_STACK_REPLACEMENT, GetBackEdgeState(isolate, code, pc));
1704 Code* patch = isolate->builtins()->builtin(Builtins::kOsrAfterStackCheck);
1705 PatchAt(code, pc, OSR_AFTER_STACK_CHECK, patch);
1706}
1707
1708
1709void BackEdgeTable::RemoveStackCheck(CompilationInfo* info) {
1710 DisallowHeapAllocation no_gc;
1711 Isolate* isolate = info->isolate();
1712 Code* code = info->shared_info()->code();
1713 Address pc = code->instruction_start() + info->osr_pc_offset();
1714 if (GetBackEdgeState(isolate, code, pc) == OSR_AFTER_STACK_CHECK) {
1715 Code* patch = isolate->builtins()->builtin(Builtins::kOnStackReplacement);
1716 PatchAt(code, pc, ON_STACK_REPLACEMENT, patch);
1717 }
1718}
1719
1720
machenbach@chromium.org528ce022013-09-23 14:09:36 +00001721#ifdef DEBUG
1722bool BackEdgeTable::Verify(Isolate* isolate,
1723 Code* unoptimized,
1724 int loop_nesting_level) {
1725 DisallowHeapAllocation no_gc;
1726 BackEdgeTable back_edges(unoptimized, &no_gc);
1727 for (uint32_t i = 0; i < back_edges.length(); i++) {
1728 uint32_t loop_depth = back_edges.loop_depth(i);
1729 CHECK_LE(static_cast<int>(loop_depth), Code::kMaxLoopNestingMarker);
1730 // Assert that all back edges for shallower loops (and only those)
1731 // have already been patched.
1732 CHECK_EQ((static_cast<int>(loop_depth) <= loop_nesting_level),
1733 GetBackEdgeState(isolate,
1734 unoptimized,
1735 back_edges.pc(i)) != INTERRUPT);
1736 }
1737 return true;
1738}
1739#endif // DEBUG
1740
1741
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001742#undef __
1743
1744
1745} } // namespace v8::internal