Merge commit 'feccd2d3100fd2964d4c2df58ab3da7ce4949a66' into vm-check
[bpt/guile.git] / module / language / assembly / decompile-bytecode.scm
1 ;;; Guile VM code converters
2
3 ;; Copyright (C) 2001 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 decompile-bytecode)
23 #:use-module (system vm instruction)
24 #:use-module (system base pmatch)
25 #:use-module (srfi srfi-4)
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 (decode-load-program pop)
43 (let* ((nargs (pop)) (nrest (pop)) (nlocs (pop)) (nexts (pop))
44 (a (pop)) (b (pop)) (c (pop)) (d (pop))
45 (e (pop)) (f (pop)) (g (pop)) (h (pop))
46 (len (+ a (ash b 8) (ash c 16) (ash d 24)))
47 (metalen (+ e (ash f 8) (ash g 16) (ash h 24)))
48 (totlen (+ len metalen))
49 (i 0))
50 (define (sub-pop) ;; ...records. ha. ha.
51 (let ((b (cond ((< i len) (pop))
52 ((= i len) #f)
53 (else (error "tried to decode too many bytes")))))
54 (if b (set! i (1+ i)))
55 b))
56 (let lp ((out '()))
57 (cond ((> i len)
58 (error "error decoding program -- read too many bytes" out))
59 ((= i len)
60 `(load-program ,nargs ,nrest ,nlocs ,nexts () ,len
61 ,(if (zero? metalen) #f (decode-load-program pop))
62 ,@(reverse! out)))
63 (else
64 (let ((exp (decode-bytecode sub-pop)))
65 ;; replace with labels?
66 (lp (cons exp out))))))))
67
68 (define (decode-bytecode pop)
69 (and=> (pop)
70 (lambda (opcode)
71 (let ((inst (opcode->instruction opcode)))
72 (cond
73 ((eq? inst 'load-program)
74 (decode-load-program pop))
75 ((< (instruction-length inst) 0)
76 (let* ((len (let* ((a (pop)) (b (pop)) (c (pop)))
77 (+ (ash a 16) (ash b 8) c)))
78 (str (make-string len)))
79 (let lp ((i 0))
80 (if (= i len)
81 `(,inst ,str)
82 (begin
83 (string-set! str i (integer->char (pop)))
84 (lp (1+ i)))))))
85 (else
86 ;; fixed length
87 (let lp ((n (instruction-length inst)) (out (list inst)))
88 (if (zero? n)
89 (reverse! out)
90 (lp (1- n) (cons (pop) out))))))))))