From: yu.dongliang <18588496441@163.com> Date: Thu, 3 Oct 2024 06:07:56 +0000 (+0800) Subject: for member array[0] in the end of a struct X-Git-Url: http://baseworks.info/?a=commitdiff_plain;h=b4399db0826f15b59fba7aded9731b08cdcb0fec;p=scf.git for member array[0] in the end of a struct --- diff --git a/core/scf_optimizer_auto_gc_find.c b/core/scf_optimizer_auto_gc_find.c index 7fdf623..45b9767 100644 --- a/core/scf_optimizer_auto_gc_find.c +++ b/core/scf_optimizer_auto_gc_find.c @@ -897,7 +897,7 @@ static int _bfs_sort_function(scf_vector_t* fqueue, scf_function_t* fmalloc) if (f->visited_flag) continue; - scf_loge("f: %p, %s\n", f, f->node.w->text->data); + scf_logd("f: %p, %s\n", f, f->node.w->text->data); f->visited_flag = 1; @@ -1090,7 +1090,7 @@ static int _auto_gc_global_find(scf_ast_t* ast, scf_vector_t* functions) total1 += ret; } - scf_loge("total0: %d, total1: %d\n", total0, total1); + scf_logi("total0: %d, total1: %d\n", total0, total1); } while (total0 != total1); diff --git a/examples/member_array0.c b/examples/member_array0.c new file mode 100644 index 0000000..0c2f233 --- /dev/null +++ b/examples/member_array0.c @@ -0,0 +1,24 @@ +#include"../lib/scf_capi.c" + +struct mat +{ + int refs; + int n; + int data[0]; +}; + +int main() +{ + mat* m = calloc(1, sizeof(mat) + 4 * sizeof(int)); + + m->data[0] = 1; + m->data[1] = 2; + m->data[2] = 3; + m->data[3] = 4; + + int i; + for (i = 0; i < 4; i++) + printf("%d\n", m->data[i]); + + return 0; +} diff --git a/lex/scf_lex.c b/lex/scf_lex.c index 71d8391..8aed106 100644 --- a/lex/scf_lex.c +++ b/lex/scf_lex.c @@ -117,9 +117,10 @@ int scf_lex_open(scf_lex_t** plex, const char* path) lex->fp = fopen(path, "r"); if (!lex->fp) { + char cwd[4096]; getcwd(cwd, 4095); - scf_loge("path: %s, errno: %d, pwd: %s\n", path, errno, cwd); + scf_loge("open file '%s' failed, errno: %d, default path dir: %s\n", path, errno, cwd); free(lex); return -1; diff --git a/parse/scf_dfa.c b/parse/scf_dfa.c index 8bc897b..ea01777 100644 --- a/parse/scf_dfa.c +++ b/parse/scf_dfa.c @@ -444,6 +444,7 @@ int scf_dfa_parse_word(scf_dfa_t* dfa, void* word, void* data) assert(words->size >= 1); scf_lex_word_t* w = words->data[words->size - 1]; + scf_loge("ret: %d, w->type: %d, '%s', line: %d\n\n", ret, w->type, w->text->data, w->line); ret = SCF_DFA_ERROR; diff --git a/parse/scf_dfa_class.c b/parse/scf_dfa_class.c index 9531048..669d517 100644 --- a/parse/scf_dfa_class.c +++ b/parse/scf_dfa_class.c @@ -91,52 +91,53 @@ static int _class_action_lb(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _class_calculate_size(scf_dfa_t* dfa, scf_type_t* s) { - scf_parse_t* parse = dfa->priv; + scf_variable_t* v; int size = 0; int i; - for (i = 0; i < s->scope->vars->size; i++) { - - scf_variable_t* v = s->scope->vars->data[i]; - - if (v->size < 0) { - scf_loge("error: sizeof var: '%s'\n", v->w->text->data); - return SCF_DFA_ERROR; - } + int j; - int align; - if (1 == v->size) - align = 1; - else if (2 == v->size) - align = 2; - else if (v->size <= 4) - align = 4; - else - align = 8; - - v->offset = (size + align - 1) / align * align; + for (i = 0; i < s->scope->vars->size; i++) { + v = s->scope->vars->data[i]; + + assert(v->size >= 0); + + switch (v->size) { + case 1: + v->offset = size; + break; + case 2: + v->offset = (size + 1) & ~0x1; + break; + case 3: + case 4: + v->offset = (size + 3) & ~0x3; + break; + default: + v->offset = (size + 7) & ~0x7; + break; + }; if (v->nb_dimentions > 0) { - int j; - int capacity = 1; + v->capacity = 1; for (j = 0; j < v->nb_dimentions; j++) { + if (v->dimentions[j] < 0) { - scf_loge("v: '%s'\n", v->w->text->data); + scf_loge("number of %d-dimention for array '%s' is less than 0, size: %d, file: %s, line: %d\n", + j, v->w->text->data, v->dimentions[j], v->w->file->data, v->w->line); return SCF_DFA_ERROR; } - capacity *= v->dimentions[j]; + v->capacity *= v->dimentions[j]; } - v->capacity = capacity; size = v->offset + v->size * v->capacity; - } else { + } else size = v->offset + v->size; - } - scf_logi("class '%s', member: '%s', offset: %d, size: %d, v->dim: %d, v->capacity: %d\n", - s->name->data, v->w->text->data, v->offset, v->size, v->nb_dimentions, v->capacity); + scf_logi("class '%s', member: '%s', member_flag: %d, offset: %d, size: %d, v->dim: %d, v->capacity: %d\n", + s->name->data, v->w->text->data, v->member_flag, v->offset, v->size, v->nb_dimentions, v->capacity); } s->size = size; s->node.define_flag = 1; diff --git a/parse/scf_dfa_expr.c b/parse/scf_dfa_expr.c index a55b24c..53827a8 100644 --- a/parse/scf_dfa_expr.c +++ b/parse/scf_dfa_expr.c @@ -115,6 +115,8 @@ int _expr_add_var(scf_parse_t* parse, dfa_data_t* d) var->const_literal_flag = 1; } + scf_loge("var: %s, member_flag: %d, line: %d\n", var->w->text->data, var->member_flag, var->w->line); + node = scf_node_alloc(w, var->type, var); if (!node) { scf_loge("var node '%s' alloc failed\n", w->text->data); diff --git a/parse/scf_dfa_for.c b/parse/scf_dfa_for.c index 9a7eeb2..fa243ed 100644 --- a/parse/scf_dfa_for.c +++ b/parse/scf_dfa_for.c @@ -32,26 +32,21 @@ static int _for_action_for(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]; + dfa_for_data_t* fd = NULL; scf_stack_t* s = d->module_datas[dfa_module_for.index]; - scf_node_t* _for = scf_node_alloc(w, SCF_OP_FOR, NULL); + scf_node_t* _for = scf_node_alloc(w, SCF_OP_FOR, NULL); - if (!_for) { - scf_loge("node alloc failed\n"); - return SCF_DFA_ERROR; - } + if (!_for) + return -ENOMEM; - dfa_for_data_t* fd = calloc(1, sizeof(dfa_for_data_t)); - if (!fd) { - scf_loge("module data alloc failed\n"); - return SCF_DFA_ERROR; - } + fd = calloc(1, sizeof(dfa_for_data_t)); + if (!fd) + return -ENOMEM; if (d->current_node) scf_node_add_child(d->current_node, _for); - else { + else scf_node_add_child((scf_node_t*)parse->ast->current_block, _for); - scf_loge("current_block: %p\n", parse->ast->current_block); - } fd->parent_block = parse->ast->current_block; fd->parent_node = d->current_node; @@ -104,7 +99,7 @@ static int _for_action_comma(scf_dfa_t* dfa, scf_vector_t* words, void* data) dfa_for_data_t* fd = scf_stack_top(s); if (!d->expr) { - scf_loge("need expr before ','\n"); + scf_loge("need expr before ',' in file: %s, line: %d\n", w->file->data, w->line); return SCF_DFA_ERROR; } @@ -126,7 +121,7 @@ static int _for_action_comma(scf_dfa_t* dfa, scf_vector_t* words, void* data) scf_vector_add(fd->update_exprs, d->expr); d->expr = NULL; } else { - scf_loge("too many ';' in for\n"); + scf_loge("too many ';' in for, file: %s, line: %d\n", w->file->data, w->line); return SCF_DFA_ERROR; } @@ -138,10 +133,8 @@ static int _for_action_comma(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _for_action_semicolon(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - if (!data) { - scf_loge("\n"); + if (!data) return SCF_DFA_ERROR; - } scf_parse_t* parse = dfa->priv; dfa_data_t* d = data; @@ -163,7 +156,7 @@ static int _for_action_semicolon(scf_dfa_t* dfa, scf_vector_t* words, void* data d->expr = NULL; } } else { - scf_loge("too many ';' in for\n"); + scf_loge("too many ';' in for, file: %s, line: %d\n", w->file->data, w->line); return SCF_DFA_ERROR; } @@ -254,7 +247,7 @@ static int _for_action_rp(scf_dfa_t* dfa, scf_vector_t* words, void* data) scf_vector_add(fd->update_exprs, d->expr); d->expr = NULL; } else { - scf_loge("too many ';' in for\n"); + scf_loge("too many ';' in for, file: %s, line: %d\n", w->file->data, w->line); return SCF_DFA_ERROR; } diff --git a/parse/scf_dfa_include.c b/parse/scf_dfa_include.c index 141bf42..c25e748 100644 --- a/parse/scf_dfa_include.c +++ b/parse/scf_dfa_include.c @@ -10,7 +10,6 @@ static int _include_action_include(scf_dfa_t* dfa, scf_vector_t* words, void* da dfa_data_t* d = data; scf_lex_word_t* w = words->data[words->size - 1]; - scf_loge("include '%s', line %d\n", w->text->data, w->line); return SCF_DFA_NEXT_WORD; } @@ -23,27 +22,30 @@ static int _include_action_path(scf_dfa_t* dfa, scf_vector_t* words, void* data) scf_block_t* cur = parse->ast->current_block; assert(w->data.s); - scf_loge("include '%s', line %d\n", w->data.s->data, w->line); + scf_logd("include '%s', line %d\n", w->data.s->data, w->line); parse->lex = NULL; parse->ast->current_block = parse->ast->root_block; int ret = scf_parse_file(parse, w->data.s->data); if (ret < 0) { - scf_loge("\n"); - return SCF_DFA_ERROR; + scf_loge("parse file '%s' failed, 'include' line: %d\n", w->data.s->data, w->line); + goto error; } if (parse->lex != lex && parse->lex->macros) { // copy macros if (!lex->macros) { lex->macros = scf_vector_clone(parse->lex->macros); - if (!lex->macros) - return -ENOMEM; + + if (!lex->macros) { + ret = -ENOMEM; + goto error; + } } else { ret = scf_vector_cat(lex->macros, parse->lex->macros); if (ret < 0) - return ret; + goto error; } scf_macro_t* m; @@ -54,10 +56,11 @@ static int _include_action_path(scf_dfa_t* dfa, scf_vector_t* words, void* data) } } + ret = SCF_DFA_NEXT_WORD; +error: parse->lex = lex; parse->ast->current_block = cur; - - return SCF_DFA_NEXT_WORD; + return ret; } static int _include_action_LF(scf_dfa_t* dfa, scf_vector_t* words, void* data) diff --git a/parse/scf_dfa_sizeof.c b/parse/scf_dfa_sizeof.c index df6366f..aec0be7 100644 --- a/parse/scf_dfa_sizeof.c +++ b/parse/scf_dfa_sizeof.c @@ -118,8 +118,6 @@ static int _sizeof_action_rp(scf_dfa_t* dfa, scf_vector_t* words, void* data) id = scf_stack_pop(d->current_identities); assert(id && id->type); - scf_loge("\n"); - if (id->nb_pointers > 0) { t = scf_block_find_type_type(parse->ast->current_block, SCF_VAR_INTPTR); diff --git a/parse/scf_dfa_union.c b/parse/scf_dfa_union.c index 6957bb7..10727b9 100644 --- a/parse/scf_dfa_union.c +++ b/parse/scf_dfa_union.c @@ -94,40 +94,43 @@ static int _union_action_lb(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _union_calculate_size(scf_dfa_t* dfa, scf_type_t* s) { - scf_parse_t* parse = dfa->priv; + 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]; - scf_variable_t* v = s->scope->vars->data[i]; assert(v->size >= 0); int size = 0; if (v->nb_dimentions > 0) { - int capacity = 1; + v->capacity = 1; + for (j = 0; j < v->nb_dimentions; j++) { + if (v->dimentions[j] < 0) { - scf_loge("v: '%s'\n", v->w->text->data); + 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], v->w->file->data, v->w->line); return SCF_DFA_ERROR; + } - } else if (0 == v->dimentions[j]) { - if (j < v->nb_dimentions - 1) { - scf_loge("v: '%s'\n", v->w->text->data); - return SCF_DFA_ERROR; - } + if (0 == v->dimentions[j] && 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; } - capacity *= v->dimentions[j]; + v->capacity *= v->dimentions[j]; } - v->capacity = capacity; size = v->size * v->capacity; - } else { + } else size = v->size; - } if (max_size < size) max_size = size; diff --git a/parse/scf_operator_handler_semantic.c b/parse/scf_operator_handler_semantic.c index 87eb380..20b9a6e 100644 --- a/parse/scf_operator_handler_semantic.c +++ b/parse/scf_operator_handler_semantic.c @@ -922,6 +922,8 @@ static int _scf_op_semantic_pointer(scf_ast_t* ast, scf_node_t** nodes, int nb_n if (!r) return -ENOMEM; + r->member_flag = v1->member_flag; + int i; for (i = 0; i < v1->nb_dimentions; i++) scf_variable_add_array_dimention(r, v1->dimentions[i]); @@ -980,13 +982,21 @@ static int _scf_op_semantic_array_index(scf_ast_t* ast, scf_node_t** nodes, int if (scf_variable_const(v1)) { if (v1->data.i < 0) { - scf_loge("error: index %d < 0\n", v1->data.i); + scf_loge("array index '%s' < 0, real: %d, file: %s, line: %d\n", + v1->w->text->data, v1->data.i, v1->w->file->data, v1->w->line); return -1; } if (v1->data.i >= v0->dimentions[0]) { - scf_loge("index %d >= size %d\n", v1->data.i, v0->dimentions[0]); - return -1; + + if (!v0->member_flag) { + scf_loge("array index '%s' >= size %d, real: %d, file: %s, line: %d\n", + v1->w->text->data, v0->dimentions[0], v1->data.i, v1->w->file->data, v1->w->line); + return -1; + } + + scf_logw("array index '%s' >= size %d, real: %d, confirm it for a zero-array end of a struct? file: %s, line: %d\n", + v1->w->text->data, v0->dimentions[0], v1->data.i, v1->w->file->data, v1->w->line); } } } else if (0 == v0->nb_dimentions && v0->nb_pointers > 0) { @@ -1007,6 +1017,8 @@ static int _scf_op_semantic_array_index(scf_ast_t* ast, scf_node_t** nodes, int if (!r) return -ENOMEM; + r->member_flag = v0->member_flag; + int i; for (i = 1; i < v0->nb_dimentions; i++) scf_variable_add_array_dimention(r, v0->dimentions[i]); @@ -1563,11 +1575,13 @@ static int _scf_op_semantic_call(scf_ast_t* ast, scf_node_t** nodes, int nb_node if (f->vargs_flag) { if (f->argv->size > nb_nodes - 1) { - scf_loge("\n"); + scf_loge("number of args pass to '%s()' at least needs %d, real: %d, file: %s, line: %d\n", + f->node.w->text->data, f->argv->size, nb_nodes - 1, parent->w->file->data, parent->w->line); return -1; } } else if (f->argv->size != nb_nodes - 1) { - scf_loge("\n"); + scf_loge("number of args pass to '%s()' needs %d, real: %d, file: %s, line: %d\n", + f->node.w->text->data, f->argv->size, nb_nodes - 1, parent->w->file->data, parent->w->line); return -1; } diff --git a/parse/scf_parse.c b/parse/scf_parse.c index 4fbda8e..2930c0b 100644 --- a/parse/scf_parse.c +++ b/parse/scf_parse.c @@ -174,10 +174,9 @@ int scf_parse_file(scf_parse_t* parse, const char* path) return 0; } - if (scf_lex_open(&parse->lex, path) < 0) { - scf_loge("\n"); + if (scf_lex_open(&parse->lex, path) < 0) return -1; - } + scf_ast_add_file_block(parse->ast, path); parse->lex->next = parse->lex_list;