[wasm] Refactor handling of data segments in WasmModuleBuilder.
R=bradnelson@chromium.org,aseemgarg@chromium.org,mstarzinger@chromium.org BUG= Review-Url: https://codereview.chromium.org/2384483002 Cr-Commit-Position: refs/heads/master@{#39887}
This commit is contained in:
parent
e6071a9c06
commit
ab21fec6d3
@ -168,23 +168,6 @@ void WasmFunctionBuilder::WriteBody(ZoneBuffer& buffer) const {
|
||||
}
|
||||
}
|
||||
|
||||
WasmDataSegmentEncoder::WasmDataSegmentEncoder(Zone* zone, const byte* data,
|
||||
uint32_t size, uint32_t dest)
|
||||
: data_(zone), dest_(dest) {
|
||||
for (size_t i = 0; i < size; ++i) {
|
||||
data_.push_back(data[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void WasmDataSegmentEncoder::Write(ZoneBuffer& buffer) const {
|
||||
buffer.write_u8(0); // linear memory zero
|
||||
buffer.write_u8(kExprI32Const);
|
||||
buffer.write_u32v(dest_);
|
||||
buffer.write_u8(kExprEnd);
|
||||
buffer.write_u32v(static_cast<uint32_t>(data_.size()));
|
||||
buffer.write(&data_[0], data_.size());
|
||||
}
|
||||
|
||||
WasmModuleBuilder::WasmModuleBuilder(Zone* zone)
|
||||
: zone_(zone),
|
||||
signatures_(zone),
|
||||
@ -203,8 +186,13 @@ WasmFunctionBuilder* WasmModuleBuilder::AddFunction(FunctionSig* sig) {
|
||||
return functions_.back();
|
||||
}
|
||||
|
||||
void WasmModuleBuilder::AddDataSegment(WasmDataSegmentEncoder* data) {
|
||||
data_segments_.push_back(data);
|
||||
void WasmModuleBuilder::AddDataSegment(const byte* data, uint32_t size,
|
||||
uint32_t dest) {
|
||||
data_segments_.push_back({ZoneVector<byte>(zone()), dest});
|
||||
ZoneVector<byte>& vec = data_segments_.back().data;
|
||||
for (uint32_t i = 0; i < size; i++) {
|
||||
vec.push_back(data[i]);
|
||||
}
|
||||
}
|
||||
|
||||
bool WasmModuleBuilder::CompareFunctionSigs::operator()(FunctionSig* a,
|
||||
@ -253,7 +241,7 @@ void WasmModuleBuilder::MarkStartFunction(WasmFunctionBuilder* function) {
|
||||
|
||||
uint32_t WasmModuleBuilder::AddGlobal(LocalType type, bool exported,
|
||||
bool mutability) {
|
||||
globals_.push_back(std::make_tuple(type, exported, mutability));
|
||||
globals_.push_back({type, exported, mutability});
|
||||
return static_cast<uint32_t>(globals_.size() - 1);
|
||||
}
|
||||
|
||||
@ -339,12 +327,9 @@ void WasmModuleBuilder::WriteTo(ZoneBuffer& buffer) const {
|
||||
buffer.write_size(globals_.size());
|
||||
|
||||
for (auto global : globals_) {
|
||||
static const int kLocalTypeIndex = 0;
|
||||
static const int kMutabilityIndex = 2;
|
||||
buffer.write_u8(
|
||||
WasmOpcodes::LocalTypeCodeFor(std::get<kLocalTypeIndex>(global)));
|
||||
buffer.write_u8(std::get<kMutabilityIndex>(global));
|
||||
switch (std::get<kLocalTypeIndex>(global)) {
|
||||
buffer.write_u8(WasmOpcodes::LocalTypeCodeFor(global.type));
|
||||
buffer.write_u8(global.mutability ? 1 : 0);
|
||||
switch (global.type) {
|
||||
case kAstI32: {
|
||||
static const byte code[] = {WASM_I32V_1(0)};
|
||||
buffer.write(code, sizeof(code));
|
||||
@ -421,7 +406,12 @@ void WasmModuleBuilder::WriteTo(ZoneBuffer& buffer) const {
|
||||
buffer.write_size(data_segments_.size());
|
||||
|
||||
for (auto segment : data_segments_) {
|
||||
segment->Write(buffer);
|
||||
buffer.write_u8(0); // linear memory segment
|
||||
buffer.write_u8(kExprI32Const); // initializer expression for dest
|
||||
buffer.write_u32v(segment.dest);
|
||||
buffer.write_u8(kExprEnd);
|
||||
buffer.write_u32v(static_cast<uint32_t>(segment.data.size()));
|
||||
buffer.write(&segment.data[0], segment.data.size());
|
||||
}
|
||||
FixupSection(buffer, start);
|
||||
}
|
||||
|
@ -193,24 +193,6 @@ class WasmTemporary {
|
||||
uint32_t index_;
|
||||
};
|
||||
|
||||
// TODO(titzer): kill!
|
||||
class WasmDataSegmentEncoder : public ZoneObject {
|
||||
public:
|
||||
WasmDataSegmentEncoder(Zone* zone, const byte* data, uint32_t size,
|
||||
uint32_t dest);
|
||||
void Write(ZoneBuffer& buffer) const;
|
||||
|
||||
private:
|
||||
ZoneVector<byte> data_;
|
||||
uint32_t dest_;
|
||||
};
|
||||
|
||||
struct WasmFunctionImport {
|
||||
uint32_t sig_index;
|
||||
const char* name;
|
||||
int name_length;
|
||||
};
|
||||
|
||||
class V8_EXPORT_PRIVATE WasmModuleBuilder : public ZoneObject {
|
||||
public:
|
||||
explicit WasmModuleBuilder(Zone* zone);
|
||||
@ -223,7 +205,7 @@ class V8_EXPORT_PRIVATE WasmModuleBuilder : public ZoneObject {
|
||||
}
|
||||
WasmFunctionBuilder* AddFunction(FunctionSig* sig = nullptr);
|
||||
uint32_t AddGlobal(LocalType type, bool exported, bool mutability = true);
|
||||
void AddDataSegment(WasmDataSegmentEncoder* data);
|
||||
void AddDataSegment(const byte* data, uint32_t size, uint32_t dest);
|
||||
uint32_t AddSignature(FunctionSig* sig);
|
||||
void AddIndirectFunction(uint32_t index);
|
||||
void MarkStartFunction(WasmFunctionBuilder* builder);
|
||||
@ -241,14 +223,31 @@ class V8_EXPORT_PRIVATE WasmModuleBuilder : public ZoneObject {
|
||||
FunctionSig* GetSignature(uint32_t index) { return signatures_[index]; }
|
||||
|
||||
private:
|
||||
struct WasmFunctionImport {
|
||||
uint32_t sig_index;
|
||||
const char* name;
|
||||
int name_length;
|
||||
};
|
||||
|
||||
struct WasmGlobal {
|
||||
LocalType type;
|
||||
bool exported;
|
||||
bool mutability;
|
||||
};
|
||||
|
||||
struct WasmDataSegment {
|
||||
ZoneVector<byte> data;
|
||||
uint32_t dest;
|
||||
};
|
||||
|
||||
friend class WasmFunctionBuilder;
|
||||
Zone* zone_;
|
||||
ZoneVector<FunctionSig*> signatures_;
|
||||
ZoneVector<WasmFunctionImport> imports_;
|
||||
ZoneVector<WasmFunctionBuilder*> functions_;
|
||||
ZoneVector<WasmDataSegmentEncoder*> data_segments_;
|
||||
ZoneVector<WasmDataSegment> data_segments_;
|
||||
ZoneVector<uint32_t> indirect_functions_;
|
||||
ZoneVector<std::tuple<LocalType, bool, bool>> globals_;
|
||||
ZoneVector<WasmGlobal> globals_;
|
||||
SignatureMap signature_map_;
|
||||
int start_function_index_;
|
||||
};
|
||||
|
@ -110,8 +110,7 @@ TEST(Run_WasmModule_ReadLoadedDataSegment) {
|
||||
WASM_LOAD_MEM(MachineType::Int32(), WASM_I8(kDataSegmentDest0))};
|
||||
f->EmitCode(code, sizeof(code));
|
||||
byte data[] = {0xaa, 0xbb, 0xcc, 0xdd};
|
||||
builder->AddDataSegment(new (&zone) WasmDataSegmentEncoder(
|
||||
&zone, data, sizeof(data), kDataSegmentDest0));
|
||||
builder->AddDataSegment(data, sizeof(data), kDataSegmentDest0);
|
||||
TestModule(&zone, builder, 0xddccbbaa);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user