982aebf8fcc022ce1ec172c50591be8026539fd0
[oota-llvm.git] / lib / CodeGen / ELFCodeEmitter.h
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
13 #include "llvm/CodeGen/MachineCodeEmitter.h"
14 #include <vector>
15
16 namespace llvm {
17   class ELFWriter;
18   class ELFSection;
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;
24
25     /// Target machine description
26     TargetMachine &TM;
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
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
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
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
54     uint8_t *FnStartPtr;
55
56     /// JumpTableSectionIdx - Holds the index of the Jump Table Section 
57     unsigned JumpTableSectionIdx;
58   public:
59     explicit ELFCodeEmitter(ELFWriter &ew) : EW(ew), TM(EW.TM),
60                                              JumpTableSectionIdx(0) {}
61
62     void startFunction(MachineFunction &F);
63     bool finishFunction(MachineFunction &F);
64
65     void addRelocation(const MachineRelocation &MR) {
66       Relocations.push_back(MR);
67     }
68
69     virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) {
70       if (MBBLocations.size() <= (unsigned)MBB->getNumber())
71         MBBLocations.resize((MBB->getNumber()+1)*2);
72       MBBLocations[MBB->getNumber()] = getCurrentPCOffset();
73     }
74
75     virtual uintptr_t getConstantPoolEntryAddress(unsigned Index) const {
76       assert(CPLocations.size() > Index && "CP not emitted!");
77       return CPLocations[Index];
78     }
79
80     virtual uintptr_t getJumpTableEntryAddress(unsigned Index) const {
81       assert(JTLocations.size() > Index && "JT not emitted!");
82       return JTLocations[Index];
83     }
84
85     virtual uintptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const {
86       assert(MBBLocations.size() > (unsigned)MBB->getNumber() && 
87              MBBLocations[MBB->getNumber()] && "MBB not emitted!");
88       return MBBLocations[MBB->getNumber()];
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
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
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) {}
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