source: src/RTLabs/import.ma @ 1047

Last change on this file since 1047 was 898, checked in by campbell, 9 years ago

Update pretty printers and examples.

File size: 6.3 KB
RevLine 
[710]1
[762]2include "RTLabs/semantics.ma".
[710]3
[738]4let rec n_idents (n:nat) (tag:String) (g:universe tag) : res (Vector (identifier tag) n × (universe tag)) ≝
[710]5match n with
[726]6[ O ⇒ OK ? 〈[[ ]], g〉
[736]7| S m ⇒ do 〈ls, g'〉 ← n_idents m tag g;
8        do 〈l, g''〉 ← fresh ? g';
[726]9        OK ? 〈l:::ls, g''〉
[710]10].
11
[750]12definition pre_register ≝ nat.
[710]13
14record pre_internal_function : Type[0] ≝
[898]15{ pf_result    : option (pre_register × typ)
16; pf_params    : list (pre_register × typ)
17; pf_locals    : list (pre_register × typ)
[710]18; pf_stacksize : nat
[750]19; pf_graph     : list (nat × ((pre_register → res register) → (nat → res label) → res statement))
[710]20; pf_entry     : nat
21; pf_exit      : nat
22}.
23
[750]24definition make_register :
25  (pre_register → option register) → pre_register → universe RegisterTag →
26  res ((nat → option register) × (universe RegisterTag) × register) ≝
27λm,reg,g.
28  match m reg with
29  [ Some r' ⇒ OK ? 〈〈m,g〉,r'〉
30  | None ⇒ do 〈r',g'〉 ← fresh ? g;
31           OK ? 〈〈λn. if eqb reg n then Some ? r' else m n,g'〉,r'〉
32  ]
33.
[710]34
35definition make_registers_list :
[898]36  (nat → option register) → list (pre_register × typ) → universe RegisterTag →
37  res ((nat → option register) × (universe RegisterTag) × (list (register×typ))) ≝
[710]38λm,lrs,g.
[898]39foldr ?? (λrst,acc. do 〈acc',l〉 ← acc;
40                   let 〈rs,ty〉 ≝ rst in
[710]41                   let 〈m,g〉 ≝ acc' in
[750]42                   do 〈mg,rs'〉 ← make_register m rs g;
[898]43                   OK ? 〈mg,〈rs',ty〉::l〉) (OK ? 〈〈m,g〉,[ ]〉) lrs.
[710]44
[797]45axiom MissingRegister : String.
46axiom MissingLabel : String.
47
[727]48definition make_internal_function : pre_internal_function → res (fundef internal_function) ≝
[710]49λpre_f.
[736]50  let rgen0 ≝ new_universe RegisterTag in
[750]51  do 〈rmapgen1, result〉 ← match pf_result pre_f with
52                          [ None ⇒ OK ? 〈〈λ_.None ?, rgen0〉, None ?〉
[898]53                          | Some rt ⇒ do 〈x,y〉 ← make_register (λ_.None ?) (\fst rt) rgen0; OK ? 〈x,Some ? 〈y,\snd rt〉〉
[750]54                          ];
[710]55  let 〈rmap1, rgen1〉 ≝ rmapgen1 in
56  do 〈rmapgen2, params〉 ← make_registers_list rmap1 (pf_params pre_f) rgen1;
57  let 〈rmap2, rgen2〉 ≝ rmapgen2 in
58  do 〈rmapgen3, locals〉 ← make_registers_list rmap2 (pf_locals pre_f) rgen2;
59  let 〈rmap3, rgen3〉 ≝ rmapgen3 in
[898]60  let rmap ≝ λn. opt_to_res … (msg MissingRegister) (rmap3 n) in
[710]61  let max_stmt ≝ foldr ?? (λp,m. max (\fst p) m) 0 (pf_graph pre_f) in
[736]62  do 〈labels, gen〉 ← n_idents (S max_stmt) ? (new_universe LabelTag);
[797]63  let get_label ≝ λn. opt_to_res … (msg MissingLabel) (get_index_weak_v ?? labels n) in
[882]64  do graph ← foldr ?? (λp:nat × ((pre_register → res register) → (nat → res label) → res statement).λg0.
65                         do g ← g0;
66                         let 〈l,s〉 ≝ p in
67                         do l' ← get_label l;
68                         do s' ← s rmap get_label;
69                         OK ? (add ?? g l' s')) (OK ? (empty_map ??)) (pf_graph pre_f);
[710]70  do entry ← get_label (pf_entry pre_f);
71  do exit ← get_label (pf_exit pre_f);
[727]72  OK ? (Internal ? (mk_internal_function
[710]73         gen
[898]74         rgen3
[710]75         result
76         params
77         locals
78         (pf_stacksize pre_f)
79         graph
80         entry
81         exit
[727]82         )).
[710]83
[750]84definition make_reg_list : (nat → res register) → list pre_register → res (list register) ≝
85λm,ps. foldr ?? (λp,rs0. do rs ← rs0; do r ← m p; OK ? (r::rs)) (OK ? [ ]) ps.
[710]86
[727]87(* XXX move somewhere sensible *)
88let rec mmap_vec (A:Type[0]) (B:Type[0]) (f:A → res B) (n:nat) (v:Vector A n) on v : res (Vector B n) ≝
89match v with
90[ VEmpty ⇒ OK ? (VEmpty …)
91| VCons m hd tl ⇒ do hd' ← f hd;
92                  do tl' ← mmap_vec A B f m tl;
93                  OK ? (hd':::tl')
94].
95
[750]96definition make_opt_reg : (pre_register → res register) → option pre_register → res (option register) ≝
97λm,o. match o with [ None ⇒ OK ? (None ?) | Some r ⇒ do r' ← m r; OK ? (Some ? r') ].
98
[710]99let rec make_St_skip l ≝ λr:nat → res register.λf:nat → res label. do l' ← f l; OK ? (St_skip l').
100let rec make_St_cost cl l ≝ λr:nat → res register.λf:nat → res label. do l' ← f l; OK ? (St_cost cl l').
[750]101let rec make_St_const rs cst l ≝ λr:nat → res register.λf:nat → res label. do rs' ← r rs; do l' ← f l; OK ? (St_const rs' cst l').
102let rec make_St_op1 op dst src l ≝ λr:nat → res register.λf:nat → res label. do dst' ← r dst; do src' ← r src; do l' ← f l; OK ? (St_op1 op dst' src' l').
103let rec make_St_op2 op dst src1 src2 l ≝ λr:nat → res register.λf:nat → res label. do dst' ← r dst; do src1' ← r src1; do src2' ← r src2; do l' ← f l; OK ? (St_op2 op dst' src1' src2' l').
[898]104let rec make_St_load chunk addr dst l ≝ λr:nat → res register.λf:nat → res label. do addr' ← r addr; do dst' ← r dst; do l' ← f l; OK ? (St_load chunk addr' dst' l').
105let rec make_St_store chunk addr src l ≝ λr:nat → res register.λf:nat → res label. do addr' ← r addr; do src' ← r src; do l' ← f l; OK ? (St_store chunk addr' src' l').
[816]106let rec make_St_call_id id args dst l ≝ λr:nat → res register.λf:nat → res label. do args' ← make_reg_list r args; do dst' ← make_opt_reg r dst; do l' ← f l; OK ? (St_call_id id args' dst' l').
107let rec make_St_call_ptr frs args dst l ≝ λr:nat → res register.λf:nat → res label. do frs' ← r frs; do args' ← make_reg_list r args; do dst' ← make_opt_reg r dst; do l' ← f l; OK ? (St_call_ptr frs' args' dst' l').
108let rec make_St_tailcall_id id args ≝ λr:nat → res register.λf:nat → res label.  do args' ← make_reg_list r args; OK statement (St_tailcall_id id args').
109let rec make_St_tailcall_ptr frs args ≝ λr:nat → res register.λf:nat → res label. do frs' ← r frs; do args' ← make_reg_list r args; OK statement (St_tailcall_ptr frs' args').
[898]110let rec make_St_cond src ltrue lfalse ≝ λr:nat → res register.λf:nat → res label. do src' ← r src; do ltrue' ← f ltrue; do lfalse' ← f lfalse;  OK ? (St_cond src' ltrue' lfalse').
[750]111let rec make_St_jumptable rs ls ≝ λr:nat → res register.λf:nat → res label. do rs' ← r rs; do ls' ← foldr ?? (λl,ls0. do ls ← ls0; do l' ← f l; OK ? (l'::ls)) (OK ? [ ]) ls; OK ? (St_jumptable rs' ls').
[765]112definition make_St_return ≝ λr:nat → res register.λf:nat → res label. OK statement St_return.
[710]113
Note: See TracBrowser for help on using the repository browser.