def visit_Return(self, node): align, _ = _DistanceGenerator(self._types).visit(node.expr) if align != '0': raise ReturnDistanceNotZero(node.coord, _code_generator.visit(node.expr), align) # insert assert(__SHADOWDP_v_epsilon <= epsilon); epsilon, *_ = self._parameters if self._set_epsilon and self._set_epsilon.isdigit(): epsilon_node = c_ast.Constant(type='float', value=self._set_epsilon) elif self._set_epsilon and not self._set_epsilon.isdigit(): epsilon_node = c_ast.ID(name=self._set_epsilon) else: epsilon_node = c_ast.ID(epsilon) if self._set_goal: assert_node = c_ast.FuncCall( c_ast.ID(self._func_map['assert']), args=c_ast.ExprList( [c_ast.BinaryOp('<=', c_ast.ID(name='__SHADOWDP_v_epsilon'), c_ast.BinaryOp(op='*', left=epsilon_node, right=convert_to_ast(self._set_goal)))])) else: assert_node = c_ast.FuncCall(c_ast.ID(self._func_map['assert']), args=c_ast.ExprList([c_ast.BinaryOp('<=', c_ast.ID('__SHADOWDP_v_epsilon'), epsilon_node)])) self._parents[node].block_items.insert(self._parents[node].block_items.index(node), assert_node) self._inserted.add(assert_node) # because we have inserted a statement before Return statement while iterating, it will be a forever loop # add the current node to the set to not visit this same node again self._inserted.add(node)
def check_value(ast_tree, ext_index, bock_item_index, var_name, func_name): Cast = c_ast.Cast( c_ast.Typename( None, [], c_ast.PtrDecl([], c_ast.TypeDecl(None, [], c_ast.IdentifierType(['void'])))), c_ast.ID(var_name)) func_call = c_ast.FuncCall( c_ast.ID('check_value'), c_ast.ExprList([ c_ast.UnaryOp('&', c_ast.ID('global_log')), Cast, c_ast.Constant('string', '"' + var_name + '"'), c_ast.Constant('string', '"' + func_name + '"'), c_ast.Constant('int', str(len(var_name))), c_ast.Constant('int', str(len(func_name))) ])) new_node = c_ast.If( c_ast.BinaryOp('==', func_call, c_ast.Constant('int', '0')), c_ast.Compound([ c_ast.FuncCall( c_ast.ID('printf'), c_ast.ExprList([ c_ast.Constant('string', '"%s\\n"'), c_ast.Constant('string', '"Attack Detected"') ])), c_ast.FuncCall(c_ast.ID('exit'), c_ast.ExprList([c_ast.Constant('int', '1')])) ]), None) return merge_ast_tree(ast_tree, ext_index, bock_item_index, new_node, "insert_before")
def __malloc(self, depth: int) -> c_ast.FuncCall: """ A helper function to generate the call of malloc function with proper arguments. Note that a constant of 2 is added to the number of allocated cells. This is meant to compensate minor errors in size estimation. Example: malloc((N + 2) * sizeof(int*)) :param depth: Which dimension of the array we want to allocate. Used to generate the argument of sizeof(). :return: c_ast.FuncCall """ size_expr = \ c_ast.BinaryOp( '+', self.sizes[depth], c_ast.Constant('int', '2') ) sizeof = \ c_ast.FuncCall( c_ast.ID('sizeof'), c_ast.ExprList([c_ast.ID(self.dtype.name + '*' * (len(self.sizes) - depth - 1))]) ) arg = c_ast.BinaryOp('*', size_expr, sizeof) return c_ast.FuncCall(c_ast.ID('malloc'), c_ast.ExprList([arg]))
def merge_libs_calls(self, node): start = -1 end = -1 argumented_client = None argumented_lib = None LibCV = LibCallHunter(self.lib_name) if isinstance(node, c_ast.If): if isinstance(node.iftrue, c_ast.Compound): checking_blocks = node.iftrue.block_items elif isinstance(node, c_ast.FuncDef): checking_blocks = node.body.block_items elif isinstance(node, c_ast.Compound): checking_blocks = node.block_items else: checking_blocks = [] new_leaf = set() mulitiple_call = False for index in range(len(checking_blocks)): LibCV.use_lib = False block = checking_blocks[index] LibCV.visit(block) if LibCV.use_lib: for lib in LibCV.lib_node: l_object = self.node_dict.get(lib, None) if l_object is None: l_object, _ = self.create_ClientContextNode( lib, lib, None, lib, None) self.node_dict[node] = l_object new_leaf.add(l_object) if (start == -1): start = index if (end < index): end = index if (len(LibCV.lib_node) > 1): mulitiple_call = True if (end > start) or (start == end and mulitiple_call): argumented_lib = c_ast.Compound( block_items=checking_blocks[start:end + 1]) argumented_client = copy.deepcopy(node) if isinstance(argumented_client, c_ast.If): if isinstance(argumented_client.iftrue, c_ast.Compound): blocks = argumented_client.iftrue.block_items argumented_client.iftrue.block_items = blocks[:start] + [ c_ast.FuncCall(name=c_ast.ID(name="CLEVER_DELETE"), args=None) ] + blocks[end + 1:] elif isinstance(argumented_client, c_ast.FuncDef): blocks = argumented_client.body.block_items argumented_client.body.block_items = blocks[:start] + [ c_ast.Compound(block_items=[ c_ast.FuncCall(name=c_ast.ID(name="CLEVER_DELETE"), args=None) ] + checking_blocks[start:end + 1]) ] + blocks[end + 1:] return start, end, argumented_lib, argumented_client, new_leaf
def visit_FuncCall(self, node): if isinstance(node, c_ast.FuncCall): if (node.name.name == "CLEVER_DELETE"): parent = self.parent_child.get(node, None) if parent is not None and isinstance(parent, c_ast.Compound): parent.block_items = [ c_ast.FuncCall(name=c_ast.ID(name="lib_old"), args=None), c_ast.FuncCall(name=c_ast.ID(name="lib_new"), args=None) ]
def generic_visit(self, node): if (node.coord): # Extract and check line number lineNum = str(node.coord)[str(node.coord).index(':')+1:] if (lineNum == self.faultyLine): if isinstance(node, c_ast.Assignment): # Replace with symbolic assignment return node elif isinstance(node, c_ast.If): # Add variable, make it symbolic return node elif isinstance(node, c_ast.Return): # Add variable, make it symbolic return node elif isinstance(node, c_ast.Decl): return node for c_name, c in node.children(): # recursively visit child nodes res = self.visit(c) if (res): if isinstance(node, c_ast.FuncDef) or isinstance(node, c_ast.Compound) or isinstance(node, c_ast.FileAST): if isinstance(node, c_ast.FuncDef): items = node.body.block_items elif isinstance(node, c_ast.Compound): items = node.block_items else: items = node.ext for i, n in enumerate(items): if res == n: if isinstance(res, c_ast.Assignment): newNode = c_ast.FuncCall(c_ast.ID('klee_make_symbolic'), c_ast.ExprList([c_ast.ID('&'+res.lvalue.name), c_ast.FuncCall(c_ast.ID('sizeof'), c_ast.ExprList([res.lvalue])), c_ast.Constant('str', '"'+res.lvalue.name+'"')])) items.insert(i+1, newNode) return elif isinstance(res, c_ast.If): newAssign = c_ast.Assignment('=', c_ast.ID('kleeVar'), res.cond) newNode = c_ast.FuncCall(c_ast.ID('klee_make_symbolic'), c_ast.ExprList([c_ast.ID('&kleeVar'), c_ast.FuncCall(c_ast.ID('sizeof'), c_ast.ExprList([c_ast.ID('kleeVar')])), c_ast.Constant('str', '"kleeVar"')])) n.cond = c_ast.ID('kleeVar') items.insert(i, newAssign) items.insert(i+1, newNode) return elif isinstance(res, c_ast.Return): newAssign = c_ast.Assignment('=', c_ast.ID('kleeVar'), res.expr) newNode = c_ast.FuncCall(c_ast.ID('klee_make_symbolic'), c_ast.ExprList([c_ast.ID('&kleeVar'), c_ast.FuncCall(c_ast.ID('sizeof'), c_ast.ExprList([c_ast.ID('kleeVar')])), c_ast.Constant('str', '"kleeVar"')])) n.expr = c_ast.ID('kleeVar') items.insert(i, newAssign) items.insert(i+1, newNode) return elif isinstance(res, c_ast.Decl): newNode = c_ast.FuncCall(c_ast.ID('klee_make_symbolic'), c_ast.ExprList([c_ast.ID('&'+res.name), c_ast.FuncCall(c_ast.ID('sizeof'), c_ast.ExprList([c_ast.ID(res.name)])), c_ast.Constant('str', '"'+res.name+'"')])) items.insert(i+1, newNode) return else: return res
def get_sizeof_pointer_to_type(t, reference_ast): if not t.get_typename(): return c_ast.FuncCall(c_ast.ID("fffc_estimate_allocation_size"), c_ast.ID("storage")) if t.get_typename(): argument_ast = t.get_reference()("storage") else: argument_ast = t.define("storage") prefix = "fffc_get_sizeof_" desired_name = CGenerator().visit(argument_ast) suffix = encode_hash(desired_name) function_name = prefix + suffix call = c_ast.FuncCall(c_ast.ID(function_name), reference_ast) return call
def work(self, new_stuffs): for parent, child in self.target_location: if isinstance(child, c_ast.Return): new_stuffs_copy = new_stuffs[:-1] + [ c_ast.FuncCall( name=c_ast.ID(name="_RETTYPE"), args=c_ast.ExprList(exprs=[ child.expr, c_ast.Constant(type='int', value=str(len(new_stuffs) - 1)) ])) ] + new_stuffs[-1:] else: new_stuffs_copy = new_stuffs if isinstance(parent, c_ast.Compound): child_loc = parent.block_items.index(child) parent.block_items = parent.block_items[: child_loc] + new_stuffs_copy + parent.block_items[ child_loc + 1:] elif isinstance(parent, c_ast.If): if parent.iftrue == child: parent.iftrue = c_ast.Compound(block_items=new_stuffs_copy) elif parent.iffalse == child: parent.iffalse = c_ast.Compound( block_items=new_stuffs_copy) elif isinstance(parent, c_ast.While): parent.stmt = c_ast.Compound(block_items=new_stuffs_copy) elif isinstance(parent, c_ast.For): parent.stmt = c_ast.Compound(block_items=new_stuffs_copy) elif isinstance(parent, c_ast.DoWhile): parent.stmt = c_ast.Compound(block_items=new_stuffs_copy) elif isinstance(parent, c_ast.Label): parent.stmt = c_ast.Compound(block_items=new_stuffs_copy)
def p_postfix_expression_gnu_bioo(self, p): """ primary_expression : __BUILTIN_OFFSETOF \ LPAREN type_name COMMA offsetof_member_designator RPAREN """ coord = self._coord(p.lineno(1)) p[0] = c_ast.FuncCall(c_ast.ID(p[1], coord), c_ast.ExprList([p[3], p[5]], coord), coord)
def createMain(self): #Main Function #Declaration z1 = c_ast.TypeDecl('args',[],c_ast.IdentifierType(['int'])) args = c_ast.Decl('args',[],[],[],z1,None,None) z2= c_ast.PtrDecl([],c_ast.TypeDecl('argv',[],c_ast.IdentifierType(['char']))) z3=c_ast.ArrayDecl(z2,None,[]) argv = c_ast.Decl('argv',[],[],[],z3,None,None) params=c_ast.ParamList([args,argv]) mainDec=c_ast.FuncDecl(params,c_ast.TypeDecl('main',[],c_ast.IdentifierType(['int']))) # insertTest(functionName,varVals,varTypes) #Body ##Signal sigalrm=c_ast.ID(name="14") funcCast=c_ast.TypeDecl(declname = None,quals=[],type=c_ast.IdentifierType(['void'])) paramCast=c_ast.ParamList([c_ast.Typename(name=None,quals=[],type=c_ast.TypeDecl(declname = None,quals=[],type=c_ast.IdentifierType(['int'])))]) typeFunc=c_ast.PtrDecl(type=c_ast.FuncDecl(paramCast,funcCast),quals=[]) kchild=c_ast.Cast(to_type=c_ast.Typename(name=None, quals=[],type=typeFunc),expr=c_ast.ID(name="kill_child")) expressList = [sigalrm,kchild] signalStmt=c_ast.FuncCall(c_ast.ID(name="signal"),c_ast.ExprList(expressList)) ##Return returnStmt=c_ast.Return(c_ast.Constant(type="int",value="0")) comp=c_ast.Compound([signalStmt,returnStmt]) return c_ast.FuncDef(mainDec,None,comp)
def insertTest(self, block, functionName, varVals, varTypes): expressList = list( c_ast.Constant(type=varTypes[i], value=str(varVals[i])) for i in range(len(varVals))) funcCall = c_ast.FuncCall(c_ast.ID(name=functionName), c_ast.ExprList(expressList)) block.body.block_items.insert(0, funcCall)
def __init__(self, linenum): self.linenum = linenum self.path = [] self.reached = False expressList = [c_ast.Constant(type='int', value='0')] self.funcCall = c_ast.FuncCall(c_ast.ID(name="assert"), c_ast.ExprList(expressList))
def _build_handle_function(functions): """ Wraps the switch statement in a function definition """ case_statement = _build_case(functions) available_check = c_ast.If( c_ast.BinaryOp( '<', c_ast.FuncCall( c_ast.ID('mailbox_available'), c_ast.ExprList([c_ast.Constant('int', '2')]) ), c_ast.Constant('int', '4') ), c_ast.Return(None), None ) handle_decl = c_ast.FuncDecl( None, c_ast.TypeDecl('_handle_events', [], c_ast.IdentifierType(['void'])), ) command_decl = c_ast.Decl('command', [], [], [], c_ast.TypeDecl('command', [], c_ast.IdentifierType(['int'])), [], []) command_read = _generate_read('command') body = c_ast.Compound([available_check, command_decl, command_read, case_statement]) return c_ast.FuncDef(handle_decl, [], body)
def astVeriNon(): veryidt = c_ast.IdentifierType(['int']) verytype = c_ast.TypeDecl('rand', [], veryidt) bifunc = c_ast.FuncCall(c_ast.ID('__VERIFIER_nondet'), None) verybina = c_ast.BinaryOp('%', bifunc, c_ast.ID('N')) simdecl = c_ast.Decl('rand', [], [], [], verytype, verybina, None, None) return simdecl
def make_klee_symbolic(variable_name, trackingName): arg1 = c_ast.UnaryOp(op='&', expr=c_ast.ID(variable_name)) arg2 = c_ast.UnaryOp(op='sizeof', expr = c_ast.Typename(name=None, quals =[], type= c_ast.TypeDecl(declname=None,quals=[], type=c_ast.IdentifierType(names=['int'])))) arg3 = c_ast.Constant(type='string', value='\"'+ trackingName +'\"') return c_ast.FuncCall(name=c_ast.ID(name = "klee_make_symbolic"), args=c_ast.ExprList(exprs=[arg1,arg2,arg3]))
def astScheDefer(id): schedef = c_ast.FuncCall(c_ast.ID('scheduler_deferral'), c_ast.Constant('int', id)) # astseq.ext[166].body.block_items[6].args = c_ast.Constant('int', id) # schedef=astseq.ext[166].body.block_items[6] # print ("\033[1;5;32;45;1mFuncCall scheduler_deferral\033[0m") # schedef.show() return schedef
def visit_Assignment(self, node): if isinstance(node.rvalue, c_ast.ArrayRef) and self.get_array_name( node.rvalue) + '_get' in self.registry.function_templates: if self.debug: print "Converting array reference %s" % self.get_array_name( node.rvalue) self.remove_node('Compound') decl = c_ast.FuncCall( c_ast.ID(self.get_array_name(node.rvalue) + '_get'), c_ast.ExprList([node.rvalue.subscript, node.lvalue])) self.insert_node_after(decl, 'Compound') elif isinstance(node.rvalue, c_ast.ArrayRef) and self.get_array_name( node.rvalue) in self.registry.function_templates: if self.debug: print "Converting array reference %s" % self.get_array_name( node.rvalue) self.remove_node('Compound') decl = c_ast.FuncCall( c_ast.ID(self.get_array_name(node.rvalue)), c_ast.ExprList([node.rvalue.subscript, node.lvalue])) self.insert_node_after(decl, 'Compound') elif isinstance(node.lvalue, c_ast.ArrayRef) and self.get_array_name( node.lvalue) + '_set' in self.registry.function_templates: if self.debug: print "Converting array assignment %s" % self.get_array_name( node.lvalue) self.remove_node('Compound') template = self.registry.function_templates[ self.get_array_name(node.lvalue) + '_set'] dummy_name = 'dummy_%d' % self.id self.id += 1 decl = c_ast.Decl( '', [], [], [], c_ast.TypeDecl( dummy_name, [], c_ast.IdentifierType(template.array_type.split())), None, None) self.insert_node_after(decl, 'Compound') decl = c_ast.FuncCall( c_ast.ID(self.get_array_name(node.lvalue) + '_set'), c_ast.ExprList([ node.lvalue.subscript, c_ast.Constant('int', '1'), node.rvalue, c_ast.ID(dummy_name) ])) self.insert_node_after(decl, 'Compound')
def insertTest(self,block,functionName,varVals,varTypes,timer,preOut,testId): #Fork call cFork=c_ast.Assignment( lvalue=c_ast.ID(name='child_pid'), op='=', rvalue=c_ast.FuncCall( c_ast.ID(name='fork'), args=None)) #Child ##stdout = freopen("out.txt", "w", stdout); if self.functype=='int': printLeft=c_ast.Constant(type="char",value='"'+"%d" +'"') elif self.functype=='float' or self.functype=='double': printLeft=c_ast.Constant(type="char",value='"'+"%f" +'"') else: printLeft=c_ast.Constant(type="char",value='"'+"%d" +'"') fileOut=c_ast.Constant(type="char",value='"'+preOut +'.' +testId+".out.txt" +'"') fileW=c_ast.Constant(type="char",value='"'+"w" +'"') fileStdout=c_ast.ID(name='stdout') expressList = [fileOut,fileW,fileStdout] freopenC = c_ast.FuncCall(c_ast.ID(name="freopen"),c_ast.ExprList(expressList)) outReassign=c_ast.Assignment( lvalue=fileStdout, op='=', rvalue=freopenC) ##mainfake() expressList = list(c_ast.Constant(type=varTypes[i],value=str(varVals[i])) for i in range(len(varVals))) funcCall = c_ast.FuncCall(c_ast.ID(name=functionName),c_ast.ExprList(expressList)) expressList = [printLeft,funcCall] funcCall = c_ast.FuncCall(c_ast.ID(name="printf"),c_ast.ExprList(expressList)) ##exit(0) funcCall2 = c_ast.FuncCall(c_ast.ID(name="exit"),c_ast.ExprList([c_ast.Constant(type='int',value='0')])) #Parent cAlarm=c_ast.FuncCall( c_ast.ID(name='alarm'),c_ast.ExprList([c_ast.Constant(type='int',value=str(timer))])) cWait=c_ast.FuncCall( c_ast.ID(name='wait'),c_ast.ExprList([c_ast.ID(name='0')])) #IFs if_false= c_ast.If( c_ast.BinaryOp(left=c_ast.ID(name='child_pid'),op='==',right= c_ast.Constant(type='int',value='0')), c_ast.Compound([outReassign,funcCall,funcCall2]),iffalse=None) if_ini=c_ast.If( c_ast.BinaryOp(left=c_ast.ID(name='child_pid'),op='>',right= c_ast.Constant(type='int',value='0')), c_ast.Compound([cAlarm,cWait]),iffalse=if_false) block.body.block_items.insert(1,if_ini) block.body.block_items.insert(1,cFork)
def makeScopeOutLog(coord): (file, line) = getLocation(coord) return c_ast.FuncCall( c_ast.ID('fprintf'), c_ast.ExprList([ c_ast.ID('stderr'), c_ast.Constant('string', '"{0}:{1}:scope_out\\n"'.format(file, line)) ]))
def astDequeue(queue): #the type of exprs is String exdeq = [c_ast.ID(queue)] exprs = c_ast.ExprList(exdeq) dequeue = c_ast.FuncCall(c_ast.ID('Dequeue'), exprs) # astseq.ext[167].body.block_items[2].args.exprs[0] = c_ast.ID(queue) # dequeue=astseq.ext[167].body.block_items[2] # print ("\033[1;5;32;45;1mFuncCall Dequeue\033[0m") # dequeue.show() return dequeue
def offsetof(self, struct_name, name): return c_ast.FuncCall( c_ast.ID('offsetof'), c_ast.ExprList([ c_ast.Typename( None, [], c_ast.TypeDecl(None, [], c_ast.Struct(struct_name, None))), c_ast.ID(name) ]))
def set_outdated(ast_tree, ext_index, bock_item_index, func_name): new_node = c_ast.FuncCall( c_ast.ID('set_outdated'), c_ast.ExprList([ c_ast.UnaryOp('&', c_ast.ID('global_log')), c_ast.Constant('string', '"' + func_name + '"'), c_ast.Constant('int', str(len(func_name))) ])) return merge_ast_tree(ast_tree, ext_index, bock_item_index, new_node, "insert_before")
def make_call(callee, ret_reg=None, args=None, for_extern=None): ret = c_ast.FuncCall(c_ast.ID(callee), args) if ret_reg is None: return ret else: if for_extern is not None: # cast the return value of an extern function to our register's type return do_assign(rt=ret_reg, op=simple_cast(for_extern.type, ret)) else: return do_assign(rt=ret_reg, op=ret)
def astEnqueue(elemtype, queue): #the type of exprs is String exenq = [c_ast.Constant('int', elemtype), c_ast.ID(queue)] exprs = c_ast.ExprList(exenq) enqueue = c_ast.FuncCall(c_ast.ID('Enqueue'), exprs) # astseq.ext[166].body.block_items[5].args.exprs[0] = c_ast.Constant('int', elemtype) # astseq.ext[166].body.block_items[5].args.exprs[1] = c_ast.ID(queue) # enqueue=astseq.ext[166].body.block_items[5] # print ("\033[1;5;32;45;1mFuncCall Enqueue\033[0m") # enqueue.show() return enqueue
def makeFuncCallLog(func): return c_ast.FuncCall( c_ast.ID('fprintf'), c_ast.ExprList([ c_ast.ID('stderr'), c_ast.Constant( 'string', '"{0}:{1}:call({2})\\n"'.format(func['file'], func['line'], func['name'])) ]))
def max_set_recur(exprs): if len(exprs) == 0: return None if len(exprs) == 1: return exprs[0] else: half = int(len(exprs) / 2) a = max_set_recur(exprs[:half]) b = max_set_recur(exprs[half:]) return c_ast.FuncCall(c_ast.ID('MAX'), c_ast.ExprList([a, b]))
def new_fcall(fname, args): fname = c_ast.ID(fname) args_list = [] for arg in args: if isinstance(arg, str): args_list.append(c_ast.ID(arg)) else: args_list.append(arg) args = c_ast.ExprList(args_list) return c_ast.FuncCall(fname, args)
def createOutputIf(self, expr): expressList = [c_ast.Constant(type='int', value='0')] funcCall = c_ast.FuncCall(c_ast.ID(name="assert"), c_ast.ExprList(expressList)) if_all = c_ast.If(c_ast.BinaryOp(left=c_ast.ID(name='__out_var'), op='==', right=expr), c_ast.Compound([funcCall]), iffalse=None) return (if_all)
def addTestFunction(ast, expectedOutput, testFxn, initVars, initList): varList = [] exprList = [] fxnName = '' inFxn = False listi = 0 for i in range(len(initVars)): v = initVars[i] if inFxn: exprList.append(c_ast.Constant('int', initList[listi])) listi += 1 if (')' in v): inFxn = False newVar = c_ast.FuncCall(c_ast.ID(fxnName), c_ast.ExprList(exprList)) varList.append(newVar) exprList = [] else: if ('(' in v): fxnName = v[:v.index('(')] if (v[v.index('(')+1] != ')'): inFxn = True exprList.append(c_ast.Constant('int', initList[listi])) listi += 1 else: newVar = c_ast.FuncCall(c_ast.ID(fxnName), c_ast.ExprList([])) varList.append(newVar) else: newVar = c_ast.Assignment('=', c_ast.ID(v), c_ast.Constant('int', initList[listi])) listi += 1 varList.append(newVar) fxnDecl = c_ast.FuncDecl(None, c_ast.TypeDecl('klee_test_entry', [], c_ast.IdentifierType(['void']))) fxnCall = c_ast.FuncCall(c_ast.ID(testFxn), c_ast.ExprList([])) binaryOp = c_ast.BinaryOp('==', fxnCall, c_ast.Constant('int', expectedOutput)) ifFalse = c_ast.Compound([c_ast.FuncCall(c_ast.ID('klee_silent_exit'), c_ast.ExprList([c_ast.Constant('int', '0')]))]) ifTrue = c_ast.Compound([]) blockItems = [] for v in varList: blockItems.append(v) blockItems.append(c_ast.If(binaryOp, ifTrue, ifFalse)) fxnBody = c_ast.Compound(blockItems) fxnNode = c_ast.FuncDef(fxnDecl, None, fxnBody) ast.ext.append(fxnNode)
def papi_instr() -> Tuple[List[c_ast.Node], List[c_ast.Node]]: """ :return: c_ast nodes representing the following code: exec(PAPI_start(set)); *begin = clock(); ... *end = clock(); exec(PAPI_stop(set, values)); """ papi_start = c_ast.FuncCall(c_ast.ID('PAPI_start'), c_ast.ParamList([c_ast.ID('set')])) papi_stop = c_ast.FuncCall(c_ast.ID('PAPI_stop'), c_ast.ParamList([c_ast.ID('set'), c_ast.ID('values')])) exec_start = c_ast.FuncCall(c_ast.ID('exec'), c_ast.ParamList([papi_start])) exec_stop = c_ast.FuncCall(c_ast.ID('exec'), c_ast.ParamList([papi_stop])) clock = c_ast.FuncCall(c_ast.ID('clock'), c_ast.ParamList([])) begin_clock = c_ast.Assignment('=', c_ast.UnaryOp('*', c_ast.ID('begin')), clock) end_clock = c_ast.Assignment('=', c_ast.UnaryOp('*', c_ast.ID('end')), clock) return [exec_start, begin_clock], [end_clock, exec_stop]