Code cleanup associated with jump tables, thanks to Chris for noticing
[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 <map>
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 class MachineCodeEmitter {
35 public:
36   virtual ~MachineCodeEmitter() {}
37
38   /// startFunction - This callback is invoked when the specified function is
39   /// about to be code generated.
40   ///
41   virtual void startFunction(MachineFunction &F) {}
42
43   /// finishFunction - This callback is invoked when the specified function has
44   /// finished code generation.
45   ///
46   virtual void finishFunction(MachineFunction &F) {}
47
48   /// emitConstantPool - This callback is invoked to output the constant pool
49   /// for the function.
50   virtual void emitConstantPool(MachineConstantPool *MCP) {}
51
52   /// initJumpTableInfo - This callback is invoked by the JIT to allocate the
53   /// necessary memory to hold the jump tables.
54   virtual void initJumpTableInfo(MachineJumpTableInfo *MJTI) {}
55   
56   /// emitJumpTableInfo - This callback is invoked to output the jump tables
57   /// for the function.  In addition to a pointer to the MachineJumpTableInfo,
58   /// this function also takes a map of MBBs to addresses, so that the final
59   /// addresses of the MBBs can be written to the jump tables.
60   virtual void emitJumpTableInfo(MachineJumpTableInfo *MJTI,
61                                  std::map<MachineBasicBlock*,uint64_t> &MBBM) {}
62   
63   /// startFunctionStub - This callback is invoked when the JIT needs the
64   /// address of a function that has not been code generated yet.  The StubSize
65   /// specifies the total size required by the stub.  Stubs are not allowed to
66   /// have constant pools, the can only use the other emit* methods.
67   ///
68   virtual void startFunctionStub(unsigned StubSize) {}
69
70   /// finishFunctionStub - This callback is invoked to terminate a function
71   /// stub.
72   ///
73   virtual void *finishFunctionStub(const Function *F) { return 0; }
74
75   /// emitByte - This callback is invoked when a byte needs to be written to the
76   /// output stream.
77   ///
78   virtual void emitByte(unsigned char B) {}
79
80   /// emitWordAt - This callback is invoked when a word needs to be written to
81   /// the output stream at a different position than the current PC (for
82   /// instance, when performing relocations).
83   ///
84   virtual void emitWordAt(unsigned W, unsigned *Ptr) {}
85
86   /// emitWord - This callback is invoked when a word needs to be written to the
87   /// output stream.
88   ///
89   virtual void emitWord(unsigned W) = 0;
90
91   /// getCurrentPCValue - This returns the address that the next emitted byte
92   /// will be output to.
93   ///
94   virtual uint64_t getCurrentPCValue() = 0;
95
96
97   /// getCurrentPCOffset - Return the offset from the start of the emitted
98   /// buffer that we are currently writing to.
99   virtual uint64_t getCurrentPCOffset() = 0;
100
101   /// addRelocation - Whenever a relocatable address is needed, it should be
102   /// noted with this interface.
103   virtual void addRelocation(const MachineRelocation &MR) = 0;
104
105   /// getConstantPoolEntryAddress - Return the address of the 'Index' entry in
106   /// the constant pool that was last emitted with the emitConstantPool method.
107   ///
108   virtual uint64_t getConstantPoolEntryAddress(unsigned Index) = 0;
109
110   /// getJumpTableEntryAddress - Return the address of the jump table with index
111   /// 'Index' in the function that last called initJumpTableInfo.
112   ///
113   virtual uint64_t getJumpTableEntryAddress(unsigned Index) = 0;
114   
115   // allocateGlobal - Allocate some space for a global variable.
116   virtual unsigned char* allocateGlobal(unsigned size, unsigned alignment) = 0;
117
118   /// createDebugEmitter - Return a dynamically allocated machine
119   /// code emitter, which just prints the opcodes and fields out the cout.  This
120   /// can be used for debugging users of the MachineCodeEmitter interface.
121   ///
122   static MachineCodeEmitter *createDebugEmitter();
123
124   /// createFilePrinterEmitter - Return a dynamically allocated
125   /// machine code emitter, which prints binary code to a file.  This
126   /// can be used for debugging users of the MachineCodeEmitter interface.
127   ///
128   static MachineCodeEmitter *createFilePrinterEmitter(MachineCodeEmitter&);
129 };
130
131 } // End llvm namespace
132
133 #endif