adamc@29
|
1 (* Copyright (c) 2008, Adam Chlipala
|
adamc@29
|
2 * All rights reserved.
|
adamc@29
|
3 *
|
adamc@29
|
4 * Redistribution and use in source and binary forms, with or without
|
adamc@29
|
5 * modification, are permitted provided that the following conditions are met:
|
adamc@29
|
6 *
|
adamc@29
|
7 * - Redistributions of source code must retain the above copyright notice,
|
adamc@29
|
8 * this list of conditions and the following disclaimer.
|
adamc@29
|
9 * - Redistributions in binary form must reproduce the above copyright notice,
|
adamc@29
|
10 * this list of conditions and the following disclaimer in the documentation
|
adamc@29
|
11 * and/or other materials provided with the distribution.
|
adamc@29
|
12 * - The names of contributors may not be used to endorse or promote products
|
adamc@29
|
13 * derived from this software without specific prior written permission.
|
adamc@29
|
14 *
|
adamc@29
|
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
adamc@29
|
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
adamc@29
|
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
adamc@29
|
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
adamc@29
|
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
adamc@29
|
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
adamc@29
|
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
adamc@29
|
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
adamc@29
|
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
adamc@29
|
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
adamc@29
|
25 * POSSIBILITY OF SUCH DAMAGE.
|
adamc@29
|
26 *)
|
adamc@29
|
27
|
adamc@29
|
28 structure Cjrize :> CJRIZE = struct
|
adamc@29
|
29
|
adamc@109
|
30 structure L = Mono
|
adamc@29
|
31 structure L' = Cjr
|
adamc@29
|
32
|
adamc@29
|
33 structure Sm :> sig
|
adamc@29
|
34 type t
|
adamc@29
|
35
|
adamc@29
|
36 val empty : t
|
adamc@29
|
37 val find : t * (string * L.typ) list * (string * L'.typ) list -> t * int
|
adamc@29
|
38
|
adamc@29
|
39 val declares : t -> (int * (string * L'.typ) list) list
|
adamc@29
|
40 end = struct
|
adamc@29
|
41
|
adamc@29
|
42 structure FM = BinaryMapFn(struct
|
adamc@29
|
43 type ord_key = L.typ
|
adamc@109
|
44 val compare = MonoUtil.Typ.compare
|
adamc@29
|
45 end)
|
adamc@29
|
46
|
adamc@29
|
47 type t = int * int FM.map * (int * (string * L'.typ) list) list
|
adamc@29
|
48
|
adamc@102
|
49 val empty : t = (1, FM.insert (FM.empty, (L.TRecord [], ErrorMsg.dummySpan), 0), [])
|
adamc@29
|
50
|
adamc@29
|
51 fun find ((n, m, ds), xts, xts') =
|
adamc@29
|
52 let
|
adamc@29
|
53 val t = (L.TRecord xts, ErrorMsg.dummySpan)
|
adamc@29
|
54 in
|
adamc@29
|
55 case FM.find (m, t) of
|
adamc@29
|
56 NONE => ((n+1, FM.insert (m, t, n), (n, xts') :: ds), n)
|
adamc@29
|
57 | SOME i => ((n, m, ds), i)
|
adamc@29
|
58 end
|
adamc@29
|
59
|
adamc@29
|
60 fun declares (_, _, ds) = ds
|
adamc@29
|
61
|
adamc@29
|
62 end
|
adamc@29
|
63
|
adamc@29
|
64 fun cifyTyp ((t, loc), sm) =
|
adamc@29
|
65 case t of
|
adamc@109
|
66 L.TFun (t1, t2) =>
|
adamc@29
|
67 let
|
adamc@29
|
68 val (t1, sm) = cifyTyp (t1, sm)
|
adamc@29
|
69 val (t2, sm) = cifyTyp (t2, sm)
|
adamc@29
|
70 in
|
adamc@109
|
71 ((L'.TFun (t1, t2), loc), sm)
|
adamc@29
|
72 end
|
adamc@29
|
73 | L.TRecord xts =>
|
adamc@29
|
74 let
|
adamc@29
|
75 val old_xts = xts
|
adamc@29
|
76 val (xts, sm) = ListUtil.foldlMap (fn ((x, t), sm) =>
|
adamc@29
|
77 let
|
adamc@29
|
78 val (t, sm) = cifyTyp (t, sm)
|
adamc@29
|
79 in
|
adamc@29
|
80 ((x, t), sm)
|
adamc@29
|
81 end)
|
adamc@29
|
82 sm xts
|
adamc@29
|
83 val (sm, si) = Sm.find (sm, old_xts, xts)
|
adamc@29
|
84 in
|
adamc@29
|
85 ((L'.TRecord si, loc), sm)
|
adamc@29
|
86 end
|
adamc@168
|
87 | L.TDatatype (n, xncs) =>
|
adamc@168
|
88 let
|
adamc@168
|
89 val (xncs, sm) = ListUtil.foldlMap (fn ((x, n, to), sm) =>
|
adamc@168
|
90 case to of
|
adamc@168
|
91 NONE => ((x, n, NONE), sm)
|
adamc@168
|
92 | SOME t =>
|
adamc@168
|
93 let
|
adamc@168
|
94 val (t, sm) = cifyTyp (t, sm)
|
adamc@168
|
95 in
|
adamc@168
|
96 ((x, n, SOME t), sm)
|
adamc@168
|
97 end)
|
adamc@168
|
98 sm xncs
|
adamc@168
|
99 in
|
adamc@168
|
100 ((L'.TDatatype (n, xncs), loc), sm)
|
adamc@168
|
101 end
|
adamc@53
|
102 | L.TFfi mx => ((L'.TFfi mx, loc), sm)
|
adamc@29
|
103
|
adamc@109
|
104 val dummye = (L'.EPrim (Prim.Int 0), ErrorMsg.dummySpan)
|
adamc@109
|
105
|
adamc@181
|
106 fun cifyPatCon pc =
|
adamc@181
|
107 case pc of
|
adamc@181
|
108 L.PConVar n => L'.PConVar n
|
adamc@181
|
109 | L.PConFfi mx => L'.PConFfi mx
|
adamc@181
|
110
|
adamc@182
|
111 fun cifyPat ((p, loc), sm) =
|
adamc@181
|
112 case p of
|
adamc@182
|
113 L.PWild => ((L'.PWild, loc), sm)
|
adamc@182
|
114 | L.PVar (x, t) =>
|
adamc@182
|
115 let
|
adamc@182
|
116 val (t, sm) = cifyTyp (t, sm)
|
adamc@182
|
117 in
|
adamc@182
|
118 ((L'.PVar (x, t), loc), sm)
|
adamc@182
|
119 end
|
adamc@182
|
120 | L.PPrim p => ((L'.PPrim p, loc), sm)
|
adamc@182
|
121 | L.PCon (pc, NONE) => ((L'.PCon (cifyPatCon pc, NONE), loc), sm)
|
adamc@182
|
122 | L.PCon (pc, SOME p) =>
|
adamc@182
|
123 let
|
adamc@182
|
124 val (p, sm) = cifyPat (p, sm)
|
adamc@182
|
125 in
|
adamc@182
|
126 ((L'.PCon (cifyPatCon pc, SOME p), loc), sm)
|
adamc@182
|
127 end
|
adamc@182
|
128 | L.PRecord xps =>
|
adamc@182
|
129 let
|
adamc@182
|
130 val (xps, sm) = ListUtil.foldlMap (fn ((x, p, t), sm) =>
|
adamc@182
|
131 let
|
adamc@182
|
132 val (p, sm) = cifyPat (p, sm)
|
adamc@182
|
133 val (t, sm) = cifyTyp (t, sm)
|
adamc@182
|
134 in
|
adamc@182
|
135 ((x, p, t), sm)
|
adamc@182
|
136 end) sm xps
|
adamc@182
|
137 in
|
adamc@182
|
138 ((L'.PRecord xps, loc), sm)
|
adamc@182
|
139 end
|
adamc@181
|
140
|
adamc@29
|
141 fun cifyExp ((e, loc), sm) =
|
adamc@29
|
142 case e of
|
adamc@29
|
143 L.EPrim p => ((L'.EPrim p, loc), sm)
|
adamc@29
|
144 | L.ERel n => ((L'.ERel n, loc), sm)
|
adamc@29
|
145 | L.ENamed n => ((L'.ENamed n, loc), sm)
|
adamc@181
|
146 | L.ECon (n, eo) =>
|
adamc@181
|
147 let
|
adamc@181
|
148 val (eo, sm) =
|
adamc@181
|
149 case eo of
|
adamc@181
|
150 NONE => (NONE, sm)
|
adamc@181
|
151 | SOME e =>
|
adamc@181
|
152 let
|
adamc@181
|
153 val (e, sm) = cifyExp (e, sm)
|
adamc@181
|
154 in
|
adamc@181
|
155 (SOME e, sm)
|
adamc@181
|
156 end
|
adamc@181
|
157 in
|
adamc@181
|
158 ((L'.ECon (n, eo), loc), sm)
|
adamc@181
|
159 end
|
adamc@53
|
160 | L.EFfi mx => ((L'.EFfi mx, loc), sm)
|
adamc@53
|
161 | L.EFfiApp (m, x, es) =>
|
adamc@53
|
162 let
|
adamc@53
|
163 val (es, sm) = ListUtil.foldlMap cifyExp sm es
|
adamc@53
|
164 in
|
adamc@53
|
165 ((L'.EFfiApp (m, x, es), loc), sm)
|
adamc@53
|
166 end
|
adamc@29
|
167 | L.EApp (e1, e2) =>
|
adamc@29
|
168 let
|
adamc@29
|
169 val (e1, sm) = cifyExp (e1, sm)
|
adamc@29
|
170 val (e2, sm) = cifyExp (e2, sm)
|
adamc@29
|
171 in
|
adamc@29
|
172 ((L'.EApp (e1, e2), loc), sm)
|
adamc@29
|
173 end
|
adamc@109
|
174 | L.EAbs _ => (ErrorMsg.errorAt loc "Anonymous function remains at code generation";
|
adamc@109
|
175 (dummye, sm))
|
adamc@29
|
176
|
adamc@29
|
177 | L.ERecord xes =>
|
adamc@29
|
178 let
|
adamc@29
|
179 val old_xts = map (fn (x, _, t) => (x, t)) xes
|
adamc@29
|
180
|
adamc@29
|
181 val (xets, sm) = ListUtil.foldlMap (fn ((x, e, t), sm) =>
|
adamc@29
|
182 let
|
adamc@29
|
183 val (e, sm) = cifyExp (e, sm)
|
adamc@29
|
184 val (t, sm) = cifyTyp (t, sm)
|
adamc@29
|
185 in
|
adamc@29
|
186 ((x, e, t), sm)
|
adamc@29
|
187 end)
|
adamc@29
|
188 sm xes
|
adamc@29
|
189
|
adamc@29
|
190 val (sm, si) = Sm.find (sm, old_xts, map (fn (x, _, t) => (x, t)) xets)
|
adamc@29
|
191
|
adamc@29
|
192 val xes = map (fn (x, e, _) => (x, e)) xets
|
adamc@29
|
193 val xes = ListMergeSort.sort (fn ((x1, _), (x2, _)) => String.compare (x1, x2) = GREATER) xes
|
adamc@29
|
194 in
|
adamc@29
|
195 ((L'.ERecord (si, xes), loc), sm)
|
adamc@29
|
196 end
|
adamc@29
|
197 | L.EField (e, x) =>
|
adamc@29
|
198 let
|
adamc@29
|
199 val (e, sm) = cifyExp (e, sm)
|
adamc@29
|
200 in
|
adamc@29
|
201 ((L'.EField (e, x), loc), sm)
|
adamc@29
|
202 end
|
adamc@29
|
203
|
adamc@182
|
204 | L.ECase (e, pes, {disc, result}) =>
|
adamc@181
|
205 let
|
adamc@181
|
206 val (e, sm) = cifyExp (e, sm)
|
adamc@181
|
207 val (pes, sm) = ListUtil.foldlMap
|
adamc@181
|
208 (fn ((p, e), sm) =>
|
adamc@181
|
209 let
|
adamc@181
|
210 val (e, sm) = cifyExp (e, sm)
|
adamc@182
|
211 val (p, sm) = cifyPat (p, sm)
|
adamc@181
|
212 in
|
adamc@182
|
213 ((p, e), sm)
|
adamc@181
|
214 end) sm pes
|
adamc@182
|
215 val (disc, sm) = cifyTyp (disc, sm)
|
adamc@182
|
216 val (result, sm) = cifyTyp (result, sm)
|
adamc@181
|
217 in
|
adamc@182
|
218 ((L'.ECase (e, pes, {disc = disc, result = result}), loc), sm)
|
adamc@181
|
219 end
|
adamc@178
|
220
|
adamc@180
|
221 | L.EStrcat (e1, e2) =>
|
adamc@180
|
222 let
|
adamc@180
|
223 val (e1, sm) = cifyExp (e1, sm)
|
adamc@180
|
224 val (e2, sm) = cifyExp (e2, sm)
|
adamc@180
|
225 in
|
adamc@180
|
226 ((L'.EFfiApp ("Basis", "strcat", [e1, e2]), loc), sm)
|
adamc@180
|
227 end
|
adamc@102
|
228
|
adamc@102
|
229 | L.EWrite e =>
|
adamc@102
|
230 let
|
adamc@102
|
231 val (e, sm) = cifyExp (e, sm)
|
adamc@102
|
232 in
|
adamc@102
|
233 ((L'.EWrite e, loc), sm)
|
adamc@102
|
234 end
|
adamc@102
|
235
|
adamc@106
|
236 | L.ESeq (e1, e2) =>
|
adamc@106
|
237 let
|
adamc@106
|
238 val (e1, sm) = cifyExp (e1, sm)
|
adamc@106
|
239 val (e2, sm) = cifyExp (e2, sm)
|
adamc@106
|
240 in
|
adamc@106
|
241 ((L'.ESeq (e1, e2), loc), sm)
|
adamc@106
|
242 end
|
adamc@106
|
243
|
adamc@111
|
244 | L.EClosure _ => (ErrorMsg.errorAt loc "Nested closure remains in code generation";
|
adamc@111
|
245 (dummye, sm))
|
adamc@111
|
246
|
adamc@29
|
247 fun cifyDecl ((d, loc), sm) =
|
adamc@29
|
248 case d of
|
adamc@165
|
249 L.DDatatype (x, n, xncs) =>
|
adamc@165
|
250 let
|
adamc@165
|
251 val (xncs, sm) = ListUtil.foldlMap (fn ((x, n, to), sm) =>
|
adamc@165
|
252 case to of
|
adamc@165
|
253 NONE => ((x, n, NONE), sm)
|
adamc@165
|
254 | SOME t =>
|
adamc@165
|
255 let
|
adamc@165
|
256 val (t, sm) = cifyTyp (t, sm)
|
adamc@165
|
257 in
|
adamc@165
|
258 ((x, n, SOME t), sm)
|
adamc@165
|
259 end) sm xncs
|
adamc@165
|
260 in
|
adamc@165
|
261 (SOME (L'.DDatatype (x, n, xncs), loc), NONE, sm)
|
adamc@165
|
262 end
|
adamc@164
|
263
|
adamc@164
|
264 | L.DVal (x, n, t, e, _) =>
|
adamc@29
|
265 let
|
adamc@29
|
266 val (t, sm) = cifyTyp (t, sm)
|
adamc@109
|
267
|
adamc@109
|
268 val (d, sm) = case #1 t of
|
adamc@121
|
269 L'.TFun _ =>
|
adamc@121
|
270 let
|
adamc@121
|
271 fun unravel (tAll as (t, _), eAll as (e, _)) =
|
adamc@121
|
272 case (t, e) of
|
adamc@121
|
273 (L'.TFun (dom, ran), L.EAbs (ax, _, _, e)) =>
|
adamc@121
|
274 let
|
adamc@121
|
275 val (args, t, e) = unravel (ran, e)
|
adamc@121
|
276 in
|
adamc@121
|
277 ((ax, dom) :: args, t, e)
|
adamc@121
|
278 end
|
adamc@121
|
279 | (L'.TFun _, _) =>
|
adamc@121
|
280 (ErrorMsg.errorAt loc "Function isn't explicit at code generation";
|
adamc@121
|
281 ([], tAll, eAll))
|
adamc@121
|
282 | _ => ([], tAll, eAll)
|
adamc@121
|
283
|
adamc@121
|
284 val (args, ran, e) = unravel (t, e)
|
adamc@121
|
285 val (e, sm) = cifyExp (e, sm)
|
adamc@121
|
286 in
|
adamc@121
|
287 (L'.DFun (x, n, args, ran, e), sm)
|
adamc@121
|
288 end
|
adamc@121
|
289
|
adamc@109
|
290 | _ =>
|
adamc@109
|
291 let
|
adamc@109
|
292 val (e, sm) = cifyExp (e, sm)
|
adamc@109
|
293 in
|
adamc@109
|
294 (L'.DVal (x, n, t, e), sm)
|
adamc@109
|
295 end
|
adamc@29
|
296 in
|
adamc@109
|
297 (SOME (d, loc), NONE, sm)
|
adamc@29
|
298 end
|
adamc@129
|
299 | L.DValRec vis =>
|
adamc@129
|
300 let
|
adamc@129
|
301 val (vis, sm) = ListUtil.foldlMap
|
adamc@129
|
302 (fn ((x, n, t, e, _), sm) =>
|
adamc@129
|
303 let
|
adamc@129
|
304 val (t, sm) = cifyTyp (t, sm)
|
adamc@129
|
305
|
adamc@129
|
306 fun unravel (tAll as (t, _), eAll as (e, _)) =
|
adamc@129
|
307 case (t, e) of
|
adamc@129
|
308 (L'.TFun (dom, ran), L.EAbs (ax, _, _, e)) =>
|
adamc@129
|
309 let
|
adamc@129
|
310 val (args, t, e) = unravel (ran, e)
|
adamc@129
|
311 in
|
adamc@129
|
312 ((ax, dom) :: args, t, e)
|
adamc@129
|
313 end
|
adamc@129
|
314 | (L'.TFun _, _) =>
|
adamc@129
|
315 (ErrorMsg.errorAt loc "Function isn't explicit at code generation";
|
adamc@129
|
316 ([], tAll, eAll))
|
adamc@129
|
317 | _ => ([], tAll, eAll)
|
adamc@129
|
318
|
adamc@129
|
319 val (args, ran, e) = unravel (t, e)
|
adamc@129
|
320 val (e, sm) = cifyExp (e, sm)
|
adamc@129
|
321 in
|
adamc@129
|
322 ((x, n, args, ran, e), sm)
|
adamc@129
|
323 end)
|
adamc@129
|
324 sm vis
|
adamc@129
|
325 in
|
adamc@129
|
326 (SOME (L'.DFunRec vis, loc), NONE, sm)
|
adamc@129
|
327 end
|
adamc@129
|
328
|
adamc@144
|
329 | L.DExport (ek, s, n, ts) =>
|
adamc@120
|
330 let
|
adamc@120
|
331 val (ts, sm) = ListUtil.foldlMap cifyTyp sm ts
|
adamc@120
|
332 in
|
adamc@144
|
333 (NONE, SOME (ek, "/" ^ s, n, ts), sm)
|
adamc@120
|
334 end
|
adamc@29
|
335
|
adamc@29
|
336 fun cjrize ds =
|
adamc@29
|
337 let
|
adamc@101
|
338 val (ds, ps, sm) = foldl (fn (d, (ds, ps, sm)) =>
|
adamc@101
|
339 let
|
adamc@101
|
340 val (dop, pop, sm) = cifyDecl (d, sm)
|
adamc@101
|
341 val ds = case dop of
|
adamc@101
|
342 NONE => ds
|
adamc@101
|
343 | SOME d => d :: ds
|
adamc@101
|
344 val ps = case pop of
|
adamc@101
|
345 NONE => ps
|
adamc@101
|
346 | SOME p => p :: ps
|
adamc@101
|
347 in
|
adamc@101
|
348 (ds, ps, sm)
|
adamc@101
|
349 end)
|
adamc@101
|
350 ([], [], Sm.empty) ds
|
adamc@29
|
351 in
|
adamc@101
|
352 (List.revAppend (map (fn v => (L'.DStruct v, ErrorMsg.dummySpan)) (Sm.declares sm),
|
adamc@101
|
353 rev ds),
|
adamc@101
|
354 ps)
|
adamc@29
|
355 end
|
adamc@29
|
356
|
adamc@29
|
357 end
|