Skip to content

Commit d7e8dae

Browse files
wip, starting to work
1 parent 85e68f2 commit d7e8dae

4 files changed

Lines changed: 966 additions & 923 deletions

File tree

src/ir/runtime-memory.h

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,14 @@ namespace {
2626

2727
void trapIfGt(uint64_t lhs, uint64_t rhs, const char* msg) {
2828
if (lhs > rhs) {
29-
std::stringstream ss;
30-
ss << msg << ": " << lhs << " > " << rhs;
29+
// std::stringstream ss;
30+
std::cerr << msg << ": " << lhs << " > " << rhs << "\n";
3131
// ss.str();
32+
// std::cerr<<ss
3233
throw TrapException{};
3334
}
3435
}
3536

36-
void checkLoadAddress(Address addr, Index bytes, Address memorySize) {
37-
Address memorySizeBytes = memorySize * Memory::kPageSize;
38-
trapIfGt(addr, memorySizeBytes - bytes, "highest > memory");
39-
}
40-
4137
// void checkAtomicAddress(Address addr, Index bytes, Address memorySize) {
4238
// checkLoadAddress(addr, bytes, memorySize);
4339
// // Unaligned atomics trap.
@@ -49,34 +45,66 @@ void checkLoadAddress(Address addr, Index bytes, Address memorySize) {
4945
// }
5046
// }
5147

52-
Address
53-
getFinalAddress(uint64_t offset, Literal ptr, Index bytes, Address memorySize) {
54-
Address memorySizeBytes = memorySize * Memory::kPageSize;
55-
uint64_t addr = ptr.type == Type::i32 ? ptr.geti32() : ptr.geti64();
48+
Address getFinalAddress(Address addr,
49+
Address offset,
50+
Index bytes,
51+
Address memorySizeBytes) {
5652
trapIfGt(offset, memorySizeBytes, "offset > memory");
5753
trapIfGt(addr, memorySizeBytes - offset, "final > memory");
58-
addr += offset;
54+
55+
// TODO: overflow here?
56+
addr = size_t(addr) + offset;
5957
trapIfGt(bytes, memorySizeBytes, "bytes > memory");
60-
checkLoadAddress(addr, bytes, memorySize);
58+
59+
// checkLoadAddress(addr, bytes, memorySizeBytes);
60+
trapIfGt(addr, memorySizeBytes - bytes, "highest > memory");
6161
return addr;
6262
}
6363

64+
template<typename T> static bool aligned(const uint8_t* address) {
65+
static_assert(!(sizeof(T) & (sizeof(T) - 1)), "must be a power of 2");
66+
return 0 == (reinterpret_cast<uintptr_t>(address) & (sizeof(T) - 1));
67+
}
68+
69+
template<typename T>
70+
T asdf(const std::vector<uint8_t>& memory, size_t address) {
71+
if (aligned<T>(&memory[address])) {
72+
return *reinterpret_cast<const T*>(&memory[address]);
73+
} else {
74+
T loaded;
75+
std::memcpy(&loaded, &memory[address], sizeof(T));
76+
return loaded;
77+
}
78+
}
6479
} // namespace
6580

6681
// TODO split into pure virtual class
6782
class RuntimeMemory {
6883
public:
69-
RuntimeMemory(Memory memory) : memoryDefinition(memory) {}
84+
// todo: might want a constructor that takes data segments
85+
RuntimeMemory(Memory memory)
86+
: memoryDefinition(std::move(memory)), memory(memory.initialByteSize(), 0) {
87+
// this->memory.reserve(memory.initialByteSize());
88+
}
7089

7190
virtual ~RuntimeMemory() = default;
7291

7392
// variants for load8 etc?
7493
// Do we care about the order here?
7594
// todo: address types? Address::address32_t is strange
7695
// todo: type of offset?
77-
virtual Literal load(Address addr, Address offset, MemoryOrder order) const {
78-
Address address = getFinalAddress(offset, Literal(addr), 4, 1);
79-
return {};
96+
virtual Literal load(Address addr,
97+
Address offset,
98+
uint8_t byteCount,
99+
MemoryOrder order) const {
100+
Address final = getFinalAddress(addr, offset, byteCount, memory.size());
101+
(void) final;
102+
103+
// return memory.get()
104+
105+
return Literal(asdf<int32_t>(memory, (size_t) final));
106+
107+
// return Literal(1);
80108
}
81109
virtual Literal load(uint64_t addr) const { return {}; }
82110

@@ -89,7 +117,10 @@ class RuntimeMemory {
89117
std::vector<uint8_t> memory;
90118
};
91119

92-
class RealRuntimeMemory : public RuntimeMemory {};
120+
class RealRuntimeMemory : public RuntimeMemory {
121+
public:
122+
using RuntimeMemory::RuntimeMemory;
123+
};
93124

94125
} // namespace wasm
95126

src/wasm-interpreter.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3262,7 +3262,9 @@ class ModuleRunnerBase : public ExpressionRunner<SubType> {
32623262
initializeTables();
32633263
initializeTags();
32643264

3265-
initializeMemoryContents();
3265+
initializeMemories();
3266+
// TODO:
3267+
// initializeMemoryContents();
32663268

32673269
// run start, if present
32683270
if (wasm.start.is()) {
@@ -3634,7 +3636,7 @@ class ModuleRunnerBase : public ExpressionRunner<SubType> {
36343636
assert(inserted && "Unexpected repeated memory name");
36353637
} else {
36363638
auto& runtimeMemory = definedMemories.emplace_back(
3637-
std::make_unique<RealRuntimeMemory>(memory));
3639+
std::make_unique<RealRuntimeMemory>(*memory));
36383640
[[maybe_unused]] auto [_, inserted] =
36393641
allMemories.try_emplace(memory->name, runtimeMemory.get());
36403642
assert(inserted && "Unexpected repeated memory name");
@@ -4110,6 +4112,7 @@ class ModuleRunnerBase : public ExpressionRunner<SubType> {
41104112
auto* memory = allMemories[curr->memory];
41114113
return memory->load(static_cast<uint32_t>(flow.getSingleValue().geti32()),
41124114
curr->offset,
4115+
curr->bytes,
41134116
curr->order);
41144117
// auto info = getMemoryInstanceInfo(curr->memory);
41154118
// auto memorySize = info.instance->getMemorySize(info.name);

src/wasm.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2572,9 +2572,9 @@ class Memory : public Importable {
25722572
}
25732573
return 1ull << (64 - pageSizeLog2);
25742574
}
2575-
Address::address64_t pageSize() const {
2576-
return 1ull << static_cast<Address::address64_t>(pageSizeLog2);
2577-
}
2575+
Address::address64_t pageSize() const { return 1ull << pageSizeLog2; }
2576+
2577+
Address::address64_t initialByteSize() const { return 1ull << pageSizeLog2; }
25782578
};
25792579

25802580
class Global : public Importable {

0 commit comments

Comments
 (0)