blob: 4ec7b7daa97d99b5aefa099f09c9719552855498 [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"
Misha Brukman3d9a6c22004-08-11 00:09:42 +000022#include "PPC32TargetMachine.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"
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"
30#include "llvm/Target/TargetMachine.h"
31#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>
Misha Brukman5dfe3a92004-06-21 16:55:25 +000037
38namespace llvm {
39
40namespace {
41 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
42
43 struct Printer : public MachineFunctionPass {
44 /// Output stream on which we're printing assembly code.
45 ///
46 std::ostream &O;
47
48 /// Target machine description which we query for reg. names, data
49 /// layout, etc.
50 ///
Misha Brukman3d9a6c22004-08-11 00:09:42 +000051 PPC32TargetMachine &TM;
Misha Brukman5dfe3a92004-06-21 16:55:25 +000052
53 /// Name-mangler for global names.
54 ///
55 Mangler *Mang;
Misha Brukman97a296f2004-07-21 20:11:11 +000056 std::set<std::string> FnStubs, GVStubs, LinkOnceStubs;
Misha Brukman5dfe3a92004-06-21 16:55:25 +000057 std::set<std::string> Strings;
58
Misha Brukmancf8d2442004-07-26 16:28:33 +000059 Printer(std::ostream &o, TargetMachine &tm) : O(o),
Misha Brukman3d9a6c22004-08-11 00:09:42 +000060 TM(reinterpret_cast<PPC32TargetMachine&>(tm)), LabelNumber(0) {}
Misha Brukman5dfe3a92004-06-21 16:55:25 +000061
Misha Brukman5dfe3a92004-06-21 16:55:25 +000062 /// Cache of mangled name for current function. This is
63 /// recalculated at the beginning of each call to
64 /// runOnMachineFunction().
65 ///
66 std::string CurrentFnName;
67
Misha Brukmancf8d2442004-07-26 16:28:33 +000068 /// Unique incrementer for label values for referencing Global values.
Misha Brukman218bec72004-06-29 17:13:26 +000069 ///
Misha Brukmancf8d2442004-07-26 16:28:33 +000070 unsigned LabelNumber;
71
Misha Brukman5dfe3a92004-06-21 16:55:25 +000072 virtual const char *getPassName() const {
Misha Brukman3d9a6c22004-08-11 00:09:42 +000073 return "PPC32 Assembly Printer";
Misha Brukman5dfe3a92004-06-21 16:55:25 +000074 }
75
76 void printMachineInstruction(const MachineInstr *MI);
Nate Begemanb73a7112004-08-13 09:32:01 +000077 void printOp(const MachineOperand &MO, bool LoadAddrOp = false);
Misha Brukmanaf313fb2004-07-28 00:00:48 +000078 void printImmOp(const MachineOperand &MO, unsigned ArgType);
Misha Brukman5dfe3a92004-06-21 16:55:25 +000079 void printConstantPool(MachineConstantPool *MCP);
80 bool runOnMachineFunction(MachineFunction &F);
81 bool doInitialization(Module &M);
82 bool doFinalization(Module &M);
83 void emitGlobalConstant(const Constant* CV);
84 void emitConstantValueOnly(const Constant *CV);
85 };
86} // end of anonymous namespace
87
Misha Brukman3d9a6c22004-08-11 00:09:42 +000088/// createPPC32AsmPrinterPass - Returns a pass that prints the PPC
Misha Brukman5dfe3a92004-06-21 16:55:25 +000089/// assembly code for a MachineFunction to the given output stream,
90/// using the given target machine description. This should work
Misha Brukman7103fba2004-08-09 22:27:45 +000091/// regardless of whether the function is in SSA form or not.
Misha Brukman5dfe3a92004-06-21 16:55:25 +000092///
Misha Brukman3d9a6c22004-08-11 00:09:42 +000093FunctionPass *createPPC32AsmPrinter(std::ostream &o,TargetMachine &tm) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +000094 return new Printer(o, tm);
95}
96
97/// isStringCompatible - Can we treat the specified array as a string?
98/// Only if it is an array of ubytes or non-negative sbytes.
99///
100static bool isStringCompatible(const ConstantArray *CVA) {
101 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
102 if (ETy == Type::UByteTy) return true;
103 if (ETy != Type::SByteTy) return false;
104
105 for (unsigned i = 0; i < CVA->getNumOperands(); ++i)
106 if (cast<ConstantSInt>(CVA->getOperand(i))->getValue() < 0)
107 return false;
108
109 return true;
110}
111
112/// toOctal - Convert the low order bits of X into an octal digit.
113///
114static inline char toOctal(int X) {
115 return (X&7)+'0';
116}
117
118/// getAsCString - Return the specified array as a C compatible
119/// string, only if the predicate isStringCompatible is true.
120///
121static void printAsCString(std::ostream &O, const ConstantArray *CVA) {
122 assert(isStringCompatible(CVA) && "Array is not string compatible!");
123
124 O << "\"";
125 for (unsigned i = 0; i < CVA->getNumOperands(); ++i) {
126 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
127
128 if (C == '"') {
129 O << "\\\"";
130 } else if (C == '\\') {
131 O << "\\\\";
132 } else if (isprint(C)) {
133 O << C;
134 } else {
Misha Brukmane2eceb52004-07-23 16:08:20 +0000135 switch (C) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000136 case '\b': O << "\\b"; break;
137 case '\f': O << "\\f"; break;
138 case '\n': O << "\\n"; break;
139 case '\r': O << "\\r"; break;
140 case '\t': O << "\\t"; break;
141 default:
142 O << '\\';
143 O << toOctal(C >> 6);
144 O << toOctal(C >> 3);
145 O << toOctal(C >> 0);
146 break;
147 }
148 }
149 }
150 O << "\"";
151}
152
153// Print out the specified constant, without a storage class. Only the
154// constants valid in constant expressions can occur here.
155void Printer::emitConstantValueOnly(const Constant *CV) {
156 if (CV->isNullValue())
157 O << "0";
158 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
159 assert(CB == ConstantBool::True);
160 O << "1";
161 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
162 O << CI->getValue();
163 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
164 O << CI->getValue();
Chris Lattner67910e12004-07-18 07:29:35 +0000165 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV))
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000166 // This is a constant address for a global variable or function. Use the
167 // name of the variable or function as the address value.
Chris Lattner67910e12004-07-18 07:29:35 +0000168 O << Mang->getValueName(GV);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000169 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
170 const TargetData &TD = TM.getTargetData();
Misha Brukmane2eceb52004-07-23 16:08:20 +0000171 switch (CE->getOpcode()) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000172 case Instruction::GetElementPtr: {
173 // generate a symbolic expression for the byte address
174 const Constant *ptrVal = CE->getOperand(0);
175 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
176 if (unsigned Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
177 O << "(";
178 emitConstantValueOnly(ptrVal);
179 O << ") + " << Offset;
180 } else {
181 emitConstantValueOnly(ptrVal);
182 }
183 break;
184 }
185 case Instruction::Cast: {
186 // Support only non-converting or widening casts for now, that is, ones
187 // that do not involve a change in value. This assertion is really gross,
188 // and may not even be a complete check.
189 Constant *Op = CE->getOperand(0);
190 const Type *OpTy = Op->getType(), *Ty = CE->getType();
191
192 // Remember, kids, pointers on x86 can be losslessly converted back and
193 // forth into 32-bit or wider integers, regardless of signedness. :-P
194 assert(((isa<PointerType>(OpTy)
195 && (Ty == Type::LongTy || Ty == Type::ULongTy
196 || Ty == Type::IntTy || Ty == Type::UIntTy))
197 || (isa<PointerType>(Ty)
198 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
199 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
200 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
201 && OpTy->isLosslesslyConvertibleTo(Ty))))
202 && "FIXME: Don't yet support this kind of constant cast expr");
203 O << "(";
204 emitConstantValueOnly(Op);
205 O << ")";
206 break;
207 }
208 case Instruction::Add:
209 O << "(";
210 emitConstantValueOnly(CE->getOperand(0));
211 O << ") + (";
212 emitConstantValueOnly(CE->getOperand(1));
213 O << ")";
214 break;
215 default:
216 assert(0 && "Unsupported operator!");
217 }
218 } else {
219 assert(0 && "Unknown constant value!");
220 }
221}
222
223// Print a constant value or values, with the appropriate storage class as a
224// prefix.
225void Printer::emitGlobalConstant(const Constant *CV) {
226 const TargetData &TD = TM.getTargetData();
227
Misha Brukmane48178e2004-07-20 15:45:27 +0000228 if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000229 if (isStringCompatible(CVA)) {
Misha Brukman218bec72004-06-29 17:13:26 +0000230 O << "\t.ascii ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000231 printAsCString(O, CVA);
232 O << "\n";
233 } else { // Not a string. Print the values in successive locations
Chris Lattner6173cd92004-08-04 17:29:14 +0000234 for (unsigned i=0, e = CVA->getNumOperands(); i != e; i++)
235 emitGlobalConstant(CVA->getOperand(i));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000236 }
237 return;
238 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
239 // Print the fields in successive locations. Pad to align if needed!
240 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000241 unsigned sizeSoFar = 0;
Chris Lattner6173cd92004-08-04 17:29:14 +0000242 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; i++) {
243 const Constant* field = CVS->getOperand(i);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000244
245 // Check if padding is needed and insert one or more 0s.
246 unsigned fieldSize = TD.getTypeSize(field->getType());
Chris Lattner6173cd92004-08-04 17:29:14 +0000247 unsigned padSize = ((i == e-1? cvsLayout->StructSize
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000248 : cvsLayout->MemberOffsets[i+1])
249 - cvsLayout->MemberOffsets[i]) - fieldSize;
250 sizeSoFar += fieldSize + padSize;
251
252 // Now print the actual field value
253 emitGlobalConstant(field);
254
255 // Insert the field padding unless it's zero bytes...
256 if (padSize)
257 O << "\t.space\t " << padSize << "\n";
258 }
259 assert(sizeSoFar == cvsLayout->StructSize &&
260 "Layout of constant struct may be incorrect!");
261 return;
262 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
263 // FP Constants are printed as integer constants to avoid losing
264 // precision...
265 double Val = CFP->getValue();
Misha Brukmand71bd562004-06-21 17:19:08 +0000266 switch (CFP->getType()->getTypeID()) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000267 default: assert(0 && "Unknown floating point type!");
268 case Type::FloatTyID: {
269 union FU { // Abide by C TBAA rules
270 float FVal;
271 unsigned UVal;
272 } U;
273 U.FVal = Val;
Misha Brukman218bec72004-06-29 17:13:26 +0000274 O << ".long\t" << U.UVal << "\t; float " << Val << "\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000275 return;
276 }
277 case Type::DoubleTyID: {
278 union DU { // Abide by C TBAA rules
279 double FVal;
280 uint64_t UVal;
281 struct {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000282 uint32_t MSWord;
283 uint32_t LSWord;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000284 } T;
285 } U;
286 U.FVal = Val;
287
Misha Brukman218bec72004-06-29 17:13:26 +0000288 O << ".long\t" << U.T.MSWord << "\t; double most significant word "
Misha Brukman46fd00a2004-06-24 23:04:11 +0000289 << Val << "\n";
Misha Brukman29188c62004-07-16 19:01:13 +0000290 O << ".long\t" << U.T.LSWord << "\t; double least significant word "
Misha Brukman46fd00a2004-06-24 23:04:11 +0000291 << Val << "\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000292 return;
293 }
294 }
Misha Brukmanf63bc192004-07-28 19:12:24 +0000295 } else if (CV->getType() == Type::ULongTy || CV->getType() == Type::LongTy) {
Misha Brukman2bf183c2004-06-25 15:42:10 +0000296 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
297 union DU { // Abide by C TBAA rules
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000298 int64_t UVal;
299 struct {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000300 uint32_t MSWord;
301 uint32_t LSWord;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000302 } T;
303 } U;
304 U.UVal = CI->getRawValue();
305
Misha Brukman218bec72004-06-29 17:13:26 +0000306 O << ".long\t" << U.T.MSWord << "\t; Double-word most significant word "
Misha Brukman46fd00a2004-06-24 23:04:11 +0000307 << U.UVal << "\n";
Misha Brukman29188c62004-07-16 19:01:13 +0000308 O << ".long\t" << U.T.LSWord << "\t; Double-word least significant word "
Misha Brukman46fd00a2004-06-24 23:04:11 +0000309 << U.UVal << "\n";
310 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000311 }
312 }
313
314 const Type *type = CV->getType();
315 O << "\t";
Misha Brukmand71bd562004-06-21 17:19:08 +0000316 switch (type->getTypeID()) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000317 case Type::UByteTyID: case Type::SByteTyID:
318 O << ".byte";
319 break;
320 case Type::UShortTyID: case Type::ShortTyID:
321 O << ".short";
322 break;
323 case Type::BoolTyID:
324 case Type::PointerTyID:
325 case Type::UIntTyID: case Type::IntTyID:
326 O << ".long";
327 break;
328 case Type::ULongTyID: case Type::LongTyID:
Misha Brukman46fd00a2004-06-24 23:04:11 +0000329 assert (0 && "Should have already output double-word constant.");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000330 case Type::FloatTyID: case Type::DoubleTyID:
331 assert (0 && "Should have already output floating point constant.");
332 default:
Misha Brukman97a296f2004-07-21 20:11:11 +0000333 if (CV == Constant::getNullValue(type)) { // Zero initializer?
334 O << ".space\t" << TD.getTypeSize(type) << "\n";
335 return;
336 }
337 std::cerr << "Can't handle printing: " << *CV;
338 abort();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000339 break;
340 }
341 O << "\t";
342 emitConstantValueOnly(CV);
343 O << "\n";
344}
345
346/// printConstantPool - Print to the current output stream assembly
347/// representations of the constants in the constant pool MCP. This is
348/// used to print out constants which have been "spilled to memory" by
349/// the code generator.
350///
351void Printer::printConstantPool(MachineConstantPool *MCP) {
352 const std::vector<Constant*> &CP = MCP->getConstants();
353 const TargetData &TD = TM.getTargetData();
354
355 if (CP.empty()) return;
356
357 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
358 O << "\t.const\n";
359 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
360 << "\n";
Misha Brukman218bec72004-06-29 17:13:26 +0000361 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t;"
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000362 << *CP[i] << "\n";
363 emitGlobalConstant(CP[i]);
364 }
365}
366
367/// runOnMachineFunction - This uses the printMachineInstruction()
368/// method to print assembly for each instruction.
369///
370bool Printer::runOnMachineFunction(MachineFunction &MF) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000371 O << "\n\n";
372 // What's my mangled name?
373 CurrentFnName = Mang->getValueName(MF.getFunction());
374
375 // Print out constants referenced by the function
376 printConstantPool(MF.getConstantPool());
377
378 // Print out labels for the function.
379 O << "\t.text\n";
380 O << "\t.globl\t" << CurrentFnName << "\n";
Misha Brukman61297ee2004-06-29 23:40:57 +0000381 O << "\t.align 2\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000382 O << CurrentFnName << ":\n";
383
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000384 // Print out code for the function.
385 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
386 I != E; ++I) {
387 // Print a label for the basic block.
Misha Brukman218bec72004-06-29 17:13:26 +0000388 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t; "
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000389 << I->getBasicBlock()->getName() << "\n";
390 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
Misha Brukman46fd00a2004-06-24 23:04:11 +0000391 II != E; ++II) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000392 // Print the assembly for the instruction.
393 O << "\t";
394 printMachineInstruction(II);
395 }
396 }
Misha Brukmancf8d2442004-07-26 16:28:33 +0000397 ++LabelNumber;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000398
399 // We didn't modify anything.
400 return false;
401}
402
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000403void Printer::printOp(const MachineOperand &MO,
Nate Begemanb73a7112004-08-13 09:32:01 +0000404 bool LoadAddrOp /* = false */) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000405 const MRegisterInfo &RI = *TM.getRegisterInfo();
406 int new_symbol;
407
408 switch (MO.getType()) {
409 case MachineOperand::MO_VirtualRegister:
410 if (Value *V = MO.getVRegValueOrNull()) {
411 O << "<" << V->getName() << ">";
412 return;
413 }
414 // FALLTHROUGH
415 case MachineOperand::MO_MachineRegister:
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000416 case MachineOperand::MO_CCRegister:
Misha Brukman7f484a52004-06-24 23:51:00 +0000417 O << LowercaseString(RI.get(MO.getReg()).Name);
418 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000419
420 case MachineOperand::MO_SignExtendedImmed:
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000421 case MachineOperand::MO_UnextendedImmed:
422 std::cerr << "printOp() does not handle immediate values\n";
423 abort();
Misha Brukman97a296f2004-07-21 20:11:11 +0000424 return;
425
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000426 case MachineOperand::MO_PCRelativeDisp:
427 std::cerr << "Shouldn't use addPCDisp() when building PPC MachineInstrs";
428 abort();
429 return;
430
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000431 case MachineOperand::MO_MachineBasicBlock: {
432 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
433 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
Misha Brukman218bec72004-06-29 17:13:26 +0000434 << "_" << MBBOp->getNumber() << "\t; "
Misha Brukman2bf183c2004-06-25 15:42:10 +0000435 << MBBOp->getBasicBlock()->getName();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000436 return;
437 }
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000438
439 case MachineOperand::MO_ConstantPoolIndex:
440 O << ".CPI" << CurrentFnName << "_" << MO.getConstantPoolIndex();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000441 return;
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000442
443 case MachineOperand::MO_ExternalSymbol:
444 O << MO.getSymbolName();
445 return;
446
Nate Begemanb73a7112004-08-13 09:32:01 +0000447 case MachineOperand::MO_GlobalAddress: {
448 GlobalValue *GV = MO.getGlobal();
449 std::string Name = Mang->getValueName(GV);
Misha Brukmane2eceb52004-07-23 16:08:20 +0000450
Nate Begemanb73a7112004-08-13 09:32:01 +0000451 // Dynamically-resolved functions need a stub for the function. Be
452 // wary however not to output $stub for external functions whose addresses
453 // are taken. Those should be emitted as $non_lazy_ptr below.
454 Function *F = dyn_cast<Function>(GV);
455 if (F && F->isExternal() && !LoadAddrOp &&
456 TM.CalledFunctions.find(F) != TM.CalledFunctions.end()) {
457 FnStubs.insert(Name);
458 O << "L" << Name << "$stub";
459 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000460 }
Nate Begemanb73a7112004-08-13 09:32:01 +0000461
462 // External global variables need a non-lazily-resolved stub
463 if (!GV->hasInternalLinkage() &&
464 TM.AddressTaken.find(GV) != TM.AddressTaken.end()) {
465 GVStubs.insert(Name);
466 O << "L" << Name << "$non_lazy_ptr";
467 return;
468 }
469
470 O << Mang->getValueName(GV);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000471 return;
Nate Begemanb73a7112004-08-13 09:32:01 +0000472 }
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000473
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000474 default:
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000475 O << "<unknown operand type: " << MO.getType() << ">";
Misha Brukman22e12072004-06-25 15:11:34 +0000476 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000477 }
478}
479
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000480void Printer::printImmOp(const MachineOperand &MO, unsigned ArgType) {
481 int Imm = MO.getImmedValue();
Misha Brukman5b570812004-08-10 22:47:03 +0000482 if (ArgType == PPCII::Simm16 || ArgType == PPCII::Disimm16) {
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000483 O << (short)Imm;
Misha Brukman5b570812004-08-10 22:47:03 +0000484 } else if (ArgType == PPCII::Zimm16) {
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000485 O << (unsigned short)Imm;
486 } else {
487 O << Imm;
488 }
489}
490
Misha Brukman5b570812004-08-10 22:47:03 +0000491/// printMachineInstruction -- Print out a single PPC LLVM instruction
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000492/// MI in Darwin syntax to the current output stream.
493///
494void Printer::printMachineInstruction(const MachineInstr *MI) {
495 unsigned Opcode = MI->getOpcode();
496 const TargetInstrInfo &TII = *TM.getInstrInfo();
497 const TargetInstrDescriptor &Desc = TII.get(Opcode);
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000498 unsigned i;
Misha Brukmanc6cc10f2004-06-25 19:24:52 +0000499
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000500 unsigned ArgCount = MI->getNumOperands();
501 unsigned ArgType[] = {
Misha Brukman5b570812004-08-10 22:47:03 +0000502 (Desc.TSFlags >> PPCII::Arg0TypeShift) & PPCII::ArgTypeMask,
503 (Desc.TSFlags >> PPCII::Arg1TypeShift) & PPCII::ArgTypeMask,
504 (Desc.TSFlags >> PPCII::Arg2TypeShift) & PPCII::ArgTypeMask,
505 (Desc.TSFlags >> PPCII::Arg3TypeShift) & PPCII::ArgTypeMask,
506 (Desc.TSFlags >> PPCII::Arg4TypeShift) & PPCII::ArgTypeMask
Misha Brukman22e12072004-06-25 15:11:34 +0000507 };
Misha Brukman5b570812004-08-10 22:47:03 +0000508 assert(((Desc.TSFlags & PPCII::VMX) == 0) &&
Misha Brukman46fd00a2004-06-24 23:04:11 +0000509 "Instruction requires VMX support");
Misha Brukman5b570812004-08-10 22:47:03 +0000510 assert(((Desc.TSFlags & PPCII::PPC64) == 0) &&
Misha Brukman46fd00a2004-06-24 23:04:11 +0000511 "Instruction requires 64 bit support");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000512 ++EmittedInsts;
513
Misha Brukman61114612004-07-20 00:42:19 +0000514 // CALLpcrel and CALLindirect are handled specially here to print only the
515 // appropriate number of args that the assembler expects. This is because
516 // may have many arguments appended to record the uses of registers that are
517 // holding arguments to the called function.
Misha Brukman5b570812004-08-10 22:47:03 +0000518 if (Opcode == PPC::COND_BRANCH) {
Misha Brukmanab967902004-07-27 18:40:39 +0000519 std::cerr << "Error: untranslated conditional branch psuedo instruction!\n";
520 abort();
Misha Brukman5b570812004-08-10 22:47:03 +0000521 } else if (Opcode == PPC::IMPLICIT_DEF) {
Misha Brukman29188c62004-07-16 19:01:13 +0000522 O << "; IMPLICIT DEF ";
523 printOp(MI->getOperand(0));
524 O << "\n";
525 return;
Misha Brukman5b570812004-08-10 22:47:03 +0000526 } else if (Opcode == PPC::CALLpcrel) {
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000527 O << TII.getName(Opcode) << " ";
Misha Brukman61114612004-07-20 00:42:19 +0000528 printOp(MI->getOperand(0));
529 O << "\n";
530 return;
Misha Brukman5b570812004-08-10 22:47:03 +0000531 } else if (Opcode == PPC::CALLindirect) {
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000532 O << TII.getName(Opcode) << " ";
533 printImmOp(MI->getOperand(0), ArgType[0]);
Misha Brukman61114612004-07-20 00:42:19 +0000534 O << ", ";
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000535 printImmOp(MI->getOperand(1), ArgType[0]);
Misha Brukman61114612004-07-20 00:42:19 +0000536 O << "\n";
537 return;
Misha Brukman5b570812004-08-10 22:47:03 +0000538 } else if (Opcode == PPC::MovePCtoLR) {
Misha Brukman61114612004-07-20 00:42:19 +0000539 // FIXME: should probably be converted to cout.width and cout.fill
Misha Brukmancf8d2442004-07-26 16:28:33 +0000540 O << "bl \"L0000" << LabelNumber << "$pb\"\n";
541 O << "\"L0000" << LabelNumber << "$pb\":\n";
Misha Brukman218bec72004-06-29 17:13:26 +0000542 O << "\tmflr ";
543 printOp(MI->getOperand(0));
Misha Brukman218bec72004-06-29 17:13:26 +0000544 O << "\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000545 return;
546 }
547
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000548 O << TII.getName(Opcode) << " ";
Misha Brukman5b570812004-08-10 22:47:03 +0000549 if (Opcode == PPC::LOADLoDirect || Opcode == PPC::LOADLoIndirect) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000550 printOp(MI->getOperand(0));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000551 O << ", lo16(";
Nate Begemanb73a7112004-08-13 09:32:01 +0000552 printOp(MI->getOperand(2), true /* LoadAddrOp */);
Misha Brukmancf8d2442004-07-26 16:28:33 +0000553 O << "-\"L0000" << LabelNumber << "$pb\")";
Misha Brukman218bec72004-06-29 17:13:26 +0000554 O << "(";
Misha Brukman5b570812004-08-10 22:47:03 +0000555 if (MI->getOperand(1).getReg() == PPC::R0)
Misha Brukman218bec72004-06-29 17:13:26 +0000556 O << "0";
557 else
558 printOp(MI->getOperand(1));
559 O << ")\n";
Misha Brukman5b570812004-08-10 22:47:03 +0000560 } else if (Opcode == PPC::LOADHiAddr) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000561 printOp(MI->getOperand(0));
562 O << ", ";
Misha Brukman5b570812004-08-10 22:47:03 +0000563 if (MI->getOperand(1).getReg() == PPC::R0)
Misha Brukman218bec72004-06-29 17:13:26 +0000564 O << "0";
565 else
566 printOp(MI->getOperand(1));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000567 O << ", ha16(" ;
Nate Begemanb73a7112004-08-13 09:32:01 +0000568 printOp(MI->getOperand(2), true /* LoadAddrOp */);
Misha Brukmancf8d2442004-07-26 16:28:33 +0000569 O << "-\"L0000" << LabelNumber << "$pb\")\n";
Misha Brukman5b570812004-08-10 22:47:03 +0000570 } else if (ArgCount == 3 && ArgType[1] == PPCII::Disimm16) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000571 printOp(MI->getOperand(0));
572 O << ", ";
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000573 printImmOp(MI->getOperand(1), ArgType[1]);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000574 O << "(";
Misha Brukmanb9e8f972004-06-30 21:54:12 +0000575 if (MI->getOperand(2).hasAllocatedReg() &&
Misha Brukman5b570812004-08-10 22:47:03 +0000576 MI->getOperand(2).getReg() == PPC::R0)
Misha Brukman46fd00a2004-06-24 23:04:11 +0000577 O << "0";
578 else
579 printOp(MI->getOperand(2));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000580 O << ")\n";
581 } else {
Misha Brukman7f484a52004-06-24 23:51:00 +0000582 for (i = 0; i < ArgCount; ++i) {
Misha Brukmanab967902004-07-27 18:40:39 +0000583 // addi and friends
Misha Brukman5b570812004-08-10 22:47:03 +0000584 if (i == 1 && ArgCount == 3 && ArgType[2] == PPCII::Simm16 &&
Misha Brukman4363bdb2004-07-01 21:09:12 +0000585 MI->getOperand(1).hasAllocatedReg() &&
Misha Brukman5b570812004-08-10 22:47:03 +0000586 MI->getOperand(1).getReg() == PPC::R0) {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000587 O << "0";
Misha Brukmanab967902004-07-27 18:40:39 +0000588 // for long branch support, bc $+8
589 } else if (i == 1 && ArgCount == 2 && MI->getOperand(1).isImmediate() &&
590 TII.isBranch(MI->getOpcode())) {
591 O << "$+8";
592 assert(8 == MI->getOperand(i).getImmedValue()
593 && "branch off PC not to pc+8?");
594 //printOp(MI->getOperand(i));
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000595 } else if (MI->getOperand(i).isImmediate()) {
596 printImmOp(MI->getOperand(i), ArgType[i]);
Misha Brukman218bec72004-06-29 17:13:26 +0000597 } else {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000598 printOp(MI->getOperand(i));
599 }
Misha Brukman7f484a52004-06-24 23:51:00 +0000600 if (ArgCount - 1 == i)
Misha Brukman46fd00a2004-06-24 23:04:11 +0000601 O << "\n";
602 else
603 O << ", ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000604 }
605 }
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000606}
607
608bool Printer::doInitialization(Module &M) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000609 Mang = new Mangler(M, true);
610 return false; // success
611}
612
613// SwitchSection - Switch to the specified section of the executable if we are
614// not already in it!
615//
616static void SwitchSection(std::ostream &OS, std::string &CurSection,
617 const char *NewSection) {
618 if (CurSection != NewSection) {
619 CurSection = NewSection;
620 if (!CurSection.empty())
621 OS << "\t" << NewSection << "\n";
622 }
623}
624
625bool Printer::doFinalization(Module &M) {
626 const TargetData &TD = TM.getTargetData();
627 std::string CurSection;
628
629 // Print out module-level global variables here.
630 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
631 if (I->hasInitializer()) { // External global require no code
632 O << "\n\n";
633 std::string name = Mang->getValueName(I);
634 Constant *C = I->getInitializer();
635 unsigned Size = TD.getTypeSize(C->getType());
636 unsigned Align = TD.getTypeAlignment(C->getType());
637
Misha Brukman97a296f2004-07-21 20:11:11 +0000638 if (C->isNullValue() && /* FIXME: Verify correct */
639 (I->hasInternalLinkage() || I->hasWeakLinkage())) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000640 SwitchSection(O, CurSection, ".data");
641 if (I->hasInternalLinkage())
Misha Brukmane2eceb52004-07-23 16:08:20 +0000642 O << ".lcomm " << name << "," << TD.getTypeSize(C->getType())
Misha Brukman218bec72004-06-29 17:13:26 +0000643 << "," << (unsigned)TD.getTypeAlignment(C->getType());
644 else
Misha Brukmane2eceb52004-07-23 16:08:20 +0000645 O << ".comm " << name << "," << TD.getTypeSize(C->getType());
Misha Brukman218bec72004-06-29 17:13:26 +0000646 O << "\t\t; ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000647 WriteAsOperand(O, I, true, true, &M);
648 O << "\n";
649 } else {
650 switch (I->getLinkage()) {
651 case GlobalValue::LinkOnceLinkage:
Misha Brukman97a296f2004-07-21 20:11:11 +0000652 O << ".section __TEXT,__textcoal_nt,coalesced,no_toc\n"
653 << ".weak_definition " << name << '\n'
654 << ".private_extern " << name << '\n'
655 << ".section __DATA,__datacoal_nt,coalesced,no_toc\n";
656 LinkOnceStubs.insert(name);
657 break;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000658 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
659 // Nonnull linkonce -> weak
660 O << "\t.weak " << name << "\n";
661 SwitchSection(O, CurSection, "");
662 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
663 break;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000664 case GlobalValue::AppendingLinkage:
665 // FIXME: appending linkage variables should go into a section of
666 // their name or something. For now, just emit them as external.
667 case GlobalValue::ExternalLinkage:
668 // If external or appending, declare as a global symbol
669 O << "\t.globl " << name << "\n";
670 // FALL THROUGH
671 case GlobalValue::InternalLinkage:
Misha Brukman61297ee2004-06-29 23:40:57 +0000672 SwitchSection(O, CurSection, ".data");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000673 break;
674 }
675
676 O << "\t.align " << Align << "\n";
Misha Brukman218bec72004-06-29 17:13:26 +0000677 O << name << ":\t\t\t\t; ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000678 WriteAsOperand(O, I, true, true, &M);
679 O << " = ";
680 WriteAsOperand(O, C, false, false, &M);
681 O << "\n";
682 emitGlobalConstant(C);
683 }
684 }
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000685
Misha Brukman97a296f2004-07-21 20:11:11 +0000686 // Output stubs for link-once variables
687 if (LinkOnceStubs.begin() != LinkOnceStubs.end())
688 O << ".data\n.align 2\n";
689 for (std::set<std::string>::iterator i = LinkOnceStubs.begin(),
Misha Brukmane2eceb52004-07-23 16:08:20 +0000690 e = LinkOnceStubs.end(); i != e; ++i) {
Misha Brukman97a296f2004-07-21 20:11:11 +0000691 O << *i << "$non_lazy_ptr:\n"
692 << "\t.long\t" << *i << '\n';
693 }
694
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000695 // Output stubs for dynamically-linked functions
696 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
697 i != e; ++i)
Misha Brukman46fd00a2004-06-24 23:04:11 +0000698 {
Misha Brukmane2eceb52004-07-23 16:08:20 +0000699 O << ".data\n";
700 O << ".section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32\n";
701 O << "\t.align 2\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000702 O << "L" << *i << "$stub:\n";
703 O << "\t.indirect_symbol " << *i << "\n";
704 O << "\tmflr r0\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000705 O << "\tbcl 20,31,L0$" << *i << "\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000706 O << "L0$" << *i << ":\n";
707 O << "\tmflr r11\n";
708 O << "\taddis r11,r11,ha16(L" << *i << "$lazy_ptr-L0$" << *i << ")\n";
709 O << "\tmtlr r0\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000710 O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr-L0$" << *i << ")(r11)\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000711 O << "\tmtctr r12\n";
712 O << "\tbctr\n";
713 O << ".data\n";
714 O << ".lazy_symbol_pointer\n";
715 O << "L" << *i << "$lazy_ptr:\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000716 O << "\t.indirect_symbol " << *i << "\n";
717 O << "\t.long dyld_stub_binding_helper\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000718 }
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000719
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000720 O << "\n";
721
722 // Output stubs for external global variables
723 if (GVStubs.begin() != GVStubs.end())
Misha Brukmane2eceb52004-07-23 16:08:20 +0000724 O << ".data\n.non_lazy_symbol_pointer\n";
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000725 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
726 i != e; ++i) {
727 O << "L" << *i << "$non_lazy_ptr:\n";
728 O << "\t.indirect_symbol " << *i << "\n";
729 O << "\t.long\t0\n";
730 }
731
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000732 delete Mang;
733 return false; // success
734}
735
736} // End llvm namespace