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