Skip to content

Replace ALIASES and CONSTANTS with hashmap #194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 1, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 31 additions & 27 deletions src/globals.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ int macros_idx = 0;
* we have to initially create large amount of buckets.
*/
hashmap_t *FUNCS_MAP;
hashmap_t *ALIASES_MAP;
hashmap_t *CONSTANTS_MAP;

type_t *TYPES;
int types_idx = 0;
Expand Down Expand Up @@ -66,12 +68,6 @@ int elf_offset = 0;

regfile_t REGS[REG_CNT];

alias_t *ALIASES;
int aliases_idx = 0;

constant_t *CONSTANTS;
int constants_idx = 0;

source_t *SOURCE;

hashmap_t *INCLUSION_MAP;
Expand Down Expand Up @@ -563,28 +559,34 @@ block_t *add_block(block_t *parent, func_t *func, macro_t *macro)

void add_alias(char *alias, char *value)
{
alias_t *al = &ALIASES[aliases_idx++];
strcpy(al->alias, alias);
alias_t *al = hashmap_get(ALIASES_MAP, alias);
if (!al) {
al = malloc(sizeof(alias_t));
if (!al) {
printf("Failed to allocate alias_t\n");
return;
}
strcpy(al->alias, alias);
hashmap_put(ALIASES_MAP, alias, al);
}
strcpy(al->value, value);
al->disabled = false;
}

char *find_alias(char alias[])
{
for (int i = 0; i < aliases_idx; i++) {
if (!ALIASES[i].disabled && !strcmp(alias, ALIASES[i].alias))
return ALIASES[i].value;
}
alias_t *al = hashmap_get(ALIASES_MAP, alias);
if (al && !al->disabled)
return al->value;
return NULL;
}

bool remove_alias(char *alias)
{
for (int i = 0; i < aliases_idx; i++) {
if (!ALIASES[i].disabled && !strcmp(alias, ALIASES[i].alias)) {
ALIASES[i].disabled = true;
return true;
}
alias_t *al = hashmap_get(ALIASES_MAP, alias);
if (al && !al->disabled) {
al->disabled = true;
return true;
}
return false;
}
Expand Down Expand Up @@ -669,18 +671,20 @@ type_t *add_named_type(char *name)

void add_constant(char alias[], int value)
{
constant_t *constant = &CONSTANTS[constants_idx++];
constant_t *constant = malloc(sizeof(constant_t));
if (!constant) {
printf("Failed to allocate constant_t\n");
return;
}

strcpy(constant->alias, alias);
constant->value = value;
hashmap_put(CONSTANTS_MAP, alias, constant);
}

constant_t *find_constant(char alias[])
{
for (int i = 0; i < constants_idx; i++) {
if (!strcmp(CONSTANTS[i].alias, alias))
return &CONSTANTS[i];
}
return NULL;
return hashmap_get(CONSTANTS_MAP, alias);
}

func_t *find_func(char func_name[])
Expand Down Expand Up @@ -1006,8 +1010,8 @@ void global_init()
LABEL_LUT = malloc(MAX_LABEL * sizeof(label_lut_t));
SOURCE = source_create(MAX_SOURCE);
INCLUSION_MAP = hashmap_create(MAX_INCLUSIONS);
ALIASES = malloc(MAX_ALIASES * sizeof(alias_t));
CONSTANTS = malloc(MAX_CONSTANTS * sizeof(constant_t));
ALIASES_MAP = hashmap_create(MAX_ALIASES);
CONSTANTS_MAP = hashmap_create(MAX_CONSTANTS);

elf_code = malloc(MAX_CODE);
elf_data = malloc(MAX_DATA);
Expand Down Expand Up @@ -1039,8 +1043,8 @@ void global_release()
free(LABEL_LUT);
source_free(SOURCE);
hashmap_free(INCLUSION_MAP);
free(ALIASES);
free(CONSTANTS);
hashmap_free(ALIASES_MAP);
hashmap_free(CONSTANTS_MAP);

free(elf_code);
free(elf_data);
Expand Down