Commit 7186b601 authored by Clemens Backes's avatar Clemens Backes Committed by Commit Bot

[wasm] Compute memory size instead of hard-coding it

With C++14, we can make {ElementSizeLog2Of} constexpr and use it to
compute the loaded or stored memory size instead of duplicating that
information.

The code does not get shorter this way, but more robust.

R=ahaas@chromium.org

Bug: v8:9810
Change-Id: Idb7e861f833798e181694cda0db21ef57804d3a2
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1914215Reviewed-by: 's avatarAndreas Haas <ahaas@chromium.org>
Commit-Queue: Clemens Backes <clemensb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#64948}
parent 66916488
......@@ -53,9 +53,10 @@ enum class MachineSemantic : uint8_t {
kAny
};
V8_EXPORT_PRIVATE inline int ElementSizeLog2Of(MachineRepresentation rep);
V8_EXPORT_PRIVATE inline constexpr int ElementSizeLog2Of(MachineRepresentation);
V8_EXPORT_PRIVATE inline int ElementSizeInBytes(MachineRepresentation rep);
V8_EXPORT_PRIVATE inline constexpr int ElementSizeInBytes(
MachineRepresentation);
class MachineType {
public:
......@@ -322,7 +323,8 @@ inline bool IsAnyCompressed(MachineRepresentation rep) {
}
// Gets the log2 of the element size in bytes of the machine type.
V8_EXPORT_PRIVATE inline int ElementSizeLog2Of(MachineRepresentation rep) {
V8_EXPORT_PRIVATE inline constexpr int ElementSizeLog2Of(
MachineRepresentation rep) {
switch (rep) {
case MachineRepresentation::kBit:
case MachineRepresentation::kWord8:
......@@ -344,12 +346,17 @@ V8_EXPORT_PRIVATE inline int ElementSizeLog2Of(MachineRepresentation rep) {
case MachineRepresentation::kCompressed:
return kTaggedSizeLog2;
default:
break;
#if V8_HAS_CXX14_CONSTEXPR
UNREACHABLE();
#else
// Return something for older compilers.
return -1;
#endif
}
UNREACHABLE();
}
V8_EXPORT_PRIVATE inline int ElementSizeInBytes(MachineRepresentation rep) {
V8_EXPORT_PRIVATE inline constexpr int ElementSizeInBytes(
MachineRepresentation rep) {
return 1 << ElementSizeLog2Of(rep);
}
......
......@@ -54,24 +54,22 @@ using FunctionSig = Signature<ValueType>;
inline size_t hash_value(ValueType type) { return static_cast<size_t>(type); }
// TODO(clemensb): Compute memtype and size from ValueType once we have c++14
// constexpr support.
#define FOREACH_LOAD_TYPE(V) \
V(I32, , Int32, 2) \
V(I32, 8S, Int8, 0) \
V(I32, 8U, Uint8, 0) \
V(I32, 16S, Int16, 1) \
V(I32, 16U, Uint16, 1) \
V(I64, , Int64, 3) \
V(I64, 8S, Int8, 0) \
V(I64, 8U, Uint8, 0) \
V(I64, 16S, Int16, 1) \
V(I64, 16U, Uint16, 1) \
V(I64, 32S, Int32, 2) \
V(I64, 32U, Uint32, 2) \
V(F32, , Float32, 2) \
V(F64, , Float64, 3) \
V(S128, , Simd128, 4)
V(I32, , Int32) \
V(I32, 8S, Int8) \
V(I32, 8U, Uint8) \
V(I32, 16S, Int16) \
V(I32, 16U, Uint16) \
V(I64, , Int64) \
V(I64, 8S, Int8) \
V(I64, 8U, Uint8) \
V(I64, 16S, Int16) \
V(I64, 16U, Uint16) \
V(I64, 32S, Int32) \
V(I64, 32U, Uint32) \
V(F32, , Float32) \
V(F64, , Float64) \
V(S128, , Simd128)
class LoadType {
public:
......@@ -110,7 +108,8 @@ class LoadType {
const LoadTypeValue val_;
static constexpr uint8_t kLoadSizeLog2[] = {
#define LOAD_SIZE(_, __, ___, size) size,
#define LOAD_SIZE(_, __, memtype) \
ElementSizeLog2Of(MachineType::memtype().representation()),
FOREACH_LOAD_TYPE(LOAD_SIZE)
#undef LOAD_SIZE
};
......@@ -122,23 +121,23 @@ class LoadType {
};
static constexpr MachineType kMemType[] = {
#define MEMTYPE(_, __, memtype, ___) MachineType::memtype(),
#define MEMTYPE(_, __, memtype) MachineType::memtype(),
FOREACH_LOAD_TYPE(MEMTYPE)
#undef MEMTYPE
};
};
#define FOREACH_STORE_TYPE(V) \
V(I32, , Word32, 2) \
V(I32, 8, Word8, 0) \
V(I32, 16, Word16, 1) \
V(I64, , Word64, 3) \
V(I64, 8, Word8, 0) \
V(I64, 16, Word16, 1) \
V(I64, 32, Word32, 2) \
V(F32, , Float32, 2) \
V(F64, , Float64, 3) \
V(S128, , Simd128, 4)
V(I32, , Word32) \
V(I32, 8, Word8) \
V(I32, 16, Word16) \
V(I64, , Word64) \
V(I64, 8, Word8) \
V(I64, 16, Word16) \
V(I64, 32, Word32) \
V(F32, , Float32) \
V(F64, , Float64) \
V(S128, , Simd128)
class StoreType {
public:
......@@ -177,7 +176,8 @@ class StoreType {
const StoreTypeValue val_;
static constexpr uint8_t kStoreSizeLog2[] = {
#define STORE_SIZE(_, __, ___, size) size,
#define STORE_SIZE(_, __, memrep) \
ElementSizeLog2Of(MachineRepresentation::k##memrep),
FOREACH_STORE_TYPE(STORE_SIZE)
#undef STORE_SIZE
};
......@@ -189,7 +189,7 @@ class StoreType {
};
static constexpr MachineRepresentation kMemRep[] = {
#define MEMREP(_, __, memrep, ___) MachineRepresentation::k##memrep,
#define MEMREP(_, __, memrep) MachineRepresentation::k##memrep,
FOREACH_STORE_TYPE(MEMREP)
#undef MEMREP
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment