-
Notifications
You must be signed in to change notification settings - Fork 2
/
init_expr.go
194 lines (175 loc) · 4.38 KB
/
init_expr.go
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
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
92
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
* Copyright (C) 2018 The ontology Authors
* This file is part of The ontology library.
*
* The ontology is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The ontology is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with The ontology. If not, see <http://www.gnu.org/licenses/>.
*/
// Copyright 2017 The go-interpreter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package wasm
import (
"bytes"
"errors"
"fmt"
"io"
"math"
"OntologyWithPOC/vm/wasmvm/wasm/leb128"
)
const (
i32Const byte = 0x41
i64Const byte = 0x42
f32Const byte = 0x43
f64Const byte = 0x44
getGlobal byte = 0x23
end byte = 0x0b
)
var ErrEmptyInitExpr = errors.New("wasm: Initializer expression produces no value")
type InvalidInitExprOpError byte
func (e InvalidInitExprOpError) Error() string {
return fmt.Sprintf("wasm: Invalid opcode in initializer expression: %#x", byte(e))
}
type InvalidGlobalIndexError uint32
func (e InvalidGlobalIndexError) Error() string {
return fmt.Sprintf("wasm: Invalid index to global index space: %#x", uint32(e))
}
func readInitExpr(r io.Reader) ([]byte, error) {
b := make([]byte, 1)
buf := new(bytes.Buffer)
r = io.TeeReader(r, buf)
outer:
for {
_, err := io.ReadFull(r, b)
if err != nil {
return nil, err
}
switch b[0] {
case i32Const:
_, err := leb128.ReadVarint32(r)
if err != nil {
return nil, err
}
case i64Const:
_, err := leb128.ReadVarint64(r)
if err != nil {
return nil, err
}
case f32Const:
if _, err := readU32(r); err != nil {
return nil, err
}
case f64Const:
if _, err := readU64(r); err != nil {
return nil, err
}
case getGlobal:
_, err := leb128.ReadVarUint32(r)
if err != nil {
return nil, err
}
case end:
break outer
default:
return nil, InvalidInitExprOpError(b[0])
}
}
if buf.Len() == 0 {
return nil, ErrEmptyInitExpr
}
return buf.Bytes(), nil
}
// ExecInitExpr executes an initializer expression and returns an interface{} value
// which can either be int32, int64, float32 or float64.
// It returns an error if the expression is invalid, and nil when the expression
// yields no value.
func (m *Module) ExecInitExpr(expr []byte) (interface{}, error) {
var stack []uint64
var lastVal ValueType
r := bytes.NewReader(expr)
if r.Len() == 0 {
return nil, ErrEmptyInitExpr
}
for {
b, err := r.ReadByte()
if err == io.EOF {
break
} else if err != nil {
return nil, err
}
switch b {
case i32Const:
i, err := leb128.ReadVarint32(r)
if err != nil {
return nil, err
}
stack = append(stack, uint64(i))
lastVal = ValueTypeI32
case i64Const:
i, err := leb128.ReadVarint64(r)
if err != nil {
return nil, err
}
stack = append(stack, uint64(i))
lastVal = ValueTypeI64
case f32Const:
i, err := readU32(r)
if err != nil {
return nil, err
}
stack = append(stack, uint64(i))
lastVal = ValueTypeF32
case f64Const:
i, err := readU64(r)
if err != nil {
return nil, err
}
stack = append(stack, i)
lastVal = ValueTypeF64
case getGlobal:
index, err := leb128.ReadVarUint32(r)
if err != nil {
return nil, err
}
globalVar := m.GetGlobal(int(index))
if globalVar == nil {
return nil, InvalidGlobalIndexError(index)
}
lastVal = globalVar.Type.Type
//for env global
if globalVar.IsEnv {
stack = append(stack, globalVar.InitVal)
}
case end:
break
default:
return nil, InvalidInitExprOpError(b)
}
}
if len(stack) == 0 {
return nil, nil
}
v := stack[len(stack)-1]
switch lastVal {
case ValueTypeI32:
return int32(v), nil
case ValueTypeI64:
return int64(v), nil
case ValueTypeF32:
return math.Float32frombits(uint32(v)), nil
case ValueTypeF64:
return math.Float64frombits(uint64(v)), nil
default:
panic(fmt.Sprintf("Invalid value type produced by initializer expression: %d", int8(lastVal)))
}
}