Nate Begeman | 21e463b | 2005-10-16 05:39:50 +0000 | [diff] [blame] | 1 | //===-- PPCAsmPrinter.cpp - Print machine instrs to PowerPC assembly --------=// |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 2 | // |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 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. |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 7 | // |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 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 |
Chris Lattner | 83660c5 | 2004-07-28 20:18:53 +0000 | [diff] [blame] | 12 | // the output mechanism used by `llc'. |
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" |
Chris Lattner | 2668959 | 2005-10-14 23:51:18 +0000 | [diff] [blame] | 20 | #include "PPC.h" |
Chris Lattner | df4ed63 | 2006-11-17 22:10:59 +0000 | [diff] [blame] | 21 | #include "PPCPredicates.h" |
Chris Lattner | 16e71f2 | 2005-10-14 23:59:06 +0000 | [diff] [blame] | 22 | #include "PPCTargetMachine.h" |
Chris Lattner | 2668959 | 2005-10-14 23:51:18 +0000 | [diff] [blame] | 23 | #include "PPCSubtarget.h" |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 24 | #include "llvm/Constants.h" |
| 25 | #include "llvm/DerivedTypes.h" |
| 26 | #include "llvm/Module.h" |
| 27 | #include "llvm/Assembly/Writer.h" |
Chris Lattner | a384079 | 2004-08-16 23:25:21 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/AsmPrinter.h" |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/DwarfWriter.h" |
Jim Laskey | 44c3b9f | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Misha Brukman | 0579449 | 2004-06-24 17:31:42 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/MachineInstr.h" |
Bill Wendling | 381802f | 2008-01-26 06:51:24 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Chris Lattner | f9bdedd | 2009-08-10 18:15:01 +0000 | [diff] [blame] | 34 | #include "llvm/MC/MCSectionMachO.h" |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 35 | #include "llvm/MC/MCStreamer.h" |
Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 36 | #include "llvm/MC/MCAsmInfo.h" |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 37 | #include "llvm/Target/TargetLoweringObjectFile.h" |
| 38 | #include "llvm/Target/TargetRegisterInfo.h" |
| 39 | #include "llvm/Target/TargetInstrInfo.h" |
| 40 | #include "llvm/Target/TargetOptions.h" |
| 41 | #include "llvm/Target/TargetRegistry.h" |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 42 | #include "llvm/Support/Mangler.h" |
Nate Begeman | a11c2e8 | 2004-09-04 14:51:26 +0000 | [diff] [blame] | 43 | #include "llvm/Support/MathExtras.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 44 | #include "llvm/Support/CommandLine.h" |
| 45 | #include "llvm/Support/Debug.h" |
Torok Edwin | dac237e | 2009-07-08 20:53:28 +0000 | [diff] [blame] | 46 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | a4f0b3a | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 47 | #include "llvm/Support/Compiler.h" |
David Greene | 7184781 | 2009-07-14 20:18:05 +0000 | [diff] [blame] | 48 | #include "llvm/Support/FormattedStream.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 49 | #include "llvm/ADT/Statistic.h" |
| 50 | #include "llvm/ADT/StringExtras.h" |
Evan Cheng | ae94e59 | 2008-12-05 01:06:39 +0000 | [diff] [blame] | 51 | #include "llvm/ADT/StringSet.h" |
Chris Lattner | a384079 | 2004-08-16 23:25:21 +0000 | [diff] [blame] | 52 | using namespace llvm; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 53 | |
Chris Lattner | 95b2c7d | 2006-12-19 22:59:26 +0000 | [diff] [blame] | 54 | STATISTIC(EmittedInsts, "Number of machine instrs printed"); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 55 | |
Chris Lattner | 95b2c7d | 2006-12-19 22:59:26 +0000 | [diff] [blame] | 56 | namespace { |
Bill Wendling | 57f0db8 | 2009-02-24 08:30:20 +0000 | [diff] [blame] | 57 | class VISIBILITY_HIDDEN PPCAsmPrinter : public AsmPrinter { |
| 58 | protected: |
Chris Lattner | 9ec8fac | 2009-07-15 02:28:57 +0000 | [diff] [blame] | 59 | struct FnStubInfo { |
| 60 | std::string Stub, LazyPtr, AnonSymbol; |
| 61 | |
| 62 | FnStubInfo() {} |
| 63 | |
| 64 | void Init(const GlobalValue *GV, Mangler *Mang) { |
| 65 | // Already initialized. |
| 66 | if (!Stub.empty()) return; |
| 67 | Stub = Mang->getMangledName(GV, "$stub", true); |
| 68 | LazyPtr = Mang->getMangledName(GV, "$lazy_ptr", true); |
| 69 | AnonSymbol = Mang->getMangledName(GV, "$stub$tmp", true); |
| 70 | } |
| 71 | |
| 72 | void Init(const std::string &GV, Mangler *Mang) { |
| 73 | // Already initialized. |
| 74 | if (!Stub.empty()) return; |
Bill Wendling | 3d10a5a | 2009-07-20 01:03:30 +0000 | [diff] [blame] | 75 | Stub = Mang->makeNameProper(GV + "$stub", |
Bill Wendling | 5c0ba80 | 2009-07-20 19:41:27 +0000 | [diff] [blame] | 76 | Mangler::Private); |
Bill Wendling | 3d10a5a | 2009-07-20 01:03:30 +0000 | [diff] [blame] | 77 | LazyPtr = Mang->makeNameProper(GV + "$lazy_ptr", |
Bill Wendling | 5c0ba80 | 2009-07-20 19:41:27 +0000 | [diff] [blame] | 78 | Mangler::Private); |
Bill Wendling | 3d10a5a | 2009-07-20 01:03:30 +0000 | [diff] [blame] | 79 | AnonSymbol = Mang->makeNameProper(GV + "$stub$tmp", |
Bill Wendling | 5c0ba80 | 2009-07-20 19:41:27 +0000 | [diff] [blame] | 80 | Mangler::Private); |
Chris Lattner | 9ec8fac | 2009-07-15 02:28:57 +0000 | [diff] [blame] | 81 | } |
| 82 | }; |
| 83 | |
| 84 | StringMap<FnStubInfo> FnStubs; |
Tilmann Scheller | 6b16eff | 2009-08-15 11:54:46 +0000 | [diff] [blame] | 85 | StringMap<std::string> GVStubs, HiddenGVStubs, TOC; |
Chris Lattner | dadceed | 2006-09-20 17:07:15 +0000 | [diff] [blame] | 86 | const PPCSubtarget &Subtarget; |
Tilmann Scheller | 6b16eff | 2009-08-15 11:54:46 +0000 | [diff] [blame] | 87 | uint64_t LabelID; |
Bill Wendling | 57f0db8 | 2009-02-24 08:30:20 +0000 | [diff] [blame] | 88 | public: |
David Greene | 7184781 | 2009-07-14 20:18:05 +0000 | [diff] [blame] | 89 | explicit PPCAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM, |
Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 90 | const MCAsmInfo *T, bool V) |
Daniel Dunbar | 5bcc8bd | 2009-07-01 01:48:54 +0000 | [diff] [blame] | 91 | : AsmPrinter(O, TM, T, V), |
Tilmann Scheller | 6b16eff | 2009-08-15 11:54:46 +0000 | [diff] [blame] | 92 | Subtarget(TM.getSubtarget<PPCSubtarget>()), LabelID(0) {} |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 93 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 94 | virtual const char *getPassName() const { |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 95 | return "PowerPC Assembly Printer"; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Nate Begeman | 21e463b | 2005-10-16 05:39:50 +0000 | [diff] [blame] | 98 | PPCTargetMachine &getTM() { |
| 99 | return static_cast<PPCTargetMachine&>(TM); |
Chris Lattner | a384079 | 2004-08-16 23:25:21 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Nate Begeman | adeb43d | 2005-07-20 22:42:00 +0000 | [diff] [blame] | 102 | unsigned enumRegToMachineReg(unsigned enumReg) { |
| 103 | switch (enumReg) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 104 | default: llvm_unreachable("Unhandled register!"); |
Nate Begeman | adeb43d | 2005-07-20 22:42:00 +0000 | [diff] [blame] | 105 | case PPC::CR0: return 0; |
| 106 | case PPC::CR1: return 1; |
| 107 | case PPC::CR2: return 2; |
| 108 | case PPC::CR3: return 3; |
| 109 | case PPC::CR4: return 4; |
| 110 | case PPC::CR5: return 5; |
| 111 | case PPC::CR6: return 6; |
| 112 | case PPC::CR7: return 7; |
| 113 | } |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 114 | llvm_unreachable(0); |
Nate Begeman | adeb43d | 2005-07-20 22:42:00 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Nate Begeman | e59bf59 | 2004-08-14 22:09:10 +0000 | [diff] [blame] | 117 | /// printInstruction - This method is automatically generated by tablegen |
| 118 | /// from the instruction set description. This method returns true if the |
| 119 | /// machine instruction was sufficiently described to print it, otherwise it |
| 120 | /// returns false. |
Chris Lattner | 41aefdc | 2009-08-08 01:32:19 +0000 | [diff] [blame] | 121 | void printInstruction(const MachineInstr *MI); |
Nate Begeman | e59bf59 | 2004-08-14 22:09:10 +0000 | [diff] [blame] | 122 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 123 | void printMachineInstruction(const MachineInstr *MI); |
Chris Lattner | 9ba13e4 | 2005-11-17 19:25:59 +0000 | [diff] [blame] | 124 | void printOp(const MachineOperand &MO); |
Anton Korobeynikov | 34da127 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 125 | |
Jim Laskey | b608a4d | 2006-12-20 20:56:46 +0000 | [diff] [blame] | 126 | /// stripRegisterPrefix - This method strips the character prefix from a |
| 127 | /// register name so that only the number is left. Used by for linux asm. |
Jim Laskey | 40ee69f | 2006-12-20 21:33:34 +0000 | [diff] [blame] | 128 | const char *stripRegisterPrefix(const char *RegName) { |
| 129 | switch (RegName[0]) { |
| 130 | case 'r': |
| 131 | case 'f': |
| 132 | case 'v': return RegName + 1; |
Jim Laskey | 2aa14aa | 2006-12-20 21:35:00 +0000 | [diff] [blame] | 133 | case 'c': if (RegName[1] == 'r') return RegName + 2; |
Jim Laskey | b608a4d | 2006-12-20 20:56:46 +0000 | [diff] [blame] | 134 | } |
Anton Korobeynikov | 34da127 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 135 | |
Jim Laskey | 40ee69f | 2006-12-20 21:33:34 +0000 | [diff] [blame] | 136 | return RegName; |
Jim Laskey | b608a4d | 2006-12-20 20:56:46 +0000 | [diff] [blame] | 137 | } |
Anton Korobeynikov | 34da127 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 138 | |
Jim Laskey | b608a4d | 2006-12-20 20:56:46 +0000 | [diff] [blame] | 139 | /// printRegister - Print register according to target requirements. |
| 140 | /// |
| 141 | void printRegister(const MachineOperand &MO, bool R0AsZero) { |
| 142 | unsigned RegNo = MO.getReg(); |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 143 | assert(TargetRegisterInfo::isPhysicalRegister(RegNo) && "Not physreg??"); |
Anton Korobeynikov | 34da127 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 144 | |
Jim Laskey | b608a4d | 2006-12-20 20:56:46 +0000 | [diff] [blame] | 145 | // If we should use 0 for R0. |
| 146 | if (R0AsZero && RegNo == PPC::R0) { |
| 147 | O << "0"; |
| 148 | return; |
| 149 | } |
Anton Korobeynikov | 34da127 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 150 | |
Bill Wendling | 74ab84c | 2008-02-26 21:11:01 +0000 | [diff] [blame] | 151 | const char *RegName = TM.getRegisterInfo()->get(RegNo).AsmName; |
Jim Laskey | b608a4d | 2006-12-20 20:56:46 +0000 | [diff] [blame] | 152 | // Linux assembler (Others?) does not take register mnemonics. |
| 153 | // FIXME - What about special registers used in mfspr/mtspr? |
Jim Laskey | 40ee69f | 2006-12-20 21:33:34 +0000 | [diff] [blame] | 154 | if (!Subtarget.isDarwin()) RegName = stripRegisterPrefix(RegName); |
| 155 | O << RegName; |
Jim Laskey | b608a4d | 2006-12-20 20:56:46 +0000 | [diff] [blame] | 156 | } |
Chris Lattner | 7bb424f | 2004-08-14 23:27:29 +0000 | [diff] [blame] | 157 | |
Chris Lattner | 5887327 | 2006-02-01 22:38:46 +0000 | [diff] [blame] | 158 | void printOperand(const MachineInstr *MI, unsigned OpNo) { |
Chris Lattner | 7bb424f | 2004-08-14 23:27:29 +0000 | [diff] [blame] | 159 | const MachineOperand &MO = MI->getOperand(OpNo); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 160 | if (MO.isReg()) { |
Jim Laskey | b608a4d | 2006-12-20 20:56:46 +0000 | [diff] [blame] | 161 | printRegister(MO, false); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 162 | } else if (MO.isImm()) { |
Chris Lattner | 9a1ceae | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 163 | O << MO.getImm(); |
Chris Lattner | 7bb424f | 2004-08-14 23:27:29 +0000 | [diff] [blame] | 164 | } else { |
| 165 | printOp(MO); |
| 166 | } |
| 167 | } |
Anton Korobeynikov | 34da127 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 168 | |
Chris Lattner | 5887327 | 2006-02-01 22:38:46 +0000 | [diff] [blame] | 169 | bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, |
Chris Lattner | e3f0157 | 2006-02-23 19:31:10 +0000 | [diff] [blame] | 170 | unsigned AsmVariant, const char *ExtraCode); |
Chris Lattner | 2c003e2 | 2006-02-24 20:27:40 +0000 | [diff] [blame] | 171 | bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo, |
| 172 | unsigned AsmVariant, const char *ExtraCode); |
Anton Korobeynikov | 34da127 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 173 | |
| 174 | |
Chris Lattner | 9c61dcf | 2006-03-25 06:12:06 +0000 | [diff] [blame] | 175 | void printS5ImmOperand(const MachineInstr *MI, unsigned OpNo) { |
Chris Lattner | 9a1ceae | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 176 | char value = MI->getOperand(OpNo).getImm(); |
Chris Lattner | 9c61dcf | 2006-03-25 06:12:06 +0000 | [diff] [blame] | 177 | value = (value << (32-5)) >> (32-5); |
| 178 | O << (int)value; |
| 179 | } |
Nate Begeman | 391c5d2 | 2005-11-30 18:54:35 +0000 | [diff] [blame] | 180 | void printU5ImmOperand(const MachineInstr *MI, unsigned OpNo) { |
Chris Lattner | 9a1ceae | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 181 | unsigned char value = MI->getOperand(OpNo).getImm(); |
Chris Lattner | 12585ba | 2004-08-21 19:11:03 +0000 | [diff] [blame] | 182 | assert(value <= 31 && "Invalid u5imm argument!"); |
Nate Begeman | c330612 | 2004-08-21 05:56:39 +0000 | [diff] [blame] | 183 | O << (unsigned int)value; |
| 184 | } |
Nate Begeman | 391c5d2 | 2005-11-30 18:54:35 +0000 | [diff] [blame] | 185 | void printU6ImmOperand(const MachineInstr *MI, unsigned OpNo) { |
Chris Lattner | 9a1ceae | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 186 | unsigned char value = MI->getOperand(OpNo).getImm(); |
Nate Begeman | 07aada8 | 2004-08-30 02:28:06 +0000 | [diff] [blame] | 187 | assert(value <= 63 && "Invalid u6imm argument!"); |
| 188 | O << (unsigned int)value; |
| 189 | } |
Nate Begeman | 391c5d2 | 2005-11-30 18:54:35 +0000 | [diff] [blame] | 190 | void printS16ImmOperand(const MachineInstr *MI, unsigned OpNo) { |
Chris Lattner | 9a1ceae | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 191 | O << (short)MI->getOperand(OpNo).getImm(); |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 192 | } |
Nate Begeman | 391c5d2 | 2005-11-30 18:54:35 +0000 | [diff] [blame] | 193 | void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo) { |
Chris Lattner | 9a1ceae | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 194 | O << (unsigned short)MI->getOperand(OpNo).getImm(); |
Chris Lattner | 97b2a2e | 2004-08-15 05:20:16 +0000 | [diff] [blame] | 195 | } |
Nate Begeman | 391c5d2 | 2005-11-30 18:54:35 +0000 | [diff] [blame] | 196 | void printS16X4ImmOperand(const MachineInstr *MI, unsigned OpNo) { |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 197 | if (MI->getOperand(OpNo).isImm()) { |
Chris Lattner | 9a1ceae | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 198 | O << (short)(MI->getOperand(OpNo).getImm()*4); |
Chris Lattner | 1b0a2d8 | 2006-11-16 21:45:30 +0000 | [diff] [blame] | 199 | } else { |
| 200 | O << "lo16("; |
| 201 | printOp(MI->getOperand(OpNo)); |
| 202 | if (TM.getRelocationModel() == Reloc::PIC_) |
Evan Cheng | 347d39f | 2007-10-14 05:57:21 +0000 | [diff] [blame] | 203 | O << "-\"L" << getFunctionNumber() << "$pb\")"; |
Chris Lattner | 1b0a2d8 | 2006-11-16 21:45:30 +0000 | [diff] [blame] | 204 | else |
| 205 | O << ')'; |
| 206 | } |
Chris Lattner | 841d12d | 2005-10-18 16:51:22 +0000 | [diff] [blame] | 207 | } |
Nate Begeman | 391c5d2 | 2005-11-30 18:54:35 +0000 | [diff] [blame] | 208 | void printBranchOperand(const MachineInstr *MI, unsigned OpNo) { |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 209 | // Branches can take an immediate operand. This is used by the branch |
| 210 | // selection pass to print $+8, an eight byte displacement from the PC. |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 211 | if (MI->getOperand(OpNo).isImm()) { |
Chris Lattner | 9a1ceae | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 212 | O << "$+" << MI->getOperand(OpNo).getImm()*4; |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 213 | } else { |
Chris Lattner | 3e7f86a | 2005-11-17 19:16:08 +0000 | [diff] [blame] | 214 | printOp(MI->getOperand(OpNo)); |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 215 | } |
Nate Begeman | b7a8f2c | 2004-09-02 08:13:00 +0000 | [diff] [blame] | 216 | } |
Nate Begeman | 391c5d2 | 2005-11-30 18:54:35 +0000 | [diff] [blame] | 217 | void printCallOperand(const MachineInstr *MI, unsigned OpNo) { |
Chris Lattner | 9ba13e4 | 2005-11-17 19:25:59 +0000 | [diff] [blame] | 218 | const MachineOperand &MO = MI->getOperand(OpNo); |
Evan Cheng | 4c1aa86 | 2006-02-22 20:19:42 +0000 | [diff] [blame] | 219 | if (TM.getRelocationModel() != Reloc::Static) { |
Chris Lattner | a637c32 | 2005-12-16 00:22:14 +0000 | [diff] [blame] | 220 | if (MO.getType() == MachineOperand::MO_GlobalAddress) { |
| 221 | GlobalValue *GV = MO.getGlobal(); |
Chris Lattner | 2067433 | 2009-07-02 16:08:53 +0000 | [diff] [blame] | 222 | if (GV->isDeclaration() || GV->isWeakForLinker()) { |
Chris Lattner | a637c32 | 2005-12-16 00:22:14 +0000 | [diff] [blame] | 223 | // Dynamically-resolved functions need a stub for the function. |
Chris Lattner | 9ec8fac | 2009-07-15 02:28:57 +0000 | [diff] [blame] | 224 | FnStubInfo &FnInfo = FnStubs[Mang->getMangledName(GV)]; |
| 225 | FnInfo.Init(GV, Mang); |
| 226 | O << FnInfo.Stub; |
Chris Lattner | a637c32 | 2005-12-16 00:22:14 +0000 | [diff] [blame] | 227 | return; |
| 228 | } |
| 229 | } |
Chris Lattner | 9542f971 | 2005-11-17 19:40:30 +0000 | [diff] [blame] | 230 | if (MO.getType() == MachineOperand::MO_ExternalSymbol) { |
Chris Lattner | 9ec8fac | 2009-07-15 02:28:57 +0000 | [diff] [blame] | 231 | FnStubInfo &FnInfo =FnStubs[Mang->makeNameProper(MO.getSymbolName())]; |
| 232 | FnInfo.Init(MO.getSymbolName(), Mang); |
| 233 | O << FnInfo.Stub; |
Chris Lattner | 9542f971 | 2005-11-17 19:40:30 +0000 | [diff] [blame] | 234 | return; |
Chris Lattner | 9542f971 | 2005-11-17 19:40:30 +0000 | [diff] [blame] | 235 | } |
Chris Lattner | 9ba13e4 | 2005-11-17 19:25:59 +0000 | [diff] [blame] | 236 | } |
Anton Korobeynikov | 34da127 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 237 | |
Chris Lattner | 9542f971 | 2005-11-17 19:40:30 +0000 | [diff] [blame] | 238 | printOp(MI->getOperand(OpNo)); |
Chris Lattner | 3e7f86a | 2005-11-17 19:16:08 +0000 | [diff] [blame] | 239 | } |
Nate Begeman | 391c5d2 | 2005-11-30 18:54:35 +0000 | [diff] [blame] | 240 | void printAbsAddrOperand(const MachineInstr *MI, unsigned OpNo) { |
Chris Lattner | 9a1ceae | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 241 | O << (int)MI->getOperand(OpNo).getImm()*4; |
Nate Begeman | 422b0ce | 2005-11-16 00:48:01 +0000 | [diff] [blame] | 242 | } |
Nate Begeman | 391c5d2 | 2005-11-30 18:54:35 +0000 | [diff] [blame] | 243 | void printPICLabel(const MachineInstr *MI, unsigned OpNo) { |
Evan Cheng | 347d39f | 2007-10-14 05:57:21 +0000 | [diff] [blame] | 244 | O << "\"L" << getFunctionNumber() << "$pb\"\n"; |
| 245 | O << "\"L" << getFunctionNumber() << "$pb\":"; |
Nate Begeman | b7a8f2c | 2004-09-02 08:13:00 +0000 | [diff] [blame] | 246 | } |
Nate Begeman | 391c5d2 | 2005-11-30 18:54:35 +0000 | [diff] [blame] | 247 | void printSymbolHi(const MachineInstr *MI, unsigned OpNo) { |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 248 | if (MI->getOperand(OpNo).isImm()) { |
Nate Begeman | 391c5d2 | 2005-11-30 18:54:35 +0000 | [diff] [blame] | 249 | printS16ImmOperand(MI, OpNo); |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 250 | } else { |
Nick Lewycky | f54949d | 2007-03-03 05:29:51 +0000 | [diff] [blame] | 251 | if (Subtarget.isDarwin()) O << "ha16("; |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 252 | printOp(MI->getOperand(OpNo)); |
Chris Lattner | 35d86fe | 2006-07-26 21:12:04 +0000 | [diff] [blame] | 253 | if (TM.getRelocationModel() == Reloc::PIC_) |
Evan Cheng | 347d39f | 2007-10-14 05:57:21 +0000 | [diff] [blame] | 254 | O << "-\"L" << getFunctionNumber() << "$pb\""; |
Nick Lewycky | f54949d | 2007-03-03 05:29:51 +0000 | [diff] [blame] | 255 | if (Subtarget.isDarwin()) |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 256 | O << ')'; |
Nick Lewycky | f54949d | 2007-03-03 05:29:51 +0000 | [diff] [blame] | 257 | else |
| 258 | O << "@ha"; |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 259 | } |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 260 | } |
Nate Begeman | 391c5d2 | 2005-11-30 18:54:35 +0000 | [diff] [blame] | 261 | void printSymbolLo(const MachineInstr *MI, unsigned OpNo) { |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 262 | if (MI->getOperand(OpNo).isImm()) { |
Nate Begeman | 391c5d2 | 2005-11-30 18:54:35 +0000 | [diff] [blame] | 263 | printS16ImmOperand(MI, OpNo); |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 264 | } else { |
Nick Lewycky | f54949d | 2007-03-03 05:29:51 +0000 | [diff] [blame] | 265 | if (Subtarget.isDarwin()) O << "lo16("; |
Nate Begeman | d4c8bea | 2004-11-25 07:09:01 +0000 | [diff] [blame] | 266 | printOp(MI->getOperand(OpNo)); |
Chris Lattner | 35d86fe | 2006-07-26 21:12:04 +0000 | [diff] [blame] | 267 | if (TM.getRelocationModel() == Reloc::PIC_) |
Evan Cheng | 347d39f | 2007-10-14 05:57:21 +0000 | [diff] [blame] | 268 | O << "-\"L" << getFunctionNumber() << "$pb\""; |
Nick Lewycky | f54949d | 2007-03-03 05:29:51 +0000 | [diff] [blame] | 269 | if (Subtarget.isDarwin()) |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 270 | O << ')'; |
Nick Lewycky | f54949d | 2007-03-03 05:29:51 +0000 | [diff] [blame] | 271 | else |
| 272 | O << "@l"; |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 273 | } |
| 274 | } |
Nate Begeman | 391c5d2 | 2005-11-30 18:54:35 +0000 | [diff] [blame] | 275 | void printcrbitm(const MachineInstr *MI, unsigned OpNo) { |
Nate Begeman | adeb43d | 2005-07-20 22:42:00 +0000 | [diff] [blame] | 276 | unsigned CCReg = MI->getOperand(OpNo).getReg(); |
| 277 | unsigned RegNo = enumRegToMachineReg(CCReg); |
| 278 | O << (0x80 >> RegNo); |
| 279 | } |
Chris Lattner | 2c003e2 | 2006-02-24 20:27:40 +0000 | [diff] [blame] | 280 | // The new addressing mode printers. |
Nate Begeman | 7fd1edd | 2005-12-19 23:25:09 +0000 | [diff] [blame] | 281 | void printMemRegImm(const MachineInstr *MI, unsigned OpNo) { |
| 282 | printSymbolLo(MI, OpNo); |
| 283 | O << '('; |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 284 | if (MI->getOperand(OpNo+1).isReg() && |
Chris Lattner | 13feb58 | 2006-03-21 17:21:13 +0000 | [diff] [blame] | 285 | MI->getOperand(OpNo+1).getReg() == PPC::R0) |
| 286 | O << "0"; |
| 287 | else |
| 288 | printOperand(MI, OpNo+1); |
Nate Begeman | 7fd1edd | 2005-12-19 23:25:09 +0000 | [diff] [blame] | 289 | O << ')'; |
| 290 | } |
Chris Lattner | e5ba580 | 2006-03-22 05:26:03 +0000 | [diff] [blame] | 291 | void printMemRegImmShifted(const MachineInstr *MI, unsigned OpNo) { |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 292 | if (MI->getOperand(OpNo).isImm()) |
Chris Lattner | e5ba580 | 2006-03-22 05:26:03 +0000 | [diff] [blame] | 293 | printS16X4ImmOperand(MI, OpNo); |
Anton Korobeynikov | 34da127 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 294 | else |
Chris Lattner | e5ba580 | 2006-03-22 05:26:03 +0000 | [diff] [blame] | 295 | printSymbolLo(MI, OpNo); |
| 296 | O << '('; |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 297 | if (MI->getOperand(OpNo+1).isReg() && |
Chris Lattner | e5ba580 | 2006-03-22 05:26:03 +0000 | [diff] [blame] | 298 | MI->getOperand(OpNo+1).getReg() == PPC::R0) |
| 299 | O << "0"; |
| 300 | else |
| 301 | printOperand(MI, OpNo+1); |
| 302 | O << ')'; |
| 303 | } |
Anton Korobeynikov | 34da127 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 304 | |
Nate Begeman | 7fd1edd | 2005-12-19 23:25:09 +0000 | [diff] [blame] | 305 | void printMemRegReg(const MachineInstr *MI, unsigned OpNo) { |
Nate Begeman | 88276b8 | 2005-12-19 23:40:42 +0000 | [diff] [blame] | 306 | // When used as the base register, r0 reads constant zero rather than |
| 307 | // the value contained in the register. For this reason, the darwin |
| 308 | // assembler requires that we print r0 as 0 (no r) when used as the base. |
| 309 | const MachineOperand &MO = MI->getOperand(OpNo); |
Jim Laskey | b608a4d | 2006-12-20 20:56:46 +0000 | [diff] [blame] | 310 | printRegister(MO, true); |
Nate Begeman | 7fd1edd | 2005-12-19 23:25:09 +0000 | [diff] [blame] | 311 | O << ", "; |
| 312 | printOperand(MI, OpNo+1); |
| 313 | } |
Anton Korobeynikov | 34da127 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 314 | |
Tilmann Scheller | 6b16eff | 2009-08-15 11:54:46 +0000 | [diff] [blame] | 315 | void printTOCEntryLabel(const MachineInstr *MI, unsigned OpNo) { |
| 316 | const MachineOperand &MO = MI->getOperand(OpNo); |
| 317 | |
| 318 | assert(MO.getType() == MachineOperand::MO_GlobalAddress); |
| 319 | |
| 320 | GlobalValue *GV = MO.getGlobal(); |
| 321 | |
| 322 | std::string Name = Mang->getMangledName(GV); |
| 323 | |
| 324 | // Map symbol -> label of TOC entry. |
| 325 | if (TOC.count(Name) == 0) { |
| 326 | std::string Label; |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 327 | Label += MAI->getPrivateGlobalPrefix(); |
Tilmann Scheller | 6b16eff | 2009-08-15 11:54:46 +0000 | [diff] [blame] | 328 | Label += "C"; |
| 329 | Label += utostr(LabelID++); |
| 330 | |
| 331 | TOC[Name] = Label; |
| 332 | } |
| 333 | |
| 334 | O << TOC[Name] << "@toc"; |
| 335 | } |
| 336 | |
Anton Korobeynikov | 34da127 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 337 | void printPredicateOperand(const MachineInstr *MI, unsigned OpNo, |
Chris Lattner | af53a87 | 2006-11-04 05:27:39 +0000 | [diff] [blame] | 338 | const char *Modifier); |
Anton Korobeynikov | 34da127 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 339 | |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 340 | virtual bool runOnMachineFunction(MachineFunction &F) = 0; |
Jim Laskey | bda9b0e | 2007-02-21 22:47:38 +0000 | [diff] [blame] | 341 | |
| 342 | virtual void EmitExternalGlobal(const GlobalVariable *GV); |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 343 | }; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 344 | |
Anton Korobeynikov | 34da127 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 345 | /// PPCLinuxAsmPrinter - PowerPC assembly printer, customized for Linux |
Bill Wendling | 57f0db8 | 2009-02-24 08:30:20 +0000 | [diff] [blame] | 346 | class VISIBILITY_HIDDEN PPCLinuxAsmPrinter : public PPCAsmPrinter { |
Bill Wendling | 57f0db8 | 2009-02-24 08:30:20 +0000 | [diff] [blame] | 347 | public: |
Daniel Dunbar | 51b198a | 2009-07-15 20:24:03 +0000 | [diff] [blame] | 348 | explicit PPCLinuxAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM, |
Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 349 | const MCAsmInfo *T, bool V) |
Daniel Dunbar | 5bcc8bd | 2009-07-01 01:48:54 +0000 | [diff] [blame] | 350 | : PPCAsmPrinter(O, TM, T, V){} |
Jim Laskey | bf11182 | 2006-12-21 20:26:09 +0000 | [diff] [blame] | 351 | |
| 352 | virtual const char *getPassName() const { |
| 353 | return "Linux PPC Assembly Printer"; |
| 354 | } |
| 355 | |
| 356 | bool runOnMachineFunction(MachineFunction &F); |
Tilmann Scheller | 6b16eff | 2009-08-15 11:54:46 +0000 | [diff] [blame] | 357 | bool doFinalization(Module &M); |
Anton Korobeynikov | 34da127 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 358 | |
Jim Laskey | bf11182 | 2006-12-21 20:26:09 +0000 | [diff] [blame] | 359 | void getAnalysisUsage(AnalysisUsage &AU) const { |
| 360 | AU.setPreservesAll(); |
Jim Laskey | 44c3b9f | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 361 | AU.addRequired<MachineModuleInfo>(); |
Devang Patel | eb3fc28 | 2009-01-08 23:40:34 +0000 | [diff] [blame] | 362 | AU.addRequired<DwarfWriter>(); |
Jim Laskey | bf11182 | 2006-12-21 20:26:09 +0000 | [diff] [blame] | 363 | PPCAsmPrinter::getAnalysisUsage(AU); |
| 364 | } |
| 365 | |
Chris Lattner | 40bbebd | 2009-07-21 18:38:57 +0000 | [diff] [blame] | 366 | void PrintGlobalVariable(const GlobalVariable *GVar); |
Jim Laskey | bf11182 | 2006-12-21 20:26:09 +0000 | [diff] [blame] | 367 | }; |
| 368 | |
Anton Korobeynikov | 34da127 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 369 | /// PPCDarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac |
| 370 | /// OS X |
Bill Wendling | 57f0db8 | 2009-02-24 08:30:20 +0000 | [diff] [blame] | 371 | class VISIBILITY_HIDDEN PPCDarwinAsmPrinter : public PPCAsmPrinter { |
David Greene | 7184781 | 2009-07-14 20:18:05 +0000 | [diff] [blame] | 372 | formatted_raw_ostream &OS; |
Bill Wendling | 57f0db8 | 2009-02-24 08:30:20 +0000 | [diff] [blame] | 373 | public: |
Daniel Dunbar | 51b198a | 2009-07-15 20:24:03 +0000 | [diff] [blame] | 374 | explicit PPCDarwinAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM, |
Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 375 | const MCAsmInfo *T, bool V) |
Daniel Dunbar | 5bcc8bd | 2009-07-01 01:48:54 +0000 | [diff] [blame] | 376 | : PPCAsmPrinter(O, TM, T, V), OS(O) {} |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 377 | |
| 378 | virtual const char *getPassName() const { |
| 379 | return "Darwin PPC Assembly Printer"; |
| 380 | } |
Anton Korobeynikov | 34da127 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 381 | |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 382 | bool runOnMachineFunction(MachineFunction &F); |
Nate Begeman | 18ed029 | 2005-07-21 01:25:49 +0000 | [diff] [blame] | 383 | bool doInitialization(Module &M); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 384 | bool doFinalization(Module &M); |
Anton Korobeynikov | 34da127 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 385 | |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 386 | void getAnalysisUsage(AnalysisUsage &AU) const { |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 387 | AU.setPreservesAll(); |
Jim Laskey | 44c3b9f | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 388 | AU.addRequired<MachineModuleInfo>(); |
Devang Patel | eb3fc28 | 2009-01-08 23:40:34 +0000 | [diff] [blame] | 389 | AU.addRequired<DwarfWriter>(); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 390 | PPCAsmPrinter::getAnalysisUsage(AU); |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 391 | } |
| 392 | |
Chris Lattner | 40bbebd | 2009-07-21 18:38:57 +0000 | [diff] [blame] | 393 | void PrintGlobalVariable(const GlobalVariable *GVar); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 394 | }; |
| 395 | } // end of anonymous namespace |
| 396 | |
Nate Begeman | e59bf59 | 2004-08-14 22:09:10 +0000 | [diff] [blame] | 397 | // Include the auto-generated portion of the assembly writer |
Chris Lattner | 4c7b43b | 2005-10-14 23:37:35 +0000 | [diff] [blame] | 398 | #include "PPCGenAsmWriter.inc" |
Nate Begeman | e59bf59 | 2004-08-14 22:09:10 +0000 | [diff] [blame] | 399 | |
Chris Lattner | 9ba13e4 | 2005-11-17 19:25:59 +0000 | [diff] [blame] | 400 | void PPCAsmPrinter::printOp(const MachineOperand &MO) { |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 401 | switch (MO.getType()) { |
Chris Lattner | 63b3d71 | 2006-05-04 17:21:20 +0000 | [diff] [blame] | 402 | case MachineOperand::MO_Immediate: |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 403 | llvm_unreachable("printOp() does not handle immediate values"); |
Misha Brukman | 97a296f | 2004-07-21 20:11:11 +0000 | [diff] [blame] | 404 | |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 405 | case MachineOperand::MO_MachineBasicBlock: |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 406 | printBasicBlockLabel(MO.getMBB()); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 407 | return; |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 408 | case MachineOperand::MO_JumpTableIndex: |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 409 | O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 410 | << '_' << MO.getIndex(); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 411 | // FIXME: PIC relocation model |
| 412 | return; |
Misha Brukman | 05fcd0c | 2004-07-08 17:58:04 +0000 | [diff] [blame] | 413 | case MachineOperand::MO_ConstantPoolIndex: |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 414 | O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 415 | << '_' << MO.getIndex(); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 416 | return; |
Chris Lattner | 8f831cb | 2009-07-15 01:14:44 +0000 | [diff] [blame] | 417 | case MachineOperand::MO_ExternalSymbol: { |
Chris Lattner | a637c32 | 2005-12-16 00:22:14 +0000 | [diff] [blame] | 418 | // Computing the address of an external symbol, not calling it. |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 419 | std::string Name(MAI->getGlobalPrefix()); |
Chris Lattner | 8f831cb | 2009-07-15 01:14:44 +0000 | [diff] [blame] | 420 | Name += MO.getSymbolName(); |
| 421 | |
Evan Cheng | 4c1aa86 | 2006-02-22 20:19:42 +0000 | [diff] [blame] | 422 | if (TM.getRelocationModel() != Reloc::Static) { |
Chris Lattner | 8f831cb | 2009-07-15 01:14:44 +0000 | [diff] [blame] | 423 | GVStubs[Name] = Name+"$non_lazy_ptr"; |
| 424 | Name += "$non_lazy_ptr"; |
Chris Lattner | a637c32 | 2005-12-16 00:22:14 +0000 | [diff] [blame] | 425 | } |
Chris Lattner | 8f831cb | 2009-07-15 01:14:44 +0000 | [diff] [blame] | 426 | O << Name; |
Misha Brukman | 05fcd0c | 2004-07-08 17:58:04 +0000 | [diff] [blame] | 427 | return; |
Chris Lattner | 8f831cb | 2009-07-15 01:14:44 +0000 | [diff] [blame] | 428 | } |
Nate Begeman | b73a711 | 2004-08-13 09:32:01 +0000 | [diff] [blame] | 429 | case MachineOperand::MO_GlobalAddress: { |
Chris Lattner | a637c32 | 2005-12-16 00:22:14 +0000 | [diff] [blame] | 430 | // Computing the address of a global symbol, not calling it. |
Nate Begeman | b73a711 | 2004-08-13 09:32:01 +0000 | [diff] [blame] | 431 | GlobalValue *GV = MO.getGlobal(); |
Chris Lattner | 8f831cb | 2009-07-15 01:14:44 +0000 | [diff] [blame] | 432 | std::string Name; |
Misha Brukman | e2eceb5 | 2004-07-23 16:08:20 +0000 | [diff] [blame] | 433 | |
Nate Begeman | fcf4a42 | 2004-10-17 23:01:34 +0000 | [diff] [blame] | 434 | // External or weakly linked global variables need non-lazily-resolved stubs |
Chris Lattner | 8f831cb | 2009-07-15 01:14:44 +0000 | [diff] [blame] | 435 | if (TM.getRelocationModel() != Reloc::Static && |
| 436 | (GV->isDeclaration() || GV->isWeakForLinker())) { |
| 437 | if (!GV->hasHiddenVisibility()) { |
| 438 | Name = Mang->getMangledName(GV, "$non_lazy_ptr", true); |
| 439 | GVStubs[Mang->getMangledName(GV)] = Name; |
| 440 | } else if (GV->isDeclaration() || GV->hasCommonLinkage() || |
| 441 | GV->hasAvailableExternallyLinkage()) { |
| 442 | Name = Mang->getMangledName(GV, "$non_lazy_ptr", true); |
| 443 | HiddenGVStubs[Mang->getMangledName(GV)] = Name; |
| 444 | } else { |
| 445 | Name = Mang->getMangledName(GV); |
Chris Lattner | a637c32 | 2005-12-16 00:22:14 +0000 | [diff] [blame] | 446 | } |
Chris Lattner | 8f831cb | 2009-07-15 01:14:44 +0000 | [diff] [blame] | 447 | } else { |
| 448 | Name = Mang->getMangledName(GV); |
Nate Begeman | b73a711 | 2004-08-13 09:32:01 +0000 | [diff] [blame] | 449 | } |
Chris Lattner | 9542f971 | 2005-11-17 19:40:30 +0000 | [diff] [blame] | 450 | O << Name; |
Anton Korobeynikov | 34da127 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 451 | |
Anton Korobeynikov | 7751ad9 | 2008-11-22 16:15:34 +0000 | [diff] [blame] | 452 | printOffset(MO.getOffset()); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 453 | return; |
Nate Begeman | b73a711 | 2004-08-13 09:32:01 +0000 | [diff] [blame] | 454 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 455 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 456 | default: |
Misha Brukman | 05fcd0c | 2004-07-08 17:58:04 +0000 | [diff] [blame] | 457 | O << "<unknown operand type: " << MO.getType() << ">"; |
Misha Brukman | 22e1207 | 2004-06-25 15:11:34 +0000 | [diff] [blame] | 458 | return; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 459 | } |
| 460 | } |
| 461 | |
Jim Laskey | bda9b0e | 2007-02-21 22:47:38 +0000 | [diff] [blame] | 462 | /// EmitExternalGlobal - In this case we need to use the indirect symbol. |
| 463 | /// |
| 464 | void PPCAsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) { |
Bill Wendling | 7d16e85 | 2009-04-10 00:12:49 +0000 | [diff] [blame] | 465 | std::string Name; |
Chris Lattner | 8f831cb | 2009-07-15 01:14:44 +0000 | [diff] [blame] | 466 | |
Jim Laskey | bda9b0e | 2007-02-21 22:47:38 +0000 | [diff] [blame] | 467 | if (TM.getRelocationModel() != Reloc::Static) { |
Chris Lattner | 8f831cb | 2009-07-15 01:14:44 +0000 | [diff] [blame] | 468 | Name = Mang->getMangledName(GV, "$non_lazy_ptr", true); |
Chris Lattner | 8f831cb | 2009-07-15 01:14:44 +0000 | [diff] [blame] | 469 | } else { |
| 470 | Name = Mang->getMangledName(GV); |
Jim Laskey | bda9b0e | 2007-02-21 22:47:38 +0000 | [diff] [blame] | 471 | } |
| 472 | O << Name; |
| 473 | } |
| 474 | |
Chris Lattner | e3f0157 | 2006-02-23 19:31:10 +0000 | [diff] [blame] | 475 | /// PrintAsmOperand - Print out an operand for an inline asm expression. |
| 476 | /// |
| 477 | bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, |
Anton Korobeynikov | 34da127 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 478 | unsigned AsmVariant, |
Chris Lattner | e3f0157 | 2006-02-23 19:31:10 +0000 | [diff] [blame] | 479 | const char *ExtraCode) { |
| 480 | // Does this asm operand have a single letter operand modifier? |
| 481 | if (ExtraCode && ExtraCode[0]) { |
| 482 | if (ExtraCode[1] != 0) return true; // Unknown modifier. |
Anton Korobeynikov | 34da127 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 483 | |
Chris Lattner | e3f0157 | 2006-02-23 19:31:10 +0000 | [diff] [blame] | 484 | switch (ExtraCode[0]) { |
| 485 | default: return true; // Unknown modifier. |
Chris Lattner | 78192b6 | 2007-01-25 02:52:50 +0000 | [diff] [blame] | 486 | case 'c': // Don't print "$" before a global var name or constant. |
| 487 | // PPC never has a prefix. |
| 488 | printOperand(MI, OpNo); |
| 489 | return false; |
Anton Korobeynikov | 34da127 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 490 | case 'L': // Write second word of DImode reference. |
Chris Lattner | e3f0157 | 2006-02-23 19:31:10 +0000 | [diff] [blame] | 491 | // Verify that this operand has two consecutive registers. |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 492 | if (!MI->getOperand(OpNo).isReg() || |
Chris Lattner | e3f0157 | 2006-02-23 19:31:10 +0000 | [diff] [blame] | 493 | OpNo+1 == MI->getNumOperands() || |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 494 | !MI->getOperand(OpNo+1).isReg()) |
Chris Lattner | e3f0157 | 2006-02-23 19:31:10 +0000 | [diff] [blame] | 495 | return true; |
| 496 | ++OpNo; // Return the high-part. |
| 497 | break; |
Chris Lattner | 6c2d260 | 2007-04-24 22:51:03 +0000 | [diff] [blame] | 498 | case 'I': |
| 499 | // Write 'i' if an integer constant, otherwise nothing. Used to print |
| 500 | // addi vs add, etc. |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 501 | if (MI->getOperand(OpNo).isImm()) |
Chris Lattner | 6c2d260 | 2007-04-24 22:51:03 +0000 | [diff] [blame] | 502 | O << "i"; |
| 503 | return false; |
Chris Lattner | e3f0157 | 2006-02-23 19:31:10 +0000 | [diff] [blame] | 504 | } |
| 505 | } |
Anton Korobeynikov | 34da127 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 506 | |
Chris Lattner | e3f0157 | 2006-02-23 19:31:10 +0000 | [diff] [blame] | 507 | printOperand(MI, OpNo); |
| 508 | return false; |
| 509 | } |
| 510 | |
Dale Johannesen | 5cfd4dd | 2009-08-18 00:18:39 +0000 | [diff] [blame] | 511 | // At the moment, all inline asm memory operands are a single register. |
| 512 | // In any case, the output of this routine should always be just one |
| 513 | // assembler operand. |
| 514 | |
Chris Lattner | 2c003e2 | 2006-02-24 20:27:40 +0000 | [diff] [blame] | 515 | bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo, |
Anton Korobeynikov | 34da127 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 516 | unsigned AsmVariant, |
Chris Lattner | 2c003e2 | 2006-02-24 20:27:40 +0000 | [diff] [blame] | 517 | const char *ExtraCode) { |
| 518 | if (ExtraCode && ExtraCode[0]) |
| 519 | return true; // Unknown modifier. |
Dale Johannesen | 5cfd4dd | 2009-08-18 00:18:39 +0000 | [diff] [blame] | 520 | assert (MI->getOperand(OpNo).isReg()); |
Dale Johannesen | 4e68f88 | 2009-08-26 18:10:32 +0000 | [diff] [blame] | 521 | O << "0("; |
Dale Johannesen | 5cfd4dd | 2009-08-18 00:18:39 +0000 | [diff] [blame] | 522 | printOperand(MI, OpNo); |
Dale Johannesen | 4e68f88 | 2009-08-26 18:10:32 +0000 | [diff] [blame] | 523 | O << ")"; |
Chris Lattner | 2c003e2 | 2006-02-24 20:27:40 +0000 | [diff] [blame] | 524 | return false; |
| 525 | } |
Chris Lattner | e3f0157 | 2006-02-23 19:31:10 +0000 | [diff] [blame] | 526 | |
Anton Korobeynikov | 34da127 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 527 | void PPCAsmPrinter::printPredicateOperand(const MachineInstr *MI, unsigned OpNo, |
Chris Lattner | af53a87 | 2006-11-04 05:27:39 +0000 | [diff] [blame] | 528 | const char *Modifier) { |
| 529 | assert(Modifier && "Must specify 'cc' or 'reg' as predicate op modifier!"); |
| 530 | unsigned Code = MI->getOperand(OpNo).getImm(); |
| 531 | if (!strcmp(Modifier, "cc")) { |
| 532 | switch ((PPC::Predicate)Code) { |
| 533 | case PPC::PRED_ALWAYS: return; // Don't print anything for always. |
| 534 | case PPC::PRED_LT: O << "lt"; return; |
| 535 | case PPC::PRED_LE: O << "le"; return; |
| 536 | case PPC::PRED_EQ: O << "eq"; return; |
| 537 | case PPC::PRED_GE: O << "ge"; return; |
| 538 | case PPC::PRED_GT: O << "gt"; return; |
| 539 | case PPC::PRED_NE: O << "ne"; return; |
| 540 | case PPC::PRED_UN: O << "un"; return; |
| 541 | case PPC::PRED_NU: O << "nu"; return; |
| 542 | } |
Anton Korobeynikov | 34da127 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 543 | |
Chris Lattner | af53a87 | 2006-11-04 05:27:39 +0000 | [diff] [blame] | 544 | } else { |
| 545 | assert(!strcmp(Modifier, "reg") && |
| 546 | "Need to specify 'cc' or 'reg' as predicate op modifier!"); |
| 547 | // Don't print the register for 'always'. |
| 548 | if (Code == PPC::PRED_ALWAYS) return; |
| 549 | printOperand(MI, OpNo+1); |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | |
Nate Begeman | e59bf59 | 2004-08-14 22:09:10 +0000 | [diff] [blame] | 554 | /// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to |
| 555 | /// the current output stream. |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 556 | /// |
Chris Lattner | 4c7b43b | 2005-10-14 23:37:35 +0000 | [diff] [blame] | 557 | void PPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) { |
Nate Begeman | e59bf59 | 2004-08-14 22:09:10 +0000 | [diff] [blame] | 558 | ++EmittedInsts; |
Chris Lattner | 634cca3 | 2009-09-09 20:34:59 +0000 | [diff] [blame^] | 559 | |
| 560 | processDebugLoc(MI->getDebugLoc()); |
Chris Lattner | 617742b | 2005-10-14 22:44:13 +0000 | [diff] [blame] | 561 | |
Nate Begeman | ad5f65c | 2005-04-05 18:19:50 +0000 | [diff] [blame] | 562 | // Check for slwi/srwi mnemonics. |
| 563 | if (MI->getOpcode() == PPC::RLWINM) { |
| 564 | bool FoundMnemonic = false; |
Chris Lattner | 9a1ceae | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 565 | unsigned char SH = MI->getOperand(2).getImm(); |
| 566 | unsigned char MB = MI->getOperand(3).getImm(); |
| 567 | unsigned char ME = MI->getOperand(4).getImm(); |
Nate Begeman | ad5f65c | 2005-04-05 18:19:50 +0000 | [diff] [blame] | 568 | if (SH <= 31 && MB == 0 && ME == (31-SH)) { |
Nate Begeman | 5a804e3 | 2008-02-05 08:49:09 +0000 | [diff] [blame] | 569 | O << "\tslwi "; FoundMnemonic = true; |
Nate Begeman | ad5f65c | 2005-04-05 18:19:50 +0000 | [diff] [blame] | 570 | } |
| 571 | if (SH <= 31 && MB == (32-SH) && ME == 31) { |
Nate Begeman | 5a804e3 | 2008-02-05 08:49:09 +0000 | [diff] [blame] | 572 | O << "\tsrwi "; FoundMnemonic = true; |
Nate Begeman | ad5f65c | 2005-04-05 18:19:50 +0000 | [diff] [blame] | 573 | SH = 32-SH; |
| 574 | } |
| 575 | if (FoundMnemonic) { |
Nate Begeman | 391c5d2 | 2005-11-30 18:54:35 +0000 | [diff] [blame] | 576 | printOperand(MI, 0); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 577 | O << ", "; |
Nate Begeman | 391c5d2 | 2005-11-30 18:54:35 +0000 | [diff] [blame] | 578 | printOperand(MI, 1); |
Anton Korobeynikov | 382f002 | 2008-08-08 18:24:10 +0000 | [diff] [blame] | 579 | O << ", " << (unsigned int)SH << '\n'; |
Nate Begeman | ad5f65c | 2005-04-05 18:19:50 +0000 | [diff] [blame] | 580 | return; |
| 581 | } |
Chris Lattner | b410dc9 | 2006-06-20 23:18:58 +0000 | [diff] [blame] | 582 | } else if (MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) { |
Chris Lattner | 4d73a5a | 2006-02-08 06:56:40 +0000 | [diff] [blame] | 583 | if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) { |
Nate Begeman | 5a804e3 | 2008-02-05 08:49:09 +0000 | [diff] [blame] | 584 | O << "\tmr "; |
Chris Lattner | 4d73a5a | 2006-02-08 06:56:40 +0000 | [diff] [blame] | 585 | printOperand(MI, 0); |
| 586 | O << ", "; |
| 587 | printOperand(MI, 1); |
Anton Korobeynikov | 382f002 | 2008-08-08 18:24:10 +0000 | [diff] [blame] | 588 | O << '\n'; |
Chris Lattner | 4d73a5a | 2006-02-08 06:56:40 +0000 | [diff] [blame] | 589 | return; |
| 590 | } |
Chris Lattner | 566c1b1 | 2006-11-18 01:23:56 +0000 | [diff] [blame] | 591 | } else if (MI->getOpcode() == PPC::RLDICR) { |
Chris Lattner | 9a1ceae | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 592 | unsigned char SH = MI->getOperand(2).getImm(); |
| 593 | unsigned char ME = MI->getOperand(3).getImm(); |
Chris Lattner | 566c1b1 | 2006-11-18 01:23:56 +0000 | [diff] [blame] | 594 | // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH |
| 595 | if (63-SH == ME) { |
Nate Begeman | 5a804e3 | 2008-02-05 08:49:09 +0000 | [diff] [blame] | 596 | O << "\tsldi "; |
Chris Lattner | 566c1b1 | 2006-11-18 01:23:56 +0000 | [diff] [blame] | 597 | printOperand(MI, 0); |
| 598 | O << ", "; |
| 599 | printOperand(MI, 1); |
Anton Korobeynikov | 382f002 | 2008-08-08 18:24:10 +0000 | [diff] [blame] | 600 | O << ", " << (unsigned int)SH << '\n'; |
Chris Lattner | 566c1b1 | 2006-11-18 01:23:56 +0000 | [diff] [blame] | 601 | return; |
| 602 | } |
Nate Begeman | ad5f65c | 2005-04-05 18:19:50 +0000 | [diff] [blame] | 603 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 604 | |
Chris Lattner | 2698cb6 | 2009-08-08 00:05:42 +0000 | [diff] [blame] | 605 | printInstruction(MI); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 606 | } |
| 607 | |
Jim Laskey | bf11182 | 2006-12-21 20:26:09 +0000 | [diff] [blame] | 608 | /// runOnMachineFunction - This uses the printMachineInstruction() |
| 609 | /// method to print assembly for each instruction. |
| 610 | /// |
Anton Korobeynikov | 34da127 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 611 | bool PPCLinuxAsmPrinter::runOnMachineFunction(MachineFunction &MF) { |
Bill Wendling | 57f0db8 | 2009-02-24 08:30:20 +0000 | [diff] [blame] | 612 | this->MF = &MF; |
Chris Lattner | 9b7ce7d | 2006-10-05 02:42:20 +0000 | [diff] [blame] | 613 | |
Jim Laskey | bf11182 | 2006-12-21 20:26:09 +0000 | [diff] [blame] | 614 | SetupMachineFunction(MF); |
| 615 | O << "\n\n"; |
Anton Korobeynikov | 34da127 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 616 | |
Jim Laskey | bf11182 | 2006-12-21 20:26:09 +0000 | [diff] [blame] | 617 | // Print out constants referenced by the function |
| 618 | EmitConstantPool(MF.getConstantPool()); |
| 619 | |
| 620 | // Print out labels for the function. |
| 621 | const Function *F = MF.getFunction(); |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 622 | OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM)); |
Anton Korobeynikov | 34da127 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 623 | |
Jim Laskey | bf11182 | 2006-12-21 20:26:09 +0000 | [diff] [blame] | 624 | switch (F->getLinkage()) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 625 | default: llvm_unreachable("Unknown linkage type!"); |
Rafael Espindola | bb46f52 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 626 | case Function::PrivateLinkage: |
Jim Laskey | bf11182 | 2006-12-21 20:26:09 +0000 | [diff] [blame] | 627 | case Function::InternalLinkage: // Symbols default to internal. |
| 628 | break; |
| 629 | case Function::ExternalLinkage: |
| 630 | O << "\t.global\t" << CurrentFnName << '\n' |
| 631 | << "\t.type\t" << CurrentFnName << ", @function\n"; |
| 632 | break; |
Dale Johannesen | a60e51f | 2009-08-24 01:03:42 +0000 | [diff] [blame] | 633 | case Function::LinkerPrivateLinkage: |
Duncan Sands | 667d4b8 | 2009-03-07 15:45:40 +0000 | [diff] [blame] | 634 | case Function::WeakAnyLinkage: |
| 635 | case Function::WeakODRLinkage: |
| 636 | case Function::LinkOnceAnyLinkage: |
| 637 | case Function::LinkOnceODRLinkage: |
Jim Laskey | bf11182 | 2006-12-21 20:26:09 +0000 | [diff] [blame] | 638 | O << "\t.global\t" << CurrentFnName << '\n'; |
| 639 | O << "\t.weak\t" << CurrentFnName << '\n'; |
| 640 | break; |
| 641 | } |
Anton Korobeynikov | 34da127 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 642 | |
Anton Korobeynikov | f5b6a47 | 2008-08-08 18:25:07 +0000 | [diff] [blame] | 643 | printVisibility(CurrentFnName, F->getVisibility()); |
Anton Korobeynikov | 34da127 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 644 | |
Bill Wendling | 20c568f | 2009-06-30 22:38:32 +0000 | [diff] [blame] | 645 | EmitAlignment(MF.getAlignment(), F); |
Tilmann Scheller | 6b16eff | 2009-08-15 11:54:46 +0000 | [diff] [blame] | 646 | |
| 647 | if (Subtarget.isPPC64()) { |
| 648 | // Emit an official procedure descriptor. |
| 649 | // FIXME 64-bit SVR4: Use MCSection here? |
| 650 | O << "\t.section\t\".opd\",\"aw\"\n"; |
| 651 | O << "\t.align 3\n"; |
| 652 | O << CurrentFnName << ":\n"; |
| 653 | O << "\t.quad .L." << CurrentFnName << ",.TOC.@tocbase\n"; |
| 654 | O << "\t.previous\n"; |
| 655 | O << ".L." << CurrentFnName << ":\n"; |
| 656 | } else { |
| 657 | O << CurrentFnName << ":\n"; |
| 658 | } |
Jim Laskey | bf11182 | 2006-12-21 20:26:09 +0000 | [diff] [blame] | 659 | |
| 660 | // Emit pre-function debug information. |
Devang Patel | eb3fc28 | 2009-01-08 23:40:34 +0000 | [diff] [blame] | 661 | DW->BeginFunction(&MF); |
Jim Laskey | bf11182 | 2006-12-21 20:26:09 +0000 | [diff] [blame] | 662 | |
| 663 | // Print out code for the function. |
| 664 | for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); |
| 665 | I != E; ++I) { |
| 666 | // Print a label for the basic block. |
| 667 | if (I != MF.begin()) { |
Evan Cheng | fb8075d | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 668 | printBasicBlockLabel(I, true, true); |
Jim Laskey | bf11182 | 2006-12-21 20:26:09 +0000 | [diff] [blame] | 669 | O << '\n'; |
| 670 | } |
| 671 | for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end(); |
| 672 | II != E; ++II) { |
| 673 | // Print the assembly for the instruction. |
Jim Laskey | bf11182 | 2006-12-21 20:26:09 +0000 | [diff] [blame] | 674 | printMachineInstruction(II); |
| 675 | } |
| 676 | } |
| 677 | |
Anton Korobeynikov | 382f002 | 2008-08-08 18:24:10 +0000 | [diff] [blame] | 678 | O << "\t.size\t" << CurrentFnName << ",.-" << CurrentFnName << '\n'; |
Jim Laskey | bf11182 | 2006-12-21 20:26:09 +0000 | [diff] [blame] | 679 | |
| 680 | // Print out jump tables referenced by the function. |
| 681 | EmitJumpTableInfo(MF.getJumpTableInfo(), MF); |
Anton Korobeynikov | 34da127 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 682 | |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 683 | OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM)); |
Dale Johannesen | f94a327 | 2008-12-03 19:33:10 +0000 | [diff] [blame] | 684 | |
Jim Laskey | bf11182 | 2006-12-21 20:26:09 +0000 | [diff] [blame] | 685 | // Emit post-function debug information. |
Devang Patel | eb3fc28 | 2009-01-08 23:40:34 +0000 | [diff] [blame] | 686 | DW->EndFunction(&MF); |
Anton Korobeynikov | 34da127 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 687 | |
Jim Laskey | bf11182 | 2006-12-21 20:26:09 +0000 | [diff] [blame] | 688 | // We didn't modify anything. |
| 689 | return false; |
| 690 | } |
| 691 | |
Chris Lattner | 40bbebd | 2009-07-21 18:38:57 +0000 | [diff] [blame] | 692 | void PPCLinuxAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) { |
Jim Laskey | bf11182 | 2006-12-21 20:26:09 +0000 | [diff] [blame] | 693 | const TargetData *TD = TM.getTargetData(); |
| 694 | |
Anton Korobeynikov | 7396e59 | 2008-08-08 18:23:49 +0000 | [diff] [blame] | 695 | if (!GVar->hasInitializer()) |
| 696 | return; // External global require no code |
Anton Korobeynikov | 34da127 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 697 | |
Anton Korobeynikov | 7396e59 | 2008-08-08 18:23:49 +0000 | [diff] [blame] | 698 | // Check to see if this is a special global used by LLVM, if so, emit it. |
| 699 | if (EmitSpecialLLVMGlobal(GVar)) |
| 700 | return; |
Chris Lattner | 70d4107 | 2007-01-14 06:37:54 +0000 | [diff] [blame] | 701 | |
Chris Lattner | b8158ac | 2009-07-14 18:17:16 +0000 | [diff] [blame] | 702 | std::string name = Mang->getMangledName(GVar); |
Chris Lattner | 70d4107 | 2007-01-14 06:37:54 +0000 | [diff] [blame] | 703 | |
Anton Korobeynikov | f5b6a47 | 2008-08-08 18:25:07 +0000 | [diff] [blame] | 704 | printVisibility(name, GVar->getVisibility()); |
Anton Korobeynikov | 34da127 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 705 | |
Anton Korobeynikov | 7396e59 | 2008-08-08 18:23:49 +0000 | [diff] [blame] | 706 | Constant *C = GVar->getInitializer(); |
| 707 | const Type *Type = C->getType(); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 708 | unsigned Size = TD->getTypeAllocSize(Type); |
Anton Korobeynikov | 7396e59 | 2008-08-08 18:23:49 +0000 | [diff] [blame] | 709 | unsigned Align = TD->getPreferredAlignmentLog(GVar); |
Jim Laskey | bf11182 | 2006-12-21 20:26:09 +0000 | [diff] [blame] | 710 | |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 711 | OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GVar, Mang, |
| 712 | TM)); |
Anton Korobeynikov | 7396e59 | 2008-08-08 18:23:49 +0000 | [diff] [blame] | 713 | |
| 714 | if (C->isNullValue() && /* FIXME: Verify correct */ |
| 715 | !GVar->hasSection() && |
Rafael Espindola | bb46f52 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 716 | (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() || |
Duncan Sands | 667d4b8 | 2009-03-07 15:45:40 +0000 | [diff] [blame] | 717 | GVar->isWeakForLinker())) { |
Jim Laskey | bf11182 | 2006-12-21 20:26:09 +0000 | [diff] [blame] | 718 | if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it. |
Anton Korobeynikov | 7396e59 | 2008-08-08 18:23:49 +0000 | [diff] [blame] | 719 | |
| 720 | if (GVar->hasExternalLinkage()) { |
Jim Laskey | bf11182 | 2006-12-21 20:26:09 +0000 | [diff] [blame] | 721 | O << "\t.global " << name << '\n'; |
| 722 | O << "\t.type " << name << ", @object\n"; |
Nick Lewycky | e2b9052 | 2007-07-25 03:48:45 +0000 | [diff] [blame] | 723 | O << name << ":\n"; |
Anton Korobeynikov | 382f002 | 2008-08-08 18:24:10 +0000 | [diff] [blame] | 724 | O << "\t.zero " << Size << '\n'; |
Rafael Espindola | bb46f52 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 725 | } else if (GVar->hasLocalLinkage()) { |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 726 | O << MAI->getLCOMMDirective() << name << ',' << Size; |
Jim Laskey | bf11182 | 2006-12-21 20:26:09 +0000 | [diff] [blame] | 727 | } else { |
Anton Korobeynikov | 382f002 | 2008-08-08 18:24:10 +0000 | [diff] [blame] | 728 | O << ".comm " << name << ',' << Size; |
Jim Laskey | bf11182 | 2006-12-21 20:26:09 +0000 | [diff] [blame] | 729 | } |
Evan Cheng | f1c0ae9 | 2009-03-24 00:17:40 +0000 | [diff] [blame] | 730 | if (VerboseAsm) { |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 731 | O << "\t\t" << MAI->getCommentString() << " '"; |
Dan Gohman | cf20ac4 | 2009-08-13 01:36:44 +0000 | [diff] [blame] | 732 | WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent()); |
Evan Cheng | f1c0ae9 | 2009-03-24 00:17:40 +0000 | [diff] [blame] | 733 | O << "'"; |
| 734 | } |
| 735 | O << '\n'; |
Anton Korobeynikov | 7396e59 | 2008-08-08 18:23:49 +0000 | [diff] [blame] | 736 | return; |
Jim Laskey | bf11182 | 2006-12-21 20:26:09 +0000 | [diff] [blame] | 737 | } |
| 738 | |
Anton Korobeynikov | 7396e59 | 2008-08-08 18:23:49 +0000 | [diff] [blame] | 739 | switch (GVar->getLinkage()) { |
Duncan Sands | 667d4b8 | 2009-03-07 15:45:40 +0000 | [diff] [blame] | 740 | case GlobalValue::LinkOnceAnyLinkage: |
| 741 | case GlobalValue::LinkOnceODRLinkage: |
| 742 | case GlobalValue::WeakAnyLinkage: |
| 743 | case GlobalValue::WeakODRLinkage: |
Duncan Sands | 4dc2b39 | 2009-03-11 20:14:15 +0000 | [diff] [blame] | 744 | case GlobalValue::CommonLinkage: |
Dale Johannesen | a60e51f | 2009-08-24 01:03:42 +0000 | [diff] [blame] | 745 | case GlobalValue::LinkerPrivateLinkage: |
Anton Korobeynikov | 7396e59 | 2008-08-08 18:23:49 +0000 | [diff] [blame] | 746 | O << "\t.global " << name << '\n' |
| 747 | << "\t.type " << name << ", @object\n" |
| 748 | << "\t.weak " << name << '\n'; |
| 749 | break; |
| 750 | case GlobalValue::AppendingLinkage: |
| 751 | // FIXME: appending linkage variables should go into a section of |
| 752 | // their name or something. For now, just emit them as external. |
| 753 | case GlobalValue::ExternalLinkage: |
| 754 | // If external or appending, declare as a global symbol |
Anton Korobeynikov | 382f002 | 2008-08-08 18:24:10 +0000 | [diff] [blame] | 755 | O << "\t.global " << name << '\n' |
Anton Korobeynikov | 7396e59 | 2008-08-08 18:23:49 +0000 | [diff] [blame] | 756 | << "\t.type " << name << ", @object\n"; |
| 757 | // FALL THROUGH |
| 758 | case GlobalValue::InternalLinkage: |
Rafael Espindola | bb46f52 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 759 | case GlobalValue::PrivateLinkage: |
Anton Korobeynikov | 7396e59 | 2008-08-08 18:23:49 +0000 | [diff] [blame] | 760 | break; |
| 761 | default: |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 762 | llvm_unreachable("Unknown linkage type!"); |
Anton Korobeynikov | 7396e59 | 2008-08-08 18:23:49 +0000 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | EmitAlignment(Align, GVar); |
Evan Cheng | f1c0ae9 | 2009-03-24 00:17:40 +0000 | [diff] [blame] | 766 | O << name << ":"; |
| 767 | if (VerboseAsm) { |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 768 | O << "\t\t\t\t" << MAI->getCommentString() << " '"; |
Dan Gohman | cf20ac4 | 2009-08-13 01:36:44 +0000 | [diff] [blame] | 769 | WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent()); |
Evan Cheng | f1c0ae9 | 2009-03-24 00:17:40 +0000 | [diff] [blame] | 770 | O << "'"; |
| 771 | } |
| 772 | O << '\n'; |
Anton Korobeynikov | 7396e59 | 2008-08-08 18:23:49 +0000 | [diff] [blame] | 773 | |
Anton Korobeynikov | 7396e59 | 2008-08-08 18:23:49 +0000 | [diff] [blame] | 774 | EmitGlobalConstant(C); |
| 775 | O << '\n'; |
| 776 | } |
| 777 | |
Tilmann Scheller | 6b16eff | 2009-08-15 11:54:46 +0000 | [diff] [blame] | 778 | bool PPCLinuxAsmPrinter::doFinalization(Module &M) { |
| 779 | const TargetData *TD = TM.getTargetData(); |
| 780 | |
| 781 | bool isPPC64 = TD->getPointerSizeInBits() == 64; |
| 782 | |
| 783 | if (isPPC64 && !TOC.empty()) { |
| 784 | // FIXME 64-bit SVR4: Use MCSection here? |
| 785 | O << "\t.section\t\".toc\",\"aw\"\n"; |
| 786 | |
| 787 | for (StringMap<std::string>::iterator I = TOC.begin(), E = TOC.end(); |
| 788 | I != E; ++I) { |
| 789 | O << I->second << ":\n"; |
| 790 | O << "\t.tc " << I->getKeyData() << "[TC]," << I->getKeyData() << '\n'; |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | return AsmPrinter::doFinalization(M); |
| 795 | } |
Jim Laskey | bf11182 | 2006-12-21 20:26:09 +0000 | [diff] [blame] | 796 | |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 797 | /// runOnMachineFunction - This uses the printMachineInstruction() |
| 798 | /// method to print assembly for each instruction. |
| 799 | /// |
Anton Korobeynikov | 34da127 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 800 | bool PPCDarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) { |
Bill Wendling | 57f0db8 | 2009-02-24 08:30:20 +0000 | [diff] [blame] | 801 | this->MF = &MF; |
| 802 | |
Chris Lattner | 8b8b951 | 2005-11-21 07:51:23 +0000 | [diff] [blame] | 803 | SetupMachineFunction(MF); |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 804 | O << "\n\n"; |
Dale Johannesen | 1d4ce2a | 2007-11-20 23:24:42 +0000 | [diff] [blame] | 805 | |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 806 | // Print out constants referenced by the function |
Chris Lattner | c569e61 | 2005-11-21 08:26:15 +0000 | [diff] [blame] | 807 | EmitConstantPool(MF.getConstantPool()); |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 808 | |
| 809 | // Print out labels for the function. |
Chris Lattner | ac7fd7f | 2005-11-14 18:52:46 +0000 | [diff] [blame] | 810 | const Function *F = MF.getFunction(); |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 811 | OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM)); |
Anton Korobeynikov | 34da127 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 812 | |
Chris Lattner | a637c32 | 2005-12-16 00:22:14 +0000 | [diff] [blame] | 813 | switch (F->getLinkage()) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 814 | default: llvm_unreachable("Unknown linkage type!"); |
Evan Cheng | 1902a12 | 2009-01-25 06:32:01 +0000 | [diff] [blame] | 815 | case Function::PrivateLinkage: |
Chris Lattner | a637c32 | 2005-12-16 00:22:14 +0000 | [diff] [blame] | 816 | case Function::InternalLinkage: // Symbols default to internal. |
Chris Lattner | a637c32 | 2005-12-16 00:22:14 +0000 | [diff] [blame] | 817 | break; |
| 818 | case Function::ExternalLinkage: |
Anton Korobeynikov | 382f002 | 2008-08-08 18:24:10 +0000 | [diff] [blame] | 819 | O << "\t.globl\t" << CurrentFnName << '\n'; |
Chris Lattner | a637c32 | 2005-12-16 00:22:14 +0000 | [diff] [blame] | 820 | break; |
Duncan Sands | 667d4b8 | 2009-03-07 15:45:40 +0000 | [diff] [blame] | 821 | case Function::WeakAnyLinkage: |
| 822 | case Function::WeakODRLinkage: |
| 823 | case Function::LinkOnceAnyLinkage: |
| 824 | case Function::LinkOnceODRLinkage: |
Dale Johannesen | a60e51f | 2009-08-24 01:03:42 +0000 | [diff] [blame] | 825 | case Function::LinkerPrivateLinkage: |
Anton Korobeynikov | 382f002 | 2008-08-08 18:24:10 +0000 | [diff] [blame] | 826 | O << "\t.globl\t" << CurrentFnName << '\n'; |
| 827 | O << "\t.weak_definition\t" << CurrentFnName << '\n'; |
Chris Lattner | a637c32 | 2005-12-16 00:22:14 +0000 | [diff] [blame] | 828 | break; |
| 829 | } |
Anton Korobeynikov | 34da127 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 830 | |
Anton Korobeynikov | f5b6a47 | 2008-08-08 18:25:07 +0000 | [diff] [blame] | 831 | printVisibility(CurrentFnName, F->getVisibility()); |
Anton Korobeynikov | 34da127 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 832 | |
Bill Wendling | 20c568f | 2009-06-30 22:38:32 +0000 | [diff] [blame] | 833 | EmitAlignment(MF.getAlignment(), F); |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 834 | O << CurrentFnName << ":\n"; |
| 835 | |
Jim Laskey | 6b92b8e | 2006-04-07 20:44:42 +0000 | [diff] [blame] | 836 | // Emit pre-function debug information. |
Devang Patel | eb3fc28 | 2009-01-08 23:40:34 +0000 | [diff] [blame] | 837 | DW->BeginFunction(&MF); |
Jim Laskey | 6b92b8e | 2006-04-07 20:44:42 +0000 | [diff] [blame] | 838 | |
Bill Wendling | 381802f | 2008-01-26 06:51:24 +0000 | [diff] [blame] | 839 | // If the function is empty, then we need to emit *something*. Otherwise, the |
| 840 | // function's label might be associated with something that it wasn't meant to |
| 841 | // be associated with. We emit a noop in this situation. |
| 842 | MachineFunction::iterator I = MF.begin(); |
| 843 | |
Bill Wendling | 824a721 | 2008-01-26 09:03:52 +0000 | [diff] [blame] | 844 | if (++I == MF.end() && MF.front().empty()) |
| 845 | O << "\tnop\n"; |
Bill Wendling | 381802f | 2008-01-26 06:51:24 +0000 | [diff] [blame] | 846 | |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 847 | // Print out code for the function. |
| 848 | for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); |
| 849 | I != E; ++I) { |
| 850 | // Print a label for the basic block. |
Chris Lattner | 1db1adb | 2005-08-21 19:09:33 +0000 | [diff] [blame] | 851 | if (I != MF.begin()) { |
Evan Cheng | f1c0ae9 | 2009-03-24 00:17:40 +0000 | [diff] [blame] | 852 | printBasicBlockLabel(I, true, true, VerboseAsm); |
Nate Begeman | cdf38c4 | 2006-05-02 05:37:32 +0000 | [diff] [blame] | 853 | O << '\n'; |
Chris Lattner | 1db1adb | 2005-08-21 19:09:33 +0000 | [diff] [blame] | 854 | } |
Bill Wendling | 381802f | 2008-01-26 06:51:24 +0000 | [diff] [blame] | 855 | for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end(); |
| 856 | II != IE; ++II) { |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 857 | // Print the assembly for the instruction. |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 858 | printMachineInstruction(II); |
| 859 | } |
| 860 | } |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 861 | |
Chris Lattner | fea13d3 | 2006-10-05 00:26:05 +0000 | [diff] [blame] | 862 | // Print out jump tables referenced by the function. |
Chris Lattner | 1da31ee | 2006-10-05 03:01:21 +0000 | [diff] [blame] | 863 | EmitJumpTableInfo(MF.getJumpTableInfo(), MF); |
Anton Korobeynikov | 34da127 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 864 | |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 865 | // Emit post-function debug information. |
Devang Patel | eb3fc28 | 2009-01-08 23:40:34 +0000 | [diff] [blame] | 866 | DW->EndFunction(&MF); |
Anton Korobeynikov | 34da127 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 867 | |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 868 | // We didn't modify anything. |
| 869 | return false; |
| 870 | } |
| 871 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 872 | |
Anton Korobeynikov | 34da127 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 873 | bool PPCDarwinAsmPrinter::doInitialization(Module &M) { |
Dan Gohman | cfbb2f0 | 2008-03-25 21:45:14 +0000 | [diff] [blame] | 874 | static const char *const CPUDirectives[] = { |
Dale Johannesen | db01c8b | 2008-02-14 23:35:16 +0000 | [diff] [blame] | 875 | "", |
Jim Laskey | c35010d | 2006-12-12 20:57:08 +0000 | [diff] [blame] | 876 | "ppc", |
| 877 | "ppc601", |
| 878 | "ppc602", |
| 879 | "ppc603", |
| 880 | "ppc7400", |
| 881 | "ppc750", |
| 882 | "ppc970", |
| 883 | "ppc64" |
| 884 | }; |
| 885 | |
| 886 | unsigned Directive = Subtarget.getDarwinDirective(); |
| 887 | if (Subtarget.isGigaProcessor() && Directive < PPC::DIR_970) |
| 888 | Directive = PPC::DIR_970; |
| 889 | if (Subtarget.hasAltivec() && Directive < PPC::DIR_7400) |
| 890 | Directive = PPC::DIR_7400; |
| 891 | if (Subtarget.isPPC64() && Directive < PPC::DIR_970) |
| 892 | Directive = PPC::DIR_64; |
| 893 | assert(Directive <= PPC::DIR_64 && "Directive out of range."); |
Anton Korobeynikov | 382f002 | 2008-08-08 18:24:10 +0000 | [diff] [blame] | 894 | O << "\t.machine " << CPUDirectives[Directive] << '\n'; |
Anton Korobeynikov | 34da127 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 895 | |
Dan Gohman | b8275a3 | 2007-07-25 19:33:14 +0000 | [diff] [blame] | 896 | bool Result = AsmPrinter::doInitialization(M); |
Devang Patel | 66b4d3b | 2009-06-20 01:00:07 +0000 | [diff] [blame] | 897 | assert(MMI); |
Dale Johannesen | 94618eb | 2008-07-09 20:43:39 +0000 | [diff] [blame] | 898 | |
Jim Laskey | 2ada085 | 2006-11-28 18:21:52 +0000 | [diff] [blame] | 899 | // Prime text sections so they are adjacent. This reduces the likelihood a |
| 900 | // large data or debug section causes a branch to exceed 16M limit. |
Chris Lattner | 2dfddee | 2009-08-03 22:52:21 +0000 | [diff] [blame] | 901 | TargetLoweringObjectFileMachO &TLOFMacho = |
| 902 | static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering()); |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 903 | OutStreamer.SwitchSection(TLOFMacho.getTextCoalSection()); |
Jim Laskey | 2ada085 | 2006-11-28 18:21:52 +0000 | [diff] [blame] | 904 | if (TM.getRelocationModel() == Reloc::PIC_) { |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 905 | OutStreamer.SwitchSection( |
| 906 | TLOFMacho.getMachOSection("__TEXT", "__picsymbolstub1", |
| 907 | MCSectionMachO::S_SYMBOL_STUBS | |
| 908 | MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, |
| 909 | 32, SectionKind::getText())); |
Jim Laskey | 2ada085 | 2006-11-28 18:21:52 +0000 | [diff] [blame] | 910 | } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) { |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 911 | OutStreamer.SwitchSection( |
| 912 | TLOFMacho.getMachOSection("__TEXT","__symbol_stub1", |
| 913 | MCSectionMachO::S_SYMBOL_STUBS | |
| 914 | MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, |
| 915 | 16, SectionKind::getText())); |
Jim Laskey | 2ada085 | 2006-11-28 18:21:52 +0000 | [diff] [blame] | 916 | } |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 917 | OutStreamer.SwitchSection(getObjFileLowering().getTextSection()); |
Anton Korobeynikov | 34da127 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 918 | |
Dan Gohman | b8275a3 | 2007-07-25 19:33:14 +0000 | [diff] [blame] | 919 | return Result; |
Nate Begeman | 18ed029 | 2005-07-21 01:25:49 +0000 | [diff] [blame] | 920 | } |
| 921 | |
Chris Lattner | 40bbebd | 2009-07-21 18:38:57 +0000 | [diff] [blame] | 922 | void PPCDarwinAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) { |
Anton Korobeynikov | bc331a8 | 2008-08-08 18:23:25 +0000 | [diff] [blame] | 923 | const TargetData *TD = TM.getTargetData(); |
| 924 | |
| 925 | if (!GVar->hasInitializer()) |
| 926 | return; // External global require no code |
| 927 | |
| 928 | // Check to see if this is a special global used by LLVM, if so, emit it. |
| 929 | if (EmitSpecialLLVMGlobal(GVar)) { |
| 930 | if (TM.getRelocationModel() == Reloc::Static) { |
Daniel Dunbar | 03d7651 | 2009-07-25 23:55:21 +0000 | [diff] [blame] | 931 | if (GVar->getName() == "llvm.global_ctors") |
Anton Korobeynikov | bc331a8 | 2008-08-08 18:23:25 +0000 | [diff] [blame] | 932 | O << ".reference .constructors_used\n"; |
Daniel Dunbar | 03d7651 | 2009-07-25 23:55:21 +0000 | [diff] [blame] | 933 | else if (GVar->getName() == "llvm.global_dtors") |
Anton Korobeynikov | bc331a8 | 2008-08-08 18:23:25 +0000 | [diff] [blame] | 934 | O << ".reference .destructors_used\n"; |
| 935 | } |
| 936 | return; |
| 937 | } |
| 938 | |
Chris Lattner | b8158ac | 2009-07-14 18:17:16 +0000 | [diff] [blame] | 939 | std::string name = Mang->getMangledName(GVar); |
Anton Korobeynikov | f5b6a47 | 2008-08-08 18:25:07 +0000 | [diff] [blame] | 940 | printVisibility(name, GVar->getVisibility()); |
Anton Korobeynikov | bc331a8 | 2008-08-08 18:23:25 +0000 | [diff] [blame] | 941 | |
| 942 | Constant *C = GVar->getInitializer(); |
| 943 | const Type *Type = C->getType(); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 944 | unsigned Size = TD->getTypeAllocSize(Type); |
Anton Korobeynikov | bc331a8 | 2008-08-08 18:23:25 +0000 | [diff] [blame] | 945 | unsigned Align = TD->getPreferredAlignmentLog(GVar); |
| 946 | |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 947 | const MCSection *TheSection = |
Chris Lattner | e53a600 | 2009-07-29 05:09:30 +0000 | [diff] [blame] | 948 | getObjFileLowering().SectionForGlobal(GVar, Mang, TM); |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 949 | OutStreamer.SwitchSection(TheSection); |
Anton Korobeynikov | bc331a8 | 2008-08-08 18:23:25 +0000 | [diff] [blame] | 950 | |
Chris Lattner | 3b24c01 | 2009-08-04 05:35:56 +0000 | [diff] [blame] | 951 | /// FIXME: Drive this off the section! |
Anton Korobeynikov | bc331a8 | 2008-08-08 18:23:25 +0000 | [diff] [blame] | 952 | if (C->isNullValue() && /* FIXME: Verify correct */ |
| 953 | !GVar->hasSection() && |
Rafael Espindola | bb46f52 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 954 | (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() || |
Duncan Sands | 667d4b8 | 2009-03-07 15:45:40 +0000 | [diff] [blame] | 955 | GVar->isWeakForLinker()) && |
Chris Lattner | c440cc7 | 2009-07-24 04:08:17 +0000 | [diff] [blame] | 956 | // Don't put things that should go in the cstring section into "comm". |
Chris Lattner | 5fe575f | 2009-07-27 05:32:16 +0000 | [diff] [blame] | 957 | !TheSection->getKind().isMergeableCString()) { |
Anton Korobeynikov | bc331a8 | 2008-08-08 18:23:25 +0000 | [diff] [blame] | 958 | if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it. |
| 959 | |
| 960 | if (GVar->hasExternalLinkage()) { |
| 961 | O << "\t.globl " << name << '\n'; |
| 962 | O << "\t.zerofill __DATA, __common, " << name << ", " |
| 963 | << Size << ", " << Align; |
Rafael Espindola | bb46f52 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 964 | } else if (GVar->hasLocalLinkage()) { |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 965 | O << MAI->getLCOMMDirective() << name << ',' << Size << ',' << Align; |
Anton Korobeynikov | bc331a8 | 2008-08-08 18:23:25 +0000 | [diff] [blame] | 966 | } else if (!GVar->hasCommonLinkage()) { |
Anton Korobeynikov | 382f002 | 2008-08-08 18:24:10 +0000 | [diff] [blame] | 967 | O << "\t.globl " << name << '\n' |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 968 | << MAI->getWeakDefDirective() << name << '\n'; |
Anton Korobeynikov | bc331a8 | 2008-08-08 18:23:25 +0000 | [diff] [blame] | 969 | EmitAlignment(Align, GVar); |
Evan Cheng | f1c0ae9 | 2009-03-24 00:17:40 +0000 | [diff] [blame] | 970 | O << name << ":"; |
| 971 | if (VerboseAsm) { |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 972 | O << "\t\t\t\t" << MAI->getCommentString() << " "; |
Dan Gohman | cf20ac4 | 2009-08-13 01:36:44 +0000 | [diff] [blame] | 973 | WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent()); |
Evan Cheng | f1c0ae9 | 2009-03-24 00:17:40 +0000 | [diff] [blame] | 974 | } |
Anton Korobeynikov | 382f002 | 2008-08-08 18:24:10 +0000 | [diff] [blame] | 975 | O << '\n'; |
Anton Korobeynikov | bc331a8 | 2008-08-08 18:23:25 +0000 | [diff] [blame] | 976 | EmitGlobalConstant(C); |
| 977 | return; |
| 978 | } else { |
Anton Korobeynikov | 382f002 | 2008-08-08 18:24:10 +0000 | [diff] [blame] | 979 | O << ".comm " << name << ',' << Size; |
Anton Korobeynikov | bc331a8 | 2008-08-08 18:23:25 +0000 | [diff] [blame] | 980 | // Darwin 9 and above support aligned common data. |
| 981 | if (Subtarget.isDarwin9()) |
Anton Korobeynikov | 382f002 | 2008-08-08 18:24:10 +0000 | [diff] [blame] | 982 | O << ',' << Align; |
Anton Korobeynikov | bc331a8 | 2008-08-08 18:23:25 +0000 | [diff] [blame] | 983 | } |
Evan Cheng | f1c0ae9 | 2009-03-24 00:17:40 +0000 | [diff] [blame] | 984 | if (VerboseAsm) { |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 985 | O << "\t\t" << MAI->getCommentString() << " '"; |
Dan Gohman | cf20ac4 | 2009-08-13 01:36:44 +0000 | [diff] [blame] | 986 | WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent()); |
Evan Cheng | f1c0ae9 | 2009-03-24 00:17:40 +0000 | [diff] [blame] | 987 | O << "'"; |
| 988 | } |
| 989 | O << '\n'; |
Anton Korobeynikov | bc331a8 | 2008-08-08 18:23:25 +0000 | [diff] [blame] | 990 | return; |
| 991 | } |
| 992 | |
| 993 | switch (GVar->getLinkage()) { |
Duncan Sands | 667d4b8 | 2009-03-07 15:45:40 +0000 | [diff] [blame] | 994 | case GlobalValue::LinkOnceAnyLinkage: |
| 995 | case GlobalValue::LinkOnceODRLinkage: |
| 996 | case GlobalValue::WeakAnyLinkage: |
| 997 | case GlobalValue::WeakODRLinkage: |
Duncan Sands | 4dc2b39 | 2009-03-11 20:14:15 +0000 | [diff] [blame] | 998 | case GlobalValue::CommonLinkage: |
Dale Johannesen | a60e51f | 2009-08-24 01:03:42 +0000 | [diff] [blame] | 999 | case GlobalValue::LinkerPrivateLinkage: |
Anton Korobeynikov | bc331a8 | 2008-08-08 18:23:25 +0000 | [diff] [blame] | 1000 | O << "\t.globl " << name << '\n' |
| 1001 | << "\t.weak_definition " << name << '\n'; |
| 1002 | break; |
| 1003 | case GlobalValue::AppendingLinkage: |
| 1004 | // FIXME: appending linkage variables should go into a section of |
| 1005 | // their name or something. For now, just emit them as external. |
| 1006 | case GlobalValue::ExternalLinkage: |
| 1007 | // If external or appending, declare as a global symbol |
Anton Korobeynikov | 382f002 | 2008-08-08 18:24:10 +0000 | [diff] [blame] | 1008 | O << "\t.globl " << name << '\n'; |
Anton Korobeynikov | bc331a8 | 2008-08-08 18:23:25 +0000 | [diff] [blame] | 1009 | // FALL THROUGH |
| 1010 | case GlobalValue::InternalLinkage: |
Evan Cheng | 1902a12 | 2009-01-25 06:32:01 +0000 | [diff] [blame] | 1011 | case GlobalValue::PrivateLinkage: |
Anton Korobeynikov | bc331a8 | 2008-08-08 18:23:25 +0000 | [diff] [blame] | 1012 | break; |
| 1013 | default: |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1014 | llvm_unreachable("Unknown linkage type!"); |
Anton Korobeynikov | bc331a8 | 2008-08-08 18:23:25 +0000 | [diff] [blame] | 1015 | } |
| 1016 | |
| 1017 | EmitAlignment(Align, GVar); |
Evan Cheng | f1c0ae9 | 2009-03-24 00:17:40 +0000 | [diff] [blame] | 1018 | O << name << ":"; |
| 1019 | if (VerboseAsm) { |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 1020 | O << "\t\t\t\t" << MAI->getCommentString() << " '"; |
Dan Gohman | cf20ac4 | 2009-08-13 01:36:44 +0000 | [diff] [blame] | 1021 | WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent()); |
Evan Cheng | f1c0ae9 | 2009-03-24 00:17:40 +0000 | [diff] [blame] | 1022 | O << "'"; |
| 1023 | } |
| 1024 | O << '\n'; |
Anton Korobeynikov | bc331a8 | 2008-08-08 18:23:25 +0000 | [diff] [blame] | 1025 | |
Anton Korobeynikov | bc331a8 | 2008-08-08 18:23:25 +0000 | [diff] [blame] | 1026 | EmitGlobalConstant(C); |
| 1027 | O << '\n'; |
| 1028 | } |
| 1029 | |
Anton Korobeynikov | 34da127 | 2008-08-08 18:22:59 +0000 | [diff] [blame] | 1030 | bool PPCDarwinAsmPrinter::doFinalization(Module &M) { |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 1031 | const TargetData *TD = TM.getTargetData(); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1032 | |
Chris Lattner | 7e097e4 | 2006-06-27 01:02:25 +0000 | [diff] [blame] | 1033 | bool isPPC64 = TD->getPointerSizeInBits() == 64; |
| 1034 | |
Chris Lattner | 2dfddee | 2009-08-03 22:52:21 +0000 | [diff] [blame] | 1035 | // Darwin/PPC always uses mach-o. |
| 1036 | TargetLoweringObjectFileMachO &TLOFMacho = |
| 1037 | static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering()); |
| 1038 | |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 1039 | |
| 1040 | const MCSection *LSPSection = 0; |
| 1041 | if (!FnStubs.empty()) // .lazy_symbol_pointer |
| 1042 | LSPSection = TLOFMacho.getLazySymbolPointerSection(); |
| 1043 | |
| 1044 | |
Misha Brukman | da2b13f | 2004-07-16 20:29:04 +0000 | [diff] [blame] | 1045 | // Output stubs for dynamically-linked functions |
Chris Lattner | ea56f10 | 2009-07-15 00:55:58 +0000 | [diff] [blame] | 1046 | if (TM.getRelocationModel() == Reloc::PIC_ && !FnStubs.empty()) { |
Chris Lattner | 2dfddee | 2009-08-03 22:52:21 +0000 | [diff] [blame] | 1047 | const MCSection *StubSection = |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 1048 | TLOFMacho.getMachOSection("__TEXT", "__picsymbolstub1", |
| 1049 | MCSectionMachO::S_SYMBOL_STUBS | |
| 1050 | MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, |
| 1051 | 32, SectionKind::getText()); |
| 1052 | for (StringMap<FnStubInfo>::iterator I = FnStubs.begin(), E = FnStubs.end(); |
Chris Lattner | 8f831cb | 2009-07-15 01:14:44 +0000 | [diff] [blame] | 1053 | I != E; ++I) { |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 1054 | OutStreamer.SwitchSection(StubSection); |
Chris Lattner | 7e097e4 | 2006-06-27 01:02:25 +0000 | [diff] [blame] | 1055 | EmitAlignment(4); |
Chris Lattner | defd300 | 2009-07-15 02:33:19 +0000 | [diff] [blame] | 1056 | const FnStubInfo &Info = I->second; |
Chris Lattner | defd300 | 2009-07-15 02:33:19 +0000 | [diff] [blame] | 1057 | O << Info.Stub << ":\n"; |
| 1058 | O << "\t.indirect_symbol " << I->getKeyData() << '\n'; |
Chris Lattner | deea416 | 2005-12-13 04:33:58 +0000 | [diff] [blame] | 1059 | O << "\tmflr r0\n"; |
Chris Lattner | 392db3e | 2009-07-15 02:56:53 +0000 | [diff] [blame] | 1060 | O << "\tbcl 20,31," << Info.AnonSymbol << '\n'; |
| 1061 | O << Info.AnonSymbol << ":\n"; |
Chris Lattner | deea416 | 2005-12-13 04:33:58 +0000 | [diff] [blame] | 1062 | O << "\tmflr r11\n"; |
Chris Lattner | 392db3e | 2009-07-15 02:56:53 +0000 | [diff] [blame] | 1063 | O << "\taddis r11,r11,ha16(" << Info.LazyPtr << "-" << Info.AnonSymbol; |
Evan Cheng | ae94e59 | 2008-12-05 01:06:39 +0000 | [diff] [blame] | 1064 | O << ")\n"; |
Chris Lattner | deea416 | 2005-12-13 04:33:58 +0000 | [diff] [blame] | 1065 | O << "\tmtlr r0\n"; |
Chris Lattner | 392db3e | 2009-07-15 02:56:53 +0000 | [diff] [blame] | 1066 | O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16("; |
| 1067 | O << Info.LazyPtr << "-" << Info.AnonSymbol << ")(r11)\n"; |
Chris Lattner | deea416 | 2005-12-13 04:33:58 +0000 | [diff] [blame] | 1068 | O << "\tmtctr r12\n"; |
| 1069 | O << "\tbctr\n"; |
Chris Lattner | fe2fe70 | 2009-07-16 01:23:26 +0000 | [diff] [blame] | 1070 | |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 1071 | OutStreamer.SwitchSection(LSPSection); |
Chris Lattner | defd300 | 2009-07-15 02:33:19 +0000 | [diff] [blame] | 1072 | O << Info.LazyPtr << ":\n"; |
| 1073 | O << "\t.indirect_symbol " << I->getKeyData() << '\n'; |
Chris Lattner | 1a87c05 | 2009-07-15 02:36:21 +0000 | [diff] [blame] | 1074 | O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n"; |
Chris Lattner | deea416 | 2005-12-13 04:33:58 +0000 | [diff] [blame] | 1075 | } |
Chris Lattner | ea56f10 | 2009-07-15 00:55:58 +0000 | [diff] [blame] | 1076 | } else if (!FnStubs.empty()) { |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 1077 | const MCSection *StubSection = |
| 1078 | TLOFMacho.getMachOSection("__TEXT","__symbol_stub1", |
| 1079 | MCSectionMachO::S_SYMBOL_STUBS | |
| 1080 | MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, |
| 1081 | 16, SectionKind::getText()); |
Chris Lattner | 2dfddee | 2009-08-03 22:52:21 +0000 | [diff] [blame] | 1082 | |
Chris Lattner | 9ec8fac | 2009-07-15 02:28:57 +0000 | [diff] [blame] | 1083 | for (StringMap<FnStubInfo>::iterator I = FnStubs.begin(), E = FnStubs.end(); |
Chris Lattner | 8f831cb | 2009-07-15 01:14:44 +0000 | [diff] [blame] | 1084 | I != E; ++I) { |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 1085 | OutStreamer.SwitchSection(StubSection); |
Chris Lattner | deea416 | 2005-12-13 04:33:58 +0000 | [diff] [blame] | 1086 | EmitAlignment(4); |
Chris Lattner | defd300 | 2009-07-15 02:33:19 +0000 | [diff] [blame] | 1087 | const FnStubInfo &Info = I->second; |
| 1088 | O << Info.Stub << ":\n"; |
| 1089 | O << "\t.indirect_symbol " << I->getKeyData() << '\n'; |
| 1090 | O << "\tlis r11,ha16(" << Info.LazyPtr << ")\n"; |
Chris Lattner | 1a87c05 | 2009-07-15 02:36:21 +0000 | [diff] [blame] | 1091 | O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16("; |
Chris Lattner | defd300 | 2009-07-15 02:33:19 +0000 | [diff] [blame] | 1092 | O << Info.LazyPtr << ")(r11)\n"; |
Chris Lattner | deea416 | 2005-12-13 04:33:58 +0000 | [diff] [blame] | 1093 | O << "\tmtctr r12\n"; |
| 1094 | O << "\tbctr\n"; |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 1095 | OutStreamer.SwitchSection(LSPSection); |
Chris Lattner | defd300 | 2009-07-15 02:33:19 +0000 | [diff] [blame] | 1096 | O << Info.LazyPtr << ":\n"; |
| 1097 | O << "\t.indirect_symbol " << I->getKeyData() << '\n'; |
Chris Lattner | 1a87c05 | 2009-07-15 02:36:21 +0000 | [diff] [blame] | 1098 | O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n"; |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 1099 | } |
Misha Brukman | 46fd00a | 2004-06-24 23:04:11 +0000 | [diff] [blame] | 1100 | } |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1101 | |
Anton Korobeynikov | 382f002 | 2008-08-08 18:24:10 +0000 | [diff] [blame] | 1102 | O << '\n'; |
Misha Brukman | da2b13f | 2004-07-16 20:29:04 +0000 | [diff] [blame] | 1103 | |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 1104 | if (MAI->doesSupportExceptionHandling() && MMI) { |
Dale Johannesen | 1d4ce2a | 2007-11-20 23:24:42 +0000 | [diff] [blame] | 1105 | // Add the (possibly multiple) personalities to the set of global values. |
Dale Johannesen | 1532f3d | 2008-04-02 00:25:04 +0000 | [diff] [blame] | 1106 | // Only referenced functions get into the Personalities list. |
Chris Lattner | 0de1fc4 | 2009-06-24 19:09:55 +0000 | [diff] [blame] | 1107 | const std::vector<Function *> &Personalities = MMI->getPersonalities(); |
Dale Johannesen | 1d4ce2a | 2007-11-20 23:24:42 +0000 | [diff] [blame] | 1108 | for (std::vector<Function *>::const_iterator I = Personalities.begin(), |
Chris Lattner | 8f831cb | 2009-07-15 01:14:44 +0000 | [diff] [blame] | 1109 | E = Personalities.end(); I != E; ++I) { |
| 1110 | if (*I) |
| 1111 | GVStubs[Mang->getMangledName(*I)] = |
Chris Lattner | 0f6c8f2 | 2009-07-15 01:16:38 +0000 | [diff] [blame] | 1112 | Mang->getMangledName(*I, "$non_lazy_ptr", true); |
Chris Lattner | 8f831cb | 2009-07-15 01:14:44 +0000 | [diff] [blame] | 1113 | } |
Dale Johannesen | 1d4ce2a | 2007-11-20 23:24:42 +0000 | [diff] [blame] | 1114 | } |
| 1115 | |
Chris Lattner | 2dfddee | 2009-08-03 22:52:21 +0000 | [diff] [blame] | 1116 | // Output macho stubs for external and common global variables. |
Dan Gohman | cb406c2 | 2007-10-03 19:26:29 +0000 | [diff] [blame] | 1117 | if (!GVStubs.empty()) { |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 1118 | // Switch with ".non_lazy_symbol_pointer" directive. |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 1119 | OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection()); |
Chris Lattner | e1e8a82 | 2009-08-10 17:58:51 +0000 | [diff] [blame] | 1120 | EmitAlignment(isPPC64 ? 3 : 2); |
| 1121 | |
Chris Lattner | 8f831cb | 2009-07-15 01:14:44 +0000 | [diff] [blame] | 1122 | for (StringMap<std::string>::iterator I = GVStubs.begin(), |
| 1123 | E = GVStubs.end(); I != E; ++I) { |
| 1124 | O << I->second << ":\n"; |
| 1125 | O << "\t.indirect_symbol " << I->getKeyData() << '\n'; |
| 1126 | O << (isPPC64 ? "\t.quad\t0\n" : "\t.long\t0\n"); |
Chris Lattner | deea416 | 2005-12-13 04:33:58 +0000 | [diff] [blame] | 1127 | } |
Nate Begeman | e59bf59 | 2004-08-14 22:09:10 +0000 | [diff] [blame] | 1128 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1129 | |
Evan Cheng | ae94e59 | 2008-12-05 01:06:39 +0000 | [diff] [blame] | 1130 | if (!HiddenGVStubs.empty()) { |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 1131 | OutStreamer.SwitchSection(getObjFileLowering().getDataSection()); |
Chris Lattner | 8f831cb | 2009-07-15 01:14:44 +0000 | [diff] [blame] | 1132 | EmitAlignment(isPPC64 ? 3 : 2); |
| 1133 | for (StringMap<std::string>::iterator I = HiddenGVStubs.begin(), |
| 1134 | E = HiddenGVStubs.end(); I != E; ++I) { |
| 1135 | O << I->second << ":\n"; |
| 1136 | O << (isPPC64 ? "\t.quad\t" : "\t.long\t") << I->getKeyData() << '\n'; |
Evan Cheng | ae94e59 | 2008-12-05 01:06:39 +0000 | [diff] [blame] | 1137 | } |
| 1138 | } |
| 1139 | |
Chris Lattner | 5b0ac99 | 2005-11-01 00:12:36 +0000 | [diff] [blame] | 1140 | // Funny Darwin hack: This flag tells the linker that no global symbols |
| 1141 | // contain code that falls through to other global symbols (e.g. the obvious |
| 1142 | // implementation of multiple entry points). If this doesn't occur, the |
| 1143 | // linker can safely perform dead code stripping. Since LLVM never generates |
| 1144 | // code that does this, it is always safe to set. |
Chris Lattner | 4da1c82 | 2006-09-20 17:12:19 +0000 | [diff] [blame] | 1145 | O << "\t.subsections_via_symbols\n"; |
Chris Lattner | 5b0ac99 | 2005-11-01 00:12:36 +0000 | [diff] [blame] | 1146 | |
Dan Gohman | b8275a3 | 2007-07-25 19:33:14 +0000 | [diff] [blame] | 1147 | return AsmPrinter::doFinalization(M); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1148 | } |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 1149 | |
Chris Lattner | 4da1c82 | 2006-09-20 17:12:19 +0000 | [diff] [blame] | 1150 | |
| 1151 | |
Jim Laskey | b608a4d | 2006-12-20 20:56:46 +0000 | [diff] [blame] | 1152 | /// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code |
| 1153 | /// for a MachineFunction to the given output stream, in a format that the |
Chris Lattner | 4da1c82 | 2006-09-20 17:12:19 +0000 | [diff] [blame] | 1154 | /// Darwin assembler can deal with. |
| 1155 | /// |
Daniel Dunbar | 7894578 | 2009-08-13 23:48:47 +0000 | [diff] [blame] | 1156 | static AsmPrinter *createPPCAsmPrinterPass(formatted_raw_ostream &o, |
| 1157 | TargetMachine &tm, |
Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 1158 | const MCAsmInfo *tai, |
Daniel Dunbar | 7894578 | 2009-08-13 23:48:47 +0000 | [diff] [blame] | 1159 | bool verbose) { |
Jim Laskey | bf11182 | 2006-12-21 20:26:09 +0000 | [diff] [blame] | 1160 | const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>(); |
| 1161 | |
Chris Lattner | 40bbebd | 2009-07-21 18:38:57 +0000 | [diff] [blame] | 1162 | if (Subtarget->isDarwin()) |
Daniel Dunbar | 67d894e | 2009-08-13 19:38:51 +0000 | [diff] [blame] | 1163 | return new PPCDarwinAsmPrinter(o, tm, tai, verbose); |
| 1164 | return new PPCLinuxAsmPrinter(o, tm, tai, verbose); |
Chris Lattner | 4da1c82 | 2006-09-20 17:12:19 +0000 | [diff] [blame] | 1165 | } |
Anton Korobeynikov | 06be997 | 2008-08-17 13:54:28 +0000 | [diff] [blame] | 1166 | |
Bob Wilson | a96751f | 2009-06-23 23:59:40 +0000 | [diff] [blame] | 1167 | // Force static initialization. |
Daniel Dunbar | 51b198a | 2009-07-15 20:24:03 +0000 | [diff] [blame] | 1168 | extern "C" void LLVMInitializePowerPCAsmPrinter() { |
Daniel Dunbar | 51b198a | 2009-07-15 20:24:03 +0000 | [diff] [blame] | 1169 | TargetRegistry::RegisterAsmPrinter(ThePPC32Target, createPPCAsmPrinterPass); |
Daniel Dunbar | 51b198a | 2009-07-15 20:24:03 +0000 | [diff] [blame] | 1170 | TargetRegistry::RegisterAsmPrinter(ThePPC64Target, createPPCAsmPrinterPass); |
| 1171 | } |