core changes

This commit is contained in:
Krzosa Karol
2025-01-06 21:33:00 +01:00
parent 2e2b3ceafb
commit 1710bc232d
11 changed files with 40 additions and 26 deletions

View File

@@ -192,6 +192,6 @@ void meta_app(ma_arena_t *arena) {
sb8_serial_ast_to_type_info(c, decls); sb8_serial_ast_to_type_info(c, decls);
} }
os_write_file(gen_c(arena), sb8_merge(c)); os_write_file(gen_c(arena), sb8_merge(arena, c));
os_write_file(gen_h(arena), sb8_merge(h)); os_write_file(gen_h(arena), sb8_merge(arena, h));
} }

View File

@@ -64,7 +64,7 @@ typedef double f64;
#define zero_struct(x) memory_zero((x), sizeof(*(x))) #define zero_struct(x) memory_zero((x), sizeof(*(x)))
#define DEFER_LOOP(begin, end) for (i32 PASTE(_i_, __LINE__) = (begin, 0); !PASTE(_i_, __LINE__); PASTE(_i_, __LINE__) += (end, 1)) #define defer_block(begin, end) for (i32 PASTE(_i_, __LINE__) = (begin, 0); !PASTE(_i_, __LINE__); PASTE(_i_, __LINE__) += (end, 1))
#define STACK(type, size) struct { type data[size]; i32 len; } #define STACK(type, size) struct { type data[size]; i32 len; }
#define STACK_PUSH(stack, ...) (assert((stack).len < lengthof((stack).data)), (stack).data[(stack).len++] = __VA_ARGS__) #define STACK_PUSH(stack, ...) (assert((stack).len < lengthof((stack).data)), (stack).data[(stack).len++] = __VA_ARGS__)
#define STACK_POP(stack) (assert((stack).len > 0), (stack).data[--(stack).len]) #define STACK_POP(stack) (assert((stack).len > 0), (stack).data[--(stack).len])

View File

@@ -33,8 +33,8 @@ struct ht_dict_t {
ht_bucket_t *buckets; ht_bucket_t *buckets;
}; };
u64 ht_hash(s8_t string) { u64 ht_hash_data(s8_t string) {
u8 *data = string.str; u8 *data = (u8 *)string.str;
uint64_t hash = (u64)14695981039346656037ULL; uint64_t hash = (u64)14695981039346656037ULL;
for (i64 i = 0; i < string.len; i++) { for (i64 i = 0; i < string.len; i++) {
hash = hash ^ (u64)(data[i]); hash = hash ^ (u64)(data[i]);
@@ -61,7 +61,7 @@ ht_node_t *ht_insert_kv(ht_dict_t *ht, u64 hash, ht_key_value_t kv) {
} }
ht_node_t *ht_insert_u64_u64(ht_dict_t *ht, u64 key, u64 value) { ht_node_t *ht_insert_u64_u64(ht_dict_t *ht, u64 key, u64 value) {
u64 hash = ht_hash(s8((char *)&key, sizeof(key))); u64 hash = ht_hash_data(s8((char *)&key, sizeof(key)));
return ht_insert_kv(ht, hash, (ht_key_value_t){.key_u64 = key, .value_u64 = value}); return ht_insert_kv(ht, hash, (ht_key_value_t){.key_u64 = key, .value_u64 = value});
} }
@@ -70,12 +70,12 @@ ht_node_t *ht_insert_ptr(ht_dict_t *ht, void *key, void *value) {
} }
ht_node_t *ht_insert_string(ht_dict_t *ht, s8_t key, s8_t value) { ht_node_t *ht_insert_string(ht_dict_t *ht, s8_t key, s8_t value) {
u64 hash = ht_hash(key); u64 hash = ht_hash_data(key);
return ht_insert_kv(ht, hash, (ht_key_value_t){.key_string = key, .value_string = value}); return ht_insert_kv(ht, hash, (ht_key_value_t){.key_string = key, .value_string = value});
} }
ht_node_t *ht_search_u64(ht_dict_t *ht, u64 key) { ht_node_t *ht_search_u64(ht_dict_t *ht, u64 key) {
u64 hash = ht_hash(s8((char *)&key, sizeof(key))); u64 hash = ht_hash_data(s8((char *)&key, sizeof(key)));
i64 idx = hash % ht->bucket_count; i64 idx = hash % ht->bucket_count;
ht_bucket_t *bucket = ht->buckets + idx; ht_bucket_t *bucket = ht->buckets + idx;
for (ht_node_t *it = bucket->first; it; it = it->next) { for (ht_node_t *it = bucket->first; it; it = it->next) {
@@ -91,7 +91,7 @@ ht_node_t *ht_search_ptr(ht_dict_t *ht, void *key_ptr) {
} }
ht_node_t *ht_search_string(ht_dict_t *ht, s8_t key) { ht_node_t *ht_search_string(ht_dict_t *ht, s8_t key) {
u64 hash = ht_hash(key); u64 hash = ht_hash_data(key);
i64 idx = hash % ht->bucket_count; i64 idx = hash % ht->bucket_count;
ht_bucket_t *bucket = ht->buckets + idx; ht_bucket_t *bucket = ht->buckets + idx;
for (ht_node_t *it = bucket->first; it; it = it->next) { for (ht_node_t *it = bucket->first; it; it = it->next) {

View File

@@ -31,7 +31,7 @@ fn void default_log_proc(log_event_t ev) {
} }
sb8_printf(sb, "%S\n", ev.string); sb8_printf(sb, "%S\n", ev.string);
s8_t result = sb8_serial_end(sb); s8_t result = sb8_serial_end(scratch.arena, sb);
if (ev.level != log_level_fatal) { if (ev.level != log_level_fatal) {
os_console_log(result.str); os_console_log(result.str);

View File

@@ -410,9 +410,9 @@ fn int64_t sb8_char_size(sb8_t *sb) {
return result; return result;
} }
fn s8_t sb8_merge(sb8_t *sb) { fn s8_t sb8_merge(ma_arena_t *arena, sb8_t *sb) {
int64_t size = sb8_char_size(sb) + 1; int64_t size = sb8_char_size(sb) + 1;
char *str = ma_push_size(sb->arena, size); char *str = ma_push_size(arena, size);
s8_t result = {str, 0}; s8_t result = {str, 0};
for (sb8_node_t *it = sb->first; it; it = it->next) { for (sb8_node_t *it = sb->first; it; it = it->next) {
memory_copy(result.str + result.len, it->str, it->len); memory_copy(result.str + result.len, it->str, it->len);

View File

@@ -123,13 +123,13 @@ fn s8_t s8_printf(ma_arena_t *ma, const char *str, ...);
// //
// string builder // string builder
#define sb8_serial_begin(ARENA) &(sb8_t){.arena = ARENA} #define sb8_serial_begin(ARENA) &(sb8_t){.arena = ARENA}
#define sb8_serial_end(sb) sb8_merge(sb) #define sb8_serial_end(ARENA, SB) sb8_merge(ARENA, SB)
fn s8_t sb8_printf(sb8_t *sb, const char *str, ...); fn s8_t sb8_printf(sb8_t *sb, const char *str, ...);
fn sb8_node_t *sb8_append(sb8_t *list, s8_t string); fn sb8_node_t *sb8_append(sb8_t *list, s8_t string);
fn sb8_node_t *sb8_create_node(ma_arena_t *ma, s8_t str); fn sb8_node_t *sb8_create_node(ma_arena_t *ma, s8_t str);
fn s8_t sb8_merge(sb8_t *sb); fn s8_t sb8_merge(ma_arena_t *arena, sb8_t *sb);
fn void sb8_indent(sb8_t *sb); fn void sb8_indent(sb8_t *sb);
fn s8_t sb8_stmtf(sb8_t *sb, const char *str, ...); fn s8_t sb8_stmtf(sb8_t *sb, const char *str, ...);
fn int64_t sb8_char_size(sb8_t *sb); fn int64_t sb8_char_size(sb8_t *sb);

View File

@@ -225,9 +225,11 @@ fn void ti__serial_data_ex(sb8_t *sb, void *p, type_t *type) {
} }
fn s8_t ti__serial_data(ma_arena_t *arena, void *p, type_t *type) { fn s8_t ti__serial_data(ma_arena_t *arena, void *p, type_t *type) {
sb8_t *sb = sb8_serial_begin(arena); ma_temp_t scratch = ma_begin_scratch1(arena);
sb8_t *sb = sb8_serial_begin(scratch.arena);
ti__serial_data_ex(sb, p, type); ti__serial_data_ex(sb, p, type);
s8_t string = sb8_serial_end(sb); s8_t string = sb8_serial_end(arena, sb);
ma_end_scratch(scratch);
return string; return string;
} }

View File

@@ -21,13 +21,14 @@ enum {
type_kind_int, type_kind_int,
type_kind_char, type_kind_char,
type_kind_count,
type_kind_pointer, type_kind_pointer,
type_kind_array, type_kind_array,
type_kind_struct, type_kind_struct,
type_kind_union, type_kind_union,
type_kind_enum, type_kind_enum,
type_kind_first_basic = type_kind_i8,
type_kind_last_basic = type_kind_char,
}; };
typedef struct type_member_t type_member_t; typedef struct type_member_t type_member_t;

View File

@@ -37,11 +37,13 @@ struct ast_t {
}; };
s8_t s8_serial_ast_flag_t(ma_arena_t *arena, ast_flag_t flag) { s8_t s8_serial_ast_flag_t(ma_arena_t *arena, ast_flag_t flag) {
sb8_t *sb = sb8_serial_begin(arena); ma_temp_t scratch = ma_begin_scratch1(arena);
sb8_t *sb = sb8_serial_begin(scratch.arena);
#define X(NAME) if (flag & set_bit(ast_flag_##NAME)) sb8_printf(sb, #NAME); #define X(NAME) if (flag & set_bit(ast_flag_##NAME)) sb8_printf(sb, #NAME);
AST_FLAG_XLIST AST_FLAG_XLIST
#undef X #undef X
s8_t result = sb8_serial_end(sb); s8_t result = sb8_serial_end(arena, sb);
ma_end_scratch(scratch);
return result; return result;
} }

View File

@@ -36,9 +36,12 @@ void sb8_serial_ast(sb8_t *sb, ast_t *n) {
} }
s8_t s8_serial_ast(ma_arena_t *arena, ast_t *n) { s8_t s8_serial_ast(ma_arena_t *arena, ast_t *n) {
sb8_t *sb = sb8_serial_begin(arena); ma_temp_t scratch = ma_begin_scratch1(arena);
sb8_t *sb = sb8_serial_begin(scratch.arena);
sb8_serial_ast(sb, n); sb8_serial_ast(sb, n);
return sb8_serial_end(sb); s8_t result = sb8_serial_end(arena, sb);
ma_end_scratch(scratch);
return result;
} }
s8_t s8_serial_ast_to_code(ma_arena_t *arena, ast_t *n); s8_t s8_serial_ast_to_code(ma_arena_t *arena, ast_t *n);
@@ -78,9 +81,11 @@ void sb8_serial_ast_to_code(sb8_t *sb, ast_t *n) {
} }
s8_t s8_serial_ast_to_code(ma_arena_t *arena, ast_t *n) { s8_t s8_serial_ast_to_code(ma_arena_t *arena, ast_t *n) {
sb8_t *sb = sb8_serial_begin(arena); ma_temp_t scratch = ma_begin_scratch1(arena);
sb8_t *sb = sb8_serial_begin(scratch.arena);
sb8_serial_ast_to_code(sb, n); sb8_serial_ast_to_code(sb, n);
s8_t result = sb8_serial_end(sb); s8_t result = sb8_serial_end(arena, sb);
ma_end_scratch(scratch);
return result; return result;
} }
@@ -148,9 +153,11 @@ void sb8_serial_ast_to_type_info(sb8_t *sb, ast_t *n) {
} }
s8_t s8_serial_ast_to_type_info(ma_arena_t *arena, ast_t *n) { s8_t s8_serial_ast_to_type_info(ma_arena_t *arena, ast_t *n) {
sb8_t *sb = sb8_serial_begin(arena); ma_temp_t scratch = ma_begin_scratch1(arena);
sb8_t *sb = sb8_serial_begin(scratch.arena);
sb8_serial_ast_to_type_info(sb, n); sb8_serial_ast_to_type_info(sb, n);
s8_t result = sb8_serial_end(sb); s8_t result = sb8_serial_end(arena, sb);
ma_end_scratch(scratch);
return result; return result;
} }

View File

@@ -2,6 +2,7 @@
[ ] sleep [ ] sleep
[ ] ui [ ] ui
[ ] event playback [ ] event playback
[ ] maybe different events structure: { frame1: { event1, event2 }, frame2: {event1} }
[ ] how to fix variable scroll? or do we not care? (probably need delta time) [ ] how to fix variable scroll? or do we not care? (probably need delta time)
[ ] win32 [ ] win32
[ ] hot reload / plugins [ ] hot reload / plugins
@@ -26,6 +27,7 @@
[ ] somehow index properly the meta files and ignore generated files [ ] somehow index properly the meta files and ignore generated files
[ ] extract declarations from c files meta(introspect) [ ] extract declarations from c files meta(introspect)
[ ] extract all globals from c files [ ] extract all globals from c files
[ ] tweak variables and embeds declared in c code
[ ] new simple format with tags [ ] new simple format with tags
[x] revisit api [x] revisit api
[ ] s8_bin [ ] s8_bin