forked from OSchip/llvm-project
[AsmParser] Backends can parameterize ASM tokenization.
llvm-svn: 252439
This commit is contained in:
parent
87f5e80614
commit
8a0453e23a
|
@ -965,6 +965,15 @@ class AsmParserVariant {
|
||||||
// register tokens as constrained registers, instead of tokens, for the
|
// register tokens as constrained registers, instead of tokens, for the
|
||||||
// purposes of matching.
|
// purposes of matching.
|
||||||
string RegisterPrefix = "";
|
string RegisterPrefix = "";
|
||||||
|
|
||||||
|
// TokenizingCharacters - Characters that are standalone tokens
|
||||||
|
string TokenizingCharacters = "[]*!";
|
||||||
|
|
||||||
|
// SeparatorCharacters - Characters that are not tokens
|
||||||
|
string SeparatorCharacters = " \t,";
|
||||||
|
|
||||||
|
// BreakCharacters - Characters that start new identifiers
|
||||||
|
string BreakCharacters = "";
|
||||||
}
|
}
|
||||||
def DefaultAsmParserVariant : AsmParserVariant;
|
def DefaultAsmParserVariant : AsmParserVariant;
|
||||||
|
|
||||||
|
|
|
@ -1334,6 +1334,15 @@ bool AsmParser::parseStatement(ParseStatementInfo &Info,
|
||||||
// Treat '.' as a valid identifier in this context.
|
// Treat '.' as a valid identifier in this context.
|
||||||
Lex();
|
Lex();
|
||||||
IDVal = ".";
|
IDVal = ".";
|
||||||
|
} else if (Lexer.is(AsmToken::LCurly)) {
|
||||||
|
// Treat '{' as a valid identifier in this context.
|
||||||
|
Lex();
|
||||||
|
IDVal = "{";
|
||||||
|
|
||||||
|
} else if (Lexer.is(AsmToken::RCurly)) {
|
||||||
|
// Treat '}' as a valid identifier in this context.
|
||||||
|
Lex();
|
||||||
|
IDVal = "}";
|
||||||
} else if (parseIdentifier(IDVal)) {
|
} else if (parseIdentifier(IDVal)) {
|
||||||
if (!TheCondState.Ignore)
|
if (!TheCondState.Ignore)
|
||||||
return TokError("unexpected token at start of statement");
|
return TokError("unexpected token at start of statement");
|
||||||
|
|
|
@ -125,11 +125,13 @@ def : ProcessorModel<"cyclone", CycloneModel, [ProcCyclone]>;
|
||||||
def GenericAsmParserVariant : AsmParserVariant {
|
def GenericAsmParserVariant : AsmParserVariant {
|
||||||
int Variant = 0;
|
int Variant = 0;
|
||||||
string Name = "generic";
|
string Name = "generic";
|
||||||
|
string BreakCharacters = ".";
|
||||||
}
|
}
|
||||||
|
|
||||||
def AppleAsmParserVariant : AsmParserVariant {
|
def AppleAsmParserVariant : AsmParserVariant {
|
||||||
int Variant = 1;
|
int Variant = 1;
|
||||||
string Name = "apple-neon";
|
string Name = "apple-neon";
|
||||||
|
string BreakCharacters = ".";
|
||||||
}
|
}
|
||||||
|
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
|
@ -511,8 +511,15 @@ def ARMAsmWriter : AsmWriter {
|
||||||
bit isMCAsmWriter = 1;
|
bit isMCAsmWriter = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def ARMAsmParserVariant : AsmParserVariant {
|
||||||
|
int Variant = 0;
|
||||||
|
string Name = "ARM";
|
||||||
|
string BreakCharacters = ".";
|
||||||
|
}
|
||||||
|
|
||||||
def ARM : Target {
|
def ARM : Target {
|
||||||
// Pull in Instruction Info:
|
// Pull in Instruction Info:
|
||||||
let InstructionSet = ARMInstrInfo;
|
let InstructionSet = ARMInstrInfo;
|
||||||
let AssemblyWriters = [ARMAsmWriter];
|
let AssemblyWriters = [ARMAsmWriter];
|
||||||
|
let AssemblyParserVariants = [ARMAsmParserVariant];
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,7 +25,14 @@ def BPFInstPrinter : AsmWriter {
|
||||||
bit isMCAsmWriter = 1;
|
bit isMCAsmWriter = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def BPFAsmParserVariant : AsmParserVariant {
|
||||||
|
int Variant = 0;
|
||||||
|
string Name = "BPF";
|
||||||
|
string BreakCharacters = ".";
|
||||||
|
}
|
||||||
|
|
||||||
def BPF : Target {
|
def BPF : Target {
|
||||||
let InstructionSet = BPFInstrInfo;
|
let InstructionSet = BPFInstrInfo;
|
||||||
let AssemblyWriters = [BPFInstPrinter];
|
let AssemblyWriters = [BPFInstPrinter];
|
||||||
|
let AssemblyParserVariants = [BPFAsmParserVariant];
|
||||||
}
|
}
|
||||||
|
|
|
@ -403,6 +403,7 @@ def PPCAsmParserVariant : AsmParserVariant {
|
||||||
// InstAlias definitions use immediate literals. Set RegisterPrefix
|
// InstAlias definitions use immediate literals. Set RegisterPrefix
|
||||||
// so that those are not misinterpreted as registers.
|
// so that those are not misinterpreted as registers.
|
||||||
string RegisterPrefix = "%";
|
string RegisterPrefix = "%";
|
||||||
|
string BreakCharacters = ".";
|
||||||
}
|
}
|
||||||
|
|
||||||
def PPC : Target {
|
def PPC : Target {
|
||||||
|
|
|
@ -294,6 +294,13 @@ public:
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class AsmVariantInfo {
|
||||||
|
public:
|
||||||
|
std::string TokenizingCharacters;
|
||||||
|
std::string SeparatorCharacters;
|
||||||
|
std::string BreakCharacters;
|
||||||
|
};
|
||||||
|
|
||||||
/// MatchableInfo - Helper class for storing the necessary information for an
|
/// MatchableInfo - Helper class for storing the necessary information for an
|
||||||
/// instruction or alias which is capable of being matched.
|
/// instruction or alias which is capable of being matched.
|
||||||
struct MatchableInfo {
|
struct MatchableInfo {
|
||||||
|
@ -484,7 +491,8 @@ struct MatchableInfo {
|
||||||
|
|
||||||
void initialize(const AsmMatcherInfo &Info,
|
void initialize(const AsmMatcherInfo &Info,
|
||||||
SmallPtrSetImpl<Record*> &SingletonRegisters,
|
SmallPtrSetImpl<Record*> &SingletonRegisters,
|
||||||
int AsmVariantNo, StringRef RegisterPrefix);
|
int AsmVariantNo, StringRef RegisterPrefix,
|
||||||
|
AsmVariantInfo const &Variant);
|
||||||
|
|
||||||
/// validate - Return true if this matchable is a valid thing to match against
|
/// validate - Return true if this matchable is a valid thing to match against
|
||||||
/// and perform a bunch of validity checking.
|
/// and perform a bunch of validity checking.
|
||||||
|
@ -584,8 +592,10 @@ struct MatchableInfo {
|
||||||
void dump() const;
|
void dump() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void tokenizeAsmString(const AsmMatcherInfo &Info);
|
void tokenizeAsmString(AsmMatcherInfo const &Info,
|
||||||
void addAsmOperand(size_t Start, size_t End);
|
AsmVariantInfo const &Variant);
|
||||||
|
void addAsmOperand(size_t Start, size_t End,
|
||||||
|
std::string const &SeparatorCharacters);
|
||||||
};
|
};
|
||||||
|
|
||||||
/// SubtargetFeatureInfo - Helper class for storing information on a subtarget
|
/// SubtargetFeatureInfo - Helper class for storing information on a subtarget
|
||||||
|
@ -828,12 +838,13 @@ extractSingletonRegisterForAsmOperand(MatchableInfo::AsmOperand &Op,
|
||||||
|
|
||||||
void MatchableInfo::initialize(const AsmMatcherInfo &Info,
|
void MatchableInfo::initialize(const AsmMatcherInfo &Info,
|
||||||
SmallPtrSetImpl<Record*> &SingletonRegisters,
|
SmallPtrSetImpl<Record*> &SingletonRegisters,
|
||||||
int AsmVariantNo, StringRef RegisterPrefix) {
|
int AsmVariantNo, StringRef RegisterPrefix,
|
||||||
|
AsmVariantInfo const &Variant) {
|
||||||
AsmVariantID = AsmVariantNo;
|
AsmVariantID = AsmVariantNo;
|
||||||
AsmString =
|
AsmString =
|
||||||
CodeGenInstruction::FlattenAsmStringVariants(AsmString, AsmVariantNo);
|
CodeGenInstruction::FlattenAsmStringVariants(AsmString, AsmVariantNo);
|
||||||
|
|
||||||
tokenizeAsmString(Info);
|
tokenizeAsmString(Info, Variant);
|
||||||
|
|
||||||
// Compute the require features.
|
// Compute the require features.
|
||||||
for (Record *Predicate : TheDef->getValueAsListOfDefs("Predicates"))
|
for (Record *Predicate : TheDef->getValueAsListOfDefs("Predicates"))
|
||||||
|
@ -857,9 +868,9 @@ void MatchableInfo::initialize(const AsmMatcherInfo &Info,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Append an AsmOperand for the given substring of AsmString.
|
/// Append an AsmOperand for the given substring of AsmString.
|
||||||
void MatchableInfo::addAsmOperand(size_t Start, size_t End) {
|
void MatchableInfo::addAsmOperand(size_t Start, size_t End,
|
||||||
|
std::string const &Separators) {
|
||||||
StringRef String = AsmString;
|
StringRef String = AsmString;
|
||||||
StringRef Separators = "[]*! \t,";
|
|
||||||
// Look for separators before and after to figure out is this token is
|
// Look for separators before and after to figure out is this token is
|
||||||
// isolated. Accept '$$' as that's how we escape '$'.
|
// isolated. Accept '$$' as that's how we escape '$'.
|
||||||
bool IsIsolatedToken =
|
bool IsIsolatedToken =
|
||||||
|
@ -870,42 +881,54 @@ void MatchableInfo::addAsmOperand(size_t Start, size_t End) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// tokenizeAsmString - Tokenize a simplified assembly string.
|
/// tokenizeAsmString - Tokenize a simplified assembly string.
|
||||||
void MatchableInfo::tokenizeAsmString(const AsmMatcherInfo &Info) {
|
void MatchableInfo::tokenizeAsmString(const AsmMatcherInfo &Info,
|
||||||
|
AsmVariantInfo const &Variant) {
|
||||||
StringRef String = AsmString;
|
StringRef String = AsmString;
|
||||||
size_t Prev = 0;
|
unsigned Prev = 0;
|
||||||
bool InTok = true;
|
bool InTok = false;
|
||||||
for (size_t i = 0, e = String.size(); i != e; ++i) {
|
std::string Separators = Variant.TokenizingCharacters +
|
||||||
switch (String[i]) {
|
Variant.SeparatorCharacters;
|
||||||
case '[':
|
for (unsigned i = 0, e = String.size(); i != e; ++i) {
|
||||||
case ']':
|
if(Variant.BreakCharacters.find(String[i]) != std::string::npos) {
|
||||||
case '*':
|
if(InTok) {
|
||||||
case '!':
|
addAsmOperand(Prev, i, Separators);
|
||||||
case ' ':
|
Prev = i;
|
||||||
case '\t':
|
}
|
||||||
case ',':
|
InTok = true;
|
||||||
if (InTok) {
|
continue;
|
||||||
addAsmOperand(Prev, i);
|
}
|
||||||
|
if(Variant.TokenizingCharacters.find(String[i]) != std::string::npos) {
|
||||||
|
if(InTok) {
|
||||||
|
addAsmOperand(Prev, i, Separators);
|
||||||
InTok = false;
|
InTok = false;
|
||||||
}
|
}
|
||||||
if (!isspace(String[i]) && String[i] != ',')
|
addAsmOperand(i, i + 1, Separators);
|
||||||
addAsmOperand(i, i + 1);
|
|
||||||
Prev = i + 1;
|
Prev = i + 1;
|
||||||
break;
|
continue;
|
||||||
|
}
|
||||||
|
if(Variant.SeparatorCharacters.find(String[i]) != std::string::npos) {
|
||||||
|
if(InTok) {
|
||||||
|
addAsmOperand(Prev, i, Separators);
|
||||||
|
InTok = false;
|
||||||
|
}
|
||||||
|
Prev = i + 1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
switch (String[i]) {
|
||||||
case '\\':
|
case '\\':
|
||||||
if (InTok) {
|
if (InTok) {
|
||||||
addAsmOperand(Prev, i);
|
addAsmOperand(Prev, i, Separators);
|
||||||
InTok = false;
|
InTok = false;
|
||||||
}
|
}
|
||||||
++i;
|
++i;
|
||||||
assert(i != String.size() && "Invalid quoted character");
|
assert(i != String.size() && "Invalid quoted character");
|
||||||
addAsmOperand(i, i + 1);
|
addAsmOperand(i, i + 1, Separators);
|
||||||
Prev = i + 1;
|
Prev = i + 1;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case '$': {
|
case '$': {
|
||||||
if (InTok) {
|
if (InTok && Prev != i) {
|
||||||
addAsmOperand(Prev, i);
|
addAsmOperand(Prev, i, Separators);
|
||||||
InTok = false;
|
InTok = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -915,31 +938,20 @@ void MatchableInfo::tokenizeAsmString(const AsmMatcherInfo &Info) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If this is "${" find the next "}" and make an identifier like "${xxx}"
|
StringRef::iterator End = std::find(String.begin() + i, String.end(),'}');
|
||||||
size_t EndPos = String.find('}', i);
|
assert(End != String.end() && "Missing brace in operand reference!");
|
||||||
assert(EndPos != StringRef::npos &&
|
size_t EndPos = End - String.begin();
|
||||||
"Missing brace in operand reference!");
|
addAsmOperand(i, EndPos+1, Separators);
|
||||||
addAsmOperand(i, EndPos+1);
|
|
||||||
Prev = EndPos + 1;
|
Prev = EndPos + 1;
|
||||||
i = EndPos;
|
i = EndPos;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case '.':
|
|
||||||
if (!Info.AsmParser->getValueAsBit("MnemonicContainsDot")) {
|
|
||||||
if (InTok)
|
|
||||||
addAsmOperand(Prev, i);
|
|
||||||
Prev = i;
|
|
||||||
}
|
|
||||||
InTok = true;
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
InTok = true;
|
InTok = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (InTok && Prev != String.size())
|
if (InTok && Prev != String.size())
|
||||||
addAsmOperand(Prev, StringRef::npos);
|
addAsmOperand(Prev, StringRef::npos, Separators);
|
||||||
|
|
||||||
// The first token of the instruction is the mnemonic, which must be a
|
// The first token of the instruction is the mnemonic, which must be a
|
||||||
// simple string, not a $foo variable or a singleton register.
|
// simple string, not a $foo variable or a singleton register.
|
||||||
|
@ -1373,6 +1385,13 @@ void AsmMatcherInfo::buildInfo() {
|
||||||
std::string CommentDelimiter =
|
std::string CommentDelimiter =
|
||||||
AsmVariant->getValueAsString("CommentDelimiter");
|
AsmVariant->getValueAsString("CommentDelimiter");
|
||||||
std::string RegisterPrefix = AsmVariant->getValueAsString("RegisterPrefix");
|
std::string RegisterPrefix = AsmVariant->getValueAsString("RegisterPrefix");
|
||||||
|
AsmVariantInfo Variant;
|
||||||
|
Variant.TokenizingCharacters =
|
||||||
|
AsmVariant->getValueAsString("TokenizingCharacters");
|
||||||
|
Variant.SeparatorCharacters =
|
||||||
|
AsmVariant->getValueAsString("SeparatorCharacters");
|
||||||
|
Variant.BreakCharacters =
|
||||||
|
AsmVariant->getValueAsString("BreakCharacters");
|
||||||
int AsmVariantNo = AsmVariant->getValueAsInt("Variant");
|
int AsmVariantNo = AsmVariant->getValueAsInt("Variant");
|
||||||
|
|
||||||
for (const CodeGenInstruction *CGI : Target.instructions()) {
|
for (const CodeGenInstruction *CGI : Target.instructions()) {
|
||||||
|
@ -1388,7 +1407,8 @@ void AsmMatcherInfo::buildInfo() {
|
||||||
|
|
||||||
auto II = llvm::make_unique<MatchableInfo>(*CGI);
|
auto II = llvm::make_unique<MatchableInfo>(*CGI);
|
||||||
|
|
||||||
II->initialize(*this, SingletonRegisters, AsmVariantNo, RegisterPrefix);
|
II->initialize(*this, SingletonRegisters, AsmVariantNo, RegisterPrefix,
|
||||||
|
Variant);
|
||||||
|
|
||||||
// Ignore instructions which shouldn't be matched and diagnose invalid
|
// Ignore instructions which shouldn't be matched and diagnose invalid
|
||||||
// instruction definitions with an error.
|
// instruction definitions with an error.
|
||||||
|
@ -1415,7 +1435,8 @@ void AsmMatcherInfo::buildInfo() {
|
||||||
|
|
||||||
auto II = llvm::make_unique<MatchableInfo>(std::move(Alias));
|
auto II = llvm::make_unique<MatchableInfo>(std::move(Alias));
|
||||||
|
|
||||||
II->initialize(*this, SingletonRegisters, AsmVariantNo, RegisterPrefix);
|
II->initialize(*this, SingletonRegisters, AsmVariantNo, RegisterPrefix,
|
||||||
|
Variant);
|
||||||
|
|
||||||
// Validate the alias definitions.
|
// Validate the alias definitions.
|
||||||
II->validate(CommentDelimiter, false);
|
II->validate(CommentDelimiter, false);
|
||||||
|
|
Loading…
Reference in New Issue