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

Brainfuck intermediate representation interface. More...

#include <stddef.h>
#include <stdint.h>
#include "bfc_error.h"
#include "bfc_token.h"

Go to the source code of this file.

Data Structures

struct  bfc_ir_instr_t
 One IR instruction. More...
struct  bfc_ir_block
 Owning, dynamically sized sequence of IR instructions. More...

Typedefs

typedef struct bfc_ir_block bfc_ir_block_t

Enumerations

enum  bfc_ir_token_type_t {
  IR_ADD , IR_MOVE , IR_PUT , IR_GET ,
  IR_SET , IR_LOOP
}
 Operations understood by the optimizer and code generator.

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-valued IR instruction.
bfc_ir_instr_t bfc_ir_make_zero_instr (bfc_ir_token_type_t const ir_token_type)
 Constructs an IR instruction with a zero-initialized 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 from the token stream.
bfc_error_t bfc_ir_optimize_rep (bfc_ir_block_t **ir_block)
 Optimizes repeated operations and recognized clear loops in place.
void bfc_ir_destroy (bfc_ir_block_t **proot_block)
 Recursively destroys an IR block and all nested loop bodies.

Detailed Description

Brainfuck intermediate representation interface.

Defines nested IR blocks, instructions, construction, optimization, and recursive destruction.

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 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().

Builds a nested IR tree from the token stream.

◆ bfc_ir_destroy()

void bfc_ir_destroy ( bfc_ir_block_t ** proot_block)

Recursively destroys an IR block and all nested loop bodies.

Recursively releases an IR block and all owned loop bodies.

Recursively destroys an IR block and all nested 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-valued IR instruction.

Constructs an immediate IR instruction.

Constructs an immediate-valued 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 IR instruction with a zero-initialized operand union.

Constructs an instruction with a zeroed operand union.

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

◆ bfc_ir_optimize_rep()

bfc_error_t bfc_ir_optimize_rep ( bfc_ir_block_t ** ir_block)

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.

Optimizes repeated operations and recognized clear loops in place.