blob: e8b3ca2161aabd6eeb24665b7cf3ac14d5f50707 [file] [log] [blame]
Misha Brukman05fcd0c2004-07-08 17:58:04 +00001//===-- Printer.cpp - Convert LLVM code 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
12// the output mechanism used by `llc' and `lli -print-machineinstrs'.
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"
22#include "llvm/Constants.h"
23#include "llvm/DerivedTypes.h"
24#include "llvm/Module.h"
25#include "llvm/Assembly/Writer.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000026#include "llvm/CodeGen/MachineConstantPool.h"
Misha Brukman05794492004-06-24 17:31:42 +000027#include "llvm/CodeGen/MachineFunctionPass.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000028#include "llvm/CodeGen/MachineInstr.h"
29#include "llvm/Target/TargetMachine.h"
30#include "llvm/Support/Mangler.h"
Misha Brukman05794492004-06-24 17:31:42 +000031#include "Support/CommandLine.h"
32#include "Support/Debug.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000033#include "Support/Statistic.h"
34#include "Support/StringExtras.h"
Misha Brukman05794492004-06-24 17:31:42 +000035#include <set>
Misha Brukman5dfe3a92004-06-21 16:55:25 +000036
37namespace llvm {
38
39namespace {
40 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
41
42 struct Printer : public MachineFunctionPass {
43 /// Output stream on which we're printing assembly code.
44 ///
45 std::ostream &O;
46
47 /// Target machine description which we query for reg. names, data
48 /// layout, etc.
49 ///
50 TargetMachine &TM;
51
52 /// Name-mangler for global names.
53 ///
54 Mangler *Mang;
Misha Brukmanda2b13f2004-07-16 20:29:04 +000055 std::set<std::string> FnStubs, GVStubs;
Misha Brukman5dfe3a92004-06-21 16:55:25 +000056 std::set<std::string> Strings;
57
Misha Brukman218bec72004-06-29 17:13:26 +000058 Printer(std::ostream &o, TargetMachine &tm) : O(o), TM(tm), labelNumber(0)
59 { }
Misha Brukman5dfe3a92004-06-21 16:55:25 +000060
Misha Brukman5dfe3a92004-06-21 16:55:25 +000061 /// Cache of mangled name for current function. This is
62 /// recalculated at the beginning of each call to
63 /// runOnMachineFunction().
64 ///
65 std::string CurrentFnName;
66
Misha Brukman218bec72004-06-29 17:13:26 +000067 /// Unique incrementer for label values for referencing
68 /// Global values.
69 ///
70 unsigned int labelNumber;
71
Misha Brukman5dfe3a92004-06-21 16:55:25 +000072 virtual const char *getPassName() const {
73 return "PowerPC Assembly Printer";
74 }
75
76 void printMachineInstruction(const MachineInstr *MI);
Misha Brukmanbb4a9082004-06-28 15:53:27 +000077 void printOp(const MachineOperand &MO, bool elideOffsetKeyword = false);
Misha Brukman5dfe3a92004-06-21 16:55:25 +000078 void printConstantPool(MachineConstantPool *MCP);
79 bool runOnMachineFunction(MachineFunction &F);
80 bool doInitialization(Module &M);
81 bool doFinalization(Module &M);
82 void emitGlobalConstant(const Constant* CV);
83 void emitConstantValueOnly(const Constant *CV);
84 };
85} // end of anonymous namespace
86
Misha Brukman218bec72004-06-29 17:13:26 +000087/// createPPCCodePrinterPass - Returns a pass that prints the PPC
Misha Brukman5dfe3a92004-06-21 16:55:25 +000088/// assembly code for a MachineFunction to the given output stream,
89/// using the given target machine description. This should work
90/// regardless of whether the function is in SSA form.
91///
Misha Brukmanbb4a9082004-06-28 15:53:27 +000092FunctionPass *createPPCCodePrinterPass(std::ostream &o,TargetMachine &tm) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +000093 return new Printer(o, tm);
94}
95
96/// isStringCompatible - Can we treat the specified array as a string?
97/// Only if it is an array of ubytes or non-negative sbytes.
98///
99static bool isStringCompatible(const ConstantArray *CVA) {
100 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
101 if (ETy == Type::UByteTy) return true;
102 if (ETy != Type::SByteTy) return false;
103
104 for (unsigned i = 0; i < CVA->getNumOperands(); ++i)
105 if (cast<ConstantSInt>(CVA->getOperand(i))->getValue() < 0)
106 return false;
107
108 return true;
109}
110
111/// toOctal - Convert the low order bits of X into an octal digit.
112///
113static inline char toOctal(int X) {
114 return (X&7)+'0';
115}
116
117/// getAsCString - Return the specified array as a C compatible
118/// string, only if the predicate isStringCompatible is true.
119///
120static void printAsCString(std::ostream &O, const ConstantArray *CVA) {
121 assert(isStringCompatible(CVA) && "Array is not string compatible!");
122
123 O << "\"";
124 for (unsigned i = 0; i < CVA->getNumOperands(); ++i) {
125 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
126
127 if (C == '"') {
128 O << "\\\"";
129 } else if (C == '\\') {
130 O << "\\\\";
131 } else if (isprint(C)) {
132 O << C;
133 } else {
134 switch(C) {
135 case '\b': O << "\\b"; break;
136 case '\f': O << "\\f"; break;
137 case '\n': O << "\\n"; break;
138 case '\r': O << "\\r"; break;
139 case '\t': O << "\\t"; break;
140 default:
141 O << '\\';
142 O << toOctal(C >> 6);
143 O << toOctal(C >> 3);
144 O << toOctal(C >> 0);
145 break;
146 }
147 }
148 }
149 O << "\"";
150}
151
152// Print out the specified constant, without a storage class. Only the
153// constants valid in constant expressions can occur here.
154void Printer::emitConstantValueOnly(const Constant *CV) {
155 if (CV->isNullValue())
156 O << "0";
157 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
158 assert(CB == ConstantBool::True);
159 O << "1";
160 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
161 O << CI->getValue();
162 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
163 O << CI->getValue();
164 else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CV))
165 // This is a constant address for a global variable or function. Use the
166 // name of the variable or function as the address value.
167 O << Mang->getValueName(CPR->getValue());
168 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
169 const TargetData &TD = TM.getTargetData();
170 switch(CE->getOpcode()) {
171 case Instruction::GetElementPtr: {
172 // generate a symbolic expression for the byte address
173 const Constant *ptrVal = CE->getOperand(0);
174 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
175 if (unsigned Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
176 O << "(";
177 emitConstantValueOnly(ptrVal);
178 O << ") + " << Offset;
179 } else {
180 emitConstantValueOnly(ptrVal);
181 }
182 break;
183 }
184 case Instruction::Cast: {
185 // Support only non-converting or widening casts for now, that is, ones
186 // that do not involve a change in value. This assertion is really gross,
187 // and may not even be a complete check.
188 Constant *Op = CE->getOperand(0);
189 const Type *OpTy = Op->getType(), *Ty = CE->getType();
190
191 // Remember, kids, pointers on x86 can be losslessly converted back and
192 // forth into 32-bit or wider integers, regardless of signedness. :-P
193 assert(((isa<PointerType>(OpTy)
194 && (Ty == Type::LongTy || Ty == Type::ULongTy
195 || Ty == Type::IntTy || Ty == Type::UIntTy))
196 || (isa<PointerType>(Ty)
197 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
198 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
199 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
200 && OpTy->isLosslesslyConvertibleTo(Ty))))
201 && "FIXME: Don't yet support this kind of constant cast expr");
202 O << "(";
203 emitConstantValueOnly(Op);
204 O << ")";
205 break;
206 }
207 case Instruction::Add:
208 O << "(";
209 emitConstantValueOnly(CE->getOperand(0));
210 O << ") + (";
211 emitConstantValueOnly(CE->getOperand(1));
212 O << ")";
213 break;
214 default:
215 assert(0 && "Unsupported operator!");
216 }
217 } else {
218 assert(0 && "Unknown constant value!");
219 }
220}
221
222// Print a constant value or values, with the appropriate storage class as a
223// prefix.
224void Printer::emitGlobalConstant(const Constant *CV) {
225 const TargetData &TD = TM.getTargetData();
226
227 if (CV->isNullValue()) {
228 O << "\t.space\t " << TD.getTypeSize(CV->getType()) << "\n";
229 return;
230 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
231 if (isStringCompatible(CVA)) {
Misha Brukman218bec72004-06-29 17:13:26 +0000232 O << "\t.ascii ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000233 printAsCString(O, CVA);
234 O << "\n";
235 } else { // Not a string. Print the values in successive locations
236 const std::vector<Use> &constValues = CVA->getValues();
237 for (unsigned i=0; i < constValues.size(); i++)
238 emitGlobalConstant(cast<Constant>(constValues[i].get()));
239 }
240 return;
241 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
242 // Print the fields in successive locations. Pad to align if needed!
243 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
244 const std::vector<Use>& constValues = CVS->getValues();
245 unsigned sizeSoFar = 0;
246 for (unsigned i=0, N = constValues.size(); i < N; i++) {
247 const Constant* field = cast<Constant>(constValues[i].get());
248
249 // Check if padding is needed and insert one or more 0s.
250 unsigned fieldSize = TD.getTypeSize(field->getType());
251 unsigned padSize = ((i == N-1? cvsLayout->StructSize
252 : cvsLayout->MemberOffsets[i+1])
253 - cvsLayout->MemberOffsets[i]) - fieldSize;
254 sizeSoFar += fieldSize + padSize;
255
256 // Now print the actual field value
257 emitGlobalConstant(field);
258
259 // Insert the field padding unless it's zero bytes...
260 if (padSize)
261 O << "\t.space\t " << padSize << "\n";
262 }
263 assert(sizeSoFar == cvsLayout->StructSize &&
264 "Layout of constant struct may be incorrect!");
265 return;
266 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
267 // FP Constants are printed as integer constants to avoid losing
268 // precision...
269 double Val = CFP->getValue();
Misha Brukmand71bd562004-06-21 17:19:08 +0000270 switch (CFP->getType()->getTypeID()) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000271 default: assert(0 && "Unknown floating point type!");
272 case Type::FloatTyID: {
273 union FU { // Abide by C TBAA rules
274 float FVal;
275 unsigned UVal;
276 } U;
277 U.FVal = Val;
Misha Brukman218bec72004-06-29 17:13:26 +0000278 O << ".long\t" << U.UVal << "\t; float " << Val << "\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000279 return;
280 }
281 case Type::DoubleTyID: {
282 union DU { // Abide by C TBAA rules
283 double FVal;
284 uint64_t UVal;
285 struct {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000286 uint32_t MSWord;
287 uint32_t LSWord;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000288 } T;
289 } U;
290 U.FVal = Val;
291
Misha Brukman218bec72004-06-29 17:13:26 +0000292 O << ".long\t" << U.T.MSWord << "\t; double most significant word "
Misha Brukman46fd00a2004-06-24 23:04:11 +0000293 << Val << "\n";
Misha Brukman29188c62004-07-16 19:01:13 +0000294 O << ".long\t" << U.T.LSWord << "\t; double least significant word "
Misha Brukman46fd00a2004-06-24 23:04:11 +0000295 << Val << "\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000296 return;
297 }
298 }
299 } else if (CV->getType()->getPrimitiveSize() == 64) {
Misha Brukman2bf183c2004-06-25 15:42:10 +0000300 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
301 union DU { // Abide by C TBAA rules
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000302 int64_t UVal;
303 struct {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000304 uint32_t MSWord;
305 uint32_t LSWord;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000306 } T;
307 } U;
308 U.UVal = CI->getRawValue();
309
Misha Brukman218bec72004-06-29 17:13:26 +0000310 O << ".long\t" << U.T.MSWord << "\t; Double-word most significant word "
Misha Brukman46fd00a2004-06-24 23:04:11 +0000311 << U.UVal << "\n";
Misha Brukman29188c62004-07-16 19:01:13 +0000312 O << ".long\t" << U.T.LSWord << "\t; Double-word least significant word "
Misha Brukman46fd00a2004-06-24 23:04:11 +0000313 << U.UVal << "\n";
314 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000315 }
316 }
317
318 const Type *type = CV->getType();
319 O << "\t";
Misha Brukmand71bd562004-06-21 17:19:08 +0000320 switch (type->getTypeID()) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000321 case Type::UByteTyID: case Type::SByteTyID:
322 O << ".byte";
323 break;
324 case Type::UShortTyID: case Type::ShortTyID:
325 O << ".short";
326 break;
327 case Type::BoolTyID:
328 case Type::PointerTyID:
329 case Type::UIntTyID: case Type::IntTyID:
330 O << ".long";
331 break;
332 case Type::ULongTyID: case Type::LongTyID:
Misha Brukman46fd00a2004-06-24 23:04:11 +0000333 assert (0 && "Should have already output double-word constant.");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000334 case Type::FloatTyID: case Type::DoubleTyID:
335 assert (0 && "Should have already output floating point constant.");
336 default:
337 assert (0 && "Can't handle printing this type of thing");
338 break;
339 }
340 O << "\t";
341 emitConstantValueOnly(CV);
342 O << "\n";
343}
344
345/// printConstantPool - Print to the current output stream assembly
346/// representations of the constants in the constant pool MCP. This is
347/// used to print out constants which have been "spilled to memory" by
348/// the code generator.
349///
350void Printer::printConstantPool(MachineConstantPool *MCP) {
351 const std::vector<Constant*> &CP = MCP->getConstants();
352 const TargetData &TD = TM.getTargetData();
353
354 if (CP.empty()) return;
355
356 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
357 O << "\t.const\n";
358 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
359 << "\n";
Misha Brukman218bec72004-06-29 17:13:26 +0000360 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t;"
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000361 << *CP[i] << "\n";
362 emitGlobalConstant(CP[i]);
363 }
364}
365
366/// runOnMachineFunction - This uses the printMachineInstruction()
367/// method to print assembly for each instruction.
368///
369bool Printer::runOnMachineFunction(MachineFunction &MF) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000370 O << "\n\n";
371 // What's my mangled name?
372 CurrentFnName = Mang->getValueName(MF.getFunction());
373
374 // Print out constants referenced by the function
375 printConstantPool(MF.getConstantPool());
376
377 // Print out labels for the function.
378 O << "\t.text\n";
379 O << "\t.globl\t" << CurrentFnName << "\n";
Misha Brukman61297ee2004-06-29 23:40:57 +0000380 O << "\t.align 2\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000381 O << CurrentFnName << ":\n";
382
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000383 // Print out code for the function.
384 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
385 I != E; ++I) {
386 // Print a label for the basic block.
Misha Brukman218bec72004-06-29 17:13:26 +0000387 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t; "
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000388 << I->getBasicBlock()->getName() << "\n";
389 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
Misha Brukman46fd00a2004-06-24 23:04:11 +0000390 II != E; ++II) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000391 // Print the assembly for the instruction.
392 O << "\t";
393 printMachineInstruction(II);
394 }
395 }
396
397 // We didn't modify anything.
398 return false;
399}
400
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000401void Printer::printOp(const MachineOperand &MO,
Misha Brukman46fd00a2004-06-24 23:04:11 +0000402 bool elideOffsetKeyword /* = false */) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000403 const MRegisterInfo &RI = *TM.getRegisterInfo();
404 int new_symbol;
405
406 switch (MO.getType()) {
407 case MachineOperand::MO_VirtualRegister:
408 if (Value *V = MO.getVRegValueOrNull()) {
409 O << "<" << V->getName() << ">";
410 return;
411 }
412 // FALLTHROUGH
413 case MachineOperand::MO_MachineRegister:
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000414 case MachineOperand::MO_CCRegister:
Misha Brukman7f484a52004-06-24 23:51:00 +0000415 O << LowercaseString(RI.get(MO.getReg()).Name);
416 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000417
418 case MachineOperand::MO_SignExtendedImmed:
419 case MachineOperand::MO_UnextendedImmed:
420 O << (int)MO.getImmedValue();
421 return;
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000422
423 case MachineOperand::MO_PCRelativeDisp:
424 std::cerr << "Shouldn't use addPCDisp() when building PPC MachineInstrs";
425 abort();
426 return;
427
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000428 case MachineOperand::MO_MachineBasicBlock: {
429 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
430 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
Misha Brukman218bec72004-06-29 17:13:26 +0000431 << "_" << MBBOp->getNumber() << "\t; "
Misha Brukman2bf183c2004-06-25 15:42:10 +0000432 << MBBOp->getBasicBlock()->getName();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000433 return;
434 }
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000435
436 case MachineOperand::MO_ConstantPoolIndex:
437 O << ".CPI" << CurrentFnName << "_" << MO.getConstantPoolIndex();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000438 return;
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000439
440 case MachineOperand::MO_ExternalSymbol:
441 O << MO.getSymbolName();
442 return;
443
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000444 case MachineOperand::MO_GlobalAddress:
445 if (!elideOffsetKeyword) {
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000446 GlobalValue *GV = MO.getGlobal();
447 std::string Name = Mang->getValueName(GV);
Misha Brukmana6e58b32004-06-28 18:03:37 +0000448 // Dynamically-resolved functions need a stub for the function
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000449 Function *F = dyn_cast<Function>(GV);
Misha Brukmana6e58b32004-06-28 18:03:37 +0000450 if (F && F->isExternal()) {
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000451 FnStubs.insert(Name);
452 O << "L" << Name << "$stub";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000453 } else {
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000454 GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
455 // External global variables need a non-lazily-resolved stub
456 if (GVar && GVar->isExternal()) {
457 GVStubs.insert(Name);
458 O << "L" << Name << "$non_lazy_ptr";
459 } else
460 O << Mang->getValueName(GV);
Misha Brukman46fd00a2004-06-24 23:04:11 +0000461 }
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000462 }
463 return;
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000464
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000465 default:
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000466 O << "<unknown operand type: " << MO.getType() << ">";
Misha Brukman22e12072004-06-25 15:11:34 +0000467 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000468 }
469}
470
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000471/// printMachineInstruction -- Print out a single PPC32 LLVM instruction
472/// MI in Darwin syntax to the current output stream.
473///
474void Printer::printMachineInstruction(const MachineInstr *MI) {
475 unsigned Opcode = MI->getOpcode();
476 const TargetInstrInfo &TII = *TM.getInstrInfo();
477 const TargetInstrDescriptor &Desc = TII.get(Opcode);
478 unsigned int i;
Misha Brukmanc6cc10f2004-06-25 19:24:52 +0000479
Misha Brukmanb9e8f972004-06-30 21:54:12 +0000480 unsigned int ArgCount = MI->getNumOperands();
481 //Desc.TSFlags & PPC32II::ArgCountMask;
Misha Brukman22e12072004-06-25 15:11:34 +0000482 unsigned int ArgType[] = {
483 (Desc.TSFlags >> PPC32II::Arg0TypeShift) & PPC32II::ArgTypeMask,
484 (Desc.TSFlags >> PPC32II::Arg1TypeShift) & PPC32II::ArgTypeMask,
485 (Desc.TSFlags >> PPC32II::Arg2TypeShift) & PPC32II::ArgTypeMask,
486 (Desc.TSFlags >> PPC32II::Arg3TypeShift) & PPC32II::ArgTypeMask,
487 (Desc.TSFlags >> PPC32II::Arg4TypeShift) & PPC32II::ArgTypeMask
488 };
Misha Brukman7f484a52004-06-24 23:51:00 +0000489 assert(((Desc.TSFlags & PPC32II::VMX) == 0) &&
Misha Brukman46fd00a2004-06-24 23:04:11 +0000490 "Instruction requires VMX support");
Misha Brukman7f484a52004-06-24 23:51:00 +0000491 assert(((Desc.TSFlags & PPC32II::PPC64) == 0) &&
Misha Brukman46fd00a2004-06-24 23:04:11 +0000492 "Instruction requires 64 bit support");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000493 ++EmittedInsts;
494
Misha Brukman29188c62004-07-16 19:01:13 +0000495 if (Opcode == PPC32::IMPLICIT_DEF) {
496 O << "; IMPLICIT DEF ";
497 printOp(MI->getOperand(0));
498 O << "\n";
499 return;
500 }
Misha Brukman218bec72004-06-29 17:13:26 +0000501 // FIXME: should probably be converted to cout.width and cout.fill
Misha Brukman46fd00a2004-06-24 23:04:11 +0000502 if (Opcode == PPC32::MovePCtoLR) {
Misha Brukman4cf51122004-07-06 22:40:34 +0000503 O << "bl \"L0000" << labelNumber << "$pb\"\n";
Misha Brukman218bec72004-06-29 17:13:26 +0000504 O << "\"L0000" << labelNumber << "$pb\":\n";
505 O << "\tmflr ";
506 printOp(MI->getOperand(0));
Misha Brukman218bec72004-06-29 17:13:26 +0000507 O << "\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000508 return;
509 }
510
511 O << TII.getName(MI->getOpcode()) << " ";
Misha Brukman05794492004-06-24 17:31:42 +0000512 DEBUG(std::cerr << TII.getName(MI->getOpcode()) << " expects "
513 << ArgCount << " args\n");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000514
Misha Brukman46fd00a2004-06-24 23:04:11 +0000515 if (Opcode == PPC32::LOADLoAddr) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000516 printOp(MI->getOperand(0));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000517 O << ", lo16(";
518 printOp(MI->getOperand(2));
Misha Brukman218bec72004-06-29 17:13:26 +0000519 O << "-\"L0000" << labelNumber << "$pb\")";
520 labelNumber++;
521 O << "(";
522 if (MI->getOperand(1).getReg() == PPC32::R0)
523 O << "0";
524 else
525 printOp(MI->getOperand(1));
526 O << ")\n";
Misha Brukmanc6cc10f2004-06-25 19:24:52 +0000527 } else if (Opcode == PPC32::LOADHiAddr) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000528 printOp(MI->getOperand(0));
529 O << ", ";
Misha Brukman218bec72004-06-29 17:13:26 +0000530 if (MI->getOperand(1).getReg() == PPC32::R0)
531 O << "0";
532 else
533 printOp(MI->getOperand(1));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000534 O << ", ha16(" ;
535 printOp(MI->getOperand(2));
Misha Brukman218bec72004-06-29 17:13:26 +0000536 O << "-\"L0000" << labelNumber << "$pb\")\n";
Misha Brukmanc6cc10f2004-06-25 19:24:52 +0000537 } else if (ArgCount == 3 && ArgType[1] == PPC32II::Disimm16) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000538 printOp(MI->getOperand(0));
539 O << ", ";
540 printOp(MI->getOperand(1));
541 O << "(";
Misha Brukmanb9e8f972004-06-30 21:54:12 +0000542 if (MI->getOperand(2).hasAllocatedReg() &&
543 MI->getOperand(2).getReg() == PPC32::R0)
Misha Brukman46fd00a2004-06-24 23:04:11 +0000544 O << "0";
545 else
546 printOp(MI->getOperand(2));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000547 O << ")\n";
548 } else {
Misha Brukman7f484a52004-06-24 23:51:00 +0000549 for (i = 0; i < ArgCount; ++i) {
Misha Brukman218bec72004-06-29 17:13:26 +0000550 if (i == 1 && ArgCount == 3 && ArgType[2] == PPC32II::Simm16 &&
Misha Brukman4363bdb2004-07-01 21:09:12 +0000551 MI->getOperand(1).hasAllocatedReg() &&
Misha Brukman218bec72004-06-29 17:13:26 +0000552 MI->getOperand(1).getReg() == PPC32::R0) {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000553 O << "0";
Misha Brukman218bec72004-06-29 17:13:26 +0000554 } else {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000555 printOp(MI->getOperand(i));
556 }
Misha Brukman7f484a52004-06-24 23:51:00 +0000557 if (ArgCount - 1 == i)
Misha Brukman46fd00a2004-06-24 23:04:11 +0000558 O << "\n";
559 else
560 O << ", ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000561 }
562 }
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000563}
564
565bool Printer::doInitialization(Module &M) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000566 Mang = new Mangler(M, true);
567 return false; // success
568}
569
570// SwitchSection - Switch to the specified section of the executable if we are
571// not already in it!
572//
573static void SwitchSection(std::ostream &OS, std::string &CurSection,
574 const char *NewSection) {
575 if (CurSection != NewSection) {
576 CurSection = NewSection;
577 if (!CurSection.empty())
578 OS << "\t" << NewSection << "\n";
579 }
580}
581
582bool Printer::doFinalization(Module &M) {
583 const TargetData &TD = TM.getTargetData();
584 std::string CurSection;
585
586 // Print out module-level global variables here.
587 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
588 if (I->hasInitializer()) { // External global require no code
589 O << "\n\n";
590 std::string name = Mang->getValueName(I);
591 Constant *C = I->getInitializer();
592 unsigned Size = TD.getTypeSize(C->getType());
593 unsigned Align = TD.getTypeAlignment(C->getType());
594
595 if (C->isNullValue() &&
596 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
597 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
598 SwitchSection(O, CurSection, ".data");
599 if (I->hasInternalLinkage())
Misha Brukman218bec72004-06-29 17:13:26 +0000600 O << "\t.lcomm " << name << "," << TD.getTypeSize(C->getType())
601 << "," << (unsigned)TD.getTypeAlignment(C->getType());
602 else
603 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType());
604 O << "\t\t; ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000605 WriteAsOperand(O, I, true, true, &M);
606 O << "\n";
607 } else {
608 switch (I->getLinkage()) {
609 case GlobalValue::LinkOnceLinkage:
610 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
611 // Nonnull linkonce -> weak
612 O << "\t.weak " << name << "\n";
613 SwitchSection(O, CurSection, "");
614 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
615 break;
616
617 case GlobalValue::AppendingLinkage:
618 // FIXME: appending linkage variables should go into a section of
619 // their name or something. For now, just emit them as external.
620 case GlobalValue::ExternalLinkage:
621 // If external or appending, declare as a global symbol
622 O << "\t.globl " << name << "\n";
623 // FALL THROUGH
624 case GlobalValue::InternalLinkage:
Misha Brukman61297ee2004-06-29 23:40:57 +0000625 SwitchSection(O, CurSection, ".data");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000626 break;
627 }
628
629 O << "\t.align " << Align << "\n";
Misha Brukman218bec72004-06-29 17:13:26 +0000630 O << name << ":\t\t\t\t; ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000631 WriteAsOperand(O, I, true, true, &M);
632 O << " = ";
633 WriteAsOperand(O, C, false, false, &M);
634 O << "\n";
635 emitGlobalConstant(C);
636 }
637 }
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000638
639 // Output stubs for dynamically-linked functions
640 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
641 i != e; ++i)
Misha Brukman46fd00a2004-06-24 23:04:11 +0000642 {
Misha Brukman218bec72004-06-29 17:13:26 +0000643 O << "\t.picsymbol_stub\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000644 O << "L" << *i << "$stub:\n";
645 O << "\t.indirect_symbol " << *i << "\n";
646 O << "\tmflr r0\n";
Misha Brukman4cf51122004-07-06 22:40:34 +0000647 O << "\tbl L0$" << *i << "\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000648 O << "L0$" << *i << ":\n";
649 O << "\tmflr r11\n";
650 O << "\taddis r11,r11,ha16(L" << *i << "$lazy_ptr-L0$" << *i << ")\n";
651 O << "\tmtlr r0\n";
Misha Brukman218bec72004-06-29 17:13:26 +0000652 O << "\tlwz r12,lo16(L" << *i << "$lazy_ptr-L0$" << *i << ")(r11)\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000653 O << "\tmtctr r12\n";
Misha Brukman218bec72004-06-29 17:13:26 +0000654 O << "\taddi r11,r11,lo16(L" << *i << "$lazy_ptr - L0$" << *i << ")\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000655 O << "\tbctr\n";
656 O << ".data\n";
657 O << ".lazy_symbol_pointer\n";
658 O << "L" << *i << "$lazy_ptr:\n";
659 O << ".indirect_symbol " << *i << "\n";
660 O << ".long dyld_stub_binding_helper\n";
661 }
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000662
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000663 O << "\n";
664
665 // Output stubs for external global variables
666 if (GVStubs.begin() != GVStubs.end())
667 O << "\t.non_lazy_symbol_pointer\n";
668 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
669 i != e; ++i) {
670 O << "L" << *i << "$non_lazy_ptr:\n";
671 O << "\t.indirect_symbol " << *i << "\n";
672 O << "\t.long\t0\n";
673 }
674
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000675 delete Mang;
676 return false; // success
677}
678
679} // End llvm namespace