source: Deliverables/D2.2/8051/src/LTL/LTLToLIN.ml @ 818

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

32 and 16 bits operations support in D2.2/8051

File size: 3.7 KB
Line 
1(* This module provides a translation of LTL programs to LIN programs. *)
2
3(* Adapted from Pottier's PP compiler *)
4
5(* ------------------------------------------------------------------------- *)
6
7(* [translate_statement] translates an [LTL] statement into a [LIN]
8   statement. *)
9
10let translate_statement = function
11
12  (* Because [Branch.compress] has been called before, no [St_skip]
13     statement can be reached. *)
14
15  | LTL.St_skip lbl ->
16    LIN.St_goto lbl
17
18  (* Sequential statements. *)
19
20  | LTL.St_comment (s, _) ->
21    LIN.St_comment s
22  | LTL.St_cost (lbl, _) ->
23    LIN.St_cost lbl
24  | LTL.St_int (r, i, _) ->
25    LIN.St_int (r, i)
26  | LTL.St_addr (x, _) ->
27    LIN.St_addr x
28  | LTL.St_pop _ ->
29    LIN.St_pop
30  | LTL.St_push _ ->
31    LIN.St_push
32  | LTL.St_clear_carry _ ->
33    LIN.St_clear_carry
34  | LTL.St_set_carry _ ->
35    LIN.St_set_carry
36  | LTL.St_from_acc (r, _) ->
37    LIN.St_from_acc (r)
38  | LTL.St_to_acc (r, _) ->
39    LIN.St_to_acc r
40  | LTL.St_opaccs (opaccs, _) ->
41    LIN.St_opaccs opaccs
42  | LTL.St_op1 (op1, _) ->
43    LIN.St_op1 op1
44  | LTL.St_op2 (op2, r, _) ->
45    LIN.St_op2 (op2, r)
46  | LTL.St_load _ ->
47    LIN.St_load
48  | LTL.St_store _ ->
49    LIN.St_store
50  | LTL.St_call_id (f, _) ->
51    LIN.St_call_id f
52
53  (* Conditional branch statement. In [LIN], control implicitly
54     falls through to the second successor, so only the first
55     successor is explicitly mentioned in the statement. *)
56
57  | LTL.St_condacc (lbl_true, _) ->
58    LIN.St_condacc lbl_true
59
60  (* Statement without a successor. *)
61
62  | LTL.St_return ->
63    LIN.St_return
64
65(* ------------------------------------------------------------------------- *)
66
67(* [translate entry graph] turns an [LTL] control flow graph into
68   a [LIN] sequence of statements. *)
69
70let translate_graph entry graph =
71
72  (* Keep track of the labels that have been visited (initially none), of the
73     labels that must exist within the [LIN] code (initially only the graph's
74     entry point) and of the list of [LIN] statements that are being
75     generated (initially empty). Statements are held in the list in reverse
76     order, for efficiency. The list is reversed once generation is over. *)
77
78  let visited, required, statements =
79    ref Label.Set.empty, ref (Label.Set.singleton entry), ref []
80  in
81
82  (* Instantiate the functor. *)
83
84  let module V = LTLToLINI.Visit (struct
85    let fetch label =
86      Label.Map.find label graph
87    let translate_statement =
88      translate_statement
89    let generate statement =
90      statements := statement :: !statements
91    let require label =
92      required := Label.Set.add label !required
93    let mark label =
94      visited := Label.Set.add label !visited
95    let marked label =
96      Label.Set.mem label !visited
97  end) in
98
99  (* Traverse the control flow graph. *)
100
101  V.visit entry;
102
103  (* Now, get rid of the labels that do not need to exist. Also,
104     reverse the list to re-establish the correct order. *)
105
106  List.filter (function
107    | LIN.St_label l ->
108        Label.Set.mem l !required
109    | _ ->
110        true
111  ) (List.rev !statements)
112
113(* ------------------------------------------------------------------------- *)
114
115(* Extend the translation to procedures and programs. *)
116
117let translate_internal int_fun =
118  (* Compress the graph to eliminate gotos (St_skip) statements. *)
119  let entry, graph = Branch.compress int_fun.LTL.f_entry int_fun.LTL.f_graph in
120  translate_graph entry graph
121
122let translate_funct (name, def) =
123  let def' = match def with
124    | LTL.F_int def -> LIN.F_int (translate_internal def)
125    | LTL.F_ext def -> LIN.F_ext def in
126  (name, def')
127
128let translate (p : LTL.program) : LIN.program =
129  { LIN.vars = p.LTL.vars;
130    LIN.functs = List.map translate_funct p.LTL.functs ;
131    LIN.main = p.LTL.main }
Note: See TracBrowser for help on using the repository browser.