blob: cab71cea0d52d648203b788c552a576aa66d9693 [file] [log] [blame]
Nate Begemaneb883af2006-08-23 21:08:52 +00001//===-- MachOWriter.cpp - Target-independent Mach-O Writer code -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Nate Begemaneb883af2006-08-23 21:08:52 +00007//
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
Bill Wendling8f84f1f2007-02-08 01:35:27 +000025#include "MachOWriter.h"
Nate Begeman3fe980b2010-01-15 18:51:18 +000026#include "llvm/Function.h"
27#include "llvm/CodeGen/FileWriters.h"
28#include "llvm/CodeGen/MachineFunction.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000029#include "llvm/MC/MCAsmInfo.h"
Nate Begeman3fe980b2010-01-15 18:51:18 +000030#include "llvm/MC/MCContext.h"
31#include "llvm/MC/MCCodeEmitter.h"
32#include "llvm/MC/MCInst.h"
33#include "llvm/MC/MCStreamer.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000034#include "llvm/Support/ErrorHandling.h"
Nate Begeman3fe980b2010-01-15 18:51:18 +000035#include "llvm/Support/FormattedStream.h"
36#include "llvm/Support/Mangler.h"
Owen Andersoncb371882008-08-21 00:14:44 +000037#include "llvm/Support/raw_ostream.h"
Nate Begeman3fe980b2010-01-15 18:51:18 +000038#include "llvm/Target/TargetData.h"
39#include "llvm/Target/TargetLowering.h"
40#include "llvm/Target/TargetLoweringObjectFile.h"
41using namespace llvm;
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000042
Nate Begeman3fe980b2010-01-15 18:51:18 +000043namespace llvm {
44MachineFunctionPass *createMachOWriter(formatted_raw_ostream &O,
45 TargetMachine &TM,
46 const MCAsmInfo *T,
47 MCCodeEmitter *MCE) {
48 return new MachOWriter(O, TM, T, MCE);
49}
Bill Wendling8f84f1f2007-02-08 01:35:27 +000050}
51
Nate Begemaneb883af2006-08-23 21:08:52 +000052//===----------------------------------------------------------------------===//
Nate Begemaneb883af2006-08-23 21:08:52 +000053// MachOWriter Implementation
54//===----------------------------------------------------------------------===//
55
Devang Patel19974732007-05-03 01:11:54 +000056char MachOWriter::ID = 0;
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000057
Nate Begeman3fe980b2010-01-15 18:51:18 +000058MachOWriter::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)) {
Nate Begemaneb883af2006-08-23 21:08:52 +000063}
64
65MachOWriter::~MachOWriter() {
Nate Begeman3fe980b2010-01-15 18:51:18 +000066 delete &OutStreamer;
67 delete &OutContext;
68 delete MCCE;
Nate Begemaneb883af2006-08-23 21:08:52 +000069}
70
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000071bool MachOWriter::doInitialization(Module &M) {
Nate Begeman3fe980b2010-01-15 18:51:18 +000072 Mang = new Mangler(M, MAI->getGlobalPrefix(), MAI->getPrivateGlobalPrefix(),
73 MAI->getLinkerPrivateGlobalPrefix());
74
75 if (MAI->doesAllowQuotesInName())
76 Mang->setUseQuotes(true);
77
78 if (MAI->doesAllowNameToStartWithDigit())
79 Mang->setSymbolsCanStartWithDigit(true);
80
81 // Initialize TargetLoweringObjectFile.
82 TM.getTargetLowering()->getObjFileLowering().Initialize(OutContext, TM);
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000083
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000084 return false;
85}
86
87/// doFinalization - Now that the module has been completely processed, emit
88/// the Mach-O file to 'O'.
89bool MachOWriter::doFinalization(Module &M) {
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000090 // Release the name mangler object.
91 delete Mang; Mang = 0;
Nate Begeman3fe980b2010-01-15 18:51:18 +000092
93 OutStreamer.Finish();
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000094 return false;
95}
96
Nate Begeman3fe980b2010-01-15 18:51:18 +000097bool 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);
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000102
Nate Begeman3fe980b2010-01-15 18:51:18 +0000103 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
104 I != E; ++I) {
105 // Print a label for the basic block.
106 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
107 II != IE; ++II) {
108 const MachineInstr *MI = II;
109 MCInst OutMI;
110 OutMI.setOpcode(MI->getOpcode());
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000111
Nate Begeman3fe980b2010-01-15 18:51:18 +0000112 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
113 const MachineOperand &MO = MI->getOperand(i);
114 MCOperand MCOp;
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000115
Nate Begeman3fe980b2010-01-15 18:51:18 +0000116 switch (MO.getType()) {
117 default:
118 MI->dump();
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());
124 break;
125 case MachineOperand::MO_Immediate:
126 MCOp = MCOperand::CreateImm(MO.getImm());
127 break;
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000128 }
Nate Begeman3fe980b2010-01-15 18:51:18 +0000129 OutMI.addOperand(MCOp);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000130 }
Nate Begeman3fe980b2010-01-15 18:51:18 +0000131
132 OutStreamer.EmitInstruction(OutMI);
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000133 }
134 }
Nate Begeman3fe980b2010-01-15 18:51:18 +0000135
136 return false;
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000137}