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 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
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 | 16e71f2 | 2005-10-14 23:59:06 +0000 | [diff] [blame] | 21 | #include "PPCTargetMachine.h" |
Chris Lattner | 2668959 | 2005-10-14 23:51:18 +0000 | [diff] [blame] | 22 | #include "PPCSubtarget.h" |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 23 | #include "llvm/Constants.h" |
| 24 | #include "llvm/DerivedTypes.h" |
| 25 | #include "llvm/Module.h" |
| 26 | #include "llvm/Assembly/Writer.h" |
Chris Lattner | a384079 | 2004-08-16 23:25:21 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/AsmPrinter.h" |
Misha Brukman | 0579449 | 2004-06-24 17:31:42 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/MachineInstr.h" |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Mangler.h" |
Nate Begeman | a11c2e8 | 2004-09-04 14:51:26 +0000 | [diff] [blame] | 31 | #include "llvm/Support/MathExtras.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 32 | #include "llvm/Support/CommandLine.h" |
| 33 | #include "llvm/Support/Debug.h" |
Nate Begeman | 17304c3 | 2004-10-26 06:02:38 +0000 | [diff] [blame] | 34 | #include "llvm/Target/MRegisterInfo.h" |
Nate Begeman | d4c8bea | 2004-11-25 07:09:01 +0000 | [diff] [blame] | 35 | #include "llvm/Target/TargetInstrInfo.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/Statistic.h" |
| 37 | #include "llvm/ADT/StringExtras.h" |
Misha Brukman | 0579449 | 2004-06-24 17:31:42 +0000 | [diff] [blame] | 38 | #include <set> |
Chris Lattner | a384079 | 2004-08-16 23:25:21 +0000 | [diff] [blame] | 39 | using namespace llvm; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 40 | |
| 41 | namespace { |
| 42 | Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed"); |
| 43 | |
Chris Lattner | ac7fd7f | 2005-11-14 18:52:46 +0000 | [diff] [blame] | 44 | class PPCAsmPrinter : public AsmPrinter { |
Chris Lattner | d717b19 | 2005-12-11 07:45:47 +0000 | [diff] [blame] | 45 | public: |
Misha Brukman | 97a296f | 2004-07-21 20:11:11 +0000 | [diff] [blame] | 46 | std::set<std::string> FnStubs, GVStubs, LinkOnceStubs; |
Chris Lattner | ac7fd7f | 2005-11-14 18:52:46 +0000 | [diff] [blame] | 47 | |
Chris Lattner | 4c7b43b | 2005-10-14 23:37:35 +0000 | [diff] [blame] | 48 | PPCAsmPrinter(std::ostream &O, TargetMachine &TM) |
Chris Lattner | 0745536 | 2005-11-21 08:14:07 +0000 | [diff] [blame] | 49 | : AsmPrinter(O, TM) {} |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 50 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 51 | virtual const char *getPassName() const { |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 52 | return "PowerPC Assembly Printer"; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Nate Begeman | 21e463b | 2005-10-16 05:39:50 +0000 | [diff] [blame] | 55 | PPCTargetMachine &getTM() { |
| 56 | return static_cast<PPCTargetMachine&>(TM); |
Chris Lattner | a384079 | 2004-08-16 23:25:21 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Nate Begeman | adeb43d | 2005-07-20 22:42:00 +0000 | [diff] [blame] | 59 | unsigned enumRegToMachineReg(unsigned enumReg) { |
| 60 | switch (enumReg) { |
| 61 | default: assert(0 && "Unhandled register!"); break; |
| 62 | case PPC::CR0: return 0; |
| 63 | case PPC::CR1: return 1; |
| 64 | case PPC::CR2: return 2; |
| 65 | case PPC::CR3: return 3; |
| 66 | case PPC::CR4: return 4; |
| 67 | case PPC::CR5: return 5; |
| 68 | case PPC::CR6: return 6; |
| 69 | case PPC::CR7: return 7; |
| 70 | } |
| 71 | abort(); |
| 72 | } |
| 73 | |
Nate Begeman | e59bf59 | 2004-08-14 22:09:10 +0000 | [diff] [blame] | 74 | /// printInstruction - This method is automatically generated by tablegen |
| 75 | /// from the instruction set description. This method returns true if the |
| 76 | /// machine instruction was sufficiently described to print it, otherwise it |
| 77 | /// returns false. |
| 78 | bool printInstruction(const MachineInstr *MI); |
| 79 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 80 | void printMachineInstruction(const MachineInstr *MI); |
Chris Lattner | 9ba13e4 | 2005-11-17 19:25:59 +0000 | [diff] [blame] | 81 | void printOp(const MachineOperand &MO); |
Chris Lattner | 7bb424f | 2004-08-14 23:27:29 +0000 | [diff] [blame] | 82 | |
Nate Begeman | 391c5d2 | 2005-11-30 18:54:35 +0000 | [diff] [blame] | 83 | void printOperand(const MachineInstr *MI, unsigned OpNo){ |
Chris Lattner | 7bb424f | 2004-08-14 23:27:29 +0000 | [diff] [blame] | 84 | const MachineOperand &MO = MI->getOperand(OpNo); |
| 85 | if (MO.getType() == MachineOperand::MO_MachineRegister) { |
| 86 | assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??"); |
Chris Lattner | 9dc4d3c | 2005-08-22 22:00:02 +0000 | [diff] [blame] | 87 | O << TM.getRegisterInfo()->get(MO.getReg()).Name; |
Chris Lattner | 0ea3171 | 2004-08-15 05:46:14 +0000 | [diff] [blame] | 88 | } else if (MO.isImmediate()) { |
| 89 | O << MO.getImmedValue(); |
Chris Lattner | 7bb424f | 2004-08-14 23:27:29 +0000 | [diff] [blame] | 90 | } else { |
| 91 | printOp(MO); |
| 92 | } |
| 93 | } |
| 94 | |
Nate Begeman | 391c5d2 | 2005-11-30 18:54:35 +0000 | [diff] [blame] | 95 | void printU5ImmOperand(const MachineInstr *MI, unsigned OpNo) { |
Nate Begeman | c330612 | 2004-08-21 05:56:39 +0000 | [diff] [blame] | 96 | unsigned char value = MI->getOperand(OpNo).getImmedValue(); |
Chris Lattner | 12585ba | 2004-08-21 19:11:03 +0000 | [diff] [blame] | 97 | assert(value <= 31 && "Invalid u5imm argument!"); |
Nate Begeman | c330612 | 2004-08-21 05:56:39 +0000 | [diff] [blame] | 98 | O << (unsigned int)value; |
| 99 | } |
Nate Begeman | 391c5d2 | 2005-11-30 18:54:35 +0000 | [diff] [blame] | 100 | void printU6ImmOperand(const MachineInstr *MI, unsigned OpNo) { |
Nate Begeman | 07aada8 | 2004-08-30 02:28:06 +0000 | [diff] [blame] | 101 | unsigned char value = MI->getOperand(OpNo).getImmedValue(); |
| 102 | assert(value <= 63 && "Invalid u6imm argument!"); |
| 103 | O << (unsigned int)value; |
| 104 | } |
Nate Begeman | 391c5d2 | 2005-11-30 18:54:35 +0000 | [diff] [blame] | 105 | void printS16ImmOperand(const MachineInstr *MI, unsigned OpNo) { |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 106 | O << (short)MI->getOperand(OpNo).getImmedValue(); |
| 107 | } |
Nate Begeman | 391c5d2 | 2005-11-30 18:54:35 +0000 | [diff] [blame] | 108 | void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo) { |
Chris Lattner | 97b2a2e | 2004-08-15 05:20:16 +0000 | [diff] [blame] | 109 | O << (unsigned short)MI->getOperand(OpNo).getImmedValue(); |
| 110 | } |
Nate Begeman | 391c5d2 | 2005-11-30 18:54:35 +0000 | [diff] [blame] | 111 | void printS16X4ImmOperand(const MachineInstr *MI, unsigned OpNo) { |
Chris Lattner | 841d12d | 2005-10-18 16:51:22 +0000 | [diff] [blame] | 112 | O << (short)MI->getOperand(OpNo).getImmedValue()*4; |
| 113 | } |
Nate Begeman | 391c5d2 | 2005-11-30 18:54:35 +0000 | [diff] [blame] | 114 | void printBranchOperand(const MachineInstr *MI, unsigned OpNo) { |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 115 | // Branches can take an immediate operand. This is used by the branch |
| 116 | // selection pass to print $+8, an eight byte displacement from the PC. |
| 117 | if (MI->getOperand(OpNo).isImmediate()) { |
Nate Begeman | 27499e3 | 2005-04-10 01:48:29 +0000 | [diff] [blame] | 118 | O << "$+" << MI->getOperand(OpNo).getImmedValue(); |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 119 | } else { |
Chris Lattner | 3e7f86a | 2005-11-17 19:16:08 +0000 | [diff] [blame] | 120 | printOp(MI->getOperand(OpNo)); |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 121 | } |
Nate Begeman | b7a8f2c | 2004-09-02 08:13:00 +0000 | [diff] [blame] | 122 | } |
Nate Begeman | 391c5d2 | 2005-11-30 18:54:35 +0000 | [diff] [blame] | 123 | void printCallOperand(const MachineInstr *MI, unsigned OpNo) { |
Chris Lattner | 9ba13e4 | 2005-11-17 19:25:59 +0000 | [diff] [blame] | 124 | const MachineOperand &MO = MI->getOperand(OpNo); |
Chris Lattner | 9542f971 | 2005-11-17 19:40:30 +0000 | [diff] [blame] | 125 | if (!PPCGenerateStaticCode) { |
| 126 | if (MO.getType() == MachineOperand::MO_ExternalSymbol) { |
| 127 | std::string Name(GlobalPrefix); Name += MO.getSymbolName(); |
| 128 | FnStubs.insert(Name); |
| 129 | O << "L" << Name << "$stub"; |
| 130 | return; |
| 131 | } else if (MO.getType() == MachineOperand::MO_GlobalAddress && |
| 132 | isa<Function>(MO.getGlobal()) && |
| 133 | cast<Function>(MO.getGlobal())->isExternal()) { |
| 134 | // Dynamically-resolved functions need a stub for the function. |
| 135 | std::string Name = Mang->getValueName(MO.getGlobal()); |
| 136 | FnStubs.insert(Name); |
| 137 | O << "L" << Name << "$stub"; |
| 138 | return; |
| 139 | } |
Chris Lattner | 9ba13e4 | 2005-11-17 19:25:59 +0000 | [diff] [blame] | 140 | } |
Chris Lattner | 9542f971 | 2005-11-17 19:40:30 +0000 | [diff] [blame] | 141 | |
| 142 | printOp(MI->getOperand(OpNo)); |
Chris Lattner | 3e7f86a | 2005-11-17 19:16:08 +0000 | [diff] [blame] | 143 | } |
Nate Begeman | 391c5d2 | 2005-11-30 18:54:35 +0000 | [diff] [blame] | 144 | void printAbsAddrOperand(const MachineInstr *MI, unsigned OpNo) { |
Nate Begeman | 422b0ce | 2005-11-16 00:48:01 +0000 | [diff] [blame] | 145 | O << (int)MI->getOperand(OpNo).getImmedValue()*4; |
| 146 | } |
Nate Begeman | 391c5d2 | 2005-11-30 18:54:35 +0000 | [diff] [blame] | 147 | void printPICLabel(const MachineInstr *MI, unsigned OpNo) { |
Nate Begeman | b7a8f2c | 2004-09-02 08:13:00 +0000 | [diff] [blame] | 148 | // FIXME: should probably be converted to cout.width and cout.fill |
Chris Lattner | 0745536 | 2005-11-21 08:14:07 +0000 | [diff] [blame] | 149 | O << "\"L0000" << getFunctionNumber() << "$pb\"\n"; |
| 150 | O << "\"L0000" << getFunctionNumber() << "$pb\":"; |
Nate Begeman | b7a8f2c | 2004-09-02 08:13:00 +0000 | [diff] [blame] | 151 | } |
Nate Begeman | 391c5d2 | 2005-11-30 18:54:35 +0000 | [diff] [blame] | 152 | void printSymbolHi(const MachineInstr *MI, unsigned OpNo) { |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 153 | if (MI->getOperand(OpNo).isImmediate()) { |
Nate Begeman | 391c5d2 | 2005-11-30 18:54:35 +0000 | [diff] [blame] | 154 | printS16ImmOperand(MI, OpNo); |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 155 | } else { |
| 156 | O << "ha16("; |
| 157 | printOp(MI->getOperand(OpNo)); |
| 158 | if (PICEnabled) |
Chris Lattner | 0745536 | 2005-11-21 08:14:07 +0000 | [diff] [blame] | 159 | O << "-\"L0000" << getFunctionNumber() << "$pb\")"; |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 160 | else |
| 161 | O << ')'; |
| 162 | } |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 163 | } |
Nate Begeman | 391c5d2 | 2005-11-30 18:54:35 +0000 | [diff] [blame] | 164 | void printSymbolLo(const MachineInstr *MI, unsigned OpNo) { |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 165 | if (MI->getOperand(OpNo).isImmediate()) { |
Nate Begeman | 391c5d2 | 2005-11-30 18:54:35 +0000 | [diff] [blame] | 166 | printS16ImmOperand(MI, OpNo); |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 167 | } else { |
| 168 | O << "lo16("; |
Nate Begeman | d4c8bea | 2004-11-25 07:09:01 +0000 | [diff] [blame] | 169 | printOp(MI->getOperand(OpNo)); |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 170 | if (PICEnabled) |
Chris Lattner | 0745536 | 2005-11-21 08:14:07 +0000 | [diff] [blame] | 171 | O << "-\"L0000" << getFunctionNumber() << "$pb\")"; |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 172 | else |
| 173 | O << ')'; |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 174 | } |
| 175 | } |
Nate Begeman | 391c5d2 | 2005-11-30 18:54:35 +0000 | [diff] [blame] | 176 | void printcrbitm(const MachineInstr *MI, unsigned OpNo) { |
Nate Begeman | adeb43d | 2005-07-20 22:42:00 +0000 | [diff] [blame] | 177 | unsigned CCReg = MI->getOperand(OpNo).getReg(); |
| 178 | unsigned RegNo = enumRegToMachineReg(CCReg); |
| 179 | O << (0x80 >> RegNo); |
| 180 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 181 | |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 182 | virtual bool runOnMachineFunction(MachineFunction &F) = 0; |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 183 | virtual bool doFinalization(Module &M) = 0; |
| 184 | }; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 185 | |
Misha Brukman | 3e0b51a | 2004-09-05 02:42:44 +0000 | [diff] [blame] | 186 | /// DarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac OS |
| 187 | /// X |
| 188 | /// |
Chris Lattner | 4c7b43b | 2005-10-14 23:37:35 +0000 | [diff] [blame] | 189 | struct DarwinAsmPrinter : public PPCAsmPrinter { |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 190 | |
| 191 | DarwinAsmPrinter(std::ostream &O, TargetMachine &TM) |
Chris Lattner | 4c7b43b | 2005-10-14 23:37:35 +0000 | [diff] [blame] | 192 | : PPCAsmPrinter(O, TM) { |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 193 | CommentString = ";"; |
| 194 | GlobalPrefix = "_"; |
Chris Lattner | f55366e | 2005-11-21 06:47:58 +0000 | [diff] [blame] | 195 | PrivateGlobalPrefix = "L"; // Marker for constant pool idxs |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 196 | ZeroDirective = "\t.space\t"; // ".space N" emits N zeros. |
| 197 | Data64bitsDirective = 0; // we can't emit a 64-bit unit |
| 198 | AlignmentIsInBytes = false; // Alignment is by power of 2. |
Chris Lattner | c569e61 | 2005-11-21 08:26:15 +0000 | [diff] [blame] | 199 | ConstantPoolSection = "\t.const\t"; |
Chris Lattner | d717b19 | 2005-12-11 07:45:47 +0000 | [diff] [blame] | 200 | LCOMMDirective = "\t.lcomm\t"; |
Chris Lattner | d1239b7 | 2005-12-13 06:32:50 +0000 | [diff] [blame^] | 201 | StaticCtorsSection = ".mod_init_func"; |
| 202 | StaticDtorsSection = ".mod_term_func"; |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | virtual const char *getPassName() const { |
| 206 | return "Darwin PPC Assembly Printer"; |
| 207 | } |
Chris Lattner | 646f7af | 2005-12-09 18:24:29 +0000 | [diff] [blame] | 208 | |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 209 | bool runOnMachineFunction(MachineFunction &F); |
Nate Begeman | 18ed029 | 2005-07-21 01:25:49 +0000 | [diff] [blame] | 210 | bool doInitialization(Module &M); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 211 | bool doFinalization(Module &M); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 212 | }; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 213 | |
Misha Brukman | 3e0b51a | 2004-09-05 02:42:44 +0000 | [diff] [blame] | 214 | /// AIXAsmPrinter - PowerPC assembly printer, customized for AIX |
| 215 | /// |
Chris Lattner | 4c7b43b | 2005-10-14 23:37:35 +0000 | [diff] [blame] | 216 | struct AIXAsmPrinter : public PPCAsmPrinter { |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 217 | /// Map for labels corresponding to global variables |
| 218 | /// |
| 219 | std::map<const GlobalVariable*,std::string> GVToLabelMap; |
| 220 | |
| 221 | AIXAsmPrinter(std::ostream &O, TargetMachine &TM) |
Chris Lattner | 4c7b43b | 2005-10-14 23:37:35 +0000 | [diff] [blame] | 222 | : PPCAsmPrinter(O, TM) { |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 223 | CommentString = "#"; |
Chris Lattner | 3459bfb | 2005-11-10 18:20:29 +0000 | [diff] [blame] | 224 | GlobalPrefix = "."; |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 225 | ZeroDirective = "\t.space\t"; // ".space N" emits N zeros. |
| 226 | Data64bitsDirective = 0; // we can't emit a 64-bit unit |
| 227 | AlignmentIsInBytes = false; // Alignment is by power of 2. |
Chris Lattner | c569e61 | 2005-11-21 08:26:15 +0000 | [diff] [blame] | 228 | ConstantPoolSection = "\t.const\t"; |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 229 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 230 | |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 231 | virtual const char *getPassName() const { |
| 232 | return "AIX PPC Assembly Printer"; |
| 233 | } |
| 234 | |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 235 | bool runOnMachineFunction(MachineFunction &F); |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 236 | bool doInitialization(Module &M); |
| 237 | bool doFinalization(Module &M); |
| 238 | }; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 239 | } // end of anonymous namespace |
| 240 | |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 241 | /// createDarwinAsmPrinterPass - Returns a pass that prints the PPC assembly |
| 242 | /// code for a MachineFunction to the given output stream, in a format that the |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 243 | /// Darwin assembler can deal with. |
| 244 | /// |
| 245 | FunctionPass *llvm::createDarwinAsmPrinter(std::ostream &o, TargetMachine &tm) { |
| 246 | return new DarwinAsmPrinter(o, tm); |
| 247 | } |
| 248 | |
| 249 | /// createAIXAsmPrinterPass - Returns a pass that prints the PPC assembly code |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 250 | /// for a MachineFunction to the given output stream, in a format that the |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 251 | /// AIX 5L assembler can deal with. |
| 252 | /// |
| 253 | FunctionPass *llvm::createAIXAsmPrinter(std::ostream &o, TargetMachine &tm) { |
| 254 | return new AIXAsmPrinter(o, tm); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Nate Begeman | e59bf59 | 2004-08-14 22:09:10 +0000 | [diff] [blame] | 257 | // Include the auto-generated portion of the assembly writer |
Chris Lattner | 4c7b43b | 2005-10-14 23:37:35 +0000 | [diff] [blame] | 258 | #include "PPCGenAsmWriter.inc" |
Nate Begeman | e59bf59 | 2004-08-14 22:09:10 +0000 | [diff] [blame] | 259 | |
Chris Lattner | 9ba13e4 | 2005-11-17 19:25:59 +0000 | [diff] [blame] | 260 | void PPCAsmPrinter::printOp(const MachineOperand &MO) { |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 261 | const MRegisterInfo &RI = *TM.getRegisterInfo(); |
| 262 | int new_symbol; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 263 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 264 | switch (MO.getType()) { |
| 265 | case MachineOperand::MO_VirtualRegister: |
| 266 | if (Value *V = MO.getVRegValueOrNull()) { |
| 267 | O << "<" << V->getName() << ">"; |
| 268 | return; |
| 269 | } |
| 270 | // FALLTHROUGH |
| 271 | case MachineOperand::MO_MachineRegister: |
Misha Brukman | 05fcd0c | 2004-07-08 17:58:04 +0000 | [diff] [blame] | 272 | case MachineOperand::MO_CCRegister: |
Chris Lattner | 9dc4d3c | 2005-08-22 22:00:02 +0000 | [diff] [blame] | 273 | O << RI.get(MO.getReg()).Name; |
Misha Brukman | 7f484a5 | 2004-06-24 23:51:00 +0000 | [diff] [blame] | 274 | return; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 275 | |
| 276 | case MachineOperand::MO_SignExtendedImmed: |
Misha Brukman | af313fb | 2004-07-28 00:00:48 +0000 | [diff] [blame] | 277 | case MachineOperand::MO_UnextendedImmed: |
| 278 | std::cerr << "printOp() does not handle immediate values\n"; |
| 279 | abort(); |
Misha Brukman | 97a296f | 2004-07-21 20:11:11 +0000 | [diff] [blame] | 280 | return; |
| 281 | |
Misha Brukman | 05fcd0c | 2004-07-08 17:58:04 +0000 | [diff] [blame] | 282 | case MachineOperand::MO_PCRelativeDisp: |
| 283 | std::cerr << "Shouldn't use addPCDisp() when building PPC MachineInstrs"; |
| 284 | abort(); |
| 285 | return; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 286 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 287 | case MachineOperand::MO_MachineBasicBlock: { |
| 288 | MachineBasicBlock *MBBOp = MO.getMachineBasicBlock(); |
Chris Lattner | 0745536 | 2005-11-21 08:14:07 +0000 | [diff] [blame] | 289 | O << PrivateGlobalPrefix << "BB" << getFunctionNumber() << "_" |
Chris Lattner | 7f9ccde | 2005-11-21 07:41:05 +0000 | [diff] [blame] | 290 | << MBBOp->getNumber() << "\t; " << MBBOp->getBasicBlock()->getName(); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 291 | return; |
| 292 | } |
Misha Brukman | 05fcd0c | 2004-07-08 17:58:04 +0000 | [diff] [blame] | 293 | |
| 294 | case MachineOperand::MO_ConstantPoolIndex: |
Chris Lattner | 0745536 | 2005-11-21 08:14:07 +0000 | [diff] [blame] | 295 | O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() |
Chris Lattner | f55366e | 2005-11-21 06:47:58 +0000 | [diff] [blame] | 296 | << '_' << MO.getConstantPoolIndex(); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 297 | return; |
Misha Brukman | 05fcd0c | 2004-07-08 17:58:04 +0000 | [diff] [blame] | 298 | |
| 299 | case MachineOperand::MO_ExternalSymbol: |
Nate Begeman | 01d0526 | 2005-03-30 01:45:43 +0000 | [diff] [blame] | 300 | O << GlobalPrefix << MO.getSymbolName(); |
Misha Brukman | 05fcd0c | 2004-07-08 17:58:04 +0000 | [diff] [blame] | 301 | return; |
| 302 | |
Nate Begeman | b73a711 | 2004-08-13 09:32:01 +0000 | [diff] [blame] | 303 | case MachineOperand::MO_GlobalAddress: { |
| 304 | GlobalValue *GV = MO.getGlobal(); |
| 305 | std::string Name = Mang->getValueName(GV); |
Misha Brukman | e2eceb5 | 2004-07-23 16:08:20 +0000 | [diff] [blame] | 306 | |
Nate Begeman | fcf4a42 | 2004-10-17 23:01:34 +0000 | [diff] [blame] | 307 | // External or weakly linked global variables need non-lazily-resolved stubs |
Chris Lattner | 9542f971 | 2005-11-17 19:40:30 +0000 | [diff] [blame] | 308 | if (!PPCGenerateStaticCode && |
| 309 | ((GV->isExternal() || GV->hasWeakLinkage() || |
| 310 | GV->hasLinkOnceLinkage()))) { |
Nate Begeman | d4c8bea | 2004-11-25 07:09:01 +0000 | [diff] [blame] | 311 | if (GV->hasLinkOnceLinkage()) |
| 312 | LinkOnceStubs.insert(Name); |
| 313 | else |
| 314 | GVStubs.insert(Name); |
Nate Begeman | b73a711 | 2004-08-13 09:32:01 +0000 | [diff] [blame] | 315 | O << "L" << Name << "$non_lazy_ptr"; |
| 316 | return; |
| 317 | } |
Nate Begeman | d4c8bea | 2004-11-25 07:09:01 +0000 | [diff] [blame] | 318 | |
Chris Lattner | 9542f971 | 2005-11-17 19:40:30 +0000 | [diff] [blame] | 319 | O << Name; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 320 | return; |
Nate Begeman | b73a711 | 2004-08-13 09:32:01 +0000 | [diff] [blame] | 321 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 322 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 323 | default: |
Misha Brukman | 05fcd0c | 2004-07-08 17:58:04 +0000 | [diff] [blame] | 324 | O << "<unknown operand type: " << MO.getType() << ">"; |
Misha Brukman | 22e1207 | 2004-06-25 15:11:34 +0000 | [diff] [blame] | 325 | return; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | |
Nate Begeman | e59bf59 | 2004-08-14 22:09:10 +0000 | [diff] [blame] | 329 | /// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to |
| 330 | /// the current output stream. |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 331 | /// |
Chris Lattner | 4c7b43b | 2005-10-14 23:37:35 +0000 | [diff] [blame] | 332 | void PPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) { |
Nate Begeman | e59bf59 | 2004-08-14 22:09:10 +0000 | [diff] [blame] | 333 | ++EmittedInsts; |
Chris Lattner | 617742b | 2005-10-14 22:44:13 +0000 | [diff] [blame] | 334 | |
Nate Begeman | ad5f65c | 2005-04-05 18:19:50 +0000 | [diff] [blame] | 335 | // Check for slwi/srwi mnemonics. |
| 336 | if (MI->getOpcode() == PPC::RLWINM) { |
| 337 | bool FoundMnemonic = false; |
| 338 | unsigned char SH = MI->getOperand(2).getImmedValue(); |
| 339 | unsigned char MB = MI->getOperand(3).getImmedValue(); |
| 340 | unsigned char ME = MI->getOperand(4).getImmedValue(); |
| 341 | if (SH <= 31 && MB == 0 && ME == (31-SH)) { |
| 342 | O << "slwi "; FoundMnemonic = true; |
| 343 | } |
| 344 | if (SH <= 31 && MB == (32-SH) && ME == 31) { |
| 345 | O << "srwi "; FoundMnemonic = true; |
| 346 | SH = 32-SH; |
| 347 | } |
| 348 | if (FoundMnemonic) { |
Nate Begeman | 391c5d2 | 2005-11-30 18:54:35 +0000 | [diff] [blame] | 349 | printOperand(MI, 0); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 350 | O << ", "; |
Nate Begeman | 391c5d2 | 2005-11-30 18:54:35 +0000 | [diff] [blame] | 351 | printOperand(MI, 1); |
Nate Begeman | ad5f65c | 2005-04-05 18:19:50 +0000 | [diff] [blame] | 352 | O << ", " << (unsigned int)SH << "\n"; |
| 353 | return; |
| 354 | } |
| 355 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 356 | |
Nate Begeman | e59bf59 | 2004-08-14 22:09:10 +0000 | [diff] [blame] | 357 | if (printInstruction(MI)) |
| 358 | return; // Printer was automatically generated |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 359 | |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 360 | assert(0 && "Unhandled instruction in asm writer!"); |
| 361 | abort(); |
Nate Begeman | e59bf59 | 2004-08-14 22:09:10 +0000 | [diff] [blame] | 362 | return; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Chris Lattner | ef65874 | 2005-11-21 07:57:37 +0000 | [diff] [blame] | 365 | |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 366 | /// runOnMachineFunction - This uses the printMachineInstruction() |
| 367 | /// method to print assembly for each instruction. |
| 368 | /// |
| 369 | bool DarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) { |
Chris Lattner | 8b8b951 | 2005-11-21 07:51:23 +0000 | [diff] [blame] | 370 | SetupMachineFunction(MF); |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 371 | O << "\n\n"; |
| 372 | |
| 373 | // Print out constants referenced by the function |
Chris Lattner | c569e61 | 2005-11-21 08:26:15 +0000 | [diff] [blame] | 374 | EmitConstantPool(MF.getConstantPool()); |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 375 | |
| 376 | // Print out labels for the function. |
Chris Lattner | ac7fd7f | 2005-11-14 18:52:46 +0000 | [diff] [blame] | 377 | const Function *F = MF.getFunction(); |
| 378 | SwitchSection(".text", F); |
Chris Lattner | 8b8b951 | 2005-11-21 07:51:23 +0000 | [diff] [blame] | 379 | EmitAlignment(4, F); |
Chris Lattner | ac7fd7f | 2005-11-14 18:52:46 +0000 | [diff] [blame] | 380 | if (!F->hasInternalLinkage()) |
Chris Lattner | f02a916 | 2005-10-28 18:44:07 +0000 | [diff] [blame] | 381 | O << "\t.globl\t" << CurrentFnName << "\n"; |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 382 | O << CurrentFnName << ":\n"; |
| 383 | |
| 384 | // Print out code for the function. |
| 385 | for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); |
| 386 | I != E; ++I) { |
| 387 | // Print a label for the basic block. |
Chris Lattner | 1db1adb | 2005-08-21 19:09:33 +0000 | [diff] [blame] | 388 | if (I != MF.begin()) { |
Chris Lattner | 0745536 | 2005-11-21 08:14:07 +0000 | [diff] [blame] | 389 | O << PrivateGlobalPrefix << "BB" << getFunctionNumber() << '_' |
Chris Lattner | 7f9ccde | 2005-11-21 07:41:05 +0000 | [diff] [blame] | 390 | << I->getNumber() << ":\t"; |
Chris Lattner | 1db1adb | 2005-08-21 19:09:33 +0000 | [diff] [blame] | 391 | if (!I->getBasicBlock()->getName().empty()) |
| 392 | O << CommentString << " " << I->getBasicBlock()->getName(); |
| 393 | O << "\n"; |
| 394 | } |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 395 | for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end(); |
| 396 | II != E; ++II) { |
| 397 | // Print the assembly for the instruction. |
| 398 | O << "\t"; |
| 399 | printMachineInstruction(II); |
| 400 | } |
| 401 | } |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 402 | |
| 403 | // We didn't modify anything. |
| 404 | return false; |
| 405 | } |
| 406 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 407 | |
Nate Begeman | 18ed029 | 2005-07-21 01:25:49 +0000 | [diff] [blame] | 408 | bool DarwinAsmPrinter::doInitialization(Module &M) { |
Chris Lattner | 3c304a3 | 2005-08-05 22:05:03 +0000 | [diff] [blame] | 409 | if (TM.getSubtarget<PPCSubtarget>().isGigaProcessor()) |
| 410 | O << "\t.machine ppc970\n"; |
Nate Begeman | 18ed029 | 2005-07-21 01:25:49 +0000 | [diff] [blame] | 411 | AsmPrinter::doInitialization(M); |
Chris Lattner | 85eac0d | 2005-11-10 19:33:43 +0000 | [diff] [blame] | 412 | |
| 413 | // Darwin wants symbols to be quoted if they have complex names. |
| 414 | Mang->setUseQuotes(true); |
Nate Begeman | 18ed029 | 2005-07-21 01:25:49 +0000 | [diff] [blame] | 415 | return false; |
| 416 | } |
| 417 | |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 418 | bool DarwinAsmPrinter::doFinalization(Module &M) { |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 419 | const TargetData &TD = TM.getTargetData(); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 420 | |
| 421 | // Print out module-level global variables here. |
Chris Lattner | ced704b | 2005-11-14 19:00:30 +0000 | [diff] [blame] | 422 | for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); |
Chris Lattner | deea416 | 2005-12-13 04:33:58 +0000 | [diff] [blame] | 423 | I != E; ++I) { |
| 424 | if (!I->hasInitializer()) continue; // External global require no code |
| 425 | |
Chris Lattner | d1239b7 | 2005-12-13 06:32:50 +0000 | [diff] [blame^] | 426 | // Check to see if this is a special global used by LLVM, if so, emit it. |
| 427 | if (I->hasAppendingLinkage() && EmitSpecialLLVMGlobal(I)) |
| 428 | continue; |
Chris Lattner | deea416 | 2005-12-13 04:33:58 +0000 | [diff] [blame] | 429 | |
| 430 | O << '\n'; |
| 431 | std::string name = Mang->getValueName(I); |
| 432 | Constant *C = I->getInitializer(); |
| 433 | unsigned Size = TD.getTypeSize(C->getType()); |
| 434 | unsigned Align = TD.getTypeAlignmentShift(C->getType()); |
| 435 | |
| 436 | if (C->isNullValue() && /* FIXME: Verify correct */ |
| 437 | (I->hasInternalLinkage() || I->hasWeakLinkage() || |
| 438 | I->hasLinkOnceLinkage())) { |
| 439 | SwitchSection(".data", I); |
| 440 | if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it. |
| 441 | if (I->hasInternalLinkage()) |
| 442 | O << LCOMMDirective << name << "," << Size << "," << Align; |
| 443 | else |
| 444 | O << ".comm " << name << "," << Size; |
| 445 | O << "\t\t; '" << I->getName() << "'\n"; |
| 446 | } else { |
| 447 | switch (I->getLinkage()) { |
| 448 | case GlobalValue::LinkOnceLinkage: |
| 449 | SwitchSection("", 0); |
| 450 | O << ".section __TEXT,__textcoal_nt,coalesced,no_toc\n" |
| 451 | << ".weak_definition " << name << '\n' |
| 452 | << ".private_extern " << name << '\n' |
| 453 | << ".section __DATA,__datacoal_nt,coalesced,no_toc\n"; |
| 454 | LinkOnceStubs.insert(name); |
| 455 | break; |
| 456 | case GlobalValue::WeakLinkage: |
| 457 | O << ".weak_definition " << name << '\n' |
| 458 | << ".private_extern " << name << '\n'; |
| 459 | break; |
| 460 | case GlobalValue::AppendingLinkage: |
| 461 | // FIXME: appending linkage variables should go into a section of |
| 462 | // their name or something. For now, just emit them as external. |
| 463 | case GlobalValue::ExternalLinkage: |
| 464 | // If external or appending, declare as a global symbol |
| 465 | O << "\t.globl " << name << "\n"; |
| 466 | // FALL THROUGH |
| 467 | case GlobalValue::InternalLinkage: |
| 468 | SwitchSection(".data", I); |
| 469 | break; |
| 470 | default: |
| 471 | std::cerr << "Unknown linkage type!"; |
| 472 | abort(); |
| 473 | } |
| 474 | |
| 475 | EmitAlignment(Align, I); |
| 476 | O << name << ":\t\t\t\t; '" << I->getName() << "'\n"; |
| 477 | EmitGlobalConstant(C); |
| 478 | } |
| 479 | } |
Misha Brukman | da2b13f | 2004-07-16 20:29:04 +0000 | [diff] [blame] | 480 | |
| 481 | // Output stubs for dynamically-linked functions |
Chris Lattner | deea416 | 2005-12-13 04:33:58 +0000 | [diff] [blame] | 482 | if (PICEnabled) { |
| 483 | for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end(); |
| 484 | i != e; ++i) { |
| 485 | O << ".data\n"; |
| 486 | O<<".section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32\n"; |
| 487 | EmitAlignment(2); |
| 488 | O << "L" << *i << "$stub:\n"; |
| 489 | O << "\t.indirect_symbol " << *i << "\n"; |
| 490 | O << "\tmflr r0\n"; |
| 491 | O << "\tbcl 20,31,L0$" << *i << "\n"; |
| 492 | O << "L0$" << *i << ":\n"; |
| 493 | O << "\tmflr r11\n"; |
| 494 | O << "\taddis r11,r11,ha16(L" << *i << "$lazy_ptr-L0$" << *i << ")\n"; |
| 495 | O << "\tmtlr r0\n"; |
| 496 | O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr-L0$" << *i << ")(r11)\n"; |
| 497 | O << "\tmtctr r12\n"; |
| 498 | O << "\tbctr\n"; |
| 499 | O << ".data\n"; |
| 500 | O << ".lazy_symbol_pointer\n"; |
| 501 | O << "L" << *i << "$lazy_ptr:\n"; |
| 502 | O << "\t.indirect_symbol " << *i << "\n"; |
| 503 | O << "\t.long dyld_stub_binding_helper\n"; |
| 504 | } |
| 505 | } else { |
| 506 | for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end(); |
| 507 | i != e; ++i) { |
| 508 | O<<"\t.section __TEXT,__symbol_stub1,symbol_stubs,pure_instructions,16\n"; |
| 509 | EmitAlignment(4); |
| 510 | O << "L" << *i << "$stub:\n"; |
| 511 | O << "\t.indirect_symbol " << *i << "\n"; |
| 512 | O << "\tlis r11,ha16(L" << *i << "$lazy_ptr)\n"; |
| 513 | O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr)(r11)\n"; |
| 514 | O << "\tmtctr r12\n"; |
| 515 | O << "\tbctr\n"; |
| 516 | O << "\t.lazy_symbol_pointer\n"; |
| 517 | O << "L" << *i << "$lazy_ptr:\n"; |
| 518 | O << "\t.indirect_symbol " << *i << "\n"; |
| 519 | O << "\t.long dyld_stub_binding_helper\n"; |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 520 | } |
Misha Brukman | 46fd00a | 2004-06-24 23:04:11 +0000 | [diff] [blame] | 521 | } |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 522 | |
Misha Brukman | da2b13f | 2004-07-16 20:29:04 +0000 | [diff] [blame] | 523 | O << "\n"; |
| 524 | |
| 525 | // Output stubs for external global variables |
| 526 | if (GVStubs.begin() != GVStubs.end()) |
Misha Brukman | e2eceb5 | 2004-07-23 16:08:20 +0000 | [diff] [blame] | 527 | O << ".data\n.non_lazy_symbol_pointer\n"; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 528 | for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end(); |
Misha Brukman | da2b13f | 2004-07-16 20:29:04 +0000 | [diff] [blame] | 529 | i != e; ++i) { |
| 530 | O << "L" << *i << "$non_lazy_ptr:\n"; |
| 531 | O << "\t.indirect_symbol " << *i << "\n"; |
| 532 | O << "\t.long\t0\n"; |
| 533 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 534 | |
Nate Begeman | e59bf59 | 2004-08-14 22:09:10 +0000 | [diff] [blame] | 535 | // Output stubs for link-once variables |
Chris Lattner | deea416 | 2005-12-13 04:33:58 +0000 | [diff] [blame] | 536 | if (LinkOnceStubs.begin() != LinkOnceStubs.end()) { |
Nate Begeman | e59bf59 | 2004-08-14 22:09:10 +0000 | [diff] [blame] | 537 | O << ".data\n.align 2\n"; |
Chris Lattner | deea416 | 2005-12-13 04:33:58 +0000 | [diff] [blame] | 538 | for (std::set<std::string>::iterator i = LinkOnceStubs.begin(), |
Nate Begeman | e59bf59 | 2004-08-14 22:09:10 +0000 | [diff] [blame] | 539 | e = LinkOnceStubs.end(); i != e; ++i) { |
Chris Lattner | deea416 | 2005-12-13 04:33:58 +0000 | [diff] [blame] | 540 | O << "L" << *i << "$non_lazy_ptr:\n" |
| 541 | << "\t.long\t" << *i << '\n'; |
| 542 | } |
Nate Begeman | e59bf59 | 2004-08-14 22:09:10 +0000 | [diff] [blame] | 543 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 544 | |
Chris Lattner | 5b0ac99 | 2005-11-01 00:12:36 +0000 | [diff] [blame] | 545 | // Funny Darwin hack: This flag tells the linker that no global symbols |
| 546 | // contain code that falls through to other global symbols (e.g. the obvious |
| 547 | // implementation of multiple entry points). If this doesn't occur, the |
| 548 | // linker can safely perform dead code stripping. Since LLVM never generates |
| 549 | // code that does this, it is always safe to set. |
| 550 | O << "\t.subsections_via_symbols\n"; |
| 551 | |
Chris Lattner | a384079 | 2004-08-16 23:25:21 +0000 | [diff] [blame] | 552 | AsmPrinter::doFinalization(M); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 553 | return false; // success |
| 554 | } |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 555 | |
| 556 | /// runOnMachineFunction - This uses the printMachineInstruction() |
| 557 | /// method to print assembly for each instruction. |
| 558 | /// |
| 559 | bool AIXAsmPrinter::runOnMachineFunction(MachineFunction &MF) { |
Chris Lattner | 6d5a4f6 | 2005-11-21 08:02:41 +0000 | [diff] [blame] | 560 | SetupMachineFunction(MF); |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 561 | |
| 562 | // Print out constants referenced by the function |
Chris Lattner | c569e61 | 2005-11-21 08:26:15 +0000 | [diff] [blame] | 563 | EmitConstantPool(MF.getConstantPool()); |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 564 | |
| 565 | // Print out header for the function. |
| 566 | O << "\t.csect .text[PR]\n" |
| 567 | << "\t.align 2\n" |
| 568 | << "\t.globl " << CurrentFnName << '\n' |
| 569 | << "\t.globl ." << CurrentFnName << '\n' |
| 570 | << "\t.csect " << CurrentFnName << "[DS],3\n" |
| 571 | << CurrentFnName << ":\n" |
| 572 | << "\t.llong ." << CurrentFnName << ", TOC[tc0], 0\n" |
| 573 | << "\t.csect .text[PR]\n" |
| 574 | << '.' << CurrentFnName << ":\n"; |
| 575 | |
| 576 | // Print out code for the function. |
| 577 | for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); |
| 578 | I != E; ++I) { |
| 579 | // Print a label for the basic block. |
Chris Lattner | 0745536 | 2005-11-21 08:14:07 +0000 | [diff] [blame] | 580 | O << PrivateGlobalPrefix << "BB" << getFunctionNumber() << '_' |
| 581 | << I->getNumber() |
Chris Lattner | 6d5a4f6 | 2005-11-21 08:02:41 +0000 | [diff] [blame] | 582 | << ":\t" << CommentString << I->getBasicBlock()->getName() << '\n'; |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 583 | for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end(); |
| 584 | II != E; ++II) { |
| 585 | // Print the assembly for the instruction. |
| 586 | O << "\t"; |
| 587 | printMachineInstruction(II); |
| 588 | } |
| 589 | } |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 590 | |
| 591 | O << "LT.." << CurrentFnName << ":\n" |
| 592 | << "\t.long 0\n" |
| 593 | << "\t.byte 0,0,32,65,128,0,0,0\n" |
| 594 | << "\t.long LT.." << CurrentFnName << "-." << CurrentFnName << '\n' |
| 595 | << "\t.short 3\n" |
| 596 | << "\t.byte \"" << CurrentFnName << "\"\n" |
| 597 | << "\t.align 2\n"; |
| 598 | |
| 599 | // We didn't modify anything. |
| 600 | return false; |
| 601 | } |
| 602 | |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 603 | bool AIXAsmPrinter::doInitialization(Module &M) { |
Chris Lattner | ac7fd7f | 2005-11-14 18:52:46 +0000 | [diff] [blame] | 604 | SwitchSection("", 0); |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 605 | const TargetData &TD = TM.getTargetData(); |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 606 | |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 607 | O << "\t.machine \"ppc64\"\n" |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 608 | << "\t.toc\n" |
| 609 | << "\t.csect .text[PR]\n"; |
| 610 | |
| 611 | // Print out module-level global variables |
Chris Lattner | 2e00d7d | 2005-07-26 19:03:27 +0000 | [diff] [blame] | 612 | for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); |
| 613 | I != E; ++I) { |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 614 | if (!I->hasInitializer()) |
| 615 | continue; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 616 | |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 617 | std::string Name = I->getName(); |
| 618 | Constant *C = I->getInitializer(); |
| 619 | // N.B.: We are defaulting to writable strings |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 620 | if (I->hasExternalLinkage()) { |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 621 | O << "\t.globl " << Name << '\n' |
| 622 | << "\t.csect .data[RW],3\n"; |
| 623 | } else { |
| 624 | O << "\t.csect _global.rw_c[RW],3\n"; |
| 625 | } |
| 626 | O << Name << ":\n"; |
Chris Lattner | 8b8b951 | 2005-11-21 07:51:23 +0000 | [diff] [blame] | 627 | EmitGlobalConstant(C); |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | // Output labels for globals |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 631 | if (M.global_begin() != M.global_end()) O << "\t.toc\n"; |
Chris Lattner | 2e00d7d | 2005-07-26 19:03:27 +0000 | [diff] [blame] | 632 | for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); |
| 633 | I != E; ++I) { |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 634 | const GlobalVariable *GV = I; |
| 635 | // Do not output labels for unused variables |
| 636 | if (GV->isExternal() && GV->use_begin() == GV->use_end()) |
| 637 | continue; |
| 638 | |
Chris Lattner | 0745536 | 2005-11-21 08:14:07 +0000 | [diff] [blame] | 639 | IncrementFunctionNumber(); |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 640 | std::string Name = GV->getName(); |
Chris Lattner | 0745536 | 2005-11-21 08:14:07 +0000 | [diff] [blame] | 641 | std::string Label = "LC.." + utostr(getFunctionNumber()); |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 642 | GVToLabelMap[GV] = Label; |
| 643 | O << Label << ":\n" |
| 644 | << "\t.tc " << Name << "[TC]," << Name; |
| 645 | if (GV->isExternal()) O << "[RW]"; |
| 646 | O << '\n'; |
Chris Lattner | 0745536 | 2005-11-21 08:14:07 +0000 | [diff] [blame] | 647 | } |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 648 | |
Chris Lattner | 3459bfb | 2005-11-10 18:20:29 +0000 | [diff] [blame] | 649 | AsmPrinter::doInitialization(M); |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 650 | return false; // success |
| 651 | } |
| 652 | |
| 653 | bool AIXAsmPrinter::doFinalization(Module &M) { |
| 654 | const TargetData &TD = TM.getTargetData(); |
| 655 | // Print out module-level global variables |
Chris Lattner | 2e00d7d | 2005-07-26 19:03:27 +0000 | [diff] [blame] | 656 | for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); |
| 657 | I != E; ++I) { |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 658 | if (I->hasInitializer() || I->hasExternalLinkage()) |
| 659 | continue; |
| 660 | |
| 661 | std::string Name = I->getName(); |
| 662 | if (I->hasInternalLinkage()) { |
| 663 | O << "\t.lcomm " << Name << ",16,_global.bss_c"; |
| 664 | } else { |
| 665 | O << "\t.comm " << Name << "," << TD.getTypeSize(I->getType()) |
Chris Lattner | 0561b3f | 2005-08-02 19:26:06 +0000 | [diff] [blame] | 666 | << "," << Log2_32((unsigned)TD.getTypeAlignment(I->getType())); |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 667 | } |
Chris Lattner | 6d5a4f6 | 2005-11-21 08:02:41 +0000 | [diff] [blame] | 668 | O << "\t\t" << CommentString << " "; |
Chris Lattner | 8ca0291 | 2005-10-03 07:08:36 +0000 | [diff] [blame] | 669 | WriteAsOperand(O, I, false, true, &M); |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 670 | O << "\n"; |
| 671 | } |
| 672 | |
| 673 | O << "_section_.text:\n" |
| 674 | << "\t.csect .data[RW],3\n" |
| 675 | << "\t.llong _section_.text\n"; |
Chris Lattner | 3459bfb | 2005-11-10 18:20:29 +0000 | [diff] [blame] | 676 | AsmPrinter::doFinalization(M); |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 677 | return false; // success |
| 678 | } |