Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===-- PPCAsmPrinter.cpp - Print machine instrs to PowerPC assembly --------=// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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 | // Documentation at http://developer.apple.com/documentation/DeveloperTools/ |
| 15 | // Reference/Assembler/ASMIntroduction/chapter_1_section_1.html |
| 16 | // |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
| 19 | #define DEBUG_TYPE "asmprinter" |
| 20 | #include "PPC.h" |
| 21 | #include "PPCPredicates.h" |
| 22 | #include "PPCTargetMachine.h" |
| 23 | #include "PPCSubtarget.h" |
| 24 | #include "llvm/Constants.h" |
| 25 | #include "llvm/DerivedTypes.h" |
| 26 | #include "llvm/Module.h" |
| 27 | #include "llvm/Assembly/Writer.h" |
| 28 | #include "llvm/CodeGen/AsmPrinter.h" |
| 29 | #include "llvm/CodeGen/DwarfWriter.h" |
| 30 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| 31 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 32 | #include "llvm/CodeGen/MachineInstr.h" |
Bill Wendling | 36ccaea | 2008-01-26 06:51:24 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Mangler.h" |
| 35 | #include "llvm/Support/MathExtras.h" |
| 36 | #include "llvm/Support/CommandLine.h" |
| 37 | #include "llvm/Support/Debug.h" |
| 38 | #include "llvm/Support/Compiler.h" |
| 39 | #include "llvm/Target/TargetAsmInfo.h" |
Dan Gohman | 1e57df3 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 40 | #include "llvm/Target/TargetRegisterInfo.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 41 | #include "llvm/Target/TargetInstrInfo.h" |
| 42 | #include "llvm/Target/TargetOptions.h" |
| 43 | #include "llvm/ADT/Statistic.h" |
| 44 | #include "llvm/ADT/StringExtras.h" |
| 45 | #include <set> |
| 46 | using namespace llvm; |
| 47 | |
| 48 | STATISTIC(EmittedInsts, "Number of machine instrs printed"); |
| 49 | |
| 50 | namespace { |
| 51 | struct VISIBILITY_HIDDEN PPCAsmPrinter : public AsmPrinter { |
| 52 | std::set<std::string> FnStubs, GVStubs; |
| 53 | const PPCSubtarget &Subtarget; |
| 54 | |
| 55 | PPCAsmPrinter(std::ostream &O, TargetMachine &TM, const TargetAsmInfo *T) |
| 56 | : AsmPrinter(O, TM, T), Subtarget(TM.getSubtarget<PPCSubtarget>()) { |
| 57 | } |
| 58 | |
| 59 | virtual const char *getPassName() const { |
| 60 | return "PowerPC Assembly Printer"; |
| 61 | } |
| 62 | |
| 63 | PPCTargetMachine &getTM() { |
| 64 | return static_cast<PPCTargetMachine&>(TM); |
| 65 | } |
| 66 | |
| 67 | unsigned enumRegToMachineReg(unsigned enumReg) { |
| 68 | switch (enumReg) { |
| 69 | default: assert(0 && "Unhandled register!"); break; |
| 70 | case PPC::CR0: return 0; |
| 71 | case PPC::CR1: return 1; |
| 72 | case PPC::CR2: return 2; |
| 73 | case PPC::CR3: return 3; |
| 74 | case PPC::CR4: return 4; |
| 75 | case PPC::CR5: return 5; |
| 76 | case PPC::CR6: return 6; |
| 77 | case PPC::CR7: return 7; |
| 78 | } |
| 79 | abort(); |
| 80 | } |
| 81 | |
| 82 | /// printInstruction - This method is automatically generated by tablegen |
| 83 | /// from the instruction set description. This method returns true if the |
| 84 | /// machine instruction was sufficiently described to print it, otherwise it |
| 85 | /// returns false. |
| 86 | bool printInstruction(const MachineInstr *MI); |
| 87 | |
| 88 | void printMachineInstruction(const MachineInstr *MI); |
| 89 | void printOp(const MachineOperand &MO); |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 90 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 91 | /// stripRegisterPrefix - This method strips the character prefix from a |
| 92 | /// register name so that only the number is left. Used by for linux asm. |
| 93 | const char *stripRegisterPrefix(const char *RegName) { |
| 94 | switch (RegName[0]) { |
| 95 | case 'r': |
| 96 | case 'f': |
| 97 | case 'v': return RegName + 1; |
| 98 | case 'c': if (RegName[1] == 'r') return RegName + 2; |
| 99 | } |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 100 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 101 | return RegName; |
| 102 | } |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 103 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 104 | /// printRegister - Print register according to target requirements. |
| 105 | /// |
| 106 | void printRegister(const MachineOperand &MO, bool R0AsZero) { |
| 107 | unsigned RegNo = MO.getReg(); |
Dan Gohman | 1e57df3 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 108 | assert(TargetRegisterInfo::isPhysicalRegister(RegNo) && "Not physreg??"); |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 109 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 110 | // If we should use 0 for R0. |
| 111 | if (R0AsZero && RegNo == PPC::R0) { |
| 112 | O << "0"; |
| 113 | return; |
| 114 | } |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 115 | |
Bill Wendling | 8eeb979 | 2008-02-26 21:11:01 +0000 | [diff] [blame] | 116 | const char *RegName = TM.getRegisterInfo()->get(RegNo).AsmName; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 117 | // Linux assembler (Others?) does not take register mnemonics. |
| 118 | // FIXME - What about special registers used in mfspr/mtspr? |
| 119 | if (!Subtarget.isDarwin()) RegName = stripRegisterPrefix(RegName); |
| 120 | O << RegName; |
| 121 | } |
| 122 | |
| 123 | void printOperand(const MachineInstr *MI, unsigned OpNo) { |
| 124 | const MachineOperand &MO = MI->getOperand(OpNo); |
| 125 | if (MO.isRegister()) { |
| 126 | printRegister(MO, false); |
| 127 | } else if (MO.isImmediate()) { |
Chris Lattner | a96056a | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 128 | O << MO.getImm(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 129 | } else { |
| 130 | printOp(MO); |
| 131 | } |
| 132 | } |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 133 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 134 | bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, |
| 135 | unsigned AsmVariant, const char *ExtraCode); |
| 136 | bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo, |
| 137 | unsigned AsmVariant, const char *ExtraCode); |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 138 | |
| 139 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 140 | void printS5ImmOperand(const MachineInstr *MI, unsigned OpNo) { |
Chris Lattner | a96056a | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 141 | char value = MI->getOperand(OpNo).getImm(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 142 | value = (value << (32-5)) >> (32-5); |
| 143 | O << (int)value; |
| 144 | } |
| 145 | void printU5ImmOperand(const MachineInstr *MI, unsigned OpNo) { |
Chris Lattner | a96056a | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 146 | unsigned char value = MI->getOperand(OpNo).getImm(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 147 | assert(value <= 31 && "Invalid u5imm argument!"); |
| 148 | O << (unsigned int)value; |
| 149 | } |
| 150 | void printU6ImmOperand(const MachineInstr *MI, unsigned OpNo) { |
Chris Lattner | a96056a | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 151 | unsigned char value = MI->getOperand(OpNo).getImm(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 152 | assert(value <= 63 && "Invalid u6imm argument!"); |
| 153 | O << (unsigned int)value; |
| 154 | } |
| 155 | void printS16ImmOperand(const MachineInstr *MI, unsigned OpNo) { |
Chris Lattner | a96056a | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 156 | O << (short)MI->getOperand(OpNo).getImm(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 157 | } |
| 158 | void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo) { |
Chris Lattner | a96056a | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 159 | O << (unsigned short)MI->getOperand(OpNo).getImm(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 160 | } |
| 161 | void printS16X4ImmOperand(const MachineInstr *MI, unsigned OpNo) { |
| 162 | if (MI->getOperand(OpNo).isImmediate()) { |
Chris Lattner | a96056a | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 163 | O << (short)(MI->getOperand(OpNo).getImm()*4); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 164 | } else { |
| 165 | O << "lo16("; |
| 166 | printOp(MI->getOperand(OpNo)); |
| 167 | if (TM.getRelocationModel() == Reloc::PIC_) |
Evan Cheng | 477013c | 2007-10-14 05:57:21 +0000 | [diff] [blame] | 168 | O << "-\"L" << getFunctionNumber() << "$pb\")"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 169 | else |
| 170 | O << ')'; |
| 171 | } |
| 172 | } |
| 173 | void printBranchOperand(const MachineInstr *MI, unsigned OpNo) { |
| 174 | // Branches can take an immediate operand. This is used by the branch |
| 175 | // selection pass to print $+8, an eight byte displacement from the PC. |
| 176 | if (MI->getOperand(OpNo).isImmediate()) { |
Chris Lattner | a96056a | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 177 | O << "$+" << MI->getOperand(OpNo).getImm()*4; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 178 | } else { |
| 179 | printOp(MI->getOperand(OpNo)); |
| 180 | } |
| 181 | } |
| 182 | void printCallOperand(const MachineInstr *MI, unsigned OpNo) { |
| 183 | const MachineOperand &MO = MI->getOperand(OpNo); |
| 184 | if (TM.getRelocationModel() != Reloc::Static) { |
| 185 | if (MO.getType() == MachineOperand::MO_GlobalAddress) { |
| 186 | GlobalValue *GV = MO.getGlobal(); |
| 187 | if (((GV->isDeclaration() || GV->hasWeakLinkage() || |
Dale Johannesen | 49c4412 | 2008-05-14 20:12:51 +0000 | [diff] [blame] | 188 | GV->hasLinkOnceLinkage() || GV->hasCommonLinkage()))) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 189 | // Dynamically-resolved functions need a stub for the function. |
| 190 | std::string Name = Mang->getValueName(GV); |
| 191 | FnStubs.insert(Name); |
Dale Johannesen | a21b520 | 2008-05-19 21:38:18 +0000 | [diff] [blame] | 192 | printSuffixedName(Name, "$stub"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 193 | if (GV->hasExternalWeakLinkage()) |
| 194 | ExtWeakSymbols.insert(GV); |
| 195 | return; |
| 196 | } |
| 197 | } |
| 198 | if (MO.getType() == MachineOperand::MO_ExternalSymbol) { |
| 199 | std::string Name(TAI->getGlobalPrefix()); Name += MO.getSymbolName(); |
| 200 | FnStubs.insert(Name); |
Dale Johannesen | a21b520 | 2008-05-19 21:38:18 +0000 | [diff] [blame] | 201 | printSuffixedName(Name, "$stub"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 202 | return; |
| 203 | } |
| 204 | } |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 205 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 206 | printOp(MI->getOperand(OpNo)); |
| 207 | } |
| 208 | void printAbsAddrOperand(const MachineInstr *MI, unsigned OpNo) { |
Chris Lattner | a96056a | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 209 | O << (int)MI->getOperand(OpNo).getImm()*4; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 210 | } |
| 211 | void printPICLabel(const MachineInstr *MI, unsigned OpNo) { |
Evan Cheng | 477013c | 2007-10-14 05:57:21 +0000 | [diff] [blame] | 212 | O << "\"L" << getFunctionNumber() << "$pb\"\n"; |
| 213 | O << "\"L" << getFunctionNumber() << "$pb\":"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 214 | } |
| 215 | void printSymbolHi(const MachineInstr *MI, unsigned OpNo) { |
| 216 | if (MI->getOperand(OpNo).isImmediate()) { |
| 217 | printS16ImmOperand(MI, OpNo); |
| 218 | } else { |
| 219 | if (Subtarget.isDarwin()) O << "ha16("; |
| 220 | printOp(MI->getOperand(OpNo)); |
| 221 | if (TM.getRelocationModel() == Reloc::PIC_) |
Evan Cheng | 477013c | 2007-10-14 05:57:21 +0000 | [diff] [blame] | 222 | O << "-\"L" << getFunctionNumber() << "$pb\""; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 223 | if (Subtarget.isDarwin()) |
| 224 | O << ')'; |
| 225 | else |
| 226 | O << "@ha"; |
| 227 | } |
| 228 | } |
| 229 | void printSymbolLo(const MachineInstr *MI, unsigned OpNo) { |
| 230 | if (MI->getOperand(OpNo).isImmediate()) { |
| 231 | printS16ImmOperand(MI, OpNo); |
| 232 | } else { |
| 233 | if (Subtarget.isDarwin()) O << "lo16("; |
| 234 | printOp(MI->getOperand(OpNo)); |
| 235 | if (TM.getRelocationModel() == Reloc::PIC_) |
Evan Cheng | 477013c | 2007-10-14 05:57:21 +0000 | [diff] [blame] | 236 | O << "-\"L" << getFunctionNumber() << "$pb\""; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 237 | if (Subtarget.isDarwin()) |
| 238 | O << ')'; |
| 239 | else |
| 240 | O << "@l"; |
| 241 | } |
| 242 | } |
| 243 | void printcrbitm(const MachineInstr *MI, unsigned OpNo) { |
| 244 | unsigned CCReg = MI->getOperand(OpNo).getReg(); |
| 245 | unsigned RegNo = enumRegToMachineReg(CCReg); |
| 246 | O << (0x80 >> RegNo); |
| 247 | } |
| 248 | // The new addressing mode printers. |
| 249 | void printMemRegImm(const MachineInstr *MI, unsigned OpNo) { |
| 250 | printSymbolLo(MI, OpNo); |
| 251 | O << '('; |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 252 | if (MI->getOperand(OpNo+1).isRegister() && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 253 | MI->getOperand(OpNo+1).getReg() == PPC::R0) |
| 254 | O << "0"; |
| 255 | else |
| 256 | printOperand(MI, OpNo+1); |
| 257 | O << ')'; |
| 258 | } |
| 259 | void printMemRegImmShifted(const MachineInstr *MI, unsigned OpNo) { |
| 260 | if (MI->getOperand(OpNo).isImmediate()) |
| 261 | printS16X4ImmOperand(MI, OpNo); |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 262 | else |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 263 | printSymbolLo(MI, OpNo); |
| 264 | O << '('; |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 265 | if (MI->getOperand(OpNo+1).isRegister() && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 266 | MI->getOperand(OpNo+1).getReg() == PPC::R0) |
| 267 | O << "0"; |
| 268 | else |
| 269 | printOperand(MI, OpNo+1); |
| 270 | O << ')'; |
| 271 | } |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 272 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 273 | void printMemRegReg(const MachineInstr *MI, unsigned OpNo) { |
| 274 | // When used as the base register, r0 reads constant zero rather than |
| 275 | // the value contained in the register. For this reason, the darwin |
| 276 | // assembler requires that we print r0 as 0 (no r) when used as the base. |
| 277 | const MachineOperand &MO = MI->getOperand(OpNo); |
| 278 | printRegister(MO, true); |
| 279 | O << ", "; |
| 280 | printOperand(MI, OpNo+1); |
| 281 | } |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 282 | |
| 283 | void printPredicateOperand(const MachineInstr *MI, unsigned OpNo, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 284 | const char *Modifier); |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 285 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 286 | virtual bool runOnMachineFunction(MachineFunction &F) = 0; |
| 287 | virtual bool doFinalization(Module &M) = 0; |
| 288 | |
| 289 | virtual void EmitExternalGlobal(const GlobalVariable *GV); |
| 290 | }; |
| 291 | |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 292 | /// PPCLinuxAsmPrinter - PowerPC assembly printer, customized for Linux |
| 293 | struct VISIBILITY_HIDDEN PPCLinuxAsmPrinter : public PPCAsmPrinter { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 294 | |
| 295 | DwarfWriter DW; |
Dale Johannesen | 2f6aa07 | 2008-07-09 21:24:07 +0000 | [diff] [blame] | 296 | MachineModuleInfo *MMI; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 297 | |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 298 | PPCLinuxAsmPrinter(std::ostream &O, PPCTargetMachine &TM, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 299 | const TargetAsmInfo *T) |
Dale Johannesen | 2f6aa07 | 2008-07-09 21:24:07 +0000 | [diff] [blame] | 300 | : PPCAsmPrinter(O, TM, T), DW(O, this, T), MMI(0) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | virtual const char *getPassName() const { |
| 304 | return "Linux PPC Assembly Printer"; |
| 305 | } |
| 306 | |
| 307 | bool runOnMachineFunction(MachineFunction &F); |
| 308 | bool doInitialization(Module &M); |
| 309 | bool doFinalization(Module &M); |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 310 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 311 | void getAnalysisUsage(AnalysisUsage &AU) const { |
| 312 | AU.setPreservesAll(); |
| 313 | AU.addRequired<MachineModuleInfo>(); |
| 314 | PPCAsmPrinter::getAnalysisUsage(AU); |
| 315 | } |
| 316 | |
| 317 | /// getSectionForFunction - Return the section that we should emit the |
| 318 | /// specified function body into. |
| 319 | virtual std::string getSectionForFunction(const Function &F) const; |
| 320 | }; |
| 321 | |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 322 | /// PPCDarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac |
| 323 | /// OS X |
| 324 | struct VISIBILITY_HIDDEN PPCDarwinAsmPrinter : public PPCAsmPrinter { |
| 325 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 326 | DwarfWriter DW; |
Dale Johannesen | fb3ac73 | 2007-11-20 23:24:42 +0000 | [diff] [blame] | 327 | MachineModuleInfo *MMI; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 328 | |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 329 | PPCDarwinAsmPrinter(std::ostream &O, PPCTargetMachine &TM, |
| 330 | const TargetAsmInfo *T) |
Dale Johannesen | fb3ac73 | 2007-11-20 23:24:42 +0000 | [diff] [blame] | 331 | : PPCAsmPrinter(O, TM, T), DW(O, this, T), MMI(0) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | virtual const char *getPassName() const { |
| 335 | return "Darwin PPC Assembly Printer"; |
| 336 | } |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 337 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 338 | bool runOnMachineFunction(MachineFunction &F); |
| 339 | bool doInitialization(Module &M); |
| 340 | bool doFinalization(Module &M); |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 341 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 342 | void getAnalysisUsage(AnalysisUsage &AU) const { |
| 343 | AU.setPreservesAll(); |
| 344 | AU.addRequired<MachineModuleInfo>(); |
| 345 | PPCAsmPrinter::getAnalysisUsage(AU); |
| 346 | } |
| 347 | |
| 348 | /// getSectionForFunction - Return the section that we should emit the |
| 349 | /// specified function body into. |
| 350 | virtual std::string getSectionForFunction(const Function &F) const; |
Anton Korobeynikov | 248e9c5 | 2008-08-08 18:23:25 +0000 | [diff] [blame^] | 351 | void printModuleLevelGV(const GlobalVariable* GVar); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 352 | }; |
| 353 | } // end of anonymous namespace |
| 354 | |
| 355 | // Include the auto-generated portion of the assembly writer |
| 356 | #include "PPCGenAsmWriter.inc" |
| 357 | |
| 358 | void PPCAsmPrinter::printOp(const MachineOperand &MO) { |
| 359 | switch (MO.getType()) { |
| 360 | case MachineOperand::MO_Immediate: |
| 361 | cerr << "printOp() does not handle immediate values\n"; |
| 362 | abort(); |
| 363 | return; |
| 364 | |
| 365 | case MachineOperand::MO_MachineBasicBlock: |
Chris Lattner | 6017d48 | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 366 | printBasicBlockLabel(MO.getMBB()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 367 | return; |
| 368 | case MachineOperand::MO_JumpTableIndex: |
Evan Cheng | 477013c | 2007-10-14 05:57:21 +0000 | [diff] [blame] | 369 | O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() |
Chris Lattner | 6017d48 | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 370 | << '_' << MO.getIndex(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 371 | // FIXME: PIC relocation model |
| 372 | return; |
| 373 | case MachineOperand::MO_ConstantPoolIndex: |
Evan Cheng | 477013c | 2007-10-14 05:57:21 +0000 | [diff] [blame] | 374 | O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() |
Chris Lattner | 6017d48 | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 375 | << '_' << MO.getIndex(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 376 | return; |
| 377 | case MachineOperand::MO_ExternalSymbol: |
| 378 | // Computing the address of an external symbol, not calling it. |
| 379 | if (TM.getRelocationModel() != Reloc::Static) { |
| 380 | std::string Name(TAI->getGlobalPrefix()); Name += MO.getSymbolName(); |
| 381 | GVStubs.insert(Name); |
Dale Johannesen | a21b520 | 2008-05-19 21:38:18 +0000 | [diff] [blame] | 382 | printSuffixedName(Name, "$non_lazy_ptr"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 383 | return; |
| 384 | } |
| 385 | O << TAI->getGlobalPrefix() << MO.getSymbolName(); |
| 386 | return; |
| 387 | case MachineOperand::MO_GlobalAddress: { |
| 388 | // Computing the address of a global symbol, not calling it. |
| 389 | GlobalValue *GV = MO.getGlobal(); |
| 390 | std::string Name = Mang->getValueName(GV); |
| 391 | |
| 392 | // External or weakly linked global variables need non-lazily-resolved stubs |
| 393 | if (TM.getRelocationModel() != Reloc::Static) { |
| 394 | if (((GV->isDeclaration() || GV->hasWeakLinkage() || |
Dale Johannesen | 49c4412 | 2008-05-14 20:12:51 +0000 | [diff] [blame] | 395 | GV->hasLinkOnceLinkage() || GV->hasCommonLinkage()))) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 396 | GVStubs.insert(Name); |
Dale Johannesen | a21b520 | 2008-05-19 21:38:18 +0000 | [diff] [blame] | 397 | printSuffixedName(Name, "$non_lazy_ptr"); |
Dale Johannesen | caf1118 | 2008-05-16 20:09:25 +0000 | [diff] [blame] | 398 | if (GV->hasExternalWeakLinkage()) |
| 399 | ExtWeakSymbols.insert(GV); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 400 | return; |
| 401 | } |
| 402 | } |
| 403 | O << Name; |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 404 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 405 | if (MO.getOffset() > 0) |
| 406 | O << "+" << MO.getOffset(); |
| 407 | else if (MO.getOffset() < 0) |
| 408 | O << MO.getOffset(); |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 409 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 410 | if (GV->hasExternalWeakLinkage()) |
| 411 | ExtWeakSymbols.insert(GV); |
| 412 | return; |
| 413 | } |
| 414 | |
| 415 | default: |
| 416 | O << "<unknown operand type: " << MO.getType() << ">"; |
| 417 | return; |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | /// EmitExternalGlobal - In this case we need to use the indirect symbol. |
| 422 | /// |
| 423 | void PPCAsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) { |
| 424 | std::string Name = getGlobalLinkName(GV); |
| 425 | if (TM.getRelocationModel() != Reloc::Static) { |
| 426 | GVStubs.insert(Name); |
Dale Johannesen | a21b520 | 2008-05-19 21:38:18 +0000 | [diff] [blame] | 427 | printSuffixedName(Name, "$non_lazy_ptr"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 428 | return; |
| 429 | } |
| 430 | O << Name; |
| 431 | } |
| 432 | |
| 433 | /// PrintAsmOperand - Print out an operand for an inline asm expression. |
| 434 | /// |
| 435 | bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 436 | unsigned AsmVariant, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 437 | const char *ExtraCode) { |
| 438 | // Does this asm operand have a single letter operand modifier? |
| 439 | if (ExtraCode && ExtraCode[0]) { |
| 440 | if (ExtraCode[1] != 0) return true; // Unknown modifier. |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 441 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 442 | switch (ExtraCode[0]) { |
| 443 | default: return true; // Unknown modifier. |
| 444 | case 'c': // Don't print "$" before a global var name or constant. |
| 445 | // PPC never has a prefix. |
| 446 | printOperand(MI, OpNo); |
| 447 | return false; |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 448 | case 'L': // Write second word of DImode reference. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 449 | // Verify that this operand has two consecutive registers. |
| 450 | if (!MI->getOperand(OpNo).isRegister() || |
| 451 | OpNo+1 == MI->getNumOperands() || |
| 452 | !MI->getOperand(OpNo+1).isRegister()) |
| 453 | return true; |
| 454 | ++OpNo; // Return the high-part. |
| 455 | break; |
| 456 | case 'I': |
| 457 | // Write 'i' if an integer constant, otherwise nothing. Used to print |
| 458 | // addi vs add, etc. |
Dan Gohman | 38a9a9f | 2007-09-14 20:33:02 +0000 | [diff] [blame] | 459 | if (MI->getOperand(OpNo).isImmediate()) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 460 | O << "i"; |
| 461 | return false; |
| 462 | } |
| 463 | } |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 464 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 465 | printOperand(MI, OpNo); |
| 466 | return false; |
| 467 | } |
| 468 | |
| 469 | bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo, |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 470 | unsigned AsmVariant, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 471 | const char *ExtraCode) { |
| 472 | if (ExtraCode && ExtraCode[0]) |
| 473 | return true; // Unknown modifier. |
| 474 | if (MI->getOperand(OpNo).isRegister()) |
| 475 | printMemRegReg(MI, OpNo); |
| 476 | else |
| 477 | printMemRegImm(MI, OpNo); |
| 478 | return false; |
| 479 | } |
| 480 | |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 481 | void PPCAsmPrinter::printPredicateOperand(const MachineInstr *MI, unsigned OpNo, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 482 | const char *Modifier) { |
| 483 | assert(Modifier && "Must specify 'cc' or 'reg' as predicate op modifier!"); |
| 484 | unsigned Code = MI->getOperand(OpNo).getImm(); |
| 485 | if (!strcmp(Modifier, "cc")) { |
| 486 | switch ((PPC::Predicate)Code) { |
| 487 | case PPC::PRED_ALWAYS: return; // Don't print anything for always. |
| 488 | case PPC::PRED_LT: O << "lt"; return; |
| 489 | case PPC::PRED_LE: O << "le"; return; |
| 490 | case PPC::PRED_EQ: O << "eq"; return; |
| 491 | case PPC::PRED_GE: O << "ge"; return; |
| 492 | case PPC::PRED_GT: O << "gt"; return; |
| 493 | case PPC::PRED_NE: O << "ne"; return; |
| 494 | case PPC::PRED_UN: O << "un"; return; |
| 495 | case PPC::PRED_NU: O << "nu"; return; |
| 496 | } |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 497 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 498 | } else { |
| 499 | assert(!strcmp(Modifier, "reg") && |
| 500 | "Need to specify 'cc' or 'reg' as predicate op modifier!"); |
| 501 | // Don't print the register for 'always'. |
| 502 | if (Code == PPC::PRED_ALWAYS) return; |
| 503 | printOperand(MI, OpNo+1); |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | |
| 508 | /// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to |
| 509 | /// the current output stream. |
| 510 | /// |
| 511 | void PPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) { |
| 512 | ++EmittedInsts; |
| 513 | |
| 514 | // Check for slwi/srwi mnemonics. |
| 515 | if (MI->getOpcode() == PPC::RLWINM) { |
| 516 | bool FoundMnemonic = false; |
Chris Lattner | a96056a | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 517 | unsigned char SH = MI->getOperand(2).getImm(); |
| 518 | unsigned char MB = MI->getOperand(3).getImm(); |
| 519 | unsigned char ME = MI->getOperand(4).getImm(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 520 | if (SH <= 31 && MB == 0 && ME == (31-SH)) { |
Nate Begeman | bd5cdf1 | 2008-02-05 08:49:09 +0000 | [diff] [blame] | 521 | O << "\tslwi "; FoundMnemonic = true; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 522 | } |
| 523 | if (SH <= 31 && MB == (32-SH) && ME == 31) { |
Nate Begeman | bd5cdf1 | 2008-02-05 08:49:09 +0000 | [diff] [blame] | 524 | O << "\tsrwi "; FoundMnemonic = true; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 525 | SH = 32-SH; |
| 526 | } |
| 527 | if (FoundMnemonic) { |
| 528 | printOperand(MI, 0); |
| 529 | O << ", "; |
| 530 | printOperand(MI, 1); |
| 531 | O << ", " << (unsigned int)SH << "\n"; |
| 532 | return; |
| 533 | } |
| 534 | } else if (MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) { |
| 535 | if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) { |
Nate Begeman | bd5cdf1 | 2008-02-05 08:49:09 +0000 | [diff] [blame] | 536 | O << "\tmr "; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 537 | printOperand(MI, 0); |
| 538 | O << ", "; |
| 539 | printOperand(MI, 1); |
| 540 | O << "\n"; |
| 541 | return; |
| 542 | } |
| 543 | } else if (MI->getOpcode() == PPC::RLDICR) { |
Chris Lattner | a96056a | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 544 | unsigned char SH = MI->getOperand(2).getImm(); |
| 545 | unsigned char ME = MI->getOperand(3).getImm(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 546 | // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH |
| 547 | if (63-SH == ME) { |
Nate Begeman | bd5cdf1 | 2008-02-05 08:49:09 +0000 | [diff] [blame] | 548 | O << "\tsldi "; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 549 | printOperand(MI, 0); |
| 550 | O << ", "; |
| 551 | printOperand(MI, 1); |
| 552 | O << ", " << (unsigned int)SH << "\n"; |
| 553 | return; |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | if (printInstruction(MI)) |
| 558 | return; // Printer was automatically generated |
| 559 | |
| 560 | assert(0 && "Unhandled instruction in asm writer!"); |
| 561 | abort(); |
| 562 | return; |
| 563 | } |
| 564 | |
| 565 | /// runOnMachineFunction - This uses the printMachineInstruction() |
| 566 | /// method to print assembly for each instruction. |
| 567 | /// |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 568 | bool PPCLinuxAsmPrinter::runOnMachineFunction(MachineFunction &MF) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 569 | |
| 570 | SetupMachineFunction(MF); |
| 571 | O << "\n\n"; |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 572 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 573 | // Print out constants referenced by the function |
| 574 | EmitConstantPool(MF.getConstantPool()); |
| 575 | |
| 576 | // Print out labels for the function. |
| 577 | const Function *F = MF.getFunction(); |
| 578 | SwitchToTextSection(getSectionForFunction(*F).c_str(), F); |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 579 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 580 | switch (F->getLinkage()) { |
| 581 | default: assert(0 && "Unknown linkage type!"); |
| 582 | case Function::InternalLinkage: // Symbols default to internal. |
| 583 | break; |
| 584 | case Function::ExternalLinkage: |
| 585 | O << "\t.global\t" << CurrentFnName << '\n' |
| 586 | << "\t.type\t" << CurrentFnName << ", @function\n"; |
| 587 | break; |
| 588 | case Function::WeakLinkage: |
| 589 | case Function::LinkOnceLinkage: |
| 590 | O << "\t.global\t" << CurrentFnName << '\n'; |
| 591 | O << "\t.weak\t" << CurrentFnName << '\n'; |
| 592 | break; |
| 593 | } |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 594 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 595 | if (F->hasHiddenVisibility()) |
| 596 | if (const char *Directive = TAI->getHiddenDirective()) |
| 597 | O << Directive << CurrentFnName << "\n"; |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 598 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 599 | EmitAlignment(2, F); |
| 600 | O << CurrentFnName << ":\n"; |
| 601 | |
| 602 | // Emit pre-function debug information. |
| 603 | DW.BeginFunction(&MF); |
| 604 | |
| 605 | // Print out code for the function. |
| 606 | for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); |
| 607 | I != E; ++I) { |
| 608 | // Print a label for the basic block. |
| 609 | if (I != MF.begin()) { |
Evan Cheng | 45c1edb | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 610 | printBasicBlockLabel(I, true, true); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 611 | O << '\n'; |
| 612 | } |
| 613 | for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end(); |
| 614 | II != E; ++II) { |
| 615 | // Print the assembly for the instruction. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 616 | printMachineInstruction(II); |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | O << "\t.size\t" << CurrentFnName << ",.-" << CurrentFnName << "\n"; |
| 621 | |
| 622 | // Print out jump tables referenced by the function. |
| 623 | EmitJumpTableInfo(MF.getJumpTableInfo(), MF); |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 624 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 625 | // Emit post-function debug information. |
| 626 | DW.EndFunction(); |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 627 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 628 | // We didn't modify anything. |
| 629 | return false; |
| 630 | } |
| 631 | |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 632 | bool PPCLinuxAsmPrinter::doInitialization(Module &M) { |
Dan Gohman | 4a558a3 | 2007-07-25 19:33:14 +0000 | [diff] [blame] | 633 | bool Result = AsmPrinter::doInitialization(M); |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 634 | |
Dale Johannesen | 2f6aa07 | 2008-07-09 21:24:07 +0000 | [diff] [blame] | 635 | // Emit initial debug information. |
| 636 | DW.BeginModule(&M); |
| 637 | |
| 638 | // AsmPrinter::doInitialization should have done this analysis. |
| 639 | MMI = getAnalysisToUpdate<MachineModuleInfo>(); |
| 640 | assert(MMI); |
| 641 | DW.SetModuleInfo(MMI); |
| 642 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 643 | // GNU as handles section names wrapped in quotes |
| 644 | Mang->setUseQuotes(true); |
| 645 | |
| 646 | SwitchToTextSection(TAI->getTextSection()); |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 647 | |
Dan Gohman | 4a558a3 | 2007-07-25 19:33:14 +0000 | [diff] [blame] | 648 | return Result; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 649 | } |
| 650 | |
Chris Lattner | 2b638a7 | 2008-02-15 19:04:54 +0000 | [diff] [blame] | 651 | /// PrintUnmangledNameSafely - Print out the printable characters in the name. |
| 652 | /// Don't print things like \n or \0. |
| 653 | static void PrintUnmangledNameSafely(const Value *V, std::ostream &OS) { |
| 654 | for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen(); |
| 655 | Name != E; ++Name) |
| 656 | if (isprint(*Name)) |
| 657 | OS << *Name; |
| 658 | } |
| 659 | |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 660 | bool PPCLinuxAsmPrinter::doFinalization(Module &M) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 661 | const TargetData *TD = TM.getTargetData(); |
| 662 | |
| 663 | // Print out module-level global variables here. |
| 664 | for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); |
| 665 | I != E; ++I) { |
| 666 | if (!I->hasInitializer()) continue; // External global require no code |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 667 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 668 | // Check to see if this is a special global used by LLVM, if so, emit it. |
| 669 | if (EmitSpecialLLVMGlobal(I)) |
| 670 | continue; |
| 671 | |
| 672 | std::string name = Mang->getValueName(I); |
| 673 | |
| 674 | if (I->hasHiddenVisibility()) |
| 675 | if (const char *Directive = TAI->getHiddenDirective()) |
| 676 | O << Directive << name << "\n"; |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 677 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 678 | Constant *C = I->getInitializer(); |
Duncan Sands | 8157ef4 | 2007-11-05 00:04:43 +0000 | [diff] [blame] | 679 | unsigned Size = TD->getABITypeSize(C->getType()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 680 | unsigned Align = TD->getPreferredAlignmentLog(I); |
| 681 | |
| 682 | if (C->isNullValue() && /* FIXME: Verify correct */ |
Dale Johannesen | 49c4412 | 2008-05-14 20:12:51 +0000 | [diff] [blame] | 683 | !I->hasSection() && (I->hasCommonLinkage() || |
| 684 | I->hasInternalLinkage() || I->hasWeakLinkage() || |
Evan Cheng | 65c0fbc | 2007-09-21 00:41:19 +0000 | [diff] [blame] | 685 | I->hasLinkOnceLinkage() || I->hasExternalLinkage())) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 686 | if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it. |
| 687 | if (I->hasExternalLinkage()) { |
| 688 | O << "\t.global " << name << '\n'; |
| 689 | O << "\t.type " << name << ", @object\n"; |
Nick Lewycky | c658375 | 2007-11-04 17:32:10 +0000 | [diff] [blame] | 690 | if (TAI->getBSSSection()) |
| 691 | SwitchToDataSection(TAI->getBSSSection(), I); |
Nick Lewycky | 3246a9c | 2007-07-25 03:48:45 +0000 | [diff] [blame] | 692 | O << name << ":\n"; |
| 693 | O << "\t.zero " << Size << "\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 694 | } else if (I->hasInternalLinkage()) { |
| 695 | SwitchToDataSection("\t.data", I); |
| 696 | O << TAI->getLCOMMDirective() << name << "," << Size; |
| 697 | } else { |
| 698 | SwitchToDataSection("\t.data", I); |
| 699 | O << ".comm " << name << "," << Size; |
| 700 | } |
Chris Lattner | 2b638a7 | 2008-02-15 19:04:54 +0000 | [diff] [blame] | 701 | O << "\t\t" << TAI->getCommentString() << " '"; |
| 702 | PrintUnmangledNameSafely(I, O); |
| 703 | O << "'\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 704 | } else { |
| 705 | switch (I->getLinkage()) { |
| 706 | case GlobalValue::LinkOnceLinkage: |
| 707 | case GlobalValue::WeakLinkage: |
Dale Johannesen | 49c4412 | 2008-05-14 20:12:51 +0000 | [diff] [blame] | 708 | case GlobalValue::CommonLinkage: |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 709 | O << "\t.global " << name << '\n' |
| 710 | << "\t.type " << name << ", @object\n" |
| 711 | << "\t.weak " << name << '\n'; |
| 712 | SwitchToDataSection("\t.data", I); |
| 713 | break; |
| 714 | case GlobalValue::AppendingLinkage: |
| 715 | // FIXME: appending linkage variables should go into a section of |
| 716 | // their name or something. For now, just emit them as external. |
| 717 | case GlobalValue::ExternalLinkage: |
| 718 | // If external or appending, declare as a global symbol |
| 719 | O << "\t.global " << name << "\n" |
| 720 | << "\t.type " << name << ", @object\n"; |
| 721 | // FALL THROUGH |
| 722 | case GlobalValue::InternalLinkage: |
| 723 | if (I->isConstant()) { |
| 724 | const ConstantArray *CVA = dyn_cast<ConstantArray>(C); |
| 725 | if (TAI->getCStringSection() && CVA && CVA->isCString()) { |
| 726 | SwitchToDataSection(TAI->getCStringSection(), I); |
| 727 | break; |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | // FIXME: special handling for ".ctors" & ".dtors" sections |
| 732 | if (I->hasSection() && |
| 733 | (I->getSection() == ".ctors" || |
| 734 | I->getSection() == ".dtors")) { |
| 735 | std::string SectionName = ".section " + I->getSection() |
| 736 | + ",\"aw\",@progbits"; |
| 737 | SwitchToDataSection(SectionName.c_str()); |
| 738 | } else { |
| 739 | if (I->isConstant() && TAI->getReadOnlySection()) |
| 740 | SwitchToDataSection(TAI->getReadOnlySection(), I); |
| 741 | else |
| 742 | SwitchToDataSection(TAI->getDataSection(), I); |
| 743 | } |
| 744 | break; |
| 745 | default: |
| 746 | cerr << "Unknown linkage type!"; |
| 747 | abort(); |
| 748 | } |
| 749 | |
| 750 | EmitAlignment(Align, I); |
Chris Lattner | 2b638a7 | 2008-02-15 19:04:54 +0000 | [diff] [blame] | 751 | O << name << ":\t\t\t\t" << TAI->getCommentString() << " '"; |
| 752 | PrintUnmangledNameSafely(I, O); |
| 753 | O << "'\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 754 | |
| 755 | // If the initializer is a extern weak symbol, remember to emit the weak |
| 756 | // reference! |
| 757 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(C)) |
| 758 | if (GV->hasExternalWeakLinkage()) |
| 759 | ExtWeakSymbols.insert(GV); |
| 760 | |
| 761 | EmitGlobalConstant(C); |
| 762 | O << '\n'; |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | // TODO |
| 767 | |
| 768 | // Emit initial debug information. |
| 769 | DW.EndModule(); |
| 770 | |
Dan Gohman | 4a558a3 | 2007-07-25 19:33:14 +0000 | [diff] [blame] | 771 | return AsmPrinter::doFinalization(M); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 772 | } |
| 773 | |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 774 | std::string PPCLinuxAsmPrinter::getSectionForFunction(const Function &F) const { |
Anton Korobeynikov | 248e9c5 | 2008-08-08 18:23:25 +0000 | [diff] [blame^] | 775 | return TAI->SectionForGlobal(&F); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 776 | } |
| 777 | |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 778 | std::string PPCDarwinAsmPrinter::getSectionForFunction(const Function &F) const { |
Anton Korobeynikov | 248e9c5 | 2008-08-08 18:23:25 +0000 | [diff] [blame^] | 779 | return TAI->SectionForGlobal(&F); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | /// runOnMachineFunction - This uses the printMachineInstruction() |
| 783 | /// method to print assembly for each instruction. |
| 784 | /// |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 785 | bool PPCDarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 786 | |
| 787 | SetupMachineFunction(MF); |
| 788 | O << "\n\n"; |
Dale Johannesen | fb3ac73 | 2007-11-20 23:24:42 +0000 | [diff] [blame] | 789 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 790 | // Print out constants referenced by the function |
| 791 | EmitConstantPool(MF.getConstantPool()); |
| 792 | |
| 793 | // Print out labels for the function. |
| 794 | const Function *F = MF.getFunction(); |
| 795 | SwitchToTextSection(getSectionForFunction(*F).c_str(), F); |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 796 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 797 | switch (F->getLinkage()) { |
| 798 | default: assert(0 && "Unknown linkage type!"); |
| 799 | case Function::InternalLinkage: // Symbols default to internal. |
| 800 | break; |
| 801 | case Function::ExternalLinkage: |
| 802 | O << "\t.globl\t" << CurrentFnName << "\n"; |
| 803 | break; |
| 804 | case Function::WeakLinkage: |
| 805 | case Function::LinkOnceLinkage: |
| 806 | O << "\t.globl\t" << CurrentFnName << "\n"; |
| 807 | O << "\t.weak_definition\t" << CurrentFnName << "\n"; |
| 808 | break; |
| 809 | } |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 810 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 811 | if (F->hasHiddenVisibility()) |
| 812 | if (const char *Directive = TAI->getHiddenDirective()) |
| 813 | O << Directive << CurrentFnName << "\n"; |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 814 | |
Evan Cheng | 2e8d3d4 | 2008-03-25 22:29:46 +0000 | [diff] [blame] | 815 | EmitAlignment(OptimizeForSize ? 2 : 4, F); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 816 | O << CurrentFnName << ":\n"; |
| 817 | |
| 818 | // Emit pre-function debug information. |
| 819 | DW.BeginFunction(&MF); |
| 820 | |
Bill Wendling | 36ccaea | 2008-01-26 06:51:24 +0000 | [diff] [blame] | 821 | // If the function is empty, then we need to emit *something*. Otherwise, the |
| 822 | // function's label might be associated with something that it wasn't meant to |
| 823 | // be associated with. We emit a noop in this situation. |
| 824 | MachineFunction::iterator I = MF.begin(); |
| 825 | |
Bill Wendling | b5880a7 | 2008-01-26 09:03:52 +0000 | [diff] [blame] | 826 | if (++I == MF.end() && MF.front().empty()) |
| 827 | O << "\tnop\n"; |
Bill Wendling | 36ccaea | 2008-01-26 06:51:24 +0000 | [diff] [blame] | 828 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 829 | // Print out code for the function. |
| 830 | for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); |
| 831 | I != E; ++I) { |
| 832 | // Print a label for the basic block. |
| 833 | if (I != MF.begin()) { |
Evan Cheng | 45c1edb | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 834 | printBasicBlockLabel(I, true, true); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 835 | O << '\n'; |
| 836 | } |
Bill Wendling | 36ccaea | 2008-01-26 06:51:24 +0000 | [diff] [blame] | 837 | for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end(); |
| 838 | II != IE; ++II) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 839 | // Print the assembly for the instruction. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 840 | printMachineInstruction(II); |
| 841 | } |
| 842 | } |
| 843 | |
| 844 | // Print out jump tables referenced by the function. |
| 845 | EmitJumpTableInfo(MF.getJumpTableInfo(), MF); |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 846 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 847 | // Emit post-function debug information. |
| 848 | DW.EndFunction(); |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 849 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 850 | // We didn't modify anything. |
| 851 | return false; |
| 852 | } |
| 853 | |
| 854 | |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 855 | bool PPCDarwinAsmPrinter::doInitialization(Module &M) { |
Dan Gohman | 12300e1 | 2008-03-25 21:45:14 +0000 | [diff] [blame] | 856 | static const char *const CPUDirectives[] = { |
Dale Johannesen | 161badc | 2008-02-14 23:35:16 +0000 | [diff] [blame] | 857 | "", |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 858 | "ppc", |
| 859 | "ppc601", |
| 860 | "ppc602", |
| 861 | "ppc603", |
| 862 | "ppc7400", |
| 863 | "ppc750", |
| 864 | "ppc970", |
| 865 | "ppc64" |
| 866 | }; |
| 867 | |
| 868 | unsigned Directive = Subtarget.getDarwinDirective(); |
| 869 | if (Subtarget.isGigaProcessor() && Directive < PPC::DIR_970) |
| 870 | Directive = PPC::DIR_970; |
| 871 | if (Subtarget.hasAltivec() && Directive < PPC::DIR_7400) |
| 872 | Directive = PPC::DIR_7400; |
| 873 | if (Subtarget.isPPC64() && Directive < PPC::DIR_970) |
| 874 | Directive = PPC::DIR_64; |
| 875 | assert(Directive <= PPC::DIR_64 && "Directive out of range."); |
| 876 | O << "\t.machine " << CPUDirectives[Directive] << "\n"; |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 877 | |
Dan Gohman | 4a558a3 | 2007-07-25 19:33:14 +0000 | [diff] [blame] | 878 | bool Result = AsmPrinter::doInitialization(M); |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 879 | |
Dale Johannesen | 58e0eeb | 2008-07-09 20:43:39 +0000 | [diff] [blame] | 880 | // Emit initial debug information. |
| 881 | DW.BeginModule(&M); |
| 882 | |
| 883 | // We need this for Personality functions. |
| 884 | // AsmPrinter::doInitialization should have done this analysis. |
| 885 | MMI = getAnalysisToUpdate<MachineModuleInfo>(); |
| 886 | assert(MMI); |
| 887 | DW.SetModuleInfo(MMI); |
| 888 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 889 | // Darwin wants symbols to be quoted if they have complex names. |
| 890 | Mang->setUseQuotes(true); |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 891 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 892 | // Prime text sections so they are adjacent. This reduces the likelihood a |
| 893 | // large data or debug section causes a branch to exceed 16M limit. |
Dale Johannesen | 3c78832 | 2008-01-11 00:54:37 +0000 | [diff] [blame] | 894 | SwitchToTextSection("\t.section __TEXT,__textcoal_nt,coalesced," |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 895 | "pure_instructions"); |
| 896 | if (TM.getRelocationModel() == Reloc::PIC_) { |
Dale Johannesen | 3c78832 | 2008-01-11 00:54:37 +0000 | [diff] [blame] | 897 | SwitchToTextSection("\t.section __TEXT,__picsymbolstub1,symbol_stubs," |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 898 | "pure_instructions,32"); |
| 899 | } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) { |
Dale Johannesen | 3c78832 | 2008-01-11 00:54:37 +0000 | [diff] [blame] | 900 | SwitchToTextSection("\t.section __TEXT,__symbol_stub1,symbol_stubs," |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 901 | "pure_instructions,16"); |
| 902 | } |
| 903 | SwitchToTextSection(TAI->getTextSection()); |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 904 | |
Dan Gohman | 4a558a3 | 2007-07-25 19:33:14 +0000 | [diff] [blame] | 905 | return Result; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 906 | } |
| 907 | |
Anton Korobeynikov | 248e9c5 | 2008-08-08 18:23:25 +0000 | [diff] [blame^] | 908 | void PPCDarwinAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) { |
| 909 | const TargetData *TD = TM.getTargetData(); |
| 910 | |
| 911 | if (!GVar->hasInitializer()) |
| 912 | return; // External global require no code |
| 913 | |
| 914 | // Check to see if this is a special global used by LLVM, if so, emit it. |
| 915 | if (EmitSpecialLLVMGlobal(GVar)) { |
| 916 | if (TM.getRelocationModel() == Reloc::Static) { |
| 917 | if (GVar->getName() == "llvm.global_ctors") |
| 918 | O << ".reference .constructors_used\n"; |
| 919 | else if (GVar->getName() == "llvm.global_dtors") |
| 920 | O << ".reference .destructors_used\n"; |
| 921 | } |
| 922 | return; |
| 923 | } |
| 924 | |
| 925 | std::string name = Mang->getValueName(GVar); |
| 926 | std::string SectionName = TAI->SectionForGlobal(GVar); |
| 927 | |
| 928 | if (GVar->hasHiddenVisibility()) |
| 929 | if (const char *Directive = TAI->getHiddenDirective()) |
| 930 | O << Directive << name << "\n"; |
| 931 | |
| 932 | Constant *C = GVar->getInitializer(); |
| 933 | const Type *Type = C->getType(); |
| 934 | unsigned Size = TD->getABITypeSize(Type); |
| 935 | unsigned Align = TD->getPreferredAlignmentLog(GVar); |
| 936 | |
| 937 | SwitchToDataSection(SectionName.c_str()); |
| 938 | |
| 939 | if (C->isNullValue() && /* FIXME: Verify correct */ |
| 940 | !GVar->hasSection() && |
| 941 | (GVar->hasInternalLinkage() || GVar->hasExternalLinkage() || |
| 942 | GVar->isWeakForLinker())) { |
| 943 | if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it. |
| 944 | |
| 945 | if (GVar->hasExternalLinkage()) { |
| 946 | O << "\t.globl " << name << '\n'; |
| 947 | O << "\t.zerofill __DATA, __common, " << name << ", " |
| 948 | << Size << ", " << Align; |
| 949 | } else if (GVar->hasInternalLinkage()) { |
| 950 | O << TAI->getLCOMMDirective() << name << "," << Size << "," << Align; |
| 951 | } else if (!GVar->hasCommonLinkage()) { |
| 952 | O << "\t.globl " << name << "\n" |
| 953 | << TAI->getWeakDefDirective() << name << "\n"; |
| 954 | EmitAlignment(Align, GVar); |
| 955 | O << name << ":\t\t\t\t" << TAI->getCommentString() << " "; |
| 956 | PrintUnmangledNameSafely(GVar, O); |
| 957 | O << "\n"; |
| 958 | EmitGlobalConstant(C); |
| 959 | return; |
| 960 | } else { |
| 961 | O << ".comm " << name << "," << Size; |
| 962 | // Darwin 9 and above support aligned common data. |
| 963 | if (Subtarget.isDarwin9()) |
| 964 | O << "," << Align; |
| 965 | } |
| 966 | O << "\t\t" << TAI->getCommentString() << " '"; |
| 967 | PrintUnmangledNameSafely(GVar, O); |
| 968 | O << "'\n"; |
| 969 | return; |
| 970 | } |
| 971 | |
| 972 | switch (GVar->getLinkage()) { |
| 973 | case GlobalValue::LinkOnceLinkage: |
| 974 | case GlobalValue::WeakLinkage: |
| 975 | case GlobalValue::CommonLinkage: |
| 976 | O << "\t.globl " << name << '\n' |
| 977 | << "\t.weak_definition " << name << '\n'; |
| 978 | break; |
| 979 | case GlobalValue::AppendingLinkage: |
| 980 | // FIXME: appending linkage variables should go into a section of |
| 981 | // their name or something. For now, just emit them as external. |
| 982 | case GlobalValue::ExternalLinkage: |
| 983 | // If external or appending, declare as a global symbol |
| 984 | O << "\t.globl " << name << "\n"; |
| 985 | // FALL THROUGH |
| 986 | case GlobalValue::InternalLinkage: |
| 987 | break; |
| 988 | default: |
| 989 | cerr << "Unknown linkage type!"; |
| 990 | abort(); |
| 991 | } |
| 992 | |
| 993 | EmitAlignment(Align, GVar); |
| 994 | O << name << ":\t\t\t\t" << TAI->getCommentString() << " '"; |
| 995 | PrintUnmangledNameSafely(GVar, O); |
| 996 | O << "'\n"; |
| 997 | |
| 998 | // If the initializer is a extern weak symbol, remember to emit the weak |
| 999 | // reference! |
| 1000 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(C)) |
| 1001 | if (GV->hasExternalWeakLinkage()) |
| 1002 | ExtWeakSymbols.insert(GV); |
| 1003 | |
| 1004 | EmitGlobalConstant(C); |
| 1005 | O << '\n'; |
| 1006 | } |
| 1007 | |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 1008 | bool PPCDarwinAsmPrinter::doFinalization(Module &M) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1009 | const TargetData *TD = TM.getTargetData(); |
| 1010 | |
| 1011 | // Print out module-level global variables here. |
| 1012 | for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); |
Anton Korobeynikov | 248e9c5 | 2008-08-08 18:23:25 +0000 | [diff] [blame^] | 1013 | I != E; ++I) |
| 1014 | printModuleLevelGV(I); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1015 | |
| 1016 | bool isPPC64 = TD->getPointerSizeInBits() == 64; |
| 1017 | |
| 1018 | // Output stubs for dynamically-linked functions |
| 1019 | if (TM.getRelocationModel() == Reloc::PIC_) { |
| 1020 | for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end(); |
| 1021 | i != e; ++i) { |
Dale Johannesen | 3c78832 | 2008-01-11 00:54:37 +0000 | [diff] [blame] | 1022 | SwitchToTextSection("\t.section __TEXT,__picsymbolstub1,symbol_stubs," |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1023 | "pure_instructions,32"); |
| 1024 | EmitAlignment(4); |
Dale Johannesen | a21b520 | 2008-05-19 21:38:18 +0000 | [diff] [blame] | 1025 | std::string p = *i; |
| 1026 | std::string L0p = (p[0]=='\"') ? "\"L0$" + p.substr(1) : "L0$" + p ; |
| 1027 | printSuffixedName(p, "$stub"); |
| 1028 | O << ":\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1029 | O << "\t.indirect_symbol " << *i << "\n"; |
| 1030 | O << "\tmflr r0\n"; |
Dale Johannesen | a21b520 | 2008-05-19 21:38:18 +0000 | [diff] [blame] | 1031 | O << "\tbcl 20,31," << L0p << "\n"; |
| 1032 | O << L0p << ":\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1033 | O << "\tmflr r11\n"; |
Dale Johannesen | a21b520 | 2008-05-19 21:38:18 +0000 | [diff] [blame] | 1034 | O << "\taddis r11,r11,ha16("; |
| 1035 | printSuffixedName(p, "$lazy_ptr"); |
| 1036 | O << "-" << L0p << ")\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1037 | O << "\tmtlr r0\n"; |
| 1038 | if (isPPC64) |
Dale Johannesen | a21b520 | 2008-05-19 21:38:18 +0000 | [diff] [blame] | 1039 | O << "\tldu r12,lo16("; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1040 | else |
Dale Johannesen | a21b520 | 2008-05-19 21:38:18 +0000 | [diff] [blame] | 1041 | O << "\tlwzu r12,lo16("; |
| 1042 | printSuffixedName(p, "$lazy_ptr"); |
| 1043 | O << "-" << L0p << ")(r11)\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1044 | O << "\tmtctr r12\n"; |
| 1045 | O << "\tbctr\n"; |
| 1046 | SwitchToDataSection(".lazy_symbol_pointer"); |
Dale Johannesen | a21b520 | 2008-05-19 21:38:18 +0000 | [diff] [blame] | 1047 | printSuffixedName(p, "$lazy_ptr"); |
| 1048 | O << ":\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1049 | O << "\t.indirect_symbol " << *i << "\n"; |
| 1050 | if (isPPC64) |
| 1051 | O << "\t.quad dyld_stub_binding_helper\n"; |
| 1052 | else |
| 1053 | O << "\t.long dyld_stub_binding_helper\n"; |
| 1054 | } |
| 1055 | } else { |
| 1056 | for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end(); |
| 1057 | i != e; ++i) { |
Dale Johannesen | 3c78832 | 2008-01-11 00:54:37 +0000 | [diff] [blame] | 1058 | SwitchToTextSection("\t.section __TEXT,__symbol_stub1,symbol_stubs," |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1059 | "pure_instructions,16"); |
| 1060 | EmitAlignment(4); |
Dale Johannesen | a21b520 | 2008-05-19 21:38:18 +0000 | [diff] [blame] | 1061 | std::string p = *i; |
| 1062 | printSuffixedName(p, "$stub"); |
| 1063 | O << ":\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1064 | O << "\t.indirect_symbol " << *i << "\n"; |
Dale Johannesen | a21b520 | 2008-05-19 21:38:18 +0000 | [diff] [blame] | 1065 | O << "\tlis r11,ha16("; |
| 1066 | printSuffixedName(p, "$lazy_ptr"); |
| 1067 | O << ")\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1068 | if (isPPC64) |
Dale Johannesen | a21b520 | 2008-05-19 21:38:18 +0000 | [diff] [blame] | 1069 | O << "\tldu r12,lo16("; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1070 | else |
Dale Johannesen | a21b520 | 2008-05-19 21:38:18 +0000 | [diff] [blame] | 1071 | O << "\tlwzu r12,lo16("; |
| 1072 | printSuffixedName(p, "$lazy_ptr"); |
| 1073 | O << ")(r11)\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1074 | O << "\tmtctr r12\n"; |
| 1075 | O << "\tbctr\n"; |
| 1076 | SwitchToDataSection(".lazy_symbol_pointer"); |
Dale Johannesen | a21b520 | 2008-05-19 21:38:18 +0000 | [diff] [blame] | 1077 | printSuffixedName(p, "$lazy_ptr"); |
| 1078 | O << ":\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1079 | O << "\t.indirect_symbol " << *i << "\n"; |
| 1080 | if (isPPC64) |
| 1081 | O << "\t.quad dyld_stub_binding_helper\n"; |
| 1082 | else |
| 1083 | O << "\t.long dyld_stub_binding_helper\n"; |
| 1084 | } |
| 1085 | } |
| 1086 | |
| 1087 | O << "\n"; |
| 1088 | |
Dale Johannesen | 8553576 | 2008-04-02 00:25:04 +0000 | [diff] [blame] | 1089 | if (TAI->doesSupportExceptionHandling() && MMI) { |
Dale Johannesen | fb3ac73 | 2007-11-20 23:24:42 +0000 | [diff] [blame] | 1090 | // Add the (possibly multiple) personalities to the set of global values. |
Dale Johannesen | 8553576 | 2008-04-02 00:25:04 +0000 | [diff] [blame] | 1091 | // Only referenced functions get into the Personalities list. |
Dale Johannesen | fb3ac73 | 2007-11-20 23:24:42 +0000 | [diff] [blame] | 1092 | const std::vector<Function *>& Personalities = MMI->getPersonalities(); |
| 1093 | |
| 1094 | for (std::vector<Function *>::const_iterator I = Personalities.begin(), |
| 1095 | E = Personalities.end(); I != E; ++I) |
| 1096 | if (*I) GVStubs.insert("_" + (*I)->getName()); |
| 1097 | } |
| 1098 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1099 | // Output stubs for external and common global variables. |
Dan Gohman | 3f7d94b | 2007-10-03 19:26:29 +0000 | [diff] [blame] | 1100 | if (!GVStubs.empty()) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1101 | SwitchToDataSection(".non_lazy_symbol_pointer"); |
| 1102 | for (std::set<std::string>::iterator I = GVStubs.begin(), |
| 1103 | E = GVStubs.end(); I != E; ++I) { |
Dale Johannesen | a21b520 | 2008-05-19 21:38:18 +0000 | [diff] [blame] | 1104 | std::string p = *I; |
| 1105 | printSuffixedName(p, "$non_lazy_ptr"); |
| 1106 | O << ":\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1107 | O << "\t.indirect_symbol " << *I << "\n"; |
| 1108 | if (isPPC64) |
| 1109 | O << "\t.quad\t0\n"; |
| 1110 | else |
| 1111 | O << "\t.long\t0\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1112 | } |
| 1113 | } |
| 1114 | |
| 1115 | // Emit initial debug information. |
| 1116 | DW.EndModule(); |
| 1117 | |
| 1118 | // Funny Darwin hack: This flag tells the linker that no global symbols |
| 1119 | // contain code that falls through to other global symbols (e.g. the obvious |
| 1120 | // implementation of multiple entry points). If this doesn't occur, the |
| 1121 | // linker can safely perform dead code stripping. Since LLVM never generates |
| 1122 | // code that does this, it is always safe to set. |
| 1123 | O << "\t.subsections_via_symbols\n"; |
| 1124 | |
Dan Gohman | 4a558a3 | 2007-07-25 19:33:14 +0000 | [diff] [blame] | 1125 | return AsmPrinter::doFinalization(M); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | |
| 1129 | |
| 1130 | /// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code |
| 1131 | /// for a MachineFunction to the given output stream, in a format that the |
| 1132 | /// Darwin assembler can deal with. |
| 1133 | /// |
| 1134 | FunctionPass *llvm::createPPCAsmPrinterPass(std::ostream &o, |
| 1135 | PPCTargetMachine &tm) { |
| 1136 | const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>(); |
| 1137 | |
| 1138 | if (Subtarget->isDarwin()) { |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 1139 | return new PPCDarwinAsmPrinter(o, tm, tm.getTargetAsmInfo()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1140 | } else { |
Anton Korobeynikov | 28f86d1 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 1141 | return new PPCLinuxAsmPrinter(o, tm, tm.getTargetAsmInfo()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1142 | } |
| 1143 | } |