PageRenderTime 2090ms CodeModel.GetById 5ms RepoModel.GetById 0ms app.codeStats 1ms

/external/llvm/lib/Target/MBlaze/MBlazeAsmPrinter.cpp

https://gitlab.com/brian0218/rk3188_r-box_android4.2.2_sdk
C++ | 326 lines | 210 code | 50 blank | 66 comment | 30 complexity | 299bfa57fcdc647979295cf132219929 MD5 | raw file
  1. //===-- MBlazeAsmPrinter.cpp - MBlaze 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 MBlaze assembly language.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #define DEBUG_TYPE "mblaze-asm-printer"
  15. #include "MBlaze.h"
  16. #include "MBlazeSubtarget.h"
  17. #include "MBlazeInstrInfo.h"
  18. #include "MBlazeTargetMachine.h"
  19. #include "MBlazeMachineFunction.h"
  20. #include "MBlazeMCInstLower.h"
  21. #include "InstPrinter/MBlazeInstPrinter.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/MachineFunctionPass.h"
  27. #include "llvm/CodeGen/MachineConstantPool.h"
  28. #include "llvm/CodeGen/MachineFrameInfo.h"
  29. #include "llvm/CodeGen/MachineInstr.h"
  30. #include "llvm/MC/MCInst.h"
  31. #include "llvm/MC/MCStreamer.h"
  32. #include "llvm/MC/MCAsmInfo.h"
  33. #include "llvm/MC/MCSymbol.h"
  34. #include "llvm/Target/Mangler.h"
  35. #include "llvm/Target/TargetData.h"
  36. #include "llvm/Target/TargetLoweringObjectFile.h"
  37. #include "llvm/Target/TargetMachine.h"
  38. #include "llvm/Target/TargetOptions.h"
  39. #include "llvm/Support/ErrorHandling.h"
  40. #include "llvm/Support/TargetRegistry.h"
  41. #include "llvm/Support/raw_ostream.h"
  42. #include <cctype>
  43. using namespace llvm;
  44. namespace {
  45. class MBlazeAsmPrinter : public AsmPrinter {
  46. const MBlazeSubtarget *Subtarget;
  47. public:
  48. explicit MBlazeAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
  49. : AsmPrinter(TM, Streamer) {
  50. Subtarget = &TM.getSubtarget<MBlazeSubtarget>();
  51. }
  52. virtual const char *getPassName() const {
  53. return "MBlaze Assembly Printer";
  54. }
  55. void printSavedRegsBitmask();
  56. void emitFrameDirective();
  57. virtual void EmitFunctionBodyStart();
  58. virtual void EmitFunctionBodyEnd();
  59. virtual void EmitFunctionEntryLabel();
  60. virtual bool isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB)
  61. const;
  62. bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
  63. unsigned AsmVariant, const char *ExtraCode,
  64. raw_ostream &O);
  65. void printOperand(const MachineInstr *MI, int opNum, raw_ostream &O);
  66. void printUnsignedImm(const MachineInstr *MI, int opNum, raw_ostream &O);
  67. void printFSLImm(const MachineInstr *MI, int opNum, raw_ostream &O);
  68. void printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &O,
  69. const char *Modifier = 0);
  70. void EmitInstruction(const MachineInstr *MI);
  71. };
  72. } // end of anonymous namespace
  73. // #include "MBlazeGenAsmWriter.inc"
  74. //===----------------------------------------------------------------------===//
  75. //
  76. // MBlaze Asm Directives
  77. //
  78. // -- Frame directive "frame Stackpointer, Stacksize, RARegister"
  79. // Describe the stack frame.
  80. //
  81. // -- Mask directives "mask bitmask, offset"
  82. // Tells the assembler which registers are saved and where.
  83. // bitmask - contain a little endian bitset indicating which registers are
  84. // saved on function prologue (e.g. with a 0x80000000 mask, the
  85. // assembler knows the register 31 (RA) is saved at prologue.
  86. // offset - the position before stack pointer subtraction indicating where
  87. // the first saved register on prologue is located. (e.g. with a
  88. //
  89. // Consider the following function prologue:
  90. //
  91. // .frame R19,48,R15
  92. // .mask 0xc0000000,-8
  93. // addiu R1, R1, -48
  94. // sw R15, 40(R1)
  95. // sw R19, 36(R1)
  96. //
  97. // With a 0xc0000000 mask, the assembler knows the register 15 (R15) and
  98. // 19 (R19) are saved at prologue. As the save order on prologue is from
  99. // left to right, R15 is saved first. A -8 offset means that after the
  100. // stack pointer subtration, the first register in the mask (R15) will be
  101. // saved at address 48-8=40.
  102. //
  103. //===----------------------------------------------------------------------===//
  104. // Print a 32 bit hex number with all numbers.
  105. static void printHex32(unsigned int Value, raw_ostream &O) {
  106. O << "0x";
  107. for (int i = 7; i >= 0; i--)
  108. O.write_hex((Value & (0xF << (i*4))) >> (i*4));
  109. }
  110. // Create a bitmask with all callee saved registers for CPU or Floating Point
  111. // registers. For CPU registers consider RA, GP and FP for saving if necessary.
  112. void MBlazeAsmPrinter::printSavedRegsBitmask() {
  113. const TargetFrameLowering *TFI = TM.getFrameLowering();
  114. const TargetRegisterInfo &RI = *TM.getRegisterInfo();
  115. // CPU Saved Registers Bitmasks
  116. unsigned int CPUBitmask = 0;
  117. // Set the CPU Bitmasks
  118. const MachineFrameInfo *MFI = MF->getFrameInfo();
  119. const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
  120. for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
  121. unsigned Reg = CSI[i].getReg();
  122. unsigned RegNum = getMBlazeRegisterNumbering(Reg);
  123. if (MBlaze::GPRRegClass.contains(Reg))
  124. CPUBitmask |= (1 << RegNum);
  125. }
  126. // Return Address and Frame registers must also be set in CPUBitmask.
  127. if (TFI->hasFP(*MF))
  128. CPUBitmask |= (1 << getMBlazeRegisterNumbering(RI.getFrameRegister(*MF)));
  129. if (MFI->adjustsStack())
  130. CPUBitmask |= (1 << getMBlazeRegisterNumbering(RI.getRARegister()));
  131. // Print CPUBitmask
  132. OutStreamer.EmitRawText("\t.mask\t0x" + Twine::utohexstr(CPUBitmask));
  133. }
  134. /// Frame Directive
  135. void MBlazeAsmPrinter::emitFrameDirective() {
  136. if (!OutStreamer.hasRawTextSupport())
  137. return;
  138. const TargetRegisterInfo &RI = *TM.getRegisterInfo();
  139. unsigned stkReg = RI.getFrameRegister(*MF);
  140. unsigned retReg = RI.getRARegister();
  141. unsigned stkSze = MF->getFrameInfo()->getStackSize();
  142. OutStreamer.EmitRawText("\t.frame\t" +
  143. Twine(MBlazeInstPrinter::getRegisterName(stkReg)) +
  144. "," + Twine(stkSze) + "," +
  145. Twine(MBlazeInstPrinter::getRegisterName(retReg)));
  146. }
  147. void MBlazeAsmPrinter::EmitFunctionEntryLabel() {
  148. if (OutStreamer.hasRawTextSupport())
  149. OutStreamer.EmitRawText("\t.ent\t" + Twine(CurrentFnSym->getName()));
  150. AsmPrinter::EmitFunctionEntryLabel();
  151. }
  152. void MBlazeAsmPrinter::EmitFunctionBodyStart() {
  153. if (!OutStreamer.hasRawTextSupport())
  154. return;
  155. emitFrameDirective();
  156. printSavedRegsBitmask();
  157. }
  158. void MBlazeAsmPrinter::EmitFunctionBodyEnd() {
  159. if (OutStreamer.hasRawTextSupport())
  160. OutStreamer.EmitRawText("\t.end\t" + Twine(CurrentFnSym->getName()));
  161. }
  162. //===----------------------------------------------------------------------===//
  163. void MBlazeAsmPrinter::EmitInstruction(const MachineInstr *MI) {
  164. MBlazeMCInstLower MCInstLowering(OutContext, *this);
  165. MCInst TmpInst;
  166. MCInstLowering.Lower(MI, TmpInst);
  167. OutStreamer.EmitInstruction(TmpInst);
  168. }
  169. // Print out an operand for an inline asm expression.
  170. bool MBlazeAsmPrinter::
  171. PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
  172. unsigned AsmVariant,const char *ExtraCode, raw_ostream &O) {
  173. // Does this asm operand have a single letter operand modifier?
  174. if (ExtraCode && ExtraCode[0])
  175. if (ExtraCode[1] != 0) return true; // Unknown modifier.
  176. switch (ExtraCode[0]) {
  177. default:
  178. // See if this is a generic print operand
  179. return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, O);
  180. }
  181. printOperand(MI, OpNo, O);
  182. return false;
  183. }
  184. void MBlazeAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
  185. raw_ostream &O) {
  186. const MachineOperand &MO = MI->getOperand(opNum);
  187. switch (MO.getType()) {
  188. case MachineOperand::MO_Register:
  189. O << MBlazeInstPrinter::getRegisterName(MO.getReg());
  190. break;
  191. case MachineOperand::MO_Immediate:
  192. O << (int32_t)MO.getImm();
  193. break;
  194. case MachineOperand::MO_FPImmediate: {
  195. const ConstantFP *fp = MO.getFPImm();
  196. printHex32(fp->getValueAPF().bitcastToAPInt().getZExtValue(), O);
  197. O << ";\t# immediate = " << *fp;
  198. break;
  199. }
  200. case MachineOperand::MO_MachineBasicBlock:
  201. O << *MO.getMBB()->getSymbol();
  202. return;
  203. case MachineOperand::MO_GlobalAddress:
  204. O << *Mang->getSymbol(MO.getGlobal());
  205. break;
  206. case MachineOperand::MO_ExternalSymbol:
  207. O << *GetExternalSymbolSymbol(MO.getSymbolName());
  208. break;
  209. case MachineOperand::MO_JumpTableIndex:
  210. O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
  211. << '_' << MO.getIndex();
  212. break;
  213. case MachineOperand::MO_ConstantPoolIndex:
  214. O << MAI->getPrivateGlobalPrefix() << "CPI"
  215. << getFunctionNumber() << "_" << MO.getIndex();
  216. if (MO.getOffset())
  217. O << "+" << MO.getOffset();
  218. break;
  219. default:
  220. llvm_unreachable("<unknown operand type>");
  221. }
  222. }
  223. void MBlazeAsmPrinter::printUnsignedImm(const MachineInstr *MI, int opNum,
  224. raw_ostream &O) {
  225. const MachineOperand &MO = MI->getOperand(opNum);
  226. if (MO.isImm())
  227. O << (uint32_t)MO.getImm();
  228. else
  229. printOperand(MI, opNum, O);
  230. }
  231. void MBlazeAsmPrinter::printFSLImm(const MachineInstr *MI, int opNum,
  232. raw_ostream &O) {
  233. const MachineOperand &MO = MI->getOperand(opNum);
  234. if (MO.isImm())
  235. O << "rfsl" << (unsigned int)MO.getImm();
  236. else
  237. printOperand(MI, opNum, O);
  238. }
  239. void MBlazeAsmPrinter::
  240. printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &O,
  241. const char *Modifier) {
  242. printOperand(MI, opNum, O);
  243. O << ", ";
  244. printOperand(MI, opNum+1, O);
  245. }
  246. /// isBlockOnlyReachableByFallthough - Return true if the basic block has
  247. /// exactly one predecessor and the control transfer mechanism between
  248. /// the predecessor and this block is a fall-through.
  249. bool MBlazeAsmPrinter::
  250. isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const {
  251. // If this is a landing pad, it isn't a fall through. If it has no preds,
  252. // then nothing falls through to it.
  253. if (MBB->isLandingPad() || MBB->pred_empty())
  254. return false;
  255. // If there isn't exactly one predecessor, it can't be a fall through.
  256. MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(), PI2 = PI;
  257. ++PI2;
  258. if (PI2 != MBB->pred_end())
  259. return false;
  260. // The predecessor has to be immediately before this block.
  261. const MachineBasicBlock *Pred = *PI;
  262. if (!Pred->isLayoutSuccessor(MBB))
  263. return false;
  264. // If the block is completely empty, then it definitely does fall through.
  265. if (Pred->empty())
  266. return true;
  267. // Check if the last terminator is an unconditional branch.
  268. MachineBasicBlock::const_iterator I = Pred->end();
  269. while (I != Pred->begin() && !(--I)->isTerminator())
  270. ; // Noop
  271. return I == Pred->end() || !I->isBarrier();
  272. }
  273. // Force static initialization.
  274. extern "C" void LLVMInitializeMBlazeAsmPrinter() {
  275. RegisterAsmPrinter<MBlazeAsmPrinter> X(TheMBlazeTarget);
  276. }