/[slime]/slime/swank-allegro.lisp
ViewVC logotype

Contents of /slime/swank-allegro.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.131 - (hide annotations)
Mon Jan 25 10:50:10 2010 UTC (4 years, 2 months ago) by trittweiler
Branch: MAIN
Changes since 1.130: +12 -2 lines
	* swank-allegro.lisp (redefinition-p, redefinition): New.
	(handle-compiler-warning): Add :severity for redefinitions,
	style-warnings, errors.
1 heller 1.79 ;;;; -*- indent-tabs-mode: nil; outline-regexp: ";;;;;* "; -*-
2 heller 1.1 ;;;
3     ;;; swank-allegro.lisp --- Allegro CL specific code for SLIME.
4     ;;;
5 heller 1.60 ;;; Created 2003
6 heller 1.1 ;;;
7     ;;; This code has been placed in the Public Domain. All warranties
8 heller 1.74 ;;; are disclaimed.
9 heller 1.1 ;;;
10    
11 heller 1.21 (in-package :swank-backend)
12    
13 heller 1.1 (eval-when (:compile-toplevel :load-toplevel :execute)
14     (require :sock)
15 heller 1.79 (require :process))
16 heller 1.1
17 heller 1.79 (import-from :excl *gray-stream-symbols* :swank-backend)
18 heller 1.1
19 mbaringer 1.53 ;;; swank-mop
20    
21 heller 1.60 (import-swank-mop-symbols :clos '(:slot-definition-documentation))
22 mbaringer 1.53
23 mbaringer 1.54 (defun swank-mop:slot-definition-documentation (slot)
24 heller 1.62 (documentation slot t))
25 mbaringer 1.53
26 heller 1.79
27 heller 1.8 ;;;; TCP Server
28 heller 1.1
29 heller 1.21 (defimplementation preferred-communication-style ()
30     :spawn)
31 heller 1.12
32 heller 1.14 (defimplementation create-socket (host port)
33     (socket:make-socket :connect :passive :local-port port
34     :local-host host :reuse-address t))
35 heller 1.5
36 heller 1.10 (defimplementation local-port (socket)
37 heller 1.6 (socket:local-port socket))
38    
39 heller 1.10 (defimplementation close-socket (socket)
40 heller 1.6 (close socket))
41    
42 dcrosher 1.85 (defimplementation accept-connection (socket &key external-format buffering
43     timeout)
44     (declare (ignore buffering timeout))
45 heller 1.94 (let ((s (socket:accept-connection socket :wait t)))
46     (when external-format
47     (setf (stream-external-format s) external-format))
48 heller 1.63 s))
49 heller 1.6
50 heller 1.94 (defvar *external-format-to-coding-system*
51     '((:iso-8859-1
52     "latin-1" "latin-1-unix" "iso-latin-1-unix"
53     "iso-8859-1" "iso-8859-1-unix")
54     (:utf-8 "utf-8" "utf-8-unix")
55     (:euc-jp "euc-jp" "euc-jp-unix")
56     (:us-ascii "us-ascii" "us-ascii-unix")
57     (:emacs-mule "emacs-mule" "emacs-mule-unix")))
58    
59     (defimplementation find-external-format (coding-system)
60     (let ((e (rassoc-if (lambda (x) (member coding-system x :test #'equal))
61     *external-format-to-coding-system*)))
62     (and e (excl:crlf-base-ef
63     (excl:find-external-format (car e)
64     :try-variant t)))))
65 heller 1.64
66 heller 1.34 (defimplementation format-sldb-condition (c)
67     (princ-to-string c))
68    
69 heller 1.39 (defimplementation call-with-syntax-hooks (fn)
70     (funcall fn))
71    
72 heller 1.9 ;;;; Unix signals
73    
74 heller 1.10 (defimplementation getpid ()
75 heller 1.8 (excl::getpid))
76 heller 1.6
77 heller 1.15 (defimplementation lisp-implementation-type-name ()
78     "allegro")
79    
80 pseibel 1.28 (defimplementation set-default-directory (directory)
81 mkoeppe 1.86 (let* ((dir (namestring (truename (merge-pathnames directory)))))
82     (setf *default-pathname-defaults* (pathname (excl:chdir dir)))
83 heller 1.66 dir))
84 pseibel 1.28
85 heller 1.35 (defimplementation default-directory ()
86 heller 1.66 (namestring (excl:current-directory)))
87 heller 1.35
88 heller 1.8 ;;;; Misc
89 heller 1.1
90 heller 1.21 (defimplementation arglist (symbol)
91 lgorrie 1.30 (handler-case (excl:arglist symbol)
92     (simple-error () :not-available)))
93 heller 1.21
94     (defimplementation macroexpand-all (form)
95     (excl::walk form))
96 heller 1.1
97 heller 1.10 (defimplementation describe-symbol-for-emacs (symbol)
98 heller 1.1 (let ((result '()))
99     (flet ((doc (kind &optional (sym symbol))
100     (or (documentation sym kind) :not-documented))
101     (maybe-push (property value)
102     (when value
103     (setf result (list* property value result)))))
104     (maybe-push
105     :variable (when (boundp symbol)
106     (doc 'variable)))
107     (maybe-push
108     :function (if (fboundp symbol)
109     (doc 'function)))
110     (maybe-push
111     :class (if (find-class symbol nil)
112     (doc 'class)))
113     result)))
114    
115 heller 1.20 (defimplementation describe-definition (symbol namespace)
116     (ecase namespace
117     (:variable
118     (describe symbol))
119     ((:function :generic-function)
120     (describe (symbol-function symbol)))
121     (:class
122     (describe (find-class symbol)))))
123 heller 1.10
124 heller 1.8 ;;;; Debugger
125    
126 heller 1.1 (defvar *sldb-topframe*)
127 heller 1.4
128 heller 1.10 (defimplementation call-with-debugging-environment (debugger-loop-fn)
129 heller 1.71 (let ((*sldb-topframe* (find-topframe))
130 heller 1.20 (excl::*break-hook* nil))
131 heller 1.4 (funcall debugger-loop-fn)))
132 heller 1.1
133 mkoeppe 1.92 (defimplementation sldb-break-at-start (fname)
134     ;; :print-before is kind of mis-used but we just want to stuff our break form
135     ;; somewhere. This does not work for setf, :before and :after methods, which
136     ;; need special syntax in the trace call, see ACL's doc/debugging.htm chapter 10.
137     (eval `(trace (,fname
138     :print-before
139     ((break "Function start breakpoint of ~A" ',fname)))))
140     `(:ok ,(format nil "Set breakpoint at start of ~S" fname)))
141    
142 heller 1.71 (defun find-topframe ()
143 trittweiler 1.125 (let ((magic-symbol (intern (symbol-name :swank-debugger-hook)
144     (find-package :swank)))
145     (top-frame (excl::int-newest-frame)))
146     (loop for frame = top-frame then (next-frame frame)
147     for name = (debugger:frame-name frame)
148     for i from 0
149     when (eq name magic-symbol)
150     return (next-frame frame)
151     until (= i 10) finally (return top-frame))))
152 heller 1.71
153 heller 1.42 (defun next-frame (frame)
154     (let ((next (excl::int-next-older-frame frame)))
155     (cond ((not next) nil)
156     ((debugger:frame-visible-p next) next)
157     (t (next-frame next)))))
158    
159 heller 1.1 (defun nth-frame (index)
160 heller 1.42 (do ((frame *sldb-topframe* (next-frame frame))
161 heller 1.1 (i index (1- i)))
162     ((zerop i) frame)))
163    
164 heller 1.20 (defimplementation compute-backtrace (start end)
165 heller 1.1 (let ((end (or end most-positive-fixnum)))
166 heller 1.42 (loop for f = (nth-frame start) then (next-frame f)
167 heller 1.1 for i from start below end
168 heller 1.117 while f collect f)))
169 heller 1.1
170 heller 1.117 (defimplementation print-frame (frame stream)
171     (debugger:output-frame stream frame :moderate))
172 heller 1.4
173 heller 1.10 (defimplementation frame-locals (index)
174 heller 1.1 (let ((frame (nth-frame index)))
175     (loop for i from 0 below (debugger:frame-number-vars frame)
176 mbaringer 1.19 collect (list :name (debugger:frame-var-name frame i)
177 heller 1.1 :id 0
178 mbaringer 1.19 :value (debugger:frame-var-value frame i)))))
179 heller 1.1
180 heller 1.39 (defimplementation frame-var-value (frame var)
181     (let ((frame (nth-frame frame)))
182     (debugger:frame-var-value frame var)))
183 heller 1.1
184 heller 1.21 (defimplementation disassemble-frame (index)
185     (disassemble (debugger:frame-function (nth-frame index))))
186    
187 heller 1.128 (defimplementation frame-source-location (index)
188 lgorrie 1.37 (let* ((frame (nth-frame index))
189     (expr (debugger:frame-expression frame))
190     (fspec (first expr)))
191     (second (first (fspec-definition-locations fspec)))))
192 heller 1.4
193 heller 1.10 (defimplementation eval-in-frame (form frame-number)
194 heller 1.73 (let ((frame (nth-frame frame-number)))
195     ;; let-bind lexical variables
196     (let ((vars (loop for i below (debugger:frame-number-vars frame)
197     for name = (debugger:frame-var-name frame i)
198     if (symbolp name)
199     collect `(,name ',(debugger:frame-var-value frame i)))))
200     (debugger:eval-form-in-context
201     `(let* ,vars ,form)
202     (debugger:environment-of-frame frame)))))
203 heller 1.10
204 heller 1.11 (defimplementation return-from-frame (frame-number form)
205     (let ((frame (nth-frame frame-number)))
206     (multiple-value-call #'debugger:frame-return
207     frame (debugger:eval-form-in-context
208 heller 1.20 form
209     (debugger:environment-of-frame frame)))))
210 heller 1.67
211 heller 1.117 (defimplementation frame-restartable-p (frame)
212 heller 1.119 (handler-case (debugger:frame-retryable-p frame)
213     (serious-condition (c)
214     (funcall (read-from-string "swank::background-message")
215     "~a ~a" frame (princ-to-string c))
216     nil)))
217 heller 1.117
218 heller 1.11 (defimplementation restart-frame (frame-number)
219     (let ((frame (nth-frame frame-number)))
220 heller 1.69 (cond ((debugger:frame-retryable-p frame)
221     (apply #'debugger:frame-retry frame (debugger:frame-function frame)
222     (cdr (debugger:frame-expression frame))))
223     (t "Frame is not retryable"))))
224 heller 1.67
225 heller 1.8 ;;;; Compiler hooks
226    
227 heller 1.1 (defvar *buffer-name* nil)
228     (defvar *buffer-start-position*)
229     (defvar *buffer-string*)
230 lgorrie 1.33 (defvar *compile-filename* nil)
231 trittweiler 1.126 (defvar *temp-file-header-end-position* nil)
232 heller 1.1
233 heller 1.66 (defun compiler-note-p (object)
234     (member (type-of object) '(excl::compiler-note compiler::compiler-note)))
235    
236 trittweiler 1.131 (defun redefinition-p (condition)
237     (and (typep condition 'style-warning)
238     (every #'char-equal "redefin" (princ-to-string condition))))
239    
240 heller 1.66 (defun compiler-undefined-functions-called-warning-p (object)
241     (typep object 'excl:compiler-undefined-functions-called-warning))
242 heller 1.64
243     (deftype compiler-note ()
244     `(satisfies compiler-note-p))
245    
246 trittweiler 1.131 (deftype redefinition ()
247     `(satisfies redefinition-p))
248    
249 heller 1.66 (defun signal-compiler-condition (&rest args)
250     (signal (apply #'make-condition 'compiler-condition args)))
251    
252 heller 1.1 (defun handle-compiler-warning (condition)
253 heller 1.66 (declare (optimize (debug 3) (speed 0) (space 0)))
254 trittweiler 1.126 (cond ((and (not *buffer-name*)
255 heller 1.66 (compiler-undefined-functions-called-warning-p condition))
256     (handle-undefined-functions-warning condition))
257     (t
258     (signal-compiler-condition
259     :original-condition condition
260     :severity (etypecase condition
261 trittweiler 1.131 (redefinition :redefinition)
262     (style-warning :style-warning)
263     (warning :warning)
264 trittweiler 1.126 (compiler-note :note)
265 trittweiler 1.131 (reader-error :read-error)
266     (error :error))
267 heller 1.66 :message (format nil "~A" condition)
268 trittweiler 1.126 :location (if (typep condition 'reader-error)
269     (location-for-reader-error condition)
270     (location-for-warning condition))))))
271 heller 1.66
272     (defun location-for-warning (condition)
273 heller 1.1 (let ((loc (getf (slot-value condition 'excl::plist) :loc)))
274 heller 1.66 (cond (*buffer-name*
275     (make-location
276     (list :buffer *buffer-name*)
277 heller 1.114 (list :offset *buffer-start-position* 0)))
278 heller 1.66 (loc
279     (destructuring-bind (file . pos) loc
280     (make-location
281     (list :file (namestring (truename file)))
282     (list :position (1+ pos)))))
283     (t
284 sboukarev 1.130 (make-error-location "No error location available.")))))
285 heller 1.66
286 trittweiler 1.126 (defun location-for-reader-error (condition)
287     (let ((pos (car (last (slot-value condition 'excl::format-arguments))))
288     (file (pathname (stream-error-stream condition))))
289     (if (integerp pos)
290     (if *buffer-name*
291     (make-location `(:buffer ,*buffer-name*)
292     `(:offset ,*buffer-start-position*
293     ,(- pos *temp-file-header-end-position* 1)))
294     (make-location `(:file ,(namestring (truename file)))
295     `(:position ,pos)))
296 sboukarev 1.130 (make-error-location "No error location available."))))
297 trittweiler 1.126
298 heller 1.66 (defun handle-undefined-functions-warning (condition)
299     (let ((fargs (slot-value condition 'excl::format-arguments)))
300 heller 1.67 (loop for (fname . pos-file) in (car fargs) do
301     (loop for (pos file) in pos-file do
302     (signal-compiler-condition
303     :original-condition condition
304     :severity :warning
305     :message (format nil "Undefined function referenced: ~S"
306     fname)
307     :location (make-location (list :file file)
308     (list :position (1+ pos))))))))
309 lgorrie 1.33 (defimplementation call-with-compilation-hooks (function)
310 trittweiler 1.126 (handler-bind ((warning #'handle-compiler-warning)
311     (compiler-note #'handle-compiler-warning)
312     (reader-error #'handle-compiler-warning))
313 lgorrie 1.33 (funcall function)))
314 heller 1.1
315 heller 1.124 (defimplementation swank-compile-file (input-file output-file
316     load-p external-format)
317 trittweiler 1.126 (handler-case
318     (with-compilation-hooks ()
319     (let ((*buffer-name* nil)
320     (*compile-filename* input-file))
321     (compile-file *compile-filename*
322     :output-file output-file
323     :load-after-compile load-p
324     :external-format external-format)))
325     (reader-error () (values nil nil t))))
326 heller 1.1
327 heller 1.46 (defun call-with-temp-file (fn)
328     (let ((tmpname (system:make-temp-file-name)))
329     (unwind-protect
330     (with-open-file (file tmpname :direction :output :if-exists :error)
331     (funcall fn file tmpname))
332     (delete-file tmpname))))
333    
334 trittweiler 1.126 (defun compile-from-temp-file (header string)
335 heller 1.46 (call-with-temp-file
336     (lambda (stream filename)
337 trittweiler 1.126 (write-string header stream)
338     (let ((*temp-file-header-end-position* (file-position stream)))
339 heller 1.46 (write-string string stream)
340     (finish-output stream)
341 heller 1.116 (multiple-value-bind (binary-filename warnings? failure?)
342 trittweiler 1.126 (excl:without-redefinition-warnings
343 heller 1.116 ;; Suppress Allegro's redefinition warnings; they are
344     ;; pointless when we are compiling via a temporary
345     ;; file.
346     (compile-file filename :load-after-compile t))
347     (declare (ignore warnings?))
348 heller 1.46 (when binary-filename
349 heller 1.116 (delete-file binary-filename))
350 trittweiler 1.126 (not failure?))))))
351    
352     (defimplementation swank-compile-string (string &key buffer position filename
353     policy)
354     (declare (ignore policy))
355     (handler-case
356     (with-compilation-hooks ()
357     (let ((*buffer-name* buffer)
358     (*buffer-start-position* position)
359     (*buffer-string* string)
360     (*default-pathname-defaults*
361     (if filename
362     (merge-pathnames (pathname filename))
363     *default-pathname-defaults*)))
364     ;; We store the source buffer in excl::*source-pathname* as a
365     ;; string of the form <buffername>;<start-offset>. Quite ugly
366     ;; encoding, but the fasl file is corrupted if we use some
367     ;; other datatype.
368     (compile-from-temp-file
369     (format nil "~S~%~S~%"
370     `(in-package ,(package-name *package*))
371     `(eval-when (:compile-toplevel :load-toplevel)
372     (setq excl::*source-pathname*
373     ',(format nil "~A;~D" buffer position))))
374     string)))
375     (reader-error () (values nil nil t))))
376 heller 1.46
377 heller 1.8 ;;;; Definition Finding
378    
379 heller 1.32 (defun fspec-primary-name (fspec)
380     (etypecase fspec
381 heller 1.62 (symbol fspec)
382     (list (fspec-primary-name (second fspec)))))
383 heller 1.32
384 heller 1.62 ;; If Emacs uses DOS-style eol conventions, \n\r are considered as a
385     ;; single character, but file-position counts them as two. Here we do
386     ;; our own conversion.
387     (defun count-cr (file pos)
388     (let* ((bufsize 256)
389 heller 1.63 (type '(unsigned-byte 8))
390     (buf (make-array bufsize :element-type type))
391 heller 1.62 (cr-count 0))
392 heller 1.63 (with-open-file (stream file :direction :input :element-type type)
393     (loop for bytes-read = (read-sequence buf stream) do
394     (incf cr-count (count (char-code #\return) buf
395     :end (min pos bytes-read)))
396     (decf pos bytes-read)
397     (when (<= pos 0)
398     (return cr-count))))))
399 heller 1.62
400 mkoeppe 1.81 (defun find-definition-in-file (fspec type file top-level)
401     (let* ((part
402     (or (scm::find-definition-in-definition-group
403     fspec type (scm:section-file :file file)
404     :top-level top-level)
405     (scm::find-definition-in-definition-group
406     (fspec-primary-name fspec)
407     type (scm:section-file :file file)
408     :top-level top-level)))
409     (start (and part
410     (scm::source-part-start part)))
411 heller 1.60 (pos (if start
412 heller 1.114 (list :position (1+ start))
413 heller 1.62 (list :function-name (string (fspec-primary-name fspec))))))
414     (make-location (list :file (namestring (truename file)))
415     pos)))
416 heller 1.60
417     (defun find-definition-in-buffer (filename)
418     (let ((pos (position #\; filename :from-end t)))
419     (make-location
420     (list :buffer (subseq filename 0 pos))
421 heller 1.114 (list :offset (parse-integer (subseq filename (1+ pos))) 0))))
422 heller 1.60
423 mkoeppe 1.81 (defun find-fspec-location (fspec type file top-level)
424 sboukarev 1.130 (handler-case
425     (etypecase file
426     (pathname
427     (find-definition-in-file fspec type file top-level))
428     ((member :top-level)
429     (make-error-location "Defined at toplevel: ~A" (fspec->string fspec)))
430     (string
431     (find-definition-in-buffer file)))
432     (error (e)
433     (make-error-location "Error: ~A" e))))
434 lgorrie 1.49
435 heller 1.50 (defun fspec->string (fspec)
436 lgorrie 1.49 (etypecase fspec
437 heller 1.50 (symbol (let ((*package* (find-package :keyword)))
438     (prin1-to-string fspec)))
439     (list (format nil "(~A ~A)"
440     (prin1-to-string (first fspec))
441     (let ((*package* (find-package :keyword)))
442     (prin1-to-string (second fspec)))))))
443 heller 1.21
444     (defun fspec-definition-locations (fspec)
445 mkoeppe 1.87 (cond
446 sboukarev 1.130 ((and (listp fspec)
447     (eql (car fspec) :top-level-form))
448     (destructuring-bind (top-level-form file &optional position) fspec
449     (declare (ignore top-level-form))
450     (list fspec
451 heller 1.114 (make-location (list :buffer file) ; FIXME: should use :file
452     (list :position position)
453 sboukarev 1.130 (list :align t)))))
454     ((and (listp fspec) (eq (car fspec) :internal))
455     (destructuring-bind (_internal next _n) fspec
456     (declare (ignore _internal _n))
457     (fspec-definition-locations next)))
458     (t
459     (let ((defs (excl::find-source-file fspec)))
460     (when (and (null defs)
461     (listp fspec)
462     (string= (car fspec) '#:method))
463     ;; If methods are defined in a defgeneric form, the source location is
464     ;; recorded for the gf but not for the methods. Therefore fall back to
465     ;; the gf as the likely place of definition.
466     (setq defs (excl::find-source-file (second fspec))))
467     (if (null defs)
468     (list
469     (list fspec
470     (make-error-location "Unknown source location for ~A"
471     (fspec->string fspec))))
472     (loop for (fspec type file top-level) in defs
473     collect (list (list type fspec)
474     (find-fspec-location fspec type file top-level))))))))
475 heller 1.21
476     (defimplementation find-definitions (symbol)
477     (fspec-definition-locations symbol))
478 heller 1.1
479 heller 1.8 ;;;; XREF
480    
481 heller 1.21 (defmacro defxref (name relation name1 name2)
482     `(defimplementation ,name (x)
483     (xref-result (xref:get-relation ,relation ,name1 ,name2))))
484    
485     (defxref who-calls :calls :wild x)
486 heller 1.70 (defxref calls-who :calls x :wild)
487 heller 1.21 (defxref who-references :uses :wild x)
488     (defxref who-binds :binds :wild x)
489     (defxref who-macroexpands :macro-calls :wild x)
490     (defxref who-sets :sets :wild x)
491    
492     (defun xref-result (fspecs)
493     (loop for fspec in fspecs
494     append (fspec-definition-locations fspec)))
495 heller 1.46
496     ;; list-callers implemented by groveling through all fbound symbols.
497     ;; Only symbols are considered. Functions in the constant pool are
498 heller 1.71 ;; searched recursively. Closure environments are ignored at the
499 heller 1.46 ;; moment (constants in methods are therefore not found).
500    
501     (defun map-function-constants (function fn depth)
502     "Call FN with the elements of FUNCTION's constant pool."
503     (do ((i 0 (1+ i))
504     (max (excl::function-constant-count function)))
505     ((= i max))
506     (let ((c (excl::function-constant function i)))
507     (cond ((and (functionp c)
508     (not (eq c function))
509     (plusp depth))
510     (map-function-constants c fn (1- depth)))
511     (t
512     (funcall fn c))))))
513    
514 heller 1.64 (defun in-constants-p (fun symbol)
515     (map-function-constants fun
516     (lambda (c)
517     (when (eq c symbol)
518     (return-from in-constants-p t)))
519     3))
520 heller 1.70
521 heller 1.46 (defun function-callers (name)
522     (let ((callers '()))
523     (do-all-symbols (sym)
524     (when (fboundp sym)
525     (let ((fn (fdefinition sym)))
526     (when (in-constants-p fn name)
527     (push sym callers)))))
528     callers))
529    
530     (defimplementation list-callers (name)
531     (xref-result (function-callers name)))
532 heller 1.4
533 heller 1.70 (defimplementation list-callees (name)
534     (let ((result '()))
535     (map-function-constants (fdefinition name)
536     (lambda (c)
537     (when (fboundp c)
538     (push c result)))
539     2)
540     (xref-result result)))
541    
542 mkoeppe 1.83 ;;;; Profiling
543    
544 heller 1.89 ;; Per-function profiling based on description in
545     ;; http://www.franz.com/support/documentation/8.0/doc/runtime-analyzer.htm#data-collection-control-2
546    
547     (defvar *profiled-functions* ())
548     (defvar *profile-depth* 0)
549    
550     (defmacro with-redirected-y-or-n-p (&body body)
551     ;; If the profiler is restarted when the data from the previous
552     ;; session is not reported yet, the user is warned via Y-OR-N-P.
553     ;; As the CL:Y-OR-N-P question is (for some reason) not directly
554     ;; sent to the Slime user, the function CL:Y-OR-N-P is temporarily
555     ;; overruled.
556     `(let* ((pkg (find-package "common-lisp"))
557     (saved-pdl (excl::package-definition-lock pkg))
558     (saved-ynp (symbol-function 'cl:y-or-n-p)))
559    
560     (setf (excl::package-definition-lock pkg) nil
561     (symbol-function 'cl:y-or-n-p) (symbol-function
562     (find-symbol "y-or-n-p-in-emacs"
563     "swank")))
564     (unwind-protect
565     (progn ,@body)
566    
567     (setf (symbol-function 'cl:y-or-n-p) saved-ynp
568     (excl::package-definition-lock pkg) saved-pdl))))
569    
570     (defun start-acl-profiler ()
571     (with-redirected-y-or-n-p
572     (prof:start-profiler :type :time :count t
573     :start-sampling-p nil :verbose nil)))
574     (defun acl-profiler-active-p ()
575     (not (eq (prof:profiler-status :verbose nil) :inactive)))
576    
577     (defun stop-acl-profiler ()
578     (prof:stop-profiler :verbose nil))
579    
580     (excl:def-fwrapper profile-fwrapper (&rest args)
581     ;; Ensures sampling is done during the execution of the function,
582     ;; taking into account recursion.
583     (declare (ignore args))
584     (cond ((zerop *profile-depth*)
585     (let ((*profile-depth* (1+ *profile-depth*)))
586     (prof:start-sampling)
587     (unwind-protect (excl:call-next-fwrapper)
588     (prof:stop-sampling))))
589     (t
590     (excl:call-next-fwrapper))))
591    
592     (defimplementation profile (fname)
593     (unless (acl-profiler-active-p)
594     (start-acl-profiler))
595     (excl:fwrap fname 'profile-fwrapper 'profile-fwrapper)
596     (push fname *profiled-functions*))
597    
598     (defimplementation profiled-functions ()
599     *profiled-functions*)
600    
601     (defimplementation unprofile (fname)
602     (excl:funwrap fname 'profile-fwrapper)
603     (setq *profiled-functions* (remove fname *profiled-functions*)))
604    
605 mkoeppe 1.83 (defimplementation profile-report ()
606 heller 1.89 (prof:show-flat-profile :verbose nil)
607     (when *profiled-functions*
608     (start-acl-profiler)))
609    
610     (defimplementation profile-reset ()
611     (when (acl-profiler-active-p)
612     (stop-acl-profiler)
613     (start-acl-profiler))
614     "Reset profiling counters.")
615 mkoeppe 1.83
616 heller 1.18 ;;;; Inspecting
617    
618 heller 1.111 (excl:without-redefinition-warnings
619 heller 1.100 (defmethod emacs-inspect ((o t))
620 heller 1.111 (allegro-inspect o)))
621 heller 1.62
622 heller 1.100 (defmethod emacs-inspect ((o function))
623 heller 1.101 (allegro-inspect o))
624 heller 1.62
625 heller 1.100 (defmethod emacs-inspect ((o standard-object))
626 heller 1.101 (allegro-inspect o))
627 heller 1.82
628 heller 1.62 (defun allegro-inspect (o)
629     (loop for (d dd) on (inspect::inspect-ctl o)
630 heller 1.73 append (frob-allegro-field-def o d)
631     until (eq d dd)))
632 heller 1.62
633 heller 1.73 (defun frob-allegro-field-def (object def)
634 heller 1.62 (with-struct (inspect::field-def- name type access) def
635 heller 1.73 (ecase type
636 heller 1.79 ((:unsigned-word :unsigned-byte :unsigned-natural
637     :unsigned-long :unsigned-half-long
638     :unsigned-3byte)
639 heller 1.73 (label-value-line name (inspect::component-ref-v object access type)))
640 heller 1.115 ((:lisp :value :func)
641 heller 1.73 (label-value-line name (inspect::component-ref object access)))
642     (:indirect
643     (destructuring-bind (prefix count ref set) access
644     (declare (ignore set prefix))
645     (loop for i below (funcall count object)
646     append (label-value-line (format nil "~A-~D" name i)
647     (funcall ref object i))))))))
648 mbaringer 1.55
649 heller 1.18 ;;;; Multithreading
650 heller 1.8
651 mbaringer 1.91 (defimplementation initialize-multiprocessing (continuation)
652     (mp:start-scheduler)
653     (funcall continuation))
654 heller 1.8
655 heller 1.10 (defimplementation spawn (fn &key name)
656 lgorrie 1.47 (mp:process-run-function name fn))
657 heller 1.8
658 heller 1.40 (defvar *id-lock* (mp:make-process-lock :name "id lock"))
659     (defvar *thread-id-counter* 0)
660    
661     (defimplementation thread-id (thread)
662     (mp:with-process-lock (*id-lock*)
663     (or (getf (mp:process-property-list thread) 'id)
664     (setf (getf (mp:process-property-list thread) 'id)
665     (incf *thread-id-counter*)))))
666    
667     (defimplementation find-thread (id)
668     (find id mp:*all-processes*
669     :key (lambda (p) (getf (mp:process-property-list p) 'id))))
670    
671 heller 1.13 (defimplementation thread-name (thread)
672     (mp:process-name thread))
673 heller 1.8
674 heller 1.13 (defimplementation thread-status (thread)
675     (format nil "~A ~D" (mp:process-whostate thread)
676     (mp:process-priority thread)))
677 heller 1.8
678 heller 1.10 (defimplementation make-lock (&key name)
679 heller 1.8 (mp:make-process-lock :name name))
680    
681 heller 1.10 (defimplementation call-with-lock-held (lock function)
682 heller 1.8 (mp:with-process-lock (lock) (funcall function)))
683 heller 1.12
684     (defimplementation current-thread ()
685     mp:*current-process*)
686    
687     (defimplementation all-threads ()
688 heller 1.13 (copy-list mp:*all-processes*))
689 heller 1.12
690     (defimplementation interrupt-thread (thread fn)
691     (mp:process-interrupt thread fn))
692    
693 heller 1.16 (defimplementation kill-thread (thread)
694     (mp:process-kill thread))
695    
696 heller 1.12 (defvar *mailbox-lock* (mp:make-process-lock :name "mailbox lock"))
697    
698     (defstruct (mailbox (:conc-name mailbox.))
699 heller 1.107 (lock (mp:make-process-lock :name "process mailbox"))
700     (queue '() :type list)
701 heller 1.108 (gate (mp:make-gate nil)))
702 heller 1.12
703     (defun mailbox (thread)
704     "Return THREAD's mailbox."
705     (mp:with-process-lock (*mailbox-lock*)
706     (or (getf (mp:process-property-list thread) 'mailbox)
707     (setf (getf (mp:process-property-list thread) 'mailbox)
708     (make-mailbox)))))
709    
710     (defimplementation send (thread message)
711 heller 1.107 (let* ((mbox (mailbox thread)))
712     (mp:with-process-lock ((mailbox.lock mbox))
713     (setf (mailbox.queue mbox)
714     (nconc (mailbox.queue mbox) (list message)))
715     (mp:open-gate (mailbox.gate mbox)))))
716 heller 1.12
717 heller 1.110 (defimplementation receive-if (test &optional timeout)
718 heller 1.104 (let ((mbox (mailbox mp:*current-process*)))
719 heller 1.110 (assert (or (not timeout) (eq timeout t)))
720 heller 1.107 (loop
721     (check-slime-interrupts)
722     (mp:with-process-lock ((mailbox.lock mbox))
723     (let* ((q (mailbox.queue mbox))
724     (tail (member-if test q)))
725     (when tail
726     (setf (mailbox.queue mbox) (nconc (ldiff q tail) (cdr tail)))
727     (return (car tail)))
728     (mp:close-gate (mailbox.gate mbox))))
729 heller 1.110 (when (eq timeout t) (return (values nil t)))
730     (mp:process-wait-with-timeout "receive-if" 0.5
731     #'mp:gate-open-p (mailbox.gate mbox)))))
732 heller 1.104
733 heller 1.123 (defimplementation set-default-initial-binding (var form)
734     (setq excl:*cl-default-special-bindings*
735     (acons var form excl:*cl-default-special-bindings*)))
736    
737 mbaringer 1.27 (defimplementation quit-lisp ()
738     (excl:exit 0 :quiet t))
739 mbaringer 1.68
740 heller 1.69
741 mbaringer 1.68 ;;Trace implementations
742     ;;In Allegro 7.0, we have:
743     ;; (trace <name>)
744     ;; (trace ((method <name> <qualifier>? (<specializer>+))))
745     ;; (trace ((labels <name> <label-name>)))
746     ;; (trace ((labels (method <name> (<specializer>+)) <label-name>)))
747     ;; <name> can be a normal name or a (setf name)
748    
749 heller 1.70 (defimplementation toggle-trace (spec)
750 lgorrie 1.72 (ecase (car spec)
751     ((setf)
752     (toggle-trace-aux spec))
753 heller 1.70 (:defgeneric (toggle-trace-generic-function-methods (second spec)))
754 heller 1.71 ((setf :defmethod :labels :flet)
755 heller 1.70 (toggle-trace-aux (process-fspec-for-allegro spec)))
756 lgorrie 1.72 (:call
757 heller 1.70 (destructuring-bind (caller callee) (cdr spec)
758     (toggle-trace-aux callee
759     :inside (list (process-fspec-for-allegro caller)))))))
760    
761     (defun tracedp (fspec)
762 heller 1.71 (member fspec (eval '(trace)) :test #'equal))
763 heller 1.70
764     (defun toggle-trace-aux (fspec &rest args)
765     (cond ((tracedp fspec)
766     (eval `(untrace ,fspec))
767     (format nil "~S is now untraced." fspec))
768     (t
769     (eval `(trace (,fspec ,@args)))
770     (format nil "~S is now traced." fspec))))
771    
772     (defun toggle-trace-generic-function-methods (name)
773 mbaringer 1.68 (let ((methods (mop:generic-function-methods (fdefinition name))))
774 heller 1.70 (cond ((tracedp name)
775 mbaringer 1.68 (eval `(untrace ,name))
776     (dolist (method methods (format nil "~S is now untraced." name))
777     (excl:funtrace (mop:method-function method))))
778     (t
779 lgorrie 1.72 (eval `(trace (,name)))
780 heller 1.70 (dolist (method methods (format nil "~S is now traced." name))
781 mbaringer 1.68 (excl:ftrace (mop:method-function method)))))))
782    
783     (defun process-fspec-for-allegro (fspec)
784     (cond ((consp fspec)
785     (ecase (first fspec)
786 heller 1.71 ((setf) fspec)
787 mbaringer 1.68 ((:defun :defgeneric) (second fspec))
788     ((:defmethod) `(method ,@(rest fspec)))
789 heller 1.69 ((:labels) `(labels ,(process-fspec-for-allegro (second fspec))
790     ,(third fspec)))
791     ((:flet) `(flet ,(process-fspec-for-allegro (second fspec))
792     ,(third fspec)))))
793 mbaringer 1.68 (t
794     fspec)))
795 heller 1.88
796    
797     ;;;; Weak hashtables
798    
799     (defimplementation make-weak-key-hash-table (&rest args)
800     (apply #'make-hash-table :weak-keys t args))
801    
802     (defimplementation make-weak-value-hash-table (&rest args)
803     (apply #'make-hash-table :values :weak args))
804 mkoeppe 1.93
805 alendvai 1.95 (defimplementation hash-table-weakness (hashtable)
806     (cond ((excl:hash-table-weak-keys hashtable) :key)
807     ((eq (excl:hash-table-values hashtable) :weak) :value)))
808    
809    
810 mkoeppe 1.93
811     ;;;; Character names
812    
813     (defimplementation character-completion-set (prefix matchp)
814     (loop for name being the hash-keys of excl::*name-to-char-table*
815     when (funcall matchp prefix name)
816     collect (string-capitalize name)))

  ViewVC Help
Powered by ViewVC 1.1.5