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