From a76525e11fdc66fe67147e4bbf5e04ef48f51b95 Mon Sep 17 00:00:00 2001 From: Max Wash Date: Thu, 15 May 2025 12:09:39 +0100 Subject: [PATCH] asm: parse: use specified indices when writing constpool values --- asm/parse/constpool.c | 79 ++++++++++++++++++++++++++----------------- 1 file changed, 48 insertions(+), 31 deletions(-) diff --git a/asm/parse/constpool.c b/asm/parse/constpool.c index c76c51f..3a2aa4a 100644 --- a/asm/parse/constpool.c +++ b/asm/parse/constpool.c @@ -1,13 +1,14 @@ #include "parse.h" + +#include +#include #include #include -#include -#include struct constpool_parser_state { struct parser_state s_base; unsigned int s_prev_token; - + enum ivy_assembler_pval_type s_current_pval_type; union { @@ -20,13 +21,15 @@ struct constpool_parser_state { size_t s_current_index; }; -static enum ivy_status parse_linefeed(struct ivy_asm_parser *ctx, struct ivy_asm_token *tok) +static enum ivy_status parse_linefeed( + struct ivy_asm_parser *ctx, struct ivy_asm_token *tok) { - struct constpool_parser_state *state = (struct constpool_parser_state *)asm_parser_get_state(ctx); + struct constpool_parser_state *state + = (struct constpool_parser_state *)asm_parser_get_state(ctx); struct ivy_bin_constpool_table_entry entry = {0}; - + state->s_prev_token = IVY_ASM_TOK_LINEFEED; - + if (!state->s_current_index_set) { /* no index set */ return IVY_OK; @@ -40,7 +43,8 @@ static enum ivy_status parse_linefeed(struct ivy_asm_parser *ctx, struct ivy_asm case IVY_ASM_PVAL_IDENT: status = ivy_assembler_put_pval( ctx->p_assembler, IVY_ASM_PVAL_IDENT, - state->s_base.s_previous_value, &key); + state->s_current_index, state->s_base.s_previous_value, + &key); if (status != IVY_OK) { return status; } @@ -52,7 +56,8 @@ static enum ivy_status parse_linefeed(struct ivy_asm_parser *ctx, struct ivy_asm case IVY_ASM_PVAL_ATOM: tmp = state->s_base.s_previous_value; status = ivy_assembler_put_pval( - ctx->p_assembler, IVY_ASM_PVAL_ATOM, tmp->t_str, &key); + ctx->p_assembler, IVY_ASM_PVAL_ATOM, + state->s_current_index, tmp->t_str, &key); if (status != IVY_OK) { return status; } @@ -63,7 +68,8 @@ static enum ivy_status parse_linefeed(struct ivy_asm_parser *ctx, struct ivy_asm break; case IVY_ASM_PVAL_SINT: entry.e_type = b_i32_htob(IVY_CONSTPOOL_TABLE_INT); - entry.e_int = b_i32_htob((uint32_t)state->s_current_pval_val.tok->t_int.v); + entry.e_int = b_i32_htob( + (uint32_t)state->s_current_pval_val.tok->t_int.v); break; case IVY_ASM_PVAL_UINT: entry.e_type = b_i32_htob(IVY_CONSTPOOL_TABLE_UINT); @@ -73,7 +79,8 @@ static enum ivy_status parse_linefeed(struct ivy_asm_parser *ctx, struct ivy_asm case IVY_ASM_PVAL_SELECTOR: status = ivy_assembler_put_pval( ctx->p_assembler, IVY_ASM_PVAL_SELECTOR, - state->s_base.s_previous_value, &key); + state->s_current_index, state->s_base.s_previous_value, + &key); if (status != IVY_OK) { return status; } @@ -85,7 +92,8 @@ static enum ivy_status parse_linefeed(struct ivy_asm_parser *ctx, struct ivy_asm case IVY_ASM_PVAL_STRING: tmp = state->s_current_pval_val.tok; status = ivy_assembler_put_pval( - ctx->p_assembler, IVY_ASM_PVAL_STRING, tmp->t_str, &key); + ctx->p_assembler, IVY_ASM_PVAL_STRING, + state->s_current_index, tmp->t_str, &key); if (status != IVY_OK) { return status; } @@ -109,7 +117,8 @@ static enum ivy_status parse_linefeed(struct ivy_asm_parser *ctx, struct ivy_asm static enum ivy_status parse_int( struct ivy_asm_parser *ctx, struct ivy_asm_token *tok) { - struct constpool_parser_state *state = (struct constpool_parser_state *)asm_parser_get_state(ctx); + struct constpool_parser_state *state + = (struct constpool_parser_state *)asm_parser_get_state(ctx); if (state->s_prev_token == IVY_ASM_SYM_COLON) { /* this is a const value. */ @@ -120,7 +129,8 @@ static enum ivy_status parse_int( } if (!state->s_current_index_set) { - state->s_current_index = tok->t_int.uv; + state->s_current_index + = tok->t_int.sign ? tok->t_int.v : tok->t_int.uv; state->s_current_index_set = true; state->s_prev_token = IVY_ASM_TOK_INT; return IVY_OK; @@ -133,7 +143,8 @@ static enum ivy_status parse_int( static enum ivy_status parse_colon( struct ivy_asm_parser *ctx, struct ivy_asm_token *tok) { - struct constpool_parser_state *state = (struct constpool_parser_state *)asm_parser_get_state(ctx); + struct constpool_parser_state *state + = (struct constpool_parser_state *)asm_parser_get_state(ctx); if (state->s_prev_token != IVY_ASM_TOK_INT) { /* not expected at this time */ @@ -148,8 +159,9 @@ static enum ivy_status parse_colon( static enum ivy_status parse_string( struct ivy_asm_parser *ctx, struct ivy_asm_token *tok) { - struct constpool_parser_state *state = (struct constpool_parser_state *)asm_parser_get_state(ctx); - + struct constpool_parser_state *state + = (struct constpool_parser_state *)asm_parser_get_state(ctx); + if (state->s_prev_token != IVY_ASM_SYM_COLON) { return IVY_ERR_BAD_SYNTAX; } @@ -157,15 +169,16 @@ static enum ivy_status parse_string( state->s_current_pval_type = IVY_ASM_PVAL_STRING; state->s_current_pval_val.tok = tok; state->s_prev_token = IVY_ASM_TOK_STRING; - + return IVY_OK; } static enum ivy_status parse_atom( struct ivy_asm_parser *ctx, struct ivy_asm_token *tok) { - struct constpool_parser_state *state = (struct constpool_parser_state *)asm_parser_get_state(ctx); - + struct constpool_parser_state *state + = (struct constpool_parser_state *)asm_parser_get_state(ctx); + if (state->s_prev_token != IVY_ASM_SYM_COLON) { return IVY_ERR_BAD_SYNTAX; } @@ -173,15 +186,16 @@ static enum ivy_status parse_atom( state->s_current_pval_type = IVY_ASM_PVAL_ATOM; state->s_current_pval_val.tok = tok; state->s_prev_token = IVY_ASM_KW_ATOM; - + return IVY_OK; } static enum ivy_status parse_kw_ident( struct ivy_asm_parser *ctx, struct ivy_asm_token *tok) { - struct constpool_parser_state *state = (struct constpool_parser_state *)asm_parser_get_state(ctx); - + struct constpool_parser_state *state + = (struct constpool_parser_state *)asm_parser_get_state(ctx); + if (state->s_prev_token != IVY_ASM_SYM_COLON) { return IVY_ERR_BAD_SYNTAX; } @@ -189,15 +203,16 @@ static enum ivy_status parse_kw_ident( state->s_current_pval_type = IVY_ASM_PVAL_IDENT; state->s_current_pval_val.tok = tok; state->s_prev_token = IVY_ASM_KW_IDENT; - + return IVY_OK; } static enum ivy_status parse_selector( struct ivy_asm_parser *ctx, struct ivy_asm_token *tok) { - struct constpool_parser_state *state = (struct constpool_parser_state *)asm_parser_get_state(ctx); - + struct constpool_parser_state *state + = (struct constpool_parser_state *)asm_parser_get_state(ctx); + if (state->s_prev_token != IVY_ASM_SYM_COLON) { return IVY_ERR_BAD_SYNTAX; } @@ -211,8 +226,9 @@ static enum ivy_status parse_selector( static enum ivy_status parse_end( struct ivy_asm_parser *ctx, struct ivy_asm_token *tok) { - struct constpool_parser_state *state = (struct constpool_parser_state *)asm_parser_get_state(ctx); - + struct constpool_parser_state *state + = (struct constpool_parser_state *)asm_parser_get_state(ctx); + if (state->s_current_index_set) { return IVY_ERR_BAD_SYNTAX; } @@ -225,8 +241,9 @@ static enum ivy_status parse_end( static enum ivy_status parse_left_paren( struct ivy_asm_parser *ctx, struct ivy_asm_token *tok) { - struct constpool_parser_state *state = (struct constpool_parser_state *)asm_parser_get_state(ctx); - + struct constpool_parser_state *state + = (struct constpool_parser_state *)asm_parser_get_state(ctx); + switch (state->s_prev_token) { case IVY_ASM_KW_SELECTOR: asm_parser_push_state(ctx, ASM_PARSER_SELECTOR, NULL); @@ -273,4 +290,4 @@ struct parser_state_type constpool_parser_state_type = { KW_PARSER(ATOM, parse_atom), KW_PARSER(END, parse_end), } -}; \ No newline at end of file +};