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 // This is the number of tabs we've seen if we're doing columnar layout.
57 unsigned CurColumn
= 0;
60 // NOTE: Any extensions to this code need to be mirrored in the
61 // AsmPrinter::printInlineAsm code that executes as compile time (assuming
62 // that inline asm strings should also get the new feature)!
63 std::string AsmString
= CGI
.FlattenAsmStringVariants(CGI
.AsmString
, Variant
);
64 std::string::size_type LastEmitted
= 0;
65 while (LastEmitted
!= AsmString
.size()) {
66 std::string::size_type DollarPos
=
67 AsmString
.find_first_of("$\\", LastEmitted
);
68 if (DollarPos
== std::string::npos
) DollarPos
= AsmString
.size();
70 // Emit a constant string fragment.
71 if (DollarPos
!= LastEmitted
) {
72 for (; LastEmitted
!= DollarPos
; ++LastEmitted
)
73 switch (AsmString
[LastEmitted
]) {
75 AddLiteralString("\\n");
78 // If the asm writer is not using a columnar layout, \t is not
80 if (FirstOperandColumn
== -1 || OperandSpacing
== -1) {
81 AddLiteralString("\\t");
83 // We recognize a tab as an operand delimeter.
84 unsigned DestColumn
= FirstOperandColumn
+
85 CurColumn
++ * OperandSpacing
;
89 utostr(DestColumn
) + ");\n",
90 AsmWriterOperand::isLiteralStatementOperand
));
94 AddLiteralString("\\\"");
97 AddLiteralString("\\\\");
100 AddLiteralString(std::string(1, AsmString
[LastEmitted
]));
103 } else if (AsmString
[DollarPos
] == '\\') {
104 if (DollarPos
+1 != AsmString
.size()) {
105 if (AsmString
[DollarPos
+1] == 'n') {
106 AddLiteralString("\\n");
107 } else if (AsmString
[DollarPos
+1] == 't') {
108 // If the asm writer is not using a columnar layout, \t is not
110 if (FirstOperandColumn
== -1 || OperandSpacing
== -1) {
111 AddLiteralString("\\t");
115 // We recognize a tab as an operand delimeter.
116 unsigned DestColumn
= FirstOperandColumn
+
117 CurColumn
++ * OperandSpacing
;
119 AsmWriterOperand("O.PadToColumn(" + utostr(DestColumn
) + ");\n",
120 AsmWriterOperand::isLiteralStatementOperand
));
122 } else if (std::string("${|}\\").find(AsmString
[DollarPos
+1])
123 != std::string::npos
) {
124 AddLiteralString(std::string(1, AsmString
[DollarPos
+1]));
126 throw "Non-supported escaped character found in instruction '" +
127 CGI
.TheDef
->getName() + "'!";
129 LastEmitted
= DollarPos
+2;
132 } else if (DollarPos
+1 != AsmString
.size() &&
133 AsmString
[DollarPos
+1] == '$') {
134 AddLiteralString("$"); // "$$" -> $
135 LastEmitted
= DollarPos
+2;
137 // Get the name of the variable.
138 std::string::size_type VarEnd
= DollarPos
+1;
140 // handle ${foo}bar as $foo by detecting whether the character following
141 // the dollar sign is a curly brace. If so, advance VarEnd and DollarPos
142 // so the variable name does not contain the leading curly brace.
143 bool hasCurlyBraces
= false;
144 if (VarEnd
< AsmString
.size() && '{' == AsmString
[VarEnd
]) {
145 hasCurlyBraces
= true;
150 while (VarEnd
< AsmString
.size() && isIdentChar(AsmString
[VarEnd
]))
152 std::string
VarName(AsmString
.begin()+DollarPos
+1,
153 AsmString
.begin()+VarEnd
);
155 // Modifier - Support ${foo:modifier} syntax, where "modifier" is passed
156 // into printOperand. Also support ${:feature}, which is passed into
158 std::string Modifier
;
160 // In order to avoid starting the next string at the terminating curly
161 // brace, advance the end position past it if we found an opening curly
163 if (hasCurlyBraces
) {
164 if (VarEnd
>= AsmString
.size())
165 throw "Reached end of string before terminating curly brace in '"
166 + CGI
.TheDef
->getName() + "'";
168 // Look for a modifier string.
169 if (AsmString
[VarEnd
] == ':') {
171 if (VarEnd
>= AsmString
.size())
172 throw "Reached end of string before terminating curly brace in '"
173 + CGI
.TheDef
->getName() + "'";
175 unsigned ModifierStart
= VarEnd
;
176 while (VarEnd
< AsmString
.size() && isIdentChar(AsmString
[VarEnd
]))
178 Modifier
= std::string(AsmString
.begin()+ModifierStart
,
179 AsmString
.begin()+VarEnd
);
180 if (Modifier
.empty())
181 throw "Bad operand modifier name in '"+ CGI
.TheDef
->getName() + "'";
184 if (AsmString
[VarEnd
] != '}')
185 throw "Variable name beginning with '{' did not end with '}' in '"
186 + CGI
.TheDef
->getName() + "'";
189 if (VarName
.empty() && Modifier
.empty())
190 throw "Stray '$' in '" + CGI
.TheDef
->getName() +
191 "' asm string, maybe you want $$?";
193 if (VarName
.empty()) {
194 // Just a modifier, pass this into PrintSpecial.
195 Operands
.push_back(AsmWriterOperand("PrintSpecial",
200 // Otherwise, normal operand.
201 unsigned OpNo
= CGI
.Operands
.getOperandNamed(VarName
);
202 CGIOperandList::OperandInfo OpInfo
= CGI
.Operands
[OpNo
];
204 unsigned MIOp
= OpInfo
.MIOperandNo
;
205 Operands
.push_back(AsmWriterOperand(OpInfo
.PrinterMethodName
,
206 OpNo
, MIOp
, Modifier
));
208 LastEmitted
= VarEnd
;
212 Operands
.push_back(AsmWriterOperand("return;",
213 AsmWriterOperand::isLiteralStatementOperand
));
216 /// MatchesAllButOneOp - If this instruction is exactly identical to the
217 /// specified instruction except for one differing operand, return the differing
218 /// operand number. If more than one operand mismatches, return ~1, otherwise
219 /// if the instructions are identical return ~0.
220 unsigned AsmWriterInst::MatchesAllButOneOp(const AsmWriterInst
&Other
)const{
221 if (Operands
.size() != Other
.Operands
.size()) return ~1;
223 unsigned MismatchOperand
= ~0U;
224 for (unsigned i
= 0, e
= Operands
.size(); i
!= e
; ++i
) {
225 if (Operands
[i
] != Other
.Operands
[i
]) {
226 if (MismatchOperand
!= ~0U) // Already have one mismatch?
232 return MismatchOperand
;