1, merge 'coroutine' to scf core framework, 2, close some logs, 3, fix: x64 integer...
authoryu.dongliang <18588496441@163.com>
Mon, 11 Nov 2024 06:01:07 +0000 (14:01 +0800)
committeryu.dongliang <18588496441@163.com>
Mon, 11 Nov 2024 06:01:07 +0000 (14:01 +0800)
27 files changed:
core/scf_operator_handler_3ac.c
coroutine/Makefile
coroutine/main.c [new file with mode: 0644]
coroutine/scf_coroutine.c
coroutine/scf_coroutine.h
coroutine/scf_coroutine_asm.S
coroutine/scf_coroutine_run.c
elf/scf_elf_arm32.c
elf/scf_elf_arm32_so.c
elf/scf_elf_arm64.c
elf/scf_elf_arm64_so.c
elf/scf_elf_naja.c
elf/scf_elf_naja_so.c
elf/scf_elf_native32.c
elf/scf_elf_x64_so.c
examples/coroutine_printf.c [new file with mode: 0644]
examples/init_array_reshape.c [new file with mode: 0644]
native/risc/scf_risc.c
native/risc/scf_risc_inst.c
native/x64/scf_x64.c
native/x64/scf_x64_reg.c
parse/main.c
parse/scf_dfa_async.c
parse/scf_operator_handler_const.c
parse/scf_struct_array.c
util/scf_rbtree.c
vm/scf_vm_naja.c

index 297aaeeeb30153e7d3d23d00aac2c8ad8517de20..cbaf8fed638a26a369fcf11cad623425a255bb79 100644 (file)
@@ -1492,6 +1492,7 @@ static int _scf_op_call(scf_ast_t* ast, scf_node_t** nodes, int nb_nodes, void*
 
        scf_handler_data_t* d      = data;
        scf_variable_t*     v      = NULL;
+       scf_variable_t*     fmt    = NULL;
        scf_function_t*     f      = NULL;
        scf_vector_t*       argv   = NULL;
        scf_node_t*         parent = nodes[0]->parent;
@@ -1561,6 +1562,19 @@ static int _scf_op_call(scf_ast_t* ast, scf_node_t** nodes, int nb_nodes, void*
                                && !v->local_flag
                                && !v->const_flag)
                        v->tmp_flag = 1;
+
+               if (!strcmp(f->node.w->text->data, "scf_async") && i > 2) {
+
+                       fmt = _scf_operand_get(nodes[2]);
+
+                       if (scf_variable_float(v))
+                               ret = scf_string_cat_cstr_len(fmt->data.s, "f", 1);
+                       else
+                               ret = scf_string_cat_cstr_len(fmt->data.s, "d", 1);
+
+                       if (ret < 0)
+                               return ret;
+               }
        }
 
        if (parent->result_nodes) {
index ec4dc2eac5843425fc138d106171eee8dbcd81f8..3db3a918b43656c35840a97a4e70a01ff4bcf41b 100644 (file)
@@ -1,13 +1,14 @@
 CFILES += ../util/scf_rbtree.c
 CFILES += scf_coroutine.c
 CFILES += scf_coroutine_run.c
+#CFILES += main.c
 CFILES += scf_coroutine_asm.S
-CFILES += scf_coroutine_test.c
 
-CFLAGS += -g -O3 -no-pie
+CFLAGS += -g -O3 -fPIC -shared
 CFLAGS += -I../util
 
 LDFLAGS +=
 
 all:
-       gcc $(CFLAGS) $(CFILES) $(LDFLAGS)
+       gcc $(CFLAGS) $(CFILES) $(LDFLAGS) -o libscf_co.so
+       gcc -I../util main.c -lscf_co -L./
diff --git a/coroutine/main.c b/coroutine/main.c
new file mode 100644 (file)
index 0000000..a476a5e
--- /dev/null
@@ -0,0 +1,72 @@
+#include"scf_coroutine.h"
+
+#include<sys/types.h>
+#include<sys/socket.h>
+#include<netinet/in.h>
+#include<arpa/inet.h>
+
+#include<sys/epoll.h>
+#include<sys/time.h>
+#include<fcntl.h>
+
+static int _async_test()
+{
+       int fd = socket(AF_INET, SOCK_STREAM, 0);
+       if (fd < 0) {
+               scf_loge("\n");
+               return -1;
+       }
+
+       struct sockaddr_in addr;
+       addr.sin_family = AF_INET;
+       addr.sin_port   = htons(2000);
+       addr.sin_addr.s_addr = inet_addr("127.0.0.1");
+
+       int ret = __async_connect(fd, (struct sockaddr*)&addr, sizeof(addr), 1000);
+       if (ret < 0) {
+               scf_loge("\n");
+               return -1;
+       }
+
+       ret = __async_write(fd, "hello\n", 7);
+       if (ret < 0) {
+               scf_loge("\n");
+               return -1;
+       }
+
+       scf_logi("async test ok, ret: %d\n", ret);
+       return 0;
+}
+
+int main()
+{
+       scf_co_thread_t* thread = NULL;
+       scf_co_task_t*   task   = NULL;
+
+       int ret = scf_co_thread_open(&thread);
+       if (ret < 0) {
+               scf_loge("\n");
+               return -1;
+       }
+
+       uintptr_t rsp[] = {5, 6, 0.1, 7};
+
+       *(double*)&rsp[2] = 0.123;
+
+// interger: rdi rsi rdx rcx r8 r9, float / double: xmm0-xmm7, stack rsp: ...
+       ret = __scf_async((uintptr_t)_async_test, "dddd ffff ffff", 1, 2, 3, 4, rsp, 0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7);
+       if (ret < 0) {
+               scf_loge("\n");
+               return -1;
+       }
+
+       scf_logi("thread: %p\n", thread);
+
+       ret = scf_co_thread_run(thread);
+       if (ret < 0) {
+               scf_loge("\n");
+               return -1;
+       }
+
+       return 0;
+}
index 711b0eb7486d92840b6acb95e45927538601e6d7..4e2325701b9ede95cf47d27261706af3971c4796 100644 (file)
@@ -1,6 +1,6 @@
 #include"scf_coroutine.h"
 
-static scf_co_thread_t* co_thread = NULL;
+scf_co_thread_t* __co_thread = NULL;
 
 int  __scf_co_task_run(scf_co_task_t* task);
 void __asm_co_task_yield(scf_co_task_t* task, uintptr_t* task_rip, uintptr_t* task_rsp, uintptr_t task_rsp0);
@@ -31,7 +31,7 @@ int scf_co_thread_open (scf_co_thread_t** pthread)
        scf_rbtree_init(&thread->timers);
        scf_list_init  (&thread->tasks);
 
-       co_thread = thread;
+       __co_thread = thread;
 
        *pthread = thread;
        return 0;
@@ -43,31 +43,118 @@ int scf_co_thread_close(scf_co_thread_t*  thread)
        return -1;
 }
 
-int scf_co_task_alloc(scf_co_task_t** ptask, uintptr_t func_ptr, uintptr_t rdi, uintptr_t rsi, uintptr_t rdx, uintptr_t rcx)
+int scf_co_task_alloc(scf_co_task_t** ptask, uintptr_t funcptr, const char* fmt, uintptr_t rdx, uintptr_t rcx, uintptr_t r8, uintptr_t r9, uintptr_t* rsp,
+               double xmm0, double xmm1, double xmm2, double xmm3, double xmm4, double xmm5, double xmm6, double xmm7)
 {
        scf_co_task_t* task = calloc(1, sizeof(scf_co_task_t));
        if (!task)
                return -ENOMEM;
 
-       task->stack_data = calloc(4, sizeof(uintptr_t));
+       const char* p;
+
+       int i = 0;
+       int j = 0;
+
+       scf_logi("fmt: %s\n", fmt);
+
+       for (p = fmt; *p; p++) {
+
+               if ('d'== *p) // 'd' in format string for 'long', 'f' for 'double'
+                       i++;
+               else if ('f' == *p)
+                       j++;
+       }
+
+       if (j > 0)
+               task->n_floats = 1;
+       else
+               task->n_floats = 0;
+
+       scf_logi("n_floats: %d\n", task->n_floats);
+
+       i -= 4; // rdx, rcx, r8, r9 not in stack, xmm0-xmm7 too.
+       j -= 8;
+
+       if (i < 0)
+               i = 0;
+
+       if (j < 0)
+               j = 0;
+
+       int n = 6 + 8 + i + j;
+       int k;
+
+       if (n % 2 == 0)
+               n++;
+
+       task->stack_data = calloc(n, sizeof(uintptr_t));
        if (!task->stack_data) {
                free(task);
                return -ENOMEM;
        }
 
-       task->stack_len = 4 * sizeof(uintptr_t);
+       scf_logi("int: %d, float: %d, n: %d\n", i, j, n);
+
+       task->stack_len = n * sizeof(uintptr_t);
+
+       task->stack_data[0]  = rdx;
+       task->stack_data[1]  = rcx;
+       task->stack_data[2]  = r8;
+       task->stack_data[3]  = r9;
+
+       task->stack_data[6]  = *(uint64_t*)&xmm0;
+       task->stack_data[7]  = *(uint64_t*)&xmm1;
+       task->stack_data[8]  = *(uint64_t*)&xmm2;
+       task->stack_data[9]  = *(uint64_t*)&xmm3;
+       task->stack_data[10] = *(uint64_t*)&xmm4;
+       task->stack_data[11] = *(uint64_t*)&xmm5;
+       task->stack_data[12] = *(uint64_t*)&xmm6;
+       task->stack_data[13] = *(uint64_t*)&xmm7;
+
+       i = 0;
+       j = 0;
+       k = 0;
+       n = 14;
+       for (p = fmt; *p; p++) {
+
+               if ('d'== *p) {
+
+                       if (4 == i || 5 == i)
+                               task->stack_data[i] = rsp[k++];
+                       else if (i >= 6)
+                               task->stack_data[n++] = rsp[k++];
+                       i++;
+
+               } else if ('f' == *p) {
+                       if (j >= 8)
+                               task->stack_data[n++] = rsp[k++];
+                       j++;
+               }
+       }
 
-       uintptr_t* stack = task->stack_data;
-       stack[0] = rdi;
-       stack[1] = rsi;
-       stack[2] = rdx;
-       stack[3] = rcx;
+       i = 0;
+       j = 0;
+       for (p = fmt; *p; p++) {
+
+               if ('d'== *p) {
+                       scf_logi("d, task->stack[%d]: %#lx\n", i, task->stack_data[i]);
+                       i++;
+               } else if ('f' == *p) {
+                       if (j >= 8) {
+                               scf_logi("f, task->stack[%d]: %lg\n", i, *(double*)&task->stack_data[i]);
+                               i++;
+                       }
+                       j++;
+               }
+       }
 
-       scf_loge("rdi: %ld, stack[0]: %ld\n", rdi, stack[0]);
+       scf_logi("-----------\n");
+       for (i = 0; i < task->stack_len / sizeof(uintptr_t); i++)
+               scf_logi("task->stack[%d]: %#lx\n", i, task->stack_data[i]);
 
        task->stack_capacity = task->stack_len;
 
-       task->rip = func_ptr;
+       task->rip = funcptr;
 
        *ptask = task;
        return 0;
@@ -85,17 +172,6 @@ static int _co_timer_cmp(scf_rbtree_node_t* node0, void* data)
        return 0;
 }
 
-static int _co_timer_cmp_task(scf_rbtree_node_t* node0, void* data)
-{
-       scf_co_task_t* task0 = (scf_co_task_t*)node0;
-       scf_co_task_t* task1 = (scf_co_task_t*)data;
-
-       if (task0->time < task1->time)
-               return -1;
-       else if (task0->time > task1->time)
-               return 1;
-}
-
 void scf_co_task_free(scf_co_task_t* task)
 {
        if (task->stack_data)
@@ -104,7 +180,7 @@ void scf_co_task_free(scf_co_task_t* task)
        free(task);
 }
 
-int scf_co_thread_add_task(scf_co_thread_t* thread, scf_co_task_t* task)
+void scf_co_thread_add_task(scf_co_thread_t* thread, scf_co_task_t* task)
 {
        task->time = scf_gettime();
 
@@ -114,14 +190,14 @@ int scf_co_thread_add_task(scf_co_thread_t* thread, scf_co_task_t* task)
 
        task->thread = thread;
 
-       thread->nb_tasks++;
-       return 0;
+       thread->n_tasks++;
 }
 
-int scf_async(uintptr_t funcptr, uintptr_t rdi, uintptr_t rsi, uintptr_t rdx)
+int __scf_async(uintptr_t funcptr, const char* fmt, uintptr_t rdx, uintptr_t rcx, uintptr_t r8, uintptr_t r9, uintptr_t* rsp,
+               double xmm0, double xmm1, double xmm2, double xmm3, double xmm4, double xmm5, double xmm6, double xmm7)
 {
-       if (!co_thread) {
-               if (scf_co_thread_open(&co_thread) < 0) {
+       if (!__co_thread) {
+               if (scf_co_thread_open(&__co_thread) < 0) {
                        scf_loge("\n");
                        return -1;
                }
@@ -129,12 +205,14 @@ int scf_async(uintptr_t funcptr, uintptr_t rdi, uintptr_t rsi, uintptr_t rdx)
 
        scf_co_task_t* task = NULL;
 
-       if (scf_co_task_alloc(&task, funcptr, rdi, rsi, rdx, 0) < 0) {
+       int ret = scf_co_task_alloc(&task, funcptr, fmt, rdx, rcx, r8, r9, rsp,
+                                            xmm0, xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7);
+       if (ret < 0) {
                scf_loge("\n");
-               return -1;
+               return ret;
        }
 
-       scf_co_thread_add_task(co_thread, task);
+       scf_co_thread_add_task(__co_thread, task);
        return 0;
 }
 
@@ -142,7 +220,7 @@ int __save_stack(scf_co_task_t* task)
 {
        task->stack_len = task->rsp0 - task->rsp;
 
-       scf_loge("task: %p, stack_len: %ld, start: %#lx, end: %#lx, task->rip: %#lx\n",
+       scf_logi("task: %p, stack_len: %ld, start: %#lx, end: %#lx, task->rip: %#lx\n",
                        task, task->stack_len, task->rsp, task->rsp0, task->rip);
 
        if (task->stack_len > task->stack_capacity) {
@@ -162,7 +240,7 @@ int __save_stack(scf_co_task_t* task)
 
 void __async_msleep(int64_t msec)
 {
-       scf_co_thread_t* thread = co_thread;
+       scf_co_thread_t* thread = __co_thread;
        scf_co_task_t*   task   = thread->current;
 
        if (task->time > 0)
@@ -256,8 +334,8 @@ static int _co_read_to_bufs(scf_co_task_t* task)
 
 static int _co_add_event(int fd, uint32_t events)
 {
-       scf_co_thread_t*   thread = co_thread;
-       scf_co_task_t*     task   = thread->current;
+       scf_co_thread_t*  thread = __co_thread;
+       scf_co_task_t*    task   = thread->current;
 
        struct epoll_event ev;
        ev.events   = events | EPOLLET | EPOLLRDHUP;
@@ -283,8 +361,8 @@ static int _co_add_event(int fd, uint32_t events)
 
 int __async_connect(int fd, const struct sockaddr *addr, socklen_t addrlen, int64_t msec)
 {
-       scf_co_thread_t*   thread = co_thread;
-       scf_co_task_t*     task   = thread->current;
+       scf_co_thread_t*  thread = __co_thread;
+       scf_co_task_t*    task   = thread->current;
 
        int flags = fcntl(fd, F_GETFL);
        flags |= O_NONBLOCK;
@@ -335,7 +413,7 @@ int __async_connect(int fd, const struct sockaddr *addr, socklen_t addrlen, int6
                        err = -err;
                }
 
-               scf_loge("err: %d\n", err);
+               scf_logi("err: %d\n", err);
        }
 
        if (epoll_ctl(thread->epfd, EPOLL_CTL_DEL, fd, NULL) < 0) {
@@ -344,7 +422,6 @@ int __async_connect(int fd, const struct sockaddr *addr, socklen_t addrlen, int6
        }
 
        if (task->time > 0) {
-               scf_loge("\n");
                scf_rbtree_delete(&thread->timers, &task->timer);
                task->time = 0;
        }
@@ -354,8 +431,8 @@ int __async_connect(int fd, const struct sockaddr *addr, socklen_t addrlen, int6
 
 int __async_read(int fd, void* buf, size_t count, int64_t msec)
 {
-       scf_co_thread_t*   thread = co_thread;
-       scf_co_task_t*     task   = thread->current;
+       scf_co_thread_t*  thread = __co_thread;
+       scf_co_task_t*    task   = thread->current;
 
        int ret = _co_add_event(fd, EPOLLIN);
        if (ret < 0) {
@@ -402,8 +479,8 @@ int __async_read(int fd, void* buf, size_t count, int64_t msec)
 
 int __async_write(int fd, void* buf, size_t count)
 {
-       scf_co_thread_t*   thread = co_thread;
-       scf_co_task_t*     task   = thread->current;
+       scf_co_thread_t*  thread = __co_thread;
+       scf_co_task_t*    task   = thread->current;
 
        int ret = _co_add_event(fd, EPOLLOUT);
        if (ret < 0) {
@@ -438,13 +515,13 @@ int __async_write(int fd, void* buf, size_t count)
 
 void __async_exit()
 {
-       if (co_thread)
-               co_thread->exit_flag = 1;
+       if (__co_thread)
+               __co_thread->exit_flag = 1;
 }
 
 int __async_loop()
 {
-       return scf_co_thread_run(co_thread);
+       return scf_co_thread_run(__co_thread);
 }
 
 #define SCF_CO_TASK_DELETE(task) \
@@ -455,7 +532,7 @@ int __async_loop()
                } \
                \
                scf_list_del(&task->list); \
-               thread->nb_tasks--; \
+               thread->n_tasks--; \
                \
                scf_co_task_free(task); \
                task = NULL; \
@@ -466,18 +543,32 @@ int scf_co_thread_run(scf_co_thread_t* thread)
        if (!thread)
                return -EINVAL;
 
+       int n_tasks = thread->n_tasks + 1;
+
+       struct epoll_event* events = malloc(n_tasks * sizeof(struct epoll_event));
+       if (!events)
+               return -ENOMEM;
+
        while (!thread->exit_flag) {
 
-               assert(thread->nb_tasks >= 0);
+               assert(thread->n_tasks >= 0);
 
-               if (0 == thread->nb_tasks)
+               if (0 == thread->n_tasks)
                        break;
 
-               struct epoll_event* events = calloc(thread->nb_tasks + 1, sizeof(struct epoll_event));
-               if (!events)
-                       return -ENOMEM;
+               if (n_tasks < thread->n_tasks + 1) {
+                       n_tasks = thread->n_tasks + 1;
+
+                       void* p = realloc(events, n_tasks * sizeof(struct epoll_event));
+                       if (!p) {
+                               free(events);
+                               return -ENOMEM;
+                       }
+
+                       events = p;
+               }
 
-               int ret = epoll_wait(thread->epfd, events, thread->nb_tasks + 1, 300);
+               int ret = epoll_wait(thread->epfd, events, n_tasks, 10);
                if (ret < 0) {
                        scf_loge("errno: %d\n", errno);
 
@@ -495,42 +586,47 @@ int scf_co_thread_run(scf_co_thread_t* thread)
 
                        int ret2 = __scf_co_task_run(task);
 
-                       if (ret2 < 0 || SCF_CO_OK == ret2) {
+                       if (ret2 < 0) {
                                scf_loge("ret2: %d, thread: %p, task: %p\n", ret2, thread, task);
 
+                               SCF_CO_TASK_DELETE(task);
+
+                       } else if (SCF_CO_OK == ret2) {
+                               scf_logi("ret2: %d, thread: %p, task: %p\n", ret2, thread, task);
+
                                SCF_CO_TASK_DELETE(task);
                        }
                }
 
                while (1) {
                        scf_co_task_t* task = (scf_co_task_t*) scf_rbtree_min(&thread->timers, thread->timers.root);
-                       if (!task) {
-                               scf_loge("\n");
+                       if (!task)
                                break;
-                       }
 
-                       if (task->time > scf_gettime()) {
+                       if (task->time > scf_gettime())
                                break;
-                       }
 
                        scf_rbtree_delete(&thread->timers, &task->timer);
                        task->time = 0;
 
                        int ret2 = __scf_co_task_run(task);
 
-                       if (ret2 < 0 || SCF_CO_OK == ret2) {
+                       if (ret2 < 0) {
                                scf_loge("ret2: %d, thread: %p, task: %p\n", ret2, thread, task);
 
+                               SCF_CO_TASK_DELETE(task);
+
+                       } else if (SCF_CO_OK == ret2) {
+                               scf_logi("ret2: %d, thread: %p, task: %p\n", ret2, thread, task);
+
                                SCF_CO_TASK_DELETE(task);
                        }
                }
 
-               free(events);
-               events = NULL;
        }
 
-       scf_loge("async exit\n");
+       scf_logi("async exit\n");
 
+       free(events);
        return 0;
 }
-
index 6d25b0a2f6b32dd2b5624725d7be87957fadcb92..abfc4c5416abe6d16f173bfedff0c20773b66d80 100644 (file)
@@ -18,6 +18,8 @@ typedef struct scf_co_thread_s  scf_co_thread_t;
 
 typedef struct scf_co_buf_s     scf_co_buf_t;
 
+extern         scf_co_thread_t* __co_thread;
+
 struct scf_co_buf_s
 {
        scf_co_buf_t*      next;
@@ -48,6 +50,7 @@ struct scf_co_task_s
        intptr_t           stack_len;
        intptr_t           stack_capacity;
 
+       int                n_floats;
        int                err;
 
        uint32_t           events;
@@ -63,7 +66,7 @@ struct scf_co_thread_s
        scf_rbtree_t       timers;
 
        scf_list_t         tasks;
-       int                nb_tasks;
+       int                n_tasks;
 
        scf_co_task_t*     current;
 
@@ -78,17 +81,19 @@ int  __async_connect(int fd, const struct sockaddr *addr, socklen_t addrlen, int
 int  __async_loop();
 
 
-int scf_co_thread_open (scf_co_thread_t** pthread);
-int scf_co_thread_close(scf_co_thread_t*  thread);
+int  scf_co_thread_open (scf_co_thread_t** pthread);
+int  scf_co_thread_close(scf_co_thread_t*  thread);
+
+void scf_co_thread_add_task(scf_co_thread_t* thread, scf_co_task_t* task);
 
-int scf_co_thread_add_task(scf_co_thread_t* thread, scf_co_task_t* task);
+int  scf_co_thread_run(scf_co_thread_t*  thread);
 
-int scf_co_thread_run(scf_co_thread_t*  thread);
+int  scf_co_task_alloc(scf_co_task_t** ptask, uintptr_t funcptr, const char* fmt, uintptr_t rdx, uintptr_t rcx, uintptr_t r8, uintptr_t r9, uintptr_t* rsp,
+               double xmm0, double xmm1, double xmm2, double xmm3, double xmm4, double xmm5, double xmm6, double xmm7);
 
-int scf_co_task_alloc(scf_co_task_t** ptask, uintptr_t func_ptr, uintptr_t rdi, uintptr_t rsi, uintptr_t rdx, uintptr_t rcx);
 void scf_co_task_free(scf_co_task_t* task);
 
-int scf_async(uintptr_t funcptr, uintptr_t rdi, uintptr_t rsi, uintptr_t rdx);
+int  __scf_async(uintptr_t funcptr, const char* fmt, uintptr_t rdx, uintptr_t rcx, uintptr_t r8, uintptr_t r9, uintptr_t* rsp,
+               double xmm0, double xmm1, double xmm2, double xmm3, double xmm4, double xmm5, double xmm6, double xmm7);
 
 #endif
-
index fb579b600f0373d717ebfcaceb3b1af5591b67bd..a4194277d4e42ae578a530c93a64a9eba93be4b8 100644 (file)
@@ -1,6 +1,6 @@
 .text
-.global __asm_co_task_run, __asm_co_task_yield, __asm_co_task_resume
-.global __jmp_ret, __jmp_yield
+.global __asm_co_task_run, __asm_co_task_yield, __save_stack
+.global scf_async, __scf_async
 
 .align 8
 __asm_co_task_run:
@@ -9,6 +9,7 @@ __asm_co_task_run:
 # rdx, task_rip
 # rcx, stack_len
 # r8,  task_rsp0 addr 
+# r9,  n_floats
 
        push %rbp
        push %rdi
@@ -25,29 +26,64 @@ __asm_co_task_run:
 
        mov (%r8), %rax
        mov  %rsp, (%r8)
-       mov  %rdx, %r8
+       mov  %rdx, %rbx
 
        mov  %rsp, %rbp
 
 _fill:
        sub  %rcx, %rsp
        mov  %rsp, %rdi
+
+       push %rcx
+       test $0x7, %rcx
+       jne 1f
+
+       shr $3, %rcx
+       cld
+       rep movsq
+       jmp 3f
+1:
+       test $0x3, %rcx
+       jne 2f
+
+       shr $2, %rcx
+       cld
+       rep movsl
+       jmp 3f
+2:
        cld
        rep movsb
+3:
+       pop  %rcx
 
        test %rax, %rax
        jne  _jmp
 
+       movq %r9, %rax
+
        pop %rdi
        pop %rsi
        pop %rdx
        pop %rcx
+       pop %r8
+       pop %r9
+
+       movsd   (%rsp), %xmm0
+       movsd  8(%rsp), %xmm1
+       movsd 16(%rsp), %xmm2
+       movsd 24(%rsp), %xmm3
+       movsd 32(%rsp), %xmm4
+       movsd 40(%rsp), %xmm5
+       movsd 48(%rsp), %xmm6
+       movsd 56(%rsp), %xmm7
 
-       call *%r8
+       addq $64, %rsp
+
+       call *%rbx
        jmp  _reset
 
 _jmp:
-       jmp  *%r8
+       jmp  *%rbx
 
 _reset:
        mov %rbp, %rsp
@@ -88,13 +124,16 @@ __asm_co_task_yield:
        push %r14
        push %r15
 
-       movq $__jmp_yield, (%rsi)
+       jmp  2f
+1:
+       pop  %rax          # rax == __jmp_yield
+       movq %rax, (%rsi)
        mov  %rsp, (%rdx)
 
        push %rsp
        push %rcx
 
-       call __save_stack
+       call __save_stack@PLT
 
        pop  %rcx
        pop  %rsp
@@ -103,6 +142,8 @@ __asm_co_task_yield:
 
        jmp  __jmp_ret
 
+2:
+       call 1b
 __jmp_yield:
        pop %r15
        pop %r14
@@ -120,3 +161,26 @@ __jmp_yield:
        mov %rsp, %rax
        ret
 
+.align 8
+scf_async:
+# rdi, funcptr
+# rsi, fmt
+# rdx, d0
+# rcx, d1
+# r8,  d2
+# r9,  d3
+# xmm0-xmm7, f0-f7
+
+# 24(rsp), ...
+# 16(rsp), stack arg1
+#  8(rsp), stack arg0  <--- rax
+#  0(rsp), ret addr
+
+       movq %rsp, %rax
+       addq $8,   %rax
+
+       push %rax
+       call __scf_async@PLT
+
+       addq $8, %rsp
+       ret
index a22b50e5d100ef04177e3019d4d10fb107a1fe3a..f2a181ee4bfeb28184d3c640188cad6821f6a0ae 100644 (file)
@@ -1,7 +1,7 @@
 #include"scf_coroutine.h"
 
 uintptr_t __asm_co_task_run(uintptr_t* task_rsp, void* stack_data,
-               uintptr_t task_rip, intptr_t stack_len, uintptr_t* task_rsp0);
+               uintptr_t task_rip, intptr_t stack_len, uintptr_t* task_rsp0, int n_floats);
 
 int __scf_co_task_run(scf_co_task_t* task)
 {
@@ -10,22 +10,19 @@ int __scf_co_task_run(scf_co_task_t* task)
        task->err = 0;
 
        uintptr_t rsp1 = 0;
-       scf_loge("task %p, rsp0: %#lx, rsp1: %#lx\n", task, task->rsp0, rsp1);
+       scf_logw("task %p, rsp0: %#lx, rsp1: %#lx\n", task, task->rsp0, rsp1);
 
        rsp1 = __asm_co_task_run(&task->rsp, task->stack_data, task->rip,
-                       task->stack_len, &task->rsp0);
+                       task->stack_len, &task->rsp0, task->n_floats);
 
        if (task->err) {
                scf_loge("task %p error: %d, rsp0: %#lx, rsp1: %#lx, task->time: %ld\n",
                                task, task->err, task->rsp0, rsp1, task->time);
                return task->err;
 
-       } else if (task->rsp0 == rsp1) {
-//             scf_loge("task %p run ok, rsp0: %#lx, rsp1: %#lx, task->time: %ld\n", task, task->rsp0, rsp1, task->time);
+       } else if (task->rsp0 == rsp1)
                return SCF_CO_OK;
-       }
 
-       scf_loge("task %p running, rsp0: %#lx, rsp1: %#lx\n", task, task->rsp0, rsp1);
+       scf_logi("task %p running, rsp0: %#lx, rsp1: %#lx\n", task, task->rsp0, rsp1);
        return SCF_CO_CONTINUE;
 }
-
index dcbae44f49858f386c3479ae787b890ca88434a3..1c1b26082f755086cacdcd51981b668edad9a6ba 100644 (file)
@@ -21,14 +21,14 @@ static int _arm32_elf_link_cs(elf_native_t* arm32, elf_section_t* s, elf_section
 
                int sym_idx = ELF32_R_SYM(rela->r_info);
 
-               scf_loge("i: %d, sym_idx '%d'\n", i, sym_idx);
+               scf_logd("i: %d, sym_idx '%d'\n", i, sym_idx);
 
                assert(sym_idx >= 1);
                assert(sym_idx -  1 < arm32->symbols->size);
 
                sym = arm32->symbols->data[sym_idx - 1];
                if (sym->dyn_flag) {
-                       scf_loge("sym '%s' in dynamic so\n", sym->name->data);
+                       scf_logd("sym '%s' in dynamic so\n", sym->name->data);
                        continue;
                }
 
@@ -47,7 +47,7 @@ static int _arm32_elf_link_cs(elf_native_t* arm32, elf_section_t* s, elf_section
                                offset -= 8; // 'pc' is 'current + 8'.
                                assert(0 == (offset & 0x3));
 
-                               scf_loge("sym: %s, offset: %#x, %#x\n", sym->name->data, offset, rela->r_offset);
+                               scf_logd("sym: %s, offset: %#x, %#x\n", sym->name->data, offset, rela->r_offset);
                                offset >>= 2;
 
                                if (offset > 0x7fffff || offset < -0x7fffff) {
@@ -55,20 +55,20 @@ static int _arm32_elf_link_cs(elf_native_t* arm32, elf_section_t* s, elf_section
                                        return -EINVAL;
                                }
 
-                               scf_loge("sym: %s, offset: %#x, %#x\n", sym->name->data, offset, rela->r_offset);
+                               scf_logd("sym: %s, offset: %#x, %#x\n", sym->name->data, offset, rela->r_offset);
 
                                *(uint32_t*)(s->data + rela->r_offset) &= 0xff000000;
                                *(uint32_t*)(s->data + rela->r_offset) |= 0x00ffffff & offset;
                                break;
 
                        case R_ARM_REL32:
-                               scf_loge("sym: %s, offset: %#x, %#x, st_value: %#x, cs: %#lx\n", sym->name->data, offset, rela->r_offset,
+                               scf_logd("sym: %s, offset: %#x, %#x, st_value: %#x, cs: %#lx\n", sym->name->data, offset, rela->r_offset,
                                                sym->sym.st_value, cs_base + rela->r_offset);
 
                                *(uint32_t*)(s->data + rela->r_offset) += offset;
                                break;
                        default:
-                               scf_loge("ELF32_R_TYPE(rela->r_info): %d\n", ELF32_R_TYPE(rela->r_info));
+                               scf_logd("ELF32_R_TYPE(rela->r_info): %d\n", ELF32_R_TYPE(rela->r_info));
                                return -EINVAL;
                                break;
                };
@@ -194,7 +194,7 @@ static int _arm32_elf_link_sections(elf_native_t* arm32, uint32_t cs_index, uint
 
                s = arm32->sections->data[rs->sh.sh_info - 1];
 
-               scf_loge("s: %s, rs: %s, rs->sh.sh_info: %u\n", s->name->data, rs->name->data, rs->sh.sh_info);
+               scf_logd("s: %s, rs: %s, rs->sh.sh_info: %u\n", s->name->data, rs->name->data, rs->sh.sh_info);
 
                assert(!strcmp(s->name->data, rs->name->data + 5));
 
@@ -236,7 +236,7 @@ static int _arm32_elf_write_exec(scf_elf_context_t* elf, const char* sysroot)
        for (i = 0; i < arm32->sections->size; i++) {
                s  =        arm32->sections->data[i];
 
-               scf_logw("i: %d, section: %s\n", i, s->name->data);
+               scf_logd("i: %d, section: %s\n", i, s->name->data);
 
                if (!strcmp(".text", s->name->data)) {
 
@@ -260,7 +260,7 @@ static int _arm32_elf_write_exec(scf_elf_context_t* elf, const char* sysroot)
 
                        assert(!crela);
                        crela = s;
-                       scf_loge("i: %d, section: %s\n", i, s->name->data);
+                       scf_logd("i: %d, section: %s\n", i, s->name->data);
 
                } else if (!strcmp(".rela.data", s->name->data)) {
 
index 3a3dbd164c5d206042fca0f4c4037dcc1c756876..504912261cb06319696bb9c21ef9ed76c6c9713c 100644 (file)
@@ -256,9 +256,9 @@ static int _arm32_elf_add_dynamic(elf_native_t* arm32, elf_section_t** ps)
        }
        s->data_len  = nb_tags * sizeof(Elf32_Dyn);
 
-       scf_logw("nb_tags: %d\n", nb_tags);
+       scf_logd("nb_tags: %d\n", nb_tags);
 
-       s->index     = 1;
+       s->index = 1;
 
        s->sh.sh_type   = SHT_PROGBITS;
        s->sh.sh_flags  = SHF_ALLOC | SHF_WRITE;
@@ -464,7 +464,7 @@ int __arm32_elf_add_dyn (elf_native_t* arm32, const char* sysroot)
 
                s->index = arm32->sections->size + 1 + sizeof(sh_names) / sizeof(sh_names[0]);
 
-               scf_logw("s: %s, link: %d, info: %d\n", s->name->data, s->sh.sh_link, s->sh.sh_info);
+               scf_logd("s: %s, link: %d, info: %d\n", s->name->data, s->sh.sh_link, s->sh.sh_info);
 
                if (s->sh.sh_link > 0) {
                        assert(s->sh.sh_link - 1 < arm32->sections->size);
@@ -509,7 +509,7 @@ int __arm32_elf_add_dyn (elf_native_t* arm32, const char* sysroot)
 
                syms[i + 1].st_name = str->len;
 
-               scf_loge("i: %d, st_value: %#x\n", i, syms[i + 1].st_value);
+               scf_logd("i: %d, st_value: %#x\n", i, syms[i + 1].st_value);
 
                scf_string_cat_cstr_len(str, xsym->name->data, xsym->name->len + 1);
        }
@@ -550,7 +550,7 @@ int __arm32_elf_add_dyn (elf_native_t* arm32, const char* sysroot)
                dyns[i].d_tag = DT_NEEDED;
                dyns[i].d_un.d_val = str->len;
 
-               scf_logw("i: %d, %s, %s\n", i, needed->data, needed->data + prefix);
+               scf_logi("i: %d, %s, %s\n", i, needed->data, needed->data + prefix);
 
                scf_string_cat_cstr_len(str, needed->data + prefix, needed->len - prefix + 1);
        }
@@ -631,7 +631,7 @@ int __arm32_elf_add_dyn (elf_native_t* arm32, const char* sysroot)
        for (i = 0; i < arm32->sections->size; i++) {
                s  =        arm32->sections->data[i];
 
-               scf_loge("i: %d, s: %s, index: %d\n", i, s->name->data, s->index);
+               scf_logd("i: %d, s: %s, index: %d\n", i, s->name->data, s->index);
 
                if (s->link) {
                        scf_logd("link: %s, index: %d\n", s->link->name->data, s->link->index);
@@ -660,7 +660,7 @@ int __arm32_elf_add_dyn (elf_native_t* arm32, const char* sysroot)
                sym =        arm32->symbols->data[i];
 
                if (sym->section) {
-                       scf_logw("sym: %s, index: %d->%d\n", sym->name->data, sym->sym.st_shndx, sym->section->index);
+                       scf_logd("sym: %s, index: %d->%d\n", sym->name->data, sym->sym.st_shndx, sym->section->index);
                        sym->sym.st_shndx = sym->section->index;
                }
        }
@@ -680,8 +680,8 @@ int __arm32_elf_post_dyn(elf_native_t* arm32, uint64_t rx_base, uint64_t rw_base
        arm32->interp->sh.sh_addr   = rx_base + arm32->interp->offset;
        arm32->plt->sh.sh_addr      = rx_base + arm32->plt->offset;
 
-       scf_loge("rw_base: %#lx, offset: %#lx\n", rw_base, arm32->got_plt->offset);
-       scf_loge("got_addr: %#x\n", arm32->got_plt->sh.sh_addr);
+       scf_logi("rw_base: %#lx, offset: %#lx\n", rw_base, arm32->got_plt->offset);
+       scf_logi("got_addr: %#x\n", arm32->got_plt->sh.sh_addr);
 
        Elf32_Rela* r;
        Elf32_Rela* rela_plt = (Elf32_Rela*)arm32->rela_plt->data;
@@ -701,7 +701,7 @@ int __arm32_elf_post_dyn(elf_native_t* arm32, uint64_t rx_base, uint64_t rw_base
        got_plt   += 4;
        got_addr  +=16;
 
-       scf_loge("got_addr: %#lx, plt_addr: %#lx, offset: %d, %#x\n", got_addr, plt_addr, offset, offset);
+       scf_logi("got_addr: %#lx, plt_addr: %#lx, offset: %d, %#x\n", got_addr, plt_addr, offset, offset);
 
        plt[4]    = offset - 16;
 
@@ -718,9 +718,9 @@ int __arm32_elf_post_dyn(elf_native_t* arm32, uint64_t rx_base, uint64_t rw_base
 
                offset = got_addr - plt_addr - 8; // 'pc = current + 8'
 
-               scf_loge("i: %d, got_addr: %#lx, plt_addr: %#lx, offset: %d, %#x\n", i, got_addr, plt_addr, offset, offset);
+               scf_logd("i: %d, got_addr: %#lx, plt_addr: %#lx, offset: %d, %#x\n", i, got_addr, plt_addr, offset, offset);
 
-               scf_logw("got_plt[%d]: %#x\n", i, *got_plt);
+               scf_logd("got_plt[%d]: %#x\n", i, *got_plt);
 
                if (offset > 0xfffffff) {
                        scf_loge("\n");
index 07a590115c4eb37d6be7cece52a560ec9a617806..55bcedb7838de03475e0642dd54a8b7b7d57bcb4 100644 (file)
@@ -53,7 +53,7 @@ static int _arm64_elf_link_cs(elf_native_t* arm64, elf_section_t* s, elf_section
 
                                offset &= 0x3ffffff;
 
-                               scf_loge("sym: %s, offset: %#x, %#lx\n", sym->name->data, offset, rela->r_offset);
+                               scf_logd("sym: %s, offset: %#x, %#lx\n", sym->name->data, offset, rela->r_offset);
 
                                *(uint32_t*)(s->data + rela->r_offset) |= offset;
                                break;
@@ -207,7 +207,7 @@ static int _arm64_elf_link_sections(elf_native_t* arm64, uint32_t cs_index, uint
 
                s = arm64->sections->data[rs->sh.sh_info - 1];
 
-               scf_loge("s: %s, rs: %s, rs->sh.sh_info: %u\n", s->name->data, rs->name->data, rs->sh.sh_info);
+               scf_logd("s: %s, rs: %s, rs->sh.sh_info: %u\n", s->name->data, rs->name->data, rs->sh.sh_info);
 
                assert(!strcmp(s->name->data, rs->name->data + 5));
 
index 0f4a0f2d5bbc4d8b5823265015f41f545685f1ae..60d8883077550e0418c98a9654c79d4ee1e25ffe 100644 (file)
@@ -466,7 +466,7 @@ int __arm64_elf_add_dyn(elf_native_t* arm64, const char* sysroot)
 
                s->index = arm64->sections->size + 1 + sizeof(sh_names) / sizeof(sh_names[0]);
 
-               scf_logw("s: %s, link: %d, info: %d\n", s->name->data, s->sh.sh_link, s->sh.sh_info);
+               scf_logd("s: %s, link: %d, info: %d\n", s->name->data, s->sh.sh_link, s->sh.sh_info);
 
                if (s->sh.sh_link > 0) {
                        assert(s->sh.sh_link - 1 < arm64->sections->size);
@@ -511,7 +511,7 @@ int __arm64_elf_add_dyn(elf_native_t* arm64, const char* sysroot)
 
                syms[i + 1].st_name = str->len;
 
-               scf_loge("i: %d, st_value: %#lx\n", i, syms[i + 1].st_value);
+               scf_logi("i: %d, st_value: %#lx\n", i, syms[i + 1].st_value);
 
                scf_string_cat_cstr_len(str, xsym->name->data, xsym->name->len + 1);
        }
@@ -552,7 +552,7 @@ int __arm64_elf_add_dyn(elf_native_t* arm64, const char* sysroot)
                dyns[i].d_tag = DT_NEEDED;
                dyns[i].d_un.d_val = str->len;
 
-               scf_logw("i: %d, %s, %s\n", i, needed->data, needed->data + prefix);
+               scf_logd("i: %d, %s, %s\n", i, needed->data, needed->data + prefix);
 
                scf_string_cat_cstr_len(str, needed->data + prefix, needed->len - prefix + 1);
        }
@@ -632,7 +632,7 @@ int __arm64_elf_add_dyn(elf_native_t* arm64, const char* sysroot)
        for (i = 0; i < arm64->sections->size; i++) {
                s  =        arm64->sections->data[i];
 
-               scf_loge("i: %d, s: %s, index: %d\n", i, s->name->data, s->index);
+               scf_logd("i: %d, s: %s, index: %d\n", i, s->name->data, s->index);
 
                if (s->link) {
                        scf_logd("link: %s, index: %d\n", s->link->name->data, s->link->index);
@@ -650,7 +650,7 @@ int __arm64_elf_add_dyn(elf_native_t* arm64, const char* sysroot)
                sym =        arm64->symbols->data[i];
 
                if (sym->section) {
-                       scf_logw("sym: %s, index: %d->%d\n", sym->name->data, sym->sym.st_shndx, sym->section->index);
+                       scf_logd("sym: %s, index: %d->%d\n", sym->name->data, sym->sym.st_shndx, sym->section->index);
                        sym->sym.st_shndx = sym->section->index;
                }
        }
@@ -670,8 +670,8 @@ int __arm64_elf_post_dyn(elf_native_t* arm64, uint64_t rx_base, uint64_t rw_base
        arm64->interp->sh.sh_addr   = rx_base + arm64->interp->offset;
        arm64->plt->sh.sh_addr      = rx_base + arm64->plt->offset;
 
-       scf_loge("rw_base: %#lx, offset: %#lx\n", rw_base, arm64->got_plt->offset);
-       scf_loge("got_addr: %#lx\n", arm64->got_plt->sh.sh_addr);
+       scf_logi("rw_base: %#lx, offset: %#lx\n", rw_base, arm64->got_plt->offset);
+       scf_logi("got_addr: %#lx\n", arm64->got_plt->sh.sh_addr);
 
        Elf64_Rela* rela_plt = (Elf64_Rela*)arm64->rela_plt->data;
        Elf64_Sym*  dynsym   = (Elf64_Sym* )arm64->dynsym->data;
@@ -690,7 +690,7 @@ int __arm64_elf_post_dyn(elf_native_t* arm64, uint64_t rx_base, uint64_t rw_base
        got_plt   += 3;
        got_addr  += 8;
 
-       scf_loge("got_addr: %#lx, plt_addr: %#lx, offset: %d, %#x\n", got_addr, plt_addr, offset, offset);
+       scf_logi("got_addr: %#lx, plt_addr: %#lx, offset: %d, %#x\n", got_addr, plt_addr, offset, offset);
 
        plt[1] |= (((offset >> 12) & 0x3) << 29) | (((offset >> 14) & 0x7ffff) << 5);
        plt[2] |=  ((got_addr & 0xfff) >> 3) << 10;
@@ -710,7 +710,7 @@ int __arm64_elf_post_dyn(elf_native_t* arm64, uint64_t rx_base, uint64_t rw_base
 
                offset = got_addr - plt_addr;
 
-               scf_loge("i: %d, got_addr: %#lx, plt_addr: %#lx, offset: %d, %#x\n", i, got_addr, plt_addr, offset, offset);
+               scf_logi("i: %d, got_addr: %#lx, plt_addr: %#lx, offset: %d, %#x\n", i, got_addr, plt_addr, offset, offset);
 
                plt[0] |= (((offset >> 12) & 0x3) << 29) | (((offset >> 14) & 0x7ffff) << 5);
                plt[1] |=  ((got_addr & 0xfff) >> 3) << 10;
index dd20336d01b99a7c7957a8a7429a67475c24409f..c911a65aeed941378cc2585acad09a1ca3e2e1b3 100644 (file)
@@ -53,7 +53,7 @@ static int _naja_elf_link_cs(elf_native_t* naja, elf_section_t* s, elf_section_t
 
                                offset &= 0x3ffffff;
 
-                               scf_loge("sym: %s, offset: %#x, %#lx\n", sym->name->data, offset, rela->r_offset);
+                               scf_logi("sym: %s, offset: %#x, %#lx\n", sym->name->data, offset, rela->r_offset);
 
                                *(uint32_t*)(s->data + rela->r_offset) |= offset;
                                break;
@@ -207,7 +207,7 @@ static int _naja_elf_link_sections(elf_native_t* naja, uint32_t cs_index, uint32
 
                s = naja->sections->data[rs->sh.sh_info - 1];
 
-               scf_loge("s: %s, rs: %s, rs->sh.sh_info: %u\n", s->name->data, rs->name->data, rs->sh.sh_info);
+               scf_logi("s: %s, rs: %s, rs->sh.sh_info: %u\n", s->name->data, rs->name->data, rs->sh.sh_info);
 
                assert(!strcmp(s->name->data, rs->name->data + 5));
 
index 31690c5318c124ca908b0c877031931103232e21..486dbb728348c8a87e90d0a58cc83032acfa38ec 100644 (file)
@@ -467,7 +467,7 @@ int __naja_elf_add_dyn(elf_native_t* naja, const char* sysroot)
 
                s->index = naja->sections->size + 1 + sizeof(sh_names) / sizeof(sh_names[0]);
 
-               scf_logw("s: %s, link: %d, info: %d\n", s->name->data, s->sh.sh_link, s->sh.sh_info);
+               scf_logd("s: %s, link: %d, info: %d\n", s->name->data, s->sh.sh_link, s->sh.sh_info);
 
                if (s->sh.sh_link > 0) {
                        assert(s->sh.sh_link - 1 < naja->sections->size);
@@ -512,7 +512,7 @@ int __naja_elf_add_dyn(elf_native_t* naja, const char* sysroot)
 
                syms[i + 1].st_name = str->len;
 
-               scf_loge("i: %d, st_value: %#lx\n", i, syms[i + 1].st_value);
+               scf_logi("i: %d, st_value: %#lx\n", i, syms[i + 1].st_value);
 
                scf_string_cat_cstr_len(str, xsym->name->data, xsym->name->len + 1);
        }
@@ -553,7 +553,7 @@ int __naja_elf_add_dyn(elf_native_t* naja, const char* sysroot)
                dyns[i].d_tag = DT_NEEDED;
                dyns[i].d_un.d_val = str->len;
 
-               scf_logw("i: %d, %s, %s\n", i, needed->data, needed->data + prefix);
+               scf_logd("i: %d, %s, %s\n", i, needed->data, needed->data + prefix);
 
                scf_string_cat_cstr_len(str, needed->data + prefix, needed->len - prefix + 1);
        }
@@ -633,7 +633,7 @@ int __naja_elf_add_dyn(elf_native_t* naja, const char* sysroot)
        for (i = 0; i < naja->sections->size; i++) {
                s  =        naja->sections->data[i];
 
-               scf_loge("i: %d, s: %s, index: %d\n", i, s->name->data, s->index);
+               scf_logd("i: %d, s: %s, index: %d\n", i, s->name->data, s->index);
 
                if (s->link) {
                        scf_logd("link: %s, index: %d\n", s->link->name->data, s->link->index);
@@ -651,7 +651,7 @@ int __naja_elf_add_dyn(elf_native_t* naja, const char* sysroot)
                sym =        naja->symbols->data[i];
 
                if (sym->section) {
-                       scf_logw("sym: %s, index: %d->%d\n", sym->name->data, sym->sym.st_shndx, sym->section->index);
+                       scf_logd("sym: %s, index: %d->%d\n", sym->name->data, sym->sym.st_shndx, sym->section->index);
                        sym->sym.st_shndx = sym->section->index;
                }
        }
@@ -671,8 +671,8 @@ int __naja_elf_post_dyn(elf_native_t* naja, uint64_t rx_base, uint64_t rw_base,
        naja->interp->sh.sh_addr   = rx_base + naja->interp->offset;
        naja->plt->sh.sh_addr      = rx_base + naja->plt->offset;
 
-       scf_loge("rw_base: %#lx, offset: %#lx\n", rw_base, naja->got_plt->offset);
-       scf_loge("got_addr: %#lx\n", naja->got_plt->sh.sh_addr);
+       scf_logi("rw_base: %#lx, offset: %#lx\n", rw_base, naja->got_plt->offset);
+       scf_logi("got_addr: %#lx\n", naja->got_plt->sh.sh_addr);
 
        Elf64_Rela* rela_plt = (Elf64_Rela*)naja->rela_plt->data;
        Elf64_Sym*  dynsym   = (Elf64_Sym* )naja->dynsym->data;
@@ -691,7 +691,7 @@ int __naja_elf_post_dyn(elf_native_t* naja, uint64_t rx_base, uint64_t rw_base,
        got_plt   += 3;
        got_addr  += 8;
 
-       scf_loge("got_addr: %#lx, plt_addr: %#lx, offset: %d, %#x\n", got_addr, plt_addr, offset, offset);
+       scf_logi("got_addr: %#lx, plt_addr: %#lx, offset: %d, %#x\n", got_addr, plt_addr, offset, offset);
 
        plt[2] |=  (offset >> 15) & 0x1fffff;
        plt[3] |=  (got_addr & 0x7fff) << 5;
@@ -710,7 +710,7 @@ int __naja_elf_post_dyn(elf_native_t* naja, uint64_t rx_base, uint64_t rw_base,
 
                offset = got_addr - plt_addr;
 
-               scf_loge("i: %d, got_addr: %#lx, plt_addr: %#lx, offset: %d, %#x\n", i, got_addr, plt_addr, offset, offset);
+               scf_logi("i: %d, got_addr: %#lx, plt_addr: %#lx, offset: %d, %#x\n", i, got_addr, plt_addr, offset, offset);
 
                plt[0] |= (offset >> 15) & 0x1fffff;
                plt[1] |= (got_addr & 0x7fff) << 5;
index 70384a86110db5ed0a1e0c8b9d36fc6bd47a7dd8..deef2cbe2a997aba77426a724c1ba3851539c587 100644 (file)
@@ -555,7 +555,7 @@ int elf32_read_syms(scf_elf_context_t* elf, scf_vector_t* syms, const char* sh_n
 
        assert(symtab->data_len % sizeof(Elf32_Sym) == 0);
 
-       scf_loge("0: syms->size: %d, %ld\n", syms->size, symtab->data_len / sizeof(Elf32_Sym));
+       scf_logd("0: syms->size: %d, %ld\n", syms->size, symtab->data_len / sizeof(Elf32_Sym));
 
        scf_elf_sym_t* esym;
        Elf32_Sym*     sym;
@@ -588,7 +588,7 @@ int elf32_read_syms(scf_elf_context_t* elf, scf_vector_t* syms, const char* sh_n
                }
        }
 
-       scf_loge("1: j: %d, syms->size: %d, %ld\n", j, syms->size, symtab->data_len / sizeof(Elf32_Sym));
+       scf_logd("1: j: %d, syms->size: %d, %ld\n", j, syms->size, symtab->data_len / sizeof(Elf32_Sym));
 
        return 0;
 }
@@ -615,7 +615,7 @@ int elf32_add_dyn_need(scf_elf_context_t* elf, const char* soname)
                return -ENOMEM;
        }
 
-       scf_loge("soname: %s\n", soname);
+       scf_logi("soname: %s\n", soname);
        return 0;
 }
 
@@ -671,7 +671,7 @@ int elf32_read_relas(scf_elf_context_t* elf, scf_vector_t* relas, const char* sh
        if (ret < 0)
                return ret;
 
-       scf_loge("sh_rela: %u\n", sh_rela->sh.sh_link);
+       scf_logd("sh_rela: %u\n", sh_rela->sh.sh_link);
 
        ret = __elf_read_section_by_index(elf, &symtab, sh_rela->sh.sh_link);
        if (ret < 0) {
@@ -850,7 +850,7 @@ int elf32_write_sections(scf_elf_context_t* elf)
        for (i = 0; i < e->sections->size; i++) {
                s  =        e->sections->data[i];
 
-               scf_loge("sh->name: %s, data: %p, len: %d\n", s->name->data, s->data, s->data_len);
+               scf_logd("sh->name: %s, data: %p, len: %d\n", s->name->data, s->data, s->data_len);
 
                if (s->data && s->data_len > 0)
                        fwrite(s->data, s->data_len, 1, elf->fp);
@@ -916,7 +916,7 @@ int elf32_write_rel(scf_elf_context_t* elf, uint16_t e_machine)
                        local_syms++;
        }
 
-       scf_loge("e->symbols->size: %d\n", e->symbols->size);
+       scf_logd("e->symbols->size: %d\n", e->symbols->size);
        // .symtab header
        section_header(&e->sh_symtab, shstrtab, 0, offset, symtab, nb_sections - 2, local_syms, sizeof(Elf32_Sym));
 
index a764444fe530988b0dcc3a5c02933567de41fc19..7c613ca83593d3f382b6acac6f8e2c2220f5bf32 100644 (file)
@@ -556,9 +556,12 @@ int __x64_elf_add_dyn(elf_native_t* x64, const char* sysroot)
                dyns[i].d_tag = DT_NEEDED;
                dyns[i].d_un.d_val = str->len;
 
-               scf_logd("i: %d, %s, %s\n", i, needed->data, needed->data + prefix);
+               scf_logi("i: %d, %s, %s\n", i, needed->data, needed->data + prefix);
 
-               scf_string_cat_cstr_len(str, needed->data + prefix, needed->len - prefix + 1);
+               if (!strncmp(needed->data, sysroot, strlen(sysroot)))
+                       scf_string_cat_cstr_len(str, needed->data + prefix, needed->len - prefix + 1);
+               else
+                       scf_string_cat_cstr_len(str, needed->data, needed->len + 1);
        }
 
        dyns[i].d_tag     = DT_STRTAB;
diff --git a/examples/coroutine_printf.c b/examples/coroutine_printf.c
new file mode 100644 (file)
index 0000000..7da877e
--- /dev/null
@@ -0,0 +1,15 @@
+
+int printf(const char* fmt, ...);
+
+int scf_async(uintptr_t funcptr, const char* fmt, ...);
+
+int __async_loop();
+
+int main()
+{
+       async printf("hello world, %d,%d,%d,%d, %lg,%lg,%lg,%lg, %lg,%lg,%lg,%lg, %lg-%lg\n", 1, 2, 3, 4,
+                       0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.1);
+
+       __async_loop();
+       return 0;
+}
diff --git a/examples/init_array_reshape.c b/examples/init_array_reshape.c
new file mode 100644 (file)
index 0000000..5770ab5
--- /dev/null
@@ -0,0 +1,25 @@
+int printf(const char* fmt, ...);
+
+int main()
+{
+       int a[2][2] = {1,  2,  3,  4};
+
+       int v = 3;
+       int i;
+       int j;
+
+       for (i = 0; i < 2; i++) {
+
+               for (j = 0; j < 2; j++) {
+
+                       if (v == a[i][j])
+                               break;
+               }
+
+               if (j < 2)
+                       break;
+       }
+
+       printf("a[%d][%d] == v: %d\n", i, j, v);
+       return 0;
+}
index d3481a336fdca44be13f5e47d7ff2e1871c0faec..6d99f57c762d960334ff701f903beb7c2e8d11d6 100644 (file)
@@ -992,12 +992,12 @@ int       _scf_risc_select_inst(scf_native_t* ctx)
                                        return ret;
                        }
 
-                       scf_loge("************ bb: %d, cmp_flag: %d\n", bb->index, bb->cmp_flag);
+                       scf_logd("************ bb: %d, cmp_flag: %d\n", bb->index, bb->cmp_flag);
                        ret = _risc_make_insts_for_list(ctx, bb, 0);
                        if (ret < 0)
                                return ret;
                        bb->native_flag = 1;
-                       scf_loge("************ bb: %d\n", bb->index);
+                       scf_logd("************ bb: %d\n", bb->index);
 
                        ret = risc_save_bb_colors(bb->dn_colors_exit, bbg, bb);
                        if (ret < 0)
index 6231fb166443978059a98de44c88484a9392f165..1bf5a695f4a5495f431dc6ac7a362fd336a606dd 100644 (file)
@@ -92,7 +92,7 @@ static int _risc_inst_call_argv(scf_native_t* ctx, scf_3ac_code_t* c, scf_functi
                        }
                }
 
-               scf_loge("i: %d, size: %d, v: %s, rabi: %s\n", i, size, v->w->text->data, rabi->name);
+               scf_logd("i: %d, size: %d, v: %s, rabi: %s\n", i, size, v->w->text->data, rabi->name);
 
                movx = NULL;
 
@@ -447,7 +447,7 @@ static int _risc_inst_call_handler(scf_native_t* ctx, scf_3ac_code_t* c)
 
        int data_rela_size = f->data_relas->size;
        int text_rela_size = f->text_relas->size;
-       scf_loge("f->data_relas->size: %d, f->text_relas->size: %d\n", f->data_relas->size, f->text_relas->size);
+       scf_logd("f->data_relas->size: %d, f->text_relas->size: %d\n", f->data_relas->size, f->text_relas->size);
 
        uint32_t opcode;
        int ret;
@@ -494,7 +494,7 @@ static int _risc_inst_call_handler(scf_native_t* ctx, scf_3ac_code_t* c)
                return save_size;
        }
 
-       scf_loge("stack_size: %d, save_size: %d\n", stack_size, save_size);
+       scf_logd("stack_size: %d, save_size: %d\n", stack_size, save_size);
 
        if (stack_size > 0) {
                assert(inst_sp);
@@ -1828,7 +1828,7 @@ static int _risc_inst_va_start_handler(scf_native_t* ctx, scf_3ac_code_t* c)
                        return -ENOMEM;
        }
 
-       scf_loge("c->srcs->size: %d\n", c->srcs->size);
+       scf_logd("c->srcs->size: %d\n", c->srcs->size);
        assert(3 == c->srcs->size);
 
        scf_register_t* rbp   = f->rops->find_register("rbp");
index 5ebd0629e29a4e0f197aca121b220d63cf2313ad..42cd47fd3dff917b09b5af195d436815f6654582 100644 (file)
@@ -1091,7 +1091,7 @@ int       _scf_x64_select_inst(scf_native_t* ctx)
                                return ret;
                }
        }
-#if 1
+#if 0
        if (x64_optimize_peephole(ctx, f) < 0) {
                scf_loge("\n");
                return -1;
index 80548d113ce3731357af8c4a57e48ed3f951815c..3c998dbfe85e0711138a5bf723d5e7820a974307 100644 (file)
@@ -122,7 +122,7 @@ int x64_reg_cached_vars(scf_register_t* r)
 
                scf_register_t* r2 = &(x64_registers[i]);
 
-               if (SCF_X64_REG_RSP == r2->id || SCF_X64_REG_RBP == r2->id)
+               if (!X64_COLOR_TYPE(r2->color) && (SCF_X64_REG_RSP == r2->id || SCF_X64_REG_RBP == r2->id))
                        continue;
 
                if (!X64_COLOR_CONFLICT(r->color, r2->color))
@@ -141,7 +141,7 @@ int x64_registers_init()
 
                scf_register_t* r = &(x64_registers[i]);
 
-               if (SCF_X64_REG_RSP == r->id || SCF_X64_REG_RBP == r->id)
+               if (!X64_COLOR_TYPE(r->color) && (SCF_X64_REG_RSP == r->id || SCF_X64_REG_RBP == r->id))
                        continue;
 
                assert(!r->dag_nodes);
@@ -163,7 +163,7 @@ void x64_registers_clear()
 
                scf_register_t* r = &(x64_registers[i]);
 
-               if (SCF_X64_REG_RSP == r->id || SCF_X64_REG_RBP == r->id)
+               if (!X64_COLOR_TYPE(r->color) && (SCF_X64_REG_RSP == r->id || SCF_X64_REG_RBP == r->id))
                        continue;
 
                if (r->dag_nodes) {
@@ -187,7 +187,7 @@ void x64_registers_print()
 
                r  = &(x64_registers[i]);
 
-               if (SCF_X64_REG_RSP == r->id || SCF_X64_REG_RBP == r->id)
+               if (!X64_COLOR_TYPE(r->color) && (SCF_X64_REG_RSP == r->id || SCF_X64_REG_RBP == r->id))
                        continue;
 
                if (!r->dag_nodes)
@@ -231,7 +231,7 @@ int x64_caller_save_regs(scf_3ac_code_t* c, uint32_t* regs, int nb_regs, int sta
                for (i = 0; i < sizeof(x64_registers) / sizeof(x64_registers[0]); i++) {
                        r  = &(x64_registers[i]);
 
-                       if (SCF_X64_REG_RSP == r->id || SCF_X64_REG_RBP == r->id)
+                       if (!X64_COLOR_TYPE(r->color) && (SCF_X64_REG_RSP == r->id || SCF_X64_REG_RBP == r->id))
                                continue;
 
                        if (0 == r->dag_nodes->size)
@@ -312,7 +312,7 @@ int x64_push_regs(scf_vector_t* instructions, uint32_t* regs, int nb_regs)
                for (i = 0; i < sizeof(x64_registers) / sizeof(x64_registers[0]); i++) {
                        r  = &(x64_registers[i]);
 
-                       if (SCF_X64_REG_RSP == r->id || SCF_X64_REG_RBP == r->id)
+                       if (!X64_COLOR_TYPE(r->color) && (SCF_X64_REG_RSP == r->id || SCF_X64_REG_RBP == r->id))
                                continue;
 
                        if (0 == r->dag_nodes->size)
@@ -351,7 +351,7 @@ int x64_pop_regs(scf_vector_t* instructions, scf_register_t** regs, int nb_regs,
                for (i = 0; i < sizeof(x64_registers) / sizeof(x64_registers[0]); i++) {
                        r  = &(x64_registers[i]);
 
-                       if (SCF_X64_REG_RSP == r->id || SCF_X64_REG_RBP == r->id)
+                       if (!X64_COLOR_TYPE(r->color) && (SCF_X64_REG_RSP == r->id || SCF_X64_REG_RBP == r->id))
                                continue;
 
                        if (0 == r->dag_nodes->size)
@@ -390,7 +390,7 @@ int x64_registers_reset()
 
                scf_register_t* r = &(x64_registers[i]);
 
-               if (SCF_X64_REG_RSP == r->id || SCF_X64_REG_RBP == r->id)
+               if (!X64_COLOR_TYPE(r->color) && (SCF_X64_REG_RSP == r->id || SCF_X64_REG_RBP == r->id))
                        continue;
 
                if (!r->dag_nodes)
@@ -482,7 +482,7 @@ scf_vector_t* x64_register_colors()
 
                scf_register_t* r = &(x64_registers[i]);
 
-               if (SCF_X64_REG_RSP == r->id || SCF_X64_REG_RBP == r->id)
+               if (!X64_COLOR_TYPE(r->color) && (SCF_X64_REG_RSP == r->id || SCF_X64_REG_RBP == r->id))
                        continue;
 
                // ah bh ch dh can't use
@@ -620,7 +620,7 @@ int x64_overflow_reg(scf_register_t* r, scf_3ac_code_t* c, scf_function_t* f)
 
                scf_register_t* r2 = &(x64_registers[i]);
 
-               if (SCF_X64_REG_RSP == r2->id || SCF_X64_REG_RBP == r2->id)
+               if (!X64_COLOR_TYPE(r2->color) && (SCF_X64_REG_RSP == r2->id || SCF_X64_REG_RBP == r2->id))
                        continue;
 
                if (!X64_COLOR_CONFLICT(r->color, r2->color))
@@ -649,7 +649,7 @@ int x64_overflow_reg2(scf_register_t* r, scf_dag_node_t* dn, scf_3ac_code_t* c,
 
                r2 = &(x64_registers[i]);
 
-               if (SCF_X64_REG_RSP == r2->id || SCF_X64_REG_RBP == r2->id)
+               if (!X64_COLOR_TYPE(r2->color) && (SCF_X64_REG_RSP == r2->id || SCF_X64_REG_RBP == r2->id))
                        continue;
 
                if (!X64_COLOR_CONFLICT(r->color, r2->color))
@@ -685,7 +685,7 @@ int x64_reg_used(scf_register_t* r, scf_dag_node_t* dn)
 
                r2 = &(x64_registers[i]);
 
-               if (SCF_X64_REG_RSP == r2->id || SCF_X64_REG_RBP == r2->id)
+               if (!X64_COLOR_TYPE(r2->color) && (SCF_X64_REG_RSP == r2->id || SCF_X64_REG_RBP == r2->id))
                        continue;
 
                if (!X64_COLOR_CONFLICT(r->color, r2->color))
@@ -754,7 +754,7 @@ scf_register_t* x64_select_overflowed_reg(scf_dag_node_t* dn, scf_3ac_code_t* c)
 
                scf_register_t* r = &(x64_registers[i]);
 
-               if (SCF_X64_REG_RSP == r->id || SCF_X64_REG_RBP == r->id)
+               if (!X64_COLOR_TYPE(r->color) && (SCF_X64_REG_RSP == r->id || SCF_X64_REG_RBP == r->id))
                        continue;
 
                if (1 == bytes) {
@@ -799,7 +799,7 @@ scf_register_t* x64_select_overflowed_reg(scf_dag_node_t* dn, scf_3ac_code_t* c)
 
                scf_register_t* r = &(x64_registers[i]);
 
-               if (SCF_X64_REG_RSP == r->id || SCF_X64_REG_RBP == r->id)
+               if (!X64_COLOR_TYPE(r->color) && (SCF_X64_REG_RSP == r->id || SCF_X64_REG_RBP == r->id))
                        continue;
 
                if (1 == bytes) {
index c96968ec7ace6718a5df0e0eba8138f75863eb6a..7e7d8541718510e9c8b099c98b6c8fbd0789fc90 100644 (file)
@@ -189,6 +189,7 @@ int main(int argc, char* argv[])
                        return -1;
                }
 
+               scf_logi("fname: %s\n", fname);
                if (scf_vector_add(vec, fname) < 0)
                        return -ENOMEM;
        }
@@ -233,25 +234,25 @@ int main(int argc, char* argv[])
                return 0;
        }
 
-#define MAIN_ADD_FILES(_objs, _sofiles) \
+#define MAIN_ADD_FILES(_objs, _sofiles, _arch) \
        do { \
-               int ret = add_sys_files(objs, sysroot, arch, _objs, sizeof(_objs) / sizeof(_objs[0])); \
+               int ret = add_sys_files(objs, sysroot, _arch, _objs, sizeof(_objs) / sizeof(_objs[0])); \
                if (ret < 0) \
                    return ret; \
                \
-               ret = add_sys_files(sofiles, sysroot, arch, _sofiles, sizeof(_sofiles) / sizeof(_sofiles[0])); \
+               ret = add_sys_files(sofiles, sysroot, _arch, _sofiles, sizeof(_sofiles) / sizeof(_sofiles[0])); \
                if (ret < 0) \
                    return ret; \
        } while (0)
 
 
        if (!strcmp(arch, "arm64") || !strcmp(arch, "naja"))
-               MAIN_ADD_FILES(__arm64_objs, __arm64_sofiles);
+               MAIN_ADD_FILES(__arm64_objs, __arm64_sofiles, "arm64");
 
        else if (!strcmp(arch, "arm32"))
-               MAIN_ADD_FILES(__arm32_objs, __arm32_sofiles);
+               MAIN_ADD_FILES(__arm32_objs, __arm32_sofiles, "arm32");
        else
-               MAIN_ADD_FILES(__objs, __sofiles);
+               MAIN_ADD_FILES(__objs, __sofiles, "x64");
 
 
        if (scf_vector_add(objs, obj) < 0) {
index d6a826da7d6376d118725ae926cde1060d443a82..15f83409e297e49ff16739e04496a57c15036fde 100644 (file)
@@ -35,14 +35,13 @@ static int _async_action_semicolon(scf_dfa_t* dfa, scf_vector_t* words, void* da
 {
        scf_parse_t*  parse = dfa->priv;
        dfa_data_t*   d     = data;
+       scf_expr_t*   e     = d->expr;
 
        if (!d->expr) {
                scf_loge("\n");
                return SCF_DFA_ERROR;
        }
 
-       scf_expr_t* e = d->expr;
-
        while (SCF_OP_EXPR == e->type) {
 
                assert(e->nodes && 1 == e->nb_nodes);
@@ -61,10 +60,13 @@ static int _async_action_semicolon(scf_dfa_t* dfa, scf_vector_t* words, void* da
        d->expr = NULL;
        d->expr_local_flag = 0;
 
-       scf_type_t*     pt = NULL;
-       scf_function_t* f  = NULL;
+       scf_type_t*     t   = NULL;
+       scf_function_t* f   = NULL;
+       scf_variable_t* v   = NULL;
+       scf_node_t*     pf  = NULL;
+       scf_node_t*     fmt = NULL;
 
-       if (scf_ast_find_type_type(&pt, parse->ast, SCF_FUNCTION_PTR) < 0)
+       if (scf_ast_find_type_type(&t, parse->ast, SCF_FUNCTION_PTR) < 0)
                return SCF_DFA_ERROR;
 
        if (scf_ast_find_function(&f, parse->ast, "scf_async") < 0)
@@ -74,26 +76,46 @@ static int _async_action_semicolon(scf_dfa_t* dfa, scf_vector_t* words, void* da
                return SCF_DFA_ERROR;
        }
 
-       scf_variable_t* var_pf = SCF_VAR_ALLOC_BY_TYPE(f->node.w, pt, 1, 1, f);
-       if (!var_pf) {
+       v = SCF_VAR_ALLOC_BY_TYPE(f->node.w, t, 1, 1, f);
+       if (!v) {
+               scf_loge("\n");
+               return SCF_DFA_ERROR;
+       }
+       v->const_literal_flag = 1;
+
+       pf = scf_node_alloc(d->current_async_w, v->type, v);
+       if (!pf) {
+               scf_loge("\n");
+               return SCF_DFA_ERROR;
+       }
+
+       if (scf_ast_find_type_type(&t, parse->ast, SCF_VAR_CHAR) < 0)
+               return SCF_DFA_ERROR;
+
+       v = SCF_VAR_ALLOC_BY_TYPE(d->current_async_w, t, 1, 1, NULL);
+       if (!v) {
                scf_loge("\n");
                return SCF_DFA_ERROR;
        }
-       var_pf->const_literal_flag = 1;
+       v->const_literal_flag = 1;
+       v->data.s = scf_string_cstr("");
 
-       scf_node_t* node_pf = scf_node_alloc(d->current_async_w, var_pf->type, var_pf);
-       if (!node_pf) {
+       fmt = scf_node_alloc(d->current_async_w, v->type, v);
+       if (!fmt) {
                scf_loge("\n");
                return SCF_DFA_ERROR;
        }
 
-       scf_node_add_child(e, node_pf);
+       scf_node_add_child(e, pf);
+       scf_node_add_child(e, fmt);
 
        int i;
-       for (i = e->nb_nodes - 2; i >= 0; i--)
-               e->nodes[i + 1] = e->nodes[i];
+       for (i = e->nb_nodes - 3; i >= 0; i--)
+               e->nodes[i + 2] = e->nodes[i];
 
-       e->nodes[0] = node_pf;
+       e->nodes[0] = pf;
+       e->nodes[1] = e->nodes[2];
+       e->nodes[2] = fmt;
 
        if (d->current_node)
                scf_node_add_child(d->current_node, e);
index eba7be0e852baff955c4ec45c0bb7f04992bf8b4..d8a10374ec383aeb0bb05dc28d836badd6a640eb 100644 (file)
@@ -133,7 +133,6 @@ static int _scf_op_const_create(scf_ast_t* ast, scf_node_t** nodes, int nb_nodes
        scf_function_t* callee2 = v2->func_ptr;
 
        if (caller != callee0) {
-
                if (scf_vector_add_unique(caller->callee_functions, callee0) < 0)
                        return -1;
 
@@ -142,7 +141,6 @@ static int _scf_op_const_create(scf_ast_t* ast, scf_node_t** nodes, int nb_nodes
        }
 
        if (caller != callee2) {
-
                if (scf_vector_add_unique(caller->callee_functions, callee2) < 0)
                        return -1;
 
@@ -170,7 +168,7 @@ static int _scf_op_const_array_index(scf_ast_t* ast, scf_node_t** nodes, int nb_
                return -1;
        }
 
-       scf_handler_data_t* d    = data;
+       scf_handler_data_t* d = data;
 
        int ret = _scf_expr_calculate_internal(ast, nodes[1], d);
 
@@ -448,7 +446,6 @@ static int _scf_op_const_call(scf_ast_t* ast, scf_node_t** nodes, int nb_nodes,
        scf_function_t* callee = v0->func_ptr;
 
        if (caller != callee) {
-
                if (scf_vector_add_unique(caller->callee_functions, callee) < 0)
                        return -1;
 
@@ -456,6 +453,16 @@ static int _scf_op_const_call(scf_ast_t* ast, scf_node_t** nodes, int nb_nodes,
                        return -1;
        }
 
+       int i;
+       for (i = 1; i < nb_nodes; i++) {
+
+               int ret = _scf_expr_calculate_internal(ast, nodes[i], data);
+               if (ret < 0) {
+                       scf_loge("\n");
+                       return -1;
+               }
+       }
+
        return 0;
 }
 
@@ -578,7 +585,7 @@ static int _scf_op_const_type_cast(scf_ast_t* ast, scf_node_t** nodes, int nb_no
        if (scf_variable_integer(src) && scf_variable_integer(dst)) {
 
                int size;
-               if (src ->nb_dimentions > 0)
+               if (src->nb_dimentions > 0)
                        size = sizeof(void*);
                else
                        size = src->size;
index dd065934849fe945b51121ac9d5444735aa48807..69d42a52a4e74ec005ea01b7affcdbf56e9b3379 100644 (file)
@@ -1,6 +1,41 @@
 #include"scf_dfa.h"
 #include"scf_parse.h"
 
+static int __reshape_index(dfa_index_t** out, scf_variable_t* array, dfa_index_t* index, int n)
+{
+       assert(array->nb_dimentions > 0);
+       assert(n > 0);
+
+       dfa_index_t* p = calloc(array->nb_dimentions, sizeof(dfa_index_t));
+       if (!p)
+               return -ENOMEM;
+
+       intptr_t i = index[n - 1].i;
+
+       scf_logw("reshape 'init exprs' from %d-dimention to %d-dimention, origin last index: %ld\n",
+                       n, array->nb_dimentions, i);
+
+       int j;
+       for (j = array->nb_dimentions - 1; j >= 0; j--) {
+
+               if (array->dimentions[j] <= 0) {
+                       scf_logw("array's %d-dimention size not set, file: %s, line: %d\n", j, array->w->file->data, array->w->line);
+
+                       free(p);
+                       return -1;
+               }
+
+               p[j].i = i % array->dimentions[j];
+               i      = i / array->dimentions[j];
+       }
+
+       for (j = 0; j < array->nb_dimentions; j++)
+               scf_logi("\033[32m dim: %d, size: %d, index: %ld\033[0m\n", j, array->dimentions[j], p[j].i);
+
+       *out = p;
+       return 0;
+}
+
 static int __array_member_init(scf_ast_t* ast, scf_lex_word_t* w, scf_variable_t* array, dfa_index_t* index, int n, scf_node_t** pnode)
 {
        if (!pnode)
@@ -13,9 +48,15 @@ static int __array_member_init(scf_ast_t* ast, scf_lex_word_t* w, scf_variable_t
                root = scf_node_alloc(NULL, array->type, array);
 
        if (n < array->nb_dimentions) {
-               scf_loge("number of indexes less than needed, array '%s', file: %s, line: %d\n",
-                               array->w->text->data, w->file->data, w->line);
-               return -1;
+               if (n <= 0) {
+                       scf_loge("number of indexes less than needed, array '%s', file: %s, line: %d\n",
+                                       array->w->text->data, w->file->data, w->line);
+                       return -1;
+               }
+
+               int ret = __reshape_index(&index, array, index, n);
+               if (ret < 0)
+                       return ret;
        }
 
        int i;
@@ -26,6 +67,11 @@ static int __array_member_init(scf_ast_t* ast, scf_lex_word_t* w, scf_variable_t
                if (k >= array->dimentions[i]) {
                        scf_loge("index [%ld] out of size [%d], in dim: %d, file: %s, line: %d\n",
                                        k, array->dimentions[i], i, w->file->data, w->line);
+
+                       if (n < array->nb_dimentions) {
+                               free(index);
+                               index = NULL;
+                       }
                        return -1;
                }
 
@@ -42,6 +88,11 @@ static int __array_member_init(scf_ast_t* ast, scf_lex_word_t* w, scf_variable_t
                root = node_op;
        }
 
+       if (n < array->nb_dimentions) {
+               free(index);
+               index = NULL;
+       }
+
        *pnode = root;
        return array->nb_dimentions;
 }
@@ -179,31 +230,33 @@ int scf_array_member_init(scf_ast_t* ast, scf_lex_word_t* w, scf_variable_t* arr
        return n;
 }
 
-int scf_array_init(scf_ast_t* ast, scf_lex_word_t* w, scf_variable_t* var, scf_vector_t* init_exprs)
+int scf_array_init(scf_ast_t* ast, scf_lex_word_t* w, scf_variable_t* v, scf_vector_t* init_exprs)
 {
        dfa_init_expr_t* ie;
 
        int unset     = 0;
        int unset_dim = -1;
+       int capacity  = 1;
        int i;
        int j;
 
-       for (i = 0; i < var->nb_dimentions; i++) {
-               assert(var->dimentions);
+       for (i = 0; i < v->nb_dimentions; i++) {
+               assert(v->dimentions);
 
-               scf_logi("dim[%d]: %d\n", i, var->dimentions[i]);
+               scf_logi("dim[%d]: %d\n", i, v->dimentions[i]);
 
-               if (var->dimentions[i] < 0) {
+               if (v->dimentions[i] < 0) {
 
                        if (unset > 0) {
                                scf_loge("array '%s' should only unset 1-dimention size, file: %s, line: %d\n",
-                                               var->w->text->data, w->file->data, w->line);
+                                               v->w->text->data, w->file->data, w->line);
                                return -1;
                        }
 
                        unset++;
                        unset_dim = i;
-               }
+               } else
+                       capacity *= v->dimentions[i];
        }
 
        if (unset) {
@@ -212,22 +265,50 @@ int scf_array_init(scf_ast_t* ast, scf_lex_word_t* w, scf_variable_t* var, scf_v
                for (i = 0; i < init_exprs->size; i++) {
                        ie =        init_exprs->data[i];
 
-                       if (unset_max < ie->index[unset_dim].i)
-                               unset_max = ie->index[unset_dim].i;
+                       if (unset_dim < ie->n) {
+                               if (unset_max < ie->index[unset_dim].i)
+                                       unset_max = ie->index[unset_dim].i;
+                       }
                }
 
-               var->dimentions[unset_dim] = unset_max + 1;
+               if (-1 == unset_max) {
+                       unset_max = init_exprs->size / capacity;
+
+                       v->dimentions[unset_dim] = unset_max;
+
+                       scf_logw("don't set %d-dimention size of array '%s', use '%d' as calculated, file: %s, line: %d\n",
+                                       unset_dim, v->w->text->data, unset_max, w->file->data, w->line);
+               } else
+                       v->dimentions[unset_dim] = unset_max + 1;
        }
 
        for (i = 0; i < init_exprs->size; i++) {
                ie =        init_exprs->data[i];
 
-               for (j = 0; j < var->nb_dimentions; j++) {
+               if (ie->n < v->nb_dimentions) {
+                       int n = ie->n;
+
+                       void* p = realloc(ie, sizeof(dfa_init_expr_t) + sizeof(dfa_index_t) * v->nb_dimentions);
+                       if (!p)
+                               return -ENOMEM;
+                       init_exprs->data[i] = p;
+
+                       ie    = p;
+                       ie->n = v->nb_dimentions;
 
-                       intptr_t index = ie->index[j].i;
+                       intptr_t index = ie->index[n - 1].i;
 
-                       scf_logi("\033[32mi: %d, dim: %d, size: %d, index: %ld\033[0m\n", i, j, var->dimentions[j], index);
+                       scf_logw("reshape 'init exprs' from %d-dimention to %d-dimention, origin last index: %ld\n", n, v->nb_dimentions, index);
+
+                       for (j = v->nb_dimentions - 1; j >= 0; j--) {
+
+                               ie->index[j].i = index % v->dimentions[j];
+                               index          = index / v->dimentions[j];
+                       }
                }
+
+               for (j = 0; j < v->nb_dimentions; j++)
+                       scf_logi("\033[32mi: %d, dim: %d, size: %d, index: %ld\033[0m\n", i, j, v->dimentions[j], ie->index[j].i);
        }
 
        for (i = 0; i < init_exprs->size; i++) {
@@ -239,7 +320,7 @@ int scf_array_init(scf_ast_t* ast, scf_lex_word_t* w, scf_variable_t* var, scf_v
                scf_node_t* assign;
                scf_node_t* node = NULL;
 
-               if (scf_array_member_init(ast, w, var, ie->index, ie->n, &node) < 0) {
+               if (scf_array_member_init(ast, w, v, ie->index, ie->n, &node) < 0) {
                        scf_loge("\n");
                        return -1;
                }
index d4dd534e0ee5057b6dd3a736516edd18de54bb0d..825f1689c11848d8f45d037b87b75ceddb191b6f 100644 (file)
@@ -725,4 +725,3 @@ int main()
        return 0;
 }
 #endif
-
index de0eb16da520f77e7813b4bc200b9fad0c3331dc..d98eb3256e84263816737997b01d368f7a8b1c93 100644 (file)
@@ -188,9 +188,9 @@ int naja_vm_init(scf_vm_t* vm, const char* path, const char* sys)
                                return -1;
                        }
 
-                       scf_loge("i: %d, ph->p_offset: %#lx, ph->p_filesz: %#lx\n", i, ph->ph.p_offset, ph->ph.p_filesz);
+                       scf_logi("i: %d, ph->p_offset: %#lx, ph->p_filesz: %#lx\n", i, ph->ph.p_offset, ph->ph.p_filesz);
 
-                       scf_loge("i: %d, ph->addr: %#lx, ph->len: %#lx, %#lx, ph->flags: %#x\n", i, ph->addr, ph->len, ph->ph.p_memsz, ph->ph.p_flags);
+                       scf_logi("i: %d, ph->addr: %#lx, ph->len: %#lx, %#lx, ph->flags: %#x\n", i, ph->addr, ph->len, ph->ph.p_memsz, ph->ph.p_flags);
 
                        if ((PF_X | PF_R) == ph->ph.p_flags)
                                vm->text   =  ph;
@@ -211,11 +211,11 @@ int naja_vm_init(scf_vm_t* vm, const char* path, const char* sys)
 
                        vm->dynamic = ph;
 
-                       scf_loge("ph->addr: %#lx, ph->len: %#lx, %#lx, ph->p_offset: %#lx\n", ph->addr, ph->len, ph->ph.p_memsz, ph->ph.p_offset);
+                       scf_logi("ph->addr: %#lx, ph->len: %#lx, %#lx, ph->p_offset: %#lx\n", ph->addr, ph->len, ph->ph.p_memsz, ph->ph.p_offset);
                }
        }
 
-       scf_loge("\n\n");
+       scf_logi("\n\n");
 
        if (vm->dynamic) {
                Elf64_Dyn* d = (Elf64_Dyn*)(vm->data->data + vm->dynamic->ph.p_offset);
@@ -226,23 +226,23 @@ int naja_vm_init(scf_vm_t* vm, const char* path, const char* sys)
                        switch (d[i].d_tag) {
 
                                case DT_STRTAB:
-                                       scf_loge("dynstr: %#lx\n", d[i].d_un.d_ptr);
+                                       scf_logi("dynstr: %#lx\n", d[i].d_un.d_ptr);
                                        vm->dynstr = d[i].d_un.d_ptr - vm->text->addr + vm->text->data;
                                        break;
 
                                case DT_SYMTAB:
-                                       scf_loge("dynsym: %#lx\n", d[i].d_un.d_ptr);
+                                       scf_logi("dynsym: %#lx\n", d[i].d_un.d_ptr);
                                        vm->dynsym = (Elf64_Sym*)(d[i].d_un.d_ptr - vm->text->addr + vm->text->data);
                                        break;
 
                                case DT_JMPREL:
-                                       scf_loge("JMPREL: %#lx\n", d[i].d_un.d_ptr);
+                                       scf_logi("JMPREL: %#lx\n", d[i].d_un.d_ptr);
                                        vm->jmprel      = (Elf64_Rela*)(d[i].d_un.d_ptr - vm->text->addr + vm->text->data);
                                        vm->jmprel_addr = d[i].d_un.d_ptr;
                                        break;
 
                                case DT_PLTGOT:
-                                       scf_loge("PLTGOT: %#lx\n", d[i].d_un.d_ptr);
+                                       scf_logi("PLTGOT: %#lx\n", d[i].d_un.d_ptr);
                                        vm->pltgot = (uint64_t*)(d[i].d_un.d_ptr - vm->data->addr + vm->data->data);
                                        break;
 
@@ -267,7 +267,7 @@ int naja_vm_init(scf_vm_t* vm, const char* path, const char* sys)
                                        }
                                }
 
-                               scf_loge("needed: %s\n", name);
+                               scf_logi("needed: %s\n", name);
 
                                void* so = dlopen(name, RTLD_LAZY);
                                if (!so) {