summaryrefslogtreecommitdiffstats
path: root/utils/TableGen
diff options
context:
space:
mode:
authorrdivacky <rdivacky@FreeBSD.org>2009-10-23 14:19:52 +0000
committerrdivacky <rdivacky@FreeBSD.org>2009-10-23 14:19:52 +0000
commit9643cca39fb9fb3b49a8912926de98acf882283c (patch)
tree22cc59e4b240d84c3a5a60531119c4eca914a256 /utils/TableGen
parent1adacceba9c9ee0f16e54388e56c9a249b296f75 (diff)
downloadFreeBSD-src-9643cca39fb9fb3b49a8912926de98acf882283c.zip
FreeBSD-src-9643cca39fb9fb3b49a8912926de98acf882283c.tar.gz
Update LLVM to r84949.
Diffstat (limited to 'utils/TableGen')
-rw-r--r--utils/TableGen/AsmWriterEmitter.cpp23
-rw-r--r--utils/TableGen/ClangDiagnosticsEmitter.cpp16
-rw-r--r--utils/TableGen/CodeGenDAGPatterns.h2
-rw-r--r--utils/TableGen/DAGISelEmitter.cpp12
-rw-r--r--utils/TableGen/LLVMCConfigurationEmitter.cpp637
-rw-r--r--utils/TableGen/LLVMCConfigurationEmitter.h3
-rw-r--r--utils/TableGen/StringToOffsetTable.h9
7 files changed, 542 insertions, 160 deletions
diff --git a/utils/TableGen/AsmWriterEmitter.cpp b/utils/TableGen/AsmWriterEmitter.cpp
index 84a647b..ff348e8 100644
--- a/utils/TableGen/AsmWriterEmitter.cpp
+++ b/utils/TableGen/AsmWriterEmitter.cpp
@@ -538,6 +538,29 @@ FindUniqueOperandCommands(std::vector<std::string> &UniqueOperandCommands,
}
+static void UnescapeString(std::string &Str) {
+ for (unsigned i = 0; i != Str.size(); ++i) {
+ if (Str[i] == '\\' && i != Str.size()-1) {
+ switch (Str[i+1]) {
+ default: continue; // Don't execute the code after the switch.
+ case 'a': Str[i] = '\a'; break;
+ case 'b': Str[i] = '\b'; break;
+ case 'e': Str[i] = 27; break;
+ case 'f': Str[i] = '\f'; break;
+ case 'n': Str[i] = '\n'; break;
+ case 'r': Str[i] = '\r'; break;
+ case 't': Str[i] = '\t'; break;
+ case 'v': Str[i] = '\v'; break;
+ case '"': Str[i] = '\"'; break;
+ case '\'': Str[i] = '\''; break;
+ case '\\': Str[i] = '\\'; break;
+ }
+ // Nuke the second character.
+ Str.erase(Str.begin()+i+1);
+ }
+ }
+}
+
/// EmitPrintInstruction - Generate the code for the "printInstruction" method
/// implementation.
void AsmWriterEmitter::EmitPrintInstruction(raw_ostream &O) {
diff --git a/utils/TableGen/ClangDiagnosticsEmitter.cpp b/utils/TableGen/ClangDiagnosticsEmitter.cpp
index c127afd..6f1080e 100644
--- a/utils/TableGen/ClangDiagnosticsEmitter.cpp
+++ b/utils/TableGen/ClangDiagnosticsEmitter.cpp
@@ -52,15 +52,12 @@ void ClangDiagsDefsEmitter::run(raw_ostream &OS) {
// Description string.
OS << ", \"";
- std::string S = R.getValueAsString("Text");
- EscapeString(S);
- OS << S << "\"";
+ OS.write_escaped(R.getValueAsString("Text")) << '"';
// Warning associated with the diagnostic.
if (DefInit *DI = dynamic_cast<DefInit*>(R.getValueInit("Group"))) {
- S = DI->getDef()->getValueAsString("GroupName");
- EscapeString(S);
- OS << ", \"" << S << "\"";
+ OS << ", \"";
+ OS.write_escaped(DI->getDef()->getValueAsString("GroupName")) << '"';
} else {
OS << ", 0";
}
@@ -151,11 +148,10 @@ void ClangDiagGroupsEmitter::run(raw_ostream &OS) {
OS << "\n#ifdef GET_DIAG_TABLE\n";
for (std::map<std::string, GroupInfo>::iterator
I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I) {
- std::string S = I->first;
- EscapeString(S);
// Group option string.
- OS << " { \"" << S << "\","
- << std::string(MaxLen-I->first.size()+1, ' ');
+ OS << " { \"";
+ OS.write_escaped(I->first) << "\","
+ << std::string(MaxLen-I->first.size()+1, ' ');
// Diagnostics in the group.
if (I->second.DiagsInGroup.empty())
diff --git a/utils/TableGen/CodeGenDAGPatterns.h b/utils/TableGen/CodeGenDAGPatterns.h
index 9b53ecc5..398764b 100644
--- a/utils/TableGen/CodeGenDAGPatterns.h
+++ b/utils/TableGen/CodeGenDAGPatterns.h
@@ -584,6 +584,8 @@ public:
return intrinsic_wo_chain_sdnode;
}
+ bool hasTargetIntrinsics() { return !TgtIntrinsics.empty(); }
+
private:
void ParseNodeInfo();
void ParseNodeTransforms();
diff --git a/utils/TableGen/DAGISelEmitter.cpp b/utils/TableGen/DAGISelEmitter.cpp
index dcf64e4..bbb8a18 100644
--- a/utils/TableGen/DAGISelEmitter.cpp
+++ b/utils/TableGen/DAGISelEmitter.cpp
@@ -2067,8 +2067,16 @@ void DAGISelEmitter::EmitInstructionSelector(raw_ostream &OS) {
<< " errs() << \"Cannot yet select: \";\n"
<< " unsigned iid = cast<ConstantSDNode>(N.getOperand("
<< "N.getOperand(0).getValueType() == MVT::Other))->getZExtValue();\n"
- << " llvm_report_error(\"Cannot yet select: intrinsic %\" +\n"
- << "Intrinsic::getName((Intrinsic::ID)iid));\n"
+ << " if (iid < Intrinsic::num_intrinsics)\n"
+ << " llvm_report_error(\"Cannot yet select: intrinsic %\" + "
+ << "Intrinsic::getName((Intrinsic::ID)iid));\n";
+ if (CGP.hasTargetIntrinsics()) {
+ OS << " else if (const TargetIntrinsicInfo *tii = TM.getIntrinsicInfo())\n"
+ << " llvm_report_error(Twine(\"Cannot yet select: target intrinsic "
+ << "%\") + tii->getName(iid));\n";
+ }
+ OS << " else\n"
+ << " llvm_report_error(\"Cannot yet select: invalid intrinsic\");\n"
<< "}\n\n";
}
diff --git a/utils/TableGen/LLVMCConfigurationEmitter.cpp b/utils/TableGen/LLVMCConfigurationEmitter.cpp
index 06afaf7..f5d1139 100644
--- a/utils/TableGen/LLVMCConfigurationEmitter.cpp
+++ b/utils/TableGen/LLVMCConfigurationEmitter.cpp
@@ -56,8 +56,14 @@ const char * SinkOptionName = "AutoGeneratedSinkOption";
/// Id - An 'identity' function object.
struct Id {
- template<typename T>
- void operator()(const T&) const {
+ template<typename T0>
+ void operator()(const T0&) const {
+ }
+ template<typename T0, typename T1>
+ void operator()(const T0&, const T1&) const {
+ }
+ template<typename T0, typename T1, typename T2>
+ void operator()(const T0&, const T1&, const T2&) const {
}
};
@@ -81,16 +87,24 @@ const DagInit& InitPtrToDag(const Init* ptr) {
return val;
}
+const std::string GetOperatorName(const DagInit* D) {
+ return D->getOperator()->getAsString();
+}
+
+const std::string GetOperatorName(const DagInit& D) {
+ return GetOperatorName(&D);
+}
+
// checkNumberOfArguments - Ensure that the number of args in d is
// greater than or equal to min_arguments, otherwise throw an exception.
void checkNumberOfArguments (const DagInit* d, unsigned min_arguments) {
if (!d || d->getNumArgs() < min_arguments)
- throw d->getOperator()->getAsString() + ": too few arguments!";
+ throw GetOperatorName(d) + ": too few arguments!";
}
// isDagEmpty - is this DAG marked with an empty marker?
bool isDagEmpty (const DagInit* d) {
- return d->getOperator()->getAsString() == "empty_dag_marker";
+ return GetOperatorName(d) == "empty_dag_marker";
}
// EscapeVariableName - Escape commas and other symbols not allowed
@@ -132,6 +146,18 @@ void checkedIncrement(I& P, I E, S ErrorString) {
throw ErrorString;
}
+// apply is needed because C++'s syntax doesn't let us construct a function
+// object and call it in the same statement.
+template<typename F, typename T0>
+void apply(F Fun, T0& Arg0) {
+ return Fun(Arg0);
+}
+
+template<typename F, typename T0, typename T1>
+void apply(F Fun, T0& Arg0, T1& Arg1) {
+ return Fun(Arg0, Arg1);
+}
+
//===----------------------------------------------------------------------===//
/// Back-end specific code
@@ -143,6 +169,10 @@ namespace OptionType {
enum OptionType { Alias, Switch, Parameter, ParameterList,
Prefix, PrefixList};
+ bool IsAlias(OptionType t) {
+ return (t == Alias);
+ }
+
bool IsList (OptionType t) {
return (t == ParameterList || t == PrefixList);
}
@@ -231,12 +261,12 @@ struct OptionDescription {
bool isReallyHidden() const;
void setReallyHidden();
- bool isParameter() const
- { return OptionType::IsParameter(this->Type); }
-
bool isSwitch() const
{ return OptionType::IsSwitch(this->Type); }
+ bool isParameter() const
+ { return OptionType::IsParameter(this->Type); }
+
bool isList() const
{ return OptionType::IsList(this->Type); }
@@ -258,7 +288,7 @@ void OptionDescription::Merge (const OptionDescription& other)
}
bool OptionDescription::isAlias() const {
- return Type == OptionType::Alias;
+ return OptionType::IsAlias(this->Type);
}
bool OptionDescription::isMultiVal() const {
@@ -352,6 +382,14 @@ public:
/// FindOption - exception-throwing wrapper for find().
const OptionDescription& FindOption(const std::string& OptName) const;
+ // Wrappers for FindOption that throw an exception in case the option has a
+ // wrong type.
+ const OptionDescription& FindSwitch(const std::string& OptName) const;
+ const OptionDescription& FindParameter(const std::string& OptName) const;
+ const OptionDescription& FindList(const std::string& OptName) const;
+ const OptionDescription&
+ FindListOrParameter(const std::string& OptName) const;
+
/// insertDescription - Insert new OptionDescription into
/// OptionDescriptions list
void InsertDescription (const OptionDescription& o);
@@ -363,8 +401,7 @@ public:
};
const OptionDescription&
-OptionDescriptions::FindOption(const std::string& OptName) const
-{
+OptionDescriptions::FindOption(const std::string& OptName) const {
const_iterator I = Descriptions.find(OptName);
if (I != Descriptions.end())
return I->second;
@@ -372,8 +409,40 @@ OptionDescriptions::FindOption(const std::string& OptName) const
throw OptName + ": no such option!";
}
-void OptionDescriptions::InsertDescription (const OptionDescription& o)
-{
+const OptionDescription&
+OptionDescriptions::FindSwitch(const std::string& OptName) const {
+ const OptionDescription& OptDesc = this->FindOption(OptName);
+ if (!OptDesc.isSwitch())
+ throw OptName + ": incorrect option type - should be a switch!";
+ return OptDesc;
+}
+
+const OptionDescription&
+OptionDescriptions::FindList(const std::string& OptName) const {
+ const OptionDescription& OptDesc = this->FindOption(OptName);
+ if (!OptDesc.isList())
+ throw OptName + ": incorrect option type - should be a list!";
+ return OptDesc;
+}
+
+const OptionDescription&
+OptionDescriptions::FindParameter(const std::string& OptName) const {
+ const OptionDescription& OptDesc = this->FindOption(OptName);
+ if (!OptDesc.isParameter())
+ throw OptName + ": incorrect option type - should be a parameter!";
+ return OptDesc;
+}
+
+const OptionDescription&
+OptionDescriptions::FindListOrParameter(const std::string& OptName) const {
+ const OptionDescription& OptDesc = this->FindOption(OptName);
+ if (!OptDesc.isList() && !OptDesc.isParameter())
+ throw OptName
+ + ": incorrect option type - should be a list or parameter!";
+ return OptDesc;
+}
+
+void OptionDescriptions::InsertDescription (const OptionDescription& o) {
container_type::iterator I = Descriptions.find(o.Name);
if (I != Descriptions.end()) {
OptionDescription& D = I->second;
@@ -409,7 +478,7 @@ public:
/// handler.
void operator() (Init* i) {
const DagInit& property = InitPtrToDag(i);
- const std::string& property_name = property.getOperator()->getAsString();
+ const std::string& property_name = GetOperatorName(property);
typename HandlerMap::iterator method = Handlers_.find(property_name);
if (method != Handlers_.end()) {
@@ -558,7 +627,7 @@ public:
checkNumberOfArguments(&d, 1);
const OptionType::OptionType Type =
- stringToOptionType(d.getOperator()->getAsString());
+ stringToOptionType(GetOperatorName(d));
const std::string& Name = InitPtrToString(d.getArg(0));
OptionDescription OD(Type, Name);
@@ -678,7 +747,7 @@ private:
checkNumberOfArguments(d, 1);
Init* Case = d->getArg(0);
if (typeid(*Case) != typeid(DagInit) ||
- static_cast<DagInit*>(Case)->getOperator()->getAsString() != "case")
+ GetOperatorName(static_cast<DagInit*>(Case)) != "case")
throw
std::string("The argument to (actions) should be a 'case' construct!");
toolDesc_.Actions = Case;
@@ -775,11 +844,17 @@ void FillInEdgeVector(RecordVector::const_iterator B,
/// CalculatePriority - Calculate the priority of this plugin.
int CalculatePriority(RecordVector::const_iterator B,
RecordVector::const_iterator E) {
- int total = 0;
- for (; B!=E; ++B) {
- total += static_cast<int>((*B)->getValueAsInt("priority"));
+ int priority = 0;
+
+ if (B != E) {
+ priority = static_cast<int>((*B)->getValueAsInt("priority"));
+
+ if (++B != E)
+ throw std::string("More than one 'PluginPriority' instance found: "
+ "most probably an error!");
}
- return total;
+
+ return priority;
}
/// NotInGraph - Helper function object for FilterNotInGraph.
@@ -874,22 +949,60 @@ void TypecheckGraph (const RecordVector& EdgeVector,
/// WalkCase - Walks the 'case' expression DAG and invokes
/// TestCallback on every test, and StatementCallback on every
/// statement. Handles 'case' nesting, but not the 'and' and 'or'
-/// combinators.
-// TODO: Re-implement EmitCaseConstructHandler on top of this function?
+/// combinators (that is, they are passed directly to TestCallback).
+/// TestCallback must have type 'void TestCallback(const DagInit*, unsigned
+/// IndentLevel, bool FirstTest)'.
+/// StatementCallback must have type 'void StatementCallback(const Init*,
+/// unsigned IndentLevel)'.
template <typename F1, typename F2>
-void WalkCase(Init* Case, F1 TestCallback, F2 StatementCallback) {
+void WalkCase(const Init* Case, F1 TestCallback, F2 StatementCallback,
+ unsigned IndentLevel = 0)
+{
const DagInit& d = InitPtrToDag(Case);
+
+ // Error checks.
+ if (GetOperatorName(d) != "case")
+ throw std::string("WalkCase should be invoked only on 'case' expressions!");
+
+ if (d.getNumArgs() < 2)
+ throw "There should be at least one clause in the 'case' expression:\n"
+ + d.getAsString();
+
+ // Main loop.
bool even = false;
+ const unsigned numArgs = d.getNumArgs();
+ unsigned i = 1;
for (DagInit::const_arg_iterator B = d.arg_begin(), E = d.arg_end();
B != E; ++B) {
Init* arg = *B;
- if (even && dynamic_cast<DagInit*>(arg)
- && static_cast<DagInit*>(arg)->getOperator()->getAsString() == "case")
- WalkCase(arg, TestCallback, StatementCallback);
- else if (!even)
- TestCallback(arg);
+
+ if (!even)
+ {
+ // Handle test.
+ const DagInit& Test = InitPtrToDag(arg);
+
+ if (GetOperatorName(Test) == "default" && (i+1 != numArgs))
+ throw std::string("The 'default' clause should be the last in the"
+ "'case' construct!");
+ if (i == numArgs)
+ throw "Case construct handler: no corresponding action "
+ "found for the test " + Test.getAsString() + '!';
+
+ TestCallback(&Test, IndentLevel, (i == 1));
+ }
else
- StatementCallback(arg);
+ {
+ if (dynamic_cast<DagInit*>(arg)
+ && GetOperatorName(static_cast<DagInit*>(arg)) == "case") {
+ // Nested 'case'.
+ WalkCase(arg, TestCallback, StatementCallback, IndentLevel + Indent1);
+ }
+
+ // Handle statement.
+ StatementCallback(arg, IndentLevel);
+ }
+
+ ++i;
even = !even;
}
}
@@ -901,7 +1014,7 @@ class ExtractOptionNames {
void processDag(const Init* Statement) {
const DagInit& Stmt = InitPtrToDag(Statement);
- const std::string& ActionName = Stmt.getOperator()->getAsString();
+ const std::string& ActionName = GetOperatorName(Stmt);
if (ActionName == "forward" || ActionName == "forward_as" ||
ActionName == "unpack_values" || ActionName == "switch_on" ||
ActionName == "parameter_equals" || ActionName == "element_in_list" ||
@@ -932,6 +1045,13 @@ public:
this->processDag(Statement);
}
}
+
+ void operator()(const DagInit* Test, unsigned, bool) {
+ this->operator()(Test);
+ }
+ void operator()(const Init* Statement, unsigned) {
+ this->operator()(Statement);
+ }
};
/// CheckForSuperfluousOptions - Check that there are no side
@@ -990,51 +1110,137 @@ bool EmitCaseTest0Args(const std::string& TestName, raw_ostream& O) {
return false;
}
+/// EmitListTest - Helper function used by EmitCaseTest1ArgList().
+template <typename F>
+void EmitListTest(const ListInit& L, const char* LogicOp,
+ F Callback, raw_ostream& O)
+{
+ // This is a lot like EmitLogicalOperationTest, but works on ListInits instead
+ // of Dags...
+ bool isFirst = true;
+ for (ListInit::const_iterator B = L.begin(), E = L.end(); B != E; ++B) {
+ if (isFirst)
+ isFirst = false;
+ else
+ O << " || ";
+ Callback(InitPtrToString(*B), O);
+ }
+}
+
+// Callbacks for use with EmitListTest.
-/// EmitCaseTest1Arg - Helper function used by EmitCaseConstructHandler().
-bool EmitCaseTest1Arg(const std::string& TestName,
- const DagInit& d,
- const OptionDescriptions& OptDescs,
- raw_ostream& O) {
- checkNumberOfArguments(&d, 1);
+class EmitSwitchOn {
+ const OptionDescriptions& OptDescs_;
+public:
+ EmitSwitchOn(const OptionDescriptions& OptDescs) : OptDescs_(OptDescs)
+ {}
+
+ void operator()(const std::string& OptName, raw_ostream& O) const {
+ const OptionDescription& OptDesc = OptDescs_.FindSwitch(OptName);
+ O << OptDesc.GenVariableName();
+ }
+};
+
+class EmitEmptyTest {
+ bool EmitNegate_;
+ const OptionDescriptions& OptDescs_;
+public:
+ EmitEmptyTest(bool EmitNegate, const OptionDescriptions& OptDescs)
+ : EmitNegate_(EmitNegate), OptDescs_(OptDescs)
+ {}
+
+ void operator()(const std::string& OptName, raw_ostream& O) const {
+ const char* Neg = (EmitNegate_ ? "!" : "");
+ if (OptName == "o") {
+ O << Neg << "OutputFilename.empty()";
+ }
+ else {
+ const OptionDescription& OptDesc = OptDescs_.FindListOrParameter(OptName);
+ O << Neg << OptDesc.GenVariableName() << ".empty()";
+ }
+ }
+};
+
+
+/// EmitCaseTest1ArgList - Helper function used by EmitCaseTest1Arg();
+bool EmitCaseTest1ArgList(const std::string& TestName,
+ const DagInit& d,
+ const OptionDescriptions& OptDescs,
+ raw_ostream& O) {
+ const ListInit& L = *static_cast<ListInit*>(d.getArg(0));
+
+ if (TestName == "any_switch_on") {
+ EmitListTest(L, "||", EmitSwitchOn(OptDescs), O);
+ return true;
+ }
+ else if (TestName == "switch_on") {
+ EmitListTest(L, "&&", EmitSwitchOn(OptDescs), O);
+ return true;
+ }
+ else if (TestName == "any_not_empty") {
+ EmitListTest(L, "||", EmitEmptyTest(true, OptDescs), O);
+ return true;
+ }
+ else if (TestName == "any_empty") {
+ EmitListTest(L, "||", EmitEmptyTest(false, OptDescs), O);
+ return true;
+ }
+ else if (TestName == "not_empty") {
+ EmitListTest(L, "&&", EmitEmptyTest(true, OptDescs), O);
+ return true;
+ }
+ else if (TestName == "empty") {
+ EmitListTest(L, "&&", EmitEmptyTest(false, OptDescs), O);
+ return true;
+ }
+
+ return false;
+}
+
+/// EmitCaseTest1ArgStr - Helper function used by EmitCaseTest1Arg();
+bool EmitCaseTest1ArgStr(const std::string& TestName,
+ const DagInit& d,
+ const OptionDescriptions& OptDescs,
+ raw_ostream& O) {
const std::string& OptName = InitPtrToString(d.getArg(0));
if (TestName == "switch_on") {
- const OptionDescription& OptDesc = OptDescs.FindOption(OptName);
- if (!OptDesc.isSwitch())
- throw OptName + ": incorrect option type - should be a switch!";
- O << OptDesc.GenVariableName();
+ apply(EmitSwitchOn(OptDescs), OptName, O);
return true;
- } else if (TestName == "input_languages_contain") {
+ }
+ else if (TestName == "input_languages_contain") {
O << "InLangs.count(\"" << OptName << "\") != 0";
return true;
- } else if (TestName == "in_language") {
+ }
+ else if (TestName == "in_language") {
// This works only for single-argument Tool::GenerateAction. Join
// tools can process several files in different languages simultaneously.
// TODO: make this work with Edge::Weight (if possible).
O << "LangMap.GetLanguage(inFile) == \"" << OptName << '\"';
return true;
- } else if (TestName == "not_empty" || TestName == "empty") {
- const char* Test = (TestName == "empty") ? "" : "!";
-
- if (OptName == "o") {
- O << Test << "OutputFilename.empty()";
- return true;
- }
- else {
- const OptionDescription& OptDesc = OptDescs.FindOption(OptName);
- if (OptDesc.isSwitch())
- throw OptName
- + ": incorrect option type - should be a list or parameter!";
- O << Test << OptDesc.GenVariableName() << ".empty()";
- return true;
- }
+ }
+ else if (TestName == "not_empty" || TestName == "empty") {
+ bool EmitNegate = (TestName == "not_empty");
+ apply(EmitEmptyTest(EmitNegate, OptDescs), OptName, O);
+ return true;
}
return false;
}
+/// EmitCaseTest1Arg - Helper function used by EmitCaseConstructHandler();
+bool EmitCaseTest1Arg(const std::string& TestName,
+ const DagInit& d,
+ const OptionDescriptions& OptDescs,
+ raw_ostream& O) {
+ checkNumberOfArguments(&d, 1);
+ if (typeid(*d.getArg(0)) == typeid(ListInit))
+ return EmitCaseTest1ArgList(TestName, d, OptDescs, O);
+ else
+ return EmitCaseTest1ArgStr(TestName, d, OptDescs, O);
+}
+
/// EmitCaseTest2Args - Helper function used by EmitCaseConstructHandler().
bool EmitCaseTest2Args(const std::string& TestName,
const DagInit& d,
@@ -1044,17 +1250,14 @@ bool EmitCaseTest2Args(const std::string& TestName,
checkNumberOfArguments(&d, 2);
const std::string& OptName = InitPtrToString(d.getArg(0));
const std::string& OptArg = InitPtrToString(d.getArg(1));
- const OptionDescription& OptDesc = OptDescs.FindOption(OptName);
if (TestName == "parameter_equals") {
- if (!OptDesc.isParameter())
- throw OptName + ": incorrect option type - should be a parameter!";
+ const OptionDescription& OptDesc = OptDescs.FindParameter(OptName);
O << OptDesc.GenVariableName() << " == \"" << OptArg << "\"";
return true;
}
else if (TestName == "element_in_list") {
- if (!OptDesc.isList())
- throw OptName + ": incorrect option type - should be a list!";
+ const OptionDescription& OptDesc = OptDescs.FindList(OptName);
const std::string& VarName = OptDesc.GenVariableName();
O << "std::find(" << VarName << ".begin(),\n";
O.indent(IndentLevel + Indent1)
@@ -1106,7 +1309,7 @@ void EmitLogicalNot(const DagInit& d, unsigned IndentLevel,
void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
const OptionDescriptions& OptDescs,
raw_ostream& O) {
- const std::string& TestName = d.getOperator()->getAsString();
+ const std::string& TestName = GetOperatorName(d);
if (TestName == "and")
EmitLogicalOperationTest(d, "&&", IndentLevel, OptDescs, O);
@@ -1124,59 +1327,79 @@ void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
throw TestName + ": unknown edge property!";
}
-// Emit code that handles the 'case' construct.
-// Takes a function object that should emit code for every case clause.
-// Callback's type is
-// void F(Init* Statement, unsigned IndentLevel, raw_ostream& O).
-template <typename F>
-void EmitCaseConstructHandler(const Init* Dag, unsigned IndentLevel,
- F Callback, bool EmitElseIf,
- const OptionDescriptions& OptDescs,
- raw_ostream& O) {
- const DagInit* d = &InitPtrToDag(Dag);
- if (d->getOperator()->getAsString() != "case")
- throw std::string("EmitCaseConstructHandler should be invoked"
- " only on 'case' expressions!");
- unsigned numArgs = d->getNumArgs();
- if (d->getNumArgs() < 2)
- throw "There should be at least one clause in the 'case' expression:\n"
- + d->getAsString();
+/// EmitCaseTestCallback - Callback used by EmitCaseConstructHandler.
+class EmitCaseTestCallback {
+ bool EmitElseIf_;
+ const OptionDescriptions& OptDescs_;
+ raw_ostream& O_;
+public:
- for (unsigned i = 0; i != numArgs; ++i) {
- const DagInit& Test = InitPtrToDag(d->getArg(i));
+ EmitCaseTestCallback(bool EmitElseIf,
+ const OptionDescriptions& OptDescs, raw_ostream& O)
+ : EmitElseIf_(EmitElseIf), OptDescs_(OptDescs), O_(O)
+ {}
- // Emit the test.
- if (Test.getOperator()->getAsString() == "default") {
- if (i+2 != numArgs)
- throw std::string("The 'default' clause should be the last in the"
- "'case' construct!");
- O.indent(IndentLevel) << "else {\n";
+ void operator()(const DagInit* Test, unsigned IndentLevel, bool FirstTest)
+ {
+ if (GetOperatorName(Test) == "default") {
+ O_.indent(IndentLevel) << "else {\n";
}
else {
- O.indent(IndentLevel) << ((i != 0 && EmitElseIf) ? "else if (" : "if (");
- EmitCaseTest(Test, IndentLevel, OptDescs, O);
- O << ") {\n";
+ O_.indent(IndentLevel)
+ << ((!FirstTest && EmitElseIf_) ? "else if (" : "if (");
+ EmitCaseTest(*Test, IndentLevel, OptDescs_, O_);
+ O_ << ") {\n";
}
+ }
+};
- // Emit the corresponding statement.
- ++i;
- if (i == numArgs)
- throw "Case construct handler: no corresponding action "
- "found for the test " + Test.getAsString() + '!';
-
- Init* arg = d->getArg(i);
- const DagInit* nd = dynamic_cast<DagInit*>(arg);
- if (nd && (nd->getOperator()->getAsString() == "case")) {
- // Handle the nested 'case'.
- EmitCaseConstructHandler(nd, (IndentLevel + Indent1),
- Callback, EmitElseIf, OptDescs, O);
- }
- else {
- Callback(arg, (IndentLevel + Indent1), O);
+/// EmitCaseStatementCallback - Callback used by EmitCaseConstructHandler.
+template <typename F>
+class EmitCaseStatementCallback {
+ F Callback_;
+ raw_ostream& O_;
+public:
+
+ EmitCaseStatementCallback(F Callback, raw_ostream& O)
+ : Callback_(Callback), O_(O)
+ {}
+
+ void operator() (const Init* Statement, unsigned IndentLevel) {
+
+ // Ignore nested 'case' DAG.
+ if (!(dynamic_cast<const DagInit*>(Statement) &&
+ GetOperatorName(static_cast<const DagInit*>(Statement)) == "case")) {
+ if (typeid(*Statement) == typeid(ListInit)) {
+ const ListInit& DagList = *static_cast<const ListInit*>(Statement);
+ for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
+ B != E; ++B)
+ Callback_(*B, (IndentLevel + Indent1), O_);
+ }
+ else {
+ Callback_(Statement, (IndentLevel + Indent1), O_);
+ }
}
- O.indent(IndentLevel) << "}\n";
+ O_.indent(IndentLevel) << "}\n";
}
+
+};
+
+/// EmitCaseConstructHandler - Emit code that handles the 'case'
+/// construct. Takes a function object that should emit code for every case
+/// clause. Implemented on top of WalkCase.
+/// Callback's type is void F(Init* Statement, unsigned IndentLevel,
+/// raw_ostream& O).
+/// EmitElseIf parameter controls the type of condition that is emitted ('if
+/// (..) {..} else if (..) {} .. else {..}' vs. 'if (..) {..} if(..) {..}
+/// .. else {..}').
+template <typename F>
+void EmitCaseConstructHandler(const Init* Case, unsigned IndentLevel,
+ F Callback, bool EmitElseIf,
+ const OptionDescriptions& OptDescs,
+ raw_ostream& O) {
+ WalkCase(Case, EmitCaseTestCallback(EmitElseIf, OptDescs, O),
+ EmitCaseStatementCallback<F>(Callback, O), IndentLevel);
}
/// TokenizeCmdline - converts from "$CALL(HookName, 'Arg1', 'Arg2')/path" to
@@ -1474,17 +1697,40 @@ void EmitForwardOptionPropertyHandlingCode (const OptionDescription& D,
}
}
-/// EmitActionHandler - Emit code that handles actions. Used by
-/// EmitGenerateActionMethod() as an argument to
-/// EmitCaseConstructHandler().
-class EmitActionHandler {
+/// ActionHandlingCallbackBase - Base class of EmitActionHandlersCallback and
+/// EmitPreprocessOptionsCallback.
+struct ActionHandlingCallbackBase {
+
+ void onErrorDag(const DagInit& d,
+ unsigned IndentLevel, raw_ostream& O) const
+ {
+ O.indent(IndentLevel)
+ << "throw std::runtime_error(\"" <<
+ (d.getNumArgs() >= 1 ? InitPtrToString(d.getArg(0))
+ : "Unknown error!")
+ << "\");\n";
+ }
+
+ void onWarningDag(const DagInit& d,
+ unsigned IndentLevel, raw_ostream& O) const
+ {
+ checkNumberOfArguments(&d, 1);
+ O.indent(IndentLevel) << "llvm::errs() << \""
+ << InitPtrToString(d.getArg(0)) << "\";\n";
+ }
+
+};
+
+/// EmitActionHandlersCallback - Emit code that handles actions. Used by
+/// EmitGenerateActionMethod() as an argument to EmitCaseConstructHandler().
+class EmitActionHandlersCallback : ActionHandlingCallbackBase {
const OptionDescriptions& OptDescs;
void processActionDag(const Init* Statement, unsigned IndentLevel,
raw_ostream& O) const
{
const DagInit& Dag = InitPtrToDag(Statement);
- const std::string& ActionName = Dag.getOperator()->getAsString();
+ const std::string& ActionName = GetOperatorName(Dag);
if (ActionName == "append_cmd") {
checkNumberOfArguments(&Dag, 1);
@@ -1497,10 +1743,10 @@ class EmitActionHandler {
O.indent(IndentLevel) << "vec.push_back(\"" << *B << "\");\n";
}
else if (ActionName == "error") {
- O.indent(IndentLevel) << "throw std::runtime_error(\"" <<
- (Dag.getNumArgs() >= 1 ? InitPtrToString(Dag.getArg(0))
- : "Unknown error!")
- << "\");\n";
+ this->onErrorDag(Dag, IndentLevel, O);
+ }
+ else if (ActionName == "warning") {
+ this->onWarningDag(Dag, IndentLevel, O);
}
else if (ActionName == "forward") {
checkNumberOfArguments(&Dag, 1);
@@ -1554,21 +1800,13 @@ class EmitActionHandler {
}
}
public:
- EmitActionHandler(const OptionDescriptions& OD)
+ EmitActionHandlersCallback(const OptionDescriptions& OD)
: OptDescs(OD) {}
- void operator()(const Init* Statement, unsigned IndentLevel,
- raw_ostream& O) const
+ void operator()(const Init* Statement,
+ unsigned IndentLevel, raw_ostream& O) const
{
- if (typeid(*Statement) == typeid(ListInit)) {
- const ListInit& DagList = *static_cast<const ListInit*>(Statement);
- for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
- B != E; ++B)
- this->processActionDag(*B, IndentLevel, O);
- }
- else {
- this->processActionDag(Statement, IndentLevel, O);
- }
+ this->processActionDag(Statement, IndentLevel, O);
}
};
@@ -1593,9 +1831,20 @@ public:
{}
void operator()(const Init* CmdLine) {
+ // Ignore nested 'case' DAG.
+ if (typeid(*CmdLine) == typeid(DagInit))
+ return;
+
if (IsOutFileIndexCheckRequiredStr(CmdLine))
*ret_ = true;
}
+
+ void operator()(const DagInit* Test, unsigned, bool) {
+ this->operator()(Test);
+ }
+ void operator()(const Init* Statement, unsigned) {
+ this->operator()(Statement);
+ }
};
bool IsOutFileIndexCheckRequiredCase (Init* CmdLine) {
@@ -1652,7 +1901,7 @@ void EmitGenerateActionMethod (const ToolDescription& D,
// For every understood option, emit handling code.
if (D.Actions)
- EmitCaseConstructHandler(D.Actions, Indent2, EmitActionHandler(OptDescs),
+ EmitCaseConstructHandler(D.Actions, Indent2, EmitActionHandlersCallback(OptDescs),
false, OptDescs, O);
O << '\n';
@@ -1870,10 +2119,93 @@ void EmitOptionDefinitions (const OptionDescriptions& descs,
O << '\n';
}
-/// EmitPopulateLanguageMap - Emit the PopulateLanguageMap() function.
+/// EmitPreprocessOptionsCallback - Helper function passed to
+/// EmitCaseConstructHandler() by EmitPreprocessOptions().
+class EmitPreprocessOptionsCallback : ActionHandlingCallbackBase {
+ const OptionDescriptions& OptDescs_;
+
+ void onUnsetOption(Init* i, unsigned IndentLevel, raw_ostream& O) {
+ const std::string& OptName = InitPtrToString(i);
+ const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
+
+ if (OptDesc.isSwitch()) {
+ O.indent(IndentLevel) << OptDesc.GenVariableName() << " = false;\n";
+ }
+ else if (OptDesc.isParameter()) {
+ O.indent(IndentLevel) << OptDesc.GenVariableName() << " = \"\";\n";
+ }
+ else if (OptDesc.isList()) {
+ O.indent(IndentLevel) << OptDesc.GenVariableName() << ".clear();\n";
+ }
+ else {
+ throw "Can't apply 'unset_option' to alias option '" + OptName + "'";
+ }
+ }
+
+ void processDag(const Init* I, unsigned IndentLevel, raw_ostream& O)
+ {
+ const DagInit& d = InitPtrToDag(I);
+ const std::string& OpName = GetOperatorName(d);
+
+ if (OpName == "warning") {
+ this->onWarningDag(d, IndentLevel, O);
+ }
+ else if (OpName == "error") {
+ this->onWarningDag(d, IndentLevel, O);
+ }
+ else if (OpName == "unset_option") {
+ checkNumberOfArguments(&d, 1);
+ Init* I = d.getArg(0);
+ if (typeid(*I) == typeid(ListInit)) {
+ const ListInit& DagList = *static_cast<const ListInit*>(I);
+ for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
+ B != E; ++B)
+ this->onUnsetOption(*B, IndentLevel, O);
+ }
+ else {
+ this->onUnsetOption(I, IndentLevel, O);
+ }
+ }
+ else {
+ throw "Unknown operator in the option preprocessor: '" + OpName + "'!"
+ "\nOnly 'warning', 'error' and 'unset_option' are allowed.";
+ }
+ }
+
+public:
+
+ void operator()(const Init* I, unsigned IndentLevel, raw_ostream& O) {
+ this->processDag(I, IndentLevel, O);
+ }
+
+ EmitPreprocessOptionsCallback(const OptionDescriptions& OptDescs)
+ : OptDescs_(OptDescs)
+ {}
+};
+
+/// EmitPreprocessOptions - Emit the PreprocessOptionsLocal() function.
+void EmitPreprocessOptions (const RecordKeeper& Records,
+ const OptionDescriptions& OptDecs, raw_ostream& O)
+{
+ O << "void PreprocessOptionsLocal() {\n";
+
+ const RecordVector& OptionPreprocessors =
+ Records.getAllDerivedDefinitions("OptionPreprocessor");
+
+ for (RecordVector::const_iterator B = OptionPreprocessors.begin(),
+ E = OptionPreprocessors.end(); B!=E; ++B) {
+ DagInit* Case = (*B)->getValueAsDag("preprocessor");
+ EmitCaseConstructHandler(Case, Indent1,
+ EmitPreprocessOptionsCallback(OptDecs),
+ false, OptDecs, O);
+ }
+
+ O << "}\n\n";
+}
+
+/// EmitPopulateLanguageMap - Emit the PopulateLanguageMapLocal() function.
void EmitPopulateLanguageMap (const RecordKeeper& Records, raw_ostream& O)
{
- // Generate code
O << "void PopulateLanguageMapLocal(LanguageMap& langMap) {\n";
// Get the relevant field out of RecordKeeper
@@ -1907,7 +2239,7 @@ void EmitPopulateLanguageMap (const RecordKeeper& Records, raw_ostream& O)
void IncDecWeight (const Init* i, unsigned IndentLevel,
raw_ostream& O) {
const DagInit& d = InitPtrToDag(i);
- const std::string& OpName = d.getOperator()->getAsString();
+ const std::string& OpName = GetOperatorName(d);
if (OpName == "inc_weight") {
O.indent(IndentLevel) << "ret += ";
@@ -1916,17 +2248,16 @@ void IncDecWeight (const Init* i, unsigned IndentLevel,
O.indent(IndentLevel) << "ret -= ";
}
else if (OpName == "error") {
- O.indent(IndentLevel)
- << "throw std::runtime_error(\"" <<
- (d.getNumArgs() >= 1 ? InitPtrToString(d.getArg(0))
- : "Unknown error!")
- << "\");\n";
+ checkNumberOfArguments(&d, 1);
+ O.indent(IndentLevel) << "throw std::runtime_error(\""
+ << InitPtrToString(d.getArg(0))
+ << "\");\n";
return;
}
-
- else
- throw "Unknown operator in edge properties list: " + OpName + '!' +
+ else {
+ throw "Unknown operator in edge properties list: '" + OpName + "'!"
"\nOnly 'inc_weight', 'dec_weight' and 'error' are allowed.";
+ }
if (d.getNumArgs() > 0)
O << InitPtrToInt(d.getArg(0)) << ";\n";
@@ -1975,7 +2306,7 @@ void EmitEdgeClasses (const RecordVector& EdgeVector,
}
}
-/// EmitPopulateCompilationGraph - Emit the PopulateCompilationGraph()
+/// EmitPopulateCompilationGraph - Emit the PopulateCompilationGraphLocal()
/// function.
void EmitPopulateCompilationGraph (const RecordVector& EdgeVector,
const ToolDescriptions& ToolDescs,
@@ -2024,6 +2355,11 @@ public:
void operator()(const Init* CmdLine) {
StrVector cmds;
+
+ // Ignore nested 'case' DAG.
+ if (typeid(*CmdLine) == typeid(DagInit))
+ return;
+
TokenizeCmdline(InitPtrToString(CmdLine), cmds);
for (StrVector::const_iterator B = cmds.begin(), E = cmds.end();
B != E; ++B) {
@@ -2053,6 +2389,13 @@ public:
}
}
}
+
+ void operator()(const DagInit* Test, unsigned, bool) {
+ this->operator()(Test);
+ }
+ void operator()(const Init* Statement, unsigned) {
+ this->operator()(Statement);
+ }
};
/// FillInHookNames - Actually extract the hook names from all command
@@ -2104,6 +2447,8 @@ void EmitRegisterPlugin(int Priority, raw_ostream& O) {
O << "struct Plugin : public llvmc::BasePlugin {\n\n";
O.indent(Indent1) << "int Priority() const { return "
<< Priority << "; }\n\n";
+ O.indent(Indent1) << "void PreprocessOptions() const\n";
+ O.indent(Indent1) << "{ PreprocessOptionsLocal(); }\n\n";
O.indent(Indent1) << "void PopulateLanguageMap(LanguageMap& langMap) const\n";
O.indent(Indent1) << "{ PopulateLanguageMapLocal(langMap); }\n\n";
O.indent(Indent1)
@@ -2123,7 +2468,8 @@ void EmitIncludes(raw_ostream& O) {
<< "#include \"llvm/CompilerDriver/Tool.h\"\n\n"
<< "#include \"llvm/ADT/StringExtras.h\"\n"
- << "#include \"llvm/Support/CommandLine.h\"\n\n"
+ << "#include \"llvm/Support/CommandLine.h\"\n"
+ << "#include \"llvm/Support/raw_ostream.h\"\n\n"
<< "#include <cstdlib>\n"
<< "#include <stdexcept>\n\n"
@@ -2223,8 +2569,11 @@ void EmitPluginCode(const PluginData& Data, raw_ostream& O) {
O << "namespace {\n\n";
- // Emit PopulateLanguageMap() function
- // (a language map maps from file extensions to language names).
+ // Emit PreprocessOptionsLocal() function.
+ EmitPreprocessOptions(Records, Data.OptDescs, O);
+
+ // Emit PopulateLanguageMapLocal() function
+ // (language map maps from file extensions to language names).
EmitPopulateLanguageMap(Records, O);
// Emit Tool classes.
@@ -2235,7 +2584,7 @@ void EmitPluginCode(const PluginData& Data, raw_ostream& O) {
// Emit Edge# classes.
EmitEdgeClasses(Data.Edges, Data.OptDescs, O);
- // Emit PopulateCompilationGraph() function.
+ // Emit PopulateCompilationGraphLocal() function.
EmitPopulateCompilationGraph(Data.Edges, Data.ToolDescs, O);
// Emit code for plugin registration.
diff --git a/utils/TableGen/LLVMCConfigurationEmitter.h b/utils/TableGen/LLVMCConfigurationEmitter.h
index 347f6f1..b37b83f 100644
--- a/utils/TableGen/LLVMCConfigurationEmitter.h
+++ b/utils/TableGen/LLVMCConfigurationEmitter.h
@@ -21,9 +21,8 @@ namespace llvm {
/// LLVMCConfigurationEmitter - TableGen backend that generates
/// configuration code for LLVMC.
class LLVMCConfigurationEmitter : public TableGenBackend {
- RecordKeeper &Records;
public:
- explicit LLVMCConfigurationEmitter(RecordKeeper &R) : Records(R) {}
+ explicit LLVMCConfigurationEmitter(RecordKeeper&) {}
// run - Output the asmwriter, returning true on failure.
void run(raw_ostream &o);
diff --git a/utils/TableGen/StringToOffsetTable.h b/utils/TableGen/StringToOffsetTable.h
index d9d7cf4..ac9422c 100644
--- a/utils/TableGen/StringToOffsetTable.h
+++ b/utils/TableGen/StringToOffsetTable.h
@@ -10,9 +10,10 @@
#ifndef TBLGEN_STRING_TO_OFFSET_TABLE_H
#define TBLGEN_STRING_TO_OFFSET_TABLE_H
+#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringMap.h"
-#include "llvm/Support/raw_ostream.h"
#include "llvm/ADT/StringExtras.h"
+#include "llvm/Support/raw_ostream.h"
namespace llvm {
@@ -38,9 +39,13 @@ public:
}
void EmitString(raw_ostream &O) {
+ // Escape the string.
+ SmallString<256> Str;
+ raw_svector_ostream(Str).write_escaped(AggregateString);
+ AggregateString = Str.str();
+
O << " \"";
unsigned CharsPrinted = 0;
- EscapeString(AggregateString);
for (unsigned i = 0, e = AggregateString.size(); i != e; ++i) {
if (CharsPrinted > 70) {
O << "\"\n \"";
OpenPOWER on IntegriCloud