trying to expose call/cc
This commit is contained in:
parent
da3568d3d2
commit
f3a5728f89
69
compile.rkt
69
compile.rkt
|
@ -9,18 +9,27 @@
|
||||||
"sets.rkt"
|
"sets.rkt"
|
||||||
racket/list)
|
racket/list)
|
||||||
|
|
||||||
(provide (rename-out [-compile compile]))
|
(provide (rename-out [-compile compile])
|
||||||
|
compile-procedure-call
|
||||||
|
append-instruction-sequences
|
||||||
|
|
||||||
|
call/cc-label)
|
||||||
|
|
||||||
;(provide compile-top)
|
;(provide compile-top)
|
||||||
|
|
||||||
(: -compile (ExpressionCore Target Linkage -> (Listof Statement)))
|
(: -compile (ExpressionCore Target Linkage -> (Listof Statement)))
|
||||||
(define (-compile exp target linkage)
|
(define (-compile exp target linkage)
|
||||||
(statements
|
(statements
|
||||||
(compile (make-Top (make-Prefix (find-toplevel-variables exp))
|
(let ([end (make-label 'end)])
|
||||||
exp)
|
(append-instruction-sequences
|
||||||
(list)
|
(compile (make-Top (make-Prefix (find-toplevel-variables exp))
|
||||||
target
|
exp)
|
||||||
linkage)))
|
(list)
|
||||||
|
target
|
||||||
|
linkage)
|
||||||
|
(make-instruction-sequence `(,(make-GotoStatement (make-Label end))))
|
||||||
|
(make-call/cc-code)
|
||||||
|
end))))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -275,6 +284,7 @@
|
||||||
(compile (Lam-body exp) extended-cenv 'val 'return))))
|
(compile (Lam-body exp) extended-cenv 'val 'return))))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#;(: compile-letrec (Letrec CompileTimeEnvironment Target Linkage -> InstructionSequence))
|
#;(: compile-letrec (Letrec CompileTimeEnvironment Target Linkage -> InstructionSequence))
|
||||||
#;(define (compile-letrec exp cenv target linkage)
|
#;(define (compile-letrec exp cenv target linkage)
|
||||||
(let* ([after-let (make-label 'afterLet)]
|
(let* ([after-let (make-label 'afterLet)]
|
||||||
|
@ -380,13 +390,13 @@
|
||||||
InstructionSequence))
|
InstructionSequence))
|
||||||
;; Assumes the procedure value has been loaded into the proc register.
|
;; Assumes the procedure value has been loaded into the proc register.
|
||||||
;; n is the number of arguments passed in.
|
;; n is the number of arguments passed in.
|
||||||
|
;; cenv is the compile-time enviroment before arguments have been shifted in.
|
||||||
|
;; extended-cenv is the compile-time environment after arguments have been shifted in.
|
||||||
(define (compile-procedure-call cenv extended-cenv n target linkage)
|
(define (compile-procedure-call cenv extended-cenv n target linkage)
|
||||||
(let ([primitive-branch (make-label 'primitiveBranch)]
|
(let ([primitive-branch (make-label 'primitiveBranch)]
|
||||||
[compiled-branch (make-label 'compiledBranch)]
|
[compiled-branch (make-label 'compiledBranch)]
|
||||||
[after-call (make-label 'afterCall)])
|
[after-call (make-label 'afterCall)])
|
||||||
(let ([compiled-linkage
|
(let ([compiled-linkage (if (eq? linkage 'next) after-call linkage)])
|
||||||
(if (eq? linkage 'next) after-call linkage)])
|
|
||||||
|
|
||||||
(append-instruction-sequences
|
(append-instruction-sequences
|
||||||
(make-instruction-sequence
|
(make-instruction-sequence
|
||||||
`(,(make-TestAndBranchStatement 'primitive-procedure?
|
`(,(make-TestAndBranchStatement 'primitive-procedure?
|
||||||
|
@ -535,4 +545,43 @@
|
||||||
(define (ensure-natural n)
|
(define (ensure-natural n)
|
||||||
(if (>= n 0)
|
(if (>= n 0)
|
||||||
n
|
n
|
||||||
(error 'ensure-natural "Not a natural: ~s\n" n)))
|
(error 'ensure-natural "Not a natural: ~s\n" n)))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
|
||||||
|
|
||||||
|
;; The primitive code necessary to do call/cc
|
||||||
|
|
||||||
|
(: call/cc-label Symbol)
|
||||||
|
(define call/cc-label 'callCCEntry)
|
||||||
|
|
||||||
|
;; (call/cc f)
|
||||||
|
;; Tail-calls f, providing it a special object that knows how to do the low-level
|
||||||
|
;; manipulations.
|
||||||
|
|
||||||
|
(define (make-call/cc-code)
|
||||||
|
(append-instruction-sequences
|
||||||
|
(make-instruction-sequence
|
||||||
|
`(,call/cc-label
|
||||||
|
;; Precondition: the environment holds the f function that we want to jump into.
|
||||||
|
|
||||||
|
;; First, move f to the proc register
|
||||||
|
,(make-AssignImmediateStatement 'proc (make-EnvLexicalReference 0))
|
||||||
|
|
||||||
|
;; Next, capture the current continuation closure and target it into env[0].
|
||||||
|
;; FIXME!
|
||||||
|
,(make-AssignImmediateStatement (make-EnvLexicalReference 0) (make-Const "I should be a continuation"))))
|
||||||
|
|
||||||
|
;; Finally, tail call into f.
|
||||||
|
(compile-procedure-call (extend-lexical-environment/placeholders '() 1)
|
||||||
|
(extend-lexical-environment/placeholders '() 1)
|
||||||
|
1
|
||||||
|
'val
|
||||||
|
'return)))
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
#lang racket/base
|
#lang racket/base
|
||||||
(require "simulator-structs.rkt"
|
(require "simulator-structs.rkt"
|
||||||
|
"compile.rkt"
|
||||||
racket/math
|
racket/math
|
||||||
(for-syntax racket/base))
|
(for-syntax racket/base))
|
||||||
|
|
||||||
|
@ -28,14 +29,16 @@
|
||||||
(make-undefined)]
|
(make-undefined)]
|
||||||
)))))]))
|
)))))]))
|
||||||
|
|
||||||
#;(define my-callcc
|
(define my-callcc
|
||||||
(make-primitive-proc
|
(make-closure call/cc-label
|
||||||
(lambda (machine return-label k)
|
1
|
||||||
(make-primitive-proc (lambda (m2 r2 k2)
|
'()))
|
||||||
...)))))
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
(define e (exp 1))
|
(define e (exp 1))
|
||||||
|
|
||||||
(define lookup-primitive (make-lookup #:functions (+ - * / = < <= > >= cons list car cdr
|
(define lookup-primitive (make-lookup #:functions (+ - * / = < <= > >= cons list car cdr
|
|
@ -11,7 +11,7 @@
|
||||||
racket/match
|
racket/match
|
||||||
(for-syntax racket/base))
|
(for-syntax racket/base))
|
||||||
|
|
||||||
(require/typed "simulator-prims.rkt"
|
(require/typed "simulator-primitives.rkt"
|
||||||
[lookup-primitive (Symbol -> PrimitiveValue)])
|
[lookup-primitive (Symbol -> PrimitiveValue)])
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
(require "il-structs.rkt"
|
(require "il-structs.rkt"
|
||||||
"simulator-structs.rkt"
|
"simulator-structs.rkt"
|
||||||
"simulator-prims.rkt"
|
"simulator-primitives.rkt"
|
||||||
"simulator.rkt")
|
"simulator.rkt")
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user