Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-98831: rewrite pattern matching opcodes in the instruction definition DSL #101287

Merged
merged 5 commits into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
50 changes: 13 additions & 37 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -2092,61 +2092,37 @@ dummy_func(
PUSH(len_o);
}

// stack effect: (__0, __1 -- )
inst(MATCH_CLASS) {
inst(MATCH_CLASS, (subject, type, names -- attrs)) {
// Pop TOS and TOS1. Set TOS to a tuple of attributes on success, or
// None on failure.
PyObject *names = POP();
PyObject *type = POP();
PyObject *subject = TOP();
assert(PyTuple_CheckExact(names));
PyObject *attrs = match_class(tstate, subject, type, oparg, names);
Py_DECREF(names);
Py_DECREF(type);
attrs = match_class(tstate, subject, type, oparg, names);
DECREF_INPUTS();
if (attrs) {
// Success!
assert(PyTuple_CheckExact(attrs));
SET_TOP(attrs);
}
else if (_PyErr_Occurred(tstate)) {
// Error!
goto error;
assert(PyTuple_CheckExact(attrs)); // Success!
}
else {
// Failure!
SET_TOP(Py_NewRef(Py_None));
ERROR_IF(_PyErr_Occurred(tstate), error); // Error!
attrs = Py_NewRef(Py_None); // Failure!
}
Py_DECREF(subject);
}

// stack effect: ( -- __0)
inst(MATCH_MAPPING) {
PyObject *subject = TOP();
inst(MATCH_MAPPING, (subject -- subject, res)) {
int match = Py_TYPE(subject)->tp_flags & Py_TPFLAGS_MAPPING;
PyObject *res = match ? Py_True : Py_False;
PUSH(Py_NewRef(res));
res = Py_NewRef(match ? Py_True : Py_False);
PREDICT(POP_JUMP_IF_FALSE);
}

// stack effect: ( -- __0)
inst(MATCH_SEQUENCE) {
PyObject *subject = TOP();
inst(MATCH_SEQUENCE, (subject -- subject, res)) {
int match = Py_TYPE(subject)->tp_flags & Py_TPFLAGS_SEQUENCE;
PyObject *res = match ? Py_True : Py_False;
PUSH(Py_NewRef(res));
res = Py_NewRef(match ? Py_True : Py_False);
PREDICT(POP_JUMP_IF_FALSE);
}

// stack effect: ( -- __0)
inst(MATCH_KEYS) {
inst(MATCH_KEYS, (subject, keys -- subject, keys, values_or_none)) {
// On successful match, PUSH(values). Otherwise, PUSH(None).
PyObject *keys = TOP();
PyObject *subject = SECOND();
PyObject *values_or_none = match_keys(tstate, subject, keys);
if (values_or_none == NULL) {
goto error;
}
PUSH(values_or_none);
values_or_none = match_keys(tstate, subject, keys);
ERROR_IF(values_or_none == NULL, error);
}

// stack effect: ( -- )
Expand Down
57 changes: 29 additions & 28 deletions Python/generated_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions Python/opcode_metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,10 @@ static const struct {
[JUMP_IF_TRUE_OR_POP] = { -1, -1, DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
[JUMP_BACKWARD_NO_INTERRUPT] = { -1, -1, DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
[GET_LEN] = { -1, -1, DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IX },
[MATCH_CLASS] = { -1, -1, DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
[MATCH_MAPPING] = { -1, -1, DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IX },
[MATCH_SEQUENCE] = { -1, -1, DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IX },
[MATCH_KEYS] = { -1, -1, DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IX },
[MATCH_CLASS] = { 3, 1, DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
[MATCH_MAPPING] = { 1, 2, DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IX },
[MATCH_SEQUENCE] = { 1, 2, DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IX },
[MATCH_KEYS] = { 2, 3, DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IX },
[GET_ITER] = { -1, -1, DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IX },
[GET_YIELD_FROM_ITER] = { -1, -1, DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IX },
[FOR_ITER] = { -1, -1, DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
Expand Down
8 changes: 4 additions & 4 deletions Tools/cases_generator/generate_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
)
BEGIN_MARKER = "// BEGIN BYTECODES //"
END_MARKER = "// END BYTECODES //"
RE_PREDICTED = r"^\s*(?:PREDICT\(|GO_TO_INSTRUCTION\(|DEOPT_IF\(.*?,\s*)(\w+)\);\s*$"
RE_PREDICTED = r"^\s*(?:PREDICT\(|GO_TO_INSTRUCTION\(|DEOPT_IF\(.*?,\s*)(\w+)\);\s*(?://.*)?$"
UNUSED = "unused"
BITS_PER_CODE_UNIT = 16

Expand Down Expand Up @@ -354,7 +354,7 @@ def write_body(self, out: Formatter, dedent: int, cache_adjust: int = 0) -> None
assert dedent <= 0
extra = " " * -dedent
for line in self.block_text:
if m := re.match(r"(\s*)ERROR_IF\((.+), (\w+)\);\s*$", line):
if m := re.match(r"(\s*)ERROR_IF\((.+), (\w+)\);\s*(?://.*)?$", line):
space, cond, label = m.groups()
# ERROR_IF() must pop the inputs from the stack.
# The code block is responsible for DECREF()ing them.
Expand All @@ -378,7 +378,7 @@ def write_body(self, out: Formatter, dedent: int, cache_adjust: int = 0) -> None
)
else:
out.write_raw(f"{extra}{space}if ({cond}) goto {label};\n")
elif m := re.match(r"(\s*)DECREF_INPUTS\(\);\s*$", line):
elif m := re.match(r"(\s*)DECREF_INPUTS\(\);\s*(?://.*)?$", line):
if not self.register:
space = m.group(1)
for ieff in self.input_effects:
Expand Down Expand Up @@ -964,7 +964,7 @@ def extract_block_text(block: parser.Block) -> tuple[list[str], list[str]]:

# Separate PREDICT(...) macros from end
predictions: list[str] = []
while blocklines and (m := re.match(r"^\s*PREDICT\((\w+)\);\s*$", blocklines[-1])):
while blocklines and (m := re.match(r"^\s*PREDICT\((\w+)\);\s*(?://.*)?$", blocklines[-1])):
predictions.insert(0, m.group(1))
blocklines.pop()

Expand Down
14 changes: 14 additions & 0 deletions Tools/cases_generator/test_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,20 @@ def test_error_if_plain():
"""
run_cases_test(input, output)

def test_error_if_plain_with_comment():
input = """
inst(OP, (--)) {
ERROR_IF(cond, label); // Comment is ok
}
"""
output = """
TARGET(OP) {
if (cond) goto label;
DISPATCH();
}
"""
run_cases_test(input, output)

def test_error_if_pop():
input = """
inst(OP, (left, right -- res)) {
Expand Down