From f96d996a07420da75fa8b137d674deb20fe495da Mon Sep 17 00:00:00 2001 From: "yu.dongliang" <18588496441@163.com> Date: Tue, 23 Jun 2026 15:30:17 +0800 Subject: [PATCH] mov parse code of 'struct, union, class' to one module --- core/scf_block.c | 4 +- core/scf_function.c | 2 +- parse/Makefile | 1 - parse/scf_dfa_class.c | 312 +++++++++++++++++++++++++++----------- parse/scf_dfa_init_data.c | 104 ++++++++++--- parse/scf_dfa_parse.c | 2 - parse/scf_dfa_type.c | 3 +- parse/scf_dfa_union.c | 312 -------------------------------------- parse/scf_dfa_util.h | 14 ++ parse/scf_parse.c | 14 +- parse/scf_parse.h | 1 + parse/scf_struct_array.c | 8 +- 12 files changed, 339 insertions(+), 438 deletions(-) delete mode 100644 parse/scf_dfa_union.c diff --git a/core/scf_block.c b/core/scf_block.c index 5461b0d..3691b63 100644 --- a/core/scf_block.c +++ b/core/scf_block.c @@ -7,7 +7,7 @@ scf_block_t* scf_block_alloc(scf_lex_word_t* w) if (!b) return NULL; - b->scope = scf_scope_alloc(w, "block"); + b->scope = scf_scope_alloc(); if (!b->scope) { free(b); return NULL; @@ -39,7 +39,7 @@ scf_block_t* scf_block_alloc_cstr(const char* name) return NULL; } - b->scope = scf_scope_alloc(NULL, name); + b->scope = scf_scope_alloc(); if (!b->scope) { scf_string_free(b->name); free(b); diff --git a/core/scf_function.c b/core/scf_function.c index 4fe5bef..ed94c45 100644 --- a/core/scf_function.c +++ b/core/scf_function.c @@ -17,7 +17,7 @@ scf_function_t* scf_function_alloc(scf_lex_word_t* w) scf_list_init(&f->basic_block_list_head); scf_list_init(&f->dag_list_head); - f->scope = scf_scope_alloc(w, "function"); + f->scope = scf_scope_alloc(); if (!f->scope) goto _scope_error; diff --git a/parse/Makefile b/parse/Makefile index 971151c..4f6e832 100644 --- a/parse/Makefile +++ b/parse/Makefile @@ -141,7 +141,6 @@ CFILES += scf_dfa_va_arg.c CFILES += scf_dfa_expr.c CFILES += scf_dfa_enum.c -CFILES += scf_dfa_union.c CFILES += scf_dfa_class.c CFILES += scf_dfa_type.c diff --git a/parse/scf_dfa_class.c b/parse/scf_dfa_class.c index be6d604..e81d27e 100644 --- a/parse/scf_dfa_class.c +++ b/parse/scf_dfa_class.c @@ -5,86 +5,94 @@ extern scf_dfa_module_t dfa_module_class; typedef struct { - scf_lex_word_t* current_identity; - scf_block_t* parent_block; - - scf_type_t* current_class; + scf_type_t* current_type; int nb_lbs; int nb_rbs; - -} class_module_data_t; +} dfa_class_data_t; static int _class_is_class(scf_dfa_t* dfa, void* word) { scf_lex_word_t* w = word; return SCF_LEX_WORD_KEY_CLASS == w->type - || SCF_LEX_WORD_KEY_STRUCT == w->type; + || SCF_LEX_WORD_KEY_STRUCT == w->type + || SCF_LEX_WORD_KEY_UNION == w->type; } -static int _class_action_identity(scf_dfa_t* dfa, scf_vector_t* words, void* data) +static int _class_action_lb(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - dfa_data_t* d = data; - class_module_data_t* md = d->module_datas[dfa_module_class.index]; - scf_lex_word_t* w = words->data[words->size - 1]; + scf_parse_t* parse = dfa->priv; + scf_ast_t* ast = parse->ast; + scf_lex_word_t* w = words->data[words->size - 1]; + scf_lex_word_t* key = words->data[words->size - 2]; + dfa_data_t* d = data; + scf_stack_t* s = d->module_datas[dfa_module_class.index]; + scf_type_t* t = NULL; - if (md->current_identity) { - scf_loge("\n"); - return SCF_DFA_ERROR; - } + if (SCF_LEX_WORD_ID == key->type) { + t = scf_block_find_type(ast->current_block, key->text->data); - scf_type_t* t = scf_block_find_type(parse->ast->current_block, w->text->data); - if (!t) { - t = scf_type_alloc(w, w->text->data, SCF_STRUCT + parse->ast->nb_structs, 0); if (!t) { - scf_loge("\n"); - return SCF_DFA_ERROR; + t = scf_type_alloc(key, key->text->data, SCF_STRUCT + ast->nb_structs, 0); + if (!t) + return SCF_DFA_ERROR; } - parse->ast->nb_structs++; - t->node.class_flag = 1; - scf_scope_push_type(parse->ast->current_block->scope, t); - scf_node_add_child((scf_node_t*)parse->ast->current_block, (scf_node_t*)t); + assert(words->size >= 3); + key = words->data[words->size - 3]; } - md->current_identity = w; - md->parent_block = parse->ast->current_block; - SCF_DFA_PUSH_HOOK(scf_dfa_find_node(dfa, "class_end"), SCF_DFA_HOOK_END); + if (!t) { + char name[256]; + snprintf(name, sizeof(name) - 1, "anonymous_%d", ast->nb_structs); - scf_logi("\033[31m t: %p, t->type: %d\033[0m\n", t, t->node.type); + t = scf_type_alloc(w, name, SCF_STRUCT + ast->nb_structs, 0); + if (!t) + return SCF_DFA_ERROR; + } - return SCF_DFA_NEXT_WORD; -} + if (!t->scope) + t->scope = scf_scope_alloc(); -static int _class_action_lb(scf_dfa_t* dfa, scf_vector_t* words, void* data) -{ - scf_parse_t* parse = dfa->priv; - dfa_data_t* d = data; - class_module_data_t* md = d->module_datas[dfa_module_class.index]; - scf_lex_word_t* w = words->data[words->size - 1]; + scf_scope_push_type(ast->current_block->scope, t); + ast->nb_structs++; - if (!md->current_identity) { - scf_loge("\n"); - return SCF_DFA_ERROR; - } + switch (key->type) + { + case SCF_LEX_WORD_KEY_STRUCT: + case SCF_LEX_WORD_KEY_CLASS: + t->node.class_flag = 1; + break; - scf_type_t* t = scf_block_find_type(parse->ast->current_block, md->current_identity->text->data); - if (!t) { - scf_loge("\n"); - return SCF_DFA_ERROR; + case SCF_LEX_WORD_KEY_UNION: + t->node.union_flag = 1; + break; + default: + return SCF_DFA_ERROR; + break; + }; + + scf_node_add_child((scf_node_t*)ast->current_block, (scf_node_t*)t); + + dfa_class_data_t* cd = calloc(1, sizeof(dfa_class_data_t)); + if (!cd) + return -ENOMEM; + + int ret = scf_stack_push(s, cd); + if (ret < 0) { + free(cd); + return ret; } - if (!t->scope) - t->scope = scf_scope_alloc(w, "class"); + cd->parent_block = ast->current_block; + cd->current_type = t; + cd->nb_lbs++; - md->parent_block = parse->ast->current_block; - md->current_class = t; - md->nb_lbs++; + ast->current_block = (scf_block_t*)t; - parse->ast->current_block = (scf_block_t*)t; + SCF_DFA_PUSH_HOOK(scf_dfa_find_node(dfa, "class_end"), SCF_DFA_HOOK_END); return SCF_DFA_NEXT_WORD; } @@ -186,18 +194,134 @@ static int _class_calculate_size(scf_dfa_t* dfa, scf_type_t* s) return 0; } +static int _union_calculate_size(scf_dfa_t* dfa, scf_type_t* s) +{ + scf_variable_t* v; + + int max_size = 0; + int i; + int j; + + for (i = 0; i < s->scope->vars->size; i++) { + v = s->scope->vars->data[i]; + + assert(v->size >= 0); + + int size = 0; + + if (v->nb_dimentions > 0) { + v->capacity = 1; + + for (j = 0; j < v->nb_dimentions; j++) { + + if (v->dimentions[j].num < 0) { + scf_loge("number of %d-dimention for array '%s' is less than 0, number: %d, file: %s, line: %d\n", + j, v->w->text->data, v->dimentions[j].num, v->w->file->data, v->w->line); + return SCF_DFA_ERROR; + } + + if (0 == v->dimentions[j].num && j < v->nb_dimentions - 1) { + + scf_loge("only the number of array's last dimention can be 0, array '%s', dimention: %d, file: %s, line: %d\n", + v->w->text->data, j, v->w->file->data, v->w->line); + return SCF_DFA_ERROR; + } + + v->capacity *= v->dimentions[j].num; + } + + size = v->size * v->capacity; + } else + size = v->size; + + if (max_size < size) + max_size = size; + + if (s->node.class_flag) + scf_logi("class '%s', member: '%s', size: %d, v->dim: %d, v->capacity: %d\n", + s->name->data, v->w->text->data, v->size, v->nb_dimentions, v->capacity); + else + scf_logi("union '%s', member: '%s', size: %d, v->dim: %d, v->capacity: %d\n", + s->name->data, v->w->text->data, v->size, v->nb_dimentions, v->capacity); + } + s->size = max_size; + + if (s->node.class_flag) + scf_logi("class '%s', size: %d\n", s->name->data, s->size); + else + scf_logi("union '%s', size: %d\n", s->name->data, s->size); + return 0; +} + static int _class_action_rb(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - dfa_data_t* d = data; - class_module_data_t* md = d->module_datas[dfa_module_class.index]; + scf_parse_t* parse = dfa->priv; + scf_ast_t* ast = parse->ast; + dfa_data_t* d = data; + scf_stack_t* s = d->module_datas[dfa_module_class.index]; + dfa_class_data_t* cd = scf_stack_top(s); + scf_lex_word_t* w; + + assert(cd); + + int ret; - if (_class_calculate_size(dfa, md->current_class) < 0) { + if (cd->current_type->node.class_flag) + ret = _class_calculate_size(dfa, cd->current_type); + else + ret = _union_calculate_size(dfa, cd->current_type); + + if (ret < 0) { scf_loge("\n"); return SCF_DFA_ERROR; } - md->nb_rbs++; + cd->nb_rbs++; + + assert(cd->nb_rbs == cd->nb_lbs); + + ast->current_block = cd->parent_block; + cd->parent_block = NULL; + + w = dfa->ops->pop_word(dfa); + dfa->ops->push_word(dfa, w); + + if (SCF_LEX_WORD_ID == w->type || SCF_LEX_WORD_STAR == w->type) { + w = scf_lex_word_clone(w); + if (!w) + return -ENOMEM; + + ret = scf_string_copy(w->text, cd->current_type->name); + if (ret < 0) { + scf_lex_word_free(w); + return ret; + } + + dfa->ops->push_word(dfa, w); + } + + return SCF_DFA_NEXT_WORD; +} + +static int _class_action_var(scf_dfa_t* dfa, scf_vector_t* words, void* data) +{ + scf_parse_t* parse = dfa->priv; + scf_ast_t* ast = parse->ast; + scf_lex_word_t* w = words->data[words->size - 1]; + dfa_data_t* d = data; + scf_stack_t* s = d->module_datas[dfa_module_class.index]; + dfa_class_data_t* cd = scf_stack_top(s); + + if (!cd->current_type) + return SCF_DFA_ERROR; + + scf_variable_t* var = scf_variable_alloc(w, cd->current_type); + if (!var) + return SCF_DFA_ERROR; + + scf_scope_push_var(ast->current_block->scope, var); + + scf_logi("class/union var: '%s', type: %d, size: %d\n", w->text->data, var->type, var->size); return SCF_DFA_NEXT_WORD; } @@ -209,20 +333,17 @@ static int _class_action_semicolon(scf_dfa_t* dfa, scf_vector_t* words, void* da static int _class_action_end(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - scf_parse_t* parse = dfa->priv; - dfa_data_t* d = data; - class_module_data_t* md = d->module_datas[dfa_module_class.index]; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; + scf_stack_t* s = d->module_datas[dfa_module_class.index]; + dfa_class_data_t* cd = scf_stack_top(s); - if (md->nb_rbs == md->nb_lbs) { + if (cd->nb_rbs == cd->nb_lbs) { - parse->ast->current_block = md->parent_block; - - md->current_identity = NULL; - md->current_class = NULL; - md->parent_block = NULL; - md->nb_lbs = 0; - md->nb_rbs = 0; + scf_stack_pop(s); + free(cd); + scf_logi("ok\n"); return SCF_DFA_OK; } @@ -235,38 +356,39 @@ static int _dfa_init_module_class(scf_dfa_t* dfa) { SCF_DFA_MODULE_NODE(dfa, class, _class, _class_is_class, NULL); - SCF_DFA_MODULE_NODE(dfa, class, identity, scf_dfa_is_identity, _class_action_identity); - + SCF_DFA_MODULE_NODE(dfa, class, identity, scf_dfa_is_identity, NULL); SCF_DFA_MODULE_NODE(dfa, class, lb, scf_dfa_is_lb, _class_action_lb); SCF_DFA_MODULE_NODE(dfa, class, rb, scf_dfa_is_rb, _class_action_rb); - SCF_DFA_MODULE_NODE(dfa, class, semicolon, scf_dfa_is_semicolon, _class_action_semicolon); SCF_DFA_MODULE_NODE(dfa, class, end, scf_dfa_is_entry, _class_action_end); - scf_parse_t* parse = dfa->priv; - dfa_data_t* d = parse->dfa_data; - class_module_data_t* md = d->module_datas[dfa_module_class.index]; + SCF_DFA_MODULE_NODE(dfa, class, var, scf_dfa_is_identity, _class_action_var); - assert(!md); + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = parse->dfa_data; + scf_stack_t* s = d->module_datas[dfa_module_class.index]; - md = calloc(1, sizeof(class_module_data_t)); - if (!md) + assert(!s); + + s = scf_stack_alloc(); + if (!s) return SCF_DFA_ERROR; - d->module_datas[dfa_module_class.index] = md; + d->module_datas[dfa_module_class.index] = s; return SCF_DFA_OK; } static int _dfa_fini_module_class(scf_dfa_t* dfa) { - scf_parse_t* parse = dfa->priv; - dfa_data_t* d = parse->dfa_data; - class_module_data_t* md = d->module_datas[dfa_module_class.index]; - - if (md) { - free(md); - md = NULL; + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = parse->dfa_data; + scf_stack_t* s = d->module_datas[dfa_module_class.index]; + + if (s) { + scf_vector_clear(s, ( void (*)(void*) )free); + scf_stack_free(s); + s = NULL; d->module_datas[dfa_module_class.index] = NULL; } @@ -276,32 +398,38 @@ static int _dfa_fini_module_class(scf_dfa_t* dfa) static int _dfa_init_syntax_class(scf_dfa_t* dfa) { SCF_DFA_GET_MODULE_NODE(dfa, class, _class, _class); - SCF_DFA_GET_MODULE_NODE(dfa, class, identity, identity); + SCF_DFA_GET_MODULE_NODE(dfa, class, identity, type_name); SCF_DFA_GET_MODULE_NODE(dfa, class, lb, lb); SCF_DFA_GET_MODULE_NODE(dfa, class, rb, rb); - SCF_DFA_GET_MODULE_NODE(dfa, class, semicolon, semicolon); SCF_DFA_GET_MODULE_NODE(dfa, class, end, end); + SCF_DFA_GET_MODULE_NODE(dfa, class, var, var); + SCF_DFA_GET_MODULE_NODE(dfa, class, semicolon, semicolon); + SCF_DFA_GET_MODULE_NODE(dfa, type, entry, member); - SCF_DFA_GET_MODULE_NODE(dfa, union, _union, _union); scf_vector_add(dfa->syntaxes, _class); // class start - scf_dfa_node_add_child(_class, identity); + scf_dfa_node_add_child(_class, type_name); + + scf_dfa_node_add_child(type_name, semicolon); + scf_dfa_node_add_child(type_name, lb); - scf_dfa_node_add_child(identity, semicolon); - scf_dfa_node_add_child(identity, lb); + // anonymous class + scf_dfa_node_add_child(_class, lb); + // empty class scf_dfa_node_add_child(lb, rb); - scf_dfa_node_add_child(lb, _union); + // member var scf_dfa_node_add_child(lb, member); scf_dfa_node_add_child(member, rb); scf_dfa_node_add_child(rb, semicolon); - scf_dfa_node_add_child(end, _union); + // end scf_dfa_node_add_child(end, member); + scf_dfa_node_add_child(end, semicolon); scf_dfa_node_add_child(end, rb); return 0; diff --git a/parse/scf_dfa_init_data.c b/parse/scf_dfa_init_data.c index da6255f..2453874 100644 --- a/parse/scf_dfa_init_data.c +++ b/parse/scf_dfa_init_data.c @@ -13,12 +13,17 @@ typedef struct { scf_lex_word_t* assign; scf_vector_t* init_exprs; + scf_type_t* current_type; dfa_index_t* current_index; int current_n; int current_dim; int nb_lbs; int nb_rbs; + + int nb_lss; + int nb_rss; + int nb_dots; } init_module_data_t; static int _do_data_init(scf_dfa_t* dfa, scf_vector_t* words, dfa_data_t* d) @@ -81,9 +86,15 @@ error: free(md->current_index); md->current_index = NULL; - md->current_dim = -1; - md->nb_lbs = 0; - md->nb_rbs = 0; + md->current_type = NULL; + md->current_dim = -1; + + md->nb_lbs = 0; + md->nb_rbs = 0; + + md->nb_lss = 0; + md->nb_rss = 0; + md->nb_dots = 0; return ret; } @@ -175,15 +186,12 @@ static int _data_action_entry(scf_dfa_t* dfa, scf_vector_t* words, void* data) return SCF_DFA_CONTINUE; } -static int _data_action_lb(scf_dfa_t* dfa, scf_vector_t* words, void* data) +static int __data_add_dim(dfa_data_t* d) { - scf_parse_t* parse = dfa->priv; - dfa_data_t* d = data; - init_module_data_t* md = d->module_datas[dfa_module_init_data.index]; + init_module_data_t* md = d->module_datas[dfa_module_init_data.index]; d->expr = NULL; md->current_dim++; - md->nb_lbs++; if (md->current_dim >= md->current_n) { @@ -204,6 +212,48 @@ static int _data_action_lb(scf_dfa_t* dfa, scf_vector_t* words, void* data) return SCF_DFA_NEXT_WORD; } +static int _data_action_lb(scf_dfa_t* dfa, scf_vector_t* words, void* data) +{ + dfa_data_t* d = data; + init_module_data_t* md = d->module_datas[dfa_module_init_data.index]; + + md->nb_lbs++; + + return __data_add_dim(d); +} + +static int _data_action_dot(scf_dfa_t* dfa, scf_vector_t* words, void* data) +{ + dfa_data_t* d = data; + init_module_data_t* md = d->module_datas[dfa_module_init_data.index]; + + if (md->nb_dots++ > 0) + return __data_add_dim(d); + + return SCF_DFA_NEXT_WORD; +} + +static int _data_action_ls(scf_dfa_t* dfa, scf_vector_t* words, void* data) +{ + dfa_data_t* d = data; + init_module_data_t* md = d->module_datas[dfa_module_init_data.index]; + + if (md->nb_lss++ > 0) + return __data_add_dim(d); + + return SCF_DFA_NEXT_WORD; +} + +static int _data_action_rs(scf_dfa_t* dfa, scf_vector_t* words, void* data) +{ + dfa_data_t* d = data; + init_module_data_t* md = d->module_datas[dfa_module_init_data.index]; + + md->nb_rss++; + + return SCF_DFA_NEXT_WORD; +} + static int _data_action_member(scf_dfa_t* dfa, scf_vector_t* words, void* data) { scf_parse_t* parse = dfa->priv; @@ -220,12 +270,17 @@ static int _data_action_member(scf_dfa_t* dfa, scf_vector_t* words, void* data) assert(d->current_var); - t = NULL; - scf_ast_find_type_type(&t, parse->ast, d->current_var->type); - if (!t->scope) { - scf_loge("base type '%s' has no member var '%s', file: %s, line: %d\n", - t->name->data, w->text->data, w->file->data, w->line); - return SCF_DFA_ERROR; + t = md->current_type; + if (!t) { + int ret = scf_ast_find_type_type(&t, parse->ast, d->current_var->type); + if (ret < 0) + return ret; + + if (!t->scope) { + scf_loge("base type '%s' has no member var '%s', file: %s, line: %d\n", + t->name->data, w->text->data, w->file->data, w->line); + return SCF_DFA_ERROR; + } } v = scf_scope_find_variable(t->scope, w->text->data); @@ -235,6 +290,20 @@ static int _data_action_member(scf_dfa_t* dfa, scf_vector_t* words, void* data) return SCF_DFA_ERROR; } + if (v->type >= SCF_STRUCT) { + t = scf_scope_find_type_type(t->scope, v->type); + + if (!t) { + int ret = scf_ast_find_type_type(&t, parse->ast, v->type); + if (ret < 0) + return ret; + if (!t) + return SCF_DFA_ERROR; + } + + md->current_type = t; + } + md->current_index[md->current_dim].w = w; return SCF_DFA_NEXT_WORD; @@ -322,10 +391,10 @@ static int _dfa_init_module_init_data(scf_dfa_t* dfa) SCF_DFA_MODULE_NODE(dfa, init_data, lb, scf_dfa_is_lb, _data_action_lb); SCF_DFA_MODULE_NODE(dfa, init_data, rb, scf_dfa_is_rb, _data_action_rb); - SCF_DFA_MODULE_NODE(dfa, init_data, ls, scf_dfa_is_ls, scf_dfa_action_next); - SCF_DFA_MODULE_NODE(dfa, init_data, rs, scf_dfa_is_rs, scf_dfa_action_next); + SCF_DFA_MODULE_NODE(dfa, init_data, ls, scf_dfa_is_ls, _data_action_ls); + SCF_DFA_MODULE_NODE(dfa, init_data, rs, scf_dfa_is_rs, _data_action_rs); - SCF_DFA_MODULE_NODE(dfa, init_data, dot, scf_dfa_is_dot, scf_dfa_action_next); + SCF_DFA_MODULE_NODE(dfa, init_data, dot, scf_dfa_is_dot, _data_action_dot); SCF_DFA_MODULE_NODE(dfa, init_data, member, scf_dfa_is_identity, _data_action_member); SCF_DFA_MODULE_NODE(dfa, init_data, index, scf_dfa_is_const_integer, _data_action_index); SCF_DFA_MODULE_NODE(dfa, init_data, assign, scf_dfa_is_assign, scf_dfa_action_next); @@ -390,6 +459,7 @@ static int _dfa_init_syntax_init_data(scf_dfa_t* dfa) scf_dfa_node_add_child(expr, rb); scf_dfa_node_add_child(dot, member); + scf_dfa_node_add_child(member, dot); scf_dfa_node_add_child(member, assign); scf_dfa_node_add_child(assign, expr); diff --git a/parse/scf_dfa_parse.c b/parse/scf_dfa_parse.c index aa94ddd..cd8b1ba 100644 --- a/parse/scf_dfa_parse.c +++ b/parse/scf_dfa_parse.c @@ -15,7 +15,6 @@ extern scf_dfa_module_t dfa_module_init_data; extern scf_dfa_module_t dfa_module_va_arg; extern scf_dfa_module_t dfa_module_enum; -extern scf_dfa_module_t dfa_module_union; extern scf_dfa_module_t dfa_module_class; extern scf_dfa_module_t dfa_module_type; @@ -57,7 +56,6 @@ scf_dfa_module_t* dfa_modules[] = &dfa_module_va_arg, &dfa_module_enum, - &dfa_module_union, &dfa_module_class, &dfa_module_type, diff --git a/parse/scf_dfa_type.c b/parse/scf_dfa_type.c index 7e72383..d2752f3 100644 --- a/parse/scf_dfa_type.c +++ b/parse/scf_dfa_type.c @@ -9,7 +9,8 @@ static int _type_is__struct(scf_dfa_t* dfa, void* word) scf_lex_word_t* w = word; return SCF_LEX_WORD_KEY_CLASS == w->type - || SCF_LEX_WORD_KEY_STRUCT == w->type; + || SCF_LEX_WORD_KEY_STRUCT == w->type + || SCF_LEX_WORD_KEY_UNION == w->type; } static int _type_action_const(scf_dfa_t* dfa, scf_vector_t* words, void* data) diff --git a/parse/scf_dfa_union.c b/parse/scf_dfa_union.c deleted file mode 100644 index f9255e0..0000000 --- a/parse/scf_dfa_union.c +++ /dev/null @@ -1,312 +0,0 @@ -#include"scf_dfa.h" -#include"scf_dfa_util.h" -#include"scf_parse.h" - -extern scf_dfa_module_t dfa_module_union; - -typedef struct { - scf_lex_word_t* current_identity; - - scf_block_t* parent_block; - - scf_type_t* current_union; - - scf_dfa_hook_t* hook; - - int nb_lbs; - int nb_rbs; - -} union_module_data_t; - -static int _union_action_identity(scf_dfa_t* dfa, scf_vector_t* words, void* data) -{ - scf_parse_t* parse = dfa->priv; - dfa_data_t* d = data; - union_module_data_t* md = d->module_datas[dfa_module_union.index]; - scf_lex_word_t* w = words->data[words->size - 1]; - - if (md->current_identity) { - scf_loge("\n"); - return SCF_DFA_ERROR; - } - - scf_type_t* t = scf_block_find_type(parse->ast->current_block, w->text->data); - if (!t) { - t = scf_type_alloc(w, w->text->data, SCF_STRUCT + parse->ast->nb_structs, 0); - if (!t) { - scf_loge("type alloc failed\n"); - return SCF_DFA_ERROR; - } - - parse->ast->nb_structs++; - t->node.union_flag = 1; - scf_scope_push_type(parse->ast->current_block->scope, t); - scf_node_add_child((scf_node_t*)parse->ast->current_block, (scf_node_t*)t); - } - - md->current_identity = w; - md->parent_block = parse->ast->current_block; - - return SCF_DFA_NEXT_WORD; -} - -static int _union_action_lb(scf_dfa_t* dfa, scf_vector_t* words, void* data) -{ - scf_parse_t* parse = dfa->priv; - dfa_data_t* d = data; - union_module_data_t* md = d->module_datas[dfa_module_union.index]; - scf_lex_word_t* w = words->data[words->size - 1]; - scf_type_t* t = NULL; - - if (md->current_identity) { - - t = scf_block_find_type(parse->ast->current_block, md->current_identity->text->data); - if (!t) { - scf_loge("type '%s' not found\n", md->current_identity->text->data); - return SCF_DFA_ERROR; - } - } else { - t = scf_type_alloc(w, "anonymous", SCF_STRUCT + parse->ast->nb_structs, 0); - if (!t) { - scf_loge("type alloc failed\n"); - return SCF_DFA_ERROR; - } - - parse->ast->nb_structs++; - t->node.union_flag = 1; - scf_scope_push_type(parse->ast->current_block->scope, t); - scf_node_add_child((scf_node_t*)parse->ast->current_block, (scf_node_t*)t); - } - - if (!t->scope) - t->scope = scf_scope_alloc(w, "union"); - - md->parent_block = parse->ast->current_block; - md->current_union = t; - md->nb_lbs++; - - parse->ast->current_block = (scf_block_t*)t; - - md->hook = SCF_DFA_PUSH_HOOK(scf_dfa_find_node(dfa, "union_semicolon"), SCF_DFA_HOOK_POST); - - return SCF_DFA_NEXT_WORD; -} - -static int _union_calculate_size(scf_dfa_t* dfa, scf_type_t* s) -{ - scf_variable_t* v; - - int max_size = 0; - int i; - int j; - - for (i = 0; i < s->scope->vars->size; i++) { - v = s->scope->vars->data[i]; - - assert(v->size >= 0); - - int size = 0; - - if (v->nb_dimentions > 0) { - v->capacity = 1; - - for (j = 0; j < v->nb_dimentions; j++) { - - if (v->dimentions[j].num < 0) { - scf_loge("number of %d-dimention for array '%s' is less than 0, number: %d, file: %s, line: %d\n", - j, v->w->text->data, v->dimentions[j].num, v->w->file->data, v->w->line); - return SCF_DFA_ERROR; - } - - if (0 == v->dimentions[j].num && j < v->nb_dimentions - 1) { - - scf_loge("only the number of array's last dimention can be 0, array '%s', dimention: %d, file: %s, line: %d\n", - v->w->text->data, j, v->w->file->data, v->w->line); - return SCF_DFA_ERROR; - } - - v->capacity *= v->dimentions[j].num; - } - - size = v->size * v->capacity; - } else - size = v->size; - - if (max_size < size) - max_size = size; - - scf_logi("union '%s', member: '%s', size: %d, v->dim: %d, v->capacity: %d\n", - s->name->data, v->w->text->data, v->size, v->nb_dimentions, v->capacity); - } - s->size = max_size; - - scf_logi("union '%s', size: %d\n", s->name->data, s->size); - return 0; -} - -static int _union_action_rb(scf_dfa_t* dfa, scf_vector_t* words, void* data) -{ - scf_parse_t* parse = dfa->priv; - dfa_data_t* d = data; - union_module_data_t* md = d->module_datas[dfa_module_union.index]; - - if (_union_calculate_size(dfa, md->current_union) < 0) { - scf_loge("\n"); - return SCF_DFA_ERROR; - } - - md->nb_rbs++; - - assert(md->nb_rbs == md->nb_lbs); - - parse->ast->current_block = md->parent_block; - md->parent_block = NULL; - - return SCF_DFA_NEXT_WORD; -} - -static int _union_action_var(scf_dfa_t* dfa, scf_vector_t* words, void* data) -{ - scf_parse_t* parse = dfa->priv; - dfa_data_t* d = data; - union_module_data_t* md = d->module_datas[dfa_module_union.index]; - scf_lex_word_t* w = words->data[words->size - 1]; - - if (!md->current_union) { - scf_loge("\n"); - return SCF_DFA_ERROR; - } - - scf_variable_t* var = scf_variable_alloc(w, md->current_union); - if (!var) { - scf_loge("var alloc failed\n"); - return SCF_DFA_ERROR; - } - - scf_scope_push_var(parse->ast->current_block->scope, var); - - scf_logi("union var: '%s', type: %d, size: %d\n", w->text->data, var->type, var->size); - - return SCF_DFA_NEXT_WORD; -} - -static int _union_action_semicolon(scf_dfa_t* dfa, scf_vector_t* words, void* data) -{ - scf_parse_t* parse = dfa->priv; - dfa_data_t* d = data; - union_module_data_t* md = d->module_datas[dfa_module_union.index]; - - if (md->nb_rbs == md->nb_lbs) { - scf_logi("SCF_DFA_OK\n"); - -// parse->ast->current_block = md->parent_block; -// md->parent_block = NULL; - - md->current_identity = NULL; - md->current_union = NULL; - md->nb_lbs = 0; - md->nb_rbs = 0; - - scf_dfa_del_hook(&(dfa->hooks[SCF_DFA_HOOK_POST]), md->hook); - md->hook = NULL; - - return SCF_DFA_OK; - } - - md->hook = SCF_DFA_PUSH_HOOK(scf_dfa_find_node(dfa, "union_semicolon"), SCF_DFA_HOOK_POST); - - scf_logi("\n"); - return SCF_DFA_SWITCH_TO; -} - -static int _dfa_init_module_union(scf_dfa_t* dfa) -{ - SCF_DFA_MODULE_NODE(dfa, union, _union, scf_dfa_is_union, NULL); - - SCF_DFA_MODULE_NODE(dfa, union, identity, scf_dfa_is_identity, _union_action_identity); - - SCF_DFA_MODULE_NODE(dfa, union, lb, scf_dfa_is_lb, _union_action_lb); - SCF_DFA_MODULE_NODE(dfa, union, rb, scf_dfa_is_rb, _union_action_rb); - SCF_DFA_MODULE_NODE(dfa, union, semicolon, scf_dfa_is_semicolon, _union_action_semicolon); - - SCF_DFA_MODULE_NODE(dfa, union, var, scf_dfa_is_identity, _union_action_var); - - scf_parse_t* parse = dfa->priv; - dfa_data_t* d = parse->dfa_data; - union_module_data_t* md = d->module_datas[dfa_module_union.index]; - - assert(!md); - - md = calloc(1, sizeof(union_module_data_t)); - if (!md) { - scf_loge("\n"); - return SCF_DFA_ERROR; - } - - d->module_datas[dfa_module_union.index] = md; - - return SCF_DFA_OK; -} - -static int _dfa_fini_module_union(scf_dfa_t* dfa) -{ - scf_parse_t* parse = dfa->priv; - dfa_data_t* d = parse->dfa_data; - union_module_data_t* md = d->module_datas[dfa_module_union.index]; - - if (md) { - free(md); - md = NULL; - d->module_datas[dfa_module_union.index] = NULL; - } - - return SCF_DFA_OK; -} - -static int _dfa_init_syntax_union(scf_dfa_t* dfa) -{ - SCF_DFA_GET_MODULE_NODE(dfa, union, _union, _union); - SCF_DFA_GET_MODULE_NODE(dfa, union, identity, identity); - SCF_DFA_GET_MODULE_NODE(dfa, union, lb, lb); - SCF_DFA_GET_MODULE_NODE(dfa, union, rb, rb); - SCF_DFA_GET_MODULE_NODE(dfa, union, semicolon, semicolon); - SCF_DFA_GET_MODULE_NODE(dfa, union, var, var); - - SCF_DFA_GET_MODULE_NODE(dfa, type, entry, member); - - scf_vector_add(dfa->syntaxes, _union); - - // union start - scf_dfa_node_add_child(_union, identity); - - scf_dfa_node_add_child(identity, semicolon); - scf_dfa_node_add_child(identity, lb); - - // anonymous union, will be only used in struct or class to define vars shared the same memory - scf_dfa_node_add_child(_union, lb); - - // empty union - scf_dfa_node_add_child(lb, rb); - - // member var - scf_dfa_node_add_child(lb, member); - scf_dfa_node_add_child(member, semicolon); - scf_dfa_node_add_child(semicolon, rb); - scf_dfa_node_add_child(semicolon, member); - - // end - scf_dfa_node_add_child(rb, var); - scf_dfa_node_add_child(var, semicolon); - scf_dfa_node_add_child(rb, semicolon); - - return 0; -} - -scf_dfa_module_t dfa_module_union = -{ - .name = "union", - .init_module = _dfa_init_module_union, - .init_syntax = _dfa_init_syntax_union, - - .fini_module = _dfa_fini_module_union, -}; diff --git a/parse/scf_dfa_util.h b/parse/scf_dfa_util.h index 0415a25..d007d15 100644 --- a/parse/scf_dfa_util.h +++ b/parse/scf_dfa_util.h @@ -88,6 +88,20 @@ static inline int scf_dfa_is_semicolon(scf_dfa_t* dfa, void* word) return SCF_LEX_WORD_SEMICOLON == w->type; } +static inline int scf_dfa_is_typedef(scf_dfa_t* dfa, void* word) +{ + scf_lex_word_t* w = word; + + return SCF_LEX_WORD_KEY_TYPEDEF == w->type; +} + +static inline int scf_dfa_is_typeof(scf_dfa_t* dfa, void* word) +{ + scf_lex_word_t* w = word; + + return SCF_LEX_WORD_KEY_TYPEOF == w->type; +} + static inline int scf_dfa_is_enum(scf_dfa_t* dfa, void* word) { scf_lex_word_t* w = word; diff --git a/parse/scf_parse.c b/parse/scf_parse.c index a768b6b..76d5120 100644 --- a/parse/scf_parse.c +++ b/parse/scf_parse.c @@ -1281,7 +1281,7 @@ static int _fill_function_inst(scf_string_t* code, scf_function_t* f, int64_t of #if 1 if (f->code_bytes & 0x7) { - size_t n = 8 - (f->code_bytes & 0x7); + int n = 8 - (f->code_bytes & 0x7); ret = scf_string_fill_zero(code, n); if (ret < 0) @@ -1355,9 +1355,7 @@ static int _fill_data(scf_parse_t* parse, scf_variable_t* v, scf_string_t* data, } // align 8 bytes - int fill_size = (data->len + 7) >> 3 << 3; - fill_size -= data->len; - + int fill_size = ((data->len + 7) & ~0x7ull) - data->len; int ret; if (fill_size > 0) { @@ -1528,7 +1526,7 @@ static int _scf_parse_add_data_relas(scf_parse_t* parse, scf_elf_context_t* elf) } } - int fill_size = ((rodata->len + 7) >> 3 << 3) - rodata->len; + int fill_size = ((rodata->len + 7) & ~0x7ull) - rodata->len; if (fill_size > 0) { ret = scf_string_fill_zero(rodata, fill_size); @@ -1606,13 +1604,11 @@ static int _scf_parse_add_ds(scf_parse_t* parse, scf_elf_context_t* elf, scf_vec goto error; } - int fill_size = ((data->len + 7) >> 3 << 3) - data->len; + int fill_size = ((data->len + 7) & ~0x7ull) - data->len; if (fill_size > 0) { ret = scf_string_fill_zero(data, fill_size); - if (ret < 0) { - ret = -ENOMEM; + if (ret < 0) goto error; - } } assert(0 == (data->len & 0x7)); diff --git a/parse/scf_parse.h b/parse/scf_parse.h index 458e691..051ff1c 100644 --- a/parse/scf_parse.h +++ b/parse/scf_parse.h @@ -92,6 +92,7 @@ struct dfa_data_s { scf_node_t* current_va_arg; scf_node_t* current_va_end; + uint32_t typedef_flag:1; uint32_t const_flag :1; uint32_t extern_flag:1; uint32_t static_flag:1; diff --git a/parse/scf_struct_array.c b/parse/scf_struct_array.c index 8e8c720..7e87c72 100644 --- a/parse/scf_struct_array.c +++ b/parse/scf_struct_array.c @@ -117,6 +117,8 @@ int scf_struct_member_init(scf_ast_t* ast, scf_lex_word_t* w, scf_variable_t* _s return -1; } + scf_logi("j: %d, n: %d, t: %s\n", j, n, t->name->data); + int k; if (index[j].w) { @@ -126,8 +128,12 @@ int scf_struct_member_init(scf_ast_t* ast, scf_lex_word_t* w, scf_variable_t* _s if (v->w && !strcmp(index[j].w->text->data, v->w->text->data)) break; } - } else + + scf_logi("t: %s, index[j].w: %s\n", t->name->data, index[j].w->text->data); + } else { k = index[j].i; + scf_logi("\n"); + } if (k >= t->scope->vars->size) { scf_loge("\n"); -- 2.25.1