Tile C Interface

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

calc.h
#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

calc.c
#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.

native.tasm
@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:

ArchitectureOperating SystemCompiler
AArch64 (ARM64)iOSClang
AArch64LinuxGCC
AArch64WindowsMSVC
AlphaLinuxGCC
AlphaTru64GCC
ARCLinuxGCC
ARC32LinuxGCC
ARC64LinuxGCC
ARMLinuxGCC
ARMiOSGCC
ARMWindowsMSVC
AVR32LinuxGCC
BlackfinuClinuxGCC
CSKYLinuxGCC
HPPAHPUXGCC
HPPA64HPUXGCC
KVXLinuxGCC
IA-64LinuxGCC
LoongArch64LinuxGCC
M68KFreeMiNTGCC
M68KLinuxGCC
M68KRTEMSGCC
M88KOpenBSD/mvme88kGCC
MetaLinuxGCC
MicroBlazeLinuxGCC
MIPSIRIXGCC
MIPSLinuxGCC
MIPSRTEMSGCC
MIPS64LinuxGCC
MoxieBare metalGCC
Nios IILinuxGCC
OpenRISCLinuxGCC
PowerPC 32-bitAIXGCC
PowerPC 32-bitAIXIBM XL C
PowerPC 64-bitAIXIBM XL C
PowerPCAMIGAGCC
PowerPCLinuxGCC
PowerPCMac OSXGCC
PowerPCFreeBSDGCC
PowerPC 64-bitFreeBSDGCC
PowerPC 64-bitLinux ELFv1GCC
PowerPC 64-bitLinux ELFv2GCC
RISC-V 32-bitLinuxGCC
RISC-V 64-bitLinuxGCC
S390LinuxGCC
S390XLinuxGCC
SPARCLinuxGCC
SPARCSolarisGCC
SPARCSolarisOracle Solaris Studio C
SPARC64LinuxGCC
SPARC64FreeBSDGCC
SPARC64SolarisOracle Solaris Studio C
TILE-Gx/TILEProLinuxGCC
VAXOpenBSD/vaxGCC
WASM32EmscriptenEMCC
X86FreeBSDGCC
X86GNU HURDGCC
X86InterixGCC
X86kFreeBSDGCC
X86LinuxGCC
X86OpenBSDGCC
X86OS/2GCC
X86SolarisGCC
X86SolarisOracle Solaris Studio C
X86Windows/CygwinGCC
X86Windows/MinGWGCC
X86-64FreeBSDGCC
X86-64LinuxGCC
X86-64Linux/x32GCC
X86-64OpenBSDGCC
X86-64SolarisOracle Solaris Studio C
X86-64Windows/CygwinGCC
X86-64Windows/MinGWGCC
X86-64Mac OSXGCC
XtensaLinuxGCC

Please send additional platform test results to libffi-discuss@sourceware.org.

Checkout libffi github page (opens in a new tab)