make sure all programs are 8-byte aligned
[bpt/guile.git] / module / language / assembly / decompile-bytecode.scm
1 ;;; Guile VM code converters
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 decompile-bytecode)
22 #:use-module (system vm instruction)
23 #:use-module (system base pmatch)
24 #:use-module (srfi srfi-4)
25 #:use-module (rnrs bytevector)
26 #:use-module (language assembly)
27 #:export (decompile-bytecode))
28
29 (define (decompile-bytecode x env opts)
30 (let ((i 0) (size (u8vector-length x)))
31 (define (pop)
32 (let ((b (cond ((< i size) (u8vector-ref x i))
33 ((= i size) #f)
34 (else (error "tried to decode too many bytes")))))
35 (if b (set! i (1+ i)))
36 b))
37 (let ((ret (decode-load-program pop)))
38 (if (= i size)
39 (values ret env)
40 (error "bad bytecode: only decoded ~a out of ~a bytes" i size)))))
41
42 (define (br-instruction? x)
43 (memq x '(br br-if br-if-not br-if-eq br-if-not-eq br-if-null br-if-not-null)))
44
45 (define (bytes->s16 a b)
46 (let ((x (+ (ash a 8) b)))
47 (if (zero? (logand (ash 1 15) x))
48 x
49 (- x (ash 1 16)))))
50
51 ;; FIXME: this is a little-endian disassembly!!!
52 (define (decode-load-program pop)
53 (let* ((nargs (pop)) (nrest (pop)) (nlocs0 (pop)) (nlocs1 (pop))
54 (nlocs (+ nlocs0 (ash nlocs1 8)))
55 (a (pop)) (b (pop)) (c (pop)) (d (pop))
56 (e (pop)) (f (pop)) (g (pop)) (h (pop))
57 (len (+ a (ash b 8) (ash c 16) (ash d 24)))
58 (metalen (+ e (ash f 8) (ash g 16) (ash h 24)))
59 (totlen (+ len metalen))
60 (pad0 (pop)) (pad1 (pop)) (pad2 (pop)) (pad3 (pop))
61 (labels '())
62 (i 0))
63 (define (ensure-label rel1 rel2)
64 (let ((where (+ i (bytes->s16 rel1 rel2))))
65 (or (assv-ref labels where)
66 (begin
67 (let ((l (gensym ":L")))
68 (set! labels (acons where l labels))
69 l)))))
70 (define (sub-pop) ;; ...records. ha. ha.
71 (let ((b (cond ((< i len) (pop))
72 ((= i len) #f)
73 (else (error "tried to decode too many bytes")))))
74 (if b (set! i (1+ i)))
75 b))
76 (let lp ((out '()))
77 (cond ((> i len)
78 (error "error decoding program -- read too many bytes" out))
79 ((= i len)
80 `(load-program ,nargs ,nrest ,nlocs
81 ,(map (lambda (x) (cons (cdr x) (car x)))
82 (reverse labels))
83 ,len
84 ,(if (zero? metalen) #f (decode-load-program pop))
85 ,@(reverse! out)))
86 (else
87 (let ((exp (decode-bytecode sub-pop)))
88 (pmatch exp
89 ((,br ,rel1 ,rel2) (guard (br-instruction? br))
90 (lp (cons `(,br ,(ensure-label rel1 rel2)) out)))
91 ((mv-call ,n ,rel1 ,rel2)
92 (lp (cons `(mv-call ,n ,(ensure-label rel1 rel2)) out)))
93 (else
94 (lp (cons exp out))))))))))
95
96 (define (decode-bytecode pop)
97 (and=> (pop)
98 (lambda (opcode)
99 (let ((inst (opcode->instruction opcode)))
100 (cond
101 ((eq? inst 'load-program)
102 (decode-load-program pop))
103 ((< (instruction-length inst) 0)
104 (let* ((make-sequence
105 (if (eq? inst 'load-array)
106 make-bytevector
107 make-string))
108 (sequence-set!
109 (if (eq? inst 'load-array)
110 bytevector-u8-set!
111 (lambda (str pos value)
112 (string-set! str pos (integer->char value)))))
113
114 (len (let* ((a (pop)) (b (pop)) (c (pop)))
115 (+ (ash a 16) (ash b 8) c)))
116 (seq (make-sequence len)))
117 (let lp ((i 0))
118 (if (= i len)
119 `(,inst ,seq)
120 (begin
121 (sequence-set! seq i (pop))
122 (lp (1+ i)))))))
123 (else
124 ;; fixed length
125 (let lp ((n (instruction-length inst)) (out (list inst)))
126 (if (zero? n)
127 (reverse! out)
128 (lp (1- n) (cons (pop) out))))))))))