Mica has no FFI in this direction either: every call from C to Mica is a
direct C ABI call, with no wrapper, no glue and no marshaling layer between
the two, and the C boundary states every rule a
call across it follows. The example
CCallsMica
is a plain C program, compiled by cc, whose link line names three archives
— your Mica library, Mica’s standard library, and the Dragon runtime floor
beneath it — and that is the entire integration:
cc -std=c23 -o ccallsmica main.c ../build/Utilities/Utilities.a /usr/lib/mica-stdlib-arm64.a /usr/lib/mica-dragon-rt-arm64.a -lm
No Mica source in the build, and nothing to start. The C program’s
main is in charge from the first instruction.
The naming rule
A Mica export’s linkage name is the build target’s name, two
underscores, and the exported name. The
Utilities library
builds a target called Utilities, so its exp function Fibonacci links
as:
extern int64_t Utilities__Fibonacci(int32_t n);
extern double Utilities__VectorMagnitude(double x, double y);
extern float Utilities__Sigmoid(float x);Two facts complete the rule. A library built for the UTF-8 encoding
appends __utf8 to every linkage name, so the same export links as
Utilities__Fibonacci__utf8; the example builds for UTF-32, which appends
nothing. And every library has one more entry, the target’s name and
___library__init (Learner___library__init__utf8 for a library Learner built for UTF-8),
which the header --emit-header writes declares. Nothing runs it for you:
a C program calls it once, before its first call into the library. The
entry first runs the entries of the libraries this one was built against,
so their bodies run before its own and a C program calls only the entries
of the libraries it uses itself. Then it runs the body’s statements and
gives each variable that starts at a value other than zero its first value
(a variable of the subrange 1..9 starts at one). All of that happens at
the first call; a later call returns at once. A library built with
--tasking multicore lets several threads call the entry at once: a call
that meets the work running on another thread waits until it is done, so
every call returns with every body beneath it complete. A library with nothing to
run, in itself and beneath it, returns from its entry at once, so a
program that calls it keeps working when a body later gains a statement.
A library that places work on processes has one entry more, the target’s
name and ___library__init_processes (Learner___library__init_processes__utf8 for Learner
built for UTF-8), which starts the process server the library’s workers are
forked from. Call it once, early in main, before your program starts a
thread of its own and before the body’s ___library__init: the server is a copy of
your program taken at that call. A call while a second thread runs ends the
program with a message that says so, a second call returns at once, and the
server and its workers end when your program does. Work the library places
on a process before the call ends the program with a message that names the
entry.
The header utilities_ffi.h in the example is exactly such declarations
plus a few typedefs — written by hand once, deliberately, so the example
shows the raw truth of the boundary with nothing generated in between.
You do not have to write them by hand. Add --emit-header to the library’s
build, beside --link, and the compiler writes Utilities.h next to the
archive or shared object, from the same facts as the library’s contract: one
declaration per export under its exact linkage name for the build’s encoding,
every record, enumeration and array type those exports use, the string
descriptor, the entry that runs the library’s body and, for a library that
places work on processes, the entry that starts its process server. A
changed export
changes the header in the same build. An export C cannot express, such as a
fixed array passed by value, is not declared: the header’s opening comment
names it with the reason, and the build prints a note naming it. The
types map as the C ABI already says they must: int64 is int64_t,
float32 is float, bool is bool, a packed Mica record is a
__attribute__((packed)) struct, and a Mica string arrives as a
two-field descriptor — pointer and rune count:
typedef struct MicaStringView
{
const mica_char_t *Data;
uint64_t Length;
} MicaStringView;The run
Ordinal contract surface
pair const check = 1
wrapper pair state/level = busy / 4
Packed aggregate contract surface
payload = 5, 16, 4.25
lane = (21,30) (22,40)
lane total = 70
Text contract surface
row = zero | | two
wrapper = north | west / center / east | south
Summary
- Called one Mica library directly from C without an adapter runtime
- Demonstrated value, pointer, and const-pointer calls across the shared ABI
- Covered classic scalars, ordinals, packed data, arrays, records, and descriptor-based stringsNote what the sections mirror: they are the same value shapes the Mica-calls-C example sends the other way, printing the same numbers. One ABI story, told from both ends, agreeing — that symmetry is the boundary’s real test, and both examples are built in the published image whenever the starter repository changes, so it cannot silently rot.
Deeper than a demo
This direction is not a courtesy feature; it is load-bearing. The Dragon SDK’s C consumers drive the compiler’s whole backend through exactly this boundary, and the PL/0 compiler that certifies the SDK exists twice — once in C, once in Mica — precisely because a C program linking Mica archives is a first-class citizen. When your build system, your test harness, or your twenty-year-old application is C, Mica joins it, not the other way around.
And the sandwich composes: a Mica program can hand a C library a callback road back into its own exports — C calling Mica inside Mica calling C — which the debugging tutorial walks with a debugger attached.
A C routine that calls back shapes what a library may hold across the call. While a library body waits in a C routine, that routine may call any of the library’s exports before it returns, so the compiler reads every such call as a call of each export: a body that owns a global’s cell across the call is refused when an export would copy that cell into another variable, or lend it to a routine that keeps the pointer, before the body releases it. A C routine that returns only by returning says so in its contract, the promise glibc’s headers make for their own functions:
"effects": {"never_calls_back": true}The C, POSIX and Linux routines Mica ships contracts for carry it wherever glibc’s headers do, and a routine without it may call back.
The same row states which pointers a library’s routine passes on to C, which
may write through them during the call or keep them and write later.
--emit-contract writes it for every exported routine, empty where the
routine passes nothing on:
"effects": {"outside_writes": {"parameters": ["cell"]}}A program that imports the library checks a subrange, enumeration, bool or
unicode value where it reads it through such a pointer, or through a result
the row marks with "result": true, and stops with ordinal_out_of_domain
when the value lies outside its type; every other read keeps the type’s range
as a proof. A contract written before the row existed states nothing, so the
program checks every pointer it hands such a routine and warns at the call.
A routine that learns
An exported routine can train. Its tape window records, Backward walks the
tape and Gradient reads the derivative before the routine returns, so the C
side sees an ordinary function:
library Learn;
imp
Backward, Gradient : std;
{ The slope of x² + 3x at 'x', recorded and walked by the routine's own window: 2x + 3. }
exp function Slope(x : float64) : float64;
var
p, y : tracked float64;
begin
p := x;
tape
y := p * p + 3.0 * p;
end;
Backward(y);
Slope := Gradient(p);
end;
begin
end.extern double Learn__Slope(double x); /* Learn__Slope(4.0) answers 11 */A routine that trains a network works the same way: it copies the knobs the
library keeps between calls into tracked locals, records one window per round,
steps every knob downhill, and copies the trained values back before it
returns, so each call continues where the last one stopped. The tape keeps its
storage from one call to the next, so a later training call allocates nothing
new, and gives it back when your program exits. Linked into a shared object
of your own that your program unloads with dlclose, the library gives the
storage back as the object unloads, so the exit finds nothing left to release.
The fixed arena
On a device with no heap to spare, Mica’s fixed-arena class serves every allocation from one block of static storage. A C program chooses it on its link line, naming the fixed-arena floor in place of the default one:
cc -std=c23 -o learner main.c arena.c Learner.a /usr/lib/mica-stdlib-arm64.a /usr/lib/mica-dragon-rt-fixedarena-arm64.a -lm
The floor brings an arena of one mebibyte. A program that wants another size
defines the arena itself, in one of its own C files, here arena.c, and the
floor takes that one instead:
#include <stddef.h>
/* every Mica allocation of this program comes from these 256 KiB */
__attribute__((aligned(16))) unsigned char __mica_arena[256 * 1024];
size_t __mica_arena_size = sizeof __mica_arena;A shared object of your own takes the same file: link arena.c into it
beside the archive, the standard library and the fixed-arena floor, and the
library inside it takes its memory from that arena.
When a library call needs more than the arena has left, the program stops
with a named failure, Mica runtime failure: reason=allocation_failure (8),
followed by the file, line and column of the library’s source that asked for
the memory, and exit status 1.
Memory crosses the boundary with its owner. What a Mica library hands your C
code, the library releases: give it an exported routine that releases what it
handed out, and call that routine, never free. free knows only the blocks
malloc made, and an arena’s cells were never among them. Memory your C code
allocated stays yours to release with free; hand it to a Mica library to
read or to fill, never to release. The compiler holds Mica code to the same
rule under the fixed arena: it refuses to Dispose a pointer a C function
returned, also when a library routine passes that pointer on to the unit
that disposes it.
A checked library’s heapless routines
A library built with checked watches its
heapless routines as a Mica
program does: the first allocation or release such a routine reaches, C code
behind its contracts included, stops the program and names the routine. The
library’s half of the watch travels in its archive and the floor. The C
allocator’s entries belong to your program’s link, so the link line of a
program that links such a library adds these words:
-Wl,--defsym=malloc=__mica_heapless_malloc,--defsym=calloc=__mica_heapless_calloc
-Wl,--defsym=realloc=__mica_heapless_realloc,--defsym=free=__mica_heapless_free
-Wl,--defsym=posix_memalign=__mica_heapless_posix_memalign,--defsym=aligned_alloc=__mica_heapless_aligned_alloc
-Wl,--defsym=memalign=__mica_heapless_memalign,--defsym=valloc=__mica_heapless_valloc
-Wl,--defsym=pvalloc=__mica_heapless_pvalloc
On the fixed-arena floor the same words watch the C allocator. The cells
Mica code takes from the arena itself are watched when Mica links the
program, so in a C program a heapless routine’s arena cells go unwatched,
as in a build without checked.
A link line without them still runs the library: its heapless routines keep
the compiler’s checks, and what their C callees allocate goes unwatched, as
in a build without checked. The words name entries only a checked library
with a heapless routine brings, so a link of any other library that names
them stops with an undefined reference to __mica_heapless_malloc.
The stack
A Mica library runs on the stack of the thread that calls it, so that stack
must hold the library’s deepest call: give a thread you create a larger one
with pthread_attr_setstacksize. Building the library with --stack-size
sets the limit its stack frames are compiled against and reserves no stack,
and a stack overflow inside the library ends the way one in your own C code
would.
What this does not do
- Non-exported names do not exist. The archive publishes
exp-marked declarations and nothing else — the same rule the Mica side lives by, enforced by the symbol table itself. - A hand-written header is on your honor. Like every
externdeclaration in C, a wrong prototype is your foot and your trigger. Let--emit-headerwrite it, and the declarations follow the library’s exports on every build. - The stdlib archive joins the link. Mica code needs its runtime and
standard library; they arrive as one more
.aon the line — no shared runtime daemon, no environment, just an archive. - Threads follow C’s rule. A library built with
--tasking multicoreand linked with the multicore floor takes calls from any number of your threads at once: each thread becomes a Mica task of its own on its first call, with nothing for you to write, and the library is exactly as thread-safe as its ownsynchronizedstatements. A library built with--tasking singleruns one thread at a time, and your threads take turns through a mutex of your own. Each library links only with the floor of its own tasking. The C boundary states every rule.
Try it
mica-container/devkit/mica-provision gcc # the C side needs a C compiler, installed once
make -C mica-container/examples/CCallsMica runAdd extern int64_t Utilities__Fibonacci(int32_t); to any C project you
own, link the two archives, and call it. That — genuinely — is the whole
procedure.
Next
Mica calls Linux — the boundary at its most
worn: system calls, and the errno convention the compiler lifts so your
program never sees a -1.