Fun 0.41.5
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.
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.

The value is deep-copied into the table; the caller retains ownership of v and may free it independently.

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

Definition at line 48 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 62 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 423 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 92 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 78 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 110 of file bytecode.c.