Skip to content

Commit 91da70b

Browse files
jiegecRot127
authored andcommitted
Handle multiple template arguments in handleDefaultArg
1 parent 1ac920c commit 91da70b

File tree

1 file changed

+47
-14
lines changed

1 file changed

+47
-14
lines changed

llvm/utils/TableGen/PrinterCapstone.cpp

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -669,33 +669,66 @@ void patchIsGetImmReg(std::string &Code) {
669669
static std::string handleDefaultArg(const std::string &TargetName,
670670
std::string &Code) {
671671
// Default values of the template function arguments.
672-
static SmallVector<std::pair<std::string, std::string>>
672+
// Tuple is (function name, default argument, number of argumetns)
673+
static SmallVector<std::tuple<std::string, std::string, int>>
673674
AArch64TemplFuncWithDefaults = {// Default is 1
674-
{"printVectorIndex", "1"},
675+
{"printVectorIndex", "1", 1},
675676
// Default is false == 0
676-
{"printPrefetchOp", "0"},
677+
{"printPrefetchOp", "0", 1},
677678
// Default is 0
678-
{"printSVERegOp", "0"},
679-
{"printMatrixIndex", "1"}
679+
{"printSVERegOp", "0", 1},
680+
{"printMatrixIndex", "1", 1}
680681
};
681-
SmallVector<std::pair<std::string, std::string>> *TemplFuncWithDefaults;
682+
static SmallVector<std::tuple<std::string, std::string, int>>
683+
LoongArchTemplFuncWithDefaults = {// Default is 0
684+
{"decodeSImmOperand", "0", 2},
685+
{"decodeUImmOperand", "0", 2},
686+
};
687+
SmallVector<std::tuple<std::string, std::string, int>> *TemplFuncWithDefaults;
682688
if (TargetName == "AArch64")
683689
TemplFuncWithDefaults = &AArch64TemplFuncWithDefaults;
690+
else if (TargetName == "LoongArch")
691+
TemplFuncWithDefaults = &LoongArchTemplFuncWithDefaults;
684692
else
685693
return Code;
686694

687-
for (std::pair Func : *TemplFuncWithDefaults) {
688-
// Search for function name without "<>" and replace it with <name>_<default-arg>
689-
// Note: There can be multiple calls to such a function in the code.
690-
// We only replace one here.
691-
auto func = Func.first;
695+
for (std::tuple Func : *TemplFuncWithDefaults) {
696+
// Search for function where default argument is not passed
697+
// e.g. printVectorIndex -> printVectorIndex_1
698+
// e.g. decodeSImmOperand<1> -> decodeSImmOperand_1_0
699+
auto Name = std::get<0>(Func);
700+
auto DefaultArg = std::get<1>(Func);
701+
auto ExpectedArgCount = std::get<2>(Func);
692702
SmallVector<StringRef> Matches;
693-
while (Regex(func + "(<>)?($|\\()").match(Code, &Matches)) {
703+
while (Regex(Name + "(<[0-9a-zA-Z,]*>)?($|\\()").match(Code, &Matches)) {
704+
StringRef Arg = Matches[1];
705+
// Count the number of passed arguments
706+
int ActualArgCount = 0;
707+
if (!Arg.empty() && Arg != "<>") {
708+
ActualArgCount = Arg.count(',') + 1;
709+
}
710+
711+
std::string NewArg;
712+
NewArg = Regex("<").sub("", Arg);
713+
NewArg = Regex(">").sub("", NewArg);
714+
NewArg = Regex(",").sub("_", NewArg);
715+
if (ActualArgCount != ExpectedArgCount) {
716+
// Add default argument
717+
if (Arg.empty()) {
718+
// e.g. printVectorIndex -> printVectorIndex_1
719+
NewArg += DefaultArg;
720+
} else {
721+
// e.g. decodeSImmOperand<1> -> decodeSimmOperand_1_0
722+
NewArg += "_";
723+
NewArg += DefaultArg;
724+
}
725+
}
726+
694727
StringRef Match = Matches[0];
695728
if (Match.ends_with("(")) {
696-
Code = Regex(func + "(<>)?\\(").sub(func + "_" + Func.second + "(", Code);
729+
Code = Regex(Name + "(<[0-9a-zA-Z,]*>)?\\(").sub(Name + "_" + NewArg + "(", Code);
697730
} else {
698-
Code = Regex(func + "(<>)?$").sub(func + "_" + Func.second, Code);
731+
Code = Regex(Name + "(<[0-9a-zA-Z,]*>)?$").sub(Name + "_" + NewArg, Code);
699732
}
700733
}
701734
}

0 commit comments

Comments
 (0)