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