Add function for initializing heap

This commit is contained in:
Yiyun Liu 2025-05-25 22:46:45 -04:00
parent f3b2d11279
commit 92e42ef26e

View file

@ -10,6 +10,14 @@
(define-type Globals (Immutable-HashTable Name Addr)) (define-type Globals (Immutable-HashTable Name Addr))
(define-type Stats Nonnegative-Integer) (define-type Stats Nonnegative-Integer)
(: empty-heap Heap)
(define empty-heap
(make-immutable-hash))
(: empty-globals Globals)
(define empty-globals
(make-immutable-hash))
(: new-addr (-> Addr)) (: new-addr (-> Addr))
(define (new-addr) (define (new-addr)
(gensym)) (gensym))
@ -23,6 +31,10 @@
(: incr-stats (-> Stats Stats)) (: incr-stats (-> Stats Stats))
(define incr-stats add1) (define incr-stats add1)
(: update-globals (-> Globals Name Addr Globals))
(define (update-globals globals name addr)
(hash-set globals name addr))
(: update-stats (-> (-> Stats Stats) State State)) (: update-stats (-> (-> Stats Stats) State State))
(define (update-stats f mstate) (define (update-stats f mstate)
(struct-copy State mstate [stats (f (State-stats mstate))])) (struct-copy State mstate [stats (f (State-stats mstate))]))
@ -32,12 +44,11 @@
(let ([addr (new-addr)]) (let ([addr (new-addr)])
(values (hash-set heap addr node) addr))) (values (hash-set heap addr node) addr)))
(: allocate-sc (-> Heap CoreScDefn (Values Heap (Pair Name Addr)))) (: initialize-heap (-> (Listof CoreScDefn) (Values Heap Globals)))
(define (allocate-sc heap scdef) (define (initialize-heap scs)
(let-values ([(heap addr) (allocate-node heap scdef)]) (for/fold ([heap : Heap empty-heap]
(values heap (cons (scdefn-name scdef) addr)))) [globals : Globals empty-globals])
([sc : CoreScDefn scs])
(let-values ([(new-heap addr) (allocate-node heap sc)])
;; (: initial-heap (-> (Listof CoreScDefn) (Values Heap Globals))) (values new-heap
;; (define (initial-heap scs) (update-globals globals (scdefn-name sc) addr)))))
;; (for/fold ([heap ])))