attempt at using VarArgs annotation for variable arguments list#381
attempt at using VarArgs annotation for variable arguments list#381Alain-Bearez wants to merge 2 commits intobedatadriven:masterfrom
Conversation
| return snprintf(string, Integer.MAX_VALUE, format, arguments); | ||
| } | ||
|
|
||
| public static int vsnprintf(BytePtr string, int limit, BytePtr format, @VarArgs OpaquePtr<?> arguments) { |
There was a problem hiding this comment.
The compile currently supports two stratgies to variadic functions:
The compiler takes any extra arguments, allocates an Object[] array for them, and passes them to the final argument of a method with the signature Object... args. This is what you see in the implementations of sprintf, etc. in this class.
The compiler takes any extra arguments to a function, and allocates a VPtr block into which their values are copied, and passes this to a final argument with the signature @VarArgs VPtr arguments. This more closely approximates how variadic args are implemented in the C memory model, and so we can fully support variadic functions written in C/C++.
The first strategy was really just a quick and dirty way to get printf() and friends working. At some point we'll need a better implementation that more closely follows the C standard for printf. Currently, we just pass the arguments we get to Java's String.format(), which doesn't support %p or many other format specifiers.
I've looked at simply importing the C implementation of these functions from a C Standard Library implementation like musl, but they tend to be tightly coupled to the library's I/O implementation. The other option is to use Renjin's Formatter class as a starting point, perhaps pulling it up into the gcc-runtime module and than extracting some kind of ArgumentAccessor interface so that it can work with input from SEXPs or from VPtrs.
In any case, the quick solution here is to simply declare vsnprintf as:
public static int vsnprintf(Ptr string, int limit, Ptr format, Object... arguments)Please also add a test case to varargs.c. These tests are run by GimpleCompilerTest.varArgCalls.
fb18e3d to
1ba3433
Compare
ef9d981 to
efef981
Compare
I could not find an example of function that would already be using a variable arguments list as parameter.
What would the correct approach be at that point?