-
Notifications
You must be signed in to change notification settings - Fork 5
/
dynamic-typing.rkt
575 lines (519 loc) · 19.3 KB
/
dynamic-typing.rkt
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
#lang racket
(require racket/set)
(require "utilities.rkt")
(require "interp.rkt")
(require "dynamic-interp.rkt")
(require "lambda.rkt")
(provide compile-R6 R6-passes R6-typechecker R7-passes)
(define compile-R6
(class compile-R4
(super-new)
(define type-predicates (set 'boolean? 'integer? 'vector? 'procedure?))
(define/override (primitives)
(set-union (super primitives)
type-predicates
(set 'inject 'project)
))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; type-check
(define/override (type-check env)
(lambda (e)
(define recur (type-check env))
(match e
[`(vector-ref ,(app recur e^ t) ,(app recur i it))
(match (list t i)
[`((Vector ,ts ...) (has-type ,i^ Integer))
(unless (and (exact-nonnegative-integer? i^)
(i^ . < . (length ts)))
(error 'type-check "invalid index ~a in ~a" i^ e))
(let ([t (list-ref ts i^)])
(values `(has-type (vector-ref ,e^ (has-type ,i^ Integer)) ,t)
t))]
[`((Vectorof ,t) ,i)
(values `(has-type (vector-ref ,e^ ,i) ,t) t)]
[else (error "expected a vector in vector-ref, not" t)])]
[`(vector-set! ,(app recur e-vec^ t-vec) ,(app recur i it)
,(app recur e-arg^ t-arg))
(match (list t-vec i)
[`((Vector ,ts ...) (has-type ,i^ Integer))
(unless (and (exact-nonnegative-integer? i^)
(i^ . < . (length ts)))
(error 'type-check "invalid index ~a in ~a" i^ e))
(unless (equal? (list-ref ts i^) t-arg)
(error 'type-check "type mismatch in vector-set! ~a ~a"
(list-ref ts i^) t-arg))
(values `(has-type (vector-set! ,e-vec^
(has-type ,i^ Integer)
,e-arg^) Void) 'Void)]
[`((Vectorof ,t) ,i)
(unless (equal? t t-arg)
(error 'type-check "type mismatch in vector-set! ~a ~a"
t t-arg))
(values `(has-type (vector-set! ,e-vec^ ,i ,e-arg^) Void) 'Void)]
[else (error 'type-check "expected a vector in vector-set!, not ~a"
t-vec)])]
[`(inject ,(app recur new-e e-ty) ,ty)
(cond
[(equal? e-ty ty)
(values `(has-type (inject ,new-e ,ty) Any) 'Any)]
[else
(error "injected expression does not have expected type" e ty)])]
[`(project ,(app recur new-e e-ty) ,ty)
(cond
[(equal? e-ty 'Any)
(values `(has-type (project ,new-e ,ty) ,ty) ty)]
[else
(error "project expression does not have type Any" e)])]
[`(,pred ,e) #:when (set-member? type-predicates pred)
(define-values (new-e e-ty) (recur e))
(cond
[(equal? e-ty 'Any)
(values `(has-type (,pred ,new-e) Boolean) 'Boolean)]
[else
(error "type predicate expected argument of type Any, not" e-ty)])]
[else
((super type-check env) e)]
)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; uniquify
(define/override (uniquify env)
(lambda (e)
(match e
[`(inject ,e ,ty)
`(inject ,((uniquify env) e) ,ty)]
[`(project ,e ,ty)
`(project ,((uniquify env) e) ,ty)]
[else ((super uniquify env) e)])))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; reveal-functions
(define/override (reveal-functions funs)
(lambda (e)
(define recur (send this reveal-functions funs))
(match e
[`(inject ,e ,t)
`(inject ,(recur e) ,t)]
[`(project ,e ,t)
`(project ,(recur e) ,t)]
[else ((super reveal-functions funs) e)])))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; convert-to-closures
(define/override (free-variables e)
(define (recur e) (send this free-variables e))
(match e
[(or `(inject ,e ,ty) `(project ,e ,ty))
(recur e)]
[else (super free-variables e)]
))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; expose-allocation
(define/override (expose-allocation)
(lambda (e)
(verbose "expose-allocation" e)
(match e
[`(inject ,(app (expose-allocation) e) ,ty)
`(inject ,e ,ty)]
[`(project ,(app (expose-allocation) e) ,ty)
`(project ,e ,ty)]
[else
((super expose-allocation) e)]
)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; flatten
(define/override (flatten need-atomic)
(lambda (e)
(verbose "flatten" e)
(match e
[`(has-type (inject ,e ,ty) ,ty2)
(define-values (new-e e-ss xs) ((send this flatten #t) e))
(cond [need-atomic
(define tmp (gensym 'tmp))
(values tmp
(append e-ss `((assign ,tmp (inject ,new-e ,ty))))
(cons (cons tmp ty2) xs)
)]
[else
(values `(inject ,new-e ,ty) e-ss xs)])]
[`(has-type (project ,e ,ty) ,ty2)
(define-values (new-e e-ss xs) ((send this flatten #t) e))
(cond [need-atomic
(define tmp (gensym 'tmp))
(values tmp
(append e-ss `((assign ,tmp (project ,new-e ,ty))))
(cons (cons tmp ty2) xs))]
[else
(values `(project ,new-e ,ty) e-ss xs)])]
[else
((super flatten need-atomic) e)]
)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define/override (root-type? t)
(match t
[`Any #t]
[`(Vectorof ,T) #t]
[else (super root-type? t)]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; select-instructions
(define (any-tag ty)
(match ty
['Integer 1] ;; 001
['Boolean 4] ;; 100
['Void 5] ;; 101
[`(Vector ,ts ...) 2] ;; 010
[`(Vectorof ,t) 2]
[`(,ts ... -> ,rt) 3] ;; 011
[else (error "in any-tag, unrecognized type" ty)]
))
(define pred->tag
(lambda (pred)
(match pred
['integer? (any-tag 'Integer)]
['boolean? (any-tag 'Boolean)]
['vector? (any-tag '(Vectorof Any))]
['procedure? (any-tag '(Any -> Any))]
['void? (any-tag 'Void)]
[else (error "in pred->tag, unrecognized predicate" pred)]
)))
(define any-mask 7) ;; 111
(define pointer-mask 2) ;; 010, to detect a vector or procedure
(define tag-len 3) ;; number of bits in the tag
(define/override (instructions)
(set-union (super instructions)
(set 'salq 'sarq)))
(define/override (select-instructions)
(lambda (e)
(define recur (send this select-instructions))
(match e
[`(assign ,lhs (inject ,e ,ty))
(define new-lhs (recur lhs))
(define new-e (recur e))
(cond [(or (equal? ty 'Integer) (equal? ty 'Boolean))
`((movq ,new-e ,new-lhs)
(salq (int ,tag-len) ,new-lhs)
(orq (int ,(any-tag ty)) ,new-lhs))]
[else
`((movq ,new-e ,new-lhs)
(orq (int ,(any-tag ty)) ,new-lhs))])]
[`(assign ,lhs (project ,e ,ty))
(define new-lhs (recur lhs))
(define new-e (recur e))
`((movq ,new-e ,new-lhs)
(andq (int ,any-mask) ,new-lhs)
(if (eq? ,new-lhs (int ,(any-tag ty)))
,(match ty
[(or 'Integer 'Boolean 'Void)
;; booleans and integers
`((movq ,new-e ,new-lhs)
(sarq (int ,tag-len) ,new-lhs))]
[else ;; vectors and procedures (pointers)
;; vectors and procedures.
;; To do: check length of vector, arity of procedure. -Jeremy
`((movq (int ,any-mask) ,new-lhs)
(notq ,new-lhs)
(andq ,new-e ,new-lhs))
])
;; shouldn't we push the status code? -Jeremy
((callq ,(string->symbol (label-name 'exit))))))]
[`(assign ,lhs (,pred ,e)) #:when (set-member? type-predicates pred)
(define new-lhs (recur lhs))
(define new-e (recur e))
`((movq ,new-e ,new-lhs)
(andq (int ,any-mask) ,new-lhs)
(cmpq (int ,(pred->tag pred)) ,new-lhs)
(set e (byte-reg al))
(movzbq (byte-reg al) ,new-lhs))]
[else ((super select-instructions) e)]
)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; uncover-live
(define/override (read-vars instr)
(match instr
[(or `(sarq ,s ,d) `(salq ,s ,d))
(set-union (send this free-vars s) (send this free-vars d))]
[else (super read-vars instr)]
))
(define/override (write-vars instr)
(match instr
[(or `(sarq ,s ,d) `(salq ,s ,d))
(send this free-vars d)]
[else (super write-vars instr)]))
))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;; R7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define compile-R7
(class compile-R6
(super-new)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; cast-insert
(define/public (cast-insert)
(lambda (e)
(match e
[(? integer?)
`(inject ,e Integer)]
[(? symbol?)
e]
[`(read)
`(inject (read) Integer)]
[`(function-ref ,e ,n)
`(inject (function-ref ,e)
(,@(map (lambda (x) 'Any) (range n)) -> Any))]
[`(+ ,e1 ,e2)
`(inject (+ (project ,((cast-insert) e1) Integer)
(project ,((cast-insert) e2) Integer))
Integer)]
[`(- ,e)
`(inject (- (project ,((cast-insert) e) Integer))
Integer)]
[`(let ([,x ,e1]) ,e2)
`(let ([,x ,((cast-insert) e1)])
,((cast-insert) e2))]
[#t `(inject #t Boolean)]
[#f `(inject #f Boolean)]
[`(and ,e1 ,e2)
(let ([gen (gensym)])
`(let ([,gen ,((cast-insert) e1)])
(if (eq? ,gen (inject #f Boolean))
,gen
,((cast-insert) e2))))]
[`(not ,e)
`(inject (not (project ,((cast-insert) e) Boolean)) Boolean)]
[`(eq? ,e1 ,e2)
`(inject (eq? ,((cast-insert) e1)
,((cast-insert) e2))
Boolean)]
[`(if ,eq ,et ,ef)
`(if (eq? ,((cast-insert) eq) (inject #f Boolean))
,((cast-insert) ef)
,((cast-insert) et))]
[`(vector ,es ...)
`(inject (vector ,@(map (cast-insert) es))
(Vector ,@(map (lambda (x) 'Any) es)))]
[`(vector-ref ,e1 ,e2)
`(vector-ref (project ,((cast-insert) e1) (Vectorof Any))
(project ,((cast-insert) e2) Integer))]
[`(vector-set! ,e1 ,e2 ,e3)
`(inject (vector-set! (project ,((cast-insert) e1) (Vectorof Any))
(project ,((cast-insert) e2) Integer)
,((cast-insert) e3))
Void)]
[`(void) `(inject (void) Void)]
[`(lambda (,xs ...) ,e)
`(inject (lambda: (,@(map (lambda (x) `[,x : Any]) xs)) : Any
,((cast-insert) e))
(,@(map (lambda (x) 'Any) xs) -> Any))]
[`(app ,e ,es ...)
`(app (project ,((cast-insert) e)
(,@(map (lambda (x) 'Any) es) -> Any))
,@(map (cast-insert) es))]
[`(define (,f ,xs ...) ,e)
`(define (,f ,@(map (lambda (x) `[,x : Any]) xs)) : Any
,((cast-insert) e))]
[`(program ,ds ... ,e)
`(program ,@(map (cast-insert) ds) ,((cast-insert) e))])))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; type-check
(define/override (type-check env)
(lambda (e)
(match e
[`(function-ref ,e)
(let ([t (lookup e env)])
(values `(has-type (function-ref ,e) ,t) t))]
[`(app ,e ,es ...)
(define-values (e* ty*)
(for/lists (e* ty*) ([e (in-list es)])
((type-check env) e)))
(define-values (e^ ty)
((type-check env) e))
(match ty
[`(,ty^* ... -> ,rt)
(unless (equal? ty* ty^*)
(error "parameter and argument type mismatch for function" e))
(vomit "app" e^ e* rt)
(values `(has-type (app ,e^ ,@e*) ,rt) rt)]
[else (error "expected a function, not" ty)])]
[else
((super type-check env) e)]
)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; uniquify
(define/override (uniquify env)
(lambda (e)
(match e
[`(define (,f ,xs ...) ,body)
(define new-xs (for/list ([x xs]) (gensym (racket-id->c-id x))))
(define new-env (append (map cons xs new-xs) env))
`(define (,(cdr (assq f env))
,@new-xs)
,((uniquify new-env) body))]
[`(program ,ds ... ,body) #:when (or (null? ds) (not (eq? (caar ds) 'type)))
(define new-env
(for/list ([d ds])
(match d
[`(define (,f ,xs ...) ,body)
(define new-f (gensym (racket-id->c-id f)))
`(,f . ,new-f)]
[else (error "type-check, ill-formed function def")])))
`(program ,@(map (uniquify new-env) ds)
,((uniquify new-env) body))]
[`(lambda (,xs ...) ,body)
(define new-xs (for/list ([x xs]) (gensym (racket-id->c-id x))))
(define new-env (append (map cons xs new-xs) env))
`(lambda ,new-xs
,((uniquify new-env) body))]
[else ((super uniquify env) e)])))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; reveal-functions
(define/override (fun-def-name d)
(match d
[`(define (,f ,xs ...) ,body)
f]
[else (super fun-def-name d)]))
(define/public (fun-def-arity d)
(match d
[`(define (,f ,xs ...) ,body)
(length xs)]
[else (error 'Syntax-Error "ill-formed function definition in ~a" d)]))
(define/override (reveal-functions funs)
(lambda (e)
(define recur (send this reveal-functions funs))
(match e
[(? symbol?) #:when (or (null? funs) (pair? (car funs))) ; This is an arity-environment, meaning R7
(cond
[(lookup e funs #f) `(function-ref ,e ,(lookup e funs))]
[else e])]
['(void) '(void)]
[`(program ,ds ... ,body) #:when (or (null? ds) (not (eq? (caar ds) 'type)))
(define funs
(for/list ([d ds])
(cons (fun-def-name d) (fun-def-arity d))))
`(program ,@(map (reveal-functions funs) ds)
,((reveal-functions funs) body))]
[`(define (,f ,params ...) ,body)
`(define (,f ,@params) ,(recur body))]
[`(lambda ,params ,body)
`(lambda ,params ,(recur body))]
[else ((super reveal-functions funs) e)])))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; select-instructions : C -> psuedo-x86
(define/override (instructions)
(set-union (super instructions)
(set 'imulq)))
(define/override (select-instructions)
(lambda (x)
(vomit "select instructions" x)
(match x
[`(assign ,lhs (vector-ref ,e-vec ,i))
#:when (not (number? i))
(define lhs^ ((select-instructions) lhs))
(define e-vec^ ((select-instructions) e-vec))
(define i^ ((select-instructions) i))
`((movq ,i^ (reg r11))
(addq (int 1) (reg r11))
(imulq (int 8) (reg r11))
(addq ,e-vec^ (reg r11))
(movq (deref r11 0) ,lhs^))]
[`(assign ,lhs (vector-set! ,e-vec ,i ,e-arg))
#:when (not (number? i))
(define lhs^ ((select-instructions) lhs))
(define e-vec^ ((select-instructions) e-vec))
(define e-arg^ ((select-instructions) e-arg))
(define i^ ((select-instructions) i))
`((movq ,i^ (reg r11))
(addq (int 1) (reg r11))
(imulq (int 8) (reg r11))
(addq ,e-vec^ (reg r11))
(movq ,e-arg^ (deref r11 0))
(movq (int 0) ,lhs^))]
[else ((super select-instructions) x)])))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; patch-instructions
(define/override (patch-instructions)
(lambda (e)
(match e
;; for imulq, destination must be a register -Jeremy
[`(imulq ,s (reg ,d))
`((imulq ,s (reg ,d)))]
[`(imulq ,s ,d)
`((movq ,d (reg rax))
(imulq ,s (reg rax))
(movq (reg rax) ,d))]
[else
((super patch-instructions) e)]
)))
));; compile-R7
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Passes
(define R6-typechecker
(let ([compiler (new compile-R6)])
(send compiler type-check '())))
(define R6-passes
(let ([compiler (new compile-R6)]
[interp (new interp-R6)])
`(
;("type-check" ,(send compiler type-check '())
; ,(send interp interp-scheme '()))
("uniquify" ,(send compiler uniquify '())
,(send interp interp-scheme '()))
("reveal-functions" ,(send compiler reveal-functions '())
,(send interp interp-F '()))
("convert-to-closures" ,(send compiler convert-to-closures)
,(send interp interp-F '()))
("expose allocation"
,(send compiler expose-allocation)
,(send interp interp-F '()))
("flatten" ,(send compiler flatten #f)
,(send interp interp-C '()))
("instruction selection" ,(send compiler select-instructions)
,(send interp interp-x86 '()))
("liveness analysis" ,(send compiler uncover-live (void))
,(send interp interp-x86 '()))
("build interference" ,(send compiler build-interference
(void) (void) (void))
,(send interp interp-x86 '()))
("build move graph" ,(send compiler
build-move-graph (void))
,(send interp interp-x86 '()))
("allocate registers" ,(send compiler allocate-registers)
,(send interp interp-x86 '()))
("lower conditionals" ,(send compiler lower-conditionals)
,(send interp interp-x86 '()))
("patch instructions" ,(send compiler patch-instructions)
,(send interp interp-x86 '()))
("print x86" ,(send compiler print-x86) #f)
)))
(define R7-passes
(let ([compiler (new compile-R7)]
[interp (new interp-R6)])
`(
("uniquify" ,(send compiler uniquify '())
,(interp-r7 '()))
("reveal-functions" ,(send compiler reveal-functions '())
,(interp-r7 '()))
("cast-insert" ,(send compiler cast-insert)
,(send interp interp-F '()))
("type-check" ,(send compiler type-check '())
,(send interp interp-F '()))
("convert-to-closures" ,(send compiler convert-to-closures)
,(send interp interp-F '()))
("expose allocation"
,(send compiler expose-allocation)
,(send interp interp-F '()))
("flatten" ,(send compiler flatten #f)
,(send interp interp-C '()))
("instruction selection" ,(send compiler select-instructions)
,(send interp interp-x86 '()))
("liveness analysis" ,(send compiler uncover-live (void))
,(send interp interp-x86 '()))
("build interference" ,(send compiler build-interference
(void) (void) (void))
,(send interp interp-x86 '()))
("build move graph" ,(send compiler
build-move-graph (void))
,(send interp interp-x86 '()))
("allocate registers" ,(send compiler allocate-registers)
,(send interp interp-x86 '()))
("lower conditionals" ,(send compiler lower-conditionals)
,(send interp interp-x86 '()))
("patch instructions" ,(send compiler patch-instructions)
,(send interp interp-x86 '()))
("print x86" ,(send compiler print-x86) #f)
)))