| 1 |
#!/usr/bin/env ikarus --r6rs-script |
| 2 |
;;; Ikarus Scheme -- A compiler for R6RS Scheme. |
| 3 |
;;; Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum |
| 4 |
;;; |
| 5 |
;;; This program is free software: you can redistribute it and/or modify |
| 6 |
;;; it under the terms of the GNU General Public License version 3 as |
| 7 |
;;; published by the Free Software Foundation. |
| 8 |
;;; |
| 9 |
;;; This program is distributed in the hope that it will be useful, but |
| 10 |
;;; WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 |
;;; General Public License for more details. |
| 13 |
;;; |
| 14 |
;;; You should have received a copy of the GNU General Public License |
| 15 |
;;; along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
;;; vim:syntax=scheme |
| 18 |
(import |
| 19 |
(ikarus.compiler) |
| 20 |
(match) |
| 21 |
(except (ikarus) perform-tag-analysis tag-analysis-output |
| 22 |
cp0-effort-limit cp0-size-limit expand/optimize |
| 23 |
optimizer-output |
| 24 |
optimize-cp optimize-level assembler-output)) |
| 25 |
|
| 26 |
|
| 27 |
(define (compile1 x) |
| 28 |
(let ([p (open-file-output-port "test64.fasl" (file-options no-fail))]) |
| 29 |
(parameterize ([optimize-level 0] |
| 30 |
[assembler-output #t]) |
| 31 |
(compile-core-expr-to-port x p)) |
| 32 |
(close-output-port p))) |
| 33 |
|
| 34 |
(define (compile-and-run x) |
| 35 |
(compile1 x) |
| 36 |
(let ([rs (system "../src/ikarus -b test64.fasl > test64.out")]) |
| 37 |
(unless (= rs 0) (error 'run1 "died with status" rs)) |
| 38 |
(with-input-from-file "test64.out" |
| 39 |
(lambda () |
| 40 |
(let ([s (get-string-all (current-input-port))]) |
| 41 |
(if (eof-object? s) |
| 42 |
"" |
| 43 |
s)))))) |
| 44 |
|
| 45 |
(define (compile-test-and-run expr expected) |
| 46 |
(printf "Compiling:\n") |
| 47 |
(pretty-print expr) |
| 48 |
(let ([val (compile-and-run (fixup expr))]) |
| 49 |
(unless (equal? val expected) |
| 50 |
(error 'compile-test-and-run "failed:got:expected" val expected)))) |
| 51 |
|
| 52 |
(define (test-all) |
| 53 |
(for-each |
| 54 |
(lambda (x) |
| 55 |
(compile-test-and-run (car x) (cadr x))) |
| 56 |
all-tests)) |
| 57 |
|
| 58 |
(define all-tests |
| 59 |
'([(quote 42) "42\n"] |
| 60 |
[(quote #f) "#f\n"] |
| 61 |
[(quote ()) "()\n"])) |
| 62 |
|
| 63 |
(define (self-evaluating? x) |
| 64 |
(or (number? x) (char? x) (boolean? x) (null? x) (string? x))) |
| 65 |
|
| 66 |
(define prims-alist |
| 67 |
'([$fxadd1 $fxadd1] |
| 68 |
[$fxsub1 $fxsub1] |
| 69 |
[$fixnum->char $fixnum->char] |
| 70 |
[$char->fixnum $char->fixnum] |
| 71 |
[fixnum? fixnum?] |
| 72 |
[$fxzero? $fxzero?] |
| 73 |
[null? null?] |
| 74 |
[boolean? boolean?] |
| 75 |
[char? char?] |
| 76 |
[not not] |
| 77 |
[$fxlognot $fxlognot] |
| 78 |
[fx+ $fx+] |
| 79 |
[fx- $fx-] |
| 80 |
[fx* $fx*] |
| 81 |
[fxadd1 $fxadd1] |
| 82 |
[fxsub1 $fxsub1] |
| 83 |
[fxlogor $fxlogor] |
| 84 |
[fxlogand $fxlogand] |
| 85 |
[fxlognot $fxlognot] |
| 86 |
[fx= $fx=] |
| 87 |
[fx< $fx<] |
| 88 |
[fx<= $fx<=] |
| 89 |
[fx> $fx>] |
| 90 |
[fx>= $fx>=] |
| 91 |
[pair? pair?] |
| 92 |
[cons cons] |
| 93 |
[car $car] |
| 94 |
[cdr $cdr] |
| 95 |
[set-car! $set-car!] |
| 96 |
[set-cdr! $set-cdr!] |
| 97 |
[eq? eq?] |
| 98 |
[make-vector $make-vector] |
| 99 |
[vector? vector?] |
| 100 |
[vector-length $vector-length] |
| 101 |
[vector-set! $vector-set!] |
| 102 |
[vector-ref $vector-ref] |
| 103 |
[string? string?] |
| 104 |
[make-string $make-string] |
| 105 |
[string-set! $string-set!] |
| 106 |
[string-ref $string-ref] |
| 107 |
[string-length $string-length] |
| 108 |
[char= $char=] |
| 109 |
[fixnum->char $fixnum->char] |
| 110 |
[char->fixnum $char->fixnum] |
| 111 |
[procedure? procedure?] |
| 112 |
[fxzero? $fxzero?] |
| 113 |
[vector vector] |
| 114 |
[symbol? symbol?] |
| 115 |
[make-bytevector $make-bytevector] |
| 116 |
[bytevector? bytevector?] |
| 117 |
[bytevector-set! $bytevector-set!] |
| 118 |
[bytevector-ref $bytevector-u8-ref] |
| 119 |
[bytevector-length $bytevector-length] |
| 120 |
)) |
| 121 |
|
| 122 |
|
| 123 |
(define (fixup x) |
| 124 |
(define (P x) |
| 125 |
`(primitive ,x)) |
| 126 |
(define (Expr x env) |
| 127 |
(match x |
| 128 |
[,n (guard (self-evaluating? n)) `(quote ,n)] |
| 129 |
[,var (guard (symbol? var)) |
| 130 |
(cond |
| 131 |
[(assq var env) => cdr] |
| 132 |
[else (error 'fixup "unbound var" var)])] |
| 133 |
[(,rator ,[rand*] ...) |
| 134 |
(guard (assq rator env)) |
| 135 |
`(,(Expr rator env) ,rand* ...)] |
| 136 |
[(quote ,x) `(quote ,x)] |
| 137 |
[(,prim ,[args] ...) |
| 138 |
(guard (assq prim prims-alist)) |
| 139 |
`((primitive ,(cadr (assq prim prims-alist))) ,args ...)] |
| 140 |
[(if ,[e0] ,[e1] ,[e2]) |
| 141 |
`(if ,e0 ,e1 ,e2)] |
| 142 |
[(let ([,lhs* ,[rhs*]] ...) ,body ,body* ...) |
| 143 |
(let ([nlhs* (map gensym lhs*)]) |
| 144 |
(let ([env (append (map cons lhs* nlhs*) env)]) |
| 145 |
`((case-lambda |
| 146 |
[,nlhs* |
| 147 |
(begin ,(Expr body env) |
| 148 |
,(map (lambda (x) (Expr x env)) body*) |
| 149 |
...)]) |
| 150 |
,rhs* ...)))] |
| 151 |
[(letrec ([,lhs* ,rhs*] ...) ,body ,body* ...) |
| 152 |
(let* ([nlhs* (map gensym lhs*)] |
| 153 |
[env (append (map cons lhs* nlhs*) env)] |
| 154 |
[E (lambda (x) (Expr x env))]) |
| 155 |
`(letrec ([,nlhs* ,(map E rhs*)] ...) |
| 156 |
(begin |
| 157 |
,(E body) |
| 158 |
,(map E body*) ...)))] |
| 159 |
[(letrec* ([,lhs* ,rhs*] ...) ,body ,body* ...) |
| 160 |
(let* ([nlhs* (map gensym lhs*)] |
| 161 |
[env (append (map cons lhs* nlhs*) env)] |
| 162 |
[E (lambda (x) (Expr x env))]) |
| 163 |
`(letrec* ([,nlhs* ,(map E rhs*)] ...) |
| 164 |
(begin |
| 165 |
,(E body) |
| 166 |
,(map E body*) ...)))] |
| 167 |
[(lambda (,fml* ...) ,body ,body* ...) |
| 168 |
(let ([g* (map gensym fml*)]) |
| 169 |
(let ([env (append (map cons fml* g*) env)]) |
| 170 |
`(case-lambda |
| 171 |
[(,g* ...) |
| 172 |
(begin ,(Expr body env) |
| 173 |
,(map (lambda (x) (Expr x env)) body*) |
| 174 |
...)])))] |
| 175 |
[(lambda (,fml* ... . ,fml) ,body ,body* ...) |
| 176 |
(let ([g* (map gensym fml*)] |
| 177 |
[g (gensym fml)]) |
| 178 |
(let ([env (append (map cons |
| 179 |
(cons fml fml*) |
| 180 |
(cons g g*)) |
| 181 |
env)]) |
| 182 |
`(case-lambda |
| 183 |
[(,g* ... . ,g) |
| 184 |
(begin ,(Expr body env) |
| 185 |
,(map (lambda (x) (Expr x env)) body*) |
| 186 |
...)])))] |
| 187 |
[(begin ,[e] ,[e*] ...) |
| 188 |
`(begin ,e ,e* ...)] |
| 189 |
[(and) ''#t] |
| 190 |
[(and ,[e]) e] |
| 191 |
[(and ,[e] ,e* ...) |
| 192 |
`(if ,e ,(Expr `(and ,e* ...) env) (quote #f))] |
| 193 |
[(or) ''#f] |
| 194 |
[(or ,[e]) e] |
| 195 |
[(or ,[e] ,e* ...) |
| 196 |
(let ([t (gensym)]) |
| 197 |
`((case-lambda |
| 198 |
[(,t) |
| 199 |
(if ,t ,t ,(Expr `(or ,e* ...) env))]) |
| 200 |
,e))] |
| 201 |
[(when ,[e] ,[b] ,[b*] ...) |
| 202 |
`(if ,e |
| 203 |
(begin ,b ,b* ...) |
| 204 |
(,(P 'void)))] |
| 205 |
[(unless ,[e] ,[b] ,[b*] ...) |
| 206 |
`(if (,(P 'not) ,e) |
| 207 |
(begin ,b ,b* ...) |
| 208 |
(,(P 'void)))] |
| 209 |
[(cond) `(,(P 'void))] |
| 210 |
[(cond ,cls* ... ,cls) |
| 211 |
(let () |
| 212 |
(define (fst-clause x rest) |
| 213 |
(match x |
| 214 |
[(,e ,arr ,v) |
| 215 |
(guard (and (eq? arr '=>) (not (assq arr env)))) |
| 216 |
(let ([t (gensym)]) |
| 217 |
`((case-lambda |
| 218 |
[(,t) |
| 219 |
(if ,t (,(Expr v env) ,t) ,rest)]) |
| 220 |
,(Expr e env)))] |
| 221 |
[(,e) |
| 222 |
(let ([t (gensym)]) |
| 223 |
`((case-lambda |
| 224 |
[(,t) |
| 225 |
(if ,t ,t ,rest)]) |
| 226 |
,(Expr e env)))] |
| 227 |
[(,e ,e* ...) |
| 228 |
`(if ,(Expr e env) |
| 229 |
(begin ,(map (lambda (x) (Expr x env)) e*) ...) |
| 230 |
,rest)] |
| 231 |
[,others (error 'cond "invalid clause" others)])) |
| 232 |
(define (last-clause x) |
| 233 |
(match x |
| 234 |
[(,els ,e ,e* ...) |
| 235 |
(guard (and (eq? els 'else) (not (assq els env)))) |
| 236 |
`(begin ,(Expr e env) |
| 237 |
,(map (lambda (x) (Expr x env)) e*) |
| 238 |
...)] |
| 239 |
[,others |
| 240 |
(fst-clause others `(,(P 'void)))])) |
| 241 |
(let f ([cls* cls*] [ac (last-clause cls)]) |
| 242 |
(cond |
| 243 |
[(null? cls*) ac] |
| 244 |
[else (fst-clause (car cls*) |
| 245 |
(f (cdr cls*) ac))])))] |
| 246 |
[(set! ,x ,[v]) |
| 247 |
(cond |
| 248 |
[(assq x env) => (lambda (p) `(set! ,(cdr p) ,v))] |
| 249 |
[else (error 'fixup "unbound" x)])] |
| 250 |
[(foreign-call ,str ,[arg*] ...) |
| 251 |
(guard (string? str)) |
| 252 |
`(foreign-call ',str ,arg* ...)] |
| 253 |
[(,[rator] ,[rand*] ...) `(,rator ,rand* ...)] |
| 254 |
[,_ (error 'fixup "invalid expression" _)])) |
| 255 |
(Expr x '())) |
| 256 |
|
| 257 |
(define-syntax add-tests-with-string-output |
| 258 |
(lambda (x) |
| 259 |
(syntax-case x (=>) |
| 260 |
[(_ name [test => string] ...) |
| 261 |
#'(set! all-tests |
| 262 |
(append all-tests |
| 263 |
'([test string] ...)))]))) |
| 264 |
|
| 265 |
(begin |
| 266 |
(include "tests/tests-1.1-req.scm") |
| 267 |
(include "tests/tests-1.2-req.scm") |
| 268 |
(include "tests/tests-1.3-req.scm") |
| 269 |
(include "tests/tests-1.4-req.scm") |
| 270 |
(include "tests/tests-1.5-req.scm") |
| 271 |
(include "tests/tests-1.6-req.scm") |
| 272 |
(include "tests/tests-1.7-req.scm") |
| 273 |
(include "tests/tests-1.8-req.scm") |
| 274 |
(include "tests/tests-1.9-req.scm") |
| 275 |
(include "tests/tests-2.1-req.scm") |
| 276 |
(include "tests/tests-2.2-req.scm") |
| 277 |
(include "tests/tests-2.3-req.scm") |
| 278 |
(include "tests/tests-2.4-req.scm") |
| 279 |
(include "tests/tests-2.6-req.scm") |
| 280 |
(include "tests/tests-2.8-req.scm") |
| 281 |
(include "tests/tests-2.9-req.scm") |
| 282 |
) |
| 283 |
|
| 284 |
|
| 285 |
(current-primitive-locations |
| 286 |
(lambda (x) |
| 287 |
(define prims |
| 288 |
'(do-overflow |
| 289 |
do-vararg-overflow |
| 290 |
error |
| 291 |
$do-event |
| 292 |
$apply-nonprocedure-error-handler |
| 293 |
$incorrect-args-error-handler |
| 294 |
$multiple-values-error)) |
| 295 |
(cond |
| 296 |
[(memq x prims) x] |
| 297 |
[else (error 'current-primloc "invalid" x)]))) |
| 298 |
|
| 299 |
|
| 300 |
(test-all) |
| 301 |
(printf "Passed ~s tests\n" (length all-tests)) |
| 302 |
(printf "Happy Happy Joy Joy\n") |
| 303 |
|