Chris Lattner | f815aeb | 2002-12-03 20:56:42 +0000 | [diff] [blame] | 1 | //===-- MachineCodeEmitter.cpp - Implement the MachineCodeEmitter itf -----===// |
| 2 | // |
| 3 | // This file implements the MachineCodeEmitter interface. |
| 4 | // |
| 5 | //===----------------------------------------------------------------------===// |
| 6 | |
| 7 | #include "llvm/CodeGen/MachineCodeEmitter.h" |
| 8 | #include "llvm/CodeGen/MachineFunction.h" |
| 9 | #include "llvm/Function.h" |
| 10 | #include <iostream> |
Misha Brukman | 3432d1d | 2003-05-27 22:43:19 +0000 | [diff] [blame^] | 11 | #include <fstream> |
Chris Lattner | f815aeb | 2002-12-03 20:56:42 +0000 | [diff] [blame] | 12 | |
| 13 | namespace { |
| 14 | struct DebugMachineCodeEmitter : public MachineCodeEmitter { |
| 15 | void startFunction(MachineFunction &F) { |
| 16 | std::cout << "\n**** Writing machine code for function: " |
| 17 | << F.getFunction()->getName() << "\n"; |
| 18 | } |
| 19 | void finishFunction(MachineFunction &F) { |
| 20 | std::cout << "\n"; |
| 21 | } |
| 22 | void startBasicBlock(MachineBasicBlock &BB) { |
| 23 | std::cout << "\n--- Basic Block: " << BB.getBasicBlock()->getName()<<"\n"; |
| 24 | } |
Chris Lattner | fd33fb8 | 2003-05-08 21:54:18 +0000 | [diff] [blame] | 25 | |
Chris Lattner | e0e7217 | 2003-05-09 03:27:41 +0000 | [diff] [blame] | 26 | void startFunctionStub(const Function &F, unsigned StubSize) { |
Chris Lattner | fd33fb8 | 2003-05-08 21:54:18 +0000 | [diff] [blame] | 27 | std::cout << "\n--- Function stub for function: " << F.getName() << "\n"; |
| 28 | } |
Chris Lattner | e0e7217 | 2003-05-09 03:27:41 +0000 | [diff] [blame] | 29 | void *finishFunctionStub(const Function &F) { |
Chris Lattner | fd33fb8 | 2003-05-08 21:54:18 +0000 | [diff] [blame] | 30 | std::cout << "\n"; |
Chris Lattner | e0e7217 | 2003-05-09 03:27:41 +0000 | [diff] [blame] | 31 | return 0; |
Chris Lattner | fd33fb8 | 2003-05-08 21:54:18 +0000 | [diff] [blame] | 32 | } |
Chris Lattner | f815aeb | 2002-12-03 20:56:42 +0000 | [diff] [blame] | 33 | |
| 34 | void emitByte(unsigned char B) { |
| 35 | std::cout << "0x" << std::hex << (unsigned int)B << std::dec << " "; |
| 36 | } |
| 37 | void emitPCRelativeDisp(Value *V) { |
Chris Lattner | b72d221 | 2002-12-04 06:44:41 +0000 | [diff] [blame] | 38 | std::cout << "<disp %" << V->getName() << ": 0xXX 0xXX 0xXX 0xXX> "; |
| 39 | } |
Chris Lattner | 7775df1 | 2003-01-13 00:22:37 +0000 | [diff] [blame] | 40 | void emitGlobalAddress(GlobalValue *V, bool isPCRelative) { |
Chris Lattner | b72d221 | 2002-12-04 06:44:41 +0000 | [diff] [blame] | 41 | std::cout << "<addr %" << V->getName() << ": 0xXX 0xXX 0xXX 0xXX> "; |
Chris Lattner | f815aeb | 2002-12-03 20:56:42 +0000 | [diff] [blame] | 42 | } |
Chris Lattner | 7775df1 | 2003-01-13 00:22:37 +0000 | [diff] [blame] | 43 | void emitGlobalAddress(const std::string &Name, bool isPCRelative) { |
| 44 | std::cout << "<addr %" << Name << ": 0xXX 0xXX 0xXX 0xXX> "; |
| 45 | } |
| 46 | |
| 47 | void emitFunctionConstantValueAddress(unsigned ConstantNum, int Offset) { |
| 48 | std::cout << "<addr const#" << ConstantNum; |
| 49 | if (Offset) std::cout << " + " << Offset; |
| 50 | std::cout << "> "; |
| 51 | } |
Chris Lattner | f815aeb | 2002-12-03 20:56:42 +0000 | [diff] [blame] | 52 | }; |
| 53 | } |
| 54 | |
| 55 | |
| 56 | /// createDebugMachineCodeEmitter - Return a dynamically allocated machine |
| 57 | /// code emitter, which just prints the opcodes and fields out the cout. This |
| 58 | /// can be used for debugging users of the MachineCodeEmitter interface. |
| 59 | /// |
| 60 | MachineCodeEmitter *MachineCodeEmitter::createDebugMachineCodeEmitter() { |
| 61 | return new DebugMachineCodeEmitter(); |
| 62 | } |
Misha Brukman | 3432d1d | 2003-05-27 22:43:19 +0000 | [diff] [blame^] | 63 | |
| 64 | namespace { |
| 65 | class FilePrinterMachineCodeEmitter : public MachineCodeEmitter { |
| 66 | std::ofstream f, actual; |
| 67 | std::ostream &o; |
| 68 | MachineCodeEmitter *MCE; |
| 69 | unsigned counter; |
| 70 | bool mustClose; |
| 71 | unsigned values[4]; |
| 72 | |
| 73 | public: |
| 74 | FilePrinterMachineCodeEmitter() : |
| 75 | f("lli.out"), o(f), counter(0), mustClose(true) |
| 76 | { |
| 77 | if (! f.good()) { |
| 78 | std::cerr << "Cannot open 'lli.out' for writing\n"; |
| 79 | abort(); |
| 80 | } |
| 81 | openActual(); |
| 82 | } |
| 83 | |
| 84 | FilePrinterMachineCodeEmitter(MachineCodeEmitter &M, std::ostream &os) : |
| 85 | o(os), MCE(&M), counter(0) |
| 86 | { |
| 87 | FilePrinterMachineCodeEmitter(); |
| 88 | mustClose = false; |
| 89 | openActual(); |
| 90 | } |
| 91 | |
| 92 | ~FilePrinterMachineCodeEmitter() { |
| 93 | o << "\n"; |
| 94 | actual.close(); |
| 95 | if (mustClose) f.close(); |
| 96 | } |
| 97 | |
| 98 | void openActual() { |
| 99 | actual.open("lli.actual.obj"); |
| 100 | if (! actual.good()) { |
| 101 | std::cerr << "Cannot open 'lli.actual.obj' for writing\n"; |
| 102 | abort(); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | void startFunction(MachineFunction &F) { |
| 107 | // resolve any outstanding calls |
| 108 | if (MCE) MCE->startFunction(F); |
| 109 | } |
| 110 | void finishFunction(MachineFunction &F) { |
| 111 | if (MCE) MCE->finishFunction(F); |
| 112 | } |
| 113 | |
| 114 | void startBasicBlock(MachineBasicBlock &BB) { |
| 115 | // if any instructions were waiting for the address of this block, |
| 116 | // let them fix their addresses now |
| 117 | if (MCE) MCE->startBasicBlock(BB); |
| 118 | } |
| 119 | |
| 120 | void startFunctionStub(const Function &F, unsigned StubSize) { |
| 121 | // |
| 122 | if (MCE) MCE->startFunctionStub(F, StubSize); |
| 123 | } |
| 124 | |
| 125 | void *finishFunctionStub(const Function &F) { |
| 126 | if (MCE) return MCE->finishFunctionStub(F); |
| 127 | else return 0; |
| 128 | } |
| 129 | |
| 130 | void emitByte(unsigned char B) { |
| 131 | if (MCE) MCE->emitByte(B); |
| 132 | |
| 133 | values[counter] = (unsigned int) B; |
| 134 | if (++counter % 4 == 0 && counter != 0) { |
| 135 | o << std::hex; |
| 136 | for (unsigned i=0; i<4; ++i) { |
| 137 | if (values[i] < 16) o << "0"; |
| 138 | o << values[i] << " "; |
| 139 | actual << values[i]; |
| 140 | } |
| 141 | actual.flush(); |
| 142 | |
| 143 | o << std::dec << "\t"; |
| 144 | for (unsigned i=0; i<4; ++i) { |
| 145 | for (int j=7; j>=0; --j) { |
| 146 | o << ((values[i] >> j) & 1); |
| 147 | } |
| 148 | o << " "; |
| 149 | } |
| 150 | |
| 151 | o << "\n"; |
| 152 | |
| 153 | unsigned instr = 0; |
| 154 | for (unsigned i=0; i<4; ++i) |
| 155 | instr |= values[i] << (i*8); |
| 156 | |
| 157 | o << "--- * --- * --- * --- * ---\n"; |
| 158 | counter %= 4; |
| 159 | } |
| 160 | } |
| 161 | void emitPCRelativeDisp(Value *V) { |
| 162 | // put block in mapping BB -> { instr, address }. when BB is beginning to |
| 163 | // output, find instr, set disp, overwrite instr at addr using the |
| 164 | // unsigned value gotten from emitter |
| 165 | } |
| 166 | |
| 167 | void emitGlobalAddress(GlobalValue *V, bool isPCRelative) { |
| 168 | if (MCE) MCE->emitGlobalAddress(V, isPCRelative); |
| 169 | } |
| 170 | void emitGlobalAddress(const std::string &Name, bool isPCRelative) { |
| 171 | if (MCE) MCE->emitGlobalAddress(Name, isPCRelative); |
| 172 | } |
| 173 | |
| 174 | void emitFunctionConstantValueAddress(unsigned ConstantNum, int Offset) { |
| 175 | if (MCE) MCE->emitFunctionConstantValueAddress(ConstantNum, Offset); |
| 176 | } |
| 177 | }; |
| 178 | } |
| 179 | |
| 180 | MachineCodeEmitter *MachineCodeEmitter::createFilePrinterMachineCodeEmitter(MachineCodeEmitter &MCE) { |
| 181 | return new FilePrinterMachineCodeEmitter(MCE, std::cerr); |
| 182 | } |