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,8 +1,9 @@
#include "parse.h"
#include <ivy/asm/assembler.h>
#include <ivy/asm/bin.h>
#include <ivy/ident.h>
#include <ivy/selector.h>
#include <ivy/asm/bin.h>
#include <ivy/asm/assembler.h>
struct constpool_parser_state {
struct parser_state s_base;
@@ -20,9 +21,11 @@ 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;
@@ -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,7 +159,8 @@ 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;
@@ -164,7 +176,8 @@ static enum ivy_status parse_string(
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;
@@ -180,7 +193,8 @@ static enum ivy_status parse_atom(
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;
@@ -196,7 +210,8 @@ static enum ivy_status parse_kw_ident(
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,7 +226,8 @@ 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,7 +241,8 @@ 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: