Skip to content

Commit c75619d

Browse files
Drop uninterned symbols
Instead, we use regular symbols, but make sure that they are not legal C identifiers, so that they cannot conflict with those parsed from the source program. Specifically, the temporaries introduced by the SimplExpr pass are of the form "$1", "$2", etc. (which is similar in effect to the old behavior when they are printed out), and temporaries introduced to separate memory loads in clightgen normalization are of the form "@1", "@2", etc.
1 parent 1e344dc commit c75619d

File tree

5 files changed

+52
-31
lines changed

5 files changed

+52
-31
lines changed

cfrontend/SimplExpr.v

+8-6
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,14 @@ Local Open Scope string_scope.
3030
(** State and error monad for generating fresh identifiers. *)
3131

3232
Record generator : Type := mkgenerator {
33-
gen_next: ident;
33+
gen_next: positive;
3434
gen_trail: list (ident * type)
3535
}.
3636

37+
(** The ML code prints out the nth reserved identifier as "$n". *)
38+
Parameter string_of_resid : positive -> String.string.
39+
Axiom string_of_resid_inj : forall x y, string_of_resid x = string_of_resid y -> x = y.
40+
3741
Inductive result (A: Type) (g: generator) : Type :=
3842
| Err: Errors.errmsg -> result A g
3943
| Res: A -> forall (g': generator), Ple (gen_next g) (gen_next g') -> result A g.
@@ -72,15 +76,13 @@ Notation "'do' ( X , Y ) <- A ; B" := (bind2 A (fun X Y => B))
7276

7377
Local Open Scope gensym_monad_scope.
7478

75-
Parameter first_unused_ident: unit -> ident.
76-
7779
Definition initial_generator (x: unit) : generator :=
78-
mkgenerator (first_unused_ident x) nil.
80+
mkgenerator 1%positive nil.
7981

8082
Definition gensym (ty: type): mon ident :=
8183
fun (g: generator) =>
82-
Res (gen_next g)
83-
(mkgenerator (Pos.succ (gen_next g)) ((gen_next g, ty) :: gen_trail g))
84+
Res #(string_of_resid (gen_next g))
85+
(mkgenerator (Pos.succ (gen_next g)) ((#(string_of_resid (gen_next g)), ty) :: gen_trail g))
8486
(Ple_succ (gen_next g)).
8587

8688
(** Construct a sequence from a list of statements. To facilitate the

cfrontend/SimplExprspec.v

+36-14
Original file line numberDiff line numberDiff line change
@@ -555,13 +555,17 @@ Ltac monadInv H :=
555555
(** ** Freshness and separation properties. *)
556556

557557
Definition within (id: ident) (g1 g2: generator) : Prop :=
558-
Ple (gen_next g1) id /\ Plt id (gen_next g2).
558+
exists p,
559+
id = #(string_of_resid p) /\
560+
Ple (gen_next g1) p /\ Plt p (gen_next g2).
559561

560562
Lemma gensym_within:
561563
forall ty g1 id g2 I,
562564
gensym ty g1 = Res id g2 I -> within id g1 g2.
563565
Proof.
564-
intros. monadInv H. split. apply Ple_refl. apply Plt_succ.
566+
intros. monadInv H.
567+
exists (gen_next g1); intuition idtac.
568+
apply Ple_refl. apply Plt_succ.
565569
Qed.
566570

567571
Lemma within_widen:
@@ -571,7 +575,8 @@ Lemma within_widen:
571575
Ple (gen_next g2) (gen_next g2') ->
572576
within id g1' g2'.
573577
Proof.
574-
intros. destruct H. split.
578+
intros. destruct H as (? & ? & ?).
579+
exists x; intuition idtac.
575580
eapply Ple_trans; eauto.
576581
eapply Plt_Ple_trans; eauto.
577582
Qed.
@@ -609,26 +614,40 @@ Proof.
609614
intros; red; intros. destruct (in_app_or _ _ _ H1); auto.
610615
Qed.
611616

617+
Lemma ident_of_string_inj s t:
618+
ident_of_string s = ident_of_string t -> s = t.
619+
Proof.
620+
intros Hst.
621+
rewrite <- (string_of_ident_of_string s).
622+
rewrite <- (string_of_ident_of_string t).
623+
congruence.
624+
Qed.
625+
612626
Lemma contained_disjoint:
613627
forall g1 l1 g2 l2 g3,
614628
contained l1 g1 g2 -> contained l2 g2 g3 -> list_disjoint l1 l2.
615629
Proof.
616630
intros; red; intros. red; intro; subst y.
617-
exploit H; eauto. intros [A B]. exploit H0; eauto. intros [Csyntax D].
618-
elim (Plt_strict x). apply Plt_Ple_trans with (gen_next g2); auto.
631+
exploit H; eauto. intros (p & Hp & A & B).
632+
exploit H0; eauto. intros (q & Hq & Csyntax & D).
633+
assert (p = q) by (apply string_of_resid_inj, ident_of_string_inj; congruence).
634+
subst q. elim (Plt_strict p). apply Plt_Ple_trans with (gen_next g2); auto.
619635
Qed.
620636

621637
Lemma contained_notin:
622638
forall g1 l g2 id g3,
623639
contained l g1 g2 -> within id g2 g3 -> ~In id l.
624640
Proof.
625-
intros; red; intros. exploit H; eauto. intros [Csyntax D]. destruct H0 as [A B].
626-
elim (Plt_strict id). apply Plt_Ple_trans with (gen_next g2); auto.
641+
intros; red; intros.
642+
exploit H; eauto. intros (p & Hp & Csyntax & D).
643+
destruct H0 as (q & Hq & A & B).
644+
assert (p = q) by (apply string_of_resid_inj, ident_of_string_inj; congruence).
645+
subst q. elim (Plt_strict p). apply Plt_Ple_trans with (gen_next g2); auto.
627646
Qed.
628647

629648
Definition dest_below (dst: destination) (g: generator) : Prop :=
630649
match dst with
631-
| For_set sd => Plt (sd_temp sd) g.(gen_next)
650+
| For_set sd => exists p, sd_temp sd = #(string_of_resid p) /\ Plt p g.(gen_next)
632651
| _ => True
633652
end.
634653

@@ -642,7 +661,7 @@ Lemma dest_for_set_seqbool_val:
642661
forall tmp ty g1 g2,
643662
within tmp g1 g2 -> dest_below (For_set (sd_seqbool_val tmp ty)) g2.
644663
Proof.
645-
intros. destruct H. simpl. auto.
664+
intros. destruct H as (p & Hp & A & B). simpl. eauto.
646665
Qed.
647666

648667
Lemma dest_for_set_seqbool_set:
@@ -654,27 +673,30 @@ Qed.
654673
Lemma dest_for_set_condition_val:
655674
forall tmp tycast ty g1 g2, within tmp g1 g2 -> dest_below (For_set (SDbase tycast ty tmp)) g2.
656675
Proof.
657-
intros. destruct H. simpl. auto.
676+
intros. destruct H as (p & Hp & A & B). simpl. eauto.
658677
Qed.
659678

660679
Lemma dest_for_set_condition_set:
661680
forall sd tmp tycast ty g1 g2, dest_below (For_set sd) g2 -> within tmp g1 g2 -> dest_below (For_set (SDcons tycast ty tmp sd)) g2.
662681
Proof.
663-
intros. destruct H0. simpl. auto.
682+
intros. destruct H0 as (p & Hp & A & B). simpl. eauto.
664683
Qed.
665684

666685
Lemma sd_temp_notin:
667686
forall sd g1 g2 l, dest_below (For_set sd) g1 -> contained l g1 g2 -> ~In (sd_temp sd) l.
668687
Proof.
669-
intros. simpl in H. red; intros. exploit H0; eauto. intros [A B].
670-
elim (Plt_strict (sd_temp sd)). apply Plt_Ple_trans with (gen_next g1); auto.
688+
intros. destruct H as (p & Hp & H). red; intros.
689+
exploit H0; eauto. intros (q & Hq & A & B).
690+
assert (p = q) by (apply string_of_resid_inj, ident_of_string_inj; congruence).
691+
subst q. elim (Plt_strict p). apply Plt_Ple_trans with (gen_next g1); auto.
671692
Qed.
672693

673694
Lemma dest_below_le:
674695
forall dst g1 g2,
675696
dest_below dst g1 -> Ple g1.(gen_next) g2.(gen_next) -> dest_below dst g2.
676697
Proof.
677-
intros. destruct dst; simpl in *; auto. eapply Plt_Ple_trans; eauto.
698+
intros. destruct dst; simpl in *; auto.
699+
destruct H as (p & Hp & H). eauto using Plt_Ple_trans.
678700
Qed.
679701

680702
Hint Resolve gensym_within within_widen contained_widen

exportclight/Clightnorm.ml

+4-7
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ open Camlcoq
3333
open Ctypes
3434
open Clight
3535

36-
let gen_next : AST.ident ref = ref P.one
36+
let gen_next : int ref = ref 1
3737
let gen_trail : (AST.ident * coq_type) list ref = ref []
3838

3939
let gensym ty =
40-
let id = !gen_next in
41-
gen_next := P.succ id;
40+
let id = intern_string (Printf.sprintf "@%d" !gen_next) in
41+
gen_next := !gen_next + 1;
4242
gen_trail := (id, ty) :: !gen_trail;
4343
id
4444

@@ -148,10 +148,7 @@ let next_var curr (v, _) = if P.lt v curr then curr else P.succ v
148148
let next_var_list vars start = List.fold_left next_var start vars
149149

150150
let norm_function f =
151-
gen_next := next_var_list f.fn_params
152-
(next_var_list f.fn_vars
153-
(next_var_list f.fn_temps
154-
(Camlcoq.first_unused_ident ())));
151+
gen_next := 1;
155152
gen_trail := [];
156153
let s' = norm_stmt f.fn_body in
157154
let new_temps = !gen_trail in

extraction/extraction.v

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ Extract Constant Allocation.regalloc => "Regalloc.regalloc".
9494
Extract Constant Linearize.enumerate_aux => "Linearizeaux.enumerate_aux".
9595

9696
(* SimplExpr *)
97-
Extract Constant SimplExpr.first_unused_ident => "Camlcoq.first_unused_ident".
97+
Extract Constant SimplExpr.string_of_resid => "Camlcoq.coqstring_of_resid".
9898
Extraction Inline SimplExpr.ret SimplExpr.error SimplExpr.bind SimplExpr.bind2.
9999

100100
(* Compopts *)

lib/Camlcoq.ml

+3-3
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ let next_temp = ref 1
299299

300300
let rec fresh_temp () =
301301
assert (!next_temp != 0);
302-
let s = Printf.sprintf "$%d" !next_temp in
302+
let s = Printf.sprintf "!%d" !next_temp in
303303
next_temp := !next_temp + 1;
304304
if Hashtbl.mem atom_of_string s then fresh_temp () else s
305305

@@ -322,8 +322,6 @@ let extern_atom a =
322322
Hashtbl.add string_of_atom a s;
323323
s
324324

325-
let first_unused_ident () = !next_atom
326-
327325
(* Strings *)
328326

329327
let camlstring_of_coqstring (s: char list) =
@@ -352,6 +350,8 @@ let ident_of_coqstring s =
352350
intern_string (camlstring_of_coqstring s)
353351
let coqstring_of_ident a =
354352
coqstring_of_camlstring (extern_atom a)
353+
let coqstring_of_resid p =
354+
coqstring_of_camlstring (Printf.sprintf "$%d" (P.to_int p))
355355

356356
(* Floats *)
357357

0 commit comments

Comments
 (0)