2023-01-11 10:23:55 +01:00
|
|
|
//===- RISCVTargetDefEmitter.cpp - Generate lists of RISCV CPUs -----------===//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This tablegen backend emits the include file needed by the target
|
|
|
|
// parser to parse the RISC-V CPUs.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "TableGenBackends.h"
|
2023-01-16 11:07:34 +08:00
|
|
|
#include "llvm/Support/RISCVISAInfo.h"
|
2023-01-11 10:23:55 +01:00
|
|
|
#include "llvm/TableGen/Record.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2023-01-16 11:07:34 +08:00
|
|
|
using ISAInfoTy = llvm::Expected<std::unique_ptr<RISCVISAInfo>>;
|
|
|
|
|
|
|
|
static int getXLen(const Record &Rec) {
|
2023-01-11 10:23:55 +01:00
|
|
|
std::vector<Record *> Features = Rec.getValueAsListOfDefs("Features");
|
|
|
|
if (find_if(Features, [](const Record *R) {
|
|
|
|
return R->getName() == "Feature64Bit";
|
|
|
|
}) != Features.end())
|
2023-01-16 11:07:34 +08:00
|
|
|
return 64;
|
|
|
|
|
|
|
|
return 32;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We can generate march string from target features as what has been described
|
|
|
|
// in RISCV ISA specification (version 20191213) 'Chapter 27. ISA Extension
|
|
|
|
// Naming Conventions'.
|
|
|
|
//
|
|
|
|
// This is almost the same as RISCVFeatures::parseFeatureBits, except that we
|
|
|
|
// get feature name from feature records instead of feature bits.
|
|
|
|
static std::string getMArch(int XLen, const Record &Rec) {
|
|
|
|
std::vector<std::string> FeatureVector;
|
|
|
|
|
|
|
|
// Convert features to FeatureVector.
|
|
|
|
for (auto *Feature : Rec.getValueAsListOfDefs("Features")) {
|
|
|
|
StringRef FeatureName = Feature->getValueAsString("Name");
|
|
|
|
if (llvm::RISCVISAInfo::isSupportedExtensionFeature(FeatureName))
|
|
|
|
FeatureVector.push_back((Twine("+") + FeatureName).str());
|
|
|
|
}
|
|
|
|
|
|
|
|
ISAInfoTy ISAInfo = llvm::RISCVISAInfo::parseFeatures(XLen, FeatureVector);
|
|
|
|
if (!ISAInfo)
|
|
|
|
report_fatal_error("Invalid features");
|
|
|
|
|
|
|
|
// RISCVISAInfo::toString will generate a march string with all the extensions
|
|
|
|
// we have added to it.
|
|
|
|
return (*ISAInfo)->toString();
|
|
|
|
}
|
2023-01-11 10:23:55 +01:00
|
|
|
|
2023-01-16 11:07:34 +08:00
|
|
|
static std::string getEnumFeatures(int XLen) {
|
|
|
|
return XLen == 64 ? "FK_64BIT" : "FK_NONE";
|
2023-01-11 10:23:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void llvm::EmitRISCVTargetDef(const RecordKeeper &RK, raw_ostream &OS) {
|
|
|
|
using MapTy = std::pair<const std::string, std::unique_ptr<llvm::Record>>;
|
|
|
|
using RecordMap = std::map<std::string, std::unique_ptr<Record>, std::less<>>;
|
|
|
|
const RecordMap &Map = RK.getDefs();
|
|
|
|
|
|
|
|
OS << "#ifndef PROC\n"
|
|
|
|
<< "#define PROC(ENUM, NAME, FEATURES, DEFAULT_MARCH)\n"
|
|
|
|
<< "#endif\n\n";
|
|
|
|
|
|
|
|
OS << "PROC(INVALID, {\"invalid\"}, FK_INVALID, {\"\"})\n";
|
|
|
|
// Iterate on all definition records.
|
|
|
|
for (const MapTy &Def : Map) {
|
|
|
|
const Record &Rec = *(Def.second);
|
2023-01-16 11:07:34 +08:00
|
|
|
if (Rec.isSubClassOf("RISCVProcessorModel")) {
|
|
|
|
int XLen = getXLen(Rec);
|
|
|
|
std::string MArch = Rec.getValueAsString("DefaultMarch").str();
|
|
|
|
|
|
|
|
// Compute MArch from features if we don't specify it.
|
|
|
|
if (MArch.empty())
|
|
|
|
MArch = getMArch(XLen, Rec);
|
|
|
|
|
2023-01-11 10:23:55 +01:00
|
|
|
OS << "PROC(" << Rec.getName() << ", "
|
|
|
|
<< "{\"" << Rec.getValueAsString("Name") << "\"},"
|
2023-01-16 11:07:34 +08:00
|
|
|
<< getEnumFeatures(XLen) << ", "
|
|
|
|
<< "{\"" << MArch << "\"})\n";
|
|
|
|
}
|
2023-01-11 10:23:55 +01:00
|
|
|
}
|
|
|
|
OS << "\n#undef PROC\n";
|
|
|
|
OS << "\n";
|
|
|
|
OS << "#ifndef TUNE_PROC\n"
|
|
|
|
<< "#define TUNE_PROC(ENUM, NAME)\n"
|
|
|
|
<< "#endif\n\n";
|
|
|
|
OS << "TUNE_PROC(GENERIC, \"generic\")\n";
|
|
|
|
for (const MapTy &Def : Map) {
|
|
|
|
const Record &Rec = *(Def.second);
|
|
|
|
if (Rec.isSubClassOf("RISCVTuneProcessorModel"))
|
|
|
|
OS << "TUNE_PROC(" << Rec.getName() << ", "
|
|
|
|
<< "\"" << Rec.getValueAsString("Name") << "\")\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
OS << "\n#undef TUNE_PROC\n";
|
|
|
|
}
|