Misha Brukman | 05fcd0c | 2004-07-08 17:58:04 +0000 | [diff] [blame] | 1 | //===-- Printer.cpp - Convert LLVM code to PowerPC assembly ---------------===// |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 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 | // |
Misha Brukman | 05fcd0c | 2004-07-08 17:58:04 +0000 | [diff] [blame] | 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' and `lli -print-machineinstrs'. |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 13 | // |
Misha Brukman | 05fcd0c | 2004-07-08 17:58:04 +0000 | [diff] [blame] | 14 | // Documentation at http://developer.apple.com/documentation/DeveloperTools/ |
| 15 | // Reference/Assembler/ASMIntroduction/chapter_1_section_1.html |
Misha Brukman | 218bec7 | 2004-06-29 17:13:26 +0000 | [diff] [blame] | 16 | // |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 17 | //===----------------------------------------------------------------------===// |
| 18 | |
Misha Brukman | 0579449 | 2004-06-24 17:31:42 +0000 | [diff] [blame] | 19 | #define DEBUG_TYPE "asmprinter" |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 20 | #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 Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineConstantPool.h" |
Misha Brukman | 0579449 | 2004-06-24 17:31:42 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineInstr.h" |
| 29 | #include "llvm/Target/TargetMachine.h" |
| 30 | #include "llvm/Support/Mangler.h" |
Misha Brukman | 0579449 | 2004-06-24 17:31:42 +0000 | [diff] [blame] | 31 | #include "Support/CommandLine.h" |
| 32 | #include "Support/Debug.h" |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 33 | #include "Support/Statistic.h" |
| 34 | #include "Support/StringExtras.h" |
Misha Brukman | 0579449 | 2004-06-24 17:31:42 +0000 | [diff] [blame] | 35 | #include <set> |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 36 | |
| 37 | namespace llvm { |
| 38 | |
| 39 | namespace { |
| 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 Brukman | 97a296f | 2004-07-21 20:11:11 +0000 | [diff] [blame^] | 55 | std::set<std::string> FnStubs, GVStubs, LinkOnceStubs; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 56 | std::set<std::string> Strings; |
| 57 | |
Misha Brukman | 218bec7 | 2004-06-29 17:13:26 +0000 | [diff] [blame] | 58 | Printer(std::ostream &o, TargetMachine &tm) : O(o), TM(tm), labelNumber(0) |
| 59 | { } |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 60 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 61 | /// 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 Brukman | 218bec7 | 2004-06-29 17:13:26 +0000 | [diff] [blame] | 67 | /// Unique incrementer for label values for referencing |
| 68 | /// Global values. |
| 69 | /// |
| 70 | unsigned int labelNumber; |
| 71 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 72 | virtual const char *getPassName() const { |
| 73 | return "PowerPC Assembly Printer"; |
| 74 | } |
| 75 | |
| 76 | void printMachineInstruction(const MachineInstr *MI); |
Misha Brukman | bb4a908 | 2004-06-28 15:53:27 +0000 | [diff] [blame] | 77 | void printOp(const MachineOperand &MO, bool elideOffsetKeyword = false); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 78 | 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 Brukman | 218bec7 | 2004-06-29 17:13:26 +0000 | [diff] [blame] | 87 | /// createPPCCodePrinterPass - Returns a pass that prints the PPC |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 88 | /// 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 Brukman | bb4a908 | 2004-06-28 15:53:27 +0000 | [diff] [blame] | 92 | FunctionPass *createPPCCodePrinterPass(std::ostream &o,TargetMachine &tm) { |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 93 | 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 | /// |
| 99 | static 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 | /// |
| 113 | static 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 | /// |
| 120 | static 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. |
| 154 | void 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(); |
Chris Lattner | 67910e1 | 2004-07-18 07:29:35 +0000 | [diff] [blame] | 164 | else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 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. |
Chris Lattner | 67910e1 | 2004-07-18 07:29:35 +0000 | [diff] [blame] | 167 | O << Mang->getValueName(GV); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 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. |
| 224 | void Printer::emitGlobalConstant(const Constant *CV) { |
| 225 | const TargetData &TD = TM.getTargetData(); |
| 226 | |
Misha Brukman | e48178e | 2004-07-20 15:45:27 +0000 | [diff] [blame] | 227 | if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) { |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 228 | if (isStringCompatible(CVA)) { |
Misha Brukman | 218bec7 | 2004-06-29 17:13:26 +0000 | [diff] [blame] | 229 | O << "\t.ascii "; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 230 | printAsCString(O, CVA); |
| 231 | O << "\n"; |
| 232 | } else { // Not a string. Print the values in successive locations |
| 233 | const std::vector<Use> &constValues = CVA->getValues(); |
| 234 | for (unsigned i=0; i < constValues.size(); i++) |
| 235 | emitGlobalConstant(cast<Constant>(constValues[i].get())); |
| 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 | const std::vector<Use>& constValues = CVS->getValues(); |
| 242 | unsigned sizeSoFar = 0; |
| 243 | for (unsigned i=0, N = constValues.size(); i < N; i++) { |
| 244 | const Constant* field = cast<Constant>(constValues[i].get()); |
| 245 | |
| 246 | // Check if padding is needed and insert one or more 0s. |
| 247 | unsigned fieldSize = TD.getTypeSize(field->getType()); |
| 248 | unsigned padSize = ((i == N-1? cvsLayout->StructSize |
| 249 | : cvsLayout->MemberOffsets[i+1]) |
| 250 | - cvsLayout->MemberOffsets[i]) - fieldSize; |
| 251 | sizeSoFar += fieldSize + padSize; |
| 252 | |
| 253 | // Now print the actual field value |
| 254 | emitGlobalConstant(field); |
| 255 | |
| 256 | // Insert the field padding unless it's zero bytes... |
| 257 | if (padSize) |
| 258 | O << "\t.space\t " << padSize << "\n"; |
| 259 | } |
| 260 | assert(sizeSoFar == cvsLayout->StructSize && |
| 261 | "Layout of constant struct may be incorrect!"); |
| 262 | return; |
| 263 | } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) { |
| 264 | // FP Constants are printed as integer constants to avoid losing |
| 265 | // precision... |
| 266 | double Val = CFP->getValue(); |
Misha Brukman | d71bd56 | 2004-06-21 17:19:08 +0000 | [diff] [blame] | 267 | switch (CFP->getType()->getTypeID()) { |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 268 | default: assert(0 && "Unknown floating point type!"); |
| 269 | case Type::FloatTyID: { |
| 270 | union FU { // Abide by C TBAA rules |
| 271 | float FVal; |
| 272 | unsigned UVal; |
| 273 | } U; |
| 274 | U.FVal = Val; |
Misha Brukman | 218bec7 | 2004-06-29 17:13:26 +0000 | [diff] [blame] | 275 | O << ".long\t" << U.UVal << "\t; float " << Val << "\n"; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 276 | return; |
| 277 | } |
| 278 | case Type::DoubleTyID: { |
| 279 | union DU { // Abide by C TBAA rules |
| 280 | double FVal; |
| 281 | uint64_t UVal; |
| 282 | struct { |
Misha Brukman | 46fd00a | 2004-06-24 23:04:11 +0000 | [diff] [blame] | 283 | uint32_t MSWord; |
| 284 | uint32_t LSWord; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 285 | } T; |
| 286 | } U; |
| 287 | U.FVal = Val; |
| 288 | |
Misha Brukman | 218bec7 | 2004-06-29 17:13:26 +0000 | [diff] [blame] | 289 | O << ".long\t" << U.T.MSWord << "\t; double most significant word " |
Misha Brukman | 46fd00a | 2004-06-24 23:04:11 +0000 | [diff] [blame] | 290 | << Val << "\n"; |
Misha Brukman | 29188c6 | 2004-07-16 19:01:13 +0000 | [diff] [blame] | 291 | O << ".long\t" << U.T.LSWord << "\t; double least significant word " |
Misha Brukman | 46fd00a | 2004-06-24 23:04:11 +0000 | [diff] [blame] | 292 | << Val << "\n"; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 293 | return; |
| 294 | } |
| 295 | } |
| 296 | } else if (CV->getType()->getPrimitiveSize() == 64) { |
Misha Brukman | 2bf183c | 2004-06-25 15:42:10 +0000 | [diff] [blame] | 297 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) { |
| 298 | union DU { // Abide by C TBAA rules |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 299 | int64_t UVal; |
| 300 | struct { |
Misha Brukman | 46fd00a | 2004-06-24 23:04:11 +0000 | [diff] [blame] | 301 | uint32_t MSWord; |
| 302 | uint32_t LSWord; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 303 | } T; |
| 304 | } U; |
| 305 | U.UVal = CI->getRawValue(); |
| 306 | |
Misha Brukman | 218bec7 | 2004-06-29 17:13:26 +0000 | [diff] [blame] | 307 | O << ".long\t" << U.T.MSWord << "\t; Double-word most significant word " |
Misha Brukman | 46fd00a | 2004-06-24 23:04:11 +0000 | [diff] [blame] | 308 | << U.UVal << "\n"; |
Misha Brukman | 29188c6 | 2004-07-16 19:01:13 +0000 | [diff] [blame] | 309 | O << ".long\t" << U.T.LSWord << "\t; Double-word least significant word " |
Misha Brukman | 46fd00a | 2004-06-24 23:04:11 +0000 | [diff] [blame] | 310 | << U.UVal << "\n"; |
| 311 | return; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | |
| 315 | const Type *type = CV->getType(); |
| 316 | O << "\t"; |
Misha Brukman | d71bd56 | 2004-06-21 17:19:08 +0000 | [diff] [blame] | 317 | switch (type->getTypeID()) { |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 318 | case Type::UByteTyID: case Type::SByteTyID: |
| 319 | O << ".byte"; |
| 320 | break; |
| 321 | case Type::UShortTyID: case Type::ShortTyID: |
| 322 | O << ".short"; |
| 323 | break; |
| 324 | case Type::BoolTyID: |
| 325 | case Type::PointerTyID: |
| 326 | case Type::UIntTyID: case Type::IntTyID: |
| 327 | O << ".long"; |
| 328 | break; |
| 329 | case Type::ULongTyID: case Type::LongTyID: |
Misha Brukman | 46fd00a | 2004-06-24 23:04:11 +0000 | [diff] [blame] | 330 | assert (0 && "Should have already output double-word constant."); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 331 | case Type::FloatTyID: case Type::DoubleTyID: |
| 332 | assert (0 && "Should have already output floating point constant."); |
| 333 | default: |
Misha Brukman | 97a296f | 2004-07-21 20:11:11 +0000 | [diff] [blame^] | 334 | if (CV == Constant::getNullValue(type)) { // Zero initializer? |
| 335 | O << ".space\t" << TD.getTypeSize(type) << "\n"; |
| 336 | return; |
| 337 | } |
| 338 | std::cerr << "Can't handle printing: " << *CV; |
| 339 | abort(); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 340 | break; |
| 341 | } |
| 342 | O << "\t"; |
| 343 | emitConstantValueOnly(CV); |
| 344 | O << "\n"; |
| 345 | } |
| 346 | |
| 347 | /// printConstantPool - Print to the current output stream assembly |
| 348 | /// representations of the constants in the constant pool MCP. This is |
| 349 | /// used to print out constants which have been "spilled to memory" by |
| 350 | /// the code generator. |
| 351 | /// |
| 352 | void Printer::printConstantPool(MachineConstantPool *MCP) { |
| 353 | const std::vector<Constant*> &CP = MCP->getConstants(); |
| 354 | const TargetData &TD = TM.getTargetData(); |
| 355 | |
| 356 | if (CP.empty()) return; |
| 357 | |
| 358 | for (unsigned i = 0, e = CP.size(); i != e; ++i) { |
| 359 | O << "\t.const\n"; |
| 360 | O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType()) |
| 361 | << "\n"; |
Misha Brukman | 218bec7 | 2004-06-29 17:13:26 +0000 | [diff] [blame] | 362 | O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t;" |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 363 | << *CP[i] << "\n"; |
| 364 | emitGlobalConstant(CP[i]); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | /// runOnMachineFunction - This uses the printMachineInstruction() |
| 369 | /// method to print assembly for each instruction. |
| 370 | /// |
| 371 | bool Printer::runOnMachineFunction(MachineFunction &MF) { |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 372 | O << "\n\n"; |
| 373 | // What's my mangled name? |
| 374 | CurrentFnName = Mang->getValueName(MF.getFunction()); |
| 375 | |
| 376 | // Print out constants referenced by the function |
| 377 | printConstantPool(MF.getConstantPool()); |
| 378 | |
| 379 | // Print out labels for the function. |
| 380 | O << "\t.text\n"; |
| 381 | O << "\t.globl\t" << CurrentFnName << "\n"; |
Misha Brukman | 61297ee | 2004-06-29 23:40:57 +0000 | [diff] [blame] | 382 | O << "\t.align 2\n"; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 383 | O << CurrentFnName << ":\n"; |
| 384 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 385 | // Print out code for the function. |
| 386 | for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); |
| 387 | I != E; ++I) { |
| 388 | // Print a label for the basic block. |
Misha Brukman | 218bec7 | 2004-06-29 17:13:26 +0000 | [diff] [blame] | 389 | O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t; " |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 390 | << I->getBasicBlock()->getName() << "\n"; |
| 391 | for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end(); |
Misha Brukman | 46fd00a | 2004-06-24 23:04:11 +0000 | [diff] [blame] | 392 | II != E; ++II) { |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 393 | // Print the assembly for the instruction. |
| 394 | O << "\t"; |
| 395 | printMachineInstruction(II); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | // We didn't modify anything. |
| 400 | return false; |
| 401 | } |
| 402 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 403 | void Printer::printOp(const MachineOperand &MO, |
Misha Brukman | 46fd00a | 2004-06-24 23:04:11 +0000 | [diff] [blame] | 404 | bool elideOffsetKeyword /* = false */) { |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 405 | const MRegisterInfo &RI = *TM.getRegisterInfo(); |
| 406 | int new_symbol; |
| 407 | |
| 408 | switch (MO.getType()) { |
| 409 | case MachineOperand::MO_VirtualRegister: |
| 410 | if (Value *V = MO.getVRegValueOrNull()) { |
| 411 | O << "<" << V->getName() << ">"; |
| 412 | return; |
| 413 | } |
| 414 | // FALLTHROUGH |
| 415 | case MachineOperand::MO_MachineRegister: |
Misha Brukman | 05fcd0c | 2004-07-08 17:58:04 +0000 | [diff] [blame] | 416 | case MachineOperand::MO_CCRegister: |
Misha Brukman | 7f484a5 | 2004-06-24 23:51:00 +0000 | [diff] [blame] | 417 | O << LowercaseString(RI.get(MO.getReg()).Name); |
| 418 | return; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 419 | |
| 420 | case MachineOperand::MO_SignExtendedImmed: |
Misha Brukman | 97a296f | 2004-07-21 20:11:11 +0000 | [diff] [blame^] | 421 | O << (short)MO.getImmedValue(); |
| 422 | return; |
| 423 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 424 | case MachineOperand::MO_UnextendedImmed: |
Misha Brukman | 97a296f | 2004-07-21 20:11:11 +0000 | [diff] [blame^] | 425 | O << (unsigned short)MO.getImmedValue(); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 426 | return; |
Misha Brukman | 05fcd0c | 2004-07-08 17:58:04 +0000 | [diff] [blame] | 427 | |
| 428 | case MachineOperand::MO_PCRelativeDisp: |
| 429 | std::cerr << "Shouldn't use addPCDisp() when building PPC MachineInstrs"; |
| 430 | abort(); |
| 431 | return; |
| 432 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 433 | case MachineOperand::MO_MachineBasicBlock: { |
| 434 | MachineBasicBlock *MBBOp = MO.getMachineBasicBlock(); |
| 435 | O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction()) |
Misha Brukman | 218bec7 | 2004-06-29 17:13:26 +0000 | [diff] [blame] | 436 | << "_" << MBBOp->getNumber() << "\t; " |
Misha Brukman | 2bf183c | 2004-06-25 15:42:10 +0000 | [diff] [blame] | 437 | << MBBOp->getBasicBlock()->getName(); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 438 | return; |
| 439 | } |
Misha Brukman | 05fcd0c | 2004-07-08 17:58:04 +0000 | [diff] [blame] | 440 | |
| 441 | case MachineOperand::MO_ConstantPoolIndex: |
| 442 | O << ".CPI" << CurrentFnName << "_" << MO.getConstantPoolIndex(); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 443 | return; |
Misha Brukman | 05fcd0c | 2004-07-08 17:58:04 +0000 | [diff] [blame] | 444 | |
| 445 | case MachineOperand::MO_ExternalSymbol: |
| 446 | O << MO.getSymbolName(); |
| 447 | return; |
| 448 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 449 | case MachineOperand::MO_GlobalAddress: |
| 450 | if (!elideOffsetKeyword) { |
Misha Brukman | da2b13f | 2004-07-16 20:29:04 +0000 | [diff] [blame] | 451 | GlobalValue *GV = MO.getGlobal(); |
| 452 | std::string Name = Mang->getValueName(GV); |
Misha Brukman | a6e58b3 | 2004-06-28 18:03:37 +0000 | [diff] [blame] | 453 | // Dynamically-resolved functions need a stub for the function |
Misha Brukman | da2b13f | 2004-07-16 20:29:04 +0000 | [diff] [blame] | 454 | Function *F = dyn_cast<Function>(GV); |
Misha Brukman | a6e58b3 | 2004-06-28 18:03:37 +0000 | [diff] [blame] | 455 | if (F && F->isExternal()) { |
Misha Brukman | da2b13f | 2004-07-16 20:29:04 +0000 | [diff] [blame] | 456 | FnStubs.insert(Name); |
| 457 | O << "L" << Name << "$stub"; |
Misha Brukman | 46fd00a | 2004-06-24 23:04:11 +0000 | [diff] [blame] | 458 | } else { |
Misha Brukman | da2b13f | 2004-07-16 20:29:04 +0000 | [diff] [blame] | 459 | GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV); |
| 460 | // External global variables need a non-lazily-resolved stub |
| 461 | if (GVar && GVar->isExternal()) { |
| 462 | GVStubs.insert(Name); |
| 463 | O << "L" << Name << "$non_lazy_ptr"; |
| 464 | } else |
| 465 | O << Mang->getValueName(GV); |
Misha Brukman | 46fd00a | 2004-06-24 23:04:11 +0000 | [diff] [blame] | 466 | } |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 467 | } |
| 468 | return; |
Misha Brukman | 05fcd0c | 2004-07-08 17:58:04 +0000 | [diff] [blame] | 469 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 470 | default: |
Misha Brukman | 05fcd0c | 2004-07-08 17:58:04 +0000 | [diff] [blame] | 471 | O << "<unknown operand type: " << MO.getType() << ">"; |
Misha Brukman | 22e1207 | 2004-06-25 15:11:34 +0000 | [diff] [blame] | 472 | return; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 473 | } |
| 474 | } |
| 475 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 476 | /// printMachineInstruction -- Print out a single PPC32 LLVM instruction |
| 477 | /// MI in Darwin syntax to the current output stream. |
| 478 | /// |
| 479 | void Printer::printMachineInstruction(const MachineInstr *MI) { |
| 480 | unsigned Opcode = MI->getOpcode(); |
| 481 | const TargetInstrInfo &TII = *TM.getInstrInfo(); |
| 482 | const TargetInstrDescriptor &Desc = TII.get(Opcode); |
| 483 | unsigned int i; |
Misha Brukman | c6cc10f | 2004-06-25 19:24:52 +0000 | [diff] [blame] | 484 | |
Misha Brukman | b9e8f97 | 2004-06-30 21:54:12 +0000 | [diff] [blame] | 485 | unsigned int ArgCount = MI->getNumOperands(); |
| 486 | //Desc.TSFlags & PPC32II::ArgCountMask; |
Misha Brukman | 22e1207 | 2004-06-25 15:11:34 +0000 | [diff] [blame] | 487 | unsigned int ArgType[] = { |
| 488 | (Desc.TSFlags >> PPC32II::Arg0TypeShift) & PPC32II::ArgTypeMask, |
| 489 | (Desc.TSFlags >> PPC32II::Arg1TypeShift) & PPC32II::ArgTypeMask, |
| 490 | (Desc.TSFlags >> PPC32II::Arg2TypeShift) & PPC32II::ArgTypeMask, |
| 491 | (Desc.TSFlags >> PPC32II::Arg3TypeShift) & PPC32II::ArgTypeMask, |
| 492 | (Desc.TSFlags >> PPC32II::Arg4TypeShift) & PPC32II::ArgTypeMask |
| 493 | }; |
Misha Brukman | 7f484a5 | 2004-06-24 23:51:00 +0000 | [diff] [blame] | 494 | assert(((Desc.TSFlags & PPC32II::VMX) == 0) && |
Misha Brukman | 46fd00a | 2004-06-24 23:04:11 +0000 | [diff] [blame] | 495 | "Instruction requires VMX support"); |
Misha Brukman | 7f484a5 | 2004-06-24 23:51:00 +0000 | [diff] [blame] | 496 | assert(((Desc.TSFlags & PPC32II::PPC64) == 0) && |
Misha Brukman | 46fd00a | 2004-06-24 23:04:11 +0000 | [diff] [blame] | 497 | "Instruction requires 64 bit support"); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 498 | ++EmittedInsts; |
| 499 | |
Misha Brukman | 6111461 | 2004-07-20 00:42:19 +0000 | [diff] [blame] | 500 | // CALLpcrel and CALLindirect are handled specially here to print only the |
| 501 | // appropriate number of args that the assembler expects. This is because |
| 502 | // may have many arguments appended to record the uses of registers that are |
| 503 | // holding arguments to the called function. |
Misha Brukman | 29188c6 | 2004-07-16 19:01:13 +0000 | [diff] [blame] | 504 | if (Opcode == PPC32::IMPLICIT_DEF) { |
| 505 | O << "; IMPLICIT DEF "; |
| 506 | printOp(MI->getOperand(0)); |
| 507 | O << "\n"; |
| 508 | return; |
Misha Brukman | 6111461 | 2004-07-20 00:42:19 +0000 | [diff] [blame] | 509 | } else if (Opcode == PPC32::CALLpcrel) { |
| 510 | O << TII.getName(MI->getOpcode()) << " "; |
| 511 | printOp(MI->getOperand(0)); |
| 512 | O << "\n"; |
| 513 | return; |
| 514 | } else if (Opcode == PPC32::CALLindirect) { |
| 515 | O << TII.getName(MI->getOpcode()) << " "; |
| 516 | printOp(MI->getOperand(0)); |
| 517 | O << ", "; |
| 518 | printOp(MI->getOperand(1)); |
| 519 | O << "\n"; |
| 520 | return; |
| 521 | } else if (Opcode == PPC32::MovePCtoLR) { |
| 522 | // FIXME: should probably be converted to cout.width and cout.fill |
Misha Brukman | 4cf5112 | 2004-07-06 22:40:34 +0000 | [diff] [blame] | 523 | O << "bl \"L0000" << labelNumber << "$pb\"\n"; |
Misha Brukman | 218bec7 | 2004-06-29 17:13:26 +0000 | [diff] [blame] | 524 | O << "\"L0000" << labelNumber << "$pb\":\n"; |
| 525 | O << "\tmflr "; |
| 526 | printOp(MI->getOperand(0)); |
Misha Brukman | 218bec7 | 2004-06-29 17:13:26 +0000 | [diff] [blame] | 527 | O << "\n"; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 528 | return; |
| 529 | } |
| 530 | |
| 531 | O << TII.getName(MI->getOpcode()) << " "; |
Misha Brukman | e48178e | 2004-07-20 15:45:27 +0000 | [diff] [blame] | 532 | if (Opcode == PPC32::LOADLoDirect || Opcode == PPC32::LOADLoIndirect) { |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 533 | printOp(MI->getOperand(0)); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 534 | O << ", lo16("; |
| 535 | printOp(MI->getOperand(2)); |
Misha Brukman | 218bec7 | 2004-06-29 17:13:26 +0000 | [diff] [blame] | 536 | O << "-\"L0000" << labelNumber << "$pb\")"; |
| 537 | labelNumber++; |
| 538 | O << "("; |
| 539 | if (MI->getOperand(1).getReg() == PPC32::R0) |
| 540 | O << "0"; |
| 541 | else |
| 542 | printOp(MI->getOperand(1)); |
| 543 | O << ")\n"; |
Misha Brukman | c6cc10f | 2004-06-25 19:24:52 +0000 | [diff] [blame] | 544 | } else if (Opcode == PPC32::LOADHiAddr) { |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 545 | printOp(MI->getOperand(0)); |
| 546 | O << ", "; |
Misha Brukman | 218bec7 | 2004-06-29 17:13:26 +0000 | [diff] [blame] | 547 | if (MI->getOperand(1).getReg() == PPC32::R0) |
| 548 | O << "0"; |
| 549 | else |
| 550 | printOp(MI->getOperand(1)); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 551 | O << ", ha16(" ; |
| 552 | printOp(MI->getOperand(2)); |
Misha Brukman | 218bec7 | 2004-06-29 17:13:26 +0000 | [diff] [blame] | 553 | O << "-\"L0000" << labelNumber << "$pb\")\n"; |
Misha Brukman | c6cc10f | 2004-06-25 19:24:52 +0000 | [diff] [blame] | 554 | } else if (ArgCount == 3 && ArgType[1] == PPC32II::Disimm16) { |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 555 | printOp(MI->getOperand(0)); |
| 556 | O << ", "; |
| 557 | printOp(MI->getOperand(1)); |
| 558 | O << "("; |
Misha Brukman | b9e8f97 | 2004-06-30 21:54:12 +0000 | [diff] [blame] | 559 | if (MI->getOperand(2).hasAllocatedReg() && |
| 560 | MI->getOperand(2).getReg() == PPC32::R0) |
Misha Brukman | 46fd00a | 2004-06-24 23:04:11 +0000 | [diff] [blame] | 561 | O << "0"; |
| 562 | else |
| 563 | printOp(MI->getOperand(2)); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 564 | O << ")\n"; |
| 565 | } else { |
Misha Brukman | 7f484a5 | 2004-06-24 23:51:00 +0000 | [diff] [blame] | 566 | for (i = 0; i < ArgCount; ++i) { |
Misha Brukman | 218bec7 | 2004-06-29 17:13:26 +0000 | [diff] [blame] | 567 | if (i == 1 && ArgCount == 3 && ArgType[2] == PPC32II::Simm16 && |
Misha Brukman | 4363bdb | 2004-07-01 21:09:12 +0000 | [diff] [blame] | 568 | MI->getOperand(1).hasAllocatedReg() && |
Misha Brukman | 218bec7 | 2004-06-29 17:13:26 +0000 | [diff] [blame] | 569 | MI->getOperand(1).getReg() == PPC32::R0) { |
Misha Brukman | 46fd00a | 2004-06-24 23:04:11 +0000 | [diff] [blame] | 570 | O << "0"; |
Misha Brukman | 218bec7 | 2004-06-29 17:13:26 +0000 | [diff] [blame] | 571 | } else { |
Misha Brukman | 46fd00a | 2004-06-24 23:04:11 +0000 | [diff] [blame] | 572 | printOp(MI->getOperand(i)); |
| 573 | } |
Misha Brukman | 7f484a5 | 2004-06-24 23:51:00 +0000 | [diff] [blame] | 574 | if (ArgCount - 1 == i) |
Misha Brukman | 46fd00a | 2004-06-24 23:04:11 +0000 | [diff] [blame] | 575 | O << "\n"; |
| 576 | else |
| 577 | O << ", "; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 578 | } |
| 579 | } |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | bool Printer::doInitialization(Module &M) { |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 583 | Mang = new Mangler(M, true); |
| 584 | return false; // success |
| 585 | } |
| 586 | |
| 587 | // SwitchSection - Switch to the specified section of the executable if we are |
| 588 | // not already in it! |
| 589 | // |
| 590 | static void SwitchSection(std::ostream &OS, std::string &CurSection, |
| 591 | const char *NewSection) { |
| 592 | if (CurSection != NewSection) { |
| 593 | CurSection = NewSection; |
| 594 | if (!CurSection.empty()) |
| 595 | OS << "\t" << NewSection << "\n"; |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | bool Printer::doFinalization(Module &M) { |
| 600 | const TargetData &TD = TM.getTargetData(); |
| 601 | std::string CurSection; |
| 602 | |
| 603 | // Print out module-level global variables here. |
| 604 | for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I) |
| 605 | if (I->hasInitializer()) { // External global require no code |
| 606 | O << "\n\n"; |
| 607 | std::string name = Mang->getValueName(I); |
| 608 | Constant *C = I->getInitializer(); |
| 609 | unsigned Size = TD.getTypeSize(C->getType()); |
| 610 | unsigned Align = TD.getTypeAlignment(C->getType()); |
| 611 | |
Misha Brukman | 97a296f | 2004-07-21 20:11:11 +0000 | [diff] [blame^] | 612 | if (C->isNullValue() && /* FIXME: Verify correct */ |
| 613 | (I->hasInternalLinkage() || I->hasWeakLinkage())) { |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 614 | SwitchSection(O, CurSection, ".data"); |
| 615 | if (I->hasInternalLinkage()) |
Misha Brukman | 218bec7 | 2004-06-29 17:13:26 +0000 | [diff] [blame] | 616 | O << "\t.lcomm " << name << "," << TD.getTypeSize(C->getType()) |
| 617 | << "," << (unsigned)TD.getTypeAlignment(C->getType()); |
| 618 | else |
| 619 | O << "\t.comm " << name << "," << TD.getTypeSize(C->getType()); |
| 620 | O << "\t\t; "; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 621 | WriteAsOperand(O, I, true, true, &M); |
| 622 | O << "\n"; |
| 623 | } else { |
| 624 | switch (I->getLinkage()) { |
| 625 | case GlobalValue::LinkOnceLinkage: |
Misha Brukman | 97a296f | 2004-07-21 20:11:11 +0000 | [diff] [blame^] | 626 | O << ".section __TEXT,__textcoal_nt,coalesced,no_toc\n" |
| 627 | << ".weak_definition " << name << '\n' |
| 628 | << ".private_extern " << name << '\n' |
| 629 | << ".section __DATA,__datacoal_nt,coalesced,no_toc\n"; |
| 630 | LinkOnceStubs.insert(name); |
| 631 | break; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 632 | case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak. |
| 633 | // Nonnull linkonce -> weak |
| 634 | O << "\t.weak " << name << "\n"; |
| 635 | SwitchSection(O, CurSection, ""); |
| 636 | O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n"; |
| 637 | break; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 638 | case GlobalValue::AppendingLinkage: |
| 639 | // FIXME: appending linkage variables should go into a section of |
| 640 | // their name or something. For now, just emit them as external. |
| 641 | case GlobalValue::ExternalLinkage: |
| 642 | // If external or appending, declare as a global symbol |
| 643 | O << "\t.globl " << name << "\n"; |
| 644 | // FALL THROUGH |
| 645 | case GlobalValue::InternalLinkage: |
Misha Brukman | 61297ee | 2004-06-29 23:40:57 +0000 | [diff] [blame] | 646 | SwitchSection(O, CurSection, ".data"); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 647 | break; |
| 648 | } |
| 649 | |
| 650 | O << "\t.align " << Align << "\n"; |
Misha Brukman | 218bec7 | 2004-06-29 17:13:26 +0000 | [diff] [blame] | 651 | O << name << ":\t\t\t\t; "; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 652 | WriteAsOperand(O, I, true, true, &M); |
| 653 | O << " = "; |
| 654 | WriteAsOperand(O, C, false, false, &M); |
| 655 | O << "\n"; |
| 656 | emitGlobalConstant(C); |
| 657 | } |
| 658 | } |
Misha Brukman | da2b13f | 2004-07-16 20:29:04 +0000 | [diff] [blame] | 659 | |
Misha Brukman | 97a296f | 2004-07-21 20:11:11 +0000 | [diff] [blame^] | 660 | // Output stubs for link-once variables |
| 661 | if (LinkOnceStubs.begin() != LinkOnceStubs.end()) |
| 662 | O << ".data\n.align 2\n"; |
| 663 | for (std::set<std::string>::iterator i = LinkOnceStubs.begin(), |
| 664 | e = LinkOnceStubs.end(); i != e; ++i) |
| 665 | { |
| 666 | O << *i << "$non_lazy_ptr:\n" |
| 667 | << "\t.long\t" << *i << '\n'; |
| 668 | } |
| 669 | |
Misha Brukman | da2b13f | 2004-07-16 20:29:04 +0000 | [diff] [blame] | 670 | // Output stubs for dynamically-linked functions |
| 671 | for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end(); |
| 672 | i != e; ++i) |
Misha Brukman | 46fd00a | 2004-06-24 23:04:11 +0000 | [diff] [blame] | 673 | { |
Misha Brukman | 218bec7 | 2004-06-29 17:13:26 +0000 | [diff] [blame] | 674 | O << "\t.picsymbol_stub\n"; |
Misha Brukman | 46fd00a | 2004-06-24 23:04:11 +0000 | [diff] [blame] | 675 | O << "L" << *i << "$stub:\n"; |
| 676 | O << "\t.indirect_symbol " << *i << "\n"; |
| 677 | O << "\tmflr r0\n"; |
Misha Brukman | 4cf5112 | 2004-07-06 22:40:34 +0000 | [diff] [blame] | 678 | O << "\tbl L0$" << *i << "\n"; |
Misha Brukman | 46fd00a | 2004-06-24 23:04:11 +0000 | [diff] [blame] | 679 | O << "L0$" << *i << ":\n"; |
| 680 | O << "\tmflr r11\n"; |
| 681 | O << "\taddis r11,r11,ha16(L" << *i << "$lazy_ptr-L0$" << *i << ")\n"; |
| 682 | O << "\tmtlr r0\n"; |
Misha Brukman | 218bec7 | 2004-06-29 17:13:26 +0000 | [diff] [blame] | 683 | O << "\tlwz r12,lo16(L" << *i << "$lazy_ptr-L0$" << *i << ")(r11)\n"; |
Misha Brukman | 46fd00a | 2004-06-24 23:04:11 +0000 | [diff] [blame] | 684 | O << "\tmtctr r12\n"; |
Misha Brukman | 218bec7 | 2004-06-29 17:13:26 +0000 | [diff] [blame] | 685 | O << "\taddi r11,r11,lo16(L" << *i << "$lazy_ptr - L0$" << *i << ")\n"; |
Misha Brukman | 46fd00a | 2004-06-24 23:04:11 +0000 | [diff] [blame] | 686 | O << "\tbctr\n"; |
| 687 | O << ".data\n"; |
| 688 | O << ".lazy_symbol_pointer\n"; |
| 689 | O << "L" << *i << "$lazy_ptr:\n"; |
| 690 | O << ".indirect_symbol " << *i << "\n"; |
| 691 | O << ".long dyld_stub_binding_helper\n"; |
| 692 | } |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 693 | |
Misha Brukman | da2b13f | 2004-07-16 20:29:04 +0000 | [diff] [blame] | 694 | O << "\n"; |
| 695 | |
| 696 | // Output stubs for external global variables |
| 697 | if (GVStubs.begin() != GVStubs.end()) |
Misha Brukman | e48178e | 2004-07-20 15:45:27 +0000 | [diff] [blame] | 698 | O << ".data\n\t.non_lazy_symbol_pointer\n"; |
Misha Brukman | da2b13f | 2004-07-16 20:29:04 +0000 | [diff] [blame] | 699 | for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end(); |
| 700 | i != e; ++i) { |
| 701 | O << "L" << *i << "$non_lazy_ptr:\n"; |
| 702 | O << "\t.indirect_symbol " << *i << "\n"; |
| 703 | O << "\t.long\t0\n"; |
| 704 | } |
| 705 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 706 | delete Mang; |
| 707 | return false; // success |
| 708 | } |
| 709 | |
| 710 | } // End llvm namespace |