Use AssertingVH, just to be paranoid.
[oota-llvm.git] / lib / CodeGen / MachOWriter.cpp
1 //===-- MachOWriter.cpp - Target-independent Mach-O Writer code -----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
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"
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/raw_ostream.h"
37 #include "llvm/Target/Mangler.h"
38 #include "llvm/Target/TargetData.h"
39 #include "llvm/Target/TargetLowering.h"
40 #include "llvm/Target/TargetLoweringObjectFile.h"
41 using namespace llvm;
42
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 }
50 }
51
52 //===----------------------------------------------------------------------===//
53 //                          MachOWriter Implementation
54 //===----------------------------------------------------------------------===//
55
56 char MachOWriter::ID = 0;
57
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)) { 
63 }
64
65 MachOWriter::~MachOWriter() {
66   delete &OutStreamer;
67   delete &OutContext;
68   delete MCCE;
69 }
70
71 bool MachOWriter::doInitialization(Module &M) {
72   // Initialize TargetLoweringObjectFile.
73   TM.getTargetLowering()->getObjFileLowering().Initialize(OutContext, TM);
74
75   return false;
76 }
77
78 /// doFinalization - Now that the module has been completely processed, emit
79 /// the Mach-O file to 'O'.
80 bool MachOWriter::doFinalization(Module &M) {
81   OutStreamer.Finish();
82   return false;
83 }
84
85 bool MachOWriter::runOnMachineFunction(MachineFunction &MF) {
86   const Function *F = MF.getFunction();
87   TargetLoweringObjectFile &TLOF = TM.getTargetLowering()->getObjFileLowering();
88   const MCSection *S = TLOF.SectionForGlobal(F, Mang, TM);
89   OutStreamer.SwitchSection(S);
90
91   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
92        I != E; ++I) {
93     // Print a label for the basic block.
94     for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
95          II != IE; ++II) {
96       const MachineInstr *MI = II;
97       MCInst OutMI;
98       OutMI.setOpcode(MI->getOpcode());
99
100       for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
101         const MachineOperand &MO = MI->getOperand(i);
102         MCOperand MCOp;
103
104         switch (MO.getType()) {
105           default:
106             MI->dump();
107             llvm_unreachable("unknown operand type");
108           case MachineOperand::MO_Register:
109             // Ignore all implicit register operands.
110             if (MO.isImplicit()) continue;
111             MCOp = MCOperand::CreateReg(MO.getReg());
112             break;
113           case MachineOperand::MO_Immediate:
114             MCOp = MCOperand::CreateImm(MO.getImm());
115             break;
116         }
117         OutMI.addOperand(MCOp);
118       }
119       
120       OutStreamer.EmitInstruction(OutMI);
121     }
122   }
123
124   return false;
125 }