4 ms·
This is demonstrative of one of the great superpowers of Lisp: that you can write mini native code compilers—without knowing a lick of assembly. (But knowing as
by reikonomusha 5y ago
This is demonstrative of one of the great superpowers of Lisp: that you can write mini native code compilers—without knowing a lick of assembly. (But knowing assembly certainly helps you optimize the heck out of it.)
More specifically, you can compile syntax (of your choosing) into performant, low-level Lisp code, which the Lisp compiler can then compile into efficient machine code. To do this, you need no extra tools, no intermediate files, no compiler hooks, no virtual machines, no byte codes, and no non-standard or non-portable features. It has been built in to Lisp since at least the 1980s.
Making miniature "JIT" compilers is also a technique a quantum simulator in Lisp [0a, 0b] uses to optimize quantum gate operations. It analyzes the quantum gate matrix and, on the fly, compiles it into optimized numerical Lisp code that is equivalent to that matrix acting on vectors of tensor product spaces (i.e., super huge arrays of complex double floats). Using another language would require me to do arduous things like write assembly code or learn a library like LibJIT [1] which may not even interface well with my host language.
Other simulators out there written in C use huge hand-unrolled loops with comments like
// THIS CODE WAS GENERATED, DO NOT TOUCH
and/or in-line assembly code, and still can't optimize specific matrix shapes and structures, or do algebraic simplifications to eliminate work altogether.
The regex library FTA is a great, and clean, example of a long standing practice of compiling regexen, except it doesn't use any fancy VMs or any fancy JITs, just "when you see this regex, automatically turn it into this Common Lisp code, and let the Lisp compiler handle the rest."
[0a] https://github.com/quil-lang/qvm https://github.com/quil-lang/qvm
[0b] COMPILE-OPERATOR: https://github.com/quil-lang/qvm/blob/master/src/compile-gate.lisp#L409 https://github.com/quil-lang/qvm/blob/master/src/compile-gat...
[1] https://www.gnu.org/software/libjit/ https://www.gnu.org/software/libjit/
- martincmartin 5y agoPerhaps most importantly, a good CL implementation can help with debugging this, by keeping track of the original CL code that generated the intermediate code, and pointing you to it. With the "use a program to generate C" approach, if there's some bug in the C code, the best the compiler or runtime can do is point you to the line of C code that's crashing. It's up to you to figure out where that came from. This was a problem for early C++ compilers like Cfront that translated C++ into C: the error messages from the C compiler referenced the intermediate C, not the original C++.
- gmfawcett 5y agoHow old is the #line directive, I wonder? It's been a long time since Cfront. https://stackoverflow.com/questions/2216540/whats-the-meaning-of-line-in-c-language#2216564 https://stackoverflow.com/questions/2216540/whats-the-meanin...