Skip to content

Commit c060de8

Browse files
committed
vectors working with new approach to the root stack
1 parent 3da0c31 commit c060de8

7 files changed

+216
-356
lines changed

dynamic-typing.rkt

-14
Original file line numberDiff line numberDiff line change
@@ -151,21 +151,13 @@
151151
)))
152152

153153
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
154-
;; uncover-call-live-roots
155154

156155
(define/override (root-type? t)
157156
(match t
158157
[`Any #t]
159158
[`(Vectorof ,T) #t]
160159
[else (super root-type? t)]))
161160

162-
(define/override ((uncover-call-live-roots-exp xs) e)
163-
(vomit "any/uncover-call-live-roots-exp" e)
164-
(match e
165-
[`(inject ,e ,ty) ((uncover-call-live-roots-exp xs) e)]
166-
[`(project ,e ,ty) ((uncover-call-live-roots-exp xs) e)]
167-
[else ((super uncover-call-live-roots-exp xs) e)]))
168-
169161
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
170162
;; select-instructions
171163

@@ -513,9 +505,6 @@
513505
("expose allocation"
514506
,(send compiler expose-allocation)
515507
,(send interp interp-C '()))
516-
("uncover call live roots"
517-
,(send compiler uncover-call-live-roots)
518-
,(send interp interp-C '()))
519508
("instruction selection" ,(send compiler select-instructions)
520509
,(send interp interp-x86 '()))
521510
("liveness analysis" ,(send compiler uncover-live (void))
@@ -554,9 +543,6 @@
554543
("expose allocation"
555544
,(send compiler expose-allocation)
556545
,(send interp interp-C '()))
557-
("uncover call live roots"
558-
,(send compiler uncover-call-live-roots)
559-
,(send interp interp-C '()))
560546
("instruction selection" ,(send compiler select-instructions)
561547
,(send interp interp-x86 '()))
562548
("liveness analysis" ,(send compiler uncover-live (void))

functions.rkt

+10-64
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22
(require "vectors.rkt")
33
(require "interp.rkt")
44
(require "utilities.rkt")
5-
(require "uncover-types.rkt")
65
(require "runtime-config.rkt")
76
(provide compile-R3 functions-passes functions-typechecker)
87

98
(define compile-R3
109
(class compile-R2
1110
(super-new)
1211

13-
(inherit primitives liveness-ss allocate-homes)
12+
(inherit primitives liveness-ss)
1413

1514
(define/public (non-apply-ast)
1615
(set-union (primitives)
@@ -170,7 +169,7 @@
170169
(values `(has-type ,fun-apply ,t) ss (append xs1 xs2))])]
171170
[else ((super flatten need-atomic) ast)])))
172171

173-
(inherit reset-vars unique-var root-type?)
172+
(inherit root-type?)
174173

175174
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
176175
;; expose allocation : C1 -> ?
@@ -185,7 +184,7 @@
185184
(defines ,(app expose-allocation-def ds) ...)
186185
,e)
187186
(let ([new-e (expose-allocation e)])
188-
`(program ,(append (reset-vars) xs)
187+
`(program ,xs
189188
(type ,ty)
190189
(defines ,@ds)
191190
,new-e))]
@@ -195,61 +194,9 @@
195194
(match def
196195
[`(define (,f ,p:t* ...) : ,t (,l* ...)
197196
. ,(app expose-allocation e))
198-
`(define (,f ,@p:t*) : ,t ,(append (reset-vars) l*) ,e)]
197+
`(define (,f ,@p:t*) : ,t ,l* ,e)]
199198
[else (error 'expose-allocation-def "unmatched ~a" def)]))
200199

201-
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
202-
;; uncover-call-live-roots : (hashtable id type) (set id) -> C1-Expr -> C1-Expr x (set id)
203-
(inherit uncover-call-live-roots-seq)
204-
205-
(define/override (uncover-call-live-roots)
206-
(lambda (x)
207-
(vomit "uncover call live roots" x)
208-
(match x
209-
[`(program ,xs ,ty (defines ,ds ...) ,ss ...)
210-
(let*-values ([(ds) (map (uncover-call-live-roots-def) ds)]
211-
[(ss clr*) ((uncover-call-live-roots-seq (set) xs) ss)])
212-
(unless (set-empty? clr*)
213-
(error 'uncover-call-live-roots
214-
"empty program call live roots invariant ~a" clr*))
215-
`(program ,(append (reset-vars) xs) ,ty (defines ,@ds) ,@ss))]
216-
[else (error 'uncover-call-live-roots "unmatched ~a" x)])))
217-
218-
;; uncover-call-live-roots-def : define -> define
219-
(define/public ((uncover-call-live-roots-def) def)
220-
(match def
221-
[`(define ,(and decl `(,f [,x* : ,p*] ...)) : ,t ,l* ,ss ...)
222-
(define v* (append l* (map cons x* p*)))
223-
(let*-values ([(ss clr*) ((uncover-call-live-roots-seq (set) v*) ss)]
224-
[(clr*) (set-subtract clr* (list->set x*))])
225-
(unless (set-empty? clr*)
226-
(error 'uncover-call-live-roots
227-
"empty define call live roots invariant ~a ~a" f clr*))
228-
`(define ,decl : ,t ,l* ,@ss))]
229-
[else (error 'uncover-call-live-roots-def "unmatched ~a" def)]))
230-
231-
;; uncover-call-live-roots-stmt : stmt (set id) -> stmt (set id)
232-
(define/override (uncover-call-live-roots-stmt stmt clr* xs)
233-
(vomit "functions/uncover-call-live-roots-stmt" stmt clr*)
234-
(match stmt
235-
[`(app ,(app (uncover-call-live-roots-exp xs) clr**) ...)
236-
(values `(call-live-roots ,(set->list clr*) ,stmt)
237-
(set-union clr* (set-union* clr**)))]
238-
[`(assign ,lhs (has-type (app ,e* ...) ,t))
239-
(let* ([clr* (set-remove clr* lhs)]
240-
[stmt `(call-live-roots ,(set->list clr*) ,stmt)]
241-
[clr** (for/list ([e e*]) ((uncover-call-live-roots-exp xs) e))]
242-
[clr* (set-union clr* (set-union* clr**))])
243-
(values stmt clr*))]
244-
[else (super uncover-call-live-roots-stmt stmt clr* xs)]))
245-
246-
;;uncover-call-live-roots-exp : expr -> (set id)
247-
(define/override ((uncover-call-live-roots-exp xs) e)
248-
(vomit "functions/uncover-call-live-roots-exp" e)
249-
(match e
250-
[`(function-ref ,f) (set)]
251-
[else ((super uncover-call-live-roots-exp xs) e)]))
252-
253200
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
254201
;; select-instructions : env -> S3 -> S3
255202

@@ -277,7 +224,7 @@
277224
(append* (map (select-instructions) ss))))
278225
;; parameters become locals
279226
`(define (,f)
280-
,(length xs) (,(append (map cons xs ps) (reset-vars) locals) ,max-stack)
227+
,(length xs) (,(append (map cons xs ps) locals) ,max-stack)
281228
,@new-ss)]
282229
[`(assign ,lhs (has-type (function-ref ,f) ,t))
283230
(define new-lhs ((select-instructions) lhs))
@@ -305,7 +252,7 @@
305252
(set! max-stack 0)
306253
(define new-ss (append* (map (select-instructions) ss)))
307254
`(program
308-
(,(append (reset-vars) locals) ,max-stack)
255+
(,locals ,max-stack)
309256
(type ,ty)
310257
(defines ,@new-ds)
311258
,@new-ss)]
@@ -422,12 +369,14 @@
422369
(define/override (allocate-registers)
423370
(lambda (ast)
424371
(match ast
425-
[`(define (,f) ,n (,xs ,max-stack ,IG ,MG) ,ss ...)
372+
;; FIX ME -Jeremy
373+
#;[`(define (,f) ,n (,xs ,max-stack ,IG ,MG) ,ss ...)
426374
(define-values (homes stk-size)
427375
(allocate-homes IG MG (map car xs) ss))
428376
(define new-ss (map (assign-homes homes) ss))
429377
`(define (,f) ,n ,(align (+ stk-size (* 8 max-stack)) 16) ,@new-ss)]
430-
[`(program (,locals ,max-stack ,IG ,MG) (type ,ty) (defines ,ds ...)
378+
;; FIX ME -Jeremy
379+
#;[`(program (,locals ,max-stack ,IG ,MG) (type ,ty) (defines ,ds ...)
431380
,ss ...)
432381
(define new-ds (map (allocate-registers) ds))
433382
(define-values (homes stk-size)
@@ -568,9 +517,6 @@
568517
,(send interp interp-F '()))
569518
("flatten" ,(send compiler flatten #f)
570519
,(send interp interp-C '()))
571-
("uncover call live roots"
572-
,(send compiler uncover-call-live-roots)
573-
,(send interp interp-C '()))
574520
("instruction selection" ,(send compiler select-instructions)
575521
,(send interp interp-x86 '()))
576522
("liveness analysis" ,(send compiler uncover-live (void))

interp.rkt

+35-48
Original file line numberDiff line numberDiff line change
@@ -406,9 +406,6 @@
406406
tys (range (length tys))))]
407407
[else (super display-by-type ty val)]))
408408

409-
;; Andre, please write a paragraph or so explaining this
410-
;; design for representing the heap. -Jeremy
411-
412409
;; The simulated global state of the program
413410
;; define produces private fields
414411
(define memory (box '()))
@@ -561,10 +558,12 @@
561558
(error 'interp-C "invalid argument to collect in ~a" ast))
562559
(void)]
563560
[`(program (type ,ty) ,e)
561+
((initialize!) runtime-config:rootstack-size
562+
runtime-config:heap-size)
564563
((interp-scheme '()) e)]
565-
[`(initialize ,stack-size ,heap-size)
566-
((initialize!) stack-size heap-size)
567-
(void)]
564+
;; [`(initialize ,stack-size ,heap-size)
565+
;; ((initialize!) stack-size heap-size)
566+
;; (void)]
568567
[else ((super interp-scheme env) ast)]
569568
)))
570569

@@ -600,12 +599,12 @@
600599
[`(global-value fromspace_end)
601600
(unbox fromspace_end)]
602601
;; I should do better than make these noops - andre
603-
[`(initialize ,s ,h)
604-
(unless (and (exact-nonnegative-integer? s)
605-
(exact-nonnegative-integer? h))
606-
(error "intialize must be called with literals"))
607-
((initialize!) s h)
608-
env]
602+
;; [`(initialize ,s ,h)
603+
;; (unless (and (exact-nonnegative-integer? s)
604+
;; (exact-nonnegative-integer? h))
605+
;; (error "intialize must be called with literals"))
606+
;; ((initialize!) s h)
607+
;; env]
609608
;; Determine if a collection is needed.
610609
;; Which it isn't because vectors stored in the environment
611610
;; is the representation of the heap in the C language,
@@ -680,28 +679,6 @@
680679
(when (pair? ast)
681680
(vomit "R2/interp-x86" (car ast)))
682681
(match ast
683-
;; cmpq performs a subq operation and examimines the state
684-
;; of the result, this is done without overwriting the second
685-
;; register. -andre
686-
;; Notice that the syntax is very confusing
687-
;; (cmpq ,s2 ,s1) (jl then) (jmp else) ...
688-
;; (if (< s1 s2) then else)
689-
#;[`((cmpq ,s2 ,s1) . ,ss)
690-
(let* ([v1 ((interp-x86-exp env) s1)]
691-
[v2 ((interp-x86-exp env) s2)]
692-
[v3 (- v2 v1)]
693-
[zero (arithmetic-shift (b2i (eq? v3 0)) 6)]
694-
[sign (arithmetic-shift (b2i (< v3 0)) 7)]
695-
;; Our numbers do not overflow so this bit is always 0
696-
[overflow (arithmetic-shift 0 11)]
697-
[eflags (bitwise-ior overflow sign zero)])
698-
((interp-x86 (cons (cons '__flag eflags) env)) ss))]
699-
;; Initialize the state of the "runtime"
700-
[`((callq initialize) . ,ss)
701-
(define stack-size ((interp-x86-exp env) '(reg rdi)))
702-
(define heap-size ((interp-x86-exp env) '(reg rsi)))
703-
((initialize!) stack-size heap-size)
704-
((interp-x86 env) ss)]
705682
[`((callq malloc) . ,ss)
706683
(define num-bytes ((interp-x86-exp env) '(reg rdi)))
707684
((interp-x86 `((rax . ,(allocate-page! 'malloc num-bytes)) . ,env))
@@ -730,6 +707,18 @@
730707
(define op (interp-x86-op unary-op))
731708
(define new-env ((interp-x86-store env) d (op dst)))
732709
((interp-x86 new-env) ss)]
710+
[`(program (,stack-space ,root-space) (type ,ty) ,ss ...)
711+
#:when (and (integer? stack-space) (integer? root-space))
712+
(define env (cons (cons 'r15 (+ root-space (unbox rootstack_begin)))
713+
'()))
714+
(parameterize ([program ss])
715+
(let ([env^ ((interp-x86 env) ss)])
716+
(display-by-type ty (lookup 'rax env^))))]
717+
[`(program ,xs (type ,ty) ,ss ...)
718+
(define env (cons (cons 'r15 (unbox rootstack_begin)) '()))
719+
(parameterize ([program ss])
720+
(let ([env^ ((interp-x86 env) ss)])
721+
(display-by-type ty (lookup 'rax env^))))]
733722
[else ((super interp-x86 env) ast)])))
734723

735724
));; interp-R2
@@ -741,7 +730,7 @@
741730
(define interp-R3
742731
(class interp-R2
743732
(super-new)
744-
(inherit primitives seq-C display-by-type interp-op)
733+
(inherit primitives seq-C display-by-type interp-op initialize!)
745734
(inherit-field result)
746735

747736
(define/public (non-apply-ast)
@@ -755,10 +744,9 @@
755744
[`(define (,f [,xs : ,ps] ...) : ,rt ,body)
756745
(cons f `(lambda ,xs ,body))]
757746
[`(program (type ,ty) ,ds ... ,body)
747+
((initialize!) runtime-config:rootstack-size
748+
runtime-config:heap-size)
758749
((interp-scheme env) `(program ,@ds ,body))]
759-
[`(program ,ds ... ,body)
760-
(let ([top-level (map (interp-scheme '()) ds)])
761-
((interp-scheme top-level) body))]
762750
[`(,fun ,args ...) #:when (not (set-member? (non-apply-ast) fun))
763751
(define new-args (map (interp-scheme env) args))
764752
(define fun-val ((interp-scheme env) fun))
@@ -841,11 +829,9 @@
841829
(lookup result result-env)]
842830
[else (error "interp-C, expected a funnction, not" f-val)])]
843831
[`(program ,locals (type ,ty) (defines ,ds ...) ,ss ...)
832+
((initialize!) runtime-config:rootstack-size
833+
runtime-config:heap-size)
844834
((interp-C env) `(program ,locals (defines ,@ds) ,@ss))]
845-
[`(program ,locals (defines ,ds ...) ,ss ...)
846-
(define new-env (map (interp-C '()) ds))
847-
(define result-env ((seq-C new-env) ss))
848-
(lookup result result-env)]
849835
[else ((super interp-C env) ast)])))
850836

851837
(define (stack-arg-name n)
@@ -911,13 +897,10 @@
911897
[`((callq ,f) . ,ss) #:when (not (set-member? (builtin-funs) f))
912898
(call-function (lookup f env) ss env)]
913899
[`(program ,extra (type ,ty) (defines ,ds ...) ,ss ...)
900+
((initialize!) runtime-config:rootstack-size
901+
runtime-config:heap-size)
914902
(display-by-type ty ((interp-x86 env)
915903
`(program ,extra (defines ,@ds) ,@ss)))]
916-
[`(program ,extra (defines ,ds ...) ,ss ...)
917-
(parameterize ([program ss])
918-
(define env (map (interp-x86 '()) ds))
919-
(define result-env ((interp-x86 env) ss))
920-
(lookup 'rax result-env))]
921904
[else ((super interp-x86 env) ast)])))
922905

923906
)) ;; end interp-R3
@@ -928,7 +911,7 @@
928911
(define interp-R4
929912
(class interp-R3
930913
(super-new)
931-
(inherit non-apply-ast)
914+
(inherit non-apply-ast initialize!)
932915
(inherit-field result)
933916

934917
(define/override (interp-scheme env)
@@ -940,8 +923,12 @@
940923
[`(define (,f [,xs : ,ps] ...) : ,rt ,body)
941924
(mcons f `(lambda ,xs ,body))]
942925
[`(program (type ,ty) ,ds ... ,body)
926+
((initialize!) runtime-config:rootstack-size
927+
runtime-config:heap-size)
943928
((interp-scheme env) `(program ,@ds ,body))]
944929
[`(program ,ds ... ,body)
930+
((initialize!) runtime-config:rootstack-size
931+
runtime-config:heap-size)
945932
(let ([top-level (map (interp-scheme '()) ds)])
946933
;; Use set-cdr! on define lambda's for mutual recursion
947934
(for/list ([b top-level])

lambda.rkt

-3
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,6 @@
197197
,(send interp interp-F '()))
198198
("flatten" ,(send compiler flatten #f)
199199
,(send interp interp-C '()))
200-
("uncover call live roots"
201-
,(send compiler uncover-call-live-roots)
202-
,(send interp interp-C '()))
203200
("instruction selection" ,(send compiler select-instructions)
204201
,(send interp interp-x86 '()))
205202
("liveness analysis" ,(send compiler uncover-live (void))

0 commit comments

Comments
 (0)