[MemProf] Templatize CallStackRadixTreeBuilder (NFC) (#117014)
[llvm-project.git] / lld / wasm / InputElement.h
blob10dc2a3e4a826a977ee8831b7d542a35ca996a99
1 //===- InputElement.h ----------------------------------------*- C++ -*-===//
2 //
3 // 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
6 //
7 //===----------------------------------------------------------------------===//
9 #ifndef LLD_WASM_INPUT_ELEMENT_H
10 #define LLD_WASM_INPUT_ELEMENT_H
12 #include "Config.h"
13 #include "InputFiles.h"
14 #include "WriterUtils.h"
15 #include "lld/Common/LLVM.h"
16 #include "llvm/Object/Wasm.h"
17 #include <optional>
19 namespace lld {
20 namespace wasm {
22 // Represents a single element (Global, Tag, Table, etc) within an input
23 // file.
24 class InputElement {
25 protected:
26 InputElement(StringRef name, ObjFile *f)
27 : file(f), live(!config->gcSections), name(name) {}
29 public:
30 StringRef getName() const { return name; }
31 uint32_t getAssignedIndex() const { return *assignedIndex; }
32 bool hasAssignedIndex() const { return assignedIndex.has_value(); }
33 void assignIndex(uint32_t index) {
34 assert(!hasAssignedIndex());
35 assignedIndex = index;
38 ObjFile *file;
39 bool live = false;
41 protected:
42 StringRef name;
43 std::optional<uint32_t> assignedIndex;
46 inline WasmInitExpr intConst(uint64_t value, bool is64) {
47 WasmInitExpr ie;
48 ie.Extended = false;
49 if (is64) {
50 ie.Inst.Opcode = llvm::wasm::WASM_OPCODE_I64_CONST;
51 ie.Inst.Value.Int64 = static_cast<int64_t>(value);
52 } else {
53 ie.Inst.Opcode = llvm::wasm::WASM_OPCODE_I32_CONST;
54 ie.Inst.Value.Int32 = static_cast<int32_t>(value);
56 return ie;
59 class InputGlobal : public InputElement {
60 public:
61 InputGlobal(const WasmGlobal &g, ObjFile *f)
62 : InputElement(g.SymbolName, f), type(g.Type), initExpr(g.InitExpr) {}
64 const WasmGlobalType &getType() const { return type; }
65 const WasmInitExpr &getInitExpr() const { return initExpr; }
67 void setPointerValue(uint64_t value) {
68 initExpr = intConst(value, config->is64.value_or(false));
71 private:
72 WasmGlobalType type;
73 WasmInitExpr initExpr;
76 class InputTag : public InputElement {
77 public:
78 InputTag(const WasmSignature &s, const WasmTag &t, ObjFile *f)
79 : InputElement(t.SymbolName, f), signature(s) {
80 assert(s.Kind == WasmSignature::Tag);
83 const WasmSignature &signature;
86 class InputTable : public InputElement {
87 public:
88 InputTable(const WasmTable &t, ObjFile *f)
89 : InputElement(t.SymbolName, f), type(t.Type) {}
91 const WasmTableType &getType() const { return type; }
92 void setLimits(const WasmLimits &limits) { type.Limits = limits; }
94 private:
95 WasmTableType type;
98 } // namespace wasm
100 inline std::string toString(const wasm::InputElement *d) {
101 return (toString(d->file) + ":(" + d->getName() + ")").str();
104 } // namespace lld
106 #endif // LLD_WASM_INPUT_ELEMENT_H