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