- Timestamp:
- 07/10/2017 07:11:13 PM (8 years ago)
- Branches:
- master
- Children:
- e877e55
- Parents:
- 14ad9c9
- Location:
- misc
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
misc/gen-glob.py
r14ad9c9 r6aa430b 5 5 6 6 cross_gcc = "/opt/cross-m68k/bin/m68k-none-elf-gcc" 7 gen = c_generator.CGenerator()8 7 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" 8 class 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 29 typ_map = { 30 "void": None 16 31 } 17 32 … … 19 34 for path in f: 20 35 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 56 class 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 80 gen = c_generator.CGenerator() 81 82 with 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 21 88 22 89 if path == "ram/wdfield.c": # breaks pycparser … … 30 97 # ast.show() 31 98 99 incs = set() 32 100 funs = {} 33 101 vars = {} 34 incs = set()35 102 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 40 110 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 57 114 58 115 else: … … 63 120 continue 64 121 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 65 129 decl.storage = ["extern"] 66 130 decl.init = None 67 131 68 132 toks = gen.visit(decl).split(" ") 69 70 for tok in toks:71 if tok in inc_map:72 incs.add(inc_map[tok])73 74 133 alig = "" 75 134 … … 87 146 alig += toks[0] + ("\t\t" if len(toks[0]) < 8 else "\t") 88 147 toks = toks[1:] 89 90 map[decl.name] = alig + " ".join(toks) + ";"148 149 dest[decl.name] = alig + " ".join(toks) + ";" 91 150 92 151 file = path.split("/")[-1] … … 135 194 136 195 with open(path[:-2] + ".x", "w") as f: 137 print("\n".join(glob), file = f)196 f.write("\n".join(glob)) 138 197 139 198 print("") -
misc/proto.x
r14ad9c9 r6aa430b 100 100 extern void vwputs(int16_t *obase, int16_t nw, int16_t fg, int16_t bg, int16_t row, int16_t col, int8_t *str); 101 101 extern void xtrap15(void); 102
Note:
See TracChangeset
for help on using the changeset viewer.