forked from DNAProject/DNA
-
Notifications
You must be signed in to change notification settings - Fork 1
/
conv.go
82 lines (72 loc) · 2.83 KB
/
conv.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
/*
* Copyright (C) 2018 The DNA Authors
* This file is part of The DNA library.
*
* The DNA 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 DNA 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 DNA. 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 operators
import (
"regexp"
"github.com/dnaproject2/DNA/vm/wasmvm/wasm"
)
var reCvrtOp = regexp.MustCompile(`(.+)\.(?:[a-z]|\_)+\/(.+)`)
func valType(s string) wasm.ValueType {
switch s {
case "i32":
return wasm.ValueTypeI32
case "i64":
return wasm.ValueTypeI64
case "f32":
return wasm.ValueTypeF32
case "f64":
return wasm.ValueTypeF64
default:
panic("Invalid value type string: " + s)
}
}
func newConversionOp(code byte, name string) byte {
matches := reCvrtOp.FindStringSubmatch(name)
if len(matches) == 0 {
panic(name + " is not a conversion operator")
}
returns := valType(matches[1])
param := valType(matches[2])
return newOp(code, name, []wasm.ValueType{param}, returns)
}
var (
I32WrapI64 = newConversionOp(0xa7, "i32.wrap/i64")
I32TruncSF32 = newConversionOp(0xa8, "i32.trunc_s/f32")
I32TruncUF32 = newConversionOp(0xa9, "i32.trunc_u/f32")
I32TruncSF64 = newConversionOp(0xaa, "i32.trunc_s/f64")
I32TruncUF64 = newConversionOp(0xab, "i32.trunc_u/f64")
I64ExtendSI32 = newConversionOp(0xac, "i64.extend_s/i32")
I64ExtendUI32 = newConversionOp(0xad, "i64.extend_u/i32")
I64TruncSF32 = newConversionOp(0xae, "i64.trunc_s/f32")
I64TruncUF32 = newConversionOp(0xaf, "i64.trunc_u/f32")
I64TruncSF64 = newConversionOp(0xb0, "i64.trunc_s/f64")
I64TruncUF64 = newConversionOp(0xb1, "i64.trunc_u/f64")
F32ConvertSI32 = newConversionOp(0xb2, "f32.convert_s/i32")
F32ConvertUI32 = newConversionOp(0xb3, "f32.convert_u/i32")
F32ConvertSI64 = newConversionOp(0xb4, "f32.convert_s/i64")
F32ConvertUI64 = newConversionOp(0xb5, "f32.convert_u/i64")
F32DemoteF64 = newConversionOp(0xb6, "f32.demote/f64")
F64ConvertSI32 = newConversionOp(0xb7, "f64.convert_s/i32")
F64ConvertUI32 = newConversionOp(0xb8, "f64.convert_u/i32")
F64ConvertSI64 = newConversionOp(0xb9, "f64.convert_s/i64")
F64ConvertUI64 = newConversionOp(0xba, "f64.convert_u/i64")
F64PromoteF32 = newConversionOp(0xbb, "f64.promote/f32")
)