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

Nested if and while fail at codegen #172

Open
Champii opened this issue Oct 5, 2022 · 0 comments
Open

Nested if and while fail at codegen #172

Champii opened this issue Oct 5, 2022 · 0 comments
Labels
bug Something isn't working

Comments

@Champii
Copy link
Owner

Champii commented Oct 5, 2022

When dealing with nested if and while , the codegen context does a terrible job managing basic blocks and how to jump between them.
There is a missing unconditional jump after the phi instruction at the if_exit that should jump into the next basic block

main: ->
  if true
    if true
      1
    else
      2
  else
    3

Produces the following LLVM IR:

; ModuleID = 'mod'
source_filename = "mod"

define i64 @main() {
entry:
  br label %if

if:                                               ; preds = %entry
  br i1 true, label %then, label %else_body3

then:                                             ; preds = %if
  br label %if1
  br label %if_exit4   ; <= There is two br here

if1:                                              ; preds = %then
  br i1 true, label %then2, label %else_body

then2:                                            ; preds = %if1
  br label %if_exit

else_body:                                        ; preds = %if1
  br label %if_exit

if_exit:                                          ; preds = %else_body, %then2
  %phi = phi i64 [ 1, %then2 ], [ 2, %else_body ]
   ; <= There should be a br

else_body3:                                       ; preds = %if
  br label %if_exit4

if_exit4:                                         ; preds = %else_body3, %then
  %phi5 = phi i64 [ %phi, %then ], [ 3, %else_body3 ]
  ret i64 %phi5
}
Error: Bug in the generated IR:

Basic Block in function 'main' does not have terminator!
label %if_exit
@Champii Champii added the bug Something isn't working label Oct 5, 2022
@Champii Champii changed the title Nested if and while fails at codegen Nested if and while fail at codegen Oct 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant