BFC-Compiler
A C23 Brainfuck compiler
Loading...
Searching...
No Matches
bfc_ir.c File Reference

IR construction, optimization, and destruction. More...

#include "bfc_ir.h"
#include <stdckdint.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include "bfc_config.h"
#include "bfc_error.h"
#include "bfc_memory.h"

Data Structures

struct  bfc_ir_stack_t
 Construction-only stack of currently active nested IR blocks. More...

Functions

bfc_ir_instr_t bfc_ir_make_imm_instr (bfc_ir_token_type_t const ir_token_type, int64_t const imm)
 Constructs an immediate IR instruction.
bfc_ir_instr_t bfc_ir_make_zero_instr (bfc_ir_token_type_t const ir_token_type)
 Constructs an instruction with a zeroed operand union.
bfc_error_t bfc_ir_create (bfc_ir_block_t **root_block, bfc_token_stream_t const *const tok_stream)
 Builds a nested IR tree using an explicit stack of active loop blocks.
bfc_error_t bfc_ir_optimize_rep (bfc_ir_block_t **ir_block)
 Combines adjacent arithmetic/movement instructions and converts clear loops to IR_SET 0.
void bfc_ir_destroy (bfc_ir_block_t **proot_block)
 Recursively releases an IR block and all owned loop bodies.

Detailed Description

IR construction, optimization, and destruction.

Builds nested blocks, grows typed arrays safely, folds repeated operations, recognizes clear loops, and manages recursive ownership.

Function Documentation

◆ bfc_ir_create()

bfc_error_t bfc_ir_create ( bfc_ir_block_t ** root_block,
bfc_token_stream_t const *const tok_stream )

Builds a nested IR tree using an explicit stack of active loop blocks.

Builds a nested IR tree from the token stream.

Builds a nested IR tree using an explicit stack of active loop blocks.

Parameters
[out]root_blockReceives the allocated root block.
[in]tok_streamValidated token stream.
Returns
BFC_ERR_OK on success; otherwise an allocation error.
Note
Release the tree with bfc_ir_destroy().

◆ bfc_ir_destroy()

void bfc_ir_destroy ( bfc_ir_block_t ** proot_block)

Recursively releases an IR block and all owned loop bodies.

Recursively destroys an IR block and all nested loop bodies.

Recursively releases an IR block and all owned loop bodies.

◆ bfc_ir_make_imm_instr()

bfc_ir_instr_t bfc_ir_make_imm_instr ( bfc_ir_token_type_t const ir_token_type,
int64_t const imm )
nodiscard

Constructs an immediate IR instruction.

Constructs an immediate-valued IR instruction.

Constructs an immediate IR instruction.

◆ bfc_ir_make_zero_instr()

bfc_ir_instr_t bfc_ir_make_zero_instr ( bfc_ir_token_type_t const ir_token_type)
nodiscard

Constructs an instruction with a zeroed operand union.

Constructs an IR instruction with a zero-initialized operand union.

Constructs an instruction with a zeroed operand union.

◆ bfc_ir_optimize_rep()

bfc_error_t bfc_ir_optimize_rep ( bfc_ir_block_t ** ir_block)

Combines adjacent arithmetic/movement instructions and converts clear loops to IR_SET 0.

Optimizes repeated operations and recognized clear loops in place.

Combines adjacent arithmetic/movement instructions and converts clear loops to IR_SET 0.

Parameters
[in,out]ir_blockAddress of the owned IR block pointer.
Returns
BFC_ERR_OK on success; otherwise an allocation error.