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