Frank Buss <fb@frank-buss.de> wrote:
+---------------
| So converting an integer to a pointer is not illegal code and well
| defined in ANSI C, but the result is implementation defined.
|
| In contrast to this: AFAIK in Common Lisp there is no such low-level
| access defined at all.
+---------------
No, but every Common Lisp implementation worth worrying about
*has* some such low-level access facilities defined. E.g., CMUCL
provides SYSTEM:SAP-REF-{8,16,32,64}, which are also SETF'able.
Aliased to "r{8,16,32,64}" and "w{8,16,32,64}", resp., I use these
quite heavily in my little user-mode hardware debugging environment:
hwtool> (deflex foo (copy-seq "abcdefghij"))
FOO
hwtool> foo
"abcdefghij"
hwtool> (d32 foo)
0x580005f8: 0x0000002a 0x00000028 0x64636261 0x68676665
0x58000608: 0x00006a69 0x00000000 0x580005ff 0x2800000b
0x58000618: 0x580005b3 0x2800000b 0x580005b3 0x2800000b
0x58000628: 0x580005b3 0x2800000b 0x580005b3 0x5800063b
hwtool> (loop for addr from 0x58000600
for c = (r8 addr)
while (plusp c)
collect (code-char c))
(#\a #\b #\c #\d #\e #\f #\g #\h #\i #\j)
hwtool>
Or more cleanly [??? -- well, at least it'll work after a GC]:
hwtool> (loop for addr from (system:sap-int (system:vector-sap foo))
for c = (r8 addr)
while (plusp c)
collect (code-char c))
(#\a #\b #\c #\d #\e #\f #\g #\h #\i #\j)
hwtool>
Let's poke a bit, shall we? ;-}
hwtool> (w32 0x58000604 0x48474645) ; [1], [2]
hwtool> foo
"abcdEFGHij"
hwtool>
It's even more fun when you're peeking & poking at hardware... ;-}
-Rob
[1] Yes, I use a "0" readmacro to allow "0x" to mean "#x".
[2] Yes, this would be much cleaner as:
(w32 (+ 4 (system:sap-int (system:vector-sap foo))) 0x48474645)
or even:
(setf (system:sap-ref-32 (system:vector-sap foo) 4) 0x48474645)
-----
Rob Warnock <rpw3@rpw3.org>
627 26th Avenue <URL:http://rpw3.org/>
San Mateo, CA 94403 (650)572-2607