blob: 974c334651b969e288e1b25255e90a69b2115760 [file] [log] [blame]
Chris Lattner2e1f51b2004-08-01 05:59:33 +00001//===- AsmWriterEmitter.cpp - Generate an assembly writer -----------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
Chris Lattner2e1f51b2004-08-01 05:59:33 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner30609102007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
Chris Lattner2e1f51b2004-08-01 05:59:33 +00008//===----------------------------------------------------------------------===//
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 Lattner175580c2004-08-14 22:50:53 +000017#include "Record.h"
Chris Lattner6af022f2006-07-14 22:59:11 +000018#include "llvm/ADT/StringExtras.h"
Chris Lattnerbdff5f92006-07-18 17:18:03 +000019#include "llvm/Support/Debug.h"
20#include "llvm/Support/MathExtras.h"
Jeff Cohen615ed992005-01-22 18:50:10 +000021#include <algorithm>
David Greenec8d06052009-07-29 20:10:24 +000022#include <sstream>
Daniel Dunbar1a551802009-07-03 00:10:29 +000023#include <iostream>
Chris Lattner2e1f51b2004-08-01 05:59:33 +000024using namespace llvm;
25
Chris Lattner076efa72004-08-01 07:43:02 +000026static bool isIdentChar(char C) {
27 return (C >= 'a' && C <= 'z') ||
28 (C >= 'A' && C <= 'Z') ||
29 (C >= '0' && C <= '9') ||
30 C == '_';
31}
32
Chris Lattnerad8c5312007-07-18 04:51:57 +000033// This should be an anon namespace, this works around a GCC warning.
34namespace llvm {
Chris Lattnerb0b55e72005-01-22 17:32:42 +000035 struct AsmWriterOperand {
David Greenec8d06052009-07-29 20:10:24 +000036 enum OpType {
David Greenebef87682009-07-31 21:57:10 +000037 // Output this text surrounded by quotes to the asm.
David Greenec8d06052009-07-29 20:10:24 +000038 isLiteralTextOperand,
David Greenebef87682009-07-31 21:57:10 +000039 // This is the name of a routine to call to print the operand.
David Greenec8d06052009-07-29 20:10:24 +000040 isMachineInstrOperand,
David Greenebef87682009-07-31 21:57:10 +000041 // Output this text verbatim to the asm writer. It is code that
42 // will output some text to the asm.
David Greenec8d06052009-07-29 20:10:24 +000043 isLiteralStatementOperand
44 } OperandType;
Chris Lattnerb0b55e72005-01-22 17:32:42 +000045
46 /// Str - For isLiteralTextOperand, this IS the literal text. For
David Greenebef87682009-07-31 21:57:10 +000047 /// isMachineInstrOperand, this is the PrinterMethodName for the operand..
48 /// For isLiteralStatementOperand, this is the code to insert verbatim
49 /// into the asm writer.
Chris Lattnerb0b55e72005-01-22 17:32:42 +000050 std::string Str;
51
52 /// MiOpNo - For isMachineInstrOperand, this is the operand number of the
53 /// machine instruction.
54 unsigned MIOpNo;
Chris Lattner04cadb32006-02-06 23:40:48 +000055
56 /// MiModifier - For isMachineInstrOperand, this is the modifier string for
57 /// an operand, specified with syntax like ${opname:modifier}.
58 std::string MiModifier;
Chris Lattnerb0b55e72005-01-22 17:32:42 +000059
Cedric Venet7caa2d02008-10-27 19:21:35 +000060 // To make VS STL happy
David Greenec8d06052009-07-29 20:10:24 +000061 AsmWriterOperand(OpType op = isLiteralTextOperand):OperandType(op) {}
Cedric Venet3bff2df2008-10-26 15:40:44 +000062
David Greenec8d06052009-07-29 20:10:24 +000063 AsmWriterOperand(const std::string &LitStr,
64 OpType op = isLiteralTextOperand)
65 : OperandType(op), Str(LitStr) {}
Chris Lattnerb0b55e72005-01-22 17:32:42 +000066
Chris Lattner04cadb32006-02-06 23:40:48 +000067 AsmWriterOperand(const std::string &Printer, unsigned OpNo,
David Greenec8d06052009-07-29 20:10:24 +000068 const std::string &Modifier,
69 OpType op = isMachineInstrOperand)
70 : OperandType(op), Str(Printer), MIOpNo(OpNo),
Chris Lattner04cadb32006-02-06 23:40:48 +000071 MiModifier(Modifier) {}
Chris Lattnerb0b55e72005-01-22 17:32:42 +000072
Chris Lattner870c0162005-01-22 18:38:13 +000073 bool operator!=(const AsmWriterOperand &Other) const {
74 if (OperandType != Other.OperandType || Str != Other.Str) return true;
75 if (OperandType == isMachineInstrOperand)
Chris Lattner04cadb32006-02-06 23:40:48 +000076 return MIOpNo != Other.MIOpNo || MiModifier != Other.MiModifier;
Chris Lattner870c0162005-01-22 18:38:13 +000077 return false;
78 }
Chris Lattner38c07512005-01-22 20:31:17 +000079 bool operator==(const AsmWriterOperand &Other) const {
80 return !operator!=(Other);
81 }
Chris Lattnerbdff5f92006-07-18 17:18:03 +000082
83 /// getCode - Return the code that prints this operand.
84 std::string getCode() const;
Chris Lattnerb0b55e72005-01-22 17:32:42 +000085 };
Chris Lattnerbdff5f92006-07-18 17:18:03 +000086}
Chris Lattnerb0b55e72005-01-22 17:32:42 +000087
Chris Lattnerbdff5f92006-07-18 17:18:03 +000088namespace llvm {
Jeff Cohend41b30d2006-11-05 19:31:28 +000089 class AsmWriterInst {
90 public:
Chris Lattnerb0b55e72005-01-22 17:32:42 +000091 std::vector<AsmWriterOperand> Operands;
Chris Lattner5765dba2005-01-22 17:40:38 +000092 const CodeGenInstruction *CGI;
Misha Brukman3da94ae2005-04-22 00:00:37 +000093
Chris Lattner5765dba2005-01-22 17:40:38 +000094 AsmWriterInst(const CodeGenInstruction &CGI, unsigned Variant);
Chris Lattner870c0162005-01-22 18:38:13 +000095
Chris Lattnerf8766682005-01-22 19:22:23 +000096 /// MatchesAllButOneOp - If this instruction is exactly identical to the
97 /// specified instruction except for one differing operand, return the
98 /// differing operand number. Otherwise return ~0.
99 unsigned MatchesAllButOneOp(const AsmWriterInst &Other) const;
Chris Lattner870c0162005-01-22 18:38:13 +0000100
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000101 private:
102 void AddLiteralString(const std::string &Str) {
103 // If the last operand was already a literal text string, append this to
104 // it, otherwise add a new operand.
105 if (!Operands.empty() &&
106 Operands.back().OperandType == AsmWriterOperand::isLiteralTextOperand)
107 Operands.back().Str.append(Str);
108 else
109 Operands.push_back(AsmWriterOperand(Str));
110 }
111 };
112}
113
114
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000115std::string AsmWriterOperand::getCode() const {
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000116 if (OperandType == isLiteralTextOperand)
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000117 return "O << \"" + Str + "\"; ";
118
David Greenec8d06052009-07-29 20:10:24 +0000119 if (OperandType == isLiteralStatementOperand) {
120 return Str;
121 }
122
Chris Lattner1bf63612006-09-26 23:45:08 +0000123 std::string Result = Str + "(MI";
124 if (MIOpNo != ~0U)
125 Result += ", " + utostr(MIOpNo);
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000126 if (!MiModifier.empty())
127 Result += ", \"" + MiModifier + '"';
128 return Result + "); ";
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000129}
130
131
132/// ParseAsmString - Parse the specified Instruction's AsmString into this
133/// AsmWriterInst.
134///
David Greenebef87682009-07-31 21:57:10 +0000135AsmWriterInst::AsmWriterInst(const CodeGenInstruction &CGI, unsigned Variant) {
Chris Lattner5765dba2005-01-22 17:40:38 +0000136 this->CGI = &CGI;
Chris Lattnerb03b0802006-02-06 22:43:28 +0000137 unsigned CurVariant = ~0U; // ~0 if we are outside a {.|.|.} region, other #.
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000138
Chris Lattner1cf9d962006-02-01 19:12:23 +0000139 // NOTE: Any extensions to this code need to be mirrored in the
140 // AsmPrinter::printInlineAsm code that executes as compile time (assuming
141 // that inline asm strings should also get the new feature)!
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000142 const std::string &AsmString = CGI.AsmString;
143 std::string::size_type LastEmitted = 0;
144 while (LastEmitted != AsmString.size()) {
145 std::string::size_type DollarPos =
Nate Begeman817affc2008-03-17 07:26:14 +0000146 AsmString.find_first_of("${|}\\", LastEmitted);
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000147 if (DollarPos == std::string::npos) DollarPos = AsmString.size();
148
149 // Emit a constant string fragment.
David Greenec8d06052009-07-29 20:10:24 +0000150
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000151 if (DollarPos != LastEmitted) {
Chris Lattner7f3b28a2009-03-13 21:33:17 +0000152 if (CurVariant == Variant || CurVariant == ~0U) {
153 for (; LastEmitted != DollarPos; ++LastEmitted)
154 switch (AsmString[LastEmitted]) {
David Greenec8d06052009-07-29 20:10:24 +0000155 case '\n':
David Greenec8d06052009-07-29 20:10:24 +0000156 AddLiteralString("\\n");
157 break;
158 case '\t':
David Greenebef87682009-07-31 21:57:10 +0000159 Operands.push_back(
160 // We recognize a tab as an operand delimeter. Either
161 // output column padding if enabled or emit a space.
162 AsmWriterOperand("PadToColumn(OperandColumn++);\n",
163 AsmWriterOperand::isLiteralStatementOperand));
David Greenec8d06052009-07-29 20:10:24 +0000164 break;
165 case '"':
David Greenec8d06052009-07-29 20:10:24 +0000166 AddLiteralString("\\\"");
167 break;
168 case '\\':
David Greenec8d06052009-07-29 20:10:24 +0000169 AddLiteralString("\\\\");
170 break;
Chris Lattner7f3b28a2009-03-13 21:33:17 +0000171 default:
172 AddLiteralString(std::string(1, AsmString[LastEmitted]));
173 break;
174 }
175 } else {
176 LastEmitted = DollarPos;
177 }
Nate Begeman817affc2008-03-17 07:26:14 +0000178 } else if (AsmString[DollarPos] == '\\') {
179 if (DollarPos+1 != AsmString.size() &&
180 (CurVariant == Variant || CurVariant == ~0U)) {
181 if (AsmString[DollarPos+1] == 'n') {
182 AddLiteralString("\\n");
183 } else if (AsmString[DollarPos+1] == 't') {
David Greenebef87682009-07-31 21:57:10 +0000184 Operands.push_back(
185 // We recognize a tab as an operand delimeter. Either
186 // output column padding if enabled or emit a space.
187 AsmWriterOperand("PadToColumn(OperandColumn++);\n",
188 AsmWriterOperand::isLiteralStatementOperand));
Nate Begeman817affc2008-03-17 07:26:14 +0000189 } else if (std::string("${|}\\").find(AsmString[DollarPos+1])
190 != std::string::npos) {
191 AddLiteralString(std::string(1, AsmString[DollarPos+1]));
192 } else {
193 throw "Non-supported escaped character found in instruction '" +
194 CGI.TheDef->getName() + "'!";
195 }
196 LastEmitted = DollarPos+2;
197 continue;
198 }
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000199 } else if (AsmString[DollarPos] == '{') {
Chris Lattnerb03b0802006-02-06 22:43:28 +0000200 if (CurVariant != ~0U)
Jeff Cohen00b168892005-07-27 06:12:32 +0000201 throw "Nested variants found for instruction '" +
Chris Lattner3e3def92005-07-15 22:43:04 +0000202 CGI.TheDef->getName() + "'!";
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000203 LastEmitted = DollarPos+1;
Chris Lattnerb03b0802006-02-06 22:43:28 +0000204 CurVariant = 0; // We are now inside of the variant!
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000205 } else if (AsmString[DollarPos] == '|') {
Chris Lattnerb03b0802006-02-06 22:43:28 +0000206 if (CurVariant == ~0U)
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000207 throw "'|' character found outside of a variant in instruction '"
Chris Lattner3e3def92005-07-15 22:43:04 +0000208 + CGI.TheDef->getName() + "'!";
Chris Lattnerb03b0802006-02-06 22:43:28 +0000209 ++CurVariant;
210 ++LastEmitted;
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000211 } else if (AsmString[DollarPos] == '}') {
Chris Lattnerb03b0802006-02-06 22:43:28 +0000212 if (CurVariant == ~0U)
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000213 throw "'}' character found outside of a variant in instruction '"
Chris Lattner3e3def92005-07-15 22:43:04 +0000214 + CGI.TheDef->getName() + "'!";
Chris Lattnerb03b0802006-02-06 22:43:28 +0000215 ++LastEmitted;
216 CurVariant = ~0U;
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000217 } else if (DollarPos+1 != AsmString.size() &&
218 AsmString[DollarPos+1] == '$') {
David Greenec8d06052009-07-29 20:10:24 +0000219 if (CurVariant == Variant || CurVariant == ~0U) {
Chris Lattnerb03b0802006-02-06 22:43:28 +0000220 AddLiteralString("$"); // "$$" -> $
David Greenec8d06052009-07-29 20:10:24 +0000221 }
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000222 LastEmitted = DollarPos+2;
223 } else {
224 // Get the name of the variable.
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000225 std::string::size_type VarEnd = DollarPos+1;
David Greenec8d06052009-07-29 20:10:24 +0000226
Nate Begemanafc54562005-07-14 22:50:30 +0000227 // handle ${foo}bar as $foo by detecting whether the character following
228 // the dollar sign is a curly brace. If so, advance VarEnd and DollarPos
229 // so the variable name does not contain the leading curly brace.
230 bool hasCurlyBraces = false;
231 if (VarEnd < AsmString.size() && '{' == AsmString[VarEnd]) {
232 hasCurlyBraces = true;
233 ++DollarPos;
234 ++VarEnd;
235 }
236
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000237 while (VarEnd < AsmString.size() && isIdentChar(AsmString[VarEnd]))
238 ++VarEnd;
239 std::string VarName(AsmString.begin()+DollarPos+1,
240 AsmString.begin()+VarEnd);
Nate Begemanafc54562005-07-14 22:50:30 +0000241
Chris Lattner04cadb32006-02-06 23:40:48 +0000242 // Modifier - Support ${foo:modifier} syntax, where "modifier" is passed
Chris Lattner1bf63612006-09-26 23:45:08 +0000243 // into printOperand. Also support ${:feature}, which is passed into
Chris Lattner16f046a2006-09-26 23:47:10 +0000244 // PrintSpecial.
Chris Lattner04cadb32006-02-06 23:40:48 +0000245 std::string Modifier;
246
Nate Begemanafc54562005-07-14 22:50:30 +0000247 // In order to avoid starting the next string at the terminating curly
248 // brace, advance the end position past it if we found an opening curly
249 // brace.
250 if (hasCurlyBraces) {
251 if (VarEnd >= AsmString.size())
252 throw "Reached end of string before terminating curly brace in '"
Chris Lattner3e3def92005-07-15 22:43:04 +0000253 + CGI.TheDef->getName() + "'";
Chris Lattner04cadb32006-02-06 23:40:48 +0000254
255 // Look for a modifier string.
256 if (AsmString[VarEnd] == ':') {
257 ++VarEnd;
258 if (VarEnd >= AsmString.size())
259 throw "Reached end of string before terminating curly brace in '"
260 + CGI.TheDef->getName() + "'";
261
262 unsigned ModifierStart = VarEnd;
263 while (VarEnd < AsmString.size() && isIdentChar(AsmString[VarEnd]))
264 ++VarEnd;
265 Modifier = std::string(AsmString.begin()+ModifierStart,
266 AsmString.begin()+VarEnd);
267 if (Modifier.empty())
268 throw "Bad operand modifier name in '"+ CGI.TheDef->getName() + "'";
269 }
270
Nate Begemanafc54562005-07-14 22:50:30 +0000271 if (AsmString[VarEnd] != '}')
Chris Lattnerb03b0802006-02-06 22:43:28 +0000272 throw "Variable name beginning with '{' did not end with '}' in '"
Chris Lattner3e3def92005-07-15 22:43:04 +0000273 + CGI.TheDef->getName() + "'";
Nate Begemanafc54562005-07-14 22:50:30 +0000274 ++VarEnd;
275 }
Chris Lattner1bf63612006-09-26 23:45:08 +0000276 if (VarName.empty() && Modifier.empty())
Jeff Cohen00b168892005-07-27 06:12:32 +0000277 throw "Stray '$' in '" + CGI.TheDef->getName() +
Chris Lattner3e3def92005-07-15 22:43:04 +0000278 "' asm string, maybe you want $$?";
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000279
Chris Lattner1bf63612006-09-26 23:45:08 +0000280 if (VarName.empty()) {
Chris Lattner16f046a2006-09-26 23:47:10 +0000281 // Just a modifier, pass this into PrintSpecial.
282 Operands.push_back(AsmWriterOperand("PrintSpecial", ~0U, Modifier));
Chris Lattner1bf63612006-09-26 23:45:08 +0000283 } else {
284 // Otherwise, normal operand.
285 unsigned OpNo = CGI.getOperandNamed(VarName);
286 CodeGenInstruction::OperandInfo OpInfo = CGI.OperandList[OpNo];
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000287
Chris Lattnerf64f9a42006-11-15 23:23:02 +0000288 if (CurVariant == Variant || CurVariant == ~0U) {
289 unsigned MIOp = OpInfo.MIOperandNo;
Chris Lattner1bf63612006-09-26 23:45:08 +0000290 Operands.push_back(AsmWriterOperand(OpInfo.PrinterMethodName, MIOp,
291 Modifier));
Chris Lattnerf64f9a42006-11-15 23:23:02 +0000292 }
Chris Lattner1bf63612006-09-26 23:45:08 +0000293 }
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000294 LastEmitted = VarEnd;
295 }
296 }
Evan Chengba8dc032009-07-20 06:10:07 +0000297
David Greenec8d06052009-07-29 20:10:24 +0000298 Operands.push_back(
299 AsmWriterOperand("EmitComments(*MI);\n",
300 AsmWriterOperand::isLiteralStatementOperand));
Evan Chengba8dc032009-07-20 06:10:07 +0000301 AddLiteralString("\\n");
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000302}
303
Chris Lattnerf8766682005-01-22 19:22:23 +0000304/// MatchesAllButOneOp - If this instruction is exactly identical to the
305/// specified instruction except for one differing operand, return the differing
306/// operand number. If more than one operand mismatches, return ~1, otherwise
307/// if the instructions are identical return ~0.
308unsigned AsmWriterInst::MatchesAllButOneOp(const AsmWriterInst &Other)const{
309 if (Operands.size() != Other.Operands.size()) return ~1;
Chris Lattner870c0162005-01-22 18:38:13 +0000310
311 unsigned MismatchOperand = ~0U;
312 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +0000313 if (Operands[i] != Other.Operands[i]) {
Chris Lattnerf8766682005-01-22 19:22:23 +0000314 if (MismatchOperand != ~0U) // Already have one mismatch?
315 return ~1U;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000316 else
Chris Lattner870c0162005-01-22 18:38:13 +0000317 MismatchOperand = i;
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +0000318 }
Chris Lattner870c0162005-01-22 18:38:13 +0000319 }
320 return MismatchOperand;
321}
322
Chris Lattner38c07512005-01-22 20:31:17 +0000323static void PrintCases(std::vector<std::pair<std::string,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000324 AsmWriterOperand> > &OpsToPrint, raw_ostream &O) {
Chris Lattner38c07512005-01-22 20:31:17 +0000325 O << " case " << OpsToPrint.back().first << ": ";
326 AsmWriterOperand TheOp = OpsToPrint.back().second;
327 OpsToPrint.pop_back();
328
329 // Check to see if any other operands are identical in this list, and if so,
330 // emit a case label for them.
331 for (unsigned i = OpsToPrint.size(); i != 0; --i)
332 if (OpsToPrint[i-1].second == TheOp) {
333 O << "\n case " << OpsToPrint[i-1].first << ": ";
334 OpsToPrint.erase(OpsToPrint.begin()+i-1);
335 }
336
337 // Finally, emit the code.
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000338 O << TheOp.getCode();
Chris Lattner38c07512005-01-22 20:31:17 +0000339 O << "break;\n";
340}
341
Chris Lattner870c0162005-01-22 18:38:13 +0000342
343/// EmitInstructions - Emit the last instruction in the vector and any other
344/// instructions that are suitably similar to it.
345static void EmitInstructions(std::vector<AsmWriterInst> &Insts,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000346 raw_ostream &O) {
Chris Lattner870c0162005-01-22 18:38:13 +0000347 AsmWriterInst FirstInst = Insts.back();
348 Insts.pop_back();
349
350 std::vector<AsmWriterInst> SimilarInsts;
351 unsigned DifferingOperand = ~0;
352 for (unsigned i = Insts.size(); i != 0; --i) {
Chris Lattnerf8766682005-01-22 19:22:23 +0000353 unsigned DiffOp = Insts[i-1].MatchesAllButOneOp(FirstInst);
354 if (DiffOp != ~1U) {
Chris Lattner870c0162005-01-22 18:38:13 +0000355 if (DifferingOperand == ~0U) // First match!
356 DifferingOperand = DiffOp;
357
358 // If this differs in the same operand as the rest of the instructions in
359 // this class, move it to the SimilarInsts list.
Chris Lattnerf8766682005-01-22 19:22:23 +0000360 if (DifferingOperand == DiffOp || DiffOp == ~0U) {
Chris Lattner870c0162005-01-22 18:38:13 +0000361 SimilarInsts.push_back(Insts[i-1]);
362 Insts.erase(Insts.begin()+i-1);
363 }
364 }
365 }
366
Chris Lattnera1e8a802006-05-01 17:01:17 +0000367 O << " case " << FirstInst.CGI->Namespace << "::"
Chris Lattner870c0162005-01-22 18:38:13 +0000368 << FirstInst.CGI->TheDef->getName() << ":\n";
369 for (unsigned i = 0, e = SimilarInsts.size(); i != e; ++i)
Chris Lattnera1e8a802006-05-01 17:01:17 +0000370 O << " case " << SimilarInsts[i].CGI->Namespace << "::"
Chris Lattner870c0162005-01-22 18:38:13 +0000371 << SimilarInsts[i].CGI->TheDef->getName() << ":\n";
372 for (unsigned i = 0, e = FirstInst.Operands.size(); i != e; ++i) {
373 if (i != DifferingOperand) {
374 // If the operand is the same for all instructions, just print it.
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000375 O << " " << FirstInst.Operands[i].getCode();
Chris Lattner870c0162005-01-22 18:38:13 +0000376 } else {
377 // If this is the operand that varies between all of the instructions,
378 // emit a switch for just this operand now.
379 O << " switch (MI->getOpcode()) {\n";
Chris Lattner38c07512005-01-22 20:31:17 +0000380 std::vector<std::pair<std::string, AsmWriterOperand> > OpsToPrint;
Chris Lattnera1e8a802006-05-01 17:01:17 +0000381 OpsToPrint.push_back(std::make_pair(FirstInst.CGI->Namespace + "::" +
Chris Lattner38c07512005-01-22 20:31:17 +0000382 FirstInst.CGI->TheDef->getName(),
383 FirstInst.Operands[i]));
Misha Brukman3da94ae2005-04-22 00:00:37 +0000384
Chris Lattner870c0162005-01-22 18:38:13 +0000385 for (unsigned si = 0, e = SimilarInsts.size(); si != e; ++si) {
Chris Lattner38c07512005-01-22 20:31:17 +0000386 AsmWriterInst &AWI = SimilarInsts[si];
Chris Lattnera1e8a802006-05-01 17:01:17 +0000387 OpsToPrint.push_back(std::make_pair(AWI.CGI->Namespace+"::"+
Chris Lattner38c07512005-01-22 20:31:17 +0000388 AWI.CGI->TheDef->getName(),
389 AWI.Operands[i]));
Chris Lattner870c0162005-01-22 18:38:13 +0000390 }
Chris Lattner38c07512005-01-22 20:31:17 +0000391 std::reverse(OpsToPrint.begin(), OpsToPrint.end());
392 while (!OpsToPrint.empty())
393 PrintCases(OpsToPrint, O);
Chris Lattner870c0162005-01-22 18:38:13 +0000394 O << " }";
395 }
396 O << "\n";
397 }
Chris Lattner870c0162005-01-22 18:38:13 +0000398 O << " break;\n";
399}
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000400
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000401void AsmWriterEmitter::
402FindUniqueOperandCommands(std::vector<std::string> &UniqueOperandCommands,
Chris Lattner96c1ade2006-07-18 18:28:27 +0000403 std::vector<unsigned> &InstIdxs,
404 std::vector<unsigned> &InstOpsUsed) const {
Chris Lattner195bb4a2006-07-18 19:27:30 +0000405 InstIdxs.assign(NumberedInstructions.size(), ~0U);
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000406
407 // This vector parallels UniqueOperandCommands, keeping track of which
408 // instructions each case are used for. It is a comma separated string of
409 // enums.
410 std::vector<std::string> InstrsForCase;
411 InstrsForCase.resize(UniqueOperandCommands.size());
Chris Lattner96c1ade2006-07-18 18:28:27 +0000412 InstOpsUsed.assign(UniqueOperandCommands.size(), 0);
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000413
414 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
415 const AsmWriterInst *Inst = getAsmWriterInstByID(i);
Dan Gohman44066042008-07-01 00:05:16 +0000416 if (Inst == 0) continue; // PHI, INLINEASM, DBG_LABEL, etc.
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000417
418 std::string Command;
Chris Lattnerb8462862006-07-18 17:56:07 +0000419 if (Inst->Operands.empty())
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000420 continue; // Instruction already done.
Chris Lattner191dd1f2006-07-18 17:50:22 +0000421
Chris Lattnerb8462862006-07-18 17:56:07 +0000422 Command = " " + Inst->Operands[0].getCode() + "\n";
Chris Lattner191dd1f2006-07-18 17:50:22 +0000423
424 // If this is the last operand, emit a return.
David Greenec8d06052009-07-29 20:10:24 +0000425 if (Inst->Operands.size() == 1) {
Chris Lattner191dd1f2006-07-18 17:50:22 +0000426 Command += " return true;\n";
David Greenec8d06052009-07-29 20:10:24 +0000427 }
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000428
429 // Check to see if we already have 'Command' in UniqueOperandCommands.
430 // If not, add it.
431 bool FoundIt = false;
432 for (unsigned idx = 0, e = UniqueOperandCommands.size(); idx != e; ++idx)
433 if (UniqueOperandCommands[idx] == Command) {
434 InstIdxs[i] = idx;
435 InstrsForCase[idx] += ", ";
436 InstrsForCase[idx] += Inst->CGI->TheDef->getName();
437 FoundIt = true;
438 break;
439 }
440 if (!FoundIt) {
441 InstIdxs[i] = UniqueOperandCommands.size();
442 UniqueOperandCommands.push_back(Command);
443 InstrsForCase.push_back(Inst->CGI->TheDef->getName());
Chris Lattner96c1ade2006-07-18 18:28:27 +0000444
445 // This command matches one operand so far.
446 InstOpsUsed.push_back(1);
447 }
448 }
449
450 // For each entry of UniqueOperandCommands, there is a set of instructions
451 // that uses it. If the next command of all instructions in the set are
452 // identical, fold it into the command.
453 for (unsigned CommandIdx = 0, e = UniqueOperandCommands.size();
454 CommandIdx != e; ++CommandIdx) {
455
456 for (unsigned Op = 1; ; ++Op) {
457 // Scan for the first instruction in the set.
458 std::vector<unsigned>::iterator NIT =
459 std::find(InstIdxs.begin(), InstIdxs.end(), CommandIdx);
460 if (NIT == InstIdxs.end()) break; // No commonality.
461
462 // If this instruction has no more operands, we isn't anything to merge
463 // into this command.
464 const AsmWriterInst *FirstInst =
465 getAsmWriterInstByID(NIT-InstIdxs.begin());
466 if (!FirstInst || FirstInst->Operands.size() == Op)
467 break;
468
469 // Otherwise, scan to see if all of the other instructions in this command
470 // set share the operand.
471 bool AllSame = true;
David Greenec8d06052009-07-29 20:10:24 +0000472 // Keep track of the maximum, number of operands or any
473 // instruction we see in the group.
474 size_t MaxSize = FirstInst->Operands.size();
475
Chris Lattner96c1ade2006-07-18 18:28:27 +0000476 for (NIT = std::find(NIT+1, InstIdxs.end(), CommandIdx);
477 NIT != InstIdxs.end();
478 NIT = std::find(NIT+1, InstIdxs.end(), CommandIdx)) {
479 // Okay, found another instruction in this command set. If the operand
480 // matches, we're ok, otherwise bail out.
481 const AsmWriterInst *OtherInst =
482 getAsmWriterInstByID(NIT-InstIdxs.begin());
David Greenec8d06052009-07-29 20:10:24 +0000483
484 if (OtherInst &&
485 OtherInst->Operands.size() > FirstInst->Operands.size())
486 MaxSize = std::max(MaxSize, OtherInst->Operands.size());
487
Chris Lattner96c1ade2006-07-18 18:28:27 +0000488 if (!OtherInst || OtherInst->Operands.size() == Op ||
489 OtherInst->Operands[Op] != FirstInst->Operands[Op]) {
490 AllSame = false;
491 break;
492 }
493 }
494 if (!AllSame) break;
495
496 // Okay, everything in this command set has the same next operand. Add it
497 // to UniqueOperandCommands and remember that it was consumed.
498 std::string Command = " " + FirstInst->Operands[Op].getCode() + "\n";
499
500 // If this is the last operand, emit a return after the code.
David Greenec8d06052009-07-29 20:10:24 +0000501 if (FirstInst->Operands.size() == Op+1 &&
502 // Don't early-out too soon. Other instructions in this
503 // group may have more operands.
504 FirstInst->Operands.size() == MaxSize) {
Chris Lattner96c1ade2006-07-18 18:28:27 +0000505 Command += " return true;\n";
David Greenec8d06052009-07-29 20:10:24 +0000506 }
Chris Lattner96c1ade2006-07-18 18:28:27 +0000507
508 UniqueOperandCommands[CommandIdx] += Command;
509 InstOpsUsed[CommandIdx]++;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000510 }
511 }
512
513 // Prepend some of the instructions each case is used for onto the case val.
514 for (unsigned i = 0, e = InstrsForCase.size(); i != e; ++i) {
515 std::string Instrs = InstrsForCase[i];
516 if (Instrs.size() > 70) {
517 Instrs.erase(Instrs.begin()+70, Instrs.end());
518 Instrs += "...";
519 }
520
521 if (!Instrs.empty())
522 UniqueOperandCommands[i] = " // " + Instrs + "\n" +
523 UniqueOperandCommands[i];
524 }
525}
526
527
528
Daniel Dunbar1a551802009-07-03 00:10:29 +0000529void AsmWriterEmitter::run(raw_ostream &O) {
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000530 EmitSourceFileHeader("Assembly Writer Source Fragment", O);
531
532 CodeGenTarget Target;
Chris Lattner175580c2004-08-14 22:50:53 +0000533 Record *AsmWriter = Target.getAsmWriter();
Chris Lattner953c6fe2004-10-03 20:19:02 +0000534 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
535 unsigned Variant = AsmWriter->getValueAsInt("Variant");
Chris Lattner175580c2004-08-14 22:50:53 +0000536
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000537 O <<
538 "/// printInstruction - This method is automatically generated by tablegen\n"
539 "/// from the instruction set description. This method returns true if the\n"
540 "/// machine instruction was sufficiently described to print it, otherwise\n"
541 "/// it returns false.\n"
Chris Lattner953c6fe2004-10-03 20:19:02 +0000542 "bool " << Target.getName() << ClassName
Chris Lattner175580c2004-08-14 22:50:53 +0000543 << "::printInstruction(const MachineInstr *MI) {\n";
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000544
Chris Lattner5765dba2005-01-22 17:40:38 +0000545 std::vector<AsmWriterInst> Instructions;
546
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000547 for (CodeGenTarget::inst_iterator I = Target.inst_begin(),
548 E = Target.inst_end(); I != E; ++I)
Chris Lattner5765dba2005-01-22 17:40:38 +0000549 if (!I->second.AsmString.empty())
550 Instructions.push_back(AsmWriterInst(I->second, Variant));
Chris Lattner076efa72004-08-01 07:43:02 +0000551
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000552 // Get the instruction numbering.
Chris Lattner0cfcc1e2006-01-27 02:10:50 +0000553 Target.getInstructionsByEnumValue(NumberedInstructions);
554
Chris Lattner6af022f2006-07-14 22:59:11 +0000555 // Compute the CodeGenInstruction -> AsmWriterInst mapping. Note that not
556 // all machine instructions are necessarily being printed, so there may be
557 // target instructions not in this map.
Chris Lattner6af022f2006-07-14 22:59:11 +0000558 for (unsigned i = 0, e = Instructions.size(); i != e; ++i)
559 CGIAWIMap.insert(std::make_pair(Instructions[i].CGI, &Instructions[i]));
Chris Lattnerf8766682005-01-22 19:22:23 +0000560
Chris Lattner6af022f2006-07-14 22:59:11 +0000561 // Build an aggregate string, and build a table of offsets into it.
562 std::map<std::string, unsigned> StringOffset;
563 std::string AggregateString;
Chris Lattner259bda42006-09-27 16:44:09 +0000564 AggregateString.push_back(0); // "\0"
565 AggregateString.push_back(0); // "\0"
Chris Lattner6af022f2006-07-14 22:59:11 +0000566
Chris Lattner259bda42006-09-27 16:44:09 +0000567 /// OpcodeInfo - This encodes the index of the string to use for the first
Chris Lattner55616402006-07-18 17:32:27 +0000568 /// chunk of the output as well as indices used for operand printing.
569 std::vector<unsigned> OpcodeInfo;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000570
Chris Lattner55616402006-07-18 17:32:27 +0000571 unsigned MaxStringIdx = 0;
Chris Lattner6af022f2006-07-14 22:59:11 +0000572 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
573 AsmWriterInst *AWI = CGIAWIMap[NumberedInstructions[i]];
574 unsigned Idx;
Chris Lattnera6dc9fb2006-07-19 01:39:06 +0000575 if (AWI == 0) {
Chris Lattner6af022f2006-07-14 22:59:11 +0000576 // Something not handled by the asmwriter printer.
577 Idx = 0;
Chris Lattnera6dc9fb2006-07-19 01:39:06 +0000578 } else if (AWI->Operands[0].OperandType !=
579 AsmWriterOperand::isLiteralTextOperand ||
580 AWI->Operands[0].Str.empty()) {
581 // Something handled by the asmwriter printer, but with no leading string.
582 Idx = 1;
Chris Lattner6af022f2006-07-14 22:59:11 +0000583 } else {
584 unsigned &Entry = StringOffset[AWI->Operands[0].Str];
585 if (Entry == 0) {
586 // Add the string to the aggregate if this is the first time found.
Chris Lattner55616402006-07-18 17:32:27 +0000587 MaxStringIdx = Entry = AggregateString.size();
Chris Lattner6af022f2006-07-14 22:59:11 +0000588 std::string Str = AWI->Operands[0].Str;
589 UnescapeString(Str);
590 AggregateString += Str;
591 AggregateString += '\0';
Chris Lattnerf8766682005-01-22 19:22:23 +0000592 }
Chris Lattner6af022f2006-07-14 22:59:11 +0000593 Idx = Entry;
Chris Lattner6af022f2006-07-14 22:59:11 +0000594
595 // Nuke the string from the operand list. It is now handled!
596 AWI->Operands.erase(AWI->Operands.begin());
Chris Lattnerf8766682005-01-22 19:22:23 +0000597 }
Chris Lattner55616402006-07-18 17:32:27 +0000598 OpcodeInfo.push_back(Idx);
Chris Lattnerf8766682005-01-22 19:22:23 +0000599 }
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000600
Chris Lattner55616402006-07-18 17:32:27 +0000601 // Figure out how many bits we used for the string index.
Nate Begeman59d28132008-04-09 16:24:11 +0000602 unsigned AsmStrBits = Log2_32_Ceil(MaxStringIdx+1);
Chris Lattner55616402006-07-18 17:32:27 +0000603
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000604 // To reduce code size, we compactify common instructions into a few bits
605 // in the opcode-indexed table.
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000606 unsigned BitsLeft = 32-AsmStrBits;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000607
608 std::vector<std::vector<std::string> > TableDrivenOperandPrinters;
609
Chris Lattnerb8462862006-07-18 17:56:07 +0000610 bool isFirst = true;
611 while (1) {
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000612 std::vector<std::string> UniqueOperandCommands;
613
Chris Lattnera6dc9fb2006-07-19 01:39:06 +0000614 // For the first operand check, add a default value for instructions with
615 // just opcode strings to use.
Chris Lattnerb8462862006-07-18 17:56:07 +0000616 if (isFirst) {
Evan Chengba8dc032009-07-20 06:10:07 +0000617 UniqueOperandCommands.push_back(" return true;\n");
Chris Lattnerb8462862006-07-18 17:56:07 +0000618 isFirst = false;
619 }
David Greenec8d06052009-07-29 20:10:24 +0000620
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000621 std::vector<unsigned> InstIdxs;
Chris Lattner96c1ade2006-07-18 18:28:27 +0000622 std::vector<unsigned> NumInstOpsHandled;
623 FindUniqueOperandCommands(UniqueOperandCommands, InstIdxs,
624 NumInstOpsHandled);
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000625
626 // If we ran out of operands to print, we're done.
627 if (UniqueOperandCommands.empty()) break;
628
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000629 // Compute the number of bits we need to represent these cases, this is
630 // ceil(log2(numentries)).
631 unsigned NumBits = Log2_32_Ceil(UniqueOperandCommands.size());
632
633 // If we don't have enough bits for this operand, don't include it.
634 if (NumBits > BitsLeft) {
Bill Wendlingf5da1332006-12-07 22:21:48 +0000635 DOUT << "Not enough bits to densely encode " << NumBits
636 << " more bits\n";
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000637 break;
638 }
639
640 // Otherwise, we can include this in the initial lookup table. Add it in.
641 BitsLeft -= NumBits;
642 for (unsigned i = 0, e = InstIdxs.size(); i != e; ++i)
Chris Lattner195bb4a2006-07-18 19:27:30 +0000643 if (InstIdxs[i] != ~0U)
644 OpcodeInfo[i] |= InstIdxs[i] << (BitsLeft+AsmStrBits);
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000645
Chris Lattnerb8462862006-07-18 17:56:07 +0000646 // Remove the info about this operand.
647 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
648 if (AsmWriterInst *Inst = getAsmWriterInstByID(i))
Chris Lattner96c1ade2006-07-18 18:28:27 +0000649 if (!Inst->Operands.empty()) {
650 unsigned NumOps = NumInstOpsHandled[InstIdxs[i]];
Chris Lattner0a012122006-07-18 19:06:01 +0000651 assert(NumOps <= Inst->Operands.size() &&
652 "Can't remove this many ops!");
Chris Lattner96c1ade2006-07-18 18:28:27 +0000653 Inst->Operands.erase(Inst->Operands.begin(),
654 Inst->Operands.begin()+NumOps);
655 }
Chris Lattnerb8462862006-07-18 17:56:07 +0000656 }
657
658 // Remember the handlers for this set of operands.
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000659 TableDrivenOperandPrinters.push_back(UniqueOperandCommands);
660 }
661
662
663
Chris Lattner55616402006-07-18 17:32:27 +0000664 O<<" static const unsigned OpInfo[] = {\n";
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000665 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000666 O << " " << OpcodeInfo[i] << "U,\t// "
Chris Lattner55616402006-07-18 17:32:27 +0000667 << NumberedInstructions[i]->TheDef->getName() << "\n";
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000668 }
669 // Add a dummy entry so the array init doesn't end with a comma.
Chris Lattner55616402006-07-18 17:32:27 +0000670 O << " 0U\n";
Chris Lattner6af022f2006-07-14 22:59:11 +0000671 O << " };\n\n";
672
673 // Emit the string itself.
674 O << " const char *AsmStrs = \n \"";
675 unsigned CharsPrinted = 0;
676 EscapeString(AggregateString);
677 for (unsigned i = 0, e = AggregateString.size(); i != e; ++i) {
678 if (CharsPrinted > 70) {
679 O << "\"\n \"";
680 CharsPrinted = 0;
681 }
682 O << AggregateString[i];
683 ++CharsPrinted;
684
685 // Print escape sequences all together.
686 if (AggregateString[i] == '\\') {
687 assert(i+1 < AggregateString.size() && "Incomplete escape sequence!");
688 if (isdigit(AggregateString[i+1])) {
689 assert(isdigit(AggregateString[i+2]) && isdigit(AggregateString[i+3]) &&
690 "Expected 3 digit octal escape!");
691 O << AggregateString[++i];
692 O << AggregateString[++i];
693 O << AggregateString[++i];
694 CharsPrinted += 3;
695 } else {
696 O << AggregateString[++i];
697 ++CharsPrinted;
698 }
699 }
700 }
701 O << "\";\n\n";
702
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +0000703 O << " processDebugLoc(MI->getDebugLoc());\n\n";
Bill Wendlingcb819f12009-02-18 23:12:06 +0000704
Chris Lattner5b842c32009-06-19 23:57:53 +0000705 O << "\n#ifndef NO_ASM_WRITER_BOILERPLATE\n";
706
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000707 O << " if (MI->getOpcode() == TargetInstrInfo::INLINEASM) {\n"
Evan Cheng4eecdeb2008-02-02 08:39:46 +0000708 << " O << \"\\t\";\n"
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000709 << " printInlineAsm(MI);\n"
710 << " return true;\n"
Dan Gohman44066042008-07-01 00:05:16 +0000711 << " } else if (MI->isLabel()) {\n"
Jim Laskeya683f9b2007-01-26 17:29:20 +0000712 << " printLabel(MI);\n"
713 << " return true;\n"
Evan Chenga844bde2008-02-02 04:07:54 +0000714 << " } else if (MI->getOpcode() == TargetInstrInfo::DECLARE) {\n"
715 << " printDeclare(MI);\n"
716 << " return true;\n"
Evan Chengda47e6e2008-03-15 00:03:38 +0000717 << " } else if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {\n"
718 << " printImplicitDef(MI);\n"
719 << " return true;\n"
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000720 << " }\n\n";
Chris Lattner5b842c32009-06-19 23:57:53 +0000721
722 O << "\n#endif\n";
723
Evan Cheng4eecdeb2008-02-02 08:39:46 +0000724 O << " O << \"\\t\";\n\n";
725
Chris Lattner6af022f2006-07-14 22:59:11 +0000726 O << " // Emit the opcode for the instruction.\n"
Chris Lattner55616402006-07-18 17:32:27 +0000727 << " unsigned Bits = OpInfo[MI->getOpcode()];\n"
David Greenea5bb59f2009-08-05 21:00:52 +0000728 << " if (Bits == 0) return false;\n"
729 << " O << AsmStrs+(Bits & " << (1 << AsmStrBits)-1 << ");\n\n";
730
Daniel Dunbarb3415772009-08-05 21:42:40 +0000731 // This variable may be unused, suppress build warnings.
732 O << " unsigned OperandColumn = 1;\n";
733 O << " (void) OperandColumn;\n\n";
Chris Lattnerf8766682005-01-22 19:22:23 +0000734
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000735 // Output the table driven operand information.
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000736 BitsLeft = 32-AsmStrBits;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000737 for (unsigned i = 0, e = TableDrivenOperandPrinters.size(); i != e; ++i) {
738 std::vector<std::string> &Commands = TableDrivenOperandPrinters[i];
739
740 // Compute the number of bits we need to represent these cases, this is
741 // ceil(log2(numentries)).
742 unsigned NumBits = Log2_32_Ceil(Commands.size());
743 assert(NumBits <= BitsLeft && "consistency error");
744
745 // Emit code to extract this field from Bits.
746 BitsLeft -= NumBits;
747
748 O << "\n // Fragment " << i << " encoded into " << NumBits
Chris Lattnere7a589d2006-07-18 17:43:54 +0000749 << " bits for " << Commands.size() << " unique commands.\n";
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000750
Chris Lattner96c1ade2006-07-18 18:28:27 +0000751 if (Commands.size() == 2) {
Chris Lattnere7a589d2006-07-18 17:43:54 +0000752 // Emit two possibilitys with if/else.
753 O << " if ((Bits >> " << (BitsLeft+AsmStrBits) << ") & "
754 << ((1 << NumBits)-1) << ") {\n"
755 << Commands[1]
756 << " } else {\n"
757 << Commands[0]
758 << " }\n\n";
759 } else {
760 O << " switch ((Bits >> " << (BitsLeft+AsmStrBits) << ") & "
761 << ((1 << NumBits)-1) << ") {\n"
762 << " default: // unreachable.\n";
763
764 // Print out all the cases.
765 for (unsigned i = 0, e = Commands.size(); i != e; ++i) {
766 O << " case " << i << ":\n";
767 O << Commands[i];
768 O << " break;\n";
769 }
770 O << " }\n\n";
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000771 }
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000772 }
773
Chris Lattnerb8462862006-07-18 17:56:07 +0000774 // Okay, delete instructions with no operand info left.
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000775 for (unsigned i = 0, e = Instructions.size(); i != e; ++i) {
776 // Entire instruction has been emitted?
777 AsmWriterInst &Inst = Instructions[i];
Chris Lattnerb8462862006-07-18 17:56:07 +0000778 if (Inst.Operands.empty()) {
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000779 Instructions.erase(Instructions.begin()+i);
Chris Lattnerb8462862006-07-18 17:56:07 +0000780 --i; --e;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000781 }
782 }
783
784
785 // Because this is a vector, we want to emit from the end. Reverse all of the
Chris Lattner870c0162005-01-22 18:38:13 +0000786 // elements in the vector.
787 std::reverse(Instructions.begin(), Instructions.end());
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000788
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000789 if (!Instructions.empty()) {
790 // Find the opcode # of inline asm.
791 O << " switch (MI->getOpcode()) {\n";
792 while (!Instructions.empty())
793 EmitInstructions(Instructions, O);
Chris Lattner870c0162005-01-22 18:38:13 +0000794
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000795 O << " }\n";
Evan Cheng3837b642009-07-18 01:43:53 +0000796 O << " return true;\n";
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000797 }
David Greenec8d06052009-07-29 20:10:24 +0000798
799 O << " return true;\n";
Chris Lattner0a012122006-07-18 19:06:01 +0000800 O << "}\n";
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000801}