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