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

attempt at using VarArgs annotation for variable arguments list #381

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Alain-Bearez
Copy link
Contributor

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?

@@ -251,6 +252,11 @@ public static int sprintf(BytePtr string, BytePtr format, Object... arguments) {
return snprintf(string, Integer.MAX_VALUE, format, arguments);
}

public static int vsnprintf(BytePtr string, int limit, BytePtr format, @VarArgs OpaquePtr<?> arguments) {
Copy link
Member

Choose a reason for hiding this comment

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

The compile currently supports two stratgies to variadic functions:

JvmVarArgsStrategy

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.

VPtrVariadicStrategy

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.

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.

2 participants