Skip to content

Nested if and while fail at codegen #172

Open
@Champii

Description

@Champii

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions