summaryrefslogtreecommitdiffstats
path: root/Include/cpython/code.h
diff options
context:
space:
mode:
authorMark Shannon <mark@hotpy.org>2022-12-14 11:12:53 (GMT)
committerGitHub <noreply@github.com>2022-12-14 11:12:53 (GMT)
commit6997e77bdf2297375962aaf82876da4e7ecdd61a (patch)
tree7f6540eecbb66fd373b22e944163bdcb436f245b /Include/cpython/code.h
parent985a71032bf055240e61259285a0b4925c925383 (diff)
downloadcpython-6997e77bdf2297375962aaf82876da4e7ecdd61a.zip
cpython-6997e77bdf2297375962aaf82876da4e7ecdd61a.tar.gz
cpython-6997e77bdf2297375962aaf82876da4e7ecdd61a.tar.bz2
GH-100222: Redefine _Py_CODEUNIT as a union to clarify structure of code unit. (GH-100223)
Diffstat (limited to 'Include/cpython/code.h')
-rw-r--r--Include/cpython/code.h31
1 files changed, 17 insertions, 14 deletions
diff --git a/Include/cpython/code.h b/Include/cpython/code.h
index fc7c5ed..6a13ff2 100644
--- a/Include/cpython/code.h
+++ b/Include/cpython/code.h
@@ -16,21 +16,24 @@ extern "C" {
* 2**32 - 1, rather than INT_MAX.
*/
-typedef uint16_t _Py_CODEUNIT;
-
-#ifdef WORDS_BIGENDIAN
-# define _Py_OPCODE(word) ((word) >> 8)
-# define _Py_OPARG(word) ((word) & 255)
-# define _Py_MAKECODEUNIT(opcode, oparg) (((opcode)<<8)|(oparg))
-#else
-# define _Py_OPCODE(word) ((word) & 255)
-# define _Py_OPARG(word) ((word) >> 8)
-# define _Py_MAKECODEUNIT(opcode, oparg) ((opcode)|((oparg)<<8))
-#endif
+typedef union {
+ uint16_t cache;
+ struct {
+ uint8_t opcode;
+ uint8_t oparg;
+ };
+} _Py_CODEUNIT;
+
+#define _Py_OPCODE(word) ((word).opcode)
+#define _Py_OPARG(word) ((word).oparg)
+
+static inline void
+_py_set_opocde(_Py_CODEUNIT *word, uint8_t opcode)
+{
+ word->opcode = opcode;
+}
-// Use "unsigned char" instead of "uint8_t" here to avoid illegal aliasing:
-#define _Py_SET_OPCODE(word, opcode) \
- do { ((unsigned char *)&(word))[0] = (opcode); } while (0)
+#define _Py_SET_OPCODE(word, opcode) _py_set_opocde(&(word), opcode)
typedef struct {
PyObject *_co_code;