Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 1 | //===-- lib/CodeGen/ELFCodeEmitter.h ----------------------------*- C++ -*-===// |
| 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 | #ifndef ELFCODEEMITTER_H |
| 11 | #define ELFCODEEMITTER_H |
| 12 | |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 13 | #include "llvm/CodeGen/MachineCodeEmitter.h" |
| 14 | #include <vector> |
| 15 | |
| 16 | namespace llvm { |
Bruno Cardoso Lopes | 45f5d64 | 2009-07-02 18:29:24 +0000 | [diff] [blame] | 17 | class ELFWriter; |
| 18 | class ELFSection; |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 19 | |
| 20 | /// ELFCodeEmitter - This class is used by the ELFWriter to |
| 21 | /// emit the code for functions to the ELF file. |
| 22 | class ELFCodeEmitter : public MachineCodeEmitter { |
| 23 | ELFWriter &EW; |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 24 | |
| 25 | /// Target machine description |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 26 | TargetMachine &TM; |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 27 | |
| 28 | /// Section containing code for functions |
| 29 | ELFSection *ES; |
| 30 | |
| 31 | /// Relocations - These are the relocations that the function needs, as |
| 32 | /// emitted. |
| 33 | std::vector<MachineRelocation> Relocations; |
| 34 | |
Bruno Cardoso Lopes | a5e0abd | 2009-06-25 07:36:24 +0000 | [diff] [blame] | 35 | /// CPLocations - This is a map of constant pool indices to offsets from the |
| 36 | /// start of the section for that constant pool index. |
| 37 | std::vector<uintptr_t> CPLocations; |
| 38 | |
| 39 | /// CPSections - This is a map of constant pool indices to the MachOSection |
| 40 | /// containing the constant pool entry for that index. |
| 41 | std::vector<unsigned> CPSections; |
| 42 | |
Bruno Cardoso Lopes | 0b1308f | 2009-07-03 04:36:26 +0000 | [diff] [blame] | 43 | /// JTLocations - This is a map of jump table indices to offsets from the |
| 44 | /// start of the section for that jump table index. |
| 45 | std::vector<uintptr_t> JTLocations; |
| 46 | |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 47 | /// MBBLocations - This vector is a mapping from MBB ID's to their address. |
| 48 | /// It is filled in by the StartMachineBasicBlock callback and queried by |
| 49 | /// the getMachineBasicBlockAddress callback. |
| 50 | std::vector<uintptr_t> MBBLocations; |
| 51 | |
| 52 | /// FnStartPtr - Pointer to the start location of the current function |
| 53 | /// in the buffer |
Bruno Cardoso Lopes | 5d41910 | 2009-06-05 00:22:10 +0000 | [diff] [blame] | 54 | uint8_t *FnStartPtr; |
Bruno Cardoso Lopes | 0b1308f | 2009-07-03 04:36:26 +0000 | [diff] [blame] | 55 | |
| 56 | /// JumpTableSectionIdx - Holds the index of the Jump Table Section |
| 57 | unsigned JumpTableSectionIdx; |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 58 | public: |
Bruno Cardoso Lopes | 0b1308f | 2009-07-03 04:36:26 +0000 | [diff] [blame] | 59 | explicit ELFCodeEmitter(ELFWriter &ew) : EW(ew), TM(EW.TM), |
| 60 | JumpTableSectionIdx(0) {} |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 61 | |
| 62 | void startFunction(MachineFunction &F); |
| 63 | bool finishFunction(MachineFunction &F); |
| 64 | |
| 65 | void addRelocation(const MachineRelocation &MR) { |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 66 | Relocations.push_back(MR); |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) { |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 70 | if (MBBLocations.size() <= (unsigned)MBB->getNumber()) |
| 71 | MBBLocations.resize((MBB->getNumber()+1)*2); |
| 72 | MBBLocations[MBB->getNumber()] = getCurrentPCOffset(); |
| 73 | } |
| 74 | |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 75 | virtual uintptr_t getConstantPoolEntryAddress(unsigned Index) const { |
Bruno Cardoso Lopes | a5e0abd | 2009-06-25 07:36:24 +0000 | [diff] [blame] | 76 | assert(CPLocations.size() > Index && "CP not emitted!"); |
| 77 | return CPLocations[Index]; |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 78 | } |
Bruno Cardoso Lopes | a5e0abd | 2009-06-25 07:36:24 +0000 | [diff] [blame] | 79 | |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 80 | virtual uintptr_t getJumpTableEntryAddress(unsigned Index) const { |
Bruno Cardoso Lopes | 0b1308f | 2009-07-03 04:36:26 +0000 | [diff] [blame] | 81 | assert(JTLocations.size() > Index && "JT not emitted!"); |
| 82 | return JTLocations[Index]; |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | virtual uintptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const { |
Bruno Cardoso Lopes | 0b1308f | 2009-07-03 04:36:26 +0000 | [diff] [blame] | 86 | assert(MBBLocations.size() > (unsigned)MBB->getNumber() && |
| 87 | MBBLocations[MBB->getNumber()] && "MBB not emitted!"); |
| 88 | return MBBLocations[MBB->getNumber()]; |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | virtual uintptr_t getLabelAddress(uint64_t Label) const { |
| 92 | assert(0 && "Label address not implementated yet!"); |
| 93 | abort(); |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | virtual void emitLabel(uint64_t LabelID) { |
| 98 | assert(0 && "emit Label not implementated yet!"); |
| 99 | abort(); |
| 100 | } |
| 101 | |
Bruno Cardoso Lopes | a5e0abd | 2009-06-25 07:36:24 +0000 | [diff] [blame] | 102 | /// emitConstantPool - For each constant pool entry, figure out which section |
| 103 | /// the constant should live in and emit the constant. |
| 104 | void emitConstantPool(MachineConstantPool *MCP); |
| 105 | |
Bruno Cardoso Lopes | 0b1308f | 2009-07-03 04:36:26 +0000 | [diff] [blame] | 106 | /// emitJumpTables - Emit all the jump tables for a given jump table info |
| 107 | /// record to the appropriate section. |
| 108 | void emitJumpTables(MachineJumpTableInfo *MJTI); |
| 109 | |
| 110 | virtual void setModuleInfo(llvm::MachineModuleInfo* MMI) {} |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 111 | |
| 112 | /// JIT SPECIFIC FUNCTIONS - DO NOT IMPLEMENT THESE HERE! |
| 113 | void startGVStub(const GlobalValue* F, unsigned StubSize, |
| 114 | unsigned Alignment = 1) { |
| 115 | assert(0 && "JIT specific function called!"); |
| 116 | abort(); |
| 117 | } |
| 118 | void startGVStub(const GlobalValue* F, void *Buffer, unsigned StubSize) { |
| 119 | assert(0 && "JIT specific function called!"); |
| 120 | abort(); |
| 121 | } |
| 122 | void *finishGVStub(const GlobalValue *F) { |
| 123 | assert(0 && "JIT specific function called!"); |
| 124 | abort(); |
| 125 | return 0; |
| 126 | } |
| 127 | }; // end class ELFCodeEmitter |
| 128 | |
| 129 | } // end namespace llvm |
| 130 | |
| 131 | #endif |
| 132 | |