Class: Memory
- Defined in:
- lib/rlang/lib/memory.rb,
lib/simul/classes/memory.rb
Overview
Rubinius WebAssembly VM Copyright © 2019-2020, Laurent Julliard and contributors All rights reserved.
Web Assembly memory access methods
Constant Summary collapse
- MAX_SIZE =
Initial memory size (8 pages of WASM memory - 64 KB each)
8
- WASM_PAGE_SIZE =
64 * 1024
- MAX_I32 =
2**32 - 1
- @@mem =
nil
Class Method Summary collapse
-
.copy(src, dest, size) ⇒ Object
Copy memory from source to destination address TODO: optimize this method using 64 bits copy first then 32 bits, then 16, then 8 bits.
- .dump ⇒ Object
- .grow(delta) ⇒ Object
-
.init(max_size) ⇒ Object
size unit in WASM pages (64 KB).
- .load(address, type = Type::I32, nbits = nil, signed = false) ⇒ Object
- .load32(addr) ⇒ Object
- .load32_16(addr) ⇒ Object
- .load32_8(addr) ⇒ Object
- .load64(addr) ⇒ Object
- .load64_16(addr) ⇒ Object
- .load64_32(addr) ⇒ Object
- .load64_8(addr) ⇒ Object
- .size ⇒ Object
- .size_in_bytes ⇒ Object
- .store(address, value, nbits = 32) ⇒ Object
- .store32(addr, value) ⇒ Object
- .store32_16(addr, value) ⇒ Object
- .store32_8(addr, value) ⇒ Object
- .store64(addr, value) ⇒ Object
- .store64_16(addr, value) ⇒ Object
- .store64_32(addr, value) ⇒ Object
- .store64_8(addr, value) ⇒ Object
Class Method Details
.copy(src, dest, size) ⇒ Object
Copy memory from source to destination address TODO: optimize this method using 64 bits copy first then 32 bits, then 16, then 8 bits
21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/rlang/lib/memory.rb', line 21 def self.copy(src, dest, size) arg src: :I32, dest: :I32 result :none idx = 0 while idx < size inline wat: '(i32.store8 (i32.add (local.get $dest) (local.get $idx)) (i32.load8_u (i32.add (local.get $src) (local.get $idx))) )', wtype: :none idx += 1 end end |
.dump ⇒ Object
26 27 28 |
# File 'lib/simul/classes/memory.rb', line 26 def self.dump @@mem end |
.grow(delta) ⇒ Object
14 15 16 |
# File 'lib/rlang/lib/memory.rb', line 14 def self.grow(delta) inline wat: '(memory.grow (local.get $delta))' end |
.init(max_size) ⇒ Object
size unit in WASM pages (64 KB)
12 13 14 15 16 |
# File 'lib/simul/classes/memory.rb', line 12 def self.init(max_size) @@max_size = max_size || MAX_SIZE @@mem_size = @@max_size * WASM_PAGE_SIZE @@mem = String.new("\00"*@@mem_size, encoding:'ASCII-8BIT', capacity: @@mem_size) end |
.load(address, type = Type::I32, nbits = nil, signed = false) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/simul/classes/memory.rb', line 30 def self.load(address, type=Type::I32 , nbits=nil, signed=false) if type == Type::I32 if nbits.nil? || nbits == 32 # i32.load: load 4 bytes as i32 value = @@mem.byteslice(address,4).unpack('L').first elsif nbits == 16 if signed # i32.load16_s: load 2 bytes and sign-extend i16 to i32 value = @@mem.byteslice(address,2).unpack('s').first else # i32.load16_u: load 2 bytes and zero-extend i16 to i32 value = @@mem.byteslice(address,2).unpack('S').first end elsif nbits == 8 if signed # i32.load8_s: load 1 byte and sign-extend i8 to i32 value = @@mem.byteslice(address).unpack('c').first else # i32.load16_u: load 1 byte and zero-extend i8 to i32 value = @@mem.byteslice(address).unpack('C').first end else raise "Unknown number of bits #{nbits}" end return Type::I32.new(value) elsif type == Type::I64 if nbits.nil? || nbits == 64 # i64.load: load 8 bytes as i64 value = @@mem.byteslice(address,8).unpack('Q').first elsif nbits == 32 if signed # i64.load32_s: load 4 bytes and sign-extend i32 to i64 value = @@mem.byteslice(address,4).unpack('l').first else # i64.load32_u: load 4 bytes and zero-extend i32 to i64 value = @@mem.byteslice(address,4).unpack('L').first end elsif nbits == 16 if signed # i64.load16_s: load 2 bytes and sign-extend i16 to i64 value = @@mem.byteslice(address,2).unpack('s').first else # i64.load16_u: load 2 bytes and zero-extend i16 to i64 value = @@mem.byteslice(address,2).unpack('S').first end elsif nbits == 8 if signed # i64.load8_s: load 1 byte and sign-extend i8 to i64 value = @@mem.byteslice(address).unpack('c').first else # i64.load8_u: load 1 byte and zero-extend i8 to i64 value = @@mem.byteslice(address).unpack('C').first end else raise "Unknown number of bits #{nbits}" end return Type::I64.new(value) else raise "Unknown target type #{type}" end end |
.load32(addr) ⇒ Object
48 49 50 51 52 53 |
# File 'lib/rlang/lib/memory.rb', line 48 def self.load32(addr) arg addr: :I32 result :I32 inline wat: '(i32.load (local.get $addr))', wtype: :I32 end |
.load32_16(addr) ⇒ Object
41 42 43 44 45 46 |
# File 'lib/rlang/lib/memory.rb', line 41 def self.load32_16(addr) arg addr: :I32 result :I32 inline wat: '(i32.load16_u (local.get $addr))', wtype: :I32 end |
.load32_8(addr) ⇒ Object
34 35 36 37 38 39 |
# File 'lib/rlang/lib/memory.rb', line 34 def self.load32_8(addr) arg addr: :I32 result :I32 inline wat: '(i32.load8_u (local.get $addr))', wtype: :I32 end |
.load64(addr) ⇒ Object
74 75 76 77 78 79 |
# File 'lib/rlang/lib/memory.rb', line 74 def self.load64(addr) arg addr: :I32 result :I64 inline wat: '(i64.load (local.get $addr))', wtype: :I64 end |
.load64_16(addr) ⇒ Object
62 63 64 65 66 67 |
# File 'lib/rlang/lib/memory.rb', line 62 def self.load64_16(addr) arg addr: :I32 result :I64 inline wat: '(i64.load16_u (local.get $addr))', wtype: :I64 end |
.load64_32(addr) ⇒ Object
68 69 70 71 72 73 |
# File 'lib/rlang/lib/memory.rb', line 68 def self.load64_32(addr) arg addr: :I32 result :I64 inline wat: '(i64.load32_u (local.get $addr))', wtype: :I64 end |
.load64_8(addr) ⇒ Object
55 56 57 58 59 60 |
# File 'lib/rlang/lib/memory.rb', line 55 def self.load64_8(addr) arg addr: :I32 result :I64 inline wat: '(i64.load8_u (local.get $addr))', wtype: :I64 end |
.size ⇒ Object
10 11 12 |
# File 'lib/rlang/lib/memory.rb', line 10 def self.size inline wat: '(memory.size)' end |
.size_in_bytes ⇒ Object
22 23 24 |
# File 'lib/simul/classes/memory.rb', line 22 def self.size_in_bytes @@mem_size end |
.store(address, value, nbits = 32) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/simul/classes/memory.rb', line 93 def self.store(address, value, nbits=32) #puts '***', address, value, nbits if value.is_a? Integer || (value.is_a? Type::I32) raise "Value #{value} too large to fit in 32 bits at address #{}" if value > MAX_I32 case nbits when 64 raise "Cannot store i64 in i32 memory location (wrap/truncate first)" when nil, 32 # i32.store: (no conversion) store 4 bytes @@mem[address,4] = [value].pack('L<') when 16 # i32.store16: wrap i32 to i16 and store 2 bytes @@mem[address,2] = [value].pack('S<') when 8 # i32.store8: wrap i32 to i8 and store 1 byte @@mem[address] = [value].pack('C') end elsif value.is_a? Type::I64 case nbits when nil, 64 # i64.store: (no conversion) store 8 bytes @@mem[address,8] = [value].pack('Q<') when 32 # i64.store32: (no conversion) store 4 bytes @@mem[address,4] = [value].pack('L<') when 16 # i64.store16: wrap i32 to i16 and store 2 bytes @@mem[address,2] = [value].pack('S<') when 8 # i64.store8: wrap i32 to i8 and store 1 byte @@mem[address] = [value].pack('C') end else raise "Unknow data type #{value.class} for #{value}" end end |
.store32(addr, value) ⇒ Object
95 96 97 98 99 100 |
# File 'lib/rlang/lib/memory.rb', line 95 def self.store32(addr, value) arg addr: :I32, value: :I32 result :none inline wat: '(i32.store (local.get $addr) (local.get $value))', wtype: :none end |
.store32_16(addr, value) ⇒ Object
88 89 90 91 92 93 |
# File 'lib/rlang/lib/memory.rb', line 88 def self.store32_16(addr, value) arg addr: :I32, value: :I32 result :none inline wat: '(i32.store16 (local.get $addr) (local.get $value))', wtype: :none end |
.store32_8(addr, value) ⇒ Object
81 82 83 84 85 86 |
# File 'lib/rlang/lib/memory.rb', line 81 def self.store32_8(addr, value) arg addr: :I32, value: :I32 result :none inline wat: '(i32.store8 (local.get $addr) (local.get $value))', wtype: :none end |
.store64(addr, value) ⇒ Object
123 124 125 126 127 128 |
# File 'lib/rlang/lib/memory.rb', line 123 def self.store64(addr, value) arg addr: :I32, value: :I64 result :none inline wat: '(i64.store (local.get $addr) (local.get $value))', wtype: :none end |
.store64_16(addr, value) ⇒ Object
109 110 111 112 113 114 |
# File 'lib/rlang/lib/memory.rb', line 109 def self.store64_16(addr, value) arg addr: :I32, value: :I64 result :none inline wat: '(i64.store16 (local.get $addr) (local.get $value))', wtype: :none end |
.store64_32(addr, value) ⇒ Object
116 117 118 119 120 121 |
# File 'lib/rlang/lib/memory.rb', line 116 def self.store64_32(addr, value) arg addr: :I32, value: :I64 result :none inline wat: '(i64.store32 (local.get $addr) (local.get $value))', wtype: :none end |
.store64_8(addr, value) ⇒ Object
102 103 104 105 106 107 |
# File 'lib/rlang/lib/memory.rb', line 102 def self.store64_8(addr, value) arg addr: :I32, value: :I64 result :none inline wat: '(i64.store8 (local.get $addr) (local.get $value))', wtype: :none end |