Remove dead code.
[oota-llvm.git] / utils / TableGen / AsmWriterInst.cpp
1 //===- AsmWriterInst.h - Classes encapsulating a printable inst -----------===//
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 classes implement a parser for assembly strings.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "AsmWriterInst.h"
15 #include "CodeGenTarget.h"
16 #include "llvm/ADT/StringExtras.h"
17 #include "llvm/TableGen/Error.h"
18 #include "llvm/TableGen/Record.h"
19
20 using namespace llvm;
21
22 static bool isIdentChar(char C) {
23   return (C >= 'a' && C <= 'z') ||
24   (C >= 'A' && C <= 'Z') ||
25   (C >= '0' && C <= '9') ||
26   C == '_';
27 }
28
29 std::string AsmWriterOperand::getCode() const {
30   if (OperandType == isLiteralTextOperand) {
31     if (Str.size() == 1)
32       return "O << '" + Str + "'; ";
33     return "O << \"" + Str + "\"; ";
34   }
35
36   if (OperandType == isLiteralStatementOperand)
37     return Str;
38
39   std::string Result = Str + "(MI";
40   if (MIOpNo != ~0U)
41     Result += ", " + utostr(MIOpNo);
42   Result += ", O";
43   if (!MiModifier.empty())
44     Result += ", \"" + MiModifier + '"';
45   return Result + "); ";
46 }
47
48 /// ParseAsmString - Parse the specified Instruction's AsmString into this
49 /// AsmWriterInst.
50 ///
51 AsmWriterInst::AsmWriterInst(const CodeGenInstruction &CGI,
52                              unsigned Variant,
53                              int OperandSpacing) {
54   this->CGI = &CGI;
55
56   // NOTE: Any extensions to this code need to be mirrored in the
57   // AsmPrinter::printInlineAsm code that executes as compile time (assuming
58   // that inline asm strings should also get the new feature)!
59   std::string AsmString = CGI.FlattenAsmStringVariants(CGI.AsmString, Variant);
60   std::string::size_type LastEmitted = 0;
61   while (LastEmitted != AsmString.size()) {
62     std::string::size_type DollarPos =
63       AsmString.find_first_of("$\\", LastEmitted);
64     if (DollarPos == std::string::npos) DollarPos = AsmString.size();
65
66     // Emit a constant string fragment.
67     if (DollarPos != LastEmitted) {
68       for (; LastEmitted != DollarPos; ++LastEmitted)
69         switch (AsmString[LastEmitted]) {
70           case '\n':
71             AddLiteralString("\\n");
72             break;
73           case '\t':
74             AddLiteralString("\\t");
75             break;
76           case '"':
77             AddLiteralString("\\\"");
78             break;
79           case '\\':
80             AddLiteralString("\\\\");
81             break;
82           default:
83             AddLiteralString(std::string(1, AsmString[LastEmitted]));
84             break;
85         }
86     } else if (AsmString[DollarPos] == '\\') {
87       if (DollarPos+1 != AsmString.size()) {
88         if (AsmString[DollarPos+1] == 'n') {
89           AddLiteralString("\\n");
90         } else if (AsmString[DollarPos+1] == 't') {
91           AddLiteralString("\\t");
92         } else if (std::string("${|}\\").find(AsmString[DollarPos+1])
93                    != std::string::npos) {
94           AddLiteralString(std::string(1, AsmString[DollarPos+1]));
95         } else {
96           PrintFatalError("Non-supported escaped character found in instruction '" +
97             CGI.TheDef->getName() + "'!");
98         }
99         LastEmitted = DollarPos+2;
100         continue;
101       }
102     } else if (DollarPos+1 != AsmString.size() &&
103                AsmString[DollarPos+1] == '$') {
104       AddLiteralString("$");  // "$$" -> $
105       LastEmitted = DollarPos+2;
106     } else {
107       // Get the name of the variable.
108       std::string::size_type VarEnd = DollarPos+1;
109
110       // handle ${foo}bar as $foo by detecting whether the character following
111       // the dollar sign is a curly brace.  If so, advance VarEnd and DollarPos
112       // so the variable name does not contain the leading curly brace.
113       bool hasCurlyBraces = false;
114       if (VarEnd < AsmString.size() && '{' == AsmString[VarEnd]) {
115         hasCurlyBraces = true;
116         ++DollarPos;
117         ++VarEnd;
118       }
119
120       while (VarEnd < AsmString.size() && isIdentChar(AsmString[VarEnd]))
121         ++VarEnd;
122       std::string VarName(AsmString.begin()+DollarPos+1,
123                           AsmString.begin()+VarEnd);
124
125       // Modifier - Support ${foo:modifier} syntax, where "modifier" is passed
126       // into printOperand.  Also support ${:feature}, which is passed into
127       // PrintSpecial.
128       std::string Modifier;
129
130       // In order to avoid starting the next string at the terminating curly
131       // brace, advance the end position past it if we found an opening curly
132       // brace.
133       if (hasCurlyBraces) {
134         if (VarEnd >= AsmString.size())
135           PrintFatalError("Reached end of string before terminating curly brace in '"
136             + CGI.TheDef->getName() + "'");
137
138         // Look for a modifier string.
139         if (AsmString[VarEnd] == ':') {
140           ++VarEnd;
141           if (VarEnd >= AsmString.size())
142             PrintFatalError("Reached end of string before terminating curly brace in '"
143               + CGI.TheDef->getName() + "'");
144
145           unsigned ModifierStart = VarEnd;
146           while (VarEnd < AsmString.size() && isIdentChar(AsmString[VarEnd]))
147             ++VarEnd;
148           Modifier = std::string(AsmString.begin()+ModifierStart,
149                                  AsmString.begin()+VarEnd);
150           if (Modifier.empty())
151             PrintFatalError("Bad operand modifier name in '"+ CGI.TheDef->getName() + "'");
152         }
153
154         if (AsmString[VarEnd] != '}')
155           PrintFatalError("Variable name beginning with '{' did not end with '}' in '"
156             + CGI.TheDef->getName() + "'");
157         ++VarEnd;
158       }
159       if (VarName.empty() && Modifier.empty())
160         PrintFatalError("Stray '$' in '" + CGI.TheDef->getName() +
161           "' asm string, maybe you want $$?");
162
163       if (VarName.empty()) {
164         // Just a modifier, pass this into PrintSpecial.
165         Operands.push_back(AsmWriterOperand("PrintSpecial",
166                                             ~0U,
167                                             ~0U,
168                                             Modifier));
169       } else {
170         // Otherwise, normal operand.
171         unsigned OpNo = CGI.Operands.getOperandNamed(VarName);
172         CGIOperandList::OperandInfo OpInfo = CGI.Operands[OpNo];
173
174         unsigned MIOp = OpInfo.MIOperandNo;
175         Operands.push_back(AsmWriterOperand(OpInfo.PrinterMethodName,
176                                             OpNo, MIOp, Modifier));
177       }
178       LastEmitted = VarEnd;
179     }
180   }
181
182   Operands.push_back(AsmWriterOperand("return;",
183     AsmWriterOperand::isLiteralStatementOperand));
184 }
185
186 /// MatchesAllButOneOp - If this instruction is exactly identical to the
187 /// specified instruction except for one differing operand, return the differing
188 /// operand number.  If more than one operand mismatches, return ~1, otherwise
189 /// if the instructions are identical return ~0.
190 unsigned AsmWriterInst::MatchesAllButOneOp(const AsmWriterInst &Other)const{
191   if (Operands.size() != Other.Operands.size()) return ~1;
192
193   unsigned MismatchOperand = ~0U;
194   for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
195     if (Operands[i] != Other.Operands[i]) {
196       if (MismatchOperand != ~0U)  // Already have one mismatch?
197         return ~1U;
198       MismatchOperand = i;
199     }
200   }
201   return MismatchOperand;
202 }