forked from jckarter/clay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
objects.cpp
158 lines (125 loc) · 3.42 KB
/
objects.cpp
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
#include "clay.hpp"
#include "objects.hpp"
#include "types.hpp"
namespace clay {
//
// ValueHolder constructor and destructor
//
ValueHolder::ValueHolder(TypePtr type)
: Object(VALUE_HOLDER), type(type)
{
this->buf = (char *)calloc(1, typeSize(type));
}
ValueHolder::~ValueHolder()
{
// FIXME: call clay 'destroy'
free(this->buf);
}
//
// objectEquals
//
// FIXME: this doesn't handle arbitrary values (need to call clay)
bool _objectValueEquals(ObjectPtr a, ObjectPtr b)
{
// at this point pointer identity should already have been checked by objectEquals
int aKind = a->objKind, bKind = b->objKind;
if (aKind != bKind)
return false;
switch (aKind) {
case IDENTIFIER : {
Identifier *a1 = (Identifier *)a.ptr();
Identifier *b1 = (Identifier *)b.ptr();
return a1->str == b1->str;
}
case VALUE_HOLDER : {
ValueHolder *a1 = (ValueHolder *)a.ptr();
ValueHolder *b1 = (ValueHolder *)b.ptr();
if (a1->type != b1->type)
return false;
size_t n = typeSize(a1->type);
return memcmp(a1->buf, b1->buf, n) == 0;
}
case PVALUE : {
PValue *a1 = (PValue *)a.ptr();
PValue *b1 = (PValue *)b.ptr();
return a1->data == b1->data;
}
default :
return false;
}
}
//
// objectHash
//
static unsigned identityHash(Object *a)
{
size_t v = (size_t)a;
return unsigned(int(v));
}
// FIXME: this doesn't handle arbitrary values (need to call clay)
unsigned objectHash(ObjectPtr a)
{
switch (a->objKind) {
case IDENTIFIER : {
Identifier *b = (Identifier *)a.ptr();
unsigned h = 0;
for (unsigned i = 0; i < b->str.size(); ++i)
h += unsigned(b->str[i]);
return h;
}
case VALUE_HOLDER : {
ValueHolder *b = (ValueHolder *)a.ptr();
// TODO: call clay 'hash'
unsigned h = 0;
size_t n = typeSize(b->type);
for (unsigned i = 0; i < n; ++i)
h += unsigned(b->buf[i]);
h = h*11 + identityHash(b->type.ptr());
return h;
}
case PVALUE : {
PValue *b = (PValue *)a.ptr();
unsigned h = identityHash(b->data.type.ptr());
h *= b->data.isTemp ? 11 : 23;
return h;
}
default : {
return identityHash(a.ptr());
}
}
}
//
// ObjectTable
//
Pointer<RefCounted> &ObjectTable::lookup(llvm::ArrayRef<ObjectPtr> key)
{
if (this->buckets.empty())
this->buckets.resize(16);
if (this->buckets.size() < 2*this->size)
rehash();
unsigned h = objectVectorHash(key);
h &= unsigned(buckets.size() - 1);
vector<ObjectTableNode> &bucket = buckets[h];
for (unsigned i = 0; i < bucket.size(); ++i) {
if (objectVectorEquals(key, bucket[i].key))
return bucket[i].value;
}
bucket.push_back(ObjectTableNode(key, NULL));
++this->size;
return bucket.back().value;
}
void ObjectTable::rehash()
{
vector< vector<ObjectTableNode> > newBuckets;
newBuckets.resize(4 * this->buckets.size());
for (unsigned i = 0; i < this->buckets.size(); ++i) {
vector<ObjectTableNode> &bucket = this->buckets[i];
for (unsigned j = 0; j < bucket.size(); ++j) {
unsigned h = objectVectorHash(bucket[j].key);
h &= unsigned(newBuckets.size() - 1);
newBuckets[h].push_back(bucket[j]);
}
}
this->buckets = newBuckets;
}
}