blob: b68d1849a7619dda25b646020cb5a37319f4b148 [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"
21#include "PowerPCInstrInfo.h"
Nate Begemane59bf592004-08-14 22:09:10 +000022#include "PowerPCTargetMachine.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000023#include "llvm/Constants.h"
24#include "llvm/DerivedTypes.h"
25#include "llvm/Module.h"
26#include "llvm/Assembly/Writer.h"
Chris Lattnera3840792004-08-16 23:25:21 +000027#include "llvm/CodeGen/AsmPrinter.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000028#include "llvm/CodeGen/MachineConstantPool.h"
Misha Brukman05794492004-06-24 17:31:42 +000029#include "llvm/CodeGen/MachineFunctionPass.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000030#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner7bb424f2004-08-14 23:27:29 +000031#include "llvm/CodeGen/ValueTypes.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000032#include "llvm/Target/TargetMachine.h"
33#include "llvm/Support/Mangler.h"
Misha Brukman05794492004-06-24 17:31:42 +000034#include "Support/CommandLine.h"
35#include "Support/Debug.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000036#include "Support/Statistic.h"
37#include "Support/StringExtras.h"
Misha Brukman05794492004-06-24 17:31:42 +000038#include <set>
Chris Lattnera3840792004-08-16 23:25:21 +000039using namespace llvm;
Misha Brukman5dfe3a92004-06-21 16:55:25 +000040
41namespace {
42 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
43
Chris Lattnera3840792004-08-16 23:25:21 +000044 struct PowerPCAsmPrinter : public AsmPrinter {
Misha Brukman97a296f2004-07-21 20:11:11 +000045 std::set<std::string> FnStubs, GVStubs, LinkOnceStubs;
Misha Brukman5dfe3a92004-06-21 16:55:25 +000046 std::set<std::string> Strings;
47
Chris Lattnera3840792004-08-16 23:25:21 +000048 PowerPCAsmPrinter(std::ostream &O, TargetMachine &TM)
49 : AsmPrinter(O, TM), LabelNumber(0) {}
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 {
Nate Begemane59bf592004-08-14 22:09:10 +000056 return "PowerPC Assembly Printer";
Misha Brukman5dfe3a92004-06-21 16:55:25 +000057 }
58
Chris Lattnera3840792004-08-16 23:25:21 +000059 PowerPCTargetMachine &getTM() {
60 return static_cast<PowerPCTargetMachine&>(TM);
61 }
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///
Chris Lattnera3840792004-08-16 23:25:21 +0000102FunctionPass *llvm::createPPCAsmPrinter(std::ostream &o,TargetMachine &tm) {
Nate Begemane59bf592004-08-14 22:09:10 +0000103 return new PowerPCAsmPrinter(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/// isStringCompatible - Can we treat the specified array as a string?
110/// Only if it is an array of ubytes or non-negative sbytes.
111///
112static bool isStringCompatible(const ConstantArray *CVA) {
113 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
114 if (ETy == Type::UByteTy) return true;
115 if (ETy != Type::SByteTy) return false;
116
117 for (unsigned i = 0; i < CVA->getNumOperands(); ++i)
118 if (cast<ConstantSInt>(CVA->getOperand(i))->getValue() < 0)
119 return false;
120
121 return true;
122}
123
124/// toOctal - Convert the low order bits of X into an octal digit.
125///
126static inline char toOctal(int X) {
127 return (X&7)+'0';
128}
129
130/// getAsCString - Return the specified array as a C compatible
131/// string, only if the predicate isStringCompatible is true.
132///
133static void printAsCString(std::ostream &O, const ConstantArray *CVA) {
134 assert(isStringCompatible(CVA) && "Array is not string compatible!");
135
136 O << "\"";
137 for (unsigned i = 0; i < CVA->getNumOperands(); ++i) {
138 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
139
140 if (C == '"') {
141 O << "\\\"";
142 } else if (C == '\\') {
143 O << "\\\\";
144 } else if (isprint(C)) {
145 O << C;
146 } else {
Misha Brukmane2eceb52004-07-23 16:08:20 +0000147 switch (C) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000148 case '\b': O << "\\b"; break;
149 case '\f': O << "\\f"; break;
150 case '\n': O << "\\n"; break;
151 case '\r': O << "\\r"; break;
152 case '\t': O << "\\t"; break;
153 default:
154 O << '\\';
155 O << toOctal(C >> 6);
156 O << toOctal(C >> 3);
157 O << toOctal(C >> 0);
158 break;
159 }
160 }
161 }
162 O << "\"";
163}
164
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000165// Print a constant value or values, with the appropriate storage class as a
166// prefix.
Nate Begemane59bf592004-08-14 22:09:10 +0000167void PowerPCAsmPrinter::emitGlobalConstant(const Constant *CV) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000168 const TargetData &TD = TM.getTargetData();
169
Misha Brukmane48178e2004-07-20 15:45:27 +0000170 if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000171 if (isStringCompatible(CVA)) {
Misha Brukman218bec72004-06-29 17:13:26 +0000172 O << "\t.ascii ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000173 printAsCString(O, CVA);
174 O << "\n";
175 } else { // Not a string. Print the values in successive locations
Chris Lattner6173cd92004-08-04 17:29:14 +0000176 for (unsigned i=0, e = CVA->getNumOperands(); i != e; i++)
177 emitGlobalConstant(CVA->getOperand(i));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000178 }
179 return;
180 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
181 // Print the fields in successive locations. Pad to align if needed!
182 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000183 unsigned sizeSoFar = 0;
Chris Lattner6173cd92004-08-04 17:29:14 +0000184 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; i++) {
185 const Constant* field = CVS->getOperand(i);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000186
187 // Check if padding is needed and insert one or more 0s.
188 unsigned fieldSize = TD.getTypeSize(field->getType());
Chris Lattner6173cd92004-08-04 17:29:14 +0000189 unsigned padSize = ((i == e-1? cvsLayout->StructSize
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000190 : cvsLayout->MemberOffsets[i+1])
191 - cvsLayout->MemberOffsets[i]) - fieldSize;
192 sizeSoFar += fieldSize + padSize;
193
194 // Now print the actual field value
195 emitGlobalConstant(field);
196
197 // Insert the field padding unless it's zero bytes...
198 if (padSize)
199 O << "\t.space\t " << padSize << "\n";
200 }
201 assert(sizeSoFar == cvsLayout->StructSize &&
202 "Layout of constant struct may be incorrect!");
203 return;
204 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
205 // FP Constants are printed as integer constants to avoid losing
206 // precision...
207 double Val = CFP->getValue();
Nate Begemane59bf592004-08-14 22:09:10 +0000208 union DU { // Abide by C TBAA rules
209 double FVal;
210 uint64_t UVal;
211 struct {
212 uint32_t MSWord;
213 uint32_t LSWord;
214 } T;
215 } U;
216 U.FVal = Val;
217
218 O << ".long\t" << U.T.MSWord << "\t; double most significant word "
219 << Val << "\n";
220 O << ".long\t" << U.T.LSWord << "\t; double least significant word "
221 << Val << "\n";
222 return;
Misha Brukmanf63bc192004-07-28 19:12:24 +0000223 } else if (CV->getType() == Type::ULongTy || CV->getType() == Type::LongTy) {
Misha Brukman2bf183c2004-06-25 15:42:10 +0000224 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
225 union DU { // Abide by C TBAA rules
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000226 int64_t UVal;
227 struct {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000228 uint32_t MSWord;
229 uint32_t LSWord;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000230 } T;
231 } U;
232 U.UVal = CI->getRawValue();
233
Misha Brukman218bec72004-06-29 17:13:26 +0000234 O << ".long\t" << U.T.MSWord << "\t; Double-word most significant word "
Misha Brukman46fd00a2004-06-24 23:04:11 +0000235 << U.UVal << "\n";
Misha Brukman29188c62004-07-16 19:01:13 +0000236 O << ".long\t" << U.T.LSWord << "\t; Double-word least significant word "
Misha Brukman46fd00a2004-06-24 23:04:11 +0000237 << U.UVal << "\n";
238 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000239 }
240 }
241
242 const Type *type = CV->getType();
243 O << "\t";
Misha Brukmand71bd562004-06-21 17:19:08 +0000244 switch (type->getTypeID()) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000245 case Type::UByteTyID: case Type::SByteTyID:
246 O << ".byte";
247 break;
248 case Type::UShortTyID: case Type::ShortTyID:
249 O << ".short";
250 break;
251 case Type::BoolTyID:
252 case Type::PointerTyID:
253 case Type::UIntTyID: case Type::IntTyID:
254 O << ".long";
255 break;
256 case Type::ULongTyID: case Type::LongTyID:
Misha Brukman46fd00a2004-06-24 23:04:11 +0000257 assert (0 && "Should have already output double-word constant.");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000258 case Type::FloatTyID: case Type::DoubleTyID:
259 assert (0 && "Should have already output floating point constant.");
260 default:
Misha Brukman97a296f2004-07-21 20:11:11 +0000261 if (CV == Constant::getNullValue(type)) { // Zero initializer?
262 O << ".space\t" << TD.getTypeSize(type) << "\n";
263 return;
264 }
265 std::cerr << "Can't handle printing: " << *CV;
266 abort();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000267 break;
268 }
269 O << "\t";
270 emitConstantValueOnly(CV);
271 O << "\n";
272}
273
274/// printConstantPool - Print to the current output stream assembly
275/// representations of the constants in the constant pool MCP. This is
276/// used to print out constants which have been "spilled to memory" by
277/// the code generator.
278///
Nate Begemane59bf592004-08-14 22:09:10 +0000279void PowerPCAsmPrinter::printConstantPool(MachineConstantPool *MCP) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000280 const std::vector<Constant*> &CP = MCP->getConstants();
281 const TargetData &TD = TM.getTargetData();
282
283 if (CP.empty()) return;
284
285 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
286 O << "\t.const\n";
287 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
288 << "\n";
Misha Brukman218bec72004-06-29 17:13:26 +0000289 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t;"
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000290 << *CP[i] << "\n";
291 emitGlobalConstant(CP[i]);
292 }
293}
294
295/// runOnMachineFunction - This uses the printMachineInstruction()
296/// method to print assembly for each instruction.
297///
Nate Begemane59bf592004-08-14 22:09:10 +0000298bool PowerPCAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattnera3840792004-08-16 23:25:21 +0000299 setupMachineFunction(MF);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000300 O << "\n\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000301
302 // Print out constants referenced by the function
303 printConstantPool(MF.getConstantPool());
304
305 // Print out labels for the function.
306 O << "\t.text\n";
307 O << "\t.globl\t" << CurrentFnName << "\n";
Misha Brukman61297ee2004-06-29 23:40:57 +0000308 O << "\t.align 2\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000309 O << CurrentFnName << ":\n";
310
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000311 // Print out code for the function.
312 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
313 I != E; ++I) {
314 // Print a label for the basic block.
Misha Brukman218bec72004-06-29 17:13:26 +0000315 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t; "
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000316 << I->getBasicBlock()->getName() << "\n";
317 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
Misha Brukman46fd00a2004-06-24 23:04:11 +0000318 II != E; ++II) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000319 // Print the assembly for the instruction.
320 O << "\t";
321 printMachineInstruction(II);
322 }
323 }
Misha Brukmancf8d2442004-07-26 16:28:33 +0000324 ++LabelNumber;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000325
326 // We didn't modify anything.
327 return false;
328}
329
Nate Begemane59bf592004-08-14 22:09:10 +0000330void PowerPCAsmPrinter::printOp(const MachineOperand &MO,
Nate Begemanb73a7112004-08-13 09:32:01 +0000331 bool LoadAddrOp /* = false */) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000332 const MRegisterInfo &RI = *TM.getRegisterInfo();
333 int new_symbol;
334
335 switch (MO.getType()) {
336 case MachineOperand::MO_VirtualRegister:
337 if (Value *V = MO.getVRegValueOrNull()) {
338 O << "<" << V->getName() << ">";
339 return;
340 }
341 // FALLTHROUGH
342 case MachineOperand::MO_MachineRegister:
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000343 case MachineOperand::MO_CCRegister:
Misha Brukman7f484a52004-06-24 23:51:00 +0000344 O << LowercaseString(RI.get(MO.getReg()).Name);
345 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000346
347 case MachineOperand::MO_SignExtendedImmed:
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000348 case MachineOperand::MO_UnextendedImmed:
349 std::cerr << "printOp() does not handle immediate values\n";
350 abort();
Misha Brukman97a296f2004-07-21 20:11:11 +0000351 return;
352
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000353 case MachineOperand::MO_PCRelativeDisp:
354 std::cerr << "Shouldn't use addPCDisp() when building PPC MachineInstrs";
355 abort();
356 return;
357
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000358 case MachineOperand::MO_MachineBasicBlock: {
359 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
360 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
Misha Brukman218bec72004-06-29 17:13:26 +0000361 << "_" << MBBOp->getNumber() << "\t; "
Misha Brukman2bf183c2004-06-25 15:42:10 +0000362 << MBBOp->getBasicBlock()->getName();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000363 return;
364 }
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000365
366 case MachineOperand::MO_ConstantPoolIndex:
367 O << ".CPI" << CurrentFnName << "_" << MO.getConstantPoolIndex();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000368 return;
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000369
370 case MachineOperand::MO_ExternalSymbol:
371 O << MO.getSymbolName();
372 return;
373
Nate Begemanb73a7112004-08-13 09:32:01 +0000374 case MachineOperand::MO_GlobalAddress: {
375 GlobalValue *GV = MO.getGlobal();
376 std::string Name = Mang->getValueName(GV);
Misha Brukmane2eceb52004-07-23 16:08:20 +0000377
Nate Begemanb73a7112004-08-13 09:32:01 +0000378 // Dynamically-resolved functions need a stub for the function. Be
379 // wary however not to output $stub for external functions whose addresses
380 // are taken. Those should be emitted as $non_lazy_ptr below.
381 Function *F = dyn_cast<Function>(GV);
382 if (F && F->isExternal() && !LoadAddrOp &&
Chris Lattnera3840792004-08-16 23:25:21 +0000383 getTM().CalledFunctions.count(F)) {
Nate Begemanb73a7112004-08-13 09:32:01 +0000384 FnStubs.insert(Name);
385 O << "L" << Name << "$stub";
386 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000387 }
Nate Begemane59bf592004-08-14 22:09:10 +0000388
Nate Begemanb73a7112004-08-13 09:32:01 +0000389 // External global variables need a non-lazily-resolved stub
Chris Lattnera3840792004-08-16 23:25:21 +0000390 if (GV->isExternal() && getTM().AddressTaken.count(GV)) {
Nate Begemanb73a7112004-08-13 09:32:01 +0000391 GVStubs.insert(Name);
392 O << "L" << Name << "$non_lazy_ptr";
393 return;
394 }
Nate Begemane59bf592004-08-14 22:09:10 +0000395
Chris Lattnera3840792004-08-16 23:25:21 +0000396 if (F && LoadAddrOp && getTM().AddressTaken.count(GV)) {
Nate Begemane59bf592004-08-14 22:09:10 +0000397 LinkOnceStubs.insert(Name);
398 O << "L" << Name << "$non_lazy_ptr";
399 return;
400 }
Nate Begemanb73a7112004-08-13 09:32:01 +0000401
402 O << Mang->getValueName(GV);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000403 return;
Nate Begemanb73a7112004-08-13 09:32:01 +0000404 }
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000405
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000406 default:
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000407 O << "<unknown operand type: " << MO.getType() << ">";
Misha Brukman22e12072004-06-25 15:11:34 +0000408 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000409 }
410}
411
Nate Begemane59bf592004-08-14 22:09:10 +0000412void PowerPCAsmPrinter::printImmOp(const MachineOperand &MO, unsigned ArgType) {
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000413 int Imm = MO.getImmedValue();
Misha Brukman5b570812004-08-10 22:47:03 +0000414 if (ArgType == PPCII::Simm16 || ArgType == PPCII::Disimm16) {
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000415 O << (short)Imm;
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000416 } else {
417 O << Imm;
418 }
419}
420
Nate Begemane59bf592004-08-14 22:09:10 +0000421/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
422/// the current output stream.
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000423///
Nate Begemane59bf592004-08-14 22:09:10 +0000424void PowerPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
425 ++EmittedInsts;
426 if (printInstruction(MI))
427 return; // Printer was automatically generated
428
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000429 unsigned Opcode = MI->getOpcode();
430 const TargetInstrInfo &TII = *TM.getInstrInfo();
431 const TargetInstrDescriptor &Desc = TII.get(Opcode);
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000432 unsigned i;
Misha Brukmanc6cc10f2004-06-25 19:24:52 +0000433
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000434 unsigned ArgCount = MI->getNumOperands();
435 unsigned ArgType[] = {
Misha Brukman5b570812004-08-10 22:47:03 +0000436 (Desc.TSFlags >> PPCII::Arg0TypeShift) & PPCII::ArgTypeMask,
437 (Desc.TSFlags >> PPCII::Arg1TypeShift) & PPCII::ArgTypeMask,
438 (Desc.TSFlags >> PPCII::Arg2TypeShift) & PPCII::ArgTypeMask,
439 (Desc.TSFlags >> PPCII::Arg3TypeShift) & PPCII::ArgTypeMask,
440 (Desc.TSFlags >> PPCII::Arg4TypeShift) & PPCII::ArgTypeMask
Misha Brukman22e12072004-06-25 15:11:34 +0000441 };
Misha Brukman5b570812004-08-10 22:47:03 +0000442 assert(((Desc.TSFlags & PPCII::VMX) == 0) &&
Misha Brukman46fd00a2004-06-24 23:04:11 +0000443 "Instruction requires VMX support");
Misha Brukman5b570812004-08-10 22:47:03 +0000444 assert(((Desc.TSFlags & PPCII::PPC64) == 0) &&
Misha Brukman46fd00a2004-06-24 23:04:11 +0000445 "Instruction requires 64 bit support");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000446
Misha Brukman61114612004-07-20 00:42:19 +0000447 // CALLpcrel and CALLindirect are handled specially here to print only the
448 // appropriate number of args that the assembler expects. This is because
449 // may have many arguments appended to record the uses of registers that are
450 // holding arguments to the called function.
Misha Brukman5b570812004-08-10 22:47:03 +0000451 if (Opcode == PPC::COND_BRANCH) {
Misha Brukmanab967902004-07-27 18:40:39 +0000452 std::cerr << "Error: untranslated conditional branch psuedo instruction!\n";
453 abort();
Misha Brukman5b570812004-08-10 22:47:03 +0000454 } else if (Opcode == PPC::IMPLICIT_DEF) {
Misha Brukman29188c62004-07-16 19:01:13 +0000455 O << "; IMPLICIT DEF ";
456 printOp(MI->getOperand(0));
457 O << "\n";
458 return;
Misha Brukman5b570812004-08-10 22:47:03 +0000459 } else if (Opcode == PPC::CALLpcrel) {
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000460 O << TII.getName(Opcode) << " ";
Misha Brukman61114612004-07-20 00:42:19 +0000461 printOp(MI->getOperand(0));
462 O << "\n";
463 return;
Misha Brukman5b570812004-08-10 22:47:03 +0000464 } else if (Opcode == PPC::CALLindirect) {
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000465 O << TII.getName(Opcode) << " ";
466 printImmOp(MI->getOperand(0), ArgType[0]);
Misha Brukman61114612004-07-20 00:42:19 +0000467 O << ", ";
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000468 printImmOp(MI->getOperand(1), ArgType[0]);
Misha Brukman61114612004-07-20 00:42:19 +0000469 O << "\n";
470 return;
Misha Brukman5b570812004-08-10 22:47:03 +0000471 } else if (Opcode == PPC::MovePCtoLR) {
Misha Brukman61114612004-07-20 00:42:19 +0000472 // FIXME: should probably be converted to cout.width and cout.fill
Misha Brukmancf8d2442004-07-26 16:28:33 +0000473 O << "bl \"L0000" << LabelNumber << "$pb\"\n";
474 O << "\"L0000" << LabelNumber << "$pb\":\n";
Misha Brukman218bec72004-06-29 17:13:26 +0000475 O << "\tmflr ";
476 printOp(MI->getOperand(0));
Misha Brukman218bec72004-06-29 17:13:26 +0000477 O << "\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000478 return;
479 }
480
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000481 O << TII.getName(Opcode) << " ";
Misha Brukman5b570812004-08-10 22:47:03 +0000482 if (Opcode == PPC::LOADLoDirect || Opcode == PPC::LOADLoIndirect) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000483 printOp(MI->getOperand(0));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000484 O << ", lo16(";
Nate Begemanb73a7112004-08-13 09:32:01 +0000485 printOp(MI->getOperand(2), true /* LoadAddrOp */);
Misha Brukmancf8d2442004-07-26 16:28:33 +0000486 O << "-\"L0000" << LabelNumber << "$pb\")";
Misha Brukman218bec72004-06-29 17:13:26 +0000487 O << "(";
Misha Brukman5b570812004-08-10 22:47:03 +0000488 if (MI->getOperand(1).getReg() == PPC::R0)
Misha Brukman218bec72004-06-29 17:13:26 +0000489 O << "0";
490 else
491 printOp(MI->getOperand(1));
492 O << ")\n";
Misha Brukman5b570812004-08-10 22:47:03 +0000493 } else if (Opcode == PPC::LOADHiAddr) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000494 printOp(MI->getOperand(0));
495 O << ", ";
Misha Brukman5b570812004-08-10 22:47:03 +0000496 if (MI->getOperand(1).getReg() == PPC::R0)
Misha Brukman218bec72004-06-29 17:13:26 +0000497 O << "0";
498 else
499 printOp(MI->getOperand(1));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000500 O << ", ha16(" ;
Nate Begemanb73a7112004-08-13 09:32:01 +0000501 printOp(MI->getOperand(2), true /* LoadAddrOp */);
Misha Brukmancf8d2442004-07-26 16:28:33 +0000502 O << "-\"L0000" << LabelNumber << "$pb\")\n";
Misha Brukman5b570812004-08-10 22:47:03 +0000503 } else if (ArgCount == 3 && ArgType[1] == PPCII::Disimm16) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000504 printOp(MI->getOperand(0));
505 O << ", ";
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000506 printImmOp(MI->getOperand(1), ArgType[1]);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000507 O << "(";
Misha Brukmanb9e8f972004-06-30 21:54:12 +0000508 if (MI->getOperand(2).hasAllocatedReg() &&
Misha Brukman5b570812004-08-10 22:47:03 +0000509 MI->getOperand(2).getReg() == PPC::R0)
Misha Brukman46fd00a2004-06-24 23:04:11 +0000510 O << "0";
511 else
512 printOp(MI->getOperand(2));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000513 O << ")\n";
514 } else {
Misha Brukman7f484a52004-06-24 23:51:00 +0000515 for (i = 0; i < ArgCount; ++i) {
Misha Brukmanab967902004-07-27 18:40:39 +0000516 // addi and friends
Misha Brukman5b570812004-08-10 22:47:03 +0000517 if (i == 1 && ArgCount == 3 && ArgType[2] == PPCII::Simm16 &&
Misha Brukman4363bdb2004-07-01 21:09:12 +0000518 MI->getOperand(1).hasAllocatedReg() &&
Misha Brukman5b570812004-08-10 22:47:03 +0000519 MI->getOperand(1).getReg() == PPC::R0) {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000520 O << "0";
Misha Brukmanab967902004-07-27 18:40:39 +0000521 // for long branch support, bc $+8
522 } else if (i == 1 && ArgCount == 2 && MI->getOperand(1).isImmediate() &&
523 TII.isBranch(MI->getOpcode())) {
524 O << "$+8";
525 assert(8 == MI->getOperand(i).getImmedValue()
526 && "branch off PC not to pc+8?");
527 //printOp(MI->getOperand(i));
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000528 } else if (MI->getOperand(i).isImmediate()) {
529 printImmOp(MI->getOperand(i), ArgType[i]);
Misha Brukman218bec72004-06-29 17:13:26 +0000530 } else {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000531 printOp(MI->getOperand(i));
532 }
Misha Brukman7f484a52004-06-24 23:51:00 +0000533 if (ArgCount - 1 == i)
Misha Brukman46fd00a2004-06-24 23:04:11 +0000534 O << "\n";
535 else
536 O << ", ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000537 }
538 }
Nate Begemane59bf592004-08-14 22:09:10 +0000539 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000540}
541
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000542// SwitchSection - Switch to the specified section of the executable if we are
543// not already in it!
544//
545static void SwitchSection(std::ostream &OS, std::string &CurSection,
546 const char *NewSection) {
547 if (CurSection != NewSection) {
548 CurSection = NewSection;
549 if (!CurSection.empty())
550 OS << "\t" << NewSection << "\n";
551 }
552}
553
Nate Begemane59bf592004-08-14 22:09:10 +0000554bool PowerPCAsmPrinter::doFinalization(Module &M) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000555 const TargetData &TD = TM.getTargetData();
556 std::string CurSection;
557
558 // Print out module-level global variables here.
559 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
560 if (I->hasInitializer()) { // External global require no code
561 O << "\n\n";
562 std::string name = Mang->getValueName(I);
563 Constant *C = I->getInitializer();
564 unsigned Size = TD.getTypeSize(C->getType());
565 unsigned Align = TD.getTypeAlignment(C->getType());
566
Misha Brukman97a296f2004-07-21 20:11:11 +0000567 if (C->isNullValue() && /* FIXME: Verify correct */
568 (I->hasInternalLinkage() || I->hasWeakLinkage())) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000569 SwitchSection(O, CurSection, ".data");
570 if (I->hasInternalLinkage())
Misha Brukmane2eceb52004-07-23 16:08:20 +0000571 O << ".lcomm " << name << "," << TD.getTypeSize(C->getType())
Misha Brukman218bec72004-06-29 17:13:26 +0000572 << "," << (unsigned)TD.getTypeAlignment(C->getType());
573 else
Misha Brukmane2eceb52004-07-23 16:08:20 +0000574 O << ".comm " << name << "," << TD.getTypeSize(C->getType());
Misha Brukman218bec72004-06-29 17:13:26 +0000575 O << "\t\t; ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000576 WriteAsOperand(O, I, true, true, &M);
577 O << "\n";
578 } else {
579 switch (I->getLinkage()) {
580 case GlobalValue::LinkOnceLinkage:
Misha Brukman97a296f2004-07-21 20:11:11 +0000581 O << ".section __TEXT,__textcoal_nt,coalesced,no_toc\n"
582 << ".weak_definition " << name << '\n'
583 << ".private_extern " << name << '\n'
584 << ".section __DATA,__datacoal_nt,coalesced,no_toc\n";
585 LinkOnceStubs.insert(name);
586 break;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000587 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
588 // Nonnull linkonce -> weak
589 O << "\t.weak " << name << "\n";
590 SwitchSection(O, CurSection, "");
591 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
592 break;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000593 case GlobalValue::AppendingLinkage:
594 // FIXME: appending linkage variables should go into a section of
595 // their name or something. For now, just emit them as external.
596 case GlobalValue::ExternalLinkage:
597 // If external or appending, declare as a global symbol
598 O << "\t.globl " << name << "\n";
599 // FALL THROUGH
600 case GlobalValue::InternalLinkage:
Misha Brukman61297ee2004-06-29 23:40:57 +0000601 SwitchSection(O, CurSection, ".data");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000602 break;
603 }
604
605 O << "\t.align " << Align << "\n";
Misha Brukman218bec72004-06-29 17:13:26 +0000606 O << name << ":\t\t\t\t; ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000607 WriteAsOperand(O, I, true, true, &M);
608 O << " = ";
609 WriteAsOperand(O, C, false, false, &M);
610 O << "\n";
611 emitGlobalConstant(C);
612 }
613 }
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000614
615 // Output stubs for dynamically-linked functions
616 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
617 i != e; ++i)
Misha Brukman46fd00a2004-06-24 23:04:11 +0000618 {
Misha Brukmane2eceb52004-07-23 16:08:20 +0000619 O << ".data\n";
620 O << ".section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32\n";
621 O << "\t.align 2\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000622 O << "L" << *i << "$stub:\n";
623 O << "\t.indirect_symbol " << *i << "\n";
624 O << "\tmflr r0\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000625 O << "\tbcl 20,31,L0$" << *i << "\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000626 O << "L0$" << *i << ":\n";
627 O << "\tmflr r11\n";
628 O << "\taddis r11,r11,ha16(L" << *i << "$lazy_ptr-L0$" << *i << ")\n";
629 O << "\tmtlr r0\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000630 O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr-L0$" << *i << ")(r11)\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000631 O << "\tmtctr r12\n";
632 O << "\tbctr\n";
633 O << ".data\n";
634 O << ".lazy_symbol_pointer\n";
635 O << "L" << *i << "$lazy_ptr:\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000636 O << "\t.indirect_symbol " << *i << "\n";
637 O << "\t.long dyld_stub_binding_helper\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000638 }
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000639
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000640 O << "\n";
641
642 // Output stubs for external global variables
643 if (GVStubs.begin() != GVStubs.end())
Misha Brukmane2eceb52004-07-23 16:08:20 +0000644 O << ".data\n.non_lazy_symbol_pointer\n";
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000645 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
646 i != e; ++i) {
647 O << "L" << *i << "$non_lazy_ptr:\n";
648 O << "\t.indirect_symbol " << *i << "\n";
649 O << "\t.long\t0\n";
650 }
651
Nate Begemane59bf592004-08-14 22:09:10 +0000652 // Output stubs for link-once variables
653 if (LinkOnceStubs.begin() != LinkOnceStubs.end())
654 O << ".data\n.align 2\n";
655 for (std::set<std::string>::iterator i = LinkOnceStubs.begin(),
656 e = LinkOnceStubs.end(); i != e; ++i) {
657 O << "L" << *i << "$non_lazy_ptr:\n"
658 << "\t.long\t" << *i << '\n';
659 }
660
Chris Lattnera3840792004-08-16 23:25:21 +0000661 AsmPrinter::doFinalization(M);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000662 return false; // success
663}