asm: parse: use specified indices when writing constpool values

This commit is contained in:
2025-05-15 12:09:39 +01:00
parent 2845c29c5f
commit a76525e11f

View File

@@ -1,13 +1,14 @@
#include "parse.h" #include "parse.h"
#include <ivy/asm/assembler.h>
#include <ivy/asm/bin.h>
#include <ivy/ident.h> #include <ivy/ident.h>
#include <ivy/selector.h> #include <ivy/selector.h>
#include <ivy/asm/bin.h>
#include <ivy/asm/assembler.h>
struct constpool_parser_state { struct constpool_parser_state {
struct parser_state s_base; struct parser_state s_base;
unsigned int s_prev_token; unsigned int s_prev_token;
enum ivy_assembler_pval_type s_current_pval_type; enum ivy_assembler_pval_type s_current_pval_type;
union { union {
@@ -20,13 +21,15 @@ struct constpool_parser_state {
size_t s_current_index; 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}; struct ivy_bin_constpool_table_entry entry = {0};
state->s_prev_token = IVY_ASM_TOK_LINEFEED; state->s_prev_token = IVY_ASM_TOK_LINEFEED;
if (!state->s_current_index_set) { if (!state->s_current_index_set) {
/* no index set */ /* no index set */
return IVY_OK; 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: case IVY_ASM_PVAL_IDENT:
status = ivy_assembler_put_pval( status = ivy_assembler_put_pval(
ctx->p_assembler, IVY_ASM_PVAL_IDENT, 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) { if (status != IVY_OK) {
return status; 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: case IVY_ASM_PVAL_ATOM:
tmp = state->s_base.s_previous_value; tmp = state->s_base.s_previous_value;
status = ivy_assembler_put_pval( 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) { if (status != IVY_OK) {
return status; return status;
} }
@@ -63,7 +68,8 @@ static enum ivy_status parse_linefeed(struct ivy_asm_parser *ctx, struct ivy_asm
break; break;
case IVY_ASM_PVAL_SINT: case IVY_ASM_PVAL_SINT:
entry.e_type = b_i32_htob(IVY_CONSTPOOL_TABLE_INT); 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; break;
case IVY_ASM_PVAL_UINT: case IVY_ASM_PVAL_UINT:
entry.e_type = b_i32_htob(IVY_CONSTPOOL_TABLE_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: case IVY_ASM_PVAL_SELECTOR:
status = ivy_assembler_put_pval( status = ivy_assembler_put_pval(
ctx->p_assembler, IVY_ASM_PVAL_SELECTOR, 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) { if (status != IVY_OK) {
return status; 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: case IVY_ASM_PVAL_STRING:
tmp = state->s_current_pval_val.tok; tmp = state->s_current_pval_val.tok;
status = ivy_assembler_put_pval( 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) { if (status != IVY_OK) {
return status; 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( static enum ivy_status parse_int(
struct ivy_asm_parser *ctx, struct ivy_asm_token *tok) 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) { if (state->s_prev_token == IVY_ASM_SYM_COLON) {
/* this is a const value. */ /* this is a const value. */
@@ -120,7 +129,8 @@ static enum ivy_status parse_int(
} }
if (!state->s_current_index_set) { 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_current_index_set = true;
state->s_prev_token = IVY_ASM_TOK_INT; state->s_prev_token = IVY_ASM_TOK_INT;
return IVY_OK; return IVY_OK;
@@ -133,7 +143,8 @@ static enum ivy_status parse_int(
static enum ivy_status parse_colon( static enum ivy_status parse_colon(
struct ivy_asm_parser *ctx, struct ivy_asm_token *tok) 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) { if (state->s_prev_token != IVY_ASM_TOK_INT) {
/* not expected at this time */ /* not expected at this time */
@@ -148,8 +159,9 @@ static enum ivy_status parse_colon(
static enum ivy_status parse_string( static enum ivy_status parse_string(
struct ivy_asm_parser *ctx, struct ivy_asm_token *tok) 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) { if (state->s_prev_token != IVY_ASM_SYM_COLON) {
return IVY_ERR_BAD_SYNTAX; 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_type = IVY_ASM_PVAL_STRING;
state->s_current_pval_val.tok = tok; state->s_current_pval_val.tok = tok;
state->s_prev_token = IVY_ASM_TOK_STRING; state->s_prev_token = IVY_ASM_TOK_STRING;
return IVY_OK; return IVY_OK;
} }
static enum ivy_status parse_atom( static enum ivy_status parse_atom(
struct ivy_asm_parser *ctx, struct ivy_asm_token *tok) 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) { if (state->s_prev_token != IVY_ASM_SYM_COLON) {
return IVY_ERR_BAD_SYNTAX; 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_type = IVY_ASM_PVAL_ATOM;
state->s_current_pval_val.tok = tok; state->s_current_pval_val.tok = tok;
state->s_prev_token = IVY_ASM_KW_ATOM; state->s_prev_token = IVY_ASM_KW_ATOM;
return IVY_OK; return IVY_OK;
} }
static enum ivy_status parse_kw_ident( static enum ivy_status parse_kw_ident(
struct ivy_asm_parser *ctx, struct ivy_asm_token *tok) 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) { if (state->s_prev_token != IVY_ASM_SYM_COLON) {
return IVY_ERR_BAD_SYNTAX; 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_type = IVY_ASM_PVAL_IDENT;
state->s_current_pval_val.tok = tok; state->s_current_pval_val.tok = tok;
state->s_prev_token = IVY_ASM_KW_IDENT; state->s_prev_token = IVY_ASM_KW_IDENT;
return IVY_OK; return IVY_OK;
} }
static enum ivy_status parse_selector( static enum ivy_status parse_selector(
struct ivy_asm_parser *ctx, struct ivy_asm_token *tok) 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) { if (state->s_prev_token != IVY_ASM_SYM_COLON) {
return IVY_ERR_BAD_SYNTAX; return IVY_ERR_BAD_SYNTAX;
} }
@@ -211,8 +226,9 @@ static enum ivy_status parse_selector(
static enum ivy_status parse_end( static enum ivy_status parse_end(
struct ivy_asm_parser *ctx, struct ivy_asm_token *tok) 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) { if (state->s_current_index_set) {
return IVY_ERR_BAD_SYNTAX; return IVY_ERR_BAD_SYNTAX;
} }
@@ -225,8 +241,9 @@ static enum ivy_status parse_end(
static enum ivy_status parse_left_paren( static enum ivy_status parse_left_paren(
struct ivy_asm_parser *ctx, struct ivy_asm_token *tok) 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) { switch (state->s_prev_token) {
case IVY_ASM_KW_SELECTOR: case IVY_ASM_KW_SELECTOR:
asm_parser_push_state(ctx, ASM_PARSER_SELECTOR, NULL); 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(ATOM, parse_atom),
KW_PARSER(END, parse_end), KW_PARSER(END, parse_end),
} }
}; };