@@ -15,6 +15,7 @@ data CType = I8 | I16 | I32 | I64 | FLOAT | DOUBLE | PTR
1515data Composite = T CType | ARRAY Int Composite | STRUCT (List Composite) | UNION (List Composite)
1616 | PACKEDSTRUCT (List Composite)
1717
18+||| Implicit conversion of primitive C types to composites
1819implicit mkComposite : CType -> Composite
1920mkComposite x = T x
2021
@@ -34,6 +35,7 @@ Show Composite where
3435 show (UNION xs) = "UNION " ++ show xs
3536 show (PACKEDSTRUCT xs) = "PACKEDSTRUCT " ++ show xs
3637
38+||| What Idris type the C type is marshalled to
3739translate : CType -> Type
3840translate I8 = Bits8
3941translate I16 = Bits16
@@ -44,6 +46,7 @@ translate DOUBLE = Double
4446translate PTR = Ptr
4547
4648mutual
49+ private
4750 sizeOfCT : CType -> Int
4851 sizeOfCT I8 = 1
4952 sizeOfCT I16 = 2
@@ -53,6 +56,8 @@ mutual
5356 sizeOfCT DOUBLE = 8
5457 sizeOfCT PTR = prim__sizeofPtr
5558
59+ ||| Size of value of the type in bytes
60+ export
5661 sizeOf : Composite -> Int
5762 sizeOf (T ct) = sizeOfCT ct
5863 sizeOf (ARRAY n t) = n * sizeOf t
@@ -60,6 +65,7 @@ mutual
6065 sizeOf (UNION xs) = foldl (\acc, x =>max acc $ sizeOf x) 0 xs
6166 sizeOf (PACKEDSTRUCT xs) = foldl (\acc, x => acc + sizeOf x) 0 xs
6267
68+ private
6369 alignOfCT : CType -> Int
6470 alignOfCT I8 = 1
6571 alignOfCT I16 = 2
@@ -69,6 +75,8 @@ mutual
6975 alignOfCT DOUBLE = prim__sizeofPtr
7076 alignOfCT PTR = prim__sizeofPtr
7177
78+ ||| Alignment requirement of the type
79+ export
7280 alignOf : Composite -> Int
7381 alignOf (T t) = alignOfCT t
7482 alignOf (ARRAY n t) = alignOf t
@@ -76,12 +84,15 @@ mutual
7684 alignOf (UNION xs) = foldl (\acc, x => max acc $ alignOf x) 0 xs
7785 alignOf (PACKEDSTRUCT xs) = 1
7886
87+ private
7988 pad : Int -> Int -> Int
8089 pad pos align = let c = pos `mod` align in if c == 0 then 0 else align - c
8190
91+ private
8292 nextOffset : Composite -> Int -> Int
8393 nextOffset ty pos = pos + pad pos (alignOf ty)
8494
95+ private
8596 offsetsStruct : List Composite -> List Int
8697 offsetsStruct xs = offsets' xs 0
8798 where
@@ -89,6 +100,7 @@ mutual
89100 offsets' [] _ = []
90101 offsets' (x::xs) pos = (nextOffset x pos)::(offsets' xs (nextOffset x pos + sizeOf x))
91102
103+ private
92104 sizeOfStruct : List Composite -> Int
93105 sizeOfStruct xs = sizeStruct' xs 0 1
94106 where
@@ -97,6 +109,8 @@ mutual
97109 sizeStruct' (x::xs) pos maxAlign = sizeStruct' xs (nextOffset x pos + sizeOf x)
98110 (max maxAlign (alignOf x))
99111
112+||| Number of fields in a composite type
113+export
100114fields : Composite -> Nat
101115fields (STRUCT xs) = length xs
102116fields (PACKEDSTRUCT xs) = length xs
@@ -104,6 +118,7 @@ fields (UNION xs) = length xs
104118fields (ARRAY n _) = toNat n
105119fields (T _) = 1
106120
121+private
107122offsetsPacked : List Composite -> List Int
108123offsetsPacked xs = offsets' xs [] 0
109124 where
@@ -117,6 +132,7 @@ indexOrFail i xs = case index' i xs of
117132 Just x => x
118133 Nothing => error "Out of bounds access"
119134
135+||| The offset of a firld in a composite type
120136export
121137offset : Composite -> Nat -> Int
122138offset (STRUCT xs) i = indexOrFail i (offsetsStruct xs)
@@ -124,6 +140,8 @@ offset (PACKEDSTRUCT xs) i = indexOrFail i (offsetsPacked xs)
124140offset (ARRAY _ t) i = sizeOf t * toIntNat i
125141offset (T _) _ = 0
126142
143+||| All offsets of a composite type
144+export
127145offsets : Composite -> List Int
128146offsets (STRUCT xs) = offsetsStruct xs
129147offsets (PACKEDSTRUCT xs) = offsetsPacked xs
@@ -131,7 +149,7 @@ offsets (UNION xs) = replicate (length xs) 0
131149offsets (ARRAY n t) = [ x*sizeOf t | x <- [0..n-1]]
132150offsets (T _) = [0]
133151
134−
152+||| The type of a field in a composite type.
135153export
136154fieldType : Composite -> Nat -> Composite
137155fieldType (STRUCT xs@(y::_)) i = indexOrFail i xs