@@ -6,30 +6,35 @@ import Control.IOExcept
6%access public
6%access public
9−data MemoryChunk : Nat -> Type where
9+data MemoryChunk : Nat -> Nat -> Type where
10− CH : Ptr -> (size : Nat) -> MemoryChunk size
10+ CH : Ptr -> MemoryChunk size initialized
13data RawMemory : Effect where
13data RawMemory : Effect where
14− Allocate : (n : Nat) ->
14+ Allocate : (n : Nat) ->
15− RawMemory () (MemoryChunk n) ()
15+ RawMemory () (MemoryChunk n 0) ()
16− Free : RawMemory (MemoryChunk n) () ()
16+ Free : RawMemory (MemoryChunk n i) () ()
17+ Initialize : Bits8 ->
19+ so (i + size <= n) ->
20+ RawMemory (MemoryChunk n i) (MemoryChunk n (i + size)) ()
17− Peek : (offset : Nat) ->
21+ Peek : (offset : Nat) ->
18− (size : Nat) ->
22+ (size : Nat) ->
19− so (offset + size <= n) ->
23+ so (offset + size <= i) ->
20− RawMemory (MemoryChunk n) (MemoryChunk n) (Vect Bits8 size)
24+ RawMemory (MemoryChunk n i) (MemoryChunk n i) (Vect Bits8 size)
21− Poke : (offset : Nat) ->
25+ Poke : (offset : Nat) ->
22− (Vect Bits8 size) ->
26+ (Vect Bits8 size) ->
23− so (offset + size <= n) ->
27+ so (offset <= i && offset + size <= n) ->
24− RawMemory (MemoryChunk n) (MemoryChunk n) ()
28+ RawMemory (MemoryChunk n i) (MemoryChunk n (max i (offset + size))) ()
25− Move : (src : MemoryChunk m) ->
29+ Move : (src : MemoryChunk src_size src_init) ->
26− (dest_offset : Nat) ->
30+ (dst_offset : Nat) ->
27− (src_offset : Nat) ->
31+ (src_offset : Nat) ->
28− (size : Nat) ->
32+ (size : Nat) ->
29− so (dest_offset + size <= n) ->
33+ so (dst_offset <= dst_init && dst_offset + size <= dst_size) ->
30− so (src_offset + size <= m) ->
34+ so (src_offset + size <= src_init) ->
31− RawMemory (MemoryChunk n) (MemoryChunk n) ()
35+ RawMemory (MemoryChunk dst_size dst_init)
36+ (MemoryChunk dst_size (max dst_init (dst_offset + size))) ()
32− GetRawPtr : RawMemory (MemoryChunk n) (MemoryChunk n) (MemoryChunk n)
37+ GetRawPtr : RawMemory (MemoryChunk n i) (MemoryChunk n i) (MemoryChunk n i)
35do_malloc : Nat -> IOExcept String Ptr
40do_malloc : Nat -> IOExcept String Ptr
@@ -40,6 +45,12 @@ do_malloc size with (fromInteger (cast size) == size)
40 else return ptr
45 else return ptr
41 | False = ioe_fail "The target architecture does not support adressing enough memory"
46 | False = ioe_fail "The target architecture does not support adressing enough memory"
49+do_memset : Ptr -> Nat -> Bits8 -> Nat -> IO ()
50+do_memset ptr offset c size
51+ = mkForeign (FFun "idris_memset" [FPtr, FInt, FAny Bits8, FInt] FUnit)
52+ ptr (cast offset) c (cast size)
44do_free : Ptr -> IO ()
55do_free : Ptr -> IO ()
45do_free ptr = mkForeign (FFun "free" [FPtr] FUnit) ptr
56do_free ptr = mkForeign (FFun "free" [FPtr] FUnit) ptr
@@ -68,72 +79,89 @@ do_poke ptr offset (b::bs)
68instance Handler RawMemory (IOExcept String) where
79instance Handler RawMemory (IOExcept String) where
69 handle () (Allocate n) k
80 handle () (Allocate n) k
70 = do ptr <- do_malloc n
81 = do ptr <- do_malloc n
71− k (CH ptr n) ()
82+ k (CH ptr) ()
83+ handle {res = MemoryChunk _ offset} (CH ptr) (Initialize c size _) k
84+ = ioe_lift (do_memset ptr offset c size) $> k (CH ptr) ()
72− handle (CH ptr n) (Free) k
85+ handle (CH ptr) (Free) k
73 = ioe_lift (do_free ptr) $> k () ()
86 = ioe_lift (do_free ptr) $> k () ()
74− handle (CH ptr n) (Peek offset size _) k
87+ handle (CH ptr) (Peek offset size _) k
75 = do res <- ioe_lift (do_peek ptr offset size)
88 = do res <- ioe_lift (do_peek ptr offset size)
76− k (CH ptr n) res
89+ k (CH ptr) res
77− handle (CH ptr n) (Poke offset content _) k
90+ handle (CH ptr) (Poke offset content _) k
78 = do ioe_lift (do_poke ptr offset content)
91 = do ioe_lift (do_poke ptr offset content)
79− k (CH ptr n) ()
92+ k (CH ptr) ()
80− handle (CH dest_ptr n) (Move (CH src_ptr m) dest_offset src_offset size _ _) k
93+ handle (CH dest_ptr) (Move (CH src_ptr) dest_offset src_offset size _ _) k
81 = do ioe_lift (do_memmove dest_ptr src_ptr dest_offset src_offset size)
94 = do ioe_lift (do_memmove dest_ptr src_ptr dest_offset src_offset size)
82− k (CH dest_ptr n) ()
95+ k (CH dest_ptr) ()
83− handle (CH ptr n) (GetRawPtr) k
96+ handle chunk (GetRawPtr) k
84− = k (CH ptr n) (CH ptr n)
97+ = k chunk chunk
86RAW_MEMORY : Type -> EFFECT
99RAW_MEMORY : Type -> EFFECT
87RAW_MEMORY t = MkEff t RawMemory
100RAW_MEMORY t = MkEff t RawMemory
89−allocate : (n : Nat) -> EffM m [RAW_MEMORY ()] [RAW_MEMORY (MemoryChunk n)] ()
102+allocate : (n : Nat) -> EffM m [RAW_MEMORY ()] [RAW_MEMORY (MemoryChunk n 0)] ()
90allocate size = Allocate size
103allocate size = Allocate size
105+initialize : {i : Nat} ->
109+ so (i + size <= n) ->
110+ EffM m [RAW_MEMORY (MemoryChunk n i)] [RAW_MEMORY (MemoryChunk n (i + size))] ()
111+initialize c size prf = Initialize c size prf
92−free : EffM m [RAW_MEMORY (MemoryChunk n)] [RAW_MEMORY ()] ()
113+free : EffM m [RAW_MEMORY (MemoryChunk n i)] [RAW_MEMORY ()] ()
93free = Free
114free = Free
95−peek : {n : Nat} ->
116+peek : {i : Nat} ->
96 (offset : Nat) ->
117 (offset : Nat) ->
97 (size : Nat) ->
118 (size : Nat) ->
98− so (offset + size <= n) ->
119+ so (offset + size <= i) ->
99− Eff m [RAW_MEMORY (MemoryChunk n)] (Vect Bits8 size)
120+ Eff m [RAW_MEMORY (MemoryChunk n i)] (Vect Bits8 size)
100peek offset size prf = Peek offset size prf
121peek offset size prf = Peek offset size prf
102poke : {n : Nat} ->
123poke : {n : Nat} ->
103 (offset : Nat) ->
125 (offset : Nat) ->
104 Vect Bits8 size ->
126 Vect Bits8 size ->
105− so (offset + size <= n) ->
127+ so (offset <= i && offset + size <= n) ->
106− Eff m [RAW_MEMORY (MemoryChunk n)] ()
128+ EffM m [RAW_MEMORY (MemoryChunk n i)] [RAW_MEMORY (MemoryChunk n (max i (offset + size)))] ()
107poke offset content prf = Poke offset content prf
129poke offset content prf = Poke offset content prf
110−getRawPtr : Eff m [RAW_MEMORY (MemoryChunk n)] (MemoryChunk n)
132+getRawPtr : Eff m [RAW_MEMORY (MemoryChunk n i)] (MemoryChunk n i)
111getRawPtr = GetRawPtr
133getRawPtr = GetRawPtr
114move' : {dst_size : Nat} ->
136move' : {dst_size : Nat} ->
115− {src_size : Nat} ->
137+ {dst_init : Nat} ->
116− (src_ptr : MemoryChunk src_size) ->
139+ (src_ptr : MemoryChunk src_size src_init) ->
117 (dst_offset : Nat) ->
140 (dst_offset : Nat) ->
118 (src_offset : Nat) ->
141 (src_offset : Nat) ->
119 (size : Nat) ->
142 (size : Nat) ->
120− so (dst_offset + size <= dst_size) ->
143+ so (dst_offset <= dst_init && dst_offset + size <= dst_size) ->
121− so (src_offset + size <= src_size) ->
144+ so (src_offset + size <= src_init) ->
122− Eff m [RAW_MEMORY (MemoryChunk dst_size)] ()
145+ EffM m [RAW_MEMORY (MemoryChunk dst_size dst_init)]
146+ [RAW_MEMORY (MemoryChunk dst_size (max dst_init (dst_offset + size)))] ()
123move' src_ptr dst_offset src_offset size dst_bounds src_bounds
147move' src_ptr dst_offset src_offset size dst_bounds src_bounds
124 = Move src_ptr dst_offset src_offset size dst_bounds src_bounds
148 = Move src_ptr dst_offset src_offset size dst_bounds src_bounds
126data MoveDescriptor = Dst | Src
150data MoveDescriptor = Dst | Src
128move : {dst_size : Nat} ->
152move : {dst_size : Nat} ->
129 {src_size : Nat} ->
154 {src_size : Nat} ->
130 (dst_offset : Nat) ->
156 (dst_offset : Nat) ->
131 (src_offset : Nat) ->
157 (src_offset : Nat) ->
132 (size : Nat) ->
158 (size : Nat) ->
133− so (dst_offset + size <= dst_size) ->
159+ so (dst_offset <= dst_init && dst_offset + size <= dst_size) ->
134− so (src_offset + size <= src_size) ->
160+ so (src_offset + size <= src_init) ->
135− Eff m [ Dst ::: RAW_MEMORY (MemoryChunk dst_size)
161+ EffM m [ Dst ::: RAW_MEMORY (MemoryChunk dst_size dst_init)
136− , Src ::: RAW_MEMORY (MemoryChunk src_size)] ()
162+ , Src ::: RAW_MEMORY (MemoryChunk src_size src_init)]
163+ [ Dst ::: RAW_MEMORY (MemoryChunk dst_size (max dst_init (dst_offset + size)))
164+ , Src ::: RAW_MEMORY (MemoryChunk src_size src_init)] ()
137move dst_offset src_offset size dst_bounds src_bounds
165move dst_offset src_offset size dst_bounds src_bounds
138 = do src_ptr <- Src :- getRawPtr
166 = do src_ptr <- Src :- getRawPtr
139 Dst :- move' src_ptr dst_offset src_offset size dst_bounds src_bounds
167 Dst :- move' src_ptr dst_offset src_offset size dst_bounds src_bounds