|
| 1 | +class: center, middle |
| 2 | +##Python Interpreter in Rust |
| 3 | +###Introduction |
| 4 | +#### 2017/3/28 |
| 5 | +#### Shing Lyu |
| 6 | + |
| 7 | + |
| 8 | +??? |
| 9 | +top, middle, bottom |
| 10 | +left, center, right |
| 11 | + |
| 12 | +--- |
| 13 | +name: toc |
| 14 | +###Agenda |
| 15 | +1. Category |
| 16 | +1. Category |
| 17 | +1. Category |
| 18 | +1. Category |
| 19 | +1. Category |
| 20 | +1. Category |
| 21 | +1. Category |
| 22 | + |
| 23 | +??? |
| 24 | +This is a template |
| 25 | +--- |
| 26 | + |
| 27 | +### Python's architecture |
| 28 | +* Interpreted |
| 29 | +* Garbage Collected |
| 30 | +* Compiler => bytecode => VM |
| 31 | + |
| 32 | +--- |
| 33 | +background-image: url('pic/ice-cream.jpg') |
| 34 | +class: bleed |
| 35 | +# Flavors |
| 36 | + |
| 37 | + |
| 38 | +--- |
| 39 | +### Python Flavors |
| 40 | +* CPython (THE python) |
| 41 | +* Jython (JVM) |
| 42 | +* IronPython (.NET) |
| 43 | +* Pypy |
| 44 | +* Educational |
| 45 | + * Byterun |
| 46 | + * Jsapy (JS) |
| 47 | + * Brython (Python in browser) |
| 48 | + |
| 49 | +--- |
| 50 | +### Why rewriting Python in Rust? |
| 51 | +* Memory safety |
| 52 | + |
| 53 | +* Learn about Python internal |
| 54 | +* Learn real world Rust |
| 55 | + |
| 56 | +--- |
| 57 | +### Implementation strategy |
| 58 | +* Focus on the VM first, then the compiler |
| 59 | + * Reuse the Python built-in compiler to generate bytecode |
| 60 | +* Basic arithmetics |
| 61 | +* Control flows (require JUMP) |
| 62 | +* Function call (require call stack) |
| 63 | +* Built-in functions (require native code) |
| 64 | +* Run popular libraries |
| 65 | + |
| 66 | + |
| 67 | +--- |
| 68 | +### References |
| 69 | +* [`dis` documentation](https://docs.python.org/3.4/library/dis.html) |
| 70 | +* [byterun](http://www.aosabook.org/en/500L/a-python-interpreter-written-in-python.html) |
| 71 | +* [byterun (GitHub)](https://github.com/nedbat/byterun/) |
| 72 | +* [cpython source code](https://github.com/python/cpython) |
| 73 | + |
| 74 | +--- |
| 75 | +### How Python VM works |
| 76 | +* Stack machine |
| 77 | +* Accepts Python bytecode |
| 78 | +* `python -m dis source.py` |
| 79 | + |
| 80 | +--- |
| 81 | + |
| 82 | +### A simple Python code |
| 83 | + |
| 84 | +``` |
| 85 | +#!/usr/bin/env python3 |
| 86 | +print(1+1) |
| 87 | +``` |
| 88 | + |
| 89 | +We run `python3 -m dis source.py` |
| 90 | + |
| 91 | +--- |
| 92 | + |
| 93 | +### The bytecode |
| 94 | + |
| 95 | +``` |
| 96 | + 1 0 LOAD_NAME 0 (print) |
| 97 | + 3 LOAD_CONST 2 (2) |
| 98 | + 6 CALL_FUNCTION 1 (1 positional, 0 keyword pair) |
| 99 | + 9 POP_TOP |
| 100 | + 10 LOAD_CONST 1 (None) |
| 101 | + 13 RETURN_VALUE |
| 102 | +
|
| 103 | +``` |
| 104 | + |
| 105 | +--- |
| 106 | + |
| 107 | +### LOAD_NAME "print" |
| 108 | +* NAMES = ["print"] |
| 109 | +* CONSTS = [None, 2] |
| 110 | +* STACK: |
| 111 | + |
| 112 | +``` |
| 113 | + | | |
| 114 | + | print (native code)| |
| 115 | + +--------------------+ |
| 116 | +``` |
| 117 | +--- |
| 118 | +### LOAD_CONST 2 |
| 119 | +* NAMES = ["print"] |
| 120 | +* CONSTS = [None, 2] |
| 121 | +* STACK: |
| 122 | + |
| 123 | +``` |
| 124 | + | | |
| 125 | + | 2 | |
| 126 | + | print (native code)| |
| 127 | + +--------------------+ |
| 128 | +``` |
| 129 | + |
| 130 | +--- |
| 131 | + |
| 132 | +### CALL_FUNCTION 1 |
| 133 | +1. argument = stack.pop() (argument == 2) |
| 134 | +2. function = stack.top() (function == print) |
| 135 | +3. call print(2) |
| 136 | + |
| 137 | +* NAMES = ["print"] |
| 138 | +* CONSTS = [None, 2] |
| 139 | +* STACK: |
| 140 | + |
| 141 | +``` |
| 142 | + | | |
| 143 | + | print (native code)| |
| 144 | + +--------------------+ |
| 145 | +``` |
| 146 | +--- |
| 147 | +### POP_TOP |
| 148 | +* NAMES = ["print"] |
| 149 | +* CONSTS = [None, 2] |
| 150 | +* STACK: |
| 151 | + |
| 152 | +``` |
| 153 | + | | |
| 154 | + | (empty) | |
| 155 | + +--------------------+ |
| 156 | +``` |
| 157 | + |
| 158 | +--- |
| 159 | +### LOAD_CONST 1 |
| 160 | +* NAMES = ["print"] |
| 161 | +* CONSTS = [None, 2] |
| 162 | +* STACK: |
| 163 | + |
| 164 | +``` |
| 165 | + | | |
| 166 | + | None | |
| 167 | + +--------------------+ |
| 168 | +``` |
| 169 | + |
| 170 | +--- |
| 171 | +### RETURN_VALUE |
| 172 | + |
| 173 | +(returns top of stack == None) |
| 174 | + |
| 175 | +--- |
| 176 | + |
| 177 | +### Next step |
| 178 | +* Make it run a small but popular tool/library |
| 179 | +* Implement the parser |
| 180 | +* Performance benchmarking |
0 commit comments