1 ;;; Guile Virtual Machine Assembly
3 ;; Copyright (C) 2001, 2009, 2010, 2011 Free Software Foundation, Inc.
5 ;;;; This library is free software; you can redistribute it and/or
6 ;;;; modify it under the terms of the GNU Lesser General Public
7 ;;;; License as published by the Free Software Foundation; either
8 ;;;; version 3 of the License, or (at your option) any later version.
10 ;;;; This library is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;;;; Lesser General Public License for more details.
15 ;;;; You should have received a copy of the GNU Lesser General Public
16 ;;;; License along with this library; if not, write to the Free Software
17 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 (define-module (language assembly)
22 #:use-module (rnrs bytevectors)
23 #:use-module (system base pmatch)
24 #:use-module (system vm instruction)
25 #:use-module ((srfi srfi-1) #:select (fold))
27 addr+ align-program align-code align-block
28 assembly-pack assembly-unpack
29 object->assembly assembly->object))
32 (define *program-header-len* (+ 4 4))
34 ;; lengths are encoded in 3 bytes
38 (define (byte-length assembly)
40 ((,inst . _) (guard (>= (instruction-length inst) 0))
41 (+ 1 (instruction-length inst)))
43 (+ 1 *len-len* (string-length str)))
45 (+ 1 *len-len* (string-length str)))
46 ((load-wide-string ,str)
47 (+ 1 *len-len* (* 4 (string-length str))))
49 (+ 1 *len-len* (string-length str)))
51 (+ 1 *len-len* (bytevector-length bv)))
52 ((load-program ,labels ,len ,meta . ,code)
53 (+ 1 *program-header-len* len (if meta (1- (byte-length meta)) 0)))
54 (,label (guard (not (pair? label)))
56 (else (error "unknown instruction" assembly))))
59 (define *program-alignment* 8)
61 (define (addr+ addr code)
62 (fold (lambda (x len) (+ (byte-length x) len))
66 (define (code-alignment addr alignment header-len)
67 (make-list (modulo (- alignment
68 (modulo (+ addr header-len) alignment))
72 (define (align-block addr)
75 (define (align-code code addr alignment header-len)
76 `(,@(code-alignment addr alignment header-len)
79 (define (align-program prog addr)
80 (align-code prog addr *program-alignment* 1))
83 ;;; Code compress/decompression
86 (define *abbreviations*
87 '(((make-int8 0) . (make-int8:0))
88 ((make-int8 1) . (make-int8:1))))
91 (map (lambda (x) (cons (cdr x) (car x))) *abbreviations*))
93 (define (assembly-pack code)
94 (or (assoc-ref *abbreviations* code)
97 (define (assembly-unpack code)
98 (or (assoc-ref *expansions* code)
106 (define (object->assembly x)
107 (cond ((eq? x #t) `(make-true))
108 ((eq? x #f) `(make-false))
109 ((eq? x #nil) `(make-nil))
110 ((null? x) `(make-eol))
111 ((and (integer? x) (exact? x))
112 (cond ((and (<= -128 x) (< x 128))
113 (assembly-pack `(make-int8 ,(modulo x 256))))
114 ((and (<= -32768 x) (< x 32768))
115 (let ((n (if (< x 0) (+ x 65536) x)))
116 `(make-int16 ,(quotient n 256) ,(modulo n 256))))
117 ((and (<= 0 x #xffffffffffffffff))
118 `(make-uint64 ,@(bytevector->u8-list
119 (let ((bv (make-bytevector 8)))
120 (bytevector-u64-set! bv 0 x (endianness big))
122 ((and (<= 0 (+ x #x8000000000000000) #x7fffffffffffffff))
123 `(make-int64 ,@(bytevector->u8-list
124 (let ((bv (make-bytevector 8)))
125 (bytevector-s64-set! bv 0 x (endianness big))
129 (cond ((<= (char->integer x) #xff)
130 `(make-char8 ,(char->integer x)))
132 `(make-char32 ,(char->integer x)))))
135 (define (assembly->object code)
138 ((make-false) #f) ;; FIXME: Same as the `else' case!
142 (if (< n 128) n (- n 256)))
143 ((make-int16 ,n1 ,n2)
144 (let ((n (+ (* n1 256) n2)))
145 (if (< n 32768) n (- n 65536))))
146 ((make-uint64 ,n1 ,n2 ,n3 ,n4 ,n5 ,n6 ,n7 ,n8)
148 (u8-list->bytevector (list n1 n2 n3 n4 n5 n6 n7 n8))
151 ((make-int64 ,n1 ,n2 ,n3 ,n4 ,n5 ,n6 ,n7 ,n8)
153 (u8-list->bytevector (list n1 n2 n3 n4 n5 n6 n7 n8))
158 ((make-char32 ,n1 ,n2 ,n3 ,n4)
159 (integer->char (+ (* n1 #x1000000)
164 ((load-symbol ,s) (string->symbol s))