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"
17
#include "llvm/ADT/StringExtras.h"
21
static bool isIdentChar(char C) {
22
return (C >= 'a' && C <= 'z') ||
23
(C >= 'A' && C <= 'Z') ||
24
(C >= '0' && C <= '9') ||
28
std::string AsmWriterOperand::getCode() const {
29
if (OperandType == isLiteralTextOperand) {
31
return "O << '" + Str + "'; ";
32
return "O << \"" + Str + "\"; ";
35
if (OperandType == isLiteralStatementOperand)
38
std::string Result = Str + "(MI";
40
Result += ", " + utostr(MIOpNo);
42
if (!MiModifier.empty())
43
Result += ", \"" + MiModifier + '"';
44
return Result + "); ";
47
/// ParseAsmString - Parse the specified Instruction's AsmString into this
50
AsmWriterInst::AsmWriterInst(const CodeGenInstruction &CGI,
52
int FirstOperandColumn,
56
unsigned CurVariant = ~0U; // ~0 if we are outside a {.|.|.} region, other #.
58
// This is the number of tabs we've seen if we're doing columnar layout.
59
unsigned CurColumn = 0;
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();
72
// Emit a constant string fragment.
74
if (DollarPos != LastEmitted) {
75
if (CurVariant == Variant || CurVariant == ~0U) {
76
for (; LastEmitted != DollarPos; ++LastEmitted)
77
switch (AsmString[LastEmitted]) {
79
AddLiteralString("\\n");
82
// If the asm writer is not using a columnar layout, \t is not
84
if (FirstOperandColumn == -1 || OperandSpacing == -1) {
85
AddLiteralString("\\t");
87
// We recognize a tab as an operand delimeter.
88
unsigned DestColumn = FirstOperandColumn +
89
CurColumn++ * OperandSpacing;
93
utostr(DestColumn) + ");\n",
94
AsmWriterOperand::isLiteralStatementOperand));
98
AddLiteralString("\\\"");
101
AddLiteralString("\\\\");
104
AddLiteralString(std::string(1, AsmString[LastEmitted]));
108
LastEmitted = DollarPos;
110
} else if (AsmString[DollarPos] == '\\') {
111
if (DollarPos+1 != AsmString.size() &&
112
(CurVariant == Variant || CurVariant == ~0U)) {
113
if (AsmString[DollarPos+1] == 'n') {
114
AddLiteralString("\\n");
115
} else if (AsmString[DollarPos+1] == 't') {
116
// If the asm writer is not using a columnar layout, \t is not
118
if (FirstOperandColumn == -1 || OperandSpacing == -1) {
119
AddLiteralString("\\t");
123
// We recognize a tab as an operand delimeter.
124
unsigned DestColumn = FirstOperandColumn +
125
CurColumn++ * OperandSpacing;
127
AsmWriterOperand("O.PadToColumn(" + utostr(DestColumn) + ");\n",
128
AsmWriterOperand::isLiteralStatementOperand));
130
} else if (std::string("${|}\\").find(AsmString[DollarPos+1])
131
!= std::string::npos) {
132
AddLiteralString(std::string(1, AsmString[DollarPos+1]));
134
throw "Non-supported escaped character found in instruction '" +
135
CGI.TheDef->getName() + "'!";
137
LastEmitted = DollarPos+2;
140
} else if (AsmString[DollarPos] == '{') {
141
if (CurVariant != ~0U)
142
throw "Nested variants found for instruction '" +
143
CGI.TheDef->getName() + "'!";
144
LastEmitted = DollarPos+1;
145
CurVariant = 0; // We are now inside of the variant!
146
} else if (AsmString[DollarPos] == '|') {
147
if (CurVariant == ~0U)
148
throw "'|' character found outside of a variant in instruction '"
149
+ CGI.TheDef->getName() + "'!";
152
} else if (AsmString[DollarPos] == '}') {
153
if (CurVariant == ~0U)
154
throw "'}' character found outside of a variant in instruction '"
155
+ CGI.TheDef->getName() + "'!";
158
} else if (DollarPos+1 != AsmString.size() &&
159
AsmString[DollarPos+1] == '$') {
160
if (CurVariant == Variant || CurVariant == ~0U) {
161
AddLiteralString("$"); // "$$" -> $
163
LastEmitted = DollarPos+2;
165
// Get the name of the variable.
166
std::string::size_type VarEnd = DollarPos+1;
168
// handle ${foo}bar as $foo by detecting whether the character following
169
// the dollar sign is a curly brace. If so, advance VarEnd and DollarPos
170
// so the variable name does not contain the leading curly brace.
171
bool hasCurlyBraces = false;
172
if (VarEnd < AsmString.size() && '{' == AsmString[VarEnd]) {
173
hasCurlyBraces = true;
178
while (VarEnd < AsmString.size() && isIdentChar(AsmString[VarEnd]))
180
std::string VarName(AsmString.begin()+DollarPos+1,
181
AsmString.begin()+VarEnd);
183
// Modifier - Support ${foo:modifier} syntax, where "modifier" is passed
184
// into printOperand. Also support ${:feature}, which is passed into
186
std::string Modifier;
188
// In order to avoid starting the next string at the terminating curly
189
// brace, advance the end position past it if we found an opening curly
191
if (hasCurlyBraces) {
192
if (VarEnd >= AsmString.size())
193
throw "Reached end of string before terminating curly brace in '"
194
+ CGI.TheDef->getName() + "'";
196
// Look for a modifier string.
197
if (AsmString[VarEnd] == ':') {
199
if (VarEnd >= AsmString.size())
200
throw "Reached end of string before terminating curly brace in '"
201
+ CGI.TheDef->getName() + "'";
203
unsigned ModifierStart = VarEnd;
204
while (VarEnd < AsmString.size() && isIdentChar(AsmString[VarEnd]))
206
Modifier = std::string(AsmString.begin()+ModifierStart,
207
AsmString.begin()+VarEnd);
208
if (Modifier.empty())
209
throw "Bad operand modifier name in '"+ CGI.TheDef->getName() + "'";
212
if (AsmString[VarEnd] != '}')
213
throw "Variable name beginning with '{' did not end with '}' in '"
214
+ CGI.TheDef->getName() + "'";
217
if (VarName.empty() && Modifier.empty())
218
throw "Stray '$' in '" + CGI.TheDef->getName() +
219
"' asm string, maybe you want $$?";
221
if (VarName.empty()) {
222
// Just a modifier, pass this into PrintSpecial.
223
Operands.push_back(AsmWriterOperand("PrintSpecial",
228
// Otherwise, normal operand.
229
unsigned OpNo = CGI.getOperandNamed(VarName);
230
CodeGenInstruction::OperandInfo OpInfo = CGI.OperandList[OpNo];
232
if (CurVariant == Variant || CurVariant == ~0U) {
233
unsigned MIOp = OpInfo.MIOperandNo;
234
Operands.push_back(AsmWriterOperand(OpInfo.PrinterMethodName,
240
LastEmitted = VarEnd;
244
Operands.push_back(AsmWriterOperand("return;",
245
AsmWriterOperand::isLiteralStatementOperand));
248
/// MatchesAllButOneOp - If this instruction is exactly identical to the
249
/// specified instruction except for one differing operand, return the differing
250
/// operand number. If more than one operand mismatches, return ~1, otherwise
251
/// if the instructions are identical return ~0.
252
unsigned AsmWriterInst::MatchesAllButOneOp(const AsmWriterInst &Other)const{
253
if (Operands.size() != Other.Operands.size()) return ~1;
255
unsigned MismatchOperand = ~0U;
256
for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
257
if (Operands[i] != Other.Operands[i]) {
258
if (MismatchOperand != ~0U) // Already have one mismatch?
264
return MismatchOperand;