blob: 11eaf3d86198a3fa857f3fef07ddf696c2d1cc29 [file] [log] [blame]
Misha Brukman3d9a6c22004-08-11 00:09:42 +00001//===-- PPC32AsmPrinter.cpp - Print machine instrs to PowerPC assembly ----===//
Misha Brukman5dfe3a92004-06-21 16:55:25 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Misha Brukman05fcd0c2004-07-08 17:58:04 +000010// This file contains a printer that converts from our internal representation
11// of machine-dependent LLVM code to PowerPC assembly language. This printer is
Chris Lattner83660c52004-07-28 20:18:53 +000012// the output mechanism used by `llc'.
Misha Brukman5dfe3a92004-06-21 16:55:25 +000013//
Misha Brukman05fcd0c2004-07-08 17:58:04 +000014// Documentation at http://developer.apple.com/documentation/DeveloperTools/
15// Reference/Assembler/ASMIntroduction/chapter_1_section_1.html
Misha Brukman218bec72004-06-29 17:13:26 +000016//
Misha Brukman5dfe3a92004-06-21 16:55:25 +000017//===----------------------------------------------------------------------===//
18
Misha Brukman05794492004-06-24 17:31:42 +000019#define DEBUG_TYPE "asmprinter"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000020#include "PowerPC.h"
Misha Brukmanf2ccb772004-08-17 04:55:41 +000021#include "PPC32TargetMachine.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000022#include "llvm/Constants.h"
23#include "llvm/DerivedTypes.h"
24#include "llvm/Module.h"
25#include "llvm/Assembly/Writer.h"
Chris Lattnera3840792004-08-16 23:25:21 +000026#include "llvm/CodeGen/AsmPrinter.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000027#include "llvm/CodeGen/MachineConstantPool.h"
Misha Brukman05794492004-06-24 17:31:42 +000028#include "llvm/CodeGen/MachineFunctionPass.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000029#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner7bb424f2004-08-14 23:27:29 +000030#include "llvm/CodeGen/ValueTypes.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000031#include "llvm/Support/Mangler.h"
Misha Brukman05794492004-06-24 17:31:42 +000032#include "Support/CommandLine.h"
33#include "Support/Debug.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000034#include "Support/Statistic.h"
35#include "Support/StringExtras.h"
Misha Brukman05794492004-06-24 17:31:42 +000036#include <set>
Chris Lattnera3840792004-08-16 23:25:21 +000037using namespace llvm;
Misha Brukman5dfe3a92004-06-21 16:55:25 +000038
39namespace {
40 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
41
Misha Brukmanf2ccb772004-08-17 04:55:41 +000042 struct PPC32AsmPrinter : public AsmPrinter {
Misha Brukman97a296f2004-07-21 20:11:11 +000043 std::set<std::string> FnStubs, GVStubs, LinkOnceStubs;
Misha Brukman5dfe3a92004-06-21 16:55:25 +000044 std::set<std::string> Strings;
45
Misha Brukmanf2ccb772004-08-17 04:55:41 +000046 PPC32AsmPrinter(std::ostream &O, TargetMachine &TM)
Chris Lattner505e7832004-08-17 02:29:00 +000047 : AsmPrinter(O, TM), LabelNumber(0) {
Chris Lattnerb462e472004-08-17 06:07:43 +000048 GlobalPrefix = "_";
Chris Lattner79835d92004-08-17 06:37:12 +000049 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
50 Data64bitsDirective = 0; // we can't emit a 64-bit unit
Chris Lattner505e7832004-08-17 02:29:00 +000051 }
Misha Brukman5dfe3a92004-06-21 16:55:25 +000052
Misha Brukmancf8d2442004-07-26 16:28:33 +000053 /// Unique incrementer for label values for referencing Global values.
Misha Brukman218bec72004-06-29 17:13:26 +000054 ///
Misha Brukmancf8d2442004-07-26 16:28:33 +000055 unsigned LabelNumber;
56
Misha Brukman5dfe3a92004-06-21 16:55:25 +000057 virtual const char *getPassName() const {
Misha Brukmanf2ccb772004-08-17 04:55:41 +000058 return "PPC32 Assembly Printer";
Misha Brukman5dfe3a92004-06-21 16:55:25 +000059 }
60
Misha Brukmanf2ccb772004-08-17 04:55:41 +000061 PPC32TargetMachine &getTM() {
62 return static_cast<PPC32TargetMachine&>(TM);
Chris Lattnera3840792004-08-16 23:25:21 +000063 }
64
Nate Begemane59bf592004-08-14 22:09:10 +000065 /// printInstruction - This method is automatically generated by tablegen
66 /// from the instruction set description. This method returns true if the
67 /// machine instruction was sufficiently described to print it, otherwise it
68 /// returns false.
69 bool printInstruction(const MachineInstr *MI);
70
Misha Brukman5dfe3a92004-06-21 16:55:25 +000071 void printMachineInstruction(const MachineInstr *MI);
Nate Begemanb73a7112004-08-13 09:32:01 +000072 void printOp(const MachineOperand &MO, bool LoadAddrOp = false);
Misha Brukmanaf313fb2004-07-28 00:00:48 +000073 void printImmOp(const MachineOperand &MO, unsigned ArgType);
Chris Lattner7bb424f2004-08-14 23:27:29 +000074
75 void printOperand(const MachineInstr *MI, unsigned OpNo, MVT::ValueType VT){
76 const MachineOperand &MO = MI->getOperand(OpNo);
77 if (MO.getType() == MachineOperand::MO_MachineRegister) {
78 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
79 O << LowercaseString(TM.getRegisterInfo()->get(MO.getReg()).Name);
Chris Lattner0ea31712004-08-15 05:46:14 +000080 } else if (MO.isImmediate()) {
81 O << MO.getImmedValue();
Chris Lattner7bb424f2004-08-14 23:27:29 +000082 } else {
83 printOp(MO);
84 }
85 }
86
Chris Lattner97b2a2e2004-08-15 05:20:16 +000087 void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo,
88 MVT::ValueType VT) {
89 O << (unsigned short)MI->getOperand(OpNo).getImmedValue();
90 }
91
Misha Brukman5dfe3a92004-06-21 16:55:25 +000092 void printConstantPool(MachineConstantPool *MCP);
93 bool runOnMachineFunction(MachineFunction &F);
Misha Brukman5dfe3a92004-06-21 16:55:25 +000094 bool doFinalization(Module &M);
Misha Brukman5dfe3a92004-06-21 16:55:25 +000095 };
96} // end of anonymous namespace
97
Misha Brukman3d9a6c22004-08-11 00:09:42 +000098/// createPPC32AsmPrinterPass - Returns a pass that prints the PPC
Misha Brukman5dfe3a92004-06-21 16:55:25 +000099/// assembly code for a MachineFunction to the given output stream,
100/// using the given target machine description. This should work
Misha Brukman7103fba2004-08-09 22:27:45 +0000101/// regardless of whether the function is in SSA form or not.
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000102///
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000103FunctionPass *llvm::createPPC32AsmPrinter(std::ostream &o, TargetMachine &tm) {
104 return new PPC32AsmPrinter(o, tm);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000105}
106
Nate Begemane59bf592004-08-14 22:09:10 +0000107// Include the auto-generated portion of the assembly writer
108#include "PowerPCGenAsmWriter.inc"
109
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000110/// printConstantPool - Print to the current output stream assembly
111/// representations of the constants in the constant pool MCP. This is
112/// used to print out constants which have been "spilled to memory" by
113/// the code generator.
114///
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000115void PPC32AsmPrinter::printConstantPool(MachineConstantPool *MCP) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000116 const std::vector<Constant*> &CP = MCP->getConstants();
117 const TargetData &TD = TM.getTargetData();
118
119 if (CP.empty()) return;
120
121 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
122 O << "\t.const\n";
123 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
124 << "\n";
Misha Brukman218bec72004-06-29 17:13:26 +0000125 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t;"
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000126 << *CP[i] << "\n";
127 emitGlobalConstant(CP[i]);
128 }
129}
130
131/// runOnMachineFunction - This uses the printMachineInstruction()
132/// method to print assembly for each instruction.
133///
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000134bool PPC32AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattnera3840792004-08-16 23:25:21 +0000135 setupMachineFunction(MF);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000136 O << "\n\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000137
138 // Print out constants referenced by the function
139 printConstantPool(MF.getConstantPool());
140
141 // Print out labels for the function.
142 O << "\t.text\n";
143 O << "\t.globl\t" << CurrentFnName << "\n";
Misha Brukman61297ee2004-06-29 23:40:57 +0000144 O << "\t.align 2\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000145 O << CurrentFnName << ":\n";
146
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000147 // Print out code for the function.
148 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
149 I != E; ++I) {
150 // Print a label for the basic block.
Misha Brukman218bec72004-06-29 17:13:26 +0000151 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t; "
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000152 << I->getBasicBlock()->getName() << "\n";
153 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
Misha Brukman46fd00a2004-06-24 23:04:11 +0000154 II != E; ++II) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000155 // Print the assembly for the instruction.
156 O << "\t";
157 printMachineInstruction(II);
158 }
159 }
Misha Brukmancf8d2442004-07-26 16:28:33 +0000160 ++LabelNumber;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000161
162 // We didn't modify anything.
163 return false;
164}
165
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000166void PPC32AsmPrinter::printOp(const MachineOperand &MO,
167 bool LoadAddrOp /* = false */) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000168 const MRegisterInfo &RI = *TM.getRegisterInfo();
169 int new_symbol;
170
171 switch (MO.getType()) {
172 case MachineOperand::MO_VirtualRegister:
173 if (Value *V = MO.getVRegValueOrNull()) {
174 O << "<" << V->getName() << ">";
175 return;
176 }
177 // FALLTHROUGH
178 case MachineOperand::MO_MachineRegister:
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000179 case MachineOperand::MO_CCRegister:
Misha Brukman7f484a52004-06-24 23:51:00 +0000180 O << LowercaseString(RI.get(MO.getReg()).Name);
181 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000182
183 case MachineOperand::MO_SignExtendedImmed:
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000184 case MachineOperand::MO_UnextendedImmed:
185 std::cerr << "printOp() does not handle immediate values\n";
186 abort();
Misha Brukman97a296f2004-07-21 20:11:11 +0000187 return;
188
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000189 case MachineOperand::MO_PCRelativeDisp:
190 std::cerr << "Shouldn't use addPCDisp() when building PPC MachineInstrs";
191 abort();
192 return;
193
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000194 case MachineOperand::MO_MachineBasicBlock: {
195 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
196 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
Misha Brukman218bec72004-06-29 17:13:26 +0000197 << "_" << MBBOp->getNumber() << "\t; "
Misha Brukman2bf183c2004-06-25 15:42:10 +0000198 << MBBOp->getBasicBlock()->getName();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000199 return;
200 }
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000201
202 case MachineOperand::MO_ConstantPoolIndex:
203 O << ".CPI" << CurrentFnName << "_" << MO.getConstantPoolIndex();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000204 return;
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000205
206 case MachineOperand::MO_ExternalSymbol:
207 O << MO.getSymbolName();
208 return;
209
Nate Begemanb73a7112004-08-13 09:32:01 +0000210 case MachineOperand::MO_GlobalAddress: {
211 GlobalValue *GV = MO.getGlobal();
212 std::string Name = Mang->getValueName(GV);
Misha Brukmane2eceb52004-07-23 16:08:20 +0000213
Nate Begemanb73a7112004-08-13 09:32:01 +0000214 // Dynamically-resolved functions need a stub for the function. Be
215 // wary however not to output $stub for external functions whose addresses
216 // are taken. Those should be emitted as $non_lazy_ptr below.
217 Function *F = dyn_cast<Function>(GV);
218 if (F && F->isExternal() && !LoadAddrOp &&
Chris Lattnera3840792004-08-16 23:25:21 +0000219 getTM().CalledFunctions.count(F)) {
Nate Begemanb73a7112004-08-13 09:32:01 +0000220 FnStubs.insert(Name);
221 O << "L" << Name << "$stub";
222 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000223 }
Nate Begemane59bf592004-08-14 22:09:10 +0000224
Nate Begemanb73a7112004-08-13 09:32:01 +0000225 // External global variables need a non-lazily-resolved stub
Chris Lattnera3840792004-08-16 23:25:21 +0000226 if (GV->isExternal() && getTM().AddressTaken.count(GV)) {
Nate Begemanb73a7112004-08-13 09:32:01 +0000227 GVStubs.insert(Name);
228 O << "L" << Name << "$non_lazy_ptr";
229 return;
230 }
Nate Begemane59bf592004-08-14 22:09:10 +0000231
Chris Lattnera3840792004-08-16 23:25:21 +0000232 if (F && LoadAddrOp && getTM().AddressTaken.count(GV)) {
Nate Begemane59bf592004-08-14 22:09:10 +0000233 LinkOnceStubs.insert(Name);
234 O << "L" << Name << "$non_lazy_ptr";
235 return;
236 }
Nate Begemanb73a7112004-08-13 09:32:01 +0000237
238 O << Mang->getValueName(GV);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000239 return;
Nate Begemanb73a7112004-08-13 09:32:01 +0000240 }
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000241
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000242 default:
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000243 O << "<unknown operand type: " << MO.getType() << ">";
Misha Brukman22e12072004-06-25 15:11:34 +0000244 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000245 }
246}
247
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000248void PPC32AsmPrinter::printImmOp(const MachineOperand &MO, unsigned ArgType) {
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000249 int Imm = MO.getImmedValue();
Misha Brukman5b570812004-08-10 22:47:03 +0000250 if (ArgType == PPCII::Simm16 || ArgType == PPCII::Disimm16) {
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000251 O << (short)Imm;
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000252 } else {
253 O << Imm;
254 }
255}
256
Nate Begemane59bf592004-08-14 22:09:10 +0000257/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
258/// the current output stream.
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000259///
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000260void PPC32AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Nate Begemane59bf592004-08-14 22:09:10 +0000261 ++EmittedInsts;
262 if (printInstruction(MI))
263 return; // Printer was automatically generated
264
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000265 unsigned Opcode = MI->getOpcode();
266 const TargetInstrInfo &TII = *TM.getInstrInfo();
267 const TargetInstrDescriptor &Desc = TII.get(Opcode);
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000268 unsigned i;
Misha Brukmanc6cc10f2004-06-25 19:24:52 +0000269
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000270 unsigned ArgCount = MI->getNumOperands();
271 unsigned ArgType[] = {
Misha Brukman5b570812004-08-10 22:47:03 +0000272 (Desc.TSFlags >> PPCII::Arg0TypeShift) & PPCII::ArgTypeMask,
273 (Desc.TSFlags >> PPCII::Arg1TypeShift) & PPCII::ArgTypeMask,
274 (Desc.TSFlags >> PPCII::Arg2TypeShift) & PPCII::ArgTypeMask,
275 (Desc.TSFlags >> PPCII::Arg3TypeShift) & PPCII::ArgTypeMask,
276 (Desc.TSFlags >> PPCII::Arg4TypeShift) & PPCII::ArgTypeMask
Misha Brukman22e12072004-06-25 15:11:34 +0000277 };
Misha Brukman5b570812004-08-10 22:47:03 +0000278 assert(((Desc.TSFlags & PPCII::VMX) == 0) &&
Misha Brukman46fd00a2004-06-24 23:04:11 +0000279 "Instruction requires VMX support");
Misha Brukman5b570812004-08-10 22:47:03 +0000280 assert(((Desc.TSFlags & PPCII::PPC64) == 0) &&
Misha Brukman46fd00a2004-06-24 23:04:11 +0000281 "Instruction requires 64 bit support");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000282
Misha Brukman61114612004-07-20 00:42:19 +0000283 // CALLpcrel and CALLindirect are handled specially here to print only the
284 // appropriate number of args that the assembler expects. This is because
285 // may have many arguments appended to record the uses of registers that are
286 // holding arguments to the called function.
Misha Brukman5b570812004-08-10 22:47:03 +0000287 if (Opcode == PPC::COND_BRANCH) {
Misha Brukmanab967902004-07-27 18:40:39 +0000288 std::cerr << "Error: untranslated conditional branch psuedo instruction!\n";
289 abort();
Misha Brukman5b570812004-08-10 22:47:03 +0000290 } else if (Opcode == PPC::IMPLICIT_DEF) {
Misha Brukman29188c62004-07-16 19:01:13 +0000291 O << "; IMPLICIT DEF ";
292 printOp(MI->getOperand(0));
293 O << "\n";
294 return;
Misha Brukman5b570812004-08-10 22:47:03 +0000295 } else if (Opcode == PPC::CALLpcrel) {
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000296 O << TII.getName(Opcode) << " ";
Misha Brukman61114612004-07-20 00:42:19 +0000297 printOp(MI->getOperand(0));
298 O << "\n";
299 return;
Misha Brukman5b570812004-08-10 22:47:03 +0000300 } else if (Opcode == PPC::CALLindirect) {
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000301 O << TII.getName(Opcode) << " ";
302 printImmOp(MI->getOperand(0), ArgType[0]);
Misha Brukman61114612004-07-20 00:42:19 +0000303 O << ", ";
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000304 printImmOp(MI->getOperand(1), ArgType[0]);
Misha Brukman61114612004-07-20 00:42:19 +0000305 O << "\n";
306 return;
Misha Brukman5b570812004-08-10 22:47:03 +0000307 } else if (Opcode == PPC::MovePCtoLR) {
Misha Brukman61114612004-07-20 00:42:19 +0000308 // FIXME: should probably be converted to cout.width and cout.fill
Misha Brukmancf8d2442004-07-26 16:28:33 +0000309 O << "bl \"L0000" << LabelNumber << "$pb\"\n";
310 O << "\"L0000" << LabelNumber << "$pb\":\n";
Misha Brukman218bec72004-06-29 17:13:26 +0000311 O << "\tmflr ";
312 printOp(MI->getOperand(0));
Misha Brukman218bec72004-06-29 17:13:26 +0000313 O << "\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000314 return;
315 }
316
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000317 O << TII.getName(Opcode) << " ";
Misha Brukman5b570812004-08-10 22:47:03 +0000318 if (Opcode == PPC::LOADLoDirect || Opcode == PPC::LOADLoIndirect) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000319 printOp(MI->getOperand(0));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000320 O << ", lo16(";
Nate Begemanb73a7112004-08-13 09:32:01 +0000321 printOp(MI->getOperand(2), true /* LoadAddrOp */);
Misha Brukmancf8d2442004-07-26 16:28:33 +0000322 O << "-\"L0000" << LabelNumber << "$pb\")";
Misha Brukman218bec72004-06-29 17:13:26 +0000323 O << "(";
Misha Brukman5b570812004-08-10 22:47:03 +0000324 if (MI->getOperand(1).getReg() == PPC::R0)
Misha Brukman218bec72004-06-29 17:13:26 +0000325 O << "0";
326 else
327 printOp(MI->getOperand(1));
328 O << ")\n";
Misha Brukman5b570812004-08-10 22:47:03 +0000329 } else if (Opcode == PPC::LOADHiAddr) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000330 printOp(MI->getOperand(0));
331 O << ", ";
Misha Brukman5b570812004-08-10 22:47:03 +0000332 if (MI->getOperand(1).getReg() == PPC::R0)
Misha Brukman218bec72004-06-29 17:13:26 +0000333 O << "0";
334 else
335 printOp(MI->getOperand(1));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000336 O << ", ha16(" ;
Nate Begemanb73a7112004-08-13 09:32:01 +0000337 printOp(MI->getOperand(2), true /* LoadAddrOp */);
Misha Brukmancf8d2442004-07-26 16:28:33 +0000338 O << "-\"L0000" << LabelNumber << "$pb\")\n";
Misha Brukman5b570812004-08-10 22:47:03 +0000339 } else if (ArgCount == 3 && ArgType[1] == PPCII::Disimm16) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000340 printOp(MI->getOperand(0));
341 O << ", ";
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000342 printImmOp(MI->getOperand(1), ArgType[1]);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000343 O << "(";
Misha Brukmanb9e8f972004-06-30 21:54:12 +0000344 if (MI->getOperand(2).hasAllocatedReg() &&
Misha Brukman5b570812004-08-10 22:47:03 +0000345 MI->getOperand(2).getReg() == PPC::R0)
Misha Brukman46fd00a2004-06-24 23:04:11 +0000346 O << "0";
347 else
348 printOp(MI->getOperand(2));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000349 O << ")\n";
350 } else {
Misha Brukman7f484a52004-06-24 23:51:00 +0000351 for (i = 0; i < ArgCount; ++i) {
Misha Brukmanab967902004-07-27 18:40:39 +0000352 // addi and friends
Misha Brukman5b570812004-08-10 22:47:03 +0000353 if (i == 1 && ArgCount == 3 && ArgType[2] == PPCII::Simm16 &&
Misha Brukman4363bdb2004-07-01 21:09:12 +0000354 MI->getOperand(1).hasAllocatedReg() &&
Misha Brukman5b570812004-08-10 22:47:03 +0000355 MI->getOperand(1).getReg() == PPC::R0) {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000356 O << "0";
Misha Brukmanab967902004-07-27 18:40:39 +0000357 // for long branch support, bc $+8
358 } else if (i == 1 && ArgCount == 2 && MI->getOperand(1).isImmediate() &&
359 TII.isBranch(MI->getOpcode())) {
360 O << "$+8";
361 assert(8 == MI->getOperand(i).getImmedValue()
362 && "branch off PC not to pc+8?");
363 //printOp(MI->getOperand(i));
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000364 } else if (MI->getOperand(i).isImmediate()) {
365 printImmOp(MI->getOperand(i), ArgType[i]);
Misha Brukman218bec72004-06-29 17:13:26 +0000366 } else {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000367 printOp(MI->getOperand(i));
368 }
Misha Brukman7f484a52004-06-24 23:51:00 +0000369 if (ArgCount - 1 == i)
Misha Brukman46fd00a2004-06-24 23:04:11 +0000370 O << "\n";
371 else
372 O << ", ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000373 }
374 }
Nate Begemane59bf592004-08-14 22:09:10 +0000375 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000376}
377
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000378// SwitchSection - Switch to the specified section of the executable if we are
379// not already in it!
380//
381static void SwitchSection(std::ostream &OS, std::string &CurSection,
382 const char *NewSection) {
383 if (CurSection != NewSection) {
384 CurSection = NewSection;
385 if (!CurSection.empty())
386 OS << "\t" << NewSection << "\n";
387 }
388}
389
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000390bool PPC32AsmPrinter::doFinalization(Module &M) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000391 const TargetData &TD = TM.getTargetData();
392 std::string CurSection;
393
394 // Print out module-level global variables here.
395 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
396 if (I->hasInitializer()) { // External global require no code
397 O << "\n\n";
398 std::string name = Mang->getValueName(I);
399 Constant *C = I->getInitializer();
400 unsigned Size = TD.getTypeSize(C->getType());
401 unsigned Align = TD.getTypeAlignment(C->getType());
402
Misha Brukman97a296f2004-07-21 20:11:11 +0000403 if (C->isNullValue() && /* FIXME: Verify correct */
404 (I->hasInternalLinkage() || I->hasWeakLinkage())) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000405 SwitchSection(O, CurSection, ".data");
406 if (I->hasInternalLinkage())
Misha Brukmane2eceb52004-07-23 16:08:20 +0000407 O << ".lcomm " << name << "," << TD.getTypeSize(C->getType())
Misha Brukman218bec72004-06-29 17:13:26 +0000408 << "," << (unsigned)TD.getTypeAlignment(C->getType());
409 else
Misha Brukmane2eceb52004-07-23 16:08:20 +0000410 O << ".comm " << name << "," << TD.getTypeSize(C->getType());
Misha Brukman218bec72004-06-29 17:13:26 +0000411 O << "\t\t; ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000412 WriteAsOperand(O, I, true, true, &M);
413 O << "\n";
414 } else {
415 switch (I->getLinkage()) {
416 case GlobalValue::LinkOnceLinkage:
Misha Brukman97a296f2004-07-21 20:11:11 +0000417 O << ".section __TEXT,__textcoal_nt,coalesced,no_toc\n"
418 << ".weak_definition " << name << '\n'
419 << ".private_extern " << name << '\n'
420 << ".section __DATA,__datacoal_nt,coalesced,no_toc\n";
421 LinkOnceStubs.insert(name);
422 break;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000423 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
424 // Nonnull linkonce -> weak
425 O << "\t.weak " << name << "\n";
426 SwitchSection(O, CurSection, "");
427 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
428 break;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000429 case GlobalValue::AppendingLinkage:
430 // FIXME: appending linkage variables should go into a section of
431 // their name or something. For now, just emit them as external.
432 case GlobalValue::ExternalLinkage:
433 // If external or appending, declare as a global symbol
434 O << "\t.globl " << name << "\n";
435 // FALL THROUGH
436 case GlobalValue::InternalLinkage:
Misha Brukman61297ee2004-06-29 23:40:57 +0000437 SwitchSection(O, CurSection, ".data");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000438 break;
439 }
440
441 O << "\t.align " << Align << "\n";
Misha Brukman218bec72004-06-29 17:13:26 +0000442 O << name << ":\t\t\t\t; ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000443 WriteAsOperand(O, I, true, true, &M);
444 O << " = ";
445 WriteAsOperand(O, C, false, false, &M);
446 O << "\n";
447 emitGlobalConstant(C);
448 }
449 }
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000450
451 // Output stubs for dynamically-linked functions
452 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
453 i != e; ++i)
Misha Brukman46fd00a2004-06-24 23:04:11 +0000454 {
Misha Brukmane2eceb52004-07-23 16:08:20 +0000455 O << ".data\n";
456 O << ".section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32\n";
457 O << "\t.align 2\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000458 O << "L" << *i << "$stub:\n";
459 O << "\t.indirect_symbol " << *i << "\n";
460 O << "\tmflr r0\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000461 O << "\tbcl 20,31,L0$" << *i << "\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000462 O << "L0$" << *i << ":\n";
463 O << "\tmflr r11\n";
464 O << "\taddis r11,r11,ha16(L" << *i << "$lazy_ptr-L0$" << *i << ")\n";
465 O << "\tmtlr r0\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000466 O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr-L0$" << *i << ")(r11)\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000467 O << "\tmtctr r12\n";
468 O << "\tbctr\n";
469 O << ".data\n";
470 O << ".lazy_symbol_pointer\n";
471 O << "L" << *i << "$lazy_ptr:\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000472 O << "\t.indirect_symbol " << *i << "\n";
473 O << "\t.long dyld_stub_binding_helper\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000474 }
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000475
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000476 O << "\n";
477
478 // Output stubs for external global variables
479 if (GVStubs.begin() != GVStubs.end())
Misha Brukmane2eceb52004-07-23 16:08:20 +0000480 O << ".data\n.non_lazy_symbol_pointer\n";
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000481 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
482 i != e; ++i) {
483 O << "L" << *i << "$non_lazy_ptr:\n";
484 O << "\t.indirect_symbol " << *i << "\n";
485 O << "\t.long\t0\n";
486 }
487
Nate Begemane59bf592004-08-14 22:09:10 +0000488 // Output stubs for link-once variables
489 if (LinkOnceStubs.begin() != LinkOnceStubs.end())
490 O << ".data\n.align 2\n";
491 for (std::set<std::string>::iterator i = LinkOnceStubs.begin(),
492 e = LinkOnceStubs.end(); i != e; ++i) {
493 O << "L" << *i << "$non_lazy_ptr:\n"
494 << "\t.long\t" << *i << '\n';
495 }
496
Chris Lattnera3840792004-08-16 23:25:21 +0000497 AsmPrinter::doFinalization(M);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000498 return false; // success
499}