00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #ifndef CBZ_DISMANTLE_H
00040 #define CBZ_DISMANTLE_H
00041
00042 #include <map>
00043 #include <set>
00044 #include <stack>
00045
00046 typedef map<unsigned int, unsigned int> op_id_map;
00047 typedef set<unsigned int> op_id_set;
00048
00049 class DismantleUtil {
00050 public:
00051 static void init(void);
00052 static stmtNode * empty_stmt(void) {
00053 return new exprstmtNode(NULL);
00054 }
00055 static blockNode * empty_block(void) {
00056 blockNode * ret = new blockNode(NULL, NULL);
00057 ret->stmts().push_back(empty_stmt());
00058 return ret;
00059 }
00060
00061 static char* new_id(const char *s = "") {
00062 char * buf = new char[80];
00063 snprintf(buf, 80, "__%s%s", CBZ::get_temp_id_str().c_str(), s);
00064 return buf;
00065 }
00066 static op_id_map not_relop;
00067 static exprNode * Not(exprNode *);
00068 static declNode * new_temp_decl(typeNode *, Coord);
00069 static labelNode * new_label(Coord);
00070 };
00071
00112 class Dismantle : public Changer {
00113 private:
00114 int _flags;
00115
00116 Dismantle(int flags);
00117 public:
00118 static const int DISMANTLE_CONTROL = 1 << 0;
00119 static const int DISMANTLE_EXPRS = 1 << 1;
00120
00121 static void dismantle_control(unitNode *);
00122 static void dismantle(unitNode *);
00123 virtual Node * at_unit(unitNode *, Order);
00124 virtual Node * at_proc(procNode *, Order);
00125 };
00126
00127 class StaticToGlobalDismantle : public Changer {
00128 public:
00129 StaticToGlobalDismantle(void);
00130 virtual Node * at_unit(unitNode *, Order);
00131 virtual Node * at_proc(procNode *, Order);
00132 virtual Node * at_block(blockNode *, Order);
00133 virtual Node * at_decl(declNode *, Order);
00134
00135 private:
00136 unitNode * _cur_unit;
00137 procNode * _cur_proc;
00138 block_list _blockStack;
00139 map<declNode *, decl_list> _declsToMove;
00140
00141
00142
00143 bool _declareProc;
00144 set < suespecNode * > _topLevelSuespecs;
00145 map < typeNode *, string > _newTypedefName;
00146
00147 void rename_decl(declNode *, procNode *);
00148 };
00149
00150 class LabelDismantle : public Changer {
00151 public:
00152 LabelDismantle(void);
00153 virtual Node * at_label(labelNode *, Order);
00154 };
00155
00156 class InitializerDismantle : public Changer {
00157 public:
00158 InitializerDismantle(void);
00159 virtual Node * at_block(blockNode *, Order);
00160 virtual Node * at_decl(declNode *, Order);
00161
00162 private:
00163 map<blockNode *, blockNode *> _assgblock;
00164 stack<blockNode *> _blockStack;
00165
00166 exprNode * eval_or_cast(exprNode *, typeNode *);
00167
00168 void init_scalar(declNode *, exprNode *);
00169 initializerNode * init_array(arrayNode *, expr_list_p &, expr_list_p,
00170 bool inSublist);
00171 initializerNode * init_struct(structNode *, expr_list_p &, expr_list_p,
00172 bool inSublist);
00173
00174 bool isStringLiteral(exprNode *);
00175 initializerNode * strLit2Init(const char *);
00176 };
00177
00178 class ControlDismantle : public Changer {
00179 public:
00180 ControlDismantle(void);
00181 virtual Node * at_proc(procNode *, Order ord);
00182 };
00183
00184 class BreakContinueChanger : public Changer {
00185 private:
00186 Node * _top;
00187 labelNode * _break_label;
00188 labelNode * _continue_label;
00189 loopNode * _new_container;
00190
00191 public:
00192 BreakContinueChanger(Node *, labelNode *, labelNode *, loopNode *L=NULL);
00193 virtual Node * at_break(breakNode *, Order);
00194 virtual Node * at_continue(continueNode *, Order);
00195 };
00196
00197 class LoopDismantle : public Changer {
00198 public:
00199 LoopDismantle(void);
00200 virtual Node * at_while(whileNode *, Order);
00201 virtual Node * at_for(forNode *, Order);
00202 virtual Node * at_do(doNode *, Order);
00203 };
00204
00205 class TernaryDismantle : public Changer {
00206 map<stmtNode*,blockNode*> _new_block;
00207 stmtNode *_cur_stmt;
00208 int _in_type;
00209
00210 public:
00211 TernaryDismantle(void);
00212 virtual Node * at_stmt(stmtNode *, Order);
00213 virtual Node * at_ternary(ternaryNode *, Order);
00214 virtual Node * at_type(typeNode *, Order);
00215 };
00216
00217 class SelectionDismantle : public Changer {
00218 private:
00219 map<stmtNode*,blockNode*> _expr_block;
00220 stmtNode *_cur_stmt;
00221 bool _in_expr;
00222
00223 public:
00224 SelectionDismantle(void);
00225 virtual Node * at_exprstmt(exprstmtNode *, Order);
00226 virtual Node * at_return(returnNode *, Order);
00227 virtual Node * at_binary(binaryNode *, Order);
00228 virtual Node * at_if(ifNode *, Order);
00229 virtual Node * at_switch(switchNode *, Order);
00230 virtual Node * at_stmt(stmtNode *, Order);
00231 private:
00232 indexNode * comparison_operand(exprNode *, blockNode *);
00233 Node * andand_oror_in_expr(stmtNode *, Order);
00234 };
00235
00236 class ReturnDismantle : public Changer {
00237 public:
00238 ReturnDismantle(void);
00239 virtual Node * at_proc(procNode *, Order);
00240 virtual Node * at_return(returnNode *, Order);
00241 };
00242
00243 class ArrowDismantle : public Changer {
00244 public:
00245 ArrowDismantle(void);
00246 virtual Node * at_binary(binaryNode *, Order);
00247 };
00248
00249 class ExpressionDismantle : public Changer {
00250 private:
00251 blockNode * _new_block;
00252 int _in_type;
00253 static op_id_map _op_assign_map;
00254 static op_id_set _op_post;
00255
00256 public:
00257 static void init();
00258
00259 ExpressionDismantle(void);
00260 virtual Node * at_exprstmt(exprstmtNode *, Order);
00261 virtual Node * at_type(typeNode *, Order);
00262 virtual Node * at_binary(binaryNode *, Order);
00263 virtual Node * at_unary(unaryNode *, Order);
00264 virtual Node * at_call(callNode *, Order);
00265 virtual Node * at_comma(commaNode *, Order);
00266 virtual Node * at_cast(castNode *, Order);
00267 };
00268
00269 class FlattenDismantle : public Changer {
00270 private:
00271 blockNode * _new_body;
00272 stmtNode * _last_stmt;
00273 set<string> _vars;
00274 set<string> _labels;
00275
00276 public:
00277 FlattenDismantle(void);
00278 virtual Node * at_proc(procNode *, Order);
00279 virtual Node * at_decl(declNode *, Order);
00280 virtual Node * at_label(labelNode *, Order);
00281 virtual Node * at_threeAddr(threeAddrNode *, Order);
00282 virtual Node * at_goto(gotoNode *, Order);
00283 virtual Node * at_return(returnNode *, Order);
00284 virtual Node * at_exprstmt(exprstmtNode *, Order);
00285 };
00286
00287 #endif