summaryrefslogtreecommitdiff
path: root/backend/Machabstr.v
blob: 25819f7209b39af35af4f8ddc59dd7e7232c2099 (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
(* *********************************************************************)
(*                                                                     *)
(*              The Compcert verified compiler                         *)
(*                                                                     *)
(*          Xavier Leroy, INRIA Paris-Rocquencourt                     *)
(*                                                                     *)
(*  Copyright Institut National de Recherche en Informatique et en     *)
(*  Automatique.  All rights reserved.  This file is distributed       *)
(*  under the terms of the INRIA Non-Commercial License Agreement.     *)
(*                                                                     *)
(* *********************************************************************)

(** The Mach intermediate language: abstract semantics. *)

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 Smallstep.
Require Import Op.
Require Import Locations.
Require Conventions.
Require Import Mach.
Require Stacklayout.

(** This file defines the "abstract" semantics for the Mach
  intermediate language, as opposed to the more concrete
  semantics given in module [Machconcr].

  The only difference with the concrete semantics is the interpretation
  of the stack access instructions [Mgetstack], [Msetstack] and [Mgetparam].
  In the concrete semantics, these are interpreted as memory accesses
  relative to the stack pointer.  In the abstract semantics, these 
  instructions are interpreted as accesses in a frame environment,
  not resident in memory.  The frame environment is an additional
  component of the state.

  Not having the frame data in memory facilitates the proof of
  the [Stacking] pass, which shows that the generated code executes
  correctly with the abstract semantics.
*)

(** * Structure of abstract stack frames *)

(** An abstract stack frame is a mapping from (type, offset) pairs to
    values.  Like location sets (see module [Locations]), overlap
    can occur. *)

Definition frame : Set := typ -> Z -> val.

Definition typ_eq: forall (ty1 ty2: typ), {ty1=ty2} + {ty1<>ty2}.
Proof. decide equality. Defined.

Definition update (ty: typ) (ofs: Z) (v: val) (f: frame) : frame :=
  fun ty' ofs' => 
    if zeq ofs ofs' then
      if typ_eq ty ty' then v else Vundef
    else
      if zle (ofs' + AST.typesize ty') ofs then f ty' ofs'
      else if zle (ofs + AST.typesize ty) ofs' then f ty' ofs'
      else Vundef.

Lemma update_same:
  forall ty ofs v fr,
  update ty ofs v fr ty ofs = v.
Proof.
  unfold update; intros. 
  rewrite zeq_true. rewrite dec_eq_true. auto.
Qed. 

Lemma update_other:
  forall ty ofs v fr ty' ofs',
  ofs + AST.typesize ty <= ofs' \/ ofs' + AST.typesize ty' <= ofs ->
  update ty ofs v fr ty' ofs' = fr ty' ofs'.
Proof.
  unfold update; intros.
  generalize (AST.typesize_pos ty). 
  generalize (AST.typesize_pos ty'). intros.
  rewrite zeq_false.
  destruct H. rewrite zle_false. apply zle_true. auto. omega.
  apply zle_true. auto. 
  omega.
Qed.

Definition empty_frame : frame := fun ty ofs => Vundef.

Section FRAME_ACCESSES.

Variable f: function.

(** A slot [(ty, ofs)] within a frame is valid in function [f] if it lies
  within the bounds of [f]'s frame, it does not overlap with
  the slots reserved for the return address and the back link,
  and it is aligned on a 4-byte boundary. *)

Inductive slot_valid: typ -> Z -> Prop :=
  slot_valid_intro:
    forall ty ofs,
    0 <= ofs ->
    ofs + AST.typesize ty <= f.(fn_framesize) ->
    (ofs + AST.typesize ty <= Int.signed f.(fn_link_ofs)
       \/ Int.signed f.(fn_link_ofs) + 4 <= ofs) ->
    (ofs + AST.typesize ty <= Int.signed f.(fn_retaddr_ofs)
       \/ Int.signed f.(fn_retaddr_ofs) + 4 <= ofs) ->
    (4 | ofs) ->
    slot_valid ty ofs.

(** [get_slot f fr ty ofs v] holds if the frame [fr] contains value [v]
  with type [ty] at word offset [ofs]. *)

Inductive get_slot: frame -> typ -> Z -> val -> Prop :=
  | get_slot_intro:
      forall fr ty ofs v,
      slot_valid ty ofs ->
      v = fr ty (ofs - f.(fn_framesize)) ->
      get_slot fr ty ofs v.

(** [set_slot f fr ty ofs v fr'] holds if frame [fr'] is obtained from
  frame [fr] by storing value [v] with type [ty] at word offset [ofs]. *)

Inductive set_slot: frame -> typ -> Z -> val -> frame -> Prop :=
  | set_slot_intro:
      forall fr ty ofs v fr',
      slot_valid ty ofs ->
      fr' = update ty (ofs - f.(fn_framesize)) v fr ->
      set_slot fr ty ofs v fr'.

(** Extract the values of the arguments of an external call. *)

Inductive extcall_arg: regset -> frame -> loc -> val -> Prop :=
  | extcall_arg_reg: forall rs fr r,
      extcall_arg rs fr (R r) (rs r)
  | extcall_arg_stack: forall rs fr ofs ty v,
      get_slot fr ty (Int.signed (Int.repr (Stacklayout.fe_ofs_arg + 4 * ofs))) v ->
      extcall_arg rs fr (S (Outgoing ofs ty)) v.

Inductive extcall_args: regset -> frame -> list loc -> list val -> Prop :=
  | extcall_args_nil: forall rs fr,
      extcall_args rs fr nil nil
  | extcall_args_cons: forall rs fr l1 ll v1 vl,
      extcall_arg rs fr l1 v1 -> extcall_args rs fr ll vl ->
      extcall_args rs fr (l1 :: ll) (v1 :: vl).

Definition extcall_arguments
   (rs: regset) (fr: frame) (sg: signature) (args: list val) : Prop :=
  extcall_args rs fr (Conventions.loc_arguments sg) args.
    
End FRAME_ACCESSES.

(** Mach execution states. *)

Inductive stackframe: Set :=
  | Stackframe:
      forall (f: function)      (**r calling function *)
             (sp: val)          (**r stack pointer in calling function *)
             (c: code)          (**r program point in calling function *)
             (fr: frame),       (**r frame state in calling function *)
      stackframe.

Inductive state: Set :=
  | State:
      forall (stack: list stackframe) (**r call stack *)
             (f: function)            (**r function currently executing *)
             (sp: val)                (**r stack pointer *)
             (c: code)                (**r current program point *)
             (rs: regset)             (**r register state *)
             (fr: frame)              (**r frame state *)
             (m: mem),                (**r memory state *)
      state
  | Callstate:
      forall (stack: list stackframe) (**r call stack *)
             (f: fundef)              (**r function to call *)
             (rs: regset)             (**r register state *)
             (m: mem),                (**r memory state *)
      state
  | Returnstate:
      forall (stack: list stackframe) (**r call stack *)
             (rs: regset)             (**r register state *)
             (m: mem),                (**r memory state *)
      state.

(** [parent_frame s] returns the frame of the calling function.
  It is used to access function parameters that are passed on the stack
  (the [Mgetparent] instruction).
  [parent_function s] returns the calling function itself.
  Suitable defaults are used if there are no calling function. *)

Definition parent_frame (s: list stackframe) : frame :=
  match s with
  | nil => empty_frame
  | Stackframe f sp c fr :: s => fr
  end.

Definition empty_function := 
  mkfunction (mksignature nil None) nil 0 0 Int.zero Int.zero.

Definition parent_function (s: list stackframe) : function :=
  match s with
  | nil => empty_function
  | Stackframe f sp c fr :: s => f
  end.

Section RELSEM.

Variable ge: genv.

Definition find_function (ros: mreg + ident) (rs: regset) : option fundef :=
  match ros with
  | inl r => Genv.find_funct ge (rs r)
  | inr symb =>
      match Genv.find_symbol ge symb with
      | None => None
      | Some b => Genv.find_funct_ptr ge b
      end
  end.

Inductive step: state -> trace -> state -> Prop :=
  | exec_Mlabel:
      forall s f sp lbl c rs fr m,
      step (State s f sp (Mlabel lbl :: c) rs fr m)
        E0 (State s f sp c rs fr m)
  | exec_Mgetstack:
      forall s f sp ofs ty dst c rs fr m v,
      get_slot f fr ty (Int.signed ofs) v ->
      step (State s f sp (Mgetstack ofs ty dst :: c) rs fr m)
        E0 (State s f sp c (rs#dst <- v) fr m)
  | exec_Msetstack:
     forall s f sp src ofs ty c rs fr m fr',
      set_slot f fr ty (Int.signed ofs) (rs src) fr' ->
      step (State s f sp (Msetstack src ofs ty :: c) rs fr m)
        E0 (State s f sp c rs fr' m)
  | exec_Mgetparam:
      forall s f sp ofs ty dst c rs fr m v,
      get_slot (parent_function s) (parent_frame s) ty (Int.signed ofs) v ->
      step (State s f sp (Mgetparam ofs ty dst :: c) rs fr m)
        E0 (State s f sp c (rs#dst <- v) fr m)
  | exec_Mop:
      forall s f sp op args res c rs fr m v,
      eval_operation ge sp op rs##args = Some v ->
      step (State s f sp (Mop op args res :: c) rs fr m)
        E0 (State s f sp c (rs#res <- v) fr m)
  | exec_Mload:
      forall s f sp chunk addr args dst c rs fr m a v,
      eval_addressing ge sp addr rs##args = Some a ->
      Mem.loadv chunk m a = Some v ->
      step (State s f sp (Mload chunk addr args dst :: c) rs fr m)
        E0 (State s f sp c (rs#dst <- v) fr m)
  | exec_Mstore:
      forall s f sp chunk addr args src c rs fr m m' a,
      eval_addressing ge sp addr rs##args = Some a ->
      Mem.storev chunk m a (rs src) = Some m' ->
      step (State s f sp (Mstore chunk addr args src :: c) rs fr m)
        E0 (State s f sp c rs fr m')
  | exec_Mcall:
      forall s f sp sig ros c rs fr m f',
      find_function ros rs = Some f' ->
      step (State s f sp (Mcall sig ros :: c) rs fr m)
        E0 (Callstate (Stackframe f sp c fr :: s) f' rs m)
  | exec_Mtailcall:
      forall s f stk soff sig ros c rs fr m f',
      find_function ros rs = Some f' ->
      step (State s f (Vptr stk soff) (Mtailcall sig ros :: c) rs fr m)
        E0 (Callstate s f' rs (Mem.free m stk))
  | exec_Mgoto:
      forall s f sp lbl c rs fr m c',
      find_label lbl f.(fn_code) = Some c' ->
      step (State s f sp (Mgoto lbl :: c) rs fr m)
        E0 (State s f sp c' rs fr m)
  | exec_Mcond_true:
      forall s f sp cond args lbl c rs fr m c',
      eval_condition cond rs##args = Some true ->
      find_label lbl f.(fn_code) = Some c' ->
      step (State s f sp (Mcond cond args lbl :: c) rs fr m)
        E0 (State s f sp c' rs fr m)
  | exec_Mcond_false:
      forall s f sp cond args lbl c rs fr m,
      eval_condition cond rs##args = Some false ->
      step (State s f sp (Mcond cond args lbl :: c) rs fr m)
        E0 (State s f sp c rs fr m)
  | exec_Mreturn:
      forall s f stk soff c rs fr m,
      step (State s f (Vptr stk soff) (Mreturn :: c) rs fr m)
        E0 (Returnstate s rs (Mem.free m stk))
  | exec_function_internal:
      forall s f rs m m' stk,
      Mem.alloc m 0 f.(fn_stacksize) = (m', stk) ->
      step (Callstate s (Internal f) rs m)
        E0 (State s f (Vptr stk (Int.repr (-f.(fn_framesize))))
                  f.(fn_code) rs empty_frame m')
  | exec_function_external:
      forall s ef args res rs1 rs2 m t,
      event_match ef args t res ->
      extcall_arguments (parent_function s) rs1 (parent_frame s) ef.(ef_sig) args ->
      rs2 = (rs1#(Conventions.loc_result ef.(ef_sig)) <- res) ->
      step (Callstate s (External ef) rs1 m)
         t (Returnstate s rs2 m)
  | exec_return:
      forall f sp c fr s rs m,
      step (Returnstate (Stackframe f sp c fr :: s) rs m)
        E0 (State s f sp c rs fr m).

End RELSEM.

Inductive initial_state (p: program): state -> Prop :=
  | initial_state_intro: forall b f,
      let ge := Genv.globalenv p in
      let m0 := Genv.init_mem p in
      Genv.find_symbol ge p.(prog_main) = Some b ->
      Genv.find_funct_ptr ge b = Some f ->
      initial_state p (Callstate nil f (Regmap.init Vundef) m0).

Inductive final_state: state -> int -> Prop :=
  | final_state_intro: forall rs m r,
      rs (Conventions.loc_result (mksignature nil (Some Tint))) = Vint r ->
      final_state (Returnstate nil rs m) r.

Definition exec_program (p: program) (beh: program_behavior) : Prop :=
  program_behaves step (initial_state p) final_state (Genv.globalenv p) beh.