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 142
|
#include <cassert>
#include <iostream>
#include <ir/utils.h>
using namespace wasm;
#define assertEqual(left, right) \
assert(ExpressionAnalyzer::hash(&left) == ExpressionAnalyzer::hash(&right));
#define assertNotEqual(left, right) \
assert(ExpressionAnalyzer::hash(&left) != ExpressionAnalyzer::hash(&right));
int main() {
{
Const x, y;
x.set(Literal(int32_t(10)));
y.set(Literal(int32_t(10)));
assertEqual(x, y);
}
{
// The value matters (with extremely high probability...)
Const x, y;
x.set(Literal(int32_t(10)));
y.set(Literal(int32_t(11)));
assertNotEqual(x, y);
}
{
// The type matters.
Const x, y;
x.set(Literal(int32_t(10)));
y.set(Literal(int64_t(10)));
assertNotEqual(x, y);
}
{
// Nested child.
Drop dx, dy;
Const x, y;
x.set(Literal(int32_t(10)));
y.set(Literal(int32_t(10)));
dx.value = &x;
dy.value = &y;
assertEqual(dx, dy);
}
{
// Nested child.
Drop dx, dy;
Const x, y;
x.set(Literal(int32_t(10)));
y.set(Literal(int32_t(11)));
dx.value = &x;
dy.value = &y;
assertNotEqual(dx, dy);
}
MixedArena arena;
{
// Blocks
Block x(arena);
Block y(arena);
assertEqual(x, y);
}
{
// Blocks with contents.
Block x(arena);
Block y(arena);
Nop n;
y.list.push_back(&n);
assertNotEqual(x, y);
}
{
// Blocks with names.
Block x(arena);
x.name = "foo";
Block y(arena);
y.name = "foo";
assertEqual(x, y);
}
{
// Different block names hash equally - we ignore internal name differences
// intentionally.
Block x(arena);
x.name = "foo";
Block y(arena);
y.name = "bar";
assertEqual(x, y);
}
{
// Different br names are checked relatively as well.
Break x;
x.name = "foo";
Break y;
y.name = "bar";
Block z(arena);
z.name = "foo";
z.list.push_back(&x);
Block w(arena);
w.name = "bar";
w.list.push_back(&y);
Block outer1(arena);
outer1.name = "outer1";
outer1.list.push_back(&z);
Block outer2(arena);
outer2.name = "outer2";
outer2.list.push_back(&w);
assertEqual(outer1, outer2);
}
{
// But referring to different relative names leads to a difference.
Break x;
x.name = "outer1"; // instead of x, go to the outer label this time
Break y;
y.name = "bar";
Block z(arena);
z.name = "foo";
z.list.push_back(&x);
Block w(arena);
w.name = "bar";
w.list.push_back(&y);
Block outer1(arena);
outer1.name = "outer1";
outer1.list.push_back(&z);
Block outer2(arena);
outer2.name = "outer2";
outer2.list.push_back(&w);
assertNotEqual(outer1, outer2);
}
{
// Indexes.
LocalGet x, y;
x.index = 10;
y.index = 10;
assertEqual(x, y);
}
{
// Indexes.
LocalGet x, y;
x.index = 10;
y.index = 11;
assertNotEqual(x, y);
}
std::cout << "success.\n";
}
|