September 12th, 2024

Writing a Lisp compiler (Lisp to assembly) from scratch in JavaScript (2018)

The article describes creating a Lisp compiler in JavaScript that converts Lisp expressions to assembly code, covering parsing, code generation, and assembly fundamentals, with source code available for exploration.

Read original articleLink Icon
Writing a Lisp compiler (Lisp to assembly) from scratch in JavaScript (2018)

This article outlines the process of creating a simple Lisp compiler in JavaScript, specifically targeting assembly language output. The compiler is designed to take a Lisp expression, such as "(+ 1 (+ 2 3))", and generate assembly code that performs the specified operations, ultimately producing an exit code of 6. The author discusses key components of the compiler, including parsing, code generation, and assembly basics. The parsing stage utilizes S-expression syntax to create an Abstract Syntax Tree (AST) from the input program. The article also covers assembly language fundamentals, including the use of registers and the function calling convention for x86_64 systems. The code generation phase involves translating the AST into assembly instructions, with a focus on handling function calls and managing parameters. The article concludes with instructions on how to compile the generated assembly code into an executable program using GCC, demonstrating the successful execution of the Lisp expression. The full source code for the compiler is made available for further exploration.

- The compiler translates Lisp expressions into assembly code using JavaScript.

- It employs S-expression syntax for easy parsing and generates an Abstract Syntax Tree (AST).

- Key assembly instructions and function calling conventions for x86_64 systems are discussed.

- The article provides a complete workflow from parsing to generating an executable program.

- The full source code of the compiler is accessible for users interested in implementation details.

Related

Dynamic Translation of Smalltalk to WebAssembly

Dynamic Translation of Smalltalk to WebAssembly

The article explores Smalltalk code translation to WebAssembly (WASM) within the Catalyst project. It details levels of Smalltalk, JavaScript, and WASM, focusing on direct Smalltalk to WASM optimization. A new translator, WATCompiledMethodTranslator, aids in this process, exemplified by a Smalltalk expression conversion. Creation of a WASM module for Smalltalk methods is explained, highlighting performance advantages and potential future comparisons with dynamic JS translations. Collaboration in the Smalltalk community is encouraged for further advancement.

Beating the Compiler

Beating the Compiler

The blog post discusses optimizing interpreters in assembly to outperform compilers. By enhancing the Uxn CPU interpreter, a 10-20% speedup was achieved through efficient assembly implementations and techniques inspired by LuaJIT.

Ask HN: Why do people say "Lisp has no syntax"? It has infinite syntax

Ask HN: Why do people say "Lisp has no syntax"? It has infinite syntax

The author discusses Lisp's syntax, highlighting its list-based structure and challenges with constructs like `cond`. They conclude that Lisp's complexity resembles other languages, despite its unique features.

Compilation of JavaScript to WASM, Part 2: Ahead-of-Time vs. JIT

Compilation of JavaScript to WASM, Part 2: Ahead-of-Time vs. JIT

Chris Fallin's blog post discusses his AOT compiler for JavaScript to WebAssembly, achieving 3-5x performance improvements while addressing challenges like dynamic typing and Wasm platform constraints.

Compilation of JavaScript to WASM, Part 3: Partial Evaluation

Compilation of JavaScript to WASM, Part 3: Partial Evaluation

The article concludes a series on compiling JavaScript to WebAssembly, discussing challenges, proposing a portable interpreter for debugging, and introducing the Futamura projection for program compilation and analysis.

Link Icon 6 comments
By @eatonphil - 4 months
Hey HN! Fun to see this older post here. If you liked this one, you might like my favorite compiler & interpreter resources page.

https://eatonphil.com/compilers-and-interpreters.html

By @theendisney4 - 4 months
Reminds me of a guy who (for fun) wrote lots of compilers and interpreters like lang A to lang B written in lang C. He was unsurprisingly familiar with quite many languages.
By @pjmlp - 4 months
This is the cool way to do it.