2017-08-10 08:46:15 +08:00
|
|
|
//===- AMDKernelCodeTUtils.cpp --------------------------------------------===//
|
2016-03-06 21:27:13 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// 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
|
2016-03-06 21:27:13 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
/// \file - utility functions to parse/print amd_kernel_code_t structure
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "AMDKernelCodeTUtils.h"
|
|
|
|
#include "SIDefines.h"
|
2017-08-10 08:46:15 +08:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
|
|
#include "llvm/ADT/StringMap.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/MC/MCParser/MCAsmLexer.h"
|
|
|
|
#include "llvm/MC/MCParser/MCAsmParser.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <utility>
|
2016-03-06 21:27:13 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
static ArrayRef<StringRef> get_amd_kernel_code_t_FldNames() {
|
|
|
|
static StringRef const Table[] = {
|
|
|
|
"", // not found placeholder
|
2016-09-09 18:08:02 +08:00
|
|
|
#define RECORD(name, altName, print, parse) #name
|
2016-03-06 21:27:13 +08:00
|
|
|
#include "AMDKernelCodeTInfo.h"
|
|
|
|
#undef RECORD
|
|
|
|
};
|
|
|
|
return makeArrayRef(Table);
|
|
|
|
}
|
|
|
|
|
2016-09-09 18:08:02 +08:00
|
|
|
static ArrayRef<StringRef> get_amd_kernel_code_t_FldAltNames() {
|
|
|
|
static StringRef const Table[] = {
|
|
|
|
"", // not found placeholder
|
|
|
|
#define RECORD(name, altName, print, parse) #altName
|
|
|
|
#include "AMDKernelCodeTInfo.h"
|
|
|
|
#undef RECORD
|
|
|
|
};
|
|
|
|
return makeArrayRef(Table);
|
|
|
|
}
|
|
|
|
|
|
|
|
static StringMap<int> createIndexMap(const ArrayRef<StringRef> &names,
|
|
|
|
const ArrayRef<StringRef> &altNames) {
|
2016-03-06 21:27:13 +08:00
|
|
|
StringMap<int> map;
|
2016-09-09 18:08:02 +08:00
|
|
|
assert(names.size() == altNames.size());
|
|
|
|
for (unsigned i = 0; i < names.size(); ++i) {
|
|
|
|
map.insert(std::make_pair(names[i], i));
|
|
|
|
map.insert(std::make_pair(altNames[i], i));
|
|
|
|
}
|
2016-03-06 23:13:54 +08:00
|
|
|
return map;
|
2016-03-06 21:27:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int get_amd_kernel_code_t_FieldIndex(StringRef name) {
|
2016-09-09 18:08:02 +08:00
|
|
|
static const auto map = createIndexMap(get_amd_kernel_code_t_FldNames(),
|
|
|
|
get_amd_kernel_code_t_FldAltNames());
|
2016-03-06 21:27:13 +08:00
|
|
|
return map.lookup(name) - 1; // returns -1 if not found
|
|
|
|
}
|
|
|
|
|
|
|
|
static StringRef get_amd_kernel_code_t_FieldName(int index) {
|
|
|
|
return get_amd_kernel_code_t_FldNames()[index + 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Field printing
|
|
|
|
|
2016-04-07 18:10:09 +08:00
|
|
|
static raw_ostream &printName(raw_ostream &OS, StringRef Name) {
|
2016-03-06 21:27:13 +08:00
|
|
|
return OS << Name << " = ";
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, T amd_kernel_code_t::*ptr>
|
2016-04-07 18:10:09 +08:00
|
|
|
static void printField(StringRef Name, const amd_kernel_code_t &C,
|
|
|
|
raw_ostream &OS) {
|
2016-03-06 21:27:13 +08:00
|
|
|
printName(OS, Name) << (int)(C.*ptr);
|
|
|
|
}
|
|
|
|
|
2016-04-07 18:10:09 +08:00
|
|
|
template <typename T, T amd_kernel_code_t::*ptr, int shift, int width = 1>
|
|
|
|
static void printBitField(StringRef Name, const amd_kernel_code_t &c,
|
|
|
|
raw_ostream &OS) {
|
2016-03-06 21:27:13 +08:00
|
|
|
const auto Mask = (static_cast<T>(1) << width) - 1;
|
|
|
|
printName(OS, Name) << (int)((c.*ptr >> shift) & Mask);
|
|
|
|
}
|
|
|
|
|
2017-08-10 08:46:15 +08:00
|
|
|
using PrintFx = void(*)(StringRef, const amd_kernel_code_t &, raw_ostream &);
|
2016-03-06 21:27:13 +08:00
|
|
|
|
|
|
|
static ArrayRef<PrintFx> getPrinterTable() {
|
|
|
|
static const PrintFx Table[] = {
|
2016-09-09 18:08:02 +08:00
|
|
|
#define RECORD(name, altName, print, parse) print
|
2016-03-06 21:27:13 +08:00
|
|
|
#include "AMDKernelCodeTInfo.h"
|
|
|
|
#undef RECORD
|
|
|
|
};
|
|
|
|
return makeArrayRef(Table);
|
|
|
|
}
|
|
|
|
|
2016-06-15 21:55:09 +08:00
|
|
|
void llvm::printAmdKernelCodeField(const amd_kernel_code_t &C,
|
2016-03-06 21:27:13 +08:00
|
|
|
int FldIndex,
|
2016-06-15 21:55:09 +08:00
|
|
|
raw_ostream &OS) {
|
2016-03-06 21:27:13 +08:00
|
|
|
auto Printer = getPrinterTable()[FldIndex];
|
|
|
|
if (Printer)
|
|
|
|
Printer(get_amd_kernel_code_t_FieldName(FldIndex), C, OS);
|
|
|
|
}
|
|
|
|
|
2016-06-15 21:55:09 +08:00
|
|
|
void llvm::dumpAmdKernelCode(const amd_kernel_code_t *C,
|
|
|
|
raw_ostream &OS,
|
|
|
|
const char *tab) {
|
2016-03-06 21:27:13 +08:00
|
|
|
const int Size = getPrinterTable().size();
|
|
|
|
for (int i = 0; i < Size; ++i) {
|
|
|
|
OS << tab;
|
|
|
|
printAmdKernelCodeField(*C, i, OS);
|
|
|
|
OS << '\n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Field parsing
|
|
|
|
|
2016-06-23 22:13:06 +08:00
|
|
|
static bool expectAbsExpression(MCAsmParser &MCParser, int64_t &Value, raw_ostream& Err) {
|
|
|
|
|
|
|
|
if (MCParser.getLexer().isNot(AsmToken::Equal)) {
|
2016-03-06 21:27:13 +08:00
|
|
|
Err << "expected '='";
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-23 22:13:06 +08:00
|
|
|
MCParser.getLexer().Lex();
|
|
|
|
|
|
|
|
if (MCParser.parseAbsoluteExpression(Value)) {
|
|
|
|
Err << "integer absolute expression expected";
|
2016-03-06 21:27:13 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, T amd_kernel_code_t::*ptr>
|
2016-06-23 22:13:06 +08:00
|
|
|
static bool parseField(amd_kernel_code_t &C, MCAsmParser &MCParser,
|
2016-04-07 18:10:09 +08:00
|
|
|
raw_ostream &Err) {
|
2016-06-23 22:13:06 +08:00
|
|
|
int64_t Value = 0;
|
|
|
|
if (!expectAbsExpression(MCParser, Value, Err))
|
2016-03-06 21:27:13 +08:00
|
|
|
return false;
|
2016-06-23 22:13:06 +08:00
|
|
|
C.*ptr = (T)Value;
|
2016-03-06 21:27:13 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, T amd_kernel_code_t::*ptr, int shift, int width = 1>
|
2016-06-23 22:13:06 +08:00
|
|
|
static bool parseBitField(amd_kernel_code_t &C, MCAsmParser &MCParser,
|
2016-04-07 18:10:09 +08:00
|
|
|
raw_ostream &Err) {
|
2016-06-23 22:13:06 +08:00
|
|
|
int64_t Value = 0;
|
|
|
|
if (!expectAbsExpression(MCParser, Value, Err))
|
2016-03-06 21:27:13 +08:00
|
|
|
return false;
|
|
|
|
const uint64_t Mask = ((UINT64_C(1) << width) - 1) << shift;
|
|
|
|
C.*ptr &= (T)~Mask;
|
2016-06-23 22:13:06 +08:00
|
|
|
C.*ptr |= (T)((Value << shift) & Mask);
|
2016-03-06 21:27:13 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-08-10 08:46:15 +08:00
|
|
|
using ParseFx = bool(*)(amd_kernel_code_t &, MCAsmParser &MCParser,
|
|
|
|
raw_ostream &Err);
|
2016-03-06 21:27:13 +08:00
|
|
|
|
|
|
|
static ArrayRef<ParseFx> getParserTable() {
|
|
|
|
static const ParseFx Table[] = {
|
2016-09-09 18:08:02 +08:00
|
|
|
#define RECORD(name, altName, print, parse) parse
|
2016-03-06 21:27:13 +08:00
|
|
|
#include "AMDKernelCodeTInfo.h"
|
|
|
|
#undef RECORD
|
|
|
|
};
|
|
|
|
return makeArrayRef(Table);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool llvm::parseAmdKernelCodeField(StringRef ID,
|
2016-06-23 22:13:06 +08:00
|
|
|
MCAsmParser &MCParser,
|
2016-06-15 21:55:09 +08:00
|
|
|
amd_kernel_code_t &C,
|
|
|
|
raw_ostream &Err) {
|
2016-03-06 21:27:13 +08:00
|
|
|
const int Idx = get_amd_kernel_code_t_FieldIndex(ID);
|
|
|
|
if (Idx < 0) {
|
|
|
|
Err << "unexpected amd_kernel_code_t field name " << ID;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto Parser = getParserTable()[Idx];
|
2016-06-23 22:13:06 +08:00
|
|
|
return Parser ? Parser(C, MCParser, Err) : false;
|
2016-03-06 21:27:13 +08:00
|
|
|
}
|