Skip to content

Commit

Permalink
Apply editorial changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jserv committed Dec 7, 2023
1 parent 6321096 commit f421dd4
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 85 deletions.
12 changes: 4 additions & 8 deletions src/arm-codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ void cfg_flatten()
flatten_ir = add_ph2_ir(OP_load_constant);
memcpy(flatten_ir, insn, sizeof(ph2_ir_t));

/**
* ARMv7 uses 12 bits to encode immediate value, but the
/* ARMv7 uses 12 bits to encode immediate value, but the
* higher 4 bits are for rotation. See A5.2.4 "Modified
* immediate constants in ARM instructions" in ARMv7-A
* manual.
Expand All @@ -88,8 +87,7 @@ void cfg_flatten()
flatten_ir = add_ph2_ir(OP_address_of);
memcpy(flatten_ir, insn, sizeof(ph2_ir_t));

/**
* ARMv7 uses 12 bits to encode immediate value, but the
/* ARMv7 uses 12 bits to encode immediate value, but the
* higher 4 bits are for rotation. See A5.2.4 "Modified
* immediate constants in ARM instructions" in ARMv7-A
* manual.
Expand All @@ -105,8 +103,7 @@ void cfg_flatten()
flatten_ir = add_ph2_ir(OP_address_of);
memcpy(flatten_ir, insn, sizeof(ph2_ir_t));

/**
* ARMv7 straight uses 12 bits to encode the offset of
/* ARMv7 straight uses 12 bits to encode the offset of
* load instruction (no rotation).
*/
if (flatten_ir->src0 > 4095)
Expand All @@ -120,8 +117,7 @@ void cfg_flatten()
flatten_ir = add_ph2_ir(OP_address_of);
memcpy(flatten_ir, insn, sizeof(ph2_ir_t));

/**
* ARMv7 straight uses 12 bits to encode the offset of
/* ARMv7 straight uses 12 bits to encode the offset of
* store instruction (no rotation).
*/
if (flatten_ir->src1 > 4095)
Expand Down
40 changes: 16 additions & 24 deletions src/cfront.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,13 @@ char next_char;
int skip_newline = 1;

int preproc_match;
/*
* Allows replacing identifiers with alias value if alias exists. This is

/* Allow replacing identifiers with alias value if alias exists. This is
* disabled in certain cases, e.g. #undef.
*/
int preproc_aliasing = 1;
/*
* Point to the first character after where the macro has been called. It is

/* Point to the first character after where the macro has been called. It is
* needed when returning from the macro body.
*/
int macro_return_idx;
Expand Down Expand Up @@ -209,8 +209,7 @@ char read_char(int is_skip_space)
return next_char;
}

/*
* get alias name from defined() directive
/* get alias name from defined() directive
* i.e., get __arm__ from defined(__arm__)
*/
void read_alias_name_from_defined(char *alias_name, char *src)
Expand Down Expand Up @@ -264,9 +263,7 @@ void ifdef_else_skip_lines()
skip_whitespace();
}

/*
* check alias defined or not
*/
/* check alias defined or not */
void chk_def(int defined)
{
char *alias = NULL;
Expand Down Expand Up @@ -383,12 +380,10 @@ token_t get_next_token()
return get_next_token();
}
if (!strcmp(token_str, "#else")) {
/*
* reach here has 2 possible cases:
/* reach here has 2 possible cases:
* 1. reach #ifdef preprocessor directive
* 2. conditional expression in #elif is false
*/

if (!preproc_match) {
skip_whitespace();
return get_next_token();
Expand Down Expand Up @@ -1023,8 +1018,7 @@ void read_func_parameters(block_t *parent, basic_block_t **bb)
for (i = 0; i < param_num; i++) {
ph1_ir_t *ph1_ir = add_ph1_ir(OP_push);
ph1_ir->src0 = params[i];
/**
* The operand should keep alive before calling function. Pass the
/* The operand should keep alive before calling function. Pass the
* number of remained parameters to allocator to extend their liveness.
*/
add_insn(parent, *bb, OP_push, NULL, ph1_ir->src0, NULL, param_num - i,
Expand Down Expand Up @@ -1676,8 +1670,7 @@ void read_lvalue(lvalue_t *lvalue,
} else {
var_t *t;

/**
* If operand is a reference, read the value and push to stack
/* If operand is a reference, read the value and push to stack
* for the incoming addition/subtraction. Otherwise, use the
* top element of stack as the one of operands and the destination.
*/
Expand Down Expand Up @@ -1921,8 +1914,7 @@ int read_body_assignment(char *token,
if (lvalue.is_ptr)
increment_size = lvalue.type->size;

/**
* If operand is a reference, read the value and push to stack
/* If operand is a reference, read the value and push to stack
* for the incoming addition/subtraction. Otherwise, use the
* top element of stack as the one of operands and the destination.
*/
Expand Down Expand Up @@ -2137,7 +2129,7 @@ void eval_ternary_imm(int cond, char *token)
if (cond == 0) {
while (next_token != T_colon) {
next_token = get_next_token();
};
}
lex_accept(T_colon);
read_global_assignment(token);
} else {
Expand Down Expand Up @@ -2183,7 +2175,8 @@ int read_global_assignment(char *token)
add_insn(parent, GLOBAL_FUNC.fn->bbs, OP_assign, ph1_ir->dest,
ph1_ir->src0, NULL, 0, NULL);
return 1;
} else if (op == OP_ternary) {
}
if (op == OP_ternary) {
lex_expect(T_question);
eval_ternary_imm(operand1, token);
return 1;
Expand All @@ -2208,15 +2201,15 @@ int read_global_assignment(char *token)
add_insn(parent, GLOBAL_FUNC.fn->bbs, OP_assign, ph1_ir->dest,
ph1_ir->src0, NULL, 0, NULL);
return 1;
} else if (op == OP_ternary) {
}
if (op == OP_ternary) {
int cond;
lex_expect(T_question);
cond = eval_expression_imm(op, operand1, operand2);
eval_ternary_imm(cond, token);
return 1;
}


/* using stack if operands more than two */
op_stack[op_stack_index++] = op;
op = next_op;
Expand Down Expand Up @@ -2654,8 +2647,7 @@ basic_block_t *read_body_statement(block_t *parent, basic_block_t *bb)
}

if (control && true_body_) {
/**
* Create a new body block for next case, and connect the last
/* Create a new body block for next case, and connect the last
* body block which lacks `break` to it to make that one ignore
* the upcoming cases.
*/
Expand Down
38 changes: 24 additions & 14 deletions src/globals.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ int macros_idx = 0;
func_t *FUNCS;
int funcs_idx = 1;

/*
* FUNC_TRIES is used to improve the performance of the find_func function.
/* FUNC_TRIES is used to improve the performance of the find_func function.
* Instead of searching through all functions and comparing their names, we can
* utilize the trie data structure to search for existing functions efficiently.
* The index starts from 1 because the first trie node represents an empty input
Expand Down Expand Up @@ -125,7 +124,7 @@ int find_trie(trie_t *trie, char *name)
int fc = first_char;
if (!fc)
return trie->index;
else if (!trie->next[fc])
if (!trie->next[fc])
return 0;
return find_trie(&FUNC_TRIES[trie->next[fc]], name + 1);
}
Expand All @@ -137,9 +136,10 @@ int dump_ir = 0;
type_t *find_type(char *type_name)
{
int i;
for (i = 0; i < types_idx; i++)
for (i = 0; i < types_idx; i++) {
if (!strcmp(TYPES[i].type_name, type_name))
return &TYPES[i];
}
return NULL;
}

Expand Down Expand Up @@ -181,9 +181,10 @@ void add_label(char *name, int offset)
int find_label_offset(char name[])
{
int i;
for (i = 0; i < label_lut_idx; i++)
for (i = 0; i < label_lut_idx; i++) {
if (!strcmp(LABEL_LUT[i].name, name))
return LABEL_LUT[i].offset;
}
return -1;
}

Expand All @@ -209,9 +210,10 @@ void add_alias(char *alias, char *value)
char *find_alias(char alias[])
{
int i;
for (i = 0; i < aliases_idx; i++)
for (i = 0; i < aliases_idx; i++) {
if (!ALIASES[i].disabled && !strcmp(alias, ALIASES[i].alias))
return ALIASES[i].value;
}
return NULL;
}

Expand All @@ -238,9 +240,10 @@ macro_t *add_macro(char *name)
macro_t *find_macro(char *name)
{
int i;
for (i = 0; i < macros_idx; i++)
for (i = 0; i < macros_idx; i++) {
if (!MACROS[i].disabled && !strcmp(name, MACROS[i].name))
return &MACROS[i];
}
return NULL;
}

Expand All @@ -267,9 +270,10 @@ int find_macro_param_src_idx(char *name, block_t *parent)
if (!parent->macro)
return 0;

for (i = 0; i < macro->num_param_defs; i++)
for (i = 0; i < macro->num_param_defs; i++) {
if (!strcmp(macro->param_defs[i].var_name, name))
return macro->params[i];
}
return 0;
}

Expand Down Expand Up @@ -308,9 +312,10 @@ void add_constant(char alias[], int value)
constant_t *find_constant(char alias[])
{
int i;
for (i = 0; i < constants_idx; i++)
for (i = 0; i < constants_idx; i++) {
if (!strcmp(CONSTANTS[i].alias, alias))
return &CONSTANTS[i];
}
return NULL;
}

Expand All @@ -325,9 +330,10 @@ func_t *find_func(char func_name[])
var_t *find_member(char token[], type_t *type)
{
int i;
for (i = 0; i < type->num_fields; i++)
for (i = 0; i < type->num_fields; i++) {
if (!strcmp(type->fields[i].var_name, token))
return &type->fields[i];
}
return NULL;
}

Expand All @@ -337,15 +343,17 @@ var_t *find_local_var(char *token, block_t *block)
func_t *fn = block->func;

for (; block; block = block->parent) {
for (i = 0; i < block->next_local; i++)
for (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 (i = 0; i < fn->num_params; i++) {
if (!strcmp(fn->param_defs[i].var_name, token))
return &fn->param_defs[i];
}
}
return NULL;
}
Expand All @@ -355,9 +363,10 @@ var_t *find_global_var(char *token)
int i;
block_t *block = &BLOCKS[0];

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

Expand Down Expand Up @@ -485,9 +494,10 @@ void bb_disconnect(basic_block_t *pred, basic_block_t *succ)
void add_symbol(basic_block_t *bb, var_t *var)
{
symbol_t *sym;
for (sym = bb->symbol_list.head; sym; sym = sym->next)
for (sym = bb->symbol_list.head; sym; sym = sym->next) {
if (sym->var == var)
return;
}

sym = calloc(1, sizeof(symbol_t));
sym->var = var;
Expand Down
Loading

0 comments on commit f421dd4

Please sign in to comment.