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