Add a newline at the end to avoid gcc warnings.
[oota-llvm.git] / utils / TableGen / IntrinsicEmitter.cpp
1 //===- IntrinsicEmitter.cpp - Generate intrinsic information --------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This tablegen backend emits information about intrinsic functions.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "IntrinsicEmitter.h"
15 #include "Record.h"
16 #include "llvm/ADT/StringExtras.h"
17 using namespace llvm;
18
19 //===----------------------------------------------------------------------===//
20 // CodeGenIntrinsic Implementation
21 //===----------------------------------------------------------------------===//
22
23 std::vector<CodeGenIntrinsic> llvm::LoadIntrinsics(const RecordKeeper &RC) {
24   std::vector<Record*> I = RC.getAllDerivedDefinitions("Intrinsic");
25   return std::vector<CodeGenIntrinsic>(I.begin(), I.end());
26 }
27
28 CodeGenIntrinsic::CodeGenIntrinsic(Record *R) {
29   std::string DefName = R->getName();
30   ModRef = WriteMem;
31   
32   if (DefName.size() <= 4 || 
33       std::string(DefName.begin(), DefName.begin()+4) != "int_")
34     throw "Intrinsic '" + DefName + "' does not start with 'int_'!";
35   EnumName = std::string(DefName.begin()+4, DefName.end());
36   GCCBuiltinName = R->getValueAsString("GCCBuiltinName");
37   
38   Name = R->getValueAsString("LLVMName");
39   if (Name == "") {
40     // If an explicit name isn't specified, derive one from the DefName.
41     Name = "llvm.";
42     for (unsigned i = 0, e = EnumName.size(); i != e; ++i)
43       if (EnumName[i] == '_')
44         Name += '.';
45       else
46         Name += EnumName[i];
47   }
48   
49   // Parse the list of argument types.
50   ListInit *TypeList = R->getValueAsListInit("Types");
51   for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
52     DefInit *DI = dynamic_cast<DefInit*>(TypeList->getElement(i));
53     assert(DI && "Invalid list type!");
54     Record *TyEl = DI->getDef();
55     assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
56     ArgTypes.push_back(TyEl->getValueAsString("TypeVal"));
57     ArgTypeDefs.push_back(TyEl);
58   }
59   if (ArgTypes.size() == 0)
60     throw "Intrinsic '"+DefName+"' needs at least a type for the ret value!";
61   
62   // Parse the intrinsic properties.
63   ListInit *PropList = R->getValueAsListInit("Properties");
64   for (unsigned i = 0, e = PropList->getSize(); i != e; ++i) {
65     DefInit *DI = dynamic_cast<DefInit*>(PropList->getElement(i));
66     assert(DI && "Invalid list type!");
67     Record *Property = DI->getDef();
68     assert(Property->isSubClassOf("IntrinsicProperty") &&
69            "Expected a property!");
70
71     if (Property->getName() == "InstrNoMem")
72       ModRef = NoMem;
73     else if (Property->getName() == "InstrReadArgMem")
74       ModRef = ReadArgMem;
75     else if (Property->getName() == "IntrReadMem")
76       ModRef = ReadMem;
77     else if (Property->getName() == "InstrWriteArgMem")
78       ModRef = WriteArgMem;
79     else if (Property->getName() == "IntrWriteMem")
80       ModRef = WriteMem;
81     else
82       assert(0 && "Unknown property!");
83   }
84 }
85
86 //===----------------------------------------------------------------------===//
87 // IntrinsicEmitter Implementation
88 //===----------------------------------------------------------------------===//
89
90 void IntrinsicEmitter::run(std::ostream &OS) {
91   EmitSourceFileHeader("Intrinsic Function Source Fragment", OS);
92   
93   std::vector<CodeGenIntrinsic> Ints = LoadIntrinsics(Records);
94
95   // Emit the enum information.
96   EmitEnumInfo(Ints, OS);
97   
98   // Emit the function name recognizer.
99   EmitFnNameRecognizer(Ints, OS);
100
101   // Emit the intrinsic verifier.
102   EmitVerifier(Ints, OS);
103   
104   // Emit mod/ref info for each function.
105   EmitModRefInfo(Ints, OS);
106   
107   // Emit side effect info for each function.
108   EmitSideEffectInfo(Ints, OS);
109
110   // Emit a list of intrinsics with corresponding GCC builtins.
111   EmitGCCBuiltinList(Ints, OS);
112 }
113
114 void IntrinsicEmitter::EmitEnumInfo(const std::vector<CodeGenIntrinsic> &Ints,
115                                     std::ostream &OS) {
116   OS << "// Enum values for Intrinsics.h\n";
117   OS << "#ifdef GET_INTRINSIC_ENUM_VALUES\n";
118   for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
119     OS << "    " << Ints[i].EnumName;
120     OS << ((i != e-1) ? ", " : "  ");
121     OS << std::string(40-Ints[i].EnumName.size(), ' ') 
122       << "// " << Ints[i].Name << "\n";
123   }
124   OS << "#endif\n\n";
125 }
126
127 void IntrinsicEmitter::
128 EmitFnNameRecognizer(const std::vector<CodeGenIntrinsic> &Ints, 
129                      std::ostream &OS) {
130   // Build a function name -> intrinsic name mapping.
131   std::map<std::string, std::string> IntMapping;
132   for (unsigned i = 0, e = Ints.size(); i != e; ++i)
133     IntMapping[Ints[i].Name] = Ints[i].EnumName;
134     
135   OS << "// Function name -> enum value recognizer code.\n";
136   OS << "#ifdef GET_FUNCTION_RECOGNIZER\n";
137   OS << "  switch (Name[5]) {\n";
138   OS << "  default: break;\n";
139   // Emit the intrinsics in sorted order.
140   char LastChar = 0;
141   for (std::map<std::string, std::string>::iterator I = IntMapping.begin(),
142        E = IntMapping.end(); I != E; ++I) {
143     assert(I->first.size() > 5 && std::string(I->first.begin(),
144                                               I->first.begin()+5) == "llvm." &&
145            "Invalid intrinsic name!");
146     if (I->first[5] != LastChar) {
147       LastChar = I->first[5];
148       OS << "  case '" << LastChar << "':\n";
149     }
150     
151     OS << "    if (Name == \"" << I->first << "\") return Intrinsic::"
152        << I->second << ";\n";
153   }
154   OS << "  }\n";
155   OS << "  // The 'llvm.' namespace is reserved!\n";
156   OS << "  assert(0 && \"Unknown LLVM intrinsic function!\");\n";
157   OS << "#endif\n\n";
158 }
159
160 static void EmitTypeVerify(std::ostream &OS, const std::string &Val,
161                            Record *ArgType) {
162   OS << "    Assert1(" << Val << "->getTypeID() == "
163      << ArgType->getValueAsString("TypeVal") << ",\n"
164      << "            \"Illegal intrinsic type!\", IF);\n";
165
166   // If this is a packed type, check that the subtype and size are correct.
167   if (ArgType->isSubClassOf("LLVMPackedType")) {
168     Record *SubType = ArgType->getValueAsDef("ElTy");
169     OS << "    Assert1(cast<PackedType>(" << Val
170        << ")->getElementType()->getTypeID() == "
171        << SubType->getValueAsString("TypeVal") << ",\n"
172        << "            \"Illegal intrinsic type!\", IF);\n";
173     OS << "    Assert1(cast<PackedType>(" << Val << ")->getNumElements() == "
174        << ArgType->getValueAsInt("NumElts") << ",\n"
175        << "            \"Illegal intrinsic type!\", IF);\n";
176   }
177 }
178
179 void IntrinsicEmitter::EmitVerifier(const std::vector<CodeGenIntrinsic> &Ints, 
180                                     std::ostream &OS) {
181   OS << "// Verifier::visitIntrinsicFunctionCall code.\n";
182   OS << "#ifdef GET_INTRINSIC_VERIFIER\n";
183   OS << "  switch (ID) {\n";
184   OS << "  default: assert(0 && \"Invalid intrinsic!\");\n";
185   for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
186     OS << "  case Intrinsic::" << Ints[i].EnumName << ":\t\t// "
187        << Ints[i].Name << "\n";
188     OS << "    Assert1(FTy->getNumParams() == " << Ints[i].ArgTypes.size()-1
189        << ",\n"
190        << "            \"Illegal # arguments for intrinsic function!\", IF);\n";
191     EmitTypeVerify(OS, "FTy->getReturnType()", Ints[i].ArgTypeDefs[0]);
192     for (unsigned j = 1; j != Ints[i].ArgTypes.size(); ++j)
193       EmitTypeVerify(OS, "FTy->getParamType(" + utostr(j-1) + ")",
194                      Ints[i].ArgTypeDefs[j]);
195     OS << "    break;\n";
196   }
197   OS << "  }\n";
198   OS << "#endif\n\n";
199 }
200
201 void IntrinsicEmitter::EmitModRefInfo(const std::vector<CodeGenIntrinsic> &Ints,
202                                       std::ostream &OS) {
203   OS << "// BasicAliasAnalysis code.\n";
204   OS << "#ifdef GET_MODREF_BEHAVIOR\n";
205   for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
206     switch (Ints[i].ModRef) {
207     default: break;
208     case CodeGenIntrinsic::NoMem:
209       OS << "  NoMemoryTable.push_back(\"" << Ints[i].Name << "\");\n";
210       break;
211     case CodeGenIntrinsic::ReadArgMem:
212     case CodeGenIntrinsic::ReadMem:
213       OS << "  OnlyReadsMemoryTable.push_back(\"" << Ints[i].Name << "\");\n";
214       break;
215     }
216   }
217   OS << "#endif\n\n";
218 }
219
220 void IntrinsicEmitter::
221 EmitSideEffectInfo(const std::vector<CodeGenIntrinsic> &Ints, std::ostream &OS){
222   OS << "// isInstructionTriviallyDead code.\n";
223   OS << "#ifdef GET_SIDE_EFFECT_INFO\n";
224   OS << "  switch (F->getIntrinsicID()) {\n";
225   OS << "  default: break;\n";
226   for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
227     switch (Ints[i].ModRef) {
228     default: break;
229     case CodeGenIntrinsic::NoMem:
230     case CodeGenIntrinsic::ReadArgMem:
231     case CodeGenIntrinsic::ReadMem:
232       OS << "  case Intrinsic::" << Ints[i].EnumName << ":\n";
233       break;
234     }
235   }
236   OS << "    return true; // These intrinsics have no side effects.\n";
237   OS << "  }\n";
238   OS << "#endif\n\n";
239 }
240
241 void IntrinsicEmitter::
242 EmitGCCBuiltinList(const std::vector<CodeGenIntrinsic> &Ints, std::ostream &OS){
243   OS << "// Get the GCC builtin that corresponds to an LLVM intrinsic.\n";
244   OS << "#ifdef GET_GCC_BUILTIN_NAME\n";
245   OS << "  switch (F->getIntrinsicID()) {\n";
246   OS << "  default: BuiltinName = \"\"; break;\n";
247   for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
248     if (!Ints[i].GCCBuiltinName.empty()) {
249       OS << "  case Intrinsic::" << Ints[i].EnumName << ": BuiltinName = \""
250          << Ints[i].GCCBuiltinName << "\"; break;\n";
251     }
252   }
253   OS << "  }\n";
254   OS << "#endif\n\n";
255 }