bytecode.scm 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. ;;; Bytecode
  2. ;; Copyright (C) 2013 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. (load-extension (string-append "libguile-" (effective-version))
  25. "scm_init_instructions")
  26. (load-extension (string-append "libguile-" (effective-version))
  27. "scm_init_vm_builtins")
  28. (define (compute-instruction-arity name args)
  29. (define (first-word-arity word)
  30. (case word
  31. ((X32) 0)
  32. ((X8_S24) 1)
  33. ((X8_F24) 1)
  34. ((X8_C24) 1)
  35. ((X8_L24) 1)
  36. ((X8_S8_I16) 2)
  37. ((X8_S12_S12) 2)
  38. ((X8_S12_C12) 2)
  39. ((X8_C12_C12) 2)
  40. ((X8_F12_F12) 2)
  41. ((X8_S8_S8_S8) 3)
  42. ((X8_S8_S8_C8) 3)
  43. ((X8_S8_C8_S8) 3)))
  44. (define (tail-word-arity word)
  45. (case word
  46. ((C32) 1)
  47. ((I32) 1)
  48. ((A32 AU32 AS32 AF32) 1)
  49. ((B32 BF32 BS32 BU32) 0)
  50. ((N32) 1)
  51. ((R32) 1)
  52. ((L32) 1)
  53. ((LO32) 1)
  54. ((C8_C24) 2)
  55. ((B1_C7_L24) 3)
  56. ((B1_X7_S24) 2)
  57. ((B1_X7_F24) 2)
  58. ((B1_X7_C24) 2)
  59. ((B1_X7_L24) 2)
  60. ((B1_X31) 1)
  61. ((X8_S24) 1)
  62. ((X8_F24) 1)
  63. ((X8_C24) 1)
  64. ((X8_L24) 1)))
  65. (match args
  66. ((arg0 . args)
  67. (fold (lambda (arg arity)
  68. (+ (tail-word-arity arg) arity))
  69. (first-word-arity arg0)
  70. args))))
  71. (define *macro-instruction-arities*
  72. '((cache-current-module! . (0 . 2))
  73. (cached-toplevel-box . (1 . 3))
  74. (cached-module-box . (1 . 4))))
  75. (define (compute-instruction-arities)
  76. (let ((table (make-hash-table)))
  77. (for-each
  78. (match-lambda
  79. ;; Put special cases here.
  80. ((name op '! . args)
  81. (hashq-set! table name
  82. (cons 0 (compute-instruction-arity name args))))
  83. ((name op '<- . args)
  84. (hashq-set! table name
  85. (cons 1 (1- (compute-instruction-arity name args))))))
  86. (instruction-list))
  87. (for-each (match-lambda
  88. ((name . arity)
  89. (hashq-set! table name arity)))
  90. *macro-instruction-arities*)
  91. table))
  92. (define *instruction-arities* (delay (compute-instruction-arities)))
  93. (define (instruction-arity name)
  94. (hashq-ref (force *instruction-arities*) name))