MontysCoconut / moco

The Monty to LLVM compiler
http://www.informatik.uni-bremen.de/monty/
GNU General Public License v3.0
10 stars 5 forks source link

Returning from if-Statement #27

Open skuzzle opened 8 years ago

skuzzle commented 8 years ago

Code generation for if-Statements seems broken. The following code produces the lli error

lli: C:\Users\Simon\Documents\Java\moco\target\test-classes\testPrograms\if.ll:110:1: error: expected instruction opcode

print(foo(true))
print(foo(false))

String foo(Bool cond):
    if cond:
        return "a"
    else:
        return "b"

Generated code:

define i8* @M.if.F.foo$M.std.C.String$M.Bool.C.Bool(%M.Bool.C.Bool* %M.if.V.cond$M.Bool.C.Bool)  {
    entry:
    %_unnamed_0 = getelementptr inbounds %M.Bool.C.Bool* %M.if.V.cond$M.Bool.C.Bool, i32 0,i32 1
    %_unnamed_1 = load i1* %_unnamed_0
    br i1 %_unnamed_1, label %if0.true, label %if0.false
    if0.true:
    ret i8* getelementptr inbounds ([2 x i8]* @.0, i32 0, i32 0)
    br label %if0.end
    if0.false:
    ret i8* getelementptr inbounds ([2 x i8]* @.1, i32 0, i32 0)
    br label %if0.end
    if0.end:
}

I'm not sure wether this applies to the current master but the code generation for if-statements seems not to have changed recently.

lummax commented 8 years ago

A possible solution (and as far as I can tell how other languages implement it):