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