11cb0ef41Sopenharmony_ci// Copyright 2019 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#ifndef V8_SNAPSHOT_EMBEDDED_PLATFORM_EMBEDDED_FILE_WRITER_BASE_H_
61cb0ef41Sopenharmony_ci#define V8_SNAPSHOT_EMBEDDED_PLATFORM_EMBEDDED_FILE_WRITER_BASE_H_
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci#include <cinttypes>
91cb0ef41Sopenharmony_ci#include <cstdio>  // For FILE.
101cb0ef41Sopenharmony_ci#include <memory>
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci#include "src/flags/flags.h"  // For ENABLE_CONTROL_FLOW_INTEGRITY_BOOL
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_cinamespace v8 {
151cb0ef41Sopenharmony_cinamespace internal {
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ciclass EmbeddedData;
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_cienum DataDirective {
201cb0ef41Sopenharmony_ci  kByte,
211cb0ef41Sopenharmony_ci  kLong,
221cb0ef41Sopenharmony_ci  kQuad,
231cb0ef41Sopenharmony_ci  kOcta,
241cb0ef41Sopenharmony_ci};
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ciDataDirective PointerSizeDirective();
271cb0ef41Sopenharmony_ciint DataDirectiveSize(DataDirective directive);
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_cienum class EmbeddedTargetOs {
301cb0ef41Sopenharmony_ci  kAIX,
311cb0ef41Sopenharmony_ci  kChromeOS,
321cb0ef41Sopenharmony_ci  kFuchsia,
331cb0ef41Sopenharmony_ci  kMac,
341cb0ef41Sopenharmony_ci  kWin,
351cb0ef41Sopenharmony_ci  kStarboard,
361cb0ef41Sopenharmony_ci  kGeneric,  // Everything not covered above falls in here.
371cb0ef41Sopenharmony_ci};
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_cienum class EmbeddedTargetArch {
401cb0ef41Sopenharmony_ci  kArm,
411cb0ef41Sopenharmony_ci  kArm64,
421cb0ef41Sopenharmony_ci  kIA32,
431cb0ef41Sopenharmony_ci  kX64,
441cb0ef41Sopenharmony_ci  kGeneric,  // Everything not covered above falls in here.
451cb0ef41Sopenharmony_ci};
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci// The platform-dependent logic for emitting assembly code for the generated
481cb0ef41Sopenharmony_ci// embedded.S file.
491cb0ef41Sopenharmony_ciclass PlatformEmbeddedFileWriterBase {
501cb0ef41Sopenharmony_ci public:
511cb0ef41Sopenharmony_ci  virtual ~PlatformEmbeddedFileWriterBase() = default;
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci  void SetFile(FILE* fp) { fp_ = fp; }
541cb0ef41Sopenharmony_ci  FILE* fp() const { return fp_; }
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci  virtual void SectionText() = 0;
571cb0ef41Sopenharmony_ci  virtual void SectionData() = 0;
581cb0ef41Sopenharmony_ci  virtual void SectionRoData() = 0;
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci  virtual void AlignToCodeAlignment() = 0;
611cb0ef41Sopenharmony_ci  virtual void PaddingAfterCode() {}
621cb0ef41Sopenharmony_ci  virtual void AlignToDataAlignment() = 0;
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci  virtual void DeclareUint32(const char* name, uint32_t value) = 0;
651cb0ef41Sopenharmony_ci  virtual void DeclarePointerToSymbol(const char* name, const char* target) = 0;
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci  virtual void DeclareSymbolGlobal(const char* name) = 0;
681cb0ef41Sopenharmony_ci  virtual void DeclareLabel(const char* name) = 0;
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci  virtual void SourceInfo(int fileid, const char* filename, int line) = 0;
711cb0ef41Sopenharmony_ci  virtual void DeclareFunctionBegin(const char* name, uint32_t size) = 0;
721cb0ef41Sopenharmony_ci  virtual void DeclareFunctionEnd(const char* name) = 0;
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ci  // Returns the number of printed characters.
751cb0ef41Sopenharmony_ci  virtual int HexLiteral(uint64_t value);
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ci  virtual void Comment(const char* string) = 0;
781cb0ef41Sopenharmony_ci  virtual void Newline() { fprintf(fp_, "\n"); }
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci  virtual void FilePrologue() = 0;
811cb0ef41Sopenharmony_ci  virtual void DeclareExternalFilename(int fileid, const char* filename) = 0;
821cb0ef41Sopenharmony_ci  virtual void FileEpilogue() = 0;
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ci  virtual int IndentedDataDirective(DataDirective directive) = 0;
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ci  virtual DataDirective ByteChunkDataDirective() const { return kOcta; }
871cb0ef41Sopenharmony_ci  virtual int WriteByteChunk(const uint8_t* data);
881cb0ef41Sopenharmony_ci
891cb0ef41Sopenharmony_ci  // This awkward interface works around the fact that unwind data emission
901cb0ef41Sopenharmony_ci  // is both high-level and platform-dependent. The former implies it should
911cb0ef41Sopenharmony_ci  // live in EmbeddedFileWriter, but code there should be platform-independent.
921cb0ef41Sopenharmony_ci  //
931cb0ef41Sopenharmony_ci  // Emits unwinding data on x64 Windows, and does nothing otherwise.
941cb0ef41Sopenharmony_ci  virtual void MaybeEmitUnwindData(const char* unwind_info_symbol,
951cb0ef41Sopenharmony_ci                                   const char* embedded_blob_data_symbol,
961cb0ef41Sopenharmony_ci                                   const EmbeddedData* blob,
971cb0ef41Sopenharmony_ci                                   const void* unwind_infos) {}
981cb0ef41Sopenharmony_ci
991cb0ef41Sopenharmony_ci protected:
1001cb0ef41Sopenharmony_ci  FILE* fp_ = nullptr;
1011cb0ef41Sopenharmony_ci};
1021cb0ef41Sopenharmony_ci
1031cb0ef41Sopenharmony_ci// The factory function. Returns the appropriate platform-specific instance.
1041cb0ef41Sopenharmony_cistd::unique_ptr<PlatformEmbeddedFileWriterBase> NewPlatformEmbeddedFileWriter(
1051cb0ef41Sopenharmony_ci    const char* target_arch, const char* target_os);
1061cb0ef41Sopenharmony_ci
1071cb0ef41Sopenharmony_ci}  // namespace internal
1081cb0ef41Sopenharmony_ci}  // namespace v8
1091cb0ef41Sopenharmony_ci
1101cb0ef41Sopenharmony_ci#endif  // V8_SNAPSHOT_EMBEDDED_PLATFORM_EMBEDDED_FILE_WRITER_BASE_H_
111