4 ms·
Was lisp compiled code, not code.running in a repl, ever able to self modify? It looks like that wasn't the case even in 1996: https://groups.google.com/g/com
by dorfsmay 3y ago
Was lisp compiled code, not code.running in a repl, ever able to self modify?
It looks like that wasn't the case even in 1996:
https://groups.google.com/g/comp.lang.lisp/c/O1dDXlQsVkw https://groups.google.com/g/comp.lang.lisp/c/O1dDXlQsVkw
- pfdietz 3y agoOne can define and compile functions in a running Common Lisp. There's a standard function, compile, that does this. In SBCL, any evaluation of an expression is done by first compiling it. Compiled functions that are no longer accessible (including not having any frames on the stack) are garbage collected. The really interesting question is not whether users can mutate existing compiled code, but whether it's useful for the Common Lisp implementation to do so. This is because Common Lisp is a dynamic language, where generic functions and classes can be redefined on the fly. If you want to implement such things efficiently, it would be useful to be able to change existing compiled functions to reflect that (for example) this slot of this object is at this offset rather than that offset. A scheme has been proposed to do that that puts such code off to the side in little chunks accessed with unconditional branches. When a redefinition occurs the branch is redirected to a newly created chunk; the old one is GCed when no longer referenced from the stack. You have to pay for the unconditional branches, but those are typically quite fast.
- dreamcompiler 3y agoSelf modifying code in the sense of changing one instruction to another at runtime (as was commonly done in assembler in the 1960s) is not generally possible with modern Common Lisps mostly because modern operating systems don't allow it. And that's a good thing because such code would be hopelessly insecure and impossible to reason about. But if you mean "compile a new function called X and replace the old X at runtime", that's easy in Common Lisp. It's not commonly done unless you're explicitly writing some kind of compiler. What is commonly done is to create a lexical closure at compile time and change its bound values at runtime. IOW changing the private data of a function at runtime is more generally useful than changing its instructions. What's most common is to write lisp programs that emit lisp source code and compile it at compile time (but usually not run time). Such programs are called macros.