| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* |
| 4 |
* Copyright 2021 Mozilla Foundation |
| 5 |
* |
| 6 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 |
* you may not use this file except in compliance with the License. |
| 8 |
* You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, software |
| 13 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
* See the License for the specific language governing permissions and |
| 16 |
* limitations under the License. |
| 17 |
*/ |
| 18 |
|
| 19 |
#ifndef wasm_builtin_module_h |
| 20 |
#define wasm_builtin_module_h |
| 21 |
|
| 22 |
#include "mozilla/EnumeratedArray.h" |
| 23 |
#include "mozilla/Maybe.h" |
| 24 |
#include "mozilla/Span.h" |
| 25 |
|
| 26 |
#include "wasm/WasmBuiltins.h" |
| 27 |
#include "wasm/WasmCompileArgs.h" |
| 28 |
#include "wasm/WasmConstants.h" |
| 29 |
#include "wasm/WasmSerialize.h" |
| 30 |
#include "wasm/WasmTypeDecls.h" |
| 31 |
#include "wasm/WasmTypeDef.h" |
| 32 |
|
| 33 |
namespace js { |
| 34 |
namespace wasm { |
| 35 |
|
| 36 |
struct MOZ_STACK_CLASS BuiltinModuleInstances { |
| 37 |
explicit BuiltinModuleInstances(JSContext* cx) |
| 38 |
: selfTest(cx), intGemm(cx), jsString(cx) {} |
| 39 |
|
| 40 |
Rooted<JSObject*> selfTest; |
| 41 |
Rooted<JSObject*> intGemm; |
| 42 |
Rooted<JSObject*> jsString; |
| 43 |
|
| 44 |
MutableHandle<JSObject*> operator[](BuiltinModuleId module) { |
| 45 |
switch (module) { |
| 46 |
case BuiltinModuleId::SelfTest: { |
| 47 |
return &selfTest; |
| 48 |
} |
| 49 |
case BuiltinModuleId::IntGemm: { |
| 50 |
return &intGemm; |
| 51 |
} |
| 52 |
case BuiltinModuleId::JSString: { |
| 53 |
return &jsString; |
| 54 |
} |
| 55 |
default: { |
| 56 |
MOZ_CRASH(); |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
}; |
| 61 |
|
| 62 |
// An builtin module func is a natively implemented function that may be |
| 63 |
// compiled into a 'builtin module', which may be instantiated with a provided |
| 64 |
// memory yielding an exported WebAssembly function wrapping the builtin module. |
| 65 |
class BuiltinModuleFunc { |
| 66 |
private: |
| 67 |
SharedRecGroup recGroup_; |
| 68 |
const char* exportName_; |
| 69 |
const SymbolicAddressSignature* sig_; |
| 70 |
bool usesMemory_; |
| 71 |
BuiltinInlineOp inlineOp_; |
| 72 |
|
| 73 |
public: |
| 74 |
// Default constructor so this can be used in an EnumeratedArray. |
| 75 |
BuiltinModuleFunc() = default; |
| 76 |
|
| 77 |
// Initialize this builtin. Must only be called once. |
| 78 |
[[nodiscard]] bool init(const RefPtr<TypeContext>& types, |
| 79 |
mozilla::Span<const ValType> params, |
| 80 |
mozilla::Maybe<ValType> result, bool usesMemory, |
| 81 |
const SymbolicAddressSignature* sig, |
| 82 |
BuiltinInlineOp inlineOp, const char* exportName); |
| 83 |
|
| 84 |
// The rec group for the function type for this builtin. |
| 85 |
const RecGroup* recGroup() const { return recGroup_.get(); } |
| 86 |
// The type definition for the function type for this builtin. |
| 87 |
const TypeDef* typeDef() const { return &recGroup_->type(0); } |
| 88 |
// The function type for this builtin. |
| 89 |
const FuncType* funcType() const { return &typeDef()->funcType(); } |
| 90 |
|
| 91 |
// The name of the func as it is exported |
| 92 |
const char* exportName() const { return exportName_; } |
| 93 |
// The signature of the builtin that implements this function. |
| 94 |
const SymbolicAddressSignature* sig() const { return sig_; } |
| 95 |
// Whether this function takes a pointer to the memory base as a hidden final |
| 96 |
// parameter. This parameter will show up in the SymbolicAddressSignature, |
| 97 |
// but not the function type. Compilers must pass the memoryBase to the |
| 98 |
// function call as the last parameter. |
| 99 |
bool usesMemory() const { return usesMemory_; } |
| 100 |
// An optional inline operation that can be used for this function instead of |
| 101 |
// calling `sig`. |
| 102 |
BuiltinInlineOp inlineOp() const { return inlineOp_; } |
| 103 |
}; |
| 104 |
|
| 105 |
// Static storage for all builtin module funcs in the system. |
| 106 |
class BuiltinModuleFuncs { |
| 107 |
using Storage = |
| 108 |
mozilla::EnumeratedArray<BuiltinModuleFuncId, BuiltinModuleFunc, |
| 109 |
size_t(BuiltinModuleFuncId::Limit)>; |
| 110 |
Storage funcs_; |
| 111 |
|
| 112 |
static BuiltinModuleFuncs* singleton_; |
| 113 |
|
| 114 |
public: |
| 115 |
[[nodiscard]] static bool init(); |
| 116 |
static void destroy(); |
| 117 |
|
| 118 |
// Get the BuiltinModuleFunc for an BuiltinModuleFuncId. BuiltinModuleFuncId |
| 119 |
// must be validated. |
| 120 |
static const BuiltinModuleFunc& getFromId(BuiltinModuleFuncId id) { |
| 121 |
return singleton_->funcs_[id]; |
| 122 |
} |
| 123 |
}; |
| 124 |
|
| 125 |
mozilla::Maybe<BuiltinModuleId> ImportMatchesBuiltinModule( |
| 126 |
mozilla::Span<const char> importName, BuiltinModuleIds enabledBuiltins); |
| 127 |
bool ImportMatchesBuiltinModuleFunc(mozilla::Span<const char> importName, |
| 128 |
BuiltinModuleId module, |
| 129 |
const BuiltinModuleFunc** matchedFunc, |
| 130 |
BuiltinModuleFuncId* matchedFuncId); |
| 131 |
|
| 132 |
// Compile and return the builtin module for a particular |
| 133 |
// builtin module. |
| 134 |
[[nodiscard]] bool CompileBuiltinModule( |
| 135 |
JSContext* cx, BuiltinModuleId module, |
| 136 |
MutableHandle<WasmModuleObject*> result); |
| 137 |
|
| 138 |
// Compile, instantiate and return the builtin module instance for a particular |
| 139 |
// builtin module. |
| 140 |
[[nodiscard]] bool InstantiateBuiltinModule(JSContext* cx, |
| 141 |
BuiltinModuleId module, |
| 142 |
MutableHandle<JSObject*> result); |
| 143 |
|
| 144 |
} // namespace wasm |
| 145 |
} // namespace js |
| 146 |
|
| 147 |
#endif // wasm_builtin_module_h |