Commit 37088878 authored by Clemens Hammacher's avatar Clemens Hammacher Committed by Commit Bot

[wasm] Clean up methods in NativeModule

Define simple accessors in the header and give them lower case names.

R=mstarzinger@chromium.org

Bug: v8:7570
Change-Id: I2914013fdea2218189275bbaa9f98ea5de0ccd7c
Reviewed-on: https://chromium-review.googlesource.com/1046546
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Reviewed-by: 's avatarMichael Starzinger <mstarzinger@chromium.org>
Cr-Commit-Position: refs/heads/master@{#53024}
parent 328f0977
......@@ -1749,7 +1749,7 @@ bool Isolate::ComputeLocationFromStackTrace(MessageLocation* target,
// a second lookup here could lead to inconsistency.
int byte_offset =
FrameSummary::WasmCompiledFrameSummary::GetWasmSourcePosition(
compiled_module->GetNativeModule()->GetCode(func_index),
compiled_module->GetNativeModule()->code(func_index),
code_offset);
bool is_at_number_conversion =
......
......@@ -650,7 +650,7 @@ void WasmStackFrame::FromFrameArray(Isolate* isolate, Handle<FrameArray> array,
if (array->IsWasmInterpretedFrame(frame_ix)) {
code_ = nullptr;
} else {
code_ = wasm_instance_->compiled_module()->GetNativeModule()->GetCode(
code_ = wasm_instance_->compiled_module()->GetNativeModule()->code(
wasm_func_index_);
}
offset_ = array->Offset(frame_ix)->value();
......
......@@ -439,7 +439,7 @@ class IndirectPatcher {
DCHECK(!entry.is_js_receiver_entry());
WasmInstanceObject* target_instance = entry.instance();
WasmCode* new_code =
target_instance->compiled_module()->GetNativeModule()->GetCode(
target_instance->compiled_module()->GetNativeModule()->code(
code->index());
if (new_code->kind() != WasmCode::kLazyStub) {
// Patch an imported function entry which is already compiled.
......@@ -461,7 +461,7 @@ class IndirectPatcher {
code->instructions().start());
WasmInstanceObject* target_instance = entry.instance();
WasmCode* new_code =
target_instance->compiled_module()->GetNativeModule()->GetCode(
target_instance->compiled_module()->GetNativeModule()->code(
code->index());
if (new_code->kind() != WasmCode::kLazyStub) {
// Patch an indirect function table entry which is already compiled.
......@@ -492,7 +492,7 @@ const wasm::WasmCode* LazyCompileFunction(
Isolate* isolate, Handle<WasmCompiledModule> compiled_module,
int func_index) {
base::ElapsedTimer compilation_timer;
wasm::WasmCode* existing_code = compiled_module->GetNativeModule()->GetCode(
wasm::WasmCode* existing_code = compiled_module->GetNativeModule()->code(
static_cast<uint32_t>(func_index));
if (existing_code != nullptr &&
existing_code->kind() == wasm::WasmCode::kFunction) {
......@@ -602,7 +602,7 @@ const wasm::WasmCode* LazyCompileFromJsToWasm(
RelocInfo::ModeMask(RelocInfo::JS_TO_WASM_CALL));
DCHECK(!it.done());
const wasm::WasmCode* callee_compiled =
compiled_module->GetNativeModule()->GetCode(callee_func_index);
compiled_module->GetNativeModule()->code(callee_func_index);
DCHECK_NOT_NULL(callee_compiled);
DCHECK_EQ(WasmCode::kLazyStub,
isolate->wasm_engine()
......@@ -619,7 +619,7 @@ const wasm::WasmCode* LazyCompileFromJsToWasm(
}
wasm::WasmCode* ret =
compiled_module->GetNativeModule()->GetCode(callee_func_index);
compiled_module->GetNativeModule()->code(callee_func_index);
DCHECK_NOT_NULL(ret);
DCHECK_EQ(wasm::WasmCode::kFunction, ret->kind());
return ret;
......@@ -685,7 +685,7 @@ const wasm::WasmCode* LazyCompileDirectCall(Isolate* isolate,
int32_t callee_func_index =
ExtractDirectCallIndex(decoder, func_bytes + byte_pos);
DCHECK_LT(callee_func_index,
wasm_caller->native_module()->FunctionCount());
wasm_caller->native_module()->function_count());
// {caller_ret_offset} points to one instruction after the call.
// Remember the last called function before that offset.
if (offset < caller_ret_offset) {
......@@ -733,7 +733,7 @@ const wasm::WasmCode* LazyCompileDirectCall(Isolate* isolate,
auto callee_index = direct_callees[pos];
if (callee_index < 0) continue; // callee already compiled.
const WasmCode* callee_compiled =
compiled_module->GetNativeModule()->GetCode(callee_index);
compiled_module->GetNativeModule()->code(callee_index);
if (callee_compiled->kind() != WasmCode::kFunction) continue;
DCHECK_EQ(WasmCode::kLazyStub,
isolate->wasm_engine()
......@@ -871,8 +871,8 @@ bool compile_lazy(const WasmModule* module) {
}
void FlushICache(const wasm::NativeModule* native_module) {
for (uint32_t i = 0, e = native_module->FunctionCount(); i < e; ++i) {
const wasm::WasmCode* code = native_module->GetCode(i);
for (uint32_t i = 0, e = native_module->function_count(); i < e; ++i) {
const wasm::WasmCode* code = native_module->code(i);
if (code == nullptr) continue;
Assembler::FlushICache(code->instructions().start(),
code->instructions().size());
......@@ -913,8 +913,8 @@ void RecordStats(Handle<FixedArray> functions, Counters* counters) {
}
void RecordStats(const wasm::NativeModule* native_module, Counters* counters) {
for (uint32_t i = 0, e = native_module->FunctionCount(); i < e; ++i) {
const wasm::WasmCode* code = native_module->GetCode(i);
for (uint32_t i = 0, e = native_module->function_count(); i < e; ++i) {
const wasm::WasmCode* code = native_module->code(i);
if (code != nullptr) RecordStats(code, counters);
}
}
......@@ -2170,7 +2170,7 @@ int InstanceBuilder::ProcessImports(Handle<WasmInstanceObject> instance) {
auto wasm_code = imported_function->GetWasmCode();
ImportedFunctionEntry(instance, func_index)
.set(*imported_instance, wasm_code);
native_module->SetCode(func_index, wasm_code);
native_module->set_code(func_index, wasm_code);
} else {
// The imported function is a callable.
Handle<JSReceiver> js_receiver(JSReceiver::cast(*value), isolate_);
......
......@@ -357,7 +357,7 @@ void NativeModule::ResizeCodeTableForTesting(size_t num_functions,
DCHECK_EQ(num_functions, 1);
code_table_.reserve(max_functions);
} else {
DCHECK_GT(num_functions, FunctionCount());
DCHECK_GT(num_functions, function_count());
if (code_table_.capacity() == 0) {
code_table_.reserve(max_functions);
}
......@@ -366,21 +366,6 @@ void NativeModule::ResizeCodeTableForTesting(size_t num_functions,
}
}
WasmCode* NativeModule::GetCode(uint32_t index) const {
DCHECK_LT(index, FunctionCount());
return code_table_[index];
}
void NativeModule::SetCode(uint32_t index, WasmCode* wasm_code) {
DCHECK_LT(index, FunctionCount());
code_table_[index] = wasm_code;
}
uint32_t NativeModule::FunctionCount() const {
DCHECK_LE(code_table_.size(), std::numeric_limits<uint32_t>::max());
return static_cast<uint32_t>(code_table_.size());
}
WasmCode* NativeModule::AddOwnedCode(
Vector<const byte> orig_instructions,
std::unique_ptr<const byte[]> reloc_info, size_t reloc_size,
......@@ -439,7 +424,8 @@ WasmCode* NativeModule::AddInterpreterWrapper(Handle<Code> code,
void NativeModule::SetLazyBuiltin(Handle<Code> code) {
WasmCode* lazy_builtin = AddAnonymousCode(code, WasmCode::kLazyStub);
for (uint32_t i = num_imported_functions(), e = FunctionCount(); i < e; ++i) {
for (uint32_t i = num_imported_functions(), e = function_count(); i < e;
++i) {
code_table_[i] = lazy_builtin;
}
}
......@@ -714,40 +700,35 @@ WasmCode* NativeModule::Lookup(Address pc) {
--iter;
WasmCode* candidate = (*iter).get();
DCHECK_NOT_NULL(candidate);
Address instructions_start = candidate->instruction_start();
if (instructions_start <= pc &&
pc < instructions_start + candidate->instructions().size()) {
return candidate;
}
return nullptr;
return candidate->contains(pc) ? candidate : nullptr;
}
WasmCode* NativeModule::GetIndirectlyCallableCode(uint32_t func_index) {
WasmCode* code = GetCode(func_index);
if (!code || code->kind() != WasmCode::kLazyStub) {
return code;
WasmCode* wasm_code = code(func_index);
if (!wasm_code || wasm_code->kind() != WasmCode::kLazyStub) {
return wasm_code;
}
#if DEBUG
auto num_imported_functions =
shared_module_data()->module()->num_imported_functions;
if (func_index < num_imported_functions) {
DCHECK(!code->IsAnonymous());
DCHECK(!wasm_code->IsAnonymous());
}
#endif
if (!code->IsAnonymous()) {
if (!wasm_code->IsAnonymous()) {
// If the function wasn't imported, its index should match.
DCHECK_IMPLIES(func_index >= num_imported_functions,
func_index == code->index());
return code;
func_index == wasm_code->index());
return wasm_code;
}
if (!lazy_compile_stubs_.get()) {
lazy_compile_stubs_ =
base::make_unique<std::vector<WasmCode*>>(FunctionCount());
base::make_unique<std::vector<WasmCode*>>(function_count());
}
WasmCode* cloned_code = lazy_compile_stubs_.get()->at(func_index);
if (cloned_code == nullptr) {
cloned_code = CloneCode(code, WasmCode::kNoFlushICache);
RelocateCode(cloned_code, code, WasmCode::kFlushICache);
cloned_code = CloneCode(wasm_code, WasmCode::kNoFlushICache);
RelocateCode(cloned_code, wasm_code, WasmCode::kFlushICache);
cloned_code->index_ = Just(func_index);
lazy_compile_stubs_.get()->at(func_index) = cloned_code;
}
......@@ -787,16 +768,18 @@ WasmCode* NativeModule::CloneCode(const WasmCode* original_code,
}
void NativeModule::UnpackAndRegisterProtectedInstructions() {
for (uint32_t i = num_imported_functions(), e = FunctionCount(); i < e; ++i) {
WasmCode* code = GetCode(i);
if (code == nullptr) continue;
code->RegisterTrapHandlerData();
for (uint32_t i = num_imported_functions(), e = function_count(); i < e;
++i) {
WasmCode* wasm_code = code(i);
if (wasm_code == nullptr) continue;
wasm_code->RegisterTrapHandlerData();
}
}
void NativeModule::ReleaseProtectedInstructions() {
for (uint32_t i = num_imported_functions(), e = FunctionCount(); i < e; ++i) {
WasmCode* wasm_code = GetCode(i);
for (uint32_t i = num_imported_functions(), e = function_count(); i < e;
++i) {
WasmCode* wasm_code = code(i);
if (wasm_code->HasTrapHandlerIndex()) {
CHECK_LT(wasm_code->trap_handler_index(),
static_cast<size_t>(std::numeric_limits<int>::max()));
......@@ -1032,9 +1015,7 @@ WasmCode* WasmCodeManager::LookupCode(Address pc) const {
NativeModule* candidate = iter->second.second;
DCHECK_NOT_NULL(candidate);
if (range_start <= pc && pc < range_end) {
return candidate->Lookup(pc);
}
if (range_start <= pc && pc < range_end) return candidate->Lookup(pc);
return nullptr;
}
......
......@@ -118,6 +118,10 @@ class V8_EXPORT_PRIVATE WasmCode final {
size_t handler_table_offset() const { return handler_table_offset_; }
uint32_t stack_slots() const { return stack_slots_; }
bool is_liftoff() const { return tier_ == kLiftoff; }
bool contains(Address pc) const {
return reinterpret_cast<Address>(instructions_.start()) <= pc &&
pc < reinterpret_cast<Address>(instructions_.end());
}
const ProtectedInstructions& protected_instructions() const {
// TODO(mstarzinger): Code that doesn't have trapping instruction should
......@@ -239,10 +243,21 @@ class V8_EXPORT_PRIVATE NativeModule final {
// by the runtime.
void SetLazyBuiltin(Handle<Code> code);
// FunctionCount is WasmModule::functions.size().
uint32_t FunctionCount() const;
WasmCode* GetCode(uint32_t index) const;
void SetCode(uint32_t index, WasmCode* wasm_code);
// function_count is WasmModule::functions.size().
uint32_t function_count() const {
DCHECK_LE(code_table_.size(), std::numeric_limits<uint32_t>::max());
return static_cast<uint32_t>(code_table_.size());
}
WasmCode* code(uint32_t index) const {
DCHECK_LT(index, function_count());
return code_table_[index];
}
void set_code(uint32_t index, WasmCode* wasm_code) {
DCHECK_LT(index, function_count());
code_table_[index] = wasm_code;
}
// Register/release the protected instructions in all code objects with the
// global trap handler for this process.
......
......@@ -92,7 +92,7 @@ bool CodeSpecialization::ApplyToWholeModule(
// Patch all wasm functions.
for (int num_wasm_functions = static_cast<int>(wasm_functions->size());
func_index < num_wasm_functions; ++func_index) {
WasmCode* wasm_function = native_module->GetCode(func_index);
WasmCode* wasm_function = native_module->code(func_index);
// TODO(clemensh): Get rid of this nullptr check
if (wasm_function == nullptr ||
wasm_function->kind() != WasmCode::kFunction) {
......@@ -197,7 +197,7 @@ bool CodeSpecialization::ApplyToWasmCode(wasm::WasmCode* code,
uint32_t called_func_index = ExtractDirectCallIndex(
patch_direct_calls_helper->decoder,
patch_direct_calls_helper->func_bytes + byte_pos);
const WasmCode* new_code = native_module->GetCode(called_func_index);
const WasmCode* new_code = native_module->code(called_func_index);
it.rinfo()->set_wasm_call_address(new_code->instruction_start(),
icache_flush_mode);
changed = true;
......
......@@ -607,7 +607,7 @@ void RedirectCallsitesInInstance(Isolate* isolate, WasmInstanceObject* instance,
// Redirect all calls in wasm functions.
for (uint32_t i = 0, e = GetNumFunctions(instance); i < e; ++i) {
wasm::WasmCode* code =
instance->compiled_module()->GetNativeModule()->GetCode(i);
instance->compiled_module()->GetNativeModule()->code(i);
RedirectCallsitesInCode(isolate, code, map);
}
// TODO(6668): Find instances that imported our code and also patch those.
......@@ -682,7 +682,7 @@ void WasmDebugInfo::RedirectToInterpreter(Handle<WasmDebugInfo> debug_info,
const wasm::WasmCode* wasm_new_code =
native_module->AddInterpreterWrapper(new_code, func_index);
const wasm::WasmCode* old_code =
native_module->GetCode(static_cast<uint32_t>(func_index));
native_module->code(static_cast<uint32_t>(func_index));
Handle<Foreign> foreign_holder = isolate->factory()->NewForeign(
wasm_new_code->instruction_start(), TENURED);
interpreted_functions->set(func_index, *foreign_holder);
......
......@@ -1438,9 +1438,9 @@ void WasmCompiledModule::Reset(Isolate* isolate,
wasm::CodeSpecialization code_specialization;
for (uint32_t i = native_module->num_imported_functions(),
end = native_module->FunctionCount();
end = native_module->function_count();
i < end; ++i) {
wasm::WasmCode* code = native_module->GetCode(i);
wasm::WasmCode* code = native_module->code(i);
// Skip lazy compile stubs.
if (code == nullptr || code->kind() != wasm::WasmCode::kFunction) continue;
bool changed = code_specialization.ApplyToWasmCode(code, SKIP_ICACHE_FLUSH);
......@@ -1605,11 +1605,11 @@ void WasmCompiledModule::LogWasmCodes(Isolate* isolate) {
wasm::NativeModule* native_module = GetNativeModule();
if (native_module == nullptr) return;
const uint32_t number_of_codes = native_module->FunctionCount();
const uint32_t number_of_codes = native_module->function_count();
if (has_shared()) {
Handle<WasmSharedModuleData> shared_handle(shared(), isolate);
for (uint32_t i = 0; i < number_of_codes; i++) {
wasm::WasmCode* code = native_module->GetCode(i);
wasm::WasmCode* code = native_module->code(i);
if (code == nullptr) continue;
code->LogCode(isolate);
}
......
......@@ -258,16 +258,16 @@ size_t NativeModuleSerializer::MeasureCode(const WasmCode* code) const {
size_t NativeModuleSerializer::Measure() const {
size_t size = kHeaderSize + MeasureCopiedStubs();
uint32_t first_wasm_fn = native_module_->num_imported_functions();
uint32_t total_fns = native_module_->FunctionCount();
uint32_t total_fns = native_module_->function_count();
for (uint32_t i = first_wasm_fn; i < total_fns; ++i) {
size += kCodeHeaderSize;
size += MeasureCode(native_module_->GetCode(i));
size += MeasureCode(native_module_->code(i));
}
return size;
}
void NativeModuleSerializer::WriteHeader(Writer* writer) {
writer->Write(native_module_->FunctionCount());
writer->Write(native_module_->function_count());
writer->Write(native_module_->num_imported_functions());
}
......@@ -382,10 +382,10 @@ bool NativeModuleSerializer::Write(Writer* writer) {
WriteHeader(writer);
WriteCopiedStubs(writer);
uint32_t total_fns = native_module_->FunctionCount();
uint32_t total_fns = native_module_->function_count();
uint32_t first_wasm_fn = native_module_->num_imported_functions();
for (uint32_t i = first_wasm_fn; i < total_fns; ++i) {
const WasmCode* code = native_module_->GetCode(i);
const WasmCode* code = native_module_->code(i);
WriteCode(code, writer);
}
return true;
......@@ -444,7 +444,7 @@ bool NativeModuleDeserializer::Read(Reader* reader) {
if (!ReadHeader(reader)) return false;
if (!ReadStubs(reader)) return false;
uint32_t total_fns = native_module_->FunctionCount();
uint32_t total_fns = native_module_->function_count();
uint32_t first_wasm_fn = native_module_->num_imported_functions();
for (uint32_t i = first_wasm_fn; i < total_fns; ++i) {
if (!ReadCode(i, reader)) return false;
......@@ -455,7 +455,7 @@ bool NativeModuleDeserializer::Read(Reader* reader) {
bool NativeModuleDeserializer::ReadHeader(Reader* reader) {
size_t functions = reader->Read<uint32_t>();
size_t imports = reader->Read<uint32_t>();
return functions == native_module_->FunctionCount() &&
return functions == native_module_->function_count() &&
imports == native_module_->num_imported_functions();
}
......
......@@ -3458,14 +3458,13 @@ TEST(Liftoff_prologue) {
r.builder().instance_object()->compiled_module()->GetNativeModule();
// This test only works if we managed to compile with Liftoff.
if (native_module->GetCode(add_compiler.function_index())->is_liftoff()) {
if (native_module->code(add_compiler.function_index())->is_liftoff()) {
// First run should execute {add_locals}.
CHECK_EQ(10, r.Call(1, 2, 3, 4));
// Update the native_module to contain the "optimized" code ({sub_locals}).
native_module->SetCode(
add_compiler.function_index(),
native_module->GetCode(sub_compiler.function_index()));
native_module->set_code(add_compiler.function_index(),
native_module->code(sub_compiler.function_index()));
// Second run should execute {add_locals}, which should detect that
// the code was updated, and run {sub_locals}.
......
......@@ -117,7 +117,7 @@ uint32_t TestingModuleBuilder::AddFunction(FunctionSig* sig, const char* name) {
Handle<JSFunction> TestingModuleBuilder::WrapCode(uint32_t index) {
// Wrap the code so it can be called as a JS function.
Link();
wasm::WasmCode* code = native_module_->GetCode(index);
wasm::WasmCode* code = native_module_->code(index);
Handle<WasmCompiledModule> compiled_module(
instance_object()->compiled_module(), isolate_);
......@@ -167,7 +167,7 @@ void TestingModuleBuilder::PopulateIndirectFunctionTable() {
for (int j = 0; j < table_size; j++) {
WasmFunction& function = test_module_->functions[table.values[j]];
int sig_id = test_module_->signature_map.Find(function.sig);
auto wasm_code = native_module_->GetCode(function.func_index);
auto wasm_code = native_module_->code(function.func_index);
IndirectFunctionTableEntry(instance, j).set(sig_id, *instance, wasm_code);
}
}
......
......@@ -98,7 +98,7 @@ class TestingModuleBuilder {
byte* AddMemory(uint32_t size);
size_t CodeTableLength() const { return native_module_->FunctionCount(); }
size_t CodeTableLength() const { return native_module_->function_count(); }
template <typename T>
T* AddMemoryElems(uint32_t count) {
......@@ -196,15 +196,19 @@ class TestingModuleBuilder {
return &test_module_->functions[index];
}
WasmInterpreter* interpreter() { return interpreter_; }
bool interpret() { return interpreter_ != nullptr; }
LowerSimd lower_simd() { return lower_simd_; }
Isolate* isolate() { return isolate_; }
Handle<WasmInstanceObject> instance_object() { return instance_object_; }
wasm::WasmCode* GetFunctionCode(uint32_t index) {
return native_module_->GetCode(index);
WasmInterpreter* interpreter() const { return interpreter_; }
bool interpret() const { return interpreter_ != nullptr; }
LowerSimd lower_simd() const { return lower_simd_; }
Isolate* isolate() const { return isolate_; }
Handle<WasmInstanceObject> instance_object() const {
return instance_object_;
}
wasm::WasmCode* GetFunctionCode(uint32_t index) const {
return native_module_->code(index);
}
Address globals_start() const {
return reinterpret_cast<Address>(globals_data_);
}
Address globals_start() { return reinterpret_cast<Address>(globals_data_); }
void Link() {
if (!linked_) {
Handle<WasmCompiledModule> compiled(instance_object()->compiled_module());
......
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