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

Contents of /slime/swank-allegro.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.132 - (hide annotations)
Tue Mar 2 12:38:06 2010 UTC (4 years, 1 month ago) by sboukarev
Branch: MAIN
Changes since 1.131: +3 -1 lines
* slime.el (slime-compile-and-load-file): Accept C-u arguments for
compilation policy the same way as slime-compile-defun.

* swank.lisp (compile-file-for-emacs): Take an additional policy argument.
* swank-backend.lisp (swank-compile-file): Ditto.

* swank-sbcl.lisp (compiler-policy, (setf compiler-policy)):
rename from get/set-compiler-policy.
(with-compiler-policy): New macro.
(swank-compile-file): Use with-compiler-policy.
(swank-compile-string): Ditto.
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 sboukarev 1.132 load-p external-format
317     &key policy)
318     (declare (ignore policy))
319 trittweiler 1.126 (handler-case
320     (with-compilation-hooks ()
321     (let ((*buffer-name* nil)
322     (*compile-filename* input-file))
323     (compile-file *compile-filename*
324     :output-file output-file
325     :load-after-compile load-p
326     :external-format external-format)))
327     (reader-error () (values nil nil t))))
328 heller 1.1
329 heller 1.46 (defun call-with-temp-file (fn)
330     (let ((tmpname (system:make-temp-file-name)))
331     (unwind-protect
332     (with-open-file (file tmpname :direction :output :if-exists :error)
333     (funcall fn file tmpname))
334     (delete-file tmpname))))
335    
336 trittweiler 1.126 (defun compile-from-temp-file (header string)
337 heller 1.46 (call-with-temp-file
338     (lambda (stream filename)
339 trittweiler 1.126 (write-string header stream)
340     (let ((*temp-file-header-end-position* (file-position stream)))
341 heller 1.46 (write-string string stream)
342     (finish-output stream)
343 heller 1.116 (multiple-value-bind (binary-filename warnings? failure?)
344 trittweiler 1.126 (excl:without-redefinition-warnings
345 heller 1.116 ;; Suppress Allegro's redefinition warnings; they are
346     ;; pointless when we are compiling via a temporary
347     ;; file.
348     (compile-file filename :load-after-compile t))
349     (declare (ignore warnings?))
350 heller 1.46 (when binary-filename
351 heller 1.116 (delete-file binary-filename))
352 trittweiler 1.126 (not failure?))))))
353    
354     (defimplementation swank-compile-string (string &key buffer position filename
355     policy)
356     (declare (ignore policy))
357     (handler-case
358     (with-compilation-hooks ()
359     (let ((*buffer-name* buffer)
360     (*buffer-start-position* position)
361     (*buffer-string* string)
362     (*default-pathname-defaults*
363     (if filename
364     (merge-pathnames (pathname filename))
365     *default-pathname-defaults*)))
366     ;; We store the source buffer in excl::*source-pathname* as a
367     ;; string of the form <buffername>;<start-offset>. Quite ugly
368     ;; encoding, but the fasl file is corrupted if we use some
369     ;; other datatype.
370     (compile-from-temp-file
371     (format nil "~S~%~S~%"
372     `(in-package ,(package-name *package*))
373     `(eval-when (:compile-toplevel :load-toplevel)
374     (setq excl::*source-pathname*
375     ',(format nil "~A;~D" buffer position))))
376     string)))
377     (reader-error () (values nil nil t))))
378 heller 1.46
379 heller 1.8 ;;;; Definition Finding
380    
381 heller 1.32 (defun fspec-primary-name (fspec)
382     (etypecase fspec
383 heller 1.62 (symbol fspec)
384     (list (fspec-primary-name (second fspec)))))
385 heller 1.32
386 heller 1.62 ;; If Emacs uses DOS-style eol conventions, \n\r are considered as a
387     ;; single character, but file-position counts them as two. Here we do
388     ;; our own conversion.
389     (defun count-cr (file pos)
390     (let* ((bufsize 256)
391 heller 1.63 (type '(unsigned-byte 8))
392     (buf (make-array bufsize :element-type type))
393 heller 1.62 (cr-count 0))
394 heller 1.63 (with-open-file (stream file :direction :input :element-type type)
395     (loop for bytes-read = (read-sequence buf stream) do
396     (incf cr-count (count (char-code #\return) buf
397     :end (min pos bytes-read)))
398     (decf pos bytes-read)
399     (when (<= pos 0)
400     (return cr-count))))))
401 heller 1.62
402 mkoeppe 1.81 (defun find-definition-in-file (fspec type file top-level)
403     (let* ((part
404     (or (scm::find-definition-in-definition-group
405     fspec type (scm:section-file :file file)
406     :top-level top-level)
407     (scm::find-definition-in-definition-group
408     (fspec-primary-name fspec)
409     type (scm:section-file :file file)
410     :top-level top-level)))
411     (start (and part
412     (scm::source-part-start part)))
413 heller 1.60 (pos (if start
414 heller 1.114 (list :position (1+ start))
415 heller 1.62 (list :function-name (string (fspec-primary-name fspec))))))
416     (make-location (list :file (namestring (truename file)))
417     pos)))
418 heller 1.60
419     (defun find-definition-in-buffer (filename)
420     (let ((pos (position #\; filename :from-end t)))
421     (make-location
422     (list :buffer (subseq filename 0 pos))
423 heller 1.114 (list :offset (parse-integer (subseq filename (1+ pos))) 0))))
424 heller 1.60
425 mkoeppe 1.81 (defun find-fspec-location (fspec type file top-level)
426 sboukarev 1.130 (handler-case
427     (etypecase file
428     (pathname
429     (find-definition-in-file fspec type file top-level))
430     ((member :top-level)
431     (make-error-location "Defined at toplevel: ~A" (fspec->string fspec)))
432     (string
433     (find-definition-in-buffer file)))
434     (error (e)
435     (make-error-location "Error: ~A" e))))
436 lgorrie 1.49
437 heller 1.50 (defun fspec->string (fspec)
438 lgorrie 1.49 (etypecase fspec
439 heller 1.50 (symbol (let ((*package* (find-package :keyword)))
440     (prin1-to-string fspec)))
441     (list (format nil "(~A ~A)"
442     (prin1-to-string (first fspec))
443     (let ((*package* (find-package :keyword)))
444     (prin1-to-string (second fspec)))))))
445 heller 1.21
446     (defun fspec-definition-locations (fspec)
447 mkoeppe 1.87 (cond
448 sboukarev 1.130 ((and (listp fspec)
449     (eql (car fspec) :top-level-form))
450     (destructuring-bind (top-level-form file &optional position) fspec
451     (declare (ignore top-level-form))
452     (list fspec
453 heller 1.114 (make-location (list :buffer file) ; FIXME: should use :file
454     (list :position position)
455 sboukarev 1.130 (list :align t)))))
456     ((and (listp fspec) (eq (car fspec) :internal))
457     (destructuring-bind (_internal next _n) fspec
458     (declare (ignore _internal _n))
459     (fspec-definition-locations next)))
460     (t
461     (let ((defs (excl::find-source-file fspec)))
462     (when (and (null defs)
463     (listp fspec)
464     (string= (car fspec) '#:method))
465     ;; If methods are defined in a defgeneric form, the source location is
466     ;; recorded for the gf but not for the methods. Therefore fall back to
467     ;; the gf as the likely place of definition.
468     (setq defs (excl::find-source-file (second fspec))))
469     (if (null defs)
470     (list
471     (list fspec
472     (make-error-location "Unknown source location for ~A"
473     (fspec->string fspec))))
474     (loop for (fspec type file top-level) in defs
475     collect (list (list type fspec)
476     (find-fspec-location fspec type file top-level))))))))
477 heller 1.21
478     (defimplementation find-definitions (symbol)
479     (fspec-definition-locations symbol))
480 heller 1.1
481 heller 1.8 ;;;; XREF
482    
483 heller 1.21 (defmacro defxref (name relation name1 name2)
484     `(defimplementation ,name (x)
485     (xref-result (xref:get-relation ,relation ,name1 ,name2))))
486    
487     (defxref who-calls :calls :wild x)
488 heller 1.70 (defxref calls-who :calls x :wild)
489 heller 1.21 (defxref who-references :uses :wild x)
490     (defxref who-binds :binds :wild x)
491     (defxref who-macroexpands :macro-calls :wild x)
492     (defxref who-sets :sets :wild x)
493    
494     (defun xref-result (fspecs)
495     (loop for fspec in fspecs
496     append (fspec-definition-locations fspec)))
497 heller 1.46
498     ;; list-callers implemented by groveling through all fbound symbols.
499     ;; Only symbols are considered. Functions in the constant pool are
500 heller 1.71 ;; searched recursively. Closure environments are ignored at the
501 heller 1.46 ;; moment (constants in methods are therefore not found).
502    
503     (defun map-function-constants (function fn depth)
504     "Call FN with the elements of FUNCTION's constant pool."
505     (do ((i 0 (1+ i))
506     (max (excl::function-constant-count function)))
507     ((= i max))
508     (let ((c (excl::function-constant function i)))
509     (cond ((and (functionp c)
510     (not (eq c function))
511     (plusp depth))
512     (map-function-constants c fn (1- depth)))
513     (t
514     (funcall fn c))))))
515    
516 heller 1.64 (defun in-constants-p (fun symbol)
517     (map-function-constants fun
518     (lambda (c)
519     (when (eq c symbol)
520     (return-from in-constants-p t)))
521     3))
522 heller 1.70
523 heller 1.46 (defun function-callers (name)
524     (let ((callers '()))
525     (do-all-symbols (sym)
526     (when (fboundp sym)
527     (let ((fn (fdefinition sym)))
528     (when (in-constants-p fn name)
529     (push sym callers)))))
530     callers))
531    
532     (defimplementation list-callers (name)
533     (xref-result (function-callers name)))
534 heller 1.4
535 heller 1.70 (defimplementation list-callees (name)
536     (let ((result '()))
537     (map-function-constants (fdefinition name)
538     (lambda (c)
539     (when (fboundp c)
540     (push c result)))
541     2)
542     (xref-result result)))
543    
544 mkoeppe 1.83 ;;;; Profiling
545    
546 heller 1.89 ;; Per-function profiling based on description in
547     ;; http://www.franz.com/support/documentation/8.0/doc/runtime-analyzer.htm#data-collection-control-2
548    
549     (defvar *profiled-functions* ())
550     (defvar *profile-depth* 0)
551    
552     (defmacro with-redirected-y-or-n-p (&body body)
553     ;; If the profiler is restarted when the data from the previous
554     ;; session is not reported yet, the user is warned via Y-OR-N-P.
555     ;; As the CL:Y-OR-N-P question is (for some reason) not directly
556     ;; sent to the Slime user, the function CL:Y-OR-N-P is temporarily
557     ;; overruled.
558     `(let* ((pkg (find-package "common-lisp"))
559     (saved-pdl (excl::package-definition-lock pkg))
560     (saved-ynp (symbol-function 'cl:y-or-n-p)))
561    
562     (setf (excl::package-definition-lock pkg) nil
563     (symbol-function 'cl:y-or-n-p) (symbol-function
564     (find-symbol "y-or-n-p-in-emacs"
565     "swank")))
566     (unwind-protect
567     (progn ,@body)
568    
569     (setf (symbol-function 'cl:y-or-n-p) saved-ynp
570     (excl::package-definition-lock pkg) saved-pdl))))
571    
572     (defun start-acl-profiler ()
573     (with-redirected-y-or-n-p
574     (prof:start-profiler :type :time :count t
575     :start-sampling-p nil :verbose nil)))
576     (defun acl-profiler-active-p ()
577     (not (eq (prof:profiler-status :verbose nil) :inactive)))
578    
579     (defun stop-acl-profiler ()
580     (prof:stop-profiler :verbose nil))
581    
582     (excl:def-fwrapper profile-fwrapper (&rest args)
583     ;; Ensures sampling is done during the execution of the function,
584     ;; taking into account recursion.
585     (declare (ignore args))
586     (cond ((zerop *profile-depth*)
587     (let ((*profile-depth* (1+ *profile-depth*)))
588     (prof:start-sampling)
589     (unwind-protect (excl:call-next-fwrapper)
590     (prof:stop-sampling))))
591     (t
592     (excl:call-next-fwrapper))))
593    
594     (defimplementation profile (fname)
595     (unless (acl-profiler-active-p)
596     (start-acl-profiler))
597     (excl:fwrap fname 'profile-fwrapper 'profile-fwrapper)
598     (push fname *profiled-functions*))
599    
600     (defimplementation profiled-functions ()
601     *profiled-functions*)
602    
603     (defimplementation unprofile (fname)
604     (excl:funwrap fname 'profile-fwrapper)
605     (setq *profiled-functions* (remove fname *profiled-functions*)))
606    
607 mkoeppe 1.83 (defimplementation profile-report ()
608 heller 1.89 (prof:show-flat-profile :verbose nil)
609     (when *profiled-functions*
610     (start-acl-profiler)))
611    
612     (defimplementation profile-reset ()
613     (when (acl-profiler-active-p)
614     (stop-acl-profiler)
615     (start-acl-profiler))
616     "Reset profiling counters.")
617 mkoeppe 1.83
618 heller 1.18 ;;;; Inspecting
619    
620 heller 1.111 (excl:without-redefinition-warnings
621 heller 1.100 (defmethod emacs-inspect ((o t))
622 heller 1.111 (allegro-inspect o)))
623 heller 1.62
624 heller 1.100 (defmethod emacs-inspect ((o function))
625 heller 1.101 (allegro-inspect o))
626 heller 1.62
627 heller 1.100 (defmethod emacs-inspect ((o standard-object))
628 heller 1.101 (allegro-inspect o))
629 heller 1.82
630 heller 1.62 (defun allegro-inspect (o)
631     (loop for (d dd) on (inspect::inspect-ctl o)
632 heller 1.73 append (frob-allegro-field-def o d)
633     until (eq d dd)))
634 heller 1.62
635 heller 1.73 (defun frob-allegro-field-def (object def)
636 heller 1.62 (with-struct (inspect::field-def- name type access) def
637 heller 1.73 (ecase type
638 heller 1.79 ((:unsigned-word :unsigned-byte :unsigned-natural
639     :unsigned-long :unsigned-half-long
640     :unsigned-3byte)
641 heller 1.73 (label-value-line name (inspect::component-ref-v object access type)))
642 heller 1.115 ((:lisp :value :func)
643 heller 1.73 (label-value-line name (inspect::component-ref object access)))
644     (:indirect
645     (destructuring-bind (prefix count ref set) access
646     (declare (ignore set prefix))
647     (loop for i below (funcall count object)
648     append (label-value-line (format nil "~A-~D" name i)
649     (funcall ref object i))))))))
650 mbaringer 1.55
651 heller 1.18 ;;;; Multithreading
652 heller 1.8
653 mbaringer 1.91 (defimplementation initialize-multiprocessing (continuation)
654     (mp:start-scheduler)
655     (funcall continuation))
656 heller 1.8
657 heller 1.10 (defimplementation spawn (fn &key name)
658 lgorrie 1.47 (mp:process-run-function name fn))
659 heller 1.8
660 heller 1.40 (defvar *id-lock* (mp:make-process-lock :name "id lock"))
661     (defvar *thread-id-counter* 0)
662    
663     (defimplementation thread-id (thread)
664     (mp:with-process-lock (*id-lock*)
665     (or (getf (mp:process-property-list thread) 'id)
666     (setf (getf (mp:process-property-list thread) 'id)
667     (incf *thread-id-counter*)))))
668    
669     (defimplementation find-thread (id)
670     (find id mp:*all-processes*
671     :key (lambda (p) (getf (mp:process-property-list p) 'id))))
672    
673 heller 1.13 (defimplementation thread-name (thread)
674     (mp:process-name thread))
675 heller 1.8
676 heller 1.13 (defimplementation thread-status (thread)
677     (format nil "~A ~D" (mp:process-whostate thread)
678     (mp:process-priority thread)))
679 heller 1.8
680 heller 1.10 (defimplementation make-lock (&key name)
681 heller 1.8 (mp:make-process-lock :name name))
682    
683 heller 1.10 (defimplementation call-with-lock-held (lock function)
684 heller 1.8 (mp:with-process-lock (lock) (funcall function)))
685 heller 1.12
686     (defimplementation current-thread ()
687     mp:*current-process*)
688    
689     (defimplementation all-threads ()
690 heller 1.13 (copy-list mp:*all-processes*))
691 heller 1.12
692     (defimplementation interrupt-thread (thread fn)
693     (mp:process-interrupt thread fn))
694    
695 heller 1.16 (defimplementation kill-thread (thread)
696     (mp:process-kill thread))
697    
698 heller 1.12 (defvar *mailbox-lock* (mp:make-process-lock :name "mailbox lock"))
699    
700     (defstruct (mailbox (:conc-name mailbox.))
701 heller 1.107 (lock (mp:make-process-lock :name "process mailbox"))
702     (queue '() :type list)
703 heller 1.108 (gate (mp:make-gate nil)))
704 heller 1.12
705     (defun mailbox (thread)
706     "Return THREAD's mailbox."
707     (mp:with-process-lock (*mailbox-lock*)
708     (or (getf (mp:process-property-list thread) 'mailbox)
709     (setf (getf (mp:process-property-list thread) 'mailbox)
710     (make-mailbox)))))
711    
712     (defimplementation send (thread message)
713 heller 1.107 (let* ((mbox (mailbox thread)))
714     (mp:with-process-lock ((mailbox.lock mbox))
715     (setf (mailbox.queue mbox)
716     (nconc (mailbox.queue mbox) (list message)))
717     (mp:open-gate (mailbox.gate mbox)))))
718 heller 1.12
719 heller 1.110 (defimplementation receive-if (test &optional timeout)
720 heller 1.104 (let ((mbox (mailbox mp:*current-process*)))
721 heller 1.110 (assert (or (not timeout) (eq timeout t)))
722 heller 1.107 (loop
723     (check-slime-interrupts)
724     (mp:with-process-lock ((mailbox.lock mbox))
725     (let* ((q (mailbox.queue mbox))
726     (tail (member-if test q)))
727     (when tail
728     (setf (mailbox.queue mbox) (nconc (ldiff q tail) (cdr tail)))
729     (return (car tail)))
730     (mp:close-gate (mailbox.gate mbox))))
731 heller 1.110 (when (eq timeout t) (return (values nil t)))
732     (mp:process-wait-with-timeout "receive-if" 0.5
733     #'mp:gate-open-p (mailbox.gate mbox)))))
734 heller 1.104
735 heller 1.123 (defimplementation set-default-initial-binding (var form)
736     (setq excl:*cl-default-special-bindings*
737     (acons var form excl:*cl-default-special-bindings*)))
738    
739 mbaringer 1.27 (defimplementation quit-lisp ()
740     (excl:exit 0 :quiet t))
741 mbaringer 1.68
742 heller 1.69
743 mbaringer 1.68 ;;Trace implementations
744     ;;In Allegro 7.0, we have:
745     ;; (trace <name>)
746     ;; (trace ((method <name> <qualifier>? (<specializer>+))))
747     ;; (trace ((labels <name> <label-name>)))
748     ;; (trace ((labels (method <name> (<specializer>+)) <label-name>)))
749     ;; <name> can be a normal name or a (setf name)
750    
751 heller 1.70 (defimplementation toggle-trace (spec)
752 lgorrie 1.72 (ecase (car spec)
753     ((setf)
754     (toggle-trace-aux spec))
755 heller 1.70 (:defgeneric (toggle-trace-generic-function-methods (second spec)))
756 heller 1.71 ((setf :defmethod :labels :flet)
757 heller 1.70 (toggle-trace-aux (process-fspec-for-allegro spec)))
758 lgorrie 1.72 (:call
759 heller 1.70 (destructuring-bind (caller callee) (cdr spec)
760     (toggle-trace-aux callee
761     :inside (list (process-fspec-for-allegro caller)))))))
762    
763     (defun tracedp (fspec)
764 heller 1.71 (member fspec (eval '(trace)) :test #'equal))
765 heller 1.70
766     (defun toggle-trace-aux (fspec &rest args)
767     (cond ((tracedp fspec)
768     (eval `(untrace ,fspec))
769     (format nil "~S is now untraced." fspec))
770     (t
771     (eval `(trace (,fspec ,@args)))
772     (format nil "~S is now traced." fspec))))
773    
774     (defun toggle-trace-generic-function-methods (name)
775 mbaringer 1.68 (let ((methods (mop:generic-function-methods (fdefinition name))))
776 heller 1.70 (cond ((tracedp name)
777 mbaringer 1.68 (eval `(untrace ,name))
778     (dolist (method methods (format nil "~S is now untraced." name))
779     (excl:funtrace (mop:method-function method))))
780     (t
781 lgorrie 1.72 (eval `(trace (,name)))
782 heller 1.70 (dolist (method methods (format nil "~S is now traced." name))
783 mbaringer 1.68 (excl:ftrace (mop:method-function method)))))))
784    
785     (defun process-fspec-for-allegro (fspec)
786     (cond ((consp fspec)
787     (ecase (first fspec)
788 heller 1.71 ((setf) fspec)
789 mbaringer 1.68 ((:defun :defgeneric) (second fspec))
790     ((:defmethod) `(method ,@(rest fspec)))
791 heller 1.69 ((:labels) `(labels ,(process-fspec-for-allegro (second fspec))
792     ,(third fspec)))
793     ((:flet) `(flet ,(process-fspec-for-allegro (second fspec))
794     ,(third fspec)))))
795 mbaringer 1.68 (t
796     fspec)))
797 heller 1.88
798    
799     ;;;; Weak hashtables
800    
801     (defimplementation make-weak-key-hash-table (&rest args)
802     (apply #'make-hash-table :weak-keys t args))
803    
804     (defimplementation make-weak-value-hash-table (&rest args)
805     (apply #'make-hash-table :values :weak args))
806 mkoeppe 1.93
807 alendvai 1.95 (defimplementation hash-table-weakness (hashtable)
808     (cond ((excl:hash-table-weak-keys hashtable) :key)
809     ((eq (excl:hash-table-values hashtable) :weak) :value)))
810    
811    
812 mkoeppe 1.93
813     ;;;; Character names
814    
815     (defimplementation character-completion-set (prefix matchp)
816     (loop for name being the hash-keys of excl::*name-to-char-table*
817     when (funcall matchp prefix name)
818     collect (string-capitalize name)))

  ViewVC Help
Powered by ViewVC 1.1.5