Skip to content

Commit 944ae89

Browse files
committed
Move Type-related functions into Type class (NFC)
Several type-related functions currently exist outside of `Type` class and thus in the `wasm`, effectively global, namespace. This moves these functions into `Type` class, making them either member functions or static functions. Also this renames `getSize` to `getByteSize` to make it not to be confused with `size`, which returns the number of types in multiple types. This also reorders the order of functions in `wasm-type.cpp` to match that of `wasm-type.h`.
1 parent f2ba91b commit 944ae89

15 files changed

+140
-127
lines changed

src/asm2wasm.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2008,7 +2008,7 @@ Function* Asm2WasmBuilder::processFunction(Ref ast) {
20082008
ret->offset = 0;
20092009
ret->align = view.bytes;
20102010
ret->ptr = processUnshifted(ast[2], view.bytes);
2011-
ret->type = getType(view.bytes, !view.integer);
2011+
ret->type = Type::getType(view.bytes, !view.integer);
20122012
return ret;
20132013
} else if (what == UNARY_PREFIX) {
20142014
if (ast[1] == PLUS) {

src/ir/ExpressionAnalyzer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ template<typename T> void visitImmediates(Expression* curr, T& visitor) {
151151
}
152152
void visitLoad(Load* curr) {
153153
visitor.visitInt(curr->bytes);
154-
if (curr->type != unreachable && curr->bytes < getTypeSize(curr->type)) {
154+
if (curr->type != unreachable && curr->bytes < curr->type.getByteSize()) {
155155
visitor.visitInt(curr->signed_);
156156
}
157157
visitor.visitAddress(curr->offset);

src/ir/load-utils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ inline bool isSignRelevant(Load* load) {
3131
if (load->type == unreachable) {
3232
return false;
3333
}
34-
return !type.isFloat() && load->bytes < getTypeSize(type);
34+
return !type.isFloat() && load->bytes < type.getByteSize();
3535
}
3636

3737
// check if a load can be signed (which some opts want to do)

src/passes/Asyncify.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,7 +1090,7 @@ struct AsyncifyLocals : public WalkerPass<PostWalker<AsyncifyLocals>> {
10901090
Index total = 0;
10911091
for (Index i = 0; i < numPreservableLocals; i++) {
10921092
auto type = func->getLocalType(i);
1093-
auto size = getTypeSize(type);
1093+
auto size = type.getByteSize();
10941094
total += size;
10951095
}
10961096
auto* block = builder->makeBlock();
@@ -1101,7 +1101,7 @@ struct AsyncifyLocals : public WalkerPass<PostWalker<AsyncifyLocals>> {
11011101
Index offset = 0;
11021102
for (Index i = 0; i < numPreservableLocals; i++) {
11031103
auto type = func->getLocalType(i);
1104-
auto size = getTypeSize(type);
1104+
auto size = type.getByteSize();
11051105
assert(size % STACK_ALIGN == 0);
11061106
// TODO: higher alignment?
11071107
block->list.push_back(builder->makeLocalSet(
@@ -1130,7 +1130,7 @@ struct AsyncifyLocals : public WalkerPass<PostWalker<AsyncifyLocals>> {
11301130
Index offset = 0;
11311131
for (Index i = 0; i < numPreservableLocals; i++) {
11321132
auto type = func->getLocalType(i);
1133-
auto size = getTypeSize(type);
1133+
auto size = type.getByteSize();
11341134
assert(size % STACK_ALIGN == 0);
11351135
// TODO: higher alignment?
11361136
block->list.push_back(

src/passes/AvoidReinterprets.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ static bool canReplaceWithReinterpret(Load* load) {
3232
// a reinterpret of the same address. A partial load would see
3333
// more bytes and possibly invalid data, and an unreachable
3434
// pointer is just not interesting to handle.
35-
return load->type != unreachable && load->bytes == getTypeSize(load->type);
35+
return load->type != unreachable && load->bytes == load->type.getByteSize();
3636
}
3737

3838
static Load* getSingleLoad(LocalGraph* localGraph, LocalGet* get) {
@@ -116,7 +116,7 @@ struct AvoidReinterprets : public WalkerPass<PostWalker<AvoidReinterprets>> {
116116
// We should use another load here, to avoid reinterprets.
117117
info.ptrLocal = Builder::addVar(func, i32);
118118
info.reinterpretedLocal =
119-
Builder::addVar(func, reinterpretType(load->type));
119+
Builder::addVar(func, load->type.reinterpretType());
120120
} else {
121121
unoptimizables.insert(load);
122122
}
@@ -151,7 +151,7 @@ struct AvoidReinterprets : public WalkerPass<PostWalker<AvoidReinterprets>> {
151151
// A reinterpret of a get of a load - use the new local.
152152
Builder builder(*module);
153153
replaceCurrent(builder.makeLocalGet(
154-
info.reinterpretedLocal, reinterpretType(load->type)));
154+
info.reinterpretedLocal, load->type.reinterpretType()));
155155
}
156156
}
157157
}
@@ -185,7 +185,7 @@ struct AvoidReinterprets : public WalkerPass<PostWalker<AvoidReinterprets>> {
185185
load->offset,
186186
load->align,
187187
ptr,
188-
reinterpretType(load->type));
188+
load->type.reinterpretType());
189189
}
190190
} finalOptimizer(infos, localGraph, getModule());
191191

src/passes/ConstHoisting.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ struct ConstHoisting : public WalkerPass<PostWalker<ConstHoisting>> {
8888
}
8989
case f32:
9090
case f64: {
91-
size = getTypeSize(value.type);
91+
size = value.type.getByteSize();
9292
break;
9393
}
9494
case v128: // v128 not implemented yet

src/passes/Print.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ struct PrintExpressionContents
186186
o << ".atomic";
187187
}
188188
o << ".load";
189-
if (curr->type != unreachable && curr->bytes < getTypeSize(curr->type)) {
189+
if (curr->type != unreachable && curr->bytes < curr->type.getByteSize()) {
190190
if (curr->bytes == 1) {
191191
o << '8';
192192
} else if (curr->bytes == 2) {
@@ -233,7 +233,7 @@ struct PrintExpressionContents
233233
}
234234
static void printRMWSize(std::ostream& o, Type type, uint8_t bytes) {
235235
prepareColor(o) << forceConcrete(type) << ".atomic.rmw";
236-
if (type != unreachable && bytes != getTypeSize(type)) {
236+
if (type != unreachable && bytes != type.getByteSize()) {
237237
if (bytes == 1) {
238238
o << '8';
239239
} else if (bytes == 2) {
@@ -269,7 +269,7 @@ struct PrintExpressionContents
269269
o << "xchg";
270270
break;
271271
}
272-
if (curr->type != unreachable && curr->bytes != getTypeSize(curr->type)) {
272+
if (curr->type != unreachable && curr->bytes != curr->type.getByteSize()) {
273273
o << "_u";
274274
}
275275
restoreNormalColor(o);
@@ -281,7 +281,7 @@ struct PrintExpressionContents
281281
prepareColor(o);
282282
printRMWSize(o, curr->type, curr->bytes);
283283
o << "cmpxchg";
284-
if (curr->type != unreachable && curr->bytes != getTypeSize(curr->type)) {
284+
if (curr->type != unreachable && curr->bytes != curr->type.getByteSize()) {
285285
o << "_u";
286286
}
287287
restoreNormalColor(o);

src/passes/SafeHeap.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ struct SafeHeap : public Pass {
176176
load.type = type;
177177
for (Index bytes : {1, 2, 4, 8, 16}) {
178178
load.bytes = bytes;
179-
if (bytes > getTypeSize(type) || (type == f32 && bytes != 4) ||
179+
if (bytes > type.getByteSize() || (type == f32 && bytes != 4) ||
180180
(type == f64 && bytes != 8) || (type == v128 && bytes != 16)) {
181181
continue;
182182
}
@@ -212,7 +212,7 @@ struct SafeHeap : public Pass {
212212
store.type = none;
213213
for (Index bytes : {1, 2, 4, 8, 16}) {
214214
store.bytes = bytes;
215-
if (bytes > getTypeSize(valueType) ||
215+
if (bytes > valueType.getByteSize() ||
216216
(valueType == f32 && bytes != 4) ||
217217
(valueType == f64 && bytes != 8) ||
218218
(valueType == v128 && bytes != 16)) {

src/passes/SpillPointers.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ struct SpillPointers
7878
PointerMap pointerMap;
7979
for (Index i = 0; i < func->getNumLocals(); i++) {
8080
if (func->getLocalType(i) == ABI::PointerType) {
81-
auto offset = pointerMap.size() * getTypeSize(ABI::PointerType);
81+
auto offset = pointerMap.size() * ABI::PointerType.getByteSize();
8282
pointerMap[i] = offset;
8383
}
8484
}
@@ -140,7 +140,7 @@ struct SpillPointers
140140
// get the stack space, and set the local to it
141141
ABI::getStackSpace(spillLocal,
142142
func,
143-
getTypeSize(ABI::PointerType) * pointerMap.size(),
143+
ABI::PointerType.getByteSize() * pointerMap.size(),
144144
*getModule());
145145
}
146146
}
@@ -184,9 +184,9 @@ struct SpillPointers
184184
// add the spills
185185
for (auto index : toSpill) {
186186
block->list.push_back(
187-
builder.makeStore(getTypeSize(ABI::PointerType),
187+
builder.makeStore(ABI::PointerType.getByteSize(),
188188
pointerMap[index],
189-
getTypeSize(ABI::PointerType),
189+
ABI::PointerType.getByteSize(),
190190
builder.makeLocalGet(spillLocal, ABI::PointerType),
191191
builder.makeLocalGet(index, ABI::PointerType),
192192
ABI::PointerType));

src/wasm-interpreter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1719,7 +1719,7 @@ template<typename GlobalManager, typename SubType> class ModuleInstanceBase {
17191719
if (timeout.breaking()) {
17201720
return timeout;
17211721
}
1722-
auto bytes = getTypeSize(curr->expectedType);
1722+
auto bytes = curr->expectedType.getByteSize();
17231723
auto addr = instance.getFinalAddress(ptr.value, bytes);
17241724
auto loaded = instance.doAtomicLoad(addr, bytes, curr->expectedType);
17251725
NOTE_EVAL1(loaded);

0 commit comments

Comments
 (0)