Skip to content

Commit 2d8053f

Browse files
committed
Refactor local variable storage
1 parent e2dac50 commit 2d8053f

File tree

4 files changed

+48
-56
lines changed

4 files changed

+48
-56
lines changed

src/defs.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,12 @@ typedef struct use_chain_node {
279279
typedef struct var var_t;
280280
typedef struct type type_t;
281281

282+
typedef struct var_list {
283+
int capacity;
284+
int size;
285+
var_t **elements;
286+
} var_list_t;
287+
282288
struct var {
283289
char type_name[MAX_TYPE_LEN];
284290
type_t *type;
@@ -321,21 +327,14 @@ typedef struct func func_t;
321327

322328
/* block definition */
323329
struct block {
324-
var_t locals[MAX_LOCALS];
325-
int next_local;
330+
var_list_t locals;
326331
struct block *parent;
327332
func_t *func;
328333
macro_t *macro;
329334
struct block *next;
330335
};
331336

332337
typedef struct block block_t;
333-
334-
typedef struct {
335-
block_t *head;
336-
block_t *tail;
337-
} block_list_t;
338-
339338
typedef struct basic_block basic_block_t;
340339

341340
/* Definition of a growable buffer for a mutable null-terminated string

src/globals.c

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ int macro_return_idx;
2828

2929
/* Global objects */
3030

31-
block_list_t BLOCKS;
32-
3331
macro_t *MACROS;
3432
int macros_idx = 0;
3533

@@ -46,6 +44,9 @@ int types_idx = 0;
4644

4745
arena_t *INSN_ARENA;
4846

47+
/* BLOCK_ARENA is responsible for block_t / var_t allocation */
48+
arena_t *BLOCK_ARENA;
49+
4950
/* BB_ARENA is responsible for basic_block_t / ph2_ir_t allocation */
5051
arena_t *BB_ARENA;
5152

@@ -56,6 +57,7 @@ int ph2_ir_idx = 0;
5657

5758
func_list_t FUNC_LIST;
5859
func_t *GLOBAL_FUNC;
60+
block_t *GLOBAL_BLOCK;
5961
basic_block_t *MAIN_BB;
6062
int elf_offset = 0;
6163

@@ -499,20 +501,14 @@ void set_var_liveout(var_t *var, int end)
499501

500502
block_t *add_block(block_t *parent, func_t *func, macro_t *macro)
501503
{
502-
block_t *blk = malloc(sizeof(block_t));
503-
504-
if (!BLOCKS.head) {
505-
BLOCKS.head = blk;
506-
BLOCKS.tail = BLOCKS.head;
507-
} else {
508-
BLOCKS.tail->next = blk;
509-
BLOCKS.tail = blk;
510-
}
504+
block_t *blk = arena_alloc(BLOCK_ARENA, sizeof(block_t));
511505

512506
blk->parent = parent;
513507
blk->func = func;
514508
blk->macro = macro;
515-
blk->next_local = 0;
509+
blk->locals.capacity = 16;
510+
blk->locals.elements =
511+
arena_alloc(BLOCK_ARENA, blk->locals.capacity * sizeof(var_t *));
516512
return blk;
517513
}
518514

@@ -645,9 +641,10 @@ var_t *find_local_var(char *token, block_t *block)
645641
func_t *func = block->func;
646642

647643
for (; block; block = block->parent) {
648-
for (int i = 0; i < block->next_local; i++) {
649-
if (!strcmp(block->locals[i].var_name, token))
650-
return &block->locals[i];
644+
var_list_t *var_list = &block->locals;
645+
for (int i = 0; i < var_list->size; i++) {
646+
if (!strcmp(var_list->elements[i]->var_name, token))
647+
return var_list->elements[i];
651648
}
652649
}
653650

@@ -662,11 +659,11 @@ var_t *find_local_var(char *token, block_t *block)
662659

663660
var_t *find_global_var(char *token)
664661
{
665-
block_t *block = BLOCKS.head;
662+
var_list_t *var_list = &GLOBAL_BLOCK->locals;
666663

667-
for (int i = 0; i < block->next_local; i++) {
668-
if (!strcmp(block->locals[i].var_name, token))
669-
return &block->locals[i];
664+
for (int i = 0; i < var_list->size; i++) {
665+
if (!strcmp(var_list->elements[i]->var_name, token))
666+
return var_list->elements[i];
670667
}
671668
return NULL;
672669
}
@@ -970,16 +967,14 @@ void global_init()
970967
{
971968
elf_code_start = ELF_START + elf_header_len;
972969

973-
BLOCKS.head = NULL;
974-
BLOCKS.tail = NULL;
975-
976970
MACROS = malloc(MAX_ALIASES * sizeof(macro_t));
977-
FUNC_MAP = hashmap_create(DEFAULT_FUNCS_SIZE);
978971
TYPES = malloc(MAX_TYPES * sizeof(type_t));
972+
BLOCK_ARENA = arena_init(DEFAULT_ARENA_SIZE);
979973
INSN_ARENA = arena_init(DEFAULT_ARENA_SIZE);
980974
BB_ARENA = arena_init(DEFAULT_ARENA_SIZE);
981975
PH2_IR_FLATTEN = malloc(MAX_IR_INSTR * sizeof(ph2_ir_t *));
982976
SOURCE = strbuf_create(MAX_SOURCE);
977+
FUNC_MAP = hashmap_create(DEFAULT_FUNCS_SIZE);
983978
INCLUSION_MAP = hashmap_create(DEFAULT_INCLUSIONS_SIZE);
984979
ALIASES_MAP = hashmap_create(MAX_ALIASES);
985980
CONSTANTS_MAP = hashmap_create(MAX_CONSTANTS);
@@ -994,19 +989,14 @@ void global_init()
994989

995990
void global_release()
996991
{
997-
while (BLOCKS.head) {
998-
block_t *next = BLOCKS.head->next;
999-
printf("%d\n", BLOCKS.head->next_local);
1000-
free(BLOCKS.head);
1001-
BLOCKS.head = next;
1002-
}
1003992
free(MACROS);
1004-
hashmap_free(FUNC_MAP);
1005993
free(TYPES);
994+
arena_free(BLOCK_ARENA);
1006995
arena_free(INSN_ARENA);
1007996
arena_free(BB_ARENA);
1008997
free(PH2_IR_FLATTEN);
1009998
strbuf_free(SOURCE);
999+
hashmap_free(FUNC_MAP);
10101000
hashmap_free(INCLUSION_MAP);
10111001
hashmap_free(ALIASES_MAP);
10121002
hashmap_free(CONSTANTS_MAP);

src/parser.c

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,26 +38,30 @@ char *gen_name_to(char *buf)
3838

3939
var_t *require_var(block_t *blk)
4040
{
41-
if (blk->next_local >= MAX_LOCALS)
42-
error("Too many locals");
41+
var_list_t *var_list = &blk->locals;
4342

44-
var_t *var = &blk->locals[blk->next_local++];
43+
if (var_list->size >= var_list->capacity) {
44+
var_list->capacity *= 2;
45+
46+
var_t **new_locals = calloc(var_list->capacity, sizeof(var_t *));
47+
memcpy(new_locals, var_list->elements,
48+
var_list->size * sizeof(var_t *));
49+
var_list->elements = new_locals;
50+
}
51+
52+
var_t *var = arena_alloc(BLOCK_ARENA, sizeof(var_t));
53+
var_list->elements[var_list->size++] = var;
4554
var->consumed = -1;
4655
var->base = var;
4756
return var;
4857
}
4958

5059
var_t *require_var_t(block_t *blk, type_t *type)
5160
{
52-
if (blk->next_local >= MAX_LOCALS)
53-
error("Too many locals");
54-
5561
if (!type)
5662
error("Type must not be NULL");
57-
58-
var_t *var = &blk->locals[blk->next_local++];
59-
var->consumed = -1;
60-
var->base = var;
63+
64+
var_t *var = require_var(blk);
6165
var->type = type;
6266
return var;
6367
}
@@ -1924,7 +1928,7 @@ void eval_ternary_imm(int cond, char *token)
19241928
bool read_global_assignment(char *token)
19251929
{
19261930
var_t *vd, *rs1, *var;
1927-
block_t *parent = BLOCKS.head;
1931+
block_t *parent = GLOBAL_BLOCK;
19281932

19291933
/* global initialization must be constant */
19301934
var = find_global_var(token);
@@ -2648,7 +2652,7 @@ void read_global_decl(block_t *block)
26482652
/* function */
26492653
func_t *func = add_func(var->var_name, false);
26502654
memcpy(&func->return_def, var, sizeof(var_t));
2651-
block->next_local--;
2655+
block->locals.size--;
26522656

26532657
read_parameter_list_decl(func, 0);
26542658

@@ -2684,7 +2688,7 @@ void read_global_decl(block_t *block)
26842688
void read_global_statement()
26852689
{
26862690
char token[MAX_ID_LEN];
2687-
block_t *block = BLOCKS.head; /* global block */
2691+
block_t *block = GLOBAL_BLOCK; /* global block */
26882692

26892693
if (lex_accept(T_struct)) {
26902694
int i = 0, size = 0;
@@ -2827,8 +2831,8 @@ void parse_internal()
28272831
type->base_type = TYPE_char;
28282832
type->size = 1;
28292833

2830-
add_block(NULL, NULL, NULL); /* global block */
2831-
elf_add_symbol("", 0); /* undef symbol */
2834+
GLOBAL_BLOCK = add_block(NULL, NULL, NULL); /* global block */
2835+
elf_add_symbol("", 0); /* undef symbol */
28322836

28332837
/* architecture defines */
28342838
add_alias(ARCH_PREDEFINED, "1");

src/ssa.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -566,9 +566,8 @@ bool var_check_in_scope(var_t *var, block_t *block)
566566
func_t *func = block->func;
567567

568568
while (block) {
569-
for (int i = 0; i < block->next_local; i++) {
570-
var_t *locals = block->locals;
571-
if (var == &locals[i])
569+
for (int i = 0; i < block->locals.capacity; i++) {
570+
if (var == block->locals.elements[i])
572571
return true;
573572
}
574573
block = block->parent;

0 commit comments

Comments
 (0)