blob: 6d3a548d48e87b204ef1d2167cea58ed6e581c6c [file] [log] [blame]
Benjamin Kramer685a2502011-07-20 19:37:35 +00001//===-- MCFunction.h ------------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the data structures to hold a CFG reconstructed from
11// machine code.
12//
13//===----------------------------------------------------------------------===//
14
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000015#ifndef LLVM_OBJECTDUMP_MCFUNCTION_H
16#define LLVM_OBJECTDUMP_MCFUNCTION_H
17
Benjamin Kramer685a2502011-07-20 19:37:35 +000018#include "llvm/ADT/ArrayRef.h"
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000019#include "llvm/ADT/DenseSet.h"
Benjamin Kramer685a2502011-07-20 19:37:35 +000020#include "llvm/MC/MCInst.h"
21#include <map>
22
23namespace llvm {
24
25class MCDisassembler;
Benjamin Kramer41ab14b2011-08-08 18:56:44 +000026class MCInstrAnalysis;
Benjamin Kramer685a2502011-07-20 19:37:35 +000027class MemoryObject;
28class raw_ostream;
29
30/// MCDecodedInst - Small container to hold an MCInst and associated info like
31/// address and size.
32struct MCDecodedInst {
33 uint64_t Address;
34 uint64_t Size;
35 MCInst Inst;
36
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000037 MCDecodedInst() {}
Benjamin Kramer685a2502011-07-20 19:37:35 +000038 MCDecodedInst(uint64_t Address, uint64_t Size, MCInst Inst)
39 : Address(Address), Size(Size), Inst(Inst) {}
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000040
41 bool operator<(const MCDecodedInst &RHS) const {
42 return Address < RHS.Address;
43 }
Benjamin Kramer685a2502011-07-20 19:37:35 +000044};
45
46/// MCBasicBlock - Consists of multiple MCDecodedInsts and a list of successing
47/// MCBasicBlocks.
48class MCBasicBlock {
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000049 std::vector<MCDecodedInst> Insts;
50 typedef DenseSet<uint64_t> SetTy;
Benjamin Kramer685a2502011-07-20 19:37:35 +000051 SetTy Succs;
52public:
53 ArrayRef<MCDecodedInst> getInsts() const { return Insts; }
54
55 typedef SetTy::const_iterator succ_iterator;
56 succ_iterator succ_begin() const { return Succs.begin(); }
57 succ_iterator succ_end() const { return Succs.end(); }
58
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000059 bool contains(uint64_t Addr) const { return Succs.count(Addr); }
Benjamin Kramerdbc46d72011-07-25 23:10:23 +000060
Benjamin Kramer685a2502011-07-20 19:37:35 +000061 void addInst(const MCDecodedInst &Inst) { Insts.push_back(Inst); }
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000062 void addSucc(uint64_t Addr) { Succs.insert(Addr); }
63
64 bool operator<(const MCBasicBlock &RHS) const {
65 return Insts.size() < RHS.Insts.size();
66 }
Benjamin Kramer685a2502011-07-20 19:37:35 +000067};
68
69/// MCFunction - Represents a named function in machine code, containing
70/// multiple MCBasicBlocks.
71class MCFunction {
72 const StringRef Name;
73 // Keep BBs sorted by address.
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000074 typedef std::vector<std::pair<uint64_t, MCBasicBlock> > MapTy;
Benjamin Kramer685a2502011-07-20 19:37:35 +000075 MapTy Blocks;
76public:
77 MCFunction(StringRef Name) : Name(Name) {}
78
79 // Create an MCFunction from a region of binary machine code.
80 static MCFunction
81 createFunctionFromMC(StringRef Name, const MCDisassembler *DisAsm,
82 const MemoryObject &Region, uint64_t Start, uint64_t End,
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000083 const MCInstrAnalysis *Ana, raw_ostream &DebugOut,
84 SmallVectorImpl<uint64_t> &Calls);
Benjamin Kramer685a2502011-07-20 19:37:35 +000085
Benjamin Kramera894c8e2011-09-20 17:53:01 +000086 typedef MapTy::const_iterator iterator;
87 iterator begin() const { return Blocks.begin(); }
88 iterator end() const { return Blocks.end(); }
Benjamin Kramer685a2502011-07-20 19:37:35 +000089
90 StringRef getName() const { return Name; }
91
92 MCBasicBlock &addBlock(uint64_t Address, const MCBasicBlock &BB) {
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000093 Blocks.push_back(std::make_pair(Address, BB));
94 return Blocks.back().second;
Benjamin Kramer685a2502011-07-20 19:37:35 +000095 }
96};
97
98}
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000099
100#endif