1 //===- AsmWriterInst.h - Classes encapsulating a printable inst -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // These classes implement a parser for assembly strings.
12 //===----------------------------------------------------------------------===//
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"
22 static bool isIdentChar(char C) {
23 return (C >= 'a' && C <= 'z') ||
24 (C >= 'A' && C <= 'Z') ||
25 (C >= '0' && C <= '9') ||
29 std::string AsmWriterOperand::getCode() const {
30 if (OperandType == isLiteralTextOperand) {
32 return "O << '" + Str + "'; ";
33 return "O << \"" + Str + "\"; ";
36 if (OperandType == isLiteralStatementOperand)
39 std::string Result = Str + "(MI";
41 Result += ", " + utostr(MIOpNo);
43 if (!MiModifier.empty())
44 Result += ", \"" + MiModifier + '"';
45 return Result + "); ";
48 /// ParseAsmString - Parse the specified Instruction's AsmString into this
51 AsmWriterInst::AsmWriterInst(const CodeGenInstruction &CGI, unsigned Variant) {
54 // NOTE: Any extensions to this code need to be mirrored in the
55 // AsmPrinter::printInlineAsm code that executes as compile time (assuming
56 // that inline asm strings should also get the new feature)!
57 std::string AsmString = CGI.FlattenAsmStringVariants(CGI.AsmString, Variant);
58 std::string::size_type LastEmitted = 0;
59 while (LastEmitted != AsmString.size()) {
60 std::string::size_type DollarPos =
61 AsmString.find_first_of("$\\", LastEmitted);
62 if (DollarPos == std::string::npos) DollarPos = AsmString.size();
64 // Emit a constant string fragment.
65 if (DollarPos != LastEmitted) {
66 for (; LastEmitted != DollarPos; ++LastEmitted)
67 switch (AsmString[LastEmitted]) {
69 AddLiteralString("\\n");
72 AddLiteralString("\\t");
75 AddLiteralString("\\\"");
78 AddLiteralString("\\\\");
81 AddLiteralString(std::string(1, AsmString[LastEmitted]));
84 } else if (AsmString[DollarPos] == '\\') {
85 if (DollarPos+1 != AsmString.size()) {
86 if (AsmString[DollarPos+1] == 'n') {
87 AddLiteralString("\\n");
88 } else if (AsmString[DollarPos+1] == 't') {
89 AddLiteralString("\\t");
90 } else if (std::string("${|}\\").find(AsmString[DollarPos+1])
91 != std::string::npos) {
92 AddLiteralString(std::string(1, AsmString[DollarPos+1]));
94 PrintFatalError("Non-supported escaped character found in instruction '" +
95 CGI.TheDef->getName() + "'!");
97 LastEmitted = DollarPos+2;
100 } else if (DollarPos+1 != AsmString.size() &&
101 AsmString[DollarPos+1] == '$') {
102 AddLiteralString("$"); // "$$" -> $
103 LastEmitted = DollarPos+2;
105 // Get the name of the variable.
106 std::string::size_type VarEnd = DollarPos+1;
108 // handle ${foo}bar as $foo by detecting whether the character following
109 // the dollar sign is a curly brace. If so, advance VarEnd and DollarPos
110 // so the variable name does not contain the leading curly brace.
111 bool hasCurlyBraces = false;
112 if (VarEnd < AsmString.size() && '{' == AsmString[VarEnd]) {
113 hasCurlyBraces = true;
118 while (VarEnd < AsmString.size() && isIdentChar(AsmString[VarEnd]))
120 std::string VarName(AsmString.begin()+DollarPos+1,
121 AsmString.begin()+VarEnd);
123 // Modifier - Support ${foo:modifier} syntax, where "modifier" is passed
124 // into printOperand. Also support ${:feature}, which is passed into
126 std::string Modifier;
128 // In order to avoid starting the next string at the terminating curly
129 // brace, advance the end position past it if we found an opening curly
131 if (hasCurlyBraces) {
132 if (VarEnd >= AsmString.size())
133 PrintFatalError("Reached end of string before terminating curly brace in '"
134 + CGI.TheDef->getName() + "'");
136 // Look for a modifier string.
137 if (AsmString[VarEnd] == ':') {
139 if (VarEnd >= AsmString.size())
140 PrintFatalError("Reached end of string before terminating curly brace in '"
141 + CGI.TheDef->getName() + "'");
143 unsigned ModifierStart = VarEnd;
144 while (VarEnd < AsmString.size() && isIdentChar(AsmString[VarEnd]))
146 Modifier = std::string(AsmString.begin()+ModifierStart,
147 AsmString.begin()+VarEnd);
148 if (Modifier.empty())
149 PrintFatalError("Bad operand modifier name in '"+ CGI.TheDef->getName() + "'");
152 if (AsmString[VarEnd] != '}')
153 PrintFatalError("Variable name beginning with '{' did not end with '}' in '"
154 + CGI.TheDef->getName() + "'");
157 if (VarName.empty() && Modifier.empty())
158 PrintFatalError("Stray '$' in '" + CGI.TheDef->getName() +
159 "' asm string, maybe you want $$?");
161 if (VarName.empty()) {
162 // Just a modifier, pass this into PrintSpecial.
163 Operands.push_back(AsmWriterOperand("PrintSpecial",
168 // Otherwise, normal operand.
169 unsigned OpNo = CGI.Operands.getOperandNamed(VarName);
170 CGIOperandList::OperandInfo OpInfo = CGI.Operands[OpNo];
172 unsigned MIOp = OpInfo.MIOperandNo;
173 Operands.push_back(AsmWriterOperand(OpInfo.PrinterMethodName,
174 OpNo, MIOp, Modifier));
176 LastEmitted = VarEnd;
180 Operands.push_back(AsmWriterOperand("return;",
181 AsmWriterOperand::isLiteralStatementOperand));
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;
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?
199 return MismatchOperand;