a783a4e5ea5078a6c124ff736997356bc10445d2
[bpt/guile.git] / module / language / objcode / spec.scm
1 ;;; Guile Lowlevel Intermediate Language
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 objcode spec)
22 #:use-module (system base language)
23 #:use-module (system vm objcode)
24 #:use-module (system vm program)
25 #:export (objcode make-objcode-env))
26
27 (define (make-objcode-env module externals)
28 (cons module externals))
29
30 (define (objcode-env-module env)
31 (if env (car env) (current-module)))
32
33 (define (objcode-env-externals env)
34 (and env (vector? (cdr env)) (cdr env)))
35
36 (define (objcode->value x e opts)
37 (let ((thunk (make-program x #f (objcode-env-externals e))))
38 (if e
39 (save-module-excursion
40 (lambda ()
41 (set-current-module (objcode-env-module e))
42 (values (thunk) #f e)))
43 (values (thunk) #f e))))
44
45 ;; since locals are allocated on the stack and can have limited scope,
46 ;; in many cases we use one local for more than one lexical variable. so
47 ;; the returned locals set is a list, where element N of the list is
48 ;; itself a list of bindings for local variable N.
49 (define (collapse-locals locs)
50 (let lp ((ret '()) (locs locs))
51 (if (null? locs)
52 (map cdr (sort! ret
53 (lambda (x y) (< (car x) (car y)))))
54 (let ((b (car locs)))
55 (cond
56 ((assv-ref ret (binding:index b))
57 => (lambda (bindings)
58 (append! bindings (list b))
59 (lp ret (cdr locs))))
60 (else
61 (lp (acons (binding:index b) (list b) ret)
62 (cdr locs))))))))
63
64 (define (decompile-value x env opts)
65 (cond
66 ((program? x)
67 (let ((objs (program-objects x))
68 (meta (program-meta x))
69 (exts (program-external x))
70 (binds (program-bindings x))
71 (srcs (program-sources x))
72 (nargs (arity:nargs (program-arity x))))
73 (let ((blocs (and binds
74 (collapse-locals
75 (append (list-head binds nargs)
76 (filter (lambda (x) (not (binding:extp x)))
77 (list-tail binds nargs))))))
78 (bexts (and binds
79 (filter binding:extp binds))))
80 (values (program-objcode x)
81 `((objects . ,objs)
82 (meta . ,(and meta (meta)))
83 (exts . ,exts)
84 (blocs . ,blocs)
85 (bexts . ,bexts)
86 (sources . ,srcs))))))
87 ((objcode? x)
88 (values x #f))
89 (else
90 (error "can't decompile ~A: not a program or objcode" x))))
91
92 (define-language objcode
93 #:title "Guile Object Code"
94 #:version "0.3"
95 #:reader #f
96 #:printer write-objcode
97 #:compilers `((value . ,objcode->value))
98 #:decompilers `((value . ,decompile-value))
99 )