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

Contents of /slime/swank-allegro.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.124 - (hide annotations)
Sat Jan 10 12:25:16 2009 UTC (5 years, 3 months ago) by heller
Branch: MAIN
Changes since 1.123: +6 -3 lines
* swank-backend.lisp (swank-compile-file): Take output-file as
additional argument.  Update backends accordingly.

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

  ViewVC Help
Powered by ViewVC 1.1.5