[MLton-user] FFI, MacOS PPC, and 16-bit ints

Dave Herman dherman at ccs.neu.edu
Tue Nov 27 10:14:03 PST 2007


On PPC MacOS, I can't seem to mutate Int16.int refs from C via the FFI. 
The program below successfully mutates the ordinary int ref to 42, but 
does not mutate the Int16.int ref.

Am I abusing the FFI somehow, or is it a bug in MLton?


1. command-line:

gcc -c ffi.c && mlton -cc-opt -I/sw/include -default-ann 'allowFFI true' 
ffi.sml ffi.o


2. ffi.sml:

val foo = _import "foo" : Int16.int ref -> unit;
val bar = _import "bar" : int ref -> unit;

val thing1 : Int16.int ref = ref 0;
val thing2 : int ref = ref 0;

val _ = foo thing1;
val _ = bar thing2;

val _ = TextIO.print ("thing1 is " ^ (Int16.toString (!thing1)) ^ "\n");
val _ = TextIO.print ("thing2 is " ^ (Int.toString (!thing2)) ^ "\n");


3. ffi.c:

#include <stdint.h>
#include <stdio.h>

typedef int16_t SHORT;

void foo(SHORT *thing)
{
     *thing = 42;
}

void bar(int *thing)
{
     *thing = 42;
}


4. run:

% ./ffi
thing1 is 0
thing2 is 42


Thanks,
Dave



More information about the MLton-user mailing list