Skip to content

Commit 361726e

Browse files
committed
Use exact arithmetic for printing positive numbers
And also for the computations in name_temporary. Overflowing OCaml's integer types is unlikely in actual use but happened in the past owing to another mistake (see issue #370).
1 parent d5e332e commit 361726e

File tree

1 file changed

+56
-52
lines changed

1 file changed

+56
-52
lines changed

exportclight/ExportClight.ml

Lines changed: 56 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,48 @@ let print_list fn p l =
4343
in plist l;
4444
fprintf p ")@]"
4545

46+
(* Numbers *)
47+
48+
let coqint p n =
49+
let n = camlint_of_coqint n in
50+
if n >= 0l
51+
then fprintf p "(Int.repr %ld)" n
52+
else fprintf p "(Int.repr (%ld))" n
53+
54+
let coqptrofs p n =
55+
let s = Z.to_string n in
56+
if Z.ge n Z.zero
57+
then fprintf p "(Ptrofs.repr %s)" s
58+
else fprintf p "(Ptrofs.repr (%s))" s
59+
60+
let coqint64 p n =
61+
let n = camlint64_of_coqint n in
62+
if n >= 0L
63+
then fprintf p "(Int64.repr %Ld)" n
64+
else fprintf p "(Int64.repr (%Ld))" n
65+
66+
let coqfloat p n =
67+
fprintf p "(Float.of_bits %a)" coqint64 (Floats.Float.to_bits n)
68+
69+
let coqsingle p n =
70+
fprintf p "(Float32.of_bits %a)" coqint (Floats.Float32.to_bits n)
71+
72+
let positive p n =
73+
fprintf p "%s%%positive" (Z.to_string (Z.Zpos n))
74+
75+
let coqN p n =
76+
fprintf p "%s%%N" (Z.to_string (Z.of_N n))
77+
78+
let coqZ p n =
79+
if Z.ge n Z.zero
80+
then fprintf p "%s" (Z.to_string n)
81+
else fprintf p "(%s)" (Z.to_string n)
82+
83+
(* Coq strings *)
84+
85+
let coqstring p s =
86+
fprintf p "\"%s\"" (camlstring_of_coqstring s)
87+
4688
(* Identifiers *)
4789

4890
exception Not_an_identifier
@@ -69,7 +111,7 @@ let ident p id =
69111
let s = Hashtbl.find temp_names id in
70112
fprintf p "%s" s
71113
with Not_found ->
72-
fprintf p "%ld%%positive" (P.to_int32 id)
114+
positive p id
73115

74116
let iter_hashtbl_sorted (h: ('a, string) Hashtbl.t) (f: 'a * string -> unit) =
75117
List.iter f
@@ -85,67 +127,29 @@ let define_idents p =
85127
fprintf p "Definition _%s : ident := $\"%s\".@ "
86128
(sanitize name) name
87129
else
88-
fprintf p "Definition _%s : ident := %ld%%positive.@ "
89-
(sanitize name) (P.to_int32 id)
130+
fprintf p "Definition _%s : ident := %a.@ "
131+
(sanitize name) positive id
90132
with Not_an_identifier ->
91133
());
92134
iter_hashtbl_sorted
93135
temp_names
94136
(fun (id, name) ->
95-
fprintf p "Definition %s : ident := %ld%%positive.@ "
96-
name (P.to_int32 id));
137+
fprintf p "Definition %s : ident := %a.@ "
138+
name positive id);
97139
fprintf p "@ "
98140

99141
let name_temporary t =
100142
if not (Hashtbl.mem string_of_atom t) && not (Hashtbl.mem temp_names t)
101143
then begin
102-
let t1 = P.to_int t and t0 = P.to_int (first_unused_ident ()) in
103-
Hashtbl.add temp_names t (sprintf "_t'%d" (t1 - t0 + 1))
144+
let t0 = first_unused_ident () in
145+
let d = Z.succ (Z.sub (Z.Zpos t) (Z.Zpos t0)) in
146+
Hashtbl.add temp_names t ("_t'" ^ Z.to_string d)
104147
end
105148

106149
let name_opt_temporary = function
107150
| None -> ()
108151
| Some id -> name_temporary id
109152

110-
(* Numbers *)
111-
112-
let coqint p n =
113-
let n = camlint_of_coqint n in
114-
if n >= 0l
115-
then fprintf p "(Int.repr %ld)" n
116-
else fprintf p "(Int.repr (%ld))" n
117-
118-
let coqptrofs p n =
119-
let s = Z.to_string n in
120-
if Z.ge n Z.zero
121-
then fprintf p "(Ptrofs.repr %s)" s
122-
else fprintf p "(Ptrofs.repr (%s))" s
123-
124-
let coqint64 p n =
125-
let n = camlint64_of_coqint n in
126-
if n >= 0L
127-
then fprintf p "(Int64.repr %Ld)" n
128-
else fprintf p "(Int64.repr (%Ld))" n
129-
130-
let coqfloat p n =
131-
fprintf p "(Float.of_bits %a)" coqint64 (Floats.Float.to_bits n)
132-
133-
let coqsingle p n =
134-
fprintf p "(Float32.of_bits %a)" coqint (Floats.Float32.to_bits n)
135-
136-
let coqN p n =
137-
fprintf p "%ld%%N" (N.to_int32 n)
138-
139-
let coqZ p n =
140-
if Z.ge n Z.zero
141-
then fprintf p "%s" (Z.to_string n)
142-
else fprintf p "(%s)" (Z.to_string n)
143-
144-
(* Coq strings *)
145-
146-
let coqstring p s =
147-
fprintf p "\"%s\"" (camlstring_of_coqstring s)
148-
149153
(* Raw attributes *)
150154

151155
let attribute p a =
@@ -269,14 +273,14 @@ let external_function p = function
269273
| EF_memcpy(sz, al) ->
270274
fprintf p "(EF_memcpy %ld %ld)" (Z.to_int32 sz) (Z.to_int32 al)
271275
| EF_annot(kind, text, targs) ->
272-
fprintf p "(EF_annot %ld%%positive %a %a)"
273-
(P.to_int32 kind) coqstring text (print_list asttype) targs
276+
fprintf p "(EF_annot %a %a %a)"
277+
positive kind coqstring text (print_list asttype) targs
274278
| EF_annot_val(kind, text, targ) ->
275-
fprintf p "(EF_annot_val %ld%%positive %a %a)"
276-
(P.to_int32 kind) coqstring text asttype targ
279+
fprintf p "(EF_annot_val %a %a %a)"
280+
positive kind coqstring text asttype targ
277281
| EF_debug(kind, text, targs) ->
278-
fprintf p "(EF_debug %ld%%positive %ld%%positive %a)"
279-
(P.to_int32 kind) (P.to_int32 text) (print_list asttype) targs
282+
fprintf p "(EF_debug %a %a %a)"
283+
positive kind positive text (print_list asttype) targs
280284
| EF_inline_asm(text, sg, clob) ->
281285
fprintf p "@[<hov 2>(EF_inline_asm %a@ %a@ %a)@]"
282286
coqstring text

0 commit comments

Comments
 (0)