Do not use typeinfo to identify pass in pass manager.
[oota-llvm.git] / include / llvm / CodeGen / MachineCodeEmitter.h
1 //===-- llvm/CodeGen/MachineCodeEmitter.h - Code emission -------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines an abstract interface that is used by the machine code
11 // emission framework to output the code.  This allows machine code emission to
12 // be separated from concerns such as resolution of call targets, and where the
13 // machine code will be written (memory or disk, f.e.).
14 //
15 //===----------------------------------------------------------------------===//
16
17 #ifndef LLVM_CODEGEN_MACHINECODEEMITTER_H
18 #define LLVM_CODEGEN_MACHINECODEEMITTER_H
19
20 #include "llvm/Support/DataTypes.h"
21 #include <vector>
22
23 namespace llvm {
24
25 class MachineBasicBlock;
26 class MachineConstantPool;
27 class MachineJumpTableInfo;
28 class MachineFunction;
29 class MachineRelocation;
30 class Value;
31 class GlobalValue;
32 class Function;
33
34 /// MachineCodeEmitter - This class defines two sorts of methods: those for
35 /// emitting the actual bytes of machine code, and those for emitting auxillary
36 /// structures, such as jump tables, relocations, etc.
37 ///
38 /// Emission of machine code is complicated by the fact that we don't (in
39 /// general) know the size of the machine code that we're about to emit before
40 /// we emit it.  As such, we preallocate a certain amount of memory, and set the
41 /// BufferBegin/BufferEnd pointers to the start and end of the buffer.  As we
42 /// emit machine instructions, we advance the CurBufferPtr to indicate the
43 /// location of the next byte to emit.  In the case of a buffer overflow (we
44 /// need to emit more machine code than we have allocated space for), the
45 /// CurBufferPtr will saturate to BufferEnd and ignore stores.  Once the entire
46 /// function has been emitted, the overflow condition is checked, and if it has
47 /// occurred, more memory is allocated, and we reemit the code into it.
48 /// 
49 class MachineCodeEmitter {
50 protected:
51   /// BufferBegin/BufferEnd - Pointers to the start and end of the memory
52   /// allocated for this code buffer.
53   unsigned char *BufferBegin, *BufferEnd;
54   
55   /// CurBufferPtr - Pointer to the next byte of memory to fill when emitting 
56   /// code.  This is guranteed to be in the range [BufferBegin,BufferEnd].  If
57   /// this pointer is at BufferEnd, it will never move due to code emission, and
58   /// all code emission requests will be ignored (this is the buffer overflow
59   /// condition).
60   unsigned char *CurBufferPtr;
61 public:
62   virtual ~MachineCodeEmitter() {}
63
64   /// startFunction - This callback is invoked when the specified function is
65   /// about to be code generated.  This initializes the BufferBegin/End/Ptr
66   /// fields.
67   ///
68   virtual void startFunction(MachineFunction &F) = 0;
69
70   /// finishFunction - This callback is invoked when the specified function has
71   /// finished code generation.  If a buffer overflow has occurred, this method
72   /// returns true (the callee is required to try again), otherwise it returns
73   /// false.
74   ///
75   virtual bool finishFunction(MachineFunction &F) = 0;
76   
77   /// startFunctionStub - This callback is invoked when the JIT needs the
78   /// address of a function that has not been code generated yet.  The StubSize
79   /// specifies the total size required by the stub.  Stubs are not allowed to
80   /// have constant pools, the can only use the other emitByte*/emitWord*
81   /// methods.
82   ///
83   virtual void startFunctionStub(unsigned StubSize, unsigned Alignment = 1) = 0;
84
85   /// finishFunctionStub - This callback is invoked to terminate a function
86   /// stub.
87   ///
88   virtual void *finishFunctionStub(const Function *F) = 0;
89
90   /// emitByte - This callback is invoked when a byte needs to be written to the
91   /// output stream.
92   ///
93   void emitByte(unsigned char B) {
94     if (CurBufferPtr != BufferEnd)
95       *CurBufferPtr++ = B;
96   }
97
98   /// emitWordLE - This callback is invoked when a 32-bit word needs to be
99   /// written to the output stream in little-endian format.
100   ///
101   void emitWordLE(unsigned W) {
102     if (CurBufferPtr+4 <= BufferEnd) {
103       *CurBufferPtr++ = (unsigned char)(W >>  0);
104       *CurBufferPtr++ = (unsigned char)(W >>  8);
105       *CurBufferPtr++ = (unsigned char)(W >> 16);
106       *CurBufferPtr++ = (unsigned char)(W >> 24);
107     } else {
108       CurBufferPtr = BufferEnd;
109     }
110   }
111   
112   /// emitWordBE - This callback is invoked when a 32-bit word needs to be
113   /// written to the output stream in big-endian format.
114   ///
115   void emitWordBE(unsigned W) {
116     if (CurBufferPtr+4 <= BufferEnd) {
117       *CurBufferPtr++ = (unsigned char)(W >> 24);
118       *CurBufferPtr++ = (unsigned char)(W >> 16);
119       *CurBufferPtr++ = (unsigned char)(W >>  8);
120       *CurBufferPtr++ = (unsigned char)(W >>  0);
121     } else {
122       CurBufferPtr = BufferEnd;
123     }
124   }
125
126   /// emitAlignment - Move the CurBufferPtr pointer up the the specified
127   /// alignment (saturated to BufferEnd of course).
128   void emitAlignment(unsigned Alignment) {
129     if (Alignment == 0) Alignment = 1;
130     // Move the current buffer ptr up to the specified alignment.
131     CurBufferPtr =
132       (unsigned char*)(((intptr_t)CurBufferPtr+Alignment-1) &
133                        ~(intptr_t)(Alignment-1));
134     if (CurBufferPtr > BufferEnd)
135       CurBufferPtr = BufferEnd;
136   }
137   
138   /// allocateSpace - Allocate a block of space in the current output buffer,
139   /// returning null (and setting conditions to indicate buffer overflow) on
140   /// failure.  Alignment is the alignment in bytes of the buffer desired.
141   void *allocateSpace(intptr_t Size, unsigned Alignment) {
142     emitAlignment(Alignment);
143     void *Result = CurBufferPtr;
144     
145     // Allocate the space.
146     CurBufferPtr += Size;
147     
148     // Check for buffer overflow.
149     if (CurBufferPtr >= BufferEnd) {
150       CurBufferPtr = BufferEnd;
151       Result = 0;
152     }
153     return Result;
154   }
155
156   /// StartMachineBasicBlock - This should be called by the target when a new
157   /// basic block is about to be emitted.  This way the MCE knows where the
158   /// start of the block is, and can implement getMachineBasicBlockAddress.
159   virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) = 0;
160   
161   /// getCurrentPCValue - This returns the address that the next emitted byte
162   /// will be output to.
163   ///
164   virtual intptr_t getCurrentPCValue() const {
165     return (intptr_t)CurBufferPtr;
166   }
167
168   /// getCurrentPCOffset - Return the offset from the start of the emitted
169   /// buffer that we are currently writing to.
170   intptr_t getCurrentPCOffset() const {
171     return CurBufferPtr-BufferBegin;
172   }
173
174   /// addRelocation - Whenever a relocatable address is needed, it should be
175   /// noted with this interface.
176   virtual void addRelocation(const MachineRelocation &MR) = 0;
177
178   
179   /// FIXME: These should all be handled with relocations!
180   
181   /// getConstantPoolEntryAddress - Return the address of the 'Index' entry in
182   /// the constant pool that was last emitted with the emitConstantPool method.
183   ///
184   virtual intptr_t getConstantPoolEntryAddress(unsigned Index) const = 0;
185
186   /// getJumpTableEntryAddress - Return the address of the jump table with index
187   /// 'Index' in the function that last called initJumpTableInfo.
188   ///
189   virtual intptr_t getJumpTableEntryAddress(unsigned Index) const = 0;
190   
191   /// getMachineBasicBlockAddress - Return the address of the specified
192   /// MachineBasicBlock, only usable after the label for the MBB has been
193   /// emitted.
194   ///
195   virtual intptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const= 0;
196 };
197
198 } // End llvm namespace
199
200 #endif