cps-primitives.scm 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. ;;; Continuation-passing style (CPS) intermediate language (IL)
  2. ;; Copyright (C) 2013-2015, 2017-2019 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. ;;; Information about named primitives, as they appear in $prim and
  19. ;;; $primcall.
  20. ;;;
  21. ;;; Code:
  22. (define-module (language tree-il cps-primitives)
  23. #:use-module (ice-9 match)
  24. #:use-module (language bytecode)
  25. #:use-module (system base types internal)
  26. #:export (tree-il-primitive->cps-primitive+nargs+nvalues
  27. branching-primitive?
  28. heap-type-predicate?))
  29. (define *primitives* (make-hash-table))
  30. (define-syntax define-cps-primitive
  31. (syntax-rules ()
  32. ((_ (tree-il-primitive cps-primitive) nargs nvalues)
  33. (hashq-set! *primitives* 'tree-il-primitive
  34. '#(cps-primitive nargs nvalues)))
  35. ((_ primitive nargs nvalues)
  36. (define-cps-primitive (primitive primitive) nargs nvalues))))
  37. ;; tree-il-prim -> #(cps-prim nargs nvalues) | #f
  38. (define (tree-il-primitive->cps-primitive+nargs+nvalues name)
  39. (hashq-ref *primitives* name))
  40. (define-cps-primitive box 1 1)
  41. (define-cps-primitive (variable-ref box-ref) 1 1)
  42. (define-cps-primitive (variable-set! box-set!) 2 0)
  43. (define-cps-primitive (%variable-ref %box-ref) 1 1)
  44. (define-cps-primitive (%variable-set! %box-set!) 2 0)
  45. (define-cps-primitive current-module 0 1)
  46. (define-cps-primitive (module-ensure-local-variable! define!) 2 1)
  47. (define-cps-primitive wind 2 0)
  48. (define-cps-primitive unwind 0 0)
  49. (define-cps-primitive push-dynamic-state 1 0)
  50. (define-cps-primitive pop-dynamic-state 0 0)
  51. (define-cps-primitive push-fluid 2 0)
  52. (define-cps-primitive pop-fluid 0 0)
  53. (define-cps-primitive fluid-ref 1 1)
  54. (define-cps-primitive fluid-set! 2 0)
  55. (define-cps-primitive string-length 1 1)
  56. (define-cps-primitive string-ref 2 1)
  57. (define-cps-primitive string-set! 3 0)
  58. (define-cps-primitive string->number 1 1)
  59. (define-cps-primitive string->symbol 1 1)
  60. (define-cps-primitive symbol->keyword 1 1)
  61. (define-cps-primitive integer->char 1 1)
  62. (define-cps-primitive char->integer 1 1)
  63. (define-cps-primitive cons 2 1)
  64. (define-cps-primitive car 1 1)
  65. (define-cps-primitive cdr 1 1)
  66. (define-cps-primitive set-car! 2 0)
  67. (define-cps-primitive set-cdr! 2 0)
  68. (define-cps-primitive (+ add) 2 1)
  69. (define-cps-primitive (- sub) 2 1)
  70. (define-cps-primitive (* mul) 2 1)
  71. (define-cps-primitive (/ div) 2 1)
  72. (define-cps-primitive (quotient quo) 2 1)
  73. (define-cps-primitive (remainder rem) 2 1)
  74. (define-cps-primitive (modulo mod) 2 1)
  75. (define-cps-primitive (exact->inexact inexact) 1 1)
  76. (define-cps-primitive sqrt 1 1)
  77. (define-cps-primitive abs 1 1)
  78. (define-cps-primitive floor 1 1)
  79. (define-cps-primitive ceiling 1 1)
  80. (define-cps-primitive sin 1 1)
  81. (define-cps-primitive cos 1 1)
  82. (define-cps-primitive tan 1 1)
  83. (define-cps-primitive asin 1 1)
  84. (define-cps-primitive acos 1 1)
  85. (define-cps-primitive atan 1 1)
  86. (define-cps-primitive atan2 2 1)
  87. (define-cps-primitive lsh 2 1)
  88. (define-cps-primitive rsh 2 1)
  89. (define-cps-primitive logand 2 1)
  90. (define-cps-primitive logior 2 1)
  91. (define-cps-primitive logxor 2 1)
  92. (define-cps-primitive logsub 2 1)
  93. (define-cps-primitive logbit? 2 1)
  94. (define-cps-primitive allocate-vector 1 1)
  95. (define-cps-primitive make-vector 2 1)
  96. (define-cps-primitive vector-length 1 1)
  97. (define-cps-primitive vector-ref 2 1)
  98. (define-cps-primitive vector-set! 3 0)
  99. (define-cps-primitive vector-init! 3 0)
  100. (define-cps-primitive struct-vtable 1 1)
  101. (define-cps-primitive allocate-struct 2 1)
  102. (define-cps-primitive struct-ref 2 1)
  103. ;; Unhappily, and undocumentedly, struct-set! returns the value that was
  104. ;; set. There is code that relies on this. The struct-set! lowering
  105. ;; routines ensure this return arity.
  106. (define-cps-primitive struct-set! 3 1)
  107. (define-cps-primitive struct-init! 3 0)
  108. (define-cps-primitive class-of 1 1)
  109. (define-cps-primitive (bytevector-length bv-length) 1 1)
  110. (define-cps-primitive (bytevector-u8-ref bv-u8-ref) 2 1)
  111. (define-cps-primitive (bytevector-u16-native-ref bv-u16-ref) 2 1)
  112. (define-cps-primitive (bytevector-u32-native-ref bv-u32-ref) 2 1)
  113. (define-cps-primitive (bytevector-u64-native-ref bv-u64-ref) 2 1)
  114. (define-cps-primitive (bytevector-s8-ref bv-s8-ref) 2 1)
  115. (define-cps-primitive (bytevector-s16-native-ref bv-s16-ref) 2 1)
  116. (define-cps-primitive (bytevector-s32-native-ref bv-s32-ref) 2 1)
  117. (define-cps-primitive (bytevector-s64-native-ref bv-s64-ref) 2 1)
  118. (define-cps-primitive (bytevector-ieee-single-native-ref bv-f32-ref) 2 1)
  119. (define-cps-primitive (bytevector-ieee-double-native-ref bv-f64-ref) 2 1)
  120. (define-cps-primitive (bytevector-u8-set! bv-u8-set!) 3 0)
  121. (define-cps-primitive (bytevector-u16-native-set! bv-u16-set!) 3 0)
  122. (define-cps-primitive (bytevector-u32-native-set! bv-u32-set!) 3 0)
  123. (define-cps-primitive (bytevector-u64-native-set! bv-u64-set!) 3 0)
  124. (define-cps-primitive (bytevector-s8-set! bv-s8-set!) 3 0)
  125. (define-cps-primitive (bytevector-s16-native-set! bv-s16-set!) 3 0)
  126. (define-cps-primitive (bytevector-s32-native-set! bv-s32-set!) 3 0)
  127. (define-cps-primitive (bytevector-s64-native-set! bv-s64-set!) 3 0)
  128. (define-cps-primitive (bytevector-ieee-single-native-set! bv-f32-set!) 3 0)
  129. (define-cps-primitive (bytevector-ieee-double-native-set! bv-f64-set!) 3 0)
  130. (define-cps-primitive current-thread 0 1)
  131. (define-cps-primitive make-atomic-box 1 1)
  132. (define-cps-primitive atomic-box-ref 1 1)
  133. (define-cps-primitive atomic-box-set! 2 0)
  134. (define-cps-primitive atomic-box-swap! 2 1)
  135. (define-cps-primitive atomic-box-compare-and-swap! 3 1)
  136. (define *branching-primitive-arities* (make-hash-table))
  137. (define-syntax-rule (define-branching-primitive name nargs)
  138. (hashq-set! *branching-primitive-arities* 'name '(0 . nargs)))
  139. (define-syntax-rule (define-immediate-type-predicate name pred mask tag)
  140. (define-branching-primitive pred 1))
  141. (define *heap-type-predicates* (make-hash-table))
  142. (define-syntax-rule (define-heap-type-predicate name pred mask tag)
  143. (begin
  144. (hashq-set! *heap-type-predicates* 'pred #t)
  145. (define-branching-primitive pred 1)))
  146. (visit-immediate-tags define-immediate-type-predicate)
  147. (visit-heap-tags define-heap-type-predicate)
  148. (define (branching-primitive? name)
  149. "Is @var{name} a primitive that can only appear in $branch CPS terms?"
  150. (hashq-ref *branching-primitive-arities* name))
  151. (define (heap-type-predicate? name)
  152. "Is @var{name} a predicate that needs guarding by @code{heap-object?}
  153. before it is lowered to CPS?"
  154. (hashq-ref *heap-type-predicates* name))
  155. ;; We only need to define those branching primitives that are used as
  156. ;; Tree-IL primitives. There are others like u64-= which are emitted by
  157. ;; CPS code.
  158. (define-branching-primitive eq? 2)
  159. (define-branching-primitive heap-numbers-equal? 2)
  160. (define-branching-primitive < 2)
  161. (define-branching-primitive <= 2)
  162. (define-branching-primitive = 2)