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();
31 if (DefName.size() <= 4 ||
32 std::string(DefName.begin(), DefName.begin()+4) != "int_")
33 throw "Intrinsic '" + DefName + "' does not start with 'int_'!";
34 EnumName = std::string(DefName.begin()+4, DefName.end());
36 Name = R->getValueAsString("LLVMName");
38 // If an explicit name isn't specified, derive one from the DefName.
40 for (unsigned i = 0, e = EnumName.size(); i != e; ++i)
41 if (EnumName[i] == '_')
47 // Parse the list of argument types.
48 ListInit *TypeList = R->getValueAsListInit("Types");
49 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
50 DefInit *DI = dynamic_cast<DefInit*>(TypeList->getElement(i));
51 assert(DI && "Invalid list type!");
52 Record *TyEl = DI->getDef();
53 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
54 ArgTypes.push_back(TyEl->getValueAsString("TypeVal"));
56 if (ArgTypes.size() == 0)
57 throw "Intrinsic '"+DefName+"' needs at least a type for the ret value!";
59 // Parse the intrinsic properties.
60 ListInit *PropList = R->getValueAsListInit("Properties");
61 for (unsigned i = 0, e = PropList->getSize(); i != e; ++i) {
62 DefInit *DI = dynamic_cast<DefInit*>(PropList->getElement(i));
63 assert(DI && "Invalid list type!");
64 Record *Property = DI->getDef();
65 assert(Property->isSubClassOf("IntrinsicProperty") &&
66 "Expected a property!");
68 if (Property->getName() == "InstrNoMem")
70 else if (Property->getName() == "InstrReadArgMem")
72 else if (Property->getName() == "IntrReadMem")
74 else if (Property->getName() == "InstrWriteArgMem")
76 else if (Property->getName() == "IntrWriteMem")
79 assert(0 && "Unknown property!");
83 //===----------------------------------------------------------------------===//
84 // IntrinsicEmitter Implementation
85 //===----------------------------------------------------------------------===//
87 void IntrinsicEmitter::run(std::ostream &OS) {
88 EmitSourceFileHeader("Intrinsic Function Source Fragment", OS);
90 std::vector<CodeGenIntrinsic> Ints = LoadIntrinsics(Records);
92 // Emit the enum information.
93 EmitEnumInfo(Ints, OS);
95 // Emit the function name recognizer.
96 EmitFnNameRecognizer(Ints, OS);
98 // Emit the intrinsic verifier.
99 EmitVerifier(Ints, OS);
101 // Emit mod/ref info for each function.
102 EmitModRefInfo(Ints, OS);
105 void IntrinsicEmitter::EmitEnumInfo(const std::vector<CodeGenIntrinsic> &Ints,
107 OS << "// Enum values for Intrinsics.h\n";
108 OS << "#ifdef GET_INTRINSIC_ENUM_VALUES\n";
109 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
110 OS << " " << Ints[i].EnumName;
111 OS << ((i != e-1) ? ", " : " ");
112 OS << std::string(40-Ints[i].EnumName.size(), ' ')
113 << "// " << Ints[i].Name << "\n";
118 void IntrinsicEmitter::
119 EmitFnNameRecognizer(const std::vector<CodeGenIntrinsic> &Ints,
121 // Build a function name -> intrinsic name mapping.
122 std::map<std::string, std::string> IntMapping;
123 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
124 IntMapping[Ints[i].Name] = Ints[i].EnumName;
126 OS << "// Function name -> enum value recognizer code.\n";
127 OS << "#ifdef GET_FUNCTION_RECOGNIZER\n";
128 OS << " switch (Name[5]) {\n";
129 OS << " default: break;\n";
130 // Emit the intrinsics in sorted order.
132 for (std::map<std::string, std::string>::iterator I = IntMapping.begin(),
133 E = IntMapping.end(); I != E; ++I) {
134 assert(I->first.size() > 5 && std::string(I->first.begin(),
135 I->first.begin()+5) == "llvm." &&
136 "Invalid intrinsic name!");
137 if (I->first[5] != LastChar) {
138 LastChar = I->first[5];
139 OS << " case '" << LastChar << "':\n";
142 OS << " if (Name == \"" << I->first << "\") return Intrinsic::"
143 << I->second << ";\n";
146 OS << " // The 'llvm.' namespace is reserved!\n";
147 OS << " assert(0 && \"Unknown LLVM intrinsic function!\");\n";
151 void IntrinsicEmitter::EmitVerifier(const std::vector<CodeGenIntrinsic> &Ints,
153 OS << "// Verifier::visitIntrinsicFunctionCall code.\n";
154 OS << "#ifdef GET_INTRINSIC_VERIFIER\n";
155 OS << " switch (ID) {\n";
156 OS << " default: assert(0 && \"Invalid intrinsic!\");\n";
157 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
158 OS << " case Intrinsic::" << Ints[i].EnumName << ":\t\t// "
159 << Ints[i].Name << "\n";
160 OS << " Assert1(FTy->getNumParams() == " << Ints[i].ArgTypes.size()-1
162 << " \"Illegal # arguments for intrinsic function!\", IF);\n";
163 OS << " Assert1(FTy->getReturnType()->getTypeID() == "
164 << Ints[i].ArgTypes[0] << ",\n"
165 << " \"Illegal result type!\", IF);\n";
166 for (unsigned j = 1; j != Ints[i].ArgTypes.size(); ++j)
167 OS << " Assert1(FTy->getParamType(" << j-1 << ")->getTypeID() == "
168 << Ints[i].ArgTypes[j] << ",\n"
169 << " \"Illegal result type!\", IF);\n";
176 void IntrinsicEmitter::EmitModRefInfo(const std::vector<CodeGenIntrinsic> &Ints,
178 OS << "// BasicAliasAnalysis code.\n";
179 OS << "#ifdef GET_MODREF_BEHAVIOR\n";
180 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
181 switch (Ints[i].ModRef) {
183 case CodeGenIntrinsic::NoMem:
184 OS << " NoMemoryTable.push_back(\"" << Ints[i].Name << "\");\n";
186 case CodeGenIntrinsic::ReadArgMem:
187 case CodeGenIntrinsic::ReadMem:
188 OS << " OnlyReadsMemoryTable.push_back(\"" << Ints[i].Name << "\");\n";