summaryrefslogtreecommitdiff
path: root/willow/lib/IR/IRBuilder.cpp
diff options
context:
space:
mode:
authorStefan Weigl-Bosker <stefan@s00.xyz>2026-02-19 13:13:41 -0500
committerGitHub <noreply@github.com>2026-02-19 13:13:41 -0500
commit1fd2d6d88f5f78d879bf38bb3fba7fa2e749d3b0 (patch)
treeeb5a0740956812678131970687377339fad5a541 /willow/lib/IR/IRBuilder.cpp
parentadd95b14f74e6dbe04a6efe98ff0f20424930b73 (diff)
downloadcompiler-1fd2d6d88f5f78d879bf38bb3fba7fa2e749d3b0.tar.gz
[willow]: initial IRBuilder API (#9)
- add IRBuilder api - remove `name` field from `Value` - fix some bugs in IList interface - more verifier tests
Diffstat (limited to 'willow/lib/IR/IRBuilder.cpp')
-rw-r--r--willow/lib/IR/IRBuilder.cpp210
1 files changed, 210 insertions, 0 deletions
diff --git a/willow/lib/IR/IRBuilder.cpp b/willow/lib/IR/IRBuilder.cpp
new file mode 100644
index 0000000..62e7a98
--- /dev/null
+++ b/willow/lib/IR/IRBuilder.cpp
@@ -0,0 +1,210 @@
+#include <willow/IR/IRBuilder.h>
+
+namespace willow {
+
+AddInst *IRBuilder::BuildAdd(Type type, Value *lhs, Value *rhs,
+ std::optional<Location> loc) {
+ auto *inst = new AddInst{type, lhs, rhs, loc};
+ insert_point->insertAfter(*inst);
+ insert_point++;
+
+ return inst;
+}
+
+MulInst *IRBuilder::BuildMul(Type type, Value *lhs, Value *rhs,
+ std::optional<Location> loc) {
+ auto *inst = new MulInst{type, lhs, rhs, loc};
+ insert_point->insertAfter(*inst);
+ insert_point++;
+
+ return inst;
+}
+
+SubInst *IRBuilder::BuildSub(Type type, Value *lhs, Value *rhs,
+ std::optional<Location> loc) {
+ auto *inst = new SubInst{type, lhs, rhs, loc};
+ insert_point->insertAfter(*inst);
+ insert_point++;
+
+ return inst;
+}
+
+DivInst *IRBuilder::BuildDiv(Type type, Value *lhs, Value *rhs,
+ std::optional<Location> loc) {
+ auto *inst = new DivInst{type, lhs, rhs, loc};
+ insert_point->insertAfter(*inst);
+ insert_point++;
+
+ return inst;
+}
+
+ModInst *IRBuilder::BuildMod(Type type, Value *lhs, Value *rhs,
+ std::optional<Location> loc) {
+ auto *inst = new ModInst{type, lhs, rhs, loc};
+ insert_point->insertAfter(*inst);
+ insert_point++;
+
+ return inst;
+}
+
+ShlInst *IRBuilder::BuildShl(Type type, Value *lhs, Value *rhs,
+ std::optional<Location> loc) {
+ auto *inst = new ShlInst{type, lhs, rhs, loc};
+ insert_point->insertAfter(*inst);
+ insert_point++;
+
+ return inst;
+}
+
+ShrInst *IRBuilder::BuildShr(Type type, Value *lhs, Value *rhs,
+ std::optional<Location> loc) {
+ auto *inst = new ShrInst{type, lhs, rhs, loc};
+ insert_point->insertAfter(*inst);
+ insert_point++;
+
+ return inst;
+}
+
+AshlInst *IRBuilder::BuildAshl(Type type, Value *lhs, Value *rhs,
+ std::optional<Location> loc) {
+ auto *inst = new AshlInst{type, lhs, rhs, loc};
+ insert_point->insertAfter(*inst);
+ insert_point++;
+
+ return inst;
+}
+
+AshrInst *IRBuilder::BuildAshr(Type type, Value *lhs, Value *rhs,
+ std::optional<Location> loc) {
+ auto *inst = new AshrInst{type, lhs, rhs, loc};
+ insert_point->insertAfter(*inst);
+ insert_point++;
+
+ return inst;
+}
+
+EqInst *IRBuilder::BuildEq(Value *lhs, Value *rhs,
+ std::optional<Location> loc) {
+ auto *inst = new EqInst{ctx.types().IntType(1), lhs, rhs, loc};
+ insert_point->insertAfter(*inst);
+ insert_point++;
+
+ return inst;
+}
+
+LtInst *IRBuilder::BuildLt(Value *lhs, Value *rhs,
+ std::optional<Location> loc) {
+ auto *inst = new LtInst{ctx.types().IntType(1), lhs, rhs, loc};
+ insert_point->insertAfter(*inst);
+ insert_point++;
+
+ return inst;
+}
+
+GtInst *IRBuilder::BuildGt(Value *lhs, Value *rhs,
+ std::optional<Location> loc) {
+ auto *inst = new GtInst{ctx.types().IntType(1), lhs, rhs, loc};
+ insert_point->insertAfter(*inst);
+ insert_point++;
+
+ return inst;
+}
+
+LeInst *IRBuilder::BuildLe(Value *lhs, Value *rhs,
+ std::optional<Location> loc) {
+ auto *inst = new LeInst{ctx.types().IntType(1), lhs, rhs, loc};
+ insert_point->insertAfter(*inst);
+ insert_point++;
+
+ return inst;
+}
+
+GeInst *IRBuilder::BuildGe(Value *lhs, Value *rhs,
+ std::optional<Location> loc) {
+ auto *inst = new GeInst{ctx.types().IntType(1), lhs, rhs, loc};
+ insert_point->insertAfter(*inst);
+ insert_point++;
+
+ return inst;
+}
+
+AndInst *IRBuilder::BuildAnd(Value *lhs, Value *rhs,
+ std::optional<Location> loc) {
+ auto *inst = new AndInst{ctx.types().IntType(1), lhs, rhs, loc};
+ insert_point->insertAfter(*inst);
+ insert_point++;
+
+ return inst;
+}
+
+OrInst *IRBuilder::BuildOr(Value *lhs, Value *rhs,
+ std::optional<Location> loc) {
+ auto *inst = new OrInst{ctx.types().IntType(1), lhs, rhs, loc};
+ insert_point->insertAfter(*inst);
+ insert_point++;
+
+ return inst;
+}
+
+NotInst *IRBuilder::BuildNot(Value *val, std::optional<Location> loc) {
+ auto *inst = new NotInst{ctx.types().IntType(1), val, loc};
+ insert_point->insertAfter(*inst);
+ insert_point++;
+
+ return inst;
+}
+
+JmpInst *IRBuilder::BuildJmp(BasicBlock *dst, std::optional<Location> loc) {
+
+ auto *inst = new JmpInst{ctx.types().VoidType(), dst, loc};
+ insert_point->insertAfter(*inst);
+ insert_point++;
+
+ return inst;
+}
+
+BrInst *IRBuilder::BuildBr(Value *predicate, BasicBlock *truedst,
+ BasicBlock *falsedst, std::optional<Location> loc) {
+ auto *inst =
+ new BrInst{ctx.types().VoidType(), predicate, truedst, falsedst, loc};
+ insert_point->insertAfter(*inst);
+ insert_point++;
+
+ return inst;
+}
+
+RetInst *IRBuilder::BuildRet(Value *val, std::optional<Location> loc) {
+ auto *inst = new RetInst{ctx.types().VoidType(), val, loc};
+ insert_point->insertAfter(*inst);
+ insert_point++;
+
+ return inst;
+}
+RetInst *IRBuilder::BuildRet(std::optional<Location> loc) {
+ auto *inst = new RetInst{ctx.types().VoidType(), loc};
+ insert_point->insertAfter(*inst);
+ insert_point++;
+
+ return inst;
+}
+
+PhiInst *IRBuilder::BuildPhi(
+ Type ty, std::initializer_list<std::pair<BasicBlock *, Value *>> args,
+ std::optional<Location> loc) {
+ auto *inst = new PhiInst(ty, args, loc);
+ insert_point->insertAfter(*inst);
+ insert_point++;
+
+ return inst;
+}
+
+AllocaInst *IRBuilder::BuildAlloca(Type ty, std::optional<Location> loc) {
+ Type pty = ctx.types().PtrType(ty);
+ auto *inst = new AllocaInst(pty, loc);
+ insert_point->insertAfter(*inst);
+ insert_point++;
+
+ return inst;
+}
+
+}; // namespace willow