summaryrefslogtreecommitdiff
path: root/backend/Machtyping.v
blob: ad3ee8860322b07ff20e86f256e6bb3852fb53f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
(** Type system for the Mach intermediate language. *)

Require Import Coqlib.
Require Import Maps.
Require Import AST.
Require Import Mem.
Require Import Integers.
Require Import Values.
Require Import Mem.
Require Import Events.
Require Import Globalenvs.
Require Import Op.
Require Import Locations.
Require Conventions.
Require Import Mach.

(** * Typing rules *)

Inductive wt_instr : instruction -> Prop :=
  | wt_Mlabel:
     forall lbl,
     wt_instr (Mlabel lbl)
  | wt_Mgetstack:
     forall ofs ty r,
     mreg_type r = ty ->
     wt_instr (Mgetstack ofs ty r)
  | wt_Msetstack:
     forall ofs ty r,
     mreg_type r = ty -> 24 <= Int.signed ofs ->
     wt_instr (Msetstack r ofs ty)
  | wt_Mgetparam:
     forall ofs ty r,
     mreg_type r = ty ->
     wt_instr (Mgetparam ofs ty r)
  | wt_Mopmove:
     forall r1 r,
     mreg_type r1 = mreg_type r ->
     wt_instr (Mop Omove (r1 :: nil) r)
  | wt_Mopundef:
     forall r,
     wt_instr (Mop Oundef nil r)
  | wt_Mop:
     forall op args res,
      op <> Omove -> op <> Oundef ->
      (List.map mreg_type args, mreg_type res) = type_of_operation op ->
      wt_instr (Mop op args res)
  | wt_Mload:
      forall chunk addr args dst,
      List.map mreg_type args = type_of_addressing addr ->
      mreg_type dst = type_of_chunk chunk ->
      wt_instr (Mload chunk addr args dst)
  | wt_Mstore:
      forall chunk addr args src,
      List.map mreg_type args = type_of_addressing addr ->
      mreg_type src = type_of_chunk chunk ->
      wt_instr (Mstore chunk addr args src)
  | wt_Mcall:
      forall sig ros,
      match ros with inl r => mreg_type r = Tint | inr s => True end ->
      wt_instr (Mcall sig ros)
  | wt_Malloc:
      wt_instr Malloc
  | wt_Mgoto:
      forall lbl,
      wt_instr (Mgoto lbl)
  | wt_Mcond:
      forall cond args lbl,
      List.map mreg_type args = type_of_condition cond ->
      wt_instr (Mcond cond args lbl)
  | wt_Mreturn: 
      wt_instr Mreturn.

Record wt_function (f: function) : Prop := mk_wt_function {
  wt_function_instrs:
    forall instr, In instr f.(fn_code) -> wt_instr instr;
  wt_function_stacksize:
    f.(fn_stacksize) >= 0;
  wt_function_framesize:
    f.(fn_framesize) >= 24;
  wt_function_no_overflow:
    f.(fn_framesize) <= -Int.min_signed
}.

Inductive wt_fundef: fundef -> Prop :=
  | wt_fundef_external: forall ef,
      wt_fundef (External ef)
  | wt_function_internal: forall f,
      wt_function f ->
      wt_fundef (Internal f).

Definition wt_program (p: program) : Prop :=
  forall i f, In (i, f) (prog_funct p) -> wt_fundef f.

(** * Type soundness *)

Require Import Machabstr.

(** We show a weak type soundness result for the alternate semantics
    of Mach: for a well-typed Mach program, if a transition is taken
    from a state where registers hold values of their static types,
    registers in the final state hold values of their static types
    as well.  This is a subject reduction theorem for our type system.
    It is used in the proof of implication from the abstract Mach
    semantics to the concrete Mach semantics (file [Machabstr2mach]).
*)

Definition wt_regset (rs: regset) : Prop :=
  forall r, Val.has_type (rs r) (mreg_type r).

Definition wt_content (c: content) : Prop :=
  match c with
  | Datum32 v => Val.has_type v Tint
  | Datum64 v => Val.has_type v Tfloat
  | _ => True
  end.

Definition wt_frame (fr: frame) : Prop :=
  forall ofs, wt_content (fr.(contents) ofs).

Lemma wt_setreg:
  forall (rs: regset) (r: mreg) (v: val),
  Val.has_type v (mreg_type r) ->
  wt_regset rs -> wt_regset (rs#r <- v).
Proof.
  intros; red; intros. unfold Regmap.set. 
  case (RegEq.eq r0 r); intro.
  subst r0; assumption.
  apply H0.
Qed.

Lemma wt_get_slot:
  forall fr ty ofs v,
  get_slot fr ty ofs v ->
  wt_frame fr ->
  Val.has_type v ty. 
Proof.
  induction 1; intro. subst v. 
  set (pos := low fr + ofs).
  case ty; simpl.
  unfold getN. case (check_cont 3 (pos + 1) (contents fr)).
  generalize (H2 pos). unfold wt_content.
  destruct (contents fr pos); simpl; tauto.
  simpl; auto.
  unfold getN. case (check_cont 7 (pos + 1) (contents fr)).
  generalize (H2 pos). unfold wt_content.
  destruct (contents fr pos); simpl; tauto.
  simpl; auto.
Qed.

Lemma wt_set_slot:
  forall fr ty ofs v fr',
  set_slot fr ty ofs v fr' ->
  wt_frame fr ->
  Val.has_type v ty ->
  wt_frame fr'.
Proof.
  intros. induction H. 
  set (i := low fr + ofs).
  red; intro j; simpl. 
  assert (forall n ofs c,
            let c' := set_cont n ofs c in
            forall k, c' k = c k \/ c' k = Cont).
    induction n; simpl; intros.
    left; auto.
    unfold update. case (zeq k ofs0); intro.
    right; auto.
    apply IHn.
  destruct ty; simpl; unfold store_contents; unfold setN;
  unfold update; case (zeq j i); intro.
  red. assumption.
  elim (H 3%nat (i + 1) (contents fr) j); intro; rewrite H2.
  apply H0. red; auto. 
  red. assumption.
  elim (H 7%nat (i + 1) (contents fr) j); intro; rewrite H2.
  apply H0. red; auto. 
Qed.

Lemma wt_init_frame:
  forall f,
  wt_frame (init_frame f).
Proof.
  intros. unfold init_frame. 
  red; intros. simpl. exact I.
Qed.

Lemma incl_find_label:
  forall lbl c c', find_label lbl c = Some c' -> incl c' c.
Proof.
  induction c; simpl.
  intros; discriminate.
  case (is_label lbl a); intros.
  injection H; intro; subst c'; apply incl_tl; apply incl_refl.
  apply incl_tl; auto.
Qed.

Lemma wt_event_match:
  forall ef args t res,
  event_match ef args t res ->
  Val.has_type res (proj_sig_res ef.(ef_sig)).
Proof.
  induction 1. inversion H0; exact I.
Qed.

Section SUBJECT_REDUCTION.

Variable p: program.
Hypothesis wt_p: wt_program p.
Let ge := Genv.globalenv p.

Definition exec_instr_prop 
  (f: function) (sp: val) (parent: frame)
  (c1: code) (rs1: regset) (fr1: frame) (m1: mem) (t: trace)
  (c2: code) (rs2: regset) (fr2: frame) (m2: mem) :=
    forall (WTF: wt_function f)
           (INCL: incl c1 f.(fn_code))
           (WTRS: wt_regset rs1)
           (WTFR: wt_frame fr1)
           (WTPA: wt_frame parent),
    incl c2 f.(fn_code) /\ wt_regset rs2 /\ wt_frame fr2.
Definition exec_function_body_prop
  (f: function) (parent: frame) (link ra: val)
  (rs1: regset) (m1: mem) (t: trace) (rs2: regset) (m2: mem) :=
    forall (WTF: wt_function f)
           (WTRS: wt_regset rs1)
           (WTPA: wt_frame parent)
           (WTLINK: Val.has_type link Tint)
           (WTRA: Val.has_type ra Tint),
    wt_regset rs2.
Definition exec_function_prop
  (f: fundef) (parent: frame)
  (rs1: regset) (m1: mem) (t: trace) (rs2: regset) (m2: mem) :=
    forall (WTF: wt_fundef f)
           (WTRS: wt_regset rs1)
           (WTPA: wt_frame parent),
    wt_regset rs2.

Lemma subject_reduction:
   (forall f sp parent c1 rs1 fr1 m1 t c2 rs2 fr2 m2,
      exec_instr ge f sp parent c1 rs1 fr1 m1 t c2 rs2 fr2 m2 ->
      exec_instr_prop f sp parent c1 rs1 fr1 m1 t c2 rs2 fr2 m2)
/\ (forall f sp parent c1 rs1 fr1 m1 t c2 rs2 fr2 m2,
      exec_instrs ge f sp parent c1 rs1 fr1 m1 t c2 rs2 fr2 m2 ->
      exec_instr_prop f sp parent c1 rs1 fr1 m1 t c2 rs2 fr2 m2)
/\ (forall f parent link ra rs1 m1 t rs2 m2,
      exec_function_body ge f parent link ra rs1 m1 t rs2 m2 ->
      exec_function_body_prop f parent link ra rs1 m1 t rs2 m2)
/\ (forall f parent rs1 m1 t rs2 m2,
      exec_function ge f parent rs1 m1 t rs2 m2 ->
      exec_function_prop f parent rs1 m1 t rs2 m2).
Proof.
  apply exec_mutual_induction; red; intros; 
  try (generalize (wt_function_instrs _ WTF _ (INCL _ (in_eq _ _))); intro;
       intuition eauto with coqlib).

  apply wt_setreg; auto. 
  inversion H0. rewrite H2. apply wt_get_slot with fr (Int.signed ofs); auto.

  inversion H0. eapply wt_set_slot; eauto. 
  rewrite <- H3. apply WTRS.

  inversion H0. apply wt_setreg; auto.
  rewrite H2. apply wt_get_slot with parent (Int.signed ofs); auto.

  apply wt_setreg; auto. inversion H0.
    simpl. subst args; subst op. simpl in H. 
    rewrite <- H2. replace v with (rs r1). apply WTRS. congruence.
    subst args; subst op. simpl in H. 
    replace v with Vundef. simpl; auto. congruence.
    replace (mreg_type res) with (snd (type_of_operation op)).
    apply type_of_operation_sound with fundef ge rs##args sp; auto.
    rewrite <- H6; reflexivity.

  apply wt_setreg; auto. inversion H1. rewrite H7.
  eapply type_of_chunk_correct; eauto.

  apply H1; auto.
  destruct ros; simpl in H. 
  apply (Genv.find_funct_prop wt_fundef wt_p H).
  destruct (Genv.find_symbol ge i). 
  apply (Genv.find_funct_ptr_prop wt_fundef wt_p H).
  discriminate.

  apply wt_setreg; auto. exact I. 

  apply incl_find_label with lbl; auto. 
  apply incl_find_label with lbl; auto. 

  tauto.
  apply H0; auto.
  generalize (H0 WTF INCL WTRS WTFR WTPA). intros [A [B C]].
  apply H2; auto.

  assert (WTFR2: wt_frame fr2).
    eapply wt_set_slot; eauto. 
    eapply wt_set_slot; eauto.
    apply wt_init_frame.
  generalize (H3 WTF (incl_refl _) WTRS WTFR2 WTPA).
  tauto.

  apply (H0 Vzero Vzero). exact I. exact I. 
  inversion WTF; auto. auto. auto. 
  exact I. exact I.

  rewrite H1. apply wt_setreg; auto. 
  generalize (wt_event_match _ _ _ _ H). 
  unfold proj_sig_res, Conventions.loc_result.
  destruct (sig_res (ef_sig ef)).
  destruct t; simpl; auto.
  simpl; auto.
Qed.

Lemma subject_reduction_instr:
   forall f sp parent c1 rs1 fr1 m1 t c2 rs2 fr2 m2,
      exec_instr ge f sp parent c1 rs1 fr1 m1 t c2 rs2 fr2 m2 ->
      exec_instr_prop f sp parent c1 rs1 fr1 m1 t c2 rs2 fr2 m2.
Proof (proj1 subject_reduction).

Lemma subject_reduction_instrs:
   forall f sp parent c1 rs1 fr1 m1 t c2 rs2 fr2 m2,
      exec_instrs ge f sp parent c1 rs1 fr1 m1 t c2 rs2 fr2 m2 ->
      exec_instr_prop f sp parent c1 rs1 fr1 m1 t c2 rs2 fr2 m2.
Proof (proj1 (proj2 subject_reduction)).

Lemma subject_reduction_function:
  forall f parent rs1 m1 t rs2 m2,
      exec_function ge f parent rs1 m1 t rs2 m2 ->
      exec_function_prop f parent rs1 m1 t rs2 m2.
Proof (proj2 (proj2 (proj2 subject_reduction))).

End SUBJECT_REDUCTION.

(** * Preservation of the reserved frame slots during execution *)

(** We now show another result useful for the proof of implication
    between the two Mach semantics: well-typed functions do not
    change the values of the back link and return address fields
    of the frame slot (at offsets 0 and 12) during their execution.
    Actually, we show that the whole reserved part of the frame
    (between offsets 0 and 24) does not change. *)

Definition link_invariant (fr1 fr2: frame) : Prop :=
  forall pos v,
  0 <= pos < 20 ->
  get_slot fr1 Tint pos v -> get_slot fr2 Tint pos v.

Remark link_invariant_refl:
  forall fr, link_invariant fr fr.
Proof.
  intros; red; auto.
Qed.

Lemma set_slot_link_invariant:
  forall fr ty ofs v fr',
  set_slot fr ty ofs v fr' ->
  24 <= ofs ->
  link_invariant fr fr'.
Proof.
  induction 1. intros; red; intros.
  inversion H1. constructor. auto. exact H3. 
  simpl contents. simpl low. symmetry. rewrite H4. 
  apply load_store_contents_other. simpl. simpl in H3.
  omega.
Qed.

Lemma exec_instr_link_invariant:
  forall ge f sp parent c1 rs1 fr1 m1 t c2 rs2 fr2 m2,
  exec_instr ge f sp parent c1 rs1 fr1 m1 t c2 rs2 fr2 m2 ->
  wt_function f ->
  incl c1 f.(fn_code) ->
  incl c2 f.(fn_code) /\ link_invariant fr1 fr2.
Proof.
  induction 1; intros; 
  (split; [eauto with coqlib | try (apply link_invariant_refl)]).
  eapply set_slot_link_invariant; eauto.
  generalize (wt_function_instrs _ H0 _ (H1 _ (in_eq _ _))); intro.
  inversion H2. auto.
  eapply incl_find_label; eauto.
  eapply incl_find_label; eauto.
Qed.

Lemma exec_instrs_link_invariant:
  forall ge f sp parent c1 rs1 fr1 m1 t c2 rs2 fr2 m2,
  exec_instrs ge f sp parent c1 rs1 fr1 m1 t c2 rs2 fr2 m2 ->
  wt_function f ->
  incl c1 f.(fn_code) ->
  incl c2 f.(fn_code) /\ link_invariant fr1 fr2.
Proof.
  induction 1; intros.
  split. auto. apply link_invariant_refl.
  eapply exec_instr_link_invariant; eauto.
  generalize (IHexec_instrs1 H2 H3); intros [A B].
  generalize (IHexec_instrs2 H2 A); intros [C D].
  split. auto. red; auto.
Qed.