• This repository has been archived on 05/Feb/2020
  • Stars
    star
    292
  • Rank 142,152 (Top 3 %)
  • Language
    Python
  • License
    The Unlicense
  • Created almost 10 years ago
  • Updated almost 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Implementation of the LLVM tutorial in Python

Pykaleidoscope

What is this?

LLVM comes with a great tutorial that builds a compiler for a simple language called Kaleidoscope. The compiler parses Kaleidoscope into an AST, from which LLVM code is then generated using the LLVM IR building APIs. Once we have LLVM IR, it can be JITed to generate machine code and run it. In other words, convert your language into LLVM IR and leave the rest to LLVM itself (including world-class optimizations).

The tutorial is presented in several "chapters" that start with a simple lexer and build up the language step by step.

This repository contains a chapter-by-chapter translation of the LLVM tutorial into Python, using the llvmlite package that exposes LLVM to Python.

This repository is fairly complete - the whole Kaleidoscope language is implemented. The only thing missing is Chapter 9 - Adding Debug Information, because llvmlite does not yet support convenient emission of debug info.

How to use this code

Go through the LLVM tutorial. The files in this repository are named after tutorial chapters and roughly correspond to the C++ code presented in the tutorial. In each source file, the __main__ section of code in the bottom is a small sample of usage, and there are also unit tests that check a variety of cases.

Testing

Some of the files have unit test classes in them. To run all unit tests:

$ python3.4 -m unittest discover -p "*.py"

Version of LLVM, Python and llvmlite

Last tested with Python 3.4, LLVM 3.8 and top-of-tree llvmlite.

Setting up llvmlite

Back in January 2015 I wrote a blog post about setting up llvmlite, but as often happens in LLVM-land, things have changed and it may no longer work.

The easiest way to use llvmlite right now is to download a binary release. If you can do that, save yourself the trouble and go do that. No need to read any further :-)

If you insist to build llvmlite on your own, you'll need LLVM. The easiest way to get LLVM is to grab a binary release from http://llvm.org/releases/. Be sure to grab a release that llvmlite works with (llvmlite has a correspondence of versions with the LLVMs supported).

When building llvmlite you'll have to pass in some flags to the Makefile that gets invoked by the Python setup process:

$ CXX_FLTO_FLAGS= LD_FLTO_FLAGS= \
  CXX=<path/to/clang++> LLVM_CONFIG=<path/to/llvm-config> \
  python3.4 setup.py build

Where path/to points to the binaries within the bin directory of the untarred LLVM binary release. The reasons for this complication are:

  1. Recent versions of Clang are built with themselves (bootstrapped), and llvm-config may have some compiler flags gcc doesn't support, so compiling with gcc won't work. We therefore use the same compiler that LLVM/Clang was built with to build llvmlite.
  2. Clang binary builds don't support LTO, and llvmlite's Makefile passes -flto when compiling. The *_FLTO_FLAGS settings are made to avoid that.

Note that these directions work at the time of writing (last updated: Nov 21, 2016) and may change with new versions of LLVM and/or llvmlite. I'll try to keep up but feel free to open issues if anything needs to be done differently.

More Repositories

1

pycparser

🐍 Complete C99 parser in pure Python
Python
3,226
star
2

pyelftools

Parsing ELF and DWARF in Python
Python
2,013
star
3

code-for-blog

Code samples from my blog
Go
1,574
star
4

llvm-clang-samples

UNMAINTAINED: Examples of using the LLVM and Clang compilation libraries and tools
C++
1,187
star
5

raft

🚣 Raft implementation in Go
Go
1,045
star
6

pss

pss is a power-tool for searching inside source code files.
Python
327
star
7

static-server

A simple, zero-configuration HTTP server CLI for serving static files
Go
183
star
8

modlib

Go project layout with exported packages and command-line tools, using modules
Go
180
star
9

deep-learning-samples

Sample code for deep learning & neural networks
Python
170
star
10

bobscheme

An implementation of Scheme in Python and C++
C++
167
star
11

luz-cpu

Educational open-source CPU suite (with assembler, linker and simulator)
Python
161
star
12

js-8080-sim

Intel 8080 assembler and simulator in JS, optimized for simplicity.
JavaScript
155
star
13

wasm-wat-samples

Samples of WebAssembly Text programs
WebAssembly
128
star
14

gemini-cli

Access Gemini LLMs from the command-line
Go
100
star
15

libjit-samples

Code samples for using libjit
C
91
star
16

go-sudoku

Toolkit for solving and generating Sudoku puzzles in Go
Go
88
star
17

wcx64

Clone of wc in x64 assembly
Assembly
87
star
18

c-unleashed-book-souce-code

Source code for the "C Unleashed" book by Richard Heathfield, Lawrence Kirby, et al.
C
70
star
19

gosax

gosax is a basic wrapper for stream parsing of XML (SAX) Go
Go
62
star
20

asdl_parser

Standalone ASDL parser for upstream CPython 3.x
Python
61
star
21

go-ungrammar

Ungrammar implementation and API in Go
Go
43
star
22

paip-in-clojure

πŸŒ… Code from "Paradigms of Artificial Intelligence Programming" in Clojure
Clojure
39
star
23

cs344

Introduction to Parallel Programming class code
C++
31
star
24

go-sentencepiece

Go implementation of the SentencePiece tokenizer
Go
20
star
25

go-websocket-sample

Sample Go websocket server and JS client with tracing and JSON data interchange
Go
20
star
26

esms

Legacy - ESMS (Electronic Soccer Management Simulator)
C++
18
star
27

xmlgen

C
15
star
28

go-quines

Some quines in the Go programming language
Go
15
star
29

python3-samples

Samples of Python 3 code, libraries, etc.
Python
12
star
30

gogl

Go Generic Library
Go
11
star
31

tupperformula

JS demo of Tupper's formula
JavaScript
9
star
32

arm_asm_hacks

ARM assembly hacks
Assembly
8
star
33

lsystem

JavaScript
8
star
34

hackutils

Various hacky utils for Go
Go
8
star
35

gocloud-samples

Samples/hacks with gocloud
Go
7
star
36

jscool2asm

JavaScript
5
star
37

sample-go-cli

Sample Go CLI project structure
Go
4
star
38

colorful-lines

My JS clone of the Colorful Lines game
JavaScript
4
star
39

cl-in-clj

Common Lisp --> Clojure snippets/idioms translation
Clojure
3
star
40

gocdkx

Fork of google/go-cloud for experiments.
Go
3
star
41

so-tag-sentiment-analysis

Go
2
star
42

ollama-import-experiment

Go
2
star
43

line-plotting

JavaScript
2
star
44

go-travis-test

sandbox
Shell
2
star
45

cljmin

Minimal project template for Clojure
Clojure
2
star
46

diffuse-sim

JavaScript
1
star
47

sample-go-project-vscode

Go
1
star
48

onlisp-in-clojure

Clojure
1
star
49

4clojure-solutions

My solutions to https://www.4clojure.com/problems
Clojure
1
star