cvs commit: hugs98/src HsFFI.h Makefile.in builtin.c connect.h ffi.c input.c iomonad.c machdep.c parser.y plugin.c prelude.h static.c storage.c storage.h type.c

Alastair Reid reid@glass.cse.ogi.edu
Fri, 14 Jun 2002 07:41:15 -0700


reid        2002/06/14 07:41:15 PDT

  Modified files:
    src                  Makefile.in builtin.c connect.h ffi.c 
                         input.c iomonad.c machdep.c parser.y 
                         plugin.c prelude.h static.c storage.c 
                         storage.h type.c 
  Added files:
    src                  HsFFI.h 
  Log:
  Last of the ffi commits.
  Hopefully won't break anything...
  
  Here's how to test it on your machine:
  
  make && ./hugs -P../lib:../lib/exts: +G test.hs < /dev/null && cc -I. -shared ../lib/exts/MarshalAlloc.c -o ../lib/exts/MarshalAlloc.so && cc -I. -shared ../lib/exts/MarshalUtils.c -o ../lib/exts/MarshalUtils.so && cc -I. -shared ../lib/exts/Storable.c ../lib/exts/Storable_aux.c -o ../lib/exts/Storable.so && cc -I. -shared test.c -o test.so && ./hugs -P../lib:../lib/exts -H test.hs; stty sane
  
  after creating hugs98/src/test.hs containing:
  
    import Foreign
    import Exception
    import Prelude hiding (read)
  
    tests = do
  
      putStrLn "\nTesting sin==mysin (should return lots of Trues)"
      print testSin
  
      putStrLn "\nTesting errno"
      err <- peek errno
      putStrLn $ "errno == " ++ show err
  
      putStrLn "\nTesting puts (and withString)"
      withString0 "Test successful" puts
  
      putStrLn "\nTesting peekArray0"
      s <- withString0 "Test successful" (peekArray0 '\0')
      putStr s
  
      putStrLn "\nTesting open, read and close"
      s <- testRead "test.hs" 200
      putStrLn s
  
      putStrLn "\nTesting open, write and close"
      testWrite "/tmp/test_write" "Test successful"
  
      putStrLn "\nTesting exit"
      exit 3
  
    foreign import ccall "sin" mysin :: Double -> Double
  
    testSin = [ (sin x == mysin x) | x <- [0,0.01 .. 1] ]
  
    foreign import ccall safe "static stdlib.h &errno" errno :: Ptr Int
  
    withString s  = bracket (newArray s)       free
    withString0 s = bracket (newArray0 '\0' s) free
    withBuffer sz m = do
      b <- mallocArray sz
      sz' <- m b
      s <- peekArray sz' b
      free b
      return s
  
    foreign import ccall puts :: Ptr Char -> IO Int
  
    foreign import ccall "open" open'  :: Ptr Char -> Int -> IO Int
    foreign import ccall "open" open2' :: Ptr Char -> Int -> Int -> IO Int
    foreign import ccall "creat" creat' :: Ptr Char -> Int -> IO Int
    foreign import ccall        close :: Int -> IO Int
    foreign import ccall "read" read' :: Int -> Ptr Char -> Int -> IO Int
    foreign import ccall "write" write' :: Int -> Ptr Char -> Int -> IO Int
  
    creat s m   = withString0 s $ \s' -> unix "creat" $ creat' s' m
    open s m    = withString0 s $ \s' -> unix "open"  $ open' s' m
    open2 s m n = withString0 s $ \s' -> unix "open2" $ open2' s' m n
    write fd s  = withString0 s $ \s' -> unix "write" $ write' fd s' (length s)
    read  fd sz = withBuffer sz $ \s' -> unix "read"  $ read' fd s' sz
  
    unix s m = do
      x <- m
      if x < 0
       then do
         err <- peek errno
         ioError $ userError $ s ++ ": " ++ show (x,err)
       else return x
  
    testRead fn sz = bracket (open fn 0) close (flip read sz)
    testWrite fn s = bracket (open2 fn (512+64+1) 511) close (flip write s)
  
    foreign import ccall exit :: Int -> IO ()
  
    -- Various bits of rubbish.
    -- foreign import ccall "static stdlib.h exit" (***) :: Ptr Char -> Ptr Char -> IO Int
    --
    -- foreign import ccall safe "static stdlib.h printf" (+++) :: Ptr Char -> Ptr Char -> IO Int
    -- foreign import ccall safe "static stdlib.h &errno" illegal_foo :: Ptr Int
    --
    -- foreign import ccall safe "wrapper" illegal_bar :: Char -> IO (FunPtr Char)
    -- foreign import ccall safe "dynamic" illegal_baz :: FunPtr Char -> Char
  
    -- foreign export ccall "id_charstar" id :: Ptr Char -> Ptr Char
  
  Here's the changes:
  
  HsFFI.h Makefile.in
  
    HsFFI.h defines the ffi-mandated types.
    It really ought to be autoconfed (current settings suits an x86/gcc).
  
    Makefile updated to include HsFFI.h
  
  machdep.c prelude.h
  
    Int64 support
  
  static.c type.c parser.y input.c storage.c storage.h connect.h
  
    New syntax and related frontend changes.
  
  ffi.c
  
    Tweak code generator to match changes.
    Extend with many new ffi-supported types
  
  builtin.c iomonad.c plugin.c
  
    Runtime support.
  
    Big change is addition of HugsAPI4 and associated operations.
    This extends HugsAPI3 (greencard support) in the usual way.
    Note: I'd like to eliminate all direct greencard support and
    do a big cleanup ASAP.  There's a lot of accumulated cruft in there.
  
  Revision  Changes    Path
  1.13      +16 -16    hugs98/src/Makefile.in
  1.24      +186 -37   hugs98/src/builtin.c
  1.36      +17 -10    hugs98/src/connect.h
  1.6       +281 -177  hugs98/src/ffi.c
  1.43      +15 -39    hugs98/src/input.c
  1.27      +15 -13    hugs98/src/iomonad.c
  1.51      +47 -2     hugs98/src/machdep.c
  1.33      +14 -44    hugs98/src/parser.y
  1.5       +13 -36    hugs98/src/plugin.c
  1.35      +4 -2      hugs98/src/prelude.h
  1.70      +283 -225  hugs98/src/static.c
  1.41      +16 -4     hugs98/src/storage.c
  1.37      +26 -17    hugs98/src/storage.h
  1.48      +33 -2     hugs98/src/type.c