def build_blocks_arb(block_links, code=None): block_list = BlockList() as_tree_string = "" current_line_number = 1 for i in range(len(block_links)): basic_block = RawBasicBlock(current_line_number, current_line_number, None) current_line_number += 1 basic_block.name = chr(65 + i) block_list.append(basic_block) if code is not None: ast_stmts = code.get(basic_block.name) as_tree_string += ast_stmts basic_block.end_line += (ast_stmts.count("\n") - 1) current_line_number = basic_block.end_line + 1 for key, value in block_links.items(): key_block = block_list.get_block_by_name(key) for value_block_str in value: Cfg.connect_2_blocks(key_block, block_list.get_block_by_name(value_block_str)) if code is None: return block_list else: return block_list, as_tree_string
def __init__(self): # 配置 self.m_cfg = Cfg() # 基础配置 self.m_cfg.load_cfg('basic.json') self.basic_info = self.m_cfg.get_basic_cfg() # 下行连续数据区 self.m_store_seq_down = Sequence(self.basic_info.sequence_max) # 下行离散数据区 self.m_store_dis_down = Discrete(self.basic_info.discrete_max) # 上行连续数据区 self.m_store_seq_up = Sequence(self.basic_info.sequence_max) # 上行离散数据区 self.m_store_dis_up = Discrete(self.basic_info.discrete_max) # 上行连续发布泵(关联下行连续数据区) self.m_pump_seq_up = PubSeq(self.m_store_seq_down) # 上行离散发布泵(关联下行离散数据区) self.m_pump_dis_up = PubDis(self.m_store_dis_down) # 下行连续发布泵(关联上行连续数据区) self.m_pump_seq_down = PubSeq(self.m_store_seq_up) # 下行离散发布泵(关联上行离散数据区) self.m_pump_dis_down = PubDis(self.m_store_dis_up) # 驱动字典 self.m_driver_dict = {} # 主题字典 self.m_topic_dict = {} # 主题数据字典 self.m_topic_data_dict = {}
def test_build_while_body_given_body_if(self): as_tree = ast.parse( ms("""\ while a < 3: # 1st block if a < 2: # 2nd block z = 2 # 3rd block b = 2 # 4th block """)) while_block = RawBasicBlock(1, 1, 'While') cfg_handler = Cfg() cfg_handler.as_tree = as_tree real_tail_list = cfg_handler.build_while_body(while_block) expected_block_list = build_blocks([1, 1, 'While'], [2, 2, 'If'], [3, 3, None], [4, 4, None], block_links={ '0': [1], '1': [2, 3], '2': [3], '3': [0] }) self.assertBasicBlockEqual(while_block, expected_block_list[0]) self.assertBasicBlockEqual(real_tail_list[0], expected_block_list[0])
def test_initial_info_given_3_simple_stmt_given_if(self): as_tree = ast.parse( ms("""\ a = 3 if c < 3: y = a + b x, y = a, b """)) cfg_real = Cfg(as_tree) cfg_real.gather_initial_info() expected_ue_var = ({'c'}, {'a', 'b'}) expected_var_kill = ({'a'}, {'y', 'x'}) self.assertUeVarKill(cfg_real.block_list, expected_ue_var, expected_var_kill) expected_globals_var = {'b', 'a', 'c'} self.assertSetEqual(cfg_real.globals_var, expected_globals_var) expected_block_set = { 'x': [cfg_real.block_list[1]], 'a': [cfg_real.block_list[0]], 'y': [cfg_real.block_list[1]] } self.assertDictEqual(cfg_real.block_set, expected_block_set)
def test_initial_info_given_3_simple_stmt_expect_ue_a_vk_a_y_x(self): as_tree = ast.parse( ms("""\ a = 3 y = a + b x, y = a, b """)) cfg_real = Cfg(as_tree) cfg_real.gather_initial_info() expected_ue_var = {'b'} expected_var_kill = {'a', 'y', 'x'} self.assertSetEqual(cfg_real.block_list[0].ue_var, expected_ue_var) self.assertSetEqual(cfg_real.block_list[0].var_kill, expected_var_kill) expected_globals_var = {'b'} self.assertSetEqual(cfg_real.globals_var, expected_globals_var) expected_block_set = { 'x': [cfg_real.block_list[0]], 'a': [cfg_real.block_list[0]], 'y': [cfg_real.block_list[0]] } self.assertDictEqual(cfg_real.block_set, expected_block_set)
def test_dominator_tree_given_13_blocks(self): """ |---------> R | / | \ | A <-- B C | | / \ | \ | D <-- E <- | ---- G | | | | F / \ | L | | | | J | \ | | \ | | | ----> H --| I <----| | \ / ----------------------> K ---/ """ blocks = self.build_blocks_arb( block_links={ 'A': ['B', 'C', 'H'], 'B': ['D'], 'C': ['B', 'D', 'F'], 'D': ['E'], 'E': ['G'], 'F': ['G'], 'G': ['F', 'M'], 'H': ['I', 'J'], 'I': ['L'], 'J': ['K', 'L'], 'K': ['L'], 'L': ['M'], 'M': ['A', 'L'] }) cfg_real = Cfg() cfg_real.block_list = blocks cfg_real.root = blocks[0] dom_tree = DominatorTree(cfg_real) dom_tree.fill_dominates() dom_tree.build_tree() expected_blocks = self.build_blocks_arb( block_links={ 'A': ['B', 'C', 'D', 'F', 'G', 'H', 'L', 'M'], 'B': [], 'C': [], 'D': ['E'], 'E': [], 'F': [], 'G': [], 'H': ['I', 'J'], 'I': [], 'J': ['K'], 'K': [], 'L': [], 'M': [] }) self.assertBasicBlockListEqual(dom_tree.dominator_nodes, expected_blocks)
def test_get_ast_node_given_2_assign(self): as_tree = ast.parse(ms("""\ a = 3 a = 4 """) ) cfg_real = Cfg() node = cfg_real.get_ast_node(as_tree, 2) self.assertEqual(node, as_tree.body[1])
def build(): """生成""" # Generate file head. cpp_file_path = op.join(Cfg.getcodepath(), 'pyllbc_scripts.h') cpp_file = CppFile(cpp_file_path, Cfg.getauthor(), Cfg.getver()) cpp_file.addincl('pyllbc/common/LibHeader.h') cpp_file.addincl('pyllbc/common/ScriptIntegrator.h') # Generate constructor/destructor. class_name = 'pyllbc_Scripts' ctor = CppFun(class_name, rtn='', visit=CppVisit(CppVisit.PUBLIC)) for root, dirs, files in os.walk(Cfg.getscriptpath()): for f in files: fpath = op.join(root, f) if '.svn' in fpath: continue elif op.splitext(fpath)[1].lower() != '.py': continue script_cnt = IntegratorBuilder.__transfer_to_cpp_str(fpath) ctor.addstmt('_scripts.insert(::std::make_pair("{}", new ::pyllbc_ScriptIntegrator({})));' .format(op.basename(fpath), script_cnt)) dtor = CppFun('~{}'.format(class_name), rtn='', visit=CppVisit(CppVisit.PUBLIC)) dtor.addstmt('LLBC_STLHelper::DeleteContainer(_scripts);') clazz = CppClass(class_name) clazz.addmethod(ctor) clazz.addmethod(dtor) # Generate integrate method. integrate_meth = CppFun('Integrate', rtn='int', visit=CppVisit(CppVisit.PUBLIC)) integrate_meth.addstmt("for (::std::map<LLBC_String, " "::pyllbc_ScriptIntegrator *>::iterator it = _scripts.begin();") integrate_meth.addstmt(' it != _scripts.end();') integrate_meth.addstmt(' it++)') integrate_meth.addstmt(' if (it->second->Integrate() != LLBC_OK)') integrate_meth.addstmt(' return LLBC_FAILED;') integrate_meth.addstmt('') integrate_meth.addstmt('return LLBC_OK;') clazz.addmethod(integrate_meth) # Generate data member. data_mem = CppData('::std::map<LLBC_String, ::pyllbc_ScriptIntegrator *>', '_scripts', False, CppVisit(CppVisit.PRIVATE)) clazz.adddata(data_mem) cpp_file.addcls(clazz) cpp_file.adddef('pyllbc_s_Scripts LLBC_Singleton<{}, LLBC_DummyLock>::Instance()'.format(class_name)) cpp_file.build()
def build_tree(self): # TODO: clarify the code below for block_in_cfg in self.cfg.walk_block(self.cfg.root): block_in_dom_list = RawBasicBlock(block_in_cfg.start_line, block_in_cfg.end_line) self.dominator_nodes.append(block_in_dom_list) for dom_block in block_in_cfg.dominates_list: dom_block_in_list = self.dominator_nodes.get_block(dom_block) if not dom_block_in_list.prev_block_list: Cfg.connect_2_blocks(block_in_dom_list, dom_block_in_list) self.dominator_root = self.dominator_nodes[-1]
def test_delete_node(self): as_tree = ast.parse(ms("""\ z = 2 # 0th block while a < 3: # 1st block if a < 2: # 2nd block z = 2 # 3rd block b = 2 # 4th block c = 3 # 5th block """)) cfg_real = Cfg(as_tree) cfg_real.root = cfg_real.delete_node(cfg_real.root, RawBasicBlock(1, 1)) pass
def test_get_ast_node_given_if(self): as_tree = ast.parse(ms("""\ a = 3 if a < 3: z = 2 a = 4 """) ) cfg_real = Cfg() node = cfg_real.get_ast_node(as_tree, 3) self.assertEqual(node, as_tree.body[1].body[0])
def build(search_path): """构建方法文件""" # Create cpp file. search_base = op.basename(search_path).capitalize() cpp_file_path = op.join(search_path, '_{}Methods.h'.format(search_base)) cpp_file = CppFile(cpp_file_path, Cfg.getauthor(), Cfg.getver()) cpp_file.addincl('pyllbc/common/LibHeader.h') # Get all methods methods = {} code_path = Cfg.getcodepath() c = re.compile(r'(_[a-zA-Z]+)+\.h') for root, dirs, files in os.walk(search_path): for f in files: fpath = op.join(root, f) if '.svn' in fpath: continue elif fpath == cpp_file_path: continue if c.match(f): file_methods = MethodsBuilder.__parse_file(fpath) if file_methods: incl = fpath[len(op.dirname(code_path)) + 1:] cpp_file.addincl(incl) methods.update(file_methods) # Create cpp class. cls_name = 'pyllbc_{}Methods'.format(search_base) cls = CppClass(cls_name) first_data = True ctor = CppFun(cls_name, rtn='', visit=CppVisit(CppVisit.PUBLIC)) for meth_name, meth in methods.iteritems(): ctor.addstmt('{}.ml_name = {};'.format(meth_name, meth['name'])) ctor.addstmt('{}.ml_meth = {};'.format(meth_name, meth['meth'])) ctor.addstmt('{}.ml_flags = {};'.format(meth_name, meth['flags'])) ctor.addstmt('{}.ml_doc = {};'.format(meth_name, meth['doc'])) visit = CppVisit(CppVisit.PUBLIC) if first_data else CppVisit(CppVisit.NONE) cls.adddata(CppData('::PyMethodDef', meth_name, visit=visit)) first_data = False cls.addmethod(ctor) cpp_file.addcls(cls) cpp_file.adddef('pyllbc_s_{0}Methods LLBC_Singleton< ::pyllbc_{0}Methods, LLBC_DummyLock>::Instance()' .format(search_base)) cpp_file.build()
def test_dominator_tree_given_complex_block(self): """ A | B <------| / \ | C F | | / \ | | G I | | \ / | | H | \ / | D-----------| | E """ blocks = self.build_blocks_arb( block_links={ 'A': ['B'], 'B': ['C', 'F'], 'C': ['D'], 'D': ['E', 'B'], 'E': [], 'F': ['G', 'I'], 'G': ['H'], 'H': ['D'], 'I': ['H'] }) cfg_real = Cfg() cfg_real.block_list = blocks cfg_real.root = blocks[0] dom_tree = DominatorTree(cfg_real) dom_tree.fill_dominates() dom_tree.build_tree() expected_blocks = self.build_blocks_arb( block_links={ 'A': ['B'], 'B': ['C', 'D', 'F'], 'C': [], 'D': ['E'], 'E': [], 'F': ['G', 'H', 'I'], 'G': [], 'H': [], 'I': [] }) self.assertBasicBlockListEqual(dom_tree.dominator_nodes, expected_blocks)
def build_blocks_arb(self, block_links): block_list = BlockList() for i in range(len(block_links)): basic_block = RawBasicBlock(i, i, None) basic_block.name = chr(65 + i) block_list.append(basic_block) for key, value in block_links.items(): key_block = block_list.get_block_by_name(key) for value_block_str in value: Cfg.connect_2_blocks( key_block, block_list.get_block_by_name(value_block_str)) return block_list
def test_get_simple_block_given_no_indent(self): as_tree = ast.parse(ms("""\ a = 3 a = 4 """) ) cfg_holder = Cfg() simple_block_list = [] for simple_block in cfg_holder.get_basic_block(as_tree.body): simple_block_list.append(simple_block) expected_block = RawBasicBlock(start_line=1, end_line=2) self.assertBasicBlockEqual(simple_block_list[0], expected_block)
def test_walk_given_recursive_block(self): as_tree = ast.parse(ms("""\ z = 2 # 0th block while a < 3: # 1st block if a < 2: # 2nd block z = 2 # 3rd block b = 2 # 4th block c = 3 # 5th block """)) cfg_real = Cfg(as_tree) blocks_list = [] for blocks in cfg_real.walk_block(cfg_real.block_list[0]): blocks_list.append(blocks) pass
def test_compute_liveout_given_5_blocks(self): blocks, ast_string = th.build_blocks_arb(block_links={ 'A': ['B'], 'B': ['C', 'D'], 'C': ['D'], 'D': ['E', 'B'], 'E': [] }, code={ 'A': ms("""\ i = 1 """), 'B': ms("""\ if i < 0: pass """), 'C': ms("""\ s = 0 """), 'D': ms("""\ s = s + i i = i + 1 if i < 0: pass """), 'E': ms("""\ if s < 3: pass """) }) as_tree = ast.parse(ast_string) cfg_real = Cfg() cfg_real.block_list = blocks cfg_real.as_tree = as_tree cfg_real.root = cfg_real.block_list[0] cfg_real.gather_initial_info() cfg_real.compute_live_out() expected_live_out = { 'A': {'s', 'i'}, 'B': {'s', 'i'}, 'C': {'s', 'i'}, 'D': {'s', 'i'}, 'E': set() } self.assertLiveOutEqual(cfg_real.block_list, expected_live_out)
def test_fill_df_given_while(self): as_tree = ast.parse( ms("""\ z = 2 # 0th block while a < 3: # 1st block if a < 2: # 2nd block z = 2 # 3rd block b = 2 # Eth block c = 3 # Fth block """)) cfg_real = Cfg(as_tree) dom_tree = DominatorTree(cfg_real) dom_tree.fill_dominates() dom_tree.build_tree() dom_tree.fill_df() self.assertDfEqual( dom_tree, [], [2, 2], [2, 2], [5, 5], [2, 2], [], )
def assertCfgWithBasicBlocks(self, cfg_real, *args, block_links): cfg_expected = Cfg() block_list = build_blocks(*args, block_links=block_links) cfg_expected.block_list.extend(block_list) self.assertCfgEqual(cfg_real, cfg_expected)
def test_build_while_body_given_only_while(self): as_tree = ast.parse(ms("""\ while a < 3: # 1st block z = 4 # 2nd block """)) while_block = RawBasicBlock(1, 1, 'While') cfg_handler = Cfg() cfg_handler.as_tree = as_tree real_tail_list = cfg_handler.build_while_body(while_block) expected_block_list = build_blocks([1, 1, 'While'], [2, 2, None], block_links={'0': [1], '1': [0]}) self.assertBasicBlockEqual(while_block, expected_block_list[0]) self.assertBasicBlockEqual(real_tail_list[0], expected_block_list[0])
def test_build_dominator_tree_given_2_lvl(self): as_tree = ast.parse( ms("""\ z = 2 # 0th block while a < 3: # 1st block if a < 2: # 2nd block z = 2 # 3rd block b = 2 # Eth block c = 3 # Fth block """)) cfg_real = Cfg(as_tree) dom_tree = DominatorTree(cfg_real) dom_tree.fill_dominates() dom_tree.build_tree() expected_block_list = build_blocks([5, 5, None], [4, 4, None], [3, 3, None], [6, 6, None], [2, 2, None], [1, 1, None], block_links={ '5': [4], '4': [2, 3], '2': [1, 0], '1': [], '0': [], '3': [] }) self.assertBasicBlockListEqual(dom_tree.dominator_nodes, expected_block_list) pass
def main(): # program start print("ParkSmart is starting up...") loop_count = 0 cfg = Cfg() camera = Camera(cfg.Camera) classifier = Classifier(cfg.Classifier) viewset = ViewSet(cfg.ViewSet, classifier) print("Now beginning to process the current ViewSet.") while True: print("Views have been processed {} times.".format(str(loop_count))) for view in viewset.views: img_pos = view.get_img_pos() print(img_pos) camera.move(img_pos[0], img_pos[1]) # NOTE: might need to cal a sleep to give camera time to move view_img = camera.capture() view.update(view_img) # TODO: print("The current state:") curr_views_dict = viewset.get_set_state() df = pd.DataFrame(curr_views_dict) df = df[['view_id', 'id', 'state', 'l_update']] df.to_csv('output.csv', index=False) print(df) loop_count += 1
def test_build_dominator_tree_given_1_lvl(self): as_tree = ast.parse( ms("""\ a = 3 # 1st if a > 3: # | a = E # 2nd else: # 3rd z = F # | y = F # Eth """)) cfg_real = Cfg(as_tree) dom_tree = DominatorTree(cfg_real) dom_tree.fill_dominates() dom_tree.build_tree() expected_block_list = build_blocks([6, 6, None], [3, 3, None], [5, 5, None], [1, 2, None], block_links={ '3': [1, 2, 0], '2': [], '1': [], '0': [] }) self.assertBasicBlockListEqual(dom_tree.dominator_nodes, expected_block_list)
def test_get_ast_node_given_if_elif_else(self): as_tree = ast.parse(ms("""\ a = 3 if a < 3: z = 2 elif z < 2: x = 2 else: y = 2 a = 4 """) ) cfg_real = Cfg() node = cfg_real.get_ast_node(as_tree, 5) self.assertEqual(node, as_tree.body[1].orelse[0].body[0])
def test_get_ast_stmt_from_block_given_if(self): as_tree = ast.parse( ms("""\ a = 3 if a < 3: x, y = a, b """)) block_1 = RawBasicBlock(1, 3) cfg_holder = Cfg() cfg_holder.as_tree = as_tree tuples_list_real = [(x, y) for x, y in cfg_holder.get_var_ast(block_1)] tuples_list_expected = [(['a'], []), ([], ['a']), (['x', 'y'], ['a', 'b'])] self.assertListEqual(tuples_list_expected, tuples_list_real)
def test_cfg_given_no_branch(self): as_tree = ast.parse( ms("""\ a = 3 a = 4 """)) cfg_real = Cfg(as_tree) self.assertCfgWithBasicBlocks(cfg_real, [1, 2, None], block_links={})
def test_get_simple_block_given_if(self): as_tree = ast.parse(ms("""\ a = 3 if a < 3: b = 4 c = 5 """) ) cfg_holder = Cfg() simple_block_list = [] for basic_block in cfg_holder.get_basic_block(as_tree.body): simple_block_list.append(basic_block) expected_block_0 = RawBasicBlock(start_line=1, end_line=2, block_end_type='If') expected_block_1 = RawBasicBlock(start_line=4, end_line=4) self.assertBasicBlockEqual(simple_block_list[0], expected_block_0, block_index=0) self.assertBasicBlockEqual(simple_block_list[1], expected_block_1, block_index=1)
def test_walk_given_2_block(self): as_tree = ast.parse(ms("""\ z = 2 if z < 2: y = 3 """)) cfg_real = Cfg(as_tree) # cfg_real.walk_block(cfg_real.block_list[0]) blocks_list = [] for blocks in cfg_real.walk_block(cfg_real.block_list[0]): blocks_list.append(blocks) expected_block_list = build_blocks([3, 3, None], [1, 2, 'If'], block_links={'1': [0], '0':[]}) self.assertBasicBlockListEqual(blocks_list, expected_block_list)
def assertCfgWithAst(self, cfg_real, *args): cfg_expected = Cfg() # a = 3 # a = 4 for ast_list in args: basic_block = RawBasicBlock() basic_block.ast_list.extend(ast_list) cfg_expected.block_list.append(basic_block) self.assertCfgEqual(cfg_real, cfg_expected)
def main(): print 'Build methods...', code_path = Cfg.getcodepath() MethodsBuilder.build(op.join(code_path, 'common')) MethodsBuilder.build(op.join(code_path, 'testcase')) MethodsBuilder.build(op.join(code_path, 'core')) MethodsBuilder.build(op.join(code_path, 'comm')) MethodsBuilder.build(op.join(code_path, 'application')) print 'Done' print 'Build script integrator...', IntegratorBuilder.build() print 'Done' sleep(1.618)
def main(): print "Build methods...", code_path = Cfg.getcodepath() MethodsBuilder.build(op.join(code_path, "common")) MethodsBuilder.build(op.join(code_path, "testcase")) MethodsBuilder.build(op.join(code_path, "core")) MethodsBuilder.build(op.join(code_path, "comm")) MethodsBuilder.build(op.join(code_path, "application")) print "Done" print "Build script integrator...", IntegratorBuilder.build() print "Done" sleep(1.618)
def CreateCommand(jid, message): """ 按照消息创建新Command的实例 """ if (message[:1] == Cfg.cfg().CommandPrefix): idx = message.find(" ") if (idx > 0): commandName = message[1:idx] else: commandName = message[1:] if (commandName in GooCommands): cls = GooCommands[commandName] return cls(jid, message) return BaseCommand(jid, message)
def test_cfg_given_if_else_without_link_tail(self): as_tree = ast.parse(ms("""\ a = 3 # 0 if a > 3: # 1 a = 4 # 2 else: # 3 z = 5 # 4 """) ) cfg_real = Cfg(as_tree) self.assertCfgWithBasicBlocks(cfg_real, [1, 2, 'If'], [3, 3, None], [5, 5, None], block_links={'0': [1, 2], '1': [], '2': []} )
def test_cfg_given_while_body_if(self): as_tree = ast.parse(ms("""\ z = 2 # 0th block while a < 3: # 1st block if a < 2: # 2nd block z = 2 # 3rd block b = 2 # 4th block c = 3 # 5th block """)) cfg_real = Cfg(as_tree) self.assertCfgWithBasicBlocks(cfg_real, [1, 1, None], [2, 2, 'While'], [3, 3, 'If'], [4, 4, None], [5, 5, None], [6, 6, None], block_links={'0': [1], '1': [2, 5], '2': [3, 4], '3': [4], '4': [1], '5': []})
def test_fill_dominate_given_if_else(self): as_tree = ast.parse( ms("""\ a = 3 # 1st if a > 3: # | a = E # 2nd else: # 3rd z = F # | y = F # Eth """)) cfg_real = Cfg(as_tree) dom_tree = DominatorTree(cfg_real) dom_tree.fill_dominates() expected_dominator = {'0': [1, 2, 3], '1': [], '2': [], '3': []} self.assertDominatorEqual(dom_tree, expected_dominator)