硬汉嵌入式论坛

 找回密码
 立即注册
查看: 781|回复: 0
收起左侧

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

[复制链接]

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
106660
QQ
发表于 2023-8-15 03:00:57 | 显示全部楼层 |阅读模式
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的无缝互操作性。



测试代码:

[C] 纯文本查看 复制代码
from lpython import i32
 
def main():
    x: i32
    x = (2+3)*5
    print(x)
 
main()


生成C和LLVM
[C] 纯文本查看 复制代码
$ 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;


[C] 纯文本查看 复制代码
$ lpython examples/expr2.py --show-llvm
; ModuleID = 'LFortran'
source_filename = "LFortran"
 
@0 = private unnamed_addr constant [2 x i8] c" \00", align 1
@1 = private unnamed_addr constant [2 x i8] c"\0A\00", align 1
@2 = private unnamed_addr constant [5 x i8] 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 ([5 x i8], [5 x i8]* @2, i32 0, i32 0), i32 %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @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**)




回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2024-4-29 04:55 , Processed in 0.144162 second(s), 25 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表