blob: d065a420969ef25d54a18906df3c2331c481426c [file] [log] [blame]
Sean Callananb7e8f4a2010-02-09 21:50:41 +00001//===- AsmWriterInst.h - Classes encapsulating a printable inst -----------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Sean Callananb7e8f4a2010-02-09 21:50:41 +00006//
7//===----------------------------------------------------------------------===//
8//
9// These classes implement a parser for assembly strings.
10//
11//===----------------------------------------------------------------------===//
12
13#include "AsmWriterInst.h"
14#include "CodeGenTarget.h"
Sean Callananb7e8f4a2010-02-09 21:50:41 +000015#include "llvm/ADT/StringExtras.h"
Joerg Sonnenberger635debe2012-10-25 20:33:17 +000016#include "llvm/TableGen/Error.h"
Peter Collingbourne84c287e2011-10-01 16:41:13 +000017#include "llvm/TableGen/Record.h"
Sean Callananb7e8f4a2010-02-09 21:50:41 +000018
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
Craig Topperc24a4012016-01-14 06:15:07 +000028std::string AsmWriterOperand::getCode(bool PassSubtarget) const {
Sean Callananb7e8f4a2010-02-09 21:50:41 +000029 if (OperandType == isLiteralTextOperand) {
30 if (Str.size() == 1)
Craig Topper6313d202016-01-11 02:11:36 +000031 return "O << '" + Str + "';";
32 return "O << \"" + Str + "\";";
Sean Callananb7e8f4a2010-02-09 21:50:41 +000033 }
Craig Topperad87dc02013-07-23 06:25:00 +000034
Sean Callananb7e8f4a2010-02-09 21:50:41 +000035 if (OperandType == isLiteralStatementOperand)
36 return Str;
Craig Topperad87dc02013-07-23 06:25:00 +000037
Sean Callananb7e8f4a2010-02-09 21:50:41 +000038 std::string Result = Str + "(MI";
39 if (MIOpNo != ~0U)
40 Result += ", " + utostr(MIOpNo);
Akira Hatanakab46d0232015-03-27 20:36:02 +000041 if (PassSubtarget)
42 Result += ", STI";
Chris Lattner76c564b2010-04-04 04:47:45 +000043 Result += ", O";
Sean Callananb7e8f4a2010-02-09 21:50:41 +000044 if (!MiModifier.empty())
45 Result += ", \"" + MiModifier + '"';
Craig Topper6313d202016-01-11 02:11:36 +000046 return Result + ");";
Sean Callananb7e8f4a2010-02-09 21:50:41 +000047}
48
49/// ParseAsmString - Parse the specified Instruction's AsmString into this
50/// AsmWriterInst.
51///
Craig Topper9e9ae602016-01-17 08:05:33 +000052AsmWriterInst::AsmWriterInst(const CodeGenInstruction &CGI, unsigned CGIIndex,
53 unsigned Variant)
54 : CGI(&CGI), CGIIndex(CGIIndex) {
Craig Topperad87dc02013-07-23 06:25:00 +000055
Craig Topperad87dc02013-07-23 06:25:00 +000056 // NOTE: Any extensions to this code need to be mirrored in the
Sean Callananb7e8f4a2010-02-09 21:50:41 +000057 // AsmPrinter::printInlineAsm code that executes as compile time (assuming
58 // that inline asm strings should also get the new feature)!
Chris Lattner25d9c7f2010-11-01 01:07:14 +000059 std::string AsmString = CGI.FlattenAsmStringVariants(CGI.AsmString, Variant);
Sean Callananb7e8f4a2010-02-09 21:50:41 +000060 std::string::size_type LastEmitted = 0;
61 while (LastEmitted != AsmString.size()) {
62 std::string::size_type DollarPos =
Chris Lattner25d9c7f2010-11-01 01:07:14 +000063 AsmString.find_first_of("$\\", LastEmitted);
Sean Callananb7e8f4a2010-02-09 21:50:41 +000064 if (DollarPos == std::string::npos) DollarPos = AsmString.size();
Craig Topperad87dc02013-07-23 06:25:00 +000065
Sean Callananb7e8f4a2010-02-09 21:50:41 +000066 // Emit a constant string fragment.
Sean Callananb7e8f4a2010-02-09 21:50:41 +000067 if (DollarPos != LastEmitted) {
Chris Lattner25d9c7f2010-11-01 01:07:14 +000068 for (; LastEmitted != DollarPos; ++LastEmitted)
69 switch (AsmString[LastEmitted]) {
70 case '\n':
71 AddLiteralString("\\n");
72 break;
73 case '\t':
Rafael Espindola53c2b1e2013-12-02 05:10:04 +000074 AddLiteralString("\\t");
Chris Lattner25d9c7f2010-11-01 01:07:14 +000075 break;
76 case '"':
77 AddLiteralString("\\\"");
78 break;
79 case '\\':
80 AddLiteralString("\\\\");
81 break;
82 default:
83 AddLiteralString(std::string(1, AsmString[LastEmitted]));
84 break;
85 }
Sean Callananb7e8f4a2010-02-09 21:50:41 +000086 } else if (AsmString[DollarPos] == '\\') {
Chris Lattner25d9c7f2010-11-01 01:07:14 +000087 if (DollarPos+1 != AsmString.size()) {
Sean Callananb7e8f4a2010-02-09 21:50:41 +000088 if (AsmString[DollarPos+1] == 'n') {
89 AddLiteralString("\\n");
90 } else if (AsmString[DollarPos+1] == 't') {
Rafael Espindola53c2b1e2013-12-02 05:10:04 +000091 AddLiteralString("\\t");
Craig Topperad87dc02013-07-23 06:25:00 +000092 } else if (std::string("${|}\\").find(AsmString[DollarPos+1])
Sean Callananb7e8f4a2010-02-09 21:50:41 +000093 != std::string::npos) {
94 AddLiteralString(std::string(1, AsmString[DollarPos+1]));
95 } else {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +000096 PrintFatalError("Non-supported escaped character found in instruction '" +
97 CGI.TheDef->getName() + "'!");
Sean Callananb7e8f4a2010-02-09 21:50:41 +000098 }
99 LastEmitted = DollarPos+2;
100 continue;
101 }
Sean Callananb7e8f4a2010-02-09 21:50:41 +0000102 } else if (DollarPos+1 != AsmString.size() &&
103 AsmString[DollarPos+1] == '$') {
Chris Lattner25d9c7f2010-11-01 01:07:14 +0000104 AddLiteralString("$"); // "$$" -> $
Sean Callananb7e8f4a2010-02-09 21:50:41 +0000105 LastEmitted = DollarPos+2;
106 } else {
107 // Get the name of the variable.
108 std::string::size_type VarEnd = DollarPos+1;
Craig Topperad87dc02013-07-23 06:25:00 +0000109
Sean Callananb7e8f4a2010-02-09 21:50:41 +0000110 // handle ${foo}bar as $foo by detecting whether the character following
111 // the dollar sign is a curly brace. If so, advance VarEnd and DollarPos
112 // so the variable name does not contain the leading curly brace.
113 bool hasCurlyBraces = false;
114 if (VarEnd < AsmString.size() && '{' == AsmString[VarEnd]) {
115 hasCurlyBraces = true;
116 ++DollarPos;
117 ++VarEnd;
118 }
Craig Topperad87dc02013-07-23 06:25:00 +0000119
Sean Callananb7e8f4a2010-02-09 21:50:41 +0000120 while (VarEnd < AsmString.size() && isIdentChar(AsmString[VarEnd]))
121 ++VarEnd;
Craig Toppere8453b02016-01-17 08:47:02 +0000122 StringRef VarName(AsmString.data()+DollarPos+1, VarEnd-DollarPos-1);
Craig Topperad87dc02013-07-23 06:25:00 +0000123
Sean Callananb7e8f4a2010-02-09 21:50:41 +0000124 // Modifier - Support ${foo:modifier} syntax, where "modifier" is passed
125 // into printOperand. Also support ${:feature}, which is passed into
126 // PrintSpecial.
127 std::string Modifier;
Craig Topperad87dc02013-07-23 06:25:00 +0000128
Sean Callananb7e8f4a2010-02-09 21:50:41 +0000129 // In order to avoid starting the next string at the terminating curly
130 // brace, advance the end position past it if we found an opening curly
131 // brace.
132 if (hasCurlyBraces) {
133 if (VarEnd >= AsmString.size())
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000134 PrintFatalError("Reached end of string before terminating curly brace in '"
135 + CGI.TheDef->getName() + "'");
Craig Topperad87dc02013-07-23 06:25:00 +0000136
Sean Callananb7e8f4a2010-02-09 21:50:41 +0000137 // Look for a modifier string.
138 if (AsmString[VarEnd] == ':') {
139 ++VarEnd;
140 if (VarEnd >= AsmString.size())
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000141 PrintFatalError("Reached end of string before terminating curly brace in '"
142 + CGI.TheDef->getName() + "'");
Craig Topperad87dc02013-07-23 06:25:00 +0000143
Craig Toppere8453b02016-01-17 08:47:02 +0000144 std::string::size_type ModifierStart = VarEnd;
Sean Callananb7e8f4a2010-02-09 21:50:41 +0000145 while (VarEnd < AsmString.size() && isIdentChar(AsmString[VarEnd]))
146 ++VarEnd;
147 Modifier = std::string(AsmString.begin()+ModifierStart,
148 AsmString.begin()+VarEnd);
149 if (Modifier.empty())
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000150 PrintFatalError("Bad operand modifier name in '"+ CGI.TheDef->getName() + "'");
Sean Callananb7e8f4a2010-02-09 21:50:41 +0000151 }
Craig Topperad87dc02013-07-23 06:25:00 +0000152
Sean Callananb7e8f4a2010-02-09 21:50:41 +0000153 if (AsmString[VarEnd] != '}')
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000154 PrintFatalError("Variable name beginning with '{' did not end with '}' in '"
155 + CGI.TheDef->getName() + "'");
Sean Callananb7e8f4a2010-02-09 21:50:41 +0000156 ++VarEnd;
157 }
158 if (VarName.empty() && Modifier.empty())
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000159 PrintFatalError("Stray '$' in '" + CGI.TheDef->getName() +
160 "' asm string, maybe you want $$?");
Craig Topperad87dc02013-07-23 06:25:00 +0000161
Sean Callananb7e8f4a2010-02-09 21:50:41 +0000162 if (VarName.empty()) {
163 // Just a modifier, pass this into PrintSpecial.
Craig Topperdb75cc12016-01-22 05:59:37 +0000164 Operands.emplace_back("PrintSpecial", ~0U, Modifier);
Sean Callananb7e8f4a2010-02-09 21:50:41 +0000165 } else {
166 // Otherwise, normal operand.
Chris Lattnerd8adec72010-11-01 04:03:32 +0000167 unsigned OpNo = CGI.Operands.getOperandNamed(VarName);
168 CGIOperandList::OperandInfo OpInfo = CGI.Operands[OpNo];
Craig Topperad87dc02013-07-23 06:25:00 +0000169
Chris Lattner25d9c7f2010-11-01 01:07:14 +0000170 unsigned MIOp = OpInfo.MIOperandNo;
Craig Topperdb75cc12016-01-22 05:59:37 +0000171 Operands.emplace_back(OpInfo.PrinterMethodName, MIOp, Modifier);
Sean Callananb7e8f4a2010-02-09 21:50:41 +0000172 }
173 LastEmitted = VarEnd;
174 }
175 }
Craig Topperad87dc02013-07-23 06:25:00 +0000176
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000177 Operands.emplace_back("return;", AsmWriterOperand::isLiteralStatementOperand);
Sean Callananb7e8f4a2010-02-09 21:50:41 +0000178}
179
180/// MatchesAllButOneOp - If this instruction is exactly identical to the
181/// specified instruction except for one differing operand, return the differing
182/// operand number. If more than one operand mismatches, return ~1, otherwise
183/// if the instructions are identical return ~0.
184unsigned AsmWriterInst::MatchesAllButOneOp(const AsmWriterInst &Other)const{
185 if (Operands.size() != Other.Operands.size()) return ~1;
Craig Topperad87dc02013-07-23 06:25:00 +0000186
Sean Callananb7e8f4a2010-02-09 21:50:41 +0000187 unsigned MismatchOperand = ~0U;
188 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
189 if (Operands[i] != Other.Operands[i]) {
190 if (MismatchOperand != ~0U) // Already have one mismatch?
191 return ~1U;
Craig Topper64a6e4b2013-07-23 06:27:36 +0000192 MismatchOperand = i;
Sean Callananb7e8f4a2010-02-09 21:50:41 +0000193 }
194 }
195 return MismatchOperand;
Sean Callanand4b19e12010-02-09 23:06:35 +0000196}