Skip to content

Commit 4148ca1

Browse files
committed
[GR-25191] [GR-25747] Directly access valist when parsing arguments.
PullRequest: graalpython/1225
2 parents c25750c + d9c8b33 commit 4148ca1

File tree

14 files changed

+467
-450
lines changed

14 files changed

+467
-450
lines changed

graalpython/com.oracle.graal.python.cext/src/_warnings.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -70,12 +70,18 @@ int PyErr_WarnEx(PyObject *category, const char *text, Py_ssize_t stack_level) {
7070
}
7171

7272
NO_INLINE int PyErr_WarnFormat(PyObject *category, Py_ssize_t stack_level, const char *format, ...) {
73-
CallWithPolyglotArgs(PyObject* result, format, 3, PyTruffle_Unicode_FromFormat, format);
73+
va_list args;
74+
va_start(args, format);
75+
PyObject* result = PyTruffle_Unicode_FromFormat(format, args);
76+
va_end(args);
7477
return warn_unicode(category, result, stack_level, Py_None);
7578
}
7679

7780
int PyErr_ResourceWarning(PyObject *source, Py_ssize_t stack_level, const char *format, ...) {
78-
CallWithPolyglotArgs(PyObject* result, format, 3, PyTruffle_Unicode_FromFormat, format);
81+
va_list args;
82+
va_start(args, format);
83+
PyObject* result = PyTruffle_Unicode_FromFormat(format, args);
84+
va_end(args);
7985
return warn_unicode(PyExc_ResourceWarning, result, stack_level, source);
8086
}
8187

graalpython/com.oracle.graal.python.cext/src/capi.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ initialize_type(_PyWeakref_CallableProxyType, CallableProxyType, PyWeakReference
199199

200200
POLYGLOT_DECLARE_TYPE(PyThreadState);
201201
POLYGLOT_DECLARE_TYPE(newfunc);
202-
POLYGLOT_DECLARE_TYPE(uint32_t);
202+
203203

204204
static void initialize_globals() {
205205
// register native NULL
@@ -349,6 +349,7 @@ polyglot_typeid get_byte_array_typeid(uint64_t len) {
349349
return polyglot_array_typeid(polyglot_i8_typeid(), len);
350350
}
351351

352+
POLYGLOT_DECLARE_TYPE(uint32_t);
352353
/** to be used from Java code only; returns the type ID for a uint32_t array */
353354
polyglot_typeid get_uint32_t_array_typeid(uint64_t len) {
354355
return polyglot_array_typeid(polyglot_uint32_t_typeid(), len);
@@ -701,8 +702,6 @@ int PyTruffle_Debug(void *arg) {
701702
return 0;
702703
}
703704

704-
#define ARG(__n) ((PyObject*)polyglot_get_arg((__n)))
705-
706705
int truffle_ptr_compare(void* x, void* y, int op) {
707706
switch (op) {
708707
case Py_LT:

graalpython/com.oracle.graal.python.cext/src/capi.h

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,7 @@ extern PyObject* wrapped_null;
379379
/* internal functions to avoid unnecessary managed <-> native conversions */
380380

381381
/* STR */
382-
__attribute__((always_inline)) PyObject* PyTruffle_Unicode_FromFormat(const char *fmt, va_list va, void **args, int argc);
383-
__attribute__((always_inline)) PyObject* PyTruffle_Tuple_Pack(int dummy, va_list va, void **args, int argc);
382+
__attribute__((always_inline)) PyObject* PyTruffle_Unicode_FromFormat(const char *fmt, va_list va);
384383

385384
/* BYTES, BYTEARRAY */
386385
int bytes_buffer_getbuffer(PyBytesObject *self, Py_buffer *view, int flags);
@@ -392,38 +391,6 @@ int bytes_copy2mem(char* target, char* source, size_t nbytes);
392391
/* MEMORYVIEW, BUFFERDECORATOR */
393392
int bufferdecorator_getbuffer(PyBufferDecorator *self, Py_buffer *view, int flags);
394393

395-
#if 1
396-
/*
397-
* (tfel): On native Sulong, using va_list will force all arguments to native
398-
* memory, which hinders escape analysis and PE in a big way. To avoid this,
399-
* when we have function called with var args (rather than already with a
400-
* va_list), we allocate a managed array of void*, fill it with the arguments,
401-
* and pass that one on. In the target functions, we use the macros below to
402-
* access the variable arguments part depending on whether it is a va_list or a
403-
* managed void* array. The assumption is that once everything is compiled
404-
* together, the managed array with arguments will be escape analyzed away.
405-
*/
406-
#define CallWithPolyglotArgs(result, last, off, function, ...) \
407-
va_list __va_list; \
408-
int __poly_argc = polyglot_get_arg_count(); \
409-
int __poly_args_s = sizeof(void*) * (__poly_argc - off); \
410-
void **__poly_args = truffle_managed_malloc(__poly_args_s); \
411-
for (int i = off; i < __poly_argc; i++) { \
412-
__poly_args[i - off] = polyglot_get_arg(i); \
413-
} \
414-
result = function(__VA_ARGS__, __va_list, __poly_args, __poly_argc)
415-
#else
416-
/*
417-
* (tfel): Just skip the optimization with using a managed malloc and use
418-
* va_list always.
419-
*/
420-
#define CallWithPolyglotArgs(result, last, off, function, ...) \
421-
va_list __poly_args; \
422-
va_start(__poly_args, last); \
423-
result = function(__VA_ARGS__, __poly_args, NULL, 0); \
424-
va_end(__poly_args)
425-
#endif
426-
427394
typedef PyObject* PyObjectPtr;
428395
POLYGLOT_DECLARE_TYPE(PyObjectPtr);
429396

graalpython/com.oracle.graal.python.cext/src/errors.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -161,7 +161,10 @@ int PyErr_ExceptionMatches(PyObject *exc) {
161161

162162
NO_INLINE
163163
PyObject* PyErr_Format(PyObject* exception, const char* fmt, ...) {
164-
CallWithPolyglotArgs(PyObject* formatted_msg, fmt, 2, PyTruffle_Unicode_FromFormat, fmt);
164+
va_list args;
165+
va_start(args, fmt);
166+
PyObject* formatted_msg = PyTruffle_Unicode_FromFormat(fmt, args);
167+
va_end(args);
165168
UPCALL_CEXT_VOID(_jls_PyErr_CreateAndSetException, native_to_java(exception), native_to_java(formatted_msg));
166169
return NULL;
167170
}

0 commit comments

Comments
 (0)