prune-top-level-scopes.scm 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. ;;; Continuation-passing style (CPS) intermediate language (IL)
  2. ;; Copyright (C) 2014, 2015, 2017 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 simple pass to prune unneeded top-level scopes.
  19. ;;;
  20. ;;; Code:
  21. (define-module (language cps prune-top-level-scopes)
  22. #:use-module (ice-9 match)
  23. #:use-module (language cps)
  24. #:use-module (language cps utils)
  25. #:use-module (language cps intmap)
  26. #:use-module (language cps intset)
  27. #:export (prune-top-level-scopes))
  28. (define (compute-used-scopes conts)
  29. (persistent-intset
  30. (intmap-fold
  31. (lambda (label cont used-scopes)
  32. (match cont
  33. (($ $kargs _ _
  34. ($ $continue k src
  35. ($ $primcall 'cached-toplevel-box (scope name bound?))))
  36. (intset-add! used-scopes scope))
  37. (_
  38. used-scopes)))
  39. conts
  40. empty-intset)))
  41. (define (prune-top-level-scopes conts)
  42. (let* ((used-scopes (compute-used-scopes conts)))
  43. (intmap-map
  44. (lambda (label cont)
  45. (match cont
  46. (($ $kargs names vars
  47. ($ $continue k src
  48. ($ $primcall 'cache-current-module! (scope-id) (module))))
  49. (if (intset-ref used-scopes scope-id)
  50. cont
  51. (build-cont ($kargs names vars
  52. ($continue k src ($values ()))))))
  53. (_
  54. cont)))
  55. conts)))