Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

backend code generation when python programm cotains class #2815

Open
ljq-0814 opened this issue Nov 9, 2024 · 1 comment
Open

backend code generation when python programm cotains class #2815

ljq-0814 opened this issue Nov 9, 2024 · 1 comment

Comments

@ljq-0814
Copy link

ljq-0814 commented Nov 9, 2024

I have a question regarding the compilation of a Python program using LPython. If a Python program contains any class or a class like the following:

class student:
    def __init__(self: InOut['student'], name: str, age: i32):
        self.name : str = name
        self.age : i32 = age

Does this imply that after compilation, the code can only generate C++ code, and not C code? I would appreciate an explanation of why that might be the case. Thank you!

@kmr-srbh
Copy link
Contributor

@ljq-0814 LPython translates the source code into an intermediary form called an Abstract Semantic Representation (ASR). This form contains all the information related to the code, including type semantics. It is done inside python_ast_to_asr.cpp. You can see the generated ASR using the --show-asr flag.

The above code gets translated to:

(TranslationUnit
    (SymbolTable
        1
        {
            __main__:
                (Module
                    (SymbolTable
                        2
                        {
                            student:
                                (Struct
                                    (SymbolTable
                                        3
                                        {
                                            __init__:
                                                (Function
                                                    (SymbolTable
                                                        4
                                                        {
                                                            age:
                                                                (Variable
                                                                    4
                                                                    age
                                                                    []
                                                                    In
                                                                    ()
                                                                    ()
                                                                    Default
                                                                    (Integer 4)
                                                                    ()
                                                                    Source
                                                                    Public
                                                                    Required
                                                                    .false.
                                                                ),
                                                            name:
                                                                (Variable
                                                                    4
                                                                    name
                                                                    []
                                                                    In
                                                                    ()
                                                                    ()
                                                                    Default
                                                                    (Character 1 -2 ())
                                                                    ()
                                                                    Source
                                                                    Public
                                                                    Required
                                                                    .false.
                                                                ),
                                                            self:
                                                                (Variable
                                                                    4
                                                                    self
                                                                    []
                                                                    InOut
                                                                    ()
                                                                    ()
                                                                    Default
                                                                    (StructType
                                                                        [(Character 1 -2 ())
                                                                        (Integer 4)]
                                                                        [(FunctionType
                                                                            [(Class
                                                                                2 student
                                                                            )
                                                                            (Character 1 -2 ())
                                                                            (Integer 4)]
                                                                            ()
                                                                            Source
                                                                            Implementation
                                                                            ()
                                                                            .false.
                                                                            .false.
                                                                            .false.
                                                                            .false.
                                                                            .false.
                                                                            []
                                                                            .false.
                                                                        )]
                                                                        .false.
                                                                        2 student
                                                                    )
                                                                    ()
                                                                    Source
                                                                    Public
                                                                    Required
                                                                    .false.
                                                                )
                                                        })
                                                    __init__
                                                    (FunctionType
                                                        [(Class
                                                            2 student
                                                        )
                                                        (Character 1 -2 ())
                                                        (Integer 4)]
                                                        ()
                                                        Source
                                                        Implementation
                                                        ()
                                                        .false.
                                                        .false.
                                                        .false.
                                                        .false.
                                                        .false.
                                                        []
                                                        .false.
                                                    )
                                                    []
                                                    [(Var 4 self)
                                                    (Var 4 name)
                                                    (Var 4 age)]
                                                    [(Assignment
                                                        (StructInstanceMember
                                                            (Var 4 self)
                                                            3 name
                                                            (Character 1 -2 ())
                                                            ()
                                                        )
                                                        (Var 4 name)
                                                        ()
                                                    )
                                                    (Assignment
                                                        (StructInstanceMember
                                                            (Var 4 self)
                                                            3 age
                                                            (Integer 4)
                                                            ()
                                                        )
                                                        (Var 4 age)
                                                        ()
                                                    )]
                                                    ()
                                                    Public
                                                    .false.
                                                    .false.
                                                    ()
                                                ),
                                            age:
                                                (Variable
                                                    3
                                                    age
                                                    []
                                                    Local
                                                    ()
                                                    ()
                                                    Default
                                                    (Integer 4)
                                                    ()
                                                    Source
                                                    Public
                                                    Required
                                                    .false.
                                                ),
                                            name:
                                                (Variable
                                                    3
                                                    name
                                                    []
                                                    Local
                                                    ()
                                                    ()
                                                    Default
                                                    (Character 1 -2 ())
                                                    ()
                                                    Source
                                                    Public
                                                    Required
                                                    .false.
                                                )
                                        })
                                    student
                                    []
                                    [name
                                    age]
                                    [__init__]
                                    Source
                                    Public
                                    .false.
                                    .false.
                                    [(())
                                    (())]
                                    ()
                                    ()
                                )
                        })
                    __main__
                    []
                    .false.
                    .false.
                ),
            main_program:
                (Program
                    (SymbolTable
                        5
                        {
                            
                        })
                    main_program
                    []
                    []
                )
        })
    []
)

We then apply some optimization passes to this initial ASR. The final ASR is passed over to the backend for further translation. The backend translator asr_to_*.cpp visits the ASR nodes and handles it appropriately. In C, the closest resemblance to a class is struct. We handle this case similarly. You can see the final transformed code after the backend visitor using the appropriate --show-* option (see --help). Currently, our implementation of OOP is basic. The above code gets translated to the following in the C backend:

#include <inttypes.h>

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


struct dimension_descriptor
{
    int32_t lower_bound, length, stride;
};
struct student {
 char * name;
 int32_t age;
};




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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants