forked from WebAssembly/binaryen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwasm-type.cpp
More file actions
143 lines (128 loc) · 2.92 KB
/
Copy pathwasm-type.cpp
File metadata and controls
143 lines (128 loc) · 2.92 KB
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
/*
* Copyright 2017 WebAssembly Community Group participants
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "wasm-type.h"
#include "wasm-features.h"
#include "compiler-support.h"
#include <cstdlib>
namespace wasm {
const char* printType(Type type) {
switch (type) {
case Type::none:
return "none";
case Type::i32:
return "i32";
case Type::i64:
return "i64";
case Type::f32:
return "f32";
case Type::f64:
return "f64";
case Type::v128:
return "v128";
case Type::exnref:
return "exnref";
case Type::unreachable:
return "unreachable";
}
WASM_UNREACHABLE();
}
unsigned getTypeSize(Type type) {
switch (type) {
case Type::i32:
return 4;
case Type::i64:
return 8;
case Type::f32:
return 4;
case Type::f64:
return 8;
case Type::v128:
return 16;
case Type::exnref: // exnref type is opaque
case Type::none:
case Type::unreachable:
WASM_UNREACHABLE();
}
WASM_UNREACHABLE();
}
FeatureSet getFeatures(Type type) {
if (type == v128) {
return FeatureSet::SIMD;
}
if (type == exnref) {
return FeatureSet::ExceptionHandling;
}
return FeatureSet();
}
Type getType(unsigned size, bool float_) {
if (size < 4) {
return Type::i32;
}
if (size == 4) {
return float_ ? Type::f32 : Type::i32;
}
if (size == 8) {
return float_ ? Type::f64 : Type::i64;
}
if (size == 16) {
return Type::v128;
}
WASM_UNREACHABLE();
}
Type getReachableType(Type a, Type b) { return a != unreachable ? a : b; }
bool isConcreteType(Type type) { return type != none && type != unreachable; }
bool isIntegerType(Type type) {
switch (type) {
case i32:
case i64:
return true;
default:
return false;
}
}
bool isFloatType(Type type) {
switch (type) {
case f32:
case f64:
return true;
default:
return false;
}
}
bool isVectorType(Type type) { return type == v128; }
bool isReferenceType(Type type) {
// TODO Add other reference types later
return type == exnref;
}
Type reinterpretType(Type type) {
switch (type) {
case Type::i32:
return f32;
case Type::i64:
return f64;
case Type::f32:
return i32;
case Type::f64:
return i64;
case Type::v128:
case Type::exnref:
case Type::none:
case Type::unreachable:
WASM_UNREACHABLE();
}
WASM_UNREACHABLE();
}
} // namespace wasm