2 * utilities for IR, such as printing
4 * ----------------------------------------------------------------
7 * Copyright (c) Geoffrey Irving, Dylan Symon
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
36 * BUG: until we fix this module.
38 (* JW XXX @ 2007-06-22 : what does the above mean??? *)
39 let print_symbol = pp_print_symbol
Format.std_formatter
42 * This printer is broken, so use these utilities.
44 let print_atom = pp_print_atom
Format.std_formatter
45 let print_set = pp_print_set
Format.std_formatter
46 let print_unop = pp_print_unop
Format.std_formatter
47 let print_binop = pp_print_binop
Format.std_formatter
48 let print_ext = pp_print_ext
Format.std_formatter
49 let print_alloc_op = pp_print_alloc_op
Format.std_formatter
50 let print_subscript a b c
= pp_print_subscript a b c
Format.std_formatter
51 let print_type = pp_print_type
Format.std_formatter
52 let print_tydef = pp_print_tydef
Format.std_formatter
54 let rec subst_type st t
=
55 let sub = subst_type st
in
57 TyFun
(tl
, t
) -> TyFun
(List.map
sub tl
, sub t
)
58 | TyUnion
(c
, tl
, i
) -> TyUnion
(c
, List.map
sub tl
, i
)
59 | TyTuple
(tclass
, tl
) -> TyTuple
(tclass
, List.map
(fun (ty
, b
) -> sub ty
, b
) tl
)
60 | TyArray t
-> TyArray
(sub t
)
61 | TyVar v
-> (try SymbolTable.find st v
with Not_found
-> t
)
62 | TyApply
(v
, tl
) -> TyApply
(v
, List.map
sub tl
)
63 | _
-> t
(* assumes no TyAggr, TyLambda, TyExists, TySubscript, TyProject *)
65 let apply_type tenv v tl
=
66 match SymbolTable.find tenv v
with
67 TyDefLambda
(tvl
, t
) ->
68 let substs = List.fold_left2
SymbolTable.add
SymbolTable.empty tvl tl
in
70 | _
-> raise
(IRException
"tried to apply a union type")
72 let type_of_alloc_op ao
=
74 AllocTuple
(_
, ty_vars
, ty
, _
)
75 | AllocUnion
(ty_vars
, ty
, _
, _
, _
) ->
78 | AllocVArray
(ty
, _
, _
, _
)
80 | AllocDTuple
(ty
, _
, _
, _
) ->
82 | AllocFrame
(v
, tyl
) ->
85 let rec polymorphic t
=
86 let pm = polymorphic in
88 TyFun
(tl
, t
) -> List.exists
pm tl
|| pm t
90 | TyApply
(_
, tl
) -> List.exists
pm tl
91 | TyTuple
(_
, tl
) -> List.exists
(fun (ty
, _
) -> pm ty
) tl
102 * Separated list of fields.
104 let print_sep_list sep printer l
=
106 ignore
(List.fold_left
(fun first x
->
109 Format.print_string sep
;
110 Format.print_space
()
112 Format.open_hvbox
tabstop;
119 * Separated list of fields.
121 let print_pre_sep_list sep printer l
=
123 ignore
(List.fold_left
(fun first x
->
126 Format.print_space
();
127 Format.print_string sep
129 Format.open_hvbox
tabstop;
138 let print_let_open v ty
=
139 Format.open_hvbox
tabstop;
140 Format.print_string
"let ";
142 Format.print_string
" :";
143 Format.print_space
();
144 pp_print_type
Format.std_formatter ty
;
145 Format.print_string
" =";
146 Format.print_space
()
148 let print_let_vars_open vars v ty
=
149 Format.open_hvbox
tabstop;
150 Format.print_string
"let ";
151 print_sep_list "," (fun v
->
152 Format.print_string
"'";
153 print_symbol v
) vars
;
155 Format.print_string
", ";
157 Format.print_string
" :";
158 Format.print_space
();
159 pp_print_type
Format.std_formatter ty
;
160 Format.print_string
" =";
161 Format.print_space
()
163 let rec print_let_close e
=
164 Format.print_string
" in";
166 Format.print_space
();
170 * Rename all the vars in the expr.
174 LetAtom
(v
, ty
, a
, e
) ->
178 | LetUnop
(v
, ty
, op
, a
, e
) ->
182 | LetBinop
(v
, ty
, op
, a1
, a2
, e
) ->
184 print_binop op a1 a2
;
186 | LetExt
(v
, ty
, s
, ty2
, args
, e
) ->
188 print_ext s ty2
[] args
;
190 | TailCall
(f
, args
) ->
192 Format.print_string
"(";
193 print_sep_list "," print_atom args
;
194 Format.print_string
")"
195 | Match
(a
, cases
) ->
196 Format.open_hvbox
tabstop;
197 Format.print_string
"match ";
199 Format.print_string
" with";
200 List.iter
(fun (set
, e
) ->
201 Format.print_space
();
202 Format.open_hvbox
tabstop;
203 Format.print_string
"| ";
205 Format.print_string
" ->";
206 Format.print_space
();
208 Format.close_box
()) cases
;
210 | LetAlloc
(v
, op
, e
) ->
211 Format.open_hvbox
tabstop;
212 Format.print_string
"let ";
214 Format.printf
" =@ ";
215 Format.open_hvbox
tabstop;
220 | LetSubscript
(so
, v
, ty
, v2
, a
, e
) ->
222 print_subscript so
(AtomVar v2
) a
;
225 | SetSubscript
(so
, v
, a1
, ty
, a2
, e
) ->
226 print_subscript so
(AtomVar v
) a1
;
227 Format.print_string
" : ";
228 pp_print_type
Format.std_formatter ty
;
229 Format.print_string
" <- ";
231 Format.print_string
";";
232 Format.print_space
();
235 | LetFuns
(defs
, e
) ->
236 Format.open_hvbox
tabstop;
237 Format.print_string
"let ";
238 print_sep_list " and" print_function defs
;
241 | LetApply
(v
, ty
, a
, al
, e
) ->
244 Format.print_string
"(";
245 print_sep_list "," print_atom al
;
246 Format.print_string
")";
248 | LetClosure
(v
, ty
, a
, al
, e
) ->
250 Format.print_string
"closure ";
252 Format.print_string
"(";
253 print_sep_list "," print_atom al
;
254 Format.print_string
")";
257 Format.open_hvbox
tabstop;
258 Format.print_string
"return ";
261 | LetExnHandler
(v
, e
) ->
262 Format.open_hvbox
tabstop;
263 Format.print_string
"try ";
265 Format.print_string
"with ";
269 Format.open_hvbox
tabstop;
270 Format.print_string
"raise ";
274 and print_gflag
= function
276 Format.print_string
"$global ";
278 Format.print_string
"$continuation "
280 Format.print_string
"$local "
282 and print_function
(f
, _
, gflag
, f_ty
, vars
, body
) =
283 Format.print_string
"(";
286 Format.print_string
" : ";
288 Format.print_string
")";
289 Format.print_string
"(";
290 print_sep_list "," print_symbol vars
;
291 Format.print_string
") =";
292 Format.print_space
();
295 (************************************************************************
297 ************************************************************************)
305 Format.open_hvbox
tabstop;
306 Format.print_string
"Program cont symbol:";
307 Format.print_space
();
311 Format.print_space
();
312 Format.open_hvbox
tabstop;
313 Format.print_string
"Program types:";
314 SymbolTable.iter
(fun v tyd
->
315 Format.print_space
();
316 Format.open_hvbox
tabstop;
318 Format.print_string
" =";
319 Format.print_space
();
321 Format.close_box
()) types
;
324 Format.print_space
();
325 Format.open_hvbox
tabstop;
326 Format.print_string
"Program body:";
327 Format.print_newline
();