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/MDNode.h"
23 #include "llvm/Assembly/Writer.h"
24 #include "llvm/CodeGen/AsmPrinter.h"
25 #include "llvm/CodeGen/DwarfWriter.h"
26 #include "llvm/CodeGen/MachineModuleInfo.h"
27 #include "llvm/CodeGen/MachineFunctionPass.h"
28 #include "llvm/CodeGen/MachineInstr.h"
29 #include "llvm/Support/Mangler.h"
30 #include "llvm/Support/MathExtras.h"
31 #include "llvm/Support/CommandLine.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/Support/Compiler.h"
34 #include "llvm/Support/raw_ostream.h"
35 #include "llvm/Target/TargetAsmInfo.h"
36 #include "llvm/Target/TargetRegisterInfo.h"
37 #include "llvm/Target/TargetInstrInfo.h"
38 #include "llvm/Target/TargetOptions.h"
39 #include "llvm/ADT/Statistic.h"
40 #include "llvm/ADT/StringExtras.h"
45 STATISTIC(EmittedInsts, "Number of machine instrs printed");
47 const std::string bss_section(".bss");
49 class VISIBILITY_HIDDEN SPUAsmPrinter : public AsmPrinter {
50 std::set<std::string> FnStubs, GVStubs;
52 explicit SPUAsmPrinter(raw_ostream &O, TargetMachine &TM,
53 const TargetAsmInfo *T, CodeGenOpt::Level OL,
55 AsmPrinter(O, TM, T, OL, V) {}
57 virtual const char *getPassName() const {
58 return "STI CBEA SPU Assembly Printer";
61 SPUTargetMachine &getTM() {
62 return static_cast<SPUTargetMachine&>(TM);
65 /// printInstruction - This method is automatically generated by tablegen
66 /// from the instruction set description. This method returns true if the
67 /// machine instruction was sufficiently described to print it, otherwise it
69 bool printInstruction(const MachineInstr *MI);
71 void printMachineInstruction(const MachineInstr *MI);
72 void printOp(const MachineOperand &MO);
74 /// printRegister - Print register according to target requirements.
76 void printRegister(const MachineOperand &MO, bool R0AsZero) {
77 unsigned RegNo = MO.getReg();
78 assert(TargetRegisterInfo::isPhysicalRegister(RegNo) &&
80 O << TM.getRegisterInfo()->get(RegNo).AsmName;
83 void printOperand(const MachineInstr *MI, unsigned OpNo) {
84 const MachineOperand &MO = MI->getOperand(OpNo);
86 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
87 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
88 } else if (MO.isImm()) {
95 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
96 unsigned AsmVariant, const char *ExtraCode);
97 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
98 unsigned AsmVariant, const char *ExtraCode);
102 printS7ImmOperand(const MachineInstr *MI, unsigned OpNo)
104 int value = MI->getOperand(OpNo).getImm();
105 value = (value << (32 - 7)) >> (32 - 7);
107 assert((value >= -(1 << 8) && value <= (1 << 7) - 1)
108 && "Invalid s7 argument");
113 printU7ImmOperand(const MachineInstr *MI, unsigned OpNo)
115 unsigned int value = MI->getOperand(OpNo).getImm();
116 assert(value < (1 << 8) && "Invalid u7 argument");
121 printShufAddr(const MachineInstr *MI, unsigned OpNo)
123 char value = MI->getOperand(OpNo).getImm();
126 printOperand(MI, OpNo+1);
131 printS16ImmOperand(const MachineInstr *MI, unsigned OpNo)
133 O << (short) MI->getOperand(OpNo).getImm();
137 printU16ImmOperand(const MachineInstr *MI, unsigned OpNo)
139 O << (unsigned short)MI->getOperand(OpNo).getImm();
143 printU32ImmOperand(const MachineInstr *MI, unsigned OpNo)
145 O << (unsigned)MI->getOperand(OpNo).getImm();
149 printMemRegReg(const MachineInstr *MI, unsigned OpNo) {
150 // When used as the base register, r0 reads constant zero rather than
151 // the value contained in the register. For this reason, the darwin
152 // assembler requires that we print r0 as 0 (no r) when used as the base.
153 const MachineOperand &MO = MI->getOperand(OpNo);
154 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
156 printOperand(MI, OpNo+1);
160 printU18ImmOperand(const MachineInstr *MI, unsigned OpNo)
162 unsigned int value = MI->getOperand(OpNo).getImm();
163 assert(value <= (1 << 19) - 1 && "Invalid u18 argument");
168 printS10ImmOperand(const MachineInstr *MI, unsigned OpNo)
170 short value = (short) (((int) MI->getOperand(OpNo).getImm() << 16)
172 assert((value >= -(1 << 9) && value <= (1 << 9) - 1)
173 && "Invalid s10 argument");
178 printU10ImmOperand(const MachineInstr *MI, unsigned OpNo)
180 short value = (short) (((int) MI->getOperand(OpNo).getImm() << 16)
182 assert((value <= (1 << 10) - 1) && "Invalid u10 argument");
187 printDFormAddr(const MachineInstr *MI, unsigned OpNo)
189 assert(MI->getOperand(OpNo).isImm() &&
190 "printDFormAddr first operand is not immediate");
191 int64_t value = int64_t(MI->getOperand(OpNo).getImm());
192 int16_t value16 = int16_t(value);
193 assert((value16 >= -(1 << (9+4)) && value16 <= (1 << (9+4)) - 1)
194 && "Invalid dform s10 offset argument");
195 O << (value16 & ~0xf) << "(";
196 printOperand(MI, OpNo+1);
201 printAddr256K(const MachineInstr *MI, unsigned OpNo)
203 /* Note: operand 1 is an offset or symbol name. */
204 if (MI->getOperand(OpNo).isImm()) {
205 printS16ImmOperand(MI, OpNo);
207 printOp(MI->getOperand(OpNo));
208 if (MI->getOperand(OpNo+1).isImm()) {
209 int displ = int(MI->getOperand(OpNo+1).getImm());
218 void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
219 printOp(MI->getOperand(OpNo));
222 void printPCRelativeOperand(const MachineInstr *MI, unsigned OpNo) {
223 // Used to generate a ".-<target>", but it turns out that the assembler
224 // really wants the target.
226 // N.B.: This operand is used for call targets. Branch hints are another
228 printOp(MI->getOperand(OpNo));
231 void printHBROperand(const MachineInstr *MI, unsigned OpNo) {
232 // HBR operands are generated in front of branches, hence, the
233 // program counter plus the target.
235 printOp(MI->getOperand(OpNo));
238 void printSymbolHi(const MachineInstr *MI, unsigned OpNo) {
239 if (MI->getOperand(OpNo).isImm()) {
240 printS16ImmOperand(MI, OpNo);
242 printOp(MI->getOperand(OpNo));
247 void printSymbolLo(const MachineInstr *MI, unsigned OpNo) {
248 if (MI->getOperand(OpNo).isImm()) {
249 printS16ImmOperand(MI, OpNo);
251 printOp(MI->getOperand(OpNo));
256 /// Print local store address
257 void printSymbolLSA(const MachineInstr *MI, unsigned OpNo) {
258 printOp(MI->getOperand(OpNo));
261 void printROTHNeg7Imm(const MachineInstr *MI, unsigned OpNo) {
262 if (MI->getOperand(OpNo).isImm()) {
263 int value = (int) MI->getOperand(OpNo).getImm();
264 assert((value >= 0 && value < 16)
265 && "Invalid negated immediate rotate 7-bit argument");
268 assert(0 &&"Invalid/non-immediate rotate amount in printRotateNeg7Imm");
272 void printROTNeg7Imm(const MachineInstr *MI, unsigned OpNo) {
273 if (MI->getOperand(OpNo).isImm()) {
274 int value = (int) MI->getOperand(OpNo).getImm();
275 assert((value >= 0 && value <= 32)
276 && "Invalid negated immediate rotate 7-bit argument");
279 assert(0 &&"Invalid/non-immediate rotate amount in printRotateNeg7Imm");
283 virtual bool runOnMachineFunction(MachineFunction &F) = 0;
284 //! Assembly printer cleanup after function has been emitted
285 virtual bool doFinalization(Module &M) = 0;
288 /// LinuxAsmPrinter - SPU assembly printer, customized for Linux
289 class VISIBILITY_HIDDEN LinuxAsmPrinter : public SPUAsmPrinter {
292 explicit LinuxAsmPrinter(raw_ostream &O, SPUTargetMachine &TM,
293 const TargetAsmInfo *T, CodeGenOpt::Level F,
295 : SPUAsmPrinter(O, TM, T, F, V), DW(0) {}
297 virtual const char *getPassName() const {
298 return "STI CBEA SPU Assembly Printer";
301 bool runOnMachineFunction(MachineFunction &F);
302 bool doInitialization(Module &M);
303 //! Dump globals, perform cleanup after function emission
304 bool doFinalization(Module &M);
306 void getAnalysisUsage(AnalysisUsage &AU) const {
307 AU.setPreservesAll();
308 AU.addRequired<MachineModuleInfo>();
309 AU.addRequired<DwarfWriter>();
310 SPUAsmPrinter::getAnalysisUsage(AU);
313 //! Emit a global variable according to its section and type
314 void printModuleLevelGV(const GlobalVariable* GVar);
316 } // end of anonymous namespace
318 // Include the auto-generated portion of the assembly writer
319 #include "SPUGenAsmWriter.inc"
321 void SPUAsmPrinter::printOp(const MachineOperand &MO) {
322 switch (MO.getType()) {
323 case MachineOperand::MO_Immediate:
324 cerr << "printOp() does not handle immediate values\n";
328 case MachineOperand::MO_MachineBasicBlock:
329 printBasicBlockLabel(MO.getMBB());
331 case MachineOperand::MO_JumpTableIndex:
332 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
333 << '_' << MO.getIndex();
335 case MachineOperand::MO_ConstantPoolIndex:
336 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
337 << '_' << MO.getIndex();
339 case MachineOperand::MO_ExternalSymbol:
340 // Computing the address of an external symbol, not calling it.
341 if (TM.getRelocationModel() != Reloc::Static) {
342 std::string Name(TAI->getGlobalPrefix()); Name += MO.getSymbolName();
343 GVStubs.insert(Name);
344 O << "L" << Name << "$non_lazy_ptr";
347 O << TAI->getGlobalPrefix() << MO.getSymbolName();
349 case MachineOperand::MO_GlobalAddress: {
350 // Computing the address of a global symbol, not calling it.
351 GlobalValue *GV = MO.getGlobal();
352 std::string Name = Mang->getValueName(GV);
354 // External or weakly linked global variables need non-lazily-resolved
356 if (TM.getRelocationModel() != Reloc::Static) {
357 if (((GV->isDeclaration() || GV->hasWeakLinkage() ||
358 GV->hasLinkOnceLinkage() || GV->hasCommonLinkage()))) {
359 GVStubs.insert(Name);
360 O << "L" << Name << "$non_lazy_ptr";
369 O << "<unknown operand type: " << MO.getType() << ">";
374 /// PrintAsmOperand - Print out an operand for an inline asm expression.
376 bool SPUAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
378 const char *ExtraCode) {
379 // Does this asm operand have a single letter operand modifier?
380 if (ExtraCode && ExtraCode[0]) {
381 if (ExtraCode[1] != 0) return true; // Unknown modifier.
383 switch (ExtraCode[0]) {
384 default: return true; // Unknown modifier.
385 case 'L': // Write second word of DImode reference.
386 // Verify that this operand has two consecutive registers.
387 if (!MI->getOperand(OpNo).isReg() ||
388 OpNo+1 == MI->getNumOperands() ||
389 !MI->getOperand(OpNo+1).isReg())
391 ++OpNo; // Return the high-part.
396 printOperand(MI, OpNo);
400 bool SPUAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
403 const char *ExtraCode) {
404 if (ExtraCode && ExtraCode[0])
405 return true; // Unknown modifier.
406 printMemRegReg(MI, OpNo);
410 /// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax
411 /// to the current output stream.
413 void SPUAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
415 printInstruction(MI);
418 /// runOnMachineFunction - This uses the printMachineInstruction()
419 /// method to print assembly for each instruction.
422 LinuxAsmPrinter::runOnMachineFunction(MachineFunction &MF)
426 SetupMachineFunction(MF);
429 // Print out constants referenced by the function
430 EmitConstantPool(MF.getConstantPool());
432 // Print out labels for the function.
433 const Function *F = MF.getFunction();
435 SwitchToSection(TAI->SectionForGlobal(F));
438 switch (F->getLinkage()) {
439 default: assert(0 && "Unknown linkage type!");
440 case Function::PrivateLinkage:
441 case Function::InternalLinkage: // Symbols default to internal.
443 case Function::ExternalLinkage:
444 O << "\t.global\t" << CurrentFnName << "\n"
445 << "\t.type\t" << CurrentFnName << ", @function\n";
447 case Function::WeakAnyLinkage:
448 case Function::WeakODRLinkage:
449 case Function::LinkOnceAnyLinkage:
450 case Function::LinkOnceODRLinkage:
451 O << "\t.global\t" << CurrentFnName << "\n";
452 O << "\t.weak_definition\t" << CurrentFnName << "\n";
455 O << CurrentFnName << ":\n";
457 // Emit pre-function debug information.
458 DW->BeginFunction(&MF);
460 // Print out code for the function.
461 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
463 // Print a label for the basic block.
464 if (I != MF.begin()) {
465 printBasicBlockLabel(I, true, true);
468 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
470 // Print the assembly for the instruction.
471 printMachineInstruction(II);
475 O << "\t.size\t" << CurrentFnName << ",.-" << CurrentFnName << "\n";
477 // Print out jump tables referenced by the function.
478 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
480 // Emit post-function debug information.
481 DW->EndFunction(&MF);
483 // We didn't modify anything.
488 bool LinuxAsmPrinter::doInitialization(Module &M) {
489 bool Result = AsmPrinter::doInitialization(M);
490 DW = getAnalysisIfAvailable<DwarfWriter>();
491 SwitchToTextSection("\t.text");
495 /// PrintUnmangledNameSafely - Print out the printable characters in the name.
496 /// Don't print things like \\n or \\0.
497 static void PrintUnmangledNameSafely(const Value *V, raw_ostream &OS) {
498 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
505 Emit a global variable according to its section, alignment, etc.
507 \note This code was shamelessly copied from the PowerPC's assembly printer,
508 which sort of screams for some kind of refactorization of common code.
510 void LinuxAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
511 const TargetData *TD = TM.getTargetData();
513 if (!GVar->hasInitializer())
516 // Check to see if this is a special global used by LLVM, if so, emit it.
517 if (EmitSpecialLLVMGlobal(GVar))
520 std::string name = Mang->getValueName(GVar);
522 printVisibility(name, GVar->getVisibility());
524 Constant *C = GVar->getInitializer();
527 const Type *Type = C->getType();
528 unsigned Size = TD->getTypeAllocSize(Type);
529 unsigned Align = TD->getPreferredAlignmentLog(GVar);
531 SwitchToSection(TAI->SectionForGlobal(GVar));
533 if (C->isNullValue() && /* FIXME: Verify correct */
534 !GVar->hasSection() &&
535 (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() ||
536 GVar->isWeakForLinker())) {
537 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
539 if (GVar->hasExternalLinkage()) {
540 O << "\t.global " << name << '\n';
541 O << "\t.type " << name << ", @object\n";
543 O << "\t.zero " << Size << '\n';
544 } else if (GVar->hasLocalLinkage()) {
545 O << TAI->getLCOMMDirective() << name << ',' << Size;
547 O << ".comm " << name << ',' << Size;
549 O << "\t\t" << TAI->getCommentString() << " '";
550 PrintUnmangledNameSafely(GVar, O);
555 switch (GVar->getLinkage()) {
556 // Should never be seen for the CellSPU platform...
557 case GlobalValue::LinkOnceAnyLinkage:
558 case GlobalValue::LinkOnceODRLinkage:
559 case GlobalValue::WeakAnyLinkage:
560 case GlobalValue::WeakODRLinkage:
561 case GlobalValue::CommonLinkage:
562 O << "\t.global " << name << '\n'
563 << "\t.type " << name << ", @object\n"
564 << "\t.weak " << name << '\n';
566 case GlobalValue::AppendingLinkage:
567 // FIXME: appending linkage variables should go into a section of
568 // their name or something. For now, just emit them as external.
569 case GlobalValue::ExternalLinkage:
570 // If external or appending, declare as a global symbol
571 O << "\t.global " << name << '\n'
572 << "\t.type " << name << ", @object\n";
574 case GlobalValue::PrivateLinkage:
575 case GlobalValue::InternalLinkage:
578 cerr << "Unknown linkage type!";
582 EmitAlignment(Align, GVar);
583 O << name << ":\t\t\t\t" << TAI->getCommentString() << " '";
584 PrintUnmangledNameSafely(GVar, O);
587 EmitGlobalConstant(C);
591 bool LinuxAsmPrinter::doFinalization(Module &M) {
592 // Print out module-level global variables here.
593 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
595 printModuleLevelGV(I);
597 return AsmPrinter::doFinalization(M);
600 /// createSPUCodePrinterPass - Returns a pass that prints the Cell SPU
601 /// assembly code for a MachineFunction to the given output stream, in a format
602 /// that the Linux SPU assembler can deal with.
604 FunctionPass *llvm::createSPUAsmPrinterPass(raw_ostream &o,
605 SPUTargetMachine &tm,
606 CodeGenOpt::Level OptLevel,
608 return new LinuxAsmPrinter(o, tm, tm.getTargetAsmInfo(), OptLevel, verbose);
611 // Force static initialization.
612 extern "C" void LLVMInitializeCellSPUAsmPrinter() { }
615 static struct Register {
617 SPUTargetMachine::registerAsmPrinter(createSPUAsmPrinterPass);