Perl 6 bindings for TCC, the Tiny C Compiler
Perl 6
Switch branches/tags
Nothing to show
Latest commit 17fd731 Jun 23, 2017 @CurtTilmes CurtTilmes edit doc
Failed to load latest commit information.
eg compiler options Jun 23, 2017
lib compiler options Jun 23, 2017
t Add tests and travis Jun 23, 2017
.gitignore start Jun 21, 2017
.travis.yml move to /usr/lib Jun 23, 2017
META6.json Add tests and travis Jun 23, 2017 edit doc Jun 23, 2017

Perl 6 bindings for TCC, the Tiny C Compiler

Build Status


TCC provides Perl 6 bindings for tcc, the Tiny C Compiler, an extremely fast C compiler that can compile C code directly into a memory block that is then callable by Perl. You can also expose your perl functions to C which can call them, and pass data back and forth.

The interface between Perl and C makes extensive use of the Perl 6 NativeCall capabilities, and is subject to its types and other restrictions.


You have to build tcc, the Tiny C Compiler by hand since it doesn't install the shared library by default, and most distributions don't include it.

git clone
cd tinycc
make   # Have to do this first to compile with PIC for shared
sudo make install
sudo cp /usr/local/lib # or somewhere convenient


See eg/testit.pl6

use v6;
use TCC;

# Make a new Tiny C Compiler
my $tcc ='-I/usr/local/include -L/usr/local/lib -DDEBUG=0');

# Compile a C program into memory
    #include <stdio.h>
    int add(int a, int b);  /* declare perl functions to quiet warnings */
    void print_something(char *my_string);

    int x = 7;

    int fib(int n)
        if (n <= 2)
            return 1;
            return fib(n-1) + fib(n-2);

    int foo(int n)
        printf("Hello World!\n");
        printf("fib(%d) = %d\n", n, fib(n));
        printf("add(%d, %d) = %d\n", n, 2 * n, add(n, 2 * n));

        print_something("this is just a test");

        if (DEBUG) { print_something("debug is on"); }

        return 0;

    int set_x(int n)
        x = n;

# These are just perl subs for C to call
sub add(int32 $a, int32 $b --> int32)
    return $a + $b;

# This one gets renamed to 'print_something' for C
sub print-something(Str $my-string)
    say $my-string;

# Add some perl subs for C to call
$tcc.add-symbol(&print-something, name => 'print_something');

# You just have to do this, so do it.

# Bind C functions to perl callable variables, must pass in signature
my &fib   := $tcc.bind('fib', :(int32 --> int32));
my &foo   := $tcc.bind('foo', :(int32 --> int32));
my &set_x := $tcc.bind('set_x', :(int32 --> int32));

# Bind Perl variable to C symbol, pass in type and an optional function
# to set the variable if you want to go both ways
my $x     := $tcc.bind('x', int32, &set_x);

# Once everything is compiled and bound, just work in Perl land like normal:

say fib(12);


say $x;


say $x;

$x = 752;

say $x;


Hello World!
fib(17) = 1597
add(17, 34) = 51
this is just a test


  • Some assumptions on 64-bit architecture, but everyone has that anyway, right?

  • Not really set up for Windows yet -- patches welcome!

  • For now you have to manually write a store function if you want two-way variable binding. Could probably do this automatically.

  • The Tiny C Compiler also includes an assembler, so if even C is too slow for you, you can embed x86 assembly language into your critical portions for super speed!


Developed after presentation and discussion with David Mertens about the Perl 5 module C::Blocks and how one might approach that in Perl 6.



Inline::C uses a slower C compiler, and goes to temp files on disk for compiling and library linking but should generate more optimized code. It also doesn't seem to have as nice an interface for interacting back and forth with perl.