Scott Michel | 73655bc | 2008-11-08 18:59:02 +0000 | [diff] [blame] | 1 | //===-- SPUAsmPrinter.cpp - Print machine instrs to Cell SPU assembly -------=// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains a printer that converts from our internal representation |
| 11 | // of machine-dependent LLVM code to Cell SPU assembly language. This printer |
| 12 | // is the output mechanism used by `llc'. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #define DEBUG_TYPE "asmprinter" |
| 17 | #include "SPU.h" |
| 18 | #include "SPUTargetMachine.h" |
| 19 | #include "llvm/Constants.h" |
| 20 | #include "llvm/DerivedTypes.h" |
| 21 | #include "llvm/Module.h" |
| 22 | #include "llvm/Assembly/Writer.h" |
| 23 | #include "llvm/CodeGen/AsmPrinter.h" |
| 24 | #include "llvm/CodeGen/DwarfWriter.h" |
| 25 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| 26 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 27 | #include "llvm/CodeGen/MachineInstr.h" |
| 28 | #include "llvm/Support/Mangler.h" |
| 29 | #include "llvm/Support/MathExtras.h" |
| 30 | #include "llvm/Support/CommandLine.h" |
| 31 | #include "llvm/Support/Debug.h" |
| 32 | #include "llvm/Support/Compiler.h" |
| 33 | #include "llvm/Support/raw_ostream.h" |
| 34 | #include "llvm/Target/TargetAsmInfo.h" |
| 35 | #include "llvm/Target/TargetRegisterInfo.h" |
| 36 | #include "llvm/Target/TargetInstrInfo.h" |
| 37 | #include "llvm/Target/TargetOptions.h" |
| 38 | #include "llvm/ADT/Statistic.h" |
| 39 | #include "llvm/ADT/StringExtras.h" |
| 40 | #include <set> |
| 41 | using namespace llvm; |
| 42 | |
| 43 | namespace { |
| 44 | STATISTIC(EmittedInsts, "Number of machine instrs printed"); |
| 45 | |
| 46 | const std::string bss_section(".bss"); |
| 47 | |
| 48 | struct VISIBILITY_HIDDEN SPUAsmPrinter : public AsmPrinter { |
| 49 | std::set<std::string> FnStubs, GVStubs; |
| 50 | |
| 51 | SPUAsmPrinter(raw_ostream &O, TargetMachine &TM, const TargetAsmInfo *T) : |
| 52 | AsmPrinter(O, TM, T) |
| 53 | { |
| 54 | } |
| 55 | |
| 56 | virtual const char *getPassName() const { |
| 57 | return "STI CBEA SPU Assembly Printer"; |
| 58 | } |
| 59 | |
| 60 | SPUTargetMachine &getTM() { |
| 61 | return static_cast<SPUTargetMachine&>(TM); |
| 62 | } |
| 63 | |
| 64 | /// printInstruction - This method is automatically generated by tablegen |
| 65 | /// from the instruction set description. This method returns true if the |
| 66 | /// machine instruction was sufficiently described to print it, otherwise it |
| 67 | /// returns false. |
| 68 | bool printInstruction(const MachineInstr *MI); |
| 69 | |
| 70 | void printMachineInstruction(const MachineInstr *MI); |
| 71 | void printOp(const MachineOperand &MO); |
| 72 | |
| 73 | /// printRegister - Print register according to target requirements. |
| 74 | /// |
| 75 | void printRegister(const MachineOperand &MO, bool R0AsZero) { |
| 76 | unsigned RegNo = MO.getReg(); |
| 77 | assert(TargetRegisterInfo::isPhysicalRegister(RegNo) && |
| 78 | "Not physreg??"); |
| 79 | O << TM.getRegisterInfo()->get(RegNo).AsmName; |
| 80 | } |
| 81 | |
| 82 | void printOperand(const MachineInstr *MI, unsigned OpNo) { |
| 83 | const MachineOperand &MO = MI->getOperand(OpNo); |
| 84 | if (MO.isReg()) { |
| 85 | assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??"); |
| 86 | O << TM.getRegisterInfo()->get(MO.getReg()).AsmName; |
| 87 | } else if (MO.isImm()) { |
| 88 | O << MO.getImm(); |
| 89 | } else { |
| 90 | printOp(MO); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, |
| 95 | unsigned AsmVariant, const char *ExtraCode); |
| 96 | bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo, |
| 97 | unsigned AsmVariant, const char *ExtraCode); |
| 98 | |
| 99 | |
| 100 | void |
| 101 | printS7ImmOperand(const MachineInstr *MI, unsigned OpNo) |
| 102 | { |
| 103 | int value = MI->getOperand(OpNo).getImm(); |
| 104 | value = (value << (32 - 7)) >> (32 - 7); |
| 105 | |
| 106 | assert((value >= -(1 << 8) && value <= (1 << 7) - 1) |
| 107 | && "Invalid s7 argument"); |
| 108 | O << value; |
| 109 | } |
| 110 | |
| 111 | void |
| 112 | printU7ImmOperand(const MachineInstr *MI, unsigned OpNo) |
| 113 | { |
| 114 | unsigned int value = MI->getOperand(OpNo).getImm(); |
| 115 | assert(value < (1 << 8) && "Invalid u7 argument"); |
| 116 | O << value; |
| 117 | } |
| 118 | |
| 119 | void |
Scott Michel | f0569be | 2008-12-27 04:51:36 +0000 | [diff] [blame] | 120 | printShufAddr(const MachineInstr *MI, unsigned OpNo) |
Scott Michel | 73655bc | 2008-11-08 18:59:02 +0000 | [diff] [blame] | 121 | { |
| 122 | char value = MI->getOperand(OpNo).getImm(); |
| 123 | O << (int) value; |
| 124 | O << "("; |
| 125 | printOperand(MI, OpNo+1); |
| 126 | O << ")"; |
| 127 | } |
| 128 | |
| 129 | void |
| 130 | printS16ImmOperand(const MachineInstr *MI, unsigned OpNo) |
| 131 | { |
| 132 | O << (short) MI->getOperand(OpNo).getImm(); |
| 133 | } |
| 134 | |
| 135 | void |
| 136 | printU16ImmOperand(const MachineInstr *MI, unsigned OpNo) |
| 137 | { |
| 138 | O << (unsigned short)MI->getOperand(OpNo).getImm(); |
| 139 | } |
| 140 | |
| 141 | void |
| 142 | printU32ImmOperand(const MachineInstr *MI, unsigned OpNo) |
| 143 | { |
| 144 | O << (unsigned)MI->getOperand(OpNo).getImm(); |
| 145 | } |
| 146 | |
| 147 | void |
| 148 | printMemRegReg(const MachineInstr *MI, unsigned OpNo) { |
| 149 | // When used as the base register, r0 reads constant zero rather than |
| 150 | // the value contained in the register. For this reason, the darwin |
| 151 | // assembler requires that we print r0 as 0 (no r) when used as the base. |
| 152 | const MachineOperand &MO = MI->getOperand(OpNo); |
| 153 | O << TM.getRegisterInfo()->get(MO.getReg()).AsmName; |
| 154 | O << ", "; |
| 155 | printOperand(MI, OpNo+1); |
| 156 | } |
| 157 | |
| 158 | void |
| 159 | printU18ImmOperand(const MachineInstr *MI, unsigned OpNo) |
| 160 | { |
| 161 | unsigned int value = MI->getOperand(OpNo).getImm(); |
| 162 | assert(value <= (1 << 19) - 1 && "Invalid u18 argument"); |
| 163 | O << value; |
| 164 | } |
| 165 | |
| 166 | void |
| 167 | printS10ImmOperand(const MachineInstr *MI, unsigned OpNo) |
| 168 | { |
| 169 | short value = (short) (((int) MI->getOperand(OpNo).getImm() << 16) |
| 170 | >> 16); |
| 171 | assert((value >= -(1 << 9) && value <= (1 << 9) - 1) |
| 172 | && "Invalid s10 argument"); |
| 173 | O << value; |
| 174 | } |
| 175 | |
| 176 | void |
| 177 | printU10ImmOperand(const MachineInstr *MI, unsigned OpNo) |
| 178 | { |
| 179 | short value = (short) (((int) MI->getOperand(OpNo).getImm() << 16) |
| 180 | >> 16); |
| 181 | assert((value <= (1 << 10) - 1) && "Invalid u10 argument"); |
| 182 | O << value; |
| 183 | } |
| 184 | |
| 185 | void |
Scott Michel | f0569be | 2008-12-27 04:51:36 +0000 | [diff] [blame] | 186 | printDFormAddr(const MachineInstr *MI, unsigned OpNo) |
Scott Michel | 73655bc | 2008-11-08 18:59:02 +0000 | [diff] [blame] | 187 | { |
Chris Lattner | 0f2d995 | 2009-01-21 18:38:18 +0000 | [diff] [blame] | 188 | assert(MI->getOperand(OpNo).isImm() && |
| 189 | "printDFormAddr first operand is not immediate"); |
Scott Michel | 73655bc | 2008-11-08 18:59:02 +0000 | [diff] [blame] | 190 | int64_t value = int64_t(MI->getOperand(OpNo).getImm()); |
Scott Michel | 4379efc | 2008-11-20 05:01:09 +0000 | [diff] [blame] | 191 | int16_t value16 = int16_t(value); |
| 192 | assert((value16 >= -(1 << (9+4)) && value16 <= (1 << (9+4)) - 1) |
Scott Michel | 73655bc | 2008-11-08 18:59:02 +0000 | [diff] [blame] | 193 | && "Invalid dform s10 offset argument"); |
Scott Michel | f0569be | 2008-12-27 04:51:36 +0000 | [diff] [blame] | 194 | O << (value16 & ~0xf) << "("; |
Scott Michel | 73655bc | 2008-11-08 18:59:02 +0000 | [diff] [blame] | 195 | printOperand(MI, OpNo+1); |
| 196 | O << ")"; |
| 197 | } |
| 198 | |
| 199 | void |
| 200 | printAddr256K(const MachineInstr *MI, unsigned OpNo) |
| 201 | { |
| 202 | /* Note: operand 1 is an offset or symbol name. */ |
| 203 | if (MI->getOperand(OpNo).isImm()) { |
| 204 | printS16ImmOperand(MI, OpNo); |
| 205 | } else { |
| 206 | printOp(MI->getOperand(OpNo)); |
| 207 | if (MI->getOperand(OpNo+1).isImm()) { |
| 208 | int displ = int(MI->getOperand(OpNo+1).getImm()); |
| 209 | if (displ > 0) |
| 210 | O << "+" << displ; |
| 211 | else if (displ < 0) |
| 212 | O << displ; |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | void printCallOperand(const MachineInstr *MI, unsigned OpNo) { |
| 218 | printOp(MI->getOperand(OpNo)); |
| 219 | } |
| 220 | |
| 221 | void printPCRelativeOperand(const MachineInstr *MI, unsigned OpNo) { |
Scott Michel | aedc637 | 2008-12-10 00:15:19 +0000 | [diff] [blame] | 222 | // Used to generate a ".-<target>", but it turns out that the assembler |
| 223 | // really wants the target. |
| 224 | // |
| 225 | // N.B.: This operand is used for call targets. Branch hints are another |
| 226 | // animal entirely. |
| 227 | printOp(MI->getOperand(OpNo)); |
| 228 | } |
| 229 | |
| 230 | void printHBROperand(const MachineInstr *MI, unsigned OpNo) { |
| 231 | // HBR operands are generated in front of branches, hence, the |
| 232 | // program counter plus the target. |
| 233 | O << ".+"; |
Scott Michel | 73655bc | 2008-11-08 18:59:02 +0000 | [diff] [blame] | 234 | printOp(MI->getOperand(OpNo)); |
Scott Michel | 73655bc | 2008-11-08 18:59:02 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | void printSymbolHi(const MachineInstr *MI, unsigned OpNo) { |
| 238 | if (MI->getOperand(OpNo).isImm()) { |
| 239 | printS16ImmOperand(MI, OpNo); |
| 240 | } else { |
| 241 | printOp(MI->getOperand(OpNo)); |
| 242 | O << "@h"; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | void printSymbolLo(const MachineInstr *MI, unsigned OpNo) { |
| 247 | if (MI->getOperand(OpNo).isImm()) { |
| 248 | printS16ImmOperand(MI, OpNo); |
| 249 | } else { |
| 250 | printOp(MI->getOperand(OpNo)); |
| 251 | O << "@l"; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | /// Print local store address |
| 256 | void printSymbolLSA(const MachineInstr *MI, unsigned OpNo) { |
| 257 | printOp(MI->getOperand(OpNo)); |
| 258 | } |
| 259 | |
| 260 | void printROTHNeg7Imm(const MachineInstr *MI, unsigned OpNo) { |
| 261 | if (MI->getOperand(OpNo).isImm()) { |
| 262 | int value = (int) MI->getOperand(OpNo).getImm(); |
| 263 | assert((value >= 0 && value < 16) |
| 264 | && "Invalid negated immediate rotate 7-bit argument"); |
| 265 | O << -value; |
| 266 | } else { |
| 267 | assert(0 &&"Invalid/non-immediate rotate amount in printRotateNeg7Imm"); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | void printROTNeg7Imm(const MachineInstr *MI, unsigned OpNo) { |
| 272 | if (MI->getOperand(OpNo).isImm()) { |
| 273 | int value = (int) MI->getOperand(OpNo).getImm(); |
Scott Michel | 104de43 | 2008-11-24 17:11:17 +0000 | [diff] [blame] | 274 | assert((value >= 0 && value <= 32) |
Scott Michel | 73655bc | 2008-11-08 18:59:02 +0000 | [diff] [blame] | 275 | && "Invalid negated immediate rotate 7-bit argument"); |
| 276 | O << -value; |
| 277 | } else { |
| 278 | assert(0 &&"Invalid/non-immediate rotate amount in printRotateNeg7Imm"); |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | virtual bool runOnMachineFunction(MachineFunction &F) = 0; |
| 283 | //! Assembly printer cleanup after function has been emitted |
| 284 | virtual bool doFinalization(Module &M) = 0; |
| 285 | }; |
| 286 | |
| 287 | /// LinuxAsmPrinter - SPU assembly printer, customized for Linux |
| 288 | struct VISIBILITY_HIDDEN LinuxAsmPrinter : public SPUAsmPrinter { |
| 289 | |
Devang Patel | eb3fc28 | 2009-01-08 23:40:34 +0000 | [diff] [blame] | 290 | DwarfWriter *DW; |
Scott Michel | 73655bc | 2008-11-08 18:59:02 +0000 | [diff] [blame] | 291 | MachineModuleInfo *MMI; |
| 292 | |
| 293 | LinuxAsmPrinter(raw_ostream &O, SPUTargetMachine &TM, |
| 294 | const TargetAsmInfo *T) : |
| 295 | SPUAsmPrinter(O, TM, T), |
Devang Patel | eb3fc28 | 2009-01-08 23:40:34 +0000 | [diff] [blame] | 296 | DW(0), |
Scott Michel | 73655bc | 2008-11-08 18:59:02 +0000 | [diff] [blame] | 297 | MMI(0) |
| 298 | { } |
| 299 | |
| 300 | virtual const char *getPassName() const { |
| 301 | return "STI CBEA SPU Assembly Printer"; |
| 302 | } |
| 303 | |
| 304 | bool runOnMachineFunction(MachineFunction &F); |
| 305 | bool doInitialization(Module &M); |
| 306 | //! Dump globals, perform cleanup after function emission |
| 307 | bool doFinalization(Module &M); |
| 308 | |
| 309 | void getAnalysisUsage(AnalysisUsage &AU) const { |
| 310 | AU.setPreservesAll(); |
| 311 | AU.addRequired<MachineModuleInfo>(); |
Devang Patel | eb3fc28 | 2009-01-08 23:40:34 +0000 | [diff] [blame] | 312 | AU.addRequired<DwarfWriter>(); |
Scott Michel | 73655bc | 2008-11-08 18:59:02 +0000 | [diff] [blame] | 313 | SPUAsmPrinter::getAnalysisUsage(AU); |
| 314 | } |
| 315 | |
| 316 | //! Emit a global variable according to its section and type |
| 317 | void printModuleLevelGV(const GlobalVariable* GVar); |
| 318 | }; |
| 319 | } // end of anonymous namespace |
| 320 | |
| 321 | // Include the auto-generated portion of the assembly writer |
| 322 | #include "SPUGenAsmWriter.inc" |
| 323 | |
| 324 | void SPUAsmPrinter::printOp(const MachineOperand &MO) { |
| 325 | switch (MO.getType()) { |
| 326 | case MachineOperand::MO_Immediate: |
| 327 | cerr << "printOp() does not handle immediate values\n"; |
| 328 | abort(); |
| 329 | return; |
| 330 | |
| 331 | case MachineOperand::MO_MachineBasicBlock: |
| 332 | printBasicBlockLabel(MO.getMBB()); |
| 333 | return; |
| 334 | case MachineOperand::MO_JumpTableIndex: |
| 335 | O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() |
| 336 | << '_' << MO.getIndex(); |
| 337 | return; |
| 338 | case MachineOperand::MO_ConstantPoolIndex: |
| 339 | O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() |
| 340 | << '_' << MO.getIndex(); |
| 341 | return; |
| 342 | case MachineOperand::MO_ExternalSymbol: |
| 343 | // Computing the address of an external symbol, not calling it. |
| 344 | if (TM.getRelocationModel() != Reloc::Static) { |
| 345 | std::string Name(TAI->getGlobalPrefix()); Name += MO.getSymbolName(); |
| 346 | GVStubs.insert(Name); |
| 347 | O << "L" << Name << "$non_lazy_ptr"; |
| 348 | return; |
| 349 | } |
| 350 | O << TAI->getGlobalPrefix() << MO.getSymbolName(); |
| 351 | return; |
| 352 | case MachineOperand::MO_GlobalAddress: { |
| 353 | // Computing the address of a global symbol, not calling it. |
| 354 | GlobalValue *GV = MO.getGlobal(); |
| 355 | std::string Name = Mang->getValueName(GV); |
| 356 | |
| 357 | // External or weakly linked global variables need non-lazily-resolved |
| 358 | // stubs |
| 359 | if (TM.getRelocationModel() != Reloc::Static) { |
| 360 | if (((GV->isDeclaration() || GV->hasWeakLinkage() || |
| 361 | GV->hasLinkOnceLinkage() || GV->hasCommonLinkage()))) { |
| 362 | GVStubs.insert(Name); |
| 363 | O << "L" << Name << "$non_lazy_ptr"; |
| 364 | return; |
| 365 | } |
| 366 | } |
| 367 | O << Name; |
| 368 | |
| 369 | if (GV->hasExternalWeakLinkage()) |
| 370 | ExtWeakSymbols.insert(GV); |
| 371 | return; |
| 372 | } |
| 373 | |
| 374 | default: |
| 375 | O << "<unknown operand type: " << MO.getType() << ">"; |
| 376 | return; |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | /// PrintAsmOperand - Print out an operand for an inline asm expression. |
| 381 | /// |
| 382 | bool SPUAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, |
| 383 | unsigned AsmVariant, |
| 384 | const char *ExtraCode) { |
| 385 | // Does this asm operand have a single letter operand modifier? |
| 386 | if (ExtraCode && ExtraCode[0]) { |
| 387 | if (ExtraCode[1] != 0) return true; // Unknown modifier. |
| 388 | |
| 389 | switch (ExtraCode[0]) { |
| 390 | default: return true; // Unknown modifier. |
| 391 | case 'L': // Write second word of DImode reference. |
| 392 | // Verify that this operand has two consecutive registers. |
| 393 | if (!MI->getOperand(OpNo).isReg() || |
| 394 | OpNo+1 == MI->getNumOperands() || |
| 395 | !MI->getOperand(OpNo+1).isReg()) |
| 396 | return true; |
| 397 | ++OpNo; // Return the high-part. |
| 398 | break; |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | printOperand(MI, OpNo); |
| 403 | return false; |
| 404 | } |
| 405 | |
| 406 | bool SPUAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, |
| 407 | unsigned OpNo, |
| 408 | unsigned AsmVariant, |
| 409 | const char *ExtraCode) { |
| 410 | if (ExtraCode && ExtraCode[0]) |
| 411 | return true; // Unknown modifier. |
| 412 | printMemRegReg(MI, OpNo); |
| 413 | return false; |
| 414 | } |
| 415 | |
| 416 | /// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax |
| 417 | /// to the current output stream. |
| 418 | /// |
| 419 | void SPUAsmPrinter::printMachineInstruction(const MachineInstr *MI) { |
| 420 | ++EmittedInsts; |
| 421 | printInstruction(MI); |
| 422 | } |
| 423 | |
| 424 | /// runOnMachineFunction - This uses the printMachineInstruction() |
| 425 | /// method to print assembly for each instruction. |
| 426 | /// |
| 427 | bool |
| 428 | LinuxAsmPrinter::runOnMachineFunction(MachineFunction &MF) |
| 429 | { |
| 430 | SetupMachineFunction(MF); |
| 431 | O << "\n\n"; |
| 432 | |
| 433 | // Print out constants referenced by the function |
| 434 | EmitConstantPool(MF.getConstantPool()); |
| 435 | |
| 436 | // Print out labels for the function. |
| 437 | const Function *F = MF.getFunction(); |
| 438 | |
| 439 | SwitchToSection(TAI->SectionForGlobal(F)); |
| 440 | EmitAlignment(3, F); |
| 441 | |
| 442 | switch (F->getLinkage()) { |
| 443 | default: assert(0 && "Unknown linkage type!"); |
Rafael Espindola | bb46f52 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 444 | case Function::PrivateLinkage: |
Scott Michel | 73655bc | 2008-11-08 18:59:02 +0000 | [diff] [blame] | 445 | case Function::InternalLinkage: // Symbols default to internal. |
| 446 | break; |
| 447 | case Function::ExternalLinkage: |
| 448 | O << "\t.global\t" << CurrentFnName << "\n" |
| 449 | << "\t.type\t" << CurrentFnName << ", @function\n"; |
| 450 | break; |
| 451 | case Function::WeakLinkage: |
| 452 | case Function::LinkOnceLinkage: |
| 453 | O << "\t.global\t" << CurrentFnName << "\n"; |
| 454 | O << "\t.weak_definition\t" << CurrentFnName << "\n"; |
| 455 | break; |
| 456 | } |
| 457 | O << CurrentFnName << ":\n"; |
| 458 | |
| 459 | // Emit pre-function debug information. |
Devang Patel | eb3fc28 | 2009-01-08 23:40:34 +0000 | [diff] [blame] | 460 | DW->BeginFunction(&MF); |
Scott Michel | 73655bc | 2008-11-08 18:59:02 +0000 | [diff] [blame] | 461 | |
| 462 | // Print out code for the function. |
| 463 | for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); |
| 464 | I != E; ++I) { |
| 465 | // Print a label for the basic block. |
| 466 | if (I != MF.begin()) { |
| 467 | printBasicBlockLabel(I, true, true); |
| 468 | O << '\n'; |
| 469 | } |
| 470 | for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end(); |
| 471 | II != E; ++II) { |
| 472 | // Print the assembly for the instruction. |
| 473 | printMachineInstruction(II); |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | O << "\t.size\t" << CurrentFnName << ",.-" << CurrentFnName << "\n"; |
| 478 | |
| 479 | // Print out jump tables referenced by the function. |
| 480 | EmitJumpTableInfo(MF.getJumpTableInfo(), MF); |
| 481 | |
| 482 | // Emit post-function debug information. |
Devang Patel | eb3fc28 | 2009-01-08 23:40:34 +0000 | [diff] [blame] | 483 | DW->EndFunction(&MF); |
Scott Michel | 73655bc | 2008-11-08 18:59:02 +0000 | [diff] [blame] | 484 | |
| 485 | // We didn't modify anything. |
| 486 | return false; |
| 487 | } |
| 488 | |
| 489 | |
| 490 | bool LinuxAsmPrinter::doInitialization(Module &M) { |
| 491 | bool Result = AsmPrinter::doInitialization(M); |
| 492 | SwitchToTextSection("\t.text"); |
| 493 | // Emit initial debug information. |
Devang Patel | eb3fc28 | 2009-01-08 23:40:34 +0000 | [diff] [blame] | 494 | DW = getAnalysisToUpdate<DwarfWriter>(); |
| 495 | assert(DW && "Dwarf Writer is not available"); |
Scott Michel | 73655bc | 2008-11-08 18:59:02 +0000 | [diff] [blame] | 496 | MMI = getAnalysisToUpdate<MachineModuleInfo>(); |
Devang Patel | eb3fc28 | 2009-01-08 23:40:34 +0000 | [diff] [blame] | 497 | DW->BeginModule(&M, MMI, O, this, TAI); |
Scott Michel | 73655bc | 2008-11-08 18:59:02 +0000 | [diff] [blame] | 498 | return Result; |
| 499 | } |
| 500 | |
| 501 | /// PrintUnmangledNameSafely - Print out the printable characters in the name. |
| 502 | /// Don't print things like \n or \0. |
| 503 | static void PrintUnmangledNameSafely(const Value *V, raw_ostream &OS) { |
| 504 | for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen(); |
| 505 | Name != E; ++Name) |
| 506 | if (isprint(*Name)) |
| 507 | OS << *Name; |
| 508 | } |
| 509 | |
| 510 | /*! |
| 511 | Emit a global variable according to its section, alignment, etc. |
| 512 | |
| 513 | \note This code was shamelessly copied from the PowerPC's assembly printer, |
| 514 | which sort of screams for some kind of refactorization of common code. |
| 515 | */ |
| 516 | void LinuxAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) { |
| 517 | const TargetData *TD = TM.getTargetData(); |
| 518 | |
| 519 | if (!GVar->hasInitializer()) |
| 520 | return; |
| 521 | |
| 522 | // Check to see if this is a special global used by LLVM, if so, emit it. |
| 523 | if (EmitSpecialLLVMGlobal(GVar)) |
| 524 | return; |
| 525 | |
| 526 | std::string name = Mang->getValueName(GVar); |
| 527 | |
| 528 | printVisibility(name, GVar->getVisibility()); |
| 529 | |
| 530 | Constant *C = GVar->getInitializer(); |
| 531 | const Type *Type = C->getType(); |
Duncan Sands | ceb4d1a | 2009-01-12 20:38:59 +0000 | [diff] [blame] | 532 | unsigned Size = TD->getTypePaddedSize(Type); |
Scott Michel | 73655bc | 2008-11-08 18:59:02 +0000 | [diff] [blame] | 533 | unsigned Align = TD->getPreferredAlignmentLog(GVar); |
| 534 | |
| 535 | SwitchToSection(TAI->SectionForGlobal(GVar)); |
| 536 | |
| 537 | if (C->isNullValue() && /* FIXME: Verify correct */ |
| 538 | !GVar->hasSection() && |
Rafael Espindola | bb46f52 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 539 | (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() || |
Scott Michel | 73655bc | 2008-11-08 18:59:02 +0000 | [diff] [blame] | 540 | GVar->mayBeOverridden())) { |
| 541 | if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it. |
| 542 | |
| 543 | if (GVar->hasExternalLinkage()) { |
| 544 | O << "\t.global " << name << '\n'; |
| 545 | O << "\t.type " << name << ", @object\n"; |
| 546 | O << name << ":\n"; |
| 547 | O << "\t.zero " << Size << '\n'; |
Rafael Espindola | bb46f52 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 548 | } else if (GVar->hasLocalLinkage()) { |
Scott Michel | 73655bc | 2008-11-08 18:59:02 +0000 | [diff] [blame] | 549 | O << TAI->getLCOMMDirective() << name << ',' << Size; |
| 550 | } else { |
| 551 | O << ".comm " << name << ',' << Size; |
| 552 | } |
| 553 | O << "\t\t" << TAI->getCommentString() << " '"; |
| 554 | PrintUnmangledNameSafely(GVar, O); |
| 555 | O << "'\n"; |
| 556 | return; |
| 557 | } |
| 558 | |
| 559 | switch (GVar->getLinkage()) { |
| 560 | // Should never be seen for the CellSPU platform... |
| 561 | case GlobalValue::LinkOnceLinkage: |
| 562 | case GlobalValue::WeakLinkage: |
| 563 | case GlobalValue::CommonLinkage: |
| 564 | O << "\t.global " << name << '\n' |
| 565 | << "\t.type " << name << ", @object\n" |
| 566 | << "\t.weak " << name << '\n'; |
| 567 | break; |
| 568 | case GlobalValue::AppendingLinkage: |
| 569 | // FIXME: appending linkage variables should go into a section of |
| 570 | // their name or something. For now, just emit them as external. |
| 571 | case GlobalValue::ExternalLinkage: |
| 572 | // If external or appending, declare as a global symbol |
| 573 | O << "\t.global " << name << '\n' |
| 574 | << "\t.type " << name << ", @object\n"; |
| 575 | // FALL THROUGH |
Rafael Espindola | bb46f52 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 576 | case GlobalValue::PrivateLinkage: |
Scott Michel | 73655bc | 2008-11-08 18:59:02 +0000 | [diff] [blame] | 577 | case GlobalValue::InternalLinkage: |
| 578 | break; |
| 579 | default: |
| 580 | cerr << "Unknown linkage type!"; |
| 581 | abort(); |
| 582 | } |
| 583 | |
| 584 | EmitAlignment(Align, GVar); |
| 585 | O << name << ":\t\t\t\t" << TAI->getCommentString() << " '"; |
| 586 | PrintUnmangledNameSafely(GVar, O); |
| 587 | O << "'\n"; |
| 588 | |
| 589 | // If the initializer is a extern weak symbol, remember to emit the weak |
| 590 | // reference! |
| 591 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(C)) |
| 592 | if (GV->hasExternalWeakLinkage()) |
| 593 | ExtWeakSymbols.insert(GV); |
| 594 | |
| 595 | EmitGlobalConstant(C); |
| 596 | O << '\n'; |
| 597 | } |
| 598 | |
| 599 | bool LinuxAsmPrinter::doFinalization(Module &M) { |
| 600 | // Print out module-level global variables here. |
| 601 | for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); |
| 602 | I != E; ++I) |
| 603 | printModuleLevelGV(I); |
| 604 | |
| 605 | // TODO |
| 606 | |
| 607 | // Emit initial debug information. |
Devang Patel | eb3fc28 | 2009-01-08 23:40:34 +0000 | [diff] [blame] | 608 | DW->EndModule(); |
Scott Michel | 73655bc | 2008-11-08 18:59:02 +0000 | [diff] [blame] | 609 | |
| 610 | return AsmPrinter::doFinalization(M); |
| 611 | } |
| 612 | |
| 613 | /// createSPUCodePrinterPass - Returns a pass that prints the Cell SPU |
| 614 | /// assembly code for a MachineFunction to the given output stream, in a format |
| 615 | /// that the Linux SPU assembler can deal with. |
| 616 | /// |
| 617 | FunctionPass *llvm::createSPUAsmPrinterPass(raw_ostream &o, |
| 618 | SPUTargetMachine &tm) { |
| 619 | return new LinuxAsmPrinter(o, tm, tm.getTargetAsmInfo()); |
| 620 | } |