-
Notifications
You must be signed in to change notification settings - Fork 1
/
wasm-rt-impl.h
113 lines (92 loc) · 3 KB
/
wasm-rt-impl.h
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
/*
* Copyright 2018 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.
*/
#ifndef WASM_RT_IMPL_H_
#define WASM_RT_IMPL_H_
#include <setjmp.h>
#include <stdint.h>
#include "wasm-rt.h"
#ifdef __cplusplus
extern "C" {
#endif
/** A setjmp buffer used for handling traps. */
extern jmp_buf g_jmp_buf;
/** Convenience macro to use before calling a wasm function. On first execution
* it will return `WASM_RT_TRAP_NONE` (i.e. 0). If the function traps, it will
* jump back and return the trap that occurred.
*
* ```
* wasm_rt_trap_t code = wasm_rt_impl_try();
* if (code != 0) {
* printf("A trap occurred with code: %d\n", code);
* ...
* }
*
* // Call the potentially-trapping function.
* my_wasm_func();
* ```
*/
#define wasm_rt_impl_try() setjmp(g_jmp_buf)
typedef uint8_t u8;
typedef int8_t s8;
typedef uint16_t u16;
typedef int16_t s16;
typedef uint32_t u32;
typedef int32_t s32;
typedef uint64_t u64;
typedef int64_t s64;
typedef float f32;
typedef double f64;
u32 wasm_rt_popcount_u32(u32 i);
u64 wasm_rt_popcount_u64(u64 i);
u32 wasm_rt_clz_u32(u32 i);
u64 wasm_rt_clz_u64(u64 i);
u32 wasm_rt_ctz_u32(u32 i);
u64 wasm_rt_ctz_u64(u64 i);
f64 wasm_rt_nearest_f64(f64 f);
f32 wasm_rt_zero_min_f32(f32 f1, f32 f2);
f32 wasm_rt_zero_max_f32(f32 f1, f32 f2);
f64 wasm_rt_zero_min_f64(f64 f1, f64 f2);
f64 wasm_rt_zero_max_f64(f64 f1, f64 f2);
// HACK: Need to load / store f32s via a C call, because
// OCaml converts to a double (and therefore is not bit-preserving)
// Both of these assume the memory check has been done.
// TODO:
// It would be nice to reconcile the store/load and set-global/get-global
// functions.
void wasm_rt_store_f32(wasm_rt_memory_t* mem, u64 offset, f32 to_store);
f32 wasm_rt_load_f32(wasm_rt_memory_t* mem, u64 offset);
f32 wasm_rt_get_global_f32(f32* ptr);
void wasm_rt_set_global_f32(f32* ptr, f32 to_set);
// Conversion functions
u32 wasm_rt_reinterpret_u32(f32 bits);
u64 wasm_rt_reinterpret_u64(f64 bits);
f32 wasm_rt_reinterpret_f32(u32 bits);
f64 wasm_rt_reinterpret_f64(u64 bits);
// OCaml lacks unsigned float<->integer conversions, so we have to
// have them in the RTS
u32 wasm_rt_trunc_u32_f32(f32 f);
u32 wasm_rt_trunc_u32_f64(f64 f);
u64 wasm_rt_trunc_u64_f32(f32 f);
u64 wasm_rt_trunc_u64_f64(f64 f);
f32 wasm_rt_convert_f32_u32(u32 f);
f32 wasm_rt_convert_f32_u64(u64 f);
f64 wasm_rt_convert_f64_u32(u32 f);
f64 wasm_rt_convert_f64_u64(u64 f);
f32 wasm_rt_neg_f32(f32 f);
#ifdef __cplusplus
}
#endif
#endif // WASM_RT_IMPL_H_