Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1 | //===-- PPC32/Printer.cpp - Convert X86 LLVM code to Intel 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 |
| 11 | // representation of machine-dependent LLVM code to Intel-format |
| 12 | // assembly language. This printer is the output mechanism used |
| 13 | // by `llc' and `lli -print-machineinstrs' on X86. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Misha Brukman | 0579449 | 2004-06-24 17:31:42 +0000 | [diff] [blame] | 17 | #define DEBUG_TYPE "asmprinter" |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 18 | #include "PowerPC.h" |
| 19 | #include "PowerPCInstrInfo.h" |
| 20 | #include "llvm/Constants.h" |
| 21 | #include "llvm/DerivedTypes.h" |
| 22 | #include "llvm/Module.h" |
| 23 | #include "llvm/Assembly/Writer.h" |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineConstantPool.h" |
Misha Brukman | 0579449 | 2004-06-24 17:31:42 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineInstr.h" |
| 27 | #include "llvm/Target/TargetMachine.h" |
| 28 | #include "llvm/Support/Mangler.h" |
Misha Brukman | 0579449 | 2004-06-24 17:31:42 +0000 | [diff] [blame] | 29 | #include "Support/CommandLine.h" |
| 30 | #include "Support/Debug.h" |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 31 | #include "Support/Statistic.h" |
| 32 | #include "Support/StringExtras.h" |
Misha Brukman | 0579449 | 2004-06-24 17:31:42 +0000 | [diff] [blame] | 33 | #include <set> |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 34 | |
| 35 | namespace llvm { |
| 36 | |
| 37 | namespace { |
| 38 | Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed"); |
| 39 | |
| 40 | struct Printer : public MachineFunctionPass { |
| 41 | /// Output stream on which we're printing assembly code. |
| 42 | /// |
| 43 | std::ostream &O; |
| 44 | |
| 45 | /// Target machine description which we query for reg. names, data |
| 46 | /// layout, etc. |
| 47 | /// |
| 48 | TargetMachine &TM; |
| 49 | |
| 50 | /// Name-mangler for global names. |
| 51 | /// |
| 52 | Mangler *Mang; |
Misha Brukman | bb4a908 | 2004-06-28 15:53:27 +0000 | [diff] [blame^] | 53 | std::set<std::string> Stubs; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 54 | std::set<std::string> Strings; |
| 55 | |
| 56 | Printer(std::ostream &o, TargetMachine &tm) : O(o), TM(tm) { } |
| 57 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 58 | /// Cache of mangled name for current function. This is |
| 59 | /// recalculated at the beginning of each call to |
| 60 | /// runOnMachineFunction(). |
| 61 | /// |
| 62 | std::string CurrentFnName; |
| 63 | |
| 64 | virtual const char *getPassName() const { |
| 65 | return "PowerPC Assembly Printer"; |
| 66 | } |
| 67 | |
| 68 | void printMachineInstruction(const MachineInstr *MI); |
Misha Brukman | bb4a908 | 2004-06-28 15:53:27 +0000 | [diff] [blame^] | 69 | void printOp(const MachineOperand &MO, bool elideOffsetKeyword = false); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 70 | void printConstantPool(MachineConstantPool *MCP); |
| 71 | bool runOnMachineFunction(MachineFunction &F); |
| 72 | bool doInitialization(Module &M); |
| 73 | bool doFinalization(Module &M); |
| 74 | void emitGlobalConstant(const Constant* CV); |
| 75 | void emitConstantValueOnly(const Constant *CV); |
| 76 | }; |
| 77 | } // end of anonymous namespace |
| 78 | |
| 79 | /// createPPCCodePrinterPass - Returns a pass that prints the X86 |
| 80 | /// assembly code for a MachineFunction to the given output stream, |
| 81 | /// using the given target machine description. This should work |
| 82 | /// regardless of whether the function is in SSA form. |
| 83 | /// |
Misha Brukman | bb4a908 | 2004-06-28 15:53:27 +0000 | [diff] [blame^] | 84 | FunctionPass *createPPCCodePrinterPass(std::ostream &o,TargetMachine &tm) { |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 85 | return new Printer(o, tm); |
| 86 | } |
| 87 | |
| 88 | /// isStringCompatible - Can we treat the specified array as a string? |
| 89 | /// Only if it is an array of ubytes or non-negative sbytes. |
| 90 | /// |
| 91 | static bool isStringCompatible(const ConstantArray *CVA) { |
| 92 | const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType(); |
| 93 | if (ETy == Type::UByteTy) return true; |
| 94 | if (ETy != Type::SByteTy) return false; |
| 95 | |
| 96 | for (unsigned i = 0; i < CVA->getNumOperands(); ++i) |
| 97 | if (cast<ConstantSInt>(CVA->getOperand(i))->getValue() < 0) |
| 98 | return false; |
| 99 | |
| 100 | return true; |
| 101 | } |
| 102 | |
| 103 | /// toOctal - Convert the low order bits of X into an octal digit. |
| 104 | /// |
| 105 | static inline char toOctal(int X) { |
| 106 | return (X&7)+'0'; |
| 107 | } |
| 108 | |
| 109 | /// getAsCString - Return the specified array as a C compatible |
| 110 | /// string, only if the predicate isStringCompatible is true. |
| 111 | /// |
| 112 | static void printAsCString(std::ostream &O, const ConstantArray *CVA) { |
| 113 | assert(isStringCompatible(CVA) && "Array is not string compatible!"); |
| 114 | |
| 115 | O << "\""; |
| 116 | for (unsigned i = 0; i < CVA->getNumOperands(); ++i) { |
| 117 | unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue(); |
| 118 | |
| 119 | if (C == '"') { |
| 120 | O << "\\\""; |
| 121 | } else if (C == '\\') { |
| 122 | O << "\\\\"; |
| 123 | } else if (isprint(C)) { |
| 124 | O << C; |
| 125 | } else { |
| 126 | switch(C) { |
| 127 | case '\b': O << "\\b"; break; |
| 128 | case '\f': O << "\\f"; break; |
| 129 | case '\n': O << "\\n"; break; |
| 130 | case '\r': O << "\\r"; break; |
| 131 | case '\t': O << "\\t"; break; |
| 132 | default: |
| 133 | O << '\\'; |
| 134 | O << toOctal(C >> 6); |
| 135 | O << toOctal(C >> 3); |
| 136 | O << toOctal(C >> 0); |
| 137 | break; |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | O << "\""; |
| 142 | } |
| 143 | |
| 144 | // Print out the specified constant, without a storage class. Only the |
| 145 | // constants valid in constant expressions can occur here. |
| 146 | void Printer::emitConstantValueOnly(const Constant *CV) { |
| 147 | if (CV->isNullValue()) |
| 148 | O << "0"; |
| 149 | else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) { |
| 150 | assert(CB == ConstantBool::True); |
| 151 | O << "1"; |
| 152 | } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV)) |
| 153 | O << CI->getValue(); |
| 154 | else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV)) |
| 155 | O << CI->getValue(); |
| 156 | else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CV)) |
| 157 | // This is a constant address for a global variable or function. Use the |
| 158 | // name of the variable or function as the address value. |
| 159 | O << Mang->getValueName(CPR->getValue()); |
| 160 | else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) { |
| 161 | const TargetData &TD = TM.getTargetData(); |
| 162 | switch(CE->getOpcode()) { |
| 163 | case Instruction::GetElementPtr: { |
| 164 | // generate a symbolic expression for the byte address |
| 165 | const Constant *ptrVal = CE->getOperand(0); |
| 166 | std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end()); |
| 167 | if (unsigned Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) { |
| 168 | O << "("; |
| 169 | emitConstantValueOnly(ptrVal); |
| 170 | O << ") + " << Offset; |
| 171 | } else { |
| 172 | emitConstantValueOnly(ptrVal); |
| 173 | } |
| 174 | break; |
| 175 | } |
| 176 | case Instruction::Cast: { |
| 177 | // Support only non-converting or widening casts for now, that is, ones |
| 178 | // that do not involve a change in value. This assertion is really gross, |
| 179 | // and may not even be a complete check. |
| 180 | Constant *Op = CE->getOperand(0); |
| 181 | const Type *OpTy = Op->getType(), *Ty = CE->getType(); |
| 182 | |
| 183 | // Remember, kids, pointers on x86 can be losslessly converted back and |
| 184 | // forth into 32-bit or wider integers, regardless of signedness. :-P |
| 185 | assert(((isa<PointerType>(OpTy) |
| 186 | && (Ty == Type::LongTy || Ty == Type::ULongTy |
| 187 | || Ty == Type::IntTy || Ty == Type::UIntTy)) |
| 188 | || (isa<PointerType>(Ty) |
| 189 | && (OpTy == Type::LongTy || OpTy == Type::ULongTy |
| 190 | || OpTy == Type::IntTy || OpTy == Type::UIntTy)) |
| 191 | || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy)) |
| 192 | && OpTy->isLosslesslyConvertibleTo(Ty)))) |
| 193 | && "FIXME: Don't yet support this kind of constant cast expr"); |
| 194 | O << "("; |
| 195 | emitConstantValueOnly(Op); |
| 196 | O << ")"; |
| 197 | break; |
| 198 | } |
| 199 | case Instruction::Add: |
| 200 | O << "("; |
| 201 | emitConstantValueOnly(CE->getOperand(0)); |
| 202 | O << ") + ("; |
| 203 | emitConstantValueOnly(CE->getOperand(1)); |
| 204 | O << ")"; |
| 205 | break; |
| 206 | default: |
| 207 | assert(0 && "Unsupported operator!"); |
| 208 | } |
| 209 | } else { |
| 210 | assert(0 && "Unknown constant value!"); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | // Print a constant value or values, with the appropriate storage class as a |
| 215 | // prefix. |
| 216 | void Printer::emitGlobalConstant(const Constant *CV) { |
| 217 | const TargetData &TD = TM.getTargetData(); |
| 218 | |
| 219 | if (CV->isNullValue()) { |
| 220 | O << "\t.space\t " << TD.getTypeSize(CV->getType()) << "\n"; |
| 221 | return; |
| 222 | } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) { |
| 223 | if (isStringCompatible(CVA)) { |
| 224 | O << ".ascii"; |
| 225 | printAsCString(O, CVA); |
| 226 | O << "\n"; |
| 227 | } else { // Not a string. Print the values in successive locations |
| 228 | const std::vector<Use> &constValues = CVA->getValues(); |
| 229 | for (unsigned i=0; i < constValues.size(); i++) |
| 230 | emitGlobalConstant(cast<Constant>(constValues[i].get())); |
| 231 | } |
| 232 | return; |
| 233 | } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) { |
| 234 | // Print the fields in successive locations. Pad to align if needed! |
| 235 | const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType()); |
| 236 | const std::vector<Use>& constValues = CVS->getValues(); |
| 237 | unsigned sizeSoFar = 0; |
| 238 | for (unsigned i=0, N = constValues.size(); i < N; i++) { |
| 239 | const Constant* field = cast<Constant>(constValues[i].get()); |
| 240 | |
| 241 | // Check if padding is needed and insert one or more 0s. |
| 242 | unsigned fieldSize = TD.getTypeSize(field->getType()); |
| 243 | unsigned padSize = ((i == N-1? cvsLayout->StructSize |
| 244 | : cvsLayout->MemberOffsets[i+1]) |
| 245 | - cvsLayout->MemberOffsets[i]) - fieldSize; |
| 246 | sizeSoFar += fieldSize + padSize; |
| 247 | |
| 248 | // Now print the actual field value |
| 249 | emitGlobalConstant(field); |
| 250 | |
| 251 | // Insert the field padding unless it's zero bytes... |
| 252 | if (padSize) |
| 253 | O << "\t.space\t " << padSize << "\n"; |
| 254 | } |
| 255 | assert(sizeSoFar == cvsLayout->StructSize && |
| 256 | "Layout of constant struct may be incorrect!"); |
| 257 | return; |
| 258 | } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) { |
| 259 | // FP Constants are printed as integer constants to avoid losing |
| 260 | // precision... |
| 261 | double Val = CFP->getValue(); |
Misha Brukman | d71bd56 | 2004-06-21 17:19:08 +0000 | [diff] [blame] | 262 | switch (CFP->getType()->getTypeID()) { |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 263 | default: assert(0 && "Unknown floating point type!"); |
| 264 | case Type::FloatTyID: { |
| 265 | union FU { // Abide by C TBAA rules |
| 266 | float FVal; |
| 267 | unsigned UVal; |
| 268 | } U; |
| 269 | U.FVal = Val; |
| 270 | O << ".long\t" << U.UVal << "\t# float " << Val << "\n"; |
| 271 | return; |
| 272 | } |
| 273 | case Type::DoubleTyID: { |
| 274 | union DU { // Abide by C TBAA rules |
| 275 | double FVal; |
| 276 | uint64_t UVal; |
| 277 | struct { |
Misha Brukman | 46fd00a | 2004-06-24 23:04:11 +0000 | [diff] [blame] | 278 | uint32_t MSWord; |
| 279 | uint32_t LSWord; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 280 | } T; |
| 281 | } U; |
| 282 | U.FVal = Val; |
| 283 | |
Misha Brukman | 46fd00a | 2004-06-24 23:04:11 +0000 | [diff] [blame] | 284 | O << ".long\t" << U.T.MSWord << "\t# double most significant word " |
| 285 | << Val << "\n"; |
| 286 | O << ".long\t" << U.T.LSWord << "\t# double least significant word" |
| 287 | << Val << "\n"; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 288 | return; |
| 289 | } |
| 290 | } |
| 291 | } else if (CV->getType()->getPrimitiveSize() == 64) { |
Misha Brukman | 2bf183c | 2004-06-25 15:42:10 +0000 | [diff] [blame] | 292 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) { |
| 293 | union DU { // Abide by C TBAA rules |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 294 | int64_t UVal; |
| 295 | struct { |
Misha Brukman | 46fd00a | 2004-06-24 23:04:11 +0000 | [diff] [blame] | 296 | uint32_t MSWord; |
| 297 | uint32_t LSWord; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 298 | } T; |
| 299 | } U; |
| 300 | U.UVal = CI->getRawValue(); |
| 301 | |
Misha Brukman | 46fd00a | 2004-06-24 23:04:11 +0000 | [diff] [blame] | 302 | O << ".long\t" << U.T.MSWord << "\t# Double-word most significant word " |
| 303 | << U.UVal << "\n"; |
| 304 | O << ".long\t" << U.T.LSWord << "\t# Double-word least significant word" |
| 305 | << U.UVal << "\n"; |
| 306 | return; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | |
| 310 | const Type *type = CV->getType(); |
| 311 | O << "\t"; |
Misha Brukman | d71bd56 | 2004-06-21 17:19:08 +0000 | [diff] [blame] | 312 | switch (type->getTypeID()) { |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 313 | case Type::UByteTyID: case Type::SByteTyID: |
| 314 | O << ".byte"; |
| 315 | break; |
| 316 | case Type::UShortTyID: case Type::ShortTyID: |
| 317 | O << ".short"; |
| 318 | break; |
| 319 | case Type::BoolTyID: |
| 320 | case Type::PointerTyID: |
| 321 | case Type::UIntTyID: case Type::IntTyID: |
| 322 | O << ".long"; |
| 323 | break; |
| 324 | case Type::ULongTyID: case Type::LongTyID: |
Misha Brukman | 46fd00a | 2004-06-24 23:04:11 +0000 | [diff] [blame] | 325 | assert (0 && "Should have already output double-word constant."); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 326 | case Type::FloatTyID: case Type::DoubleTyID: |
| 327 | assert (0 && "Should have already output floating point constant."); |
| 328 | default: |
| 329 | assert (0 && "Can't handle printing this type of thing"); |
| 330 | break; |
| 331 | } |
| 332 | O << "\t"; |
| 333 | emitConstantValueOnly(CV); |
| 334 | O << "\n"; |
| 335 | } |
| 336 | |
| 337 | /// printConstantPool - Print to the current output stream assembly |
| 338 | /// representations of the constants in the constant pool MCP. This is |
| 339 | /// used to print out constants which have been "spilled to memory" by |
| 340 | /// the code generator. |
| 341 | /// |
| 342 | void Printer::printConstantPool(MachineConstantPool *MCP) { |
| 343 | const std::vector<Constant*> &CP = MCP->getConstants(); |
| 344 | const TargetData &TD = TM.getTargetData(); |
| 345 | |
| 346 | if (CP.empty()) return; |
| 347 | |
| 348 | for (unsigned i = 0, e = CP.size(); i != e; ++i) { |
| 349 | O << "\t.const\n"; |
| 350 | O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType()) |
| 351 | << "\n"; |
| 352 | O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t#" |
| 353 | << *CP[i] << "\n"; |
| 354 | emitGlobalConstant(CP[i]); |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | /// runOnMachineFunction - This uses the printMachineInstruction() |
| 359 | /// method to print assembly for each instruction. |
| 360 | /// |
| 361 | bool Printer::runOnMachineFunction(MachineFunction &MF) { |
| 362 | // BBNumber is used here so that a given Printer will never give two |
| 363 | // BBs the same name. (If you have a better way, please let me know!) |
| 364 | static unsigned BBNumber = 0; |
| 365 | |
| 366 | O << "\n\n"; |
| 367 | // What's my mangled name? |
| 368 | CurrentFnName = Mang->getValueName(MF.getFunction()); |
| 369 | |
| 370 | // Print out constants referenced by the function |
| 371 | printConstantPool(MF.getConstantPool()); |
| 372 | |
| 373 | // Print out labels for the function. |
| 374 | O << "\t.text\n"; |
| 375 | O << "\t.globl\t" << CurrentFnName << "\n"; |
| 376 | O << "\t.align 5\n"; |
| 377 | O << CurrentFnName << ":\n"; |
| 378 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 379 | // Print out code for the function. |
| 380 | for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); |
| 381 | I != E; ++I) { |
| 382 | // Print a label for the basic block. |
Misha Brukman | 2bf183c | 2004-06-25 15:42:10 +0000 | [diff] [blame] | 383 | O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t# " |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 384 | << I->getBasicBlock()->getName() << "\n"; |
| 385 | for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end(); |
Misha Brukman | 46fd00a | 2004-06-24 23:04:11 +0000 | [diff] [blame] | 386 | II != E; ++II) { |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 387 | // Print the assembly for the instruction. |
| 388 | O << "\t"; |
| 389 | printMachineInstruction(II); |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | // We didn't modify anything. |
| 394 | return false; |
| 395 | } |
| 396 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 397 | void Printer::printOp(const MachineOperand &MO, |
Misha Brukman | 46fd00a | 2004-06-24 23:04:11 +0000 | [diff] [blame] | 398 | bool elideOffsetKeyword /* = false */) { |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 399 | const MRegisterInfo &RI = *TM.getRegisterInfo(); |
| 400 | int new_symbol; |
| 401 | |
| 402 | switch (MO.getType()) { |
| 403 | case MachineOperand::MO_VirtualRegister: |
| 404 | if (Value *V = MO.getVRegValueOrNull()) { |
| 405 | O << "<" << V->getName() << ">"; |
| 406 | return; |
| 407 | } |
| 408 | // FALLTHROUGH |
| 409 | case MachineOperand::MO_MachineRegister: |
Misha Brukman | 7f484a5 | 2004-06-24 23:51:00 +0000 | [diff] [blame] | 410 | O << LowercaseString(RI.get(MO.getReg()).Name); |
| 411 | return; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 412 | |
| 413 | case MachineOperand::MO_SignExtendedImmed: |
| 414 | case MachineOperand::MO_UnextendedImmed: |
| 415 | O << (int)MO.getImmedValue(); |
| 416 | return; |
| 417 | case MachineOperand::MO_MachineBasicBlock: { |
| 418 | MachineBasicBlock *MBBOp = MO.getMachineBasicBlock(); |
| 419 | O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction()) |
Misha Brukman | 2bf183c | 2004-06-25 15:42:10 +0000 | [diff] [blame] | 420 | << "_" << MBBOp->getNumber() << "\t# " |
| 421 | << MBBOp->getBasicBlock()->getName(); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 422 | return; |
| 423 | } |
| 424 | case MachineOperand::MO_PCRelativeDisp: |
| 425 | std::cerr << "Shouldn't use addPCDisp() when building PPC MachineInstrs"; |
| 426 | abort (); |
| 427 | return; |
| 428 | case MachineOperand::MO_GlobalAddress: |
| 429 | if (!elideOffsetKeyword) { |
Misha Brukman | bb4a908 | 2004-06-28 15:53:27 +0000 | [diff] [blame^] | 430 | if (isa<Function>(MO.getGlobal())) { |
Misha Brukman | 46fd00a | 2004-06-24 23:04:11 +0000 | [diff] [blame] | 431 | Stubs.insert(Mang->getValueName(MO.getGlobal())); |
| 432 | O << "L" << Mang->getValueName(MO.getGlobal()) << "$stub"; |
| 433 | } else { |
| 434 | O << Mang->getValueName(MO.getGlobal()); |
| 435 | } |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 436 | } |
| 437 | return; |
| 438 | case MachineOperand::MO_ExternalSymbol: |
| 439 | O << MO.getSymbolName(); |
| 440 | return; |
| 441 | default: |
Misha Brukman | 22e1207 | 2004-06-25 15:11:34 +0000 | [diff] [blame] | 442 | O << "<unknown operand type>"; |
| 443 | return; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 444 | } |
| 445 | } |
| 446 | |
| 447 | #if 0 |
| 448 | static inline |
| 449 | unsigned int ValidOpcodes(const MachineInstr *MI, unsigned int ArgType[5]) { |
Misha Brukman | 46fd00a | 2004-06-24 23:04:11 +0000 | [diff] [blame] | 450 | int i; |
| 451 | unsigned int retval = 1; |
| 452 | |
| 453 | for(i = 0; i<5; i++) { |
| 454 | switch(ArgType[i]) { |
| 455 | case none: |
| 456 | break; |
| 457 | case Gpr: |
| 458 | case Gpr0: |
| 459 | Type::UIntTy |
| 460 | case Simm16: |
| 461 | case Zimm16: |
| 462 | case PCRelimm24: |
| 463 | case Imm24: |
| 464 | case Imm5: |
| 465 | case PCRelimm14: |
| 466 | case Imm14: |
| 467 | case Imm2: |
| 468 | case Crf: |
| 469 | case Imm3: |
| 470 | case Imm1: |
| 471 | case Fpr: |
| 472 | case Imm4: |
| 473 | case Imm8: |
| 474 | case Disimm16: |
| 475 | case Spr: |
| 476 | case Sgr: |
| 477 | }; |
| 478 | |
| 479 | } |
| 480 | } |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 481 | } |
| 482 | #endif |
| 483 | |
| 484 | /// printMachineInstruction -- Print out a single PPC32 LLVM instruction |
| 485 | /// MI in Darwin syntax to the current output stream. |
| 486 | /// |
| 487 | void Printer::printMachineInstruction(const MachineInstr *MI) { |
| 488 | unsigned Opcode = MI->getOpcode(); |
| 489 | const TargetInstrInfo &TII = *TM.getInstrInfo(); |
| 490 | const TargetInstrDescriptor &Desc = TII.get(Opcode); |
| 491 | unsigned int i; |
Misha Brukman | c6cc10f | 2004-06-25 19:24:52 +0000 | [diff] [blame] | 492 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 493 | unsigned int ArgCount = Desc.TSFlags & PPC32II::ArgCountMask; |
Misha Brukman | 22e1207 | 2004-06-25 15:11:34 +0000 | [diff] [blame] | 494 | unsigned int ArgType[] = { |
| 495 | (Desc.TSFlags >> PPC32II::Arg0TypeShift) & PPC32II::ArgTypeMask, |
| 496 | (Desc.TSFlags >> PPC32II::Arg1TypeShift) & PPC32II::ArgTypeMask, |
| 497 | (Desc.TSFlags >> PPC32II::Arg2TypeShift) & PPC32II::ArgTypeMask, |
| 498 | (Desc.TSFlags >> PPC32II::Arg3TypeShift) & PPC32II::ArgTypeMask, |
| 499 | (Desc.TSFlags >> PPC32II::Arg4TypeShift) & PPC32II::ArgTypeMask |
| 500 | }; |
Misha Brukman | 7f484a5 | 2004-06-24 23:51:00 +0000 | [diff] [blame] | 501 | assert(((Desc.TSFlags & PPC32II::VMX) == 0) && |
Misha Brukman | 46fd00a | 2004-06-24 23:04:11 +0000 | [diff] [blame] | 502 | "Instruction requires VMX support"); |
Misha Brukman | 7f484a5 | 2004-06-24 23:51:00 +0000 | [diff] [blame] | 503 | assert(((Desc.TSFlags & PPC32II::PPC64) == 0) && |
Misha Brukman | 46fd00a | 2004-06-24 23:04:11 +0000 | [diff] [blame] | 504 | "Instruction requires 64 bit support"); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 505 | //assert ( ValidOpcodes(MI, ArgType) && "Instruction has invalid inputs"); |
| 506 | ++EmittedInsts; |
| 507 | |
Misha Brukman | 46fd00a | 2004-06-24 23:04:11 +0000 | [diff] [blame] | 508 | if (Opcode == PPC32::MovePCtoLR) { |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 509 | O << "mflr r0\n"; |
Misha Brukman | c6cc10f | 2004-06-25 19:24:52 +0000 | [diff] [blame] | 510 | O << "\tbcl 20,31,L" << CurrentFnName << "$pb\n"; |
Misha Brukman | 2bf183c | 2004-06-25 15:42:10 +0000 | [diff] [blame] | 511 | O << "L" << CurrentFnName << "$pb:\n"; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 512 | return; |
| 513 | } |
| 514 | |
| 515 | O << TII.getName(MI->getOpcode()) << " "; |
Misha Brukman | 0579449 | 2004-06-24 17:31:42 +0000 | [diff] [blame] | 516 | DEBUG(std::cerr << TII.getName(MI->getOpcode()) << " expects " |
| 517 | << ArgCount << " args\n"); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 518 | |
Misha Brukman | 46fd00a | 2004-06-24 23:04:11 +0000 | [diff] [blame] | 519 | if (Opcode == PPC32::LOADLoAddr) { |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 520 | printOp(MI->getOperand(0)); |
| 521 | O << ", "; |
| 522 | printOp(MI->getOperand(1)); |
| 523 | O << ", lo16("; |
| 524 | printOp(MI->getOperand(2)); |
| 525 | O << "-L" << CurrentFnName << "$pb)\n"; |
Misha Brukman | c6cc10f | 2004-06-25 19:24:52 +0000 | [diff] [blame] | 526 | } else if (Opcode == PPC32::LOADHiAddr) { |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 527 | printOp(MI->getOperand(0)); |
| 528 | O << ", "; |
| 529 | printOp(MI->getOperand(1)); |
| 530 | O << ", ha16(" ; |
| 531 | printOp(MI->getOperand(2)); |
| 532 | O << "-L" << CurrentFnName << "$pb)\n"; |
Misha Brukman | c6cc10f | 2004-06-25 19:24:52 +0000 | [diff] [blame] | 533 | } else if (ArgCount == 3 && ArgType[1] == PPC32II::Disimm16) { |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 534 | printOp(MI->getOperand(0)); |
| 535 | O << ", "; |
| 536 | printOp(MI->getOperand(1)); |
| 537 | O << "("; |
Misha Brukman | 46fd00a | 2004-06-24 23:04:11 +0000 | [diff] [blame] | 538 | if (ArgType[2] == PPC32II::Gpr0 && MI->getOperand(2).getReg() == PPC32::R0) |
| 539 | O << "0"; |
| 540 | else |
| 541 | printOp(MI->getOperand(2)); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 542 | O << ")\n"; |
| 543 | } else { |
Misha Brukman | 7f484a5 | 2004-06-24 23:51:00 +0000 | [diff] [blame] | 544 | for (i = 0; i < ArgCount; ++i) { |
Misha Brukman | 46fd00a | 2004-06-24 23:04:11 +0000 | [diff] [blame] | 545 | if (ArgType[i] == PPC32II::Gpr0 && |
| 546 | MI->getOperand(i).getReg() == PPC32::R0) |
| 547 | O << "0"; |
| 548 | else { |
| 549 | //std::cout << "DEBUG " << (*(TM.getRegisterInfo())).get(MI->getOperand(i).getReg()).Name << "\n"; |
| 550 | printOp(MI->getOperand(i)); |
| 551 | } |
Misha Brukman | 7f484a5 | 2004-06-24 23:51:00 +0000 | [diff] [blame] | 552 | if (ArgCount - 1 == i) |
Misha Brukman | 46fd00a | 2004-06-24 23:04:11 +0000 | [diff] [blame] | 553 | O << "\n"; |
| 554 | else |
| 555 | O << ", "; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 556 | } |
| 557 | } |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | bool Printer::doInitialization(Module &M) { |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 561 | Mang = new Mangler(M, true); |
| 562 | return false; // success |
| 563 | } |
| 564 | |
| 565 | // SwitchSection - Switch to the specified section of the executable if we are |
| 566 | // not already in it! |
| 567 | // |
| 568 | static void SwitchSection(std::ostream &OS, std::string &CurSection, |
| 569 | const char *NewSection) { |
| 570 | if (CurSection != NewSection) { |
| 571 | CurSection = NewSection; |
| 572 | if (!CurSection.empty()) |
| 573 | OS << "\t" << NewSection << "\n"; |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | bool Printer::doFinalization(Module &M) { |
| 578 | const TargetData &TD = TM.getTargetData(); |
| 579 | std::string CurSection; |
| 580 | |
| 581 | // Print out module-level global variables here. |
| 582 | for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I) |
| 583 | if (I->hasInitializer()) { // External global require no code |
| 584 | O << "\n\n"; |
| 585 | std::string name = Mang->getValueName(I); |
| 586 | Constant *C = I->getInitializer(); |
| 587 | unsigned Size = TD.getTypeSize(C->getType()); |
| 588 | unsigned Align = TD.getTypeAlignment(C->getType()); |
| 589 | |
| 590 | if (C->isNullValue() && |
| 591 | (I->hasLinkOnceLinkage() || I->hasInternalLinkage() || |
| 592 | I->hasWeakLinkage() /* FIXME: Verify correct */)) { |
| 593 | SwitchSection(O, CurSection, ".data"); |
| 594 | if (I->hasInternalLinkage()) |
| 595 | O << "\t.local " << name << "\n"; |
| 596 | |
| 597 | O << "\t.comm " << name << "," << TD.getTypeSize(C->getType()) |
| 598 | << "," << (unsigned)TD.getTypeAlignment(C->getType()); |
| 599 | O << "\t\t# "; |
| 600 | WriteAsOperand(O, I, true, true, &M); |
| 601 | O << "\n"; |
| 602 | } else { |
| 603 | switch (I->getLinkage()) { |
| 604 | case GlobalValue::LinkOnceLinkage: |
| 605 | case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak. |
| 606 | // Nonnull linkonce -> weak |
| 607 | O << "\t.weak " << name << "\n"; |
| 608 | SwitchSection(O, CurSection, ""); |
| 609 | O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n"; |
| 610 | break; |
| 611 | |
| 612 | case GlobalValue::AppendingLinkage: |
| 613 | // FIXME: appending linkage variables should go into a section of |
| 614 | // their name or something. For now, just emit them as external. |
| 615 | case GlobalValue::ExternalLinkage: |
| 616 | // If external or appending, declare as a global symbol |
| 617 | O << "\t.globl " << name << "\n"; |
| 618 | // FALL THROUGH |
| 619 | case GlobalValue::InternalLinkage: |
| 620 | if (C->isNullValue()) |
| 621 | SwitchSection(O, CurSection, ".bss"); |
| 622 | else |
| 623 | SwitchSection(O, CurSection, ".data"); |
| 624 | break; |
| 625 | } |
| 626 | |
| 627 | O << "\t.align " << Align << "\n"; |
| 628 | O << name << ":\t\t\t\t# "; |
| 629 | WriteAsOperand(O, I, true, true, &M); |
| 630 | O << " = "; |
| 631 | WriteAsOperand(O, C, false, false, &M); |
| 632 | O << "\n"; |
| 633 | emitGlobalConstant(C); |
| 634 | } |
| 635 | } |
| 636 | |
Misha Brukman | 46fd00a | 2004-06-24 23:04:11 +0000 | [diff] [blame] | 637 | for(std::set<std::string>::iterator i = Stubs.begin(); i != Stubs.end(); ++i) |
| 638 | { |
| 639 | O << ".data\n"; |
| 640 | O<<".section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32\n"; |
| 641 | O << "\t.align 2\n"; |
| 642 | O << "L" << *i << "$stub:\n"; |
| 643 | O << "\t.indirect_symbol " << *i << "\n"; |
| 644 | O << "\tmflr r0\n"; |
| 645 | O << "\tbcl 20,31,L0$" << *i << "\n"; |
| 646 | O << "L0$" << *i << ":\n"; |
| 647 | O << "\tmflr r11\n"; |
| 648 | O << "\taddis r11,r11,ha16(L" << *i << "$lazy_ptr-L0$" << *i << ")\n"; |
| 649 | O << "\tmtlr r0\n"; |
| 650 | O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr-L0$" << *i << ")(r11)\n"; |
| 651 | O << "\tmtctr r12\n"; |
| 652 | O << "\tbctr\n"; |
| 653 | O << ".data\n"; |
| 654 | O << ".lazy_symbol_pointer\n"; |
| 655 | O << "L" << *i << "$lazy_ptr:\n"; |
| 656 | O << ".indirect_symbol " << *i << "\n"; |
| 657 | O << ".long dyld_stub_binding_helper\n"; |
| 658 | } |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 659 | |
| 660 | delete Mang; |
| 661 | return false; // success |
| 662 | } |
| 663 | |
| 664 | } // End llvm namespace |