WO2022099888A1 - 一种实现编译型嵌入式Python的方法 - Google Patents

一种实现编译型嵌入式Python的方法 Download PDF

Info

Publication number
WO2022099888A1
WO2022099888A1 PCT/CN2020/140482 CN2020140482W WO2022099888A1 WO 2022099888 A1 WO2022099888 A1 WO 2022099888A1 CN 2020140482 W CN2020140482 W CN 2020140482W WO 2022099888 A1 WO2022099888 A1 WO 2022099888A1
Authority
WO
WIPO (PCT)
Prior art keywords
python
function
type
statement
code
Prior art date
Application number
PCT/CN2020/140482
Other languages
English (en)
French (fr)
Inventor
王宜怀
许明宇
帅辉明
余文森
蔡闯华
Original Assignee
苏州大学
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by 苏州大学 filed Critical 苏州大学
Priority to US17/786,502 priority Critical patent/US11740881B2/en
Publication of WO2022099888A1 publication Critical patent/WO2022099888A1/zh

Links

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/30Creation or generation of source code
    • G06F8/31Programming languages or programming paradigms
    • G06F8/311Functional or applicative languages; Rewrite languages
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation
    • G06F8/42Syntactic analysis
    • G06F8/427Parsing
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation
    • G06F8/43Checking; Contextual analysis
    • G06F8/436Semantic checking
    • G06F8/437Type checking
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/51Source to source

Definitions

  • the invention relates to the technical field of computers, in particular to a method for realizing compiled embedded Python.
  • MicroPython is an ultra-small Python interpreter that can run on Windows systems, Unix systems and some microcontrollers. It compresses the huge Python interpreter to less than 256KB and supports most Python3 functions. Python language has better readability and reliability than C language, and using MicroPython can effectively improve programming efficiency. The Python interpreter occupies larger Flash space and more computing resources, and is not suitable for developing embedded applications with low resource occupation and high real-time requirements.
  • Cython and PyPy have proposed two tools to improve the efficiency of Python, but they are only applicable to the field of desktop system programming and cannot be applied to microcontrollers.
  • the method provided by Cython is to use Python-like syntax to write or refer to static functions for the Python interpreter to call, which is essentially interpreter-based and therefore not suitable for microcontroller programming.
  • PyPy provides Python's Just In Time (JIT) technology, which translates the functions that need to be used into native code at runtime to improve running efficiency, but the running time of JIT itself will occupy the overall running time, and at the same time There is also a huge overhead in memory, so the JIT approach is also not suitable for microcontrollers.
  • JIT Just In Time
  • the purpose of the embodiments of the present invention is to provide a method for implementing compiled embedded Python.
  • the method for realizing compiled embedded Python provided by the embodiment of the present invention realizes a source code translator based on type annotation and static analysis, and integrates the translator into the embedded platform, so as to realize the editing, compiling, linking and programming of Python source files. .
  • a method for realizing compiled embedded Python comprising step S1: traversing the abstract syntax tree of the Python source code to obtain the semantic information of the program and according to the semantic information Generate the corresponding C++ code; Step S2: Type annotation on the Python source code to generate C++ variable definitions and function definitions; Step S3: Use a translator to translate the Python source code processed in Step S1 and Step S2 into C++ source code file; Step S4: Store the C++ source file and the files related to the embedded chip together to form a file package, compile and link the file package, and generate an ASCII text file.
  • the files related to the embedded chip include header files, startup files and link files.
  • the type annotation includes variable annotation and function annotation
  • the variable annotation includes the steps of: assigning a value to a single variable in the Python source code while giving the type of the variable, thereby generating a corresponding C++ variable definition
  • the function annotation includes the steps: while defining the function in the Python source code, the type of the parameter in the function and the type of the return value of the function are given, thereby generating a corresponding C++ function definition.
  • the design process of the translator includes step S31: defining the structure of the translation content, the structure includes a header file containing area, a namespace definition area and a source code area; step S32: designing the type of Python to C++ The mapping relationship of the type; Step S33: adopt the bottom-up method to perform semantic analysis on the abstract syntax tree and generate C++ code, and splicing the generated C++ code according to the structure of the translation content.
  • step S33 includes: step S331: using bottom-up sub-node access to perform expression translation; step S332: translating the statement or/and each line of program code in the statement list into C++ code statement.
  • step S332 in the translation process of step S332, it is necessary to first distinguish the statement into a function definition statement and a non-function definition statement, and fill in the C++ code statement corresponding to the function definition statement. In other scope statement areas, the C++ code statement translated into the non-function definition statement corresponding to the statement is filled into the function body of the scope where it is located.
  • the abstract syntax tree is a list of node objects, each object includes a plurality of sublists or references to other objects, and each list or sublist includes any number of node objects.
  • the process of obtaining the abstract syntax tree of the Python source code comprises the steps of: calling the parse() function of the abstract syntax tree module, and using the dump() function to obtain the abstract syntax tree of the Python source code obtained by the parse() function into natural language form.
  • the Python method for realizing compiled embedded provided by the embodiment of the present invention realizes a source code translator based on type annotation and static analysis, integrates the translator into the embedded platform, and realizes editing, compiling, linking and burning of Python source files. It breaks through the defect that the existing technology cannot apply Python to the field of embedded platforms in real time.
  • FIG. 1 is a schematic flowchart of a method for implementing a compiled embedded Python provided by an embodiment of the present invention
  • FIG. 2 is a schematic flowchart of another expression in the embodiment shown in FIG. 1;
  • FIG. 3 is a schematic diagram of a successful compilation interface in E-Python-AHL-GEC-IDE using the Python method for realizing compiled embedded provided by an embodiment of the present invention
  • Fig. 4 is the interface schematic diagram that the serial port in the embodiment shown in Fig. 3 receives the light-dark change information
  • FIG. 5 is an actual illustration of the small lamp lighting up in the embodiment shown in FIG. 3 .
  • FIG. 1 and FIG. 2 it is a schematic flowchart of a method for implementing a compiled embedded Python provided by an embodiment of the present invention.
  • the implementation of the compiled embedded Python method includes four steps, and the specific content of each step is as follows.
  • Step S1 Traverse the abstract syntax tree of the Python source code to obtain the semantic information of the program and generate corresponding C++ code according to the semantic information.
  • AST Abstract Syntax Tree
  • Python's AST can be represented as a list of node objects, each object containing multiple sublists or references to other objects, each list or sublist containing any number of node objects. Python's AST is obtained by calling the parse() function of the abstract syntax tree (ast) module, and can be converted to natural language form using the dump() function. Table 1 shows the abstract syntax tree of a Python function and its calls.
  • the root node Module represents the whole code
  • the list body contains a function definition FunctionDef and an expression statement Expr.
  • the function definition node has the function name name, the parameter list args, the function body sublist body and the return value annotation returns.
  • the expression statement includes a function call node Call, the function call node has the function name func and the parameter list args.
  • the compiled embedded Python obtains the semantic information of the program by traversing the AST of the Python source code, and generates the corresponding C++ code according to its semantics.
  • the main processing syntax tree nodes and the content they represent are shown in Table 2.
  • Step S2 Type annotation is performed on the Python source code, thereby generating C++ variable definitions and function definitions.
  • Type Hints (also known as type hints or type annotations) is a grammar added after Python 3.5. It writes the type information of variables or functions into the source code, so that the type information can be directly read out during the semantic analysis stage. No more complicated derivations. This feature is designed to open up Python code to simplify static analysis of programs, run-time type checking, and generating other code based on type information.
  • variable annotations There are two types of type annotations: variable annotations and function annotations.
  • a variable comment is to give the type of the variable while assigning a value to a single variable, and this type of assignment statement is called a labeled assignment statement.
  • a function annotation is when the function definition gives the type of the parameters and the type of the return value. Table 3 gives examples of type annotations, one is a variable annotation for a variable x of type int, and the other is a function annotation for a function func whose return value is int and whose parameter is an int.
  • Compiled embedded Python utilizes type annotations to generate C++ variable definitions and function definitions. For variables, it is required that the first occurrence must be a variable comment, so that the corresponding C++ variable definition statement can be generated. For functions, function annotations are required to generate function definitions and declarations with parameter and return value types corresponding to the content of the annotation.
  • V is the annotated assignment statement syntax used in the definition of the variable va
  • F is the definition syntax of the function func
  • stmt_list is the function body
  • type and B define the types that can be used.
  • Step S3 using a translator to translate the Python source code processed in steps S1 and S2 into C++ source files.
  • the translator design process consists of three steps, as follows.
  • Step S31 Define the structure of the translation content, where the structure includes a header file containing area, a namespace definition area and a source code area. Extracting commonalities between the execution process of Python code and the execution process of C++, the structure of the translation content can be divided into three parts: header file inclusion area, namespace definition and source code area, as shown in Table 4. "builtin.h" must be included in the include area of the header file, which declares the C++ implementation of Python's built-in functions. Namespaces are used to emulate Python modules.
  • the source code area must contain the initialization function ⁇ Module>_init() of the current module, which is used to store the code translation results in the global scope in the Python source file.
  • Step S32 Design the mapping relationship between Python types and C++ types.
  • Python's types are divided into two types: basic types and composite types.
  • Basic types include integers, floating-point types, strings, booleans, and byte arrays.
  • Composite types include list types, tuple types, collection types, and dictionary types. .
  • T ⁇ int,float,str,bytes,bool ⁇
  • the mapping relationship between Python types and C/C++ be f, and the mapping relationship is shown in Table 5, where x, y ⁇ T.
  • Step S33 using a bottom-up method to perform semantic analysis on the abstract syntax tree and generate C++ code, and splicing the generated C++ code according to the structure of the translation content.
  • the translation process is the process of semantically analyzing the syntax tree from the bottom up and generating C++ code.
  • the generated code is finally spliced according to the content structure of the translation file.
  • the node types given in Table 2 it can be divided into two processes: the translation of expression nodes and the translation of statement nodes. This step specifically includes two steps, and the specific content is as follows.
  • Step S331 Perform expression translation in a bottom-up manner of accessing child nodes.
  • Expression translation is the basis of statement translation. It translates a Python expression that constitutes a certain statement into a C++ expression and returns it to the statement translation process. Table 1 has described the types of Python expression nodes. The formal description of an expression expr is shown below.
  • Step S332 Translate the statement or/and each line of program code in the statement list into a C++ code statement.
  • a statement is an ordered arrangement of keywords and expressions, and is used to represent the operations performed by the program. Therefore, the statement translation process is a recombination of expressions and keywords according to the content of the syntax tree nodes.
  • Commonly used statements in Python include assignment statement, program structure control statement, function definition statement, module import statement and expression statement. Let the single-sentence statement of Python be stmt, the statement block be stmt_list, and the carriage return and linefeed character as the formal description of the statement and the statement list as follows.
  • the embodiments of the present invention mainly describe the translation process of flow control class statements and function definition statements.
  • the program structure of Python is divided into three categories: sequence structure, selection structure and loop structure.
  • a sequential structure executes two line statements in top-to-bottom order.
  • the selection structure makes the program execute the branch program according to a certain condition, which is the if statement in Python, and the switch statement is less than that in C++.
  • the loop structure makes the program execute a piece of code repeatedly within a certain condition.
  • Python there are two kinds of loops, while and for. Here's how selection structures and looping structures are translated from Python syntax to C++ syntax.
  • the if statement consists of three parts: the branch condition, the if body code block and the else code block.
  • the branch condition is an expression
  • the body code block and else code block are both statement lists.
  • the while statement consists of a loop control condition and a loop body code block.
  • the loop control condition is an expression
  • the loop body code block is a list of statements.
  • the for statement consists of a loop variable, a sequence object, and a loop body code block.
  • Loop variables and sequence objects are expressions, and the body of the loop is a list of statements.
  • This type of loop implements the loop from the first element of the sequence object seq to the last element.
  • Each loop uses the loop variable iter to represent the current element, which is similar to the iterator in C++. Since the sequence type is represented by the STL container type after translation, the for statement is also the iterator syntax translated into C++.
  • the translation process function visit_for can be described as:
  • the function definition is composed of the function name func, the parameter list args, the return value annotation returns and the function content body.
  • Embedded Python requires that both parameters and return values must be typed with annotations, so the function definition can be described as:
  • arg1:typea1 ⁇ argn:typean is the parameter list
  • typer1 ⁇ typerm is the return value annotation
  • the translation function definition needs to use the first return value type as the function type, and insert the reference type corresponding to the subsequent return value type into the parameter list.
  • the return value type is tr1 ⁇ trn
  • f.stmt_list is the statement block in the function
  • the translation function visit_func defined by the function can be described as:
  • Step S4 Store the C++ source file and the files related to the embedded chip together to form a file package, compile and link the file package, and generate an ASCII text file.
  • the Python method for realizing compiled embedded provided by the embodiment of the present invention realizes a source code translator based on type annotation and static analysis, integrates the translator into the embedded platform, and realizes editing, compiling, linking and burning of Python source files. It breaks through the defect that the existing technology cannot apply Python to the field of embedded platforms in real time.
  • Python Since Python does not have the function of pointer operation and cannot directly access memory by address, Python implements IO operations with the help of modules encapsulated in C or other languages. IO operation is the basis of embedded applications, so it is necessary to design a mixed programming method with C/C++, so as to realize the IO operation of embedded Python.
  • the implementation of the function needs to add the "Py_" prefix to the function name and parameter name, and add the first return value as the function type, and add the reference type of the second and subsequent return values to the parameter list.
  • Table 5 describes the content of the source file and Module.hpp, the ampersand represents the AND operation of the grammar, and includes is the list of header file statements that the functions and variables defined in other files used in the mix need to be included.
  • the above-mentioned ASCII text file is also applied to the embedded platform to conduct experiments.
  • the embedded platform is selected as STM32L431RC as the hardware platform, and the operating system API of RT-Thread is provided to the embedded Python through the mixed programming method, and the flashing of three small lights is realized in the real-time operating system environment.
  • the STM32L431 is a 32-bit reduced instruction set ultra-low-power microcontroller based on a high-performance Cortex-M4 core, operating at up to 80 MHz. It has 256KB of Flash and 64KB of RAM, and provides a low-power RTC, a general-purpose 32-bit timer, a 16-bit PWM timer dedicated to motor control, four general-purpose 16-bit timers, and two 16-bit timers Low power timer.
  • the function implemented in the experiment is that in the RT-Thread system environment, the main thread creates three threads of red light, green light and blue light, the three threads control the color of the three-color light, and send the light and dark status of the small light to the PC through UART.
  • the experiment uses E-Python-AHL-GEC-IDE, which is based on the embedded development integrated development environment AHL-GEC-IDE jointly produced by Soochow University and ARM Company, as the development environment, and writes the program and burns it to the target board, as shown in Figure 3 shown.
  • E-Python-AHL-GEC-IDE which is based on the embedded development integrated development environment AHL-GEC-IDE jointly produced by Soochow University and ARM Company, as the development environment, and writes the program and burns it to the target board, as shown in Figure 3 shown.
  • After the compilation is successful use the serial port to update and program it into the STM32 to run. You can see that the three threads are executed alternately, the small lights change light and dark alternately, and the light and dark state change information is sent to the upper computer, as shown in Figure 4. The small light is on as shown in Figure 5.

Landscapes

  • Engineering & Computer Science (AREA)
  • General Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Software Systems (AREA)
  • Physics & Mathematics (AREA)
  • General Physics & Mathematics (AREA)
  • Computing Systems (AREA)
  • Computational Linguistics (AREA)
  • Devices For Executing Special Programs (AREA)

Abstract

一种实现编译型嵌入式Python的方法。该方法包括遍历Python源码的抽象语法树而获取程序的语义信息并根据所述语义信息生成对应的C++代码(S1);对Python源码的进行类型注释,从而生成C++的变量定义和函数定义(S2);采用翻译器对经过上述步骤处理过的Python源代码翻译成C++源文件(S3);将所述C++源文件与嵌入式芯片相关的文件存储在一起而形成文件包,对所述文件包进行编译和链接并生成ASCII文本文件(S4)。该方法基于类型注释和静态分析实现源码翻译器,并将翻译器集成至嵌入式平台中,实现了Python源文件的编辑、编译、链接和烧写。

Description

一种实现编译型嵌入式Python的方法 技术领域
本发明涉及计算机的技术领域,特别是涉及一种实现编译型嵌入式Python的方法。
背景技术
MicroPython是一款能够运行于Windows系统、Unix系统和部分微控制器上的超小型Python解释器,它把庞大的Python解释器压缩到256KB以内并且能够支持大部分的Python3功能。Python语言有着比C语言更优秀的可读性和可靠性,使用MicroPython能够有效提高编程效率。Python解释器占用较大的Flash空间和更多的计算资源,不适合用于开发有着低资源占用和高实时性要求的嵌入式应用。
目前,Cython和PyPy分别提出两种提高Python运行效率的工具,但是都只适用桌面系统编程领域,无法适用于微控制器。Cython提供的方法是用类Python语法编写或引用静态函数供Python解释器调用,本质上还是基于解释器因此不适合用于微控制器编程。PyPy提供了Python的即时编译技术(Just In Time,简称JIT),它在运行时会先把需要使用的函数翻译成本机代码从而提高运行效率,但是JIT本身的运行时间会占用总体运行时间,同时还会造成内存的巨大开销,因此JIT方法也不适合用于微控制器。
因此,针对上述技术问题,有必要提供一种能够满足实时性、可应用于微控制器的实现编译型嵌入式的Python方法。
技术解决方案
有鉴于此,本发明实施例的目的在于提供一种实现编译型嵌入式Python的方法。本发明实施例提供的实现编译型嵌入式Python的方法基于类型注释和静态分析实现源码翻译器,并将翻译器集成至嵌入式平台中,实现了Python源文件的编辑、编译、链接和烧写。
为了实现上述目的,本发明一实施例提供的技术方案如下:一种实现编译型嵌入式Python的方法,包括步骤S1:遍历Python源码的抽象语法树而获取程序的语义信息并根据所述语义信息生成对应的C++代码;步骤S2:对Python源码的进行类型注释,从而生成C++的变量定义和函数定义;步骤S3:采用翻译器对经过步骤S1和步骤S2处理过的Python源代码翻译成C++源文件;步骤S4:将所述C++源文件与嵌入式芯片相关的文件存储在一起而形成文件包,对所述文件包进行编译和链接并生成ASCII文本文件。
作为本发明的进一步改进,所述嵌入式芯片相关的文件包括头文件、启动文件和链接文件。
作为本发明的进一步改进,所述类型注释包括变量注释和函数注释,所述变量注释包括步骤:对Python源码中的单个变量赋值的同时给出所述变量的类型,从而生成对应的C++变量定义;所述函数注释包括步骤:对Python源码中的函数进行定义的同时给出所述函数中参数的类型和所述函数的返回值的类型,从而生成对应的C++函数定义。
作为本发明的进一步改进,所述翻译器的设计过程包括步骤S31:定义翻译内容的结构,所述结构包括头文件包含区、命名空间定义区和源码区;步骤S32:设计Python的类型至C++的类型的映射关系;步骤S33:采用自底向上的方法对抽象语法树进行语义分析并生成C++代码,将生成的C++代码按照所述翻译内容的结构进行拼接。
作为本发明的进一步改进,所述映射关系如下表所示,其中x,y∈T,T={int,float,str,bytes,bool}
 注释的类型  翻译后的类型
 int  int
 float  float
 str  string
 bytes  vector<char>
 bool  bool
 List[x]   (列表类型)  vector<f(x)>
 Tuple[x]  (元组类型)  vector<f(x)>
 Set[x]    (集合类型)  set<f(x)>
 Dict[x,y] (字典类型)  map<f(x),f(y)>
 作为本发明的进一步改进,所述步骤S33包括:步骤S331:采用自底向上的子节点访问的方式进行表达式翻译;步骤S332:将语句或者/和语句列表中的每行程序代码翻译成C++代码语句。
作为本发明的进一步改进,所述步骤S332的翻译过程中,需要先将所述语句区分为函数定义语句和非函数定义语句,并将所述函数定义语句所对应翻译成的C++代码语句填入其他作用域语句区域,将所述非函数定义语句所对应翻译成的C++代码语句填入所在作用域的函数体内。
作为本发明的进一步改进,所述抽象语法树为一个结点对象列表,每个对象包括多个子列表或者其他对象的引用,每个列表或者子列表包括任意数量的结点对象。
作为本发明的进一步改进,获取所述Python源码的抽象语法树的过程包括步骤:调用抽象语法树模块的parse()函数,采用dump()函数将parse()函数获取的Python源码的抽象语法树转化为自然语言形式。
作为本发明的进一步改进,将所述ASCII文本文件应用至嵌入式平台时,采用C或者其他语言封装模块实现IO操作。
有益效果
本发明具有以下优点:
本发明实施例提供的实现编译型嵌入式的Python方法基于类型注释和静态分析实现了源码翻译器,并将翻译器集成到嵌入式平台中,实现了Python源文件的编辑、编译、链接和烧写,突破了现有技术无法将Python实时应用至嵌入式平台领域的缺陷。
附图说明
为了更清楚地说明本发明实施例或现有技术中的技术方案,下面将对实施例或现有技术描述中所需要使用的附图作简单地介绍,显而易见地,下面描述中的附图仅仅是本发明中记载的一些实施例,对于本领域普通技术人员来讲,在不付出创造性劳动的前提下,还可以根据这些附图获得其他的附图。
图1为本发明实施例提供的实现编译型嵌入式的Python方法的流程示意图;
图2为图1所示实施例中的另一种表达方式的流程示意图;
图3为应用本发明实施例提供的实现编译型嵌入式的Python方法在E-Python-AHL-GEC-IDE编译成功界面的示意图;
图4为图3所示实施例中的串口接收亮暗改变信息的界面示意图;
图5为图3所示实施例中的小灯亮起的实际图示。
本发明的实施方式
为了使本技术领域的人员更好地理解本发明中的技术方案,下面将结合本发明实施例中的附图,对本发明实施例中的技术方案进行清楚、完整地描述,显然,所描述的实施例仅仅是本发明一部分实施例,而不是全部的实施例。基于本发明中的实施例,本领域普通技术人员在没有做出创造性劳动前提下所获得的所有其他实施例,都应当属于本发明保护的范围。
如图1和图2所示,本发明实施例提供的实现编译型嵌入式的Python方法的流程示意图。在该实施例中,实现编译型嵌入式的Python方法包括四个步骤,每个步骤的具体内容如下所示。
步骤S1:遍历Python源码的抽象语法树而获取程序的语义信息并根据所述语义信息生成对应的C++代码。
抽象语法树(Abstract Syntax Tree,简称AST)是源代码到目标代码的一种中间表示,用树状结构描述了程序构造。Python的AST可以表示为一个结点对象列表,每个对象包含多个子列表或其他对象的引用,每个列表或子列表包含任意数量的结点对象。Python的AST通过调用抽象语法树(ast)模块的parse()函数获取,并且可以使用dump()函数将其转为自然语言形式。表1展示了一个Python函数及其调用的抽象语法树。
Figure 206585dest_path_image001
在表1中,其中根节点Module表示代码整体,列表body包含一个函数定义FunctionDef和一个表达式语句Expr。函数定义结点中有函数名name、参数列表args、函数体子列表body和返回值注释returns。表达式语句包括了一个函数调用结点Call,函数调用结点中有函数名func和参数列表args。
编译型嵌入式Python通过遍历Python源码的AST获取程序的语义信息,并根据其语义生成对应的C++代码。在遍历过程中主要处理的语法树结点及其表示的内容如表2所示。
Figure 700889dest_path_image002
步骤S2:对Python源码的进行类型注释,从而生成C++的变量定义和函数定义。
类型注释(Type Hints,又称类型提示或类型注解)是Python3.5版本之后加入的语法,它将变量或函数的类型信息写入源代码中,使得在语义分析阶段可以直接读出类型信息而不用再进行复杂的推导。该功能旨在开放Python代码来简化程序的静态分析、运行时类型检查和根据类型信息生成其他代码。
类型注释有变量注释和函数注释两种。变量注释是在对单个变量赋值的同时给出变量的类型,该类赋值语句又被成为带标注的赋值语句。函数注释是在函数定义时给出参数的类型和返回值的类型。表3给出了类型注释的例子,一个是int类型变量x的变量注释,另一个是返回值为int、参数为一个int的函数func的函数注释。
Figure 480626dest_path_image003
编译型嵌入式Python利用类型注释来生成C++的变量定义和函数定义。对于变量,要求第一次出现必须是变量注释,从而能够生成对应的C++变量定义语句。对于函数,要求必须使用函数注释,从而生成参数和返回值类型与注释内容对应的函数定义和声明。
编译型嵌入式Python要求变量先定义类型后再使用,函数必须使用函数注释,示例如表3,对应的语法描述如下:
Figure 938153dest_path_image004
其中,V是变量va定义示使用的带标注的赋值语句语法;F是函数func的定义语法,stmt_list是函数体,
Figure 752525dest_path_image005
是参数列表,
Figure 122457dest_path_image006
是返回值类型;type和B定义了能够使用的类型。
步骤S3:采用翻译器对经过步骤S1和步骤S2处理过的Python源代码翻译成C++源文件。
翻译器的设计过程包括三个步骤,具体如下所示。
步骤S31:定义翻译内容的结构,所述结构包括头文件包含区、命名空间定义区和源码区。从Python代码的执行流程与C++的执行流程中抽取共性,可将翻译内容的结构分为头文件包含区、命名空间定义和源码区三部分,如表4所示。在头文件包含区中"builtin.h"必须被包含,该文件内声明了Python的内置函数的C++实现。命名空间用来模拟Python的模块。源码区必须包含当前模块的初始化函数<Module>_init(),该函数用于存放Python源文件中全局作用域内的代码翻译结果。
Figure 389491dest_path_image007
步骤S32:设计Python的类型至C++的类型的映射关系。Python的类型分为基础类型和复合类型两种,基础类型包括了整型、浮点型、字符串、布尔型和字节数组,复合类型包括了列表类型、元组类型、集合类型和字典类型。设基础类型集为T={int,float,str,bytes,bool},设Python类型到C/C++的映射关系为f,映射关系如表5所示,其中x,y∈T。
Figure 588391dest_path_image008
步骤S33:采用自底向上的方法对抽象语法树进行语义分析并生成C++代码,将生成的C++代码按照所述翻译内容的结构进行拼接。翻译过程是自底向上对语法树进行语义分析并生成C++代码的过程,生成的代码最终按翻译文件的内容结构进行拼接。按表2中给出的结点类型可以分为表达式结点的翻译和语句结点的翻译两类过程。该步骤具体包括两个步骤,具体内容如下所示。
步骤S331:采用自底向上的子节点访问的方式进行表达式翻译。表达式翻译是语句翻译的基础,它将构成某种语句的Python表达式翻译成C++表达式后返回给语句翻译过程。表1已经描述了Python表达式结点的类型,一个表达式expr的形式化描述如下所示。
Figure 585166dest_path_image009
表达式由于存在递归定义,所以其翻译过程是一个自底向上的子结点访问过程,设表达式翻译函数名为visit_expr,则其内容就是将传入的表达式结点参数代入对应类型的访问函数中,如表6所示。
Figure 578529dest_path_image010
限于篇幅,具体类型的表达式翻译过程不进行详细描述。
步骤S332:将语句或者/和语句列表中的每行程序代码翻译成C++代码语句。语句是关键字和表达式的有序排列,用来表示程序执行的操作,因此语句翻译过程是根据语法树结点内容对表达式与关键字的重新组合。Python中常用的语句有赋值类语句、程序结构控制类语句、函数定义语句、模块导入语句和表达式语句。设Python的单句语句为stmt,语句块为stmt_list,回车换行符为,语句和语句列表的形式化描述如下所示。
Figure 378864dest_path_image011
在该步骤的翻译过程中,需要先将所述语句区分为函数定义语句和非函数定义语句,并将所述函数定义语句所对应翻译成的C++代码语句填入其他作用域语句区域,将所述非函数定义语句所对应翻译成的C++代码语句填入所在作用域的函数体内。设语句翻译函数名为visit_stmt,输入是一个语句列表L,输出是函数定义翻译列表func_result和其他语句翻译列表other,其伪代码描述如表7所示。
Figure 115876dest_path_image012
限于篇幅,本发明实施例主要描述流程控制类语句和函数定义语句的翻译过程。
1.流程控制类语句的翻译
Python的程序结构分为顺序结构、选择结构和循环结构三类。顺序结构即按从上到下的顺序执行两句行语句。选择结构使程序按某个条件执行分支程序,在Python中即为if语句,相比C++少了switch语句。循环结构使程序在某个条件内重复执行一段代码,在Python中有while和for两种循环。下面是选择结构和循环结构从Python语法翻译成C++语法的方法。
if语句由分支条件、if主体代码块和else代码块三部分构成。分支条件是一个表达式,主体代码块和else代码块都是语句列表。
翻译if语句需要将分支条件condition、主体代码块body和else代码块orelse代入对应的翻译函数,然后将翻译结果按C++语法拼接。对于任意的If语句结点i,翻译过程函数visit_if可以描述为:
Figure 639261dest_path_image013
while语句由循环控制条件和循环体代码块构成。循环控制条件是一个表达式,循环体代码块是语句列表。
翻译while语句需要将循环条件condition、循环体代码块body代入对应的翻译函数,然后将翻译结果按C++语法拼接。对于任意的While语句结点w,翻译过程函数visit_while可以描述为:
Figure 397002dest_path_image014
for语句由循环变量、序列对象和循环体代码块构成。循环变量和序列对象都是表达式,循环体代码块是语句列表。
该类循环实现的是从序列对象seq第一个元素循环到最后一个元素,每次循环都用循环变量iter表示当前元素,与C++的迭代器相似。由于序列类型翻译后都使用STL容器类型来表示,因此for语句也是翻译成C++的迭代器语法。对于任意的For语句结点f,翻译过程函数visit_for可以描述为:
Figure 638627dest_path_image015
2.函数定义的翻译
函数定义由函数名func、参数列表args、返回值注释returns和函数内容body构成,嵌入式Python要求参数和返回值都必须加上类型注释,因此函数定义可以描述为:
Figure 913751dest_path_image016
其中arg1:typea1~argn:typean是参数列表,typer1~typerm是返回值注释。
翻译函数定义需要将第一个返回值类型作为函数类型,将后面的返回值类型对应的引用类型插入到参数列表中。设按基本类型映射关系映射后的参数类型为ta1~tan,返回值类型为tr1~trn,f.stmt_list是函数内的语句块,函数定义的翻译函数visit_func可以描述为:
Figure 370271dest_path_image017
步骤S4:将所述C++源文件与嵌入式芯片相关的文件存储在一起而形成文件包,对所述文件包进行编译和链接并生成ASCII文本文件。
本发明实施例提供的实现编译型嵌入式的Python方法基于类型注释和静态分析实现了源码翻译器,并将翻译器集成到嵌入式平台中,实现了Python源文件的编辑、编译、链接和烧写,突破了现有技术无法将Python实时应用至嵌入式平台领域的缺陷。
由于Python不具有指针操作功能,不能直接按地址访问存储器,所以Python都是借助C或其他语言封装的模块实现IO操作。IO操作是嵌入式应用的基础,因此必须设计与C/C++的混合编程方法,从而实现嵌入式Python的IO操作。
在实现IO操作过程中,函数和变量的声明规则具体过程如下:函数和变量需要使用一个Python源文件声明,该文件第一行内容是"#Extern Definition",翻译器遇到该类源文件时,只对其生成标识符信息表而不进行内容翻译。函数和变量的声明语法需要符合上文编译型嵌入式Python中函数注释中的中的F和V,并且函数体的内容是"pass",表示空内容。设用来存放函数和变量声明的Python源文件(模块)名为Module.py,则其内容C可以描述如下所示:
Figure 705437dest_path_image018
在实现IO操作过程中,函数和变量的实现规则具体过程如下:函数和变量使用C++语法进行实现,需要使用一个扩展名为".cpp"的文件作为源文件,和一个文件名为"Module.hpp"的文件作为头文件。
(1)变量
全局变量的实现需要对Module.py中声明的变量按C++语法加上前缀"Py_"。设Module.py中有名称为v变量声明V,它的类型注释为type,经过类型映射后得到t,则该变量的在Module.hpp中的内容E和在源文件中的内容T可以描述为:
Figure 434359dest_path_image019
(2)函数
函数的实现需要对函数名、参数名添加"Py_"前缀,并且将第一个返回值作为函数类型,第二个以后的返回值的引用类型加入到参数列表中。
设Module.py中有函数名为func的函数声明F,有k个参数arg1~argk并且它们的类型经过映射后为t1~tk,有z个返回值,并且映射后的类型为r1~rz,函数内容为S(使用C++编写),则Module.hpp中需要的函数声明H和源文件中需要的函数实现C可以描述为:
Figure 575490dest_path_image020
在确定函数和变量的实现内容后,最后对这些内容按翻译文件的结构进行排布,即可完成混编的C++实现部分。表5描述了源文件和Module.hpp的内容,&符号表示文法的与运算,includes是混编中使用的其他文件中定义的函数、变量所需要包含的头文件语句列表。
Figure 73468dest_path_image021
在本发明实施例中,还将上述的ASCII文本文件应用至嵌入式平台进行了实验。在该实验中,嵌入式平台选取为STM32L431RC为硬件平台,将RT-Thread的操作系统API通过混合编程方法提供给嵌入式Python,并在该实时操作系统环境下实现了三盏小灯的闪烁。
STM32L431是基于高性能Cortex-M4内核的32位精简指令集超低功耗微控制器,工作频率最高达80 MHz。它有256KB的Flash和64KB的RAM,并提供了一个低功耗RTC、一个通用32位定时器、一个专用于电机控制的16位PWM定时器、四个通用16位定时器和两个16位低功耗定时器。
实验实现的功能是在RT-Thread系统环境下,主线程创建红灯、绿灯和蓝灯三个线程,三个线程控制三色灯的颜色,并通过UART向PC机发送小灯亮暗状态。
实验使用基于苏州大学与ARM公司联合出品的嵌入式开发集成开发环境AHL-GEC-IDE改版的E-Python-AHL-GEC-IDE作为开发环境,编写程序并烧写到目标板上,如图3所示。编译成功后使用串口更新烧写到STM32中运行,可以看到三个线程交替执行,小灯交替改变亮暗,并向上位机发送亮暗状态改变信息,如图4所示。小灯亮起如图5所示。
通过实验,有效地证明了本发明实施例所提出的编译型嵌入式Python的方法可以在嵌入式平台上实现,能够符合嵌入式平台的实时性要求。
对于本领域技术人员而言,显然本发明不限于上述示范性实施例的细节,而且在不背离本发明的精神或基本特征的情况下,能够以其他的具体形式实现本发明。因此,无论从哪一点来看,均应将实施例看作是示范性的,而且是非限制性的,本发明的范围由所附权利要求而不是上述说明限定,因此旨在将落在权利要求的等同要件的含义和范围内的所有变化囊括在本发明内。不应将权利要求中的任何附图标记视为限制所涉及的权利要求。
此外,应当理解,虽然本说明书按照实施方式加以描述,但并非每个实施方式仅包含一个独立的技术方案,说明书的这种叙述方式仅仅是为清楚起见,本领域技术人员应当将说明书作为一个整体,各实施例中的技术方案也可以经适当组合,形成本领域技术人员可以理解的其他实施方式。

Claims (10)

  1. 一种实现编译型嵌入式Python的方法,其特征在于,所述方法包括:
    步骤S1:遍历Python源码的抽象语法树而获取程序的语义信息并根据所述语义信息生成对应的C++代码;
    步骤S2:对Python源码的进行类型注释,从而生成C++的变量定义和函数定义;
    步骤S3:采用翻译器对经过步骤S1和步骤S2处理过的Python源代码翻译成C++源文件;
    步骤S4:将所述C++源文件与嵌入式芯片相关的文件存储在一起而形成文件包,对所述文件包进行编译和链接并生成ASCII文本文件。
  2. 根据权利要求1所述的实现编译型嵌入式Python的方法,其特征在于,所述嵌入式芯片相关的文件包括头文件、启动文件和链接文件。
  3. 根据权利要求1所述的实现编译型嵌入式Python的方法,其特征在于,所述类型注释包括变量注释和函数注释,
    所述变量注释包括步骤:对Python源码中的单个变量赋值的同时给出所述变量的类型,从而生成对应的C++变量定义;
    所述函数注释包括步骤:对Python源码中的函数进行定义的同时给出所述函数中参数的类型和所述函数的返回值的类型,从而生成对应的C++函数定义。
  4. 根据权利要求1所述的实现编译型嵌入式Python的方法,其特征在于,所述翻译器的设计过程包括:
    步骤S31:定义翻译内容的结构,所述结构包括头文件包含区、命名空间定义区和源码区;
    步骤S32:设计Python的类型至C++的类型的映射关系;
    步骤S33:采用自底向上的方法对抽象语法树进行语义分析并生成C++代码,将生成的C++代码按照所述翻译内容的结构进行拼接。
  5. 根据权利要求4所述的实现编译型嵌入式Python的方法,其特征在于,所述映射关系如下表所示,其中x,y∈T,T={int,float,str,bytes,bool}
    注释的类型 翻译后的类型 int int float float str string bytes vector<char> bool bool List[x]   (列表类型) vector<f(x)> Tuple[x]  (元组类型) vector<f(x)> Set[x]    (集合类型) set<f(x)> Dict[x,y] (字典类型) map<f(x),f(y)>
     。
  6. 根据权利要求4所述的实现编译型嵌入式Python的方法,其特征在于,所述步骤S33包括:
    步骤S331:采用自底向上的子节点访问的方式进行表达式翻译;
    步骤S332:将语句或者/和语句列表中的每行程序代码翻译成C++代码语句。
  7. 根据权利要求6所述的实现编译型嵌入式Python的方法,其特征在于,所述步骤S332的翻译过程中,需要先将所述语句区分为函数定义语句和非函数定义语句,并将所述函数定义语句所对应翻译成的C++代码语句填入其他作用域语句区域,将所述非函数定义语句所对应翻译成的C++代码语句填入所在作用域的函数体内。
  8. 根据权利要求1所述的实现编译型嵌入式Python的方法,其特征在于,所述抽象语法树为一个结点对象列表,每个对象包括多个子列表或者其他对象的引用,每个列表或者子列表包括任意数量的结点对象。
  9. 根据权利要求8所述的实现编译型嵌入式Python的方法,其特征在于,获取所述Python源码的抽象语法树的过程包括步骤:调用抽象语法树模块的parse()函数,采用dump()函数将parse()函数获取的Python源码的抽象语法树转化为自然语言形式。
  10. 根据权利要求1所述的实现编译型嵌入式Python的方法,其特征在于,将所述ASCII文本文件应用至嵌入式平台时,采用C或者其他语言封装模块实现IO操作。
PCT/CN2020/140482 2020-11-11 2020-12-29 一种实现编译型嵌入式Python的方法 WO2022099888A1 (zh)

Priority Applications (1)

Application Number Priority Date Filing Date Title
US17/786,502 US11740881B2 (en) 2020-11-11 2020-12-29 Method for implementing compiled embedded Python

Applications Claiming Priority (2)

Application Number Priority Date Filing Date Title
CN202011253208.6A CN112346722B (zh) 2020-11-11 2020-11-11 一种实现编译型嵌入式Python的方法
CN202011253208.6 2020-11-11

Publications (1)

Publication Number Publication Date
WO2022099888A1 true WO2022099888A1 (zh) 2022-05-19

Family

ID=74363337

Family Applications (1)

Application Number Title Priority Date Filing Date
PCT/CN2020/140482 WO2022099888A1 (zh) 2020-11-11 2020-12-29 一种实现编译型嵌入式Python的方法

Country Status (3)

Country Link
US (1) US11740881B2 (zh)
CN (1) CN112346722B (zh)
WO (1) WO2022099888A1 (zh)

Cited By (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN116112479A (zh) * 2022-12-08 2023-05-12 联通(浙江)产业互联网有限公司 http接口实现方法、装置、设备及存储介质
CN118733007A (zh) * 2024-09-03 2024-10-01 成都精灵云科技有限公司 基于Python的代码自动生成和自动检查方法

Families Citing this family (9)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN113126976B (zh) * 2021-04-08 2022-06-21 复旦大学 基于语法制导编辑器生成器的跨界服务长尾变化应变方法
CN113821204B (zh) * 2021-08-30 2024-02-23 济南浪潮数据技术有限公司 一种组件生成方法、装置、电子设备及可读存储介质
CN113641389B (zh) * 2021-08-31 2024-02-09 广东九联科技股份有限公司 基于OpenCPU的软件升级方法、装置及设备
CN113626006A (zh) * 2021-10-11 2021-11-09 深圳市中科先见医疗科技有限公司 基于RT Thread操作系统的图像处理方法及设备
CN114706570A (zh) * 2022-03-30 2022-07-05 宿迁学院产业技术研究院 一种图形化编程系统
CN116506527A (zh) * 2023-03-27 2023-07-28 中国船舶集团有限公司第七一三研究所 一种报文的编码方法及解码方法
CN116149671B (zh) * 2023-04-23 2023-07-04 中国科学院软件研究所 用于翻译智能合约语言的方法和装置、电子设备
CN117076296B (zh) * 2023-07-31 2025-06-06 上海欣诺通信技术股份有限公司 网络嵌入式设备的控制系统、方法、设备及介质
CN118885179B (zh) * 2024-09-27 2024-12-20 宁波天巡科技有限公司 一种提高航天器飞行状态识别效率的方法和装置

Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN102902540A (zh) * 2012-09-25 2013-01-30 浙江创佳数字技术有限公司 嵌入式自动化开发方法
CN104182267A (zh) * 2013-05-21 2014-12-03 中兴通讯股份有限公司 编译方法、解释方法、装置及用户设备
CN111142876A (zh) * 2020-04-02 2020-05-12 华控清交信息科技(北京)有限公司 一种编译处理方法、装置和用于编译处理的装置
CN111249736A (zh) * 2020-01-16 2020-06-09 网易(杭州)网络有限公司 代码处理方法及装置
US10831456B1 (en) * 2019-05-31 2020-11-10 The Mathworks, Inc. External code integrations within a computing environment

Family Cites Families (8)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US7010580B1 (en) * 1999-10-08 2006-03-07 Agile Software Corp. Method and apparatus for exchanging data in a platform independent manner
US8332828B2 (en) * 2002-11-20 2012-12-11 Purenative Software Corporation System for translating diverse programming languages
US8032877B2 (en) * 2006-10-20 2011-10-04 International Business Machines Corporation Compiler neutral linking solution for C++ code
CN101414265A (zh) * 2008-12-02 2009-04-22 南京大学 一种基于向量处理器的在线编译方法
EP3155513A1 (en) * 2014-06-13 2017-04-19 The Charles Stark Draper Laboratory, Inc. Systems and methods for software analysis
CN110275709B (zh) * 2018-03-15 2023-07-25 斑马智行网络(香港)有限公司 针对动态语言的处理及优化方法、装置、设备及存储介质
US10545730B2 (en) * 2018-05-07 2020-01-28 Red Hat, Inc. Automatically generating code for application programming interfaces
CN111459500A (zh) * 2020-06-17 2020-07-28 北京机电工程研究所 基于海鹰翼辉操作系统的安全编译方法及装置

Patent Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN102902540A (zh) * 2012-09-25 2013-01-30 浙江创佳数字技术有限公司 嵌入式自动化开发方法
CN104182267A (zh) * 2013-05-21 2014-12-03 中兴通讯股份有限公司 编译方法、解释方法、装置及用户设备
US10831456B1 (en) * 2019-05-31 2020-11-10 The Mathworks, Inc. External code integrations within a computing environment
CN111249736A (zh) * 2020-01-16 2020-06-09 网易(杭州)网络有限公司 代码处理方法及装置
CN111142876A (zh) * 2020-04-02 2020-05-12 华控清交信息科技(北京)有限公司 一种编译处理方法、装置和用于编译处理的装置

Cited By (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN116112479A (zh) * 2022-12-08 2023-05-12 联通(浙江)产业互联网有限公司 http接口实现方法、装置、设备及存储介质
CN118733007A (zh) * 2024-09-03 2024-10-01 成都精灵云科技有限公司 基于Python的代码自动生成和自动检查方法

Also Published As

Publication number Publication date
CN112346722A (zh) 2021-02-09
US20230075927A1 (en) 2023-03-09
CN112346722B (zh) 2022-04-19
US11740881B2 (en) 2023-08-29

Similar Documents

Publication Publication Date Title
CN112346722B (zh) 一种实现编译型嵌入式Python的方法
CN110187885B (zh) 一种量子程序编译的中间代码生成方法及装置
Chambers et al. An efficient implementation of SELF a dynamically-typed object-oriented language based on prototypes
CN108920133B (zh) 跨语言编程方法、装置、电子设备及存储介质
CN106462425B (zh) 使用复常量的方法和系统
US8332828B2 (en) System for translating diverse programming languages
US7526755B2 (en) Plug-in pre- and postconditions for static program analysis
US6055370A (en) Apparatus which allows data sharing amongst computer programs from different program environments
US8656372B2 (en) System for translating diverse programming languages
JPH08115209A (ja) ソース・コード作成システム及び方法
KR950006609B1 (ko) 다중 패스코드 발생에 있어서 템플리트를 이용한 다중 언어 최적화 컴파일러
CN110399133A (zh) 一种基于前端字节码技术的JavaScript代码优化方法
US20130152061A1 (en) Full fidelity parse tree for programming language processing
Lu et al. Gradual soundness: Lessons from static python
CN113138755A (zh) 一种json序列化和反序列化的优化方法及系统
Chambers et al. An efficient implementation of SELF, a dynamically-typed object-oriented language based on prototypes
Racordon From ASTs to machine code with LLVM
CN113835688B (zh) 一种科学计算语言解释器的对象封装方法
Jeffery Build Your Own Programming Language: A programmer's guide to designing compilers, interpreters, and DSLs for solving modern computing problems
CN111767033A (zh) 用于机械臂程序开发的编程系统及功能扩展方法
Harsu Re-engineering legacy software through language conversion
Nabiyev Translating c into java bytecode
Salgado The Design and Implementation of an Extensible System Meta-Programming Language
Di Giacomo Metacasanova: a high-performance meta-compiler for domain-specific languages
CN118747066A (zh) 一种基于3de平台组件应用架构二次开发的方法及系统

Legal Events

Date Code Title Description
121 Ep: the epo has been informed by wipo that ep was designated in this application

Ref document number: 20961441

Country of ref document: EP

Kind code of ref document: A1

NENP Non-entry into the national phase

Ref country code: DE

122 Ep: pct application non-entry in european phase

Ref document number: 20961441

Country of ref document: EP

Kind code of ref document: A1