@@ -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,34 @@ 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
+ if (!al ) {
566
+ printf ("Failed to allocate alias_t\n" );
567
+ return ;
568
+ }
569
+ strcpy (al -> alias , alias );
570
+ hashmap_put (ALIASES_MAP , alias , al );
571
+ }
568
572
strcpy (al -> value , value );
569
573
al -> disabled = false;
570
574
}
571
575
572
576
char * find_alias (char alias [])
573
577
{
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
- }
578
+ alias_t * al = hashmap_get (ALIASES_MAP , alias );
579
+ if (al && !al -> disabled )
580
+ return al -> value ;
578
581
return NULL ;
579
582
}
580
583
581
584
bool remove_alias (char * alias )
582
585
{
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
- }
586
+ alias_t * al = hashmap_get (ALIASES_MAP , alias );
587
+ if (al && !al -> disabled ) {
588
+ al -> disabled = true;
589
+ return true;
588
590
}
589
591
return false;
590
592
}
@@ -669,18 +671,20 @@ type_t *add_named_type(char *name)
669
671
670
672
void add_constant (char alias [], int value )
671
673
{
672
- constant_t * constant = & CONSTANTS [constants_idx ++ ];
674
+ constant_t * constant = malloc (sizeof (constant_t ));
675
+ if (!constant ) {
676
+ printf ("Failed to allocate constant_t\n" );
677
+ return ;
678
+ }
679
+
673
680
strcpy (constant -> alias , alias );
674
681
constant -> value = value ;
682
+ hashmap_put (CONSTANTS_MAP , alias , constant );
675
683
}
676
684
677
685
constant_t * find_constant (char alias [])
678
686
{
679
- for (int i = 0 ; i < constants_idx ; i ++ ) {
680
- if (!strcmp (CONSTANTS [i ].alias , alias ))
681
- return & CONSTANTS [i ];
682
- }
683
- return NULL ;
687
+ return hashmap_get (CONSTANTS_MAP , alias );
684
688
}
685
689
686
690
func_t * find_func (char func_name [])
@@ -1006,8 +1010,8 @@ void global_init()
1006
1010
LABEL_LUT = malloc (MAX_LABEL * sizeof (label_lut_t ));
1007
1011
SOURCE = source_create (MAX_SOURCE );
1008
1012
INCLUSION_MAP = hashmap_create (MAX_INCLUSIONS );
1009
- ALIASES = malloc (MAX_ALIASES * sizeof ( alias_t ) );
1010
- CONSTANTS = malloc (MAX_CONSTANTS * sizeof ( constant_t ) );
1013
+ ALIASES_MAP = hashmap_create (MAX_ALIASES );
1014
+ CONSTANTS_MAP = hashmap_create (MAX_CONSTANTS );
1011
1015
1012
1016
elf_code = malloc (MAX_CODE );
1013
1017
elf_data = malloc (MAX_DATA );
@@ -1039,8 +1043,8 @@ void global_release()
1039
1043
free (LABEL_LUT );
1040
1044
source_free (SOURCE );
1041
1045
hashmap_free (INCLUSION_MAP );
1042
- free ( ALIASES );
1043
- free ( CONSTANTS );
1046
+ hashmap_free ( ALIASES_MAP );
1047
+ hashmap_free ( CONSTANTS_MAP );
1044
1048
1045
1049
free (elf_code );
1046
1050
free (elf_data );
0 commit comments