Skip to content

Add diagnostic help for inline asm operand constraint 'H' #86910

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions llvm/include/llvm/CodeGen/AsmPrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -816,9 +816,11 @@ class AsmPrinter : public MachineFunctionPass {

/// Print the specified operand of MI, an INLINEASM instruction, using the
/// specified assembler variant. Targets should override this to format as
/// appropriate. This method can return true if the operand is erroneous.
/// appropriate. This method can return true if the operand is erroneous,
/// method can provide helpful diagnostic message in some case.
virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
const char *ExtraCode, raw_ostream &OS);
const char *ExtraCode, raw_ostream &OS,
std::string &ErrorMsg);

/// Print the specified operand of MI, an INLINEASM instruction, using the
/// specified assembler variant as an address. Targets should override this to
Expand Down
11 changes: 7 additions & 4 deletions llvm/lib/CodeGen/AsmPrinter/AsmPrinterInlineAsm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ static void EmitInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
// We may have a location metadata attached to the end of the
// instruction, and at no point should see metadata at any
// other point while processing. It's an error if so.
std::string ErrorMsg;
if (OpNo >= MI->getNumOperands() || MI->getOperand(OpNo).isMetadata()) {
Error = true;
} else {
Expand All @@ -306,14 +307,15 @@ static void EmitInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
Error = AP->PrintAsmMemoryOperand(
MI, OpNo, Modifier[0] ? Modifier : nullptr, OS);
} else {
Error = AP->PrintAsmOperand(MI, OpNo,
Modifier[0] ? Modifier : nullptr, OS);
Error = AP->PrintAsmOperand(
MI, OpNo, Modifier[0] ? Modifier : nullptr, OS, ErrorMsg);
}
}
if (Error) {
std::string msg;
raw_string_ostream Msg(msg);
Msg << "invalid operand in inline asm: '" << AsmStr << "'";
Msg << "invalid operand in inline asm: '" << AsmStr << "'"
<< ErrorMsg;
MMI->getModule()->getContext().emitError(LocCookie, Msg.str());
}
}
Expand Down Expand Up @@ -466,7 +468,8 @@ void AsmPrinter::PrintSymbolOperand(const MachineOperand &MO, raw_ostream &OS) {
/// override this to format as appropriate for machine specific ExtraCodes
/// or when the arch-independent handling would be too complex otherwise.
bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
const char *ExtraCode, raw_ostream &O) {
const char *ExtraCode, raw_ostream &O,
std::string &ErrorMsg) {
// Does this asm operand have a single letter operand modifier?
if (ExtraCode && ExtraCode[0]) {
if (ExtraCode[1] != 0) return true; // Unknown modifier.
Expand Down
8 changes: 5 additions & 3 deletions llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ class AArch64AsmPrinter : public AsmPrinter {
raw_ostream &O);

bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
const char *ExtraCode, raw_ostream &O) override;
const char *ExtraCode, raw_ostream &O,
std::string &ErrorMsg) override;
bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNum,
const char *ExtraCode, raw_ostream &O) override;

Expand Down Expand Up @@ -910,11 +911,12 @@ bool AArch64AsmPrinter::printAsmRegInClass(const MachineOperand &MO,
}

bool AArch64AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
const char *ExtraCode, raw_ostream &O) {
const char *ExtraCode, raw_ostream &O,
std::string &ErrorMsg) {
const MachineOperand &MO = MI->getOperand(OpNum);

// First try the generic code, which knows about modifiers like 'c' and 'n'.
if (!AsmPrinter::PrintAsmOperand(MI, OpNum, ExtraCode, O))
if (!AsmPrinter::PrintAsmOperand(MI, OpNum, ExtraCode, O, ErrorMsg))
return false;

// Does this asm operand have a single letter operand modifier?
Expand Down
5 changes: 3 additions & 2 deletions llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1235,9 +1235,10 @@ void AMDGPUAsmPrinter::getAmdKernelCode(amd_kernel_code_t &Out,
}

bool AMDGPUAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
const char *ExtraCode, raw_ostream &O) {
const char *ExtraCode, raw_ostream &O,
std::string &ErrorMsg) {
// First try the generic code, which knows about modifiers like 'c' and 'n'.
if (!AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, O))
if (!AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, O, ErrorMsg))
return false;

if (ExtraCode && ExtraCode[0]) {
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ class AMDGPUAsmPrinter final : public AsmPrinter {
void emitEndOfAsmFile(Module &M) override;

bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
const char *ExtraCode, raw_ostream &O) override;
const char *ExtraCode, raw_ostream &O,
std::string &ErrorMsg) override;

protected:
void getAnalysisUsage(AnalysisUsage &AU) const override;
Expand Down
5 changes: 3 additions & 2 deletions llvm/lib/Target/ARM/ARMAsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,15 +284,16 @@ GetARMJTIPICJumpTableLabel(unsigned uid) const {
}

bool ARMAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
const char *ExtraCode, raw_ostream &O) {
const char *ExtraCode, raw_ostream &O,
std::string &ErrorMsg) {
// Does this asm operand have a single letter operand modifier?
if (ExtraCode && ExtraCode[0]) {
if (ExtraCode[1] != 0) return true; // Unknown modifier.

switch (ExtraCode[0]) {
default:
// See if this is a generic print operand
return AsmPrinter::PrintAsmOperand(MI, OpNum, ExtraCode, O);
return AsmPrinter::PrintAsmOperand(MI, OpNum, ExtraCode, O, ErrorMsg);
case 'P': // Print a VFP double precision register.
case 'q': // Print a NEON quad precision register.
printOperand(MI, OpNum, O);
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/Target/ARM/ARMAsmPrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ class LLVM_LIBRARY_VISIBILITY ARMAsmPrinter : public AsmPrinter {

void PrintSymbolOperand(const MachineOperand &MO, raw_ostream &O) override;
bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
const char *ExtraCode, raw_ostream &O) override;
const char *ExtraCode, raw_ostream &O,
std::string &ErrorMsg) override;
bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNum,
const char *ExtraCode, raw_ostream &O) override;

Expand Down
8 changes: 5 additions & 3 deletions llvm/lib/Target/AVR/AVRAsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ class AVRAsmPrinter : public AsmPrinter {
void printOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O);

bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
const char *ExtraCode, raw_ostream &O) override;
const char *ExtraCode, raw_ostream &O,
std::string &ErrorMsg) override;

bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNum,
const char *ExtraCode, raw_ostream &O) override;
Expand Down Expand Up @@ -98,10 +99,11 @@ void AVRAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
}

bool AVRAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
const char *ExtraCode, raw_ostream &O) {
const char *ExtraCode, raw_ostream &O,
std::string &ErrorMsg) {
// Default asm printer can only deal with some extra codes,
// so try it first.
if (!AsmPrinter::PrintAsmOperand(MI, OpNum, ExtraCode, O))
if (!AsmPrinter::PrintAsmOperand(MI, OpNum, ExtraCode, O, ErrorMsg))
return false;

const MachineOperand &MO = MI->getOperand(OpNum);
Expand Down
8 changes: 5 additions & 3 deletions llvm/lib/Target/BPF/BPFAsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ class BPFAsmPrinter : public AsmPrinter {
bool doInitialization(Module &M) override;
void printOperand(const MachineInstr *MI, int OpNum, raw_ostream &O);
bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
const char *ExtraCode, raw_ostream &O) override;
const char *ExtraCode, raw_ostream &O,
std::string &ErrorMsg) override;
bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNum,
const char *ExtraCode, raw_ostream &O) override;

Expand Down Expand Up @@ -108,9 +109,10 @@ void BPFAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
}

bool BPFAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
const char *ExtraCode, raw_ostream &O) {
const char *ExtraCode, raw_ostream &O,
std::string &ErrorMsg) {
if (ExtraCode && ExtraCode[0])
return AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, O);
return AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, O, ErrorMsg);

printOperand(MI, OpNo, O);
return false;
Expand Down
5 changes: 3 additions & 2 deletions llvm/lib/Target/CSKY/CSKYAsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,10 @@ void CSKYAsmPrinter::emitAttributes() {
}

bool CSKYAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
const char *ExtraCode, raw_ostream &OS) {
const char *ExtraCode, raw_ostream &OS,
std::string &ErrorMsg) {
// First try the generic code, which knows about modifiers like 'c' and 'n'.
if (!AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, OS))
if (!AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, OS, ErrorMsg))
return false;

const MachineOperand &MO = MI->getOperand(OpNo);
Expand Down
6 changes: 3 additions & 3 deletions llvm/lib/Target/Hexagon/HexagonAsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ bool HexagonAsmPrinter::isBlockOnlyReachableByFallthrough(

/// PrintAsmOperand - Print out an operand for an inline asm expression.
bool HexagonAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
const char *ExtraCode,
raw_ostream &OS) {
const char *ExtraCode, raw_ostream &OS,
std::string &ErrorMsg) {
// Does this asm operand have a single letter operand modifier?
if (ExtraCode && ExtraCode[0]) {
if (ExtraCode[1] != 0)
Expand All @@ -123,7 +123,7 @@ bool HexagonAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
switch (ExtraCode[0]) {
default:
// See if this is a generic print operand
return AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, OS);
return AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, OS, ErrorMsg);
case 'L':
case 'H': { // The highest-numbered register of a pair.
const MachineOperand &MO = MI->getOperand(OpNo);
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/Target/Hexagon/HexagonAsmPrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ class TargetMachine;

void printOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O);
bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
const char *ExtraCode, raw_ostream &OS) override;
const char *ExtraCode, raw_ostream &OS,
std::string &ErrorMsg) override;
bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
const char *ExtraCode, raw_ostream &OS) override;
void emitStartOfAsmFile(Module &M) override;
Expand Down
8 changes: 5 additions & 3 deletions llvm/lib/Target/Lanai/LanaiAsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ class LanaiAsmPrinter : public AsmPrinter {

void printOperand(const MachineInstr *MI, int OpNum, raw_ostream &O);
bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
const char *ExtraCode, raw_ostream &O) override;
const char *ExtraCode, raw_ostream &O,
std::string &ErrorMsg) override;
void emitInstruction(const MachineInstr *MI) override;
bool isBlockOnlyReachableByFallthrough(
const MachineBasicBlock *MBB) const override;
Expand Down Expand Up @@ -109,7 +110,8 @@ void LanaiAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,

// PrintAsmOperand - Print out an operand for an inline asm expression.
bool LanaiAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
const char *ExtraCode, raw_ostream &O) {
const char *ExtraCode, raw_ostream &O,
std::string &ErrorMsg) {
// Does this asm operand have a single letter operand modifier?
if (ExtraCode && ExtraCode[0]) {
if (ExtraCode[1])
Expand Down Expand Up @@ -138,7 +140,7 @@ bool LanaiAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
return false;
}
default:
return AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, O);
return AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, O, ErrorMsg);
}
}
printOperand(MI, OpNo, O);
Expand Down
5 changes: 3 additions & 2 deletions llvm/lib/Target/LoongArch/LoongArchAsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ void LoongArchAsmPrinter::emitInstruction(const MachineInstr *MI) {

bool LoongArchAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
const char *ExtraCode,
raw_ostream &OS) {
raw_ostream &OS,
std::string &ErrorMsg) {
// First try the generic code, which knows about modifiers like 'c' and 'n'.
if (!AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, OS))
if (!AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, OS, ErrorMsg))
return false;

const MachineOperand &MO = MI->getOperand(OpNo);
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/Target/LoongArch/LoongArchAsmPrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ class LLVM_LIBRARY_VISIBILITY LoongArchAsmPrinter : public AsmPrinter {
void emitInstruction(const MachineInstr *MI) override;

bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
const char *ExtraCode, raw_ostream &OS) override;
const char *ExtraCode, raw_ostream &OS,
std::string &ErrorMsg) override;
bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
const char *ExtraCode, raw_ostream &OS) override;

Expand Down
5 changes: 3 additions & 2 deletions llvm/lib/Target/M68k/M68kAsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,16 @@ void M68kAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
}

bool M68kAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
const char *ExtraCode, raw_ostream &OS) {
const char *ExtraCode, raw_ostream &OS,
std::string &ErrorMsg) {
// Print the operand if there is no operand modifier.
if (!ExtraCode || !ExtraCode[0]) {
printOperand(MI, OpNo, OS);
return false;
}

// Fallback to the default implementation.
return AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, OS);
return AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, OS, ErrorMsg);
}

void M68kAsmPrinter::printDisp(const MachineInstr *MI, unsigned opNum,
Expand Down
8 changes: 5 additions & 3 deletions llvm/lib/Target/MSP430/MSP430AsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ namespace {
void printSrcMemOperand(const MachineInstr *MI, int OpNum,
raw_ostream &O);
bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
const char *ExtraCode, raw_ostream &O) override;
const char *ExtraCode, raw_ostream &O,
std::string &ErrorMsg) override;
bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
const char *ExtraCode, raw_ostream &O) override;
void emitInstruction(const MachineInstr *MI) override;
Expand Down Expand Up @@ -127,10 +128,11 @@ void MSP430AsmPrinter::printSrcMemOperand(const MachineInstr *MI, int OpNum,
/// PrintAsmOperand - Print out an operand for an inline asm expression.
///
bool MSP430AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
const char *ExtraCode, raw_ostream &O) {
const char *ExtraCode, raw_ostream &O,
std::string &ErrorMsg) {
// Does this asm operand have a single letter operand modifier?
if (ExtraCode && ExtraCode[0])
return AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, O);
return AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, O, ErrorMsg);

printOperand(MI, OpNo, O);
return false;
Expand Down
5 changes: 3 additions & 2 deletions llvm/lib/Target/Mips/MipsAsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,8 @@ void MipsAsmPrinter::emitBasicBlockEnd(const MachineBasicBlock &MBB) {

// Print out an operand for an inline asm expression.
bool MipsAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
const char *ExtraCode, raw_ostream &O) {
const char *ExtraCode, raw_ostream &O,
std::string &ErrorMsg) {
// Does this asm operand have a single letter operand modifier?
if (ExtraCode && ExtraCode[0]) {
if (ExtraCode[1] != 0) return true; // Unknown modifier.
Expand All @@ -482,7 +483,7 @@ bool MipsAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
switch (ExtraCode[0]) {
default:
// See if this is a generic print operand
return AsmPrinter::PrintAsmOperand(MI, OpNum, ExtraCode, O);
return AsmPrinter::PrintAsmOperand(MI, OpNum, ExtraCode, O, ErrorMsg);
case 'X': // hex const int
if (!MO.isImm())
return true;
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/Target/Mips/MipsAsmPrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ class LLVM_LIBRARY_VISIBILITY MipsAsmPrinter : public AsmPrinter {
void emitFunctionBodyEnd() override;
void emitBasicBlockEnd(const MachineBasicBlock &MBB) override;
bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
const char *ExtraCode, raw_ostream &O) override;
const char *ExtraCode, raw_ostream &O,
std::string &ErrorMsg) override;
bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNum,
const char *ExtraCode, raw_ostream &O) override;
void printOperand(const MachineInstr *MI, int opNum, raw_ostream &O);
Expand Down
5 changes: 3 additions & 2 deletions llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2169,15 +2169,16 @@ void NVPTXAsmPrinter::printMCExpr(const MCExpr &Expr, raw_ostream &OS) {
/// PrintAsmOperand - Print out an operand for an inline asm expression.
///
bool NVPTXAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
const char *ExtraCode, raw_ostream &O) {
const char *ExtraCode, raw_ostream &O,
std::string &ErrorMsg) {
if (ExtraCode && ExtraCode[0]) {
if (ExtraCode[1] != 0)
return true; // Unknown modifier.

switch (ExtraCode[0]) {
default:
// See if this is a generic print operand
return AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, O);
return AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, O, ErrorMsg);
case 'r':
break;
}
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/Target/NVPTX/NVPTXAsmPrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ class LLVM_LIBRARY_VISIBILITY NVPTXAsmPrinter : public AsmPrinter {
void printReturnValStr(const Function *, raw_ostream &O);
void printReturnValStr(const MachineFunction &MF, raw_ostream &O);
bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
const char *ExtraCode, raw_ostream &) override;
const char *ExtraCode, raw_ostream &,
std::string &ErrorMsg) override;
void printOperand(const MachineInstr *MI, unsigned OpNum, raw_ostream &O);
bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
const char *ExtraCode, raw_ostream &) override;
Expand Down
Loading