Home My Page Projects Code Snippets Project Openings SML/NJ
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files

SCM Repository

[smlnj] Annotation of /sml/trunk/compiler/CPS/cps/ppcps.sml
ViewVC logotype

Annotation of /sml/trunk/compiler/CPS/cps/ppcps.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5162 - (view) (download)

1 : jhr 4446 (* ppcps.sml
2 :     *
3 : jhr 4540 * COPYRIGHT (c) 2018 The Fellowship of SML/NJ (http://www.smlnj.org)
4 : jhr 4446 * All rights reserved.
5 :     *)
6 : monnier 245
7 :     signature PPCPS =
8 : jhr 4540 sig
9 : monnier 245
10 : jhr 4540 val value2str : CPS.value -> string
11 : monnier 245
12 : jhr 5162 val vpathToString : CPS.value * CPS.accesspath -> string
13 :    
14 : jhr 4540 val printcps : (CPS.function * LtyDef.lty IntHashTable.hash_table) -> unit
15 :     val printcps0: CPS.function -> unit
16 :     val prcps : CPS.cexp -> unit
17 :    
18 : jhr 4960 (* string conversions for various CPS.P types *)
19 :     val numkindToString : CPS.P.numkind -> string
20 :     val arithopToString : CPS.P.arithop -> string
21 : jhr 5022 val pureopToString : CPS.P.pureop -> string
22 : jhr 4960 val cmpopToString : CPS.P.cmpop -> string
23 :     val fcmpopToString : CPS.P.fcmpop -> string
24 :     val branchToString : CPS.P.branch -> string
25 :     val setterToString : CPS.P.setter -> string
26 :     val lookerToString : CPS.P.looker -> string
27 :     val arithToString : CPS.P.arith -> string
28 :     val pureToString : CPS.P.pure -> string
29 :    
30 : jhr 4540 end (* signature PPCPS *)
31 :    
32 : monnier 245 structure PPCps : PPCPS =
33 : jhr 4960 struct
34 : monnier 245
35 : jhr 4960 open CPS
36 :     structure LV = LambdaVar
37 :     structure U = CPSUtil
38 : monnier 245
39 : jhr 4960 val say = Control.Print.say
40 : monnier 245
41 : jhr 4960 val sayt = say o U.ctyToString
42 : monnier 245
43 : jhr 4960 fun value2str (VAR v) = LV.lvarName v
44 :     | value2str (LABEL v) = "(L)" ^ LV.lvarName v
45 :     | value2str (NUM{ival, ty={sz=0, ...}}) = "(II)" ^ IntInf.toString ival
46 :     | value2str (NUM{ival, ty={tag=true, ...}}) = "(I)" ^ IntInf.toString ival
47 :     | value2str (NUM{ival, ty={sz, ...}}) = concat[
48 :     "(I", Int.toString sz, ")", IntInf.toString ival
49 :     ]
50 :     | value2str (REAL{rval, ty}) = concat[
51 :     "(R", Int.toString ty, ")", RealLit.toString rval
52 :     ]
53 :     | value2str (STRING s) = concat["\"", String.toString s, "\""]
54 :     | value2str (VOID) = "(void)"
55 : jhr 4540
56 : jhr 4960 fun numkindToString (P.INT bits) = "i" ^ Int.toString bits
57 :     | numkindToString (P.UINT bits) = "u" ^ Int.toString bits
58 :     | numkindToString (P.FLOAT bits) = "f" ^ Int.toString bits
59 : monnier 245
60 : jhr 5022 val arithopToString = ArithOps.arithopToString
61 : monnier 245
62 : jhr 5022 val pureopToString = ArithOps.pureopToString
63 : monnier 245
64 : jhr 5022 val cmpopToString = ArithOps.cmpopToString
65 :    
66 : jhr 4960 fun fcmpopToString P.F_EQ = "="
67 :     | fcmpopToString P.F_ULG = "?<>"
68 :     | fcmpopToString P.F_GT = ">"
69 :     | fcmpopToString P.F_GE = ">="
70 :     | fcmpopToString P.F_LT = "<"
71 :     | fcmpopToString P.F_LE = "<="
72 :     | fcmpopToString P.F_LG = "<>"
73 :     | fcmpopToString P.F_LEG = "<="
74 :     | fcmpopToString P.F_UGT = "?>"
75 :     | fcmpopToString P.F_UGE = "?>="
76 :     | fcmpopToString P.F_ULT = "?<"
77 :     | fcmpopToString P.F_ULE = "?<="
78 :     | fcmpopToString P.F_UE = "?="
79 :     | fcmpopToString P.F_UN = "?"
80 : monnier 245
81 : jhr 4970 fun branchToString (P.CMP{oper, kind}) = numkindToString kind ^ cmpopToString oper
82 :     | branchToString (P.FCMP{oper, size}) = numkindToString (P.FLOAT size) ^ fcmpopToString oper
83 :     | branchToString (P.FSGN sz) = numkindToString (P.FLOAT sz) ^ "sgn"
84 :     | branchToString P.BOXED = "boxed"
85 : jhr 4960 | branchToString P.UNBOXED = "unboxed"
86 :     | branchToString P.PEQL = "peql"
87 :     | branchToString P.PNEQ = "pneq"
88 :     | branchToString P.STREQL = "streql"
89 :     | branchToString P.STRNEQ = "strneq"
90 : monnier 245
91 : jhr 4960 fun setterToString P.UNBOXEDUPDATE = "unboxedupdate"
92 :     | setterToString P.UPDATE = "update"
93 :     | setterToString (P.NUMUPDATE{kind}) = ("numupdate" ^ numkindToString kind)
94 :     | setterToString P.UNBOXEDASSIGN = "unboxedassign"
95 :     | setterToString P.ASSIGN = "assign"
96 :     | setterToString P.SETHDLR = "sethdlr"
97 :     | setterToString P.SETVAR = "setvar"
98 :     | setterToString P.SETSPECIAL = "setspecial"
99 :     | setterToString (P.RAWSTORE{kind}) = ("rawstore" ^ numkindToString kind)
100 :     | setterToString (P.RAWUPDATE cty) = ("rawupdate" ^ U.ctyToString cty)
101 : monnier 245
102 : jhr 4960 fun lookerToString P.DEREF = "!"
103 :     | lookerToString P.GETHDLR = "gethdlr"
104 :     | lookerToString P.SUBSCRIPT = "subscript"
105 :     | lookerToString (P.NUMSUBSCRIPT{kind}) = ("numsubscript" ^ numkindToString kind)
106 :     | lookerToString P.GETVAR = "getvar"
107 :     | lookerToString P.GETSPECIAL = "getspecial"
108 :     | lookerToString (P.RAWLOAD{kind}) = ("rawload" ^ numkindToString kind)
109 : monnier 245
110 : jhr 4960 val cvtParam = Int.toString
111 :     fun cvtParams (prefix, from, to) = concat [prefix, cvtParam from, "_", cvtParam to]
112 : leunga 1094
113 : jhr 5022 fun arithToString (P.IARITH{oper, sz}) = arithopToString oper ^ cvtParam sz
114 : jhr 4960 | arithToString (P.TEST{from, to}) = cvtParams ("test_", from, to)
115 :     | arithToString (P.TESTU{from, to}) = cvtParams ("testu_", from, to)
116 :     | arithToString (P.TEST_INF i) = "test_inf_" ^ cvtParam i
117 : jhr 4986 | arithToString (P.REAL_TO_INT{floor, from, to}) = concat[
118 :     if floor then "floor_" else "round_", cvtParam from, "to", cvtParam to
119 : jhr 4960 ]
120 : jhr 4446
121 : jhr 4960 fun pureToString P.LENGTH = "length"
122 : jhr 5022 | pureToString (P.PURE_ARITH{oper,kind}) = pureopToString oper ^ numkindToString kind
123 : jhr 4960 | pureToString P.OBJLENGTH = "objlength"
124 :     | pureToString P.MAKEREF = "makeref"
125 :     | pureToString (P.EXTEND{from, to}) = cvtParams ("extend_", from, to)
126 :     | pureToString (P.COPY{from, to}) = cvtParams ("copy_", from, to)
127 :     | pureToString (P.TRUNC{from, to}) = cvtParams ("trunc_", from, to)
128 :     | pureToString (P.TRUNC_INF i) = "trunc_inf_" ^ cvtParam i
129 :     | pureToString (P.COPY_INF i) = concat ["copy_", cvtParam i, "_inf"]
130 :     | pureToString (P.EXTEND_INF i) = concat ["extend_", cvtParam i, "_inf"]
131 : jhr 4986 | pureToString (P.INT_TO_REAL{from, to}) =
132 :     concat ["real", cvtParam from, "_", cvtParam to]
133 : jhr 4960 | pureToString P.SUBSCRIPTV = "subscriptv"
134 :     | pureToString (P.PURE_NUMSUBSCRIPT{kind}) = "numsubscriptv" ^ numkindToString kind
135 :     | pureToString P.GETTAG = "gettag"
136 :     | pureToString P.MKSPECIAL = "mkspecial"
137 :     | pureToString P.CAST = "cast"
138 :     | pureToString P.GETCON = "getcon"
139 :     | pureToString P.GETEXN = "getexn"
140 :     | pureToString P.BOX = "box"
141 :     | pureToString P.UNBOX = "unbox"
142 :     | pureToString (P.WRAP kind) = "wrap_" ^ numkindToString kind
143 :     | pureToString (P.UNWRAP kind) = "unwrap_" ^ numkindToString kind
144 :     | pureToString P.GETSEQDATA = "getseqdata"
145 :     | pureToString P.RECSUBSCRIPT = "recsubscript"
146 :     | pureToString P.RAW64SUBSCRIPT = "raw64subscript"
147 :     | pureToString P.NEWARRAY0 = "newarray0"
148 :     | pureToString (P.RAWRECORD rk) = "rawrecord_"^getOpt(Option.map rkstring rk, "notag")
149 : monnier 245
150 : jhr 4960 and rkstring rk = (case rk
151 :     of RK_VECTOR => "RK_VECTOR"
152 :     | RK_RECORD => "RK_RECORD"
153 :     | RK_ESCAPE => "RK_ESCAPE"
154 :     | RK_CONT => "RK_CONT"
155 :     | RK_FCONT => "RK_FCONT"
156 :     | RK_KNOWN => "RK_KNOWN"
157 : jhr 5015 | RK_RAW64BLOCK => "RK_RAW64BLOCK"
158 :     | RK_RAWBLOCK => "RK_RAWBLOCK"
159 : jhr 4960 (* end case *))
160 : monnier 245
161 : jhr 5162 fun vpathToString (v, p) = let
162 :     fun toList (OFFp 0) = []
163 :     | toList (OFFp n) = ["+", Int.toString n] (* assumes n > 0 *)
164 :     | toList (SELp(i, p)) = "." :: toList p
165 :     in
166 :     String.concat(value2str v :: toList p)
167 :     end
168 :    
169 : jhr 4960 fun show0 say = let
170 :     fun sayc (#"\n") = say "\\n"
171 :     | sayc c = say(String.str c)
172 : monnier 245
173 : jhr 4960 fun sayv v = say(value2str v)
174 : monnier 245
175 : jhr 4960 fun sayvlist [v] = sayv v
176 :     | sayvlist nil = ()
177 :     | sayvlist (v::vl) = (sayv v; say ","; sayvlist vl)
178 : monnier 245
179 : jhr 4960 fun sayrk (RK_RECORD, n) = ()
180 :     | sayrk (RK_VECTOR, n) = ()
181 :     | sayrk (k, n) = (say (rkstring k); say " "; say (Int.toString n); say ",")
182 : jhr 4446
183 : jhr 4960 fun sayparam ([v],[ct]) = (sayv v; sayt ct)
184 :     | sayparam (nil,nil) = ()
185 :     | sayparam (v::vl,ct::cl) = (sayv v; sayt ct; say ","; sayparam(vl,cl))
186 :     | sayparam _ = ErrorMsg.impossible "sayparam in ppcps.sml"
187 : monnier 245
188 : jhr 5162 fun sayvp (v,path) = say(vpathToString(v, path))
189 : jhr 4960 fun saylist f [x] = f x | saylist f nil = ()
190 :     | saylist f (x::r) = (f x; say ","; saylist f r)
191 :     fun indent n = let
192 :     fun space 0 = () | space k = (say " "; space(k-1))
193 :     fun nl() = say "\n"
194 :     fun f (RECORD(k,vl,v,c)) = (
195 :     space n;
196 :     case k of RK_VECTOR => say "#{" | _ => say "{";
197 :     sayrk(k,length vl);
198 :     saylist sayvp vl; say "} -> ";
199 :     sayv(VAR v);
200 :     nl(); f c)
201 :     | f (SELECT(i,v,w,t,c)) = (
202 :     space n; sayv v; say "."; say(Int.toString i); say " -> ";
203 :     sayv(VAR w); sayt(t); nl(); f c)
204 :     | f (OFFSET(i,v,w,c)) = (
205 :     space n; sayv v; say "+"; say(Int.toString i); say " -> ";
206 :     sayv(VAR w); nl(); f c)
207 :     | f (APP(w,vl)) = (
208 :     space n; sayv w; say "("; sayvlist vl; say ")\n")
209 :     | f (FIX(bl,c)) = let
210 :     fun g (_,v,wl,cl,d) = (
211 :     space n; sayv(VAR v); say "(";
212 :     sayparam (map VAR wl,cl);
213 :     say ") =\n";
214 :     indent (n+3) d)
215 :     in
216 :     app g bl; f c
217 :     end
218 :     | f (SWITCH(v,c,cl)) = let
219 :     fun g (i,c::cl) = (
220 :     space(n+1); say(Int.toString(i:int));
221 :     say " =>\n"; indent (n+3) c; g(i+1,cl))
222 :     | g (_,nil) = ()
223 :     in
224 :     space n; say "case "; sayv v; say " [";
225 :     say(Int.toString(c));
226 :     say "] of\n";
227 :     g(0,cl)
228 :     end
229 :     | f (LOOKER(i,vl,w,t,e)) = (
230 :     space n; say(lookerToString i); say "("; sayvlist vl;
231 :     say ") -> "; sayv(VAR w); sayt(t); nl(); f e)
232 :     | f (ARITH(i,vl,w,t,e)) = (
233 :     space n; say(arithToString i); say "("; sayvlist vl;
234 :     say ") -> "; sayv(VAR w); sayt(t); nl(); f e)
235 :     | f (PURE(i,vl,w,t,e)) = (
236 :     space n; say(pureToString i); say "("; sayvlist vl;
237 :     say ") -> "; sayv(VAR w); sayt(t); nl(); f e)
238 :     | f (SETTER(i,vl,e)) = (
239 :     space n; say(setterToString i); say "("; sayvlist vl;
240 :     say ")"; nl(); f e)
241 :     | f (BRANCH(i,vl,c,e1,e2)) = (
242 :     space n; say "if "; say(branchToString i);
243 :     say "("; sayvlist vl; say ") [";
244 :     sayv(VAR c); say "] then\n";
245 :     indent (n+3) e1;
246 :     space n; say "else\n";
247 :     indent (n+3) e2)
248 :     | f (RCC(k,l,p,vl,wtl,e)) = (
249 :     space n;
250 :     if k = REENTRANT_RCC then say "reentrant " else ();
251 :     if l = "" then () else (say l; say " ");
252 :     say "rcc("; sayvlist vl; say ") -> ";
253 :     app (fn (w, t) => (sayv (VAR w); sayt(t))) wtl;
254 :     nl(); f e)
255 :     in
256 :     f
257 :     end
258 :     in
259 :     indent
260 :     end (* show0 *)
261 : monnier 245
262 : jhr 4960 fun printcps((_,f,vl,cl,e),m) = let
263 :     fun ptv(v,t) = (say(LV.lvarName v); say " type ===>>>";
264 :     say(LtyExtern.lt_print t); say "\n")
265 :     val _ = if (!Control.CG.debugRep)
266 :     then (say "************************************************\n";
267 :     IntHashTable.appi ptv m;
268 :     say "************************************************\n")
269 :     else ()
270 :     fun sayv(v) = say(LV.lvarName v)
271 :     fun sayparam ([v],[ct]) = (sayv v; sayt ct)
272 :     | sayparam (nil,nil) = ()
273 :     | sayparam (v::vl,ct::cl) = (sayv v; sayt ct; say ","; sayparam(vl,cl))
274 :     | sayparam _ = ErrorMsg.impossible "sayparam in ppcps.sml 3435"
275 : monnier 245
276 : jhr 4960 in
277 :     say(LV.lvarName f); say "("; sayparam(vl,cl); say ") =\n";
278 :     show0 say 3 e
279 :     end
280 : monnier 245
281 : jhr 4960 exception NULLTABLE
282 :     val nulltable : LtyDef.lty IntHashTable.hash_table =
283 :     IntHashTable.mkTable(8, NULLTABLE)
284 : monnier 245
285 : jhr 4960 fun printcps0 f = printcps(f,nulltable)
286 : monnier 245
287 : jhr 4960 fun prcps(ce) = show0 (Control.Print.say) 1 ce
288 : monnier 245
289 : jhr 4960 end (* structure PPCps *)

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0