resume.scm 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. ; -*- Mode: Scheme; Syntax: Scheme; Package: Scheme; -*-
  2. ; Copyright (c) 1993-2008 by Richard Kelsey and Jonathan Rees. See file COPYING.
  3. ; This is file resume.scm.
  4. (define (s48-initialize-vm stack-begin stack-size)
  5. (install-symbols!+gc (s48-initial-symbols))
  6. (install-shared-bindings!+gc (s48-initial-imported-bindings)
  7. (s48-initial-exported-bindings))
  8. (initialize-external-events)
  9. (initialize-stack+gc stack-begin stack-size)
  10. (initialize-interpreter+gc)
  11. (initialize-bignums)
  12. (initialize-proposals!+gc))
  13. ;----------------
  14. ; Push the arguments to the initial procedure (a vector of strings passed
  15. ; in from the outside and the three standard channels) and call it.
  16. ; The argument list needs to be in sync with MAKE-USUAL-RESUMER in
  17. ; rts/init.scm, and MAKE-STARTUP-PROCEDURE in bcomp/comp.scm.
  18. (define (s48-call-startup-procedure startup-vector startup-vector-length)
  19. (clear-registers)
  20. (push (enter-startup-argument+gc startup-vector startup-vector-length))
  21. (receive (input input-encoding output output-encoding error error-encoding)
  22. (initialize-i/o-system+gc)
  23. (push input)
  24. (push input-encoding)
  25. (push output)
  26. (push output-encoding)
  27. (push error)
  28. (push error-encoding)
  29. (push (s48-resumer-records))
  30. (s48-initialization-complete!)
  31. (s48-restart (s48-startup-procedure) 8)))
  32. (define (enter-startup-argument+gc startup-vector startup-vector-length)
  33. (let* ((argv-total-bytes-count
  34. (let loop ((i 0) (count 0))
  35. (if (= i startup-vector-length)
  36. count
  37. (goto loop
  38. (+ 1 i)
  39. (+ count (+ (string-length (vector-ref startup-vector i)) 1))))))
  40. (key (ensure-space
  41. (+ stob-overhead startup-vector-length
  42. (* startup-vector-length stob-overhead)
  43. (bytes->cells argv-total-bytes-count))))
  44. (vector (make-d-vector (enum stob vector) startup-vector-length key)))
  45. (natural-for-each (lambda (i)
  46. (vm-vector-set! vector
  47. i
  48. (enter-os-string-byte-vector
  49. (vector-ref startup-vector i)
  50. key)))
  51. startup-vector-length)
  52. vector))
  53. (define (enter-os-string-byte-vector s key)
  54. (let* ((len (string-length s))
  55. (vec (make-code-vector (+ len 1) key))) ; NUL
  56. (do ((i 0 (+ 1 i)))
  57. ((> i len) vec)
  58. (code-vector-set! vec i (char->ascii (string-ref s i))))))
  59. ;----------------
  60. ; Restart the interpreter, calling PROC with NARGS arguments already on the
  61. ; stack.
  62. (define (s48-restart proc nargs)
  63. (cond ((closure? proc)
  64. (set-val! proc)
  65. (let ((retval (perform-application nargs)))
  66. ;; This is necessary to remove the stack from a callback
  67. ;; from C. If we don't do this, a single callback works,
  68. ;; but two in a row fails. I'm not sure if this is the
  69. ;; right place for this fix. --Mike
  70. (remove-current-frame)
  71. retval))
  72. (else
  73. (error "s48-restart called with non-procedure" proc))))