LLVM  4.0.0
AMDKernelCodeTUtils.cpp
Go to the documentation of this file.
1 //===--------------------AMDKernelCodeTUtils.cpp --------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //===----------------------------------------------------------------------===//
11 //
12 /// \file - utility functions to parse/print amd_kernel_code_t structure
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #include "AMDKernelCodeTUtils.h"
17 #include "SIDefines.h"
21 
22 using namespace llvm;
23 
24 static ArrayRef<StringRef> get_amd_kernel_code_t_FldNames() {
25  static StringRef const Table[] = {
26  "", // not found placeholder
27 #define RECORD(name, altName, print, parse) #name
28 #include "AMDKernelCodeTInfo.h"
29 #undef RECORD
30  };
31  return makeArrayRef(Table);
32 }
33 
34 static ArrayRef<StringRef> get_amd_kernel_code_t_FldAltNames() {
35  static StringRef const Table[] = {
36  "", // not found placeholder
37 #define RECORD(name, altName, print, parse) #altName
38 #include "AMDKernelCodeTInfo.h"
39 #undef RECORD
40  };
41  return makeArrayRef(Table);
42 }
43 
45  const ArrayRef<StringRef> &altNames) {
46  StringMap<int> map;
47  assert(names.size() == altNames.size());
48  for (unsigned i = 0; i < names.size(); ++i) {
49  map.insert(std::make_pair(names[i], i));
50  map.insert(std::make_pair(altNames[i], i));
51  }
52  return map;
53 }
54 
56  static const auto map = createIndexMap(get_amd_kernel_code_t_FldNames(),
57  get_amd_kernel_code_t_FldAltNames());
58  return map.lookup(name) - 1; // returns -1 if not found
59 }
60 
62  return get_amd_kernel_code_t_FldNames()[index + 1];
63 }
64 
65 
66 // Field printing
67 
69  return OS << Name << " = ";
70 }
71 
72 template <typename T, T amd_kernel_code_t::*ptr>
74  raw_ostream &OS) {
75  printName(OS, Name) << (int)(C.*ptr);
76 }
77 
78 template <typename T, T amd_kernel_code_t::*ptr, int shift, int width = 1>
80  raw_ostream &OS) {
81  const auto Mask = (static_cast<T>(1) << width) - 1;
82  printName(OS, Name) << (int)((c.*ptr >> shift) & Mask);
83 }
84 
85 typedef void(*PrintFx)(StringRef,
86  const amd_kernel_code_t &,
87  raw_ostream &);
88 
89 static ArrayRef<PrintFx> getPrinterTable() {
90  static const PrintFx Table[] = {
91 #define RECORD(name, altName, print, parse) print
92 #include "AMDKernelCodeTInfo.h"
93 #undef RECORD
94  };
95  return makeArrayRef(Table);
96 }
97 
99  int FldIndex,
100  raw_ostream &OS) {
101  auto Printer = getPrinterTable()[FldIndex];
102  if (Printer)
103  Printer(get_amd_kernel_code_t_FieldName(FldIndex), C, OS);
104 }
105 
107  raw_ostream &OS,
108  const char *tab) {
109  const int Size = getPrinterTable().size();
110  for (int i = 0; i < Size; ++i) {
111  OS << tab;
112  printAmdKernelCodeField(*C, i, OS);
113  OS << '\n';
114  }
115 }
116 
117 
118 // Field parsing
119 
120 static bool expectAbsExpression(MCAsmParser &MCParser, int64_t &Value, raw_ostream& Err) {
121 
122  if (MCParser.getLexer().isNot(AsmToken::Equal)) {
123  Err << "expected '='";
124  return false;
125  }
126  MCParser.getLexer().Lex();
127 
128  if (MCParser.parseAbsoluteExpression(Value)) {
129  Err << "integer absolute expression expected";
130  return false;
131  }
132  return true;
133 }
134 
135 template <typename T, T amd_kernel_code_t::*ptr>
136 static bool parseField(amd_kernel_code_t &C, MCAsmParser &MCParser,
137  raw_ostream &Err) {
138  int64_t Value = 0;
139  if (!expectAbsExpression(MCParser, Value, Err))
140  return false;
141  C.*ptr = (T)Value;
142  return true;
143 }
144 
145 template <typename T, T amd_kernel_code_t::*ptr, int shift, int width = 1>
147  raw_ostream &Err) {
148  int64_t Value = 0;
149  if (!expectAbsExpression(MCParser, Value, Err))
150  return false;
151  const uint64_t Mask = ((UINT64_C(1) << width) - 1) << shift;
152  C.*ptr &= (T)~Mask;
153  C.*ptr |= (T)((Value << shift) & Mask);
154  return true;
155 }
156 
158  MCAsmParser &MCParser,
159  raw_ostream &Err);
160 
161 static ArrayRef<ParseFx> getParserTable() {
162  static const ParseFx Table[] = {
163 #define RECORD(name, altName, print, parse) parse
164 #include "AMDKernelCodeTInfo.h"
165 #undef RECORD
166  };
167  return makeArrayRef(Table);
168 }
169 
171  MCAsmParser &MCParser,
173  raw_ostream &Err) {
174  const int Idx = get_amd_kernel_code_t_FieldIndex(ID);
175  if (Idx < 0) {
176  Err << "unexpected amd_kernel_code_t field name " << ID;
177  return false;
178  }
179  auto Parser = getParserTable()[Idx];
180  return Parser ? Parser(C, MCParser, Err) : false;
181 }
static StringMap< int > createIndexMap(const ArrayRef< StringRef > &names, const ArrayRef< StringRef > &altNames)
size_t i
Generic assembler parser interface, for use by target specific assembly parsers.
Definition: MCAsmParser.h:66
print alias Alias Set Printer
bool parseAmdKernelCodeField(StringRef ID, MCAsmParser &Parser, amd_kernel_code_t &C, raw_ostream &Err)
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:440
AMD Kernel Code Object (amd_kernel_code_t).
static bool parseBitField(amd_kernel_code_t &C, MCAsmParser &MCParser, raw_ostream &Err)
bool isNot(AsmToken::TokenKind K) const
Check if the current token has kind K.
Definition: MCAsmLexer.h:245
#define T
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
static void printField(StringRef Name, const amd_kernel_code_t &C, raw_ostream &OS)
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:141
static void printBitField(StringRef Name, const amd_kernel_code_t &c, raw_ostream &OS)
virtual MCAsmLexer & getLexer()=0
static raw_ostream & printName(raw_ostream &OS, StringRef Name)
void printAmdKernelCodeField(const amd_kernel_code_t &C, int FldIndex, raw_ostream &OS)
static int get_amd_kernel_code_t_FieldIndex(StringRef name)
static bool expectAbsExpression(MCAsmParser &MCParser, int64_t &Value, raw_ostream &Err)
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
Definition: StringMap.h:348
static GCRegistry::Add< ShadowStackGC > C("shadow-stack","Very portable GC for uncooperative code generators")
const AsmToken & Lex()
Consume the next token from the input stream and return it.
Definition: MCAsmLexer.h:180
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
Definition: StringMap.h:223
bool(* ParseFx)(amd_kernel_code_t &, MCAsmParser &MCParser, raw_ostream &Err)
static StringRef get_amd_kernel_code_t_FieldName(int index)
void(* PrintFx)(StringRef, const amd_kernel_code_t &, raw_ostream &)
virtual bool parseAbsoluteExpression(int64_t &Res)=0
Parse an expression which must evaluate to an absolute value.
void dumpAmdKernelCode(const amd_kernel_code_t *C, raw_ostream &OS, const char *tab)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:71
static const char * name
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
Definition: BitmaskEnum.h:81
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
static bool parseField(amd_kernel_code_t &C, MCAsmParser &MCParser, raw_ostream &Err)