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 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
142 } else { | 142 } else { |
143 current_function_builder_->Emit(kExprNop); | 143 current_function_builder_->Emit(kExprNop); |
144 } | 144 } |
145 if (stmt->HasElseStatement()) { | 145 if (stmt->HasElseStatement()) { |
146 RECURSE(Visit(stmt->else_statement())); | 146 RECURSE(Visit(stmt->else_statement())); |
147 } | 147 } |
148 } | 148 } |
149 | 149 |
150 void VisitContinueStatement(ContinueStatement* stmt) { | 150 void VisitContinueStatement(ContinueStatement* stmt) { |
151 DCHECK(in_function_); | 151 DCHECK(in_function_); |
152 DCHECK(stmt->target() != NULL); | |
bradnelson
2016/01/12 01:06:43
Is this actually guaranteed?
aseemgarg
2016/01/12 01:38:37
yes
| |
152 int i = static_cast<int>(breakable_blocks_.size()) - 1; | 153 int i = static_cast<int>(breakable_blocks_.size()) - 1; |
153 int block_distance = 0; | 154 int block_distance = 0; |
154 for (; i >= 0; i--) { | 155 for (; i >= 0; i--) { |
155 auto elem = breakable_blocks_.at(i); | 156 auto elem = breakable_blocks_.at(i); |
156 if (elem.first == stmt->target()) { | 157 if (elem.first == stmt->target()) { |
157 DCHECK(elem.second); | 158 DCHECK(elem.second); |
158 break; | 159 break; |
159 } else if (elem.second) { | 160 } else if (elem.second) { |
160 block_distance += 2; | 161 block_distance += 2; |
161 } else { | 162 } else { |
162 block_distance += 1; | 163 block_distance += 1; |
163 } | 164 } |
164 } | 165 } |
165 DCHECK(i >= 0); | 166 DCHECK(i >= 0); |
166 current_function_builder_->EmitWithU8(kExprBr, block_distance); | 167 current_function_builder_->EmitWithU8(kExprBr, block_distance); |
167 current_function_builder_->Emit(kExprNop); | 168 current_function_builder_->Emit(kExprNop); |
168 } | 169 } |
169 | 170 |
170 void VisitBreakStatement(BreakStatement* stmt) { | 171 void VisitBreakStatement(BreakStatement* stmt) { |
171 DCHECK(in_function_); | 172 DCHECK(in_function_); |
173 DCHECK(stmt->target() != NULL); | |
172 int i = static_cast<int>(breakable_blocks_.size()) - 1; | 174 int i = static_cast<int>(breakable_blocks_.size()) - 1; |
173 int block_distance = 0; | 175 int block_distance = 0; |
174 for (; i >= 0; i--) { | 176 for (; i >= 0; i--) { |
175 auto elem = breakable_blocks_.at(i); | 177 auto elem = breakable_blocks_.at(i); |
176 if (elem.first == stmt->target()) { | 178 if (elem.first == stmt->target()) { |
177 if (elem.second) { | 179 if (elem.second) { |
178 block_distance++; | 180 block_distance++; |
179 } | 181 } |
180 break; | 182 break; |
181 } else if (elem.second) { | 183 } else if (elem.second) { |
(...skipping 14 matching lines...) Expand all Loading... | |
196 marking_exported = true; | 198 marking_exported = true; |
197 } | 199 } |
198 RECURSE(Visit(stmt->expression())); | 200 RECURSE(Visit(stmt->expression())); |
199 if (!in_function_) { | 201 if (!in_function_) { |
200 marking_exported = false; | 202 marking_exported = false; |
201 } | 203 } |
202 } | 204 } |
203 | 205 |
204 void VisitWithStatement(WithStatement* stmt) { UNREACHABLE(); } | 206 void VisitWithStatement(WithStatement* stmt) { UNREACHABLE(); } |
205 | 207 |
208 void SetLocalTo(uint16_t index, int value) { | |
209 current_function_builder_->Emit(kExprSetLocal); | |
210 AddLeb128(index, true); | |
211 byte code[] = {WASM_I32(value)}; | |
212 current_function_builder_->EmitCode(code, sizeof(code)); | |
213 block_size_++; | |
214 } | |
215 | |
216 void CompileCase(CaseClause* clause, uint16_t fall_through, | |
217 VariableProxy* tag) { | |
218 Literal* label = clause->label()->AsLiteral(); | |
219 DCHECK(label != nullptr); | |
220 block_size_++; | |
221 current_function_builder_->Emit(kExprIf); | |
222 current_function_builder_->Emit(kExprI32Ior); | |
223 current_function_builder_->Emit(kExprI32Eq); | |
224 VisitVariableProxy(tag); | |
225 VisitLiteral(label); | |
226 current_function_builder_->Emit(kExprGetLocal); | |
227 AddLeb128(fall_through, true); | |
228 BlockVisitor visitor(this, nullptr, kExprBlock, false); | |
229 block_size_ = 0; | |
230 SetLocalTo(fall_through, 1); | |
231 ZoneList<Statement*>* stmts = clause->statements(); | |
232 block_size_ += stmts->length(); | |
233 RECURSE(VisitStatements(stmts)); | |
234 } | |
235 | |
206 void VisitSwitchStatement(SwitchStatement* stmt) { | 236 void VisitSwitchStatement(SwitchStatement* stmt) { |
207 RECURSE(Visit(stmt->tag())); | 237 VariableProxy* tag = stmt->tag()->AsVariableProxy(); |
238 DCHECK(tag != NULL); | |
239 BlockVisitor visitor(this, stmt->AsBreakableStatement(), kExprBlock, false); | |
bradnelson
2016/01/12 01:06:43
I wonder if folding the block_size_ = 0 into the v
aseemgarg
2016/01/12 01:38:37
Will take care in next commit
| |
240 block_size_ = 0; | |
241 uint16_t fall_through = current_function_builder_->AddLocal(kAstI32); | |
242 SetLocalTo(fall_through, 0); | |
208 | 243 |
209 ZoneList<CaseClause*>* clauses = stmt->cases(); | 244 ZoneList<CaseClause*>* clauses = stmt->cases(); |
210 | |
211 for (int i = 0; i < clauses->length(); ++i) { | 245 for (int i = 0; i < clauses->length(); ++i) { |
212 CaseClause* clause = clauses->at(i); | 246 CaseClause* clause = clauses->at(i); |
213 if (!clause->is_default()) { | 247 if (!clause->is_default()) { |
214 Expression* label = clause->label(); | 248 CompileCase(clause, fall_through, tag); |
215 RECURSE(Visit(label)); | 249 } else { |
250 ZoneList<Statement*>* stmts = clause->statements(); | |
251 block_size_ += stmts->length(); | |
252 RECURSE(VisitStatements(stmts)); | |
216 } | 253 } |
217 ZoneList<Statement*>* stmts = clause->statements(); | |
218 RECURSE(VisitStatements(stmts)); | |
219 } | 254 } |
220 } | 255 } |
221 | 256 |
222 void VisitCaseClause(CaseClause* clause) { UNREACHABLE(); } | 257 void VisitCaseClause(CaseClause* clause) { UNREACHABLE(); } |
223 | 258 |
224 void VisitDoWhileStatement(DoWhileStatement* stmt) { | 259 void VisitDoWhileStatement(DoWhileStatement* stmt) { |
225 DCHECK(in_function_); | 260 DCHECK(in_function_); |
226 BlockVisitor visitor(this, stmt->AsBreakableStatement(), kExprLoop, true); | 261 BlockVisitor visitor(this, stmt->AsBreakableStatement(), kExprLoop, true); |
227 block_size_ = 2; | 262 block_size_ = 2; |
228 RECURSE(Visit(stmt->body())); | 263 RECURSE(Visit(stmt->body())); |
(...skipping 769 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
998 // that zone in constructor may be thrown away once wasm module is written. | 1033 // that zone in constructor may be thrown away once wasm module is written. |
999 WasmModuleIndex* AsmWasmBuilder::Run() { | 1034 WasmModuleIndex* AsmWasmBuilder::Run() { |
1000 AsmWasmBuilderImpl impl(isolate_, zone_, literal_); | 1035 AsmWasmBuilderImpl impl(isolate_, zone_, literal_); |
1001 impl.Compile(); | 1036 impl.Compile(); |
1002 WasmModuleWriter* writer = impl.builder_->Build(zone_); | 1037 WasmModuleWriter* writer = impl.builder_->Build(zone_); |
1003 return writer->WriteTo(zone_); | 1038 return writer->WriteTo(zone_); |
1004 } | 1039 } |
1005 } // namespace wasm | 1040 } // namespace wasm |
1006 } // namespace internal | 1041 } // namespace internal |
1007 } // namespace v8 | 1042 } // namespace v8 |
OLD | NEW |