1 //===-- MachOWriter.cpp - Target-independent Mach-O Writer code -----------===//
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 // This file implements the target-independent Mach-O writer. This file writes
11 // out the Mach-O file in the following order:
13 // #1 FatHeader (universal-only)
14 // #2 FatArch (universal-only, 1 per universal arch)
23 //===----------------------------------------------------------------------===//
25 #include "MachOWriter.h"
26 #include "llvm/Function.h"
27 #include "llvm/CodeGen/FileWriters.h"
28 #include "llvm/CodeGen/MachineFunction.h"
29 #include "llvm/MC/MCAsmInfo.h"
30 #include "llvm/MC/MCContext.h"
31 #include "llvm/MC/MCCodeEmitter.h"
32 #include "llvm/MC/MCInst.h"
33 #include "llvm/MC/MCStreamer.h"
34 #include "llvm/Support/ErrorHandling.h"
35 #include "llvm/Support/FormattedStream.h"
36 #include "llvm/Support/Mangler.h"
37 #include "llvm/Support/raw_ostream.h"
38 #include "llvm/Target/TargetData.h"
39 #include "llvm/Target/TargetLowering.h"
40 #include "llvm/Target/TargetLoweringObjectFile.h"
44 MachineFunctionPass *createMachOWriter(formatted_raw_ostream &O,
48 return new MachOWriter(O, TM, T, MCE);
52 //===----------------------------------------------------------------------===//
53 // MachOWriter Implementation
54 //===----------------------------------------------------------------------===//
56 char MachOWriter::ID = 0;
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)) {
65 MachOWriter::~MachOWriter() {
71 bool MachOWriter::doInitialization(Module &M) {
72 Mang = new Mangler(M, MAI->getGlobalPrefix(), MAI->getPrivateGlobalPrefix(),
73 MAI->getLinkerPrivateGlobalPrefix());
75 if (MAI->doesAllowQuotesInName())
76 Mang->setUseQuotes(true);
78 if (MAI->doesAllowNameToStartWithDigit())
79 Mang->setSymbolsCanStartWithDigit(true);
81 // Initialize TargetLoweringObjectFile.
82 TM.getTargetLowering()->getObjFileLowering().Initialize(OutContext, TM);
87 /// doFinalization - Now that the module has been completely processed, emit
88 /// the Mach-O file to 'O'.
89 bool MachOWriter::doFinalization(Module &M) {
90 // Release the name mangler object.
91 delete Mang; Mang = 0;
97 bool MachOWriter::runOnMachineFunction(MachineFunction &MF) {
98 const Function *F = MF.getFunction();
99 TargetLoweringObjectFile &TLOF = TM.getTargetLowering()->getObjFileLowering();
100 const MCSection *S = TLOF.SectionForGlobal(F, Mang, TM);
101 OutStreamer.SwitchSection(S);
103 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
105 // Print a label for the basic block.
106 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
108 const MachineInstr *MI = II;
110 OutMI.setOpcode(MI->getOpcode());
112 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
113 const MachineOperand &MO = MI->getOperand(i);
116 switch (MO.getType()) {
119 llvm_unreachable("unknown operand type");
120 case MachineOperand::MO_Register:
121 // Ignore all implicit register operands.
122 if (MO.isImplicit()) continue;
123 MCOp = MCOperand::CreateReg(MO.getReg());
125 case MachineOperand::MO_Immediate:
126 MCOp = MCOperand::CreateImm(MO.getImm());
129 OutMI.addOperand(MCOp);
132 OutStreamer.EmitInstruction(OutMI);