1 /*
2 * Copyright 2018 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 #ifndef wasm_abi_abi_h
18 #define wasm_abi_abi_h
19
20 #include "asmjs/shared-constants.h"
21 #include "wasm.h"
22
23 namespace wasm {
24
25 namespace ABI {
26
27 enum class LegalizationLevel { Full = 0, Minimal = 1 };
28
getLegalizationPass(LegalizationLevel level)29 inline std::string getLegalizationPass(LegalizationLevel level) {
30 if (level == LegalizationLevel::Full) {
31 return "legalize-js-interface";
32 } else {
33 return "legalize-js-interface-minimally";
34 }
35 }
36
37 namespace wasm2js {
38
39 extern cashew::IString SCRATCH_LOAD_I32;
40 extern cashew::IString SCRATCH_STORE_I32;
41 extern cashew::IString SCRATCH_LOAD_F32;
42 extern cashew::IString SCRATCH_STORE_F32;
43 extern cashew::IString SCRATCH_LOAD_F64;
44 extern cashew::IString SCRATCH_STORE_F64;
45 extern cashew::IString MEMORY_INIT;
46 extern cashew::IString MEMORY_FILL;
47 extern cashew::IString MEMORY_COPY;
48 extern cashew::IString DATA_DROP;
49 extern cashew::IString ATOMIC_WAIT_I32;
50 extern cashew::IString ATOMIC_RMW_I64;
51 extern cashew::IString GET_STASHED_BITS;
52
53 // The wasm2js helpers let us do things that can't be done without special help,
54 // like read and write to scratch memory for purposes of implementing things
55 // like reinterpret, etc.
56 // The optional "specific" parameter is a specific function we want. If not
57 // provided, we create them all.
58 inline void ensureHelpers(Module* wasm,
59 cashew::IString specific = cashew::IString()) {
60 auto ensureImport = [&](Name name, Type params, Type results) {
61 if (wasm->getFunctionOrNull(name)) {
62 return;
63 }
64 if (specific.is() && name != specific) {
65 return;
66 }
67 auto func = make_unique<Function>();
68 func->name = name;
69 func->sig = Signature(params, results);
70 func->module = ENV;
71 func->base = name;
72 wasm->addFunction(std::move(func));
73 };
74
75 ensureImport(SCRATCH_LOAD_I32, {Type::i32}, Type::i32);
76 ensureImport(SCRATCH_STORE_I32, {Type::i32, Type::i32}, Type::none);
77 ensureImport(SCRATCH_LOAD_F32, {}, Type::f32);
78 ensureImport(SCRATCH_STORE_F32, {Type::f32}, Type::none);
79 ensureImport(SCRATCH_LOAD_F64, {}, Type::f64);
80 ensureImport(SCRATCH_STORE_F64, {Type::f64}, Type::none);
81 ensureImport(
82 MEMORY_INIT, {Type::i32, Type::i32, Type::i32, Type::i32}, Type::none);
83 ensureImport(MEMORY_FILL, {Type::i32, Type::i32, Type::i32}, Type::none);
84 ensureImport(MEMORY_COPY, {Type::i32, Type::i32, Type::i32}, Type::none);
85 ensureImport(DATA_DROP, {Type::i32}, Type::none);
86 ensureImport(
87 ATOMIC_WAIT_I32, {Type::i32, Type::i32, Type::i32, Type::i32}, Type::i32);
88 ensureImport(
89 ATOMIC_RMW_I64,
90 {Type::i32, Type::i32, Type::i32, Type::i32, Type::i32, Type::i32},
91 Type::i32);
92 ensureImport(GET_STASHED_BITS, {}, Type::i32);
93 }
94
isHelper(cashew::IString name)95 inline bool isHelper(cashew::IString name) {
96 return name == SCRATCH_LOAD_I32 || name == SCRATCH_STORE_I32 ||
97 name == SCRATCH_LOAD_F32 || name == SCRATCH_STORE_F32 ||
98 name == SCRATCH_LOAD_F64 || name == SCRATCH_STORE_F64 ||
99 name == ATOMIC_WAIT_I32 || name == MEMORY_INIT ||
100 name == MEMORY_FILL || name == MEMORY_COPY || name == DATA_DROP ||
101 name == ATOMIC_RMW_I64 || name == GET_STASHED_BITS;
102 }
103
104 } // namespace wasm2js
105
106 } // namespace ABI
107
108 } // namespace wasm
109
110 #endif // wasm_abi_abi_h
111