47ccef5317a04668f120b75ab3d901772d216927
[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   /// emitWord - This callback is invoked when a word needs to be written to the
81   /// output stream.
82   ///
83   virtual void emitWord(unsigned W) = 0;
84
85   /// getCurrentPCValue - This returns the address that the next emitted byte
86   /// will be output to.
87   ///
88   virtual uint64_t getCurrentPCValue() = 0;
89
90
91   /// getCurrentPCOffset - Return the offset from the start of the emitted
92   /// buffer that we are currently writing to.
93   virtual uint64_t getCurrentPCOffset() = 0;
94
95   /// addRelocation - Whenever a relocatable address is needed, it should be
96   /// noted with this interface.
97   virtual void addRelocation(const MachineRelocation &MR) = 0;
98
99   /// getConstantPoolEntryAddress - Return the address of the 'Index' entry in
100   /// the constant pool that was last emitted with the emitConstantPool method.
101   ///
102   virtual uint64_t getConstantPoolEntryAddress(unsigned Index) = 0;
103
104   /// getJumpTableEntryAddress - Return the address of the jump table with index
105   /// 'Index' in the function that last called initJumpTableInfo.
106   ///
107   virtual uint64_t getJumpTableEntryAddress(unsigned Index) = 0;
108   
109   // allocateGlobal - Allocate some space for a global variable.
110   virtual unsigned char* allocateGlobal(unsigned size, unsigned alignment) = 0;
111
112   /// createFilePrinterEmitter - Return a dynamically allocated
113   /// machine code emitter, which prints binary code to a file.  This
114   /// can be used for debugging users of the MachineCodeEmitter interface.
115   ///
116   static MachineCodeEmitter *createFilePrinterEmitter(MachineCodeEmitter&);
117 };
118
119 } // End llvm namespace
120
121 #endif