summaryrefslogtreecommitdiff
path: root/willow/unittest/IR/InstructionsTest.cpp
blob: acb83a1a5fe15123b537162a26b97e970358eb79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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)));
  }

}