Stream: t-compiler

Topic: c variadics in MIR


dlrobertson (Dec 21 2018 at 00:57, on Zulip):

I'm working on implementing C variadics in Rust and I'm having a hard time figuring out the best way to generate the correct MIR

dlrobertson (Dec 21 2018 at 00:58, on Zulip):

I currently see three approaches

dlrobertson (Dec 21 2018 at 01:00, on Zulip):

1) create a new TyKind for C variadic arguments. This gets a bit hard in typeck method resolution because we need to adjust it to be the type of the va_list language item

dlrobertson (Dec 21 2018 at 01:03, on Zulip):

2) Change FnTypes variadic boolean to the name of the user provided argument and when generating the function signature remove the last argument. (I need the name to generate va_start/va_end correctly)

dlrobertson (Dec 21 2018 at 01:04, on Zulip):

3) Rely on FnSigs variadic boolean and somehow hack stuff so that the input signature is something like fn (i32, VaList, ...) but in codegen we generate fn (i32, ...).

dlrobertson (Dec 21 2018 at 01:05, on Zulip):

Suggestions on code and/or documentation to read would be appreciated!

dlrobertson (Dec 21 2018 at 01:05, on Zulip):

I'm currently just trying things and spending some quality time in gdb :smiley:

dlrobertson (Dec 21 2018 at 01:06, on Zulip):

For reference the RFC is here https://github.com/rust-lang/rfcs/blob/master/text/2137-variadic.md

oli (Dec 21 2018 at 09:55, on Zulip):

to 1) I think it would be best not to have a custom TyKind, but instead wherever you are currently producing your TyKind::Variadic value, obtain the va_list lang item's DefId via tcx.language_items().va_list and yield a TyKind::Adt with the given DefId and empty substs

dlrobertson (Dec 21 2018 at 13:04, on Zulip):

This is what my current attempts at 2 and 3 do, but then we generate the function signature fn (i32, VaList, ...).

dlrobertson (Dec 21 2018 at 13:07, on Zulip):

So then when we generate the MIR I guess I need to remove the VaList from the argument list after it is added to the local_decls, but remove it from the function input?

oli (Dec 21 2018 at 13:33, on Zulip):

Can you show me the code where you generate new function signatures?

oli (Dec 21 2018 at 13:34, on Zulip):

What's your goal in this situation? have the function signature be fn(i32, VaList)?

oli (Dec 21 2018 at 13:35, on Zulip):

ah, so you want typeck to treat it as fn(i32, VaList) and mir generation to treat it as fn(i32, ...)?

oli (Dec 21 2018 at 13:38, on Zulip):

hmm... reading the RFC suggests to me that you would want the VaList argument in typeck and MIR generation and everything, and only make codegen forget about that argument

oli (Dec 21 2018 at 13:38, on Zulip):

or am I misinterpreting something here?

dlrobertson (Dec 21 2018 at 14:06, on Zulip):

ah, so you want typeck to treat it as fn(i32, VaList) and mir generation to treat it as fn(i32, ...)?

dlrobertson (Dec 21 2018 at 14:06, on Zulip):

exactly

dlrobertson (Dec 21 2018 at 14:10, on Zulip):

you would want the VaList argument in typeck and MIR generation and everything, and only make codegen forget about that argument

I think that could work...

dlrobertson (Dec 21 2018 at 14:10, on Zulip):

I might need to add a boolean on ArgType to indicate to codegen that the arg is indeed a variadic argument

dlrobertson (Dec 21 2018 at 14:12, on Zulip):

or am I misinterpreting something here?

nope

oli (Dec 21 2018 at 15:57, on Zulip):

I might need to add a boolean on ArgType to indicate to codegen that the arg is indeed a variadic argument

it could also be a new variant in the PassMode enum

oli (Dec 21 2018 at 15:57, on Zulip):

https://github.com/rust-lang/rust/blob/059e6a6f57f4e80d527a3cd8a8afe7f51f01af8e/src/librustc_target/abi/call/mod.rs#L37

dlrobertson (Dec 21 2018 at 15:58, on Zulip):

Ah nice!

dlrobertson (Dec 21 2018 at 15:59, on Zulip):

Thanks I'll check that out

dlrobertson (Dec 21 2018 at 15:59, on Zulip):

I haven't looked into PassMode yet, so I'll have to read the code

dlrobertson (Dec 21 2018 at 16:35, on Zulip):

FWIW my current progress is here https://github.com/dlrobertson/rust/tree/varargs0

dlrobertson (Dec 21 2018 at 17:21, on Zulip):

@Oli After looking at PassMode, it definitely seems like the right direction. Thanks

dlrobertson (Dec 21 2018 at 17:21, on Zulip):

In retrospect it seems so obvious to look at what is done for zst's :smiley:

Last update: Nov 22 2019 at 05:35UTC