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

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