blob: 206c90b75db5d3e1335b97f5ec1af9e987c608ce [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 Greene76081c42009-07-20 22:02:59 +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 Greene76081c42009-07-20 22:02:59 +000036 enum OpType {
37 isLiteralTextOperand,
38 isMachineInstrOperand,
39 isLiteralStatementOperand
40 } OperandType;
Chris Lattnerb0b55e72005-01-22 17:32:42 +000041
42 /// Str - For isLiteralTextOperand, this IS the literal text. For
43 /// isMachineInstrOperand, this is the PrinterMethodName for the operand.
44 std::string Str;
45
46 /// MiOpNo - For isMachineInstrOperand, this is the operand number of the
47 /// machine instruction.
48 unsigned MIOpNo;
Chris Lattner04cadb32006-02-06 23:40:48 +000049
50 /// MiModifier - For isMachineInstrOperand, this is the modifier string for
51 /// an operand, specified with syntax like ${opname:modifier}.
52 std::string MiModifier;
Chris Lattnerb0b55e72005-01-22 17:32:42 +000053
Cedric Venet7caa2d02008-10-27 19:21:35 +000054 // To make VS STL happy
David Greene76081c42009-07-20 22:02:59 +000055 AsmWriterOperand(OpType op = isLiteralTextOperand):OperandType(op) {}
Cedric Venet3bff2df2008-10-26 15:40:44 +000056
David Greene76081c42009-07-20 22:02:59 +000057 AsmWriterOperand(const std::string &LitStr,
58 OpType op = isLiteralTextOperand)
59 : OperandType(op), Str(LitStr) {}
Chris Lattnerb0b55e72005-01-22 17:32:42 +000060
Chris Lattner04cadb32006-02-06 23:40:48 +000061 AsmWriterOperand(const std::string &Printer, unsigned OpNo,
David Greene76081c42009-07-20 22:02:59 +000062 const std::string &Modifier,
63 OpType op = isMachineInstrOperand)
64 : OperandType(op), Str(Printer), MIOpNo(OpNo),
Chris Lattner04cadb32006-02-06 23:40:48 +000065 MiModifier(Modifier) {}
Chris Lattnerb0b55e72005-01-22 17:32:42 +000066
Chris Lattner870c0162005-01-22 18:38:13 +000067 bool operator!=(const AsmWriterOperand &Other) const {
68 if (OperandType != Other.OperandType || Str != Other.Str) return true;
69 if (OperandType == isMachineInstrOperand)
Chris Lattner04cadb32006-02-06 23:40:48 +000070 return MIOpNo != Other.MIOpNo || MiModifier != Other.MiModifier;
Chris Lattner870c0162005-01-22 18:38:13 +000071 return false;
72 }
Chris Lattner38c07512005-01-22 20:31:17 +000073 bool operator==(const AsmWriterOperand &Other) const {
74 return !operator!=(Other);
75 }
Chris Lattnerbdff5f92006-07-18 17:18:03 +000076
77 /// getCode - Return the code that prints this operand.
78 std::string getCode() const;
Chris Lattnerb0b55e72005-01-22 17:32:42 +000079 };
Chris Lattnerbdff5f92006-07-18 17:18:03 +000080}
Chris Lattnerb0b55e72005-01-22 17:32:42 +000081
Chris Lattnerbdff5f92006-07-18 17:18:03 +000082namespace llvm {
Jeff Cohend41b30d2006-11-05 19:31:28 +000083 class AsmWriterInst {
84 public:
Chris Lattnerb0b55e72005-01-22 17:32:42 +000085 std::vector<AsmWriterOperand> Operands;
Chris Lattner5765dba2005-01-22 17:40:38 +000086 const CodeGenInstruction *CGI;
Misha Brukman3da94ae2005-04-22 00:00:37 +000087
David Greene76081c42009-07-20 22:02:59 +000088 /// MAX_GROUP_NESTING_LEVEL - The maximum number of group nesting
89 /// levels we ever expect to see in an asm operand.
90 static const int MAX_GROUP_NESTING_LEVEL = 10;
91
92 /// GroupLevel - The level of nesting of the current operand
93 /// group, such as [reg + (reg + offset)]. -1 means we are not in
94 /// a group.
95 int GroupLevel;
96
97 /// GroupDelim - Remember the delimeter for a group operand.
98 char GroupDelim[MAX_GROUP_NESTING_LEVEL];
99
100 /// InGroup - Determine whether we are in the middle of an
101 /// operand group.
102 bool InGroup() const { return GroupLevel != -1; }
103
Chris Lattner5765dba2005-01-22 17:40:38 +0000104 AsmWriterInst(const CodeGenInstruction &CGI, unsigned Variant);
Chris Lattner870c0162005-01-22 18:38:13 +0000105
Chris Lattnerf8766682005-01-22 19:22:23 +0000106 /// MatchesAllButOneOp - If this instruction is exactly identical to the
107 /// specified instruction except for one differing operand, return the
108 /// differing operand number. Otherwise return ~0.
109 unsigned MatchesAllButOneOp(const AsmWriterInst &Other) const;
Chris Lattner870c0162005-01-22 18:38:13 +0000110
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000111 private:
112 void AddLiteralString(const std::string &Str) {
113 // If the last operand was already a literal text string, append this to
114 // it, otherwise add a new operand.
David Greene76081c42009-07-20 22:02:59 +0000115
116 std::string::size_type SearchStart = 0;
117 std::string::size_type SpaceStartPos = std::string::npos;
118 do {
119 // Search for whitespace and replace with calls to set the
120 // output column.
121 SpaceStartPos = Str.find_first_of(" \t", SearchStart);
122 // Assume grouped text is one operand.
123 std::string::size_type StartDelimPos = Str.find_first_of("[{(", SearchStart);
124
125 SearchStart = std::string::npos;
126
127 if (StartDelimPos != std::string::npos) {
128 ++GroupLevel;
129 assert(GroupLevel < MAX_GROUP_NESTING_LEVEL
130 && "Exceeded maximum operand group nesting level");
131 GroupDelim[GroupLevel] = Str[StartDelimPos];
132 if (SpaceStartPos != std::string::npos &&
133 SpaceStartPos > StartDelimPos) {
134 // This space doesn't count.
135 SpaceStartPos = std::string::npos;
136 }
137 }
138
139 if (InGroup()) {
140 // Find the end delimiter.
141 char EndDelim = (GroupDelim[GroupLevel] == '{' ? '}' :
142 (GroupDelim[GroupLevel] == '(' ? ')' : ']'));
143 std::string::size_type EndDelimSearchStart =
144 StartDelimPos == std::string::npos ? 0 : StartDelimPos+1;
145 std::string::size_type EndDelimPos = Str.find(EndDelim,
146 EndDelimSearchStart);
147 SearchStart = EndDelimPos;
148 if (EndDelimPos != std::string::npos) {
149 // Iterate.
150 SearchStart = EndDelimPos + 1;
151 --GroupLevel;
152 assert(GroupLevel > -2 && "Too many end delimeters!");
153 }
154 if (InGroup())
155 SpaceStartPos = std::string::npos;
156 }
157 } while (SearchStart != std::string::npos);
158
159
160 if (SpaceStartPos != std::string::npos) {
161 std::string::size_type SpaceEndPos =
162 Str.find_first_not_of(" \t", SpaceStartPos+1);
163 if (SpaceStartPos != 0) {
164 // Emit the first part of the string.
165 AddLiteralString(Str.substr(0, SpaceStartPos));
166 }
167 Operands.push_back(
168 AsmWriterOperand(
169 "O.PadToColumn(TAI->getOperandColumn(OperandColumn++), 1);\n",
170 AsmWriterOperand::isLiteralStatementOperand));
171 if (SpaceEndPos != std::string::npos) {
172 // Emit the last part of the string.
173 AddLiteralString(Str.substr(SpaceEndPos));
174 }
175 // We've emitted the whole string.
176 return;
177 }
178
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000179 if (!Operands.empty() &&
180 Operands.back().OperandType == AsmWriterOperand::isLiteralTextOperand)
181 Operands.back().Str.append(Str);
182 else
183 Operands.push_back(AsmWriterOperand(Str));
184 }
185 };
186}
187
188
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000189std::string AsmWriterOperand::getCode() const {
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000190 if (OperandType == isLiteralTextOperand)
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000191 return "O << \"" + Str + "\"; ";
192
David Greene76081c42009-07-20 22:02:59 +0000193 if (OperandType == isLiteralStatementOperand) {
194 return Str;
195 }
196
197 if (OperandType == isLiteralStatementOperand) {
198 return Str;
199 }
200
201 if (OperandType == isLiteralStatementOperand) {
202 return Str;
203 }
204
Chris Lattner1bf63612006-09-26 23:45:08 +0000205 std::string Result = Str + "(MI";
206 if (MIOpNo != ~0U)
207 Result += ", " + utostr(MIOpNo);
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000208 if (!MiModifier.empty())
209 Result += ", \"" + MiModifier + '"';
210 return Result + "); ";
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000211}
212
213
214/// ParseAsmString - Parse the specified Instruction's AsmString into this
215/// AsmWriterInst.
216///
David Greene76081c42009-07-20 22:02:59 +0000217AsmWriterInst::AsmWriterInst(const CodeGenInstruction &CGI, unsigned Variant)
218 : GroupLevel(-1) {
Chris Lattner5765dba2005-01-22 17:40:38 +0000219 this->CGI = &CGI;
Chris Lattnerb03b0802006-02-06 22:43:28 +0000220 unsigned CurVariant = ~0U; // ~0 if we are outside a {.|.|.} region, other #.
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000221
Chris Lattner1cf9d962006-02-01 19:12:23 +0000222 // NOTE: Any extensions to this code need to be mirrored in the
223 // AsmPrinter::printInlineAsm code that executes as compile time (assuming
224 // that inline asm strings should also get the new feature)!
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000225 const std::string &AsmString = CGI.AsmString;
226 std::string::size_type LastEmitted = 0;
227 while (LastEmitted != AsmString.size()) {
228 std::string::size_type DollarPos =
Nate Begeman817affc2008-03-17 07:26:14 +0000229 AsmString.find_first_of("${|}\\", LastEmitted);
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000230 if (DollarPos == std::string::npos) DollarPos = AsmString.size();
231
232 // Emit a constant string fragment.
233 if (DollarPos != LastEmitted) {
Chris Lattner7f3b28a2009-03-13 21:33:17 +0000234 if (CurVariant == Variant || CurVariant == ~0U) {
235 for (; LastEmitted != DollarPos; ++LastEmitted)
236 switch (AsmString[LastEmitted]) {
237 case '\n': AddLiteralString("\\n"); break;
238 case '\t': AddLiteralString("\\t"); break;
239 case '"': AddLiteralString("\\\""); break;
240 case '\\': AddLiteralString("\\\\"); break;
241 default:
242 AddLiteralString(std::string(1, AsmString[LastEmitted]));
243 break;
244 }
245 } else {
246 LastEmitted = DollarPos;
247 }
Nate Begeman817affc2008-03-17 07:26:14 +0000248 } else if (AsmString[DollarPos] == '\\') {
249 if (DollarPos+1 != AsmString.size() &&
250 (CurVariant == Variant || CurVariant == ~0U)) {
251 if (AsmString[DollarPos+1] == 'n') {
252 AddLiteralString("\\n");
253 } else if (AsmString[DollarPos+1] == 't') {
254 AddLiteralString("\\t");
255 } else if (std::string("${|}\\").find(AsmString[DollarPos+1])
256 != std::string::npos) {
257 AddLiteralString(std::string(1, AsmString[DollarPos+1]));
258 } else {
259 throw "Non-supported escaped character found in instruction '" +
260 CGI.TheDef->getName() + "'!";
261 }
262 LastEmitted = DollarPos+2;
263 continue;
264 }
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000265 } else if (AsmString[DollarPos] == '{') {
Chris Lattnerb03b0802006-02-06 22:43:28 +0000266 if (CurVariant != ~0U)
Jeff Cohen00b168892005-07-27 06:12:32 +0000267 throw "Nested variants found for instruction '" +
Chris Lattner3e3def92005-07-15 22:43:04 +0000268 CGI.TheDef->getName() + "'!";
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000269 LastEmitted = DollarPos+1;
Chris Lattnerb03b0802006-02-06 22:43:28 +0000270 CurVariant = 0; // We are now inside of the variant!
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000271 } else if (AsmString[DollarPos] == '|') {
Chris Lattnerb03b0802006-02-06 22:43:28 +0000272 if (CurVariant == ~0U)
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000273 throw "'|' character found outside of a variant in instruction '"
Chris Lattner3e3def92005-07-15 22:43:04 +0000274 + CGI.TheDef->getName() + "'!";
Chris Lattnerb03b0802006-02-06 22:43:28 +0000275 ++CurVariant;
276 ++LastEmitted;
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000277 } else if (AsmString[DollarPos] == '}') {
Chris Lattnerb03b0802006-02-06 22:43:28 +0000278 if (CurVariant == ~0U)
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000279 throw "'}' character found outside of a variant in instruction '"
Chris Lattner3e3def92005-07-15 22:43:04 +0000280 + CGI.TheDef->getName() + "'!";
Chris Lattnerb03b0802006-02-06 22:43:28 +0000281 ++LastEmitted;
282 CurVariant = ~0U;
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000283 } else if (DollarPos+1 != AsmString.size() &&
284 AsmString[DollarPos+1] == '$') {
Chris Lattnerb03b0802006-02-06 22:43:28 +0000285 if (CurVariant == Variant || CurVariant == ~0U)
286 AddLiteralString("$"); // "$$" -> $
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000287 LastEmitted = DollarPos+2;
288 } else {
289 // Get the name of the variable.
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000290 std::string::size_type VarEnd = DollarPos+1;
David Greene76081c42009-07-20 22:02:59 +0000291
Nate Begemanafc54562005-07-14 22:50:30 +0000292 // handle ${foo}bar as $foo by detecting whether the character following
293 // the dollar sign is a curly brace. If so, advance VarEnd and DollarPos
294 // so the variable name does not contain the leading curly brace.
295 bool hasCurlyBraces = false;
296 if (VarEnd < AsmString.size() && '{' == AsmString[VarEnd]) {
297 hasCurlyBraces = true;
298 ++DollarPos;
299 ++VarEnd;
300 }
301
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000302 while (VarEnd < AsmString.size() && isIdentChar(AsmString[VarEnd]))
303 ++VarEnd;
304 std::string VarName(AsmString.begin()+DollarPos+1,
305 AsmString.begin()+VarEnd);
Nate Begemanafc54562005-07-14 22:50:30 +0000306
Chris Lattner04cadb32006-02-06 23:40:48 +0000307 // Modifier - Support ${foo:modifier} syntax, where "modifier" is passed
Chris Lattner1bf63612006-09-26 23:45:08 +0000308 // into printOperand. Also support ${:feature}, which is passed into
Chris Lattner16f046a2006-09-26 23:47:10 +0000309 // PrintSpecial.
Chris Lattner04cadb32006-02-06 23:40:48 +0000310 std::string Modifier;
311
Nate Begemanafc54562005-07-14 22:50:30 +0000312 // In order to avoid starting the next string at the terminating curly
313 // brace, advance the end position past it if we found an opening curly
314 // brace.
315 if (hasCurlyBraces) {
316 if (VarEnd >= AsmString.size())
317 throw "Reached end of string before terminating curly brace in '"
Chris Lattner3e3def92005-07-15 22:43:04 +0000318 + CGI.TheDef->getName() + "'";
Chris Lattner04cadb32006-02-06 23:40:48 +0000319
320 // Look for a modifier string.
321 if (AsmString[VarEnd] == ':') {
322 ++VarEnd;
323 if (VarEnd >= AsmString.size())
324 throw "Reached end of string before terminating curly brace in '"
325 + CGI.TheDef->getName() + "'";
326
327 unsigned ModifierStart = VarEnd;
328 while (VarEnd < AsmString.size() && isIdentChar(AsmString[VarEnd]))
329 ++VarEnd;
330 Modifier = std::string(AsmString.begin()+ModifierStart,
331 AsmString.begin()+VarEnd);
332 if (Modifier.empty())
333 throw "Bad operand modifier name in '"+ CGI.TheDef->getName() + "'";
334 }
335
Nate Begemanafc54562005-07-14 22:50:30 +0000336 if (AsmString[VarEnd] != '}')
Chris Lattnerb03b0802006-02-06 22:43:28 +0000337 throw "Variable name beginning with '{' did not end with '}' in '"
Chris Lattner3e3def92005-07-15 22:43:04 +0000338 + CGI.TheDef->getName() + "'";
Nate Begemanafc54562005-07-14 22:50:30 +0000339 ++VarEnd;
340 }
Chris Lattner1bf63612006-09-26 23:45:08 +0000341 if (VarName.empty() && Modifier.empty())
Jeff Cohen00b168892005-07-27 06:12:32 +0000342 throw "Stray '$' in '" + CGI.TheDef->getName() +
Chris Lattner3e3def92005-07-15 22:43:04 +0000343 "' asm string, maybe you want $$?";
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000344
Chris Lattner1bf63612006-09-26 23:45:08 +0000345 if (VarName.empty()) {
Chris Lattner16f046a2006-09-26 23:47:10 +0000346 // Just a modifier, pass this into PrintSpecial.
347 Operands.push_back(AsmWriterOperand("PrintSpecial", ~0U, Modifier));
Chris Lattner1bf63612006-09-26 23:45:08 +0000348 } else {
349 // Otherwise, normal operand.
350 unsigned OpNo = CGI.getOperandNamed(VarName);
351 CodeGenInstruction::OperandInfo OpInfo = CGI.OperandList[OpNo];
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000352
Chris Lattnerf64f9a42006-11-15 23:23:02 +0000353 if (CurVariant == Variant || CurVariant == ~0U) {
354 unsigned MIOp = OpInfo.MIOperandNo;
Chris Lattner1bf63612006-09-26 23:45:08 +0000355 Operands.push_back(AsmWriterOperand(OpInfo.PrinterMethodName, MIOp,
356 Modifier));
Chris Lattnerf64f9a42006-11-15 23:23:02 +0000357 }
Chris Lattner1bf63612006-09-26 23:45:08 +0000358 }
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000359 LastEmitted = VarEnd;
360 }
361 }
Evan Chengba8dc032009-07-20 06:10:07 +0000362
David Greene76081c42009-07-20 22:02:59 +0000363 Operands.push_back(
364 AsmWriterOperand("EmitComments(*MI);\n",
365 AsmWriterOperand::isLiteralStatementOperand));
Evan Chengba8dc032009-07-20 06:10:07 +0000366 AddLiteralString("\\n");
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000367}
368
Chris Lattnerf8766682005-01-22 19:22:23 +0000369/// MatchesAllButOneOp - If this instruction is exactly identical to the
370/// specified instruction except for one differing operand, return the differing
371/// operand number. If more than one operand mismatches, return ~1, otherwise
372/// if the instructions are identical return ~0.
373unsigned AsmWriterInst::MatchesAllButOneOp(const AsmWriterInst &Other)const{
374 if (Operands.size() != Other.Operands.size()) return ~1;
Chris Lattner870c0162005-01-22 18:38:13 +0000375
376 unsigned MismatchOperand = ~0U;
377 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +0000378 if (Operands[i] != Other.Operands[i]) {
Chris Lattnerf8766682005-01-22 19:22:23 +0000379 if (MismatchOperand != ~0U) // Already have one mismatch?
380 return ~1U;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000381 else
Chris Lattner870c0162005-01-22 18:38:13 +0000382 MismatchOperand = i;
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +0000383 }
Chris Lattner870c0162005-01-22 18:38:13 +0000384 }
385 return MismatchOperand;
386}
387
Chris Lattner38c07512005-01-22 20:31:17 +0000388static void PrintCases(std::vector<std::pair<std::string,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000389 AsmWriterOperand> > &OpsToPrint, raw_ostream &O) {
Chris Lattner38c07512005-01-22 20:31:17 +0000390 O << " case " << OpsToPrint.back().first << ": ";
391 AsmWriterOperand TheOp = OpsToPrint.back().second;
392 OpsToPrint.pop_back();
393
394 // Check to see if any other operands are identical in this list, and if so,
395 // emit a case label for them.
396 for (unsigned i = OpsToPrint.size(); i != 0; --i)
397 if (OpsToPrint[i-1].second == TheOp) {
398 O << "\n case " << OpsToPrint[i-1].first << ": ";
399 OpsToPrint.erase(OpsToPrint.begin()+i-1);
400 }
401
402 // Finally, emit the code.
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000403 O << TheOp.getCode();
Chris Lattner38c07512005-01-22 20:31:17 +0000404 O << "break;\n";
405}
406
Chris Lattner870c0162005-01-22 18:38:13 +0000407
408/// EmitInstructions - Emit the last instruction in the vector and any other
409/// instructions that are suitably similar to it.
410static void EmitInstructions(std::vector<AsmWriterInst> &Insts,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000411 raw_ostream &O) {
Chris Lattner870c0162005-01-22 18:38:13 +0000412 AsmWriterInst FirstInst = Insts.back();
413 Insts.pop_back();
414
415 std::vector<AsmWriterInst> SimilarInsts;
416 unsigned DifferingOperand = ~0;
417 for (unsigned i = Insts.size(); i != 0; --i) {
Chris Lattnerf8766682005-01-22 19:22:23 +0000418 unsigned DiffOp = Insts[i-1].MatchesAllButOneOp(FirstInst);
419 if (DiffOp != ~1U) {
Chris Lattner870c0162005-01-22 18:38:13 +0000420 if (DifferingOperand == ~0U) // First match!
421 DifferingOperand = DiffOp;
422
423 // If this differs in the same operand as the rest of the instructions in
424 // this class, move it to the SimilarInsts list.
Chris Lattnerf8766682005-01-22 19:22:23 +0000425 if (DifferingOperand == DiffOp || DiffOp == ~0U) {
Chris Lattner870c0162005-01-22 18:38:13 +0000426 SimilarInsts.push_back(Insts[i-1]);
427 Insts.erase(Insts.begin()+i-1);
428 }
429 }
430 }
431
Chris Lattnera1e8a802006-05-01 17:01:17 +0000432 O << " case " << FirstInst.CGI->Namespace << "::"
Chris Lattner870c0162005-01-22 18:38:13 +0000433 << FirstInst.CGI->TheDef->getName() << ":\n";
434 for (unsigned i = 0, e = SimilarInsts.size(); i != e; ++i)
Chris Lattnera1e8a802006-05-01 17:01:17 +0000435 O << " case " << SimilarInsts[i].CGI->Namespace << "::"
Chris Lattner870c0162005-01-22 18:38:13 +0000436 << SimilarInsts[i].CGI->TheDef->getName() << ":\n";
437 for (unsigned i = 0, e = FirstInst.Operands.size(); i != e; ++i) {
438 if (i != DifferingOperand) {
439 // If the operand is the same for all instructions, just print it.
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000440 O << " " << FirstInst.Operands[i].getCode();
Chris Lattner870c0162005-01-22 18:38:13 +0000441 } else {
442 // If this is the operand that varies between all of the instructions,
443 // emit a switch for just this operand now.
444 O << " switch (MI->getOpcode()) {\n";
Chris Lattner38c07512005-01-22 20:31:17 +0000445 std::vector<std::pair<std::string, AsmWriterOperand> > OpsToPrint;
Chris Lattnera1e8a802006-05-01 17:01:17 +0000446 OpsToPrint.push_back(std::make_pair(FirstInst.CGI->Namespace + "::" +
Chris Lattner38c07512005-01-22 20:31:17 +0000447 FirstInst.CGI->TheDef->getName(),
448 FirstInst.Operands[i]));
Misha Brukman3da94ae2005-04-22 00:00:37 +0000449
Chris Lattner870c0162005-01-22 18:38:13 +0000450 for (unsigned si = 0, e = SimilarInsts.size(); si != e; ++si) {
Chris Lattner38c07512005-01-22 20:31:17 +0000451 AsmWriterInst &AWI = SimilarInsts[si];
Chris Lattnera1e8a802006-05-01 17:01:17 +0000452 OpsToPrint.push_back(std::make_pair(AWI.CGI->Namespace+"::"+
Chris Lattner38c07512005-01-22 20:31:17 +0000453 AWI.CGI->TheDef->getName(),
454 AWI.Operands[i]));
Chris Lattner870c0162005-01-22 18:38:13 +0000455 }
Chris Lattner38c07512005-01-22 20:31:17 +0000456 std::reverse(OpsToPrint.begin(), OpsToPrint.end());
457 while (!OpsToPrint.empty())
458 PrintCases(OpsToPrint, O);
Chris Lattner870c0162005-01-22 18:38:13 +0000459 O << " }";
460 }
461 O << "\n";
462 }
Chris Lattner870c0162005-01-22 18:38:13 +0000463 O << " break;\n";
464}
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000465
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000466void AsmWriterEmitter::
467FindUniqueOperandCommands(std::vector<std::string> &UniqueOperandCommands,
Chris Lattner96c1ade2006-07-18 18:28:27 +0000468 std::vector<unsigned> &InstIdxs,
469 std::vector<unsigned> &InstOpsUsed) const {
Chris Lattner195bb4a2006-07-18 19:27:30 +0000470 InstIdxs.assign(NumberedInstructions.size(), ~0U);
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000471
472 // This vector parallels UniqueOperandCommands, keeping track of which
473 // instructions each case are used for. It is a comma separated string of
474 // enums.
475 std::vector<std::string> InstrsForCase;
476 InstrsForCase.resize(UniqueOperandCommands.size());
Chris Lattner96c1ade2006-07-18 18:28:27 +0000477 InstOpsUsed.assign(UniqueOperandCommands.size(), 0);
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000478
479 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
480 const AsmWriterInst *Inst = getAsmWriterInstByID(i);
Dan Gohman44066042008-07-01 00:05:16 +0000481 if (Inst == 0) continue; // PHI, INLINEASM, DBG_LABEL, etc.
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000482
483 std::string Command;
Chris Lattnerb8462862006-07-18 17:56:07 +0000484 if (Inst->Operands.empty())
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000485 continue; // Instruction already done.
Chris Lattner191dd1f2006-07-18 17:50:22 +0000486
Chris Lattnerb8462862006-07-18 17:56:07 +0000487 Command = " " + Inst->Operands[0].getCode() + "\n";
Chris Lattner191dd1f2006-07-18 17:50:22 +0000488
489 // If this is the last operand, emit a return.
David Greene76081c42009-07-20 22:02:59 +0000490 if (Inst->Operands.size() == 1) {
Chris Lattner191dd1f2006-07-18 17:50:22 +0000491 Command += " return true;\n";
David Greene76081c42009-07-20 22:02:59 +0000492 }
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000493
494 // Check to see if we already have 'Command' in UniqueOperandCommands.
495 // If not, add it.
496 bool FoundIt = false;
497 for (unsigned idx = 0, e = UniqueOperandCommands.size(); idx != e; ++idx)
498 if (UniqueOperandCommands[idx] == Command) {
499 InstIdxs[i] = idx;
500 InstrsForCase[idx] += ", ";
501 InstrsForCase[idx] += Inst->CGI->TheDef->getName();
502 FoundIt = true;
503 break;
504 }
505 if (!FoundIt) {
506 InstIdxs[i] = UniqueOperandCommands.size();
507 UniqueOperandCommands.push_back(Command);
508 InstrsForCase.push_back(Inst->CGI->TheDef->getName());
Chris Lattner96c1ade2006-07-18 18:28:27 +0000509
510 // This command matches one operand so far.
511 InstOpsUsed.push_back(1);
512 }
513 }
514
515 // For each entry of UniqueOperandCommands, there is a set of instructions
516 // that uses it. If the next command of all instructions in the set are
517 // identical, fold it into the command.
518 for (unsigned CommandIdx = 0, e = UniqueOperandCommands.size();
519 CommandIdx != e; ++CommandIdx) {
520
521 for (unsigned Op = 1; ; ++Op) {
522 // Scan for the first instruction in the set.
523 std::vector<unsigned>::iterator NIT =
524 std::find(InstIdxs.begin(), InstIdxs.end(), CommandIdx);
525 if (NIT == InstIdxs.end()) break; // No commonality.
526
527 // If this instruction has no more operands, we isn't anything to merge
528 // into this command.
529 const AsmWriterInst *FirstInst =
530 getAsmWriterInstByID(NIT-InstIdxs.begin());
531 if (!FirstInst || FirstInst->Operands.size() == Op)
532 break;
533
534 // Otherwise, scan to see if all of the other instructions in this command
535 // set share the operand.
536 bool AllSame = true;
David Greene76081c42009-07-20 22:02:59 +0000537 // Keep track of the maximum, number of operands or any
538 // instruction we see in the group.
539 size_t MaxSize = FirstInst->Operands.size();
540
Chris Lattner96c1ade2006-07-18 18:28:27 +0000541 for (NIT = std::find(NIT+1, InstIdxs.end(), CommandIdx);
542 NIT != InstIdxs.end();
543 NIT = std::find(NIT+1, InstIdxs.end(), CommandIdx)) {
544 // Okay, found another instruction in this command set. If the operand
545 // matches, we're ok, otherwise bail out.
546 const AsmWriterInst *OtherInst =
547 getAsmWriterInstByID(NIT-InstIdxs.begin());
David Greene76081c42009-07-20 22:02:59 +0000548
549 if (OtherInst &&
550 OtherInst->Operands.size() > FirstInst->Operands.size())
551 MaxSize = std::max(MaxSize, OtherInst->Operands.size());
552
Chris Lattner96c1ade2006-07-18 18:28:27 +0000553 if (!OtherInst || OtherInst->Operands.size() == Op ||
554 OtherInst->Operands[Op] != FirstInst->Operands[Op]) {
555 AllSame = false;
556 break;
557 }
558 }
559 if (!AllSame) break;
560
561 // Okay, everything in this command set has the same next operand. Add it
562 // to UniqueOperandCommands and remember that it was consumed.
563 std::string Command = " " + FirstInst->Operands[Op].getCode() + "\n";
564
565 // If this is the last operand, emit a return after the code.
David Greene76081c42009-07-20 22:02:59 +0000566 if (FirstInst->Operands.size() == Op+1 &&
567 // Don't early-out too soon. Other instructions in this
568 // group may have more operands.
569 FirstInst->Operands.size() == MaxSize) {
Chris Lattner96c1ade2006-07-18 18:28:27 +0000570 Command += " return true;\n";
David Greene76081c42009-07-20 22:02:59 +0000571 }
Chris Lattner96c1ade2006-07-18 18:28:27 +0000572
573 UniqueOperandCommands[CommandIdx] += Command;
574 InstOpsUsed[CommandIdx]++;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000575 }
576 }
577
578 // Prepend some of the instructions each case is used for onto the case val.
579 for (unsigned i = 0, e = InstrsForCase.size(); i != e; ++i) {
580 std::string Instrs = InstrsForCase[i];
581 if (Instrs.size() > 70) {
582 Instrs.erase(Instrs.begin()+70, Instrs.end());
583 Instrs += "...";
584 }
585
586 if (!Instrs.empty())
587 UniqueOperandCommands[i] = " // " + Instrs + "\n" +
588 UniqueOperandCommands[i];
589 }
590}
591
592
593
Daniel Dunbar1a551802009-07-03 00:10:29 +0000594void AsmWriterEmitter::run(raw_ostream &O) {
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000595 EmitSourceFileHeader("Assembly Writer Source Fragment", O);
596
597 CodeGenTarget Target;
Chris Lattner175580c2004-08-14 22:50:53 +0000598 Record *AsmWriter = Target.getAsmWriter();
Chris Lattner953c6fe2004-10-03 20:19:02 +0000599 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
600 unsigned Variant = AsmWriter->getValueAsInt("Variant");
Chris Lattner175580c2004-08-14 22:50:53 +0000601
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000602 O <<
603 "/// printInstruction - This method is automatically generated by tablegen\n"
604 "/// from the instruction set description. This method returns true if the\n"
605 "/// machine instruction was sufficiently described to print it, otherwise\n"
606 "/// it returns false.\n"
Chris Lattner953c6fe2004-10-03 20:19:02 +0000607 "bool " << Target.getName() << ClassName
Chris Lattner175580c2004-08-14 22:50:53 +0000608 << "::printInstruction(const MachineInstr *MI) {\n";
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000609
Chris Lattner5765dba2005-01-22 17:40:38 +0000610 std::vector<AsmWriterInst> Instructions;
611
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000612 for (CodeGenTarget::inst_iterator I = Target.inst_begin(),
613 E = Target.inst_end(); I != E; ++I)
Chris Lattner5765dba2005-01-22 17:40:38 +0000614 if (!I->second.AsmString.empty())
615 Instructions.push_back(AsmWriterInst(I->second, Variant));
Chris Lattner076efa72004-08-01 07:43:02 +0000616
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000617 // Get the instruction numbering.
Chris Lattner0cfcc1e2006-01-27 02:10:50 +0000618 Target.getInstructionsByEnumValue(NumberedInstructions);
619
Chris Lattner6af022f2006-07-14 22:59:11 +0000620 // Compute the CodeGenInstruction -> AsmWriterInst mapping. Note that not
621 // all machine instructions are necessarily being printed, so there may be
622 // target instructions not in this map.
Chris Lattner6af022f2006-07-14 22:59:11 +0000623 for (unsigned i = 0, e = Instructions.size(); i != e; ++i)
624 CGIAWIMap.insert(std::make_pair(Instructions[i].CGI, &Instructions[i]));
Chris Lattnerf8766682005-01-22 19:22:23 +0000625
Chris Lattner6af022f2006-07-14 22:59:11 +0000626 // Build an aggregate string, and build a table of offsets into it.
627 std::map<std::string, unsigned> StringOffset;
628 std::string AggregateString;
Chris Lattner259bda42006-09-27 16:44:09 +0000629 AggregateString.push_back(0); // "\0"
630 AggregateString.push_back(0); // "\0"
Chris Lattner6af022f2006-07-14 22:59:11 +0000631
Chris Lattner259bda42006-09-27 16:44:09 +0000632 /// OpcodeInfo - This encodes the index of the string to use for the first
Chris Lattner55616402006-07-18 17:32:27 +0000633 /// chunk of the output as well as indices used for operand printing.
634 std::vector<unsigned> OpcodeInfo;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000635
Chris Lattner55616402006-07-18 17:32:27 +0000636 unsigned MaxStringIdx = 0;
Chris Lattner6af022f2006-07-14 22:59:11 +0000637 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
638 AsmWriterInst *AWI = CGIAWIMap[NumberedInstructions[i]];
639 unsigned Idx;
Chris Lattnera6dc9fb2006-07-19 01:39:06 +0000640 if (AWI == 0) {
Chris Lattner6af022f2006-07-14 22:59:11 +0000641 // Something not handled by the asmwriter printer.
642 Idx = 0;
Chris Lattnera6dc9fb2006-07-19 01:39:06 +0000643 } else if (AWI->Operands[0].OperandType !=
644 AsmWriterOperand::isLiteralTextOperand ||
645 AWI->Operands[0].Str.empty()) {
646 // Something handled by the asmwriter printer, but with no leading string.
647 Idx = 1;
Chris Lattner6af022f2006-07-14 22:59:11 +0000648 } else {
649 unsigned &Entry = StringOffset[AWI->Operands[0].Str];
650 if (Entry == 0) {
651 // Add the string to the aggregate if this is the first time found.
Chris Lattner55616402006-07-18 17:32:27 +0000652 MaxStringIdx = Entry = AggregateString.size();
Chris Lattner6af022f2006-07-14 22:59:11 +0000653 std::string Str = AWI->Operands[0].Str;
654 UnescapeString(Str);
655 AggregateString += Str;
656 AggregateString += '\0';
Chris Lattnerf8766682005-01-22 19:22:23 +0000657 }
Chris Lattner6af022f2006-07-14 22:59:11 +0000658 Idx = Entry;
Chris Lattner6af022f2006-07-14 22:59:11 +0000659
660 // Nuke the string from the operand list. It is now handled!
661 AWI->Operands.erase(AWI->Operands.begin());
Chris Lattnerf8766682005-01-22 19:22:23 +0000662 }
Chris Lattner55616402006-07-18 17:32:27 +0000663 OpcodeInfo.push_back(Idx);
Chris Lattnerf8766682005-01-22 19:22:23 +0000664 }
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000665
Chris Lattner55616402006-07-18 17:32:27 +0000666 // Figure out how many bits we used for the string index.
Nate Begeman59d28132008-04-09 16:24:11 +0000667 unsigned AsmStrBits = Log2_32_Ceil(MaxStringIdx+1);
Chris Lattner55616402006-07-18 17:32:27 +0000668
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000669 // To reduce code size, we compactify common instructions into a few bits
670 // in the opcode-indexed table.
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000671 unsigned BitsLeft = 32-AsmStrBits;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000672
673 std::vector<std::vector<std::string> > TableDrivenOperandPrinters;
674
Chris Lattnerb8462862006-07-18 17:56:07 +0000675 bool isFirst = true;
676 while (1) {
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000677 std::vector<std::string> UniqueOperandCommands;
678
Chris Lattnera6dc9fb2006-07-19 01:39:06 +0000679 // For the first operand check, add a default value for instructions with
680 // just opcode strings to use.
Chris Lattnerb8462862006-07-18 17:56:07 +0000681 if (isFirst) {
Evan Chengba8dc032009-07-20 06:10:07 +0000682 UniqueOperandCommands.push_back(" return true;\n");
Chris Lattnerb8462862006-07-18 17:56:07 +0000683 isFirst = false;
684 }
David Greene76081c42009-07-20 22:02:59 +0000685
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000686 std::vector<unsigned> InstIdxs;
Chris Lattner96c1ade2006-07-18 18:28:27 +0000687 std::vector<unsigned> NumInstOpsHandled;
688 FindUniqueOperandCommands(UniqueOperandCommands, InstIdxs,
689 NumInstOpsHandled);
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000690
691 // If we ran out of operands to print, we're done.
692 if (UniqueOperandCommands.empty()) break;
693
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000694 // Compute the number of bits we need to represent these cases, this is
695 // ceil(log2(numentries)).
696 unsigned NumBits = Log2_32_Ceil(UniqueOperandCommands.size());
697
698 // If we don't have enough bits for this operand, don't include it.
699 if (NumBits > BitsLeft) {
Bill Wendlingf5da1332006-12-07 22:21:48 +0000700 DOUT << "Not enough bits to densely encode " << NumBits
701 << " more bits\n";
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000702 break;
703 }
704
705 // Otherwise, we can include this in the initial lookup table. Add it in.
706 BitsLeft -= NumBits;
707 for (unsigned i = 0, e = InstIdxs.size(); i != e; ++i)
Chris Lattner195bb4a2006-07-18 19:27:30 +0000708 if (InstIdxs[i] != ~0U)
709 OpcodeInfo[i] |= InstIdxs[i] << (BitsLeft+AsmStrBits);
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000710
Chris Lattnerb8462862006-07-18 17:56:07 +0000711 // Remove the info about this operand.
712 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
713 if (AsmWriterInst *Inst = getAsmWriterInstByID(i))
Chris Lattner96c1ade2006-07-18 18:28:27 +0000714 if (!Inst->Operands.empty()) {
715 unsigned NumOps = NumInstOpsHandled[InstIdxs[i]];
Chris Lattner0a012122006-07-18 19:06:01 +0000716 assert(NumOps <= Inst->Operands.size() &&
717 "Can't remove this many ops!");
Chris Lattner96c1ade2006-07-18 18:28:27 +0000718 Inst->Operands.erase(Inst->Operands.begin(),
719 Inst->Operands.begin()+NumOps);
720 }
Chris Lattnerb8462862006-07-18 17:56:07 +0000721 }
722
723 // Remember the handlers for this set of operands.
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000724 TableDrivenOperandPrinters.push_back(UniqueOperandCommands);
725 }
726
727
728
Chris Lattner55616402006-07-18 17:32:27 +0000729 O<<" static const unsigned OpInfo[] = {\n";
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000730 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000731 O << " " << OpcodeInfo[i] << "U,\t// "
Chris Lattner55616402006-07-18 17:32:27 +0000732 << NumberedInstructions[i]->TheDef->getName() << "\n";
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000733 }
734 // Add a dummy entry so the array init doesn't end with a comma.
Chris Lattner55616402006-07-18 17:32:27 +0000735 O << " 0U\n";
Chris Lattner6af022f2006-07-14 22:59:11 +0000736 O << " };\n\n";
737
738 // Emit the string itself.
739 O << " const char *AsmStrs = \n \"";
740 unsigned CharsPrinted = 0;
741 EscapeString(AggregateString);
742 for (unsigned i = 0, e = AggregateString.size(); i != e; ++i) {
743 if (CharsPrinted > 70) {
744 O << "\"\n \"";
745 CharsPrinted = 0;
746 }
747 O << AggregateString[i];
748 ++CharsPrinted;
749
750 // Print escape sequences all together.
751 if (AggregateString[i] == '\\') {
752 assert(i+1 < AggregateString.size() && "Incomplete escape sequence!");
753 if (isdigit(AggregateString[i+1])) {
754 assert(isdigit(AggregateString[i+2]) && isdigit(AggregateString[i+3]) &&
755 "Expected 3 digit octal escape!");
756 O << AggregateString[++i];
757 O << AggregateString[++i];
758 O << AggregateString[++i];
759 CharsPrinted += 3;
760 } else {
761 O << AggregateString[++i];
762 ++CharsPrinted;
763 }
764 }
765 }
766 O << "\";\n\n";
767
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +0000768 O << " processDebugLoc(MI->getDebugLoc());\n\n";
Bill Wendlingcb819f12009-02-18 23:12:06 +0000769
Chris Lattner5b842c32009-06-19 23:57:53 +0000770 O << "\n#ifndef NO_ASM_WRITER_BOILERPLATE\n";
771
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000772 O << " if (MI->getOpcode() == TargetInstrInfo::INLINEASM) {\n"
Evan Cheng4eecdeb2008-02-02 08:39:46 +0000773 << " O << \"\\t\";\n"
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000774 << " printInlineAsm(MI);\n"
775 << " return true;\n"
Dan Gohman44066042008-07-01 00:05:16 +0000776 << " } else if (MI->isLabel()) {\n"
Jim Laskeya683f9b2007-01-26 17:29:20 +0000777 << " printLabel(MI);\n"
778 << " return true;\n"
Evan Chenga844bde2008-02-02 04:07:54 +0000779 << " } else if (MI->getOpcode() == TargetInstrInfo::DECLARE) {\n"
780 << " printDeclare(MI);\n"
781 << " return true;\n"
Evan Chengda47e6e2008-03-15 00:03:38 +0000782 << " } else if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {\n"
783 << " printImplicitDef(MI);\n"
784 << " return true;\n"
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000785 << " }\n\n";
Chris Lattner5b842c32009-06-19 23:57:53 +0000786
787 O << "\n#endif\n";
788
Evan Cheng4eecdeb2008-02-02 08:39:46 +0000789 O << " O << \"\\t\";\n\n";
790
Chris Lattner6af022f2006-07-14 22:59:11 +0000791 O << " // Emit the opcode for the instruction.\n"
Chris Lattner55616402006-07-18 17:32:27 +0000792 << " unsigned Bits = OpInfo[MI->getOpcode()];\n"
David Greene76081c42009-07-20 22:02:59 +0000793 << " if (Bits == 0) return false;\n\n";
794
795 O << " std::string OpStr(AsmStrs+(Bits & " << (1 << AsmStrBits)-1 << "));\n"
796 << " unsigned OperandColumn = 1;\n"
797 << " O << OpStr;\n\n";
798
799 O << " if (OpStr.find_last_of(\" \\t\") == OpStr.size()-1) {\n"
800 << " O.PadToColumn(TAI->getOperandColumn(1));\n"
801 << " OperandColumn = 2;\n"
802 << " }\n\n";
Chris Lattnerf8766682005-01-22 19:22:23 +0000803
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000804 // Output the table driven operand information.
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000805 BitsLeft = 32-AsmStrBits;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000806 for (unsigned i = 0, e = TableDrivenOperandPrinters.size(); i != e; ++i) {
807 std::vector<std::string> &Commands = TableDrivenOperandPrinters[i];
808
809 // Compute the number of bits we need to represent these cases, this is
810 // ceil(log2(numentries)).
811 unsigned NumBits = Log2_32_Ceil(Commands.size());
812 assert(NumBits <= BitsLeft && "consistency error");
813
814 // Emit code to extract this field from Bits.
815 BitsLeft -= NumBits;
816
817 O << "\n // Fragment " << i << " encoded into " << NumBits
Chris Lattnere7a589d2006-07-18 17:43:54 +0000818 << " bits for " << Commands.size() << " unique commands.\n";
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000819
Chris Lattner96c1ade2006-07-18 18:28:27 +0000820 if (Commands.size() == 2) {
Chris Lattnere7a589d2006-07-18 17:43:54 +0000821 // Emit two possibilitys with if/else.
822 O << " if ((Bits >> " << (BitsLeft+AsmStrBits) << ") & "
823 << ((1 << NumBits)-1) << ") {\n"
824 << Commands[1]
825 << " } else {\n"
826 << Commands[0]
827 << " }\n\n";
828 } else {
829 O << " switch ((Bits >> " << (BitsLeft+AsmStrBits) << ") & "
830 << ((1 << NumBits)-1) << ") {\n"
831 << " default: // unreachable.\n";
832
833 // Print out all the cases.
834 for (unsigned i = 0, e = Commands.size(); i != e; ++i) {
835 O << " case " << i << ":\n";
836 O << Commands[i];
837 O << " break;\n";
838 }
839 O << " }\n\n";
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000840 }
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000841 }
842
Chris Lattnerb8462862006-07-18 17:56:07 +0000843 // Okay, delete instructions with no operand info left.
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000844 for (unsigned i = 0, e = Instructions.size(); i != e; ++i) {
845 // Entire instruction has been emitted?
846 AsmWriterInst &Inst = Instructions[i];
Chris Lattnerb8462862006-07-18 17:56:07 +0000847 if (Inst.Operands.empty()) {
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000848 Instructions.erase(Instructions.begin()+i);
Chris Lattnerb8462862006-07-18 17:56:07 +0000849 --i; --e;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000850 }
851 }
852
853
854 // Because this is a vector, we want to emit from the end. Reverse all of the
Chris Lattner870c0162005-01-22 18:38:13 +0000855 // elements in the vector.
856 std::reverse(Instructions.begin(), Instructions.end());
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000857
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000858 if (!Instructions.empty()) {
859 // Find the opcode # of inline asm.
860 O << " switch (MI->getOpcode()) {\n";
861 while (!Instructions.empty())
862 EmitInstructions(Instructions, O);
Chris Lattner870c0162005-01-22 18:38:13 +0000863
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000864 O << " }\n";
Evan Cheng3837b642009-07-18 01:43:53 +0000865 O << " return true;\n";
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000866 }
David Greene76081c42009-07-20 22:02:59 +0000867
868 O << " return true;\n";
Chris Lattner0a012122006-07-18 19:06:01 +0000869 O << "}\n";
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000870}