1 //===- AsmWriterInst.h - Classes encapsulating a printable inst -----------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // These classes implement a parser for assembly strings. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "AsmWriterInst.h" 15 #include "CodeGenTarget.h" 16 #include "llvm/ADT/StringExtras.h" 17 #include "llvm/TableGen/Error.h" 18 #include "llvm/TableGen/Record.h" 19 20 using namespace llvm; 21 22 static bool isIdentChar(char C) { 23 return (C >= 'a' && C <= 'z') || 24 (C >= 'A' && C <= 'Z') || 25 (C >= '0' && C <= '9') || 26 C == '_'; 27 } 28 29 std::string AsmWriterOperand::getCode() const { 30 if (OperandType == isLiteralTextOperand) { 31 if (Str.size() == 1) 32 return "O << '" + Str + "'; "; 33 return "O << \"" + Str + "\"; "; 34 } 35 36 if (OperandType == isLiteralStatementOperand) 37 return Str; 38 39 std::string Result = Str + "(MI"; 40 if (MIOpNo != ~0U) 41 Result += ", " + utostr(MIOpNo); 42 Result += ", O"; 43 if (!MiModifier.empty()) 44 Result += ", \"" + MiModifier + '"'; 45 return Result + "); "; 46 } 47 48 /// ParseAsmString - Parse the specified Instruction's AsmString into this 49 /// AsmWriterInst. 50 /// 51 AsmWriterInst::AsmWriterInst(const CodeGenInstruction &CGI, unsigned Variant) { 52 this->CGI = &CGI; 53 54 // NOTE: Any extensions to this code need to be mirrored in the 55 // AsmPrinter::printInlineAsm code that executes as compile time (assuming 56 // that inline asm strings should also get the new feature)! 57 std::string AsmString = CGI.FlattenAsmStringVariants(CGI.AsmString, Variant); 58 std::string::size_type LastEmitted = 0; 59 while (LastEmitted != AsmString.size()) { 60 std::string::size_type DollarPos = 61 AsmString.find_first_of("$\\", LastEmitted); 62 if (DollarPos == std::string::npos) DollarPos = AsmString.size(); 63 64 // Emit a constant string fragment. 65 if (DollarPos != LastEmitted) { 66 for (; LastEmitted != DollarPos; ++LastEmitted) 67 switch (AsmString[LastEmitted]) { 68 case '\n': 69 AddLiteralString("\\n"); 70 break; 71 case '\t': 72 AddLiteralString("\\t"); 73 break; 74 case '"': 75 AddLiteralString("\\\""); 76 break; 77 case '\\': 78 AddLiteralString("\\\\"); 79 break; 80 default: 81 AddLiteralString(std::string(1, AsmString[LastEmitted])); 82 break; 83 } 84 } else if (AsmString[DollarPos] == '\\') { 85 if (DollarPos+1 != AsmString.size()) { 86 if (AsmString[DollarPos+1] == 'n') { 87 AddLiteralString("\\n"); 88 } else if (AsmString[DollarPos+1] == 't') { 89 AddLiteralString("\\t"); 90 } else if (std::string("${|}\\").find(AsmString[DollarPos+1]) 91 != std::string::npos) { 92 AddLiteralString(std::string(1, AsmString[DollarPos+1])); 93 } else { 94 PrintFatalError("Non-supported escaped character found in instruction '" + 95 CGI.TheDef->getName() + "'!"); 96 } 97 LastEmitted = DollarPos+2; 98 continue; 99 } 100 } else if (DollarPos+1 != AsmString.size() && 101 AsmString[DollarPos+1] == '$') { 102 AddLiteralString("$"); // "$$" -> $ 103 LastEmitted = DollarPos+2; 104 } else { 105 // Get the name of the variable. 106 std::string::size_type VarEnd = DollarPos+1; 107 108 // handle ${foo}bar as $foo by detecting whether the character following 109 // the dollar sign is a curly brace. If so, advance VarEnd and DollarPos 110 // so the variable name does not contain the leading curly brace. 111 bool hasCurlyBraces = false; 112 if (VarEnd < AsmString.size() && '{' == AsmString[VarEnd]) { 113 hasCurlyBraces = true; 114 ++DollarPos; 115 ++VarEnd; 116 } 117 118 while (VarEnd < AsmString.size() && isIdentChar(AsmString[VarEnd])) 119 ++VarEnd; 120 std::string VarName(AsmString.begin()+DollarPos+1, 121 AsmString.begin()+VarEnd); 122 123 // Modifier - Support ${foo:modifier} syntax, where "modifier" is passed 124 // into printOperand. Also support ${:feature}, which is passed into 125 // PrintSpecial. 126 std::string Modifier; 127 128 // In order to avoid starting the next string at the terminating curly 129 // brace, advance the end position past it if we found an opening curly 130 // brace. 131 if (hasCurlyBraces) { 132 if (VarEnd >= AsmString.size()) 133 PrintFatalError("Reached end of string before terminating curly brace in '" 134 + CGI.TheDef->getName() + "'"); 135 136 // Look for a modifier string. 137 if (AsmString[VarEnd] == ':') { 138 ++VarEnd; 139 if (VarEnd >= AsmString.size()) 140 PrintFatalError("Reached end of string before terminating curly brace in '" 141 + CGI.TheDef->getName() + "'"); 142 143 unsigned ModifierStart = VarEnd; 144 while (VarEnd < AsmString.size() && isIdentChar(AsmString[VarEnd])) 145 ++VarEnd; 146 Modifier = std::string(AsmString.begin()+ModifierStart, 147 AsmString.begin()+VarEnd); 148 if (Modifier.empty()) 149 PrintFatalError("Bad operand modifier name in '"+ CGI.TheDef->getName() + "'"); 150 } 151 152 if (AsmString[VarEnd] != '}') 153 PrintFatalError("Variable name beginning with '{' did not end with '}' in '" 154 + CGI.TheDef->getName() + "'"); 155 ++VarEnd; 156 } 157 if (VarName.empty() && Modifier.empty()) 158 PrintFatalError("Stray '$' in '" + CGI.TheDef->getName() + 159 "' asm string, maybe you want $$?"); 160 161 if (VarName.empty()) { 162 // Just a modifier, pass this into PrintSpecial. 163 Operands.push_back(AsmWriterOperand("PrintSpecial", 164 ~0U, 165 ~0U, 166 Modifier)); 167 } else { 168 // Otherwise, normal operand. 169 unsigned OpNo = CGI.Operands.getOperandNamed(VarName); 170 CGIOperandList::OperandInfo OpInfo = CGI.Operands[OpNo]; 171 172 unsigned MIOp = OpInfo.MIOperandNo; 173 Operands.push_back(AsmWriterOperand(OpInfo.PrinterMethodName, 174 OpNo, MIOp, Modifier)); 175 } 176 LastEmitted = VarEnd; 177 } 178 } 179 180 Operands.push_back(AsmWriterOperand("return;", 181 AsmWriterOperand::isLiteralStatementOperand)); 182 } 183 184 /// MatchesAllButOneOp - If this instruction is exactly identical to the 185 /// specified instruction except for one differing operand, return the differing 186 /// operand number. If more than one operand mismatches, return ~1, otherwise 187 /// if the instructions are identical return ~0. 188 unsigned AsmWriterInst::MatchesAllButOneOp(const AsmWriterInst &Other)const{ 189 if (Operands.size() != Other.Operands.size()) return ~1; 190 191 unsigned MismatchOperand = ~0U; 192 for (unsigned i = 0, e = Operands.size(); i != e; ++i) { 193 if (Operands[i] != Other.Operands[i]) { 194 if (MismatchOperand != ~0U) // Already have one mismatch? 195 return ~1U; 196 MismatchOperand = i; 197 } 198 } 199 return MismatchOperand; 200 } 201