1717#include " ir/runtime-memory.h"
1818#include " fp16.h"
1919#include " interpreter/exception.h"
20+ #include < iostream>
2021
2122namespace wasm {
2223
23- RuntimeMemory::RuntimeMemory (Memory memory,
24- ExternalInterface* externalInterface)
25- : externalInterface(externalInterface), memoryDefinition(std::move(memory)) {}
26-
2724namespace {
2825
29- Address getFinalAddress (const RuntimeMemory& runtimeMemory,
30- Address addr,
26+ [[noreturn]] void trap (std::string_view reason) {
27+ std::cout << " [trap " << reason << " ]\n " ;
28+ throw TrapException{};
29+ }
30+
31+ Address getFinalAddress (Address addr,
3132 Address offset,
3233 Index bytes,
3334 Address memorySizeBytes) {
@@ -36,14 +37,14 @@ Address getFinalAddress(const RuntimeMemory& runtimeMemory,
3637 msg += std::to_string (uint64_t (offset));
3738 msg += " > " ;
3839 msg += std::to_string (uint64_t (memorySizeBytes));
39- runtimeMemory. trap (msg);
40+ trap (msg);
4041 }
4142 if (addr > memorySizeBytes - offset) {
4243 std::string msg = " final > memory: " ;
4344 msg += std::to_string (uint64_t (addr));
4445 msg += " > " ;
4546 msg += std::to_string (uint64_t (memorySizeBytes - offset));
46- runtimeMemory. trap (msg);
47+ trap (msg);
4748 }
4849
4950 addr = size_t (addr) + offset;
@@ -55,13 +56,12 @@ Address getFinalAddress(const RuntimeMemory& runtimeMemory,
5556 msg += std::to_string (uint64_t (bytes));
5657 msg += " > " ;
5758 msg += std::to_string (uint64_t (memorySizeBytes));
58- runtimeMemory. trap (msg);
59+ trap (msg);
5960 }
6061 return addr;
6162}
6263
63- void checkLoadAddress (const RuntimeMemory& runtimeMemory,
64- Address addr,
64+ void checkLoadAddress (Address addr,
6565 Index bytes,
6666 Address memorySizeBytes) {
6767 if (addr > memorySizeBytes || bytes > memorySizeBytes - addr) {
@@ -71,19 +71,18 @@ void checkLoadAddress(const RuntimeMemory& runtimeMemory,
7171 msg += std::to_string (uint64_t (bytes));
7272 msg += " > " ;
7373 msg += std::to_string (uint64_t (memorySizeBytes));
74- runtimeMemory. trap (msg);
74+ trap (msg);
7575 }
7676}
7777
78- void checkAtomicAddress (const RuntimeMemory& runtimeMemory,
79- Address addr,
78+ void checkAtomicAddress (Address addr,
8079 Index bytes,
8180 Address memorySizeBytes) {
82- checkLoadAddress (runtimeMemory, addr, bytes, memorySizeBytes);
81+ checkLoadAddress (addr, bytes, memorySizeBytes);
8382 // Unaligned atomics trap.
8483 if (bytes > 1 ) {
8584 if (addr & (bytes - 1 )) {
86- runtimeMemory. trap (" unaligned atomic operation" );
85+ trap (" unaligned atomic operation" );
8786 }
8887 }
8988}
@@ -95,9 +94,8 @@ template<typename T> bool aligned(const uint8_t* address) {
9594
9695} // namespace
9796
98- RealRuntimeMemory::RealRuntimeMemory (Memory memory,
99- ExternalInterface* externalInterface)
100- : RuntimeMemory(std::move(memory), externalInterface) {
97+ RealRuntimeMemory::RealRuntimeMemory (Memory memory)
98+ : RuntimeMemory(std::move(memory)) {
10199 resize (memoryDefinition.initialByteSize ());
102100}
103101
@@ -107,9 +105,9 @@ Literal RealRuntimeMemory::load(Address addr,
107105 MemoryOrder order,
108106 Type type,
109107 bool signed_) const {
110- Address final = getFinalAddress (* this , addr, offset, byteCount, size ());
108+ Address final = getFinalAddress (addr, offset, byteCount, size ());
111109 if (order != MemoryOrder::Unordered) {
112- checkAtomicAddress (* this , final , byteCount, size ());
110+ checkAtomicAddress (final , byteCount, size ());
113111 }
114112 switch (type.getBasic ()) {
115113 case Type::i32 : {
@@ -170,9 +168,9 @@ void RealRuntimeMemory::store(Address addr,
170168 MemoryOrder order,
171169 Literal value,
172170 Type type) {
173- Address final = getFinalAddress (* this , addr, offset, byteCount, size ());
171+ Address final = getFinalAddress (addr, offset, byteCount, size ());
174172 if (order != MemoryOrder::Unordered) {
175- checkAtomicAddress (* this , final , byteCount, size ());
173+ checkAtomicAddress (final , byteCount, size ());
176174 }
177175 switch (type.getBasic ()) {
178176 case Type::i32 : {
@@ -213,9 +211,9 @@ void RealRuntimeMemory::store(Address addr,
213211 case Type::f32 : {
214212 switch (byteCount) {
215213 case 2 :
216- set<uint16_t >(
217- final ,
218- fp16_ieee_from_fp32_value ( bit_cast<float >(value.reinterpreti32 ())));
214+ set<uint16_t >(final ,
215+ fp16_ieee_from_fp32_value (
216+ bit_cast<float >(value.reinterpreti32 ())));
219217 break ;
220218 case 4 :
221219 set<int32_t >(final , value.reinterpreti32 ());
@@ -261,7 +259,7 @@ void RealRuntimeMemory::init(Address dest,
261259 if (src > data->data .size () || byteCount > data->data .size () - src) {
262260 trap (" out of bounds segment access in memory.init" );
263261 }
264- Address final = getFinalAddress (* this , dest, 0 , byteCount, size ());
262+ Address final = getFinalAddress (dest, 0 , byteCount, size ());
265263 if (byteCount > 0 ) {
266264 std::memcpy (&memory[final ], &data->data [src], byteCount);
267265 }
@@ -271,9 +269,8 @@ void RealRuntimeMemory::copy(Address dest,
271269 Address src,
272270 Address byteCount,
273271 const RuntimeMemory* srcMemory) {
274- Address finalDest = getFinalAddress (*this , dest, 0 , byteCount, size ());
275- Address finalSrc =
276- getFinalAddress (*srcMemory, src, 0 , byteCount, srcMemory->size ());
272+ Address finalDest = getFinalAddress (dest, 0 , byteCount, size ());
273+ Address finalSrc = getFinalAddress (src, 0 , byteCount, srcMemory->size ());
277274 const std::vector<uint8_t >* srcBuffer = srcMemory->getBuffer ();
278275 if (!srcBuffer) {
279276 // If it's not a memory with a direct buffer, we might need another way to
@@ -287,7 +284,7 @@ void RealRuntimeMemory::copy(Address dest,
287284}
288285
289286void RealRuntimeMemory::fill (Address dest, uint8_t value, Address byteCount) {
290- Address final = getFinalAddress (* this , dest, 0 , byteCount, size ());
287+ Address final = getFinalAddress (dest, 0 , byteCount, size ());
291288 if (byteCount > 0 ) {
292289 std::memset (&memory[final ], value, byteCount);
293290 }
@@ -300,8 +297,7 @@ void RealRuntimeMemory::resize(size_t newSize) {
300297 size_t newAllocatedSize = std::max (minSize, newSize);
301298 if (newAllocatedSize > oldAllocatedSize) {
302299 memory.resize (newAllocatedSize);
303- std::memset (
304- &memory[oldAllocatedSize], 0 , newAllocatedSize - oldAllocatedSize);
300+ std::memset (&memory[oldAllocatedSize], 0 , newAllocatedSize - oldAllocatedSize);
305301 }
306302 if (newSize < oldAllocatedSize && newSize < minSize) {
307303 std::memset (&memory[newSize], 0 , minSize - newSize);
@@ -347,7 +343,6 @@ template void RealRuntimeMemory::set<uint32_t>(size_t, uint32_t);
347343template void RealRuntimeMemory::set<int64_t >(size_t , int64_t );
348344template void RealRuntimeMemory::set<uint64_t >(size_t , uint64_t );
349345template void
350- RealRuntimeMemory::set<std::array<uint8_t , 16 >>(size_t ,
351- std::array<uint8_t , 16 >);
346+ RealRuntimeMemory::set<std::array<uint8_t , 16 >>(size_t , std::array<uint8_t , 16 >);
352347
353348} // namespace wasm
0 commit comments