brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.2 KiB · d6b002e Raw
93 lines · cpp
1//===-- RISCVISAUtils.cpp - RISC-V ISA Utilities --------------------------===//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-exception6//7//===----------------------------------------------------------------------===//8//9// Utilities shared by TableGen and RISCVISAInfo.10//11//===----------------------------------------------------------------------===//12 13#include "llvm/Support/RISCVISAUtils.h"14#include "llvm/ADT/StringExtras.h"15#include <cassert>16 17using namespace llvm;18 19// We rank extensions in the following order:20// -Single letter extensions in canonical order.21// -Unknown single letter extensions in alphabetical order.22// -Multi-letter extensions starting with 'z' sorted by canonical order of23//  the second letter then sorted alphabetically.24// -Multi-letter extensions starting with 's' in alphabetical order.25// -(TODO) Multi-letter extensions starting with 'zxm' in alphabetical order.26// -X extensions in alphabetical order.27// -Unknown multi-letter extensions in alphabetical order.28// These flags are used to indicate the category. The first 6 bits store the29// single letter extension rank for single letter and multi-letter extensions30// starting with 'z'.31enum RankFlags {32  RF_Z_EXTENSION = 1 << 6,33  RF_S_EXTENSION = 2 << 6,34  RF_X_EXTENSION = 3 << 6,35  RF_UNKNOWN_MULTILETTER_EXTENSION = 4 << 6,36};37 38// Get the rank for single-letter extension, lower value meaning higher39// priority.40static unsigned singleLetterExtensionRank(char Ext) {41  assert(isLower(Ext));42  switch (Ext) {43  case 'i':44    return 0;45  case 'e':46    return 1;47  }48 49  size_t Pos = RISCVISAUtils::AllStdExts.find(Ext);50  if (Pos != StringRef::npos)51    return Pos + 2; // Skip 'e' and 'i' from above.52 53  // If we got an unknown extension letter, then give it an alphabetical54  // order, but after all known standard extensions.55  return 2 + RISCVISAUtils::AllStdExts.size() + (Ext - 'a');56}57 58// Get the rank for multi-letter extension, lower value meaning higher59// priority/order in canonical order.60static unsigned getExtensionRank(const std::string &ExtName) {61  assert(ExtName.size() >= 1);62  switch (ExtName[0]) {63  case 's':64    return RF_S_EXTENSION;65  case 'z':66    assert(ExtName.size() >= 2);67    // `z` extension must be sorted by canonical order of second letter.68    // e.g. zmx has higher rank than zax.69    return RF_Z_EXTENSION | singleLetterExtensionRank(ExtName[1]);70  case 'x':71    return RF_X_EXTENSION;72  default:73    if (ExtName.size() == 1)74      return singleLetterExtensionRank(ExtName[0]);75    return RF_UNKNOWN_MULTILETTER_EXTENSION;76  }77}78 79// Compare function for extension.80// Only compare the extension name, ignore version comparison.81bool llvm::RISCVISAUtils::compareExtension(const std::string &LHS,82                                           const std::string &RHS) {83  unsigned LHSRank = getExtensionRank(LHS);84  unsigned RHSRank = getExtensionRank(RHS);85 86  // If the ranks differ, pick the lower rank.87  if (LHSRank != RHSRank)88    return LHSRank < RHSRank;89 90  // If the rank is same, it must be sorted by lexicographic order.91  return LHS < RHS;92}93