Skip to content

Commit 8eaff6b

Browse files
committed
clightgen: handle empty names given to padding bit fields
In the Clight AST, padding bit fields (such as `int : 6;`) in composite declarations are given an ident that corresponds to the empty string. Previously, clightgen would give name `_` to this ident, but this is not valid Coq. This commit gives name `empty_ident` to the empty ident. This name does not start with an underscore, so it cannot conflict with the names for regular idents, which all start with `_`.
1 parent 4ba1b9e commit 8eaff6b

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

exportclight/ExportClight.ml

+15-16
Original file line numberDiff line numberDiff line change
@@ -90,23 +90,22 @@ let coqstring p s =
9090

9191
exception Not_an_identifier
9292

93+
let sanitize_char = function
94+
| 'A'..'Z' | 'a'..'z' | '0'..'9' | '_' as c -> c
95+
| ' ' | '$' -> '_'
96+
| _ -> raise Not_an_identifier
97+
9398
let sanitize s =
94-
let s' = Bytes.create (String.length s) in
95-
for i = 0 to String.length s - 1 do
96-
Bytes.set s' i
97-
(match String.get s i with
98-
| 'A'..'Z' | 'a'..'z' | '0'..'9' | '_' as c -> c
99-
| ' ' | '$' -> '_'
100-
| _ -> raise Not_an_identifier)
101-
done;
102-
Bytes.to_string s'
99+
if s <> ""
100+
then "_" ^ String.map sanitize_char s
101+
else "empty_ident"
103102

104103
let temp_names : (ident, string) Hashtbl.t = Hashtbl.create 17
105104

106105
let ident p id =
107106
try
108107
let s = Hashtbl.find string_of_atom id in
109-
fprintf p "_%s" (sanitize s)
108+
fprintf p "%s" (sanitize s)
110109
with Not_found | Not_an_identifier ->
111110
try
112111
let s = Hashtbl.find temp_names id in
@@ -125,10 +124,10 @@ let define_idents p =
125124
(fun (id, name) ->
126125
try
127126
if !use_canonical_atoms && id = pos_of_string name then
128-
fprintf p "Definition _%s : ident := $\"%s\".@ "
127+
fprintf p "Definition %s : ident := $\"%s\".@ "
129128
(sanitize name) name
130129
else
131-
fprintf p "Definition _%s : ident := %a.@ "
130+
fprintf p "Definition %s : ident := %a.@ "
132131
(sanitize name) positive id
133132
with Not_an_identifier ->
134133
());
@@ -415,7 +414,7 @@ and lblstmts p = function
415414
(print_option coqZ) lbl stmt s lblstmts ls
416415

417416
let print_function p (id, f) =
418-
fprintf p "Definition f_%s := {|@ " (sanitize (extern_atom id));
417+
fprintf p "Definition f%s := {|@ " (sanitize (extern_atom id));
419418
fprintf p " fn_return := %a;@ " typ f.fn_return;
420419
fprintf p " fn_callconv := %a;@ " callconv f.fn_callconv;
421420
fprintf p " fn_params := %a;@ " (print_list (print_pair ident typ)) f.fn_params;
@@ -436,7 +435,7 @@ let init_data p = function
436435
| Init_addrof(id,ofs) -> fprintf p "Init_addrof %a %a" ident id coqptrofs ofs
437436

438437
let print_variable p (id, v) =
439-
fprintf p "Definition v_%s := {|@ " (sanitize (extern_atom id));
438+
fprintf p "Definition v%s := {|@ " (sanitize (extern_atom id));
440439
fprintf p " gvar_info := %a;@ " typ v.gvar_info;
441440
fprintf p " gvar_init := %a;@ " (print_list init_data) v.gvar_init;
442441
fprintf p " gvar_readonly := %B;@ " v.gvar_readonly;
@@ -451,12 +450,12 @@ let print_globdef p (id, gd) =
451450

452451
let print_ident_globdef p = function
453452
| (id, Gfun(Ctypes.Internal f)) ->
454-
fprintf p "(%a, Gfun(Internal f_%s))" ident id (sanitize (extern_atom id))
453+
fprintf p "(%a, Gfun(Internal f%s))" ident id (sanitize (extern_atom id))
455454
| (id, Gfun(Ctypes.External(ef, targs, tres, cc))) ->
456455
fprintf p "@[<hov 2>(%a,@ @[<hov 2>Gfun(External %a@ %a@ %a@ %a))@]@]"
457456
ident id external_function ef typlist targs typ tres callconv cc
458457
| (id, Gvar v) ->
459-
fprintf p "(%a, Gvar v_%s)" ident id (sanitize (extern_atom id))
458+
fprintf p "(%a, Gvar v%s)" ident id (sanitize (extern_atom id))
460459

461460
(* Composite definitions *)
462461

test/clightgen/bitfields.c

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
struct s {
2+
int a: 10;
3+
char : 6;
4+
_Bool b : 1;
5+
int : 0;
6+
short c: 7;
7+
};
8+
9+
int f(void)
10+
{
11+
struct s x = { -1, 1, 2 };
12+
return x.a + x.b + x.c;
13+
}

0 commit comments

Comments
 (0)