tanvimoharir / kaleidoscope

llvm tutorial
1 stars 0 forks source link

Understanding for control flow and adding while? #6

Open tanvimoharir opened 1 year ago

tanvimoharir commented 1 year ago

Understanding for loop (especially the IR codegen part) Can also understand how to add recursion?

tanvimoharir commented 1 year ago

Currently the fibonacci function doesnt work properly Refer

ready> def fib(x)
ready> if x < 3 then
              1
       else
          fib(x-1) + fib(x-2);
Read function definition:define double @fib(double %x) {
entry:
  %cmptmp = fcmp ult double %x, 3.000000e+00
  br i1 %cmptmp, label %ifcont, label %else

else:                                             ; preds = %entry
  %subtmp = fadd double %x, -1.000000e+00
  %calltmp = call double @fib(double %subtmp)
  %subtmp1 = fadd double %x, -2.000000e+00
  %calltmp2 = call double @fib(double %subtmp1)
  %addtmp = fadd double %calltmp, %calltmp2
  br label %ifcont

ifcont:                                           ; preds = %entry, %else
  %iftmp = phi double [ %addtmp, %else ], [ 1.000000e+00, %entry ]
  ret double %iftmp
}

ready> fib(3);
ready> Evaluated to 2.000000
ready> fib(5);
ready> Evaluated to 5.000000