Skip to content

Commit

Permalink
Reimplement fixed size blocks array with linked list
Browse files Browse the repository at this point in the history
  • Loading branch information
ChAoSUnItY committed Jan 15, 2025
1 parent 95a55e7 commit 0434527
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
3 changes: 1 addition & 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,7 +226,7 @@ struct block {
func_t *func;
macro_t *macro;
int locals_size;
int index;
struct block *next;
};

typedef struct block block_t;
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,8 @@

/* Global objects */

block_t *BLOCKS;
int blocks_idx = 0;
block_t *BLOCKS_HEAD;
block_t *BLOCKS_TAIL;

macro_t *MACROS;
int macros_idx = 0;
Expand Down Expand Up @@ -226,8 +226,15 @@ 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;
if (!BLOCKS_TAIL) {
BLOCKS_TAIL = BLOCKS_HEAD;
blk = BLOCKS_TAIL;
} else {
blk = malloc(sizeof(block_t));
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 = malloc(sizeof(block_t));
BLOCKS_TAIL = BLOCKS_HEAD;
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_blk = BLOCKS_TAIL->next;
free(BLOCKS_HEAD);
BLOCKS_HEAD = next_blk;
}
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 0434527

Please sign in to comment.