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