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

Prepare disassembling unpublished wasm code

This extends the CodeReference class to be able to hold a pointer to
a CodeDesc struct which represents finished but unpublished wasm
code.
For readability, it refactors the implementation of the dispatch for
the different CodeReference kinds.

R=mstarzinger@chromium.org

Bug: v8:8689
Change-Id: Idd031dffaa9d80740c073b6cca0fc3561c5df0c1
Reviewed-on: https://chromium-review.googlesource.com/c/1411885
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Reviewed-by: 's avatarMichael Starzinger <mstarzinger@chromium.org>
Cr-Commit-Position: refs/heads/master@{#58958}
parent 7da7c0bd
......@@ -4,6 +4,7 @@
#include "src/code-reference.h"
#include "src/globals.h"
#include "src/handles-inl.h"
#include "src/objects-inl.h"
#include "src/wasm/wasm-code-manager.h"
......@@ -11,47 +12,91 @@
namespace v8 {
namespace internal {
Address CodeReference::constant_pool() const {
return kind_ == JS ? js_code_->constant_pool() : wasm_code_->constant_pool();
}
Address CodeReference::instruction_start() const {
return kind_ == JS
? js_code_->InstructionStart()
: reinterpret_cast<Address>(wasm_code_->instructions().start());
}
Address CodeReference::instruction_end() const {
return kind_ == JS
? js_code_->InstructionEnd()
: reinterpret_cast<Address>(wasm_code_->instructions().start() +
wasm_code_->instructions().size());
}
int CodeReference::instruction_size() const {
return kind_ == JS ? js_code_->InstructionSize()
: wasm_code_->instructions().length();
}
const byte* CodeReference::relocation_start() const {
return kind_ == JS ? js_code_->relocation_start()
: wasm_code_->reloc_info().start();
}
const byte* CodeReference::relocation_end() const {
return kind_ == JS ? js_code_->relocation_end()
: wasm_code_->reloc_info().start() +
wasm_code_->reloc_info().length();
}
int CodeReference::relocation_size() const {
return kind_ == JS ? js_code_->relocation_size()
: wasm_code_->reloc_info().length();
}
Address CodeReference::code_comments() const {
return kind_ == JS ? js_code_->code_comments() : wasm_code_->code_comments();
}
namespace {
struct JSOps {
Handle<Code> code;
Address constant_pool() const { return code->constant_pool(); }
Address instruction_start() const { return code->InstructionStart(); }
Address instruction_end() const { return code->InstructionEnd(); }
int instruction_size() const { return code->InstructionSize(); }
const byte* relocation_start() const { return code->relocation_start(); }
const byte* relocation_end() const { return code->relocation_end(); }
int relocation_size() const { return code->relocation_size(); }
Address code_comments() const { return code->code_comments(); }
};
struct WasmOps {
const wasm::WasmCode* code;
Address constant_pool() const { return code->constant_pool(); }
Address instruction_start() const {
return reinterpret_cast<Address>(code->instructions().start());
}
Address instruction_end() const {
return reinterpret_cast<Address>(code->instructions().start() +
code->instructions().size());
}
int instruction_size() const { return code->instructions().length(); }
const byte* relocation_start() const { return code->reloc_info().start(); }
const byte* relocation_end() const {
return code->reloc_info().start() + code->reloc_info().length();
}
int relocation_size() const { return code->reloc_info().length(); }
Address code_comments() const { return code->code_comments(); }
};
struct CodeDescOps {
const CodeDesc* code_desc;
Address constant_pool() const {
return instruction_start() + code_desc->constant_pool_offset();
}
Address instruction_start() const {
return reinterpret_cast<Address>(code_desc->buffer);
}
Address instruction_end() const {
return instruction_start() + code_desc->instr_size;
}
int instruction_size() const { return code_desc->instr_size; }
const byte* relocation_start() const {
return code_desc->buffer + code_desc->buffer_size - code_desc->reloc_size;
}
const byte* relocation_end() const {
return code_desc->buffer + code_desc->buffer_size;
}
int relocation_size() const { return code_desc->reloc_size; }
Address code_comments() const {
return instruction_start() + code_desc->code_comments_size;
}
};
} // namespace
#define DISPATCH(ret, method) \
ret CodeReference::method() const { \
DCHECK(!is_null()); \
switch (kind_) { \
case JS: \
return JSOps{js_code_}.method(); \
case WASM: \
return WasmOps{wasm_code_}.method(); \
case CODE_DESC: \
return CodeDescOps{code_desc_}.method(); \
default: \
UNREACHABLE(); \
} \
}
DISPATCH(Address, constant_pool);
DISPATCH(Address, instruction_start);
DISPATCH(Address, instruction_end);
DISPATCH(int, instruction_size);
DISPATCH(const byte*, relocation_start);
DISPATCH(const byte*, relocation_end);
DISPATCH(int, relocation_size);
DISPATCH(Address, code_comments);
#undef DISPATCH
} // namespace internal
} // namespace v8
......@@ -12,6 +12,7 @@ namespace v8 {
namespace internal {
class Code;
struct CodeDesc;
namespace wasm {
class WasmCode;
......@@ -19,9 +20,11 @@ class WasmCode;
class CodeReference {
public:
CodeReference() : kind_(JS), js_code_() {}
CodeReference() : kind_(NONE), null_(nullptr) {}
explicit CodeReference(const wasm::WasmCode* wasm_code)
: kind_(WASM), wasm_code_(wasm_code) {}
explicit CodeReference(const CodeDesc* code_desc)
: kind_(CODE_DESC), code_desc_(code_desc) {}
explicit CodeReference(Handle<Code> js_code) : kind_(JS), js_code_(js_code) {}
Address constant_pool() const;
......@@ -32,9 +35,10 @@ class CodeReference {
const byte* relocation_end() const;
int relocation_size() const;
Address code_comments() const;
bool is_null() const {
return kind_ == JS ? js_code_.is_null() : wasm_code_ == nullptr;
}
bool is_null() const { return kind_ == NONE; }
bool is_js() const { return kind_ == JS; }
bool is_wasm_code() const { return kind_ == WASM; }
Handle<Code> as_js_code() const {
DCHECK_EQ(JS, kind_);
......@@ -47,9 +51,11 @@ class CodeReference {
}
private:
enum { JS, WASM } kind_;
enum { NONE, JS, WASM, CODE_DESC } kind_;
union {
std::nullptr_t null_;
const wasm::WasmCode* wasm_code_;
const CodeDesc* code_desc_;
Handle<Code> js_code_;
};
......
......@@ -240,7 +240,7 @@ static void PrintRelocInfo(StringBuilder* out, Isolate* isolate,
} else {
out->AddFormatted(" %s", Code::Kind2String(kind));
}
} else if (RelocInfo::IsWasmStubCall(rmode) && !isolate) {
} else if (RelocInfo::IsWasmStubCall(rmode) && host.is_wasm_code()) {
// Host is isolate-independent, try wasm native module instead.
wasm::WasmCode* code = host.as_wasm_code()->native_module()->Lookup(
relocinfo->wasm_stub_call_address());
......
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