1 /*
2  * Copyright 2015 WebAssembly Community Group participants
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "asm_v_wasm.h"
18 #include "wasm.h"
19 
20 namespace wasm {
21 
asmToWasmType(AsmType asmType)22 Type asmToWasmType(AsmType asmType) {
23   switch (asmType) {
24     case ASM_INT:
25       return Type::i32;
26     case ASM_DOUBLE:
27       return Type::f64;
28     case ASM_FLOAT:
29       return Type::f32;
30     case ASM_INT64:
31       return Type::i64;
32     case ASM_NONE:
33       return Type::none;
34     case ASM_FLOAT32X4:
35     case ASM_FLOAT64X2:
36     case ASM_INT8X16:
37     case ASM_INT16X8:
38     case ASM_INT32X4:
39       return Type::v128;
40   }
41   WASM_UNREACHABLE("invalid type");
42 }
43 
wasmToAsmType(Type type)44 AsmType wasmToAsmType(Type type) {
45   TODO_SINGLE_COMPOUND(type);
46   switch (type.getBasic()) {
47     case Type::i32:
48       return ASM_INT;
49     case Type::f32:
50       return ASM_FLOAT;
51     case Type::f64:
52       return ASM_DOUBLE;
53     case Type::i64:
54       return ASM_INT64;
55     case Type::v128:
56       assert(false && "v128 not implemented yet");
57     case Type::funcref:
58     case Type::externref:
59     case Type::exnref:
60     case Type::anyref:
61     case Type::eqref:
62     case Type::i31ref:
63       assert(false && "reference types are not supported by asm2wasm");
64     case Type::none:
65       return ASM_NONE;
66     case Type::unreachable:
67       WASM_UNREACHABLE("invalid type");
68   }
69   WASM_UNREACHABLE("invalid type");
70 }
71 
getSig(Type type)72 char getSig(Type type) {
73   TODO_SINGLE_COMPOUND(type);
74   switch (type.getBasic()) {
75     case Type::i32:
76       return 'i';
77     case Type::i64:
78       return 'j';
79     case Type::f32:
80       return 'f';
81     case Type::f64:
82       return 'd';
83     case Type::v128:
84       return 'V';
85     case Type::funcref:
86       return 'F';
87     case Type::externref:
88       return 'X';
89     case Type::exnref:
90       return 'E';
91     case Type::anyref:
92       return 'A';
93     case Type::eqref:
94       return 'Q';
95     case Type::i31ref:
96       return 'I';
97     case Type::none:
98       return 'v';
99     case Type::unreachable:
100       WASM_UNREACHABLE("invalid type");
101   }
102   WASM_UNREACHABLE("invalid type");
103 }
104 
getSig(Function * func)105 std::string getSig(Function* func) {
106   return getSig(func->sig.results, func->sig.params);
107 }
108 
getSig(Type results,Type params)109 std::string getSig(Type results, Type params) {
110   assert(!results.isTuple());
111   std::string sig;
112   sig += getSig(results);
113   for (const auto& param : params) {
114     sig += getSig(param);
115   }
116   return sig;
117 }
118 
ensureDouble(Expression * expr,MixedArena & allocator)119 Expression* ensureDouble(Expression* expr, MixedArena& allocator) {
120   if (expr->type == Type::f32) {
121     auto conv = allocator.alloc<Unary>();
122     conv->op = PromoteFloat32;
123     conv->value = expr;
124     conv->type = Type::f64;
125     return conv;
126   }
127   assert(expr->type == Type::f64);
128   return expr;
129 }
130 
131 } // namespace wasm
132