summaryrefslogtreecommitdiff
path: root/compiler.scm
blob: 411fe658cf96628a9375e4a9a8d0e50f831964ab (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
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
(use-modules (ice-9 format)
             (ice-9 popen)
             (ice-9 rdelim)
             (srfi srfi-1))

;; Assuming a 64 bit intel machine here.
(define wordsize 8)
(define fixnum-mask 3)
(define fixnum-shift 2)
(define fixnum-tag 0)
(define char-mask 255)
(define char-shift 8)
(define char-tag 15)
(define boolean-mask 127)
(define boolean-shift 7)
(define boolean-tag 31)
(define empty-list 47) ;; #b00101111

(define (emit template-string . args)
  (apply format #t template-string args)
  (newline))

(define (immediate-rep x)
    (cond
     ((integer? x)
      (ash x fixnum-shift))
     ((char? x)
      (logior (ash (char->integer x) char-shift) char-tag))
     ((boolean? x)
      (logior (ash (if x 1 0) boolean-shift) boolean-tag))
     ((null? x)
      empty-list)))

(define (immediate? x)
  (or (integer? x)
      (char? x)
      (boolean? x)
      (null? x)))

(define (primcall? x)
  (and (pair? x)
       (memq (first x)
             '(add1
               sub1
               integer->char
               char->integer
               zero?
               null?
               integer?
               char?
               boolean?
               + - *
               = < <= > >=
               eq? char=?))))

(define (primcall-op x)
  (first x))

(define (primcall-operand1 x)
  (second x))

(define (primcall-operand2 x)
  (third x))

(define (emit-tag-check x mask tag si)
  (emit-expr x si)
  (emit "andl $~a, %eax" mask)
  (emit "cmpl $~a, %eax" tag)
  (emit "movl $0, %eax")
  (emit "sete %al")
  (emit "sall $~a, %eax" boolean-shift)
  (emit "orl $~a, %eax" boolean-tag))

(define (emit-comparison x y instruction si)
  (emit-expr y si)
  (emit "movl %eax, ~a(%rsp)" si)
  (emit-expr x (- si wordsize))
  (emit "cmpl ~a(%rsp), %eax" si)
  (emit "movl $0, %eax")
  (emit "~a %al" instruction)
  (emit "sall $~a, %eax" boolean-shift)
  (emit "orl $~a, %eax" boolean-tag))

(define (emit-primitive-call x si)
  (case (primcall-op x)
    ((add1)
     (emit-expr (primcall-operand1 x) si)
     (emit "addl $~a, %eax" (immediate-rep 1)))
    ((sub1)
     (emit-expr (primcall-operand1 x) si)
     (emit "subl $~a, %eax" (immediate-rep 1)))
    ((integer->char)
     (emit-expr (primcall-operand1 x) si)
     (emit "sall $~a, %eax" (- char-shift fixnum-shift))
     (emit "orl $~a, %eax" char-tag))
    ((char->integer)
     (emit-expr (primcall-operand1 x) si)
     (emit "shr $~a, %eax" (- char-shift fixnum-shift)))
    ((zero?)
     (emit-expr (primcall-operand1 x) si)
     ;; Since the tag of fixnums is 0, we can skip an 'andl'
     ;; instruction that would apply the mask to the immediate
     ;; value.
     (emit "cmpl $0, %eax")
     (emit "movl $0, %eax")
     (emit "sete %al")
     (emit "sall $~a, %eax" boolean-shift)
     (emit "orl $~a, %eax" boolean-tag))
    ((null?)
     (emit-expr (primcall-operand1 x) si)
     (emit "cmpl $~a, %eax" empty-list)
     (emit "movl $0, %eax")
     (emit "sete %al")
     (emit "sall $~a, %eax" boolean-shift)
     (emit "orl $~a, %eax" boolean-tag))
    ((integer?)
     (emit-tag-check (primcall-operand1 x) fixnum-mask fixnum-tag si))
    ((char?)
     (emit-tag-check (primcall-operand1 x) char-mask char-tag si))
    ((boolean?)
     (emit-tag-check (primcall-operand1 x) boolean-mask boolean-tag si))
    ((+)
     (emit-expr (primcall-operand2 x) si)
     (emit "movl %eax, ~a(%rsp)" si)
     (emit-expr (primcall-operand1 x) (- si wordsize))
     (emit "addl ~a(%rsp), %eax" si))
    ((-)
     (emit-expr (primcall-operand2 x) si)
     (emit "movl %eax, ~a(%rsp)" si)
     (emit-expr (primcall-operand1 x) (- si wordsize))
     (emit "subl ~a(%rsp), %eax" si))
    ((*)
     (emit-expr (primcall-operand2 x) si)
     (emit "movl %eax, ~a(%rsp)" si)
     (emit-expr (primcall-operand1 x) (- si wordsize))
     (emit "imull ~a(%rsp), %eax" si)
     ;; When two fixnums (which have 2 tag bits) are multiplied, the
     ;; relevant bits for the result are now 4 bytes to the left, so
     ;; we have to shift back 2 bytes.
     (emit "shr $~a, %eax" fixnum-shift))
    ((=)
     (emit-comparison (primcall-operand1 x) (primcall-operand2 x) "sete" si))
    ((<)
     (emit-comparison (primcall-operand1 x) (primcall-operand2 x) "setl" si))
    ((<=)
     (emit-comparison (primcall-operand1 x) (primcall-operand2 x) "setle" si))
    ((>)
     (emit-comparison (primcall-operand1 x) (primcall-operand2 x) "setg" si))
    ((>=)
     (emit-comparison (primcall-operand1 x) (primcall-operand2 x) "setge" si))
    ((eq?)
     (emit-comparison (primcall-operand1 x) (primcall-operand2 x) "sete" si))
    ((char=?)
     (emit-comparison (primcall-operand1 x) (primcall-operand2 x) "sete" si))
    (else
     (error "unknown primcall op" (primcall-op x)))))


(define (emit-expr x si)
  (cond
   ((immediate? x)
    (emit "movl $~a, %eax" (immediate-rep x)))
   ((primcall? x)
    (emit-primitive-call x si))
   (else
    (error "unknown expression" x))))

(define (compile-program x)
  (emit-expr x (- wordsize))
  (emit "ret"))

(define (compile-and-run x)
  (with-output-to-file "scheme_entry.S"
    (lambda ()
      (display ".text
.p2align 4
.globl	scheme_entry
.type	scheme_entry, @function
scheme_entry:
")
      (compile-program x)))
  (and (zero? (system* "gcc" "-c" "scheme_entry.S"))
       (zero? (system* "gcc" "-c" "test.c"))
       (zero? (system* "gcc" "-o" "test" "scheme_entry.o" "test.o"))
       (let* ((pipe (open-pipe* OPEN_READ "./test"))
              (output (read-line pipe)))
         (close pipe)
         output)))

(define (test-case x expected-output)
  (let ((result (compile-and-run x)))
    (if (string=? result expected-output)
        #t
        (begin
          (display "expected: ")
          (display expected-output)
          (display ", got: ")
          (display result)
          (newline)
          #f))))

(begin
  (test-case 1 "1")
  (test-case #\b "b")
  (test-case #t "#t")
  (test-case #f "#f")
  (test-case '(add1 3) "4")
  (test-case '(sub1 3) "2")
  (test-case '(integer->char 98) "b")
  (test-case '(char->integer #\b) "98")
  (test-case '(zero? 1) "#f")
  (test-case '(zero? 0) "#t")
  (test-case '(null? 1) "#f")
  (test-case '(null? #\b) "#f")
  (test-case '(null? #t) "#f")
  (test-case '(null? #f) "#f")
  (test-case '(null? ()) "#t")
  (test-case '(integer? #\b) "#f")
  (test-case '(integer? #f) "#f")
  (test-case '(integer? 1) "#t")
  (test-case '(char? 1) "#f")
  (test-case '(char? #f) "#f")
  (test-case '(char? #\b) "#t")
  (test-case '(boolean? 1) "#f")
  (test-case '(boolean? #\b) "#f")
  (test-case '(boolean? #f) "#t")
  (test-case '(boolean? #t) "#t")
  (test-case '(+ 1 2) "3")
  (test-case '(- 3 1) "2")
  (test-case '(* 2 3) "6")
  (test-case '(= 1 2) "#f")
  (test-case '(= 1 1) "#t")
  (test-case '(< 2 1) "#f")
  (test-case '(< 1 2) "#t")
  (test-case '(<= 2 1) "#f")
  (test-case '(<= 1 2) "#t")
  (test-case '(<= 2 2) "#t")
  (test-case '(> 1 2) "#f")
  (test-case '(> 2 1) "#t")
  (test-case '(>= 1 2) "#f")
  (test-case '(>= 2 1) "#t")
  (test-case '(>= 2 2) "#t")
  (test-case '(char=? #\a #\b) "#f")
  (test-case '(char=? #\b #\b) "#t"))