Owen Anderson | 6cca67f | 2011-09-22 22:32:22 +0000 | [diff] [blame] | 1 | //===- lib/MC/MCAtom.cpp - MCAtom implementation --------------------------===// |
| 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 | #include "llvm/MC/MCAtom.h" |
| 11 | #include "llvm/MC/MCModule.h" |
| 12 | #include "llvm/Support/ErrorHandling.h" |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 13 | #include <iterator> |
Owen Anderson | 6cca67f | 2011-09-22 22:32:22 +0000 | [diff] [blame] | 14 | |
| 15 | using namespace llvm; |
| 16 | |
Juergen Ributzka | d12ccbd | 2013-11-19 00:57:56 +0000 | [diff] [blame] | 17 | // Pin the vtable to this file. |
| 18 | void MCAtom::anchor() {} |
| 19 | |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 20 | void MCAtom::remap(uint64_t NewBegin, uint64_t NewEnd) { |
| 21 | Parent->remap(this, NewBegin, NewEnd); |
Owen Anderson | bed5504 | 2011-10-10 18:09:38 +0000 | [diff] [blame] | 22 | } |
| 23 | |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 24 | void MCAtom::remapForTruncate(uint64_t TruncPt) { |
| 25 | assert((TruncPt >= Begin && TruncPt < End) && |
| 26 | "Truncation point not contained in atom!"); |
| 27 | remap(Begin, TruncPt); |
Owen Anderson | bed5504 | 2011-10-10 18:09:38 +0000 | [diff] [blame] | 28 | } |
| 29 | |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 30 | void MCAtom::remapForSplit(uint64_t SplitPt, |
| 31 | uint64_t &LBegin, uint64_t &LEnd, |
| 32 | uint64_t &RBegin, uint64_t &REnd) { |
Owen Anderson | 6cca67f | 2011-09-22 22:32:22 +0000 | [diff] [blame] | 33 | assert((SplitPt > Begin && SplitPt <= End) && |
| 34 | "Splitting at point not contained in atom!"); |
| 35 | |
| 36 | // Compute the new begin/end points. |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 37 | LBegin = Begin; |
| 38 | LEnd = SplitPt - 1; |
| 39 | RBegin = SplitPt; |
| 40 | REnd = End; |
Owen Anderson | 6cca67f | 2011-09-22 22:32:22 +0000 | [diff] [blame] | 41 | |
| 42 | // Remap this atom to become the lower of the two new ones. |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 43 | remap(LBegin, LEnd); |
| 44 | } |
Owen Anderson | 6cca67f | 2011-09-22 22:32:22 +0000 | [diff] [blame] | 45 | |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 46 | // MCDataAtom |
Owen Anderson | 6cca67f | 2011-09-22 22:32:22 +0000 | [diff] [blame] | 47 | |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 48 | void MCDataAtom::addData(const MCData &D) { |
| 49 | Data.push_back(D); |
Rafael Espindola | bab2afb | 2013-10-16 18:26:16 +0000 | [diff] [blame] | 50 | if (Data.size() > End + 1 - Begin) |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 51 | remap(Begin, End + 1); |
| 52 | } |
Owen Anderson | 6cca67f | 2011-09-22 22:32:22 +0000 | [diff] [blame] | 53 | |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 54 | void MCDataAtom::truncate(uint64_t TruncPt) { |
| 55 | remapForTruncate(TruncPt); |
Owen Anderson | 6cca67f | 2011-09-22 22:32:22 +0000 | [diff] [blame] | 56 | |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 57 | Data.resize(TruncPt - Begin + 1); |
| 58 | } |
Owen Anderson | 6cca67f | 2011-09-22 22:32:22 +0000 | [diff] [blame] | 59 | |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 60 | MCDataAtom *MCDataAtom::split(uint64_t SplitPt) { |
| 61 | uint64_t LBegin, LEnd, RBegin, REnd; |
| 62 | remapForSplit(SplitPt, LBegin, LEnd, RBegin, REnd); |
Owen Anderson | 6cca67f | 2011-09-22 22:32:22 +0000 | [diff] [blame] | 63 | |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 64 | MCDataAtom *RightAtom = Parent->createDataAtom(RBegin, REnd); |
| 65 | RightAtom->setName(getName()); |
Owen Anderson | 6cca67f | 2011-09-22 22:32:22 +0000 | [diff] [blame] | 66 | |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 67 | std::vector<MCData>::iterator I = Data.begin() + (RBegin - LBegin); |
| 68 | assert(I != Data.end() && "Split point not found in range!"); |
Owen Anderson | 6cca67f | 2011-09-22 22:32:22 +0000 | [diff] [blame] | 69 | |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 70 | std::copy(I, Data.end(), std::back_inserter(RightAtom->Data)); |
| 71 | Data.erase(I, Data.end()); |
Owen Anderson | 6cca67f | 2011-09-22 22:32:22 +0000 | [diff] [blame] | 72 | return RightAtom; |
| 73 | } |
| 74 | |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 75 | // MCTextAtom |
Owen Anderson | 6cca67f | 2011-09-22 22:32:22 +0000 | [diff] [blame] | 76 | |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 77 | void MCTextAtom::addInst(const MCInst &I, uint64_t Size) { |
Ahmed Bougacha | c43aa4e | 2013-08-21 07:27:47 +0000 | [diff] [blame] | 78 | if (NextInstAddress + Size - 1 > End) |
| 79 | remap(Begin, NextInstAddress + Size - 1); |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 80 | Insts.push_back(MCDecodedInst(I, NextInstAddress, Size)); |
| 81 | NextInstAddress += Size; |
Owen Anderson | 6cca67f | 2011-09-22 22:32:22 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 84 | void MCTextAtom::truncate(uint64_t TruncPt) { |
| 85 | remapForTruncate(TruncPt); |
| 86 | |
| 87 | InstListTy::iterator I = Insts.begin(); |
| 88 | while (I != Insts.end() && I->Address <= TruncPt) ++I; |
| 89 | |
| 90 | assert(I != Insts.end() && "Truncation point not found in disassembly!"); |
| 91 | assert(I->Address == TruncPt + 1 && |
| 92 | "Truncation point does not fall on instruction boundary"); |
| 93 | |
| 94 | Insts.erase(I, Insts.end()); |
| 95 | } |
| 96 | |
| 97 | MCTextAtom *MCTextAtom::split(uint64_t SplitPt) { |
| 98 | uint64_t LBegin, LEnd, RBegin, REnd; |
| 99 | remapForSplit(SplitPt, LBegin, LEnd, RBegin, REnd); |
| 100 | |
| 101 | MCTextAtom *RightAtom = Parent->createTextAtom(RBegin, REnd); |
| 102 | RightAtom->setName(getName()); |
| 103 | |
| 104 | InstListTy::iterator I = Insts.begin(); |
| 105 | while (I != Insts.end() && I->Address < SplitPt) ++I; |
| 106 | assert(I != Insts.end() && "Split point not found in disassembly!"); |
| 107 | assert(I->Address == SplitPt && |
| 108 | "Split point does not fall on instruction boundary!"); |
| 109 | |
| 110 | std::copy(I, Insts.end(), std::back_inserter(RightAtom->Insts)); |
| 111 | Insts.erase(I, Insts.end()); |
Ahmed Bougacha | ff12d02 | 2013-08-21 07:28:24 +0000 | [diff] [blame] | 112 | Parent->splitBasicBlocksForAtom(this, RightAtom); |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 113 | return RightAtom; |
| 114 | } |