summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Weigl-Bosker <stefan@s00.xyz>2026-01-11 21:34:23 -0500
committerGitHub <noreply@github.com>2026-01-11 21:34:23 -0500
commit21668be4e41ee27c0cf2f83d3d79ed88b0257d4d (patch)
treecfa836d244b9cc1194e3be817453af59d7166863
parent3f60b0f7f43815f7cd38f744d3fbe56d04b6d8c3 (diff)
downloadcompiler-21668be4e41ee27c0cf2f83d3d79ed88b0257d4d.tar.gz
[willow]: Diagnostics and plumbing (#2)
-rw-r--r--BUILD.bazel (renamed from BUILD)0
-rw-r--r--willow/include/willow/IR/Context.h18
-rw-r--r--willow/include/willow/IR/Diagnostic.h30
-rw-r--r--willow/include/willow/IR/DiagnosticEngine.h83
-rw-r--r--willow/include/willow/IR/Instruction.h3
-rw-r--r--willow/include/willow/Util/LogicalResult.h14
-rw-r--r--willow/lib/IR/Instruction.cpp1
7 files changed, 149 insertions, 0 deletions
diff --git a/BUILD b/BUILD.bazel
index 6cbf83c..6cbf83c 100644
--- a/BUILD
+++ b/BUILD.bazel
diff --git a/willow/include/willow/IR/Context.h b/willow/include/willow/IR/Context.h
new file mode 100644
index 0000000..3468e8f
--- /dev/null
+++ b/willow/include/willow/IR/Context.h
@@ -0,0 +1,18 @@
+#ifndef WILLOW_INCLUDE_CONTEXT_H
+#define WILLOW_INCLUDE_CONTEXT_H
+
+#include <willow/IR/Module.h>
+#include <willow/IR/TypeContext.h>
+
+namespace willow {
+
+/// The global context. Contains all shared state.
+class WillowContext {
+ TypeContext ctx;
+
+ std::vector<std::unique_ptr<Module>> modules;
+};
+
+} // namespace willow
+
+#endif // WILLOW_INCLUDE_CONTEXT_H
diff --git a/willow/include/willow/IR/Diagnostic.h b/willow/include/willow/IR/Diagnostic.h
new file mode 100644
index 0000000..2a74898
--- /dev/null
+++ b/willow/include/willow/IR/Diagnostic.h
@@ -0,0 +1,30 @@
+#ifndef WILLOW_INCLUDE_IR_DIAGNOSTIC_H
+#define WILLOW_INCLUDE_IR_DIAGNOSTIC_H
+
+#include <willow/IR/Module.h>
+
+#include <cstdint>
+#include <string>
+
+namespace willow {
+
+enum class Severity { Debug, Remark, Warning, Error };
+
+struct Location {
+ std::string filename;
+ int line;
+ int col;
+};
+
+struct Diagnostic {
+ Severity severity;
+ std::string message;
+ std::optional<Location> location;
+ /// Extra lines that will be printed after the diagnostic. Call sites, extra
+ /// info, etc
+ std::vector<std::string> notes;
+};
+
+} // namespace willow
+
+#endif // WILLOW_INCLUDE_SUPPORT_DIAGNOSTICS_H
diff --git a/willow/include/willow/IR/DiagnosticEngine.h b/willow/include/willow/IR/DiagnosticEngine.h
new file mode 100644
index 0000000..2293734
--- /dev/null
+++ b/willow/include/willow/IR/DiagnosticEngine.h
@@ -0,0 +1,83 @@
+#ifndef WILLOW_INCLUDE_IR_DIAGNOSTIC_ENGINE_H
+#define WILLOW_INCLUDE_IR_DIAGNOSTIC_ENGINE_H
+
+/// \file DiagnosticEngine.h Portable diagnostic handling
+
+#include <willow/IR/Diagnostic.h>
+#include <willow/Util/LogicalResult.h>
+
+#include <sstream>
+
+namespace willow {
+
+class DiagnosticEngine {
+public:
+ using Handler = std::function<void(const Diagnostic &)>;
+
+ DiagnosticEngine() = default;
+ explicit DiagnosticEngine(Handler h) : handler(std::move(h)) {}
+
+ void setHandler(Handler h) { handler = std::move(h); }
+
+ void report(Diagnostic d) { // NOLINT(performance-unnecessary-value-param)
+ if (handler)
+ handler(d);
+ }
+
+private:
+ Handler handler;
+};
+
+/// Helper class for building, propagating and reporting a Diagnostic.
+class DiagnosticBuilder {
+ DiagnosticEngine *engine = nullptr;
+ Diagnostic diag;
+ std::ostringstream os;
+
+public:
+ DiagnosticBuilder(const DiagnosticBuilder &) = delete;
+ DiagnosticBuilder &operator=(const DiagnosticBuilder &) = delete;
+
+ DiagnosticBuilder(DiagnosticEngine &eng, Severity severity,
+ std::optional<Location> loc)
+ : engine(&eng) {
+ diag.severity = severity;
+ diag.location = std::move(loc);
+ }
+
+ DiagnosticBuilder(DiagnosticBuilder &&other) noexcept
+ : engine(std::exchange(other.engine, nullptr)),
+ diag(std::move(other.diag)), os(std::move(other.os)) {}
+
+ ~DiagnosticBuilder() {
+ if (!engine) // was moved from
+ return;
+
+ diag.message += os.str();
+ engine->report(std::move(diag));
+ }
+
+ template <typename T>
+ DiagnosticBuilder &operator<<(T &&x) {
+ os << std::forward<T>(x);
+ }
+
+ /// Add a note to the diagnostic. Will be printed on its own line, following
+ /// the diagnostic itself.
+ DiagnosticBuilder &note(std::string n) {
+ diag.notes.push_back(std::move(n));
+ return *this;
+ }
+
+ operator LogicalResult() const { return LogicalResult::Failure; }
+};
+
+/// DiagnosticBuilder factory.
+inline DiagnosticBuilder emit(DiagnosticEngine &eng, Severity severity,
+ std::optional<Location> loc = std::nullopt) {
+ return DiagnosticBuilder(eng, severity, std::move(loc));
+}
+
+} // namespace willow
+
+#endif // WILLOW_INCLUDE_IR_DIAGNOSTIC_ENGINE_H
diff --git a/willow/include/willow/IR/Instruction.h b/willow/include/willow/IR/Instruction.h
index a4dfbbb..f1bd0e6 100644
--- a/willow/include/willow/IR/Instruction.h
+++ b/willow/include/willow/IR/Instruction.h
@@ -80,6 +80,8 @@ public:
operand->delUse(this);
}
+ virtual bool verify(std::ostream &os) = 0;
+
bool hasParent() const { return parent != nullptr; }
BasicBlock *getParent() { return parent; }
const BasicBlock *getParent() const { return parent; }
@@ -107,6 +109,7 @@ public:
}
};
+/// The base class for unary arithemetic instructions
class UnaryInst : public Instruction {
public:
UnaryInst(std::string name, Type type, Op op, Value *value)
diff --git a/willow/include/willow/Util/LogicalResult.h b/willow/include/willow/Util/LogicalResult.h
new file mode 100644
index 0000000..3a345eb
--- /dev/null
+++ b/willow/include/willow/Util/LogicalResult.h
@@ -0,0 +1,14 @@
+#ifndef WILLOW_INCLUDE_LOGICAL_RESULT_H
+#define WILLOW_INCLUDE_LOGICAL_RESULT_H
+
+#include <cstdint>
+
+namespace willow {
+
+enum class LogicalResult : uint8_t { Success, Failure };
+inline bool succeeded(LogicalResult r) { return r == LogicalResult::Success; }
+inline bool failed(LogicalResult r) { return r == LogicalResult::Failure; }
+
+} // namespace willow
+
+#endif // WILLOW_INCLUDE_LOGICAL_RESULT_H
diff --git a/willow/lib/IR/Instruction.cpp b/willow/lib/IR/Instruction.cpp
new file mode 100644
index 0000000..21a4cae
--- /dev/null
+++ b/willow/lib/IR/Instruction.cpp
@@ -0,0 +1 @@
+#include <willow/IR/Instruction.h>