blob: ae512fd27be2da008afdcee61f94dda5edff43bb [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- MCSymbolWasm.h - ----------------------------------------*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
7//===----------------------------------------------------------------------===//
8#ifndef LLVM_MC_MCSYMBOLWASM_H
9#define LLVM_MC_MCSYMBOLWASM_H
10
11#include "llvm/BinaryFormat/Wasm.h"
12#include "llvm/MC/MCSymbol.h"
13
14namespace llvm {
15
16class MCSymbolWasm : public MCSymbol {
17 wasm::WasmSymbolType Type = wasm::WASM_SYMBOL_TYPE_DATA;
18 bool IsWeak = false;
19 bool IsHidden = false;
20 bool IsComdat = false;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020021 mutable bool IsUsedInInitArray = false;
Andrew Walbran3d2c1972020-04-07 12:24:26 +010022 mutable bool IsUsedInGOT = false;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020023 Optional<StringRef> ImportModule;
24 Optional<StringRef> ImportName;
25 Optional<StringRef> ExportName;
Andrew Scull0372a572018-11-16 15:47:06 +000026 wasm::WasmSignature *Signature = nullptr;
Andrew Walbran16937d02019-10-22 13:54:20 +010027 Optional<wasm::WasmGlobalType> GlobalType;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020028 Optional<wasm::ValType> TableType;
Andrew Walbran16937d02019-10-22 13:54:20 +010029 Optional<wasm::WasmEventType> EventType;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010030
31 /// An expression describing how to calculate the size of a symbol. If a
32 /// symbol has no size this field will be NULL.
33 const MCExpr *SymbolSize = nullptr;
34
35public:
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010036 MCSymbolWasm(const StringMapEntry<bool> *Name, bool isTemporary)
Andrew Walbran16937d02019-10-22 13:54:20 +010037 : MCSymbol(SymbolKindWasm, Name, isTemporary) {}
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010038 static bool classof(const MCSymbol *S) { return S->isWasm(); }
39
40 const MCExpr *getSize() const { return SymbolSize; }
41 void setSize(const MCExpr *SS) { SymbolSize = SS; }
42
43 bool isFunction() const { return Type == wasm::WASM_SYMBOL_TYPE_FUNCTION; }
44 bool isData() const { return Type == wasm::WASM_SYMBOL_TYPE_DATA; }
45 bool isGlobal() const { return Type == wasm::WASM_SYMBOL_TYPE_GLOBAL; }
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020046 bool isTable() const { return Type == wasm::WASM_SYMBOL_TYPE_TABLE; }
Andrew Scullcdfcccc2018-10-05 20:58:37 +010047 bool isSection() const { return Type == wasm::WASM_SYMBOL_TYPE_SECTION; }
Andrew Walbran16937d02019-10-22 13:54:20 +010048 bool isEvent() const { return Type == wasm::WASM_SYMBOL_TYPE_EVENT; }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010049 wasm::WasmSymbolType getType() const { return Type; }
50 void setType(wasm::WasmSymbolType type) { Type = type; }
51
Andrew Walbran16937d02019-10-22 13:54:20 +010052 bool isExported() const {
53 return getFlags() & wasm::WASM_SYMBOL_EXPORTED;
54 }
55 void setExported() const {
56 modifyFlags(wasm::WASM_SYMBOL_EXPORTED, wasm::WASM_SYMBOL_EXPORTED);
57 }
58
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020059 bool isNoStrip() const {
60 return getFlags() & wasm::WASM_SYMBOL_NO_STRIP;
61 }
62 void setNoStrip() const {
63 modifyFlags(wasm::WASM_SYMBOL_NO_STRIP, wasm::WASM_SYMBOL_NO_STRIP);
64 }
65
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010066 bool isWeak() const { return IsWeak; }
67 void setWeak(bool isWeak) { IsWeak = isWeak; }
68
69 bool isHidden() const { return IsHidden; }
70 void setHidden(bool isHidden) { IsHidden = isHidden; }
71
72 bool isComdat() const { return IsComdat; }
73 void setComdat(bool isComdat) { IsComdat = isComdat; }
74
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020075 bool hasImportModule() const { return ImportModule.hasValue(); }
76 StringRef getImportModule() const {
77 if (ImportModule.hasValue())
78 return ImportModule.getValue();
79 // Use a default module name of "env" for now, for compatibility with
80 // existing tools.
81 // TODO(sbc): Find a way to specify a default value in the object format
82 // without picking a hardcoded value like this.
83 return "env";
Andrew Walbran16937d02019-10-22 13:54:20 +010084 }
85 void setImportModule(StringRef Name) { ImportModule = Name; }
86
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020087 bool hasImportName() const { return ImportName.hasValue(); }
88 StringRef getImportName() const {
89 if (ImportName.hasValue())
90 return ImportName.getValue();
91 return getName();
Andrew Walbran16937d02019-10-22 13:54:20 +010092 }
93 void setImportName(StringRef Name) { ImportName = Name; }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010094
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020095 bool hasExportName() const { return ExportName.hasValue(); }
96 StringRef getExportName() const { return ExportName.getValue(); }
97 void setExportName(StringRef Name) { ExportName = Name; }
98
99 bool isFunctionTable() const {
100 return isTable() && hasTableType() &&
101 getTableType() == wasm::ValType::FUNCREF;
102 }
103 void setFunctionTable() {
104 setType(wasm::WASM_SYMBOL_TYPE_TABLE);
105 setTableType(wasm::ValType::FUNCREF);
106 }
107
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100108 void setUsedInGOT() const { IsUsedInGOT = true; }
109 bool isUsedInGOT() const { return IsUsedInGOT; }
110
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200111 void setUsedInInitArray() const { IsUsedInInitArray = true; }
112 bool isUsedInInitArray() const { return IsUsedInInitArray; }
113
Andrew Scull0372a572018-11-16 15:47:06 +0000114 const wasm::WasmSignature *getSignature() const { return Signature; }
115 void setSignature(wasm::WasmSignature *Sig) { Signature = Sig; }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100116
117 const wasm::WasmGlobalType &getGlobalType() const {
Andrew Walbran16937d02019-10-22 13:54:20 +0100118 assert(GlobalType.hasValue());
119 return GlobalType.getValue();
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100120 }
Andrew Walbran16937d02019-10-22 13:54:20 +0100121 void setGlobalType(wasm::WasmGlobalType GT) { GlobalType = GT; }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100122
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200123 bool hasTableType() const { return TableType.hasValue(); }
124 wasm::ValType getTableType() const {
125 assert(hasTableType());
126 return TableType.getValue();
127 }
128 void setTableType(wasm::ValType TT) { TableType = TT; }
129
Andrew Walbran16937d02019-10-22 13:54:20 +0100130 const wasm::WasmEventType &getEventType() const {
131 assert(EventType.hasValue());
132 return EventType.getValue();
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100133 }
Andrew Walbran16937d02019-10-22 13:54:20 +0100134 void setEventType(wasm::WasmEventType ET) { EventType = ET; }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100135};
136
Andrew Scull0372a572018-11-16 15:47:06 +0000137} // end namespace llvm
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100138
139#endif // LLVM_MC_MCSYMBOLWASM_H