blob: 2b7a2144eeee4eb8b5c44c209a0fb144d42ecd76 [file] [log] [blame]
Sean Callanand32c02f2010-02-09 21:50:41 +00001//===- 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
19using namespace llvm;
20
21static bool isIdentChar(char C) {
22 return (C >= 'a' && C <= 'z') ||
23 (C >= 'A' && C <= 'Z') ||
24 (C >= '0' && C <= '9') ||
25 C == '_';
26}
27
28std::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///
49AsmWriterInst::AsmWriterInst(const CodeGenInstruction &CGI, Record *AsmWriter) {
50 this->CGI = &CGI;
51
52 unsigned Variant = AsmWriter->getValueAsInt("Variant");
53 int FirstOperandColumn = AsmWriter->getValueAsInt("FirstOperandColumn");
54 int OperandSpacing = AsmWriter->getValueAsInt("OperandSpacing");
55
56 unsigned CurVariant = ~0U; // ~0 if we are outside a {.|.|.} region, other #.
57
58 // This is the number of tabs we've seen if we're doing columnar layout.
59 unsigned CurColumn = 0;
60
61
62 // NOTE: Any extensions to this code need to be mirrored in the
63 // AsmPrinter::printInlineAsm code that executes as compile time (assuming
64 // that inline asm strings should also get the new feature)!
65 const std::string &AsmString = CGI.AsmString;
66 std::string::size_type LastEmitted = 0;
67 while (LastEmitted != AsmString.size()) {
68 std::string::size_type DollarPos =
69 AsmString.find_first_of("${|}\\", LastEmitted);
70 if (DollarPos == std::string::npos) DollarPos = AsmString.size();
71
72 // Emit a constant string fragment.
73
74 if (DollarPos != LastEmitted) {
75 if (CurVariant == Variant || CurVariant == ~0U) {
76 for (; LastEmitted != DollarPos; ++LastEmitted)
77 switch (AsmString[LastEmitted]) {
78 case '\n':
79 AddLiteralString("\\n");
80 break;
81 case '\t':
82 // If the asm writer is not using a columnar layout, \t is not
83 // magic.
84 if (FirstOperandColumn == -1 || OperandSpacing == -1) {
85 AddLiteralString("\\t");
86 } else {
87 // We recognize a tab as an operand delimeter.
88 unsigned DestColumn = FirstOperandColumn +
89 CurColumn++ * OperandSpacing;
90 Operands.push_back(
91 AsmWriterOperand("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", ~0U, Modifier));
223 } else {
224 // Otherwise, normal operand.
225 unsigned OpNo = CGI.getOperandNamed(VarName);
226 CodeGenInstruction::OperandInfo OpInfo = CGI.OperandList[OpNo];
227
228 if (CurVariant == Variant || CurVariant == ~0U) {
229 unsigned MIOp = OpInfo.MIOperandNo;
230 Operands.push_back(AsmWriterOperand(OpInfo.PrinterMethodName, MIOp,
231 Modifier));
232 }
233 }
234 LastEmitted = VarEnd;
235 }
236 }
237
238 Operands.push_back(AsmWriterOperand("return;",
239 AsmWriterOperand::isLiteralStatementOperand));
240}
241
242/// MatchesAllButOneOp - If this instruction is exactly identical to the
243/// specified instruction except for one differing operand, return the differing
244/// operand number. If more than one operand mismatches, return ~1, otherwise
245/// if the instructions are identical return ~0.
246unsigned AsmWriterInst::MatchesAllButOneOp(const AsmWriterInst &Other)const{
247 if (Operands.size() != Other.Operands.size()) return ~1;
248
249 unsigned MismatchOperand = ~0U;
250 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
251 if (Operands[i] != Other.Operands[i]) {
252 if (MismatchOperand != ~0U) // Already have one mismatch?
253 return ~1U;
254 else
255 MismatchOperand = i;
256 }
257 }
258 return MismatchOperand;
259}