@@ -3,7 +3,6 @@
3module CFFI.Types
3module CFFI.Types
5import Data.Vect
5import Data.Vect
8%access public export
7%access public export
9%default partial
8%default partial
@@ -11,6 +10,7 @@ import Data.HVect
11||| An universe of C types.
10||| An universe of C types.
12data CType = I8 | I16 | I32 | I64 | FLOAT | DOUBLE | PTR
11data CType = I8 | I16 | I32 | I64 | FLOAT | DOUBLE | PTR
13+||| Composites of C types
14data Composite = T CType | ARRAY Int Composite | STRUCT (List Composite) | UNION (List Composite)
14data Composite = T CType | ARRAY Int Composite | STRUCT (List Composite) | UNION (List Composite)
15 | PACKEDSTRUCT (List Composite)
15 | PACKEDSTRUCT (List Composite)
@@ -42,95 +42,87 @@ translate FLOAT = Double
42translate DOUBLE = Double
42translate DOUBLE = Double
43translate PTR = Ptr
43translate PTR = Ptr
45−translateComp : Composite -> Type
46−translateComp (T ct) = translate ct
47−translateComp (ARRAY n t) = Vect (toNat n) (translateComp t)
48−translateComp (STRUCT xs) = HVect $ fromList (map translateComp xs)
49−translateComp (UNION xs) = HVect $ fromList (map translateComp xs)
50−translateComp (PACKEDSTRUCT xs) = HVect $ fromList (map translateComp xs)
53− sizeOf : CType -> Int
46+ sizeOfCT : CType -> Int
54− sizeOf I8 = 1
47+ sizeOfCT I8 = 1
55− sizeOf I16 = 2
48+ sizeOfCT I16 = 2
56− sizeOf I32 = 4
49+ sizeOfCT I32 = 4
57− sizeOf I64 = 8
50+ sizeOfCT I64 = 8
58− sizeOf FLOAT = 4
51+ sizeOfCT FLOAT = 4
59− sizeOf DOUBLE = 8
52+ sizeOfCT DOUBLE = 8
60− sizeOf PTR = prim__sizeofPtr
53+ sizeOfCT PTR = prim__sizeofPtr
62− sizeOfComp : Composite -> Int
55+ sizeOf : Composite -> Int
63− sizeOfComp (T ct) = sizeOf ct
56+ sizeOf (T ct) = sizeOfCT ct
64− sizeOfComp (ARRAY n t) = n * sizeOfComp t
57+ sizeOf (ARRAY n t) = n * sizeOf t
65− sizeOfComp (STRUCT xs) = sizeOfStruct xs
58+ sizeOf (STRUCT xs) = sizeOfStruct xs
66− sizeOfComp (UNION xs) = foldl (\acc, x =>max acc $ sizeOfComp x) 0 xs
59+ sizeOf (UNION xs) = foldl (\acc, x =>max acc $ sizeOf x) 0 xs
67− sizeOfComp (PACKEDSTRUCT xs) = foldl (\acc, x => acc + sizeOfComp x) 0 xs
60+ sizeOf (PACKEDSTRUCT xs) = foldl (\acc, x => acc + sizeOf x) 0 xs
69− alignOf : CType -> Int
62+ alignOfCT : CType -> Int
70− alignOf I8 = 1
63+ alignOfCT I8 = 1
71− alignOf I16 = 2
64+ alignOfCT I16 = 2
72− alignOf I32 = 4
65+ alignOfCT I32 = 4
73− alignOf I64 = prim__sizeofPtr
66+ alignOfCT I64 = prim__sizeofPtr
74− alignOf FLOAT = 4
67+ alignOfCT FLOAT = 4
75− alignOf DOUBLE = prim__sizeofPtr
68+ alignOfCT DOUBLE = prim__sizeofPtr
76− alignOf PTR = prim__sizeofPtr
69+ alignOfCT PTR = prim__sizeofPtr
78− alignOfComp : Composite -> Int
71+ alignOf : Composite -> Int
79− alignOfComp (T t) = alignOf t
72+ alignOf (T t) = alignOfCT t
80− alignOfComp (ARRAY n t) = alignOfComp t
73+ alignOf (ARRAY n t) = alignOf t
81− alignOfComp (STRUCT xs) = foldl (\acc, x => max acc $ alignOfComp x) 0 xs
74+ alignOf (STRUCT xs) = foldl (\acc, x => max acc $ alignOf x) 0 xs
82− alignOfComp (UNION xs) = foldl (\acc, x => max acc $ alignOfComp x) 0 xs
75+ alignOf (UNION xs) = foldl (\acc, x => max acc $ alignOf x) 0 xs
83− alignOfComp (PACKEDSTRUCT xs) = 1
76+ alignOf (PACKEDSTRUCT xs) = 1
78+ pad : Int -> Int -> Int
79+ pad pos align = let c = pos `mod` align in if c == 0 then 0 else align - c
81+ nextOffset : Composite -> Int -> Int
82+ nextOffset ty pos = pos + pad pos (alignOf ty)
85 offsetsStruct : List Composite -> List Int
84 offsetsStruct : List Composite -> List Int
86− offsetsStruct xs = offsets' xs [] 0
85+ offsetsStruct xs = offsets' xs 0
88− offsets' : List Composite -> List Int -> Int -> List Int
87+ offsets' : List Composite -> Int -> List Int
89− offsets' [] acc _ = reverse acc
88+ offsets' [] _ = []
90− offsets' (x::xs) ys curr = let a = alignOfComp x
89+ offsets' (x::xs) pos = (nextOffset x pos)::(offsets' xs (nextOffset x pos + sizeOf x))
91− padding = let c = curr `mod` a
92− in if c == 0 then 0 else a - c
94− in offsets' xs (off::ys) (off + sizeOfComp x)
96 sizeOfStruct : List Composite -> Int
91 sizeOfStruct : List Composite -> Int
97− sizeOfStruct xs = case (reverse xs, reverse (offsetsStruct xs)) of
92+ sizeOfStruct xs = sizeStruct' xs 0 1
99− (x::_, y::_) => let end = y + sizeOfComp x
100− endPadding = let c = end `mod` align in
101− if c == 0 then 0 else align - c
104− align : Int
94+ sizeStruct' : List Composite -> Int -> Int -> Int
105− align = foldl (\acc, x => max acc $ alignOfComp x) 1 xs
95+ sizeStruct' [] pos maxAlign = pos + (pad pos maxAlign)
96+ sizeStruct' (x::xs) pos maxAlign = sizeStruct' xs (nextOffset x pos + sizeOf x)
97+ (max maxAlign (alignOf x))
107fields : Composite -> Nat
99fields : Composite -> Nat
108fields (STRUCT xs) = length xs
100fields (STRUCT xs) = length xs
109fields (PACKEDSTRUCT xs) = length xs
101fields (PACKEDSTRUCT xs) = length xs
110fields (UNION xs) = length xs
102fields (UNION xs) = length xs
111fields (ARRAY n _) = toNat n
103fields (ARRAY n _) = toNat n
112−fields _ = 1
104+fields (T _) = 1
114offsetsPacked : List Composite -> List Int
106offsetsPacked : List Composite -> List Int
115offsetsPacked xs = offsets' xs [] 0
107offsetsPacked xs = offsets' xs [] 0
117 offsets' : List Composite -> List Int -> Int -> List Int
109 offsets' : List Composite -> List Int -> Int -> List Int
118 offsets' [] acc _ = reverse acc
110 offsets' [] acc _ = reverse acc
119− offsets' (x::xs) acc pos = offsets' xs (pos::acc) (sizeOfComp x)
111+ offsets' (x::xs) acc pos = offsets' xs (pos::acc) (sizeOf x)
121-- TODO: Use index and fix upp proofs.
113-- TODO: Use index and fix upp proofs.
122offset : Composite -> Nat -> Int
114offset : Composite -> Nat -> Int
123offset (STRUCT xs) i = fromMaybe 0 $ index' i (offsetsStruct xs)
115offset (STRUCT xs) i = fromMaybe 0 $ index' i (offsetsStruct xs)
124offset (PACKEDSTRUCT xs) i = fromMaybe 0 $ index' i (offsetsPacked xs)
116offset (PACKEDSTRUCT xs) i = fromMaybe 0 $ index' i (offsetsPacked xs)
125−offset (ARRAY _ t) i = sizeOfComp t * toIntNat i
117+offset (ARRAY _ t) i = sizeOf t * toIntNat i
126−offset _ _ = 0
118+offset (T _) _ = 0
128offsets : Composite -> List Int
120offsets : Composite -> List Int
129offsets (STRUCT xs) = offsetsStruct xs
121offsets (STRUCT xs) = offsetsStruct xs
130offsets (PACKEDSTRUCT xs) = offsetsPacked xs
122offsets (PACKEDSTRUCT xs) = offsetsPacked xs
131offsets (UNION xs) = replicate (length xs) 0
123offsets (UNION xs) = replicate (length xs) 0
132−offsets (ARRAY n t) = [ x*sizeOfComp t | x <- [0..n-1]]
124+offsets (ARRAY n t) = [ x*sizeOf t | x <- [0..n-1]]
133−offsets _ = [0]
125+offsets (T _) = [0]
135-- TODO: handle out of bounds with proofs
127-- TODO: handle out of bounds with proofs
136-- Also choose a better name.
128-- Also choose a better name.