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/MC/MCStreamer.h"
29 #include "llvm/MC/MCAsmInfo.h"
30 #include "llvm/MC/MCSymbol.h"
31 #include "llvm/Target/TargetLoweringObjectFile.h"
32 #include "llvm/Target/TargetInstrInfo.h"
33 #include "llvm/Target/TargetOptions.h"
34 #include "llvm/Target/TargetRegisterInfo.h"
35 #include "llvm/Target/TargetRegistry.h"
36 #include "llvm/ADT/Statistic.h"
37 #include "llvm/ADT/StringExtras.h"
38 #include "llvm/Support/CommandLine.h"
39 #include "llvm/Support/Debug.h"
40 #include "llvm/Support/ErrorHandling.h"
41 #include "llvm/Support/FormattedStream.h"
42 #include "llvm/Support/MathExtras.h"
46 STATISTIC(EmittedInsts, "Number of machine instrs printed");
48 const std::string bss_section(".bss");
50 class SPUAsmPrinter : public AsmPrinter {
52 explicit SPUAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
53 const MCAsmInfo *T, bool V) :
54 AsmPrinter(O, TM, T, V) {}
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.
66 void printInstruction(const MachineInstr *MI);
67 static const char *getRegisterName(unsigned RegNo);
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 << getRegisterName(RegNo);
82 void printOperand(const MachineInstr *MI, unsigned OpNo) {
83 const MachineOperand &MO = MI->getOperand(OpNo);
85 O << getRegisterName(MO.getReg());
86 } else if (MO.isImm()) {
93 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
94 unsigned AsmVariant, const char *ExtraCode);
95 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
96 unsigned AsmVariant, const char *ExtraCode);
100 printS7ImmOperand(const MachineInstr *MI, unsigned OpNo)
102 int value = MI->getOperand(OpNo).getImm();
103 value = (value << (32 - 7)) >> (32 - 7);
105 assert((value >= -(1 << 8) && value <= (1 << 7) - 1)
106 && "Invalid s7 argument");
111 printU7ImmOperand(const MachineInstr *MI, unsigned OpNo)
113 unsigned int value = MI->getOperand(OpNo).getImm();
114 assert(value < (1 << 8) && "Invalid u7 argument");
119 printShufAddr(const MachineInstr *MI, unsigned OpNo)
121 char value = MI->getOperand(OpNo).getImm();
124 printOperand(MI, OpNo+1);
129 printS16ImmOperand(const MachineInstr *MI, unsigned OpNo)
131 O << (short) MI->getOperand(OpNo).getImm();
135 printU16ImmOperand(const MachineInstr *MI, unsigned OpNo)
137 O << (unsigned short)MI->getOperand(OpNo).getImm();
141 printU32ImmOperand(const MachineInstr *MI, unsigned OpNo)
143 O << (unsigned)MI->getOperand(OpNo).getImm();
147 printMemRegReg(const MachineInstr *MI, unsigned OpNo) {
148 // When used as the base register, r0 reads constant zero rather than
149 // the value contained in the register. For this reason, the darwin
150 // assembler requires that we print r0 as 0 (no r) when used as the base.
151 const MachineOperand &MO = MI->getOperand(OpNo);
152 O << getRegisterName(MO.getReg()) << ", ";
153 printOperand(MI, OpNo+1);
157 printU18ImmOperand(const MachineInstr *MI, unsigned OpNo)
159 unsigned int value = MI->getOperand(OpNo).getImm();
160 assert(value <= (1 << 19) - 1 && "Invalid u18 argument");
165 printS10ImmOperand(const MachineInstr *MI, unsigned OpNo)
167 short value = (short) (((int) MI->getOperand(OpNo).getImm() << 16)
169 assert((value >= -(1 << 9) && value <= (1 << 9) - 1)
170 && "Invalid s10 argument");
175 printU10ImmOperand(const MachineInstr *MI, unsigned OpNo)
177 short value = (short) (((int) MI->getOperand(OpNo).getImm() << 16)
179 assert((value <= (1 << 10) - 1) && "Invalid u10 argument");
184 printDFormAddr(const MachineInstr *MI, unsigned OpNo)
186 assert(MI->getOperand(OpNo).isImm() &&
187 "printDFormAddr first operand is not immediate");
188 int64_t value = int64_t(MI->getOperand(OpNo).getImm());
189 int16_t value16 = int16_t(value);
190 assert((value16 >= -(1 << (9+4)) && value16 <= (1 << (9+4)) - 1)
191 && "Invalid dform s10 offset argument");
192 O << (value16 & ~0xf) << "(";
193 printOperand(MI, OpNo+1);
198 printAddr256K(const MachineInstr *MI, unsigned OpNo)
200 /* Note: operand 1 is an offset or symbol name. */
201 if (MI->getOperand(OpNo).isImm()) {
202 printS16ImmOperand(MI, OpNo);
204 printOp(MI->getOperand(OpNo));
205 if (MI->getOperand(OpNo+1).isImm()) {
206 int displ = int(MI->getOperand(OpNo+1).getImm());
215 void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
216 printOp(MI->getOperand(OpNo));
219 void printPCRelativeOperand(const MachineInstr *MI, unsigned OpNo) {
220 // Used to generate a ".-<target>", but it turns out that the assembler
221 // really wants the target.
223 // N.B.: This operand is used for call targets. Branch hints are another
225 printOp(MI->getOperand(OpNo));
228 void printHBROperand(const MachineInstr *MI, unsigned OpNo) {
229 // HBR operands are generated in front of branches, hence, the
230 // program counter plus the target.
232 printOp(MI->getOperand(OpNo));
235 void printSymbolHi(const MachineInstr *MI, unsigned OpNo) {
236 if (MI->getOperand(OpNo).isImm()) {
237 printS16ImmOperand(MI, OpNo);
239 printOp(MI->getOperand(OpNo));
244 void printSymbolLo(const MachineInstr *MI, unsigned OpNo) {
245 if (MI->getOperand(OpNo).isImm()) {
246 printS16ImmOperand(MI, OpNo);
248 printOp(MI->getOperand(OpNo));
253 /// Print local store address
254 void printSymbolLSA(const MachineInstr *MI, unsigned OpNo) {
255 printOp(MI->getOperand(OpNo));
258 void printROTHNeg7Imm(const MachineInstr *MI, unsigned OpNo) {
259 if (MI->getOperand(OpNo).isImm()) {
260 int value = (int) MI->getOperand(OpNo).getImm();
261 assert((value >= 0 && value < 16)
262 && "Invalid negated immediate rotate 7-bit argument");
265 llvm_unreachable("Invalid/non-immediate rotate amount in printRotateNeg7Imm");
269 void printROTNeg7Imm(const MachineInstr *MI, unsigned OpNo) {
270 if (MI->getOperand(OpNo).isImm()) {
271 int value = (int) MI->getOperand(OpNo).getImm();
272 assert((value >= 0 && value <= 32)
273 && "Invalid negated immediate rotate 7-bit argument");
276 llvm_unreachable("Invalid/non-immediate rotate amount in printRotateNeg7Imm");
280 virtual bool runOnMachineFunction(MachineFunction &F) = 0;
283 /// LinuxAsmPrinter - SPU assembly printer, customized for Linux
284 class LinuxAsmPrinter : public SPUAsmPrinter {
286 explicit LinuxAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
287 const MCAsmInfo *T, bool V)
288 : SPUAsmPrinter(O, TM, T, V) {}
290 virtual const char *getPassName() const {
291 return "STI CBEA SPU Assembly Printer";
294 bool runOnMachineFunction(MachineFunction &F);
296 void getAnalysisUsage(AnalysisUsage &AU) const {
297 AU.setPreservesAll();
298 AU.addRequired<MachineModuleInfo>();
299 AU.addRequired<DwarfWriter>();
300 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 llvm_report_error("printOp() does not handle immediate values");
314 case MachineOperand::MO_MachineBasicBlock:
315 O << *GetMBBSymbol(MO.getMBB()->getNumber());
317 case MachineOperand::MO_JumpTableIndex:
318 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
319 << '_' << MO.getIndex();
321 case MachineOperand::MO_ConstantPoolIndex:
322 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
323 << '_' << MO.getIndex();
325 case MachineOperand::MO_ExternalSymbol:
326 // Computing the address of an external symbol, not calling it.
327 if (TM.getRelocationModel() != Reloc::Static) {
328 O << "L" << MAI->getGlobalPrefix() << MO.getSymbolName()
332 O << *GetExternalSymbolSymbol(MO.getSymbolName());
334 case MachineOperand::MO_GlobalAddress:
335 // External or weakly linked global variables need non-lazily-resolved
337 if (TM.getRelocationModel() != Reloc::Static) {
338 GlobalValue *GV = MO.getGlobal();
339 if (((GV->isDeclaration() || GV->hasWeakLinkage() ||
340 GV->hasLinkOnceLinkage() || GV->hasCommonLinkage()))) {
341 O << *GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
345 O << *GetGlobalValueSymbol(MO.getGlobal());
348 O << "<unknown operand type: " << MO.getType() << ">";
353 /// PrintAsmOperand - Print out an operand for an inline asm expression.
355 bool SPUAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
357 const char *ExtraCode) {
358 // Does this asm operand have a single letter operand modifier?
359 if (ExtraCode && ExtraCode[0]) {
360 if (ExtraCode[1] != 0) return true; // Unknown modifier.
362 switch (ExtraCode[0]) {
363 default: return true; // Unknown modifier.
364 case 'L': // Write second word of DImode reference.
365 // Verify that this operand has two consecutive registers.
366 if (!MI->getOperand(OpNo).isReg() ||
367 OpNo+1 == MI->getNumOperands() ||
368 !MI->getOperand(OpNo+1).isReg())
370 ++OpNo; // Return the high-part.
375 printOperand(MI, OpNo);
379 bool SPUAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
382 const char *ExtraCode) {
383 if (ExtraCode && ExtraCode[0])
384 return true; // Unknown modifier.
385 printMemRegReg(MI, OpNo);
389 /// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax
390 /// to the current output stream.
392 void SPUAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
394 processDebugLoc(MI, true);
395 printInstruction(MI);
398 processDebugLoc(MI, false);
402 /// runOnMachineFunction - This uses the printMachineInstruction()
403 /// method to print assembly for each instruction.
405 bool LinuxAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
408 SetupMachineFunction(MF);
411 // Print out constants referenced by the function
412 EmitConstantPool(MF.getConstantPool());
414 // Print out labels for the function.
415 const Function *F = MF.getFunction();
417 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
418 EmitAlignment(MF.getAlignment(), F);
420 switch (F->getLinkage()) {
421 default: llvm_unreachable("Unknown linkage type!");
422 case Function::PrivateLinkage:
423 case Function::LinkerPrivateLinkage:
424 case Function::InternalLinkage: // Symbols default to internal.
426 case Function::ExternalLinkage:
427 O << "\t.global\t" << *CurrentFnSym << "\n" << "\t.type\t";
428 O << *CurrentFnSym << ", @function\n";
430 case Function::WeakAnyLinkage:
431 case Function::WeakODRLinkage:
432 case Function::LinkOnceAnyLinkage:
433 case Function::LinkOnceODRLinkage:
434 O << "\t.global\t" << *CurrentFnSym << "\n";
435 O << "\t.weak_definition\t" << *CurrentFnSym << "\n";
439 O << *CurrentFnSym << ":\n";
441 // Emit pre-function debug information.
442 DW->BeginFunction(&MF);
444 // Print out code for the function.
445 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
447 // Print a label for the basic block.
448 if (I != MF.begin()) {
449 EmitBasicBlockStart(I);
451 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
453 // Print the assembly for the instruction.
454 printMachineInstruction(II);
458 O << "\t.size\t" << *CurrentFnSym << ",.-" << *CurrentFnSym << "\n";
460 // Print out jump tables referenced by the function.
461 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
463 // Emit post-function debug information.
464 DW->EndFunction(&MF);
466 // We didn't modify anything.
470 // Force static initialization.
471 extern "C" void LLVMInitializeCellSPUAsmPrinter() {
472 RegisterAsmPrinter<LinuxAsmPrinter> X(TheCellSPUTarget);