use the same syntax to C when init struct & array
authoryu.dongliang <18588496441@163.com>
Fri, 28 Feb 2025 04:17:29 +0000 (12:17 +0800)
committeryu.dongliang <18588496441@163.com>
Fri, 28 Feb 2025 04:17:29 +0000 (12:17 +0800)
elf/scf_elf_native.h
elf/scf_elf_x64.c
elf/scf_elf_x64_so.c
examples/init_struct_array.c
parse/complex.c [deleted file]
parse/scf_dfa_init_data.c

index cca7d3da504e0287ecad48a2ed91ac4a54f987c3..e1273b7792ca4184d02541a37b6cb099892875dc 100644 (file)
@@ -58,6 +58,8 @@ typedef struct {
        scf_vector_t*   dyn_needs;
        scf_vector_t*   dyn_relas;
 
+       int             n_plts;
+
        elf_section_t*  gnu_hash;
        elf_section_t*  interp;
        elf_section_t*  dynsym;
index 9b20ab05d740b2a3c1e7d18e04bfea1a1bd2b98e..0c5fd5698f9a9f4e4005a0e2fab1fcc8be27ed40 100644 (file)
@@ -417,6 +417,8 @@ static int _x64_elf_write_exec(scf_elf_context_t* elf, const char* sysroot)
        int               nb_phdrs = 3;
 
        if (x64->dynsyms && x64->dynsyms->size > 0) {
+               x64->n_plts   = x64->dynsyms->size;
+
                __x64_elf_add_dyn(x64, sysroot);
                nb_phdrs = 6;
        }
@@ -541,6 +543,8 @@ static int _x64_elf_write_dyn(scf_elf_context_t* elf, const char* sysroot)
                        return -ENOMEM;
        }
 
+       x64->n_plts = x64->dynsyms->size;
+
        for (i = 0; i < x64->symbols->size; i++) {
                sym       = x64->symbols->data[i];
 
index 366955b190d14e768a6bd405db1dd20845d2465c..59129594304b8081ac6c91deefa533c21eb884d1 100644 (file)
@@ -338,15 +338,15 @@ static int _x64_elf_add_got_plt(elf_native_t* x64, elf_section_t** ps)
                return -ENOMEM;
        }
 
-       s->data = calloc(x64->dynsyms->size + 3, sizeof(void*));
+       s->data = calloc(x64->n_plts + 3, sizeof(void*));
        if (!s->data) {
                scf_string_free(s->name);
                free(s);
                return -ENOMEM;
        }
-       s->data_len  = (x64->dynsyms->size + 3) * sizeof(void*);
+       s->data_len = (x64->n_plts + 3) * sizeof(void*);
 
-       s->index     = 1;
+       s->index = 1;
 
        s->sh.sh_type   = SHT_PROGBITS;
        s->sh.sh_flags  = SHF_ALLOC | SHF_WRITE;
@@ -378,15 +378,15 @@ static int _x64_elf_add_rela_plt(elf_native_t* x64, elf_section_t** ps)
                return -ENOMEM;
        }
 
-       s->data = calloc(x64->dynsyms->size, sizeof(Elf64_Rela));
+       s->data = calloc(x64->n_plts, sizeof(Elf64_Rela));
        if (!s->data) {
                scf_string_free(s->name);
                free(s);
                return -ENOMEM;
        }
-       s->data_len  = x64->dynsyms->size * sizeof(Elf64_Rela);
+       s->data_len = x64->n_plts * sizeof(Elf64_Rela);
 
-       s->index     = 1;
+       s->index = 1;
 
        s->sh.sh_type   = SHT_RELA;
        s->sh.sh_flags  = SHF_ALLOC | SHF_INFO_LINK;
@@ -431,7 +431,7 @@ static int _x64_elf_add_plt(elf_native_t* x64, elf_section_t** ps)
                0xe9, 0,    0, 0, 0,    // jmp
        };
 
-       s->data = malloc(sizeof(plt_lazy) + sizeof(plt) * x64->dynsyms->size);
+       s->data = malloc(sizeof(plt_lazy) + sizeof(plt) * x64->n_plts);
        if (!s->data) {
                scf_string_free(s->name);
                free(s);
@@ -443,7 +443,7 @@ static int _x64_elf_add_plt(elf_native_t* x64, elf_section_t** ps)
 
        int jmp = -32;
        int i;
-       for (i = 0; i < x64->dynsyms->size; i++) {
+       for (i = 0; i < x64->n_plts; i++) {
                memcpy(s->data + s->data_len, plt, sizeof(plt));
 
                s->data[s->data_len + 7    ] = i;
@@ -461,7 +461,7 @@ static int _x64_elf_add_plt(elf_native_t* x64, elf_section_t** ps)
                s->data_len += sizeof(plt);
        }
 
-       s->index     = 1;
+       s->index = 1;
 
        s->sh.sh_type   = SHT_PROGBITS;
        s->sh.sh_flags  = SHF_ALLOC;
@@ -1048,10 +1048,10 @@ static void __x64_plt_link(elf_native_t* x64, uint64_t rx_base, uint64_t rw_base
        plt_addr += 16;
 
        int i;
-       for (i = 0; i < x64->dynsyms->size; i++) {
-               rela_plt[i].r_offset   = got_addr;
-               rela_plt[i].r_addend   = 0;
-               rela_plt[i].r_info     = ELF64_R_INFO(i + 1, R_X86_64_JUMP_SLOT);
+       for (i = 0; i < x64->n_plts; i++) {
+               rela_plt[i].r_offset = got_addr;
+               rela_plt[i].r_addend = 0;
+               rela_plt[i].r_info   = ELF64_R_INFO(i + 1, R_X86_64_JUMP_SLOT);
 
                scf_logd("got_addr: %#lx\n", got_addr);
 
index 2a7b76bdcee5fd0f70c71a23039850c315c415d0..1cc4d5e89cb182b6196372ad9559acb045bf9e3c 100644 (file)
@@ -16,10 +16,10 @@ S s =
 
 int a[4] =
 {
-       .0  = 1,
-       .A  = 2,
+       [0] = 1,
+       [A] = 2,
        [2] = 3,
-       .3  = 4,
+       [3] = 4,
 };
 
 int main()
diff --git a/parse/complex.c b/parse/complex.c
deleted file mode 100644 (file)
index 4c4274a..0000000
+++ /dev/null
@@ -1,159 +0,0 @@
-
-include "../lib/scf_capi.c";
-
-struct complex
-{
-       double real;
-       double imag;
-
-       int __init(complex* this, double real, double imag)
-       {
-               this->real = real;
-               this->imag = imag;
-               return 0;
-       }
-
-/*     int operator+=(complex* this, complex* that)
-       {
-               if (!this || !that)
-                       return -1;
-
-               this->real += that->real;
-               this->imag += that->imag;
-               return 0;
-       }
-
-       int operator-=(complex* this, complex* that)
-       {
-               if (!this || !that)
-                       return -1;
-
-               this->real -= that->real;
-               this->imag -= that->imag;
-               return 0;
-       }
-
-       int operator*=(complex* this, complex* that)
-       {
-               if (!this || !that)
-                       return -1;
-
-               double a = this->real;
-               double b = this->imag;
-               double c = that->real;
-               double d = that->imag;
-
-               this->real = a * c - b * d;
-               this->imag = a * d + b * c;
-               return 0;
-       }
-
-       int operator/=(complex* this, complex* that)
-       {
-               if (!this || !that)
-                       return -1;
-
-               double a = this->real;
-               double b = this->imag;
-               double c = that->real;
-               double d = that->imag;
-
-               this->real = (a * c + b * d) / (c * c + d * d);
-               this->imag = (b * c - a * d) / (c * c + d * d);
-               return 0;
-       }
-*/
-       complex*, int operator+(complex* this, complex* that)
-       {
-               if (!this || !that)
-                       return -1;
-
-               complex* res;
-
-               res = create complex(0.0, 0.0);
-               if (!res)
-                       return NULL, -1;
-
-               res->real = this->real + that->real;
-               res->imag = this->imag + that->imag;
-               return res, 0;
-       }
-
-       complex*, int operator-(complex* this, complex* that)
-       {
-               if (!this || !that)
-                       return -1;
-
-               complex* res;
-
-               res = create complex(0.0, 0.0);
-               if (!res)
-                       return NULL, -1;
-
-               res->real = this->real - that->real;
-               res->imag = this->imag - that->imag;
-               return res, 0;
-       }
-
-       complex*, int operator*(complex* this, complex* that)
-       {
-               if (!this || !that)
-                       return -1;
-
-               complex* res;
-
-               res = create complex(0.0, 0.0);
-               if (!res)
-                       return NULL, -1;
-
-               double a = this->real;
-               double b = this->imag;
-               double c = that->real;
-               double d = that->imag;
-
-               res->real = a * c - b * d;
-               res->imag = a * d + b * c;
-               return res, 0;
-       }
-
-       complex*, int operator/(complex* this, complex* that)
-       {
-               if (!this || !that)
-                       return -1;
-
-               complex* res;
-
-               res = create complex(0.0, 0.0);
-               if (!res)
-                       return NULL, -1;
-
-               double a = this->real;
-               double b = this->imag;
-               double c = that->real;
-               double d = that->imag;
-
-               res->real = (a * c + b * d) / (c * c + d * d);
-               res->imag = (b * c - a * d) / (c * c + d * d);
-               return res, 0;
-       }
-};
-
-int main()
-{
-       complex* c0;
-       complex* c1;
-       complex* c2;
-       complex* c3;
-
-       c0 = create complex(1.0, 2.0);
-       c1 = create complex(3.0, 4.0);
-
-       c2 = c0 + c1;
-       c3 = c0 * c1;
-
-       printf("c2: %lf,%lf\n", c2->real, c2->imag);
-       printf("c3: %lf,%lf\n", c3->real, c3->imag);
-
-       return 0;
-}
-
index 56699596b307d46ad2de4f9f50dff82acc417c63..da6255f02756f1ee1fc75712cb6041d3d3a52cc1 100644 (file)
@@ -240,6 +240,24 @@ static int _data_action_member(scf_dfa_t* dfa, scf_vector_t* words, void* data)
        return SCF_DFA_NEXT_WORD;
 }
 
+static int _error_action_member(scf_dfa_t* dfa, scf_vector_t* words, void* data)
+{
+       scf_lex_word_t* w = words->data[words->size - 1];
+
+       scf_loge("member '%s' should be an var in struct, file: %s, line: %d\n",
+                       w->text->data, w->file->data, w->line);
+       return SCF_DFA_ERROR;
+}
+
+static int _error_action_index(scf_dfa_t* dfa, scf_vector_t* words, void* data)
+{
+       scf_lex_word_t* w = words->data[words->size - 1];
+
+       scf_loge("array index '%s' should be an integer, file: %s, line: %d\n",
+                       w->text->data, w->file->data, w->line);
+       return SCF_DFA_ERROR;
+}
+
 static int _data_action_index(scf_dfa_t* dfa, scf_vector_t* words, void* data)
 {
        scf_parse_t*        parse = dfa->priv;
@@ -309,10 +327,12 @@ static int _dfa_init_module_init_data(scf_dfa_t* dfa)
 
        SCF_DFA_MODULE_NODE(dfa, init_data, dot,    scf_dfa_is_dot,            scf_dfa_action_next);
        SCF_DFA_MODULE_NODE(dfa, init_data, member, scf_dfa_is_identity,       _data_action_member);
-       SCF_DFA_MODULE_NODE(dfa, init_data, index0, scf_dfa_is_const_integer,  _data_action_index);
-       SCF_DFA_MODULE_NODE(dfa, init_data, index1, scf_dfa_is_const_integer,  _data_action_index);
+       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);
 
+       SCF_DFA_MODULE_NODE(dfa, init_data, merr,   scf_dfa_is_entry,          _error_action_member);
+       SCF_DFA_MODULE_NODE(dfa, init_data, ierr,   scf_dfa_is_entry,          _error_action_index);
+
        scf_parse_t*        parse = dfa->priv;
        dfa_data_t*         d     = parse->dfa_data;
        init_module_data_t* md    = d->module_datas[dfa_module_init_data.index];
@@ -340,10 +360,12 @@ static int _dfa_init_syntax_init_data(scf_dfa_t* dfa)
 
        SCF_DFA_GET_MODULE_NODE(dfa, init_data, dot,    dot);
        SCF_DFA_GET_MODULE_NODE(dfa, init_data, member, member);
-       SCF_DFA_GET_MODULE_NODE(dfa, init_data, index0, index0);
-       SCF_DFA_GET_MODULE_NODE(dfa, init_data, index1, index1);
+       SCF_DFA_GET_MODULE_NODE(dfa, init_data, index,  index);
        SCF_DFA_GET_MODULE_NODE(dfa, init_data, assign, assign);
 
+       SCF_DFA_GET_MODULE_NODE(dfa, init_data, merr,   merr);
+       SCF_DFA_GET_MODULE_NODE(dfa, init_data, ierr,   ierr);
+
        SCF_DFA_GET_MODULE_NODE(dfa, expr,      entry,  expr);
 
        // empty init, use 0 to fill the data
@@ -368,16 +390,16 @@ 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(dot,       index0);
        scf_dfa_node_add_child(member,    assign);
-       scf_dfa_node_add_child(index0,    assign);
        scf_dfa_node_add_child(assign,    expr);
 
-       scf_dfa_node_add_child(ls,        index1);
-       scf_dfa_node_add_child(index1,    rs);
+       scf_dfa_node_add_child(ls,        index);
+       scf_dfa_node_add_child(index    rs);
        scf_dfa_node_add_child(rs,        ls);
        scf_dfa_node_add_child(rs,        assign);
 
+       scf_dfa_node_add_child(dot,       merr);
+       scf_dfa_node_add_child(ls,        ierr);
        return 0;
 }