Skip to content

Integrate function-related structures #195

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 2, 2025
Merged
Show file tree
Hide file tree
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
36 changes: 18 additions & 18 deletions src/arm-codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,30 +128,30 @@ void update_elf_offset(ph2_ir_t *ph2_ir)
void cfg_flatten()
{
func_t *func = find_func("__syscall");
func->fn->bbs->elf_offset = 44; /* offset of start + exit in codegen */
func->bbs->elf_offset = 44; /* offset of start + exit in codegen */

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

for (ph2_ir_t *ph2_ir = GLOBAL_FUNC.fn->bbs->ph2_ir_list.head; ph2_ir;
for (ph2_ir_t *ph2_ir = GLOBAL_FUNC->bbs->ph2_ir_list.head; ph2_ir;
ph2_ir = ph2_ir->next) {
update_elf_offset(ph2_ir);
}

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

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

/* reserve stack */
flatten_ir = add_ph2_ir(OP_define);
flatten_ir->src0 = fn->func->stack_size;
flatten_ir->src0 = func->stack_size;

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

if (bb == fn->bbs) {
if (bb == func->bbs) {
/* save ra, sp */
elf_offset += 16;
}
Expand All @@ -162,7 +162,7 @@ void cfg_flatten()

if (insn->op == OP_return) {
/* restore sp */
flatten_ir->src1 = bb->belong_to->func->stack_size;
flatten_ir->src1 = bb->belong_to->stack_size;
}

if (insn->op == OP_branch) {
Expand Down Expand Up @@ -280,15 +280,15 @@ void emit_ph2_ir(ph2_ir_t *ph2_ir)
return;
case OP_call:
func = find_func(ph2_ir->func_name);
emit(__bl(__AL, func->fn->bbs->elf_offset - elf_code_idx));
emit(__bl(__AL, func->bbs->elf_offset - elf_code_idx));
return;
case OP_load_data_address:
emit(__movw(__AL, rd, ph2_ir->src0 + elf_data_start));
emit(__movt(__AL, rd, ph2_ir->src0 + elf_data_start));
return;
case OP_address_of_func:
func = find_func(ph2_ir->func_name);
ofs = elf_code_start + func->fn->bbs->elf_offset;
ofs = elf_code_start + func->bbs->elf_offset;
emit(__movw(__AL, __r8, ofs));
emit(__movt(__AL, __r8, ofs));
emit(__sw(__AL, __r8, rn, 0));
Expand Down Expand Up @@ -432,15 +432,15 @@ void code_generate()
elf_data_start = elf_code_start + elf_offset;

/* start */
emit(__movw(__AL, __r8, GLOBAL_FUNC.stack_size));
emit(__movt(__AL, __r8, GLOBAL_FUNC.stack_size));
emit(__movw(__AL, __r8, GLOBAL_FUNC->stack_size));
emit(__movt(__AL, __r8, GLOBAL_FUNC->stack_size));
emit(__sub_r(__AL, __sp, __sp, __r8));
emit(__mov_r(__AL, __r12, __sp));
emit(__bl(__AL, GLOBAL_FUNC.fn->bbs->elf_offset - elf_code_idx));
emit(__bl(__AL, GLOBAL_FUNC->bbs->elf_offset - elf_code_idx));

/* exit */
emit(__movw(__AL, __r8, GLOBAL_FUNC.stack_size));
emit(__movt(__AL, __r8, GLOBAL_FUNC.stack_size));
emit(__movw(__AL, __r8, GLOBAL_FUNC->stack_size));
emit(__movt(__AL, __r8, GLOBAL_FUNC->stack_size));
emit(__add_r(__AL, __sp, __sp, __r8));
emit(__mov_r(__AL, __r0, __r0));
emit(__mov_i(__AL, __r7, 1));
Expand All @@ -458,13 +458,13 @@ void code_generate()
emit(__mov_r(__AL, __pc, __lr));

ph2_ir_t *ph2_ir;
for (ph2_ir = GLOBAL_FUNC.fn->bbs->ph2_ir_list.head; ph2_ir;
for (ph2_ir = GLOBAL_FUNC->bbs->ph2_ir_list.head; ph2_ir;
ph2_ir = ph2_ir->next)
emit_ph2_ir(ph2_ir);

/* prepare 'argc' and 'argv', then proceed to 'main' function */
emit(__movw(__AL, __r8, GLOBAL_FUNC.stack_size));
emit(__movt(__AL, __r8, GLOBAL_FUNC.stack_size));
emit(__movw(__AL, __r8, GLOBAL_FUNC->stack_size));
emit(__movt(__AL, __r8, GLOBAL_FUNC->stack_size));
emit(__add_r(__AL, __r8, __r12, __r8));
emit(__lw(__AL, __r0, __r8, 0));
emit(__add_i(__AL, __r1, __r8, 4));
Expand Down
56 changes: 26 additions & 30 deletions src/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@
#define MAX_PARAMS 8
#define MAX_LOCALS 1600
#define MAX_FIELDS 64
#define MAX_FUNCS 512
#define MAX_TYPES 64
#define MAX_IR_INSTR 60000
#define MAX_BB_PRED 128
#define MAX_BB_DOM_SUCC 64
#define MAX_BB_RDOM_SUCC 256
#define MAX_GLOBAL_IR 256
#define MAX_LABEL 4096
#define MAX_SOURCE 524288
#define MAX_CODE 262144
#define MAX_DATA 262144
Expand All @@ -40,11 +38,12 @@
#define MAX_NESTING 128
#define MAX_OPERAND_STACK_SIZE 32
#define MAX_ANALYSIS_STACK_SIZE 800
#define MAX_INCLUSIONS 16

/* Default capacities for common data structures */
/* Default arena size is initialized with 256 KiB */
#define DEFAULT_ARENA_SIZE 262144
#define DEFAULT_FUNCS_SIZE 64
#define DEFAULT_INCLUSIONS_SIZE 16

#define ELF_START 0x10000
#define PTR_SIZE 4
Expand Down Expand Up @@ -319,17 +318,7 @@ typedef struct {
bool disabled;
} macro_t;

typedef struct fn fn_t;

/* function definition */
typedef struct {
var_t return_def;
var_t param_defs[MAX_PARAMS];
int num_params;
int va_args;
int stack_size; /* stack always starts at offset 4 for convenience */
fn_t *fn;
} func_t;
typedef struct func func_t;

/* block definition */
struct block {
Expand Down Expand Up @@ -360,12 +349,6 @@ typedef struct {
var_t *src1;
} ph1_ir_t;

/* label lookup table*/
typedef struct {
char name[MAX_VAR_LEN];
int offset;
} label_lut_t;

typedef struct basic_block basic_block_t;

/* Definition of a dynamic array structure for sources in src/globals.c
Expand Down Expand Up @@ -513,7 +496,7 @@ struct basic_block {
struct basic_block *dom_prev;
struct basic_block *rdom_next[256];
struct basic_block *rdom_prev;
fn_t *belong_to;
func_t *belong_to;
block_t *scope;
symbol_list_t symbol_list; /* variable declaration */
int elf_offset;
Expand All @@ -524,27 +507,40 @@ struct ref_block {
struct ref_block *next;
};

/* TODO: integrate func_t into fn_t */
struct fn {
/**
* Syntatic representation of func, combines syntactic details
* (e.g., return type, parameters) with SSA-related information
* (e.g., basic blocks, control flow) to support parsing,
* analysis, optimization, and code generation.
*/
struct func {
/* Syntatic info */
var_t return_def;
var_t param_defs[MAX_PARAMS];
int num_params;
int va_args;
int stack_size; /* stack always starts at offset 4 for convenience */

/* SSA info */
basic_block_t *bbs;
basic_block_t *exit;
symbol_list_t global_sym_list;
int bb_cnt;
int visited;
func_t *func;
struct fn *next;

struct func *next;
};

typedef struct {
fn_t *head;
fn_t *tail;
func_t *head;
func_t *tail;
} func_list_t;

typedef struct {
fn_t *fn;
func_t *func;
basic_block_t *bb;
void (*preorder_cb)(fn_t *, basic_block_t *);
void (*postorder_cb)(fn_t *, basic_block_t *);
void (*preorder_cb)(func_t *, basic_block_t *);
void (*postorder_cb)(func_t *, basic_block_t *);
} bb_traversal_args_t;

typedef struct {
Expand Down
Loading