Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 1 | //===-- PowerPCAsmPrinter.cpp - Print machine instrs to PowerPC assembly --===// |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Misha Brukman | 05fcd0c | 2004-07-08 17:58:04 +0000 | [diff] [blame] | 10 | // This file contains a printer that converts from our internal representation |
| 11 | // of machine-dependent LLVM code to PowerPC assembly language. This printer is |
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" |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 20 | #include "PowerPC.h" |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 21 | #include "PowerPCTargetMachine.h" |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 22 | #include "llvm/Constants.h" |
| 23 | #include "llvm/DerivedTypes.h" |
| 24 | #include "llvm/Module.h" |
| 25 | #include "llvm/Assembly/Writer.h" |
Chris Lattner | a384079 | 2004-08-16 23:25:21 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/AsmPrinter.h" |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MachineConstantPool.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" |
Chris Lattner | 7bb424f | 2004-08-14 23:27:29 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/ValueTypes.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" |
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 | |
Misha Brukman | 3e0b51a | 2004-09-05 02:42:44 +0000 | [diff] [blame] | 44 | struct PowerPCAsmPrinter : public AsmPrinter { |
Misha Brukman | 97a296f | 2004-07-21 20:11:11 +0000 | [diff] [blame] | 45 | std::set<std::string> FnStubs, GVStubs, LinkOnceStubs; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 46 | std::set<std::string> Strings; |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 47 | |
Misha Brukman | 3e0b51a | 2004-09-05 02:42:44 +0000 | [diff] [blame] | 48 | PowerPCAsmPrinter(std::ostream &O, TargetMachine &TM) |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 49 | : AsmPrinter(O, TM), LabelNumber(0) {} |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 50 | |
Misha Brukman | cf8d244 | 2004-07-26 16:28:33 +0000 | [diff] [blame] | 51 | /// Unique incrementer for label values for referencing Global values. |
Misha Brukman | 218bec7 | 2004-06-29 17:13:26 +0000 | [diff] [blame] | 52 | /// |
Misha Brukman | cf8d244 | 2004-07-26 16:28:33 +0000 | [diff] [blame] | 53 | unsigned LabelNumber; |
| 54 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 55 | virtual const char *getPassName() const { |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 56 | return "PowerPC Assembly Printer"; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 59 | PowerPCTargetMachine &getTM() { |
| 60 | return static_cast<PowerPCTargetMachine&>(TM); |
Chris Lattner | a384079 | 2004-08-16 23:25:21 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Nate Begeman | e59bf59 | 2004-08-14 22:09:10 +0000 | [diff] [blame] | 63 | /// printInstruction - This method is automatically generated by tablegen |
| 64 | /// from the instruction set description. This method returns true if the |
| 65 | /// machine instruction was sufficiently described to print it, otherwise it |
| 66 | /// returns false. |
| 67 | bool printInstruction(const MachineInstr *MI); |
| 68 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 69 | void printMachineInstruction(const MachineInstr *MI); |
Nate Begeman | b73a711 | 2004-08-13 09:32:01 +0000 | [diff] [blame] | 70 | void printOp(const MachineOperand &MO, bool LoadAddrOp = false); |
Chris Lattner | 7bb424f | 2004-08-14 23:27:29 +0000 | [diff] [blame] | 71 | |
| 72 | void printOperand(const MachineInstr *MI, unsigned OpNo, MVT::ValueType VT){ |
| 73 | const MachineOperand &MO = MI->getOperand(OpNo); |
| 74 | if (MO.getType() == MachineOperand::MO_MachineRegister) { |
| 75 | assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??"); |
| 76 | O << LowercaseString(TM.getRegisterInfo()->get(MO.getReg()).Name); |
Chris Lattner | 0ea3171 | 2004-08-15 05:46:14 +0000 | [diff] [blame] | 77 | } else if (MO.isImmediate()) { |
| 78 | O << MO.getImmedValue(); |
Chris Lattner | 7bb424f | 2004-08-14 23:27:29 +0000 | [diff] [blame] | 79 | } else { |
| 80 | printOp(MO); |
| 81 | } |
| 82 | } |
| 83 | |
Nate Begeman | c330612 | 2004-08-21 05:56:39 +0000 | [diff] [blame] | 84 | void printU5ImmOperand(const MachineInstr *MI, unsigned OpNo, |
| 85 | MVT::ValueType VT) { |
| 86 | unsigned char value = MI->getOperand(OpNo).getImmedValue(); |
Chris Lattner | 12585ba | 2004-08-21 19:11:03 +0000 | [diff] [blame] | 87 | assert(value <= 31 && "Invalid u5imm argument!"); |
Nate Begeman | c330612 | 2004-08-21 05:56:39 +0000 | [diff] [blame] | 88 | O << (unsigned int)value; |
| 89 | } |
Nate Begeman | 07aada8 | 2004-08-30 02:28:06 +0000 | [diff] [blame] | 90 | void printU6ImmOperand(const MachineInstr *MI, unsigned OpNo, |
| 91 | MVT::ValueType VT) { |
| 92 | unsigned char value = MI->getOperand(OpNo).getImmedValue(); |
| 93 | assert(value <= 63 && "Invalid u6imm argument!"); |
| 94 | O << (unsigned int)value; |
| 95 | } |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 96 | void printS16ImmOperand(const MachineInstr *MI, unsigned OpNo, |
| 97 | MVT::ValueType VT) { |
| 98 | O << (short)MI->getOperand(OpNo).getImmedValue(); |
| 99 | } |
Chris Lattner | 97b2a2e | 2004-08-15 05:20:16 +0000 | [diff] [blame] | 100 | void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo, |
| 101 | MVT::ValueType VT) { |
| 102 | O << (unsigned short)MI->getOperand(OpNo).getImmedValue(); |
| 103 | } |
Nate Begeman | b7a8f2c | 2004-09-02 08:13:00 +0000 | [diff] [blame] | 104 | void printBranchOperand(const MachineInstr *MI, unsigned OpNo, |
Misha Brukman | 3a060e5 | 2004-10-23 04:58:32 +0000 | [diff] [blame] | 105 | MVT::ValueType VT) { |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 106 | // Branches can take an immediate operand. This is used by the branch |
| 107 | // selection pass to print $+8, an eight byte displacement from the PC. |
| 108 | if (MI->getOperand(OpNo).isImmediate()) { |
| 109 | O << "$+" << MI->getOperand(OpNo).getImmedValue() << '\n'; |
| 110 | } else { |
| 111 | printOp(MI->getOperand(OpNo)); |
| 112 | } |
Nate Begeman | b7a8f2c | 2004-09-02 08:13:00 +0000 | [diff] [blame] | 113 | } |
| 114 | void printPICLabel(const MachineInstr *MI, unsigned OpNo, |
Misha Brukman | 3a060e5 | 2004-10-23 04:58:32 +0000 | [diff] [blame] | 115 | MVT::ValueType VT) { |
Nate Begeman | b7a8f2c | 2004-09-02 08:13:00 +0000 | [diff] [blame] | 116 | // FIXME: should probably be converted to cout.width and cout.fill |
| 117 | O << "\"L0000" << LabelNumber << "$pb\"\n"; |
| 118 | O << "\"L0000" << LabelNumber << "$pb\":"; |
| 119 | } |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 120 | void printSymbolHi(const MachineInstr *MI, unsigned OpNo, |
Misha Brukman | 3a060e5 | 2004-10-23 04:58:32 +0000 | [diff] [blame] | 121 | MVT::ValueType VT) { |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 122 | O << "ha16("; |
| 123 | printOp(MI->getOperand(OpNo), true /* LoadAddrOp */); |
| 124 | O << "-\"L0000" << LabelNumber << "$pb\")"; |
| 125 | } |
| 126 | void printSymbolLo(const MachineInstr *MI, unsigned OpNo, |
Misha Brukman | 3a060e5 | 2004-10-23 04:58:32 +0000 | [diff] [blame] | 127 | MVT::ValueType VT) { |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 128 | // FIXME: Because LFS, LFD, and LWZ can be used either with a s16imm or |
| 129 | // a lo16 of a global or constant pool operand, we must handle both here. |
| 130 | // this isn't a great design, but it works for now. |
| 131 | if (MI->getOperand(OpNo).isImmediate()) { |
| 132 | O << (short)MI->getOperand(OpNo).getImmedValue(); |
| 133 | } else { |
| 134 | O << "lo16("; |
| 135 | printOp(MI->getOperand(OpNo), true /* LoadAddrOp */); |
| 136 | O << "-\"L0000" << LabelNumber << "$pb\")"; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | virtual void printConstantPool(MachineConstantPool *MCP) = 0; |
| 141 | virtual bool runOnMachineFunction(MachineFunction &F) = 0; |
| 142 | virtual bool doFinalization(Module &M) = 0; |
| 143 | }; |
| 144 | |
Misha Brukman | 3e0b51a | 2004-09-05 02:42:44 +0000 | [diff] [blame] | 145 | /// DarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac OS |
| 146 | /// X |
| 147 | /// |
| 148 | struct DarwinAsmPrinter : public PowerPCAsmPrinter { |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 149 | |
| 150 | DarwinAsmPrinter(std::ostream &O, TargetMachine &TM) |
Misha Brukman | 3e0b51a | 2004-09-05 02:42:44 +0000 | [diff] [blame] | 151 | : PowerPCAsmPrinter(O, TM) { |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 152 | CommentString = ";"; |
| 153 | GlobalPrefix = "_"; |
| 154 | ZeroDirective = "\t.space\t"; // ".space N" emits N zeros. |
| 155 | Data64bitsDirective = 0; // we can't emit a 64-bit unit |
| 156 | AlignmentIsInBytes = false; // Alignment is by power of 2. |
| 157 | } |
| 158 | |
| 159 | virtual const char *getPassName() const { |
| 160 | return "Darwin PPC Assembly Printer"; |
| 161 | } |
Chris Lattner | 97b2a2e | 2004-08-15 05:20:16 +0000 | [diff] [blame] | 162 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 163 | void printConstantPool(MachineConstantPool *MCP); |
| 164 | bool runOnMachineFunction(MachineFunction &F); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 165 | bool doFinalization(Module &M); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 166 | }; |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 167 | |
Misha Brukman | 3e0b51a | 2004-09-05 02:42:44 +0000 | [diff] [blame] | 168 | /// AIXAsmPrinter - PowerPC assembly printer, customized for AIX |
| 169 | /// |
| 170 | struct AIXAsmPrinter : public PowerPCAsmPrinter { |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 171 | /// Map for labels corresponding to global variables |
| 172 | /// |
| 173 | std::map<const GlobalVariable*,std::string> GVToLabelMap; |
| 174 | |
| 175 | AIXAsmPrinter(std::ostream &O, TargetMachine &TM) |
Misha Brukman | 3e0b51a | 2004-09-05 02:42:44 +0000 | [diff] [blame] | 176 | : PowerPCAsmPrinter(O, TM) { |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 177 | CommentString = "#"; |
| 178 | GlobalPrefix = "_"; |
| 179 | ZeroDirective = "\t.space\t"; // ".space N" emits N zeros. |
| 180 | Data64bitsDirective = 0; // we can't emit a 64-bit unit |
| 181 | AlignmentIsInBytes = false; // Alignment is by power of 2. |
| 182 | } |
| 183 | |
| 184 | virtual const char *getPassName() const { |
| 185 | return "AIX PPC Assembly Printer"; |
| 186 | } |
| 187 | |
| 188 | void printConstantPool(MachineConstantPool *MCP); |
| 189 | bool runOnMachineFunction(MachineFunction &F); |
| 190 | bool doInitialization(Module &M); |
| 191 | bool doFinalization(Module &M); |
| 192 | }; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 193 | } // end of anonymous namespace |
| 194 | |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 195 | // SwitchSection - Switch to the specified section of the executable if we are |
| 196 | // not already in it! |
| 197 | // |
| 198 | static void SwitchSection(std::ostream &OS, std::string &CurSection, |
| 199 | const char *NewSection) { |
| 200 | if (CurSection != NewSection) { |
| 201 | CurSection = NewSection; |
| 202 | if (!CurSection.empty()) |
| 203 | OS << "\t" << NewSection << "\n"; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | /// isStringCompatible - Can we treat the specified array as a string? |
| 208 | /// Only if it is an array of ubytes or non-negative sbytes. |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 209 | /// |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 210 | static bool isStringCompatible(const ConstantArray *CVA) { |
| 211 | const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType(); |
| 212 | if (ETy == Type::UByteTy) return true; |
| 213 | if (ETy != Type::SByteTy) return false; |
| 214 | |
| 215 | for (unsigned i = 0; i < CVA->getNumOperands(); ++i) |
| 216 | if (cast<ConstantSInt>(CVA->getOperand(i))->getValue() < 0) |
| 217 | return false; |
| 218 | |
| 219 | return true; |
| 220 | } |
| 221 | |
| 222 | /// toOctal - Convert the low order bits of X into an octal digit. |
| 223 | /// |
| 224 | static inline char toOctal(int X) { |
| 225 | return (X&7)+'0'; |
| 226 | } |
| 227 | |
| 228 | // Possible states while outputting ASCII strings |
| 229 | namespace { |
| 230 | enum StringSection { |
| 231 | None, |
| 232 | Alpha, |
| 233 | Numeric |
| 234 | }; |
| 235 | } |
| 236 | |
| 237 | /// SwitchStringSection - manage the changes required to output bytes as |
| 238 | /// characters in a string vs. numeric decimal values |
| 239 | /// |
| 240 | static inline void SwitchStringSection(std::ostream &O, StringSection NewSect, |
| 241 | StringSection &Current) { |
| 242 | if (Current == None) { |
| 243 | if (NewSect == Alpha) |
| 244 | O << "\t.byte \""; |
| 245 | else if (NewSect == Numeric) |
| 246 | O << "\t.byte "; |
| 247 | } else if (Current == Alpha) { |
| 248 | if (NewSect == None) |
| 249 | O << "\""; |
| 250 | else if (NewSect == Numeric) |
| 251 | O << "\"\n" |
| 252 | << "\t.byte "; |
| 253 | } else if (Current == Numeric) { |
| 254 | if (NewSect == Alpha) |
| 255 | O << '\n' |
| 256 | << "\t.byte \""; |
| 257 | else if (NewSect == Numeric) |
| 258 | O << ", "; |
| 259 | } |
| 260 | |
| 261 | Current = NewSect; |
| 262 | } |
| 263 | |
| 264 | /// getAsCString - Return the specified array as a C compatible |
| 265 | /// string, only if the predicate isStringCompatible is true. |
| 266 | /// |
| 267 | static void printAsCString(std::ostream &O, const ConstantArray *CVA) { |
| 268 | assert(isStringCompatible(CVA) && "Array is not string compatible!"); |
| 269 | |
| 270 | if (CVA->getNumOperands() == 0) |
| 271 | return; |
| 272 | |
| 273 | StringSection Current = None; |
| 274 | for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i) { |
| 275 | unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue(); |
| 276 | if (C == '"') { |
| 277 | SwitchStringSection(O, Alpha, Current); |
| 278 | O << "\"\""; |
| 279 | } else if (isprint(C)) { |
| 280 | SwitchStringSection(O, Alpha, Current); |
| 281 | O << C; |
| 282 | } else { |
| 283 | SwitchStringSection(O, Numeric, Current); |
| 284 | O << utostr((unsigned)C); |
| 285 | } |
| 286 | } |
| 287 | SwitchStringSection(O, None, Current); |
| 288 | O << '\n'; |
| 289 | } |
| 290 | |
| 291 | /// createDarwinAsmPrinterPass - Returns a pass that prints the PPC assembly |
| 292 | /// code for a MachineFunction to the given output stream, in a format that the |
| 293 | /// Darwin assembler can deal with. |
| 294 | /// |
| 295 | FunctionPass *llvm::createDarwinAsmPrinter(std::ostream &o, TargetMachine &tm) { |
| 296 | return new DarwinAsmPrinter(o, tm); |
| 297 | } |
| 298 | |
| 299 | /// createAIXAsmPrinterPass - Returns a pass that prints the PPC assembly code |
| 300 | /// for a MachineFunction to the given output stream, in a format that the |
| 301 | /// AIX 5L assembler can deal with. |
| 302 | /// |
| 303 | FunctionPass *llvm::createAIXAsmPrinter(std::ostream &o, TargetMachine &tm) { |
| 304 | return new AIXAsmPrinter(o, tm); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Nate Begeman | e59bf59 | 2004-08-14 22:09:10 +0000 | [diff] [blame] | 307 | // Include the auto-generated portion of the assembly writer |
| 308 | #include "PowerPCGenAsmWriter.inc" |
| 309 | |
Misha Brukman | 3e0b51a | 2004-09-05 02:42:44 +0000 | [diff] [blame] | 310 | void PowerPCAsmPrinter::printOp(const MachineOperand &MO, |
Misha Brukman | f2ccb77 | 2004-08-17 04:55:41 +0000 | [diff] [blame] | 311 | bool LoadAddrOp /* = false */) { |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 312 | const MRegisterInfo &RI = *TM.getRegisterInfo(); |
| 313 | int new_symbol; |
| 314 | |
| 315 | switch (MO.getType()) { |
| 316 | case MachineOperand::MO_VirtualRegister: |
| 317 | if (Value *V = MO.getVRegValueOrNull()) { |
| 318 | O << "<" << V->getName() << ">"; |
| 319 | return; |
| 320 | } |
| 321 | // FALLTHROUGH |
| 322 | case MachineOperand::MO_MachineRegister: |
Misha Brukman | 05fcd0c | 2004-07-08 17:58:04 +0000 | [diff] [blame] | 323 | case MachineOperand::MO_CCRegister: |
Misha Brukman | 7f484a5 | 2004-06-24 23:51:00 +0000 | [diff] [blame] | 324 | O << LowercaseString(RI.get(MO.getReg()).Name); |
| 325 | return; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 326 | |
| 327 | case MachineOperand::MO_SignExtendedImmed: |
Misha Brukman | af313fb | 2004-07-28 00:00:48 +0000 | [diff] [blame] | 328 | case MachineOperand::MO_UnextendedImmed: |
| 329 | std::cerr << "printOp() does not handle immediate values\n"; |
| 330 | abort(); |
Misha Brukman | 97a296f | 2004-07-21 20:11:11 +0000 | [diff] [blame] | 331 | return; |
| 332 | |
Misha Brukman | 05fcd0c | 2004-07-08 17:58:04 +0000 | [diff] [blame] | 333 | case MachineOperand::MO_PCRelativeDisp: |
| 334 | std::cerr << "Shouldn't use addPCDisp() when building PPC MachineInstrs"; |
| 335 | abort(); |
| 336 | return; |
| 337 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 338 | case MachineOperand::MO_MachineBasicBlock: { |
| 339 | MachineBasicBlock *MBBOp = MO.getMachineBasicBlock(); |
| 340 | O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction()) |
Misha Brukman | 218bec7 | 2004-06-29 17:13:26 +0000 | [diff] [blame] | 341 | << "_" << MBBOp->getNumber() << "\t; " |
Misha Brukman | 2bf183c | 2004-06-25 15:42:10 +0000 | [diff] [blame] | 342 | << MBBOp->getBasicBlock()->getName(); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 343 | return; |
| 344 | } |
Misha Brukman | 05fcd0c | 2004-07-08 17:58:04 +0000 | [diff] [blame] | 345 | |
| 346 | case MachineOperand::MO_ConstantPoolIndex: |
| 347 | O << ".CPI" << CurrentFnName << "_" << MO.getConstantPoolIndex(); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 348 | return; |
Misha Brukman | 05fcd0c | 2004-07-08 17:58:04 +0000 | [diff] [blame] | 349 | |
| 350 | case MachineOperand::MO_ExternalSymbol: |
| 351 | O << MO.getSymbolName(); |
| 352 | return; |
| 353 | |
Nate Begeman | b73a711 | 2004-08-13 09:32:01 +0000 | [diff] [blame] | 354 | case MachineOperand::MO_GlobalAddress: { |
| 355 | GlobalValue *GV = MO.getGlobal(); |
| 356 | std::string Name = Mang->getValueName(GV); |
Misha Brukman | e2eceb5 | 2004-07-23 16:08:20 +0000 | [diff] [blame] | 357 | |
Nate Begeman | b73a711 | 2004-08-13 09:32:01 +0000 | [diff] [blame] | 358 | // Dynamically-resolved functions need a stub for the function. Be |
| 359 | // wary however not to output $stub for external functions whose addresses |
| 360 | // are taken. Those should be emitted as $non_lazy_ptr below. |
| 361 | Function *F = dyn_cast<Function>(GV); |
| 362 | if (F && F->isExternal() && !LoadAddrOp && |
Chris Lattner | a384079 | 2004-08-16 23:25:21 +0000 | [diff] [blame] | 363 | getTM().CalledFunctions.count(F)) { |
Nate Begeman | b73a711 | 2004-08-13 09:32:01 +0000 | [diff] [blame] | 364 | FnStubs.insert(Name); |
| 365 | O << "L" << Name << "$stub"; |
| 366 | return; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 367 | } |
Nate Begeman | e59bf59 | 2004-08-14 22:09:10 +0000 | [diff] [blame] | 368 | |
Nate Begeman | fcf4a42 | 2004-10-17 23:01:34 +0000 | [diff] [blame] | 369 | // External or weakly linked global variables need non-lazily-resolved stubs |
Nate Begeman | a591457 | 2004-11-09 04:01:18 +0000 | [diff] [blame] | 370 | if ((GV->isExternal() || GV->hasWeakLinkage() || GV->hasLinkOnceLinkage()) |
| 371 | && getTM().AddressTaken.count(GV)) { |
Nate Begeman | b73a711 | 2004-08-13 09:32:01 +0000 | [diff] [blame] | 372 | GVStubs.insert(Name); |
| 373 | O << "L" << Name << "$non_lazy_ptr"; |
| 374 | return; |
| 375 | } |
Nate Begeman | e59bf59 | 2004-08-14 22:09:10 +0000 | [diff] [blame] | 376 | |
Chris Lattner | a384079 | 2004-08-16 23:25:21 +0000 | [diff] [blame] | 377 | if (F && LoadAddrOp && getTM().AddressTaken.count(GV)) { |
Nate Begeman | e59bf59 | 2004-08-14 22:09:10 +0000 | [diff] [blame] | 378 | LinkOnceStubs.insert(Name); |
| 379 | O << "L" << Name << "$non_lazy_ptr"; |
| 380 | return; |
| 381 | } |
Nate Begeman | b73a711 | 2004-08-13 09:32:01 +0000 | [diff] [blame] | 382 | |
| 383 | O << Mang->getValueName(GV); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 384 | return; |
Nate Begeman | b73a711 | 2004-08-13 09:32:01 +0000 | [diff] [blame] | 385 | } |
Misha Brukman | 05fcd0c | 2004-07-08 17:58:04 +0000 | [diff] [blame] | 386 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 387 | default: |
Misha Brukman | 05fcd0c | 2004-07-08 17:58:04 +0000 | [diff] [blame] | 388 | O << "<unknown operand type: " << MO.getType() << ">"; |
Misha Brukman | 22e1207 | 2004-06-25 15:11:34 +0000 | [diff] [blame] | 389 | return; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 390 | } |
| 391 | } |
| 392 | |
Nate Begeman | e59bf59 | 2004-08-14 22:09:10 +0000 | [diff] [blame] | 393 | /// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to |
| 394 | /// the current output stream. |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 395 | /// |
Misha Brukman | 3e0b51a | 2004-09-05 02:42:44 +0000 | [diff] [blame] | 396 | void PowerPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) { |
Nate Begeman | e59bf59 | 2004-08-14 22:09:10 +0000 | [diff] [blame] | 397 | ++EmittedInsts; |
| 398 | if (printInstruction(MI)) |
| 399 | return; // Printer was automatically generated |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 400 | |
| 401 | assert(0 && "Unhandled instruction in asm writer!"); |
| 402 | abort(); |
Nate Begeman | e59bf59 | 2004-08-14 22:09:10 +0000 | [diff] [blame] | 403 | return; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 404 | } |
| 405 | |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 406 | /// runOnMachineFunction - This uses the printMachineInstruction() |
| 407 | /// method to print assembly for each instruction. |
| 408 | /// |
| 409 | bool DarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) { |
| 410 | setupMachineFunction(MF); |
| 411 | O << "\n\n"; |
| 412 | |
| 413 | // Print out constants referenced by the function |
| 414 | printConstantPool(MF.getConstantPool()); |
| 415 | |
| 416 | // Print out labels for the function. |
| 417 | O << "\t.text\n"; |
| 418 | emitAlignment(2); |
| 419 | O << "\t.globl\t" << CurrentFnName << "\n"; |
| 420 | O << CurrentFnName << ":\n"; |
| 421 | |
| 422 | // Print out code for the function. |
| 423 | for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); |
| 424 | I != E; ++I) { |
| 425 | // Print a label for the basic block. |
| 426 | O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t" |
| 427 | << CommentString << " " << I->getBasicBlock()->getName() << "\n"; |
| 428 | for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end(); |
| 429 | II != E; ++II) { |
| 430 | // Print the assembly for the instruction. |
| 431 | O << "\t"; |
| 432 | printMachineInstruction(II); |
| 433 | } |
| 434 | } |
| 435 | ++LabelNumber; |
| 436 | |
| 437 | // We didn't modify anything. |
| 438 | return false; |
| 439 | } |
| 440 | |
| 441 | /// printConstantPool - Print to the current output stream assembly |
| 442 | /// representations of the constants in the constant pool MCP. This is |
| 443 | /// used to print out constants which have been "spilled to memory" by |
| 444 | /// the code generator. |
| 445 | /// |
| 446 | void DarwinAsmPrinter::printConstantPool(MachineConstantPool *MCP) { |
| 447 | const std::vector<Constant*> &CP = MCP->getConstants(); |
| 448 | const TargetData &TD = TM.getTargetData(); |
| 449 | |
| 450 | if (CP.empty()) return; |
| 451 | |
| 452 | for (unsigned i = 0, e = CP.size(); i != e; ++i) { |
| 453 | O << "\t.const\n"; |
| 454 | emitAlignment(TD.getTypeAlignmentShift(CP[i]->getType())); |
| 455 | O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t" << CommentString |
| 456 | << *CP[i] << "\n"; |
| 457 | emitGlobalConstant(CP[i]); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 458 | } |
| 459 | } |
| 460 | |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 461 | bool DarwinAsmPrinter::doFinalization(Module &M) { |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 462 | const TargetData &TD = TM.getTargetData(); |
| 463 | std::string CurSection; |
| 464 | |
| 465 | // Print out module-level global variables here. |
| 466 | for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I) |
| 467 | if (I->hasInitializer()) { // External global require no code |
Nate Begeman | a2de102 | 2004-09-22 04:40:25 +0000 | [diff] [blame] | 468 | O << '\n'; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 469 | std::string name = Mang->getValueName(I); |
| 470 | Constant *C = I->getInitializer(); |
| 471 | unsigned Size = TD.getTypeSize(C->getType()); |
Chris Lattner | 69d485e | 2004-08-17 19:26:03 +0000 | [diff] [blame] | 472 | unsigned Align = TD.getTypeAlignmentShift(C->getType()); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 473 | |
Misha Brukman | 97a296f | 2004-07-21 20:11:11 +0000 | [diff] [blame] | 474 | if (C->isNullValue() && /* FIXME: Verify correct */ |
Nate Begeman | a591457 | 2004-11-09 04:01:18 +0000 | [diff] [blame] | 475 | (I->hasInternalLinkage() || I->hasWeakLinkage() || |
| 476 | I->hasLinkOnceLinkage())) { |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 477 | SwitchSection(O, CurSection, ".data"); |
| 478 | if (I->hasInternalLinkage()) |
Misha Brukman | e2eceb5 | 2004-07-23 16:08:20 +0000 | [diff] [blame] | 479 | O << ".lcomm " << name << "," << TD.getTypeSize(C->getType()) |
Chris Lattner | 69d485e | 2004-08-17 19:26:03 +0000 | [diff] [blame] | 480 | << "," << Align; |
Misha Brukman | 218bec7 | 2004-06-29 17:13:26 +0000 | [diff] [blame] | 481 | else |
Misha Brukman | e2eceb5 | 2004-07-23 16:08:20 +0000 | [diff] [blame] | 482 | O << ".comm " << name << "," << TD.getTypeSize(C->getType()); |
Misha Brukman | 218bec7 | 2004-06-29 17:13:26 +0000 | [diff] [blame] | 483 | O << "\t\t; "; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 484 | WriteAsOperand(O, I, true, true, &M); |
Nate Begeman | a2de102 | 2004-09-22 04:40:25 +0000 | [diff] [blame] | 485 | O << '\n'; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 486 | } else { |
| 487 | switch (I->getLinkage()) { |
| 488 | case GlobalValue::LinkOnceLinkage: |
Misha Brukman | 97a296f | 2004-07-21 20:11:11 +0000 | [diff] [blame] | 489 | O << ".section __TEXT,__textcoal_nt,coalesced,no_toc\n" |
| 490 | << ".weak_definition " << name << '\n' |
| 491 | << ".private_extern " << name << '\n' |
| 492 | << ".section __DATA,__datacoal_nt,coalesced,no_toc\n"; |
| 493 | LinkOnceStubs.insert(name); |
| 494 | break; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 495 | case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak. |
| 496 | // Nonnull linkonce -> weak |
| 497 | O << "\t.weak " << name << "\n"; |
| 498 | SwitchSection(O, CurSection, ""); |
| 499 | O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n"; |
| 500 | break; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 501 | case GlobalValue::AppendingLinkage: |
| 502 | // FIXME: appending linkage variables should go into a section of |
| 503 | // their name or something. For now, just emit them as external. |
| 504 | case GlobalValue::ExternalLinkage: |
| 505 | // If external or appending, declare as a global symbol |
| 506 | O << "\t.globl " << name << "\n"; |
| 507 | // FALL THROUGH |
| 508 | case GlobalValue::InternalLinkage: |
Misha Brukman | 61297ee | 2004-06-29 23:40:57 +0000 | [diff] [blame] | 509 | SwitchSection(O, CurSection, ".data"); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 510 | break; |
Misha Brukman | 3a8a42a | 2004-11-14 21:03:30 +0000 | [diff] [blame^] | 511 | case GlobalValue::GhostLinkage: |
| 512 | std::cerr << "Error: unmaterialized (GhostLinkage) function in asm!"; |
| 513 | abort(); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 514 | } |
| 515 | |
Chris Lattner | 69d485e | 2004-08-17 19:26:03 +0000 | [diff] [blame] | 516 | emitAlignment(Align); |
Misha Brukman | 218bec7 | 2004-06-29 17:13:26 +0000 | [diff] [blame] | 517 | O << name << ":\t\t\t\t; "; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 518 | WriteAsOperand(O, I, true, true, &M); |
| 519 | O << " = "; |
| 520 | WriteAsOperand(O, C, false, false, &M); |
| 521 | O << "\n"; |
| 522 | emitGlobalConstant(C); |
| 523 | } |
| 524 | } |
Misha Brukman | da2b13f | 2004-07-16 20:29:04 +0000 | [diff] [blame] | 525 | |
| 526 | // Output stubs for dynamically-linked functions |
| 527 | for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end(); |
| 528 | i != e; ++i) |
Misha Brukman | 46fd00a | 2004-06-24 23:04:11 +0000 | [diff] [blame] | 529 | { |
Misha Brukman | e2eceb5 | 2004-07-23 16:08:20 +0000 | [diff] [blame] | 530 | O << ".data\n"; |
| 531 | O << ".section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32\n"; |
Chris Lattner | 69d485e | 2004-08-17 19:26:03 +0000 | [diff] [blame] | 532 | emitAlignment(2); |
Misha Brukman | 46fd00a | 2004-06-24 23:04:11 +0000 | [diff] [blame] | 533 | O << "L" << *i << "$stub:\n"; |
| 534 | O << "\t.indirect_symbol " << *i << "\n"; |
| 535 | O << "\tmflr r0\n"; |
Misha Brukman | e2eceb5 | 2004-07-23 16:08:20 +0000 | [diff] [blame] | 536 | O << "\tbcl 20,31,L0$" << *i << "\n"; |
Misha Brukman | 46fd00a | 2004-06-24 23:04:11 +0000 | [diff] [blame] | 537 | O << "L0$" << *i << ":\n"; |
| 538 | O << "\tmflr r11\n"; |
| 539 | O << "\taddis r11,r11,ha16(L" << *i << "$lazy_ptr-L0$" << *i << ")\n"; |
| 540 | O << "\tmtlr r0\n"; |
Misha Brukman | e2eceb5 | 2004-07-23 16:08:20 +0000 | [diff] [blame] | 541 | O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr-L0$" << *i << ")(r11)\n"; |
Misha Brukman | 46fd00a | 2004-06-24 23:04:11 +0000 | [diff] [blame] | 542 | O << "\tmtctr r12\n"; |
| 543 | O << "\tbctr\n"; |
| 544 | O << ".data\n"; |
| 545 | O << ".lazy_symbol_pointer\n"; |
| 546 | O << "L" << *i << "$lazy_ptr:\n"; |
Misha Brukman | e2eceb5 | 2004-07-23 16:08:20 +0000 | [diff] [blame] | 547 | O << "\t.indirect_symbol " << *i << "\n"; |
| 548 | O << "\t.long dyld_stub_binding_helper\n"; |
Misha Brukman | 46fd00a | 2004-06-24 23:04:11 +0000 | [diff] [blame] | 549 | } |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 550 | |
Misha Brukman | da2b13f | 2004-07-16 20:29:04 +0000 | [diff] [blame] | 551 | O << "\n"; |
| 552 | |
| 553 | // Output stubs for external global variables |
| 554 | if (GVStubs.begin() != GVStubs.end()) |
Misha Brukman | e2eceb5 | 2004-07-23 16:08:20 +0000 | [diff] [blame] | 555 | O << ".data\n.non_lazy_symbol_pointer\n"; |
Misha Brukman | da2b13f | 2004-07-16 20:29:04 +0000 | [diff] [blame] | 556 | for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end(); |
| 557 | i != e; ++i) { |
| 558 | O << "L" << *i << "$non_lazy_ptr:\n"; |
| 559 | O << "\t.indirect_symbol " << *i << "\n"; |
| 560 | O << "\t.long\t0\n"; |
| 561 | } |
| 562 | |
Nate Begeman | e59bf59 | 2004-08-14 22:09:10 +0000 | [diff] [blame] | 563 | // Output stubs for link-once variables |
| 564 | if (LinkOnceStubs.begin() != LinkOnceStubs.end()) |
| 565 | O << ".data\n.align 2\n"; |
| 566 | for (std::set<std::string>::iterator i = LinkOnceStubs.begin(), |
| 567 | e = LinkOnceStubs.end(); i != e; ++i) { |
| 568 | O << "L" << *i << "$non_lazy_ptr:\n" |
| 569 | << "\t.long\t" << *i << '\n'; |
| 570 | } |
| 571 | |
Chris Lattner | a384079 | 2004-08-16 23:25:21 +0000 | [diff] [blame] | 572 | AsmPrinter::doFinalization(M); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 573 | return false; // success |
| 574 | } |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 575 | |
| 576 | /// runOnMachineFunction - This uses the printMachineInstruction() |
| 577 | /// method to print assembly for each instruction. |
| 578 | /// |
| 579 | bool AIXAsmPrinter::runOnMachineFunction(MachineFunction &MF) { |
| 580 | CurrentFnName = MF.getFunction()->getName(); |
| 581 | |
| 582 | // Print out constants referenced by the function |
| 583 | printConstantPool(MF.getConstantPool()); |
| 584 | |
| 585 | // Print out header for the function. |
| 586 | O << "\t.csect .text[PR]\n" |
| 587 | << "\t.align 2\n" |
| 588 | << "\t.globl " << CurrentFnName << '\n' |
| 589 | << "\t.globl ." << CurrentFnName << '\n' |
| 590 | << "\t.csect " << CurrentFnName << "[DS],3\n" |
| 591 | << CurrentFnName << ":\n" |
| 592 | << "\t.llong ." << CurrentFnName << ", TOC[tc0], 0\n" |
| 593 | << "\t.csect .text[PR]\n" |
| 594 | << '.' << CurrentFnName << ":\n"; |
| 595 | |
| 596 | // Print out code for the function. |
| 597 | for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); |
| 598 | I != E; ++I) { |
| 599 | // Print a label for the basic block. |
| 600 | O << "LBB" << CurrentFnName << "_" << I->getNumber() << ":\t# " |
| 601 | << I->getBasicBlock()->getName() << "\n"; |
| 602 | for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end(); |
| 603 | II != E; ++II) { |
| 604 | // Print the assembly for the instruction. |
| 605 | O << "\t"; |
| 606 | printMachineInstruction(II); |
| 607 | } |
| 608 | } |
| 609 | ++LabelNumber; |
| 610 | |
| 611 | O << "LT.." << CurrentFnName << ":\n" |
| 612 | << "\t.long 0\n" |
| 613 | << "\t.byte 0,0,32,65,128,0,0,0\n" |
| 614 | << "\t.long LT.." << CurrentFnName << "-." << CurrentFnName << '\n' |
| 615 | << "\t.short 3\n" |
| 616 | << "\t.byte \"" << CurrentFnName << "\"\n" |
| 617 | << "\t.align 2\n"; |
| 618 | |
| 619 | // We didn't modify anything. |
| 620 | return false; |
| 621 | } |
| 622 | |
| 623 | /// printConstantPool - Print to the current output stream assembly |
| 624 | /// representations of the constants in the constant pool MCP. This is |
| 625 | /// used to print out constants which have been "spilled to memory" by |
| 626 | /// the code generator. |
| 627 | /// |
| 628 | void AIXAsmPrinter::printConstantPool(MachineConstantPool *MCP) { |
| 629 | const std::vector<Constant*> &CP = MCP->getConstants(); |
| 630 | const TargetData &TD = TM.getTargetData(); |
| 631 | |
| 632 | if (CP.empty()) return; |
| 633 | |
| 634 | for (unsigned i = 0, e = CP.size(); i != e; ++i) { |
| 635 | O << "\t.const\n"; |
| 636 | O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType()) |
| 637 | << "\n"; |
| 638 | O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t;" |
| 639 | << *CP[i] << "\n"; |
| 640 | emitGlobalConstant(CP[i]); |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | bool AIXAsmPrinter::doInitialization(Module &M) { |
| 645 | const TargetData &TD = TM.getTargetData(); |
| 646 | std::string CurSection; |
| 647 | |
| 648 | O << "\t.machine \"ppc64\"\n" |
| 649 | << "\t.toc\n" |
| 650 | << "\t.csect .text[PR]\n"; |
| 651 | |
| 652 | // Print out module-level global variables |
| 653 | for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I) { |
| 654 | if (!I->hasInitializer()) |
| 655 | continue; |
| 656 | |
| 657 | std::string Name = I->getName(); |
| 658 | Constant *C = I->getInitializer(); |
| 659 | // N.B.: We are defaulting to writable strings |
| 660 | if (I->hasExternalLinkage()) { |
| 661 | O << "\t.globl " << Name << '\n' |
| 662 | << "\t.csect .data[RW],3\n"; |
| 663 | } else { |
| 664 | O << "\t.csect _global.rw_c[RW],3\n"; |
| 665 | } |
| 666 | O << Name << ":\n"; |
| 667 | emitGlobalConstant(C); |
| 668 | } |
| 669 | |
| 670 | // Output labels for globals |
| 671 | if (M.gbegin() != M.gend()) O << "\t.toc\n"; |
| 672 | for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I) { |
| 673 | const GlobalVariable *GV = I; |
| 674 | // Do not output labels for unused variables |
| 675 | if (GV->isExternal() && GV->use_begin() == GV->use_end()) |
| 676 | continue; |
| 677 | |
| 678 | std::string Name = GV->getName(); |
| 679 | std::string Label = "LC.." + utostr(LabelNumber++); |
| 680 | GVToLabelMap[GV] = Label; |
| 681 | O << Label << ":\n" |
| 682 | << "\t.tc " << Name << "[TC]," << Name; |
| 683 | if (GV->isExternal()) O << "[RW]"; |
| 684 | O << '\n'; |
| 685 | } |
| 686 | |
| 687 | Mang = new Mangler(M, "."); |
| 688 | return false; // success |
| 689 | } |
| 690 | |
| 691 | bool AIXAsmPrinter::doFinalization(Module &M) { |
| 692 | const TargetData &TD = TM.getTargetData(); |
| 693 | // Print out module-level global variables |
| 694 | for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I) { |
| 695 | if (I->hasInitializer() || I->hasExternalLinkage()) |
| 696 | continue; |
| 697 | |
| 698 | std::string Name = I->getName(); |
| 699 | if (I->hasInternalLinkage()) { |
| 700 | O << "\t.lcomm " << Name << ",16,_global.bss_c"; |
| 701 | } else { |
| 702 | O << "\t.comm " << Name << "," << TD.getTypeSize(I->getType()) |
| 703 | << "," << log2((unsigned)TD.getTypeAlignment(I->getType())); |
| 704 | } |
| 705 | O << "\t\t# "; |
| 706 | WriteAsOperand(O, I, true, true, &M); |
| 707 | O << "\n"; |
| 708 | } |
| 709 | |
| 710 | O << "_section_.text:\n" |
| 711 | << "\t.csect .data[RW],3\n" |
| 712 | << "\t.llong _section_.text\n"; |
| 713 | |
| 714 | delete Mang; |
| 715 | return false; // success |
| 716 | } |