bytecode.scm 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. ;;; Bytecode
  2. ;; Copyright (C) 2013, 2017, 2018 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. ;;; Code:
  17. (define-module (language bytecode)
  18. #:use-module (ice-9 match)
  19. #:use-module ((srfi srfi-1) #:select (fold))
  20. #:export (instruction-list
  21. instruction-arity
  22. builtin-name->index
  23. builtin-index->name
  24. intrinsic-name->index
  25. intrinsic-index->name))
  26. (load-extension (string-append "libguile-" (effective-version))
  27. "scm_init_instructions")
  28. (load-extension (string-append "libguile-" (effective-version))
  29. "scm_init_vm_builtins")
  30. (load-extension (string-append "libguile-" (effective-version))
  31. "scm_init_intrinsics")
  32. (define (compute-instruction-arity name args)
  33. (define (first-word-arity word)
  34. (case word
  35. ((X32) 0)
  36. ((X8_S24) 1)
  37. ((X8_F24) 1)
  38. ((X8_C24) 1)
  39. ((X8_L24) 1)
  40. ((X8_S8_I16) 2)
  41. ((X8_S12_S12) 2)
  42. ((X8_S12_C12) 2)
  43. ((X8_S12_Z12) 2)
  44. ((X8_C12_C12) 2)
  45. ((X8_F12_F12) 2)
  46. ((X8_S8_S8_S8) 3)
  47. ((X8_S8_S8_C8) 3)
  48. ((X8_S8_C8_S8) 3)))
  49. (define (tail-word-arity word)
  50. (case word
  51. ((C32) 1)
  52. ((I32) 1)
  53. ((A32 AU32 AS32 AF32) 1)
  54. ((B32 BF32 BS32 BU32) 0)
  55. ((N32) 1)
  56. ((R32) 1)
  57. ((L32) 1)
  58. ((LO32) 1)
  59. ((C8_C24) 2)
  60. ((C8_S24) 2)
  61. ((C16_C16) 2)
  62. ((B1_C7_L24) 3)
  63. ((B1_X7_S24) 2)
  64. ((B1_X7_F24) 2)
  65. ((B1_X7_C24) 2)
  66. ((B1_X7_L24) 2)
  67. ((B1_X31) 1)
  68. ((X8_S24) 1)
  69. ((X8_F24) 1)
  70. ((X8_C24) 1)
  71. ((X8_L24) 1)))
  72. (match args
  73. ((arg0 . args)
  74. (fold (lambda (arg arity)
  75. (+ (tail-word-arity arg) arity))
  76. (first-word-arity arg0)
  77. args))))
  78. (define *macro-instruction-arities*
  79. '((cache-current-module! . (0 . 1))
  80. (cached-toplevel-box . (1 . 0))
  81. (cached-module-box . (1 . 0))))
  82. (define (compute-instruction-arities)
  83. (let ((table (make-hash-table)))
  84. (for-each
  85. (match-lambda
  86. ;; Put special cases here.
  87. ((name op '! . args)
  88. (hashq-set! table name
  89. (cons 0 (compute-instruction-arity name args))))
  90. ((name op '<- . args)
  91. (hashq-set! table name
  92. (cons 1 (1- (compute-instruction-arity name args))))))
  93. (instruction-list))
  94. (for-each (match-lambda
  95. ((name . arity)
  96. (hashq-set! table name arity)))
  97. *macro-instruction-arities*)
  98. table))
  99. (define *instruction-arities* (delay (compute-instruction-arities)))
  100. (define (instruction-arity name)
  101. (hashq-ref (force *instruction-arities*) name))
  102. (define *intrinsic-codes*
  103. (delay (let ((tab (make-hash-table)))
  104. (for-each (lambda (pair)
  105. (hashv-set! tab (car pair) (cdr pair)))
  106. (intrinsic-list))
  107. tab)))
  108. (define *intrinsic-names*
  109. (delay (let ((tab (make-hash-table)))
  110. (hash-for-each (lambda (k v) (hashq-set! tab v k))
  111. (force *intrinsic-codes*))
  112. tab)))
  113. (define (intrinsic-name->index name)
  114. (hashq-ref (force *intrinsic-codes*) name))
  115. (define (intrinsic-index->name index)
  116. (hashv-ref (force *intrinsic-names*) index))