1 //===-- llvm/CodeGen/ObjectCodeEmitter.h - Object Code Emitter -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Generalized Object Code Emitter, works with ObjectModule and BinaryObject.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CODEGEN_OBJECTCODEEMITTER_H
15 #define LLVM_CODEGEN_OBJECTCODEEMITTER_H
17 #include "llvm/CodeGen/MachineCodeEmitter.h"
22 class MachineBasicBlock;
23 class MachineCodeEmitter;
24 class MachineFunction;
25 class MachineConstantPool;
26 class MachineJumpTableInfo;
27 class MachineModuleInfo;
29 class ObjectCodeEmitter : public MachineCodeEmitter {
32 /// Binary Object (Section or Segment) we are emitting to.
35 /// MBBLocations - This vector is a mapping from MBB ID's to their address.
36 /// It is filled in by the StartMachineBasicBlock callback and queried by
37 /// the getMachineBasicBlockAddress callback.
38 std::vector<uintptr_t> MBBLocations;
40 /// LabelLocations - This vector is a mapping from Label ID's to their
42 std::vector<uintptr_t> LabelLocations;
44 /// CPLocations - This is a map of constant pool indices to offsets from the
45 /// start of the section for that constant pool index.
46 std::vector<uintptr_t> CPLocations;
48 /// CPSections - This is a map of constant pool indices to the Section
49 /// containing the constant pool entry for that index.
50 std::vector<uintptr_t> CPSections;
52 /// JTLocations - This is a map of jump table indices to offsets from the
53 /// start of the section for that jump table index.
54 std::vector<uintptr_t> JTLocations;
58 ObjectCodeEmitter(BinaryObject *bo);
59 virtual ~ObjectCodeEmitter();
61 /// setBinaryObject - set the BinaryObject we are writting to
62 void setBinaryObject(BinaryObject *bo);
64 /// emitByte - This callback is invoked when a byte needs to be
65 /// written to the data stream, without buffer overflow testing.
66 void emitByte(uint8_t B);
68 /// emitWordLE - This callback is invoked when a 32-bit word needs to be
69 /// written to the data stream in little-endian format.
70 void emitWordLE(uint32_t W);
72 /// emitWordBE - This callback is invoked when a 32-bit word needs to be
73 /// written to the data stream in big-endian format.
74 void emitWordBE(uint32_t W);
76 /// emitDWordLE - This callback is invoked when a 64-bit word needs to be
77 /// written to the data stream in little-endian format.
78 void emitDWordLE(uint64_t W);
80 /// emitDWordBE - This callback is invoked when a 64-bit word needs to be
81 /// written to the data stream in big-endian format.
82 void emitDWordBE(uint64_t W);
84 /// emitAlignment - Move the CurBufferPtr pointer up to the specified
85 /// alignment (saturated to BufferEnd of course).
86 void emitAlignment(unsigned Alignment = 0, uint8_t fill = 0);
88 /// emitULEB128Bytes - This callback is invoked when a ULEB128 needs to be
89 /// written to the data stream.
90 void emitULEB128Bytes(uint64_t Value);
92 /// emitSLEB128Bytes - This callback is invoked when a SLEB128 needs to be
93 /// written to the data stream.
94 void emitSLEB128Bytes(uint64_t Value);
96 /// emitString - This callback is invoked when a String needs to be
97 /// written to the data stream.
98 void emitString(const std::string &String);
100 /// getCurrentPCValue - This returns the address that the next emitted byte
101 /// will be output to.
102 uintptr_t getCurrentPCValue() const;
104 /// getCurrentPCOffset - Return the offset from the start of the emitted
105 /// buffer that we are currently writing to.
106 uintptr_t getCurrentPCOffset() const;
108 /// addRelocation - Whenever a relocatable address is needed, it should be
109 /// noted with this interface.
110 void addRelocation(const MachineRelocation& relocation);
112 /// earlyResolveAddresses - True if the code emitter can use symbol addresses
113 /// during code emission time. The JIT is capable of doing this because it
114 /// creates jump tables or constant pools in memory on the fly while the
115 /// object code emitters rely on a linker to have real addresses and should
116 /// use relocations instead.
117 bool earlyResolveAddresses() const { return false; }
119 /// startFunction - This callback is invoked when the specified function is
120 /// about to be code generated. This initializes the BufferBegin/End/Ptr
122 virtual void startFunction(MachineFunction &F) = 0;
124 /// finishFunction - This callback is invoked when the specified function has
125 /// finished code generation. If a buffer overflow has occurred, this method
126 /// returns true (the callee is required to try again), otherwise it returns
128 virtual bool finishFunction(MachineFunction &F) = 0;
130 /// StartMachineBasicBlock - This should be called by the target when a new
131 /// basic block is about to be emitted. This way the MCE knows where the
132 /// start of the block is, and can implement getMachineBasicBlockAddress.
133 virtual void StartMachineBasicBlock(MachineBasicBlock *MBB);
135 /// getMachineBasicBlockAddress - Return the address of the specified
136 /// MachineBasicBlock, only usable after the label for the MBB has been
138 virtual uintptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const;
140 /// emitJumpTables - Emit all the jump tables for a given jump table info
141 /// record to the appropriate section.
142 virtual void emitJumpTables(MachineJumpTableInfo *MJTI) = 0;
144 /// getJumpTableEntryAddress - Return the address of the jump table with index
145 /// 'Index' in the function that last called initJumpTableInfo.
146 virtual uintptr_t getJumpTableEntryAddress(unsigned Index) const;
148 /// emitConstantPool - For each constant pool entry, figure out which section
149 /// the constant should live in, allocate space for it, and emit it to the
150 /// Section data buffer.
151 virtual void emitConstantPool(MachineConstantPool *MCP) = 0;
153 /// getConstantPoolEntryAddress - Return the address of the 'Index' entry in
154 /// the constant pool that was last emitted with the emitConstantPool method.
155 virtual uintptr_t getConstantPoolEntryAddress(unsigned Index) const;
157 /// getConstantPoolEntrySection - Return the section of the 'Index' entry in
158 /// the constant pool that was last emitted with the emitConstantPool method.
159 virtual uintptr_t getConstantPoolEntrySection(unsigned Index) const;
161 /// Specifies the MachineModuleInfo object. This is used for exception handling
163 virtual void setModuleInfo(MachineModuleInfo* Info) = 0;
164 // to be implemented or depreciated with MachineModuleInfo
166 }; // end class ObjectCodeEmitter
168 } // end namespace llvm