Thanks to visit codestin.com
Credit goes to github.com

Skip to content

REPL str support #2724

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

Merged
merged 2 commits into from
Jun 6, 2024
Merged

REPL str support #2724

merged 2 commits into from
Jun 6, 2024

Conversation

Vipul-Cariappa
Copy link
Contributor

No description provided.

@Vipul-Cariappa
Copy link
Contributor Author

cc @Shaikh-Ubaid

Copy link
Collaborator

@ubaidsk ubaidsk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two comments. Rest looks good. Thanks for this!

if (y == NULL) {
*x = NULL;
return;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this? Could you share an example where this fails currently as of the main branch?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without the above change, the following fails (SEGFAULT):

❯ lp                                                           
>>> s: str
>>> s
The stack address was not found in any shared library or the main program, the stack is probably corrupted. Aborting.

Tested by Test Case PythonCompiler string 2.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is fine. I am curious about the following:

  • why does _lfortran_strcpy get called in this case?
  • are you able to reproduce this same bug in normal/regular lpython invocation?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why does _lfortran_strcpy get called in this case?

The global_stmt function that is constructed with the top-level expressions, copies the string into the return variable. You can run the example code above with lp -v flag to look at the ASR and LLVM IR generated.

The ASR and IR for the second line:

(TranslationUnit
    (SymbolTable
        1
        {
            __main__:
                (Module
                    (SymbolTable
                        2
                        {
                            __main__global_stmts_2__:
                                (Function
                                    (SymbolTable
                                        3
                                        {
                                            __main__global_stmts_2__1:
                                                (Variable
                                                    3
                                                    __main__global_stmts_2__1
                                                    []
                                                    ReturnVar
                                                    ()
                                                    ()
                                                    Default
                                                    (Character 1 -2 ())
                                                    ()
                                                    BindC
                                                    Public
                                                    Required
                                                    .false.
                                                )
                                        })
                                    __main__global_stmts_2__
                                    (FunctionType
                                        []
                                        (Character 1 -2 ())
                                        BindC
                                        Implementation
                                        ()
                                        .false.
                                        .false.
                                        .false.
                                        .false.
                                        .false.
                                        []
                                        .false.
                                    )
                                    []
                                    []
                                    [(Assignment
                                        (Var 3 __main__global_stmts_2__1)
                                        (Var 2 s)
                                        ()
                                    )]
                                    (Var 3 __main__global_stmts_2__1)
                                    Public
                                    .false.
                                    .false.
                                    ()
                                ),
                            s:
                                (Variable
                                    2
                                    s
                                    []
                                    Local
                                    ()
                                    ()
                                    Default
                                    (Character 1 -2 ())
                                    ()
                                    Interactive
                                    Public
                                    Required
                                    .false.
                                )
                        })
                    __main__
                    []
                    .false.
                    .false.
                )
        })
    []
)
; ModuleID = 'LFortran'
source_filename = "LFortran"

@s = external global i8*

define i8* @__main__global_stmts_2__() {
.entry:
  %__main__global_stmts_2__1 = alloca i8*, align 8
  store i8* null, i8** %__main__global_stmts_2__1, align 8
  %0 = load i8*, i8** @s, align 8
  call void @_lfortran_strcpy(i8** %__main__global_stmts_2__1, i8* %0, i8 0)
  br label %return

return:                                           ; preds = %.entry
  %1 = load i8*, i8** %__main__global_stmts_2__1, align 8
  ret i8* %1
}

declare void @_lfortran_strcpy(i8**, i8*, i8)

are you able to reproduce this same bug in normal/regular lpython invocation?

No, this does not cause any problem with normal lpython execution.


Maybe we can optimize it to not make a copy. I will look into it.

result.type = EvalResult::string;
result.str = r;
} else {
goto raise_error;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't use goto. Rather we can simply have throw LCompilersException("FortranEvaluator::evaluate(): Return type not supported"); here. (It might look like duplication, but I think its fine at the moment).

@ubaidsk ubaidsk marked this pull request as draft June 5, 2024 14:15
@Vipul-Cariappa Vipul-Cariappa marked this pull request as ready for review June 6, 2024 06:31
@Vipul-Cariappa Vipul-Cariappa requested a review from ubaidsk June 6, 2024 06:31
Copy link
Collaborator

@ubaidsk ubaidsk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Great work!

@ubaidsk ubaidsk merged commit 7ecac3e into lcompilers:main Jun 6, 2024
14 checks passed
@Vipul-Cariappa Vipul-Cariappa deleted the strings branch June 8, 2024 03:44
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

Successfully merging this pull request may close these issues.

2 participants