1 //===- InputElement.h ----------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLD_WASM_INPUT_ELEMENT_H
10 #define LLD_WASM_INPUT_ELEMENT_H
13 #include "InputFiles.h"
14 #include "WriterUtils.h"
15 #include "lld/Common/LLVM.h"
16 #include "llvm/Object/Wasm.h"
22 // Represents a single element (Global, Tag, Table, etc) within an input
26 InputElement(StringRef name
, ObjFile
*f
)
27 : file(f
), live(!config
->gcSections
), name(name
) {}
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
;
43 std::optional
<uint32_t> assignedIndex
;
46 inline WasmInitExpr
intConst(uint64_t value
, bool is64
) {
50 ie
.Inst
.Opcode
= llvm::wasm::WASM_OPCODE_I64_CONST
;
51 ie
.Inst
.Value
.Int64
= static_cast<int64_t>(value
);
53 ie
.Inst
.Opcode
= llvm::wasm::WASM_OPCODE_I32_CONST
;
54 ie
.Inst
.Value
.Int32
= static_cast<int32_t>(value
);
59 class InputGlobal
: public InputElement
{
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));
73 WasmInitExpr initExpr
;
76 class InputTag
: public InputElement
{
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
{
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
; }
100 inline std::string
toString(const wasm::InputElement
*d
) {
101 return (toString(d
->file
) + ":(" + d
->getName() + ")").str();
106 #endif // LLD_WASM_INPUT_ELEMENT_H