aboutsummaryrefslogtreecommitdiff
path: root/backend/lib/MIR/MIRDocumentParser.cpp
diff options
context:
space:
mode:
authorRichard-Qin-X <richard.qin.001@gmail.com>2026-08-11 17:37:42 +0800
committerRichard-Qin-X <richard.qin.001@gmail.com>2026-08-11 17:37:42 +0800
commit6a7b083f322f4ac47e58cbf9d4d5fd4d1f6fecd0 (patch)
treef363c502f0e2b9cda2f12928049275a433db6ad4 /backend/lib/MIR/MIRDocumentParser.cpp
parentadcf8c9b1bd1b47e00bf859693112cea1f7e8c9f (diff)
downloadrcc-project-6a7b083f322f4ac47e58cbf9d4d5fd4d1f6fecd0.tar.gz
rcc-project-6a7b083f322f4ac47e58cbf9d4d5fd4d1f6fecd0.tar.xz
[MIR] Accept fixed-baseline LLVM syntax
Diffstat (limited to 'backend/lib/MIR/MIRDocumentParser.cpp')
-rw-r--r--backend/lib/MIR/MIRDocumentParser.cpp46
1 files changed, 35 insertions, 11 deletions
diff --git a/backend/lib/MIR/MIRDocumentParser.cpp b/backend/lib/MIR/MIRDocumentParser.cpp
index defcce9..ca2c6cd 100644
--- a/backend/lib/MIR/MIRDocumentParser.cpp
+++ b/backend/lib/MIR/MIRDocumentParser.cpp
@@ -49,8 +49,6 @@ using parser_detail::trim;
bool MIRDocumentParser::fail(const TextRange &Range, std::string Message, std::string Category) {
if (Result.Diagnostics.empty()) {
- if (ActiveFunction != nullptr)
- Message = "function '" + std::string(ActiveFunction->name()) + "': " + std::move(Message);
Result.Diagnostics.push_back(TextDiagnostic{
.Severity = DiagnosticSeverity::Error,
.Category = std::move(Category),
@@ -210,7 +208,14 @@ MIRDocumentParseResult MIRDocumentParser::run() {
std::string(Result.Source.name()) + ":embedded-ir", std::string(FirstRoot->scalar()));
if (!Embedded.succeeded()) {
const auto Diagnostics = Embedded.diagnostics();
- Result.Diagnostics.assign(Diagnostics.begin(), Diagnostics.end());
+ if (Diagnostics.empty()) {
+ static_cast<void>(fail(FirstRoot->range(), "embedded LLVM IR indexing failed",
+ "mir-embedded-ir"));
+ } else {
+ const TextDiagnostic &Diagnostic = Diagnostics.front();
+ const TextRange Range = scalarDiagnosticRange(*FirstRoot, Diagnostic.Range);
+ static_cast<void>(fail(Range, Diagnostic.Message, Diagnostic.Category));
+ }
return std::move(Result);
}
Staging.EmbeddedIR = std::move(Embedded).takeIndex();
@@ -301,22 +306,36 @@ bool MIRDocumentParser::parseFunction(const yaml::Node &Root, MIRDocument &Docum
std::string_view{"prefetch-targets"},
std::string_view{"body"},
};
- if (validateKeys(Root, Keys, "machine function"))
- return true;
const yaml::Node *NameNode = mappingValue(Root, "name");
if (NameNode == nullptr)
- return fail(Root.range(), "machine function requires 'name'");
+ return fail(Root.range(), "missing required key 'name'");
+ if (validateKeys(Root, Keys, "machine function"))
+ return true;
std::string_view Name;
- if (scalar(*NameNode, Name, "machine function name") || Name.empty())
- return fail(NameNode->range(), "machine function name cannot be empty");
+ if (scalar(*NameNode, Name, "machine function name"))
+ return true;
+ const EmbeddedIRFunction *IRFunction = nullptr;
if (Document.EmbeddedIR) {
- const EmbeddedIRFunction *IRFunction = Document.EmbeddedIR->function(Name);
+ if (Name.empty()) {
+ std::size_t AnonymousOrdinal = 0;
+ for (std::size_t Index = 0; Index < Document.Unit.functionCount(); ++Index) {
+ const MachineFunction *Existing = Document.Unit.functionAt(Index);
+ if (Existing != nullptr && Existing->name().empty())
+ ++AnonymousOrdinal;
+ }
+ IRFunction = Document.EmbeddedIR->unnamedFunction(AnonymousOrdinal);
+ } else {
+ IRFunction = Document.EmbeddedIR->function(Name);
+ }
if (IRFunction == nullptr)
- return fail(NameNode->range(),
- "machine function has no corresponding embedded LLVM IR function");
+ return fail(NameNode->range(), "function '" + std::string(Name) +
+ "' isn't defined in the provided LLVM IR");
}
+ if (!Name.empty() && Document.Unit.function(Name) != nullptr)
+ return fail(NameNode->range(), "redefinition of machine function '" + std::string(Name) + "'");
+
auto Created = Document.Unit.createMachineFunction(std::string(Name));
if (!Created)
return importError(NameNode->range(), Created.error());
@@ -332,6 +351,11 @@ bool MIRDocumentParser::parseFunction(const yaml::Node &Root, MIRDocument &Docum
};
MIParserContext Context;
Context.DefaultPointerSizeInBits = Options.DefaultPointerSizeInBits;
+ if (IRFunction != nullptr) {
+ Context.HasIRBlock = [IRFunction](std::string_view BlockName) {
+ return IRFunction->block(BlockName) != nullptr;
+ };
+ }
std::vector<PendingJumpTable> PendingJumpTables;
if (parseFunctionScalars(Root, Function, DocumentInfo) ||