00001 # -*- coding: utf-8 -*- 00002 # Copyright (c) 2009, Fixstars Corporation 00003 # All rights reserved. 00004 # 00005 # Redistribution and use in source and binary forms, with or without modification, 00006 # are permitted provided that the following conditions are met: 00007 # 00008 # * Redistributions of source code must retain the above copyright notice, 00009 # this list of conditions and the following disclaimer. 00010 # 00011 # * Redistributions in binary form must reproduce the above copyright notice, 00012 # this list of conditions and the following disclaimer in the documentation 00013 # and/or other materials provided with the distribution. 00014 # 00015 # * Neither the name of Fixstars Corporation nor the names of its contributors 00016 # may be used to endorse or promote products derived from this software 00017 # without specific prior written permission. 00018 # 00019 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 00020 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00021 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 00022 # IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 00023 # INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00024 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00025 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 00026 # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00027 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 00028 # THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00029 00030 """extract loop from translation unit""" 00031 00032 import ctrump 00033 from ctrump import * 00034 00035 def extract_loop_from_compound(list, cfg, comp): 00036 for i in comp.items: 00037 extract_loop_from_stmt(list, cfg, i) 00038 00039 def extract_loop_from_stmt(list, cfg, stmt): 00040 if stmt.code == STMT_FOR or stmt.code == STMT_WHILE: 00041 loop_cfg_info = stmt.loop_cfg 00042 00043 if loop_cfg_info.code == LOOP_COMPLICATED: 00044 extract_loop_from_stmt(list, cfg, stmt.body) 00045 00046 list.append((stmt,cfg,stmt.loop_cfg)) 00047 elif stmt.code == STMT_COMPOUND: 00048 extract_loop_from_compound(list, cfg, stmt) 00049 elif stmt.code == STMT_IF: 00050 extract_loop_from_stmt(list, cfg, stmt.body) 00051 elif stmt.code == STMT_IF_ELSE: 00052 extract_loop_from_stmt(list, cfg, stmt.then_body) 00053 extract_loop_from_stmt(list, cfg, stmt.else_body) 00054 00055 def extract_loop(tree): 00056 ret = [] 00057 00058 for i in tree.decls: 00059 if i.code == EXT_FUNCTION_DEFINITION: 00060 cfg = i.cfg 00061 extract_loop_from_compound(ret, cfg, i.body) 00062 00063 return ret