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"
24 class MachineBasicBlock;
25 class MachineConstantPool;
26 class MachineFunction;
27 class MachineRelocation;
32 class MachineCodeEmitter {
34 virtual ~MachineCodeEmitter() {}
36 /// startFunction - This callback is invoked when the specified function is
37 /// about to be code generated.
39 virtual void startFunction(MachineFunction &F) {}
41 /// finishFunction - This callback is invoked when the specified function has
42 /// finished code generation.
44 virtual void finishFunction(MachineFunction &F) {}
46 /// emitConstantPool - This callback is invoked to output the constant pool
48 virtual void emitConstantPool(MachineConstantPool *MCP) {}
50 /// startFunctionStub - This callback is invoked when the JIT needs the
51 /// address of a function that has not been code generated yet. The StubSize
52 /// specifies the total size required by the stub. Stubs are not allowed to
53 /// have constant pools, the can only use the other emit* methods.
55 virtual void startFunctionStub(unsigned StubSize) {}
57 /// finishFunctionStub - This callback is invoked to terminate a function
60 virtual void *finishFunctionStub(const Function *F) { return 0; }
62 /// emitByte - This callback is invoked when a byte needs to be written to the
65 virtual void emitByte(unsigned char B) {}
67 /// emitWordAt - This callback is invoked when a word needs to be written to
68 /// the output stream at a different position than the current PC (for
69 /// instance, when performing relocations).
71 virtual void emitWordAt(unsigned W, unsigned *Ptr) {}
73 /// emitWord - This callback is invoked when a word needs to be written to the
76 virtual void emitWord(unsigned W) = 0;
78 /// getCurrentPCValue - This returns the address that the next emitted byte
79 /// will be output to.
81 virtual uint64_t getCurrentPCValue() = 0;
84 /// getCurrentPCOffset - Return the offset from the start of the emitted
85 /// buffer that we are currently writing to.
86 virtual uint64_t getCurrentPCOffset() = 0;
88 /// addRelocation - Whenever a relocatable address is needed, it should be
89 /// noted with this interface.
90 virtual void addRelocation(const MachineRelocation &MR) = 0;
92 /// getGlobalValueAddress - This method is used to get the address of the
93 /// specified global value. In some cases, however, the address may not yet
94 /// be known at the point that the method is called (for example, getting the
95 /// address of a function which has not yet been code generated). If this is
96 /// the case, the function returns zero, and the callee has to be able to
97 /// handle the situation.
99 virtual uint64_t getGlobalValueAddress(GlobalValue *V) = 0;
100 virtual uint64_t getGlobalValueAddress(const char *SymName) = 0;
102 // getConstantPoolEntryAddress - Return the address of the 'Index' entry in
103 // the constant pool that was last emitted with the 'emitConstantPool' method.
105 virtual uint64_t getConstantPoolEntryAddress(unsigned Index) = 0;
107 // forceCompilationOf - Force the compilation of the specified function, and
108 // return its address, because we REALLY need the address now.
110 // FIXME: This is JIT specific!
112 virtual uint64_t forceCompilationOf(Function *F) = 0;
114 /// createDebugEmitter - Return a dynamically allocated machine
115 /// code emitter, which just prints the opcodes and fields out the cout. This
116 /// can be used for debugging users of the MachineCodeEmitter interface.
118 static MachineCodeEmitter *createDebugEmitter();
120 /// createFilePrinterEmitter - Return a dynamically allocated
121 /// machine code emitter, which prints binary code to a file. This
122 /// can be used for debugging users of the MachineCodeEmitter interface.
124 static MachineCodeEmitter *createFilePrinterEmitter(MachineCodeEmitter&);
127 } // End llvm namespace