for member array[0] in the end of a struct
authoryu.dongliang <18588496441@163.com>
Thu, 3 Oct 2024 06:07:56 +0000 (14:07 +0800)
committeryu.dongliang <18588496441@163.com>
Thu, 3 Oct 2024 06:07:56 +0000 (14:07 +0800)
12 files changed:
core/scf_optimizer_auto_gc_find.c
examples/member_array0.c [new file with mode: 0644]
lex/scf_lex.c
parse/scf_dfa.c
parse/scf_dfa_class.c
parse/scf_dfa_expr.c
parse/scf_dfa_for.c
parse/scf_dfa_include.c
parse/scf_dfa_sizeof.c
parse/scf_dfa_union.c
parse/scf_operator_handler_semantic.c
parse/scf_parse.c

index 7fdf6235a02e0c81b727c3742aee66e179e85421..45b97677ef013253916e6ed2f4c930093ae05aab 100644 (file)
@@ -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 (file)
index 0000000..0c2f233
--- /dev/null
@@ -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;
+}
index 71d8391a83116b7e2de4b202692760e4a0a32d64..8aed1064a68f5422496fe10f5d5e179ccaa4fac2 100644 (file)
@@ -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;
index 8bc897b54e9e7dda8bb7095d2abadd8553450524..ea01777de5ea3d61ad2591f23bc77c8a9c8059ec 100644 (file)
@@ -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;
index 9531048bb220c4f710b109c138e06200a6e0ba5b..669d5179379405616a3fa9f80720761b2f94075e 100644 (file)
@@ -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;
index a55b24c77eeb1436a154922ebe73a26063b7fb51..53827a8c1472f9578ea705963d25c4a950e2d308 100644 (file)
@@ -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);
index 9a7eeb27410d0332066e44c489541698f6bab8b4..fa243edf038607f558f554edc30267e7a184e556 100644 (file)
@@ -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;
        }
 
index 141bf429bdded8a714e5cc8bd85d8607493e081d..c25e7482d0a7f59a5b5fab9425075cfbf39d9da1 100644 (file)
@@ -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)
index df6366f4c31e4dd91660d83977752ca15832215d..aec0be79125e19fe624562e1db5a9d3d51dad8ef 100644 (file)
@@ -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);
index 6957bb73198df6f50a1931f059a6a64edf539e2c..10727b9051f94489c3402e3cd848193c3d475e05 100644 (file)
@@ -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;
index 87eb380c9017743a6fc7ca552b9e366d50b7ff99..20b9a6e41faa253b5d7316ec9c5b0afff5f9bcf2 100644 (file)
@@ -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;
        }
 
index 4fbda8e22868f362b18358ea4a7b15949e31611a..2930c0b4e78139b229ee8f493f4299e07ab4f01a 100644 (file)
@@ -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;