eric2013 发表于 2023-8-15 03:00:57

LPython:新颖、快速、可重定向的 Python 编译器

https://github.com/lcompilers/lpython
https://lpython.org/blog/2023/07/lpython-novel-fast-retargetable-python-compiler/

LPython是一个Python编译器,可以将type annotated 动态类型的Python代码编译为优化的机器代码。LPython提供了几个后端,如LLVM,C,C++,WASM,Julia和x86。LPython 具有快速编译和运行时性能。LPython还提供即时(JIT)编译以及与CPython的无缝互操作性。

https://img.anfulai.cn/dz/attachment/forum/202308/14/175057acrstcrfm33cfgcc.png

测试代码:

from lpython import i32

def main():
    x: i32
    x = (2+3)*5
    print(x)

main()

生成C和LLVM
$ lpython examples/expr2.py --show-c
#include <inttypes.h>

#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <lfortran_intrinsics.h>

void main0();
void __main____global_statements();

// Implementations
void main0()
{
    int32_t x;
    x = (2 + 3)*5;
    printf("%d\n", x);
}

void __main____global_statements()
{
    main0();
}

int main(int argc, char* argv[])
{
    _lpython_set_argv(argc, argv);
    __main____global_statements();
    return 0;

$ lpython examples/expr2.py --show-llvm
; ModuleID = 'LFortran'
source_filename = "LFortran"

@0 = private unnamed_addr constant c" \00", align 1
@1 = private unnamed_addr constant c"\0A\00", align 1
@2 = private unnamed_addr constant c"%d%s\00", align 1

define void @__module___main_____main____global_statements() {
.entry:
call void @__module___main___main0()
br label %return

return:                                           ; preds = %.entry
ret void
}

define void @__module___main___main0() {
.entry:
%x = alloca i32, align 4
store i32 25, i32* %x, align 4
%0 = load i32, i32* %x, align 4
call void (i8*, ...) @_lfortran_printf(i8* getelementptr inbounds (, * @2, i32 0, i32 0), i32 %0, i8* getelementptr inbounds (, * @1, i32 0, i32 0))
br label %return

return:                                           ; preds = %.entry
ret void
}

declare void @_lfortran_printf(i8*, ...)

define i32 @main(i32 %0, i8** %1) {
.entry:
call void @_lpython_set_argv(i32 %0, i8** %1)
call void @__module___main_____main____global_statements()
ret i32 0
}

declare void @_lpython_set_argv(i32, i8**)



页: [1]
查看完整版本: LPython:新颖、快速、可重定向的 Python 编译器