continuation.scm 3.5 KB

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