Tile C Interface
Tile C Interface (tci) is an interface to handle dynamic native calls.
Let's say you have a C dynamic library called calc
to do some calculations.
Here is the header
#ifndef CALC_H_
#define CALC_H_
#ifdef CALC_EXPORTS /* define CALC_EXPORTS *only* when building the DLL. */
#define CALC_API __declspec(dllexport)
#else
#define CALC_API __declspec(dllimport)
#endif
/* Define calling convention in one place, for convenience. */
#define CALL __cdecl
/* Make sure functions are exported with C linkage under C++ compilers. */
#ifdef __cplusplus
extern "C"
{
#endif
/* Declare our Add function using the above definitions. */
CALC_API int CALL Add(int a, int b);
CALC_API int CALL Subtract(int a, int b);
CALC_API int CALL Divide(int a, int b);
CALC_API int CALL Multiply(int a, int b);
#ifdef __cplusplus
} // __cplusplus defined.
#endif
#endif//CALC_H_
And .c file
#include "calc.h"
int CALL Add(int a, int b) {
return (a + b);
}
int CALL Subtract(int a, int b) {
return (a - b);
}
int CALL Divide(int a, int b) {
return (a/b);
}
int CALL Multiply(int a, int b) {
return (a * b);
}
After you compile your C code as a dynamic library (.so, .dll, .dylib) you can call that library with just decleration of the function.
@cfun i32 Add i32 i32
jmp _main
main:
push 5
push 8
native 0
hlt
Generate out.bin
tasm native.tasm
Run tvm to execute the code
Don't forget to add the dynamic library
tvm out.bin ./calc.dll
tvm uses libffi to have a Foreign Function Interface behind the scene.
Status
libffi-3.4.6 was released on February 18, 2024. Check the libffi web page for updates: http://sourceware.org/libffi/
What is libffi?
Compilers for high level languages generate code that follow certain conventions. These conventions are necessary, in part, for separate compilation to work. One such convention is the "calling convention". The "calling convention" is essentially a set of assumptions made by the compiler about where function arguments will be found on entry to a function. A "calling convention" also specifies where the return value for a function is found.
Some programs may not know at the time of compilation what arguments are to be passed to a function. For instance, an interpreter may be told at run-time about the number and types of arguments used to call a given function. Libffi can be used in such programs to provide a bridge from the interpreter program to compiled code.
The libffi library provides a portable, high level programming interface to various calling conventions. This allows a programmer to call any function specified by a call interface description at run time.
FFI stands for Foreign Function Interface. A foreign function interface is the popular name for the interface that allows code written in one language to call code written in another language. The libffi library really only provides the lowest, machine dependent layer of a fully featured foreign function interface. A layer must exist above libffi that handles type conversions for values passed between the two languages.
Supported Platforms
Libffi has been ported to many different platforms.
At the time of release, the following basic configurations have been tested:
Architecture | Operating System | Compiler |
---|---|---|
AArch64 (ARM64) | iOS | Clang |
AArch64 | Linux | GCC |
AArch64 | Windows | MSVC |
Alpha | Linux | GCC |
Alpha | Tru64 | GCC |
ARC | Linux | GCC |
ARC32 | Linux | GCC |
ARC64 | Linux | GCC |
ARM | Linux | GCC |
ARM | iOS | GCC |
ARM | Windows | MSVC |
AVR32 | Linux | GCC |
Blackfin | uClinux | GCC |
CSKY | Linux | GCC |
HPPA | HPUX | GCC |
HPPA64 | HPUX | GCC |
KVX | Linux | GCC |
IA-64 | Linux | GCC |
LoongArch64 | Linux | GCC |
M68K | FreeMiNT | GCC |
M68K | Linux | GCC |
M68K | RTEMS | GCC |
M88K | OpenBSD/mvme88k | GCC |
Meta | Linux | GCC |
MicroBlaze | Linux | GCC |
MIPS | IRIX | GCC |
MIPS | Linux | GCC |
MIPS | RTEMS | GCC |
MIPS64 | Linux | GCC |
Moxie | Bare metal | GCC |
Nios II | Linux | GCC |
OpenRISC | Linux | GCC |
PowerPC 32-bit | AIX | GCC |
PowerPC 32-bit | AIX | IBM XL C |
PowerPC 64-bit | AIX | IBM XL C |
PowerPC | AMIGA | GCC |
PowerPC | Linux | GCC |
PowerPC | Mac OSX | GCC |
PowerPC | FreeBSD | GCC |
PowerPC 64-bit | FreeBSD | GCC |
PowerPC 64-bit | Linux ELFv1 | GCC |
PowerPC 64-bit | Linux ELFv2 | GCC |
RISC-V 32-bit | Linux | GCC |
RISC-V 64-bit | Linux | GCC |
S390 | Linux | GCC |
S390X | Linux | GCC |
SPARC | Linux | GCC |
SPARC | Solaris | GCC |
SPARC | Solaris | Oracle Solaris Studio C |
SPARC64 | Linux | GCC |
SPARC64 | FreeBSD | GCC |
SPARC64 | Solaris | Oracle Solaris Studio C |
TILE-Gx/TILEPro | Linux | GCC |
VAX | OpenBSD/vax | GCC |
WASM32 | Emscripten | EMCC |
X86 | FreeBSD | GCC |
X86 | GNU HURD | GCC |
X86 | Interix | GCC |
X86 | kFreeBSD | GCC |
X86 | Linux | GCC |
X86 | OpenBSD | GCC |
X86 | OS/2 | GCC |
X86 | Solaris | GCC |
X86 | Solaris | Oracle Solaris Studio C |
X86 | Windows/Cygwin | GCC |
X86 | Windows/MinGW | GCC |
X86-64 | FreeBSD | GCC |
X86-64 | Linux | GCC |
X86-64 | Linux/x32 | GCC |
X86-64 | OpenBSD | GCC |
X86-64 | Solaris | Oracle Solaris Studio C |
X86-64 | Windows/Cygwin | GCC |
X86-64 | Windows/MinGW | GCC |
X86-64 | Mac OSX | GCC |
Xtensa | Linux | GCC |
Please send additional platform test results to libffi-discuss@sourceware.org.