blob: 8f8f89a79b0bb51dd715d7d2052474d2dda9eefe [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 Lattner44da5fb2009-09-14 01:19:16 +000018#include "StringToOffsetTable.h"
Chris Lattner6af022f2006-07-14 22:59:11 +000019#include "llvm/ADT/StringExtras.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
Chris Lattner076efa72004-08-01 07:43:02 +000026static bool isIdentChar(char C) {
27 return (C >= 'a' && C <= 'z') ||
28 (C >= 'A' && C <= 'Z') ||
29 (C >= '0' && C <= '9') ||
30 C == '_';
31}
32
Chris Lattnerad8c5312007-07-18 04:51:57 +000033// This should be an anon namespace, this works around a GCC warning.
34namespace llvm {
Chris Lattnerb0b55e72005-01-22 17:32:42 +000035 struct AsmWriterOperand {
David Greenec8d06052009-07-29 20:10:24 +000036 enum OpType {
David Greenebef87682009-07-31 21:57:10 +000037 // Output this text surrounded by quotes to the asm.
David Greenec8d06052009-07-29 20:10:24 +000038 isLiteralTextOperand,
David Greenebef87682009-07-31 21:57:10 +000039 // This is the name of a routine to call to print the operand.
David Greenec8d06052009-07-29 20:10:24 +000040 isMachineInstrOperand,
David Greenebef87682009-07-31 21:57:10 +000041 // Output this text verbatim to the asm writer. It is code that
42 // will output some text to the asm.
David Greenec8d06052009-07-29 20:10:24 +000043 isLiteralStatementOperand
44 } OperandType;
Chris Lattnerb0b55e72005-01-22 17:32:42 +000045
46 /// Str - For isLiteralTextOperand, this IS the literal text. For
David Greenebef87682009-07-31 21:57:10 +000047 /// isMachineInstrOperand, this is the PrinterMethodName for the operand..
48 /// For isLiteralStatementOperand, this is the code to insert verbatim
49 /// into the asm writer.
Chris Lattnerb0b55e72005-01-22 17:32:42 +000050 std::string Str;
51
52 /// MiOpNo - For isMachineInstrOperand, this is the operand number of the
53 /// machine instruction.
54 unsigned MIOpNo;
Chris Lattner04cadb32006-02-06 23:40:48 +000055
56 /// MiModifier - For isMachineInstrOperand, this is the modifier string for
57 /// an operand, specified with syntax like ${opname:modifier}.
58 std::string MiModifier;
Chris Lattnerb0b55e72005-01-22 17:32:42 +000059
Cedric Venet7caa2d02008-10-27 19:21:35 +000060 // To make VS STL happy
David Greenec8d06052009-07-29 20:10:24 +000061 AsmWriterOperand(OpType op = isLiteralTextOperand):OperandType(op) {}
Cedric Venet3bff2df2008-10-26 15:40:44 +000062
David Greenec8d06052009-07-29 20:10:24 +000063 AsmWriterOperand(const std::string &LitStr,
64 OpType op = isLiteralTextOperand)
65 : OperandType(op), Str(LitStr) {}
Chris Lattnerb0b55e72005-01-22 17:32:42 +000066
Chris Lattner04cadb32006-02-06 23:40:48 +000067 AsmWriterOperand(const std::string &Printer, unsigned OpNo,
David Greenec8d06052009-07-29 20:10:24 +000068 const std::string &Modifier,
69 OpType op = isMachineInstrOperand)
70 : OperandType(op), Str(Printer), MIOpNo(OpNo),
Chris Lattner04cadb32006-02-06 23:40:48 +000071 MiModifier(Modifier) {}
Chris Lattnerb0b55e72005-01-22 17:32:42 +000072
Chris Lattner870c0162005-01-22 18:38:13 +000073 bool operator!=(const AsmWriterOperand &Other) const {
74 if (OperandType != Other.OperandType || Str != Other.Str) return true;
75 if (OperandType == isMachineInstrOperand)
Chris Lattner04cadb32006-02-06 23:40:48 +000076 return MIOpNo != Other.MIOpNo || MiModifier != Other.MiModifier;
Chris Lattner870c0162005-01-22 18:38:13 +000077 return false;
78 }
Chris Lattner38c07512005-01-22 20:31:17 +000079 bool operator==(const AsmWriterOperand &Other) const {
80 return !operator!=(Other);
81 }
Chris Lattnerbdff5f92006-07-18 17:18:03 +000082
83 /// getCode - Return the code that prints this operand.
84 std::string getCode() const;
Chris Lattnerb0b55e72005-01-22 17:32:42 +000085 };
Chris Lattnerbdff5f92006-07-18 17:18:03 +000086}
Chris Lattnerb0b55e72005-01-22 17:32:42 +000087
Chris Lattnerbdff5f92006-07-18 17:18:03 +000088namespace llvm {
Jeff Cohend41b30d2006-11-05 19:31:28 +000089 class AsmWriterInst {
90 public:
Chris Lattnerb0b55e72005-01-22 17:32:42 +000091 std::vector<AsmWriterOperand> Operands;
Chris Lattner5765dba2005-01-22 17:40:38 +000092 const CodeGenInstruction *CGI;
Misha Brukman3da94ae2005-04-22 00:00:37 +000093
Chris Lattner59e86772009-08-07 23:13:38 +000094 AsmWriterInst(const CodeGenInstruction &CGI, Record *AsmWriter);
Chris Lattner870c0162005-01-22 18:38:13 +000095
Chris Lattnerf8766682005-01-22 19:22:23 +000096 /// MatchesAllButOneOp - If this instruction is exactly identical to the
97 /// specified instruction except for one differing operand, return the
98 /// differing operand number. Otherwise return ~0.
99 unsigned MatchesAllButOneOp(const AsmWriterInst &Other) const;
Chris Lattner870c0162005-01-22 18:38:13 +0000100
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000101 private:
102 void AddLiteralString(const std::string &Str) {
103 // If the last operand was already a literal text string, append this to
104 // it, otherwise add a new operand.
105 if (!Operands.empty() &&
106 Operands.back().OperandType == AsmWriterOperand::isLiteralTextOperand)
107 Operands.back().Str.append(Str);
108 else
109 Operands.push_back(AsmWriterOperand(Str));
110 }
111 };
112}
113
114
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000115std::string AsmWriterOperand::getCode() const {
Chris Lattner2698cb62009-08-08 00:05:42 +0000116 if (OperandType == isLiteralTextOperand) {
117 if (Str.size() == 1)
118 return "O << '" + Str + "'; ";
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000119 return "O << \"" + Str + "\"; ";
David Greenec8d06052009-07-29 20:10:24 +0000120 }
121
Chris Lattner2698cb62009-08-08 00:05:42 +0000122 if (OperandType == isLiteralStatementOperand)
123 return Str;
124
Chris Lattner1bf63612006-09-26 23:45:08 +0000125 std::string Result = Str + "(MI";
126 if (MIOpNo != ~0U)
127 Result += ", " + utostr(MIOpNo);
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000128 if (!MiModifier.empty())
129 Result += ", \"" + MiModifier + '"';
130 return Result + "); ";
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000131}
132
133
134/// ParseAsmString - Parse the specified Instruction's AsmString into this
135/// AsmWriterInst.
136///
Chris Lattner59e86772009-08-07 23:13:38 +0000137AsmWriterInst::AsmWriterInst(const CodeGenInstruction &CGI, Record *AsmWriter) {
Chris Lattner5765dba2005-01-22 17:40:38 +0000138 this->CGI = &CGI;
Chris Lattner59e86772009-08-07 23:13:38 +0000139
140 unsigned Variant = AsmWriter->getValueAsInt("Variant");
141 int FirstOperandColumn = AsmWriter->getValueAsInt("FirstOperandColumn");
142 int OperandSpacing = AsmWriter->getValueAsInt("OperandSpacing");
143
Chris Lattnerb03b0802006-02-06 22:43:28 +0000144 unsigned CurVariant = ~0U; // ~0 if we are outside a {.|.|.} region, other #.
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000145
Chris Lattner59e86772009-08-07 23:13:38 +0000146 // This is the number of tabs we've seen if we're doing columnar layout.
147 unsigned CurColumn = 0;
148
149
Chris Lattner1cf9d962006-02-01 19:12:23 +0000150 // NOTE: Any extensions to this code need to be mirrored in the
151 // AsmPrinter::printInlineAsm code that executes as compile time (assuming
152 // that inline asm strings should also get the new feature)!
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000153 const std::string &AsmString = CGI.AsmString;
154 std::string::size_type LastEmitted = 0;
155 while (LastEmitted != AsmString.size()) {
156 std::string::size_type DollarPos =
Nate Begeman817affc2008-03-17 07:26:14 +0000157 AsmString.find_first_of("${|}\\", LastEmitted);
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000158 if (DollarPos == std::string::npos) DollarPos = AsmString.size();
159
160 // Emit a constant string fragment.
David Greenec8d06052009-07-29 20:10:24 +0000161
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000162 if (DollarPos != LastEmitted) {
Chris Lattner7f3b28a2009-03-13 21:33:17 +0000163 if (CurVariant == Variant || CurVariant == ~0U) {
164 for (; LastEmitted != DollarPos; ++LastEmitted)
165 switch (AsmString[LastEmitted]) {
David Greenec8d06052009-07-29 20:10:24 +0000166 case '\n':
David Greenec8d06052009-07-29 20:10:24 +0000167 AddLiteralString("\\n");
168 break;
Chris Lattner59e86772009-08-07 23:13:38 +0000169 case '\t':
170 // If the asm writer is not using a columnar layout, \t is not
171 // magic.
172 if (FirstOperandColumn == -1 || OperandSpacing == -1) {
173 AddLiteralString("\\t");
Benjamin Kramerfadf1312009-08-07 23:37:47 +0000174 } else {
175 // We recognize a tab as an operand delimeter.
176 unsigned DestColumn = FirstOperandColumn +
177 CurColumn++ * OperandSpacing;
178 Operands.push_back(
179 AsmWriterOperand("O.PadToColumn(" +
Chris Lattner8f4b1ec2009-08-17 15:48:08 +0000180 utostr(DestColumn) + ");\n",
Benjamin Kramerfadf1312009-08-07 23:37:47 +0000181 AsmWriterOperand::isLiteralStatementOperand));
Chris Lattner59e86772009-08-07 23:13:38 +0000182 }
David Greenec8d06052009-07-29 20:10:24 +0000183 break;
184 case '"':
David Greenec8d06052009-07-29 20:10:24 +0000185 AddLiteralString("\\\"");
186 break;
187 case '\\':
David Greenec8d06052009-07-29 20:10:24 +0000188 AddLiteralString("\\\\");
189 break;
Chris Lattner7f3b28a2009-03-13 21:33:17 +0000190 default:
191 AddLiteralString(std::string(1, AsmString[LastEmitted]));
192 break;
193 }
194 } else {
195 LastEmitted = DollarPos;
196 }
Nate Begeman817affc2008-03-17 07:26:14 +0000197 } else if (AsmString[DollarPos] == '\\') {
198 if (DollarPos+1 != AsmString.size() &&
199 (CurVariant == Variant || CurVariant == ~0U)) {
200 if (AsmString[DollarPos+1] == 'n') {
201 AddLiteralString("\\n");
202 } else if (AsmString[DollarPos+1] == 't') {
Chris Lattner59e86772009-08-07 23:13:38 +0000203 // If the asm writer is not using a columnar layout, \t is not
204 // magic.
205 if (FirstOperandColumn == -1 || OperandSpacing == -1) {
206 AddLiteralString("\\t");
207 break;
208 }
209
210 // We recognize a tab as an operand delimeter.
211 unsigned DestColumn = FirstOperandColumn +
212 CurColumn++ * OperandSpacing;
David Greenebef87682009-07-31 21:57:10 +0000213 Operands.push_back(
Chris Lattner8f4b1ec2009-08-17 15:48:08 +0000214 AsmWriterOperand("O.PadToColumn(" + utostr(DestColumn) + ");\n",
David Greenebef87682009-07-31 21:57:10 +0000215 AsmWriterOperand::isLiteralStatementOperand));
Chris Lattner59e86772009-08-07 23:13:38 +0000216 break;
Nate Begeman817affc2008-03-17 07:26:14 +0000217 } else if (std::string("${|}\\").find(AsmString[DollarPos+1])
218 != std::string::npos) {
219 AddLiteralString(std::string(1, AsmString[DollarPos+1]));
220 } else {
221 throw "Non-supported escaped character found in instruction '" +
222 CGI.TheDef->getName() + "'!";
223 }
224 LastEmitted = DollarPos+2;
225 continue;
226 }
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000227 } else if (AsmString[DollarPos] == '{') {
Chris Lattnerb03b0802006-02-06 22:43:28 +0000228 if (CurVariant != ~0U)
Jeff Cohen00b168892005-07-27 06:12:32 +0000229 throw "Nested variants found for instruction '" +
Chris Lattner3e3def92005-07-15 22:43:04 +0000230 CGI.TheDef->getName() + "'!";
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000231 LastEmitted = DollarPos+1;
Chris Lattnerb03b0802006-02-06 22:43:28 +0000232 CurVariant = 0; // We are now inside of the variant!
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000233 } else if (AsmString[DollarPos] == '|') {
Chris Lattnerb03b0802006-02-06 22:43:28 +0000234 if (CurVariant == ~0U)
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000235 throw "'|' character found outside of a variant in instruction '"
Chris Lattner3e3def92005-07-15 22:43:04 +0000236 + CGI.TheDef->getName() + "'!";
Chris Lattnerb03b0802006-02-06 22:43:28 +0000237 ++CurVariant;
238 ++LastEmitted;
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000239 } else if (AsmString[DollarPos] == '}') {
Chris Lattnerb03b0802006-02-06 22:43:28 +0000240 if (CurVariant == ~0U)
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000241 throw "'}' character found outside of a variant in instruction '"
Chris Lattner3e3def92005-07-15 22:43:04 +0000242 + CGI.TheDef->getName() + "'!";
Chris Lattnerb03b0802006-02-06 22:43:28 +0000243 ++LastEmitted;
244 CurVariant = ~0U;
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000245 } else if (DollarPos+1 != AsmString.size() &&
246 AsmString[DollarPos+1] == '$') {
David Greenec8d06052009-07-29 20:10:24 +0000247 if (CurVariant == Variant || CurVariant == ~0U) {
Chris Lattnerb03b0802006-02-06 22:43:28 +0000248 AddLiteralString("$"); // "$$" -> $
David Greenec8d06052009-07-29 20:10:24 +0000249 }
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000250 LastEmitted = DollarPos+2;
251 } else {
252 // Get the name of the variable.
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000253 std::string::size_type VarEnd = DollarPos+1;
David Greenec8d06052009-07-29 20:10:24 +0000254
Nate Begemanafc54562005-07-14 22:50:30 +0000255 // handle ${foo}bar as $foo by detecting whether the character following
256 // the dollar sign is a curly brace. If so, advance VarEnd and DollarPos
257 // so the variable name does not contain the leading curly brace.
258 bool hasCurlyBraces = false;
259 if (VarEnd < AsmString.size() && '{' == AsmString[VarEnd]) {
260 hasCurlyBraces = true;
261 ++DollarPos;
262 ++VarEnd;
263 }
264
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000265 while (VarEnd < AsmString.size() && isIdentChar(AsmString[VarEnd]))
266 ++VarEnd;
267 std::string VarName(AsmString.begin()+DollarPos+1,
268 AsmString.begin()+VarEnd);
Nate Begemanafc54562005-07-14 22:50:30 +0000269
Chris Lattner04cadb32006-02-06 23:40:48 +0000270 // Modifier - Support ${foo:modifier} syntax, where "modifier" is passed
Chris Lattner1bf63612006-09-26 23:45:08 +0000271 // into printOperand. Also support ${:feature}, which is passed into
Chris Lattner16f046a2006-09-26 23:47:10 +0000272 // PrintSpecial.
Chris Lattner04cadb32006-02-06 23:40:48 +0000273 std::string Modifier;
274
Nate Begemanafc54562005-07-14 22:50:30 +0000275 // In order to avoid starting the next string at the terminating curly
276 // brace, advance the end position past it if we found an opening curly
277 // brace.
278 if (hasCurlyBraces) {
279 if (VarEnd >= AsmString.size())
280 throw "Reached end of string before terminating curly brace in '"
Chris Lattner3e3def92005-07-15 22:43:04 +0000281 + CGI.TheDef->getName() + "'";
Chris Lattner04cadb32006-02-06 23:40:48 +0000282
283 // Look for a modifier string.
284 if (AsmString[VarEnd] == ':') {
285 ++VarEnd;
286 if (VarEnd >= AsmString.size())
287 throw "Reached end of string before terminating curly brace in '"
288 + CGI.TheDef->getName() + "'";
289
290 unsigned ModifierStart = VarEnd;
291 while (VarEnd < AsmString.size() && isIdentChar(AsmString[VarEnd]))
292 ++VarEnd;
293 Modifier = std::string(AsmString.begin()+ModifierStart,
294 AsmString.begin()+VarEnd);
295 if (Modifier.empty())
296 throw "Bad operand modifier name in '"+ CGI.TheDef->getName() + "'";
297 }
298
Nate Begemanafc54562005-07-14 22:50:30 +0000299 if (AsmString[VarEnd] != '}')
Chris Lattnerb03b0802006-02-06 22:43:28 +0000300 throw "Variable name beginning with '{' did not end with '}' in '"
Chris Lattner3e3def92005-07-15 22:43:04 +0000301 + CGI.TheDef->getName() + "'";
Nate Begemanafc54562005-07-14 22:50:30 +0000302 ++VarEnd;
303 }
Chris Lattner1bf63612006-09-26 23:45:08 +0000304 if (VarName.empty() && Modifier.empty())
Jeff Cohen00b168892005-07-27 06:12:32 +0000305 throw "Stray '$' in '" + CGI.TheDef->getName() +
Chris Lattner3e3def92005-07-15 22:43:04 +0000306 "' asm string, maybe you want $$?";
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000307
Chris Lattner1bf63612006-09-26 23:45:08 +0000308 if (VarName.empty()) {
Chris Lattner16f046a2006-09-26 23:47:10 +0000309 // Just a modifier, pass this into PrintSpecial.
310 Operands.push_back(AsmWriterOperand("PrintSpecial", ~0U, Modifier));
Chris Lattner1bf63612006-09-26 23:45:08 +0000311 } else {
312 // Otherwise, normal operand.
313 unsigned OpNo = CGI.getOperandNamed(VarName);
314 CodeGenInstruction::OperandInfo OpInfo = CGI.OperandList[OpNo];
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000315
Chris Lattnerf64f9a42006-11-15 23:23:02 +0000316 if (CurVariant == Variant || CurVariant == ~0U) {
317 unsigned MIOp = OpInfo.MIOperandNo;
Chris Lattner1bf63612006-09-26 23:45:08 +0000318 Operands.push_back(AsmWriterOperand(OpInfo.PrinterMethodName, MIOp,
319 Modifier));
Chris Lattnerf64f9a42006-11-15 23:23:02 +0000320 }
Chris Lattner1bf63612006-09-26 23:45:08 +0000321 }
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000322 LastEmitted = VarEnd;
323 }
324 }
Chris Lattner28179db2009-09-09 23:09:29 +0000325
Chris Lattner28179db2009-09-09 23:09:29 +0000326 Operands.push_back(AsmWriterOperand("return;",
327 AsmWriterOperand::isLiteralStatementOperand));
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000328}
329
Chris Lattnerf8766682005-01-22 19:22:23 +0000330/// MatchesAllButOneOp - If this instruction is exactly identical to the
331/// specified instruction except for one differing operand, return the differing
332/// operand number. If more than one operand mismatches, return ~1, otherwise
333/// if the instructions are identical return ~0.
334unsigned AsmWriterInst::MatchesAllButOneOp(const AsmWriterInst &Other)const{
335 if (Operands.size() != Other.Operands.size()) return ~1;
Chris Lattner870c0162005-01-22 18:38:13 +0000336
337 unsigned MismatchOperand = ~0U;
338 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +0000339 if (Operands[i] != Other.Operands[i]) {
Chris Lattnerf8766682005-01-22 19:22:23 +0000340 if (MismatchOperand != ~0U) // Already have one mismatch?
341 return ~1U;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000342 else
Chris Lattner870c0162005-01-22 18:38:13 +0000343 MismatchOperand = i;
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +0000344 }
Chris Lattner870c0162005-01-22 18:38:13 +0000345 }
346 return MismatchOperand;
347}
348
Chris Lattner38c07512005-01-22 20:31:17 +0000349static void PrintCases(std::vector<std::pair<std::string,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000350 AsmWriterOperand> > &OpsToPrint, raw_ostream &O) {
Chris Lattner38c07512005-01-22 20:31:17 +0000351 O << " case " << OpsToPrint.back().first << ": ";
352 AsmWriterOperand TheOp = OpsToPrint.back().second;
353 OpsToPrint.pop_back();
354
355 // Check to see if any other operands are identical in this list, and if so,
356 // emit a case label for them.
357 for (unsigned i = OpsToPrint.size(); i != 0; --i)
358 if (OpsToPrint[i-1].second == TheOp) {
359 O << "\n case " << OpsToPrint[i-1].first << ": ";
360 OpsToPrint.erase(OpsToPrint.begin()+i-1);
361 }
362
363 // Finally, emit the code.
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000364 O << TheOp.getCode();
Chris Lattner38c07512005-01-22 20:31:17 +0000365 O << "break;\n";
366}
367
Chris Lattner870c0162005-01-22 18:38:13 +0000368
369/// EmitInstructions - Emit the last instruction in the vector and any other
370/// instructions that are suitably similar to it.
371static void EmitInstructions(std::vector<AsmWriterInst> &Insts,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000372 raw_ostream &O) {
Chris Lattner870c0162005-01-22 18:38:13 +0000373 AsmWriterInst FirstInst = Insts.back();
374 Insts.pop_back();
375
376 std::vector<AsmWriterInst> SimilarInsts;
377 unsigned DifferingOperand = ~0;
378 for (unsigned i = Insts.size(); i != 0; --i) {
Chris Lattnerf8766682005-01-22 19:22:23 +0000379 unsigned DiffOp = Insts[i-1].MatchesAllButOneOp(FirstInst);
380 if (DiffOp != ~1U) {
Chris Lattner870c0162005-01-22 18:38:13 +0000381 if (DifferingOperand == ~0U) // First match!
382 DifferingOperand = DiffOp;
383
384 // If this differs in the same operand as the rest of the instructions in
385 // this class, move it to the SimilarInsts list.
Chris Lattnerf8766682005-01-22 19:22:23 +0000386 if (DifferingOperand == DiffOp || DiffOp == ~0U) {
Chris Lattner870c0162005-01-22 18:38:13 +0000387 SimilarInsts.push_back(Insts[i-1]);
388 Insts.erase(Insts.begin()+i-1);
389 }
390 }
391 }
392
Chris Lattnera1e8a802006-05-01 17:01:17 +0000393 O << " case " << FirstInst.CGI->Namespace << "::"
Chris Lattner870c0162005-01-22 18:38:13 +0000394 << FirstInst.CGI->TheDef->getName() << ":\n";
395 for (unsigned i = 0, e = SimilarInsts.size(); i != e; ++i)
Chris Lattnera1e8a802006-05-01 17:01:17 +0000396 O << " case " << SimilarInsts[i].CGI->Namespace << "::"
Chris Lattner870c0162005-01-22 18:38:13 +0000397 << SimilarInsts[i].CGI->TheDef->getName() << ":\n";
398 for (unsigned i = 0, e = FirstInst.Operands.size(); i != e; ++i) {
399 if (i != DifferingOperand) {
400 // If the operand is the same for all instructions, just print it.
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000401 O << " " << FirstInst.Operands[i].getCode();
Chris Lattner870c0162005-01-22 18:38:13 +0000402 } else {
403 // If this is the operand that varies between all of the instructions,
404 // emit a switch for just this operand now.
405 O << " switch (MI->getOpcode()) {\n";
Chris Lattner38c07512005-01-22 20:31:17 +0000406 std::vector<std::pair<std::string, AsmWriterOperand> > OpsToPrint;
Chris Lattnera1e8a802006-05-01 17:01:17 +0000407 OpsToPrint.push_back(std::make_pair(FirstInst.CGI->Namespace + "::" +
Chris Lattner38c07512005-01-22 20:31:17 +0000408 FirstInst.CGI->TheDef->getName(),
409 FirstInst.Operands[i]));
Misha Brukman3da94ae2005-04-22 00:00:37 +0000410
Chris Lattner870c0162005-01-22 18:38:13 +0000411 for (unsigned si = 0, e = SimilarInsts.size(); si != e; ++si) {
Chris Lattner38c07512005-01-22 20:31:17 +0000412 AsmWriterInst &AWI = SimilarInsts[si];
Chris Lattnera1e8a802006-05-01 17:01:17 +0000413 OpsToPrint.push_back(std::make_pair(AWI.CGI->Namespace+"::"+
Chris Lattner38c07512005-01-22 20:31:17 +0000414 AWI.CGI->TheDef->getName(),
415 AWI.Operands[i]));
Chris Lattner870c0162005-01-22 18:38:13 +0000416 }
Chris Lattner38c07512005-01-22 20:31:17 +0000417 std::reverse(OpsToPrint.begin(), OpsToPrint.end());
418 while (!OpsToPrint.empty())
419 PrintCases(OpsToPrint, O);
Chris Lattner870c0162005-01-22 18:38:13 +0000420 O << " }";
421 }
422 O << "\n";
423 }
Chris Lattner870c0162005-01-22 18:38:13 +0000424 O << " break;\n";
425}
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000426
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000427void AsmWriterEmitter::
428FindUniqueOperandCommands(std::vector<std::string> &UniqueOperandCommands,
Chris Lattner96c1ade2006-07-18 18:28:27 +0000429 std::vector<unsigned> &InstIdxs,
430 std::vector<unsigned> &InstOpsUsed) const {
Chris Lattner195bb4a2006-07-18 19:27:30 +0000431 InstIdxs.assign(NumberedInstructions.size(), ~0U);
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000432
433 // This vector parallels UniqueOperandCommands, keeping track of which
434 // instructions each case are used for. It is a comma separated string of
435 // enums.
436 std::vector<std::string> InstrsForCase;
437 InstrsForCase.resize(UniqueOperandCommands.size());
Chris Lattner96c1ade2006-07-18 18:28:27 +0000438 InstOpsUsed.assign(UniqueOperandCommands.size(), 0);
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000439
440 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
441 const AsmWriterInst *Inst = getAsmWriterInstByID(i);
Dan Gohman44066042008-07-01 00:05:16 +0000442 if (Inst == 0) continue; // PHI, INLINEASM, DBG_LABEL, etc.
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000443
444 std::string Command;
Chris Lattnerb8462862006-07-18 17:56:07 +0000445 if (Inst->Operands.empty())
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000446 continue; // Instruction already done.
Chris Lattner191dd1f2006-07-18 17:50:22 +0000447
Chris Lattnerb8462862006-07-18 17:56:07 +0000448 Command = " " + Inst->Operands[0].getCode() + "\n";
Chris Lattner191dd1f2006-07-18 17:50:22 +0000449
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000450 // Check to see if we already have 'Command' in UniqueOperandCommands.
451 // If not, add it.
452 bool FoundIt = false;
453 for (unsigned idx = 0, e = UniqueOperandCommands.size(); idx != e; ++idx)
454 if (UniqueOperandCommands[idx] == Command) {
455 InstIdxs[i] = idx;
456 InstrsForCase[idx] += ", ";
457 InstrsForCase[idx] += Inst->CGI->TheDef->getName();
458 FoundIt = true;
459 break;
460 }
461 if (!FoundIt) {
462 InstIdxs[i] = UniqueOperandCommands.size();
463 UniqueOperandCommands.push_back(Command);
464 InstrsForCase.push_back(Inst->CGI->TheDef->getName());
Chris Lattner96c1ade2006-07-18 18:28:27 +0000465
466 // This command matches one operand so far.
467 InstOpsUsed.push_back(1);
468 }
469 }
470
471 // For each entry of UniqueOperandCommands, there is a set of instructions
472 // that uses it. If the next command of all instructions in the set are
473 // identical, fold it into the command.
474 for (unsigned CommandIdx = 0, e = UniqueOperandCommands.size();
475 CommandIdx != e; ++CommandIdx) {
476
477 for (unsigned Op = 1; ; ++Op) {
478 // Scan for the first instruction in the set.
479 std::vector<unsigned>::iterator NIT =
480 std::find(InstIdxs.begin(), InstIdxs.end(), CommandIdx);
481 if (NIT == InstIdxs.end()) break; // No commonality.
482
483 // If this instruction has no more operands, we isn't anything to merge
484 // into this command.
485 const AsmWriterInst *FirstInst =
486 getAsmWriterInstByID(NIT-InstIdxs.begin());
487 if (!FirstInst || FirstInst->Operands.size() == Op)
488 break;
489
490 // Otherwise, scan to see if all of the other instructions in this command
491 // set share the operand.
492 bool AllSame = true;
David Greenec8d06052009-07-29 20:10:24 +0000493 // Keep track of the maximum, number of operands or any
494 // instruction we see in the group.
495 size_t MaxSize = FirstInst->Operands.size();
496
Chris Lattner96c1ade2006-07-18 18:28:27 +0000497 for (NIT = std::find(NIT+1, InstIdxs.end(), CommandIdx);
498 NIT != InstIdxs.end();
499 NIT = std::find(NIT+1, InstIdxs.end(), CommandIdx)) {
500 // Okay, found another instruction in this command set. If the operand
501 // matches, we're ok, otherwise bail out.
502 const AsmWriterInst *OtherInst =
503 getAsmWriterInstByID(NIT-InstIdxs.begin());
David Greenec8d06052009-07-29 20:10:24 +0000504
505 if (OtherInst &&
506 OtherInst->Operands.size() > FirstInst->Operands.size())
507 MaxSize = std::max(MaxSize, OtherInst->Operands.size());
508
Chris Lattner96c1ade2006-07-18 18:28:27 +0000509 if (!OtherInst || OtherInst->Operands.size() == Op ||
510 OtherInst->Operands[Op] != FirstInst->Operands[Op]) {
511 AllSame = false;
512 break;
513 }
514 }
515 if (!AllSame) break;
516
517 // Okay, everything in this command set has the same next operand. Add it
518 // to UniqueOperandCommands and remember that it was consumed.
519 std::string Command = " " + FirstInst->Operands[Op].getCode() + "\n";
520
Chris Lattner96c1ade2006-07-18 18:28:27 +0000521 UniqueOperandCommands[CommandIdx] += Command;
522 InstOpsUsed[CommandIdx]++;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000523 }
524 }
525
526 // Prepend some of the instructions each case is used for onto the case val.
527 for (unsigned i = 0, e = InstrsForCase.size(); i != e; ++i) {
528 std::string Instrs = InstrsForCase[i];
529 if (Instrs.size() > 70) {
530 Instrs.erase(Instrs.begin()+70, Instrs.end());
531 Instrs += "...";
532 }
533
534 if (!Instrs.empty())
535 UniqueOperandCommands[i] = " // " + Instrs + "\n" +
536 UniqueOperandCommands[i];
537 }
538}
539
540
Chris Lattner05af2612009-09-13 20:08:00 +0000541/// EmitPrintInstruction - Generate the code for the "printInstruction" method
542/// implementation.
543void AsmWriterEmitter::EmitPrintInstruction(raw_ostream &O) {
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000544 CodeGenTarget Target;
Chris Lattner175580c2004-08-14 22:50:53 +0000545 Record *AsmWriter = Target.getAsmWriter();
Chris Lattner953c6fe2004-10-03 20:19:02 +0000546 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
Chris Lattner05af2612009-09-13 20:08:00 +0000547
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000548 O <<
549 "/// printInstruction - This method is automatically generated by tablegen\n"
Chris Lattner05af2612009-09-13 20:08:00 +0000550 "/// from the instruction set description.\n"
Chris Lattner41aefdc2009-08-08 01:32:19 +0000551 "void " << Target.getName() << ClassName
Chris Lattner175580c2004-08-14 22:50:53 +0000552 << "::printInstruction(const MachineInstr *MI) {\n";
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000553
Chris Lattner5765dba2005-01-22 17:40:38 +0000554 std::vector<AsmWriterInst> Instructions;
555
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000556 for (CodeGenTarget::inst_iterator I = Target.inst_begin(),
557 E = Target.inst_end(); I != E; ++I)
Chris Lattner5f12c212009-09-11 00:41:15 +0000558 if (!I->second.AsmString.empty() &&
559 I->second.TheDef->getName() != "PHI")
Chris Lattner59e86772009-08-07 23:13:38 +0000560 Instructions.push_back(AsmWriterInst(I->second, AsmWriter));
Chris Lattner076efa72004-08-01 07:43:02 +0000561
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000562 // Get the instruction numbering.
Chris Lattner0cfcc1e2006-01-27 02:10:50 +0000563 Target.getInstructionsByEnumValue(NumberedInstructions);
564
Chris Lattner6af022f2006-07-14 22:59:11 +0000565 // Compute the CodeGenInstruction -> AsmWriterInst mapping. Note that not
566 // all machine instructions are necessarily being printed, so there may be
567 // target instructions not in this map.
Chris Lattner6af022f2006-07-14 22:59:11 +0000568 for (unsigned i = 0, e = Instructions.size(); i != e; ++i)
569 CGIAWIMap.insert(std::make_pair(Instructions[i].CGI, &Instructions[i]));
Chris Lattnerf8766682005-01-22 19:22:23 +0000570
Chris Lattner6af022f2006-07-14 22:59:11 +0000571 // Build an aggregate string, and build a table of offsets into it.
Chris Lattner3200fc92009-09-14 01:16:36 +0000572 StringToOffsetTable StringTable;
Chris Lattner6af022f2006-07-14 22:59:11 +0000573
Chris Lattner259bda42006-09-27 16:44:09 +0000574 /// OpcodeInfo - This encodes the index of the string to use for the first
Chris Lattner55616402006-07-18 17:32:27 +0000575 /// chunk of the output as well as indices used for operand printing.
576 std::vector<unsigned> OpcodeInfo;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000577
Chris Lattner55616402006-07-18 17:32:27 +0000578 unsigned MaxStringIdx = 0;
Chris Lattner6af022f2006-07-14 22:59:11 +0000579 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
580 AsmWriterInst *AWI = CGIAWIMap[NumberedInstructions[i]];
581 unsigned Idx;
Chris Lattnera6dc9fb2006-07-19 01:39:06 +0000582 if (AWI == 0) {
Chris Lattner6af022f2006-07-14 22:59:11 +0000583 // Something not handled by the asmwriter printer.
Chris Lattner3200fc92009-09-14 01:16:36 +0000584 Idx = ~0U;
Chris Lattnera6dc9fb2006-07-19 01:39:06 +0000585 } else if (AWI->Operands[0].OperandType !=
586 AsmWriterOperand::isLiteralTextOperand ||
587 AWI->Operands[0].Str.empty()) {
588 // Something handled by the asmwriter printer, but with no leading string.
Chris Lattner3200fc92009-09-14 01:16:36 +0000589 Idx = StringTable.GetOrAddStringOffset("");
Chris Lattner6af022f2006-07-14 22:59:11 +0000590 } else {
Chris Lattner3200fc92009-09-14 01:16:36 +0000591 std::string Str = AWI->Operands[0].Str;
592 UnescapeString(Str);
593 Idx = StringTable.GetOrAddStringOffset(Str);
594 MaxStringIdx = std::max(MaxStringIdx, Idx);
595
Chris Lattner6af022f2006-07-14 22:59:11 +0000596 // Nuke the string from the operand list. It is now handled!
597 AWI->Operands.erase(AWI->Operands.begin());
Chris Lattnerf8766682005-01-22 19:22:23 +0000598 }
Chris Lattner3200fc92009-09-14 01:16:36 +0000599
600 // Bias offset by one since we want 0 as a sentinel.
601 OpcodeInfo.push_back(Idx+1);
Chris Lattnerf8766682005-01-22 19:22:23 +0000602 }
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000603
Chris Lattner55616402006-07-18 17:32:27 +0000604 // Figure out how many bits we used for the string index.
Chris Lattner3200fc92009-09-14 01:16:36 +0000605 unsigned AsmStrBits = Log2_32_Ceil(MaxStringIdx+2);
Chris Lattner55616402006-07-18 17:32:27 +0000606
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000607 // To reduce code size, we compactify common instructions into a few bits
608 // in the opcode-indexed table.
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000609 unsigned BitsLeft = 32-AsmStrBits;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000610
611 std::vector<std::vector<std::string> > TableDrivenOperandPrinters;
612
Chris Lattnerb8462862006-07-18 17:56:07 +0000613 while (1) {
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000614 std::vector<std::string> UniqueOperandCommands;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000615 std::vector<unsigned> InstIdxs;
Chris Lattner96c1ade2006-07-18 18:28:27 +0000616 std::vector<unsigned> NumInstOpsHandled;
617 FindUniqueOperandCommands(UniqueOperandCommands, InstIdxs,
618 NumInstOpsHandled);
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000619
620 // If we ran out of operands to print, we're done.
621 if (UniqueOperandCommands.empty()) break;
622
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000623 // Compute the number of bits we need to represent these cases, this is
624 // ceil(log2(numentries)).
625 unsigned NumBits = Log2_32_Ceil(UniqueOperandCommands.size());
626
627 // If we don't have enough bits for this operand, don't include it.
628 if (NumBits > BitsLeft) {
Chris Lattner569f1212009-08-23 04:44:11 +0000629 DEBUG(errs() << "Not enough bits to densely encode " << NumBits
630 << " more bits\n");
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000631 break;
632 }
633
634 // Otherwise, we can include this in the initial lookup table. Add it in.
635 BitsLeft -= NumBits;
636 for (unsigned i = 0, e = InstIdxs.size(); i != e; ++i)
Chris Lattner195bb4a2006-07-18 19:27:30 +0000637 if (InstIdxs[i] != ~0U)
638 OpcodeInfo[i] |= InstIdxs[i] << (BitsLeft+AsmStrBits);
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000639
Chris Lattnerb8462862006-07-18 17:56:07 +0000640 // Remove the info about this operand.
641 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
642 if (AsmWriterInst *Inst = getAsmWriterInstByID(i))
Chris Lattner96c1ade2006-07-18 18:28:27 +0000643 if (!Inst->Operands.empty()) {
644 unsigned NumOps = NumInstOpsHandled[InstIdxs[i]];
Chris Lattner0a012122006-07-18 19:06:01 +0000645 assert(NumOps <= Inst->Operands.size() &&
646 "Can't remove this many ops!");
Chris Lattner96c1ade2006-07-18 18:28:27 +0000647 Inst->Operands.erase(Inst->Operands.begin(),
648 Inst->Operands.begin()+NumOps);
649 }
Chris Lattnerb8462862006-07-18 17:56:07 +0000650 }
651
652 // Remember the handlers for this set of operands.
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000653 TableDrivenOperandPrinters.push_back(UniqueOperandCommands);
654 }
655
656
657
Chris Lattner55616402006-07-18 17:32:27 +0000658 O<<" static const unsigned OpInfo[] = {\n";
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000659 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000660 O << " " << OpcodeInfo[i] << "U,\t// "
Chris Lattner55616402006-07-18 17:32:27 +0000661 << NumberedInstructions[i]->TheDef->getName() << "\n";
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000662 }
663 // Add a dummy entry so the array init doesn't end with a comma.
Chris Lattner55616402006-07-18 17:32:27 +0000664 O << " 0U\n";
Chris Lattner6af022f2006-07-14 22:59:11 +0000665 O << " };\n\n";
666
667 // Emit the string itself.
Chris Lattner3200fc92009-09-14 01:16:36 +0000668 O << " const char *AsmStrs = \n";
669 StringTable.EmitString(O);
670 O << ";\n\n";
Chris Lattner6af022f2006-07-14 22:59:11 +0000671
Chris Lattner5b842c32009-06-19 23:57:53 +0000672 O << "\n#ifndef NO_ASM_WRITER_BOILERPLATE\n";
673
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000674 O << " if (MI->getOpcode() == TargetInstrInfo::INLINEASM) {\n"
Evan Cheng4eecdeb2008-02-02 08:39:46 +0000675 << " O << \"\\t\";\n"
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000676 << " printInlineAsm(MI);\n"
Chris Lattner41aefdc2009-08-08 01:32:19 +0000677 << " return;\n"
Dan Gohman44066042008-07-01 00:05:16 +0000678 << " } else if (MI->isLabel()) {\n"
Jim Laskeya683f9b2007-01-26 17:29:20 +0000679 << " printLabel(MI);\n"
Chris Lattner41aefdc2009-08-08 01:32:19 +0000680 << " return;\n"
Evan Chengda47e6e2008-03-15 00:03:38 +0000681 << " } else if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {\n"
682 << " printImplicitDef(MI);\n"
Chris Lattner41aefdc2009-08-08 01:32:19 +0000683 << " return;\n"
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000684 << " }\n\n";
Chris Lattner5b842c32009-06-19 23:57:53 +0000685
686 O << "\n#endif\n";
687
Evan Cheng4eecdeb2008-02-02 08:39:46 +0000688 O << " O << \"\\t\";\n\n";
689
Chris Lattner6af022f2006-07-14 22:59:11 +0000690 O << " // Emit the opcode for the instruction.\n"
Chris Lattner55616402006-07-18 17:32:27 +0000691 << " unsigned Bits = OpInfo[MI->getOpcode()];\n"
Chris Lattner41aefdc2009-08-08 01:32:19 +0000692 << " assert(Bits != 0 && \"Cannot print this instruction.\");\n"
Chris Lattner3200fc92009-09-14 01:16:36 +0000693 << " O << AsmStrs+(Bits & " << (1 << AsmStrBits)-1 << ")-1;\n\n";
David Greenea5bb59f2009-08-05 21:00:52 +0000694
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000695 // Output the table driven operand information.
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000696 BitsLeft = 32-AsmStrBits;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000697 for (unsigned i = 0, e = TableDrivenOperandPrinters.size(); i != e; ++i) {
698 std::vector<std::string> &Commands = TableDrivenOperandPrinters[i];
699
700 // Compute the number of bits we need to represent these cases, this is
701 // ceil(log2(numentries)).
702 unsigned NumBits = Log2_32_Ceil(Commands.size());
703 assert(NumBits <= BitsLeft && "consistency error");
704
705 // Emit code to extract this field from Bits.
706 BitsLeft -= NumBits;
707
708 O << "\n // Fragment " << i << " encoded into " << NumBits
Chris Lattnere7a589d2006-07-18 17:43:54 +0000709 << " bits for " << Commands.size() << " unique commands.\n";
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000710
Chris Lattner96c1ade2006-07-18 18:28:27 +0000711 if (Commands.size() == 2) {
Chris Lattnere7a589d2006-07-18 17:43:54 +0000712 // Emit two possibilitys with if/else.
713 O << " if ((Bits >> " << (BitsLeft+AsmStrBits) << ") & "
714 << ((1 << NumBits)-1) << ") {\n"
715 << Commands[1]
716 << " } else {\n"
717 << Commands[0]
718 << " }\n\n";
719 } else {
720 O << " switch ((Bits >> " << (BitsLeft+AsmStrBits) << ") & "
721 << ((1 << NumBits)-1) << ") {\n"
722 << " default: // unreachable.\n";
723
724 // Print out all the cases.
725 for (unsigned i = 0, e = Commands.size(); i != e; ++i) {
726 O << " case " << i << ":\n";
727 O << Commands[i];
728 O << " break;\n";
729 }
730 O << " }\n\n";
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000731 }
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000732 }
733
Chris Lattnerb8462862006-07-18 17:56:07 +0000734 // Okay, delete instructions with no operand info left.
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000735 for (unsigned i = 0, e = Instructions.size(); i != e; ++i) {
736 // Entire instruction has been emitted?
737 AsmWriterInst &Inst = Instructions[i];
Chris Lattnerb8462862006-07-18 17:56:07 +0000738 if (Inst.Operands.empty()) {
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000739 Instructions.erase(Instructions.begin()+i);
Chris Lattnerb8462862006-07-18 17:56:07 +0000740 --i; --e;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000741 }
742 }
743
744
745 // Because this is a vector, we want to emit from the end. Reverse all of the
Chris Lattner870c0162005-01-22 18:38:13 +0000746 // elements in the vector.
747 std::reverse(Instructions.begin(), Instructions.end());
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000748
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000749 if (!Instructions.empty()) {
750 // Find the opcode # of inline asm.
751 O << " switch (MI->getOpcode()) {\n";
752 while (!Instructions.empty())
753 EmitInstructions(Instructions, O);
Chris Lattner870c0162005-01-22 18:38:13 +0000754
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000755 O << " }\n";
Chris Lattner41aefdc2009-08-08 01:32:19 +0000756 O << " return;\n";
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000757 }
David Greenec8d06052009-07-29 20:10:24 +0000758
Chris Lattner41aefdc2009-08-08 01:32:19 +0000759 O << " return;\n";
Chris Lattner0a012122006-07-18 19:06:01 +0000760 O << "}\n";
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000761}
Chris Lattner05af2612009-09-13 20:08:00 +0000762
763
764void AsmWriterEmitter::EmitGetRegisterName(raw_ostream &O) {
765 CodeGenTarget Target;
766 Record *AsmWriter = Target.getAsmWriter();
767 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
768 const std::vector<CodeGenRegister> &Registers = Target.getRegisters();
769
770 O <<
771 "\n\n/// getRegisterName - This method is automatically generated by tblgen\n"
772 "/// from the register set description. This returns the assembler name\n"
773 "/// for the specified register.\n"
774 "const char *" << Target.getName() << ClassName
Chris Lattnerd95148f2009-09-13 20:19:22 +0000775 << "::getRegisterName(unsigned RegNo) {\n"
Chris Lattner05af2612009-09-13 20:08:00 +0000776 << " assert(RegNo && RegNo < " << (Registers.size()+1)
777 << " && \"Invalid register number!\");\n"
778 << "\n"
779 << " static const char *const RegAsmNames[] = {\n";
780 for (unsigned i = 0, e = Registers.size(); i != e; ++i) {
781 const CodeGenRegister &Reg = Registers[i];
782
783 std::string AsmName = Reg.TheDef->getValueAsString("AsmName");
784 if (AsmName.empty())
785 AsmName = Reg.getName();
786 O << " \"" << AsmName << "\",\n";
787 }
788 O << " 0\n"
789 << " };\n"
790 << "\n"
791 << " return RegAsmNames[RegNo-1];\n"
792 << "}\n";
793}
794
795
796void AsmWriterEmitter::run(raw_ostream &O) {
797 EmitSourceFileHeader("Assembly Writer Source Fragment", O);
798
799 EmitPrintInstruction(O);
800 EmitGetRegisterName(O);
801}
802