[MLton-user] bug report, FFI of mlton-20070826

Matthew Fluet fluet at tti-c.org
Mon Sep 3 11:04:41 PDT 2007


Katshurio,

Thank you for the bug report.  You are correct that the error is due to 
the mismatch in FFI types in the compiler and basis library.  We've 
settled on treating the Standard ML 'char' type as a C 'uint8_t' type; so 
the comment in basis-library/mlton/ffi.sml is outdated.  A better fix is 
the following:

--- basis-library/mlton/ffi.sml (revision 5964)
+++ basis-library/mlton/ffi.sml (working copy)
@@ -47,17 +47,17 @@
  val atomicEnd = MLtonThread.atomicEnd
  val register = MLtonThread.register

-(* To the C-world, chars are signed integers. *)
-val getChar8 = Primitive.Char8.idFromInt8 o getInt8
+(* To the C-world, chars are unsigned integers. *)
+val getChar8 = Primitive.Char8.idFromWord8 o getWord8
  (*
-val getChar16 = Primitive.Char16.idFromInt16 o getInt16
-val getChar32 = Primitive.Char32.idFromInt32 o getInt32
+val getChar16 = Primitive.Char16.idFromWord16 o getWord16
+val getChar32 = Primitive.Char32.idFromWord32 o getWord32
  *)

-val setChar8 = setInt8 o Primitive.Char8.idToInt8
+val setChar8 = setWord8 o Primitive.Char8.idToWord8
  (*
-val setChar16 = setInt16 o Primitive.Char16.idToInt16
-val setChar32 = setInt32 o Primitive.Char32.idToInt32
+val setChar16 = setWord16 o Primitive.Char16.idToWord16
+val setChar32 = setWord32 o Primitive.Char32.idToWord32
  *)

Note, that since this only affects the Basis Library implementation, it 
can safely be applied to the binary packages (though, in the binary 
packages, the file is moved to /usr/local/lib/mlton/sml/basis/ffi.sml), 
without needing to rebuild the compiler.

-Matthew


On Fri, 31 Aug 2007, Katsuhiro Ueno wrote:
> Hello,
>
> I found a problem about FFI of the latest MLton.
>
> Exporting an ML function which returns "char" causes linker error.
> For example:
>
> val e = _export "f" : (unit -> char) -> unit;
> val _ = e (fn () => #"a")
>
> Compiling this code goes well, but linking will be aborted by
> "undefined symbols" error (I am on x86-darwin):
>
> $ mlton -default-ann 'allowFFI true' test.sml
> /usr/bin/ld: Undefined symbols:
> _MLton_FFI_Int8
> collect2: ld returned 1 exit status
> call to system failed with exit status 1:
> gcc -o a /tmp/filegdbAmA.o /tmp/fileAZL4LJ.o
> -L/usr/local/lib/mlton/self -lmlton -lgdtoa -lm -lgmp -L/sw/lib
>
>
> I think this error is due to that correspondance of FFI types is
> different between compiler and basis library.
>
> Here is my ad-hoc fix.
>
> --- mlton/elaborate/elaborate-core.fun	(revision 5976)
> +++ mlton/elaborate/elaborate-core.fun	(working copy)
> @@ -752,7 +752,9 @@
>                      let
>                         open CType
>                      in
> -                        [Word8, Word16, Word32]
> +                        (* According to basis-library/mlton/ffi.fun,
> +                         * to the C-world, chars are signed integers. *)
> +                        [Int8, Int16, Int32]
>                      end)
>             @ sized (Tycon.int o IntSiz
>
> _______________________________________________
> MLton-user mailing list
> MLton-user at mlton.org
> http://mlton.org/mailman/listinfo/mlton-user
>



More information about the MLton-user mailing list