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; |
Jim Laskey | e719a7c | 2006-01-18 16:54:26 +0000 | [diff] [blame] | 218 | DwarfAbbrevSection = ".section __DWARFA,__debug_abbrev"; |
| 219 | DwarfInfoSection = ".section __DWARFA,__debug_info"; |
| 220 | DwarfLineSection = ".section __DWARFA,__debug_line"; |
| 221 | DwarfFrameSection = |
| 222 | ".section __DWARFA,__debug_frame,,coalesced,no_toc+strip_static_syms"; |
| 223 | DwarfPubNamesSection = ".section __DWARFA,__debug_pubnames"; |
| 224 | DwarfPubTypesSection = ".section __DWARFA,__debug_pubtypes"; |
| 225 | DwarfStrSection = ".section __DWARFA,__debug_str"; |
| 226 | DwarfLocSection = ".section __DWARFA,__debug_loc"; |
| 227 | DwarfARangesSection = ".section __DWARFA,__debug_aranges"; |
| 228 | DwarfRangesSection = ".section __DWARFA,__debug_ranges"; |
| 229 | DwarfMacInfoSection = ".section __DWARFA,__debug_macinfo"; |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 230 | TextSection = ".text"; |
| 231 | DataSection = ".data"; |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 232 | } |
| 233 | }; |
| 234 | |
Misha Brukman | 3e0b51a | 2004-09-05 02:42:44 +0000 | [diff] [blame] | 235 | /// DarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac OS |
| 236 | /// X |
Chris Lattner | 4c7b43b | 2005-10-14 23:37:35 +0000 | [diff] [blame] | 237 | struct DarwinAsmPrinter : public PPCAsmPrinter { |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 238 | |
| 239 | DarwinDwarfWriter DW; |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 240 | |
| 241 | DarwinAsmPrinter(std::ostream &O, TargetMachine &TM) |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 242 | : PPCAsmPrinter(O, TM), DW(O, this) |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 243 | { |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 244 | CommentString = ";"; |
| 245 | GlobalPrefix = "_"; |
Chris Lattner | f55366e | 2005-11-21 06:47:58 +0000 | [diff] [blame] | 246 | PrivateGlobalPrefix = "L"; // Marker for constant pool idxs |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 247 | ZeroDirective = "\t.space\t"; // ".space N" emits N zeros. |
| 248 | Data64bitsDirective = 0; // we can't emit a 64-bit unit |
| 249 | AlignmentIsInBytes = false; // Alignment is by power of 2. |
Chris Lattner | c569e61 | 2005-11-21 08:26:15 +0000 | [diff] [blame] | 250 | ConstantPoolSection = "\t.const\t"; |
Chris Lattner | d717b19 | 2005-12-11 07:45:47 +0000 | [diff] [blame] | 251 | LCOMMDirective = "\t.lcomm\t"; |
Chris Lattner | d1239b7 | 2005-12-13 06:32:50 +0000 | [diff] [blame] | 252 | StaticCtorsSection = ".mod_init_func"; |
| 253 | StaticDtorsSection = ".mod_term_func"; |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | virtual const char *getPassName() const { |
| 257 | return "Darwin PPC Assembly Printer"; |
| 258 | } |
Chris Lattner | 646f7af | 2005-12-09 18:24:29 +0000 | [diff] [blame] | 259 | |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 260 | bool runOnMachineFunction(MachineFunction &F); |
Nate Begeman | 18ed029 | 2005-07-21 01:25:49 +0000 | [diff] [blame] | 261 | bool doInitialization(Module &M); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 262 | bool doFinalization(Module &M); |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 263 | |
| 264 | void getAnalysisUsage(AnalysisUsage &AU) const { |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 265 | AU.setPreservesAll(); |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 266 | AU.addRequired<MachineDebugInfo>(); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 267 | PPCAsmPrinter::getAnalysisUsage(AU); |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 270 | }; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 271 | |
Misha Brukman | 3e0b51a | 2004-09-05 02:42:44 +0000 | [diff] [blame] | 272 | /// AIXAsmPrinter - PowerPC assembly printer, customized for AIX |
| 273 | /// |
Chris Lattner | 4c7b43b | 2005-10-14 23:37:35 +0000 | [diff] [blame] | 274 | struct AIXAsmPrinter : public PPCAsmPrinter { |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 275 | /// Map for labels corresponding to global variables |
| 276 | /// |
| 277 | std::map<const GlobalVariable*,std::string> GVToLabelMap; |
| 278 | |
| 279 | AIXAsmPrinter(std::ostream &O, TargetMachine &TM) |
Chris Lattner | 4c7b43b | 2005-10-14 23:37:35 +0000 | [diff] [blame] | 280 | : PPCAsmPrinter(O, TM) { |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 281 | CommentString = "#"; |
Chris Lattner | 3459bfb | 2005-11-10 18:20:29 +0000 | [diff] [blame] | 282 | GlobalPrefix = "."; |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 283 | ZeroDirective = "\t.space\t"; // ".space N" emits N zeros. |
| 284 | Data64bitsDirective = 0; // we can't emit a 64-bit unit |
| 285 | AlignmentIsInBytes = false; // Alignment is by power of 2. |
Chris Lattner | c569e61 | 2005-11-21 08:26:15 +0000 | [diff] [blame] | 286 | ConstantPoolSection = "\t.const\t"; |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 287 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 288 | |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 289 | virtual const char *getPassName() const { |
| 290 | return "AIX PPC Assembly Printer"; |
| 291 | } |
| 292 | |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 293 | bool runOnMachineFunction(MachineFunction &F); |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 294 | bool doInitialization(Module &M); |
| 295 | bool doFinalization(Module &M); |
| 296 | }; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 297 | } // end of anonymous namespace |
| 298 | |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 299 | /// createDarwinAsmPrinterPass - Returns a pass that prints the PPC assembly |
| 300 | /// 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] | 301 | /// Darwin assembler can deal with. |
| 302 | /// |
| 303 | FunctionPass *llvm::createDarwinAsmPrinter(std::ostream &o, TargetMachine &tm) { |
| 304 | return new DarwinAsmPrinter(o, tm); |
| 305 | } |
| 306 | |
| 307 | /// createAIXAsmPrinterPass - Returns a pass that prints the PPC assembly code |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 308 | /// 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] | 309 | /// AIX 5L assembler can deal with. |
| 310 | /// |
| 311 | FunctionPass *llvm::createAIXAsmPrinter(std::ostream &o, TargetMachine &tm) { |
| 312 | return new AIXAsmPrinter(o, tm); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Nate Begeman | e59bf59 | 2004-08-14 22:09:10 +0000 | [diff] [blame] | 315 | // Include the auto-generated portion of the assembly writer |
Chris Lattner | 4c7b43b | 2005-10-14 23:37:35 +0000 | [diff] [blame] | 316 | #include "PPCGenAsmWriter.inc" |
Nate Begeman | e59bf59 | 2004-08-14 22:09:10 +0000 | [diff] [blame] | 317 | |
Chris Lattner | 9ba13e4 | 2005-11-17 19:25:59 +0000 | [diff] [blame] | 318 | void PPCAsmPrinter::printOp(const MachineOperand &MO) { |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 319 | const MRegisterInfo &RI = *TM.getRegisterInfo(); |
| 320 | int new_symbol; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 321 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 322 | switch (MO.getType()) { |
| 323 | case MachineOperand::MO_VirtualRegister: |
| 324 | if (Value *V = MO.getVRegValueOrNull()) { |
| 325 | O << "<" << V->getName() << ">"; |
| 326 | return; |
| 327 | } |
| 328 | // FALLTHROUGH |
| 329 | case MachineOperand::MO_MachineRegister: |
Misha Brukman | 05fcd0c | 2004-07-08 17:58:04 +0000 | [diff] [blame] | 330 | case MachineOperand::MO_CCRegister: |
Chris Lattner | 9dc4d3c | 2005-08-22 22:00:02 +0000 | [diff] [blame] | 331 | O << RI.get(MO.getReg()).Name; |
Misha Brukman | 7f484a5 | 2004-06-24 23:51:00 +0000 | [diff] [blame] | 332 | return; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 333 | |
| 334 | case MachineOperand::MO_SignExtendedImmed: |
Misha Brukman | af313fb | 2004-07-28 00:00:48 +0000 | [diff] [blame] | 335 | case MachineOperand::MO_UnextendedImmed: |
| 336 | std::cerr << "printOp() does not handle immediate values\n"; |
| 337 | abort(); |
Misha Brukman | 97a296f | 2004-07-21 20:11:11 +0000 | [diff] [blame] | 338 | return; |
| 339 | |
Misha Brukman | 05fcd0c | 2004-07-08 17:58:04 +0000 | [diff] [blame] | 340 | case MachineOperand::MO_PCRelativeDisp: |
| 341 | std::cerr << "Shouldn't use addPCDisp() when building PPC MachineInstrs"; |
| 342 | abort(); |
| 343 | return; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 344 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 345 | case MachineOperand::MO_MachineBasicBlock: { |
| 346 | MachineBasicBlock *MBBOp = MO.getMachineBasicBlock(); |
Chris Lattner | 0745536 | 2005-11-21 08:14:07 +0000 | [diff] [blame] | 347 | O << PrivateGlobalPrefix << "BB" << getFunctionNumber() << "_" |
Chris Lattner | 7f9ccde | 2005-11-21 07:41:05 +0000 | [diff] [blame] | 348 | << MBBOp->getNumber() << "\t; " << MBBOp->getBasicBlock()->getName(); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 349 | return; |
| 350 | } |
Misha Brukman | 05fcd0c | 2004-07-08 17:58:04 +0000 | [diff] [blame] | 351 | |
| 352 | case MachineOperand::MO_ConstantPoolIndex: |
Chris Lattner | 0745536 | 2005-11-21 08:14:07 +0000 | [diff] [blame] | 353 | O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() |
Chris Lattner | f55366e | 2005-11-21 06:47:58 +0000 | [diff] [blame] | 354 | << '_' << MO.getConstantPoolIndex(); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 355 | return; |
Misha Brukman | 05fcd0c | 2004-07-08 17:58:04 +0000 | [diff] [blame] | 356 | case MachineOperand::MO_ExternalSymbol: |
Chris Lattner | a637c32 | 2005-12-16 00:22:14 +0000 | [diff] [blame] | 357 | // Computing the address of an external symbol, not calling it. |
| 358 | if (!PPCGenerateStaticCode) { |
| 359 | std::string Name(GlobalPrefix); Name += MO.getSymbolName(); |
| 360 | GVStubs.insert(Name); |
| 361 | O << "L" << Name << "$non_lazy_ptr"; |
| 362 | return; |
| 363 | } |
Nate Begeman | 01d0526 | 2005-03-30 01:45:43 +0000 | [diff] [blame] | 364 | O << GlobalPrefix << MO.getSymbolName(); |
Misha Brukman | 05fcd0c | 2004-07-08 17:58:04 +0000 | [diff] [blame] | 365 | return; |
Nate Begeman | b73a711 | 2004-08-13 09:32:01 +0000 | [diff] [blame] | 366 | case MachineOperand::MO_GlobalAddress: { |
Chris Lattner | a637c32 | 2005-12-16 00:22:14 +0000 | [diff] [blame] | 367 | // Computing the address of a global symbol, not calling it. |
Nate Begeman | b73a711 | 2004-08-13 09:32:01 +0000 | [diff] [blame] | 368 | GlobalValue *GV = MO.getGlobal(); |
| 369 | std::string Name = Mang->getValueName(GV); |
Nate Begeman | 50fb3c4 | 2005-12-24 01:00:15 +0000 | [diff] [blame] | 370 | int offset = MO.getOffset(); |
Misha Brukman | e2eceb5 | 2004-07-23 16:08:20 +0000 | [diff] [blame] | 371 | |
Nate Begeman | fcf4a42 | 2004-10-17 23:01:34 +0000 | [diff] [blame] | 372 | // External or weakly linked global variables need non-lazily-resolved stubs |
Chris Lattner | a637c32 | 2005-12-16 00:22:14 +0000 | [diff] [blame] | 373 | if (!PPCGenerateStaticCode) { |
| 374 | if (((GV->isExternal() || GV->hasWeakLinkage() || |
| 375 | GV->hasLinkOnceLinkage()))) { |
Nate Begeman | d4c8bea | 2004-11-25 07:09:01 +0000 | [diff] [blame] | 376 | GVStubs.insert(Name); |
Chris Lattner | a637c32 | 2005-12-16 00:22:14 +0000 | [diff] [blame] | 377 | O << "L" << Name << "$non_lazy_ptr"; |
| 378 | return; |
| 379 | } |
Nate Begeman | b73a711 | 2004-08-13 09:32:01 +0000 | [diff] [blame] | 380 | } |
Nate Begeman | d4c8bea | 2004-11-25 07:09:01 +0000 | [diff] [blame] | 381 | |
Chris Lattner | 9542f971 | 2005-11-17 19:40:30 +0000 | [diff] [blame] | 382 | O << Name; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 383 | return; |
Nate Begeman | b73a711 | 2004-08-13 09:32:01 +0000 | [diff] [blame] | 384 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 385 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 386 | default: |
Misha Brukman | 05fcd0c | 2004-07-08 17:58:04 +0000 | [diff] [blame] | 387 | O << "<unknown operand type: " << MO.getType() << ">"; |
Misha Brukman | 22e1207 | 2004-06-25 15:11:34 +0000 | [diff] [blame] | 388 | return; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 389 | } |
| 390 | } |
| 391 | |
Nate Begeman | e59bf59 | 2004-08-14 22:09:10 +0000 | [diff] [blame] | 392 | /// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to |
| 393 | /// the current output stream. |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 394 | /// |
Chris Lattner | 4c7b43b | 2005-10-14 23:37:35 +0000 | [diff] [blame] | 395 | void PPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) { |
Nate Begeman | e59bf59 | 2004-08-14 22:09:10 +0000 | [diff] [blame] | 396 | ++EmittedInsts; |
Chris Lattner | 617742b | 2005-10-14 22:44:13 +0000 | [diff] [blame] | 397 | |
Nate Begeman | ad5f65c | 2005-04-05 18:19:50 +0000 | [diff] [blame] | 398 | // Check for slwi/srwi mnemonics. |
| 399 | if (MI->getOpcode() == PPC::RLWINM) { |
| 400 | bool FoundMnemonic = false; |
| 401 | unsigned char SH = MI->getOperand(2).getImmedValue(); |
| 402 | unsigned char MB = MI->getOperand(3).getImmedValue(); |
| 403 | unsigned char ME = MI->getOperand(4).getImmedValue(); |
| 404 | if (SH <= 31 && MB == 0 && ME == (31-SH)) { |
| 405 | O << "slwi "; FoundMnemonic = true; |
| 406 | } |
| 407 | if (SH <= 31 && MB == (32-SH) && ME == 31) { |
| 408 | O << "srwi "; FoundMnemonic = true; |
| 409 | SH = 32-SH; |
| 410 | } |
| 411 | if (FoundMnemonic) { |
Nate Begeman | 391c5d2 | 2005-11-30 18:54:35 +0000 | [diff] [blame] | 412 | printOperand(MI, 0); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 413 | O << ", "; |
Nate Begeman | 391c5d2 | 2005-11-30 18:54:35 +0000 | [diff] [blame] | 414 | printOperand(MI, 1); |
Nate Begeman | ad5f65c | 2005-04-05 18:19:50 +0000 | [diff] [blame] | 415 | O << ", " << (unsigned int)SH << "\n"; |
| 416 | return; |
| 417 | } |
| 418 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 419 | |
Nate Begeman | e59bf59 | 2004-08-14 22:09:10 +0000 | [diff] [blame] | 420 | if (printInstruction(MI)) |
| 421 | return; // Printer was automatically generated |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 422 | |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 423 | assert(0 && "Unhandled instruction in asm writer!"); |
| 424 | abort(); |
Nate Begeman | e59bf59 | 2004-08-14 22:09:10 +0000 | [diff] [blame] | 425 | return; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Chris Lattner | ef65874 | 2005-11-21 07:57:37 +0000 | [diff] [blame] | 428 | |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 429 | /// runOnMachineFunction - This uses the printMachineInstruction() |
| 430 | /// method to print assembly for each instruction. |
| 431 | /// |
| 432 | bool DarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) { |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 433 | // FIXME - is this the earliest this can be set. |
| 434 | DW.SetDebugInfo(&getAnalysis<MachineDebugInfo>()); |
| 435 | |
Chris Lattner | 8b8b951 | 2005-11-21 07:51:23 +0000 | [diff] [blame] | 436 | SetupMachineFunction(MF); |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 437 | O << "\n\n"; |
Jim Laskey | f5395ce | 2005-12-16 22:45:29 +0000 | [diff] [blame] | 438 | |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 439 | // Emit pre-function debug information. |
| 440 | DW.BeginFunction(); |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 441 | |
| 442 | // Print out constants referenced by the function |
Chris Lattner | c569e61 | 2005-11-21 08:26:15 +0000 | [diff] [blame] | 443 | EmitConstantPool(MF.getConstantPool()); |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 444 | |
| 445 | // Print out labels for the function. |
Chris Lattner | ac7fd7f | 2005-11-14 18:52:46 +0000 | [diff] [blame] | 446 | const Function *F = MF.getFunction(); |
Chris Lattner | a637c32 | 2005-12-16 00:22:14 +0000 | [diff] [blame] | 447 | switch (F->getLinkage()) { |
| 448 | default: assert(0 && "Unknown linkage type!"); |
| 449 | case Function::InternalLinkage: // Symbols default to internal. |
| 450 | SwitchSection(".text", F); |
| 451 | EmitAlignment(4, F); |
| 452 | break; |
| 453 | case Function::ExternalLinkage: |
| 454 | SwitchSection(".text", F); |
| 455 | EmitAlignment(4, F); |
Chris Lattner | f02a916 | 2005-10-28 18:44:07 +0000 | [diff] [blame] | 456 | O << "\t.globl\t" << CurrentFnName << "\n"; |
Chris Lattner | a637c32 | 2005-12-16 00:22:14 +0000 | [diff] [blame] | 457 | break; |
| 458 | case Function::WeakLinkage: |
| 459 | case Function::LinkOnceLinkage: |
| 460 | SwitchSection(".section __TEXT,__textcoal_nt,coalesced,pure_instructions", |
| 461 | F); |
| 462 | O << "\t.globl\t" << CurrentFnName << "\n"; |
| 463 | O << "\t.weak_definition\t" << CurrentFnName << "\n"; |
| 464 | break; |
| 465 | } |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 466 | O << CurrentFnName << ":\n"; |
| 467 | |
| 468 | // Print out code for the function. |
| 469 | for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); |
| 470 | I != E; ++I) { |
| 471 | // Print a label for the basic block. |
Chris Lattner | 1db1adb | 2005-08-21 19:09:33 +0000 | [diff] [blame] | 472 | if (I != MF.begin()) { |
Chris Lattner | 0745536 | 2005-11-21 08:14:07 +0000 | [diff] [blame] | 473 | O << PrivateGlobalPrefix << "BB" << getFunctionNumber() << '_' |
Chris Lattner | 7f9ccde | 2005-11-21 07:41:05 +0000 | [diff] [blame] | 474 | << I->getNumber() << ":\t"; |
Chris Lattner | 1db1adb | 2005-08-21 19:09:33 +0000 | [diff] [blame] | 475 | if (!I->getBasicBlock()->getName().empty()) |
| 476 | O << CommentString << " " << I->getBasicBlock()->getName(); |
| 477 | O << "\n"; |
| 478 | } |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 479 | for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end(); |
| 480 | II != E; ++II) { |
| 481 | // Print the assembly for the instruction. |
| 482 | O << "\t"; |
| 483 | printMachineInstruction(II); |
| 484 | } |
| 485 | } |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 486 | |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 487 | // Emit post-function debug information. |
| 488 | DW.EndFunction(); |
| 489 | |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 490 | // We didn't modify anything. |
| 491 | return false; |
| 492 | } |
| 493 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 494 | |
Nate Begeman | 18ed029 | 2005-07-21 01:25:49 +0000 | [diff] [blame] | 495 | bool DarwinAsmPrinter::doInitialization(Module &M) { |
Chris Lattner | 3c304a3 | 2005-08-05 22:05:03 +0000 | [diff] [blame] | 496 | if (TM.getSubtarget<PPCSubtarget>().isGigaProcessor()) |
| 497 | O << "\t.machine ppc970\n"; |
Nate Begeman | 18ed029 | 2005-07-21 01:25:49 +0000 | [diff] [blame] | 498 | AsmPrinter::doInitialization(M); |
Chris Lattner | 85eac0d | 2005-11-10 19:33:43 +0000 | [diff] [blame] | 499 | |
| 500 | // Darwin wants symbols to be quoted if they have complex names. |
| 501 | Mang->setUseQuotes(true); |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 502 | |
| 503 | // Emit initial debug information. |
| 504 | DW.BeginModule(); |
Nate Begeman | 18ed029 | 2005-07-21 01:25:49 +0000 | [diff] [blame] | 505 | return false; |
| 506 | } |
| 507 | |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 508 | bool DarwinAsmPrinter::doFinalization(Module &M) { |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 509 | const TargetData &TD = TM.getTargetData(); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 510 | |
| 511 | // Print out module-level global variables here. |
Chris Lattner | ced704b | 2005-11-14 19:00:30 +0000 | [diff] [blame] | 512 | 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] | 513 | I != E; ++I) { |
| 514 | if (!I->hasInitializer()) continue; // External global require no code |
| 515 | |
Chris Lattner | d1239b7 | 2005-12-13 06:32:50 +0000 | [diff] [blame] | 516 | // Check to see if this is a special global used by LLVM, if so, emit it. |
| 517 | if (I->hasAppendingLinkage() && EmitSpecialLLVMGlobal(I)) |
| 518 | continue; |
Chris Lattner | deea416 | 2005-12-13 04:33:58 +0000 | [diff] [blame] | 519 | |
Chris Lattner | deea416 | 2005-12-13 04:33:58 +0000 | [diff] [blame] | 520 | std::string name = Mang->getValueName(I); |
| 521 | Constant *C = I->getInitializer(); |
| 522 | unsigned Size = TD.getTypeSize(C->getType()); |
| 523 | unsigned Align = TD.getTypeAlignmentShift(C->getType()); |
| 524 | |
| 525 | if (C->isNullValue() && /* FIXME: Verify correct */ |
| 526 | (I->hasInternalLinkage() || I->hasWeakLinkage() || |
| 527 | I->hasLinkOnceLinkage())) { |
| 528 | SwitchSection(".data", I); |
| 529 | if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it. |
| 530 | if (I->hasInternalLinkage()) |
| 531 | O << LCOMMDirective << name << "," << Size << "," << Align; |
| 532 | else |
| 533 | O << ".comm " << name << "," << Size; |
| 534 | O << "\t\t; '" << I->getName() << "'\n"; |
| 535 | } else { |
| 536 | switch (I->getLinkage()) { |
| 537 | case GlobalValue::LinkOnceLinkage: |
Chris Lattner | deea416 | 2005-12-13 04:33:58 +0000 | [diff] [blame] | 538 | case GlobalValue::WeakLinkage: |
Chris Lattner | cec26fc | 2005-12-22 21:15:17 +0000 | [diff] [blame] | 539 | O << "\t.globl " << name << '\n' |
| 540 | << "\t.weak_definition " << name << '\n'; |
Chris Lattner | a637c32 | 2005-12-16 00:22:14 +0000 | [diff] [blame] | 541 | SwitchSection(".section __DATA,__datacoal_nt,coalesced", I); |
Chris Lattner | deea416 | 2005-12-13 04:33:58 +0000 | [diff] [blame] | 542 | break; |
| 543 | case GlobalValue::AppendingLinkage: |
| 544 | // FIXME: appending linkage variables should go into a section of |
| 545 | // their name or something. For now, just emit them as external. |
| 546 | case GlobalValue::ExternalLinkage: |
| 547 | // If external or appending, declare as a global symbol |
| 548 | O << "\t.globl " << name << "\n"; |
| 549 | // FALL THROUGH |
| 550 | case GlobalValue::InternalLinkage: |
| 551 | SwitchSection(".data", I); |
| 552 | break; |
| 553 | default: |
| 554 | std::cerr << "Unknown linkage type!"; |
| 555 | abort(); |
| 556 | } |
| 557 | |
| 558 | EmitAlignment(Align, I); |
| 559 | O << name << ":\t\t\t\t; '" << I->getName() << "'\n"; |
| 560 | EmitGlobalConstant(C); |
Chris Lattner | bc1c215 | 2006-01-21 01:35:26 +0000 | [diff] [blame^] | 561 | O << '\n'; |
Chris Lattner | deea416 | 2005-12-13 04:33:58 +0000 | [diff] [blame] | 562 | } |
| 563 | } |
Misha Brukman | da2b13f | 2004-07-16 20:29:04 +0000 | [diff] [blame] | 564 | |
| 565 | // Output stubs for dynamically-linked functions |
Chris Lattner | deea416 | 2005-12-13 04:33:58 +0000 | [diff] [blame] | 566 | if (PICEnabled) { |
| 567 | for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end(); |
| 568 | i != e; ++i) { |
Chris Lattner | a637c32 | 2005-12-16 00:22:14 +0000 | [diff] [blame] | 569 | SwitchSection(".section __TEXT,__picsymbolstub1,symbol_stubs," |
| 570 | "pure_instructions,32", 0); |
Chris Lattner | deea416 | 2005-12-13 04:33:58 +0000 | [diff] [blame] | 571 | EmitAlignment(2); |
| 572 | O << "L" << *i << "$stub:\n"; |
| 573 | O << "\t.indirect_symbol " << *i << "\n"; |
| 574 | O << "\tmflr r0\n"; |
| 575 | O << "\tbcl 20,31,L0$" << *i << "\n"; |
| 576 | O << "L0$" << *i << ":\n"; |
| 577 | O << "\tmflr r11\n"; |
| 578 | O << "\taddis r11,r11,ha16(L" << *i << "$lazy_ptr-L0$" << *i << ")\n"; |
| 579 | O << "\tmtlr r0\n"; |
| 580 | O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr-L0$" << *i << ")(r11)\n"; |
| 581 | O << "\tmtctr r12\n"; |
| 582 | O << "\tbctr\n"; |
Chris Lattner | a637c32 | 2005-12-16 00:22:14 +0000 | [diff] [blame] | 583 | SwitchSection(".lazy_symbol_pointer", 0); |
Chris Lattner | deea416 | 2005-12-13 04:33:58 +0000 | [diff] [blame] | 584 | O << "L" << *i << "$lazy_ptr:\n"; |
| 585 | O << "\t.indirect_symbol " << *i << "\n"; |
| 586 | O << "\t.long dyld_stub_binding_helper\n"; |
| 587 | } |
| 588 | } else { |
| 589 | for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end(); |
| 590 | i != e; ++i) { |
Chris Lattner | a637c32 | 2005-12-16 00:22:14 +0000 | [diff] [blame] | 591 | SwitchSection(".section __TEXT,__symbol_stub1,symbol_stubs," |
| 592 | "pure_instructions,16", 0); |
Chris Lattner | deea416 | 2005-12-13 04:33:58 +0000 | [diff] [blame] | 593 | EmitAlignment(4); |
| 594 | O << "L" << *i << "$stub:\n"; |
| 595 | O << "\t.indirect_symbol " << *i << "\n"; |
| 596 | O << "\tlis r11,ha16(L" << *i << "$lazy_ptr)\n"; |
| 597 | O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr)(r11)\n"; |
| 598 | O << "\tmtctr r12\n"; |
| 599 | O << "\tbctr\n"; |
Chris Lattner | a637c32 | 2005-12-16 00:22:14 +0000 | [diff] [blame] | 600 | SwitchSection(".lazy_symbol_pointer", 0); |
Chris Lattner | deea416 | 2005-12-13 04:33:58 +0000 | [diff] [blame] | 601 | O << "L" << *i << "$lazy_ptr:\n"; |
| 602 | O << "\t.indirect_symbol " << *i << "\n"; |
| 603 | O << "\t.long dyld_stub_binding_helper\n"; |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 604 | } |
Misha Brukman | 46fd00a | 2004-06-24 23:04:11 +0000 | [diff] [blame] | 605 | } |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 606 | |
Misha Brukman | da2b13f | 2004-07-16 20:29:04 +0000 | [diff] [blame] | 607 | O << "\n"; |
| 608 | |
Chris Lattner | a637c32 | 2005-12-16 00:22:14 +0000 | [diff] [blame] | 609 | // Output stubs for external and common global variables. |
| 610 | if (GVStubs.begin() != GVStubs.end()) { |
| 611 | SwitchSection(".non_lazy_symbol_pointer", 0); |
| 612 | for (std::set<std::string>::iterator I = GVStubs.begin(), |
| 613 | E = GVStubs.end(); I != E; ++I) { |
| 614 | O << "L" << *I << "$non_lazy_ptr:\n"; |
| 615 | O << "\t.indirect_symbol " << *I << "\n"; |
| 616 | O << "\t.long\t0\n"; |
Chris Lattner | deea416 | 2005-12-13 04:33:58 +0000 | [diff] [blame] | 617 | } |
Nate Begeman | e59bf59 | 2004-08-14 22:09:10 +0000 | [diff] [blame] | 618 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 619 | |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 620 | // Emit initial debug information. |
| 621 | DW.EndModule(); |
| 622 | |
Chris Lattner | 5b0ac99 | 2005-11-01 00:12:36 +0000 | [diff] [blame] | 623 | // Funny Darwin hack: This flag tells the linker that no global symbols |
| 624 | // contain code that falls through to other global symbols (e.g. the obvious |
| 625 | // implementation of multiple entry points). If this doesn't occur, the |
| 626 | // linker can safely perform dead code stripping. Since LLVM never generates |
| 627 | // code that does this, it is always safe to set. |
| 628 | O << "\t.subsections_via_symbols\n"; |
| 629 | |
Chris Lattner | a384079 | 2004-08-16 23:25:21 +0000 | [diff] [blame] | 630 | AsmPrinter::doFinalization(M); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 631 | return false; // success |
| 632 | } |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 633 | |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 634 | /// runOnMachineFunction - This uses the e() |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 635 | /// method to print assembly for each instruction. |
| 636 | /// |
| 637 | bool AIXAsmPrinter::runOnMachineFunction(MachineFunction &MF) { |
Chris Lattner | 6d5a4f6 | 2005-11-21 08:02:41 +0000 | [diff] [blame] | 638 | SetupMachineFunction(MF); |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 639 | |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 640 | // Print out constants referenced by the function |
Chris Lattner | c569e61 | 2005-11-21 08:26:15 +0000 | [diff] [blame] | 641 | EmitConstantPool(MF.getConstantPool()); |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 642 | |
| 643 | // Print out header for the function. |
| 644 | O << "\t.csect .text[PR]\n" |
| 645 | << "\t.align 2\n" |
| 646 | << "\t.globl " << CurrentFnName << '\n' |
| 647 | << "\t.globl ." << CurrentFnName << '\n' |
| 648 | << "\t.csect " << CurrentFnName << "[DS],3\n" |
| 649 | << CurrentFnName << ":\n" |
| 650 | << "\t.llong ." << CurrentFnName << ", TOC[tc0], 0\n" |
| 651 | << "\t.csect .text[PR]\n" |
| 652 | << '.' << CurrentFnName << ":\n"; |
| 653 | |
| 654 | // Print out code for the function. |
| 655 | for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); |
| 656 | I != E; ++I) { |
| 657 | // Print a label for the basic block. |
Chris Lattner | 0745536 | 2005-11-21 08:14:07 +0000 | [diff] [blame] | 658 | O << PrivateGlobalPrefix << "BB" << getFunctionNumber() << '_' |
| 659 | << I->getNumber() |
Chris Lattner | 6d5a4f6 | 2005-11-21 08:02:41 +0000 | [diff] [blame] | 660 | << ":\t" << CommentString << I->getBasicBlock()->getName() << '\n'; |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 661 | for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end(); |
| 662 | II != E; ++II) { |
| 663 | // Print the assembly for the instruction. |
| 664 | O << "\t"; |
| 665 | printMachineInstruction(II); |
| 666 | } |
| 667 | } |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 668 | |
| 669 | O << "LT.." << CurrentFnName << ":\n" |
| 670 | << "\t.long 0\n" |
| 671 | << "\t.byte 0,0,32,65,128,0,0,0\n" |
| 672 | << "\t.long LT.." << CurrentFnName << "-." << CurrentFnName << '\n' |
| 673 | << "\t.short 3\n" |
| 674 | << "\t.byte \"" << CurrentFnName << "\"\n" |
| 675 | << "\t.align 2\n"; |
| 676 | |
| 677 | // We didn't modify anything. |
| 678 | return false; |
| 679 | } |
| 680 | |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 681 | bool AIXAsmPrinter::doInitialization(Module &M) { |
Chris Lattner | ac7fd7f | 2005-11-14 18:52:46 +0000 | [diff] [blame] | 682 | SwitchSection("", 0); |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 683 | const TargetData &TD = TM.getTargetData(); |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 684 | |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 685 | O << "\t.machine \"ppc64\"\n" |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 686 | << "\t.toc\n" |
| 687 | << "\t.csect .text[PR]\n"; |
| 688 | |
| 689 | // Print out module-level global variables |
Chris Lattner | 2e00d7d | 2005-07-26 19:03:27 +0000 | [diff] [blame] | 690 | for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); |
| 691 | I != E; ++I) { |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 692 | if (!I->hasInitializer()) |
| 693 | continue; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 694 | |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 695 | std::string Name = I->getName(); |
| 696 | Constant *C = I->getInitializer(); |
| 697 | // N.B.: We are defaulting to writable strings |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 698 | if (I->hasExternalLinkage()) { |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 699 | O << "\t.globl " << Name << '\n' |
| 700 | << "\t.csect .data[RW],3\n"; |
| 701 | } else { |
| 702 | O << "\t.csect _global.rw_c[RW],3\n"; |
| 703 | } |
| 704 | O << Name << ":\n"; |
Chris Lattner | 8b8b951 | 2005-11-21 07:51:23 +0000 | [diff] [blame] | 705 | EmitGlobalConstant(C); |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | // Output labels for globals |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 709 | if (M.global_begin() != M.global_end()) O << "\t.toc\n"; |
Chris Lattner | 2e00d7d | 2005-07-26 19:03:27 +0000 | [diff] [blame] | 710 | for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); |
| 711 | I != E; ++I) { |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 712 | const GlobalVariable *GV = I; |
| 713 | // Do not output labels for unused variables |
| 714 | if (GV->isExternal() && GV->use_begin() == GV->use_end()) |
| 715 | continue; |
| 716 | |
Chris Lattner | 0745536 | 2005-11-21 08:14:07 +0000 | [diff] [blame] | 717 | IncrementFunctionNumber(); |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 718 | std::string Name = GV->getName(); |
Chris Lattner | 0745536 | 2005-11-21 08:14:07 +0000 | [diff] [blame] | 719 | std::string Label = "LC.." + utostr(getFunctionNumber()); |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 720 | GVToLabelMap[GV] = Label; |
| 721 | O << Label << ":\n" |
| 722 | << "\t.tc " << Name << "[TC]," << Name; |
| 723 | if (GV->isExternal()) O << "[RW]"; |
| 724 | O << '\n'; |
Chris Lattner | 0745536 | 2005-11-21 08:14:07 +0000 | [diff] [blame] | 725 | } |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 726 | |
Chris Lattner | 3459bfb | 2005-11-10 18:20:29 +0000 | [diff] [blame] | 727 | AsmPrinter::doInitialization(M); |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 728 | return false; // success |
| 729 | } |
| 730 | |
| 731 | bool AIXAsmPrinter::doFinalization(Module &M) { |
| 732 | const TargetData &TD = TM.getTargetData(); |
| 733 | // Print out module-level global variables |
Chris Lattner | 2e00d7d | 2005-07-26 19:03:27 +0000 | [diff] [blame] | 734 | for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); |
| 735 | I != E; ++I) { |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 736 | if (I->hasInitializer() || I->hasExternalLinkage()) |
| 737 | continue; |
| 738 | |
| 739 | std::string Name = I->getName(); |
| 740 | if (I->hasInternalLinkage()) { |
| 741 | O << "\t.lcomm " << Name << ",16,_global.bss_c"; |
| 742 | } else { |
| 743 | O << "\t.comm " << Name << "," << TD.getTypeSize(I->getType()) |
Chris Lattner | 0561b3f | 2005-08-02 19:26:06 +0000 | [diff] [blame] | 744 | << "," << Log2_32((unsigned)TD.getTypeAlignment(I->getType())); |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 745 | } |
Chris Lattner | 6d5a4f6 | 2005-11-21 08:02:41 +0000 | [diff] [blame] | 746 | O << "\t\t" << CommentString << " "; |
Chris Lattner | 8ca0291 | 2005-10-03 07:08:36 +0000 | [diff] [blame] | 747 | WriteAsOperand(O, I, false, true, &M); |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 748 | O << "\n"; |
| 749 | } |
| 750 | |
| 751 | O << "_section_.text:\n" |
| 752 | << "\t.csect .data[RW],3\n" |
| 753 | << "\t.llong _section_.text\n"; |
Chris Lattner | 3459bfb | 2005-11-10 18:20:29 +0000 | [diff] [blame] | 754 | AsmPrinter::doFinalization(M); |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 755 | return false; // success |
| 756 | } |