1 ;;; 'SCM' type tag decoding.
2 ;;; Copyright (C) 2014, 2015 Free Software Foundation, Inc.
4 ;;; This library is free software; you can redistribute it and/or modify it
5 ;;; under the terms of the GNU Lesser General Public License as published by
6 ;;; the Free Software Foundation; either version 3 of the License, or (at
7 ;;; your option) any later version.
9 ;;; This library is distributed in the hope that it will be useful, but
10 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
11 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
12 ;;; General Public License for more details.
14 ;;; You should have received a copy of the GNU Lesser General Public License
15 ;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
17 (define-module (system base types)
18 #:use-module (rnrs bytevectors)
19 #:use-module (rnrs io ports)
20 #:use-module (srfi srfi-1)
21 #:use-module (srfi srfi-9)
22 #:use-module (srfi srfi-9 gnu)
23 #:use-module (srfi srfi-11)
24 #:use-module (srfi srfi-26)
25 #:use-module (srfi srfi-60)
26 #:use-module (ice-9 match)
27 #:use-module (ice-9 iconv)
28 #:use-module (ice-9 format)
29 #:use-module (ice-9 vlist)
30 #:use-module (system foreign)
42 inferior-object-sub-kind
43 inferior-object-address
50 inferior-struct-fields
56 ;;; 'SCM' type tag decoding, primarily to support Guile debugging in GDB.
69 (define-record-type <memory-backend>
70 (memory-backend peek open type-name)
72 (peek memory-backend-peek)
73 (open memory-backend-open)
74 (type-name memory-backend-type-name)) ; for SMOBs and ports
76 (define %ffi-memory-backend
77 ;; The FFI back-end to access the current process's memory. The main
78 ;; purpose of this back-end is to allow testing.
80 (define (dereference-word address)
81 (let* ((ptr (make-pointer address))
82 (bv (pointer->bytevector ptr %word-size)))
83 (bytevector-uint-ref bv 0 (native-endianness) %word-size)))
85 (define (open address size)
86 (define current-address address)
88 (define (read-memory! bv index count)
89 (let* ((ptr (make-pointer current-address))
90 (mem (pointer->bytevector ptr count)))
91 (bytevector-copy! mem 0 bv index count)
92 (set! current-address (+ current-address count))
96 (let* ((ptr (make-pointer address))
97 (bv (pointer->bytevector ptr size)))
98 (open-bytevector-input-port bv))
99 (let ((port (make-custom-binary-input-port "ffi-memory"
102 (setvbuf port _IONBF)
105 (memory-backend dereference-word open #f)))
107 (define-inlinable (dereference-word backend address)
108 "Return the word at ADDRESS, using BACKEND."
109 (let ((peek (memory-backend-peek backend)))
112 (define-syntax memory-port
114 "Return an input port to the SIZE bytes at ADDRESS, using BACKEND. When
115 SIZE is omitted, return an unbounded port to the memory at ADDRESS."
117 (let ((open (memory-backend-open backend)))
119 ((_ backend address size)
120 (let ((open (memory-backend-open backend)))
121 (open address size)))))
123 (define (get-word port)
124 "Read a word from PORT and return it as an integer."
125 (let ((bv (get-bytevector-n port %word-size)))
126 (bytevector-uint-ref bv 0 (native-endianness) %word-size)))
128 (define-inlinable (type-number->name backend kind number)
129 "Return the name of the type NUMBER of KIND, where KIND is one of
130 'smob or 'port, or #f if the information is unavailable."
131 (let ((proc (memory-backend-type-name backend)))
132 (and proc (proc kind number))))
136 ;;; Matching bit patterns and cells.
139 (define-syntax match-cell-words
140 (syntax-rules (bytevector)
141 ((_ port ((bytevector name len) rest ...) body)
142 (let ((name (get-bytevector-n port len))
143 (remainder (modulo len %word-size)))
144 (unless (zero? remainder)
145 (get-bytevector-n port (- %word-size remainder)))
146 (match-cell-words port (rest ...) body)))
147 ((_ port (name rest ...) body)
148 (let ((name (get-word port)))
149 (match-cell-words port (rest ...) body)))
153 (define-syntax match-bit-pattern
154 (syntax-rules (& || = _)
155 ((match-bit-pattern bits ((a || b) & n = c) consequent alternate)
156 (let ((tag (logand bits n)))
159 (a (logand bits (bitwise-not n))))
162 ((match-bit-pattern bits (x & n = c) consequent alternate)
163 (let ((tag (logand bits n)))
168 ((match-bit-pattern bits (_ & n = c) consequent alternate)
169 (let ((tag (logand bits n)))
173 ((match-bit-pattern bits ((a << n) || c) consequent alternate)
174 (let ((tag (bitwise-and bits (- (expt 2 n) 1))))
176 (let ((a (arithmetic-shift bits (- n))))
180 (define-syntax match-cell-clauses
182 ((_ port tag (((tag-pattern thing ...) body) rest ...))
183 (match-bit-pattern tag tag-pattern
184 (match-cell-words port (thing ...) body)
185 (match-cell-clauses port tag (rest ...))))
187 (inferior-object 'unmatched-tag tag))))
189 (define-syntax match-cell
191 "Match a cell---i.e., a non-immediate value other than a pair. The
192 cell's contents are read from PORT."
193 ((_ port (pattern body ...) ...)
195 (tag (get-word port)))
196 (match-cell-clauses port* tag
197 ((pattern (begin body ...))
200 (define-syntax match-scm-clauses
203 (bit-pattern body ...)
205 (match-bit-pattern bits bit-pattern
207 (match-scm-clauses bits rest ...)))
211 (define-syntax match-scm
213 "Match BITS, an integer representation of an 'SCM' value, against
214 CLAUSES. Each clause must have the form:
218 PATTERN is a bit pattern that may specify bitwise operations on BITS to
219 determine if it matches. TEMPLATE specify the name of the variable to bind
220 the matching bits, possibly with bitwise operations to extract it from BITS."
221 ((_ bits clauses ...)
223 (match-scm-clauses bits* clauses ...)))))
227 ;;; Tags---keep in sync with libguile/tags.h!
232 (define %tc3-imm24 4)
235 (define %tc3-int1 %tc2-int)
236 (define %tc3-int2 (+ %tc2-int 4))
238 (define %tc8-char (+ 8 %tc3-imm24))
239 (define %tc8-flag (+ %tc3-imm24 0))
242 (define %tc3-struct 1)
243 (define %tc7-symbol 5)
244 (define %tc7-vector 13)
245 (define %tc7-wvect 15)
246 (define %tc7-string 21)
247 (define %tc7-number 23)
248 (define %tc7-hashtable 29)
249 (define %tc7-pointer 31)
250 (define %tc7-fluid 37)
251 (define %tc7-stringbuf 39)
252 (define %tc7-dynamic-state 45)
253 (define %tc7-frame 47)
254 (define %tc7-keyword 53)
255 (define %tc7-program 69)
256 (define %tc7-vm-continuation 71)
257 (define %tc7-bytevector 77)
258 (define %tc7-weak-set 85)
259 (define %tc7-weak-table 87)
260 (define %tc7-array 93)
261 (define %tc7-bitvector 95)
262 (define %tc7-port 125)
263 (define %tc7-smob 127)
265 (define %tc16-bignum (+ %tc7-number (* 1 256)))
266 (define %tc16-real (+ %tc7-number (* 2 256)))
267 (define %tc16-complex (+ %tc7-number (* 3 256)))
268 (define %tc16-fraction (+ %tc7-number (* 4 256)))
272 (define-record-type <stringbuf>
275 (string stringbuf-contents))
277 (set-record-type-printer! <stringbuf>
278 (lambda (stringbuf port)
279 (display "#<stringbuf " port)
280 (write (stringbuf-contents stringbuf) port)
281 (display "#>" port)))
284 (define-record-type <inferior-struct>
285 (inferior-struct name fields)
287 (name inferior-struct-name)
288 (fields inferior-struct-fields set-inferior-struct-fields!))
290 (define print-inferior-struct
291 (let ((%printed-struct (make-parameter vlist-null)))
292 (lambda (struct port)
293 (if (vhash-assq struct (%printed-struct))
296 (format port "#<struct ~a"
297 (inferior-struct-name struct))
298 (parameterize ((%printed-struct
299 (vhash-consq struct #t (%printed-struct))))
300 (for-each (lambda (field)
301 (if (eq? field struct)
302 (display " #0#" port)
303 (format port " ~s" field)))
304 (inferior-struct-fields struct)))
305 (format port " ~x>" (object-address struct)))))))
307 (set-record-type-printer! <inferior-struct> print-inferior-struct)
310 (define-record-type <inferior-fluid>
311 (inferior-fluid number value)
313 (number inferior-fluid-number)
314 (value inferior-fluid-value))
316 (set-record-type-printer! <inferior-fluid>
319 (($ <inferior-fluid> number)
320 (format port "#<fluid ~a ~x>"
322 (object-address fluid))))))
324 ;; Object type to represent complex objects from the inferior process that
325 ;; cannot be really converted to usable Scheme objects in the current
327 (define-record-type <inferior-object>
328 (%inferior-object kind sub-kind address)
330 (kind inferior-object-kind)
331 (sub-kind inferior-object-sub-kind)
332 (address inferior-object-address))
334 (define inferior-object
336 "Return an object representing an inferior object at ADDRESS, of type
339 (%inferior-object kind #f address))
340 ((kind sub-kind address)
341 (%inferior-object kind sub-kind address))))
343 (set-record-type-printer! <inferior-object>
346 (($ <inferior-object> kind sub-kind address)
347 (format port "#<~a ~:[~*~;~a ~]~x>"
348 kind sub-kind sub-kind
351 (define (inferior-smob backend type-number address)
352 "Return an object representing the SMOB at ADDRESS whose type is
354 (inferior-object 'smob
355 (or (type-number->name backend 'smob type-number)
359 (define (inferior-port backend type-number address)
360 "Return an object representing the port at ADDRESS whose type is
362 (inferior-object 'port
363 (or (type-number->name backend 'port type-number)
367 (define %visited-cells
368 ;; Vhash of mapping addresses of already visited cells to the
369 ;; corresponding inferior object. This is used to detect and represent
371 (make-parameter vlist-null))
373 (define-syntax visited
375 ((_ (address -> object) body ...)
376 (parameterize ((%visited-cells (vhash-consv address object
380 (define (address->inferior-struct address vtable-data-address backend)
381 "Read the struct at ADDRESS using BACKEND. Return an 'inferior-struct'
382 object representing it."
383 (define %vtable-layout-index 0)
384 (define %vtable-name-index 5)
386 (let* ((layout-address (+ vtable-data-address
387 (* %vtable-layout-index %word-size)))
388 (layout-bits (dereference-word backend layout-address))
389 (layout (scm->object layout-bits backend))
390 (name-address (+ vtable-data-address
391 (* %vtable-name-index %word-size)))
392 (name-bits (dereference-word backend name-address))
393 (name (scm->object name-bits backend)))
395 (let* ((layout (symbol->string layout))
396 (len (/ (string-length layout) 2))
397 (slots (dereference-word backend (+ address %word-size)))
398 (port (memory-port backend slots (* len %word-size)))
399 (fields (get-bytevector-n port (* len %word-size)))
400 (result (inferior-struct name #f)))
402 ;; Keep track of RESULT so callees can refer to it if we are
403 ;; decoding a circular struct.
404 (visited (address -> result)
405 (let ((values (map (cut scm->object <> backend)
406 (bytevector->uint-list fields
409 (set-inferior-struct-fields! result values)
411 (inferior-object 'invalid-struct address))))
413 (define* (cell->object address #:optional (backend %ffi-memory-backend))
414 "Return an object representing the object at ADDRESS, reading from memory
416 (or (and=> (vhash-assv address (%visited-cells)) cdr) ; circular object
417 (let ((port (memory-port backend address)))
419 (((vtable-data-address & 7 = %tc3-struct))
420 (address->inferior-struct address
421 (- vtable-data-address %tc3-struct)
423 (((_ & #x7f = %tc7-symbol) buf hash props)
424 (match (cell->object buf backend)
425 (($ <stringbuf> string)
426 (string->symbol string))))
427 (((_ & #x7f = %tc7-string) buf start len)
428 (match (cell->object buf backend)
429 (($ <stringbuf> string)
430 (substring string start (+ start len)))))
431 (((_ & #x047f = %tc7-stringbuf) len (bytevector buf len))
432 (stringbuf (bytevector->string buf "ISO-8859-1")))
433 (((_ & #x047f = (bitwise-ior #x400 %tc7-stringbuf))
434 len (bytevector buf (* 4 len)))
435 (stringbuf (bytevector->string buf (match (native-endianness)
437 ('big "UTF-32BE")))))
438 (((_ & #x7f = %tc7-bytevector) len address)
439 (let ((bv-port (memory-port backend address len)))
440 (get-bytevector-all bv-port)))
441 ((((len << 8) || %tc7-vector))
442 (let ((words (get-bytevector-n port (* len %word-size)))
443 (vector (make-vector len)))
444 (visited (address -> vector)
445 (fold (lambda (element index)
446 (vector-set! vector index element)
449 (map (cut scm->object <> backend)
450 (bytevector->uint-list words (native-endianness)
453 (((_ & #x7f = %tc7-wvect))
454 (inferior-object 'weak-vector address)) ; TODO: show elements
455 ((((n << 8) || %tc7-fluid) init-value)
456 (inferior-fluid n #f)) ; TODO: show current value
457 (((_ & #x7f = %tc7-dynamic-state))
458 (inferior-object 'dynamic-state address))
459 ((((flags+type << 8) || %tc7-port))
460 (inferior-port backend (logand flags+type #xff) address))
461 (((_ & #x7f = %tc7-program))
462 (inferior-object 'program address))
463 (((_ & #xffff = %tc16-bignum))
464 (inferior-object 'bignum address))
465 (((_ & #xffff = %tc16-real) pad)
466 (let* ((address (+ address (* 2 %word-size)))
467 (port (memory-port backend address (sizeof double)))
468 (words (get-bytevector-n port (sizeof double))))
469 (bytevector-ieee-double-ref words 0 (native-endianness))))
470 (((_ & #x7f = %tc7-number) mpi)
471 (inferior-object 'number address))
472 (((_ & #x7f = %tc7-hashtable) buckets meta-data unused)
473 (inferior-object 'hash-table address))
474 (((_ & #x7f = %tc7-pointer) address)
475 (make-pointer address))
476 (((_ & #x7f = %tc7-keyword) symbol)
477 (symbol->keyword (cell->object symbol backend)))
478 (((_ & #x7f = %tc7-vm-continuation))
479 (inferior-object 'vm-continuation address))
480 (((_ & #x7f = %tc7-weak-set))
481 (inferior-object 'weak-set address))
482 (((_ & #x7f = %tc7-weak-table))
483 (inferior-object 'weak-table address))
484 (((_ & #x7f = %tc7-array))
485 (inferior-object 'array address))
486 (((_ & #x7f = %tc7-bitvector))
487 (inferior-object 'bitvector address))
488 ((((smob-type << 8) || %tc7-smob) word1)
489 (inferior-smob backend smob-type address))))))
492 (define* (scm->object bits #:optional (backend %ffi-memory-backend))
493 "Return the Scheme object corresponding to BITS, the bits of an 'SCM'
496 (((integer << 2) || %tc2-int)
498 ((address & 6 = %tc3-cons)
499 (let* ((type (dereference-word backend address))
500 (pair? (not (bit-set? 0 type))))
502 (or (and=> (vhash-assv address (%visited-cells)) cdr)
504 (cdrloc (+ address %word-size))
505 (pair (cons *unspecified* *unspecified*)))
506 (visited (address -> pair)
507 (set-car! pair (scm->object car backend))
509 (scm->object (dereference-word backend cdrloc)
512 (cell->object address backend))))
513 (((char << 8) || %tc8-char)
514 (integer->char char))
515 (((flag << 8) || %tc8-flag)
522 ((9) (inferior-object 'undefined bits))
524 ((11) (inferior-object 'unbound bits))))))
527 ;;; eval: (put 'match-scm 'scheme-indent-function 1)
528 ;;; eval: (put 'match-cell 'scheme-indent-function 1)
529 ;;; eval: (put 'visited 'scheme-indent-function 1)
532 ;;; types.scm ends here