Coder Social home page Coder Social logo

hoangpq / ulisp Goto Github PK

View Code? Open in Web Editor NEW

This project forked from eatonphil/ulisp

0.0 1.0 0.0 105 KB

A compiler for a lisp-like language targeting LLVM IR, x86 assembly

Home Page: http://notes.eatonphil.com/compiler-basics-llvm.html

JavaScript 93.91% Common Lisp 4.93% Shell 0.29% NewLisp 0.88%

ulisp's Introduction

ulisp

A compiler for a lisp-like language written in JavaScript targeting LLVM, x86 assembly.

Tutorials

  1. Lisp to assembly
  2. User-defined functions and variables
  3. LLVM
  4. LLVM conditionals and compiling fibonacci
  5. LLVM system calls
  6. An x86 upgrade

Example

The following program:

$ cat tests/fib.lisp
(def fib (n)
     (if (< n 2)
	 n
       (+ (fib (- n 1)) (fib (- n 2)))))

(def main ()
     (fib 8))

Returns 21 as its exit code when compiled:

$ node src/ulisp.js tests/fib.lisp
$ ./build/prog
$ echo $?
21

By generating this LLVM IR:

define i32 @fib(i32 %n) {
  %ifresult7 = alloca i32, align 4
  %sym8 = add i32 %n, 0
  %sym9 = add i32 2, 0
  %sym6 = icmp slt i32 %sym8, %sym9
  br i1 %sym6, label %iftrue10, label %iffalse11
iftrue10:
  %sym12 = add i32 %n, 0
  store i32 %sym12, i32* %ifresult7, align 4
  br label %ifend13
iffalse11:
  %sym18 = add i32 %n, 0
  %sym19 = add i32 1, 0
  %sym17 = sub i32 %sym18, %sym19
  %sym15 = call i32 @fib(i32 %sym17)
  %sym21 = add i32 %n, 0
  %sym22 = add i32 2, 0
  %sym20 = sub i32 %sym21, %sym22
  %sym16 = call i32 @fib(i32 %sym20)
  %sym14 = add i32 %sym15, %sym16
  store i32 %sym14, i32* %ifresult7, align 4
  br label %ifend13
ifend13:
  %sym5 = load i32, i32* %ifresult7, align 4
  ret i32 %sym5
}

define i32 @main() {
  %sym6 = add i32 8, 0
  %sym5 = call i32 @fib(i32 %sym6)
  ret i32 %sym5
}

ulisp's People

Contributors

dependabot[bot] avatar eatonphil avatar eksea avatar frankleonrose avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.