make-array
如何在SBCL中工作?C++中是否有一些等价的new
和delete
运算符,还是其他东西,也许是汇编程序级别?
我偷看了一下源头,但什么都不懂。
当使用从源代码编译的 SBCL 和像 Emacs/Slime 这样的环境时,可以使用M-. (元点(非常轻松地导航代码。基本上,make-array
符号绑定到多个事物:deftransform
定义和defun
。deftransform
主要用于优化,因此最好先遵循该功能。
make-array
函数委托给内部make-array%
函数,这非常复杂:它检查参数,并根据这些参数调度到数组的不同专用实现:例如,位向量的实现方式与字符串不同。
如果你遵循simple-array
的情况,你会发现一个调用allocate-vector-with-widetag
的函数,而又调用allocate-vector
。
现在,allocate-vector
绑定到多个对象、多个defoptimizers
形式、一个函数和一个define-vop
形式。
该功能仅是:
(defun allocate-vector (type length words)
(allocate-vector type length words))
即使它看起来像递归调用,也不是。
define-vop
表单是一种定义如何编译对allocate-vector
的调用的方法。在函数中,以及任何调用allocate-vector
的地方,编译器知道如何编写实现内置操作的程序集。但是,函数本身的定义是,因此有一个同名的入口点,以及一个包装该代码的函数对象。
define-vop
依赖于 SBCL 中的域特定语言,该语言在汇编上抽象。如果遵循该定义,则可以为allocate-vector
找到不同的vops(虚拟操作(,例如allocate-vector-on-heap
和allocate-vector-on-stack
。
堆上的分配转化为对calc-size-in-bytes
的调用,对allocation
和put-header
的调用,这很可能分配内存并标记它(我按照定义src/compiler/x86-64/alloc.lisp
(。 如何分配(和垃圾回收(内存是另一个问题。
allocation
使用%alloc-tramp
发出汇编代码,进而执行以下操作:
(invoke-asm-routine 'call (if to-r11 'alloc-tramp-r11 'alloc-tramp) node)
显然有称为alloc-tramp-r11
和alloc-tramp
的汇编例程,它们是预定义的汇编指令。一条评论说:
;;; Most allocation is done by inline code with sometimes help
;;; from the C alloc() function by way of the alloc-tramp
;;; assembly routine.
运行时有一个 C 代码基础,请参阅示例/src/runtime/alloc.c
。
-tramp
后缀代表蹦床。
也看看src/runtime/x86-assem.S
.