~ubuntu-branches/ubuntu/quantal/gclcvs/quantal

« back to all changes in this revision

Viewing changes to cmpnew/gcl_cmpinline.lsp

  • Committer: Bazaar Package Importer
  • Author(s): Camm Maguire
  • Date: 2004-06-24 15:13:46 UTC
  • Revision ID: james.westby@ubuntu.com-20040624151346-xh0xaaktyyp7aorc
Tags: 2.7.0-26
C_GC_OFFSET is 2 on m68k-linux

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
;;; CMPINLINE  Open coding optimizer.
 
2
;;;
 
3
;; Copyright (C) 1994 M. Hagiya, W. Schelter, T. Yuasa
 
4
 
 
5
;; This file is part of GNU Common Lisp, herein referred to as GCL
 
6
;;
 
7
;; GCL is free software; you can redistribute it and/or modify it under
 
8
;;  the terms of the GNU LIBRARY GENERAL PUBLIC LICENSE as published by
 
9
;; the Free Software Foundation; either version 2, or (at your option)
 
10
;; any later version.
 
11
;; 
 
12
;; GCL is distributed in the hope that it will be useful, but WITHOUT
 
13
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
14
;; FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public 
 
15
;; License for more details.
 
16
;; 
 
17
;; You should have received a copy of the GNU Library General Public License 
 
18
;; along with GCL; see the file COPYING.  If not, write to the Free Software
 
19
;; Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 
20
 
 
21
 
 
22
(in-package 'compiler)
 
23
 
 
24
;;; Pass 1 generates the internal form
 
25
;;;     ( id  info-object . rest )
 
26
;;; for each form encountered.
 
27
 
 
28
;;;  Change changed-vars and referrred-vars slots in info structure to arrays
 
29
;;;  for dramatic compilation speed improvements when the number of variables
 
30
;;;  are large, as occurs at present in running the random-int-form tester.
 
31
;;;  20040320 CM
 
32
 
 
33
 
 
34
(defmacro mia (x y) `(make-array ,x :adjustable t :fill-pointer ,y))
 
35
(defmacro eql-not-nil (x y) `(and ,x (eql ,x ,y)))
 
36
 
 
37
(defstruct (info (:copier old-copy-info))
 
38
  (type t)              ;;; Type of the form.
 
39
  (sp-change nil)       ;;; Whether execution of the form may change
 
40
                        ;;; the value of a special variable *VS*.
 
41
  (volatile nil)        ;;; whether there is a possible setjmp
 
42
  (changed-array (mia 10 0))     ;;; List of var-objects changed by the form. 
 
43
  (referred-array (mia 10 0)))   ;;; List of var-objects referred in the form.
 
44
 
 
45
(defun copy-array (array)
 
46
  (declare ((vector t) array))
 
47
  (let ((new-array (mia (the fixnum (array-total-size array)) (length array))))
 
48
    (declare ((vector t) new-array))
 
49
    (do ((i 0 (1+ i))) ((>= i (length array)) new-array)
 
50
      (declare (fixnum i))
 
51
      (setf (aref new-array i) (aref array i)))))
 
52
 
 
53
(defun copy-info (info)
 
54
  (let ((new-info (old-copy-info info)))
 
55
    (setf (info-referred-array new-info)
 
56
          (copy-array (info-referred-array info)))
 
57
    (setf (info-changed-array new-info)
 
58
          (copy-array (info-changed-array info)))    
 
59
    new-info))
 
60
 
 
61
(defun bsearchleq (x a i j le)
 
62
  (declare (object x le) ((vector t) a) (fixnum i j))
 
63
  (when (eql i j)
 
64
    (return-from bsearchleq (if (or le (and (< i (length a)) (eq x (aref a i)))) i (length a))))
 
65
  (let* ((k (the fixnum (+ i (the fixnum (ash (the fixnum (- j i) ) -1)))))
 
66
         (y (aref a k)))
 
67
    (declare (fixnum k) (object y))
 
68
    (cond ((si::objlt x y)
 
69
           (bsearchleq x a i k le))
 
70
          ((eq x y) k)
 
71
          (t (bsearchleq x a (1+ k) j le)))))
 
72
 
 
73
(defun push-array (x ar s lin)
 
74
  (declare  (object x lin) ((vector t) ar) (fixnum s) (ignore lin))
 
75
;        (j (if lin
 
76
;                (do ((k s (1+ k))) ((or (eql k (length ar)) (si::objlt x (aref ar k)) (eq x (aref ar k))) k)
 
77
;                  (declare (fixnum k)))
 
78
;                (bsearchleq x ar s (length ar)))))
 
79
  (let ((j (bsearchleq x ar s (length ar) t)))
 
80
    (declare (fixnum j))
 
81
    (when (and (< j (length ar)) (eq (aref ar j) x))
 
82
        (return-from push-array -1))
 
83
    (let ((ar (if (eql (length ar) (the fixnum (array-total-size ar)))
 
84
                  (adjust-array ar (the fixnum (* 2 (length ar))))
 
85
                ar)))
 
86
      (declare ((vector t) ar))
 
87
      (do ((i (length ar) (1- i))) ((<= i j))
 
88
        (declare (fixnum i))
 
89
        (setf (aref ar i) (aref ar (the fixnum (1- i)))))
 
90
      (setf (aref ar j) x)
 
91
      (setf (fill-pointer ar) (the fixnum (1+ (length ar))))
 
92
      j)))
 
93
 
 
94
 
 
95
(defmacro do-array ((v oar) &rest body)
 
96
  (let ((count (gensym)) (ar (gensym)))
 
97
    `(let* ((,ar ,oar))
 
98
       (declare ((vector t) ,ar))
 
99
       (do ((,count 0 (1+ ,count))) ((eql ,count (length ,ar)))
 
100
         (declare (fixnum ,count))
 
101
         (let ((,v (aref ,ar ,count)))
 
102
           ,@body)))))
 
103
 
 
104
(defmacro in-array (v ar)
 
105
  `(< (bsearchleq ,v ,ar 0 (length ,ar) nil) (length ,ar)))
 
106
 
 
107
 
 
108
(defmacro do-referred ((v info) &rest body)
 
109
  `(do-array (,v (info-referred-array ,info)) ,@body))
 
110
(defmacro do-changed ((v info) &rest body)
 
111
  `(do-array (,v (info-changed-array ,info)) ,@body))
 
112
(defmacro is-referred (var info)
 
113
  `(in-array ,var (info-referred-array ,info)))
 
114
(defmacro is-changed (var info)
 
115
  `(in-array ,var (info-changed-array ,info)))
 
116
(defmacro push-referred (var info)
 
117
  `(push-array ,var (info-referred-array ,info) 0 nil))
 
118
(defmacro push-changed (var info)
 
119
  `(push-array ,var (info-changed-array ,info) 0 nil))
 
120
(defmacro push-referred-with-start (var info s lin)
 
121
  `(push-array ,var (info-referred-array ,info) ,s ,lin))
 
122
(defmacro push-changed-with-start (var info s lin)
 
123
  `(push-array ,var (info-changed-array ,info) ,s ,lin))
 
124
(defmacro changed-length (info)
 
125
  `(length (info-changed-array ,info)))
 
126
(defmacro referred-length (info)
 
127
  `(length (info-referred-array ,info)))
 
128
 
 
129
 
 
130
(defvar *info* (make-info))
 
131
 
 
132
(defun mlin (x y)
 
133
  (declare (fixnum x y))
 
134
  (when (<= y 3)
 
135
    (return-from mlin nil))
 
136
  (let ((ly
 
137
         (do ((tl y (ash tl -1)) (k -1 (1+ k))) ((eql tl 0) k)
 
138
           (declare (fixnum k tl)))))
 
139
    (declare (fixnum ly))
 
140
    (let ((lyr (the fixnum (truncate y (the fixnum (1- ly))))))
 
141
      (declare (fixnum lyr))
 
142
      (> x (the fixnum (1+ lyr))))))
 
143
 
 
144
(defun add-info (to-info from-info)
 
145
  ;; Allow nil from-info without error CM 20031030
 
146
  (unless from-info
 
147
    (return-from add-info to-info))
 
148
  (let* ((s 0)
 
149
         (lin)); (mlin (changed-length from-info) (changed-length to-info))))
 
150
    (declare (fixnum s) (object lin))
 
151
    (do-changed (v from-info)
 
152
                (let ((res (push-changed-with-start v to-info s lin)))
 
153
                  (declare (fixnum res))
 
154
                  (when (>= res 0)
 
155
                    (setq s (the fixnum (1+ res)))))))
 
156
  (let* ((s 0)
 
157
         (lin)); (mlin (referred-length from-info) (referred-length to-info))))
 
158
    (declare (fixnum s) (object lin))
 
159
    (do-referred (v from-info)
 
160
                 (let ((res (push-referred-with-start v to-info s lin)))
 
161
                   (declare (fixnum res))
 
162
                   (when (>= res 0)
 
163
                     (setq s (the fixnum (1+ res)))))))
 
164
  (when (info-sp-change from-info)
 
165
    (setf (info-sp-change to-info) t))
 
166
  ;; Return to-info, CM 20031030
 
167
  to-info)
 
168
 
 
169
(defun args-info-changed-vars (var forms)
 
170
  (case (var-kind var)
 
171
    ((LEXICAL FIXNUM CHARACTER LONG-FLOAT SHORT-FLOAT OBJECT)
 
172
     (dolist** (form forms)
 
173
               (when (is-changed var (cadr form))
 
174
                 (return-from args-info-changed-vars t))))
 
175
    (REPLACED nil)
 
176
    (t (dolist** (form forms nil)
 
177
                 (when (or (is-changed var (cadr form))
 
178
                           (info-sp-change (cadr form)))
 
179
                   (return-from args-info-changed-vars t)))))
 
180
  )
 
181
 
 
182
;; Variable references in arguments can also be via replaced variables
 
183
;; (see gcl_cmplet.lsp) It appears that this is not necessary when
 
184
;; checking for changed variables, as matches would appear to require
 
185
;; that the variable not be replaced.  It might be better to provide a
 
186
;; new slot in the var structure to point to the variable by which one
 
187
;; is replaced -- one would need to consider chains in such a case.
 
188
;; Here we match on the C variable reference, which should be complete.
 
189
;; 20040306 CM
 
190
 
 
191
(defun var-rep-loc (x)
 
192
  (and
 
193
   (eq (var-kind x) 'replaced)
 
194
   (consp (var-loc x)) ;; may not be necessary, but vars can also be replaced to 'locations
 
195
                       ;; see gcl_cmplet.lsp
 
196
   (cadr (var-loc x))))
 
197
 
 
198
(defun is-rep-referred (var info)
 
199
  (let ((rx (var-rep-loc var)))
 
200
    (do-referred (v info)
 
201
     (let ((ry (var-rep-loc v)))
 
202
       (when (or (eql-not-nil (var-loc var) ry)
 
203
                 (eql-not-nil (var-loc v) rx)
 
204
                 (eql-not-nil rx ry))
 
205
         (return-from is-rep-referred t))))))
 
206
 
 
207
(defun args-info-referred-vars (var forms)
 
208
  (case (var-kind var)
 
209
        ((LEXICAL REPLACED FIXNUM CHARACTER LONG-FLOAT SHORT-FLOAT OBJECT)
 
210
         (dolist** (form forms nil)
 
211
           (when (or (is-referred var (cadr form))
 
212
                     (is-rep-referred var (cadr form)))
 
213
                 (return-from args-info-referred-vars t))))
 
214
        (t (dolist** (form forms nil)
 
215
                     (when (or (is-referred var (cadr form))
 
216
                               (is-rep-referred var (cadr form))
 
217
                               (info-sp-change (cadr form)))
 
218
                       (return-from args-info-referred-vars t))))
 
219
        ))
 
220
 
 
221
;;; Valid property names for open coded functions are:
 
222
;;;  INLINE
 
223
;;;  INLINE-SAFE        safe-compile only
 
224
;;;  INLINE-UNSAFE      non-safe-compile only
 
225
;;;
 
226
;;; Each property is a list of 'inline-info's, where each inline-info is:
 
227
;;; ( types { type | boolean } side-effect new-object { string | function } ).
 
228
;;;
 
229
;;; For each open-codable function, open coding will occur only if there exits
 
230
;;; an appropriate property with the argument types equal to 'types' and with
 
231
;;; the return-type equal to 'type'.  The third element
 
232
;;; is T if and only if side effects may occur by the call of the function.
 
233
;;; Even if *VALUE-TO-GO* is TRASH, open code for such a function with side
 
234
;;; effects must be included in the compiled code.
 
235
;;; The forth element is T if and only if the result value is a new Lisp
 
236
;;; object, i.e., it must be explicitly protected against GBC.
 
237
 
 
238
(defvar *inline-functions* nil)
 
239
(defvar *inline-blocks* 0)
 
240
;;; *inline-functions* holds:
 
241
;;;     (...( function-name . inline-info )...)
 
242
;;;
 
243
;;; *inline-blocks* holds the number of temporary cvars used to save
 
244
;;; intermediate results during evaluation of inlined function calls.
 
245
;;; This variable is used to close up blocks introduced to declare static
 
246
;;; c variables.
 
247
 
 
248
(defvar *special-types* '(FIXNUM CHARACTER LONG-FLOAT SHORT-FLOAT integer))
 
249
 
 
250
(defun inc-inline-blocks()
 
251
  (cond ((consp *inline-blocks*)
 
252
         (incf (car *inline-blocks*)))
 
253
        (t (incf *inline-blocks*))))
 
254
 
 
255
(defun inline-args (forms types &optional fun &aux (locs nil) ii)
 
256
  (do ((forms forms (cdr forms))
 
257
       (types types (cdr types)))
 
258
      ((endp forms) (reverse locs))
 
259
      (declare (object forms types))
 
260
      (let ((form (car forms))
 
261
            (type (car types)))
 
262
        (declare (object form type))
 
263
        (case (car form)
 
264
              (LOCATION (push (coerce-loc (caddr form) type) locs))
 
265
              (VAR
 
266
               (cond ((args-info-changed-vars (caaddr form) (cdr forms))
 
267
                      (cond ((and (member (var-kind (caaddr form))
 
268
                                         *special-types*)
 
269
                                  (eq type (var-kind (caaddr form))))
 
270
                             (let ((cvar (next-cvar)))
 
271
                               (wt-nl "{" (rep-type type) "V" cvar "= V"
 
272
                                      (var-loc (caaddr form)) ";")
 
273
                               (push (list 'cvar cvar 'inline-args) locs)
 
274
                               (inc-inline-blocks)))
 
275
                            (t 
 
276
                             (let ((temp (wt-c-push)))
 
277
                               (wt-nl temp "= ")
 
278
                               (wt-var (caaddr form) (cadr (caddr form)))
 
279
                               (wt ";")
 
280
                               (push (coerce-loc temp type) locs)))))
 
281
                     ((and (member (var-kind (caaddr form))
 
282
                                       '(FIXNUM LONG-FLOAT SHORT-FLOAT INTEGER))
 
283
                               (not (eq type (var-kind (caaddr form)))))
 
284
                      (let ((temp (cs-push type)))
 
285
                        (wt-nl "V" temp " = "
 
286
                               (coerce-loc (cons 'var (caddr form)) type) ";")
 
287
                        (push (list 'cvar temp) locs)))
 
288
                     (t (push (coerce-loc (cons 'VAR (caddr form)) type)
 
289
                              locs))))
 
290
              (CALL-GLOBAL
 
291
               (if (let ((fname (caddr form)))
 
292
                     (and (inline-possible fname)
 
293
                          (setq ii (get-inline-info
 
294
                                    fname (cadddr form)
 
295
                                    (info-type (cadr form))))
 
296
                          (progn  (save-avma ii) t)))
 
297
                   (let ((loc (get-inline-loc ii (cadddr form))))
 
298
                        (cond
 
299
                         ((or (and (flag-p (caddr ii) ans)(not *c-gc*))
 
300
                                                ; returns new object
 
301
                              (and (member (cadr ii)
 
302
                                           '(FIXNUM LONG-FLOAT SHORT-FLOAT))
 
303
                                   (not (eq type (cadr ii)))))
 
304
                          (let ((temp (cs-push type)))
 
305
                            (wt-nl "V" temp " = " (coerce-loc loc type) ";")
 
306
                            (push (list 'cvar temp) locs))
 
307
                         )
 
308
                         ((or (need-to-protect (cdr forms) (cdr types))
 
309
                              ;;if either new form or side effect,
 
310
                              ;;we don't want double evaluation
 
311
                              (and (flag-p (caddr ii) allocates-new-storage)
 
312
                                   (or (null fun)
 
313
                                       ;; Any fun such as list,list* which
 
314
                                       ;; does not cause side effects or
 
315
                                       ;; do double eval (ie not "@..")
 
316
                                       ;; could go here.
 
317
                                       (not
 
318
                                         (si::memq
 
319
                                           fun '(list-inline list*-inline)))))
 
320
                              (flag-p (caddr ii) is)
 
321
                              (and (flag-p (caddr ii) set) ; side-effectp
 
322
                                   (not (null (cdr forms)))))
 
323
                          (let (cvar)
 
324
                            (cond
 
325
                              ((eq type t)
 
326
                               (setq cvar (cs-push))
 
327
                               (wt-nl "V" cvar "= ")
 
328
                               (wt-loc loc))
 
329
                              (t (setq cvar (next-cvar))
 
330
                                 (wt-nl "{" (rep-type type) "V" cvar "= ")
 
331
                                 (case type
 
332
                                   (fixnum (wt-fixnum-loc loc))
 
333
                                   (integer (wt-integer-loc loc 'inline-args))
 
334
                                   (character (wt-character-loc loc))
 
335
                                   (long-float (wt-long-float-loc loc))
 
336
                                   (short-float (wt-short-float-loc loc))
 
337
                                   (otherwise (wt-loc loc)))
 
338
                                 (inc-inline-blocks)))
 
339
                            (wt ";")
 
340
                            (push (list 'cvar cvar 'inline-args) locs)
 
341
                            ))
 
342
                         (t (push (coerce-loc loc type) locs))))
 
343
                   (let ((temp (if *c-gc* (list 'cvar (cs-push)) (list 'vs (vs-push)))))
 
344
                        (let ((*value-to-go* temp)) (c2expr* form))
 
345
                        (push (coerce-loc temp type) locs))))
 
346
              (structure-ref
 
347
               (push (coerce-loc-structure-ref (cdr form) type)
 
348
                     locs))
 
349
              (SETQ
 
350
               (let ((vref (caddr form))
 
351
                     (form1 (cadddr form)))
 
352
                 (let ((*value-to-go* (cons 'var vref))) (c2expr* form1))
 
353
                 (cond ((eq (car form1) 'LOCATION)
 
354
                        (push (coerce-loc (caddr form1) type) locs))
 
355
                       (t
 
356
                         (setq forms (list* form
 
357
                                             (list 'VAR (cadr form) vref)
 
358
                                             (cdr forms)))
 
359
                         ;; want (setq types (list* type type (cdr  types)))
 
360
                         ;; but type is first of types
 
361
                          (setq types (list* type  types))))))
 
362
              (t (let
 
363
                     ((temp
 
364
                       (cond (*c-gc*
 
365
                              (cond ((eq type t)
 
366
                                     (list 'cvar (cs-push)))
 
367
                                    (t (push (cons type (next-cvar)) *c-vars*)
 
368
                                       (list 'var
 
369
                                             (make-var
 
370
                                              :type type
 
371
                                              :kind
 
372
                                              (if (member type
 
373
                                                          *special-types*)
 
374
                                                  type 'object)
 
375
                                                       :loc (cdar *c-vars*))
 
376
                                             nil
 
377
                                             ))))
 
378
                             (t  (list 'vs (vs-push))))))
 
379
                   (let ((*value-to-go* temp))
 
380
                     (c2expr* form)
 
381
                     (push (coerce-loc temp type) locs))))))))
 
382
 
 
383
(defun coerce-loc (loc type)
 
384
  (case type
 
385
        (fixnum (list 'FIXNUM-LOC loc))
 
386
        (integer (list 'integer-loc loc ))
 
387
        (character (list 'CHARACTER-LOC loc))
 
388
        (long-float (list 'LONG-FLOAT-LOC loc))
 
389
        (short-float (list 'SHORT-FLOAT-LOC loc))
 
390
        (t loc)))
 
391
 
 
392
(defun get-inline-loc (ii args &aux (fun (car (cdddr ii))) locs)
 
393
  ;;; Those functions that use GET-INLINE-LOC must rebind the variable *VS*.
 
394
 (setq locs (inline-args args (car ii) fun))
 
395
  (when (and (stringp fun) (char= (char (the string fun) 0) #\@))
 
396
    (let ((i 1) (saves nil))
 
397
         (declare (fixnum i))
 
398
      (do ((char (char (the string fun) i)
 
399
                 (char (the string fun) i)))
 
400
          ((char= char #\;) (incf i))
 
401
          (declare (character char))
 
402
          (push (the fixnum (- (char-code char) #.(char-code #\0))) saves)
 
403
          (incf i))
 
404
      (do ((l locs (cdr l))
 
405
           (n 0 (1+ n))
 
406
           (locs1 nil))
 
407
          ((endp l) (setq locs (reverse locs1)))
 
408
          (declare (fixnum n) (object l))
 
409
          (if (member n saves)
 
410
              (let* ((loc1 (car l)) (loc loc1) (coersion nil))
 
411
                    (declare (object loc loc1))
 
412
                (when (and (consp loc1)
 
413
                           (member (car loc1)
 
414
                                   '(FIXNUM-LOC integer-loc CHARACTER-LOC
 
415
                                     LONG-FLOAT-LOC SHORT-FLOAT-LOC)))
 
416
                      (setq coersion (car loc1))
 
417
                      (setq loc (cadr loc1))  ; remove coersion
 
418
                      )
 
419
                (cond
 
420
                 ((and (consp loc)
 
421
                       (or
 
422
                         (member (car loc) 
 
423
                                    '(INLINE INLINE-COND))
 
424
                         (and    (member (car loc)
 
425
                                         '(
 
426
                                           INLINE-FIXNUM inline-integer
 
427
                                           INLINE-CHARACTER INLINE-LONG-FLOAT
 
428
                                           INLINE-SHORT-FLOAT))
 
429
                                 (or (flag-p (cadr loc) allocates-new-storage)
 
430
                                     (flag-p (cadr loc) side-effect-p))
 
431
                                    )))
 
432
                  (wt-nl "{")
 
433
                  (inc-inline-blocks)
 
434
                  (let ((cvar (next-cvar)))
 
435
                    (push (list 'CVAR cvar) locs1)
 
436
                    (case coersion
 
437
                     ((nil) (wt "object V" cvar "= ") (wt-loc loc1))
 
438
                     (FIXNUM-LOC (wt "int V" cvar "= ") (wt-fixnum-loc loc))
 
439
                     (integer-loc (wt "GEN V" cvar "= ") (wt-integer-loc loc
 
440
                                                                         'get-inline-locs))
 
441
                     (CHARACTER-LOC
 
442
                      (wt "unsigned char V" cvar "= ") (wt-character-loc loc))
 
443
                     (LONG-FLOAT-LOC
 
444
                      (wt "double V" cvar "= ") (wt-long-float-loc loc))
 
445
                     (SHORT-FLOAT-LOC
 
446
                      (wt "float V" cvar "= ") (wt-short-float-loc loc))
 
447
                     (t (baboon))))
 
448
                  (wt ";")
 
449
                  )
 
450
                 (t (push loc1 locs1))))
 
451
              (push (car l) locs1)))))
 
452
  (list (inline-type (cadr ii))
 
453
        (caddr ii)
 
454
        fun
 
455
        locs)
 
456
  )
 
457
(defvar *inline-types*
 
458
  '((boolean . INLINE-COND)
 
459
    (fixnum . INLINE-FIXNUM)
 
460
    (character . INLINE-CHARACTER)
 
461
    (long-float . INLINE-LONG-FLOAT)
 
462
    (short-float . INLINE-SHORT-FLOAT)
 
463
    (integer . INLINE-INTEGER)
 
464
    (t . INLINE)))
 
465
 
 
466
(defun inline-type (type)
 
467
  (or (cdr (assoc type *inline-types*)) 'inline))
 
468
 
 
469
(defun get-inline-info (fname args return-type &aux x ii)
 
470
  (and  (fast-link-proclaimed-type-p fname args)
 
471
        (add-fast-link fname return-type args))
 
472
  (setq args (mapcar #'(lambda (form) (info-type (cadr form))) args))
 
473
  (when (if *safe-compile*
 
474
            (setq x (get fname 'inline-safe))
 
475
            (setq x (get fname 'inline-unsafe)))
 
476
        (dolist** (y x nil)
 
477
          (when (setq ii (inline-type-matches y args return-type))
 
478
                (return-from get-inline-info ii))))
 
479
  (when (setq x (get fname 'inline-always))
 
480
        (dolist** (y x)
 
481
          (when (setq ii (inline-type-matches y args return-type))
 
482
                (return-from get-inline-info ii))))
 
483
  (dolist* (x *inline-functions*)
 
484
        (when (and (eq (car x) fname)
 
485
                   (setq ii (inline-type-matches (cdr x) args return-type)))
 
486
                      (return-from get-inline-info ii)))
 
487
  ;; ( n . string , function ) or string , function
 
488
  
 
489
  (when (and (setq x (get fname 'vfun))
 
490
             (if (and (consp x) (typep (car x) 'fixnum))
 
491
                 (prog1 (>= (length args)  (car x)) (setq x (cdr x)))
 
492
               t))
 
493
        (return-from get-inline-info
 
494
                     (list (make-list (length args) :initial-element t)
 
495
                           t (flags allocates-new-storage side-effect-p)
 
496
                           #'(lambda (&rest l)
 
497
                               (wt "(VFUN_NARGS="(length l) ",")
 
498
                               (wt-inline-loc x l)
 
499
                               (wt ")")))))
 
500
  nil
 
501
  )
 
502
 
 
503
(defun inline-type-matches (inline-info arg-types return-type
 
504
                                        &aux (rts nil))
 
505
  (if (not (typep (third inline-info) 'fixnum))
 
506
      (fix-opt inline-info))
 
507
  (if (member 'integer (car inline-info))
 
508
      (return-from inline-type-matches nil))
 
509
  (if (and (let ((types (car inline-info)))
 
510
                (declare (object types))
 
511
                (dolist** (arg-type arg-types (or (equal types '(*))
 
512
                                                  (endp types)))
 
513
                        (when (endp types) (return nil))
 
514
                  (cond ((equal types '(*))
 
515
                         (setq types '(t *))))
 
516
                  (cond ((eq (car types) 'fixnum-float)
 
517
                         (cond ((type>= 'fixnum arg-type)
 
518
                                (push 'fixnum rts))
 
519
                               ((type>= 'long-float arg-type)
 
520
                                (push 'long-float rts))
 
521
                               ((type>= 'short-float arg-type)
 
522
                                (push 'short-float rts))
 
523
                               (t (return nil))))
 
524
                        ((type>= (car types) arg-type)
 
525
                         (push (car types) rts))
 
526
                        (t (return nil)))
 
527
                  (pop types)))
 
528
           (type>= (cadr inline-info) return-type))
 
529
       (cons (reverse rts) (cdr inline-info))
 
530
      nil)
 
531
  )
 
532
 
 
533
(defun need-to-protect (forms types &aux ii)
 
534
  (do ((forms forms (cdr forms))
 
535
       (types types (cdr types)))
 
536
      ((endp forms) nil)
 
537
      (declare (object forms types))
 
538
      (let ((form (car forms)))
 
539
        (declare (object form))
 
540
        (case (car form)
 
541
              (LOCATION)
 
542
              (VAR
 
543
               (when (or (args-info-changed-vars (caaddr form) (cdr forms))
 
544
                         (and (member (var-kind (caaddr form))
 
545
                                      '(FIXNUM LONG-FLOAT SHORT-FLOAT))
 
546
                              (not (eq (car types)
 
547
                                       (var-kind (caaddr form))))))
 
548
                     (return t)))
 
549
              (CALL-GLOBAL
 
550
               (let ((fname (caddr form)))
 
551
                    (declare (object fname))
 
552
                    (when
 
553
                     (or (not (inline-possible fname))
 
554
                         (null (setq ii (get-inline-info
 
555
                                         fname (cadddr form)
 
556
                                         (info-type (cadr form)))))
 
557
                         (flag-p (caddr ii) allocates-new-storage)
 
558
                         (flag-p (caddr ii) set)
 
559
                         (flag-p (caddr ii) is)
 
560
                         (and (member (cadr ii)
 
561
                                      '(fixnum long-float short-float))
 
562
                              (not (eq (car types) (cadr ii))))
 
563
                         (need-to-protect (cadddr form) (car ii)))
 
564
                     (return t))))
 
565
              (structure-ref
 
566
               (when (need-to-protect (list (caddr form)) '(t))
 
567
                     (return t)))
 
568
              (t (return t)))))
 
569
  )
 
570
 
 
571
(defun wt-c-push ()
 
572
  (cond (*c-gc* (inc-inline-blocks)
 
573
                (let ((tem (next-cvar)))
 
574
                  (wt "{" *volatile* "object V" tem ";")
 
575
                  (list 'cvar tem)))
 
576
        (t (list 'VS (vs-push)))))
 
577
 
 
578
(defun close-inline-blocks ( &aux (bl *inline-blocks*))
 
579
  (when (consp bl)
 
580
    (if (eql (cdr bl) 'restore-avma) (wt "restore_avma;"))
 
581
    (setq bl (car bl)))
 
582
  (dotimes** (i bl) (wt "}")))
 
583
 
 
584
(si:putprop 'inline 'wt-inline 'wt-loc)
 
585
(si:putprop 'inline-cond 'wt-inline-cond 'wt-loc)
 
586
(si:putprop 'inline-fixnum 'wt-inline-fixnum 'wt-loc)
 
587
(si:putprop 'inline-integer 'wt-inline-integer 'wt-loc)
 
588
(si:putprop 'inline-character 'wt-inline-character 'wt-loc)
 
589
(si:putprop 'inline-long-float 'wt-inline-long-float 'wt-loc)
 
590
(si:putprop 'inline-short-float 'wt-inline-short-float 'wt-loc)
 
591
 
 
592
(defun wt-inline-loc (fun locs &aux (i 0) (max -1))
 
593
       (declare (fixnum i max))
 
594
  (cond ((stringp fun)
 
595
         (when (char= (char (the string fun) 0) #\@)
 
596
           (setq i 1)
 
597
           (do ()
 
598
               ((char= (char (the string fun) i) #\;) (incf i))
 
599
               (incf i)))
 
600
         (do ((size (length (the string fun))))
 
601
             ((>= i size))
 
602
             (declare (fixnum size ))
 
603
             (let ((char (char (the string fun) i)))
 
604
                  (declare (character char))
 
605
                  (cond ((char= char #\#)
 
606
                         (let ((ch  (char (the string fun)
 
607
                                                        (the fixnum (1+ i))))
 
608
                               (n 0))
 
609
                           (cond ((eql ch #\*)
 
610
                                  (if (and (>= max 0)
 
611
                                           (< (1+ max) (length locs)))
 
612
                                      (wt ","))
 
613
                                  (do ((v  (nthcdr (1+ max) locs) (cdr v)))
 
614
                                      ((null v))
 
615
                                      (wt-loc (car v))
 
616
                                      (if (cdr v) (wt ","))))
 
617
                                 ((digit-char-p ch 10)
 
618
                                  (setq n (- (char-code ch)
 
619
                                             (char-code #\0)))
 
620
                                  (when (and
 
621
                                         (> (length fun) (+ i 2))
 
622
                                         (progn (setq ch (char (the string fun)
 
623
                                                        (+ i 2)))
 
624
                                         (digit-char-p ch)))
 
625
                                        (setq n (+ (* n 10)
 
626
                                                   (- (char-code ch)
 
627
                                                      (char-code #\0))))
 
628
                                        (incf i))
 
629
                                  (cond ((>= n max) (setq  max n)))
 
630
                                  (wt-loc (nth n locs)))))
 
631
                         (incf i 2))
 
632
                        (t
 
633
                         (princ char *compiler-output1*)
 
634
                         (incf i)))))
 
635
         )
 
636
        (t (apply fun locs))))
 
637
 
 
638
(defun wt-inline (side-effectp fun locs)
 
639
  (declare (ignore side-effectp))
 
640
  (wt-inline-loc fun locs))
 
641
 
 
642
(defun wt-inline-cond (side-effectp fun locs)
 
643
  (declare (ignore side-effectp))
 
644
  (wt "(") (wt-inline-loc fun locs) (wt "?Ct:Cnil)"))
 
645
 
 
646
(defun wt-inline-fixnum (side-effectp fun locs)
 
647
  (declare (ignore side-effectp))
 
648
  (when (zerop *space*) (wt "CMP"))
 
649
  (wt "make_fixnum((long)") (wt-inline-loc fun locs) (wt ")"))
 
650
 
 
651
(defun wt-inline-integer (side-effectp fun locs)
 
652
  (declare (ignore side-effectp))
 
653
  (wt "make_integer(") (wt-inline-loc fun locs) (wt ")"))
 
654
 
 
655
(defun wt-inline-character (side-effectp fun locs)
 
656
  (declare (ignore side-effectp))
 
657
  (wt "code_char(") (wt-inline-loc fun locs) (wt ")"))
 
658
 
 
659
(defun wt-inline-long-float (side-effectp fun locs)
 
660
  (declare (ignore side-effectp))
 
661
  (wt "make_longfloat(") (wt-inline-loc fun locs) (wt ")"))
 
662
 
 
663
(defun wt-inline-short-float (side-effectp fun locs)
 
664
  (declare (ignore side-effectp))
 
665
  (wt "make_shortfloat(") (wt-inline-loc fun locs) (wt ")"))
 
666
 
 
667
(defun args-cause-side-effect (forms &aux ii)
 
668
  (dolist** (form forms nil)
 
669
    (case (car form)
 
670
          ((LOCATION VAR structure-ref))
 
671
          (CALL-GLOBAL
 
672
           (let ((fname (caddr form)))
 
673
                (declare (object fname))
 
674
                (unless (and (inline-possible fname)
 
675
                             (setq ii (get-inline-info
 
676
                                       fname (cadddr form)
 
677
                                       (info-type (cadr form))))
 
678
                             (progn (fix-opt ii)
 
679
                                    (not (flag-p (caddr ii) side-effect-p)))
 
680
                                  )
 
681
                        (return t))))
 
682
          (otherwise (return t)))))
 
683
 
 
684
;;; Borrowed from CMPOPT.LSP
 
685
 
 
686
(defun list-inline (&rest x &aux tem (n (length x)))
 
687
   (cond ((setq tem
 
688
                (and (consp *value-to-go*)
 
689
                     (eq (car *value-to-go*) 'var)
 
690
                     (eq (var-type (second *value-to-go*)) :dynamic-extent)))
 
691
          (wt "(ALLOCA_CONS(" n "),ON_STACK_LIST(" n))
 
692
         (t (wt "list(" (length x))))
 
693
   (dolist (loc x) (wt #\, loc))
 
694
   (wt #\))
 
695
   (if tem (wt #\)))
 
696
)
 
697
 
 
698
 
 
699
(defun list*-inline (&rest x)
 
700
  (case (length x)
 
701
        (1 (wt (car x)))
 
702
        (2 (wt "make_cons(" (car x) "," (cadr x) ")"))
 
703
        (otherwise
 
704
         (wt "listA(" (length x)) (dolist (loc x) (wt #\, loc)) (wt #\)))))
 
705
 
 
706
;;; Borrowed from LFUN_LIST.LSP
 
707
 
 
708
(defun defsysfun (fname cname-string arg-types return-type
 
709
                        never-change-special-var-p predicate)
 
710
  ;;; The value NIL for each parameter except for fname means "not known".
 
711
  (when cname-string (si:putprop fname cname-string 'Lfun))
 
712
  (when arg-types
 
713
        (si:putprop fname (mapcar #'(lambda (x)
 
714
                                      (if (eq x '*) '* (type-filter x)))
 
715
                                      arg-types) 'arg-types))
 
716
 
 
717
  (when return-type
 
718
        (let ((rt (function-return-type (if (atom return-type)
 
719
                                            (list return-type)
 
720
                                          return-type))))
 
721
          (or  (consp rt) (setq rt (list rt)))
 
722
        (si:putprop fname (if (null (cdr rt)) (car rt) (cons 'values rt))
 
723
                                'return-type)))
 
724
  (when never-change-special-var-p (si:putprop fname t 'no-sp-change))
 
725
  (when predicate (si:putprop fname t 'predicate))
 
726
  )
 
727