Commit 008888c8 authored by titzer's avatar titzer Committed by Commit bot

[wasm] Allocate WasmModule and WasmModuleInstance vectors inline.

R=bradnelson@chromium.org,ahaas@chromium.org
BUG=

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

Cr-Commit-Position: refs/heads/master@{#34351}
parent 91802542
......@@ -1854,7 +1854,7 @@ Node* WasmGraphBuilder::LoadGlobal(uint32_t index) {
MachineType mem_type = module_->GetGlobalType(index);
Node* addr = jsgraph()->IntPtrConstant(
reinterpret_cast<uintptr_t>(module_->instance->globals_start +
module_->module->globals->at(index).offset));
module_->module->globals[index].offset));
const Operator* op = jsgraph()->machine()->Load(mem_type);
Node* node = graph()->NewNode(op, addr, jsgraph()->Int32Constant(0), *effect_,
*control_);
......@@ -1868,7 +1868,7 @@ Node* WasmGraphBuilder::StoreGlobal(uint32_t index, Node* val) {
MachineType mem_type = module_->GetGlobalType(index);
Node* addr = jsgraph()->IntPtrConstant(
reinterpret_cast<uintptr_t>(module_->instance->globals_start +
module_->module->globals->at(index).offset));
module_->module->globals[index].offset));
const Operator* op = jsgraph()->machine()->Store(
StoreRepresentation(mem_type.representation(), kNoWriteBarrier));
Node* node = graph()->NewNode(op, addr, jsgraph()->Int32Constant(0), val,
......@@ -2006,7 +2006,7 @@ static void RecordFunctionCompilation(Logger::LogEventsAndTags tag,
Handle<JSFunction> CompileJSToWasmWrapper(
Isolate* isolate, wasm::ModuleEnv* module, Handle<String> name,
Handle<Code> wasm_code, Handle<JSObject> module_object, uint32_t index) {
wasm::WasmFunction* func = &module->module->functions->at(index);
wasm::WasmFunction* func = &module->module->functions[index];
//----------------------------------------------------------------------------
// Create the JSFunction object.
......
......@@ -146,8 +146,8 @@ class WasmDecoder : public Decoder {
inline bool Validate(const byte* pc, GlobalIndexOperand& operand) {
ModuleEnv* m = function_env_->module;
if (m && m->module && operand.index < m->module->globals->size()) {
operand.machine_type = m->module->globals->at(operand.index).type;
if (m && m->module && operand.index < m->module->globals.size()) {
operand.machine_type = m->module->globals[operand.index].type;
operand.type = WasmOpcodes::LocalTypeFor(operand.machine_type);
return true;
}
......@@ -157,8 +157,8 @@ class WasmDecoder : public Decoder {
inline bool Validate(const byte* pc, FunctionIndexOperand& operand) {
ModuleEnv* m = function_env_->module;
if (m && m->module && operand.index < m->module->functions->size()) {
operand.sig = m->module->functions->at(operand.index).sig;
if (m && m->module && operand.index < m->module->functions.size()) {
operand.sig = m->module->functions[operand.index].sig;
return true;
}
error(pc, pc + 1, "invalid function index");
......@@ -167,8 +167,8 @@ class WasmDecoder : public Decoder {
inline bool Validate(const byte* pc, SignatureIndexOperand& operand) {
ModuleEnv* m = function_env_->module;
if (m && m->module && operand.index < m->module->signatures->size()) {
operand.sig = m->module->signatures->at(operand.index);
if (m && m->module && operand.index < m->module->signatures.size()) {
operand.sig = m->module->signatures[operand.index];
return true;
}
error(pc, pc + 1, "invalid signature index");
......@@ -177,8 +177,8 @@ class WasmDecoder : public Decoder {
inline bool Validate(const byte* pc, ImportIndexOperand& operand) {
ModuleEnv* m = function_env_->module;
if (m && m->module && operand.index < m->module->import_table->size()) {
operand.sig = m->module->import_table->at(operand.index).sig;
if (m && m->module && operand.index < m->module->import_table.size()) {
operand.sig = m->module->import_table[operand.index].sig;
return true;
}
error(pc, pc + 1, "invalid signature index");
......
......@@ -50,13 +50,6 @@ class ModuleDecoder : public Decoder {
module->mem_export = false;
module->mem_external = false;
module->origin = origin_;
module->globals = new std::vector<WasmGlobal>();
module->signatures = new std::vector<FunctionSig*>();
module->functions = new std::vector<WasmFunction>();
module->data_segments = new std::vector<WasmDataSegment>();
module->function_table = new std::vector<uint16_t>();
module->import_table = new std::vector<WasmImport>();
module->export_table = new std::vector<WasmExport>();
bool sections[kMaxModuleSectionCode];
memset(sections, 0, sizeof(sections));
......@@ -106,14 +99,14 @@ class ModuleDecoder : public Decoder {
case kDeclSignatures: {
int length;
uint32_t signatures_count = consume_u32v(&length, "signatures count");
module->signatures->reserve(SafeReserve(signatures_count));
module->signatures.reserve(SafeReserve(signatures_count));
// Decode signatures.
for (uint32_t i = 0; i < signatures_count; i++) {
if (failed()) break;
TRACE("DecodeSignature[%d] module+%d\n", i,
static_cast<int>(pc_ - start_));
FunctionSig* s = consume_sig(); // read function sig.
module->signatures->push_back(s);
module->signatures.push_back(s);
}
break;
}
......@@ -122,7 +115,7 @@ class ModuleDecoder : public Decoder {
CheckForPreviousSection(sections, kDeclSignatures, true);
int length;
uint32_t functions_count = consume_u32v(&length, "functions count");
module->functions->reserve(SafeReserve(functions_count));
module->functions.reserve(SafeReserve(functions_count));
// Set up module environment for verification.
ModuleEnv menv;
menv.module = module;
......@@ -134,15 +127,15 @@ class ModuleDecoder : public Decoder {
TRACE("DecodeFunction[%d] module+%d\n", i,
static_cast<int>(pc_ - start_));
module->functions->push_back(
module->functions.push_back(
{nullptr, i, 0, 0, 0, 0, 0, 0, false, false});
WasmFunction* function = &module->functions->back();
WasmFunction* function = &module->functions.back();
DecodeFunctionInModule(module, function, false);
}
if (ok() && verify_functions) {
for (uint32_t i = 0; i < functions_count; i++) {
if (failed()) break;
WasmFunction* function = &module->functions->at(i);
WasmFunction* function = &module->functions[i];
if (!function->external) {
VerifyFunctionBody(i, &menv, function);
if (result_.failed())
......@@ -155,14 +148,14 @@ class ModuleDecoder : public Decoder {
case kDeclGlobals: {
int length;
uint32_t globals_count = consume_u32v(&length, "globals count");
module->globals->reserve(SafeReserve(globals_count));
module->globals.reserve(SafeReserve(globals_count));
// Decode globals.
for (uint32_t i = 0; i < globals_count; i++) {
if (failed()) break;
TRACE("DecodeGlobal[%d] module+%d\n", i,
static_cast<int>(pc_ - start_));
module->globals->push_back({0, MachineType::Int32(), 0, false});
WasmGlobal* global = &module->globals->back();
module->globals.push_back({0, MachineType::Int32(), 0, false});
WasmGlobal* global = &module->globals.back();
DecodeGlobalInModule(global);
}
break;
......@@ -171,14 +164,14 @@ class ModuleDecoder : public Decoder {
int length;
uint32_t data_segments_count =
consume_u32v(&length, "data segments count");
module->data_segments->reserve(SafeReserve(data_segments_count));
module->data_segments.reserve(SafeReserve(data_segments_count));
// Decode data segments.
for (uint32_t i = 0; i < data_segments_count; i++) {
if (failed()) break;
TRACE("DecodeDataSegment[%d] module+%d\n", i,
static_cast<int>(pc_ - start_));
module->data_segments->push_back({0, 0, 0});
WasmDataSegment* segment = &module->data_segments->back();
module->data_segments.push_back({0, 0, 0});
WasmDataSegment* segment = &module->data_segments.back();
DecodeDataSegmentInModule(module, segment);
}
break;
......@@ -189,18 +182,18 @@ class ModuleDecoder : public Decoder {
int length;
uint32_t function_table_count =
consume_u32v(&length, "function table count");
module->function_table->reserve(SafeReserve(function_table_count));
module->function_table.reserve(SafeReserve(function_table_count));
// Decode function table.
for (uint32_t i = 0; i < function_table_count; i++) {
if (failed()) break;
TRACE("DecodeFunctionTable[%d] module+%d\n", i,
static_cast<int>(pc_ - start_));
uint16_t index = consume_u16();
if (index >= module->functions->size()) {
if (index >= module->functions.size()) {
error(pc_ - 2, "invalid function index");
break;
}
module->function_table->push_back(index);
module->function_table.push_back(index);
}
break;
}
......@@ -214,13 +207,13 @@ class ModuleDecoder : public Decoder {
int length;
const byte* before = pc_;
uint32_t index = consume_u32v(&length, "start function index");
if (index >= module->functions->size()) {
if (index >= module->functions.size()) {
error(before, "invalid start function index");
break;
}
module->start_function_index = static_cast<int>(index);
FunctionSig* sig =
module->signatures->at(module->functions->at(index).sig_index);
module->signatures[module->functions[index].sig_index];
if (sig->parameter_count() > 0) {
error(before, "invalid start function: non-zero parameter count");
break;
......@@ -233,23 +226,23 @@ class ModuleDecoder : public Decoder {
int length;
uint32_t import_table_count =
consume_u32v(&length, "import table count");
module->import_table->reserve(SafeReserve(import_table_count));
module->import_table.reserve(SafeReserve(import_table_count));
// Decode import table.
for (uint32_t i = 0; i < import_table_count; i++) {
if (failed()) break;
TRACE("DecodeImportTable[%d] module+%d\n", i,
static_cast<int>(pc_ - start_));
module->import_table->push_back({nullptr, 0, 0});
WasmImport* import = &module->import_table->back();
module->import_table.push_back({nullptr, 0, 0});
WasmImport* import = &module->import_table.back();
const byte* sigpos = pc_;
import->sig_index = consume_u16("signature index");
if (import->sig_index >= module->signatures->size()) {
if (import->sig_index >= module->signatures.size()) {
error(sigpos, "invalid signature index");
} else {
import->sig = module->signatures->at(import->sig_index);
import->sig = module->signatures[import->sig_index];
}
import->module_name_offset = consume_string("import module name");
import->function_name_offset =
......@@ -263,23 +256,23 @@ class ModuleDecoder : public Decoder {
int length;
uint32_t export_table_count =
consume_u32v(&length, "export table count");
module->export_table->reserve(SafeReserve(export_table_count));
module->export_table.reserve(SafeReserve(export_table_count));
// Decode export table.
for (uint32_t i = 0; i < export_table_count; i++) {
if (failed()) break;
TRACE("DecodeExportTable[%d] module+%d\n", i,
static_cast<int>(pc_ - start_));
module->export_table->push_back({0, 0});
WasmExport* exp = &module->export_table->back();
module->export_table.push_back({0, 0});
WasmExport* exp = &module->export_table.back();
const byte* sigpos = pc_;
exp->func_index = consume_u16("function index");
if (exp->func_index >= module->functions->size()) {
if (exp->func_index >= module->functions.size()) {
error(sigpos, sigpos,
"function index %u out of bounds (%d functions)",
exp->func_index,
static_cast<int>(module->functions->size()));
static_cast<int>(module->functions.size()));
}
exp->name_offset = consume_string("export name");
}
......@@ -402,10 +395,10 @@ class ModuleDecoder : public Decoder {
const byte* sigpos = pc_;
function->sig_index = consume_u16("signature index");
if (function->sig_index >= module->signatures->size()) {
if (function->sig_index >= module->signatures.size()) {
return error(sigpos, "invalid signature index");
} else {
function->sig = module->signatures->at(function->sig_index);
function->sig = module->signatures[function->sig_index];
}
TRACE(" +%d <function attributes:%s%s%s%s%s>\n",
......
......@@ -23,9 +23,9 @@ std::ostream& operator<<(std::ostream& os, const WasmModule& module) {
os << "WASM module with ";
os << (1 << module.min_mem_size_log2) << " min mem";
os << (1 << module.max_mem_size_log2) << " max mem";
if (module.functions) os << module.functions->size() << " functions";
if (module.globals) os << module.functions->size() << " globals";
if (module.data_segments) os << module.functions->size() << " data segments";
os << module.functions.size() << " functions";
os << module.functions.size() << " globals";
os << module.functions.size() << " data segments";
return os;
}
......@@ -91,15 +91,15 @@ class WasmLinker {
}
void Link(Handle<FixedArray> function_table,
std::vector<uint16_t>* functions) {
std::vector<uint16_t>& functions) {
for (size_t i = 0; i < function_code_.size(); i++) {
LinkFunction(function_code_[i]);
}
if (functions && !function_table.is_null()) {
int table_size = static_cast<int>(functions->size());
if (!function_table.is_null()) {
int table_size = static_cast<int>(functions.size());
DCHECK_EQ(function_table->length(), table_size * 2);
for (int i = 0; i < table_size; i++) {
function_table->set(i + table_size, *function_code_[functions->at(i)]);
function_table->set(i + table_size, *function_code_[functions[i]]);
}
}
}
......@@ -151,11 +151,10 @@ const int kWasmModuleCodeTable = 1;
const int kWasmMemArrayBuffer = 2;
const int kWasmGlobalsArrayBuffer = 3;
size_t AllocateGlobalsOffsets(std::vector<WasmGlobal>* globals) {
size_t AllocateGlobalsOffsets(std::vector<WasmGlobal>& globals) {
uint32_t offset = 0;
if (!globals) return 0;
for (WasmGlobal& global : *globals) {
if (globals.size() == 0) return 0;
for (WasmGlobal& global : globals) {
byte size = WasmOpcodes::MemSize(global.type);
offset = (offset + size - 1) & ~(size - 1); // align
global.offset = offset;
......@@ -166,7 +165,7 @@ size_t AllocateGlobalsOffsets(std::vector<WasmGlobal>* globals) {
void LoadDataSegments(WasmModule* module, byte* mem_addr, size_t mem_size) {
for (const WasmDataSegment& segment : *module->data_segments) {
for (const WasmDataSegment& segment : module->data_segments) {
if (!segment.init) continue;
CHECK_LT(segment.dest_addr, mem_size);
CHECK_LE(segment.source_size, mem_size);
......@@ -179,14 +178,13 @@ void LoadDataSegments(WasmModule* module, byte* mem_addr, size_t mem_size) {
Handle<FixedArray> BuildFunctionTable(Isolate* isolate, WasmModule* module) {
if (!module->function_table || module->function_table->size() == 0) {
if (module->function_table.size() == 0) {
return Handle<FixedArray>::null();
}
int table_size = static_cast<int>(module->function_table->size());
int table_size = static_cast<int>(module->function_table.size());
Handle<FixedArray> fixed = isolate->factory()->NewFixedArray(2 * table_size);
for (int i = 0; i < table_size; i++) {
WasmFunction* function =
&module->functions->at(module->function_table->at(i));
WasmFunction* function = &module->functions[module->function_table[i]];
fixed->set(i, Smi::FromInt(function->sig_index));
}
return fixed;
......@@ -278,24 +276,7 @@ WasmModule::WasmModule()
mem_export(false),
mem_external(false),
start_function_index(-1),
origin(kWasmOrigin),
globals(nullptr),
signatures(nullptr),
functions(nullptr),
data_segments(nullptr),
function_table(nullptr),
import_table(nullptr),
export_table(nullptr) {}
WasmModule::~WasmModule() {
if (globals) delete globals;
if (signatures) delete signatures;
if (functions) delete functions;
if (data_segments) delete data_segments;
if (function_table) delete function_table;
if (import_table) delete import_table;
if (export_table) delete export_table;
}
origin(kWasmOrigin) {}
static MaybeHandle<JSFunction> LookupFunction(ErrorThrower& thrower,
Handle<JSObject> ffi,
......@@ -341,11 +322,10 @@ MaybeHandle<JSObject> WasmModule::Instantiate(Isolate* isolate,
JS_OBJECT_TYPE,
JSObject::kHeaderSize + kWasmModuleInternalFieldCount * kPointerSize);
WasmModuleInstance instance(this);
std::vector<Handle<Code>> import_code;
instance.context = isolate->native_context();
instance.js_object = factory->NewJSObjectFromMap(map, TENURED);
Handle<FixedArray> code_table =
factory->NewFixedArray(static_cast<int>(functions->size()), TENURED);
factory->NewFixedArray(static_cast<int>(functions.size()), TENURED);
instance.js_object->SetInternalField(kWasmModuleCodeTable, *code_table);
//-------------------------------------------------------------------------
......@@ -385,17 +365,16 @@ MaybeHandle<JSObject> WasmModule::Instantiate(Isolate* isolate,
//-------------------------------------------------------------------------
uint32_t index = 0;
instance.function_table = BuildFunctionTable(isolate, this);
WasmLinker linker(isolate, functions->size());
WasmLinker linker(isolate, functions.size());
ModuleEnv module_env;
module_env.module = this;
module_env.instance = &instance;
module_env.linker = &linker;
module_env.origin = origin;
if (import_table->size() > 0) {
instance.import_code = &import_code;
instance.import_code->reserve(import_table->size());
for (const WasmImport& import : *import_table) {
if (import_table.size() > 0) {
instance.import_code.reserve(import_table.size());
for (const WasmImport& import : import_table) {
const char* cstr = GetName(import.function_name_offset);
Handle<String> name = factory->InternalizeUtf8String(cstr);
MaybeHandle<JSFunction> function =
......@@ -403,7 +382,7 @@ MaybeHandle<JSObject> WasmModule::Instantiate(Isolate* isolate,
if (function.is_null()) return MaybeHandle<JSObject>();
Handle<Code> code = compiler::CompileWasmToJSWrapper(
isolate, &module_env, function.ToHandleChecked(), import.sig, cstr);
instance.import_code->push_back(code);
instance.import_code.push_back(code);
index++;
}
}
......@@ -414,7 +393,7 @@ MaybeHandle<JSObject> WasmModule::Instantiate(Isolate* isolate,
// First pass: compile each function and initialize the code table.
index = 0;
for (const WasmFunction& func : *functions) {
for (const WasmFunction& func : functions) {
if (thrower.error()) break;
DCHECK_EQ(index, func.func_index);
......@@ -461,7 +440,7 @@ MaybeHandle<JSObject> WasmModule::Instantiate(Isolate* isolate,
//-------------------------------------------------------------------------
// Create and populate the exports object.
//-------------------------------------------------------------------------
if (export_table->size() > 0) {
if (export_table.size() > 0) {
index = 0;
// Create the "exports" object.
Handle<JSFunction> object_function = Handle<JSFunction>(
......@@ -473,7 +452,7 @@ MaybeHandle<JSObject> WasmModule::Instantiate(Isolate* isolate,
READ_ONLY);
// Compile wrappers and add them to the exports object.
for (const WasmExport& exp : *export_table) {
for (const WasmExport& exp : export_table) {
if (thrower.error()) break;
const char* cstr = GetName(exp.name_offset);
Handle<String> name = factory->InternalizeUtf8String(cstr);
......@@ -509,18 +488,12 @@ MaybeHandle<JSObject> WasmModule::Instantiate(Isolate* isolate,
Handle<Code> ModuleEnv::GetFunctionCode(uint32_t index) {
DCHECK(IsValidFunction(index));
if (linker) return linker->GetFunctionCode(index);
if (instance && instance->function_code) {
return instance->function_code->at(index);
}
return Handle<Code>::null();
return instance ? instance->function_code[index] : Handle<Code>::null();
}
Handle<Code> ModuleEnv::GetImportCode(uint32_t index) {
DCHECK(IsValidImport(index));
if (instance && instance->import_code) {
return instance->import_code->at(index);
}
return Handle<Code>::null();
return instance ? instance->import_code[index] : Handle<Code>::null();
}
compiler::CallDescriptor* ModuleEnv::GetCallDescriptor(Zone* zone,
......@@ -528,7 +501,7 @@ compiler::CallDescriptor* ModuleEnv::GetCallDescriptor(Zone* zone,
DCHECK(IsValidFunction(index));
// Always make a direct call to whatever is in the table at that location.
// A wrapper will be generated for FFI calls.
WasmFunction* function = &module->functions->at(index);
WasmFunction* function = &module->functions[index];
return GetWasmCallDescriptor(zone, function->sig);
}
......@@ -575,7 +548,7 @@ int32_t CompileAndRunWasmModule(Isolate* isolate, WasmModule* module) {
instance.function_table = BuildFunctionTable(isolate, module);
// Create module environment.
WasmLinker linker(isolate, module->functions->size());
WasmLinker linker(isolate, module->functions.size());
ModuleEnv module_env;
module_env.module = module;
module_env.instance = &instance;
......@@ -586,7 +559,7 @@ int32_t CompileAndRunWasmModule(Isolate* isolate, WasmModule* module) {
Handle<Code> main_code = Handle<Code>::null(); // record last code.
uint32_t index = 0;
int main_index = 0;
for (const WasmFunction& func : *module->functions) {
for (const WasmFunction& func : module->functions) {
DCHECK_EQ(index, func.func_index);
if (!func.external) {
// Compile the function and install it in the code table.
......
......@@ -116,16 +116,15 @@ struct WasmModule {
int start_function_index; // start function, if any.
ModuleOrigin origin; // origin of the module
std::vector<WasmGlobal>* globals; // globals in this module.
std::vector<FunctionSig*>* signatures; // signatures in this module.
std::vector<WasmFunction>* functions; // functions in this module.
std::vector<WasmDataSegment>* data_segments; // data segments in this module.
std::vector<uint16_t>* function_table; // function table.
std::vector<WasmImport>* import_table; // import table.
std::vector<WasmExport>* export_table; // export table.
std::vector<WasmGlobal> globals; // globals in this module.
std::vector<FunctionSig*> signatures; // signatures in this module.
std::vector<WasmFunction> functions; // functions in this module.
std::vector<WasmDataSegment> data_segments; // data segments in this module.
std::vector<uint16_t> function_table; // function table.
std::vector<WasmImport> import_table; // import table.
std::vector<WasmExport> export_table; // export table.
WasmModule();
~WasmModule();
// Get a pointer to a string stored in the module bytes representing a name.
const char* GetName(uint32_t offset) const {
......@@ -154,8 +153,8 @@ struct WasmModuleInstance {
Handle<JSArrayBuffer> mem_buffer; // Handle to array buffer of memory.
Handle<JSArrayBuffer> globals_buffer; // Handle to array buffer of globals.
Handle<FixedArray> function_table; // indirect function table.
std::vector<Handle<Code>>* function_code; // code objects for each function.
std::vector<Handle<Code>>* import_code; // code objects for each import.
std::vector<Handle<Code>> function_code; // code objects for each function.
std::vector<Handle<Code>> import_code; // code objects for each import.
// -- raw memory ------------------------------------------------------------
byte* mem_start; // start of linear memory.
size_t mem_size; // size of the linear memory.
......@@ -165,7 +164,6 @@ struct WasmModuleInstance {
explicit WasmModuleInstance(WasmModule* m)
: module(m),
function_code(nullptr),
mem_start(nullptr),
mem_size(0),
globals_start(nullptr),
......@@ -184,36 +182,35 @@ struct ModuleEnv {
ModuleOrigin origin;
bool IsValidGlobal(uint32_t index) {
return module && index < module->globals->size();
return module && index < module->globals.size();
}
bool IsValidFunction(uint32_t index) {
return module && index < module->functions->size();
return module && index < module->functions.size();
}
bool IsValidSignature(uint32_t index) {
return module && index < module->signatures->size();
return module && index < module->signatures.size();
}
bool IsValidImport(uint32_t index) {
return module && index < module->import_table->size();
return module && index < module->import_table.size();
}
MachineType GetGlobalType(uint32_t index) {
DCHECK(IsValidGlobal(index));
return module->globals->at(index).type;
return module->globals[index].type;
}
FunctionSig* GetFunctionSignature(uint32_t index) {
DCHECK(IsValidFunction(index));
return module->functions->at(index).sig;
return module->functions[index].sig;
}
FunctionSig* GetImportSignature(uint32_t index) {
DCHECK(IsValidImport(index));
return module->import_table->at(index).sig;
return module->import_table[index].sig;
}
FunctionSig* GetSignature(uint32_t index) {
DCHECK(IsValidSignature(index));
return module->signatures->at(index);
return module->signatures[index];
}
size_t FunctionTableSize() {
return module && module->function_table ? module->function_table->size()
: 0;
return module ? module->function_table.size() : 0;
}
bool asm_js() { return origin == kAsmJsOrigin; }
......
......@@ -54,11 +54,11 @@ uint32_t AddJsFunction(TestingModule* module, FunctionSig* sig,
Handle<JSFunction> jsfunc = Handle<JSFunction>::cast(v8::Utils::OpenHandle(
*v8::Local<v8::Function>::Cast(CompileRun(source))));
module->AddFunction(sig, Handle<Code>::null());
uint32_t index = static_cast<uint32_t>(module->module->functions->size() - 1);
uint32_t index = static_cast<uint32_t>(module->module->functions.size() - 1);
Isolate* isolate = CcTest::InitIsolateOnce();
Handle<Code> code =
CompileWasmToJSWrapper(isolate, module, jsfunc, sig, "test");
module->instance->function_code->at(index) = code;
module->instance->function_code[index] = code;
return index;
}
......@@ -86,7 +86,7 @@ Handle<JSFunction> WrapCode(ModuleEnv* module, uint32_t index) {
// Wrap the code so it can be called as a JS function.
Handle<String> name = isolate->factory()->NewStringFromStaticChars("main");
Handle<JSObject> module_object = Handle<JSObject>(0, isolate);
Handle<Code> code = module->instance->function_code->at(index);
Handle<Code> code = module->instance->function_code[index];
WasmJs::InstallWasmFunctionMap(isolate, isolate->native_context());
return compiler::CompileJSToWasmWrapper(isolate, module, name, code,
module_object, index);
......
......@@ -89,7 +89,6 @@ class TestingModule : public ModuleEnv {
instance->globals_size = kMaxGlobalsSize;
instance->mem_start = nullptr;
instance->mem_size = 0;
instance->function_code = nullptr;
linker = nullptr;
origin = kWasmOrigin;
memset(global_data, 0, sizeof(global_data));
......@@ -99,9 +98,6 @@ class TestingModule : public ModuleEnv {
if (instance->mem_start) {
free(instance->mem_start);
}
if (instance->function_code) {
delete instance->function_code;
}
}
byte* AddMemory(size_t size) {
......@@ -127,11 +123,8 @@ class TestingModule : public ModuleEnv {
}
byte AddSignature(FunctionSig* sig) {
if (!module->signatures) {
module->signatures = new std::vector<FunctionSig*>();
}
module->signatures->push_back(sig);
size_t size = module->signatures->size();
module->signatures.push_back(sig);
size_t size = module->signatures.size();
CHECK(size < 127);
return static_cast<byte>(size - 1);
}
......@@ -177,23 +170,21 @@ class TestingModule : public ModuleEnv {
}
int AddFunction(FunctionSig* sig, Handle<Code> code) {
if (module->functions == nullptr) {
module->functions = new std::vector<WasmFunction>();
if (module->functions.size() == 0) {
// TODO(titzer): Reserving space here to avoid the underlying WasmFunction
// structs from moving.
module->functions->reserve(kMaxFunctions);
instance->function_code = new std::vector<Handle<Code>>();
module->functions.reserve(kMaxFunctions);
}
uint32_t index = static_cast<uint32_t>(module->functions->size());
module->functions->push_back(
uint32_t index = static_cast<uint32_t>(module->functions.size());
module->functions.push_back(
{sig, index, 0, 0, 0, 0, 0, 0, 0, false, false});
instance->function_code->push_back(code);
instance->function_code.push_back(code);
DCHECK_LT(index, kMaxFunctions); // limited for testing.
return index;
}
void SetFunctionCode(uint32_t index, Handle<Code> code) {
instance->function_code->at(index) = code;
instance->function_code[index] = code;
}
void AddIndirectFunctionTable(int* functions, int table_size) {
......@@ -201,21 +192,21 @@ class TestingModule : public ModuleEnv {
Handle<FixedArray> fixed =
isolate->factory()->NewFixedArray(2 * table_size);
instance->function_table = fixed;
module->function_table = new std::vector<uint16_t>();
DCHECK_EQ(0u, module->function_table.size());
for (int i = 0; i < table_size; i++) {
module->function_table->push_back(functions[i]);
module->function_table.push_back(functions[i]);
}
}
void PopulateIndirectFunctionTable() {
if (instance->function_table.is_null()) return;
int table_size = static_cast<int>(module->function_table->size());
int table_size = static_cast<int>(module->function_table.size());
for (int i = 0; i < table_size; i++) {
int function_index = module->function_table->at(i);
WasmFunction* function = &module->functions->at(function_index);
int function_index = module->function_table[i];
WasmFunction* function = &module->functions[function_index];
instance->function_table->set(i, Smi::FromInt(function->sig_index));
instance->function_table->set(
i + table_size, *instance->function_code->at(function_index));
instance->function_table->set(i + table_size,
*instance->function_code[function_index]);
}
}
......@@ -226,16 +217,13 @@ class TestingModule : public ModuleEnv {
V8_ALIGNED(8) byte global_data[kMaxGlobalsSize]; // preallocated global data.
WasmGlobal* AddGlobal(MachineType mem_type) {
if (!module->globals) {
module->globals = new std::vector<WasmGlobal>();
}
byte size = WasmOpcodes::MemSize(mem_type);
global_offset = (global_offset + size - 1) & ~(size - 1); // align
module->globals->push_back({0, mem_type, global_offset, false});
module->globals.push_back({0, mem_type, global_offset, false});
global_offset += size;
// limit number of globals.
CHECK_LT(global_offset, kMaxGlobalsSize);
return &module->globals->back();
return &module->globals.back();
}
};
......@@ -501,7 +489,7 @@ class WasmFunctionCompiler : public HandleAndZoneScope,
WasmFunction* function() {
if (function_) return function_;
return &testing_module_->module->functions->at(function_index_);
return &testing_module_->module->functions[function_index_];
}
};
......
......@@ -1189,30 +1189,26 @@ class TestModuleEnv : public ModuleEnv {
instance = nullptr;
module = &mod;
linker = nullptr;
mod.globals = new std::vector<WasmGlobal>;
mod.signatures = new std::vector<FunctionSig*>;
mod.functions = new std::vector<WasmFunction>;
mod.import_table = new std::vector<WasmImport>;
}
byte AddGlobal(MachineType mem_type) {
mod.globals->push_back({0, mem_type, 0, false});
CHECK(mod.globals->size() <= 127);
return static_cast<byte>(mod.globals->size() - 1);
mod.globals.push_back({0, mem_type, 0, false});
CHECK(mod.globals.size() <= 127);
return static_cast<byte>(mod.globals.size() - 1);
}
byte AddSignature(FunctionSig* sig) {
mod.signatures->push_back(sig);
CHECK(mod.signatures->size() <= 127);
return static_cast<byte>(mod.signatures->size() - 1);
mod.signatures.push_back(sig);
CHECK(mod.signatures.size() <= 127);
return static_cast<byte>(mod.signatures.size() - 1);
}
byte AddFunction(FunctionSig* sig) {
mod.functions->push_back({sig, 0, 0, 0, 0, 0, 0, 0, false, false});
CHECK(mod.functions->size() <= 127);
return static_cast<byte>(mod.functions->size() - 1);
mod.functions.push_back({sig, 0, 0, 0, 0, 0, 0, 0, false, false});
CHECK(mod.functions.size() <= 127);
return static_cast<byte>(mod.functions.size() - 1);
}
byte AddImport(FunctionSig* sig) {
mod.import_table->push_back({sig, 0, 0});
CHECK(mod.import_table->size() <= 127);
return static_cast<byte>(mod.import_table->size() - 1);
mod.import_table.push_back({sig, 0, 0});
CHECK(mod.import_table.size() <= 127);
return static_cast<byte>(mod.import_table.size() - 1);
}
private:
......
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