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