Changeset 6aa430b in buchla-68k for misc/gen-glob.py


Ignore:
Timestamp:
07/10/2017 07:11:13 PM (7 years ago)
Author:
Thomas Lopatic <thomas@…>
Branches:
master
Children:
e877e55
Parents:
14ad9c9
Message:

More accurate headers.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • misc/gen-glob.py

    r14ad9c9 r6aa430b  
    55
    66cross_gcc = "/opt/cross-m68k/bin/m68k-none-elf-gcc"
    7 gen = c_generator.CGenerator()
    87
    9 inc_map = {
    10     "int8_t": "stdint.h",
    11     "uint8_t": "stdint.h",
    12     "int16_t": "stdint.h",
    13     "uint16_t": "stdint.h",
    14     "int32_t": "stdint.h",
    15     "uint32_t": "stdint.h"
     8class InclVis(c_ast.NodeVisitor):
     9    def __init__(self, path):
     10        self.path = path
     11        self.typs = set()
     12
     13    def visit_Typedef(self, node):
     14        if node.coord.file == self.path:
     15            self.typs.add(node.name)
     16
     17        self.generic_visit(node)
     18
     19    def visit_Struct(self, node):
     20        if node.coord.file == self.path and \
     21           node.name is not None and node.decls is not None:
     22            self.typs.add(node.name)
     23
     24        self.generic_visit(node)
     25
     26    def get_typs(self):
     27        return self.typs
     28
     29typ_map = {
     30    "void": None
    1631}
    1732
     
    1934    for path in f:
    2035        path = path.rstrip()
     36
     37        if len(path) < 8 or path[:8] != "include/":
     38            continue
     39
     40        stdout.write("parsing {}                    \r".format(path))
     41        stdout.flush()
     42
     43        ast = parse_file(path, use_cpp = True, cpp_path = cross_gcc,
     44                         cpp_args = ["-E", "-I", "include"])
     45        # ast.show()
     46
     47        vis = InclVis(path)
     48        vis.visit(ast)
     49
     50        for typ in vis.get_typs():
     51            if typ in typ_map:
     52                raise Exception("redefinition of {}".format(typ))
     53
     54            typ_map[typ] = path[8:]
     55
     56class DeclVis(c_ast.NodeVisitor):
     57    def __init__(self, typ_map):
     58        self.typ_map = typ_map
     59        self.typs = set()
     60
     61    def visit_IdentifierType(self, node):
     62        if node.names[0] not in self.typ_map:
     63            raise Exception("unknown type {} in {}:{}". \
     64                            format(node.names[0], node.coord.file, node.coord.line))
     65
     66        self.typs.add(node.names[0])
     67        self.generic_visit(node)
     68
     69    def visit_Struct(self, node):
     70        if node.name not in self.typ_map:
     71            raise Exception("unknown struct {} in {}:{}". \
     72                            format(node.name, node.coord.file, node.coord.line))
     73
     74        self.typs.add(node.name)
     75        self.generic_visit(node)
     76
     77    def get_typs(self):
     78        return self.typs
     79
     80gen = c_generator.CGenerator()
     81
     82with open("misc/c-files.txt", "r") as f:
     83    for path in f:
     84        path = path.rstrip()
     85
     86        if len(path) >= 8 and path[:8] == "include/":
     87            continue
    2188
    2289        if path == "ram/wdfield.c": # breaks pycparser
     
    3097        # ast.show()
    3198
     99        incs = set()
    32100        funs = {}
    33101        vars = {}
    34         incs = set()
    35102
    36         for ext in ast.ext:
    37             # assumes that includes come before .c files in c-files.txt, i.e.,
    38             # that we've already seen the .h file, when its declarations are used
    39             # by a .c file.
     103        for node in ast.ext:
     104            if type(node) is c_ast.Decl and \
     105               (type(node.type) is c_ast.TypeDecl or \
     106                type(node.type) is c_ast.PtrDecl or \
     107                type(node.type) is c_ast.ArrayDecl):
     108                decl = node
     109                dest = vars
    40110
    41             if ext.coord.file == path and path[-2:] == ".h":
    42                 if type(ext) is c_ast.Decl and \
    43                    type(ext.type) is c_ast.Struct:
    44                     inc_map[ext.type.name] = path.split("/")[-1]
    45                     continue
    46 
    47             if type(ext) is c_ast.Decl and \
    48                (type(ext.type) is c_ast.TypeDecl or \
    49                 type(ext.type) is c_ast.PtrDecl or \
    50                 type(ext.type) is c_ast.ArrayDecl):
    51                 decl = ext
    52                 map = vars
    53 
    54             elif type(ext) is c_ast.FuncDef:
    55                 decl = ext.decl
    56                 map = funs
     111            elif type(node) is c_ast.FuncDef:
     112                decl = node.decl
     113                dest = funs
    57114
    58115            else:
     
    63120                continue
    64121
     122            vis = DeclVis(typ_map)
     123            vis.visit(decl)
     124
     125            for typ in vis.get_typs():
     126                if typ_map[typ] is not None:
     127                    incs.add(typ_map[typ])
     128
    65129            decl.storage = ["extern"]
    66130            decl.init = None
    67131
    68132            toks = gen.visit(decl).split(" ")
    69 
    70             for tok in toks:
    71                 if tok in inc_map:
    72                     incs.add(inc_map[tok])
    73 
    74133            alig = ""
    75134
     
    87146                alig += toks[0] + ("\t\t" if len(toks[0]) < 8 else "\t")
    88147                toks = toks[1:]
    89 
    90             map[decl.name] = alig + " ".join(toks) + ";"
     148
     149            dest[decl.name] = alig + " ".join(toks) + ";"
    91150
    92151        file = path.split("/")[-1]
     
    135194
    136195        with open(path[:-2] + ".x", "w") as f:
    137             print("\n".join(glob), file = f)
     196            f.write("\n".join(glob))
    138197
    139198    print("")
Note: See TracChangeset for help on using the changeset viewer.