-
Notifications
You must be signed in to change notification settings - Fork 1
/
espresso.scm
55 lines (45 loc) · 1.51 KB
/
espresso.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
45
46
47
48
49
50
51
52
53
54
(load "lib/constants.scm")
(load "lib/types.scm")
(load "lib/assemble.scm")
(load "lib/find-externs.scm")
(load "lib/utils.scm")
(load "tests.scm")
(define passes)
(define-syntax enable-passes
(syntax-rules ()
((_ pass* ...)
(begin
(for-each (lambda (pass)
(load (format "passes/~a.scm" pass)))
'(pass* ...))
(set! passes (list 'pass* ...))))))
(enable-passes normalize-program
normalize-fn-pointer
normalize-predicate
normalize-immediate
)
(define apply-passes
(lambda (expr)
(let loop ((passes passes)
(expr expr))
(if (null? passes)
expr
(loop (cdr passes) ((eval (car passes)) expr))))))
(define compile-program
(lambda (expr . opts)
(let ((name (if (null? opts) 'tmp (car opts))))
(assemble name (apply-passes expr))
(system (format "llvm-as -f -o=~a.o.bc ~a.ll" name name))
(system (format "llvm-link -f -o=~a.bc ~a.o.bc lib/espresso.o.bc" name name))
(run-program name))))
(define compile-c-program
(lambda (name)
(system (format "clang -emit-llvm -c -o ~a.o.bc ~a.c" name name))
;; also generate IR representation for debugging
(system (format "clang -emit-llvm -S -o ~a.ll ~a.c" name name))
(system (format "llvm-link -f -o=~a.bc ~a.o.bc lib/espresso.o.bc" name name))
(run-program name)))
(define run-program
(lambda (name)
(let ((proc (process (format "lli ~a.bc" name))))
(read (car proc)))))