| 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 != NULL) { | 87 if (expr != NULL) { |
| 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 breakable_blocks_.push_back( | 95 BlockVisitor visitor(this, stmt->AsBreakableStatement(), kExprBlock, false); |
| 96 std::make_pair(stmt->AsBreakableStatement(), false)); | |
| 97 current_function_builder_->Emit(kExprBlock); | |
| 98 uint32_t index = current_function_builder_->EmitEditableImmediate(0); | |
| 99 int prev_block_size = block_size_; | |
| 100 block_size_ = static_cast<byte>(stmt->statements()->length()); | 96 block_size_ = static_cast<byte>(stmt->statements()->length()); |
| 101 RECURSE(VisitStatements(stmt->statements())); | 97 RECURSE(VisitStatements(stmt->statements())); |
| 102 DCHECK(block_size_ >= 0); | 98 DCHECK(block_size_ >= 0); |
| 103 current_function_builder_->EditImmediate(index, block_size_); | |
| 104 block_size_ = prev_block_size; | |
| 105 breakable_blocks_.pop_back(); | |
| 106 } | 99 } |
| 107 | 100 |
| 101 class BlockVisitor { |
| 102 private: |
| 103 int prev_block_size_; |
| 104 uint32_t index_; |
| 105 AsmWasmBuilderImpl* builder_; |
| 106 |
| 107 public: |
| 108 BlockVisitor(AsmWasmBuilderImpl* builder, BreakableStatement* stmt, |
| 109 WasmOpcode opcode, bool is_loop) |
| 110 : builder_(builder) { |
| 111 builder_->breakable_blocks_.push_back(std::make_pair(stmt, is_loop)); |
| 112 builder_->current_function_builder_->Emit(opcode); |
| 113 index_ = builder_->current_function_builder_->EmitEditableImmediate(0); |
| 114 prev_block_size_ = builder_->block_size_; |
| 115 } |
| 116 ~BlockVisitor() { |
| 117 builder_->current_function_builder_->EditImmediate(index_, |
| 118 builder_->block_size_); |
| 119 builder_->block_size_ = prev_block_size_; |
| 120 builder_->breakable_blocks_.pop_back(); |
| 121 } |
| 122 }; |
| 123 |
| 108 void VisitExpressionStatement(ExpressionStatement* stmt) { | 124 void VisitExpressionStatement(ExpressionStatement* stmt) { |
| 109 RECURSE(Visit(stmt->expression())); | 125 RECURSE(Visit(stmt->expression())); |
| 110 } | 126 } |
| 111 | 127 |
| 112 void VisitEmptyStatement(EmptyStatement* stmt) {} | 128 void VisitEmptyStatement(EmptyStatement* stmt) {} |
| 113 | 129 |
| 114 void VisitEmptyParentheses(EmptyParentheses* paren) { UNREACHABLE(); } | 130 void VisitEmptyParentheses(EmptyParentheses* paren) { UNREACHABLE(); } |
| 115 | 131 |
| 116 void VisitIfStatement(IfStatement* stmt) { | 132 void VisitIfStatement(IfStatement* stmt) { |
| 117 DCHECK(in_function_); | 133 DCHECK(in_function_); |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 } | 216 } |
| 201 ZoneList<Statement*>* stmts = clause->statements(); | 217 ZoneList<Statement*>* stmts = clause->statements(); |
| 202 RECURSE(VisitStatements(stmts)); | 218 RECURSE(VisitStatements(stmts)); |
| 203 } | 219 } |
| 204 } | 220 } |
| 205 | 221 |
| 206 void VisitCaseClause(CaseClause* clause) { UNREACHABLE(); } | 222 void VisitCaseClause(CaseClause* clause) { UNREACHABLE(); } |
| 207 | 223 |
| 208 void VisitDoWhileStatement(DoWhileStatement* stmt) { | 224 void VisitDoWhileStatement(DoWhileStatement* stmt) { |
| 209 DCHECK(in_function_); | 225 DCHECK(in_function_); |
| 210 current_function_builder_->Emit(kExprLoop); | 226 BlockVisitor visitor(this, stmt->AsBreakableStatement(), kExprLoop, true); |
| 211 uint32_t index = current_function_builder_->EmitEditableImmediate(0); | 227 block_size_ = 2; |
| 212 int prev_block_size = block_size_; | |
| 213 block_size_ = 0; | |
| 214 breakable_blocks_.push_back( | |
| 215 std::make_pair(stmt->AsBreakableStatement(), true)); | |
| 216 block_size_++; | |
| 217 RECURSE(Visit(stmt->body())); | 228 RECURSE(Visit(stmt->body())); |
| 218 block_size_++; | |
| 219 current_function_builder_->Emit(kExprIf); | 229 current_function_builder_->Emit(kExprIf); |
| 220 RECURSE(Visit(stmt->cond())); | 230 RECURSE(Visit(stmt->cond())); |
| 221 current_function_builder_->EmitWithU8(kExprBr, 0); | 231 current_function_builder_->EmitWithU8(kExprBr, 0); |
| 222 current_function_builder_->Emit(kExprNop); | 232 current_function_builder_->Emit(kExprNop); |
| 223 current_function_builder_->EditImmediate(index, block_size_); | |
| 224 block_size_ = prev_block_size; | |
| 225 breakable_blocks_.pop_back(); | |
| 226 } | 233 } |
| 227 | 234 |
| 228 void VisitWhileStatement(WhileStatement* stmt) { | 235 void VisitWhileStatement(WhileStatement* stmt) { |
| 229 DCHECK(in_function_); | 236 DCHECK(in_function_); |
| 230 current_function_builder_->EmitWithU8(kExprLoop, 1); | 237 BlockVisitor visitor(this, stmt->AsBreakableStatement(), kExprLoop, true); |
| 231 breakable_blocks_.push_back( | 238 block_size_ = 1; |
| 232 std::make_pair(stmt->AsBreakableStatement(), true)); | |
| 233 current_function_builder_->Emit(kExprIf); | 239 current_function_builder_->Emit(kExprIf); |
| 234 RECURSE(Visit(stmt->cond())); | 240 RECURSE(Visit(stmt->cond())); |
| 235 current_function_builder_->EmitWithU8(kExprBr, 0); | 241 current_function_builder_->EmitWithU8(kExprBr, 0); |
| 236 RECURSE(Visit(stmt->body())); | 242 RECURSE(Visit(stmt->body())); |
| 237 breakable_blocks_.pop_back(); | |
| 238 } | 243 } |
| 239 | 244 |
| 240 void VisitForStatement(ForStatement* stmt) { | 245 void VisitForStatement(ForStatement* stmt) { |
| 241 DCHECK(in_function_); | 246 DCHECK(in_function_); |
| 242 if (stmt->init() != NULL) { | 247 if (stmt->init() != NULL) { |
| 243 block_size_++; | 248 block_size_++; |
| 244 RECURSE(Visit(stmt->init())); | 249 RECURSE(Visit(stmt->init())); |
| 245 } | 250 } |
| 246 current_function_builder_->Emit(kExprLoop); | 251 BlockVisitor visitor(this, stmt->AsBreakableStatement(), kExprLoop, true); |
| 247 uint32_t index = current_function_builder_->EmitEditableImmediate(0); | |
| 248 int prev_block_size = block_size_; | |
| 249 block_size_ = 0; | 252 block_size_ = 0; |
| 250 breakable_blocks_.push_back( | |
| 251 std::make_pair(stmt->AsBreakableStatement(), true)); | |
| 252 if (stmt->cond() != NULL) { | 253 if (stmt->cond() != NULL) { |
| 253 block_size_++; | 254 block_size_++; |
| 254 current_function_builder_->Emit(kExprIf); | 255 current_function_builder_->Emit(kExprIf); |
| 255 current_function_builder_->Emit(kExprBoolNot); | 256 current_function_builder_->Emit(kExprBoolNot); |
| 256 RECURSE(Visit(stmt->cond())); | 257 RECURSE(Visit(stmt->cond())); |
| 257 current_function_builder_->EmitWithU8(kExprBr, 1); | 258 current_function_builder_->EmitWithU8(kExprBr, 1); |
| 258 current_function_builder_->Emit(kExprNop); | 259 current_function_builder_->Emit(kExprNop); |
| 259 } | 260 } |
| 260 if (stmt->body() != NULL) { | 261 if (stmt->body() != NULL) { |
| 261 block_size_++; | 262 block_size_++; |
| 262 RECURSE(Visit(stmt->body())); | 263 RECURSE(Visit(stmt->body())); |
| 263 } | 264 } |
| 264 if (stmt->next() != NULL) { | 265 if (stmt->next() != NULL) { |
| 265 block_size_++; | 266 block_size_++; |
| 266 RECURSE(Visit(stmt->next())); | 267 RECURSE(Visit(stmt->next())); |
| 267 } | 268 } |
| 268 block_size_++; | 269 block_size_++; |
| 269 current_function_builder_->EmitWithU8(kExprBr, 0); | 270 current_function_builder_->EmitWithU8(kExprBr, 0); |
| 270 current_function_builder_->Emit(kExprNop); | 271 current_function_builder_->Emit(kExprNop); |
| 271 current_function_builder_->EditImmediate(index, block_size_); | |
| 272 block_size_ = prev_block_size; | |
| 273 breakable_blocks_.pop_back(); | |
| 274 } | 272 } |
| 275 | 273 |
| 276 void VisitForInStatement(ForInStatement* stmt) { UNREACHABLE(); } | 274 void VisitForInStatement(ForInStatement* stmt) { UNREACHABLE(); } |
| 277 | 275 |
| 278 void VisitForOfStatement(ForOfStatement* stmt) { UNREACHABLE(); } | 276 void VisitForOfStatement(ForOfStatement* stmt) { UNREACHABLE(); } |
| 279 | 277 |
| 280 void VisitTryCatchStatement(TryCatchStatement* stmt) { UNREACHABLE(); } | 278 void VisitTryCatchStatement(TryCatchStatement* stmt) { UNREACHABLE(); } |
| 281 | 279 |
| 282 void VisitTryFinallyStatement(TryFinallyStatement* stmt) { UNREACHABLE(); } | 280 void VisitTryFinallyStatement(TryFinallyStatement* stmt) { UNREACHABLE(); } |
| 283 | 281 |
| (...skipping 716 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1000 // that zone in constructor may be thrown away once wasm module is written. | 998 // that zone in constructor may be thrown away once wasm module is written. |
| 1001 WasmModuleIndex* AsmWasmBuilder::Run() { | 999 WasmModuleIndex* AsmWasmBuilder::Run() { |
| 1002 AsmWasmBuilderImpl impl(isolate_, zone_, literal_); | 1000 AsmWasmBuilderImpl impl(isolate_, zone_, literal_); |
| 1003 impl.Compile(); | 1001 impl.Compile(); |
| 1004 WasmModuleWriter* writer = impl.builder_->Build(zone_); | 1002 WasmModuleWriter* writer = impl.builder_->Build(zone_); |
| 1005 return writer->WriteTo(zone_); | 1003 return writer->WriteTo(zone_); |
| 1006 } | 1004 } |
| 1007 } // namespace wasm | 1005 } // namespace wasm |
| 1008 } // namespace internal | 1006 } // namespace internal |
| 1009 } // namespace v8 | 1007 } // namespace v8 |
| OLD | NEW |