source: Deliverables/D2.2/8051/src/LTL/LTLInterpret.ml @ 740

Last change on this file since 740 was 740, checked in by ayache, 9 years ago

New memory model and bug fixes in 8051 branch. Added primitive operations in interpreters from Clight to LIN.

File size: 11.9 KB
Line 
1
2(** This module provides an interpreter for the LTL language. *)
3
4
5let error_prefix = "LTL interpret"
6let error s = Error.global_error error_prefix s
7
8
9module Mem = Driver.LTLMemory
10module Val = Mem.Value
11let chunk = Driver.LTLMemory.int_size
12module Eval = I8051.Eval (Val)
13
14
15(* Memory *)
16
17type memory = LTL.function_def Mem.memory
18
19(* Hardware registers environments. They associate a value to the each hardware
20   register. *)
21
22type hdw_reg_env = Val.t I8051.RegisterMap.t
23
24(* Execution states. *)
25
26type state =
27    { pc     : Val.address ;
28      isp    : Val.address ;
29      exit   : Val.address ;
30      carry  : Val.t ;
31      renv   : hdw_reg_env ;
32      mem    : memory ;
33      trace  : CostLabel.t list }
34
35
36(* Helpers *)
37
38let change_pc st pc = { st with pc = pc }
39let change_isp st isp = { st with isp = isp }
40let change_exit st exit = { st with exit = exit }
41let change_carry st carry = { st with carry = carry }
42let change_renv st renv = { st with renv = renv }
43let change_mem st mem = { st with mem = mem }
44let change_trace st trace = { st with trace = trace }
45let add_trace st cost_lbl = change_trace st (cost_lbl :: st.trace)
46
47let empty_state =
48  { pc     = Val.null ;
49    isp    = Val.null ;
50    exit   = Val.null ;
51    carry  = Val.undef ;
52    renv   = I8051.RegisterMap.empty ;
53    mem    = Mem.empty ;
54    trace  = [] }
55
56
57(* Each label of each function is associated a pointer. The base of this pointer
58   is the base of the function in memory. Inside a function, offsets are
59   bijectively associated to labels. *)
60
61module Labels_Offsets = Bijection.Make (Label) (Val.Offset)
62
63let labels_offsets_internal int_fun =
64  let f lbl _ (lbls_offs, i) =
65    (Labels_Offsets.add1 lbl i lbls_offs, Val.Offset.succ i) in
66  Label.Map.fold f int_fun.LTL.f_graph
67
68(* [labels_offsets p] builds a bijection between the labels found in the
69   functions of [p] and some offsets. *)
70
71let labels_offsets p =
72  let f (lbls_offs, i) (_, def) = match def with
73    | LTL.F_int int_fun -> labels_offsets_internal int_fun (lbls_offs, i)
74    | _ -> (lbls_offs, i) in
75  fst (List.fold_left f (Labels_Offsets.empty, Val.Offset.zero) p.LTL.functs)
76
77let fun_def_of_ptr mem ptr = match Mem.find_fun_def mem ptr with
78  | LTL.F_int def -> def
79  | _ -> error "Trying to fetch the definition of an external function."
80
81let fetch_stmt lbls_offs st =
82  let msg =
83    Printf.sprintf "%s does not point to a statement."
84      (Val.string_of_address st.pc) in
85  if Val.is_mem_address st.pc then
86    let off = Val.offset_of_address st.pc in
87    let def = fun_def_of_ptr st.mem st.pc in
88    let lbl = Labels_Offsets.find2 off lbls_offs in
89    Label.Map.find lbl def.LTL.f_graph
90  else error msg
91
92let entry_pc lbls_offs ptr def =
93  Val.change_address_offset ptr (Labels_Offsets.find1 def.LTL.f_entry lbls_offs)
94
95let init_fun_call lbls_offs st ptr def =
96  let pc = entry_pc lbls_offs ptr def in
97  change_pc st pc
98
99let next_pc lbls_offs st lbl =
100  let off = Labels_Offsets.find1 lbl lbls_offs in
101  change_pc st (Val.change_address_offset st.pc off)
102
103let framesize st =
104  if Val.is_mem_address st.pc then
105    let def = fun_def_of_ptr st.mem st.pc in
106    def.LTL.f_stacksize
107  else error "Trying to load the stack size of an external function."
108
109let add_reg r v st =
110  let renv = I8051.RegisterMap.add r v st.renv in
111  change_renv st renv
112
113let get_reg r st =
114  if I8051.RegisterMap.mem r st.renv then I8051.RegisterMap.find r st.renv
115  else error ("Unknown hardware register " ^ (I8051.print_register r) ^ ".")
116
117let push st v =
118  let mem = Mem.store st.mem chunk st.isp v in
119  let isp = Val.add_address st.isp (Val.Offset.of_int chunk) in
120  change_mem (change_isp st isp) mem
121
122let pop st =
123  let isp = Val.add_address st.isp (Val.Offset.of_int (-chunk)) in
124  let st = change_isp st isp in
125  let v = Mem.load st.mem chunk st.isp in
126  (st, v)
127
128let save_ra lbls_offs st lbl =
129  let ra =
130    Val.change_address_offset st.pc (Labels_Offsets.find1 lbl lbls_offs) in
131  let st = push st (List.nth ra 0) in
132  let st = push st (List.nth ra 1) in
133  st
134
135let label_of_pointer lbls_offs ptr =
136(*
137  Printf.printf "Retrieving label of %s\n%!" (Val.to_string ptr) ;
138*)
139  let off = Val.offset_of_address ptr in
140  Labels_Offsets.find2 off lbls_offs
141
142let pointer_of_label lbls_offs ptr lbl =
143  Val.change_address_offset ptr (Labels_Offsets.find1 lbl lbls_offs)
144
145let return_pc st =
146  let (st, pch) = pop st in
147  let (st, pcl) = pop st in
148  (st, [pcl ; pch])
149
150let dptr st = List.map (fun r -> get_reg r st) [I8051.dpl ; I8051.dph]
151
152
153(* State pretty-printing *)
154
155let current_label lbls_offs st =
156  Labels_Offsets.find2 (Val.offset_of_address st.pc) lbls_offs
157
158let print_renv renv =
159  let f r v =
160    if not (Val.eq v Val.undef) then
161    Printf.printf "\n%s = %s%!" (I8051.print_register r) (Val.to_string v) in
162  I8051.RegisterMap.iter f renv
163
164let print_state lbls_offs st =
165  Printf.printf "PC: %s (%s)\n%!"
166    (Val.string_of_address st.pc) (current_label lbls_offs st) ;
167  Printf.printf "SP: %s\n%!"
168    (Val.string_of_address [get_reg I8051.spl st ; get_reg I8051.sph st]) ;
169  Printf.printf "ISP: %s%!" (Val.string_of_address st.isp) ;
170  print_renv st.renv ;
171  Printf.printf "\nC = %s%!" (Val.to_string st.carry) ;
172  Mem.print st.mem ;
173  Printf.printf "\n%!"
174
175
176module InterpretExternal = Primitive.Interpret (Mem)
177
178let interpret_external mem f args = match InterpretExternal.t mem f args with
179  | (mem', InterpretExternal.V v) -> (mem', [v])
180  | (mem', InterpretExternal.A addr) -> (mem', addr)
181
182let fetch_external_args st =
183  (* TODO: this is bad when parameters are the empty list. *)
184  [get_reg (List.hd I8051.parameters) st]
185
186let set_result st = function
187  | [] -> assert false (* should be impossible: at least one value returned *)
188  | [v] -> add_reg I8051.dpl v st
189  | v1 :: v2 :: _ ->
190    let st = add_reg I8051.dpl v1 st in
191    add_reg I8051.dph v2 st
192
193let interpret_external_call st f next_pc =
194  let args = fetch_external_args st in
195  let (mem, vs) = interpret_external st.mem f args in
196  let st = change_mem st mem in
197  let st = set_result st vs in
198  change_pc st next_pc
199
200let interpret_call lbls_offs st f ra =
201  let ptr = Mem.find_global st.mem f in
202  match Mem.find_fun_def st.mem ptr with
203    | LTL.F_int def ->
204      let st = save_ra lbls_offs st ra in
205      init_fun_call lbls_offs st ptr def
206    | LTL.F_ext def ->
207      let next_pc = 
208        Val.change_address_offset st.pc (Labels_Offsets.find1 ra lbls_offs) in
209      interpret_external_call st def.AST.ef_tag next_pc
210
211let interpret_return lbls_offs st =
212  let (st, pc) = return_pc st in
213  change_pc st pc
214
215
216(* Interpret statements. *)
217
218let interpret_stmt lbls_offs st stmt =
219  let next_pc = next_pc lbls_offs in
220  match stmt with
221
222    | LTL.St_skip lbl ->
223      next_pc st lbl
224
225    | LTL.St_comment (s, lbl) ->
226(*
227      Printf.printf "*** %s ***\n\n%!" s ;
228*)
229      next_pc st lbl
230
231    | LTL.St_cost (cost_lbl, lbl) ->
232      let st = add_trace st cost_lbl in
233      next_pc st lbl
234
235    | LTL.St_int (r, i, lbl) ->
236      let st = add_reg r (Val.of_int i) st in
237      next_pc st lbl
238
239    | LTL.St_pop lbl ->
240      let (st, v) = pop st in
241      let st = add_reg I8051.a v st in
242      next_pc st lbl
243
244    | LTL.St_push lbl ->
245      let v = get_reg I8051.a st in
246      let st = push st v in
247      next_pc st lbl
248
249    | LTL.St_addr (x, lbl) ->
250      let vs = Mem.find_global st.mem x in
251      let st = add_reg I8051.dpl (List.nth vs 0) st in
252      let st = add_reg I8051.dph (List.nth vs 1) st in
253      next_pc st lbl
254
255    | LTL.St_from_acc (destr, lbl) ->
256      let st = add_reg destr (get_reg (I8051.a) st) st in
257      next_pc st lbl
258
259    | LTL.St_to_acc (srcr, lbl) ->
260      let st = add_reg I8051.a (get_reg srcr st) st in
261      next_pc st lbl
262
263    | LTL.St_opaccs (opaccs, lbl) ->
264      let v =
265        Eval.opaccs opaccs
266          (get_reg I8051.a st)
267          (get_reg I8051.b st) in
268      let st = add_reg I8051.a v st in
269      next_pc st lbl
270
271    | LTL.St_op1 (op1, lbl) ->
272      let v = Eval.op1 op1 (get_reg I8051.a st) in
273      let st = add_reg I8051.a v st in
274      next_pc st lbl
275
276    | LTL.St_op2 (op2, srcr, lbl) ->
277      let (v, carry) =
278        Eval.op2 st.carry op2
279          (get_reg I8051.a st)
280          (get_reg srcr st) in
281      let st = change_carry st carry in
282      let st = add_reg I8051.a v st in
283      next_pc st lbl
284
285    | LTL.St_clear_carry lbl ->
286      let st = change_carry st Val.zero in
287      next_pc st lbl
288
289    | LTL.St_load lbl ->
290      let addr = dptr st in
291      let v = Mem.load st.mem chunk addr in
292      let st = add_reg I8051.a v st in
293      next_pc st lbl
294
295    | LTL.St_store lbl ->
296      let addr = dptr st in
297      let mem = Mem.store st.mem chunk addr (get_reg I8051.a st) in
298      let st = change_mem st mem in
299      next_pc st lbl
300
301    | LTL.St_call_id (f, lbl) ->
302      interpret_call lbls_offs st f lbl
303
304    | LTL.St_condacc (lbl_true, lbl_false) ->
305      let v = get_reg I8051.a st in
306      let lbl =
307        if Val.is_true v then lbl_true
308        else
309          if Val.is_false v then lbl_false
310          else error "Undecidable branchment." in
311      next_pc st lbl
312
313    | LTL.St_return ->
314      interpret_return lbls_offs st
315
316
317let compute_result st =
318  let v = get_reg I8051.dpl st in
319  if Val.is_int v then IntValue.Int8.cast (Val.to_int_repr v)
320  else IntValue.Int8.zero
321
322let rec iter_small_step debug lbls_offs st =
323  if debug then print_state lbls_offs st ;
324  match fetch_stmt lbls_offs st with
325    | LTL.St_return when Val.eq_address (snd (return_pc st)) st.exit ->
326      (compute_result st, List.rev st.trace)
327    | stmt ->
328      let st' = interpret_stmt lbls_offs st stmt in
329      iter_small_step debug lbls_offs st'
330
331
332let add_global_vars =
333  List.fold_left
334    (fun mem (id, size) -> Mem.add_var mem id [AST.Data_reserve size])
335
336let add_fun_defs =
337  List.fold_left (fun mem (f_id, f_def) -> Mem.add_fun_def mem f_id f_def)
338
339let init_prog (st : state) (p : LTL.program) : state =
340  let mem = add_global_vars (add_fun_defs st.mem p.LTL.functs) p.LTL.vars in
341  change_mem st mem
342
343let init_sp st =
344  let (mem, sp) = Mem.alloc st.mem I8051.ext_ram_size in
345  let sp =
346    Val.change_address_offset sp (Val.Offset.of_int I8051.ext_ram_size) in
347  let st = change_mem st mem in
348  (st, sp)
349
350let init_isp st =
351  let (mem, isp) = Mem.alloc st.mem I8051.int_ram_size in
352  let st = change_mem (change_isp st isp) mem in
353  let (mem, exit) = Mem.alloc st.mem 1 in
354  let st = change_exit st exit in
355  let st = push st (List.nth exit 0) in
356  let st = push st (List.nth exit 1) in
357  st 
358
359let init_renv st sp =
360  let f r st = add_reg r Val.undef st in
361  let st = I8051.RegisterSet.fold f I8051.registers st in
362  let spl = List.nth sp 0 in
363  let sph = List.nth sp 1 in
364  let st = add_reg I8051.spl spl st in
365  let st = add_reg I8051.sph sph st in
366  st
367
368let init_main_call lbls_offs st main =
369  let ptr = Mem.find_global st.mem main in
370  match Mem.find_fun_def st.mem ptr with
371    | LTL.F_int def ->
372      init_fun_call lbls_offs st ptr def
373    | _ -> error ("Cannot execute the main (\"" ^ main ^ "\"): it is external.")
374
375
376(* Before interpreting, the environment is initialized:
377   - Build a bijection between the labels in the program and some values (taken
378     amongst the offsets).
379   - Add function definitions to the memory and reserve space for the globals.
380   - Allocate memory to emulate the external stack and initialize the external
381     stack pointer.
382   - Allocate memory to emulate the internal stack and initialize the internal
383     stack pointer.
384   - Initialiaze the physical register environment. All are set to 0, except for
385     the stack pointer registers that take the high and low bits of the external
386     stack pointer.
387   - Initialize a call to the main (set the current program counter to the
388     beginning of the function).
389   - Initialize the carry flag to 0. *)
390
391let interpret debug p =
392  if debug then Printf.printf "*** LTL ***\n\n%!" ;
393  match p.LTL.main with
394    | None -> (IntValue.Int8.zero, [])
395    | Some main ->
396      let lbls_offs = labels_offsets p in
397      let st = empty_state in
398      let st = init_prog st p in
399      let (st, sp) = init_sp st in
400      let st = init_isp st in
401      let st = init_renv st sp in
402      let st = init_main_call lbls_offs st main in
403      let st = change_carry st Val.zero in
404      iter_small_step debug lbls_offs st
Note: See TracBrowser for help on using the repository browser.