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);
41
if (!MiModifier.empty())
42
Result += ", \"" + MiModifier + '"';
43
return Result + "); ";
46
/// ParseAsmString - Parse the specified Instruction's AsmString into this
49
AsmWriterInst::AsmWriterInst(const CodeGenInstruction &CGI,
51
int FirstOperandColumn,
55
unsigned CurVariant = ~0U; // ~0 if we are outside a {.|.|.} region, other #.
57
// This is the number of tabs we've seen if we're doing columnar layout.
58
unsigned CurColumn = 0;
61
// NOTE: Any extensions to this code need to be mirrored in the
62
// AsmPrinter::printInlineAsm code that executes as compile time (assuming
63
// that inline asm strings should also get the new feature)!
64
const std::string &AsmString = CGI.AsmString;
65
std::string::size_type LastEmitted = 0;
66
while (LastEmitted != AsmString.size()) {
67
std::string::size_type DollarPos =
68
AsmString.find_first_of("${|}\\", LastEmitted);
69
if (DollarPos == std::string::npos) DollarPos = AsmString.size();
71
// Emit a constant string fragment.
73
if (DollarPos != LastEmitted) {
74
if (CurVariant == Variant || CurVariant == ~0U) {
75
for (; LastEmitted != DollarPos; ++LastEmitted)
76
switch (AsmString[LastEmitted]) {
78
AddLiteralString("\\n");
81
// If the asm writer is not using a columnar layout, \t is not
83
if (FirstOperandColumn == -1 || OperandSpacing == -1) {
84
AddLiteralString("\\t");
86
// We recognize a tab as an operand delimeter.
87
unsigned DestColumn = FirstOperandColumn +
88
CurColumn++ * OperandSpacing;
92
utostr(DestColumn) + ");\n",
93
AsmWriterOperand::isLiteralStatementOperand));
97
AddLiteralString("\\\"");
100
AddLiteralString("\\\\");
103
AddLiteralString(std::string(1, AsmString[LastEmitted]));
107
LastEmitted = DollarPos;
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
117
if (FirstOperandColumn == -1 || OperandSpacing == -1) {
118
AddLiteralString("\\t");
122
// We recognize a tab as an operand delimeter.
123
unsigned DestColumn = FirstOperandColumn +
124
CurColumn++ * OperandSpacing;
126
AsmWriterOperand("O.PadToColumn(" + utostr(DestColumn) + ");\n",
127
AsmWriterOperand::isLiteralStatementOperand));
129
} else if (std::string("${|}\\").find(AsmString[DollarPos+1])
130
!= std::string::npos) {
131
AddLiteralString(std::string(1, AsmString[DollarPos+1]));
133
throw "Non-supported escaped character found in instruction '" +
134
CGI.TheDef->getName() + "'!";
136
LastEmitted = DollarPos+2;
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() + "'!";
151
} else if (AsmString[DollarPos] == '}') {
152
if (CurVariant == ~0U)
153
throw "'}' character found outside of a variant in instruction '"
154
+ CGI.TheDef->getName() + "'!";
157
} else if (DollarPos+1 != AsmString.size() &&
158
AsmString[DollarPos+1] == '$') {
159
if (CurVariant == Variant || CurVariant == ~0U) {
160
AddLiteralString("$"); // "$$" -> $
162
LastEmitted = DollarPos+2;
164
// Get the name of the variable.
165
std::string::size_type VarEnd = DollarPos+1;
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;
177
while (VarEnd < AsmString.size() && isIdentChar(AsmString[VarEnd]))
179
std::string VarName(AsmString.begin()+DollarPos+1,
180
AsmString.begin()+VarEnd);
182
// Modifier - Support ${foo:modifier} syntax, where "modifier" is passed
183
// into printOperand. Also support ${:feature}, which is passed into
185
std::string Modifier;
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
190
if (hasCurlyBraces) {
191
if (VarEnd >= AsmString.size())
192
throw "Reached end of string before terminating curly brace in '"
193
+ CGI.TheDef->getName() + "'";
195
// Look for a modifier string.
196
if (AsmString[VarEnd] == ':') {
198
if (VarEnd >= AsmString.size())
199
throw "Reached end of string before terminating curly brace in '"
200
+ CGI.TheDef->getName() + "'";
202
unsigned ModifierStart = VarEnd;
203
while (VarEnd < AsmString.size() && isIdentChar(AsmString[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() + "'";
211
if (AsmString[VarEnd] != '}')
212
throw "Variable name beginning with '{' did not end with '}' in '"
213
+ CGI.TheDef->getName() + "'";
216
if (VarName.empty() && Modifier.empty())
217
throw "Stray '$' in '" + CGI.TheDef->getName() +
218
"' asm string, maybe you want $$?";
220
if (VarName.empty()) {
221
// Just a modifier, pass this into PrintSpecial.
222
Operands.push_back(AsmWriterOperand("PrintSpecial",
227
// Otherwise, normal operand.
228
unsigned OpNo = CGI.getOperandNamed(VarName);
229
CodeGenInstruction::OperandInfo OpInfo = CGI.OperandList[OpNo];
231
if (CurVariant == Variant || CurVariant == ~0U) {
232
unsigned MIOp = OpInfo.MIOperandNo;
233
Operands.push_back(AsmWriterOperand(OpInfo.PrinterMethodName,
239
LastEmitted = VarEnd;
243
Operands.push_back(AsmWriterOperand("return;",
244
AsmWriterOperand::isLiteralStatementOperand));
247
/// MatchesAllButOneOp - If this instruction is exactly identical to the
248
/// specified instruction except for one differing operand, return the differing
249
/// operand number. If more than one operand mismatches, return ~1, otherwise
250
/// if the instructions are identical return ~0.
251
unsigned AsmWriterInst::MatchesAllButOneOp(const AsmWriterInst &Other)const{
252
if (Operands.size() != Other.Operands.size()) return ~1;
254
unsigned MismatchOperand = ~0U;
255
for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
256
if (Operands[i] != Other.Operands[i]) {
257
if (MismatchOperand != ~0U) // Already have one mismatch?
263
return MismatchOperand;