X Tutup
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Refactor a bit
  • Loading branch information
markshannon committed Nov 26, 2023
commit 3204e55d295f15d89cc3d32fbe9a570375adb1b7
12 changes: 12 additions & 0 deletions Tools/cases_generator/generators_common.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
from pathlib import Path
from typing import TextIO

ROOT = Path(__file__).parent.parent.parent
DEFAULT_INPUT = (ROOT / "Python/bytecodes.c").absolute()


def root_relative_path(filename: str) -> str:
return Path(filename).relative_to(ROOT).as_posix()


def write_header(generator: str, source: str, outfile: TextIO) -> None:
outfile.write(
f"""// This file is generated by {root_relative_path(generator)}
// from:
// {source}
// Do not edit!
"""
)
47 changes: 17 additions & 30 deletions Tools/cases_generator/opcode_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,19 @@
from generators_common import (
DEFAULT_INPUT,
ROOT,
root_relative_path,
write_header,
)
from cwriter import CWriter
from typing import TextIO, Iterator
from lexer import Token
from stack import StackOffset

THIS = root_relative_path(__file__)

DEFAULT_OUTPUT = (ROOT / "Include/opcode_ids.h")
DEFAULT_OUTPUT = ROOT / "Include/opcode_ids.h"


def write_header(filename: str, outfile: TextIO) -> None:
outfile.write(
f"""// This file is generated by {THIS}
// from:
// {filename}
// Do not edit!
"""
)


def generate_opcode_header(
filenames: str, analysis: Analysis, outfile: TextIO
) -> None:


write_header(filenames, outfile)
def generate_opcode_header(filenames: str, analysis: Analysis, outfile: TextIO) -> None:
write_header(__file__, filenames, outfile)
out = CWriter(outfile, 0, False)
out.emit("\n")
instmap: dict[str, int] = {}
Expand All @@ -63,7 +48,9 @@ def generate_opcode_header(
instmap["RESUME"] = 149
instmap["INSTRUMENTED_LINE"] = 254

instrumented = [name for name in analysis.instructions if name.startswith("INSTRUMENTED")]
instrumented = [
name for name in analysis.instructions if name.startswith("INSTRUMENTED")
]

# Special case: this instruction is implemented in ceval.c
# rather than bytecodes.c, so we need to add it explicitly
Expand Down Expand Up @@ -100,7 +87,7 @@ def generate_opcode_header(
def add_instruction(name: str) -> None:
nonlocal next_opcode
if name in instmap:
return # Pre-defined name
return # Pre-defined name
while next_opcode in instmap.values():
next_opcode += 1
instmap[name] = next_opcode
Expand All @@ -120,24 +107,26 @@ def add_instruction(name: str) -> None:

for op, name in enumerate(sorted(analysis.pseudos), 256):
instmap[name] = op

assert 255 not in instmap.values()

out.emit("""#ifndef Py_OPCODE_IDS_H
assert 255 not in instmap.values()

out.emit(
"""#ifndef Py_OPCODE_IDS_H
#define Py_OPCODE_IDS_H
#ifdef __cplusplus
extern "C" {
#endif

/* Instruction opcodes for compiled code */
""")
"""
)

def write_define(name:str, op: int) -> None:
out.emit(f'#define {name:<38} {op:>3}\n')
def write_define(name: str, op: int) -> None:
out.emit(f"#define {name:<38} {op:>3}\n")

for op, name in sorted([(op, name) for (name, op) in instmap.items()]):
write_define(name, op)

out.emit("\n")
write_define("HAVE_ARGUMENT", len(no_arg))
write_define("MIN_INSTRUMENTED_OPCODE", min_instrumented)
Expand All @@ -149,7 +138,6 @@ def write_define(name:str, op: int) -> None:
out.emit("#endif /* !Py_OPCODE_IDS_H */\n")



arg_parser = argparse.ArgumentParser(
description="Generate the header file with all opcode IDs.",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
Expand All @@ -159,7 +147,6 @@ def write_define(name:str, op: int) -> None:
"-o", "--output", type=str, help="Generated code", default=DEFAULT_OUTPUT
)


arg_parser.add_argument(
"input", nargs=argparse.REMAINDER, help="Instruction definition file(s)"
)
Expand Down
31 changes: 11 additions & 20 deletions Tools/cases_generator/tier1_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,15 @@
from generators_common import (
DEFAULT_INPUT,
ROOT,
root_relative_path,
write_header,
)
from cwriter import CWriter
from typing import TextIO, Iterator
from lexer import Token
from stack import StackOffset


THIS = root_relative_path(__file__)

DEFAULT_OUTPUT = (ROOT / "Python/generated_cases.c.h")


def write_header(filename: str, outfile: TextIO) -> None:
outfile.write(
f"""// This file is generated by {THIS}
// from:
// {filename}
// Do not edit!

#ifdef TIER_TWO
#error "This file is for Tier 1 only"
#endif
#define TIER_ONE 1
"""
)
DEFAULT_OUTPUT = ROOT / "Python/generated_cases.c.h"


FOOTER = "#undef TIER_ONE\n"
Expand Down Expand Up @@ -353,7 +336,15 @@ def uses_this(inst: Instruction) -> bool:
def generate_tier1(
filenames: str, analysis: Analysis, outfile: TextIO, lines: bool
) -> None:
write_header(filenames, outfile)
write_header(__file__, filenames, outfile)
outfile.write(
"""
#ifdef TIER_TWO
#error "This file is for Tier 1 only"
#endif
#define TIER_ONE 1
"""
)
out = CWriter(outfile, 2, lines)
out.emit("\n")
for name, inst in sorted(analysis.instructions.items()):
Expand Down
X Tutup