680264ff20bf3bee8dea6602185ab2811cf43f76
[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 using namespace llvm;
17
18 //===----------------------------------------------------------------------===//
19 // CodeGenIntrinsic Implementation
20 //===----------------------------------------------------------------------===//
21
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());
25 }
26
27 CodeGenIntrinsic::CodeGenIntrinsic(Record *R) {
28   std::string DefName = R->getName();
29   ModRef = WriteMem;
30   
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());
35   
36   Name = R->getValueAsString("LLVMName");
37   if (Name == "") {
38     // If an explicit name isn't specified, derive one from the DefName.
39     Name = "llvm.";
40     for (unsigned i = 0, e = EnumName.size(); i != e; ++i)
41       if (EnumName[i] == '_')
42         Name += '.';
43       else
44         Name += EnumName[i];
45   }
46   
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"));
55   }
56   if (ArgTypes.size() == 0)
57     throw "Intrinsic '"+DefName+"' needs at least a type for the ret value!";
58   
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!");
67
68     if (Property->getName() == "InstrNoMem")
69       ModRef = NoMem;
70     else if (Property->getName() == "InstrReadArgMem")
71       ModRef = ReadArgMem;
72     else if (Property->getName() == "IntrReadMem")
73       ModRef = ReadMem;
74     else if (Property->getName() == "InstrWriteArgMem")
75       ModRef = WriteArgMem;
76     else if (Property->getName() == "IntrWriteMem")
77       ModRef = WriteMem;
78     else
79       assert(0 && "Unknown property!");
80   }
81 }
82
83 //===----------------------------------------------------------------------===//
84 // IntrinsicEmitter Implementation
85 //===----------------------------------------------------------------------===//
86
87 void IntrinsicEmitter::run(std::ostream &OS) {
88   EmitSourceFileHeader("Intrinsic Function Source Fragment", OS);
89   
90   std::vector<CodeGenIntrinsic> Ints = LoadIntrinsics(Records);
91
92   // Emit the enum information.
93   EmitEnumInfo(Ints, OS);
94   
95   // Emit the function name recognizer.
96   EmitFnNameRecognizer(Ints, OS);
97
98   // Emit the intrinsic verifier.
99   EmitVerifier(Ints, OS);
100   
101   // Emit mod/ref info for each function.
102   EmitModRefInfo(Ints, OS);
103 }
104
105 void IntrinsicEmitter::EmitEnumInfo(const std::vector<CodeGenIntrinsic> &Ints,
106                                     std::ostream &OS) {
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";
114   }
115   OS << "#endif\n\n";
116 }
117
118 void IntrinsicEmitter::
119 EmitFnNameRecognizer(const std::vector<CodeGenIntrinsic> &Ints, 
120                      std::ostream &OS) {
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;
125     
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.
131   char LastChar = 0;
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";
140     }
141     
142     OS << "    if (Name == \"" << I->first << "\") return Intrinsic::"
143        << I->second << ";\n";
144   }
145   OS << "  }\n";
146   OS << "  // The 'llvm.' namespace is reserved!\n";
147   OS << "  assert(0 && \"Unknown LLVM intrinsic function!\");\n";
148   OS << "#endif\n\n";
149 }
150
151 void IntrinsicEmitter::EmitVerifier(const std::vector<CodeGenIntrinsic> &Ints, 
152                                     std::ostream &OS) {
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
161        << ",\n"
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";
170     OS << "    break;\n";
171   }
172   OS << "  }\n";
173   OS << "#endif\n\n";
174 }
175
176 void IntrinsicEmitter::EmitModRefInfo(const std::vector<CodeGenIntrinsic> &Ints,
177                                       std::ostream &OS) {
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) {
182     default: break;
183     case CodeGenIntrinsic::NoMem:
184       OS << "  NoMemoryTable.push_back(\"" << Ints[i].Name << "\");\n";
185       break;
186     case CodeGenIntrinsic::ReadArgMem:
187     case CodeGenIntrinsic::ReadMem:
188       OS << "  OnlyReadsMemoryTable.push_back(\"" << Ints[i].Name << "\");\n";
189       break;
190     }
191   }
192   OS << "#endif\n\n";
193 }