Skip to content

C FFI & Python Bridge

C FFI & Python Bridge

C Foreign Function Interface

Thagore provides direct access to C functions through extern func declarations. Any function available in a C shared library or the system’s C standard library can be called.

Basic C FFI

ffi_basic.tg
# Declare C standard library functions
extern func strlen(s: String) -> i32
extern func puts(text: String) -> i32
extern func printf(fmt: String, value: i32) -> i32
func main() -> i32:
let msg = "Hello from FFI!"
puts(msg)
printf("String length: %d\n", strlen(msg))
return 0

Math Library FFI

Call C math functions directly:

ffi_math.tg
# Float math functions
extern func sqrtf(x: f32) -> f32
extern func powf(base: f32, exp: f32) -> f32
extern func sinf(x: f32) -> f32
extern func cosf(x: f32) -> f32
extern func fabsf(x: f32) -> f32
# Double math functions
extern func sqrt(x: f64) -> f64
extern func pow(base: f64, exp: f64) -> f64
func calc_hypotenuse(a: f32, b: f32) -> f32:
return sqrtf(a * a + b * b)
func main() -> i32:
let h = calc_hypotenuse(3.0, 4.0)
print(h) # Output: 5.0
return 0

Memory Management FFI

ffi_memory.tg
extern func malloc(size: i32) -> ptr
extern func free(p: ptr) -> void
extern func memcpy(dst: ptr, src: ptr, size: i32) -> ptr
func allocate_buffer(size: i32) -> ptr:
let buffer = malloc(size)
return buffer
func main() -> i32:
let buf = allocate_buffer(1024)
# ... use buffer ...
free(buf)
return 0

FFI Type Mapping Reference

Thagore TypeC TypeNotes
i32int / int32_t32-bit signed integer
f32floatIEEE 754 single precision
f64doubleIEEE 754 double precision
Stringconst char*Null-terminated C string
ptrvoid*Generic pointer
voidvoidNo return value

Python Bridge

Thagore can interface with Python through the runtime’s Python bridge. This enables calling Python libraries (including NumPy, PyTorch, etc.) from Thagore code.

Python Bridge Functions

FunctionPurpose
__thg_py_initialize()Initialize the Python interpreter
__thg_py_finalize()Shutdown the Python interpreter
__thg_py_import(name)Import a Python module
__thg_py_getattr(obj, name)Get an attribute from a Python object
__thg_py_call0(callable)Call a Python callable with zero arguments
__thg_py_call_2(func, a1, a2)Call with two arguments
__thg_py_from_i32(val)Convert Thagore i32 to Python int
__thg_py_print_obj(obj)Print a Python object to stdout

Basic Python Usage

python_basic.tg
extern func __thg_py_initialize() -> void
extern func __thg_py_finalize() -> void
extern func __thg_py_import(name: String) -> ptr
extern func __thg_py_print_obj(obj: ptr) -> void
func main() -> i32:
print("Importing Python math module...")
__thg_py_initialize()
let math_mod = __thg_py_import("math")
__thg_py_print_obj(math_mod)
__thg_py_finalize()
return 0

PyTorch Integration

Call PyTorch from Thagore for tensor operations:

pytorch_tensor.tg
extern func __thg_py_initialize() -> void
extern func __thg_py_import(name: String) -> ptr
extern func __thg_py_getattr(obj: ptr, name: String) -> ptr
extern func __thg_py_print_obj(obj: ptr) -> void
extern func __thg_py_from_i32(val: i32) -> ptr
extern func __thg_py_call_2(func_obj: ptr, a1: ptr, a2: ptr) -> ptr
func main() -> i32:
__thg_py_initialize()
print("Loading PyTorch...")
let torch = __thg_py_import("torch")
# Get torch.rand function
let rand_func = __thg_py_getattr(torch, "rand")
# Create dimensions
let dim1 = __thg_py_from_i32(3)
let dim2 = __thg_py_from_i32(3)
# Generate a 3x3 random tensor
print("Generating Tensor(3, 3)...")
let tensor = __thg_py_call_2(rand_func, dim1, dim2)
__thg_py_print_obj(tensor)
return 0

Requirements

To use the Python bridge:

  1. Python 3.x must be installed and on PATH
  2. Link against the Python shared library
  3. Required Python packages must be installed (pip install torch for PyTorch)