Reduct  v4.2.1-1-g0b59ae3
A functional and immutable language.
Loading...
Searching...
No Matches
eval.h
Go to the documentation of this file.
1#ifndef REDUCT_EVAL_H
2#define REDUCT_EVAL_H 1
3
4#include <reduct/function.h>
5#include <reduct/handle.h>
6#include <reduct/optimize.h>
7
8struct reduct_closure;
9
10/**
11 * @file eval.h
12 * @brief Virtual machine evaluation.
13 * @defgroup eval Evaluation
14 *
15 * The evaluator is a register-based virtual machine executing the bytecode produced by the emitter.
16 *
17 * @warning Calls are zero-copy such that the callee's frame is placed directly over the caller's argument registers, so
18 * arguments become the callee's starting registers, and tail calls reuse the current frame.
19 *
20 * @warning Registers and frames are held in arrays that are realloc'd as frames are pushed and popped. Any pointer into
21 * them, most notably the argv pointer passed to native functions, is only valid for the duration of the call that
22 * produced it. Native functions that call back into Reduct must copy the handles they need out of argv before doing so.
23 *
24 * @{
25 */
26
27#define REDUCT_EVAL_REGS_INITIAL 64 ///< The initial amount of registers.
28#define REDUCT_EVAL_REGS_GROWTH_FACTOR 2 ///< The growth factor of the registers array.
29
30#define REDUCT_EVAL_FRAMES_INITIAL 32 ///< The initial size of the frames array.
31#define REDUCT_EVAL_FRAMES_GROWTH_FACTOR 2 ///< The growth factor of the frames array.
32
33/**
34 * @brief Evaluation frame structure.
35 * @struct reduct_eval_frame_t
36 */
37typedef struct reduct_eval_frame
38{
39 struct reduct_closure* closure; ///< The closure being evaluated.
40 reduct_inst_t* ip; ///< The current instruction pointer.
41 reduct_handle_t* constants; ///< Cached pointer to closure constants.
42 uint32_t base; ///< The base register, where the functions registers start.
43 uint32_t prevRegCount; ///< The previous register count to restore upon return.
45
46/**
47 * @brief Per-thread eval-related state structure.
48 * @struct reduct_eval_local_t
49 */
50typedef struct reduct_eval_state
51{
52 struct reduct_eval_frame* frames;
53 size_t frameCount;
56 size_t regCount;
59
60/**
61 * @brief Initialize a local eval state.
62 *
63 * @param local Pointer to the local eval state to initialize.
64 */
66
67/**
68 * @brief Deinitialize a local eval state.
69 *
70 * @param local Pointer to the local eval state to deinitialize.
71 */
73
74/**
75 * @brief Evaluates a handle.
76 *
77 * If the handle is a compiled function then it will be interpreted, otherwise, it will first be compiled into a
78 * function.
79 *
80 * @warning May be called re-entrantly, including from within a native function. Doing so may grow the register and
81 * frame arrays, invalidating any pointers previously obtained into them.
82 *
83 * @param reduct The Reduct instance.
84 * @param handle The handle to evaluate.
85 * @return The result of the evaluation as a Reduct handle.
86 */
88
89/**
90 * @brief Parses, builds, optimizes, emits and evaluates a file.
91 *
92 * @warning May be called re-entrantly, including from within a native function. Doing so may grow the register and
93 * frame arrays, invalidating any pointers previously obtained into them.
94 *
95 * @param reduct The Reduct instance.
96 * @param path The path to the file.
97 * @param flags Optimization flags to control which optimizations are applied.
98 * @return The result of the evaluation.
99 */
100REDUCT_API reduct_handle_t reduct_eval_file(struct reduct* reduct, const char* path, reduct_optimize_flags_t optimize);
101
102/**
103 * @brief Parses, builds, optimizes, emits and evaluates a string.
104 *
105 * @warning May be called re-entrantly, including from within a native function. Doing so may grow the register and
106 * frame arrays, invalidating any pointers previously obtained into them.
107 *
108 * @param reduct The Reduct instance.
109 * @param str The string to evaluate.
110 * @param len The length of the string.
111 * @param flags Optimization flags to control which optimizations are applied.
112 * @return The result of the evaluation.
113 */
114REDUCT_API reduct_handle_t reduct_eval_string(struct reduct* reduct, const char* str, size_t len,
115 reduct_optimize_flags_t optimize);
116
117/**
118 * @brief Calls a Reduct callable (closure or native) with arguments.
119 *
120 * @warning May be called re-entrantly, including from within a native function. Doing so may grow the register and
121 * frame arrays, invalidating any pointers previously obtained into them.
122 *
123 * @param reduct The Reduct instance.
124 * @param callable The callable item handle.
125 * @param argc The number of arguments.
126 * @param argv Pointer to the arguments array.
127 * @return The result of the call.
128 */
129REDUCT_API reduct_handle_t reduct_eval_call(struct reduct* reduct, reduct_handle_t callable, size_t argc,
130 reduct_handle_t* argv);
131
132/**
133 * @brief Calls a Reduct callable (closure or native) with variadic arguments.
134 *
135 * @warning May be called re-entrantly, including from within a native function. Doing so may grow the register and
136 * frame arrays, invalidating any pointers previously obtained into them.
137 *
138 * @param reduct The Reduct instance.
139 * @param callable The callable item handle.
140 * @param argc The number of arguments.
141 * @param ... The arguments (as reduct_handle_t).
142 * @return The result of the call.
143 */
144REDUCT_API reduct_handle_t reduct_eval_call_v(struct reduct* reduct, reduct_handle_t callable, size_t argc, ...);
145
146/** @} */
147
148#endif
#define REDUCT_API
Definition defs.h:24
Compiled function.
REDUCT_API reduct_handle_t reduct_eval_string(struct reduct *reduct, const char *str, size_t len, reduct_optimize_flags_t optimize)
Parses, builds, optimizes, emits and evaluates a string.
REDUCT_API reduct_handle_t reduct_eval_file(struct reduct *reduct, const char *path, reduct_optimize_flags_t optimize)
Parses, builds, optimizes, emits and evaluates a file.
REDUCT_API reduct_handle_t reduct_eval_call_v(struct reduct *reduct, reduct_handle_t callable, size_t argc,...)
Calls a Reduct callable (closure or native) with variadic arguments.
REDUCT_API void reduct_eval_local_deinit(reduct_eval_local_t *local)
Deinitialize a local eval state.
REDUCT_API reduct_handle_t reduct_eval(struct reduct *reduct, reduct_handle_t handle)
Evaluates a handle.
REDUCT_API reduct_handle_t reduct_eval_call(struct reduct *reduct, reduct_handle_t callable, size_t argc, reduct_handle_t *argv)
Calls a Reduct callable (closure or native) with arguments.
REDUCT_API void reduct_eval_local_init(reduct_eval_local_t *local)
Initialize a local eval state.
uint32_t reduct_inst_t
Instruction type.
Definition inst.h:57
reduct_optimize_flags_t
Optimization flags.
Definition optimize.h:24
Handle management.
Bytecode optimization.
Evaluation frame structure.
Definition eval.h:38
uint32_t base
The base register, where the functions registers start.
Definition eval.h:42
struct reduct_closure * closure
The closure being evaluated.
Definition eval.h:39
uint32_t prevRegCount
The previous register count to restore upon return.
Definition eval.h:43
reduct_inst_t * ip
The current instruction pointer.
Definition eval.h:40
reduct_handle_t * constants
Cached pointer to closure constants.
Definition eval.h:41
Per-thread eval-related state structure.
Definition eval.h:51
size_t regCount
Definition eval.h:56
reduct_handle_t * regs
Definition eval.h:55
size_t regCapacity
Definition eval.h:57
size_t frameCapacity
Definition eval.h:54
struct reduct_eval_frame * frames
Definition eval.h:52
size_t frameCount
Definition eval.h:53
Handle type.
Definition defs.h:121