1 //===-- SPUAsmPrinter.cpp - Print machine instrs to Cell SPU assembly -------=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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'.
14 //===----------------------------------------------------------------------===//
16 #define DEBUG_TYPE "asmprinter"
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"
44 STATISTIC(EmittedInsts, "Number of machine instrs printed");
46 const std::string bss_section(".bss");
48 struct VISIBILITY_HIDDEN SPUAsmPrinter : public AsmPrinter {
49 std::set<std::string> FnStubs, GVStubs;
51 SPUAsmPrinter(raw_ostream &O, TargetMachine &TM, const TargetAsmInfo *T) :
56 virtual const char *getPassName() const {
57 return "STI CBEA SPU Assembly Printer";
60 SPUTargetMachine &getTM() {
61 return static_cast<SPUTargetMachine&>(TM);
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
68 bool printInstruction(const MachineInstr *MI);
70 void printMachineInstruction(const MachineInstr *MI);
71 void printOp(const MachineOperand &MO);
73 /// printRegister - Print register according to target requirements.
75 void printRegister(const MachineOperand &MO, bool R0AsZero) {
76 unsigned RegNo = MO.getReg();
77 assert(TargetRegisterInfo::isPhysicalRegister(RegNo) &&
79 O << TM.getRegisterInfo()->get(RegNo).AsmName;
82 void printOperand(const MachineInstr *MI, unsigned OpNo) {
83 const MachineOperand &MO = MI->getOperand(OpNo);
85 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
86 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
87 } else if (MO.isImm()) {
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);
101 printS7ImmOperand(const MachineInstr *MI, unsigned OpNo)
103 int value = MI->getOperand(OpNo).getImm();
104 value = (value << (32 - 7)) >> (32 - 7);
106 assert((value >= -(1 << 8) && value <= (1 << 7) - 1)
107 && "Invalid s7 argument");
112 printU7ImmOperand(const MachineInstr *MI, unsigned OpNo)
114 unsigned int value = MI->getOperand(OpNo).getImm();
115 assert(value < (1 << 8) && "Invalid u7 argument");
120 printMemRegImmS7(const MachineInstr *MI, unsigned OpNo)
122 char value = MI->getOperand(OpNo).getImm();
125 printOperand(MI, OpNo+1);
130 printS16ImmOperand(const MachineInstr *MI, unsigned OpNo)
132 O << (short) MI->getOperand(OpNo).getImm();
136 printU16ImmOperand(const MachineInstr *MI, unsigned OpNo)
138 O << (unsigned short)MI->getOperand(OpNo).getImm();
142 printU32ImmOperand(const MachineInstr *MI, unsigned OpNo)
144 O << (unsigned)MI->getOperand(OpNo).getImm();
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;
155 printOperand(MI, OpNo+1);
159 printU18ImmOperand(const MachineInstr *MI, unsigned OpNo)
161 unsigned int value = MI->getOperand(OpNo).getImm();
162 assert(value <= (1 << 19) - 1 && "Invalid u18 argument");
167 printS10ImmOperand(const MachineInstr *MI, unsigned OpNo)
169 short value = (short) (((int) MI->getOperand(OpNo).getImm() << 16)
171 assert((value >= -(1 << 9) && value <= (1 << 9) - 1)
172 && "Invalid s10 argument");
177 printU10ImmOperand(const MachineInstr *MI, unsigned OpNo)
179 short value = (short) (((int) MI->getOperand(OpNo).getImm() << 16)
181 assert((value <= (1 << 10) - 1) && "Invalid u10 argument");
186 printMemRegImmS10(const MachineInstr *MI, unsigned OpNo)
188 const MachineOperand &MO = MI->getOperand(OpNo);
190 "printMemRegImmS10 first operand is not immedate");
191 int64_t value = int64_t(MI->getOperand(OpNo).getImm());
192 assert((value >= -(1 << (9+4)) && value <= (1 << (9+4)) - 1)
193 && "Invalid dform s10 offset argument");
195 printOperand(MI, OpNo+1);
200 printAddr256K(const MachineInstr *MI, unsigned OpNo)
202 /* Note: operand 1 is an offset or symbol name. */
203 if (MI->getOperand(OpNo).isImm()) {
204 printS16ImmOperand(MI, OpNo);
206 printOp(MI->getOperand(OpNo));
207 if (MI->getOperand(OpNo+1).isImm()) {
208 int displ = int(MI->getOperand(OpNo+1).getImm());
217 void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
218 printOp(MI->getOperand(OpNo));
221 void printPCRelativeOperand(const MachineInstr *MI, unsigned OpNo) {
222 printOp(MI->getOperand(OpNo));
226 void printSymbolHi(const MachineInstr *MI, unsigned OpNo) {
227 if (MI->getOperand(OpNo).isImm()) {
228 printS16ImmOperand(MI, OpNo);
230 printOp(MI->getOperand(OpNo));
235 void printSymbolLo(const MachineInstr *MI, unsigned OpNo) {
236 if (MI->getOperand(OpNo).isImm()) {
237 printS16ImmOperand(MI, OpNo);
239 printOp(MI->getOperand(OpNo));
244 /// Print local store address
245 void printSymbolLSA(const MachineInstr *MI, unsigned OpNo) {
246 printOp(MI->getOperand(OpNo));
249 void printROTHNeg7Imm(const MachineInstr *MI, unsigned OpNo) {
250 if (MI->getOperand(OpNo).isImm()) {
251 int value = (int) MI->getOperand(OpNo).getImm();
252 assert((value >= 0 && value < 16)
253 && "Invalid negated immediate rotate 7-bit argument");
256 assert(0 &&"Invalid/non-immediate rotate amount in printRotateNeg7Imm");
260 void printROTNeg7Imm(const MachineInstr *MI, unsigned OpNo) {
261 if (MI->getOperand(OpNo).isImm()) {
262 int value = (int) MI->getOperand(OpNo).getImm();
263 assert((value >= 0 && value < 32)
264 && "Invalid negated immediate rotate 7-bit argument");
267 assert(0 &&"Invalid/non-immediate rotate amount in printRotateNeg7Imm");
271 virtual bool runOnMachineFunction(MachineFunction &F) = 0;
272 virtual bool doFinalization(Module &M) = 0;
275 /// LinuxAsmPrinter - SPU assembly printer, customized for Linux
276 struct VISIBILITY_HIDDEN LinuxAsmPrinter : public SPUAsmPrinter {
279 MachineModuleInfo *MMI;
281 LinuxAsmPrinter(raw_ostream &O, SPUTargetMachine &TM,
282 const TargetAsmInfo *T) :
283 SPUAsmPrinter(O, TM, T),
288 virtual const char *getPassName() const {
289 return "STI CBEA SPU Assembly Printer";
292 bool runOnMachineFunction(MachineFunction &F);
293 bool doInitialization(Module &M);
294 bool doFinalization(Module &M);
296 void getAnalysisUsage(AnalysisUsage &AU) const {
297 AU.setPreservesAll();
298 AU.addRequired<MachineModuleInfo>();
299 SPUAsmPrinter::getAnalysisUsage(AU);
303 } // end of anonymous namespace
305 // Include the auto-generated portion of the assembly writer
306 #include "SPUGenAsmWriter.inc"
308 void SPUAsmPrinter::printOp(const MachineOperand &MO) {
309 switch (MO.getType()) {
310 case MachineOperand::MO_Immediate:
311 cerr << "printOp() does not handle immediate values\n";
315 case MachineOperand::MO_MachineBasicBlock:
316 printBasicBlockLabel(MO.getMBB());
318 case MachineOperand::MO_JumpTableIndex:
319 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
320 << '_' << MO.getIndex();
322 case MachineOperand::MO_ConstantPoolIndex:
323 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
324 << '_' << MO.getIndex();
326 case MachineOperand::MO_ExternalSymbol:
327 // Computing the address of an external symbol, not calling it.
328 if (TM.getRelocationModel() != Reloc::Static) {
329 std::string Name(TAI->getGlobalPrefix()); Name += MO.getSymbolName();
330 GVStubs.insert(Name);
331 O << "L" << Name << "$non_lazy_ptr";
334 O << TAI->getGlobalPrefix() << MO.getSymbolName();
336 case MachineOperand::MO_GlobalAddress: {
337 // Computing the address of a global symbol, not calling it.
338 GlobalValue *GV = MO.getGlobal();
339 std::string Name = Mang->getValueName(GV);
341 // External or weakly linked global variables need non-lazily-resolved
343 if (TM.getRelocationModel() != Reloc::Static) {
344 if (((GV->isDeclaration() || GV->hasWeakLinkage() ||
345 GV->hasLinkOnceLinkage() || GV->hasCommonLinkage()))) {
346 GVStubs.insert(Name);
347 O << "L" << Name << "$non_lazy_ptr";
353 if (GV->hasExternalWeakLinkage())
354 ExtWeakSymbols.insert(GV);
359 O << "<unknown operand type: " << MO.getType() << ">";
364 /// PrintAsmOperand - Print out an operand for an inline asm expression.
366 bool SPUAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
368 const char *ExtraCode) {
369 // Does this asm operand have a single letter operand modifier?
370 if (ExtraCode && ExtraCode[0]) {
371 if (ExtraCode[1] != 0) return true; // Unknown modifier.
373 switch (ExtraCode[0]) {
374 default: return true; // Unknown modifier.
375 case 'L': // Write second word of DImode reference.
376 // Verify that this operand has two consecutive registers.
377 if (!MI->getOperand(OpNo).isReg() ||
378 OpNo+1 == MI->getNumOperands() ||
379 !MI->getOperand(OpNo+1).isReg())
381 ++OpNo; // Return the high-part.
386 printOperand(MI, OpNo);
390 bool SPUAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
393 const char *ExtraCode) {
394 if (ExtraCode && ExtraCode[0])
395 return true; // Unknown modifier.
396 printMemRegReg(MI, OpNo);
400 /// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax
401 /// to the current output stream.
403 void SPUAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
405 printInstruction(MI);
408 /// runOnMachineFunction - This uses the printMachineInstruction()
409 /// method to print assembly for each instruction.
412 LinuxAsmPrinter::runOnMachineFunction(MachineFunction &MF)
414 SetupMachineFunction(MF);
417 // Print out constants referenced by the function
418 EmitConstantPool(MF.getConstantPool());
420 // Print out labels for the function.
421 const Function *F = MF.getFunction();
423 SwitchToSection(TAI->SectionForGlobal(F));
426 switch (F->getLinkage()) {
427 default: assert(0 && "Unknown linkage type!");
428 case Function::InternalLinkage: // Symbols default to internal.
430 case Function::ExternalLinkage:
431 O << "\t.global\t" << CurrentFnName << "\n"
432 << "\t.type\t" << CurrentFnName << ", @function\n";
434 case Function::WeakLinkage:
435 case Function::LinkOnceLinkage:
436 O << "\t.global\t" << CurrentFnName << "\n";
437 O << "\t.weak_definition\t" << CurrentFnName << "\n";
440 O << CurrentFnName << ":\n";
442 // Emit pre-function debug information.
443 DW.BeginFunction(&MF);
445 // Print out code for the function.
446 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
448 // Print a label for the basic block.
449 if (I != MF.begin()) {
450 printBasicBlockLabel(I, true, true);
453 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
455 // Print the assembly for the instruction.
456 printMachineInstruction(II);
460 O << "\t.size\t" << CurrentFnName << ",.-" << CurrentFnName << "\n";
462 // Print out jump tables referenced by the function.
463 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
465 // Emit post-function debug information.
468 // We didn't modify anything.
473 bool LinuxAsmPrinter::doInitialization(Module &M) {
474 bool Result = AsmPrinter::doInitialization(M);
475 SwitchToTextSection("\t.text");
476 // Emit initial debug information.
478 MMI = getAnalysisToUpdate<MachineModuleInfo>();
479 DW.SetModuleInfo(MMI);
483 bool LinuxAsmPrinter::doFinalization(Module &M) {
484 const TargetData *TD = TM.getTargetData();
486 // Print out module-level global variables here.
487 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
489 if (!I->hasInitializer()) continue; // External global require no code
491 // Check to see if this is a special global used by LLVM, if so, emit it.
492 if (EmitSpecialLLVMGlobal(I))
495 std::string name = Mang->getValueName(I);
496 Constant *C = I->getInitializer();
497 unsigned Size = TD->getTypeStoreSize(C->getType());
498 unsigned Align = TD->getPreferredAlignmentLog(I);
500 if (C->isNullValue() && /* FIXME: Verify correct */
501 (I->hasInternalLinkage() || I->hasWeakLinkage() ||
502 I->hasLinkOnceLinkage() || I->hasCommonLinkage() ||
503 (I->hasExternalLinkage() && !I->hasSection()))) {
504 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
505 if (I->hasExternalLinkage()) {
506 // External linkage globals -> .bss section
507 // FIXME: Want to set the global variable's section so that
508 // SwitchToDataSection emits the ".section" directive
509 SwitchToDataSection("\t.section\t.bss", I);
510 O << "\t.global\t" << name << '\n';
511 O << "\t.align\t" << Align << '\n';
512 O << "\t.type\t" << name << ", @object\n";
513 O << "\t.size\t" << name << ", " << Size << '\n';
515 O << "\t.zero\t" << Size;
516 } else if (I->hasInternalLinkage()) {
517 SwitchToDataSection("\t.data", I);
518 O << ".local " << name << "\n";
519 O << TAI->getCOMMDirective() << name << "," << Size << "," << Align << "\n";
521 SwitchToDataSection("\t.data", I);
522 O << ".comm " << name << "," << Size;
524 O << "\t\t# '" << I->getName() << "'\n";
526 switch (I->getLinkage()) {
527 case GlobalValue::LinkOnceLinkage:
528 case GlobalValue::WeakLinkage:
529 case GlobalValue::CommonLinkage:
530 O << "\t.global " << name << '\n'
531 << "\t.weak_definition " << name << '\n';
532 SwitchToDataSection(".section __DATA,__datacoal_nt,coalesced", I);
534 case GlobalValue::AppendingLinkage:
535 // FIXME: appending linkage variables should go into a section of
536 // their name or something. For now, just emit them as external.
537 case GlobalValue::ExternalLinkage:
538 // If external or appending, declare as a global symbol
539 O << "\t.global " << name << "\n";
541 case GlobalValue::InternalLinkage:
542 if (I->isConstant()) {
543 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
544 if (TAI->getCStringSection() && CVA && CVA->isCString()) {
545 SwitchToDataSection("\t.cstring", I);
550 SwitchToDataSection("\t.data", I);
553 cerr << "Unknown linkage type!";
557 EmitAlignment(Align, I);
558 O << name << ":\t\t\t\t# '" << I->getName() << "'\n";
560 // If the initializer is a extern weak symbol, remember to emit the weak
562 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
563 if (GV->hasExternalWeakLinkage())
564 ExtWeakSymbols.insert(GV);
566 EmitGlobalConstant(C);
571 // Output stubs for dynamically-linked functions
572 if (TM.getRelocationModel() == Reloc::PIC_) {
573 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
575 SwitchToTextSection(".section __TEXT,__picsymbolstub1,symbol_stubs,"
576 "pure_instructions,32");
578 O << "L" << *i << "$stub:\n";
579 O << "\t.indirect_symbol " << *i << "\n";
581 O << "\tbcl 20,31,L0$" << *i << "\n";
582 O << "L0$" << *i << ":\n";
584 O << "\taddis r11,r11,ha16(L" << *i << "$lazy_ptr-L0$" << *i << ")\n";
586 O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr-L0$" << *i << ")(r11)\n";
587 O << "\tmtctr r12\n";
589 SwitchToDataSection(".lazy_symbol_pointer");
590 O << "L" << *i << "$lazy_ptr:\n";
591 O << "\t.indirect_symbol " << *i << "\n";
592 O << "\t.long dyld_stub_binding_helper\n";
595 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
597 SwitchToTextSection(".section __TEXT,__symbol_stub1,symbol_stubs,"
598 "pure_instructions,16");
600 O << "L" << *i << "$stub:\n";
601 O << "\t.indirect_symbol " << *i << "\n";
602 O << "\tlis r11,ha16(L" << *i << "$lazy_ptr)\n";
603 O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr)(r11)\n";
604 O << "\tmtctr r12\n";
606 SwitchToDataSection(".lazy_symbol_pointer");
607 O << "L" << *i << "$lazy_ptr:\n";
608 O << "\t.indirect_symbol " << *i << "\n";
609 O << "\t.long dyld_stub_binding_helper\n";
615 // Output stubs for external and common global variables.
616 if (GVStubs.begin() != GVStubs.end()) {
617 SwitchToDataSection(".non_lazy_symbol_pointer");
618 for (std::set<std::string>::iterator I = GVStubs.begin(),
619 E = GVStubs.end(); I != E; ++I) {
620 O << "L" << *I << "$non_lazy_ptr:\n";
621 O << "\t.indirect_symbol " << *I << "\n";
626 // Emit initial debug information.
629 // Emit ident information
630 O << "\t.ident\t\"(llvm 2.2+) STI CBEA Cell SPU backend\"\n";
632 return AsmPrinter::doFinalization(M);
637 /// createSPUCodePrinterPass - Returns a pass that prints the Cell SPU
638 /// assembly code for a MachineFunction to the given output stream, in a format
639 /// that the Linux SPU assembler can deal with.
641 FunctionPass *llvm::createSPUAsmPrinterPass(raw_ostream &o,
642 SPUTargetMachine &tm) {
643 return new LinuxAsmPrinter(o, tm, tm.getTargetAsmInfo());