1 //===-- llvm/CodeGen/ObjectCodeEmitter.cpp -------------------- -*- 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 #include "llvm/CodeGen/BinaryObject.h"
11 #include "llvm/CodeGen/MachineBasicBlock.h"
12 #include "llvm/CodeGen/MachineRelocation.h"
13 #include "llvm/CodeGen/ObjectCodeEmitter.h"
15 //===----------------------------------------------------------------------===//
16 // ObjectCodeEmitter Implementation
17 //===----------------------------------------------------------------------===//
21 ObjectCodeEmitter::ObjectCodeEmitter() : BO(0) {}
22 ObjectCodeEmitter::ObjectCodeEmitter(BinaryObject
*bo
) : BO(bo
) {}
23 ObjectCodeEmitter::~ObjectCodeEmitter() {}
25 /// setBinaryObject - set the BinaryObject we are writting to
26 void ObjectCodeEmitter::setBinaryObject(BinaryObject
*bo
) { BO
= bo
; }
28 /// emitByte - This callback is invoked when a byte needs to be
29 /// written to the data stream, without buffer overflow testing.
30 void ObjectCodeEmitter::emitByte(uint8_t B
) {
34 /// emitWordLE - This callback is invoked when a 32-bit word needs to be
35 /// written to the data stream in little-endian format.
36 void ObjectCodeEmitter::emitWordLE(uint32_t W
) {
40 /// emitWordBE - This callback is invoked when a 32-bit word needs to be
41 /// written to the data stream in big-endian format.
42 void ObjectCodeEmitter::emitWordBE(uint32_t W
) {
46 /// emitDWordLE - This callback is invoked when a 64-bit word needs to be
47 /// written to the data stream in little-endian format.
48 void ObjectCodeEmitter::emitDWordLE(uint64_t W
) {
52 /// emitDWordBE - This callback is invoked when a 64-bit word needs to be
53 /// written to the data stream in big-endian format.
54 void ObjectCodeEmitter::emitDWordBE(uint64_t W
) {
58 /// emitAlignment - Align 'BO' to the necessary alignment boundary.
59 void ObjectCodeEmitter::emitAlignment(unsigned Alignment
/* 0 */,
60 uint8_t fill
/* 0 */) {
61 BO
->emitAlignment(Alignment
, fill
);
64 /// emitULEB128Bytes - This callback is invoked when a ULEB128 needs to be
65 /// written to the data stream.
66 void ObjectCodeEmitter::emitULEB128Bytes(uint64_t Value
) {
67 BO
->emitULEB128Bytes(Value
);
70 /// emitSLEB128Bytes - This callback is invoked when a SLEB128 needs to be
71 /// written to the data stream.
72 void ObjectCodeEmitter::emitSLEB128Bytes(uint64_t Value
) {
73 BO
->emitSLEB128Bytes(Value
);
76 /// emitString - This callback is invoked when a String needs to be
77 /// written to the data stream.
78 void ObjectCodeEmitter::emitString(const std::string
&String
) {
79 BO
->emitString(String
);
82 /// getCurrentPCValue - This returns the address that the next emitted byte
83 /// will be output to.
84 uintptr_t ObjectCodeEmitter::getCurrentPCValue() const {
85 return BO
->getCurrentPCOffset();
88 /// getCurrentPCOffset - Return the offset from the start of the emitted
89 /// buffer that we are currently writing to.
90 uintptr_t ObjectCodeEmitter::getCurrentPCOffset() const {
91 return BO
->getCurrentPCOffset();
94 /// addRelocation - Whenever a relocatable address is needed, it should be
95 /// noted with this interface.
96 void ObjectCodeEmitter::addRelocation(const MachineRelocation
& relocation
) {
97 BO
->addRelocation(relocation
);
100 /// StartMachineBasicBlock - This should be called by the target when a new
101 /// basic block is about to be emitted. This way the MCE knows where the
102 /// start of the block is, and can implement getMachineBasicBlockAddress.
103 void ObjectCodeEmitter::StartMachineBasicBlock(MachineBasicBlock
*MBB
) {
104 if (MBBLocations
.size() <= (unsigned)MBB
->getNumber())
105 MBBLocations
.resize((MBB
->getNumber()+1)*2);
106 MBBLocations
[MBB
->getNumber()] = getCurrentPCOffset();
109 /// getMachineBasicBlockAddress - Return the address of the specified
110 /// MachineBasicBlock, only usable after the label for the MBB has been
113 ObjectCodeEmitter::getMachineBasicBlockAddress(MachineBasicBlock
*MBB
) const {
114 assert(MBBLocations
.size() > (unsigned)MBB
->getNumber() &&
115 MBBLocations
[MBB
->getNumber()] && "MBB not emitted!");
116 return MBBLocations
[MBB
->getNumber()];
119 /// getJumpTableEntryAddress - Return the address of the jump table with index
120 /// 'Index' in the function that last called initJumpTableInfo.
121 uintptr_t ObjectCodeEmitter::getJumpTableEntryAddress(unsigned Index
) const {
122 assert(JTLocations
.size() > Index
&& "JT not emitted!");
123 return JTLocations
[Index
];
126 /// getConstantPoolEntryAddress - Return the address of the 'Index' entry in
127 /// the constant pool that was last emitted with the emitConstantPool method.
128 uintptr_t ObjectCodeEmitter::getConstantPoolEntryAddress(unsigned Index
) const {
129 assert(CPLocations
.size() > Index
&& "CP not emitted!");
130 return CPLocations
[Index
];
133 /// getConstantPoolEntrySection - Return the section of the 'Index' entry in
134 /// the constant pool that was last emitted with the emitConstantPool method.
135 uintptr_t ObjectCodeEmitter::getConstantPoolEntrySection(unsigned Index
) const {
136 assert(CPSections
.size() > Index
&& "CP not emitted!");
137 return CPSections
[Index
];
140 } // end namespace llvm