1 //===-- ClangOpcodesEmitter.cpp - constexpr interpreter opcodes -----------===// 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-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // These tablegen backends emit Clang AST node tables 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "TableGenBackends.h" 14 #include "llvm/TableGen/Error.h" 15 #include "llvm/TableGen/Record.h" 16 #include "llvm/TableGen/StringMatcher.h" 17 #include "llvm/TableGen/TableGenBackend.h" 18 19 using namespace llvm; 20 21 namespace { 22 class ClangOpcodesEmitter { 23 const RecordKeeper &Records; 24 unsigned NumTypes; 25 26 public: 27 ClangOpcodesEmitter(const RecordKeeper &R) 28 : Records(R), NumTypes(Records.getAllDerivedDefinitions("Type").size()) {} 29 30 void run(raw_ostream &OS); 31 32 private: 33 /// Emits the opcode name for the opcode enum. 34 /// The name is obtained by concatenating the name with the list of types. 35 void EmitEnum(raw_ostream &OS, StringRef N, const Record *R); 36 37 /// Emits the switch case and the invocation in the interpreter. 38 void EmitInterp(raw_ostream &OS, StringRef N, const Record *R); 39 40 /// Emits the disassembler. 41 void EmitDisasm(raw_ostream &OS, StringRef N, const Record *R); 42 43 /// Emits the byte code emitter method. 44 void EmitEmitter(raw_ostream &OS, StringRef N, const Record *R); 45 46 /// Emits the prototype. 47 void EmitProto(raw_ostream &OS, StringRef N, const Record *R); 48 49 /// Emits the prototype to dispatch from a type. 50 void EmitGroup(raw_ostream &OS, StringRef N, const Record *R); 51 52 /// Emits the evaluator method. 53 void EmitEval(raw_ostream &OS, StringRef N, const Record *R); 54 55 void PrintTypes(raw_ostream &OS, ArrayRef<const Record *> Types); 56 }; 57 58 void Enumerate(const Record *R, StringRef N, 59 std::function<void(ArrayRef<const Record *>, Twine)> &&F) { 60 SmallVector<const Record *, 2> TypePath; 61 const auto *Types = R->getValueAsListInit("Types"); 62 63 std::function<void(size_t, const Twine &)> Rec; 64 Rec = [&TypePath, Types, &Rec, &F](size_t I, const Twine &ID) { 65 if (I >= Types->size()) { 66 F(TypePath, ID); 67 return; 68 } 69 70 if (const auto *TypeClass = dyn_cast<DefInit>(Types->getElement(I))) { 71 for (const auto *Type : 72 TypeClass->getDef()->getValueAsListOfDefs("Types")) { 73 TypePath.push_back(Type); 74 Rec(I + 1, ID + Type->getName()); 75 TypePath.pop_back(); 76 } 77 } else { 78 PrintFatalError("Expected a type class"); 79 } 80 }; 81 Rec(0, N); 82 } 83 84 } // namespace 85 86 void ClangOpcodesEmitter::run(raw_ostream &OS) { 87 for (const auto *Opcode : Records.getAllDerivedDefinitions("Opcode")) { 88 // The name is the record name, unless overriden. 89 StringRef N = Opcode->getValueAsString("Name"); 90 if (N.empty()) 91 N = Opcode->getName(); 92 93 EmitEnum(OS, N, Opcode); 94 EmitInterp(OS, N, Opcode); 95 EmitDisasm(OS, N, Opcode); 96 EmitProto(OS, N, Opcode); 97 EmitGroup(OS, N, Opcode); 98 EmitEmitter(OS, N, Opcode); 99 EmitEval(OS, N, Opcode); 100 } 101 } 102 103 void ClangOpcodesEmitter::EmitEnum(raw_ostream &OS, StringRef N, 104 const Record *R) { 105 OS << "#ifdef GET_OPCODE_NAMES\n"; 106 Enumerate(R, N, [&OS](ArrayRef<const Record *>, const Twine &ID) { 107 OS << "OP_" << ID << ",\n"; 108 }); 109 OS << "#endif\n"; 110 } 111 112 void ClangOpcodesEmitter::EmitInterp(raw_ostream &OS, StringRef N, 113 const Record *R) { 114 OS << "#ifdef GET_INTERP\n"; 115 116 Enumerate(R, N, 117 [this, R, &OS, &N](ArrayRef<const Record *> TS, const Twine &ID) { 118 bool CanReturn = R->getValueAsBit("CanReturn"); 119 bool ChangesPC = R->getValueAsBit("ChangesPC"); 120 const auto &Args = R->getValueAsListOfDefs("Args"); 121 122 OS << "case OP_" << ID << ": {\n"; 123 124 if (CanReturn) 125 OS << " bool DoReturn = (S.Current == StartFrame);\n"; 126 127 // Emit calls to read arguments. 128 for (size_t I = 0, N = Args.size(); I < N; ++I) { 129 const auto *Arg = Args[I]; 130 bool AsRef = Arg->getValueAsBit("AsRef"); 131 132 if (AsRef) 133 OS << " const auto &V" << I; 134 else 135 OS << " const auto V" << I; 136 OS << " = "; 137 OS << "ReadArg<" << Arg->getValueAsString("Name") 138 << ">(S, PC);\n"; 139 } 140 141 // Emit a call to the template method and pass arguments. 142 OS << " if (!" << N; 143 PrintTypes(OS, TS); 144 OS << "(S"; 145 if (ChangesPC) 146 OS << ", PC"; 147 else 148 OS << ", OpPC"; 149 for (size_t I = 0, N = Args.size(); I < N; ++I) 150 OS << ", V" << I; 151 OS << "))\n"; 152 OS << " return false;\n"; 153 154 // Bail out if interpreter returned. 155 if (CanReturn) { 156 OS << " if (!S.Current || S.Current->isRoot())\n"; 157 OS << " return true;\n"; 158 159 OS << " if (DoReturn)\n"; 160 OS << " return true;\n"; 161 } 162 163 OS << " continue;\n"; 164 OS << "}\n"; 165 }); 166 OS << "#endif\n"; 167 } 168 169 void ClangOpcodesEmitter::EmitDisasm(raw_ostream &OS, StringRef N, 170 const Record *R) { 171 OS << "#ifdef GET_DISASM\n"; 172 Enumerate(R, N, [R, &OS](ArrayRef<const Record *>, const Twine &ID) { 173 OS << "case OP_" << ID << ":\n"; 174 OS << " PrintName(\"" << ID << "\");\n"; 175 OS << " OS << \"\\t\""; 176 177 for (const auto *Arg : R->getValueAsListOfDefs("Args")) { 178 OS << " << ReadArg<" << Arg->getValueAsString("Name") << ">(P, PC)"; 179 OS << " << \" \""; 180 } 181 182 OS << " << \"\\n\";\n"; 183 OS << " continue;\n"; 184 }); 185 OS << "#endif\n"; 186 } 187 188 void ClangOpcodesEmitter::EmitEmitter(raw_ostream &OS, StringRef N, 189 const Record *R) { 190 if (R->getValueAsBit("HasCustomLink")) 191 return; 192 193 OS << "#ifdef GET_LINK_IMPL\n"; 194 Enumerate(R, N, [R, &OS](ArrayRef<const Record *>, const Twine &ID) { 195 const auto &Args = R->getValueAsListOfDefs("Args"); 196 197 // Emit the list of arguments. 198 OS << "bool ByteCodeEmitter::emit" << ID << "("; 199 for (size_t I = 0, N = Args.size(); I < N; ++I) { 200 const auto *Arg = Args[I]; 201 bool AsRef = Arg->getValueAsBit("AsRef"); 202 auto Name = Arg->getValueAsString("Name"); 203 204 OS << (AsRef ? "const " : " ") << Name << " " << (AsRef ? "&" : "") << "A" 205 << I << ", "; 206 } 207 OS << "const SourceInfo &L) {\n"; 208 209 // Emit a call to write the opcodes. 210 OS << " return emitOp<"; 211 for (size_t I = 0, N = Args.size(); I < N; ++I) { 212 if (I != 0) 213 OS << ", "; 214 OS << Args[I]->getValueAsString("Name"); 215 } 216 OS << ">(OP_" << ID; 217 for (size_t I = 0, N = Args.size(); I < N; ++I) 218 OS << ", A" << I; 219 OS << ", L);\n"; 220 OS << "}\n"; 221 }); 222 OS << "#endif\n"; 223 } 224 225 void ClangOpcodesEmitter::EmitProto(raw_ostream &OS, StringRef N, 226 const Record *R) { 227 OS << "#if defined(GET_EVAL_PROTO) || defined(GET_LINK_PROTO)\n"; 228 auto Args = R->getValueAsListOfDefs("Args"); 229 Enumerate(R, N, [&OS, &Args](ArrayRef<const Record *> TS, const Twine &ID) { 230 OS << "bool emit" << ID << "("; 231 for (size_t I = 0, N = Args.size(); I < N; ++I) { 232 const auto *Arg = Args[I]; 233 bool AsRef = Arg->getValueAsBit("AsRef"); 234 auto Name = Arg->getValueAsString("Name"); 235 236 OS << (AsRef ? "const " : " ") << Name << " " << (AsRef ? "&" : "") 237 << ", "; 238 } 239 OS << "const SourceInfo &);\n"; 240 }); 241 242 // Emit a template method for custom emitters to have less to implement. 243 auto TypeCount = R->getValueAsListInit("Types")->size(); 244 if (R->getValueAsBit("HasCustomEval") && TypeCount) { 245 OS << "#if defined(GET_EVAL_PROTO)\n"; 246 OS << "template<"; 247 for (size_t I = 0; I < TypeCount; ++I) { 248 if (I != 0) 249 OS << ", "; 250 OS << "PrimType"; 251 } 252 OS << ">\n"; 253 OS << "bool emit" << N << "("; 254 for (const auto *Arg : Args) 255 OS << Arg->getValueAsString("Name") << ", "; 256 OS << "const SourceInfo &);\n"; 257 OS << "#endif\n"; 258 } 259 260 OS << "#endif\n"; 261 } 262 263 void ClangOpcodesEmitter::EmitGroup(raw_ostream &OS, StringRef N, 264 const Record *R) { 265 if (!R->getValueAsBit("HasGroup")) 266 return; 267 268 const auto *Types = R->getValueAsListInit("Types"); 269 const auto &Args = R->getValueAsListOfDefs("Args"); 270 271 Twine EmitFuncName = "emit" + N; 272 273 // Emit the prototype of the group emitter in the header. 274 OS << "#if defined(GET_EVAL_PROTO) || defined(GET_LINK_PROTO)\n"; 275 OS << "[[nodiscard]] bool " << EmitFuncName << "("; 276 for (size_t I = 0, N = Types->size(); I < N; ++I) 277 OS << "PrimType, "; 278 for (auto *Arg : Args) 279 OS << Arg->getValueAsString("Name") << ", "; 280 OS << "const SourceInfo &I);\n"; 281 OS << "#endif\n"; 282 283 // Emit the dispatch implementation in the source. 284 OS << "#if defined(GET_EVAL_IMPL) || defined(GET_LINK_IMPL)\n"; 285 OS << "bool\n"; 286 OS << "#if defined(GET_EVAL_IMPL)\n"; 287 OS << "EvalEmitter\n"; 288 OS << "#else\n"; 289 OS << "ByteCodeEmitter\n"; 290 OS << "#endif\n"; 291 OS << "::" << EmitFuncName << "("; 292 for (size_t I = 0, N = Types->size(); I < N; ++I) 293 OS << "PrimType T" << I << ", "; 294 for (size_t I = 0, N = Args.size(); I < N; ++I) { 295 const auto *Arg = Args[I]; 296 bool AsRef = Arg->getValueAsBit("AsRef"); 297 auto Name = Arg->getValueAsString("Name"); 298 299 OS << (AsRef ? "const " : " ") << Name << " " << (AsRef ? "&" : "") << "A" 300 << I << ", "; 301 } 302 OS << "const SourceInfo &I) {\n"; 303 304 std::function<void(size_t, const Twine &)> Rec; 305 SmallVector<const Record *, 2> TS; 306 Rec = [this, &Rec, &OS, Types, &Args, R, &TS, N, 307 EmitFuncName](size_t I, const Twine &ID) { 308 if (I >= Types->size()) { 309 // Print a call to the emitter method. 310 // Custom evaluator methods dispatch to template methods. 311 if (R->getValueAsBit("HasCustomEval")) { 312 OS << "#ifdef GET_LINK_IMPL\n"; 313 OS << " return emit" << ID << "\n"; 314 OS << "#else\n"; 315 OS << " return emit" << N; 316 PrintTypes(OS, TS); 317 OS << "\n#endif\n"; 318 OS << " "; 319 } else { 320 OS << " return emit" << ID; 321 } 322 323 OS << "("; 324 for (size_t I = 0; I < Args.size(); ++I) { 325 OS << "A" << I << ", "; 326 } 327 OS << "I);\n"; 328 return; 329 } 330 331 // Print a switch statement selecting T. 332 if (auto *TypeClass = dyn_cast<DefInit>(Types->getElement(I))) { 333 OS << " switch (T" << I << ") {\n"; 334 auto Cases = TypeClass->getDef()->getValueAsListOfDefs("Types"); 335 for (auto *Case : Cases) { 336 OS << " case PT_" << Case->getName() << ":\n"; 337 TS.push_back(Case); 338 Rec(I + 1, ID + Case->getName()); 339 TS.pop_back(); 340 } 341 // Emit a default case if not all types are present. 342 if (Cases.size() < NumTypes) 343 OS << " default: llvm_unreachable(\"invalid type: " << EmitFuncName 344 << "\");\n"; 345 OS << " }\n"; 346 OS << " llvm_unreachable(\"invalid enum value\");\n"; 347 } else { 348 PrintFatalError("Expected a type class"); 349 } 350 }; 351 Rec(0, N); 352 353 OS << "}\n"; 354 OS << "#endif\n"; 355 } 356 357 void ClangOpcodesEmitter::EmitEval(raw_ostream &OS, StringRef N, 358 const Record *R) { 359 if (R->getValueAsBit("HasCustomEval")) 360 return; 361 362 OS << "#ifdef GET_EVAL_IMPL\n"; 363 Enumerate(R, N, 364 [this, R, &N, &OS](ArrayRef<const Record *> TS, const Twine &ID) { 365 auto Args = R->getValueAsListOfDefs("Args"); 366 367 OS << "bool EvalEmitter::emit" << ID << "("; 368 for (size_t I = 0, N = Args.size(); I < N; ++I) { 369 const auto *Arg = Args[I]; 370 bool AsRef = Arg->getValueAsBit("AsRef"); 371 auto Name = Arg->getValueAsString("Name"); 372 373 OS << (AsRef ? "const " : " ") << Name << " " 374 << (AsRef ? "&" : "") << "A" << I << ", "; 375 } 376 OS << "const SourceInfo &L) {\n"; 377 OS << " if (!isActive()) return true;\n"; 378 OS << " CurrentSource = L;\n"; 379 380 OS << " return " << N; 381 PrintTypes(OS, TS); 382 OS << "(S, OpPC"; 383 for (size_t I = 0, N = Args.size(); I < N; ++I) 384 OS << ", A" << I; 385 OS << ");\n"; 386 OS << "}\n"; 387 }); 388 389 OS << "#endif\n"; 390 } 391 392 void ClangOpcodesEmitter::PrintTypes(raw_ostream &OS, 393 ArrayRef<const Record *> Types) { 394 if (Types.empty()) 395 return; 396 OS << "<"; 397 for (size_t I = 0, N = Types.size(); I < N; ++I) { 398 if (I != 0) 399 OS << ", "; 400 OS << "PT_" << Types[I]->getName(); 401 } 402 OS << ">"; 403 } 404 405 void clang::EmitClangOpcodes(const RecordKeeper &Records, raw_ostream &OS) { 406 ClangOpcodesEmitter(Records).run(OS); 407 } 408