From 86f877de5102e62cf100eb14b4f2acd86ee3f24d Mon Sep 17 00:00:00 2001 From: Michael Starzinger Date: Wed, 17 Apr 2019 15:18:17 +0200 Subject: [PATCH] Simplify encoding of handler table by removing size. R=jgruber@chromium.org BUG=v8:8758 Change-Id: Iba62ca0f9010cd68b47966ad8d04c1a4149efe70 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1571415 Reviewed-by: Jakob Gruber Commit-Queue: Michael Starzinger Cr-Commit-Position: refs/heads/master@{#60918} --- src/builtins/setup-builtins-internal.cc | 4 +--- src/compiler/backend/code-generator.cc | 3 +-- src/frames.cc | 4 ++-- src/handler-table.cc | 27 +++++++++---------------- src/handler-table.h | 6 +++--- src/isolate.cc | 2 +- src/wasm/wasm-code-manager.cc | 25 +++++++++++++++-------- src/wasm/wasm-code-manager.h | 2 ++ 8 files changed, 36 insertions(+), 37 deletions(-) diff --git a/src/builtins/setup-builtins-internal.cc b/src/builtins/setup-builtins-internal.cc index 44f27f24f8..121b8b7c62 100644 --- a/src/builtins/setup-builtins-internal.cc +++ b/src/builtins/setup-builtins-internal.cc @@ -110,9 +110,7 @@ Code BuildWithMacroAssembler(Isolate* isolate, int32_t builtin_index, DCHECK_EQ(Builtins::KindOf(Builtins::kJSConstructEntry), Builtins::ASM); DCHECK_EQ(Builtins::KindOf(Builtins::kJSRunMicrotasksEntry), Builtins::ASM); if (Builtins::IsJSEntryVariant(builtin_index)) { - static constexpr int kJSEntryHandlerCount = 1; - handler_table_offset = - HandlerTable::EmitReturnTableStart(&masm, kJSEntryHandlerCount); + handler_table_offset = HandlerTable::EmitReturnTableStart(&masm); HandlerTable::EmitReturnEntry( &masm, 0, isolate->builtins()->js_entry_handler_offset()); } diff --git a/src/compiler/backend/code-generator.cc b/src/compiler/backend/code-generator.cc index d16933ba89..8a25f2115a 100644 --- a/src/compiler/backend/code-generator.cc +++ b/src/compiler/backend/code-generator.cc @@ -305,8 +305,7 @@ void CodeGenerator::AssembleCode() { // Emit the exception handler table. if (!handlers_.empty()) { - handler_table_offset_ = HandlerTable::EmitReturnTableStart( - tasm(), static_cast(handlers_.size())); + handler_table_offset_ = HandlerTable::EmitReturnTableStart(tasm()); for (size_t i = 0; i < handlers_.size(); ++i) { HandlerTable::EmitReturnEntry(tasm(), handlers_[i].pc_offset, handlers_[i].handler->pos()); diff --git a/src/frames.cc b/src/frames.cc index 0692d215c0..0418acda92 100644 --- a/src/frames.cc +++ b/src/frames.cc @@ -1892,8 +1892,8 @@ int WasmCompiledFrame::LookupExceptionHandlerInTable(int* stack_slots) { DCHECK_NOT_NULL(stack_slots); wasm::WasmCode* code = isolate()->wasm_engine()->code_manager()->LookupCode(pc()); - if (!code->IsAnonymous() && code->handler_table_offset() > 0) { - HandlerTable table(code->instruction_start(), code->handler_table_offset()); + if (!code->IsAnonymous() && code->handler_table_size() > 0) { + HandlerTable table(code->handler_table(), code->handler_table_size()); int pc_offset = static_cast(pc() - code->instruction_start()); *stack_slots = static_cast(code->stack_slots()); return table.LookupReturn(pc_offset); diff --git a/src/handler-table.cc b/src/handler-table.cc index 56c5cefecb..cd6943a232 100644 --- a/src/handler-table.cc +++ b/src/handler-table.cc @@ -14,9 +14,8 @@ namespace v8 { namespace internal { HandlerTable::HandlerTable(Code code) - : HandlerTable(code->InstructionStart(), code->has_handler_table() - ? code->handler_table_offset() - : 0) {} + : HandlerTable(code->InstructionStart() + code->handler_table_offset(), + code->handler_table_size()) {} HandlerTable::HandlerTable(BytecodeArray bytecode_array) : HandlerTable(bytecode_array->handler_table()) {} @@ -29,24 +28,17 @@ HandlerTable::HandlerTable(ByteArray byte_array) #endif raw_encoded_data_( reinterpret_cast
(byte_array->GetDataStartAddress())) { + DCHECK_EQ(0, byte_array->length() % (kRangeEntrySize * sizeof(int32_t))); } -// TODO(jgruber,v8:8758): This constructor should eventually take the handler -// table size in addition to the offset. That way the {HandlerTable} class -// remains independent of how the offset/size is encoded in the various code -// objects. This could even allow us to change the encoding to no longer expect -// the "number of entries" in the beginning. -HandlerTable::HandlerTable(Address instruction_start, - size_t handler_table_offset) - : number_of_entries_(0), +HandlerTable::HandlerTable(Address handler_table, int handler_table_size) + : number_of_entries_(handler_table_size / kReturnEntrySize / + sizeof(int32_t)), #ifdef DEBUG mode_(kReturnAddressBasedEncoding), #endif - raw_encoded_data_(instruction_start + handler_table_offset) { - if (handler_table_offset > 0) { - number_of_entries_ = Memory(raw_encoded_data_); - raw_encoded_data_ += sizeof(int32_t); - } + raw_encoded_data_(handler_table) { + DCHECK_EQ(0, handler_table_size % (kReturnEntrySize * sizeof(int32_t))); } int HandlerTable::GetRangeStart(int index) const { @@ -131,11 +123,10 @@ int HandlerTable::LengthForRange(int entries) { } // static -int HandlerTable::EmitReturnTableStart(Assembler* masm, int entries) { +int HandlerTable::EmitReturnTableStart(Assembler* masm) { masm->DataAlign(sizeof(int32_t)); // Make sure entries are aligned. masm->RecordComment(";;; Exception handler table."); int table_start = masm->pc_offset(); - masm->dd(entries); return table_start; } diff --git a/src/handler-table.h b/src/handler-table.h index b4ea8b6ed7..b4907f32b5 100644 --- a/src/handler-table.h +++ b/src/handler-table.h @@ -49,7 +49,7 @@ class V8_EXPORT_PRIVATE HandlerTable { explicit HandlerTable(Code code); explicit HandlerTable(ByteArray byte_array); explicit HandlerTable(BytecodeArray bytecode_array); - explicit HandlerTable(Address instruction_start, size_t handler_table_offset); + explicit HandlerTable(Address handler_table, int handler_table_size); // Getters for handler table based on ranges. int GetRangeStart(int index) const; @@ -67,7 +67,7 @@ class V8_EXPORT_PRIVATE HandlerTable { static int LengthForRange(int entries); // Emitters for handler table based on return addresses. - static int EmitReturnTableStart(Assembler* masm, int entries); + static int EmitReturnTableStart(Assembler* masm); static void EmitReturnEntry(Assembler* masm, int offset, int handler); // Lookup handler in a table based on ranges. The {pc_offset} is an offset to @@ -106,7 +106,7 @@ class V8_EXPORT_PRIVATE HandlerTable { EncodingMode mode_; #endif - // Direct pointer into the encoded data. This pointer points into object on + // Direct pointer into the encoded data. This pointer points into objects on // the GC heap (either {ByteArray} or {Code}) and hence would become stale // during a collection. Hence we disallow any allocation. Address raw_encoded_data_; diff --git a/src/isolate.cc b/src/isolate.cc index baf7163082..88e794356e 100644 --- a/src/isolate.cc +++ b/src/isolate.cc @@ -1741,7 +1741,7 @@ Object Isolate::UnwindAndFindHandler() { // It is safe to skip Wasm runtime stubs as none of them contain local // exception handlers. CHECK_EQ(wasm::WasmCode::kRuntimeStub, wasm_code->kind()); - CHECK_EQ(0, wasm_code->handler_table_offset()); + CHECK_EQ(0, wasm_code->handler_table_size()); break; } Code code = stub_frame->LookupCode(); diff --git a/src/wasm/wasm-code-manager.cc b/src/wasm/wasm-code-manager.cc index 14f0745c7f..652f6a9f45 100644 --- a/src/wasm/wasm-code-manager.cc +++ b/src/wasm/wasm-code-manager.cc @@ -108,6 +108,15 @@ Address WasmCode::constant_pool() const { return kNullAddress; } +Address WasmCode::handler_table() const { + return instruction_start() + handler_table_offset_; +} + +uint32_t WasmCode::handler_table_size() const { + DCHECK_GE(constant_pool_offset_, handler_table_offset_); + return static_cast(constant_pool_offset_ - handler_table_offset_); +} + Address WasmCode::code_comments() const { return instruction_start() + code_comments_offset_; } @@ -274,7 +283,7 @@ void WasmCode::Disassemble(const char* name, std::ostream& os, if (safepoint_table_offset_ && safepoint_table_offset_ < instruction_size) { instruction_size = safepoint_table_offset_; } - if (handler_table_offset_ && handler_table_offset_ < instruction_size) { + if (handler_table_offset_ < instruction_size) { instruction_size = handler_table_offset_; } DCHECK_LT(0, instruction_size); @@ -284,8 +293,8 @@ void WasmCode::Disassemble(const char* name, std::ostream& os, CodeReference(this), current_pc); os << "\n"; - if (handler_table_offset_ > 0) { - HandlerTable table(instruction_start(), handler_table_offset_); + if (handler_table_size() > 0) { + HandlerTable table(handler_table(), handler_table_size()); os << "Exception Handler Table (size = " << table.NumberOfReturnEntries() << "):\n"; table.HandlerTableReturnPrint(os); @@ -592,8 +601,8 @@ WasmCode* NativeModule::AddAndPublishAnonymousCode(Handle code, // mean 'empty'. const size_t safepoint_table_offset = static_cast( code->has_safepoint_table() ? code->safepoint_table_offset() : 0); - const size_t handler_table_offset = static_cast( - code->has_handler_table() ? code->handler_table_offset() : 0); + const size_t handler_table_offset = + static_cast(code->handler_table_offset()); const size_t constant_pool_offset = static_cast(code->constant_pool_offset()); const size_t code_comments_offset = @@ -681,8 +690,8 @@ std::unique_ptr NativeModule::AddCodeWithCodeSpace( // 'empty'. const size_t safepoint_table_offset = static_cast( desc.safepoint_table_size == 0 ? 0 : desc.safepoint_table_offset); - const size_t handler_table_offset = static_cast( - desc.handler_table_size == 0 ? 0 : desc.handler_table_offset); + const size_t handler_table_offset = + static_cast(desc.handler_table_offset); const size_t constant_pool_offset = static_cast(desc.constant_pool_offset); const size_t code_comments_offset = @@ -868,7 +877,7 @@ WasmCode* NativeModule::CreateEmptyJumpTable(uint32_t jump_table_size) { 0, // stack_slots 0, // tagged_parameter_slots 0, // safepoint_table_offset - 0, // handler_table_offset + jump_table_size, // handler_table_offset jump_table_size, // constant_pool_offset jump_table_size, // code_comments_offset jump_table_size, // unpadded_binary_size diff --git a/src/wasm/wasm-code-manager.h b/src/wasm/wasm-code-manager.h index e689644430..4d9a995f56 100644 --- a/src/wasm/wasm-code-manager.h +++ b/src/wasm/wasm-code-manager.h @@ -114,6 +114,8 @@ class V8_EXPORT_PRIVATE WasmCode final { NativeModule* native_module() const { return native_module_; } ExecutionTier tier() const { return tier_; } Address constant_pool() const; + Address handler_table() const; + uint32_t handler_table_size() const; Address code_comments() const; uint32_t code_comments_size() const; size_t constant_pool_offset() const { return constant_pool_offset_; }