Reduct  v4.0.5-1-g4851deb
A functional and immutable language.
Loading...
Searching...
No Matches
function.h
Go to the documentation of this file.
1#ifndef REDUCT_FUNCTION_H
2#define REDUCT_FUNCTION_H 1
3
4#include <reduct/defs.h>
5#include <reduct/inst.h>
6#include <reduct/optimize.h>
7
8#include <assert.h>
9#include <stdlib.h>
10
11struct reduct;
12struct reduct_item;
13struct reduct_atom;
14
15/**
16 * @file function.h
17 * @brief Compiled function
18 * @defgroup function Reduct function
19 *
20 * A reduct function is a sequence of instructions and an associated constant pool that can be executed by the Reduct
21 * virtual machine.
22 *
23 * ## Constants Template
24 *
25 * A function's constant pool is actually a template of constant slots. These slots can either
26 * contain ann item or a variable name that needs to be captured from the enclosing scope when a closure is created.
27 *
28 * @{
29 */
30
31/**
32 * @brief Constant slot type.
33 * @typedef reduct_const_slot_type_t
34 */
35typedef enum
36{
37 REDUCT_CONST_SLOT_TYPE_NONE, ///< No constant slot.
38 REDUCT_CONST_SLOT_TYPE_STATIC, ///< A constant slot containing a static value.
39 REDUCT_CONST_SLOT_TYPE_CAPTURE, ///< A constant slot acting as a placeholder for a capture.
41
42/**
43 * @brief Constant slot.
44 * @struct reduct_const_slot_t
45 */
46typedef struct reduct_const_slot
47{
48 reduct_const_slot_type_t type; ///< The type of the constant slot.
49 reduct_handle_t handle; ///< The static handle, or `NIL` for captures.
51
52/**
53 * @brief Function flags.
54 * @typedef reduct_function_flags_t
55 */
56typedef enum reduct_function_flags
57{
59 REDUCT_FUNCTION_FLAG_VARIADIC = 1 << 0, ///< Function accepts variadic arguments.
60 REDUCT_FUNCTION_FLAG_OPTIMIZED = 1 << 1, ///< Function has been optimized.
62
63/**
64 * @brief Compiled function structure.
65 * @struct reduct_function_t
66 */
67typedef struct reduct_function
68{
69 uint32_t instCount; ///< Number of instructions.
70 uint32_t instCapacity; ///< Capacity of the instruction array.
71 reduct_inst_t* insts; ///< An array of instructions.
72 uint32_t* positions; ///< An array of source positions parallel to the instructions.
73 reduct_const_slot_t* constants; ///< The array of constant slots forming the constant template.
74 uint16_t constantCount; ///< Number of constants.
75 uint16_t constantCapacity; ///< Capacity of the constant array.
76 uint16_t registerCount; ///< The number of registers the function uses.
77 uint8_t arity; ///< The number of arguments the function expects.
78 reduct_function_flags_t flags; ///< The function flags.
80
81/**
82 * @brief Initialize a function structure.
83 *
84 * @param func The function to initialize.
85 */
87
88/**
89 * @brief Create a new function.
90 *
91 * @param reduct Pointer to the Reduct structure.
92 * @return A pointer to the newly allocated function.
93 */
95
96/**
97 * @brief Grow the instruction buffer.
98 *
99 * @param reduct Pointer to the Reduct structure.
100 * @param func The function to grow.
101 */
102REDUCT_API void reduct_function_grow(struct reduct* reduct, reduct_function_t* func);
103
104/**
105 * @brief Emit an instruction to the function.
106 *
107 * @param reduct Pointer to the Reduct structure.
108 * @param func The function to emit to.
109 * @param inst The instruction to emit.
110 * @param position The position in the source code.
111 */
112static inline void reduct_function_emit(struct reduct* reduct, reduct_function_t* func, reduct_inst_t inst,
113 uint32_t position)
114{
115 assert(reduct != NULL);
116 assert(func != NULL);
117 if (func->instCount >= func->instCapacity)
118 {
119 reduct_function_grow(reduct, func);
120 }
121 func->positions[func->instCount] = position;
122 func->insts[func->instCount++] = inst;
123}
124
125/**
126 * @brief Add a static constant to the function's template, returning its index.
127 *
128 * Will return the index of an existing identical constant if found.
129 *
130 * @param reduct Pointer to the Reduct structure.
131 * @param func The function.
132 * @param handle The value to add.
133 * @return The index in the constant pool.
134 */
136 reduct_handle_t handle);
137
138/**
139 * @brief Add a capture placeholder slot to the function's template, returning its index.
140 *
141 * @param reduct Pointer to the Reduct structure.
142 * @param func The function.
143 * @return The index in the constant pool.
144 */
146
147/**
148 * @brief Retain a function, preventing it from being collected by the garbage collector.
149 *
150 * @param reduct Pointer to the Reduct structure.
151 * @param function Pointer to the function.
152 */
153REDUCT_API void reduct_function_retain(struct reduct* reduct, reduct_function_t* function);
154
155/**
156 * @brief Release a function, potentially allowing the garbage collector to collect it.
157 *
158 * @param reduct Pointer to the Reduct structure.
159 * @param function Pointer to the function.
160 */
161REDUCT_API void reduct_function_release(struct reduct* reduct, reduct_function_t* function);
162
163/** @} */
164
165#endif
#define REDUCT_API
Definition defs.h:24
REDUCT_API reduct_const_t reduct_function_add_constant(struct reduct *reduct, reduct_function_t *func, reduct_handle_t handle)
Add a static constant to the function's template, returning its index.
REDUCT_API reduct_function_t * reduct_function_new(struct reduct *reduct)
Create a new function.
REDUCT_API reduct_const_t reduct_function_add_capture(struct reduct *reduct, reduct_function_t *func)
Add a capture placeholder slot to the function's template, returning its index.
REDUCT_API void reduct_function_retain(struct reduct *reduct, reduct_function_t *function)
Retain a function, preventing it from being collected by the garbage collector.
static void reduct_function_emit(struct reduct *reduct, reduct_function_t *func, reduct_inst_t inst, uint32_t position)
Emit an instruction to the function.
Definition function.h:112
REDUCT_API void reduct_function_release(struct reduct *reduct, reduct_function_t *function)
Release a function, potentially allowing the garbage collector to collect it.
reduct_function_flags_t
Function flags.
Definition function.h:57
REDUCT_API void reduct_function_grow(struct reduct *reduct, reduct_function_t *func)
Grow the instruction buffer.
REDUCT_API void reduct_function_init(reduct_function_t *func)
Initialize a function structure.
reduct_const_slot_type_t
Constant slot type.
Definition function.h:36
@ REDUCT_FUNCTION_FLAG_OPTIMIZED
Function has been optimized.
Definition function.h:60
@ REDUCT_FUNCTION_FLAG_VARIADIC
Function accepts variadic arguments.
Definition function.h:59
@ REDUCT_FUNCTION_FLAG_NONE
Definition function.h:58
@ REDUCT_CONST_SLOT_TYPE_STATIC
A constant slot containing a static value.
Definition function.h:38
@ REDUCT_CONST_SLOT_TYPE_NONE
No constant slot.
Definition function.h:37
@ REDUCT_CONST_SLOT_TYPE_CAPTURE
A constant slot acting as a placeholder for a capture.
Definition function.h:39
uint16_t reduct_const_t
Constant index type.
Definition inst.h:36
uint32_t reduct_inst_t
Instruction type.
Definition inst.h:52
Bytecode instruction format.
Bytecode optimization.
Constant slot.
Definition function.h:47
reduct_handle_t handle
The static handle, or NIL for captures.
Definition function.h:49
reduct_const_slot_type_t type
The type of the constant slot.
Definition function.h:48
Compiled function structure.
Definition function.h:68
reduct_inst_t * insts
An array of instructions.
Definition function.h:71
reduct_const_slot_t * constants
The array of constant slots forming the constant template.
Definition function.h:73
uint32_t * positions
An array of source positions parallel to the instructions.
Definition function.h:72
uint32_t instCount
Number of instructions.
Definition function.h:69
uint16_t registerCount
The number of registers the function uses.
Definition function.h:76
uint8_t arity
The number of arguments the function expects.
Definition function.h:77
uint16_t constantCapacity
Capacity of the constant array.
Definition function.h:75
uint16_t constantCount
Number of constants.
Definition function.h:74
uint32_t instCapacity
Capacity of the instruction array.
Definition function.h:70
reduct_function_flags_t flags
The function flags.
Definition function.h:78
Handle type.
Definition defs.h:119