Skip to content

Commit b6a467b

Browse files
committed
More prudent handling of static names for string literals
- Make sure the names are fresh and not already used. - Mark the names specially so that the special printing of initializers as strings applies only to those names.
1 parent 1b71fec commit b6a467b

4 files changed

Lines changed: 33 additions & 15 deletions

File tree

‎backend/PrintAsm.ml‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,11 @@ module Printer(Target:TARGET) =
127127
symbol_offset (symb, ofs)
128128

129129

130-
let print_init_data oc name id =
131-
if Str.string_match PrintCsyntax.re_string_literal (extern_atom name) 0
132-
&& List.for_all (function Init_int8 _ -> true | _ -> false) id
133-
then
134-
fprintf oc " .ascii \"%s\"\n" (PrintCsyntax.string_of_init id)
130+
let print_init_data oc name il =
131+
if C2C.atom_literal name = C2C.String_literal then
132+
fprintf oc " .ascii \"%s\"\n" (PrintCsyntax.string_of_init il)
135133
else
136-
List.iter (print_init oc) id
134+
List.iter (print_init oc) il
137135

138136
let print_var oc name v =
139137
match v.gvar_init with

‎cfrontend/C2C.ml‎

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ type inline_status =
3232
| Noinline (* The atom is declared with the noinline attribute *)
3333
| Inline (* The atom is declared inline *)
3434

35+
type literal_status =
36+
| No_literal
37+
| String_literal
38+
| Wide_string_literal
39+
3540
type atom_info =
3641
{ a_storage: C.storage; (* storage class *)
3742
a_defined: bool; (* defined in the current comp. unit? *)
@@ -41,6 +46,7 @@ type atom_info =
4146
(* 1 section for data, 3 sections (code/lit/jumptbl) for functions *)
4247
a_access: Sections.access_mode; (* access mode, e.g. small data area *)
4348
a_inline: inline_status; (* function declared inline? *)
49+
a_literal: literal_status; (* is this a string literal? *)
4450
a_loc: location (* source location *)
4551
}
4652

@@ -138,6 +144,12 @@ let atom_location a =
138144
with Not_found ->
139145
Cutil.no_loc
140146

147+
let atom_literal a =
148+
try
149+
(Hashtbl.find decl_atom a).a_literal
150+
with Not_found ->
151+
No_literal
152+
141153
(** The current environment of composite definitions *)
142154

143155
let comp_env : composite_env ref = ref Maps.PTree.empty
@@ -594,6 +606,14 @@ let is_int64 env ty =
594606
(** String literals *)
595607

596608
let stringNum = ref 0 (* number of next global for string literals *)
609+
610+
let rec gensym_string_literal () =
611+
incr stringNum;
612+
let name = Printf.sprintf "__stringlit_%d" !stringNum in
613+
if Hashtbl.mem atom_of_string name
614+
then gensym_string_literal ()
615+
else name
616+
597617
let stringTable : (string, AST.ident) Hashtbl.t = Hashtbl.create 47
598618
let wstringTable : (int64 list * ikind, AST.ident) Hashtbl.t = Hashtbl.create 47
599619

@@ -603,8 +623,7 @@ let name_for_string_literal s =
603623
try
604624
Hashtbl.find stringTable s
605625
with Not_found ->
606-
incr stringNum;
607-
let name = Printf.sprintf "__stringlit_%d" !stringNum in
626+
let name = gensym_string_literal () in
608627
let id = intern_string name in
609628
let mergeable = if is_C_string s then 1 else 0 in
610629
Hashtbl.add decl_atom id
@@ -615,6 +634,7 @@ let name_for_string_literal s =
615634
a_sections = [Sections.for_stringlit mergeable];
616635
a_access = Sections.Access_default;
617636
a_inline = No_specifier;
637+
a_literal = String_literal;
618638
a_loc = Cutil.no_loc };
619639
Hashtbl.add stringTable s id;
620640
id
@@ -638,8 +658,7 @@ let name_for_wide_string_literal s ik =
638658
try
639659
Hashtbl.find wstringTable (s, ik)
640660
with Not_found ->
641-
incr stringNum;
642-
let name = Printf.sprintf "__stringlit_%d" !stringNum in
661+
let name = gensym_string_literal () in
643662
let id = intern_string name in
644663
let wchar_size = Cutil.sizeof_ikind ik in
645664
let mergeable = if is_C_wide_string s then wchar_size else 0 in
@@ -652,6 +671,7 @@ let name_for_wide_string_literal s ik =
652671
a_sections = [Sections.for_stringlit mergeable];
653672
a_access = Sections.Access_default;
654673
a_inline = No_specifier;
674+
a_literal = Wide_string_literal;
655675
a_loc = Cutil.no_loc };
656676
Hashtbl.add wstringTable (s, ik) id;
657677
id
@@ -1185,6 +1205,7 @@ let convertFundef loc env fd =
11851205
a_sections = Sections.for_function env loc id' fd.fd_attrib;
11861206
a_access = Sections.Access_default;
11871207
a_inline = inline;
1208+
a_literal = No_literal;
11881209
a_loc = loc };
11891210
(id', AST.Gfun(Ctypes.Internal
11901211
{fn_return = ret;
@@ -1276,6 +1297,7 @@ let convertGlobvar loc env (sto, id, ty, optinit) =
12761297
a_sections = [section];
12771298
a_access = access;
12781299
a_inline = No_specifier;
1300+
a_literal = No_literal;
12791301
a_loc = loc };
12801302
let volatile = List.mem C.AVolatile attr in
12811303
let readonly = List.mem C.AConst attr && not volatile in

‎cfrontend/PrintCsyntax.ml‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,7 @@ let string_of_init id =
451451
let add_init = function
452452
| Init_int8 n ->
453453
let c = Int32.to_int (camlint_of_coqint n) in
454+
assert (c >= 0 && c < 256);
454455
if c >= 32 && c <= 126 && c <> Char.code '\"' && c <> Char.code '\\'
455456
then Buffer.add_char b (Char.chr c)
456457
else Buffer.add_string b (Printf.sprintf "\\%03o" c)
@@ -486,8 +487,6 @@ let print_composite_init p il =
486487
il;
487488
fprintf p "}"
488489

489-
let re_string_literal = Str.regexp "__stringlit_[0-9]+"
490-
491490
let print_globvar p id v =
492491
let name1 = extern_atom id in
493492
let name2 = if v.gvar_readonly then "const " ^ name1 else name1 in
@@ -506,8 +505,7 @@ let print_globvar p id v =
506505
[i1] ->
507506
print_init p i1
508507
| _, il ->
509-
if Str.string_match re_string_literal (extern_atom id) 0
510-
&& List.for_all (function Init_int8 _ -> true | _ -> false) il
508+
if C2C.atom_literal id = C2C.String_literal
511509
then fprintf p "\"%s\"" (string_of_init (chop_last_nul il))
512510
else print_composite_init p il
513511
end;

0 commit comments

Comments
 (0)