Reduct  v4.1.3-1-gd06c383
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/module.h>
7#include <reduct/optimize.h>
8
9#include <assert.h>
10#include <stdlib.h>
11
12struct reduct;
13struct reduct_item;
14struct reduct_atom;
15
16/**
17 * @file function.h
18 * @brief Compiled function
19 * @defgroup function Reduct function
20 *
21 * A reduct function is a sequence of instructions and an associated constant pool that can be executed by the Reduct
22 * virtual machine.
23 *
24 * ## Constants Template
25 *
26 * A function's constant template is split into two contiguous regions and describe how a closures constant pool should
27 * be created.
28 *
29 * The inital `captureCount` number of constant slots are for captured variables and are specified in the same order as
30 * the RVSDG lambda nodes inputs. These are not actually stored physically within the function but instead will be
31 * created as empty constants within created closures which are then filled using capture opcodes.
32 *
33 * After the captured constants are a `constantCount` number of static constants which will be copied into the closure's
34 * constant pool.
35 *
36 * @{
37 */
38
39/**
40 * @brief Function flags.
41 * @typedef reduct_function_flags_t
42 */
43typedef enum reduct_function_flags
44{
46 REDUCT_FUNCTION_FLAG_VARIADIC = 1 << 0, ///< Function accepts variadic arguments.
47 REDUCT_FUNCTION_FLAG_OPTIMIZED = 1 << 1, ///< Function has been optimized.
49
50/**
51 * @brief Source position of a parallel instruction.
52 * @struct reduct_function_inst_source_t
53 */
54typedef struct
55{
56 uint32_t modulePos; ///< The index within the modules buffer that created the parallel instruction.
57 reduct_module_id_t moduleId; ///< The ID of the module that the parallel instruction is associated with.
59
60/**
61 * @brief Compiled function structure.
62 * @struct reduct_function_t
63 */
64typedef struct reduct_function
65{
66 uint32_t instCount; ///< Number of instructions.
67 uint32_t instCapacity; ///< Capacity of the instruction array.
68 reduct_inst_t* insts; ///< An array of instructions.
69 reduct_function_inst_source_t* sources; ///< An array specifying the source of instructions.
70 reduct_handle_t* constants; ///< Static constant values. Slot `i` is `captureCount + i`.
71 uint16_t constantCount; ///< Number of static constants.
72 uint16_t constantCapacity; ///< Capacity of the constants array.
73 uint16_t captureCount; ///< Number of captured-variable slots.
74 uint16_t registerCount; ///< The number of registers the function uses.
75 uint8_t arity; ///< The number of arguments the function expects.
76 reduct_function_flags_t flags; ///< The function flags.
78
79/**
80 * @brief Initialize a function structure.
81 *
82 * @param func The function to initialize.
83 */
85
86/**
87 * @brief Create a new function.
88 *
89 * @param reduct Pointer to the Reduct structure.
90 * @return A pointer to the newly allocated function.
91 */
93
94/**
95 * @brief Grow the instruction buffer.
96 *
97 * @param reduct Pointer to the Reduct structure.
98 * @param func The function to grow.
99 */
100REDUCT_API void reduct_function_grow(struct reduct* reduct, reduct_function_t* func);
101
102/**
103 * @brief Emit an instruction to the function.
104 *
105 * @param reduct Pointer to the Reduct structure.
106 * @param func The function to emit to.
107 * @param inst The instruction to emit.
108 * @param modulePos The index within the modules buffer that created the parallel instruction.
109 * @param moduleId The ID of the module that the parallel instruction is associated with.
110 */
111static inline void reduct_function_emit(struct reduct* reduct, reduct_function_t* func, reduct_inst_t inst,
112 uint32_t modulePos, reduct_module_id_t moduleId)
113{
114 assert(reduct != NULL);
115 assert(func != NULL);
116 if (func->instCount >= func->instCapacity)
117 {
118 reduct_function_grow(reduct, func);
119 }
120 func->sources[func->instCount].modulePos = modulePos;
121 func->sources[func->instCount].moduleId = moduleId;
122 func->insts[func->instCount++] = inst;
123}
124
125/**
126 * @brief Set the number of captured variables for a function.
127 *
128 * @param reduct Pointer to the Reduct structure.
129 * @param func The function to modify.
130 * @param captureCount The new number of captured variables.
131 */
133 uint16_t captureCount);
134
135/**
136 * @brief Add a static constant to the function's template, returning its index.
137 *
138 * Will return the index of an existing identical constant if found.
139 *
140 * @param reduct Pointer to the Reduct structure.
141 * @param func The function.
142 * @param handle The value to add.
143 * @return The index in the constant pool.
144 */
146 reduct_handle_t handle);
147
148/**
149 * @brief Retain a function, preventing it from being collected by the garbage collector.
150 *
151 * @param reduct Pointer to the Reduct structure.
152 * @param function Pointer to the function, can be `NULL`.
153 */
154REDUCT_API void reduct_function_retain(struct reduct* reduct, reduct_function_t* function);
155
156/**
157 * @brief Release a function, potentially allowing the garbage collector to collect it.
158 *
159 * @param reduct Pointer to the Reduct structure.
160 * @param function Pointer to the function, can be `NULL`.
161 */
162REDUCT_API void reduct_function_release(struct reduct* reduct, reduct_function_t* function);
163
164/** @} */
165
166#endif
#define REDUCT_API
Definition defs.h:24
static void reduct_function_emit(struct reduct *reduct, reduct_function_t *func, reduct_inst_t inst, uint32_t modulePos, reduct_module_id_t moduleId)
Emit an instruction to the function.
Definition function.h:111
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 void reduct_function_retain(struct reduct *reduct, reduct_function_t *function)
Retain a function, preventing it from being collected by the garbage collector.
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:44
REDUCT_API void reduct_function_grow(struct reduct *reduct, reduct_function_t *func)
Grow the instruction buffer.
REDUCT_API void reduct_function_set_capture_count(struct reduct *reduct, reduct_function_t *func, uint16_t captureCount)
Set the number of captured variables for a function.
REDUCT_API void reduct_function_init(reduct_function_t *func)
Initialize a function structure.
@ REDUCT_FUNCTION_FLAG_OPTIMIZED
Function has been optimized.
Definition function.h:47
@ REDUCT_FUNCTION_FLAG_VARIADIC
Function accepts variadic arguments.
Definition function.h:46
@ REDUCT_FUNCTION_FLAG_NONE
Definition function.h:45
uint16_t reduct_const_t
Constant index type.
Definition inst.h:36
uint32_t reduct_inst_t
Instruction type.
Definition inst.h:57
uint16_t reduct_module_id_t
Identifies a reduct_module_t within a Reduct structure.
Definition module.h:36
Bytecode instruction format.
Module system.
Bytecode optimization.
Source position of a parallel instruction.
Definition function.h:55
uint32_t modulePos
The index within the modules buffer that created the parallel instruction.
Definition function.h:56
reduct_module_id_t moduleId
The ID of the module that the parallel instruction is associated with.
Definition function.h:57
Compiled function structure.
Definition function.h:65
reduct_inst_t * insts
An array of instructions.
Definition function.h:68
uint16_t captureCount
Number of captured-variable slots.
Definition function.h:73
reduct_function_inst_source_t * sources
An array specifying the source of instructions.
Definition function.h:69
uint32_t instCount
Number of instructions.
Definition function.h:66
uint16_t registerCount
The number of registers the function uses.
Definition function.h:74
uint8_t arity
The number of arguments the function expects.
Definition function.h:75
uint16_t constantCapacity
Capacity of the constants array.
Definition function.h:72
uint16_t constantCount
Number of static constants.
Definition function.h:71
reduct_handle_t * constants
Static constant values. Slot i is captureCount + i.
Definition function.h:70
uint32_t instCapacity
Capacity of the instruction array.
Definition function.h:67
reduct_function_flags_t flags
The function flags.
Definition function.h:76
Handle type.
Definition defs.h:121