Top Banner
Python Compiler Internals Thomas Lee Shine Technologies, Melbourne
31

Python Compiler Internals Presentation Slides

Jul 15, 2015

Download

Technology

Tom Lee
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Python Compiler Internals Presentation Slides

Python Compiler Internals

Thomas LeeShine Technologies, Melbourne

Page 2: Python Compiler Internals Presentation Slides

My Contributions to Python

● try/except/finally syntax for 2.5● AST to bytecode compilation from Python● “optimization” at the AST level

Page 3: Python Compiler Internals Presentation Slides

What will you get out of this?

Compiler development != rocket science.No magic: it's just code.

Page 4: Python Compiler Internals Presentation Slides

Compiler? Isn't Python interpreted?

Well, yes and no.

Page 5: Python Compiler Internals Presentation Slides

WTF

It ... It's Java?

Page 6: Python Compiler Internals Presentation Slides

Aaanyway

Let's screw with the compiler.

Page 7: Python Compiler Internals Presentation Slides

Erm ... Before we start ...

A patch to fix a quirk in the build process.

This has been reported on the bug tracker!

Page 8: Python Compiler Internals Presentation Slides

An overview of the compiler

Tokenizer Parser

AST Translation

Tokens

Parse Tree

Bytecode Generation Bytecode Optimization

Execution

Symtable ConstructionAST

Bytecode+ Data

AST + Symtable

Page 9: Python Compiler Internals Presentation Slides

New construct:the “unless” statement

unless may_give_you_up: print “never gonna give you up...”

Page 10: Python Compiler Internals Presentation Slides

Semantics of “unless”

Works just like “if not” ...

if not would_consider_letting_you_down: print “never gonna let you down...”

Page 11: Python Compiler Internals Presentation Slides

Implementing “unless”

1. Modify the Grammar2. Change the AST definition3. Generate bytecode for “unless”

Page 12: Python Compiler Internals Presentation Slides

So what's first?

1. Modify the Grammar2. Change the AST definition3. Generate bytecode for “unless”

Page 13: Python Compiler Internals Presentation Slides

What does this change affect?

Tokenizer Parser

AST Translation

Tokens

Parse Tree

Bytecode Generation Bytecode Optimization

Execution

Symtable ConstructionAST

Bytecode+ Data

AST + Symtable

Page 14: Python Compiler Internals Presentation Slides

WTF is a “grammar”?

Defines the syntactic structure of the language.

Page 15: Python Compiler Internals Presentation Slides

Why a tokenizer?

Makes parsing easier.

Page 16: Python Compiler Internals Presentation Slides

So what does a tokenizer do, then?

Generates a stream of events for the parser.

Page 17: Python Compiler Internals Presentation Slides

What does the parser do?

Organises tokens into the structure defined by the grammar.This is the “parse tree”.

Page 18: Python Compiler Internals Presentation Slides

What's next?

1. Modify the Grammar2. Change the AST definition3. Generate bytecode for “unless”

Page 19: Python Compiler Internals Presentation Slides

What's affected by this change?

Tokenizer Parser

AST Translation

Tokens

Parse Tree

Bytecode Generation Bytecode Optimization

Execution

Symtable ConstructionAST

Bytecode+ Data

AST + Symtable

Page 20: Python Compiler Internals Presentation Slides

What's an AST?

Similar to a parse tree, but not bound to the syntax

“Abstract Syntax Tree”

Page 21: Python Compiler Internals Presentation Slides

Why does Python use an AST?

Much easier to operate upon an AST than a parse tree.

Page 22: Python Compiler Internals Presentation Slides

AST reuse

New constructs can sometimes be expressedusing existing AST nodes ...

Page 23: Python Compiler Internals Presentation Slides

AST reuse

... so we could implement “unless” using “if” and “not” AST nodes.

But then I wouldn't be able to show you the code generator ...

Unless(test, body) = If(Not(test), body)

Page 24: Python Compiler Internals Presentation Slides

What's next?

1. Modify the Grammar2. Change the AST definition3. Generate bytecode for “unless”

Page 25: Python Compiler Internals Presentation Slides

What's affected by this change?

Tokenizer Parser

AST Translation

Tokens

Parse Tree

Bytecode Generation Bytecode Optimization

Execution

Symtable ConstructionAST

Bytecode+ Data

AST + Symtable

Page 26: Python Compiler Internals Presentation Slides

Generating bytecode for “unless”

● Add a hook for our new AST node● Generate bytecode implementing “unless” logic● Use basicblocks as labels for jumps

Page 27: Python Compiler Internals Presentation Slides

What about the rest of the compiler?

Tokenizer Parser

AST Translation

Tokens

Parse Tree

Bytecode Generation Bytecode Optimization

Execution

Symtable ConstructionAST

Bytecode+ Data

AST + Symtable

Page 28: Python Compiler Internals Presentation Slides

“unless” in action

Let's see if this stuff works ...

Page 29: Python Compiler Internals Presentation Slides

Where to go from here?

● Use the Source.● Don't try to grok it all at once.● Don't be afraid to be wrong.● Annoy the python-dev mailing list, like I do!

Page 30: Python Compiler Internals Presentation Slides

Thanks!

Page 31: Python Compiler Internals Presentation Slides

Questions?