comparison lab4/optree.ml @ 0:bfdcc3820b32

Basis
author Mike Spivey <mike@cs.ox.ac.uk>
date Thu, 05 Oct 2017 08:04:15 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:bfdcc3820b32
1 (* lab4/optree.ml *)
2 (* Copyright (c) 2017 J. M. Spivey *)
3
4 open Print
5
6 (* |symbol| -- global symbols *)
7 type symbol = string
8
9 type codelab = int
10
11 let nolab = -1
12
13 (* |lab| -- last used code label *)
14 let lab = ref 0
15
16 (* |label| -- allocate a code label *)
17 let label () = incr lab; !lab
18
19 (* |fLab| -- format a code label for printf *)
20 let fLab n = fMeta "L$" [fNum n]
21
22 let nosym = "*nosym*"
23
24 let gensym () = sprintf "g$" [fNum (label ())]
25
26 (* |op| -- type of picoPascal operators *)
27 type op = Plus | Minus | Times | Div | Mod | Eq
28 | Uminus | Lt | Gt | Leq | Geq | Neq | And | Or | Not | Lsl
29 | Lsr | Asr | BitAnd | BitOr | BitNot
30
31 (* |inst| -- type of intermediate instructions *)
32 type inst =
33 CONST of int (* Constant (value) *)
34 | GLOBAL of symbol (* Constant (symbol, offset) *)
35 | LOCAL of int (* Local address (offset) *)
36 | REGVAR of int (* Register (index) *)
37 | LOADC (* Load char *)
38 | LOADW (* Load word *)
39 | STOREC (* Store char *)
40 | STOREW (* Store word *)
41 | ARG of int (* Pass argument (index) *)
42 | SLINK (* Pass static link *)
43 | PCALL of int (* Call procedure (nparams) *)
44 | RESULTW (* Procedure result *)
45 | MONOP of op (* Perform unary operation (op) *)
46 | BINOP of op (* Perform binary operation (op) *)
47 | OFFSET (* Add address and offset *)
48 | BOUND (* Array bound check *)
49 | NCHECK (* Null pointer check *)
50 | LABEL of codelab (* Set code label *)
51 | JUMP of codelab (* Unconditional branch (dest) *)
52 | JUMPC of op * codelab (* Conditional branch (cond, dest) *)
53 | JCASE of codelab list * codelab (* Jump table *)
54
55 (* Extra instructions *)
56 | LINE of int (* Line number *)
57 | NOP
58 | SEQ
59 | AFTER (* Expression with side effect *)
60 | DEFTMP of int
61 | TEMP of int (* Temporary *)
62
63 let op_name =
64 function
65 Plus -> "PLUS" | Minus -> "MINUS" | Times -> "TIMES"
66 | Div -> "DIV" | Mod -> "MOD" | Eq -> "EQ"
67 | Uminus -> "UMINUS" | Lt -> "LT" | Gt -> "GT"
68 | Leq -> "LEQ" | Geq -> "GEQ" | Neq -> "NEQ"
69 | And -> "AND" | Or -> "OR" | Not -> "NOT"
70 | Lsl -> "LSL" | Lsr -> "LSR" | Asr -> "ASR"
71 | BitAnd -> "BITAND" | BitOr -> "BITOR" | BitNot -> "BITNOT"
72
73 let fOp w = fStr (op_name w)
74
75 let fType1 =
76 function 0 -> fStr "" | 1 -> fStr "W" | s -> fMeta "*$*" [fNum s]
77
78 let fInst =
79 function
80 CONST x -> fMeta "CONST $" [fNum x]
81 | GLOBAL a -> fMeta "GLOBAL $" [fStr a]
82 | LOCAL n -> fMeta "LOCAL $" [fNum n]
83 | REGVAR i -> fMeta "REGVAR $" [fNum i]
84 | LOADC -> fStr "LOADC"
85 | LOADW -> fStr "LOADW"
86 | STOREC -> fStr "STOREC"
87 | STOREW -> fStr "STOREW"
88 | ARG n -> fMeta "ARG $" [fNum n]
89 | SLINK -> fStr "STATLINK"
90 | PCALL n -> fMeta "PCALL $" [fNum n]
91 | RESULTW -> fStr "RESULTW"
92 | MONOP w -> fStr (op_name w)
93 | BINOP w -> fStr (op_name w)
94 | OFFSET -> fStr "OFFSET"
95 | BOUND -> fStr "BOUND"
96 | NCHECK -> fStr "NCHECK"
97 | LABEL l -> fMeta "LABEL $" [fLab l]
98 | JUMP l -> fMeta "JUMP $" [fLab l]
99 | JUMPC (w, l) -> fMeta "J$ $" [fStr (op_name w); fLab l]
100 | JCASE (labs, def) -> fMeta "JCASE $ $" [fNum (List.length labs); fLab def]
101 | LINE n -> fMeta "LINE $" [fNum n]
102 | NOP -> fStr "NOP"
103 | SEQ -> fStr "SEQ"
104 | AFTER -> fStr "AFTER"
105 | DEFTMP n -> fMeta "DEFTMP $" [fNum n]
106 | TEMP n -> fMeta "TEMP $" [fNum n]
107
108 let int_of_bool b = if b then 1 else 0
109
110 (* |do_monop| -- evaluate unary operators *)
111 let do_monop w x =
112 match w with
113 Uminus -> - x
114 | Not -> if x <> 0 then 0 else 1
115 | BitNot -> lnot x
116 | _ -> failwith "do_monop"
117
118 (* |do_binop| -- evaluate binary operators *)
119 let do_binop w x y =
120 match w with
121 Plus -> x + y
122 | Minus -> x - y
123 | Times -> x * y
124 | Div -> x / y
125 | Mod -> x mod y
126 | Eq -> int_of_bool (x = y)
127 | Lt -> int_of_bool (x < y)
128 | Gt -> int_of_bool (x > y)
129 | Leq -> int_of_bool (x <= y)
130 | Geq -> int_of_bool (x >= y)
131 | Neq -> int_of_bool (x <> y)
132 | And -> if x <> 0 then y else 0
133 | Or -> if x <> 0 then 1 else y
134 | BitAnd -> x land y
135 | BitOr -> x lor y
136 | Lsl -> x lsl y
137 | Lsr -> x lsr y
138 | Asr -> x asr y
139 | _ -> failwith "do_binop"
140
141 (* |negate| -- negation of a comparison *)
142 let negate =
143 function Eq -> Neq | Neq -> Eq | Lt -> Geq
144 | Leq -> Gt | Gt -> Leq | Geq -> Lt
145 | _ -> failwith "negate"
146
147
148 (* Operator trees *)
149
150 type optree = Node of inst * optree list
151
152 let rec canon_app t us =
153 match t with
154 <SEQ, @ts> -> List.fold_right canon_app ts us
155 | <NOP> -> us
156 | <LINE n> -> if n = 0 then us else <LINE n> :: set_line n us
157 | _ -> effects t (result t :: us)
158
159 and set_line n ts =
160 match ts with
161 [] -> []
162 | <LINE m> :: us -> if n <> m then ts else us
163 | u :: us -> u :: set_line n us
164
165 and effects t us =
166 match t with
167 <AFTER, t1, t2> -> canon_app t1 (effects t2 us)
168 | <w, @ts> -> List.fold_right effects ts us
169
170 and result =
171 function
172 <AFTER, t1, t2> -> result t2
173 | <w, @ts> -> <w, @(List.map result ts)>
174
175 let canon t = canon_app t []
176
177 let flat =
178 function
179 <PCALL n, @(fn::args)> ->
180 List.rev args @ [<PCALL n, fn>]
181 | <DEFTMP k, <PCALL n, @(fn::args)>> ->
182 List.rev args @ [<DEFTMP k, <PCALL n, fn>>]
183 | t -> [t]
184
185 let flatten ts = List.concat (List.map flat ts)
186
187 let fSeq(f) xs =
188 let g prf = List.iter (fun x -> prf "$" [f x]) xs in fExt g
189
190 let rec fTree <x, @ts> =
191 let op = sprintf "$" [fInst x] in
192 fMeta "<$$>" [fStr op; fSeq(fun t -> fMeta ", $" [fTree t]) ts]
193
194 let print_optree pfx t =
195 match t with
196 <LINE n> ->
197 Print.printf "$$\n" [fStr pfx; fStr (Source.get_line n)]
198 | _ ->
199 fgrindf stdout pfx "$" [fTree t];