Skip to content

Commit b3270f6

Browse files
committed
[GR-44391] Share constant arrays between LLVM and native C API backend.
PullRequest: graalpython/2649
2 parents a7b3063 + 22aa92d commit b3270f6

File tree

6 files changed

+374
-299
lines changed

6 files changed

+374
-299
lines changed

graalpython/com.oracle.graal.python.cext/modules/_testcapi.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,10 @@ test_L_code(PyObject *self, PyObject *Py_UNUSED(ignored))
973973
if (num == NULL)
974974
return NULL;
975975

976+
// GraalPy change: for explanation, see similar comment in 'test_k_code'
977+
#ifdef GRAALVM_PYTHON
978+
Py_INCREF(num);
979+
#endif
976980
PyTuple_SET_ITEM(tuple, 0, num);
977981

978982
value = -1;
@@ -1362,6 +1366,15 @@ test_k_code(PyObject *self, PyObject *Py_UNUSED(ignored))
13621366
return raiseTestError("test_k_code",
13631367
"PyLong_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF");
13641368

1369+
/*
1370+
* GraalPy change: CPython's tuple is stealing the reference to 'num' which
1371+
* means it just does nothing. In our case, we are internally decref'ing
1372+
* since it is then reference managed. This makes 'num' invalid for the
1373+
* later usage in Py_DECREF. We therefore incref once here.
1374+
*/
1375+
#ifdef GRAALVM_PYTHON
1376+
Py_INCREF(num);
1377+
#endif
13651378
PyTuple_SET_ITEM(tuple, 0, num);
13661379

13671380
value = 0;

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,12 @@ PyTypeObject PyWrapperDescr_Type = PY_TRUFFLE_TYPE("wrapper_descriptor", &PyT
155155
// dummy definitions:
156156
PyTypeObject _PyBytesIOBuffer_Type = PY_TRUFFLE_TYPE("_io._BytesIOBuffer", &PyType_Type, Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, 0);
157157

158+
/*
159+
* This header includes definitions for constant arrays like:
160+
* _Py_ascii_whitespace, _Py_ctype_table, _Py_ctype_tolower, _Py_ctype_toupper.
161+
*/
162+
#include "const_arrays.h"
163+
158164
#define BUILTIN(NAME, RET, ...) RET (*Graal##NAME)(__VA_ARGS__);
159165
CAPI_BUILTINS
160166
#undef BUILTIN
Lines changed: 296 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,296 @@
1+
/*
2+
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
42+
#ifndef SRC_CONST_ARRAYS_H_
43+
#define SRC_CONST_ARRAYS_H_
44+
45+
/******************************************************************************
46+
* imported from 'unicodeobject.c'
47+
******************************************************************************/
48+
49+
// partially taken from CPython "Objects/unicodeobject.c"
50+
const unsigned char _Py_ascii_whitespace[] = {
51+
0, 0, 0, 0, 0, 0, 0, 0,
52+
/* case 0x0009: * CHARACTER TABULATION */
53+
/* case 0x000A: * LINE FEED */
54+
/* case 0x000B: * LINE TABULATION */
55+
/* case 0x000C: * FORM FEED */
56+
/* case 0x000D: * CARRIAGE RETURN */
57+
0, 1, 1, 1, 1, 1, 0, 0,
58+
0, 0, 0, 0, 0, 0, 0, 0,
59+
/* case 0x001C: * FILE SEPARATOR */
60+
/* case 0x001D: * GROUP SEPARATOR */
61+
/* case 0x001E: * RECORD SEPARATOR */
62+
/* case 0x001F: * UNIT SEPARATOR */
63+
0, 0, 0, 0, 1, 1, 1, 1,
64+
/* case 0x0020: * SPACE */
65+
1, 0, 0, 0, 0, 0, 0, 0,
66+
0, 0, 0, 0, 0, 0, 0, 0,
67+
0, 0, 0, 0, 0, 0, 0, 0,
68+
0, 0, 0, 0, 0, 0, 0, 0,
69+
70+
0, 0, 0, 0, 0, 0, 0, 0,
71+
0, 0, 0, 0, 0, 0, 0, 0,
72+
0, 0, 0, 0, 0, 0, 0, 0,
73+
0, 0, 0, 0, 0, 0, 0, 0,
74+
0, 0, 0, 0, 0, 0, 0, 0,
75+
0, 0, 0, 0, 0, 0, 0, 0,
76+
0, 0, 0, 0, 0, 0, 0, 0,
77+
0, 0, 0, 0, 0, 0, 0, 0
78+
};
79+
80+
/******************************************************************************
81+
* imported from 'pyctype.c'
82+
******************************************************************************/
83+
84+
/* Our own locale-independent ctype.h-like macros */
85+
86+
const unsigned int _Py_ctype_table[256] = {
87+
0, /* 0x0 '\x00' */
88+
0, /* 0x1 '\x01' */
89+
0, /* 0x2 '\x02' */
90+
0, /* 0x3 '\x03' */
91+
0, /* 0x4 '\x04' */
92+
0, /* 0x5 '\x05' */
93+
0, /* 0x6 '\x06' */
94+
0, /* 0x7 '\x07' */
95+
0, /* 0x8 '\x08' */
96+
PY_CTF_SPACE, /* 0x9 '\t' */
97+
PY_CTF_SPACE, /* 0xa '\n' */
98+
PY_CTF_SPACE, /* 0xb '\v' */
99+
PY_CTF_SPACE, /* 0xc '\f' */
100+
PY_CTF_SPACE, /* 0xd '\r' */
101+
0, /* 0xe '\x0e' */
102+
0, /* 0xf '\x0f' */
103+
0, /* 0x10 '\x10' */
104+
0, /* 0x11 '\x11' */
105+
0, /* 0x12 '\x12' */
106+
0, /* 0x13 '\x13' */
107+
0, /* 0x14 '\x14' */
108+
0, /* 0x15 '\x15' */
109+
0, /* 0x16 '\x16' */
110+
0, /* 0x17 '\x17' */
111+
0, /* 0x18 '\x18' */
112+
0, /* 0x19 '\x19' */
113+
0, /* 0x1a '\x1a' */
114+
0, /* 0x1b '\x1b' */
115+
0, /* 0x1c '\x1c' */
116+
0, /* 0x1d '\x1d' */
117+
0, /* 0x1e '\x1e' */
118+
0, /* 0x1f '\x1f' */
119+
PY_CTF_SPACE, /* 0x20 ' ' */
120+
0, /* 0x21 '!' */
121+
0, /* 0x22 '"' */
122+
0, /* 0x23 '#' */
123+
0, /* 0x24 '$' */
124+
0, /* 0x25 '%' */
125+
0, /* 0x26 '&' */
126+
0, /* 0x27 "'" */
127+
0, /* 0x28 '(' */
128+
0, /* 0x29 ')' */
129+
0, /* 0x2a '*' */
130+
0, /* 0x2b '+' */
131+
0, /* 0x2c ',' */
132+
0, /* 0x2d '-' */
133+
0, /* 0x2e '.' */
134+
0, /* 0x2f '/' */
135+
PY_CTF_DIGIT|PY_CTF_XDIGIT, /* 0x30 '0' */
136+
PY_CTF_DIGIT|PY_CTF_XDIGIT, /* 0x31 '1' */
137+
PY_CTF_DIGIT|PY_CTF_XDIGIT, /* 0x32 '2' */
138+
PY_CTF_DIGIT|PY_CTF_XDIGIT, /* 0x33 '3' */
139+
PY_CTF_DIGIT|PY_CTF_XDIGIT, /* 0x34 '4' */
140+
PY_CTF_DIGIT|PY_CTF_XDIGIT, /* 0x35 '5' */
141+
PY_CTF_DIGIT|PY_CTF_XDIGIT, /* 0x36 '6' */
142+
PY_CTF_DIGIT|PY_CTF_XDIGIT, /* 0x37 '7' */
143+
PY_CTF_DIGIT|PY_CTF_XDIGIT, /* 0x38 '8' */
144+
PY_CTF_DIGIT|PY_CTF_XDIGIT, /* 0x39 '9' */
145+
0, /* 0x3a ':' */
146+
0, /* 0x3b ';' */
147+
0, /* 0x3c '<' */
148+
0, /* 0x3d '=' */
149+
0, /* 0x3e '>' */
150+
0, /* 0x3f '?' */
151+
0, /* 0x40 '@' */
152+
PY_CTF_UPPER|PY_CTF_XDIGIT, /* 0x41 'A' */
153+
PY_CTF_UPPER|PY_CTF_XDIGIT, /* 0x42 'B' */
154+
PY_CTF_UPPER|PY_CTF_XDIGIT, /* 0x43 'C' */
155+
PY_CTF_UPPER|PY_CTF_XDIGIT, /* 0x44 'D' */
156+
PY_CTF_UPPER|PY_CTF_XDIGIT, /* 0x45 'E' */
157+
PY_CTF_UPPER|PY_CTF_XDIGIT, /* 0x46 'F' */
158+
PY_CTF_UPPER, /* 0x47 'G' */
159+
PY_CTF_UPPER, /* 0x48 'H' */
160+
PY_CTF_UPPER, /* 0x49 'I' */
161+
PY_CTF_UPPER, /* 0x4a 'J' */
162+
PY_CTF_UPPER, /* 0x4b 'K' */
163+
PY_CTF_UPPER, /* 0x4c 'L' */
164+
PY_CTF_UPPER, /* 0x4d 'M' */
165+
PY_CTF_UPPER, /* 0x4e 'N' */
166+
PY_CTF_UPPER, /* 0x4f 'O' */
167+
PY_CTF_UPPER, /* 0x50 'P' */
168+
PY_CTF_UPPER, /* 0x51 'Q' */
169+
PY_CTF_UPPER, /* 0x52 'R' */
170+
PY_CTF_UPPER, /* 0x53 'S' */
171+
PY_CTF_UPPER, /* 0x54 'T' */
172+
PY_CTF_UPPER, /* 0x55 'U' */
173+
PY_CTF_UPPER, /* 0x56 'V' */
174+
PY_CTF_UPPER, /* 0x57 'W' */
175+
PY_CTF_UPPER, /* 0x58 'X' */
176+
PY_CTF_UPPER, /* 0x59 'Y' */
177+
PY_CTF_UPPER, /* 0x5a 'Z' */
178+
0, /* 0x5b '[' */
179+
0, /* 0x5c '\\' */
180+
0, /* 0x5d ']' */
181+
0, /* 0x5e '^' */
182+
0, /* 0x5f '_' */
183+
0, /* 0x60 '`' */
184+
PY_CTF_LOWER|PY_CTF_XDIGIT, /* 0x61 'a' */
185+
PY_CTF_LOWER|PY_CTF_XDIGIT, /* 0x62 'b' */
186+
PY_CTF_LOWER|PY_CTF_XDIGIT, /* 0x63 'c' */
187+
PY_CTF_LOWER|PY_CTF_XDIGIT, /* 0x64 'd' */
188+
PY_CTF_LOWER|PY_CTF_XDIGIT, /* 0x65 'e' */
189+
PY_CTF_LOWER|PY_CTF_XDIGIT, /* 0x66 'f' */
190+
PY_CTF_LOWER, /* 0x67 'g' */
191+
PY_CTF_LOWER, /* 0x68 'h' */
192+
PY_CTF_LOWER, /* 0x69 'i' */
193+
PY_CTF_LOWER, /* 0x6a 'j' */
194+
PY_CTF_LOWER, /* 0x6b 'k' */
195+
PY_CTF_LOWER, /* 0x6c 'l' */
196+
PY_CTF_LOWER, /* 0x6d 'm' */
197+
PY_CTF_LOWER, /* 0x6e 'n' */
198+
PY_CTF_LOWER, /* 0x6f 'o' */
199+
PY_CTF_LOWER, /* 0x70 'p' */
200+
PY_CTF_LOWER, /* 0x71 'q' */
201+
PY_CTF_LOWER, /* 0x72 'r' */
202+
PY_CTF_LOWER, /* 0x73 's' */
203+
PY_CTF_LOWER, /* 0x74 't' */
204+
PY_CTF_LOWER, /* 0x75 'u' */
205+
PY_CTF_LOWER, /* 0x76 'v' */
206+
PY_CTF_LOWER, /* 0x77 'w' */
207+
PY_CTF_LOWER, /* 0x78 'x' */
208+
PY_CTF_LOWER, /* 0x79 'y' */
209+
PY_CTF_LOWER, /* 0x7a 'z' */
210+
0, /* 0x7b '{' */
211+
0, /* 0x7c '|' */
212+
0, /* 0x7d '}' */
213+
0, /* 0x7e '~' */
214+
0, /* 0x7f '\x7f' */
215+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
216+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
217+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
218+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
219+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
220+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
221+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
222+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
223+
};
224+
225+
226+
const unsigned char _Py_ctype_tolower[256] = {
227+
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
228+
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
229+
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
230+
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
231+
0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
232+
0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
233+
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
234+
0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
235+
0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
236+
0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
237+
0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
238+
0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
239+
0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
240+
0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
241+
0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
242+
0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
243+
0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
244+
0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
245+
0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
246+
0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
247+
0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
248+
0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
249+
0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
250+
0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
251+
0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
252+
0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
253+
0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
254+
0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
255+
0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
256+
0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
257+
0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
258+
0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
259+
};
260+
261+
const unsigned char _Py_ctype_toupper[256] = {
262+
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
263+
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
264+
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
265+
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
266+
0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
267+
0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
268+
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
269+
0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
270+
0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
271+
0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
272+
0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
273+
0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
274+
0x60, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
275+
0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
276+
0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
277+
0x58, 0x59, 0x5a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
278+
0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
279+
0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
280+
0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
281+
0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
282+
0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
283+
0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
284+
0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
285+
0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
286+
0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
287+
0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
288+
0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
289+
0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
290+
0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
291+
0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
292+
0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
293+
0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
294+
};
295+
296+
#endif /* SRC_CONST_ARRAYS_H_ */

0 commit comments

Comments
 (0)