Commit a35e79ee authored by Jakob Gruber's avatar Jakob Gruber Committed by Commit Bot

[mksnapshot] Add abstract base class for platform embedded file writers

This is in preparation for better cross-compile support in mksnapshot.
Specifically, this CL series will introduce runtime switches to select
the target platform for generated embedded.S assembly.

Each platform writer will derive from the abstract base class
PlatformEmbeddedFileWriterBase. Currently, the code remains
functionally unmodified and was just moved to
PlatformEmbeddedFileWriterGeneric. This will be split up in future
CLs.

Bug: v8:9103
Change-Id: Ie7e29bb60ba5a8ff6c0c1edec676943b80a1781b
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1622854
Commit-Queue: Jakob Gruber <jgruber@chromium.org>
Reviewed-by: 's avatarPeter Marshall <petermarshall@chromium.org>
Cr-Commit-Position: refs/heads/master@{#61745}
parent 6908209d
......@@ -3792,6 +3792,10 @@ if (v8_use_snapshot && current_toolchain == v8_snapshot_toolchain) {
sources = [
"src/snapshot/embedded-file-writer.cc",
"src/snapshot/embedded-file-writer.h",
"src/snapshot/embedded/platform-embedded-file-writer-base.cc",
"src/snapshot/embedded/platform-embedded-file-writer-base.h",
"src/snapshot/embedded/platform-embedded-file-writer-generic.cc",
"src/snapshot/embedded/platform-embedded-file-writer-generic.h",
"src/snapshot/mksnapshot.cc",
]
......
This diff is collapsed.
......@@ -11,7 +11,8 @@
#include "src/codegen/source-position-table.h"
#include "src/globals.h"
#include "src/snapshot/snapshot.h"
#include "src/snapshot/embedded-data.h"
#include "src/snapshot/embedded/platform-embedded-file-writer-base.h"
#if defined(V8_OS_WIN_X64)
#include "src/diagnostics/unwinding-info-win64.h"
......@@ -20,71 +21,8 @@
namespace v8 {
namespace internal {
enum DataDirective {
kByte,
kLong,
kQuad,
kOcta,
};
static constexpr char kDefaultEmbeddedVariant[] = "Default";
// The platform-dependent logic for emitting assembly code for the generated
// embedded.S file.
class EmbeddedFileWriter;
class PlatformDependentEmbeddedFileWriter final {
public:
void SetFile(FILE* fp) { fp_ = fp; }
void SectionText();
void SectionData();
void SectionRoData();
void AlignToCodeAlignment();
void AlignToDataAlignment();
void DeclareUint32(const char* name, uint32_t value);
void DeclarePointerToSymbol(const char* name, const char* target);
#if defined(V8_OS_WIN_X64)
void StartPdataSection();
void EndPdataSection();
void StartXdataSection();
void EndXdataSection();
void DeclareExternalFunction(const char* name);
// Emits an RVA (address relative to the module load address) specified as an
// offset from a given symbol.
void DeclareRvaToSymbol(const char* name, uint64_t offset = 0);
#endif
void DeclareLabel(const char* name);
void SourceInfo(int fileid, const char* filename, int line);
void DeclareFunctionBegin(const char* name);
void DeclareFunctionEnd(const char* name);
// Returns the number of printed characters.
int HexLiteral(uint64_t value);
void Comment(const char* string);
void Newline() { fprintf(fp_, "\n"); }
void FilePrologue();
void DeclareExternalFilename(int fileid, const char* filename);
void FileEpilogue();
int IndentedDataDirective(DataDirective directive);
FILE* fp() const { return fp_; }
private:
void DeclareSymbolGlobal(const char* name);
private:
FILE* fp_ = nullptr;
};
// When writing out compiled builtins to a file, we
// Detailed source-code information about builtins can only be obtained by
// registration on the isolate during compilation.
......@@ -173,14 +111,15 @@ class EmbeddedFileWriter : public EmbeddedFileWriterInterface {
FILE* fp = GetFileDescriptorOrDie(embedded_src_path_);
PlatformDependentEmbeddedFileWriter writer;
writer.SetFile(fp);
std::unique_ptr<PlatformEmbeddedFileWriterBase> writer =
NewPlatformEmbeddedFileWriter(target_arch_, target_os_);
writer->SetFile(fp);
WriteFilePrologue(&writer);
WriteExternalFilenames(&writer);
WriteMetadataSection(&writer, blob);
WriteInstructionStreams(&writer, blob);
WriteFileEpilogue(&writer, blob);
WriteFilePrologue(writer.get());
WriteExternalFilenames(writer.get());
WriteMetadataSection(writer.get(), blob);
WriteInstructionStreams(writer.get(), blob);
WriteFileEpilogue(writer.get(), blob);
fclose(fp);
}
......@@ -194,13 +133,13 @@ class EmbeddedFileWriter : public EmbeddedFileWriterInterface {
return fp;
}
void WriteFilePrologue(PlatformDependentEmbeddedFileWriter* w) const {
void WriteFilePrologue(PlatformEmbeddedFileWriterBase* w) const {
w->Comment("Autogenerated file. Do not edit.");
w->Newline();
w->FilePrologue();
}
void WriteExternalFilenames(PlatformDependentEmbeddedFileWriter* w) const {
void WriteExternalFilenames(PlatformEmbeddedFileWriterBase* w) const {
w->Comment(
"Source positions in the embedded blob refer to filenames by id.");
w->Comment("Assembly directives here map the id to a filename.");
......@@ -224,7 +163,7 @@ class EmbeddedFileWriter : public EmbeddedFileWriterInterface {
return std::string{embedded_blob_data_symbol.begin()};
}
void WriteMetadataSection(PlatformDependentEmbeddedFileWriter* w,
void WriteMetadataSection(PlatformEmbeddedFileWriterBase* w,
const i::EmbeddedData* blob) const {
w->Comment("The embedded blob starts here. Metadata comes first, followed");
w->Comment("by builtin instruction streams.");
......@@ -236,7 +175,7 @@ class EmbeddedFileWriter : public EmbeddedFileWriterInterface {
i::EmbeddedData::RawDataOffset());
}
void WriteBuiltin(PlatformDependentEmbeddedFileWriter* w,
void WriteBuiltin(PlatformEmbeddedFileWriterBase* w,
const i::EmbeddedData* blob, const int builtin_id) const {
const bool is_default_variant =
std::strcmp(embedded_variant_, kDefaultEmbeddedVariant) == 0;
......@@ -288,7 +227,7 @@ class EmbeddedFileWriter : public EmbeddedFileWriterInterface {
w->DeclareFunctionEnd(builtin_symbol.begin());
}
void WriteInstructionStreams(PlatformDependentEmbeddedFileWriter* w,
void WriteInstructionStreams(PlatformEmbeddedFileWriterBase* w,
const i::EmbeddedData* blob) const {
for (int i = 0; i < i::Builtins::builtin_count; i++) {
if (!blob->ContainsBuiltin(i)) continue;
......@@ -298,7 +237,7 @@ class EmbeddedFileWriter : public EmbeddedFileWriterInterface {
w->Newline();
}
void WriteFileEpilogue(PlatformDependentEmbeddedFileWriter* w,
void WriteFileEpilogue(PlatformEmbeddedFileWriterBase* w,
const i::EmbeddedData* blob) const {
{
i::EmbeddedVector<char, kTemporaryStringLength> embedded_blob_symbol;
......@@ -336,9 +275,9 @@ class EmbeddedFileWriter : public EmbeddedFileWriterInterface {
#if defined(V8_OS_WIN_X64)
std::string BuiltinsUnwindInfoLabel() const;
void WriteUnwindInfo(PlatformDependentEmbeddedFileWriter* w,
void WriteUnwindInfo(PlatformEmbeddedFileWriterBase* w,
const i::EmbeddedData* blob) const;
void WriteUnwindInfoEntry(PlatformDependentEmbeddedFileWriter* w,
void WriteUnwindInfoEntry(PlatformEmbeddedFileWriterBase* w,
uint64_t rva_start, uint64_t rva_end) const;
#endif
......@@ -356,7 +295,7 @@ class EmbeddedFileWriter : public EmbeddedFileWriterInterface {
static constexpr DataDirective kByteChunkDirective = kQuad;
static constexpr int kByteChunkSize = 8;
static int WriteByteChunk(PlatformDependentEmbeddedFileWriter* w,
static int WriteByteChunk(PlatformEmbeddedFileWriterBase* w,
int current_line_length, const uint8_t* data) {
const uint64_t* quad_ptr = reinterpret_cast<const uint64_t*>(data);
return current_line_length + w->HexLiteral(*quad_ptr);
......@@ -368,7 +307,7 @@ class EmbeddedFileWriter : public EmbeddedFileWriterInterface {
static constexpr DataDirective kByteChunkDirective = kLong;
static constexpr int kByteChunkSize = 4;
static int WriteByteChunk(PlatformDependentEmbeddedFileWriter* w,
static int WriteByteChunk(PlatformEmbeddedFileWriterBase* w,
int current_line_length, const uint8_t* data) {
const uint32_t* long_ptr = reinterpret_cast<const uint32_t*>(data);
return current_line_length + w->HexLiteral(*long_ptr);
......@@ -378,7 +317,7 @@ class EmbeddedFileWriter : public EmbeddedFileWriterInterface {
static constexpr DataDirective kByteChunkDirective = kOcta;
static constexpr int kByteChunkSize = 16;
static int WriteByteChunk(PlatformDependentEmbeddedFileWriter* w,
static int WriteByteChunk(PlatformEmbeddedFileWriterBase* w,
int current_line_length, const uint8_t* data) {
const size_t size = kInt64Size;
......@@ -403,7 +342,7 @@ class EmbeddedFileWriter : public EmbeddedFileWriterInterface {
#endif // defined(V8_COMPILER_IS_MSVC) || defined(V8_OS_AIX)
#undef V8_COMPILER_IS_MSVC
static int WriteDirectiveOrSeparator(PlatformDependentEmbeddedFileWriter* w,
static int WriteDirectiveOrSeparator(PlatformEmbeddedFileWriterBase* w,
int current_line_length,
DataDirective directive) {
int printed_chars;
......@@ -417,7 +356,7 @@ class EmbeddedFileWriter : public EmbeddedFileWriterInterface {
return current_line_length + printed_chars;
}
static int WriteLineEndIfNeeded(PlatformDependentEmbeddedFileWriter* w,
static int WriteLineEndIfNeeded(PlatformEmbeddedFileWriterBase* w,
int current_line_length, int write_size) {
static const int kTextWidth = 100;
// Check if adding ',0xFF...FF\n"' would force a line wrap. This doesn't use
......@@ -432,8 +371,7 @@ class EmbeddedFileWriter : public EmbeddedFileWriterInterface {
}
static void WriteBinaryContentsAsInlineAssembly(
PlatformDependentEmbeddedFileWriter* w, const uint8_t* data,
uint32_t size) {
PlatformEmbeddedFileWriterBase* w, const uint8_t* data, uint32_t size) {
int current_line_length = 0;
uint32_t i = 0;
......
// Copyright 2019 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/snapshot/embedded/platform-embedded-file-writer-base.h"
#include <string>
#include "src/snapshot/embedded/platform-embedded-file-writer-generic.h"
namespace v8 {
namespace internal {
namespace {
EmbeddedTargetArch DefaultEmbeddedTargetArch() {
#if defined(V8_TARGET_ARCH_ARM)
return EmbeddedTargetArch::kArm;
#elif defined(V8_TARGET_ARCH_ARM64)
return EmbeddedTargetArch::kArm64;
#elif defined(V8_TARGET_ARCH_IA32)
return EmbeddedTargetArch::kIA32;
#elif defined(V8_TARGET_ARCH_X64)
return EmbeddedTargetArch::kX64;
#else
return EmbeddedTargetArch::kGeneric;
#endif
}
EmbeddedTargetArch ToEmbeddedTargetArch(const char* s) {
if (s == nullptr) {
return DefaultEmbeddedTargetArch();
}
std::string string(s);
if (string == "arm") {
return EmbeddedTargetArch::kArm;
} else if (string == "arm64") {
return EmbeddedTargetArch::kArm64;
} else if (string == "ia32") {
return EmbeddedTargetArch::kIA32;
} else if (string == "x64") {
return EmbeddedTargetArch::kX64;
} else {
return EmbeddedTargetArch::kGeneric;
}
}
EmbeddedTargetOs DefaultEmbeddedTargetOs() {
#if defined(V8_OS_AIX)
return EmbeddedTargetOs::kAIX;
#elif defined(V8_OS_MACOSX)
return EmbeddedTargetOs::kMac;
#elif defined(V8_OS_WIN)
return EmbeddedTargetOs::kWin;
#else
return EmbeddedTargetOs::kGeneric;
#endif
}
EmbeddedTargetOs ToEmbeddedTargetOs(const char* s) {
if (s == nullptr) {
return DefaultEmbeddedTargetOs();
}
std::string string(s);
if (string == "aix") {
return EmbeddedTargetOs::kAIX;
} else if (string == "chromeos") {
return EmbeddedTargetOs::kChromeOS;
} else if (string == "fuchsia") {
return EmbeddedTargetOs::kFuchsia;
} else if (string == "mac") {
return EmbeddedTargetOs::kMac;
} else if (string == "win") {
return EmbeddedTargetOs::kWin;
} else {
return EmbeddedTargetOs::kGeneric;
}
}
} // namespace
std::unique_ptr<PlatformEmbeddedFileWriterBase> NewPlatformEmbeddedFileWriter(
const char* target_arch, const char* target_os) {
return std::unique_ptr<PlatformEmbeddedFileWriterGeneric>(
new PlatformEmbeddedFileWriterGeneric(ToEmbeddedTargetArch(target_arch),
ToEmbeddedTargetOs(target_os)));
}
} // namespace internal
} // namespace v8
// Copyright 2019 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_SNAPSHOT_EMBEDDED_PLATFORM_EMBEDDED_FILE_WRITER_BASE_H_
#define V8_SNAPSHOT_EMBEDDED_PLATFORM_EMBEDDED_FILE_WRITER_BASE_H_
#include <cinttypes>
#include <cstdio> // For FILE.
#include <memory>
namespace v8 {
namespace internal {
enum DataDirective {
kByte,
kLong,
kQuad,
kOcta,
};
enum class EmbeddedTargetOs {
kAIX,
kChromeOS,
kFuchsia,
kMac,
kWin,
kGeneric, // Everything not covered above falls in here.
};
enum class EmbeddedTargetArch {
kArm,
kArm64,
kIA32,
kX64,
kGeneric, // Everything not covered above falls in here.
};
// The platform-dependent logic for emitting assembly code for the generated
// embedded.S file.
class PlatformEmbeddedFileWriterBase {
public:
virtual ~PlatformEmbeddedFileWriterBase() = default;
void SetFile(FILE* fp) { fp_ = fp; }
FILE* fp() const { return fp_; }
virtual void SectionText() = 0;
virtual void SectionData() = 0;
virtual void SectionRoData() = 0;
virtual void AlignToCodeAlignment() = 0;
virtual void AlignToDataAlignment() = 0;
virtual void DeclareUint32(const char* name, uint32_t value) = 0;
virtual void DeclarePointerToSymbol(const char* name, const char* target) = 0;
virtual void DeclareLabel(const char* name) = 0;
virtual void SourceInfo(int fileid, const char* filename, int line) = 0;
virtual void DeclareFunctionBegin(const char* name) = 0;
virtual void DeclareFunctionEnd(const char* name) = 0;
// Returns the number of printed characters.
virtual int HexLiteral(uint64_t value) = 0;
virtual void Comment(const char* string) = 0;
virtual void Newline() { fprintf(fp_, "\n"); }
virtual void FilePrologue() = 0;
virtual void DeclareExternalFilename(int fileid, const char* filename) = 0;
virtual void FileEpilogue() = 0;
virtual int IndentedDataDirective(DataDirective directive) = 0;
protected:
FILE* fp_ = nullptr;
};
// The factory function. Returns the appropriate platform-specific instance.
std::unique_ptr<PlatformEmbeddedFileWriterBase> NewPlatformEmbeddedFileWriter(
const char* target_arch, const char* target_os);
} // namespace internal
} // namespace v8
#endif // V8_SNAPSHOT_EMBEDDED_PLATFORM_EMBEDDED_FILE_WRITER_BASE_H_
This diff is collapsed.
// Copyright 2019 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_SNAPSHOT_EMBEDDED_PLATFORM_EMBEDDED_FILE_WRITER_GENERIC_H_
#define V8_SNAPSHOT_EMBEDDED_PLATFORM_EMBEDDED_FILE_WRITER_GENERIC_H_
#include <cinttypes>
#include <cstdio> // For FILE.
#include "src/base/macros.h"
#include "src/globals.h" // For V8_OS_WIN_X64
#include "src/snapshot/embedded/platform-embedded-file-writer-base.h"
namespace v8 {
namespace internal {
class PlatformEmbeddedFileWriterGeneric
: public PlatformEmbeddedFileWriterBase {
public:
PlatformEmbeddedFileWriterGeneric(EmbeddedTargetArch target_arch,
EmbeddedTargetOs target_os)
: target_arch_(target_arch), target_os_(target_os) {
// TODO(jgruber): Remove these once platforms have been split off.
USE(target_arch_);
USE(target_os_);
}
void SectionText() override;
void SectionData() override;
void SectionRoData() override;
void AlignToCodeAlignment() override;
void AlignToDataAlignment() override;
void DeclareUint32(const char* name, uint32_t value) override;
void DeclarePointerToSymbol(const char* name, const char* target) override;
void DeclareLabel(const char* name) override;
void SourceInfo(int fileid, const char* filename, int line) override;
void DeclareFunctionBegin(const char* name) override;
void DeclareFunctionEnd(const char* name) override;
int HexLiteral(uint64_t value) override;
void Comment(const char* string) override;
void FilePrologue() override;
void DeclareExternalFilename(int fileid, const char* filename) override;
void FileEpilogue() override;
int IndentedDataDirective(DataDirective directive) override;
#if defined(V8_OS_WIN_X64)
// TODO(jgruber): Move these to the windows-specific writer.
void StartPdataSection();
void EndPdataSection();
void StartXdataSection();
void EndXdataSection();
void DeclareExternalFunction(const char* name);
// Emits an RVA (address relative to the module load address) specified as an
// offset from a given symbol.
void DeclareRvaToSymbol(const char* name, uint64_t offset = 0);
#endif // defined(V8_OS_WIN_X64)
private:
void DeclareSymbolGlobal(const char* name);
private:
const EmbeddedTargetArch target_arch_;
const EmbeddedTargetOs target_os_;
};
} // namespace internal
} // namespace v8
#endif // V8_SNAPSHOT_EMBEDDED_PLATFORM_EMBEDDED_FILE_WRITER_GENERIC_H_
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