Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===-- SparcAsmPrinter.cpp - Sparc LLVM assembly writer ------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains a printer that converts from our internal representation |
| 11 | // of machine-dependent LLVM code to GAS-format SPARC assembly language. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #define DEBUG_TYPE "asm-printer" |
| 16 | #include "Sparc.h" |
| 17 | #include "SparcInstrInfo.h" |
| 18 | #include "llvm/Constants.h" |
| 19 | #include "llvm/DerivedTypes.h" |
| 20 | #include "llvm/Module.h" |
| 21 | #include "llvm/CodeGen/AsmPrinter.h" |
| 22 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 23 | #include "llvm/CodeGen/MachineConstantPool.h" |
| 24 | #include "llvm/CodeGen/MachineInstr.h" |
| 25 | #include "llvm/Target/TargetAsmInfo.h" |
| 26 | #include "llvm/Target/TargetData.h" |
| 27 | #include "llvm/Target/TargetMachine.h" |
| 28 | #include "llvm/Support/Mangler.h" |
| 29 | #include "llvm/ADT/Statistic.h" |
| 30 | #include "llvm/ADT/StringExtras.h" |
| 31 | #include "llvm/Support/CommandLine.h" |
| 32 | #include "llvm/Support/MathExtras.h" |
| 33 | #include <cctype> |
Anton Korobeynikov | 357a27d | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 34 | #include <cstring> |
Owen Anderson | 80045bf | 2008-07-10 01:44:27 +0000 | [diff] [blame] | 35 | #include <map> |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 36 | using namespace llvm; |
| 37 | |
| 38 | STATISTIC(EmittedInsts, "Number of machine instrs printed"); |
| 39 | |
| 40 | namespace { |
| 41 | struct VISIBILITY_HIDDEN SparcAsmPrinter : public AsmPrinter { |
| 42 | SparcAsmPrinter(std::ostream &O, TargetMachine &TM, const TargetAsmInfo *T) |
| 43 | : AsmPrinter(O, TM, T) { |
| 44 | } |
| 45 | |
| 46 | /// We name each basic block in a Function with a unique number, so |
| 47 | /// that we can consistently refer to them later. This is cleared |
| 48 | /// at the beginning of each call to runOnMachineFunction(). |
| 49 | /// |
| 50 | typedef std::map<const Value *, unsigned> ValueMapTy; |
| 51 | ValueMapTy NumberForBB; |
| 52 | |
| 53 | virtual const char *getPassName() const { |
| 54 | return "Sparc Assembly Printer"; |
| 55 | } |
| 56 | |
Anton Korobeynikov | db9a895 | 2008-08-07 09:51:25 +0000 | [diff] [blame] | 57 | void printModuleLevelGV(const GlobalVariable* GVar); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 58 | void printOperand(const MachineInstr *MI, int opNum); |
| 59 | void printMemOperand(const MachineInstr *MI, int opNum, |
| 60 | const char *Modifier = 0); |
| 61 | void printCCOperand(const MachineInstr *MI, int opNum); |
| 62 | |
| 63 | bool printInstruction(const MachineInstr *MI); // autogenerated. |
| 64 | bool runOnMachineFunction(MachineFunction &F); |
Anton Korobeynikov | db9a895 | 2008-08-07 09:51:25 +0000 | [diff] [blame] | 65 | std::string getSectionForFunction(const Function &F) const; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 66 | bool doInitialization(Module &M); |
| 67 | bool doFinalization(Module &M); |
| 68 | }; |
| 69 | } // end of anonymous namespace |
| 70 | |
| 71 | #include "SparcGenAsmWriter.inc" |
| 72 | |
| 73 | /// createSparcCodePrinterPass - Returns a pass that prints the SPARC |
| 74 | /// assembly code for a MachineFunction to the given output stream, |
| 75 | /// using the given target machine description. This should work |
| 76 | /// regardless of whether the function is in SSA form. |
| 77 | /// |
| 78 | FunctionPass *llvm::createSparcCodePrinterPass(std::ostream &o, |
| 79 | TargetMachine &tm) { |
| 80 | return new SparcAsmPrinter(o, tm, tm.getTargetAsmInfo()); |
| 81 | } |
| 82 | |
Anton Korobeynikov | db9a895 | 2008-08-07 09:51:25 +0000 | [diff] [blame] | 83 | // Substitute old hook with new one temporary |
| 84 | std::string SparcAsmPrinter::getSectionForFunction(const Function &F) const { |
| 85 | return TAI->SectionForGlobal(&F); |
| 86 | } |
| 87 | |
Evan Cheng | 8b98869 | 2008-02-02 08:39:46 +0000 | [diff] [blame] | 88 | /// runOnMachineFunction - This uses the printInstruction() |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 89 | /// method to print assembly for each instruction. |
| 90 | /// |
| 91 | bool SparcAsmPrinter::runOnMachineFunction(MachineFunction &MF) { |
| 92 | SetupMachineFunction(MF); |
| 93 | |
| 94 | // Print out constants referenced by the function |
| 95 | EmitConstantPool(MF.getConstantPool()); |
| 96 | |
| 97 | // BBNumber is used here so that a given Printer will never give two |
| 98 | // BBs the same name. (If you have a better way, please let me know!) |
| 99 | static unsigned BBNumber = 0; |
| 100 | |
| 101 | O << "\n\n"; |
| 102 | // What's my mangled name? |
| 103 | CurrentFnName = Mang->getValueName(MF.getFunction()); |
| 104 | |
| 105 | // Print out the label for the function. |
| 106 | const Function *F = MF.getFunction(); |
| 107 | SwitchToTextSection(getSectionForFunction(*F).c_str(), F); |
| 108 | EmitAlignment(4, F); |
Anton Korobeynikov | 140c7e7 | 2008-08-07 09:53:13 +0000 | [diff] [blame^] | 109 | O << "\t.globl\t" << CurrentFnName << '\n'; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 110 | O << "\t.type\t" << CurrentFnName << ", #function\n"; |
| 111 | O << CurrentFnName << ":\n"; |
| 112 | |
| 113 | // Number each basic block so that we can consistently refer to them |
| 114 | // in PC-relative references. |
| 115 | // FIXME: Why not use the MBB numbers? |
| 116 | NumberForBB.clear(); |
| 117 | for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); |
| 118 | I != E; ++I) { |
| 119 | NumberForBB[I->getBasicBlock()] = BBNumber++; |
| 120 | } |
| 121 | |
| 122 | // Print out code for the function. |
| 123 | for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); |
| 124 | I != E; ++I) { |
| 125 | // Print a label for the basic block. |
| 126 | if (I != MF.begin()) { |
Evan Cheng | 45c1edb | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 127 | printBasicBlockLabel(I, true, true); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 128 | O << '\n'; |
| 129 | } |
| 130 | for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end(); |
| 131 | II != E; ++II) { |
| 132 | // Print the assembly for the instruction. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 133 | printInstruction(II); |
| 134 | ++EmittedInsts; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | // We didn't modify anything. |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum) { |
| 143 | const MachineOperand &MO = MI->getOperand (opNum); |
Dan Gohman | 1e57df3 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 144 | const TargetRegisterInfo &RI = *TM.getRegisterInfo(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 145 | bool CloseParen = false; |
| 146 | if (MI->getOpcode() == SP::SETHIi && !MO.isRegister() && !MO.isImmediate()) { |
| 147 | O << "%hi("; |
| 148 | CloseParen = true; |
| 149 | } else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri) |
| 150 | && !MO.isRegister() && !MO.isImmediate()) { |
| 151 | O << "%lo("; |
| 152 | CloseParen = true; |
| 153 | } |
| 154 | switch (MO.getType()) { |
| 155 | case MachineOperand::MO_Register: |
Dan Gohman | 1e57df3 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 156 | if (TargetRegisterInfo::isPhysicalRegister(MO.getReg())) |
Bill Wendling | 8eeb979 | 2008-02-26 21:11:01 +0000 | [diff] [blame] | 157 | O << "%" << LowercaseString (RI.get(MO.getReg()).AsmName); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 158 | else |
| 159 | O << "%reg" << MO.getReg(); |
| 160 | break; |
| 161 | |
| 162 | case MachineOperand::MO_Immediate: |
Chris Lattner | a96056a | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 163 | O << (int)MO.getImm(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 164 | break; |
| 165 | case MachineOperand::MO_MachineBasicBlock: |
Chris Lattner | 6017d48 | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 166 | printBasicBlockLabel(MO.getMBB()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 167 | return; |
| 168 | case MachineOperand::MO_GlobalAddress: |
| 169 | O << Mang->getValueName(MO.getGlobal()); |
| 170 | break; |
| 171 | case MachineOperand::MO_ExternalSymbol: |
| 172 | O << MO.getSymbolName(); |
| 173 | break; |
| 174 | case MachineOperand::MO_ConstantPoolIndex: |
Evan Cheng | 477013c | 2007-10-14 05:57:21 +0000 | [diff] [blame] | 175 | O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_" |
Chris Lattner | 6017d48 | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 176 | << MO.getIndex(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 177 | break; |
| 178 | default: |
| 179 | O << "<unknown operand type>"; abort (); break; |
| 180 | } |
| 181 | if (CloseParen) O << ")"; |
| 182 | } |
| 183 | |
| 184 | void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum, |
| 185 | const char *Modifier) { |
| 186 | printOperand(MI, opNum); |
Anton Korobeynikov | db9a895 | 2008-08-07 09:51:25 +0000 | [diff] [blame] | 187 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 188 | // If this is an ADD operand, emit it like normal operands. |
| 189 | if (Modifier && !strcmp(Modifier, "arith")) { |
| 190 | O << ", "; |
| 191 | printOperand(MI, opNum+1); |
| 192 | return; |
| 193 | } |
Anton Korobeynikov | db9a895 | 2008-08-07 09:51:25 +0000 | [diff] [blame] | 194 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 195 | if (MI->getOperand(opNum+1).isRegister() && |
| 196 | MI->getOperand(opNum+1).getReg() == SP::G0) |
| 197 | return; // don't print "+%g0" |
| 198 | if (MI->getOperand(opNum+1).isImmediate() && |
Chris Lattner | a96056a | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 199 | MI->getOperand(opNum+1).getImm() == 0) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 200 | return; // don't print "+0" |
Anton Korobeynikov | db9a895 | 2008-08-07 09:51:25 +0000 | [diff] [blame] | 201 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 202 | O << "+"; |
| 203 | if (MI->getOperand(opNum+1).isGlobalAddress() || |
| 204 | MI->getOperand(opNum+1).isConstantPoolIndex()) { |
| 205 | O << "%lo("; |
| 206 | printOperand(MI, opNum+1); |
| 207 | O << ")"; |
| 208 | } else { |
| 209 | printOperand(MI, opNum+1); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) { |
Chris Lattner | a96056a | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 214 | int CC = (int)MI->getOperand(opNum).getImm(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 215 | O << SPARCCondCodeToString((SPCC::CondCodes)CC); |
| 216 | } |
| 217 | |
| 218 | |
| 219 | |
| 220 | bool SparcAsmPrinter::doInitialization(Module &M) { |
| 221 | Mang = new Mangler(M); |
| 222 | return false; // success |
| 223 | } |
| 224 | |
| 225 | bool SparcAsmPrinter::doFinalization(Module &M) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 226 | // Print out module-level global variables here. |
| 227 | for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); |
| 228 | I != E; ++I) |
Anton Korobeynikov | db9a895 | 2008-08-07 09:51:25 +0000 | [diff] [blame] | 229 | printModuleLevelGV(I); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 230 | |
Anton Korobeynikov | db9a895 | 2008-08-07 09:51:25 +0000 | [diff] [blame] | 231 | O << '\n'; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 232 | |
Dan Gohman | 4a558a3 | 2007-07-25 19:33:14 +0000 | [diff] [blame] | 233 | return AsmPrinter::doFinalization(M); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 234 | } |
Anton Korobeynikov | db9a895 | 2008-08-07 09:51:25 +0000 | [diff] [blame] | 235 | |
| 236 | void SparcAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) { |
| 237 | const TargetData *TD = TM.getTargetData(); |
| 238 | |
| 239 | if (!GVar->hasInitializer()) |
| 240 | return; // External global require no code |
| 241 | |
| 242 | // Check to see if this is a special global used by LLVM, if so, emit it. |
| 243 | if (EmitSpecialLLVMGlobal(GVar)) |
| 244 | return; |
| 245 | |
| 246 | O << "\n\n"; |
| 247 | std::string SectionName = TAI->SectionForGlobal(GVar); |
| 248 | std::string name = Mang->getValueName(GVar); |
| 249 | Constant *C = GVar->getInitializer(); |
| 250 | unsigned Size = TD->getABITypeSize(C->getType()); |
| 251 | unsigned Align = TD->getPreferredAlignment(GVar); |
| 252 | |
| 253 | // FIXME: ELF supports visibility |
| 254 | SwitchToDataSection(SectionName.c_str()); |
| 255 | |
| 256 | if (C->isNullValue() && !GVar->hasSection()) { |
| 257 | if (!GVar->isThreadLocal() && |
| 258 | (GVar->hasInternalLinkage() || GVar->isWeakForLinker())) { |
| 259 | if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it. |
| 260 | |
| 261 | if (GVar->hasInternalLinkage()) |
Anton Korobeynikov | 140c7e7 | 2008-08-07 09:53:13 +0000 | [diff] [blame^] | 262 | O << "\t.local " << name << '\n'; |
Anton Korobeynikov | db9a895 | 2008-08-07 09:51:25 +0000 | [diff] [blame] | 263 | |
| 264 | O << TAI->getCOMMDirective() << name << ',' << Size; |
| 265 | if (TAI->getCOMMDirectiveTakesAlignment()) |
| 266 | O << ',' << (1 << Align); |
| 267 | |
| 268 | O << '\n'; |
| 269 | return; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | switch (GVar->getLinkage()) { |
| 274 | case GlobalValue::CommonLinkage: |
| 275 | case GlobalValue::LinkOnceLinkage: |
| 276 | case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak. |
| 277 | // Nonnull linkonce -> weak |
Anton Korobeynikov | 140c7e7 | 2008-08-07 09:53:13 +0000 | [diff] [blame^] | 278 | O << "\t.weak " << name << '\n'; |
Anton Korobeynikov | db9a895 | 2008-08-07 09:51:25 +0000 | [diff] [blame] | 279 | break; |
| 280 | case GlobalValue::AppendingLinkage: |
| 281 | // FIXME: appending linkage variables should go into a section of |
| 282 | // their name or something. For now, just emit them as external. |
| 283 | case GlobalValue::ExternalLinkage: |
| 284 | // If external or appending, declare as a global symbol |
Anton Korobeynikov | 140c7e7 | 2008-08-07 09:53:13 +0000 | [diff] [blame^] | 285 | O << TAI->getGlobalDirective() << name << '\n'; |
Anton Korobeynikov | db9a895 | 2008-08-07 09:51:25 +0000 | [diff] [blame] | 286 | // FALL THROUGH |
| 287 | case GlobalValue::InternalLinkage: |
| 288 | break; |
| 289 | case GlobalValue::GhostLinkage: |
| 290 | cerr << "Should not have any unmaterialized functions!\n"; |
| 291 | abort(); |
| 292 | case GlobalValue::DLLImportLinkage: |
| 293 | cerr << "DLLImport linkage is not supported by this target!\n"; |
| 294 | abort(); |
| 295 | case GlobalValue::DLLExportLinkage: |
| 296 | cerr << "DLLExport linkage is not supported by this target!\n"; |
| 297 | abort(); |
| 298 | default: |
| 299 | assert(0 && "Unknown linkage type!"); |
| 300 | } |
| 301 | |
| 302 | if (Align) |
Anton Korobeynikov | 140c7e7 | 2008-08-07 09:53:13 +0000 | [diff] [blame^] | 303 | O << "\t.align " << Align << '\n'; |
Anton Korobeynikov | db9a895 | 2008-08-07 09:51:25 +0000 | [diff] [blame] | 304 | |
| 305 | if (TAI->hasDotTypeDotSizeDirective()) { |
| 306 | O << "\t.type " << name << ",#object\n"; |
Anton Korobeynikov | 140c7e7 | 2008-08-07 09:53:13 +0000 | [diff] [blame^] | 307 | O << "\t.size " << name << ',' << Size << '\n'; |
Anton Korobeynikov | db9a895 | 2008-08-07 09:51:25 +0000 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | O << name << ":\n"; |
| 311 | EmitGlobalConstant(C); |
| 312 | } |