Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(185)

Side by Side Diff: src/wasm/wasm-module-builder.cc

Issue 2404253002: [wasm] Provide better stack traces for asm.js code (Closed)
Patch Set: Rebase & fix paths for windows Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/signature.h" 5 #include "src/signature.h"
6 6
7 #include "src/handles.h" 7 #include "src/handles.h"
8 #include "src/v8.h" 8 #include "src/v8.h"
9 #include "src/zone/zone-containers.h" 9 #include "src/zone/zone-containers.h"
10 10
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
52 signature_index_(0), 52 signature_index_(0),
53 exported_(0), 53 exported_(0),
54 func_index_(static_cast<uint32_t>(builder->functions_.size())), 54 func_index_(static_cast<uint32_t>(builder->functions_.size())),
55 body_(builder->zone()), 55 body_(builder->zone()),
56 name_(builder->zone()), 56 name_(builder->zone()),
57 exported_name_(builder->zone()), 57 exported_name_(builder->zone()),
58 i32_temps_(builder->zone()), 58 i32_temps_(builder->zone()),
59 i64_temps_(builder->zone()), 59 i64_temps_(builder->zone()),
60 f32_temps_(builder->zone()), 60 f32_temps_(builder->zone()),
61 f64_temps_(builder->zone()), 61 f64_temps_(builder->zone()),
62 direct_calls_(builder->zone()) {} 62 direct_calls_(builder->zone()),
63 asm_offsets_(builder->zone(), 8) {}
63 64
64 void WasmFunctionBuilder::EmitVarInt(uint32_t val) { 65 void WasmFunctionBuilder::EmitVarInt(uint32_t val) {
65 byte buffer[8]; 66 byte buffer[8];
66 byte* ptr = buffer; 67 byte* ptr = buffer;
67 LEBHelper::write_u32v(&ptr, val); 68 LEBHelper::write_u32v(&ptr, val);
68 for (byte* p = buffer; p < ptr; p++) { 69 for (byte* p = buffer; p < ptr; p++) {
69 body_.push_back(*p); 70 body_.push_back(*p);
70 } 71 }
71 } 72 }
72 73
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 exported_ = true; 147 exported_ = true;
147 exported_name_.resize(name.length()); 148 exported_name_.resize(name.length());
148 memcpy(exported_name_.data(), name.start(), name.length()); 149 memcpy(exported_name_.data(), name.start(), name.length());
149 } 150 }
150 151
151 void WasmFunctionBuilder::SetName(Vector<const char> name) { 152 void WasmFunctionBuilder::SetName(Vector<const char> name) {
152 name_.resize(name.length()); 153 name_.resize(name.length());
153 memcpy(name_.data(), name.start(), name.length()); 154 memcpy(name_.data(), name.start(), name.length());
154 } 155 }
155 156
157 void WasmFunctionBuilder::AddAsmWasmOffset(int asm_position) {
158 // We only want to emit one mapping per byte offset:
159 DCHECK(asm_offsets_.size() == 0 || body_.size() > last_asm_byte_offset_);
160
161 DCHECK_LE(body_.size(), kMaxUInt32);
162 uint32_t byte_offset = static_cast<uint32_t>(body_.size());
163 asm_offsets_.write_u32v(byte_offset - last_asm_byte_offset_);
164 last_asm_byte_offset_ = byte_offset;
165
166 DCHECK_GE(asm_position, 0);
167 asm_offsets_.write_i32v(asm_position - last_asm_source_position_);
168 last_asm_source_position_ = asm_position;
169 }
170
156 void WasmFunctionBuilder::WriteSignature(ZoneBuffer& buffer) const { 171 void WasmFunctionBuilder::WriteSignature(ZoneBuffer& buffer) const {
157 buffer.write_u32v(signature_index_); 172 buffer.write_u32v(signature_index_);
158 } 173 }
159 174
160 void WasmFunctionBuilder::WriteExport(ZoneBuffer& buffer) const { 175 void WasmFunctionBuilder::WriteExport(ZoneBuffer& buffer) const {
161 if (exported_) { 176 if (exported_) {
162 const ZoneVector<char>* exported_name = 177 const ZoneVector<char>* exported_name =
163 exported_name_.size() == 0 ? &name_ : &exported_name_; 178 exported_name_.size() == 0 ? &name_ : &exported_name_;
164 buffer.write_size(exported_name->size()); 179 buffer.write_size(exported_name->size());
165 buffer.write(reinterpret_cast<const byte*>(exported_name->data()), 180 buffer.write(reinterpret_cast<const byte*>(exported_name->data()),
(...skipping 15 matching lines...) Expand all
181 size_t base = buffer.offset(); 196 size_t base = buffer.offset();
182 buffer.write(&body_[0], body_.size()); 197 buffer.write(&body_[0], body_.size());
183 for (DirectCallIndex call : direct_calls_) { 198 for (DirectCallIndex call : direct_calls_) {
184 buffer.patch_u32v( 199 buffer.patch_u32v(
185 base + call.offset, 200 base + call.offset,
186 call.direct_index + static_cast<uint32_t>(builder_->imports_.size())); 201 call.direct_index + static_cast<uint32_t>(builder_->imports_.size()));
187 } 202 }
188 } 203 }
189 } 204 }
190 205
206 void WasmFunctionBuilder::WriteAsmWasmOffsetTable(ZoneBuffer& buffer) const {
207 if (asm_offsets_.size() == 0) {
208 buffer.write_size(0);
209 return;
210 }
211 buffer.write_size(asm_offsets_.size() + kInt32Size);
212 // Offset of the recorded byte offsets.
213 DCHECK_GE(kMaxUInt32, locals_.Size());
214 buffer.write_u32(static_cast<uint32_t>(locals_.Size()));
215 buffer.write(asm_offsets_.begin(), asm_offsets_.size());
216 }
217
191 WasmModuleBuilder::WasmModuleBuilder(Zone* zone) 218 WasmModuleBuilder::WasmModuleBuilder(Zone* zone)
192 : zone_(zone), 219 : zone_(zone),
193 signatures_(zone), 220 signatures_(zone),
194 imports_(zone), 221 imports_(zone),
195 functions_(zone), 222 functions_(zone),
196 data_segments_(zone), 223 data_segments_(zone),
197 indirect_functions_(zone), 224 indirect_functions_(zone),
198 globals_(zone), 225 globals_(zone),
199 signature_map_(zone), 226 signature_map_(zone),
200 start_function_index_(-1) {} 227 start_function_index_(-1) {}
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
302 import.name_length); 329 import.name_length);
303 buffer.write_u32v(0); // field name length 330 buffer.write_u32v(0); // field name length
304 buffer.write_u8(kExternalFunction); 331 buffer.write_u8(kExternalFunction);
305 buffer.write_u32v(import.sig_index); 332 buffer.write_u32v(import.sig_index);
306 } 333 }
307 FixupSection(buffer, start); 334 FixupSection(buffer, start);
308 } 335 }
309 336
310 // == Emit function signatures =============================================== 337 // == Emit function signatures ===============================================
311 bool has_names = false; 338 bool has_names = false;
339 bool has_asm_offsets = false;
312 if (functions_.size() > 0) { 340 if (functions_.size() > 0) {
313 size_t start = EmitSection(kFunctionSectionCode, buffer); 341 size_t start = EmitSection(kFunctionSectionCode, buffer);
314 buffer.write_size(functions_.size()); 342 buffer.write_size(functions_.size());
315 for (auto function : functions_) { 343 for (auto function : functions_) {
316 function->WriteSignature(buffer); 344 function->WriteSignature(buffer);
317 if (function->exported()) exports++; 345 if (function->exported()) exports++;
318 if (function->name_.size() > 0) has_names = true; 346 if (function->name_.size() > 0) has_names = true;
347 if (function->asm_offsets_.size() > 0) has_asm_offsets = true;
319 } 348 }
320 FixupSection(buffer, start); 349 FixupSection(buffer, start);
321 } 350 }
322 351
323 // == emit function table ==================================================== 352 // == emit function table ====================================================
324 if (indirect_functions_.size() > 0) { 353 if (indirect_functions_.size() > 0) {
325 size_t start = EmitSection(kTableSectionCode, buffer); 354 size_t start = EmitSection(kTableSectionCode, buffer);
326 buffer.write_u8(1); // table count 355 buffer.write_u8(1); // table count
327 buffer.write_u8(kWasmAnyFunctionTypeForm); 356 buffer.write_u8(kWasmAnyFunctionTypeForm);
328 buffer.write_u8(kResizableMaximumFlag); 357 buffer.write_u8(kResizableMaximumFlag);
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
486 for (auto function : functions_) { 515 for (auto function : functions_) {
487 buffer.write_size(function->name_.size()); 516 buffer.write_size(function->name_.size());
488 if (function->name_.size() > 0) { 517 if (function->name_.size() > 0) {
489 buffer.write(reinterpret_cast<const byte*>(&function->name_[0]), 518 buffer.write(reinterpret_cast<const byte*>(&function->name_[0]),
490 function->name_.size()); 519 function->name_.size());
491 } 520 }
492 buffer.write_u8(0); 521 buffer.write_u8(0);
493 } 522 }
494 FixupSection(buffer, start); 523 FixupSection(buffer, start);
495 } 524 }
525
526 // == Emit asm.js offset table ===============================================
527 if (has_asm_offsets) {
528 // Emit the section code.
529 buffer.write_u8(kUnknownSectionCode);
530 // Emit a placeholder for the length.
531 size_t start = buffer.reserve_u32v();
532 // Emit the section string.
533 const char* asm_offsets_name = "asm_offsets";
534 buffer.write_size(strlen(asm_offsets_name));
535 buffer.write(reinterpret_cast<const byte*>(asm_offsets_name),
536 strlen(asm_offsets_name));
537 buffer.write_size(functions_.size());
538 // Emit the offset table per function.
539 for (auto function : functions_) {
540 function->WriteAsmWasmOffsetTable(buffer);
541 }
542 FixupSection(buffer, start);
543 }
496 } 544 }
497 } // namespace wasm 545 } // namespace wasm
498 } // namespace internal 546 } // namespace internal
499 } // namespace v8 547 } // namespace v8
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698