-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanalyze.slog
549 lines (436 loc) · 14 KB
/
analyze.slog
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
;----------------------------------------------------------------------;
;;;;;;;;;;;;;;;;;;;;;;;;;APPENDED PROGRAM ABOVE;;;;;;;;;;;;;;;;;;;;;;;;;
;----------------------------------------------------------------------;
;;;;;;;;;;;;;;;;;;;;;;;;;ANALYSIS PROGRAM BELOW;;;;;;;;;;;;;;;;;;;;;;;;;
;----------------------------------------------------------------------;
;----------------------------------------------------------------------;
;;;;;;;;;;;;;;;;;;;;;;;;::;;;;PRIM TAGGING::;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;----------------------------------------------------------------------;
;NUMERIC-PRIMS
(numeric-prim "+")
(numeric-prim "-")
(numeric-prim "*")
(numeric-prim "/")
(numeric-prim "=")
(numeric-prim "modulo")
;BOOL-PRIMS
(bool-prim "=")
(bool-prim ">")
(bool-prim "<")
(bool-prim "<=")
(bool-prim ">=")
(bool-prim "null?")
(bool-prim "equal?")
(bool-prim "eq?")
;----------------------------------------------------------------------;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;FREE VARIABLES;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;----------------------------------------------------------------------;
;NON-LIST-VALUES
(non-list-value ?(symbol v) (symbol-val v))
(non-list-value ?(bool v) (bool-val v))
(non-list-value ?(int v) (int-val v))
(non-list-value ?(string v) (string-val v))
(non-list-value ?(state v) (state-val v))
; (non-list-value (or (addr _) (quote _) (string _) (bool _) (state _)))
;REF VARIABLES
(free x ?(ref x))
;LAMBDAS
[(free x eb)
(=/= y x)
-->
(free x ?(lambda (varparam y) eb))]
[(free x eb)
-->
(do-free-lam ?(lambda (fixedparam ys) eb) ys x)]
[(do-free-lam lam [y ys ...] x)
(=/= y x)
-->
(do-free-lam lam ys x)]
[(do-free-lam lam [] x)
-->
(free x lam)]
;APPL
[(or (free x ef) (free x ea))
-->
(free x ?(appl ef ea))]
;IF EXPRESSIONS
[(or (free x guard) (free x tExp) (free x fExp))
-->
(free x ?(if guard tExp fExp))]
;APP
[(free x ef)
-->
(free x ?(app ef eas))]
(do-free-app ?(app ef eas) eas)
[(do-free-app app [ea eas ...])
-->
(do-free-app app eas)
[(free x ea)
-->
(free x app)]]
;LET BINDINGS RIGHT HAND SIDES
(do-free-let-rhs ?(let binds body) binds)
[(do-free-let-rhs let [bind binds ...])
-->
(do-free-let-rhs let binds)]
[(do-free-let-rhs let [(binding lhs rhs) binds ...])
(free x rhs)
-->
(free x let)]
;LET BINDINGS LEFT HAND SIDES
[(free x body)
-->
(do-free-let-lhs ?(let binds body) binds x)]
[(do-free-let-lhs let [(binding lhs rhs) binds ...] x)
(=/= lhs x)
-->
(do-free-let-lhs let binds x)]
[(do-free-let-lhs let [] x)
-->
(free x let)]
;----------------------------------------------------------------------;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;START RULE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;----------------------------------------------------------------------;
[(define "brouhaha_main" (fixedparam []) body)
-->
(eval body [] (halt))]
(tick ?(do-tick [] call) [call])
(tick ?(do-tick [x] call) [call]) ; for m = 1 uncomment this, comment out the lines below
; (tick ?(do-tick [x] call) [call x])
; ; ; (tick ?(do-tick [x _] call) [call x]) ; for m = 2, uncomment this, comment out the lines below
; (tick ?(do-tick [x y] call) [call x y])
; (tick ?(do-tick [x y _] call) [call x y]) ;for m = 3, keeping latest 3-calls
;----------------------------------------------------------------------;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EVAL CASES;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;----------------------------------------------------------------------;
;BASE PRIMITIVE TYPES
[(eval e ctx kont)
(= e (symbol val))
-->
(store (f-addr ctx "symbol") (symbol-val val))
(ret (f-addr ctx "symbol") kont)]
[(eval e ctx kont)
(= e (bool val))
-->
(store (f-addr ctx e) (bool-val val))
(ret (f-addr ctx e) kont)]
[(eval e ctx kont)
(= e (int val))
-->
(store (f-addr ctx "int") (int-val val))
(ret (f-addr ctx "int") kont)]
[(eval e ctx kont)
(= e (string val))
-->
(store (f-addr ctx "string") (string-val val))
(ret (f-addr ctx "string") kont)]
[(eval e ctx kont)
(= e (state val))
-->
(store (f-addr ctx "state") (state-val val))
(ret (f-addr ctx "state") kont)]
;FUNCTION AND VARIABLE REFERENCES
[(eval (ref x) ctx kont)
-->
(ret (f-addr ctx x) kont)]
;LAMBDA AND FUNC
[(eval e ctx kont)
(= e (lambda _ _))
-->
(store (f-addr ctx "addr-lam") (clo e ctx))
(ret (f-addr ctx "addr-lam") kont)]
;APP AND APPLY
[(eval call ctx kont)
(= call (app ef [eas ...]))
-->
(eval ef ctx (appk call [] eas ctx kont))]
[(eval call ctx kont)
(= call (appl ef ea))
-->
(eval ef ctx (applak call ea ctx kont))]
;IF CASE
[(eval (if guard tExp fExp) ctx kont)
-->
(eval guard ctx (if-k tExp fExp ctx kont))]
;LET CASE
[(eval call ctx kont)
(= call (let [(binding lhs rhs) bind-rest ...] body))
-->
(eval rhs ctx (letk call body [lhs] [] bind-rest ctx kont))]
;---------------------------------------------------------------------;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;RET CASES;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;---------------------------------------------------------------------;
;LAMBDA AND FUNC
[(ret a (appk call [as ...] [ea0 ea-rest ...] ctx kont))
-->
(eval ea0 ctx (appk call [as ... a] [ea-rest ...] ctx kont))]
[(ret a (appk call [af as ...] [] ctx kont))
(store af vf)
-->
(apply vf (fixedarg [as ... a]) ctx kont call)]
[(ret af (appk call [] [] ctx kont))
(store af vf)
-->
(apply vf (fixedarg []) ctx kont call)]
[(ret af (applak call ea ctx kont))
-->
(eval ea ctx (applfk call af ctx kont))]
[(ret a (applfk call af ctx kont))
(store af vf)
-->
(apply vf (vararg a) ctx kont call)]
[(ret a (k-addr eb))
-->
(ret a {store (k-addr eb)})]
;IF CASES
[(ret ga (if-k tExp fExp ctx kont))
(store ga (bool-val "t"))
-->
(eval tExp ctx kont)]
[(ret ga (if-k tExp fExp ctx kont))
(store ga (bool-val "f"))
-->
(eval fExp ctx kont)]
[(ret (s-addr _) (if-k tExp fExp ctx kont))
-->
(eval tExp ctx kont)
(eval fExp ctx kont)]
;LET CASE
[(ret a (letk call body [xs ...] [as ...] [(binding lhs rhs) bind-rest ...] ctx kont))
-->
(eval rhs ctx (letk call body [xs ... lhs] [as ... a] bind-rest ctx kont))]
[(ret a (letk call body [xs ...] [as ...] [] ctxlet kont))
-->
(= ctxtick {tick !(do-tick ctxlet call)})
(store (k-addr body) kont)
(do-fixed-prop-all ctxtick xs [as ... a])
(eval body ctxtick (k-addr body))
[(free y body)
-->
(store (f-addr ctxtick y) {store (f-addr ctxlet y)})]]
;---------------------------------------------------------------------;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;APPLY CASES;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;---------------------------------------------------------------------;
;DEFINE --> CLOSURE
[(apply (define fname params body) args ctx kont call)
-->
(apply (clo (lambda params body) []) args ctx kont call)]
;DEFINE-PRIM HANDLING (NOT SPECIALIZED)
[(apply (define-prim fname _) _ _ kont _)
(~ specialized-prim fname)
-->
(store (s-addr fname) (state-val fname))
(ret (s-addr fname) kont)]
; [(= e (apply (define-prim fname _) args _ kont _))
; (~ specialized-prim fname)
; -->
; (store (s-addr e) (state-val e))
; (ret (s-addr e) kont)]
;SPECIALIZED PRIMS
(specialized-prim "cons")
[(= e (apply (define-prim "cons" _) (fixedarg [a0 a1]) ctx kont _))
(store a1 (list-of-vals as))
-->
(store (f-addr ctx "addr-cons") (list-of-vals [a0 a1]))
(ret (f-addr ctx "addr-cons") kont)]
[(= e (apply (define-prim "cons" _) (fixedarg [a0 a1]) ctx kont _))
(store a1 val)
(non-list-value _ val)
-->
(store (f-addr ctx "addr-cons") (list-of-vals [a0 a1]))
(ret (f-addr ctx "addr-cons") kont)]
;VARIADIC
[(= e (apply (define-prim "cons" _) (vararg a) ctx kont call))
(store a (list-of-vals as))
-->
(do-store-addr-union (f-addr ctx "addr-cons") as [])
(ret (f-addr ctx "addr-cons") kont)]
;When the next address points to a list-of-vals
[(do-store-addr-union addr [a as ...] [union-as ...])
(store a (list-of-vals under-as))
-->
(do-store-addr-union addr as [a under-as ... union-as ...])]
;When the next address points to a single addr
[(do-store-addr-union addr [a as ...] [union-as ...])
(store a val)
(non-list-value _ val)
-->
(do-store-addr-union addr as [a union-as])]
[(do-store-addr-union addr [] [union-as ...])
-->
(store addr (list-of-vals union-as))]
(specialized-prim "null?")
[(= e (apply (define-prim "null?" _) (fixedarg [a]) ctx kont _))
(store a (list-of-vals as))
(= as [])
-->
(store (f-addr ctx "addr-null?") (bool-val "t"))
(ret (f-addr ctx "addr-null?") kont)]
[(= e (apply (define-prim "null?" _) (fixedarg [a]) ctx kont _))
(store a (list-of-vals [a0 as ...]))
-->
(store (f-addr ctx "addr-null?") (bool-val "t"))
(store (f-addr ctx "addr-null?") (bool-val "f"))
(ret (f-addr ctx "addr-null?") kont)]
[(= e (apply (define-prim "null?" _) (fixedarg [a]) ctx kont _))
(store a (state-val val))
-->
(store (f-addr ctx "addr-null?") (bool-val "t"))
(store (f-addr ctx "addr-null?") (bool-val "f"))
(ret (f-addr ctx "addr-null?") kont)]
[(= e (apply (define-prim "null?" _) (fixedarg [a]) ctx kont _))
(store a (int-val val))
-->
(store (f-addr ctx "addr-null?") (bool-val "t"))
(store (f-addr ctx "addr-null?") (bool-val "f"))
(ret (f-addr ctx "addr-null?") kont)]
(specialized-prim "car")
[(= e (apply (define-prim "car" _) (fixedarg [a]) ctx kont _))
(store a (list-of-vals [_ ... ai _ ...]))
-->
(store (f-addr ctx "addr-car") {store ai})
(ret (f-addr ctx "addr-car") kont)]
(specialized-prim "cdr")
[(= e (apply (define-prim "cdr" _) (fixedarg [a]) ctx kont _))
(store a (list-of-vals as))
-->
(store (f-addr ctx "addr-cdr") (list-of-vals as))
(ret (f-addr ctx "addr-cdr") kont)]
(specialized-prim "list")
[(= e (apply (define-prim "list" _) (fixedarg as) ctx kont _))
(=/= as [])
-->
(store (f-addr ctx "addr-list") (list-of-vals as))
(ret (f-addr ctx "addr-list") kont)]
[(= e (apply (define-prim "list" _) (fixedarg as) ctx kont _))
(= as [])
-->
(store (f-addr ctx "addr-list-null") (list-of-vals as))
(ret (f-addr ctx "addr-list-null") kont)]
[(= e (apply (define-prim "list" _) (vararg a) ctx kont _))
(store a (list-of-vals as))
-->
(store (f-addr ctx "addr-list") (list-of-vals as))
(ret (f-addr ctx "addr-list") kont)]
;; ASK ABOUT DO-TICK FOR PRIMS
;; >> We do not need DO-TICK for prims, because their body needs
;; >> no evaluation, thus require no-context meaning no DO-TICK!
;FIXED ARG
[(apply (clo (lambda (fixedparam [xs ...]) eb) ctxlam) (fixedarg [as ...]) ctxcall kont call)
-->
(store (k-addr eb) kont)
(= ctxtick {tick !(do-tick ctxcall call)})
(do-fixed-prop-all ctxtick xs as)
(eval eb ctxtick (k-addr eb))
[(free y eb)
-->
(store (f-addr ctxtick y) {store (f-addr ctxlam y)})
(store-flow (f-addr ctxtick y) (f-addr ctxlam y))
]]
[(apply (clo (lambda (varparam x) eb) ctxlam) (fixedarg [as ...]) ctxcall kont call)
-->
(store (k-addr eb) kont)
(= ctxtick {tick !(do-tick ctxcall call)})
(store (f-addr ctxtick x) (list-of-vals as))
(eval eb ctxtick (k-addr eb))
[(free y eb)
-->
(store-flow (f-addr ctxtick y) (f-addr ctxlam y))]]
;VARIADIC ARG
[(apply (clo (lambda (varparam x) eb) ctxlam) (vararg a) ctxcall kont call)
-->
(store (k-addr eb) kont)
(= ctxtick {tick !(do-tick ctxcall call)})
(store-flow a (f-addr ctxtick x))
(eval eb ctxtick (k-addr eb))
[(free y eb)
-->
(store-flow (f-addr ctxtick y) (f-addr ctxlam y))]]
[(apply (clo (lambda (fixedparam [xs ...]) eb) ctxlam) (vararg a) ctxcall kont call)
(store a (list-of-vals as))
-->
(store (k-addr eb) kont)
(= ctxtick {tick !(do-tick ctxcall call)})
(do-var-prop-all ctxtick xs as)
(eval eb ctxtick (k-addr eb))
[(free y eb)
-->
(store-flow (f-addr ctxtick y) (f-addr ctxlam y))]]
;----------------------------------------------------------------------;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;HELPER FUNCTIONS;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;----------------------------------------------------------------------;
;DO-VIXED-PROP-ALL
[(do-fixed-prop-all ctxtick [x x-rest ...] [a a-rest ...])
-->
(do-fixed-prop-all ctxtick x-rest a-rest)
(store (f-addr ctxtick x) {store a})
(store-flow a (f-addr ctxtick x))]
;DO-VAR-PROP-ALL
[(do-var-prop-all ctxtick [x x-rest ...] [a a-rest ...])
-->
(do-var-prop-all-h ctxtick [x x-rest ...] [a a-rest ...] [a a-rest ...])]
[(do-var-prop-all-h ctxtick [x x-rest ...] [a a-rest ...] a-old)
-->
(do-var-prop-all-h ctxtick [x x-rest ...] a-rest a-old)
(store-flow a (f-addr ctxtick x))]
[(do-var-prop-all-h ctxtick [x x-rest ...] [] a-old)
-->
(do-var-prop-all-h ctxtick x-rest a-old a-old)]
;STORE-FLOW
[(store a v)
(store-flow a a1)
-->
(store a1 v)]
;DETECTING ANSWER
(answer ?(ret (s-addr a) (halt)) {store (s-addr a)})
(answer ?(ret (f-addr c a) (halt)) {store (f-addr c a)})
;----------------------------------------------------------------------;
;;;;;;;;;;;;;;;;;;;;;;;;;;Inlinable Callsite Detection;;;;;;;;;;;;;;;;;;
;----------------------------------------------------------------------;
; IS DEFINE-PRIM
; [(eval exp _ _)
; (= exp (app (ref func) _))
; (store (f-addr _ func) (define-prim dp_func _))
; (count store (f-addr _ func) (define-prim dp_func _) cnt)
; -->
; (inlinable-call exp func dp_func cnt)]
; [(inlinable-call exp func dp_func cnt)
; -->
; (is-define-prim exp func dp_func {maximum inlinable-call exp func dp_func} {prim-count dp_func})]
; ; Combinator
; [(eval exp _ _)
; (= exp (app lam _))
; (= lam (lambda var body))
; (~ store (f-addr _ x) (define-prim x _))
; (free x lam)
; -->
; (is-combinator exp lam x)]
; GENERATING CALL-SETS FOR FUNCTIONS
[(= e (app ef [eas ...]))
(apply vf _ _ _ e)
-->
(fun-at-call e vf)]
[(fun-at-call e vf)
(count fun-at-call e _ cnt)
-->
(fun-at-call-count e cnt)]
[(free var exp)
-->
(freevars exp var)]
; CONTEXT SENSITIVE ABS. COUNTING
[(store (f-addr ctx var) val)
-->
(temp-abstract-count (store (f-addr ctx var) val) 1)
(abstract-count (f-addr ctx var) 1)]
; under the same context if an address is used again,
; we are bumping up the count to 2
[(temp-abstract-count (store (f-addr ctx var) val1) cnt)
(store (f-addr ctx var) val2)
(= cnt 1)
(=/= val1 val2)
-->
(abstract-count (f-addr ctx var) 2)]
; taking the maximum
[(abstract-count addr cnt)
-->
(final-abstract-count addr {maximum abstract-count addr})]