@@ -10,8 +10,12 @@ import Data.HVect
1010
1111||| An universe of C types.
1212data CType = I8 | I16 | I32 | I64 | FLOAT | DOUBLE | PTR
13− | ARRAY Int CType | STRUCT (List CType) | UNION (List CType)
14− | PACKEDSTRUCT (List CType)
13+
14+data Composite = T CType | ARRAY Int Composite | STRUCT (List Composite) | UNION (List Composite)
15+ | PACKEDSTRUCT (List Composite)
16+
17+implicit mkComposite : CType -> Composite
18+mkComposite x = T x
1519
1620Show CType where
1721 show I8 = "I8"
@@ -21,6 +25,9 @@ Show CType where
2125 show FLOAT = "FLOAT"
2226 show DOUBLE = "DOUBLE"
2327 show PTR = "PTR"
28+
29+Show Composite where
30+ show (T ct) = show ct
2431 show (ARRAY n t) = "ARRAY " ++ show n ++ " " ++ show t
2532 show (STRUCT xs) = "STRUCT " ++ show xs
2633 show (UNION xs) = "UNION " ++ show xs
@@ -34,10 +41,13 @@ translate I64 = Bits64
3441translate FLOAT = Double
3542translate DOUBLE = Double
3643translate PTR = Ptr
37−translate (ARRAY n t) = Vect (toNat n) (translate t)
38−translate (STRUCT xs) = HVect $ fromList (map translate xs)
39−translate (UNION xs) = HVect $ fromList (map translate xs)
40−translate (PACKEDSTRUCT xs) = HVect $ fromList (map translate xs)
44+
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)
4151
4252mutual
4353 sizeOf : CType -> Int
@@ -48,10 +58,13 @@ mutual
4858 sizeOf FLOAT = 4
4959 sizeOf DOUBLE = 8
5060 sizeOf PTR = prim__sizeofPtr
51− sizeOf (ARRAY n t) = n * sizeOf t
52− sizeOf (STRUCT xs) = sizeOfStruct xs
53− sizeOf (UNION xs) = foldl (\acc, x =>max acc $ sizeOf x) 0 xs
54− sizeOf (PACKEDSTRUCT xs) = foldl (\acc, x => acc + sizeOf x) 0 xs
61+
62+ sizeOfComp : Composite -> Int
63+ sizeOfComp (T ct) = sizeOf ct
64+ sizeOfComp (ARRAY n t) = n * sizeOfComp t
65+ sizeOfComp (STRUCT xs) = sizeOfStruct xs
66+ sizeOfComp (UNION xs) = foldl (\acc, x =>max acc $ sizeOfComp x) 0 xs
67+ sizeOfComp (PACKEDSTRUCT xs) = foldl (\acc, x => acc + sizeOfComp x) 0 xs
5568
5669 alignOf : CType -> Int
5770 alignOf I8 = 1
@@ -61,64 +74,67 @@ mutual
6174 alignOf FLOAT = 4
6275 alignOf DOUBLE = prim__sizeofPtr
6376 alignOf PTR = prim__sizeofPtr
64− alignOf (ARRAY n t) = alignOf t
65− alignOf (STRUCT xs) = foldl (\acc, x => max acc $ alignOf x) 0 xs
66− alignOf (UNION xs) = foldl (\acc, x => max acc $ alignOf x) 0 xs
67− alignOf (PACKEDSTRUCT xs) = 1
6877
69− offsetsStruct : List CType -> List Int
78+ alignOfComp : Composite -> Int
79+ alignOfComp (T t) = alignOf t
80+ alignOfComp (ARRAY n t) = alignOfComp t
81+ alignOfComp (STRUCT xs) = foldl (\acc, x => max acc $ alignOfComp x) 0 xs
82+ alignOfComp (UNION xs) = foldl (\acc, x => max acc $ alignOfComp x) 0 xs
83+ alignOfComp (PACKEDSTRUCT xs) = 1
84+
85+ offsetsStruct : List Composite -> List Int
7086 offsetsStruct xs = offsets' xs [] 0
7187 where
72− offsets' : List CType -> List Int -> Int -> List Int
88+ offsets' : List Composite -> List Int -> Int -> List Int
7389 offsets' [] acc _ = reverse acc
74− offsets' (x::xs) ys curr = let a = alignOf x
90+ offsets' (x::xs) ys curr = let a = alignOfComp x
7591 padding = let c = curr `mod` a
7692 in if c == 0 then 0 else a - c
7793 off = curr + padding
78− in offsets' xs (off::ys) (off + sizeOf x)
94+ in offsets' xs (off::ys) (off + sizeOfComp x)
7995
80− sizeOfStruct : List CType -> Int
96+ sizeOfStruct : List Composite -> Int
8197 sizeOfStruct xs = case (reverse xs, reverse (offsetsStruct xs)) of
8298 ([], _) => 0
83− (x::_, y::_) => let end = y + sizeOf x
99+ (x::_, y::_) => let end = y + sizeOfComp x
84100 endPadding = let c = end `mod` align in
85101 if c == 0 then 0 else align - c
86102 in end + endPadding
87103 where
88104 align : Int
89− align = foldl (\acc, x => max acc $ alignOf x) 1 xs
105+ align = foldl (\acc, x => max acc $ alignOfComp x) 1 xs
90106
91−fields : CType -> Nat
107+fields : Composite -> Nat
92108fields (STRUCT xs) = length xs
93109fields (PACKEDSTRUCT xs) = length xs
94110fields (UNION xs) = length xs
95111fields (ARRAY n _) = toNat n
96112fields _ = 1
97113
98−offsetsPacked : List CType -> List Int
114+offsetsPacked : List Composite -> List Int
99115offsetsPacked xs = offsets' xs [] 0
100116 where
101− offsets' : List CType -> List Int -> Int -> List Int
117+ offsets' : List Composite -> List Int -> Int -> List Int
102118 offsets' [] acc _ = reverse acc
103− offsets' (x::xs) acc pos = offsets' xs (pos::acc) (sizeOf x)
119+ offsets' (x::xs) acc pos = offsets' xs (pos::acc) (sizeOfComp x)
104120
105121-- TODO: Use index and fix upp proofs.
106−offset : CType -> Nat -> Int
122+offset : Composite -> Nat -> Int
107123offset (STRUCT xs) i = fromMaybe 0 $ index' i (offsetsStruct xs)
108124offset (PACKEDSTRUCT xs) i = fromMaybe 0 $ index' i (offsetsPacked xs)
109−offset (ARRAY _ t) i = sizeOf t * toIntNat i
125+offset (ARRAY _ t) i = sizeOfComp t * toIntNat i
110126offset _ _ = 0
111127
112−offsets : CType -> List Int
128+offsets : Composite -> List Int
113129offsets (STRUCT xs) = offsetsStruct xs
114130offsets (PACKEDSTRUCT xs) = offsetsPacked xs
115131offsets (UNION xs) = replicate (length xs) 0
116−offsets (ARRAY n t) = [ x*sizeOf t | x <- [0..n-1]]
132+offsets (ARRAY n t) = [ x*sizeOfComp t | x <- [0..n-1]]
117133offsets _ = [0]
118134
119135-- TODO: handle out of bounds with proofs
120136-- Also choose a better name.
121−select : CType -> Nat -> CType
137+select : Composite -> Nat -> Composite
122138select (STRUCT xs@(y::_)) i = fromMaybe y (index' i xs)
123139select (PACKEDSTRUCT xs@(y::_)) i = fromMaybe y (index' i xs)
124140select (UNION xs@(y::_)) i = fromMaybe y (index' i xs)