LoongArchMCInstLower.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //=- LoongArchMCInstLower.cpp - Convert LoongArch MachineInstr to an MCInst -=//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file contains code to lower LoongArch MachineInstrs to their
  10. // corresponding MCInst records.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "LoongArch.h"
  14. #include "LoongArchSubtarget.h"
  15. #include "MCTargetDesc/LoongArchBaseInfo.h"
  16. #include "MCTargetDesc/LoongArchMCExpr.h"
  17. #include "llvm/CodeGen/AsmPrinter.h"
  18. #include "llvm/CodeGen/MachineBasicBlock.h"
  19. #include "llvm/CodeGen/MachineInstr.h"
  20. #include "llvm/MC/MCAsmInfo.h"
  21. #include "llvm/MC/MCContext.h"
  22. #include "llvm/Support/raw_ostream.h"
  23. using namespace llvm;
  24. static MCOperand lowerSymbolOperand(const MachineOperand &MO, MCSymbol *Sym,
  25. const AsmPrinter &AP) {
  26. MCContext &Ctx = AP.OutContext;
  27. LoongArchMCExpr::VariantKind Kind;
  28. switch (MO.getTargetFlags()) {
  29. default:
  30. llvm_unreachable("Unknown target flag on GV operand");
  31. case LoongArchII::MO_None:
  32. Kind = LoongArchMCExpr::VK_LoongArch_None;
  33. break;
  34. case LoongArchII::MO_CALL:
  35. Kind = LoongArchMCExpr::VK_LoongArch_CALL;
  36. break;
  37. case LoongArchII::MO_CALL_PLT:
  38. Kind = LoongArchMCExpr::VK_LoongArch_CALL_PLT;
  39. break;
  40. case LoongArchII::MO_PCREL_HI:
  41. Kind = LoongArchMCExpr::VK_LoongArch_PCALA_HI20;
  42. break;
  43. case LoongArchII::MO_PCREL_LO:
  44. Kind = LoongArchMCExpr::VK_LoongArch_PCALA_LO12;
  45. break;
  46. case LoongArchII::MO_GOT_PC_HI:
  47. Kind = LoongArchMCExpr::VK_LoongArch_GOT_PC_HI20;
  48. break;
  49. case LoongArchII::MO_GOT_PC_LO:
  50. Kind = LoongArchMCExpr::VK_LoongArch_GOT_PC_LO12;
  51. break;
  52. case LoongArchII::MO_LE_HI:
  53. Kind = LoongArchMCExpr::VK_LoongArch_TLS_LE_HI20;
  54. break;
  55. case LoongArchII::MO_LE_LO:
  56. Kind = LoongArchMCExpr::VK_LoongArch_TLS_LE_LO12;
  57. break;
  58. case LoongArchII::MO_IE_PC_HI:
  59. Kind = LoongArchMCExpr::VK_LoongArch_TLS_IE_PC_HI20;
  60. break;
  61. case LoongArchII::MO_IE_PC_LO:
  62. Kind = LoongArchMCExpr::VK_LoongArch_TLS_IE_PC_LO12;
  63. break;
  64. case LoongArchII::MO_LD_PC_HI:
  65. Kind = LoongArchMCExpr::VK_LoongArch_TLS_LD_PC_HI20;
  66. break;
  67. case LoongArchII::MO_GD_PC_HI:
  68. Kind = LoongArchMCExpr::VK_LoongArch_TLS_GD_PC_HI20;
  69. break;
  70. // TODO: Handle more target-flags.
  71. }
  72. const MCExpr *ME =
  73. MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, Ctx);
  74. if (!MO.isJTI() && !MO.isMBB() && MO.getOffset())
  75. ME = MCBinaryExpr::createAdd(
  76. ME, MCConstantExpr::create(MO.getOffset(), Ctx), Ctx);
  77. if (Kind != LoongArchMCExpr::VK_LoongArch_None)
  78. ME = LoongArchMCExpr::create(ME, Kind, Ctx);
  79. return MCOperand::createExpr(ME);
  80. }
  81. bool llvm::lowerLoongArchMachineOperandToMCOperand(const MachineOperand &MO,
  82. MCOperand &MCOp,
  83. const AsmPrinter &AP) {
  84. switch (MO.getType()) {
  85. default:
  86. report_fatal_error(
  87. "lowerLoongArchMachineOperandToMCOperand: unknown operand type");
  88. case MachineOperand::MO_Register:
  89. // Ignore all implicit register operands.
  90. if (MO.isImplicit())
  91. return false;
  92. MCOp = MCOperand::createReg(MO.getReg());
  93. break;
  94. case MachineOperand::MO_RegisterMask:
  95. // Regmasks are like implicit defs.
  96. return false;
  97. case MachineOperand::MO_Immediate:
  98. MCOp = MCOperand::createImm(MO.getImm());
  99. break;
  100. case MachineOperand::MO_ConstantPoolIndex:
  101. MCOp = lowerSymbolOperand(MO, AP.GetCPISymbol(MO.getIndex()), AP);
  102. break;
  103. case MachineOperand::MO_GlobalAddress:
  104. MCOp = lowerSymbolOperand(MO, AP.getSymbolPreferLocal(*MO.getGlobal()), AP);
  105. break;
  106. case MachineOperand::MO_MachineBasicBlock:
  107. MCOp = lowerSymbolOperand(MO, MO.getMBB()->getSymbol(), AP);
  108. break;
  109. case MachineOperand::MO_ExternalSymbol:
  110. MCOp = lowerSymbolOperand(
  111. MO, AP.GetExternalSymbolSymbol(MO.getSymbolName()), AP);
  112. break;
  113. case MachineOperand::MO_BlockAddress:
  114. MCOp = lowerSymbolOperand(
  115. MO, AP.GetBlockAddressSymbol(MO.getBlockAddress()), AP);
  116. break;
  117. case MachineOperand::MO_JumpTableIndex:
  118. MCOp = lowerSymbolOperand(MO, AP.GetJTISymbol(MO.getIndex()), AP);
  119. break;
  120. }
  121. return true;
  122. }
  123. bool llvm::lowerLoongArchMachineInstrToMCInst(const MachineInstr *MI,
  124. MCInst &OutMI, AsmPrinter &AP) {
  125. OutMI.setOpcode(MI->getOpcode());
  126. for (const MachineOperand &MO : MI->operands()) {
  127. MCOperand MCOp;
  128. if (lowerLoongArchMachineOperandToMCOperand(MO, MCOp, AP))
  129. OutMI.addOperand(MCOp);
  130. }
  131. return false;
  132. }