primitives.scm 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. ;;; Continuation-passing style (CPS) intermediate language (IL)
  2. ;; Copyright (C) 2013, 2014, 2015 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 cps primitives)
  23. #:use-module (ice-9 match)
  24. #:use-module ((srfi srfi-1) #:select (fold))
  25. #:use-module (srfi srfi-26)
  26. #:use-module (language bytecode)
  27. #:export (prim-instruction
  28. branching-primitive?
  29. prim-arity
  30. ))
  31. (define *instruction-aliases*
  32. '((+ . add)
  33. (- . sub)
  34. (* . mul)
  35. (/ . div)
  36. (quotient . quo) (remainder . rem)
  37. (modulo . mod)
  38. (variable-ref . box-ref)
  39. (variable-set! . box-set!)
  40. (bytevector-length . bv-length)
  41. (bytevector-u8-ref . bv-u8-ref)
  42. (bytevector-u16-native-ref . bv-u16-ref)
  43. (bytevector-u32-native-ref . bv-u32-ref)
  44. (bytevector-u64-native-ref . bv-u64-ref)
  45. (bytevector-s8-ref . bv-s8-ref)
  46. (bytevector-s16-native-ref . bv-s16-ref)
  47. (bytevector-s32-native-ref . bv-s32-ref)
  48. (bytevector-s64-native-ref . bv-s64-ref)
  49. (bytevector-ieee-single-native-ref . bv-f32-ref)
  50. (bytevector-ieee-double-native-ref . bv-f64-ref)
  51. (bytevector-u8-set! . bv-u8-set!)
  52. (bytevector-u16-native-set! . bv-u16-set!)
  53. (bytevector-u32-native-set! . bv-u32-set!)
  54. (bytevector-u64-native-set! . bv-u64-set!)
  55. (bytevector-s8-set! . bv-s8-set!)
  56. (bytevector-s16-native-set! . bv-s16-set!)
  57. (bytevector-s32-native-set! . bv-s32-set!)
  58. (bytevector-s64-native-set! . bv-s64-set!)
  59. (bytevector-ieee-single-native-set! . bv-f32-set!)
  60. (bytevector-ieee-double-native-set! . bv-f64-set!)))
  61. (define *macro-instruction-arities*
  62. '((cache-current-module! . (0 . 2))
  63. (cached-toplevel-box . (1 . 3))
  64. (cached-module-box . (1 . 4))))
  65. (define *branching-primcall-arities*
  66. '((null? . (1 . 1))
  67. (nil? . (1 . 1))
  68. (pair? . (1 . 1))
  69. (struct? . (1 . 1))
  70. (string? . (1 . 1))
  71. (vector? . (1 . 1))
  72. (symbol? . (1 . 1))
  73. (keyword? . (1 . 1))
  74. (variable? . (1 . 1))
  75. (bitvector? . (1 . 1))
  76. (bytevector? . (1 . 1))
  77. (char? . (1 . 1))
  78. (eq? . (1 . 2))
  79. (eqv? . (1 . 2))
  80. (= . (1 . 2))
  81. (< . (1 . 2))
  82. (> . (1 . 2))
  83. (<= . (1 . 2))
  84. (>= . (1 . 2))
  85. (u64-= . (1 . 2))
  86. (u64-< . (1 . 2))
  87. (u64-> . (1 . 2))
  88. (u64-<= . (1 . 2))
  89. (u64->= . (1 . 2))
  90. (u64-<-scm . (1 . 2))
  91. (u64-<=-scm . (1 . 2))
  92. (u64-=-scm . (1 . 2))
  93. (u64->=-scm . (1 . 2))
  94. (u64->-scm . (1 . 2))
  95. (logtest . (1 . 2))))
  96. (define (compute-prim-instructions)
  97. (let ((table (make-hash-table)))
  98. (for-each
  99. (match-lambda ((inst . _) (hashq-set! table inst inst)))
  100. (instruction-list))
  101. (for-each
  102. (match-lambda ((prim . inst) (hashq-set! table prim inst)))
  103. *instruction-aliases*)
  104. (for-each
  105. (match-lambda ((inst . arity) (hashq-set! table inst inst)))
  106. *macro-instruction-arities*)
  107. table))
  108. (define *prim-instructions* (delay (compute-prim-instructions)))
  109. ;; prim -> instruction | #f
  110. (define (prim-instruction name)
  111. (hashq-ref (force *prim-instructions*) name))
  112. (define (branching-primitive? name)
  113. (and (assq name *branching-primcall-arities*) #t))
  114. (define *prim-arities* (make-hash-table))
  115. (define (prim-arity name)
  116. (or (hashq-ref *prim-arities* name)
  117. (let ((arity (cond
  118. ((prim-instruction name) => instruction-arity)
  119. ((assq name *branching-primcall-arities*) => cdr)
  120. (else
  121. (error "Primitive of unknown arity" name)))))
  122. (hashq-set! *prim-arities* name arity)
  123. arity)))