assembly.scm 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. ;;; Guile Virtual Machine Assembly
  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)
  18. #:use-module (rnrs bytevectors)
  19. #:use-module (system base pmatch)
  20. #:use-module (system vm instruction)
  21. #:use-module ((srfi srfi-1) #:select (fold))
  22. #:export (byte-length
  23. addr+ align-program align-code align-block
  24. assembly-pack assembly-unpack
  25. object->assembly assembly->object))
  26. ;; len, metalen
  27. (define *program-header-len* (+ 4 4))
  28. ;; lengths are encoded in 3 bytes
  29. (define *len-len* 3)
  30. (define (byte-length assembly)
  31. (pmatch assembly
  32. ((,inst . _) (guard (>= (instruction-length inst) 0))
  33. (+ 1 (instruction-length inst)))
  34. ((load-number ,str)
  35. (+ 1 *len-len* (string-length str)))
  36. ((load-string ,str)
  37. (+ 1 *len-len* (string-length str)))
  38. ((load-wide-string ,str)
  39. (+ 1 *len-len* (* 4 (string-length str))))
  40. ((load-symbol ,str)
  41. (+ 1 *len-len* (string-length str)))
  42. ((load-array ,bv)
  43. (+ 1 *len-len* (bytevector-length bv)))
  44. ((load-program ,labels ,len ,meta . ,code)
  45. (+ 1 *program-header-len* len (if meta (1- (byte-length meta)) 0)))
  46. (,label (guard (not (pair? label)))
  47. 0)
  48. (else (error "unknown instruction" assembly))))
  49. (define *program-alignment* 8)
  50. (define (addr+ addr code)
  51. (fold (lambda (x len) (+ (byte-length x) len))
  52. addr
  53. code))
  54. (define (code-alignment addr alignment header-len)
  55. (make-list (modulo (- alignment
  56. (modulo (+ addr header-len) alignment))
  57. alignment)
  58. '(nop)))
  59. (define (align-block addr)
  60. '())
  61. (define (align-code code addr alignment header-len)
  62. `(,@(code-alignment addr alignment header-len)
  63. ,code))
  64. (define (align-program prog addr)
  65. (align-code prog addr *program-alignment* 1))
  66. ;;;
  67. ;;; Code compress/decompression
  68. ;;;
  69. (define *abbreviations*
  70. '(((make-int8 0) . (make-int8:0))
  71. ((make-int8 1) . (make-int8:1))))
  72. (define *expansions*
  73. (map (lambda (x) (cons (cdr x) (car x))) *abbreviations*))
  74. (define (assembly-pack code)
  75. (or (assoc-ref *abbreviations* code)
  76. code))
  77. (define (assembly-unpack code)
  78. (or (assoc-ref *expansions* code)
  79. code))
  80. ;;;
  81. ;;; Encoder/decoder
  82. ;;;
  83. (define (object->assembly x)
  84. (cond ((eq? x #t) `(make-true))
  85. ((eq? x #f) `(make-false))
  86. ((eq? x #nil) `(make-nil))
  87. ((null? x) `(make-eol))
  88. ((and (integer? x) (exact? x))
  89. (cond ((and (<= -128 x) (< x 128))
  90. (assembly-pack `(make-int8 ,(modulo x 256))))
  91. ((and (<= -32768 x) (< x 32768))
  92. (let ((n (if (< x 0) (+ x 65536) x)))
  93. `(make-int16 ,(quotient n 256) ,(modulo n 256))))
  94. ((and (<= 0 x #xffffffffffffffff))
  95. `(make-uint64 ,@(bytevector->u8-list
  96. (let ((bv (make-bytevector 8)))
  97. (bytevector-u64-set! bv 0 x (endianness big))
  98. bv))))
  99. ((and (<= 0 (+ x #x8000000000000000) #x7fffffffffffffff))
  100. `(make-int64 ,@(bytevector->u8-list
  101. (let ((bv (make-bytevector 8)))
  102. (bytevector-s64-set! bv 0 x (endianness big))
  103. bv))))
  104. (else #f)))
  105. ((char? x)
  106. (cond ((<= (char->integer x) #xff)
  107. `(make-char8 ,(char->integer x)))
  108. (else
  109. `(make-char32 ,(char->integer x)))))
  110. (else #f)))
  111. (define (assembly->object code)
  112. (pmatch code
  113. ((make-true) #t)
  114. ((make-false) #f) ;; FIXME: Same as the `else' case!
  115. ((make-nil) #nil)
  116. ((make-eol) '())
  117. ((make-int8 ,n)
  118. (if (< n 128) n (- n 256)))
  119. ((make-int16 ,n1 ,n2)
  120. (let ((n (+ (* n1 256) n2)))
  121. (if (< n 32768) n (- n 65536))))
  122. ((make-uint64 ,n1 ,n2 ,n3 ,n4 ,n5 ,n6 ,n7 ,n8)
  123. (bytevector-u64-ref
  124. (u8-list->bytevector (list n1 n2 n3 n4 n5 n6 n7 n8))
  125. 0
  126. (endianness big)))
  127. ((make-int64 ,n1 ,n2 ,n3 ,n4 ,n5 ,n6 ,n7 ,n8)
  128. (bytevector-s64-ref
  129. (u8-list->bytevector (list n1 n2 n3 n4 n5 n6 n7 n8))
  130. 0
  131. (endianness big)))
  132. ((make-char8 ,n)
  133. (integer->char n))
  134. ((make-char32 ,n1 ,n2 ,n3 ,n4)
  135. (integer->char (+ (* n1 #x1000000)
  136. (* n2 #x10000)
  137. (* n3 #x100)
  138. n4)))
  139. ((load-string ,s) s)
  140. ((load-symbol ,s) (string->symbol s))
  141. (else #f)))