MCSymbolWasm.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- MCSymbolWasm.h - ----------------------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_MC_MCSYMBOLWASM_H
  14. #define LLVM_MC_MCSYMBOLWASM_H
  15. #include "llvm/BinaryFormat/Wasm.h"
  16. #include "llvm/MC/MCSymbol.h"
  17. namespace llvm {
  18. class MCSymbolWasm : public MCSymbol {
  19. Optional<wasm::WasmSymbolType> Type;
  20. bool IsWeak = false;
  21. bool IsHidden = false;
  22. bool IsComdat = false;
  23. bool OmitFromLinkingSection = false;
  24. mutable bool IsUsedInInitArray = false;
  25. mutable bool IsUsedInGOT = false;
  26. Optional<StringRef> ImportModule;
  27. Optional<StringRef> ImportName;
  28. Optional<StringRef> ExportName;
  29. wasm::WasmSignature *Signature = nullptr;
  30. Optional<wasm::WasmGlobalType> GlobalType;
  31. Optional<wasm::WasmTableType> TableType;
  32. /// An expression describing how to calculate the size of a symbol. If a
  33. /// symbol has no size this field will be NULL.
  34. const MCExpr *SymbolSize = nullptr;
  35. public:
  36. MCSymbolWasm(const StringMapEntry<bool> *Name, bool isTemporary)
  37. : MCSymbol(SymbolKindWasm, Name, isTemporary) {}
  38. static bool classof(const MCSymbol *S) { return S->isWasm(); }
  39. const MCExpr *getSize() const { return SymbolSize; }
  40. void setSize(const MCExpr *SS) { SymbolSize = SS; }
  41. bool isFunction() const { return Type == wasm::WASM_SYMBOL_TYPE_FUNCTION; }
  42. // Data is the default value if not set.
  43. bool isData() const { return !Type || Type == wasm::WASM_SYMBOL_TYPE_DATA; }
  44. bool isGlobal() const { return Type == wasm::WASM_SYMBOL_TYPE_GLOBAL; }
  45. bool isTable() const { return Type == wasm::WASM_SYMBOL_TYPE_TABLE; }
  46. bool isSection() const { return Type == wasm::WASM_SYMBOL_TYPE_SECTION; }
  47. bool isTag() const { return Type == wasm::WASM_SYMBOL_TYPE_TAG; }
  48. Optional<wasm::WasmSymbolType> getType() const { return Type; }
  49. void setType(wasm::WasmSymbolType type) { Type = type; }
  50. bool isExported() const {
  51. return getFlags() & wasm::WASM_SYMBOL_EXPORTED;
  52. }
  53. void setExported() const {
  54. modifyFlags(wasm::WASM_SYMBOL_EXPORTED, wasm::WASM_SYMBOL_EXPORTED);
  55. }
  56. bool isNoStrip() const {
  57. return getFlags() & wasm::WASM_SYMBOL_NO_STRIP;
  58. }
  59. void setNoStrip() const {
  60. modifyFlags(wasm::WASM_SYMBOL_NO_STRIP, wasm::WASM_SYMBOL_NO_STRIP);
  61. }
  62. bool isTLS() const { return getFlags() & wasm::WASM_SYMBOL_TLS; }
  63. void setTLS() const {
  64. modifyFlags(wasm::WASM_SYMBOL_TLS, wasm::WASM_SYMBOL_TLS);
  65. }
  66. bool isWeak() const { return IsWeak; }
  67. void setWeak(bool isWeak) { IsWeak = isWeak; }
  68. bool isHidden() const { return IsHidden; }
  69. void setHidden(bool isHidden) { IsHidden = isHidden; }
  70. bool isComdat() const { return IsComdat; }
  71. void setComdat(bool isComdat) { IsComdat = isComdat; }
  72. // wasm-ld understands a finite set of symbol types. This flag allows the
  73. // compiler to avoid emitting symbol table entries that would confuse the
  74. // linker, unless the user specifically requests the feature.
  75. bool omitFromLinkingSection() const { return OmitFromLinkingSection; }
  76. void setOmitFromLinkingSection() { OmitFromLinkingSection = true; }
  77. bool hasImportModule() const { return ImportModule.hasValue(); }
  78. StringRef getImportModule() const {
  79. if (ImportModule.hasValue())
  80. return ImportModule.getValue();
  81. // Use a default module name of "env" for now, for compatibility with
  82. // existing tools.
  83. // TODO(sbc): Find a way to specify a default value in the object format
  84. // without picking a hardcoded value like this.
  85. return "env";
  86. }
  87. void setImportModule(StringRef Name) { ImportModule = Name; }
  88. bool hasImportName() const { return ImportName.hasValue(); }
  89. StringRef getImportName() const {
  90. if (ImportName.hasValue())
  91. return ImportName.getValue();
  92. return getName();
  93. }
  94. void setImportName(StringRef Name) { ImportName = Name; }
  95. bool hasExportName() const { return ExportName.hasValue(); }
  96. StringRef getExportName() const { return ExportName.getValue(); }
  97. void setExportName(StringRef Name) { ExportName = Name; }
  98. bool isFunctionTable() const {
  99. return isTable() && hasTableType() &&
  100. getTableType().ElemType == wasm::WASM_TYPE_FUNCREF;
  101. }
  102. void setFunctionTable() {
  103. setType(wasm::WASM_SYMBOL_TYPE_TABLE);
  104. setTableType(wasm::ValType::FUNCREF);
  105. }
  106. void setUsedInGOT() const { IsUsedInGOT = true; }
  107. bool isUsedInGOT() const { return IsUsedInGOT; }
  108. void setUsedInInitArray() const { IsUsedInInitArray = true; }
  109. bool isUsedInInitArray() const { return IsUsedInInitArray; }
  110. const wasm::WasmSignature *getSignature() const { return Signature; }
  111. void setSignature(wasm::WasmSignature *Sig) { Signature = Sig; }
  112. const wasm::WasmGlobalType &getGlobalType() const {
  113. assert(GlobalType.hasValue());
  114. return GlobalType.getValue();
  115. }
  116. void setGlobalType(wasm::WasmGlobalType GT) { GlobalType = GT; }
  117. bool hasTableType() const { return TableType.hasValue(); }
  118. const wasm::WasmTableType &getTableType() const {
  119. assert(hasTableType());
  120. return TableType.getValue();
  121. }
  122. void setTableType(wasm::WasmTableType TT) { TableType = TT; }
  123. void setTableType(wasm::ValType VT) {
  124. // Declare a table with element type VT and no limits (min size 0, no max
  125. // size).
  126. wasm::WasmLimits Limits = {wasm::WASM_LIMITS_FLAG_NONE, 0, 0};
  127. setTableType({uint8_t(VT), Limits});
  128. }
  129. };
  130. } // end namespace llvm
  131. #endif // LLVM_MC_MCSYMBOLWASM_H
  132. #ifdef __GNUC__
  133. #pragma GCC diagnostic pop
  134. #endif