forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEquivalentTypeSet.cpp
More file actions
165 lines (147 loc) · 4.01 KB
/
EquivalentTypeSet.cpp
File metadata and controls
165 lines (147 loc) · 4.01 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#include "Backend.h"
#if ENABLE_NATIVE_CODEGEN
namespace Js
{
EquivalentTypeSet::EquivalentTypeSet(RecyclerJITTypeHolder * types, uint16 count)
: types(types), count(count), sortedAndDuplicatesRemoved(false)
{
}
JITTypeHolder EquivalentTypeSet::GetType(uint16 index) const
{
Assert(this->types != nullptr && this->count > 0 && index < this->count);
return this->types[index];
}
void EquivalentTypeSet::SetType(uint16 index, JITTypeHolder type)
{
Assert(index < this->count);
this->types[index] = type;
}
JITTypeHolder EquivalentTypeSet::GetFirstType() const
{
return GetType(0);
}
bool EquivalentTypeSet::Contains(const JITTypeHolder type, uint16* pIndex)
{
if (!this->GetSortedAndDuplicatesRemoved())
{
this->SortAndRemoveDuplicates();
}
for (uint16 ti = 0; ti < this->count; ti++)
{
if (this->GetType(ti) == type)
{
if (pIndex)
{
*pIndex = ti;
}
return true;
}
}
return false;
}
bool EquivalentTypeSet::AreIdentical(EquivalentTypeSet * left, EquivalentTypeSet * right)
{
if (!left->GetSortedAndDuplicatesRemoved())
{
left->SortAndRemoveDuplicates();
}
if (!right->GetSortedAndDuplicatesRemoved())
{
right->SortAndRemoveDuplicates();
}
Assert(left->GetSortedAndDuplicatesRemoved() && right->GetSortedAndDuplicatesRemoved());
if (left->count != right->count)
{
return false;
}
// TODO: OOP JIT, optimize this (previously we had memcmp)
for (uint i = 0; i < left->count; ++i)
{
if (left->types[i] != right->types[i])
{
return false;
}
}
return true;
}
bool EquivalentTypeSet::IsSubsetOf(EquivalentTypeSet * left, EquivalentTypeSet * right)
{
if (!left->GetSortedAndDuplicatesRemoved())
{
left->SortAndRemoveDuplicates();
}
if (!right->GetSortedAndDuplicatesRemoved())
{
right->SortAndRemoveDuplicates();
}
if (left->count > right->count)
{
return false;
}
// Try to find each left type in the right set.
int j = 0;
for (int i = 0; i < left->count; i++)
{
bool found = false;
for (; j < right->count; j++)
{
if (left->types[i] < right->types[j])
{
// Didn't find the left type. Fail.
return false;
}
if (left->types[i] == right->types[j])
{
// Found the left type. Continue to the next left/right pair.
found = true;
j++;
break;
}
}
Assert(j <= right->count);
if (j == right->count && !found)
{
// Exhausted the right set without finding the current left type.
return false;
}
}
return true;
}
void EquivalentTypeSet::SortAndRemoveDuplicates()
{
uint16 oldCount = this->count;
uint16 i;
// sorting
for (i = 1; i < oldCount; i++)
{
uint16 j = i;
while (j > 0 && (this->types[j - 1] > this->types[j]))
{
JITTypeHolder tmp = this->types[j];
this->types[j] = this->types[j - 1];
this->types[j - 1] = tmp;
j--;
}
}
// removing duplicate types from the sorted set
i = 0;
for (uint16 j = 1; j < oldCount; j++)
{
if (this->types[i] != this->types[j])
{
this->types[++i] = this->types[j];
}
}
this->count = ++i;
for (i; i < oldCount; i++)
{
this->types[i] = JITTypeHolder(nullptr);
}
this->sortedAndDuplicatesRemoved = true;
}
}
#endif