Skip to content
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

Enforce C99 to improve readability #121

Merged
merged 1 commit into from
Mar 22, 2024
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CC ?= gcc
CFLAGS := -O -g \
-ansi -pedantic \
-std=c99 -pedantic \
-Wall -Wextra \
-Wno-unused-but-set-variable \
-Wno-variadic-macros \
Expand Down
16 changes: 6 additions & 10 deletions src/arm-codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,32 +125,29 @@ void cfg_flatten()
elf_offset = 80; /* offset of start + exit + syscall in codegen */
GLOBAL_FUNC.fn->bbs->elf_offset = elf_offset;

ph2_ir_t *ph2_ir;
for (ph2_ir = GLOBAL_FUNC.fn->bbs->ph2_ir_list.head; ph2_ir;
for (ph2_ir_t *ph2_ir = GLOBAL_FUNC.fn->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;

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

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

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

if (bb == fn->bbs)
/* save ra, sp */
elf_offset += 16;

ph2_ir_t *insn;
for (insn = bb->ph2_ir_list.head; insn; insn = insn->next) {
for (ph2_ir_t *insn = bb->ph2_ir_list.head; insn;
insn = insn->next) {
flatten_ir = add_ph2_ir(OP_generic);
memcpy(flatten_ir, insn, sizeof(ph2_ir_t));

Expand Down Expand Up @@ -426,8 +423,7 @@ void code_generate()
emit(__add_i(__AL, __r1, __r8, 4));
emit(__b(__AL, MAIN_BB->elf_offset - elf_code_idx));

int i;
for (i = 0; i < ph2_ir_idx; i++) {
for (int i = 0; i < ph2_ir_idx; i++) {
ph2_ir = &PH2_IR[i];
emit_ph2_ir(ph2_ir);
}
Expand Down
4 changes: 1 addition & 3 deletions src/arm.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,11 @@ arm_cond_t arm_get_cond(opcode_t op)

int arm_extract_bits(int imm, int i_start, int i_end, int d_start, int d_end)
{
int v;

if (((d_end - d_start) != (i_end - i_start)) || (i_start > i_end) ||
(d_start > d_end))
error("Invalid bit copy");

v = imm >> i_start;
int v = imm >> i_start;
v = v & ((2 << (i_end - i_start)) - 1);
v = v << d_start;
return v;
Expand Down
27 changes: 10 additions & 17 deletions src/elf.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@ int elf_section_index;

void elf_write_section_str(char *vals, int len)
{
int i;
for (i = 0; i < len; i++)
for (int i = 0; i < len; i++)
elf_section[elf_section_index++] = vals[i];
}

void elf_write_data_str(char *vals, int len)
{
int i;
for (i = 0; i < len; i++)
for (int i = 0; i < len; i++)
elf_data[elf_data_idx++] = vals[i];
}

Expand All @@ -43,8 +41,7 @@ char e_extract_byte(int v, int b)

int elf_write_int(char *buf, int index, int val)
{
int i = 0;
for (i = 0; i < 4; i++)
for (int i = 0; i < 4; i++)
buf[index++] = e_extract_byte(val, i);
return index;
}
Expand Down Expand Up @@ -118,12 +115,11 @@ void elf_generate_header()
void elf_generate_sections()
{
/* symtab section */
int b;
for (b = 0; b < elf_symtab_index; b++)
for (int b = 0; b < elf_symtab_index; b++)
elf_write_section_byte(elf_symtab[b]);

/* strtab section */
for (b = 0; b < elf_strtab_index; b++)
for (int b = 0; b < elf_strtab_index; b++)
elf_write_section_byte(elf_strtab[b]);

/* shstr section; len = 39 */
Expand Down Expand Up @@ -246,9 +242,6 @@ void elf_add_symbol(char *symbol, int len, int pc)

void elf_generate(char *outfile)
{
FILE *fp;
int i;

elf_symbol_index = 0;
elf_symtab_index = 0;
elf_strtab_index = 0;
Expand All @@ -261,14 +254,14 @@ void elf_generate(char *outfile)
if (!outfile)
outfile = "a.out";

fp = fopen(outfile, "wb");
for (i = 0; i < elf_header_idx; i++)
FILE *fp = fopen(outfile, "wb");
for (int i = 0; i < elf_header_idx; i++)
fputc(elf_header[i], fp);
for (i = 0; i < elf_code_idx; i++)
for (int i = 0; i < elf_code_idx; i++)
fputc(elf_code[i], fp);
for (i = 0; i < elf_data_idx; i++)
for (int i = 0; i < elf_data_idx; i++)
fputc(elf_data[i], fp);
for (i = 0; i < elf_section_index; i++)
for (int i = 0; i < elf_section_index; i++)
fputc(elf_section[i], fp);
fclose(fp);
}
58 changes: 21 additions & 37 deletions src/globals.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ int insert_trie(trie_t *trie, char *name, int funcs_index)
{
char first_char = *name;
int fc = first_char;
int i;
if (!fc) {
if (!trie->index)
trie->index = funcs_index;
Expand All @@ -102,7 +101,7 @@ int insert_trie(trie_t *trie, char *name, int funcs_index)
* handle this is to dynamically allocate a new element.
*/
trie->next[fc] = func_tries_idx++;
for (i = 0; i < 128; i++)
for (int i = 0; i < 128; i++)
FUNC_TRIES[trie->next[fc]].next[i] = 0;
FUNC_TRIES[trie->next[fc]].index = 0;
}
Expand Down Expand Up @@ -146,8 +145,7 @@ int dump_ir = 0;
*/
type_t *find_type(char *type_name, int flag)
{
int i;
for (i = 0; i < types_idx; i++) {
for (int i = 0; i < types_idx; i++) {
if (TYPES[i].base_type == TYPE_struct) {
if (flag == 1)
continue;
Expand Down Expand Up @@ -207,8 +205,7 @@ void add_label(char *name, int offset)

int find_label_offset(char name[])
{
int i;
for (i = 0; i < label_lut_idx; i++) {
for (int i = 0; i < label_lut_idx; i++) {
if (!strcmp(LABEL_LUT[i].name, name))
return LABEL_LUT[i].offset;
}
Expand Down Expand Up @@ -236,8 +233,7 @@ void add_alias(char *alias, char *value)

char *find_alias(char alias[])
{
int i;
for (i = 0; i < aliases_idx; i++) {
for (int i = 0; i < aliases_idx; i++) {
if (!ALIASES[i].disabled && !strcmp(alias, ALIASES[i].alias))
return ALIASES[i].value;
}
Expand All @@ -246,8 +242,7 @@ char *find_alias(char alias[])

int remove_alias(char *alias)
{
int i;
for (i = 0; i < aliases_idx; i++) {
for (int i = 0; i < aliases_idx; i++) {
if (!ALIASES[i].disabled && !strcmp(alias, ALIASES[i].alias)) {
ALIASES[i].disabled = 1;
return 1;
Expand All @@ -266,8 +261,7 @@ macro_t *add_macro(char *name)

macro_t *find_macro(char *name)
{
int i;
for (i = 0; i < macros_idx; i++) {
for (int i = 0; i < macros_idx; i++) {
if (!MACROS[i].disabled && !strcmp(name, MACROS[i].name))
return &MACROS[i];
}
Expand All @@ -276,8 +270,7 @@ macro_t *find_macro(char *name)

int remove_macro(char *name)
{
int i;
for (i = 0; i < macros_idx; i++) {
for (int i = 0; i < macros_idx; i++) {
if (!MACROS[i].disabled && !strcmp(name, MACROS[i].name)) {
MACROS[i].disabled = 1;
return 1;
Expand All @@ -289,15 +282,14 @@ int remove_macro(char *name)
void error(char *msg);
int find_macro_param_src_idx(char *name, block_t *parent)
{
int i;
macro_t *macro = parent->macro;

if (!parent)
error("The macro expansion is not supported in the global scope");
if (!parent->macro)
return 0;

for (i = 0; i < macro->num_param_defs; i++) {
for (int i = 0; i < macro->num_param_defs; i++) {
if (!strcmp(macro->param_defs[i].var_name, name))
return macro->params[i];
}
Expand Down Expand Up @@ -338,8 +330,7 @@ void add_constant(char alias[], int value)

constant_t *find_constant(char alias[])
{
int i;
for (i = 0; i < constants_idx; i++) {
for (int i = 0; i < constants_idx; i++) {
if (!strcmp(CONSTANTS[i].alias, alias))
return &CONSTANTS[i];
}
Expand All @@ -363,8 +354,7 @@ var_t *find_member(char token[], type_t *type)
if (type->size == 0)
type = type->base_struct;

int i;
for (i = 0; i < type->num_fields; i++) {
for (int i = 0; i < type->num_fields; i++) {
if (!strcmp(type->fields[i].var_name, token))
return &type->fields[i];
}
Expand All @@ -373,18 +363,17 @@ var_t *find_member(char token[], type_t *type)

var_t *find_local_var(char *token, block_t *block)
{
int i;
func_t *fn = block->func;

for (; block; block = block->parent) {
for (i = 0; i < block->next_local; i++) {
for (int i = 0; i < block->next_local; i++) {
if (!strcmp(block->locals[i].var_name, token))
return &block->locals[i];
}
}

if (fn) {
for (i = 0; i < fn->num_params; i++) {
for (int i = 0; i < fn->num_params; i++) {
if (!strcmp(fn->param_defs[i].var_name, token))
return &fn->param_defs[i];
}
Expand All @@ -394,10 +383,9 @@ var_t *find_local_var(char *token, block_t *block)

var_t *find_global_var(char *token)
{
int i;
block_t *block = &BLOCKS[0];

for (i = 0; i < block->next_local; i++) {
for (int i = 0; i < block->next_local; i++) {
if (!strcmp(block->locals[i].var_name, token))
return &block->locals[i];
}
Expand Down Expand Up @@ -451,8 +439,7 @@ basic_block_t *bb_create(block_t *parent)
{
basic_block_t *bb = calloc(1, sizeof(basic_block_t));

int i;
for (i = 0; i < MAX_BB_PRED; i++) {
for (int i = 0; i < MAX_BB_PRED; i++) {
bb->prev[i].bb = NULL;
bb->prev[i].type = NEXT;
}
Expand Down Expand Up @@ -501,8 +488,7 @@ void bb_connect(basic_block_t *pred,
/* The pred-succ pair must have only one connection */
void bb_disconnect(basic_block_t *pred, basic_block_t *succ)
{
int i;
for (i = 0; i < MAX_BB_PRED; i++) {
for (int i = 0; i < MAX_BB_PRED; i++) {
if (succ->prev[i].bb == pred) {
switch (succ->prev[i].type) {
case NEXT:
Expand Down Expand Up @@ -666,8 +652,7 @@ void error(char *msg)

void print_indent(int indent)
{
int i;
for (i = 0; i < indent; i++)
for (int i = 0; i < indent; i++)
printf("\t");
}

Expand All @@ -676,10 +661,9 @@ void dump_ph1_ir()
int indent = 0;
ph1_ir_t *ph1_ir;
func_t *fn;
int i, j, k;
char rd[MAX_VAR_LEN], op1[MAX_VAR_LEN], op2[MAX_VAR_LEN];

for (i = 0; i < ph1_ir_idx; i++) {
for (int i = 0; i < ph1_ir_idx; i++) {
ph1_ir = &PH1_IR[i];

if (ph1_ir->dest)
Expand All @@ -694,16 +678,16 @@ void dump_ph1_ir()
fn = find_func(ph1_ir->func_name);
printf("def %s", fn->return_def.type_name);

for (j = 0; j < fn->return_def.is_ptr; j++)
for (int j = 0; j < fn->return_def.is_ptr; j++)
printf("*");
printf(" @%s(", ph1_ir->func_name);

for (j = 0; j < fn->num_params; j++) {
for (int j = 0; j < fn->num_params; j++) {
if (j != 0)
printf(", ");
printf("%s", fn->param_defs[j].type_name);

for (k = 0; k < fn->param_defs[j].is_ptr; k++)
for (int k = 0; k < fn->param_defs[j].is_ptr; k++)
printf("*");
printf(" %%%s", fn->param_defs[j].var_name);
}
Expand All @@ -722,7 +706,7 @@ void dump_ph1_ir()
case OP_allocat:
print_indent(indent);
printf("allocat %s", ph1_ir->src0->type_name);
for (j = 0; j < ph1_ir->src0->is_ptr; j++)
for (int j = 0; j < ph1_ir->src0->is_ptr; j++)
printf("*");
printf(" %%%s", op1);

Expand Down
4 changes: 2 additions & 2 deletions src/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,12 @@ int is_hex(char c)

int is_numeric(char buffer[])
{
int i, hex = 0, size = strlen(buffer);
int hex = 0, size = strlen(buffer);

if (size > 2)
hex = (buffer[0] == '0' && buffer[1] == 'x') ? 1 : 0;

for (i = 0; i < size; i++) {
for (int i = 0; i < size; i++) {
if (hex && (is_hex(buffer[i]) == 0))
return 0;
if (!hex && (is_digit(buffer[i]) == 0))
Expand Down
3 changes: 1 addition & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ int main(int argc, char *argv[])
{
int libc = 1;
char *out = NULL, *in = NULL;
int i;

for (i = 1; i < argc; i++) {
for (int i = 1; i < argc; i++) {
if (!strcmp(argv[i], "--dump-ir"))
dump_ir = 1;
else if (!strcmp(argv[i], "--no-libc"))
Expand Down
Loading