Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===-- MachOWriter.cpp - Target-independent Mach-O Writer code -----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the target-independent Mach-O writer. This file writes |
| 11 | // out the Mach-O file in the following order: |
| 12 | // |
| 13 | // #1 FatHeader (universal-only) |
| 14 | // #2 FatArch (universal-only, 1 per universal arch) |
| 15 | // Per arch: |
| 16 | // #3 Header |
| 17 | // #4 Load Commands |
| 18 | // #5 Sections |
| 19 | // #6 Relocations |
| 20 | // #7 Symbols |
| 21 | // #8 Strings |
| 22 | // |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | |
| 25 | #include "MachOWriter.h" |
Nate Begeman | 5694b47 | 2010-01-15 18:51:18 +0000 | [diff] [blame] | 26 | #include "llvm/Function.h" |
| 27 | #include "llvm/CodeGen/FileWriters.h" |
| 28 | #include "llvm/CodeGen/MachineFunction.h" |
Chris Lattner | 621c44d | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 29 | #include "llvm/MC/MCAsmInfo.h" |
Nate Begeman | 5694b47 | 2010-01-15 18:51:18 +0000 | [diff] [blame] | 30 | #include "llvm/MC/MCContext.h" |
| 31 | #include "llvm/MC/MCCodeEmitter.h" |
| 32 | #include "llvm/MC/MCInst.h" |
| 33 | #include "llvm/MC/MCStreamer.h" |
Edwin Török | ced9ff8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 34 | #include "llvm/Support/ErrorHandling.h" |
Nate Begeman | 5694b47 | 2010-01-15 18:51:18 +0000 | [diff] [blame] | 35 | #include "llvm/Support/FormattedStream.h" |
| 36 | #include "llvm/Support/Mangler.h" |
Owen Anderson | 847b99b | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 37 | #include "llvm/Support/raw_ostream.h" |
Nate Begeman | 5694b47 | 2010-01-15 18:51:18 +0000 | [diff] [blame] | 38 | #include "llvm/Target/TargetData.h" |
| 39 | #include "llvm/Target/TargetLowering.h" |
| 40 | #include "llvm/Target/TargetLoweringObjectFile.h" |
| 41 | using namespace llvm; |
Bruno Cardoso Lopes | f42e3eb | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 42 | |
Nate Begeman | 5694b47 | 2010-01-15 18:51:18 +0000 | [diff] [blame] | 43 | namespace llvm { |
| 44 | MachineFunctionPass *createMachOWriter(formatted_raw_ostream &O, |
| 45 | TargetMachine &TM, |
| 46 | const MCAsmInfo *T, |
| 47 | MCCodeEmitter *MCE) { |
| 48 | return new MachOWriter(O, TM, T, MCE); |
| 49 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | //===----------------------------------------------------------------------===// |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 53 | // MachOWriter Implementation |
| 54 | //===----------------------------------------------------------------------===// |
| 55 | |
| 56 | char MachOWriter::ID = 0; |
Bruno Cardoso Lopes | f42e3eb | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 57 | |
Nate Begeman | 5694b47 | 2010-01-15 18:51:18 +0000 | [diff] [blame] | 58 | MachOWriter::MachOWriter(formatted_raw_ostream &o, TargetMachine &tm, |
| 59 | const MCAsmInfo *T, MCCodeEmitter *MCE) |
| 60 | : MachineFunctionPass(&ID), O(o), TM(tm), MAI(T), MCCE(MCE), |
| 61 | OutContext(*new MCContext()), |
| 62 | OutStreamer(*createMachOStreamer(OutContext, O, MCCE)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | MachOWriter::~MachOWriter() { |
Nate Begeman | 5694b47 | 2010-01-15 18:51:18 +0000 | [diff] [blame] | 66 | delete &OutStreamer; |
| 67 | delete &OutContext; |
| 68 | delete MCCE; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Bruno Cardoso Lopes | f42e3eb | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 71 | bool MachOWriter::doInitialization(Module &M) { |
Nate Begeman | 5694b47 | 2010-01-15 18:51:18 +0000 | [diff] [blame] | 72 | Mang = new Mangler(M, MAI->getGlobalPrefix(), MAI->getPrivateGlobalPrefix(), |
| 73 | MAI->getLinkerPrivateGlobalPrefix()); |
| 74 | |
Nate Begeman | 5694b47 | 2010-01-15 18:51:18 +0000 | [diff] [blame] | 75 | // Initialize TargetLoweringObjectFile. |
| 76 | TM.getTargetLowering()->getObjFileLowering().Initialize(OutContext, TM); |
Bruno Cardoso Lopes | f42e3eb | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 77 | |
Bruno Cardoso Lopes | f42e3eb | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 78 | return false; |
| 79 | } |
| 80 | |
| 81 | /// doFinalization - Now that the module has been completely processed, emit |
| 82 | /// the Mach-O file to 'O'. |
| 83 | bool MachOWriter::doFinalization(Module &M) { |
Bruno Cardoso Lopes | f42e3eb | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 84 | // Release the name mangler object. |
| 85 | delete Mang; Mang = 0; |
Nate Begeman | 5694b47 | 2010-01-15 18:51:18 +0000 | [diff] [blame] | 86 | |
| 87 | OutStreamer.Finish(); |
Bruno Cardoso Lopes | f42e3eb | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 88 | return false; |
| 89 | } |
| 90 | |
Nate Begeman | 5694b47 | 2010-01-15 18:51:18 +0000 | [diff] [blame] | 91 | bool MachOWriter::runOnMachineFunction(MachineFunction &MF) { |
| 92 | const Function *F = MF.getFunction(); |
| 93 | TargetLoweringObjectFile &TLOF = TM.getTargetLowering()->getObjFileLowering(); |
| 94 | const MCSection *S = TLOF.SectionForGlobal(F, Mang, TM); |
| 95 | OutStreamer.SwitchSection(S); |
Bruno Cardoso Lopes | 3290ac9 | 2009-07-06 06:40:51 +0000 | [diff] [blame] | 96 | |
Nate Begeman | 5694b47 | 2010-01-15 18:51:18 +0000 | [diff] [blame] | 97 | for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); |
| 98 | I != E; ++I) { |
| 99 | // Print a label for the basic block. |
| 100 | for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end(); |
| 101 | II != IE; ++II) { |
| 102 | const MachineInstr *MI = II; |
| 103 | MCInst OutMI; |
| 104 | OutMI.setOpcode(MI->getOpcode()); |
Bruno Cardoso Lopes | 3290ac9 | 2009-07-06 06:40:51 +0000 | [diff] [blame] | 105 | |
Nate Begeman | 5694b47 | 2010-01-15 18:51:18 +0000 | [diff] [blame] | 106 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 107 | const MachineOperand &MO = MI->getOperand(i); |
| 108 | MCOperand MCOp; |
Bruno Cardoso Lopes | 3290ac9 | 2009-07-06 06:40:51 +0000 | [diff] [blame] | 109 | |
Nate Begeman | 5694b47 | 2010-01-15 18:51:18 +0000 | [diff] [blame] | 110 | switch (MO.getType()) { |
| 111 | default: |
| 112 | MI->dump(); |
| 113 | llvm_unreachable("unknown operand type"); |
| 114 | case MachineOperand::MO_Register: |
| 115 | // Ignore all implicit register operands. |
| 116 | if (MO.isImplicit()) continue; |
| 117 | MCOp = MCOperand::CreateReg(MO.getReg()); |
| 118 | break; |
| 119 | case MachineOperand::MO_Immediate: |
| 120 | MCOp = MCOperand::CreateImm(MO.getImm()); |
| 121 | break; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 122 | } |
Nate Begeman | 5694b47 | 2010-01-15 18:51:18 +0000 | [diff] [blame] | 123 | OutMI.addOperand(MCOp); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 124 | } |
Nate Begeman | 5694b47 | 2010-01-15 18:51:18 +0000 | [diff] [blame] | 125 | |
| 126 | OutStreamer.EmitInstruction(OutMI); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 127 | } |
| 128 | } |
Nate Begeman | 5694b47 | 2010-01-15 18:51:18 +0000 | [diff] [blame] | 129 | |
| 130 | return false; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 131 | } |