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