summaryrefslogtreecommitdiff
path: root/fibo.lisp
blob: 3beae63327bb85063c270a420811f27809fe43e0 (plain)
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
(define fibo1 (n)
	(if (<= n 0) 0
		(if (<= n 2) 1
			(+ (fibo1 (- n 1)) (fibo1 (- n 2))))))

; == main lambda ==
; Params: 1
; B0:
;   t0 <- param 0
;   t1 <- callf "<=" [t0 0]
;   br t1 B1 B2
; B1:
;   t2 <- assign 0
;   jmp B5
; B2:
;   t3 <- param 0
;   t4 <- callf "<=" [t3 2]
;   br t4 B3 B4
; B3:
;   t2 <- assign 1
;   jmp B5
; B4:
;   t5 <- param 0
;   t6 <- callf "-" [t5 1]
;   t7 <- callf "fibo1" [t6]   ; patched up (statically known address, afterwards)
;   t8 <- param 0
;   t9 <- callf "-" [t8 2]
;   t10 <- callf "fibo1" [t9]   ; patched up
;   t2 <- callf "+" [t7 t10]
;   jmp B5
; B5:
;   return t2


(define fibo2help (a b n)
	(if (<= n 0) b
		(fibo2help b (+ a b) (- n 1))))

(define fibo2 (n)
	(if (<= n 0) 0
		(if (<= n 2) 1
			(fibo2help 1 1 (- n 2)))))


(define fibo3 (n)
	((lambda (helper)
			(if (<= n 0) 0
				(if (<= n 2) 1
					(helper helper 1 1 (- n 2)))))
		(lambda (recur a b n)
			(if (<= n 0) b
				(recur recur b (+ a b) (- n 1))))))

; == L1 ==
; Params: 1
; Closure slots: 1
; B0:
;   t0 <- closure 0
;   t1 <- callf "<=" [t0 0]
;   br t1 B1 B2
; B1:
;   t2 <- assign 0
;   jmp B5
; B2:
;   t3 <- closure 0
;   t4 <- callf "<=" [t3 2]
;   br t4 B3 B4
; B3:
;   t2 <- assign 1
;   jmp B5
; B4:
;   t5 <- param 0
;   t6 <- param 0
;   t7 <- closure 0
;   t8 <- callf "-" [t7 2]
;   t2 <- callc t5 [t6 1 1 t8]
;   jmp B5
; B5:
;   return t2
; 
; == L2 ==
; Params: 4
; B0:
;   t0 <- param 3
;   t1 <- callf "<=" [t0 0]
;   br t1 B1 B2
; B1:
;   t2 <- param 2
;   jmp B3
; B2:
;   t3 <- param 0
;   t4 <- param 0
;   t5 <- param 2
;   t6 <- param 1
;   t7 <- param 2
;   t8 <- callf "+" [t6 t7]
;   t9 <- param 3
;   t10 <- callf "-" [t9 1]
;   t2 <- callc t3 [t4 t5 t8 t10]
;   jmp B3
; B3:
;   return t2
; 
; == main lambda ==
; Params: 1
; B0:
;   t0 <- param 0
;   t1 <- alloc-closure L1 [t0]
;   t2 <- callc t1 [C(L2)]
;   free-closure t1
;   return t2


(define for (start end f)
	(if (<= start end)
		(do (f start) (for (+ start 1) end f))
		'()))

; == main lambda ==
; Params: 3
; Data table:
;   0: ()
; B0:
;   t0 <- param 0
;   t1 <- param 1
;   t2 <- callf "<=" [t0 t1]
;   br t2 B1 B2
; B1:
;   t3 <- param 2
;   t4 <- param 0
;   _ <- callc t3 [t4]
;   t5 <- param 0
;   t6 <- callf "+" [t5 1]
;   t7 <- param 1
;   t8 <- param 2
;   t9 <- callf "for" [t6 t7 t8]   ; patched up
;   jmp B3
; B2:
;   t9 <- data 0
;   jmp B3
; B3:
;   return t9

(for 1 25 (lambda (n) (print (fibo3 n))))

; == L1 ==
; Params: 1
; B0:
;   t0 <- param 0
;   t1 <- callf "fibo3" [t0]
;   t2 <- callf "print" [t1]
;   return t2
; 
; == global code ==
; B0:
;   _ <- callf "for" [1 25 C(L1)]