Skip to content

Commit 832622c

Browse files
committed
Update HPy inlined files: 5ddcbc8d
1 parent 1cadcd7 commit 832622c

File tree

109 files changed

+13060
-3038
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

109 files changed

+13060
-3038
lines changed

graalpython/com.oracle.graal.python.hpy.llvm/include/hpy.h

Lines changed: 120 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,59 @@
44
extern "C" {
55
#endif
66

7-
#ifndef HPY_UNIVERSAL_ABI
8-
/* It would be nice if we could include hpy.h WITHOUT bringing in all the
9-
stuff from Python.h, to make sure that people don't use the CPython API by
10-
mistake. How to achieve it, though? */
7+
/* ~~~~~~~~~~~~~~~~ HPy ABI version ~~~~~~~~~~~~~~~ */
8+
// NOTE: these must be kept on sync with the equivalent variables in hpy/devel/abitag.py
9+
/**
10+
* The ABI version.
11+
*
12+
* Minor version N+1 is binary compatible to minor version N. Major versions
13+
* are not binary compatible (note: HPy can run several binary incompatible
14+
* versions in one process).
15+
*/
16+
#define HPY_ABI_VERSION 0
17+
#define HPY_ABI_VERSION_MINOR 0
18+
#define HPY_ABI_TAG "hpy0"
19+
20+
21+
/* ~~~~~~~~~~~~~~~~ HPy ABI macros ~~~~~~~~~~~~~~~~ */
22+
23+
/* The following macros are used to determine which kind of module we want to
24+
compile. The build system must set one of these (e.g. by using `gcc
25+
-D...`). This is the approach used by the setuptools support provided by
26+
hpy.devel:
27+
28+
- HPY_ABI_CPYTHON
29+
- HPY_ABI_UNIVERSAL
30+
- HPY_ABI_HYBRID
31+
32+
In addition we also define HPY_ABI which is a string literal containing a
33+
string representation of it.
34+
*/
35+
36+
#if defined(HPY_ABI_CPYTHON)
37+
# if defined(HPY_ABI_HYBRID)
38+
# error "Conflicting macros are defined: HPY_ABI_CPYTHON and HPY_ABI_HYBRID"
39+
# endif
40+
# if defined(HPY_ABI_UNIVERSAL)
41+
# error "Conflicting macros are defined: HPY_ABI_CPYTHON and HPY_ABI_UNIVERSAL"
42+
# endif
43+
# define HPY_ABI "cpython"
44+
45+
#elif defined(HPY_ABI_HYBRID)
46+
# if defined(HPY_ABI_UNIVERSAL)
47+
# error "Conflicting macros are defined: HPY_ABI_HYBRID and HPY_ABI_UNIVERSAL"
48+
# endif
49+
# define HPY_ABI "hybrid"
50+
51+
#elif defined(HPY_ABI_UNIVERSAL)
52+
# define HPY_ABI "universal"
53+
54+
#else
55+
# error "Cannot determine the desired HPy ABI: you must set one of HPY_ABI_CPYTHON, HPY_ABI_UNIVERSAL or HPY_ABI_HYBRID"
56+
#endif
57+
58+
59+
#if defined(HPY_ABI_CPYTHON) || defined(HPY_ABI_HYBRID)
1160
# define PY_SSIZE_T_CLEAN
1261
# include <Python.h>
1362
#endif
@@ -25,6 +74,7 @@ extern "C" {
2574
# define _HPy_HIDDEN
2675
# define _HPy_UNUSED
2776
#endif /* __GNUC__ */
77+
#define _HPy_UNUSED_ARG(x) (__HPy_UNUSED_TAGGED ## x) _HPy_UNUSED
2878

2979
#if defined(__clang__) || \
3080
(defined(__GNUC__) && \
@@ -37,6 +87,28 @@ extern "C" {
3787
# define _HPy_NO_RETURN
3888
#endif
3989

90+
91+
// clang and gcc supports __has_attribute, MSVC doesn't. This should be enough
92+
// to be able to use it portably
93+
#ifdef __has_attribute
94+
# define _HPY_compiler_has_attribute(x) __has_attribute(x)
95+
#else
96+
# define _HPY_compiler_has_attribute(x) 0
97+
#endif
98+
99+
#ifdef HPY_ABI_UNIVERSAL
100+
# if _HPY_compiler_has_attribute(error)
101+
// gcc, clang>=14
102+
# define _HPY_LEGACY __attribute__((error("Cannot use legacy functions when targeting the HPy Universal ABI")))
103+
# else
104+
// we don't have any diagnostic feature, too bad
105+
# define _HPY_LEGACY
106+
# endif
107+
#else
108+
// in non-universal modes, we don't attach any attribute
109+
# define _HPY_LEGACY
110+
#endif
111+
40112
#if defined(_MSC_VER) && defined(__cplusplus) // MSVC C4576
41113
# define _hconv(h) {h}
42114
# define _hfconv(h) {h}
@@ -67,6 +139,9 @@ extern "C" {
67139
*/
68140
#define HPyAPI_FUNC _HPy_UNUSED static inline
69141

142+
/** An alias for ``HPyAPI_FUNC`` so we can handle it properly in the docs. */
143+
#define HPyAPI_INLINE_HELPER HPyAPI_FUNC
144+
70145
/**
71146
* CPython implementations for ``HPyAPI_FUNC``
72147
* functions. Generally speaking, they are put in ctx_*.c files and they are
@@ -104,7 +179,8 @@ extern "C" {
104179
105180
- PyPy: ._i is an index into a list
106181
107-
- GraalPython: ???
182+
- GraalPy: ._i is a tagged value, either an index into a list,
183+
or an immediate integer or double value
108184
109185
- Debug mode: _i is a pointer to a DebugHandle, which contains a
110186
another HPy among other stuff
@@ -123,15 +199,12 @@ typedef struct { intptr_t _i; } HPyThreadState;
123199
#define HPy_NULL _hconv(0)
124200
#define HPy_IsNull(h) ((h)._i == 0)
125201

126-
#define HPyListBuilder_IsNull(h) ((h)._lst == 0)
127-
#define HPyTupleBuilder_IsNull(h) ((h)._tup == 0)
128-
129202
#define HPyField_NULL _hfconv(0)
130203
#define HPyField_IsNull(f) ((f)._i == 0)
131204

132205
/* Convenience functions to cast between HPy and void*. We need to decide
133206
whether these are part of the official API or not, and maybe introduce a
134-
better naming convetion. For now, they are needed for ujson. */
207+
better naming convention. For now, they are needed for ujson. */
135208
static inline HPy HPy_FromVoidP(void *p) { return _hconv((intptr_t)p); }
136209
static inline void* HPy_AsVoidP(HPy h) { return (void*)h._i; }
137210

@@ -140,51 +213,75 @@ static inline void* HPy_AsVoidP(HPy h) { return (void*)h._i; }
140213

141214
typedef struct _HPyContext_s HPyContext;
142215

143-
#ifdef HPY_UNIVERSAL_ABI
216+
/** An enumeration of the different kinds of source code strings. */
217+
typedef enum {
218+
/** Parse isolated expressions (e.g. ``a + b``). */
219+
HPy_SourceKind_Expr = 0,
220+
221+
/**
222+
* Parse sequences of statements as read from a file or other source. This
223+
* is the symbol to use when compiling arbitrarily long Python source code.
224+
*/
225+
HPy_SourceKind_File = 1,
226+
227+
/**
228+
* Parse a single statement. This is the mode used for the interactive
229+
* interpreter loop.
230+
*/
231+
HPy_SourceKind_Single = 2,
232+
} HPy_SourceKind;
233+
234+
#ifdef HPY_ABI_CPYTHON
235+
typedef Py_ssize_t HPy_ssize_t;
236+
typedef Py_hash_t HPy_hash_t;
237+
typedef Py_UCS4 HPy_UCS4;
238+
239+
# define HPY_SSIZE_T_MAX PY_SSIZE_T_MAX
240+
# define HPY_SSIZE_T_MIN PY_SSIZE_T_MIN
241+
242+
#else
144243
typedef intptr_t HPy_ssize_t;
145244
typedef intptr_t HPy_hash_t;
146245
typedef uint32_t HPy_UCS4;
147246

247+
# define HPY_SSIZE_T_MAX INTPTR_MAX
248+
# define HPY_SSIZE_T_MIN (-HPY_SSIZE_T_MAX-1)
249+
148250
/* HPyCapsule field keys */
149251
typedef enum {
150252
HPyCapsule_key_Pointer = 0,
151253
HPyCapsule_key_Name = 1,
152254
HPyCapsule_key_Context = 2,
153255
HPyCapsule_key_Destructor = 3,
154256
} _HPyCapsule_key;
155-
156-
#else
157-
typedef Py_ssize_t HPy_ssize_t;
158-
typedef Py_hash_t HPy_hash_t;
159-
typedef Py_UCS4 HPy_UCS4;
160257
#endif
161258

162-
typedef void (*HPyCapsule_Destructor)(const char *name, void *pointer, void *context);
163-
164259

165260
/* ~~~~~~~~~~~~~~~~ Additional #includes ~~~~~~~~~~~~~~~~ */
166261

167262
#include "hpy/cpy_types.h"
263+
#include "hpy/hpyexports.h"
168264
#include "hpy/macros.h"
169265
#include "hpy/hpyfunc.h"
170266
#include "hpy/hpydef.h"
171267
#include "hpy/hpytype.h"
172268
#include "hpy/hpymodule.h"
173269
#include "hpy/runtime/argparse.h"
174270
#include "hpy/runtime/buildvalue.h"
271+
#include "hpy/runtime/format.h"
175272
#include "hpy/runtime/helpers.h"
176273
#include "hpy/runtime/structseq.h"
177274

178-
#ifdef HPY_UNIVERSAL_ABI
179-
# include "hpy/universal/autogen_ctx.h"
180-
# include "hpy/universal/autogen_trampolines.h"
181-
# include "hpy/universal/misc_trampolines.h"
182-
#else
183-
// CPython-ABI
275+
#ifdef HPY_ABI_CPYTHON
276+
# include "hpy/cpython/autogen_ctx.h"
184277
# include "hpy/runtime/ctx_funcs.h"
185278
# include "hpy/runtime/ctx_type.h"
186279
# include "hpy/cpython/misc.h"
187280
# include "hpy/cpython/autogen_api_impl.h"
281+
#else
282+
# include "hpy/universal/autogen_ctx.h"
283+
# include "hpy/universal/autogen_trampolines.h"
284+
# include "hpy/universal/misc_trampolines.h"
188285
#endif
189286

190287
#include "hpy/inline_helpers.h"

graalpython/com.oracle.graal.python.hpy.llvm/include/hpy/autogen_hpyfunc_declare.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313
#define _HPyFunc_DECLARE_HPyFunc_NOARGS(SYM) static HPy SYM(HPyContext *ctx, HPy self)
1414
#define _HPyFunc_DECLARE_HPyFunc_O(SYM) static HPy SYM(HPyContext *ctx, HPy self, HPy arg)
15-
#define _HPyFunc_DECLARE_HPyFunc_VARARGS(SYM) static HPy SYM(HPyContext *ctx, HPy self, HPy *args, HPy_ssize_t nargs)
16-
#define _HPyFunc_DECLARE_HPyFunc_KEYWORDS(SYM) static HPy SYM(HPyContext *ctx, HPy self, HPy *args, HPy_ssize_t nargs, HPy kw)
15+
#define _HPyFunc_DECLARE_HPyFunc_VARARGS(SYM) static HPy SYM(HPyContext *ctx, HPy self, const HPy *args, size_t nargs)
16+
#define _HPyFunc_DECLARE_HPyFunc_KEYWORDS(SYM) static HPy SYM(HPyContext *ctx, HPy self, const HPy *args, size_t nargs, HPy kwnames)
1717
#define _HPyFunc_DECLARE_HPyFunc_UNARYFUNC(SYM) static HPy SYM(HPyContext *ctx, HPy)
1818
#define _HPyFunc_DECLARE_HPyFunc_BINARYFUNC(SYM) static HPy SYM(HPyContext *ctx, HPy, HPy)
1919
#define _HPyFunc_DECLARE_HPyFunc_TERNARYFUNC(SYM) static HPy SYM(HPyContext *ctx, HPy, HPy, HPy)
@@ -36,7 +36,8 @@
3636
#define _HPyFunc_DECLARE_HPyFunc_ITERNEXTFUNC(SYM) static HPy SYM(HPyContext *ctx, HPy)
3737
#define _HPyFunc_DECLARE_HPyFunc_DESCRGETFUNC(SYM) static HPy SYM(HPyContext *ctx, HPy, HPy, HPy)
3838
#define _HPyFunc_DECLARE_HPyFunc_DESCRSETFUNC(SYM) static int SYM(HPyContext *ctx, HPy, HPy, HPy)
39-
#define _HPyFunc_DECLARE_HPyFunc_INITPROC(SYM) static int SYM(HPyContext *ctx, HPy self, HPy *args, HPy_ssize_t nargs, HPy kw)
39+
#define _HPyFunc_DECLARE_HPyFunc_INITPROC(SYM) static int SYM(HPyContext *ctx, HPy self, const HPy *args, HPy_ssize_t nargs, HPy kw)
40+
#define _HPyFunc_DECLARE_HPyFunc_NEWFUNC(SYM) static HPy SYM(HPyContext *ctx, HPy type, const HPy *args, HPy_ssize_t nargs, HPy kw)
4041
#define _HPyFunc_DECLARE_HPyFunc_GETTER(SYM) static HPy SYM(HPyContext *ctx, HPy, void *)
4142
#define _HPyFunc_DECLARE_HPyFunc_SETTER(SYM) static int SYM(HPyContext *ctx, HPy, HPy, void *)
4243
#define _HPyFunc_DECLARE_HPyFunc_OBJOBJPROC(SYM) static int SYM(HPyContext *ctx, HPy, HPy)
@@ -45,11 +46,12 @@
4546
#define _HPyFunc_DECLARE_HPyFunc_TRAVERSEPROC(SYM) static int SYM(void *object, HPyFunc_visitproc visit, void *arg)
4647
#define _HPyFunc_DECLARE_HPyFunc_DESTRUCTOR(SYM) static void SYM(HPyContext *ctx, HPy)
4748
#define _HPyFunc_DECLARE_HPyFunc_DESTROYFUNC(SYM) static void SYM(void *)
49+
#define _HPyFunc_DECLARE_HPyFunc_MOD_CREATE(SYM) static HPy SYM(HPyContext *ctx, HPy)
4850

4951
typedef HPy (*HPyFunc_noargs)(HPyContext *ctx, HPy self);
5052
typedef HPy (*HPyFunc_o)(HPyContext *ctx, HPy self, HPy arg);
51-
typedef HPy (*HPyFunc_varargs)(HPyContext *ctx, HPy self, HPy *args, HPy_ssize_t nargs);
52-
typedef HPy (*HPyFunc_keywords)(HPyContext *ctx, HPy self, HPy *args, HPy_ssize_t nargs, HPy kw);
53+
typedef HPy (*HPyFunc_varargs)(HPyContext *ctx, HPy self, const HPy *args, size_t nargs);
54+
typedef HPy (*HPyFunc_keywords)(HPyContext *ctx, HPy self, const HPy *args, size_t nargs, HPy kwnames);
5355
typedef HPy (*HPyFunc_unaryfunc)(HPyContext *ctx, HPy);
5456
typedef HPy (*HPyFunc_binaryfunc)(HPyContext *ctx, HPy, HPy);
5557
typedef HPy (*HPyFunc_ternaryfunc)(HPyContext *ctx, HPy, HPy, HPy);
@@ -72,7 +74,8 @@ typedef HPy (*HPyFunc_getiterfunc)(HPyContext *ctx, HPy);
7274
typedef HPy (*HPyFunc_iternextfunc)(HPyContext *ctx, HPy);
7375
typedef HPy (*HPyFunc_descrgetfunc)(HPyContext *ctx, HPy, HPy, HPy);
7476
typedef int (*HPyFunc_descrsetfunc)(HPyContext *ctx, HPy, HPy, HPy);
75-
typedef int (*HPyFunc_initproc)(HPyContext *ctx, HPy self, HPy *args, HPy_ssize_t nargs, HPy kw);
77+
typedef int (*HPyFunc_initproc)(HPyContext *ctx, HPy self, const HPy *args, HPy_ssize_t nargs, HPy kw);
78+
typedef HPy (*HPyFunc_newfunc)(HPyContext *ctx, HPy type, const HPy *args, HPy_ssize_t nargs, HPy kw);
7679
typedef HPy (*HPyFunc_getter)(HPyContext *ctx, HPy, void *);
7780
typedef int (*HPyFunc_setter)(HPyContext *ctx, HPy, HPy, void *);
7881
typedef int (*HPyFunc_objobjproc)(HPyContext *ctx, HPy, HPy);
@@ -81,3 +84,4 @@ typedef void (*HPyFunc_releasebufferproc)(HPyContext *ctx, HPy, HPy_buffer *);
8184
typedef int (*HPyFunc_traverseproc)(void *object, HPyFunc_visitproc visit, void *arg);
8285
typedef void (*HPyFunc_destructor)(HPyContext *ctx, HPy);
8386
typedef void (*HPyFunc_destroyfunc)(void *);
87+
typedef HPy (*HPyFunc_mod_create)(HPyContext *ctx, HPy);

graalpython/com.oracle.graal.python.hpy.llvm/include/hpy/autogen_hpyslot.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,19 @@ typedef enum {
5858
HPy_sq_length = 45,
5959
HPy_sq_repeat = 46,
6060
HPy_tp_call = 50,
61+
HPy_tp_hash = 59,
6162
HPy_tp_init = 60,
62-
HPy_tp_iter = 62,
6363
HPy_tp_new = 65,
6464
HPy_tp_repr = 66,
6565
HPy_tp_richcompare = 67,
66+
HPy_tp_str = 70,
6667
HPy_tp_traverse = 71,
6768
HPy_nb_matrix_multiply = 75,
6869
HPy_nb_inplace_matrix_multiply = 76,
6970
HPy_tp_finalize = 80,
7071
HPy_tp_destroy = 1000,
72+
HPy_mod_create = 2000,
73+
HPy_mod_exec = 2001,
7174
} HPySlot_Slot;
7275

7376
#define _HPySlot_SIG__HPy_bf_getbuffer HPyFunc_GETBUFFERPROC
@@ -117,13 +120,16 @@ typedef enum {
117120
#define _HPySlot_SIG__HPy_sq_length HPyFunc_LENFUNC
118121
#define _HPySlot_SIG__HPy_sq_repeat HPyFunc_SSIZEARGFUNC
119122
#define _HPySlot_SIG__HPy_tp_call HPyFunc_KEYWORDS
123+
#define _HPySlot_SIG__HPy_tp_hash HPyFunc_HASHFUNC
120124
#define _HPySlot_SIG__HPy_tp_init HPyFunc_INITPROC
121-
#define _HPySlot_SIG__HPy_tp_iter HPyFunc_GETITERFUNC
122-
#define _HPySlot_SIG__HPy_tp_new HPyFunc_KEYWORDS
125+
#define _HPySlot_SIG__HPy_tp_new HPyFunc_NEWFUNC
123126
#define _HPySlot_SIG__HPy_tp_repr HPyFunc_REPRFUNC
124127
#define _HPySlot_SIG__HPy_tp_richcompare HPyFunc_RICHCMPFUNC
128+
#define _HPySlot_SIG__HPy_tp_str HPyFunc_REPRFUNC
125129
#define _HPySlot_SIG__HPy_tp_traverse HPyFunc_TRAVERSEPROC
126130
#define _HPySlot_SIG__HPy_nb_matrix_multiply HPyFunc_BINARYFUNC
127131
#define _HPySlot_SIG__HPy_nb_inplace_matrix_multiply HPyFunc_BINARYFUNC
128132
#define _HPySlot_SIG__HPy_tp_finalize HPyFunc_DESTRUCTOR
129133
#define _HPySlot_SIG__HPy_tp_destroy HPyFunc_DESTROYFUNC
134+
#define _HPySlot_SIG__HPy_mod_create HPyFunc_MOD_CREATE
135+
#define _HPySlot_SIG__HPy_mod_exec HPyFunc_INQUIRY
Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,49 @@
11
#ifndef HPY_UNIVERSAL_CPY_TYPES_H
22
#define HPY_UNIVERSAL_CPY_TYPES_H
33

4-
/* generally speaking, we can't #include Python.h, but there are a bunch of
5-
* types defined there that we need to use. Here, we redefine all the types
6-
* we need, with a cpy_ prefix.
7-
*/
8-
9-
typedef struct _object cpy_PyObject;
10-
typedef cpy_PyObject *(*cpy_PyCFunction)(cpy_PyObject *, cpy_PyObject *);
11-
typedef struct PyMethodDef cpy_PyMethodDef;
12-
typedef struct bufferinfo cpy_Py_buffer;
4+
/* ~~~~~~~~~~~~~~~~ CPython legacy types ~~~~~~~~~~~~~~~~ */
5+
6+
/* These are the types which are needed to implement legacy features such as
7+
.legacy_slots, .legacy_methods, HPy_FromPyObject, HPy_AsPyObject, etc.
8+
9+
In cpython and hybrid ABI mode we can #include Python.h and use the "real"
10+
types.
11+
12+
In universal ABI mode, legacy features cannot be used, but we still need
13+
the corresponding C types to use in the HPy declarations. Note that we use
14+
only forward declarations, meaning that it will actually be impossible to
15+
instantiate any of these struct.
16+
*/
17+
18+
#ifdef HPY_ABI_UNIVERSAL
19+
20+
typedef struct FORBIDDEN_cpy_PyObject cpy_PyObject;
21+
typedef struct FORBIDDEN_PyMethodDef cpy_PyMethodDef;
22+
typedef struct FORBIDDEN_PyModuleDef cpy_PyModuleDef;
23+
typedef struct FORBIDDEN_bufferinfo cpy_Py_buffer;
24+
25+
// declare the following API functions as _HPY_LEGACY, which triggers an
26+
// #error if they are used
27+
HPyAPI_FUNC _HPY_LEGACY cpy_PyObject *HPy_AsPyObject(HPyContext *ctx, HPy h);
28+
HPyAPI_FUNC _HPY_LEGACY HPy HPy_FromPyObject(HPyContext *ctx, cpy_PyObject *obj);
29+
30+
#else
31+
32+
// Python.h has already been included by the main hpy.h
33+
typedef PyObject cpy_PyObject;
34+
typedef PyMethodDef cpy_PyMethodDef;
35+
typedef PyModuleDef cpy_PyModuleDef;
36+
typedef Py_buffer cpy_Py_buffer;
37+
38+
#endif /* HPY_ABI_UNIVERSAL */
39+
40+
41+
typedef cpy_PyObject *(*cpy_PyCFunction)(cpy_PyObject *, cpy_PyObject *const *, HPy_ssize_t);
1342
typedef int (*cpy_visitproc)(cpy_PyObject *, void *);
14-
typedef cpy_PyObject *(*cpy_PyCFunction)(cpy_PyObject *, cpy_PyObject *);
1543
typedef cpy_PyObject *(*cpy_getter)(cpy_PyObject *, void *);
1644
typedef int (*cpy_setter)(cpy_PyObject *, cpy_PyObject *, void *);
17-
45+
typedef void (*cpy_PyCapsule_Destructor)(cpy_PyObject *);
46+
typedef cpy_PyObject *(*cpy_vectorcallfunc)(cpy_PyObject *callable, cpy_PyObject *const *args,
47+
size_t nargsf, cpy_PyObject *kwnames);
1848

1949
#endif /* HPY_UNIVERSAL_CPY_TYPES_H */

0 commit comments

Comments
 (0)