LLVM's va_arg instruction / Clang's __builtin_va_arg() function

390 views
Skip to first unread message

Alex Rønne Petersen

unread,
Jul 7, 2012, 11:15:53 PM7/7/12
to ldc...@googlegroups.com
Hey,

LLVM has a va_arg instruction while Clang has the __builtin_va_arg()
function (like GCC/GDC).

We really need access to either of these in D code compiled with LDC;
it's the most portable and maintainable way to implement va_arg() in
core.vararg.

I would seriously appreciate it if someone could think of a clever way
that this could be done, because reimplementing va_arg by hand for
every target is not feasible.

Regards,
Alex

Alex Rønne Petersen

unread,
Jul 7, 2012, 11:18:26 PM7/7/12
to ldc...@googlegroups.com
Sorry, core.stdc.stdarg, not core.vararg.

Regards,
Alex

David Nadlinger

unread,
Jul 8, 2012, 12:28:55 AM7/8/12
to ldc...@googlegroups.com
On 8 Jul 2012, at 5:15, Alex Rønne Petersen wrote:
> I would seriously appreciate it if someone could think of a clever way
> that this could be done, because reimplementing va_arg by hand for
> every target is not feasible.

Unfortunately, that's pretty much the only option right now, and what
Clang does – LLVM's va_arg instruction does not work with aggregate
types (see the Language Reference). Why? I'm not sure…

We also can't just borrow Clang's implementation, because behind the
scenes it recognizes __builtin_va_arg calls are lowers them into a
series of LLVM instructions. The code depends on Clang data structures
to get the necessary type information (as opposed to the "lowered" LLVM
types). Thus, the code would need to be adapted quite a bit anyway, and
as actual code in D form, it's much more readable than code for
synthesizing LLVM instructions, at least for complex ABIs like System V
x86_64. This is an option which simply isn't feasible in C/C++ due to
the lack of expressive enough metaprogramming features…

I have a rewrite of varargs on x86 in the pipe (so that they actually
work and conform to the C ABI), but I can't see a way how this could be
changed.

David

David Nadlinger

unread,
Jul 8, 2012, 12:31:13 AM7/8/12
to ldc...@googlegroups.com
On 8 Jul 2012, at 5:18, Alex Rønne Petersen wrote:
> Sorry, core.stdc.stdarg, not core.vararg.

They should be the same (the latter just forwarding to the former). The
fact that they aren't right now is just an artifact of the state of
LDC's x86_64 vararg implementation, and will change in the future.

David
Reply all
Reply to author
Forward
0 new messages