Hide WasmModule.origin field behind readable accessors.

Besides adding accessors get_origin() and set_origin(), it creates easier test
accessors is_wasm() and is_asm_js().

This allows the possibility of caching boolean flags for is_wasm() and
is_asm_js() without having to change any code except for the files containing
the class definition for WasmModule.

BUG= v8:6152
R=bbudge@chromium.org,mtrofin@chromium.org

Review-Url: https://codereview.chromium.org/2771803005
Cr-Commit-Position: refs/heads/master@{#44130}
This commit is contained in:
kschimpf 2017-03-24 18:54:09 -07:00 committed by Commit bot
parent a1f2239e0b
commit 98ed1f9ca9
8 changed files with 47 additions and 40 deletions

View File

@ -2856,7 +2856,7 @@ Node* WasmGraphBuilder::MemBuffer(uint32_t offset) {
Node* WasmGraphBuilder::CurrentMemoryPages() { Node* WasmGraphBuilder::CurrentMemoryPages() {
// CurrentMemoryPages will not be called from asm.js, hence we cannot be in // CurrentMemoryPages will not be called from asm.js, hence we cannot be in
// lazy-compilation mode, hence the instance will be set. // lazy-compilation mode, hence the instance will be set.
DCHECK_EQ(wasm::kWasmOrigin, module_->module->origin); DCHECK_EQ(wasm::kWasmOrigin, module_->module->get_origin());
DCHECK_NOT_NULL(module_); DCHECK_NOT_NULL(module_);
DCHECK_NOT_NULL(module_->instance); DCHECK_NOT_NULL(module_->instance);
@ -3967,7 +3967,7 @@ void WasmCompilationUnit::ExecuteCompilation() {
} }
job_.reset(Pipeline::NewWasmCompilationJob( job_.reset(Pipeline::NewWasmCompilationJob(
&info_, jsgraph_, descriptor, source_positions, &protected_instructions_, &info_, jsgraph_, descriptor, source_positions, &protected_instructions_,
module_env_->module->origin != wasm::kWasmOrigin)); !module_env_->module->is_wasm()));
ok_ = job_->ExecuteJob() == CompilationJob::SUCCEEDED; ok_ = job_->ExecuteJob() == CompilationJob::SUCCEEDED;
// TODO(bradnelson): Improve histogram handling of size_t. // TODO(bradnelson): Improve histogram handling of size_t.
// TODO(ahaas): The counters are not thread-safe at the moment. // TODO(ahaas): The counters are not thread-safe at the moment.

View File

@ -35,13 +35,13 @@ namespace wasm {
#define TRACE(...) #define TRACE(...)
#endif #endif
#define CHECK_PROTOTYPE_OPCODE(flag) \ #define CHECK_PROTOTYPE_OPCODE(flag) \
if (module_ != nullptr && module_->origin == kAsmJsOrigin) { \ if (module_ != nullptr && module_->is_asm_js()) { \
error("Opcode not supported for asmjs modules"); \ error("Opcode not supported for asmjs modules"); \
} \ } \
if (!FLAG_##flag) { \ if (!FLAG_##flag) { \
error("Invalid opcode (enable with --" #flag ")"); \ error("Invalid opcode (enable with --" #flag ")"); \
break; \ break; \
} }
// An SsaEnv environment carries the current local variable renaming // An SsaEnv environment carries the current local variable renaming
@ -1194,7 +1194,7 @@ class WasmFullDecoder : public WasmDecoder {
if (!CheckHasMemory()) break; if (!CheckHasMemory()) break;
MemoryIndexOperand operand(this, pc_); MemoryIndexOperand operand(this, pc_);
DCHECK_NOT_NULL(module_); DCHECK_NOT_NULL(module_);
if (module_->origin != kAsmJsOrigin) { if (module_->is_wasm()) {
Value val = Pop(0, kWasmI32); Value val = Pop(0, kWasmI32);
Push(kWasmI32, BUILD(GrowMemory, val.node)); Push(kWasmI32, BUILD(GrowMemory, val.node));
} else { } else {
@ -1245,7 +1245,7 @@ class WasmFullDecoder : public WasmDecoder {
break; break;
} }
case kAtomicPrefix: { case kAtomicPrefix: {
if (module_ == nullptr || module_->origin != kAsmJsOrigin) { if (module_ == nullptr || !module_->is_asm_js()) {
error("Atomics are allowed only in AsmJs modules"); error("Atomics are allowed only in AsmJs modules");
break; break;
} }
@ -1264,7 +1264,7 @@ class WasmFullDecoder : public WasmDecoder {
} }
default: { default: {
// Deal with special asmjs opcodes. // Deal with special asmjs opcodes.
if (module_ != nullptr && module_->origin == kAsmJsOrigin) { if (module_ != nullptr && module_->is_asm_js()) {
sig = WasmOpcodes::AsmjsSignature(opcode); sig = WasmOpcodes::AsmjsSignature(opcode);
if (sig) { if (sig) {
BuildSimpleOperator(opcode, sig); BuildSimpleOperator(opcode, sig);

View File

@ -259,7 +259,7 @@ class ModuleDecoder : public Decoder {
module->min_mem_pages = 0; module->min_mem_pages = 0;
module->max_mem_pages = 0; module->max_mem_pages = 0;
module->mem_export = false; module->mem_export = false;
module->origin = origin_; module->set_origin(origin_);
const byte* pos = pc_; const byte* pos = pc_;
uint32_t magic_word = consume_u32("wasm magic"); uint32_t magic_word = consume_u32("wasm magic");

View File

@ -290,8 +290,8 @@ Handle<Code> EnsureTableExportLazyDeoptData(
} }
bool compile_lazy(const WasmModule* module) { bool compile_lazy(const WasmModule* module) {
return FLAG_wasm_lazy_compilation || (FLAG_asm_wasm_lazy_compilation && return FLAG_wasm_lazy_compilation ||
module->origin == wasm::kAsmJsOrigin); (FLAG_asm_wasm_lazy_compilation && module->is_asm_js());
} }
// A helper for compiling an entire module. // A helper for compiling an entire module.
@ -514,7 +514,7 @@ class CompilationHelper {
} }
HistogramTimerScope wasm_compile_module_time_scope( HistogramTimerScope wasm_compile_module_time_scope(
module_->origin == ModuleOrigin::kWasmOrigin module_->is_wasm()
? isolate_->counters()->wasm_compile_wasm_module_time() ? isolate_->counters()->wasm_compile_wasm_module_time()
: isolate_->counters()->wasm_compile_asm_module_time()); : isolate_->counters()->wasm_compile_asm_module_time());
@ -1157,7 +1157,7 @@ class InstantiationHelper {
// Record build time into correct bucket, then build instance. // Record build time into correct bucket, then build instance.
HistogramTimerScope wasm_instantiate_module_time_scope( HistogramTimerScope wasm_instantiate_module_time_scope(
module_->origin == ModuleOrigin::kWasmOrigin module_->is_wasm()
? isolate_->counters()->wasm_instantiate_wasm_module_time() ? isolate_->counters()->wasm_instantiate_wasm_module_time()
: isolate_->counters()->wasm_instantiate_asm_module_time()); : isolate_->counters()->wasm_instantiate_asm_module_time());
Factory* factory = isolate_->factory(); Factory* factory = isolate_->factory();
@ -1319,8 +1319,8 @@ class InstantiationHelper {
// Set externally passed ArrayBuffer non neuterable. // Set externally passed ArrayBuffer non neuterable.
memory_->set_is_neuterable(false); memory_->set_is_neuterable(false);
DCHECK_IMPLIES(EnableGuardRegions(), module_->origin == kAsmJsOrigin || DCHECK_IMPLIES(EnableGuardRegions(),
memory_->has_guard_region()); module_->is_asm_js() || memory_->has_guard_region());
} else if (min_mem_pages > 0) { } else if (min_mem_pages > 0) {
memory_ = AllocateMemory(min_mem_pages); memory_ = AllocateMemory(min_mem_pages);
if (memory_.is_null()) return {}; // failed to allocate memory if (memory_.is_null()) return {}; // failed to allocate memory
@ -1706,7 +1706,7 @@ class InstantiationHelper {
Handle<Code> import_wrapper = CompileImportWrapper( Handle<Code> import_wrapper = CompileImportWrapper(
isolate_, index, module_->functions[import.index].sig, isolate_, index, module_->functions[import.index].sig,
Handle<JSReceiver>::cast(value), module_name, import_name, Handle<JSReceiver>::cast(value), module_name, import_name,
module_->origin); module_->get_origin());
if (import_wrapper.is_null()) { if (import_wrapper.is_null()) {
ReportLinkError( ReportLinkError(
"imported function does not match the expected type", index, "imported function does not match the expected type", index,
@ -1940,10 +1940,10 @@ class InstantiationHelper {
} }
Handle<JSObject> exports_object; Handle<JSObject> exports_object;
if (module_->origin == kWasmOrigin) { if (module_->is_wasm()) {
// Create the "exports" object. // Create the "exports" object.
exports_object = isolate_->factory()->NewJSObjectWithNullProto(); exports_object = isolate_->factory()->NewJSObjectWithNullProto();
} else if (module_->origin == kAsmJsOrigin) { } else if (module_->is_asm_js()) {
Handle<JSFunction> object_function = Handle<JSFunction>( Handle<JSFunction> object_function = Handle<JSFunction>(
isolate_->native_context()->object_function(), isolate_); isolate_->native_context()->object_function(), isolate_);
exports_object = isolate_->factory()->NewJSObject(object_function); exports_object = isolate_->factory()->NewJSObject(object_function);
@ -1962,7 +1962,7 @@ class InstantiationHelper {
wasm::AsmWasmBuilder::single_function_name); wasm::AsmWasmBuilder::single_function_name);
PropertyDescriptor desc; PropertyDescriptor desc;
desc.set_writable(module_->origin == kAsmJsOrigin); desc.set_writable(module_->is_asm_js());
desc.set_enumerable(true); desc.set_enumerable(true);
// Count up export indexes. // Count up export indexes.
@ -1992,7 +1992,7 @@ class InstantiationHelper {
isolate_, compiled_module_, exp.name_offset, exp.name_length) isolate_, compiled_module_, exp.name_offset, exp.name_length)
.ToHandleChecked(); .ToHandleChecked();
Handle<JSObject> export_to; Handle<JSObject> export_to;
if (module_->origin == kAsmJsOrigin && exp.kind == kExternalFunction && if (module_->is_asm_js() && exp.kind == kExternalFunction &&
(String::Equals(name, foreign_init_name) || (String::Equals(name, foreign_init_name) ||
String::Equals(name, single_function_name))) { String::Equals(name, single_function_name))) {
export_to = instance; export_to = instance;
@ -2012,7 +2012,7 @@ class InstantiationHelper {
Handle<Code> export_code = Handle<Code> export_code =
code_table->GetValueChecked<Code>(isolate_, func_index); code_table->GetValueChecked<Code>(isolate_, func_index);
MaybeHandle<String> func_name; MaybeHandle<String> func_name;
if (module_->origin == kAsmJsOrigin) { if (module_->is_asm_js()) {
// For modules arising from asm.js, honor the names section. // For modules arising from asm.js, honor the names section.
func_name = WasmCompiledModule::ExtractUtf8StringFromModuleBytes( func_name = WasmCompiledModule::ExtractUtf8StringFromModuleBytes(
isolate_, compiled_module_, function.name_offset, isolate_, compiled_module_, function.name_offset,
@ -2095,7 +2095,7 @@ class InstantiationHelper {
} }
// Skip duplicates for asm.js. // Skip duplicates for asm.js.
if (module_->origin == kAsmJsOrigin) { if (module_->is_asm_js()) {
v8::Maybe<bool> status = JSReceiver::HasOwnProperty(export_to, name); v8::Maybe<bool> status = JSReceiver::HasOwnProperty(export_to, name);
if (status.FromMaybe(false)) { if (status.FromMaybe(false)) {
continue; continue;
@ -2110,7 +2110,7 @@ class InstantiationHelper {
} }
} }
if (module_->origin == kWasmOrigin) { if (module_->is_wasm()) {
v8::Maybe<bool> success = JSReceiver::SetIntegrityLevel( v8::Maybe<bool> success = JSReceiver::SetIntegrityLevel(
exports_object, FROZEN, Object::DONT_THROW); exports_object, FROZEN, Object::DONT_THROW);
DCHECK(success.FromMaybe(false)); DCHECK(success.FromMaybe(false));
@ -2245,7 +2245,7 @@ class InstantiationHelper {
js_to_wasm_cache_.CloneOrCompileJSToWasmWrapper( js_to_wasm_cache_.CloneOrCompileJSToWasmWrapper(
isolate_, module_, wasm_code, func_index); isolate_, module_, wasm_code, func_index);
MaybeHandle<String> func_name; MaybeHandle<String> func_name;
if (module_->origin == kAsmJsOrigin) { if (module_->is_asm_js()) {
// For modules arising from asm.js, honor the names section. // For modules arising from asm.js, honor the names section.
func_name = func_name =
WasmCompiledModule::ExtractUtf8StringFromModuleBytes( WasmCompiledModule::ExtractUtf8StringFromModuleBytes(

View File

@ -154,7 +154,6 @@ struct V8_EXPORT_PRIVATE WasmModule {
// the fact that we index on uint32_t, so we may technically not be // the fact that we index on uint32_t, so we may technically not be
// able to represent some start_function_index -es. // able to represent some start_function_index -es.
int start_function_index = -1; // start function, if any int start_function_index = -1; // start function, if any
ModuleOrigin origin = kWasmOrigin; // origin of the module
std::vector<WasmGlobal> globals; // globals in this module. std::vector<WasmGlobal> globals; // globals in this module.
uint32_t globals_size = 0; // size of globals table. uint32_t globals_size = 0; // size of globals table.
@ -182,6 +181,15 @@ struct V8_EXPORT_PRIVATE WasmModule {
~WasmModule() { ~WasmModule() {
if (owned_zone) delete owned_zone; if (owned_zone) delete owned_zone;
} }
ModuleOrigin get_origin() const { return origin_; }
void set_origin(ModuleOrigin new_value) { origin_ = new_value; }
bool is_wasm() const { return origin_ == kWasmOrigin; }
bool is_asm_js() const { return origin_ == kAsmJsOrigin; }
private:
// TODO(kschimpf) - Encapsulate more fields.
ModuleOrigin origin_ = kWasmOrigin; // origin of the module
}; };
typedef Managed<WasmModule> WasmModuleWrapper; typedef Managed<WasmModule> WasmModuleWrapper;
@ -318,7 +326,7 @@ struct V8_EXPORT_PRIVATE ModuleEnv {
return &module->function_tables[index]; return &module->function_tables[index];
} }
bool asm_js() { return module->origin == kAsmJsOrigin; } bool asm_js() { return module->is_asm_js(); }
// Only used for testing. // Only used for testing.
Handle<Code> GetFunctionCode(uint32_t index) { Handle<Code> GetFunctionCode(uint32_t index) {

View File

@ -221,10 +221,9 @@ bool IsBreakablePosition(Handle<WasmCompiledModule> compiled_module,
Handle<WasmModuleObject> WasmModuleObject::New( Handle<WasmModuleObject> WasmModuleObject::New(
Isolate* isolate, Handle<WasmCompiledModule> compiled_module) { Isolate* isolate, Handle<WasmCompiledModule> compiled_module) {
ModuleOrigin origin = compiled_module->module()->origin; WasmModule* module = compiled_module->module();
Handle<JSObject> module_object; Handle<JSObject> module_object;
if (origin == ModuleOrigin::kWasmOrigin) { if (module->is_wasm()) {
Handle<JSFunction> module_cons( Handle<JSFunction> module_cons(
isolate->native_context()->wasm_module_constructor()); isolate->native_context()->wasm_module_constructor());
module_object = isolate->factory()->NewJSObject(module_cons); module_object = isolate->factory()->NewJSObject(module_cons);
@ -232,7 +231,7 @@ Handle<WasmModuleObject> WasmModuleObject::New(
Object::SetProperty(module_object, module_sym, module_object, STRICT) Object::SetProperty(module_object, module_sym, module_object, STRICT)
.Check(); .Check();
} else { } else {
DCHECK(origin == ModuleOrigin::kAsmJsOrigin); DCHECK(module->is_asm_js());
Handle<Map> map = isolate->factory()->NewMap( Handle<Map> map = isolate->factory()->NewMap(
JS_OBJECT_TYPE, JS_OBJECT_TYPE,
JSObject::kHeaderSize + WasmModuleObject::kFieldCount * kPointerSize); JSObject::kHeaderSize + WasmModuleObject::kFieldCount * kPointerSize);
@ -584,7 +583,7 @@ Handle<WasmSharedModuleData> WasmSharedModuleData::New(
} }
bool WasmSharedModuleData::is_asm_js() { bool WasmSharedModuleData::is_asm_js() {
bool asm_js = module()->origin == wasm::ModuleOrigin::kAsmJsOrigin; bool asm_js = module()->is_asm_js();
DCHECK_EQ(asm_js, script()->IsUserJavaScript()); DCHECK_EQ(asm_js, script()->IsUserJavaScript());
DCHECK_EQ(asm_js, has_asm_js_offset_table()); DCHECK_EQ(asm_js, has_asm_js_offset_table());
return asm_js; return asm_js;

View File

@ -98,7 +98,7 @@ class TestingModule : public ModuleEnv {
~TestingModule() { ~TestingModule() {
if (instance->mem_start) { if (instance->mem_start) {
if (EnableGuardRegions() && module_.origin == kWasmOrigin) { if (EnableGuardRegions() && module_.is_wasm()) {
// See the corresponding code in AddMemory. We use a different // See the corresponding code in AddMemory. We use a different
// allocation path when guard regions are enabled, which means we have // allocation path when guard regions are enabled, which means we have
// to free it differently too. // to free it differently too.
@ -112,14 +112,14 @@ class TestingModule : public ModuleEnv {
if (interpreter_) delete interpreter_; if (interpreter_) delete interpreter_;
} }
void ChangeOriginToAsmjs() { module_.origin = kAsmJsOrigin; } void ChangeOriginToAsmjs() { module_.set_origin(kAsmJsOrigin); }
byte* AddMemory(uint32_t size) { byte* AddMemory(uint32_t size) {
CHECK(!module_.has_memory); CHECK(!module_.has_memory);
CHECK_NULL(instance->mem_start); CHECK_NULL(instance->mem_start);
CHECK_EQ(0, instance->mem_size); CHECK_EQ(0, instance->mem_size);
module_.has_memory = true; module_.has_memory = true;
if (EnableGuardRegions() && module_.origin == kWasmOrigin) { if (EnableGuardRegions() && module_.is_wasm()) {
const size_t alloc_size = const size_t alloc_size =
RoundUp(kWasmMaxHeapOffset, v8::base::OS::CommitPageSize()); RoundUp(kWasmMaxHeapOffset, v8::base::OS::CommitPageSize());
instance->mem_start = reinterpret_cast<byte*>( instance->mem_start = reinterpret_cast<byte*>(
@ -235,7 +235,7 @@ class TestingModule : public ModuleEnv {
uint32_t index = AddFunction(sig, Handle<Code>::null(), nullptr); uint32_t index = AddFunction(sig, Handle<Code>::null(), nullptr);
Handle<Code> code = CompileWasmToJSWrapper( Handle<Code> code = CompileWasmToJSWrapper(
isolate_, jsfunc, sig, index, Handle<String>::null(), isolate_, jsfunc, sig, index, Handle<String>::null(),
Handle<String>::null(), module->origin); Handle<String>::null(), module->get_origin());
instance->function_code[index] = code; instance->function_code[index] = code;
return index; return index;
} }

View File

@ -211,7 +211,7 @@ class TestModuleEnv : public ModuleEnv {
public: public:
explicit TestModuleEnv(ModuleOrigin origin = kWasmOrigin) explicit TestModuleEnv(ModuleOrigin origin = kWasmOrigin)
: ModuleEnv(&mod, nullptr) { : ModuleEnv(&mod, nullptr) {
mod.origin = origin; mod.set_origin(origin);
} }
byte AddGlobal(ValueType type, bool mutability = true) { byte AddGlobal(ValueType type, bool mutability = true) {
mod.globals.push_back({type, mutability, WasmInitExpr(), 0, false, false}); mod.globals.push_back({type, mutability, WasmInitExpr(), 0, false, false});