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