_____________ ____________________________.___. / _____/ | \______ \______ \__ | | \_____ \| | /| | _/| ___// | | / \ | / | | \| | \____ | /_______ /______/ |______ /|____| / ______| \/ \/ \/
a subset of python, vm implemented with ANSI C and compiler implemented with Python 3.
Python子集,虚拟机部分使用C语言实现,编译器使用Python3实现(开始是用Python2写的,应该还能兼容,没有测试)。
run python setup.py gcc
in console(or you may use other c compiler)
在控制台(终端)运行python setup.py gcc
(你也可以用其他C编译器,我喜欢TCC,小而且快速)
tokenize.py
tokenizer modified from tinypy/tokenizeparse.py
handwrite LL parserencode.py
,asm.py
code generator
- stack based
- bytecode defined in
python/libs/tmcode.py
- exception handling implemented with setjmp
- C function and python function.
- exception can be tracked by lineno and handled.
vm.c
entryinterp.c
interpreterbuiltins.c
built-in functionsbuiltins2.c
built-in functions for developers.
- string, strings are immutable.
- number, use
double
as default number. - list, array list increase with simple strategy.
- dict, this is not hashtable, for the optimization of global access, because the offset of a variable in
globals
dict never change.Besides, object are implemented in dict. - function, C functions, python functions and methods.
- None, NoneType object.
- repl.py repl mode
- dis.py dissamble bytecode.
- libs/tm2c.py convert Python source code to C source code, not done yet.
- repl.py 交互式模式,直接运行./tm.exe进入交互模式
- dis.py 反编译
- libs/tm2c.py 转换TMPython的源代码到C语言源代码,部分完成。
位于subpy/python/libs/tm2c.py
, 例子在subpy/python/test/tm2c/
目录下面,目前完成的部分比较少,有了大体思路
使用方法
- 进入
subpy/python
目录,编译python setup.py gcc
- 运行例子
tm.exe libs/tm2c.py testspeed.py
,结果会输出到控制台,可以重定向到文件tm libs/tm2c.py testspeed.py > ../testspeed.c
- 运行
gcc ../testspeed.c -o testspeed.exe
- 运行
testspeed.exe
- 易于实现,不用分配寄存器
- 占用的字节码长度小一些
- 更容易反编译,所以在字节码级别优化的程度可以更大(优点&缺点)
- 需要分配寄存器,稍微复杂一点,不过使用虚拟寄存器,难度不大
- 一条指令是相对完整的语句,不会有操作栈,GC的时候不用标记操作栈
- 解释执行更快,因为没有大量的栈操作
- 指令更少
That's all.