Reduct  v1.0.4-3-gdaf0d70
A functional and immutable language.
Loading...
Searching...
No Matches
compile.h File Reference

Bytecode compilation. More...

#include "defs.h"
#include "core.h"
#include "function.h"
#include "gc.h"
#include "inst.h"
#include "item.h"
#include "list.h"
Include dependency graph for compile.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

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_treduct_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_treduct_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_treduct_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_treduct_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.
 

Detailed Description

Bytecode compilation.

Definition in file compile.h.