Skip to content

Commit 657b78c

Browse files
committed
eval: separate ir_ctx -> ir_state & ir_ctx; ir_state for implicit thread-local caching mechanisms for eval, ir_ctx for explicitly supplied user info
1 parent 9a405be commit 657b78c

File tree

3 files changed

+58
-37
lines changed

3 files changed

+58
-37
lines changed

src/eval/eval_ir.c

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,26 @@ e_expr_kind_is_comparison(E_ExprKind kind)
6161
internal E_IRCtx *
6262
e_selected_ir_ctx(void)
6363
{
64-
return e_ir_ctx;
64+
return e_ir_state->ctx;
6565
}
6666

6767
internal void
6868
e_select_ir_ctx(E_IRCtx *ctx)
6969
{
70-
e_ir_ctx = ctx;
70+
if(e_ir_state == 0)
71+
{
72+
Arena *arena = arena_alloc();
73+
e_ir_state = push_array(arena, E_IRState, 1);
74+
e_ir_state->arena = arena;
75+
e_ir_state->arena_eval_start_pos = arena_pos(arena);
76+
}
77+
e_ir_state->ctx = ctx;
78+
e_ir_state->used_tag_map = push_array(e_ir_state->arena, E_UsedTagMap, 1);
79+
e_ir_state->used_tag_map->slots_count = 64;
80+
e_ir_state->used_tag_map->slots = push_array(e_ir_state->arena, E_UsedTagSlot, e_ir_state->used_tag_map->slots_count);
81+
e_ir_state->type_auto_hook_cache_map = push_array(e_ir_state->arena, E_TypeAutoHookCacheMap, 1);
82+
e_ir_state->type_auto_hook_cache_map->slots_count = 256;
83+
e_ir_state->type_auto_hook_cache_map->slots = push_array(e_ir_state->arena, E_TypeAutoHookCacheSlot, e_ir_state->type_auto_hook_cache_map->slots_count);
7184
}
7285

7386
////////////////////////////////
@@ -102,11 +115,11 @@ internal E_LookupRule *
102115
e_lookup_rule_from_string(String8 string)
103116
{
104117
E_LookupRule *result = &e_lookup_rule__nil;
105-
if(e_ir_ctx->lookup_rule_map != 0 && e_ir_ctx->lookup_rule_map->slots_count != 0)
118+
if(e_ir_state->ctx->lookup_rule_map != 0 && e_ir_state->ctx->lookup_rule_map->slots_count != 0)
106119
{
107120
U64 hash = e_hash_from_string(5381, string);
108-
U64 slot_idx = hash%e_ir_ctx->lookup_rule_map->slots_count;
109-
for(E_LookupRuleNode *n = e_ir_ctx->lookup_rule_map->slots[slot_idx].first;
121+
U64 slot_idx = hash%e_ir_state->ctx->lookup_rule_map->slots_count;
122+
for(E_LookupRuleNode *n = e_ir_state->ctx->lookup_rule_map->slots[slot_idx].first;
110123
n != 0;
111124
n = n->next)
112125
{
@@ -664,9 +677,9 @@ internal E_IRGenRule *
664677
e_irgen_rule_from_string(String8 string)
665678
{
666679
E_IRGenRule *rule = &e_irgen_rule__default;
667-
if(e_ir_ctx != 0 && e_ir_ctx->irgen_rule_map != 0 && e_ir_ctx->irgen_rule_map->slots_count != 0)
680+
if(e_ir_state != 0 && e_ir_state->ctx != 0 && e_ir_state->ctx->irgen_rule_map != 0 && e_ir_state->ctx->irgen_rule_map->slots_count != 0)
668681
{
669-
E_IRGenRuleMap *map = e_ir_ctx->irgen_rule_map;
682+
E_IRGenRuleMap *map = e_ir_state->ctx->irgen_rule_map;
670683
U64 hash = e_hash_from_string(5381, string);
671684
U64 slot_idx = hash%map->slots_count;
672685
for(E_IRGenRuleNode *n = map->slots[slot_idx].first; n != 0; n = n->next)
@@ -724,10 +737,10 @@ e_auto_hook_tag_exprs_from_type_key(Arena *arena, E_TypeKey type_key)
724737
{
725738
ProfBeginFunction();
726739
E_ExprList exprs = {0};
727-
if(e_ir_ctx != 0)
740+
if(e_ir_state != 0 && e_ir_state->ctx != 0)
728741
{
729742
Temp scratch = scratch_begin(&arena, 1);
730-
E_AutoHookMap *map = e_ir_ctx->auto_hook_map;
743+
E_AutoHookMap *map = e_ir_state->ctx->auto_hook_map;
731744

732745
//- rjf: gather exact-type-key-matches from the map
733746
if(map != 0 && map->slots_count != 0)
@@ -782,12 +795,12 @@ internal E_ExprList
782795
e_auto_hook_tag_exprs_from_type_key__cached(E_TypeKey type_key)
783796
{
784797
E_ExprList exprs = {0};
785-
if(e_ir_ctx != 0 && e_ir_ctx->type_auto_hook_cache_map != 0 && e_ir_ctx->type_auto_hook_cache_map->slots_count != 0)
798+
if(e_ir_state != 0 && e_ir_state->ctx != 0 && e_ir_state->type_auto_hook_cache_map != 0 && e_ir_state->type_auto_hook_cache_map->slots_count != 0)
786799
{
787800
U64 hash = e_hash_from_string(5381, str8_struct(&type_key));
788-
U64 slot_idx = hash%e_ir_ctx->type_auto_hook_cache_map->slots_count;
801+
U64 slot_idx = hash%e_ir_state->type_auto_hook_cache_map->slots_count;
789802
E_TypeAutoHookCacheNode *node = 0;
790-
for(E_TypeAutoHookCacheNode *n = e_ir_ctx->type_auto_hook_cache_map->slots[slot_idx].first;
803+
for(E_TypeAutoHookCacheNode *n = e_ir_state->type_auto_hook_cache_map->slots[slot_idx].first;
791804
n != 0;
792805
n = n->next)
793806
{
@@ -798,9 +811,8 @@ e_auto_hook_tag_exprs_from_type_key__cached(E_TypeKey type_key)
798811
}
799812
if(node == 0)
800813
{
801-
// TODO(rjf): @cfg hack!!! should not be using this arena...
802-
node = push_array(e_type_state->arena, E_TypeAutoHookCacheNode, 1);
803-
SLLQueuePush(e_ir_ctx->type_auto_hook_cache_map->slots[slot_idx].first, e_ir_ctx->type_auto_hook_cache_map->slots[slot_idx].last, node);
814+
node = push_array(e_ir_state->arena, E_TypeAutoHookCacheNode, 1);
815+
SLLQueuePush(e_ir_state->type_auto_hook_cache_map->slots[slot_idx].first, e_ir_state->type_auto_hook_cache_map->slots[slot_idx].last, node);
804816
node->key = type_key;
805817
node->exprs = e_auto_hook_tag_exprs_from_type_key(e_type_state->arena, type_key);
806818
}
@@ -1807,16 +1819,16 @@ E_IRGEN_FUNCTION_DEF(default)
18071819
//- rjf: leaf identifiers
18081820
case E_ExprKind_LeafIdent:
18091821
{
1810-
E_Expr *macro_expr = e_string2expr_lookup(e_ir_ctx->macro_map, expr->string);
1822+
E_Expr *macro_expr = e_string2expr_lookup(e_ir_state->ctx->macro_map, expr->string);
18111823
if(macro_expr == &e_expr_nil)
18121824
{
18131825
e_msgf(arena, &result.msgs, E_MsgKind_ResolutionFailure, expr->location, "`%S` could not be found.", expr->string);
18141826
}
18151827
else
18161828
{
1817-
e_string2expr_map_inc_poison(e_ir_ctx->macro_map, expr->string);
1829+
e_string2expr_map_inc_poison(e_ir_state->ctx->macro_map, expr->string);
18181830
result = e_irtree_and_type_from_expr(arena, macro_expr);
1819-
e_string2expr_map_dec_poison(e_ir_ctx->macro_map, expr->string);
1831+
e_string2expr_map_dec_poison(e_ir_state->ctx->macro_map, expr->string);
18201832
}
18211833
}break;
18221834

@@ -1946,8 +1958,8 @@ e_irtree_and_type_from_expr(Arena *arena, E_Expr *expr)
19461958
{
19471959
B32 tag_is_poisoned = 0;
19481960
U64 hash = e_hash_from_string(5381, str8_struct(&tag));
1949-
U64 slot_idx = hash%e_ir_ctx->used_tag_map->slots_count;
1950-
for(E_UsedTagNode *n = e_ir_ctx->used_tag_map->slots[slot_idx].first; n != 0; n = n->next)
1961+
U64 slot_idx = hash%e_ir_state->used_tag_map->slots_count;
1962+
for(E_UsedTagNode *n = e_ir_state->used_tag_map->slots[slot_idx].first; n != 0; n = n->next)
19511963
{
19521964
if(n->tag == tag)
19531965
{
@@ -1981,10 +1993,10 @@ e_irtree_and_type_from_expr(Arena *arena, E_Expr *expr)
19811993
if(t->tag != &e_expr_nil)
19821994
{
19831995
U64 hash = e_hash_from_string(5381, str8_struct(&t->tag));
1984-
U64 slot_idx = hash%e_ir_ctx->used_tag_map->slots_count;
1996+
U64 slot_idx = hash%e_ir_state->used_tag_map->slots_count;
19851997
E_UsedTagNode *n = push_array(arena, E_UsedTagNode, 1);
19861998
n->tag = t->tag;
1987-
DLLPushBack(e_ir_ctx->used_tag_map->slots[slot_idx].first, e_ir_ctx->used_tag_map->slots[slot_idx].last, n);
1999+
DLLPushBack(e_ir_state->used_tag_map->slots[slot_idx].first, e_ir_state->used_tag_map->slots[slot_idx].last, n);
19882000
}
19892001

19902002
// rjf: do this rule's generation
@@ -2002,8 +2014,8 @@ e_irtree_and_type_from_expr(Arena *arena, E_Expr *expr)
20022014
{
20032015
B32 tag_is_poisoned = 0;
20042016
U64 hash = e_hash_from_string(5381, str8_struct(&tag));
2005-
U64 slot_idx = hash%e_ir_ctx->used_tag_map->slots_count;
2006-
for(E_UsedTagNode *n = e_ir_ctx->used_tag_map->slots[slot_idx].first; n != 0; n = n->next)
2017+
U64 slot_idx = hash%e_ir_state->used_tag_map->slots_count;
2018+
for(E_UsedTagNode *n = e_ir_state->used_tag_map->slots[slot_idx].first; n != 0; n = n->next)
20072019
{
20082020
if(n->tag == tag)
20092021
{
@@ -2034,12 +2046,12 @@ e_irtree_and_type_from_expr(Arena *arena, E_Expr *expr)
20342046
if(t->tag != &e_expr_nil)
20352047
{
20362048
U64 hash = e_hash_from_string(5381, str8_struct(&t->tag));
2037-
U64 slot_idx = hash%e_ir_ctx->used_tag_map->slots_count;
2038-
for(E_UsedTagNode *n = e_ir_ctx->used_tag_map->slots[slot_idx].first; n != 0; n = n->next)
2049+
U64 slot_idx = hash%e_ir_state->used_tag_map->slots_count;
2050+
for(E_UsedTagNode *n = e_ir_state->used_tag_map->slots[slot_idx].first; n != 0; n = n->next)
20392051
{
20402052
if(n->tag == t->tag)
20412053
{
2042-
DLLRemove(e_ir_ctx->used_tag_map->slots[slot_idx].first, e_ir_ctx->used_tag_map->slots[slot_idx].last, n);
2054+
DLLRemove(e_ir_state->used_tag_map->slots[slot_idx].first, e_ir_state->used_tag_map->slots[slot_idx].last, n);
20432055
break;
20442056
}
20452057
}

src/eval/eval_ir.h

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ struct E_TypeAutoHookCacheMap
252252
};
253253

254254
////////////////////////////////
255-
//~ rjf: Parse Context
255+
//~ rjf: IR Context
256256

257257
typedef struct E_IRCtx E_IRCtx;
258258
struct E_IRCtx
@@ -261,6 +261,21 @@ struct E_IRCtx
261261
E_LookupRuleMap *lookup_rule_map;
262262
E_IRGenRuleMap *irgen_rule_map;
263263
E_AutoHookMap *auto_hook_map;
264+
};
265+
266+
////////////////////////////////
267+
//~ rjf: IR State
268+
269+
typedef struct E_IRState E_IRState;
270+
struct E_IRState
271+
{
272+
Arena *arena;
273+
U64 arena_eval_start_pos;
274+
275+
// rjf: ir context
276+
E_IRCtx *ctx;
277+
278+
// rjf: caches
264279
E_UsedTagMap *used_tag_map;
265280
E_TypeAutoHookCacheMap *type_auto_hook_cache_map;
266281
};
@@ -292,7 +307,7 @@ local_persist read_only E_IRGenRule e_irgen_rule__default =
292307
E_IRGEN_FUNCTION_NAME(default),
293308
};
294309
global read_only E_IRNode e_irnode_nil = {&e_irnode_nil, &e_irnode_nil, &e_irnode_nil};
295-
thread_static E_IRCtx *e_ir_ctx = 0;
310+
thread_static E_IRState *e_ir_state = 0;
296311

297312
////////////////////////////////
298313
//~ rjf: Expr Kind Enum Functions

src/raddbg/raddbg_core.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12805,7 +12805,7 @@ rd_frame(void)
1280512805
//
1280612806
E_TypeKey meta_eval_type_key = e_type_key_cons_base(type(CTRL_MetaEval));
1280712807
E_IRCtx *ir_ctx = push_array(scratch.arena, E_IRCtx, 1);
12808-
e_ir_ctx = 0;
12808+
if(e_ir_state != 0) { e_ir_state->ctx = 0; }
1280912809
{
1281012810
E_IRCtx *ctx = ir_ctx;
1281112811
ctx->macro_map = push_array(scratch.arena, E_String2ExprMap, 1);
@@ -12814,12 +12814,6 @@ rd_frame(void)
1281412814
ctx->lookup_rule_map[0] = e_lookup_rule_map_make(scratch.arena, 512);
1281512815
ctx->auto_hook_map = push_array(scratch.arena, E_AutoHookMap, 1);
1281612816
ctx->auto_hook_map[0] = e_auto_hook_map_make(scratch.arena, 512);
12817-
ctx->used_tag_map = push_array(scratch.arena, E_UsedTagMap, 1);
12818-
ctx->used_tag_map->slots_count = 64;
12819-
ctx->used_tag_map->slots = push_array(scratch.arena, E_UsedTagSlot, ctx->used_tag_map->slots_count);
12820-
ctx->type_auto_hook_cache_map = push_array(scratch.arena, E_TypeAutoHookCacheMap, 1);
12821-
ctx->type_auto_hook_cache_map->slots_count = 256;
12822-
ctx->type_auto_hook_cache_map->slots = push_array(scratch.arena, E_TypeAutoHookCacheSlot, ctx->type_auto_hook_cache_map->slots_count);
1282312817

1282412818
//- rjf: choose set of evallable meta names
1282512819
String8 evallable_meta_names[] =
@@ -16252,7 +16246,7 @@ X(getting_started)
1625216246
{
1625316247
if(t->expr->kind == E_ExprKind_LeafIdent)
1625416248
{
16255-
E_Expr *macro_expr = e_string2expr_lookup(e_ir_ctx->macro_map, t->expr->string);
16249+
E_Expr *macro_expr = e_string2expr_lookup(e_ir_state->ctx->macro_map, t->expr->string);
1625616250
if(macro_expr != &e_expr_nil)
1625716251
{
1625816252
E_Eval eval = e_eval_from_expr(scratch.arena, macro_expr);

0 commit comments

Comments
 (0)