class ProgramDataVisitor(c_ast.NodeVisitor): # Function List functions = [] # Global variable list stateVariables = [] # Type definition list typeDefs = [] # Struct definition list structs = [] # Union definition list unions = [] # Enum definition list enums = [] # Generator to get C code from a node generator = c_generator.CGenerator() # When we hit a function definition, grab the function name, return type, and arguments. def visit_FuncDef(self, node): function = [] # Name function.append(node.decl.name) # Return type if type(node.decl.type.type) is c_ast.PtrDecl: rType = [] if type(node.decl.type.type.type.type) is c_ast.Enum: rType = ['*', 'enum'] rType.append(node.decl.type.type.type.type.name) elif type(node.decl.type.type.type.type) is c_ast.Struct: rType = ['*', 'struct'] rType.append(node.decl.type.type.type.type.name) elif type(node.decl.type.type.type.type) is c_ast.Union: rType = ['*', 'union'] rType.append(node.decl.type.type.type.type.name) else: rType = node.decl.type.type.type.type.names rType.insert(0, "*") function.append(rType) elif type(node.decl.type.type.type) is c_ast.Enum: rType = ['enum'] rType.append(node.decl.type.type.type.name) function.append(rType) elif type(node.decl.type.type.type) is c_ast.Struct: rType = ['struct'] rType.append(node.decl.type.type.type.name) function.append(rType) elif type(node.decl.type.type.type) is c_ast.Union: rType = ['union'] rType.append(node.decl.type.type.type.name) function.append(rType) else: function.append(node.decl.type.type.type.names) # Arguments args = self.generator.visit(node.decl.type.args).split(",") function.append(args) self.functions.append(function) # Decl nodes correspond to global variables and structs. def visit_Decl(self, node): # Get name, type, status (variable, array, pointer), and declared value (if any). # Make sure that it is not the obligations array. if node.name != "obligations" and node.name != "scoreEpsilon": var = [] var.append(node.name) # Handle based on type if type(node.type) is c_ast.TypeDecl: # Status var.append("var") # Type if type(node.type.type) is c_ast.Struct: var.append(["struct", node.type.type.name]) elif type(node.type.type) is c_ast.Union: var.append(["union", node.type.type.name]) elif type(node.type.type) is c_ast.Enum: var.append(["enum", node.type.type.name]) else: var.append(node.type.type.names) # Initial Value var.append(self.generator.visit(node.init)) self.stateVariables.append(var) elif type(node.type) is c_ast.ArrayDecl: var.append("array," + str(node.type.dim.value)) # Type var.append(node.type.type.type.names) # Initial Value var.append(self.generator.visit(node.init)) self.stateVariables.append(var) elif type(node.type) is c_ast.PtrDecl: # Status var.append("pointer") # Type nextLevel = node.type # Could have multiple levels of pointer, i.e., **int or ***int # We need to dig through until we find the type information while type(nextLevel) is c_ast.PtrDecl: nextLevel = nextLevel.type if type(nextLevel.type) is c_ast.Struct: var.append(["struct", nextLevel.type.name]) elif type(nextLevel.type) is c_ast.Union: var.append(["union", nextLevel.type.name]) elif type(nextLevel.type) is c_ast.Enum: var.append(["enum", nextLevel.type.name]) else: var.append(nextLevel.type.names) # Initial value var.append(self.generator.visit(node.init)) self.stateVariables.append(var) elif type(node.type) is c_ast.Struct: struct = [] struct.append(node.type.name) struct.append([]) for decl in node.type.decls: member = [] member.append(decl.name) member.append([]) if type(decl.type) is c_ast.TypeDecl: # Status member[1].append("var") # Type cType = [] if type(decl.type.type) is c_ast.Enum: cType = ['enum'] cType.append(decl.type.type.name) elif type(decl.type.type) is c_ast.Struct: cType = ['struct'] cType.append(decl.type.type.name) elif type(decl.type.type) is c_ast.Union: cType = ['union'] cType.append(decl.type.type.name) else: cType = decl.type.type.names member[1].append(cType) elif type(decl.type) is c_ast.ArrayDecl: member[1].append("array," + str(decl.type.dim.value)) # Type cType = [] if type(decl.type.type.type) is c_ast.Enum: cType = ['enum'] cType.append(decl.type.type.type.name) elif type(decl.type.type.type) is c_ast.Struct: cType = ['struct'] cType.append(decl.type.type.type.name) elif type(decl.type.type.type) is c_ast.Union: cType = ['union'] cType.append(decl.type.type.type.name) else: cType = decl.type.type.type.names member[1].append(cType) elif type(decl.type) is c_ast.PtrDecl: member[1].append("pointer") nextLevel = decl.type.type # Could have multiple levels of pointer, i.e., **int or ***int # We need to dig through until we find the type information while type(nextLevel) is c_ast.PtrDecl: nextLevel = nextLevel.type # Type cType = [] if type(nextLevel.type) is c_ast.Enum: cType = ['enum'] cType.append(nextLevel.type.name) elif type(nextLevel.type) is c_ast.Struct: cType = ['struct'] cType.append(nextLevel.type.name) elif type(nextLevel.type) is c_ast.Union: cType = ['union'] cType.append(nextLevel.type.name) else: cType = nextLevel.type.names member[1].append(cType) struct[1].append(member) self.structs.append(struct) # TypeDecl nodes indicate type definitions def visit_TypeDecl(self, node): # Get the name and type of all typedefs. if type(node.type) is c_ast.Enum: if node.type.name != None: self.typeDefs.append([node.declname, ["enum", node.type.name]]) else: self.typeDefs.append([node.declname, ["enum", node.declname]]) elif type(node.type) is c_ast.Struct: if node.type.name != None: self.typeDefs.append( [node.declname, ["struct", node.type.name]]) else: self.typeDefs.append( [node.declname, ["struct", node.declname]]) # If the structure definition is included, capture it if node.type.decls != None: struct = [] if node.type.name != None: struct.append(node.type.name) else: struct.append(node.declname) struct.append([]) for decl in node.type.decls: member = [] member.append(decl.name) member.append([]) if type(decl.type) is c_ast.TypeDecl: # Status member[1].append("var") # Type member[1].append(decl.type.type.names) elif type(decl.type) is c_ast.ArrayDecl: member[1].append("array," + str(decl.type.dim.value)) member[1].append(decl.type.type.type.names) elif type(decl.type) is c_ast.PtrDecl: member[1].append("pointer") member[1].append(decl.type.type.type.names) struct[1].append(member) self.structs.append(struct) elif type(node.type) is c_ast.Union: if node.type.name != None: self.typeDefs.append( [node.declname, ["union", node.type.name]]) else: self.typeDefs.append([node.declname, ["union", node.declname]]) # If the structure definition is included, capture it if node.type.decls != None: union = [] if node.type.name != None: union.append(node.type.name) else: union.append(node.declname) union.append([]) for decl in node.type.decls: member = [] member.append(decl.name) member.append([]) if type(decl.type) is c_ast.TypeDecl: # Status member[1].append("var") # Type member[1].append(decl.type.type.names) elif type(decl.type) is c_ast.ArrayDecl: member[1].append("array," + str(decl.type.dim.value)) member[1].append(decl.type.type.type.names) elif type(decl.type) is c_ast.PtrDecl: member[1].append("pointer") member[1].append(decl.type.type.type.names) union[1].append(member) self.unions.append(union) else: self.typeDefs.append([node.declname, node.type.names])
def generate_c(node): return c_generator.CGenerator().visit(node)
def translate_to_c(filename): ast = parse_file(filename, use_cpp=True) generator = c_generator.CGenerator() print(generator.visit(ast))
def genH(self): generator = c_generator.CGenerator() return generator.visit(self.hAST)
def _run_c_to_c(self, src): ast = parse_to_ast(src) generator = c_generator.CGenerator() return generator.visit(ast)
def __init__(self): super().__init__() self.external_functions = list() self.nondet_variables = dict() self.c_generator = c_generator.CGenerator() self.var_stack = [{'name': None, 'vars': dict()}]
def get_source_string(node): return c_generator.CGenerator().visit(node)
def ast_to_code(ast_node): """ ast_to_code(ast_node) -> str Восстанавливает код Си по узлу pycparser-AST в нормализованном форматировании. """ generator = c_generator.CGenerator() return generator.visit(ast_node)
def split_func(fd, ofile): prog = FlatProgram() has_nested = False (fd, nondet) = replace_nondet(fd) fd_body = fd.body for i in xrange(nondet): id = c_ast.ID('nondet_%d' % i) decl = c_ast.Decl( id.name, [], [], [], c_ast.TypeDecl(id.name, [], c_ast.IdentifierType(['word_t'])), None, None) fd_body.block_items.insert(0, decl) flatten(fd_body, prog) cgen = c_generator.CGenerator() (id_map, rev_id_map) = number_ids(fd) f = FileWriter(ofile, cgen) f.write('#include "synth.h"\n') f.write("/*\n") for id in sorted(rev_id_map.keys()): f.write(" * %s -> %d\n" % (rev_id_map[id], id)) f.write("*/\n\n") nids = len(id_map) prefix = copy.deepcopy(prog.blocks[0]) loop = copy.deepcopy(prog.blocks[1]) #(prefix, prefix_nondet) = replace_nondet(prefix) #(guard, guard_nondet) = replace_nondet(loop.cond) #(body, body_nondet) = replace_nondet(loop.stmt) decls = [] copy_out = [] prefix_block = [] retvis = ReturnVisitor() retvis.visit(prefix) for b in prefix.block_items: prefix_block.append(b) if isinstance(b, c_ast.Decl): id = b.name vid = id_map[id] b.init = c_ast.ArrayRef(c_ast.ID('in_vars'), c_ast.Constant('int', str(vid))) decls.append(b) if is_signed(b): extend = c_ast.FuncCall(c_ast.ID('SIGN_EXTEND'), c_ast.ExprList([c_ast.ID(id)])) decls.append(extend) prefix_block.append(extend) prefix.block_items = prefix_block for id in sorted(rev_id_map.keys()): varname = rev_id_map[id] arr = c_ast.ArrayRef(c_ast.ID('out_vars'), c_ast.Constant('int', str(id))) var = c_ast.ID(varname) copy_out.append(c_ast.Assignment("=", arr, var)) copy_out.append(c_ast.Return(c_ast.Constant('int', str('1')))) prefix.block_items += copy_out f.write("int prefix(word_t in_vars[NARGS], word_t out_vars[NARGS]) {\n") f.write(prefix) f.write("}\n\n") f.write("int guard(word_t in_vars[NARGS]) {\n") guard_body = c_ast.Compound(copy.copy(decls)) guard_body.block_items.append(c_ast.Return(loop.cond)) f.write(guard_body) ofile.write("}\n\n") if is_nested_loop(loop): has_nested = True nested_prog = FlatProgram() flatten(loop.stmt, nested_prog) inner_prefix = nested_prog.blocks[0] inner_body = nested_prog.blocks[1] inner_suffix = nested_prog.blocks[2] inner_guard = c_ast.Compound( copy.copy(decls) + [c_ast.Return(inner_body.cond)]) if isinstance(inner_body.stmt, c_ast.Compound): inner_body = inner_body.stmt.block_items else: inner_body = [inner_body.stmt] inner_body = c_ast.Compound(copy.copy(decls) + inner_body + copy_out) outer_guard = c_ast.Compound( copy.copy(decls) + [c_ast.Return(loop.cond)]) f.write( "int inner_prefix(word_t in_vars[NARGS], word_t out_vars[NARGS]) {\n" ) inner_prefix.block_items = decls + inner_prefix.block_items + copy_out f.write(inner_prefix) f.write("}\n\n") f.write("int inner_guard(word_t in_vars[NARGS]) {\n") f.write(inner_guard) f.write("}\n\n") f.write( "int inner_body(word_t in_vars[NARGS], word_t out_vars[NARGS]) {\n" ) f.write(inner_body) f.write("}\n\n") f.write( "int inner_suffix(word_t in_vars[NARGS], word_t out_vars[NARGS]) {\n" ) inner_suffix.block_items = decls + inner_suffix.block_items + copy_out f.write(inner_suffix) f.write("}\n\n") f.write("int outer_guard(word_t in_vars[NARGS]) {\n") f.write(outer_guard) f.write("}\n\n") else: f.write("int body(word_t in_vars[NARGS], word_t out_vars[NARGS]) {\n") loop_body = c_ast.Compound( copy.copy(decls) + loop.stmt.block_items + copy_out) f.write(loop_body) f.write("}\n\n") return (rev_id_map, has_nested, nondet)
def __init__(self): self.gen = c_generator.CGenerator() self.func = {}
def bracket(self): ast = parse_file(self.fname, use_cpp=True) generator = c_generator.CGenerator() text = generator.visit(ast) self.fohandle.write(text) self.fohandle.close()
def get_groups(ast): generator = c_generator.CGenerator() lines = generator.visit(ast).splitlines() seen = set() groups = {} vals = {x for x in lines if 'extern GrB_Info GxB' in x} - seen seen.update(vals) groups['GxB methods'] = sorted(vals) vals = {x for x in lines if 'extern GrB_Info GrB' in x} - seen seen.update(vals) groups['GrB methods'] = sorted(vals) vals = {x for x in lines if 'extern GrB_Info GB' in x} - seen seen.update(vals) groups['GB methods'] = sorted(vals) missing_methods = {x for x in lines if 'extern GrB_Info ' in x} - seen assert not missing_methods vals = {x for x in lines if 'extern GrB' in x} - seen seen.update(vals) groups['GrB objects'] = sorted(vals) vals = {x for x in lines if 'extern GxB' in x} - seen seen.update(vals) groups['GxB objects'] = sorted(vals) vals = {x for x in lines if 'extern const' in x and 'GxB' in x} - seen seen.update(vals) groups['GxB const'] = sorted(vals) vals = {x for x in lines if 'extern const' in x and 'GrB' in x} - seen seen.update(vals) groups['GrB const'] = sorted(vals) missing_const = {x for x in lines if 'extern const' in x} - seen assert not missing_const vals = { x for x in lines if 'typedef' in x and 'GxB' in x and '(' not in x } - seen seen.update(vals) groups['GxB typedef'] = sorted(vals) vals = { x for x in lines if 'typedef' in x and 'GrB' in x and '(' not in x } - seen seen.update(vals) groups['GrB typedef'] = sorted(vals) missing_typedefs = { x for x in lines if 'typedef' in x and 'GB' in x and '(' not in x } - seen assert not missing_typedefs assert all(x.endswith(';') for x in seen) # sanity check g = VisitEnumTypedef() _ = g.visit(ast) enums = g.results vals = {x for x in enums if '} GrB' in x} for val in vals: seen.update(val.splitlines()) groups['GrB typedef enums'] = sorted(vals, key=lambda x: x.rsplit('}', 1)[-1]) vals = {x for x in enums if '} GxB' in x} for val in vals: seen.update(val.splitlines()) groups['GxB typedef enums'] = sorted(vals, key=lambda x: x.rsplit('}', 1)[-1]) missing_enums = set(enums) - set(groups['GrB typedef enums']) - set( groups['GxB typedef enums']) assert not missing_enums vals = {x for x in lines if 'typedef' in x and 'GxB' in x} - seen seen.update(vals) groups['GxB typedef funcs'] = vals vals = {x for x in lines if 'typedef' in x and 'GrB' in x} - seen assert not vals groups['not seen'] = sorted(set(lines) - seen) return groups
def compute_rel(node): #FIXME faire les opérations unaires et constantes generator = c_generator.CGenerator() if (isinstance(node, c_ast.Assignment)): x = node.lvalue.name dblist = [[x], []] if (isinstance(node.rvalue, c_ast.BinaryOp)): #x=y+z if (not isinstance(node.rvalue.left, c_ast.Constant)): y = node.rvalue.left.name dblist[1].append(y) if (not isinstance(node.rvalue.right, c_ast.Constant)): z = node.rvalue.right.name dblist[1].append(z) listvar = list(set(dblist[0]) | set(dblist[1])) rest = Relation(listvar) rest.identity() rest.algebraic(dblist) if DEBUG_LEVEL >= 2: print("DEBUG_LEVEL: Computing Relation (first case)") node.show() rest.show() return rest if (isinstance(node.rvalue, c_ast.Constant)): #x=exp(…) TODO rest = Relation([x]) if DEBUG_LEVEL >= 2: print("DEBUG_LEVEL: Computing Relation (second case)") node.show() rest.show() return rest if (isinstance(node.rvalue, c_ast.UnaryOp)): #x=exp(…) TODO listVar = list_var(exp) rels = Relation([x] + listVar) rels.identity() rels.algebraic([[x], listVar]) if DEBUG_LEVEL >= 2: print("DEBUG: Computing Relation (third case)") node.show() rels.show() return rels if (isinstance(node, c_ast.If)): #if cond then … else … relT = Relation([]) for child in node.iftrue.block_items: relT = relT.composition(compute_rel(child)) relF = Relation([]) if (node.iffalse != None): for child in node.iffalse.block_items: relF = relF.composition(compute_rel(child)) rels = relF.sumRel(relT) rels = rels.conditionRel(list_var(node.cond)) if DEBUG_LEVEL >= 2: print("DEBUG_LEVEL: Computing Relation (conditional case)") node.show() rels.show() return rels if (isinstance(node, c_ast.While)): rels = Relation([]) for child in node.stmt.block_items: rels = rels.composition(compute_rel(child)) rels = rels.fixpoint() rels = rels.conditionRel(list_var(node.cond)) if DEBUG_LEVEL >= 2: print("DEBUG_LEVEL: Computing Relation (loop case)") node.show() rels.show() return rels return Relation([])
EPANOS_REG a6; EPANOS_REG a7; EPANOS_REG f0; EPANOS_REG f2; EPANOS_REG f12; EPANOS_REG f13; EPANOS_REG f14; EPANOS_REG f15; EPANOS_REG f16; EPANOS_REG f17; EPANOS_REG f18; EPANOS_REG f19; } EPANOS_ARGS; ''' gen_from_node = c_generator.CGenerator().visit flatten = chain.from_iterable def c_for_insn(ea, our_fns, extern_reg_map, stkvars): while True: (ea, c) = cpu_ida.ida_current_cpu().gen.fmt_insn(ea, our_fns, extern_reg_map, stkvars, from_delay=False) yield c if ea == ida.BADADDR: break
def instrument(self, instrument): ast = c_parser.CParser().parse(self._code) Kernel.Instrument().instrument(ast, self._name, instrument) return c_generator.CGenerator().visit(ast)
def ast_to_str(node: c_ast.Node): generator = c_generator.CGenerator() nodestr = generator.visit(node) return nodestr.replace('\n', '').strip()
def generate_code(sems, filename, line): generator = c_generator.CGenerator() parser = c_parser.CParser() src, fmt = preprocess(filename) ast = parser.parse(src) codes = [] for sem in sems: # error : if the first result has error, then exit if len(codes) == 1 and isinstance(codes[0], dict): if 'error' in codes[0]: break if 'request' not in sem or 'construct' not in sem or\ sem['request'] not in ['declare', 'add', 'include', 'edit']: if 'request' in sem and sem['request'] in [ 'navigate', 'systemCommand' ]: codes.append(sem) else: codes.append({ 'output': 'UnknownReqError' + '\n' + str(sem), 'error': 'Could not understand request.\ \nSem : ' + str(sem), 'id': 'UnknownReqError' }) continue # editing if sem['request'] == 'edit': rng = None if 'name' in sem: rng, found = find_range(ast.ext, sem['name']) if rng is None: error = { 'error': 'could not find name', 'output': 'NameNotFoundError', 'id': 'NameNotFoundError' } codes.append(error) else: if rng[0] == rng[1]: rng = (rng[0], ) sem['value'] = rng codes.append(sem) continue new_ext = deepcopy(ast.ext) new_fmt = deepcopy(fmt) coord, node = handle_req(new_ext, new_fmt, sem, line) if isinstance(node, dict): codes.append(node) continue op = node # op is string for include_package if not isinstance(op, str): op = generator.visit(node) # if the request threw error rp = generator.visit(FileAST(new_ext)) rp = postprocess(new_fmt, rp) codes.append({'output': op, 'replace': rp, 'cursor': coord.line}) return codes
def get_cdef(): generator = c_generator.CGenerator() return generator.visit(get_ast())
# v.visit(ast) for i in child: print type(i[1]) print type(child[1]), translate_to_c("1.c") print type(ast.ext) l = ast.ext for i in l: print type(i), isinstance(i, pycparser.c_ast.FuncDef) print "function", i.decl.name, get_source_string(i.decl) # print i.body.block_items for j in i.body.block_items: if (isinstance(j, pycparser.c_ast.While)): print "while" # for k in j.stmt.block_items: # print k print c_generator.CGenerator().visit(j) elif isinstance(j, pycparser.c_ast.If): print "If block" print type(j.iftrue) print isinstance(j.iftrue, pycparser.c_ast.EmptyStatement) # print c_generator.CGenerator().visit(j.iffalse) elif isinstance(j, pycparser.c_ast.FuncCall): print "function call" elif isinstance(j, pycparser.c_ast.FuncDef): print "function definition", j, get_source_string(j) elif isinstance(j, pycparser.c_ast.Decl): print "declaration" elif isinstance(j, pycparser.c_ast.Assignment): print "assignment" elif isinstance(j, pycparser.c_ast.Return): print "return", get_source_string(j)
from pycparser import c_generator, c_ast cgen = c_generator.CGenerator() FILESTORAGE = {} def closeFiles(): global FILESTORAGE FILESTORAGE = {} class FunctionWrapper(object): """ """ def __init__(self, ts3decl=None, pytsondecl=None): """ :param ts3decl: :param pytsondecl: """ if not ts3decl and not pytsondecl: raise Exception("No declaration given") self.ts3decl = ts3decl self.pytsondecl = pytsondecl self.bodylines = [] #if we generate from previous code, extract the body #to work, developers have to use correct indentation! (function-closing line must be "}" without any leading spaces)
def genC(self): generator = c_generator.CGenerator() return generator.visit(self.cAST)
def visit_FuncDecl(self, node): gen = c_generator.CGenerator() self.stubs.append(gen.visit(node))
def __ifblock_to_rust(dec): generator = c_generator.CGenerator() return generator.visit(dec.iftrue) + ';'
def processWitness(witness, benchmark, bitwidth): try: root = ElementTree.parse(witness).getroot() except: eprint("INVALID WITNESS FILE: failed to parse XML") sys.exit(1) # print(ElementTree.tostring(root)) ns = {'graphml': 'http://graphml.graphdrawing.org/xmlns'} graph = root.find('./graphml:graph', ns) if graph is None: eprint("INVALID WITNESS FILE: failed to parse XML - no graph node") sys.exit(1) entryFun = validateConfig(graph, ns, witness, benchmark, bitwidth) benchmarkString = '' with tempfile.NamedTemporaryFile() as fp: subprocess.check_call( ['gcc', '-x', 'c', '-E', benchmark, '-o', fp.name]) with open(fp.name, 'r') as b: needStructBody = False skipAsm = False inAttribute = False for line in b: # remove __attribute__ line = re.sub(r'__attribute__\s*\(\(\s*[a-z_, ]+\s*\)\)\s*', '', line) # line = re.sub(r'__attribute__\s*\(\(\s*[a-z_, ]+\s*\(\s*[a-zA-Z0-9_, "\.]+\s*\)\s*\)\)\s*', '', line) # line = re.sub(r'__attribute__\s*\(\(\s*[a-z_, ]+\s*\(\s*sizeof\s*\([a-z ]+\)\s*\)\s*\)\)\s*', '', line) # line = re.sub(r'__attribute__\s*\(\(\s*[a-z_, ]+\s*\(\s*\([0-9]+\)\s*<<\s*\([0-9]+\)\s*\)\s*\)\)\s*', '', line) line = re.sub(r'__attribute__\s*\(\(.*\)\)\s*', '', line) if re.search(r'__attribute__\s*\(\(', line): line = re.sub(r'__attribute__\s*\(\(.*', '', line) inAttribute = True elif inAttribute: line = re.sub(r'.*\)\)', '', line) inAttribute = False # rewrite some GCC extensions line = re.sub(r'__extension__', '', line) line = re.sub(r'__restrict', 'restrict', line) line = re.sub(r'__inline__', 'inline', line) line = re.sub(r'__inline', 'inline', line) line = re.sub(r'__const', 'const', line) line = re.sub(r'__signed__', 'signed', line) line = re.sub(r'__builtin_va_list', 'int', line) # a hack for some C-standards violating code in LDV benchmarks if needStructBody and re.match(r'^\s*}\s*;\s*$', line): line = 'int __dummy; ' + line needStructBody = False elif needStructBody: needStructBody = re.match(r'^\s*$', line) is not None elif re.match(r'^\s*struct\s+[a-zA-Z0-9_]+\s*{\s*$', line): needStructBody = True # remove inline asm if re.match( r'^\s*__asm__(\s+volatile)?\s*\("([^"]|\\")*"[^;]*$', line): skipAsm = True elif skipAsm and re.search(r'\)\s*;\s*$', line): line = '\n' skipAsm = False line = '\n' if (skipAsm or re.match( r'^\s*__asm__(\s+volatile)?\s*\("([^"]|\\")*"[^;]*\)\s*;\s*$', line)): line = '\n' # remove asm renaming line = re.sub(r'__asm__\s*\(""\s+"[a-zA-Z0-9_]+"\)', '', line) benchmarkString += line parser = c_parser.CParser() ast = parser.parse(benchmarkString, filename=benchmark) # ast.show(showcoord=True) inputs = {} nondets = {} entry = {} setupTypes(ast, entryFun, inputs, nondets, entry) assert entry watch = {} setupWatch(ast, watch) trace = {} entryNode = buildTrace(graph, ns, trace) values = [] n = entryNode missing_nondets = set(nondets) while trace[n].get('target') is not None: if trace[n].get('assumption') is not None: # assumptions may use = or == a = re.sub(r'==', '=', trace[n]['assumption']) a = re.sub(r'\\result', '__SV_COMP_result', a) # we may be missing typedefs used in type casts a = re.sub(r'\([a-zA-Z0-9_]+\s*\*\)', '(int*)', a) wrapped = 'void foo() { ' + a + '}' a_ast = parser.parse(wrapped).ext[0].body.block_items[0] if isinstance(a_ast, c_ast.Assignment): f = trace[n].get('assumption.scope') v = c_generator.CGenerator().visit(a_ast.rvalue) if (trace[n].get('startline') is not None and watch.get(int(trace[n]['startline'])) is not None): w = watch[int(trace[n]['startline'])] values.append([w, v]) if w in missing_nondets: missing_nondets.remove(w) elif (f is not None and isinstance(a_ast.lvalue, c_ast.ID) and inputs[f].get(a_ast.lvalue.name) is not None): values.append([f, a_ast.lvalue.name, v]) # else: # print(trace[n]['startline']) # a_ast.show() # else: # print(trace[n]['startline']) # a_ast.show() n = trace[n]['target'] if watch and not values: eprint('inputs: ') eprint(inputs) eprint('nondets: ') eprint(nondets) eprint('watch: ') eprint(watch) eprint("No input values found in witness file") sys.exit(1) print('IN:') print(' ENTRY {n}()@[file {f} line {l}]'.format(n=entryFun, f=benchmark, l=entry['line'])) for v in values: if len(v) == 3: info = inputs[v[0]][v[1]] print( ' {t} {n}@[file {f} line {l} function {fun}]={value}'.format( t=info['type'], n=v[1], f=benchmark, l=info['line'], fun=v[0], value=v[2])) else: info = nondets[v[0]] print(' {t}@[file {f} line {l}]={value}'.format(t=info['type'], f=benchmark, l=info['line'], value=v[1])) for n in missing_nondets: info = nondets[n] print(' {t}@[file {f} line {l}]=0'.format(t=info['type'], f=benchmark, l=info['line']))
def translate_to_c(filename): """ Simply use the c_generator module to emit a parsed AST. """ ast = parse_file(filename, use_cpp=True) generator = c_generator.CGenerator() print(generator.visit(ast))
#!/usr/bin/env python3 import sys from pycparser import parse_file, c_generator def usage(): sys.stderr.write('Usage:\n') sys.stderr.write(' build_header.py <odc.h> <odc_cffi.h>\n') if len(sys.argv) != 3: usage() sys.exit(-1) input_filename = sys.argv[1] output_filename = sys.argv[2] ast = parse_file(input_filename, use_cpp=True) with open(output_filename, 'w') as f: f.write(c_generator.CGenerator().visit(ast))
def generate_imgui(source_file, output_file): # Note that cpp is used. Provide a path to your own cpp or # make sure one exists in PATH. ast = parse_file(source_file) v = FuncCollector('ig') v.visit(ast) v = FuncCollector('ImGuiIO_', False, 'ImGuiIO') v.visit(ast) v = FuncCollector('ImDrawList_', False, 'ImDrawList') v.visit(ast) v = FuncCollector('ImFont_', False, 'ImDrawList') v.visit(ast) v = FuncCollector('ImFontAtlas_', False, 'ImFontAtlas') v.visit(ast) v = FuncCollector('ImGuiPayload_', False, 'ImGuiPayload') v.visit(ast) v = FuncCollector('ImGuiListClipper_', False, 'ImGuiListClipper') v.visit(ast) v = FuncCollector('ImGuiTextFilter_', False, 'ImGuiTextFilter') v.visit(ast) v = FuncCollector('ImGuiTextBuffer_', False, 'ImGuiTextBuffer') v.visit(ast) generator = c_generator.CGenerator() with open(output_file, 'wt') as file: file.write('typedef struct rizz_api_imgui\n{\n') for f in func_nodes: if ('comment' in f): file.write(' // ' + f['comment'] + '\n') node = f['node'] src_name = f['src_name'] line = generator.visit(node) name_idx = line.find(src_name) first_paran = line.find('(') # fix pointer star position first_star = line.find('*') if (first_star > 0 and line[first_star - 1] == ' ' and first_star < first_paran): line = line[:first_star - 1] + line[first_star:] if (first_star > 0 and (first_star + 1) == name_idx and first_star < first_paran): line = line[:first_star] + ' ' + line[first_star:] # first_space = -1 space_idx = line.find(' ') while space_idx != -1: if (space_idx == name_idx - 1): first_space = space_idx break space_idx = line.find(' ', space_idx + 1) if (first_space != -1 and first_paran != -1): final_line = ' %-14s %s%s)%s;\n' % (line[:first_space + 1], '(*', node.name, line[first_paran:]) file.write(final_line) file.write('} rizz_api_imgui;\n') file.write('\nstatic rizz_api_imgui the__imgui = {\n') for i, f in enumerate(func_nodes): node = f['node'] src_name = f['src_name'] line = ' .%s = %s' % (node.name, src_name) if i < len(func_nodes) - 1: line = line + ',\n' else: line = line + '\n' file.write(line) file.write('};\n') file.close()
def loadTester(self): self.ffi=cffi.FFI() generator = c_generator.CGenerator() self.ffi.cdef(generator.visit(self.ast.decl)+";") self.lib = self.ffi.verify(generator.visit(self.ast)) self.loaded=True
import functools import itertools from pycparser import c_ast from pycparser import c_generator from pycparser.plyparser import ParseError from copy import deepcopy from pynq.ps import ZU_ARCH, CPU_ARCH, ZYNQ_ARCH from .compile import preprocess from .streams import SimpleMBStream from .streams import InterruptMBStream from . import MicroblazeProgram # Use a global parser and generator _parser = pycparser.CParser() _generator = c_generator.CGenerator() if CPU_ARCH == ZYNQ_ARCH: PTR_OFFSET = "0x20000000" elif CPU_ARCH == ZU_ARCH: PTR_OFFSET = "0x80000000" else: PTR_OFFSET = "0x0" # First we define a series of classes to represent types # Each class is responsible for one particular type of C # types class PrimitiveWrapper: """ Wrapper for C primitives that can be represented by a single Struct string.
class DependencyMapVisitor(c_ast.NodeVisitor): # Dependency Map dependencyMap = [] # Generator to get C code from a node generator = c_generator.CGenerator() # List of functions functions = [] # List of state variables stateVariables = [] # Current function being examined currentFunction = [] # Variable status flag status = "use" def __init__(self, functions, stateVariables): self.functions = functions self.stateVariables = stateVariables # When we hit a function definition, grab the function code and look for defs and uses of state variables. def visit_FuncDef(self, node): # Discard anything seen before we started examining this function self.currentFunction = [] self.currentFunction.append(node.decl.name) # Add empty variable mapping self.currentFunction.append([]) # Crawl through function body for variables uses/defs self.visit(node.body) # Add to map self.dependencyMap.append(self.currentFunction) self.currentFunction = [] # When we hit an assignment, flip the flag to "def" instead of "use" def visit_Assignment(self, node): # Make sure this is an "=" assignment. "+=", etc count as uses for our purposes (initialization check) if node.op == "=": if "obligations" not in self.generator.visit(node.lvalue): # LHS is the variable being assigned self.status = "def" self.visit(node.lvalue) # Reset before moving on self.status = "use" self.visit(node.rvalue) else: self.visit(node.lvalue) self.visit(node.rvalue) # Do the same for unary operations that make assignments. # Note that these are both defs and uses #def visit_UnaryOp(self, node): # if "++" in node.op or "--" in node.op: # self.status="def" # self.visit(node.expr) # self.status="use" # We want to record function calls as well to indicate dependencies. # Function arguments can also use state variables def visit_FuncCall(self, node): if type(node.name) is c_ast.UnaryOp: self.currentFunction[1].append([node.name.expr.name, "function"]) else: self.currentFunction[1].append([node.name.name, "function"]) args = self.generator.visit(node.args) for var in self.stateVariables: if var[0] in args: self.currentFunction[1].append([var[0], "use"]) break # When we visit a variable reference, add it to the list, along with its status. def visit_ID(self, node): if node.name != "obligations" and node.name != "scoreEpsilon": found = 0 for var in self.stateVariables: if node.name == var[0]: found = 1 break if found == 1: self.currentFunction[1].append([node.name, self.status])