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

Returning from if-Statement #27

Open
skuzzle opened this issue Jul 23, 2015 · 1 comment
Open

Returning from if-Statement #27

skuzzle opened this issue Jul 23, 2015 · 1 comment

Comments

@skuzzle
Copy link
Contributor

skuzzle commented Jul 23, 2015

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
Copy link
Member

lummax commented Aug 14, 2015

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

  • Create a ret_value variable on function definition generation at the top of the function definition.
  • Create a ret_label block at the end of the function definition where the ret_value is returned vial LLVM ret.
  • Don't actually return on return statement, but set the ret_value and branch to the ret_label.

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

No branches or pull requests

2 participants