blob: aee4653bcde4fbed48e8e1db2889c78b5881aabd [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 Lattner3200fc92009-09-14 01:16:36 +000019#include "llvm/ADT/StringMap.h"
Chris Lattnerbdff5f92006-07-18 17:18:03 +000020#include "llvm/Support/Debug.h"
21#include "llvm/Support/MathExtras.h"
Jeff Cohen615ed992005-01-22 18:50:10 +000022#include <algorithm>
Chris Lattner2e1f51b2004-08-01 05:59:33 +000023using namespace llvm;
24
Chris Lattner3200fc92009-09-14 01:16:36 +000025/// StringToOffsetTable - This class uniques a bunch of nul-terminated strings
26/// and keeps track of their offset in a massive contiguous string allocation.
27/// It can then output this string blob and use indexes into the string to
28/// reference each piece.
29class StringToOffsetTable {
30 StringMap<unsigned> StringOffset;
31 std::string AggregateString;
32public:
33
34 unsigned GetOrAddStringOffset(StringRef Str) {
35 unsigned &Entry = StringOffset[Str];
36 if (Entry == 0) {
37 // Add the string to the aggregate if this is the first time found.
38 Entry = AggregateString.size();
39 AggregateString.append(Str.begin(), Str.end());
40 AggregateString += '\0';
41 }
42
43 return Entry;
44 }
45
46 void EmitString(raw_ostream &O) {
47 O << " \"";
48 unsigned CharsPrinted = 0;
49 EscapeString(AggregateString);
50 for (unsigned i = 0, e = AggregateString.size(); i != e; ++i) {
51 if (CharsPrinted > 70) {
52 O << "\"\n \"";
53 CharsPrinted = 0;
54 }
55 O << AggregateString[i];
56 ++CharsPrinted;
57
58 // Print escape sequences all together.
59 if (AggregateString[i] != '\\')
60 continue;
61
62 assert(i+1 < AggregateString.size() && "Incomplete escape sequence!");
63 if (isdigit(AggregateString[i+1])) {
64 assert(isdigit(AggregateString[i+2]) &&
65 isdigit(AggregateString[i+3]) &&
66 "Expected 3 digit octal escape!");
67 O << AggregateString[++i];
68 O << AggregateString[++i];
69 O << AggregateString[++i];
70 CharsPrinted += 3;
71 } else {
72 O << AggregateString[++i];
73 ++CharsPrinted;
74 }
75 }
76 O << "\"";
77 }
78};
79
80
Chris Lattner076efa72004-08-01 07:43:02 +000081static bool isIdentChar(char C) {
82 return (C >= 'a' && C <= 'z') ||
83 (C >= 'A' && C <= 'Z') ||
84 (C >= '0' && C <= '9') ||
85 C == '_';
86}
87
Chris Lattnerad8c5312007-07-18 04:51:57 +000088// This should be an anon namespace, this works around a GCC warning.
89namespace llvm {
Chris Lattnerb0b55e72005-01-22 17:32:42 +000090 struct AsmWriterOperand {
David Greenec8d06052009-07-29 20:10:24 +000091 enum OpType {
David Greenebef87682009-07-31 21:57:10 +000092 // Output this text surrounded by quotes to the asm.
David Greenec8d06052009-07-29 20:10:24 +000093 isLiteralTextOperand,
David Greenebef87682009-07-31 21:57:10 +000094 // This is the name of a routine to call to print the operand.
David Greenec8d06052009-07-29 20:10:24 +000095 isMachineInstrOperand,
David Greenebef87682009-07-31 21:57:10 +000096 // Output this text verbatim to the asm writer. It is code that
97 // will output some text to the asm.
David Greenec8d06052009-07-29 20:10:24 +000098 isLiteralStatementOperand
99 } OperandType;
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000100
101 /// Str - For isLiteralTextOperand, this IS the literal text. For
David Greenebef87682009-07-31 21:57:10 +0000102 /// isMachineInstrOperand, this is the PrinterMethodName for the operand..
103 /// For isLiteralStatementOperand, this is the code to insert verbatim
104 /// into the asm writer.
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000105 std::string Str;
106
107 /// MiOpNo - For isMachineInstrOperand, this is the operand number of the
108 /// machine instruction.
109 unsigned MIOpNo;
Chris Lattner04cadb32006-02-06 23:40:48 +0000110
111 /// MiModifier - For isMachineInstrOperand, this is the modifier string for
112 /// an operand, specified with syntax like ${opname:modifier}.
113 std::string MiModifier;
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000114
Cedric Venet7caa2d02008-10-27 19:21:35 +0000115 // To make VS STL happy
David Greenec8d06052009-07-29 20:10:24 +0000116 AsmWriterOperand(OpType op = isLiteralTextOperand):OperandType(op) {}
Cedric Venet3bff2df2008-10-26 15:40:44 +0000117
David Greenec8d06052009-07-29 20:10:24 +0000118 AsmWriterOperand(const std::string &LitStr,
119 OpType op = isLiteralTextOperand)
120 : OperandType(op), Str(LitStr) {}
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000121
Chris Lattner04cadb32006-02-06 23:40:48 +0000122 AsmWriterOperand(const std::string &Printer, unsigned OpNo,
David Greenec8d06052009-07-29 20:10:24 +0000123 const std::string &Modifier,
124 OpType op = isMachineInstrOperand)
125 : OperandType(op), Str(Printer), MIOpNo(OpNo),
Chris Lattner04cadb32006-02-06 23:40:48 +0000126 MiModifier(Modifier) {}
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000127
Chris Lattner870c0162005-01-22 18:38:13 +0000128 bool operator!=(const AsmWriterOperand &Other) const {
129 if (OperandType != Other.OperandType || Str != Other.Str) return true;
130 if (OperandType == isMachineInstrOperand)
Chris Lattner04cadb32006-02-06 23:40:48 +0000131 return MIOpNo != Other.MIOpNo || MiModifier != Other.MiModifier;
Chris Lattner870c0162005-01-22 18:38:13 +0000132 return false;
133 }
Chris Lattner38c07512005-01-22 20:31:17 +0000134 bool operator==(const AsmWriterOperand &Other) const {
135 return !operator!=(Other);
136 }
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000137
138 /// getCode - Return the code that prints this operand.
139 std::string getCode() const;
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000140 };
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000141}
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000142
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000143namespace llvm {
Jeff Cohend41b30d2006-11-05 19:31:28 +0000144 class AsmWriterInst {
145 public:
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000146 std::vector<AsmWriterOperand> Operands;
Chris Lattner5765dba2005-01-22 17:40:38 +0000147 const CodeGenInstruction *CGI;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000148
Chris Lattner59e86772009-08-07 23:13:38 +0000149 AsmWriterInst(const CodeGenInstruction &CGI, Record *AsmWriter);
Chris Lattner870c0162005-01-22 18:38:13 +0000150
Chris Lattnerf8766682005-01-22 19:22:23 +0000151 /// MatchesAllButOneOp - If this instruction is exactly identical to the
152 /// specified instruction except for one differing operand, return the
153 /// differing operand number. Otherwise return ~0.
154 unsigned MatchesAllButOneOp(const AsmWriterInst &Other) const;
Chris Lattner870c0162005-01-22 18:38:13 +0000155
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000156 private:
157 void AddLiteralString(const std::string &Str) {
158 // If the last operand was already a literal text string, append this to
159 // it, otherwise add a new operand.
160 if (!Operands.empty() &&
161 Operands.back().OperandType == AsmWriterOperand::isLiteralTextOperand)
162 Operands.back().Str.append(Str);
163 else
164 Operands.push_back(AsmWriterOperand(Str));
165 }
166 };
167}
168
169
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000170std::string AsmWriterOperand::getCode() const {
Chris Lattner2698cb62009-08-08 00:05:42 +0000171 if (OperandType == isLiteralTextOperand) {
172 if (Str.size() == 1)
173 return "O << '" + Str + "'; ";
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000174 return "O << \"" + Str + "\"; ";
David Greenec8d06052009-07-29 20:10:24 +0000175 }
176
Chris Lattner2698cb62009-08-08 00:05:42 +0000177 if (OperandType == isLiteralStatementOperand)
178 return Str;
179
Chris Lattner1bf63612006-09-26 23:45:08 +0000180 std::string Result = Str + "(MI";
181 if (MIOpNo != ~0U)
182 Result += ", " + utostr(MIOpNo);
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000183 if (!MiModifier.empty())
184 Result += ", \"" + MiModifier + '"';
185 return Result + "); ";
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000186}
187
188
189/// ParseAsmString - Parse the specified Instruction's AsmString into this
190/// AsmWriterInst.
191///
Chris Lattner59e86772009-08-07 23:13:38 +0000192AsmWriterInst::AsmWriterInst(const CodeGenInstruction &CGI, Record *AsmWriter) {
Chris Lattner5765dba2005-01-22 17:40:38 +0000193 this->CGI = &CGI;
Chris Lattner59e86772009-08-07 23:13:38 +0000194
195 unsigned Variant = AsmWriter->getValueAsInt("Variant");
196 int FirstOperandColumn = AsmWriter->getValueAsInt("FirstOperandColumn");
197 int OperandSpacing = AsmWriter->getValueAsInt("OperandSpacing");
198
Chris Lattnerb03b0802006-02-06 22:43:28 +0000199 unsigned CurVariant = ~0U; // ~0 if we are outside a {.|.|.} region, other #.
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000200
Chris Lattner59e86772009-08-07 23:13:38 +0000201 // This is the number of tabs we've seen if we're doing columnar layout.
202 unsigned CurColumn = 0;
203
204
Chris Lattner1cf9d962006-02-01 19:12:23 +0000205 // NOTE: Any extensions to this code need to be mirrored in the
206 // AsmPrinter::printInlineAsm code that executes as compile time (assuming
207 // that inline asm strings should also get the new feature)!
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000208 const std::string &AsmString = CGI.AsmString;
209 std::string::size_type LastEmitted = 0;
210 while (LastEmitted != AsmString.size()) {
211 std::string::size_type DollarPos =
Nate Begeman817affc2008-03-17 07:26:14 +0000212 AsmString.find_first_of("${|}\\", LastEmitted);
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000213 if (DollarPos == std::string::npos) DollarPos = AsmString.size();
214
215 // Emit a constant string fragment.
David Greenec8d06052009-07-29 20:10:24 +0000216
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000217 if (DollarPos != LastEmitted) {
Chris Lattner7f3b28a2009-03-13 21:33:17 +0000218 if (CurVariant == Variant || CurVariant == ~0U) {
219 for (; LastEmitted != DollarPos; ++LastEmitted)
220 switch (AsmString[LastEmitted]) {
David Greenec8d06052009-07-29 20:10:24 +0000221 case '\n':
David Greenec8d06052009-07-29 20:10:24 +0000222 AddLiteralString("\\n");
223 break;
Chris Lattner59e86772009-08-07 23:13:38 +0000224 case '\t':
225 // If the asm writer is not using a columnar layout, \t is not
226 // magic.
227 if (FirstOperandColumn == -1 || OperandSpacing == -1) {
228 AddLiteralString("\\t");
Benjamin Kramerfadf1312009-08-07 23:37:47 +0000229 } else {
230 // We recognize a tab as an operand delimeter.
231 unsigned DestColumn = FirstOperandColumn +
232 CurColumn++ * OperandSpacing;
233 Operands.push_back(
234 AsmWriterOperand("O.PadToColumn(" +
Chris Lattner8f4b1ec2009-08-17 15:48:08 +0000235 utostr(DestColumn) + ");\n",
Benjamin Kramerfadf1312009-08-07 23:37:47 +0000236 AsmWriterOperand::isLiteralStatementOperand));
Chris Lattner59e86772009-08-07 23:13:38 +0000237 }
David Greenec8d06052009-07-29 20:10:24 +0000238 break;
239 case '"':
David Greenec8d06052009-07-29 20:10:24 +0000240 AddLiteralString("\\\"");
241 break;
242 case '\\':
David Greenec8d06052009-07-29 20:10:24 +0000243 AddLiteralString("\\\\");
244 break;
Chris Lattner7f3b28a2009-03-13 21:33:17 +0000245 default:
246 AddLiteralString(std::string(1, AsmString[LastEmitted]));
247 break;
248 }
249 } else {
250 LastEmitted = DollarPos;
251 }
Nate Begeman817affc2008-03-17 07:26:14 +0000252 } else if (AsmString[DollarPos] == '\\') {
253 if (DollarPos+1 != AsmString.size() &&
254 (CurVariant == Variant || CurVariant == ~0U)) {
255 if (AsmString[DollarPos+1] == 'n') {
256 AddLiteralString("\\n");
257 } else if (AsmString[DollarPos+1] == 't') {
Chris Lattner59e86772009-08-07 23:13:38 +0000258 // If the asm writer is not using a columnar layout, \t is not
259 // magic.
260 if (FirstOperandColumn == -1 || OperandSpacing == -1) {
261 AddLiteralString("\\t");
262 break;
263 }
264
265 // We recognize a tab as an operand delimeter.
266 unsigned DestColumn = FirstOperandColumn +
267 CurColumn++ * OperandSpacing;
David Greenebef87682009-07-31 21:57:10 +0000268 Operands.push_back(
Chris Lattner8f4b1ec2009-08-17 15:48:08 +0000269 AsmWriterOperand("O.PadToColumn(" + utostr(DestColumn) + ");\n",
David Greenebef87682009-07-31 21:57:10 +0000270 AsmWriterOperand::isLiteralStatementOperand));
Chris Lattner59e86772009-08-07 23:13:38 +0000271 break;
Nate Begeman817affc2008-03-17 07:26:14 +0000272 } else if (std::string("${|}\\").find(AsmString[DollarPos+1])
273 != std::string::npos) {
274 AddLiteralString(std::string(1, AsmString[DollarPos+1]));
275 } else {
276 throw "Non-supported escaped character found in instruction '" +
277 CGI.TheDef->getName() + "'!";
278 }
279 LastEmitted = DollarPos+2;
280 continue;
281 }
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000282 } else if (AsmString[DollarPos] == '{') {
Chris Lattnerb03b0802006-02-06 22:43:28 +0000283 if (CurVariant != ~0U)
Jeff Cohen00b168892005-07-27 06:12:32 +0000284 throw "Nested variants found for instruction '" +
Chris Lattner3e3def92005-07-15 22:43:04 +0000285 CGI.TheDef->getName() + "'!";
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000286 LastEmitted = DollarPos+1;
Chris Lattnerb03b0802006-02-06 22:43:28 +0000287 CurVariant = 0; // We are now inside of the variant!
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000288 } else if (AsmString[DollarPos] == '|') {
Chris Lattnerb03b0802006-02-06 22:43:28 +0000289 if (CurVariant == ~0U)
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000290 throw "'|' character found outside of a variant in instruction '"
Chris Lattner3e3def92005-07-15 22:43:04 +0000291 + CGI.TheDef->getName() + "'!";
Chris Lattnerb03b0802006-02-06 22:43:28 +0000292 ++CurVariant;
293 ++LastEmitted;
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000294 } else if (AsmString[DollarPos] == '}') {
Chris Lattnerb03b0802006-02-06 22:43:28 +0000295 if (CurVariant == ~0U)
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000296 throw "'}' character found outside of a variant in instruction '"
Chris Lattner3e3def92005-07-15 22:43:04 +0000297 + CGI.TheDef->getName() + "'!";
Chris Lattnerb03b0802006-02-06 22:43:28 +0000298 ++LastEmitted;
299 CurVariant = ~0U;
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000300 } else if (DollarPos+1 != AsmString.size() &&
301 AsmString[DollarPos+1] == '$') {
David Greenec8d06052009-07-29 20:10:24 +0000302 if (CurVariant == Variant || CurVariant == ~0U) {
Chris Lattnerb03b0802006-02-06 22:43:28 +0000303 AddLiteralString("$"); // "$$" -> $
David Greenec8d06052009-07-29 20:10:24 +0000304 }
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000305 LastEmitted = DollarPos+2;
306 } else {
307 // Get the name of the variable.
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000308 std::string::size_type VarEnd = DollarPos+1;
David Greenec8d06052009-07-29 20:10:24 +0000309
Nate Begemanafc54562005-07-14 22:50:30 +0000310 // handle ${foo}bar as $foo by detecting whether the character following
311 // the dollar sign is a curly brace. If so, advance VarEnd and DollarPos
312 // so the variable name does not contain the leading curly brace.
313 bool hasCurlyBraces = false;
314 if (VarEnd < AsmString.size() && '{' == AsmString[VarEnd]) {
315 hasCurlyBraces = true;
316 ++DollarPos;
317 ++VarEnd;
318 }
319
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000320 while (VarEnd < AsmString.size() && isIdentChar(AsmString[VarEnd]))
321 ++VarEnd;
322 std::string VarName(AsmString.begin()+DollarPos+1,
323 AsmString.begin()+VarEnd);
Nate Begemanafc54562005-07-14 22:50:30 +0000324
Chris Lattner04cadb32006-02-06 23:40:48 +0000325 // Modifier - Support ${foo:modifier} syntax, where "modifier" is passed
Chris Lattner1bf63612006-09-26 23:45:08 +0000326 // into printOperand. Also support ${:feature}, which is passed into
Chris Lattner16f046a2006-09-26 23:47:10 +0000327 // PrintSpecial.
Chris Lattner04cadb32006-02-06 23:40:48 +0000328 std::string Modifier;
329
Nate Begemanafc54562005-07-14 22:50:30 +0000330 // In order to avoid starting the next string at the terminating curly
331 // brace, advance the end position past it if we found an opening curly
332 // brace.
333 if (hasCurlyBraces) {
334 if (VarEnd >= AsmString.size())
335 throw "Reached end of string before terminating curly brace in '"
Chris Lattner3e3def92005-07-15 22:43:04 +0000336 + CGI.TheDef->getName() + "'";
Chris Lattner04cadb32006-02-06 23:40:48 +0000337
338 // Look for a modifier string.
339 if (AsmString[VarEnd] == ':') {
340 ++VarEnd;
341 if (VarEnd >= AsmString.size())
342 throw "Reached end of string before terminating curly brace in '"
343 + CGI.TheDef->getName() + "'";
344
345 unsigned ModifierStart = VarEnd;
346 while (VarEnd < AsmString.size() && isIdentChar(AsmString[VarEnd]))
347 ++VarEnd;
348 Modifier = std::string(AsmString.begin()+ModifierStart,
349 AsmString.begin()+VarEnd);
350 if (Modifier.empty())
351 throw "Bad operand modifier name in '"+ CGI.TheDef->getName() + "'";
352 }
353
Nate Begemanafc54562005-07-14 22:50:30 +0000354 if (AsmString[VarEnd] != '}')
Chris Lattnerb03b0802006-02-06 22:43:28 +0000355 throw "Variable name beginning with '{' did not end with '}' in '"
Chris Lattner3e3def92005-07-15 22:43:04 +0000356 + CGI.TheDef->getName() + "'";
Nate Begemanafc54562005-07-14 22:50:30 +0000357 ++VarEnd;
358 }
Chris Lattner1bf63612006-09-26 23:45:08 +0000359 if (VarName.empty() && Modifier.empty())
Jeff Cohen00b168892005-07-27 06:12:32 +0000360 throw "Stray '$' in '" + CGI.TheDef->getName() +
Chris Lattner3e3def92005-07-15 22:43:04 +0000361 "' asm string, maybe you want $$?";
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000362
Chris Lattner1bf63612006-09-26 23:45:08 +0000363 if (VarName.empty()) {
Chris Lattner16f046a2006-09-26 23:47:10 +0000364 // Just a modifier, pass this into PrintSpecial.
365 Operands.push_back(AsmWriterOperand("PrintSpecial", ~0U, Modifier));
Chris Lattner1bf63612006-09-26 23:45:08 +0000366 } else {
367 // Otherwise, normal operand.
368 unsigned OpNo = CGI.getOperandNamed(VarName);
369 CodeGenInstruction::OperandInfo OpInfo = CGI.OperandList[OpNo];
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000370
Chris Lattnerf64f9a42006-11-15 23:23:02 +0000371 if (CurVariant == Variant || CurVariant == ~0U) {
372 unsigned MIOp = OpInfo.MIOperandNo;
Chris Lattner1bf63612006-09-26 23:45:08 +0000373 Operands.push_back(AsmWriterOperand(OpInfo.PrinterMethodName, MIOp,
374 Modifier));
Chris Lattnerf64f9a42006-11-15 23:23:02 +0000375 }
Chris Lattner1bf63612006-09-26 23:45:08 +0000376 }
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000377 LastEmitted = VarEnd;
378 }
379 }
Chris Lattner28179db2009-09-09 23:09:29 +0000380
Chris Lattner28179db2009-09-09 23:09:29 +0000381 Operands.push_back(AsmWriterOperand("return;",
382 AsmWriterOperand::isLiteralStatementOperand));
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000383}
384
Chris Lattnerf8766682005-01-22 19:22:23 +0000385/// MatchesAllButOneOp - If this instruction is exactly identical to the
386/// specified instruction except for one differing operand, return the differing
387/// operand number. If more than one operand mismatches, return ~1, otherwise
388/// if the instructions are identical return ~0.
389unsigned AsmWriterInst::MatchesAllButOneOp(const AsmWriterInst &Other)const{
390 if (Operands.size() != Other.Operands.size()) return ~1;
Chris Lattner870c0162005-01-22 18:38:13 +0000391
392 unsigned MismatchOperand = ~0U;
393 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +0000394 if (Operands[i] != Other.Operands[i]) {
Chris Lattnerf8766682005-01-22 19:22:23 +0000395 if (MismatchOperand != ~0U) // Already have one mismatch?
396 return ~1U;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000397 else
Chris Lattner870c0162005-01-22 18:38:13 +0000398 MismatchOperand = i;
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +0000399 }
Chris Lattner870c0162005-01-22 18:38:13 +0000400 }
401 return MismatchOperand;
402}
403
Chris Lattner38c07512005-01-22 20:31:17 +0000404static void PrintCases(std::vector<std::pair<std::string,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000405 AsmWriterOperand> > &OpsToPrint, raw_ostream &O) {
Chris Lattner38c07512005-01-22 20:31:17 +0000406 O << " case " << OpsToPrint.back().first << ": ";
407 AsmWriterOperand TheOp = OpsToPrint.back().second;
408 OpsToPrint.pop_back();
409
410 // Check to see if any other operands are identical in this list, and if so,
411 // emit a case label for them.
412 for (unsigned i = OpsToPrint.size(); i != 0; --i)
413 if (OpsToPrint[i-1].second == TheOp) {
414 O << "\n case " << OpsToPrint[i-1].first << ": ";
415 OpsToPrint.erase(OpsToPrint.begin()+i-1);
416 }
417
418 // Finally, emit the code.
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000419 O << TheOp.getCode();
Chris Lattner38c07512005-01-22 20:31:17 +0000420 O << "break;\n";
421}
422
Chris Lattner870c0162005-01-22 18:38:13 +0000423
424/// EmitInstructions - Emit the last instruction in the vector and any other
425/// instructions that are suitably similar to it.
426static void EmitInstructions(std::vector<AsmWriterInst> &Insts,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000427 raw_ostream &O) {
Chris Lattner870c0162005-01-22 18:38:13 +0000428 AsmWriterInst FirstInst = Insts.back();
429 Insts.pop_back();
430
431 std::vector<AsmWriterInst> SimilarInsts;
432 unsigned DifferingOperand = ~0;
433 for (unsigned i = Insts.size(); i != 0; --i) {
Chris Lattnerf8766682005-01-22 19:22:23 +0000434 unsigned DiffOp = Insts[i-1].MatchesAllButOneOp(FirstInst);
435 if (DiffOp != ~1U) {
Chris Lattner870c0162005-01-22 18:38:13 +0000436 if (DifferingOperand == ~0U) // First match!
437 DifferingOperand = DiffOp;
438
439 // If this differs in the same operand as the rest of the instructions in
440 // this class, move it to the SimilarInsts list.
Chris Lattnerf8766682005-01-22 19:22:23 +0000441 if (DifferingOperand == DiffOp || DiffOp == ~0U) {
Chris Lattner870c0162005-01-22 18:38:13 +0000442 SimilarInsts.push_back(Insts[i-1]);
443 Insts.erase(Insts.begin()+i-1);
444 }
445 }
446 }
447
Chris Lattnera1e8a802006-05-01 17:01:17 +0000448 O << " case " << FirstInst.CGI->Namespace << "::"
Chris Lattner870c0162005-01-22 18:38:13 +0000449 << FirstInst.CGI->TheDef->getName() << ":\n";
450 for (unsigned i = 0, e = SimilarInsts.size(); i != e; ++i)
Chris Lattnera1e8a802006-05-01 17:01:17 +0000451 O << " case " << SimilarInsts[i].CGI->Namespace << "::"
Chris Lattner870c0162005-01-22 18:38:13 +0000452 << SimilarInsts[i].CGI->TheDef->getName() << ":\n";
453 for (unsigned i = 0, e = FirstInst.Operands.size(); i != e; ++i) {
454 if (i != DifferingOperand) {
455 // If the operand is the same for all instructions, just print it.
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000456 O << " " << FirstInst.Operands[i].getCode();
Chris Lattner870c0162005-01-22 18:38:13 +0000457 } else {
458 // If this is the operand that varies between all of the instructions,
459 // emit a switch for just this operand now.
460 O << " switch (MI->getOpcode()) {\n";
Chris Lattner38c07512005-01-22 20:31:17 +0000461 std::vector<std::pair<std::string, AsmWriterOperand> > OpsToPrint;
Chris Lattnera1e8a802006-05-01 17:01:17 +0000462 OpsToPrint.push_back(std::make_pair(FirstInst.CGI->Namespace + "::" +
Chris Lattner38c07512005-01-22 20:31:17 +0000463 FirstInst.CGI->TheDef->getName(),
464 FirstInst.Operands[i]));
Misha Brukman3da94ae2005-04-22 00:00:37 +0000465
Chris Lattner870c0162005-01-22 18:38:13 +0000466 for (unsigned si = 0, e = SimilarInsts.size(); si != e; ++si) {
Chris Lattner38c07512005-01-22 20:31:17 +0000467 AsmWriterInst &AWI = SimilarInsts[si];
Chris Lattnera1e8a802006-05-01 17:01:17 +0000468 OpsToPrint.push_back(std::make_pair(AWI.CGI->Namespace+"::"+
Chris Lattner38c07512005-01-22 20:31:17 +0000469 AWI.CGI->TheDef->getName(),
470 AWI.Operands[i]));
Chris Lattner870c0162005-01-22 18:38:13 +0000471 }
Chris Lattner38c07512005-01-22 20:31:17 +0000472 std::reverse(OpsToPrint.begin(), OpsToPrint.end());
473 while (!OpsToPrint.empty())
474 PrintCases(OpsToPrint, O);
Chris Lattner870c0162005-01-22 18:38:13 +0000475 O << " }";
476 }
477 O << "\n";
478 }
Chris Lattner870c0162005-01-22 18:38:13 +0000479 O << " break;\n";
480}
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000481
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000482void AsmWriterEmitter::
483FindUniqueOperandCommands(std::vector<std::string> &UniqueOperandCommands,
Chris Lattner96c1ade2006-07-18 18:28:27 +0000484 std::vector<unsigned> &InstIdxs,
485 std::vector<unsigned> &InstOpsUsed) const {
Chris Lattner195bb4a2006-07-18 19:27:30 +0000486 InstIdxs.assign(NumberedInstructions.size(), ~0U);
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000487
488 // This vector parallels UniqueOperandCommands, keeping track of which
489 // instructions each case are used for. It is a comma separated string of
490 // enums.
491 std::vector<std::string> InstrsForCase;
492 InstrsForCase.resize(UniqueOperandCommands.size());
Chris Lattner96c1ade2006-07-18 18:28:27 +0000493 InstOpsUsed.assign(UniqueOperandCommands.size(), 0);
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000494
495 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
496 const AsmWriterInst *Inst = getAsmWriterInstByID(i);
Dan Gohman44066042008-07-01 00:05:16 +0000497 if (Inst == 0) continue; // PHI, INLINEASM, DBG_LABEL, etc.
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000498
499 std::string Command;
Chris Lattnerb8462862006-07-18 17:56:07 +0000500 if (Inst->Operands.empty())
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000501 continue; // Instruction already done.
Chris Lattner191dd1f2006-07-18 17:50:22 +0000502
Chris Lattnerb8462862006-07-18 17:56:07 +0000503 Command = " " + Inst->Operands[0].getCode() + "\n";
Chris Lattner191dd1f2006-07-18 17:50:22 +0000504
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000505 // Check to see if we already have 'Command' in UniqueOperandCommands.
506 // If not, add it.
507 bool FoundIt = false;
508 for (unsigned idx = 0, e = UniqueOperandCommands.size(); idx != e; ++idx)
509 if (UniqueOperandCommands[idx] == Command) {
510 InstIdxs[i] = idx;
511 InstrsForCase[idx] += ", ";
512 InstrsForCase[idx] += Inst->CGI->TheDef->getName();
513 FoundIt = true;
514 break;
515 }
516 if (!FoundIt) {
517 InstIdxs[i] = UniqueOperandCommands.size();
518 UniqueOperandCommands.push_back(Command);
519 InstrsForCase.push_back(Inst->CGI->TheDef->getName());
Chris Lattner96c1ade2006-07-18 18:28:27 +0000520
521 // This command matches one operand so far.
522 InstOpsUsed.push_back(1);
523 }
524 }
525
526 // For each entry of UniqueOperandCommands, there is a set of instructions
527 // that uses it. If the next command of all instructions in the set are
528 // identical, fold it into the command.
529 for (unsigned CommandIdx = 0, e = UniqueOperandCommands.size();
530 CommandIdx != e; ++CommandIdx) {
531
532 for (unsigned Op = 1; ; ++Op) {
533 // Scan for the first instruction in the set.
534 std::vector<unsigned>::iterator NIT =
535 std::find(InstIdxs.begin(), InstIdxs.end(), CommandIdx);
536 if (NIT == InstIdxs.end()) break; // No commonality.
537
538 // If this instruction has no more operands, we isn't anything to merge
539 // into this command.
540 const AsmWriterInst *FirstInst =
541 getAsmWriterInstByID(NIT-InstIdxs.begin());
542 if (!FirstInst || FirstInst->Operands.size() == Op)
543 break;
544
545 // Otherwise, scan to see if all of the other instructions in this command
546 // set share the operand.
547 bool AllSame = true;
David Greenec8d06052009-07-29 20:10:24 +0000548 // Keep track of the maximum, number of operands or any
549 // instruction we see in the group.
550 size_t MaxSize = FirstInst->Operands.size();
551
Chris Lattner96c1ade2006-07-18 18:28:27 +0000552 for (NIT = std::find(NIT+1, InstIdxs.end(), CommandIdx);
553 NIT != InstIdxs.end();
554 NIT = std::find(NIT+1, InstIdxs.end(), CommandIdx)) {
555 // Okay, found another instruction in this command set. If the operand
556 // matches, we're ok, otherwise bail out.
557 const AsmWriterInst *OtherInst =
558 getAsmWriterInstByID(NIT-InstIdxs.begin());
David Greenec8d06052009-07-29 20:10:24 +0000559
560 if (OtherInst &&
561 OtherInst->Operands.size() > FirstInst->Operands.size())
562 MaxSize = std::max(MaxSize, OtherInst->Operands.size());
563
Chris Lattner96c1ade2006-07-18 18:28:27 +0000564 if (!OtherInst || OtherInst->Operands.size() == Op ||
565 OtherInst->Operands[Op] != FirstInst->Operands[Op]) {
566 AllSame = false;
567 break;
568 }
569 }
570 if (!AllSame) break;
571
572 // Okay, everything in this command set has the same next operand. Add it
573 // to UniqueOperandCommands and remember that it was consumed.
574 std::string Command = " " + FirstInst->Operands[Op].getCode() + "\n";
575
Chris Lattner96c1ade2006-07-18 18:28:27 +0000576 UniqueOperandCommands[CommandIdx] += Command;
577 InstOpsUsed[CommandIdx]++;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000578 }
579 }
580
581 // Prepend some of the instructions each case is used for onto the case val.
582 for (unsigned i = 0, e = InstrsForCase.size(); i != e; ++i) {
583 std::string Instrs = InstrsForCase[i];
584 if (Instrs.size() > 70) {
585 Instrs.erase(Instrs.begin()+70, Instrs.end());
586 Instrs += "...";
587 }
588
589 if (!Instrs.empty())
590 UniqueOperandCommands[i] = " // " + Instrs + "\n" +
591 UniqueOperandCommands[i];
592 }
593}
594
595
Chris Lattner05af2612009-09-13 20:08:00 +0000596/// EmitPrintInstruction - Generate the code for the "printInstruction" method
597/// implementation.
598void AsmWriterEmitter::EmitPrintInstruction(raw_ostream &O) {
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000599 CodeGenTarget Target;
Chris Lattner175580c2004-08-14 22:50:53 +0000600 Record *AsmWriter = Target.getAsmWriter();
Chris Lattner953c6fe2004-10-03 20:19:02 +0000601 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
Chris Lattner05af2612009-09-13 20:08:00 +0000602
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000603 O <<
604 "/// printInstruction - This method is automatically generated by tablegen\n"
Chris Lattner05af2612009-09-13 20:08:00 +0000605 "/// from the instruction set description.\n"
Chris Lattner41aefdc2009-08-08 01:32:19 +0000606 "void " << Target.getName() << ClassName
Chris Lattner175580c2004-08-14 22:50:53 +0000607 << "::printInstruction(const MachineInstr *MI) {\n";
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000608
Chris Lattner5765dba2005-01-22 17:40:38 +0000609 std::vector<AsmWriterInst> Instructions;
610
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000611 for (CodeGenTarget::inst_iterator I = Target.inst_begin(),
612 E = Target.inst_end(); I != E; ++I)
Chris Lattner5f12c212009-09-11 00:41:15 +0000613 if (!I->second.AsmString.empty() &&
614 I->second.TheDef->getName() != "PHI")
Chris Lattner59e86772009-08-07 23:13:38 +0000615 Instructions.push_back(AsmWriterInst(I->second, AsmWriter));
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.
Chris Lattner3200fc92009-09-14 01:16:36 +0000627 StringToOffsetTable StringTable;
Chris Lattner6af022f2006-07-14 22:59:11 +0000628
Chris Lattner259bda42006-09-27 16:44:09 +0000629 /// OpcodeInfo - This encodes the index of the string to use for the first
Chris Lattner55616402006-07-18 17:32:27 +0000630 /// chunk of the output as well as indices used for operand printing.
631 std::vector<unsigned> OpcodeInfo;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000632
Chris Lattner55616402006-07-18 17:32:27 +0000633 unsigned MaxStringIdx = 0;
Chris Lattner6af022f2006-07-14 22:59:11 +0000634 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
635 AsmWriterInst *AWI = CGIAWIMap[NumberedInstructions[i]];
636 unsigned Idx;
Chris Lattnera6dc9fb2006-07-19 01:39:06 +0000637 if (AWI == 0) {
Chris Lattner6af022f2006-07-14 22:59:11 +0000638 // Something not handled by the asmwriter printer.
Chris Lattner3200fc92009-09-14 01:16:36 +0000639 Idx = ~0U;
Chris Lattnera6dc9fb2006-07-19 01:39:06 +0000640 } else if (AWI->Operands[0].OperandType !=
641 AsmWriterOperand::isLiteralTextOperand ||
642 AWI->Operands[0].Str.empty()) {
643 // Something handled by the asmwriter printer, but with no leading string.
Chris Lattner3200fc92009-09-14 01:16:36 +0000644 Idx = StringTable.GetOrAddStringOffset("");
Chris Lattner6af022f2006-07-14 22:59:11 +0000645 } else {
Chris Lattner3200fc92009-09-14 01:16:36 +0000646 std::string Str = AWI->Operands[0].Str;
647 UnescapeString(Str);
648 Idx = StringTable.GetOrAddStringOffset(Str);
649 MaxStringIdx = std::max(MaxStringIdx, Idx);
650
Chris Lattner6af022f2006-07-14 22:59:11 +0000651 // Nuke the string from the operand list. It is now handled!
652 AWI->Operands.erase(AWI->Operands.begin());
Chris Lattnerf8766682005-01-22 19:22:23 +0000653 }
Chris Lattner3200fc92009-09-14 01:16:36 +0000654
655 // Bias offset by one since we want 0 as a sentinel.
656 OpcodeInfo.push_back(Idx+1);
Chris Lattnerf8766682005-01-22 19:22:23 +0000657 }
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000658
Chris Lattner55616402006-07-18 17:32:27 +0000659 // Figure out how many bits we used for the string index.
Chris Lattner3200fc92009-09-14 01:16:36 +0000660 unsigned AsmStrBits = Log2_32_Ceil(MaxStringIdx+2);
Chris Lattner55616402006-07-18 17:32:27 +0000661
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000662 // To reduce code size, we compactify common instructions into a few bits
663 // in the opcode-indexed table.
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000664 unsigned BitsLeft = 32-AsmStrBits;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000665
666 std::vector<std::vector<std::string> > TableDrivenOperandPrinters;
667
Chris Lattnerb8462862006-07-18 17:56:07 +0000668 while (1) {
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000669 std::vector<std::string> UniqueOperandCommands;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000670 std::vector<unsigned> InstIdxs;
Chris Lattner96c1ade2006-07-18 18:28:27 +0000671 std::vector<unsigned> NumInstOpsHandled;
672 FindUniqueOperandCommands(UniqueOperandCommands, InstIdxs,
673 NumInstOpsHandled);
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000674
675 // If we ran out of operands to print, we're done.
676 if (UniqueOperandCommands.empty()) break;
677
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000678 // Compute the number of bits we need to represent these cases, this is
679 // ceil(log2(numentries)).
680 unsigned NumBits = Log2_32_Ceil(UniqueOperandCommands.size());
681
682 // If we don't have enough bits for this operand, don't include it.
683 if (NumBits > BitsLeft) {
Chris Lattner569f1212009-08-23 04:44:11 +0000684 DEBUG(errs() << "Not enough bits to densely encode " << NumBits
685 << " more bits\n");
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000686 break;
687 }
688
689 // Otherwise, we can include this in the initial lookup table. Add it in.
690 BitsLeft -= NumBits;
691 for (unsigned i = 0, e = InstIdxs.size(); i != e; ++i)
Chris Lattner195bb4a2006-07-18 19:27:30 +0000692 if (InstIdxs[i] != ~0U)
693 OpcodeInfo[i] |= InstIdxs[i] << (BitsLeft+AsmStrBits);
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000694
Chris Lattnerb8462862006-07-18 17:56:07 +0000695 // Remove the info about this operand.
696 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
697 if (AsmWriterInst *Inst = getAsmWriterInstByID(i))
Chris Lattner96c1ade2006-07-18 18:28:27 +0000698 if (!Inst->Operands.empty()) {
699 unsigned NumOps = NumInstOpsHandled[InstIdxs[i]];
Chris Lattner0a012122006-07-18 19:06:01 +0000700 assert(NumOps <= Inst->Operands.size() &&
701 "Can't remove this many ops!");
Chris Lattner96c1ade2006-07-18 18:28:27 +0000702 Inst->Operands.erase(Inst->Operands.begin(),
703 Inst->Operands.begin()+NumOps);
704 }
Chris Lattnerb8462862006-07-18 17:56:07 +0000705 }
706
707 // Remember the handlers for this set of operands.
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000708 TableDrivenOperandPrinters.push_back(UniqueOperandCommands);
709 }
710
711
712
Chris Lattner55616402006-07-18 17:32:27 +0000713 O<<" static const unsigned OpInfo[] = {\n";
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000714 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000715 O << " " << OpcodeInfo[i] << "U,\t// "
Chris Lattner55616402006-07-18 17:32:27 +0000716 << NumberedInstructions[i]->TheDef->getName() << "\n";
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000717 }
718 // Add a dummy entry so the array init doesn't end with a comma.
Chris Lattner55616402006-07-18 17:32:27 +0000719 O << " 0U\n";
Chris Lattner6af022f2006-07-14 22:59:11 +0000720 O << " };\n\n";
721
722 // Emit the string itself.
Chris Lattner3200fc92009-09-14 01:16:36 +0000723 O << " const char *AsmStrs = \n";
724 StringTable.EmitString(O);
725 O << ";\n\n";
Chris Lattner6af022f2006-07-14 22:59:11 +0000726
Chris Lattner5b842c32009-06-19 23:57:53 +0000727 O << "\n#ifndef NO_ASM_WRITER_BOILERPLATE\n";
728
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000729 O << " if (MI->getOpcode() == TargetInstrInfo::INLINEASM) {\n"
Evan Cheng4eecdeb2008-02-02 08:39:46 +0000730 << " O << \"\\t\";\n"
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000731 << " printInlineAsm(MI);\n"
Chris Lattner41aefdc2009-08-08 01:32:19 +0000732 << " return;\n"
Dan Gohman44066042008-07-01 00:05:16 +0000733 << " } else if (MI->isLabel()) {\n"
Jim Laskeya683f9b2007-01-26 17:29:20 +0000734 << " printLabel(MI);\n"
Chris Lattner41aefdc2009-08-08 01:32:19 +0000735 << " return;\n"
Evan Chengda47e6e2008-03-15 00:03:38 +0000736 << " } else if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {\n"
737 << " printImplicitDef(MI);\n"
Chris Lattner41aefdc2009-08-08 01:32:19 +0000738 << " return;\n"
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000739 << " }\n\n";
Chris Lattner5b842c32009-06-19 23:57:53 +0000740
741 O << "\n#endif\n";
742
Evan Cheng4eecdeb2008-02-02 08:39:46 +0000743 O << " O << \"\\t\";\n\n";
744
Chris Lattner6af022f2006-07-14 22:59:11 +0000745 O << " // Emit the opcode for the instruction.\n"
Chris Lattner55616402006-07-18 17:32:27 +0000746 << " unsigned Bits = OpInfo[MI->getOpcode()];\n"
Chris Lattner41aefdc2009-08-08 01:32:19 +0000747 << " assert(Bits != 0 && \"Cannot print this instruction.\");\n"
Chris Lattner3200fc92009-09-14 01:16:36 +0000748 << " O << AsmStrs+(Bits & " << (1 << AsmStrBits)-1 << ")-1;\n\n";
David Greenea5bb59f2009-08-05 21:00:52 +0000749
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000750 // Output the table driven operand information.
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000751 BitsLeft = 32-AsmStrBits;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000752 for (unsigned i = 0, e = TableDrivenOperandPrinters.size(); i != e; ++i) {
753 std::vector<std::string> &Commands = TableDrivenOperandPrinters[i];
754
755 // Compute the number of bits we need to represent these cases, this is
756 // ceil(log2(numentries)).
757 unsigned NumBits = Log2_32_Ceil(Commands.size());
758 assert(NumBits <= BitsLeft && "consistency error");
759
760 // Emit code to extract this field from Bits.
761 BitsLeft -= NumBits;
762
763 O << "\n // Fragment " << i << " encoded into " << NumBits
Chris Lattnere7a589d2006-07-18 17:43:54 +0000764 << " bits for " << Commands.size() << " unique commands.\n";
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000765
Chris Lattner96c1ade2006-07-18 18:28:27 +0000766 if (Commands.size() == 2) {
Chris Lattnere7a589d2006-07-18 17:43:54 +0000767 // Emit two possibilitys with if/else.
768 O << " if ((Bits >> " << (BitsLeft+AsmStrBits) << ") & "
769 << ((1 << NumBits)-1) << ") {\n"
770 << Commands[1]
771 << " } else {\n"
772 << Commands[0]
773 << " }\n\n";
774 } else {
775 O << " switch ((Bits >> " << (BitsLeft+AsmStrBits) << ") & "
776 << ((1 << NumBits)-1) << ") {\n"
777 << " default: // unreachable.\n";
778
779 // Print out all the cases.
780 for (unsigned i = 0, e = Commands.size(); i != e; ++i) {
781 O << " case " << i << ":\n";
782 O << Commands[i];
783 O << " break;\n";
784 }
785 O << " }\n\n";
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000786 }
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000787 }
788
Chris Lattnerb8462862006-07-18 17:56:07 +0000789 // Okay, delete instructions with no operand info left.
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000790 for (unsigned i = 0, e = Instructions.size(); i != e; ++i) {
791 // Entire instruction has been emitted?
792 AsmWriterInst &Inst = Instructions[i];
Chris Lattnerb8462862006-07-18 17:56:07 +0000793 if (Inst.Operands.empty()) {
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000794 Instructions.erase(Instructions.begin()+i);
Chris Lattnerb8462862006-07-18 17:56:07 +0000795 --i; --e;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000796 }
797 }
798
799
800 // Because this is a vector, we want to emit from the end. Reverse all of the
Chris Lattner870c0162005-01-22 18:38:13 +0000801 // elements in the vector.
802 std::reverse(Instructions.begin(), Instructions.end());
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000803
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000804 if (!Instructions.empty()) {
805 // Find the opcode # of inline asm.
806 O << " switch (MI->getOpcode()) {\n";
807 while (!Instructions.empty())
808 EmitInstructions(Instructions, O);
Chris Lattner870c0162005-01-22 18:38:13 +0000809
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000810 O << " }\n";
Chris Lattner41aefdc2009-08-08 01:32:19 +0000811 O << " return;\n";
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000812 }
David Greenec8d06052009-07-29 20:10:24 +0000813
Chris Lattner41aefdc2009-08-08 01:32:19 +0000814 O << " return;\n";
Chris Lattner0a012122006-07-18 19:06:01 +0000815 O << "}\n";
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000816}
Chris Lattner05af2612009-09-13 20:08:00 +0000817
818
819void AsmWriterEmitter::EmitGetRegisterName(raw_ostream &O) {
820 CodeGenTarget Target;
821 Record *AsmWriter = Target.getAsmWriter();
822 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
823 const std::vector<CodeGenRegister> &Registers = Target.getRegisters();
824
825 O <<
826 "\n\n/// getRegisterName - This method is automatically generated by tblgen\n"
827 "/// from the register set description. This returns the assembler name\n"
828 "/// for the specified register.\n"
829 "const char *" << Target.getName() << ClassName
Chris Lattnerd95148f2009-09-13 20:19:22 +0000830 << "::getRegisterName(unsigned RegNo) {\n"
Chris Lattner05af2612009-09-13 20:08:00 +0000831 << " assert(RegNo && RegNo < " << (Registers.size()+1)
832 << " && \"Invalid register number!\");\n"
833 << "\n"
834 << " static const char *const RegAsmNames[] = {\n";
835 for (unsigned i = 0, e = Registers.size(); i != e; ++i) {
836 const CodeGenRegister &Reg = Registers[i];
837
838 std::string AsmName = Reg.TheDef->getValueAsString("AsmName");
839 if (AsmName.empty())
840 AsmName = Reg.getName();
841 O << " \"" << AsmName << "\",\n";
842 }
843 O << " 0\n"
844 << " };\n"
845 << "\n"
846 << " return RegAsmNames[RegNo-1];\n"
847 << "}\n";
848}
849
850
851void AsmWriterEmitter::run(raw_ostream &O) {
852 EmitSourceFileHeader("Assembly Writer Source Fragment", O);
853
854 EmitPrintInstruction(O);
855 EmitGetRegisterName(O);
856}
857