00001 #ifndef ROSE_AsmUnparser_H
00002 #define ROSE_AsmUnparser_H
00003
00004 #include <ostream>
00005
00006 #include "callbacks.h"
00007 #include "BinaryControlFlow.h"
00008
00009 class SgAsmInstruction;
00010 class SgAsmBlock;
00011 class SgAsmFunction;
00012 class SgAsmInterpretation;
00013
00014
00176 class AsmUnparser {
00177 public:
00178 enum Organization {
00179 ORGANIZED_BY_AST,
00184 ORGANIZED_BY_ADDRESS
00188 };
00189
00192 typedef BinaryAnalysis::ControlFlow::Graph CFG;
00193 typedef boost::graph_traits<CFG>::vertex_descriptor CFG_Vertex;
00194 typedef std::map<SgAsmBlock*, CFG_Vertex> CFG_BlockMap;
00195
00196 class UnparserCallback {
00197 public:
00199 struct GeneralArgs {
00200 GeneralArgs(AsmUnparser *unparser, std::ostream &output)
00201 : unparser(unparser), output(output) {}
00202 AsmUnparser *unparser;
00203 std::ostream &output;
00204 };
00205
00207 struct InsnArgs: public GeneralArgs {
00208 InsnArgs(AsmUnparser *unparser, std::ostream &output, SgAsmInstruction *insn, size_t position_in_block)
00209 : GeneralArgs(unparser, output), insn(insn), position_in_block(position_in_block) {}
00210 SgAsmInstruction *get_node() const { return insn; }
00211 SgAsmInstruction *insn;
00212 size_t position_in_block;
00213 };
00214
00216 struct BasicBlockArgs: public GeneralArgs {
00217 BasicBlockArgs(AsmUnparser *unparser, std::ostream &output, SgAsmBlock *block,
00218 const std::vector<SgAsmInstruction*> &insns)
00219 : GeneralArgs(unparser, output), block(block), insns(insns) {}
00220 SgAsmBlock *get_node() const { return block; }
00221 SgAsmBlock *block;
00222 const std::vector<SgAsmInstruction*> &insns;
00223 };
00224
00226 struct StaticDataArgs: public GeneralArgs {
00227 StaticDataArgs(AsmUnparser *unparser, std::ostream &output, SgAsmStaticData *data, size_t position_in_block)
00228 : GeneralArgs(unparser, output), data(data), position_in_block(position_in_block) {}
00229 SgAsmStaticData *get_node() const { return data; }
00230 SgAsmStaticData *data;
00231 size_t position_in_block;
00232 };
00233
00235 struct DataBlockArgs: public GeneralArgs {
00236 DataBlockArgs(AsmUnparser *unparser, std::ostream &output, SgAsmBlock *block,
00237 const std::vector<SgAsmStaticData*> &datalist)
00238 : GeneralArgs(unparser, output), block(block), datalist(datalist) {}
00239 SgAsmBlock *get_node() const { return block; }
00240 SgAsmBlock *block;
00241 const std::vector<SgAsmStaticData*> &datalist;
00242 };
00243
00245 struct FunctionArgs: public GeneralArgs {
00246 FunctionArgs(AsmUnparser *unparser, std::ostream &output, SgAsmFunction *func)
00247 : GeneralArgs(unparser, output), func(func) {}
00248 SgAsmFunction *get_node() const { return func; }
00249 SgAsmFunction *func;
00250 };
00251
00253 struct InterpretationArgs: public GeneralArgs {
00254 InterpretationArgs(AsmUnparser *unparser, std::ostream &output, SgAsmInterpretation *interp)
00255 : GeneralArgs(unparser, output), interp(interp) {}
00256 SgAsmInterpretation *get_node() const { return interp; }
00257 SgAsmInterpretation *interp;
00258 };
00259
00260 virtual ~UnparserCallback() {}
00261
00272 virtual bool operator()(bool enabled, const InsnArgs&) { abort(); }
00273 virtual bool operator()(bool enabled, const BasicBlockArgs&) { abort(); }
00274 virtual bool operator()(bool enabled, const StaticDataArgs&) { abort(); }
00275 virtual bool operator()(bool enabled, const DataBlockArgs&) { abort(); }
00276 virtual bool operator()(bool enabled, const FunctionArgs&) { abort(); }
00277 virtual bool operator()(bool enabled, const InterpretationArgs&) { abort(); }
00279 };
00280
00281
00282
00283
00284
00287 class InsnBlockSeparation: public UnparserCallback {
00288 public:
00289 SgAsmBlock *prev_block;
00290 InsnBlockSeparation(): prev_block(NULL) {}
00291 virtual bool operator()(bool enabled, const InsnArgs &args);
00292 };
00293
00296 class InsnFuncEntry: public UnparserCallback {
00297 public:
00298 virtual bool operator()(bool enabled, const InsnArgs &args);
00299 };
00300
00302 class InsnAddress: public UnparserCallback {
00303 public:
00304 virtual bool operator()(bool enabled, const InsnArgs &args);
00305 };
00306
00308 class InsnRawBytes: public UnparserCallback {
00309 public:
00310 HexdumpFormat fmt;
00311 InsnRawBytes() {
00312 fmt.width = 8;
00313 fmt.pad_chars = true;
00314 }
00315 virtual bool operator()(bool enabled, const InsnArgs &args);
00316 };
00317
00322 class InsnBlockEntry: public UnparserCallback {
00323 public:
00324 bool show_function;
00325 bool show_reasons;
00326 InsnBlockEntry(): show_function(true), show_reasons(true) {}
00327 virtual bool operator()(bool enabled, const InsnArgs &args);
00328 };
00329
00331 class InsnBody: public UnparserCallback {
00332 public:
00333 virtual bool operator()(bool enabled, const InsnArgs &args);
00334 };
00335
00338 class InsnNoEffect: public UnparserCallback {
00339 public:
00340 virtual bool operator()(bool enabled, const InsnArgs &args);
00341 };
00342
00344 class InsnComment: public UnparserCallback {
00345 public:
00346 virtual bool operator()(bool enabled, const InsnArgs &args);
00347 };
00348
00350 class InsnLineTermination: public UnparserCallback {
00351 public:
00352 virtual bool operator()(bool enabled, const InsnArgs &args);
00353 };
00354
00355
00356
00357
00358
00360 class BasicBlockReasons: public UnparserCallback {
00361 public:
00362 virtual bool operator()(bool enabled, const BasicBlockArgs &args);
00363 };
00364
00367 class BasicBlockPredecessors: public UnparserCallback {
00368 public:
00369 virtual bool operator()(bool enabled, const BasicBlockArgs &args);
00370 };
00371
00373 class BasicBlockNoopUpdater: public UnparserCallback {
00374 public:
00375 bool debug;
00376 BasicBlockNoopUpdater(): debug(false) {}
00377 virtual bool operator()(bool enabled, const BasicBlockArgs &args);
00378 };
00379
00381 class BasicBlockNoopWarning: public UnparserCallback {
00382 public:
00383 virtual bool operator()(bool enabled, const BasicBlockArgs &args);
00384 };
00385
00388 class BasicBlockBody: public UnparserCallback {
00389 public:
00390 virtual bool operator()(bool enabled, const BasicBlockArgs &args);
00391 };
00392
00397 class BasicBlockSuccessors: public UnparserCallback {
00398 public:
00399 virtual bool operator()(bool enabled, const BasicBlockArgs &args);
00400 };
00401
00403 class BasicBlockLineTermination: public UnparserCallback {
00404 public:
00405 virtual bool operator()(bool enabled, const BasicBlockArgs &args);
00406 };
00407
00410 class BasicBlockCleanup: public UnparserCallback {
00411 public:
00412 virtual bool operator()(bool enabled, const BasicBlockArgs &args);
00413 };
00414
00415
00416
00417
00418
00421 class StaticDataBlockSeparation: public UnparserCallback {
00422 public:
00423 SgAsmBlock *prev_block;
00424 StaticDataBlockSeparation(): prev_block(NULL) {}
00425 virtual bool operator()(bool enabled, const StaticDataArgs &args);
00426 };
00427
00429 class StaticDataRawBytes: public UnparserCallback {
00430 public:
00431 bool show_address;
00432 bool show_offset;
00433
00434 HexdumpFormat fmt;
00435 StaticDataRawBytes() {
00436 show_address = true;
00437 show_offset = false;
00438 fmt.prefix = NULL;
00439 fmt.multiline = false;
00440 fmt.width = 8;
00441 fmt.pad_chars = true;
00442 }
00443 virtual bool operator()(bool enabled, const StaticDataArgs &args);
00444 };
00445
00450 class StaticDataBlockEntry: public UnparserCallback {
00451 public:
00452 bool show_function;
00453 bool show_reasons;
00454 StaticDataBlockEntry(): show_function(true), show_reasons(true) {}
00455 virtual bool operator()(bool enabled, const StaticDataArgs &args);
00456 };
00457
00459 class StaticDataDetails: public UnparserCallback {
00460 public:
00461 virtual bool operator()(bool enabled, const StaticDataArgs &args);
00462 };
00463
00465 class StaticDataComment: public UnparserCallback {
00466 public:
00467 virtual bool operator()(bool enabled, const StaticDataArgs &args);
00468 };
00469
00471 class StaticDataLineTermination: public UnparserCallback {
00472 public:
00473 virtual bool operator()(bool enabled, const StaticDataArgs &args);
00474 };
00475
00476
00477
00478
00479
00481 class DataBlockTitle: public UnparserCallback {
00482 public:
00483 virtual bool operator()(bool enabled, const StaticDataArgs &args);
00484 };
00485
00487 class DataBlockBody: public UnparserCallback {
00488 public:
00489 virtual bool operator()(bool enabled, const DataBlockArgs &args);
00490 };
00491
00493 class DataBlockLineTermination: public UnparserCallback {
00494 public:
00495 virtual bool operator()(bool enabled, const DataBlockArgs &args);
00496 };
00497
00498
00499
00500
00501
00503 class FunctionEntryAddress: public UnparserCallback {
00504 public:
00505 virtual bool operator()(bool enabled, const FunctionArgs &args);
00506 };
00507
00509 class FunctionSeparator: public UnparserCallback {
00510 public:
00511 virtual bool operator()(bool enabled, const FunctionArgs &args);
00512 };
00513
00515 class FunctionReasons: public UnparserCallback {
00516 public:
00517 virtual bool operator()(bool enabled, const FunctionArgs &args);
00518 };
00519
00521 class FunctionName: public UnparserCallback {
00522 public:
00523 virtual bool operator()(bool enabled, const FunctionArgs &args);
00524 };
00525
00527 class FunctionLineTermination: public UnparserCallback {
00528 public:
00529 virtual bool operator()(bool enabled, const FunctionArgs &args);
00530 };
00531
00533 class FunctionComment: public UnparserCallback {
00534 public:
00535 virtual bool operator()(bool enabled, const FunctionArgs &args);
00536 };
00537
00541 class FunctionAttributes: public UnparserCallback {
00542 public:
00543 std::string prefix;
00544 FunctionAttributes();
00545 virtual bool operator()(bool enabled, const FunctionArgs &args);
00546 };
00547
00549 class FunctionBody: public UnparserCallback {
00550 public:
00551 virtual bool operator()(bool enabled, const FunctionArgs &args);
00552 };
00553
00554
00555
00556
00557
00559 class InterpName: public UnparserCallback {
00560 public:
00561 virtual bool operator()(bool enabled, const InterpretationArgs &args);
00562 };
00563
00565 class InterpBody: public UnparserCallback {
00566 public:
00567 virtual bool operator()(bool enabled, const InterpretationArgs &args);
00568 };
00569
00570
00571
00572
00573
00574
00583 InsnBlockSeparation insnBlockSeparation;
00584 InsnFuncEntry insnFuncEntry;
00585 InsnAddress insnAddress;
00586 InsnRawBytes insnRawBytes;
00587 InsnBlockEntry insnBlockEntry;
00588 InsnBody insnBody;
00589 InsnNoEffect insnNoEffect;
00590 InsnComment insnComment;
00591 InsnLineTermination insnLineTermination;
00592
00593 BasicBlockReasons basicBlockReasons;
00594 BasicBlockPredecessors basicBlockPredecessors;
00595 BasicBlockNoopUpdater basicBlockNoopUpdater;
00596 BasicBlockNoopWarning basicBlockNoopWarning;
00597 BasicBlockBody basicBlockBody;
00598 BasicBlockSuccessors basicBlockSuccessors;
00599 BasicBlockLineTermination basicBlockLineTermination;
00600 BasicBlockCleanup basicBlockCleanup;
00601
00602 StaticDataBlockSeparation staticDataBlockSeparation;
00603 StaticDataRawBytes staticDataRawBytes;
00604 StaticDataBlockEntry staticDataBlockEntry;
00605 StaticDataDetails staticDataDetails;
00606 StaticDataComment staticDataComment;
00607 StaticDataLineTermination staticDataLineTermination;
00608
00609 DataBlockBody dataBlockBody;
00610 DataBlockLineTermination dataBlockLineTermination;
00611
00612 FunctionEntryAddress functionEntryAddress;
00613 FunctionSeparator functionSeparator;
00614 FunctionReasons functionReasons;
00615 FunctionName functionName;
00616 FunctionLineTermination functionLineTermination;
00617 FunctionComment functionComment;
00618 FunctionAttributes functionAttributes;
00619 FunctionBody functionBody;
00620
00621 InterpName interpName;
00622 InterpBody interpBody;
00625
00626
00627
00628
00630 AsmUnparser() {
00631 init();
00632 }
00633
00634 virtual ~AsmUnparser() {}
00635
00641 virtual Organization get_organization() const { return organization; }
00642 virtual void set_organization(Organization organization) { this->organization = organization; }
00650 virtual bool is_unparsable_node(SgNode *node);
00651
00657 virtual SgNode *find_unparsable_node(SgNode *ast);
00658
00663 virtual std::vector<SgNode*> find_unparsable_nodes(SgNode *ast);
00664
00673 std::vector<bool> insn_is_noop;
00674
00685 virtual size_t unparse(std::ostream&, SgNode *ast);
00686
00693 virtual bool unparse_one_node(std::ostream&, SgNode*);
00694
00698 virtual bool unparse_insn(bool enabled, std::ostream&, SgAsmInstruction*, size_t position_in_block=(size_t)-1);
00699 virtual bool unparse_basicblock(bool enabled, std::ostream&, SgAsmBlock*);
00700 virtual bool unparse_staticdata(bool enabled, std::ostream&, SgAsmStaticData*, size_t position_in_block=(size_t)-1);
00701 virtual bool unparse_datablock(bool enabled, std::ostream&, SgAsmBlock*);
00702 virtual bool unparse_function(bool enabled, std::ostream&, SgAsmFunction*);
00703 virtual bool unparse_interpretation(bool enabled, std::ostream&, SgAsmInterpretation*);
00707 typedef std::map<uint64_t, std::string> LabelMap;
00708
00713 void add_function_labels(SgNode *ast);
00714
00718 void add_control_flow_graph(const BinaryAnalysis::ControlFlow::Graph &cfg);
00719
00720 protected:
00721 struct CallbackLists {
00722 ROSE_Callbacks::List<UnparserCallback> unparse;
00723 ROSE_Callbacks::List<UnparserCallback> pre;
00724 ROSE_Callbacks::List<UnparserCallback> post;
00727 void clear() {
00728 unparse.clear();
00729 pre.clear();
00730 post.clear();
00731 }
00732 };
00733
00734 CallbackLists insn_callbacks;
00735 CallbackLists basicblock_callbacks;
00736 CallbackLists staticdata_callbacks;
00737 CallbackLists datablock_callbacks;
00738 CallbackLists function_callbacks;
00739 CallbackLists interp_callbacks;
00760 LabelMap labels;
00761
00763 Organization organization;
00764
00766 virtual void init();
00767
00769 CFG cfg;
00770
00773 CFG_BlockMap cfg_blockmap;
00774 };
00775
00776 #endif