1 //===-- llvm/CodeGen/MachineCodeEmitter.h - Code emission -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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.).
15 //===----------------------------------------------------------------------===//
17 #ifndef LLVM_CODEGEN_MACHINECODEEMITTER_H
18 #define LLVM_CODEGEN_MACHINECODEEMITTER_H
20 #include "llvm/Support/DataTypes.h"
25 class MachineBasicBlock;
26 class MachineConstantPool;
27 class MachineJumpTableInfo;
28 class MachineFunction;
29 class MachineRelocation;
34 class MachineCodeEmitter {
36 virtual ~MachineCodeEmitter() {}
38 /// startFunction - This callback is invoked when the specified function is
39 /// about to be code generated.
41 virtual void startFunction(MachineFunction &F) {}
43 /// finishFunction - This callback is invoked when the specified function has
44 /// finished code generation.
46 virtual void finishFunction(MachineFunction &F) {}
48 /// emitConstantPool - This callback is invoked to output the constant pool
50 virtual void emitConstantPool(MachineConstantPool *MCP) {}
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) {}
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) {}
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.
68 virtual void startFunctionStub(unsigned StubSize) {}
70 /// finishFunctionStub - This callback is invoked to terminate a function
73 virtual void *finishFunctionStub(const Function *F) { return 0; }
75 /// emitByte - This callback is invoked when a byte needs to be written to the
78 virtual void emitByte(unsigned char B) {}
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).
84 virtual void emitWordAt(unsigned W, unsigned *Ptr) {}
86 /// emitWord - This callback is invoked when a word needs to be written to the
89 virtual void emitWord(unsigned W) = 0;
91 /// getCurrentPCValue - This returns the address that the next emitted byte
92 /// will be output to.
94 virtual uint64_t getCurrentPCValue() = 0;
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;
101 /// addRelocation - Whenever a relocatable address is needed, it should be
102 /// noted with this interface.
103 virtual void addRelocation(const MachineRelocation &MR) = 0;
105 // getConstantPoolEntryAddress - Return the address of the 'Index' entry in
106 // the constant pool that was last emitted with the 'emitConstantPool' method.
108 virtual uint64_t getConstantPoolEntryAddress(unsigned Index) = 0;
110 // getJumpTablelEntryAddress - Return the address of the jump table with index
111 // 'Index' in the function that last called initJumpTableInfo.
113 virtual uint64_t getJumpTableEntryAddress(unsigned Index) = 0;
115 // allocateGlobal - Allocate some space for a global variable.
116 virtual unsigned char* allocateGlobal(unsigned size, unsigned alignment) = 0;
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.
122 static MachineCodeEmitter *createDebugEmitter();
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.
128 static MachineCodeEmitter *createFilePrinterEmitter(MachineCodeEmitter&);
131 } // End llvm namespace