Change Guile license to LGPLv3+
[bpt/guile.git] / module / language / assembly / compile-bytecode.scm
1 ;;; Guile VM assembler
2
3 ;; Copyright (C) 2001, 2009 Free Software Foundation, Inc.
4
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.
9 ;;;;
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.
14 ;;;;
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
18
19 ;;; Code:
20
21 (define-module (language assembly compile-bytecode)
22 #:use-module (system base pmatch)
23 #:use-module (language assembly)
24 #:use-module (system vm instruction)
25 #:use-module (srfi srfi-4)
26 #:use-module (rnrs bytevector)
27 #:use-module ((srfi srfi-1) #:select (fold))
28 #:use-module ((system vm objcode) #:select (byte-order))
29 #:export (compile-bytecode write-bytecode))
30
31 (define (compile-bytecode assembly env . opts)
32 (pmatch assembly
33 ((load-program . _)
34 ;; the 1- and -1 are so that we drop the load-program byte
35 (letrec ((v (make-u8vector (1- (byte-length assembly))))
36 (i -1)
37 (write-byte (lambda (b)
38 (if (>= i 0) (u8vector-set! v i b))
39 (set! i (1+ i))))
40 (get-addr (lambda () i)))
41 (write-bytecode assembly write-byte get-addr '())
42 (if (= i (u8vector-length v))
43 (values v env env)
44 (error "incorrect length in assembly" i (u8vector-length v)))))
45 (else (error "bad assembly" assembly))))
46
47 (define (write-bytecode asm write-byte get-addr labels)
48 (define (write-char c)
49 (write-byte (char->integer c)))
50 (define (write-string s)
51 (string-for-each write-char s))
52 (define (write-uint16-be x)
53 (write-byte (logand (ash x -8) 255))
54 (write-byte (logand x 255)))
55 (define (write-uint16-le x)
56 (write-byte (logand x 255))
57 (write-byte (logand (ash x -8) 255)))
58 (define (write-uint32-be x)
59 (write-byte (logand (ash x -24) 255))
60 (write-byte (logand (ash x -16) 255))
61 (write-byte (logand (ash x -8) 255))
62 (write-byte (logand x 255)))
63 (define (write-uint32-le x)
64 (write-byte (logand x 255))
65 (write-byte (logand (ash x -8) 255))
66 (write-byte (logand (ash x -16) 255))
67 (write-byte (logand (ash x -24) 255)))
68 (define (write-loader-len len)
69 (write-byte (ash len -16))
70 (write-byte (logand (ash len -8) 255))
71 (write-byte (logand len 255)))
72 (define (write-loader str)
73 (write-loader-len (string-length str))
74 (write-string str))
75 (define (write-bytevector bv)
76 (write-loader-len (bytevector-length bv))
77 ;; Ew!
78 (for-each write-byte (bytevector->u8-list bv)))
79 (define (write-break label)
80 (write-uint16-be (- (assq-ref labels label) (+ (get-addr) 2))))
81
82 (let ((inst (car asm))
83 (args (cdr asm))
84 (write-uint32 (case byte-order
85 ((1234) write-uint32-le)
86 ((4321) write-uint32-be)
87 (else (error "unknown endianness" byte-order)))))
88 (let ((opcode (instruction->opcode inst))
89 (len (instruction-length inst)))
90 (write-byte opcode)
91 (pmatch asm
92 ((load-program ,nargs ,nrest ,nlocs ,nexts
93 ,labels ,length ,meta . ,code)
94 (write-byte nargs)
95 (write-byte nrest)
96 (write-byte nlocs)
97 (write-byte nexts)
98 (write-uint32 length)
99 (write-uint32 (if meta (1- (byte-length meta)) 0))
100 (letrec ((i 0)
101 (write (lambda (x) (set! i (1+ i)) (write-byte x)))
102 (get-addr (lambda () i)))
103 (for-each (lambda (asm)
104 (write-bytecode asm write get-addr labels))
105 code))
106 (if meta
107 ;; don't write the load-program byte for metadata
108 (letrec ((i -1)
109 (write (lambda (x)
110 (set! i (1+ i))
111 (if (> i 0) (write-byte x))))
112 (get-addr (lambda () i)))
113 (write-bytecode meta write get-addr '()))))
114 ((load-unsigned-integer ,str) (write-loader str))
115 ((load-integer ,str) (write-loader str))
116 ((load-number ,str) (write-loader str))
117 ((load-string ,str) (write-loader str))
118 ((load-symbol ,str) (write-loader str))
119 ((load-keyword ,str) (write-loader str))
120 ((load-array ,bv) (write-bytevector bv))
121 ((define ,str) (write-loader str))
122 ((br ,l) (write-break l))
123 ((br-if ,l) (write-break l))
124 ((br-if-not ,l) (write-break l))
125 ((br-if-eq ,l) (write-break l))
126 ((br-if-not-eq ,l) (write-break l))
127 ((br-if-null ,l) (write-break l))
128 ((br-if-not-null ,l) (write-break l))
129 ((mv-call ,n ,l) (write-byte n) (write-break l))
130 (else
131 (cond
132 ((< (instruction-length inst) 0)
133 (error "unhanded variable-length instruction" asm))
134 ((not (= (length args) len))
135 (error "bad number of args to instruction" asm len))
136 (else
137 (for-each write-byte args))))))))