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

source code of lld/wasm/InputElement.h