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