OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/v8.h" | 5 #include "src/v8.h" |
6 | 6 |
7 #include "src/wasm/asm-wasm-builder.h" | 7 #include "src/wasm/asm-wasm-builder.h" |
8 #include "src/wasm/wasm-macro-gen.h" | 8 #include "src/wasm/wasm-macro-gen.h" |
9 #include "src/wasm/wasm-opcodes.h" | 9 #include "src/wasm/wasm-opcodes.h" |
10 | 10 |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 ExpressionStatement* expr = | 85 ExpressionStatement* expr = |
86 stmt->statements()->at(0)->AsExpressionStatement(); | 86 stmt->statements()->at(0)->AsExpressionStatement(); |
87 if (expr != nullptr) { | 87 if (expr != nullptr) { |
88 if (expr->expression()->IsAssignment()) { | 88 if (expr->expression()->IsAssignment()) { |
89 RECURSE(VisitExpressionStatement(expr)); | 89 RECURSE(VisitExpressionStatement(expr)); |
90 return; | 90 return; |
91 } | 91 } |
92 } | 92 } |
93 } | 93 } |
94 DCHECK(in_function_); | 94 DCHECK(in_function_); |
95 BlockVisitor visitor(this, stmt->AsBreakableStatement(), kExprBlock, false); | 95 BlockVisitor visitor(this, stmt->AsBreakableStatement(), kExprBlock, false, |
96 block_size_ = static_cast<byte>(stmt->statements()->length()); | 96 static_cast<byte>(stmt->statements()->length())); |
97 RECURSE(VisitStatements(stmt->statements())); | 97 RECURSE(VisitStatements(stmt->statements())); |
98 DCHECK(block_size_ >= 0); | 98 DCHECK(block_size_ >= 0); |
99 } | 99 } |
100 | 100 |
101 class BlockVisitor { | 101 class BlockVisitor { |
102 private: | 102 private: |
103 int prev_block_size_; | 103 int prev_block_size_; |
104 uint32_t index_; | 104 uint32_t index_; |
105 AsmWasmBuilderImpl* builder_; | 105 AsmWasmBuilderImpl* builder_; |
106 | 106 |
107 public: | 107 public: |
108 BlockVisitor(AsmWasmBuilderImpl* builder, BreakableStatement* stmt, | 108 BlockVisitor(AsmWasmBuilderImpl* builder, BreakableStatement* stmt, |
109 WasmOpcode opcode, bool is_loop) | 109 WasmOpcode opcode, bool is_loop, int initial_block_size) |
110 : builder_(builder) { | 110 : builder_(builder) { |
111 builder_->breakable_blocks_.push_back(std::make_pair(stmt, is_loop)); | 111 builder_->breakable_blocks_.push_back(std::make_pair(stmt, is_loop)); |
112 builder_->current_function_builder_->Emit(opcode); | 112 builder_->current_function_builder_->Emit(opcode); |
113 index_ = builder_->current_function_builder_->EmitEditableImmediate(0); | 113 index_ = builder_->current_function_builder_->EmitEditableImmediate(0); |
114 prev_block_size_ = builder_->block_size_; | 114 prev_block_size_ = builder_->block_size_; |
| 115 builder_->block_size_ = initial_block_size; |
115 } | 116 } |
116 ~BlockVisitor() { | 117 ~BlockVisitor() { |
117 builder_->current_function_builder_->EditImmediate(index_, | 118 builder_->current_function_builder_->EditImmediate(index_, |
118 builder_->block_size_); | 119 builder_->block_size_); |
119 builder_->block_size_ = prev_block_size_; | 120 builder_->block_size_ = prev_block_size_; |
120 builder_->breakable_blocks_.pop_back(); | 121 builder_->breakable_blocks_.pop_back(); |
121 } | 122 } |
122 }; | 123 }; |
123 | 124 |
124 void VisitExpressionStatement(ExpressionStatement* stmt) { | 125 void VisitExpressionStatement(ExpressionStatement* stmt) { |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
218 Literal* label = clause->label()->AsLiteral(); | 219 Literal* label = clause->label()->AsLiteral(); |
219 DCHECK(label != nullptr); | 220 DCHECK(label != nullptr); |
220 block_size_++; | 221 block_size_++; |
221 current_function_builder_->Emit(kExprIf); | 222 current_function_builder_->Emit(kExprIf); |
222 current_function_builder_->Emit(kExprI32Ior); | 223 current_function_builder_->Emit(kExprI32Ior); |
223 current_function_builder_->Emit(kExprI32Eq); | 224 current_function_builder_->Emit(kExprI32Eq); |
224 VisitVariableProxy(tag); | 225 VisitVariableProxy(tag); |
225 VisitLiteral(label); | 226 VisitLiteral(label); |
226 current_function_builder_->Emit(kExprGetLocal); | 227 current_function_builder_->Emit(kExprGetLocal); |
227 AddLeb128(fall_through, true); | 228 AddLeb128(fall_through, true); |
228 BlockVisitor visitor(this, nullptr, kExprBlock, false); | 229 BlockVisitor visitor(this, nullptr, kExprBlock, false, 0); |
229 block_size_ = 0; | |
230 SetLocalTo(fall_through, 1); | 230 SetLocalTo(fall_through, 1); |
231 ZoneList<Statement*>* stmts = clause->statements(); | 231 ZoneList<Statement*>* stmts = clause->statements(); |
232 block_size_ += stmts->length(); | 232 block_size_ += stmts->length(); |
233 RECURSE(VisitStatements(stmts)); | 233 RECURSE(VisitStatements(stmts)); |
234 } | 234 } |
235 | 235 |
236 void VisitSwitchStatement(SwitchStatement* stmt) { | 236 void VisitSwitchStatement(SwitchStatement* stmt) { |
237 VariableProxy* tag = stmt->tag()->AsVariableProxy(); | 237 VariableProxy* tag = stmt->tag()->AsVariableProxy(); |
238 DCHECK(tag != NULL); | 238 DCHECK(tag != NULL); |
239 BlockVisitor visitor(this, stmt->AsBreakableStatement(), kExprBlock, false); | 239 BlockVisitor visitor(this, stmt->AsBreakableStatement(), kExprBlock, false, |
240 block_size_ = 0; | 240 0); |
241 uint16_t fall_through = current_function_builder_->AddLocal(kAstI32); | 241 uint16_t fall_through = current_function_builder_->AddLocal(kAstI32); |
242 SetLocalTo(fall_through, 0); | 242 SetLocalTo(fall_through, 0); |
243 | 243 |
244 ZoneList<CaseClause*>* clauses = stmt->cases(); | 244 ZoneList<CaseClause*>* clauses = stmt->cases(); |
245 for (int i = 0; i < clauses->length(); ++i) { | 245 for (int i = 0; i < clauses->length(); ++i) { |
246 CaseClause* clause = clauses->at(i); | 246 CaseClause* clause = clauses->at(i); |
247 if (!clause->is_default()) { | 247 if (!clause->is_default()) { |
248 CompileCase(clause, fall_through, tag); | 248 CompileCase(clause, fall_through, tag); |
249 } else { | 249 } else { |
250 ZoneList<Statement*>* stmts = clause->statements(); | 250 ZoneList<Statement*>* stmts = clause->statements(); |
251 block_size_ += stmts->length(); | 251 block_size_ += stmts->length(); |
252 RECURSE(VisitStatements(stmts)); | 252 RECURSE(VisitStatements(stmts)); |
253 } | 253 } |
254 } | 254 } |
255 } | 255 } |
256 | 256 |
257 void VisitCaseClause(CaseClause* clause) { UNREACHABLE(); } | 257 void VisitCaseClause(CaseClause* clause) { UNREACHABLE(); } |
258 | 258 |
259 void VisitDoWhileStatement(DoWhileStatement* stmt) { | 259 void VisitDoWhileStatement(DoWhileStatement* stmt) { |
260 DCHECK(in_function_); | 260 DCHECK(in_function_); |
261 BlockVisitor visitor(this, stmt->AsBreakableStatement(), kExprLoop, true); | 261 BlockVisitor visitor(this, stmt->AsBreakableStatement(), kExprLoop, true, |
262 block_size_ = 2; | 262 2); |
263 RECURSE(Visit(stmt->body())); | 263 RECURSE(Visit(stmt->body())); |
264 current_function_builder_->Emit(kExprIf); | 264 current_function_builder_->Emit(kExprIf); |
265 RECURSE(Visit(stmt->cond())); | 265 RECURSE(Visit(stmt->cond())); |
266 current_function_builder_->EmitWithU8(kExprBr, 0); | 266 current_function_builder_->EmitWithU8(kExprBr, 0); |
267 current_function_builder_->Emit(kExprNop); | 267 current_function_builder_->Emit(kExprNop); |
268 } | 268 } |
269 | 269 |
270 void VisitWhileStatement(WhileStatement* stmt) { | 270 void VisitWhileStatement(WhileStatement* stmt) { |
271 DCHECK(in_function_); | 271 DCHECK(in_function_); |
272 BlockVisitor visitor(this, stmt->AsBreakableStatement(), kExprLoop, true); | 272 BlockVisitor visitor(this, stmt->AsBreakableStatement(), kExprLoop, true, |
273 block_size_ = 1; | 273 1); |
274 current_function_builder_->Emit(kExprIf); | 274 current_function_builder_->Emit(kExprIf); |
275 RECURSE(Visit(stmt->cond())); | 275 RECURSE(Visit(stmt->cond())); |
276 current_function_builder_->EmitWithU8(kExprBr, 0); | 276 current_function_builder_->EmitWithU8(kExprBr, 0); |
277 RECURSE(Visit(stmt->body())); | 277 RECURSE(Visit(stmt->body())); |
278 } | 278 } |
279 | 279 |
280 void VisitForStatement(ForStatement* stmt) { | 280 void VisitForStatement(ForStatement* stmt) { |
281 DCHECK(in_function_); | 281 DCHECK(in_function_); |
282 if (stmt->init() != nullptr) { | 282 if (stmt->init() != nullptr) { |
283 block_size_++; | 283 block_size_++; |
284 RECURSE(Visit(stmt->init())); | 284 RECURSE(Visit(stmt->init())); |
285 } | 285 } |
286 BlockVisitor visitor(this, stmt->AsBreakableStatement(), kExprLoop, true); | 286 BlockVisitor visitor(this, stmt->AsBreakableStatement(), kExprLoop, true, |
287 block_size_ = 0; | 287 0); |
288 if (stmt->cond() != nullptr) { | 288 if (stmt->cond() != nullptr) { |
289 block_size_++; | 289 block_size_++; |
290 current_function_builder_->Emit(kExprIf); | 290 current_function_builder_->Emit(kExprIf); |
291 current_function_builder_->Emit(kExprBoolNot); | 291 current_function_builder_->Emit(kExprBoolNot); |
292 RECURSE(Visit(stmt->cond())); | 292 RECURSE(Visit(stmt->cond())); |
293 current_function_builder_->EmitWithU8(kExprBr, 1); | 293 current_function_builder_->EmitWithU8(kExprBr, 1); |
294 current_function_builder_->Emit(kExprNop); | 294 current_function_builder_->Emit(kExprNop); |
295 } | 295 } |
296 if (stmt->body() != nullptr) { | 296 if (stmt->body() != nullptr) { |
297 block_size_++; | 297 block_size_++; |
(...skipping 732 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1030 // that zone in constructor may be thrown away once wasm module is written. | 1030 // that zone in constructor may be thrown away once wasm module is written. |
1031 WasmModuleIndex* AsmWasmBuilder::Run() { | 1031 WasmModuleIndex* AsmWasmBuilder::Run() { |
1032 AsmWasmBuilderImpl impl(isolate_, zone_, literal_); | 1032 AsmWasmBuilderImpl impl(isolate_, zone_, literal_); |
1033 impl.Compile(); | 1033 impl.Compile(); |
1034 WasmModuleWriter* writer = impl.builder_->Build(zone_); | 1034 WasmModuleWriter* writer = impl.builder_->Build(zone_); |
1035 return writer->WriteTo(zone_); | 1035 return writer->WriteTo(zone_); |
1036 } | 1036 } |
1037 } // namespace wasm | 1037 } // namespace wasm |
1038 } // namespace internal | 1038 } // namespace internal |
1039 } // namespace v8 | 1039 } // namespace v8 |
OLD | NEW |