1 ;;; Guile VM code converters
3 ;; Copyright (C) 2001 Free Software Foundation, Inc.
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)
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.
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.
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))
29 (define (decompile-bytecode x env opts)
30 (let ((i 0) (size (u8vector-length x)))
32 (let ((b (cond ((< i size) (u8vector-ref x i))
34 (else (error "tried to decode too many bytes")))))
35 (if b (set! i (1+ i)))
37 (let ((ret (decode-load-program pop)))
40 (error "bad bytecode: only decoded ~a out of ~a bytes" i size)))))
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))
50 (define (sub-pop) ;; ...records. ha. ha.
51 (let ((b (cond ((< i len) (pop))
53 (else (error "tried to decode too many bytes")))))
54 (if b (set! i (1+ i)))
58 (error "error decoding program -- read too many bytes" out))
60 `(load-program ,nargs ,nrest ,nlocs ,nexts () ,len
61 ,(if (zero? metalen) #f (decode-load-program pop))
64 (let ((exp (decode-bytecode sub-pop)))
65 ;; replace with labels?
66 (lp (cons exp out))))))))
68 (define (decode-bytecode pop)
71 (let ((inst (opcode->instruction opcode)))
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)))
83 (string-set! str i (integer->char (pop)))
87 (let lp ((n (instruction-length inst)) (out (list inst)))
90 (lp (1- n) (cons (pop) out))))))))))