@@ -28,8 +28,6 @@ int macro_return_idx;
28
28
29
29
/* Global objects */
30
30
31
- block_list_t BLOCKS ;
32
-
33
31
macro_t * MACROS ;
34
32
int macros_idx = 0 ;
35
33
@@ -41,11 +39,23 @@ hashmap_t *FUNC_MAP;
41
39
hashmap_t * ALIASES_MAP ;
42
40
hashmap_t * CONSTANTS_MAP ;
43
41
42
+ /* Types */
43
+
44
44
type_t * TYPES ;
45
45
int types_idx = 0 ;
46
46
47
+ type_t * TY_void ;
48
+ type_t * TY_char ;
49
+ type_t * TY_bool ;
50
+ type_t * TY_int ;
51
+
52
+ /* Arenas */
53
+
47
54
arena_t * INSN_ARENA ;
48
55
56
+ /* BLOCK_ARENA is responsible for block_t / var_t allocation */
57
+ arena_t * BLOCK_ARENA ;
58
+
49
59
/* BB_ARENA is responsible for basic_block_t / ph2_ir_t allocation */
50
60
arena_t * BB_ARENA ;
51
61
@@ -56,6 +66,7 @@ int ph2_ir_idx = 0;
56
66
57
67
func_list_t FUNC_LIST ;
58
68
func_t * GLOBAL_FUNC ;
69
+ block_t * GLOBAL_BLOCK ;
59
70
basic_block_t * MAIN_BB ;
60
71
int elf_offset = 0 ;
61
72
@@ -499,20 +510,14 @@ void set_var_liveout(var_t *var, int end)
499
510
500
511
block_t * add_block (block_t * parent , func_t * func , macro_t * macro )
501
512
{
502
- block_t * blk = malloc (sizeof (block_t ));
503
-
504
- if (!BLOCKS .head ) {
505
- BLOCKS .head = blk ;
506
- BLOCKS .tail = BLOCKS .head ;
507
- } else {
508
- BLOCKS .tail -> next = blk ;
509
- BLOCKS .tail = blk ;
510
- }
513
+ block_t * blk = arena_alloc (BLOCK_ARENA , sizeof (block_t ));
511
514
512
515
blk -> parent = parent ;
513
516
blk -> func = func ;
514
517
blk -> macro = macro ;
515
- blk -> next_local = 0 ;
518
+ blk -> locals .capacity = 16 ;
519
+ blk -> locals .elements =
520
+ arena_alloc (BLOCK_ARENA , blk -> locals .capacity * sizeof (var_t * ));
516
521
return blk ;
517
522
}
518
523
@@ -645,9 +650,10 @@ var_t *find_local_var(char *token, block_t *block)
645
650
func_t * func = block -> func ;
646
651
647
652
for (; block ; block = block -> parent ) {
648
- for (int i = 0 ; i < block -> next_local ; i ++ ) {
649
- if (!strcmp (block -> locals [i ].var_name , token ))
650
- return & block -> locals [i ];
653
+ var_list_t * var_list = & block -> locals ;
654
+ for (int i = 0 ; i < var_list -> size ; i ++ ) {
655
+ if (!strcmp (var_list -> elements [i ]-> var_name , token ))
656
+ return var_list -> elements [i ];
651
657
}
652
658
}
653
659
@@ -662,11 +668,11 @@ var_t *find_local_var(char *token, block_t *block)
662
668
663
669
var_t * find_global_var (char * token )
664
670
{
665
- block_t * block = BLOCKS . head ;
671
+ var_list_t * var_list = & GLOBAL_BLOCK -> locals ;
666
672
667
- for (int i = 0 ; i < block -> next_local ; i ++ ) {
668
- if (!strcmp (block -> locals [i ]. var_name , token ))
669
- return & block -> locals [i ];
673
+ for (int i = 0 ; i < var_list -> size ; i ++ ) {
674
+ if (!strcmp (var_list -> elements [i ]-> var_name , token ))
675
+ return var_list -> elements [i ];
670
676
}
671
677
return NULL ;
672
678
}
@@ -685,9 +691,7 @@ int size_var(var_t *var)
685
691
if (var -> is_ptr > 0 || var -> is_func ) {
686
692
size = 4 ;
687
693
} else {
688
- type_t * type = find_type (var -> type_name , 0 );
689
- if (!type )
690
- error ("Incomplete type" );
694
+ type_t * type = var -> type ;
691
695
if (type -> size == 0 )
692
696
size = type -> base_struct -> size ;
693
697
else
@@ -970,16 +974,14 @@ void global_init()
970
974
{
971
975
elf_code_start = ELF_START + elf_header_len ;
972
976
973
- BLOCKS .head = NULL ;
974
- BLOCKS .tail = NULL ;
975
-
976
977
MACROS = malloc (MAX_ALIASES * sizeof (macro_t ));
977
- FUNC_MAP = hashmap_create (DEFAULT_FUNCS_SIZE );
978
978
TYPES = malloc (MAX_TYPES * sizeof (type_t ));
979
+ BLOCK_ARENA = arena_init (DEFAULT_ARENA_SIZE );
979
980
INSN_ARENA = arena_init (DEFAULT_ARENA_SIZE );
980
981
BB_ARENA = arena_init (DEFAULT_ARENA_SIZE );
981
982
PH2_IR_FLATTEN = malloc (MAX_IR_INSTR * sizeof (ph2_ir_t * ));
982
983
SOURCE = strbuf_create (MAX_SOURCE );
984
+ FUNC_MAP = hashmap_create (DEFAULT_FUNCS_SIZE );
983
985
INCLUSION_MAP = hashmap_create (DEFAULT_INCLUSIONS_SIZE );
984
986
ALIASES_MAP = hashmap_create (MAX_ALIASES );
985
987
CONSTANTS_MAP = hashmap_create (MAX_CONSTANTS );
@@ -994,18 +996,14 @@ void global_init()
994
996
995
997
void global_release ()
996
998
{
997
- while (BLOCKS .head ) {
998
- block_t * next = BLOCKS .head -> next ;
999
- free (BLOCKS .head );
1000
- BLOCKS .head = next ;
1001
- }
1002
999
free (MACROS );
1003
- hashmap_free (FUNC_MAP );
1004
1000
free (TYPES );
1001
+ arena_free (BLOCK_ARENA );
1005
1002
arena_free (INSN_ARENA );
1006
1003
arena_free (BB_ARENA );
1007
1004
free (PH2_IR_FLATTEN );
1008
1005
strbuf_free (SOURCE );
1006
+ hashmap_free (FUNC_MAP );
1009
1007
hashmap_free (INCLUSION_MAP );
1010
1008
hashmap_free (ALIASES_MAP );
1011
1009
hashmap_free (CONSTANTS_MAP );
@@ -1018,6 +1016,14 @@ void global_release()
1018
1016
strbuf_free (elf_section );
1019
1017
}
1020
1018
1019
+ /* Reports an error without specifying a position */
1020
+ void fatal (char * msg )
1021
+ {
1022
+ printf ("[Error]: %s\n" , msg );
1023
+ abort ();
1024
+ }
1025
+
1026
+ /* Reports an error and specifying a position */
1021
1027
void error (char * msg )
1022
1028
{
1023
1029
/* Construct error source diagnostics, enabling precise identification of
@@ -1048,8 +1054,8 @@ void error(char *msg)
1048
1054
/* TODO: figure out the corresponding C source file path and report line
1049
1055
* number.
1050
1056
*/
1051
- printf ("Error %s at source location %d\n%s\n" , msg , SOURCE -> size ,
1052
- diagnostic );
1057
+ printf ("[ Error]: %s\nOccurs at source location %d. \n%s\n" , msg ,
1058
+ SOURCE -> size , diagnostic );
1053
1059
abort ();
1054
1060
}
1055
1061
@@ -1081,7 +1087,7 @@ void dump_bb_insn(func_t *func, basic_block_t *bb, bool *at_func_start)
1081
1087
continue ;
1082
1088
case OP_allocat :
1083
1089
print_indent (1 );
1084
- printf ("allocat %s" , rd -> type_name );
1090
+ printf ("allocat %s" , rd -> type -> type_name );
1085
1091
1086
1092
for (int i = 0 ; i < rd -> is_ptr ; i ++ )
1087
1093
printf ("*" );
@@ -1251,6 +1257,16 @@ void dump_bb_insn(func_t *func, basic_block_t *bb, bool *at_func_start)
1251
1257
printf ("%%%s = lshift %%%s, %%%s" , rd -> var_name , rs1 -> var_name ,
1252
1258
rs2 -> var_name );
1253
1259
break ;
1260
+ case OP_trunc :
1261
+ print_indent (1 );
1262
+ printf ("%%%s = trunc %%%s, %d" , rd -> var_name , rs1 -> var_name ,
1263
+ insn -> sz );
1264
+ break ;
1265
+ case OP_sign_ext :
1266
+ print_indent (1 );
1267
+ printf ("%%%s = sign_ext %%%s, %d" , rd -> var_name , rs1 -> var_name ,
1268
+ insn -> sz );
1269
+ break ;
1254
1270
default :
1255
1271
printf ("<Unsupported opcode: %d>" , insn -> opcode );
1256
1272
break ;
@@ -1277,7 +1293,7 @@ void dump_insn()
1277
1293
for (func_t * func = FUNC_LIST .head ; func ; func = func -> next ) {
1278
1294
bool at_func_start = true;
1279
1295
1280
- printf ("def %s" , func -> return_def .type_name );
1296
+ printf ("def %s" , func -> return_def .type -> type_name );
1281
1297
1282
1298
for (int i = 0 ; i < func -> return_def .is_ptr ; i ++ )
1283
1299
printf ("*" );
@@ -1286,7 +1302,7 @@ void dump_insn()
1286
1302
for (int i = 0 ; i < func -> num_params ; i ++ ) {
1287
1303
if (i != 0 )
1288
1304
printf (", " );
1289
- printf ("%s" , func -> param_defs [i ].type_name );
1305
+ printf ("%s" , func -> param_defs [i ].type -> type_name );
1290
1306
1291
1307
for (int k = 0 ; k < func -> param_defs [i ].is_ptr ; k ++ )
1292
1308
printf ("*" );
@@ -1302,7 +1318,7 @@ void dump_insn()
1302
1318
if (!bb )
1303
1319
continue ;
1304
1320
1305
- if (strcmp ( func -> return_def .type_name , "void" ) )
1321
+ if (func -> return_def .type != TY_void )
1306
1322
continue ;
1307
1323
1308
1324
if (bb -> insn_list .tail )
0 commit comments