@@ -38,6 +38,8 @@ int macros_idx = 0;
38
38
* we have to initially create large amount of buckets.
39
39
*/
40
40
hashmap_t * FUNCS_MAP ;
41
+ hashmap_t * ALIASES_MAP ;
42
+ hashmap_t * CONSTANTS_MAP ;
41
43
42
44
type_t * TYPES ;
43
45
int types_idx = 0 ;
@@ -66,12 +68,6 @@ int elf_offset = 0;
66
68
67
69
regfile_t REGS [REG_CNT ];
68
70
69
- alias_t * ALIASES ;
70
- int aliases_idx = 0 ;
71
-
72
- constant_t * CONSTANTS ;
73
- int constants_idx = 0 ;
74
-
75
71
source_t * SOURCE ;
76
72
77
73
hashmap_t * INCLUSION_MAP ;
@@ -563,28 +559,30 @@ block_t *add_block(block_t *parent, func_t *func, macro_t *macro)
563
559
564
560
void add_alias (char * alias , char * value )
565
561
{
566
- alias_t * al = & ALIASES [aliases_idx ++ ];
567
- strcpy (al -> alias , alias );
562
+ alias_t * al = hashmap_get (ALIASES_MAP , alias );
563
+ if (!al ) {
564
+ al = malloc (sizeof (alias_t ));
565
+ strcpy (al -> alias , alias );
566
+ hashmap_put (ALIASES_MAP , alias , al );
567
+ }
568
568
strcpy (al -> value , value );
569
569
al -> disabled = false;
570
570
}
571
571
572
572
char * find_alias (char alias [])
573
573
{
574
- for (int i = 0 ; i < aliases_idx ; i ++ ) {
575
- if (!ALIASES [i ].disabled && !strcmp (alias , ALIASES [i ].alias ))
576
- return ALIASES [i ].value ;
577
- }
574
+ alias_t * al = hashmap_get (ALIASES_MAP , alias );
575
+ if (al && !al -> disabled )
576
+ return al -> value ;
578
577
return NULL ;
579
578
}
580
579
581
580
bool remove_alias (char * alias )
582
581
{
583
- for (int i = 0 ; i < aliases_idx ; i ++ ) {
584
- if (!ALIASES [i ].disabled && !strcmp (alias , ALIASES [i ].alias )) {
585
- ALIASES [i ].disabled = true;
586
- return true;
587
- }
582
+ alias_t * al = hashmap_get (ALIASES_MAP , alias );
583
+ if (al && !al -> disabled ) {
584
+ al -> disabled = true;
585
+ return true;
588
586
}
589
587
return false;
590
588
}
@@ -669,18 +667,20 @@ type_t *add_named_type(char *name)
669
667
670
668
void add_constant (char alias [], int value )
671
669
{
672
- constant_t * constant = & CONSTANTS [constants_idx ++ ];
670
+ constant_t * constant = malloc (sizeof (constant_t ));
671
+ if (!constant ) {
672
+ printf ("Failed to allocate constant_t\n" );
673
+ return ;
674
+ }
675
+
673
676
strcpy (constant -> alias , alias );
674
677
constant -> value = value ;
678
+ hashmap_put (CONSTANTS_MAP , alias , constant );
675
679
}
676
680
677
681
constant_t * find_constant (char alias [])
678
682
{
679
- for (int i = 0 ; i < constants_idx ; i ++ ) {
680
- if (!strcmp (CONSTANTS [i ].alias , alias ))
681
- return & CONSTANTS [i ];
682
- }
683
- return NULL ;
683
+ return hashmap_get (CONSTANTS_MAP , alias );
684
684
}
685
685
686
686
func_t * find_func (char func_name [])
@@ -987,8 +987,8 @@ void global_init()
987
987
LABEL_LUT = malloc (MAX_LABEL * sizeof (label_lut_t ));
988
988
SOURCE = create_source (MAX_SOURCE );
989
989
INCLUSION_MAP = hashmap_create (MAX_INCLUSIONS );
990
- ALIASES = malloc (MAX_ALIASES * sizeof ( alias_t ) );
991
- CONSTANTS = malloc (MAX_CONSTANTS * sizeof ( constant_t ) );
990
+ ALIASES_MAP = hashmap_create (MAX_ALIASES );
991
+ CONSTANTS_MAP = hashmap_create (MAX_CONSTANTS );
992
992
993
993
elf_code = malloc (MAX_CODE );
994
994
elf_data = malloc (MAX_DATA );
@@ -1020,8 +1020,8 @@ void global_release()
1020
1020
free (LABEL_LUT );
1021
1021
source_release (SOURCE );
1022
1022
hashmap_free (INCLUSION_MAP );
1023
- free ( ALIASES );
1024
- free ( CONSTANTS );
1023
+ hashmap_free ( ALIASES_MAP );
1024
+ hashmap_free ( CONSTANTS_MAP );
1025
1025
1026
1026
free (elf_code );
1027
1027
free (elf_data );
0 commit comments