I'm sure there's a simple answer to this question. I'm browsing the sbcl source to find the lowest level implementation of CAR and I found this
(defun car (list) (car list))
So the 'real' definition of CAR must be elsewhere. :) Perhaps in the C source code? Would someone point me to the correct location?
Thanks.
* jcm@sdf.lonestar.org [2009-02-04 13:50+0100] writes:
I'm sure there's a simple answer to this question. I'm browsing the sbcl source to find the lowest level implementation of CAR and I found this
(defun car (list) (car list))
So the 'real' definition of CAR must be elsewhere. :) Perhaps in the C source code? Would someone point me to the correct location?
You can start in the file compiler/generic/objdef.lisp and look at the definition of cons. If you macroexpand the define-primitive-object form you will see somehing like:
... (DEF-REFFER CAR 0 LIST-POINTER-LOWTAG)
This tells the compiler that CAR is a function to access slot 0 in objects with LIST-POINTER-LOWTAG. If you read further at %DEF-REFFER you will see that CAR is eventually handled by IR2-CONVERT-REFFER which calls
(vop slot ...) (move-lvar-result node block locs lvar)
The name of the vop is SLOT. That vop is defined in compiler/x86/cell.lisp. And there we have it:
(loadw result object offset lowtag)
Yes, M-. could be more useful to find all those places.
Helmut.
jcm@sdf.lonestar.org wrote, On 5/2/09 1:50 AM:
I'm sure there's a simple answer to this question. I'm browsing the sbcl source to find the lowest level implementation of CAR and I found this
(defun car (list) (car list))
So the 'real' definition of CAR must be elsewhere. :) Perhaps in the C source code? Would someone point me to the correct location?
When the compiler encounters car it emits inline code. So try this in sbcl
(defun foo (x) (car x)) (disassemble #'foo)
Compare that with
(defun bar (x) (foo x)) (disassemble #'bar)
The former does not contain any function call, the latter will include a call to the function object #'foo
So the purpose of (defun car (list) (car list)) is to define a function object #'car after the compiler has already been taught how to compile car inline.