forked from samirose/sicp-compiler-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
module-compiler.sls
147 lines (138 loc) · 4.74 KB
/
module-compiler.sls
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
#!r6rs
(library
(module-compiler)
(export compile-r7rs-library-to-wasm-module
compile-single-exp-to-wasm-module)
(import (rnrs base)
(rnrs lists)
(lists)
(scheme-syntax)
(scheme-r7rs-syntax)
(compilation-error)
(lexical-env)
(compiled-program)
(wasm-syntax)
(pattern-match)
(expression-compiler))
;;;; SCHEME to WAT (WebAssembly Text format) compiler written in R6RS
;;;; BASED ON COMPILER FROM SECTION 5.5 OF
;;;; STRUCTURE AND INTERPRETATION OF COMPUTER PROGRAMS
(define (compile-r7rs-library-to-wasm-module exp)
(if (not (r7rs-library? exp))
(raise-compilation-error "Invalid R7RS library" exp))
(raise-if-error (check-library-declarations exp))
(compile-program-to-module (compile-library-to-program exp)))
(define (compile-single-exp-to-wasm-module exp)
(let ((library
(if (r7rs-library? exp)
exp
(let ((sequence (if (pattern-match? `(begin ,??*) exp) exp `(begin ,exp))))
`(define-library ,sequence)))))
(compile-r7rs-library-to-wasm-module library)))
(define (compile-library-to-program library)
(let*
((exp-sequence
(let*
((exps
(cond ((library-declaration 'begin library))
(else (raise-compilation-error "No begin declaration in library" library))))
(actions (cdr exps)))
(if (null? actions)
(raise-compilation-error "Empty begin declaration in library" library)
actions)))
(definitions-and-non-definitions
(partition-list definition? exp-sequence))
(definitions
(car definitions-and-non-definitions))
(non-definitions
(cdr definitions-and-non-definitions))
(definition-names
(map definition-variable definitions))
(exports
(library-declarations 'export library))
(lexical-env
(make-global-lexical-env definition-names exports))
(program
(make-empty-compiled-program))
(program
(if (null? definitions)
program
(compile-values
definitions
program
lexical-env compile)))
(global-init-code
(compiled-program-value-code
program))
(program
(if (null? global-init-code)
program
(let ((global-init-func-index
(compiled-program-definitions-count program 'func)))
(compiled-program-with-definitions-and-value-code
program
`((func ,@(wasm-local-definitions-to-top global-init-code))
(start ,global-init-func-index))
'()))))
(program
(if (null? non-definitions)
program
(compile-proc-to-func
"$main"
'()
non-definitions
program
lexical-env
"main"
compile)))
(elem-defs-count
(compiled-program-definitions-count program 'elem))
(program
(if (= elem-defs-count 0)
program
(compiled-program-add-definition
program
`(table ,elem-defs-count funcref)))))
program))
(define (compile-program-to-module program)
(let*
((elem-defs
(compiled-program-get-definitions program 'elem))
(elem-func-indices
(map wasm-elem-definition-func-index elem-defs))
(elems-def
(if (null? elem-defs)
'()
`((elem (i32.const 0) func ,@elem-func-indices))))
(get-module-definitions
(lambda (type)
(compiled-program-get-definitions program type))))
`(module
,@(get-module-definitions 'type)
,@(get-module-definitions 'func)
,@(get-module-definitions 'table)
,@(get-module-definitions 'global)
,@(get-module-definitions 'export)
,@(get-module-definitions 'start)
,@elems-def)))
(define (make-global-lexical-env variables exports)
(let ((duplicate-var (first-duplicate variables)))
(if (not (null? duplicate-var))
(raise-compilation-error "Top-level identifier already defined" (car duplicate-var))))
(for-each
(lambda (export)
(if (not (memq export variables))
(raise-compilation-error "No top-level definition for export" export)))
exports)
(add-new-lexical-frame
(make-empty-lexical-env)
variables
(fold-left
(lambda (additional-info var)
(if (memq var exports)
(cons `(,var (export ,(symbol->string var)))
additional-info)
additional-info))
'()
variables)))
)