compile-bytecode.scm 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. ;;; Guile VM assembler
  2. ;; Copyright (C) 2001, 2009, 2010, 2011 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 assembly compile-bytecode)
  18. #:use-module (system base pmatch)
  19. #:use-module (system base target)
  20. #:use-module (language assembly)
  21. #:use-module (system vm instruction)
  22. #:use-module (rnrs bytevectors)
  23. #:use-module ((srfi srfi-1) #:select (fold))
  24. #:export (compile-bytecode))
  25. (define (compile-bytecode assembly env . opts)
  26. (define-syntax-rule (define-inline1 (proc arg) body body* ...)
  27. (define-syntax proc
  28. (syntax-rules ()
  29. ((_ (arg-expr (... ...)))
  30. (let ((x (arg-expr (... ...))))
  31. (proc x)))
  32. ((_ arg)
  33. (begin body body* ...)))))
  34. (define (fill-bytecode bv target-endianness)
  35. (let ((pos 0))
  36. (define-inline1 (write-byte b)
  37. (bytevector-u8-set! bv pos b)
  38. (set! pos (1+ pos)))
  39. (define u32-bv (make-bytevector 4))
  40. (define-inline1 (write-int24-be x)
  41. (bytevector-s32-set! u32-bv 0 x (endianness big))
  42. (bytevector-u8-set! bv pos (bytevector-u8-ref u32-bv 1))
  43. (bytevector-u8-set! bv (+ pos 1) (bytevector-u8-ref u32-bv 2))
  44. (bytevector-u8-set! bv (+ pos 2) (bytevector-u8-ref u32-bv 3))
  45. (set! pos (+ pos 3)))
  46. (define-inline1 (write-uint32-be x)
  47. (bytevector-u32-set! bv pos x (endianness big))
  48. (set! pos (+ pos 4)))
  49. (define-inline1 (write-uint32 x)
  50. (bytevector-u32-set! bv pos x target-endianness)
  51. (set! pos (+ pos 4)))
  52. (define-inline1 (write-loader-len len)
  53. (bytevector-u8-set! bv pos (ash len -16))
  54. (bytevector-u8-set! bv (+ pos 1) (logand (ash len -8) 255))
  55. (bytevector-u8-set! bv (+ pos 2) (logand len 255))
  56. (set! pos (+ pos 3)))
  57. (define-inline1 (write-latin1-string s)
  58. (let ((len (string-length s)))
  59. (write-loader-len len)
  60. (let lp ((i 0))
  61. (if (< i len)
  62. (begin
  63. (bytevector-u8-set! bv (+ pos i)
  64. (char->integer (string-ref s i)))
  65. (lp (1+ i)))))
  66. (set! pos (+ pos len))))
  67. (define-inline1 (write-bytevector bv*)
  68. (let ((len (bytevector-length bv*)))
  69. (write-loader-len len)
  70. (bytevector-copy! bv* 0 bv pos len)
  71. (set! pos (+ pos len))))
  72. (define-inline1 (write-wide-string s)
  73. (write-bytevector (string->utf32 s target-endianness)))
  74. (define-inline1 (write-break label)
  75. (let ((offset (- (assq-ref labels label) (+ (get-addr) 3))))
  76. (cond ((>= offset (ash 1 23)) (error "jump too far forward" offset))
  77. ((< offset (- (ash 1 23))) (error "jump too far backwards" offset))
  78. (else (write-int24-be offset)))))
  79. (define (write-bytecode asm labels address emit-opcode?)
  80. ;; Write ASM's bytecode to BV. If EMIT-OPCODE? is false, don't
  81. ;; emit bytecode for the first opcode encountered. Assume code
  82. ;; starts at ADDRESS (an integer). LABELS is assumed to be an
  83. ;; alist mapping labels to addresses.
  84. (define get-addr
  85. (let ((start pos))
  86. (lambda ()
  87. (+ address (- pos start)))))
  88. (define (write-break label)
  89. (let ((offset (- (assq-ref labels label) (+ (get-addr) 3))))
  90. (cond ((>= offset (ash 1 23)) (error "jump too far forward" offset))
  91. ((< offset (- (ash 1 23))) (error "jump too far backwards" offset))
  92. (else (write-int24-be offset)))))
  93. (let ((inst (car asm))
  94. (args (cdr asm)))
  95. (let ((opcode (instruction->opcode inst))
  96. (len (instruction-length inst)))
  97. (if emit-opcode?
  98. (write-byte opcode))
  99. (pmatch asm
  100. ((load-program ,labels ,length ,meta . ,code)
  101. (write-uint32 length)
  102. (write-uint32 (if meta (1- (byte-length meta)) 0))
  103. (fold (lambda (asm address)
  104. (let ((start pos))
  105. (write-bytecode asm labels address #t)
  106. (+ address (- pos start))))
  107. 0
  108. code)
  109. (if meta
  110. ;; Don't emit the `load-program' byte for metadata. Note that
  111. ;; META's bytecode meets the alignment requirements of
  112. ;; `scm_objcode', thanks to the alignment computed in `(language
  113. ;; assembly)'.
  114. (write-bytecode meta '() 0 #f)))
  115. ((make-char32 ,x) (write-uint32-be x))
  116. ((load-number ,str) (write-latin1-string str))
  117. ((load-string ,str) (write-latin1-string str))
  118. ((load-wide-string ,str) (write-wide-string str))
  119. ((load-symbol ,str) (write-latin1-string str))
  120. ((load-array ,bv) (write-bytevector bv))
  121. ((br ,l) (write-break l))
  122. ((br-if ,l) (write-break l))
  123. ((br-if-not ,l) (write-break l))
  124. ((br-if-eq ,l) (write-break l))
  125. ((br-if-not-eq ,l) (write-break l))
  126. ((br-if-null ,l) (write-break l))
  127. ((br-if-not-null ,l) (write-break l))
  128. ((br-if-nargs-ne ,hi ,lo ,l) (write-byte hi) (write-byte lo) (write-break l))
  129. ((br-if-nargs-lt ,hi ,lo ,l) (write-byte hi) (write-byte lo) (write-break l))
  130. ((br-if-nargs-gt ,hi ,lo ,l) (write-byte hi) (write-byte lo) (write-break l))
  131. ((mv-call ,n ,l) (write-byte n) (write-break l))
  132. ((prompt ,escape-only? ,l) (write-byte escape-only?) (write-break l))
  133. (else
  134. (cond
  135. ((< len 0)
  136. (error "unhanded variable-length instruction" asm))
  137. ((not (= (length args) len))
  138. (error "bad number of args to instruction" asm len))
  139. (else
  140. (for-each (lambda (x) (write-byte x)) args))))))))
  141. ;; Don't emit the `load-program' byte.
  142. (write-bytecode assembly '() 0 #f)
  143. (if (= pos (bytevector-length bv))
  144. (values bv env env)
  145. (error "failed to fill bytevector" bv pos
  146. (bytevector-length bv)))))
  147. (pmatch assembly
  148. ((load-program ,labels ,length ,meta . ,code)
  149. (fill-bytecode (make-bytevector (+ 4 4 length
  150. (if meta
  151. (1- (byte-length meta))
  152. 0)))
  153. (target-endianness)))
  154. (else (error "bad assembly" assembly))))