Reduct  v1.0.4-3-gdaf0d70
A functional and immutable language.
Loading...
Searching...
No Matches
Compilation

Detailed Description

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

Variables

reduct_mode_t reduct_expr_t::mode
 Expression mode.
 
reduct_uint16_t   reduct_expr_t::value 
 Raw union value. More...
 
reduct_reg_t   reduct_expr_t::reg 
 Register index. More...
 
reduct_const_t   reduct_expr_t::constant 
 Constant index. More...
 
union { 
 
   reduct_uint16_t   reduct_expr_t::value 
 Raw union value. More...
 
   reduct_reg_t   reduct_expr_t::reg 
 Register index. More...
 
   reduct_const_t   reduct_expr_t::constant 
 Constant index. More...
 
};  
 
reduct_atom_treduct_local_t::name
 The name of the local variable.
 
reduct_expr_t reduct_local_t::expr
 The expression representing the local's value.
 
struct reduct_compiler * reduct_compiler_t::enclosing
 The enclosing compiler context, or REDUCT_NULL.
 
reduct_treduct_compiler_t::reduct
 The Reduct structure.
 
reduct_function_treduct_compiler_t::function
 The function being compiled.
 
reduct_uint16_t reduct_compiler_t::localCount
 The amount of local variables.
 
reduct_uint64_t reduct_compiler_t::regAlloc [REDUCT_REGISTER_MAX/64]
 Bitmask of allocated registers.
 
reduct_uint64_t reduct_compiler_t::regLocal [REDUCT_REGISTER_MAX/64]
 Bitmask of registers used by locals.
 
reduct_local_t reduct_compiler_t::locals [REDUCT_REGISTER_MAX]
 The local variables.
 
reduct_item_treduct_compiler_t::lastItem
 The last item processed by the compiler, used for error reporting.
 

Macro Definition Documentation

◆ REDUCT_REG_SET_ALLOCATED

#define REDUCT_REG_SET_ALLOCATED (   _compiler,
  _reg 
)
Value:
do \
{ \
(_compiler)->regAlloc[(_reg) / 64] |= (1ULL << ((_reg) % 64)); \
if ((_reg) + 1 > (_compiler)->function->registerCount) \
{ \
(_compiler)->function->registerCount = (_reg) + 1; \
} \
} while (0)

Set a register as allocated.

Parameters
_compilerThe compiler instance.
_regThe register to set as allocated.

Definition at line 98 of file compile.h.

◆ REDUCT_REG_CLEAR_ALLOCATED

#define REDUCT_REG_CLEAR_ALLOCATED (   _compiler,
  _reg 
)    ((_compiler)->regAlloc[(_reg) / 64] &= ~(1ULL << ((_reg) % 64)))

Clear a register's allocation status.

Parameters
_compilerThe compiler instance.
_regThe register to clear.

Definition at line 114 of file compile.h.

◆ REDUCT_REG_IS_ALLOCATED

#define REDUCT_REG_IS_ALLOCATED (   _compiler,
  _reg 
)    (((_compiler)->regAlloc[(_reg) / 64] & (1ULL << ((_reg) % 64))) != 0)

Check if a register is allocated.

Parameters
_compilerThe compiler instance.
_regThe register to check.

Definition at line 122 of file compile.h.

◆ REDUCT_REG_SET_LOCAL

#define REDUCT_REG_SET_LOCAL (   _compiler,
  _reg 
)    ((_compiler)->regLocal[(_reg) / 64] |= (1ULL << ((_reg) % 64)))

Set a register as a local.

Parameters
_compilerThe compiler instance.
_regThe register to set as a local.

Definition at line 130 of file compile.h.

◆ REDUCT_REG_CLEAR_LOCAL

#define REDUCT_REG_CLEAR_LOCAL (   _compiler,
  _reg 
)    ((_compiler)->regLocal[(_reg) / 64] &= ~(1ULL << ((_reg) % 64)))

Clear a register's local status.

Parameters
_compilerThe compiler instance.
_regThe register to clear.

Definition at line 138 of file compile.h.

◆ REDUCT_REG_IS_LOCAL

#define REDUCT_REG_IS_LOCAL (   _compiler,
  _reg 
)    (((_compiler)->regLocal[(_reg) / 64] & (1ULL << ((_reg) % 64))) != 0)

Check if a register is a local.

Parameters
_compilerThe compiler instance.
_regThe register to check.

Definition at line 146 of file compile.h.

◆ REDUCT_EXPR_NONE

#define REDUCT_EXPR_NONE ( )    ((reduct_expr_t){.mode = REDUCT_MODE_NONE})

Create a REDUCT_MODE_NONE mode expression.

Definition at line 185 of file compile.h.

◆ REDUCT_EXPR_REG

#define REDUCT_EXPR_REG (   _reg)    ((reduct_expr_t){.mode = REDUCT_MODE_REG, .reg = (_reg)})

Create a REDUCT_MODE_REG mode expression.

Parameters
_regThe register index.

Definition at line 192 of file compile.h.

◆ REDUCT_EXPR_CONST

#define REDUCT_EXPR_CONST (   _const)    ((reduct_expr_t){.mode = REDUCT_MODE_CONST, .constant = (_const)})

Create a REDUCT_MODE_CONST mode expression.

Parameters
_constThe constant index.

Definition at line 199 of file compile.h.

◆ REDUCT_EXPR_CONST_ITEM

#define REDUCT_EXPR_CONST_ITEM (   _compiler,
  _item 
)
Value:
REDUCT_EXPR_CONST(reduct_function_lookup_constant((_compiler)->reduct, (_compiler)->function, \
&(reduct_const_slot_t){.type = REDUCT_CONST_SLOT_ITEM, .item = (_item)}))
#define REDUCT_EXPR_CONST(_const)
Create a REDUCT_MODE_CONST mode expression.
Definition compile.h:199
#define REDUCT_CONST_SLOT_ITEM(_item)
Create a constant slot containing an item.
Definition function.h:57
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.
Constant slot.
Definition function.h:43

Create a REDUCT_MODE_CONST mode expression for a specific item.

Parameters
_compilerThe compiler context.
_itemThe item to look up.

Definition at line 207 of file compile.h.

◆ REDUCT_EXPR_CONST_ATOM

#define REDUCT_EXPR_CONST_ATOM (   _compiler,
  _atom 
)
Value:
REDUCT_EXPR_CONST(reduct_function_lookup_constant((_compiler)->reduct, (_compiler)->function, \
.item = REDUCT_CONTAINER_OF(_atom, reduct_item_t, atom)}))
#define REDUCT_CONTAINER_OF(_ptr, _type, _member)
Container of macro.
Definition defs.h:184
Item structure.
Definition item.h:57

Create a REDUCT_MODE_CONST mode expression for a specific atom.

Parameters
_compilerThe compiler context.
_atomThe atom to look up.

Definition at line 217 of file compile.h.

◆ REDUCT_EXPR_TARGET

#define REDUCT_EXPR_TARGET (   _reg)    ((reduct_expr_t){.mode = REDUCT_MODE_TARGET, .reg = (_reg)})

Create a REDUCT_MODE_TARGET mode expression.

Parameters
_regThe target register hint.

Definition at line 227 of file compile.h.

◆ REDUCT_EXPR_TRUE

#define REDUCT_EXPR_TRUE (   _compiler)
Value:
REDUCT_EXPR_CONST(reduct_function_lookup_constant((_compiler)->reduct, (_compiler)->function, \
&(reduct_const_slot_t){.type = REDUCT_CONST_SLOT_ITEM, .item = (_compiler)->reduct->trueItem}))

Create a REDUCT_MODE_CONST mode expression for the true constant.

Parameters
_compilerThe compiler context.

Definition at line 234 of file compile.h.

◆ REDUCT_EXPR_FALSE

#define REDUCT_EXPR_FALSE (   _compiler)
Value:
REDUCT_EXPR_CONST(reduct_function_lookup_constant((_compiler)->reduct, (_compiler)->function, \
&(reduct_const_slot_t){.type = REDUCT_CONST_SLOT_ITEM, .item = (_compiler)->reduct->falseItem}))

Create a REDUCT_MODE_CONST mode expression for the false constant.

Parameters
_compilerThe compiler context.

Definition at line 243 of file compile.h.

◆ REDUCT_EXPR_NIL

#define REDUCT_EXPR_NIL (   _compiler)
Value:
REDUCT_EXPR_CONST(reduct_function_lookup_constant((_compiler)->reduct, (_compiler)->function, \
&(reduct_const_slot_t){.type = REDUCT_CONST_SLOT_ITEM, .item = (_compiler)->reduct->nilItem}))

Create a REDUCT_MODE_CONST mode expression for the nil constant.

Parameters
_compilerThe compiler context.

Definition at line 252 of file compile.h.

◆ REDUCT_EXPR_INT

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

Parameters
_compilerThe compiler context.
_valThe integer value.

Definition at line 262 of file compile.h.

◆ REDUCT_EXPR_GET_TARGET

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

Parameters
_exprThe expression to check.

Definition at line 269 of file compile.h.

◆ REDUCT_EXPR_FLOAT

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

Parameters
_compilerThe compiler context.
_valThe float value.

Definition at line 277 of file compile.h.

◆ REDUCT_LOCAL_IS_DEFINED

#define REDUCT_LOCAL_IS_DEFINED (   _local)    ((_local)->expr.mode != REDUCT_MODE_NONE)

Check if a local variable has finished being defined.

If the local variable is not defined, then we are currently within the expression that defines it.

Parameters
_localThe local variable descriptor.

Definition at line 347 of file compile.h.

Function Documentation

◆ reduct_compile()

REDUCT_API reduct_function_t * reduct_compile ( reduct_t reduct,
reduct_handle_t ast 
)

Compiles a Reduct AST into a callable bytecode function.

Warning
The jump buffer must have been set using REDUCT_CATCH before calling this function.
Parameters
reductThe Reduct structure.
astThe root AST item to compile (usually a list of expressions).
Returns
A pointer to the compiled function.

Definition at line 13 of file compile_impl.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ reduct_compiler_init()

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.

Parameters
compilerThe compiler context to initialize.
reductThe Reduct structure.
functionThe function to compile into.
enclosingThe enclosing compiler context, or REDUCT_NULL.

Definition at line 40 of file compile_impl.h.

Here is the caller graph for this function:

◆ reduct_compiler_deinit()

REDUCT_API void reduct_compiler_deinit ( reduct_compiler_t compiler)

Deinitialize a compiler context.

Parameters
compilerThe compiler context to deinitialize.

Definition at line 58 of file compile_impl.h.

Here is the caller graph for this function:

◆ reduct_reg_alloc()

REDUCT_API reduct_reg_t reduct_reg_alloc ( reduct_compiler_t compiler)

Allocate a new register.

Parameters
compilerThe compiler context.
Returns
The allocated register index.

Definition at line 64 of file compile_impl.h.

Here is the caller graph for this function:

◆ reduct_reg_alloc_range()

REDUCT_API reduct_reg_t reduct_reg_alloc_range ( reduct_compiler_t compiler,
reduct_uint32_t  count 
)

Allocate a range of registers.

Parameters
compilerThe compiler context.
countThe number of registers to allocate.
Returns
The first register index in the allocated range.

Definition at line 88 of file compile_impl.h.

Here is the caller graph for this function:

◆ reduct_reg_free()

REDUCT_API void reduct_reg_free ( reduct_compiler_t compiler,
reduct_reg_t  reg 
)

Free a register.

Parameters
compilerThe compiler context.
regThe register index to free.

Definition at line 124 of file compile_impl.h.

Here is the caller graph for this function:

◆ reduct_reg_free_range()

REDUCT_API void reduct_reg_free_range ( reduct_compiler_t compiler,
reduct_reg_t  start,
reduct_uint32_t  count 
)

Free a range of registers.

Parameters
compilerThe compiler context.
startThe first register index in the range to free.
countThe number of registers to free.

Definition at line 140 of file compile_impl.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ reduct_expr_build()

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.

Parameters
compilerThe compiler context.
itemThe item to compile.
Outputpointer for the compiled expression.

Definition at line 286 of file compile_impl.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ reduct_expr_done()

static void reduct_expr_done ( reduct_compiler_t compiler,
reduct_expr_t expr 
)
inlinestatic

Free resources associated with an expression descriptor.

Parameters
compilerThe compiler context.
exprThe expression to free.

Definition at line 295 of file compile.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ reduct_expr_get_reg()

static reduct_reg_t reduct_expr_get_reg ( reduct_compiler_t compiler,
reduct_expr_t out 
)
inlinestatic

Allocate a new register, favoring the output expression's target if provided.

Parameters
compilerThe compiler context.
outThe output expression which may contain a target hint.
Returns
The allocated register index.

Definition at line 312 of file compile.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ reduct_reg_get_base()

static reduct_reg_t reduct_reg_get_base ( reduct_compiler_t compiler)
inlinestatic

Get the first unallocated register index.

Parameters
compilerThe compiler context.
Returns
The first register index that is not currently allocated.

Definition at line 328 of file compile.h.

Here is the caller graph for this function:

◆ reduct_local_def()

REDUCT_API reduct_local_t * reduct_local_def ( reduct_compiler_t compiler,
reduct_atom_t name 
)

Define a new local variable.

Note
The reduct_local_def_done() function must be called after this one.
Parameters
compilerThe compiler context.
nameThe name of the local.
Returns
A pointer to the local variable descriptor.

Definition at line 310 of file compile_impl.h.

Here is the caller graph for this function:

◆ reduct_local_def_done()

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.

Parameters
compilerThe compiler context.
localThe local variable descriptor.
exprThe expression representing the local's value.

Definition at line 325 of file compile_impl.h.

Here is the caller graph for this function:

◆ reduct_local_add_arg()

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.

Parameters
compilerThe compiler context.
nameThe name of the argument.
Returns
A pointer to the local variable descriptor.

Definition at line 344 of file compile_impl.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ reduct_local_pop()

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.

Parameters
compilerThe compiler context.
toCountThe local count to restore to.
resultThe result expression of the block, whose register should not be freed.

Definition at line 362 of file compile_impl.h.

Here is the caller graph for this function:

◆ reduct_local_lookup()

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.

Parameters
compilerThe compiler context.
nameThe name of the local.
Returns
A pointer to the local variable descriptor, or REDUCT_NULL if not found.

Definition at line 399 of file compile_impl.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ reduct_compile_inst()

static void reduct_compile_inst ( reduct_compiler_t compiler,
reduct_inst_t  inst 
)
inlinestatic

Emits an instruction to the current function.

Parameters
compilerThe compiler context.
instThe instruction to emit.

Definition at line 402 of file compile.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ reduct_compile_list()

static void reduct_compile_list ( reduct_compiler_t compiler,
reduct_reg_t  target 
)
inlinestatic

Emits a REDUCT_OPCODE_LIST instruction, that creates a list in the target register.

Parameters
compilerThe compiler context.
targetThe target register.

Definition at line 415 of file compile.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ reduct_compile_call()

static void reduct_compile_call ( reduct_compiler_t compiler,
reduct_reg_t  target,
reduct_expr_t callable,
reduct_uint32_t  arity 
)
inlinestatic

Emits a REDUCT_OPCODE_CALL instruction, that returns its result in the target register.

Parameters
compilerThe compiler context.
targetThe target register.
callableThe callable expression.
arityThe number of arguments.

Definition at line 429 of file compile.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ reduct_compile_move()

static void reduct_compile_move ( reduct_compiler_t compiler,
reduct_reg_t  target,
reduct_expr_t expr 
)
inlinestatic

Emits a REDUCT_OPCODE_MOVE instruction, that moves the value of the source expression to the target register.

Parameters
compilerThe compiler context.
targetThe target register.
exprThe source expression.

Definition at line 447 of file compile.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ reduct_compile_jump()

static reduct_size_t reduct_compile_jump ( reduct_compiler_t compiler,
reduct_opcode_t  op,
reduct_reg_t  a 
)
inlinestatic

Emits a jump instruction without a target offset.

Parameters
compilerThe compiler context.
opThe jump opcode (e.g., REDUCT_OPCODE_JMP, REDUCT_OPCODE_JMPT, REDUCT_OPCODE_JMPF).
aThe register to test (if not REDUCT_OPCODE_JMP).
Returns
The index of the emitted instruction to be patched later.

Definition at line 467 of file compile.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ reduct_compile_jump_patch()

static void reduct_compile_jump_patch ( reduct_compiler_t compiler,
reduct_size_t  pos 
)
inlinestatic

Patch a previously emitted jump instruction to point to the current instruction.

Parameters
compilerThe compiler context.
posThe index of the jump instruction to patch.

Definition at line 481 of file compile.h.

Here is the caller graph for this function:

◆ reduct_compile_jump_patch_list()

static void reduct_compile_jump_patch_list ( reduct_compiler_t compiler,
reduct_size_t jumps,
reduct_size_t  count 
)
inlinestatic

Patch a list of jump instructions to point to the current instruction.

Parameters
compilerThe compiler context.
jumpsArray of jump instruction indices.
countNumber of jumps in the array.

Definition at line 495 of file compile.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ reduct_compile_move_or_alloc()

static reduct_reg_t reduct_compile_move_or_alloc ( reduct_compiler_t compiler,
reduct_expr_t expr 
)
inlinestatic

Emits a move instruction or allocates a new register if the expression is not already in a register.

Parameters
compilerThe compiler context.
exprThe expression to move or allocate.
Returns
The register where the value is stored.

Definition at line 511 of file compile.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ reduct_compile_return()

static void reduct_compile_return ( reduct_compiler_t compiler,
reduct_expr_t expr 
)
inlinestatic

Emits a REDUCT_OPCODE_RET instruction.

Parameters
compilerThe compiler context.
exprThe expression to return.

Definition at line 533 of file compile.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ reduct_compile_append()

static void reduct_compile_append ( reduct_compiler_t compiler,
reduct_reg_t  target,
reduct_expr_t expr 
)
inlinestatic

Emits an REDUCT_OPCODE_APPEND instruction.

Parameters
compilerThe compiler context.
targetThe target list register.
exprThe expression to append.

Definition at line 623 of file compile.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ reduct_compile_binary()

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 
)
inlinestatic

Emits a comparison, arithmetic or bitwise instruction.

Parameters
compilerThe compiler context.
opBaseThe base opcode (without a mode) for the operation (e.g, REDUCT_OPCODE_ADD, REDUCT_OPCODE_EQ).
targetThe target register.
leftThe left operand register.
rightThe right operand expression.

Definition at line 641 of file compile.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ reduct_compile_closure()

static void reduct_compile_closure ( reduct_compiler_t compiler,
reduct_reg_t  target,
reduct_const_t  funcConst 
)
inlinestatic

Emits a REDUCT_OPCODE_CLOSURE instruction.

Parameters
compilerThe compiler context.
targetThe target register.
funcConstThe constant index of the function prototype.

Definition at line 658 of file compile.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ reduct_compile_capture()

static void reduct_compile_capture ( reduct_compiler_t compiler,
reduct_reg_t  closureReg,
reduct_uint32_t  slot,
reduct_expr_t expr 
)
inlinestatic

Emits a REDUCT_OPCODE_CAPTURE instruction.

Parameters
compilerThe compiler context.
closureRegThe register containing the closure.
slotThe constant slot index in the closure to capture into.
exprThe expression to be captured.

Definition at line 672 of file compile.h.

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ mode

reduct_mode_t reduct_expr_t::mode

Expression mode.

Definition at line 29 of file compile.h.

◆ value [1/2]

reduct_uint16_t reduct_expr_t::value

Raw union value.

Definition at line 31 of file compile.h.

◆ [] [2/2]

reduct_uint16_t { ... } ::value

Raw union value.

Definition at line 31 of file compile.h.

◆ reg [1/2]

reduct_reg_t reduct_expr_t::reg

Register index.

Definition at line 32 of file compile.h.

◆ [] [2/2]

reduct_reg_t { ... } ::reg

Register index.

Definition at line 32 of file compile.h.

◆ constant [1/2]

reduct_const_t reduct_expr_t::constant

Constant index.

Definition at line 33 of file compile.h.

◆ [] [2/2]

reduct_const_t { ... } ::constant

Constant index.

Definition at line 33 of file compile.h.

◆ [union]

union { ... } reduct_expr_t

◆ name

reduct_atom_t* reduct_local_t::name

The name of the local variable.

Definition at line 43 of file compile.h.

◆ expr

reduct_expr_t reduct_local_t::expr

The expression representing the local's value.

Definition at line 44 of file compile.h.

◆ enclosing

struct reduct_compiler* reduct_compiler_t::enclosing

The enclosing compiler context, or REDUCT_NULL.

Definition at line 53 of file compile.h.

◆ reduct

reduct_t* reduct_compiler_t::reduct

The Reduct structure.

Definition at line 54 of file compile.h.

◆ function

reduct_function_t* reduct_compiler_t::function

The function being compiled.

Definition at line 55 of file compile.h.

◆ localCount

reduct_uint16_t reduct_compiler_t::localCount

The amount of local variables.

Definition at line 56 of file compile.h.

◆ regAlloc

reduct_uint64_t reduct_compiler_t::regAlloc[REDUCT_REGISTER_MAX/64]

Bitmask of allocated registers.

Definition at line 57 of file compile.h.

◆ regLocal

reduct_uint64_t reduct_compiler_t::regLocal[REDUCT_REGISTER_MAX/64]

Bitmask of registers used by locals.

Definition at line 58 of file compile.h.

◆ locals

reduct_local_t reduct_compiler_t::locals[REDUCT_REGISTER_MAX]

The local variables.

Definition at line 59 of file compile.h.

◆ lastItem

reduct_item_t* reduct_compiler_t::lastItem

The last item processed by the compiler, used for error reporting.

Definition at line 60 of file compile.h.