Add support for asm printing machine instructions that have operands.

llvm-svn: 15391
This commit is contained in:
Chris Lattner 2004-08-01 07:43:02 +00:00
parent 5572682faa
commit 65512a0c49
1 changed files with 48 additions and 2 deletions

View File

@ -17,6 +17,13 @@
#include <ostream>
using namespace llvm;
static bool isIdentChar(char C) {
return (C >= 'a' && C <= 'z') ||
(C >= 'A' && C <= 'Z') ||
(C >= '0' && C <= '9') ||
C == '_';
}
void AsmWriterEmitter::run(std::ostream &O) {
EmitSourceFileHeader("Assembly Writer Source Fragment", O);
@ -37,8 +44,47 @@ void AsmWriterEmitter::run(std::ostream &O) {
E = Target.inst_end(); I != E; ++I)
if (!I->second.AsmString.empty()) {
const std::string &AsmString = I->second.AsmString;
O << " case " << Namespace << "::" << I->first << ": O << \""
<< AsmString << "\" << '\\n'; break;\n";
O << " case " << Namespace << "::" << I->first << ": O ";
std::string::size_type LastEmitted = 0;
while (LastEmitted != AsmString.size()) {
std::string::size_type DollarPos = AsmString.find('$', LastEmitted);
if (DollarPos == std::string::npos) DollarPos = AsmString.size();
// Emit a constant string fragment.
if (DollarPos != LastEmitted) {
// TODO: this should eventually handle escaping.
O << " << \"" << std::string(AsmString.begin()+LastEmitted,
AsmString.begin()+DollarPos) << "\"";
LastEmitted = DollarPos;
} else if (DollarPos+1 != AsmString.size() &&
AsmString[DollarPos+1] == '$') {
O << " << '$'"; // "$$" -> $
} else {
// Get the name of the variable.
// TODO: should eventually handle ${foo}bar as $foo
std::string::size_type VarEnd = DollarPos+1;
while (VarEnd < AsmString.size() && isIdentChar(AsmString[VarEnd]))
++VarEnd;
std::string VarName(AsmString.begin()+DollarPos+1,
AsmString.begin()+VarEnd);
if (VarName.empty())
throw "Stray '$' in '" + I->first +
"' asm string, maybe you want $$?";
unsigned OpNo = I->second.getOperandNamed(VarName);
// If this is a two-address instruction and we are not accessing the
// 0th operand, remove an operand.
if (I->second.isTwoAddress && OpNo != 0)
--OpNo;
O << "; printOperand(MI->getOperand(" << OpNo << "), MVT::"
<< getName(I->second.OperandList[OpNo].Ty) << "); O ";
LastEmitted = VarEnd;
}
}
O << " << '\\n'; break;\n";
}
O << " }\n"