1// Copyright 2019 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_SNAPSHOT_EMBEDDED_PLATFORM_EMBEDDED_FILE_WRITER_BASE_H_
6#define V8_SNAPSHOT_EMBEDDED_PLATFORM_EMBEDDED_FILE_WRITER_BASE_H_
7
8#include <cinttypes>
9#include <cstdio>  // For FILE.
10#include <memory>
11
12#include "src/flags/flags.h"  // For ENABLE_CONTROL_FLOW_INTEGRITY_BOOL
13
14namespace v8 {
15namespace internal {
16
17class EmbeddedData;
18
19enum DataDirective {
20  kByte,
21  kLong,
22  kQuad,
23  kOcta,
24};
25
26DataDirective PointerSizeDirective();
27int DataDirectiveSize(DataDirective directive);
28
29enum class EmbeddedTargetOs {
30  kAIX,
31  kChromeOS,
32  kFuchsia,
33  kMac,
34  kWin,
35  kStarboard,
36  kGeneric,  // Everything not covered above falls in here.
37};
38
39enum class EmbeddedTargetArch {
40  kArm,
41  kArm64,
42  kIA32,
43  kX64,
44  kGeneric,  // Everything not covered above falls in here.
45};
46
47// The platform-dependent logic for emitting assembly code for the generated
48// embedded.S file.
49class PlatformEmbeddedFileWriterBase {
50 public:
51  virtual ~PlatformEmbeddedFileWriterBase() = default;
52
53  void SetFile(FILE* fp) { fp_ = fp; }
54  FILE* fp() const { return fp_; }
55
56  virtual void SectionText() = 0;
57  virtual void SectionData() = 0;
58  virtual void SectionRoData() = 0;
59
60  virtual void AlignToCodeAlignment() = 0;
61  virtual void PaddingAfterCode() {}
62  virtual void AlignToDataAlignment() = 0;
63
64  virtual void DeclareUint32(const char* name, uint32_t value) = 0;
65  virtual void DeclarePointerToSymbol(const char* name, const char* target) = 0;
66
67  virtual void DeclareSymbolGlobal(const char* name) = 0;
68  virtual void DeclareLabel(const char* name) = 0;
69
70  virtual void SourceInfo(int fileid, const char* filename, int line) = 0;
71  virtual void DeclareFunctionBegin(const char* name, uint32_t size) = 0;
72  virtual void DeclareFunctionEnd(const char* name) = 0;
73
74  // Returns the number of printed characters.
75  virtual int HexLiteral(uint64_t value);
76
77  virtual void Comment(const char* string) = 0;
78  virtual void Newline() { fprintf(fp_, "\n"); }
79
80  virtual void FilePrologue() = 0;
81  virtual void DeclareExternalFilename(int fileid, const char* filename) = 0;
82  virtual void FileEpilogue() = 0;
83
84  virtual int IndentedDataDirective(DataDirective directive) = 0;
85
86  virtual DataDirective ByteChunkDataDirective() const { return kOcta; }
87  virtual int WriteByteChunk(const uint8_t* data);
88
89  // This awkward interface works around the fact that unwind data emission
90  // is both high-level and platform-dependent. The former implies it should
91  // live in EmbeddedFileWriter, but code there should be platform-independent.
92  //
93  // Emits unwinding data on x64 Windows, and does nothing otherwise.
94  virtual void MaybeEmitUnwindData(const char* unwind_info_symbol,
95                                   const char* embedded_blob_data_symbol,
96                                   const EmbeddedData* blob,
97                                   const void* unwind_infos) {}
98
99 protected:
100  FILE* fp_ = nullptr;
101};
102
103// The factory function. Returns the appropriate platform-specific instance.
104std::unique_ptr<PlatformEmbeddedFileWriterBase> NewPlatformEmbeddedFileWriter(
105    const char* target_arch, const char* target_os);
106
107}  // namespace internal
108}  // namespace v8
109
110#endif  // V8_SNAPSHOT_EMBEDDED_PLATFORM_EMBEDDED_FILE_WRITER_BASE_H_
111