File size: 5,921 Bytes
1e94262 | 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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 | /*
* IR - Lightweight JIT Compilation Framework
* (String table)
* Copyright (C) 2022 Zend by Perforce.
* Authors: Dmitry Stogov <dmitry@php.net>
*/
#include "ir.h"
#include "ir_private.h"
typedef struct _ir_strtab_bucket {
uint32_t h;
uint32_t len;
const char *str;
uint32_t next;
ir_ref val;
} ir_strtab_bucket;
static uint32_t ir_str_hash(const char *str, size_t len)
{
size_t i;
uint32_t h = 5381;
for (i = 0; i < len; i++) {
h = ((h << 5) + h) + *str;
str++;
}
return h | 0x10000000;
}
static uint32_t ir_strtab_hash_size(uint32_t size)
{
/* Use big enough power of 2 */
size -= 1;
size |= (size >> 1);
size |= (size >> 2);
size |= (size >> 4);
size |= (size >> 8);
size |= (size >> 16);
return size + 1;
}
static void ir_strtab_resize(ir_strtab *strtab)
{
uint32_t old_hash_size = (uint32_t)(-(int32_t)strtab->mask);
char *old_data = strtab->data;
uint32_t size = strtab->size * 2;
uint32_t hash_size = ir_strtab_hash_size(size);
char *data = ir_mem_malloc(hash_size * sizeof(uint32_t) + size * sizeof(ir_strtab_bucket));
ir_strtab_bucket *p;
uint32_t pos, i;
memset(data, IR_INVALID_IDX, hash_size * sizeof(uint32_t));
strtab->data = data + (hash_size * sizeof(uint32_t));
strtab->mask = (uint32_t)(-(int32_t)hash_size);
strtab->size = size;
memcpy(strtab->data, old_data, strtab->count * sizeof(ir_strtab_bucket));
ir_mem_free(old_data - (old_hash_size * sizeof(uint32_t)));
i = strtab->count;
pos = 0;
p = (ir_strtab_bucket*)strtab->data;
do {
uint32_t h = p->h | strtab->mask;
p->next = ((uint32_t*)strtab->data)[(int32_t)h];
((uint32_t*)strtab->data)[(int32_t)h] = pos;
pos += sizeof(ir_strtab_bucket);
p++;
} while (--i);
}
static void ir_strtab_grow_buf(ir_strtab *strtab, uint32_t len)
{
intptr_t old = (intptr_t)strtab->buf;
do {
strtab->buf_size *= 2;
} while (UNEXPECTED(strtab->buf_size - strtab->buf_top < len + 1));
strtab->buf = ir_mem_realloc(strtab->buf, strtab->buf_size);
if ((intptr_t)strtab->buf != old) {
intptr_t offset = (intptr_t)strtab->buf - old;
ir_strtab_bucket *p = (ir_strtab_bucket*)strtab->data;
uint32_t i;
for (i = strtab->count; i > 0; i--) {
p->str += offset;
p++;
}
}
}
void ir_strtab_init(ir_strtab *strtab, uint32_t size, uint32_t buf_size)
{
IR_ASSERT(size > 0);
uint32_t hash_size = ir_strtab_hash_size(size);
char *data = ir_mem_malloc(hash_size * sizeof(uint32_t) + size * sizeof(ir_strtab_bucket));
memset(data, IR_INVALID_IDX, hash_size * sizeof(uint32_t));
strtab->data = (data + (hash_size * sizeof(uint32_t)));
strtab->mask = (uint32_t)(-(int32_t)hash_size);
strtab->size = size;
strtab->count = 0;
strtab->pos = 0;
if (buf_size) {
strtab->buf = ir_mem_malloc(buf_size);
strtab->buf_size = buf_size;
strtab->buf_top = 0;
} else {
strtab->buf = NULL;
strtab->buf_size = 0;
strtab->buf_top = 0;
}
}
ir_ref ir_strtab_find(const ir_strtab *strtab, const char *str, uint32_t len)
{
uint32_t h = ir_str_hash(str, len);
const char *data = (const char*)strtab->data;
uint32_t pos = ((uint32_t*)data)[(int32_t)(h | strtab->mask)];
ir_strtab_bucket *p;
while (pos != IR_INVALID_IDX) {
p = (ir_strtab_bucket*)(data + pos);
if (p->h == h
&& p->len == len
&& memcmp(p->str, str, len) == 0) {
return p->val;
}
pos = p->next;
}
return 0;
}
ir_ref ir_strtab_lookup(ir_strtab *strtab, const char *str, uint32_t len, ir_ref val)
{
uint32_t h = ir_str_hash(str, len);
char *data = (char*)strtab->data;
uint32_t pos = ((uint32_t*)data)[(int32_t)(h | strtab->mask)];
ir_strtab_bucket *p;
while (pos != IR_INVALID_IDX) {
p = (ir_strtab_bucket*)(data + pos);
if (p->h == h
&& p->len == len
&& memcmp(p->str, str, len) == 0) {
return p->val;
}
pos = p->next;
}
IR_ASSERT(val != 0);
if (UNEXPECTED(strtab->count >= strtab->size)) {
ir_strtab_resize(strtab);
data = strtab->data;
}
if (strtab->buf) {
if (UNEXPECTED(strtab->buf_size - strtab->buf_top < len + 1)) {
ir_strtab_grow_buf(strtab, len + 1);
}
memcpy(strtab->buf + strtab->buf_top, str, len);
strtab->buf[strtab->buf_top + len] = 0;
str = (const char*)strtab->buf + strtab->buf_top;
strtab->buf_top += len + 1;
}
pos = strtab->pos;
strtab->pos += sizeof(ir_strtab_bucket);
strtab->count++;
p = (ir_strtab_bucket*)(data + pos);
p->h = h;
p->len = len;
p->str = str;
h |= strtab->mask;
p->next = ((uint32_t*)data)[(int32_t)h];
((uint32_t*)data)[(int32_t)h] = pos;
p->val = val;
return val;
}
ir_ref ir_strtab_update(ir_strtab *strtab, const char *str, uint32_t len, ir_ref val)
{
uint32_t h = ir_str_hash(str, len);
char *data = (char*)strtab->data;
uint32_t pos = ((uint32_t*)data)[(int32_t)(h | strtab->mask)];
ir_strtab_bucket *p;
while (pos != IR_INVALID_IDX) {
p = (ir_strtab_bucket*)(data + pos);
if (p->h == h
&& p->len == len
&& memcmp(p->str, str, len) == 0) {
return p->val = val;
}
pos = p->next;
}
return 0;
}
const char *ir_strtab_str(const ir_strtab *strtab, ir_ref idx)
{
IR_ASSERT(idx >= 0 && (uint32_t)idx < strtab->count);
return ((const ir_strtab_bucket*)strtab->data)[idx].str;
}
const char *ir_strtab_strl(const ir_strtab *strtab, ir_ref idx, size_t *len)
{
const ir_strtab_bucket *b = ((const ir_strtab_bucket*)strtab->data) + idx;
IR_ASSERT(idx >= 0 && (uint32_t)idx < strtab->count);
*len = b->len;
return b->str;
}
void ir_strtab_free(ir_strtab *strtab)
{
uint32_t hash_size = (uint32_t)(-(int32_t)strtab->mask);
char *data = (char*)strtab->data - (hash_size * sizeof(uint32_t));
ir_mem_free(data);
strtab->data = NULL;
if (strtab->buf) {
ir_mem_free(strtab->buf);
strtab->buf = NULL;
}
}
void ir_strtab_apply(const ir_strtab *strtab, ir_strtab_apply_t func)
{
uint32_t i;
for (i = 0; i < strtab->count; i++) {
const ir_strtab_bucket *b = &((ir_strtab_bucket*)strtab->data)[i];
func(b->str, b->len, b->val);
}
}
|