1 ;;; Guile Low Intermediate Language
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 glil)
23 :use-syntax (system base syntax)
26 <glil-vars> make-glil-vars
27 glil-vars-nargs glil-vars-nrest glil-vars-nlocs glil-vars-nexts
29 <glil-asm> make-glil-asm glil-asm?
30 glil-asm-vars glil-asm-body
32 <glil-bind> make-glil-bind glil-bind?
35 <glil-unbind> make-glil-unbind glil-unbind?
37 <glil-source> make-glil-source glil-source?
40 <glil-void> make-glil-void glil-void?
42 <glil-const> make-glil-const glil-const?
45 <glil-argument> make-glil-argument glil-argument?
46 glil-argument-op glil-argument-index
48 <glil-local> make-glil-local glil-local?
49 glil-local-op glil-local-index
51 <glil-external> make-glil-external glil-external?
52 glil-external-op glil-external-depth glil-external-index
54 <glil-module> make-glil-module glil-module?
55 glil-module-op glil-module-module glil-module-index
57 <glil-label> make-glil-label glil-label?
60 <glil-branch> make-glil-branch glil-branch?
61 glil-branch-int glil-branch-label
63 <glil-call> make-glil-call glil-call?
64 glil-call-int glil-call-nargs))
66 (define-record (<glil-vars> nargs nrest nlocs nexts))
71 (<glil-asm> vars body)
79 (<glil-argument> op index)
80 (<glil-local> op index)
81 (<glil-external> op depth index)
82 (<glil-module> op module name)
85 (<glil-branch> inst label)
86 (<glil-call> inst nargs)))
93 ;;; (define (parse-glil x)
95 ;;; (('@asm args . body)
96 ;;; (let* ((env (make-new-env e))
97 ;;; (args (parse-args args env)))
98 ;;; (make-asm env args (map-parse body env))))
100 ;;; (error "Invalid assembly code:" x))))
102 ;;; (define (parse-args x e)
103 ;;; (let ((args (cond ((symbol? x) (make-args (list (make-local-var x)) #t))
104 ;;; ((list? x) (make-args (map make-local-var x) #f))
105 ;;; (else (let loop ((l x) (v '()))
107 ;;; (loop (cdr l) (cons (car l) v))
108 ;;; (make-args (map make-local-var
109 ;;; (reverse! (cons l v)))
111 ;;; (for-each (lambda (v) (env-add! e v)) (args-vars args))
114 ;;; (define (map-parse x e)
115 ;;; (map (lambda (x) (parse x e)) x))
117 ;;; (define (parse x e)
119 ;;; ;; (@asm ARGS BODY...)
120 ;;; (('@asm args . body)
122 ;;; ;; (@bind VARS BODY...)
123 ;;; ;; (@block VARS BODY...)
124 ;;; (((or '@bind '@block) vars . body)
125 ;;; (let* ((offset (env-nvars e))
126 ;;; (vars (args-vars (parse-args vars e)))
127 ;;; (block (make-block (car x) offset vars (map-parse body e))))
128 ;;; (for-each (lambda (v) (env-remove! e)) vars)
135 ;;; (make-const obj))
138 ;;; (((or 'ref 'set) name)
139 ;;; (make-access (car x) (env-ref e name)))
142 ;;; (make-label label))
145 ;;; (((or 'br-if 'jump) label)
146 ;;; (make-instl (car x) label))
148 ;;; ;; (tail-call NARGS)
149 ;;; (((or 'call 'tail-call) n)
150 ;;; (make-instn (car x) n))
153 ;;; (if (instruction? inst)
155 ;;; (error "Unknown instruction:" inst)))))
162 (define (unparse glil)
165 ((<glil-asm> vars body)
166 `(@asm (,(glil-vars-nargs vars) ,(glil-vars-nrest vars)
167 ,(glil-vars-nlocs vars) ,(glil-vars-nexts vars))
168 ,@(map unparse body)))
169 ((<glil-bind> vars) `(@bind ,@vars))
170 ((<glil-unbind>) `(@unbind))
171 ((<glil-source> loc) `(@source ,(car loc) ,(cdr loc)))
173 ((<glil-void>) `(void))
174 ((<glil-const> obj) `(const ,obj))
176 ((<glil-argument> op index)
177 `(,(symbol-append 'argument- op) ,index))
178 ((<glil-local> op index)
179 `(,(symbol-append 'local- op) ,index))
180 ((<glil-external> op depth index)
181 `(,(symbol-append 'external- op) ,depth ,index))
182 ((<glil-module> op module name)
183 `(,(symbol-append 'module- op) ,module ,name))
185 ((<glil-label> label) label)
186 ((<glil-branch> inst label) `(,inst ,label))
187 ((<glil-call> inst nargs) `(,inst ,nargs))))
194 (define (pprint-glil glil . port)
195 (let ((port (if (pair? port) (car port) (current-output-port))))
196 (let print ((code (unparse glil)) (column 0))
197 (display (make-string column #\space) port)
198 (cond ((and (pair? code) (eq? (car code) '@asm))
199 (format port "(@asm ~A\n" (cadr code))
200 (let ((col (+ column 2)))
201 (let loop ((l (cddr code)))
205 (begin (newline port) (loop (cdr l)))))))
206 (else (write code port))))