Skip to content

Commit 411a6f6

Browse files
authored
Merge pull request #195 from ChAoSUnItY/feat/func_integrate
Integrate function-related structures
2 parents f3bf67a + b83d08b commit 411a6f6

File tree

12 files changed

+389
-415
lines changed

12 files changed

+389
-415
lines changed

src/arm-codegen.c

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -128,30 +128,30 @@ void update_elf_offset(ph2_ir_t *ph2_ir)
128128
void cfg_flatten()
129129
{
130130
func_t *func = find_func("__syscall");
131-
func->fn->bbs->elf_offset = 44; /* offset of start + exit in codegen */
131+
func->bbs->elf_offset = 44; /* offset of start + exit in codegen */
132132

133133
elf_offset = 80; /* offset of start + exit + syscall in codegen */
134-
GLOBAL_FUNC.fn->bbs->elf_offset = elf_offset;
134+
GLOBAL_FUNC->bbs->elf_offset = elf_offset;
135135

136-
for (ph2_ir_t *ph2_ir = GLOBAL_FUNC.fn->bbs->ph2_ir_list.head; ph2_ir;
136+
for (ph2_ir_t *ph2_ir = GLOBAL_FUNC->bbs->ph2_ir_list.head; ph2_ir;
137137
ph2_ir = ph2_ir->next) {
138138
update_elf_offset(ph2_ir);
139139
}
140140

141141
/* prepare 'argc' and 'argv', then proceed to 'main' function */
142142
elf_offset += 24;
143143

144-
for (fn_t *fn = FUNC_LIST.head; fn; fn = fn->next) {
144+
for (func = FUNC_LIST.head; func; func = func->next) {
145145
ph2_ir_t *flatten_ir;
146146

147147
/* reserve stack */
148148
flatten_ir = add_ph2_ir(OP_define);
149-
flatten_ir->src0 = fn->func->stack_size;
149+
flatten_ir->src0 = func->stack_size;
150150

151-
for (basic_block_t *bb = fn->bbs; bb; bb = bb->rpo_next) {
151+
for (basic_block_t *bb = func->bbs; bb; bb = bb->rpo_next) {
152152
bb->elf_offset = elf_offset;
153153

154-
if (bb == fn->bbs) {
154+
if (bb == func->bbs) {
155155
/* save ra, sp */
156156
elf_offset += 16;
157157
}
@@ -162,7 +162,7 @@ void cfg_flatten()
162162

163163
if (insn->op == OP_return) {
164164
/* restore sp */
165-
flatten_ir->src1 = bb->belong_to->func->stack_size;
165+
flatten_ir->src1 = bb->belong_to->stack_size;
166166
}
167167

168168
if (insn->op == OP_branch) {
@@ -280,15 +280,15 @@ void emit_ph2_ir(ph2_ir_t *ph2_ir)
280280
return;
281281
case OP_call:
282282
func = find_func(ph2_ir->func_name);
283-
emit(__bl(__AL, func->fn->bbs->elf_offset - elf_code_idx));
283+
emit(__bl(__AL, func->bbs->elf_offset - elf_code_idx));
284284
return;
285285
case OP_load_data_address:
286286
emit(__movw(__AL, rd, ph2_ir->src0 + elf_data_start));
287287
emit(__movt(__AL, rd, ph2_ir->src0 + elf_data_start));
288288
return;
289289
case OP_address_of_func:
290290
func = find_func(ph2_ir->func_name);
291-
ofs = elf_code_start + func->fn->bbs->elf_offset;
291+
ofs = elf_code_start + func->bbs->elf_offset;
292292
emit(__movw(__AL, __r8, ofs));
293293
emit(__movt(__AL, __r8, ofs));
294294
emit(__sw(__AL, __r8, rn, 0));
@@ -432,15 +432,15 @@ void code_generate()
432432
elf_data_start = elf_code_start + elf_offset;
433433

434434
/* start */
435-
emit(__movw(__AL, __r8, GLOBAL_FUNC.stack_size));
436-
emit(__movt(__AL, __r8, GLOBAL_FUNC.stack_size));
435+
emit(__movw(__AL, __r8, GLOBAL_FUNC->stack_size));
436+
emit(__movt(__AL, __r8, GLOBAL_FUNC->stack_size));
437437
emit(__sub_r(__AL, __sp, __sp, __r8));
438438
emit(__mov_r(__AL, __r12, __sp));
439-
emit(__bl(__AL, GLOBAL_FUNC.fn->bbs->elf_offset - elf_code_idx));
439+
emit(__bl(__AL, GLOBAL_FUNC->bbs->elf_offset - elf_code_idx));
440440

441441
/* exit */
442-
emit(__movw(__AL, __r8, GLOBAL_FUNC.stack_size));
443-
emit(__movt(__AL, __r8, GLOBAL_FUNC.stack_size));
442+
emit(__movw(__AL, __r8, GLOBAL_FUNC->stack_size));
443+
emit(__movt(__AL, __r8, GLOBAL_FUNC->stack_size));
444444
emit(__add_r(__AL, __sp, __sp, __r8));
445445
emit(__mov_r(__AL, __r0, __r0));
446446
emit(__mov_i(__AL, __r7, 1));
@@ -458,13 +458,13 @@ void code_generate()
458458
emit(__mov_r(__AL, __pc, __lr));
459459

460460
ph2_ir_t *ph2_ir;
461-
for (ph2_ir = GLOBAL_FUNC.fn->bbs->ph2_ir_list.head; ph2_ir;
461+
for (ph2_ir = GLOBAL_FUNC->bbs->ph2_ir_list.head; ph2_ir;
462462
ph2_ir = ph2_ir->next)
463463
emit_ph2_ir(ph2_ir);
464464

465465
/* prepare 'argc' and 'argv', then proceed to 'main' function */
466-
emit(__movw(__AL, __r8, GLOBAL_FUNC.stack_size));
467-
emit(__movt(__AL, __r8, GLOBAL_FUNC.stack_size));
466+
emit(__movw(__AL, __r8, GLOBAL_FUNC->stack_size));
467+
emit(__movt(__AL, __r8, GLOBAL_FUNC->stack_size));
468468
emit(__add_r(__AL, __r8, __r12, __r8));
469469
emit(__lw(__AL, __r0, __r8, 0));
470470
emit(__add_i(__AL, __r1, __r8, 4));

src/defs.h

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,12 @@
1919
#define MAX_PARAMS 8
2020
#define MAX_LOCALS 1600
2121
#define MAX_FIELDS 64
22-
#define MAX_FUNCS 512
2322
#define MAX_TYPES 64
2423
#define MAX_IR_INSTR 60000
2524
#define MAX_BB_PRED 128
2625
#define MAX_BB_DOM_SUCC 64
2726
#define MAX_BB_RDOM_SUCC 256
2827
#define MAX_GLOBAL_IR 256
29-
#define MAX_LABEL 4096
3028
#define MAX_SOURCE 524288
3129
#define MAX_CODE 262144
3230
#define MAX_DATA 262144
@@ -40,11 +38,12 @@
4038
#define MAX_NESTING 128
4139
#define MAX_OPERAND_STACK_SIZE 32
4240
#define MAX_ANALYSIS_STACK_SIZE 800
43-
#define MAX_INCLUSIONS 16
4441

4542
/* Default capacities for common data structures */
4643
/* Default arena size is initialized with 256 KiB */
4744
#define DEFAULT_ARENA_SIZE 262144
45+
#define DEFAULT_FUNCS_SIZE 64
46+
#define DEFAULT_INCLUSIONS_SIZE 16
4847

4948
#define ELF_START 0x10000
5049
#define PTR_SIZE 4
@@ -319,17 +318,7 @@ typedef struct {
319318
bool disabled;
320319
} macro_t;
321320

322-
typedef struct fn fn_t;
323-
324-
/* function definition */
325-
typedef struct {
326-
var_t return_def;
327-
var_t param_defs[MAX_PARAMS];
328-
int num_params;
329-
int va_args;
330-
int stack_size; /* stack always starts at offset 4 for convenience */
331-
fn_t *fn;
332-
} func_t;
321+
typedef struct func func_t;
333322

334323
/* block definition */
335324
struct block {
@@ -360,12 +349,6 @@ typedef struct {
360349
var_t *src1;
361350
} ph1_ir_t;
362351

363-
/* label lookup table*/
364-
typedef struct {
365-
char name[MAX_VAR_LEN];
366-
int offset;
367-
} label_lut_t;
368-
369352
typedef struct basic_block basic_block_t;
370353

371354
/* Definition of a dynamic array structure for sources in src/globals.c
@@ -513,7 +496,7 @@ struct basic_block {
513496
struct basic_block *dom_prev;
514497
struct basic_block *rdom_next[256];
515498
struct basic_block *rdom_prev;
516-
fn_t *belong_to;
499+
func_t *belong_to;
517500
block_t *scope;
518501
symbol_list_t symbol_list; /* variable declaration */
519502
int elf_offset;
@@ -524,27 +507,40 @@ struct ref_block {
524507
struct ref_block *next;
525508
};
526509

527-
/* TODO: integrate func_t into fn_t */
528-
struct fn {
510+
/**
511+
* Syntatic representation of func, combines syntactic details
512+
* (e.g., return type, parameters) with SSA-related information
513+
* (e.g., basic blocks, control flow) to support parsing,
514+
* analysis, optimization, and code generation.
515+
*/
516+
struct func {
517+
/* Syntatic info */
518+
var_t return_def;
519+
var_t param_defs[MAX_PARAMS];
520+
int num_params;
521+
int va_args;
522+
int stack_size; /* stack always starts at offset 4 for convenience */
523+
524+
/* SSA info */
529525
basic_block_t *bbs;
530526
basic_block_t *exit;
531527
symbol_list_t global_sym_list;
532528
int bb_cnt;
533529
int visited;
534-
func_t *func;
535-
struct fn *next;
530+
531+
struct func *next;
536532
};
537533

538534
typedef struct {
539-
fn_t *head;
540-
fn_t *tail;
535+
func_t *head;
536+
func_t *tail;
541537
} func_list_t;
542538

543539
typedef struct {
544-
fn_t *fn;
540+
func_t *func;
545541
basic_block_t *bb;
546-
void (*preorder_cb)(fn_t *, basic_block_t *);
547-
void (*postorder_cb)(fn_t *, basic_block_t *);
542+
void (*preorder_cb)(func_t *, basic_block_t *);
543+
void (*postorder_cb)(func_t *, basic_block_t *);
548544
} bb_traversal_args_t;
549545

550546
typedef struct {

0 commit comments

Comments
 (0)