1 //===- IntrinsicEmitter.cpp - Generate intrinsic information --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Chris Lattner and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This tablegen backend emits information about intrinsic functions.
12 //===----------------------------------------------------------------------===//
14 #include "IntrinsicEmitter.h"
18 //===----------------------------------------------------------------------===//
19 // CodeGenIntrinsic Implementation
20 //===----------------------------------------------------------------------===//
22 std::vector<CodeGenIntrinsic> llvm::LoadIntrinsics(const RecordKeeper &RC) {
23 std::vector<Record*> I = RC.getAllDerivedDefinitions("Intrinsic");
24 return std::vector<CodeGenIntrinsic>(I.begin(), I.end());
27 CodeGenIntrinsic::CodeGenIntrinsic(Record *R) {
28 std::string DefName = R->getName();
30 if (DefName.size() <= 4 ||
31 std::string(DefName.begin(), DefName.begin()+4) != "int_")
32 throw "Intrinsic '" + DefName + "' does not start with 'int_'!";
33 EnumName = std::string(DefName.begin()+4, DefName.end());
35 Name = R->getValueAsString("LLVMName");
37 // If an explicit name isn't specified, derive one from the DefName.
39 for (unsigned i = 0, e = EnumName.size(); i != e; ++i)
40 if (EnumName[i] == '_')
47 //===----------------------------------------------------------------------===//
48 // IntrinsicEmitter Implementation
49 //===----------------------------------------------------------------------===//
51 void IntrinsicEmitter::run(std::ostream &OS) {
52 EmitSourceFileHeader("Intrinsic Function Source Fragment", OS);
54 std::vector<CodeGenIntrinsic> Ints = LoadIntrinsics(Records);
56 // Emit the enum information.
57 EmitEnumInfo(Ints, OS);
59 // Emit the function name recognizer.
60 EmitFnNameRecognizer(Ints, OS);
63 void IntrinsicEmitter::EmitEnumInfo(const std::vector<CodeGenIntrinsic> &Ints,
65 OS << "// Enum values for Intrinsics.h\n";
66 OS << "#ifdef GET_INTRINSIC_ENUM_VALUES\n";
67 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
68 OS << " " << Ints[i].EnumName;
69 OS << ((i != e-1) ? ", " : " ");
70 OS << std::string(40-Ints[i].EnumName.size(), ' ')
71 << "// " << Ints[i].Name << "\n";
76 void IntrinsicEmitter::
77 EmitFnNameRecognizer(const std::vector<CodeGenIntrinsic> &Ints,
79 // Build a function name -> intrinsic name mapping.
80 std::map<std::string, std::string> IntMapping;
81 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
82 IntMapping[Ints[i].Name] = Ints[i].EnumName;
84 OS << "// Function name -> enum value recognizer code.\n";
85 OS << "#ifdef GET_FUNCTION_RECOGNIZER\n";
86 OS << " switch (Name[5]) {\n";
87 OS << " // The 'llvm.' namespace is reserved!\n";
88 OS << " default: assert(0 && \"Unknown LLVM intrinsic function!\");\n";
89 // Emit the intrinsics in sorted order.
91 for (std::map<std::string, std::string>::iterator I = IntMapping.begin(),
92 E = IntMapping.end(); I != E; ++I) {
93 assert(I->first.size() > 5 && std::string(I->first.begin(),
94 I->first.begin()+5) == "llvm." &&
95 "Invalid intrinsic name!");
96 if (I->first[5] != LastChar) {
97 LastChar = I->first[5];
98 OS << " case '" << LastChar << "':\n";
101 OS << " if (Name == \"" << I->first << "\") return Intrinsic::"
102 << I->second << ";\n";