GithubHelp home page GithubHelp logo

bennetleff / pykaleidoscope Goto Github PK

View Code? Open in Web Editor NEW

This project forked from eliben/pykaleidoscope

0.0 2.0 0.0 59 KB

Implementation of the LLVM tutorial in Python

License: The Unlicense

Vim Script 0.12% Python 99.88%

pykaleidoscope's Introduction

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 8 - Debug Information, because llvlite 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.7 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: Mar 15, 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.

pykaleidoscope's People

Contributors

eliben avatar

Watchers

James Cloos avatar Bennet Leff 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.