vm-exception.scm 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. ; A VM exception is an unusual situation detected by the virtual machine.
  5. ; Usual exception handler vector.
  6. (define (define-vm-exception-handler opcode proc)
  7. (vector-set! vm-exception-handlers opcode proc))
  8. (define signal-condition (unspecific))
  9. (define (signal-vm-exception opcode reason . args)
  10. (signal-condition
  11. (condition (make-vm-exception opcode
  12. (if reason
  13. (enumerand->name reason exception)
  14. #f))
  15. (make-message-condition (vm-exception-reason->message reason))
  16. (make-who-condition (enumerand->name opcode op))
  17. (make-assertion-violation) ; when in doubt
  18. (make-irritants-condition args))))
  19. (define (vm-exception-reason->message reason)
  20. (if (not reason)
  21. "unknown VM exception"
  22. (enum-case exception reason
  23. ((unassigned-local)
  24. "LETREC variable used before its value has been produced")
  25. ((undefined-global) "undefined global variable")
  26. ((unbound-global) "unbound global variable")
  27. ((bad-procedure) "attempt to call a non-procedure")
  28. ((wrong-number-of-arguments) "wrong number of arguments")
  29. ((wrong-type-argument) "argument of wrong type")
  30. ((immutable-argument) "immutable argument")
  31. ((arithmetic-overflow) "arithmetic overflow")
  32. ((index-out-of-range) "index out of range")
  33. ((heap-overflow) "heap overflow")
  34. ((out-of-memory) "out of memory")
  35. ((cannot-open-channel) "cannot open channel")
  36. ((channel-os-index-already-in-use) "OS index of channel already in use")
  37. ((closed-channel) "channel closed")
  38. ((buffer-full/empty) "buffer full or empty")
  39. ((unimplemented-instruction) "unimplemented instruction")
  40. ((trap) "VM trap")
  41. ((proceeding-after-exception) "proceeding after exception")
  42. ((bad-option) "bad option")
  43. ((unbound-external-name) "unbound external name")
  44. ((too-many-arguments-to-external-procedure)
  45. "too many arguments to externalprocedure")
  46. ((too-many-arguments-in-callback) "too many arguments in callback")
  47. ((callback-return-uncovered) "uncovered callback return")
  48. ((extension-exception) "exception in VM extension")
  49. ((extension-return-error) "return error in VM extension")
  50. ((os-error external-os-error) "OS error")
  51. ((gc-protection-mismatch) "GC protection mismatch in external code")
  52. ((no-current-proposal) "no current proposal")
  53. ((native-code-not-supported) "native code not supported")
  54. ((illegal-exception-return) "illegal return from exception")
  55. ((external-error) "error in external code")
  56. ((external-assertion-violation) "assertion violation in external code")
  57. ((external-os-error) "OS error in external code")
  58. (else "unknown VM exception"))))
  59. (define vm-exception-handlers
  60. (make-vector op-count signal-vm-exception))
  61. (define (initialize-vm-exceptions! the-signal-condition)
  62. (set! signal-condition the-signal-condition)
  63. (set-exception-handlers! vm-exception-handlers))
  64. ; TRAP is the same as SIGNAL-CONDITION.
  65. (define-vm-exception-handler (enum op trap)
  66. (lambda (opcode reason arg)
  67. (signal-condition arg)))
  68. ; The time opcode sometimes needs a little help.
  69. (define-vm-exception-handler (enum op time)
  70. (lambda (opcode reason option arg0 . maybe-arg1)
  71. (if (= reason (enum exception arithmetic-overflow))
  72. (+ (* arg0 1000) ; seconds
  73. (car maybe-arg1)) ; milliseconds
  74. (apply signal-vm-exception opcode reason option arg0 maybe-arg1))))
  75. ; This is for generic arithmetic, mostly
  76. (define (extend-opcode! opcode make-handler)
  77. (let* ((except (lambda (reason . args)
  78. (apply signal-vm-exception opcode reason
  79. args)))
  80. (handler (make-handler except)))
  81. (define-vm-exception-handler opcode
  82. (lambda (opcode reason . args)
  83. (apply handler args)))))