summaryrefslogtreecommitdiff
path: root/willow/unittest/IR/InstructionsTest.cpp
diff options
context:
space:
mode:
authorStefan Weigl-Bosker <stefan@s00.xyz>2026-02-19 18:45:44 -0500
committerStefan Weigl-Bosker <stefan@s00.xyz>2026-02-19 18:45:44 -0500
commitaf3d0ad1926eb825f64152cf217fc9a4777f0be3 (patch)
tree8d7f73ce6c3c89863418382d8d553a06c668bbb3 /willow/unittest/IR/InstructionsTest.cpp
parentd11fbc8268f5775ad783f8570478daad4a9e81cf (diff)
downloadcompiler-more-tests.tar.gz
[willow]: more cleanup, testsmore-tests
Diffstat (limited to 'willow/unittest/IR/InstructionsTest.cpp')
-rw-r--r--willow/unittest/IR/InstructionsTest.cpp141
1 files changed, 141 insertions, 0 deletions
diff --git a/willow/unittest/IR/InstructionsTest.cpp b/willow/unittest/IR/InstructionsTest.cpp
new file mode 100644
index 0000000..acb83a1
--- /dev/null
+++ b/willow/unittest/IR/InstructionsTest.cpp
@@ -0,0 +1,141 @@
+#include <catch2/catch_test_macros.hpp>
+
+#include <willow/IR/Context.h>
+#include <willow/IR/Diagnostic.h>
+#include <willow/IR/DiagnosticEngine.h>
+#include <willow/IR/IRBuilder.h>
+#include <willow/IR/Instructions.h>
+#include <willow/IR/Verifier.h>
+
+#include <iostream>
+
+using namespace willow;
+
+TEST_CASE("valid arithmetic instructions") {
+ WillowContext ctx;
+ DiagnosticEngine eng;
+ TypeContext &types = ctx.types();
+ ConstantPool &consts = ctx.constants();
+
+ Type i64Ty = types.IntType(64);
+ Type i34Ty = types.IntType(34);
+ Type voidTy = types.VoidType();
+ Type voidFnTy = types.FunctionType(voidTy, {});
+
+ Value *one64 = consts.getInt(i64Ty, 1);
+ Value *two64 = consts.getInt(i64Ty, 2);
+ Value *twelve34 = consts.getInt(i34Ty, 12);
+
+ Module *m = ctx.addModule("testmod");
+
+ Function *fn = m->emplaceFunction("fn", m, voidFnTy);
+
+ eng.setHandler([](const Diagnostic& d) { std::cout << d << "\n"; });
+
+ BasicBlock *bb =
+ fn->addBlock(std::make_unique<BasicBlock>(fn, types.BasicBlockType()));
+ IRBuilder builder{ctx, *bb, bb->begin()};
+
+ SECTION("Valid add instruction") {
+ auto &inst = *builder.buildAdd(i64Ty, one64, two64);
+ REQUIRE(inst.opcode() == Instruction::Opcode::Add);
+ REQUIRE(succeeded(verifyInst(ctx, inst, eng)));
+ }
+
+ SECTION("Valid mul instruction") {
+ auto &inst = *builder.buildMul(i34Ty, twelve34, twelve34);
+ REQUIRE(inst.opcode() == Instruction::Opcode::Mul);
+ REQUIRE(succeeded(verifyInst(ctx, inst, eng)));
+ }
+
+ SECTION("Valid sub instruction") {
+ auto &inst = *builder.buildSub(i64Ty, one64, two64);
+ REQUIRE(inst.opcode() == Instruction::Opcode::Sub);
+ REQUIRE(succeeded(verifyInst(ctx, inst, eng)));
+ }
+
+ SECTION("Valid div instruction") {
+ auto &inst = *builder.buildDiv(i64Ty, one64, two64);
+ REQUIRE(inst.opcode() == Instruction::Opcode::Div);
+ REQUIRE(succeeded(verifyInst(ctx, inst, eng)));
+ }
+
+ SECTION("Valid mod instruction") {
+ auto &inst = *builder.buildMod(i64Ty, one64, two64);
+ REQUIRE(inst.opcode() == Instruction::Opcode::Mod);
+ REQUIRE(succeeded(verifyInst(ctx, inst, eng)));
+ }
+
+ SECTION("Valid shl instruction") {
+ auto &inst = *builder.buildShl(i64Ty, one64, two64);
+ REQUIRE(inst.opcode() == Instruction::Opcode::Shl);
+ REQUIRE(succeeded(verifyInst(ctx, inst, eng)));
+ }
+
+ SECTION("Valid shr instruction") {
+ auto &inst = *builder.buildShr(i64Ty, one64, two64);
+ REQUIRE(inst.opcode() == Instruction::Opcode::Shr);
+ REQUIRE(succeeded(verifyInst(ctx, inst, eng)));
+ }
+
+ SECTION("Valid ashl instruction") {
+ auto &inst = *builder.buildAshl(i64Ty, one64, two64);
+ REQUIRE(inst.opcode() == Instruction::Opcode::Ashl);
+ REQUIRE(succeeded(verifyInst(ctx, inst, eng)));
+ }
+
+ SECTION("Valid ashr instruction") {
+ auto &inst = *builder.buildAshr(i64Ty, one64, two64);
+ REQUIRE(inst.opcode() == Instruction::Opcode::Ashr);
+ REQUIRE(succeeded(verifyInst(ctx, inst, eng)));
+ }
+
+ SECTION("Valid and instruction") {
+ auto &inst = *builder.buildAnd(i64Ty, one64, two64);
+ REQUIRE(inst.opcode() == Instruction::Opcode::And);
+ REQUIRE(succeeded(verifyInst(ctx, inst, eng)));
+ }
+
+ SECTION("Valid not instruction") {
+ auto &inst = *builder.buildNot(i34Ty, twelve34);
+ REQUIRE(inst.opcode() == Instruction::Opcode::Not);
+ REQUIRE(succeeded(verifyInst(ctx, inst, eng)));
+ }
+
+ SECTION("Valid or instruction") {
+ auto &inst = *builder.buildOr(i64Ty, one64, two64);
+ REQUIRE(inst.opcode() == Instruction::Opcode::Or);
+ REQUIRE(succeeded(verifyInst(ctx, inst, eng)));
+ }
+
+ SECTION("Valid eq Instruction") {
+ auto &inst = *builder.buildEq(one64, two64);
+ REQUIRE(inst.opcode() == Instruction::Opcode::Eq);
+ REQUIRE(succeeded(verifyInst(ctx, inst, eng)));
+ }
+
+ SECTION("Valid lt Instruction") {
+ auto &inst = *builder.buildEq(one64, two64);
+ REQUIRE(inst.opcode() == Instruction::Opcode::Eq);
+ REQUIRE(succeeded(verifyInst(ctx, inst, eng)));
+ }
+
+ SECTION("Valid gt instruction") {
+ auto &inst = *builder.buildGt(one64, two64);
+ REQUIRE(inst.opcode() == Instruction::Opcode::Gt);
+ REQUIRE(succeeded(verifyInst(ctx, inst, eng)));
+ }
+
+ SECTION("Valid le instruction") {
+ auto &inst = *builder.buildLe(one64, two64);
+ REQUIRE(inst.opcode() == Instruction::Opcode::Le);
+ REQUIRE(succeeded(verifyInst(ctx, inst, eng)));
+ }
+
+ SECTION("Valid ge instruction") {
+ auto &inst = *builder.buildGe(one64, two64);
+ REQUIRE(inst.opcode() == Instruction::Opcode::Ge);
+ REQUIRE(succeeded(verifyInst(ctx, inst, eng)));
+ }
+
+}