Reduct  v4.0.5-1-g4851deb
A functional and immutable language.
Loading...
Searching...
No Matches
native.h
Go to the documentation of this file.
1#ifndef REDUCT_NATIVE_H
2#define REDUCT_NATIVE_H 1
3
4#include <reduct/defs.h>
5#include <reduct/rvsdg.h>
6#include <reduct/sync.h>
7
8struct reduct;
9struct reduct_builder;
10struct reduct_item;
11struct reduct_expr;
12
13/**
14 * @file native.h
15 * @brief Native function and intrinsic registration.
16 * @defgroup native Native Functions and Intrinsics
17 *
18 * A "native" is a C function that can be called at runtime, each native may have an associated "intrinsic", which is a
19 * function that is called during compilation.
20 *
21 * Both are stored in a unified hash map keyed by name.
22 *
23 * @see intrinsic
24 *
25 * @{
26 */
27
28/**
29 * @brief Native function definition structure.
30 */
37
38#define REDUCT_NATIVE_MAP_INITIAL 256 ///< The initial size of the native map.
39#define REDUCT_NATIVE_MAP_GROWTH 2 ///< The growth factor of the native map.
40
41/**
42 * @brief Global native-related state structure.
43 * @struct reduct_native_global_t
44 */
45typedef struct
46{
47 struct reduct_native_entry* map;
48 size_t size;
49 size_t capacity;
50 size_t mask;
53
54/**
55 * @brief Initialize a global native state.
56 *
57 * @param global Pointer to the global native state to initialize.
58 */
60
61/**
62 * @brief Deinitialize a global native state.
63 *
64 * @param global Pointer to the global native state to deinitialize.
65 */
67
68/**
69 * @brief Native map entry.
70 */
71typedef struct reduct_native_entry
72{
73 uint32_t hash;
74 uint32_t length;
77 char* name;
79
80/**
81 * @brief Find a native entry in the map.
82 *
83 * @param reduct Pointer to the Reduct structure.
84 * @param hash The hash of the name.
85 * @param str The name string.
86 * @param len The length of the name.
87 * @return A pointer to the native entry, or `NULL` if not found.
88 */
89REDUCT_API reduct_native_entry_t* reduct_native_map_find(struct reduct* reduct, uint32_t hash, const char* str,
90 size_t len);
91
92/**
93 * @brief Register native functions.
94 *
95 * @param reduct Pointer to the Reduct structure.
96 * @param array An array of native function definitions.
97 * @param count The number of functions in the array.
98 */
99REDUCT_API void reduct_native_register(struct reduct* reduct, const reduct_native_t* array, size_t count);
100
101/** @} */
102
103#endif
reduct_handle_t(* reduct_native_fn)(struct reduct *reduct, size_t argc, reduct_handle_t *argv)
Native function pointer type.
Definition defs.h:126
struct reduct_rvsdg_origin *(* reduct_native_intrinsic_fn)(struct reduct_builder *builder, struct reduct_list *expr)
Intrinsic handler function type.
Definition defs.h:132
#define REDUCT_API
Definition defs.h:24
REDUCT_API void reduct_native_register(struct reduct *reduct, const reduct_native_t *array, size_t count)
Register native functions.
REDUCT_API reduct_native_entry_t * reduct_native_map_find(struct reduct *reduct, uint32_t hash, const char *str, size_t len)
Find a native entry in the map.
REDUCT_API void reduct_native_global_deinit(reduct_native_global_t *global)
Deinitialize a global native state.
REDUCT_API void reduct_native_global_init(reduct_native_global_t *global)
Initialize a global native state.
Intermediate Representation.
Native map entry.
Definition native.h:72
uint32_t length
Definition native.h:74
reduct_native_intrinsic_fn intrinsicFn
Definition native.h:76
uint32_t hash
Definition native.h:73
reduct_native_fn nativeFn
Definition native.h:75
char * name
Definition native.h:77
Global native-related state structure.
Definition native.h:46
struct reduct_native_entry * map
Definition native.h:47
reduct_rwmutex_t mutex
Definition native.h:51
Native function definition structure.
Definition native.h:32
reduct_native_intrinsic_fn intrinsicFn
Definition native.h:35
const char * name
Definition native.h:33
reduct_native_fn nativeFn
Definition native.h:34
Read-Write Mutex structure.
Definition sync.h:23
Syncronization primitives.