blob: 9a591a17c4b75f91c20e6bde26df9f1402646fb6 [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) {
48 UsesUnderscorePrefix = 1;
49 }
Misha Brukman5dfe3a92004-06-21 16:55:25 +000050
Misha Brukmancf8d2442004-07-26 16:28:33 +000051 /// Unique incrementer for label values for referencing Global values.
Misha Brukman218bec72004-06-29 17:13:26 +000052 ///
Misha Brukmancf8d2442004-07-26 16:28:33 +000053 unsigned LabelNumber;
54
Misha Brukman5dfe3a92004-06-21 16:55:25 +000055 virtual const char *getPassName() const {
Misha Brukmanf2ccb772004-08-17 04:55:41 +000056 return "PPC32 Assembly Printer";
Misha Brukman5dfe3a92004-06-21 16:55:25 +000057 }
58
Misha Brukmanf2ccb772004-08-17 04:55:41 +000059 PPC32TargetMachine &getTM() {
60 return static_cast<PPC32TargetMachine&>(TM);
Chris Lattnera3840792004-08-16 23:25:21 +000061 }
62
Nate Begemane59bf592004-08-14 22:09:10 +000063 /// printInstruction - This method is automatically generated by tablegen
64 /// from the instruction set description. This method returns true if the
65 /// machine instruction was sufficiently described to print it, otherwise it
66 /// returns false.
67 bool printInstruction(const MachineInstr *MI);
68
Misha Brukman5dfe3a92004-06-21 16:55:25 +000069 void printMachineInstruction(const MachineInstr *MI);
Nate Begemanb73a7112004-08-13 09:32:01 +000070 void printOp(const MachineOperand &MO, bool LoadAddrOp = false);
Misha Brukmanaf313fb2004-07-28 00:00:48 +000071 void printImmOp(const MachineOperand &MO, unsigned ArgType);
Chris Lattner7bb424f2004-08-14 23:27:29 +000072
73 void printOperand(const MachineInstr *MI, unsigned OpNo, MVT::ValueType VT){
74 const MachineOperand &MO = MI->getOperand(OpNo);
75 if (MO.getType() == MachineOperand::MO_MachineRegister) {
76 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
77 O << LowercaseString(TM.getRegisterInfo()->get(MO.getReg()).Name);
Chris Lattner0ea31712004-08-15 05:46:14 +000078 } else if (MO.isImmediate()) {
79 O << MO.getImmedValue();
Chris Lattner7bb424f2004-08-14 23:27:29 +000080 } else {
81 printOp(MO);
82 }
83 }
84
Chris Lattner97b2a2e2004-08-15 05:20:16 +000085 void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo,
86 MVT::ValueType VT) {
87 O << (unsigned short)MI->getOperand(OpNo).getImmedValue();
88 }
89
Misha Brukman5dfe3a92004-06-21 16:55:25 +000090 void printConstantPool(MachineConstantPool *MCP);
91 bool runOnMachineFunction(MachineFunction &F);
Misha Brukman5dfe3a92004-06-21 16:55:25 +000092 bool doFinalization(Module &M);
93 void emitGlobalConstant(const Constant* CV);
Misha Brukman5dfe3a92004-06-21 16:55:25 +000094 };
95} // end of anonymous namespace
96
Misha Brukman3d9a6c22004-08-11 00:09:42 +000097/// createPPC32AsmPrinterPass - Returns a pass that prints the PPC
Misha Brukman5dfe3a92004-06-21 16:55:25 +000098/// assembly code for a MachineFunction to the given output stream,
99/// using the given target machine description. This should work
Misha Brukman7103fba2004-08-09 22:27:45 +0000100/// regardless of whether the function is in SSA form or not.
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000101///
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000102FunctionPass *llvm::createPPC32AsmPrinter(std::ostream &o, TargetMachine &tm) {
103 return new PPC32AsmPrinter(o, tm);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000104}
105
Nate Begemane59bf592004-08-14 22:09:10 +0000106// Include the auto-generated portion of the assembly writer
107#include "PowerPCGenAsmWriter.inc"
108
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000109/// toOctal - Convert the low order bits of X into an octal digit.
110///
111static inline char toOctal(int X) {
112 return (X&7)+'0';
113}
114
115/// getAsCString - Return the specified array as a C compatible
Chris Lattner36aacdb2004-08-16 23:30:16 +0000116/// string, only if the predicate isString is true.
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000117///
118static void printAsCString(std::ostream &O, const ConstantArray *CVA) {
Chris Lattner36aacdb2004-08-16 23:30:16 +0000119 assert(CVA->isString() && "Array is not string compatible!");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000120
121 O << "\"";
Chris Lattner36aacdb2004-08-16 23:30:16 +0000122 for (unsigned i = 0; i != CVA->getNumOperands(); ++i) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000123 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
124
125 if (C == '"') {
126 O << "\\\"";
127 } else if (C == '\\') {
128 O << "\\\\";
129 } else if (isprint(C)) {
130 O << C;
131 } else {
Chris Lattner36aacdb2004-08-16 23:30:16 +0000132 switch(C) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000133 case '\b': O << "\\b"; break;
134 case '\f': O << "\\f"; break;
135 case '\n': O << "\\n"; break;
136 case '\r': O << "\\r"; break;
137 case '\t': O << "\\t"; break;
138 default:
139 O << '\\';
140 O << toOctal(C >> 6);
141 O << toOctal(C >> 3);
142 O << toOctal(C >> 0);
143 break;
144 }
145 }
146 }
147 O << "\"";
148}
149
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000150// Print a constant value or values, with the appropriate storage class as a
151// prefix.
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000152void PPC32AsmPrinter::emitGlobalConstant(const Constant *CV) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000153 const TargetData &TD = TM.getTargetData();
154
Chris Lattner416ca332004-08-16 23:38:36 +0000155 if (CV->isNullValue()) {
156 O << "\t.space\t" << TD.getTypeSize(CV->getType()) << "\n";
157 return;
158 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
159 if (CVA->isString()) {
160 O << "\t.ascii\t";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000161 printAsCString(O, CVA);
162 O << "\n";
163 } else { // Not a string. Print the values in successive locations
Chris Lattner416ca332004-08-16 23:38:36 +0000164 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Chris Lattner6173cd92004-08-04 17:29:14 +0000165 emitGlobalConstant(CVA->getOperand(i));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000166 }
167 return;
168 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
169 // Print the fields in successive locations. Pad to align if needed!
170 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000171 unsigned sizeSoFar = 0;
Chris Lattner416ca332004-08-16 23:38:36 +0000172 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
Chris Lattner6173cd92004-08-04 17:29:14 +0000173 const Constant* field = CVS->getOperand(i);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000174
175 // Check if padding is needed and insert one or more 0s.
176 unsigned fieldSize = TD.getTypeSize(field->getType());
Chris Lattner6173cd92004-08-04 17:29:14 +0000177 unsigned padSize = ((i == e-1? cvsLayout->StructSize
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000178 : cvsLayout->MemberOffsets[i+1])
179 - cvsLayout->MemberOffsets[i]) - fieldSize;
180 sizeSoFar += fieldSize + padSize;
181
182 // Now print the actual field value
183 emitGlobalConstant(field);
184
185 // Insert the field padding unless it's zero bytes...
186 if (padSize)
187 O << "\t.space\t " << padSize << "\n";
188 }
189 assert(sizeSoFar == cvsLayout->StructSize &&
190 "Layout of constant struct may be incorrect!");
191 return;
192 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
193 // FP Constants are printed as integer constants to avoid losing
194 // precision...
195 double Val = CFP->getValue();
Chris Lattner469ab792004-08-17 02:48:44 +0000196 if (CFP->getType() == Type::DoubleTy) {
Chris Lattner505e7832004-08-17 02:29:00 +0000197 union DU { // Abide by C TBAA rules
198 double FVal;
199 uint64_t UVal;
Chris Lattner505e7832004-08-17 02:29:00 +0000200 } U;
201 U.FVal = Val;
Chris Lattner469ab792004-08-17 02:48:44 +0000202
203 if (TD.isBigEndian()) {
204 O << ".long\t" << unsigned(U.UVal >> 32)
205 << "\t; double most significant word " << Val << "\n";
206 O << ".long\t" << unsigned(U.UVal)
207 << "\t; double least significant word " << Val << "\n";
208 } else {
209 O << ".long\t" << unsigned(U.UVal)
210 << "\t; double least significant word " << Val << "\n";
211 O << ".long\t" << unsigned(U.UVal >> 32)
212 << "\t; double most significant word " << Val << "\n";
213 }
Chris Lattner505e7832004-08-17 02:29:00 +0000214 return;
215 } else {
216 union FU { // Abide by C TBAA rules
217 float FVal;
218 int32_t UVal;
219 } U;
220 U.FVal = Val;
221
222 O << ".long\t" << U.UVal << "\t; float " << Val << "\n";
223 return;
224 }
Misha Brukmanf63bc192004-07-28 19:12:24 +0000225 } else if (CV->getType() == Type::ULongTy || CV->getType() == Type::LongTy) {
Misha Brukman2bf183c2004-06-25 15:42:10 +0000226 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Chris Lattner469ab792004-08-17 02:48:44 +0000227 uint64_t Val = CI->getRawValue();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000228
Chris Lattner469ab792004-08-17 02:48:44 +0000229 if (TD.isBigEndian()) {
230 O << ".long\t" << unsigned(Val >> 32)
231 << "\t; Double-word most significant word " << Val << "\n";
232 O << ".long\t" << unsigned(Val)
233 << "\t; Double-word least significant word " << Val << "\n";
234 } else {
235 O << ".long\t" << unsigned(Val)
236 << "\t; Double-word least significant word " << Val << "\n";
237 O << ".long\t" << unsigned(Val >> 32)
238 << "\t; Double-word most significant word " << Val << "\n";
239 }
Misha Brukman46fd00a2004-06-24 23:04:11 +0000240 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000241 }
242 }
243
244 const Type *type = CV->getType();
245 O << "\t";
Misha Brukmand71bd562004-06-21 17:19:08 +0000246 switch (type->getTypeID()) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000247 case Type::UByteTyID: case Type::SByteTyID:
248 O << ".byte";
249 break;
250 case Type::UShortTyID: case Type::ShortTyID:
251 O << ".short";
252 break;
253 case Type::BoolTyID:
254 case Type::PointerTyID:
255 case Type::UIntTyID: case Type::IntTyID:
256 O << ".long";
257 break;
258 case Type::ULongTyID: case Type::LongTyID:
Misha Brukman46fd00a2004-06-24 23:04:11 +0000259 assert (0 && "Should have already output double-word constant.");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000260 case Type::FloatTyID: case Type::DoubleTyID:
261 assert (0 && "Should have already output floating point constant.");
262 default:
Chris Lattner416ca332004-08-16 23:38:36 +0000263 assert (0 && "Can't handle printing this type of thing");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000264 break;
265 }
266 O << "\t";
267 emitConstantValueOnly(CV);
268 O << "\n";
269}
270
271/// printConstantPool - Print to the current output stream assembly
272/// representations of the constants in the constant pool MCP. This is
273/// used to print out constants which have been "spilled to memory" by
274/// the code generator.
275///
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000276void PPC32AsmPrinter::printConstantPool(MachineConstantPool *MCP) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000277 const std::vector<Constant*> &CP = MCP->getConstants();
278 const TargetData &TD = TM.getTargetData();
279
280 if (CP.empty()) return;
281
282 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
283 O << "\t.const\n";
284 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
285 << "\n";
Misha Brukman218bec72004-06-29 17:13:26 +0000286 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t;"
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000287 << *CP[i] << "\n";
288 emitGlobalConstant(CP[i]);
289 }
290}
291
292/// runOnMachineFunction - This uses the printMachineInstruction()
293/// method to print assembly for each instruction.
294///
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000295bool PPC32AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattnera3840792004-08-16 23:25:21 +0000296 setupMachineFunction(MF);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000297 O << "\n\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000298
299 // Print out constants referenced by the function
300 printConstantPool(MF.getConstantPool());
301
302 // Print out labels for the function.
303 O << "\t.text\n";
304 O << "\t.globl\t" << CurrentFnName << "\n";
Misha Brukman61297ee2004-06-29 23:40:57 +0000305 O << "\t.align 2\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000306 O << CurrentFnName << ":\n";
307
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000308 // Print out code for the function.
309 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
310 I != E; ++I) {
311 // Print a label for the basic block.
Misha Brukman218bec72004-06-29 17:13:26 +0000312 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t; "
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000313 << I->getBasicBlock()->getName() << "\n";
314 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
Misha Brukman46fd00a2004-06-24 23:04:11 +0000315 II != E; ++II) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000316 // Print the assembly for the instruction.
317 O << "\t";
318 printMachineInstruction(II);
319 }
320 }
Misha Brukmancf8d2442004-07-26 16:28:33 +0000321 ++LabelNumber;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000322
323 // We didn't modify anything.
324 return false;
325}
326
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000327void PPC32AsmPrinter::printOp(const MachineOperand &MO,
328 bool LoadAddrOp /* = false */) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000329 const MRegisterInfo &RI = *TM.getRegisterInfo();
330 int new_symbol;
331
332 switch (MO.getType()) {
333 case MachineOperand::MO_VirtualRegister:
334 if (Value *V = MO.getVRegValueOrNull()) {
335 O << "<" << V->getName() << ">";
336 return;
337 }
338 // FALLTHROUGH
339 case MachineOperand::MO_MachineRegister:
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000340 case MachineOperand::MO_CCRegister:
Misha Brukman7f484a52004-06-24 23:51:00 +0000341 O << LowercaseString(RI.get(MO.getReg()).Name);
342 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000343
344 case MachineOperand::MO_SignExtendedImmed:
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000345 case MachineOperand::MO_UnextendedImmed:
346 std::cerr << "printOp() does not handle immediate values\n";
347 abort();
Misha Brukman97a296f2004-07-21 20:11:11 +0000348 return;
349
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000350 case MachineOperand::MO_PCRelativeDisp:
351 std::cerr << "Shouldn't use addPCDisp() when building PPC MachineInstrs";
352 abort();
353 return;
354
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000355 case MachineOperand::MO_MachineBasicBlock: {
356 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
357 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
Misha Brukman218bec72004-06-29 17:13:26 +0000358 << "_" << MBBOp->getNumber() << "\t; "
Misha Brukman2bf183c2004-06-25 15:42:10 +0000359 << MBBOp->getBasicBlock()->getName();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000360 return;
361 }
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000362
363 case MachineOperand::MO_ConstantPoolIndex:
364 O << ".CPI" << CurrentFnName << "_" << MO.getConstantPoolIndex();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000365 return;
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000366
367 case MachineOperand::MO_ExternalSymbol:
368 O << MO.getSymbolName();
369 return;
370
Nate Begemanb73a7112004-08-13 09:32:01 +0000371 case MachineOperand::MO_GlobalAddress: {
372 GlobalValue *GV = MO.getGlobal();
373 std::string Name = Mang->getValueName(GV);
Misha Brukmane2eceb52004-07-23 16:08:20 +0000374
Nate Begemanb73a7112004-08-13 09:32:01 +0000375 // Dynamically-resolved functions need a stub for the function. Be
376 // wary however not to output $stub for external functions whose addresses
377 // are taken. Those should be emitted as $non_lazy_ptr below.
378 Function *F = dyn_cast<Function>(GV);
379 if (F && F->isExternal() && !LoadAddrOp &&
Chris Lattnera3840792004-08-16 23:25:21 +0000380 getTM().CalledFunctions.count(F)) {
Nate Begemanb73a7112004-08-13 09:32:01 +0000381 FnStubs.insert(Name);
382 O << "L" << Name << "$stub";
383 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000384 }
Nate Begemane59bf592004-08-14 22:09:10 +0000385
Nate Begemanb73a7112004-08-13 09:32:01 +0000386 // External global variables need a non-lazily-resolved stub
Chris Lattnera3840792004-08-16 23:25:21 +0000387 if (GV->isExternal() && getTM().AddressTaken.count(GV)) {
Nate Begemanb73a7112004-08-13 09:32:01 +0000388 GVStubs.insert(Name);
389 O << "L" << Name << "$non_lazy_ptr";
390 return;
391 }
Nate Begemane59bf592004-08-14 22:09:10 +0000392
Chris Lattnera3840792004-08-16 23:25:21 +0000393 if (F && LoadAddrOp && getTM().AddressTaken.count(GV)) {
Nate Begemane59bf592004-08-14 22:09:10 +0000394 LinkOnceStubs.insert(Name);
395 O << "L" << Name << "$non_lazy_ptr";
396 return;
397 }
Nate Begemanb73a7112004-08-13 09:32:01 +0000398
399 O << Mang->getValueName(GV);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000400 return;
Nate Begemanb73a7112004-08-13 09:32:01 +0000401 }
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000402
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000403 default:
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000404 O << "<unknown operand type: " << MO.getType() << ">";
Misha Brukman22e12072004-06-25 15:11:34 +0000405 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000406 }
407}
408
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000409void PPC32AsmPrinter::printImmOp(const MachineOperand &MO, unsigned ArgType) {
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000410 int Imm = MO.getImmedValue();
Misha Brukman5b570812004-08-10 22:47:03 +0000411 if (ArgType == PPCII::Simm16 || ArgType == PPCII::Disimm16) {
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000412 O << (short)Imm;
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000413 } else {
414 O << Imm;
415 }
416}
417
Nate Begemane59bf592004-08-14 22:09:10 +0000418/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
419/// the current output stream.
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000420///
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000421void PPC32AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Nate Begemane59bf592004-08-14 22:09:10 +0000422 ++EmittedInsts;
423 if (printInstruction(MI))
424 return; // Printer was automatically generated
425
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000426 unsigned Opcode = MI->getOpcode();
427 const TargetInstrInfo &TII = *TM.getInstrInfo();
428 const TargetInstrDescriptor &Desc = TII.get(Opcode);
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000429 unsigned i;
Misha Brukmanc6cc10f2004-06-25 19:24:52 +0000430
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000431 unsigned ArgCount = MI->getNumOperands();
432 unsigned ArgType[] = {
Misha Brukman5b570812004-08-10 22:47:03 +0000433 (Desc.TSFlags >> PPCII::Arg0TypeShift) & PPCII::ArgTypeMask,
434 (Desc.TSFlags >> PPCII::Arg1TypeShift) & PPCII::ArgTypeMask,
435 (Desc.TSFlags >> PPCII::Arg2TypeShift) & PPCII::ArgTypeMask,
436 (Desc.TSFlags >> PPCII::Arg3TypeShift) & PPCII::ArgTypeMask,
437 (Desc.TSFlags >> PPCII::Arg4TypeShift) & PPCII::ArgTypeMask
Misha Brukman22e12072004-06-25 15:11:34 +0000438 };
Misha Brukman5b570812004-08-10 22:47:03 +0000439 assert(((Desc.TSFlags & PPCII::VMX) == 0) &&
Misha Brukman46fd00a2004-06-24 23:04:11 +0000440 "Instruction requires VMX support");
Misha Brukman5b570812004-08-10 22:47:03 +0000441 assert(((Desc.TSFlags & PPCII::PPC64) == 0) &&
Misha Brukman46fd00a2004-06-24 23:04:11 +0000442 "Instruction requires 64 bit support");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000443
Misha Brukman61114612004-07-20 00:42:19 +0000444 // CALLpcrel and CALLindirect are handled specially here to print only the
445 // appropriate number of args that the assembler expects. This is because
446 // may have many arguments appended to record the uses of registers that are
447 // holding arguments to the called function.
Misha Brukman5b570812004-08-10 22:47:03 +0000448 if (Opcode == PPC::COND_BRANCH) {
Misha Brukmanab967902004-07-27 18:40:39 +0000449 std::cerr << "Error: untranslated conditional branch psuedo instruction!\n";
450 abort();
Misha Brukman5b570812004-08-10 22:47:03 +0000451 } else if (Opcode == PPC::IMPLICIT_DEF) {
Misha Brukman29188c62004-07-16 19:01:13 +0000452 O << "; IMPLICIT DEF ";
453 printOp(MI->getOperand(0));
454 O << "\n";
455 return;
Misha Brukman5b570812004-08-10 22:47:03 +0000456 } else if (Opcode == PPC::CALLpcrel) {
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000457 O << TII.getName(Opcode) << " ";
Misha Brukman61114612004-07-20 00:42:19 +0000458 printOp(MI->getOperand(0));
459 O << "\n";
460 return;
Misha Brukman5b570812004-08-10 22:47:03 +0000461 } else if (Opcode == PPC::CALLindirect) {
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000462 O << TII.getName(Opcode) << " ";
463 printImmOp(MI->getOperand(0), ArgType[0]);
Misha Brukman61114612004-07-20 00:42:19 +0000464 O << ", ";
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000465 printImmOp(MI->getOperand(1), ArgType[0]);
Misha Brukman61114612004-07-20 00:42:19 +0000466 O << "\n";
467 return;
Misha Brukman5b570812004-08-10 22:47:03 +0000468 } else if (Opcode == PPC::MovePCtoLR) {
Misha Brukman61114612004-07-20 00:42:19 +0000469 // FIXME: should probably be converted to cout.width and cout.fill
Misha Brukmancf8d2442004-07-26 16:28:33 +0000470 O << "bl \"L0000" << LabelNumber << "$pb\"\n";
471 O << "\"L0000" << LabelNumber << "$pb\":\n";
Misha Brukman218bec72004-06-29 17:13:26 +0000472 O << "\tmflr ";
473 printOp(MI->getOperand(0));
Misha Brukman218bec72004-06-29 17:13:26 +0000474 O << "\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000475 return;
476 }
477
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000478 O << TII.getName(Opcode) << " ";
Misha Brukman5b570812004-08-10 22:47:03 +0000479 if (Opcode == PPC::LOADLoDirect || Opcode == PPC::LOADLoIndirect) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000480 printOp(MI->getOperand(0));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000481 O << ", lo16(";
Nate Begemanb73a7112004-08-13 09:32:01 +0000482 printOp(MI->getOperand(2), true /* LoadAddrOp */);
Misha Brukmancf8d2442004-07-26 16:28:33 +0000483 O << "-\"L0000" << LabelNumber << "$pb\")";
Misha Brukman218bec72004-06-29 17:13:26 +0000484 O << "(";
Misha Brukman5b570812004-08-10 22:47:03 +0000485 if (MI->getOperand(1).getReg() == PPC::R0)
Misha Brukman218bec72004-06-29 17:13:26 +0000486 O << "0";
487 else
488 printOp(MI->getOperand(1));
489 O << ")\n";
Misha Brukman5b570812004-08-10 22:47:03 +0000490 } else if (Opcode == PPC::LOADHiAddr) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000491 printOp(MI->getOperand(0));
492 O << ", ";
Misha Brukman5b570812004-08-10 22:47:03 +0000493 if (MI->getOperand(1).getReg() == PPC::R0)
Misha Brukman218bec72004-06-29 17:13:26 +0000494 O << "0";
495 else
496 printOp(MI->getOperand(1));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000497 O << ", ha16(" ;
Nate Begemanb73a7112004-08-13 09:32:01 +0000498 printOp(MI->getOperand(2), true /* LoadAddrOp */);
Misha Brukmancf8d2442004-07-26 16:28:33 +0000499 O << "-\"L0000" << LabelNumber << "$pb\")\n";
Misha Brukman5b570812004-08-10 22:47:03 +0000500 } else if (ArgCount == 3 && ArgType[1] == PPCII::Disimm16) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000501 printOp(MI->getOperand(0));
502 O << ", ";
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000503 printImmOp(MI->getOperand(1), ArgType[1]);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000504 O << "(";
Misha Brukmanb9e8f972004-06-30 21:54:12 +0000505 if (MI->getOperand(2).hasAllocatedReg() &&
Misha Brukman5b570812004-08-10 22:47:03 +0000506 MI->getOperand(2).getReg() == PPC::R0)
Misha Brukman46fd00a2004-06-24 23:04:11 +0000507 O << "0";
508 else
509 printOp(MI->getOperand(2));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000510 O << ")\n";
511 } else {
Misha Brukman7f484a52004-06-24 23:51:00 +0000512 for (i = 0; i < ArgCount; ++i) {
Misha Brukmanab967902004-07-27 18:40:39 +0000513 // addi and friends
Misha Brukman5b570812004-08-10 22:47:03 +0000514 if (i == 1 && ArgCount == 3 && ArgType[2] == PPCII::Simm16 &&
Misha Brukman4363bdb2004-07-01 21:09:12 +0000515 MI->getOperand(1).hasAllocatedReg() &&
Misha Brukman5b570812004-08-10 22:47:03 +0000516 MI->getOperand(1).getReg() == PPC::R0) {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000517 O << "0";
Misha Brukmanab967902004-07-27 18:40:39 +0000518 // for long branch support, bc $+8
519 } else if (i == 1 && ArgCount == 2 && MI->getOperand(1).isImmediate() &&
520 TII.isBranch(MI->getOpcode())) {
521 O << "$+8";
522 assert(8 == MI->getOperand(i).getImmedValue()
523 && "branch off PC not to pc+8?");
524 //printOp(MI->getOperand(i));
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000525 } else if (MI->getOperand(i).isImmediate()) {
526 printImmOp(MI->getOperand(i), ArgType[i]);
Misha Brukman218bec72004-06-29 17:13:26 +0000527 } else {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000528 printOp(MI->getOperand(i));
529 }
Misha Brukman7f484a52004-06-24 23:51:00 +0000530 if (ArgCount - 1 == i)
Misha Brukman46fd00a2004-06-24 23:04:11 +0000531 O << "\n";
532 else
533 O << ", ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000534 }
535 }
Nate Begemane59bf592004-08-14 22:09:10 +0000536 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000537}
538
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000539// SwitchSection - Switch to the specified section of the executable if we are
540// not already in it!
541//
542static void SwitchSection(std::ostream &OS, std::string &CurSection,
543 const char *NewSection) {
544 if (CurSection != NewSection) {
545 CurSection = NewSection;
546 if (!CurSection.empty())
547 OS << "\t" << NewSection << "\n";
548 }
549}
550
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000551bool PPC32AsmPrinter::doFinalization(Module &M) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000552 const TargetData &TD = TM.getTargetData();
553 std::string CurSection;
554
555 // Print out module-level global variables here.
556 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
557 if (I->hasInitializer()) { // External global require no code
558 O << "\n\n";
559 std::string name = Mang->getValueName(I);
560 Constant *C = I->getInitializer();
561 unsigned Size = TD.getTypeSize(C->getType());
562 unsigned Align = TD.getTypeAlignment(C->getType());
563
Misha Brukman97a296f2004-07-21 20:11:11 +0000564 if (C->isNullValue() && /* FIXME: Verify correct */
565 (I->hasInternalLinkage() || I->hasWeakLinkage())) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000566 SwitchSection(O, CurSection, ".data");
567 if (I->hasInternalLinkage())
Misha Brukmane2eceb52004-07-23 16:08:20 +0000568 O << ".lcomm " << name << "," << TD.getTypeSize(C->getType())
Misha Brukman218bec72004-06-29 17:13:26 +0000569 << "," << (unsigned)TD.getTypeAlignment(C->getType());
570 else
Misha Brukmane2eceb52004-07-23 16:08:20 +0000571 O << ".comm " << name << "," << TD.getTypeSize(C->getType());
Misha Brukman218bec72004-06-29 17:13:26 +0000572 O << "\t\t; ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000573 WriteAsOperand(O, I, true, true, &M);
574 O << "\n";
575 } else {
576 switch (I->getLinkage()) {
577 case GlobalValue::LinkOnceLinkage:
Misha Brukman97a296f2004-07-21 20:11:11 +0000578 O << ".section __TEXT,__textcoal_nt,coalesced,no_toc\n"
579 << ".weak_definition " << name << '\n'
580 << ".private_extern " << name << '\n'
581 << ".section __DATA,__datacoal_nt,coalesced,no_toc\n";
582 LinkOnceStubs.insert(name);
583 break;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000584 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
585 // Nonnull linkonce -> weak
586 O << "\t.weak " << name << "\n";
587 SwitchSection(O, CurSection, "");
588 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
589 break;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000590 case GlobalValue::AppendingLinkage:
591 // FIXME: appending linkage variables should go into a section of
592 // their name or something. For now, just emit them as external.
593 case GlobalValue::ExternalLinkage:
594 // If external or appending, declare as a global symbol
595 O << "\t.globl " << name << "\n";
596 // FALL THROUGH
597 case GlobalValue::InternalLinkage:
Misha Brukman61297ee2004-06-29 23:40:57 +0000598 SwitchSection(O, CurSection, ".data");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000599 break;
600 }
601
602 O << "\t.align " << Align << "\n";
Misha Brukman218bec72004-06-29 17:13:26 +0000603 O << name << ":\t\t\t\t; ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000604 WriteAsOperand(O, I, true, true, &M);
605 O << " = ";
606 WriteAsOperand(O, C, false, false, &M);
607 O << "\n";
608 emitGlobalConstant(C);
609 }
610 }
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000611
612 // Output stubs for dynamically-linked functions
613 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
614 i != e; ++i)
Misha Brukman46fd00a2004-06-24 23:04:11 +0000615 {
Misha Brukmane2eceb52004-07-23 16:08:20 +0000616 O << ".data\n";
617 O << ".section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32\n";
618 O << "\t.align 2\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000619 O << "L" << *i << "$stub:\n";
620 O << "\t.indirect_symbol " << *i << "\n";
621 O << "\tmflr r0\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000622 O << "\tbcl 20,31,L0$" << *i << "\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000623 O << "L0$" << *i << ":\n";
624 O << "\tmflr r11\n";
625 O << "\taddis r11,r11,ha16(L" << *i << "$lazy_ptr-L0$" << *i << ")\n";
626 O << "\tmtlr r0\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000627 O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr-L0$" << *i << ")(r11)\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000628 O << "\tmtctr r12\n";
629 O << "\tbctr\n";
630 O << ".data\n";
631 O << ".lazy_symbol_pointer\n";
632 O << "L" << *i << "$lazy_ptr:\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000633 O << "\t.indirect_symbol " << *i << "\n";
634 O << "\t.long dyld_stub_binding_helper\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000635 }
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000636
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000637 O << "\n";
638
639 // Output stubs for external global variables
640 if (GVStubs.begin() != GVStubs.end())
Misha Brukmane2eceb52004-07-23 16:08:20 +0000641 O << ".data\n.non_lazy_symbol_pointer\n";
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000642 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
643 i != e; ++i) {
644 O << "L" << *i << "$non_lazy_ptr:\n";
645 O << "\t.indirect_symbol " << *i << "\n";
646 O << "\t.long\t0\n";
647 }
648
Nate Begemane59bf592004-08-14 22:09:10 +0000649 // Output stubs for link-once variables
650 if (LinkOnceStubs.begin() != LinkOnceStubs.end())
651 O << ".data\n.align 2\n";
652 for (std::set<std::string>::iterator i = LinkOnceStubs.begin(),
653 e = LinkOnceStubs.end(); i != e; ++i) {
654 O << "L" << *i << "$non_lazy_ptr:\n"
655 << "\t.long\t" << *i << '\n';
656 }
657
Chris Lattnera3840792004-08-16 23:25:21 +0000658 AsmPrinter::doFinalization(M);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000659 return false; // success
660}