small string evaluation; strings as values in eval

This commit is contained in:
Ryan Fleury
2024-08-14 10:49:56 -07:00
parent eac1672c4e
commit 918895c091
12 changed files with 175 additions and 112 deletions
+30
View File
@@ -19,6 +19,7 @@ e_eval_from_string(Arena *arena, String8 string)
.mode = irtree.mode,
.type_key = irtree.type_key,
.code = interp.code,
.advance = parse.last_token >= tokens.v + tokens.count ? string.size : parse.last_token->range.min,
};
e_msg_list_concat_in_place(&eval.msgs, &parse.msgs);
e_msg_list_concat_in_place(&eval.msgs, &irtree.msgs);
@@ -200,3 +201,32 @@ e_value_eval_from_eval(E_Eval eval)
return eval;
}
internal E_Eval
e_element_eval_from_array_eval_index(E_Eval eval, U64 index)
{
E_Eval result = {0};
result.mode = eval.mode;
result.type_key = e_type_direct_from_key(eval.type_key);
result.code = eval.code;
result.msgs = eval.msgs;
U64 element_size = e_type_byte_size_from_key(result.type_key);
switch(eval.mode)
{
default:{}break;
case E_Mode_Value:
if(element_size <= sizeof(E_Value) &&
index < sizeof(E_Value)/element_size)
{
MemoryCopy((U8 *)(&result.value.u512[0]),
(U8 *)(&eval.value.u512[0]) + index*element_size,
element_size);
}break;
case E_Mode_Addr:
case E_Mode_Reg:
{
result.value.u64 = eval.value.u64 + element_size*index;
}break;
}
return result;
}
+2
View File
@@ -15,6 +15,7 @@ struct E_Eval
E_TypeKey type_key;
E_InterpretationCode code;
E_MsgList msgs;
U64 advance;
};
////////////////////////////////
@@ -24,5 +25,6 @@ internal E_Eval e_eval_from_string(Arena *arena, String8 string);
internal E_Eval e_autoresolved_eval_from_eval(E_Eval eval);
internal E_Eval e_dynamically_typed_eval_from_eval(E_Eval eval);
internal E_Eval e_value_eval_from_eval(E_Eval eval);
internal E_Eval e_element_eval_from_array_eval_index(E_Eval eval, U64 index);
#endif // EVAL_BUNDLES_H
+6
View File
@@ -206,6 +206,12 @@ e_interpret(String8 bytecode)
nval.u64 = imm;
}break;
case RDI_EvalOp_ConstString:
{
MemoryCopy(&nval, ptr, imm);
ptr += imm;
}break;
case RDI_EvalOp_Abs:
{
if(imm == RDI_EvalTypeGroup_F32)
+42 -14
View File
@@ -82,7 +82,7 @@ e_oplist_push_op(Arena *arena, E_OpList *list, RDI_EvalOp opcode, U64 p)
U32 p_size = RDI_DECODEN_FROM_CTRLBITS(ctrlbits);
E_Op *node = push_array_no_zero(arena, E_Op, 1);
node->opcode = opcode;
node->p = p;
node->u64 = p;
SLLQueuePush(list->first, list->last, node);
list->op_count += 1;
list->encoded_size += 1 + p_size;
@@ -127,12 +127,27 @@ e_oplist_push_bytecode(Arena *arena, E_OpList *list, String8 bytecode)
{
E_Op *node = push_array_no_zero(arena, E_Op, 1);
node->opcode = E_IRExtKind_Bytecode;
node->bytecode = bytecode;
node->string = bytecode;
SLLQueuePush(list->first, list->last, node);
list->op_count += 1;
list->encoded_size += bytecode.size;
}
internal void
e_oplist_push_string_literal(Arena *arena, E_OpList *list, String8 string)
{
RDI_EvalOp opcode = RDI_EvalOp_ConstString;
U8 ctrlbits = rdi_eval_op_ctrlbits_table[opcode];
U32 p_size = RDI_DECODEN_FROM_CTRLBITS(ctrlbits);
E_Op *node = push_array_no_zero(arena, E_Op, 1);
node->opcode = opcode;
node->string = string;
node->u64 = Min(string.size, 64);
SLLQueuePush(list->first, list->last, node);
list->op_count += 1;
list->encoded_size += 1 + p_size + node->u64;
}
internal void
e_oplist_concat_in_place(E_OpList *dst, E_OpList *to_push)
{
@@ -231,8 +246,8 @@ e_irtree_bytecode_no_copy(Arena *arena, String8 bytecode)
internal E_IRNode *
e_irtree_string_literal(Arena *arena, String8 string)
{
E_IRNode *root = e_push_irnode(arena, RDI_EvalOp_ConstU512);
root->string = str8(string.str, Min(64, string.size));
E_IRNode *root = e_push_irnode(arena, RDI_EvalOp_ConstString);
root->string = string;
return root;
}
@@ -1138,13 +1153,6 @@ e_append_oplist_from_irtree(Arena *arena, E_IRNode *root, E_OpList *out)
e_oplist_push_bytecode(arena, out, root->string);
}break;
case RDI_EvalOp_ConstU128:
case RDI_EvalOp_ConstU256:
case RDI_EvalOp_ConstU512:
{
// TODO(rjf)
}break;
case RDI_EvalOp_Cond:
{
// rjf: generate oplists for each child
@@ -1171,6 +1179,11 @@ e_append_oplist_from_irtree(Arena *arena, E_IRNode *root, E_OpList *out)
e_oplist_concat_in_place(out, &prt_left);
}break;
case RDI_EvalOp_ConstString:
{
e_oplist_push_string_literal(arena, out, root->string);
}break;
default:
{
if(op >= RDI_EvalOp_COUNT)
@@ -1232,7 +1245,22 @@ e_bytecode_from_oplist(Arena *arena, E_OpList *oplist)
// rjf: fill bytecode
ptr[0] = opcode;
MemoryCopy(ptr + 1, &op->p, extra_byte_count);
MemoryCopy(ptr + 1, &op->u64, extra_byte_count);
// rjf: advance
ptr = next_ptr;
}break;
case RDI_EvalOp_ConstString:
{
// rjf: compute bytecode advance
U8 *next_ptr = ptr + 2 + op->u64;
Assert(next_ptr <= opl);
// rjf: fill
ptr[0] = opcode;
ptr[1] = (U8)op->u64;
MemoryCopy(ptr+2, op->string.str, op->u64);
// rjf: advance
ptr = next_ptr;
@@ -1241,12 +1269,12 @@ e_bytecode_from_oplist(Arena *arena, E_OpList *oplist)
case E_IRExtKind_Bytecode:
{
// rjf: compute bytecode advance
U64 size = op->bytecode.size;
U64 size = op->string.size;
U8 *next_ptr = ptr + size;
Assert(next_ptr <= opl);
// rjf: fill bytecode
MemoryCopy(ptr, op->bytecode.str, size);
MemoryCopy(ptr, op->string.str, size);
// rjf: advance
ptr = next_ptr;
+3 -6
View File
@@ -18,12 +18,8 @@ struct E_Op
{
E_Op *next;
RDI_EvalOp opcode;
union
{
U64 p;
String8 bytecode;
String8 data;
};
U64 u64;
String8 string;
};
typedef struct E_OpList E_OpList;
@@ -93,6 +89,7 @@ internal void e_oplist_push_op(Arena *arena, E_OpList *list, RDI_EvalOp opcode,
internal void e_oplist_push_uconst(Arena *arena, E_OpList *list, U64 x);
internal void e_oplist_push_sconst(Arena *arena, E_OpList *list, S64 x);
internal void e_oplist_push_bytecode(Arena *arena, E_OpList *list, String8 bytecode);
internal void e_oplist_push_string_literal(Arena *arena, E_OpList *list, String8 string);
internal void e_oplist_concat_in_place(E_OpList *dst, E_OpList *to_push);
//- rjf: ir tree core building helpers