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