Skip to content

Commit a56e574

Browse files
committed
[lld][WebAssembly] Common superclass for input globals/events/tables
This commit regroups commonalities among InputGlobal, InputEvent, and InputTable into the new InputElement. The subclasses are defined inline in the new InputElement.h. NFC. Reviewed By: sbc100 Differential Revision: https://reviews.llvm.org/D94677
1 parent 67464df commit a56e574

15 files changed

+145
-250
lines changed

lld/include/lld/Common/LLVM.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ struct WasmEventType;
4949
struct WasmFunction;
5050
struct WasmGlobal;
5151
struct WasmGlobalType;
52+
struct WasmInitExpr;
5253
struct WasmLimits;
5354
struct WasmRelocation;
5455
struct WasmSignature;
@@ -91,6 +92,7 @@ using llvm::wasm::WasmEventType;
9192
using llvm::wasm::WasmFunction;
9293
using llvm::wasm::WasmGlobal;
9394
using llvm::wasm::WasmGlobalType;
95+
using llvm::wasm::WasmInitExpr;
9496
using llvm::wasm::WasmLimits;
9597
using llvm::wasm::WasmRelocation;
9698
using llvm::wasm::WasmSignature;

lld/wasm/Driver.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
#include "lld/Common/Driver.h"
1010
#include "Config.h"
1111
#include "InputChunks.h"
12-
#include "InputGlobal.h"
13-
#include "InputTable.h"
12+
#include "InputElement.h"
1413
#include "MarkLive.h"
1514
#include "SymbolTable.h"
1615
#include "Writer.h"

lld/wasm/InputElement.h

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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, Event, Table, etc) within an input
22+
// file.
23+
class InputElement {
24+
protected:
25+
InputElement(StringRef name, ObjFile *f)
26+
: file(f), live(!config->gcSections), name(name) {}
27+
28+
public:
29+
StringRef getName() const { return name; }
30+
uint32_t getAssignedIndex() const { return assignedIndex.getValue(); }
31+
bool hasAssignedIndex() const { return assignedIndex.hasValue(); }
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+
45+
class InputGlobal : public InputElement {
46+
public:
47+
InputGlobal(const WasmGlobal &g, ObjFile *f)
48+
: InputElement(g.SymbolName, f), type(g.Type), initExpr(g.InitExpr) {}
49+
50+
const WasmGlobalType &getType() const { return type; }
51+
const WasmInitExpr &getInitExpr() const { return initExpr; }
52+
53+
void setPointerValue(uint64_t value) {
54+
if (config->is64.getValueOr(false)) {
55+
assert(initExpr.Opcode == llvm::wasm::WASM_OPCODE_I64_CONST);
56+
initExpr.Value.Int64 = value;
57+
} else {
58+
assert(initExpr.Opcode == llvm::wasm::WASM_OPCODE_I32_CONST);
59+
initExpr.Value.Int32 = value;
60+
}
61+
}
62+
63+
private:
64+
WasmGlobalType type;
65+
WasmInitExpr initExpr;
66+
};
67+
68+
class InputEvent : public InputElement {
69+
public:
70+
InputEvent(const WasmSignature &s, const WasmEvent &e, ObjFile *f)
71+
: InputElement(e.SymbolName, f), signature(s), type(e.Type) {}
72+
73+
const WasmEventType &getType() const { return type; }
74+
75+
const WasmSignature &signature;
76+
77+
private:
78+
WasmEventType type;
79+
};
80+
81+
class InputTable : public InputElement {
82+
public:
83+
InputTable(const WasmTable &t, ObjFile *f)
84+
: InputElement(t.SymbolName, f), type(t.Type) {}
85+
86+
const WasmTableType &getType() const { return type; }
87+
void setLimits(const WasmLimits &limits) { type.Limits = limits; }
88+
89+
private:
90+
WasmTableType type;
91+
};
92+
93+
} // namespace wasm
94+
95+
inline std::string toString(const wasm::InputElement *d) {
96+
return (toString(d->file) + ":(" + d->getName() + ")").str();
97+
}
98+
99+
} // namespace lld
100+
101+
#endif // LLD_WASM_INPUT_ELEMENT_H

lld/wasm/InputEvent.h

Lines changed: 0 additions & 62 deletions
This file was deleted.

lld/wasm/InputFiles.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
#include "InputFiles.h"
1010
#include "Config.h"
1111
#include "InputChunks.h"
12-
#include "InputEvent.h"
13-
#include "InputGlobal.h"
14-
#include "InputTable.h"
12+
#include "InputElement.h"
1513
#include "OutputSegment.h"
1614
#include "SymbolTable.h"
1715
#include "lld/Common/ErrorHandler.h"

lld/wasm/InputGlobal.h

Lines changed: 0 additions & 55 deletions
This file was deleted.

lld/wasm/InputTable.h

Lines changed: 0 additions & 60 deletions
This file was deleted.

lld/wasm/MapFile.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
//===----------------------------------------------------------------------===//
2020

2121
#include "MapFile.h"
22+
#include "InputElement.h"
2223
#include "InputFiles.h"
23-
#include "InputGlobal.h"
2424
#include "OutputSections.h"
2525
#include "OutputSegment.h"
2626
#include "SymbolTable.h"
@@ -148,7 +148,7 @@ void lld::wasm::writeMapFile(ArrayRef<OutputSection *> outputSections) {
148148
}
149149
} else if (auto *globals = dyn_cast<GlobalSection>(osec)) {
150150
for (auto *global : globals->inputGlobals) {
151-
writeHeader(os, global->getGlobalIndex(), 0, 0);
151+
writeHeader(os, global->getAssignedIndex(), 0, 0);
152152
os.indent(8) << global->getName() << '\n';
153153
}
154154
}

lld/wasm/MarkLive.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@
2121
#include "MarkLive.h"
2222
#include "Config.h"
2323
#include "InputChunks.h"
24-
#include "InputEvent.h"
25-
#include "InputGlobal.h"
26-
#include "InputTable.h"
24+
#include "InputElement.h"
2725
#include "SymbolTable.h"
2826
#include "Symbols.h"
2927

lld/wasm/SymbolTable.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
#include "SymbolTable.h"
1010
#include "Config.h"
1111
#include "InputChunks.h"
12-
#include "InputEvent.h"
13-
#include "InputGlobal.h"
14-
#include "InputTable.h"
12+
#include "InputElement.h"
1513
#include "WriterUtils.h"
1614
#include "lld/Common/ErrorHandler.h"
1715
#include "lld/Common/Memory.h"

0 commit comments

Comments
 (0)