0a6e4e4d19ec3871d7c22329c40858e87deb4e3c
[oota-llvm.git] / lib / CodeGen / MachOCodeEmitter.h
1 //===-- MachOEmitter.h - Target-independent Mach-O Emitter class ----------===//
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 MACHOCODEEMITTER_H
11 #define MACHOCODEEMITTER_H
12
13 #include "MachOWriter.h"
14 #include "llvm/CodeGen/MachineCodeEmitter.h"
15 #include <vector>
16
17 namespace llvm {
18
19 /// MachOCodeEmitter - This class is used by the MachOWriter to emit the code 
20 /// for functions to the Mach-O file.
21
22 class MachOCodeEmitter : public MachineCodeEmitter {
23   MachOWriter &MOW;
24
25   /// Target machine description.
26   TargetMachine &TM;
27
28   /// is64Bit/isLittleEndian - This information is inferred from the target
29   /// machine directly, indicating what header values and flags to set.
30   bool is64Bit, isLittleEndian;
31
32   const TargetAsmInfo *TAI;
33
34   /// Relocations - These are the relocations that the function needs, as
35   /// emitted.
36   std::vector<MachineRelocation> Relocations;
37   
38   /// CPLocations - This is a map of constant pool indices to offsets from the
39   /// start of the section for that constant pool index.
40   std::vector<uintptr_t> CPLocations;
41
42   /// CPSections - This is a map of constant pool indices to the MachOSection
43   /// containing the constant pool entry for that index.
44   std::vector<unsigned> CPSections;
45
46   /// JTLocations - This is a map of jump table indices to offsets from the
47   /// start of the section for that jump table index.
48   std::vector<uintptr_t> JTLocations;
49
50   /// MBBLocations - This vector is a mapping from MBB ID's to their address.
51   /// It is filled in by the StartMachineBasicBlock callback and queried by
52   /// the getMachineBasicBlockAddress callback.
53   std::vector<uintptr_t> MBBLocations;
54   
55 public:
56   MachOCodeEmitter(MachOWriter &mow) : MOW(mow), TM(MOW.TM)
57   {
58     is64Bit = TM.getTargetData()->getPointerSizeInBits() == 64;
59     isLittleEndian = TM.getTargetData()->isLittleEndian();
60     TAI = TM.getTargetAsmInfo();  
61   }
62
63   virtual void startFunction(MachineFunction &MF);
64   virtual bool finishFunction(MachineFunction &MF);
65
66   virtual void addRelocation(const MachineRelocation &MR) {
67     Relocations.push_back(MR);
68   }
69   
70   void emitConstantPool(MachineConstantPool *MCP);
71   void emitJumpTables(MachineJumpTableInfo *MJTI);
72   
73   virtual uintptr_t getConstantPoolEntryAddress(unsigned Index) const {
74     assert(CPLocations.size() > Index && "CP not emitted!");
75     return CPLocations[Index];
76   }
77   virtual uintptr_t getJumpTableEntryAddress(unsigned Index) const {
78     assert(JTLocations.size() > Index && "JT not emitted!");
79     return JTLocations[Index];
80   }
81
82   virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) {
83     if (MBBLocations.size() <= (unsigned)MBB->getNumber())
84       MBBLocations.resize((MBB->getNumber()+1)*2);
85     MBBLocations[MBB->getNumber()] = getCurrentPCOffset();
86   }
87
88   virtual uintptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const {
89     assert(MBBLocations.size() > (unsigned)MBB->getNumber() && 
90            MBBLocations[MBB->getNumber()] && "MBB not emitted!");
91     return MBBLocations[MBB->getNumber()];
92   }
93
94   virtual uintptr_t getLabelAddress(uint64_t Label) const {
95     assert(0 && "get Label not implemented");
96     abort();
97     return 0;
98   }
99
100   virtual void emitLabel(uint64_t LabelID) {
101     assert(0 && "emit Label not implemented");
102     abort();
103   }
104
105   virtual void setModuleInfo(llvm::MachineModuleInfo* MMI) { }
106
107   /// JIT SPECIFIC FUNCTIONS - DO NOT IMPLEMENT THESE HERE!
108   virtual void startGVStub(const GlobalValue* F, unsigned StubSize,
109                            unsigned Alignment = 1) {
110     assert(0 && "JIT specific function called!");
111     abort();
112   }
113   virtual void startGVStub(const GlobalValue* F, void *Buffer, 
114                            unsigned StubSize) {
115     assert(0 && "JIT specific function called!");
116     abort();
117   }
118   virtual void *finishGVStub(const GlobalValue* F) {
119     assert(0 && "JIT specific function called!");
120     abort();
121     return 0;
122   }
123
124 }; // end class MachOCodeEmitter
125
126 } // end namespace llvm
127
128 #endif
129