Hash :
1165bf0f
Author :
Thomas de Grivel
Date :
2024-12-03T11:06:11
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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
/* kc3
* Copyright 2022,2023,2024 kmx.io <contact@kmx.io>
*
* Permission is hereby granted to use this software granted the above
* copyright notice and this permission paragraph are included in all
* copies and substantial portions of this software.
*
* THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
* PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
#include <string.h>
#include "alloc.h"
#include "assert.h"
#include "data.h"
#include "env.h"
#include "list.h"
#include "map.h"
#include "struct.h"
#include "struct_type.h"
#include "sym.h"
#include "tag.h"
#include "tag_init.h"
#include "tag_type.h"
void struct_type_clean (s_struct_type *st)
{
assert(st);
map_clean(&st->map);
free(st->offset);
}
void * struct_type_copy_data (const s_struct_type *st, void *dest,
const void *src)
{
uw count;
uw i;
uw offset;
const s_sym *sym;
assert(st);
assert(dest);
assert(src);
i = 0;
count = st->map.count;
while (i < count) {
if (! tag_type(st->map.value + i, &sym))
return NULL;
offset = st->offset[i];
if (! data_init_copy(sym, (s8 *) dest + offset,
(s8 *) src + offset))
return NULL;
i++;
}
return dest;
}
void struct_type_delete (s_struct_type *st)
{
assert(st);
struct_type_clean(st);
free(st);
}
bool * struct_type_exists (const s_sym *module, bool *dest)
{
return env_struct_type_exists(g_kc3_env, module, dest);
}
s_struct_type ** struct_type_find (const s_sym *module,
s_struct_type **dest)
{
return env_struct_type_find(g_kc3_env, module, dest);
}
uw * struct_type_find_key_index (const s_struct_type *st,
const s_sym *key, uw *dest)
{
uw i = 0;
assert(st);
assert(key);
while (i < st->map.count) {
assert(st->map.key[i].type == TAG_SYM);
if (st->map.key[i].data.sym == key) {
*dest = i;
return dest;
}
i++;
}
return NULL;
}
s_struct_type * struct_type_init (s_struct_type *st,
const s_sym *module,
const s_list *spec)
{
uw count;
uw i;
bool must_clean = false;
uw offset;
const s_list *s;
uw size;
s_struct_type tmp = {0};
const s_tuple *tuple;
const s_sym *type;
assert(st);
assert(module);
assert(spec);
count = list_length(spec);
tmp.module = module;
if (! map_init(&tmp.map, count))
return NULL;
tmp.offset = alloc(count * sizeof(uw));
if (! tmp.offset) {
map_clean(&tmp.map);
return NULL;
}
tmp.must_clean = false;
offset = 0;
i = 0;
s = spec;
while (s) {
if (s->tag.type != TAG_TUPLE || s->tag.data.tuple.count != 2) {
err_puts("struct_type_init: invalid spec");
assert(! "struct_type_init: invalid spec");
map_clean(&tmp.map);
free(tmp.offset);
return NULL;
}
tuple = &s->tag.data.tuple;
if (tuple->tag[1].type == TAG_VAR) {
type = tuple->tag[1].data.var.type;
if (! sym_type_size(&type, &size)) {
map_clean(&tmp.map);
free(tmp.offset);
return NULL;
}
}
else if (! tag_size(tuple->tag + 1, &size)) {
map_clean(&tmp.map);
free(tmp.offset);
return NULL;
}
tag_init_copy(tmp.map.key + i, tuple->tag + 0);
tag_init_copy(tmp.map.value + i, tuple->tag + 1);
tag_type(tmp.map.value + i, &type);
if (! sym_must_clean(type, &must_clean)) {
map_clean(&tmp.map);
free(tmp.offset);
return NULL;
}
if (must_clean)
tmp.must_clean = true;
offset = struct_type_padding(offset, size);
tmp.offset[i] = offset;
offset += size;
i++;
s = list_next(s);
}
if (sizeof(long) == 4)
tmp.size = (offset + 3) / 4 * 4;
else
tmp.size = (offset + 15) / 16 * 16;
*st = tmp;
return st;
}
s_struct_type * struct_type_init_cast (s_struct_type *st,
const s_sym * const *type,
const s_tag *tag)
{
assert(st);
assert(tag);
switch (tag->type) {
case TAG_STRUCT_TYPE:
if (*type == &g_sym_StructType)
return struct_type_init_copy(st, &tag->data.struct_type);
default:
break;
}
err_write_1("struct_type_init_cast: cannot cast ");
err_write_1(tag_type_to_string(tag->type));
err_puts(" to StructType");
assert(! "struct_type_init_cast: cannot cast to StructType");
return NULL;
}
s_struct_type * struct_type_init_update_clean (s_struct_type *st,
const s_struct_type *src,
const s_cfn *clean)
{
s_struct_type tmp = {0};
assert(st);
assert(src);
assert(clean);
if (! struct_type_init_copy(&tmp, src))
return NULL;
tmp.clean = (f_clean) clean->ptr.f;
*st = tmp;
return st;
}
s_struct_type * struct_type_init_copy (s_struct_type *st,
const s_struct_type *src)
{
s_struct_type tmp = {0};
assert(st);
assert(src);
assert(src->module);
assert(src->map.count);
tmp.clean = src->clean;
if (! map_init_copy(&tmp.map, &src->map))
return NULL;
tmp.module = src->module;
tmp.must_clean = src->must_clean;
tmp.offset = alloc(tmp.map.count * sizeof(uw));
if (! tmp.offset) {
map_clean(&tmp.map);
return NULL;
}
memcpy(tmp.offset, src->offset, tmp.map.count * sizeof(uw));
tmp.size = src->size;
*st = tmp;
return st;
}
s_struct_type * struct_type_init_from_env (s_struct_type *st,
const s_sym *module,
s_env *env)
{
s_list *spec;
assert(st);
assert(module);
assert(env);
if (! env_struct_type_get_spec(env, module, &spec) ||
! spec)
return NULL;
if (! struct_type_init(st, module, spec))
return NULL;
list_delete_all(spec);
return st;
}
s_struct_type * struct_type_new (const s_sym *module,
const s_list *spec)
{
s_struct_type *st;
assert(module);
st = alloc(sizeof(s_struct_type));
if (! st)
return NULL;
if (! struct_type_init(st, module, spec)) {
free(st);
return NULL;
}
return st;
}
uw struct_type_padding (uw offset, uw size)
{
unsigned int align = 1;
if (size >= 8)
align = 8;
return (offset + (align - 1)) / align * align;
}