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>>;
|
|
|
|
|
2023-01-20 10:44:49 -08:00
|
|
|
static int getXLen(const Record &Rec) {
|
|
|
|
std::vector<Record *> Features = Rec.getValueAsListOfDefs("Features");
|
|
|
|
if (find_if(Features, [](const Record *R) {
|
|
|
|
return R->getName() == "Feature64Bit";
|
|
|
|
}) != Features.end())
|
|
|
|
return 64;
|
|
|
|
|
|
|
|
return 32;
|
|
|
|
}
|
|
|
|
|
2023-01-16 11:07:34 +08:00
|
|
|
// 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.
|
2023-01-20 10:44:49 -08:00
|
|
|
static std::string getMArch(int XLen, const Record &Rec) {
|
2023-01-16 11:07:34 +08:00
|
|
|
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
|
|
|
|
|
|
|
void llvm::EmitRISCVTargetDef(const RecordKeeper &RK, raw_ostream &OS) {
|
|
|
|
OS << "#ifndef PROC\n"
|
2023-01-20 10:03:37 -08:00
|
|
|
<< "#define PROC(ENUM, NAME, DEFAULT_MARCH)\n"
|
2023-01-11 10:23:55 +01:00
|
|
|
<< "#endif\n\n";
|
|
|
|
|
2023-01-20 10:03:37 -08:00
|
|
|
OS << "PROC(INVALID, {\"invalid\"}, {\"\"})\n";
|
2023-01-11 10:23:55 +01:00
|
|
|
// Iterate on all definition records.
|
2023-01-20 00:20:13 -08:00
|
|
|
for (const Record *Rec : RK.getAllDerivedDefinitions("RISCVProcessorModel")) {
|
2023-01-20 10:44:49 -08:00
|
|
|
int XLen = getXLen(*Rec);
|
2023-01-20 00:20:13 -08:00
|
|
|
std::string MArch = Rec->getValueAsString("DefaultMarch").str();
|
|
|
|
|
|
|
|
// Compute MArch from features if we don't specify it.
|
|
|
|
if (MArch.empty())
|
2023-01-20 10:44:49 -08:00
|
|
|
MArch = getMArch(XLen, *Rec);
|
2023-01-20 00:20:13 -08:00
|
|
|
|
|
|
|
OS << "PROC(" << Rec->getName() << ", "
|
2023-01-20 10:03:37 -08:00
|
|
|
<< "{\"" << Rec->getValueAsString("Name") << "\"}, "
|
2023-01-20 00:20:13 -08:00
|
|
|
<< "{\"" << 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";
|
2023-01-20 00:20:13 -08:00
|
|
|
|
|
|
|
for (const Record *Rec :
|
|
|
|
RK.getAllDerivedDefinitions("RISCVTuneProcessorModel")) {
|
|
|
|
OS << "TUNE_PROC(" << Rec->getName() << ", "
|
|
|
|
<< "\"" << Rec->getValueAsString("Name") << "\")\n";
|
2023-01-11 10:23:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
OS << "\n#undef TUNE_PROC\n";
|
|
|
|
}
|