Merge commit '95a040cd2be7ad03bf197edbdb1fec2c52749ef6' into vm-check
[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 program is free software; you can redistribute it and/or modify
6 ;; it under the terms of the GNU General Public License as published by
7 ;; the Free Software Foundation; either version 2, or (at your option)
8 ;; any later version.
9 ;;
10 ;; This program 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
13 ;; GNU General Public License for more details.
14 ;;
15 ;; You should have received a copy of the GNU General Public License
16 ;; along with this program; see the file COPYING. If not, write to
17 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 ;; Boston, MA 02111-1307, USA.
19
20 ;;; Code:
21
22 (define-module (language assembly compile-bytecode)
23 #:use-module (system base pmatch)
24 #:use-module (language assembly)
25 #:use-module (system vm instruction)
26 #:use-module (srfi srfi-4)
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)
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-break label)
76 (write-uint16-be (- (assq-ref labels label) (+ (get-addr) 2))))
77
78 (let ((inst (car asm))
79 (args (cdr asm))
80 (write-uint32 (case byte-order
81 ((1234) write-uint32-le)
82 ((4321) write-uint32-be)
83 (else (error "unknown endianness" byte-order)))))
84 (let ((opcode (instruction->opcode inst))
85 (len (instruction-length inst)))
86 (write-byte opcode)
87 (pmatch asm
88 ((load-program ,nargs ,nrest ,nlocs ,nexts
89 ,labels ,length ,meta . ,code)
90 (write-byte nargs)
91 (write-byte nrest)
92 (write-byte nlocs)
93 (write-byte nexts)
94 (write-uint32 length)
95 (write-uint32 (if meta (1- (byte-length meta)) 0))
96 (letrec ((i 0)
97 (write (lambda (x) (set! i (1+ i)) (write-byte x)))
98 (get-addr (lambda () i)))
99 (for-each (lambda (asm)
100 (write-bytecode asm write get-addr labels))
101 code))
102 (if meta
103 ;; don't write the load-program byte for metadata
104 (letrec ((i -1)
105 (write (lambda (x)
106 (set! i (1+ i))
107 (if (> i 0) (write-byte x))))
108 (get-addr (lambda () i)))
109 (write-bytecode meta write get-addr '()))))
110 ((load-unsigned-integer ,str) (write-loader str))
111 ((load-integer ,str) (write-loader str))
112 ((load-number ,str) (write-loader str))
113 ((load-string ,str) (write-loader str))
114 ((load-symbol ,str) (write-loader str))
115 ((load-keyword ,str) (write-loader str))
116 ((define ,str) (write-loader str))
117 ((br ,l) (write-break l))
118 ((br-if ,l) (write-break l))
119 ((br-if-not ,l) (write-break l))
120 ((br-if-eq ,l) (write-break l))
121 ((br-if-not-eq ,l) (write-break l))
122 ((br-if-null ,l) (write-break l))
123 ((br-if-not-null ,l) (write-break l))
124 ((mv-call ,n ,l) (write-byte n) (write-break l))
125 (else
126 (cond
127 ((< (instruction-length inst) 0)
128 (error "unhanded variable-length instruction" asm))
129 ((not (= (length args) len))
130 (error "bad number of args to instruction" asm len))
131 (else
132 (for-each write-byte args))))))))