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