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:
4 * Copyright 2021 Mozilla Foundation
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #ifndef wasm_builtin_module_h
20 #define wasm_builtin_module_h
22 #include "mozilla/EnumeratedArray.h"
23 #include "mozilla/Maybe.h"
24 #include "mozilla/Span.h"
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"
36 struct MOZ_STACK_CLASS BuiltinModuleInstances
{
37 explicit BuiltinModuleInstances(JSContext
* cx
)
38 : selfTest(cx
), intGemm(cx
), jsString(cx
) {}
40 Rooted
<JSObject
*> selfTest
;
41 Rooted
<JSObject
*> intGemm
;
42 Rooted
<JSObject
*> jsString
;
44 MutableHandle
<JSObject
*> operator[](BuiltinModuleId module
) {
46 case BuiltinModuleId::SelfTest
: {
49 case BuiltinModuleId::IntGemm
: {
52 case BuiltinModuleId::JSString
: {
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
{
67 SharedRecGroup recGroup_
;
68 const char* exportName_
;
69 const SymbolicAddressSignature
* sig_
;
73 // Default constructor so this can be used in an EnumeratedArray.
74 BuiltinModuleFunc() = default;
76 // Initialize this builtin. Must only be called once.
77 [[nodiscard
]] bool init(const RefPtr
<TypeContext
>& types
,
78 mozilla::Span
<const ValType
> params
,
79 Maybe
<ValType
> result
, bool usesMemory
,
80 const SymbolicAddressSignature
* sig
,
81 const char* exportName
);
83 // The rec group for the function type for this builtin.
84 const RecGroup
* recGroup() const { return recGroup_
.get(); }
85 // The type definition for the function type for this builtin.
86 const TypeDef
* typeDef() const { return &recGroup_
->type(0); }
87 // The function type for this builtin.
88 const FuncType
* funcType() const { return &typeDef()->funcType(); }
90 // The name of the func as it is exported
91 const char* exportName() const { return exportName_
; }
92 // The signature of the builtin that implements this function.
93 const SymbolicAddressSignature
* sig() const { return sig_
; }
94 // Whether this function takes a pointer to the memory base as a hidden final
95 // parameter. This parameter will show up in the SymbolicAddressSignature,
96 // but not the function type. Compilers must pass the memoryBase to the
97 // function call as the last parameter.
98 bool usesMemory() const { return usesMemory_
; }
101 // Static storage for all builtin module funcs in the system.
102 class BuiltinModuleFuncs
{
104 mozilla::EnumeratedArray
<BuiltinModuleFuncId
, BuiltinModuleFunc
,
105 size_t(BuiltinModuleFuncId::Limit
)>;
108 static BuiltinModuleFuncs
* singleton_
;
111 [[nodiscard
]] static bool init();
112 static void destroy();
114 // Get the BuiltinModuleFunc for an BuiltinModuleFuncId. BuiltinModuleFuncId
115 // must be validated.
116 static const BuiltinModuleFunc
& getFromId(BuiltinModuleFuncId id
) {
117 return singleton_
->funcs_
[id
];
121 Maybe
<BuiltinModuleId
> ImportMatchesBuiltinModule(
122 mozilla::Span
<const char> importName
, BuiltinModuleIds enabledBuiltins
);
123 Maybe
<const BuiltinModuleFunc
*> ImportMatchesBuiltinModuleFunc(
124 mozilla::Span
<const char> importName
, BuiltinModuleId module
);
126 // Compile and return the builtin module for a particular
128 bool CompileBuiltinModule(JSContext
* cx
, BuiltinModuleId module
,
129 MutableHandle
<WasmModuleObject
*> result
);
131 // Compile, instantiate and return the builtin module instance for a particular
133 bool InstantiateBuiltinModule(JSContext
* cx
, BuiltinModuleId module
,
134 MutableHandle
<JSObject
*> result
);
139 #endif // wasm_builtin_module_h