1 //===-- RISCVISAInfo.h - RISC-V ISA Information -----------------*- C++ -*-===//
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 #ifndef LLVM_SUPPORT_RISCVISAINFO_H
10 #define LLVM_SUPPORT_RISCVISAINFO_H
11 
12 #include "llvm/ADT/StringMap.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/Support/Error.h"
15 
16 #include <map>
17 #include <string>
18 #include <vector>
19 
20 namespace llvm {
21 void riscvExtensionsHelp(StringMap<StringRef> DescMap);
22 
23 class RISCVISAInfo {
24 public:
25   RISCVISAInfo(const RISCVISAInfo &) = delete;
26   RISCVISAInfo &operator=(const RISCVISAInfo &) = delete;
27 
28   /// Represents the major and version number components of a RISC-V extension.
29   struct ExtensionVersion {
30     unsigned Major;
31     unsigned Minor;
32   };
33 
34   static bool compareExtension(const std::string &LHS, const std::string &RHS);
35 
36   /// Helper class for OrderedExtensionMap.
37   struct ExtensionComparator {
38     bool operator()(const std::string &LHS, const std::string &RHS) const {
39       return compareExtension(LHS, RHS);
40     }
41   };
42 
43   /// OrderedExtensionMap is std::map, it's specialized to keep entries
44   /// in canonical order of extension.
45   typedef std::map<std::string, ExtensionVersion, ExtensionComparator>
46       OrderedExtensionMap;
47 
48   RISCVISAInfo(unsigned XLen, OrderedExtensionMap &Exts)
49       : XLen(XLen), FLen(0), MinVLen(0), MaxELen(0), MaxELenFp(0), Exts(Exts) {}
50 
51   /// Parse RISC-V ISA info from arch string.
52   /// If IgnoreUnknown is set, any unrecognised extension names or
53   /// extensions with unrecognised versions will be silently dropped, except
54   /// for the special case of the base 'i' and 'e' extensions, where the
55   /// default version will be used (as ignoring the base is not possible).
56   static llvm::Expected<std::unique_ptr<RISCVISAInfo>>
57   parseArchString(StringRef Arch, bool EnableExperimentalExtension,
58                   bool ExperimentalExtensionVersionCheck = true,
59                   bool IgnoreUnknown = false);
60 
61   /// Parse RISC-V ISA info from an arch string that is already in normalized
62   /// form (as defined in the psABI). Unlike parseArchString, this function
63   /// will not error for unrecognized extension names or extension versions.
64   static llvm::Expected<std::unique_ptr<RISCVISAInfo>>
65   parseNormalizedArchString(StringRef Arch);
66 
67   /// Parse RISC-V ISA info from feature vector.
68   static llvm::Expected<std::unique_ptr<RISCVISAInfo>>
69   parseFeatures(unsigned XLen, const std::vector<std::string> &Features);
70 
71   /// Convert RISC-V ISA info to a feature vector.
72   std::vector<std::string> toFeatures(bool AddAllExtensions = false,
73                                       bool IgnoreUnknown = true) const;
74 
75   const OrderedExtensionMap &getExtensions() const { return Exts; }
76 
77   unsigned getXLen() const { return XLen; }
78   unsigned getFLen() const { return FLen; }
79   unsigned getMinVLen() const { return MinVLen; }
80   unsigned getMaxVLen() const { return 65536; }
81   unsigned getMaxELen() const { return MaxELen; }
82   unsigned getMaxELenFp() const { return MaxELenFp; }
83 
84   bool hasExtension(StringRef Ext) const;
85   std::string toString() const;
86   StringRef computeDefaultABI() const;
87 
88   static bool isSupportedExtensionFeature(StringRef Ext);
89   static bool isSupportedExtension(StringRef Ext);
90   static bool isSupportedExtensionWithVersion(StringRef Ext);
91   static bool isSupportedExtension(StringRef Ext, unsigned MajorVersion,
92                                    unsigned MinorVersion);
93   static llvm::Expected<std::unique_ptr<RISCVISAInfo>>
94   postProcessAndChecking(std::unique_ptr<RISCVISAInfo> &&ISAInfo);
95   static std::string getTargetFeatureForExtension(StringRef Ext);
96 
97 private:
98   RISCVISAInfo(unsigned XLen)
99       : XLen(XLen), FLen(0), MinVLen(0), MaxELen(0), MaxELenFp(0) {}
100 
101   unsigned XLen;
102   unsigned FLen;
103   unsigned MinVLen;
104   unsigned MaxELen, MaxELenFp;
105 
106   OrderedExtensionMap Exts;
107 
108   void addExtension(StringRef ExtName, ExtensionVersion Version);
109 
110   Error checkDependency();
111 
112   void updateImplication();
113   void updateCombination();
114   void updateFLen();
115   void updateMinVLen();
116   void updateMaxELen();
117 };
118 
119 } // namespace llvm
120 
121 #endif
122