Chris Lattner | 7e1a8f8 | 2010-04-04 19:09:29 +0000 | [diff] [blame] | 1 | //===-- AsmPrinterInlineAsm.cpp - AsmPrinter Inline Asm Handling ----------===// |
Chris Lattner | 736e31d | 2010-04-04 18:34:07 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | 7e1a8f8 | 2010-04-04 19:09:29 +0000 | [diff] [blame] | 10 | // This file implements the inline assembler pieces of the AsmPrinter class. |
Chris Lattner | 736e31d | 2010-04-04 18:34:07 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #define DEBUG_TYPE "asm-printer" |
Chris Lattner | 736e31d | 2010-04-04 18:34:07 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/AsmPrinter.h" |
Chris Lattner | 7e1a8f8 | 2010-04-04 19:09:29 +0000 | [diff] [blame] | 16 | #include "llvm/InlineAsm.h" |
Chris Lattner | 6eb7806 | 2010-04-06 00:55:39 +0000 | [diff] [blame] | 17 | #include "llvm/LLVMContext.h" |
| 18 | #include "llvm/Module.h" |
Chris Lattner | 736e31d | 2010-04-04 18:34:07 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineBasicBlock.h" |
Chris Lattner | 6eb7806 | 2010-04-06 00:55:39 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Chris Lattner | 736e31d | 2010-04-04 18:34:07 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCAsmInfo.h" |
| 22 | #include "llvm/MC/MCStreamer.h" |
| 23 | #include "llvm/MC/MCSymbol.h" |
Chris Lattner | af632c9 | 2010-04-05 23:11:24 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCParser/AsmParser.h" |
| 25 | #include "llvm/Target/TargetAsmParser.h" |
| 26 | #include "llvm/Target/TargetMachine.h" |
| 27 | #include "llvm/Target/TargetRegistry.h" |
| 28 | #include "llvm/ADT/OwningPtr.h" |
Chris Lattner | 736e31d | 2010-04-04 18:34:07 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/SmallString.h" |
| 30 | #include "llvm/ADT/Twine.h" |
| 31 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | af632c9 | 2010-04-05 23:11:24 +0000 | [diff] [blame] | 32 | #include "llvm/Support/MemoryBuffer.h" |
| 33 | #include "llvm/Support/SourceMgr.h" |
Chris Lattner | 736e31d | 2010-04-04 18:34:07 +0000 | [diff] [blame] | 34 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 736e31d | 2010-04-04 18:34:07 +0000 | [diff] [blame] | 35 | using namespace llvm; |
| 36 | |
| 37 | /// EmitInlineAsm - Emit a blob of inline asm to the output streamer. |
Chris Lattner | 885d941 | 2010-04-06 00:58:50 +0000 | [diff] [blame^] | 38 | void AsmPrinter::EmitInlineAsm(StringRef Str, unsigned LocCookie) const { |
Chris Lattner | 736e31d | 2010-04-04 18:34:07 +0000 | [diff] [blame] | 39 | assert(!Str.empty() && "Can't emit empty inline asm block"); |
| 40 | |
Chris Lattner | af632c9 | 2010-04-05 23:11:24 +0000 | [diff] [blame] | 41 | // Remember if the buffer is nul terminated or not so we can avoid a copy. |
| 42 | bool isNullTerminated = Str.back() == 0; |
| 43 | if (isNullTerminated) |
| 44 | Str = Str.substr(0, Str.size()-1); |
| 45 | |
Chris Lattner | 736e31d | 2010-04-04 18:34:07 +0000 | [diff] [blame] | 46 | // If the output streamer is actually a .s file, just emit the blob textually. |
| 47 | // This is useful in case the asm parser doesn't handle something but the |
| 48 | // system assembler does. |
| 49 | if (OutStreamer.hasRawTextSupport()) { |
| 50 | OutStreamer.EmitRawText(Str); |
| 51 | return; |
| 52 | } |
| 53 | |
Chris Lattner | af632c9 | 2010-04-05 23:11:24 +0000 | [diff] [blame] | 54 | SourceMgr SrcMgr; |
Chris Lattner | 6eb7806 | 2010-04-06 00:55:39 +0000 | [diff] [blame] | 55 | |
| 56 | // If the current LLVMContext has an inline asm handler, set it in SourceMgr. |
| 57 | LLVMContext &LLVMCtx = MMI->getModule()->getContext(); |
| 58 | bool HasDiagHandler = false; |
| 59 | if (void *DiagHandler = LLVMCtx.getInlineAsmDiagnosticHandler()) { |
Chris Lattner | 6eb7806 | 2010-04-06 00:55:39 +0000 | [diff] [blame] | 60 | SrcMgr.setDiagHandler((SourceMgr::DiagHandlerTy)(intptr_t)DiagHandler, |
Chris Lattner | 885d941 | 2010-04-06 00:58:50 +0000 | [diff] [blame^] | 61 | LLVMCtx.getInlineAsmDiagnosticContext(), LocCookie); |
Chris Lattner | 6eb7806 | 2010-04-06 00:55:39 +0000 | [diff] [blame] | 62 | HasDiagHandler = true; |
| 63 | } |
| 64 | |
Chris Lattner | af632c9 | 2010-04-05 23:11:24 +0000 | [diff] [blame] | 65 | MemoryBuffer *Buffer; |
| 66 | if (isNullTerminated) |
| 67 | Buffer = MemoryBuffer::getMemBuffer(Str, "<inline asm>"); |
| 68 | else |
| 69 | Buffer = MemoryBuffer::getMemBufferCopy(Str, "<inline asm>"); |
| 70 | |
| 71 | // Tell SrcMgr about this buffer, it takes ownership of the buffer. |
| 72 | SrcMgr.AddNewSourceBuffer(Buffer, SMLoc()); |
| 73 | |
| 74 | AsmParser Parser(SrcMgr, OutContext, OutStreamer, *MAI); |
| 75 | OwningPtr<TargetAsmParser> TAP(TM.getTarget().createAsmParser(Parser)); |
| 76 | if (!TAP) |
| 77 | llvm_report_error("Inline asm not supported by this streamer because" |
| 78 | " we don't have an asm parser for this target\n"); |
| 79 | Parser.setTargetParser(*TAP.get()); |
| 80 | |
| 81 | // Don't implicitly switch to the text section before the asm. |
Chris Lattner | 79180e2 | 2010-04-05 23:15:42 +0000 | [diff] [blame] | 82 | int Res = Parser.Run(/*NoInitialTextSection*/ true, |
| 83 | /*NoFinalize*/ true); |
Chris Lattner | 6eb7806 | 2010-04-06 00:55:39 +0000 | [diff] [blame] | 84 | if (Res && !HasDiagHandler) |
Chris Lattner | af632c9 | 2010-04-05 23:11:24 +0000 | [diff] [blame] | 85 | llvm_report_error("Error parsing inline asm\n"); |
Chris Lattner | 736e31d | 2010-04-04 18:34:07 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | |
| 89 | /// EmitInlineAsm - This method formats and emits the specified machine |
| 90 | /// instruction that is an inline asm. |
| 91 | void AsmPrinter::EmitInlineAsm(const MachineInstr *MI) const { |
| 92 | assert(MI->isInlineAsm() && "printInlineAsm only works on inline asms"); |
| 93 | |
| 94 | unsigned NumOperands = MI->getNumOperands(); |
| 95 | |
| 96 | // Count the number of register definitions to find the asm string. |
| 97 | unsigned NumDefs = 0; |
| 98 | for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef(); |
| 99 | ++NumDefs) |
| 100 | assert(NumDefs != NumOperands-1 && "No asm string?"); |
| 101 | |
| 102 | assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?"); |
| 103 | |
| 104 | // Disassemble the AsmStr, printing out the literal pieces, the operands, etc. |
| 105 | const char *AsmStr = MI->getOperand(NumDefs).getSymbolName(); |
| 106 | |
| 107 | // If this asmstr is empty, just print the #APP/#NOAPP markers. |
| 108 | // These are useful to see where empty asm's wound up. |
| 109 | if (AsmStr[0] == 0) { |
Chris Lattner | 4c842dd | 2010-04-05 22:42:30 +0000 | [diff] [blame] | 110 | // Don't emit the comments if writing to a .o file. |
Chris Lattner | 736e31d | 2010-04-04 18:34:07 +0000 | [diff] [blame] | 111 | if (!OutStreamer.hasRawTextSupport()) return; |
| 112 | |
| 113 | OutStreamer.EmitRawText(Twine("\t")+MAI->getCommentString()+ |
| 114 | MAI->getInlineAsmStart()); |
| 115 | OutStreamer.EmitRawText(Twine("\t")+MAI->getCommentString()+ |
| 116 | MAI->getInlineAsmEnd()); |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | // Emit the #APP start marker. This has to happen even if verbose-asm isn't |
| 121 | // enabled, so we use EmitRawText. |
| 122 | if (OutStreamer.hasRawTextSupport()) |
| 123 | OutStreamer.EmitRawText(Twine("\t")+MAI->getCommentString()+ |
| 124 | MAI->getInlineAsmStart()); |
| 125 | |
| 126 | // Emit the inline asm to a temporary string so we can emit it through |
| 127 | // EmitInlineAsm. |
| 128 | SmallString<256> StringData; |
| 129 | raw_svector_ostream OS(StringData); |
| 130 | |
| 131 | OS << '\t'; |
| 132 | |
| 133 | // The variant of the current asmprinter. |
| 134 | int AsmPrinterVariant = MAI->getAssemblerDialect(); |
| 135 | |
| 136 | int CurVariant = -1; // The number of the {.|.|.} region we are in. |
| 137 | const char *LastEmitted = AsmStr; // One past the last character emitted. |
| 138 | |
| 139 | while (*LastEmitted) { |
| 140 | switch (*LastEmitted) { |
| 141 | default: { |
| 142 | // Not a special case, emit the string section literally. |
| 143 | const char *LiteralEnd = LastEmitted+1; |
| 144 | while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' && |
| 145 | *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n') |
| 146 | ++LiteralEnd; |
| 147 | if (CurVariant == -1 || CurVariant == AsmPrinterVariant) |
| 148 | OS.write(LastEmitted, LiteralEnd-LastEmitted); |
| 149 | LastEmitted = LiteralEnd; |
| 150 | break; |
| 151 | } |
| 152 | case '\n': |
| 153 | ++LastEmitted; // Consume newline character. |
Chris Lattner | 4c842dd | 2010-04-05 22:42:30 +0000 | [diff] [blame] | 154 | OS << '\n'; // Indent code with newline. |
Chris Lattner | 736e31d | 2010-04-04 18:34:07 +0000 | [diff] [blame] | 155 | break; |
| 156 | case '$': { |
| 157 | ++LastEmitted; // Consume '$' character. |
| 158 | bool Done = true; |
| 159 | |
| 160 | // Handle escapes. |
| 161 | switch (*LastEmitted) { |
| 162 | default: Done = false; break; |
| 163 | case '$': // $$ -> $ |
| 164 | if (CurVariant == -1 || CurVariant == AsmPrinterVariant) |
| 165 | OS << '$'; |
| 166 | ++LastEmitted; // Consume second '$' character. |
| 167 | break; |
| 168 | case '(': // $( -> same as GCC's { character. |
| 169 | ++LastEmitted; // Consume '(' character. |
| 170 | if (CurVariant != -1) { |
| 171 | llvm_report_error("Nested variants found in inline asm string: '" |
| 172 | + std::string(AsmStr) + "'"); |
| 173 | } |
| 174 | CurVariant = 0; // We're in the first variant now. |
| 175 | break; |
| 176 | case '|': |
| 177 | ++LastEmitted; // consume '|' character. |
| 178 | if (CurVariant == -1) |
| 179 | OS << '|'; // this is gcc's behavior for | outside a variant |
| 180 | else |
| 181 | ++CurVariant; // We're in the next variant. |
| 182 | break; |
| 183 | case ')': // $) -> same as GCC's } char. |
| 184 | ++LastEmitted; // consume ')' character. |
| 185 | if (CurVariant == -1) |
| 186 | OS << '}'; // this is gcc's behavior for } outside a variant |
| 187 | else |
| 188 | CurVariant = -1; |
| 189 | break; |
| 190 | } |
| 191 | if (Done) break; |
| 192 | |
| 193 | bool HasCurlyBraces = false; |
| 194 | if (*LastEmitted == '{') { // ${variable} |
| 195 | ++LastEmitted; // Consume '{' character. |
| 196 | HasCurlyBraces = true; |
| 197 | } |
| 198 | |
| 199 | // If we have ${:foo}, then this is not a real operand reference, it is a |
| 200 | // "magic" string reference, just like in .td files. Arrange to call |
| 201 | // PrintSpecial. |
| 202 | if (HasCurlyBraces && *LastEmitted == ':') { |
| 203 | ++LastEmitted; |
| 204 | const char *StrStart = LastEmitted; |
| 205 | const char *StrEnd = strchr(StrStart, '}'); |
| 206 | if (StrEnd == 0) |
| 207 | llvm_report_error(Twine("Unterminated ${:foo} operand in inline asm" |
| 208 | " string: '") + Twine(AsmStr) + "'"); |
| 209 | |
| 210 | std::string Val(StrStart, StrEnd); |
| 211 | PrintSpecial(MI, OS, Val.c_str()); |
| 212 | LastEmitted = StrEnd+1; |
| 213 | break; |
| 214 | } |
| 215 | |
| 216 | const char *IDStart = LastEmitted; |
Chris Lattner | 65eeaad | 2010-04-04 18:42:18 +0000 | [diff] [blame] | 217 | const char *IDEnd = IDStart; |
| 218 | while (*IDEnd >= '0' && *IDEnd <= '9') ++IDEnd; |
| 219 | |
| 220 | unsigned Val; |
| 221 | if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val)) |
Chris Lattner | 736e31d | 2010-04-04 18:34:07 +0000 | [diff] [blame] | 222 | llvm_report_error("Bad $ operand number in inline asm string: '" |
| 223 | + std::string(AsmStr) + "'"); |
Chris Lattner | 736e31d | 2010-04-04 18:34:07 +0000 | [diff] [blame] | 224 | LastEmitted = IDEnd; |
| 225 | |
| 226 | char Modifier[2] = { 0, 0 }; |
| 227 | |
| 228 | if (HasCurlyBraces) { |
| 229 | // If we have curly braces, check for a modifier character. This |
| 230 | // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm. |
| 231 | if (*LastEmitted == ':') { |
| 232 | ++LastEmitted; // Consume ':' character. |
Chris Lattner | 4c842dd | 2010-04-05 22:42:30 +0000 | [diff] [blame] | 233 | if (*LastEmitted == 0) |
| 234 | llvm_report_error("Bad ${:} expression in inline asm string: '" + |
| 235 | std::string(AsmStr) + "'"); |
Chris Lattner | 736e31d | 2010-04-04 18:34:07 +0000 | [diff] [blame] | 236 | |
| 237 | Modifier[0] = *LastEmitted; |
| 238 | ++LastEmitted; // Consume modifier character. |
| 239 | } |
| 240 | |
Chris Lattner | 4c842dd | 2010-04-05 22:42:30 +0000 | [diff] [blame] | 241 | if (*LastEmitted != '}') |
Chris Lattner | 736e31d | 2010-04-04 18:34:07 +0000 | [diff] [blame] | 242 | llvm_report_error("Bad ${} expression in inline asm string: '" |
| 243 | + std::string(AsmStr) + "'"); |
Chris Lattner | 736e31d | 2010-04-04 18:34:07 +0000 | [diff] [blame] | 244 | ++LastEmitted; // Consume '}' character. |
| 245 | } |
| 246 | |
Chris Lattner | 4c842dd | 2010-04-05 22:42:30 +0000 | [diff] [blame] | 247 | if (Val >= NumOperands-1) |
Chris Lattner | 736e31d | 2010-04-04 18:34:07 +0000 | [diff] [blame] | 248 | llvm_report_error("Invalid $ operand number in inline asm string: '" |
| 249 | + std::string(AsmStr) + "'"); |
Chris Lattner | 736e31d | 2010-04-04 18:34:07 +0000 | [diff] [blame] | 250 | |
| 251 | // Okay, we finally have a value number. Ask the target to print this |
| 252 | // operand! |
| 253 | if (CurVariant == -1 || CurVariant == AsmPrinterVariant) { |
| 254 | unsigned OpNo = 1; |
| 255 | |
| 256 | bool Error = false; |
| 257 | |
| 258 | // Scan to find the machine operand number for the operand. |
| 259 | for (; Val; --Val) { |
| 260 | if (OpNo >= MI->getNumOperands()) break; |
| 261 | unsigned OpFlags = MI->getOperand(OpNo).getImm(); |
| 262 | OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1; |
| 263 | } |
| 264 | |
| 265 | if (OpNo >= MI->getNumOperands()) { |
| 266 | Error = true; |
| 267 | } else { |
| 268 | unsigned OpFlags = MI->getOperand(OpNo).getImm(); |
| 269 | ++OpNo; // Skip over the ID number. |
| 270 | |
| 271 | if (Modifier[0] == 'l') // labels are target independent |
| 272 | // FIXME: What if the operand isn't an MBB, report error? |
| 273 | OS << *MI->getOperand(OpNo).getMBB()->getSymbol(); |
| 274 | else { |
| 275 | AsmPrinter *AP = const_cast<AsmPrinter*>(this); |
| 276 | if ((OpFlags & 7) == 4) { |
| 277 | Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant, |
| 278 | Modifier[0] ? Modifier : 0, |
| 279 | OS); |
| 280 | } else { |
| 281 | Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant, |
| 282 | Modifier[0] ? Modifier : 0, OS); |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | if (Error) { |
| 287 | std::string msg; |
| 288 | raw_string_ostream Msg(msg); |
| 289 | Msg << "Invalid operand found in inline asm: '" << AsmStr << "'\n"; |
| 290 | MI->print(Msg); |
| 291 | llvm_report_error(Msg.str()); |
| 292 | } |
| 293 | } |
| 294 | break; |
| 295 | } |
| 296 | } |
| 297 | } |
Chris Lattner | af632c9 | 2010-04-05 23:11:24 +0000 | [diff] [blame] | 298 | OS << '\n' << (char)0; // null terminate string. |
Chris Lattner | 885d941 | 2010-04-06 00:58:50 +0000 | [diff] [blame^] | 299 | EmitInlineAsm(OS.str(), 0/*no loc cookie*/); |
Chris Lattner | 736e31d | 2010-04-04 18:34:07 +0000 | [diff] [blame] | 300 | |
| 301 | // Emit the #NOAPP end marker. This has to happen even if verbose-asm isn't |
| 302 | // enabled, so we use EmitRawText. |
| 303 | if (OutStreamer.hasRawTextSupport()) |
| 304 | OutStreamer.EmitRawText(Twine("\t")+MAI->getCommentString()+ |
| 305 | MAI->getInlineAsmEnd()); |
| 306 | } |
| 307 | |
| 308 | |
| 309 | /// PrintSpecial - Print information related to the specified machine instr |
| 310 | /// that is independent of the operand, and may be independent of the instr |
| 311 | /// itself. This can be useful for portably encoding the comment character |
| 312 | /// or other bits of target-specific knowledge into the asmstrings. The |
| 313 | /// syntax used is ${:comment}. Targets can override this to add support |
| 314 | /// for their own strange codes. |
| 315 | void AsmPrinter::PrintSpecial(const MachineInstr *MI, raw_ostream &OS, |
| 316 | const char *Code) const { |
| 317 | if (!strcmp(Code, "private")) { |
| 318 | OS << MAI->getPrivateGlobalPrefix(); |
| 319 | } else if (!strcmp(Code, "comment")) { |
| 320 | OS << MAI->getCommentString(); |
| 321 | } else if (!strcmp(Code, "uid")) { |
| 322 | // Comparing the address of MI isn't sufficient, because machineinstrs may |
| 323 | // be allocated to the same address across functions. |
| 324 | |
| 325 | // If this is a new LastFn instruction, bump the counter. |
| 326 | if (LastMI != MI || LastFn != getFunctionNumber()) { |
| 327 | ++Counter; |
| 328 | LastMI = MI; |
| 329 | LastFn = getFunctionNumber(); |
| 330 | } |
| 331 | OS << Counter; |
| 332 | } else { |
| 333 | std::string msg; |
| 334 | raw_string_ostream Msg(msg); |
| 335 | Msg << "Unknown special formatter '" << Code |
| 336 | << "' for machine instr: " << *MI; |
| 337 | llvm_report_error(Msg.str()); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM |
| 342 | /// instruction, using the specified assembler variant. Targets should |
| 343 | /// override this to format as appropriate. |
| 344 | bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, |
| 345 | unsigned AsmVariant, const char *ExtraCode, |
| 346 | raw_ostream &O) { |
| 347 | // Target doesn't support this yet! |
| 348 | return true; |
| 349 | } |
| 350 | |
| 351 | bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo, |
| 352 | unsigned AsmVariant, |
| 353 | const char *ExtraCode, raw_ostream &O) { |
| 354 | // Target doesn't support this yet! |
| 355 | return true; |
| 356 | } |
| 357 | |