-
Notifications
You must be signed in to change notification settings - Fork 2
/
allegro.lisp
662 lines (557 loc) · 23.1 KB
/
allegro.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
;;;; -*- indent-tabs-mode: nil; outline-regexp: ";;;;;* "; -*-
;;;
;;; swank-allegro.lisp --- Allegro CL specific code for SLIME.
;;;
;;; Created 2003
;;;
;;; This code has been placed in the Public Domain. All warranties
;;; are disclaimed.
;;;
(in-package :conium)
(eval-when (:compile-toplevel :load-toplevel :execute)
(require :sock)
(require :process))
(defvar *external-format-to-coding-system*
'((:iso-8859-1
"latin-1" "latin-1-unix" "iso-latin-1-unix"
"iso-8859-1" "iso-8859-1-unix")
(:utf-8 "utf-8" "utf-8-unix")
(:euc-jp "euc-jp" "euc-jp-unix")
(:us-ascii "us-ascii" "us-ascii-unix")
(:emacs-mule "emacs-mule" "emacs-mule-unix")))
(defimplementation find-external-format (coding-system)
(let ((e (rassoc-if (lambda (x) (member coding-system x :test #'equal))
*external-format-to-coding-system*)))
(and e (excl:crlf-base-ef
(excl:find-external-format (car e)
:try-variant t)))))
(defimplementation format-sldb-condition (c)
(princ-to-string c))
(defimplementation call-with-syntax-hooks (fn)
(funcall fn))
;;;; Unix signals
(defimplementation call-without-interrupts (fn)
(excl:without-interrupts (funcall fn)))
(defimplementation getpid ()
(excl::getpid))
(defimplementation lisp-implementation-type-name ()
"allegro")
(defimplementation set-default-directory (directory)
(let* ((dir (namestring (truename (merge-pathnames directory)))))
(setf *default-pathname-defaults* (pathname (excl:chdir dir)))
dir))
(defimplementation default-directory ()
(namestring (excl:current-directory)))
;;;; Misc
(defimplementation arglist (symbol)
(handler-case (excl:arglist symbol)
(simple-error () :not-available)))
(defimplementation macroexpand-all (form)
(excl::walk form))
(defimplementation describe-symbol-for-emacs (symbol)
(let ((result '()))
(flet ((doc (kind &optional (sym symbol))
(or (documentation sym kind) :not-documented))
(maybe-push (property value)
(when value
(setf result (list* property value result)))))
(maybe-push
:variable (when (boundp symbol)
(doc 'variable)))
(maybe-push
:function (if (fboundp symbol)
(doc 'function)))
(maybe-push
:class (if (find-class symbol nil)
(doc 'class)))
result)))
(defimplementation describe-definition (symbol namespace)
(ecase namespace
(:variable
(describe symbol))
((:function :generic-function)
(describe (symbol-function symbol)))
(:class
(describe (find-class symbol)))))
;;;; Debugger
(defvar *sldb-topframe*)
(defimplementation call-with-debugging-environment (debugger-loop-fn)
(let ((*sldb-topframe* (find-topframe))
(excl::*break-hook* nil))
(funcall debugger-loop-fn)))
(defimplementation sldb-break-at-start (fname)
;; :print-before is kind of mis-used but we just want to stuff our break form
;; somewhere. This does not work for setf, :before and :after methods, which
;; need special syntax in the trace call, see ACL's doc/debugging.htm chapter 10.
(eval `(trace (,fname
:print-before
((break "Function start breakpoint of ~A" ',fname)))))
`(:ok ,(format nil "Set breakpoint at start of ~S" fname)))
(defun find-topframe ()
(let ((skip-frames 3))
(do ((f (excl::int-newest-frame) (next-frame f))
(i 0 (1+ i)))
((= i skip-frames) f))))
(defun next-frame (frame)
(let ((next (excl::int-next-older-frame frame)))
(cond ((not next) nil)
((debugger:frame-visible-p next) next)
(t (next-frame next)))))
(defun nth-frame (index)
(do ((frame *sldb-topframe* (next-frame frame))
(i index (1- i)))
((zerop i) frame)))
(defimplementation compute-backtrace (start end)
(let ((end (or end most-positive-fixnum)))
(loop for f = (nth-frame start) then (next-frame f)
for i from start below end
while f collect f)))
(defimplementation print-frame (frame stream)
(debugger:output-frame stream frame :moderate))
(defimplementation frame-locals (index)
(let ((frame (nth-frame index)))
(loop for i from 0 below (debugger:frame-number-vars frame)
collect (list :name (debugger:frame-var-name frame i)
:id 0
:value (debugger:frame-var-value frame i)))))
(defimplementation frame-var-value (frame var)
(let ((frame (nth-frame frame)))
(debugger:frame-var-value frame var)))
(defimplementation disassemble-frame (index)
(disassemble (debugger:frame-function (nth-frame index))))
(defimplementation frame-source-location-for-emacs (index)
(let* ((frame (nth-frame index))
(expr (debugger:frame-expression frame))
(fspec (first expr)))
(second (first (fspec-definition-locations fspec)))))
(defimplementation eval-in-frame (form frame-number)
(let ((frame (nth-frame frame-number)))
;; let-bind lexical variables
(let ((vars (loop for i below (debugger:frame-number-vars frame)
for name = (debugger:frame-var-name frame i)
if (symbolp name)
collect `(,name ',(debugger:frame-var-value frame i)))))
(debugger:eval-form-in-context
`(let* ,vars ,form)
(debugger:environment-of-frame frame)))))
(defimplementation return-from-frame (frame-number form)
(let ((frame (nth-frame frame-number)))
(multiple-value-call #'debugger:frame-return
frame (debugger:eval-form-in-context
form
(debugger:environment-of-frame frame)))))
(defimplementation frame-restartable-p (frame)
(handler-case (debugger:frame-retryable-p frame)
(serious-condition (c)
(funcall (read-from-string "swank::background-message")
"~a ~a" frame (princ-to-string c))
nil)))
(defimplementation restart-frame (frame-number)
(let ((frame (nth-frame frame-number)))
(cond ((debugger:frame-retryable-p frame)
(apply #'debugger:frame-retry frame (debugger:frame-function frame)
(cdr (debugger:frame-expression frame))))
(t "Frame is not retryable"))))
;;;; Compiler hooks
(defvar *buffer-name* nil)
(defvar *buffer-start-position*)
(defvar *buffer-string*)
(defvar *compile-filename* nil)
(defun compiler-note-p (object)
(member (type-of object) '(excl::compiler-note compiler::compiler-note)))
(defun compiler-undefined-functions-called-warning-p (object)
(typep object 'excl:compiler-undefined-functions-called-warning))
(deftype compiler-note ()
`(satisfies compiler-note-p))
(defun signal-compiler-condition (&rest args)
(signal (apply #'make-condition 'compiler-condition args)))
(defun handle-compiler-warning (condition)
(declare (optimize (debug 3) (speed 0) (space 0)))
(cond ((and (not *buffer-name*)
(compiler-undefined-functions-called-warning-p condition))
(handle-undefined-functions-warning condition))
(t
(signal-compiler-condition
:original-condition condition
:severity (etypecase condition
(warning :warning)
(compiler-note :note))
:message (format nil "~A" condition)
:location (location-for-warning condition)))))
(defun location-for-warning (condition)
(let ((loc (getf (slot-value condition 'excl::plist) :loc)))
(cond (*buffer-name*
(make-location
(list :buffer *buffer-name*)
(list :offset *buffer-start-position* 0)))
(loc
(destructuring-bind (file . pos) loc
(make-location
(list :file (namestring (truename file)))
(list :position (1+ pos)))))
(t
(list :error "No error location available.")))))
(defun handle-undefined-functions-warning (condition)
(let ((fargs (slot-value condition 'excl::format-arguments)))
(loop for (fname . pos-file) in (car fargs) do
(loop for (pos file) in pos-file do
(signal-compiler-condition
:original-condition condition
:severity :warning
:message (format nil "Undefined function referenced: ~S"
fname)
:location (make-location (list :file file)
(list :position (1+ pos))))))))
(defimplementation call-with-compilation-hooks (function)
(handler-bind ((warning #'handle-compiler-warning)
;;(compiler-note #'handle-compiler-warning)
)
(funcall function)))
(defimplementation swank-compile-file (input-file output-file
load-p external-format)
(with-compilation-hooks ()
(let ((*buffer-name* nil)
(*compile-filename* input-file))
(compile-file *compile-filename*
:output-file output-file
:load-after-compile load-p
:external-format external-format))))
(defun call-with-temp-file (fn)
(let ((tmpname (system:make-temp-file-name)))
(unwind-protect
(with-open-file (file tmpname :direction :output :if-exists :error)
(funcall fn file tmpname))
(delete-file tmpname))))
(defun compile-from-temp-file (string)
(call-with-temp-file
(lambda (stream filename)
(write-string string stream)
(finish-output stream)
(multiple-value-bind (binary-filename warnings? failure?)
(excl:without-redefinition-warnings
;; Suppress Allegro's redefinition warnings; they are
;; pointless when we are compiling via a temporary
;; file.
(compile-file filename :load-after-compile t))
(declare (ignore warnings?))
(when binary-filename
(delete-file binary-filename))
(not failure?)))))
(defimplementation swank-compile-string (string &key buffer position filename
policy)
(declare (ignore policy))
;; We store the source buffer in excl::*source-pathname* as a string
;; of the form <buffername>;<start-offset>. Quite ugly encoding, but
;; the fasl file is corrupted if we use some other datatype.
(with-compilation-hooks ()
(let ((*buffer-name* buffer)
(*buffer-start-position* position)
(*buffer-string* string)
(*default-pathname-defaults*
(if filename
(merge-pathnames (pathname filename))
*default-pathname-defaults*)))
(compile-from-temp-file
(format nil "~S ~S~%~A"
`(in-package ,(package-name *package*))
`(eval-when (:compile-toplevel :load-toplevel)
(setq excl::*source-pathname*
',(format nil "~A;~D" buffer position)))
string)))))
;;;; Definition Finding
(defun fspec-primary-name (fspec)
(etypecase fspec
(symbol fspec)
(list (fspec-primary-name (second fspec)))))
;; If Emacs uses DOS-style eol conventions, \n\r are considered as a
;; single character, but file-position counts them as two. Here we do
;; our own conversion.
(defun count-cr (file pos)
(let* ((bufsize 256)
(type '(unsigned-byte 8))
(buf (make-array bufsize :element-type type))
(cr-count 0))
(with-open-file (stream file :direction :input :element-type type)
(loop for bytes-read = (read-sequence buf stream) do
(incf cr-count (count (char-code #\return) buf
:end (min pos bytes-read)))
(decf pos bytes-read)
(when (<= pos 0)
(return cr-count))))))
(defun find-definition-in-file (fspec type file top-level)
(let* ((part
(or (scm::find-definition-in-definition-group
fspec type (scm:section-file :file file)
:top-level top-level)
(scm::find-definition-in-definition-group
(fspec-primary-name fspec)
type (scm:section-file :file file)
:top-level top-level)))
(start (and part
(scm::source-part-start part)))
(pos (if start
(list :position (1+ start))
(list :function-name (string (fspec-primary-name fspec))))))
(make-location (list :file (namestring (truename file)))
pos)))
(defun find-definition-in-buffer (filename)
(let ((pos (position #\; filename :from-end t)))
(make-location
(list :buffer (subseq filename 0 pos))
(list :offset (parse-integer (subseq filename (1+ pos))) 0))))
(defun find-fspec-location (fspec type file top-level)
(etypecase file
(pathname
(find-definition-in-file fspec type file top-level))
((member :top-level)
(list :error (format nil "Defined at toplevel: ~A"
(fspec->string fspec))))
(string
(find-definition-in-buffer file))))
(defun fspec->string (fspec)
(etypecase fspec
(symbol (let ((*package* (find-package :keyword)))
(prin1-to-string fspec)))
(list (format nil "(~A ~A)"
(prin1-to-string (first fspec))
(let ((*package* (find-package :keyword)))
(prin1-to-string (second fspec)))))))
(defun fspec-definition-locations (fspec)
(cond
((and (listp fspec)
(eql (car fspec) :top-level-form))
(destructuring-bind (top-level-form file &optional position) fspec
(declare (ignore top-level-form))
(list
(list (list nil fspec)
(make-location (list :buffer file) ; FIXME: should use :file
(list :position position)
(list :align t))))))
((and (listp fspec) (eq (car fspec) :internal))
(destructuring-bind (_internal next _n) fspec
(declare (ignore _internal _n))
(fspec-definition-locations next)))
(t
(let ((defs (excl::find-source-file fspec)))
(when (and (null defs)
(listp fspec)
(string= (car fspec) '#:method))
;; If methods are defined in a defgeneric form, the source location is
;; recorded for the gf but not for the methods. Therefore fall back to
;; the gf as the likely place of definition.
(setq defs (excl::find-source-file (second fspec))))
(if (null defs)
(list
(list (list nil fspec)
(list :error
(format nil "Unknown source location for ~A"
(fspec->string fspec)))))
(loop for (fspec type file top-level) in defs
collect (list (list type fspec)
(find-fspec-location fspec type file top-level))))))))
(defimplementation find-definitions (symbol)
(fspec-definition-locations symbol))
;;;; XREF
(defmacro defxref (name relation name1 name2)
`(defimplementation ,name (x)
(xref-result (xref:get-relation ,relation ,name1 ,name2))))
(defxref who-calls :calls :wild x)
(defxref calls-who :calls x :wild)
(defxref who-references :uses :wild x)
(defxref who-binds :binds :wild x)
(defxref who-macroexpands :macro-calls :wild x)
(defxref who-sets :sets :wild x)
(defun xref-result (fspecs)
(loop for fspec in fspecs
append (fspec-definition-locations fspec)))
;; list-callers implemented by groveling through all fbound symbols.
;; Only symbols are considered. Functions in the constant pool are
;; searched recursively. Closure environments are ignored at the
;; moment (constants in methods are therefore not found).
(defun map-function-constants (function fn depth)
"Call FN with the elements of FUNCTION's constant pool."
(do ((i 0 (1+ i))
(max (excl::function-constant-count function)))
((= i max))
(let ((c (excl::function-constant function i)))
(cond ((and (functionp c)
(not (eq c function))
(plusp depth))
(map-function-constants c fn (1- depth)))
(t
(funcall fn c))))))
(defun in-constants-p (fun symbol)
(map-function-constants fun
(lambda (c)
(when (eq c symbol)
(return-from in-constants-p t)))
3))
(defun function-callers (name)
(let ((callers '()))
(do-all-symbols (sym)
(when (fboundp sym)
(let ((fn (fdefinition sym)))
(when (in-constants-p fn name)
(push sym callers)))))
callers))
(defimplementation list-callers (name)
(xref-result (function-callers name)))
(defimplementation list-callees (name)
(let ((result '()))
(map-function-constants (fdefinition name)
(lambda (c)
(when (fboundp c)
(push c result)))
2)
(xref-result result)))
;;;; Profiling
;; Per-function profiling based on description in
;; http://www.franz.com/support/documentation/8.0/doc/runtime-analyzer.htm#data-collection-control-2
(defvar *profiled-functions* ())
(defvar *profile-depth* 0)
(defmacro with-redirected-y-or-n-p (&body body)
;; If the profiler is restarted when the data from the previous
;; session is not reported yet, the user is warned via Y-OR-N-P.
;; As the CL:Y-OR-N-P question is (for some reason) not directly
;; sent to the Slime user, the function CL:Y-OR-N-P is temporarily
;; overruled.
`(let* ((pkg (find-package "common-lisp"))
(saved-pdl (excl::package-definition-lock pkg))
(saved-ynp (symbol-function 'cl:y-or-n-p)))
(setf (excl::package-definition-lock pkg) nil
(symbol-function 'cl:y-or-n-p) (symbol-function
(find-symbol "y-or-n-p-in-emacs"
"swank")))
(unwind-protect
(progn ,@body)
(setf (symbol-function 'cl:y-or-n-p) saved-ynp
(excl::package-definition-lock pkg) saved-pdl))))
(defun start-acl-profiler ()
(with-redirected-y-or-n-p
(prof:start-profiler :type :time :count t
:start-sampling-p nil :verbose nil)))
(defun acl-profiler-active-p ()
(not (eq (prof:profiler-status :verbose nil) :inactive)))
(defun stop-acl-profiler ()
(prof:stop-profiler :verbose nil))
(excl:def-fwrapper profile-fwrapper (&rest args)
;; Ensures sampling is done during the execution of the function,
;; taking into account recursion.
(declare (ignore args))
(cond ((zerop *profile-depth*)
(let ((*profile-depth* (1+ *profile-depth*)))
(prof:start-sampling)
(unwind-protect (excl:call-next-fwrapper)
(prof:stop-sampling))))
(t
(excl:call-next-fwrapper))))
(defimplementation profile (fname)
(unless (acl-profiler-active-p)
(start-acl-profiler))
(excl:fwrap fname 'profile-fwrapper 'profile-fwrapper)
(push fname *profiled-functions*))
(defimplementation profiled-functions ()
*profiled-functions*)
(defimplementation unprofile (fname)
(excl:funwrap fname 'profile-fwrapper)
(setq *profiled-functions* (remove fname *profiled-functions*)))
(defimplementation profile-report ()
(prof:show-flat-profile :verbose nil)
(when *profiled-functions*
(start-acl-profiler)))
(defimplementation profile-reset ()
(when (acl-profiler-active-p)
(stop-acl-profiler)
(start-acl-profiler))
"Reset profiling counters.")
;;;; Inspecting
(excl:without-redefinition-warnings
(defmethod emacs-inspect ((o t))
(allegro-inspect o)))
(defmethod emacs-inspect ((o function))
(allegro-inspect o))
(defmethod emacs-inspect ((o standard-object))
(allegro-inspect o))
(defun allegro-inspect (o)
(loop for (d dd) on (inspect::inspect-ctl o)
append (frob-allegro-field-def o d)
until (eq d dd)))
(defun frob-allegro-field-def (object def)
(with-struct (inspect::field-def- name type access) def
(ecase type
((:unsigned-word :unsigned-byte :unsigned-natural
:unsigned-long :unsigned-half-long
:unsigned-3byte)
(label-value-line name (inspect::component-ref-v object access type)))
((:lisp :value :func)
(label-value-line name (inspect::component-ref object access)))
(:indirect
(destructuring-bind (prefix count ref set) access
(declare (ignore set prefix))
(loop for i below (funcall count object)
append (label-value-line (format nil "~A-~D" name i)
(funcall ref object i))))))))
(defimplementation quit-lisp ()
(excl:exit 0 :quiet t))
;;Trace implementations
;;In Allegro 7.0, we have:
;; (trace <name>)
;; (trace ((method <name> <qualifier>? (<specializer>+))))
;; (trace ((labels <name> <label-name>)))
;; (trace ((labels (method <name> (<specializer>+)) <label-name>)))
;; <name> can be a normal name or a (setf name)
(defimplementation toggle-trace (spec)
(ecase (car spec)
((setf)
(toggle-trace-aux spec))
(:defgeneric (toggle-trace-generic-function-methods (second spec)))
((setf :defmethod :labels :flet)
(toggle-trace-aux (process-fspec-for-allegro spec)))
(:call
(destructuring-bind (caller callee) (cdr spec)
(toggle-trace-aux callee
:inside (list (process-fspec-for-allegro caller)))))))
(defun tracedp (fspec)
(member fspec (eval '(trace)) :test #'equal))
(defun toggle-trace-aux (fspec &rest args)
(cond ((tracedp fspec)
(eval `(untrace ,fspec))
(format nil "~S is now untraced." fspec))
(t
(eval `(trace (,fspec ,@args)))
(format nil "~S is now traced." fspec))))
(defun toggle-trace-generic-function-methods (name)
(let ((methods (mop:generic-function-methods (fdefinition name))))
(cond ((tracedp name)
(eval `(untrace ,name))
(dolist (method methods (format nil "~S is now untraced." name))
(excl:funtrace (mop:method-function method))))
(t
(eval `(trace (,name)))
(dolist (method methods (format nil "~S is now traced." name))
(excl:ftrace (mop:method-function method)))))))
(defun process-fspec-for-allegro (fspec)
(cond ((consp fspec)
(ecase (first fspec)
((setf) fspec)
((:defun :defgeneric) (second fspec))
((:defmethod) `(method ,@(rest fspec)))
((:labels) `(labels ,(process-fspec-for-allegro (second fspec))
,(third fspec)))
((:flet) `(flet ,(process-fspec-for-allegro (second fspec))
,(third fspec)))))
(t
fspec)))
;;;; Weak hashtables
(defimplementation make-weak-key-hash-table (&rest args)
(apply #'make-hash-table :weak-keys t args))
(defimplementation make-weak-value-hash-table (&rest args)
(apply #'make-hash-table :values :weak args))
(defimplementation hash-table-weakness (hashtable)
(cond ((excl:hash-table-weak-keys hashtable) :key)
((eq (excl:hash-table-values hashtable) :weak) :value)))
;;;; Character names
(defimplementation character-completion-set (prefix matchp)
(loop for name being the hash-keys of excl::*name-to-char-table*
when (funcall matchp prefix name)
collect (string-capitalize name)))