NVPTXMCExpr.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //===-- NVPTXMCExpr.cpp - NVPTX specific MC expression classes ------------===//
  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. #include "NVPTXMCExpr.h"
  9. #include "llvm/ADT/StringExtras.h"
  10. #include "llvm/MC/MCAssembler.h"
  11. #include "llvm/MC/MCContext.h"
  12. #include "llvm/Support/Format.h"
  13. using namespace llvm;
  14. #define DEBUG_TYPE "nvptx-mcexpr"
  15. const NVPTXFloatMCExpr *
  16. NVPTXFloatMCExpr::create(VariantKind Kind, const APFloat &Flt, MCContext &Ctx) {
  17. return new (Ctx) NVPTXFloatMCExpr(Kind, Flt);
  18. }
  19. void NVPTXFloatMCExpr::printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const {
  20. bool Ignored;
  21. unsigned NumHex;
  22. APFloat APF = getAPFloat();
  23. switch (Kind) {
  24. default: llvm_unreachable("Invalid kind!");
  25. case VK_NVPTX_HALF_PREC_FLOAT:
  26. // ptxas does not have a way to specify half-precision floats.
  27. // Instead we have to print and load fp16 constants as .b16
  28. OS << "0x";
  29. NumHex = 4;
  30. APF.convert(APFloat::IEEEhalf(), APFloat::rmNearestTiesToEven, &Ignored);
  31. break;
  32. case VK_NVPTX_SINGLE_PREC_FLOAT:
  33. OS << "0f";
  34. NumHex = 8;
  35. APF.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven, &Ignored);
  36. break;
  37. case VK_NVPTX_DOUBLE_PREC_FLOAT:
  38. OS << "0d";
  39. NumHex = 16;
  40. APF.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &Ignored);
  41. break;
  42. }
  43. APInt API = APF.bitcastToAPInt();
  44. OS << format_hex_no_prefix(API.getZExtValue(), NumHex, /*Upper=*/true);
  45. }
  46. const NVPTXGenericMCSymbolRefExpr*
  47. NVPTXGenericMCSymbolRefExpr::create(const MCSymbolRefExpr *SymExpr,
  48. MCContext &Ctx) {
  49. return new (Ctx) NVPTXGenericMCSymbolRefExpr(SymExpr);
  50. }
  51. void NVPTXGenericMCSymbolRefExpr::printImpl(raw_ostream &OS,
  52. const MCAsmInfo *MAI) const {
  53. OS << "generic(";
  54. SymExpr->print(OS, MAI);
  55. OS << ")";
  56. }