Add MachineInstr::readsVirtualRegister() in preparation for proper handling of
[oota-llvm.git] / utils / TableGen / ClangASTNodesEmitter.cpp
1 //=== ClangASTNodesEmitter.cpp - Generate Clang AST node tables -*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // These tablegen backends emit Clang AST node tables
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "ClangASTNodesEmitter.h"
15 #include "Record.h"
16 #include <map>
17 #include <cctype>
18 using namespace llvm;
19
20 //===----------------------------------------------------------------------===//
21 // Statement Node Tables (.inc file) generation.
22 //===----------------------------------------------------------------------===//
23
24 // Create a macro-ized version of a name
25 static std::string macroName(std::string S) {
26   for (unsigned i = 0; i < S.size(); ++i)
27     S[i] = std::toupper(S[i]);
28
29   return S;
30 }
31
32 // A map from a node to each of its derived nodes.
33 typedef std::multimap<Record*, Record*> ChildMap;
34 typedef ChildMap::const_iterator ChildIterator;
35
36 // Returns the first and last non-abstract subrecords
37 // Called recursively to ensure that nodes remain contiguous
38 static std::pair<Record *, Record *> EmitStmtNode(const ChildMap &Tree,
39                                                   raw_ostream &OS,
40                                                   Record *Base,
41                                                   bool Root = true) {
42   std::string BaseName = macroName(Base->getName());
43
44   ChildIterator i = Tree.lower_bound(Base), e = Tree.upper_bound(Base);
45
46   Record *First = 0, *Last = 0;
47   // This might be the pseudo-node for Stmt; don't assume it has an Abstract
48   // bit
49   if (Base->getValue("Abstract") && !Base->getValueAsBit("Abstract"))
50     First = Last = Base;
51
52   for (; i != e; ++i) {
53     Record *R = i->second;
54     bool Abstract = R->getValueAsBit("Abstract");
55     std::string NodeName = macroName(R->getName());
56
57     OS << "#ifndef " << NodeName << "\n";
58     OS << "#  define " << NodeName << "(Type, Base) "
59         << BaseName << "(Type, Base)\n";
60     OS << "#endif\n";
61
62     if (Abstract)
63       OS << "ABSTRACT_STMT(" << NodeName << "(" << R->getName() << ", "
64           << Base->getName() << "))\n";
65     else
66       OS << NodeName << "(" << R->getName() << ", "
67           << Base->getName() << ")\n";
68
69     if (Tree.find(R) != Tree.end()) {
70       const std::pair<Record *, Record *> &Result
71         = EmitStmtNode(Tree, OS, R, false);
72       if (!First && Result.first)
73         First = Result.first;
74       if (Result.second)
75         Last = Result.second;
76     } else {
77       if (!Abstract) {
78         Last = R;
79
80         if (!First)
81           First = R;
82       }
83     }
84
85     OS << "#undef " << NodeName << "\n\n";
86   }
87
88   if (First) {
89     assert (Last && "Got a first node but not a last node for a range!");
90     if (Root)
91       OS << "LAST_STMT_RANGE(";
92     else
93       OS << "STMT_RANGE(";
94  
95     OS << Base->getName() << ", " << First->getName() << ", "
96        << Last->getName() << ")\n\n";
97   }
98
99   return std::make_pair(First, Last);
100 }
101
102 void ClangStmtNodesEmitter::run(raw_ostream &OS) {
103   // Write the preamble
104   OS << "#ifndef ABSTRACT_STMT\n";
105   OS << "#  define ABSTRACT_STMT(Stmt) Stmt\n";
106   OS << "#endif\n";
107
108   OS << "#ifndef STMT_RANGE\n";
109   OS << "#  define STMT_RANGE(Base, First, Last)\n";
110   OS << "#endif\n\n";
111
112   OS << "#ifndef LAST_STMT_RANGE\n";
113   OS << "#  define LAST_STMT_RANGE(Base, First, Last) "
114           "STMT_RANGE(Base, First, Last)\n";
115   OS << "#endif\n\n";
116  
117   // Emit statements
118   const std::vector<Record*> Stmts = Records.getAllDerivedDefinitions("Stmt");
119
120   ChildMap Tree;
121
122   // Create a pseudo-record to serve as the Stmt node, which isn't actually
123   // output.
124   Record Stmt ("Stmt", SMLoc());
125
126   for (unsigned i = 0, e = Stmts.size(); i != e; ++i) {
127     Record *R = Stmts[i];
128
129     if (R->getValue("Base"))
130       Tree.insert(std::make_pair(R->getValueAsDef("Base"), R));
131     else
132       Tree.insert(std::make_pair(&Stmt, R));
133   }
134
135   EmitStmtNode(Tree, OS, &Stmt);
136
137   OS << "#undef STMT\n";
138   OS << "#undef STMT_RANGE\n";
139   OS << "#undef LAST_STMT_RANGE\n";
140   OS << "#undef ABSTRACT_STMT\n";
141 }