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,
|
2023-12-20 10:27:25 -04:00
|
|
|
Properties,
|
2024-07-09 07:33:56 -03:00
|
|
|
StackItem,
|
2024-08-07 14:23:53 -03:00
|
|
|
analysis_error,
|
2023-12-12 08:12:17 -04:00
|
|
|
)
|
|
|
|
from cwriter import CWriter
|
2024-08-01 05:27:26 -03:00
|
|
|
from typing import Callable, Mapping, TextIO, Iterator
|
2023-12-12 08:12:17 -04:00
|
|
|
from lexer import Token
|
2024-06-17 16:58:56 -03:00
|
|
|
from stack import Stack
|
2023-12-12 08:12:17 -04:00
|
|
|
|
|
|
|
|
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-08-01 05:27:26 -03:00
|
|
|
def type_and_null(var: StackItem) -> tuple[str, str]:
|
2024-07-09 07:33:56 -03:00
|
|
|
if var.type:
|
|
|
|
return var.type, "NULL"
|
|
|
|
elif var.is_array():
|
|
|
|
return "_PyStackRef *", "NULL"
|
|
|
|
else:
|
|
|
|
return "_PyStackRef", "PyStackRef_NULL"
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2024-08-08 06:57:59 -03:00
|
|
|
|
2024-08-06 09:04:33 -03:00
|
|
|
ReplacementFunctionType = Callable[
|
|
|
|
[Token, Iterator[Token], Uop, Stack, Instruction | None], None
|
|
|
|
]
|
2023-12-12 08:12:17 -04:00
|
|
|
|
|
|
|
|
2024-08-08 06:57:59 -03:00
|
|
|
class Emitter:
|
2024-08-06 09:04:33 -03:00
|
|
|
out: CWriter
|
|
|
|
_replacers: dict[str, ReplacementFunctionType]
|
2023-12-12 08:12:17 -04:00
|
|
|
|
2024-08-06 09:04:33 -03:00
|
|
|
def __init__(self, out: CWriter):
|
|
|
|
self._replacers = {
|
|
|
|
"EXIT_IF": self.exit_if,
|
|
|
|
"DEOPT_IF": self.deopt_if,
|
|
|
|
"ERROR_IF": self.error_if,
|
|
|
|
"ERROR_NO_POP": self.error_no_pop,
|
|
|
|
"DECREF_INPUTS": self.decref_inputs,
|
|
|
|
"SYNC_SP": self.sync_sp,
|
2024-08-07 14:23:53 -03:00
|
|
|
"PyStackRef_FromPyObjectNew": self.py_stack_ref_from_py_object_new,
|
2024-08-06 09:04:33 -03:00
|
|
|
}
|
|
|
|
self.out = out
|
2023-12-12 08:12:17 -04:00
|
|
|
|
2024-08-06 09:04:33 -03:00
|
|
|
def deopt_if(
|
|
|
|
self,
|
|
|
|
tkn: Token,
|
|
|
|
tkn_iter: Iterator[Token],
|
|
|
|
uop: Uop,
|
|
|
|
unused: Stack,
|
|
|
|
inst: Instruction | None,
|
|
|
|
) -> None:
|
|
|
|
self.out.emit_at("DEOPT_IF", tkn)
|
|
|
|
self.out.emit(next(tkn_iter))
|
|
|
|
emit_to(self.out, tkn_iter, "RPAREN")
|
|
|
|
next(tkn_iter) # Semi colon
|
|
|
|
self.out.emit(", ")
|
|
|
|
assert inst is not None
|
|
|
|
assert inst.family is not None
|
|
|
|
self.out.emit(inst.family.name)
|
|
|
|
self.out.emit(");\n")
|
2024-03-26 06:35:11 -03:00
|
|
|
|
2024-08-06 09:04:33 -03:00
|
|
|
exit_if = deopt_if
|
2024-03-26 06:35:11 -03:00
|
|
|
|
2024-08-06 09:04:33 -03:00
|
|
|
def error_if(
|
|
|
|
self,
|
|
|
|
tkn: Token,
|
|
|
|
tkn_iter: Iterator[Token],
|
|
|
|
uop: Uop,
|
|
|
|
stack: Stack,
|
|
|
|
inst: Instruction | None,
|
|
|
|
) -> None:
|
|
|
|
self.out.emit_at("if ", tkn)
|
|
|
|
self.out.emit(next(tkn_iter))
|
|
|
|
emit_to(self.out, tkn_iter, "COMMA")
|
|
|
|
label = next(tkn_iter).text
|
|
|
|
next(tkn_iter) # RPAREN
|
|
|
|
next(tkn_iter) # Semi colon
|
|
|
|
self.out.emit(") ")
|
|
|
|
c_offset = stack.peek_offset()
|
|
|
|
try:
|
|
|
|
offset = -int(c_offset)
|
|
|
|
except ValueError:
|
|
|
|
offset = -1
|
|
|
|
if offset > 0:
|
|
|
|
self.out.emit(f"goto pop_{offset}_")
|
|
|
|
self.out.emit(label)
|
|
|
|
self.out.emit(";\n")
|
|
|
|
elif offset == 0:
|
|
|
|
self.out.emit("goto ")
|
|
|
|
self.out.emit(label)
|
|
|
|
self.out.emit(";\n")
|
2023-12-12 08:12:17 -04:00
|
|
|
else:
|
2024-08-06 09:04:33 -03:00
|
|
|
self.out.emit("{\n")
|
|
|
|
stack.flush_locally(self.out)
|
|
|
|
self.out.emit("goto ")
|
|
|
|
self.out.emit(label)
|
|
|
|
self.out.emit(";\n")
|
|
|
|
self.out.emit("}\n")
|
2023-12-12 08:12:17 -04:00
|
|
|
|
2024-08-06 09:04:33 -03:00
|
|
|
def error_no_pop(
|
|
|
|
self,
|
|
|
|
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
|
|
|
|
self.out.emit_at("goto error;", tkn)
|
2023-12-12 08:12:17 -04:00
|
|
|
|
2024-08-06 09:04:33 -03:00
|
|
|
def decref_inputs(
|
|
|
|
self,
|
|
|
|
tkn: Token,
|
|
|
|
tkn_iter: Iterator[Token],
|
|
|
|
uop: Uop,
|
|
|
|
stack: Stack,
|
|
|
|
inst: Instruction | None,
|
|
|
|
) -> None:
|
|
|
|
next(tkn_iter)
|
|
|
|
next(tkn_iter)
|
|
|
|
next(tkn_iter)
|
|
|
|
self.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:
|
|
|
|
self.out.emit(f"for (int _i = {var.size}; --_i >= 0;) {{\n")
|
|
|
|
self.out.emit(f"PyStackRef_CLOSE({var.name}[_i]);\n")
|
|
|
|
self.out.emit("}\n")
|
|
|
|
elif var.condition:
|
|
|
|
if var.condition == "1":
|
|
|
|
self.out.emit(f"PyStackRef_CLOSE({var.name});\n")
|
|
|
|
elif var.condition != "0":
|
|
|
|
self.out.emit(f"PyStackRef_XCLOSE({var.name});\n")
|
|
|
|
else:
|
|
|
|
self.out.emit(f"PyStackRef_CLOSE({var.name});\n")
|
2023-12-12 08:12:17 -04:00
|
|
|
|
2024-08-06 09:04:33 -03:00
|
|
|
def sync_sp(
|
|
|
|
self,
|
|
|
|
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(self.out)
|
2023-12-12 08:12:17 -04:00
|
|
|
|
2024-08-07 14:23:53 -03:00
|
|
|
def py_stack_ref_from_py_object_new(
|
|
|
|
self,
|
|
|
|
tkn: Token,
|
|
|
|
tkn_iter: Iterator[Token],
|
|
|
|
uop: Uop,
|
|
|
|
stack: Stack,
|
|
|
|
inst: Instruction | None,
|
|
|
|
) -> None:
|
|
|
|
self.out.emit(tkn)
|
|
|
|
emit_to(self.out, tkn_iter, "SEMI")
|
|
|
|
self.out.emit(";\n")
|
|
|
|
|
|
|
|
target = uop.deferred_refs[tkn]
|
|
|
|
if target is None:
|
|
|
|
# An assignment we don't handle, such as to a pointer or array.
|
|
|
|
return
|
|
|
|
|
|
|
|
# Flush the assignment to the stack. Note that we don't flush the
|
|
|
|
# stack pointer here, and instead are currently relying on initializing
|
|
|
|
# unused portions of the stack to NULL.
|
|
|
|
stack.flush_single_var(self.out, target, uop.stack.outputs)
|
|
|
|
|
2024-08-06 09:04:33 -03:00
|
|
|
def emit_tokens(
|
|
|
|
self,
|
|
|
|
uop: Uop,
|
|
|
|
stack: Stack,
|
|
|
|
inst: Instruction | None,
|
|
|
|
) -> None:
|
|
|
|
tkns = uop.body[1:-1]
|
|
|
|
if not tkns:
|
|
|
|
return
|
|
|
|
tkn_iter = iter(tkns)
|
|
|
|
self.out.start_line()
|
|
|
|
for tkn in tkn_iter:
|
|
|
|
if tkn.kind == "IDENTIFIER" and tkn.text in self._replacers:
|
|
|
|
self._replacers[tkn.text](tkn, tkn_iter, uop, stack, inst)
|
|
|
|
else:
|
|
|
|
self.out.emit(tkn)
|
2023-12-20 10:27:25 -04:00
|
|
|
|
2024-08-06 09:04:33 -03:00
|
|
|
def emit(self, txt: str | Token) -> None:
|
|
|
|
self.out.emit(txt)
|
2023-12-20 10:27:25 -04:00
|
|
|
|
2024-08-08 06:57:59 -03:00
|
|
|
|
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"
|