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

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

Issue 2384623003: [wasm] [asm.js] Track direct function indices and fixup. (Closed)
Patch Set: fix 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
« no previous file with comments | « src/wasm/wasm-module-builder.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 void FixupSection(ZoneBuffer& buffer, size_t start) { 44 void FixupSection(ZoneBuffer& buffer, size_t start) {
45 buffer.patch_u32v(start, static_cast<uint32_t>(buffer.offset() - start - 45 buffer.patch_u32v(start, static_cast<uint32_t>(buffer.offset() - start -
46 kPaddedVarInt32Size)); 46 kPaddedVarInt32Size));
47 } 47 }
48 48
49 WasmFunctionBuilder::WasmFunctionBuilder(WasmModuleBuilder* builder) 49 WasmFunctionBuilder::WasmFunctionBuilder(WasmModuleBuilder* builder)
50 : builder_(builder), 50 : builder_(builder),
51 locals_(builder->zone()), 51 locals_(builder->zone()),
52 signature_index_(0), 52 signature_index_(0),
53 exported_(0), 53 exported_(0),
54 func_index_(static_cast<uint32_t>(builder->imports_.size() + 54 func_index_(static_cast<uint32_t>(builder->functions_.size())),
55 builder->functions_.size())),
56 body_(builder->zone()), 55 body_(builder->zone()),
57 name_(builder->zone()), 56 name_(builder->zone()),
58 i32_temps_(builder->zone()), 57 i32_temps_(builder->zone()),
59 i64_temps_(builder->zone()), 58 i64_temps_(builder->zone()),
60 f32_temps_(builder->zone()), 59 f32_temps_(builder->zone()),
61 f64_temps_(builder->zone()) {} 60 f64_temps_(builder->zone()),
61 direct_calls_(builder->zone()) {}
62 62
63 void WasmFunctionBuilder::EmitVarInt(uint32_t val) { 63 void WasmFunctionBuilder::EmitVarInt(uint32_t val) {
64 byte buffer[8]; 64 byte buffer[8];
65 byte* ptr = buffer; 65 byte* ptr = buffer;
66 LEBHelper::write_u32v(&ptr, val); 66 LEBHelper::write_u32v(&ptr, val);
67 for (byte* p = buffer; p < ptr; p++) { 67 for (byte* p = buffer; p < ptr; p++) {
68 body_.push_back(*p); 68 body_.push_back(*p);
69 } 69 }
70 } 70 }
71 71
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 void WasmFunctionBuilder::EmitI32Const(int32_t value) { 123 void WasmFunctionBuilder::EmitI32Const(int32_t value) {
124 // TODO(titzer): variable-length signed and unsigned i32 constants. 124 // TODO(titzer): variable-length signed and unsigned i32 constants.
125 if (-128 <= value && value <= 127) { 125 if (-128 <= value && value <= 127) {
126 EmitWithU8(kExprI8Const, static_cast<byte>(value)); 126 EmitWithU8(kExprI8Const, static_cast<byte>(value));
127 } else { 127 } else {
128 byte code[] = {WASM_I32V_5(value)}; 128 byte code[] = {WASM_I32V_5(value)};
129 EmitCode(code, sizeof(code)); 129 EmitCode(code, sizeof(code));
130 } 130 }
131 } 131 }
132 132
133 void WasmFunctionBuilder::EmitDirectCallIndex(uint32_t index) {
134 DirectCallIndex call;
135 call.offset = body_.size();
136 call.direct_index = index;
137 direct_calls_.push_back(call);
138 byte code[] = {U32V_5(0)};
139 EmitCode(code, sizeof(code));
140 }
141
133 void WasmFunctionBuilder::SetExported() { exported_ = true; } 142 void WasmFunctionBuilder::SetExported() { exported_ = true; }
134 143
135 void WasmFunctionBuilder::SetName(const char* name, int name_length) { 144 void WasmFunctionBuilder::SetName(const char* name, int name_length) {
136 name_.clear(); 145 name_.clear();
137 if (name_length > 0) { 146 if (name_length > 0) {
138 for (int i = 0; i < name_length; ++i) { 147 for (int i = 0; i < name_length; ++i) {
139 name_.push_back(*(name + i)); 148 name_.push_back(*(name + i));
140 } 149 }
141 } 150 }
142 } 151 }
143 152
144 void WasmFunctionBuilder::WriteSignature(ZoneBuffer& buffer) const { 153 void WasmFunctionBuilder::WriteSignature(ZoneBuffer& buffer) const {
145 buffer.write_u32v(signature_index_); 154 buffer.write_u32v(signature_index_);
146 } 155 }
147 156
148 void WasmFunctionBuilder::WriteExport(ZoneBuffer& buffer) const { 157 void WasmFunctionBuilder::WriteExport(ZoneBuffer& buffer) const {
149 if (exported_) { 158 if (exported_) {
150 buffer.write_size(name_.size()); 159 buffer.write_size(name_.size());
151 if (name_.size() > 0) { 160 if (name_.size() > 0) {
152 buffer.write(reinterpret_cast<const byte*>(&name_[0]), name_.size()); 161 buffer.write(reinterpret_cast<const byte*>(&name_[0]), name_.size());
153 } 162 }
154 buffer.write_u8(kExternalFunction); 163 buffer.write_u8(kExternalFunction);
155 buffer.write_u32v(func_index_); 164 buffer.write_u32v(func_index_ +
165 static_cast<uint32_t>(builder_->imports_.size()));
156 } 166 }
157 } 167 }
158 168
159 void WasmFunctionBuilder::WriteBody(ZoneBuffer& buffer) const { 169 void WasmFunctionBuilder::WriteBody(ZoneBuffer& buffer) const {
160 size_t locals_size = locals_.Size(); 170 size_t locals_size = locals_.Size();
161 buffer.write_size(locals_size + body_.size()); 171 buffer.write_size(locals_size + body_.size());
162 buffer.EnsureSpace(locals_size); 172 buffer.EnsureSpace(locals_size);
163 byte** ptr = buffer.pos_ptr(); 173 byte** ptr = buffer.pos_ptr();
164 locals_.Emit(*ptr); 174 locals_.Emit(*ptr);
165 (*ptr) += locals_size; // UGLY: manual bump of position pointer 175 (*ptr) += locals_size; // UGLY: manual bump of position pointer
166 if (body_.size() > 0) { 176 if (body_.size() > 0) {
177 size_t base = buffer.offset();
167 buffer.write(&body_[0], body_.size()); 178 buffer.write(&body_[0], body_.size());
179 for (DirectCallIndex call : direct_calls_) {
180 buffer.patch_u32v(
181 base + call.offset,
182 call.direct_index + static_cast<uint32_t>(builder_->imports_.size()));
183 }
168 } 184 }
169 } 185 }
170 186
171 WasmModuleBuilder::WasmModuleBuilder(Zone* zone) 187 WasmModuleBuilder::WasmModuleBuilder(Zone* zone)
172 : zone_(zone), 188 : zone_(zone),
173 signatures_(zone), 189 signatures_(zone),
174 imports_(zone), 190 imports_(zone),
175 functions_(zone), 191 functions_(zone),
176 data_segments_(zone), 192 data_segments_(zone),
177 indirect_functions_(zone), 193 indirect_functions_(zone),
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
223 return index; 239 return index;
224 } 240 }
225 } 241 }
226 242
227 void WasmModuleBuilder::AddIndirectFunction(uint32_t index) { 243 void WasmModuleBuilder::AddIndirectFunction(uint32_t index) {
228 indirect_functions_.push_back(index); 244 indirect_functions_.push_back(index);
229 } 245 }
230 246
231 uint32_t WasmModuleBuilder::AddImport(const char* name, int name_length, 247 uint32_t WasmModuleBuilder::AddImport(const char* name, int name_length,
232 FunctionSig* sig) { 248 FunctionSig* sig) {
233 DCHECK_EQ(0, functions_.size()); // imports must be added before functions!
234 imports_.push_back({AddSignature(sig), name, name_length}); 249 imports_.push_back({AddSignature(sig), name, name_length});
235 return static_cast<uint32_t>(imports_.size() - 1); 250 return static_cast<uint32_t>(imports_.size() - 1);
236 } 251 }
237 252
238 void WasmModuleBuilder::MarkStartFunction(WasmFunctionBuilder* function) { 253 void WasmModuleBuilder::MarkStartFunction(WasmFunctionBuilder* function) {
239 start_function_index_ = function->func_index(); 254 start_function_index_ = function->func_index();
240 } 255 }
241 256
242 uint32_t WasmModuleBuilder::AddGlobal(LocalType type, bool exported, 257 uint32_t WasmModuleBuilder::AddGlobal(LocalType type, bool exported,
243 bool mutability) { 258 bool mutability) {
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
362 if (exports > 0) { 377 if (exports > 0) {
363 size_t start = EmitSection(kExportSectionCode, buffer); 378 size_t start = EmitSection(kExportSectionCode, buffer);
364 buffer.write_u32v(exports); 379 buffer.write_u32v(exports);
365 for (auto function : functions_) function->WriteExport(buffer); 380 for (auto function : functions_) function->WriteExport(buffer);
366 FixupSection(buffer, start); 381 FixupSection(buffer, start);
367 } 382 }
368 383
369 // == emit start function index ============================================== 384 // == emit start function index ==============================================
370 if (start_function_index_ >= 0) { 385 if (start_function_index_ >= 0) {
371 size_t start = EmitSection(kStartSectionCode, buffer); 386 size_t start = EmitSection(kStartSectionCode, buffer);
372 buffer.write_u32v(start_function_index_); 387 buffer.write_u32v(start_function_index_ +
388 static_cast<uint32_t>(imports_.size()));
373 FixupSection(buffer, start); 389 FixupSection(buffer, start);
374 } 390 }
375 391
376 // == emit function table elements =========================================== 392 // == emit function table elements ===========================================
377 if (indirect_functions_.size() > 0) { 393 if (indirect_functions_.size() > 0) {
378 size_t start = EmitSection(kElementSectionCode, buffer); 394 size_t start = EmitSection(kElementSectionCode, buffer);
379 buffer.write_u8(1); // count of entries 395 buffer.write_u8(1); // count of entries
380 buffer.write_u8(0); // table index 396 buffer.write_u8(0); // table index
381 buffer.write_u8(kExprI32Const); // offset 397 buffer.write_u8(kExprI32Const); // offset
382 buffer.write_u32v(0); 398 buffer.write_u32v(0);
383 buffer.write_u8(kExprEnd); 399 buffer.write_u8(kExprEnd);
384 buffer.write_size(indirect_functions_.size()); // element count 400 buffer.write_size(indirect_functions_.size()); // element count
385 401
386 for (auto index : indirect_functions_) { 402 for (auto index : indirect_functions_) {
387 buffer.write_u32v(index); 403 buffer.write_u32v(index + static_cast<uint32_t>(imports_.size()));
388 } 404 }
389 405
390 FixupSection(buffer, start); 406 FixupSection(buffer, start);
391 } 407 }
392 408
393 // == emit code ============================================================== 409 // == emit code ==============================================================
394 if (functions_.size() > 0) { 410 if (functions_.size() > 0) {
395 size_t start = EmitSection(kCodeSectionCode, buffer); 411 size_t start = EmitSection(kCodeSectionCode, buffer);
396 buffer.write_size(functions_.size()); 412 buffer.write_size(functions_.size());
397 for (auto function : functions_) { 413 for (auto function : functions_) {
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
434 function->name_.size()); 450 function->name_.size());
435 } 451 }
436 buffer.write_u8(0); 452 buffer.write_u8(0);
437 } 453 }
438 FixupSection(buffer, start); 454 FixupSection(buffer, start);
439 } 455 }
440 } 456 }
441 } // namespace wasm 457 } // namespace wasm
442 } // namespace internal 458 } // namespace internal
443 } // namespace v8 459 } // namespace v8
OLDNEW
« no previous file with comments | « src/wasm/wasm-module-builder.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698