Skip to content

Commit

Permalink
Merge pull request #176 from ChAoSUnItY/refactor/reduce-blocks-alloc
Browse files Browse the repository at this point in the history
Replace fixed size blocks with linked list
  • Loading branch information
jserv authored Jan 16, 2025
2 parents 95a55e7 + e22f595 commit cfcc553
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
8 changes: 6 additions & 2 deletions src/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#define MAX_FIELDS 64
#define MAX_FUNCS 512
#define MAX_FUNC_TRIES 2160
#define MAX_BLOCKS 2048
#define MAX_TYPES 64
#define MAX_IR_INSTR 50000
#define MAX_BB_PRED 128
Expand Down Expand Up @@ -227,11 +226,16 @@ struct block {
func_t *func;
macro_t *macro;
int locals_size;
int index;
struct block *next;
};

typedef struct block block_t;

typedef struct {
block_t *head;
block_t *tail;
} block_list_t;

/* phase-1 IR definition */
typedef struct {
opcode_t op;
Expand Down
26 changes: 19 additions & 7 deletions src/globals.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@

/* Global objects */

block_t *BLOCKS;
int blocks_idx = 0;
block_list_t BLOCKS;

macro_t *MACROS;
int macros_idx = 0;
Expand Down Expand Up @@ -226,8 +225,16 @@ int find_label_offset(char name[])

block_t *add_block(block_t *parent, func_t *func, macro_t *macro)
{
block_t *blk = &BLOCKS[blocks_idx];
blk->index = blocks_idx++;
block_t *blk = malloc(sizeof(block_t));

if (!BLOCKS.head) {
BLOCKS.head = blk;
BLOCKS.tail = BLOCKS.head;
} else {
BLOCKS.tail->next = blk;
BLOCKS.tail = blk;
}

blk->parent = parent;
blk->func = func;
blk->macro = macro;
Expand Down Expand Up @@ -394,7 +401,7 @@ var_t *find_local_var(char *token, block_t *block)

var_t *find_global_var(char *token)
{
block_t *block = &BLOCKS[0];
block_t *block = BLOCKS.head;

for (int i = 0; i < block->next_local; i++) {
if (!strcmp(block->locals[i].var_name, token))
Expand Down Expand Up @@ -587,7 +594,8 @@ void global_init()
{
elf_code_start = ELF_START + elf_header_len;

BLOCKS = malloc(MAX_BLOCKS * sizeof(block_t));
BLOCKS.head = NULL;
BLOCKS.tail = NULL;
MACROS = malloc(MAX_ALIASES * sizeof(macro_t));
FUNCS = malloc(MAX_FUNCS * sizeof(func_t));
FUNC_TRIES = malloc(MAX_FUNC_TRIES * sizeof(trie_t));
Expand All @@ -613,7 +621,11 @@ void global_init()

void global_release()
{
free(BLOCKS);
while (BLOCKS.head) {
block_t *next = BLOCKS.head->next;
free(BLOCKS.head);
BLOCKS.head = next;
}
free(MACROS);
free(FUNCS);
free(FUNC_TRIES);
Expand Down
4 changes: 2 additions & 2 deletions src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -2153,7 +2153,7 @@ bool read_global_assignment(char *token)
{
ph1_ir_t *ph1_ir;
var_t *vd;
block_t *parent = &BLOCKS[0];
block_t *parent = BLOCKS.head;

/* global initialization must be constant */
var_t *var = find_global_var(token);
Expand Down Expand Up @@ -3182,7 +3182,7 @@ void read_global_decl(block_t *block)
void read_global_statement()
{
char token[MAX_ID_LEN];
block_t *block = &BLOCKS[0]; /* global block */
block_t *block = BLOCKS.head; /* global block */

if (lex_accept(T_struct)) {
int i = 0, size = 0;
Expand Down

0 comments on commit cfcc553

Please sign in to comment.