1 //===- AsmWriterInst.h - Classes encapsulating a printable inst -----------===// 2 // 3 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // These classes implement a parser for assembly strings. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "AsmWriterInst.h" 14 #include "CodeGenTarget.h" 15 #include "llvm/ADT/StringExtras.h" 16 #include "llvm/TableGen/Error.h" 17 #include "llvm/TableGen/Record.h" 18 19 using namespace llvm; 20 21 static bool isIdentChar(char C) { 22 return (C >= 'a' && C <= 'z') || 23 (C >= 'A' && C <= 'Z') || 24 (C >= '0' && C <= '9') || 25 C == '_'; 26 } 27 28 std::string AsmWriterOperand::getCode(bool PassSubtarget) 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 (PassSubtarget) 42 Result += ", STI"; 43 Result += ", O"; 44 if (!MiModifier.empty()) 45 Result += ", \"" + MiModifier + '"'; 46 return Result + ");"; 47 } 48 49 /// ParseAsmString - Parse the specified Instruction's AsmString into this 50 /// AsmWriterInst. 51 /// 52 AsmWriterInst::AsmWriterInst(const CodeGenInstruction &CGI, unsigned CGIIndex, 53 unsigned Variant) 54 : CGI(&CGI), CGIIndex(CGIIndex) { 55 56 // NOTE: Any extensions to this code need to be mirrored in the 57 // AsmPrinter::printInlineAsm code that executes as compile time (assuming 58 // that inline asm strings should also get the new feature)! 59 std::string AsmString = CGI.FlattenAsmStringVariants(CGI.AsmString, Variant); 60 std::string::size_type LastEmitted = 0; 61 while (LastEmitted != AsmString.size()) { 62 std::string::size_type DollarPos = 63 AsmString.find_first_of("$\\", LastEmitted); 64 if (DollarPos == std::string::npos) DollarPos = AsmString.size(); 65 66 // Emit a constant string fragment. 67 if (DollarPos != LastEmitted) { 68 for (; LastEmitted != DollarPos; ++LastEmitted) 69 switch (AsmString[LastEmitted]) { 70 case '\n': 71 AddLiteralString("\\n"); 72 break; 73 case '\t': 74 AddLiteralString("\\t"); 75 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 } 86 } else if (AsmString[DollarPos] == '\\') { 87 if (DollarPos+1 != AsmString.size()) { 88 if (AsmString[DollarPos+1] == 'n') { 89 AddLiteralString("\\n"); 90 } else if (AsmString[DollarPos+1] == 't') { 91 AddLiteralString("\\t"); 92 } else if (std::string("${|}\\").find(AsmString[DollarPos+1]) 93 != std::string::npos) { 94 AddLiteralString(std::string(1, AsmString[DollarPos+1])); 95 } else { 96 PrintFatalError("Non-supported escaped character found in instruction '" + 97 CGI.TheDef->getName() + "'!"); 98 } 99 LastEmitted = DollarPos+2; 100 continue; 101 } 102 } else if (DollarPos+1 != AsmString.size() && 103 AsmString[DollarPos+1] == '$') { 104 AddLiteralString("$"); // "$$" -> $ 105 LastEmitted = DollarPos+2; 106 } else { 107 // Get the name of the variable. 108 std::string::size_type VarEnd = DollarPos+1; 109 110 // 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 } 119 120 while (VarEnd < AsmString.size() && isIdentChar(AsmString[VarEnd])) 121 ++VarEnd; 122 StringRef VarName(AsmString.data()+DollarPos+1, VarEnd-DollarPos-1); 123 124 // 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; 128 129 // 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()) 134 PrintFatalError("Reached end of string before terminating curly brace in '" 135 + CGI.TheDef->getName() + "'"); 136 137 // Look for a modifier string. 138 if (AsmString[VarEnd] == ':') { 139 ++VarEnd; 140 if (VarEnd >= AsmString.size()) 141 PrintFatalError("Reached end of string before terminating curly brace in '" 142 + CGI.TheDef->getName() + "'"); 143 144 std::string::size_type ModifierStart = VarEnd; 145 while (VarEnd < AsmString.size() && isIdentChar(AsmString[VarEnd])) 146 ++VarEnd; 147 Modifier = std::string(AsmString.begin()+ModifierStart, 148 AsmString.begin()+VarEnd); 149 if (Modifier.empty()) 150 PrintFatalError("Bad operand modifier name in '"+ CGI.TheDef->getName() + "'"); 151 } 152 153 if (AsmString[VarEnd] != '}') 154 PrintFatalError("Variable name beginning with '{' did not end with '}' in '" 155 + CGI.TheDef->getName() + "'"); 156 ++VarEnd; 157 } 158 if (VarName.empty() && Modifier.empty()) 159 PrintFatalError("Stray '$' in '" + CGI.TheDef->getName() + 160 "' asm string, maybe you want $$?"); 161 162 if (VarName.empty()) { 163 // Just a modifier, pass this into PrintSpecial. 164 Operands.emplace_back("PrintSpecial", ~0U, Modifier); 165 } else { 166 // Otherwise, normal operand. 167 unsigned OpNo = CGI.Operands.getOperandNamed(VarName); 168 CGIOperandList::OperandInfo OpInfo = CGI.Operands[OpNo]; 169 170 unsigned MIOp = OpInfo.MIOperandNo; 171 Operands.emplace_back(OpInfo.PrinterMethodName, MIOp, Modifier); 172 } 173 LastEmitted = VarEnd; 174 } 175 } 176 177 Operands.emplace_back("return;", AsmWriterOperand::isLiteralStatementOperand); 178 } 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. 184 unsigned AsmWriterInst::MatchesAllButOneOp(const AsmWriterInst &Other)const{ 185 if (Operands.size() != Other.Operands.size()) return ~1; 186 187 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; 192 MismatchOperand = i; 193 } 194 } 195 return MismatchOperand; 196 } 197