xref: /freebsd/contrib/llvm-project/llvm/lib/Target/NVPTX/NVPTXMCExpr.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //===-- NVPTXMCExpr.h - NVPTX specific MC expression classes ----*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric // Modeled after ARMMCExpr
100b57cec5SDimitry Andric 
110b57cec5SDimitry Andric #ifndef LLVM_LIB_TARGET_NVPTX_NVPTXMCEXPR_H
120b57cec5SDimitry Andric #define LLVM_LIB_TARGET_NVPTX_NVPTXMCEXPR_H
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #include "llvm/ADT/APFloat.h"
150b57cec5SDimitry Andric #include "llvm/MC/MCExpr.h"
160b57cec5SDimitry Andric #include <utility>
170b57cec5SDimitry Andric 
180b57cec5SDimitry Andric namespace llvm {
190b57cec5SDimitry Andric 
200b57cec5SDimitry Andric class NVPTXFloatMCExpr : public MCTargetExpr {
210b57cec5SDimitry Andric public:
220b57cec5SDimitry Andric   enum VariantKind {
230b57cec5SDimitry Andric     VK_NVPTX_None,
2406c3fb27SDimitry Andric     VK_NVPTX_BFLOAT_PREC_FLOAT, // FP constant in bfloat-precision
250b57cec5SDimitry Andric     VK_NVPTX_HALF_PREC_FLOAT,   // FP constant in half-precision
260b57cec5SDimitry Andric     VK_NVPTX_SINGLE_PREC_FLOAT, // FP constant in single-precision
270b57cec5SDimitry Andric     VK_NVPTX_DOUBLE_PREC_FLOAT  // FP constant in double-precision
280b57cec5SDimitry Andric   };
290b57cec5SDimitry Andric 
300b57cec5SDimitry Andric private:
310b57cec5SDimitry Andric   const VariantKind Kind;
320b57cec5SDimitry Andric   const APFloat Flt;
330b57cec5SDimitry Andric 
NVPTXFloatMCExpr(VariantKind Kind,APFloat Flt)340b57cec5SDimitry Andric   explicit NVPTXFloatMCExpr(VariantKind Kind, APFloat Flt)
350b57cec5SDimitry Andric       : Kind(Kind), Flt(std::move(Flt)) {}
360b57cec5SDimitry Andric 
370b57cec5SDimitry Andric public:
380b57cec5SDimitry Andric   /// @name Construction
390b57cec5SDimitry Andric   /// @{
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric   static const NVPTXFloatMCExpr *create(VariantKind Kind, const APFloat &Flt,
420b57cec5SDimitry Andric                                         MCContext &Ctx);
430b57cec5SDimitry Andric 
createConstantBFPHalf(const APFloat & Flt,MCContext & Ctx)4406c3fb27SDimitry Andric   static const NVPTXFloatMCExpr *createConstantBFPHalf(const APFloat &Flt,
4506c3fb27SDimitry Andric                                                        MCContext &Ctx) {
4606c3fb27SDimitry Andric     return create(VK_NVPTX_BFLOAT_PREC_FLOAT, Flt, Ctx);
4706c3fb27SDimitry Andric   }
4806c3fb27SDimitry Andric 
createConstantFPHalf(const APFloat & Flt,MCContext & Ctx)490b57cec5SDimitry Andric   static const NVPTXFloatMCExpr *createConstantFPHalf(const APFloat &Flt,
500b57cec5SDimitry Andric                                                         MCContext &Ctx) {
510b57cec5SDimitry Andric     return create(VK_NVPTX_HALF_PREC_FLOAT, Flt, Ctx);
520b57cec5SDimitry Andric   }
530b57cec5SDimitry Andric 
createConstantFPSingle(const APFloat & Flt,MCContext & Ctx)540b57cec5SDimitry Andric   static const NVPTXFloatMCExpr *createConstantFPSingle(const APFloat &Flt,
550b57cec5SDimitry Andric                                                         MCContext &Ctx) {
560b57cec5SDimitry Andric     return create(VK_NVPTX_SINGLE_PREC_FLOAT, Flt, Ctx);
570b57cec5SDimitry Andric   }
580b57cec5SDimitry Andric 
createConstantFPDouble(const APFloat & Flt,MCContext & Ctx)590b57cec5SDimitry Andric   static const NVPTXFloatMCExpr *createConstantFPDouble(const APFloat &Flt,
600b57cec5SDimitry Andric                                                         MCContext &Ctx) {
610b57cec5SDimitry Andric     return create(VK_NVPTX_DOUBLE_PREC_FLOAT, Flt, Ctx);
620b57cec5SDimitry Andric   }
630b57cec5SDimitry Andric 
640b57cec5SDimitry Andric   /// @}
650b57cec5SDimitry Andric   /// @name Accessors
660b57cec5SDimitry Andric   /// @{
670b57cec5SDimitry Andric 
680b57cec5SDimitry Andric   /// getOpcode - Get the kind of this expression.
getKind()690b57cec5SDimitry Andric   VariantKind getKind() const { return Kind; }
700b57cec5SDimitry Andric 
710b57cec5SDimitry Andric   /// getSubExpr - Get the child of this expression.
getAPFloat()720b57cec5SDimitry Andric   APFloat getAPFloat() const { return Flt; }
730b57cec5SDimitry Andric 
740b57cec5SDimitry Andric /// @}
750b57cec5SDimitry Andric 
760b57cec5SDimitry Andric   void printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const override;
evaluateAsRelocatableImpl(MCValue & Res,const MCAssembler * Asm,const MCFixup * Fixup)77*0fca6ea1SDimitry Andric   bool evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
780b57cec5SDimitry Andric                                  const MCFixup *Fixup) const override {
790b57cec5SDimitry Andric     return false;
800b57cec5SDimitry Andric   }
visitUsedExpr(MCStreamer & Streamer)810b57cec5SDimitry Andric   void visitUsedExpr(MCStreamer &Streamer) const override {};
findAssociatedFragment()820b57cec5SDimitry Andric   MCFragment *findAssociatedFragment() const override { return nullptr; }
830b57cec5SDimitry Andric 
840b57cec5SDimitry Andric   // There are no TLS NVPTXMCExprs at the moment.
fixELFSymbolsInTLSFixups(MCAssembler & Asm)850b57cec5SDimitry Andric   void fixELFSymbolsInTLSFixups(MCAssembler &Asm) const override {}
860b57cec5SDimitry Andric 
classof(const MCExpr * E)870b57cec5SDimitry Andric   static bool classof(const MCExpr *E) {
880b57cec5SDimitry Andric     return E->getKind() == MCExpr::Target;
890b57cec5SDimitry Andric   }
900b57cec5SDimitry Andric };
910b57cec5SDimitry Andric 
920b57cec5SDimitry Andric /// A wrapper for MCSymbolRefExpr that tells the assembly printer that the
930b57cec5SDimitry Andric /// symbol should be enclosed by generic().
940b57cec5SDimitry Andric class NVPTXGenericMCSymbolRefExpr : public MCTargetExpr {
950b57cec5SDimitry Andric private:
960b57cec5SDimitry Andric   const MCSymbolRefExpr *SymExpr;
970b57cec5SDimitry Andric 
NVPTXGenericMCSymbolRefExpr(const MCSymbolRefExpr * _SymExpr)980b57cec5SDimitry Andric   explicit NVPTXGenericMCSymbolRefExpr(const MCSymbolRefExpr *_SymExpr)
990b57cec5SDimitry Andric       : SymExpr(_SymExpr) {}
1000b57cec5SDimitry Andric 
1010b57cec5SDimitry Andric public:
1020b57cec5SDimitry Andric   /// @name Construction
1030b57cec5SDimitry Andric   /// @{
1040b57cec5SDimitry Andric 
1050b57cec5SDimitry Andric   static const NVPTXGenericMCSymbolRefExpr
1060b57cec5SDimitry Andric   *create(const MCSymbolRefExpr *SymExpr, MCContext &Ctx);
1070b57cec5SDimitry Andric 
1080b57cec5SDimitry Andric   /// @}
1090b57cec5SDimitry Andric   /// @name Accessors
1100b57cec5SDimitry Andric   /// @{
1110b57cec5SDimitry Andric 
1120b57cec5SDimitry Andric   /// getOpcode - Get the kind of this expression.
getSymbolExpr()1130b57cec5SDimitry Andric   const MCSymbolRefExpr *getSymbolExpr() const { return SymExpr; }
1140b57cec5SDimitry Andric 
1150b57cec5SDimitry Andric   /// @}
1160b57cec5SDimitry Andric 
1170b57cec5SDimitry Andric   void printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const override;
evaluateAsRelocatableImpl(MCValue & Res,const MCAssembler * Asm,const MCFixup * Fixup)118*0fca6ea1SDimitry Andric   bool evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
1190b57cec5SDimitry Andric                                  const MCFixup *Fixup) const override {
1200b57cec5SDimitry Andric     return false;
1210b57cec5SDimitry Andric   }
visitUsedExpr(MCStreamer & Streamer)1220b57cec5SDimitry Andric   void visitUsedExpr(MCStreamer &Streamer) const override {};
findAssociatedFragment()1230b57cec5SDimitry Andric   MCFragment *findAssociatedFragment() const override { return nullptr; }
1240b57cec5SDimitry Andric 
1250b57cec5SDimitry Andric   // There are no TLS NVPTXMCExprs at the moment.
fixELFSymbolsInTLSFixups(MCAssembler & Asm)1260b57cec5SDimitry Andric   void fixELFSymbolsInTLSFixups(MCAssembler &Asm) const override {}
1270b57cec5SDimitry Andric 
classof(const MCExpr * E)1280b57cec5SDimitry Andric   static bool classof(const MCExpr *E) {
1290b57cec5SDimitry Andric     return E->getKind() == MCExpr::Target;
1300b57cec5SDimitry Andric   }
1310b57cec5SDimitry Andric   };
1320b57cec5SDimitry Andric } // end namespace llvm
1330b57cec5SDimitry Andric 
1340b57cec5SDimitry Andric #endif
135