From 06f384e089fa6840cd1fa3019dfe8ca4d842c749 Mon Sep 17 00:00:00 2001 From: Max Wash Date: Thu, 6 Nov 2025 10:38:40 +0000 Subject: [PATCH] asm: update bluelib api usage --- asm/CMakeLists.txt | 2 +- asm/assembler/assembler.c | 7 +++-- asm/assembler/block.c | 13 ++++---- asm/assembler/constpool.c | 24 ++++++++------- asm/lex.c | 62 +++++++++++++++++++++------------------ asm/lex.h | 4 +-- asm/mie/select.c | 16 +++++----- asm/parse/block.c | 47 ++++++++++++++++------------- asm/parse/class.c | 2 +- asm/parse/ident.c | 12 ++++---- asm/parse/unit.c | 2 +- 11 files changed, 105 insertions(+), 86 deletions(-) diff --git a/asm/CMakeLists.txt b/asm/CMakeLists.txt index 4d216b8..c0f32b9 100644 --- a/asm/CMakeLists.txt +++ b/asm/CMakeLists.txt @@ -11,5 +11,5 @@ else () endif () target_include_directories(ivy-asm PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include/) -target_link_libraries(ivy-asm ivy-common mie Bluelib::Core Bluelib::Object) +target_link_libraries(ivy-asm ivy-common mie Bluelib::Core Bluelib::Ds) target_compile_definitions(ivy-asm PRIVATE IVY_EXPORT=1 IVY_STATIC=${IVY_STATIC}) diff --git a/asm/assembler/assembler.c b/asm/assembler/assembler.c index 11c1b8f..02b0a61 100644 --- a/asm/assembler/assembler.c +++ b/asm/assembler/assembler.c @@ -118,10 +118,10 @@ enum ivy_status ivy_assembler_finish(struct ivy_assembler *as) header.h_table_len = b_i16_htob(0); header.h_magic = b_i32_htob(IVY_BIN_MAGIC); - b_queue_iterator it = {0}; - b_queue_foreach (&it, &as->as_table) { + b_queue_entry *entry = b_queue_first(&as->as_table); + while (entry) { struct asm_table_entry *e - = b_unbox(struct asm_table_entry, it.entry, e_entry); + = b_unbox(struct asm_table_entry, entry, e_entry); size_t w = fwrite(&e->e_data, 1, sizeof e->e_data, as->as_data); if (w < sizeof e->e_data) { @@ -129,6 +129,7 @@ enum ivy_status ivy_assembler_finish(struct ivy_assembler *as) } nr_table_entries++; + entry = b_queue_next(entry); } fwrite(&xdat, 1, sizeof xdat, as->as_data); diff --git a/asm/assembler/block.c b/asm/assembler/block.c index a1237e2..8210d20 100644 --- a/asm/assembler/block.c +++ b/asm/assembler/block.c @@ -1,9 +1,9 @@ #include "assembler.h" #include -#include -#include -#include +#include +#include +#include #include #include #include @@ -88,9 +88,9 @@ static enum ivy_status resolve_label_refs( enum ivy_status status = IVY_OK; size_t nr_read = 0; - b_queue_iterator it; - b_queue_foreach (&it, &c->s_label_refs) { - struct label *label_ref = b_unbox(struct label, it.entry, l_entry); + b_queue_entry *entry = b_queue_first(&c->s_label_refs); + while (entry) { + struct label *label_ref = b_unbox(struct label, entry, l_entry); struct label *label_dest = get_label(c, label_ref->l_name->t_str); if (!label_dest) { @@ -114,6 +114,7 @@ static enum ivy_status resolve_label_refs( x = b_i32_htob(instr); assembler_write_data_at(as, &x, label_ref->l_offset, sizeof x); + entry = b_queue_next(entry); } return status; diff --git a/asm/assembler/constpool.c b/asm/assembler/constpool.c index 11e56a1..a5d41b3 100644 --- a/asm/assembler/constpool.c +++ b/asm/assembler/constpool.c @@ -1,9 +1,9 @@ #include "assembler.h" #include -#include -#include -#include +#include +#include +#include #include #include #include @@ -35,7 +35,7 @@ static enum ivy_status init_scope( static ivy_extended_data_key get_cached_string( struct constpool_assembler_scope *scope, const char *s) { - b_number *key = B_NUMBER(b_dict_at(scope->s_strings, s)); + b_number *key = b_dict_at(scope->s_strings, s); if (!key) { return IVY_EX_DATA_KEY_NULL; } @@ -63,7 +63,7 @@ static ivy_extended_data_key write_string(struct ivy_assembler *as, const char * size_t len = strlen(s); struct ivy_bin_string str = {0}; - str.s_hash = b_i32_htob((uint32_t)b_hash_string(s)); + str.s_hash = b_i32_htob((uint32_t)b_hash_cstr(s)); str.s_len = b_i32_htob((uint32_t)len); key = assembler_write_extended_data(as, &str, sizeof str); @@ -101,11 +101,12 @@ static ivy_extended_data_key write_selector( ivy_extended_data_key *arg_handles = calloc(nr_args, sizeof(ivy_extended_data_key)); - b_queue_iterator it = {0}; - b_queue_foreach (&it, &sel->sel_args) { + b_queue_entry *entry = b_queue_first(&sel->sel_args); + while (entry) { struct ivy_selector_arg *arg - = b_unbox(struct ivy_selector_arg, it.entry, arg_entry); + = b_unbox(struct ivy_selector_arg, entry, arg_entry); arg_handles[i++] = write_string(as, arg->arg_label); + entry = b_queue_next(entry); } ivy_extended_data_key selector_handle @@ -130,11 +131,12 @@ static ivy_extended_data_key write_ident( ivy_extended_data_key *part_handles = calloc(nr_parts, sizeof(ivy_extended_data_key)); - b_queue_iterator it = {0}; - b_queue_foreach (&it, &id->id_parts) { + b_queue_entry *entry = b_queue_first(&id->id_parts); + while (entry) { struct ivy_ident_part *arg - = b_unbox(struct ivy_ident_part, it.entry, p_entry); + = b_unbox(struct ivy_ident_part, entry, p_entry); part_handles[i++] = write_string(as, arg->p_str); + entry = b_queue_next(entry); } dat.id_nr_parts = (uint8_t)nr_parts; diff --git a/asm/lex.c b/asm/lex.c index 0fcdc76..01288fb 100644 --- a/asm/lex.c +++ b/asm/lex.c @@ -2,9 +2,9 @@ #include #include -#include -#include -#include +#include +#include +#include #include #include #include @@ -15,10 +15,7 @@ #define LINEBUF_DEFAULT_CAPACITY 1024 -#define LEX_TOKEN_DEF(i, n) \ - { \ - .id = (i), .name = (n) \ - } +#define LEX_TOKEN_DEF(i, n) {.id = (i), .name = (n)} static struct lex_token_def keywords[] = { LEX_TOKEN_DEF(IVY_ASM_KW_IMPORT, "@import"), @@ -94,27 +91,31 @@ static struct lexer_state *get_lexer_state(struct ivy_asm_lexer *lex) static void destroy_state_stack(b_queue *state) { - b_queue_iterator it; - b_queue_iterator_begin(state, &it); - while (b_queue_iterator_is_valid(&it)) { + b_queue_entry *entry = b_queue_first(state); + while (entry) { struct lexer_state *node - = b_unbox(struct lexer_state, it.entry, s_entry); - b_queue_iterator_erase(&it); + = b_unbox(struct lexer_state, entry, s_entry); + b_queue_entry *next = b_queue_next(entry); + b_queue_delete(state, entry); free(node); + + entry = next; } } static struct ivy_asm_lexer_symbol_node *get_symbol_node( struct ivy_asm_lexer_symbol_node *node, char c) { - b_queue_iterator it; - b_queue_foreach (&it, &node->s_children) { + b_queue_entry *entry = b_queue_first(&node->s_children); + while (entry) { struct ivy_asm_lexer_symbol_node *child = b_unbox( - struct ivy_asm_lexer_symbol_node, it.entry, s_entry); + struct ivy_asm_lexer_symbol_node, entry, s_entry); if (child->s_char == c) { return child; } + + entry = b_queue_next(entry); } return NULL; @@ -161,14 +162,16 @@ static enum ivy_status put_symbol( static void destroy_symbol_tree(struct ivy_asm_lexer_symbol_node *tree) { - b_queue_iterator it; - b_queue_iterator_begin(&tree->s_children, &it); - while (b_queue_iterator_is_valid(&it)) { + b_queue_entry *entry = b_queue_first(&tree->s_children); + while (entry) { struct ivy_asm_lexer_symbol_node *node = b_unbox( - struct ivy_asm_lexer_symbol_node, it.entry, s_entry); - b_queue_iterator_erase(&it); + struct ivy_asm_lexer_symbol_node, entry, s_entry); + b_queue_entry *next = b_queue_next(entry); + b_queue_delete(&tree->s_children, entry); destroy_symbol_tree(node); + + entry = next; } free(tree); @@ -208,7 +211,7 @@ static void init_keywords(b_dict *keyword_dict) static enum ivy_asm_keyword find_keyword_by_name( struct ivy_asm_lexer *lex, const char *s) { - b_number *id = B_NUMBER(b_dict_at(lex->lex_keywords, s)); + b_number *id = b_dict_at(lex->lex_keywords, s); if (!id) { return IVY_ASM_KW_NONE; } @@ -251,14 +254,17 @@ enum ivy_status ivy_asm_lexer_create(struct ivy_asm_lexer **lexp) void ivy_asm_lexer_destroy(struct ivy_asm_lexer *lex) { - b_queue_iterator it = {0}; - b_queue_iterator_begin(&lex->lex_queue, &it); + b_queue_entry *entry = b_queue_first(&lex->lex_queue); - while (b_queue_iterator_is_valid(&it)) { + while (entry) { struct ivy_asm_token *tok - = b_unbox(struct ivy_asm_token, it.entry, t_entry); - b_queue_iterator_erase(&it); + = b_unbox(struct ivy_asm_token, entry, t_entry); + b_queue_entry *next = b_queue_next(entry); + b_queue_delete(&lex->lex_queue, entry); + ivy_asm_token_destroy(tok); + + entry = next; } if (lex->lex_linebuf) { @@ -270,11 +276,11 @@ void ivy_asm_lexer_destroy(struct ivy_asm_lexer *lex) } if (lex->lex_temp) { - b_string_release(lex->lex_temp); + b_string_unref(lex->lex_temp); } if (lex->lex_keywords) { - b_dict_release(lex->lex_keywords); + b_dict_unref(lex->lex_keywords); } destroy_state_stack(&lex->lex_state); diff --git a/asm/lex.h b/asm/lex.h index 47a2b04..8ce4e7c 100644 --- a/asm/lex.h +++ b/asm/lex.h @@ -2,8 +2,8 @@ #define _LEX_H_ #include -#include -#include +#include +#include #include #include diff --git a/asm/mie/select.c b/asm/mie/select.c index 3b12c4e..37103dd 100644 --- a/asm/mie/select.c +++ b/asm/mie/select.c @@ -99,30 +99,32 @@ static enum mie_status get_selector( struct mie_select_graph *graph = mie_select_builder_get_graph(builder); b_queue *nodes = &graph->g_nodes; - b_queue_iterator it; - b_queue_foreach (&it, nodes) { + b_queue_entry *entry = b_queue_first(nodes); + while (entry) { struct mie_select_node *node - = b_unbox(struct mie_select_node, it.entry, n_entry); + = b_unbox(struct mie_select_node, entry, n_entry); if (node->n_target != target) { - continue; + goto skip; } if (node->n_opcode != IVY_SELECT_OP_SELECTOR) { - continue; + goto skip; } if (!node->n_value.v || !mie_value_is_selector(node->n_value.v)) { - continue; + goto skip; } struct mie_selector *sel_node = (struct mie_selector *)node; if (strcmp(sel->sel_value, sel_node->sel_value) != 0) { - continue; + goto skip; } mie_select_node_get_value(node, ptr_type, 0, out); return MIE_SUCCESS; + skip: + entry = b_queue_next(entry); } struct mie_select_node *sel_node; diff --git a/asm/parse/block.c b/asm/parse/block.c index 7ccc455..8d9bb54 100644 --- a/asm/parse/block.c +++ b/asm/parse/block.c @@ -101,9 +101,7 @@ struct mnemonic { }; #define MNEMONIC(id, name, hash) \ - { \ - .m_id = (id), .m_name = (name), .m_hash = (hash) \ - } + {.m_id = (id), .m_name = (name), .m_hash = (hash)} static const struct mnemonic mnemonics[] = { MNEMONIC(IVY_INSTR_LDR, "ldr", 0x127688191dd0471d), @@ -165,7 +163,7 @@ static enum index_base get_index_base(struct ivy_asm_token *tok) } const char *s = tok->t_str; - uint64_t hash = b_hash_string(s); + uint64_t hash = b_hash_cstr(s); switch (hash) { case HASH_SELF: @@ -202,29 +200,31 @@ static enum ivy_instr_id get_instruction_id(b_queue *mnemonic_tokens) char mnemonic[64]; mnemonic[0] = 0; - b_stringstream s; - b_stringstream_begin(&s, mnemonic, sizeof mnemonic); + b_stringstream *s + = b_stringstream_create_with_buffer(mnemonic, sizeof mnemonic); unsigned int i = 0; - b_queue_iterator it = {0}; - b_queue_iterator_begin(mnemonic_tokens, &it); - while (b_queue_iterator_is_valid(&it)) { + b_queue_entry *entry = b_queue_first(mnemonic_tokens); + while (entry) { struct ivy_asm_token *tok - = b_unbox(struct ivy_asm_token, it.entry, t_entry); - b_queue_iterator_erase(&it); + = b_unbox(struct ivy_asm_token, entry, t_entry); + b_queue_entry *next = b_queue_next(entry); + b_queue_delete(mnemonic_tokens, entry); if (i > 0) { - b_stringstream_add(&s, "."); + b_stream_write_char(s, '.'); } - b_stringstream_add(&s, tok->t_str); + b_stream_write_string(s, tok->t_str, NULL); i++; ivy_asm_token_destroy(tok); + + entry = next; } - uint64_t hash = b_hash_string(mnemonic); + uint64_t hash = b_hash_cstr(mnemonic); for (i = 0; i < nr_mnemonics; i++) { if (hash == mnemonics[i].m_hash && !strcmp(mnemonic, mnemonics[i].m_name)) { @@ -251,9 +251,9 @@ static enum ivy_status write_instruction( enum ivy_instr_operand_type operand_types[MAX_ARGS] = {0}; - b_queue_iterator it = {0}; - b_queue_foreach (&it, &state->s_args) { - struct arg *arg = b_unbox(struct arg, it.entry, arg_entry); + b_queue_entry *entry = b_queue_first(&state->s_args); + while (entry) { + struct arg *arg = b_unbox(struct arg, entry, arg_entry); if (i >= MAX_ARGS) { return IVY_ERR_BAD_SYNTAX; @@ -309,6 +309,8 @@ static enum ivy_status write_instruction( default: return IVY_ERR_BAD_SYNTAX; } + + entry = b_queue_next(entry); } const struct ivy_instr_definition *instr_info @@ -321,10 +323,11 @@ static enum ivy_status write_instruction( instr.i_op = instr_info; i = 0; - b_queue_iterator_begin(&state->s_args, &it); - while (b_queue_iterator_is_valid(&it)) { - struct arg *arg = b_unbox(struct arg, it.entry, arg_entry); - b_queue_iterator_erase(&it); + entry = b_queue_first(&state->s_args); + while (entry) { + struct arg *arg = b_unbox(struct arg, entry, arg_entry); + b_queue_entry *next = b_queue_next(entry); + b_queue_delete(&state->s_args, entry); switch (arg->arg_type) { case ARG_REG: @@ -357,6 +360,8 @@ static enum ivy_status write_instruction( } free(arg); + + entry = next; } ivy_assembler_put_instr(p->p_assembler, &instr); diff --git a/asm/parse/class.c b/asm/parse/class.c index 76be265..d0b09c4 100644 --- a/asm/parse/class.c +++ b/asm/parse/class.c @@ -23,7 +23,7 @@ struct class_parser_state { static enum ivy_status push_attrib(struct class_parser_state *state) { - uint64_t hash = b_hash_string(state->s_attrib_name->t_str); + uint64_t hash = b_hash_cstr(state->s_attrib_name->t_str); const char *s = state->s_attrib_name->t_str; unsigned long long v = state->s_attrib_value->t_int.uv; diff --git a/asm/parse/ident.c b/asm/parse/ident.c index a0aa0d0..4213f00 100644 --- a/asm/parse/ident.c +++ b/asm/parse/ident.c @@ -61,15 +61,17 @@ static enum ivy_status parse_right_paren( struct ivy_ident *ident = ivy_ident_create(); - b_queue_iterator it = {0}; - b_queue_iterator_begin(&state->s_parts, &it); - while (b_queue_iterator_is_valid(&it)) { + b_queue_entry *entry = b_queue_first(&state->s_parts); + while (entry) { struct ivy_asm_token *tok - = b_unbox(struct ivy_asm_token, it.entry, t_entry); - b_queue_iterator_erase(&it); + = b_unbox(struct ivy_asm_token, entry, t_entry); + b_queue_entry *next = b_queue_next(entry); + b_queue_delete(&state->s_parts, entry); ivy_ident_add_part(ident, tok->t_str); ivy_asm_token_destroy(tok); + + entry = next; } asm_parser_pop_state(ctx, ident); diff --git a/asm/parse/unit.c b/asm/parse/unit.c index 980a9dc..dd7e1c6 100644 --- a/asm/parse/unit.c +++ b/asm/parse/unit.c @@ -16,7 +16,7 @@ struct unit_parser_state { static enum ivy_status push_attrib(struct unit_parser_state *state) { - uint64_t hash = b_hash_string(state->s_attrib_name->t_str); + uint64_t hash = b_hash_cstr(state->s_attrib_name->t_str); const char *s = state->s_attrib_name->t_str; unsigned long long v = state->s_attrib_value->t_int.uv;