aboutsummaryrefslogtreecommitdiffhomepage
path: root/externals/biscuit/tests/src/assembler_branch_tests.cpp
blob: ed0c7f23a15f7e79ab2a7d707c8074f2fac04da7 (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
#include <catch/catch.hpp>

#include <array>
#include <biscuit/assembler.hpp>

#include "assembler_test_utils.hpp"

using namespace biscuit;

TEST_CASE("Branch to Self", "[branch]") {
    uint32_t data;
    auto as = MakeAssembler32(data);

    // Simple branch to self with a jump instruction.
    {
        Label label;
        as.Bind(&label);
        as.J(&label);
        REQUIRE(data == 0x0000006F);
    }

    as.RewindBuffer();

    // Simple branch to self with a compressed jump instruction.
    {
        Label label;
        as.Bind(&label);
        as.C_J(&label);
        REQUIRE((data & 0xFFFF) == 0xA001);
    }

    as.RewindBuffer();

    // Simple branch to self with a conditional branch instruction.
    {
        Label label;
        as.Bind(&label);
        as.BNE(x3, x4, &label);
        REQUIRE(data == 0x00419063);
    }

    as.RewindBuffer();

    // Simple branch to self with a compressed branch instruction.
    {
        Label label;
        as.Bind(&label);
        as.C_BNEZ(x15, &label);
        REQUIRE((data & 0xFFFF) == 0xE381);
    }
}

TEST_CASE("Branch with Instructions Between", "[branch]") {
    std::array<uint32_t, 20> data{};
    auto as = MakeAssembler32(data);

    // Simple branch backward
    {
        Label label;
        as.Bind(&label);
        as.ADD(x1, x2, x3);
        as.SUB(x2, x4, x3);
        as.J(&label);
        REQUIRE(data[2] == 0xFF9FF06F);
    }

    as.RewindBuffer();
    data.fill(0);

    // Simple branch forward
    {
        Label label;
        as.J(&label);
        as.ADD(x1, x2, x3);
        as.SUB(x2, x4, x3);
        as.Bind(&label);
        REQUIRE(data[0] == 0x00C0006F);
    }

    as.RewindBuffer();
    data.fill(0);

    // Simple branch backward (compressed)
    {
        Label label;
        as.Bind(&label);
        as.ADD(x1, x2, x3);
        as.SUB(x2, x4, x3);
        as.C_J(&label);
        REQUIRE((data[2] & 0xFFFF) == 0xBFC5);
    }

    as.RewindBuffer();
    data.fill(0);

    // Simple branch forward (compressed)
    {
        Label label;
        as.C_J(&label);
        as.ADD(x1, x2, x3);
        as.SUB(x2, x4, x3);
        as.Bind(&label);
        REQUIRE((data[0] & 0xFFFF) == 0xA0A1);
    }
}