define-type no longer expects `|' subform
[bpt/guile.git] / module / system / il / inline.scm
CommitLineData
22bcbe8c
AW
1;;; GHIL macros
2
3;; Copyright (C) 2001 Free Software Foundation, Inc.
4
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)
8;; any later version.
9;;
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.
14;;
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.
19
20;;; Code:
21
22(define-module (system il inline)
1a1a10d3
AW
23 #:use-module (system base syntax)
24 #:use-module (system il ghil)
25 #:use-module (srfi srfi-16)
26 #:export (*inline-table* define-inline try-inline try-inline-with-env))
22bcbe8c
AW
27
28(define *inline-table* '())
29
30(define-macro (define-inline sym . clauses)
31 (define (inline-args args)
32 (let lp ((in args) (out '()))
33 (cond ((null? in) `(list ,@(reverse out)))
34 ((symbol? in) `(cons* ,@(reverse out) ,in))
35 ((pair? (car in))
36 (lp (cdr in)
37 (cons `(or (try-inline ,(caar in) ,(inline-args (cdar in)))
38 (error "what" ',(car in)))
39 out)))
40 ((symbol? (car in))
41 ;; assume it's locally bound
42 (lp (cdr in) (cons (car in) out)))
43 ((number? (car in))
44 (lp (cdr in) (cons `(make-ghil-quote #f #f ,(car in)) out)))
45 (else
46 (error "what what" (car in))))))
47 (define (consequent exp)
48 (cond
49 ((pair? exp)
50 `(make-ghil-inline #f #f ',(car exp) ,(inline-args (cdr exp))))
51 ((symbol? exp)
52 ;; assume locally bound
53 exp)
54 ((number? exp)
55 `(make-ghil-quote #f #f ,exp))
56 (else (error "bad consequent yall" exp))))
57 `(set! *inline-table*
58 (assq-set! *inline-table*
59 ,sym
60 (case-lambda
61 ,@(let lp ((in clauses) (out '()))
62 (if (null? in)
63 (reverse (cons '(else #f) out))
64 (lp (cddr in)
65 (cons `(,(car in)
66 ,(consequent (cadr in))) out))))))))
67
68(define (try-inline head-value args)
69 (and=> (assq-ref *inline-table* head-value)
70 (lambda (proc) (apply proc args))))
71
22bcbe8c
AW
72
73(define (try-inline-with-env env loc exp)
74 (let ((sym (car exp)))
2e7e6969
AW
75 (let loop ((e env))
76 (record-case e
77 ((<ghil-toplevel-env> table)
78 (let ((mod (current-module)))
79 (and (not (assoc-ref table (cons (module-name mod) sym)))
80 (module-bound? mod sym)
81 (try-inline (module-ref mod sym) (cdr exp)))))
82 ((<ghil-env> parent table variables)
83 (and (not (assq-ref table sym))
84 (loop parent)))))))
22bcbe8c
AW
85
86(define-inline eq? (x y)
87 (eq? x y))
88
89(define-inline eqv? (x y)
90 (eqv? x y))
91
92(define-inline equal? (x y)
93 (equal? x y))
94
95(define-inline = (x y)
96 (ee? x y))
97
98(define-inline < (x y)
99 (lt? x y))
100
101(define-inline > (x y)
102 (gt? x y))
103
104(define-inline <= (x y)
105 (le? x y))
106
107(define-inline >= (x y)
108 (ge? x y))
109
6cc3f99e
AW
110(define-inline zero? (x)
111 (ee? x 0))
112
22bcbe8c
AW
113(define-inline +
114 () 0
115 (x) x
116 (x y) (add x y)
117 (x y . rest) (add x (+ y . rest)))
118
119(define-inline *
120 () 1
121 (x) x
122 (x y) (mul x y)
123 (x y . rest) (mul x (* y . rest)))
124
125(define-inline -
126 (x) (sub 0 x)
127 (x y) (sub x y)
128 (x y . rest) (sub x (+ y . rest)))
129
6cc3f99e
AW
130(define-inline 1-
131 (x) (sub x 1))
132
22bcbe8c
AW
133(define-inline /
134 (x) (div 1 x)
135 (x y) (div x y)
136 (x y . rest) (div x (* y . rest)))
137
138(define-inline quotient (x y)
139 (quo x y))
140
141(define-inline remainder (x y)
142 (rem x y))
143
144(define-inline modulo (x y)
145 (mod x y))
146
147(define-inline not (x)
148 (not x))
149
150(define-inline pair? (x)
151 (pair? x))
152
153(define-inline cons (x y)
154 (cons x y))
155
156(define-inline car (x) (car x))
157(define-inline cdr (x) (cdr x))
158
159(define-inline set-car! (x y) (set-car! x y))
160(define-inline set-cdr! (x y) (set-cdr! x y))
161
162(define-inline caar (x) (car (car x)))
163(define-inline cadr (x) (car (cdr x)))
164(define-inline cdar (x) (cdr (car x)))
165(define-inline cddr (x) (cdr (cdr x)))
166(define-inline caaar (x) (car (car (car x))))
167(define-inline caadr (x) (car (car (cdr x))))
168(define-inline cadar (x) (car (cdr (car x))))
169(define-inline caddr (x) (car (cdr (cdr x))))
170(define-inline cdaar (x) (cdr (car (car x))))
171(define-inline cdadr (x) (cdr (car (cdr x))))
172(define-inline cddar (x) (cdr (cdr (car x))))
173(define-inline cdddr (x) (cdr (cdr (cdr x))))
174(define-inline caaaar (x) (car (car (car (car x)))))
175(define-inline caaadr (x) (car (car (car (cdr x)))))
176(define-inline caadar (x) (car (car (cdr (car x)))))
177(define-inline caaddr (x) (car (car (cdr (cdr x)))))
178(define-inline cadaar (x) (car (cdr (car (car x)))))
179(define-inline cadadr (x) (car (cdr (car (cdr x)))))
180(define-inline caddar (x) (car (cdr (cdr (car x)))))
181(define-inline cadddr (x) (car (cdr (cdr (cdr x)))))
182(define-inline cdaaar (x) (cdr (car (car (car x)))))
183(define-inline cdaadr (x) (cdr (car (car (cdr x)))))
184(define-inline cdadar (x) (cdr (car (cdr (car x)))))
185(define-inline cdaddr (x) (cdr (car (cdr (cdr x)))))
186(define-inline cddaar (x) (cdr (cdr (car (car x)))))
187(define-inline cddadr (x) (cdr (cdr (car (cdr x)))))
188(define-inline cdddar (x) (cdr (cdr (cdr (car x)))))
189(define-inline cddddr (x) (cdr (cdr (cdr (cdr x)))))
190
191(define-inline null? (x)
192 (null? x))
193
194(define-inline list? (x)
195 (list? x))
196
22bcbe8c
AW
197(define-inline cons*
198 (x) x
199 (x y) (cons x y)
200 (x y . rest) (cons x (cons* y . rest)))