Debian Codegraph @

kernel/primred.ml

raw symbols SCIP
1 (* Reduction of native operators *)
2 open Names
3 open CPrimitives
4 open Retroknowledge
5 open Environ
6 open CErrors
7
8 type _ action_kind =
9 | IncompatTypes : _ prim_type -> Constant.t action_kind
10 | IncompatInd : _ prim_ind -> inductive action_kind
11
12 type exn += IncompatibleDeclarations : 'a action_kind * 'a * 'a -> exn
13
14 let check_same_types typ c1 c2 =
15 if not (Constant.UserOrd.equal c1 c2)
16 then raise (IncompatibleDeclarations (IncompatTypes typ, c1, c2))
17
18 let check_same_inds ind i1 i2 =
19 if not (Ind.UserOrd.equal i1 i2)
20 then raise (IncompatibleDeclarations (IncompatInd ind, i1, i2))
21
22 let add_retroknowledge retro action =
23 match action with
24 | Register_type(typ,c) ->
25 begin match typ with
26 | PT_int63 ->
27 (match retro.retro_int63 with
28 | None -> { retro with retro_int63 = Some c }
29 | Some c' -> check_same_types typ c c'; retro)
30
31 | PT_float64 ->
32 (match retro.retro_float64 with
33 | None -> { retro with retro_float64 = Some c }
34 | Some c' -> check_same_types typ c c'; retro)
35
36 | PT_string ->
37 (match retro.retro_string with
38 | None -> { retro with retro_string = Some c }
39 | Some c' -> check_same_types typ c c'; retro)
40
41 | PT_array ->
42 (match retro.retro_array with
43 | None -> { retro with retro_array = Some c }
44 | Some c' -> check_same_types typ c c'; retro)
45 end
46
47 | Register_ind(pit,ind) ->
48 begin match pit with
49 | PIT_bool ->
50 let r =
51 match retro.retro_bool with
52 | None -> ((ind,1), (ind,2))
53 | Some (((ind',_),_) as t) -> check_same_inds pit ind ind'; t in
54 { retro with retro_bool = Some r }
55 | PIT_carry ->
56 let r =
57 match retro.retro_carry with
58 | None -> ((ind,1), (ind,2))
59 | Some (((ind',_),_) as t) -> check_same_inds pit ind ind'; t in
60 { retro with retro_carry = Some r }
61 | PIT_pair ->
62 let r =
63 match retro.retro_pair with
64 | None -> (ind,1)
65 | Some ((ind',_) as t) -> check_same_inds pit ind ind'; t in
66 { retro with retro_pair = Some r }
67 | PIT_cmp ->
68 let r =
69 match retro.retro_cmp with
70 | None -> ((ind,1), (ind,2), (ind,3))
71 | Some (((ind',_),_,_) as t) -> check_same_inds pit ind ind'; t in
72 { retro with retro_cmp = Some r }
73 | PIT_f_cmp ->
74 let r =
75 match retro.retro_f_cmp with
76 | None -> ((ind,1), (ind,2), (ind,3), (ind,4))
77 | Some (((ind',_),_,_,_) as t) -> check_same_inds pit ind ind'; t in
78 { retro with retro_f_cmp = Some r }
79 | PIT_f_class ->
80 let r =
81 match retro.retro_f_class with
82 | None -> ((ind,1), (ind,2), (ind,3), (ind,4),
83 (ind,5), (ind,6), (ind,7), (ind,8),
84 (ind,9))
85 | Some (((ind',_),_,_,_,_,_,_,_,_) as t) ->
86 check_same_inds pit ind ind'; t in
87 { retro with retro_f_class = Some r }
88 end
89
90 let add_retroknowledge env action =
91 set_retroknowledge env (add_retroknowledge (Environ.retroknowledge env) action)
92
93 let get_int_type env =
94 match (Environ.retroknowledge env).retro_int63 with
95 | Some c -> c
96 | None -> anomaly Pp.(str"Reduction of primitive: int63 not registered")
97
98 let get_float_type env =
99 match (Environ.retroknowledge env).retro_float64 with
100 | Some c -> c
101 | None -> anomaly Pp.(str"Reduction of primitive: float64 not registered")
102
103 let get_string_type env =
104 match (Environ.retroknowledge env).retro_string with
105 | Some c -> c
106 | None -> anomaly Pp.(str"Reduction of primitive: string not registered")
107
108 let get_cmp_type env =
109 match (Environ.retroknowledge env).retro_cmp with
110 | Some (((mindcmp,_),_),_,_) ->
111 Constant.make (MutInd.user mindcmp) (MutInd.canonical mindcmp)
112 | None -> anomaly Pp.(str"Reduction of primitive: comparison not registered")
113
114 let get_bool_constructors env =
115 match (Environ.retroknowledge env).retro_bool with
116 | Some r -> r
117 | None -> anomaly Pp.(str"Reduction of primitive: bool not registered")
118
119 let get_carry_constructors env =
120 match (Environ.retroknowledge env).retro_carry with
121 | Some r -> r
122 | None -> anomaly Pp.(str"Reduction of primitive: carry not registered")
123
124 let get_pair_constructor env =
125 match (Environ.retroknowledge env).retro_pair with
126 | Some c -> c
127 | None -> anomaly Pp.(str"Reduction of primitive: pair not registered")
128
129 let get_cmp_constructors env =
130 match (Environ.retroknowledge env).retro_cmp with
131 | Some r -> r
132 | None -> anomaly Pp.(str"Reduction of primitive: cmp not registered")
133
134 let get_f_cmp_constructors env =
135 match (Environ.retroknowledge env).retro_f_cmp with
136 | Some r -> r
137 | None -> anomaly Pp.(str"Reduction of primitive: fcmp not registered")
138
139 let get_f_class_constructors env =
140 match (Environ.retroknowledge env).retro_f_class with
141 | Some r -> r
142 | None -> anomaly Pp.(str"Reduction of primitive: fclass not registered")
143
144 exception NativeDestKO
145
146 module type RedNativeEntries =
147 sig
148 type elem
149 type args
150 type evd (* will be unit in kernel, evar_map outside *)
151 type uinstance
152
153 val get : args -> int -> elem
154 val get_int : evd -> elem -> Uint63.t
155 val get_float : evd -> elem -> Float64.t
156 val get_string : evd -> elem -> Pstring.t
157 val get_parray : evd -> elem -> elem Parray.t
158 val mkInt : env -> Uint63.t -> elem
159 val mkFloat : env -> Float64.t -> elem
160 val mkString : env -> Pstring.t -> elem
161 val mkBool : env -> bool -> elem
162 val mkCarry : env -> bool -> elem -> elem (* true if carry *)
163 val mkIntPair : env -> elem -> elem -> elem
164 val mkFloatIntPair : env -> elem -> elem -> elem
165 val mkLt : env -> elem
166 val mkEq : env -> elem
167 val mkGt : env -> elem
168 val mkFLt : env -> elem
169 val mkFEq : env -> elem
170 val mkFGt : env -> elem
171 val mkFNotComparable : env -> elem
172 val mkPNormal : env -> elem
173 val mkNNormal : env -> elem
174 val mkPSubn : env -> elem
175 val mkNSubn : env -> elem
176 val mkPZero : env -> elem
177 val mkNZero : env -> elem
178 val mkPInf : env -> elem
179 val mkNInf : env -> elem
180 val mkNaN : env -> elem
181 val mkArray : env -> uinstance -> elem Parray.t -> elem -> elem
182 end
183
184 module type RedNative =
185 sig
186 type elem
187 type args
188 type evd
189 type uinstance
190 val red_prim : env -> evd -> CPrimitives.t -> uinstance -> args -> elem option
191 end
192
193 module RedNative (E:RedNativeEntries) :
194 RedNative with type elem = E.elem
195 with type args = E.args
196 with type evd = E.evd
197 with type uinstance = E.uinstance =
198 struct
199 type elem = E.elem
200 type args = E.args
201 type evd = E.evd
202 type uinstance = E.uinstance
203
204 let get_int evd args i = E.get_int evd (E.get args i)
205
206 let get_int1 evd args = get_int evd args 0
207
208 let get_int2 evd args = get_int evd args 0, get_int evd args 1
209
210 let get_int3 evd args =
211 get_int evd args 0, get_int evd args 1, get_int evd args 2
212
213 let get_float evd args i = E.get_float evd (E.get args i)
214
215 let get_float1 evd args = get_float evd args 0
216
217 let get_float2 evd args = get_float evd args 0, get_float evd args 1
218
219 let get_parray evd args i = E.get_parray evd (E.get args i)
220
221 let get_string evd args i = E.get_string evd (E.get args i)
222
223 let red_prim_aux env evd op u args =
224 let open CPrimitives in
225 match op with
226 | Int63head0 ->
227 let i = get_int1 evd args in E.mkInt env (Uint63.head0 i)
228 | Int63tail0 ->
229 let i = get_int1 evd args in E.mkInt env (Uint63.tail0 i)
230 | Int63add ->
231 let i1, i2 = get_int2 evd args in E.mkInt env (Uint63.add i1 i2)
232 | Int63sub ->
233 let i1, i2 = get_int2 evd args in E.mkInt env (Uint63.sub i1 i2)
234 | Int63mul ->
235 let i1, i2 = get_int2 evd args in E.mkInt env (Uint63.mul i1 i2)
236 | Int63div ->
237 let i1, i2 = get_int2 evd args in E.mkInt env (Uint63.div i1 i2)
238 | Int63mod ->
239 let i1, i2 = get_int2 evd args in E.mkInt env (Uint63.rem i1 i2)
240 | Int63divs ->
241 let i1, i2 = get_int2 evd args in E.mkInt env (Uint63.divs i1 i2)
242 | Int63mods ->
243 let i1, i2 = get_int2 evd args in E.mkInt env (Uint63.rems i1 i2)
244 | Int63lsr ->
245 let i1, i2 = get_int2 evd args in E.mkInt env (Uint63.l_sr i1 i2)
246 | Int63lsl ->
247 let i1, i2 = get_int2 evd args in E.mkInt env (Uint63.l_sl i1 i2)
248 | Int63asr ->
249 let i1, i2 = get_int2 evd args in E.mkInt env (Uint63.a_sr i1 i2)
250 | Int63land ->
251 let i1, i2 = get_int2 evd args in E.mkInt env (Uint63.l_and i1 i2)
252 | Int63lor ->
253 let i1, i2 = get_int2 evd args in E.mkInt env (Uint63.l_or i1 i2)
254 | Int63lxor ->
255 let i1, i2 = get_int2 evd args in E.mkInt env (Uint63.l_xor i1 i2)
256 | Int63addc ->
257 let i1, i2 = get_int2 evd args in
258 let s = Uint63.add i1 i2 in
259 E.mkCarry env (Uint63.lt s i1) (E.mkInt env s)
260 | Int63subc ->
261 let i1, i2 = get_int2 evd args in
262 let s = Uint63.sub i1 i2 in
263 E.mkCarry env (Uint63.lt i1 i2) (E.mkInt env s)
264 | Int63addCarryC ->
265 let i1, i2 = get_int2 evd args in
266 let s = Uint63.add (Uint63.add i1 i2) (Uint63.of_int 1) in
267 E.mkCarry env (Uint63.le s i1) (E.mkInt env s)
268 | Int63subCarryC ->
269 let i1, i2 = get_int2 evd args in
270 let s = Uint63.sub (Uint63.sub i1 i2) (Uint63.of_int 1) in
271 E.mkCarry env (Uint63.le i1 i2) (E.mkInt env s)
272 | Int63mulc ->
273 let i1, i2 = get_int2 evd args in
274 let (h, l) = Uint63.mulc i1 i2 in
275 E.mkIntPair env (E.mkInt env h) (E.mkInt env l)
276 | Int63diveucl ->
277 let i1, i2 = get_int2 evd args in
278 let q,r = Uint63.div i1 i2, Uint63.rem i1 i2 in
279 E.mkIntPair env (E.mkInt env q) (E.mkInt env r)
280 | Int63div21 ->
281 let i1, i2, i3 = get_int3 evd args in
282 let q,r = Uint63.div21 i1 i2 i3 in
283 E.mkIntPair env (E.mkInt env q) (E.mkInt env r)
284 | Int63addMulDiv ->
285 let p, i, j = get_int3 evd args in
286 E.mkInt env
287 (Uint63.l_or
288 (Uint63.l_sl i p)
289 (Uint63.l_sr j (Uint63.sub (Uint63.of_int Uint63.uint_size) p)))
290 | Int63eq ->
291 let i1, i2 = get_int2 evd args in
292 E.mkBool env (Uint63.equal i1 i2)
293 | Int63lt ->
294 let i1, i2 = get_int2 evd args in
295 E.mkBool env (Uint63.lt i1 i2)
296 | Int63le ->
297 let i1, i2 = get_int2 evd args in
298 E.mkBool env (Uint63.le i1 i2)
299 | Int63lts ->
300 let i1, i2 = get_int2 evd args in
301 E.mkBool env (Uint63.lts i1 i2)
302 | Int63les ->
303 let i1, i2 = get_int2 evd args in
304 E.mkBool env (Uint63.les i1 i2)
305 | Int63compare ->
306 let i1, i2 = get_int2 evd args in
307 begin match Uint63.compare i1 i2 with
308 | x when x < 0 -> E.mkLt env
309 | 0 -> E.mkEq env
310 | _ -> E.mkGt env
311 end
312 | Int63compares ->
313 let i1, i2 = get_int2 evd args in
314 begin match Uint63.compares i1 i2 with
315 | x when x < 0 -> E.mkLt env
316 | 0 -> E.mkEq env
317 | _ -> E.mkGt env
318 end
319 | Float64opp ->
320 let f = get_float1 evd args in E.mkFloat env (Float64.opp f)
321 | Float64abs ->
322 let f = get_float1 evd args in E.mkFloat env (Float64.abs f)
323 | Float64eq ->
324 let i1, i2 = get_float2 evd args in
325 E.mkBool env (Float64.eq i1 i2)
326 | Float64lt ->
327 let i1, i2 = get_float2 evd args in
328 E.mkBool env (Float64.lt i1 i2)
329 | Float64le ->
330 let i1, i2 = get_float2 evd args in
331 E.mkBool env (Float64.le i1 i2)
332 | Float64compare ->
333 let f1, f2 = get_float2 evd args in
334 (match Float64.compare f1 f2 with
335 | Float64.FEq -> E.mkFEq env
336 | Float64.FLt -> E.mkFLt env
337 | Float64.FGt -> E.mkFGt env
338 | Float64.FNotComparable -> E.mkFNotComparable env)
339 | Float64equal ->
340 let f1, f2 = get_float2 evd args in
341 E.mkBool env (Float64.equal f1 f2)
342 | Float64classify ->
343 let f = get_float1 evd args in
344 (match Float64.classify f with
345 | Float64.PNormal -> E.mkPNormal env
346 | Float64.NNormal -> E.mkNNormal env
347 | Float64.PSubn -> E.mkPSubn env
348 | Float64.NSubn -> E.mkNSubn env
349 | Float64.PZero -> E.mkPZero env
350 | Float64.NZero -> E.mkNZero env
351 | Float64.PInf -> E.mkPInf env
352 | Float64.NInf -> E.mkNInf env
353 | Float64.NaN -> E.mkNaN env)
354 | Float64add ->
355 let f1, f2 = get_float2 evd args in E.mkFloat env (Float64.add f1 f2)
356 | Float64sub ->
357 let f1, f2 = get_float2 evd args in E.mkFloat env (Float64.sub f1 f2)
358 | Float64mul ->
359 let f1, f2 = get_float2 evd args in E.mkFloat env (Float64.mul f1 f2)
360 | Float64div ->
361 let f1, f2 = get_float2 evd args in E.mkFloat env (Float64.div f1 f2)
362 | Float64sqrt ->
363 let f = get_float1 evd args in E.mkFloat env (Float64.sqrt f)
364 | Float64ofUint63 ->
365 let i = get_int1 evd args in E.mkFloat env (Float64.of_uint63 i)
366 | Float64normfr_mantissa ->
367 let f = get_float1 evd args in E.mkInt env (Float64.normfr_mantissa f)
368 | Float64frshiftexp ->
369 let f = get_float1 evd args in
370 let (m,e) = Float64.frshiftexp f in
371 E.mkFloatIntPair env (E.mkFloat env m) (E.mkInt env e)
372 | Float64ldshiftexp ->
373 let f = get_float evd args 0 in
374 let e = get_int evd args 1 in
375 E.mkFloat env (Float64.ldshiftexp f e)
376 | Float64next_up ->
377 let f = get_float1 evd args in E.mkFloat env (Float64.next_up f)
378 | Float64next_down ->
379 let f = get_float1 evd args in E.mkFloat env (Float64.next_down f)
380 | Arraymake ->
381 let ty = E.get args 0 in
382 let i = get_int evd args 1 in
383 let d = E.get args 2 in
384 E.mkArray env u (Parray.make i d) ty
385 | Arrayget ->
386 let t = get_parray evd args 1 in
387 let i = get_int evd args 2 in
388 Parray.get t i
389 | Arraydefault ->
390 let t = get_parray evd args 1 in
391 Parray.default t
392 | Arrayset ->
393 let ty = E.get args 0 in
394 let t = get_parray evd args 1 in
395 let i = get_int evd args 2 in
396 let a = E.get args 3 in
397 let t' = Parray.set t i a in
398 E.mkArray env u t' ty
399 | Arraycopy ->
400 let ty = E.get args 0 in
401 let t = get_parray evd args 1 in
402 let t' = Parray.copy t in
403 E.mkArray env u t' ty
404 | Arraylength ->
405 let t = get_parray evd args 1 in
406 E.mkInt env (Parray.length t)
407 | Stringmake ->
408 let i = get_int evd args 0 in
409 let c = get_int evd args 1 in
410 E.mkString env (Pstring.make i c)
411 | Stringlength ->
412 let s = get_string evd args 0 in
413 E.mkInt env (Pstring.length s)
414 | Stringget ->
415 let s = get_string evd args 0 in
416 let i = get_int evd args 1 in
417 E.mkInt env (Pstring.get s i)
418 | Stringsub ->
419 let s = get_string evd args 0 in
420 let off = get_int evd args 1 in
421 let len = get_int evd args 2 in
422 E.mkString env (Pstring.sub s off len)
423 | Stringcat ->
424 let s1 = get_string evd args 0 in
425 let s2 = get_string evd args 1 in
426 E.mkString env (Pstring.cat s1 s2)
427 | Stringcompare ->
428 let s1 = get_string evd args 0 in
429 let s2 = get_string evd args 1 in
430 begin match Pstring.compare s1 s2 with
431 | x when x < 0 -> E.mkLt env
432 | 0 -> E.mkEq env
433 | _ -> E.mkGt env
434 end
435
436 let red_prim env evd p u args =
437 try
438 let r =
439 red_prim_aux env evd p u args
440 in Some r
441 with NativeDestKO -> None
442
443 end