2023-12-08 07:48:30 -04:00
|
|
|
from pathlib import Path
|
|
|
|
from typing import TextIO
|
|
|
|
|
2023-12-12 08:12:17 -04:00
|
|
|
from analyzer import (
|
|
|
|
Instruction,
|
|
|
|
Uop,
|
|
|
|
analyze_files,
|
2023-12-20 10:27:25 -04:00
|
|
|
Properties,
|
2023-12-12 08:12:17 -04:00
|
|
|
Skip,
|
|
|
|
)
|
|
|
|
from cwriter import CWriter
|
|
|
|
from typing import Callable, Mapping, TextIO, Iterator
|
|
|
|
from lexer import Token
|
|
|
|
from stack import StackOffset, Stack
|
|
|
|
|
|
|
|
|
2023-12-08 07:48:30 -04:00
|
|
|
ROOT = Path(__file__).parent.parent.parent
|
2023-12-12 08:12:17 -04:00
|
|
|
DEFAULT_INPUT = (ROOT / "Python/bytecodes.c").absolute().as_posix()
|
2023-12-08 07:48:30 -04:00
|
|
|
|
|
|
|
|
|
|
|
def root_relative_path(filename: str) -> str:
|
2023-12-18 07:14:40 -04:00
|
|
|
try:
|
|
|
|
return Path(filename).absolute().relative_to(ROOT).as_posix()
|
|
|
|
except ValueError:
|
2023-12-20 10:27:25 -04:00
|
|
|
# Not relative to root, just return original path.
|
2023-12-18 07:14:40 -04:00
|
|
|
return filename
|
2023-12-08 07:48:30 -04:00
|
|
|
|
2023-12-20 10:27:25 -04:00
|
|
|
|
2024-01-12 13:30:27 -04:00
|
|
|
def write_header(
|
|
|
|
generator: str, sources: list[str], outfile: TextIO, comment: str = "//"
|
|
|
|
) -> None:
|
2023-12-08 07:48:30 -04:00
|
|
|
outfile.write(
|
2023-12-20 10:27:25 -04:00
|
|
|
f"""{comment} This file is generated by {root_relative_path(generator)}
|
|
|
|
{comment} from:
|
|
|
|
{comment} {", ".join(root_relative_path(src) for src in sources)}
|
|
|
|
{comment} Do not edit!
|
2023-12-08 07:48:30 -04:00
|
|
|
"""
|
|
|
|
)
|
2023-12-12 08:12:17 -04:00
|
|
|
|
|
|
|
|
|
|
|
def emit_to(out: CWriter, tkn_iter: Iterator[Token], end: str) -> None:
|
|
|
|
parens = 0
|
|
|
|
for tkn in tkn_iter:
|
|
|
|
if tkn.kind == end and parens == 0:
|
|
|
|
return
|
|
|
|
if tkn.kind == "LPAREN":
|
|
|
|
parens += 1
|
|
|
|
if tkn.kind == "RPAREN":
|
|
|
|
parens -= 1
|
|
|
|
out.emit(tkn)
|
|
|
|
|
|
|
|
|
|
|
|
def replace_deopt(
|
|
|
|
out: CWriter,
|
|
|
|
tkn: Token,
|
|
|
|
tkn_iter: Iterator[Token],
|
|
|
|
uop: Uop,
|
|
|
|
unused: Stack,
|
|
|
|
inst: Instruction | None,
|
|
|
|
) -> None:
|
|
|
|
out.emit_at("DEOPT_IF", tkn)
|
|
|
|
out.emit(next(tkn_iter))
|
|
|
|
emit_to(out, tkn_iter, "RPAREN")
|
|
|
|
next(tkn_iter) # Semi colon
|
|
|
|
out.emit(", ")
|
|
|
|
assert inst is not None
|
|
|
|
assert inst.family is not None
|
|
|
|
out.emit(inst.family.name)
|
|
|
|
out.emit(");\n")
|
|
|
|
|
|
|
|
|
|
|
|
def replace_error(
|
|
|
|
out: CWriter,
|
|
|
|
tkn: Token,
|
|
|
|
tkn_iter: Iterator[Token],
|
|
|
|
uop: Uop,
|
|
|
|
stack: Stack,
|
|
|
|
inst: Instruction | None,
|
|
|
|
) -> None:
|
|
|
|
out.emit_at("if ", tkn)
|
|
|
|
out.emit(next(tkn_iter))
|
|
|
|
emit_to(out, tkn_iter, "COMMA")
|
|
|
|
label = next(tkn_iter).text
|
|
|
|
next(tkn_iter) # RPAREN
|
|
|
|
next(tkn_iter) # Semi colon
|
|
|
|
out.emit(") ")
|
|
|
|
c_offset = stack.peek_offset.to_c()
|
|
|
|
try:
|
|
|
|
offset = -int(c_offset)
|
|
|
|
close = ";\n"
|
|
|
|
except ValueError:
|
|
|
|
offset = None
|
|
|
|
out.emit(f"{{ stack_pointer += {c_offset}; ")
|
|
|
|
close = "; }\n"
|
|
|
|
out.emit("goto ")
|
|
|
|
if offset:
|
|
|
|
out.emit(f"pop_{offset}_")
|
|
|
|
out.emit(label)
|
|
|
|
out.emit(close)
|
|
|
|
|
|
|
|
|
2024-03-26 06:35:11 -03:00
|
|
|
def replace_error_no_pop(
|
|
|
|
out: CWriter,
|
|
|
|
tkn: Token,
|
|
|
|
tkn_iter: Iterator[Token],
|
|
|
|
uop: Uop,
|
|
|
|
stack: Stack,
|
|
|
|
inst: Instruction | None,
|
|
|
|
) -> None:
|
|
|
|
next(tkn_iter) # LPAREN
|
|
|
|
next(tkn_iter) # RPAREN
|
|
|
|
next(tkn_iter) # Semi colon
|
|
|
|
out.emit_at("goto error;", tkn)
|
|
|
|
|
|
|
|
|
2023-12-12 08:12:17 -04:00
|
|
|
def replace_decrefs(
|
|
|
|
out: CWriter,
|
|
|
|
tkn: Token,
|
|
|
|
tkn_iter: Iterator[Token],
|
|
|
|
uop: Uop,
|
|
|
|
stack: Stack,
|
|
|
|
inst: Instruction | None,
|
|
|
|
) -> None:
|
|
|
|
next(tkn_iter)
|
|
|
|
next(tkn_iter)
|
|
|
|
next(tkn_iter)
|
|
|
|
out.emit_at("", tkn)
|
|
|
|
for var in uop.stack.inputs:
|
|
|
|
if var.name == "unused" or var.name == "null" or var.peek:
|
|
|
|
continue
|
|
|
|
if var.size != "1":
|
|
|
|
out.emit(f"for (int _i = {var.size}; --_i >= 0;) {{\n")
|
|
|
|
out.emit(f"Py_DECREF({var.name}[_i]);\n")
|
|
|
|
out.emit("}\n")
|
|
|
|
elif var.condition:
|
2024-02-20 06:50:59 -04:00
|
|
|
if var.condition == "1":
|
|
|
|
out.emit(f"Py_DECREF({var.name});\n")
|
|
|
|
elif var.condition != "0":
|
|
|
|
out.emit(f"Py_XDECREF({var.name});\n")
|
2023-12-12 08:12:17 -04:00
|
|
|
else:
|
|
|
|
out.emit(f"Py_DECREF({var.name});\n")
|
|
|
|
|
|
|
|
|
2024-01-15 07:41:06 -04:00
|
|
|
def replace_sync_sp(
|
2023-12-12 08:12:17 -04:00
|
|
|
out: CWriter,
|
|
|
|
tkn: Token,
|
|
|
|
tkn_iter: Iterator[Token],
|
|
|
|
uop: Uop,
|
|
|
|
stack: Stack,
|
|
|
|
inst: Instruction | None,
|
|
|
|
) -> None:
|
|
|
|
next(tkn_iter)
|
|
|
|
next(tkn_iter)
|
|
|
|
next(tkn_iter)
|
|
|
|
stack.flush(out)
|
|
|
|
|
|
|
|
|
|
|
|
def replace_check_eval_breaker(
|
|
|
|
out: CWriter,
|
|
|
|
tkn: Token,
|
|
|
|
tkn_iter: Iterator[Token],
|
|
|
|
uop: Uop,
|
|
|
|
stack: Stack,
|
|
|
|
inst: Instruction | None,
|
|
|
|
) -> None:
|
|
|
|
next(tkn_iter)
|
|
|
|
next(tkn_iter)
|
|
|
|
next(tkn_iter)
|
|
|
|
if not uop.properties.ends_with_eval_breaker:
|
|
|
|
out.emit_at("CHECK_EVAL_BREAKER();", tkn)
|
|
|
|
|
|
|
|
|
|
|
|
REPLACEMENT_FUNCTIONS = {
|
2024-02-20 05:39:55 -04:00
|
|
|
"EXIT_IF": replace_deopt,
|
2023-12-12 08:12:17 -04:00
|
|
|
"DEOPT_IF": replace_deopt,
|
|
|
|
"ERROR_IF": replace_error,
|
2024-03-26 06:35:11 -03:00
|
|
|
"ERROR_NO_POP": replace_error_no_pop,
|
2023-12-12 08:12:17 -04:00
|
|
|
"DECREF_INPUTS": replace_decrefs,
|
|
|
|
"CHECK_EVAL_BREAKER": replace_check_eval_breaker,
|
2024-01-15 07:41:06 -04:00
|
|
|
"SYNC_SP": replace_sync_sp,
|
2023-12-12 08:12:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ReplacementFunctionType = Callable[
|
|
|
|
[CWriter, Token, Iterator[Token], Uop, Stack, Instruction | None], None
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def emit_tokens(
|
|
|
|
out: CWriter,
|
|
|
|
uop: Uop,
|
|
|
|
stack: Stack,
|
|
|
|
inst: Instruction | None,
|
|
|
|
replacement_functions: Mapping[
|
|
|
|
str, ReplacementFunctionType
|
|
|
|
] = REPLACEMENT_FUNCTIONS,
|
|
|
|
) -> None:
|
|
|
|
tkns = uop.body[1:-1]
|
|
|
|
if not tkns:
|
|
|
|
return
|
|
|
|
tkn_iter = iter(tkns)
|
|
|
|
out.start_line()
|
|
|
|
for tkn in tkn_iter:
|
|
|
|
if tkn.kind == "IDENTIFIER" and tkn.text in replacement_functions:
|
|
|
|
replacement_functions[tkn.text](out, tkn, tkn_iter, uop, stack, inst)
|
|
|
|
else:
|
|
|
|
out.emit(tkn)
|
2023-12-20 10:27:25 -04:00
|
|
|
|
|
|
|
|
|
|
|
def cflags(p: Properties) -> str:
|
|
|
|
flags: list[str] = []
|
|
|
|
if p.oparg:
|
|
|
|
flags.append("HAS_ARG_FLAG")
|
|
|
|
if p.uses_co_consts:
|
|
|
|
flags.append("HAS_CONST_FLAG")
|
|
|
|
if p.uses_co_names:
|
|
|
|
flags.append("HAS_NAME_FLAG")
|
|
|
|
if p.jumps:
|
|
|
|
flags.append("HAS_JUMP_FLAG")
|
|
|
|
if p.has_free:
|
|
|
|
flags.append("HAS_FREE_FLAG")
|
|
|
|
if p.uses_locals:
|
|
|
|
flags.append("HAS_LOCAL_FLAG")
|
|
|
|
if p.eval_breaker:
|
|
|
|
flags.append("HAS_EVAL_BREAK_FLAG")
|
|
|
|
if p.deopts:
|
|
|
|
flags.append("HAS_DEOPT_FLAG")
|
2024-02-20 05:39:55 -04:00
|
|
|
if p.side_exit:
|
|
|
|
flags.append("HAS_EXIT_FLAG")
|
2023-12-20 10:27:25 -04:00
|
|
|
if not p.infallible:
|
|
|
|
flags.append("HAS_ERROR_FLAG")
|
2024-03-26 06:35:11 -03:00
|
|
|
if p.error_without_pop:
|
|
|
|
flags.append("HAS_ERROR_NO_POP_FLAG")
|
2023-12-20 10:27:25 -04:00
|
|
|
if p.escapes:
|
|
|
|
flags.append("HAS_ESCAPES_FLAG")
|
2024-01-12 13:30:27 -04:00
|
|
|
if p.pure:
|
|
|
|
flags.append("HAS_PURE_FLAG")
|
2024-02-20 06:50:59 -04:00
|
|
|
if p.oparg_and_1:
|
|
|
|
flags.append("HAS_OPARG_AND_1_FLAG")
|
2023-12-20 10:27:25 -04:00
|
|
|
if flags:
|
|
|
|
return " | ".join(flags)
|
|
|
|
else:
|
|
|
|
return "0"
|