Misha Brukman | ca9309f | 2004-08-11 23:42:15 +0000 | [diff] [blame] | 1 | //===-- PPC64AsmPrinter.cpp - Print machine instrs to PowerPC assembly ----===// |
| 2 | // |
| 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 | // |
| 10 | // 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'. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #define DEBUG_TYPE "asmprinter" |
| 17 | #include "PowerPC.h" |
| 18 | #include "PowerPCInstrInfo.h" |
| 19 | #include "PPC64TargetMachine.h" |
| 20 | #include "llvm/Constants.h" |
| 21 | #include "llvm/DerivedTypes.h" |
| 22 | #include "llvm/Module.h" |
| 23 | #include "llvm/Assembly/Writer.h" |
| 24 | #include "llvm/CodeGen/MachineConstantPool.h" |
| 25 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 26 | #include "llvm/CodeGen/MachineInstr.h" |
| 27 | #include "llvm/Target/TargetMachine.h" |
| 28 | #include "llvm/Support/Mangler.h" |
| 29 | #include "Support/CommandLine.h" |
| 30 | #include "Support/Debug.h" |
| 31 | #include "Support/MathExtras.h" |
| 32 | #include "Support/Statistic.h" |
| 33 | #include "Support/StringExtras.h" |
| 34 | #include <set> |
| 35 | |
| 36 | namespace llvm { |
| 37 | |
| 38 | namespace { |
| 39 | Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed"); |
| 40 | |
| 41 | struct Printer : public MachineFunctionPass { |
| 42 | /// Output stream on which we're printing assembly code. |
| 43 | /// |
| 44 | std::ostream &O; |
| 45 | |
| 46 | /// Target machine description which we query for reg. names, data |
| 47 | /// layout, etc. |
| 48 | /// |
| 49 | PPC64TargetMachine &TM; |
| 50 | |
| 51 | /// Name-mangler for global names. |
| 52 | /// |
| 53 | Mangler *Mang; |
| 54 | |
| 55 | /// Map for labels corresponding to global variables |
| 56 | /// |
| 57 | std::map<const GlobalVariable*,std::string> GVToLabelMap; |
| 58 | |
| 59 | Printer(std::ostream &o, TargetMachine &tm) : O(o), |
| 60 | TM(reinterpret_cast<PPC64TargetMachine&>(tm)), LabelNumber(0) {} |
| 61 | |
| 62 | /// 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 | |
| 68 | /// Unique incrementer for label values for referencing Global values. |
| 69 | /// |
| 70 | unsigned LabelNumber; |
| 71 | |
| 72 | virtual const char *getPassName() const { |
| 73 | return "PPC64 Assembly Printer"; |
| 74 | } |
| 75 | |
| 76 | void printMachineInstruction(const MachineInstr *MI); |
| 77 | void printOp(const MachineOperand &MO, bool elideOffsetKeyword = false); |
| 78 | void printImmOp(const MachineOperand &MO, unsigned ArgType); |
| 79 | 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 | |
| 88 | /// createPPC64AsmPrinterPass - Returns a pass that prints the PPC |
| 89 | /// assembly code for a MachineFunction to the given output stream, |
| 90 | /// using the given target machine description. This should work |
| 91 | /// regardless of whether the function is in SSA form or not. |
| 92 | /// |
| 93 | FunctionPass *createPPC64AsmPrinter(std::ostream &o,TargetMachine &tm) { |
| 94 | 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 | /// |
| 100 | static 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 | /// |
| 114 | static inline char toOctal(int X) { |
| 115 | return (X&7)+'0'; |
| 116 | } |
| 117 | |
Misha Brukman | c90f296 | 2004-08-12 01:01:13 +0000 | [diff] [blame^] | 118 | // Possible states while outputting ASCII strings |
| 119 | namespace { |
| 120 | enum StringSection { |
| 121 | None, |
| 122 | Alpha, |
| 123 | Numeric |
| 124 | }; |
| 125 | } |
| 126 | |
| 127 | /// SwitchStringSection - manage the changes required to output bytes as |
| 128 | /// characters in a string vs. numeric decimal values |
| 129 | /// |
| 130 | static inline void SwitchStringSection(std::ostream &O, StringSection NewSect, |
| 131 | StringSection &Current) { |
| 132 | if (Current == None) { |
| 133 | if (NewSect == Alpha) |
| 134 | O << "\t.byte \""; |
| 135 | else if (NewSect == Numeric) |
| 136 | O << "\t.byte "; |
| 137 | } else if (Current == Alpha) { |
| 138 | if (NewSect == None) |
| 139 | O << "\""; |
| 140 | else if (NewSect == Numeric) |
| 141 | O << "\"\n" |
| 142 | << "\t.byte "; |
| 143 | } else if (Current == Numeric) { |
| 144 | if (NewSect == Alpha) |
| 145 | O << '\n' |
| 146 | << "\t.byte \""; |
| 147 | else if (NewSect == Numeric) |
| 148 | O << ", "; |
| 149 | } |
| 150 | |
| 151 | Current = NewSect; |
| 152 | } |
| 153 | |
Misha Brukman | ca9309f | 2004-08-11 23:42:15 +0000 | [diff] [blame] | 154 | /// getAsCString - Return the specified array as a C compatible |
| 155 | /// string, only if the predicate isStringCompatible is true. |
| 156 | /// |
| 157 | static void printAsCString(std::ostream &O, const ConstantArray *CVA) { |
| 158 | assert(isStringCompatible(CVA) && "Array is not string compatible!"); |
| 159 | |
Misha Brukman | c90f296 | 2004-08-12 01:01:13 +0000 | [diff] [blame^] | 160 | if (CVA->getNumOperands() == 0) |
| 161 | return; |
Misha Brukman | ca9309f | 2004-08-11 23:42:15 +0000 | [diff] [blame] | 162 | |
Misha Brukman | c90f296 | 2004-08-12 01:01:13 +0000 | [diff] [blame^] | 163 | StringSection Current = None; |
| 164 | for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i) { |
| 165 | unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue(); |
Misha Brukman | ca9309f | 2004-08-11 23:42:15 +0000 | [diff] [blame] | 166 | if (C == '"') { |
Misha Brukman | c90f296 | 2004-08-12 01:01:13 +0000 | [diff] [blame^] | 167 | SwitchStringSection(O, Alpha, Current); |
| 168 | O << "\"\""; |
Misha Brukman | ca9309f | 2004-08-11 23:42:15 +0000 | [diff] [blame] | 169 | } else if (isprint(C)) { |
Misha Brukman | c90f296 | 2004-08-12 01:01:13 +0000 | [diff] [blame^] | 170 | SwitchStringSection(O, Alpha, Current); |
Misha Brukman | ca9309f | 2004-08-11 23:42:15 +0000 | [diff] [blame] | 171 | O << C; |
| 172 | } else { |
Misha Brukman | c90f296 | 2004-08-12 01:01:13 +0000 | [diff] [blame^] | 173 | SwitchStringSection(O, Numeric, Current); |
| 174 | O << utostr((unsigned)C); |
Misha Brukman | ca9309f | 2004-08-11 23:42:15 +0000 | [diff] [blame] | 175 | } |
| 176 | } |
Misha Brukman | c90f296 | 2004-08-12 01:01:13 +0000 | [diff] [blame^] | 177 | SwitchStringSection(O, None, Current); |
| 178 | O << '\n'; |
Misha Brukman | ca9309f | 2004-08-11 23:42:15 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | // Print out the specified constant, without a storage class. Only the |
| 182 | // constants valid in constant expressions can occur here. |
| 183 | void Printer::emitConstantValueOnly(const Constant *CV) { |
| 184 | if (CV->isNullValue()) |
| 185 | O << "0"; |
| 186 | else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) { |
| 187 | assert(CB == ConstantBool::True); |
| 188 | O << "1"; |
| 189 | } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV)) |
| 190 | O << CI->getValue(); |
| 191 | else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV)) |
| 192 | O << CI->getValue(); |
| 193 | else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) |
| 194 | // This is a constant address for a global variable or function. Use the |
| 195 | // name of the variable or function as the address value. |
| 196 | O << Mang->getValueName(GV); |
| 197 | else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) { |
| 198 | const TargetData &TD = TM.getTargetData(); |
| 199 | switch (CE->getOpcode()) { |
| 200 | case Instruction::GetElementPtr: { |
| 201 | // generate a symbolic expression for the byte address |
| 202 | const Constant *ptrVal = CE->getOperand(0); |
| 203 | std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end()); |
| 204 | if (unsigned Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) { |
| 205 | O << "("; |
| 206 | emitConstantValueOnly(ptrVal); |
| 207 | O << ") + " << Offset; |
| 208 | } else { |
| 209 | emitConstantValueOnly(ptrVal); |
| 210 | } |
| 211 | break; |
| 212 | } |
| 213 | case Instruction::Cast: { |
| 214 | // Support only non-converting or widening casts for now, that is, ones |
| 215 | // that do not involve a change in value. This assertion is really gross, |
| 216 | // and may not even be a complete check. |
| 217 | Constant *Op = CE->getOperand(0); |
| 218 | const Type *OpTy = Op->getType(), *Ty = CE->getType(); |
| 219 | |
| 220 | // Remember, kids, pointers on x86 can be losslessly converted back and |
| 221 | // forth into 32-bit or wider integers, regardless of signedness. :-P |
| 222 | assert(((isa<PointerType>(OpTy) |
| 223 | && (Ty == Type::LongTy || Ty == Type::ULongTy |
| 224 | || Ty == Type::IntTy || Ty == Type::UIntTy)) |
| 225 | || (isa<PointerType>(Ty) |
| 226 | && (OpTy == Type::LongTy || OpTy == Type::ULongTy |
| 227 | || OpTy == Type::IntTy || OpTy == Type::UIntTy)) |
| 228 | || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy)) |
| 229 | && OpTy->isLosslesslyConvertibleTo(Ty)))) |
| 230 | && "FIXME: Don't yet support this kind of constant cast expr"); |
| 231 | O << "("; |
| 232 | emitConstantValueOnly(Op); |
| 233 | O << ")"; |
| 234 | break; |
| 235 | } |
| 236 | case Instruction::Add: |
| 237 | O << "("; |
| 238 | emitConstantValueOnly(CE->getOperand(0)); |
| 239 | O << ") + ("; |
| 240 | emitConstantValueOnly(CE->getOperand(1)); |
| 241 | O << ")"; |
| 242 | break; |
| 243 | default: |
| 244 | assert(0 && "Unsupported operator!"); |
| 245 | } |
| 246 | } else { |
| 247 | assert(0 && "Unknown constant value!"); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | // Print a constant value or values, with the appropriate storage class as a |
| 252 | // prefix. |
| 253 | void Printer::emitGlobalConstant(const Constant *CV) { |
| 254 | const TargetData &TD = TM.getTargetData(); |
| 255 | |
| 256 | if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) { |
| 257 | if (isStringCompatible(CVA)) { |
Misha Brukman | ca9309f | 2004-08-11 23:42:15 +0000 | [diff] [blame] | 258 | printAsCString(O, CVA); |
Misha Brukman | ca9309f | 2004-08-11 23:42:15 +0000 | [diff] [blame] | 259 | } else { // Not a string. Print the values in successive locations |
| 260 | for (unsigned i=0, e = CVA->getNumOperands(); i != e; i++) |
| 261 | emitGlobalConstant(CVA->getOperand(i)); |
| 262 | } |
| 263 | return; |
| 264 | } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) { |
| 265 | // Print the fields in successive locations. Pad to align if needed! |
| 266 | const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType()); |
| 267 | unsigned sizeSoFar = 0; |
| 268 | for (unsigned i = 0, e = CVS->getNumOperands(); i != e; i++) { |
| 269 | const Constant* field = CVS->getOperand(i); |
| 270 | |
| 271 | // Check if padding is needed and insert one or more 0s. |
| 272 | unsigned fieldSize = TD.getTypeSize(field->getType()); |
| 273 | unsigned padSize = ((i == e-1? cvsLayout->StructSize |
| 274 | : cvsLayout->MemberOffsets[i+1]) |
| 275 | - cvsLayout->MemberOffsets[i]) - fieldSize; |
| 276 | sizeSoFar += fieldSize + padSize; |
| 277 | |
| 278 | // Now print the actual field value |
| 279 | emitGlobalConstant(field); |
| 280 | |
| 281 | // Insert the field padding unless it's zero bytes... |
| 282 | if (padSize) |
| 283 | O << "\t.space\t " << padSize << "\n"; |
| 284 | } |
| 285 | assert(sizeSoFar == cvsLayout->StructSize && |
| 286 | "Layout of constant struct may be incorrect!"); |
| 287 | return; |
| 288 | } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) { |
| 289 | // FP Constants are printed as integer constants to avoid losing |
| 290 | // precision... |
| 291 | double Val = CFP->getValue(); |
| 292 | switch (CFP->getType()->getTypeID()) { |
| 293 | default: assert(0 && "Unknown floating point type!"); |
| 294 | case Type::FloatTyID: { |
| 295 | union FU { // Abide by C TBAA rules |
| 296 | float FVal; |
| 297 | unsigned UVal; |
| 298 | } U; |
| 299 | U.FVal = Val; |
| 300 | O << "\t.long " << U.UVal << "\t# float " << Val << "\n"; |
| 301 | return; |
| 302 | } |
| 303 | case Type::DoubleTyID: { |
| 304 | union DU { // Abide by C TBAA rules |
| 305 | double FVal; |
| 306 | uint64_t UVal; |
| 307 | struct { |
| 308 | uint32_t MSWord; |
| 309 | uint32_t LSWord; |
| 310 | } T; |
| 311 | } U; |
| 312 | U.FVal = Val; |
| 313 | |
| 314 | O << ".long " << U.T.MSWord << "\t# double most significant word " |
| 315 | << Val << "\n"; |
| 316 | O << ".long " << U.T.LSWord << "\t# double least significant word " |
| 317 | << Val << "\n"; |
| 318 | return; |
| 319 | } |
| 320 | } |
| 321 | } else if (CV->getType() == Type::ULongTy || CV->getType() == Type::LongTy) { |
| 322 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) { |
| 323 | union DU { // Abide by C TBAA rules |
| 324 | int64_t UVal; |
| 325 | struct { |
| 326 | uint32_t MSWord; |
| 327 | uint32_t LSWord; |
| 328 | } T; |
| 329 | } U; |
| 330 | U.UVal = CI->getRawValue(); |
| 331 | |
| 332 | O << ".long " << U.T.MSWord << "\t# Double-word most significant word " |
| 333 | << U.UVal << "\n"; |
| 334 | O << ".long " << U.T.LSWord << "\t# Double-word least significant word " |
| 335 | << U.UVal << "\n"; |
| 336 | return; |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | const Type *type = CV->getType(); |
| 341 | O << "\t"; |
| 342 | switch (type->getTypeID()) { |
| 343 | case Type::UByteTyID: case Type::SByteTyID: |
| 344 | O << "\t.byte"; |
| 345 | break; |
| 346 | case Type::UShortTyID: case Type::ShortTyID: |
| 347 | O << "\t.short"; |
| 348 | break; |
| 349 | case Type::BoolTyID: |
| 350 | case Type::PointerTyID: |
| 351 | case Type::UIntTyID: case Type::IntTyID: |
| 352 | O << "\t.long"; |
| 353 | break; |
| 354 | case Type::ULongTyID: case Type::LongTyID: |
| 355 | assert (0 && "Should have already output double-word constant."); |
| 356 | case Type::FloatTyID: case Type::DoubleTyID: |
| 357 | assert (0 && "Should have already output floating point constant."); |
| 358 | default: |
| 359 | if (CV == Constant::getNullValue(type)) { // Zero initializer? |
| 360 | O << "\t.space " << TD.getTypeSize(type) << "\n"; |
| 361 | return; |
| 362 | } |
| 363 | std::cerr << "Can't handle printing: " << *CV; |
| 364 | abort(); |
| 365 | break; |
| 366 | } |
| 367 | O << ' '; |
| 368 | emitConstantValueOnly(CV); |
| 369 | O << '\n'; |
| 370 | } |
| 371 | |
| 372 | /// printConstantPool - Print to the current output stream assembly |
| 373 | /// representations of the constants in the constant pool MCP. This is |
| 374 | /// used to print out constants which have been "spilled to memory" by |
| 375 | /// the code generator. |
| 376 | /// |
| 377 | void Printer::printConstantPool(MachineConstantPool *MCP) { |
| 378 | const std::vector<Constant*> &CP = MCP->getConstants(); |
| 379 | const TargetData &TD = TM.getTargetData(); |
| 380 | |
| 381 | if (CP.empty()) return; |
| 382 | |
| 383 | for (unsigned i = 0, e = CP.size(); i != e; ++i) { |
| 384 | O << "\t.const\n"; |
| 385 | O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType()) |
| 386 | << "\n"; |
| 387 | O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t;" |
| 388 | << *CP[i] << "\n"; |
| 389 | emitGlobalConstant(CP[i]); |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | /// runOnMachineFunction - This uses the printMachineInstruction() |
| 394 | /// method to print assembly for each instruction. |
| 395 | /// |
| 396 | bool Printer::runOnMachineFunction(MachineFunction &MF) { |
| 397 | CurrentFnName = MF.getFunction()->getName(); |
| 398 | |
| 399 | // Print out constants referenced by the function |
| 400 | printConstantPool(MF.getConstantPool()); |
| 401 | |
| 402 | // Print out header for the function. |
| 403 | O << "\t.csect .text[PR]\n" |
| 404 | << "\t.align 2\n" |
| 405 | << "\t.globl " << CurrentFnName << '\n' |
| 406 | << "\t.globl ." << CurrentFnName << '\n' |
| 407 | << "\t.csect " << CurrentFnName << "[DS],3\n" |
| 408 | << CurrentFnName << ":\n" |
| 409 | << "\t.llong ." << CurrentFnName << ", TOC[tc0], 0\n" |
| 410 | << "\t.csect .text[PR]\n" |
| 411 | << '.' << CurrentFnName << ":\n"; |
| 412 | |
| 413 | // Print out code for the function. |
| 414 | for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); |
| 415 | I != E; ++I) { |
| 416 | // Print a label for the basic block. |
| 417 | O << "LBB" << CurrentFnName << "_" << I->getNumber() << ":\t# " |
| 418 | << I->getBasicBlock()->getName() << "\n"; |
| 419 | for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end(); |
| 420 | II != E; ++II) { |
| 421 | // Print the assembly for the instruction. |
| 422 | O << "\t"; |
| 423 | printMachineInstruction(II); |
| 424 | } |
| 425 | } |
| 426 | ++LabelNumber; |
| 427 | |
| 428 | O << "LT.." << CurrentFnName << ":\n" |
| 429 | << "\t.long 0\n" |
| 430 | << "\t.byte 0,0,32,65,128,0,0,0\n" |
| 431 | << "\t.long LT.." << CurrentFnName << "-." << CurrentFnName << '\n' |
| 432 | << "\t.short 3\n" |
| 433 | << "\t.byte \"" << CurrentFnName << "\"\n" |
| 434 | << "\t.align 2\n"; |
| 435 | |
| 436 | // We didn't modify anything. |
| 437 | return false; |
| 438 | } |
| 439 | |
| 440 | void Printer::printOp(const MachineOperand &MO, |
| 441 | bool elideOffsetKeyword /* = false */) { |
| 442 | const MRegisterInfo &RI = *TM.getRegisterInfo(); |
| 443 | int new_symbol; |
| 444 | |
| 445 | switch (MO.getType()) { |
| 446 | case MachineOperand::MO_VirtualRegister: |
| 447 | if (Value *V = MO.getVRegValueOrNull()) { |
| 448 | O << "<" << V->getName() << ">"; |
| 449 | return; |
| 450 | } |
| 451 | // FALLTHROUGH |
| 452 | case MachineOperand::MO_MachineRegister: |
| 453 | case MachineOperand::MO_CCRegister: { |
| 454 | // On AIX, do not print out the 'r' in register names |
| 455 | const char *regName = RI.get(MO.getReg()).Name; |
| 456 | O << ®Name[1]; |
| 457 | return; |
| 458 | } |
| 459 | |
| 460 | case MachineOperand::MO_SignExtendedImmed: |
| 461 | case MachineOperand::MO_UnextendedImmed: |
| 462 | std::cerr << "printOp() does not handle immediate values\n"; |
| 463 | abort(); |
| 464 | return; |
| 465 | |
| 466 | case MachineOperand::MO_PCRelativeDisp: |
| 467 | std::cerr << "Shouldn't use addPCDisp() when building PPC MachineInstrs"; |
| 468 | abort(); |
| 469 | return; |
| 470 | |
| 471 | case MachineOperand::MO_MachineBasicBlock: { |
| 472 | MachineBasicBlock *MBBOp = MO.getMachineBasicBlock(); |
| 473 | O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction()) |
| 474 | << "_" << MBBOp->getNumber() << "\t# " |
| 475 | << MBBOp->getBasicBlock()->getName(); |
| 476 | return; |
| 477 | } |
| 478 | |
| 479 | case MachineOperand::MO_ConstantPoolIndex: |
| 480 | O << ".CPI" << CurrentFnName << "_" << MO.getConstantPoolIndex(); |
| 481 | return; |
| 482 | |
| 483 | case MachineOperand::MO_ExternalSymbol: |
| 484 | O << MO.getSymbolName(); |
| 485 | return; |
| 486 | |
| 487 | case MachineOperand::MO_GlobalAddress: |
| 488 | if (!elideOffsetKeyword) { |
| 489 | GlobalValue *GV = MO.getGlobal(); |
| 490 | |
| 491 | if (Function *F = dyn_cast<Function>(GV)) { |
| 492 | O << "." << F->getName(); |
| 493 | } else if (GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV)) { |
| 494 | // output the label name |
| 495 | O << GVToLabelMap[GVar]; |
| 496 | } |
| 497 | } |
| 498 | return; |
| 499 | |
| 500 | default: |
| 501 | O << "<unknown operand type: " << MO.getType() << ">"; |
| 502 | return; |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | void Printer::printImmOp(const MachineOperand &MO, unsigned ArgType) { |
| 507 | int Imm = MO.getImmedValue(); |
| 508 | if (ArgType == PPCII::Simm16 || ArgType == PPCII::Disimm16) { |
| 509 | O << (short)Imm; |
| 510 | } else if (ArgType == PPCII::Zimm16) { |
| 511 | O << (unsigned short)Imm; |
| 512 | } else { |
| 513 | O << Imm; |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | /// printMachineInstruction -- Print out a single PPC LLVM instruction |
| 518 | /// MI in Darwin syntax to the current output stream. |
| 519 | /// |
| 520 | void Printer::printMachineInstruction(const MachineInstr *MI) { |
| 521 | unsigned Opcode = MI->getOpcode(); |
| 522 | const TargetInstrInfo &TII = *TM.getInstrInfo(); |
| 523 | const TargetInstrDescriptor &Desc = TII.get(Opcode); |
| 524 | unsigned i; |
| 525 | |
| 526 | unsigned ArgCount = MI->getNumOperands(); |
| 527 | unsigned ArgType[] = { |
| 528 | (Desc.TSFlags >> PPCII::Arg0TypeShift) & PPCII::ArgTypeMask, |
| 529 | (Desc.TSFlags >> PPCII::Arg1TypeShift) & PPCII::ArgTypeMask, |
| 530 | (Desc.TSFlags >> PPCII::Arg2TypeShift) & PPCII::ArgTypeMask, |
| 531 | (Desc.TSFlags >> PPCII::Arg3TypeShift) & PPCII::ArgTypeMask, |
| 532 | (Desc.TSFlags >> PPCII::Arg4TypeShift) & PPCII::ArgTypeMask |
| 533 | }; |
| 534 | assert(((Desc.TSFlags & PPCII::VMX) == 0) && |
| 535 | "Instruction requires VMX support"); |
| 536 | ++EmittedInsts; |
| 537 | |
| 538 | // CALLpcrel and CALLindirect are handled specially here to print only the |
| 539 | // appropriate number of args that the assembler expects. This is because |
| 540 | // may have many arguments appended to record the uses of registers that are |
| 541 | // holding arguments to the called function. |
| 542 | if (Opcode == PPC::COND_BRANCH) { |
| 543 | std::cerr << "Error: untranslated conditional branch psuedo instruction!\n"; |
| 544 | abort(); |
| 545 | } else if (Opcode == PPC::IMPLICIT_DEF) { |
| 546 | O << "# IMPLICIT DEF "; |
| 547 | printOp(MI->getOperand(0)); |
| 548 | O << "\n"; |
| 549 | return; |
| 550 | } else if (Opcode == PPC::CALLpcrel) { |
| 551 | O << TII.getName(Opcode) << " "; |
| 552 | printOp(MI->getOperand(0)); |
| 553 | O << "\n"; |
| 554 | return; |
| 555 | } else if (Opcode == PPC::CALLindirect) { |
| 556 | O << TII.getName(Opcode) << " "; |
| 557 | printImmOp(MI->getOperand(0), ArgType[0]); |
| 558 | O << ", "; |
| 559 | printImmOp(MI->getOperand(1), ArgType[0]); |
| 560 | O << "\n"; |
| 561 | return; |
| 562 | } else if (Opcode == PPC::MovePCtoLR) { |
| 563 | // FIXME: should probably be converted to cout.width and cout.fill |
| 564 | O << "bl \"L0000" << LabelNumber << "$pb\"\n"; |
| 565 | O << "\"L0000" << LabelNumber << "$pb\":\n"; |
| 566 | O << "\tmflr "; |
| 567 | printOp(MI->getOperand(0)); |
| 568 | O << "\n"; |
| 569 | return; |
| 570 | } |
| 571 | |
| 572 | O << TII.getName(Opcode) << " "; |
| 573 | if (Opcode == PPC::LD || Opcode == PPC::LWA || |
| 574 | Opcode == PPC::STDU || Opcode == PPC::STDUX) { |
| 575 | printOp(MI->getOperand(0)); |
| 576 | O << ", "; |
| 577 | MachineOperand MO = MI->getOperand(1); |
| 578 | if (MO.isImmediate()) |
| 579 | printImmOp(MO, ArgType[1]); |
| 580 | else |
| 581 | printOp(MO); |
| 582 | O << "("; |
| 583 | printOp(MI->getOperand(2)); |
| 584 | O << ")\n"; |
| 585 | } else if (Opcode == PPC::BLR || Opcode == PPC::NOP) { |
| 586 | // FIXME: BuildMI() should handle 0 params |
| 587 | O << "\n"; |
| 588 | } else if (ArgCount == 3 && ArgType[1] == PPCII::Disimm16) { |
| 589 | printOp(MI->getOperand(0)); |
| 590 | O << ", "; |
| 591 | printImmOp(MI->getOperand(1), ArgType[1]); |
| 592 | O << "("; |
| 593 | if (MI->getOperand(2).hasAllocatedReg() && |
| 594 | MI->getOperand(2).getReg() == PPC::R0) |
| 595 | O << "0"; |
| 596 | else |
| 597 | printOp(MI->getOperand(2)); |
| 598 | O << ")\n"; |
| 599 | } else { |
| 600 | for (i = 0; i < ArgCount; ++i) { |
| 601 | // addi and friends |
| 602 | if (i == 1 && ArgCount == 3 && ArgType[2] == PPCII::Simm16 && |
| 603 | MI->getOperand(1).hasAllocatedReg() && |
| 604 | MI->getOperand(1).getReg() == PPC::R0) { |
| 605 | O << "0"; |
| 606 | // for long branch support, bc $+8 |
| 607 | } else if (i == 1 && ArgCount == 2 && MI->getOperand(1).isImmediate() && |
| 608 | TII.isBranch(MI->getOpcode())) { |
| 609 | O << "$+8"; |
| 610 | assert(8 == MI->getOperand(i).getImmedValue() |
| 611 | && "branch off PC not to pc+8?"); |
| 612 | //printOp(MI->getOperand(i)); |
| 613 | } else if (MI->getOperand(i).isImmediate()) { |
| 614 | printImmOp(MI->getOperand(i), ArgType[i]); |
| 615 | } else { |
| 616 | printOp(MI->getOperand(i)); |
| 617 | } |
| 618 | if (ArgCount - 1 == i) |
| 619 | O << "\n"; |
| 620 | else |
| 621 | O << ", "; |
| 622 | } |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | // SwitchSection - Switch to the specified section of the executable if we are |
| 627 | // not already in it! |
| 628 | // |
| 629 | static void SwitchSection(std::ostream &OS, std::string &CurSection, |
| 630 | const char *NewSection) { |
| 631 | if (CurSection != NewSection) { |
| 632 | CurSection = NewSection; |
| 633 | if (!CurSection.empty()) |
| 634 | OS << "\t" << NewSection << "\n"; |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | bool Printer::doInitialization(Module &M) { |
| 639 | const TargetData &TD = TM.getTargetData(); |
| 640 | std::string CurSection; |
| 641 | |
| 642 | O << "\t.machine \"ppc64\"\n" |
| 643 | << "\t.toc\n" |
| 644 | << "\t.csect .text[PR]\n"; |
| 645 | |
| 646 | // Print out module-level global variables |
| 647 | for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I) { |
| 648 | if (!I->hasInitializer()) |
| 649 | continue; |
| 650 | |
| 651 | std::string Name = I->getName(); |
| 652 | Constant *C = I->getInitializer(); |
| 653 | // N.B.: We are defaulting to writable strings |
| 654 | if (I->hasExternalLinkage()) { |
| 655 | O << "\t.globl " << Name << '\n' |
| 656 | << "\t.csect .data[RW],3\n"; |
| 657 | } else { |
| 658 | O << "\t.csect _global.rw_c[RW],3\n"; |
| 659 | } |
| 660 | O << Name << ":\n"; |
| 661 | emitGlobalConstant(C); |
| 662 | } |
| 663 | |
| 664 | // Output labels for globals |
| 665 | if (M.gbegin() != M.gend()) O << "\t.toc\n"; |
| 666 | for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I) { |
| 667 | const GlobalVariable *GV = I; |
| 668 | // Do not output labels for unused variables |
| 669 | if (GV->isExternal() && GV->use_begin() == GV->use_end()) |
| 670 | continue; |
| 671 | |
| 672 | std::string Name = GV->getName(); |
| 673 | std::string Label = "LC.." + utostr(LabelNumber++); |
| 674 | GVToLabelMap[GV] = Label; |
| 675 | O << Label << ":\n" |
| 676 | << "\t.tc " << Name << "[TC]," << Name; |
| 677 | if (GV->isExternal()) O << "[RW]"; |
| 678 | O << '\n'; |
| 679 | } |
| 680 | |
| 681 | Mang = new Mangler(M, true); |
| 682 | return false; // success |
| 683 | } |
| 684 | |
| 685 | bool Printer::doFinalization(Module &M) { |
| 686 | const TargetData &TD = TM.getTargetData(); |
| 687 | // Print out module-level global variables |
| 688 | for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I) { |
| 689 | if (I->hasInitializer() || I->hasExternalLinkage()) |
| 690 | continue; |
| 691 | |
| 692 | std::string Name = I->getName(); |
| 693 | if (I->hasInternalLinkage()) { |
| 694 | O << "\t.lcomm " << Name << ",16,_global.bss_c"; |
| 695 | } else { |
| 696 | O << "\t.comm " << Name << "," << TD.getTypeSize(I->getType()) |
| 697 | << "," << log2((unsigned)TD.getTypeAlignment(I->getType())); |
| 698 | } |
| 699 | O << "\t\t# "; |
| 700 | WriteAsOperand(O, I, true, true, &M); |
| 701 | O << "\n"; |
| 702 | } |
| 703 | |
| 704 | O << "_section_.text:\n" |
| 705 | << "\t.csect .data[RW],3\n" |
| 706 | << "\t.llong _section_.text\n"; |
| 707 | |
| 708 | delete Mang; |
| 709 | return false; // success |
| 710 | } |
| 711 | |
| 712 | } // End llvm namespace |