Changed AsmWriterOperand to also include the index of the
[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 "Record.h"
17 #include "llvm/ADT/StringExtras.h"
18
19 using namespace llvm;
20
21 static bool isIdentChar(char C) {
22   return (C >= 'a' && C <= 'z') ||
23   (C >= 'A' && C <= 'Z') ||
24   (C >= '0' && C <= '9') ||
25   C == '_';
26 }
27
28 std::string AsmWriterOperand::getCode() const {
29   if (OperandType == isLiteralTextOperand) {
30     if (Str.size() == 1)
31       return "O << '" + Str + "'; ";
32     return "O << \"" + Str + "\"; ";
33   }
34   
35   if (OperandType == isLiteralStatementOperand)
36     return Str;
37   
38   std::string Result = Str + "(MI";
39   if (MIOpNo != ~0U)
40     Result += ", " + utostr(MIOpNo);
41   if (!MiModifier.empty())
42     Result += ", \"" + MiModifier + '"';
43   return Result + "); ";
44 }
45
46 /// ParseAsmString - Parse the specified Instruction's AsmString into this
47 /// AsmWriterInst.
48 ///
49 AsmWriterInst::AsmWriterInst(const CodeGenInstruction &CGI,
50                              unsigned Variant,
51                              int FirstOperandColumn,
52                              int OperandSpacing) {
53   this->CGI = &CGI;
54   
55   unsigned CurVariant = ~0U;  // ~0 if we are outside a {.|.|.} region, other #.
56   
57   // This is the number of tabs we've seen if we're doing columnar layout.
58   unsigned CurColumn = 0;
59   
60   
61   // NOTE: Any extensions to this code need to be mirrored in the 
62   // AsmPrinter::printInlineAsm code that executes as compile time (assuming
63   // that inline asm strings should also get the new feature)!
64   const std::string &AsmString = CGI.AsmString;
65   std::string::size_type LastEmitted = 0;
66   while (LastEmitted != AsmString.size()) {
67     std::string::size_type DollarPos =
68     AsmString.find_first_of("${|}\\", LastEmitted);
69     if (DollarPos == std::string::npos) DollarPos = AsmString.size();
70     
71     // Emit a constant string fragment.
72     
73     if (DollarPos != LastEmitted) {
74       if (CurVariant == Variant || CurVariant == ~0U) {
75         for (; LastEmitted != DollarPos; ++LastEmitted)
76           switch (AsmString[LastEmitted]) {
77             case '\n':
78               AddLiteralString("\\n");
79               break;
80             case '\t':
81               // If the asm writer is not using a columnar layout, \t is not
82               // magic.
83               if (FirstOperandColumn == -1 || OperandSpacing == -1) {
84                 AddLiteralString("\\t");
85               } else {
86                 // We recognize a tab as an operand delimeter.
87                 unsigned DestColumn = FirstOperandColumn + 
88                 CurColumn++ * OperandSpacing;
89                 Operands.push_back(
90                   AsmWriterOperand(
91                     "O.PadToColumn(" +
92                     utostr(DestColumn) + ");\n",
93                     AsmWriterOperand::isLiteralStatementOperand));
94               }
95               break;
96             case '"':
97               AddLiteralString("\\\"");
98               break;
99             case '\\':
100               AddLiteralString("\\\\");
101               break;
102             default:
103               AddLiteralString(std::string(1, AsmString[LastEmitted]));
104               break;
105           }
106       } else {
107         LastEmitted = DollarPos;
108       }
109     } else if (AsmString[DollarPos] == '\\') {
110       if (DollarPos+1 != AsmString.size() &&
111           (CurVariant == Variant || CurVariant == ~0U)) {
112         if (AsmString[DollarPos+1] == 'n') {
113           AddLiteralString("\\n");
114         } else if (AsmString[DollarPos+1] == 't') {
115           // If the asm writer is not using a columnar layout, \t is not
116           // magic.
117           if (FirstOperandColumn == -1 || OperandSpacing == -1) {
118             AddLiteralString("\\t");
119             break;
120           }
121           
122           // We recognize a tab as an operand delimeter.
123           unsigned DestColumn = FirstOperandColumn + 
124           CurColumn++ * OperandSpacing;
125           Operands.push_back(
126             AsmWriterOperand("O.PadToColumn(" + utostr(DestColumn) + ");\n",
127                              AsmWriterOperand::isLiteralStatementOperand));
128           break;
129         } else if (std::string("${|}\\").find(AsmString[DollarPos+1]) 
130                    != std::string::npos) {
131           AddLiteralString(std::string(1, AsmString[DollarPos+1]));
132         } else {
133           throw "Non-supported escaped character found in instruction '" +
134           CGI.TheDef->getName() + "'!";
135         }
136         LastEmitted = DollarPos+2;
137         continue;
138       }
139     } else if (AsmString[DollarPos] == '{') {
140       if (CurVariant != ~0U)
141         throw "Nested variants found for instruction '" +
142         CGI.TheDef->getName() + "'!";
143       LastEmitted = DollarPos+1;
144       CurVariant = 0;   // We are now inside of the variant!
145     } else if (AsmString[DollarPos] == '|') {
146       if (CurVariant == ~0U)
147         throw "'|' character found outside of a variant in instruction '"
148         + CGI.TheDef->getName() + "'!";
149       ++CurVariant;
150       ++LastEmitted;
151     } else if (AsmString[DollarPos] == '}') {
152       if (CurVariant == ~0U)
153         throw "'}' character found outside of a variant in instruction '"
154         + CGI.TheDef->getName() + "'!";
155       ++LastEmitted;
156       CurVariant = ~0U;
157     } else if (DollarPos+1 != AsmString.size() &&
158                AsmString[DollarPos+1] == '$') {
159       if (CurVariant == Variant || CurVariant == ~0U) {
160         AddLiteralString("$");  // "$$" -> $
161       }
162       LastEmitted = DollarPos+2;
163     } else {
164       // Get the name of the variable.
165       std::string::size_type VarEnd = DollarPos+1;
166       
167       // handle ${foo}bar as $foo by detecting whether the character following
168       // the dollar sign is a curly brace.  If so, advance VarEnd and DollarPos
169       // so the variable name does not contain the leading curly brace.
170       bool hasCurlyBraces = false;
171       if (VarEnd < AsmString.size() && '{' == AsmString[VarEnd]) {
172         hasCurlyBraces = true;
173         ++DollarPos;
174         ++VarEnd;
175       }
176       
177       while (VarEnd < AsmString.size() && isIdentChar(AsmString[VarEnd]))
178         ++VarEnd;
179       std::string VarName(AsmString.begin()+DollarPos+1,
180                           AsmString.begin()+VarEnd);
181       
182       // Modifier - Support ${foo:modifier} syntax, where "modifier" is passed
183       // into printOperand.  Also support ${:feature}, which is passed into
184       // PrintSpecial.
185       std::string Modifier;
186       
187       // In order to avoid starting the next string at the terminating curly
188       // brace, advance the end position past it if we found an opening curly
189       // brace.
190       if (hasCurlyBraces) {
191         if (VarEnd >= AsmString.size())
192           throw "Reached end of string before terminating curly brace in '"
193           + CGI.TheDef->getName() + "'";
194         
195         // Look for a modifier string.
196         if (AsmString[VarEnd] == ':') {
197           ++VarEnd;
198           if (VarEnd >= AsmString.size())
199             throw "Reached end of string before terminating curly brace in '"
200             + CGI.TheDef->getName() + "'";
201           
202           unsigned ModifierStart = VarEnd;
203           while (VarEnd < AsmString.size() && isIdentChar(AsmString[VarEnd]))
204             ++VarEnd;
205           Modifier = std::string(AsmString.begin()+ModifierStart,
206                                  AsmString.begin()+VarEnd);
207           if (Modifier.empty())
208             throw "Bad operand modifier name in '"+ CGI.TheDef->getName() + "'";
209         }
210         
211         if (AsmString[VarEnd] != '}')
212           throw "Variable name beginning with '{' did not end with '}' in '"
213           + CGI.TheDef->getName() + "'";
214         ++VarEnd;
215       }
216       if (VarName.empty() && Modifier.empty())
217         throw "Stray '$' in '" + CGI.TheDef->getName() +
218         "' asm string, maybe you want $$?";
219       
220       if (VarName.empty()) {
221         // Just a modifier, pass this into PrintSpecial.
222         Operands.push_back(AsmWriterOperand("PrintSpecial", 
223                                             ~0U, 
224                                             ~0U, 
225                                             Modifier));
226       } else {
227         // Otherwise, normal operand.
228         unsigned OpNo = CGI.getOperandNamed(VarName);
229         CodeGenInstruction::OperandInfo OpInfo = CGI.OperandList[OpNo];
230         
231         if (CurVariant == Variant || CurVariant == ~0U) {
232           unsigned MIOp = OpInfo.MIOperandNo;
233           Operands.push_back(AsmWriterOperand(OpInfo.PrinterMethodName, 
234                                               OpNo,
235                                               MIOp,
236                                               Modifier));
237         }
238       }
239       LastEmitted = VarEnd;
240     }
241   }
242   
243   Operands.push_back(AsmWriterOperand("return;",
244     AsmWriterOperand::isLiteralStatementOperand));
245 }
246
247 /// MatchesAllButOneOp - If this instruction is exactly identical to the
248 /// specified instruction except for one differing operand, return the differing
249 /// operand number.  If more than one operand mismatches, return ~1, otherwise
250 /// if the instructions are identical return ~0.
251 unsigned AsmWriterInst::MatchesAllButOneOp(const AsmWriterInst &Other)const{
252   if (Operands.size() != Other.Operands.size()) return ~1;
253   
254   unsigned MismatchOperand = ~0U;
255   for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
256     if (Operands[i] != Other.Operands[i]) {
257       if (MismatchOperand != ~0U)  // Already have one mismatch?
258         return ~1U;
259       else
260         MismatchOperand = i;
261     }
262   }
263   return MismatchOperand;
264 }