blob: e14afefda4547a722d4185f496173ccd9e684e1a [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);
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000348 cgen.PopulateTypeFeedbackCells(code);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000349 code->set_has_deoptimization_support(info->HasDeoptimizationSupport());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +0000350 code->set_handler_table(*cgen.handler_table());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000351#ifdef ENABLE_DEBUGGER_SUPPORT
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000352 code->set_compiled_optimizable(info->IsOptimizable());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000353#endif // ENABLE_DEBUGGER_SUPPORT
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000354 code->set_allow_osr_at_loop_nesting_level(0);
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000355 code->set_profiler_ticks(0);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000356 code->set_back_edge_table_offset(table_offset);
357 code->set_back_edges_patched_for_osr(false);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000358 CodeGenerator::PrintCode(code, info);
rossberg@chromium.orgebeba022013-08-19 09:36:44 +0000359 info->SetCode(code);
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000360#ifdef ENABLE_GDB_JIT_INTERFACE
rossberg@chromium.orgebeba022013-08-19 09:36:44 +0000361 if (FLAG_gdbjit) {
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000362 GDBJITLineInfo* lineinfo =
363 masm.positions_recorder()->DetachGDBJITLineInfo();
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000364 GDBJIT(RegisterDetailedLineInfo(*code, lineinfo));
365 }
366#endif
rossberg@chromium.orgebeba022013-08-19 09:36:44 +0000367 void* line_info = masm.positions_recorder()->DetachJITHandlerData();
368 LOG_CODE_EVENT(isolate, CodeEndLinePosInfoRecordEvent(*code, line_info));
369 return true;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000370}
371
372
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000373unsigned FullCodeGenerator::EmitBackEdgeTable() {
374 // The back edge table consists of a length (in number of entries)
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000375 // field, and then a sequence of entries. Each entry is a pair of AST id
376 // and code-relative pc offset.
377 masm()->Align(kIntSize);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000378 unsigned offset = masm()->pc_offset();
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000379 unsigned length = back_edges_.length();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000380 __ dd(length);
381 for (unsigned i = 0; i < length; ++i) {
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000382 __ dd(back_edges_[i].id.ToInt());
383 __ dd(back_edges_[i].pc);
jkummerow@chromium.orgba72ec82013-07-22 09:21:20 +0000384 __ dd(back_edges_[i].loop_depth);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000385 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000386 return offset;
387}
388
389
390void FullCodeGenerator::PopulateDeoptimizationData(Handle<Code> code) {
391 // Fill in the deoptimization information.
392 ASSERT(info_->HasDeoptimizationSupport() || bailout_entries_.is_empty());
393 if (!info_->HasDeoptimizationSupport()) return;
394 int length = bailout_entries_.length();
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000395 Handle<DeoptimizationOutputData> data = isolate()->factory()->
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000396 NewDeoptimizationOutputData(length, TENURED);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000397 for (int i = 0; i < length; i++) {
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000398 data->SetAstId(i, bailout_entries_[i].id);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000399 data->SetPcAndState(i, Smi::FromInt(bailout_entries_[i].pc_and_state));
400 }
401 code->set_deoptimization_data(*data);
402}
403
404
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000405void FullCodeGenerator::PopulateTypeFeedbackInfo(Handle<Code> code) {
406 Handle<TypeFeedbackInfo> info = isolate()->factory()->NewTypeFeedbackInfo();
407 info->set_ic_total_count(ic_total_count_);
yangguo@chromium.orga7d3df92012-02-27 11:46:55 +0000408 ASSERT(!isolate()->heap()->InNewSpace(*info));
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000409 code->set_type_feedback_info(*info);
410}
411
412
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +0000413void FullCodeGenerator::Initialize() {
414 // The generation of debug code must match between the snapshot code and the
415 // code that is generated later. This is assumed by the debugger when it is
416 // calculating PC offsets after generating a debug version of code. Therefore
417 // we disable the production of debug code in the full compiler if we are
418 // either generating a snapshot or we booted from a snapshot.
419 generate_debug_code_ = FLAG_debug_code &&
420 !Serializer::enabled() &&
421 !Snapshot::HaveASnapshotToStartFrom();
422 masm_->set_emit_debug_code(generate_debug_code_);
423 masm_->set_predictable_code_size(true);
machenbach@chromium.org6d26cbb2014-01-22 10:50:56 +0000424 InitializeAstVisitor(info_->zone());
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +0000425}
426
427
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000428void FullCodeGenerator::PopulateTypeFeedbackCells(Handle<Code> code) {
429 if (type_feedback_cells_.is_empty()) return;
430 int length = type_feedback_cells_.length();
431 int array_size = TypeFeedbackCells::LengthOfFixedArray(length);
432 Handle<TypeFeedbackCells> cache = Handle<TypeFeedbackCells>::cast(
433 isolate()->factory()->NewFixedArray(array_size, TENURED));
434 for (int i = 0; i < length; i++) {
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000435 cache->SetAstId(i, type_feedback_cells_[i].ast_id);
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000436 cache->SetCell(i, *type_feedback_cells_[i].cell);
437 }
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000438 TypeFeedbackInfo::cast(code->type_feedback_info())->set_type_feedback_cells(
439 *cache);
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000440}
441
442
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000443void FullCodeGenerator::PrepareForBailout(Expression* node, State state) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000444 PrepareForBailoutForId(node->id(), state);
445}
446
447
machenbach@chromium.orga03ba1e2014-02-01 08:54:43 +0000448void FullCodeGenerator::CallLoadIC(ContextualMode contextual_mode,
449 TypeFeedbackId id) {
450 ExtraICState extra_state = LoadIC::ComputeExtraICState(contextual_mode);
451 Handle<Code> ic = LoadIC::initialize_stub(isolate(), extra_state);
452 CallIC(ic, contextual_mode, id);
ulan@chromium.org9cbaabd2014-01-08 10:55:36 +0000453}
454
455
456void FullCodeGenerator::CallStoreIC(ContextualMode mode, TypeFeedbackId id) {
machenbach@chromium.org43c51e52014-01-20 07:57:28 +0000457 Handle<Code> ic = StoreIC::initialize_stub(isolate(), strict_mode());
ulan@chromium.org9cbaabd2014-01-08 10:55:36 +0000458 CallIC(ic, mode, id);
459}
460
461
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000462void FullCodeGenerator::RecordJSReturnSite(Call* call) {
463 // We record the offset of the function return so we can rebuild the frame
464 // if the function was inlined, i.e., this is the return address in the
465 // inlined function's frame.
466 //
467 // The state is ignored. We defensively set it to TOS_REG, which is the
468 // real state of the unoptimized code at the return site.
469 PrepareForBailoutForId(call->ReturnId(), TOS_REG);
470#ifdef DEBUG
471 // In debug builds, mark the return so we can verify that this function
472 // was called.
473 ASSERT(!call->return_is_recorded_);
474 call->return_is_recorded_ = true;
475#endif
476}
477
478
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000479void FullCodeGenerator::PrepareForBailoutForId(BailoutId id, State state) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000480 // There's no need to prepare this code for bailouts from already optimized
481 // code or code that can't be optimized.
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000482 if (!info_->HasDeoptimizationSupport()) return;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000483 unsigned pc_and_state =
484 StateField::encode(state) | PcField::encode(masm_->pc_offset());
yangguo@chromium.org56454712012-02-16 15:33:53 +0000485 ASSERT(Smi::IsValid(pc_and_state));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000486 BailoutEntry entry = { id, pc_and_state };
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000487 ASSERT(!prepared_bailout_ids_.Contains(id.ToInt()));
488 prepared_bailout_ids_.Add(id.ToInt(), zone());
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000489 bailout_entries_.Add(entry, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000490}
491
492
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000493void FullCodeGenerator::RecordTypeFeedbackCell(
danno@chromium.org41728482013-06-12 22:31:22 +0000494 TypeFeedbackId id, Handle<Cell> cell) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000495 TypeFeedbackCellEntry entry = { id, cell };
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000496 type_feedback_cells_.Add(entry, zone());
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000497}
498
499
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +0000500void FullCodeGenerator::RecordBackEdge(BailoutId ast_id) {
501 // The pc offset does not need to be encoded and packed together with a state.
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +0000502 ASSERT(masm_->pc_offset() > 0);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000503 ASSERT(loop_depth() > 0);
504 uint8_t depth = Min(loop_depth(), Code::kMaxLoopNestingMarker);
505 BackEdgeEntry entry =
506 { ast_id, static_cast<unsigned>(masm_->pc_offset()), depth };
507 back_edges_.Add(entry, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000508}
509
510
ricow@chromium.org65fae842010-08-25 15:26:24 +0000511bool FullCodeGenerator::ShouldInlineSmiCase(Token::Value op) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000512 // Inline smi case inside loops, but not division and modulo which
513 // are too complicated and take up too much space.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000514 if (op == Token::DIV ||op == Token::MOD) return false;
515 if (FLAG_always_inline_smi_code) return true;
516 return loop_depth_ > 0;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000517}
518
519
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000520void FullCodeGenerator::EffectContext::Plug(Register reg) const {
521}
522
523
524void FullCodeGenerator::AccumulatorValueContext::Plug(Register reg) const {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000525 __ Move(result_register(), reg);
526}
527
528
529void FullCodeGenerator::StackValueContext::Plug(Register reg) const {
danno@chromium.org59400602013-08-13 17:09:37 +0000530 __ Push(reg);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000531}
532
533
534void FullCodeGenerator::TestContext::Plug(Register reg) const {
535 // For simplicity we always test the accumulator register.
536 __ Move(result_register(), reg);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000537 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000538 codegen()->DoTest(this);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000539}
540
541
542void FullCodeGenerator::EffectContext::PlugTOS() const {
543 __ Drop(1);
544}
545
546
547void FullCodeGenerator::AccumulatorValueContext::PlugTOS() const {
danno@chromium.org59400602013-08-13 17:09:37 +0000548 __ Pop(result_register());
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000549}
550
551
552void FullCodeGenerator::StackValueContext::PlugTOS() const {
553}
554
555
556void FullCodeGenerator::TestContext::PlugTOS() const {
557 // For simplicity we always test the accumulator register.
danno@chromium.org59400602013-08-13 17:09:37 +0000558 __ Pop(result_register());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000559 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000560 codegen()->DoTest(this);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000561}
562
563
564void FullCodeGenerator::EffectContext::PrepareTest(
565 Label* materialize_true,
566 Label* materialize_false,
567 Label** if_true,
568 Label** if_false,
569 Label** fall_through) const {
570 // In an effect context, the true and the false case branch to the
571 // same label.
572 *if_true = *if_false = *fall_through = materialize_true;
573}
574
575
576void FullCodeGenerator::AccumulatorValueContext::PrepareTest(
577 Label* materialize_true,
578 Label* materialize_false,
579 Label** if_true,
580 Label** if_false,
581 Label** fall_through) const {
582 *if_true = *fall_through = materialize_true;
583 *if_false = materialize_false;
584}
585
586
587void FullCodeGenerator::StackValueContext::PrepareTest(
588 Label* materialize_true,
589 Label* materialize_false,
590 Label** if_true,
591 Label** if_false,
592 Label** fall_through) const {
593 *if_true = *fall_through = materialize_true;
594 *if_false = materialize_false;
595}
596
597
598void FullCodeGenerator::TestContext::PrepareTest(
599 Label* materialize_true,
600 Label* materialize_false,
601 Label** if_true,
602 Label** if_false,
603 Label** fall_through) const {
604 *if_true = true_label_;
605 *if_false = false_label_;
606 *fall_through = fall_through_;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000607}
608
609
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000610void FullCodeGenerator::DoTest(const TestContext* context) {
611 DoTest(context->condition(),
612 context->true_label(),
613 context->false_label(),
614 context->fall_through());
615}
616
617
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000618void FullCodeGenerator::AllocateModules(ZoneList<Declaration*>* declarations) {
619 ASSERT(scope_->is_global_scope());
620
621 for (int i = 0; i < declarations->length(); i++) {
622 ModuleDeclaration* declaration = declarations->at(i)->AsModuleDeclaration();
623 if (declaration != NULL) {
624 ModuleLiteral* module = declaration->module()->AsModuleLiteral();
625 if (module != NULL) {
626 Comment cmnt(masm_, "[ Link nested modules");
627 Scope* scope = module->body()->scope();
628 Interface* interface = scope->interface();
629 ASSERT(interface->IsModule() && interface->IsFrozen());
630
631 interface->Allocate(scope->module_var()->index());
632
633 // Set up module context.
634 ASSERT(scope->interface()->Index() >= 0);
635 __ Push(Smi::FromInt(scope->interface()->Index()));
636 __ Push(scope->GetScopeInfo());
637 __ CallRuntime(Runtime::kPushModuleContext, 2);
638 StoreToFrameField(StandardFrameConstants::kContextOffset,
639 context_register());
640
641 AllocateModules(scope->declarations());
642
643 // Pop module context.
644 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
645 // Update local stack frame context field.
646 StoreToFrameField(StandardFrameConstants::kContextOffset,
647 context_register());
648 }
649 }
650 }
651}
652
653
654// Modules have their own local scope, represented by their own context.
655// Module instance objects have an accessor for every export that forwards
656// access to the respective slot from the module's context. (Exports that are
657// modules themselves, however, are simple data properties.)
658//
659// All modules have a _hosting_ scope/context, which (currently) is the
660// (innermost) enclosing global scope. To deal with recursion, nested modules
661// are hosted by the same scope as global ones.
662//
663// For every (global or nested) module literal, the hosting context has an
664// internal slot that points directly to the respective module context. This
665// enables quick access to (statically resolved) module members by 2-dimensional
666// access through the hosting context. For example,
667//
668// module A {
669// let x;
670// module B { let y; }
671// }
672// module C { let z; }
673//
674// allocates contexts as follows:
675//
676// [header| .A | .B | .C | A | C ] (global)
677// | | |
678// | | +-- [header| z ] (module)
679// | |
680// | +------- [header| y ] (module)
681// |
682// +------------ [header| x | B ] (module)
683//
684// Here, .A, .B, .C are the internal slots pointing to the hosted module
685// contexts, whereas A, B, C hold the actual instance objects (note that every
686// module context also points to the respective instance object through its
687// extension slot in the header).
688//
689// To deal with arbitrary recursion and aliases between modules,
690// they are created and initialized in several stages. Each stage applies to
691// all modules in the hosting global scope, including nested ones.
692//
693// 1. Allocate: for each module _literal_, allocate the module contexts and
694// respective instance object and wire them up. This happens in the
695// PushModuleContext runtime function, as generated by AllocateModules
696// (invoked by VisitDeclarations in the hosting scope).
697//
698// 2. Bind: for each module _declaration_ (i.e. literals as well as aliases),
699// assign the respective instance object to respective local variables. This
700// happens in VisitModuleDeclaration, and uses the instance objects created
701// in the previous stage.
702// For each module _literal_, this phase also constructs a module descriptor
703// for the next stage. This happens in VisitModuleLiteral.
704//
705// 3. Populate: invoke the DeclareModules runtime function to populate each
706// _instance_ object with accessors for it exports. This is generated by
707// DeclareModules (invoked by VisitDeclarations in the hosting scope again),
708// and uses the descriptors generated in the previous stage.
709//
710// 4. Initialize: execute the module bodies (and other code) in sequence. This
711// happens by the separate statements generated for module bodies. To reenter
712// the module scopes properly, the parser inserted ModuleStatements.
713
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000714void FullCodeGenerator::VisitDeclarations(
715 ZoneList<Declaration*>* declarations) {
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000716 Handle<FixedArray> saved_modules = modules_;
717 int saved_module_index = module_index_;
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000718 ZoneList<Handle<Object> >* saved_globals = globals_;
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000719 ZoneList<Handle<Object> > inner_globals(10, zone());
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000720 globals_ = &inner_globals;
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000721
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000722 if (scope_->num_modules() != 0) {
723 // This is a scope hosting modules. Allocate a descriptor array to pass
724 // to the runtime for initialization.
725 Comment cmnt(masm_, "[ Allocate modules");
726 ASSERT(scope_->is_global_scope());
727 modules_ =
728 isolate()->factory()->NewFixedArray(scope_->num_modules(), TENURED);
729 module_index_ = 0;
730
731 // Generate code for allocating all modules, including nested ones.
732 // The allocated contexts are stored in internal variables in this scope.
733 AllocateModules(declarations);
734 }
735
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000736 AstVisitor::VisitDeclarations(declarations);
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000737
738 if (scope_->num_modules() != 0) {
739 // Initialize modules from descriptor array.
740 ASSERT(module_index_ == modules_->length());
741 DeclareModules(modules_);
742 modules_ = saved_modules;
743 module_index_ = saved_module_index;
744 }
745
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000746 if (!globals_->is_empty()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000747 // Invoke the platform-dependent code generator to do the actual
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000748 // declaration of the global functions and variables.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000749 Handle<FixedArray> array =
750 isolate()->factory()->NewFixedArray(globals_->length(), TENURED);
751 for (int i = 0; i < globals_->length(); ++i)
752 array->set(i, *globals_->at(i));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000753 DeclareGlobals(array);
754 }
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000755
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000756 globals_ = saved_globals;
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000757}
758
759
760void FullCodeGenerator::VisitModuleLiteral(ModuleLiteral* module) {
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000761 Block* block = module->body();
762 Scope* saved_scope = scope();
763 scope_ = block->scope();
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000764 Interface* interface = scope_->interface();
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000765
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000766 Comment cmnt(masm_, "[ ModuleLiteral");
767 SetStatementPosition(block);
768
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000769 ASSERT(!modules_.is_null());
770 ASSERT(module_index_ < modules_->length());
771 int index = module_index_++;
772
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000773 // Set up module context.
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000774 ASSERT(interface->Index() >= 0);
775 __ Push(Smi::FromInt(interface->Index()));
776 __ Push(Smi::FromInt(0));
777 __ CallRuntime(Runtime::kPushModuleContext, 2);
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000778 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000779
780 {
781 Comment cmnt(masm_, "[ Declarations");
782 VisitDeclarations(scope_->declarations());
783 }
784
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000785 // Populate the module description.
786 Handle<ModuleInfo> description =
787 ModuleInfo::Create(isolate(), interface, scope_);
788 modules_->set(index, *description);
789
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000790 scope_ = saved_scope;
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000791 // Pop module context.
792 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
793 // Update local stack frame context field.
794 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000795}
796
797
798void FullCodeGenerator::VisitModuleVariable(ModuleVariable* module) {
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000799 // Nothing to do.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000800 // The instance object is resolved statically through the module's interface.
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000801}
802
803
804void FullCodeGenerator::VisitModulePath(ModulePath* module) {
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000805 // Nothing to do.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000806 // The instance object is resolved statically through the module's interface.
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000807}
808
809
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000810void FullCodeGenerator::VisitModuleUrl(ModuleUrl* module) {
811 // TODO(rossberg): dummy allocation for now.
812 Scope* scope = module->body()->scope();
813 Interface* interface = scope_->interface();
814
815 ASSERT(interface->IsModule() && interface->IsFrozen());
816 ASSERT(!modules_.is_null());
817 ASSERT(module_index_ < modules_->length());
818 interface->Allocate(scope->module_var()->index());
819 int index = module_index_++;
820
821 Handle<ModuleInfo> description =
822 ModuleInfo::Create(isolate(), interface, scope_);
823 modules_->set(index, *description);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000824}
825
826
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000827int FullCodeGenerator::DeclareGlobalsFlags() {
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000828 ASSERT(DeclareGlobalsLanguageMode::is_valid(language_mode()));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000829 return DeclareGlobalsEvalFlag::encode(is_eval()) |
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000830 DeclareGlobalsNativeFlag::encode(is_native()) |
831 DeclareGlobalsLanguageMode::encode(language_mode());
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000832}
833
834
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000835void FullCodeGenerator::SetFunctionPosition(FunctionLiteral* fun) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000836 CodeGenerator::RecordPositions(masm_, fun->start_position());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000837}
838
839
840void FullCodeGenerator::SetReturnPosition(FunctionLiteral* fun) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000841 CodeGenerator::RecordPositions(masm_, fun->end_position() - 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000842}
843
844
845void FullCodeGenerator::SetStatementPosition(Statement* stmt) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000846#ifdef ENABLE_DEBUGGER_SUPPORT
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000847 if (!isolate()->debugger()->IsDebuggerActive()) {
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +0000848 CodeGenerator::RecordPositions(masm_, stmt->position());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000849 } else {
850 // Check if the statement will be breakable without adding a debug break
851 // slot.
machenbach@chromium.org6d26cbb2014-01-22 10:50:56 +0000852 BreakableStatementChecker checker(zone());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000853 checker.Check(stmt);
854 // Record the statement position right here if the statement is not
855 // breakable. For breakable statements the actual recording of the
856 // position will be postponed to the breakable code (typically an IC).
857 bool position_recorded = CodeGenerator::RecordPositions(
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +0000858 masm_, stmt->position(), !checker.is_breakable());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000859 // If the position recording did record a new position generate a debug
860 // break slot to make the statement breakable.
861 if (position_recorded) {
862 Debug::GenerateSlot(masm_);
863 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000864 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000865#else
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +0000866 CodeGenerator::RecordPositions(masm_, stmt->position());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000867#endif
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000868}
869
870
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +0000871void FullCodeGenerator::SetExpressionPosition(Expression* expr) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000872#ifdef ENABLE_DEBUGGER_SUPPORT
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000873 if (!isolate()->debugger()->IsDebuggerActive()) {
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +0000874 CodeGenerator::RecordPositions(masm_, expr->position());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000875 } else {
876 // Check if the expression will be breakable without adding a debug break
877 // slot.
machenbach@chromium.org6d26cbb2014-01-22 10:50:56 +0000878 BreakableStatementChecker checker(zone());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000879 checker.Check(expr);
880 // Record a statement position right here if the expression is not
881 // breakable. For breakable expressions the actual recording of the
882 // position will be postponed to the breakable code (typically an IC).
883 // NOTE this will record a statement position for something which might
884 // not be a statement. As stepping in the debugger will only stop at
885 // statement positions this is used for e.g. the condition expression of
886 // a do while loop.
887 bool position_recorded = CodeGenerator::RecordPositions(
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +0000888 masm_, expr->position(), !checker.is_breakable());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000889 // If the position recording did record a new position generate a debug
890 // break slot to make the statement breakable.
891 if (position_recorded) {
892 Debug::GenerateSlot(masm_);
893 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000894 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000895#else
896 CodeGenerator::RecordPositions(masm_, pos);
897#endif
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000898}
899
900
901void FullCodeGenerator::SetStatementPosition(int pos) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000902 CodeGenerator::RecordPositions(masm_, pos);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000903}
904
905
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000906void FullCodeGenerator::SetSourcePosition(int pos) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000907 if (pos != RelocInfo::kNoPosition) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000908 masm_->positions_recorder()->RecordPosition(pos);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000909 }
910}
911
912
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000913// Lookup table for code generators for special runtime calls which are
914// generated inline.
915#define INLINE_FUNCTION_GENERATOR_ADDRESS(Name, argc, ressize) \
916 &FullCodeGenerator::Emit##Name,
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000917
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000918const FullCodeGenerator::InlineFunctionGenerator
919 FullCodeGenerator::kInlineFunctionGenerators[] = {
920 INLINE_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS)
921 INLINE_RUNTIME_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS)
922 };
923#undef INLINE_FUNCTION_GENERATOR_ADDRESS
924
925
926FullCodeGenerator::InlineFunctionGenerator
927 FullCodeGenerator::FindInlineFunctionGenerator(Runtime::FunctionId id) {
whesse@chromium.org023421e2010-12-21 12:19:12 +0000928 int lookup_index =
929 static_cast<int>(id) - static_cast<int>(Runtime::kFirstInlineFunction);
930 ASSERT(lookup_index >= 0);
931 ASSERT(static_cast<size_t>(lookup_index) <
932 ARRAY_SIZE(kInlineFunctionGenerators));
933 return kInlineFunctionGenerators[lookup_index];
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000934}
935
936
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000937void FullCodeGenerator::EmitInlineRuntimeCall(CallRuntime* expr) {
938 const Runtime::Function* function = expr->function();
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000939 ASSERT(function != NULL);
940 ASSERT(function->intrinsic_type == Runtime::INLINE);
941 InlineFunctionGenerator generator =
942 FindInlineFunctionGenerator(function->function_id);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000943 ((*this).*(generator))(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000944}
945
946
verwaest@chromium.org8a00e822013-06-10 15:11:22 +0000947void FullCodeGenerator::EmitGeneratorNext(CallRuntime* expr) {
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000948 ZoneList<Expression*>* args = expr->arguments();
949 ASSERT(args->length() == 2);
verwaest@chromium.org8a00e822013-06-10 15:11:22 +0000950 EmitGeneratorResume(args->at(0), args->at(1), JSGeneratorObject::NEXT);
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000951}
952
953
954void FullCodeGenerator::EmitGeneratorThrow(CallRuntime* expr) {
955 ZoneList<Expression*>* args = expr->arguments();
956 ASSERT(args->length() == 2);
957 EmitGeneratorResume(args->at(0), args->at(1), JSGeneratorObject::THROW);
958}
959
960
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +0000961void FullCodeGenerator::EmitDebugBreakInOptimizedCode(CallRuntime* expr) {
962 context()->Plug(handle(Smi::FromInt(0), isolate()));
963}
964
965
ricow@chromium.org65fae842010-08-25 15:26:24 +0000966void FullCodeGenerator::VisitBinaryOperation(BinaryOperation* expr) {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000967 switch (expr->op()) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000968 case Token::COMMA:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000969 return VisitComma(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000970 case Token::OR:
971 case Token::AND:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000972 return VisitLogicalExpression(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000973 default:
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000974 return VisitArithmeticExpression(expr);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000975 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000976}
977
978
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000979void FullCodeGenerator::VisitInDuplicateContext(Expression* expr) {
980 if (context()->IsEffect()) {
981 VisitForEffect(expr);
982 } else if (context()->IsAccumulatorValue()) {
983 VisitForAccumulatorValue(expr);
984 } else if (context()->IsStackValue()) {
985 VisitForStackValue(expr);
986 } else if (context()->IsTest()) {
987 const TestContext* test = TestContext::cast(context());
988 VisitForControl(expr, test->true_label(), test->false_label(),
989 test->fall_through());
990 }
991}
992
993
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000994void FullCodeGenerator::VisitComma(BinaryOperation* expr) {
995 Comment cmnt(masm_, "[ Comma");
996 VisitForEffect(expr->left());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000997 VisitInDuplicateContext(expr->right());
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000998}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000999
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001000
1001void FullCodeGenerator::VisitLogicalExpression(BinaryOperation* expr) {
1002 bool is_logical_and = expr->op() == Token::AND;
1003 Comment cmnt(masm_, is_logical_and ? "[ Logical AND" : "[ Logical OR");
1004 Expression* left = expr->left();
1005 Expression* right = expr->right();
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001006 BailoutId right_id = expr->RightId();
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001007 Label done;
1008
1009 if (context()->IsTest()) {
1010 Label eval_right;
1011 const TestContext* test = TestContext::cast(context());
1012 if (is_logical_and) {
1013 VisitForControl(left, &eval_right, test->false_label(), &eval_right);
1014 } else {
1015 VisitForControl(left, test->true_label(), &eval_right, &eval_right);
1016 }
1017 PrepareForBailoutForId(right_id, NO_REGISTERS);
1018 __ bind(&eval_right);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001019
1020 } else if (context()->IsAccumulatorValue()) {
1021 VisitForAccumulatorValue(left);
1022 // We want the value in the accumulator for the test, and on the stack in
1023 // case we need it.
danno@chromium.org59400602013-08-13 17:09:37 +00001024 __ Push(result_register());
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001025 Label discard, restore;
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001026 if (is_logical_and) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001027 DoTest(left, &discard, &restore, &restore);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001028 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001029 DoTest(left, &restore, &discard, &restore);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001030 }
1031 __ bind(&restore);
danno@chromium.org59400602013-08-13 17:09:37 +00001032 __ Pop(result_register());
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001033 __ jmp(&done);
1034 __ bind(&discard);
1035 __ Drop(1);
1036 PrepareForBailoutForId(right_id, NO_REGISTERS);
1037
1038 } else if (context()->IsStackValue()) {
1039 VisitForAccumulatorValue(left);
1040 // We want the value in the accumulator for the test, and on the stack in
1041 // case we need it.
danno@chromium.org59400602013-08-13 17:09:37 +00001042 __ Push(result_register());
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001043 Label discard;
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001044 if (is_logical_and) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001045 DoTest(left, &discard, &done, &discard);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001046 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001047 DoTest(left, &done, &discard, &discard);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001048 }
1049 __ bind(&discard);
1050 __ Drop(1);
1051 PrepareForBailoutForId(right_id, NO_REGISTERS);
1052
1053 } else {
1054 ASSERT(context()->IsEffect());
1055 Label eval_right;
1056 if (is_logical_and) {
1057 VisitForControl(left, &eval_right, &done, &eval_right);
1058 } else {
1059 VisitForControl(left, &done, &eval_right, &eval_right);
1060 }
1061 PrepareForBailoutForId(right_id, NO_REGISTERS);
1062 __ bind(&eval_right);
1063 }
1064
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001065 VisitInDuplicateContext(right);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001066 __ bind(&done);
1067}
1068
1069
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001070void FullCodeGenerator::VisitArithmeticExpression(BinaryOperation* expr) {
1071 Token::Value op = expr->op();
1072 Comment cmnt(masm_, "[ ArithmeticExpression");
1073 Expression* left = expr->left();
1074 Expression* right = expr->right();
1075 OverwriteMode mode =
1076 left->ResultOverwriteAllowed()
1077 ? OVERWRITE_LEFT
1078 : (right->ResultOverwriteAllowed() ? OVERWRITE_RIGHT : NO_OVERWRITE);
1079
1080 VisitForStackValue(left);
1081 VisitForAccumulatorValue(right);
1082
1083 SetSourcePosition(expr->position());
1084 if (ShouldInlineSmiCase(op)) {
1085 EmitInlineSmiBinaryOp(expr, op, mode, left, right);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001086 } else {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001087 EmitBinaryOp(expr, op, mode);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001088 }
1089}
1090
1091
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001092void FullCodeGenerator::VisitBlock(Block* stmt) {
1093 Comment cmnt(masm_, "[ Block");
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001094 NestedBlock nested_block(this, stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001095 SetStatementPosition(stmt);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001096
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001097 Scope* saved_scope = scope();
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001098 // Push a block context when entering a block with block scoped variables.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00001099 if (stmt->scope() != NULL) {
danno@chromium.org81cac2b2012-07-10 11:28:27 +00001100 scope_ = stmt->scope();
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00001101 ASSERT(!scope_->is_module_scope());
1102 { Comment cmnt(masm_, "[ Extend block context");
machenbach@chromium.org05150ab2014-01-29 08:13:29 +00001103 __ Push(scope_->GetScopeInfo());
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00001104 PushFunctionArgumentForContextAllocation();
machenbach@chromium.org05150ab2014-01-29 08:13:29 +00001105 __ CallRuntime(Runtime::kPushBlockContext, 2);
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001106
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00001107 // Replace the context stored in the frame.
1108 StoreToFrameField(StandardFrameConstants::kContextOffset,
1109 context_register());
1110 }
1111 { Comment cmnt(masm_, "[ Declarations");
1112 VisitDeclarations(scope_->declarations());
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001113 }
1114 }
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00001115
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00001116 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001117 VisitStatements(stmt->statements());
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001118 scope_ = saved_scope;
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001119 __ bind(nested_block.break_label());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001120 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001121
1122 // Pop block context if necessary.
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00001123 if (stmt->scope() != NULL) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001124 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1125 // Update local stack frame context field.
1126 StoreToFrameField(StandardFrameConstants::kContextOffset,
1127 context_register());
1128 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001129}
1130
1131
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00001132void FullCodeGenerator::VisitModuleStatement(ModuleStatement* stmt) {
1133 Comment cmnt(masm_, "[ Module context");
1134
1135 __ Push(Smi::FromInt(stmt->proxy()->interface()->Index()));
1136 __ Push(Smi::FromInt(0));
1137 __ CallRuntime(Runtime::kPushModuleContext, 2);
1138 StoreToFrameField(
1139 StandardFrameConstants::kContextOffset, context_register());
1140
1141 Scope* saved_scope = scope_;
1142 scope_ = stmt->body()->scope();
1143 VisitStatements(stmt->body()->statements());
1144 scope_ = saved_scope;
1145 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1146 // Update local stack frame context field.
1147 StoreToFrameField(StandardFrameConstants::kContextOffset,
1148 context_register());
1149}
1150
1151
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001152void FullCodeGenerator::VisitExpressionStatement(ExpressionStatement* stmt) {
1153 Comment cmnt(masm_, "[ ExpressionStatement");
1154 SetStatementPosition(stmt);
1155 VisitForEffect(stmt->expression());
1156}
1157
1158
1159void FullCodeGenerator::VisitEmptyStatement(EmptyStatement* stmt) {
1160 Comment cmnt(masm_, "[ EmptyStatement");
1161 SetStatementPosition(stmt);
1162}
1163
1164
1165void FullCodeGenerator::VisitIfStatement(IfStatement* stmt) {
1166 Comment cmnt(masm_, "[ IfStatement");
1167 SetStatementPosition(stmt);
1168 Label then_part, else_part, done;
1169
ricow@chromium.org65fae842010-08-25 15:26:24 +00001170 if (stmt->HasElseStatement()) {
1171 VisitForControl(stmt->condition(), &then_part, &else_part, &then_part);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001172 PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001173 __ bind(&then_part);
1174 Visit(stmt->then_statement());
1175 __ jmp(&done);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001176
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001177 PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001178 __ bind(&else_part);
1179 Visit(stmt->else_statement());
1180 } else {
1181 VisitForControl(stmt->condition(), &then_part, &done, &then_part);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001182 PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001183 __ bind(&then_part);
1184 Visit(stmt->then_statement());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001185
1186 PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001187 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001188 __ bind(&done);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001189 PrepareForBailoutForId(stmt->IfId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001190}
1191
1192
1193void FullCodeGenerator::VisitContinueStatement(ContinueStatement* stmt) {
1194 Comment cmnt(masm_, "[ ContinueStatement");
1195 SetStatementPosition(stmt);
1196 NestedStatement* current = nesting_stack_;
1197 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001198 int context_length = 0;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001199 // When continuing, we clobber the unpredictable value in the accumulator
1200 // with one that's safe for GC. If we hit an exit from the try block of
1201 // try...finally on our way out, we will unconditionally preserve the
1202 // accumulator on the stack.
1203 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001204 while (!current->IsContinueTarget(stmt->target())) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001205 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001206 }
1207 __ Drop(stack_depth);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001208 if (context_length > 0) {
1209 while (context_length > 0) {
1210 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1211 --context_length;
1212 }
1213 StoreToFrameField(StandardFrameConstants::kContextOffset,
1214 context_register());
1215 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001216
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001217 __ jmp(current->AsIteration()->continue_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001218}
1219
1220
1221void FullCodeGenerator::VisitBreakStatement(BreakStatement* stmt) {
1222 Comment cmnt(masm_, "[ BreakStatement");
1223 SetStatementPosition(stmt);
1224 NestedStatement* current = nesting_stack_;
1225 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001226 int context_length = 0;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001227 // When breaking, we clobber the unpredictable value in the accumulator
1228 // with one that's safe for GC. If we hit an exit from the try block of
1229 // try...finally on our way out, we will unconditionally preserve the
1230 // accumulator on the stack.
1231 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001232 while (!current->IsBreakTarget(stmt->target())) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001233 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001234 }
1235 __ Drop(stack_depth);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001236 if (context_length > 0) {
1237 while (context_length > 0) {
1238 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1239 --context_length;
1240 }
1241 StoreToFrameField(StandardFrameConstants::kContextOffset,
1242 context_register());
1243 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001244
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001245 __ jmp(current->AsBreakable()->break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001246}
1247
1248
danno@chromium.org41728482013-06-12 22:31:22 +00001249void FullCodeGenerator::EmitUnwindBeforeReturn() {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001250 NestedStatement* current = nesting_stack_;
1251 int stack_depth = 0;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001252 int context_length = 0;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001253 while (current != NULL) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001254 current = current->Exit(&stack_depth, &context_length);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001255 }
1256 __ Drop(stack_depth);
danno@chromium.org41728482013-06-12 22:31:22 +00001257}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001258
danno@chromium.org41728482013-06-12 22:31:22 +00001259
1260void FullCodeGenerator::VisitReturnStatement(ReturnStatement* stmt) {
1261 Comment cmnt(masm_, "[ ReturnStatement");
1262 SetStatementPosition(stmt);
1263 Expression* expr = stmt->expression();
1264 VisitForAccumulatorValue(expr);
1265 EmitUnwindBeforeReturn();
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001266 EmitReturnSequence();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001267}
1268
1269
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001270void FullCodeGenerator::VisitWithStatement(WithStatement* stmt) {
1271 Comment cmnt(masm_, "[ WithStatement");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001272 SetStatementPosition(stmt);
1273
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001274 VisitForStackValue(stmt->expression());
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00001275 PushFunctionArgumentForContextAllocation();
1276 __ CallRuntime(Runtime::kPushWithContext, 2);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001277 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001278
danno@chromium.orgca29dd82013-04-26 11:59:48 +00001279 Scope* saved_scope = scope();
1280 scope_ = stmt->scope();
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001281 { WithOrCatch body(this);
1282 Visit(stmt->statement());
1283 }
danno@chromium.orgca29dd82013-04-26 11:59:48 +00001284 scope_ = saved_scope;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001285
1286 // Pop context.
1287 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1288 // Update local stack frame context field.
1289 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001290}
1291
1292
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001293void FullCodeGenerator::VisitDoWhileStatement(DoWhileStatement* stmt) {
1294 Comment cmnt(masm_, "[ DoWhileStatement");
1295 SetStatementPosition(stmt);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001296 Label body, book_keeping;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001297
1298 Iteration loop_statement(this, stmt);
1299 increment_loop_depth();
1300
1301 __ bind(&body);
1302 Visit(stmt->body());
1303
ricow@chromium.org65fae842010-08-25 15:26:24 +00001304 // Record the position of the do while condition and make sure it is
1305 // possible to break on the condition.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001306 __ bind(loop_statement.continue_label());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001307 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +00001308 SetExpressionPosition(stmt->cond());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001309 VisitForControl(stmt->cond(),
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001310 &book_keeping,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001311 loop_statement.break_label(),
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001312 &book_keeping);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001313
1314 // Check stack before looping.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001315 PrepareForBailoutForId(stmt->BackEdgeId(), NO_REGISTERS);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001316 __ bind(&book_keeping);
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +00001317 EmitBackEdgeBookkeeping(stmt, &body);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001318 __ jmp(&body);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001319
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001320 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001321 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001322 decrement_loop_depth();
1323}
1324
1325
1326void FullCodeGenerator::VisitWhileStatement(WhileStatement* stmt) {
1327 Comment cmnt(masm_, "[ WhileStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001328 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001329
1330 Iteration loop_statement(this, stmt);
1331 increment_loop_depth();
1332
1333 // Emit the test at the bottom of the loop.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001334 __ jmp(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001335
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001336 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001337 __ bind(&body);
1338 Visit(stmt->body());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001339
1340 // Emit the statement position here as this is where the while
1341 // statement code starts.
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001342 __ bind(loop_statement.continue_label());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001343 SetStatementPosition(stmt);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001344
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001345 // Check stack before looping.
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +00001346 EmitBackEdgeBookkeeping(stmt, &body);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001347
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001348 __ bind(&test);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001349 VisitForControl(stmt->cond(),
1350 &body,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001351 loop_statement.break_label(),
1352 loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001353
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001354 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001355 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001356 decrement_loop_depth();
1357}
1358
1359
1360void FullCodeGenerator::VisitForStatement(ForStatement* stmt) {
1361 Comment cmnt(masm_, "[ ForStatement");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001362 Label test, body;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001363
1364 Iteration loop_statement(this, stmt);
erik.corry@gmail.combbceb572012-03-09 10:52:05 +00001365
1366 // Set statement position for a break slot before entering the for-body.
1367 SetStatementPosition(stmt);
1368
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001369 if (stmt->init() != NULL) {
1370 Visit(stmt->init());
1371 }
1372
1373 increment_loop_depth();
1374 // Emit the test at the bottom of the loop (even if empty).
1375 __ jmp(&test);
1376
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001377 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001378 __ bind(&body);
1379 Visit(stmt->body());
1380
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001381 PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001382 __ bind(loop_statement.continue_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001383 if (stmt->next() != NULL) {
1384 Visit(stmt->next());
1385 }
1386
ricow@chromium.org65fae842010-08-25 15:26:24 +00001387 // Emit the statement position here as this is where the for
1388 // statement code starts.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001389 SetStatementPosition(stmt);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001390
1391 // Check stack before looping.
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +00001392 EmitBackEdgeBookkeeping(stmt, &body);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001393
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001394 __ bind(&test);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001395 if (stmt->cond() != NULL) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00001396 VisitForControl(stmt->cond(),
1397 &body,
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001398 loop_statement.break_label(),
1399 loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001400 } else {
1401 __ jmp(&body);
1402 }
1403
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001404 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001405 __ bind(loop_statement.break_label());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001406 decrement_loop_depth();
1407}
1408
1409
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001410void FullCodeGenerator::VisitTryCatchStatement(TryCatchStatement* stmt) {
1411 Comment cmnt(masm_, "[ TryCatchStatement");
1412 SetStatementPosition(stmt);
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001413 // The try block adds a handler to the exception handler chain before
1414 // entering, and removes it again when exiting normally. If an exception
1415 // is thrown during execution of the try block, the handler is consumed
1416 // and control is passed to the catch block with the exception in the
1417 // result register.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001418
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001419 Label try_entry, handler_entry, exit;
1420 __ jmp(&try_entry);
1421 __ bind(&handler_entry);
1422 handler_table()->set(stmt->index(), Smi::FromInt(handler_entry.pos()));
1423 // Exception handler code, the exception is in the result register.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001424 // Extend the context before executing the catch block.
1425 { Comment cmnt(masm_, "[ Extend catch context");
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001426 __ Push(stmt->variable()->name());
danno@chromium.org59400602013-08-13 17:09:37 +00001427 __ Push(result_register());
vegorov@chromium.org3cf47312011-06-29 13:20:01 +00001428 PushFunctionArgumentForContextAllocation();
1429 __ CallRuntime(Runtime::kPushCatchContext, 3);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001430 StoreToFrameField(StandardFrameConstants::kContextOffset,
1431 context_register());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001432 }
1433
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001434 Scope* saved_scope = scope();
1435 scope_ = stmt->scope();
1436 ASSERT(scope_->declarations()->is_empty());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001437 { WithOrCatch catch_body(this);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001438 Visit(stmt->catch_block());
1439 }
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001440 // Restore the context.
1441 LoadContextField(context_register(), Context::PREVIOUS_INDEX);
1442 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001443 scope_ = saved_scope;
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001444 __ jmp(&exit);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001445
1446 // Try block code. Sets up the exception handler chain.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001447 __ bind(&try_entry);
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00001448 __ PushTryHandler(StackHandler::CATCH, stmt->index());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001449 { TryCatch try_body(this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001450 Visit(stmt->try_block());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001451 }
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001452 __ PopTryHandler();
1453 __ bind(&exit);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001454}
1455
1456
1457void FullCodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* stmt) {
1458 Comment cmnt(masm_, "[ TryFinallyStatement");
1459 SetStatementPosition(stmt);
1460 // Try finally is compiled by setting up a try-handler on the stack while
1461 // executing the try body, and removing it again afterwards.
1462 //
1463 // The try-finally construct can enter the finally block in three ways:
1464 // 1. By exiting the try-block normally. This removes the try-handler and
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001465 // calls the finally block code before continuing.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001466 // 2. By exiting the try-block with a function-local control flow transfer
1467 // (break/continue/return). The site of the, e.g., break removes the
1468 // try handler and calls the finally block code before continuing
1469 // its outward control transfer.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001470 // 3. By exiting the try-block with a thrown exception.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001471 // This can happen in nested function calls. It traverses the try-handler
1472 // chain and consumes the try-handler entry before jumping to the
1473 // handler code. The handler code then calls the finally-block before
1474 // rethrowing the exception.
1475 //
1476 // The finally block must assume a return address on top of the stack
1477 // (or in the link register on ARM chips) and a value (return value or
1478 // exception) in the result register (rax/eax/r0), both of which must
1479 // be preserved. The return address isn't GC-safe, so it should be
1480 // cooked before GC.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001481 Label try_entry, handler_entry, finally_entry;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001482
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001483 // Jump to try-handler setup and try-block code.
1484 __ jmp(&try_entry);
1485 __ bind(&handler_entry);
1486 handler_table()->set(stmt->index(), Smi::FromInt(handler_entry.pos()));
1487 // Exception handler code. This code is only executed when an exception
1488 // is thrown. The exception is in the result register, and must be
1489 // preserved by the finally block. Call the finally block and then
1490 // rethrow the exception if it returns.
1491 __ Call(&finally_entry);
danno@chromium.org59400602013-08-13 17:09:37 +00001492 __ Push(result_register());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001493 __ CallRuntime(Runtime::kReThrow, 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001494
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001495 // Finally block implementation.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001496 __ bind(&finally_entry);
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001497 EnterFinallyBlock();
1498 { Finally finally_body(this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001499 Visit(stmt->finally_block());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001500 }
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001501 ExitFinallyBlock(); // Return to the calling code.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001502
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001503 // Set up try handler.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001504 __ bind(&try_entry);
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00001505 __ PushTryHandler(StackHandler::FINALLY, stmt->index());
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001506 { TryFinally try_body(this, &finally_entry);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001507 Visit(stmt->try_block());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001508 }
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001509 __ PopTryHandler();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001510 // Execute the finally block on the way out. Clobber the unpredictable
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00001511 // value in the result register with one that's safe for GC because the
1512 // finally block will unconditionally preserve the result register on the
1513 // stack.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001514 ClearAccumulator();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001515 __ Call(&finally_entry);
1516}
1517
1518
1519void FullCodeGenerator::VisitDebuggerStatement(DebuggerStatement* stmt) {
1520#ifdef ENABLE_DEBUGGER_SUPPORT
1521 Comment cmnt(masm_, "[ DebuggerStatement");
1522 SetStatementPosition(stmt);
1523
ager@chromium.org5c838252010-02-19 08:53:10 +00001524 __ DebugBreak();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001525 // Ignore the return value.
1526#endif
1527}
1528
1529
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +00001530void FullCodeGenerator::VisitCaseClause(CaseClause* clause) {
1531 UNREACHABLE();
1532}
1533
1534
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001535void FullCodeGenerator::VisitConditional(Conditional* expr) {
1536 Comment cmnt(masm_, "[ Conditional");
1537 Label true_case, false_case, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001538 VisitForControl(expr->condition(), &true_case, &false_case, &true_case);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001539
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001540 PrepareForBailoutForId(expr->ThenId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001541 __ bind(&true_case);
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +00001542 SetExpressionPosition(expr->then_expression());
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001543 if (context()->IsTest()) {
1544 const TestContext* for_test = TestContext::cast(context());
1545 VisitForControl(expr->then_expression(),
1546 for_test->true_label(),
1547 for_test->false_label(),
1548 NULL);
1549 } else {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001550 VisitInDuplicateContext(expr->then_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001551 __ jmp(&done);
1552 }
1553
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001554 PrepareForBailoutForId(expr->ElseId(), NO_REGISTERS);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001555 __ bind(&false_case);
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +00001556 SetExpressionPosition(expr->else_expression());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001557 VisitInDuplicateContext(expr->else_expression());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001558 // If control flow falls through Visit, merge it with true case here.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001559 if (!context()->IsTest()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001560 __ bind(&done);
1561 }
1562}
1563
1564
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001565void FullCodeGenerator::VisitLiteral(Literal* expr) {
1566 Comment cmnt(masm_, "[ Literal");
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00001567 context()->Plug(expr->value());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001568}
1569
1570
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001571void FullCodeGenerator::VisitFunctionLiteral(FunctionLiteral* expr) {
1572 Comment cmnt(masm_, "[ FunctionLiteral");
1573
1574 // Build the function boilerplate and instantiate it.
1575 Handle<SharedFunctionInfo> function_info =
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001576 Compiler::BuildFunctionInfo(expr, script());
1577 if (function_info.is_null()) {
1578 SetStackOverflow();
1579 return;
1580 }
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001581 EmitNewClosure(function_info, expr->pretenure());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001582}
1583
1584
jkummerow@chromium.orgfb7a7c42013-10-02 11:41:02 +00001585void FullCodeGenerator::VisitNativeFunctionLiteral(
1586 NativeFunctionLiteral* expr) {
1587 Comment cmnt(masm_, "[ NativeFunctionLiteral");
1588
1589 // Compute the function template for the native function.
1590 Handle<String> name = expr->name();
1591 v8::Handle<v8::FunctionTemplate> fun_template =
machenbach@chromium.org37be4082013-11-26 13:50:38 +00001592 expr->extension()->GetNativeFunctionTemplate(
1593 reinterpret_cast<v8::Isolate*>(isolate()), v8::Utils::ToLocal(name));
jkummerow@chromium.orgfb7a7c42013-10-02 11:41:02 +00001594 ASSERT(!fun_template.IsEmpty());
1595
1596 // Instantiate the function and create a shared function info from it.
1597 Handle<JSFunction> fun = Utils::OpenHandle(*fun_template->GetFunction());
1598 const int literals = fun->NumberOfLiterals();
1599 Handle<Code> code = Handle<Code>(fun->shared()->code());
1600 Handle<Code> construct_stub = Handle<Code>(fun->shared()->construct_stub());
1601 bool is_generator = false;
1602 Handle<SharedFunctionInfo> shared =
1603 isolate()->factory()->NewSharedFunctionInfo(name, literals, is_generator,
1604 code, Handle<ScopeInfo>(fun->shared()->scope_info()));
1605 shared->set_construct_stub(*construct_stub);
1606
1607 // Copy the function data to the shared function info.
1608 shared->set_function_data(fun->shared()->function_data());
1609 int parameters = fun->shared()->formal_parameter_count();
1610 shared->set_formal_parameter_count(parameters);
1611
1612 EmitNewClosure(shared, false);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001613}
1614
1615
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001616void FullCodeGenerator::VisitThrow(Throw* expr) {
1617 Comment cmnt(masm_, "[ Throw");
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001618 VisitForStackValue(expr->exception());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001619 __ CallRuntime(Runtime::kThrow, 1);
1620 // Never returns here.
1621}
1622
1623
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001624FullCodeGenerator::NestedStatement* FullCodeGenerator::TryCatch::Exit(
1625 int* stack_depth,
1626 int* context_length) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001627 // The macros used here must preserve the result register.
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001628 __ Drop(*stack_depth);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001629 __ PopTryHandler();
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001630 *stack_depth = 0;
1631 return previous_;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001632}
1633
ricow@chromium.org65fae842010-08-25 15:26:24 +00001634
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001635bool FullCodeGenerator::TryLiteralCompare(CompareOperation* expr) {
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001636 Expression* sub_expr;
ager@chromium.org04921a82011-06-27 13:21:41 +00001637 Handle<String> check;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001638 if (expr->IsLiteralCompareTypeof(&sub_expr, &check)) {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001639 EmitLiteralCompareTypeof(expr, sub_expr, check);
ager@chromium.org04921a82011-06-27 13:21:41 +00001640 return true;
1641 }
1642
jkummerow@chromium.org96a3c512013-07-18 17:02:47 +00001643 if (expr->IsLiteralCompareUndefined(&sub_expr, isolate())) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001644 EmitLiteralCompareNil(expr, sub_expr, kUndefinedValue);
1645 return true;
1646 }
1647
1648 if (expr->IsLiteralCompareNull(&sub_expr)) {
1649 EmitLiteralCompareNil(expr, sub_expr, kNullValue);
ager@chromium.org04921a82011-06-27 13:21:41 +00001650 return true;
1651 }
1652
1653 return false;
1654}
1655
1656
yangguo@chromium.org49546742013-12-23 16:17:49 +00001657void BackEdgeTable::Patch(Isolate* isolate, Code* unoptimized) {
machenbach@chromium.org528ce022013-09-23 14:09:36 +00001658 DisallowHeapAllocation no_gc;
machenbach@chromium.org8e36b5b2013-09-26 07:36:30 +00001659 Code* patch = isolate->builtins()->builtin(Builtins::kOnStackReplacement);
machenbach@chromium.org528ce022013-09-23 14:09:36 +00001660
1661 // Iterate over the back edge table and patch every interrupt
1662 // call to an unconditional call to the replacement code.
1663 int loop_nesting_level = unoptimized->allow_osr_at_loop_nesting_level();
1664
1665 BackEdgeTable back_edges(unoptimized, &no_gc);
1666 for (uint32_t i = 0; i < back_edges.length(); i++) {
1667 if (static_cast<int>(back_edges.loop_depth(i)) == loop_nesting_level) {
1668 ASSERT_EQ(INTERRUPT, GetBackEdgeState(isolate,
1669 unoptimized,
1670 back_edges.pc(i)));
machenbach@chromium.org8e36b5b2013-09-26 07:36:30 +00001671 PatchAt(unoptimized, back_edges.pc(i), ON_STACK_REPLACEMENT, patch);
machenbach@chromium.org528ce022013-09-23 14:09:36 +00001672 }
1673 }
1674
1675 unoptimized->set_back_edges_patched_for_osr(true);
1676 ASSERT(Verify(isolate, unoptimized, loop_nesting_level));
1677}
1678
1679
yangguo@chromium.org49546742013-12-23 16:17:49 +00001680void BackEdgeTable::Revert(Isolate* isolate, Code* unoptimized) {
machenbach@chromium.org528ce022013-09-23 14:09:36 +00001681 DisallowHeapAllocation no_gc;
machenbach@chromium.org8e36b5b2013-09-26 07:36:30 +00001682 Code* patch = isolate->builtins()->builtin(Builtins::kInterruptCheck);
machenbach@chromium.org528ce022013-09-23 14:09:36 +00001683
1684 // Iterate over the back edge table and revert the patched interrupt calls.
1685 ASSERT(unoptimized->back_edges_patched_for_osr());
1686 int loop_nesting_level = unoptimized->allow_osr_at_loop_nesting_level();
1687
1688 BackEdgeTable back_edges(unoptimized, &no_gc);
1689 for (uint32_t i = 0; i < back_edges.length(); i++) {
1690 if (static_cast<int>(back_edges.loop_depth(i)) <= loop_nesting_level) {
machenbach@chromium.org8e36b5b2013-09-26 07:36:30 +00001691 ASSERT_NE(INTERRUPT, GetBackEdgeState(isolate,
1692 unoptimized,
1693 back_edges.pc(i)));
1694 PatchAt(unoptimized, back_edges.pc(i), INTERRUPT, patch);
machenbach@chromium.org528ce022013-09-23 14:09:36 +00001695 }
1696 }
1697
1698 unoptimized->set_back_edges_patched_for_osr(false);
1699 unoptimized->set_allow_osr_at_loop_nesting_level(0);
1700 // Assert that none of the back edges are patched anymore.
1701 ASSERT(Verify(isolate, unoptimized, -1));
1702}
1703
1704
yangguo@chromium.org49546742013-12-23 16:17:49 +00001705void BackEdgeTable::AddStackCheck(Handle<Code> code, uint32_t pc_offset) {
machenbach@chromium.org8e36b5b2013-09-26 07:36:30 +00001706 DisallowHeapAllocation no_gc;
yangguo@chromium.org49546742013-12-23 16:17:49 +00001707 Isolate* isolate = code->GetIsolate();
1708 Address pc = code->instruction_start() + pc_offset;
machenbach@chromium.org8e36b5b2013-09-26 07:36:30 +00001709 Code* patch = isolate->builtins()->builtin(Builtins::kOsrAfterStackCheck);
yangguo@chromium.org49546742013-12-23 16:17:49 +00001710 PatchAt(*code, pc, OSR_AFTER_STACK_CHECK, patch);
machenbach@chromium.org8e36b5b2013-09-26 07:36:30 +00001711}
1712
1713
yangguo@chromium.org49546742013-12-23 16:17:49 +00001714void BackEdgeTable::RemoveStackCheck(Handle<Code> code, uint32_t pc_offset) {
machenbach@chromium.org8e36b5b2013-09-26 07:36:30 +00001715 DisallowHeapAllocation no_gc;
yangguo@chromium.org49546742013-12-23 16:17:49 +00001716 Isolate* isolate = code->GetIsolate();
1717 Address pc = code->instruction_start() + pc_offset;
1718
1719 if (OSR_AFTER_STACK_CHECK == GetBackEdgeState(isolate, *code, pc)) {
machenbach@chromium.org8e36b5b2013-09-26 07:36:30 +00001720 Code* patch = isolate->builtins()->builtin(Builtins::kOnStackReplacement);
yangguo@chromium.org49546742013-12-23 16:17:49 +00001721 PatchAt(*code, pc, ON_STACK_REPLACEMENT, patch);
machenbach@chromium.org8e36b5b2013-09-26 07:36:30 +00001722 }
1723}
1724
1725
machenbach@chromium.org528ce022013-09-23 14:09:36 +00001726#ifdef DEBUG
1727bool BackEdgeTable::Verify(Isolate* isolate,
1728 Code* unoptimized,
1729 int loop_nesting_level) {
1730 DisallowHeapAllocation no_gc;
1731 BackEdgeTable back_edges(unoptimized, &no_gc);
1732 for (uint32_t i = 0; i < back_edges.length(); i++) {
1733 uint32_t loop_depth = back_edges.loop_depth(i);
1734 CHECK_LE(static_cast<int>(loop_depth), Code::kMaxLoopNestingMarker);
1735 // Assert that all back edges for shallower loops (and only those)
1736 // have already been patched.
1737 CHECK_EQ((static_cast<int>(loop_depth) <= loop_nesting_level),
1738 GetBackEdgeState(isolate,
1739 unoptimized,
1740 back_edges.pc(i)) != INTERRUPT);
1741 }
1742 return true;
1743}
1744#endif // DEBUG
1745
1746
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001747#undef __
1748
1749
1750} } // namespace v8::internal