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