@@ -0,0 +1,96 @@
1+||| Types for interfacing with C.
10+||| An universe of C types.
11+data CType = I8 | I16 | I32 | I64 | FLOAT | DOUBLE | PTR
12+ | ARRAY Int CType | STRUCT (List CType) | UNION (List CType)
13+ | PACKEDSTRUCT (List CType)
15+translate : CType -> Type
17+translate I16 = Bits16
18+translate I32 = Bits32
19+translate I64 = Bits64
20+translate FLOAT = Bits32
21+translate DOUBLE = Double
23+translate (ARRAY n t) = Vect (toNat n) (translate t)
24+translate (STRUCT xs) = HList (map translate xs)
25+translate (UNION xs) = HList (map translate xs)
26+translate (PACKEDSTRUCT xs) = HList (map translate xs)
29+ sizeOf : CType -> Int
36+ sizeOf PTR = prim__sizeofPtr
37+ sizeOf (ARRAY n t) = n * sizeOf t
38+ sizeOf (STRUCT xs) = sizeOfStruct xs
39+ sizeOf (UNION xs) = foldl (\acc, x =>max acc $ sizeOf x) 0 xs
40+ sizeOf (PACKEDSTRUCT xs) = foldl (\acc, x => acc + sizeOf x) 0 xs
42+ alignOf : CType -> Int
46+ alignOf I64 = prim__sizeofPtr
48+ alignOf DOUBLE = prim__sizeofPtr
49+ alignOf PTR = prim__sizeofPtr
50+ alignOf (ARRAY n t) = alignOf t
51+ alignOf (STRUCT xs) = foldl (\acc, x => max acc $ alignOf x) 0 xs
52+ alignOf (UNION xs) = foldl (\acc, x => max acc $ alignOf x) 0 xs
53+ alignOf (PACKEDSTRUCT xs) = 1
55+ offsetsStruct : List CType -> List Int
56+ offsetsStruct xs = offsets' xs [] 0
58+ offsets' : List CType -> List Int -> Int -> List Int
59+ offsets' [] acc _ = reverse acc
60+ offsets' (x::xs) ys curr = let a = alignOf x
61+ padding = let c = curr `mod` a
62+ in if c == 0 then 0 else a - c
64+ in offsets' xs (off::ys) (off + sizeOf x)
66+ sizeOfStruct : List CType -> Int
67+ sizeOfStruct xs = case (reverse xs, reverse (offsetsStruct xs)) of
69+ (x::_, y::_) => let end = y + sizeOf x
70+ endPadding = let c = end `mod` align in
71+ if c == 0 then 0 else align - c
75+ align = foldl (\acc, x => max acc $ alignOf x) 1 xs
78+fields (STRUCT xs) = length xs
79+fields (PACKEDSTRUCT xs) = length xs
80+fields (UNION xs) = length xs
81+fields (ARRAY n _) = toNat n
84+offsetsPacked : List CType -> List Int
85+offsetsPacked xs = offsets' xs [] 0
87+ offsets' : List CType -> List Int -> Int -> List Int
88+ offsets' [] acc _ = reverse acc
89+ offsets' (x::xs) acc pos = offsets' xs (pos::acc) (sizeOf x)
91+offsets : CType -> List Int
92+offsets (STRUCT xs) = offsetsStruct xs
93+offsets (PACKEDSTRUCT xs) = offsetsPacked xs
94+offsets (UNION xs) = replicate (length xs) 0
95+offsets (ARRAY n t) = [ x*sizeOf t | x <- [0..n]]