Commit 4537f093 authored by aseemgarg's avatar aseemgarg Committed by Commit bot

Replace __init__ function in asm-wasm-builder with the start function section

BUG= https://code.google.com/p/v8/issues/detail?id=4203
TEST=mjsunit/asm-wasm
R=titzer@chromium.org,bradnelson@chromium.org
LOG=N

Review URL: https://codereview.chromium.org/1750153002

Cr-Commit-Position: refs/heads/master@{#34375}
parent a4462be1
......@@ -66,12 +66,10 @@ class AsmWasmBuilderImpl : public AstVisitor {
}
void InitializeInitFunction() {
unsigned char init[] = "__init__";
init_function_index_ = builder_->AddFunction();
current_function_builder_ = builder_->FunctionAt(init_function_index_);
current_function_builder_->SetName(init, 8);
current_function_builder_->ReturnType(kAstStmt);
current_function_builder_->Exported(1);
builder_->MarkStartFunction(init_function_index_);
current_function_builder_ = nullptr;
}
......
......@@ -333,7 +333,6 @@ void WasmDataSegmentEncoder::Serialize(byte* buffer, byte** header,
(*body) += data_.size();
}
WasmModuleBuilder::WasmModuleBuilder(Zone* zone)
: zone_(zone),
signatures_(zone),
......@@ -341,8 +340,8 @@ WasmModuleBuilder::WasmModuleBuilder(Zone* zone)
data_segments_(zone),
indirect_functions_(zone),
globals_(zone),
signature_map_(zone) {}
signature_map_(zone),
start_function_index_(-1) {}
uint16_t WasmModuleBuilder::AddFunction() {
functions_.push_back(new (zone_) WasmFunctionBuilder(zone_));
......@@ -399,6 +398,9 @@ void WasmModuleBuilder::AddIndirectFunction(uint16_t index) {
indirect_functions_.push_back(index);
}
void WasmModuleBuilder::MarkStartFunction(uint16_t index) {
start_function_index_ = index;
}
WasmModuleWriter* WasmModuleBuilder::Build(Zone* zone) {
WasmModuleWriter* writer = new (zone) WasmModuleWriter(zone);
......@@ -417,6 +419,7 @@ WasmModuleWriter* WasmModuleBuilder::Build(Zone* zone) {
for (auto global : globals_) {
writer->globals_.push_back(global);
}
writer->start_function_index_ = start_function_index_;
return writer;
}
......@@ -434,6 +437,14 @@ WasmModuleWriter::WasmModuleWriter(Zone* zone)
indirect_functions_(zone),
globals_(zone) {}
size_t SizeOfVarInt(size_t value) {
size_t size = 0;
do {
size++;
value = value >> 7;
} while (value > 0);
return size;
}
struct Sizes {
size_t header_size;
......@@ -449,10 +460,7 @@ struct Sizes {
void AddSection(size_t size) {
if (size > 0) {
Add(1, 0);
while (size > 0) {
Add(1, 0);
size = size >> 7;
}
Add(SizeOfVarInt(size), 0);
}
}
};
......@@ -482,6 +490,11 @@ WasmModuleIndex* WasmModuleWriter::WriteTo(Zone* zone) const {
function->NameSize());
}
if (start_function_index_ >= 0) {
sizes.Add(1, 0);
sizes.Add(SizeOfVarInt(start_function_index_), 0);
}
sizes.AddSection(data_segments_.size());
for (auto segment : data_segments_) {
sizes.Add(segment->HeaderSize(), segment->BodySize());
......@@ -547,6 +560,12 @@ WasmModuleIndex* WasmModuleWriter::WriteTo(Zone* zone) const {
}
}
// -- emit start function index ----------------------------------------------
if (start_function_index_ >= 0) {
EmitUint8(&header, kDeclStartFunction);
EmitVarInt(&header, start_function_index_);
}
// -- emit data segments -----------------------------------------------------
if (data_segments_.size() > 0) {
EmitUint8(&header, kDeclDataSegments);
......
......@@ -120,6 +120,7 @@ class WasmModuleWriter : public ZoneObject {
ZoneVector<FunctionSig*> signatures_;
ZoneVector<uint16_t> indirect_functions_;
ZoneVector<std::pair<MachineType, bool>> globals_;
int start_function_index_;
};
class WasmModuleBuilder : public ZoneObject {
......@@ -131,6 +132,7 @@ class WasmModuleBuilder : public ZoneObject {
void AddDataSegment(WasmDataSegmentEncoder* data);
uint16_t AddSignature(FunctionSig* sig);
void AddIndirectFunction(uint16_t index);
void MarkStartFunction(uint16_t index);
WasmModuleWriter* Build(Zone* zone);
struct CompareFunctionSigs {
......@@ -146,6 +148,7 @@ class WasmModuleBuilder : public ZoneObject {
ZoneVector<uint16_t> indirect_functions_;
ZoneVector<std::pair<MachineType, bool>> globals_;
SignatureMap signature_map_;
int start_function_index_;
};
std::vector<uint8_t> UnsignedLEB128From(uint32_t result);
......
......@@ -415,7 +415,6 @@ function TestInt32HeapAccessExternal() {
var memory_int32 = new Int32Array(memory);
var module = _WASMEXP_.instantiateModuleFromAsm(
TestInt32HeapAccess.toString(), null, memory);
module.__init__();
assertEquals(7, module.caller());
assertEquals(7, memory_int32[2]);
}
......@@ -439,7 +438,6 @@ function TestHeapAccessIntTypes() {
var memory = new ArrayBuffer(1024);
var memory_view = new types[i][0](memory);
var module = _WASMEXP_.instantiateModuleFromAsm(code, null, memory);
module.__init__();
assertEquals(7, module.caller());
assertEquals(7, memory_view[2]);
assertEquals(7, _WASMEXP_.instantiateModuleFromAsm(code).caller());
......@@ -480,7 +478,6 @@ function TestFloatHeapAccessExternal() {
var memory_float64 = new Float64Array(memory);
var module = _WASMEXP_.instantiateModuleFromAsm(
TestFloatHeapAccess.toString(), null, memory);
module.__init__();
assertEquals(1, module.caller());
assertEquals(9.0, memory_float64[1]);
}
......@@ -656,7 +653,6 @@ function TestGlobalsWithInit() {
}
var module = _WASMEXP_.instantiateModuleFromAsm(TestGlobalsWithInit.toString());
module.__init__();
assertEquals(77.5, module.add());
......@@ -879,7 +875,6 @@ function TestInitFunctionWithNoGlobals() {
var module = _WASMEXP_.instantiateModuleFromAsm(
TestInitFunctionWithNoGlobals.toString());
module.__init__();
assertEquals(51, module.caller());
......@@ -893,7 +888,6 @@ function TestExportNameDifferentFromFunctionName() {
var module = _WASMEXP_.instantiateModuleFromAsm(
TestExportNameDifferentFromFunctionName.toString());
module.__init__();
assertEquals(55, module.alt_caller());
......@@ -988,7 +982,6 @@ function TestFunctionTable() {
}
var module = _WASMEXP_.instantiateModuleFromAsm(TestFunctionTable.toString());
module.__init__();
assertEquals(55, module.caller(0, 0, 33, 22));
assertEquals(11, module.caller(0, 1, 33, 22));
assertEquals(9, module.caller(0, 2, 54, 45));
......@@ -1036,7 +1029,6 @@ function TestForeignFunctions() {
var module = _WASMEXP_.instantiateModuleFromAsm(AsmModule.toString(),
foreign, null);
module.__init__();
assertEquals(103, module.caller(23, 103));
}
......@@ -1076,7 +1068,6 @@ function TestForeignFunctionMultipleUse() {
var module = _WASMEXP_.instantiateModuleFromAsm(AsmModule.toString(),
foreign, null);
module.__init__();
assertEquals(89, module.caller(83, 83.25));
}
......@@ -1114,7 +1105,6 @@ function TestForeignVariables() {
function TestCase(env, i1, f1, i2, f2) {
var module = _WASMEXP_.instantiateModuleFromAsm(
AsmModule.toString(), env);
module.__init__();
assertEquals(i1, module.geti1());
assertEquals(f1, module.getf1());
assertEquals(i2, module.geti2());
......@@ -1237,7 +1227,6 @@ TestForeignVariables();
var m = _WASMEXP_.instantiateModuleFromAsm(
Module.toString(), { x: 4, y: 11 });
m.__init__();
assertEquals(15, m.test());
})();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment