source: Deliverables/Dissemination/final-review/wp5/Pics/summul_cost_scope.ml @ 3282

Last change on this file since 3282 was 3282, checked in by regisgia, 7 years ago
  • WP5 slides for the final review.
File size: 5.9 KB
Line 
1
2open Mlpost;;
3open Box;;
4open Arrow;;
5open Path;;
6open Command;;
7open Num;;
8open Dash;;
9open Color;;
10open Pen;;
11open Point;;
12open Picture;;
13
14
15let box = Box.box ~stroke:None ~dx:zero ~dy:zero
16let path = Box.path ~stroke:None ~dx:zero ~dy:zero
17let pic = Box.pic ~stroke:None ~dx:zero ~dy:zero
18
19(* Helpers *)
20
21let string_of_list sep f =
22  let rec aux = function
23    | [] -> ""
24    | [e] -> f e
25    | e :: l -> (f e) ^ sep ^ (aux l)
26  in
27  aux
28
29let mapi f =
30  let rec aux i = function
31    | [] -> []
32    | e :: l -> (f i e) :: (aux (i+1) l)
33  in
34  aux 0
35
36let cell = Box.rect ~stroke:None
37
38
39(* Text *)
40
41let color_text color text = "\\textcolor{" ^ color ^ "}{" ^ text ^ "}"
42let texttt text = "\\texttt{" ^ text ^ "}"
43
44let line name lexs =
45  Box.tex ~name (texttt (string_of_list " " (fun s -> s) lexs))
46
47let code base_name lines =
48  let f (i, b_prev, b_res) l =
49    let name = base_name ^ (string_of_int i) in
50    let b_line = line name l in
51    let b_line = Box.place `Southwest ~pos:`Northwest b_prev b_line in
52    (i+1, b_line, b_res @ [b_line])
53  in
54  let (_, _, b_res) =
55    List.fold_left f (0, Box.empty (), []) lines in
56  Box.group b_res
57
58let typ = color_text "black"
59let op = color_text "black"
60let keyword = color_text "black"
61let comment s = [color_text "black" s]
62let lab color l = color_text color l
63let label color l = [lab color (l ^ ":")]
64let cost_lab color l = lab color l
65let cost_label color lab = 
66  [ Printf.sprintf
67      "{\\textbf{emit} \\textcolor{%s}{%s}}" 
68      color
69      lab
70  ]
71(* let cost_label color l = label color l *)
72let lab l = lab "black" l
73let label l = label "black" l
74let reg i = color_text "black" ("\\%" ^ (string_of_int i))
75let hreg s = color_text "black" ("\\$" ^ s)
76let string_id s = color_text "black" ("\"" ^ s ^ "\"")
77let special = op
78
79let kif = keyword "if"
80let kreturn = keyword "return"
81let kcall f = [special "call" ; string_id f]
82
83let typ_int = typ "int"
84
85let assigns = op "$\\leftarrow$"
86let line_assigns x y = [x ; assigns ; y]
87let nop = [special "nop"]
88let newframe = [special "newframe"]
89let delframe = [special "delframe"]
90
91let preamble =
92  code "preamble"
93    [comment "\\# begin preamble" ; [".data"] ; label "globals" ;
94     ["\\ \\ .space" ; "0"] ; [".text"] ; label "main" ;
95     line_assigns (hreg "gp") (lab "globals") ;
96     [special "j" ; lab "main14"] ; comment "\\# end preamble"]
97
98let summul =
99  code "summul"
100    [(* comment "\\# begin summul" ; label "summul34" ;*)
101     cost_label "red" "\\_cost2" ;
102     line_assigns (hreg "v0") "1" ; line_assigns (hreg "a1") "1" ;
103     label "summul10" ; line_assigns (hreg "a3") "0" ;
104     [hreg "a2" ; assigns ; hreg "a0" ; "$\\ge$" ; hreg "a1"] ;
105     [hreg "a2" ; assigns ; hreg "a2" ; "==" ; hreg "zero"] ;
106     [hreg "a2" ; op "==" ; hreg "a3" ; "$\\Rightarrow$" ;
107      lab "summul6"] ; cost_label "blue" "\\_cost4" ;
108     [special "jr" ; hreg "ra"] ;
109     label "summul6" ;
110     cost_label "darkgreen" "\\_cost3" ;
111     [hreg "a2" ; assigns ; hreg "v0" ; "$\\times$" ; hreg "a0"] ;
112     [hreg "v0" ; assigns ; hreg "v0" ; "+" ; hreg "a2"] ;
113     line_assigns (hreg "a2") "1" ;
114     [hreg "a0" ; assigns ; hreg "a0" ; "$-$" ; hreg "a2"] ;
115     [special "j" ; lab "summul10"] 
116     (* comment "\\# end summul" *)
117    ]
118
119let main =
120  code "main"
121    [comment "\\# begin main" ; label "main14" ;
122     [hreg "sp" ; assigns ; hreg "sp" ; "$-$" ; "4"] ;
123     line_assigns ((hreg "sp") ^ "[0]") (hreg "ra") ;
124     cost_label "orange" "\\_cost1" ;
125     line_assigns (hreg "a0") "5" ;
126     [special "jal" ; lab "summul34"] ;
127     line_assigns (hreg "ra") ((hreg "sp") ^ "[0]") ;
128     [hreg "sp" ; assigns ; hreg "sp" ; "$+$" ; "4"] ;
129     [special "jr" ; hreg "ra"] ;
130     comment "\\# end main"]
131
132
133(* Place Text *)
134
135let code = Box.hbox ~padding:(cm 1.5) ~pos:`Top [summul]
136
137(* Strokes and arrows *)
138
139let get s = Box.get s code
140
141let xshift x p =
142  let pshft = Point.pt (x, zero) in
143  Point.add pshft p
144
145let yshift y p =
146  let pshft = Point.pt (zero, y) in
147  Point.add pshft p
148
149let scope color b1 b2 dx dpth dyu dyd =
150  let p1 = Box.north_west b1 in
151  let p4 = Box.south_west b2 in
152  let p1 = xshift dx p1 in
153  let p1 = yshift dyd p1 in
154  let p4 = xshift dx p4 in
155  let p4 = yshift dyu p4 in
156  let p2 = xshift dpth p1 in
157  let p3 = xshift dpth p4 in
158  let p = Path.pathp ~style:jLine [p1 ; p2 ; p3 ; p4] in
159  pic (Path.draw ~color p)
160
161let dpth1 = 0.1
162let dx0 = zero
163let dx1 = dpth1 +. 0.05
164let dpth1 = cm (-.dpth1)
165let dx1 = cm (-.dx1)
166let dy = 0.05
167let dy0 = zero
168let dyu1 = cm dy
169let dyd1 = cm (-.dy)
170
171let reg_scope color b1 b2 = scope color b1 b2 dx0 dpth1 dy0 dy0
172let dpth_scope color b1 b2 = scope color b1 b2 dx1 dpth1 dy0 dy0
173let up_scope color b1 b2 = scope color b1 b2 dx0 dpth1 dyu1 dy0
174let down_scope color b1 b2 = scope color b1 b2 dx0 dpth1 dy0 dyd1
175
176(* let scope1 = *)
177(*   let b1 = get "preamble0" in *)
178(*   let b2 = get "preamble7" in *)
179(*   reg_scope Color.orange b1 b2 *)
180
181(* let scope21 = *)
182(*   let b1 = get "main1" in *)
183(*   let b2 = get "main3" in *)
184(*   reg_scope Color.orange b1 b2 *)
185
186(* let scope22 = *)
187(*   let b1 = get "main5" in *)
188(*   let b2 = get "main9" in *)
189(*   reg_scope Color.orange b1 b2 *)
190
191let scope3 =
192  let b1 = get "summul1" in
193  let b2 = get "summul2" in
194  reg_scope Color.red b1 b2
195
196let scope4 =
197  let b1 = get "summul4" in
198  let b2 = get "summul7" in
199  reg_scope Color.red b1 b2
200
201let scope5 =
202  let b1 = get "summul9" in
203  let b2 = get "summul9" in
204  up_scope Color.blue b1 b2
205
206(* let scope6 =
207  let b1 = get "summul12" in
208  let b2 = get "summul12" in
209  down_scope Color.red b1 b2
210*)
211
212let scope7 =
213  let b1 = get "summul4" in
214  let b2 = get "summul7" in
215  dpth_scope Color.green b1 b2
216
217let scope8 =
218  let b1 = get "summul12" in
219  let b2 = get "summul16" in
220  reg_scope Color.green b1 b2
221
222
223(* Main *)
224
225let summul_cost_scope =
226  Box.group [code ; 
227             (* scope1 ; scope21 ; scope22 ; *)
228             scope3 ; scope4 ; scope5 ;
229             scope7 ; scope8]
230
231let summul_cost_scope = Box.draw summul_cost_scope
232
233let _ = Metapost.emit "summul_cost_scope" summul_cost_scope
Note: See TracBrowser for help on using the repository browser.