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

Definitions for the Fun VM bytecode: opcodes, instruction format, and bytecode container API. More...

#include "value.h"
#include <stdint.h>
Include dependency graph for bytecode.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  Instruction
struct  Bytecode

Macros

#define OPCODE_COUNT   (OP_CPP_ADD + 1)

Typedefs

typedef struct Bytecode Bytecode

Enumerations

enum  OpCode {
  OP_NOP , OP_LOAD_CONST , OP_LOAD_LOCAL , OP_STORE_LOCAL ,
  OP_LOAD_GLOBAL , OP_STORE_GLOBAL , OP_ADD , OP_SUB ,
  OP_MUL , OP_DIV , OP_LT , OP_LTE ,
  OP_GT , OP_GTE , OP_EQ , OP_NEQ ,
  OP_POP , OP_JUMP , OP_JUMP_IF_FALSE , OP_CALL ,
  OP_RETURN , OP_PRINT , OP_ECHO , OP_HALT ,
  OP_LINE , OP_MOD , OP_AND , OP_OR ,
  OP_NOT , OP_DUP , OP_SWAP , OP_MAKE_ARRAY ,
  OP_INDEX_GET , OP_INDEX_SET , OP_LEN , OP_PUSH ,
  OP_APOP , OP_SET , OP_INSERT , OP_REMOVE ,
  OP_SLICE , OP_TO_NUMBER , OP_TO_STRING , OP_CAST ,
  OP_TYPEOF , OP_UCLAMP , OP_SCLAMP , OP_SPLIT ,
  OP_JOIN , OP_SUBSTR , OP_FIND , OP_REGEX_MATCH ,
  OP_REGEX_SEARCH , OP_REGEX_REPLACE , OP_CONTAINS , OP_INDEX_OF ,
  OP_CLEAR , OP_ENUMERATE , OP_ZIP , OP_MIN ,
  OP_MAX , OP_CLAMP , OP_ABS , OP_POW ,
  OP_RANDOM_SEED , OP_RANDOM_INT , OP_MAKE_MAP , OP_KEYS ,
  OP_VALUES , OP_HAS_KEY , OP_READ_FILE , OP_WRITE_FILE ,
  OP_ENV , OP_INPUT_LINE , OP_PROC_RUN , OP_PROC_SYSTEM ,
  OP_TIME_NOW_MS , OP_CLOCK_MONO_MS , OP_KCGI_PARSE , OP_KCGI_REPLY_START ,
  OP_KCGI_WRITE , OP_KCGI_END , OP_DATE_FORMAT , OP_ENV_ALL ,
  OP_FUN_VERSION , OP_THREAD_SPAWN , OP_THREAD_JOIN , OP_SLEEP_MS ,
  OP_RANDOM_NUMBER , OP_BAND , OP_BOR , OP_BXOR ,
  OP_BNOT , OP_SHL , OP_SHR , OP_ROTL ,
  OP_ROTR , OP_JSON_PARSE , OP_JSON_STRINGIFY , OP_JSON_FROM_FILE ,
  OP_JSON_TO_FILE , OP_CURL_GET , OP_CURL_POST , OP_CURL_DOWNLOAD ,
  OP_SQLITE_OPEN , OP_SQLITE_CLOSE , OP_SQLITE_EXEC , OP_SQLITE_QUERY ,
  OP_REDIS_CONNECT , OP_REDIS_CMD , OP_REDIS_CLOSE , OP_PCSC_ESTABLISH ,
  OP_PCSC_RELEASE , OP_PCSC_LIST_READERS , OP_PCSC_CONNECT , OP_PCSC_DISCONNECT ,
  OP_PCSC_TRANSMIT , OP_PCRE2_TEST , OP_PCRE2_MATCH , OP_PCRE2_FINDALL ,
  OP_OPENSSL_MD5 , OP_OPENSSL_SHA256 , OP_OPENSSL_SHA512 , OP_OPENSSL_RIPEMD160 ,
  OP_INI_LOAD , OP_INI_FREE , OP_INI_GET_STRING , OP_INI_GET_INT ,
  OP_INI_GET_DOUBLE , OP_INI_GET_BOOL , OP_INI_SET , OP_INI_UNSET ,
  OP_INI_SAVE , OP_XML_PARSE , OP_XML_ROOT , OP_XML_NAME ,
  OP_XML_TEXT , OP_SOCK_TCP_LISTEN , OP_SOCK_TCP_ACCEPT , OP_SOCK_TCP_CONNECT ,
  OP_SOCK_SEND , OP_SOCK_RECV , OP_SOCK_CLOSE , OP_SOCK_UNIX_LISTEN ,
  OP_SOCK_UNIX_CONNECT , OP_FD_SET_NONBLOCK , OP_FD_POLL_READ , OP_FD_POLL_WRITE ,
  OP_EXIT , OP_OS_LIST_DIR , OP_SERIAL_OPEN , OP_SERIAL_CONFIG ,
  OP_SERIAL_SEND , OP_SERIAL_RECV , OP_SERIAL_CLOSE , OP_TRY_PUSH ,
  OP_TRY_POP , OP_THROW , OP_FLOOR , OP_CEIL ,
  OP_TRUNC , OP_ROUND , OP_SIN , OP_COS ,
  OP_TAN , OP_EXP , OP_LOG , OP_LOG10 ,
  OP_SQRT , OP_GCD , OP_LCM , OP_ISQRT ,
  OP_SIGN , OP_FMIN , OP_FMAX , OP_RUST_HELLO ,
  OP_RUST_HELLO_ARGS , OP_RUST_HELLO_ARGS_RETURN , OP_RUST_GET_SP , OP_RUST_SET_EXIT ,
  OP_CPP_ADD
}
 VM operation codes executed by the Fun virtual machine. More...

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.
void bytecode_dump (const Bytecode *bc)
 Print a human-readable dump of constants and instructions to stdout.

Detailed Description

Definitions for the Fun VM bytecode: opcodes, instruction format, and bytecode container API.

This header declares the VM's operation codes (OpCode), the compact instruction representation (Instruction), and the owning bytecode container (Bytecode) together with minimal constructor/manipulation helpers. The concrete execution semantics for each opcode are implemented in the VM (see vm.c and vm/some_file.c handlers).

Definition in file bytecode.h.

Macro Definition Documentation

◆ OPCODE_COUNT

#define OPCODE_COUNT   (OP_CPP_ADD + 1)

Definition at line 300 of file bytecode.h.

Typedef Documentation

◆ Bytecode

typedef struct Bytecode Bytecode

Enumeration Type Documentation

◆ OpCode

enum OpCode

VM operation codes executed by the Fun virtual machine.

Unless stated otherwise, opcodes operate on the VM stack. Comments on each opcode describe stack effects using a left-to-right pop order and the value pushed as a result. For example, "pops b, a; pushes a+b" means the instruction will pop first b then a from the stack and finally push the result of a+b.

Enumerator
OP_NOP 
OP_LOAD_CONST 
OP_LOAD_LOCAL 
OP_STORE_LOCAL 
OP_LOAD_GLOBAL 
OP_STORE_GLOBAL 
OP_ADD 
OP_SUB 
OP_MUL 
OP_DIV 
OP_LT 
OP_LTE 
OP_GT 
OP_GTE 
OP_EQ 
OP_NEQ 
OP_POP 
OP_JUMP 
OP_JUMP_IF_FALSE 
OP_CALL 
OP_RETURN 
OP_PRINT 
OP_ECHO 
OP_HALT 
OP_LINE 
OP_MOD 
OP_AND 
OP_OR 
OP_NOT 
OP_DUP 
OP_SWAP 
OP_MAKE_ARRAY 
OP_INDEX_GET 
OP_INDEX_SET 
OP_LEN 
OP_PUSH 
OP_APOP 
OP_SET 
OP_INSERT 
OP_REMOVE 
OP_SLICE 
OP_TO_NUMBER 
OP_TO_STRING 
OP_CAST 
OP_TYPEOF 
OP_UCLAMP 
OP_SCLAMP 
OP_SPLIT 
OP_JOIN 
OP_SUBSTR 
OP_FIND 
OP_REGEX_MATCH 
OP_REGEX_SEARCH 
OP_REGEX_REPLACE 
OP_CONTAINS 
OP_INDEX_OF 
OP_CLEAR 
OP_ENUMERATE 
OP_ZIP 
OP_MIN 
OP_MAX 
OP_CLAMP 
OP_ABS 
OP_POW 
OP_RANDOM_SEED 
OP_RANDOM_INT 
OP_MAKE_MAP 
OP_KEYS 
OP_VALUES 
OP_HAS_KEY 
OP_READ_FILE 
OP_WRITE_FILE 
OP_ENV 
OP_INPUT_LINE 
OP_PROC_RUN 
OP_PROC_SYSTEM 
OP_TIME_NOW_MS 
OP_CLOCK_MONO_MS 
OP_KCGI_PARSE 
OP_KCGI_REPLY_START 
OP_KCGI_WRITE 
OP_KCGI_END 
OP_DATE_FORMAT 
OP_ENV_ALL 
OP_FUN_VERSION 
OP_THREAD_SPAWN 
OP_THREAD_JOIN 
OP_SLEEP_MS 
OP_RANDOM_NUMBER 
OP_BAND 
OP_BOR 
OP_BXOR 
OP_BNOT 
OP_SHL 
OP_SHR 
OP_ROTL 
OP_ROTR 
OP_JSON_PARSE 
OP_JSON_STRINGIFY 
OP_JSON_FROM_FILE 
OP_JSON_TO_FILE 
OP_CURL_GET 
OP_CURL_POST 
OP_CURL_DOWNLOAD 
OP_SQLITE_OPEN 
OP_SQLITE_CLOSE 
OP_SQLITE_EXEC 
OP_SQLITE_QUERY 
OP_REDIS_CONNECT 
OP_REDIS_CMD 
OP_REDIS_CLOSE 
OP_PCSC_ESTABLISH 
OP_PCSC_RELEASE 
OP_PCSC_LIST_READERS 
OP_PCSC_CONNECT 
OP_PCSC_DISCONNECT 
OP_PCSC_TRANSMIT 
OP_PCRE2_TEST 
OP_PCRE2_MATCH 
OP_PCRE2_FINDALL 
OP_OPENSSL_MD5 
OP_OPENSSL_SHA256 
OP_OPENSSL_SHA512 
OP_OPENSSL_RIPEMD160 
OP_INI_LOAD 
OP_INI_FREE 
OP_INI_GET_STRING 
OP_INI_GET_INT 
OP_INI_GET_DOUBLE 
OP_INI_GET_BOOL 
OP_INI_SET 
OP_INI_UNSET 
OP_INI_SAVE 
OP_XML_PARSE 
OP_XML_ROOT 
OP_XML_NAME 
OP_XML_TEXT 
OP_SOCK_TCP_LISTEN 
OP_SOCK_TCP_ACCEPT 
OP_SOCK_TCP_CONNECT 
OP_SOCK_SEND 
OP_SOCK_RECV 
OP_SOCK_CLOSE 
OP_SOCK_UNIX_LISTEN 
OP_SOCK_UNIX_CONNECT 
OP_FD_SET_NONBLOCK 
OP_FD_POLL_READ 
OP_FD_POLL_WRITE 
OP_EXIT 
OP_OS_LIST_DIR 
OP_SERIAL_OPEN 
OP_SERIAL_CONFIG 
OP_SERIAL_SEND 
OP_SERIAL_RECV 
OP_SERIAL_CLOSE 
OP_TRY_PUSH 
OP_TRY_POP 
OP_THROW 
OP_FLOOR 
OP_CEIL 
OP_TRUNC 
OP_ROUND 
OP_SIN 
OP_COS 
OP_TAN 
OP_EXP 
OP_LOG 
OP_LOG10 
OP_SQRT 
OP_GCD 
OP_LCM 
OP_ISQRT 
OP_SIGN 
OP_FMIN 
OP_FMAX 
OP_RUST_HELLO 
OP_RUST_HELLO_ARGS 
OP_RUST_HELLO_ARGS_RETURN 
OP_RUST_GET_SP 
OP_RUST_SET_EXIT 
OP_CPP_ADD 

Definition at line 35 of file bytecode.h.

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.