Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # |
| 3 | # Copyright (c) 2017 Intel Corporation |
| 4 | # |
| 5 | # SPDX-License-Identifier: Apache-2.0 |
| 6 | |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 7 | """ |
| 8 | gperf C file post-processor |
| 9 | |
| 10 | We use gperf to build up a perfect hashtable of pointer values. The way gperf |
Ruslan Mstoi | 7d8addd | 2020-05-26 16:41:05 +0300 | [diff] [blame] | 11 | does this is to create a table 'wordlist' indexed by a string representation |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 12 | of a pointer address, and then doing memcmp() on a string passed in for |
| 13 | comparison |
| 14 | |
| 15 | We are exclusively working with 4-byte pointer values. This script adjusts |
| 16 | the generated code so that we work with pointers directly and not strings. |
| 17 | This saves a considerable amount of space. |
| 18 | """ |
| 19 | |
Ulf Magnusson | 605423f | 2019-03-25 20:29:13 +0100 | [diff] [blame] | 20 | import sys |
| 21 | import argparse |
| 22 | import os |
| 23 | import re |
Julien Massot | 36f116b | 2021-11-19 16:30:43 +0100 | [diff] [blame] | 24 | from packaging import version |
Ulf Magnusson | 605423f | 2019-03-25 20:29:13 +0100 | [diff] [blame] | 25 | |
| 26 | # --- debug stuff --- |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 27 | |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 28 | def debug(text): |
| 29 | if not args.verbose: |
| 30 | return |
| 31 | sys.stdout.write(os.path.basename(sys.argv[0]) + ": " + text + "\n") |
| 32 | |
| 33 | |
| 34 | def error(text): |
Ulf Magnusson | 50b9b12 | 2019-09-07 14:41:01 +0200 | [diff] [blame] | 35 | sys.exit(os.path.basename(sys.argv[0]) + " ERROR: " + text) |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 36 | |
| 37 | |
| 38 | def warn(text): |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 39 | sys.stdout.write( |
| 40 | os.path.basename( |
| 41 | sys.argv[0]) + |
| 42 | " WARNING: " + |
| 43 | text + |
| 44 | "\n") |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 45 | |
| 46 | |
| 47 | def reformat_str(match_obj): |
| 48 | addr_str = match_obj.group(0) |
| 49 | |
| 50 | # Nip quotes |
| 51 | addr_str = addr_str[1:-1] |
Andrew Boie | f290ab5 | 2019-11-18 17:06:13 -0800 | [diff] [blame] | 52 | addr_vals = [0, 0, 0, 0, 0, 0, 0 , 0] |
| 53 | ctr = 7 |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 54 | i = 0 |
| 55 | |
Ulf Magnusson | ba312fe | 2019-03-20 19:30:29 +0100 | [diff] [blame] | 56 | while True: |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 57 | if i >= len(addr_str): |
| 58 | break |
| 59 | |
| 60 | if addr_str[i] == "\\": |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 61 | if addr_str[i + 1].isdigit(): |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 62 | # Octal escape sequence |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 63 | val_str = addr_str[i + 1:i + 4] |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 64 | addr_vals[ctr] = int(val_str, 8) |
| 65 | i += 4 |
| 66 | else: |
| 67 | # Char value that had to be escaped by C string rules |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 68 | addr_vals[ctr] = ord(addr_str[i + 1]) |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 69 | i += 2 |
| 70 | |
| 71 | else: |
| 72 | addr_vals[ctr] = ord(addr_str[i]) |
| 73 | i += 1 |
| 74 | |
| 75 | ctr -= 1 |
| 76 | |
Andrew Boie | f290ab5 | 2019-11-18 17:06:13 -0800 | [diff] [blame] | 77 | return "(char *)0x%02x%02x%02x%02x%02x%02x%02x%02x" % tuple(addr_vals) |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 78 | |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 79 | |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 80 | def process_line(line, fp): |
| 81 | if line.startswith("#"): |
| 82 | fp.write(line) |
| 83 | return |
| 84 | |
| 85 | # Set the lookup function to static inline so it gets rolled into |
Andrew Boie | ae8acff | 2020-03-11 07:19:16 -0700 | [diff] [blame] | 86 | # z_object_find(), nothing else will use it |
Chunlin Han | 18560a0 | 2018-02-01 01:19:49 -0600 | [diff] [blame] | 87 | if re.search(args.pattern + " [*]$", line): |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 88 | fp.write("static inline " + line) |
| 89 | return |
| 90 | |
| 91 | m = re.search("gperf version (.*) [*][/]$", line) |
| 92 | if m: |
Julien Massot | 36f116b | 2021-11-19 16:30:43 +0100 | [diff] [blame] | 93 | v = version.parse(m.groups()[0]) |
| 94 | v_lo = version.parse("3.0") |
| 95 | v_hi = version.parse("3.1") |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 96 | if (v < v_lo or v > v_hi): |
| 97 | warn("gperf %s is not tested, versions %s through %s supported" % |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 98 | (v, v_lo, v_hi)) |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 99 | |
Andrew Boie | f290ab5 | 2019-11-18 17:06:13 -0800 | [diff] [blame] | 100 | # Replace length lookups with constant len since we're always |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 101 | # looking at pointers |
Andrew Boie | f290ab5 | 2019-11-18 17:06:13 -0800 | [diff] [blame] | 102 | line = re.sub(r'lengthtable\[key\]', r'sizeof(void *)', line) |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 103 | |
| 104 | # Empty wordlist entries to have NULLs instead of "" |
| 105 | line = re.sub(r'[{]["]["][}]', r'{}', line) |
| 106 | |
| 107 | # Suppress a compiler warning since this table is no longer necessary |
| 108 | line = re.sub(r'static unsigned char lengthtable', |
| 109 | r'static unsigned char __unused lengthtable', line) |
| 110 | |
| 111 | # drop all use of register keyword, let compiler figure that out, |
| 112 | # we have to do this since we change stuff to take the address of some |
| 113 | # parameters |
| 114 | line = re.sub(r'register', r'', line) |
| 115 | |
| 116 | # Hashing the address of the string |
| 117 | line = re.sub(r"hash [(]str, len[)]", |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 118 | r"hash((const char *)&str, len)", line) |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 119 | |
| 120 | # Just compare pointers directly instead of using memcmp |
| 121 | if re.search("if [(][*]str", line): |
| 122 | fp.write(" if (str == s)\n") |
| 123 | return |
| 124 | |
| 125 | # Take the strings with the binary information for the pointer values, |
| 126 | # and just turn them into pointers |
| 127 | line = re.sub(r'["].*["]', reformat_str, line) |
| 128 | |
| 129 | fp.write(line) |
| 130 | |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 131 | |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 132 | def parse_args(): |
| 133 | global args |
| 134 | |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 135 | parser = argparse.ArgumentParser( |
| 136 | description=__doc__, |
| 137 | formatter_class=argparse.RawDescriptionHelpFormatter) |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 138 | |
| 139 | parser.add_argument("-i", "--input", required=True, |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 140 | help="Input C file from gperf") |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 141 | parser.add_argument("-o", "--output", required=True, |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 142 | help="Output C file with processing done") |
Chunlin Han | 18560a0 | 2018-02-01 01:19:49 -0600 | [diff] [blame] | 143 | parser.add_argument("-p", "--pattern", required=True, |
| 144 | help="Search pattern for objects") |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 145 | parser.add_argument("-v", "--verbose", action="store_true", |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 146 | help="Print extra debugging information") |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 147 | args = parser.parse_args() |
Sebastian Bøe | 4971d2a | 2017-12-28 17:34:50 +0100 | [diff] [blame] | 148 | if "VERBOSE" in os.environ: |
| 149 | args.verbose = 1 |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 150 | |
| 151 | def main(): |
| 152 | parse_args() |
| 153 | |
| 154 | with open(args.input, "r") as in_fp, open(args.output, "w") as out_fp: |
| 155 | for line in in_fp.readlines(): |
| 156 | process_line(line, out_fp) |
| 157 | |
| 158 | |
| 159 | if __name__ == "__main__": |
| 160 | main() |