by rbanffy · 13y ago · 0 comments
But I wouldn't go for Common Lisp. I'd go for something smaller and cleaner.
1. The ability to manually manage memory instead of relying on a GC.
2. Low-level access to memory. As in, the ability to address an arbitrary location in physical memory
3. Inline assembly and the ability to link with assembly code
There have been operating systems written in Lisp in the past, such as the operating systems for the various Lisp Machines. https://en.wikipedia.org/wiki/Lisp_machine
In other words, the problem is not with Lisp, nor with a lack of support for low-level features, but with tooling. The tool support for writing a kernel in Common Lisp is pretty bad at the moment, and there are not many people trying to improve on that.
http://www.sbcl.org/manual/#Foreign-Function-Interface
You can disable the garbage collector with without-gcing or gc-off. To manually allocate and deallocate you need to go beyond SBCL, at least as far as I know; you can call malloc and free directly if you want (via the FFI) or you can write your own allocator using the low-level memory access facility.
Hiding in the low-level code in the FFI and in SBCL itself are "system area pointers:"
http://www.sbcl.org/manual/#index-int_002dsap
Finally, inline assembly. This is the least documented of the three features you mentioned, but here is an example of how to do it with the VOP facility:
http://pvk.ca/Blog/Lisp/hacking_SSE_intrinsics-part_1.html
Unfortunately, all of the above are extremely hard to use due to the lack of good tooling. Manual memory management is possible, but going from a pointer to a block of memory to a Lisp object is not at all straightforward (it is possible with a lot of low-level hacking using the sb-vm and sb-sys packages). You can define VOPs, but it involves a lot of steps; you are probably better off just using the FFI, but debugging across the FFI boundary is hard, and the FFI imposes a performance hit that might outweigh the benefit of your assembly language code.
Like I said, the problem here is tooling. I suspect that there is a lack of demand for these things among SBCL and CMUCL users, and in the Lisp community in general.
It's just interesting that in theory a CPU could be designed that didn't have the concept of memory as a sequential array of bytes.
In fact, in the future this will probably rear its head again. Managing storage at a higher level could make things like massively parallel computing, caching schemes and virtual memory more powerful.
Perhaps one day the elaborate hoops current CPUs and OSs have to jump through to maintain performance in the face of memory latency limitations will be surpassed by something like this, and just as C is best suited as a systems programming language for a flat memory model, so a language like lisp will be best suited to low-level programming for this new architecture.
https://en.wikipedia.org/wiki/Lisp_machine
It would be interesting to see how to implement a Lisp machine for the modern day. For it to be useful, it should support some sorts of parallel-processing capabilities that conventional CPUs don't have. Perhaps pmap and preduce as SIMD instructions.
It's difficult to take the question seriously rather than as an attention-grab.
I think CL is in a way more operating system like than most other Lisps and would make good base for operating system. You can load and unload stuff, clean the system, etc.