Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Haskell
Wiki community
Recent changes
Random page
HaskellWiki
Search
Search
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
HSFFIG/Tutorial
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==== Bit fields ==== Bit fields (declared as <tt>int somefield:n</tt>) are supported by '''hsffig''', but there are some additional things that need attention. There is no such thing as ''offsetof'' for bit fields. To implement the <tt>--></tt> and <tt><--</tt> combinators, special inline '''C''' functions are embedded in the hsc code '''hsffig''' generates. This is achieved with the ''#def'' macro that '''hsc2hs''' recognizes. These functions are placed in a special '''C''' file. This file must be compiled and linked to the final executable. Consider the following '''C''' header (''bf.h''): <pre> union bitfields { struct { int a:1; int b:1; int c:1; int d:1; } x; int y; }; </pre> and the Haskell program (''bftest.hs''): <pre> -- Test for bit fields. module Main where import BF_H main = do putStrLn $ "Test of Bit Fields" alloca $ \(bfu :: Ptr U_bitfields) -> do x <- (bfu --> V_x) (x, V_a) <-- fromIntegral 1 (x, V_b) <-- fromIntegral 0 (x, V_c) <-- fromIntegral 0 (x, V_d) <-- fromIntegral 1 y <- (bfu --> V_y) putStrLn $ "y = " ++ (show y) </pre> When '''hsffig''' processes ''bf.h'' it creates ''BF_H.hsc''. When '''hsc2hs''' processes ''BF_H.hsc'' it creates ''BF_H.hs'', and additionally ''BF_H_hsc.h'' and ''BF_H_hsc.c''. The latter is recommended to compile with '''ghc''': <pre> ghc -c BF_H_hsc.c </pre> which provides correct compilation environment, so necessary paths for the runtime header files will be passed to '''gcc''' properly. After all, the executable may be compiled and linked: <pre> ghc -ffi -fglasgow-exts -package HSFFIG --make bftest.hs BF_H_hsc.o -o bftest </pre> If ''bftest'' is run, it prints "9", as one might expect (at least on the x86 architecture).
Summary:
Please note that all contributions to HaskellWiki are considered to be released under simple permissive license (see
HaskellWiki:Copyrights
for details). If you don't want your writing to be edited mercilessly and redistributed at will, then don't submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
DO NOT SUBMIT COPYRIGHTED WORK WITHOUT PERMISSION!
Cancel
Editing help
(opens in new window)
Toggle limited content width