Fun 0.41.5
The programming language that makes You have fun
Loading...
Searching...
No Matches
vm.c File Reference

Core virtual machine implementation and opcode dispatch for Fun. More...

#include <math.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "iter.c"
#include "map.c"
#include "string.c"
#include "value.h"
#include "vm.h"
#include "extensions/curl.c"
#include "extensions/ini.c"
#include "extensions/json.c"
#include "extensions/openssl.c"
#include "extensions/pcre2.c"
#include "extensions/pcsc.c"
#include "extensions/sqlite.c"
#include "extensions/xml2.c"
#include "vm/os/thread_common.c"
#include <setjmp.h>
#include "vm/arithmetic/add.c"
#include "vm/arithmetic/div.c"
#include "vm/arithmetic/mul.c"
#include "vm/arithmetic/sub.c"
#include "vm/arrays/apop.c"
#include "vm/arrays/clear.c"
#include "vm/arrays/contains.c"
#include "vm/arrays/enumerate.c"
#include "vm/arrays/index_get.c"
#include "vm/arrays/index_of.c"
#include "vm/arrays/index_set.c"
#include "vm/arrays/insert.c"
#include "vm/arrays/join.c"
#include "vm/arrays/make_array.c"
#include "vm/arrays/push.c"
#include "vm/arrays/remove.c"
#include "vm/arrays/set.c"
#include "vm/arrays/slice.c"
#include "vm/arrays/zip.c"
#include "vm/bitwise/band.c"
#include "vm/bitwise/bnot.c"
#include "vm/bitwise/bor.c"
#include "vm/bitwise/bxor.c"
#include "vm/bitwise/rol.c"
#include "vm/bitwise/ror.c"
#include "vm/bitwise/shl.c"
#include "vm/bitwise/shr.c"
#include "vm/core/call.c"
#include "vm/core/dup.c"
#include "vm/core/exit.c"
#include "vm/core/halt.c"
#include "vm/core/jump.c"
#include "vm/core/jump_if_false.c"
#include "vm/core/load_const.c"
#include "vm/core/load_global.c"
#include "vm/core/load_local.c"
#include "vm/core/nop.c"
#include "vm/core/pop.c"
#include "vm/core/return.c"
#include "vm/core/store_global.c"
#include "vm/core/store_local.c"
#include "vm/core/swap.c"
#include "vm/core/throw.c"
#include "vm/core/try_pop.c"
#include "vm/core/try_push.c"
#include "vm/io/input_line.c"
#include "vm/io/read_file.c"
#include "vm/io/write_file.c"
#include "vm/logic/and.c"
#include "vm/logic/eq.c"
#include "vm/logic/gt.c"

Go to the source code of this file.

Macros

#define _POSIX_C_SOURCE   200809L
#define _XOPEN_SOURCE   700
#define fprintf   fun_vm_fprintf
#define exit(code)

Functions

char * preprocess_includes (const char *src)
 Public wrapper to preprocess includes without a current path.
int map_expanded_line_to_include_path (const char *path, int line, char *out_path, size_t out_path_cap, int *out_line)
 Map a line number in expanded source back to original include path/line.
void vm_raise_error (VM *vm, const char *msg)
 Raise a runtime error inside the VM, honoring try/catch/finally.
void vm_clear_output (VM *vm)
 Clear the VM's buffered output values and partial flags.
void vm_free (VM *vm)
 Free resources owned directly by the VM structure.
void vm_reset (VM *vm)
 Reset the VM to a clean state.
void vm_dump_globals (VM *vm)
 Print all non-nil global variables to stdout for debugging.
void vm_debug_reset (VM *vm)
 Reset debugger state: breakpoints and stepping controls.
int vm_debug_add_breakpoint (VM *vm, const char *file, int line)
 Add a source breakpoint.
int vm_debug_delete_breakpoint (VM *vm, int id)
 Delete a breakpoint by id.
void vm_debug_clear_breakpoints (VM *vm)
 Remove all breakpoints from the VM.
void vm_debug_list_breakpoints (VM *vm)
 Print active breakpoints to stdout.
void vm_debug_request_step (VM *vm)
 Request single-step execution (stop after next instruction).
void vm_debug_request_next (VM *vm)
 Request step-over (stop after next instruction in current frame).
void vm_debug_request_finish (VM *vm)
 Request finish (run until the current frame returns).
void vm_debug_request_continue (VM *vm)
 Resume normal execution (clear stepping state and stop flag).
int64_t vm_pop_i64 (VM *vm)
 Pop a numeric Value and convert it to a 64-bit integer (C ABI helper).
void vm_push_i64 (VM *vm, int64_t v)
 Push a 64-bit integer as a VM int Value (C ABI helper).
size_t vm_sizeof (void)
 Return sizeof(VM) for external FFI consumers.
size_t vm_value_sizeof (void)
 Return sizeof(Value) for external FFI consumers.
void * vm_as_mut_ptr (VM *vm)
 Cast the VM pointer to an opaque mutable void* (unsafe FFI helper).
size_t vm_offset_of_exit_code (void)
 Obtain offsetof(VM, exit_code) for FFI struct field access.
size_t vm_offset_of_sp (void)
 Obtain offsetof(VM, sp) for FFI struct field access.
size_t vm_offset_of_stack (void)
 Obtain offsetof(VM, stack) for FFI struct field access.
size_t vm_offset_of_globals (void)
 Obtain offsetof(VM, globals) for FFI struct field access.
void vm_init (VM *vm)
 Initialize a VM instance to its default state.
void vm_print_output (VM *vm)
 Print the VM's buffered output values to stdout.

Detailed Description

Core virtual machine implementation and opcode dispatch for Fun.

Defines the VM state, stack helpers, debugger/stepping support, built-in opcode handlers (included as amalgamated C files), platform I/O helpers, and the main interpreter loop that executes bytecode produced by the Fun compiler. This file is the central runtime of the language.

Definition in file vm.c.

Macro Definition Documentation

◆ _POSIX_C_SOURCE

#define _POSIX_C_SOURCE   200809L

Definition at line 24 of file vm.c.

◆ _XOPEN_SOURCE

#define _XOPEN_SOURCE   700

Definition at line 27 of file vm.c.

◆ exit

#define exit(code)
Value:
fun_vm_exit(code)
int code
Definition proc_system.c:31

Definition at line 230 of file vm.c.

◆ fprintf

#define fprintf   fun_vm_fprintf

Definition at line 200 of file vm.c.

Function Documentation

◆ map_expanded_line_to_include_path()

int map_expanded_line_to_include_path(const char *path,
intline,
char *out_path,
size_tout_path_cap,
int *out_line )
extern

Map a line number in expanded source back to original include path/line.

Scans the expanded text for the nearest preceding __include_begin__ marker and computes the corresponding inner line number.

Parameters
pathPath to the original top-level file that was expanded.
line1-based line number in the expanded text.
out_pathOutput buffer for the resolved file path.
out_path_capCapacity of out_path.
out_lineReceives 1-based line number within resolved file.
Returns
1 on success, 0 on failure.

Definition at line 996 of file parser_utils.c.

Here is the call graph for this function:

◆ preprocess_includes()

char * preprocess_includes(const char *src)
extern

Public wrapper to preprocess includes without a current path.

Definition at line 965 of file parser_utils.c.

Here is the caller graph for this function:

◆ vm_as_mut_ptr()

void * vm_as_mut_ptr(VM *vm)

Cast the VM pointer to an opaque mutable void* (unsafe FFI helper).

Get a mutable byte pointer to the VM object. Extremely unsafe; for low-level FFI use only.

Parameters
vmVM instance pointer.
Returns
The same pointer reinterpreted as void*.

Definition at line 650 of file vm.c.

◆ vm_clear_output()

void vm_clear_output(VM *vm)

Clear the VM's buffered output values and partial flags.

Clear the buffered output captured by the VM.

Frees any dynamic storage held by buffered output Values and resets the output counters and partial line indicators.

Parameters
vmVM instance whose output buffer should be cleared.

Definition at line 349 of file vm.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ vm_debug_add_breakpoint()

int vm_debug_add_breakpoint(VM *vm,
const char *file,
intline )

Add a source breakpoint.

Parameters
vmVM instance.
fileSource file path (must not be NULL).
lineOne-based source line number (> 0).
Returns
Breakpoint id (>=0) on success, -1 on failure (invalid args or full).

Definition at line 453 of file vm.c.

◆ vm_debug_clear_breakpoints()

void vm_debug_clear_breakpoints(VM *vm)

Remove all breakpoints from the VM.

Parameters
vmVM instance.

Definition at line 492 of file vm.c.

Here is the call graph for this function:

◆ vm_debug_delete_breakpoint()

int vm_debug_delete_breakpoint(VM *vm,
intid )

Delete a breakpoint by id.

Compacts the internal breakpoint list to keep ids dense.

Parameters
vmVM instance.
idBreakpoint identifier previously returned by add.
Returns
1 if deleted, 0 if id was invalid.

Definition at line 472 of file vm.c.

Here is the call graph for this function:

◆ vm_debug_list_breakpoints()

void vm_debug_list_breakpoints(VM *vm)

Print active breakpoints to stdout.

Parameters
vmVM instance.

Definition at line 501 of file vm.c.

◆ vm_debug_request_continue()

void vm_debug_request_continue(VM *vm)

Resume normal execution (clear stepping state and stop flag).

Parameters
vmVM instance.

Definition at line 551 of file vm.c.

◆ vm_debug_request_finish()

void vm_debug_request_finish(VM *vm)

Request finish (run until the current frame returns).

Parameters
vmVM instance.

Definition at line 540 of file vm.c.

◆ vm_debug_request_next()

void vm_debug_request_next(VM *vm)

Request step-over (stop after next instruction in current frame).

Parameters
vmVM instance.

Definition at line 528 of file vm.c.

◆ vm_debug_request_step()

void vm_debug_request_step(VM *vm)

Request single-step execution (stop after next instruction).

Parameters
vmVM instance.

Definition at line 517 of file vm.c.

◆ vm_debug_reset()

void vm_debug_reset(VM *vm)

Reset debugger state: breakpoints and stepping controls.

Clears all breakpoints, disables stepping modes, and resets counters.

Parameters
vmVM instance with debugger state to reset.

Definition at line 429 of file vm.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ vm_dump_globals()

void vm_dump_globals(VM *vm)

Print all non-nil global variables to stdout for debugging.

Print non-nil globals (index and value) to stdout.

Parameters
vmVM whose globals should be dumped.

Definition at line 408 of file vm.c.

Here is the call graph for this function:

◆ vm_free()

void vm_free(VM *vm)

Free resources owned directly by the VM structure.

Free all resources owned by the VM (globals, frames, output buffers). The VM object itself is not freed when allocated on the stack.

Currently a no-op because the VM does not allocate persistent internal resources outside frames, globals and outputs, which are managed elsewhere.

Parameters
vmVM instance to free resources for.

Definition at line 367 of file vm.c.

Here is the caller graph for this function:

◆ vm_init()

void vm_init(VM *vm)

Initialize a VM instance to its default state.

Initialize a VM instance to zero/initial state.

Resets stack/frame pointers, output buffers, instruction counters, debugger state and globals. Does not allocate memory.

Parameters
vmVM instance to initialize.

Definition at line 714 of file vm.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ vm_offset_of_exit_code()

size_t vm_offset_of_exit_code(void)

Obtain offsetof(VM, exit_code) for FFI struct field access.

Returns
Byte offset of the exit_code field within VM.

Definition at line 659 of file vm.c.

◆ vm_offset_of_globals()

size_t vm_offset_of_globals(void)

Obtain offsetof(VM, globals) for FFI struct field access.

Returns
Byte offset of the globals field within VM.

Definition at line 686 of file vm.c.

◆ vm_offset_of_sp()

size_t vm_offset_of_sp(void)

Obtain offsetof(VM, sp) for FFI struct field access.

Returns
Byte offset of the sp field within VM.

Definition at line 668 of file vm.c.

◆ vm_offset_of_stack()

size_t vm_offset_of_stack(void)

Obtain offsetof(VM, stack) for FFI struct field access.

Returns
Byte offset of the stack field within VM.

Definition at line 677 of file vm.c.

◆ vm_pop_i64()

int64_t vm_pop_i64(VM *vm)

Pop a numeric Value and convert it to a 64-bit integer (C ABI helper).

Accepts int or float Values on the stack. Other types raise a runtime type error. The popped Value is freed.

Parameters
vmVM instance.
Returns
The numeric value converted to int64_t.

Definition at line 598 of file vm.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ vm_print_output()

void vm_print_output(VM *vm)

Print the VM's buffered output values to stdout.

Print buffered output entries to stdout (debug aid).

Emits a newline after each value unless the corresponding partial flag is set.

Parameters
vmVM instance whose output should be printed.

Definition at line 797 of file vm.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ vm_push_i64()

void vm_push_i64(VM *vm,
int64_tv )

Push a 64-bit integer as a VM int Value (C ABI helper).

Parameters
vmVM instance.
vInteger value to push.

Definition at line 621 of file vm.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ vm_raise_error()

void vm_raise_error(VM *vm,
const char *msg )

Raise a runtime error inside the VM, honoring try/catch/finally.

Raise a runtime error honoring active try/catch/finally handlers. If a try handler is active in the current frame, control jumps to it with an error string pushed on the stack. Otherwise, prints the error (with location) and terminates execution.

If the current frame has a pending try handler, control is transferred to that handler and the error message is pushed onto the stack for the catch clause. If no handler exists, the error is printed and the VM is stopped.

Parameters
vmPointer to the VM instance.
msgHuman-readable error message (may be NULL).

Definition at line 248 of file vm.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ vm_reset()

void vm_reset(VM *vm)

Reset the VM to a clean state.

Reset VM to initial state, freeing globals/locals/output. The VM object remains valid for reuse after this call.

Pops all frames (releasing local variables), clears the operand stack and globals, resets the output buffer and debugger state, and zeros the exit code.

Parameters
vmVM instance to reset.

Definition at line 382 of file vm.c.

Here is the call graph for this function:

◆ vm_sizeof()

size_t vm_sizeof(void)

Return sizeof(VM) for external FFI consumers.

Returns
Size of the VM struct in bytes.

Definition at line 631 of file vm.c.

◆ vm_value_sizeof()

size_t vm_value_sizeof(void)

Return sizeof(Value) for external FFI consumers.

Returns
Size of the Value struct in bytes.

Definition at line 640 of file vm.c.