Skip to content

Commit e22251d

Browse files
committed
Avoid code duplication.
1 parent ef9623a commit e22251d

File tree

3 files changed

+30
-42
lines changed

3 files changed

+30
-42
lines changed

Objects/frameobject.c

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "frameobject.h"
88
#include "opcode.h"
99
#include "structmember.h"
10+
#include "Python/wordcode_helpers.h"
1011

1112
#define OFF(x) offsetof(PyFrameObject, x)
1213

@@ -45,25 +46,6 @@ frame_getlineno(PyFrameObject *f, void *closure)
4546
return PyLong_FromLong(PyFrame_GetLineNumber(f));
4647
}
4748

48-
/* Given the index of the effective opcode,
49-
scan back to construct the oparg with EXTENDED_ARG */
50-
static unsigned int
51-
get_arg(const _Py_CODEUNIT *codestr, Py_ssize_t i)
52-
{
53-
_Py_CODEUNIT word;
54-
unsigned int oparg = _Py_OPARG(codestr[i]);
55-
if (i >= 1 && _Py_OPCODE(word = codestr[i-1]) == EXTENDED_ARG) {
56-
oparg |= _Py_OPARG(word) << 8;
57-
if (i >= 2 && _Py_OPCODE(word = codestr[i-2]) == EXTENDED_ARG) {
58-
oparg |= _Py_OPARG(word) << 16;
59-
if (i >= 3 && _Py_OPCODE(word = codestr[i-3]) == EXTENDED_ARG) {
60-
oparg |= _Py_OPARG(word) << 24;
61-
}
62-
}
63-
}
64-
return oparg;
65-
}
66-
6749
#define ITER_MARKER 1
6850
#define EXCEPT_MARKER 2
6951
#define FINALLY_MARKER 3

Python/peephole.c

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -58,25 +58,6 @@ find_op(const _Py_CODEUNIT *codestr, Py_ssize_t i)
5858
return i;
5959
}
6060

61-
/* Given the index of the effective opcode,
62-
scan back to construct the oparg with EXTENDED_ARG */
63-
static unsigned int
64-
get_arg(const _Py_CODEUNIT *codestr, Py_ssize_t i)
65-
{
66-
_Py_CODEUNIT word;
67-
unsigned int oparg = _Py_OPARG(codestr[i]);
68-
if (i >= 1 && _Py_OPCODE(word = codestr[i-1]) == EXTENDED_ARG) {
69-
oparg |= _Py_OPARG(word) << 8;
70-
if (i >= 2 && _Py_OPCODE(word = codestr[i-2]) == EXTENDED_ARG) {
71-
oparg |= _Py_OPARG(word) << 16;
72-
if (i >= 3 && _Py_OPCODE(word = codestr[i-3]) == EXTENDED_ARG) {
73-
oparg |= _Py_OPARG(word) << 24;
74-
}
75-
}
76-
}
77-
return oparg;
78-
}
79-
8061
/* Fill the region with NOPs. */
8162
static void
8263
fill_nops(_Py_CODEUNIT *codestr, Py_ssize_t start, Py_ssize_t end)

Python/wordcode_helpers.h

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
/* This file contains code shared by the compiler and the peephole
2-
optimizer.
1+
/* This file contains code shared by the compiler, the peephole
2+
optimizer and frameobject.
33
*/
44

55
#ifdef WORDS_BIGENDIAN
@@ -8,9 +8,15 @@
88
# define PACKOPARG(opcode, oparg) ((_Py_CODEUNIT)(((oparg) << 8) | (opcode)))
99
#endif
1010

11+
#ifdef __GNUC__
12+
#define MAYBE_UNUSED __attribute__ ((unused))
13+
#else
14+
#define MAYBE_UNUSED
15+
#endif
16+
1117
/* Minimum number of code units necessary to encode instruction with
1218
EXTENDED_ARGs */
13-
static int
19+
MAYBE_UNUSED static int
1420
instrsize(unsigned int oparg)
1521
{
1622
return oparg <= 0xff ? 1 :
@@ -21,7 +27,7 @@ instrsize(unsigned int oparg)
2127

2228
/* Spits out op/oparg pair using ilen bytes. codestr should be pointed at the
2329
desired location of the first EXTENDED_ARG */
24-
static void
30+
MAYBE_UNUSED static void
2531
write_op_arg(_Py_CODEUNIT *codestr, unsigned char opcode,
2632
unsigned int oparg, int ilen)
2733
{
@@ -42,3 +48,22 @@ write_op_arg(_Py_CODEUNIT *codestr, unsigned char opcode,
4248
Py_UNREACHABLE();
4349
}
4450
}
51+
52+
/* Given the index of the effective opcode,
53+
scan back to construct the oparg with EXTENDED_ARG */
54+
MAYBE_UNUSED static unsigned int
55+
get_arg(const _Py_CODEUNIT *codestr, Py_ssize_t i)
56+
{
57+
_Py_CODEUNIT word;
58+
unsigned int oparg = _Py_OPARG(codestr[i]);
59+
if (i >= 1 && _Py_OPCODE(word = codestr[i-1]) == EXTENDED_ARG) {
60+
oparg |= _Py_OPARG(word) << 8;
61+
if (i >= 2 && _Py_OPCODE(word = codestr[i-2]) == EXTENDED_ARG) {
62+
oparg |= _Py_OPARG(word) << 16;
63+
if (i >= 3 && _Py_OPCODE(word = codestr[i-3]) == EXTENDED_ARG) {
64+
oparg |= _Py_OPARG(word) << 24;
65+
}
66+
}
67+
}
68+
return oparg;
69+
}

0 commit comments

Comments
 (0)