Chris Lattner | 2e1f51b | 2004-08-01 05:59:33 +0000 | [diff] [blame] | 1 | //===- AsmWriterEmitter.cpp - Generate an assembly writer -----------------===// |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 2e1f51b | 2004-08-01 05:59:33 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | 2e1f51b | 2004-08-01 05:59:33 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This tablegen backend is emits an assembly printer for the current target. |
| 11 | // Note that this is currently fairly skeletal, but will grow over time. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "AsmWriterEmitter.h" |
| 16 | #include "CodeGenTarget.h" |
Chris Lattner | 175580c | 2004-08-14 22:50:53 +0000 | [diff] [blame] | 17 | #include "Record.h" |
Chris Lattner | 6af022f | 2006-07-14 22:59:11 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | bdff5f9 | 2006-07-18 17:18:03 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Debug.h" |
| 20 | #include "llvm/Support/MathExtras.h" |
Jeff Cohen | 615ed99 | 2005-01-22 18:50:10 +0000 | [diff] [blame] | 21 | #include <algorithm> |
Chris Lattner | 2e1f51b | 2004-08-01 05:59:33 +0000 | [diff] [blame] | 22 | #include <ostream> |
| 23 | using namespace llvm; |
| 24 | |
Chris Lattner | 076efa7 | 2004-08-01 07:43:02 +0000 | [diff] [blame] | 25 | static bool isIdentChar(char C) { |
| 26 | return (C >= 'a' && C <= 'z') || |
| 27 | (C >= 'A' && C <= 'Z') || |
| 28 | (C >= '0' && C <= '9') || |
| 29 | C == '_'; |
| 30 | } |
| 31 | |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame] | 32 | namespace { |
| 33 | struct AsmWriterOperand { |
| 34 | enum { isLiteralTextOperand, isMachineInstrOperand } OperandType; |
| 35 | |
| 36 | /// Str - For isLiteralTextOperand, this IS the literal text. For |
| 37 | /// isMachineInstrOperand, this is the PrinterMethodName for the operand. |
| 38 | std::string Str; |
| 39 | |
| 40 | /// MiOpNo - For isMachineInstrOperand, this is the operand number of the |
| 41 | /// machine instruction. |
| 42 | unsigned MIOpNo; |
Chris Lattner | 04cadb3 | 2006-02-06 23:40:48 +0000 | [diff] [blame] | 43 | |
| 44 | /// MiModifier - For isMachineInstrOperand, this is the modifier string for |
| 45 | /// an operand, specified with syntax like ${opname:modifier}. |
| 46 | std::string MiModifier; |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame] | 47 | |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame] | 48 | AsmWriterOperand(const std::string &LitStr) |
Nate Begeman | 391c5d2 | 2005-11-30 18:54:35 +0000 | [diff] [blame] | 49 | : OperandType(isLiteralTextOperand), Str(LitStr) {} |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame] | 50 | |
Chris Lattner | 04cadb3 | 2006-02-06 23:40:48 +0000 | [diff] [blame] | 51 | AsmWriterOperand(const std::string &Printer, unsigned OpNo, |
| 52 | const std::string &Modifier) |
| 53 | : OperandType(isMachineInstrOperand), Str(Printer), MIOpNo(OpNo), |
| 54 | MiModifier(Modifier) {} |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame] | 55 | |
Chris Lattner | 870c016 | 2005-01-22 18:38:13 +0000 | [diff] [blame] | 56 | bool operator!=(const AsmWriterOperand &Other) const { |
| 57 | if (OperandType != Other.OperandType || Str != Other.Str) return true; |
| 58 | if (OperandType == isMachineInstrOperand) |
Chris Lattner | 04cadb3 | 2006-02-06 23:40:48 +0000 | [diff] [blame] | 59 | return MIOpNo != Other.MIOpNo || MiModifier != Other.MiModifier; |
Chris Lattner | 870c016 | 2005-01-22 18:38:13 +0000 | [diff] [blame] | 60 | return false; |
| 61 | } |
Chris Lattner | 38c0751 | 2005-01-22 20:31:17 +0000 | [diff] [blame] | 62 | bool operator==(const AsmWriterOperand &Other) const { |
| 63 | return !operator!=(Other); |
| 64 | } |
Chris Lattner | bdff5f9 | 2006-07-18 17:18:03 +0000 | [diff] [blame] | 65 | |
| 66 | /// getCode - Return the code that prints this operand. |
| 67 | std::string getCode() const; |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame] | 68 | }; |
Chris Lattner | bdff5f9 | 2006-07-18 17:18:03 +0000 | [diff] [blame] | 69 | } |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame] | 70 | |
Chris Lattner | bdff5f9 | 2006-07-18 17:18:03 +0000 | [diff] [blame] | 71 | namespace llvm { |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame] | 72 | struct AsmWriterInst { |
| 73 | std::vector<AsmWriterOperand> Operands; |
Chris Lattner | 5765dba | 2005-01-22 17:40:38 +0000 | [diff] [blame] | 74 | const CodeGenInstruction *CGI; |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 75 | |
Chris Lattner | 5765dba | 2005-01-22 17:40:38 +0000 | [diff] [blame] | 76 | AsmWriterInst(const CodeGenInstruction &CGI, unsigned Variant); |
Chris Lattner | 870c016 | 2005-01-22 18:38:13 +0000 | [diff] [blame] | 77 | |
Chris Lattner | f876668 | 2005-01-22 19:22:23 +0000 | [diff] [blame] | 78 | /// MatchesAllButOneOp - If this instruction is exactly identical to the |
| 79 | /// specified instruction except for one differing operand, return the |
| 80 | /// differing operand number. Otherwise return ~0. |
| 81 | unsigned MatchesAllButOneOp(const AsmWriterInst &Other) const; |
Chris Lattner | 870c016 | 2005-01-22 18:38:13 +0000 | [diff] [blame] | 82 | |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame] | 83 | private: |
| 84 | void AddLiteralString(const std::string &Str) { |
| 85 | // If the last operand was already a literal text string, append this to |
| 86 | // it, otherwise add a new operand. |
| 87 | if (!Operands.empty() && |
| 88 | Operands.back().OperandType == AsmWriterOperand::isLiteralTextOperand) |
| 89 | Operands.back().Str.append(Str); |
| 90 | else |
| 91 | Operands.push_back(AsmWriterOperand(Str)); |
| 92 | } |
| 93 | }; |
| 94 | } |
| 95 | |
| 96 | |
Chris Lattner | bdff5f9 | 2006-07-18 17:18:03 +0000 | [diff] [blame] | 97 | std::string AsmWriterOperand::getCode() const { |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame] | 98 | if (OperandType == isLiteralTextOperand) |
Chris Lattner | bdff5f9 | 2006-07-18 17:18:03 +0000 | [diff] [blame] | 99 | return "O << \"" + Str + "\"; "; |
| 100 | |
| 101 | std::string Result = Str + "(MI, " + utostr(MIOpNo); |
| 102 | if (!MiModifier.empty()) |
| 103 | Result += ", \"" + MiModifier + '"'; |
| 104 | return Result + "); "; |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | |
| 108 | /// ParseAsmString - Parse the specified Instruction's AsmString into this |
| 109 | /// AsmWriterInst. |
| 110 | /// |
Chris Lattner | 5765dba | 2005-01-22 17:40:38 +0000 | [diff] [blame] | 111 | AsmWriterInst::AsmWriterInst(const CodeGenInstruction &CGI, unsigned Variant) { |
| 112 | this->CGI = &CGI; |
Chris Lattner | b03b080 | 2006-02-06 22:43:28 +0000 | [diff] [blame] | 113 | unsigned CurVariant = ~0U; // ~0 if we are outside a {.|.|.} region, other #. |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame] | 114 | |
Chris Lattner | 1cf9d96 | 2006-02-01 19:12:23 +0000 | [diff] [blame] | 115 | // NOTE: Any extensions to this code need to be mirrored in the |
| 116 | // AsmPrinter::printInlineAsm code that executes as compile time (assuming |
| 117 | // that inline asm strings should also get the new feature)! |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame] | 118 | const std::string &AsmString = CGI.AsmString; |
| 119 | std::string::size_type LastEmitted = 0; |
| 120 | while (LastEmitted != AsmString.size()) { |
| 121 | std::string::size_type DollarPos = |
| 122 | AsmString.find_first_of("${|}", LastEmitted); |
| 123 | if (DollarPos == std::string::npos) DollarPos = AsmString.size(); |
| 124 | |
| 125 | // Emit a constant string fragment. |
| 126 | if (DollarPos != LastEmitted) { |
| 127 | // TODO: this should eventually handle escaping. |
Chris Lattner | b03b080 | 2006-02-06 22:43:28 +0000 | [diff] [blame] | 128 | if (CurVariant == Variant || CurVariant == ~0U) |
| 129 | AddLiteralString(std::string(AsmString.begin()+LastEmitted, |
| 130 | AsmString.begin()+DollarPos)); |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame] | 131 | LastEmitted = DollarPos; |
| 132 | } else if (AsmString[DollarPos] == '{') { |
Chris Lattner | b03b080 | 2006-02-06 22:43:28 +0000 | [diff] [blame] | 133 | if (CurVariant != ~0U) |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 134 | throw "Nested variants found for instruction '" + |
Chris Lattner | 3e3def9 | 2005-07-15 22:43:04 +0000 | [diff] [blame] | 135 | CGI.TheDef->getName() + "'!"; |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame] | 136 | LastEmitted = DollarPos+1; |
Chris Lattner | b03b080 | 2006-02-06 22:43:28 +0000 | [diff] [blame] | 137 | CurVariant = 0; // We are now inside of the variant! |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame] | 138 | } else if (AsmString[DollarPos] == '|') { |
Chris Lattner | b03b080 | 2006-02-06 22:43:28 +0000 | [diff] [blame] | 139 | if (CurVariant == ~0U) |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame] | 140 | throw "'|' character found outside of a variant in instruction '" |
Chris Lattner | 3e3def9 | 2005-07-15 22:43:04 +0000 | [diff] [blame] | 141 | + CGI.TheDef->getName() + "'!"; |
Chris Lattner | b03b080 | 2006-02-06 22:43:28 +0000 | [diff] [blame] | 142 | ++CurVariant; |
| 143 | ++LastEmitted; |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame] | 144 | } else if (AsmString[DollarPos] == '}') { |
Chris Lattner | b03b080 | 2006-02-06 22:43:28 +0000 | [diff] [blame] | 145 | if (CurVariant == ~0U) |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame] | 146 | throw "'}' character found outside of a variant in instruction '" |
Chris Lattner | 3e3def9 | 2005-07-15 22:43:04 +0000 | [diff] [blame] | 147 | + CGI.TheDef->getName() + "'!"; |
Chris Lattner | b03b080 | 2006-02-06 22:43:28 +0000 | [diff] [blame] | 148 | ++LastEmitted; |
| 149 | CurVariant = ~0U; |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame] | 150 | } else if (DollarPos+1 != AsmString.size() && |
| 151 | AsmString[DollarPos+1] == '$') { |
Chris Lattner | b03b080 | 2006-02-06 22:43:28 +0000 | [diff] [blame] | 152 | if (CurVariant == Variant || CurVariant == ~0U) |
| 153 | AddLiteralString("$"); // "$$" -> $ |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame] | 154 | LastEmitted = DollarPos+2; |
| 155 | } else { |
| 156 | // Get the name of the variable. |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame] | 157 | std::string::size_type VarEnd = DollarPos+1; |
Nate Begeman | afc5456 | 2005-07-14 22:50:30 +0000 | [diff] [blame] | 158 | |
| 159 | // handle ${foo}bar as $foo by detecting whether the character following |
| 160 | // the dollar sign is a curly brace. If so, advance VarEnd and DollarPos |
| 161 | // so the variable name does not contain the leading curly brace. |
| 162 | bool hasCurlyBraces = false; |
| 163 | if (VarEnd < AsmString.size() && '{' == AsmString[VarEnd]) { |
| 164 | hasCurlyBraces = true; |
| 165 | ++DollarPos; |
| 166 | ++VarEnd; |
| 167 | } |
| 168 | |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame] | 169 | while (VarEnd < AsmString.size() && isIdentChar(AsmString[VarEnd])) |
| 170 | ++VarEnd; |
| 171 | std::string VarName(AsmString.begin()+DollarPos+1, |
| 172 | AsmString.begin()+VarEnd); |
Nate Begeman | afc5456 | 2005-07-14 22:50:30 +0000 | [diff] [blame] | 173 | |
Chris Lattner | 04cadb3 | 2006-02-06 23:40:48 +0000 | [diff] [blame] | 174 | // Modifier - Support ${foo:modifier} syntax, where "modifier" is passed |
| 175 | // into printOperand. |
| 176 | std::string Modifier; |
| 177 | |
Nate Begeman | afc5456 | 2005-07-14 22:50:30 +0000 | [diff] [blame] | 178 | // In order to avoid starting the next string at the terminating curly |
| 179 | // brace, advance the end position past it if we found an opening curly |
| 180 | // brace. |
| 181 | if (hasCurlyBraces) { |
| 182 | if (VarEnd >= AsmString.size()) |
| 183 | throw "Reached end of string before terminating curly brace in '" |
Chris Lattner | 3e3def9 | 2005-07-15 22:43:04 +0000 | [diff] [blame] | 184 | + CGI.TheDef->getName() + "'"; |
Chris Lattner | 04cadb3 | 2006-02-06 23:40:48 +0000 | [diff] [blame] | 185 | |
| 186 | // Look for a modifier string. |
| 187 | if (AsmString[VarEnd] == ':') { |
| 188 | ++VarEnd; |
| 189 | if (VarEnd >= AsmString.size()) |
| 190 | throw "Reached end of string before terminating curly brace in '" |
| 191 | + CGI.TheDef->getName() + "'"; |
| 192 | |
| 193 | unsigned ModifierStart = VarEnd; |
| 194 | while (VarEnd < AsmString.size() && isIdentChar(AsmString[VarEnd])) |
| 195 | ++VarEnd; |
| 196 | Modifier = std::string(AsmString.begin()+ModifierStart, |
| 197 | AsmString.begin()+VarEnd); |
| 198 | if (Modifier.empty()) |
| 199 | throw "Bad operand modifier name in '"+ CGI.TheDef->getName() + "'"; |
| 200 | } |
| 201 | |
Nate Begeman | afc5456 | 2005-07-14 22:50:30 +0000 | [diff] [blame] | 202 | if (AsmString[VarEnd] != '}') |
Chris Lattner | b03b080 | 2006-02-06 22:43:28 +0000 | [diff] [blame] | 203 | throw "Variable name beginning with '{' did not end with '}' in '" |
Chris Lattner | 3e3def9 | 2005-07-15 22:43:04 +0000 | [diff] [blame] | 204 | + CGI.TheDef->getName() + "'"; |
Nate Begeman | afc5456 | 2005-07-14 22:50:30 +0000 | [diff] [blame] | 205 | ++VarEnd; |
| 206 | } |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame] | 207 | if (VarName.empty()) |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 208 | throw "Stray '$' in '" + CGI.TheDef->getName() + |
Chris Lattner | 3e3def9 | 2005-07-15 22:43:04 +0000 | [diff] [blame] | 209 | "' asm string, maybe you want $$?"; |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame] | 210 | |
| 211 | unsigned OpNo = CGI.getOperandNamed(VarName); |
Chris Lattner | 5765dba | 2005-01-22 17:40:38 +0000 | [diff] [blame] | 212 | CodeGenInstruction::OperandInfo OpInfo = CGI.OperandList[OpNo]; |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame] | 213 | |
| 214 | // If this is a two-address instruction and we are not accessing the |
| 215 | // 0th operand, remove an operand. |
Chris Lattner | 5765dba | 2005-01-22 17:40:38 +0000 | [diff] [blame] | 216 | unsigned MIOp = OpInfo.MIOperandNo; |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame] | 217 | if (CGI.isTwoAddress && MIOp != 0) { |
| 218 | if (MIOp == 1) |
| 219 | throw "Should refer to operand #0 instead of #1 for two-address" |
Chris Lattner | 3e3def9 | 2005-07-15 22:43:04 +0000 | [diff] [blame] | 220 | " instruction '" + CGI.TheDef->getName() + "'!"; |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame] | 221 | --MIOp; |
| 222 | } |
| 223 | |
Chris Lattner | b03b080 | 2006-02-06 22:43:28 +0000 | [diff] [blame] | 224 | if (CurVariant == Variant || CurVariant == ~0U) |
Chris Lattner | 04cadb3 | 2006-02-06 23:40:48 +0000 | [diff] [blame] | 225 | Operands.push_back(AsmWriterOperand(OpInfo.PrinterMethodName, MIOp, |
| 226 | Modifier)); |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame] | 227 | LastEmitted = VarEnd; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | AddLiteralString("\\n"); |
| 232 | } |
| 233 | |
Chris Lattner | f876668 | 2005-01-22 19:22:23 +0000 | [diff] [blame] | 234 | /// MatchesAllButOneOp - If this instruction is exactly identical to the |
| 235 | /// specified instruction except for one differing operand, return the differing |
| 236 | /// operand number. If more than one operand mismatches, return ~1, otherwise |
| 237 | /// if the instructions are identical return ~0. |
| 238 | unsigned AsmWriterInst::MatchesAllButOneOp(const AsmWriterInst &Other)const{ |
| 239 | if (Operands.size() != Other.Operands.size()) return ~1; |
Chris Lattner | 870c016 | 2005-01-22 18:38:13 +0000 | [diff] [blame] | 240 | |
| 241 | unsigned MismatchOperand = ~0U; |
| 242 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) { |
Chris Lattner | 870c016 | 2005-01-22 18:38:13 +0000 | [diff] [blame] | 243 | if (Operands[i] != Other.Operands[i]) |
Chris Lattner | f876668 | 2005-01-22 19:22:23 +0000 | [diff] [blame] | 244 | if (MismatchOperand != ~0U) // Already have one mismatch? |
| 245 | return ~1U; |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 246 | else |
Chris Lattner | 870c016 | 2005-01-22 18:38:13 +0000 | [diff] [blame] | 247 | MismatchOperand = i; |
| 248 | } |
| 249 | return MismatchOperand; |
| 250 | } |
| 251 | |
Chris Lattner | 38c0751 | 2005-01-22 20:31:17 +0000 | [diff] [blame] | 252 | static void PrintCases(std::vector<std::pair<std::string, |
| 253 | AsmWriterOperand> > &OpsToPrint, std::ostream &O) { |
| 254 | O << " case " << OpsToPrint.back().first << ": "; |
| 255 | AsmWriterOperand TheOp = OpsToPrint.back().second; |
| 256 | OpsToPrint.pop_back(); |
| 257 | |
| 258 | // Check to see if any other operands are identical in this list, and if so, |
| 259 | // emit a case label for them. |
| 260 | for (unsigned i = OpsToPrint.size(); i != 0; --i) |
| 261 | if (OpsToPrint[i-1].second == TheOp) { |
| 262 | O << "\n case " << OpsToPrint[i-1].first << ": "; |
| 263 | OpsToPrint.erase(OpsToPrint.begin()+i-1); |
| 264 | } |
| 265 | |
| 266 | // Finally, emit the code. |
Chris Lattner | bdff5f9 | 2006-07-18 17:18:03 +0000 | [diff] [blame] | 267 | O << TheOp.getCode(); |
Chris Lattner | 38c0751 | 2005-01-22 20:31:17 +0000 | [diff] [blame] | 268 | O << "break;\n"; |
| 269 | } |
| 270 | |
Chris Lattner | 870c016 | 2005-01-22 18:38:13 +0000 | [diff] [blame] | 271 | |
| 272 | /// EmitInstructions - Emit the last instruction in the vector and any other |
| 273 | /// instructions that are suitably similar to it. |
| 274 | static void EmitInstructions(std::vector<AsmWriterInst> &Insts, |
| 275 | std::ostream &O) { |
| 276 | AsmWriterInst FirstInst = Insts.back(); |
| 277 | Insts.pop_back(); |
| 278 | |
| 279 | std::vector<AsmWriterInst> SimilarInsts; |
| 280 | unsigned DifferingOperand = ~0; |
| 281 | for (unsigned i = Insts.size(); i != 0; --i) { |
Chris Lattner | f876668 | 2005-01-22 19:22:23 +0000 | [diff] [blame] | 282 | unsigned DiffOp = Insts[i-1].MatchesAllButOneOp(FirstInst); |
| 283 | if (DiffOp != ~1U) { |
Chris Lattner | 870c016 | 2005-01-22 18:38:13 +0000 | [diff] [blame] | 284 | if (DifferingOperand == ~0U) // First match! |
| 285 | DifferingOperand = DiffOp; |
| 286 | |
| 287 | // If this differs in the same operand as the rest of the instructions in |
| 288 | // this class, move it to the SimilarInsts list. |
Chris Lattner | f876668 | 2005-01-22 19:22:23 +0000 | [diff] [blame] | 289 | if (DifferingOperand == DiffOp || DiffOp == ~0U) { |
Chris Lattner | 870c016 | 2005-01-22 18:38:13 +0000 | [diff] [blame] | 290 | SimilarInsts.push_back(Insts[i-1]); |
| 291 | Insts.erase(Insts.begin()+i-1); |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | |
Chris Lattner | a1e8a80 | 2006-05-01 17:01:17 +0000 | [diff] [blame] | 296 | O << " case " << FirstInst.CGI->Namespace << "::" |
Chris Lattner | 870c016 | 2005-01-22 18:38:13 +0000 | [diff] [blame] | 297 | << FirstInst.CGI->TheDef->getName() << ":\n"; |
| 298 | for (unsigned i = 0, e = SimilarInsts.size(); i != e; ++i) |
Chris Lattner | a1e8a80 | 2006-05-01 17:01:17 +0000 | [diff] [blame] | 299 | O << " case " << SimilarInsts[i].CGI->Namespace << "::" |
Chris Lattner | 870c016 | 2005-01-22 18:38:13 +0000 | [diff] [blame] | 300 | << SimilarInsts[i].CGI->TheDef->getName() << ":\n"; |
| 301 | for (unsigned i = 0, e = FirstInst.Operands.size(); i != e; ++i) { |
| 302 | if (i != DifferingOperand) { |
| 303 | // If the operand is the same for all instructions, just print it. |
Chris Lattner | bdff5f9 | 2006-07-18 17:18:03 +0000 | [diff] [blame] | 304 | O << " " << FirstInst.Operands[i].getCode(); |
Chris Lattner | 870c016 | 2005-01-22 18:38:13 +0000 | [diff] [blame] | 305 | } else { |
| 306 | // If this is the operand that varies between all of the instructions, |
| 307 | // emit a switch for just this operand now. |
| 308 | O << " switch (MI->getOpcode()) {\n"; |
Chris Lattner | 38c0751 | 2005-01-22 20:31:17 +0000 | [diff] [blame] | 309 | std::vector<std::pair<std::string, AsmWriterOperand> > OpsToPrint; |
Chris Lattner | a1e8a80 | 2006-05-01 17:01:17 +0000 | [diff] [blame] | 310 | OpsToPrint.push_back(std::make_pair(FirstInst.CGI->Namespace + "::" + |
Chris Lattner | 38c0751 | 2005-01-22 20:31:17 +0000 | [diff] [blame] | 311 | FirstInst.CGI->TheDef->getName(), |
| 312 | FirstInst.Operands[i])); |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 313 | |
Chris Lattner | 870c016 | 2005-01-22 18:38:13 +0000 | [diff] [blame] | 314 | for (unsigned si = 0, e = SimilarInsts.size(); si != e; ++si) { |
Chris Lattner | 38c0751 | 2005-01-22 20:31:17 +0000 | [diff] [blame] | 315 | AsmWriterInst &AWI = SimilarInsts[si]; |
Chris Lattner | a1e8a80 | 2006-05-01 17:01:17 +0000 | [diff] [blame] | 316 | OpsToPrint.push_back(std::make_pair(AWI.CGI->Namespace+"::"+ |
Chris Lattner | 38c0751 | 2005-01-22 20:31:17 +0000 | [diff] [blame] | 317 | AWI.CGI->TheDef->getName(), |
| 318 | AWI.Operands[i])); |
Chris Lattner | 870c016 | 2005-01-22 18:38:13 +0000 | [diff] [blame] | 319 | } |
Chris Lattner | 38c0751 | 2005-01-22 20:31:17 +0000 | [diff] [blame] | 320 | std::reverse(OpsToPrint.begin(), OpsToPrint.end()); |
| 321 | while (!OpsToPrint.empty()) |
| 322 | PrintCases(OpsToPrint, O); |
Chris Lattner | 870c016 | 2005-01-22 18:38:13 +0000 | [diff] [blame] | 323 | O << " }"; |
| 324 | } |
| 325 | O << "\n"; |
| 326 | } |
| 327 | |
| 328 | O << " break;\n"; |
| 329 | } |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame] | 330 | |
Chris Lattner | bdff5f9 | 2006-07-18 17:18:03 +0000 | [diff] [blame] | 331 | void AsmWriterEmitter:: |
| 332 | FindUniqueOperandCommands(std::vector<std::string> &UniqueOperandCommands, |
| 333 | std::vector<unsigned> &InstIdxs, unsigned Op) const { |
| 334 | InstIdxs.clear(); |
| 335 | InstIdxs.resize(NumberedInstructions.size()); |
| 336 | |
| 337 | // This vector parallels UniqueOperandCommands, keeping track of which |
| 338 | // instructions each case are used for. It is a comma separated string of |
| 339 | // enums. |
| 340 | std::vector<std::string> InstrsForCase; |
| 341 | InstrsForCase.resize(UniqueOperandCommands.size()); |
| 342 | |
| 343 | for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) { |
| 344 | const AsmWriterInst *Inst = getAsmWriterInstByID(i); |
| 345 | if (Inst == 0) continue; // PHI, INLINEASM, etc. |
| 346 | |
| 347 | std::string Command; |
Chris Lattner | 191dd1f | 2006-07-18 17:50:22 +0000 | [diff] [blame^] | 348 | if (Op >= Inst->Operands.size()) |
Chris Lattner | bdff5f9 | 2006-07-18 17:18:03 +0000 | [diff] [blame] | 349 | continue; // Instruction already done. |
Chris Lattner | 191dd1f | 2006-07-18 17:50:22 +0000 | [diff] [blame^] | 350 | |
| 351 | Command = " " + Inst->Operands[Op].getCode() + "\n"; |
| 352 | |
| 353 | // If this is the last operand, emit a return. |
| 354 | if (Op == Inst->Operands.size()-1) |
| 355 | Command += " return true;\n"; |
Chris Lattner | bdff5f9 | 2006-07-18 17:18:03 +0000 | [diff] [blame] | 356 | |
| 357 | // Check to see if we already have 'Command' in UniqueOperandCommands. |
| 358 | // If not, add it. |
| 359 | bool FoundIt = false; |
| 360 | for (unsigned idx = 0, e = UniqueOperandCommands.size(); idx != e; ++idx) |
| 361 | if (UniqueOperandCommands[idx] == Command) { |
| 362 | InstIdxs[i] = idx; |
| 363 | InstrsForCase[idx] += ", "; |
| 364 | InstrsForCase[idx] += Inst->CGI->TheDef->getName(); |
| 365 | FoundIt = true; |
| 366 | break; |
| 367 | } |
| 368 | if (!FoundIt) { |
| 369 | InstIdxs[i] = UniqueOperandCommands.size(); |
| 370 | UniqueOperandCommands.push_back(Command); |
| 371 | InstrsForCase.push_back(Inst->CGI->TheDef->getName()); |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | // Prepend some of the instructions each case is used for onto the case val. |
| 376 | for (unsigned i = 0, e = InstrsForCase.size(); i != e; ++i) { |
| 377 | std::string Instrs = InstrsForCase[i]; |
| 378 | if (Instrs.size() > 70) { |
| 379 | Instrs.erase(Instrs.begin()+70, Instrs.end()); |
| 380 | Instrs += "..."; |
| 381 | } |
| 382 | |
| 383 | if (!Instrs.empty()) |
| 384 | UniqueOperandCommands[i] = " // " + Instrs + "\n" + |
| 385 | UniqueOperandCommands[i]; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | |
| 390 | |
Chris Lattner | 2e1f51b | 2004-08-01 05:59:33 +0000 | [diff] [blame] | 391 | void AsmWriterEmitter::run(std::ostream &O) { |
| 392 | EmitSourceFileHeader("Assembly Writer Source Fragment", O); |
| 393 | |
| 394 | CodeGenTarget Target; |
Chris Lattner | 175580c | 2004-08-14 22:50:53 +0000 | [diff] [blame] | 395 | Record *AsmWriter = Target.getAsmWriter(); |
Chris Lattner | 953c6fe | 2004-10-03 20:19:02 +0000 | [diff] [blame] | 396 | std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName"); |
| 397 | unsigned Variant = AsmWriter->getValueAsInt("Variant"); |
Chris Lattner | 175580c | 2004-08-14 22:50:53 +0000 | [diff] [blame] | 398 | |
Chris Lattner | 2e1f51b | 2004-08-01 05:59:33 +0000 | [diff] [blame] | 399 | O << |
| 400 | "/// printInstruction - This method is automatically generated by tablegen\n" |
| 401 | "/// from the instruction set description. This method returns true if the\n" |
| 402 | "/// machine instruction was sufficiently described to print it, otherwise\n" |
| 403 | "/// it returns false.\n" |
Chris Lattner | 953c6fe | 2004-10-03 20:19:02 +0000 | [diff] [blame] | 404 | "bool " << Target.getName() << ClassName |
Chris Lattner | 175580c | 2004-08-14 22:50:53 +0000 | [diff] [blame] | 405 | << "::printInstruction(const MachineInstr *MI) {\n"; |
Chris Lattner | 2e1f51b | 2004-08-01 05:59:33 +0000 | [diff] [blame] | 406 | |
Chris Lattner | 5765dba | 2005-01-22 17:40:38 +0000 | [diff] [blame] | 407 | std::vector<AsmWriterInst> Instructions; |
| 408 | |
Chris Lattner | 2e1f51b | 2004-08-01 05:59:33 +0000 | [diff] [blame] | 409 | for (CodeGenTarget::inst_iterator I = Target.inst_begin(), |
| 410 | E = Target.inst_end(); I != E; ++I) |
Chris Lattner | 5765dba | 2005-01-22 17:40:38 +0000 | [diff] [blame] | 411 | if (!I->second.AsmString.empty()) |
| 412 | Instructions.push_back(AsmWriterInst(I->second, Variant)); |
Chris Lattner | 076efa7 | 2004-08-01 07:43:02 +0000 | [diff] [blame] | 413 | |
Chris Lattner | bdff5f9 | 2006-07-18 17:18:03 +0000 | [diff] [blame] | 414 | // Get the instruction numbering. |
Chris Lattner | 0cfcc1e | 2006-01-27 02:10:50 +0000 | [diff] [blame] | 415 | Target.getInstructionsByEnumValue(NumberedInstructions); |
| 416 | |
Chris Lattner | 6af022f | 2006-07-14 22:59:11 +0000 | [diff] [blame] | 417 | // Compute the CodeGenInstruction -> AsmWriterInst mapping. Note that not |
| 418 | // all machine instructions are necessarily being printed, so there may be |
| 419 | // target instructions not in this map. |
Chris Lattner | 6af022f | 2006-07-14 22:59:11 +0000 | [diff] [blame] | 420 | for (unsigned i = 0, e = Instructions.size(); i != e; ++i) |
| 421 | CGIAWIMap.insert(std::make_pair(Instructions[i].CGI, &Instructions[i])); |
Chris Lattner | f876668 | 2005-01-22 19:22:23 +0000 | [diff] [blame] | 422 | |
Chris Lattner | 6af022f | 2006-07-14 22:59:11 +0000 | [diff] [blame] | 423 | // Build an aggregate string, and build a table of offsets into it. |
| 424 | std::map<std::string, unsigned> StringOffset; |
| 425 | std::string AggregateString; |
| 426 | AggregateString += '\0'; |
| 427 | |
Chris Lattner | 5561640 | 2006-07-18 17:32:27 +0000 | [diff] [blame] | 428 | /// OpcodeInfo - Theis encodes the index of the string to use for the first |
| 429 | /// chunk of the output as well as indices used for operand printing. |
| 430 | std::vector<unsigned> OpcodeInfo; |
Chris Lattner | bdff5f9 | 2006-07-18 17:18:03 +0000 | [diff] [blame] | 431 | |
Chris Lattner | 5561640 | 2006-07-18 17:32:27 +0000 | [diff] [blame] | 432 | unsigned MaxStringIdx = 0; |
Chris Lattner | 6af022f | 2006-07-14 22:59:11 +0000 | [diff] [blame] | 433 | for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) { |
| 434 | AsmWriterInst *AWI = CGIAWIMap[NumberedInstructions[i]]; |
| 435 | unsigned Idx; |
| 436 | if (AWI == 0 || AWI->Operands[0].Str.empty()) { |
| 437 | // Something not handled by the asmwriter printer. |
| 438 | Idx = 0; |
| 439 | } else { |
| 440 | unsigned &Entry = StringOffset[AWI->Operands[0].Str]; |
| 441 | if (Entry == 0) { |
| 442 | // Add the string to the aggregate if this is the first time found. |
Chris Lattner | 5561640 | 2006-07-18 17:32:27 +0000 | [diff] [blame] | 443 | MaxStringIdx = Entry = AggregateString.size(); |
Chris Lattner | 6af022f | 2006-07-14 22:59:11 +0000 | [diff] [blame] | 444 | std::string Str = AWI->Operands[0].Str; |
| 445 | UnescapeString(Str); |
| 446 | AggregateString += Str; |
| 447 | AggregateString += '\0'; |
Chris Lattner | f876668 | 2005-01-22 19:22:23 +0000 | [diff] [blame] | 448 | } |
Chris Lattner | 6af022f | 2006-07-14 22:59:11 +0000 | [diff] [blame] | 449 | Idx = Entry; |
Chris Lattner | 6af022f | 2006-07-14 22:59:11 +0000 | [diff] [blame] | 450 | |
| 451 | // Nuke the string from the operand list. It is now handled! |
| 452 | AWI->Operands.erase(AWI->Operands.begin()); |
Chris Lattner | f876668 | 2005-01-22 19:22:23 +0000 | [diff] [blame] | 453 | } |
Chris Lattner | 5561640 | 2006-07-18 17:32:27 +0000 | [diff] [blame] | 454 | OpcodeInfo.push_back(Idx); |
Chris Lattner | f876668 | 2005-01-22 19:22:23 +0000 | [diff] [blame] | 455 | } |
Chris Lattner | bdff5f9 | 2006-07-18 17:18:03 +0000 | [diff] [blame] | 456 | |
Chris Lattner | 5561640 | 2006-07-18 17:32:27 +0000 | [diff] [blame] | 457 | // Figure out how many bits we used for the string index. |
| 458 | unsigned AsmStrBits = Log2_32_Ceil(MaxStringIdx); |
| 459 | |
Chris Lattner | bdff5f9 | 2006-07-18 17:18:03 +0000 | [diff] [blame] | 460 | // To reduce code size, we compactify common instructions into a few bits |
| 461 | // in the opcode-indexed table. |
Chris Lattner | b51ecd4 | 2006-07-18 17:38:46 +0000 | [diff] [blame] | 462 | unsigned BitsLeft = 32-AsmStrBits; |
Chris Lattner | bdff5f9 | 2006-07-18 17:18:03 +0000 | [diff] [blame] | 463 | |
| 464 | std::vector<std::vector<std::string> > TableDrivenOperandPrinters; |
| 465 | |
| 466 | for (unsigned i = 0; ; ++i) { |
| 467 | std::vector<std::string> UniqueOperandCommands; |
| 468 | |
| 469 | // For the first operand check, add a default value that unhandled |
| 470 | // instructions will use. |
| 471 | if (i == 0) |
| 472 | UniqueOperandCommands.push_back(" return false;\n"); |
| 473 | |
| 474 | std::vector<unsigned> InstIdxs; |
| 475 | FindUniqueOperandCommands(UniqueOperandCommands, InstIdxs, i); |
| 476 | |
| 477 | // If we ran out of operands to print, we're done. |
| 478 | if (UniqueOperandCommands.empty()) break; |
| 479 | |
| 480 | // FIXME: GROW THEM MAXIMALLY. |
| 481 | |
| 482 | // Compute the number of bits we need to represent these cases, this is |
| 483 | // ceil(log2(numentries)). |
| 484 | unsigned NumBits = Log2_32_Ceil(UniqueOperandCommands.size()); |
| 485 | |
| 486 | // If we don't have enough bits for this operand, don't include it. |
| 487 | if (NumBits > BitsLeft) { |
| 488 | DEBUG(std::cerr << "Not enough bits to densely encode " << NumBits |
| 489 | << " more bits\n"); |
| 490 | break; |
| 491 | } |
| 492 | |
| 493 | // Otherwise, we can include this in the initial lookup table. Add it in. |
| 494 | BitsLeft -= NumBits; |
| 495 | for (unsigned i = 0, e = InstIdxs.size(); i != e; ++i) |
Chris Lattner | 5561640 | 2006-07-18 17:32:27 +0000 | [diff] [blame] | 496 | OpcodeInfo[i] |= InstIdxs[i] << (BitsLeft+AsmStrBits); |
Chris Lattner | bdff5f9 | 2006-07-18 17:18:03 +0000 | [diff] [blame] | 497 | |
| 498 | TableDrivenOperandPrinters.push_back(UniqueOperandCommands); |
| 499 | } |
| 500 | |
| 501 | |
| 502 | |
Chris Lattner | 5561640 | 2006-07-18 17:32:27 +0000 | [diff] [blame] | 503 | O<<" static const unsigned OpInfo[] = {\n"; |
Chris Lattner | bdff5f9 | 2006-07-18 17:18:03 +0000 | [diff] [blame] | 504 | for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) { |
Chris Lattner | b51ecd4 | 2006-07-18 17:38:46 +0000 | [diff] [blame] | 505 | O << " " << OpcodeInfo[i] << "U,\t// " |
Chris Lattner | 5561640 | 2006-07-18 17:32:27 +0000 | [diff] [blame] | 506 | << NumberedInstructions[i]->TheDef->getName() << "\n"; |
Chris Lattner | bdff5f9 | 2006-07-18 17:18:03 +0000 | [diff] [blame] | 507 | } |
| 508 | // Add a dummy entry so the array init doesn't end with a comma. |
Chris Lattner | 5561640 | 2006-07-18 17:32:27 +0000 | [diff] [blame] | 509 | O << " 0U\n"; |
Chris Lattner | 6af022f | 2006-07-14 22:59:11 +0000 | [diff] [blame] | 510 | O << " };\n\n"; |
| 511 | |
| 512 | // Emit the string itself. |
| 513 | O << " const char *AsmStrs = \n \""; |
| 514 | unsigned CharsPrinted = 0; |
| 515 | EscapeString(AggregateString); |
| 516 | for (unsigned i = 0, e = AggregateString.size(); i != e; ++i) { |
| 517 | if (CharsPrinted > 70) { |
| 518 | O << "\"\n \""; |
| 519 | CharsPrinted = 0; |
| 520 | } |
| 521 | O << AggregateString[i]; |
| 522 | ++CharsPrinted; |
| 523 | |
| 524 | // Print escape sequences all together. |
| 525 | if (AggregateString[i] == '\\') { |
| 526 | assert(i+1 < AggregateString.size() && "Incomplete escape sequence!"); |
| 527 | if (isdigit(AggregateString[i+1])) { |
| 528 | assert(isdigit(AggregateString[i+2]) && isdigit(AggregateString[i+3]) && |
| 529 | "Expected 3 digit octal escape!"); |
| 530 | O << AggregateString[++i]; |
| 531 | O << AggregateString[++i]; |
| 532 | O << AggregateString[++i]; |
| 533 | CharsPrinted += 3; |
| 534 | } else { |
| 535 | O << AggregateString[++i]; |
| 536 | ++CharsPrinted; |
| 537 | } |
| 538 | } |
| 539 | } |
| 540 | O << "\";\n\n"; |
| 541 | |
Chris Lattner | bdff5f9 | 2006-07-18 17:18:03 +0000 | [diff] [blame] | 542 | O << " if (MI->getOpcode() == TargetInstrInfo::INLINEASM) {\n" |
| 543 | << " printInlineAsm(MI);\n" |
| 544 | << " return true;\n" |
| 545 | << " }\n\n"; |
| 546 | |
Chris Lattner | 6af022f | 2006-07-14 22:59:11 +0000 | [diff] [blame] | 547 | O << " // Emit the opcode for the instruction.\n" |
Chris Lattner | 5561640 | 2006-07-18 17:32:27 +0000 | [diff] [blame] | 548 | << " unsigned Bits = OpInfo[MI->getOpcode()];\n" |
| 549 | << " O << AsmStrs+(Bits & " << (1 << AsmStrBits)-1 << ");\n\n"; |
Chris Lattner | f876668 | 2005-01-22 19:22:23 +0000 | [diff] [blame] | 550 | |
Chris Lattner | bdff5f9 | 2006-07-18 17:18:03 +0000 | [diff] [blame] | 551 | // Output the table driven operand information. |
Chris Lattner | b51ecd4 | 2006-07-18 17:38:46 +0000 | [diff] [blame] | 552 | BitsLeft = 32-AsmStrBits; |
Chris Lattner | bdff5f9 | 2006-07-18 17:18:03 +0000 | [diff] [blame] | 553 | for (unsigned i = 0, e = TableDrivenOperandPrinters.size(); i != e; ++i) { |
| 554 | std::vector<std::string> &Commands = TableDrivenOperandPrinters[i]; |
| 555 | |
| 556 | // Compute the number of bits we need to represent these cases, this is |
| 557 | // ceil(log2(numentries)). |
| 558 | unsigned NumBits = Log2_32_Ceil(Commands.size()); |
| 559 | assert(NumBits <= BitsLeft && "consistency error"); |
| 560 | |
| 561 | // Emit code to extract this field from Bits. |
| 562 | BitsLeft -= NumBits; |
| 563 | |
| 564 | O << "\n // Fragment " << i << " encoded into " << NumBits |
Chris Lattner | e7a589d | 2006-07-18 17:43:54 +0000 | [diff] [blame] | 565 | << " bits for " << Commands.size() << " unique commands.\n"; |
Chris Lattner | bdff5f9 | 2006-07-18 17:18:03 +0000 | [diff] [blame] | 566 | |
Chris Lattner | e7a589d | 2006-07-18 17:43:54 +0000 | [diff] [blame] | 567 | if (Commands.size() == 1) { |
| 568 | // Only one possibility, just emit it. |
| 569 | O << Commands[0]; |
| 570 | } else if (Commands.size() == 2) { |
| 571 | // Emit two possibilitys with if/else. |
| 572 | O << " if ((Bits >> " << (BitsLeft+AsmStrBits) << ") & " |
| 573 | << ((1 << NumBits)-1) << ") {\n" |
| 574 | << Commands[1] |
| 575 | << " } else {\n" |
| 576 | << Commands[0] |
| 577 | << " }\n\n"; |
| 578 | } else { |
| 579 | O << " switch ((Bits >> " << (BitsLeft+AsmStrBits) << ") & " |
| 580 | << ((1 << NumBits)-1) << ") {\n" |
| 581 | << " default: // unreachable.\n"; |
| 582 | |
| 583 | // Print out all the cases. |
| 584 | for (unsigned i = 0, e = Commands.size(); i != e; ++i) { |
| 585 | O << " case " << i << ":\n"; |
| 586 | O << Commands[i]; |
| 587 | O << " break;\n"; |
| 588 | } |
| 589 | O << " }\n\n"; |
Chris Lattner | bdff5f9 | 2006-07-18 17:18:03 +0000 | [diff] [blame] | 590 | } |
Chris Lattner | bdff5f9 | 2006-07-18 17:18:03 +0000 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | // Okay, go through and strip out the operand information that we just |
| 594 | // emitted. |
| 595 | unsigned NumOpsToRemove = TableDrivenOperandPrinters.size(); |
| 596 | for (unsigned i = 0, e = Instructions.size(); i != e; ++i) { |
| 597 | // Entire instruction has been emitted? |
| 598 | AsmWriterInst &Inst = Instructions[i]; |
| 599 | if (Inst.Operands.size() <= NumOpsToRemove) { |
| 600 | Instructions.erase(Instructions.begin()+i); |
| 601 | --i; --e; |
| 602 | } else { |
| 603 | Inst.Operands.erase(Inst.Operands.begin(), |
| 604 | Inst.Operands.begin()+NumOpsToRemove); |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | |
| 609 | // Because this is a vector, we want to emit from the end. Reverse all of the |
Chris Lattner | 870c016 | 2005-01-22 18:38:13 +0000 | [diff] [blame] | 610 | // elements in the vector. |
| 611 | std::reverse(Instructions.begin(), Instructions.end()); |
Chris Lattner | bdff5f9 | 2006-07-18 17:18:03 +0000 | [diff] [blame] | 612 | |
Chris Lattner | b51ecd4 | 2006-07-18 17:38:46 +0000 | [diff] [blame] | 613 | if (!Instructions.empty()) { |
| 614 | // Find the opcode # of inline asm. |
| 615 | O << " switch (MI->getOpcode()) {\n"; |
| 616 | while (!Instructions.empty()) |
| 617 | EmitInstructions(Instructions, O); |
Chris Lattner | 870c016 | 2005-01-22 18:38:13 +0000 | [diff] [blame] | 618 | |
Chris Lattner | b51ecd4 | 2006-07-18 17:38:46 +0000 | [diff] [blame] | 619 | O << " }\n"; |
| 620 | } |
| 621 | |
| 622 | O << " return true;\n" |
Chris Lattner | 2e1f51b | 2004-08-01 05:59:33 +0000 | [diff] [blame] | 623 | "}\n"; |
Chris Lattner | 2e1f51b | 2004-08-01 05:59:33 +0000 | [diff] [blame] | 624 | } |