diff options
-rw-r--r-- | src/s2wasm.h | 13 | ||||
-rw-r--r-- | src/wasm-binary.h | 2 | ||||
-rw-r--r-- | src/wasm-linker.h | 6 | ||||
-rw-r--r-- | src/wasm.h | 30 |
4 files changed, 36 insertions, 15 deletions
diff --git a/src/s2wasm.h b/src/s2wasm.h index c4bd76e43..54d36011f 100644 --- a/src/s2wasm.h +++ b/src/s2wasm.h @@ -211,7 +211,8 @@ class S2WasmBuilder { // gets a constant, which may be a relocation for later. // returns whether this is a relocation - bool getConst(uint32_t* target) { + // TODO: Clean up this and the way relocs are created from parsed objects + bool getRelocatableConst(uint32_t* target) { if (isdigit(*s) || *s == '-') { int32_t val = getInt(); memcpy(target, &val, sizeof(val)); @@ -659,7 +660,7 @@ class S2WasmBuilder { curr->signed_ = match("_s"); match("_u"); Name assign = getAssign(); - getConst(&curr->offset); + getRelocatableConst(&curr->offset.addr); mustMatch("("); auto attributes = getAttributes(1); curr->ptr = getInput(); @@ -677,7 +678,7 @@ class S2WasmBuilder { int32_t bytes = getInt() / CHAR_BIT; curr->bytes = bytes > 0 ? bytes : getWasmTypeSize(type); Name assign = getAssign(); - getConst(&curr->offset); + getRelocatableConst(&curr->offset.addr); mustMatch("("); auto attributes = getAttributes(2); auto inputs = getInputs(2); @@ -755,7 +756,7 @@ class S2WasmBuilder { // may be a relocation auto curr = allocator->alloc<Const>(); curr->type = curr->value.type = i32; - getConst((uint32_t*)curr->value.geti32Ptr()); + getRelocatableConst((uint32_t*)curr->value.geti32Ptr()); setOutput(curr, assign); } else { cashew::IString str = getStr(); @@ -1071,7 +1072,7 @@ class S2WasmBuilder { value = getInt(); if (value != 0) zero = false; } - for (Address i = 0, e = size; i < e; i++) { + for (Address i = 0, e = size; i < e; ++i) { raw.push_back(value); } } else if (match(".int8")) { @@ -1088,7 +1089,7 @@ class S2WasmBuilder { } else if (match(".int32")) { Address size = raw.size(); raw.resize(size + 4); - if (getConst((uint32_t*)&raw[size])) { // just the size, as we may reallocate; we must fix this later, if it's a relocation + if (getRelocatableConst((uint32_t*)&raw[size])) { // just the size, as we may reallocate; we must fix this later, if it's a relocation currRelocations.emplace_back(linkerObj->getCurrentRelocation(), size); } zero = false; diff --git a/src/wasm-binary.h b/src/wasm-binary.h index bd78d9f9d..1fe3ef26a 100644 --- a/src/wasm-binary.h +++ b/src/wasm-binary.h @@ -1829,7 +1829,7 @@ public: curr->type = curr->value->type; } - void readMemoryAccess(uint32_t& alignment, size_t bytes, uint32_t& offset) { + void readMemoryAccess(Address& alignment, size_t bytes, Address& offset) { alignment = Pow2(getU32LEB()); offset = getU32LEB(); } diff --git a/src/wasm-linker.h b/src/wasm-linker.h index 41c0f3c7e..5fe01b393 100644 --- a/src/wasm-linker.h +++ b/src/wasm-linker.h @@ -100,14 +100,12 @@ class LinkerObject { // Add an initializer segment for the named static variable. void addSegment(Name name, const char* data, Address size) { - assert(wasm.memory.segments.size() < std::numeric_limits<Address>::max()); - segments[name] = static_cast<Address>(wasm.memory.segments.size()); + segments[name] = wasm.memory.segments.size(); wasm.memory.segments.emplace_back(0, data, size); } void addSegment(Name name, std::vector<char>& data) { - assert(wasm.memory.segments.size() < std::numeric_limits<Address>::max()); - segments[name] = static_cast<Address>(wasm.memory.segments.size()); + segments[name] = wasm.memory.segments.size(); wasm.memory.segments.emplace_back(0, data); } diff --git a/src/wasm.h b/src/wasm.h index 9f5f6820c..327be9c7e 100644 --- a/src/wasm.h +++ b/src/wasm.h @@ -92,7 +92,21 @@ struct Name : public cashew::IString { typedef uint32_t Index; // An address in linear memory. For now only wasm32 -typedef uint32_t Address; +struct Address { + typedef uint32_t address_t; + address_t addr; + Address() : addr(0) {} + Address(uint64_t a) : addr(static_cast<address_t>(a)) { + assert(a <= std::numeric_limits<address_t>::max()); + } + Address& operator=(uint64_t a) { + assert(a <= std::numeric_limits<address_t>::max()); + addr = static_cast<address_t>(a); + return *this; + } + operator address_t() const { return addr; } + Address& operator++() { ++addr; return *this; } +}; // Types @@ -1266,8 +1280,8 @@ public: class Memory { public: - static const Address kPageSize = 64 * 1024; - static const Address kPageMask = ~(kPageSize - 1); + static const Address::address_t kPageSize = 64 * 1024; + static const Address::address_t kPageMask = ~(kPageSize - 1); struct Segment { Address offset; std::vector<char> data; // TODO: optimize @@ -1285,7 +1299,7 @@ public: std::vector<Segment> segments; Name exportName; - Memory() : initial(0), max((Address)-1) {} + Memory() : initial(0), max(-1U) {} }; class Module { @@ -1387,4 +1401,12 @@ private: } // namespace wasm +namespace std { +template<> struct hash<wasm::Address> { + size_t operator()(const wasm::Address a) const { + return std::hash<uint32_t>()(a.addr); + } +}; +} + #endif // wasm_wasm_h |