David Johnson-Davies Builds a Lisp Compiler for the RP2350's RISC-V Cores — in Lisp, Naturally

From seconds to milliseconds, this Lisp-based Lisp compiler shows just how efficient uLisp can be.

Gareth Halfacree
2 months agoHW101

Lisp fan David Johnson-Davies is putting his favorite language on the Raspberry Pi Pico 2 development board, but in an unusual way — by writing a Lisp compiler for the RP2350's RISC-V cores… in his microcontroller-centric uLisp language.

"This is a simple experimental Lisp compiler, written in uLisp, that will compile a Lisp function into RISC-V machine code," Johnson-Davies explains of his creation. You can run the compiler on the RISC-V core of a Raspberry Pi Pico 2 (or another RP2350-based board). The compiler program is written in the subset of Common Lisp supported by uLisp, and will run on the RISC-V core of a RP2350-based board; I used a Raspberry Pi Pico 2."

uLisp, or microLisp, is, as the name suggests, a variant of Lisp for microcontrollers. Originally styled LISP, and named for an abbreviation of "list processor," Lisp was created by John McCarthy, Steve Russell, Timothy P. Hart, and Mike Levin in the 1960s as a language for mathematical notation influenced by lambda calculus. uLisp takes this and makes it suitable for microcontroller use, in much the same way the MicroPython does for Python.

Johnson-Davies' latest project builds on his earlier work to build a Lisp compiler in uLisp targeting Arm architecture cores, as found on the original Raspberry Pi Pico and its RP2040 microcontroller. This time, it targets the newer RP2350 on the Raspberry Pi Pico 2 — ignoring the chip's two Arm cores for the two Hazard3 RISC-V cores instead.

"To run the compiler you simply call compile on a Lisp function," Johnson-Davies explains. "The function will be compiled into a machine code function, replacing the original Lisp code, so that calling [the function name] will now execute the RISC-V machine-code version."

The simple reason for doing so, aside from "because you can": performance. Compared to interpreted Lisp, the compiled functions run orders of magnitude faster: a least-prime factorization function takes 5.4s as interpreted Lisp, and just 19ms compiled; a Takeuchi function benchmark completes in 4.1s interpreted and just 16ms compiled; and a recursive implementation of the Fibonacci series takes a whopping 50.5s interpreted and just 80ms once compiled.

The project is documented in full on the uLisp website.

Gareth Halfacree
Freelance journalist, technical author, hacker, tinkerer, erstwhile sysadmin. For hire: freelance@halfacree.co.uk.
Latest articles
Sponsored articles
Related articles
Latest articles
Read more
Related articles