Compiling Linux with LLVM - The LLVM Compiler Infrastructure Project
llvm-py: Writing Compilers In Python
-
Upload
mdevan -
Category
Technology
-
view
10.497 -
download
1
description
Transcript of llvm-py: Writing Compilers In Python
![Page 1: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/1.jpg)
llvm-py: Writing Compilers Using PythonPyCon India 2010
Mahadevan [email protected]
September 25, 2010
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 1 / 14
![Page 2: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/2.jpg)
About This Talk
Outline
Compilers overview, some terminology
LLVM about the “Low Level Virtual Machine”
llvm-py Python bindings for LLVM
About me:
Open source developer
Day job is as a software architect (medical imaging)
Started my career in 1999; mostly C, C++, a little Python!
..also the author of llvm-py.
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 2 / 14
![Page 3: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/3.jpg)
Compilers What is a Compiler?
What is a Compiler?
What is a compiler?
Something that transforms “source code” into “something else”
What is “source code”?
A well-structured, textual representation of a programNot: preprocessors, assemblers
What is “something else?”
Assembly languageAnother well-structured textual representation (e.g. cfront)Intermediate, binary representation (AOT compilers)In-memory executable code (JIT compilers)Executable images
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 3 / 14
![Page 4: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/4.jpg)
Compilers What is a Compiler?
What is a Compiler?
What is a compiler?
Something that transforms “source code” into “something else”
What is “source code”?
A well-structured, textual representation of a programNot: preprocessors, assemblers
What is “something else?”
Assembly languageAnother well-structured textual representation (e.g. cfront)Intermediate, binary representation (AOT compilers)In-memory executable code (JIT compilers)Executable images
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 3 / 14
![Page 5: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/5.jpg)
Compilers What is a Compiler?
What is a Compiler?
What is a compiler?
Something that transforms “source code” into “something else”
What is “source code”?
A well-structured, textual representation of a programNot: preprocessors, assemblers
What is “something else?”
Assembly languageAnother well-structured textual representation (e.g. cfront)Intermediate, binary representation (AOT compilers)In-memory executable code (JIT compilers)Executable images
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 3 / 14
![Page 6: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/6.jpg)
Compilers What is a Compiler?
What is a Compiler?
What is a compiler?
Something that transforms “source code” into “something else”
What is “source code”?
A well-structured, textual representation of a program
Not: preprocessors, assemblers
What is “something else?”
Assembly languageAnother well-structured textual representation (e.g. cfront)Intermediate, binary representation (AOT compilers)In-memory executable code (JIT compilers)Executable images
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 3 / 14
![Page 7: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/7.jpg)
Compilers What is a Compiler?
What is a Compiler?
What is a compiler?
Something that transforms “source code” into “something else”
What is “source code”?
A well-structured, textual representation of a programNot: preprocessors, assemblers
What is “something else?”
Assembly languageAnother well-structured textual representation (e.g. cfront)Intermediate, binary representation (AOT compilers)In-memory executable code (JIT compilers)Executable images
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 3 / 14
![Page 8: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/8.jpg)
Compilers What is a Compiler?
What is a Compiler?
What is a compiler?
Something that transforms “source code” into “something else”
What is “source code”?
A well-structured, textual representation of a programNot: preprocessors, assemblers
What is “something else?”
Assembly languageAnother well-structured textual representation (e.g. cfront)Intermediate, binary representation (AOT compilers)In-memory executable code (JIT compilers)Executable images
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 3 / 14
![Page 9: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/9.jpg)
Compilers What is a Compiler?
What is a Compiler?
What is a compiler?
Something that transforms “source code” into “something else”
What is “source code”?
A well-structured, textual representation of a programNot: preprocessors, assemblers
What is “something else?”
Assembly language
Another well-structured textual representation (e.g. cfront)Intermediate, binary representation (AOT compilers)In-memory executable code (JIT compilers)Executable images
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 3 / 14
![Page 10: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/10.jpg)
Compilers What is a Compiler?
What is a Compiler?
What is a compiler?
Something that transforms “source code” into “something else”
What is “source code”?
A well-structured, textual representation of a programNot: preprocessors, assemblers
What is “something else?”
Assembly languageAnother well-structured textual representation (e.g. cfront)
Intermediate, binary representation (AOT compilers)In-memory executable code (JIT compilers)Executable images
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 3 / 14
![Page 11: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/11.jpg)
Compilers What is a Compiler?
What is a Compiler?
What is a compiler?
Something that transforms “source code” into “something else”
What is “source code”?
A well-structured, textual representation of a programNot: preprocessors, assemblers
What is “something else?”
Assembly languageAnother well-structured textual representation (e.g. cfront)Intermediate, binary representation (AOT compilers)
In-memory executable code (JIT compilers)Executable images
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 3 / 14
![Page 12: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/12.jpg)
Compilers What is a Compiler?
What is a Compiler?
What is a compiler?
Something that transforms “source code” into “something else”
What is “source code”?
A well-structured, textual representation of a programNot: preprocessors, assemblers
What is “something else?”
Assembly languageAnother well-structured textual representation (e.g. cfront)Intermediate, binary representation (AOT compilers)In-memory executable code (JIT compilers)
Executable images
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 3 / 14
![Page 13: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/13.jpg)
Compilers What is a Compiler?
What is a Compiler?
What is a compiler?
Something that transforms “source code” into “something else”
What is “source code”?
A well-structured, textual representation of a programNot: preprocessors, assemblers
What is “something else?”
Assembly languageAnother well-structured textual representation (e.g. cfront)Intermediate, binary representation (AOT compilers)In-memory executable code (JIT compilers)Executable images
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 3 / 14
![Page 14: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/14.jpg)
Compilers Inside a Compiler
Inside a Compiler
What does the compiler do with the source code?
Lexical analysis produces tokens
Parser eats tokens, produces AST
What is an AST? And what is abstract about it?
What next after an AST?
What is an intermediate form (IR)?
example: IRs in gcc: source → GENERIC → GIMPLE → RTL →backend
What is Static Single Assignment (SSA) form?
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 4 / 14
![Page 15: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/15.jpg)
Compilers Inside a Compiler
Inside a Compiler
What does the compiler do with the source code?
Lexical analysis produces tokens
Parser eats tokens, produces AST
What is an AST? And what is abstract about it?
What next after an AST?
What is an intermediate form (IR)?
example: IRs in gcc: source → GENERIC → GIMPLE → RTL →backend
What is Static Single Assignment (SSA) form?
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 4 / 14
![Page 16: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/16.jpg)
Compilers Inside a Compiler
Inside a Compiler
What does the compiler do with the source code?
Lexical analysis produces tokens
Parser eats tokens, produces AST
What is an AST? And what is abstract about it?
What next after an AST?
What is an intermediate form (IR)?
example: IRs in gcc: source → GENERIC → GIMPLE → RTL →backend
What is Static Single Assignment (SSA) form?
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 4 / 14
![Page 17: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/17.jpg)
Compilers Inside a Compiler
Inside a Compiler
What does the compiler do with the source code?
Lexical analysis produces tokens
Parser eats tokens, produces AST
What is an AST? And what is abstract about it?
What next after an AST?
What is an intermediate form (IR)?
example: IRs in gcc: source → GENERIC → GIMPLE → RTL →backend
What is Static Single Assignment (SSA) form?
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 4 / 14
![Page 18: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/18.jpg)
Compilers Inside a Compiler
Inside a Compiler
What does the compiler do with the source code?
Lexical analysis produces tokens
Parser eats tokens, produces AST
What is an AST? And what is abstract about it?
What next after an AST?
What is an intermediate form (IR)?
example: IRs in gcc: source → GENERIC → GIMPLE → RTL →backend
What is Static Single Assignment (SSA) form?
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 4 / 14
![Page 19: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/19.jpg)
Compilers Inside a Compiler
Inside a Compiler
What does the compiler do with the source code?
Lexical analysis produces tokens
Parser eats tokens, produces AST
What is an AST? And what is abstract about it?
What next after an AST?
What is an intermediate form (IR)?
example: IRs in gcc: source → GENERIC → GIMPLE → RTL →backend
What is Static Single Assignment (SSA) form?
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 4 / 14
![Page 20: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/20.jpg)
Compilers Inside a Compiler
Inside a Compiler
What does the compiler do with the source code?
Lexical analysis produces tokens
Parser eats tokens, produces AST
What is an AST? And what is abstract about it?
What next after an AST?
What is an intermediate form (IR)?
example: IRs in gcc: source → GENERIC → GIMPLE → RTL →backend
What is Static Single Assignment (SSA) form?
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 4 / 14
![Page 21: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/21.jpg)
Compilers Passes, Optimization and Code Generation
Passes, Optimization and Code Generation
What is a “pass”?
What is a “optimization”?
What is a “code generator”?
What is a “code generator generator”?
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 5 / 14
![Page 22: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/22.jpg)
Compilers Passes, Optimization and Code Generation
Passes, Optimization and Code Generation
What is a “pass”?
What is a “optimization”?
What is a “code generator”?
What is a “code generator generator”?
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 5 / 14
![Page 23: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/23.jpg)
Compilers Passes, Optimization and Code Generation
Passes, Optimization and Code Generation
What is a “pass”?
What is a “optimization”?
What is a “code generator”?
What is a “code generator generator”?
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 5 / 14
![Page 24: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/24.jpg)
Compilers Passes, Optimization and Code Generation
Passes, Optimization and Code Generation
What is a “pass”?
What is a “optimization”?
What is a “code generator”?
What is a “code generator generator”?
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 5 / 14
![Page 25: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/25.jpg)
Compilers The Frontend and The Backend
The Frontend and The Backend
Everything before the first IR is usually called the frontend
And everything after as the backend
LLVM and llvm-py deal only with backends
Frontends tend to be simpler
Many frontends for a backend is common (Scala, Groovy, Clojure:Java; C#, IronPython, etc: .NET)
To build a front end in Python:
hand-coded lexer, (rec-desc) parserAntlr (generates Python)Yacc, Bison, Lemon (generates C, wrap to Python)Sphinx (C++, wrap to Python)various Python parser generators
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 6 / 14
![Page 26: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/26.jpg)
Compilers The Frontend and The Backend
The Frontend and The Backend
Everything before the first IR is usually called the frontend
And everything after as the backend
LLVM and llvm-py deal only with backends
Frontends tend to be simpler
Many frontends for a backend is common (Scala, Groovy, Clojure:Java; C#, IronPython, etc: .NET)
To build a front end in Python:
hand-coded lexer, (rec-desc) parserAntlr (generates Python)Yacc, Bison, Lemon (generates C, wrap to Python)Sphinx (C++, wrap to Python)various Python parser generators
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 6 / 14
![Page 27: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/27.jpg)
Compilers The Frontend and The Backend
The Frontend and The Backend
Everything before the first IR is usually called the frontend
And everything after as the backend
LLVM and llvm-py deal only with backends
Frontends tend to be simpler
Many frontends for a backend is common (Scala, Groovy, Clojure:Java; C#, IronPython, etc: .NET)
To build a front end in Python:
hand-coded lexer, (rec-desc) parserAntlr (generates Python)Yacc, Bison, Lemon (generates C, wrap to Python)Sphinx (C++, wrap to Python)various Python parser generators
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 6 / 14
![Page 28: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/28.jpg)
Compilers The Frontend and The Backend
The Frontend and The Backend
Everything before the first IR is usually called the frontend
And everything after as the backend
LLVM and llvm-py deal only with backends
Frontends tend to be simpler
Many frontends for a backend is common (Scala, Groovy, Clojure:Java; C#, IronPython, etc: .NET)
To build a front end in Python:
hand-coded lexer, (rec-desc) parserAntlr (generates Python)Yacc, Bison, Lemon (generates C, wrap to Python)Sphinx (C++, wrap to Python)various Python parser generators
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 6 / 14
![Page 29: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/29.jpg)
Compilers The Frontend and The Backend
The Frontend and The Backend
Everything before the first IR is usually called the frontend
And everything after as the backend
LLVM and llvm-py deal only with backends
Frontends tend to be simpler
Many frontends for a backend is common (Scala, Groovy, Clojure:Java; C#, IronPython, etc: .NET)
To build a front end in Python:
hand-coded lexer, (rec-desc) parserAntlr (generates Python)Yacc, Bison, Lemon (generates C, wrap to Python)Sphinx (C++, wrap to Python)various Python parser generators
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 6 / 14
![Page 30: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/30.jpg)
Compilers The Frontend and The Backend
The Frontend and The Backend
Everything before the first IR is usually called the frontend
And everything after as the backend
LLVM and llvm-py deal only with backends
Frontends tend to be simpler
Many frontends for a backend is common (Scala, Groovy, Clojure:Java; C#, IronPython, etc: .NET)
To build a front end in Python:
hand-coded lexer, (rec-desc) parserAntlr (generates Python)Yacc, Bison, Lemon (generates C, wrap to Python)Sphinx (C++, wrap to Python)various Python parser generators
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 6 / 14
![Page 31: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/31.jpg)
Compilers The Frontend and The Backend
The Frontend and The Backend
Everything before the first IR is usually called the frontend
And everything after as the backend
LLVM and llvm-py deal only with backends
Frontends tend to be simpler
Many frontends for a backend is common (Scala, Groovy, Clojure:Java; C#, IronPython, etc: .NET)
To build a front end in Python:
hand-coded lexer, (rec-desc) parser
Antlr (generates Python)Yacc, Bison, Lemon (generates C, wrap to Python)Sphinx (C++, wrap to Python)various Python parser generators
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 6 / 14
![Page 32: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/32.jpg)
Compilers The Frontend and The Backend
The Frontend and The Backend
Everything before the first IR is usually called the frontend
And everything after as the backend
LLVM and llvm-py deal only with backends
Frontends tend to be simpler
Many frontends for a backend is common (Scala, Groovy, Clojure:Java; C#, IronPython, etc: .NET)
To build a front end in Python:
hand-coded lexer, (rec-desc) parserAntlr (generates Python)
Yacc, Bison, Lemon (generates C, wrap to Python)Sphinx (C++, wrap to Python)various Python parser generators
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 6 / 14
![Page 33: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/33.jpg)
Compilers The Frontend and The Backend
The Frontend and The Backend
Everything before the first IR is usually called the frontend
And everything after as the backend
LLVM and llvm-py deal only with backends
Frontends tend to be simpler
Many frontends for a backend is common (Scala, Groovy, Clojure:Java; C#, IronPython, etc: .NET)
To build a front end in Python:
hand-coded lexer, (rec-desc) parserAntlr (generates Python)Yacc, Bison, Lemon (generates C, wrap to Python)
Sphinx (C++, wrap to Python)various Python parser generators
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 6 / 14
![Page 34: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/34.jpg)
Compilers The Frontend and The Backend
The Frontend and The Backend
Everything before the first IR is usually called the frontend
And everything after as the backend
LLVM and llvm-py deal only with backends
Frontends tend to be simpler
Many frontends for a backend is common (Scala, Groovy, Clojure:Java; C#, IronPython, etc: .NET)
To build a front end in Python:
hand-coded lexer, (rec-desc) parserAntlr (generates Python)Yacc, Bison, Lemon (generates C, wrap to Python)Sphinx (C++, wrap to Python)
various Python parser generators
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 6 / 14
![Page 35: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/35.jpg)
Compilers The Frontend and The Backend
The Frontend and The Backend
Everything before the first IR is usually called the frontend
And everything after as the backend
LLVM and llvm-py deal only with backends
Frontends tend to be simpler
Many frontends for a backend is common (Scala, Groovy, Clojure:Java; C#, IronPython, etc: .NET)
To build a front end in Python:
hand-coded lexer, (rec-desc) parserAntlr (generates Python)Yacc, Bison, Lemon (generates C, wrap to Python)Sphinx (C++, wrap to Python)various Python parser generators
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 6 / 14
![Page 36: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/36.jpg)
LLVM What is LLVM?
What is LLVM?
Primarily a set of libraries..
..with which you can make a compiler backend..
..or a VM with JIT support (but hard to get this right)
It provides:
IR data structure, with text and binary representationswide range of optimizationscode generator (partly description-based) for many platformsJIT-compile-execute from IRlink-time optimization (LTO)support for accurate garbage collection
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 7 / 14
![Page 37: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/37.jpg)
LLVM What is LLVM?
What is LLVM?
Primarily a set of libraries..
..with which you can make a compiler backend..
..or a VM with JIT support (but hard to get this right)
It provides:
IR data structure, with text and binary representationswide range of optimizationscode generator (partly description-based) for many platformsJIT-compile-execute from IRlink-time optimization (LTO)support for accurate garbage collection
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 7 / 14
![Page 38: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/38.jpg)
LLVM What is LLVM?
What is LLVM?
Primarily a set of libraries..
..with which you can make a compiler backend..
..or a VM with JIT support (but hard to get this right)
It provides:
IR data structure, with text and binary representationswide range of optimizationscode generator (partly description-based) for many platformsJIT-compile-execute from IRlink-time optimization (LTO)support for accurate garbage collection
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 7 / 14
![Page 39: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/39.jpg)
LLVM What is LLVM?
What is LLVM?
Primarily a set of libraries..
..with which you can make a compiler backend..
..or a VM with JIT support (but hard to get this right)
It provides:
IR data structure, with text and binary representationswide range of optimizationscode generator (partly description-based) for many platformsJIT-compile-execute from IRlink-time optimization (LTO)support for accurate garbage collection
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 7 / 14
![Page 40: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/40.jpg)
LLVM What is LLVM?
What is LLVM?
Primarily a set of libraries..
..with which you can make a compiler backend..
..or a VM with JIT support (but hard to get this right)
It provides:
IR data structure, with text and binary representations
wide range of optimizationscode generator (partly description-based) for many platformsJIT-compile-execute from IRlink-time optimization (LTO)support for accurate garbage collection
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 7 / 14
![Page 41: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/41.jpg)
LLVM What is LLVM?
What is LLVM?
Primarily a set of libraries..
..with which you can make a compiler backend..
..or a VM with JIT support (but hard to get this right)
It provides:
IR data structure, with text and binary representationswide range of optimizations
code generator (partly description-based) for many platformsJIT-compile-execute from IRlink-time optimization (LTO)support for accurate garbage collection
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 7 / 14
![Page 42: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/42.jpg)
LLVM What is LLVM?
What is LLVM?
Primarily a set of libraries..
..with which you can make a compiler backend..
..or a VM with JIT support (but hard to get this right)
It provides:
IR data structure, with text and binary representationswide range of optimizationscode generator (partly description-based) for many platforms
JIT-compile-execute from IRlink-time optimization (LTO)support for accurate garbage collection
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 7 / 14
![Page 43: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/43.jpg)
LLVM What is LLVM?
What is LLVM?
Primarily a set of libraries..
..with which you can make a compiler backend..
..or a VM with JIT support (but hard to get this right)
It provides:
IR data structure, with text and binary representationswide range of optimizationscode generator (partly description-based) for many platformsJIT-compile-execute from IR
link-time optimization (LTO)support for accurate garbage collection
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 7 / 14
![Page 44: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/44.jpg)
LLVM What is LLVM?
What is LLVM?
Primarily a set of libraries..
..with which you can make a compiler backend..
..or a VM with JIT support (but hard to get this right)
It provides:
IR data structure, with text and binary representationswide range of optimizationscode generator (partly description-based) for many platformsJIT-compile-execute from IRlink-time optimization (LTO)
support for accurate garbage collection
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 7 / 14
![Page 45: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/45.jpg)
LLVM What is LLVM?
What is LLVM?
Primarily a set of libraries..
..with which you can make a compiler backend..
..or a VM with JIT support (but hard to get this right)
It provides:
IR data structure, with text and binary representationswide range of optimizationscode generator (partly description-based) for many platformsJIT-compile-execute from IRlink-time optimization (LTO)support for accurate garbage collection
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 7 / 14
![Page 46: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/46.jpg)
LLVM LLVM Highlights
LLVM Highlights
Written in readable C++
Reasonable documentation, helpful and mature community
clang is a now-famous subproject of LLVM
llvm-gcc is gcc frontend (C, C++, Java, Ada, Fortran, ObjC) +LLVM backend
Many users: Unladen Swallow, Iced Tea, Rubinus, llvm-lua, GHC,LDC
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 8 / 14
![Page 47: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/47.jpg)
LLVM LLVM Highlights
LLVM Highlights
Written in readable C++
Reasonable documentation, helpful and mature community
clang is a now-famous subproject of LLVM
llvm-gcc is gcc frontend (C, C++, Java, Ada, Fortran, ObjC) +LLVM backend
Many users: Unladen Swallow, Iced Tea, Rubinus, llvm-lua, GHC,LDC
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 8 / 14
![Page 48: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/48.jpg)
LLVM LLVM Highlights
LLVM Highlights
Written in readable C++
Reasonable documentation, helpful and mature community
clang is a now-famous subproject of LLVM
llvm-gcc is gcc frontend (C, C++, Java, Ada, Fortran, ObjC) +LLVM backend
Many users: Unladen Swallow, Iced Tea, Rubinus, llvm-lua, GHC,LDC
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 8 / 14
![Page 49: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/49.jpg)
LLVM LLVM Highlights
LLVM Highlights
Written in readable C++
Reasonable documentation, helpful and mature community
clang is a now-famous subproject of LLVM
llvm-gcc is gcc frontend (C, C++, Java, Ada, Fortran, ObjC) +LLVM backend
Many users: Unladen Swallow, Iced Tea, Rubinus, llvm-lua, GHC,LDC
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 8 / 14
![Page 50: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/50.jpg)
LLVM LLVM Highlights
LLVM Highlights
Written in readable C++
Reasonable documentation, helpful and mature community
clang is a now-famous subproject of LLVM
llvm-gcc is gcc frontend (C, C++, Java, Ada, Fortran, ObjC) +LLVM backend
Many users: Unladen Swallow, Iced Tea, Rubinus, llvm-lua, GHC,LDC
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 8 / 14
![Page 51: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/51.jpg)
LLVM The LLVM IR
The LLVM IR
The IR looks like this (textual representation):
“Hello world” in LLVM Assembly
@msg = private constant [15 x i8] c"Hello , world !\0A\00"
declare i32 @puts(i8*)
define i32 @main(i32 %argc , i8** %argv) {
entry:
%0 = getelementptr [15 x i8]* @msg , i64 0, i64 0
%1 = tail call i32 @puts(i8* %0)
ret i32 undef
}
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 9 / 14
![Page 52: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/52.jpg)
LLVM A Word About clang
A Word About clang
A sub-project of the LLVM team
clang is a C, Objective C and C++ frontend
works with the LLVM backend (obviously!)
It can convert C/ObjC/C++ code to LLVM IR
which can be played around with using llvm-py
and then compiled “normally”
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 10 / 14
![Page 53: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/53.jpg)
LLVM A Word About clang
A Word About clang
A sub-project of the LLVM team
clang is a C, Objective C and C++ frontend
works with the LLVM backend (obviously!)
It can convert C/ObjC/C++ code to LLVM IR
which can be played around with using llvm-py
and then compiled “normally”
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 10 / 14
![Page 54: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/54.jpg)
LLVM A Word About clang
A Word About clang
A sub-project of the LLVM team
clang is a C, Objective C and C++ frontend
works with the LLVM backend (obviously!)
It can convert C/ObjC/C++ code to LLVM IR
which can be played around with using llvm-py
and then compiled “normally”
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 10 / 14
![Page 55: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/55.jpg)
LLVM A Word About clang
A Word About clang
A sub-project of the LLVM team
clang is a C, Objective C and C++ frontend
works with the LLVM backend (obviously!)
It can convert C/ObjC/C++ code to LLVM IR
which can be played around with using llvm-py
and then compiled “normally”
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 10 / 14
![Page 56: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/56.jpg)
LLVM A Word About clang
A Word About clang
A sub-project of the LLVM team
clang is a C, Objective C and C++ frontend
works with the LLVM backend (obviously!)
It can convert C/ObjC/C++ code to LLVM IR
which can be played around with using llvm-py
and then compiled “normally”
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 10 / 14
![Page 57: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/57.jpg)
LLVM A Word About clang
A Word About clang
A sub-project of the LLVM team
clang is a C, Objective C and C++ frontend
works with the LLVM backend (obviously!)
It can convert C/ObjC/C++ code to LLVM IR
which can be played around with using llvm-py
and then compiled “normally”
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 10 / 14
![Page 58: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/58.jpg)
llvm-py What is llvm-py?
What is llvm-py?
Python bindings for LLVM
Including JIT
Excellent for experimenting and prototyping
Available in Ubuntu, Debian, MacPorts (but may be out of date)
Current version (0.6) works with LLVM 2.7
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 11 / 14
![Page 59: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/59.jpg)
llvm-py What is llvm-py?
What is llvm-py?
Python bindings for LLVM
Including JIT
Excellent for experimenting and prototyping
Available in Ubuntu, Debian, MacPorts (but may be out of date)
Current version (0.6) works with LLVM 2.7
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 11 / 14
![Page 60: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/60.jpg)
llvm-py What is llvm-py?
What is llvm-py?
Python bindings for LLVM
Including JIT
Excellent for experimenting and prototyping
Available in Ubuntu, Debian, MacPorts (but may be out of date)
Current version (0.6) works with LLVM 2.7
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 11 / 14
![Page 61: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/61.jpg)
llvm-py What is llvm-py?
What is llvm-py?
Python bindings for LLVM
Including JIT
Excellent for experimenting and prototyping
Available in Ubuntu, Debian, MacPorts (but may be out of date)
Current version (0.6) works with LLVM 2.7
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 11 / 14
![Page 62: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/62.jpg)
llvm-py What is llvm-py?
What is llvm-py?
Python bindings for LLVM
Including JIT
Excellent for experimenting and prototyping
Available in Ubuntu, Debian, MacPorts (but may be out of date)
Current version (0.6) works with LLVM 2.7
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 11 / 14
![Page 63: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/63.jpg)
llvm-py More on llvm-py
More on llvm-py
Covers most of the LLVM APIs, esp. IR
See examples to get started
User guide at http://www.mdevan.org/userguide.html
Some LLVM knowledge required: Modules, IR instructions, ...
Learning curve of LLVM is steep, llvm-py/Python helps!
Users group: http://groups.google.com/group/llvm-py
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 12 / 14
![Page 64: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/64.jpg)
llvm-py More on llvm-py
More on llvm-py
Covers most of the LLVM APIs, esp. IR
See examples to get started
User guide at http://www.mdevan.org/userguide.html
Some LLVM knowledge required: Modules, IR instructions, ...
Learning curve of LLVM is steep, llvm-py/Python helps!
Users group: http://groups.google.com/group/llvm-py
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 12 / 14
![Page 65: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/65.jpg)
llvm-py More on llvm-py
More on llvm-py
Covers most of the LLVM APIs, esp. IR
See examples to get started
User guide at http://www.mdevan.org/userguide.html
Some LLVM knowledge required: Modules, IR instructions, ...
Learning curve of LLVM is steep, llvm-py/Python helps!
Users group: http://groups.google.com/group/llvm-py
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 12 / 14
![Page 66: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/66.jpg)
llvm-py More on llvm-py
More on llvm-py
Covers most of the LLVM APIs, esp. IR
See examples to get started
User guide at http://www.mdevan.org/userguide.html
Some LLVM knowledge required: Modules, IR instructions, ...
Learning curve of LLVM is steep, llvm-py/Python helps!
Users group: http://groups.google.com/group/llvm-py
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 12 / 14
![Page 67: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/67.jpg)
llvm-py More on llvm-py
More on llvm-py
Covers most of the LLVM APIs, esp. IR
See examples to get started
User guide at http://www.mdevan.org/userguide.html
Some LLVM knowledge required: Modules, IR instructions, ...
Learning curve of LLVM is steep, llvm-py/Python helps!
Users group: http://groups.google.com/group/llvm-py
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 12 / 14
![Page 68: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/68.jpg)
llvm-py More on llvm-py
More on llvm-py
Covers most of the LLVM APIs, esp. IR
See examples to get started
User guide at http://www.mdevan.org/userguide.html
Some LLVM knowledge required: Modules, IR instructions, ...
Learning curve of LLVM is steep, llvm-py/Python helps!
Users group: http://groups.google.com/group/llvm-py
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 12 / 14
![Page 69: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/69.jpg)
llvm-py llvm-py Internals
llvm-py Internals
Wraps LLVM’s C API as a Python extension module
Extends LLVM C API as it is incomplete
Does not use binding generators (Boost.Python, swig etc)
Public APIs are in Python, use extension module internally
Mostly straightforward Python, a little metaclass magic
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 13 / 14
![Page 70: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/70.jpg)
llvm-py llvm-py Internals
llvm-py Internals
Wraps LLVM’s C API as a Python extension module
Extends LLVM C API as it is incomplete
Does not use binding generators (Boost.Python, swig etc)
Public APIs are in Python, use extension module internally
Mostly straightforward Python, a little metaclass magic
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 13 / 14
![Page 71: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/71.jpg)
llvm-py llvm-py Internals
llvm-py Internals
Wraps LLVM’s C API as a Python extension module
Extends LLVM C API as it is incomplete
Does not use binding generators (Boost.Python, swig etc)
Public APIs are in Python, use extension module internally
Mostly straightforward Python, a little metaclass magic
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 13 / 14
![Page 72: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/72.jpg)
llvm-py llvm-py Internals
llvm-py Internals
Wraps LLVM’s C API as a Python extension module
Extends LLVM C API as it is incomplete
Does not use binding generators (Boost.Python, swig etc)
Public APIs are in Python, use extension module internally
Mostly straightforward Python, a little metaclass magic
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 13 / 14
![Page 73: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/73.jpg)
llvm-py llvm-py Internals
llvm-py Internals
Wraps LLVM’s C API as a Python extension module
Extends LLVM C API as it is incomplete
Does not use binding generators (Boost.Python, swig etc)
Public APIs are in Python, use extension module internally
Mostly straightforward Python, a little metaclass magic
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 13 / 14
![Page 74: llvm-py: Writing Compilers In Python](https://reader034.fdocuments.us/reader034/viewer/2022050906/554a2bcab4c9051b578b4de6/html5/thumbnails/74.jpg)
Thanks References and Thanks
Thanks!
Thanks for listening!
Must read: Steven S. Muchnick. Advanced Compiler Design &Implementation.
Me: Mahadevan R, [email protected], http://www.mdevan.org/
Mahadevan R llvm-py: Writing Compilers Using Python September 25, 2010 14 / 14