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 | |
| 118 | /// getAsCString - Return the specified array as a C compatible |
| 119 | /// string, only if the predicate isStringCompatible is true. |
| 120 | /// |
| 121 | static 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 { |
| 135 | switch (C) { |
| 136 | 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. |
| 155 | void 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(); |
| 165 | else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) |
| 166 | // This is a constant address for a global variable or function. Use the |
| 167 | // name of the variable or function as the address value. |
| 168 | O << Mang->getValueName(GV); |
| 169 | else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) { |
| 170 | const TargetData &TD = TM.getTargetData(); |
| 171 | switch (CE->getOpcode()) { |
| 172 | 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. |
| 225 | void Printer::emitGlobalConstant(const Constant *CV) { |
| 226 | const TargetData &TD = TM.getTargetData(); |
| 227 | |
| 228 | if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) { |
| 229 | if (isStringCompatible(CVA)) { |
| 230 | O << "\t.byte "; |
| 231 | printAsCString(O, CVA); |
| 232 | O << "\n"; |
| 233 | } else { // Not a string. Print the values in successive locations |
| 234 | for (unsigned i=0, e = CVA->getNumOperands(); i != e; i++) |
| 235 | emitGlobalConstant(CVA->getOperand(i)); |
| 236 | } |
| 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()); |
| 241 | unsigned sizeSoFar = 0; |
| 242 | for (unsigned i = 0, e = CVS->getNumOperands(); i != e; i++) { |
| 243 | const Constant* field = CVS->getOperand(i); |
| 244 | |
| 245 | // Check if padding is needed and insert one or more 0s. |
| 246 | unsigned fieldSize = TD.getTypeSize(field->getType()); |
| 247 | unsigned padSize = ((i == e-1? cvsLayout->StructSize |
| 248 | : 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(); |
| 266 | switch (CFP->getType()->getTypeID()) { |
| 267 | 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; |
| 274 | O << "\t.long " << U.UVal << "\t# float " << Val << "\n"; |
| 275 | return; |
| 276 | } |
| 277 | case Type::DoubleTyID: { |
| 278 | union DU { // Abide by C TBAA rules |
| 279 | double FVal; |
| 280 | uint64_t UVal; |
| 281 | struct { |
| 282 | uint32_t MSWord; |
| 283 | uint32_t LSWord; |
| 284 | } T; |
| 285 | } U; |
| 286 | U.FVal = Val; |
| 287 | |
| 288 | O << ".long " << U.T.MSWord << "\t# double most significant word " |
| 289 | << Val << "\n"; |
| 290 | O << ".long " << U.T.LSWord << "\t# double least significant word " |
| 291 | << Val << "\n"; |
| 292 | return; |
| 293 | } |
| 294 | } |
| 295 | } else if (CV->getType() == Type::ULongTy || CV->getType() == Type::LongTy) { |
| 296 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) { |
| 297 | union DU { // Abide by C TBAA rules |
| 298 | int64_t UVal; |
| 299 | struct { |
| 300 | uint32_t MSWord; |
| 301 | uint32_t LSWord; |
| 302 | } T; |
| 303 | } U; |
| 304 | U.UVal = CI->getRawValue(); |
| 305 | |
| 306 | O << ".long " << U.T.MSWord << "\t# Double-word most significant word " |
| 307 | << U.UVal << "\n"; |
| 308 | O << ".long " << U.T.LSWord << "\t# Double-word least significant word " |
| 309 | << U.UVal << "\n"; |
| 310 | return; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | const Type *type = CV->getType(); |
| 315 | O << "\t"; |
| 316 | switch (type->getTypeID()) { |
| 317 | case Type::UByteTyID: case Type::SByteTyID: |
| 318 | O << "\t.byte"; |
| 319 | break; |
| 320 | case Type::UShortTyID: case Type::ShortTyID: |
| 321 | O << "\t.short"; |
| 322 | break; |
| 323 | case Type::BoolTyID: |
| 324 | case Type::PointerTyID: |
| 325 | case Type::UIntTyID: case Type::IntTyID: |
| 326 | O << "\t.long"; |
| 327 | break; |
| 328 | case Type::ULongTyID: case Type::LongTyID: |
| 329 | assert (0 && "Should have already output double-word constant."); |
| 330 | case Type::FloatTyID: case Type::DoubleTyID: |
| 331 | assert (0 && "Should have already output floating point constant."); |
| 332 | default: |
| 333 | if (CV == Constant::getNullValue(type)) { // Zero initializer? |
| 334 | O << "\t.space " << TD.getTypeSize(type) << "\n"; |
| 335 | return; |
| 336 | } |
| 337 | std::cerr << "Can't handle printing: " << *CV; |
| 338 | abort(); |
| 339 | break; |
| 340 | } |
| 341 | O << ' '; |
| 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 | /// |
| 351 | void 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"; |
| 361 | O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t;" |
| 362 | << *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 | /// |
| 370 | bool Printer::runOnMachineFunction(MachineFunction &MF) { |
| 371 | CurrentFnName = MF.getFunction()->getName(); |
| 372 | |
| 373 | // Print out constants referenced by the function |
| 374 | printConstantPool(MF.getConstantPool()); |
| 375 | |
| 376 | // Print out header for the function. |
| 377 | O << "\t.csect .text[PR]\n" |
| 378 | << "\t.align 2\n" |
| 379 | << "\t.globl " << CurrentFnName << '\n' |
| 380 | << "\t.globl ." << CurrentFnName << '\n' |
| 381 | << "\t.csect " << CurrentFnName << "[DS],3\n" |
| 382 | << CurrentFnName << ":\n" |
| 383 | << "\t.llong ." << CurrentFnName << ", TOC[tc0], 0\n" |
| 384 | << "\t.csect .text[PR]\n" |
| 385 | << '.' << CurrentFnName << ":\n"; |
| 386 | |
| 387 | // Print out code for the function. |
| 388 | for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); |
| 389 | I != E; ++I) { |
| 390 | // Print a label for the basic block. |
| 391 | O << "LBB" << CurrentFnName << "_" << I->getNumber() << ":\t# " |
| 392 | << I->getBasicBlock()->getName() << "\n"; |
| 393 | for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end(); |
| 394 | II != E; ++II) { |
| 395 | // Print the assembly for the instruction. |
| 396 | O << "\t"; |
| 397 | printMachineInstruction(II); |
| 398 | } |
| 399 | } |
| 400 | ++LabelNumber; |
| 401 | |
| 402 | O << "LT.." << CurrentFnName << ":\n" |
| 403 | << "\t.long 0\n" |
| 404 | << "\t.byte 0,0,32,65,128,0,0,0\n" |
| 405 | << "\t.long LT.." << CurrentFnName << "-." << CurrentFnName << '\n' |
| 406 | << "\t.short 3\n" |
| 407 | << "\t.byte \"" << CurrentFnName << "\"\n" |
| 408 | << "\t.align 2\n"; |
| 409 | |
| 410 | // We didn't modify anything. |
| 411 | return false; |
| 412 | } |
| 413 | |
| 414 | void Printer::printOp(const MachineOperand &MO, |
| 415 | bool elideOffsetKeyword /* = false */) { |
| 416 | const MRegisterInfo &RI = *TM.getRegisterInfo(); |
| 417 | int new_symbol; |
| 418 | |
| 419 | switch (MO.getType()) { |
| 420 | case MachineOperand::MO_VirtualRegister: |
| 421 | if (Value *V = MO.getVRegValueOrNull()) { |
| 422 | O << "<" << V->getName() << ">"; |
| 423 | return; |
| 424 | } |
| 425 | // FALLTHROUGH |
| 426 | case MachineOperand::MO_MachineRegister: |
| 427 | case MachineOperand::MO_CCRegister: { |
| 428 | // On AIX, do not print out the 'r' in register names |
| 429 | const char *regName = RI.get(MO.getReg()).Name; |
| 430 | O << ®Name[1]; |
| 431 | return; |
| 432 | } |
| 433 | |
| 434 | case MachineOperand::MO_SignExtendedImmed: |
| 435 | case MachineOperand::MO_UnextendedImmed: |
| 436 | std::cerr << "printOp() does not handle immediate values\n"; |
| 437 | abort(); |
| 438 | return; |
| 439 | |
| 440 | case MachineOperand::MO_PCRelativeDisp: |
| 441 | std::cerr << "Shouldn't use addPCDisp() when building PPC MachineInstrs"; |
| 442 | abort(); |
| 443 | return; |
| 444 | |
| 445 | case MachineOperand::MO_MachineBasicBlock: { |
| 446 | MachineBasicBlock *MBBOp = MO.getMachineBasicBlock(); |
| 447 | O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction()) |
| 448 | << "_" << MBBOp->getNumber() << "\t# " |
| 449 | << MBBOp->getBasicBlock()->getName(); |
| 450 | return; |
| 451 | } |
| 452 | |
| 453 | case MachineOperand::MO_ConstantPoolIndex: |
| 454 | O << ".CPI" << CurrentFnName << "_" << MO.getConstantPoolIndex(); |
| 455 | return; |
| 456 | |
| 457 | case MachineOperand::MO_ExternalSymbol: |
| 458 | O << MO.getSymbolName(); |
| 459 | return; |
| 460 | |
| 461 | case MachineOperand::MO_GlobalAddress: |
| 462 | if (!elideOffsetKeyword) { |
| 463 | GlobalValue *GV = MO.getGlobal(); |
| 464 | |
| 465 | if (Function *F = dyn_cast<Function>(GV)) { |
| 466 | O << "." << F->getName(); |
| 467 | } else if (GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV)) { |
| 468 | // output the label name |
| 469 | O << GVToLabelMap[GVar]; |
| 470 | } |
| 471 | } |
| 472 | return; |
| 473 | |
| 474 | default: |
| 475 | O << "<unknown operand type: " << MO.getType() << ">"; |
| 476 | return; |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | void Printer::printImmOp(const MachineOperand &MO, unsigned ArgType) { |
| 481 | int Imm = MO.getImmedValue(); |
| 482 | if (ArgType == PPCII::Simm16 || ArgType == PPCII::Disimm16) { |
| 483 | O << (short)Imm; |
| 484 | } else if (ArgType == PPCII::Zimm16) { |
| 485 | O << (unsigned short)Imm; |
| 486 | } else { |
| 487 | O << Imm; |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | /// printMachineInstruction -- Print out a single PPC LLVM instruction |
| 492 | /// MI in Darwin syntax to the current output stream. |
| 493 | /// |
| 494 | void Printer::printMachineInstruction(const MachineInstr *MI) { |
| 495 | unsigned Opcode = MI->getOpcode(); |
| 496 | const TargetInstrInfo &TII = *TM.getInstrInfo(); |
| 497 | const TargetInstrDescriptor &Desc = TII.get(Opcode); |
| 498 | unsigned i; |
| 499 | |
| 500 | unsigned ArgCount = MI->getNumOperands(); |
| 501 | unsigned ArgType[] = { |
| 502 | (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 |
| 507 | }; |
| 508 | assert(((Desc.TSFlags & PPCII::VMX) == 0) && |
| 509 | "Instruction requires VMX support"); |
| 510 | ++EmittedInsts; |
| 511 | |
| 512 | // CALLpcrel and CALLindirect are handled specially here to print only the |
| 513 | // appropriate number of args that the assembler expects. This is because |
| 514 | // may have many arguments appended to record the uses of registers that are |
| 515 | // holding arguments to the called function. |
| 516 | if (Opcode == PPC::COND_BRANCH) { |
| 517 | std::cerr << "Error: untranslated conditional branch psuedo instruction!\n"; |
| 518 | abort(); |
| 519 | } else if (Opcode == PPC::IMPLICIT_DEF) { |
| 520 | O << "# IMPLICIT DEF "; |
| 521 | printOp(MI->getOperand(0)); |
| 522 | O << "\n"; |
| 523 | return; |
| 524 | } else if (Opcode == PPC::CALLpcrel) { |
| 525 | O << TII.getName(Opcode) << " "; |
| 526 | printOp(MI->getOperand(0)); |
| 527 | O << "\n"; |
| 528 | return; |
| 529 | } else if (Opcode == PPC::CALLindirect) { |
| 530 | O << TII.getName(Opcode) << " "; |
| 531 | printImmOp(MI->getOperand(0), ArgType[0]); |
| 532 | O << ", "; |
| 533 | printImmOp(MI->getOperand(1), ArgType[0]); |
| 534 | O << "\n"; |
| 535 | return; |
| 536 | } else if (Opcode == PPC::MovePCtoLR) { |
| 537 | // FIXME: should probably be converted to cout.width and cout.fill |
| 538 | O << "bl \"L0000" << LabelNumber << "$pb\"\n"; |
| 539 | O << "\"L0000" << LabelNumber << "$pb\":\n"; |
| 540 | O << "\tmflr "; |
| 541 | printOp(MI->getOperand(0)); |
| 542 | O << "\n"; |
| 543 | return; |
| 544 | } |
| 545 | |
| 546 | O << TII.getName(Opcode) << " "; |
| 547 | if (Opcode == PPC::LD || Opcode == PPC::LWA || |
| 548 | Opcode == PPC::STDU || Opcode == PPC::STDUX) { |
| 549 | printOp(MI->getOperand(0)); |
| 550 | O << ", "; |
| 551 | MachineOperand MO = MI->getOperand(1); |
| 552 | if (MO.isImmediate()) |
| 553 | printImmOp(MO, ArgType[1]); |
| 554 | else |
| 555 | printOp(MO); |
| 556 | O << "("; |
| 557 | printOp(MI->getOperand(2)); |
| 558 | O << ")\n"; |
| 559 | } else if (Opcode == PPC::BLR || Opcode == PPC::NOP) { |
| 560 | // FIXME: BuildMI() should handle 0 params |
| 561 | O << "\n"; |
| 562 | } else if (ArgCount == 3 && ArgType[1] == PPCII::Disimm16) { |
| 563 | printOp(MI->getOperand(0)); |
| 564 | O << ", "; |
| 565 | printImmOp(MI->getOperand(1), ArgType[1]); |
| 566 | O << "("; |
| 567 | if (MI->getOperand(2).hasAllocatedReg() && |
| 568 | MI->getOperand(2).getReg() == PPC::R0) |
| 569 | O << "0"; |
| 570 | else |
| 571 | printOp(MI->getOperand(2)); |
| 572 | O << ")\n"; |
| 573 | } else { |
| 574 | for (i = 0; i < ArgCount; ++i) { |
| 575 | // addi and friends |
| 576 | if (i == 1 && ArgCount == 3 && ArgType[2] == PPCII::Simm16 && |
| 577 | MI->getOperand(1).hasAllocatedReg() && |
| 578 | MI->getOperand(1).getReg() == PPC::R0) { |
| 579 | O << "0"; |
| 580 | // for long branch support, bc $+8 |
| 581 | } else if (i == 1 && ArgCount == 2 && MI->getOperand(1).isImmediate() && |
| 582 | TII.isBranch(MI->getOpcode())) { |
| 583 | O << "$+8"; |
| 584 | assert(8 == MI->getOperand(i).getImmedValue() |
| 585 | && "branch off PC not to pc+8?"); |
| 586 | //printOp(MI->getOperand(i)); |
| 587 | } else if (MI->getOperand(i).isImmediate()) { |
| 588 | printImmOp(MI->getOperand(i), ArgType[i]); |
| 589 | } else { |
| 590 | printOp(MI->getOperand(i)); |
| 591 | } |
| 592 | if (ArgCount - 1 == i) |
| 593 | O << "\n"; |
| 594 | else |
| 595 | O << ", "; |
| 596 | } |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | // SwitchSection - Switch to the specified section of the executable if we are |
| 601 | // not already in it! |
| 602 | // |
| 603 | static void SwitchSection(std::ostream &OS, std::string &CurSection, |
| 604 | const char *NewSection) { |
| 605 | if (CurSection != NewSection) { |
| 606 | CurSection = NewSection; |
| 607 | if (!CurSection.empty()) |
| 608 | OS << "\t" << NewSection << "\n"; |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | bool Printer::doInitialization(Module &M) { |
| 613 | const TargetData &TD = TM.getTargetData(); |
| 614 | std::string CurSection; |
| 615 | |
| 616 | O << "\t.machine \"ppc64\"\n" |
| 617 | << "\t.toc\n" |
| 618 | << "\t.csect .text[PR]\n"; |
| 619 | |
| 620 | // Print out module-level global variables |
| 621 | for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I) { |
| 622 | if (!I->hasInitializer()) |
| 623 | continue; |
| 624 | |
| 625 | std::string Name = I->getName(); |
| 626 | Constant *C = I->getInitializer(); |
| 627 | // N.B.: We are defaulting to writable strings |
| 628 | if (I->hasExternalLinkage()) { |
| 629 | O << "\t.globl " << Name << '\n' |
| 630 | << "\t.csect .data[RW],3\n"; |
| 631 | } else { |
| 632 | O << "\t.csect _global.rw_c[RW],3\n"; |
| 633 | } |
| 634 | O << Name << ":\n"; |
| 635 | emitGlobalConstant(C); |
| 636 | } |
| 637 | |
| 638 | // Output labels for globals |
| 639 | if (M.gbegin() != M.gend()) O << "\t.toc\n"; |
| 640 | for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I) { |
| 641 | const GlobalVariable *GV = I; |
| 642 | // Do not output labels for unused variables |
| 643 | if (GV->isExternal() && GV->use_begin() == GV->use_end()) |
| 644 | continue; |
| 645 | |
| 646 | std::string Name = GV->getName(); |
| 647 | std::string Label = "LC.." + utostr(LabelNumber++); |
| 648 | GVToLabelMap[GV] = Label; |
| 649 | O << Label << ":\n" |
| 650 | << "\t.tc " << Name << "[TC]," << Name; |
| 651 | if (GV->isExternal()) O << "[RW]"; |
| 652 | O << '\n'; |
| 653 | } |
| 654 | |
| 655 | Mang = new Mangler(M, true); |
| 656 | return false; // success |
| 657 | } |
| 658 | |
| 659 | bool Printer::doFinalization(Module &M) { |
| 660 | const TargetData &TD = TM.getTargetData(); |
| 661 | // Print out module-level global variables |
| 662 | for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I) { |
| 663 | if (I->hasInitializer() || I->hasExternalLinkage()) |
| 664 | continue; |
| 665 | |
| 666 | std::string Name = I->getName(); |
| 667 | if (I->hasInternalLinkage()) { |
| 668 | O << "\t.lcomm " << Name << ",16,_global.bss_c"; |
| 669 | } else { |
| 670 | O << "\t.comm " << Name << "," << TD.getTypeSize(I->getType()) |
| 671 | << "," << log2((unsigned)TD.getTypeAlignment(I->getType())); |
| 672 | } |
| 673 | O << "\t\t# "; |
| 674 | WriteAsOperand(O, I, true, true, &M); |
| 675 | O << "\n"; |
| 676 | } |
| 677 | |
| 678 | O << "_section_.text:\n" |
| 679 | << "\t.csect .data[RW],3\n" |
| 680 | << "\t.llong _section_.text\n"; |
| 681 | |
| 682 | delete Mang; |
| 683 | return false; // success |
| 684 | } |
| 685 | |
| 686 | } // End llvm namespace |