mohanson / pywasm

A WebAssembly interpreter written in pure Python
MIT License
450 stars 39 forks source link

The structure of memoryInstance. #31

Open binwang-neu opened 4 years ago

binwang-neu commented 4 years ago

I was confused with the structure of memoryInstance, why the type of data is bytearray? How about dict with bytearray in it? Doesn't this structure better match the addressing approach of memory?

mohanson commented 4 years ago

Good idea! In fact, when I wrote the code, I only noticed that the specification stated vec<byte>, I will try to use mem page in pywasm1.0

binwang-neu commented 4 years ago

In fact, I was confused with the addressing approach of memory in Webassembly. For example, ' i32.load ' instruction, it needs two args: address-operand and offset (ignore align). Assuming that address-operand is 100 and offset is 10, we can address 'a' in memory. If address-operand is 10 and offset is 100, which value can we address in memory? Is ‘a’? Of course, I'm not sure that's true in Webassembly. If address-operand and offset are both symbolic, is it theoretically possible to address all values in memory? What's your opinion?

mohanson commented 4 years ago

1) Is 'a'. real_address = operand + offset 2) The type of operand is int32, but the type of offset is uint32. The spec doesn't rules the addition operand, but i'm sure it's possible to address all values.

mohanson commented 4 years ago
mem_page = (operand + offset) / 65536
mem_page_offset =  (operand + offset) % 65536
real_byte = mem[mem_page][mem_page_offset]

Maybe poor in performance.

binwang-neu commented 4 years ago

Thanks.

mohanson commented 4 years ago

Due to the linear memory used by wasm, It does not exist concepts such as virtual memory, physical memory, etc.

After thinking and learning, I don't think it's necessary to modify the existing memory design, keeping flat is optimal.

https://github.com/WebAssembly/design/blob/master/Semantics.md#linear-memory

A linear memory can be considered to be an untyped array of bytes