llvm.org GIT mirror llvm / ac57e6e
Add the Object Code Emitter class. Original patch by Aaron Gray, I did some cleanup, removed some #includes and moved Object Code Emitter out-of-line. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@74813 91177308-0d34-0410-b5e6-96231b3b80d8 Bruno Cardoso Lopes 11 years ago
29 changed file(s) with 443 addition(s) and 328 deletion(s). Raw diff Collapse all Expand all
145145 /// emitDWordLE - This callback is invoked when a 64-bit word needs to be
146146 /// written to the data stream in little-endian format.
147147 inline void emitDWordLE(uint64_t W) {
148 Data.push_back(unsigned(W >> 0) & 255);
149 Data.push_back(unsigned(W >> 8) & 255);
150 Data.push_back(unsigned(W >> 16) & 255);
151 Data.push_back(unsigned(W >> 24) & 255);
152 Data.push_back(unsigned(W >> 32) & 255);
153 Data.push_back(unsigned(W >> 40) & 255);
154 Data.push_back(unsigned(W >> 48) & 255);
155 Data.push_back(unsigned(W >> 56) & 255);
148 Data.push_back((W >> 0) & 255);
149 Data.push_back((W >> 8) & 255);
150 Data.push_back((W >> 16) & 255);
151 Data.push_back((W >> 24) & 255);
152 Data.push_back((W >> 32) & 255);
153 Data.push_back((W >> 40) & 255);
154 Data.push_back((W >> 48) & 255);
155 Data.push_back((W >> 56) & 255);
156156 }
157157
158158 /// emitDWordBE - This callback is invoked when a 64-bit word needs to be
159159 /// written to the data stream in big-endian format.
160160 inline void emitDWordBE(uint64_t W) {
161 Data.push_back(unsigned(W >> 56) & 255);
162 Data.push_back(unsigned(W >> 48) & 255);
163 Data.push_back(unsigned(W >> 40) & 255);
164 Data.push_back(unsigned(W >> 32) & 255);
165 Data.push_back(unsigned(W >> 24) & 255);
166 Data.push_back(unsigned(W >> 16) & 255);
167 Data.push_back(unsigned(W >> 8) & 255);
168 Data.push_back(unsigned(W >> 0) & 255);
161 Data.push_back((W >> 56) & 255);
162 Data.push_back((W >> 48) & 255);
163 Data.push_back((W >> 40) & 255);
164 Data.push_back((W >> 32) & 255);
165 Data.push_back((W >> 24) & 255);
166 Data.push_back((W >> 16) & 255);
167 Data.push_back((W >> 8) & 255);
168 Data.push_back((W >> 0) & 255);
169169 }
170170
171171 /// fixByte - This callback is invoked when a byte needs to be
269269 }
270270
271271 /// emitAlignment - Pad the data to the specified alignment.
272 void emitAlignment(unsigned Alignment) {
272 void emitAlignment(unsigned Alignment, uint8_t fill = 0) {
273273 if (Alignment <= 1) return;
274274 unsigned PadSize = -Data.size() & (Alignment-1);
275275 for (unsigned i = 0; i
276 Data.push_back(0);
276 Data.push_back(fill);
277277 }
278278
279279 /// emitULEB128Bytes - This callback is invoked when a ULEB128 needs to be
1616 namespace llvm {
1717
1818 class PassManagerBase;
19 class MachineCodeEmitter;
19 class ObjectCodeEmitter;
2020 class TargetMachine;
2121 class raw_ostream;
2222
23 MachineCodeEmitter *AddELFWriter(PassManagerBase &FPM, raw_ostream &O,
24 TargetMachine &TM);
25 MachineCodeEmitter *AddMachOWriter(PassManagerBase &FPM, raw_ostream &O,
26 TargetMachine &TM);
23 ObjectCodeEmitter *AddELFWriter(PassManagerBase &FPM, raw_ostream &O,
24 TargetMachine &TM);
25 ObjectCodeEmitter *AddMachOWriter(PassManagerBase &FPM, raw_ostream &O,
26 TargetMachine &TM);
2727
2828 } // end llvm namespace
2929
7373 /// false.
7474 ///
7575 virtual bool finishFunction(MachineFunction &F) = 0;
76
77 /// startGVStub - This callback is invoked when the JIT needs the
78 /// address of a GV (e.g. function) that has not been code generated yet.
79 /// The StubSize specifies the total size required by the stub.
80 ///
81 virtual void startGVStub(const GlobalValue* GV, unsigned StubSize,
82 unsigned Alignment = 1) = 0;
83
84 /// startGVStub - This callback is invoked when the JIT needs the address of a
85 /// GV (e.g. function) that has not been code generated yet. Buffer points to
86 /// memory already allocated for this stub.
87 ///
88 virtual void startGVStub(const GlobalValue* GV, void *Buffer,
89 unsigned StubSize) = 0;
90
91 /// finishGVStub - This callback is invoked to terminate a GV stub.
92 ///
93 virtual void *finishGVStub(const GlobalValue* F) = 0;
9476
9577 /// emitByte - This callback is invoked when a byte needs to be written to the
9678 /// output stream.
287269
288270 /// getCurrentPCOffset - Return the offset from the start of the emitted
289271 /// buffer that we are currently writing to.
290 uintptr_t getCurrentPCOffset() const {
272 virtual uintptr_t getCurrentPCOffset() const {
291273 return CurBufferPtr-BufferBegin;
292274 }
293275
294276 /// addRelocation - Whenever a relocatable address is needed, it should be
295277 /// noted with this interface.
296278 virtual void addRelocation(const MachineRelocation &MR) = 0;
297
298279
299280 /// FIXME: These should all be handled with relocations!
300281
2828 class TargetFrameInfo;
2929 class MachineCodeEmitter;
3030 class JITCodeEmitter;
31 class ObjectCodeEmitter;
3132 class TargetRegisterInfo;
3233 class Module;
3334 class PassManagerBase;
256257 return true;
257258 }
258259
260 /// addPassesToEmitFileFinish - If the passes to emit the specified file had
261 /// to be split up (e.g., to add an object writer pass), this method can be
262 /// used to finish up adding passes to emit the file, if necessary.
263 ///
264 virtual bool addPassesToEmitFileFinish(PassManagerBase &,
265 ObjectCodeEmitter *,
266 CodeGenOpt::Level) {
267 return true;
268 }
269
259270 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
260271 /// get machine code emitted. This uses a MachineCodeEmitter object to handle
261272 /// actually outputting the machine code and resolving things like the address
334345 /// used to finish up adding passes to emit the file, if necessary.
335346 ///
336347 virtual bool addPassesToEmitFileFinish(PassManagerBase &PM,
337 JITCodeEmitter *MCE,
348 JITCodeEmitter *JCE,
349 CodeGenOpt::Level);
350
351 /// addPassesToEmitFileFinish - If the passes to emit the specified file had
352 /// to be split up (e.g., to add an object writer pass), this method can be
353 /// used to finish up adding passes to emit the file, if necessary.
354 ///
355 virtual bool addPassesToEmitFileFinish(PassManagerBase &PM,
356 ObjectCodeEmitter *OCE,
338357 CodeGenOpt::Level);
339358
340359 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
431450 return true;
432451 }
433452
453 /// addSimpleCodeEmitter - This pass should be overridden by the target to add
454 /// a code emitter (without setting flags), if supported. If this is not
455 /// supported, 'true' should be returned. If DumpAsm is true, the generated
456 /// assembly is printed to cerr.
457 virtual bool addSimpleCodeEmitter(PassManagerBase &, CodeGenOpt::Level,
458 bool /*DumpAsm*/, ObjectCodeEmitter &) {
459 return true;
460 }
461
434462 /// getEnableTailMergeDefault - the default setting for -enable-tail-merge
435463 /// on this target. User flag overrides.
436464 virtual bool getEnableTailMergeDefault() const { return true; }
3939 #include "llvm/CodeGen/BinaryObject.h"
4040 #include "llvm/CodeGen/FileWriters.h"
4141 #include "llvm/CodeGen/MachineCodeEmitter.h"
42 #include "llvm/CodeGen/ObjectCodeEmitter.h"
43 #include "llvm/CodeGen/MachineCodeEmitter.h"
4244 #include "llvm/CodeGen/MachineConstantPool.h"
4345 #include "llvm/Target/TargetAsmInfo.h"
4446 #include "llvm/Target/TargetData.h"
5355 char ELFWriter::ID = 0;
5456 /// AddELFWriter - Concrete function to add the ELF writer to the function pass
5557 /// manager.
56 MachineCodeEmitter *llvm::AddELFWriter(PassManagerBase &PM,
58 ObjectCodeEmitter *llvm::AddELFWriter(PassManagerBase &PM,
5759 raw_ostream &O,
5860 TargetMachine &TM) {
5961 ELFWriter *EW = new ELFWriter(O, TM);
6062 PM.add(EW);
61 return &EW->getMachineCodeEmitter();
63 return (ObjectCodeEmitter*) &EW->getMachineCodeEmitter();
6264 }
6365
6466 //===----------------------------------------------------------------------===//
125125 return false; // success!
126126 }
127127
128 /// addPassesToEmitFileFinish - If the passes to emit the specified file had to
129 /// be split up (e.g., to add an object writer pass), this method can be used to
130 /// finish up adding passes to emit the file, if necessary.
131 bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM,
132 ObjectCodeEmitter *OCE,
133 CodeGenOpt::Level OptLevel) {
134 if (OCE)
135 addSimpleCodeEmitter(PM, OptLevel, PrintEmittedAsm, *OCE);
136
137 PM.add(createGCInfoDeleter());
138
139 // Delete machine code for this function
140 PM.add(createMachineCodeDeleter());
141
142 return false; // success!
143 }
144
128145 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
129146 /// get machine code emitted. This uses a MachineCodeEmitter object to handle
130147 /// actually outputting the machine code and resolving things like the address
1616 #include "llvm/Constants.h"
1717 #include "llvm/DerivedTypes.h"
1818 #include "llvm/CodeGen/MachineRelocation.h"
19 #include "llvm/CodeGen/BinaryObject.h"
1920 #include "llvm/Target/TargetAsmInfo.h"
2021 #include
2122 #include
271272 /// turned into the SectionCommand in the load command for a particlar
272273 /// segment.
273274
274 struct MachOSection {
275 struct MachOSection : public BinaryObject {
275276 std::string sectname; // name of this section,
276277 std::string segname; // segment this section goes in
277278 uint64_t addr; // memory address of this section
278 uint64_t size; // size in bytes of this section
279279 uint32_t offset; // file offset of this section
280280 uint32_t align; // section alignment (power of 2)
281281 uint32_t reloff; // file offset of relocation entries
289289 /// to the correct section.
290290 uint32_t Index;
291291
292 /// SectionData - The actual data for this section which we are building
293 /// up for emission to the file.
294 DataBuffer SectionData;
295
296292 /// RelocBuffer - A buffer to hold the mach-o relocations before we write
297293 /// them out at the appropriate location in the file.
298294 DataBuffer RelocBuffer;
299
300 /// Relocations - The relocations that we have encountered so far in this
301 /// section that we will need to convert to MachORelocation entries when
302 /// the file is written.
303 std::vector Relocations;
304295
305296 // Constants for the section types (low 8 bits of flags field)
306297 // see
373364 }
374365
375366 MachOSection(const std::string &seg, const std::string §)
376 : sectname(sect), segname(seg), addr(0), size(0), offset(0), align(2),
377 reloff(0), nreloc(0), flags(0), reserved1(0), reserved2(0),
367 : BinaryObject(), sectname(sect), segname(seg), addr(0), offset(0),
368 align(2), reloff(0), nreloc(0), flags(0), reserved1(0), reserved2(0),
378369 reserved3(0) { }
379370
380371 }; // end struct MachOSection
381372
382 /// MachOSymTab - This struct contains information about the offsets and
383 /// size of symbol table information.
384 /// segment.
385 struct MachODySymTab {
386 uint32_t cmd; // LC_DYSYMTAB
387 uint32_t cmdsize; // sizeof( MachODySymTab )
388 uint32_t ilocalsym; // index to local symbols
389 uint32_t nlocalsym; // number of local symbols
390 uint32_t iextdefsym; // index to externally defined symbols
391 uint32_t nextdefsym; // number of externally defined symbols
392 uint32_t iundefsym; // index to undefined symbols
393 uint32_t nundefsym; // number of undefined symbols
394 uint32_t tocoff; // file offset to table of contents
395 uint32_t ntoc; // number of entries in table of contents
396 uint32_t modtaboff; // file offset to module table
397 uint32_t nmodtab; // number of module table entries
398 uint32_t extrefsymoff; // offset to referenced symbol table
399 uint32_t nextrefsyms; // number of referenced symbol table entries
400 uint32_t indirectsymoff; // file offset to the indirect symbol table
401 uint32_t nindirectsyms; // number of indirect symbol table entries
402 uint32_t extreloff; // offset to external relocation entries
403 uint32_t nextrel; // number of external relocation entries
404 uint32_t locreloff; // offset to local relocation entries
405 uint32_t nlocrel; // number of local relocation entries
406
407 // Constants for the cmd field
408 // see
409 enum { LC_DYSYMTAB = 0x0B // dynamic link-edit symbol table info
410 };
411
412 MachODySymTab() : cmd(LC_DYSYMTAB), cmdsize(20 * sizeof(uint32_t)),
413 ilocalsym(0), nlocalsym(0), iextdefsym(0), nextdefsym(0),
414 iundefsym(0), nundefsym(0), tocoff(0), ntoc(0), modtaboff(0),
415 nmodtab(0), extrefsymoff(0), nextrefsyms(0), indirectsymoff(0),
416 nindirectsyms(0), extreloff(0), nextrel(0), locreloff(0), nlocrel(0) { }
417 };
373 /// MachOSymTab - This struct contains information about the offsets and
374 /// size of symbol table information.
375 /// segment.
376 struct MachODySymTab {
377 uint32_t cmd; // LC_DYSYMTAB
378 uint32_t cmdsize; // sizeof(MachODySymTab)
379 uint32_t ilocalsym; // index to local symbols
380 uint32_t nlocalsym; // number of local symbols
381 uint32_t iextdefsym; // index to externally defined symbols
382 uint32_t nextdefsym; // number of externally defined symbols
383 uint32_t iundefsym; // index to undefined symbols
384 uint32_t nundefsym; // number of undefined symbols
385 uint32_t tocoff; // file offset to table of contents
386 uint32_t ntoc; // number of entries in table of contents
387 uint32_t modtaboff; // file offset to module table
388 uint32_t nmodtab; // number of module table entries
389 uint32_t extrefsymoff; // offset to referenced symbol table
390 uint32_t nextrefsyms; // number of referenced symbol table entries
391 uint32_t indirectsymoff; // file offset to the indirect symbol table
392 uint32_t nindirectsyms; // number of indirect symbol table entries
393 uint32_t extreloff; // offset to external relocation entries
394 uint32_t nextrel; // number of external relocation entries
395 uint32_t locreloff; // offset to local relocation entries
396 uint32_t nlocrel; // number of local relocation entries
397
398 // Constants for the cmd field
399 // see
400 enum { LC_DYSYMTAB = 0x0B // dynamic link-edit symbol table info
401 };
402
403 MachODySymTab() : cmd(LC_DYSYMTAB), cmdsize(20 * sizeof(uint32_t)),
404 ilocalsym(0), nlocalsym(0), iextdefsym(0), nextdefsym(0),
405 iundefsym(0), nundefsym(0), tocoff(0), ntoc(0), modtaboff(0),
406 nmodtab(0), extrefsymoff(0), nextrefsyms(0), indirectsymoff(0),
407 nindirectsyms(0), extreloff(0), nextrel(0), locreloff(0), nlocrel(0) { }
408
409 }; // end struct MachODySymTab
418410
419411 } // end namespace llvm
420412
1515 #include "llvm/Target/TargetAsmInfo.h"
1616 #include "llvm/Support/Mangler.h"
1717 #include "llvm/Support/OutputBuffer.h"
18 #include
1819
1920 //===----------------------------------------------------------------------===//
2021 // MachOCodeEmitter Implementation
3839 // Get the Mach-O Section that this function belongs in.
3940 MachOSection *MOS = MOW.getTextSection();
4041
41 // FIXME: better memory management
42 MOS->SectionData.reserve(4096);
43 BufferBegin = &MOS->SectionData[0];
44 BufferEnd = BufferBegin + MOS->SectionData.capacity();
45
4642 // Upgrade the section alignment if required.
4743 if (MOS->align < Align) MOS->align = Align;
4844
49 // Round the size up to the correct alignment for starting the new function.
50 if ((MOS->size & ((1 << Align) - 1)) != 0) {
51 MOS->size += (1 << Align);
52 MOS->size &= ~((1 << Align) - 1);
53 }
45 MOS->emitAlignment(Align);
5446
55 // FIXME: Using MOS->size directly here instead of calculating it from the
56 // output buffer size (impossible because the code emitter deals only in raw
57 // bytes) forces us to manually synchronize size and write padding zero bytes
58 // to the output buffer for all non-text sections. For text sections, we do
59 // not synchonize the output buffer, and we just blow up if anyone tries to
60 // write non-code to it. An assert should probably be added to
61 // AddSymbolToSection to prevent calling it on the text section.
62 CurBufferPtr = BufferBegin + MOS->size;
47 // Create symbol for function entry
48 const GlobalValue *FuncV = MF.getFunction();
49 MachOSym FnSym(FuncV, MOW.Mang->getValueName(FuncV), MOS->Index, TAI);
50 FnSym.n_value = getCurrentPCOffset();
51
52 // add it to the symtab.
53 MOW.SymbolTable.push_back(FnSym);
6354 }
6455
6556 /// finishFunction - This callback is invoked after the function is completely
7061 // Get the Mach-O Section that this function belongs in.
7162 MachOSection *MOS = MOW.getTextSection();
7263
73 // Get a symbol for the function to add to the symbol table
74 // FIXME: it seems like we should call something like AddSymbolToSection
75 // in startFunction rather than changing the section size and symbol n_value
76 // here.
77 const GlobalValue *FuncV = MF.getFunction();
78 MachOSym FnSym(FuncV, MOW.Mang->getValueName(FuncV), MOS->Index, TAI);
79 FnSym.n_value = MOS->size;
80 MOS->size = CurBufferPtr - BufferBegin;
81
8264 // Emit constant pool to appropriate section(s)
8365 emitConstantPool(MF.getConstantPool());
8466
11193 } else {
11294 assert(0 && "Unhandled relocation type");
11395 }
114 MOS->Relocations.push_back(MR);
96 MOS->addRelocation(MR);
11597 }
11698 Relocations.clear();
117
118 // Finally, add it to the symtab.
119 MOW.SymbolTable.push_back(FnSym);
12099
121100 // Clear per-function data structures.
122101 CPLocations.clear();
150129 unsigned Size = TM.getTargetData()->getTypeAllocSize(Ty);
151130
152131 MachOSection *Sec = MOW.getConstSection(CP[i].Val.ConstVal);
153 OutputBuffer SecDataOut(Sec->SectionData, is64Bit, isLittleEndian);
132 OutputBuffer SecDataOut(Sec->getData(), is64Bit, isLittleEndian);
154133
155 CPLocations.push_back(Sec->SectionData.size());
134 CPLocations.push_back(Sec->size());
156135 CPSections.push_back(Sec->Index);
157
158 // FIXME: remove when we have unified size + output buffer
159 Sec->size += Size;
160136
161137 // Allocate space in the section for the global.
162138 // FIXME: need alignment?
164140 for (unsigned j = 0; j < Size; ++j)
165141 SecDataOut.outbyte(0);
166142
167 MOW.InitMem(CP[i].Val.ConstVal, &Sec->SectionData[0], CPLocations[i],
168 TM.getTargetData(), Sec->Relocations);
143 MachOWriter::InitMem(CP[i].Val.ConstVal, CPLocations[i], TM.getTargetData(), Sec);
169144 }
170145 }
171146
172147 /// emitJumpTables - Emit all the jump tables for a given jump table info
173148 /// record to the appropriate section.
174
175149 void MachOCodeEmitter::emitJumpTables(MachineJumpTableInfo *MJTI) {
176150 const std::vector &JT = MJTI->getJumpTables();
177151 if (JT.empty()) return;
182156
183157 MachOSection *Sec = MOW.getJumpTableSection();
184158 unsigned TextSecIndex = MOW.getTextSection()->Index;
185 OutputBuffer SecDataOut(Sec->SectionData, is64Bit, isLittleEndian);
159 OutputBuffer SecDataOut(Sec->getData(), is64Bit, isLittleEndian);
186160
187161 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
188162 // For each jump table, record its offset from the start of the section,
189163 // reserve space for the relocations to the MBBs, and add the relocations.
190164 const std::vector &MBBs = JT[i].MBBs;
191 JTLocations.push_back(Sec->SectionData.size());
165 JTLocations.push_back(Sec->size());
192166 for (unsigned mi = 0, me = MBBs.size(); mi != me; ++mi) {
193 MachineRelocation MR(MOW.GetJTRelocation(Sec->SectionData.size(),
194 MBBs[mi]));
167 MachineRelocation MR(MOW.GetJTRelocation(Sec->size(), MBBs[mi]));
195168 MR.setResultPointer((void *)JTLocations[i]);
196169 MR.setConstantVal(TextSecIndex);
197 Sec->Relocations.push_back(MR);
170 Sec->addRelocation(MR);
198171 SecDataOut.outaddr(0);
199172 }
200173 }
201 // FIXME: remove when we have unified size + output buffer
202 Sec->size = Sec->SectionData.size();
203174 }
204175
205176 } // end namespace llvm
1010 #define MACHOCODEEMITTER_H
1111
1212 #include "MachOWriter.h"
13 #include "llvm/CodeGen/MachineCodeEmitter.h"
14 #include
1513
1614 namespace llvm {
1715
1816 /// MachOCodeEmitter - This class is used by the MachOWriter to emit the code
1917 /// for functions to the Mach-O file.
2018
21 class MachOCodeEmitter : public MachineCodeEmitter {
19 class MachOCodeEmitter : public ObjectCodeEmitter {
2220 MachOWriter &MOW;
2321
2422 /// Target machine description.
3331 /// Relocations - These are the relocations that the function needs, as
3432 /// emitted.
3533 std::vector Relocations;
36
37 /// CPLocations - This is a map of constant pool indices to offsets from the
38 /// start of the section for that constant pool index.
39 std::vector CPLocations;
4034
41 /// CPSections - This is a map of constant pool indices to the MachOSection
42 /// containing the constant pool entry for that index.
43 std::vector CPSections;
35 std::map Labels;
4436
45 /// JTLocations - This is a map of jump table indices to offsets from the
46 /// start of the section for that jump table index.
47 std::vector JTLocations;
48
49 /// MBBLocations - This vector is a mapping from MBB ID's to their address.
50 /// It is filled in by the StartMachineBasicBlock callback and queried by
51 /// the getMachineBasicBlockAddress callback.
52 std::vector MBBLocations;
53
5437 public:
55 MachOCodeEmitter(MachOWriter &mow) : MOW(mow), TM(MOW.TM)
56 {
38 MachOCodeEmitter(MachOWriter &mow, MachOSection &mos) :
39 ObjectCodeEmitter(&mos), MOW(mow), TM(MOW.TM) {
5740 is64Bit = TM.getTargetData()->getPointerSizeInBits() == 64;
5841 isLittleEndian = TM.getTargetData()->isLittleEndian();
5942 TAI = TM.getTargetAsmInfo();
6851
6952 void emitConstantPool(MachineConstantPool *MCP);
7053 void emitJumpTables(MachineJumpTableInfo *MJTI);
71
72 virtual uintptr_t getConstantPoolEntryAddress(unsigned Index) const {
73 assert(CPLocations.size() > Index && "CP not emitted!");
74 return CPLocations[Index];
75 }
76 virtual uintptr_t getJumpTableEntryAddress(unsigned Index) const {
77 assert(JTLocations.size() > Index && "JT not emitted!");
78 return JTLocations[Index];
79 }
8054
81 virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) {
82 if (MBBLocations.size() <= (unsigned)MBB->getNumber())
83 MBBLocations.resize((MBB->getNumber()+1)*2);
84 MBBLocations[MBB->getNumber()] = getCurrentPCOffset();
85 }
86
87 virtual uintptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const {
88 assert(MBBLocations.size() > (unsigned)MBB->getNumber() &&
89 MBBLocations[MBB->getNumber()] && "MBB not emitted!");
90 return MBBLocations[MBB->getNumber()];
55 virtual void emitLabel(uint64_t LabelID) {
56 Labels[LabelID] = getCurrentPCOffset();
9157 }
9258
9359 virtual uintptr_t getLabelAddress(uint64_t Label) const {
94 assert(0 && "get Label not implemented");
95 abort();
96 return 0;
97 }
98
99 virtual void emitLabel(uint64_t LabelID) {
100 assert(0 && "emit Label not implemented");
101 abort();
60 return Labels.find(Label)->second;
10261 }
10362
10463 virtual void setModuleInfo(llvm::MachineModuleInfo* MMI) { }
105
106 /// JIT SPECIFIC FUNCTIONS - DO NOT IMPLEMENT THESE HERE!
107 virtual void startGVStub(const GlobalValue* F, unsigned StubSize,
108 unsigned Alignment = 1) {
109 assert(0 && "JIT specific function called!");
110 abort();
111 }
112 virtual void startGVStub(const GlobalValue* F, void *Buffer,
113 unsigned StubSize) {
114 assert(0 && "JIT specific function called!");
115 abort();
116 }
117 virtual void *finishGVStub(const GlobalValue* F) {
118 assert(0 && "JIT specific function called!");
119 abort();
120 return 0;
121 }
12264
12365 }; // end class MachOCodeEmitter
12466
4545
4646 /// AddMachOWriter - Concrete function to add the Mach-O writer to the function
4747 /// pass manager.
48 MachineCodeEmitter *AddMachOWriter(PassManagerBase &PM,
48 ObjectCodeEmitter *AddMachOWriter(PassManagerBase &PM,
4949 raw_ostream &O,
5050 TargetMachine &TM) {
5151 MachOWriter *MOW = new MachOWriter(O, TM);
5252 PM.add(MOW);
53 return &MOW->getMachineCodeEmitter();
53 return MOW->getObjectCodeEmitter();
5454 }
5555
5656 //===----------------------------------------------------------------------===//
5959
6060 char MachOWriter::ID = 0;
6161
62 MachOWriter::MachOWriter(raw_ostream &o, TargetMachine &tm)
63 : MachineFunctionPass(&ID), O(o), TM(tm) {
62 MachOWriter::MachOWriter(raw_ostream &o, TargetMachine &tm)
63 : MachineFunctionPass(&ID), O(o), TM(tm)
64 {
6465 is64Bit = TM.getTargetData()->getPointerSizeInBits() == 64;
6566 isLittleEndian = TM.getTargetData()->isLittleEndian();
6667
6869
6970 // Create the machine code emitter object for this target.
7071
71 MCE = new MachOCodeEmitter(*this);
72 MachOCE = new MachOCodeEmitter(*this, *getTextSection(true));
7273 }
7374
7475 MachOWriter::~MachOWriter() {
75 delete MCE;
76 delete MachOCE;
7677 }
7778
7879 bool MachOWriter::doInitialization(Module &M) {
9697 /// the Mach-O file to 'O'.
9798 bool MachOWriter::doFinalization(Module &M) {
9899 // FIXME: we don't handle debug info yet, we should probably do that.
99
100 // Okay, the.text section has been completed, build the .data, .bss, and
100 // Okay, the.text section has been completed, build the .data, .bss, and
101101 // "common" sections next.
102
102103 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
103104 I != E; ++I)
104105 EmitGlobal(I);
105
106
106107 // Emit the header and load commands.
107108 EmitHeaderAndLoadCommands();
108109
132133 // Reserve space in the .bss section for this symbol while maintaining the
133134 // desired section alignment, which must be at least as much as required by
134135 // this symbol.
135 OutputBuffer SecDataOut(Sec->SectionData, is64Bit, isLittleEndian);
136 OutputBuffer SecDataOut(Sec->getData(), is64Bit, isLittleEndian);
136137
137138 if (Align) {
138 uint64_t OrigSize = Sec->size;
139139 Align = Log2_32(Align);
140140 Sec->align = std::max(unsigned(Sec->align), Align);
141 Sec->size = (Sec->size + Align - 1) & ~(Align-1);
142
143 // Add alignment padding to buffer as well.
144 // FIXME: remove when we have unified size + output buffer
145 unsigned AlignedSize = Sec->size - OrigSize;
146 for (unsigned i = 0; i < AlignedSize; ++i)
147 SecDataOut.outbyte(0);
141
142 Sec->emitAlignment(Sec->align);
148143 }
149144 // Globals without external linkage apparently do not go in the symbol table.
150145 if (!GV->hasLocalLinkage()) {
151146 MachOSym Sym(GV, Mang->getValueName(GV), Sec->Index, TAI);
152 Sym.n_value = Sec->size;
147 Sym.n_value = Sec->size();
153148 SymbolTable.push_back(Sym);
154149 }
155150
156151 // Record the offset of the symbol, and then allocate space for it.
157152 // FIXME: remove when we have unified size + output buffer
158 Sec->size += Size;
159
160 // Now that we know what section the GlovalVariable is going to be emitted
153
154 // Now that we know what section the GlovalVariable is going to be emitted
161155 // into, update our mappings.
162156 // FIXME: We may also need to update this when outputting non-GlobalVariable
163157 // GlobalValues such as functions.
158
164159 GVSection[GV] = Sec;
165 GVOffset[GV] = Sec->SectionData.size();
166
160 GVOffset[GV] = Sec->size();
161
167162 // Allocate space in the section for the global.
168163 for (unsigned i = 0; i < Size; ++i)
169164 SecDataOut.outbyte(0);
173168 const Type *Ty = GV->getType()->getElementType();
174169 unsigned Size = TM.getTargetData()->getTypeAllocSize(Ty);
175170 bool NoInit = !GV->hasInitializer();
176
171
177172 // If this global has a zero initializer, it is part of the .bss or common
178173 // section.
179174 if (NoInit || GV->getInitializer()->isNullValue()) {
182177 // merged with other symbols.
183178 if (NoInit || GV->hasLinkOnceLinkage() || GV->hasWeakLinkage() ||
184179 GV->hasCommonLinkage()) {
185 MachOSym ExtOrCommonSym(GV, Mang->getValueName(GV),
186 MachOSym::NO_SECT, TAI);
180 MachOSym ExtOrCommonSym(GV, Mang->getValueName(GV), MachOSym::NO_SECT, TAI);
187181 // For undefined (N_UNDF) external (N_EXT) types, n_value is the size in
188182 // bytes of the symbol.
189183 ExtOrCommonSym.n_value = Size;
197191 AddSymbolToSection(BSS, GV);
198192 return;
199193 }
200
194
201195 // Scalar read-only data goes in a literal section if the scalar is 4, 8, or
202196 // 16 bytes, or a cstring. Other read only data goes into a regular const
203197 // section. Read-write data goes in the data section.
204 MachOSection *Sec = GV->isConstant() ? getConstSection(GV->getInitializer()) :
198 MachOSection *Sec = GV->isConstant() ? getConstSection(GV->getInitializer()) :
205199 getDataSection();
206200 AddSymbolToSection(Sec, GV);
207 InitMem(GV->getInitializer(), &Sec->SectionData[0], GVOffset[GV],
208 TM.getTargetData(), Sec->Relocations);
201 InitMem(GV->getInitializer(), GVOffset[GV], TM.getTargetData(), Sec);
209202 }
210203
211204
213206 void MachOWriter::EmitHeaderAndLoadCommands() {
214207 // Step #0: Fill in the segment load command size, since we need it to figure
215208 // out the rest of the header fields
209
216210 MachOSegment SEG("", is64Bit);
217211 SEG.nsects = SectionList.size();
218 SEG.cmdsize = SEG.cmdSize(is64Bit) +
212 SEG.cmdsize = SEG.cmdSize(is64Bit) +
219213 SEG.nsects * SectionList[0]->cmdSize(is64Bit);
220
214
221215 // Step #1: calculate the number of load commands. We always have at least
222216 // one, for the LC_SEGMENT load command, plus two for the normal
223217 // and dynamic symbol tables, if there are any symbols.
224218 Header.ncmds = SymbolTable.empty() ? 1 : 3;
225
219
226220 // Step #2: calculate the size of the load commands
227221 Header.sizeofcmds = SEG.cmdsize;
228222 if (!SymbolTable.empty())
229223 Header.sizeofcmds += SymTab.cmdsize + DySymTab.cmdsize;
230
224
231225 // Step #3: write the header to the file
232226 // Local alias to shortenify coming code.
233227 DataBuffer &FH = Header.HeaderData;
242236 FHOut.outword(Header.flags);
243237 if (is64Bit)
244238 FHOut.outword(Header.reserved);
245
239
246240 // Step #4: Finish filling in the segment load command and write it out
247241 for (std::vector::iterator I = SectionList.begin(),
248242 E = SectionList.end(); I != E; ++I)
249 SEG.filesize += (*I)->size;
243 SEG.filesize += (*I)->size();
250244
251245 SEG.vmsize = SEG.filesize;
252246 SEG.fileoff = Header.cmdSize(is64Bit) + Header.sizeofcmds;
253
247
254248 FHOut.outword(SEG.cmd);
255249 FHOut.outword(SEG.cmdsize);
256250 FHOut.outstring(SEG.segname, 16);
262256 FHOut.outword(SEG.initprot);
263257 FHOut.outword(SEG.nsects);
264258 FHOut.outword(SEG.flags);
265
266 // Step #5: Finish filling in the fields of the MachOSections
259
260 // Step #5: Finish filling in the fields of the MachOSections
267261 uint64_t currentAddr = 0;
268262 for (std::vector::iterator I = SectionList.begin(),
269263 E = SectionList.end(); I != E; ++I) {
270264 MachOSection *MOS = *I;
271265 MOS->addr = currentAddr;
272266 MOS->offset = currentAddr + SEG.fileoff;
273
274267 // FIXME: do we need to do something with alignment here?
275 currentAddr += MOS->size;
276 }
277
268 currentAddr += MOS->size();
269 }
270
278271 // Step #6: Emit the symbol table to temporary buffers, so that we know the
279272 // size of the string table when we write the next load command. This also
280273 // sorts and assigns indices to each of the symbols, which is necessary for
281274 // emitting relocations to externally-defined objects.
282275 BufferSymbolAndStringTable();
283
276
284277 // Step #7: Calculate the number of relocations for each section and write out
285278 // the section commands for each section
286279 currentAddr += SEG.fileoff;
287280 for (std::vector::iterator I = SectionList.begin(),
288281 E = SectionList.end(); I != E; ++I) {
289282 MachOSection *MOS = *I;
283
290284 // Convert the relocations to target-specific relocations, and fill in the
291285 // relocation offset for this section.
292286 CalculateRelocations(*MOS);
293287 MOS->reloff = MOS->nreloc ? currentAddr : 0;
294288 currentAddr += MOS->nreloc * 8;
295
289
296290 // write the finalized section command to the output buffer
297291 FHOut.outstring(MOS->sectname, 16);
298292 FHOut.outstring(MOS->segname, 16);
299293 FHOut.outaddr(MOS->addr);
300 FHOut.outaddr(MOS->size);
294 FHOut.outaddr(MOS->size());
301295 FHOut.outword(MOS->offset);
302296 FHOut.outword(MOS->align);
303297 FHOut.outword(MOS->reloff);
308302 if (is64Bit)
309303 FHOut.outword(MOS->reserved3);
310304 }
311
305
312306 // Step #8: Emit LC_SYMTAB/LC_DYSYMTAB load commands
313307 SymTab.symoff = currentAddr;
314308 SymTab.nsyms = SymbolTable.size();
344338 FHOut.outword(DySymTab.nextrel);
345339 FHOut.outword(DySymTab.locreloff);
346340 FHOut.outword(DySymTab.nlocrel);
347
341
348342 O.write((char*)&FH[0], FH.size());
349343 }
350344
351345 /// EmitSections - Now that we have constructed the file header and load
352346 /// commands, emit the data for each section to the file.
353
354347 void MachOWriter::EmitSections() {
355348 for (std::vector::iterator I = SectionList.begin(),
356349 E = SectionList.end(); I != E; ++I)
357350 // Emit the contents of each section
358 O.write((char*)&(*I)->SectionData[0], (*I)->size);
359 }
351 if ((*I)->size())
352 O.write((char*)&(*I)->getData()[0], (*I)->size());
353 }
354
355 /// EmitRelocations - emit relocation data from buffer.
360356 void MachOWriter::EmitRelocations() {
361357 for (std::vector::iterator I = SectionList.begin(),
362358 E = SectionList.end(); I != E; ++I)
363359 // Emit the relocation entry data for each section.
364 O.write((char*)&(*I)->RelocBuffer[0], (*I)->RelocBuffer.size());
360 if ((*I)->RelocBuffer.size())
361 O.write((char*)&(*I)->RelocBuffer[0], (*I)->RelocBuffer.size());
365362 }
366363
367364 /// BufferSymbolAndStringTable - Sort the symbols we encountered and assign them
368365 /// each a string table index so that they appear in the correct order in the
369366 /// output file.
370
371367 void MachOWriter::BufferSymbolAndStringTable() {
372368 // The order of the symbol table is:
373369 // 1. local symbols
374370 // 2. defined external symbols (sorted by name)
375371 // 3. undefined external symbols (sorted by name)
376
372
377373 // Before sorting the symbols, check the PendingGlobals for any undefined
378374 // globals that need to be put in the symbol table.
379
380375 for (std::vector::iterator I = PendingGlobals.begin(),
381376 E = PendingGlobals.end(); I != E; ++I) {
382377 if (GVOffset[*I] == 0 && GVSection[*I] == 0) {
388383
389384 // Sort the symbols by name, so that when we partition the symbols by scope
390385 // of definition, we won't have to sort by name within each partition.
391
392386 std::sort(SymbolTable.begin(), SymbolTable.end(), MachOSym::SymCmp());
393387
394 // Parition the symbol table entries so that all local symbols come before
388 // Parition the symbol table entries so that all local symbols come before
395389 // all symbols with external linkage. { 1 | 2 3 }
396
397 std::partition(SymbolTable.begin(), SymbolTable.end(),
398 MachOSym::PartitionByLocal);
399
390 std::partition(SymbolTable.begin(), SymbolTable.end(), MachOSym::PartitionByLocal);
391
400392 // Advance iterator to beginning of external symbols and partition so that
401393 // all external symbols defined in this module come before all external
402394 // symbols defined elsewhere. { 1 | 2 | 3 }
403
404395 for (std::vector::iterator I = SymbolTable.begin(),
405396 E = SymbolTable.end(); I != E; ++I) {
406397 if (!MachOSym::PartitionByLocal(*I)) {
409400 }
410401 }
411402
412 // Calculate the starting index for each of the local, extern defined, and
403 // Calculate the starting index for each of the local, extern defined, and
413404 // undefined symbols, as well as the number of each to put in the LC_DYSYMTAB
414405 // load command.
415
416406 for (std::vector::iterator I = SymbolTable.begin(),
417407 E = SymbolTable.end(); I != E; ++I) {
418408 if (MachOSym::PartitionByLocal(*I)) {
426416 ++DySymTab.nundefsym;
427417 }
428418 }
429
419
430420 // Write out a leading zero byte when emitting string table, for n_strx == 0
431421 // which means an empty string.
432
433422 OutputBuffer StrTOut(StrT, is64Bit, isLittleEndian);
434423 StrTOut.outbyte(0);
435424
438427 // 2. strings for local symbols
439428 // Since this is the opposite order from the symbol table, which we have just
440429 // sorted, we can walk the symbol table backwards to output the string table.
441
442430 for (std::vector::reverse_iterator I = SymbolTable.rbegin(),
443431 E = SymbolTable.rend(); I != E; ++I) {
444432 if (I->GVName == "") {
462450 I->n_value += GVSection[GV]->addr;
463451 if (GV && (GVOffset[GV] == -1))
464452 GVOffset[GV] = index;
465
453
466454 // Emit nlist to buffer
467455 SymTOut.outword(I->n_strx);
468456 SymTOut.outbyte(I->n_type);
477465 /// and the offset into that section. From this information, create the
478466 /// appropriate target-specific MachORelocation type and add buffer it to be
479467 /// written out after we are finished writing out sections.
480
481468 void MachOWriter::CalculateRelocations(MachOSection &MOS) {
482 for (unsigned i = 0, e = MOS.Relocations.size(); i != e; ++i) {
483 MachineRelocation &MR = MOS.Relocations[i];
469 std::vector Relocations = MOS.getRelocations();
470 for (unsigned i = 0, e = Relocations.size(); i != e; ++i) {
471 MachineRelocation &MR = Relocations[i];
484472 unsigned TargetSection = MR.getConstantVal();
485473 unsigned TargetAddr = 0;
486474 unsigned TargetIndex = 0;
487475
488476 // This is a scattered relocation entry if it points to a global value with
489477 // a non-zero offset.
490
491478 bool Scattered = false;
492479 bool Extern = false;
493480
494481 // Since we may not have seen the GlobalValue we were interested in yet at
495482 // the time we emitted the relocation for it, fix it up now so that it
496483 // points to the offset into the correct section.
497
498484 if (MR.isGlobalValue()) {
499485 GlobalValue *GV = MR.getGlobalValue();
500486 MachOSection *MOSPtr = GVSection[GV];
501487 intptr_t Offset = GVOffset[GV];
502
488
503489 // If we have never seen the global before, it must be to a symbol
504490 // defined in another module (N_UNDF).
505
506491 if (!MOSPtr) {
507492 // FIXME: need to append stub suffix
508493 Extern = true;
514499 }
515500 MR.setResultPointer((void*)Offset);
516501 }
517
502
518503 // If the symbol is locally defined, pass in the address of the section and
519504 // the section index to the code which will generate the target relocation.
520
521505 if (!Extern) {
522506 MachOSection &To = *SectionList[TargetSection - 1];
523507 TargetAddr = To.addr;
525509 }
526510
527511 OutputBuffer RelocOut(MOS.RelocBuffer, is64Bit, isLittleEndian);
528 OutputBuffer SecOut(MOS.SectionData, is64Bit, isLittleEndian);
512 OutputBuffer SecOut(MOS.getData(), is64Bit, isLittleEndian);
529513
530514 MOS.nreloc += GetTargetRelocation(MR, MOS.Index, TargetAddr, TargetIndex,
531515 RelocOut, SecOut, Scattered, Extern);
534518
535519 // InitMem - Write the value of a Constant to the specified memory location,
536520 // converting it into bytes and relocations.
537
538 void MachOWriter::InitMem(const Constant *C, void *Addr, intptr_t Offset,
539 const TargetData *TD,
540 std::vector &MRs) {
521 void MachOWriter::InitMem(const Constant *C, uintptr_t Offset,
522 const TargetData *TD, MachOSection* mos) {
541523 typedef std::pair CPair;
542524 std::vector WorkList;
543
525 uint8_t *Addr = &mos->getData()[0];
526
544527 WorkList.push_back(CPair(C,(intptr_t)Addr + Offset));
545
528
546529 intptr_t ScatteredOffset = 0;
547
530
548531 while (!WorkList.empty()) {
549532 const Constant *PC = WorkList.back().first;
550533 intptr_t PA = WorkList.back().second;
551534 WorkList.pop_back();
552
535
553536 if (isa(PC)) {
554537 continue;
555538 } else if (const ConstantVector *CP = dyn_cast(PC)) {
642625 memset(ptr, 0, TD->getPointerSize());
643626 else if (const GlobalValue* GV = dyn_cast(PC)) {
644627 // FIXME: what about function stubs?
645 MRs.push_back(MachineRelocation::getGV(PA-(intptr_t)Addr,
628 mos->addRelocation(MachineRelocation::getGV(PA-(intptr_t)Addr,
646629 MachineRelocation::VANILLA,
647630 const_cast(GV),
648631 ScatteredOffset));
1414 #define MACHOWRITER_H
1515
1616 #include "MachO.h"
17 #include "llvm/Constants.h"
18 #include "llvm/DerivedTypes.h"
1719 #include "llvm/CodeGen/MachineFunctionPass.h"
20 #include "llvm/CodeGen/ObjectCodeEmitter.h"
1821 #include "llvm/Target/TargetData.h"
1922 #include "llvm/Target/TargetMachine.h"
2023 #include "llvm/Target/TargetMachOWriterInfo.h"
24 #include
2125 #include
2226
2327 namespace llvm {
2428 class GlobalVariable;
2529 class Mangler;
26 class MachineCodeEmitter;
30 class MachineRelocation;
31 class ObjectCodeEmitter;
2732 class MachOCodeEmitter;
33 class TargetData;
34 class TargetMachine;
2835 class OutputBuffer;
2936 class raw_ostream;
3037
3744 friend class MachOCodeEmitter;
3845 public:
3946 static char ID;
40 MachineCodeEmitter &getMachineCodeEmitter() const {
41 return *(MachineCodeEmitter*)MCE;
47
48 ObjectCodeEmitter *getObjectCodeEmitter() {
49 return reinterpret_cast(MachOCE);
4250 }
4351
4452 MachOWriter(raw_ostream &O, TargetMachine &TM);
6169 ///
6270 Mangler *Mang;
6371
64 /// MCE - The MachineCodeEmitter object that we are exposing to emit machine
72 /// MachOCE - The MachineCodeEmitter object that we are exposing to emit machine
6573 /// code for functions to the .o file.
6674
67 MachOCodeEmitter *MCE;
75 MachOCodeEmitter *MachOCE;
6876
6977 /// is64Bit/isLittleEndian - This information is inferred from the target
7078 /// machine directly, indicating what header values and flags to set.
224232 /// SymbolTable to aid in emitting the DYSYMTAB load command.
225233 std::vector DynamicSymbolTable;
226234
227 static void InitMem(const Constant *C, void *Addr, intptr_t Offset,
235 static void InitMem(const Constant *C,
236 uintptr_t Offset,
228237 const TargetData *TD,
229 std::vector &MRs);
238 MachOSection* mos);
230239
231240 private:
232241 void AddSymbolToSection(MachOSection *MOS, GlobalVariable *GV);
2323 class FunctionPass;
2424 class MachineCodeEmitter;
2525 class JITCodeEmitter;
26 class ObjectCodeEmitter;
2627 class raw_ostream;
2728
2829 // Enums corresponding to ARM condition codes
100101 MachineCodeEmitter &MCE);
101102 FunctionPass *createARMJITCodeEmitterPass(ARMBaseTargetMachine &TM,
102103 JITCodeEmitter &JCE);
104 FunctionPass *createARMObjectCodeEmitterPass(ARMBaseTargetMachine &TM,
105 ObjectCodeEmitter &OCE);
103106
104107 FunctionPass *createARMLoadStoreOptimizationPass(bool PreAlloc = false);
105108 FunctionPass *createARMConstantIslandPass();
2525 #include "llvm/PassManager.h"
2626 #include "llvm/CodeGen/MachineCodeEmitter.h"
2727 #include "llvm/CodeGen/JITCodeEmitter.h"
28 #include "llvm/CodeGen/ObjectCodeEmitter.h"
2829 #include "llvm/CodeGen/MachineConstantPool.h"
2930 #include "llvm/CodeGen/MachineFunctionPass.h"
3031 #include "llvm/CodeGen/MachineInstr.h"
173174 /// createARMCodeEmitterPass - Return a pass that emits the collected ARM code
174175 /// to the specified MCE object.
175176
176 namespace llvm {
177
178 FunctionPass *createARMCodeEmitterPass(ARMBaseTargetMachine &TM,
179 MachineCodeEmitter &MCE) {
177 FunctionPass *llvm::createARMCodeEmitterPass(ARMBaseTargetMachine &TM,
178 MachineCodeEmitter &MCE) {
180179 return new Emitter(TM, MCE);
181180 }
182 FunctionPass *createARMJITCodeEmitterPass(ARMBaseTargetMachine &TM,
183 JITCodeEmitter &JCE) {
181 FunctionPass *llvm::createARMJITCodeEmitterPass(ARMBaseTargetMachine &TM,
182 JITCodeEmitter &JCE) {
184183 return new Emitter(TM, JCE);
185184 }
186
187 } // end namespace llvm
185 FunctionPass *llvm::createARMObjectCodeEmitterPass(ARMBaseTargetMachine &TM,
186 ObjectCodeEmitter &OCE) {
187 return new Emitter(TM, OCE);
188 }
188189
189190 template
190191 bool Emitter::runOnMachineFunction(MachineFunction &MF) {
227227 return false;
228228 }
229229
230 bool ARMBaseTargetMachine::addCodeEmitter(PassManagerBase &PM,
231 CodeGenOpt::Level OptLevel,
232 bool DumpAsm,
233 ObjectCodeEmitter &OCE) {
234 // FIXME: Move this to TargetJITInfo!
235 if (DefRelocModel == Reloc::Default)
236 setRelocationModel(Reloc::Static);
237
238 // Machine code emitter pass for ARM.
239 PM.add(createARMObjectCodeEmitterPass(*this, OCE));
240 if (DumpAsm) {
241 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
242 if (AsmPrinterCtor)
243 PM.add(AsmPrinterCtor(errs(), *this, true));
244 }
245
246 return false;
247 }
248
230249 bool ARMBaseTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
231250 CodeGenOpt::Level OptLevel,
232251 bool DumpAsm,
257276 return false;
258277 }
259278
260
279 bool ARMBaseTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
280 CodeGenOpt::Level OptLevel,
281 bool DumpAsm,
282 ObjectCodeEmitter &OCE) {
283 // Machine code emitter pass for ARM.
284 PM.add(createARMObjectCodeEmitterPass(*this, OCE));
285 if (DumpAsm) {
286 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
287 if (AsmPrinterCtor)
288 PM.add(AsmPrinterCtor(errs(), *this, true));
289 }
290
291 return false;
292 }
293
7676 bool DumpAsm, MachineCodeEmitter &MCE);
7777 virtual bool addCodeEmitter(PassManagerBase &PM, CodeGenOpt::Level OptLevel,
7878 bool DumpAsm, JITCodeEmitter &MCE);
79 virtual bool addCodeEmitter(PassManagerBase &PM, CodeGenOpt::Level OptLevel,
80 bool DumpAsm, ObjectCodeEmitter &OCE);
7981 virtual bool addSimpleCodeEmitter(PassManagerBase &PM,
8082 CodeGenOpt::Level OptLevel,
8183 bool DumpAsm,
8486 CodeGenOpt::Level OptLevel,
8587 bool DumpAsm,
8688 JITCodeEmitter &MCE);
89 virtual bool addSimpleCodeEmitter(PassManagerBase &PM,
90 CodeGenOpt::Level OptLevel,
91 bool DumpAsm,
92 ObjectCodeEmitter &OCE);
8793 };
8894
8995 /// ARMTargetMachine - ARM target machine.
2121 class AlphaTargetMachine;
2222 class FunctionPass;
2323 class MachineCodeEmitter;
24 class ObjectCodeEmitter;
2425 class raw_ostream;
2526
2627 FunctionPass *createAlphaISelDag(AlphaTargetMachine &TM);
3132 FunctionPass *createAlphaCodeEmitterPass(AlphaTargetMachine &TM,
3233 MachineCodeEmitter &MCE);
3334 FunctionPass *createAlphaJITCodeEmitterPass(AlphaTargetMachine &TM,
34 JITCodeEmitter &JCE);
35 JITCodeEmitter &JCE);
36 FunctionPass *createAlphaObjectCodeEmitterPass(AlphaTargetMachine &TM,
37 ObjectCodeEmitter &OCE);
3538 FunctionPass *createAlphaLLRPPass(AlphaTargetMachine &tm);
3639 FunctionPass *createAlphaBranchSelectionPass();
3740
1818 #include "llvm/PassManager.h"
1919 #include "llvm/CodeGen/MachineCodeEmitter.h"
2020 #include "llvm/CodeGen/JITCodeEmitter.h"
21 #include "llvm/CodeGen/ObjectCodeEmitter.h"
2122 #include "llvm/CodeGen/MachineFunctionPass.h"
2223 #include "llvm/CodeGen/MachineInstr.h"
2324 #include "llvm/CodeGen/Passes.h"
8990 FunctionPass *llvm::createAlphaJITCodeEmitterPass(AlphaTargetMachine &TM,
9091 JITCodeEmitter &JCE) {
9192 return new Emitter(TM, JCE);
93 }
94 FunctionPass *llvm::createAlphaObjectCodeEmitterPass(AlphaTargetMachine &TM,
95 ObjectCodeEmitter &OCE) {
96 return new Emitter(TM, OCE);
9297 }
9398
9499 template
118118 }
119119 return false;
120120 }
121 bool AlphaTargetMachine::addCodeEmitter(PassManagerBase &PM,
122 CodeGenOpt::Level OptLevel,
123 bool DumpAsm, ObjectCodeEmitter &OCE) {
124 PM.add(createAlphaObjectCodeEmitterPass(*this, OCE));
125 if (DumpAsm) {
126 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
127 if (AsmPrinterCtor)
128 PM.add(AsmPrinterCtor(errs(), *this, true));
129 }
130 return false;
131 }
121132 bool AlphaTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
122133 CodeGenOpt::Level OptLevel,
123134 bool DumpAsm,
130141 JITCodeEmitter &JCE) {
131142 return addCodeEmitter(PM, OptLevel, DumpAsm, JCE);
132143 }
144 bool AlphaTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
145 CodeGenOpt::Level OptLevel,
146 bool DumpAsm,
147 ObjectCodeEmitter &OCE) {
148 return addCodeEmitter(PM, OptLevel, DumpAsm, OCE);
149 }
133150
7373 bool DumpAsm, MachineCodeEmitter &MCE);
7474 virtual bool addCodeEmitter(PassManagerBase &PM, CodeGenOpt::Level OptLevel,
7575 bool DumpAsm, JITCodeEmitter &JCE);
76 virtual bool addCodeEmitter(PassManagerBase &PM, CodeGenOpt::Level OptLevel,
77 bool DumpAsm, ObjectCodeEmitter &JCE);
7678 virtual bool addSimpleCodeEmitter(PassManagerBase &PM,
7779 CodeGenOpt::Level OptLevel,
7880 bool DumpAsm,
8183 CodeGenOpt::Level OptLevel,
8284 bool DumpAsm,
8385 JITCodeEmitter &JCE);
86 virtual bool addSimpleCodeEmitter(PassManagerBase &PM,
87 CodeGenOpt::Level OptLevel,
88 bool DumpAsm,
89 ObjectCodeEmitter &OCE);
8490
8591 static void registerAsmPrinter(AsmPrinterCtorFn F) {
8692 AsmPrinterCtor = F;
2323 class PPCTargetMachine;
2424 class FunctionPass;
2525 class MachineCodeEmitter;
26 class ObjectCodeEmitter;
2627 class raw_ostream;
2728
2829 FunctionPass *createPPCBranchSelectionPass();
3233 FunctionPass *createPPCCodeEmitterPass(PPCTargetMachine &TM,
3334 MachineCodeEmitter &MCE);
3435 FunctionPass *createPPCJITCodeEmitterPass(PPCTargetMachine &TM,
35 JITCodeEmitter &MCE);
36 JITCodeEmitter &MCE);
37 FunctionPass *createPPCObjectCodeEmitterPass(PPCTargetMachine &TM,
38 ObjectCodeEmitter &OCE);
3639 } // end namespace llvm;
3740
3841 // Defines symbolic names for PowerPC registers. This defines a mapping from
1818 #include "llvm/PassManager.h"
1919 #include "llvm/CodeGen/MachineCodeEmitter.h"
2020 #include "llvm/CodeGen/JITCodeEmitter.h"
21 #include "llvm/CodeGen/ObjectCodeEmitter.h"
2122 #include "llvm/CodeGen/MachineFunctionPass.h"
2223 #include "llvm/CodeGen/MachineInstrBuilder.h"
2324 #include "llvm/CodeGen/MachineModuleInfo.h"
9091
9192 /// createPPCCodeEmitterPass - Return a pass that emits the collected PPC code
9293 /// to the specified MCE object.
94
9395 FunctionPass *llvm::createPPCCodeEmitterPass(PPCTargetMachine &TM,
9496 MachineCodeEmitter &MCE) {
9597 return new Emitter(TM, MCE);
98100 FunctionPass *llvm::createPPCJITCodeEmitterPass(PPCTargetMachine &TM,
99101 JITCodeEmitter &JCE) {
100102 return new Emitter(TM, JCE);
103 }
104
105 FunctionPass *llvm::createPPCObjectCodeEmitterPass(PPCTargetMachine &TM,
106 ObjectCodeEmitter &OCE) {
107 return new Emitter(TM, OCE);
101108 }
102109
103110 template
251258 Reloc = PPC::reloc_pcrel_bx;
252259 else // BCC instruction
253260 Reloc = PPC::reloc_pcrel_bcx;
261
254262 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
255263 Reloc, MO.getMBB()));
256264 } else {
220220 return false;
221221 }
222222
223 bool PPCTargetMachine::addCodeEmitter(PassManagerBase &PM,
224 CodeGenOpt::Level OptLevel,
225 bool DumpAsm, ObjectCodeEmitter &OCE) {
226 // The JIT should use the static relocation model in ppc32 mode, PIC in ppc64.
227 // FIXME: This should be moved to TargetJITInfo!!
228 if (Subtarget.isPPC64()) {
229 // We use PIC codegen in ppc64 mode, because otherwise we'd have to use many
230 // instructions to materialize arbitrary global variable + function +
231 // constant pool addresses.
232 setRelocationModel(Reloc::PIC_);
233 // Temporary workaround for the inability of PPC64 JIT to handle jump
234 // tables.
235 DisableJumpTables = true;
236 } else {
237 setRelocationModel(Reloc::Static);
238 }
239
240 // Inform the subtarget that we are in JIT mode. FIXME: does this break macho
241 // writing?
242 Subtarget.SetJITMode();
243
244 // Machine code emitter pass for PowerPC.
245 PM.add(createPPCObjectCodeEmitterPass(*this, OCE));
246 if (DumpAsm) {
247 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
248 if (AsmPrinterCtor)
249 PM.add(AsmPrinterCtor(errs(), *this, true));
250 }
251
252 return false;
253 }
254
223255 bool PPCTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
224256 CodeGenOpt::Level OptLevel,
225257 bool DumpAsm,
250282 return false;
251283 }
252284
285 bool PPCTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
286 CodeGenOpt::Level OptLevel,
287 bool DumpAsm,
288 ObjectCodeEmitter &OCE) {
289 // Machine code emitter pass for PowerPC.
290 PM.add(createPPCObjectCodeEmitterPass(*this, OCE));
291 if (DumpAsm) {
292 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
293 if (AsmPrinterCtor)
294 PM.add(AsmPrinterCtor(errs(), *this, true));
295 }
296
297 return false;
298 }
299
300
8484 bool DumpAsm, MachineCodeEmitter &MCE);
8585 virtual bool addCodeEmitter(PassManagerBase &PM, CodeGenOpt::Level OptLevel,
8686 bool DumpAsm, JITCodeEmitter &JCE);
87 virtual bool addCodeEmitter(PassManagerBase &PM, CodeGenOpt::Level OptLevel,
88 bool DumpAsm, ObjectCodeEmitter &OCE);
8789 virtual bool addSimpleCodeEmitter(PassManagerBase &PM,
8890 CodeGenOpt::Level OptLevel,
8991 bool DumpAsm, MachineCodeEmitter &MCE);
9092 virtual bool addSimpleCodeEmitter(PassManagerBase &PM,
9193 CodeGenOpt::Level OptLevel,
9294 bool DumpAsm, JITCodeEmitter &JCE);
95 virtual bool addSimpleCodeEmitter(PassManagerBase &PM,
96 CodeGenOpt::Level OptLevel,
97 bool DumpAsm, ObjectCodeEmitter &OCE);
9398 virtual bool getEnableTailMergeDefault() const;
9499 };
95100
5555 MachineCodeEmitter &MCE);
5656 FunctionPass *createX86JITCodeEmitterPass(X86TargetMachine &TM,
5757 JITCodeEmitter &JCE);
58 FunctionPass *createX86ObjectCodeEmitterPass(X86TargetMachine &TM,
59 ObjectCodeEmitter &OCE);
5860
5961 /// createX86EmitCodeToMemory - Returns a pass that converts a register
6062 /// allocated function into raw machine code in a dynamically
2121 #include "llvm/PassManager.h"
2222 #include "llvm/CodeGen/MachineCodeEmitter.h"
2323 #include "llvm/CodeGen/JITCodeEmitter.h"
24 #include "llvm/CodeGen/ObjectCodeEmitter.h"
2425 #include "llvm/CodeGen/MachineFunctionPass.h"
2526 #include "llvm/CodeGen/MachineInstr.h"
2627 #include "llvm/CodeGen/MachineModuleInfo.h"
105106 /// createX86CodeEmitterPass - Return a pass that emits the collected X86 code
106107 /// to the specified templated MachineCodeEmitter object.
107108
108 namespace llvm {
109
110 FunctionPass *createX86CodeEmitterPass(X86TargetMachine &TM,
111 MachineCodeEmitter &MCE) {
109 FunctionPass *llvm::createX86CodeEmitterPass(X86TargetMachine &TM,
110 MachineCodeEmitter &MCE) {
112111 return new Emitter(TM, MCE);
113112 }
114 FunctionPass *createX86JITCodeEmitterPass(X86TargetMachine &TM,
115 JITCodeEmitter &JCE) {
113 FunctionPass *llvm::createX86JITCodeEmitterPass(X86TargetMachine &TM,
114 JITCodeEmitter &JCE) {
116115 return new Emitter(TM, JCE);
117116 }
118
119 } // end namespace llvm
117 FunctionPass *llvm::createX86ObjectCodeEmitterPass(X86TargetMachine &TM,
118 ObjectCodeEmitter &OCE) {
119 return new Emitter(TM, OCE);
120 }
120121
121122 template
122123 bool Emitter::runOnMachineFunction(MachineFunction &MF) {
289289 return false;
290290 }
291291
292 bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
293 CodeGenOpt::Level OptLevel,
294 bool DumpAsm,
295 ObjectCodeEmitter &OCE) {
296 // FIXME: Move this to TargetJITInfo!
297 // On Darwin, do not override 64-bit setting made in X86TargetMachine().
298 if (DefRelocModel == Reloc::Default &&
299 (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit()))
300 setRelocationModel(Reloc::Static);
301
302 // 64-bit JIT places everything in the same buffer except external functions.
303 // On Darwin, use small code model but hack the call instruction for
304 // externals. Elsewhere, do not assume globals are in the lower 4G.
305 if (Subtarget.is64Bit()) {
306 if (Subtarget.isTargetDarwin())
307 setCodeModel(CodeModel::Small);
308 else
309 setCodeModel(CodeModel::Large);
310 }
311
312 PM.add(createX86ObjectCodeEmitterPass(*this, OCE));
313 if (DumpAsm) {
314 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
315 if (AsmPrinterCtor)
316 PM.add(AsmPrinterCtor(errs(), *this, true));
317 }
318
319 return false;
320 }
321
292322 bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
293323 CodeGenOpt::Level OptLevel,
294324 bool DumpAsm,
317347 return false;
318348 }
319349
350 bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
351 CodeGenOpt::Level OptLevel,
352 bool DumpAsm,
353 ObjectCodeEmitter &OCE) {
354 PM.add(createX86ObjectCodeEmitterPass(*this, OCE));
355 if (DumpAsm) {
356 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
357 if (AsmPrinterCtor)
358 PM.add(AsmPrinterCtor(errs(), *this, true));
359 }
360
361 return false;
362 }
8383 bool DumpAsm, MachineCodeEmitter &MCE);
8484 virtual bool addCodeEmitter(PassManagerBase &PM, CodeGenOpt::Level OptLevel,
8585 bool DumpAsm, JITCodeEmitter &JCE);
86 virtual bool addCodeEmitter(PassManagerBase &PM, CodeGenOpt::Level OptLevel,
87 bool DumpAsm, ObjectCodeEmitter &OCE);
8688 virtual bool addSimpleCodeEmitter(PassManagerBase &PM,
8789 CodeGenOpt::Level OptLevel,
8890 bool DumpAsm, MachineCodeEmitter &MCE);
8991 virtual bool addSimpleCodeEmitter(PassManagerBase &PM,
9092 CodeGenOpt::Level OptLevel,
9193 bool DumpAsm, JITCodeEmitter &JCE);
94 virtual bool addSimpleCodeEmitter(PassManagerBase &PM,
95 CodeGenOpt::Level OptLevel,
96 bool DumpAsm, ObjectCodeEmitter &OCE);
9297 };
9398
9499 /// X86_32TargetMachine - X86 32-bit target machine.
1616 #include "llvm/CodeGen/FileWriters.h"
1717 #include "llvm/CodeGen/LinkAllCodegenComponents.h"
1818 #include "llvm/CodeGen/LinkAllAsmWriterComponents.h"
19 #include "llvm/CodeGen/ObjectCodeEmitter.h"
1920 #include "llvm/Target/SubtargetFeature.h"
2021 #include "llvm/Target/TargetData.h"
2122 #include "llvm/Target/TargetMachine.h"
311312 #endif
312313
313314 // Ask the target to add backend passes as necessary.
314 MachineCodeEmitter *MCE = 0;
315 ObjectCodeEmitter *OCE = 0;
315316
316317 // Override default to generate verbose assembly.
317318 Target.setAsmVerbosityDefault(true);
330331 case FileModel::AsmFile:
331332 break;
332333 case FileModel::MachOFile:
333 MCE = AddMachOWriter(Passes, *Out, Target);
334 OCE = AddMachOWriter(Passes, *Out, Target);
334335 break;
335336 case FileModel::ElfFile:
336 MCE = AddELFWriter(Passes, *Out, Target);
337 OCE = AddELFWriter(Passes, *Out, Target);
337338 break;
338339 }
339340
340 if (Target.addPassesToEmitFileFinish(Passes, MCE, OLvl)) {
341 if (Target.addPassesToEmitFileFinish(Passes, OCE, OLvl)) {
341342 std::cerr << argv[0] << ": target does not support generation of this"
342343 << " file type!\n";
343344 if (Out != &outs()) delete Out;
429429
430430 codeGenPasses->add(new TargetData(*_target->getTargetData()));
431431
432 MachineCodeEmitter* mce = NULL;
432 ObjectCodeEmitter* oce = NULL;
433433
434434 switch (_target->addPassesToEmitFile(*codeGenPasses, out,
435435 TargetMachine::AssemblyFile,
436436 CodeGenOpt::Aggressive)) {
437437 case FileModel::MachOFile:
438 mce = AddMachOWriter(*codeGenPasses, out, *_target);
438 oce = AddMachOWriter(*codeGenPasses, out, *_target);
439439 break;
440440 case FileModel::ElfFile:
441 mce = AddELFWriter(*codeGenPasses, out, *_target);
441 oce = AddELFWriter(*codeGenPasses, out, *_target);
442442 break;
443443 case FileModel::AsmFile:
444444 break;
448448 return true;
449449 }
450450
451 if (_target->addPassesToEmitFileFinish(*codeGenPasses, mce,
451 if (_target->addPassesToEmitFileFinish(*codeGenPasses, oce,
452452 CodeGenOpt::Aggressive)) {
453453 errMsg = "target does not support generation of this file type";
454454 return true;