blob: 069578056c40745d4da9fe296653365d49b7dbe1 [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 {
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 Greenec8d06052009-07-29 20:10:24 +000055 AsmWriterOperand(OpType op = isLiteralTextOperand):OperandType(op) {}
Cedric Venet3bff2df2008-10-26 15:40:44 +000056
David Greenec8d06052009-07-29 20:10:24 +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 Greenec8d06052009-07-29 20:10:24 +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 Greenec8d06052009-07-29 20:10:24 +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 /// ReadingWhitespace - Tell whether we just read some whitespace.
101 bool ReadingWhitespace;
102
103 /// InGroup - Determine whether we are in the middle of an
104 /// operand group.
105 bool InGroup() const { return GroupLevel != -1; }
106
107 /// InWhitespace - Determine whether we are in the middle of
108 /// emitting whitespace.
109 bool InWhitespace() const { return ReadingWhitespace; }
110
Chris Lattner5765dba2005-01-22 17:40:38 +0000111 AsmWriterInst(const CodeGenInstruction &CGI, unsigned Variant);
Chris Lattner870c0162005-01-22 18:38:13 +0000112
Chris Lattnerf8766682005-01-22 19:22:23 +0000113 /// MatchesAllButOneOp - If this instruction is exactly identical to the
114 /// specified instruction except for one differing operand, return the
115 /// differing operand number. Otherwise return ~0.
116 unsigned MatchesAllButOneOp(const AsmWriterInst &Other) const;
Chris Lattner870c0162005-01-22 18:38:13 +0000117
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000118 private:
119 void AddLiteralString(const std::string &Str) {
120 // If the last operand was already a literal text string, append this to
121 // it, otherwise add a new operand.
122 if (!Operands.empty() &&
123 Operands.back().OperandType == AsmWriterOperand::isLiteralTextOperand)
124 Operands.back().Str.append(Str);
125 else
126 Operands.push_back(AsmWriterOperand(Str));
127 }
128 };
129}
130
131
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000132std::string AsmWriterOperand::getCode() const {
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000133 if (OperandType == isLiteralTextOperand)
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000134 return "O << \"" + Str + "\"; ";
135
David Greenec8d06052009-07-29 20:10:24 +0000136 if (OperandType == isLiteralStatementOperand) {
137 return Str;
138 }
139
140 if (OperandType == isLiteralStatementOperand) {
141 return Str;
142 }
143
144 if (OperandType == isLiteralStatementOperand) {
145 return Str;
146 }
147
Chris Lattner1bf63612006-09-26 23:45:08 +0000148 std::string Result = Str + "(MI";
149 if (MIOpNo != ~0U)
150 Result += ", " + utostr(MIOpNo);
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000151 if (!MiModifier.empty())
152 Result += ", \"" + MiModifier + '"';
153 return Result + "); ";
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000154}
155
156
157/// ParseAsmString - Parse the specified Instruction's AsmString into this
158/// AsmWriterInst.
159///
David Greenec8d06052009-07-29 20:10:24 +0000160AsmWriterInst::AsmWriterInst(const CodeGenInstruction &CGI, unsigned Variant)
161 : GroupLevel(-1), ReadingWhitespace(false) {
Chris Lattner5765dba2005-01-22 17:40:38 +0000162 this->CGI = &CGI;
Chris Lattnerb03b0802006-02-06 22:43:28 +0000163 unsigned CurVariant = ~0U; // ~0 if we are outside a {.|.|.} region, other #.
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000164
Chris Lattner1cf9d962006-02-01 19:12:23 +0000165 // NOTE: Any extensions to this code need to be mirrored in the
166 // AsmPrinter::printInlineAsm code that executes as compile time (assuming
167 // that inline asm strings should also get the new feature)!
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000168 const std::string &AsmString = CGI.AsmString;
169 std::string::size_type LastEmitted = 0;
170 while (LastEmitted != AsmString.size()) {
171 std::string::size_type DollarPos =
Nate Begeman817affc2008-03-17 07:26:14 +0000172 AsmString.find_first_of("${|}\\", LastEmitted);
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000173 if (DollarPos == std::string::npos) DollarPos = AsmString.size();
174
175 // Emit a constant string fragment.
David Greenec8d06052009-07-29 20:10:24 +0000176
177 // TODO: Recognize an operand separator to determine when to pad
178 // to the next operator.
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000179 if (DollarPos != LastEmitted) {
Chris Lattner7f3b28a2009-03-13 21:33:17 +0000180 if (CurVariant == Variant || CurVariant == ~0U) {
181 for (; LastEmitted != DollarPos; ++LastEmitted)
182 switch (AsmString[LastEmitted]) {
David Greenec8d06052009-07-29 20:10:24 +0000183 case '\n':
184 assert(!InGroup() && "Missing matching group delimeter");
185 ReadingWhitespace = false;
186 AddLiteralString("\\n");
187 break;
188 case '\t':
189 if (!InGroup()) {
190 ReadingWhitespace = true;
191 }
192 AddLiteralString("\\t");
193 break;
194 case '"':
195 if (InWhitespace() && !InGroup())
196 Operands.push_back(
197 AsmWriterOperand(
198 "O.PadToColumn(TAI->getOperandColumn(OperandColumn++));\n",
199 AsmWriterOperand::isLiteralStatementOperand));
200 ReadingWhitespace = false;
201 AddLiteralString("\\\"");
202 break;
203 case '\\':
204 if (InWhitespace() && !InGroup())
205 Operands.push_back(
206 AsmWriterOperand(
207 "O.PadToColumn(TAI->getOperandColumn(OperandColumn++));\n",
208 AsmWriterOperand::isLiteralStatementOperand));
209 ReadingWhitespace = false;
210 AddLiteralString("\\\\");
211 break;
212
213 case '(': // Fallthrough
214 case '[':
215 if (InWhitespace() && !InGroup())
216 Operands.push_back(
217 AsmWriterOperand(
218 "O.PadToColumn(TAI->getOperandColumn(OperandColumn++));\n",
219 AsmWriterOperand::isLiteralStatementOperand));
220 ReadingWhitespace = false;
221
222 ++GroupLevel;
223 assert(GroupLevel < MAX_GROUP_NESTING_LEVEL
224 && "Exceeded maximum operand group nesting level");
225 GroupDelim[GroupLevel] = AsmString[LastEmitted];
226 AddLiteralString(std::string(1, AsmString[LastEmitted]));
227 break;
228
229 case ')': // Fallthrough
230 case ']':
231 if (InWhitespace() && !InGroup())
232 Operands.push_back(
233 AsmWriterOperand(
234 "O.PadToColumn(TAI->getOperandColumn(OperandColumn++));\n",
235 AsmWriterOperand::isLiteralStatementOperand));
236 ReadingWhitespace = false;
237
238 if (AsmString[LastEmitted] == ')')
239 assert(GroupDelim[GroupLevel] == '(' && "Mismatched delimeters");
240 else
241 assert(GroupDelim[GroupLevel] == '[' && "Mismatched delimeters");
242
243 --GroupLevel;
244 assert(GroupLevel > -2 && "Too many end delimeters!");
245 AddLiteralString(std::string(1, AsmString[LastEmitted]));
246 break;
247
Chris Lattner7f3b28a2009-03-13 21:33:17 +0000248 default:
David Greenec8d06052009-07-29 20:10:24 +0000249 if (AsmString[LastEmitted] != ' ' &&
250 AsmString[LastEmitted] != '\t') {
251 if (!InGroup() && InWhitespace())
252 Operands.push_back(
253 AsmWriterOperand(
254 "O.PadToColumn(TAI->getOperandColumn(OperandColumn++));\n",
255 AsmWriterOperand::isLiteralStatementOperand));
256 ReadingWhitespace = false;
257 }
258 else
259 if (!InGroup())
260 ReadingWhitespace = true;
261
Chris Lattner7f3b28a2009-03-13 21:33:17 +0000262 AddLiteralString(std::string(1, AsmString[LastEmitted]));
263 break;
264 }
265 } else {
266 LastEmitted = DollarPos;
267 }
Nate Begeman817affc2008-03-17 07:26:14 +0000268 } else if (AsmString[DollarPos] == '\\') {
269 if (DollarPos+1 != AsmString.size() &&
270 (CurVariant == Variant || CurVariant == ~0U)) {
271 if (AsmString[DollarPos+1] == 'n') {
David Greenec8d06052009-07-29 20:10:24 +0000272 assert(!InGroup() && "Missing matching group delimeter");
273 ReadingWhitespace = false;
Nate Begeman817affc2008-03-17 07:26:14 +0000274 AddLiteralString("\\n");
275 } else if (AsmString[DollarPos+1] == 't') {
David Greenec8d06052009-07-29 20:10:24 +0000276 if (!InGroup()) {
277 ReadingWhitespace = true;
278 }
Nate Begeman817affc2008-03-17 07:26:14 +0000279 AddLiteralString("\\t");
280 } else if (std::string("${|}\\").find(AsmString[DollarPos+1])
281 != std::string::npos) {
David Greenec8d06052009-07-29 20:10:24 +0000282 if (InWhitespace() && !InGroup())
283 Operands.push_back(
284 AsmWriterOperand(
285 "O.PadToColumn(TAI->getOperandColumn(OperandColumn++));\n",
286 AsmWriterOperand::isLiteralStatementOperand));
287 ReadingWhitespace = false;
288
289 if (AsmString[DollarPos+1] == '{') {
290 ++GroupLevel;
291 assert(GroupLevel < MAX_GROUP_NESTING_LEVEL
292 && "Exceeded maximum operand group nesting level");
293 GroupDelim[GroupLevel] = AsmString[DollarPos+1];
294 } else if (AsmString[DollarPos+1] == '}') {
295 assert(GroupDelim[GroupLevel] == '{' && "Mismatched delimeters");
296 --GroupLevel;
297 assert(GroupLevel > -2 && "Too many end delimeters!");
298 }
Nate Begeman817affc2008-03-17 07:26:14 +0000299 AddLiteralString(std::string(1, AsmString[DollarPos+1]));
300 } else {
301 throw "Non-supported escaped character found in instruction '" +
302 CGI.TheDef->getName() + "'!";
303 }
304 LastEmitted = DollarPos+2;
305 continue;
306 }
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000307 } else if (AsmString[DollarPos] == '{') {
Chris Lattnerb03b0802006-02-06 22:43:28 +0000308 if (CurVariant != ~0U)
Jeff Cohen00b168892005-07-27 06:12:32 +0000309 throw "Nested variants found for instruction '" +
Chris Lattner3e3def92005-07-15 22:43:04 +0000310 CGI.TheDef->getName() + "'!";
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000311 LastEmitted = DollarPos+1;
Chris Lattnerb03b0802006-02-06 22:43:28 +0000312 CurVariant = 0; // We are now inside of the variant!
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000313 } else if (AsmString[DollarPos] == '|') {
Chris Lattnerb03b0802006-02-06 22:43:28 +0000314 if (CurVariant == ~0U)
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000315 throw "'|' character found outside of a variant in instruction '"
Chris Lattner3e3def92005-07-15 22:43:04 +0000316 + CGI.TheDef->getName() + "'!";
Chris Lattnerb03b0802006-02-06 22:43:28 +0000317 ++CurVariant;
318 ++LastEmitted;
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000319 } else if (AsmString[DollarPos] == '}') {
Chris Lattnerb03b0802006-02-06 22:43:28 +0000320 if (CurVariant == ~0U)
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000321 throw "'}' character found outside of a variant in instruction '"
Chris Lattner3e3def92005-07-15 22:43:04 +0000322 + CGI.TheDef->getName() + "'!";
Chris Lattnerb03b0802006-02-06 22:43:28 +0000323 ++LastEmitted;
324 CurVariant = ~0U;
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000325 } else if (DollarPos+1 != AsmString.size() &&
326 AsmString[DollarPos+1] == '$') {
David Greenec8d06052009-07-29 20:10:24 +0000327 if (CurVariant == Variant || CurVariant == ~0U) {
328 if (InWhitespace() && !InGroup())
329 Operands.push_back(
330 AsmWriterOperand(
331 "O.PadToColumn(TAI->getOperandColumn(OperandColumn++));\n",
332 AsmWriterOperand::isLiteralStatementOperand));
333 ReadingWhitespace = false;
Chris Lattnerb03b0802006-02-06 22:43:28 +0000334 AddLiteralString("$"); // "$$" -> $
David Greenec8d06052009-07-29 20:10:24 +0000335 }
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000336 LastEmitted = DollarPos+2;
337 } else {
David Greenec8d06052009-07-29 20:10:24 +0000338 if (InWhitespace() && !InGroup())
339 Operands.push_back(
340 AsmWriterOperand(
341 "O.PadToColumn(TAI->getOperandColumn(OperandColumn++));\n",
342 AsmWriterOperand::isLiteralStatementOperand));
343 ReadingWhitespace = false;
344
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000345 // Get the name of the variable.
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000346 std::string::size_type VarEnd = DollarPos+1;
David Greenec8d06052009-07-29 20:10:24 +0000347
Nate Begemanafc54562005-07-14 22:50:30 +0000348 // handle ${foo}bar as $foo by detecting whether the character following
349 // the dollar sign is a curly brace. If so, advance VarEnd and DollarPos
350 // so the variable name does not contain the leading curly brace.
351 bool hasCurlyBraces = false;
352 if (VarEnd < AsmString.size() && '{' == AsmString[VarEnd]) {
353 hasCurlyBraces = true;
354 ++DollarPos;
355 ++VarEnd;
356 }
357
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000358 while (VarEnd < AsmString.size() && isIdentChar(AsmString[VarEnd]))
359 ++VarEnd;
360 std::string VarName(AsmString.begin()+DollarPos+1,
361 AsmString.begin()+VarEnd);
Nate Begemanafc54562005-07-14 22:50:30 +0000362
Chris Lattner04cadb32006-02-06 23:40:48 +0000363 // Modifier - Support ${foo:modifier} syntax, where "modifier" is passed
Chris Lattner1bf63612006-09-26 23:45:08 +0000364 // into printOperand. Also support ${:feature}, which is passed into
Chris Lattner16f046a2006-09-26 23:47:10 +0000365 // PrintSpecial.
Chris Lattner04cadb32006-02-06 23:40:48 +0000366 std::string Modifier;
367
Nate Begemanafc54562005-07-14 22:50:30 +0000368 // In order to avoid starting the next string at the terminating curly
369 // brace, advance the end position past it if we found an opening curly
370 // brace.
371 if (hasCurlyBraces) {
372 if (VarEnd >= AsmString.size())
373 throw "Reached end of string before terminating curly brace in '"
Chris Lattner3e3def92005-07-15 22:43:04 +0000374 + CGI.TheDef->getName() + "'";
Chris Lattner04cadb32006-02-06 23:40:48 +0000375
376 // Look for a modifier string.
377 if (AsmString[VarEnd] == ':') {
378 ++VarEnd;
379 if (VarEnd >= AsmString.size())
380 throw "Reached end of string before terminating curly brace in '"
381 + CGI.TheDef->getName() + "'";
382
383 unsigned ModifierStart = VarEnd;
384 while (VarEnd < AsmString.size() && isIdentChar(AsmString[VarEnd]))
385 ++VarEnd;
386 Modifier = std::string(AsmString.begin()+ModifierStart,
387 AsmString.begin()+VarEnd);
388 if (Modifier.empty())
389 throw "Bad operand modifier name in '"+ CGI.TheDef->getName() + "'";
390 }
391
Nate Begemanafc54562005-07-14 22:50:30 +0000392 if (AsmString[VarEnd] != '}')
Chris Lattnerb03b0802006-02-06 22:43:28 +0000393 throw "Variable name beginning with '{' did not end with '}' in '"
Chris Lattner3e3def92005-07-15 22:43:04 +0000394 + CGI.TheDef->getName() + "'";
Nate Begemanafc54562005-07-14 22:50:30 +0000395 ++VarEnd;
396 }
Chris Lattner1bf63612006-09-26 23:45:08 +0000397 if (VarName.empty() && Modifier.empty())
Jeff Cohen00b168892005-07-27 06:12:32 +0000398 throw "Stray '$' in '" + CGI.TheDef->getName() +
Chris Lattner3e3def92005-07-15 22:43:04 +0000399 "' asm string, maybe you want $$?";
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000400
Chris Lattner1bf63612006-09-26 23:45:08 +0000401 if (VarName.empty()) {
Chris Lattner16f046a2006-09-26 23:47:10 +0000402 // Just a modifier, pass this into PrintSpecial.
403 Operands.push_back(AsmWriterOperand("PrintSpecial", ~0U, Modifier));
Chris Lattner1bf63612006-09-26 23:45:08 +0000404 } else {
405 // Otherwise, normal operand.
406 unsigned OpNo = CGI.getOperandNamed(VarName);
407 CodeGenInstruction::OperandInfo OpInfo = CGI.OperandList[OpNo];
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000408
Chris Lattnerf64f9a42006-11-15 23:23:02 +0000409 if (CurVariant == Variant || CurVariant == ~0U) {
410 unsigned MIOp = OpInfo.MIOperandNo;
Chris Lattner1bf63612006-09-26 23:45:08 +0000411 Operands.push_back(AsmWriterOperand(OpInfo.PrinterMethodName, MIOp,
412 Modifier));
Chris Lattnerf64f9a42006-11-15 23:23:02 +0000413 }
Chris Lattner1bf63612006-09-26 23:45:08 +0000414 }
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000415 LastEmitted = VarEnd;
416 }
417 }
Evan Chengba8dc032009-07-20 06:10:07 +0000418
David Greenec8d06052009-07-29 20:10:24 +0000419 Operands.push_back(
420 AsmWriterOperand("EmitComments(*MI);\n",
421 AsmWriterOperand::isLiteralStatementOperand));
Evan Chengba8dc032009-07-20 06:10:07 +0000422 AddLiteralString("\\n");
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000423}
424
Chris Lattnerf8766682005-01-22 19:22:23 +0000425/// MatchesAllButOneOp - If this instruction is exactly identical to the
426/// specified instruction except for one differing operand, return the differing
427/// operand number. If more than one operand mismatches, return ~1, otherwise
428/// if the instructions are identical return ~0.
429unsigned AsmWriterInst::MatchesAllButOneOp(const AsmWriterInst &Other)const{
430 if (Operands.size() != Other.Operands.size()) return ~1;
Chris Lattner870c0162005-01-22 18:38:13 +0000431
432 unsigned MismatchOperand = ~0U;
433 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +0000434 if (Operands[i] != Other.Operands[i]) {
Chris Lattnerf8766682005-01-22 19:22:23 +0000435 if (MismatchOperand != ~0U) // Already have one mismatch?
436 return ~1U;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000437 else
Chris Lattner870c0162005-01-22 18:38:13 +0000438 MismatchOperand = i;
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +0000439 }
Chris Lattner870c0162005-01-22 18:38:13 +0000440 }
441 return MismatchOperand;
442}
443
Chris Lattner38c07512005-01-22 20:31:17 +0000444static void PrintCases(std::vector<std::pair<std::string,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000445 AsmWriterOperand> > &OpsToPrint, raw_ostream &O) {
Chris Lattner38c07512005-01-22 20:31:17 +0000446 O << " case " << OpsToPrint.back().first << ": ";
447 AsmWriterOperand TheOp = OpsToPrint.back().second;
448 OpsToPrint.pop_back();
449
450 // Check to see if any other operands are identical in this list, and if so,
451 // emit a case label for them.
452 for (unsigned i = OpsToPrint.size(); i != 0; --i)
453 if (OpsToPrint[i-1].second == TheOp) {
454 O << "\n case " << OpsToPrint[i-1].first << ": ";
455 OpsToPrint.erase(OpsToPrint.begin()+i-1);
456 }
457
458 // Finally, emit the code.
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000459 O << TheOp.getCode();
Chris Lattner38c07512005-01-22 20:31:17 +0000460 O << "break;\n";
461}
462
Chris Lattner870c0162005-01-22 18:38:13 +0000463
464/// EmitInstructions - Emit the last instruction in the vector and any other
465/// instructions that are suitably similar to it.
466static void EmitInstructions(std::vector<AsmWriterInst> &Insts,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000467 raw_ostream &O) {
Chris Lattner870c0162005-01-22 18:38:13 +0000468 AsmWriterInst FirstInst = Insts.back();
469 Insts.pop_back();
470
471 std::vector<AsmWriterInst> SimilarInsts;
472 unsigned DifferingOperand = ~0;
473 for (unsigned i = Insts.size(); i != 0; --i) {
Chris Lattnerf8766682005-01-22 19:22:23 +0000474 unsigned DiffOp = Insts[i-1].MatchesAllButOneOp(FirstInst);
475 if (DiffOp != ~1U) {
Chris Lattner870c0162005-01-22 18:38:13 +0000476 if (DifferingOperand == ~0U) // First match!
477 DifferingOperand = DiffOp;
478
479 // If this differs in the same operand as the rest of the instructions in
480 // this class, move it to the SimilarInsts list.
Chris Lattnerf8766682005-01-22 19:22:23 +0000481 if (DifferingOperand == DiffOp || DiffOp == ~0U) {
Chris Lattner870c0162005-01-22 18:38:13 +0000482 SimilarInsts.push_back(Insts[i-1]);
483 Insts.erase(Insts.begin()+i-1);
484 }
485 }
486 }
487
Chris Lattnera1e8a802006-05-01 17:01:17 +0000488 O << " case " << FirstInst.CGI->Namespace << "::"
Chris Lattner870c0162005-01-22 18:38:13 +0000489 << FirstInst.CGI->TheDef->getName() << ":\n";
490 for (unsigned i = 0, e = SimilarInsts.size(); i != e; ++i)
Chris Lattnera1e8a802006-05-01 17:01:17 +0000491 O << " case " << SimilarInsts[i].CGI->Namespace << "::"
Chris Lattner870c0162005-01-22 18:38:13 +0000492 << SimilarInsts[i].CGI->TheDef->getName() << ":\n";
493 for (unsigned i = 0, e = FirstInst.Operands.size(); i != e; ++i) {
494 if (i != DifferingOperand) {
495 // If the operand is the same for all instructions, just print it.
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000496 O << " " << FirstInst.Operands[i].getCode();
Chris Lattner870c0162005-01-22 18:38:13 +0000497 } else {
498 // If this is the operand that varies between all of the instructions,
499 // emit a switch for just this operand now.
500 O << " switch (MI->getOpcode()) {\n";
Chris Lattner38c07512005-01-22 20:31:17 +0000501 std::vector<std::pair<std::string, AsmWriterOperand> > OpsToPrint;
Chris Lattnera1e8a802006-05-01 17:01:17 +0000502 OpsToPrint.push_back(std::make_pair(FirstInst.CGI->Namespace + "::" +
Chris Lattner38c07512005-01-22 20:31:17 +0000503 FirstInst.CGI->TheDef->getName(),
504 FirstInst.Operands[i]));
Misha Brukman3da94ae2005-04-22 00:00:37 +0000505
Chris Lattner870c0162005-01-22 18:38:13 +0000506 for (unsigned si = 0, e = SimilarInsts.size(); si != e; ++si) {
Chris Lattner38c07512005-01-22 20:31:17 +0000507 AsmWriterInst &AWI = SimilarInsts[si];
Chris Lattnera1e8a802006-05-01 17:01:17 +0000508 OpsToPrint.push_back(std::make_pair(AWI.CGI->Namespace+"::"+
Chris Lattner38c07512005-01-22 20:31:17 +0000509 AWI.CGI->TheDef->getName(),
510 AWI.Operands[i]));
Chris Lattner870c0162005-01-22 18:38:13 +0000511 }
Chris Lattner38c07512005-01-22 20:31:17 +0000512 std::reverse(OpsToPrint.begin(), OpsToPrint.end());
513 while (!OpsToPrint.empty())
514 PrintCases(OpsToPrint, O);
Chris Lattner870c0162005-01-22 18:38:13 +0000515 O << " }";
516 }
517 O << "\n";
518 }
Chris Lattner870c0162005-01-22 18:38:13 +0000519 O << " break;\n";
520}
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000521
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000522void AsmWriterEmitter::
523FindUniqueOperandCommands(std::vector<std::string> &UniqueOperandCommands,
Chris Lattner96c1ade2006-07-18 18:28:27 +0000524 std::vector<unsigned> &InstIdxs,
525 std::vector<unsigned> &InstOpsUsed) const {
Chris Lattner195bb4a2006-07-18 19:27:30 +0000526 InstIdxs.assign(NumberedInstructions.size(), ~0U);
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000527
528 // This vector parallels UniqueOperandCommands, keeping track of which
529 // instructions each case are used for. It is a comma separated string of
530 // enums.
531 std::vector<std::string> InstrsForCase;
532 InstrsForCase.resize(UniqueOperandCommands.size());
Chris Lattner96c1ade2006-07-18 18:28:27 +0000533 InstOpsUsed.assign(UniqueOperandCommands.size(), 0);
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000534
535 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
536 const AsmWriterInst *Inst = getAsmWriterInstByID(i);
Dan Gohman44066042008-07-01 00:05:16 +0000537 if (Inst == 0) continue; // PHI, INLINEASM, DBG_LABEL, etc.
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000538
539 std::string Command;
Chris Lattnerb8462862006-07-18 17:56:07 +0000540 if (Inst->Operands.empty())
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000541 continue; // Instruction already done.
Chris Lattner191dd1f2006-07-18 17:50:22 +0000542
Chris Lattnerb8462862006-07-18 17:56:07 +0000543 Command = " " + Inst->Operands[0].getCode() + "\n";
Chris Lattner191dd1f2006-07-18 17:50:22 +0000544
545 // If this is the last operand, emit a return.
David Greenec8d06052009-07-29 20:10:24 +0000546 if (Inst->Operands.size() == 1) {
Chris Lattner191dd1f2006-07-18 17:50:22 +0000547 Command += " return true;\n";
David Greenec8d06052009-07-29 20:10:24 +0000548 }
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000549
550 // Check to see if we already have 'Command' in UniqueOperandCommands.
551 // If not, add it.
552 bool FoundIt = false;
553 for (unsigned idx = 0, e = UniqueOperandCommands.size(); idx != e; ++idx)
554 if (UniqueOperandCommands[idx] == Command) {
555 InstIdxs[i] = idx;
556 InstrsForCase[idx] += ", ";
557 InstrsForCase[idx] += Inst->CGI->TheDef->getName();
558 FoundIt = true;
559 break;
560 }
561 if (!FoundIt) {
562 InstIdxs[i] = UniqueOperandCommands.size();
563 UniqueOperandCommands.push_back(Command);
564 InstrsForCase.push_back(Inst->CGI->TheDef->getName());
Chris Lattner96c1ade2006-07-18 18:28:27 +0000565
566 // This command matches one operand so far.
567 InstOpsUsed.push_back(1);
568 }
569 }
570
571 // For each entry of UniqueOperandCommands, there is a set of instructions
572 // that uses it. If the next command of all instructions in the set are
573 // identical, fold it into the command.
574 for (unsigned CommandIdx = 0, e = UniqueOperandCommands.size();
575 CommandIdx != e; ++CommandIdx) {
576
577 for (unsigned Op = 1; ; ++Op) {
578 // Scan for the first instruction in the set.
579 std::vector<unsigned>::iterator NIT =
580 std::find(InstIdxs.begin(), InstIdxs.end(), CommandIdx);
581 if (NIT == InstIdxs.end()) break; // No commonality.
582
583 // If this instruction has no more operands, we isn't anything to merge
584 // into this command.
585 const AsmWriterInst *FirstInst =
586 getAsmWriterInstByID(NIT-InstIdxs.begin());
587 if (!FirstInst || FirstInst->Operands.size() == Op)
588 break;
589
590 // Otherwise, scan to see if all of the other instructions in this command
591 // set share the operand.
592 bool AllSame = true;
David Greenec8d06052009-07-29 20:10:24 +0000593 // Keep track of the maximum, number of operands or any
594 // instruction we see in the group.
595 size_t MaxSize = FirstInst->Operands.size();
596
Chris Lattner96c1ade2006-07-18 18:28:27 +0000597 for (NIT = std::find(NIT+1, InstIdxs.end(), CommandIdx);
598 NIT != InstIdxs.end();
599 NIT = std::find(NIT+1, InstIdxs.end(), CommandIdx)) {
600 // Okay, found another instruction in this command set. If the operand
601 // matches, we're ok, otherwise bail out.
602 const AsmWriterInst *OtherInst =
603 getAsmWriterInstByID(NIT-InstIdxs.begin());
David Greenec8d06052009-07-29 20:10:24 +0000604
605 if (OtherInst &&
606 OtherInst->Operands.size() > FirstInst->Operands.size())
607 MaxSize = std::max(MaxSize, OtherInst->Operands.size());
608
Chris Lattner96c1ade2006-07-18 18:28:27 +0000609 if (!OtherInst || OtherInst->Operands.size() == Op ||
610 OtherInst->Operands[Op] != FirstInst->Operands[Op]) {
611 AllSame = false;
612 break;
613 }
614 }
615 if (!AllSame) break;
616
617 // Okay, everything in this command set has the same next operand. Add it
618 // to UniqueOperandCommands and remember that it was consumed.
619 std::string Command = " " + FirstInst->Operands[Op].getCode() + "\n";
620
621 // If this is the last operand, emit a return after the code.
David Greenec8d06052009-07-29 20:10:24 +0000622 if (FirstInst->Operands.size() == Op+1 &&
623 // Don't early-out too soon. Other instructions in this
624 // group may have more operands.
625 FirstInst->Operands.size() == MaxSize) {
Chris Lattner96c1ade2006-07-18 18:28:27 +0000626 Command += " return true;\n";
David Greenec8d06052009-07-29 20:10:24 +0000627 }
Chris Lattner96c1ade2006-07-18 18:28:27 +0000628
629 UniqueOperandCommands[CommandIdx] += Command;
630 InstOpsUsed[CommandIdx]++;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000631 }
632 }
633
634 // Prepend some of the instructions each case is used for onto the case val.
635 for (unsigned i = 0, e = InstrsForCase.size(); i != e; ++i) {
636 std::string Instrs = InstrsForCase[i];
637 if (Instrs.size() > 70) {
638 Instrs.erase(Instrs.begin()+70, Instrs.end());
639 Instrs += "...";
640 }
641
642 if (!Instrs.empty())
643 UniqueOperandCommands[i] = " // " + Instrs + "\n" +
644 UniqueOperandCommands[i];
645 }
646}
647
648
649
Daniel Dunbar1a551802009-07-03 00:10:29 +0000650void AsmWriterEmitter::run(raw_ostream &O) {
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000651 EmitSourceFileHeader("Assembly Writer Source Fragment", O);
652
653 CodeGenTarget Target;
Chris Lattner175580c2004-08-14 22:50:53 +0000654 Record *AsmWriter = Target.getAsmWriter();
Chris Lattner953c6fe2004-10-03 20:19:02 +0000655 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
656 unsigned Variant = AsmWriter->getValueAsInt("Variant");
Chris Lattner175580c2004-08-14 22:50:53 +0000657
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000658 O <<
659 "/// printInstruction - This method is automatically generated by tablegen\n"
660 "/// from the instruction set description. This method returns true if the\n"
661 "/// machine instruction was sufficiently described to print it, otherwise\n"
662 "/// it returns false.\n"
Chris Lattner953c6fe2004-10-03 20:19:02 +0000663 "bool " << Target.getName() << ClassName
Chris Lattner175580c2004-08-14 22:50:53 +0000664 << "::printInstruction(const MachineInstr *MI) {\n";
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000665
Chris Lattner5765dba2005-01-22 17:40:38 +0000666 std::vector<AsmWriterInst> Instructions;
667
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000668 for (CodeGenTarget::inst_iterator I = Target.inst_begin(),
669 E = Target.inst_end(); I != E; ++I)
Chris Lattner5765dba2005-01-22 17:40:38 +0000670 if (!I->second.AsmString.empty())
671 Instructions.push_back(AsmWriterInst(I->second, Variant));
Chris Lattner076efa72004-08-01 07:43:02 +0000672
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000673 // Get the instruction numbering.
Chris Lattner0cfcc1e2006-01-27 02:10:50 +0000674 Target.getInstructionsByEnumValue(NumberedInstructions);
675
Chris Lattner6af022f2006-07-14 22:59:11 +0000676 // Compute the CodeGenInstruction -> AsmWriterInst mapping. Note that not
677 // all machine instructions are necessarily being printed, so there may be
678 // target instructions not in this map.
Chris Lattner6af022f2006-07-14 22:59:11 +0000679 for (unsigned i = 0, e = Instructions.size(); i != e; ++i)
680 CGIAWIMap.insert(std::make_pair(Instructions[i].CGI, &Instructions[i]));
Chris Lattnerf8766682005-01-22 19:22:23 +0000681
Chris Lattner6af022f2006-07-14 22:59:11 +0000682 // Build an aggregate string, and build a table of offsets into it.
683 std::map<std::string, unsigned> StringOffset;
684 std::string AggregateString;
Chris Lattner259bda42006-09-27 16:44:09 +0000685 AggregateString.push_back(0); // "\0"
686 AggregateString.push_back(0); // "\0"
Chris Lattner6af022f2006-07-14 22:59:11 +0000687
Chris Lattner259bda42006-09-27 16:44:09 +0000688 /// OpcodeInfo - This encodes the index of the string to use for the first
Chris Lattner55616402006-07-18 17:32:27 +0000689 /// chunk of the output as well as indices used for operand printing.
690 std::vector<unsigned> OpcodeInfo;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000691
Chris Lattner55616402006-07-18 17:32:27 +0000692 unsigned MaxStringIdx = 0;
Chris Lattner6af022f2006-07-14 22:59:11 +0000693 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
694 AsmWriterInst *AWI = CGIAWIMap[NumberedInstructions[i]];
695 unsigned Idx;
Chris Lattnera6dc9fb2006-07-19 01:39:06 +0000696 if (AWI == 0) {
Chris Lattner6af022f2006-07-14 22:59:11 +0000697 // Something not handled by the asmwriter printer.
698 Idx = 0;
Chris Lattnera6dc9fb2006-07-19 01:39:06 +0000699 } else if (AWI->Operands[0].OperandType !=
700 AsmWriterOperand::isLiteralTextOperand ||
701 AWI->Operands[0].Str.empty()) {
702 // Something handled by the asmwriter printer, but with no leading string.
703 Idx = 1;
Chris Lattner6af022f2006-07-14 22:59:11 +0000704 } else {
705 unsigned &Entry = StringOffset[AWI->Operands[0].Str];
706 if (Entry == 0) {
707 // Add the string to the aggregate if this is the first time found.
Chris Lattner55616402006-07-18 17:32:27 +0000708 MaxStringIdx = Entry = AggregateString.size();
Chris Lattner6af022f2006-07-14 22:59:11 +0000709 std::string Str = AWI->Operands[0].Str;
710 UnescapeString(Str);
711 AggregateString += Str;
712 AggregateString += '\0';
Chris Lattnerf8766682005-01-22 19:22:23 +0000713 }
Chris Lattner6af022f2006-07-14 22:59:11 +0000714 Idx = Entry;
Chris Lattner6af022f2006-07-14 22:59:11 +0000715
716 // Nuke the string from the operand list. It is now handled!
717 AWI->Operands.erase(AWI->Operands.begin());
Chris Lattnerf8766682005-01-22 19:22:23 +0000718 }
Chris Lattner55616402006-07-18 17:32:27 +0000719 OpcodeInfo.push_back(Idx);
Chris Lattnerf8766682005-01-22 19:22:23 +0000720 }
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000721
Chris Lattner55616402006-07-18 17:32:27 +0000722 // Figure out how many bits we used for the string index.
Nate Begeman59d28132008-04-09 16:24:11 +0000723 unsigned AsmStrBits = Log2_32_Ceil(MaxStringIdx+1);
Chris Lattner55616402006-07-18 17:32:27 +0000724
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000725 // To reduce code size, we compactify common instructions into a few bits
726 // in the opcode-indexed table.
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000727 unsigned BitsLeft = 32-AsmStrBits;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000728
729 std::vector<std::vector<std::string> > TableDrivenOperandPrinters;
730
Chris Lattnerb8462862006-07-18 17:56:07 +0000731 bool isFirst = true;
732 while (1) {
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000733 std::vector<std::string> UniqueOperandCommands;
734
Chris Lattnera6dc9fb2006-07-19 01:39:06 +0000735 // For the first operand check, add a default value for instructions with
736 // just opcode strings to use.
Chris Lattnerb8462862006-07-18 17:56:07 +0000737 if (isFirst) {
Evan Chengba8dc032009-07-20 06:10:07 +0000738 UniqueOperandCommands.push_back(" return true;\n");
Chris Lattnerb8462862006-07-18 17:56:07 +0000739 isFirst = false;
740 }
David Greenec8d06052009-07-29 20:10:24 +0000741
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000742 std::vector<unsigned> InstIdxs;
Chris Lattner96c1ade2006-07-18 18:28:27 +0000743 std::vector<unsigned> NumInstOpsHandled;
744 FindUniqueOperandCommands(UniqueOperandCommands, InstIdxs,
745 NumInstOpsHandled);
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000746
747 // If we ran out of operands to print, we're done.
748 if (UniqueOperandCommands.empty()) break;
749
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000750 // Compute the number of bits we need to represent these cases, this is
751 // ceil(log2(numentries)).
752 unsigned NumBits = Log2_32_Ceil(UniqueOperandCommands.size());
753
754 // If we don't have enough bits for this operand, don't include it.
755 if (NumBits > BitsLeft) {
Bill Wendlingf5da1332006-12-07 22:21:48 +0000756 DOUT << "Not enough bits to densely encode " << NumBits
757 << " more bits\n";
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000758 break;
759 }
760
761 // Otherwise, we can include this in the initial lookup table. Add it in.
762 BitsLeft -= NumBits;
763 for (unsigned i = 0, e = InstIdxs.size(); i != e; ++i)
Chris Lattner195bb4a2006-07-18 19:27:30 +0000764 if (InstIdxs[i] != ~0U)
765 OpcodeInfo[i] |= InstIdxs[i] << (BitsLeft+AsmStrBits);
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000766
Chris Lattnerb8462862006-07-18 17:56:07 +0000767 // Remove the info about this operand.
768 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
769 if (AsmWriterInst *Inst = getAsmWriterInstByID(i))
Chris Lattner96c1ade2006-07-18 18:28:27 +0000770 if (!Inst->Operands.empty()) {
771 unsigned NumOps = NumInstOpsHandled[InstIdxs[i]];
Chris Lattner0a012122006-07-18 19:06:01 +0000772 assert(NumOps <= Inst->Operands.size() &&
773 "Can't remove this many ops!");
Chris Lattner96c1ade2006-07-18 18:28:27 +0000774 Inst->Operands.erase(Inst->Operands.begin(),
775 Inst->Operands.begin()+NumOps);
776 }
Chris Lattnerb8462862006-07-18 17:56:07 +0000777 }
778
779 // Remember the handlers for this set of operands.
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000780 TableDrivenOperandPrinters.push_back(UniqueOperandCommands);
781 }
782
783
784
Chris Lattner55616402006-07-18 17:32:27 +0000785 O<<" static const unsigned OpInfo[] = {\n";
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000786 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000787 O << " " << OpcodeInfo[i] << "U,\t// "
Chris Lattner55616402006-07-18 17:32:27 +0000788 << NumberedInstructions[i]->TheDef->getName() << "\n";
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000789 }
790 // Add a dummy entry so the array init doesn't end with a comma.
Chris Lattner55616402006-07-18 17:32:27 +0000791 O << " 0U\n";
Chris Lattner6af022f2006-07-14 22:59:11 +0000792 O << " };\n\n";
793
794 // Emit the string itself.
795 O << " const char *AsmStrs = \n \"";
796 unsigned CharsPrinted = 0;
797 EscapeString(AggregateString);
798 for (unsigned i = 0, e = AggregateString.size(); i != e; ++i) {
799 if (CharsPrinted > 70) {
800 O << "\"\n \"";
801 CharsPrinted = 0;
802 }
803 O << AggregateString[i];
804 ++CharsPrinted;
805
806 // Print escape sequences all together.
807 if (AggregateString[i] == '\\') {
808 assert(i+1 < AggregateString.size() && "Incomplete escape sequence!");
809 if (isdigit(AggregateString[i+1])) {
810 assert(isdigit(AggregateString[i+2]) && isdigit(AggregateString[i+3]) &&
811 "Expected 3 digit octal escape!");
812 O << AggregateString[++i];
813 O << AggregateString[++i];
814 O << AggregateString[++i];
815 CharsPrinted += 3;
816 } else {
817 O << AggregateString[++i];
818 ++CharsPrinted;
819 }
820 }
821 }
822 O << "\";\n\n";
823
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +0000824 O << " processDebugLoc(MI->getDebugLoc());\n\n";
Bill Wendlingcb819f12009-02-18 23:12:06 +0000825
Chris Lattner5b842c32009-06-19 23:57:53 +0000826 O << "\n#ifndef NO_ASM_WRITER_BOILERPLATE\n";
827
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000828 O << " if (MI->getOpcode() == TargetInstrInfo::INLINEASM) {\n"
Evan Cheng4eecdeb2008-02-02 08:39:46 +0000829 << " O << \"\\t\";\n"
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000830 << " printInlineAsm(MI);\n"
831 << " return true;\n"
Dan Gohman44066042008-07-01 00:05:16 +0000832 << " } else if (MI->isLabel()) {\n"
Jim Laskeya683f9b2007-01-26 17:29:20 +0000833 << " printLabel(MI);\n"
834 << " return true;\n"
Evan Chenga844bde2008-02-02 04:07:54 +0000835 << " } else if (MI->getOpcode() == TargetInstrInfo::DECLARE) {\n"
836 << " printDeclare(MI);\n"
837 << " return true;\n"
Evan Chengda47e6e2008-03-15 00:03:38 +0000838 << " } else if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {\n"
839 << " printImplicitDef(MI);\n"
840 << " return true;\n"
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000841 << " }\n\n";
Chris Lattner5b842c32009-06-19 23:57:53 +0000842
843 O << "\n#endif\n";
844
Evan Cheng4eecdeb2008-02-02 08:39:46 +0000845 O << " O << \"\\t\";\n\n";
846
Chris Lattner6af022f2006-07-14 22:59:11 +0000847 O << " // Emit the opcode for the instruction.\n"
Chris Lattner55616402006-07-18 17:32:27 +0000848 << " unsigned Bits = OpInfo[MI->getOpcode()];\n"
David Greenec8d06052009-07-29 20:10:24 +0000849 << " if (Bits == 0) return false;\n\n";
850
851 O << " unsigned OperandColumn = 1;\n\n"
852 << " if (TAI->getOperandColumn(1) > 0) {\n"
853 << " // Don't emit trailing whitespace, let the column padding do it. This\n"
854 << " // guarantees that a stray long opcode + tab won't upset the alignment.\n"
855 << " unsigned OpLength = std::strlen(AsmStrs+(Bits & " << (1 << AsmStrBits)-1 << "));\n"
856 << " if (OpLength > 0 &&\n"
857 << " ((AsmStrs+(Bits & " << (1 << AsmStrBits)-1 << "))[OpLength-1] == ' ' ||\n"
858 << " (AsmStrs+(Bits & " << (1 << AsmStrBits)-1 << "))[OpLength-1] == '\\t')) {\n"
859 << " do {\n"
860 << " --OpLength;\n"
861 << " } while ((AsmStrs+(Bits & " << (1 << AsmStrBits)-1 << "))[OpLength-1] == ' ' ||\n"
862 << " (AsmStrs+(Bits & " << (1 << AsmStrBits)-1 << "))[OpLength-1] == '\\t');\n"
863 << " for (unsigned Idx = 0; Idx < OpLength; ++Idx)\n"
864 << " O << (AsmStrs+(Bits & " << (1 << AsmStrBits)-1 << "))[Idx];\n"
865 << " O.PadToColumn(TAI->getOperandColumn(OperandColumn++), 1);\n"
866 << " }\n"
867 << " } else {\n"
868 << " O << AsmStrs+(Bits & " << (1 << AsmStrBits)-1 << ");\n"
869 << " }\n\n";
870
Chris Lattnerf8766682005-01-22 19:22:23 +0000871
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000872 // Output the table driven operand information.
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000873 BitsLeft = 32-AsmStrBits;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000874 for (unsigned i = 0, e = TableDrivenOperandPrinters.size(); i != e; ++i) {
875 std::vector<std::string> &Commands = TableDrivenOperandPrinters[i];
876
877 // Compute the number of bits we need to represent these cases, this is
878 // ceil(log2(numentries)).
879 unsigned NumBits = Log2_32_Ceil(Commands.size());
880 assert(NumBits <= BitsLeft && "consistency error");
881
882 // Emit code to extract this field from Bits.
883 BitsLeft -= NumBits;
884
885 O << "\n // Fragment " << i << " encoded into " << NumBits
Chris Lattnere7a589d2006-07-18 17:43:54 +0000886 << " bits for " << Commands.size() << " unique commands.\n";
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000887
Chris Lattner96c1ade2006-07-18 18:28:27 +0000888 if (Commands.size() == 2) {
Chris Lattnere7a589d2006-07-18 17:43:54 +0000889 // Emit two possibilitys with if/else.
890 O << " if ((Bits >> " << (BitsLeft+AsmStrBits) << ") & "
891 << ((1 << NumBits)-1) << ") {\n"
892 << Commands[1]
893 << " } else {\n"
894 << Commands[0]
895 << " }\n\n";
896 } else {
897 O << " switch ((Bits >> " << (BitsLeft+AsmStrBits) << ") & "
898 << ((1 << NumBits)-1) << ") {\n"
899 << " default: // unreachable.\n";
900
901 // Print out all the cases.
902 for (unsigned i = 0, e = Commands.size(); i != e; ++i) {
903 O << " case " << i << ":\n";
904 O << Commands[i];
905 O << " break;\n";
906 }
907 O << " }\n\n";
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000908 }
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000909 }
910
Chris Lattnerb8462862006-07-18 17:56:07 +0000911 // Okay, delete instructions with no operand info left.
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000912 for (unsigned i = 0, e = Instructions.size(); i != e; ++i) {
913 // Entire instruction has been emitted?
914 AsmWriterInst &Inst = Instructions[i];
Chris Lattnerb8462862006-07-18 17:56:07 +0000915 if (Inst.Operands.empty()) {
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000916 Instructions.erase(Instructions.begin()+i);
Chris Lattnerb8462862006-07-18 17:56:07 +0000917 --i; --e;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000918 }
919 }
920
921
922 // Because this is a vector, we want to emit from the end. Reverse all of the
Chris Lattner870c0162005-01-22 18:38:13 +0000923 // elements in the vector.
924 std::reverse(Instructions.begin(), Instructions.end());
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000925
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000926 if (!Instructions.empty()) {
927 // Find the opcode # of inline asm.
928 O << " switch (MI->getOpcode()) {\n";
929 while (!Instructions.empty())
930 EmitInstructions(Instructions, O);
Chris Lattner870c0162005-01-22 18:38:13 +0000931
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000932 O << " }\n";
Evan Cheng3837b642009-07-18 01:43:53 +0000933 O << " return true;\n";
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000934 }
David Greenec8d06052009-07-29 20:10:24 +0000935
936 O << " return true;\n";
Chris Lattner0a012122006-07-18 19:06:01 +0000937 O << "}\n";
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000938}