-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpeephole.scm
44 lines (31 loc) · 1.2 KB
/
peephole.scm
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
;;(include "utils/pattern-matching.scm")
(define (peephole code)
(define (peep-optimise acc code)
(match code
;; ((('acc x) ('push 'acc) . rest)
;; (peep-optimise acc (cons `(push ,x) rest)))
;; ((('acc x) ('push 'acc) ('acc y) . rest)
;; (peep-optimise acc (cons `(push ,x) (cons `(acc ,y) rest))))
;; ((('jump x) (instr _) . rest) where (not (eq? instr 'label))
;; (peep-optimise acc (cons `(jump ,x) rest)))
((('acc _) ('acc x) . rest)
(peep-optimise acc (cons `(acc ,x) rest)))
((('restore-state) ('save-state) . rest)
(peep-optimise acc rest))
((('pop 0) . rest)
(peep-optimise acc rest))
((('pop) ('pop) . rest)
(peep-optimise acc (cons `(pop ,2) rest)))
((('pop) ('pop x) . rest)
(peep-optimise acc (cons `(pop ,(+ x 1)) rest)))
((('pop x) ('pop) . rest)
(peep-optimise acc (cons `(pop ,(+ x 1)) rest)))
((('pop x) ('pop y) . rest)
(peep-optimise acc (cons `(pop ,(+ x y)) rest)))
(() acc);;x where (null? x) acc)
((instr . rest)
(peep-optimise (cons instr acc) rest))
(_ (error "peephole"))))
;; (trace peep-optimise)
(reverse (peep-optimise '() code)))
(peephole '((add) (pop) (pop) (call qwf)))