902c261c8802b83ac98db4ec5e4d802d86b12cc7
[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   
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());
34   
35   Name = R->getValueAsString("LLVMName");
36   if (Name == "") {
37     // If an explicit name isn't specified, derive one from the DefName.
38     Name = "llvm.";
39     for (unsigned i = 0, e = EnumName.size(); i != e; ++i)
40       if (EnumName[i] == '_')
41         Name += '.';
42       else
43         Name += EnumName[i];
44   }
45 }
46
47 //===----------------------------------------------------------------------===//
48 // IntrinsicEmitter Implementation
49 //===----------------------------------------------------------------------===//
50
51 void IntrinsicEmitter::run(std::ostream &OS) {
52   EmitSourceFileHeader("Intrinsic Function Source Fragment", OS);
53   
54   std::vector<CodeGenIntrinsic> Ints = LoadIntrinsics(Records);
55
56   // Emit the enum information.
57   EmitEnumInfo(Ints, OS);
58   
59   // Emit the function name recognizer.
60   EmitFnNameRecognizer(Ints, OS);
61 }
62
63 void IntrinsicEmitter::EmitEnumInfo(const std::vector<CodeGenIntrinsic> &Ints,
64                                     std::ostream &OS) {
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";
72   }
73   OS << "#endif\n\n";
74 }
75
76 void IntrinsicEmitter::
77 EmitFnNameRecognizer(const std::vector<CodeGenIntrinsic> &Ints, 
78                      std::ostream &OS) {
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;
83     
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.
90   char LastChar = 0;
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";
99     }
100     
101     OS << "    if (Name == \"" << I->first << "\") return Intrinsic::"
102        << I->second << ";\n";
103   }
104   OS << "  }\n";
105   OS << "#endif\n";
106 }
107