Fun API Documentation 0.42.1
The programming language that makes you have fun!
Loading...
Searching...
No Matches
bytecode.c File Reference

Bytecode container utilities: creation, mutation, dump helpers. More...

#include "bytecode.h"
#include <stdio.h>
#include <stdlib.h>
Include dependency graph for bytecode.c:

Go to the source code of this file.

Functions

Bytecodebytecode_new (void)
 Allocate and initialize an empty Bytecode object.
int bytecode_add_constant (Bytecode *bc, Value v)
 Append a constant to a Bytecode's constant table with de-duplication.
int bytecode_add_instruction (Bytecode *bc, OpCode op, int32_t operand)
 Append a single instruction to the instruction stream.
void bytecode_set_operand (Bytecode *bc, int idx, int32_t operand)
 Patch the operand of a previously emitted instruction.
void bytecode_free (Bytecode *bc)
 Free a Bytecode and all memory it owns.
static const char * opcode_name (OpCode op)
 Convert an opcode enum to a short mnemonic string.
void bytecode_dump (const Bytecode *bc)
 Print a human-readable dump of constants and instructions to stdout.

Detailed Description

Bytecode container utilities: creation, mutation, dump helpers.

Definition in file bytecode.c.

Function Documentation

◆ bytecode_add_constant()

int bytecode_add_constant ( Bytecode * bc,
Value v )

Append a constant to a Bytecode's constant table with de-duplication.

The value is compared against existing constants and if an equal constant is already present, its index is returned without modifying the table. Equality uses value_equals which supports numeric cross-type (int/float) equality and string content equality. The caller retains ownership of v in all cases. When a new constant is inserted, a deep copy is stored in the table.

Parameters
bcTarget bytecode (must not be NULL).
vValue to store (copied on insert).
Returns
The index of the stored or matched existing constant (zero-based).

Definition at line 51 of file bytecode.c.

◆ bytecode_add_instruction()

int bytecode_add_instruction ( Bytecode * bc,
OpCode op,
int32_t operand )

Append a single instruction to the instruction stream.

Parameters
bcTarget bytecode (must not be NULL).
opOpcode to emit.
operandOperand value (semantics depend on opcode).
Returns
The index of the emitted instruction (zero-based).

Definition at line 74 of file bytecode.c.

◆ bytecode_dump()

void bytecode_dump ( const Bytecode * bc)

Print a human-readable dump of constants and instructions to stdout.

Intended for debugging and tests. Formats constants with print_value() and shows each instruction index, mnemonic and operand.

Parameters
bcBytecode to dump (prints "<null bytecode>" if NULL).

Definition at line 435 of file bytecode.c.

◆ bytecode_free()

void bytecode_free ( Bytecode * bc)

Free a Bytecode and all memory it owns.

Frees constants (deep), instruction array, and metadata strings. Accepts NULL and is then a no-op.

Parameters
bcBytecode to free (may be NULL).

Definition at line 104 of file bytecode.c.

◆ bytecode_new()

Bytecode * bytecode_new ( void )

Allocate and initialize an empty Bytecode object.

Initializes instruction and constant arrays to empty and clears metadata. Caller owns the returned pointer and must free it with bytecode_free().

Returns
Newly allocated Bytecode*, or NULL on allocation failure.

Definition at line 27 of file bytecode.c.

◆ bytecode_set_operand()

void bytecode_set_operand ( Bytecode * bc,
int idx,
int32_t operand )

Patch the operand of a previously emitted instruction.

Silently ignores out-of-bounds indices.

Parameters
bcTarget bytecode.
idxInstruction index to patch.
operandNew operand value.

Definition at line 90 of file bytecode.c.

◆ opcode_name()

const char * opcode_name ( OpCode op)
static

Convert an opcode enum to a short mnemonic string.

Parameters
opOpcode value.
Returns
Read-only C string with the mnemonic, or "???" if unknown.

Definition at line 122 of file bytecode.c.