Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

GH-93678: reduce boilerplate and code repetition in the compiler#93682

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Merged
Changes from1 commit
Commits
Show all changes
18 commits
Select commitHold shift + click to select a range
996be64
move line/noline to arg and remove _noline version of functions
iritkatrielJun 10, 2022
af31e92
add struct location to reduce boilerplate
iritkatrielJun 10, 2022
f8a0767
merge basicblock_addop, basicblock_addop_i and basicblock_add_jump in…
iritkatrielJun 10, 2022
430b4b9
add news
iritkatrielJun 10, 2022
66d5609
assume that oparg is < (1<<30)
iritkatrielJun 10, 2022
7670a7c
Merge remote-tracking branch 'upstream/main' into codegen-reduce-repe…
iritkatrielJun 10, 2022
7d74572
use the new location struct for the compiler unit's location
iritkatrielJun 10, 2022
016ebbc
use the new location struct for the instruction location
iritkatrielJun 10, 2022
5e06cad
basicblock_addop takes location by reference
iritkatrielJun 10, 2022
c23e2cf
LOCATION needs to return a const
iritkatrielJun 10, 2022
fc74555
define static no_location in function scope
iritkatrielJun 10, 2022
9220fc1
are we const now?
iritkatrielJun 10, 2022
9b5dc34
trivial stuff
iritkatrielJun 10, 2022
5330060
Merge remote-tracking branch 'upstream/main' into codegen-reduce-repe…
iritkatrielJun 13, 2022
414e26a
remove two unused fields from struct assembler
iritkatrielJun 13, 2022
26dc190
define NO_LOCATION as a static const in global scope
iritkatrielJun 13, 2022
98d24f2
Merge remote-tracking branch 'upstream/main' into codegen-reduce-repe…
iritkatrielJun 14, 2022
7851ccd
Merge remote-tracking branch 'upstream/main' into codegen-reduce-repe…
iritkatrielJun 14, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
NextNext commit
move line/noline to arg and remove _noline version of functions
  • Loading branch information
@iritkatriel
iritkatriel committedJun 10, 2022
commit996be64b4d5d85ad0d92f03f8f7de87e737ebde6
110 changes: 47 additions & 63 deletionsPython/compile.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -432,10 +432,9 @@ static int basicblock_next_instr(basicblock *);
static int compiler_enter_scope(struct compiler *, identifier, int, void *, int);
static void compiler_free(struct compiler *);
static basicblock *compiler_new_block(struct compiler *);
static int compiler_addop(struct compiler *, int);
static int compiler_addop_i(struct compiler *, int, Py_ssize_t);
static int compiler_addop_j(struct compiler *, int, basicblock *);
static int compiler_addop_j_noline(struct compiler *, int, basicblock *);
static int compiler_addop(struct compiler *, int, bool);
static int compiler_addop_i(struct compiler *, int, Py_ssize_t, bool);
static int compiler_addop_j(struct compiler *, int, basicblock *, bool);
static int compiler_error(struct compiler *, const char *, ...);
static int compiler_warn(struct compiler *, const char *, ...);
static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
Expand DownExpand Up@@ -1269,8 +1268,8 @@ compiler_use_new_implicit_block_if_needed(struct compiler *c)
*/

static int
basicblock_addop_line(basicblock *b, int opcode, intline,
intend_line, int col_offset, int end_col_offset)
basicblock_addop(basicblock *b, int opcode, intlineno,
intend_lineno, int col_offset, int end_col_offset)
{
assert(IS_WITHIN_OPCODE_RANGE(opcode));
assert(!IS_ASSEMBLER_OPCODE(opcode));
Expand All@@ -1283,34 +1282,29 @@ basicblock_addop_line(basicblock *b, int opcode, int line,
struct instr *i = &b->b_instr[off];
i->i_opcode = opcode;
i->i_oparg = 0;
i->i_lineno =line;
i->i_end_lineno =end_line;
i->i_lineno =lineno;
i->i_end_lineno =end_lineno;
i->i_col_offset = col_offset;
i->i_end_col_offset = end_col_offset;

return 1;
}

static int
compiler_addop(struct compiler *c, int opcode)
compiler_addop(struct compiler *c, int opcode, bool line)
{
if (compiler_use_new_implicit_block_if_needed(c) < 0) {
return -1;
}
return basicblock_addop_line(c->u->u_curblock, opcode, c->u->u_lineno, c->u->u_end_lineno,
c->u->u_col_offset, c->u->u_end_col_offset);
}
int lineno = line ? c->u->u_lineno : -1;
int end_lineno = line ? c->u->u_end_lineno : 0;
int col_offset = line ? c->u->u_col_offset : 0;
int end_col_offset = line ? c->u->u_end_col_offset : 0;

static int
compiler_addop_noline(struct compiler *c, int opcode)
{
if (compiler_use_new_implicit_block_if_needed(c) < 0) {
return -1;
}
return basicblock_addop_line(c->u->u_curblock, opcode, -1, 0, 0, 0);
return basicblock_addop(c->u->u_curblock, opcode,
lineno, end_lineno, col_offset, end_col_offset);
}


static Py_ssize_t
compiler_add_o(PyObject *dict, PyObject *o)
{
Expand DownExpand Up@@ -1467,7 +1461,7 @@ compiler_addop_load_const(struct compiler *c, PyObject *o)
Py_ssize_t arg = compiler_add_const(c, o);
if (arg < 0)
return 0;
return compiler_addop_i(c, LOAD_CONST, arg);
return compiler_addop_i(c, LOAD_CONST, arg, true);
}

static int
Expand All@@ -1477,7 +1471,7 @@ compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
Py_ssize_t arg = compiler_add_o(dict, o);
if (arg < 0)
return 0;
return compiler_addop_i(c, opcode, arg);
return compiler_addop_i(c, opcode, arg, true);
}

static int
Expand All@@ -1493,17 +1487,17 @@ compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
Py_DECREF(mangled);
if (arg < 0)
return 0;
return compiler_addop_i(c, opcode, arg);
return compiler_addop_i(c, opcode, arg, true);
}

/* Add an opcode with an integer argument.
Returns 0 on failure, 1 on success.
*/

static int
basicblock_addop_i_line(basicblock *b, int opcode, Py_ssize_t oparg,
int lineno, int end_lineno,
int col_offset, int end_col_offset)
basicblock_addop_i(basicblock *b, int opcode, Py_ssize_t oparg,
int lineno, int end_lineno,
int col_offset, int end_col_offset)
{
/* oparg value is unsigned, but a signed C int is usually used to store
it in the C code (like Python/ceval.c).
Expand All@@ -1515,7 +1509,6 @@ basicblock_addop_i_line(basicblock *b, int opcode, Py_ssize_t oparg,

assert(IS_WITHIN_OPCODE_RANGE(opcode));
assert(!IS_ASSEMBLER_OPCODE(opcode));
assert(HAS_ARG(opcode));
assert(0 <= oparg && oparg <= 2147483647);

int off = basicblock_next_instr(b);
Expand All@@ -1534,23 +1527,18 @@ basicblock_addop_i_line(basicblock *b, int opcode, Py_ssize_t oparg,
}

static int
compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg)
compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg, bool line)
{
if (compiler_use_new_implicit_block_if_needed(c) < 0) {
return -1;
}
return basicblock_addop_i_line(c->u->u_curblock, opcode, oparg,
c->u->u_lineno, c->u->u_end_lineno,
c->u->u_col_offset, c->u->u_end_col_offset);
}
int lineno = line ?c->u->u_lineno : -1;
int end_lineno = line ?c->u->u_end_lineno : 0;
int col_offset = line ?c->u->u_col_offset : 0;
int end_col_offset = line ? c->u->u_end_col_offset : 0;

static int
compiler_addop_i_noline(struct compiler *c, int opcode, Py_ssize_t oparg)
{
if (compiler_use_new_implicit_block_if_needed(c) < 0) {
return -1;
}
return basicblock_addop_i_line(c->u->u_curblock, opcode, oparg, -1, 0, 0, 0);
return basicblock_addop_i(c->u->u_curblock, opcode, oparg,
lineno, end_lineno, col_offset, end_col_offset);
}

static int
Expand DownExpand Up@@ -1580,37 +1568,33 @@ basicblock_add_jump(basicblock *b, int opcode,
}

static int
compiler_addop_j(struct compiler *c, int opcode, basicblock *b)
compiler_addop_j(struct compiler *c, int opcode, basicblock *target, bool line)
{
if (compiler_use_new_implicit_block_if_needed(c) < 0) {
return -1;
}
return basicblock_add_jump(c->u->u_curblock, opcode,c->u->u_lineno,
c->u->u_end_lineno, c->u->u_col_offset,
c->u->u_end_col_offset, b);
}
int lineno = line ?c->u->u_lineno : -1;
int end_lineno = line ?c->u->u_end_lineno : 0;
int col_offset = line ?c->u->u_col_offset : 0;
int end_col_offset = line ? c->u->u_end_col_offset : 0;

static int
compiler_addop_j_noline(struct compiler *c, int opcode, basicblock *b)
{
if (compiler_use_new_implicit_block_if_needed(c) < 0) {
return -1;
}
return basicblock_add_jump(c->u->u_curblock, opcode, -1, 0, 0, 0, b);
return basicblock_add_jump(c->u->u_curblock, opcode,
lineno, end_lineno, col_offset, end_col_offset,
target);
}

#define ADDOP(C, OP) { \
if (!compiler_addop((C), (OP))) \
if (!compiler_addop((C), (OP), true)) \
return 0; \
}

#define ADDOP_NOLINE(C, OP) { \
if (!compiler_addop_noline((C), (OP))) \
if (!compiler_addop((C), (OP), false)) \
return 0; \
}

#define ADDOP_IN_SCOPE(C, OP) { \
if (!compiler_addop((C), (OP))) { \
if (!compiler_addop((C), (OP), true)) { \
compiler_exit_scope(c); \
return 0; \
} \
Expand DownExpand Up@@ -1649,25 +1633,25 @@ compiler_addop_j_noline(struct compiler *c, int opcode, basicblock *b)
}

#define ADDOP_I(C, OP, O) { \
if (!compiler_addop_i((C), (OP), (O))) \
if (!compiler_addop_i((C), (OP), (O), true)) \
return 0; \
}

#define ADDOP_I_NOLINE(C, OP, O) { \
if (!compiler_addop_i_noline((C), (OP), (O))) \
if (!compiler_addop_i((C), (OP), (O), false)) \
return 0; \
}

#define ADDOP_JUMP(C, OP, O) { \
if (!compiler_addop_j((C), (OP), (O))) \
if (!compiler_addop_j((C), (OP), (O), true)) \
return 0; \
}

/* Add a jump with no line number.
* Used for artificial jumps that have no corresponding
* token in the source code. */
#define ADDOP_JUMP_NOLINE(C, OP, O) { \
if (!compiler_addop_j_noline((C), (OP), (O))) \
if (!compiler_addop_j((C), (OP), (O), false)) \
return 0; \
}

Expand DownExpand Up@@ -4320,7 +4304,7 @@ compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
if (op == LOAD_GLOBAL) {
arg <<= 1;
}
return compiler_addop_i(c, op, arg);
return compiler_addop_i(c, op, arg, true);
}

static int
Expand DownExpand Up@@ -6299,7 +6283,7 @@ emit_and_reset_fail_pop(struct compiler *c, pattern_context *pc)
}
while (--pc->fail_pop_size) {
compiler_use_next_block(c, pc->fail_pop[pc->fail_pop_size]);
if (!compiler_addop(c, POP_TOP)) {
if (!compiler_addop(c, POP_TOP, true)) {
pc->fail_pop_size = 0;
PyObject_Free(pc->fail_pop);
pc->fail_pop = NULL;
Expand DownExpand Up@@ -6733,7 +6717,7 @@ compiler_pattern_or(struct compiler *c, pattern_ty p, pattern_context *pc)
pc->fail_pop = NULL;
pc->fail_pop_size = 0;
pc->on_top = 0;
if (!compiler_addop_i(c, COPY, 1) || !compiler_pattern(c, alt, pc)) {
if (!compiler_addop_i(c, COPY, 1, true) || !compiler_pattern(c, alt, pc)) {
goto error;
}
// Success!
Expand DownExpand Up@@ -6796,7 +6780,7 @@ compiler_pattern_or(struct compiler *c, pattern_ty p, pattern_context *pc)
}
}
assert(control);
if (!compiler_addop_j(c, JUMP, end) ||
if (!compiler_addop_j(c, JUMP, end, true) ||
!emit_and_reset_fail_pop(c, pc))
{
goto error;
Expand All@@ -6808,7 +6792,7 @@ compiler_pattern_or(struct compiler *c, pattern_ty p, pattern_context *pc)
// Need to NULL this for the PyObject_Free call in the error block.
old_pc.fail_pop = NULL;
// No match. Pop the remaining copy of the subject and fail:
if (!compiler_addop(c, POP_TOP) || !jump_to_fail_pop(c, pc, JUMP)) {
if (!compiler_addop(c, POP_TOP, true) || !jump_to_fail_pop(c, pc, JUMP)) {
goto error;
}
compiler_use_next_block(c, end);
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp