def _build_case(functions): """ Builds the switch statement that will form the foundation of the RPC handler """ cases = [] for i, func in enumerate(functions.values()): case = c_ast.Case(c_ast.Constant('int', str(i)), [func.call_ast, c_ast.Break()]) cases.append(case)
def insert_node_after_continue(codeast, node): if type(codeast) == c_ast.Compound: continues = [ n for n in codeast.block_items if type(n) == c_ast.Continue ] if len(continues) > 0: items = copy.copy(codeast.block_items) for i in items: if type(i) == c_ast.Continue: codeast.block_items.remove(i) body = copy.deepcopy(node) for e in body: codeast.block_items.append(e) # uncomment this line to keep the old continue #codeast.block_items.append(i) codeast.block_items.append(c_ast.Break()) return False
def loop_peel(graph, list_deg, loop): """Peel the loop based on the maximum invariance degree""" peeling_deg = max_deg_of_list(list_deg) + 1 if peeling_deg != 0: # if loop can be peeled loop_node = loop.loop_node parent_index = loop.parent[1] # index in the AST of the original loop loop.parent[0].block_items.pop(parent_index) # remove the orignal loop from AST commands = init_commands(peeling_deg, loop_node.stmt.block_items) # initialize all the commands in the body of the loop peel = [] # list of AST nodes for each peel for i in range(peeling_deg): peel_body = [] # list of commands for current peel for (node, ind) in commands[i]: # if the command can be peeled and has reached its peeling degree if ind != -1 and list_deg[ind] == i + 1: (capture_tab, recover_tab) = captures(ind, loop_node.stmt.block_items, list_deg, graph) add_new_vars(commands, i, node, ind, peeling_deg, capture_tab, recover_tab) peel_body.append(node) # if its the last peel, add the body to a while statement with the same cond exp as the original while loop if i == peeling_deg - 1: peel.append(c_ast.While(loop_node.cond, c_ast.Compound(peel_body))) else: # if not last peel, add the body to an if statement with the same cond exp as the original while loop peel.append(c_ast.If(loop_node.cond, c_ast.Compound(peel_body), None)) # if the loop contains a break/contiue, wrap the entire peeled loop inside of a while loop with the same cond # exp as the original if check_break(loop_node.stmt): peel = c_ast.While(loop_node.cond, c_ast.Compound(peel + [c_ast.Break()]), None) loop.parent[0].block_items.insert(parent_index, peel) else: for i in peel: # add each peel to the modified AST loop.parent[0].block_items.insert(parent_index, i) parent_index = parent_index + 1 loop.isOpt = True