1 | include "ASM/ASMCosts.ma". |
---|
2 | include "common/Measurable.ma". |
---|
3 | |
---|
4 | (* Move where abstract_status is defined! *) |
---|
5 | definition mk_trans_system_of_abstract_status: |
---|
6 | ∀S:abstract_status. (S → IOMonad io_out io_in S) → trans_system io_out io_in |
---|
7 | ≝ |
---|
8 | λS,as_eval. |
---|
9 | mk_trans_system ?? unit (λ_.as_status S) (λ_.as_result S) |
---|
10 | (λ_.λstatus. |
---|
11 | let tr ≝ |
---|
12 | match as_label … status with |
---|
13 | [ Some cst ⇒ Echarge cst |
---|
14 | | None ⇒ E0 ] in |
---|
15 | ! status' ← as_eval … status ; |
---|
16 | return 〈 tr, status' 〉). |
---|
17 | |
---|
18 | definition mk_fullexec_of_abstract_status: |
---|
19 | ∀program: Type[0]. |
---|
20 | ∀S:abstract_status. (S → IOMonad io_out io_in S) → as_status S → fullexec io_out io_in |
---|
21 | ≝ λprogram,S,as_eval,as_init. |
---|
22 | mk_fullexec ?? program (mk_trans_system_of_abstract_status S as_eval) (λ_.it) |
---|
23 | (λ_.return as_init). |
---|
24 | |
---|
25 | definition mk_preclassified_system_of_abstract_status: |
---|
26 | ∀program: Type[0]. |
---|
27 | ∀S:abstract_status. (S → IOMonad io_out io_in S) → as_status S → preclassified_system |
---|
28 | ≝ λprogram,S,as_eval,as_init. |
---|
29 | mk_preclassified_system |
---|
30 | (mk_fullexec_of_abstract_status program S as_eval as_init) |
---|
31 | (λ_.λst. ¬(is_none … (as_label … st))) |
---|
32 | (λ_.as_classify S) |
---|
33 | (λ_.λst,prf. as_call_ident S «st,?»). |
---|
34 | @hide_prf cases (as_classify ??) in prf; normalize // * |
---|
35 | qed. |
---|
36 | |
---|
37 | (* End move *) |
---|
38 | |
---|
39 | definition OC_preclassified_system: labelled_object_code → preclassified_system ≝ |
---|
40 | λc:labelled_object_code. |
---|
41 | mk_preclassified_system_of_abstract_status |
---|
42 | labelled_object_code |
---|
43 | (OC_abstract_status c) |
---|
44 | (λst. return (execute_1 (cm c) … st)) |
---|
45 | (initialise_status … (cm c)). |
---|
46 | |
---|
47 | (* Pre-classified system for ASM *) |
---|
48 | |
---|
49 | include "ASM/Assembly.ma". |
---|
50 | |
---|
51 | (* Move next function in interpret? *) |
---|
52 | definition execute_1_pseudo_instruction': |
---|
53 | ∀cm. ∀sigma,policy.PseudoStatus cm → PseudoStatus cm ≝ |
---|
54 | λcm,sigma,policy,status. |
---|
55 | execute_1_pseudo_instruction cm |
---|
56 | (ticks_of cm sigma policy) status …. |
---|
57 | cases daemon (*CSC: we need to prove that we remain inside the program |
---|
58 | (code closed), which is impossible in case of function pointers. |
---|
59 | But the type does not allow to fail (yet?) *) |
---|
60 | qed. |
---|
61 | |
---|
62 | definition classify_pseudo_instruction: pseudo_instruction → status_class ≝ |
---|
63 | λinstr. |
---|
64 | match instr with |
---|
65 | [ Instruction pre ⇒ ASM_classify00 … pre |
---|
66 | | Comment _ ⇒ cl_other |
---|
67 | | Cost _ ⇒ cl_other |
---|
68 | | Jmp _ ⇒ cl_other |
---|
69 | | Jnz _ _ _ ⇒ cl_jump |
---|
70 | | MovSuccessor _ _ _ ⇒ cl_other |
---|
71 | | Call _ ⇒ cl_call |
---|
72 | | Mov _ _ ⇒ cl_other ]. |
---|
73 | |
---|
74 | definition ASM_classify: ∀cm. PseudoStatus cm → status_class ≝ |
---|
75 | λcm,s. |
---|
76 | classify_pseudo_instruction |
---|
77 | (\fst (fetch_pseudo_instruction (code cm) (program_counter … s) ?)). |
---|
78 | cases daemon (*CSC: again*) |
---|
79 | qed. |
---|
80 | |
---|
81 | definition ASM_next_instruction_properly_relates_program_counters ≝ |
---|
82 | λcm. |
---|
83 | λbefore: PseudoStatus cm. |
---|
84 | λafter : PseudoStatus cm. |
---|
85 | let 〈instruction, program_counter_after〉 ≝ |
---|
86 | fetch_pseudo_instruction (code cm) (program_counter … before) ? |
---|
87 | in |
---|
88 | program_counter ?? after = program_counter_after. |
---|
89 | cases daemon (*CSC: again*) |
---|
90 | qed. |
---|
91 | |
---|
92 | definition ASM_as_label_of_pc: |
---|
93 | ∀prog:pseudo_assembly_program. Word → option costlabel |
---|
94 | ≝ |
---|
95 | λprog,pc. |
---|
96 | match \fst (fetch_pseudo_instruction (code prog) pc ?) with |
---|
97 | [ Cost label ⇒ Some … label |
---|
98 | | _ ⇒ None ? ]. |
---|
99 | cases daemon (*CSC: again*) |
---|
100 | qed. |
---|
101 | |
---|
102 | definition ASM_as_result: ∀prog. PseudoStatus prog → option int ≝ |
---|
103 | λprog,st. |
---|
104 | let finaladdr ≝ address_of_word_labels (code prog) (final_label … prog) in |
---|
105 | as_result_of_finaladdr … st finaladdr. |
---|
106 | |
---|
107 | include "common/AssocList.ma". |
---|
108 | |
---|
109 | definition ASM_as_call_ident: |
---|
110 | ∀prog,sigma,policy.(Σs:PseudoStatus prog. ASM_classify … s = cl_call) → ident |
---|
111 | ≝ |
---|
112 | λprog,sigma,policy,s0. |
---|
113 | let st ≝ execute_1_pseudo_instruction' prog sigma policy s0 in |
---|
114 | let 〈lbl, instr〉 ≝ nth_safe … (nat_of_bitvector ? (program_counter … st)) … (code prog) ? in |
---|
115 | match lbl with |
---|
116 | [ None ⇒ ⊥ |
---|
117 | | Some lbl' ⇒ |
---|
118 | match assoc_list_lookup ?? lbl' (eq_identifier …) (renamed_symbols prog) with |
---|
119 | [ None ⇒ ⊥ | Some id ⇒ id ]]. |
---|
120 | cases daemon (*CSC: we need a specification of the renamed_symbols for this *) |
---|
121 | qed. |
---|
122 | |
---|
123 | definition ASM_abstract_status: |
---|
124 | ∀prog:pseudo_assembly_program.∀sigma,policy.abstract_status ≝ |
---|
125 | λprog,sigma,policy. |
---|
126 | mk_abstract_status |
---|
127 | (PseudoStatus prog) |
---|
128 | (λs1,s2. execute_1_pseudo_instruction' … sigma policy s1 = s2) |
---|
129 | word_deqset |
---|
130 | (program_counter …) |
---|
131 | (ASM_classify …) |
---|
132 | (ASM_as_label_of_pc prog …) |
---|
133 | (ASM_next_instruction_properly_relates_program_counters prog) |
---|
134 | (ASM_as_result …) |
---|
135 | (ASM_as_call_ident prog sigma policy …) |
---|
136 | ?. |
---|
137 | * #st whd in ⊢ (??%? → ?); cases (\fst (fetch_pseudo_instruction ???)) [*] |
---|
138 | normalize try (#x #y #abs) try (#x #abs) try #abs destruct (abs) |
---|
139 | qed. |
---|
140 | |
---|
141 | definition ASM_preclassified_system: |
---|
142 | pseudo_assembly_program → ∀sigma,policy. preclassified_system ≝ |
---|
143 | λc,sigma,policy. |
---|
144 | mk_preclassified_system_of_abstract_status |
---|
145 | (pseudo_assembly_program × (Word → Word) × (Word → bool)) |
---|
146 | (ASM_abstract_status c sigma policy) |
---|
147 | (λst. return (execute_1_pseudo_instruction' … sigma policy st)) |
---|
148 | (initialise_status … c). |
---|