@@ -15,6 +15,7 @@ data CType = I8 | I16 | I32 | I64 | FLOAT | DOUBLE | PTR
15data Composite = T CType | ARRAY Int Composite | STRUCT (List Composite) | UNION (List Composite)
15data Composite = T CType | ARRAY Int Composite | STRUCT (List Composite) | UNION (List Composite)
16 | PACKEDSTRUCT (List Composite)
16 | PACKEDSTRUCT (List Composite)
18+||| Implicit conversion of primitive C types to composites
18implicit mkComposite : CType -> Composite
19implicit mkComposite : CType -> Composite
19mkComposite x = T x
20mkComposite x = T x
@@ -34,6 +35,7 @@ Show Composite where
34 show (UNION xs) = "UNION " ++ show xs
35 show (UNION xs) = "UNION " ++ show xs
35 show (PACKEDSTRUCT xs) = "PACKEDSTRUCT " ++ show xs
36 show (PACKEDSTRUCT xs) = "PACKEDSTRUCT " ++ show xs
38+||| What Idris type the C type is marshalled to
37translate : CType -> Type
39translate : CType -> Type
38translate I8 = Bits8
40translate I8 = Bits8
39translate I16 = Bits16
41translate I16 = Bits16
@@ -44,6 +46,7 @@ translate DOUBLE = Double
44translate PTR = Ptr
46translate PTR = Ptr
47 sizeOfCT : CType -> Int
50 sizeOfCT : CType -> Int
48 sizeOfCT I8 = 1
51 sizeOfCT I8 = 1
49 sizeOfCT I16 = 2
52 sizeOfCT I16 = 2
@@ -53,6 +56,8 @@ mutual
53 sizeOfCT DOUBLE = 8
56 sizeOfCT DOUBLE = 8
54 sizeOfCT PTR = prim__sizeofPtr
57 sizeOfCT PTR = prim__sizeofPtr
59+ ||| Size of value of the type in bytes
56 sizeOf : Composite -> Int
61 sizeOf : Composite -> Int
57 sizeOf (T ct) = sizeOfCT ct
62 sizeOf (T ct) = sizeOfCT ct
58 sizeOf (ARRAY n t) = n * sizeOf t
63 sizeOf (ARRAY n t) = n * sizeOf t
@@ -60,6 +65,7 @@ mutual
60 sizeOf (UNION xs) = foldl (\acc, x =>max acc $ sizeOf x) 0 xs
65 sizeOf (UNION xs) = foldl (\acc, x =>max acc $ sizeOf x) 0 xs
61 sizeOf (PACKEDSTRUCT xs) = foldl (\acc, x => acc + sizeOf x) 0 xs
66 sizeOf (PACKEDSTRUCT xs) = foldl (\acc, x => acc + sizeOf x) 0 xs
63 alignOfCT : CType -> Int
69 alignOfCT : CType -> Int
64 alignOfCT I8 = 1
70 alignOfCT I8 = 1
65 alignOfCT I16 = 2
71 alignOfCT I16 = 2
@@ -69,6 +75,8 @@ mutual
69 alignOfCT DOUBLE = prim__sizeofPtr
75 alignOfCT DOUBLE = prim__sizeofPtr
70 alignOfCT PTR = prim__sizeofPtr
76 alignOfCT PTR = prim__sizeofPtr
78+ ||| Alignment requirement of the type
72 alignOf : Composite -> Int
80 alignOf : Composite -> Int
73 alignOf (T t) = alignOfCT t
81 alignOf (T t) = alignOfCT t
74 alignOf (ARRAY n t) = alignOf t
82 alignOf (ARRAY n t) = alignOf t
@@ -76,12 +84,15 @@ mutual
76 alignOf (UNION xs) = foldl (\acc, x => max acc $ alignOf x) 0 xs
84 alignOf (UNION xs) = foldl (\acc, x => max acc $ alignOf x) 0 xs
77 alignOf (PACKEDSTRUCT xs) = 1
85 alignOf (PACKEDSTRUCT xs) = 1
79 pad : Int -> Int -> Int
88 pad : Int -> Int -> Int
80 pad pos align = let c = pos `mod` align in if c == 0 then 0 else align - c
89 pad pos align = let c = pos `mod` align in if c == 0 then 0 else align - c
82 nextOffset : Composite -> Int -> Int
92 nextOffset : Composite -> Int -> Int
83 nextOffset ty pos = pos + pad pos (alignOf ty)
93 nextOffset ty pos = pos + pad pos (alignOf ty)
85 offsetsStruct : List Composite -> List Int
96 offsetsStruct : List Composite -> List Int
86 offsetsStruct xs = offsets' xs 0
97 offsetsStruct xs = offsets' xs 0
@@ -89,6 +100,7 @@ mutual
89 offsets' [] _ = []
100 offsets' [] _ = []
90 offsets' (x::xs) pos = (nextOffset x pos)::(offsets' xs (nextOffset x pos + sizeOf x))
101 offsets' (x::xs) pos = (nextOffset x pos)::(offsets' xs (nextOffset x pos + sizeOf x))
92 sizeOfStruct : List Composite -> Int
104 sizeOfStruct : List Composite -> Int
93 sizeOfStruct xs = sizeStruct' xs 0 1
105 sizeOfStruct xs = sizeStruct' xs 0 1
@@ -97,6 +109,8 @@ mutual
97 sizeStruct' (x::xs) pos maxAlign = sizeStruct' xs (nextOffset x pos + sizeOf x)
109 sizeStruct' (x::xs) pos maxAlign = sizeStruct' xs (nextOffset x pos + sizeOf x)
98 (max maxAlign (alignOf x))
110 (max maxAlign (alignOf x))
112+||| Number of fields in a composite type
100fields : Composite -> Nat
114fields : Composite -> Nat
101fields (STRUCT xs) = length xs
115fields (STRUCT xs) = length xs
102fields (PACKEDSTRUCT xs) = length xs
116fields (PACKEDSTRUCT xs) = length xs
@@ -104,6 +118,7 @@ fields (UNION xs) = length xs
104fields (ARRAY n _) = toNat n
118fields (ARRAY n _) = toNat n
105fields (T _) = 1
119fields (T _) = 1
107offsetsPacked : List Composite -> List Int
122offsetsPacked : List Composite -> List Int
108offsetsPacked xs = offsets' xs [] 0
123offsetsPacked xs = offsets' xs [] 0
@@ -117,6 +132,7 @@ indexOrFail i xs = case index' i xs of
117 Just x => x
132 Just x => x
118 Nothing => error "Out of bounds access"
133 Nothing => error "Out of bounds access"
135+||| The offset of a firld in a composite type
121offset : Composite -> Nat -> Int
137offset : Composite -> Nat -> Int
122offset (STRUCT xs) i = indexOrFail i (offsetsStruct xs)
138offset (STRUCT xs) i = indexOrFail i (offsetsStruct xs)
@@ -124,6 +140,8 @@ offset (PACKEDSTRUCT xs) i = indexOrFail i (offsetsPacked xs)
124offset (ARRAY _ t) i = sizeOf t * toIntNat i
140offset (ARRAY _ t) i = sizeOf t * toIntNat i
125offset (T _) _ = 0
141offset (T _) _ = 0
143+||| All offsets of a composite type
127offsets : Composite -> List Int
145offsets : Composite -> List Int
128offsets (STRUCT xs) = offsetsStruct xs
146offsets (STRUCT xs) = offsetsStruct xs
129offsets (PACKEDSTRUCT xs) = offsetsPacked xs
147offsets (PACKEDSTRUCT xs) = offsetsPacked xs
@@ -131,7 +149,7 @@ offsets (UNION xs) = replicate (length xs) 0
131offsets (ARRAY n t) = [ x*sizeOf t | x <- [0..n-1]]
149offsets (ARRAY n t) = [ x*sizeOf t | x <- [0..n-1]]
132offsets (T _) = [0]
150offsets (T _) = [0]
134−
152+||| The type of a field in a composite type.
136fieldType : Composite -> Nat -> Composite
154fieldType : Composite -> Nat -> Composite
137fieldType (STRUCT xs@(y::_)) i = indexOrFail i xs
155fieldType (STRUCT xs@(y::_)) i = indexOrFail i xs