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

Unified Diff: src/wasm/encoder.cc

Issue 1765843002: wasm: use strings for section names (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Fix some encoding. A few tests still fail. Created 4 years, 9 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | src/wasm/module-decoder.cc » ('j') | src/wasm/module-decoder.cc » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/wasm/encoder.cc
diff --git a/src/wasm/encoder.cc b/src/wasm/encoder.cc
index 21799eff5271a7e60343c120757304981e33f0fb..f39bddf083b38f2a5d8b05f1bf585250fe81dd27 100644
--- a/src/wasm/encoder.cc
+++ b/src/wasm/encoder.cc
@@ -42,6 +42,11 @@ void EmitUint32(byte** b, uint32_t x) {
}
+// Sections all start with a size, but it's unknown at the start.
+// We generate a large varint which we then fixup later when the size is known.
+const size_t padded_varint = 4;
+
+
void EmitVarInt(byte** b, size_t val) {
while (true) {
size_t next = val >> 7;
@@ -55,9 +60,38 @@ void EmitVarInt(byte** b, size_t val) {
}
}
}
-} // namespace
+void FixupSection(byte* start, byte* end) {
+ // Same as EmitVarInt, but fixed-width with zeroes in the MSBs.
+ size_t val = end - start - padded_varint;
+ for (size_t pos = 0; pos != padded_varint; ++pos) {
+ size_t next = val >> 7;
+ byte out = static_cast<byte>(val & 0x7f);
+ if (pos != padded_varint - 1) {
+ *(start++) = 0x80 | out;
+ val = next;
+ } else {
+ *(start++) = out;
+ }
+ }
+}
+
+
+// Returns the start of the section, including its VarInt size.
+byte* EmitSection(byte** b, WasmSection::Code code) {
+ byte* start = *b;
+ const char* name = WasmSection::getName(code);
+ size_t length = WasmSection::getNameLength(code);
+ for (size_t padding = 0; padding != padded_varint; ++padding) {
+ EmitUint8(b, 0xff); // Will get fixed up later.
+ }
+ EmitVarInt(b, length); // Section name string size.
+ for (size_t i = 0; i != length; ++i) EmitUint8(b, name[i]);
+ return start;
+}
+} // namespace
+
struct WasmFunctionBuilder::Type {
bool param_;
LocalType type_;
@@ -466,9 +500,11 @@ struct Sizes {
body_size += body;
}
- void AddSection(size_t size) {
+ void AddSection(WasmSection::Code code, size_t size) {
if (size > 0) {
- Add(1, 0);
+ Add(padded_varint, 0); // Section size.
+ Add(SizeOfVarInt(WasmSection::getNameLength(code)), 0);
+ Add(WasmSection::getNameLength(code), 0);
Add(SizeOfVarInt(size), 0);
}
}
@@ -483,17 +519,17 @@ WasmModuleIndex* WasmModuleWriter::WriteTo(Zone* zone) const {
sizes.Add(1, 0);
sizes.Add(kDeclMemorySize, 0);
- sizes.AddSection(signatures_.size());
+ sizes.AddSection(WasmSection::Code::Signatures, signatures_.size());
for (auto sig : signatures_) {
sizes.Add(2 + sig->parameter_count(), 0);
}
- sizes.AddSection(globals_.size());
+ sizes.AddSection(WasmSection::Code::Globals, globals_.size());
if (globals_.size() > 0) {
sizes.Add(kDeclGlobalSize * globals_.size(), 0);
}
- sizes.AddSection(functions_.size());
+ sizes.AddSection(WasmSection::Code::Functions, functions_.size());
for (auto function : functions_) {
sizes.Add(function->HeaderSize() + function->BodySize(),
function->NameSize());
@@ -504,12 +540,13 @@ WasmModuleIndex* WasmModuleWriter::WriteTo(Zone* zone) const {
sizes.Add(SizeOfVarInt(start_function_index_), 0);
}
- sizes.AddSection(data_segments_.size());
+ sizes.AddSection(WasmSection::Code::DataSegments, data_segments_.size());
for (auto segment : data_segments_) {
sizes.Add(segment->HeaderSize(), segment->BodySize());
}
- sizes.AddSection(indirect_functions_.size());
+ sizes.AddSection(WasmSection::Code::FunctionTable,
+ indirect_functions_.size());
sizes.Add(2 * static_cast<uint32_t>(indirect_functions_.size()), 0);
if (sizes.body_size > 0) sizes.Add(1, 0);
@@ -524,14 +561,17 @@ WasmModuleIndex* WasmModuleWriter::WriteTo(Zone* zone) const {
EmitUint32(&header, kWasmVersion);
// -- emit memory declaration ------------------------------------------------
- EmitUint8(&header, kDeclMemory);
- EmitUint8(&header, 16); // min memory size
- EmitUint8(&header, 16); // max memory size
- EmitUint8(&header, 0); // memory export
+ {
+ byte* section = EmitSection(&header, WasmSection::Code::Memory);
+ EmitUint8(&header, 16); // min memory size
+ EmitUint8(&header, 16); // max memory size
+ EmitUint8(&header, 0); // memory export
+ FixupSection(section, header);
+ }
// -- emit globals -----------------------------------------------------------
if (globals_.size() > 0) {
- EmitUint8(&header, kDeclGlobals);
+ byte* section = EmitSection(&header, WasmSection::Code::Globals);
EmitVarInt(&header, globals_.size());
for (auto global : globals_) {
@@ -539,11 +579,12 @@ WasmModuleIndex* WasmModuleWriter::WriteTo(Zone* zone) const {
EmitUint8(&header, WasmOpcodes::MemTypeCodeFor(global.first));
EmitUint8(&header, global.second);
}
+ FixupSection(section, header);
}
// -- emit signatures --------------------------------------------------------
if (signatures_.size() > 0) {
- EmitUint8(&header, kDeclSignatures);
+ byte* section = EmitSection(&header, WasmSection::Code::Signatures);
EmitVarInt(&header, signatures_.size());
for (FunctionSig* sig : signatures_) {
@@ -557,45 +598,53 @@ WasmModuleIndex* WasmModuleWriter::WriteTo(Zone* zone) const {
EmitUint8(&header, WasmOpcodes::LocalTypeCodeFor(sig->GetParam(j)));
}
}
+ FixupSection(section, header);
}
// -- emit functions ---------------------------------------------------------
if (functions_.size() > 0) {
- EmitUint8(&header, kDeclFunctions);
+ byte* section = EmitSection(&header, WasmSection::Code::Functions);
EmitVarInt(&header, functions_.size());
for (auto func : functions_) {
func->Serialize(buffer, &header, &body);
}
+ FixupSection(section, header);
}
// -- emit start function index ----------------------------------------------
if (start_function_index_ >= 0) {
- EmitUint8(&header, kDeclStartFunction);
+ byte* section = EmitSection(&header, WasmSection::Code::StartFunction);
EmitVarInt(&header, start_function_index_);
+ FixupSection(section, header);
}
// -- emit data segments -----------------------------------------------------
if (data_segments_.size() > 0) {
- EmitUint8(&header, kDeclDataSegments);
+ byte* section = EmitSection(&header, WasmSection::Code::DataSegments);
EmitVarInt(&header, data_segments_.size());
for (auto segment : data_segments_) {
segment->Serialize(buffer, &header, &body);
}
+ FixupSection(section, header);
}
// -- emit function table ----------------------------------------------------
if (indirect_functions_.size() > 0) {
- EmitUint8(&header, kDeclFunctionTable);
+ byte* section = EmitSection(&header, WasmSection::Code::FunctionTable);
EmitVarInt(&header, indirect_functions_.size());
for (auto index : indirect_functions_) {
EmitUint16(&header, index);
}
+ FixupSection(section, header);
}
- if (sizes.body_size > 0) EmitUint8(&header, kDeclEnd);
+ if (sizes.body_size > 0) {
+ byte* section = EmitSection(&header, WasmSection::Code::End);
+ FixupSection(section, header);
+ }
return new (zone) WasmModuleIndex(buffer, buffer + sizes.total());
}
« no previous file with comments | « no previous file | src/wasm/module-decoder.cc » ('j') | src/wasm/module-decoder.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698