blob: 07aaa49be5976c39d666b6a8c4c7d3b6052a3aa2 [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 Lattnerf746a7d2004-08-18 02:22:55 +000048 CommentString = ";";
Chris Lattnerb462e472004-08-17 06:07:43 +000049 GlobalPrefix = "_";
Chris Lattner79835d92004-08-17 06:37:12 +000050 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
51 Data64bitsDirective = 0; // we can't emit a 64-bit unit
Chris Lattner69d485e2004-08-17 19:26:03 +000052 AlignmentIsInBytes = false; // Alignment is by power of 2.
Chris Lattner505e7832004-08-17 02:29:00 +000053 }
Misha Brukman5dfe3a92004-06-21 16:55:25 +000054
Misha Brukmancf8d2442004-07-26 16:28:33 +000055 /// Unique incrementer for label values for referencing Global values.
Misha Brukman218bec72004-06-29 17:13:26 +000056 ///
Misha Brukmancf8d2442004-07-26 16:28:33 +000057 unsigned LabelNumber;
58
Misha Brukman5dfe3a92004-06-21 16:55:25 +000059 virtual const char *getPassName() const {
Misha Brukmanf2ccb772004-08-17 04:55:41 +000060 return "PPC32 Assembly Printer";
Misha Brukman5dfe3a92004-06-21 16:55:25 +000061 }
62
Misha Brukmanf2ccb772004-08-17 04:55:41 +000063 PPC32TargetMachine &getTM() {
64 return static_cast<PPC32TargetMachine&>(TM);
Chris Lattnera3840792004-08-16 23:25:21 +000065 }
66
Nate Begemane59bf592004-08-14 22:09:10 +000067 /// printInstruction - This method is automatically generated by tablegen
68 /// from the instruction set description. This method returns true if the
69 /// machine instruction was sufficiently described to print it, otherwise it
70 /// returns false.
71 bool printInstruction(const MachineInstr *MI);
72
Misha Brukman5dfe3a92004-06-21 16:55:25 +000073 void printMachineInstruction(const MachineInstr *MI);
Nate Begemanb73a7112004-08-13 09:32:01 +000074 void printOp(const MachineOperand &MO, bool LoadAddrOp = false);
Misha Brukmanaf313fb2004-07-28 00:00:48 +000075 void printImmOp(const MachineOperand &MO, unsigned ArgType);
Chris Lattner7bb424f2004-08-14 23:27:29 +000076
77 void printOperand(const MachineInstr *MI, unsigned OpNo, MVT::ValueType VT){
78 const MachineOperand &MO = MI->getOperand(OpNo);
79 if (MO.getType() == MachineOperand::MO_MachineRegister) {
80 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
81 O << LowercaseString(TM.getRegisterInfo()->get(MO.getReg()).Name);
Chris Lattner0ea31712004-08-15 05:46:14 +000082 } else if (MO.isImmediate()) {
83 O << MO.getImmedValue();
Chris Lattner7bb424f2004-08-14 23:27:29 +000084 } else {
85 printOp(MO);
86 }
87 }
88
Chris Lattner97b2a2e2004-08-15 05:20:16 +000089 void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo,
90 MVT::ValueType VT) {
91 O << (unsigned short)MI->getOperand(OpNo).getImmedValue();
92 }
93
Misha Brukman5dfe3a92004-06-21 16:55:25 +000094 void printConstantPool(MachineConstantPool *MCP);
95 bool runOnMachineFunction(MachineFunction &F);
Misha Brukman5dfe3a92004-06-21 16:55:25 +000096 bool doFinalization(Module &M);
Misha Brukman5dfe3a92004-06-21 16:55:25 +000097 };
98} // end of anonymous namespace
99
Misha Brukman3d9a6c22004-08-11 00:09:42 +0000100/// createPPC32AsmPrinterPass - Returns a pass that prints the PPC
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000101/// assembly code for a MachineFunction to the given output stream,
102/// using the given target machine description. This should work
Misha Brukman7103fba2004-08-09 22:27:45 +0000103/// regardless of whether the function is in SSA form or not.
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000104///
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000105FunctionPass *llvm::createPPC32AsmPrinter(std::ostream &o, TargetMachine &tm) {
106 return new PPC32AsmPrinter(o, tm);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000107}
108
Nate Begemane59bf592004-08-14 22:09:10 +0000109// Include the auto-generated portion of the assembly writer
110#include "PowerPCGenAsmWriter.inc"
111
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000112/// printConstantPool - Print to the current output stream assembly
113/// representations of the constants in the constant pool MCP. This is
114/// used to print out constants which have been "spilled to memory" by
115/// the code generator.
116///
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000117void PPC32AsmPrinter::printConstantPool(MachineConstantPool *MCP) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000118 const std::vector<Constant*> &CP = MCP->getConstants();
119 const TargetData &TD = TM.getTargetData();
120
121 if (CP.empty()) return;
122
123 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
124 O << "\t.const\n";
Chris Lattner69d485e2004-08-17 19:26:03 +0000125 emitAlignment(TD.getTypeAlignmentShift(CP[i]->getType()));
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000126 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t" << CommentString
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000127 << *CP[i] << "\n";
128 emitGlobalConstant(CP[i]);
129 }
130}
131
132/// runOnMachineFunction - This uses the printMachineInstruction()
133/// method to print assembly for each instruction.
134///
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000135bool PPC32AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattnera3840792004-08-16 23:25:21 +0000136 setupMachineFunction(MF);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000137 O << "\n\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000138
139 // Print out constants referenced by the function
140 printConstantPool(MF.getConstantPool());
141
142 // Print out labels for the function.
Chris Lattner69d485e2004-08-17 19:26:03 +0000143 O << "\t.text\n";
144 emitAlignment(2);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000145 O << "\t.globl\t" << CurrentFnName << "\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000146 O << CurrentFnName << ":\n";
147
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000148 // Print out code for the function.
149 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
150 I != E; ++I) {
151 // Print a label for the basic block.
Chris Lattner69d485e2004-08-17 19:26:03 +0000152 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t"
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000153 << CommentString << " " << I->getBasicBlock()->getName() << "\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000154 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
Chris Lattner69d485e2004-08-17 19:26:03 +0000155 II != E; ++II) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000156 // Print the assembly for the instruction.
157 O << "\t";
158 printMachineInstruction(II);
159 }
160 }
Misha Brukmancf8d2442004-07-26 16:28:33 +0000161 ++LabelNumber;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000162
163 // We didn't modify anything.
164 return false;
165}
166
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000167void PPC32AsmPrinter::printOp(const MachineOperand &MO,
168 bool LoadAddrOp /* = false */) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000169 const MRegisterInfo &RI = *TM.getRegisterInfo();
170 int new_symbol;
171
172 switch (MO.getType()) {
173 case MachineOperand::MO_VirtualRegister:
174 if (Value *V = MO.getVRegValueOrNull()) {
175 O << "<" << V->getName() << ">";
176 return;
177 }
178 // FALLTHROUGH
179 case MachineOperand::MO_MachineRegister:
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000180 case MachineOperand::MO_CCRegister:
Misha Brukman7f484a52004-06-24 23:51:00 +0000181 O << LowercaseString(RI.get(MO.getReg()).Name);
182 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000183
184 case MachineOperand::MO_SignExtendedImmed:
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000185 case MachineOperand::MO_UnextendedImmed:
186 std::cerr << "printOp() does not handle immediate values\n";
187 abort();
Misha Brukman97a296f2004-07-21 20:11:11 +0000188 return;
189
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000190 case MachineOperand::MO_PCRelativeDisp:
191 std::cerr << "Shouldn't use addPCDisp() when building PPC MachineInstrs";
192 abort();
193 return;
194
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000195 case MachineOperand::MO_MachineBasicBlock: {
196 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
197 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
Misha Brukman218bec72004-06-29 17:13:26 +0000198 << "_" << MBBOp->getNumber() << "\t; "
Misha Brukman2bf183c2004-06-25 15:42:10 +0000199 << MBBOp->getBasicBlock()->getName();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000200 return;
201 }
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000202
203 case MachineOperand::MO_ConstantPoolIndex:
204 O << ".CPI" << CurrentFnName << "_" << MO.getConstantPoolIndex();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000205 return;
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000206
207 case MachineOperand::MO_ExternalSymbol:
208 O << MO.getSymbolName();
209 return;
210
Nate Begemanb73a7112004-08-13 09:32:01 +0000211 case MachineOperand::MO_GlobalAddress: {
212 GlobalValue *GV = MO.getGlobal();
213 std::string Name = Mang->getValueName(GV);
Misha Brukmane2eceb52004-07-23 16:08:20 +0000214
Nate Begemanb73a7112004-08-13 09:32:01 +0000215 // Dynamically-resolved functions need a stub for the function. Be
216 // wary however not to output $stub for external functions whose addresses
217 // are taken. Those should be emitted as $non_lazy_ptr below.
218 Function *F = dyn_cast<Function>(GV);
219 if (F && F->isExternal() && !LoadAddrOp &&
Chris Lattnera3840792004-08-16 23:25:21 +0000220 getTM().CalledFunctions.count(F)) {
Nate Begemanb73a7112004-08-13 09:32:01 +0000221 FnStubs.insert(Name);
222 O << "L" << Name << "$stub";
223 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000224 }
Nate Begemane59bf592004-08-14 22:09:10 +0000225
Nate Begemanb73a7112004-08-13 09:32:01 +0000226 // External global variables need a non-lazily-resolved stub
Chris Lattnera3840792004-08-16 23:25:21 +0000227 if (GV->isExternal() && getTM().AddressTaken.count(GV)) {
Nate Begemanb73a7112004-08-13 09:32:01 +0000228 GVStubs.insert(Name);
229 O << "L" << Name << "$non_lazy_ptr";
230 return;
231 }
Nate Begemane59bf592004-08-14 22:09:10 +0000232
Chris Lattnera3840792004-08-16 23:25:21 +0000233 if (F && LoadAddrOp && getTM().AddressTaken.count(GV)) {
Nate Begemane59bf592004-08-14 22:09:10 +0000234 LinkOnceStubs.insert(Name);
235 O << "L" << Name << "$non_lazy_ptr";
236 return;
237 }
Nate Begemanb73a7112004-08-13 09:32:01 +0000238
239 O << Mang->getValueName(GV);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000240 return;
Nate Begemanb73a7112004-08-13 09:32:01 +0000241 }
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000242
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000243 default:
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000244 O << "<unknown operand type: " << MO.getType() << ">";
Misha Brukman22e12072004-06-25 15:11:34 +0000245 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000246 }
247}
248
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000249void PPC32AsmPrinter::printImmOp(const MachineOperand &MO, unsigned ArgType) {
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000250 int Imm = MO.getImmedValue();
Misha Brukman5b570812004-08-10 22:47:03 +0000251 if (ArgType == PPCII::Simm16 || ArgType == PPCII::Disimm16) {
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000252 O << (short)Imm;
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000253 } else {
254 O << Imm;
255 }
256}
257
Nate Begemane59bf592004-08-14 22:09:10 +0000258/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
259/// the current output stream.
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000260///
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000261void PPC32AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Nate Begemane59bf592004-08-14 22:09:10 +0000262 ++EmittedInsts;
263 if (printInstruction(MI))
264 return; // Printer was automatically generated
265
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000266 unsigned Opcode = MI->getOpcode();
267 const TargetInstrInfo &TII = *TM.getInstrInfo();
268 const TargetInstrDescriptor &Desc = TII.get(Opcode);
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000269 unsigned i;
Misha Brukmanc6cc10f2004-06-25 19:24:52 +0000270
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000271 unsigned ArgCount = MI->getNumOperands();
272 unsigned ArgType[] = {
Misha Brukman5b570812004-08-10 22:47:03 +0000273 (Desc.TSFlags >> PPCII::Arg0TypeShift) & PPCII::ArgTypeMask,
274 (Desc.TSFlags >> PPCII::Arg1TypeShift) & PPCII::ArgTypeMask,
275 (Desc.TSFlags >> PPCII::Arg2TypeShift) & PPCII::ArgTypeMask,
276 (Desc.TSFlags >> PPCII::Arg3TypeShift) & PPCII::ArgTypeMask,
277 (Desc.TSFlags >> PPCII::Arg4TypeShift) & PPCII::ArgTypeMask
Misha Brukman22e12072004-06-25 15:11:34 +0000278 };
Misha Brukman5b570812004-08-10 22:47:03 +0000279 assert(((Desc.TSFlags & PPCII::VMX) == 0) &&
Misha Brukman46fd00a2004-06-24 23:04:11 +0000280 "Instruction requires VMX support");
Misha Brukman5b570812004-08-10 22:47:03 +0000281 assert(((Desc.TSFlags & PPCII::PPC64) == 0) &&
Misha Brukman46fd00a2004-06-24 23:04:11 +0000282 "Instruction requires 64 bit support");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000283
Misha Brukman61114612004-07-20 00:42:19 +0000284 // CALLpcrel and CALLindirect are handled specially here to print only the
285 // appropriate number of args that the assembler expects. This is because
286 // may have many arguments appended to record the uses of registers that are
287 // holding arguments to the called function.
Misha Brukman5b570812004-08-10 22:47:03 +0000288 if (Opcode == PPC::COND_BRANCH) {
Misha Brukmanab967902004-07-27 18:40:39 +0000289 std::cerr << "Error: untranslated conditional branch psuedo instruction!\n";
290 abort();
Misha Brukman5b570812004-08-10 22:47:03 +0000291 } else if (Opcode == PPC::IMPLICIT_DEF) {
Misha Brukman29188c62004-07-16 19:01:13 +0000292 O << "; IMPLICIT DEF ";
293 printOp(MI->getOperand(0));
294 O << "\n";
295 return;
Misha Brukman5b570812004-08-10 22:47:03 +0000296 } else if (Opcode == PPC::CALLpcrel) {
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000297 O << TII.getName(Opcode) << " ";
Misha Brukman61114612004-07-20 00:42:19 +0000298 printOp(MI->getOperand(0));
299 O << "\n";
300 return;
Misha Brukman5b570812004-08-10 22:47:03 +0000301 } else if (Opcode == PPC::CALLindirect) {
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000302 O << TII.getName(Opcode) << " ";
303 printImmOp(MI->getOperand(0), ArgType[0]);
Misha Brukman61114612004-07-20 00:42:19 +0000304 O << ", ";
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000305 printImmOp(MI->getOperand(1), ArgType[0]);
Misha Brukman61114612004-07-20 00:42:19 +0000306 O << "\n";
307 return;
Misha Brukman5b570812004-08-10 22:47:03 +0000308 } else if (Opcode == PPC::MovePCtoLR) {
Misha Brukman61114612004-07-20 00:42:19 +0000309 // FIXME: should probably be converted to cout.width and cout.fill
Misha Brukmancf8d2442004-07-26 16:28:33 +0000310 O << "bl \"L0000" << LabelNumber << "$pb\"\n";
311 O << "\"L0000" << LabelNumber << "$pb\":\n";
Misha Brukman218bec72004-06-29 17:13:26 +0000312 O << "\tmflr ";
313 printOp(MI->getOperand(0));
Misha Brukman218bec72004-06-29 17:13:26 +0000314 O << "\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000315 return;
316 }
317
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000318 O << TII.getName(Opcode) << " ";
Misha Brukman5b570812004-08-10 22:47:03 +0000319 if (Opcode == PPC::LOADLoDirect || Opcode == PPC::LOADLoIndirect) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000320 printOp(MI->getOperand(0));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000321 O << ", lo16(";
Nate Begemanb73a7112004-08-13 09:32:01 +0000322 printOp(MI->getOperand(2), true /* LoadAddrOp */);
Misha Brukmancf8d2442004-07-26 16:28:33 +0000323 O << "-\"L0000" << LabelNumber << "$pb\")";
Misha Brukman218bec72004-06-29 17:13:26 +0000324 O << "(";
Misha Brukman5b570812004-08-10 22:47:03 +0000325 if (MI->getOperand(1).getReg() == PPC::R0)
Misha Brukman218bec72004-06-29 17:13:26 +0000326 O << "0";
327 else
328 printOp(MI->getOperand(1));
329 O << ")\n";
Misha Brukman5b570812004-08-10 22:47:03 +0000330 } else if (Opcode == PPC::LOADHiAddr) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000331 printOp(MI->getOperand(0));
332 O << ", ";
Misha Brukman5b570812004-08-10 22:47:03 +0000333 if (MI->getOperand(1).getReg() == PPC::R0)
Misha Brukman218bec72004-06-29 17:13:26 +0000334 O << "0";
335 else
336 printOp(MI->getOperand(1));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000337 O << ", ha16(" ;
Nate Begemanb73a7112004-08-13 09:32:01 +0000338 printOp(MI->getOperand(2), true /* LoadAddrOp */);
Misha Brukmancf8d2442004-07-26 16:28:33 +0000339 O << "-\"L0000" << LabelNumber << "$pb\")\n";
Misha Brukman5b570812004-08-10 22:47:03 +0000340 } else if (ArgCount == 3 && ArgType[1] == PPCII::Disimm16) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000341 printOp(MI->getOperand(0));
342 O << ", ";
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000343 printImmOp(MI->getOperand(1), ArgType[1]);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000344 O << "(";
Misha Brukmanb9e8f972004-06-30 21:54:12 +0000345 if (MI->getOperand(2).hasAllocatedReg() &&
Misha Brukman5b570812004-08-10 22:47:03 +0000346 MI->getOperand(2).getReg() == PPC::R0)
Misha Brukman46fd00a2004-06-24 23:04:11 +0000347 O << "0";
348 else
349 printOp(MI->getOperand(2));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000350 O << ")\n";
351 } else {
Misha Brukman7f484a52004-06-24 23:51:00 +0000352 for (i = 0; i < ArgCount; ++i) {
Misha Brukmanab967902004-07-27 18:40:39 +0000353 // addi and friends
Misha Brukman5b570812004-08-10 22:47:03 +0000354 if (i == 1 && ArgCount == 3 && ArgType[2] == PPCII::Simm16 &&
Misha Brukman4363bdb2004-07-01 21:09:12 +0000355 MI->getOperand(1).hasAllocatedReg() &&
Misha Brukman5b570812004-08-10 22:47:03 +0000356 MI->getOperand(1).getReg() == PPC::R0) {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000357 O << "0";
Misha Brukmanab967902004-07-27 18:40:39 +0000358 // for long branch support, bc $+8
359 } else if (i == 1 && ArgCount == 2 && MI->getOperand(1).isImmediate() &&
360 TII.isBranch(MI->getOpcode())) {
361 O << "$+8";
362 assert(8 == MI->getOperand(i).getImmedValue()
363 && "branch off PC not to pc+8?");
364 //printOp(MI->getOperand(i));
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000365 } else if (MI->getOperand(i).isImmediate()) {
366 printImmOp(MI->getOperand(i), ArgType[i]);
Misha Brukman218bec72004-06-29 17:13:26 +0000367 } else {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000368 printOp(MI->getOperand(i));
369 }
Misha Brukman7f484a52004-06-24 23:51:00 +0000370 if (ArgCount - 1 == i)
Misha Brukman46fd00a2004-06-24 23:04:11 +0000371 O << "\n";
372 else
373 O << ", ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000374 }
375 }
Nate Begemane59bf592004-08-14 22:09:10 +0000376 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000377}
378
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000379// SwitchSection - Switch to the specified section of the executable if we are
380// not already in it!
381//
382static void SwitchSection(std::ostream &OS, std::string &CurSection,
383 const char *NewSection) {
384 if (CurSection != NewSection) {
385 CurSection = NewSection;
386 if (!CurSection.empty())
387 OS << "\t" << NewSection << "\n";
388 }
389}
390
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000391bool PPC32AsmPrinter::doFinalization(Module &M) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000392 const TargetData &TD = TM.getTargetData();
393 std::string CurSection;
394
395 // Print out module-level global variables here.
396 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
397 if (I->hasInitializer()) { // External global require no code
398 O << "\n\n";
399 std::string name = Mang->getValueName(I);
400 Constant *C = I->getInitializer();
401 unsigned Size = TD.getTypeSize(C->getType());
Chris Lattner69d485e2004-08-17 19:26:03 +0000402 unsigned Align = TD.getTypeAlignmentShift(C->getType());
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000403
Misha Brukman97a296f2004-07-21 20:11:11 +0000404 if (C->isNullValue() && /* FIXME: Verify correct */
405 (I->hasInternalLinkage() || I->hasWeakLinkage())) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000406 SwitchSection(O, CurSection, ".data");
407 if (I->hasInternalLinkage())
Misha Brukmane2eceb52004-07-23 16:08:20 +0000408 O << ".lcomm " << name << "," << TD.getTypeSize(C->getType())
Chris Lattner69d485e2004-08-17 19:26:03 +0000409 << "," << Align;
Misha Brukman218bec72004-06-29 17:13:26 +0000410 else
Misha Brukmane2eceb52004-07-23 16:08:20 +0000411 O << ".comm " << name << "," << TD.getTypeSize(C->getType());
Misha Brukman218bec72004-06-29 17:13:26 +0000412 O << "\t\t; ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000413 WriteAsOperand(O, I, true, true, &M);
414 O << "\n";
415 } else {
416 switch (I->getLinkage()) {
417 case GlobalValue::LinkOnceLinkage:
Misha Brukman97a296f2004-07-21 20:11:11 +0000418 O << ".section __TEXT,__textcoal_nt,coalesced,no_toc\n"
419 << ".weak_definition " << name << '\n'
420 << ".private_extern " << name << '\n'
421 << ".section __DATA,__datacoal_nt,coalesced,no_toc\n";
422 LinkOnceStubs.insert(name);
423 break;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000424 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
425 // Nonnull linkonce -> weak
426 O << "\t.weak " << name << "\n";
427 SwitchSection(O, CurSection, "");
428 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
429 break;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000430 case GlobalValue::AppendingLinkage:
431 // FIXME: appending linkage variables should go into a section of
432 // their name or something. For now, just emit them as external.
433 case GlobalValue::ExternalLinkage:
434 // If external or appending, declare as a global symbol
435 O << "\t.globl " << name << "\n";
436 // FALL THROUGH
437 case GlobalValue::InternalLinkage:
Misha Brukman61297ee2004-06-29 23:40:57 +0000438 SwitchSection(O, CurSection, ".data");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000439 break;
440 }
441
Chris Lattner69d485e2004-08-17 19:26:03 +0000442 emitAlignment(Align);
Misha Brukman218bec72004-06-29 17:13:26 +0000443 O << name << ":\t\t\t\t; ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000444 WriteAsOperand(O, I, true, true, &M);
445 O << " = ";
446 WriteAsOperand(O, C, false, false, &M);
447 O << "\n";
448 emitGlobalConstant(C);
449 }
450 }
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000451
452 // Output stubs for dynamically-linked functions
453 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
454 i != e; ++i)
Misha Brukman46fd00a2004-06-24 23:04:11 +0000455 {
Misha Brukmane2eceb52004-07-23 16:08:20 +0000456 O << ".data\n";
457 O << ".section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32\n";
Chris Lattner69d485e2004-08-17 19:26:03 +0000458 emitAlignment(2);
Misha Brukman46fd00a2004-06-24 23:04:11 +0000459 O << "L" << *i << "$stub:\n";
460 O << "\t.indirect_symbol " << *i << "\n";
461 O << "\tmflr r0\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000462 O << "\tbcl 20,31,L0$" << *i << "\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000463 O << "L0$" << *i << ":\n";
464 O << "\tmflr r11\n";
465 O << "\taddis r11,r11,ha16(L" << *i << "$lazy_ptr-L0$" << *i << ")\n";
466 O << "\tmtlr r0\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000467 O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr-L0$" << *i << ")(r11)\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000468 O << "\tmtctr r12\n";
469 O << "\tbctr\n";
470 O << ".data\n";
471 O << ".lazy_symbol_pointer\n";
472 O << "L" << *i << "$lazy_ptr:\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000473 O << "\t.indirect_symbol " << *i << "\n";
474 O << "\t.long dyld_stub_binding_helper\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000475 }
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000476
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000477 O << "\n";
478
479 // Output stubs for external global variables
480 if (GVStubs.begin() != GVStubs.end())
Misha Brukmane2eceb52004-07-23 16:08:20 +0000481 O << ".data\n.non_lazy_symbol_pointer\n";
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000482 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
483 i != e; ++i) {
484 O << "L" << *i << "$non_lazy_ptr:\n";
485 O << "\t.indirect_symbol " << *i << "\n";
486 O << "\t.long\t0\n";
487 }
488
Nate Begemane59bf592004-08-14 22:09:10 +0000489 // Output stubs for link-once variables
490 if (LinkOnceStubs.begin() != LinkOnceStubs.end())
491 O << ".data\n.align 2\n";
492 for (std::set<std::string>::iterator i = LinkOnceStubs.begin(),
493 e = LinkOnceStubs.end(); i != e; ++i) {
494 O << "L" << *i << "$non_lazy_ptr:\n"
495 << "\t.long\t" << *i << '\n';
496 }
497
Chris Lattnera3840792004-08-16 23:25:21 +0000498 AsmPrinter::doFinalization(M);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000499 return false; // success
500}