SCM Repository
Annotation of /sml/trunk/src/compiler/MiscUtil/profile/btrace.sml
Parent Directory
|
Revision Log
Revision 677 - (view) (download)
1 : | blume | 675 | (* |
2 : | * Perform Absyn annotations for back-tracing support. | ||
3 : | * This adds a bt_add at the entry point of each FNexp, | ||
4 : | * a push-restore sequence (bt_push) at each non-tail call site of | ||
5 : | * a non-primitive function, and a save-restore sequence to each HANDLEexp. | ||
6 : | * | ||
7 : | * Copyright (c) 2000 by Lucent Bell Laboratories | ||
8 : | * | ||
9 : | * author: Matthias Blume (blume@kurims.kyoto-u.ac.jp) | ||
10 : | *) | ||
11 : | local | ||
12 : | structure A = Absyn | ||
13 : | structure SE = StaticEnv | ||
14 : | structure CB = CompBasic | ||
15 : | structure SP = SymPath | ||
16 : | structure EM = ErrorMsg | ||
17 : | structure VC = VarCon | ||
18 : | structure BT = BasicTypes | ||
19 : | structure II = InlInfo | ||
20 : | structure EU = ElabUtil | ||
21 : | |||
22 : | structure Dummy = BTImp (* mention it, so it gets made! *) | ||
23 : | in | ||
24 : | |||
25 : | signature BTRACE = sig | ||
26 : | val instrument : SE.staticEnv * CB.compInfo -> A.dec -> A.dec | ||
27 : | end | ||
28 : | |||
29 : | structure BTrace :> BTRACE = struct | ||
30 : | |||
31 : | blume | 677 | exception NoCore |
32 : | |||
33 : | blume | 675 | fun impossible s = EM.impossible ("BTrace: " ^ s) |
34 : | |||
35 : | infix --> | ||
36 : | val op --> = BT.--> | ||
37 : | |||
38 : | blume | 677 | val i_i_Ty = BT.intTy --> BT.intTy |
39 : | val ii_u_Ty = BT.tupleTy [BT.intTy, BT.intTy] --> BT.unitTy | ||
40 : | blume | 675 | val u_u_Ty = BT.unitTy --> BT.unitTy |
41 : | val u_u_u_Ty = BT.unitTy --> u_u_Ty | ||
42 : | blume | 677 | val iis_u_Ty = BT.tupleTy [BT.intTy, BT.intTy, BT.unitTy] --> BT.unitTy |
43 : | blume | 675 | |
44 : | fun instrument0 (senv, cinfo: CB.compInfo) d = let | ||
45 : | |||
46 : | val matchstring = #errorMatch cinfo | ||
47 : | |||
48 : | val mkv = #mkLvar cinfo | ||
49 : | |||
50 : | fun tmpvar (n, t) = let | ||
51 : | val sy = Symbol.varSymbol n | ||
52 : | in | ||
53 : | VC.VALvar { access = Access.namedAcc (sy, mkv), info = II.nullInfo, | ||
54 : | path = SP.SPATH [sy], typ = ref t } | ||
55 : | end | ||
56 : | |||
57 : | val isSpecial = let | ||
58 : | val l = [SpecialSymbols.paramId, | ||
59 : | SpecialSymbols.functorId, | ||
60 : | SpecialSymbols.hiddenId, | ||
61 : | SpecialSymbols.tempStrId, | ||
62 : | SpecialSymbols.tempFctId, | ||
63 : | SpecialSymbols.fctbodyId, | ||
64 : | SpecialSymbols.anonfsigId, | ||
65 : | SpecialSymbols.resultId, | ||
66 : | SpecialSymbols.returnId, | ||
67 : | SpecialSymbols.internalVarId] | ||
68 : | in | ||
69 : | fn s => List.exists (fn s' => Symbol.eq (s, s')) l | ||
70 : | end | ||
71 : | |||
72 : | fun cons (s, []) = if isSpecial s then [] else [(s, 0)] | ||
73 : | | cons (s, l as ((s', m) :: t)) = | ||
74 : | if isSpecial s then l | ||
75 : | else if Symbol.eq (s, s') then (s, m+1) :: t | ||
76 : | else (s, 0) :: l | ||
77 : | |||
78 : | fun getCore s = let | ||
79 : | blume | 677 | fun err _ _ _ = raise NoCore |
80 : | blume | 675 | in |
81 : | Lookup.lookVal (senv, SP.SPATH [CoreSym.coreSym, | ||
82 : | Symbol.varSymbol s], err) | ||
83 : | end | ||
84 : | |||
85 : | fun getCoreVal s = | ||
86 : | case getCore s of | ||
87 : | VC.VAL r => r | ||
88 : | | _ => impossible "getCoreVal" | ||
89 : | |||
90 : | fun getCoreCon s = | ||
91 : | case getCore s of | ||
92 : | VC.CON c => c | ||
93 : | | _ => impossible "getCoreCon" | ||
94 : | |||
95 : | blume | 677 | val bt_reserve = getCoreVal "bt_reserve" |
96 : | blume | 675 | val bt_register = getCoreVal "bt_register" |
97 : | val bt_save = getCoreVal "bt_save" | ||
98 : | val bt_push = getCoreVal "bt_push" | ||
99 : | val bt_add = getCoreVal "bt_add" | ||
100 : | val matchcon = getCoreCon "Match" | ||
101 : | |||
102 : | blume | 677 | val bt_register_var = tmpvar ("<bt_register>", iis_u_Ty) |
103 : | blume | 675 | val bt_save_var = tmpvar ("<bt_save>", u_u_u_Ty) |
104 : | val bt_push_var = tmpvar ("<bt_push>", u_u_u_Ty) | ||
105 : | blume | 677 | val bt_add_var = tmpvar ("<bt_add>", ii_u_Ty) |
106 : | val bt_reserve_var = tmpvar ("<bt_reserve>", i_i_Ty) | ||
107 : | val bt_module_var = tmpvar ("<bt_module>", BT.intTy) | ||
108 : | blume | 675 | |
109 : | fun VARexp v = A.VARexp (ref v, []) | ||
110 : | fun INTexp i = A.INTexp (IntInf.fromInt i, BT.intTy) | ||
111 : | |||
112 : | val uExp = EU.unitExp | ||
113 : | val pushexp = A.APPexp (VARexp bt_push_var, uExp) | ||
114 : | val saveexp = A.APPexp (VARexp bt_save_var, uExp) | ||
115 : | |||
116 : | blume | 677 | fun mkaddexp id = A.APPexp (VARexp bt_add_var, |
117 : | EU.TUPLEexp [VARexp bt_module_var, | ||
118 : | INTexp id]) | ||
119 : | blume | 675 | fun mkregexp (id, s) = |
120 : | A.APPexp (VARexp bt_register_var, | ||
121 : | blume | 677 | EU.TUPLEexp [VARexp bt_module_var, |
122 : | INTexp id, A.STRINGexp s]) | ||
123 : | blume | 675 | |
124 : | val regexps = ref [] | ||
125 : | blume | 677 | val next = ref 0 |
126 : | blume | 675 | |
127 : | fun mkadd (id, s) = | ||
128 : | (regexps := mkregexp (id, s) :: !regexps; | ||
129 : | mkaddexp id) | ||
130 : | |||
131 : | fun VALdec (v, e) = | ||
132 : | A.VALdec [A.VB { pat = A.VARpat v, exp = e, | ||
133 : | tyvars = ref [], boundtvs = [] }] | ||
134 : | fun LETexp (v, e, b) = A.LETexp (VALdec (v, e), b) | ||
135 : | fun AUexp v = A.APPexp (VARexp v, uExp) (* apply to unit *) | ||
136 : | |||
137 : | fun is_prim_exp (A.VARexp (ref (VC.VALvar v), _)) = | ||
138 : | II.isPrimInfo (#info v) | ||
139 : | | is_prim_exp (A.CONexp _) = true | ||
140 : | | is_prim_exp (A.CONSTRAINTexp (e, _)) = is_prim_exp e | ||
141 : | | is_prim_exp (A.MARKexp (e, _)) = is_prim_exp e | ||
142 : | | is_prim_exp _ = false | ||
143 : | |||
144 : | fun i_exp _ loc (A.RECORDexp l) = | ||
145 : | A.RECORDexp (map (fn (l, e) => (l, i_exp false loc e)) l) | ||
146 : | | i_exp _ loc (A.SELECTexp (l, e)) = | ||
147 : | A.SELECTexp (l, i_exp false loc e) | ||
148 : | | i_exp _ loc (A.VECTORexp (l, t)) = | ||
149 : | A.VECTORexp (map (i_exp false loc) l, t) | ||
150 : | | i_exp tail loc (A.PACKexp (e, t, tcl)) = | ||
151 : | A.PACKexp (i_exp tail loc e, t, tcl) | ||
152 : | | i_exp tail loc (e as A.APPexp (f, a)) = | ||
153 : | if tail orelse is_prim_exp f then | ||
154 : | A.APPexp (i_exp false loc f, i_exp false loc a) | ||
155 : | else let | ||
156 : | val mainexp = A.APPexp (i_exp false loc f, | ||
157 : | i_exp false loc a) | ||
158 : | val ty = Reconstruct.expType e | ||
159 : | val result = tmpvar ("tmpresult", ty) | ||
160 : | val restore = tmpvar ("tmprestore", u_u_Ty) | ||
161 : | in | ||
162 : | LETexp (restore, pushexp, | ||
163 : | LETexp (result, mainexp, | ||
164 : | A.SEQexp [AUexp restore, VARexp result])) | ||
165 : | end | ||
166 : | | i_exp tail loc (A.HANDLEexp (e, A.HANDLER (A.FNexp (rl, t)))) = let | ||
167 : | val restore = tmpvar ("tmprestore", u_u_Ty) | ||
168 : | fun rule (A.RULE (p, e)) = | ||
169 : | A.RULE (p, A.SEQexp [AUexp restore, i_exp tail loc e]) | ||
170 : | in | ||
171 : | LETexp (restore, saveexp, | ||
172 : | A.HANDLEexp (i_exp false loc e, | ||
173 : | A.HANDLER (A.FNexp (map rule rl, t)))) | ||
174 : | end | ||
175 : | | i_exp _ _ (A.HANDLEexp _) = impossible "bad HANDLEexp" | ||
176 : | | i_exp _ loc (A.RAISEexp (e, t)) = | ||
177 : | A.RAISEexp (i_exp false loc e, t) | ||
178 : | | i_exp tail loc (A.CASEexp (e, rl, b)) = | ||
179 : | A.CASEexp (i_exp false loc e, map (i_rule tail loc) rl, b) | ||
180 : | | i_exp tail loc (A.FNexp (rl, t)) = let | ||
181 : | fun name ((s, 0), a) = Symbol.name s :: a | ||
182 : | | name ((s, m), a) = Symbol.name s :: "[" :: | ||
183 : | Int.toString (m + 1) :: "]" :: a | ||
184 : | fun dot ([z], a) = name (z, a) | ||
185 : | | dot (h :: t, a) = dot (t, "." :: name (h, a)) | ||
186 : | | dot ([], a) = impossible "FNexp: no path" | ||
187 : | val (n, r) = loc | ||
188 : | val ms = matchstring r | ||
189 : | val descr = concat (ms :: ": " :: dot (n, [])) | ||
190 : | blume | 677 | val id = !next |
191 : | val _ = next := id + 1 | ||
192 : | blume | 675 | val addexp = mkadd (id, descr) |
193 : | val arg = tmpvar ("fnvar", t) | ||
194 : | val rl' = map (i_rule true loc) rl | ||
195 : | val re = let | ||
196 : | val A.RULE (_, lst) = List.last rl | ||
197 : | val t = Reconstruct.expType lst | ||
198 : | in | ||
199 : | A.RAISEexp (A.CONexp (matchcon, []), t) | ||
200 : | end | ||
201 : | in | ||
202 : | A.FNexp ([A.RULE (A.VARpat arg, | ||
203 : | A.SEQexp [addexp, | ||
204 : | A.CASEexp (A.VARexp (ref arg, []), | ||
205 : | rl', true)]), | ||
206 : | A.RULE (A.WILDpat, re)], | ||
207 : | t) | ||
208 : | end | ||
209 : | | i_exp tail loc (A.LETexp (d, e)) = | ||
210 : | A.LETexp (i_dec loc d, i_exp tail loc e) | ||
211 : | | i_exp tail loc (A.SEQexp l) = | ||
212 : | A.SEQexp (#1 (foldr (fn (e, (l, t)) => (i_exp t loc e :: l, false)) | ||
213 : | ([], tail) l)) | ||
214 : | | i_exp tail loc (A.CONSTRAINTexp (e, t)) = | ||
215 : | A.CONSTRAINTexp (i_exp tail loc e, t) | ||
216 : | | i_exp tail (n, _) (A.MARKexp (e, r)) = | ||
217 : | A.MARKexp (i_exp tail (n, r) e, r) | ||
218 : | | i_exp _ _ e = e | ||
219 : | |||
220 : | and i_dec loc (A.VALdec l) = A.VALdec (map (i_vb loc) l) | ||
221 : | | i_dec loc (A.VALRECdec l) = A.VALRECdec (map (i_rvb loc) l) | ||
222 : | | i_dec loc (A.ABSTYPEdec { abstycs, withtycs, body }) = | ||
223 : | A.ABSTYPEdec { abstycs = abstycs, withtycs = withtycs, | ||
224 : | body = i_dec loc body } | ||
225 : | | i_dec loc (A.EXCEPTIONdec l) = A.EXCEPTIONdec (map (i_eb loc) l) | ||
226 : | | i_dec loc (A.STRdec l) = A.STRdec (map (i_strb loc) l) | ||
227 : | | i_dec loc (A.ABSdec l) = A.ABSdec (map (i_strb loc) l) | ||
228 : | | i_dec loc (A.FCTdec l) = A.FCTdec (map (i_fctb loc) l) | ||
229 : | | i_dec loc (A.LOCALdec (d, d')) = | ||
230 : | A.LOCALdec (i_dec loc d, i_dec loc d') | ||
231 : | | i_dec loc (A.SEQdec l) = A.SEQdec (map (i_dec loc) l) | ||
232 : | | i_dec (n, _) (A.MARKdec (d, r)) = A.MARKdec (i_dec (n, r) d, r) | ||
233 : | | i_dec _ d = d | ||
234 : | |||
235 : | and i_rule tail loc (A.RULE (p, e)) = A.RULE (p, i_exp tail loc e) | ||
236 : | |||
237 : | and i_vb (n, r) (vb as A.VB { pat, exp, boundtvs, tyvars }) = let | ||
238 : | fun gv (A.VARpat v) = SOME v | ||
239 : | | gv (A.CONSTRAINTpat (p, _)) = gv p | ||
240 : | | gv (A.LAYEREDpat (p, p')) = | ||
241 : | (case gv p of | ||
242 : | SOME v => SOME v | ||
243 : | | NONE => gv p') | ||
244 : | | gv _ = NONE | ||
245 : | fun recur n = A.VB { pat = pat, exp = i_exp false (n, r) exp, | ||
246 : | boundtvs = boundtvs, tyvars = tyvars } | ||
247 : | in | ||
248 : | case gv pat of | ||
249 : | SOME (VC.VALvar { path = SP.SPATH [x], info, ... }) => | ||
250 : | if II.isPrimInfo info then vb else recur (cons (x, n)) | ||
251 : | | SOME (VC.VALvar { info, ... }) => | ||
252 : | if II.isPrimInfo info then vb else recur n | ||
253 : | | _ => recur n | ||
254 : | end | ||
255 : | |||
256 : | and i_rvb (n, r) (A.RVB { var, exp, boundtvs, resultty, tyvars }) = let | ||
257 : | val x = | ||
258 : | case var of | ||
259 : | VC.VALvar { path = SymPath.SPATH [x], ... } => x | ||
260 : | | _ => impossible "VALRECdec" | ||
261 : | in | ||
262 : | A.RVB { var = var, exp = i_exp false (cons (x, n), r) exp, | ||
263 : | boundtvs = boundtvs, resultty = resultty, tyvars = tyvars } | ||
264 : | end | ||
265 : | |||
266 : | and i_eb loc (A.EBgen { exn, etype, ident }) = | ||
267 : | A.EBgen { exn = exn, etype = etype, ident = i_exp false loc ident } | ||
268 : | | i_eb _ eb = eb | ||
269 : | |||
270 : | and i_strb (n, r) (A.STRB { name, str, def }) = | ||
271 : | A.STRB { name = name, str = str, | ||
272 : | def = i_strexp (cons (name, n), r) def } | ||
273 : | |||
274 : | and i_fctb (n, r) (A.FCTB { name, fct, def }) = | ||
275 : | A.FCTB { name = name, fct = fct, | ||
276 : | def = i_fctexp (cons (name, n), r) def } | ||
277 : | |||
278 : | and i_strexp loc (A.LETstr (d, s)) = | ||
279 : | A.LETstr (i_dec loc d, i_strexp loc s) | ||
280 : | | i_strexp (n, _) (A.MARKstr (s, r)) = | ||
281 : | A.MARKstr (i_strexp (n, r) s, r) | ||
282 : | | i_strexp _ s = s | ||
283 : | |||
284 : | and i_fctexp loc (A.FCTfct { param, argtycs, def }) = | ||
285 : | A.FCTfct { param = param, argtycs = argtycs, | ||
286 : | def = i_strexp loc def } | ||
287 : | | i_fctexp loc (A.LETfct (d, f)) = | ||
288 : | A.LETfct (i_dec loc d, i_fctexp loc f) | ||
289 : | | i_fctexp (n, _) (A.MARKfct (f, r)) = | ||
290 : | A.MARKfct (i_fctexp (n, r) f, r) | ||
291 : | | i_fctexp _ f = f | ||
292 : | |||
293 : | val d' = i_dec ([], (0, 0)) d | ||
294 : | in | ||
295 : | blume | 677 | A.LOCALdec (A.SEQdec [VALdec (bt_reserve_var, AUexp bt_reserve), |
296 : | VALdec (bt_module_var, | ||
297 : | A.APPexp (VARexp bt_reserve_var, | ||
298 : | INTexp (!next))), | ||
299 : | VALdec (bt_save_var, AUexp bt_save), | ||
300 : | blume | 675 | VALdec (bt_push_var, AUexp bt_push), |
301 : | VALdec (bt_register_var, AUexp bt_register), | ||
302 : | VALdec (bt_add_var, | ||
303 : | A.SEQexp (!regexps @ [AUexp bt_add]))], | ||
304 : | d') | ||
305 : | end | ||
306 : | |||
307 : | fun instrument params d = | ||
308 : | blume | 677 | if SMLofNJ.Internals.BTrace.mode NONE then |
309 : | instrument0 params d | ||
310 : | handle NoCore => d (* this takes care of core.sml *) | ||
311 : | blume | 675 | else d |
312 : | end | ||
313 : | |||
314 : | end (* local *) |
root@smlnj-gforge.cs.uchicago.edu | ViewVC Help |
Powered by ViewVC 1.0.0 |