def convert_function_handler(func, func_dict, func_count, banres, file_name): func_for_loop_var_list = find_all_for_variable_list(func, func_dict) func_while_loop_var_list = find_all_while_variable_list(func, func_dict) #print(func_for_loop_var_list) #print(func_while_loop_var_list) func_for_loop_line_number_arr = find_func_for_loop_line_number_arr(func) func_while_loop_line_number_arr = find_func_while_loop_line_number_arr( func) ##print(func_for_loop_line_number_arr) func_for_loop_line_number_arr = func_for_loop_line_number_arr[:-1] func_for_loop_line_number_arr = set(func_for_loop_line_number_arr) func_for_loop_line_number_arr = list(func_for_loop_line_number_arr) current_for_loop_number = 0 current_while_loop_number = 0 loop_count = 0 for stmt in func.body.block_items: stmt_dict = ast_utilities.to_dict(stmt) if stmt_dict["_nodetype"] == "For": loop_count += 1 for_loop_handle(file_name, banres, func_for_loop_line_number_arr, func_count) if stmt_dict["_nodetype"] == "While" or stmt_dict[ "_nodetype"] == "DoWhile": loop_count += 1 while_loop_handle(file_name, banres, func_while_loop_line_number_arr, func_count) return func_for_loop_var_list, func_while_loop_var_list, func_for_loop_line_number_arr, func_while_loop_line_number_arr
def convert(file_name): banres, forstmt = dependency_test.test_dependency(file_name) dependency_test.comment_pre_processor(file_name) ##print(banres) ##print(forstmt) ast = parse_file(file_name, use_cpp=True) ##print(ast) dependency_test.remove_pre_processor(file_name, file_name) func_count = 0 all_for_list = [] all_while_list = [] for func in ast.ext: func_dict = ast_utilities.to_dict(func) ##print(func_dict) if "body" in func_dict and "block_items" in func_dict[ "body"] and func_dict["_nodetype"] == "FuncDef": func_count += 1 func_for_loop_var_list, func_while_loop_var_list, func_for_loop_line_number_arr, func_while_loop_line_number_arr = convert_function_handler( func, func_dict, func_count, banres, file_name) all_for_list.append(func_for_loop_var_list) #print(all_for_list) all_while_list.append(func_while_loop_var_list) #data data_directive('mid.c', all_for_list, all_while_list) return 0
def find_func_line_number_arr(ast): func_line_number_arr = [] for func in ast.ext: func_dict = ast_utilities.to_dict(func) if "body" in func_dict and "block_items" in func_dict[ "body"] and func_dict["_nodetype"] == "FuncDef": func_line_number_arr.append(int(str(func.coord).split(":")[1])) return func_line_number_arr
def find_func_for_loop_line_number_arr(func): func_for_loop_line_number_arr = [] flag = 0 for stmt in func.body.block_items: stmt_dict = ast_utilities.to_dict(stmt) if flag == 1: func_for_loop_line_number_arr.append( int(str(stmt.coord).split(":")[1])) flag = 0 if stmt_dict["_nodetype"] == "For": func_for_loop_line_number_arr.append( int(str(stmt.coord).split(":")[1])) flag = 1 return func_for_loop_line_number_arr
def test_dependency(file_name): comment_pre_processor(file_name) ast = parse_file(file_name, use_cpp=True) #print(ast) # print(type(ast)) remove_pre_processor(file_name, file_name) banres = [] forstmt = [] # print(ast) func_count = 0 for func in ast.ext: func_dict = ast_utilities.to_dict(func) #print(func_dict) if "body" in func_dict and "block_items" in func_dict["body"] and func_dict["_nodetype"] == "FuncDef": banres.append([]) forstmt.append([]) func_count += 1 for stmt in func.body.block_items: stmt_dict = ast_utilities.to_dict(stmt) if stmt_dict["_nodetype"] == "For" or stmt_dict["_nodetype"] == "While" or stmt_dict["_nodetype"] == "DoWhile": banres[func_count -1].append(isdependent(stmt) ) forstmt[func_count -1].append(gen.visit(stmt)) return banres,forstmt
def isdependent(ast_stmt): # dic = ast_utilities.to_dict(ast_stmt) # val = dic['stmt']["block_items"] val1 = ast_stmt.stmt.block_items lvar,rvar = [],[] # print(val1[0].lvalue) for ast_key in val1: dic = ast_utilities.to_dict(ast_key) if dic["_nodetype"] == "Assignment": getleftvariable(dic["lvalue"],ast_key.lvalue,lvar) getrightvariable(dic["rvalue"],ast_key.rvalue,rvar) elif dic["_nodetype"] == "FuncCall" and ( dic["name"]["name"] == "printf" or dic["name"]["name"] == "scanf"): return False #print('lvar') #print(lvar) #print('rvar') #print(rvar) for leftitem in lvar: for rightitem in rvar: if leftitem[0] == rightitem[0] and leftitem[1] != rightitem[1]: return False return True
def data_directive(file_name, all_for_list, all_while_list): for_arr = [] while_arr = [] for i in range(len(all_for_list)): for_arr.append(all_for_list[i][2]) #print(for_arr) print(all_while_list) while_arr = [] for i in range(len(all_while_list)): while_arr.append(all_while_list[i][2]) print(while_arr) banres, forstmt = dependency_test.test_dependency(file_name) #print(banres) #print(forstmt) f = open(file_name, 'r') stmt = "#pragma acc data" lines = f.readlines() f2 = open('final.c', 'w+') dependency_test.comment_pre_processor(file_name) ast = parse_file(file_name, use_cpp=True) dependency_test.remove_pre_processor(file_name, file_name) func_line_number_arr = find_func_line_number_arr(ast) final_list = [] for func in ast.ext: list1 = [] func_dict = ast_utilities.to_dict(func) if "body" in func_dict and "block_items" in func_dict[ "body"] and func_dict["_nodetype"] == "FuncDef": for stmt_line in func.body.block_items: stmt_dict = ast_utilities.to_dict(stmt_line) #print(stmt_dict) if stmt_dict["_nodetype"] == "For" or stmt_dict[ "_nodetype"] == "While" or stmt_dict[ "_nodetype"] == "DoWhile": #print(len(stmt_dict['stmt']['block_items'])) for i in range(len(stmt_dict['stmt']['block_items'])): #print(stmt_dict['stmt']['block_items'][0]) d = stmt_dict['stmt']['block_items'][0] if 'rvalue' in d.keys(): d1 = d['rvalue'] #print(d1) if d1['_nodetype'] == 'BinaryOp': if d1['right']['_nodetype'] == 'ArrayRef': #print(stmt_line) list1.append('True') var1 = stmt_line #print(var1) else: list1.append('False') break elif d1['_nodetype'] == 'ArrayRef': #print(stmt_line) list1.append('True') var1 = stmt_line #print(var1) else: list1.append('False') break else: continue final_list.append(list1) print(final_list) if len(func_line_number_arr) > 1: for j in range(len(final_list)): if len(final_list[j]) != 0 and ('True') in final_list[j]: #print(len(final_list[j])) pragma_no = 0 for i in range(len(final_list[j])): if final_list[j][i] == 'True': pragma_no = i else: continue """ #print(pragma_no) k=0 t=0 for i in range(len(lines)): #print(k) if lines[i].startswith('#pragma') and k==pragma_no: t=1 f2.write(stmt+'\n') f2.write('{'+'\n') f2.write(lines[i]) continue elif lines[i].startswith('#pragma') and k != pragma_no: k+=1 f2.write(lines[i]) if t==1: f2.write('}'+'\n') """ n_list = [] #print(func_line_number_arr) for i in range(len(func_line_number_arr) - 1): n_list.append([ int(func_line_number_arr[i]), int(func_line_number_arr[i + 1]) ]) #print(n_list) flag = 0 #print(n_list[len(n_list)-1][1]-2) for i in range(len(lines)): if i < (n_list[0][0] - 1): f2.write(lines[i]) elif i > (n_list[len(n_list) - 1][1] - 2): f2.write(lines[i]) elif i > (n_list[0][0] - 1) and i < (n_list[len(n_list) - 1][1] - 2) and flag == 0: flag = 1 for ele in n_list: t, k = 0, 0 flag = 1 for i in range(ele[0] - 1, ele[1] - 1): if lines[i].startswith( "#pragma") and t == 0 and k == pragma_no: t = 1 f2.write(stmt + '\n') f2.write('{' + '\n') f2.write(lines[i]) continue elif lines[i].startswith('#pragma') and k != pragma_no: k += 1 f2.write(lines[i]) if t == 1: f2.write('}' + '\n') """ n_list=[] #print(func_line_number_arr) for i in range(len(func_line_number_arr)-1): n_list.append([int(func_line_number_arr[i]),int(func_line_number_arr[i+1])]) #print(n_list) flag =0 #print(n_list[len(n_list)-1][1]-2) for i in range(len(lines)): if i < (n_list[0][0]-1): f2.write(lines[i]) elif i> (n_list[len(n_list)-1][1]-2): f2.write(lines[i]) elif i>(n_list[0][0]-1) and i<(n_list[len(n_list)-1][1]-2) and flag==0: flag=1 for ele in n_list: t=0 flag=1 for i in range (ele[0]-1, ele[1]-1): if lines[i].startswith("#pragma") and t==0: t=1 f2.write(stmt+'\n') f2.write('{'+'\n') f2.write(lines[i]) continue f2.write(lines[i]) if t==1: f2.write('}'+'\n')""" else: pragma_no = 0 for i in range(len(final_list[0])): if final_list[0][i] == 'True': pragma_no = i break else: continue #print(pragma_no) k = 0 t = 0 for i in range(len(lines)): #print(k) if lines[i].startswith('#pragma') and k == pragma_no: t = 1 f2.write(stmt + '\n') f2.write('{' + '\n') f2.write(lines[i]) continue elif lines[i].startswith('#pragma') and k != pragma_no: k += 1 f2.write(lines[i]) if t == 1: f2.write('}' + '\n') return 0