This repository was archived by the owner on Nov 1, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGcode.m
412 lines (356 loc) · 10.7 KB
/
Gcode.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
module
#include "../expr/id_t.t"
#include "../expr/id.t"
#include "../expr/ttype.t"
#include "../expr/einfo_t.t"
#include "../expr/constr_t.t"
#include "../expr/constrfun.t"
#include "../ExprE/Eprint.t"
#include "../ExprE/Expr_t.t"
#include "../ExprE/Eutil.t"
#include "../misc/flags.t"
#include "../misc/misc.t"
#include "../funnos.h"
#include "Gcodedef_t.t"
#include "Gmode_t.t"
#include "Gseq.t"
#include "Genv.t"
#include "Gcase.t"
#include "Gconst.t"
#include "Gtail.t"
#include "Gutil.t"
#include "Gid.t"
#include "Gprint.t"
export G;
rec
G _ (e as Eidapl i _) _ _ _ _ & (id_ismethod i) =
fail ("method application "@pr e)
|| G _ (e as Eidapl (mkid _ _ (idi_var (var_dict _) _ _) _) (_._)) _ _ _ _ =
fail ("dictionary application "@pr e)
|| G mode (Ecase e pl de) r n fl fs = Gcase mode e pl de r n fl fs
|| G mode (Efailmatch _) _ _ Notalabel _ = fail "G,failmatch,notalabel"
|| G mode (Efailmatch _) r n fl fs = Gseq [ Gpop (n-fs); Gi [JMP fl] ]
|| G mode (ee as Elet re d e) r n fl fs =
let (r2,n2) = addenv r (map (\(i,_).i) d) n in
let m = n2-n in
if re then
#if 0
if exists (isvar o snd) d then
fail ("G:black hole "@pr ee)
else
#else
-- replace a single variable with an application of the identity function to it
let d = mapsnd (\ e . if isvar e then Eidapl identityfun [e] else e) d in
#endif
(\t.
let (Gs, t') = Gseql (map (\(_,e). G (C n2) e r2 n2 Notalabel 0) d) t
in Gseq [ (if RecBlock & all cleanG Gs then Gi [RECBLOCK Gs] else flatrecblock Gs m);
G mode e r2 n2 fl fs
] t'
)
#if 0
Gseq [ Gi [ ALLOC m ];
Gseq (map2 (\(_,e).\x.
Gseq [ G (C n2) e r2 n2 Notalabel 0;
Gi [ UPDATE ??? (m-x) ]])
d (from 0));
G mode e r2 n2 fl fs
]
#endif
else
Gseq [ Gseq (map2 (\(_,e).\x.G (C(n+x)) e r (n+x) Notalabel 0) d (from 0));
G mode e r2 n2 fl fs
]
|| G m (Einfo vectordef e) r n fl fs =
Gseq [ Gi [ ANNOT "VECTORDEF" ];
G m e r n fl fs
]
|| G m (Einfo (spark is) e) r n fl fs = G m e r n fl fs
|| G m (Einfo (doeval is) e) r n fl fs =
Gseq [ Gdoeval is r n;
G m e r n fl fs
]
|| G R e r n fl fs & (isconstant r e & ConstReturnCaf) =
Gseq [ Gi [ CONSTBLOCK (Gconst e) ];
Gslide n;
Gi [
EVAL [];
UPDATE Gbother 1;
UNWIND
]
]
|| G R (Einfo (noarrow t) (Einfo noeval (Eidapl i []))) r n fl fs =
Gseq [ pushvar i r n 0;
Gi [ UPDATE (updtype t) 1; RET ]
]
|| G R (Einfo noeval (Einfo (noarrow t) (Eidapl i []))) r n fl fs =
Gseq [ pushvar i r n 0;
Gi [ UPDATE (updtype t) 1; RET ]
]
|| G R (Einfo (noarrow t) (Eidapl i [])) r n fl fs =
Gseq [ pushvar i r n 0;
if gnoargs i <= 0 then
Gi [ EVAL [] ]
else
Gi [];
Gi [ UPDATE (updtype t) 1; RET ]
]
|| G R (Einfo noeval (Eidapl i [])) r n fl fs =
Gseq [ pushvar i r n 0;
Gi [ UPDATE Gbother 1; UNWIND ]
]
|| G R (Eidapl i []) r n fl fs =
Gseq [ pushvar i r n 0;
if gnoargs i <= 0 then
Gi [ EVAL [] ]
else
Gi [];
Gi [ UPDATE Gbother 1; UNWIND ]
]
|| G R (Eidapl (mkid Fseq _ _ _) [e1;e2]) r n fl fs =
Gseq [ G (E n) e1 r n fl fs;
Gi [ POP 1 ];
G R e2 r n fl fs
]
|| G R (Eidapl (id as mkid Fleftpat _ _ _) [Econstr c1 el1; Econstr c2 el2]) r n fl fs =
let nel = length el1 in
let is_global (Eidapl id []._) = id_is_global id
|| is_global _ = false in
Gseq [ Gcpushrev (el1 @ el2) r n;
if is_global el2 then
Gi (conc (rept nel [ PUSHGLOBAL identityfun;
MKAP (idtopstr identityfun);
UPDATE Gbother nel
]))
else
Gi (conc (rept nel [ UPDATEINDIR nel ]));
Gi [POP (nel+n); PUSH 1; EVAL []; UPDATE Gbother 1; RET]
]
|| G R (Eidapl (id as mkid idnr _ _ _) [e]) r n fl fs & (idnr >= PBSELBASE) =
Gseq [ Gi [PUSH 0]; -- To get the stack set up for leftpat
G (C n) e r n fl fs;
Gi [EVAL []; UPDATE Gbother 1; UNWIND]
]
-- All metcalls are considered to have the correct arity
|| G R (Einfo metcall (Eidapl i el)) r n fl fs =
Gseq [ Gtail2 el r n;
Gi [ JGLOBAL (gnoargs i) i ]
]
|| G R (Einfo vecreg2 e) r n fl fs =
Gseq [ Gi [ ANNOT "REG2";
POP 1 ];
G R e r (n-1) fl fs
]
#if 1
|| G R (e as Eidapl mi el) r n fl fs & (InlineCallMet & iscallmethod mi) =
case el in
(e.(ec as Econstr c _).es) :
Gseq [ Gtail2 (e.es) r n;
Gi [ EVAL [];
SPLITPAIR true true;
JMETHOD (length es) (cno c) ]
]
|| _ : fail ("G-callmethod "@pr e)
end
#endif
|| G R (Eidapl i el) r n fl fs & (~id_is_predef i) =
let nel = length el in
let na = gnoargs i in
if na = nel then
Gseq [ Gtail2 el r n;
Gi [ JGLOBAL nel i ]
]
else if na = -1 | na > nel then
Gseq [ Gtail (Eidapl i [].el) r n;
Gi [ JFUN nel ]
]
else -- too many args supplied
#if 0
let d = nel-na in
Gseq [ Gtail2 el r n;
Gi [ ALLOC 1 ];
Gi (rept (d+1) (PUSH (nel+1)));
Gi [ PUSH (d+1) ];
Gi (conc (rept d [ MKAP; PUSH 0; MOVE (nel+4) ]));
Gi [ UPDATE Gbother 1; POP 1; MOVE (na+1) ];
Gi [ JGLOBAL na i ]
]
#else
Gseq [ Gtail (Eidapl i [].el) r n;
Gi [ JFUN nel ]
]
#endif
|| G R (Einfo _ e) r n fl fs = G R e r n fl fs
|| G R e r n fl fs =
Gseq [ G (E n) e r n fl fs;
Gi [ UPDATE Gbother (n+1); POP n; UNWIND ]
]
|| G (E s) (Eidapl (mkid Fseq _ _ _) [e1;e2]) r n fl fs =
Gseq [ G (E s) e1 r n fl fs;
Gi [ POP 1 ];
G (E s) e2 r n fl fs
]
|| G (E s) (Eidapl (mkid ix _ _ _) [e1;e2]) r n fl fs & (ix = Frindex | ix = Frindexu) =
Gseq [ G (E n) e1 r n fl fs;
G (B (n+1) Gbint) e2 r (n+1) fl fs;
Gi ([ BASICOP Gbint Gbother INDEX ] @ if ix = Frindex then [ EVAL [] ] else []);
Gslide (n-s)
]
|| G (E s) (e as Eidapl (mkid pn _ (idi_var (var_pre _) _ _) _) el) r n fl fs =
let p = prebasicconstr pn in
let a = prebasicarg pn in
Gseq [ G (B s a) e r n Notalabel 0;
Gi [ BCONSTR p ]
]
|| G (E s) (Einfo noeval (Eidapl i [])) r n fl fs =
pushvar i r n s
|| G (E s) (Eidapl i []) r n fl fs =
Gseq [ pushvar i r n s;
Gi [ EVAL [] ];
updstack i r s
]
#if 1
|| G (E s) (e as Eidapl mi el) r n fl fs & (InlineCallMet & iscallmethod mi) =
case el in
(e.(ec as Econstr c _).es) :
Gseq [ Gi [ ALLOC 1 ];
Gcpushrev (e.es) r (n+1);
Gi [ EVAL [];
SPLITPAIR true true;
CALLMETHOD (length es) (cno c) [] ];
Gslide (n-s)
]
|| _ : fail ("G-callmethod "@pr e)
end
#endif
|| G (E s) (Eidapl i el) r n fl fs & (~id_is_predef i) =
let k = length el in
if gnoargs i = k then
Gseq [ Gi [ ALLOC 1 ];
Gcpushrev el r (n+1);
Gi [ CALLGLOBAL k i [] ];
Gslide (n-s)
]
else
Gseq [ Gi [ ALLOC 1 ];
Gcpushrev (Eidapl i [].el) r (n+1);
Gi [ CALLFUN k [] ];
Gslide (n-s)
]
|| G (E s) (Econstr c el) r n fl fs =
G (C s) (Econstr c el) r n fl fs
|| G (E s) (Einfo _ e) r n fl fs = G (E s) e r n fl fs
|| G (j as (J s ls)) (Einfo strict e) r n fl fs = G j e r n fl fs
|| G (J s ls) (e as Econstr c []) r n fl fs =
case assocdef (cno c) ls (-1,Notalabel) in
(-1,Notalabel) : G (E s) e r n fl fs
|| (k, l) :
Gseq[ Gpop (n-s);
Gi [ JMP l ]
]
end
|| G (J s ls) e r n fl fs =
G (E s) e r n fl fs
|| G (B s _) (Eidapl (mkid Fcno _ _ _) [e1]) r n fl fs =
Gseq [ G (E n) e1 r n fl fs;
Gi [ GETTAG ];
Gpop (n-s)
]
|| G (B s _) (Eidapl (mkid pn _ _ _) [e1;e2]) r n fl fs & (bigop pn) =
Gseq [ G (E n) e1 r n fl fs;
G (E (n+1)) e2 r (n+1) fl fs;
Gi [ BIGOP Gbother (prebasicop pn) ];
Gpop (n-s)
]
|| G (B s _) (Eidapl (mkid pn _ _ _) [e1;e2]) r n fl fs & (lstrop pn) =
Gseq [ G (E n) e1 r n fl fs;
G (E (n+1)) e2 r (n+1) fl fs;
Gi [ BIGOP Gbstring (prebasicop pn) ];
Gpop (n-s)
]
|| G (B s _) (Eidapl (mkid pn ps (idi_var (var_pre _) _ _) _) el) r n fl fs & (pn ~= Frindex & pn ~= Frindexu ) =
let p = prebasicconstr pn in
let a = prebasicarg pn in
Gseq [ Gseq (map (\e.G (B n a) e r n Notalabel 0) el);
Gi [ BASICOP a p (prebasicop pn) ];
Gpop (n-s)
]
|| G (B s _) (Econstr c []) r n fl fs =
Gseq [ Gi [ PUSHBASIC (if isdfloat c then (GvDFloat (stof (cname c)))
else if issfloat c then (GvSFloat (stof (cname c)))
else (GvInt (cno c))) ];
Gpop (n-s)
]
|| G (B s p) (Einfo noeval (Eidapl i [])) r n fl fs =
Gseq [ pushvar i r n s;
Gi [ GET p ]
]
|| G (B s p) (Eidapl i []) r n fl fs =
Gseq [ pushvar i r n s;
Gi [ EVAL [] ];
updstack i r s;
Gi [ GET p ]
]
|| G (B s p) (Einfo _ e) r n fl fs = G (B s p) e r n fl fs
|| G (B s p) e r n fl fs =
Gseq [ Gi [ AMODE Astack ];
G (E s) e r n fl fs;
Gi [ AMODE Aheap; GET p ]
]
|| G (C s) e r n fl fs & (isconstant r e & ConstCaf) =
Gseq [ Gi [CONSTBLOCK (Gconst e)];
Gslide (n-s)
]
|| G (C s) (Einfo strict e) r n fl fs =
-- switch to strict evaluation
G (E s) e r n fl fs
|| G (C s) (Eidapl i []) r n fl fs =
pushvar i r n s
|| G (C s) (Eidapl (id as mkid idnr _ _ _) el) r n fl fs & (idnr >= PBSELBASE) =
G (C s) (Eidapl pbselfun el) r n fl fs
|| G (C s) (e as Eidapl (mkid _ _ (idi_method _ _ _) _) _) r n fl fs =
fail ("Construct method call "@pr e)
|| G (C s) (Eidapl i el) r n fl fs & (~id_is_predef i) =
let lel = length el in
let ar = gnoargs i in
if Stingy & ar = lel & ~ all Eisconst el then
Gseq [ Gcpushrev el r n;
Gi [ SCALLGLOBAL ar i ]
]
else
if ar ~= -1 & ar ~= 0 & ar <= lel & ~NoVector then
Gseq [ Gcpushrev el r n;
Gi [ MKAPLV i ar ];
Gi (rept (lel-ar) (MKAP (idtopstr i)));
Gslide (n-s)
]
else
Gseq [ Gcpushrev (Eidapl i [].el) r n;
Gi (rept lel ((if ar > 0 & lel < ar then MKCAP else MKAP) (idtopstr i)));
Gslide (n-s)
]
|| G (C s) (Econstr c el) r n fl fs =
Gseq [ Gcpushrev el r n;
Gi [ CONSTR (cname c, typenamefromtinfo (ctinfo c)) (constrtype c) (cno c) (length el)];
Gslide (n-s)
]
|| G (C _) (Ecfunction b i) r n fl fs = Gi [ PUSHCFUNCTION b (idtostr i) ]
|| G mode (Einfo _ e) r n fl fs =
G mode e r n fl fs
|| G _ (Eidapl (id as mkid idnr _ _ _) [e]) r n fl fs & (idnr >= PBSELBASE) = fail "PBsel not in C scheme"
|| G _ (Eidapl (id as mkid Fleftpat _ _ _) el) r n fl fs = fail "leftpat not in the R compilation scheme"
|| G _ e _ _ _ _ = fail ("Gcode "@pr e)
and flatrecblock Gs m =
Gseq [ Gi [ ALLOC m ];
Gi (concmap2 (\g.\x.g @ [ UPDATE Gbother (m-x) ]) Gs (from 0)) ]
and isvar (Eidapl _ []) = true
|| isvar (Einfo _ e) = isvar e
|| isvar _ = false
and iscallmethod i = let n = id_no i in n > Fcallmethod & n < Fcallmethod + 50
and updtype t =
if eqtype t Tint then Gbint
else if eqtype t Tchar then Gbchar
else if eqtype t Tdfloat then Gbdfloat
else if eqtype t Tsfloat then Gbsfloat
else Gbother
end