Reduct  v1.0.4-3-gdaf0d70
A functional and immutable language.
Loading...
Searching...
No Matches
function.h
Go to the documentation of this file.
1#include "defs.h"
2#ifndef REDUCT_FUNCTION_H
3#define REDUCT_FUNCTION_H 1
4
5struct reduct;
6struct reduct_item;
7struct reduct_atom;
8
9#include "inst.h"
10
11/**
12 * @file function.h
13 * @brief Compiled function
14 * @defgroup function Reduct function
15 *
16 * A reduct function is a sequence of instructions and an associated constant pool that can be executed by the Reduct
17 * virtual machine.
18 *
19 * ## Constants Template
20 *
21 * A function's constant pool is actually a template of constant slots. These slots can either
22 * contain ann item or a variable name that needs to be captured from the enclosing scope when a closure is created.
23 *
24 * @{
25 */
26
27/**
28 * @brief Constant slot type.
29 * @typedef reduct_const_slot_type_t
30 */
31typedef enum
32{
33 REDUCT_CONST_SLOT_NONE, ///< No constant slot.
34 REDUCT_CONST_SLOT_ITEM, ///< A constant slot containing an item.
35 REDUCT_CONST_SLOT_CAPTURE, ///< A constant slot containing a variable name to be captured.
37
38/**
39 * @brief Constant slot.
40 * @struct reduct_const_slot_t
41 */
42typedef struct reduct_const_slot
43{
44 reduct_const_slot_type_t type; ///< The type of the constant slot.
45 union {
47 struct reduct_item* item; ///< The item contained in the constant slot.
48 struct reduct_atom* capture; ///< The name of the variable to be captured.
49 };
51
52/**
53 * @brief Create a constant slot containing an item.
54 *
55 * @param _item The item to wrap in a slot.
56 */
57#define REDUCT_CONST_SLOT_ITEM(_item) ((reduct_const_slot_t){.type = REDUCT_CONST_SLOT_ITEM, .item = (_item)})
58
59/**
60 * @brief Create a constant slot containing a variable name to be captured.
61 *
62 * @param _capture The name of the variable to be captured.
63 */
64#define REDUCT_CONST_SLOT_CAPTURE(_capture) \
65 ((reduct_const_slot_t){.type = REDUCT_CONST_SLOT_CAPTURE, .capture = (_capture)})
66
67/**
68 * @brief Constant index type.
69 * @typedef reduct_const_t
70 */
72
73/**
74 * @brief Compiled function structure.
75 * @struct reduct_function_t
76 */
77typedef struct reduct_function
78{
79 reduct_uint32_t instCount; ///< Number of instructions.
80 reduct_uint32_t instCapacity; ///< Capacity of the instruction array.
81 reduct_inst_t* insts; ///< An array of instructions.
82 reduct_uint32_t* positions; ///< An array of source positions parallel to the instructions.
83 reduct_const_slot_t* constants; ///< The array of constant slots forming the constant template.
84 reduct_uint16_t constantCount; ///< Number of constants.
85 reduct_uint16_t constantCapacity; ///< Capacity of the constant array.
86 reduct_uint16_t registerCount; ///< The number of registers the function uses.
87 reduct_uint8_t arity; ///< The number of arguments the function expects.
89
90/**
91 * @brief Initialize a function structure.
92 *
93 * @param func The function to initialize.
94 */
96
97/**
98 * @brief Deinitialize a function structure.
99 *
100 * @param func The function to deinitialize.
101 */
103
104/**
105 * @brief Create a new function.
106 *
107 * @param reduct The Reduct structure.
108 * @return A pointer to the newly allocated function.
109 */
111
112/**
113 * @brief Grow the instruction buffer.
114 *
115 * @param reduct The Reduct structure.
116 * @param func The function to grow.
117 */
118REDUCT_API void reduct_function_grow(struct reduct* reduct, reduct_function_t* func);
119
120/**
121 * @brief Emit an instruction to the function.
122 *
123 * @param reduct The Reduct structure.
124 * @param func The function to emit to.
125 * @param inst The instruction to emit.
126 * @param position The position in the source code.
127 */
128static inline void reduct_function_emit(struct reduct* reduct, reduct_function_t* func, reduct_inst_t inst,
129 reduct_uint32_t position)
130{
131 REDUCT_ASSERT(reduct != REDUCT_NULL);
132 REDUCT_ASSERT(func != REDUCT_NULL);
133 if (func->instCount >= func->instCapacity)
134 {
135 reduct_function_grow(reduct, func);
136 }
137 func->positions[func->instCount] = position;
138 func->insts[func->instCount++] = inst;
139}
140
141/**
142 * @brief Get the index of a constant in a function's constant template, adding it if it doesn't exist.
143 *
144 * @param reduct The Reduct structure.
145 * @param func The function.
146 * @param slot The constant slot to add or lookup.
147 * @return The index of the constant in the constant template.
148 */
150 reduct_const_slot_t* slot);
151
152/** @} */
153
154#endif
uint32_t reduct_uint32_t
Definition defs.h:95
#define REDUCT_ASSERT(_cond)
Definition defs.h:25
uint8_t reduct_uint8_t
Definition defs.h:99
#define REDUCT_API
Definition defs.h:7
uint16_t reduct_uint16_t
Definition defs.h:97
uint64_t reduct_uint64_t
Definition defs.h:93
#define REDUCT_NULL
Definition defs.h:23
#define REDUCT_CONST_SLOT_CAPTURE(_capture)
Create a constant slot containing a variable name to be captured.
Definition function.h:64
reduct_uint16_t reduct_const_t
Constant index type.
Definition function.h:71
REDUCT_API reduct_function_t * reduct_function_new(struct reduct *reduct)
Create a new function.
static void reduct_function_emit(struct reduct *reduct, reduct_function_t *func, reduct_inst_t inst, reduct_uint32_t position)
Emit an instruction to the function.
Definition function.h:128
REDUCT_API void reduct_function_grow(struct reduct *reduct, reduct_function_t *func)
Grow the instruction buffer.
#define REDUCT_CONST_SLOT_ITEM(_item)
Create a constant slot containing an item.
Definition function.h:57
REDUCT_API void reduct_function_init(reduct_function_t *func)
Initialize a function structure.
REDUCT_API reduct_const_t reduct_function_lookup_constant(struct reduct *reduct, reduct_function_t *func, reduct_const_slot_t *slot)
Get the index of a constant in a function's constant template, adding it if it doesn't exist.
REDUCT_API void reduct_function_deinit(reduct_function_t *func)
Deinitialize a function structure.
reduct_const_slot_type_t
Constant slot type.
Definition function.h:32
@ REDUCT_CONST_SLOT_NONE
No constant slot.
Definition function.h:33
reduct_uint32_t reduct_inst_t
Instruction type.
Definition inst.h:97
Bytecode instruction format.
Constant slot.
Definition function.h:43
reduct_const_slot_type_t type
The type of the constant slot.
Definition function.h:44
struct reduct_item * item
The item contained in the constant slot.
Definition function.h:47
reduct_uint64_t raw
Definition function.h:46
struct reduct_atom * capture
The name of the variable to be captured.
Definition function.h:48
Compiled function structure.
Definition function.h:78
reduct_inst_t * insts
An array of instructions.
Definition function.h:81
reduct_const_slot_t * constants
The array of constant slots forming the constant template.
Definition function.h:83
reduct_uint16_t constantCapacity
Capacity of the constant array.
Definition function.h:85
reduct_uint32_t instCapacity
Capacity of the instruction array.
Definition function.h:80
reduct_uint32_t instCount
Number of instructions.
Definition function.h:79
reduct_uint32_t * positions
An array of source positions parallel to the instructions.
Definition function.h:82
reduct_uint16_t registerCount
The number of registers the function uses.
Definition function.h:86
reduct_uint16_t constantCount
Number of constants.
Definition function.h:84
reduct_uint8_t arity
The number of arguments the function expects.
Definition function.h:87