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

Generic IR-to-assembly code generation. More...

#include "bfc_codegen_internal.h"
#include <stdarg.h>
#include <stdckdint.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bfc_config.h"
#include "bfc_memory.h"

Functions

static const bfc_backend_tbfc_backend_select (bfc_target_t target)
 Selects the immutable backend descriptor for a target.
static bfc_error_t bfc_codegen_emit_loop (bfc_asm_t *asm_prog, const bfc_ir_block_t *body)
 Recursively lowers one loop using backend-specific branch callbacks.
bfc_error_t bfc_codegen_emit_text (bfc_asm_t *asm_prog, const char *text)
 Appends null-terminated text while preserving the assembly-buffer invariant.
bfc_error_t bfc_codegen_emit_block (bfc_asm_t *asm_prog, const bfc_ir_block_t *ir_block)
 Dispatches every IR operation through the active backend.
bfc_error_t bfc_codegen_emitf (bfc_asm_t *asm_prog, const char *format,...)
 Formats one assembly fragment into a bounded temporary buffer and appends it.
bfc_error_t bfc_codegen (bfc_asm_t **out_asm, const bfc_ir_block_t *ir_block, bfc_target_t target)
 Allocates assembly state and orchestrates complete target-specific emission.
void bfc_asm_destroy (bfc_asm_t **pasm_prog)
 Releases generated assembly state and nulls the caller pointer.
bfc_error_t bfc_asm_write_file (const bfc_asm_t *asm_prog, const char *path)
 Writes exactly the generated assembly length to a binary-mode file.

Detailed Description

Generic IR-to-assembly code generation.

Selects backends, owns the assembly buffer, recursively lowers loops, dispatches IR instructions, and writes assembly files.

Function Documentation

◆ bfc_asm_destroy()

void bfc_asm_destroy ( bfc_asm_t ** asm_prog)

Releases generated assembly state and nulls the caller pointer.

Releases an assembly object and nulls the caller pointer.

Releases generated assembly state and nulls the caller pointer.

Parameters
[in,out]asm_progAddress of the owned assembly pointer.

◆ bfc_asm_write_file()

bfc_error_t bfc_asm_write_file ( const bfc_asm_t * asm_prog,
const char * path )

Writes exactly the generated assembly length to a binary-mode file.

Writes generated assembly bytes to a file.

Writes exactly the generated assembly length to a binary-mode file.

Parameters
[in]asm_progCompleted assembly object.
[in]pathDestination file path.
Returns
BFC_ERR_OK on success; otherwise an I/O error.

◆ bfc_backend_select()

const bfc_backend_t * bfc_backend_select ( bfc_target_t target)
static

Selects the immutable backend descriptor for a target.

◆ bfc_codegen()

bfc_error_t bfc_codegen ( bfc_asm_t ** out_asm,
const bfc_ir_block_t * ir_block,
bfc_target_t target )

Allocates assembly state and orchestrates complete target-specific emission.

Generates target-specific assembly from an optimized IR tree.

Allocates assembly state and orchestrates complete target-specific emission.

Parameters
[out]out_asmReceives the allocated assembly object on success.
[in]ir_blockRoot IR block.
[in]targetRequested code-generation target.
Returns
BFC_ERR_OK on success; otherwise a target, allocation, or emission error.
Note
Release the result with bfc_asm_destroy().

◆ bfc_codegen_emit_block()

bfc_error_t bfc_codegen_emit_block ( bfc_asm_t * asm_prog,
const bfc_ir_block_t * ir_block )

Dispatches every IR operation through the active backend.

Recursively emits every instruction in an IR block.

Dispatches every IR operation through the active backend.

◆ bfc_codegen_emit_loop()

bfc_error_t bfc_codegen_emit_loop ( bfc_asm_t * asm_prog,
const bfc_ir_block_t * body )
static

Recursively lowers one loop using backend-specific branch callbacks.

◆ bfc_codegen_emit_text()

bfc_error_t bfc_codegen_emit_text ( bfc_asm_t * asm_prog,
const char * text )

Appends null-terminated text while preserving the assembly-buffer invariant.

Appends raw text to the assembly buffer.

Appends null-terminated text while preserving the assembly-buffer invariant.

◆ bfc_codegen_emitf()

bfc_error_t bfc_codegen_emitf ( bfc_asm_t * asm_prog,
const char * format,
... )

Formats one assembly fragment into a bounded temporary buffer and appends it.

Formats and appends one assembly fragment.

Formats one assembly fragment into a bounded temporary buffer and appends it.