handle-interrupts.scm 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. ;;; Continuation-passing style (CPS) intermediate language (IL)
  2. ;; Copyright (C) 2016 Free Software Foundation, Inc.
  3. ;;;; This library is free software; you can redistribute it and/or
  4. ;;;; modify it under the terms of the GNU Lesser General Public
  5. ;;;; License as published by the Free Software Foundation; either
  6. ;;;; version 3 of the License, or (at your option) any later version.
  7. ;;;;
  8. ;;;; This library is distributed in the hope that it will be useful,
  9. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. ;;;; Lesser General Public License for more details.
  12. ;;;;
  13. ;;;; You should have received a copy of the GNU Lesser General Public
  14. ;;;; License along with this library; if not, write to the Free Software
  15. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16. ;;; Commentary:
  17. ;;;
  18. ;;; A pass to add "handle-interrupts" primcalls before calls, loop
  19. ;;; back-edges, and returns.
  20. ;;;
  21. ;;; Code:
  22. (define-module (language cps handle-interrupts)
  23. #:use-module (ice-9 match)
  24. #:use-module (language cps)
  25. #:use-module (language cps utils)
  26. #:use-module (language cps with-cps)
  27. #:use-module (language cps intmap)
  28. #:use-module (language cps renumber)
  29. #:export (add-handle-interrupts))
  30. (define (add-handle-interrupts cps)
  31. (define (visit-cont label cont cps)
  32. (match cont
  33. (($ $kargs names vars ($ $continue k src exp))
  34. (if (or (<= k label)
  35. (match exp
  36. (($ $call) #t)
  37. (($ $callk) #t)
  38. (($ $values)
  39. (match (intmap-ref cps k)
  40. (($ $ktail) #t)
  41. (_ #f)))
  42. (_ #f)))
  43. (with-cps cps
  44. (letk k* ($kargs () () ($continue k src ,exp)))
  45. (setk label
  46. ($kargs names vars
  47. ($continue k* src
  48. ($primcall 'handle-interrupts ())))))
  49. cps))
  50. (_ cps)))
  51. (let ((cps (renumber cps)))
  52. (with-fresh-name-state cps
  53. (persistent-intmap (intmap-fold visit-cont cps cps)))))