blob: df7b1bece454d522dd2dda6b53e02ebdc30f686b [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/// 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.
Nate Begemane59bf592004-08-14 22:09:10 +0000152void PowerPCAsmPrinter::emitGlobalConstant(const Constant *CV) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000153 const TargetData &TD = TM.getTargetData();
154
Misha Brukmane48178e2004-07-20 15:45:27 +0000155 if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000156 if (isStringCompatible(CVA)) {
Misha Brukman218bec72004-06-29 17:13:26 +0000157 O << "\t.ascii ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000158 printAsCString(O, CVA);
159 O << "\n";
160 } else { // Not a string. Print the values in successive locations
Chris Lattner6173cd92004-08-04 17:29:14 +0000161 for (unsigned i=0, e = CVA->getNumOperands(); i != e; i++)
162 emitGlobalConstant(CVA->getOperand(i));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000163 }
164 return;
165 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
166 // Print the fields in successive locations. Pad to align if needed!
167 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000168 unsigned sizeSoFar = 0;
Chris Lattner6173cd92004-08-04 17:29:14 +0000169 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; i++) {
170 const Constant* field = CVS->getOperand(i);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000171
172 // Check if padding is needed and insert one or more 0s.
173 unsigned fieldSize = TD.getTypeSize(field->getType());
Chris Lattner6173cd92004-08-04 17:29:14 +0000174 unsigned padSize = ((i == e-1? cvsLayout->StructSize
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000175 : cvsLayout->MemberOffsets[i+1])
176 - cvsLayout->MemberOffsets[i]) - fieldSize;
177 sizeSoFar += fieldSize + padSize;
178
179 // Now print the actual field value
180 emitGlobalConstant(field);
181
182 // Insert the field padding unless it's zero bytes...
183 if (padSize)
184 O << "\t.space\t " << padSize << "\n";
185 }
186 assert(sizeSoFar == cvsLayout->StructSize &&
187 "Layout of constant struct may be incorrect!");
188 return;
189 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
190 // FP Constants are printed as integer constants to avoid losing
191 // precision...
192 double Val = CFP->getValue();
Nate Begemane59bf592004-08-14 22:09:10 +0000193 union DU { // Abide by C TBAA rules
194 double FVal;
195 uint64_t UVal;
196 struct {
197 uint32_t MSWord;
198 uint32_t LSWord;
199 } T;
200 } U;
201 U.FVal = Val;
202
203 O << ".long\t" << U.T.MSWord << "\t; double most significant word "
204 << Val << "\n";
205 O << ".long\t" << U.T.LSWord << "\t; double least significant word "
206 << Val << "\n";
207 return;
Misha Brukmanf63bc192004-07-28 19:12:24 +0000208 } else if (CV->getType() == Type::ULongTy || CV->getType() == Type::LongTy) {
Misha Brukman2bf183c2004-06-25 15:42:10 +0000209 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
210 union DU { // Abide by C TBAA rules
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000211 int64_t UVal;
212 struct {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000213 uint32_t MSWord;
214 uint32_t LSWord;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000215 } T;
216 } U;
217 U.UVal = CI->getRawValue();
218
Misha Brukman218bec72004-06-29 17:13:26 +0000219 O << ".long\t" << U.T.MSWord << "\t; Double-word most significant word "
Misha Brukman46fd00a2004-06-24 23:04:11 +0000220 << U.UVal << "\n";
Misha Brukman29188c62004-07-16 19:01:13 +0000221 O << ".long\t" << U.T.LSWord << "\t; Double-word least significant word "
Misha Brukman46fd00a2004-06-24 23:04:11 +0000222 << U.UVal << "\n";
223 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000224 }
225 }
226
227 const Type *type = CV->getType();
228 O << "\t";
Misha Brukmand71bd562004-06-21 17:19:08 +0000229 switch (type->getTypeID()) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000230 case Type::UByteTyID: case Type::SByteTyID:
231 O << ".byte";
232 break;
233 case Type::UShortTyID: case Type::ShortTyID:
234 O << ".short";
235 break;
236 case Type::BoolTyID:
237 case Type::PointerTyID:
238 case Type::UIntTyID: case Type::IntTyID:
239 O << ".long";
240 break;
241 case Type::ULongTyID: case Type::LongTyID:
Misha Brukman46fd00a2004-06-24 23:04:11 +0000242 assert (0 && "Should have already output double-word constant.");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000243 case Type::FloatTyID: case Type::DoubleTyID:
244 assert (0 && "Should have already output floating point constant.");
245 default:
Misha Brukman97a296f2004-07-21 20:11:11 +0000246 if (CV == Constant::getNullValue(type)) { // Zero initializer?
247 O << ".space\t" << TD.getTypeSize(type) << "\n";
248 return;
249 }
250 std::cerr << "Can't handle printing: " << *CV;
251 abort();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000252 break;
253 }
254 O << "\t";
255 emitConstantValueOnly(CV);
256 O << "\n";
257}
258
259/// printConstantPool - Print to the current output stream assembly
260/// representations of the constants in the constant pool MCP. This is
261/// used to print out constants which have been "spilled to memory" by
262/// the code generator.
263///
Nate Begemane59bf592004-08-14 22:09:10 +0000264void PowerPCAsmPrinter::printConstantPool(MachineConstantPool *MCP) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000265 const std::vector<Constant*> &CP = MCP->getConstants();
266 const TargetData &TD = TM.getTargetData();
267
268 if (CP.empty()) return;
269
270 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
271 O << "\t.const\n";
272 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
273 << "\n";
Misha Brukman218bec72004-06-29 17:13:26 +0000274 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t;"
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000275 << *CP[i] << "\n";
276 emitGlobalConstant(CP[i]);
277 }
278}
279
280/// runOnMachineFunction - This uses the printMachineInstruction()
281/// method to print assembly for each instruction.
282///
Nate Begemane59bf592004-08-14 22:09:10 +0000283bool PowerPCAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattnera3840792004-08-16 23:25:21 +0000284 setupMachineFunction(MF);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000285 O << "\n\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000286
287 // Print out constants referenced by the function
288 printConstantPool(MF.getConstantPool());
289
290 // Print out labels for the function.
291 O << "\t.text\n";
292 O << "\t.globl\t" << CurrentFnName << "\n";
Misha Brukman61297ee2004-06-29 23:40:57 +0000293 O << "\t.align 2\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000294 O << CurrentFnName << ":\n";
295
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000296 // Print out code for the function.
297 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
298 I != E; ++I) {
299 // Print a label for the basic block.
Misha Brukman218bec72004-06-29 17:13:26 +0000300 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t; "
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000301 << I->getBasicBlock()->getName() << "\n";
302 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
Misha Brukman46fd00a2004-06-24 23:04:11 +0000303 II != E; ++II) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000304 // Print the assembly for the instruction.
305 O << "\t";
306 printMachineInstruction(II);
307 }
308 }
Misha Brukmancf8d2442004-07-26 16:28:33 +0000309 ++LabelNumber;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000310
311 // We didn't modify anything.
312 return false;
313}
314
Nate Begemane59bf592004-08-14 22:09:10 +0000315void PowerPCAsmPrinter::printOp(const MachineOperand &MO,
Nate Begemanb73a7112004-08-13 09:32:01 +0000316 bool LoadAddrOp /* = false */) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000317 const MRegisterInfo &RI = *TM.getRegisterInfo();
318 int new_symbol;
319
320 switch (MO.getType()) {
321 case MachineOperand::MO_VirtualRegister:
322 if (Value *V = MO.getVRegValueOrNull()) {
323 O << "<" << V->getName() << ">";
324 return;
325 }
326 // FALLTHROUGH
327 case MachineOperand::MO_MachineRegister:
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000328 case MachineOperand::MO_CCRegister:
Misha Brukman7f484a52004-06-24 23:51:00 +0000329 O << LowercaseString(RI.get(MO.getReg()).Name);
330 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000331
332 case MachineOperand::MO_SignExtendedImmed:
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000333 case MachineOperand::MO_UnextendedImmed:
334 std::cerr << "printOp() does not handle immediate values\n";
335 abort();
Misha Brukman97a296f2004-07-21 20:11:11 +0000336 return;
337
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000338 case MachineOperand::MO_PCRelativeDisp:
339 std::cerr << "Shouldn't use addPCDisp() when building PPC MachineInstrs";
340 abort();
341 return;
342
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000343 case MachineOperand::MO_MachineBasicBlock: {
344 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
345 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
Misha Brukman218bec72004-06-29 17:13:26 +0000346 << "_" << MBBOp->getNumber() << "\t; "
Misha Brukman2bf183c2004-06-25 15:42:10 +0000347 << MBBOp->getBasicBlock()->getName();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000348 return;
349 }
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000350
351 case MachineOperand::MO_ConstantPoolIndex:
352 O << ".CPI" << CurrentFnName << "_" << MO.getConstantPoolIndex();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000353 return;
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000354
355 case MachineOperand::MO_ExternalSymbol:
356 O << MO.getSymbolName();
357 return;
358
Nate Begemanb73a7112004-08-13 09:32:01 +0000359 case MachineOperand::MO_GlobalAddress: {
360 GlobalValue *GV = MO.getGlobal();
361 std::string Name = Mang->getValueName(GV);
Misha Brukmane2eceb52004-07-23 16:08:20 +0000362
Nate Begemanb73a7112004-08-13 09:32:01 +0000363 // Dynamically-resolved functions need a stub for the function. Be
364 // wary however not to output $stub for external functions whose addresses
365 // are taken. Those should be emitted as $non_lazy_ptr below.
366 Function *F = dyn_cast<Function>(GV);
367 if (F && F->isExternal() && !LoadAddrOp &&
Chris Lattnera3840792004-08-16 23:25:21 +0000368 getTM().CalledFunctions.count(F)) {
Nate Begemanb73a7112004-08-13 09:32:01 +0000369 FnStubs.insert(Name);
370 O << "L" << Name << "$stub";
371 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000372 }
Nate Begemane59bf592004-08-14 22:09:10 +0000373
Nate Begemanb73a7112004-08-13 09:32:01 +0000374 // External global variables need a non-lazily-resolved stub
Chris Lattnera3840792004-08-16 23:25:21 +0000375 if (GV->isExternal() && getTM().AddressTaken.count(GV)) {
Nate Begemanb73a7112004-08-13 09:32:01 +0000376 GVStubs.insert(Name);
377 O << "L" << Name << "$non_lazy_ptr";
378 return;
379 }
Nate Begemane59bf592004-08-14 22:09:10 +0000380
Chris Lattnera3840792004-08-16 23:25:21 +0000381 if (F && LoadAddrOp && getTM().AddressTaken.count(GV)) {
Nate Begemane59bf592004-08-14 22:09:10 +0000382 LinkOnceStubs.insert(Name);
383 O << "L" << Name << "$non_lazy_ptr";
384 return;
385 }
Nate Begemanb73a7112004-08-13 09:32:01 +0000386
387 O << Mang->getValueName(GV);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000388 return;
Nate Begemanb73a7112004-08-13 09:32:01 +0000389 }
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000390
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000391 default:
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000392 O << "<unknown operand type: " << MO.getType() << ">";
Misha Brukman22e12072004-06-25 15:11:34 +0000393 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000394 }
395}
396
Nate Begemane59bf592004-08-14 22:09:10 +0000397void PowerPCAsmPrinter::printImmOp(const MachineOperand &MO, unsigned ArgType) {
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000398 int Imm = MO.getImmedValue();
Misha Brukman5b570812004-08-10 22:47:03 +0000399 if (ArgType == PPCII::Simm16 || ArgType == PPCII::Disimm16) {
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000400 O << (short)Imm;
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000401 } else {
402 O << Imm;
403 }
404}
405
Nate Begemane59bf592004-08-14 22:09:10 +0000406/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
407/// the current output stream.
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000408///
Nate Begemane59bf592004-08-14 22:09:10 +0000409void PowerPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
410 ++EmittedInsts;
411 if (printInstruction(MI))
412 return; // Printer was automatically generated
413
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000414 unsigned Opcode = MI->getOpcode();
415 const TargetInstrInfo &TII = *TM.getInstrInfo();
416 const TargetInstrDescriptor &Desc = TII.get(Opcode);
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000417 unsigned i;
Misha Brukmanc6cc10f2004-06-25 19:24:52 +0000418
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000419 unsigned ArgCount = MI->getNumOperands();
420 unsigned ArgType[] = {
Misha Brukman5b570812004-08-10 22:47:03 +0000421 (Desc.TSFlags >> PPCII::Arg0TypeShift) & PPCII::ArgTypeMask,
422 (Desc.TSFlags >> PPCII::Arg1TypeShift) & PPCII::ArgTypeMask,
423 (Desc.TSFlags >> PPCII::Arg2TypeShift) & PPCII::ArgTypeMask,
424 (Desc.TSFlags >> PPCII::Arg3TypeShift) & PPCII::ArgTypeMask,
425 (Desc.TSFlags >> PPCII::Arg4TypeShift) & PPCII::ArgTypeMask
Misha Brukman22e12072004-06-25 15:11:34 +0000426 };
Misha Brukman5b570812004-08-10 22:47:03 +0000427 assert(((Desc.TSFlags & PPCII::VMX) == 0) &&
Misha Brukman46fd00a2004-06-24 23:04:11 +0000428 "Instruction requires VMX support");
Misha Brukman5b570812004-08-10 22:47:03 +0000429 assert(((Desc.TSFlags & PPCII::PPC64) == 0) &&
Misha Brukman46fd00a2004-06-24 23:04:11 +0000430 "Instruction requires 64 bit support");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000431
Misha Brukman61114612004-07-20 00:42:19 +0000432 // CALLpcrel and CALLindirect are handled specially here to print only the
433 // appropriate number of args that the assembler expects. This is because
434 // may have many arguments appended to record the uses of registers that are
435 // holding arguments to the called function.
Misha Brukman5b570812004-08-10 22:47:03 +0000436 if (Opcode == PPC::COND_BRANCH) {
Misha Brukmanab967902004-07-27 18:40:39 +0000437 std::cerr << "Error: untranslated conditional branch psuedo instruction!\n";
438 abort();
Misha Brukman5b570812004-08-10 22:47:03 +0000439 } else if (Opcode == PPC::IMPLICIT_DEF) {
Misha Brukman29188c62004-07-16 19:01:13 +0000440 O << "; IMPLICIT DEF ";
441 printOp(MI->getOperand(0));
442 O << "\n";
443 return;
Misha Brukman5b570812004-08-10 22:47:03 +0000444 } else if (Opcode == PPC::CALLpcrel) {
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000445 O << TII.getName(Opcode) << " ";
Misha Brukman61114612004-07-20 00:42:19 +0000446 printOp(MI->getOperand(0));
447 O << "\n";
448 return;
Misha Brukman5b570812004-08-10 22:47:03 +0000449 } else if (Opcode == PPC::CALLindirect) {
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000450 O << TII.getName(Opcode) << " ";
451 printImmOp(MI->getOperand(0), ArgType[0]);
Misha Brukman61114612004-07-20 00:42:19 +0000452 O << ", ";
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000453 printImmOp(MI->getOperand(1), ArgType[0]);
Misha Brukman61114612004-07-20 00:42:19 +0000454 O << "\n";
455 return;
Misha Brukman5b570812004-08-10 22:47:03 +0000456 } else if (Opcode == PPC::MovePCtoLR) {
Misha Brukman61114612004-07-20 00:42:19 +0000457 // FIXME: should probably be converted to cout.width and cout.fill
Misha Brukmancf8d2442004-07-26 16:28:33 +0000458 O << "bl \"L0000" << LabelNumber << "$pb\"\n";
459 O << "\"L0000" << LabelNumber << "$pb\":\n";
Misha Brukman218bec72004-06-29 17:13:26 +0000460 O << "\tmflr ";
461 printOp(MI->getOperand(0));
Misha Brukman218bec72004-06-29 17:13:26 +0000462 O << "\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000463 return;
464 }
465
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000466 O << TII.getName(Opcode) << " ";
Misha Brukman5b570812004-08-10 22:47:03 +0000467 if (Opcode == PPC::LOADLoDirect || Opcode == PPC::LOADLoIndirect) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000468 printOp(MI->getOperand(0));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000469 O << ", lo16(";
Nate Begemanb73a7112004-08-13 09:32:01 +0000470 printOp(MI->getOperand(2), true /* LoadAddrOp */);
Misha Brukmancf8d2442004-07-26 16:28:33 +0000471 O << "-\"L0000" << LabelNumber << "$pb\")";
Misha Brukman218bec72004-06-29 17:13:26 +0000472 O << "(";
Misha Brukman5b570812004-08-10 22:47:03 +0000473 if (MI->getOperand(1).getReg() == PPC::R0)
Misha Brukman218bec72004-06-29 17:13:26 +0000474 O << "0";
475 else
476 printOp(MI->getOperand(1));
477 O << ")\n";
Misha Brukman5b570812004-08-10 22:47:03 +0000478 } else if (Opcode == PPC::LOADHiAddr) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000479 printOp(MI->getOperand(0));
480 O << ", ";
Misha Brukman5b570812004-08-10 22:47:03 +0000481 if (MI->getOperand(1).getReg() == PPC::R0)
Misha Brukman218bec72004-06-29 17:13:26 +0000482 O << "0";
483 else
484 printOp(MI->getOperand(1));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000485 O << ", ha16(" ;
Nate Begemanb73a7112004-08-13 09:32:01 +0000486 printOp(MI->getOperand(2), true /* LoadAddrOp */);
Misha Brukmancf8d2442004-07-26 16:28:33 +0000487 O << "-\"L0000" << LabelNumber << "$pb\")\n";
Misha Brukman5b570812004-08-10 22:47:03 +0000488 } else if (ArgCount == 3 && ArgType[1] == PPCII::Disimm16) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000489 printOp(MI->getOperand(0));
490 O << ", ";
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000491 printImmOp(MI->getOperand(1), ArgType[1]);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000492 O << "(";
Misha Brukmanb9e8f972004-06-30 21:54:12 +0000493 if (MI->getOperand(2).hasAllocatedReg() &&
Misha Brukman5b570812004-08-10 22:47:03 +0000494 MI->getOperand(2).getReg() == PPC::R0)
Misha Brukman46fd00a2004-06-24 23:04:11 +0000495 O << "0";
496 else
497 printOp(MI->getOperand(2));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000498 O << ")\n";
499 } else {
Misha Brukman7f484a52004-06-24 23:51:00 +0000500 for (i = 0; i < ArgCount; ++i) {
Misha Brukmanab967902004-07-27 18:40:39 +0000501 // addi and friends
Misha Brukman5b570812004-08-10 22:47:03 +0000502 if (i == 1 && ArgCount == 3 && ArgType[2] == PPCII::Simm16 &&
Misha Brukman4363bdb2004-07-01 21:09:12 +0000503 MI->getOperand(1).hasAllocatedReg() &&
Misha Brukman5b570812004-08-10 22:47:03 +0000504 MI->getOperand(1).getReg() == PPC::R0) {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000505 O << "0";
Misha Brukmanab967902004-07-27 18:40:39 +0000506 // for long branch support, bc $+8
507 } else if (i == 1 && ArgCount == 2 && MI->getOperand(1).isImmediate() &&
508 TII.isBranch(MI->getOpcode())) {
509 O << "$+8";
510 assert(8 == MI->getOperand(i).getImmedValue()
511 && "branch off PC not to pc+8?");
512 //printOp(MI->getOperand(i));
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000513 } else if (MI->getOperand(i).isImmediate()) {
514 printImmOp(MI->getOperand(i), ArgType[i]);
Misha Brukman218bec72004-06-29 17:13:26 +0000515 } else {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000516 printOp(MI->getOperand(i));
517 }
Misha Brukman7f484a52004-06-24 23:51:00 +0000518 if (ArgCount - 1 == i)
Misha Brukman46fd00a2004-06-24 23:04:11 +0000519 O << "\n";
520 else
521 O << ", ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000522 }
523 }
Nate Begemane59bf592004-08-14 22:09:10 +0000524 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000525}
526
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000527// SwitchSection - Switch to the specified section of the executable if we are
528// not already in it!
529//
530static void SwitchSection(std::ostream &OS, std::string &CurSection,
531 const char *NewSection) {
532 if (CurSection != NewSection) {
533 CurSection = NewSection;
534 if (!CurSection.empty())
535 OS << "\t" << NewSection << "\n";
536 }
537}
538
Nate Begemane59bf592004-08-14 22:09:10 +0000539bool PowerPCAsmPrinter::doFinalization(Module &M) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000540 const TargetData &TD = TM.getTargetData();
541 std::string CurSection;
542
543 // Print out module-level global variables here.
544 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
545 if (I->hasInitializer()) { // External global require no code
546 O << "\n\n";
547 std::string name = Mang->getValueName(I);
548 Constant *C = I->getInitializer();
549 unsigned Size = TD.getTypeSize(C->getType());
550 unsigned Align = TD.getTypeAlignment(C->getType());
551
Misha Brukman97a296f2004-07-21 20:11:11 +0000552 if (C->isNullValue() && /* FIXME: Verify correct */
553 (I->hasInternalLinkage() || I->hasWeakLinkage())) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000554 SwitchSection(O, CurSection, ".data");
555 if (I->hasInternalLinkage())
Misha Brukmane2eceb52004-07-23 16:08:20 +0000556 O << ".lcomm " << name << "," << TD.getTypeSize(C->getType())
Misha Brukman218bec72004-06-29 17:13:26 +0000557 << "," << (unsigned)TD.getTypeAlignment(C->getType());
558 else
Misha Brukmane2eceb52004-07-23 16:08:20 +0000559 O << ".comm " << name << "," << TD.getTypeSize(C->getType());
Misha Brukman218bec72004-06-29 17:13:26 +0000560 O << "\t\t; ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000561 WriteAsOperand(O, I, true, true, &M);
562 O << "\n";
563 } else {
564 switch (I->getLinkage()) {
565 case GlobalValue::LinkOnceLinkage:
Misha Brukman97a296f2004-07-21 20:11:11 +0000566 O << ".section __TEXT,__textcoal_nt,coalesced,no_toc\n"
567 << ".weak_definition " << name << '\n'
568 << ".private_extern " << name << '\n'
569 << ".section __DATA,__datacoal_nt,coalesced,no_toc\n";
570 LinkOnceStubs.insert(name);
571 break;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000572 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
573 // Nonnull linkonce -> weak
574 O << "\t.weak " << name << "\n";
575 SwitchSection(O, CurSection, "");
576 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
577 break;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000578 case GlobalValue::AppendingLinkage:
579 // FIXME: appending linkage variables should go into a section of
580 // their name or something. For now, just emit them as external.
581 case GlobalValue::ExternalLinkage:
582 // If external or appending, declare as a global symbol
583 O << "\t.globl " << name << "\n";
584 // FALL THROUGH
585 case GlobalValue::InternalLinkage:
Misha Brukman61297ee2004-06-29 23:40:57 +0000586 SwitchSection(O, CurSection, ".data");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000587 break;
588 }
589
590 O << "\t.align " << Align << "\n";
Misha Brukman218bec72004-06-29 17:13:26 +0000591 O << name << ":\t\t\t\t; ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000592 WriteAsOperand(O, I, true, true, &M);
593 O << " = ";
594 WriteAsOperand(O, C, false, false, &M);
595 O << "\n";
596 emitGlobalConstant(C);
597 }
598 }
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000599
600 // Output stubs for dynamically-linked functions
601 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
602 i != e; ++i)
Misha Brukman46fd00a2004-06-24 23:04:11 +0000603 {
Misha Brukmane2eceb52004-07-23 16:08:20 +0000604 O << ".data\n";
605 O << ".section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32\n";
606 O << "\t.align 2\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000607 O << "L" << *i << "$stub:\n";
608 O << "\t.indirect_symbol " << *i << "\n";
609 O << "\tmflr r0\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000610 O << "\tbcl 20,31,L0$" << *i << "\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000611 O << "L0$" << *i << ":\n";
612 O << "\tmflr r11\n";
613 O << "\taddis r11,r11,ha16(L" << *i << "$lazy_ptr-L0$" << *i << ")\n";
614 O << "\tmtlr r0\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000615 O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr-L0$" << *i << ")(r11)\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000616 O << "\tmtctr r12\n";
617 O << "\tbctr\n";
618 O << ".data\n";
619 O << ".lazy_symbol_pointer\n";
620 O << "L" << *i << "$lazy_ptr:\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000621 O << "\t.indirect_symbol " << *i << "\n";
622 O << "\t.long dyld_stub_binding_helper\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000623 }
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000624
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000625 O << "\n";
626
627 // Output stubs for external global variables
628 if (GVStubs.begin() != GVStubs.end())
Misha Brukmane2eceb52004-07-23 16:08:20 +0000629 O << ".data\n.non_lazy_symbol_pointer\n";
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000630 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
631 i != e; ++i) {
632 O << "L" << *i << "$non_lazy_ptr:\n";
633 O << "\t.indirect_symbol " << *i << "\n";
634 O << "\t.long\t0\n";
635 }
636
Nate Begemane59bf592004-08-14 22:09:10 +0000637 // Output stubs for link-once variables
638 if (LinkOnceStubs.begin() != LinkOnceStubs.end())
639 O << ".data\n.align 2\n";
640 for (std::set<std::string>::iterator i = LinkOnceStubs.begin(),
641 e = LinkOnceStubs.end(); i != e; ++i) {
642 O << "L" << *i << "$non_lazy_ptr:\n"
643 << "\t.long\t" << *i << '\n';
644 }
645
Chris Lattnera3840792004-08-16 23:25:21 +0000646 AsmPrinter::doFinalization(M);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000647 return false; // success
648}