3 ;; Copyright (C) 2001 Free Software Foundation, Inc.
5 ;; This program is free software; you can redistribute it and/or modify
6 ;; it under the terms of the GNU General Public License as published by
7 ;; the Free Software Foundation; either version 2, or (at your option)
10 ;; This program is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ;; GNU General Public License for more details.
15 ;; You should have received a copy of the GNU General Public License
16 ;; along with this program; see the file COPYING. If not, write to
17 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 ;; Boston, MA 02111-1307, USA.
22 (define (puts x) (display x) (newline))
24 (define (user-error msg . args)
25 (throw 'user-error #f msg args #f))
32 (define *command-table*
33 '((help (help h) (apropos a) (describe d) (option o) (quit q))
34 (module (module m) (use u) (import i) (load l) (binding b) (lsmod lm))
35 (package (package p) (lspkg lp) (autopackage) (globals g))
36 (language (language L))
37 (compile (compile c) (compile-file cc)
38 (disassemble x) (disassemble-file xx))
39 (profile (time t) (profile pr))
40 (debug (backtrace bt) (debugger db) (trace tr) (step st))
41 (system (statistics stat) (gc))))
43 (define (group-name g) (car g))
44 (define (group-commands g) (cdr g))
46 (define *command-module* (current-module))
47 (define (command-name c) (car c))
48 (define (command-abbrev c) (if (null? (cdr c)) #f (cadr c)))
49 (define (command-procedure c) (module-ref *command-module* (command-name c)))
50 (define (command-doc c) (procedure-documentation (command-procedure c)))
52 (define (command-usage c)
53 (let ((doc (command-doc c)))
54 (substring doc 0 (string-index doc #\newline))))
56 (define (command-summary c)
57 (let* ((doc (command-doc c))
58 (start (1+ (string-index doc #\newline))))
59 (cond ((string-index doc #\newline start)
60 => (lambda (end) (substring doc start end)))
61 (else (substring doc start)))))
63 (define (lookup-group name)
64 (assq name *command-table*))
66 (define (lookup-command key)
67 (let loop ((groups *command-table*) (commands '()))
68 (cond ((and (null? groups) (null? commands)) #f)
70 (loop (cdr groups) (cdar groups)))
71 ((memq key (car commands)) (car commands))
72 (else (loop groups (cdr commands))))))
74 (define (display-group group . opts)
75 (format #t "~:(~A~) Commands [abbrev]:~2%" (group-name group))
77 (display-summary (command-usage c)
80 (group-commands group))
83 (define (display-command command)
85 (display (command-doc command))
88 (define (display-summary usage abbrev summary)
89 (let ((abbrev (if abbrev (format #f "[,~A]" abbrev) "")))
90 (format #t " ,~24A ~8@A - ~A\n" usage abbrev summary)))
92 (define (meta-command repl line)
93 (let ((input (call-with-input-string (string-append "(" line ")") read)))
94 (if (not (null? input))
95 (do ((key (car input))
96 (args (cdr input) (cdr args))
97 (opts '() (cons (make-keyword-from-dash-symbol (car args)) opts)))
99 (not (symbol? (car args)))
100 (not (eq? (string-ref (symbol->string (car args)) 0) #\-)))
101 (let ((c (lookup-command key)))
103 (cond ((memq :h opts) (display-command c))
104 (else (apply (command-procedure c)
105 repl (append! args opts))))
106 (user-error "Unknown meta command: ~A" key))))))))
113 (define (help repl . args)
116 The optional argument can be either one of command groups or
117 command names. Without argument, a list of help commands and
118 all command groups are displayed, as you have already seen :)"
121 (display-group (lookup-group 'help))
122 (display "Command Groups:\n\n")
123 (display-summary "help all" #f "List all commands")
124 (for-each (lambda (g)
125 (let* ((name (symbol->string (group-name g)))
126 (usage (string-append "help " name))
127 (header (string-append "List " name " commands")))
128 (display-summary usage #f header)))
129 (cdr *command-table*))
131 (display "Enter `,COMMAND -h' to display documentation of each command.")
134 (for-each display-group *command-table*))
135 ((? lookup-group group)
136 (display-group (lookup-group group)))
137 (else (user-error "Unknown command group: ~A" (car args)))))
139 (define guile-apropos apropos)
140 (define (apropos repl regexp)
141 "apropos [options] REGEXP
142 Find bindings/modules/packages."
143 (guile-apropos (object->string regexp display)))
145 (define (describe repl obj)
147 Show description/documentation."
148 (display "Not implemented yet\n"))
150 (define (option repl . args)
151 "option [KEY [VALUE]]
152 List/show/set options."
153 (display "Not implemented yet\n"))
165 (define (module repl . args)
167 Change modules / Show current module."
169 (() (puts (binding repl.module)))))
171 (define (use repl . args)
175 (let ((mod (resolve-interface name)))
177 (module-use! repl.module mod)
178 (user-error "No such module: ~A" name))))
180 (for-each puts (map module-name
181 (cons repl.module (module-uses repl.module))))
182 (for-each (lambda (name)
184 ((pair? name) (use name))
186 (cond ((find-one-module (symbol->string name)) => use)))
187 (else (user-error "Invalid module name: ~A" name))))
190 (define (import repl . args)
192 Import modules / List those imported."
194 (let ((mod (resolve-interface name)))
196 (module-use! repl.module mod)
197 (user-error "No such module: ~A" name))))
199 (for-each puts (map module-name
200 (cons repl.module (module-uses repl.module))))
201 (for-each (lambda (name)
203 ((pair? name) (use name))
205 (and-let* ((m (find-one-module (symbol->string name))))
207 (else (user-error "Invalid module name: ~A" name))))
210 (define (load repl file . opts)
212 Load a file in the current module."
213 (apply repl-load-file repl (->string file) opts))
215 (define (binding repl . opts)
217 List current bindings."
218 (fold (lambda (s v d) (format #t "~23A ~A\n" s v)) #f repl.module))
220 (define (lsmod repl . args)
224 (set! repl.module (resolve-module name))
225 (module-use! repl.module repl.value-history))
228 (let ((name (car args)))
230 ((pair? name) (use name))
232 (and-let* ((m (find-one-module (symbol->string name))))
234 (else (user-error "Invalid module name: ~A" name))))))
241 (define (package repl)
243 List available packages/modules."
244 (for-each puts (find-module "")))
248 List available packages/modules."
249 (for-each puts (find-module "")))
251 (define (autopackage repl)
253 List available packages/modules."
254 (for-each puts (find-module "")))
256 (define (globals repl)
258 List all global variables."
259 (global-fold (lambda (s v d) (format #t "~A\t~S\n" s v)) #f))
263 ;;; Language commands
266 (define (language repl name)
269 (set! repl.language (lookup-language name))
277 (define (compile repl form . opts)
278 "compile [options] FORM
279 Generate compiled code.
281 -e Stop after expanding syntax/macro
282 -t Stop after translating into GHIL
283 -c Stop after generating GLIL
284 -l Stop before linking
285 -o Compile into bytecode
287 -O Enable optimization
288 -D Add debug information"
289 (let ((x (apply repl-compile repl form opts)))
291 (disassemble-program x))
293 (disassemble-bytecode x))
299 (define (compile-file repl file . opts)
300 "compile-file [options] FILE
302 (apply repl-compile-file repl (->string file) opts))
304 (define (disassemble repl prog)
306 Disassemble a program."
307 (disassemble-program (repl.vm (repl-compile repl prog))))
309 (define (disassemble-file repl file)
310 "disassemble-file FILE
312 (disassemble-bytecode (load-file-in (->string file)
317 (object->string x display))
324 (define (profile repl form . opts)
327 (apply vm-profile repl.vm (repl-compile repl form) opts))
334 (define guile-backtrace backtrace)
335 (define (backtrace repl)
337 Show backtrace (if any)."
340 (define (debugger repl)
345 (define (trace repl form . opts)
348 (apply vm-trace repl.vm (repl-compile repl form) opts))
353 (display "Not implemented yet\n"))
360 (define (time repl form)
363 (let* ((vms-start (vm-stats repl.vm))
364 (gc-start (gc-run-time))
366 (result (repl-eval repl form))
368 (gc-end (gc-run-time))
369 (vms-end (vm-stats repl.vm)))
370 (define (get proc start end)
371 (/ (- (proc end) (proc start)) internal-time-units-per-second))
372 (repl-print repl result)
373 (display "clock utime stime cutime cstime gctime\n")
374 (format #t "~5,2F ~5,2F ~5,2F ~6,2F ~6,2F ~6,2F\n"
375 (get tms:clock tms-start tms-end)
376 (get tms:utime tms-start tms-end)
377 (get tms:stime tms-start tms-end)
378 (get tms:cutime tms-start tms-end)
379 (get tms:cstime tms-start tms-end)
380 (get id gc-start gc-end))
393 (define (display-stat title flag field1 field2 unit)
394 (let ((str (format #f "~~20~AA ~~10@A /~~10@A ~~A~~%" (if flag "" "@"))))
395 (format #t str title field1 field2 unit)))
397 (define (display-stat-title title field1 field2)
398 (display-stat title #t field1 field2 ""))
400 (define (display-diff-stat title flag this last unit)
401 (display-stat title flag (- this last) this unit))
403 (define (display-time-stat title this last)
405 (format #f "~10,2F" (/ num internal-time-units-per-second)))
406 (display-stat title #f (conv (- this last)) (conv this) "s"))
408 (define (display-mips-stat title this-time this-clock last-time last-clock)
409 (define (mips time clock)
410 (if (= time 0) "----" (format #f "~10,2F" (/ clock time 1000000))))
411 (display-stat title #f
412 (mips (- this-time last-time) (- this-clock last-clock))
413 (mips this-time this-clock) "mips"))
415 (define (statistics repl)
418 (let ((this-tms (times))
419 (this-vms (vm-stats repl.vm))
420 (this-gcs (gc-stats))
421 (last-tms repl.tm-stats)
422 (last-vms repl.vm-stats)
423 (last-gcs repl.gc-stats))
425 (let ((this-times (assq-ref this-gcs 'gc-times))
426 (last-times (assq-ref last-gcs 'gc-times)))
427 (display-diff-stat "GC times:" #t this-times last-times "times")
430 (let ((this-cells (assq-ref this-gcs 'cells-allocated))
431 (this-heap (assq-ref this-gcs 'cell-heap-size))
432 (this-bytes (assq-ref this-gcs 'bytes-malloced))
433 (this-malloc (assq-ref this-gcs 'gc-malloc-threshold)))
434 (display-stat-title "Memory size:" "current" "limit")
435 (display-stat "heap" #f this-cells this-heap "cells")
436 (display-stat "malloc" #f this-bytes this-malloc "bytes")
439 (let ((this-marked (assq-ref this-gcs 'cells-marked))
440 (last-marked (assq-ref last-gcs 'cells-marked))
441 (this-swept (assq-ref this-gcs 'cells-swept))
442 (last-swept (assq-ref last-gcs 'cells-swept)))
443 (display-stat-title "Cells collected:" "diff" "total")
444 (display-diff-stat "marked" #f this-marked last-marked "cells")
445 (display-diff-stat "swept" #f this-swept last-swept "cells")
448 (let ((this-mark (assq-ref this-gcs 'gc-mark-time-taken))
449 (last-mark (assq-ref last-gcs 'gc-mark-time-taken))
450 (this-sweep (assq-ref this-gcs 'gc-sweep-time-taken))
451 (last-sweep (assq-ref last-gcs 'gc-sweep-time-taken))
452 (this-total (assq-ref this-gcs 'gc-time-taken))
453 (last-total (assq-ref last-gcs 'gc-time-taken)))
454 (display-stat-title "GC time taken:" "diff" "total")
455 (display-time-stat "mark" this-mark last-mark)
456 (display-time-stat "sweep" this-sweep last-sweep)
457 (display-time-stat "total" this-total last-total)
459 ;; Process time spent
460 (let ((this-utime (tms:utime this-tms))
461 (last-utime (tms:utime last-tms))
462 (this-stime (tms:stime this-tms))
463 (last-stime (tms:stime last-tms))
464 (this-cutime (tms:cutime this-tms))
465 (last-cutime (tms:cutime last-tms))
466 (this-cstime (tms:cstime this-tms))
467 (last-cstime (tms:cstime last-tms)))
468 (display-stat-title "Process time spent:" "diff" "total")
469 (display-time-stat "user" this-utime last-utime)
470 (display-time-stat "system" this-stime last-stime)
471 (display-time-stat "child user" this-cutime last-cutime)
472 (display-time-stat "child system" this-cstime last-cstime)
475 (let ((this-time (vms:time this-vms))
476 (last-time (vms:time last-vms))
477 (this-clock (vms:clock this-vms))
478 (last-clock (vms:clock last-vms)))
479 (display-stat-title "VM statistics:" "diff" "total")
480 (display-time-stat "time spent" this-time last-time)
481 (display-diff-stat "bogoclock" #f this-clock last-clock "clock")
482 (display-mips-stat "bogomips" this-time this-clock last-time last-clock)
486 (set! repl.tm-stats this-tms)
487 (set! repl.vm-stats this-vms)
488 (set! repl.gc-stats this-gcs)))