~ubuntu-branches/ubuntu/intrepid/ecl/intrepid

« back to all changes in this revision

Viewing changes to src/cmp/cmputil.lsp

  • Committer: Bazaar Package Importer
  • Author(s): Peter Van Eynde
  • Date: 2006-05-17 02:46:26 UTC
  • Revision ID: james.westby@ubuntu.com-20060517024626-lljr08ftv9g9vefl
Tags: upstream-0.9h-20060510
ImportĀ upstreamĀ versionĀ 0.9h-20060510

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
;;;; CMPUTIL  --  Miscellaneous Functions.
 
2
 
 
3
;;;;  Copyright (c) 1984, Taiichi Yuasa and Masami Hagiya.
 
4
;;;;  Copyright (c) 1990, Giuseppe Attardi.
 
5
;;;;
 
6
;;;;    ECoLisp is free software; you can redistribute it and/or
 
7
;;;;    modify it under the terms of the GNU Library General Public
 
8
;;;;    License as published by the Free Software Foundation; either
 
9
;;;;    version 2 of the License, or (at your option) any later version.
 
10
;;;;
 
11
;;;;    See file '../Copyright' for full details.
 
12
 
 
13
(in-package "COMPILER")
 
14
 
 
15
(defvar *c1form-level* 0)
 
16
(defun print-c1forms (form)
 
17
  (cond ((consp form)
 
18
         (let ((*c1form-level* (1+ *c1form-level*)))
 
19
           (mapc #'print-c1forms form)))
 
20
        ((c1form-p form)
 
21
         (format t "~% ~D > ~A, parent ~A" *c1form-level* form (c1form-parent form))
 
22
         (print-c1forms (c1form-args form))
 
23
         form
 
24
         )))
 
25
 
 
26
(defun print-ref (ref-object stream)
 
27
  (let ((name (ref-name ref-object)))
 
28
    (if name
 
29
        (format stream "#<a ~A: ~A>" (type-of ref-object) name)
 
30
        (format stream "#<a ~A>" (type-of ref-object)))))
 
31
 
 
32
(defun print-var (var-object stream)
 
33
  (format stream "#<a VAR: ~A KIND: ~A>" (var-name var-object) (var-kind var-object)))
 
34
 
 
35
(defun cmperr (string &rest args &aux (*print-case* :upcase))
 
36
  (print-current-form)
 
37
  (format t "~&;;; Error: ")
 
38
  (apply #'format t string args)
 
39
  (incf *error-count*)
 
40
  (throw *cmperr-tag* '*cmperr-tag*))
 
41
 
 
42
(defun check-args-number (operator args &optional (min 0) (max nil))
 
43
  (let ((l (length args)))
 
44
    (when (< l min)
 
45
      (too-few-args operator min l))
 
46
    (when (and max (> l max))
 
47
      (too-many-args operator max l))))
 
48
 
 
49
(defun too-many-args (name upper-bound n &aux (*print-case* :upcase))
 
50
  (print-current-form)
 
51
  (format t
 
52
          "~&;;; ~S requires at most ~R argument~:p, ~
 
53
          but ~R ~:*~[were~;was~:;were~] supplied.~%"
 
54
          name
 
55
          upper-bound
 
56
          n)
 
57
  (incf *error-count*)
 
58
  (throw *cmperr-tag* '*cmperr-tag*))
 
59
 
 
60
(defun too-few-args (name lower-bound n &aux (*print-case* :upcase))
 
61
  (print-current-form)
 
62
  (format t
 
63
          "~&;;; ~S requires at least ~R argument~:p, ~
 
64
          but only ~R ~:*~[were~;was~:;were~] supplied.~%"
 
65
          name
 
66
          lower-bound
 
67
          n)
 
68
  (incf *error-count*)
 
69
  (throw *cmperr-tag* '*cmperr-tag*))
 
70
 
 
71
(defun cmpwarn (string &rest args &aux (*print-case* :upcase))
 
72
  (unless *suppress-compiler-warnings*
 
73
    (print-current-form)
 
74
    (format t "~&;;; Warning: ")
 
75
    (apply #'format t string args)
 
76
    (terpri))
 
77
  nil)
 
78
 
 
79
(defun cmpnote (string &rest args &aux (*print-case* :upcase))
 
80
  (unless *suppress-compiler-notes* 
 
81
    (format t "~&;;; Note: ")
 
82
    (apply #'format t string args)
 
83
    (terpri))
 
84
  nil)
 
85
 
 
86
(defun print-current-form ()
 
87
  (unless *suppress-compiler-notes*
 
88
    (let ((*print-length* 2)
 
89
          (*print-level* 2))
 
90
      (format t "~&;;; Compiling ~s.~%" *current-form*)))
 
91
  nil)
 
92
 
 
93
(defun print-emitting (f)
 
94
  (let* ((name (fun-name f)))
 
95
    (unless name
 
96
      (setf name (fun-description f)))
 
97
    (when (and name (not *suppress-compiler-notes*))
 
98
      (format t "~&;;; Emitting code for ~s.~%" name))))
 
99
 
 
100
(defun undefined-variable (sym &aux (*print-case* :upcase))
 
101
  (print-current-form)
 
102
  (format t
 
103
          "~&;;; The variable ~s is undefined.~
 
104
           ~%;;; The compiler will assume this variable is a global.~%"
 
105
          sym)
 
106
  nil)
 
107
 
 
108
(defun baboon (&aux (*print-case* :upcase))
 
109
  (print-current-form)
 
110
  (error "~&;;; A bug was found in the compiler.  Contact worm@arrakis.es.~%")
 
111
  (format
 
112
   t "~&;;; A bug was found in the compiler.  Contact worm@arrakis.es.~%")
 
113
  (incf *error-count*)
 
114
  (break)
 
115
;  (throw *cmperr-tag* '*cmperr-tag*) DEBUG
 
116
)
 
117
 
 
118
(defun cmp-eval (form &aux (throw-flag t))
 
119
  (unwind-protect
 
120
       (prog1
 
121
           (cmp-toplevel-eval form)
 
122
         (setq throw-flag nil))
 
123
    (when throw-flag
 
124
      (let ((*print-case* :upcase))
 
125
        (print-current-form)
 
126
        (format t "~&;;; The form ~s was not evaluated successfully.~
 
127
                   ~%;;; You are recommended to compile again.~%"
 
128
                form)))))
 
129
 
 
130
 
 
131
(defun cmp-macroexpand (form &aux env (throw-flag t))
 
132
  ;; Obtain the local macro environment for expansion.
 
133
  (dolist (v *funs*)
 
134
    (when (consp v) (push v env)))
 
135
  (when env (setq env (cons nil (nreverse env))))
 
136
  (unwind-protect
 
137
      (prog1
 
138
          (cmp-toplevel-eval `(macroexpand ',form ',env))
 
139
        (setq throw-flag nil))
 
140
    (when throw-flag
 
141
      (let ((*print-case* :upcase))
 
142
        (print-current-form)
 
143
        (format t
 
144
                "~&;;; The macro form ~s was not expanded successfully.~
 
145
                   ~%;;; You are recommended to compile again.~%"
 
146
                form)))))
 
147
 
 
148
(defun cmp-expand-macro (fd fname args &aux env (throw-flag t))
 
149
  (dolist (v *funs*)
 
150
    (when (consp v) (push v env)))
 
151
  (when env (setq env (cons nil (nreverse env))))
 
152
  (unwind-protect
 
153
      (prog1
 
154
          (cmp-toplevel-eval
 
155
           `(funcall *macroexpand-hook* ',fd ',(cons fname args) ',env))
 
156
        (setq throw-flag nil))
 
157
    (if throw-flag
 
158
        (let ((*print-case* :upcase))
 
159
          (print-current-form)
 
160
          (format t
 
161
                  "~&;;; The macro form (~s ...) was not expanded successfully.~
 
162
                   ~%;;; You are recommended to compile again.~%"
 
163
                  fname)))))
 
164
 
 
165
(defun cmp-expand-compiler-macro (fd fname args &aux env (throw-flag t))
 
166
  (dolist (v *funs*)
 
167
    (when (consp v) (push v env)))
 
168
  (when env (setq env (cons nil (nreverse env))))
 
169
  (let ((form (cons fname args)))
 
170
    (unwind-protect
 
171
         (let ((new-form (cmp-toplevel-eval `(funcall *macroexpand-hook* ',fd ',form ',env))))
 
172
           (setq throw-flag nil)
 
173
           (values new-form (not (eql new-form form))))
 
174
      (when throw-flag
 
175
          (let ((*print-case* :upcase))
 
176
            (print-current-form)
 
177
            (format t
 
178
                    "~&;;; The macro form (~s ...) was not expanded successfully.~
 
179
                   ~%;;; You are recommended to compile again.~%"
 
180
                    fname))))))
 
181
 
 
182
(defun cmp-toplevel-eval (form)
 
183
   (let*
 
184
     #-:CCL
 
185
     ((sys::*ihs-base* sys::*ihs-top*)
 
186
      (sys::*ihs-top* (sys::ihs-top 'cmp-toplevel-eval))
 
187
      (*break-enable* *compiler-break-enable*)
 
188
      (sys::*break-hidden-packages*
 
189
       (cons (find-package 'compiler)
 
190
             sys::*break-hidden-packages*)))
 
191
     #+:CCL
 
192
     ((*break-on-errors* *compiler-break-enable*))
 
193
         (eval form)))
 
194
 
 
195
(defun si::compiler-clear-compiler-properties (symbol)
 
196
  #-:CCL
 
197
  ;(sys::unlink-symbol symbol)
 
198
  (rem-sysprop symbol 't1)
 
199
  (rem-sysprop symbol 't2)
 
200
  (rem-sysprop symbol 't3)
 
201
  (rem-sysprop symbol 'c1)
 
202
  (rem-sysprop symbol 'c2)
 
203
  (rem-sysprop symbol 'c1conditional)
 
204
  (rem-sysprop symbol ':inline-always)
 
205
  (rem-sysprop symbol ':inline-unsafe)
 
206
  (rem-sysprop symbol ':inline-safe)
 
207
  (rem-sysprop symbol 'lfun))
 
208
  
 
209
(defun lisp-to-c-name (obj)
 
210
  "Translate Lisp object prin1 representation to valid C identifier name"
 
211
  (and obj 
 
212
       (map 'string 
 
213
            #'(lambda (c)
 
214
                (let ((cc (char-code c)))
 
215
                  (if (or (<= #.(char-code #\a) cc #.(char-code #\z))
 
216
                          (<= #.(char-code #\0) cc #.(char-code #\9)))
 
217
                      c #\_)))
 
218
            (string-downcase (prin1-to-string obj)))))
 
219
 
 
220
(defun proper-list-p (x &optional test)
 
221
  (and (listp x)
 
222
       (handler-case (list-length x) (type-error (c) nil))
 
223
       (or (null test) (every test x))))