Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 44 additions & 8 deletions src/literal.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <array>
#include <iostream>
#include <variant>

#include "support/bits.h"
#include "support/hash.h"
Expand Down Expand Up @@ -212,7 +213,7 @@ class Literal {
}
}

static Literal makeFromMemory(void* p, Type type);
static Literal makeFromMemory(const void* p, Type type);

static Literal makeSignedMin(Type type) {
switch (type.getBasic()) {
Expand Down Expand Up @@ -784,28 +785,63 @@ std::ostream& operator<<(std::ostream& o, wasm::Literals literals);
// A GC Struct, Array, or String is a set of values with a type saying how it
// should be interpreted.
struct GCData {
// The element or field values.
Literals values;
Type type;

// The element or field values. Primitive numeric arrays use raw byte buffers
// (std::vector<uint8_t>), while reference arrays, structs, strings, and other
// reference allocations use Literals.
std::variant<std::vector<uint8_t>, Literals> storage;

// The descriptor, if it exists, or null.
Literal desc;

GCData(Literals&& values,
GCData(Type type,
Literals&& values,
const Literal& desc = Literal::makeNull(HeapType::none))
: type(type), storage(std::move(values)), desc(desc) {}

GCData(Type type,
std::vector<uint8_t>&& data,
const Literal& desc = Literal::makeNull(HeapType::none))
: values(std::move(values)), desc(desc) {}
: type(type), storage(std::move(data)), desc(desc) {}

bool isRawBytes() const {
return std::holds_alternative<std::vector<uint8_t>>(storage);
}

const std::vector<uint8_t>& getRawBytes() const {
return std::get<std::vector<uint8_t>>(storage);
}

std::vector<uint8_t>& getRawBytes() {
return std::get<std::vector<uint8_t>>(storage);
}

const Literals& getLiterals() const { return std::get<Literals>(storage); }

Literals& getLiterals() { return std::get<Literals>(storage); }

size_t getNumElements() const;
Literal getElement(size_t index, bool signed_ = false) const;
void setElement(size_t index, Literal value);

static void writeField(void* p, const Field& field, Literal value);
static Literal
readField(const void* p, const Field& field, bool signed_ = false);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is p in these? Perhaps add a comment?

};

inline bool Literal::hasExternPayload() const {
if (isNull()) {
return false;
}
assert(type.getHeapType().isMaybeShared(HeapType::ext));
return gcData->values[0].type == Type::i32;
return !gcData->getLiterals().empty() &&
gcData->getLiterals()[0].type == Type::i32;
}

inline int32_t Literal::getExternPayload() const {
assert(hasExternPayload());
return gcData->values[0].geti32();
return gcData->getLiterals()[0].geti32();
}

} // namespace wasm
Expand Down Expand Up @@ -869,7 +905,7 @@ template<> struct hash<wasm::Literal> {
return digest;
}
if (a.type.isString()) {
auto& values = a.getGCData()->values;
auto& values = a.getGCData()->getLiterals();
wasm::rehash(digest, values.size());
for (auto c : values) {
wasm::rehash(digest, c.getInteger());
Expand Down
2 changes: 1 addition & 1 deletion src/passes/Precompute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1103,7 +1103,7 @@ struct Precompute
// string.
bool isValidUTF16Literal(const Literal& value) {
bool expectLowSurrogate = false;
for (auto& v : value.getGCData()->values) {
for (auto& v : value.getGCData()->getLiterals()) {
auto c = v.getInteger();
if (c >= 0xDC00 && c <= 0xDFFF) {
if (expectLowSurrogate) {
Expand Down
5 changes: 2 additions & 3 deletions src/tools/wasm-ctor-eval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,6 @@ struct CtorEvalExternalInterface : EvallingModuleRunner::ExternalInterface {
} else {
// This is the first usage of this data. Generate a struct.new /
// array.new for it.
auto& values = data->values;
std::vector<Expression*> args;

// The initial values for this allocation may themselves be GC
Expand All @@ -934,8 +933,8 @@ struct CtorEvalExternalInterface : EvallingModuleRunner::ExternalInterface {
definingGlobals[data] = DefiningGlobalInfo{definingGlobalName, type};
}

for (auto& value : values) {
auto* serialized = getSerialization(value);
for (size_t i = 0; i < data->getNumElements(); i++) {
auto* serialized = getSerialization(data->getElement(i));
if (!serialized) {
return nullptr;
}
Expand Down
2 changes: 1 addition & 1 deletion src/wasm-builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -1504,7 +1504,7 @@ class Builder {
// The string is already WTF-16, but we need to convert from `Literals` to
// actual string.
std::stringstream wtf16;
for (auto c : value.getGCData()->values) {
for (auto c : value.getGCData()->getLiterals()) {
auto u = c.getInteger();
assert(u < 0x10000);
wtf16 << uint8_t(u & 0xFF);
Expand Down
Loading
Loading