[MLton] C codegen and world* regressions

Stephen Weeks MLton@mlton.org
Sat, 17 Jun 2006 13:11:19 -0700


>    val f = _import "f": unit -> bool;
...
> I point this out because if the C-function f returns a value which
> is neither zero nor one, then the program compiled with two
> different codegens might exhibit different behavior.
>
> I think this is o.k.; all it means is that an ML 'bool' is not the
> same as a C conditional expression; rather it is closer to the C99
> bool_t type. Using a value not equal to either zero or one for an ML
> 'bool' leads to undefined behavior.

That seems OK to me too.  The old runtime/basis was sloppy about this,
confusing bools and ints.  Hopefully the new runtime will completely
clarify things.  Also once this is all done, the representation pass
can be tweaked so it packs booleans as single bits -- right now it
keeps them as 32 bits, at least partially because of confusion between
C booleans and ints.

I also see that

  http://mlton.org/ForeignFunctionInterfaceTypes

says that the SML bool type is equivalent to the C Int32 type.  We'll
need to change this to C "bool" type and to warn people about the new,
more precise type (and the fact that the value must be 0 or 1).

I looked at all the uses of Bool_t in basis-ffi.h to see if the
functions exported by the runtime were confusing bools and ints.  I
found a few that I'm not sure about. 

1. The functions

    PosixFileSys_ST_is{Blk,Chr,Dir,FIFO,Link,Reg,Sock}

  all return Bool_t by calling the corresponding macro

    S_IS{BLK,CHR,DIR,FIFO,LNK,REG,SOCK}

  On my Linux machine these all expand to relational expressions (==),
  but I don't know if the standard (or standard practice) guarantees
  these to be booleans. 

2. The function Posix_ProcEnv_isatty returns a Bool_t by calling
   isatty, which on my machine is specified to return an int.

3. The functions

     Posix_Process_if{Exited,Signaled,Stopped}

   all return Bool_t by calling the corresponding macro

     WIF{EXIT,SIGNAL,STOPP}ED

   Again, on my machine these all expand to relational expressions,
   but I don't know if the standard guarantees these to be booleans.

4. The function Posix_Signal_isPending returns a Bool_t by calling
   sigismember, which on my machine is specified to return an int.

I wonder if the right thing to do for situations where we are unsure
or have C functions that return int instead of bool, is to expose them
as int in the FFI and convert the int type to bool in SML code with a
comparison to zero.