|
Reduct
v1.0.4-3-gdaf0d70
A functional and immutable language.
|
The compilation process converts S-expressions into register-based bytecode that can be executed by the Reduct virtual machine.
Data Structures | |
| struct | reduct_expr_t |
| Expression descriptor structure. More... | |
| struct | reduct_local_t |
| Local structure. More... | |
| struct | reduct_compiler_t |
| Compiler structure. More... | |
Macros | |
| #define | REDUCT_REG_SET_ALLOCATED(_compiler, _reg) |
| Set a register as allocated. | |
| #define | REDUCT_REG_CLEAR_ALLOCATED(_compiler, _reg) ((_compiler)->regAlloc[(_reg) / 64] &= ~(1ULL << ((_reg) % 64))) |
| Clear a register's allocation status. | |
| #define | REDUCT_REG_IS_ALLOCATED(_compiler, _reg) (((_compiler)->regAlloc[(_reg) / 64] & (1ULL << ((_reg) % 64))) != 0) |
| Check if a register is allocated. | |
| #define | REDUCT_REG_SET_LOCAL(_compiler, _reg) ((_compiler)->regLocal[(_reg) / 64] |= (1ULL << ((_reg) % 64))) |
| Set a register as a local. | |
| #define | REDUCT_REG_CLEAR_LOCAL(_compiler, _reg) ((_compiler)->regLocal[(_reg) / 64] &= ~(1ULL << ((_reg) % 64))) |
| Clear a register's local status. | |
| #define | REDUCT_REG_IS_LOCAL(_compiler, _reg) (((_compiler)->regLocal[(_reg) / 64] & (1ULL << ((_reg) % 64))) != 0) |
| Check if a register is a local. | |
| #define | REDUCT_EXPR_NONE() ((reduct_expr_t){.mode = REDUCT_MODE_NONE}) |
Create a REDUCT_MODE_NONE mode expression. | |
| #define | REDUCT_EXPR_REG(_reg) ((reduct_expr_t){.mode = REDUCT_MODE_REG, .reg = (_reg)}) |
Create a REDUCT_MODE_REG mode expression. | |
| #define | REDUCT_EXPR_CONST(_const) ((reduct_expr_t){.mode = REDUCT_MODE_CONST, .constant = (_const)}) |
Create a REDUCT_MODE_CONST mode expression. | |
| #define | REDUCT_EXPR_CONST_ITEM(_compiler, _item) |
Create a REDUCT_MODE_CONST mode expression for a specific item. | |
| #define | REDUCT_EXPR_CONST_ATOM(_compiler, _atom) |
Create a REDUCT_MODE_CONST mode expression for a specific atom. | |
| #define | REDUCT_EXPR_TARGET(_reg) ((reduct_expr_t){.mode = REDUCT_MODE_TARGET, .reg = (_reg)}) |
Create a REDUCT_MODE_TARGET mode expression. | |
| #define | REDUCT_EXPR_TRUE(_compiler) |
Create a REDUCT_MODE_CONST mode expression for the true constant. | |
| #define | REDUCT_EXPR_FALSE(_compiler) |
Create a REDUCT_MODE_CONST mode expression for the false constant. | |
| #define | REDUCT_EXPR_NIL(_compiler) |
Create a REDUCT_MODE_CONST mode expression for the nil constant. | |
| #define | REDUCT_EXPR_INT(_compiler, _val) REDUCT_EXPR_CONST_ATOM(_compiler, reduct_atom_lookup_int((_compiler)->reduct, (_val))) |
Create a REDUCT_MODE_CONST mode expression for an integer. | |
| #define | REDUCT_EXPR_GET_TARGET(_expr) (((_expr)->mode == REDUCT_MODE_TARGET) ? (_expr)->reg : REDUCT_REG_INVALID) |
| Get the target register index from an expression, or -1 if no target is specified. | |
| #define | REDUCT_EXPR_FLOAT(_compiler, _val) REDUCT_EXPR_CONST_ATOM(_compiler, reduct_atom_lookup_float((_compiler)->reduct, (_val))) |
Create a REDUCT_MODE_CONST mode expression for a float. | |
| #define | REDUCT_LOCAL_IS_DEFINED(_local) ((_local)->expr.mode != REDUCT_MODE_NONE) |
| Check if a local variable has finished being defined. | |
Functions | |
| REDUCT_API reduct_function_t * | reduct_compile (reduct_t *reduct, reduct_handle_t *ast) |
| Compiles a Reduct AST into a callable bytecode function. | |
| REDUCT_API void | reduct_compiler_init (reduct_compiler_t *compiler, reduct_t *reduct, reduct_function_t *function, reduct_compiler_t *enclosing) |
| Initialize a compiler context. | |
| REDUCT_API void | reduct_compiler_deinit (reduct_compiler_t *compiler) |
| Deinitialize a compiler context. | |
| REDUCT_API reduct_reg_t | reduct_reg_alloc (reduct_compiler_t *compiler) |
| Allocate a new register. | |
| REDUCT_API reduct_reg_t | reduct_reg_alloc_range (reduct_compiler_t *compiler, reduct_uint32_t count) |
| Allocate a range of registers. | |
| REDUCT_API void | reduct_reg_free (reduct_compiler_t *compiler, reduct_reg_t reg) |
| Free a register. | |
| REDUCT_API void | reduct_reg_free_range (reduct_compiler_t *compiler, reduct_reg_t start, reduct_uint32_t count) |
| Free a range of registers. | |
| REDUCT_API void | reduct_expr_build (reduct_compiler_t *compiler, reduct_item_t *item, reduct_expr_t *out) |
| Compiles a single Reduct item into an expression descriptor. | |
| static void | reduct_expr_done (reduct_compiler_t *compiler, reduct_expr_t *expr) |
| Free resources associated with an expression descriptor. | |
| static reduct_reg_t | reduct_expr_get_reg (reduct_compiler_t *compiler, reduct_expr_t *out) |
| Allocate a new register, favoring the output expression's target if provided. | |
| static reduct_reg_t | reduct_reg_get_base (reduct_compiler_t *compiler) |
| Get the first unallocated register index. | |
| REDUCT_API reduct_local_t * | reduct_local_def (reduct_compiler_t *compiler, reduct_atom_t *name) |
| Define a new local variable. | |
| REDUCT_API void | reduct_local_def_done (reduct_compiler_t *compiler, reduct_local_t *local, reduct_expr_t *expr) |
| Finalize a local variable definition with its value expression. | |
| REDUCT_API reduct_local_t * | reduct_local_add_arg (reduct_compiler_t *compiler, reduct_atom_t *name) |
| Add a function argument local to the compiler context. | |
| REDUCT_API void | reduct_local_pop (reduct_compiler_t *compiler, reduct_uint16_t toCount, reduct_expr_t *result) |
| Pop local variables from the stack, releasing their registers if they are no longer used. | |
| REDUCT_API reduct_local_t * | reduct_local_lookup (reduct_compiler_t *compiler, reduct_atom_t *name) |
| Look up a local by name and return its expression. | |
| static void | reduct_compile_inst (reduct_compiler_t *compiler, reduct_inst_t inst) |
| Emits an instruction to the current function. | |
| static void | reduct_compile_list (reduct_compiler_t *compiler, reduct_reg_t target) |
Emits a REDUCT_OPCODE_LIST instruction, that creates a list in the target register. | |
| static void | reduct_compile_call (reduct_compiler_t *compiler, reduct_reg_t target, reduct_expr_t *callable, reduct_uint32_t arity) |
Emits a REDUCT_OPCODE_CALL instruction, that returns its result in the target register. | |
| static void | reduct_compile_move (reduct_compiler_t *compiler, reduct_reg_t target, reduct_expr_t *expr) |
Emits a REDUCT_OPCODE_MOVE instruction, that moves the value of the source expression to the target register. | |
| static reduct_size_t | reduct_compile_jump (reduct_compiler_t *compiler, reduct_opcode_t op, reduct_reg_t a) |
| Emits a jump instruction without a target offset. | |
| static void | reduct_compile_jump_patch (reduct_compiler_t *compiler, reduct_size_t pos) |
| Patch a previously emitted jump instruction to point to the current instruction. | |
| static void | reduct_compile_jump_patch_list (reduct_compiler_t *compiler, reduct_size_t *jumps, reduct_size_t count) |
| Patch a list of jump instructions to point to the current instruction. | |
| static reduct_reg_t | reduct_compile_move_or_alloc (reduct_compiler_t *compiler, reduct_expr_t *expr) |
| Emits a move instruction or allocates a new register if the expression is not already in a register. | |
| static void | reduct_compile_return (reduct_compiler_t *compiler, reduct_expr_t *expr) |
Emits a REDUCT_OPCODE_RET instruction. | |
| static void | reduct_compile_append (reduct_compiler_t *compiler, reduct_reg_t target, reduct_expr_t *expr) |
Emits an REDUCT_OPCODE_APPEND instruction. | |
| static void | reduct_compile_binary (reduct_compiler_t *compiler, reduct_opcode_t opBase, reduct_reg_t target, reduct_reg_t left, reduct_expr_t *right) |
| Emits a comparison, arithmetic or bitwise instruction. | |
| static void | reduct_compile_closure (reduct_compiler_t *compiler, reduct_reg_t target, reduct_const_t funcConst) |
Emits a REDUCT_OPCODE_CLOSURE instruction. | |
| static void | reduct_compile_capture (reduct_compiler_t *compiler, reduct_reg_t closureReg, reduct_uint32_t slot, reduct_expr_t *expr) |
Emits a REDUCT_OPCODE_CAPTURE instruction. | |
| #define REDUCT_REG_SET_ALLOCATED | ( | _compiler, | |
| _reg | |||
| ) |
Set a register as allocated.
| _compiler | The compiler instance. |
| _reg | The register to set as allocated. |
| #define REDUCT_REG_CLEAR_ALLOCATED | ( | _compiler, | |
| _reg | |||
| ) | ((_compiler)->regAlloc[(_reg) / 64] &= ~(1ULL << ((_reg) % 64))) |
| #define REDUCT_REG_IS_ALLOCATED | ( | _compiler, | |
| _reg | |||
| ) | (((_compiler)->regAlloc[(_reg) / 64] & (1ULL << ((_reg) % 64))) != 0) |
| #define REDUCT_REG_SET_LOCAL | ( | _compiler, | |
| _reg | |||
| ) | ((_compiler)->regLocal[(_reg) / 64] |= (1ULL << ((_reg) % 64))) |
| #define REDUCT_REG_CLEAR_LOCAL | ( | _compiler, | |
| _reg | |||
| ) | ((_compiler)->regLocal[(_reg) / 64] &= ~(1ULL << ((_reg) % 64))) |
| #define REDUCT_REG_IS_LOCAL | ( | _compiler, | |
| _reg | |||
| ) | (((_compiler)->regLocal[(_reg) / 64] & (1ULL << ((_reg) % 64))) != 0) |
| #define REDUCT_EXPR_NONE | ( | ) | ((reduct_expr_t){.mode = REDUCT_MODE_NONE}) |
| #define REDUCT_EXPR_REG | ( | _reg | ) | ((reduct_expr_t){.mode = REDUCT_MODE_REG, .reg = (_reg)}) |
| #define REDUCT_EXPR_CONST | ( | _const | ) | ((reduct_expr_t){.mode = REDUCT_MODE_CONST, .constant = (_const)}) |
| #define REDUCT_EXPR_CONST_ITEM | ( | _compiler, | |
| _item | |||
| ) |
Create a REDUCT_MODE_CONST mode expression for a specific item.
| _compiler | The compiler context. |
| _item | The item to look up. |
| #define REDUCT_EXPR_CONST_ATOM | ( | _compiler, | |
| _atom | |||
| ) |
Create a REDUCT_MODE_CONST mode expression for a specific atom.
| _compiler | The compiler context. |
| _atom | The atom to look up. |
| #define REDUCT_EXPR_TARGET | ( | _reg | ) | ((reduct_expr_t){.mode = REDUCT_MODE_TARGET, .reg = (_reg)}) |
| #define REDUCT_EXPR_TRUE | ( | _compiler | ) |
Create a REDUCT_MODE_CONST mode expression for the true constant.
| _compiler | The compiler context. |
| #define REDUCT_EXPR_FALSE | ( | _compiler | ) |
Create a REDUCT_MODE_CONST mode expression for the false constant.
| _compiler | The compiler context. |
| #define REDUCT_EXPR_NIL | ( | _compiler | ) |
Create a REDUCT_MODE_CONST mode expression for the nil constant.
| _compiler | The compiler context. |
| #define REDUCT_EXPR_INT | ( | _compiler, | |
| _val | |||
| ) | REDUCT_EXPR_CONST_ATOM(_compiler, reduct_atom_lookup_int((_compiler)->reduct, (_val))) |
| #define REDUCT_EXPR_GET_TARGET | ( | _expr | ) | (((_expr)->mode == REDUCT_MODE_TARGET) ? (_expr)->reg : REDUCT_REG_INVALID) |
| #define REDUCT_EXPR_FLOAT | ( | _compiler, | |
| _val | |||
| ) | REDUCT_EXPR_CONST_ATOM(_compiler, reduct_atom_lookup_float((_compiler)->reduct, (_val))) |
| #define REDUCT_LOCAL_IS_DEFINED | ( | _local | ) | ((_local)->expr.mode != REDUCT_MODE_NONE) |
| REDUCT_API reduct_function_t * reduct_compile | ( | reduct_t * | reduct, |
| reduct_handle_t * | ast | ||
| ) |
Compiles a Reduct AST into a callable bytecode function.
REDUCT_CATCH before calling this function.| reduct | The Reduct structure. |
| ast | The root AST item to compile (usually a list of expressions). |
Definition at line 13 of file compile_impl.h.
| REDUCT_API void reduct_compiler_init | ( | reduct_compiler_t * | compiler, |
| reduct_t * | reduct, | ||
| reduct_function_t * | function, | ||
| reduct_compiler_t * | enclosing | ||
| ) |
Initialize a compiler context.
| compiler | The compiler context to initialize. |
| reduct | The Reduct structure. |
| function | The function to compile into. |
| enclosing | The enclosing compiler context, or REDUCT_NULL. |
Definition at line 40 of file compile_impl.h.
| REDUCT_API void reduct_compiler_deinit | ( | reduct_compiler_t * | compiler | ) |
Deinitialize a compiler context.
| compiler | The compiler context to deinitialize. |
Definition at line 58 of file compile_impl.h.
| REDUCT_API reduct_reg_t reduct_reg_alloc | ( | reduct_compiler_t * | compiler | ) |
Allocate a new register.
| compiler | The compiler context. |
Definition at line 64 of file compile_impl.h.
| REDUCT_API reduct_reg_t reduct_reg_alloc_range | ( | reduct_compiler_t * | compiler, |
| reduct_uint32_t | count | ||
| ) |
Allocate a range of registers.
| compiler | The compiler context. |
| count | The number of registers to allocate. |
Definition at line 88 of file compile_impl.h.
| REDUCT_API void reduct_reg_free | ( | reduct_compiler_t * | compiler, |
| reduct_reg_t | reg | ||
| ) |
Free a register.
| compiler | The compiler context. |
| reg | The register index to free. |
Definition at line 124 of file compile_impl.h.
| REDUCT_API void reduct_reg_free_range | ( | reduct_compiler_t * | compiler, |
| reduct_reg_t | start, | ||
| reduct_uint32_t | count | ||
| ) |
Free a range of registers.
| compiler | The compiler context. |
| start | The first register index in the range to free. |
| count | The number of registers to free. |
Definition at line 140 of file compile_impl.h.
| REDUCT_API void reduct_expr_build | ( | reduct_compiler_t * | compiler, |
| reduct_item_t * | item, | ||
| reduct_expr_t * | out | ||
| ) |
Compiles a single Reduct item into an expression descriptor.
| compiler | The compiler context. |
| item | The item to compile. |
| Output | pointer for the compiled expression. |
Definition at line 286 of file compile_impl.h.
|
inlinestatic |
|
inlinestatic |
Allocate a new register, favoring the output expression's target if provided.
| compiler | The compiler context. |
| out | The output expression which may contain a target hint. |
Definition at line 312 of file compile.h.
|
inlinestatic |
| REDUCT_API reduct_local_t * reduct_local_def | ( | reduct_compiler_t * | compiler, |
| reduct_atom_t * | name | ||
| ) |
Define a new local variable.
reduct_local_def_done() function must be called after this one.| compiler | The compiler context. |
| name | The name of the local. |
Definition at line 310 of file compile_impl.h.
| REDUCT_API void reduct_local_def_done | ( | reduct_compiler_t * | compiler, |
| reduct_local_t * | local, | ||
| reduct_expr_t * | expr | ||
| ) |
Finalize a local variable definition with its value expression.
| compiler | The compiler context. |
| local | The local variable descriptor. |
| expr | The expression representing the local's value. |
Definition at line 325 of file compile_impl.h.
| REDUCT_API reduct_local_t * reduct_local_add_arg | ( | reduct_compiler_t * | compiler, |
| reduct_atom_t * | name | ||
| ) |
Add a function argument local to the compiler context.
| compiler | The compiler context. |
| name | The name of the argument. |
Definition at line 344 of file compile_impl.h.
| REDUCT_API void reduct_local_pop | ( | reduct_compiler_t * | compiler, |
| reduct_uint16_t | toCount, | ||
| reduct_expr_t * | result | ||
| ) |
Pop local variables from the stack, releasing their registers if they are no longer used.
| compiler | The compiler context. |
| toCount | The local count to restore to. |
| result | The result expression of the block, whose register should not be freed. |
Definition at line 362 of file compile_impl.h.
| REDUCT_API reduct_local_t * reduct_local_lookup | ( | reduct_compiler_t * | compiler, |
| reduct_atom_t * | name | ||
| ) |
Look up a local by name and return its expression.
| compiler | The compiler context. |
| name | The name of the local. |
REDUCT_NULL if not found. Definition at line 399 of file compile_impl.h.
|
inlinestatic |
|
inlinestatic |
|
inlinestatic |
Emits a REDUCT_OPCODE_CALL instruction, that returns its result in the target register.
| compiler | The compiler context. |
| target | The target register. |
| callable | The callable expression. |
| arity | The number of arguments. |
Definition at line 429 of file compile.h.
|
inlinestatic |
Emits a REDUCT_OPCODE_MOVE instruction, that moves the value of the source expression to the target register.
| compiler | The compiler context. |
| target | The target register. |
| expr | The source expression. |
Definition at line 447 of file compile.h.
|
inlinestatic |
Emits a jump instruction without a target offset.
| compiler | The compiler context. |
| op | The jump opcode (e.g., REDUCT_OPCODE_JMP, REDUCT_OPCODE_JMPT, REDUCT_OPCODE_JMPF). |
| a | The register to test (if not REDUCT_OPCODE_JMP). |
Definition at line 467 of file compile.h.
|
inlinestatic |
|
inlinestatic |
Patch a list of jump instructions to point to the current instruction.
| compiler | The compiler context. |
| jumps | Array of jump instruction indices. |
| count | Number of jumps in the array. |
Definition at line 495 of file compile.h.
|
inlinestatic |
Emits a move instruction or allocates a new register if the expression is not already in a register.
| compiler | The compiler context. |
| expr | The expression to move or allocate. |
Definition at line 511 of file compile.h.
|
inlinestatic |
|
inlinestatic |
|
inlinestatic |
Emits a comparison, arithmetic or bitwise instruction.
| compiler | The compiler context. |
| opBase | The base opcode (without a mode) for the operation (e.g, REDUCT_OPCODE_ADD, REDUCT_OPCODE_EQ). |
| target | The target register. |
| left | The left operand register. |
| right | The right operand expression. |
Definition at line 641 of file compile.h.
|
inlinestatic |
|
inlinestatic |
Emits a REDUCT_OPCODE_CAPTURE instruction.
| compiler | The compiler context. |
| closureReg | The register containing the closure. |
| slot | The constant slot index in the closure to capture into. |
| expr | The expression to be captured. |
Definition at line 672 of file compile.h.
| reduct_mode_t reduct_expr_t::mode |
| reduct_uint16_t reduct_expr_t::value |
| reduct_reg_t reduct_expr_t::reg |
| reduct_const_t reduct_expr_t::constant |
| union { ... } reduct_expr_t |
| reduct_atom_t* reduct_local_t::name |
| reduct_expr_t reduct_local_t::expr |
| struct reduct_compiler* reduct_compiler_t::enclosing |
| reduct_function_t* reduct_compiler_t::function |
| reduct_uint16_t reduct_compiler_t::localCount |
| reduct_uint64_t reduct_compiler_t::regAlloc[REDUCT_REGISTER_MAX/64] |
| reduct_uint64_t reduct_compiler_t::regLocal[REDUCT_REGISTER_MAX/64] |
| reduct_local_t reduct_compiler_t::locals[REDUCT_REGISTER_MAX] |
| reduct_item_t* reduct_compiler_t::lastItem |