| 1 |
;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil; Package: SYSTEM -*- |
| 2 |
;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab: |
| 3 |
|
| 4 |
;;;; |
| 5 |
;;;; Copyright (c) 1984, Taiichi Yuasa and Masami Hagiya. |
| 6 |
;;;; Copyright (c) 1990, Giuseppe Attardi. |
| 7 |
;;;; Copyright (c) 2001, Juan Jose Garcia Ripoll. |
| 8 |
;;;; |
| 9 |
;;;; See file 'LICENSE' for the copyright details. |
| 10 |
|
| 11 |
(in-package "SYSTEM") |
| 12 |
|
| 13 |
(defmacro unless (pred &rest body) |
| 14 |
"Syntax: (unless test {form}*) |
| 15 |
If TEST evaluates to NIL, then evaluates FORMs and returns all values of the |
| 16 |
last FORM. If not, simply returns NIL." |
| 17 |
`(IF (NOT ,pred) (PROGN ,@body))) |
| 18 |
|
| 19 |
(defmacro defmacro (&whole whole name vl &body body &aux doc-string) |
| 20 |
;; Documentation in help.lsp |
| 21 |
(multiple-value-bind (function pprint doc-string) |
| 22 |
(sys::expand-defmacro name vl body) |
| 23 |
(setq function `(function ,function)) |
| 24 |
(when *dump-defun-definitions* |
| 25 |
(print function) |
| 26 |
(setq function `(si::bc-disassemble ,function))) |
| 27 |
`(eval-when (:compile-toplevel :load-toplevel :execute) |
| 28 |
,(ext:register-with-pde whole `(si::fset ',name ,function t ,pprint)) |
| 29 |
,@(si::expand-set-documentation name 'function doc-string) |
| 30 |
',name))) |
| 31 |
|
| 32 |
(defmacro defvar (&whole whole var &optional (form nil form-sp) doc-string) |
| 33 |
"Syntax: (defvar name [form [doc]]) |
| 34 |
Declares the variable named by NAME as a special variable. If the variable |
| 35 |
does not have a value, then evaluates FORM and assigns the value to the |
| 36 |
variable. FORM defaults to NIL. The doc-string DOC, if supplied, is saved |
| 37 |
as a VARIABLE doc and can be retrieved by (documentation 'NAME 'variable)." |
| 38 |
`(LOCALLY (DECLARE (SPECIAL ,var)) |
| 39 |
(SYS:*MAKE-SPECIAL ',var) |
| 40 |
,@(when form-sp |
| 41 |
`((UNLESS (BOUNDP ',var) |
| 42 |
(SETQ ,var ,form)))) |
| 43 |
,@(si::expand-set-documentation var 'variable doc-string) |
| 44 |
,(ext:register-with-pde whole) |
| 45 |
,(if *bytecodes-compiler* |
| 46 |
`(eval-when (:compile-toplevel) |
| 47 |
(sys:*make-special ',var)) |
| 48 |
`(eval-when (:compile-toplevel) |
| 49 |
(si::register-global ',var))) |
| 50 |
',var)) |
| 51 |
|
| 52 |
(defmacro defparameter (&whole whole var form &optional doc-string) |
| 53 |
"Syntax: (defparameter name form [doc]) |
| 54 |
Declares the global variable named by NAME as a special variable and assigns |
| 55 |
the value of FORM to the variable. The doc-string DOC, if supplied, is saved |
| 56 |
as a VARIABLE doc and can be retrieved by (documentation 'NAME 'variable)." |
| 57 |
`(LOCALLY (DECLARE (SPECIAL ,var)) |
| 58 |
(SYS:*MAKE-SPECIAL ',var) |
| 59 |
(SETQ ,var ,form) |
| 60 |
,@(si::expand-set-documentation var 'variable doc-string) |
| 61 |
,(ext:register-with-pde whole) |
| 62 |
,(if *bytecodes-compiler* |
| 63 |
`(eval-when (:compile-toplevel) |
| 64 |
(sys:*make-special ',var)) |
| 65 |
`(eval-when (:compile-toplevel) |
| 66 |
(si::register-global ',var))) |
| 67 |
',var)) |
| 68 |
|
| 69 |
(defmacro defconstant (&whole whole var form &optional doc-string) |
| 70 |
"Syntax: (defconstant symbol form [doc]) |
| 71 |
|
| 72 |
Declares that the global variable named by SYMBOL is a constant with the value |
| 73 |
of FORM as its constant value. The doc-string DOC, if supplied, is saved as a |
| 74 |
VARIABLE doc and can be retrieved by (DOCUMENTATION 'SYMBOL 'VARIABLE)." |
| 75 |
`(PROGN |
| 76 |
(SYS:*MAKE-CONSTANT ',var ,form) |
| 77 |
,@(si::expand-set-documentation var 'variable doc-string) |
| 78 |
,(ext:register-with-pde whole) |
| 79 |
,(if *bytecodes-compiler* |
| 80 |
`(eval-when (:compile-toplevel) |
| 81 |
(sys:*make-constant ',var ,form)) |
| 82 |
`(eval-when (:compile-toplevel) |
| 83 |
(sys:*make-constant ',var ,form) |
| 84 |
(si::register-global ',var))) |
| 85 |
',var)) |
| 86 |
|
| 87 |
(defparameter *defun-inline-hook* |
| 88 |
#'(lambda (fname form) |
| 89 |
;; To prevent inlining of stale function definitions by the native |
| 90 |
;; compiler after a new definition has been established by the |
| 91 |
;; bytecodes compiler, we save function definitions in the global |
| 92 |
;; environment when the function has been proclaimed inline. |
| 93 |
(let ((proclaimed (si:get-sysprop fname 'inline))) |
| 94 |
(when proclaimed |
| 95 |
`(eval-when (:load-toplevel :execute) |
| 96 |
(si:put-sysprop ',fname 'inline ',form)))))) |
| 97 |
|
| 98 |
(defmacro defun (&whole whole name vl &body body &aux doc-string) |
| 99 |
;; Documentation in help.lsp |
| 100 |
(multiple-value-setq (body doc-string) (remove-documentation body)) |
| 101 |
(let* ((function `#'(ext::lambda-block ,name ,vl ,@body)) |
| 102 |
(global-function `#'(ext::lambda-block ,name ,vl |
| 103 |
(declare (si::c-global)) |
| 104 |
,@body))) |
| 105 |
(when *dump-defun-definitions* |
| 106 |
(print function) |
| 107 |
(setq function `(si::bc-disassemble ,function))) |
| 108 |
`(progn |
| 109 |
,(ext:register-with-pde whole `(si::fset ',name ,global-function)) |
| 110 |
,@(si::expand-set-documentation name 'function doc-string) |
| 111 |
,(let ((hook *defun-inline-hook*)) |
| 112 |
(and hook (funcall hook name global-function))) |
| 113 |
',name))) |
| 114 |
|
| 115 |
;;; |
| 116 |
;;; This is a no-op unless the compiler is installed |
| 117 |
;;; |
| 118 |
(defmacro define-compiler-macro (&whole whole name vl &rest body) |
| 119 |
(multiple-value-bind (function pprint doc-string) |
| 120 |
(si:expand-defmacro name vl body 'cl:define-compiler-macro) |
| 121 |
(declare (ignore pprint)) |
| 122 |
(setq function `(function ,function)) |
| 123 |
(when *dump-defun-definitions* |
| 124 |
(print function) |
| 125 |
(setq function `(si::bc-disassemble ,function))) |
| 126 |
`(progn |
| 127 |
,(unless *bytecodes-compiler* |
| 128 |
`(eval-when (:compile-toplevel) |
| 129 |
(c::cmp-env-register-compiler-macro ',name ,function))) |
| 130 |
(put-sysprop ',name 'sys::compiler-macro ,function) |
| 131 |
,@(si::expand-set-documentation name 'compiler-macro doc-string) |
| 132 |
,(ext:register-with-pde whole) |
| 133 |
',name))) |
| 134 |
|
| 135 |
(defun compiler-macro-function (name &optional env) |
| 136 |
(declare (ignorable env)) |
| 137 |
(values (get-sysprop name 'sys::compiler-macro))) |
| 138 |
|
| 139 |
;;; Each of the following macros is also defined as a special form, |
| 140 |
;;; as required by CLtL. Some of them are used by the compiler (e.g. |
| 141 |
;;; dolist), some not at all (e.g. defun). |
| 142 |
;;; Thus their names need not be exported. |
| 143 |
|
| 144 |
(let () |
| 145 |
;; We enclose the macro in a LET form so that it is no longer a |
| 146 |
;; toplevel form. This solves the problem of this simple LOOP |
| 147 |
;; replacing the more complex form in loop.lsp when evalmacros.lsp |
| 148 |
;; gets compiled. |
| 149 |
(defmacro loop (&rest body &aux (tag (gensym))) |
| 150 |
"Syntax: (loop {form}*) |
| 151 |
Establishes a NIL block and executes FORMs repeatedly. The loop is normally |
| 152 |
terminated by a non-local exit." |
| 153 |
`(BLOCK NIL (TAGBODY ,tag (PROGN ,@body) (GO ,tag)))) |
| 154 |
) |
| 155 |
|
| 156 |
(defmacro lambda (&rest body) |
| 157 |
`(function (lambda ,@body))) |
| 158 |
|
| 159 |
(defmacro lambda-block (name lambda-list &rest lambda-body) |
| 160 |
(multiple-value-bind (decl body doc) |
| 161 |
(si::process-declarations lambda-body) |
| 162 |
(let ((decl (and decl (list (cons 'declare decl)))) |
| 163 |
(block-name (si:function-block-name name))) |
| 164 |
`(lambda ,lambda-list ,@doc ,@decl |
| 165 |
(declare (si::function-block-name ,block-name)) |
| 166 |
(block ,block-name ,@body))))) |
| 167 |
|
| 168 |
; assignment |
| 169 |
|
| 170 |
(defmacro psetq (&rest args) |
| 171 |
"Syntax: (psetq {var form}*) |
| 172 |
Similar to SETQ, but evaluates all FORMs first, and then assigns each value to |
| 173 |
the corresponding VAR. Returns NIL." |
| 174 |
(do ((l args (cddr l)) |
| 175 |
(forms nil) |
| 176 |
(bindings nil)) |
| 177 |
((endp l) (list* 'LET* (nreverse bindings) (nreverse (cons nil forms)))) |
| 178 |
(let ((sym (gensym))) |
| 179 |
(push (list sym (cadr l)) bindings) |
| 180 |
(push (list 'setq (car l) sym) forms))) |
| 181 |
) |
| 182 |
|
| 183 |
; conditionals |
| 184 |
|
| 185 |
(defmacro cond (&rest clauses &aux (form nil)) |
| 186 |
"Syntax: (cond {(test {form}*)}*) |
| 187 |
Evaluates TESTs in order until one evaluates to non-NIL. Then evaluates FORMs |
| 188 |
in order that follow the TEST and returns all values of the last FORM. If no |
| 189 |
forms follow the TEST, then returns the value of the TEST. Returns NIL, if no |
| 190 |
TESTs evaluates to non-NIL." |
| 191 |
(dolist (l (reverse clauses) form) ; don't use nreverse here |
| 192 |
(if (endp (cdr l)) |
| 193 |
(if (eq (car l) 't) |
| 194 |
(setq form 't) |
| 195 |
(let ((sym (gensym))) |
| 196 |
(setq form `(LET ((,sym ,(car l))) |
| 197 |
; (DECLARE (:READ-ONLY ,sym)) ; Beppe |
| 198 |
(IF ,sym ,sym ,form))))) |
| 199 |
(if (eq (car l) 't) |
| 200 |
(setq form (if (endp (cddr l)) |
| 201 |
(cadr l) |
| 202 |
`(PROGN ,@(cdr l)))) |
| 203 |
(setq form (if (endp (cddr l)) |
| 204 |
`(IF ,(car l) ,(cadr l) ,form) |
| 205 |
`(IF ,(car l) (PROGN ,@(cdr l)) ,form)))))) |
| 206 |
) |
| 207 |
|
| 208 |
; program feature |
| 209 |
|
| 210 |
(defmacro prog (vl &rest body &aux (decl nil)) |
| 211 |
"Syntax: (prog ({var | (var [init])}*) {decl}* {tag | statement}*) |
| 212 |
Establishes a NIL block, binds each VAR to the value of INIT (which defaults |
| 213 |
to NIL) in parallel, and executes STATEMENTs. Returns NIL." |
| 214 |
(multiple-value-setq (decl body) |
| 215 |
(find-declarations body)) |
| 216 |
`(BLOCK NIL (LET ,vl ,@decl (TAGBODY ,@body))) |
| 217 |
) |
| 218 |
|
| 219 |
(defmacro prog* (vl &rest body &aux (decl nil)) |
| 220 |
"Syntax: (prog* ({var | (var [init])}*) {decl}* {tag | statement}*) |
| 221 |
Establishes a NIL block, binds each VAR to the value of INIT (which defaults |
| 222 |
to NIL) sequentially, and executes STATEMENTs. Returns NIL." |
| 223 |
(multiple-value-setq (decl body) |
| 224 |
(find-declarations body)) |
| 225 |
`(BLOCK NIL (LET* ,vl ,@decl (TAGBODY ,@body))) |
| 226 |
) |
| 227 |
|
| 228 |
; sequencing |
| 229 |
|
| 230 |
(defmacro prog1 (first &rest body &aux (sym (gensym))) |
| 231 |
"Syntax: (prog1 first-form {form}*) |
| 232 |
Evaluates FIRST-FORM and FORMs in order. Returns the value of FIRST-FORM." |
| 233 |
`(LET ((,sym ,first)) |
| 234 |
; (DECLARE (:READ-ONLY ,sym)) ; Beppe |
| 235 |
,@body ,sym)) |
| 236 |
|
| 237 |
(defmacro prog2 (first second &rest body &aux (sym (gensym))) |
| 238 |
"Syntax: (prog2 first-form second-form {forms}*) |
| 239 |
Evaluates FIRST-FORM, SECOND-FORM, and FORMs in order. Returns the value of |
| 240 |
SECOND-FORM." |
| 241 |
`(PROGN ,first (LET ((,sym ,second)) |
| 242 |
; (DECLARE (:READ-ONLY ,sym)) ; Beppe |
| 243 |
,@body ,sym))) |
| 244 |
|
| 245 |
; multiple values |
| 246 |
|
| 247 |
(defmacro multiple-value-list (form) |
| 248 |
"Evaluates FORM and returns a list of all values FORM returns." |
| 249 |
`(MULTIPLE-VALUE-CALL 'LIST ,form)) |
| 250 |
|
| 251 |
(defmacro multiple-value-setq (vars form) |
| 252 |
"Syntax: (multiple-value-setq {var}* form) |
| 253 |
|
| 254 |
Evaluates FORM and binds the N-th VAR to the N-th value of FORM or, if FORM |
| 255 |
returns less than N values, to NIL. Returns the first value of FORM or, if |
| 256 |
FORM returns no value, NIL." |
| 257 |
(do ((vl vars (cdr vl)) |
| 258 |
(sym (gensym)) |
| 259 |
(forms nil) |
| 260 |
(n 0 (truly-the fixnum (1+ n)))) |
| 261 |
((endp vl) `(LET ((,sym (MULTIPLE-VALUE-LIST ,form))) ,@forms)) |
| 262 |
(declare (fixnum n)) |
| 263 |
(push `(SETQ ,(car vl) (NTH ,n ,sym)) forms))) |
| 264 |
|
| 265 |
;; We do not use this macroexpanso, and thus we do not care whether it is |
| 266 |
;; efficiently compiled by ECL or not. |
| 267 |
(defmacro multiple-value-bind (vars form &rest body) |
| 268 |
"Syntax: (multiple-value-bind ({var}*) init {decl}* {form}*) |
| 269 |
|
| 270 |
Evaluates INIT and binds the N-th VAR to the N-th value of INIT or, if INIT |
| 271 |
returns less than N values, to NIL. Then evaluates FORMs, and returns all |
| 272 |
values of the last FORM. If no FORM is given, returns NIL." |
| 273 |
(declare (notinline mapcar)) |
| 274 |
(let ((args (mapcar #'list vars)) |
| 275 |
(rest (gensym))) |
| 276 |
`(multiple-value-call #'(lambda (&optional ,@args &rest ,rest) ,@body) |
| 277 |
,form))) |
| 278 |
|
| 279 |
(defun while-until (test body jmp-op) |
| 280 |
(declare (si::c-local)) |
| 281 |
(let ((label (gensym)) |
| 282 |
(exit (gensym))) |
| 283 |
`(TAGBODY |
| 284 |
(GO ,exit) |
| 285 |
,label |
| 286 |
,@body |
| 287 |
,exit |
| 288 |
(,jmp-op ,test (GO ,label))))) |
| 289 |
|
| 290 |
(defmacro sys::while (test &body body) |
| 291 |
(while-until test body 'when)) |
| 292 |
|
| 293 |
(defmacro sys::until (test &body body) |
| 294 |
(while-until test body 'unless)) |
| 295 |
|
| 296 |
(defmacro case (keyform &rest clauses) |
| 297 |
(let* ((last t) |
| 298 |
(form nil) |
| 299 |
(key (gensym))) |
| 300 |
(dolist (clause (reverse clauses) |
| 301 |
`(LET ((,key ,keyform)) |
| 302 |
;;(DECLARE (:READ-ONLY ,key)) ; Beppe |
| 303 |
,form)) |
| 304 |
(let ((selector (car clause))) |
| 305 |
(cond ((or (eq selector T) (eq selector 'OTHERWISE)) |
| 306 |
(unless last |
| 307 |
(si::signal-simple-error |
| 308 |
'program-error nil |
| 309 |
"CASE: The selector ~A can only appear at the last position." |
| 310 |
(list selector))) |
| 311 |
(setq form `(PROGN ,@(cdr clause)))) |
| 312 |
((consp selector) |
| 313 |
(setq form `(IF (MEMBER ,key ',selector) |
| 314 |
(PROGN ,@(cdr clause)) |
| 315 |
,form))) |
| 316 |
(selector |
| 317 |
(setq form `(IF (EQL ,key ',selector) |
| 318 |
(PROGN ,@(cdr clause)) |
| 319 |
,form)))) |
| 320 |
(setq last nil))))) |
| 321 |
|
| 322 |
(defmacro return (&optional (val nil)) `(RETURN-FROM NIL ,val)) |
| 323 |
|
| 324 |
;; Declarations |
| 325 |
(let () |
| 326 |
(defmacro declaim (&rest decl-specs) |
| 327 |
(if (cdr decl-specs) |
| 328 |
`(eval-when (:compile-toplevel :load-toplevel :execute) |
| 329 |
(mapcar #'proclaim ',decl-specs)) |
| 330 |
`(eval-when (:compile-toplevel :load-toplevel :execute) |
| 331 |
(proclaim ',(car decl-specs))))) |
| 332 |
) |
| 333 |
|
| 334 |
(let () |
| 335 |
(defmacro c-declaim (&rest decl-specs) |
| 336 |
(if (cdr decl-specs) |
| 337 |
`(eval-when (:compile-toplevel) |
| 338 |
(mapcar #'proclaim ',decl-specs)) |
| 339 |
`(eval-when (:compile-toplevel) |
| 340 |
(proclaim ',(car decl-specs))))) |
| 341 |
) |
| 342 |
|
| 343 |
(defmacro in-package (name) |
| 344 |
`(eval-when (:compile-toplevel :load-toplevel :execute) |
| 345 |
(si::select-package ,(string name)))) |
| 346 |
|
| 347 |
(defmacro define-symbol-macro (&whole whole symbol expansion) |
| 348 |
(cond ((not (symbolp symbol)) |
| 349 |
(error "DEFINE-SYMBOL-MACRO: ~A is not a symbol" |
| 350 |
symbol)) |
| 351 |
((specialp symbol) |
| 352 |
(error "DEFINE-SYMBOL-MACRO: cannot redefine a special variable, ~A" |
| 353 |
symbol)) |
| 354 |
(t |
| 355 |
`(eval-when (:compile-toplevel :load-toplevel :execute) |
| 356 |
(put-sysprop ',symbol 'si::symbol-macro |
| 357 |
(lambda (form env) |
| 358 |
(declare (ignore form env)) |
| 359 |
',expansion)) |
| 360 |
,(ext:register-with-pde whole) |
| 361 |
',symbol)))) |
| 362 |
|
| 363 |
(defmacro nth-value (n expr) |
| 364 |
`(nth ,n (multiple-value-list ,expr))) |
| 365 |
|
| 366 |
(defun maybe-unquote (form) |
| 367 |
(if (and (consp form) (eq (car form) 'quote)) |
| 368 |
(second form) |
| 369 |
form)) |
| 370 |
|
| 371 |
(defun maybe-quote (form) |
| 372 |
;; Quotes a form only if strictly required. This happens only when FORM is |
| 373 |
;; either a symbol and not a keyword |
| 374 |
(if (if (atom form) |
| 375 |
(typep form '(and symbol (not keyword) (not boolean))) |
| 376 |
(not (eq (first form) 'quote))) |
| 377 |
(list 'quote form) |
| 378 |
form)) |
| 379 |
|
| 380 |
(defmacro ext:truly-the (&rest args) |
| 381 |
`(the ,@args)) |
| 382 |
|
| 383 |
(defmacro ext:checked-value (&rest args) |
| 384 |
`(the ,@args)) |