Cancel execution command on thread exit, when stepping, nexting, etc.
[binutils-gdb.git] / sim / common / gennltvals.py
index b3e558d580a91c56c1bd2a3791b9152ea78afd82..62304a8beab107d5cd1d2a46e1c3cb886ab3f97b 100755 (executable)
@@ -1,5 +1,5 @@
 #!/usr/bin/env python3
-# Copyright (C) 1996-2021 Free Software Foundation, Inc.
+# Copyright (C) 1996-2023 Free Software Foundation, Inc.
 #
 # This file is part of the GNU simulators.
 #
@@ -16,9 +16,9 @@
 # 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 nltvals.def.
+"""Helper to generate target-newlib-* files.
 
-nltvals.def is a file that describes various newlib/libgloss target values used
+target-newlib-* are files that describe various newlib/libgloss values used
 by the host/target interface.  This needs to be rerun whenever the newlib source
 changes.  Developers manually run it.
 
@@ -49,7 +49,8 @@ PROG = Path(__file__).name
 TARGET_DIRS = {
     'cr16': 'libgloss/cr16/sys',
     'd10v': 'newlib/libc/sys/d10v/sys',
-    'i960': 'libgloss/i960',
+    # Port removed from the tree years ago.
+    #'i960': 'libgloss/i960',
     'mcore': 'libgloss/mcore',
     'riscv': 'libgloss/riscv/machine',
     'sh': 'newlib/libc/sys/sh/sys',
@@ -63,94 +64,174 @@ FILE_HEADER = f"""\
 /* This file is machine generated by {PROG}.  */\
 """
 
+# Used to update sections of files.
+START_MARKER = 'gennltvals: START'
+END_MARKER = 'gennltvals: END'
 
-def gentvals(output: TextIO, cpp: str, srctype: str, srcdir: Path,
-             headers: Iterable[str],
-             pattern: str,
-             target: str = None):
-    """Extract constants from the specified files using a regular expression.
 
-    We'll run things through the preprocessor.
-    """
-    headers = tuple(headers)
-
-    # Require all files exist in order to regenerate properly.
-    for header in headers:
-        fullpath = srcdir / header
-        assert fullpath.exists(), f'{fullpath} does not exist'
-
-    if target is not None:
-        print(f'#ifdef NL_TARGET_{target}', file=output)
-    print(f'#ifdef {srctype}_defs', file=output)
-
-    print('\n'.join(f'/* from {x} */' for x in headers), file=output)
-
-    if target is None:
-        print(f'/* begin {srctype} target macros */', file=output)
-    else:
-        print(f'/* begin {target} {srctype} target macros */', file=output)
-
-    # Extract all the symbols.
+def extract_syms(cpp: str, srcdir: Path,
+                 headers: Iterable[str],
+                 pattern: str,
+                 filter: str = r'^$') -> dict:
+    """Extract all the symbols from |headers| matching |pattern| using |cpp|."""
     srcfile = ''.join(f'#include <{x}>\n' for x in headers)
     syms = set()
     define_pattern = re.compile(r'^#\s*define\s+(' + pattern + ')')
+    filter_pattern = re.compile(filter)
     for header in headers:
         with open(srcdir / header, 'r', encoding='utf-8') as fp:
             data = fp.read()
         for line in data.splitlines():
             m = define_pattern.match(line)
-            if m:
+            if m and not filter_pattern.search(line):
                 syms.add(m.group(1))
-    for sym in sorted(syms):
-        srcfile += f'#ifdef {sym}\nDEFVAL {{ "{sym}", {sym} }},\n#endif\n'
+    for sym in syms:
+        srcfile += f'#ifdef {sym}\nDEFVAL "{sym}" {sym}\n#endif\n'
 
     result = subprocess.run(
         f'{cpp} -E -I"{srcdir}" -', shell=True, check=True, encoding='utf-8',
         input=srcfile, capture_output=True)
+    ret = {}
     for line in result.stdout.splitlines():
         if line.startswith('DEFVAL '):
-            print(line[6:].rstrip(), file=output)
+            _, sym, val = line.split()
+            ret[sym.strip('"')] = val
+    return ret
 
-    print(f'#undef {srctype}_defs', file=output)
-    if target is None:
-        print(f'/* end {srctype} target macros */', file=output)
-    else:
-        print(f'/* end {target} {srctype} target macros */', file=output)
-        print('#endif', file=output)
-    print('#endif', file=output)
 
+def gentvals(output_dir: Path,
+             cpp: str, srctype: str, srcdir: Path,
+             headers: Iterable[str],
+             pattern: str,
+             filter: str = r'^$',
+             target: str = None):
+    """Extract constants from the specified files using a regular expression.
+
+    We'll run things through the preprocessor.
+    """
+    headers = tuple(headers)
 
-def gen_common(output: TextIO, newlib: Path, cpp: str):
+    # Require all files exist in order to regenerate properly.
+    for header in headers:
+        fullpath = srcdir / header
+        assert fullpath.exists(), f'{fullpath} does not exist'
+
+    syms = extract_syms(cpp, srcdir, headers, pattern, filter)
+
+    target_map = output_dir / f'target-newlib-{srctype}.c'
+    assert target_map.exists(), f'{target_map}: Missing skeleton'
+    old_lines = target_map.read_text().splitlines()
+    start_i = end_i = None
+    for i, line in enumerate(old_lines):
+        if START_MARKER in line:
+            start_i = i
+        if END_MARKER in line:
+            end_i = i
+    assert start_i and end_i
+    new_lines = old_lines[0:start_i + 1]
+    new_lines.extend(
+        f'#ifdef {sym}\n'
+        f'  {{ "{sym}", {sym}, {val} }},\n'
+        f'#endif' for sym, val in sorted(syms.items()))
+    new_lines.extend(old_lines[end_i:])
+    target_map.write_text('\n'.join(new_lines) + '\n')
+
+
+def gen_common(output_dir: Path, newlib: Path, cpp: str):
     """Generate the common C library constants.
 
     No arch should override these.
     """
-    gentvals(output, cpp, 'errno', newlib / 'newlib/libc/include',
+    gentvals(output_dir, cpp, 'errno', newlib / 'newlib/libc/include',
              ('errno.h', 'sys/errno.h'), 'E[A-Z0-9]*')
 
-    gentvals(output, cpp, 'signal', newlib / 'newlib/libc/include',
-             ('signal.h', 'sys/signal.h'), r'SIG[A-Z0-9]*')
+    gentvals(output_dir, cpp, 'signal', newlib / 'newlib/libc/include',
+             ('signal.h', 'sys/signal.h'), r'SIG[A-Z0-9]*', filter=r'SIGSTKSZ')
 
-    gentvals(output, cpp, 'open', newlib / 'newlib/libc/include',
+    gentvals(output_dir, cpp, 'open', newlib / 'newlib/libc/include',
              ('fcntl.h', 'sys/fcntl.h', 'sys/_default_fcntl.h'), r'O_[A-Z0-9]*')
 
 
-def gen_targets(output: TextIO, newlib: Path, cpp: str):
-    """Generate the target-specific lists."""
+def gen_target_syscall(output_dir: Path, newlib: Path, cpp: str):
+    """Generate the target-specific syscall lists."""
+    target_map_c = output_dir / 'target-newlib-syscall.c'
+    old_lines_c = target_map_c.read_text().splitlines()
+    start_i = end_i = None
+    for i, line in enumerate(old_lines_c):
+        if START_MARKER in line:
+            start_i = i
+        if END_MARKER in line:
+            end_i = i
+    assert start_i and end_i, f'{target_map_c}: Unable to find markers'
+    new_lines_c = old_lines_c[0:start_i + 1]
+    new_lines_c_end = old_lines_c[end_i:]
+
+    target_map_h = output_dir / 'target-newlib-syscall.h'
+    old_lines_h = target_map_h.read_text().splitlines()
+    start_i = end_i = None
+    for i, line in enumerate(old_lines_h):
+        if START_MARKER in line:
+            start_i = i
+        if END_MARKER in line:
+            end_i = i
+    assert start_i and end_i, f'{target_map_h}: Unable to find markers'
+    new_lines_h = old_lines_h[0:start_i + 1]
+    new_lines_h_end = old_lines_h[end_i:]
+
+    headers = ('syscall.h',)
+    pattern = r'SYS_[_a-zA-Z0-9]*'
+
+    # Output the target-specific syscalls.
     for target, subdir in sorted(TARGET_DIRS.items()):
-        gentvals(output, cpp, 'sys', newlib / subdir, ('syscall.h',),
-                 r'SYS_[_a-zA-Z0-9]*', target=target)
+        syms = extract_syms(cpp, newlib / subdir, headers, pattern)
+        new_lines_c.append(f'CB_TARGET_DEFS_MAP cb_{target}_syscall_map[] = {{')
+        new_lines_c.extend(
+            f'#ifdef CB_{sym}\n'
+            '  { '
+            f'"{sym[4:]}", CB_{sym}, TARGET_NEWLIB_{target.upper()}_{sym}'
+            ' },\n'
+            '#endif' for sym in sorted(syms))
+        new_lines_c.append('  {NULL, -1, -1},')
+        new_lines_c.append('};\n')
+
+        new_lines_h.append(
+            f'extern CB_TARGET_DEFS_MAP cb_{target}_syscall_map[];')
+        new_lines_h.extend(
+            f'#define TARGET_NEWLIB_{target.upper()}_{sym} {val}'
+            for sym, val in sorted(syms.items()))
+        new_lines_h.append('')
 
     # Then output the common syscall targets.
-    gentvals(output, cpp, 'sys', newlib / 'libgloss', ('syscall.h',),
-             r'SYS_[_a-zA-Z0-9]*')
+    syms = extract_syms(cpp, newlib / 'libgloss', headers, pattern)
+    new_lines_c.append(f'CB_TARGET_DEFS_MAP cb_init_syscall_map[] = {{')
+    new_lines_c.extend(
+        f'#ifdef CB_{sym}\n'
+        f'  {{ "{sym[4:]}", CB_{sym}, TARGET_NEWLIB_{sym} }},\n'
+        f'#endif' for sym in sorted(syms))
+    new_lines_c.append('  {NULL, -1, -1},')
+    new_lines_c.append('};')
 
+    new_lines_h.append('extern CB_TARGET_DEFS_MAP cb_init_syscall_map[];')
+    new_lines_h.extend(
+        f'#define TARGET_NEWLIB_{sym} {val}'
+        for sym, val in sorted(syms.items()))
 
-def gen(output: TextIO, newlib: Path, cpp: str):
+    new_lines_c.extend(new_lines_c_end)
+    target_map_c.write_text('\n'.join(new_lines_c) + '\n')
+
+    new_lines_h.extend(new_lines_h_end)
+    target_map_h.write_text('\n'.join(new_lines_h) + '\n')
+
+
+def gen_targets(output_dir: Path, newlib: Path, cpp: str):
+    """Generate the target-specific lists."""
+    gen_target_syscall(output_dir, newlib, cpp)
+
+
+def gen(output_dir: Path, newlib: Path, cpp: str):
     """Generate all the things!"""
-    print(FILE_HEADER, file=output)
-    gen_common(output, newlib, cpp)
-    gen_targets(output, newlib, cpp)
+    gen_common(output_dir, newlib, cpp)
+    gen_targets(output_dir, newlib, cpp)
 
 
 def get_parser() -> argparse.ArgumentParser:
@@ -160,7 +241,7 @@ def get_parser() -> argparse.ArgumentParser:
         formatter_class=argparse.RawDescriptionHelpFormatter)
     parser.add_argument(
         '-o', '--output', type=Path,
-        help='write to the specified file instead of stdout')
+        help='write to the specified directory')
     parser.add_argument(
         '--cpp', type=str, default='cpp',
         help='the preprocessor to use')
@@ -178,8 +259,14 @@ def parse_args(argv: List[str]) -> argparse.Namespace:
     parser = get_parser()
     opts = parser.parse_args(argv)
 
+    if opts.output is None:
+        # Default to where the script lives.
+        opts.output = Path(__file__).resolve().parent
+
     if opts.srcroot is None:
         opts.srcroot = Path(__file__).resolve().parent.parent.parent
+    else:
+        opts.srcroot = opts.srcroot.resolve()
 
     if opts.newlib is None:
         # Try to find newlib relative to our source tree.
@@ -202,12 +289,7 @@ def main(argv: List[str]) -> int:
     """The main entry point for scripts."""
     opts = parse_args(argv)
 
-    if opts.output is not None:
-        output = open(opts.output, 'w', encoding='utf-8')
-    else:
-        output = sys.stdout
-
-    gen(output, opts.newlib, opts.cpp)
+    gen(opts.output, opts.newlib, opts.cpp)
     return 0