Skip to content

Commit 6ed2f30

Browse files
committed
functions are working again
1 parent c060de8 commit 6ed2f30

File tree

6 files changed

+179
-107
lines changed

6 files changed

+179
-107
lines changed

dynamic-typing.rkt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@
510510
("liveness analysis" ,(send compiler uncover-live (void))
511511
,(send interp interp-x86 '()))
512512
("build interference" ,(send compiler build-interference
513-
(void) (void))
513+
(void) (void) (void))
514514
,(send interp interp-x86 '()))
515515
("build move graph" ,(send compiler
516516
build-move-graph (void))
@@ -548,7 +548,7 @@
548548
("liveness analysis" ,(send compiler uncover-live (void))
549549
,(send interp interp-x86 '()))
550550
("build interference" ,(send compiler build-interference
551-
(void) (void))
551+
(void) (void) (void))
552552
,(send interp interp-x86 '()))
553553
("build move graph" ,(send compiler
554554
build-move-graph (void))

functions.rkt

Lines changed: 81 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
(class compile-R2
1010
(super-new)
1111

12-
(inherit primitives liveness-ss)
12+
(inherit primitives liveness-ss allocate-homes color-graph)
1313

1414
(define/public (non-apply-ast)
1515
(set-union (primitives)
@@ -127,13 +127,39 @@
127127
))
128128
ret)))
129129

130+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
131+
;; expose allocation : C1 -> ?
132+
;; This pass has to thread a new environment through that isn't extended
133+
;; This could be overcome with pointers or we could use
134+
135+
(define/override (expose-allocation)
136+
(lambda (e)
137+
(verbose "expose-allocation" e)
138+
(match e
139+
[`(app ,(app (expose-allocation) es) ...)
140+
`(app ,@es)]
141+
[`(function-ref ,f)
142+
`(function-ref ,f)]
143+
[`(program (type ,ty) ,ds ...
144+
,(app (expose-allocation) new-e))
145+
`(program (type ,ty) ,@(map (lambda (d) (expose-allocation-def d))
146+
ds) ,new-e)]
147+
[else
148+
((super expose-allocation) e)])))
149+
150+
(define/public (expose-allocation-def def)
151+
(match def
152+
[`(define (,f ,p:t* ...) : ,t
153+
,(app (expose-allocation) e))
154+
`(define (,f ,@p:t*) : ,t ,e)]
155+
[else (error 'expose-allocation-def "unmatched ~a" def)]))
156+
130157
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
131158
;; flatten : S3 -> C3-expr x (C3-stmt list)
132159

133160
(define/public (flatten-body body)
134161
(define-values (new-body ss locals) ((flatten #t) body))
135-
(values locals
136-
(append ss `((return ,new-body)))))
162+
(values locals (append ss `((return ,new-body)))))
137163

138164
(define/override (flatten need-atomic)
139165
(lambda (ast)
@@ -149,8 +175,8 @@
149175
,@new-body)]
150176
[`(has-type (function-ref ,f) ,t)
151177
(define tmp (gensym 'tmp))
152-
(values `(has-type ,tmp ,t)
153-
(list `(assign ,tmp (has-type (function-ref ,f) ,t)))
178+
(values tmp
179+
(list `(assign ,tmp (function-ref ,f)))
154180
(list (cons tmp t)))]
155181
[`(has-type (app ,f ,es ...) ,t)
156182
(define-values (new-f f-ss xs1) ((flatten #t) f))
@@ -161,42 +187,16 @@
161187
(cond
162188
[need-atomic
163189
(define tmp (gensym 'tmp))
164-
(values `(has-type ,tmp ,t)
165-
(append ss `((assign ,tmp (has-type ,fun-apply ,t))))
190+
(values tmp
191+
(append ss `((assign ,tmp ,fun-apply)))
166192
(cons (cons tmp t) (append xs1 xs2))
167193
)]
168194
[else
169-
(values `(has-type ,fun-apply ,t) ss (append xs1 xs2))])]
195+
(values fun-apply ss (append xs1 xs2))])]
170196
[else ((super flatten need-atomic) ast)])))
171197

172198
(inherit root-type?)
173199

174-
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
175-
;; expose allocation : C1 -> ?
176-
;; This pass has to thread a new environment through that isn't extended
177-
;; This could be overcome with pointers or we could use
178-
179-
; (inherit expose-allocation-seq)
180-
(define/override (expose-allocation)
181-
(lambda (prog)
182-
(match prog
183-
[`(program (,xs ...) (type ,ty)
184-
(defines ,(app expose-allocation-def ds) ...)
185-
,e)
186-
(let ([new-e (expose-allocation e)])
187-
`(program ,xs
188-
(type ,ty)
189-
(defines ,@ds)
190-
,new-e))]
191-
[else (error 'expose-allocation "unmatched ~a" prog)])))
192-
193-
(define/public (expose-allocation-def def)
194-
(match def
195-
[`(define (,f ,p:t* ...) : ,t (,l* ...)
196-
. ,(app expose-allocation e))
197-
`(define (,f ,@p:t*) : ,t ,l* ,e)]
198-
[else (error 'expose-allocation-def "unmatched ~a" def)]))
199-
200200
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
201201
;; select-instructions : env -> S3 -> S3
202202

@@ -226,10 +226,10 @@
226226
`(define (,f)
227227
,(length xs) (,(append (map cons xs ps) locals) ,max-stack)
228228
,@new-ss)]
229-
[`(assign ,lhs (has-type (function-ref ,f) ,t))
229+
[`(assign ,lhs (function-ref ,f))
230230
(define new-lhs ((select-instructions) lhs))
231231
`((leaq (function-ref ,f) ,new-lhs))]
232-
[`(assign ,lhs (has-type (app ,f ,es ...) ,t))
232+
[`(assign ,lhs (app ,f ,es ...))
233233
(define new-lhs ((select-instructions) lhs))
234234
(define new-f ((select-instructions) f))
235235
(define new-es (map (select-instructions) es))
@@ -298,26 +298,28 @@
298298
;; build-interference : live-after x graph -> pseudo-x86* -> pseudo-x86*
299299
;; *annotate program with interference graph
300300

301-
(define/override (build-interference live-after G)
301+
(define/override (build-interference live-after G xs)
302302
(lambda (ast)
303303
(vomit "build-interference" ast live-after G)
304304
(match ast
305305
[`(define (,f) ,n (,locals ,max-stack ,lives) ,ss ...)
306306
(define new-G (make-graph (map car locals)))
307307
(define new-ss
308308
(for/list ([inst ss] [live-after lives])
309-
((build-interference live-after new-G) inst)))
309+
((build-interference live-after new-G locals) inst)))
310310
`(define (,f) ,n (,locals ,max-stack ,new-G) ,@new-ss)]
311-
[`(program (,locals ,max-stack ,lives) (type ,ty) (defines ,ds ...) ,ss ...)
311+
[`(program (,locals ,max-stack ,lives) (type ,ty)
312+
(defines ,ds ...) ,ss ...)
312313
(define new-G (make-graph (map car locals)))
313314
(define new-ds
314315
(for/list ([d ds])
315-
((build-interference (void) (void)) d)))
316+
((build-interference (void) (void) (void)) d)))
316317
(define new-ss
317318
(for/list ([inst ss] [live-after lives])
318-
((build-interference live-after new-G) inst)))
319-
`(program (,locals ,max-stack ,new-G) (type ,ty) (defines ,@new-ds) ,@new-ss)]
320-
[else ((super build-interference live-after G) ast)]
319+
((build-interference live-after new-G locals) inst)))
320+
`(program (,locals ,max-stack ,new-G) (type ,ty)
321+
(defines ,@new-ds) ,@new-ss)]
322+
[else ((super build-interference live-after G xs) ast)]
321323
)))
322324

323325
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@@ -327,15 +329,17 @@
327329
(define/override (build-move-graph G)
328330
(lambda (ast)
329331
(match ast
330-
[`(program (,locals ,max-stack ,IG) (type ,ty) (defines ,ds ...) ,ss ...)
332+
[`(program (,locals ,max-stack ,IG) (type ,ty)
333+
(defines ,ds ...) ,ss ...)
331334
(define new-ds (for/list ([d ds])
332335
((build-move-graph (void)) d)))
333336
(define MG (make-graph (map car locals)))
334337
(define new-ss
335338
(for/list ([inst ss])
336339
((build-move-graph MG) inst)))
337340
(print-dot MG "./move.dot")
338-
`(program (,locals ,max-stack ,IG ,MG) (type ,ty) (defines ,@new-ds) ,@new-ss)]
341+
`(program (,locals ,max-stack ,IG ,MG) (type ,ty)
342+
(defines ,@new-ds) ,@new-ss)]
339343
[`(define (,f) ,n (,locals ,max-stack ,IG) ,ss ...)
340344
(define MG (make-graph (map car locals)))
341345
(define new-ss
@@ -355,8 +359,7 @@
355359
(define/override (assign-homes homes)
356360
(lambda (e)
357361
(match e
358-
#;[`(stack ,i) `(stack ,i)]
359-
[`(stack-arg ,i) `(stack-arg ,i)]
362+
[`(stack-arg ,i) `(stack-arg ,i)] ;; obsolete?? -JGS
360363
[`(indirect-callq ,f)
361364
`(indirect-callq ,((assign-homes homes) f))]
362365
[`(function-ref ,f) `(function-ref ,f) ]
@@ -369,20 +372,29 @@
369372
(define/override (allocate-registers)
370373
(lambda (ast)
371374
(match ast
372-
;; FIX ME -Jeremy
373-
#;[`(define (,f) ,n (,xs ,max-stack ,IG ,MG) ,ss ...)
374-
(define-values (homes stk-size)
375-
(allocate-homes IG MG (map car xs) ss))
375+
[`(define (,f) ,n (,locals ,max-stack ,IG ,MG) ,ss ...)
376+
(define color (color-graph IG MG (map car locals)))
377+
(define-values (homes stack-spills root-spills)
378+
(allocate-homes locals color))
379+
376380
(define new-ss (map (assign-homes homes) ss))
377-
`(define (,f) ,n ,(align (+ stk-size (* 8 max-stack)) 16) ,@new-ss)]
378-
;; FIX ME -Jeremy
379-
#;[`(program (,locals ,max-stack ,IG ,MG) (type ,ty) (defines ,ds ...)
381+
(define stack-size (align (+ (* stack-spills (variable-size))
382+
(* max-stack (variable-size))) 16))
383+
(define root-size (* root-spills (variable-size)))
384+
`(define (,f) ,n (,stack-size ,root-size) ,@new-ss)]
385+
386+
[`(program (,locals ,max-stack ,IG ,MG) (type ,ty) (defines ,ds ...)
380387
,ss ...)
381388
(define new-ds (map (allocate-registers) ds))
382-
(define-values (homes stk-size)
383-
(allocate-homes IG MG (map car locals) ss))
389+
(define color (color-graph IG MG (map car locals)))
390+
(define-values (homes stack-spills root-spills)
391+
(allocate-homes locals color))
384392
(define new-ss (map (assign-homes homes) ss))
385-
`(program ,(+ stk-size (* 8 max-stack)) (type ,ty)
393+
394+
(define stack-size (align (+ (* stack-spills (variable-size))
395+
(* max-stack (variable-size))) 16))
396+
(define root-size (* root-spills (variable-size)))
397+
`(program (,stack-size ,root-size) (type ,ty)
386398
(defines ,@new-ds) ,@new-ss)]
387399
)))
388400

@@ -456,7 +468,7 @@
456468
(format "\tcallq\t*~a\n" ((print-x86) f))]
457469
[`(stack-arg ,i)
458470
(format "~a(%rsp)" i)]
459-
[`(define (,f) ,n ,spill-space ,ss ...)
471+
[`(define (,f) ,n (,spill-space ,root-space) ,ss ...)
460472
(define callee-reg (set->list callee-save))
461473
(define save-callee-reg
462474
(for/list ([r callee-reg])
@@ -468,13 +480,20 @@
468480
(variable-size)))
469481
(define stack-adj (- (align (+ callee-space spill-space) 16)
470482
callee-space))
483+
(define initialize-roots
484+
(for/list ([i (range (/ root-space (variable-size)))])
485+
(string-append
486+
(format "\tmovq $0, (%~a)\n" rootstack-reg)
487+
(format "\taddq $~a, %~a\n"
488+
(variable-size) rootstack-reg))))
471489
(string-append
472490
(format "\t.globl ~a\n" f)
473491
(format "~a:\n" f)
474492
(format "\tpushq\t%rbp\n")
475493
(format "\tmovq\t%rsp, %rbp\n")
476494
(string-append* save-callee-reg)
477495
(format "\tsubq\t$~a, %rsp\n" stack-adj)
496+
(string-append* initialize-roots)
478497
;; Push callee saves at the bottom of the stack
479498
;; frame because the current code for stack nodes
480499
;; doesn't reason about them. -andre
@@ -483,14 +502,15 @@
483502
"\n"
484503
(format "\taddq\t$~a, %rsp\n" stack-adj)
485504
(string-append* restore-callee-reg)
505+
(format "\tsubq $~a, %~a\n" root-space rootstack-reg)
486506
(format "\tpopq\t%rbp\n")
487507
(format "\tretq\n")
488508
)]
489-
[`(program ,stack-space (type ,ty) (defines ,ds ...) ,ss ...)
509+
[`(program ,space (type ,ty) (defines ,ds ...) ,ss ...)
490510
(string-append
491511
(string-append* (for/list ([d ds]) ((print-x86) d)))
492512
"\n"
493-
((super print-x86) `(program ,stack-space (type ,ty) ,@ss)))]
513+
((super print-x86) `(program ,space (type ,ty) ,@ss)))]
494514
[else ((super print-x86) e)]
495515
)))
496516

@@ -522,7 +542,7 @@
522542
("liveness analysis" ,(send compiler uncover-live (void))
523543
,(send interp interp-x86 '()))
524544
("build interference" ,(send compiler build-interference
525-
(void) (void))
545+
(void) (void) (void))
526546
,(send interp interp-x86 '()))
527547
("build move graph" ,(send compiler
528548
build-move-graph (void))

0 commit comments

Comments
 (0)