sim: ppc: move spreg.[ch] files to the source tree

Simplify the build by moving the generation of these files from
build-time (via dgen.c that we have to compile & execute on the
build system) to maintainer/release mode (via spreg-gen.py that
we only ever execute when the spreg table actually changes).  It
speeds up the build process and makes it easier for us to reason
about & review changes to the code generator.

The tool is renamed from "dgen" because it's hardcoded to only
generated spreg files.  It isn't a generalized tool for creating
lookup tables.
This commit is contained in:
Mike Frysinger 2022-11-10 02:52:45 +07:00
parent 0d90ae96c5
commit ee3314c436
5 changed files with 1608 additions and 0 deletions

View File

@ -3469,6 +3469,16 @@ testsuite/common/bits64m63.c: testsuite/common/bits-gen$(EXEEXT) testsuite/commo
@SIM_ENABLE_ARCH_ppc_TRUE@ppc/%.o: ppc/%.c | ppc/libsim.a $(SIM_ALL_RECURSIVE_DEPS)
@SIM_ENABLE_ARCH_ppc_TRUE@ $(MAKE) $(AM_MAKEFLAGS) -C $(@D) $(@F)
@SIM_ENABLE_ARCH_ppc_TRUE@ppc/spreg.c: @MAINT@ ppc/ppc-spr-table ppc/spreg-gen.py ppc/$(am__dirstamp)
@SIM_ENABLE_ARCH_ppc_TRUE@ $(AM_V_GEN)$(srcdir)/ppc/spreg-gen.py --source $@.tmp
@SIM_ENABLE_ARCH_ppc_TRUE@ $(AM_V_at)$(SHELL) $(srcroot)/move-if-change $@.tmp $(srcdir)/ppc/spreg.c
@SIM_ENABLE_ARCH_ppc_TRUE@ $(AM_V_at)touch $(srcdir)/ppc/spreg.c
@SIM_ENABLE_ARCH_ppc_TRUE@ppc/spreg.h: @MAINT@ ppc/ppc-spr-table ppc/spreg-gen.py ppc/$(am__dirstamp)
@SIM_ENABLE_ARCH_ppc_TRUE@ $(AM_V_GEN)$(srcdir)/ppc/spreg-gen.py --header $@.tmp
@SIM_ENABLE_ARCH_ppc_TRUE@ $(AM_V_at)$(SHELL) $(srcroot)/move-if-change $@.tmp $(srcdir)/ppc/spreg.h
@SIM_ENABLE_ARCH_ppc_TRUE@ $(AM_V_at)touch $(srcdir)/ppc/spreg.h
@SIM_ENABLE_ARCH_rl78_TRUE@rl78/%.o: rl78/%.c | rl78/libsim.a $(SIM_ALL_RECURSIVE_DEPS)
@SIM_ENABLE_ARCH_rl78_TRUE@ $(MAKE) $(AM_MAKEFLAGS) -C $(@D) $(@F)

View File

@ -30,5 +30,15 @@
noinst_PROGRAMS += %D%/run %D%/psim
%D%/spreg.c: @MAINT@ %D%/ppc-spr-table %D%/spreg-gen.py %D%/$(am__dirstamp)
$(AM_V_GEN)$(srcdir)/%D%/spreg-gen.py --source $@.tmp
$(AM_V_at)$(SHELL) $(srcroot)/move-if-change $@.tmp $(srcdir)/%D%/spreg.c
$(AM_V_at)touch $(srcdir)/%D%/spreg.c
%D%/spreg.h: @MAINT@ %D%/ppc-spr-table %D%/spreg-gen.py %D%/$(am__dirstamp)
$(AM_V_GEN)$(srcdir)/%D%/spreg-gen.py --header $@.tmp
$(AM_V_at)$(SHELL) $(srcroot)/move-if-change $@.tmp $(srcdir)/%D%/spreg.h
$(AM_V_at)touch $(srcdir)/%D%/spreg.h
%C%docdir = $(docdir)/%C%
%C%doc_DATA = %D%/BUGS %D%/INSTALL %D%/README %D%/RUN

305
sim/ppc/spreg-gen.py Executable file
View File

@ -0,0 +1,305 @@
#!/usr/bin/env python3
# Copyright (C) 1994-1995 Andrew Cagney <cagney@highland.com.au>
# Copyright (C) 1996-2022 Free Software Foundation, Inc.
#
# This file is part of the GNU simulators.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""Helper to generate spreg.[ch] files."""
import argparse
from pathlib import Path
import sys
from typing import NamedTuple, TextIO
FILE = Path(__file__).resolve()
DIR = FILE.parent
NR_OF_SPRS = 1024
SPREG_ATTRIBUTES = (
"is_valid",
"is_readonly",
"name",
"index",
"length",
)
class Spreg(NamedTuple):
"""A single spreg entry."""
name: str
reg_nr: int
is_readonly: int
length: int
def load_table(source: Path) -> list[Spreg]:
"""Load the spreg table & return all entries in it."""
ret = []
with source.open("r", encoding="utf-8") as fp:
for i, line in enumerate(fp):
line = line.split("#", 1)[0].strip()
if not line:
# Skip blank & comment lines.
continue
fields = line.split(":")
assert len(fields) == 4, f"{source}:{i}: bad line: {line}"
spreg = Spreg(
name=fields[0].lower(),
reg_nr=int(fields[1]),
is_readonly=int(fields[2]),
length=int(fields[3]),
)
ret.append(spreg)
return sorted(ret, key=lambda x: x[1])
def print_copyleft(fp: TextIO) -> None:
"""Write out the standard copyright & license file block."""
fp.write(
f"""\
/* DO NOT EDIT: GENERATED BY {FILE.name}.
Copyright (C) 1994-1995 Andrew Cagney <cagney@highland.com.au>
Copyright (C) 1996-2022 Free Software Foundation, Inc.
This file is part of the GNU simulators.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
"""
)
def gen_header(table: list[Spreg], output: Path) -> None:
"""Write header to |output| from spreg |table|."""
with output.open("w", encoding="utf-8") as fp:
print_copyleft(fp)
fp.write(
"""
#ifndef _SPREG_H_
#define _SPREG_H_
typedef unsigned_word spreg;
typedef enum {
"""
)
for spreg in table:
fp.write(f" spr_{spreg.name} = {spreg.reg_nr},\n")
fp.write(
f"""\
nr_of_sprs = {NR_OF_SPRS}
}} sprs;
"""
)
for attr in SPREG_ATTRIBUTES:
ret_type = "const char *" if attr == "name" else "int"
fp.write(f"INLINE_SPREG({ret_type}) spr_{attr}(sprs spr);\n")
fp.write(
"""
#endif /* _SPREG_H_ */
"""
)
def gen_switch_is_valid(table: list[Spreg], fp: TextIO) -> None:
"""Generate switch table for is_valid property."""
fp.write(" switch (spr) {\n")
# Output all the known registers. We'll return 1 for them.
for spreg in table:
fp.write(f" case {spreg.reg_nr}:\n")
# All other registers return 0.
fp.write(
"""\
return 1;
}
return 0;
"""
)
def gen_switch_is_readonly(table: list[Spreg], fp: TextIO) -> None:
"""Generate switch table for is_readonly property."""
# Find any readonly registers and output a switch for them.
# If there aren't any, we can optimize this away to a single return.
output_switch = False
has_readonly = False
for spreg in table:
if spreg.is_readonly:
if not output_switch:
fp.write(" switch (spr) {\n")
output_switch = True
has_readonly = True
fp.write(f" case {spreg.reg_nr}:\n")
if has_readonly:
fp.write(" return 1;\n")
if output_switch:
fp.write(" }\n")
fp.write(" return 0;\n")
def gen_switch_length(table: list[Spreg], fp: TextIO) -> None:
"""Generate switch table for length property."""
# Find any registers with a length property and output a switch for them.
# If there aren't any, we can optimize this away to a single return.
output_switch = False
for spreg in table:
if spreg.length:
if not output_switch:
fp.write(" switch (spr) {\n")
output_switch = True
fp.write(f" case {spreg.reg_nr}:\n")
fp.write(f" return {spreg.length};\n")
if output_switch:
fp.write(" }\n")
fp.write(" return 0;\n")
def gen_source(table: list[Spreg], output: Path) -> None:
"""Write header to |output| from spreg |table|."""
with output.open("w", encoding="utf-8") as fp:
print_copyleft(fp)
fp.write(
"""
#ifndef _SPREG_C_
#define _SPREG_C_
#include "basics.h"
#include "spreg.h"
typedef struct _spreg_info {
const char *name;
int is_valid;
int length;
int is_readonly;
int index;
} spreg_info;
static const spreg_info spr_info[nr_of_sprs+1] = {
"""
)
entries = iter(table)
entry = next(entries)
for spreg_nr in range(0, NR_OF_SPRS + 1):
if entry is None or spreg_nr < entry.reg_nr:
fp.write(f" {{ 0, 0, 0, 0, {spreg_nr} }},\n")
else:
fp.write(
f' {{ "{entry.name}", 1, {entry.length}, {entry.is_readonly}, spr_{entry.name} /*{spreg_nr}*/ }},\n'
)
entry = next(entries, None)
fp.write("};\n")
for attr in SPREG_ATTRIBUTES:
ret_type = "const char *" if attr == "name" else "int"
fp.write(
f"""
INLINE_SPREG({ret_type}) spr_{attr}(sprs spr)
{{
"""
)
if attr not in ("index", "name"):
fp.write(
"""\
#ifdef WITH_SPREG_SWITCH_TABLE
"""
)
if attr == "is_valid":
gen_switch_is_valid(table, fp)
elif attr == "is_readonly":
gen_switch_is_readonly(table, fp)
elif attr == "length":
gen_switch_length(table, fp)
else:
assert False, f"{attr}: Unknown attribute"
fp.write("#else\n")
fp.write(f" return spr_info[spr].{attr};\n")
if attr not in ("index", "name"):
fp.write("#endif\n")
fp.write("}\n")
fp.write("\n#endif /* _SPREG_C_ */\n")
def get_parser() -> argparse.ArgumentParser:
"""Get CLI parser."""
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument(
"--table",
type=Path,
default=DIR / "ppc-spr-table",
help="path to source table",
)
parser.add_argument("--header", type=Path, help="path to header (.h) file")
parser.add_argument("--source", type=Path, help="path to source (.c) file")
return parser
def parse_args(argv: list[str]) -> argparse.Namespace:
"""Process the command line & default options."""
parser = get_parser()
opts = parser.parse_args(argv)
if not opts.header and not opts.source:
opts.header = DIR / "spreg.h"
opts.source = DIR / "spreg.c"
return opts
def main(argv: list[str]) -> int:
"""The main entry point for scripts."""
opts = parse_args(argv)
table = load_table(opts.table)
if opts.header:
gen_header(table, opts.header)
if opts.source:
gen_source(table, opts.source)
return 0
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))

1175
sim/ppc/spreg.c Normal file

File diff suppressed because it is too large Load Diff

108
sim/ppc/spreg.h Normal file
View File

@ -0,0 +1,108 @@
/* DO NOT EDIT: GENERATED BY spreg-gen.py.
Copyright (C) 1994-1995 Andrew Cagney <cagney@highland.com.au>
Copyright (C) 1996-2022 Free Software Foundation, Inc.
This file is part of the GNU simulators.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#ifndef _SPREG_H_
#define _SPREG_H_
typedef unsigned_word spreg;
typedef enum {
spr_mq = 0,
spr_xer = 1,
spr_rtcu = 4,
spr_rtcl = 5,
spr_lr = 8,
spr_ctr = 9,
spr_dsisr = 18,
spr_dar = 19,
spr_dec = 22,
spr_sdr1 = 25,
spr_srr0 = 26,
spr_srr1 = 27,
spr_vrsave = 256,
spr_tbrl = 268,
spr_tbru = 269,
spr_sprg0 = 272,
spr_sprg1 = 273,
spr_sprg2 = 274,
spr_sprg3 = 275,
spr_ear = 282,
spr_tbl = 284,
spr_tbu = 285,
spr_pvr = 287,
spr_spefscr = 512,
spr_ibat0u = 528,
spr_ibat0l = 529,
spr_ibat1u = 530,
spr_ibat1l = 531,
spr_ibat2u = 532,
spr_ibat2l = 533,
spr_ibat3u = 534,
spr_ibat3l = 535,
spr_dbat0u = 536,
spr_dbat0l = 537,
spr_dbat1u = 538,
spr_dbat1l = 539,
spr_dbat2u = 540,
spr_dbat2l = 541,
spr_dbat3u = 542,
spr_dbat3l = 543,
spr_ummcr0 = 936,
spr_upmc1 = 937,
spr_upmc2 = 938,
spr_usia = 939,
spr_ummcr1 = 940,
spr_upmc3 = 941,
spr_upmc4 = 942,
spr_mmcr0 = 952,
spr_pmc1 = 953,
spr_pmc2 = 954,
spr_sia = 955,
spr_mmcr1 = 956,
spr_pmc3 = 957,
spr_pmc4 = 958,
spr_sda = 959,
spr_dmiss = 976,
spr_dcmp = 977,
spr_hash1 = 978,
spr_hash2 = 979,
spr_imiss = 980,
spr_icmp = 981,
spr_rpa = 982,
spr_hid0 = 1008,
spr_hid1 = 1009,
spr_iabr = 1010,
spr_dabr = 1013,
spr_l2cr = 1017,
spr_ictc = 1019,
spr_thrm1 = 1020,
spr_thrm2 = 1021,
spr_thrm3 = 1022,
spr_pir = 1023,
nr_of_sprs = 1024
} sprs;
INLINE_SPREG(int) spr_is_valid(sprs spr);
INLINE_SPREG(int) spr_is_readonly(sprs spr);
INLINE_SPREG(const char *) spr_name(sprs spr);
INLINE_SPREG(int) spr_index(sprs spr);
INLINE_SPREG(int) spr_length(sprs spr);
#endif /* _SPREG_H_ */