1 ;;; GHIL -> GLIL compiler
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 (system il compile)
23 :use-module (oop goops)
24 :use-syntax (system base syntax)
25 :use-module (system il glil)
26 :use-module (system il ghil)
27 :use-module (ice-9 common-list)
30 (define (compile x e . opts)
31 (set! x (parse-ghil x e))
32 (if (memq :O opts) (set! x (optimize x)))
37 ;;; Stage 2: Optimization
42 (($ <ghil-set> env var val)
43 (make-<ghil-set> env var (optimize val)))
45 (($ <ghil-if> test then else)
46 (make-<ghil-if> (optimize test) (optimize then) (optimize else)))
48 (($ <ghil-begin> exps)
49 (make-<ghil-begin> (map optimize exps)))
51 (($ <ghil-bind> env vars vals body)
52 (make-<ghil-bind> env vars (map optimize vals) (optimize body)))
54 (($ <ghil-lambda> env vars rest body)
55 (make-<ghil-lambda> env vars rest (optimize body)))
57 (($ <ghil-inst> inst args)
58 (make-<ghil-inst> inst (map optimize args)))
60 (($ <ghil-call> env proc args)
62 ;; ((@lambda (VAR...) BODY...) ARG...) =>
63 ;; (@let ((VAR ARG) ...) BODY...)
64 (($ <ghil-lambda> lambda-env vars #f body)
66 (if (eq? v.kind 'argument) (set! v.kind 'local))
68 (ghil-env-add! env v))
70 (optimize (make-<ghil-bind> env vars args body)))
72 (make-<ghil-call> env (optimize proc) (map optimize args)))))
77 ;;; Stage 3: Code generation
80 (define *ia-void* (make-<glil-void>))
81 (define *ia-drop* (make-<glil-call> 'drop 0))
82 (define *ia-return* (make-<glil-call> 'return 0))
84 (define (make-label) (gensym ":L"))
86 (define (make-glil-var op env var)
89 (make-<glil-argument> op var.index))
91 (make-<glil-local> op var.index))
93 (do ((depth 0 (1+ depth))
96 (make-<glil-external> op depth var.index))))
98 (make-<glil-module> op var.env var.name))
99 (else (error "Unknown kind of variable:" var))))
101 (define (codegen ghil)
103 (define (push-code! code)
104 (set! stack (cons code stack)))
105 (define (comp tree tail drop)
106 ;; possible tail position
107 (define (comp-tail tree) (comp tree tail drop))
109 (define (comp-push tree) (comp tree #f #f))
111 (define (comp-drop tree) (comp tree #f #t))
112 ;; return this code if necessary
113 (define (return-code! code)
114 (if (not drop) (push-code! code))
115 (if tail (push-code! *ia-return*)))
116 ;; return void if necessary
117 (define (return-void!) (return-code! *ia-void*))
124 (($ <ghil-quote> obj)
125 (return-code! (make-<glil-const> obj)))
127 (($ <ghil-ref> env var)
128 (return-code! (make-glil-var 'ref env var)))
130 (($ <ghil-set> env var val)
132 (push-code! (make-glil-var 'set env var))
135 (($ <ghil-if> test then else)
142 (let ((L1 (make-label)) (L2 (make-label)))
144 (push-code! (make-<glil-branch> 'br-if-not L1))
146 (if (not tail) (push-code! (make-<glil-branch> 'br L2)))
147 (push-code! (make-<glil-label> L1))
149 (if (not tail) (push-code! (make-<glil-label> L2)))))
151 (($ <ghil-begin> exps)
156 (do ((exps exps (cdr exps)))
158 (comp-tail (car exps)))
159 (comp-drop (car exps)))))
161 (($ <ghil-bind> env vars vals body)
165 (for-each comp-push vals)
166 (for-each (lambda (var) (push-code! (make-glil-var 'set env var)))
170 (($ <ghil-lambda> env vars rest body)
171 (return-code! (codegen tree)))
173 (($ <ghil-inst> inst args)
176 (for-each comp-push args)
177 (push-code! (make-<glil-call> inst (length args)))
178 (if drop (push-code! *ia-drop*))
179 (if tail (push-code! *ia-return*)))
181 (($ <ghil-call> env proc args)
184 ;; ([tail-]call NARGS)
186 (for-each comp-push args)
187 (let ((inst (if tail 'tail-call 'call)))
188 (push-code! (make-<glil-call> inst (length args))))
189 (if drop (push-code! *ia-drop*)))))
193 (($ <ghil-lambda> env args rest body)
194 (let* ((vars env.variables)
195 (locs (pick (lambda (v) (eq? v.kind 'local)) vars))
196 (exts (pick (lambda (v) (eq? v.kind 'external)) vars)))
197 ;; initialize variable indexes
198 (finalize-index! args)
199 (finalize-index! locs)
200 (finalize-index! exts)
202 (do ((n 0 (1+ n)) (l args (cdr l)))
205 (if (eq? v.kind 'external)
206 (begin (push-code! (make-<glil-argument> 'ref n))
207 (push-code! (make-<glil-external> 'set 0 v.index))))))
211 (make-<glil-asm> (length args) (if rest 1 0) (length locs)
212 (length exts) (reverse! stack)))))))
214 (define (finalize-index! list)
218 (let ((v (car l))) (set! v.index n))))