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" |
Jeff Cohen | 615ed99 | 2005-01-22 18:50:10 +0000 | [diff] [blame] | 18 | #include <algorithm> |
Chris Lattner | 2e1f51b | 2004-08-01 05:59:33 +0000 | [diff] [blame] | 19 | #include <ostream> |
| 20 | using namespace llvm; |
| 21 | |
Chris Lattner | 076efa7 | 2004-08-01 07:43:02 +0000 | [diff] [blame] | 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 | |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame] | 29 | namespace { |
| 30 | struct AsmWriterOperand { |
| 31 | enum { isLiteralTextOperand, isMachineInstrOperand } OperandType; |
| 32 | |
| 33 | /// Str - For isLiteralTextOperand, this IS the literal text. For |
| 34 | /// isMachineInstrOperand, this is the PrinterMethodName for the operand. |
| 35 | std::string Str; |
| 36 | |
| 37 | /// MiOpNo - For isMachineInstrOperand, this is the operand number of the |
| 38 | /// machine instruction. |
| 39 | unsigned MIOpNo; |
Chris Lattner | 04cadb3 | 2006-02-06 23:40:48 +0000 | [diff] [blame] | 40 | |
| 41 | /// MiModifier - For isMachineInstrOperand, this is the modifier string for |
| 42 | /// an operand, specified with syntax like ${opname:modifier}. |
| 43 | std::string MiModifier; |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame] | 44 | |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame] | 45 | AsmWriterOperand(const std::string &LitStr) |
Nate Begeman | 391c5d2 | 2005-11-30 18:54:35 +0000 | [diff] [blame] | 46 | : OperandType(isLiteralTextOperand), Str(LitStr) {} |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame] | 47 | |
Chris Lattner | 04cadb3 | 2006-02-06 23:40:48 +0000 | [diff] [blame] | 48 | AsmWriterOperand(const std::string &Printer, unsigned OpNo, |
| 49 | const std::string &Modifier) |
| 50 | : OperandType(isMachineInstrOperand), Str(Printer), MIOpNo(OpNo), |
| 51 | MiModifier(Modifier) {} |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame] | 52 | |
Chris Lattner | 870c016 | 2005-01-22 18:38:13 +0000 | [diff] [blame] | 53 | bool operator!=(const AsmWriterOperand &Other) const { |
| 54 | if (OperandType != Other.OperandType || Str != Other.Str) return true; |
| 55 | if (OperandType == isMachineInstrOperand) |
Chris Lattner | 04cadb3 | 2006-02-06 23:40:48 +0000 | [diff] [blame] | 56 | return MIOpNo != Other.MIOpNo || MiModifier != Other.MiModifier; |
Chris Lattner | 870c016 | 2005-01-22 18:38:13 +0000 | [diff] [blame] | 57 | return false; |
| 58 | } |
Chris Lattner | 38c0751 | 2005-01-22 20:31:17 +0000 | [diff] [blame] | 59 | bool operator==(const AsmWriterOperand &Other) const { |
| 60 | return !operator!=(Other); |
| 61 | } |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame] | 62 | void EmitCode(std::ostream &OS) const; |
| 63 | }; |
| 64 | |
| 65 | struct AsmWriterInst { |
| 66 | std::vector<AsmWriterOperand> Operands; |
Chris Lattner | 5765dba | 2005-01-22 17:40:38 +0000 | [diff] [blame] | 67 | const CodeGenInstruction *CGI; |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 68 | |
Chris Lattner | 5765dba | 2005-01-22 17:40:38 +0000 | [diff] [blame] | 69 | AsmWriterInst(const CodeGenInstruction &CGI, unsigned Variant); |
Chris Lattner | 870c016 | 2005-01-22 18:38:13 +0000 | [diff] [blame] | 70 | |
Chris Lattner | f876668 | 2005-01-22 19:22:23 +0000 | [diff] [blame] | 71 | /// MatchesAllButOneOp - If this instruction is exactly identical to the |
| 72 | /// specified instruction except for one differing operand, return the |
| 73 | /// differing operand number. Otherwise return ~0. |
| 74 | unsigned MatchesAllButOneOp(const AsmWriterInst &Other) const; |
Chris Lattner | 870c016 | 2005-01-22 18:38:13 +0000 | [diff] [blame] | 75 | |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame] | 76 | private: |
| 77 | void AddLiteralString(const std::string &Str) { |
| 78 | // If the last operand was already a literal text string, append this to |
| 79 | // it, otherwise add a new operand. |
| 80 | if (!Operands.empty() && |
| 81 | Operands.back().OperandType == AsmWriterOperand::isLiteralTextOperand) |
| 82 | Operands.back().Str.append(Str); |
| 83 | else |
| 84 | Operands.push_back(AsmWriterOperand(Str)); |
| 85 | } |
| 86 | }; |
| 87 | } |
| 88 | |
| 89 | |
| 90 | void AsmWriterOperand::EmitCode(std::ostream &OS) const { |
| 91 | if (OperandType == isLiteralTextOperand) |
| 92 | OS << "O << \"" << Str << "\"; "; |
Chris Lattner | 04cadb3 | 2006-02-06 23:40:48 +0000 | [diff] [blame] | 93 | else { |
| 94 | OS << Str << "(MI, " << MIOpNo; |
| 95 | if (!MiModifier.empty()) |
| 96 | OS << ", \"" << MiModifier << '"'; |
| 97 | OS << "); "; |
| 98 | } |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | |
| 102 | /// ParseAsmString - Parse the specified Instruction's AsmString into this |
| 103 | /// AsmWriterInst. |
| 104 | /// |
Chris Lattner | 5765dba | 2005-01-22 17:40:38 +0000 | [diff] [blame] | 105 | AsmWriterInst::AsmWriterInst(const CodeGenInstruction &CGI, unsigned Variant) { |
| 106 | this->CGI = &CGI; |
Chris Lattner | b03b080 | 2006-02-06 22:43:28 +0000 | [diff] [blame] | 107 | unsigned CurVariant = ~0U; // ~0 if we are outside a {.|.|.} region, other #. |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame] | 108 | |
Chris Lattner | 1cf9d96 | 2006-02-01 19:12:23 +0000 | [diff] [blame] | 109 | // NOTE: Any extensions to this code need to be mirrored in the |
| 110 | // AsmPrinter::printInlineAsm code that executes as compile time (assuming |
| 111 | // that inline asm strings should also get the new feature)! |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame] | 112 | const std::string &AsmString = CGI.AsmString; |
| 113 | std::string::size_type LastEmitted = 0; |
| 114 | while (LastEmitted != AsmString.size()) { |
| 115 | std::string::size_type DollarPos = |
| 116 | AsmString.find_first_of("${|}", LastEmitted); |
| 117 | if (DollarPos == std::string::npos) DollarPos = AsmString.size(); |
| 118 | |
| 119 | // Emit a constant string fragment. |
| 120 | if (DollarPos != LastEmitted) { |
| 121 | // TODO: this should eventually handle escaping. |
Chris Lattner | b03b080 | 2006-02-06 22:43:28 +0000 | [diff] [blame] | 122 | if (CurVariant == Variant || CurVariant == ~0U) |
| 123 | AddLiteralString(std::string(AsmString.begin()+LastEmitted, |
| 124 | AsmString.begin()+DollarPos)); |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame] | 125 | LastEmitted = DollarPos; |
| 126 | } else if (AsmString[DollarPos] == '{') { |
Chris Lattner | b03b080 | 2006-02-06 22:43:28 +0000 | [diff] [blame] | 127 | if (CurVariant != ~0U) |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 128 | throw "Nested variants found for instruction '" + |
Chris Lattner | 3e3def9 | 2005-07-15 22:43:04 +0000 | [diff] [blame] | 129 | CGI.TheDef->getName() + "'!"; |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame] | 130 | LastEmitted = DollarPos+1; |
Chris Lattner | b03b080 | 2006-02-06 22:43:28 +0000 | [diff] [blame] | 131 | CurVariant = 0; // We are now inside of the variant! |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame] | 132 | } else if (AsmString[DollarPos] == '|') { |
Chris Lattner | b03b080 | 2006-02-06 22:43:28 +0000 | [diff] [blame] | 133 | if (CurVariant == ~0U) |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame] | 134 | throw "'|' character found outside of a variant in instruction '" |
Chris Lattner | 3e3def9 | 2005-07-15 22:43:04 +0000 | [diff] [blame] | 135 | + CGI.TheDef->getName() + "'!"; |
Chris Lattner | b03b080 | 2006-02-06 22:43:28 +0000 | [diff] [blame] | 136 | ++CurVariant; |
| 137 | ++LastEmitted; |
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 | ++LastEmitted; |
| 143 | CurVariant = ~0U; |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame] | 144 | } else if (DollarPos+1 != AsmString.size() && |
| 145 | AsmString[DollarPos+1] == '$') { |
Chris Lattner | b03b080 | 2006-02-06 22:43:28 +0000 | [diff] [blame] | 146 | if (CurVariant == Variant || CurVariant == ~0U) |
| 147 | AddLiteralString("$"); // "$$" -> $ |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame] | 148 | LastEmitted = DollarPos+2; |
| 149 | } else { |
| 150 | // Get the name of the variable. |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame] | 151 | std::string::size_type VarEnd = DollarPos+1; |
Nate Begeman | afc5456 | 2005-07-14 22:50:30 +0000 | [diff] [blame] | 152 | |
| 153 | // handle ${foo}bar as $foo by detecting whether the character following |
| 154 | // the dollar sign is a curly brace. If so, advance VarEnd and DollarPos |
| 155 | // so the variable name does not contain the leading curly brace. |
| 156 | bool hasCurlyBraces = false; |
| 157 | if (VarEnd < AsmString.size() && '{' == AsmString[VarEnd]) { |
| 158 | hasCurlyBraces = true; |
| 159 | ++DollarPos; |
| 160 | ++VarEnd; |
| 161 | } |
| 162 | |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame] | 163 | while (VarEnd < AsmString.size() && isIdentChar(AsmString[VarEnd])) |
| 164 | ++VarEnd; |
| 165 | std::string VarName(AsmString.begin()+DollarPos+1, |
| 166 | AsmString.begin()+VarEnd); |
Nate Begeman | afc5456 | 2005-07-14 22:50:30 +0000 | [diff] [blame] | 167 | |
Chris Lattner | 04cadb3 | 2006-02-06 23:40:48 +0000 | [diff] [blame] | 168 | // Modifier - Support ${foo:modifier} syntax, where "modifier" is passed |
| 169 | // into printOperand. |
| 170 | std::string Modifier; |
| 171 | |
Nate Begeman | afc5456 | 2005-07-14 22:50:30 +0000 | [diff] [blame] | 172 | // In order to avoid starting the next string at the terminating curly |
| 173 | // brace, advance the end position past it if we found an opening curly |
| 174 | // brace. |
| 175 | if (hasCurlyBraces) { |
| 176 | if (VarEnd >= AsmString.size()) |
| 177 | throw "Reached end of string before terminating curly brace in '" |
Chris Lattner | 3e3def9 | 2005-07-15 22:43:04 +0000 | [diff] [blame] | 178 | + CGI.TheDef->getName() + "'"; |
Chris Lattner | 04cadb3 | 2006-02-06 23:40:48 +0000 | [diff] [blame] | 179 | |
| 180 | // Look for a modifier string. |
| 181 | if (AsmString[VarEnd] == ':') { |
| 182 | ++VarEnd; |
| 183 | if (VarEnd >= AsmString.size()) |
| 184 | throw "Reached end of string before terminating curly brace in '" |
| 185 | + CGI.TheDef->getName() + "'"; |
| 186 | |
| 187 | unsigned ModifierStart = VarEnd; |
| 188 | while (VarEnd < AsmString.size() && isIdentChar(AsmString[VarEnd])) |
| 189 | ++VarEnd; |
| 190 | Modifier = std::string(AsmString.begin()+ModifierStart, |
| 191 | AsmString.begin()+VarEnd); |
| 192 | if (Modifier.empty()) |
| 193 | throw "Bad operand modifier name in '"+ CGI.TheDef->getName() + "'"; |
| 194 | } |
| 195 | |
Nate Begeman | afc5456 | 2005-07-14 22:50:30 +0000 | [diff] [blame] | 196 | if (AsmString[VarEnd] != '}') |
Chris Lattner | b03b080 | 2006-02-06 22:43:28 +0000 | [diff] [blame] | 197 | throw "Variable name beginning with '{' did not end with '}' in '" |
Chris Lattner | 3e3def9 | 2005-07-15 22:43:04 +0000 | [diff] [blame] | 198 | + CGI.TheDef->getName() + "'"; |
Nate Begeman | afc5456 | 2005-07-14 22:50:30 +0000 | [diff] [blame] | 199 | ++VarEnd; |
| 200 | } |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame] | 201 | if (VarName.empty()) |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 202 | throw "Stray '$' in '" + CGI.TheDef->getName() + |
Chris Lattner | 3e3def9 | 2005-07-15 22:43:04 +0000 | [diff] [blame] | 203 | "' asm string, maybe you want $$?"; |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame] | 204 | |
| 205 | unsigned OpNo = CGI.getOperandNamed(VarName); |
Chris Lattner | 5765dba | 2005-01-22 17:40:38 +0000 | [diff] [blame] | 206 | CodeGenInstruction::OperandInfo OpInfo = CGI.OperandList[OpNo]; |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame] | 207 | |
| 208 | // If this is a two-address instruction and we are not accessing the |
| 209 | // 0th operand, remove an operand. |
Chris Lattner | 5765dba | 2005-01-22 17:40:38 +0000 | [diff] [blame] | 210 | unsigned MIOp = OpInfo.MIOperandNo; |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame] | 211 | if (CGI.isTwoAddress && MIOp != 0) { |
| 212 | if (MIOp == 1) |
| 213 | throw "Should refer to operand #0 instead of #1 for two-address" |
Chris Lattner | 3e3def9 | 2005-07-15 22:43:04 +0000 | [diff] [blame] | 214 | " instruction '" + CGI.TheDef->getName() + "'!"; |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame] | 215 | --MIOp; |
| 216 | } |
| 217 | |
Chris Lattner | b03b080 | 2006-02-06 22:43:28 +0000 | [diff] [blame] | 218 | if (CurVariant == Variant || CurVariant == ~0U) |
Chris Lattner | 04cadb3 | 2006-02-06 23:40:48 +0000 | [diff] [blame] | 219 | Operands.push_back(AsmWriterOperand(OpInfo.PrinterMethodName, MIOp, |
| 220 | Modifier)); |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame] | 221 | LastEmitted = VarEnd; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | AddLiteralString("\\n"); |
| 226 | } |
| 227 | |
Chris Lattner | f876668 | 2005-01-22 19:22:23 +0000 | [diff] [blame] | 228 | /// MatchesAllButOneOp - If this instruction is exactly identical to the |
| 229 | /// specified instruction except for one differing operand, return the differing |
| 230 | /// operand number. If more than one operand mismatches, return ~1, otherwise |
| 231 | /// if the instructions are identical return ~0. |
| 232 | unsigned AsmWriterInst::MatchesAllButOneOp(const AsmWriterInst &Other)const{ |
| 233 | if (Operands.size() != Other.Operands.size()) return ~1; |
Chris Lattner | 870c016 | 2005-01-22 18:38:13 +0000 | [diff] [blame] | 234 | |
| 235 | unsigned MismatchOperand = ~0U; |
| 236 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) { |
Chris Lattner | 870c016 | 2005-01-22 18:38:13 +0000 | [diff] [blame] | 237 | if (Operands[i] != Other.Operands[i]) |
Chris Lattner | f876668 | 2005-01-22 19:22:23 +0000 | [diff] [blame] | 238 | if (MismatchOperand != ~0U) // Already have one mismatch? |
| 239 | return ~1U; |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 240 | else |
Chris Lattner | 870c016 | 2005-01-22 18:38:13 +0000 | [diff] [blame] | 241 | MismatchOperand = i; |
| 242 | } |
| 243 | return MismatchOperand; |
| 244 | } |
| 245 | |
Chris Lattner | 38c0751 | 2005-01-22 20:31:17 +0000 | [diff] [blame] | 246 | static void PrintCases(std::vector<std::pair<std::string, |
| 247 | AsmWriterOperand> > &OpsToPrint, std::ostream &O) { |
| 248 | O << " case " << OpsToPrint.back().first << ": "; |
| 249 | AsmWriterOperand TheOp = OpsToPrint.back().second; |
| 250 | OpsToPrint.pop_back(); |
| 251 | |
| 252 | // Check to see if any other operands are identical in this list, and if so, |
| 253 | // emit a case label for them. |
| 254 | for (unsigned i = OpsToPrint.size(); i != 0; --i) |
| 255 | if (OpsToPrint[i-1].second == TheOp) { |
| 256 | O << "\n case " << OpsToPrint[i-1].first << ": "; |
| 257 | OpsToPrint.erase(OpsToPrint.begin()+i-1); |
| 258 | } |
| 259 | |
| 260 | // Finally, emit the code. |
| 261 | TheOp.EmitCode(O); |
| 262 | O << "break;\n"; |
| 263 | } |
| 264 | |
Chris Lattner | 870c016 | 2005-01-22 18:38:13 +0000 | [diff] [blame] | 265 | |
| 266 | /// EmitInstructions - Emit the last instruction in the vector and any other |
| 267 | /// instructions that are suitably similar to it. |
| 268 | static void EmitInstructions(std::vector<AsmWriterInst> &Insts, |
| 269 | std::ostream &O) { |
| 270 | AsmWriterInst FirstInst = Insts.back(); |
| 271 | Insts.pop_back(); |
| 272 | |
| 273 | std::vector<AsmWriterInst> SimilarInsts; |
| 274 | unsigned DifferingOperand = ~0; |
| 275 | for (unsigned i = Insts.size(); i != 0; --i) { |
Chris Lattner | f876668 | 2005-01-22 19:22:23 +0000 | [diff] [blame] | 276 | unsigned DiffOp = Insts[i-1].MatchesAllButOneOp(FirstInst); |
| 277 | if (DiffOp != ~1U) { |
Chris Lattner | 870c016 | 2005-01-22 18:38:13 +0000 | [diff] [blame] | 278 | if (DifferingOperand == ~0U) // First match! |
| 279 | DifferingOperand = DiffOp; |
| 280 | |
| 281 | // If this differs in the same operand as the rest of the instructions in |
| 282 | // this class, move it to the SimilarInsts list. |
Chris Lattner | f876668 | 2005-01-22 19:22:23 +0000 | [diff] [blame] | 283 | if (DifferingOperand == DiffOp || DiffOp == ~0U) { |
Chris Lattner | 870c016 | 2005-01-22 18:38:13 +0000 | [diff] [blame] | 284 | SimilarInsts.push_back(Insts[i-1]); |
| 285 | Insts.erase(Insts.begin()+i-1); |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | |
Chris Lattner | a1e8a80 | 2006-05-01 17:01:17 +0000 | [diff] [blame^] | 290 | O << " case " << FirstInst.CGI->Namespace << "::" |
Chris Lattner | 870c016 | 2005-01-22 18:38:13 +0000 | [diff] [blame] | 291 | << FirstInst.CGI->TheDef->getName() << ":\n"; |
| 292 | for (unsigned i = 0, e = SimilarInsts.size(); i != e; ++i) |
Chris Lattner | a1e8a80 | 2006-05-01 17:01:17 +0000 | [diff] [blame^] | 293 | O << " case " << SimilarInsts[i].CGI->Namespace << "::" |
Chris Lattner | 870c016 | 2005-01-22 18:38:13 +0000 | [diff] [blame] | 294 | << SimilarInsts[i].CGI->TheDef->getName() << ":\n"; |
| 295 | for (unsigned i = 0, e = FirstInst.Operands.size(); i != e; ++i) { |
| 296 | if (i != DifferingOperand) { |
| 297 | // If the operand is the same for all instructions, just print it. |
| 298 | O << " "; |
| 299 | FirstInst.Operands[i].EmitCode(O); |
| 300 | } else { |
| 301 | // If this is the operand that varies between all of the instructions, |
| 302 | // emit a switch for just this operand now. |
| 303 | O << " switch (MI->getOpcode()) {\n"; |
Chris Lattner | 38c0751 | 2005-01-22 20:31:17 +0000 | [diff] [blame] | 304 | std::vector<std::pair<std::string, AsmWriterOperand> > OpsToPrint; |
Chris Lattner | a1e8a80 | 2006-05-01 17:01:17 +0000 | [diff] [blame^] | 305 | OpsToPrint.push_back(std::make_pair(FirstInst.CGI->Namespace + "::" + |
Chris Lattner | 38c0751 | 2005-01-22 20:31:17 +0000 | [diff] [blame] | 306 | FirstInst.CGI->TheDef->getName(), |
| 307 | FirstInst.Operands[i])); |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 308 | |
Chris Lattner | 870c016 | 2005-01-22 18:38:13 +0000 | [diff] [blame] | 309 | for (unsigned si = 0, e = SimilarInsts.size(); si != e; ++si) { |
Chris Lattner | 38c0751 | 2005-01-22 20:31:17 +0000 | [diff] [blame] | 310 | AsmWriterInst &AWI = SimilarInsts[si]; |
Chris Lattner | a1e8a80 | 2006-05-01 17:01:17 +0000 | [diff] [blame^] | 311 | OpsToPrint.push_back(std::make_pair(AWI.CGI->Namespace+"::"+ |
Chris Lattner | 38c0751 | 2005-01-22 20:31:17 +0000 | [diff] [blame] | 312 | AWI.CGI->TheDef->getName(), |
| 313 | AWI.Operands[i])); |
Chris Lattner | 870c016 | 2005-01-22 18:38:13 +0000 | [diff] [blame] | 314 | } |
Chris Lattner | 38c0751 | 2005-01-22 20:31:17 +0000 | [diff] [blame] | 315 | std::reverse(OpsToPrint.begin(), OpsToPrint.end()); |
| 316 | while (!OpsToPrint.empty()) |
| 317 | PrintCases(OpsToPrint, O); |
Chris Lattner | 870c016 | 2005-01-22 18:38:13 +0000 | [diff] [blame] | 318 | O << " }"; |
| 319 | } |
| 320 | O << "\n"; |
| 321 | } |
| 322 | |
| 323 | O << " break;\n"; |
| 324 | } |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame] | 325 | |
Chris Lattner | 2e1f51b | 2004-08-01 05:59:33 +0000 | [diff] [blame] | 326 | void AsmWriterEmitter::run(std::ostream &O) { |
| 327 | EmitSourceFileHeader("Assembly Writer Source Fragment", O); |
| 328 | |
| 329 | CodeGenTarget Target; |
Chris Lattner | 175580c | 2004-08-14 22:50:53 +0000 | [diff] [blame] | 330 | Record *AsmWriter = Target.getAsmWriter(); |
Chris Lattner | 953c6fe | 2004-10-03 20:19:02 +0000 | [diff] [blame] | 331 | std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName"); |
| 332 | unsigned Variant = AsmWriter->getValueAsInt("Variant"); |
Chris Lattner | 175580c | 2004-08-14 22:50:53 +0000 | [diff] [blame] | 333 | |
Chris Lattner | 2e1f51b | 2004-08-01 05:59:33 +0000 | [diff] [blame] | 334 | O << |
| 335 | "/// printInstruction - This method is automatically generated by tablegen\n" |
| 336 | "/// from the instruction set description. This method returns true if the\n" |
| 337 | "/// machine instruction was sufficiently described to print it, otherwise\n" |
| 338 | "/// it returns false.\n" |
Chris Lattner | 953c6fe | 2004-10-03 20:19:02 +0000 | [diff] [blame] | 339 | "bool " << Target.getName() << ClassName |
Chris Lattner | 175580c | 2004-08-14 22:50:53 +0000 | [diff] [blame] | 340 | << "::printInstruction(const MachineInstr *MI) {\n"; |
Chris Lattner | 2e1f51b | 2004-08-01 05:59:33 +0000 | [diff] [blame] | 341 | |
Chris Lattner | 5765dba | 2005-01-22 17:40:38 +0000 | [diff] [blame] | 342 | std::vector<AsmWriterInst> Instructions; |
| 343 | |
Chris Lattner | 2e1f51b | 2004-08-01 05:59:33 +0000 | [diff] [blame] | 344 | for (CodeGenTarget::inst_iterator I = Target.inst_begin(), |
| 345 | E = Target.inst_end(); I != E; ++I) |
Chris Lattner | 5765dba | 2005-01-22 17:40:38 +0000 | [diff] [blame] | 346 | if (!I->second.AsmString.empty()) |
| 347 | Instructions.push_back(AsmWriterInst(I->second, Variant)); |
Chris Lattner | 076efa7 | 2004-08-01 07:43:02 +0000 | [diff] [blame] | 348 | |
Chris Lattner | f876668 | 2005-01-22 19:22:23 +0000 | [diff] [blame] | 349 | // If all of the instructions start with a constant string (a very very common |
| 350 | // occurance), emit all of the constant strings as a big table lookup instead |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 351 | // of requiring a switch for them. |
Chris Lattner | f876668 | 2005-01-22 19:22:23 +0000 | [diff] [blame] | 352 | bool AllStartWithString = true; |
| 353 | |
| 354 | for (unsigned i = 0, e = Instructions.size(); i != e; ++i) |
| 355 | if (Instructions[i].Operands.empty() || |
| 356 | Instructions[i].Operands[0].OperandType != |
| 357 | AsmWriterOperand::isLiteralTextOperand) { |
| 358 | AllStartWithString = false; |
| 359 | break; |
| 360 | } |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 361 | |
Chris Lattner | 0cfcc1e | 2006-01-27 02:10:50 +0000 | [diff] [blame] | 362 | std::vector<const CodeGenInstruction*> NumberedInstructions; |
| 363 | Target.getInstructionsByEnumValue(NumberedInstructions); |
| 364 | |
Chris Lattner | f876668 | 2005-01-22 19:22:23 +0000 | [diff] [blame] | 365 | if (AllStartWithString) { |
| 366 | // Compute the CodeGenInstruction -> AsmWriterInst mapping. Note that not |
| 367 | // all machine instructions are necessarily being printed, so there may be |
| 368 | // target instructions not in this map. |
| 369 | std::map<const CodeGenInstruction*, AsmWriterInst*> CGIAWIMap; |
| 370 | for (unsigned i = 0, e = Instructions.size(); i != e; ++i) |
| 371 | CGIAWIMap.insert(std::make_pair(Instructions[i].CGI, &Instructions[i])); |
| 372 | |
| 373 | // Emit a table of constant strings. |
Chris Lattner | f876668 | 2005-01-22 19:22:23 +0000 | [diff] [blame] | 374 | O << " static const char * const OpStrs[] = {\n"; |
| 375 | for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) { |
| 376 | AsmWriterInst *AWI = CGIAWIMap[NumberedInstructions[i]]; |
| 377 | if (AWI == 0) { |
| 378 | // Something not handled by the asmwriter printer. |
| 379 | O << " 0,\t// "; |
| 380 | } else { |
| 381 | O << " \"" << AWI->Operands[0].Str << "\",\t// "; |
| 382 | // Nuke the string from the operand list. It is now handled! |
| 383 | AWI->Operands.erase(AWI->Operands.begin()); |
| 384 | } |
| 385 | O << NumberedInstructions[i]->TheDef->getName() << "\n"; |
| 386 | } |
| 387 | O << " };\n\n" |
| 388 | << " // Emit the opcode for the instruction.\n" |
| 389 | << " if (const char *AsmStr = OpStrs[MI->getOpcode()])\n" |
| 390 | << " O << AsmStr;\n\n"; |
| 391 | } |
| 392 | |
Chris Lattner | 870c016 | 2005-01-22 18:38:13 +0000 | [diff] [blame] | 393 | // Because this is a vector we want to emit from the end. Reverse all of the |
| 394 | // elements in the vector. |
| 395 | std::reverse(Instructions.begin(), Instructions.end()); |
Chris Lattner | f876668 | 2005-01-22 19:22:23 +0000 | [diff] [blame] | 396 | |
Chris Lattner | 0cfcc1e | 2006-01-27 02:10:50 +0000 | [diff] [blame] | 397 | // Find the opcode # of inline asm |
Chris Lattner | f876668 | 2005-01-22 19:22:23 +0000 | [diff] [blame] | 398 | O << " switch (MI->getOpcode()) {\n" |
Chris Lattner | 0cfcc1e | 2006-01-27 02:10:50 +0000 | [diff] [blame] | 399 | " default: return false;\n" |
| 400 | " case " << NumberedInstructions.back()->Namespace |
| 401 | << "::INLINEASM: printInlineAsm(MI); break;\n"; |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 402 | |
Chris Lattner | 870c016 | 2005-01-22 18:38:13 +0000 | [diff] [blame] | 403 | while (!Instructions.empty()) |
| 404 | EmitInstructions(Instructions, O); |
| 405 | |
Chris Lattner | 2e1f51b | 2004-08-01 05:59:33 +0000 | [diff] [blame] | 406 | O << " }\n" |
| 407 | " return true;\n" |
| 408 | "}\n"; |
Chris Lattner | 2e1f51b | 2004-08-01 05:59:33 +0000 | [diff] [blame] | 409 | } |