From 17ff4da9bffdf9d3c3b645e6592c19893678da53 Mon Sep 17 00:00:00 2001 From: Ryan Fleury Date: Mon, 31 Mar 2025 13:09:30 -0700 Subject: [PATCH] more eval scratch work --- src/base/base_core.h | 26 +++++++++- src/eval/eval_core.h | 4 +- src/eval/eval_interpret.c | 6 +-- src/eval/eval_parse.c | 71 +++++++++++++++------------ src/lib_rdi_format/rdi_format_parse.c | 1 - src/scratch/eval_scratch.c | 34 +++++++------ 6 files changed, 88 insertions(+), 54 deletions(-) diff --git a/src/base/base_core.h b/src/base/base_core.h index 441fec7f..cc0a6545 100644 --- a/src/base/base_core.h +++ b/src/base/base_core.h @@ -382,11 +382,33 @@ typedef S64 B64; typedef float F32; typedef double F64; typedef void VoidProc(void); -typedef struct U128 U128; -struct U128 +typedef union U128 U128; +union U128 { + U8 u8[16]; + U16 u16[8]; + U32 u32[4]; U64 u64[2]; }; +typedef union U256 U256; +union U256 +{ + U8 u8[32]; + U16 u16[16]; + U32 u32[8]; + U64 u64[4]; + U128 u128[2]; +}; +typedef union U512 U512; +union U512 +{ + U8 u8[64]; + U16 u16[32]; + U32 u32[16]; + U64 u64[8]; + U128 u128[4]; + U256 u256[2]; +}; //////////////////////////////// //~ rjf: Basic Types & Spaces diff --git a/src/eval/eval_core.h b/src/eval/eval_core.h index 650597b8..a9d1fd4a 100644 --- a/src/eval/eval_core.h +++ b/src/eval/eval_core.h @@ -42,8 +42,8 @@ struct E_MsgList typedef union E_Value E_Value; union E_Value { - U64 u512[8]; - U64 u256[4]; + U512 u512; + U256 u256; U128 u128; U64 u64; U32 u32; diff --git a/src/eval/eval_interpret.c b/src/eval/eval_interpret.c index 5f994405..ee5c96f3 100644 --- a/src/eval/eval_interpret.c +++ b/src/eval/eval_interpret.c @@ -572,13 +572,13 @@ e_interpret(String8 bytecode) case RDI_EvalOp_EqEq: { - B32 result = MemoryMatchArray(svals[0].u512, svals[1].u512); + B32 result = MemoryMatchArray(svals[0].u512.u64, svals[1].u512.u64); nval.u64 = !!result; }break; case RDI_EvalOp_NtEq: { - B32 result = MemoryMatchArray(svals[0].u512, svals[1].u512); + B32 result = MemoryMatchArray(svals[0].u512.u64, svals[1].u512.u64); nval.u64 = !result; }break; @@ -813,7 +813,7 @@ e_interpret(String8 bytecode) if(offset + bytes_to_read <= sizeof(E_Value)) { E_Value src_val = svals[1]; - MemoryCopy(&nval.u512[0], (U8 *)(&src_val.u512[0]) + offset, bytes_to_read); + MemoryCopy(&nval.u512.u64[0], (U8 *)(&src_val.u512.u64[0]) + offset, bytes_to_read); } }break; diff --git a/src/eval/eval_parse.c b/src/eval/eval_parse.c index 6ecd95eb..6076f906 100644 --- a/src/eval/eval_parse.c +++ b/src/eval/eval_parse.c @@ -633,6 +633,10 @@ e_select_parse_ctx(E_ParseCtx *ctx) e_parse_state->arena_eval_start_pos = arena_pos(arena); } arena_pop_to(e_parse_state->arena, e_parse_state->arena_eval_start_pos); + if(ctx->regs_map == 0) { ctx->regs_map = &e_string2num_map_nil; } + if(ctx->reg_alias_map == 0) { ctx->reg_alias_map = &e_string2num_map_nil; } + if(ctx->locals_map == 0) { ctx->locals_map = &e_string2num_map_nil; } + if(ctx->member_map == 0) { ctx->member_map = &e_string2num_map_nil; } e_parse_state->ctx = ctx; } @@ -1498,43 +1502,46 @@ e_parse_expr_from_text_tokens__prec(Arena *arena, String8 text, E_TokenArray *to arch = module->arch; U64 all_location_data_size = 0; U8 *all_location_data = rdi_table_from_name(rdi, LocationData, &all_location_data_size); - loc_kind = *((RDI_LocationKind *)(all_location_data + block->location_data_off)); - switch(loc_kind) + if(block->location_data_off + sizeof(RDI_LocationKind) <= all_location_data_size) { - default:{mapped_identifier = 0;}break; - case RDI_LocationKind_ValBytecodeStream: goto bytecode_stream; - case RDI_LocationKind_AddrBytecodeStream: goto bytecode_stream; - bytecode_stream:; + loc_kind = *((RDI_LocationKind *)(all_location_data + block->location_data_off)); + switch(loc_kind) { - U64 bytecode_size = 0; - U64 off_first = block->location_data_off + sizeof(RDI_LocationKind); - U64 off_opl = all_location_data_size; - for(U64 off = off_first, next_off = off_opl; - off < all_location_data_size; - off = next_off) + default:{mapped_identifier = 0;}break; + case RDI_LocationKind_ValBytecodeStream: goto bytecode_stream; + case RDI_LocationKind_AddrBytecodeStream: goto bytecode_stream; + bytecode_stream:; { - next_off = off_opl; - U8 op = all_location_data[off]; - if(op == 0) + U64 bytecode_size = 0; + U64 off_first = block->location_data_off + sizeof(RDI_LocationKind); + U64 off_opl = all_location_data_size; + for(U64 off = off_first, next_off = off_opl; + off < all_location_data_size; + off = next_off) { - break; + next_off = off_opl; + U8 op = all_location_data[off]; + if(op == 0) + { + break; + } + U16 ctrlbits = rdi_eval_op_ctrlbits_table[op]; + U32 p_size = RDI_DECODEN_FROM_CTRLBITS(ctrlbits); + bytecode_size += (1 + p_size); + next_off = (off + 1 + p_size); } - U16 ctrlbits = rdi_eval_op_ctrlbits_table[op]; - U32 p_size = RDI_DECODEN_FROM_CTRLBITS(ctrlbits); - bytecode_size += (1 + p_size); - next_off = (off + 1 + p_size); - } - loc_bytecode = str8(all_location_data + off_first, bytecode_size); - }break; - case RDI_LocationKind_AddrRegPlusU16: - case RDI_LocationKind_AddrAddrRegPlusU16: - { - MemoryCopy(&loc_reg_u16, (all_location_data + block->location_data_off), sizeof(loc_reg_u16)); - }break; - case RDI_LocationKind_ValReg: - { - MemoryCopy(&loc_reg, (all_location_data + block->location_data_off), sizeof(loc_reg)); - }break; + loc_bytecode = str8(all_location_data + off_first, bytecode_size); + }break; + case RDI_LocationKind_AddrRegPlusU16: + case RDI_LocationKind_AddrAddrRegPlusU16: + { + MemoryCopy(&loc_reg_u16, (all_location_data + block->location_data_off), sizeof(loc_reg_u16)); + }break; + case RDI_LocationKind_ValReg: + { + MemoryCopy(&loc_reg, (all_location_data + block->location_data_off), sizeof(loc_reg)); + }break; + } } } } diff --git a/src/lib_rdi_format/rdi_format_parse.c b/src/lib_rdi_format/rdi_format_parse.c index 6f51204d..f099ec6c 100644 --- a/src/lib_rdi_format/rdi_format_parse.c +++ b/src/lib_rdi_format/rdi_format_parse.c @@ -834,4 +834,3 @@ rdi_size_from_bytecode_stream(RDI_U8 *ptr, RDI_U8 *opl) } return bytecode_size; } - diff --git a/src/scratch/eval_scratch.c b/src/scratch/eval_scratch.c index 3b43e616..8b4de5b0 100644 --- a/src/scratch/eval_scratch.c +++ b/src/scratch/eval_scratch.c @@ -27,7 +27,7 @@ internal void entry_point(CmdLine *cmdline) { Arena *arena = arena_alloc(); - char *indent_spaces = " "; + char *indent_spaces = " "; E_TypeCtx *type_ctx = push_array(arena, E_TypeCtx, 1); e_select_type_ctx(type_ctx); E_ParseCtx *parse_ctx = push_array(arena, E_ParseCtx, 1); @@ -39,6 +39,8 @@ entry_point(CmdLine *cmdline) String8 exprs[] = { str8_lit("123"), + str8_lit("1 + 2"), + str8_lit("foo"), }; for EachElement(idx, exprs) { @@ -48,12 +50,12 @@ entry_point(CmdLine *cmdline) //- rjf: tokenize E_TokenArray tokens = e_token_array_from_text(arena, expr_text); - raddbg_log(" tokens:\n"); + raddbg_log(" tokens:\n"); for EachIndex(idx, tokens.count) { E_Token token = tokens.v[idx]; String8 token_string = str8_substr(expr_text, token.range); - raddbg_log(" %S: `%S`\n", e_token_kind_strings[token.kind], token_string); + raddbg_log(" %S: `%S`\n", e_token_kind_strings[token.kind], token_string); } //- rjf: parse @@ -66,13 +68,13 @@ entry_point(CmdLine *cmdline) E_Expr *expr; S32 indent; }; - raddbg_log(" expr:\n"); + raddbg_log(" expr:\n"); Task start_task = {0, parse.exprs.first, 2}; Task *first_task = &start_task; for(Task *t = first_task; t != 0; t = t->next) { E_Expr *expr = t->expr; - raddbg_log("%.*s%S", (int)t->indent*2, indent_spaces, e_expr_kind_strings[expr->kind]); + raddbg_log("%.*s%S", (int)t->indent*4, indent_spaces, e_expr_kind_strings[expr->kind]); switch(expr->kind) { default:{}break; @@ -82,13 +84,15 @@ entry_point(CmdLine *cmdline) }break; } raddbg_log("\n"); + Task *last_task = t; for(E_Expr *child = expr->first; child != &e_expr_nil; child = child->next) { Task *task = push_array(arena, Task, 1); - task->next = t->next; - t->next = task; + task->next = last_task->next; + last_task->next = task; task->expr = child; task->indent = t->indent+1; + last_task = task; } } } @@ -96,7 +100,7 @@ entry_point(CmdLine *cmdline) //- rjf: type E_IRTreeAndType irtree = e_irtree_and_type_from_expr(arena, parse.exprs.first); { - raddbg_log(" type:\n"); + raddbg_log(" type:\n"); S32 indent = 2; for(E_TypeKey type_key = irtree.type_key; !e_type_key_match(e_type_key_zero(), type_key); @@ -104,7 +108,7 @@ entry_point(CmdLine *cmdline) indent += 1) { E_Type *type = e_type_from_key(arena, type_key); - raddbg_log("%.*s%S\n", (int)indent*2, indent_spaces, e_kind_basic_string_table[type->kind]); + raddbg_log("%.*s%S\n", (int)indent*4, indent_spaces, e_kind_basic_string_table[type->kind]); } } @@ -117,17 +121,17 @@ entry_point(CmdLine *cmdline) E_IRNode *irnode; S32 indent; }; - raddbg_log(" ir_tree:\n"); + raddbg_log(" ir_tree:\n"); Task start_task = {0, irtree.root, 2}; Task *first_task = &start_task; for(Task *t = first_task; t != 0; t = t->next) { E_IRNode *irnode = t->irnode; - raddbg_log("%.*s", (int)t->indent*2, indent_spaces); + raddbg_log("%.*s", (int)t->indent*4, indent_spaces); switch(irnode->op) { default:{}break; -#define X(name) case RDI_EvalOp_##name:{raddbg_log(" " #name);}break; +#define X(name) case RDI_EvalOp_##name:{raddbg_log(#name);}break; RDI_EvalOp_XList #undef X } @@ -136,13 +140,15 @@ entry_point(CmdLine *cmdline) raddbg_log(" (%I64u)", irnode->value.u64); } raddbg_log("\n"); + Task *last_task = t; for(E_IRNode *child = irnode->first; child != &e_irnode_nil; child = child->next) { Task *task = push_array(arena, Task, 1); - task->next = t->next; - t->next = task; + task->next = last_task->next; + last_task->next = task; task->irnode = child; task->indent = t->indent+1; + last_task = task; } } }