-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgen.scm
480 lines (440 loc) · 16.3 KB
/
gen.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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
(load "utilities.scm")
(define num-arguments-limit 4)
(define (has-optional? args)
(cond ((null? args) #f)
((pair? args) (has-optional? (cdr args)))
(else #t)))
(define (make-function)
;; name, arguments, local variables, and body (inverse order)
(list #f '() '() '()))
(define (function-name fun)
(car fun))
(define (set-function-name! fun name) ; 'name' is a symbol
(set-car! fun name))
(define (function-args fun)
(cadr fun))
(define (set-function-args! fun args) ; 'args' is a list of 'arg'
(set-car! (cdr fun) args))
(define (push-function-args! arg fun) ; 'arg' is a pair of type and name
(set-function-args!
fun
(cons arg (function-args fun))))
(define (function-vars fun)
(caddr fun))
(define (set-function-vars! fun vars) ; 'vars' is a list of 'var'
(set-car! (cddr fun) vars))
(define (push-function-vars! var fun) ; 'var' is a pair of type and name
(set-function-vars!
fun
(cons var (function-vars fun))))
(define (function-body fun)
(cadddr fun))
(define (set-function-body! fun body)
(set-car! (cdddr fun) body))
(define (push-function-body! b fun)
(set-function-body!
fun
(cons b (function-body fun))))
(define (translate-prototype fun . ret-type)
(string-append
(if (null? ret-type) "static void" (ensure-string (car ret-type)))
" "
(ensure-string (function-name fun))
"("
(apply
string-append
(implode
", "
(map (lambda (p)
(string-append (ensure-string (car p))
" "
(ensure-string (cdr p))))
(let ((args (function-args fun))) ; args can be ().
(if (> (- (length args) 1) num-arguments-limit)
(list (car args) (cadr args))
args)))))
")"))
(define (translate-local-vars vars indent)
(apply
string-append
(map (lambda (p)
(string-append (repeat-string indent " ")
(ensure-string (car p))
" "
(ensure-string (cdr p))
";" (string #\Newline)))
vars)))
(define (translate-argument arg)
(cond ((and (pair? arg) (eq? (car arg) 'int2fixnum))
(string-append "INT2FIXNUM(" (ensure-string (cadr arg)) ")"))
((boolean? arg) (if arg "sharpt" "sharpf"))
((null? arg) "nil")
(else (ensure-string arg))))
(define (translate-builtin-call fn args . optional?)
(string-append
"builtin_"
(lisp-name-to-c-name (ensure-string fn))
(if (null? optional?)
"((void*)0, (cont_t*)"
(string-append
"((void*)0, " (number->string (length args)) ", (cont_t*)"))
(apply
string-append
(implode ", " (map translate-argument args)))
");" (string #\Newline)))
(define (translate-builtin-optional-call fn args)
(translate-builtin-call fn args #t))
(define (translate-cont-call clos args . safe)
(string-append
(if (> (length args) num-arguments-limit)
"continue_with_many((cont_t*)"
(string-append
(if (null? safe) "RAW_CONTINUE" "CONTINUE")
(ensure-string (length args))
"("))
(ensure-string clos)
", "
(if (> (length args) num-arguments-limit)
(string-append (ensure-string (length args)) ", ")
"")
(apply
string-append
(implode ", " (map translate-argument args)))
");" (string #\Newline)))
(define (translate-safe-cont-call clos args)
(translate-cont-call clos args #t))
(define (translate-if test then alt indent)
(string-append
"if (" (ensure-string test) ") {" (string #\Newline)
(repeat-string (+ indent 2) " ")
(translate-sentence then (+ indent 2))
(repeat-string indent " ")
"} else {" (string #\Newline)
(repeat-string (+ indent 2) " ")
(translate-sentence alt (+ indent 2))
(repeat-string indent " ")
"}" (string #\Newline)))
(define (translate-sentence s indent)
(cond ((not (pair? s)) (error "Translation error"))
((pair? (car s))
(apply string-append
(implode (repeat-string indent " ")
(translate-sentence-list s indent))))
((eq? (car s) 'builtin-call)
(translate-builtin-call (cadr s) (caddr s)))
((eq? (car s) 'builtin-optional-call)
(translate-builtin-optional-call (cadr s) (caddr s)))
((eq? (car s) 'proc-call)
(translate-safe-cont-call (cadr s) (caddr s)))
((eq? (car s) 'cont-call)
(translate-cont-call (cadr s) (caddr s)))
((eq? (car s) 'safe-cont-call)
(translate-safe-cont-call (cadr s) (caddr s)))
((eq? (car s) 'if)
(translate-if (cadr s) (caddr s) (cadddr s) indent))
(else (string-append (ensure-string s) ";" (string #\Newline)))))
(define (translate-sentence-list slist indent)
(map (lambda (x) (translate-sentence x indent)) slist))
(define (translate-body body indent)
(apply
string-append
(cons
(repeat-string indent " ")
(implode
(repeat-string indent " ")
(translate-sentence-list body indent)))))
(define (translate-to-c fun . ret-type)
(string-append
(apply translate-prototype fun ret-type)
" {" (string #\Newline)
(translate-local-vars (function-vars fun) 2)
(translate-body (reverse (function-body fun)) 2)
"}" (string #\Newline)
))
(define lambda-names '())
(define (add-lambda-name exp name)
(set! lambda-names
(cons (cons exp name) lambda-names)))
(define (find-lambda-name exp)
(let ((ret (assq exp lambda-names)))
(and ret (cdr ret))))
(define compiled-results '())
(define (add-compiled-function name fun)
(set! compiled-results
(cons (cons name fun) compiled-results)))
(define (find-compiled-function name)
(let ((ret (assq name compiled-results)))
(and ret (cdr ret))))
(define (init-compile)
(set! lambda-names '())
(set! compiled-results '()))
(define (has-lambda? exp)
(define (check-arguments args)
(cond ((null? args) #f)
((has-lambda? (car args)) #t)
(else (check-arguments (cdr args)))))
(cond ((not (pair? exp)) #f)
((eq? (car exp) 'lambda) #t)
((has-lambda? (car exp)) #t)
(else (check-arguments (cdr exp)))))
(define (lisp-name-to-c-name str)
(do ((i 0 (+ i 1))
(acc '()))
((= i (string-length str)) (apply string (reverse acc)))
(let ((c (string-ref str i)))
(cond ((eq? c #\-) (set! acc (cons #\_ acc)))
((eq? c #\?) (set! acc (cons #\p acc)))
((eq? c #\!) (set! acc (list* #\n #\a #\b acc)))
((eq? c #\=) (set! acc (list* #\l #\q #\e acc)))
((eq? c #\+) (set! acc (list* #\s #\u #\l #\p acc)))
((eq? c #\*) (set! acc (list* #\r #\a #\t #\s acc)))
((eq? c #\/) (set! acc (list* #\a #\l #\s acc)))
(else (set! acc (cons c acc)))))))
(define (lookup-var var env)
(define (iter env level)
(cond ((null? env) #f)
((member var (car env))
(list level (position var (car env))))
(else (iter (cdr env) (+ level 1)))))
(iter env 0))
(define (gen-lookup-var-code var link pos fun)
(if (= link 0)
;; If the number of arguments is greater than num-arguments-limit,
;; function-args return the list (env args_env original-args ...).
;; args_env->vars[i] hold the value of argument.
;; original-args is used at only compile time.
(if (> (- (length (function-args fun)) 1) num-arguments-limit)
(list (cdadr (function-args fun)) "->vars[" pos "]")
var)
(list
(cdar (function-args fun))
(repeat-string (- link 1) "->link")
"->vars[" pos "]")))
(define (gen-builtin-as-variable var)
(list "builtin_clos_" (lisp-name-to-c-name (ensure-string var))))
(define (gen-lookup-var var env fun)
(let* ((loc (lookup-var var env))
(link (and loc (car loc)))
(pos (and loc (cadr loc))))
(cond (loc
(gen-lookup-var-code var link pos fun))
((assoc var builtin-list)
(gen-builtin-as-variable var))
(else var)))) ; global variable
(define (gen-literal-lambda-code exp env fun)
(gen-lambda-code exp env)
(let ((clos (gensym "clos"))
(cenv
(if (> (- (length (function-args fun)) 1) num-arguments-limit)
(cdadr (function-args fun))
(cdr (list-ref (function-vars fun)
(- (length (function-vars fun)) 1)))))
(name (find-lambda-name exp))
(len (length (improper-list->proper-list (cadr exp)))))
(push-function-vars! (cons "cont_t" clos) fun)
(push-function-body!
(list
(list clos ".tag = TAG_CONT")
(list clos ".env = " cenv)
(list clos ".fn = (function1_t)" name)
(list clos ".num_required_args = " len)
(list clos ".optional_args = "
(if (has-optional? (cadr exp)) 1 0)))
fun)
(list "&" clos)))
(define (gen-literal-code exp env fun)
(cond ((number? exp)
(list 'int2fixnum exp))
((symbol? exp)
(gen-lookup-var exp env fun))
((boolean? exp) exp)
((null? exp) exp)
((and (pair? exp) (eq? (car exp) 'quote))
(gen-quote-code exp env fun))
((and (pair? exp) (eq? (car exp) 'set!))
(gen-set!-code exp env fun))
((and (pair? exp) (eq? (car exp) 'lambda))
(gen-literal-lambda-code exp env fun))))
(define (gen-quote-code exp env fun)
(cond ((number? (cadr exp))
(list 'int2fixnum (cadr exp)))
((boolean? (cadr exp)) (cadr exp))
((null? (cadr exp)) (cadr exp))
((symbol? (cadr exp))
(let ((sym (gensym "sym")))
(push-function-vars! (cons "static lobject" sym) fun)
(push-function-body!
(list 'if (list sym " == 0")
(list
(list sym " = intern(\"" (cadr exp) "\")")
(list "add_symbol_rootset(&" sym ")"))
(list ""))
fun)
sym))
(else
(error "Not implemented"))))
(define (gen-if-code exp env fun)
(list 'if
(list "BOOLTEST(" (gen-lookup-var (cadr exp) env fun) ")")
(gen-apply-code (caddr exp) env fun)
(gen-apply-code (cadddr exp) env fun)))
(define (gen-builtin-application exp env fun)
(let* ((builtin (assoc (car exp) builtin-list))
(num-required-args (+ (cadr builtin) 1)) ; Add a continuation.
(optional-args? (= (caddr builtin) 1))
(num-args (length (cdr exp))))
(if (or (= num-required-args num-args)
(and (< num-required-args num-args) optional-args?))
(list (if optional-args? 'builtin-optional-call 'builtin-call)
(car exp)
(map (lambda (x) (gen-literal-code x env fun)) (cdr exp)))
(error "Wrong number of arguments."))))
(define (gen-user-proc-application exp env fun)
(list 'proc-call (car exp)
(map (lambda (x) (gen-literal-code x env fun)) (cdr exp))))
(define (gen-user-closure-application name lambda-exp args env fun)
(let ((clos (gensym "clos"))
(cenv
(if (> (- (length (function-args fun)) 1) num-arguments-limit)
(cdadr (function-args gun))
(cdr (list-ref (function-vars fun)
(- (length (function-vars fun)) 1))))))
(push-function-vars! (cons "cont_t" clos) fun)
(list
(list clos ".tag = TAG_CONT")
(list clos ".env = " cenv)
(list clos ".fn = (function1_t)" name)
(list clos ".num_required_args = "
(length (improper-list->proper-list (cadr lambda-exp))))
(list clos ".optional_args = "
(if (has-optional? (cadr lambda-exp)) 1 0))
(list 'safe-cont-call (list "&" clos)
(map (lambda (x) (gen-literal-code x env fun)) args)))))
(define (gen-continuation-code exp env fun)
(list 'safe-cont-call
(gen-lookup-var (car exp) env fun)
(map (lambda (x) (gen-literal-code x env fun)) (cdr exp))))
(define (gen-set!-assign-code var val fun)
(push-function-body!
(if (symbol? var) ; global variable
(list
var
" = (lobject)"
(translate-argument val))
(list
"SET_WITH_BARRIER("
(ensure-string (reverse (cdddr (reverse var)))) ; environment
", "
(ensure-string var)
", "
(translate-argument val)
")"))
fun))
(define (gen-set!-code exp env fun)
(gen-set!-assign-code
(if (lookup-var (cadr exp) env)
(gen-lookup-var (cadr exp) env fun)
(cadr exp)) ; global variable
(gen-literal-code (caddr exp) env fun)
fun)
(list 'int2fixnum 0)) ; undefined value
(define (gen-apply-code exp env fun)
(cond ((not (pair? exp)) (error "Compile error"))
((eq? (car exp) 'if)
(gen-if-code exp env fun))
((pair? (car exp)) ; lambda-exp
(gen-lambda-code (car exp) env)
(gen-user-closure-application
(find-lambda-name (car exp))
(car exp)
(cdr exp)
env
fun))
((lookup-var (car exp) env)
(gen-continuation-code exp env fun))
((assoc (car exp) builtin-list) ; builtin function
(gen-builtin-application exp env fun))
(else
(gen-user-proc-application exp env fun))))
(define (gen-current-env-for-many-args fun args)
(let ((penv (cdar (function-args fun)))
(args (cdadr (function-args fun)))
(len (length (cddr (function-args fun)))))
(list
(list 'if (list args "->tag != TAG_ENV || " args "->num"
(if (has-optional? args) " < " " != ") len)
(list (list "fprintf(stderr, \"Wrong number of arguments.\\n\")")
(list "exit(1)"))
(list ""))
(list args "->link = " penv))))
(define (gen-current-env fun)
;; assume that the first variable is cenv and the first argument is penv.
(let ((ret '())
(cenv (cdar (function-vars fun)))
(penv (cdar (function-args fun))))
(set!
ret
(cons (list cenv " = (env_t*)alloca(sizeof(env_t) + sizeof(lobject) * "
(- (length (function-args fun)) 2) ")")
ret))
(set! ret (cons (list cenv "->tag = TAG_ENV") ret))
(set! ret (cons (list cenv "->num = "
(- (length (function-args fun)) 1)) ret))
(set! ret (cons (list cenv "->link = " penv) ret))
(do ((i 0 (+ i 1))
(args (cdr (function-args fun)) (cdr args)))
((null? args) #t)
(set! ret
(cons (list cenv "->vars[" i "] = (lobject)" (cdar args)) ret)))
(reverse ret)))
(define (gen-check-stack-code fun)
(let ((name (function-name fun))
(args (map cdr (function-args fun))))
(if (> (- (length args) 1) num-arguments-limit)
(set! args (list (car args) (cadr args))))
(append
(list* "check_stack(" name ", " (car args) ", " (length (cdr args)) ", "
(implode ", " (cdr args)))
'(")"))))
(define (gen-lambda-code exp env)
(if (find-lambda-name exp)
'already-compiled
(let ((fun (make-function))
(args (cadr exp))
(body (caddr exp)))
(if (has-optional? args)
(set! args (improper-list->proper-list args)))
(set-function-name! fun (gensym "fun"))
(set-function-args!
fun
(map (lambda (x) (cons 'lobject x)) args))
(if (> (length args) num-arguments-limit)
(push-function-args! (cons "env_t*" (gensym "args")) fun))
(push-function-args! (cons "env_t*" (gensym "penv")) fun)
(push-function-body! (gen-check-stack-code fun) fun)
(if (> (length args) num-arguments-limit)
(push-function-body! (gen-current-env-for-many-args fun args) fun)
(if (has-lambda? body)
(begin
(push-function-vars! (cons "env_t*" (gensym "cenv")) fun)
(push-function-body! (gen-current-env fun) fun))))
(add-lambda-name exp (function-name fun))
(push-function-body!
(gen-apply-code body (cons args env) fun)
fun)
(add-compiled-function (function-name fun) fun)
fun)))
(define (gen exp)
;; assume that exp is the lambda expression.
(if (and (pair? exp) (eq? (car exp) 'lambda))
(gen-lambda-code exp '())
(error "Compile error")))
(define (show-result)
(for-each
(lambda (x)
(display (string-append (translate-prototype (cdr x)) ";")) (newline))
compiled-results)
(for-each
(lambda (x)
(display (translate-to-c (cdr x)))(newline))
compiled-results))