compile lexical variable access and closure creation to the new ops
[bpt/guile.git] / module / language / glil.scm
CommitLineData
17e90c5e
KN
1;;; Guile Low Intermediate Language
2
66d3e9a3 3;; Copyright (C) 2001, 2009 Free Software Foundation, Inc.
17e90c5e 4
53befeb7
NJ
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
17e90c5e
KN
18
19;;; Code:
20
9ff56d9e 21(define-module (language glil)
b0b180d5
AW
22 #:use-module (system base syntax)
23 #:use-module (system base pmatch)
53e28ed9 24 #:use-module ((srfi srfi-1) #:select (fold))
1a1a10d3 25 #:export
c850030f
AW
26 (<glil-program> make-glil-program glil-program?
27 glil-program-nargs glil-program-nrest glil-program-nlocs glil-program-nexts
53e28ed9 28 glil-program-meta glil-program-body glil-program-closure-level
01967b69
AW
29
30 <glil-bind> make-glil-bind glil-bind?
bdaffda2 31 glil-bind-vars
01967b69 32
d51406fe
AW
33 <glil-mv-bind> make-glil-mv-bind glil-mv-bind?
34 glil-mv-bind-vars glil-mv-bind-rest
35
01967b69
AW
36 <glil-unbind> make-glil-unbind glil-unbind?
37
38 <glil-source> make-glil-source glil-source?
028e3d06 39 glil-source-props
17e90c5e 40
01967b69
AW
41 <glil-void> make-glil-void glil-void?
42
43 <glil-const> make-glil-const glil-const?
bdaffda2 44 glil-const-obj
17e90c5e 45
01967b69 46 <glil-local> make-glil-local glil-local?
bdaffda2 47 glil-local-op glil-local-index
01967b69
AW
48
49 <glil-external> make-glil-external glil-external?
bdaffda2 50 glil-external-op glil-external-depth glil-external-index
01967b69 51
66d3e9a3
AW
52 <glil-lexical> make-glil-lexical glil-lexical?
53 glil-lexical-local? glil-lexical-boxed? glil-lexical-op glil-lexical-index
54
a1122f8c
AW
55 <glil-toplevel> make-glil-toplevel glil-toplevel?
56 glil-toplevel-op glil-toplevel-name
9cc649b8 57
fd358575
AW
58 <glil-module> make-glil-module glil-module?
59 glil-module-op glil-module-mod glil-module-name glil-module-public?
60
01967b69 61 <glil-label> make-glil-label glil-label?
bdaffda2 62 glil-label-label
01967b69
AW
63
64 <glil-branch> make-glil-branch glil-branch?
efbd5892 65 glil-branch-inst glil-branch-label
01967b69
AW
66
67 <glil-call> make-glil-call glil-call?
efbd5892
AW
68 glil-call-inst glil-call-nargs
69
70 <glil-mv-call> make-glil-mv-call glil-mv-call?
b0b180d5 71 glil-mv-call-nargs glil-mv-call-ra
17e90c5e 72
b0b180d5 73 parse-glil unparse-glil))
ac99cb0c 74
b0b180d5
AW
75(define (print-glil x port)
76 (format port "#<glil ~s>" (unparse-glil x)))
77
78(define-type (<glil> #:printer print-glil)
1086fabd 79 ;; Meta operations
53e28ed9 80 (<glil-program> nargs nrest nlocs nexts meta body (closure-level #f))
1086fabd
AW
81 (<glil-bind> vars)
82 (<glil-mv-bind> vars rest)
83 (<glil-unbind>)
028e3d06 84 (<glil-source> props)
1086fabd
AW
85 ;; Objects
86 (<glil-void>)
87 (<glil-const> obj)
88 ;; Variables
1086fabd
AW
89 (<glil-local> op index)
90 (<glil-external> op depth index)
66d3e9a3 91 (<glil-lexical> local? boxed? op index)
1086fabd
AW
92 (<glil-toplevel> op name)
93 (<glil-module> op mod name public?)
94 ;; Controls
95 (<glil-label> label)
96 (<glil-branch> inst label)
97 (<glil-call> inst nargs)
98 (<glil-mv-call> nargs ra))
17e90c5e 99
53e28ed9
AW
100(define (compute-closure-level body)
101 (fold (lambda (x ret)
102 (record-case x
103 ((<glil-program> closure-level) (max ret closure-level))
104 ((<glil-external> depth) (max ret depth))
105 (else ret)))
106 0 body))
107
108(define %make-glil-program make-glil-program)
109(define (make-glil-program . args)
110 (let ((prog (apply %make-glil-program args)))
111 (if (not (glil-program-closure-level prog))
112 (set! (glil-program-closure-level prog)
113 (compute-closure-level (glil-program-body prog))))
114 prog))
115
17e90c5e 116\f
b0b180d5
AW
117(define (parse-glil x)
118 (pmatch x
c850030f
AW
119 ((program ,nargs ,nrest ,nlocs ,nexts ,meta . ,body)
120 (make-glil-program nargs nrest nlocs nexts meta (map parse-glil body)))
b0b180d5 121 ((bind . ,vars) (make-glil-bind vars))
594d9d4c 122 ((mv-bind ,vars ,rest) (make-glil-mv-bind vars rest))
b0b180d5 123 ((unbind) (make-glil-unbind))
028e3d06 124 ((source ,props) (make-glil-source props))
b0b180d5
AW
125 ((void) (make-glil-void))
126 ((const ,obj) (make-glil-const obj))
b0b180d5
AW
127 ((local ,op ,index) (make-glil-local op index))
128 ((external ,op ,depth ,index) (make-glil-external op depth index))
66d3e9a3 129 ((lexical ,local? ,boxed? ,op ,index) (make-glil-lexical local? boxed? op index))
b0b180d5
AW
130 ((toplevel ,op ,name) (make-glil-toplevel op name))
131 ((module public ,op ,mod ,name) (make-glil-module op mod name #t))
132 ((module private ,op ,mod ,name) (make-glil-module op mod name #f))
01c161ca 133 ((label ,label) (make-label label))
b0b180d5
AW
134 ((branch ,inst ,label) (make-glil-branch inst label))
135 ((call ,inst ,nargs) (make-glil-call inst nargs))
136 ((mv-call ,nargs ,ra) (make-glil-mv-call nargs ra))
137 (else (error "invalid glil" x))))
138
139(define (unparse-glil glil)
67169b29 140 (record-case glil
17e90c5e 141 ;; meta
c850030f
AW
142 ((<glil-program> nargs nrest nlocs nexts meta body)
143 `(program ,nargs ,nrest ,nlocs ,nexts ,meta ,@(map unparse-glil body)))
b0b180d5 144 ((<glil-bind> vars) `(bind ,@vars))
594d9d4c 145 ((<glil-mv-bind> vars rest) `(mv-bind ,vars ,rest))
b0b180d5 146 ((<glil-unbind>) `(unbind))
028e3d06 147 ((<glil-source> props) `(source ,props))
17e90c5e 148 ;; constants
67169b29
AW
149 ((<glil-void>) `(void))
150 ((<glil-const> obj) `(const ,obj))
17e90c5e 151 ;; variables
67169b29 152 ((<glil-external> op depth index)
b0b180d5 153 `(external ,op ,depth ,index))
66d3e9a3
AW
154 ((<glil-lexical> local? boxed? op index)
155 `(lexical ,local? ,boxed? ,op ,index))
a1122f8c 156 ((<glil-toplevel> op name)
b0b180d5 157 `(toplevel ,op ,name))
fd358575 158 ((<glil-module> op mod name public?)
b0b180d5 159 `(module ,(if public? 'public 'private) ,op ,mod ,name))
17e90c5e 160 ;; controls
53e28ed9 161 ((<glil-label> label) `(label ,label))
b0b180d5
AW
162 ((<glil-branch> inst label) `(branch ,inst ,label))
163 ((<glil-call> inst nargs) `(call ,inst ,nargs))
594d9d4c 164 ((<glil-mv-call> nargs ra) `(mv-call ,nargs ,ra))))