make tblgen produce a function that returns the name for a physreg.
[llvm/avr.git] / lib / Target / Mips / AsmPrinter / MipsAsmPrinter.cpp
blobb9d543edc8429c36f9c453090551056d025c0a7a
1 //===-- MipsAsmPrinter.cpp - Mips LLVM assembly writer --------------------===//
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 GAS-format MIPS assembly language.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "mips-asm-printer"
17 #include "Mips.h"
18 #include "MipsSubtarget.h"
19 #include "MipsInstrInfo.h"
20 #include "MipsTargetMachine.h"
21 #include "MipsMachineFunction.h"
22 #include "llvm/Constants.h"
23 #include "llvm/DerivedTypes.h"
24 #include "llvm/Module.h"
25 #include "llvm/CodeGen/AsmPrinter.h"
26 #include "llvm/CodeGen/DwarfWriter.h"
27 #include "llvm/CodeGen/MachineFunctionPass.h"
28 #include "llvm/CodeGen/MachineConstantPool.h"
29 #include "llvm/CodeGen/MachineFrameInfo.h"
30 #include "llvm/CodeGen/MachineInstr.h"
31 #include "llvm/MC/MCStreamer.h"
32 #include "llvm/MC/MCAsmInfo.h"
33 #include "llvm/MC/MCSymbol.h"
34 #include "llvm/Target/TargetData.h"
35 #include "llvm/Target/TargetLoweringObjectFile.h"
36 #include "llvm/Target/TargetMachine.h"
37 #include "llvm/Target/TargetOptions.h"
38 #include "llvm/Target/TargetRegistry.h"
39 #include "llvm/Support/ErrorHandling.h"
40 #include "llvm/Support/Mangler.h"
41 #include "llvm/ADT/Statistic.h"
42 #include "llvm/ADT/StringExtras.h"
43 #include "llvm/Support/Debug.h"
44 #include "llvm/Support/CommandLine.h"
45 #include "llvm/Support/FormattedStream.h"
46 #include "llvm/Support/MathExtras.h"
47 #include <cctype>
49 using namespace llvm;
51 STATISTIC(EmittedInsts, "Number of machine instrs printed");
53 namespace {
54 class VISIBILITY_HIDDEN MipsAsmPrinter : public AsmPrinter {
55 const MipsSubtarget *Subtarget;
56 public:
57 explicit MipsAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
58 const MCAsmInfo *T, bool V)
59 : AsmPrinter(O, TM, T, V) {
60 Subtarget = &TM.getSubtarget<MipsSubtarget>();
63 virtual const char *getPassName() const {
64 return "Mips Assembly Printer";
67 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
68 unsigned AsmVariant, const char *ExtraCode);
69 void printOperand(const MachineInstr *MI, int opNum);
70 void printUnsignedImm(const MachineInstr *MI, int opNum);
71 void printMemOperand(const MachineInstr *MI, int opNum,
72 const char *Modifier = 0);
73 void printFCCOperand(const MachineInstr *MI, int opNum,
74 const char *Modifier = 0);
75 void PrintGlobalVariable(const GlobalVariable *GVar);
76 void printSavedRegsBitmask(MachineFunction &MF);
77 void printHex32(unsigned int Value);
79 const char *emitCurrentABIString();
80 void emitFunctionStart(MachineFunction &MF);
81 void emitFunctionEnd(MachineFunction &MF);
82 void emitFrameDirective(MachineFunction &MF);
84 void printInstruction(const MachineInstr *MI); // autogenerated.
85 const char *getRegisterName(unsigned RegNo) const;
87 bool runOnMachineFunction(MachineFunction &F);
88 bool doInitialization(Module &M);
90 } // end of anonymous namespace
92 #include "MipsGenAsmWriter.inc"
94 //===----------------------------------------------------------------------===//
96 // Mips Asm Directives
98 // -- Frame directive "frame Stackpointer, Stacksize, RARegister"
99 // Describe the stack frame.
101 // -- Mask directives "(f)mask bitmask, offset"
102 // Tells the assembler which registers are saved and where.
103 // bitmask - contain a little endian bitset indicating which registers are
104 // saved on function prologue (e.g. with a 0x80000000 mask, the
105 // assembler knows the register 31 (RA) is saved at prologue.
106 // offset - the position before stack pointer subtraction indicating where
107 // the first saved register on prologue is located. (e.g. with a
109 // Consider the following function prologue:
111 // .frame $fp,48,$ra
112 // .mask 0xc0000000,-8
113 // addiu $sp, $sp, -48
114 // sw $ra, 40($sp)
115 // sw $fp, 36($sp)
117 // With a 0xc0000000 mask, the assembler knows the register 31 (RA) and
118 // 30 (FP) are saved at prologue. As the save order on prologue is from
119 // left to right, RA is saved first. A -8 offset means that after the
120 // stack pointer subtration, the first register in the mask (RA) will be
121 // saved at address 48-8=40.
123 //===----------------------------------------------------------------------===//
125 //===----------------------------------------------------------------------===//
126 // Mask directives
127 //===----------------------------------------------------------------------===//
129 // Create a bitmask with all callee saved registers for CPU or Floating Point
130 // registers. For CPU registers consider RA, GP and FP for saving if necessary.
131 void MipsAsmPrinter::
132 printSavedRegsBitmask(MachineFunction &MF)
134 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
135 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
137 // CPU and FPU Saved Registers Bitmasks
138 unsigned int CPUBitmask = 0;
139 unsigned int FPUBitmask = 0;
141 // Set the CPU and FPU Bitmasks
142 MachineFrameInfo *MFI = MF.getFrameInfo();
143 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
144 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
145 unsigned RegNum = MipsRegisterInfo::getRegisterNumbering(CSI[i].getReg());
146 if (CSI[i].getRegClass() == Mips::CPURegsRegisterClass)
147 CPUBitmask |= (1 << RegNum);
148 else
149 FPUBitmask |= (1 << RegNum);
152 // Return Address and Frame registers must also be set in CPUBitmask.
153 if (RI.hasFP(MF))
154 CPUBitmask |= (1 << MipsRegisterInfo::
155 getRegisterNumbering(RI.getFrameRegister(MF)));
157 if (MF.getFrameInfo()->hasCalls())
158 CPUBitmask |= (1 << MipsRegisterInfo::
159 getRegisterNumbering(RI.getRARegister()));
161 // Print CPUBitmask
162 O << "\t.mask \t"; printHex32(CPUBitmask); O << ','
163 << MipsFI->getCPUTopSavedRegOff() << '\n';
165 // Print FPUBitmask
166 O << "\t.fmask\t"; printHex32(FPUBitmask); O << ","
167 << MipsFI->getFPUTopSavedRegOff() << '\n';
170 // Print a 32 bit hex number with all numbers.
171 void MipsAsmPrinter::
172 printHex32(unsigned int Value)
174 O << "0x";
175 for (int i = 7; i >= 0; i--)
176 O << utohexstr( (Value & (0xF << (i*4))) >> (i*4) );
179 //===----------------------------------------------------------------------===//
180 // Frame and Set directives
181 //===----------------------------------------------------------------------===//
183 /// Frame Directive
184 void MipsAsmPrinter::emitFrameDirective(MachineFunction &MF) {
185 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
187 unsigned stackReg = RI.getFrameRegister(MF);
188 unsigned returnReg = RI.getRARegister();
189 unsigned stackSize = MF.getFrameInfo()->getStackSize();
192 O << "\t.frame\t" << '$' << LowercaseString(RI.get(stackReg).AsmName)
193 << ',' << stackSize << ','
194 << '$' << LowercaseString(RI.get(returnReg).AsmName)
195 << '\n';
198 /// Emit Set directives.
199 const char *MipsAsmPrinter::emitCurrentABIString() {
200 switch(Subtarget->getTargetABI()) {
201 case MipsSubtarget::O32: return "abi32";
202 case MipsSubtarget::O64: return "abiO64";
203 case MipsSubtarget::N32: return "abiN32";
204 case MipsSubtarget::N64: return "abi64";
205 case MipsSubtarget::EABI: return "eabi32"; // TODO: handle eabi64
206 default: break;
209 llvm_unreachable("Unknown Mips ABI");
210 return NULL;
213 /// Emit the directives used by GAS on the start of functions
214 void MipsAsmPrinter::emitFunctionStart(MachineFunction &MF) {
215 // Print out the label for the function.
216 const Function *F = MF.getFunction();
217 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
219 // 2 bits aligned
220 EmitAlignment(MF.getAlignment(), F);
222 O << "\t.globl\t" << CurrentFnName << '\n';
223 O << "\t.ent\t" << CurrentFnName << '\n';
225 printVisibility(CurrentFnName, F->getVisibility());
227 if ((MAI->hasDotTypeDotSizeDirective()) && Subtarget->isLinux())
228 O << "\t.type\t" << CurrentFnName << ", @function\n";
230 O << CurrentFnName << ":\n";
232 emitFrameDirective(MF);
233 printSavedRegsBitmask(MF);
235 O << '\n';
238 /// Emit the directives used by GAS on the end of functions
239 void MipsAsmPrinter::emitFunctionEnd(MachineFunction &MF) {
240 // There are instruction for this macros, but they must
241 // always be at the function end, and we can't emit and
242 // break with BB logic.
243 O << "\t.set\tmacro\n";
244 O << "\t.set\treorder\n";
246 O << "\t.end\t" << CurrentFnName << '\n';
247 if (MAI->hasDotTypeDotSizeDirective() && !Subtarget->isLinux())
248 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
251 /// runOnMachineFunction - This uses the printMachineInstruction()
252 /// method to print assembly for each instruction.
253 bool MipsAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
254 this->MF = &MF;
256 SetupMachineFunction(MF);
258 // Print out constants referenced by the function
259 EmitConstantPool(MF.getConstantPool());
261 // Print out jump tables referenced by the function
262 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
264 O << "\n\n";
266 // Emit the function start directives
267 emitFunctionStart(MF);
269 // Print out code for the function.
270 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
271 I != E; ++I) {
273 // Print a label for the basic block.
274 if (I != MF.begin()) {
275 EmitBasicBlockStart(I);
276 O << '\n';
279 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
280 II != E; ++II) {
281 processDebugLoc(II->getDebugLoc());
283 // Print the assembly for the instruction.
284 printInstruction(II);
286 if (VerboseAsm && !II->getDebugLoc().isUnknown())
287 EmitComments(*II);
288 O << '\n';
290 ++EmittedInsts;
293 // Each Basic Block is separated by a newline
294 O << '\n';
297 // Emit function end directives
298 emitFunctionEnd(MF);
300 // We didn't modify anything.
301 return false;
304 // Print out an operand for an inline asm expression.
305 bool MipsAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
306 unsigned AsmVariant,const char *ExtraCode){
307 // Does this asm operand have a single letter operand modifier?
308 if (ExtraCode && ExtraCode[0])
309 return true; // Unknown modifier.
311 printOperand(MI, OpNo);
312 return false;
315 void MipsAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
316 const MachineOperand &MO = MI->getOperand(opNum);
317 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
318 bool closeP = false;
320 if (MO.getTargetFlags())
321 closeP = true;
323 switch(MO.getTargetFlags()) {
324 case MipsII::MO_GPREL: O << "%gp_rel("; break;
325 case MipsII::MO_GOT_CALL: O << "%call16("; break;
326 case MipsII::MO_GOT:
327 if (MI->getOpcode() == Mips::LW)
328 O << "%got(";
329 else
330 O << "%lo(";
331 break;
332 case MipsII::MO_ABS_HILO:
333 if (MI->getOpcode() == Mips::LUi)
334 O << "%hi(";
335 else
336 O << "%lo(";
337 break;
340 switch (MO.getType())
342 case MachineOperand::MO_Register:
343 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
344 O << '$' << LowercaseString (RI.get(MO.getReg()).AsmName);
345 else
346 O << '$' << MO.getReg();
347 break;
349 case MachineOperand::MO_Immediate:
350 O << (short int)MO.getImm();
351 break;
353 case MachineOperand::MO_MachineBasicBlock:
354 GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI);
355 return;
357 case MachineOperand::MO_GlobalAddress:
358 O << Mang->getMangledName(MO.getGlobal());
359 break;
361 case MachineOperand::MO_ExternalSymbol:
362 O << MO.getSymbolName();
363 break;
365 case MachineOperand::MO_JumpTableIndex:
366 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
367 << '_' << MO.getIndex();
368 break;
370 case MachineOperand::MO_ConstantPoolIndex:
371 O << MAI->getPrivateGlobalPrefix() << "CPI"
372 << getFunctionNumber() << "_" << MO.getIndex();
373 break;
375 default:
376 llvm_unreachable("<unknown operand type>");
379 if (closeP) O << ")";
382 void MipsAsmPrinter::printUnsignedImm(const MachineInstr *MI, int opNum) {
383 const MachineOperand &MO = MI->getOperand(opNum);
384 if (MO.getType() == MachineOperand::MO_Immediate)
385 O << (unsigned short int)MO.getImm();
386 else
387 printOperand(MI, opNum);
390 void MipsAsmPrinter::
391 printMemOperand(const MachineInstr *MI, int opNum, const char *Modifier) {
392 // when using stack locations for not load/store instructions
393 // print the same way as all normal 3 operand instructions.
394 if (Modifier && !strcmp(Modifier, "stackloc")) {
395 printOperand(MI, opNum+1);
396 O << ", ";
397 printOperand(MI, opNum);
398 return;
401 // Load/Store memory operands -- imm($reg)
402 // If PIC target the target is loaded as the
403 // pattern lw $25,%call16($28)
404 printOperand(MI, opNum);
405 O << "(";
406 printOperand(MI, opNum+1);
407 O << ")";
410 void MipsAsmPrinter::
411 printFCCOperand(const MachineInstr *MI, int opNum, const char *Modifier) {
412 const MachineOperand& MO = MI->getOperand(opNum);
413 O << Mips::MipsFCCToString((Mips::CondCode)MO.getImm());
416 bool MipsAsmPrinter::doInitialization(Module &M) {
417 // FIXME: Use SwitchSection.
419 // Tell the assembler which ABI we are using
420 O << "\t.section .mdebug." << emitCurrentABIString() << '\n';
422 // TODO: handle O64 ABI
423 if (Subtarget->isABI_EABI())
424 O << "\t.section .gcc_compiled_long" <<
425 (Subtarget->isGP32bit() ? "32" : "64") << '\n';
427 // return to previous section
428 O << "\t.previous" << '\n';
430 return AsmPrinter::doInitialization(M);
433 void MipsAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
434 const TargetData *TD = TM.getTargetData();
436 if (!GVar->hasInitializer())
437 return; // External global require no code
439 // Check to see if this is a special global used by LLVM, if so, emit it.
440 if (EmitSpecialLLVMGlobal(GVar))
441 return;
443 O << "\n\n";
444 std::string name = Mang->getMangledName(GVar);
445 Constant *C = GVar->getInitializer();
446 const Type *CTy = C->getType();
447 unsigned Size = TD->getTypeAllocSize(CTy);
448 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
449 bool printSizeAndType = true;
451 // A data structure or array is aligned in memory to the largest
452 // alignment boundary required by any data type inside it (this matches
453 // the Preferred Type Alignment). For integral types, the alignment is
454 // the type size.
455 unsigned Align;
456 if (CTy->getTypeID() == Type::IntegerTyID ||
457 CTy->getTypeID() == Type::VoidTyID) {
458 assert(!(Size & (Size-1)) && "Alignment is not a power of two!");
459 Align = Log2_32(Size);
460 } else
461 Align = TD->getPreferredTypeAlignmentShift(CTy);
463 printVisibility(name, GVar->getVisibility());
465 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GVar, Mang,
466 TM));
468 if (C->isNullValue() && !GVar->hasSection()) {
469 if (!GVar->isThreadLocal() &&
470 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
471 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
473 if (GVar->hasLocalLinkage())
474 O << "\t.local\t" << name << '\n';
476 O << MAI->getCOMMDirective() << name << ',' << Size;
477 if (MAI->getCOMMDirectiveTakesAlignment())
478 O << ',' << (1 << Align);
480 O << '\n';
481 return;
484 switch (GVar->getLinkage()) {
485 case GlobalValue::LinkOnceAnyLinkage:
486 case GlobalValue::LinkOnceODRLinkage:
487 case GlobalValue::CommonLinkage:
488 case GlobalValue::WeakAnyLinkage:
489 case GlobalValue::WeakODRLinkage:
490 // FIXME: Verify correct for weak.
491 // Nonnull linkonce -> weak
492 O << "\t.weak " << name << '\n';
493 break;
494 case GlobalValue::AppendingLinkage:
495 // FIXME: appending linkage variables should go into a section of their name
496 // or something. For now, just emit them as external.
497 case GlobalValue::ExternalLinkage:
498 // If external or appending, declare as a global symbol
499 O << MAI->getGlobalDirective() << name << '\n';
500 // Fall Through
501 case GlobalValue::PrivateLinkage:
502 case GlobalValue::LinkerPrivateLinkage:
503 case GlobalValue::InternalLinkage:
504 if (CVA && CVA->isCString())
505 printSizeAndType = false;
506 break;
507 case GlobalValue::GhostLinkage:
508 llvm_unreachable("Should not have any unmaterialized functions!");
509 case GlobalValue::DLLImportLinkage:
510 llvm_unreachable("DLLImport linkage is not supported by this target!");
511 case GlobalValue::DLLExportLinkage:
512 llvm_unreachable("DLLExport linkage is not supported by this target!");
513 default:
514 llvm_unreachable("Unknown linkage type!");
517 EmitAlignment(Align, GVar);
519 if (MAI->hasDotTypeDotSizeDirective() && printSizeAndType) {
520 O << "\t.type " << name << ",@object\n";
521 O << "\t.size " << name << ',' << Size << '\n';
524 O << name << ":\n";
525 EmitGlobalConstant(C);
529 // Force static initialization.
530 extern "C" void LLVMInitializeMipsAsmPrinter() {
531 RegisterAsmPrinter<MipsAsmPrinter> X(TheMipsTarget);
532 RegisterAsmPrinter<MipsAsmPrinter> Y(TheMipselTarget);