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 //===----------------------------------------------------------------------===//
8
9 #ifndef LLD_WASM_INPUT_ELEMENT_H
10 #define LLD_WASM_INPUT_ELEMENT_H
11
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>
18
19 namespace lld {
20 namespace wasm {
21
22 // Represents a single element (Global, Tag, Table, etc) within an input
23 // file.
24 class InputElement {
25 protected:
InputElement(StringRef name,ObjFile * f)26 InputElement(StringRef name, ObjFile *f)
27 : file(f), live(!config->gcSections), name(name) {}
28
29 public:
getName()30 StringRef getName() const { return name; }
getAssignedIndex()31 uint32_t getAssignedIndex() const { return *assignedIndex; }
hasAssignedIndex()32 bool hasAssignedIndex() const { return assignedIndex.has_value(); }
assignIndex(uint32_t index)33 void assignIndex(uint32_t index) {
34 assert(!hasAssignedIndex());
35 assignedIndex = index;
36 }
37
38 ObjFile *file;
39 bool live = false;
40
41 protected:
42 StringRef name;
43 std::optional<uint32_t> assignedIndex;
44 };
45
intConst(uint64_t value,bool is64)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);
55 }
56 return ie;
57 }
58
59 class InputGlobal : public InputElement {
60 public:
InputGlobal(const WasmGlobal & g,ObjFile * f)61 InputGlobal(const WasmGlobal &g, ObjFile *f)
62 : InputElement(g.SymbolName, f), type(g.Type), initExpr(g.InitExpr) {}
63
getType()64 const WasmGlobalType &getType() const { return type; }
getInitExpr()65 const WasmInitExpr &getInitExpr() const { return initExpr; }
66
setPointerValue(uint64_t value)67 void setPointerValue(uint64_t value) {
68 initExpr = intConst(value, config->is64.value_or(false));
69 }
70
71 private:
72 WasmGlobalType type;
73 WasmInitExpr initExpr;
74 };
75
76 class InputTag : public InputElement {
77 public:
InputTag(const WasmSignature & s,const WasmTag & t,ObjFile * f)78 InputTag(const WasmSignature &s, const WasmTag &t, ObjFile *f)
79 : InputElement(t.SymbolName, f), signature(s) {}
80
81 const WasmSignature &signature;
82 };
83
84 class InputTable : public InputElement {
85 public:
InputTable(const WasmTable & t,ObjFile * f)86 InputTable(const WasmTable &t, ObjFile *f)
87 : InputElement(t.SymbolName, f), type(t.Type) {}
88
getType()89 const WasmTableType &getType() const { return type; }
setLimits(const WasmLimits & limits)90 void setLimits(const WasmLimits &limits) { type.Limits = limits; }
91
92 private:
93 WasmTableType type;
94 };
95
96 } // namespace wasm
97
toString(const wasm::InputElement * d)98 inline std::string toString(const wasm::InputElement *d) {
99 return (toString(d->file) + ":(" + d->getName() + ")").str();
100 }
101
102 } // namespace lld
103
104 #endif // LLD_WASM_INPUT_ELEMENT_H
105