Fun 0.41.5
The programming language that makes You have fun
Loading...
Searching...
No Matches
test_opcodes.c
Go to the documentation of this file.
1/*
2 * This file is part of the Fun programming language.
3 * https://fun-lang.xyz/
4 *
5 * Copyright 2025 Johannes Findeisen <you@hanez.org>
6 * Licensed under the terms of the Apache-2.0 license.
7 * https://opensource.org/license/apache-2-0
8 */
9
18
19#include "bytecode.h"
20#include "value.h"
21#include "vm.h"
22#include <stdio.h>
23
33int main() {
34
35 VM vm;
36 vm_init(&vm);
37
39
40 // Example: test OP_ADD
41 int c1 = bytecode_add_constant(bc, make_int(5));
42 int c2 = bytecode_add_constant(bc, make_int(3));
47
48 printf("=== Bytecode dump ===\n");
49 for (int i = 0; i < bc->instr_count; ++i) {
50 Instruction instr = bc->instructions[i];
51 printf("instr %3d: opcode=%2d operand=%d\n", i, instr.op, instr.operand);
52 }
53 printf("=====================\n");
55 printf("=====================\n");
56
57 vm_run(&vm, bc);
58
59 printf("Output count: %d\n", vm.output_count);
60 for (int i = 0; i < vm.output_count; i++) {
61 printf("Output[%d] = ", i);
62 print_value(&vm.output[i]);
63 printf("\n");
64 }
65
67
68 /* --- Rust FFI demo: call a Rust opcode and string function --- */
69#ifdef FUN_WITH_RUST
70 extern int fun_op_radd(VM * vm);
71 extern const char *fun_rust_get_string(void);
72
73 printf("=== Rust FFI demo ===\n");
74 const char *rs = fun_rust_get_string();
75 if (rs) {
76 printf("Rust says: %s\n", rs);
77 }
78
79 /* prepare stack: push 10 and 32, then call Rust add -> expect 42 */
80 vm_push_i64(&vm, 10);
81 vm_push_i64(&vm, 32);
82 int rc = fun_op_radd(&vm);
83 printf("fun_op_radd rc=%d\n", rc);
84 long long sum = (long long)vm_pop_i64(&vm);
85 printf("Rust op result: %lld\n", sum);
86#else
87 printf("=== Rust FFI demo (disabled; build with -DFUN_WITH_RUST=ON) ===\n");
88#endif
89
90 vm_free(&vm);
92 return 0;
93}
Bytecode * bytecode_new(void)
Allocate and initialize an empty Bytecode object.
Definition bytecode.c:27
int bytecode_add_instruction(Bytecode *bc, OpCode op, int32_t operand)
Append a single instruction to the instruction stream.
Definition bytecode.c:62
void bytecode_free(Bytecode *bc)
Free a Bytecode and all memory it owns.
Definition bytecode.c:92
void bytecode_dump(const Bytecode *bc)
Print a human-readable dump of constants and instructions to stdout.
Definition bytecode.c:423
int bytecode_add_constant(Bytecode *bc, Value v)
Append a constant to a Bytecode's constant table.
Definition bytecode.c:48
Definitions for the Fun VM bytecode: opcodes, instruction format, and bytecode container API.
@ OP_PRINT
Definition bytecode.h:63
@ OP_LOAD_CONST
Definition bytecode.h:37
@ OP_ADD
Definition bytecode.h:44
int rc
Instruction * instructions
Definition bytecode.h:294
int instr_count
Definition bytecode.h:295
OpCode op
Definition bytecode.h:289
int32_t operand
Definition bytecode.h:290
The Fun virtual machine state.
Definition vm.h:110
Value output[OUTPUT_SIZE]
Definition vm.h:119
int output_count
Definition vm.h:120
int main()
Minimal executable to exercise a subset of VM opcodes.
void print_value(const Value *v)
Print a human-readable representation of a Value to stdout.
Definition value.c:552
Value make_int(int64_t v)
Construct a Value representing a 64-bit integer.
Definition value.c:51
Defines the Value type and associated functions for the Fun VM.
void vm_push_i64(VM *vm, int64_t v)
Push a 64-bit integer as a VM int Value (C ABI helper).
Definition vm.c:621
void vm_init(VM *vm)
Initialize a VM instance to its default state.
Definition vm.c:714
void vm_free(VM *vm)
Free resources owned directly by the VM structure.
Definition vm.c:367
void vm_clear_output(VM *vm)
Clear the VM's buffered output values and partial flags.
Definition vm.c:349
int64_t vm_pop_i64(VM *vm)
Pop a numeric Value and convert it to a 64-bit integer (C ABI helper).
Definition vm.c:598
Core virtual machine data structures and public VM API.
const char * fun_rust_get_string(void)
void vm_run(VM *vm, Bytecode *entry)
Execute the provided entry bytecode in the VM. Pushes an initial frame and runs until HALT or an unre...
int fun_op_radd(VM *vm)