2#ifndef REDUCT_COMPILE_IMPL_H
3#define REDUCT_COMPILE_IMPL_H 1
74 if (!(compiler->
regAlloc[w] & (1ULL << b)))
100 for (length = 0; length < count; length++)
237 handler(compiler, list, out);
408 return &compiler->
locals[i];
static void reduct_expr_build_list(reduct_compiler_t *compiler, reduct_item_t *list, reduct_expr_t *out)
static void reduct_expr_build_atom(reduct_compiler_t *compiler, reduct_item_t *atom, reduct_expr_t *out)
Core definitions and structures.
reduct_bool_t
Boolean type.
#define REDUCT_CONTAINER_OF(_ptr, _type, _member)
Container of macro.
reduct_uint64_t reduct_handle_t
Handle type.
#define REDUCT_ASSERT(_cond)
#define REDUCT_MEMSET(_ptr, _val, _size)
reduct_const_t constant
Constant index.
#define REDUCT_EXPR_CONST_ITEM(_compiler, _item)
Create a REDUCT_MODE_CONST mode expression for a specific item.
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_reg_free(reduct_compiler_t *compiler, reduct_reg_t reg)
Free a register.
reduct_mode_t mode
Expression mode.
#define REDUCT_EXPR_NIL(_compiler)
Create a REDUCT_MODE_CONST mode expression for the nil constant.
#define REDUCT_REG_SET_ALLOCATED(_compiler, _reg)
Set a register as allocated.
static void reduct_expr_done(reduct_compiler_t *compiler, reduct_expr_t *expr)
Free resources associated with an expression descriptor.
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.
#define REDUCT_EXPR_CONST(_const)
Create a REDUCT_MODE_CONST mode expression.
reduct_uint16_t localCount
The amount of local variables.
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 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.
static reduct_reg_t reduct_reg_get_base(reduct_compiler_t *compiler)
Get the first unallocated register index.
#define REDUCT_REG_CLEAR_ALLOCATED(_compiler, _reg)
Clear a register's allocation status.
reduct_local_t locals[REDUCT_REGISTER_MAX]
The local variables.
struct reduct_compiler * enclosing
The enclosing compiler context, or REDUCT_NULL.
static void reduct_compile_return(reduct_compiler_t *compiler, reduct_expr_t *expr)
Emits a REDUCT_OPCODE_RET instruction.
reduct_uint64_t regAlloc[REDUCT_REGISTER_MAX/64]
Bitmask of allocated registers.
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_expr_t expr
The expression representing the local's value.
reduct_reg_t reg
Register index.
#define REDUCT_REG_CLEAR_LOCAL(_compiler, _reg)
Clear a register's local status.
reduct_atom_t * name
The name of the local variable.
reduct_t * reduct
The Reduct structure.
#define REDUCT_EXPR_TARGET(_reg)
Create a REDUCT_MODE_TARGET mode expression.
#define REDUCT_REG_IS_LOCAL(_compiler, _reg)
Check if a register is a local.
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.
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.
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.
#define REDUCT_EXPR_NONE()
Create a REDUCT_MODE_NONE mode expression.
reduct_function_t * function
The function being compiled.
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 reduct_reg_t reduct_reg_alloc(reduct_compiler_t *compiler)
Allocate a new register.
REDUCT_API void reduct_compiler_deinit(reduct_compiler_t *compiler)
Deinitialize a compiler context.
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 r...
#define REDUCT_LOCAL_IS_DEFINED(_local)
Check if a local variable has finished being defined.
reduct_item_t * lastItem
The last item processed by the compiler, used for error reporting.
#define REDUCT_REG_SET_LOCAL(_compiler, _reg)
Set a register as a local.
static void reduct_compile_append(reduct_compiler_t *compiler, reduct_reg_t target, reduct_expr_t *expr)
Emits an REDUCT_OPCODE_APPEND instruction.
#define REDUCT_REG_IS_ALLOCATED(_compiler, _reg)
Check if a register is allocated.
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.
reduct_uint64_t regLocal[REDUCT_REGISTER_MAX/64]
Bitmask of registers used by locals.
REDUCT_API reduct_reg_t reduct_reg_alloc_range(reduct_compiler_t *compiler, reduct_uint32_t count)
Allocate a range of registers.
#define REDUCT_EXPR_REG(_reg)
Create a REDUCT_MODE_REG mode expression.
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.
#define REDUCT_ERROR_COMPILE(_compiler, _item,...)
Throw a compile error using the jump buffer in the error structure.
#define REDUCT_CONST_SLOT_CAPTURE(_capture)
Create a constant slot containing a variable name to be captured.
reduct_uint16_t reduct_const_t
Constant index type.
REDUCT_API reduct_function_t * reduct_function_new(struct reduct *reduct)
Create a new function.
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.
#define REDUCT_GC_RETAIN_ITEM(_reduct, _item)
Retain a item, preventing it from being collected by the GC.
#define REDUCT_HANDLE_TO_ITEM(_handle)
Get the item pointer of a handle.
#define REDUCT_REG_INVALID
Invalid register value.
reduct_uint16_t reduct_reg_t
Register type.
#define REDUCT_REGISTER_MAX
The max number of registers per function frame.
@ REDUCT_MODE_REG
Register operand mode.
@ REDUCT_MODE_NONE
Invalid mode.
@ REDUCT_MODE_CONST
Constant operand mode.
reduct_intrinsic_handler_t reductIntrinsicHandlers[REDUCT_INTRINSIC_MAX]
Intrinsic handler functions array.
void reduct_intrinsic_block_generic(struct reduct_compiler *compiler, struct reduct_item *list, reduct_uint32_t startIdx, struct reduct_expr *out)
Compiles a block of expressions (like a do block).
void(* reduct_intrinsic_handler_t)(struct reduct_compiler *compiler, struct reduct_item *expr, struct reduct_expr *out)
Intrinsic handler function type.
#define REDUCT_ITEM_TYPE_LIST
A list.
#define REDUCT_ITEM_TYPE_ATOM
An atom.
#define REDUCT_ITEM_FLAG_INT_SHAPED
Item is an integer shaped atom.
#define REDUCT_ITEM_FLAG_FLOAT_SHAPED
Item is a float shaped atom.
#define REDUCT_ITEM_FLAG_NATIVE
Item is an atom and a native function.
#define REDUCT_ITEM_FLAG_INTRINSIC
Item is an atom and a intrinsic.
#define REDUCT_ITEM_FLAG_QUOTED
Item is a quoted atom.
Bytecode instruction format.
#define REDUCT_LIST_FOR_EACH_AT(_handle, _list, _start)
Macro for iterating over elements in a list starting from a specific index.
#define REDUCT_LIST_FOR_EACH(_handle, _list)
Macro for iterating over all elements in a list.
REDUCT_API struct reduct_item * reduct_list_nth_item(struct reduct *reduct, reduct_list_t *list, reduct_size_t index)
Get the nth element of the list as an item.
reduct_uint32_t length
The length of the string (must be first, check the reduct_item_t structure).
char * string
Pointer to the string.
reduct_intrinsic_t intrinsic
Cached intrinsic, item must have REDUCT_ITEM_FLAG_INTRINSIC.
struct reduct_atom * name
struct reduct_item * item
Expression descriptor structure.
Compiled function structure.
reduct_const_slot_t * constants
The array of constant slots forming the constant template.
reduct_list_t list
A list.
reduct_uint32_t length
Common length for the item. (Stored in the union to save space due to padding rules....
struct reduct_input * input
The parsed input that created this item.
reduct_item_flags_t flags
Flags for the item.
reduct_atom_t atom
An atom.
reduct_item_type_t type
The type of the item.
reduct_constant_t constants[REDUCT_CONSTANTS_MAX]
reduct_uint32_t constantCount