summaryrefslogtreecommitdiff
path: root/vm/memory.go
blob: a0c610a4868da0c45ceacdb0b1cda35273ac40fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package main

import (
	"github.com/holiman/uint256"
	"log"
)

type Memory []byte

// store 256b word at offset in memory
func (m *Memory) sw(ost uint64, val *uint256.Int) {
	if ost + 32 > uint64(len(*m)) {
		*m = append(*m, make([]byte, ost + 32 - uint64(len(*m)))...)
	}

	bytes := val.Bytes32()
	copy((*m)[ost:], bytes[:])
}

// store byte at offset in memory
func (m *Memory) sb(ost uint64, val byte) {
	if ost + 1 > uint64(len(*m)) {
		*m = append(*m, make([]byte, ost + 1 - uint64(len(*m)))...)
	}
	
	(*m)[ost] = val
}

func (m *Memory) ld(ost uint64, n uint64) []byte {
	if ost + n > uint64(len(*m)) {
		log.Fatal("trying to load out of memory")
	}

	r := make([]byte, n)
	copy(r, (*m)[ost:ost+n])
	return r
}

func (m *Memory) lw(ost uint64) []byte {
	r := m.ld(ost, 32)
	return r
}