Skip to content

compiler: add support for recursive function types #2175

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

Merged
merged 1 commit into from
Oct 30, 2021

Conversation

aykevl
Copy link
Member

@aykevl aykevl commented Oct 13, 2021

This adds support for a construct like this:

type foo func(fn foo)

Unfortunately, LLVM cannot create function pointers that look like this.
LLVM only supports named types for structs (not for pointers) and thus
can't add a pointer to a function type of the same type to a parameter
of that function type.

The fix is simple: cast all function pointers to a void function, in
LLVM IR:

void ()*

Raw function pointers are cast to this type before storing, and cast
back to the regular function type before calling. This means that
function parameters will never refer to its own type because raw
function types are fixed at that one type.

Somehow, this does have an effect on binary size in some cases. The
effect is small and goes both ways. On top of that, there is work
underway in LLVM which would make all pointer types opaque (without a
pointee type). This would make this whole commit useless and therefore
should fix any size increases that might happen.
https://llvm.org/docs/OpaquePointers.html

@aykevl aykevl force-pushed the recursive-func-types branch 2 times, most recently from b9c47f0 to d300743 Compare October 28, 2021 14:17
@aykevl
Copy link
Member Author

aykevl commented Oct 28, 2021

Rebased to fix the merge conflict.
This PR solves #2172.

This adds support for a construct like this:

    type foo func(fn foo)

Unfortunately, LLVM cannot create function pointers that look like this.
LLVM only supports named types for structs (not for pointers) and thus
can't add a pointer to a function type of the same type to a parameter
of that function type.

The fix is simple: cast all function pointers to a void function, in
LLVM IR:

    void ()*

Raw function pointers are cast to this type before storing, and cast
back to the regular function type before calling. This means that
function parameters will never refer to its own type because raw
function types are fixed at that one type.

Somehow, this does have an effect on binary size in some cases. The
effect is small and goes both ways. On top of that, there is work
underway in LLVM which would make all pointer types opaque (without a
pointee type). This would make this whole commit useless and therefore
should fix any size increases that might happen.
https://llvm.org/docs/OpaquePointers.html
@niaow niaow force-pushed the recursive-func-types branch from d300743 to 471d08c Compare October 28, 2021 21:13
@niaow
Copy link
Member

niaow commented Oct 28, 2021

I just gave this a rebase so that #2209 is applied and CI should run (I hope this was okay?)

Copy link
Member

@niaow niaow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be working as expected. LGTM.

@deadprogram
Copy link
Member

Thanks @aykevl for working on this and thanks @niaow for review.

Now merging.

@deadprogram deadprogram merged commit afd49e7 into dev Oct 30, 2021
@deadprogram deadprogram deleted the recursive-func-types branch October 30, 2021 13:55
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

Successfully merging this pull request may close these issues.

3 participants