Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:15

0001 //===- MCSymbolWasm.h -  ----------------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 #ifndef LLVM_MC_MCSYMBOLWASM_H
0009 #define LLVM_MC_MCSYMBOLWASM_H
0010 
0011 #include "llvm/BinaryFormat/Wasm.h"
0012 #include "llvm/MC/MCSymbol.h"
0013 #include "llvm/MC/MCSymbolTableEntry.h"
0014 
0015 namespace llvm {
0016 
0017 class MCSymbolWasm : public MCSymbol {
0018   std::optional<wasm::WasmSymbolType> Type;
0019   bool IsWeak = false;
0020   bool IsHidden = false;
0021   bool IsComdat = false;
0022   bool OmitFromLinkingSection = false;
0023   mutable bool IsUsedInInitArray = false;
0024   mutable bool IsUsedInGOT = false;
0025   std::optional<StringRef> ImportModule;
0026   std::optional<StringRef> ImportName;
0027   std::optional<StringRef> ExportName;
0028   wasm::WasmSignature *Signature = nullptr;
0029   std::optional<wasm::WasmGlobalType> GlobalType;
0030   std::optional<wasm::WasmTableType> TableType;
0031 
0032   /// An expression describing how to calculate the size of a symbol. If a
0033   /// symbol has no size this field will be NULL.
0034   const MCExpr *SymbolSize = nullptr;
0035 
0036 public:
0037   MCSymbolWasm(const MCSymbolTableEntry *Name, bool isTemporary)
0038       : MCSymbol(SymbolKindWasm, Name, isTemporary) {}
0039   static bool classof(const MCSymbol *S) { return S->isWasm(); }
0040 
0041   const MCExpr *getSize() const { return SymbolSize; }
0042   void setSize(const MCExpr *SS) { SymbolSize = SS; }
0043 
0044   bool isFunction() const { return Type == wasm::WASM_SYMBOL_TYPE_FUNCTION; }
0045   // Data is the default value if not set.
0046   bool isData() const { return !Type || Type == wasm::WASM_SYMBOL_TYPE_DATA; }
0047   bool isGlobal() const { return Type == wasm::WASM_SYMBOL_TYPE_GLOBAL; }
0048   bool isTable() const { return Type == wasm::WASM_SYMBOL_TYPE_TABLE; }
0049   bool isSection() const { return Type == wasm::WASM_SYMBOL_TYPE_SECTION; }
0050   bool isTag() const { return Type == wasm::WASM_SYMBOL_TYPE_TAG; }
0051 
0052   std::optional<wasm::WasmSymbolType> getType() const { return Type; }
0053 
0054   void setType(wasm::WasmSymbolType type) { Type = type; }
0055 
0056   bool isExported() const {
0057     return getFlags() & wasm::WASM_SYMBOL_EXPORTED;
0058   }
0059   void setExported() const {
0060     modifyFlags(wasm::WASM_SYMBOL_EXPORTED, wasm::WASM_SYMBOL_EXPORTED);
0061   }
0062 
0063   bool isNoStrip() const {
0064     return getFlags() & wasm::WASM_SYMBOL_NO_STRIP;
0065   }
0066   void setNoStrip() const {
0067     modifyFlags(wasm::WASM_SYMBOL_NO_STRIP, wasm::WASM_SYMBOL_NO_STRIP);
0068   }
0069 
0070   bool isTLS() const { return getFlags() & wasm::WASM_SYMBOL_TLS; }
0071   void setTLS() const {
0072     modifyFlags(wasm::WASM_SYMBOL_TLS, wasm::WASM_SYMBOL_TLS);
0073   }
0074 
0075   bool isWeak() const { return IsWeak; }
0076   void setWeak(bool isWeak) { IsWeak = isWeak; }
0077 
0078   bool isHidden() const { return IsHidden; }
0079   void setHidden(bool isHidden) { IsHidden = isHidden; }
0080 
0081   bool isComdat() const { return IsComdat; }
0082   void setComdat(bool isComdat) { IsComdat = isComdat; }
0083 
0084   // wasm-ld understands a finite set of symbol types.  This flag allows the
0085   // compiler to avoid emitting symbol table entries that would confuse the
0086   // linker, unless the user specifically requests the feature.
0087   bool omitFromLinkingSection() const { return OmitFromLinkingSection; }
0088   void setOmitFromLinkingSection() { OmitFromLinkingSection = true; }
0089 
0090   bool hasImportModule() const { return ImportModule.has_value(); }
0091   StringRef getImportModule() const {
0092     if (ImportModule)
0093       return *ImportModule;
0094     // Use a default module name of "env" for now, for compatibility with
0095     // existing tools.
0096     // TODO(sbc): Find a way to specify a default value in the object format
0097     // without picking a hardcoded value like this.
0098     return "env";
0099   }
0100   void setImportModule(StringRef Name) { ImportModule = Name; }
0101 
0102   bool hasImportName() const { return ImportName.has_value(); }
0103   StringRef getImportName() const {
0104     if (ImportName)
0105       return *ImportName;
0106     return getName();
0107   }
0108   void setImportName(StringRef Name) { ImportName = Name; }
0109 
0110   bool hasExportName() const { return ExportName.has_value(); }
0111   StringRef getExportName() const { return *ExportName; }
0112   void setExportName(StringRef Name) { ExportName = Name; }
0113 
0114   bool isFunctionTable() const {
0115     return isTable() && hasTableType() &&
0116            getTableType().ElemType == wasm::ValType::FUNCREF;
0117   }
0118   void setFunctionTable(bool is64) {
0119     setType(wasm::WASM_SYMBOL_TYPE_TABLE);
0120     uint8_t flags =
0121         is64 ? wasm::WASM_LIMITS_FLAG_IS_64 : wasm::WASM_LIMITS_FLAG_NONE;
0122     setTableType(wasm::ValType::FUNCREF, flags);
0123   }
0124 
0125   void setUsedInGOT() const { IsUsedInGOT = true; }
0126   bool isUsedInGOT() const { return IsUsedInGOT; }
0127 
0128   void setUsedInInitArray() const { IsUsedInInitArray = true; }
0129   bool isUsedInInitArray() const { return IsUsedInInitArray; }
0130 
0131   const wasm::WasmSignature *getSignature() const { return Signature; }
0132   void setSignature(wasm::WasmSignature *Sig) { Signature = Sig; }
0133 
0134   const wasm::WasmGlobalType &getGlobalType() const {
0135     assert(GlobalType);
0136     return *GlobalType;
0137   }
0138   void setGlobalType(wasm::WasmGlobalType GT) { GlobalType = GT; }
0139 
0140   bool hasTableType() const { return TableType.has_value(); }
0141   const wasm::WasmTableType &getTableType() const {
0142     assert(hasTableType());
0143     return *TableType;
0144   }
0145   void setTableType(wasm::WasmTableType TT) { TableType = TT; }
0146   void setTableType(wasm::ValType VT,
0147                     uint8_t flags = wasm::WASM_LIMITS_FLAG_NONE) {
0148     // Declare a table with element type VT and no limits (min size 0, no max
0149     // size).
0150     wasm::WasmLimits Limits = {flags, 0, 0};
0151     setTableType({VT, Limits});
0152   }
0153 };
0154 
0155 } // end namespace llvm
0156 
0157 #endif // LLVM_MC_MCSYMBOLWASM_H