39 #define DEBUG_TYPE "asm-printer" 47 assert(DiagInfo &&
"Diagnostic context not passed down?");
51 const MDNode *LocInfo =
nullptr;
52 if (BufNum > 0 && BufNum <= DiagInfo->LocInfos.size())
53 LocInfo = DiagInfo->
LocInfos[BufNum-1];
57 unsigned LocCookie = 0;
65 mdconst::dyn_extract<ConstantInt>(LocInfo->
getOperand(ErrorLine)))
66 LocCookie = CI->getZExtValue();
72 unsigned AsmPrinter::addInlineAsmDiagBuffer(
StringRef AsmStr,
73 const MDNode *LocMDNode)
const {
75 DiagInfo = std::make_unique<SrcMgrDiagInfo>();
90 std::unique_ptr<MemoryBuffer> Buffer;
100 DiagInfo->LocInfos.resize(BufNum);
101 DiagInfo->LocInfos[BufNum - 1] = LocMDNode;
113 assert(!Str.
empty() &&
"Can't emit empty inline asm block");
116 bool isNullTerminated = Str.
back() == 0;
117 if (isNullTerminated)
126 assert(MCAI &&
"No MCAsmInfo");
135 unsigned BufNum = addInlineAsmDiagBuffer(Str, LocMDNode);
150 STI, *Parser, *MII, MCOptions));
153 " we don't have an asm parser for this target\n");
154 Parser->setAssemblerDialect(Dialect);
155 Parser->setTargetParser(*TAP.get());
159 Parser->getLexer().setLexMasmIntegers(
true);
163 int Res = Parser->Run(
true,
167 if (Res && !DiagInfo->DiagHandler)
175 OS <<
"\t.intel_syntax\n\t";
177 const char *LastEmitted = AsmStr;
180 while (*LastEmitted) {
181 switch (*LastEmitted) {
184 const char *LiteralEnd = LastEmitted+1;
185 while (*LiteralEnd && *LiteralEnd !=
'{' && *LiteralEnd !=
'|' &&
186 *LiteralEnd !=
'}' && *LiteralEnd !=
'$' && *LiteralEnd !=
'\n')
189 OS.
write(LastEmitted, LiteralEnd-LastEmitted);
190 LastEmitted = LiteralEnd;
202 switch (*LastEmitted) {
203 default: Done =
false;
break;
210 bool HasCurlyBraces =
false;
211 if (*LastEmitted ==
'{') {
213 HasCurlyBraces =
true;
219 if (HasCurlyBraces && LastEmitted[0] ==
':') {
221 const char *StrStart = LastEmitted;
222 const char *StrEnd = strchr(StrStart,
'}');
225 " string: '" +
Twine(AsmStr) +
"'");
227 std::string Val(StrStart, StrEnd);
229 LastEmitted = StrEnd+1;
233 const char *IDStart = LastEmitted;
234 const char *IDEnd = IDStart;
235 while (*IDEnd >=
'0' && *IDEnd <=
'9') ++IDEnd;
240 Twine(AsmStr) +
"'");
243 if (Val >= NumOperands-1)
245 Twine(AsmStr) +
"'");
247 char Modifier[2] = { 0, 0 };
249 if (HasCurlyBraces) {
252 if (*LastEmitted ==
':') {
254 if (*LastEmitted == 0)
256 Twine(AsmStr) +
"'");
258 Modifier[0] = *LastEmitted;
262 if (*LastEmitted !=
'}')
264 Twine(AsmStr) +
"'");
293 MI, OpNo, Modifier[0] ? Modifier :
nullptr, OS);
296 Modifier[0] ? Modifier :
nullptr, OS);
302 Msg <<
"invalid operand in inline asm: '" << AsmStr <<
"'";
309 OS <<
"\n\t.att_syntax\n" << (
char)0;
317 const char *LastEmitted = AsmStr;
322 while (*LastEmitted) {
323 switch (*LastEmitted) {
326 const char *LiteralEnd = LastEmitted+1;
327 while (*LiteralEnd && *LiteralEnd !=
'{' && *LiteralEnd !=
'|' &&
328 *LiteralEnd !=
'}' && *LiteralEnd !=
'$' && *LiteralEnd !=
'\n')
330 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
331 OS.
write(LastEmitted, LiteralEnd-LastEmitted);
332 LastEmitted = LiteralEnd;
344 switch (*LastEmitted) {
345 default: Done =
false;
break;
347 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
353 if (CurVariant != -1)
355 Twine(AsmStr) +
"'");
360 if (CurVariant == -1)
367 if (CurVariant == -1)
375 bool HasCurlyBraces =
false;
376 if (*LastEmitted ==
'{') {
378 HasCurlyBraces =
true;
384 if (HasCurlyBraces && *LastEmitted ==
':') {
386 const char *StrStart = LastEmitted;
387 const char *StrEnd = strchr(StrStart,
'}');
390 " string: '" +
Twine(AsmStr) +
"'");
392 std::string Val(StrStart, StrEnd);
394 LastEmitted = StrEnd+1;
398 const char *IDStart = LastEmitted;
399 const char *IDEnd = IDStart;
400 while (*IDEnd >=
'0' && *IDEnd <=
'9') ++IDEnd;
405 Twine(AsmStr) +
"'");
408 char Modifier[2] = { 0, 0 };
410 if (HasCurlyBraces) {
413 if (*LastEmitted ==
':') {
415 if (*LastEmitted == 0)
417 Twine(AsmStr) +
"'");
419 Modifier[0] = *LastEmitted;
423 if (*LastEmitted !=
'}')
425 Twine(AsmStr) +
"'");
429 if (Val >= NumOperands-1)
431 Twine(AsmStr) +
"'");
435 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
468 }
else if (Modifier[0] ==
'l') {
472 MI, OpNo, Modifier[0] ? Modifier :
nullptr, OS);
475 Modifier[0] ? Modifier :
nullptr, OS);
481 Msg <<
"invalid operand in inline asm: '" << AsmStr <<
"'";
489 OS <<
'\n' << (
char)0;
498 unsigned NumDefs = 0;
510 if (AsmStr[0] == 0) {
522 unsigned LocCookie = 0;
523 const MDNode *LocMD =
nullptr;
529 mdconst::dyn_extract<ConstantInt>(LocMD->
getOperand(0))) {
530 LocCookie = CI->getZExtValue();
551 std::vector<std::string> RestrRegs;
557 unsigned Flags = MO.
getImm();
568 if (!RestrRegs.empty()) {
569 unsigned BufNum = addInlineAsmDiagBuffer(OS.
str(), LocMD);
570 auto &
SrcMgr = DiagInfo->SrcMgr;
572 SrcMgr.getMemoryBuffer(BufNum)->getBuffer().begin());
574 std::string Msg =
"inline asm clobber list contains reserved registers: ";
575 for (
auto I = RestrRegs.begin(),
E = RestrRegs.end();
I !=
E;
I++) {
576 if(
I != RestrRegs.begin())
580 std::string
Note =
"Reserved registers on the clobber list may not be " 581 "preserved across the asm statement, and clobbering them may " 582 "lead to undefined behaviour.";
603 const char *Code)
const {
604 if (!strcmp(Code,
"private")) {
607 }
else if (!strcmp(Code,
"comment")) {
609 }
else if (!strcmp(Code,
"uid")) {
623 Msg <<
"Unknown special formatter '" << Code
624 <<
"' for machine instr: " << *
MI;
642 if (ExtraCode && ExtraCode[0]) {
643 if (ExtraCode[1] != 0)
return true;
647 switch (ExtraCode[0]) {
674 O << ((32 - MO.
getImm()) & 31);
virtual void emitInlineAsmStart() const
Let the target do anything it needs to do before emitting inlineasm.
A parsed version of the target data layout string in and methods for querying it. ...
const char * getInlineAsmStart() const
unsigned FindBufferContainingLoc(SMLoc Loc) const
Return the ID of the buffer containing the specified location.
MachineBasicBlock * getMBB() const
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
MCTargetOptions MCOptions
Machine level options.
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
This class represents lattice values for constants.
StringRef getPrivateGlobalPrefix() const
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
std::vector< std::string > IASSearchPaths
Additional paths to search for .include directives when using the integrated assembler.
MCContext & OutContext
This is the context for the output file that we are streaming.
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
const MCSubtargetInfo & getSubtargetInfo() const
Return information about subtarget.
MCInstrInfo * createMCInstrInfo() const
createMCInstrInfo - Create a MCInstrInfo implementation.
void * getInlineAsmDiagnosticContext() const
getInlineAsmDiagnosticContext - Return the diagnostic context set by setInlineAsmDiagnosticHandler.
A raw_ostream that writes to an SmallVector or SmallString.
bool isMetadata() const
isMetadata - Tests if this is a MO_Metadata operand.
unsigned const TargetRegisterInfo * TRI
MachineFunction * MF
The current machine function.
const MDOperand & getOperand(unsigned I) const
const char * getInlineAsmEnd() const
LLVMContext::InlineAsmDiagHandlerTy DiagHandler
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
static void EmitGCCInlineAsmStr(const char *AsmStr, const MachineInstr *MI, MachineModuleInfo *MMI, int AsmPrinterVariant, AsmPrinter *AP, unsigned LocCookie, raw_ostream &OS)
MCAsmParser * createMCAsmParser(SourceMgr &, MCContext &, MCStreamer &, const MCAsmInfo &, unsigned CB=0)
Create an MCAsmParser instance.
The address of a basic block.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
unsigned getNumOperands() const
Retuns the total number of operands.
LLVMContext & getContext() const
Get the global data context.
static void EmitMSInlineAsmStr(const char *AsmStr, const MachineInstr *MI, MachineModuleInfo *MMI, AsmPrinter *AP, unsigned LocCookie, raw_ostream &OS)
const char * getSymbolName() const
LLVM_NODISCARD StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Context object for machine code objects.
void emitError(unsigned LocCookie, const Twine &ErrorStr)
emitError - Emit an error message to the currently installed error handler with optional location inf...
const MCContext & getContext() const
unsigned AddNewSourceBuffer(std::unique_ptr< MemoryBuffer > F, SMLoc IncludeLoc)
Add a new source buffer to this source manager.
LLVM_NODISCARD size_t size() const
size - Get the string size.
void printOffset(int64_t Offset, raw_ostream &OS) const
This is just convenient handler for printing offsets.
MachineModuleInfo * MMI
This is a pointer to the current MachineModuleInfo.
const DataLayout & getDataLayout() const
Return the DataLayout attached to the Module associated to this MF.
This class is intended to be used as a base class for asm properties and features specific to the tar...
void setInlineSourceManager(SourceMgr *SM)
* if(!EatIfPresent(lltok::kw_thread_local)) return false
ParseOptionalThreadLocal := /*empty.
const MCAsmInfo * MAI
Target Asm Printer information.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
This is an important class for using LLVM in a threaded context.
unsigned getAssemblerDialect() const
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
const GlobalValue * getGlobal() const
const MCAsmInfo * getMCAsmInfo() const
Return target specific asm information.
TargetMachine & TM
Target machine description.
This class is intended to be used as a driving class for all asm writers.
static unsigned getNumOperandRegisters(unsigned Flag)
getNumOperandRegisters - Extract the number of registers field from the inline asm operand flag...
StringRef getCommentString() const
static unsigned getKind(unsigned Flags)
static void srcMgrDiagHandler(const SMDiagnostic &Diag, void *diagInfo)
srcMgrDiagHandler - This callback is invoked when the SourceMgr for an inline asm has an error in it...
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling...
const Target & getTarget() const
static bool isMemKind(unsigned Flag)
MCTargetAsmParser * createMCAsmParser(const MCSubtargetInfo &STI, MCAsmParser &Parser, const MCInstrInfo &MII, const MCTargetOptions &Options) const
createMCAsmParser - Create a target specific assembly parser.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
LLVM_NODISCARD char back() const
back - Get the last character in the string.
std::string & str()
Flushes the stream contents to the target string and returns the string's reference.
raw_ostream & write(unsigned char C)
std::enable_if< std::numeric_limits< T >::is_signed, bool >::type getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
void registerInlineAsmLabel(MCSymbol *Sym)
registerInlineAsmLabel - Records that the name is a label referenced in inline assembly.
unsigned getFunctionNumber() const
Return a unique ID for the current function.
MCSymbol * GetBlockAddressSymbol(const BlockAddress *BA) const
Return the MCSymbol used to satisfy BlockAddress uses of the specified basic block.
InlineAsm::AsmDialect getInlineAsmDialect() const
This is the shared class of boolean and integer constants.
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
MCSymbol * getSymbol(const GlobalValue *GV) const
MachineOperand class - Representation of each machine instruction operand.
Module.h This file contains the declarations for the Module class.
virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, const char *ExtraCode, raw_ostream &OS)
Print the specified operand of MI, an INLINEASM instruction, using the specified assembler variant...
virtual void emitInlineAsmEnd(const MCSubtargetInfo &StartInfo, const MCSubtargetInfo *EndInfo) const
Let the target do anything it needs to do after emitting inlineasm.
StringRef str()
Return a StringRef for the vector contents.
bool isBlockAddress() const
isBlockAddress - Tests if this is a MO_BlockAddress operand.
static std::unique_ptr< MemoryBuffer > getMemBufferCopy(StringRef InputData, const Twine &BufferName="")
Open the specified memory range as a MemoryBuffer, copying the contents and taking ownership of it...
const char * getName(MCRegister RegNo) const
Return the human-readable symbolic target-specific name for the specified physical register...
virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo, const char *ExtraCode, raw_ostream &OS)
Print the specified operand of MI, an INLINEASM instruction, using the specified assembler variant as...
bool useIntegratedAssembler() const
Return true if assembly (inline or otherwise) should be parsed.
static SMLoc getFromPointer(const char *Ptr)
Representation of each machine instruction.
int64_t getOffset() const
Return the offset from the symbol in this operand.
const BlockAddress * getBlockAddress() const
MCSymbol * getSymbol() const
Return the MCSymbol for this basic block.
Generic base class for all target subtargets.
virtual void print(raw_ostream &OS, const Module *M) const
print - Print out the internal state of the pass.
virtual void PrintSymbolOperand(const MachineOperand &MO, raw_ostream &OS)
Print the MachineOperand as a symbol.
InlineAsmDiagHandlerTy getInlineAsmDiagnosticHandler() const
getInlineAsmDiagnosticHandler - Return the diagnostic handler set by setInlineAsmDiagnosticHandler.
bool isSymbol() const
isSymbol - Tests if this is a MO_ExternalSymbol operand.
const Module * getModule() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
A raw_ostream that writes to an std::string.
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
Lightweight error class with error context and mandatory checking.
This class implements an extremely fast bulk output stream that can only output to a stream...
StringRef - Represent a constant reference to a string, i.e.
bool isMBB() const
isMBB - Tests if this is a MO_MachineBasicBlock operand.
Register getReg() const
getReg - Returns the register number.
virtual bool isAsmClobberable(const MachineFunction &MF, unsigned PhysReg) const
Returns false if we can't guarantee that Physreg, specified as an IR asm clobber constraint, will be preserved across the statement.
std::vector< const MDNode * > LocInfos
Represents a location in source code.
unsigned getNumOperands() const
Return number of MDNode operands.
const MachineOperand & getOperand(unsigned i) const
virtual void PrintSpecial(const MachineInstr *MI, raw_ostream &OS, const char *Code) const
Print information related to the specified machine instr that is independent of the operand...
const MDNode * getMetadata() const
This class contains meta information specific to a module.
Instances of this class encapsulate one diagnostic report, allowing printing to a raw_ostream as a ca...
void print(raw_ostream &OS, const MCAsmInfo *MAI) const
print - Print the value to the stream OS.