From 40502f69aadda8c21bca0038df266cc5c7649de6 Mon Sep 17 00:00:00 2001 From: "yu.dongliang" <18588496441@163.com> Date: Wed, 17 Jun 2026 22:55:05 +0800 Subject: [PATCH] delete some un-used member vars and code for them. --- core/scf_3ac.c | 6 +- core/scf_3ac.h | 4 +- core/scf_ast.c | 2 +- core/scf_ast.h | 26 -------- core/scf_basic_block.c | 2 +- core/scf_label.c | 4 -- core/scf_node.c | 28 +-------- core/scf_node.h | 9 +-- core/scf_operator_handler_3ac.c | 50 +++++++--------- core/scf_scope.c | 71 ++++++---------------- core/scf_scope.h | 41 ++++++------- core/scf_type.c | 14 ++--- core/scf_type.h | 11 +--- core/scf_variable.c | 2 +- parse/scf_dfa_class.c | 2 +- parse/scf_dfa_container.c | 2 +- parse/scf_dfa_function.c | 4 +- parse/scf_dfa_goto.c | 6 +- parse/scf_dfa_label.c | 3 +- parse/scf_dfa_type.c | 2 +- parse/scf_dfa_va_arg.c | 2 +- parse/scf_dfa_var.c | 12 ++-- parse/scf_operator_handler_semantic.c | 19 +++--- parse/scf_parse.c | 86 +++++++++++++-------------- parse/scf_parse.h | 5 -- 25 files changed, 145 insertions(+), 268 deletions(-) diff --git a/core/scf_3ac.c b/core/scf_3ac.c index 0e28716..07401bb 100644 --- a/core/scf_3ac.c +++ b/core/scf_3ac.c @@ -384,7 +384,7 @@ scf_3ac_code_t* scf_3ac_alloc_by_dst(int op_type, scf_dag_node_t* dst) return c; } -scf_3ac_code_t* scf_3ac_jmp_code(int type, scf_label_t* l, scf_node_t* err) +scf_3ac_code_t* scf_3ac_jmp_code(int type, scf_lex_word_t* label) { scf_3ac_operand_t* dst; scf_3ac_code_t* c; @@ -394,9 +394,9 @@ scf_3ac_code_t* scf_3ac_jmp_code(int type, scf_label_t* l, scf_node_t* err) return NULL; c->op = scf_3ac_find_operator(type); - c->label = l; + c->label = label; - c->dsts = scf_vector_alloc(); + c->dsts = scf_vector_alloc(); if (!c->dsts) { scf_3ac_code_free(c); return NULL; diff --git a/core/scf_3ac.h b/core/scf_3ac.h index 947bff2..7f45cb6 100644 --- a/core/scf_3ac.h +++ b/core/scf_3ac.h @@ -32,7 +32,7 @@ struct scf_3ac_code_s { scf_vector_t* dsts; // dst operands, maybe only used for function return value scf_vector_t* srcs; // src operands, usually 2 - scf_label_t* label; // only for 'goto' to find the destination to go + scf_lex_word_t* label; // only for 'goto' to find the destination scf_3ac_code_t* origin; @@ -60,7 +60,7 @@ void scf_3ac_code_print(scf_3ac_code_t* c, scf_list_t* sentinel); void scf_3ac_list_print(scf_list_t* h); -scf_3ac_code_t* scf_3ac_jmp_code(int type, scf_label_t* l, scf_node_t* err); +scf_3ac_code_t* scf_3ac_jmp_code(int type, scf_lex_word_t* label); scf_3ac_operator_t* scf_3ac_find_operator(const int type); diff --git a/core/scf_ast.c b/core/scf_ast.c index d7a827c..50eafa2 100644 --- a/core/scf_ast.c +++ b/core/scf_ast.c @@ -158,7 +158,7 @@ static int _find_type_by_type(scf_node_t* node, void* arg, scf_vector_t* vec) scf_type_t* t = (scf_type_t*)node; - if (t->type == (intptr_t)arg) { + if (t->node.type == (intptr_t)arg) { int ret = scf_vector_add(vec, t); if (ret < 0) diff --git a/core/scf_ast.h b/core/scf_ast.h index 0cc4cc0..55da0d1 100644 --- a/core/scf_ast.h +++ b/core/scf_ast.h @@ -56,32 +56,6 @@ int scf_expr_calculate_internal(scf_ast_t* ast, scf_node_t* node, void* data); scf_string_t* scf_variable_type_name(scf_ast_t* ast, scf_variable_t* v); -static inline void scf_ast_push_block(scf_ast_t* ast, scf_block_t* b) -{ - scf_node_add_child((scf_node_t*)ast->current_block, (scf_node_t*)b); - ast->current_block = b; -} - -static inline void scf_ast_pop_block(scf_ast_t* ast) -{ - ast->current_block = (scf_block_t*)(ast->current_block->node.parent); -} - -static inline scf_block_t* scf_ast_parent_block(scf_ast_t* ast) -{ - scf_node_t* parent = ast->current_block->node.parent; - - while (parent && parent->type != SCF_OP_BLOCK && parent->type != SCF_FUNCTION) - parent = parent->parent; - - return (scf_block_t*)parent; -} - -static inline scf_scope_t* scf_ast_current_scope(scf_ast_t* ast) -{ - return ast->current_block->scope; -} - int scf_ast_find_proper_function (scf_function_t** pf, scf_ast_t* ast, scf_vector_t* fvec, scf_vector_t* argv); int scf_ast_find_global_function (scf_function_t** pf, scf_ast_t* ast, char* name); int scf_ast_find_global_variable (scf_variable_t** pv, scf_ast_t* ast, char* name); diff --git a/core/scf_basic_block.c b/core/scf_basic_block.c index 55a6426..0fc2e60 100644 --- a/core/scf_basic_block.c +++ b/core/scf_basic_block.c @@ -143,7 +143,7 @@ scf_basic_block_t* scf_basic_block_jcc(scf_basic_block_t* to, scf_function_t* f, if (!bb) return NULL; - c = scf_3ac_jmp_code(jcc, NULL, NULL); + c = scf_3ac_jmp_code(jcc, NULL); if (!c) { scf_basic_block_free(bb); return NULL; diff --git a/core/scf_label.c b/core/scf_label.c index 63bdb9e..7bb478b 100644 --- a/core/scf_label.c +++ b/core/scf_label.c @@ -12,7 +12,6 @@ scf_label_t* scf_label_alloc(scf_lex_word_t* w) return NULL; } - l->refs = 1; l->type = SCF_LABEL; return l; } @@ -20,9 +19,6 @@ scf_label_t* scf_label_alloc(scf_lex_word_t* w) void scf_label_free(scf_label_t* l) { if (l) { - if (--l->refs > 0) - return; - if (l->w) { scf_lex_word_free(l->w); l->w = NULL; diff --git a/core/scf_node.c b/core/scf_node.c index 89d68ad..deaf7be 100644 --- a/core/scf_node.c +++ b/core/scf_node.c @@ -76,9 +76,7 @@ scf_node_t* scf_node_clone(scf_node_t* node) if (!dst->var) goto failed; - } else if (SCF_LABEL == node->type) - dst->label = node->label; - else { + } else { if (node->w) { dst->w = scf_lex_word_clone(node->w); if (!dst->w) @@ -110,19 +108,6 @@ failed: return NULL; } -scf_node_t* scf_node_alloc_label(scf_label_t* l) -{ - scf_node_t* node = calloc(1, sizeof(scf_node_t)); - if (!node) { - scf_loge("node alloc failed\n"); - return NULL; - } - - node->type = SCF_LABEL; - node->label = l; - return node; -} - int scf_node_add_child(scf_node_t* parent, scf_node_t* child) { if (!parent) @@ -168,11 +153,6 @@ void scf_node_free_data(scf_node_t* node) scf_variable_free(node->var); node->var = NULL; } - } else if (SCF_LABEL == node->type) { - if (node->label) { - scf_label_free(node->label); - node->label = NULL; - } } else { if (node->w) { scf_lex_word_free(node->w); @@ -277,11 +257,7 @@ void scf_node_print(scf_node_t* node) if (node) { scf_logw("node: %p, type: %d", node, node->type); - if (SCF_LABEL == node->type) { - if (node->label && node->label->w) - printf(", w: %s, line: %d", node->label->w->text->data, node->label->w->line); - - } else if (scf_type_is_var(node->type)) { + if (scf_type_is_var(node->type)) { if (node->var && node->var->w) printf(", w: %s, line: %d", node->var->w->text->data, node->var->w->line); diff --git a/core/scf_node.h b/core/scf_node.h index 7023d1e..523e324 100644 --- a/core/scf_node.h +++ b/core/scf_node.h @@ -20,9 +20,8 @@ struct scf_node_s { scf_node_t** nodes; // children nodes union { - scf_variable_t* var; - scf_lex_word_t* w; - scf_label_t* label; + scf_variable_t* var; + scf_lex_word_t* w; }; scf_lex_word_t* debug_w; @@ -60,9 +59,6 @@ struct scf_label_s { }; scf_node_t* scf_node_alloc(scf_lex_word_t* w, int type, scf_variable_t* var); -scf_node_t* scf_node_alloc_label(scf_label_t* l); - -scf_node_t* scf_node_clone(scf_node_t* node); int scf_node_add_child(scf_node_t* parent, scf_node_t* child); void scf_node_del_child(scf_node_t* parent, scf_node_t* child); @@ -71,6 +67,7 @@ void scf_node_free(scf_node_t* node); void scf_node_free_data(scf_node_t* node); void scf_node_move_data(scf_node_t* dst, scf_node_t* src); +scf_node_t* scf_node_clone(scf_node_t* node); void scf_node_print(scf_node_t* node); scf_variable_t* _scf_operand_get(const scf_node_t* node); diff --git a/core/scf_operator_handler_3ac.c b/core/scf_operator_handler_3ac.c index 33d276c..cb81514 100644 --- a/core/scf_operator_handler_3ac.c +++ b/core/scf_operator_handler_3ac.c @@ -385,9 +385,7 @@ static int _scf_op_block(scf_ast_t* ast, scf_node_t** nodes, int nb_nodes, void* int i; int j; - for (i = 0; i < nb_nodes; i++) { - node = nodes[i]; op = node->op; @@ -413,7 +411,6 @@ static int _scf_op_block(scf_ast_t* ast, scf_node_t** nodes, int nb_nodes, void* // for goto if (SCF_LABEL == node->type) { - scf_label_t* l = node->label; scf_list_t* tail = scf_list_tail(d->_3ac_list_head); scf_3ac_code_t* end = scf_list_data(tail, scf_3ac_code_t, list); scf_3ac_code_t* c; @@ -423,7 +420,7 @@ static int _scf_op_block(scf_ast_t* ast, scf_node_t** nodes, int nb_nodes, void* scf_loge("\n"); return -1; } - end->label = l; + end->label = node->w; int j; for (j = 0; j < d->branch_ops->_gotos->size; j++) { @@ -432,10 +429,9 @@ static int _scf_op_block(scf_ast_t* ast, scf_node_t** nodes, int nb_nodes, void* if (!c) continue; - assert(l->w); - assert(c->label->w); + assert(c->label); - if (!strcmp(l->w->text->data, c->label->w->text->data)) { + if (!strcmp(node->w->text->data, c->label->text->data)) { dst = c->dsts->data[0]; dst->code = end; } @@ -486,7 +482,7 @@ static int _scf_op_return(scf_ast_t* ast, scf_node_t** nodes, int nb_nodes, void return -1; } - scf_3ac_code_t* end = scf_3ac_jmp_code(SCF_OP_GOTO, NULL, NULL); + scf_3ac_code_t* end = scf_3ac_jmp_code(SCF_OP_GOTO, NULL); scf_list_add_tail(d->_3ac_list_head, &end->list); @@ -510,7 +506,7 @@ static int _scf_op_break(scf_ast_t* ast, scf_node_t** nodes, int nb_nodes, void* return -1; } - scf_3ac_code_t* jmp = scf_3ac_jmp_code(SCF_OP_GOTO, NULL, NULL); + scf_3ac_code_t* jmp = scf_3ac_jmp_code(SCF_OP_GOTO, NULL); scf_list_add_tail(d->_3ac_list_head, &jmp->list); @@ -534,7 +530,7 @@ static int _scf_op_continue(scf_ast_t* ast, scf_node_t** nodes, int nb_nodes, vo return -1; } - scf_3ac_code_t* jmp = scf_3ac_jmp_code(SCF_OP_GOTO, NULL, NULL); + scf_3ac_code_t* jmp = scf_3ac_jmp_code(SCF_OP_GOTO, NULL); scf_list_add_tail(d->_3ac_list_head, &jmp->list); @@ -553,14 +549,12 @@ static int _scf_op_goto(scf_ast_t* ast, scf_node_t** nodes, int nb_nodes, void* scf_handler_data_t* d = data; scf_node_t* nl = nodes[0]; - scf_label_t* l = nl->label; assert(SCF_LABEL == nl->type); - assert(l->w); scf_3ac_operand_t* dst; scf_3ac_code_t* c; - scf_3ac_code_t* jmp = scf_3ac_jmp_code(SCF_OP_GOTO, l, NULL); + scf_3ac_code_t* jmp = scf_3ac_jmp_code(SCF_OP_GOTO, nl->w); scf_list_add_tail(d->_3ac_list_head, &jmp->list); @@ -568,7 +562,9 @@ static int _scf_op_goto(scf_ast_t* ast, scf_node_t** nodes, int nb_nodes, void* for (i = 0; i < d->branch_ops->_labels->size; i++) { c = d->branch_ops->_labels->data[i]; - if (!strcmp(l->w->text->data, c->label->w->text->data)) { + assert(c->label); + + if (!strcmp(nl->w->text->data, c->label->text->data)) { dst = jmp->dsts->data[0]; dst->code = c; break; @@ -734,7 +730,7 @@ static int _scf_op_if(scf_ast_t* ast, scf_node_t** nodes, int nb_nodes, void* da } scf_3ac_operand_t* dst; - scf_3ac_code_t* jmp_else = scf_3ac_jmp_code(jmp_op, NULL, NULL); + scf_3ac_code_t* jmp_else = scf_3ac_jmp_code(jmp_op, NULL); scf_3ac_code_t* jmp_endif = NULL; scf_list_t* l; @@ -751,7 +747,7 @@ static int _scf_op_if(scf_ast_t* ast, scf_node_t** nodes, int nb_nodes, void* da if (1 == i) { if (3 == nb_nodes) { - jmp_endif = scf_3ac_jmp_code(SCF_OP_GOTO, NULL, NULL); + jmp_endif = scf_3ac_jmp_code(SCF_OP_GOTO, NULL); scf_list_add_tail(d->_3ac_list_head, &jmp_endif->list); } @@ -877,13 +873,13 @@ static int _scf_op_end_loop(scf_list_t* start_prev, scf_list_t* continue_prev, s if (jmp_end) { // add loop when true - loop = scf_3ac_jmp_code(jmp_op, NULL, NULL); + loop = scf_3ac_jmp_code(jmp_op, NULL); scf_list_add_tail(d->_3ac_list_head, &loop->list); // should get the real start here, start = scf_list_data(scf_list_next(&jmp_end->list), scf_3ac_code_t, list); } else { - loop = scf_3ac_jmp_code(SCF_OP_GOTO, NULL, NULL); + loop = scf_3ac_jmp_code(SCF_OP_GOTO, NULL); scf_list_add_tail(d->_3ac_list_head, &loop->list); start = scf_list_data(scf_list_next(start_prev), scf_3ac_code_t, list); @@ -990,7 +986,7 @@ static int _scf_op_do(scf_ast_t* ast, scf_node_t** nodes, int nb_nodes, void* da scf_list_t* l; scf_3ac_code_t* c; - scf_3ac_code_t* jmp_end = scf_3ac_jmp_code(jmp_op, NULL, NULL); + scf_3ac_code_t* jmp_end = scf_3ac_jmp_code(jmp_op, NULL); scf_list_add_tail(d->_3ac_list_head, &jmp_end->list); @@ -1050,7 +1046,7 @@ static int _scf_op_while(scf_ast_t* ast, scf_node_t** nodes, int nb_nodes, void* return -1; } - scf_3ac_code_t* jmp_end = scf_3ac_jmp_code(jmp_op, NULL, NULL); + scf_3ac_code_t* jmp_end = scf_3ac_jmp_code(jmp_op, NULL); scf_list_add_tail(d->_3ac_list_head, &jmp_end->list); scf_branch_ops_t* local_branch_ops = scf_branch_ops_alloc(); @@ -1115,7 +1111,7 @@ static int _scf_op_vla_alloc(scf_ast_t* ast, scf_node_t** nodes, int nb_nodes, v if (!cmp) return -ENOMEM; - jgt = scf_3ac_jmp_code(SCF_OP_3AC_JGT, NULL, NULL); + jgt = scf_3ac_jmp_code(SCF_OP_3AC_JGT, NULL); if (!jgt) { scf_3ac_code_free(cmp); return -ENOMEM; @@ -1194,7 +1190,7 @@ static int _scf_op_switch(scf_ast_t* ast, scf_node_t** nodes, int nb_nodes, void if (SCF_OP_CASE == child->type || SCF_OP_DEFAULT == child->type) { if (jnot) { - jnext = scf_3ac_jmp_code(SCF_OP_GOTO, NULL, NULL); + jnext = scf_3ac_jmp_code(SCF_OP_GOTO, NULL); scf_list_add_tail(d->_3ac_list_head, &jnext->list); scf_vector_add(up_branch_ops->_breaks, jnext); @@ -1229,7 +1225,7 @@ static int _scf_op_switch(scf_ast_t* ast, scf_node_t** nodes, int nb_nodes, void else cmp = scf_3ac_code_NN(SCF_OP_3AC_TEQ, NULL, 0, &e2, 1); - jnot = scf_3ac_jmp_code(SCF_OP_3AC_JNZ, NULL, NULL); + jnot = scf_3ac_jmp_code(SCF_OP_3AC_JNZ, NULL); scf_list_add_tail(d->_3ac_list_head, &cmp->list); scf_list_add_tail(d->_3ac_list_head, &jnot->list); @@ -1316,7 +1312,7 @@ static int _scf_op_for(scf_ast_t* ast, scf_node_t** nodes, int nb_nodes, void* d return -1; } - jmp_end = scf_3ac_jmp_code(jmp_op, NULL, NULL); + jmp_end = scf_3ac_jmp_code(jmp_op, NULL); scf_list_add_tail(d->_3ac_list_head, &jmp_end->list); } } @@ -1509,7 +1505,7 @@ static int _scf_op_new(scf_ast_t* ast, scf_node_t** nodes, int nb_nodes, void* d return ret; } - jz = scf_3ac_jmp_code(SCF_OP_3AC_JZ, NULL, NULL); + jz = scf_3ac_jmp_code(SCF_OP_3AC_JZ, NULL); scf_list_add_tail(d->_3ac_list_head, &jz->list); for (i = 3; i < nb_nodes; i++) { @@ -1533,7 +1529,7 @@ static int _scf_op_new(scf_ast_t* ast, scf_node_t** nodes, int nb_nodes, void* d return ret; } - jmp = scf_3ac_jmp_code(SCF_OP_GOTO, NULL, NULL); + jmp = scf_3ac_jmp_code(SCF_OP_GOTO, NULL); scf_list_add_tail(d->_3ac_list_head, &jmp->list); scf_vector_add(d->branch_ops->_breaks, jz); @@ -2378,7 +2374,7 @@ static int _scf_op_logic_##name(scf_ast_t* ast, scf_node_t** nodes, int nb_nodes if (jmp_op < 0) \ return -1; \ \ - scf_3ac_code_t* jmp = scf_3ac_jmp_code(jmp_op, NULL, NULL); \ + scf_3ac_code_t* jmp = scf_3ac_jmp_code(jmp_op, NULL); \ if (!jmp) \ return -ENOMEM; \ \ diff --git a/core/scf_scope.c b/core/scf_scope.c index 7a06609..732b59e 100644 --- a/core/scf_scope.c +++ b/core/scf_scope.c @@ -1,93 +1,58 @@ #include"scf_ast.h" -scf_scope_t* scf_scope_alloc(scf_lex_word_t* w, const char* name) +scf_scope_t* scf_scope_alloc() { scf_scope_t* scope = calloc(1, sizeof(scf_scope_t)); if (!scope) return NULL; - scope->name = scf_string_cstr(name); - if (!scope->name) { - free(scope); - return NULL; - } - scope->vars = scf_vector_alloc(); if (!scope->vars) { - scf_string_free(scope->name); free(scope); return NULL; } - if (w) { - scope->w = scf_lex_word_clone(w); - - if (!scope->w) { - scf_vector_free(scope->vars); - scf_string_free(scope->name); - free(scope); - return NULL; - } - } - scf_list_init(&scope->list); - scf_list_init(&scope->type_list_head); - scf_list_init(&scope->operator_list_head); - scf_list_init(&scope->function_list_head); - scf_list_init(&scope->label_list_head); + scf_list_init(&scope->types); + scf_list_init(&scope->operators); + scf_list_init(&scope->functions); + scf_list_init(&scope->labels); return scope; } void scf_scope_push_var(scf_scope_t* scope, scf_variable_t* var) { - assert(scope); - assert(var); scf_vector_add(scope->vars, var); } void scf_scope_push_type(scf_scope_t* scope, scf_type_t* t) { - assert(scope); - assert(t); - scf_list_add_front(&scope->type_list_head, &t->list); + scf_list_add_front(&scope->types, &t->list); } void scf_scope_push_operator(scf_scope_t* scope, scf_function_t* op) { - assert(scope); - assert(op); - scf_list_add_front(&scope->operator_list_head, &op->list); + scf_list_add_front(&scope->operators, &op->list); } void scf_scope_push_function(scf_scope_t* scope, scf_function_t* f) { - assert(scope); - assert(f); - scf_list_add_front(&scope->function_list_head, &f->list); + scf_list_add_front(&scope->functions, &f->list); } void scf_scope_push_label(scf_scope_t* scope, scf_label_t* l) { - assert(scope); - assert(l); - scf_list_add_front(&scope->label_list_head, &l->list); + scf_list_add_front(&scope->labels, &l->list); } void scf_scope_free(scf_scope_t* scope) { if (scope) { - scf_string_free(scope->name); - scope->name = NULL; - - if (scope->w) - scf_lex_word_free(scope->w); - scf_vector_clear(scope->vars, (void (*)(void*))scf_variable_free); scf_vector_free(scope->vars); scope->vars = NULL; - scf_list_clear(&scope->type_list_head, scf_type_t, list, scf_type_free); - scf_list_clear(&scope->function_list_head, scf_function_t, list, scf_function_free); + scf_list_clear(&scope->labels, scf_label_t, list, scf_label_free); free(scope); } @@ -98,7 +63,7 @@ scf_type_t* scf_scope_find_type(scf_scope_t* scope, const char* name) scf_type_t* t; scf_list_t* l; - for (l = scf_list_head(&scope->type_list_head); l != scf_list_sentinel(&scope->type_list_head); l = scf_list_next(l)) { + for (l = scf_list_head(&scope->types); l != scf_list_sentinel(&scope->types); l = scf_list_next(l)) { t = scf_list_data(l, scf_type_t, list); if (!strcmp(name, t->name->data)) { @@ -113,10 +78,10 @@ scf_type_t* scf_scope_find_type_type(scf_scope_t* scope, const int type) scf_type_t* t; scf_list_t* l; - for (l = scf_list_head(&scope->type_list_head); l != scf_list_sentinel(&scope->type_list_head); l = scf_list_next(l)) { + for (l = scf_list_head(&scope->types); l != scf_list_sentinel(&scope->types); l = scf_list_next(l)) { t = scf_list_data(l, scf_type_t, list); - if (type == t->type) + if (type == t->node.type) return t; } return NULL; @@ -141,7 +106,7 @@ scf_label_t* scf_scope_find_label(scf_scope_t* scope, const char* name) scf_label_t* label; scf_list_t* l; - for (l = scf_list_head(&scope->label_list_head); l != scf_list_sentinel(&scope->label_list_head); l = scf_list_next(l)) { + for (l = scf_list_head(&scope->labels); l != scf_list_sentinel(&scope->labels); l = scf_list_next(l)) { label = scf_list_data(l, scf_label_t, list); if (!strcmp(name, label->w->text->data)) @@ -155,7 +120,7 @@ scf_function_t* scf_scope_find_function(scf_scope_t* scope, const char* name) scf_function_t* f; scf_list_t* l; - for (l = scf_list_head(&scope->function_list_head); l != scf_list_sentinel(&scope->function_list_head); l = scf_list_next(l)) { + for (l = scf_list_head(&scope->functions); l != scf_list_sentinel(&scope->functions); l = scf_list_next(l)) { f = scf_list_data(l, scf_function_t, list); if (!strcmp(name, f->node.w->text->data)) @@ -169,7 +134,7 @@ scf_function_t* scf_scope_find_same_function(scf_scope_t* scope, scf_function_t* scf_function_t* f1; scf_list_t* l; - for (l = scf_list_head(&scope->function_list_head); l != scf_list_sentinel(&scope->function_list_head); l = scf_list_next(l)) { + for (l = scf_list_head(&scope->functions); l != scf_list_sentinel(&scope->functions); l = scf_list_next(l)) { f1 = scf_list_data(l, scf_function_t, list); if (scf_function_same(f0, f1)) @@ -188,7 +153,7 @@ int scf_scope_find_like_functions(scf_vector_t** pfunctions, scf_scope_t* scope, if (!vec) return -ENOMEM; - for (l = scf_list_head(&scope->function_list_head); l != scf_list_sentinel(&scope->function_list_head); l = scf_list_next(l)) { + for (l = scf_list_head(&scope->functions); l != scf_list_sentinel(&scope->functions); l = scf_list_next(l)) { f = scf_list_data(l, scf_function_t, list); if (strcmp(f->node.w->text->data, name)) @@ -223,7 +188,7 @@ int scf_scope_find_overloaded_functions(scf_vector_t** pfunctions, scf_scope_t* if (!vec) return -ENOMEM; - for (l = scf_list_head(&scope->operator_list_head); l != scf_list_sentinel(&scope->operator_list_head); l = scf_list_next(l)) { + for (l = scf_list_head(&scope->operators); l != scf_list_sentinel(&scope->operators); l = scf_list_next(l)) { f = scf_list_data(l, scf_function_t, list); if (op_type != f->op_type) diff --git a/core/scf_scope.h b/core/scf_scope.h index 53ef96a..ac823f1 100644 --- a/core/scf_scope.h +++ b/core/scf_scope.h @@ -6,37 +6,30 @@ #include"scf_lex_word.h" #include"scf_core_types.h" -struct scf_scope_s { - scf_list_t list; // scope list - scf_string_t* name; // scope name - - scf_lex_word_t* w; // scope name - - scf_vector_t* vars; // vars in this scope, should not have the same name - scf_list_t type_list_head; // type list in this scope, not define the same type - scf_list_t operator_list_head; // operator list in this scope - scf_list_t function_list_head; // function list in this scope - scf_list_t label_list_head; // label list in this scope +struct scf_scope_s +{ + scf_list_t list; // scope list + + scf_vector_t* vars; // vars in this scope, should not have the same name + scf_list_t types; // type list in this scope, not define the same type + scf_list_t operators; // operator list in this scope + scf_list_t functions; // function list in this scope + scf_list_t labels; // label list in this scope }; -scf_scope_t* scf_scope_alloc(scf_lex_word_t* w, const char* name); -void scf_scope_free( scf_scope_t* scope); +scf_scope_t* scf_scope_alloc(); +void scf_scope_free(scf_scope_t* scope); -void scf_scope_push_var(scf_scope_t* scope, scf_variable_t* var); - -void scf_scope_push_type(scf_scope_t* scope, scf_type_t* t); +void scf_scope_push_var (scf_scope_t* scope, scf_variable_t* var); +void scf_scope_push_type(scf_scope_t* scope, scf_type_t* t); void scf_scope_push_operator(scf_scope_t* scope, scf_function_t* op); - void scf_scope_push_function(scf_scope_t* scope, scf_function_t* f); -scf_type_t* scf_scope_find_type(scf_scope_t* scope, const char* name); - -scf_type_t* scf_scope_find_type_type(scf_scope_t* scope, const int type); - -scf_variable_t* scf_scope_find_variable(scf_scope_t* scope, const char* name); - -scf_function_t* scf_scope_find_function(scf_scope_t* scope, const char* name); +scf_type_t* scf_scope_find_type (scf_scope_t* scope, const char* name); +scf_type_t* scf_scope_find_type_type(scf_scope_t* scope, const int type); +scf_variable_t* scf_scope_find_variable (scf_scope_t* scope, const char* name); +scf_function_t* scf_scope_find_function (scf_scope_t* scope, const char* name); scf_function_t* scf_scope_find_same_function(scf_scope_t* scope, scf_function_t* f0); diff --git a/core/scf_type.c b/core/scf_type.c index 5a13a3a..94455a5 100644 --- a/core/scf_type.c +++ b/core/scf_type.c @@ -7,7 +7,6 @@ scf_type_t* scf_type_alloc(scf_lex_word_t* w, const char* name, int type, int si if (!t) return NULL; - t->type = type; t->node.type = type; t->name = scf_string_cstr(name); @@ -17,8 +16,8 @@ scf_type_t* scf_type_alloc(scf_lex_word_t* w, const char* name, int type, int si } if (w) { - t->w = scf_lex_word_clone(w); - if (!t->w) { + t->node.w = scf_lex_word_clone(w); + if (!t->node.w) { scf_string_free(t->name); free(t); return NULL; @@ -35,18 +34,12 @@ void scf_type_free(scf_type_t* t) scf_string_free(t->name); t->name = NULL; - if (t->w) { - scf_lex_word_free(t->w); - t->w = NULL; - } - if (t->scope) { scf_scope_free(t->scope); t->scope = NULL; } - free(t); - t = NULL; + scf_node_free((scf_node_t*)t); } } @@ -54,6 +47,7 @@ static scf_type_abbrev_t type_abbrevs[] = { {"int", "i"}, {"void", "v"}, + {"bool", "B"}, {"char", "c"}, {"float", "f"}, diff --git a/core/scf_type.h b/core/scf_type.h index efb3a15..3ece46b 100644 --- a/core/scf_type.h +++ b/core/scf_type.h @@ -4,8 +4,8 @@ #include"scf_node.h" typedef struct { - int type; const char* name; + int type; int size; } scf_base_type_t; @@ -14,20 +14,16 @@ typedef struct { const char* abbrev; } scf_type_abbrev_t; -struct scf_type_s { +struct scf_type_s +{ // same as block, only used for class type scf_node_t node; - scf_scope_t* scope; - scf_string_t* name; // list for scope's type_list_head scf_list_t list; - int type; - scf_lex_word_t* w; - int nb_pointers; // int array_capacity; @@ -45,4 +41,3 @@ void scf_type_free(scf_type_t* t); const char* scf_type_find_abbrev(const char* name); #endif - diff --git a/core/scf_variable.c b/core/scf_variable.c index 96197d2..bcd9cdb 100644 --- a/core/scf_variable.c +++ b/core/scf_variable.c @@ -98,7 +98,7 @@ scf_variable_t* scf_variable_alloc(scf_lex_word_t* w, scf_type_t* t) return NULL; v->refs = 1; - v->type = t->type; + v->type = t->node.type; v->const_flag = t->node.const_flag; v->nb_pointers = t->nb_pointers; diff --git a/parse/scf_dfa_class.c b/parse/scf_dfa_class.c index 2e87944..be6d604 100644 --- a/parse/scf_dfa_class.c +++ b/parse/scf_dfa_class.c @@ -54,7 +54,7 @@ static int _class_action_identity(scf_dfa_t* dfa, scf_vector_t* words, void* dat md->parent_block = parse->ast->current_block; SCF_DFA_PUSH_HOOK(scf_dfa_find_node(dfa, "class_end"), SCF_DFA_HOOK_END); - scf_logi("\033[31m t: %p, t->type: %d\033[0m\n", t, t->type); + scf_logi("\033[31m t: %p, t->type: %d\033[0m\n", t, t->node.type); return SCF_DFA_NEXT_WORD; } diff --git a/parse/scf_dfa_container.c b/parse/scf_dfa_container.c index 5a2ac2b..03d1bbb 100644 --- a/parse/scf_dfa_container.c +++ b/parse/scf_dfa_container.c @@ -120,7 +120,7 @@ static int _container_action_comma(scf_dfa_t* dfa, scf_vector_t* words, void* da return SCF_DFA_ERROR; } - if (id->type->type < SCF_STRUCT) { + if (id->type->node.type < SCF_STRUCT) { scf_loge("'%s' is not a class or struct\n", w->text->data); free(id); return SCF_DFA_ERROR; diff --git a/parse/scf_dfa_function.c b/parse/scf_dfa_function.c index 2e7fe32..14f4394 100644 --- a/parse/scf_dfa_function.c +++ b/parse/scf_dfa_function.c @@ -59,7 +59,7 @@ int _function_add_function(scf_dfa_t* dfa, dfa_data_t* d) return SCF_DFA_ERROR; } - if (SCF_VAR_VOID == id->type->type && 0 == id->nb_pointers) + if (SCF_VAR_VOID == id->type->node.type && 0 == id->nb_pointers) void_flag = 1; f->extern_flag |= id->extern_flag; @@ -152,7 +152,7 @@ int _function_add_arg(scf_dfa_t* dfa, dfa_data_t* d) if (v && v->identity) w = v->identity; - if (SCF_VAR_VOID == t->type->type && 0 == t->nb_pointers) { + if (SCF_VAR_VOID == t->type->node.type && 0 == t->nb_pointers) { scf_loge("\n"); return SCF_DFA_ERROR; } diff --git a/parse/scf_dfa_goto.c b/parse/scf_dfa_goto.c index b3ea503..2c035d5 100644 --- a/parse/scf_dfa_goto.c +++ b/parse/scf_dfa_goto.c @@ -31,11 +31,9 @@ static int _goto_action_identity(scf_dfa_t* dfa, scf_vector_t* words, void* data scf_parse_t* parse = dfa->priv; dfa_data_t* d = data; scf_lex_word_t* w = words->data[words->size - 1]; + scf_node_t* node = scf_node_alloc(w, SCF_LABEL, NULL); - scf_label_t* l = scf_label_alloc(w); - scf_node_t* n = scf_node_alloc_label(l); - - scf_node_add_child(d->current_goto, n); + scf_node_add_child(d->current_goto, node); return SCF_DFA_NEXT_WORD; } diff --git a/parse/scf_dfa_label.c b/parse/scf_dfa_label.c index a017cf7..f8679fa 100644 --- a/parse/scf_dfa_label.c +++ b/parse/scf_dfa_label.c @@ -16,7 +16,7 @@ static int _label_action_colon(scf_dfa_t* dfa, scf_vector_t* words, void* data) } scf_label_t* l = scf_label_alloc(id->identity); - scf_node_t* n = scf_node_alloc_label(l); + scf_node_t* n = scf_node_alloc(l->w, SCF_LABEL, NULL); scf_stack_pop(d->current_identities); free(id); @@ -24,6 +24,7 @@ static int _label_action_colon(scf_dfa_t* dfa, scf_vector_t* words, void* data) scf_node_add_child((scf_node_t*)parse->ast->current_block, n); + l->node = n; scf_scope_push_label(parse->ast->current_block->scope, l); return SCF_DFA_OK; diff --git a/parse/scf_dfa_type.c b/parse/scf_dfa_type.c index 5ef9055..7e72383 100644 --- a/parse/scf_dfa_type.c +++ b/parse/scf_dfa_type.c @@ -132,7 +132,7 @@ int _type_find_type(scf_dfa_t* dfa, dfa_identity_t* id) } } - if (SCF_FUNCTION_PTR == id->type->type) { + if (SCF_FUNCTION_PTR == id->type->node.type) { id->func_ptr = _type_find_function(parse->ast->current_block, id->identity->text->data); diff --git a/parse/scf_dfa_va_arg.c b/parse/scf_dfa_va_arg.c index 0a4dc98..918690c 100644 --- a/parse/scf_dfa_va_arg.c +++ b/parse/scf_dfa_va_arg.c @@ -120,7 +120,7 @@ static int _va_arg_action_ap(scf_dfa_t* dfa, scf_vector_t* words, void* data) } assert(t); - if (t->type != ap->type || 0 != ap->nb_dimentions) { + if (t->node.type != ap->type || 0 != ap->nb_dimentions) { scf_loge("variable %s is not va_list type\n", w->text->data); return SCF_DFA_ERROR; } diff --git a/parse/scf_dfa_var.c b/parse/scf_dfa_var.c index b911d12..c626f56 100644 --- a/parse/scf_dfa_var.c +++ b/parse/scf_dfa_var.c @@ -8,8 +8,8 @@ int _expr_multi_rets(scf_expr_t* e); int _check_recursive(scf_type_t* parent, scf_type_t* child, scf_lex_word_t* w) { - if (child->type == parent->type) { - + if (child->node.type == parent->node.type) { + scf_loge("recursive define '%s' type member var '%s' in type '%s', line: %d\n", child->name->data, w->text->data, parent->name->data, w->line); @@ -17,7 +17,7 @@ int _check_recursive(scf_type_t* parent, scf_type_t* child, scf_lex_word_t* w) } if (child->scope) { - assert(child->type >= SCF_STRUCT); + assert(child->node.type >= SCF_STRUCT); scf_variable_t* v = NULL; scf_type_t* type_v = NULL; @@ -84,7 +84,7 @@ static int _var_add_var(scf_dfa_t* dfa, dfa_data_t* d) global_flag = 0; member_flag = 1; - if (0 == id0->nb_pointers && id0->type->type >= SCF_STRUCT) { + if (0 == id0->nb_pointers && id0->type->node.type >= SCF_STRUCT) { // if not pointer var, check if define recursive struct/union/class var if (_check_recursive((scf_type_t*)b, id0->type, id->identity) < 0) { @@ -96,7 +96,7 @@ static int _var_add_var(scf_dfa_t* dfa, dfa_data_t* d) } } - if (SCF_FUNCTION_PTR == id0->type->type + if (SCF_FUNCTION_PTR == id0->type->node.type && (!id0->func_ptr || 0 == id0->nb_pointers)) { scf_loge("invalid func ptr\n"); return SCF_DFA_ERROR; @@ -115,7 +115,7 @@ static int _var_add_var(scf_dfa_t* dfa, dfa_data_t* d) } } - if (SCF_VAR_VOID == id0->type->type && 0 == id0->nb_pointers) { + if (SCF_VAR_VOID == id0->type->node.type && 0 == id0->nb_pointers) { scf_loge("void var must be a pointer, like void*\n"); return SCF_DFA_ERROR; } diff --git a/parse/scf_operator_handler_semantic.c b/parse/scf_operator_handler_semantic.c index 5655a3b..6ba2ae3 100644 --- a/parse/scf_operator_handler_semantic.c +++ b/parse/scf_operator_handler_semantic.c @@ -150,7 +150,7 @@ static int _semantic_do_type_cast(scf_ast_t* ast, scf_node_t** nodes, int nb_nod if (!v_std) return -ENOMEM; - if (t->type != v0->type) { + if (t->node.type != v0->type) { ret = _semantic_add_type_cast(ast, &nodes[0], v_std, nodes[0]); if (ret < 0) { scf_loge("add type cast failed\n"); @@ -158,7 +158,7 @@ static int _semantic_do_type_cast(scf_ast_t* ast, scf_node_t** nodes, int nb_nod } } - if (t->type != v1->type) { + if (t->node.type != v1->type) { ret = _semantic_add_type_cast(ast, &nodes[1], v_std, nodes[1]); if (ret < 0) { scf_loge("add type cast failed\n"); @@ -467,12 +467,12 @@ static int _semantic_do_new(scf_ast_t* ast, scf_node_t** nodes, int nb_nodes, sc if (!new) return -ENOMEM; - v_pf = SCF_VAR_ALLOC_BY_TYPE(t->w, pt, 1, 1, NULL); + v_pf = SCF_VAR_ALLOC_BY_TYPE(t->node.w, pt, 1, 1, NULL); if (!v_pf) return -ENOMEM; v_pf->const_literal_flag = 1; - node_pf = scf_node_alloc(t->w, v_pf->type, v_pf); + node_pf = scf_node_alloc(t->node.w, v_pf->type, v_pf); if (!node_pf) return -ENOMEM; @@ -727,7 +727,7 @@ static int _scf_op_semantic_new(scf_ast_t* ast, scf_node_t** nodes, int nb_nodes if (!argv) return -ENOMEM; - ret = _semantic_add_var(&nthis, ast, NULL, v0->w, class->type, 0, 1, NULL); + ret = _semantic_add_var(&nthis, ast, NULL, v0->w, class->node.type, 0, 1, NULL); if (ret < 0) { scf_vector_free(argv); return ret; @@ -1149,11 +1149,8 @@ static int _scf_op_semantic_goto(scf_ast_t* ast, scf_node_t** nodes, int nb_node scf_node_t* nl = nodes[0]; assert(SCF_LABEL == nl->type); - scf_label_t* l = nl->label; - assert(l->w); - - scf_label_t* l2 = scf_block_find_label(ast->current_block, l->w->text->data); - if (!l2) { + scf_label_t* l = scf_block_find_label(ast->current_block, nl->w->text->data); + if (!l) { scf_loge("label '%s' not found\n", l->w->text->data); return -1; } @@ -2425,7 +2422,7 @@ static int _scf_op_semantic_binary(scf_ast_t* ast, scf_node_t** nodes, int nb_no if (ret < 0) return ret; - if (t->type != v0->type) + if (t->node.type != v0->type) ret = _semantic_add_type_cast(ast, &nodes[0], v1, nodes[0]); else ret = _semantic_add_type_cast(ast, &nodes[1], v0, nodes[1]); diff --git a/parse/scf_parse.c b/parse/scf_parse.c index fc05258..6e11519 100644 --- a/parse/scf_parse.c +++ b/parse/scf_parse.c @@ -10,38 +10,38 @@ #include"scf_leb128.h" #include"scf_eda.h" -scf_base_type_t base_types[] = +scf_base_type_t base_types[] = { - {SCF_VAR_CHAR, "char", 1}, - - {SCF_VAR_VOID, "void", 1}, - {SCF_VAR_BIT, "bit", 1}, - {SCF_VAR_U2, "bit2_t", 1}, - {SCF_VAR_U3, "bit3_t", 1}, - {SCF_VAR_U4, "bit4_t", 1}, - - {SCF_VAR_I1, "int1_t", 1}, - {SCF_VAR_I2, "int2_t", 1}, - {SCF_VAR_I3, "int3_t", 1}, - {SCF_VAR_I4, "int4_t", 1}, - - {SCF_VAR_INT, "int", 4}, - {SCF_VAR_FLOAT, "float", 4}, - {SCF_VAR_DOUBLE, "double", 8}, - - {SCF_VAR_I8, "int8_t", 1}, - {SCF_VAR_I16, "int16_t", 2}, - {SCF_VAR_I32, "int32_t", 4}, - {SCF_VAR_I64, "int64_t", 8}, - - {SCF_VAR_U8, "uint8_t", 1}, - {SCF_VAR_U16, "uint16_t", 2}, - {SCF_VAR_U32, "uint32_t", 4}, - {SCF_VAR_U64, "uint64_t", 8}, - - {SCF_VAR_INTPTR, "intptr_t", sizeof(void*)}, - {SCF_VAR_UINTPTR, "uintptr_t", sizeof(void*)}, - {SCF_FUNCTION_PTR, "funcptr", sizeof(void*)}, + {"char", SCF_VAR_CHAR, 1}, + {"void", SCF_VAR_VOID, 1}, + + {"bit", SCF_VAR_BIT, 1}, + {"bit2_t", SCF_VAR_U2, 1}, + {"bit3_t", SCF_VAR_U3, 1}, + {"bit4_t", SCF_VAR_U4, 1}, + + {"int1_t", SCF_VAR_I1, 1}, + {"int2_t", SCF_VAR_I2, 1}, + {"int3_t", SCF_VAR_I3, 1}, + {"int4_t", SCF_VAR_I4, 1}, + + {"int", SCF_VAR_INT, 4}, + {"float", SCF_VAR_FLOAT, 4}, + {"double", SCF_VAR_DOUBLE, 8}, + + {"int8_t", SCF_VAR_I8, 1}, + {"int16_t", SCF_VAR_I16, 2}, + {"int32_t", SCF_VAR_I32, 4}, + {"int64_t", SCF_VAR_I64, 8}, + + {"uint8_t", SCF_VAR_U8, 1}, + {"uint16_t", SCF_VAR_U16, 2}, + {"uint32_t", SCF_VAR_U32, 4}, + {"uint64_t", SCF_VAR_U64, 8}, + + {"intptr_t", SCF_VAR_INTPTR, sizeof(void*)}, + {"uintptr_t", SCF_VAR_UINTPTR, sizeof(void*)}, + {"funcptr", SCF_FUNCTION_PTR, sizeof(void*)}, }; int scf_parse_open(scf_parse_t** pparse) @@ -193,7 +193,7 @@ static scf_dwarf_info_entry_t* _debug_find_type(scf_parse_t* parse, scf_type_t* tag = DW_TAG_pointer_type; types = parse->debug->base_types; - } else if (t->type < SCF_STRUCT) { + } else if (t->node.type < SCF_STRUCT) { tag = DW_TAG_base_type; types = parse->debug->base_types; @@ -217,7 +217,7 @@ static scf_dwarf_info_entry_t* _debug_find_type(scf_parse_t* parse, scf_type_t* assert(ie->attributes->size == d->attributes->size); - if (ie->type == t->type && ie->nb_pointers == nb_pointers) + if (ie->type == t->node.type && ie->nb_pointers == nb_pointers) return ie; #if 0 for (j = 0; j < d ->attributes->size; j++) { @@ -264,7 +264,7 @@ static int __debug_add_type(scf_dwarf_info_entry_t** pie, scf_dwarf_abbrev_decla types = parse->debug->base_types; - } else if (t->type < SCF_STRUCT) { + } else if (t->node.type < SCF_STRUCT) { d = scf_vector_find_cmp(parse->debug->abbrevs, (void*)DW_TAG_base_type, _debug_abbrev_find_by_tag); if (!d) { @@ -302,7 +302,7 @@ static int __debug_add_type(scf_dwarf_info_entry_t** pie, scf_dwarf_abbrev_decla return -ENOMEM; ie->code = d->code; - ie->type = t->type; + ie->type = t->node.type; ie->nb_pointers = nb_pointers; ret = scf_vector_add(types, ie); @@ -345,19 +345,19 @@ static int __debug_add_type(scf_dwarf_info_entry_t** pie, scf_dwarf_abbrev_decla uint8_t ate; - if (SCF_VAR_CHAR == t->type || SCF_VAR_I8 == t->type) + if (SCF_VAR_CHAR == t->node.type || SCF_VAR_I8 == t->node.type) ate = DW_ATE_signed_char; - else if (scf_type_is_signed(t->type)) + else if (scf_type_is_signed(t->node.type)) ate = DW_ATE_signed; - else if (SCF_VAR_U8 == t->type) + else if (SCF_VAR_U8 == t->node.type) ate = DW_ATE_unsigned_char; - else if (scf_type_is_unsigned(t->type)) + else if (scf_type_is_unsigned(t->node.type)) ate = DW_ATE_unsigned; - else if (scf_type_is_float(t->type)) + else if (scf_type_is_float(t->node.type)) ate = DW_ATE_float; else { scf_loge("\n"); @@ -384,7 +384,7 @@ static int __debug_add_type(scf_dwarf_info_entry_t** pie, scf_dwarf_abbrev_decla } else if (DW_AT_decl_line == iattr->name) { - ret = scf_dwarf_info_fill_attr(iattr, (uint8_t*)&t->w->line, sizeof(t->w->line)); + ret = scf_dwarf_info_fill_attr(iattr, (uint8_t*)&t->node.w->line, sizeof(t->node.w->line)); if (ret < 0) return ret; @@ -634,7 +634,7 @@ static int _debug_add_struct_type(scf_dwarf_info_entry_t** pie, scf_dwarf_abbrev v_member = t->scope->vars->data[i]; - if (v_member->type != t->type) + if (v_member->type != t->node.type) continue; if (v_member->nb_pointers != j) @@ -677,7 +677,7 @@ static int _debug_add_type(scf_dwarf_info_entry_t** pie, scf_parse_t* parse, scf ie = _debug_find_type(parse, t, 0); if (!ie) { - if (t->type < SCF_STRUCT) { + if (t->node.type < SCF_STRUCT) { ret = __debug_add_type(&ie, &d, parse, t, 0, NULL); if (ret < 0) diff --git a/parse/scf_parse.h b/parse/scf_parse.h index 216b915..32ac181 100644 --- a/parse/scf_parse.h +++ b/parse/scf_parse.h @@ -87,11 +87,6 @@ struct dfa_data_s { scf_node_t* current_node; - scf_node_t* current_while; - - scf_node_t* current_for; - scf_vector_t* for_exprs; - scf_node_t* current_return; scf_node_t* current_goto; -- 2.25.1