Reduct  v1.0.4-3-gdaf0d70
A functional and immutable language.
Loading...
Searching...
No Matches
function_impl.h
Go to the documentation of this file.
1#ifndef REDUCT_FUNCTION_IMPL_H
2#define REDUCT_FUNCTION_IMPL_H 1
3
4#include "core.h"
5#include "function.h"
6#include "gc.h"
7#include "handle.h"
8#include "item.h"
9
11{
13
14 func->insts = REDUCT_NULL;
15 func->positions = REDUCT_NULL;
16 func->constants = REDUCT_NULL;
17 func->instCount = 0;
18 func->instCapacity = 0;
19 func->constantCount = 0;
20 func->constantCapacity = 0;
21 func->registerCount = 0;
22 func->arity = 0;
23}
24
26{
28
29 if (func->insts != REDUCT_NULL)
30 {
31 REDUCT_FREE(func->insts);
32 }
33 if (func->positions != REDUCT_NULL)
34 {
36 }
37 if (func->constants != REDUCT_NULL)
38 {
40 }
41}
42
44{
45 REDUCT_ASSERT(reduct != REDUCT_NULL);
46
47 reduct_item_t* item = reduct_item_new(reduct);
49 reduct_function_t* func = &item->function;
51 return func;
52}
53
55{
56 REDUCT_ASSERT(reduct != REDUCT_NULL);
58
59 reduct_size_t newCapacity = func->instCapacity == 0 ? 16 : func->instCapacity * 2;
60 reduct_inst_t* newInsts = (reduct_inst_t*)REDUCT_REALLOC(func->insts, newCapacity * sizeof(reduct_inst_t));
61 reduct_uint32_t* newPositions =
62 (reduct_uint32_t*)REDUCT_REALLOC(func->positions, newCapacity * sizeof(reduct_uint32_t));
63
64 if (newInsts == REDUCT_NULL || newPositions == REDUCT_NULL)
65 {
66 REDUCT_ERROR_INTERNAL(reduct, "out of memory");
67 }
68
69 func->insts = newInsts;
70 func->positions = newPositions;
71 func->instCapacity = newCapacity;
72}
73
76{
77 REDUCT_ASSERT(reduct != REDUCT_NULL);
80
81 for (reduct_const_t i = 0; i < func->constantCount; i++)
82 {
83 if (func->constants[i].type == slot->type && func->constants[i].raw == slot->raw)
84 {
85 return i;
86 }
87 }
88
89 if (func->constantCount >= func->constantCapacity)
90 {
91 reduct_uint32_t newCapacity = func->constantCapacity == 0 ? 16 : func->constantCapacity * 2;
92 if (newCapacity > REDUCT_CONSTANT_MAX)
93 {
94 REDUCT_ERROR_RUNTIME(reduct, "too many constants in function");
95 }
96 reduct_const_slot_t* newConstants = REDUCT_REALLOC(func->constants, newCapacity * sizeof(reduct_const_slot_t));
97 if (newConstants == REDUCT_NULL)
98 {
99 REDUCT_ERROR_INTERNAL(reduct, "out of memory");
100 }
101 func->constants = newConstants;
102 func->constantCapacity = newCapacity;
103 }
104
105 func->constants[func->constantCount] = *slot;
106 return func->constantCount++;
107}
108
109#endif
Core definitions and structures.
size_t reduct_size_t
Definition defs.h:100
#define REDUCT_REALLOC(_ptr, _size)
Definition defs.h:29
uint32_t reduct_uint32_t
Definition defs.h:95
#define REDUCT_ASSERT(_cond)
Definition defs.h:25
#define REDUCT_FREE(_ptr)
Definition defs.h:30
#define REDUCT_API
Definition defs.h:7
#define REDUCT_NULL
Definition defs.h:23
Compiled function.
REDUCT_API reduct_function_t * reduct_function_new(reduct_t *reduct)
REDUCT_API void reduct_function_grow(reduct_t *reduct, reduct_function_t *func)
REDUCT_API reduct_const_t reduct_function_lookup_constant(reduct_t *reduct, reduct_function_t *func, reduct_const_slot_t *slot)
Garbage collection.
#define REDUCT_ERROR_RUNTIME(_reduct,...)
Throw a runtime error using the jump buffer in the error structure.
Definition error.h:175
#define REDUCT_ERROR_INTERNAL(_reduct,...)
Throw an internal error using the jump buffer in the error structure.
Definition error.h:183
reduct_uint16_t reduct_const_t
Constant index type.
Definition function.h:71
REDUCT_API void reduct_function_init(reduct_function_t *func)
Initialize a function structure.
REDUCT_API void reduct_function_deinit(reduct_function_t *func)
Deinitialize a function structure.
reduct_uint32_t reduct_inst_t
Instruction type.
Definition inst.h:97
#define REDUCT_CONSTANT_MAX
The max number of constants per function.
Definition inst.h:112
#define REDUCT_ITEM_TYPE_FUNCTION
A function.
Definition item.h:29
REDUCT_API reduct_item_t * reduct_item_new(struct reduct *reduct)
Allocate a new Reduct item.
Handle management.
Item management.
Constant slot.
Definition function.h:43
reduct_const_slot_type_t type
The type of the constant slot.
Definition function.h:44
reduct_uint64_t raw
Definition function.h:46
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
Item structure.
Definition item.h:57
reduct_function_t function
A function.
Definition item.h:69
reduct_item_type_t type
The type of the item.
Definition item.h:61
State structure.
Definition core.h:61