1 //===-- AsmPrinter.cpp - Common AsmPrinter code ---------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the AsmPrinter class.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "asm-printer"
15 #include "llvm/InlineAsm.h"
16 #include "llvm/CodeGen/AsmPrinter.h"
17 #include "llvm/CodeGen/MachineBasicBlock.h"
18 #include "llvm/MC/MCAsmInfo.h"
19 #include "llvm/MC/MCStreamer.h"
20 #include "llvm/MC/MCSymbol.h"
21 #include "llvm/ADT/SmallString.h"
22 #include "llvm/ADT/Twine.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include <cerrno> // FIXME: Stop using this.
28 /// EmitInlineAsm - Emit a blob of inline asm to the output streamer.
29 void AsmPrinter::EmitInlineAsm(StringRef Str) const {
30 assert(!Str.empty() && "Can't emit empty inline asm block");
32 // If the output streamer is actually a .s file, just emit the blob textually.
33 // This is useful in case the asm parser doesn't handle something but the
34 // system assembler does.
35 if (OutStreamer.hasRawTextSupport()) {
36 OutStreamer.EmitRawText(Str);
40 errs() << "Inline asm not supported by this streamer!\n";
44 /// EmitInlineAsm - This method formats and emits the specified machine
45 /// instruction that is an inline asm.
46 void AsmPrinter::EmitInlineAsm(const MachineInstr *MI) const {
47 assert(MI->isInlineAsm() && "printInlineAsm only works on inline asms");
49 unsigned NumOperands = MI->getNumOperands();
51 // Count the number of register definitions to find the asm string.
53 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
55 assert(NumDefs != NumOperands-1 && "No asm string?");
57 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
59 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
60 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
62 // If this asmstr is empty, just print the #APP/#NOAPP markers.
63 // These are useful to see where empty asm's wound up.
65 if (!OutStreamer.hasRawTextSupport()) return;
67 OutStreamer.EmitRawText(Twine("\t")+MAI->getCommentString()+
68 MAI->getInlineAsmStart());
69 OutStreamer.EmitRawText(Twine("\t")+MAI->getCommentString()+
70 MAI->getInlineAsmEnd());
74 // Emit the #APP start marker. This has to happen even if verbose-asm isn't
75 // enabled, so we use EmitRawText.
76 if (OutStreamer.hasRawTextSupport())
77 OutStreamer.EmitRawText(Twine("\t")+MAI->getCommentString()+
78 MAI->getInlineAsmStart());
80 // Emit the inline asm to a temporary string so we can emit it through
82 SmallString<256> StringData;
83 raw_svector_ostream OS(StringData);
87 // The variant of the current asmprinter.
88 int AsmPrinterVariant = MAI->getAssemblerDialect();
90 int CurVariant = -1; // The number of the {.|.|.} region we are in.
91 const char *LastEmitted = AsmStr; // One past the last character emitted.
93 while (*LastEmitted) {
94 switch (*LastEmitted) {
96 // Not a special case, emit the string section literally.
97 const char *LiteralEnd = LastEmitted+1;
98 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
99 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
101 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
102 OS.write(LastEmitted, LiteralEnd-LastEmitted);
103 LastEmitted = LiteralEnd;
107 ++LastEmitted; // Consume newline character.
108 OS << '\n'; // Indent code with newline.
111 ++LastEmitted; // Consume '$' character.
115 switch (*LastEmitted) {
116 default: Done = false; break;
118 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
120 ++LastEmitted; // Consume second '$' character.
122 case '(': // $( -> same as GCC's { character.
123 ++LastEmitted; // Consume '(' character.
124 if (CurVariant != -1) {
125 llvm_report_error("Nested variants found in inline asm string: '"
126 + std::string(AsmStr) + "'");
128 CurVariant = 0; // We're in the first variant now.
131 ++LastEmitted; // consume '|' character.
132 if (CurVariant == -1)
133 OS << '|'; // this is gcc's behavior for | outside a variant
135 ++CurVariant; // We're in the next variant.
137 case ')': // $) -> same as GCC's } char.
138 ++LastEmitted; // consume ')' character.
139 if (CurVariant == -1)
140 OS << '}'; // this is gcc's behavior for } outside a variant
147 bool HasCurlyBraces = false;
148 if (*LastEmitted == '{') { // ${variable}
149 ++LastEmitted; // Consume '{' character.
150 HasCurlyBraces = true;
153 // If we have ${:foo}, then this is not a real operand reference, it is a
154 // "magic" string reference, just like in .td files. Arrange to call
156 if (HasCurlyBraces && *LastEmitted == ':') {
158 const char *StrStart = LastEmitted;
159 const char *StrEnd = strchr(StrStart, '}');
161 llvm_report_error(Twine("Unterminated ${:foo} operand in inline asm"
162 " string: '") + Twine(AsmStr) + "'");
164 std::string Val(StrStart, StrEnd);
165 PrintSpecial(MI, OS, Val.c_str());
166 LastEmitted = StrEnd+1;
170 const char *IDStart = LastEmitted;
173 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
174 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
175 llvm_report_error("Bad $ operand number in inline asm string: '"
176 + std::string(AsmStr) + "'");
180 char Modifier[2] = { 0, 0 };
182 if (HasCurlyBraces) {
183 // If we have curly braces, check for a modifier character. This
184 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
185 if (*LastEmitted == ':') {
186 ++LastEmitted; // Consume ':' character.
187 if (*LastEmitted == 0) {
188 llvm_report_error("Bad ${:} expression in inline asm string: '"
189 + std::string(AsmStr) + "'");
192 Modifier[0] = *LastEmitted;
193 ++LastEmitted; // Consume modifier character.
196 if (*LastEmitted != '}') {
197 llvm_report_error("Bad ${} expression in inline asm string: '"
198 + std::string(AsmStr) + "'");
200 ++LastEmitted; // Consume '}' character.
203 if ((unsigned)Val >= NumOperands-1) {
204 llvm_report_error("Invalid $ operand number in inline asm string: '"
205 + std::string(AsmStr) + "'");
208 // Okay, we finally have a value number. Ask the target to print this
210 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
215 // Scan to find the machine operand number for the operand.
217 if (OpNo >= MI->getNumOperands()) break;
218 unsigned OpFlags = MI->getOperand(OpNo).getImm();
219 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
222 if (OpNo >= MI->getNumOperands()) {
225 unsigned OpFlags = MI->getOperand(OpNo).getImm();
226 ++OpNo; // Skip over the ID number.
228 if (Modifier[0] == 'l') // labels are target independent
229 // FIXME: What if the operand isn't an MBB, report error?
230 OS << *MI->getOperand(OpNo).getMBB()->getSymbol();
232 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
233 if ((OpFlags & 7) == 4) {
234 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
235 Modifier[0] ? Modifier : 0,
238 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
239 Modifier[0] ? Modifier : 0, OS);
245 raw_string_ostream Msg(msg);
246 Msg << "Invalid operand found in inline asm: '" << AsmStr << "'\n";
248 llvm_report_error(Msg.str());
257 EmitInlineAsm(OS.str());
259 // Emit the #NOAPP end marker. This has to happen even if verbose-asm isn't
260 // enabled, so we use EmitRawText.
261 if (OutStreamer.hasRawTextSupport())
262 OutStreamer.EmitRawText(Twine("\t")+MAI->getCommentString()+
263 MAI->getInlineAsmEnd());
267 /// PrintSpecial - Print information related to the specified machine instr
268 /// that is independent of the operand, and may be independent of the instr
269 /// itself. This can be useful for portably encoding the comment character
270 /// or other bits of target-specific knowledge into the asmstrings. The
271 /// syntax used is ${:comment}. Targets can override this to add support
272 /// for their own strange codes.
273 void AsmPrinter::PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
274 const char *Code) const {
275 if (!strcmp(Code, "private")) {
276 OS << MAI->getPrivateGlobalPrefix();
277 } else if (!strcmp(Code, "comment")) {
278 OS << MAI->getCommentString();
279 } else if (!strcmp(Code, "uid")) {
280 // Comparing the address of MI isn't sufficient, because machineinstrs may
281 // be allocated to the same address across functions.
283 // If this is a new LastFn instruction, bump the counter.
284 if (LastMI != MI || LastFn != getFunctionNumber()) {
287 LastFn = getFunctionNumber();
292 raw_string_ostream Msg(msg);
293 Msg << "Unknown special formatter '" << Code
294 << "' for machine instr: " << *MI;
295 llvm_report_error(Msg.str());
299 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
300 /// instruction, using the specified assembler variant. Targets should
301 /// override this to format as appropriate.
302 bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
303 unsigned AsmVariant, const char *ExtraCode,
305 // Target doesn't support this yet!
309 bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
311 const char *ExtraCode, raw_ostream &O) {
312 // Target doesn't support this yet!