continuation.scm 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. ; -*- Mode: Scheme; Syntax: Scheme; Package: Scheme; -*-
  2. ; Copyright (c) 1993-2008 by Richard Kelsey and Jonathan Rees. See file COPYING.
  3. ; Continuations
  4. (define (make-ref index)
  5. (lambda (c)
  6. (continuation-ref c index)))
  7. (define continuation-cont (make-ref continuation-cont-index))
  8. (define real-continuation-code (make-ref continuation-code-index))
  9. (define real-continuation-pc (make-ref continuation-pc-index))
  10. (define vm-exception-cont-pc (make-ref exception-cont-pc-index))
  11. (define vm-exception-cont-code (make-ref exception-cont-code-index))
  12. ; This one is exported
  13. (define vm-exception-continuation-exception
  14. (make-ref exception-cont-exception-index))
  15. ; Exception continuations contain the state of the VM when an exception occured.
  16. (define (vm-exception-continuation? thing)
  17. (and (continuation? thing)
  18. (= 13 (real-continuation-pc thing))
  19. (let ((code (real-continuation-code thing)))
  20. (and (= 1 ; one return value
  21. (code-vector-ref code 14))
  22. (= (enum op return-from-exception)
  23. (code-vector-ref code 15))))))
  24. (define (call-with-values-continuation? thing)
  25. (and (continuation? thing)
  26. (= 13 (real-continuation-pc thing))
  27. (= call-with-values-protocol
  28. (code-vector-ref (real-continuation-code thing)
  29. 14))))
  30. (define (continuation-pc c)
  31. (if (vm-exception-continuation? c)
  32. (vm-exception-cont-pc c)
  33. (real-continuation-pc c)))
  34. (define (continuation-code c)
  35. (if (vm-exception-continuation? c)
  36. (vm-exception-cont-code c)
  37. (real-continuation-code c)))
  38. ; This finds the template if it is in the continuation. Not all continuations
  39. ; have templates.
  40. (define (continuation-template c)
  41. (cond
  42. ((and (call-with-values-continuation? c)
  43. (closure? (continuation-arg c 0)))
  44. (closure-template (continuation-arg c 0)))
  45. ((let loop ((i 0))
  46. (if (= i (continuation-length c))
  47. #f
  48. (let ((value (continuation-ref c i)))
  49. (if (and (template? value)
  50. (eq? (template-code value)
  51. (continuation-code c)))
  52. value
  53. (loop (+ i 1)))))))
  54. ;; look among the primops for the template this continuation
  55. ;; belongs to
  56. (else
  57. (let ((code (continuation-code c)))
  58. (let loop ((i (vector-length all-operators)))
  59. (if (zero? i)
  60. #f
  61. (let* ((primitive-proc (vector-ref all-operators (- i 1)))
  62. (primitive-template (closure-template primitive-proc)))
  63. (if (eq? code (template-code primitive-template))
  64. primitive-template
  65. (loop (- i 1))))))))))
  66. ; Accessing the saved operand stack.
  67. (define (continuation-arg c i)
  68. (continuation-ref c (+ continuation-cells
  69. (if (vm-exception-continuation? c)
  70. exception-continuation-cells
  71. 0)
  72. i)))
  73. (define (continuation-arg-count c)
  74. (- (continuation-length c)
  75. (+ continuation-cells
  76. (if (vm-exception-continuation? c)
  77. exception-continuation-cells
  78. 0))))
  79. (define-simple-type :continuation (:value) continuation?)
  80. (define-method &disclose ((obj :continuation))
  81. (list (if (vm-exception-continuation? obj)
  82. 'vm-exception-continuation
  83. 'continuation)
  84. `(pc ,(continuation-pc obj))
  85. (let ((template (continuation-template obj)))
  86. (if template
  87. (template-info template)
  88. '?))))
  89. (define (continuation-preview c)
  90. (if (continuation? c)
  91. (cons (cons (let ((template (continuation-template c)))
  92. (if template
  93. (template-info template)
  94. '?))
  95. (continuation-pc c))
  96. (continuation-preview (continuation-cont c)))
  97. '()))