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;
&& !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) {
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./
--- /dev/null
+#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;
+}
#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);
scf_rbtree_init(&thread->timers);
scf_list_init (&thread->tasks);
- co_thread = thread;
+ __co_thread = thread;
*pthread = thread;
return 0;
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;
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)
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();
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;
}
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;
}
{
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) {
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)
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;
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;
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) {
}
if (task->time > 0) {
- scf_loge("\n");
scf_rbtree_delete(&thread->timers, &task->timer);
task->time = 0;
}
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) {
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) {
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) \
} \
\
scf_list_del(&task->list); \
- thread->nb_tasks--; \
+ thread->n_tasks--; \
\
scf_co_task_free(task); \
task = NULL; \
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);
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;
}
-
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;
intptr_t stack_len;
intptr_t stack_capacity;
+ int n_floats;
int err;
uint32_t events;
scf_rbtree_t timers;
scf_list_t tasks;
- int nb_tasks;
+ int n_tasks;
scf_co_task_t* current;
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
-
.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:
# rdx, task_rip
# rcx, stack_len
# r8, task_rsp0 addr
+# r9, n_floats
push %rbp
push %rdi
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
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
jmp __jmp_ret
+2:
+ call 1b
__jmp_yield:
pop %r15
pop %r14
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
#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)
{
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;
}
-
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;
}
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) {
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;
};
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));
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)) {
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)) {
}
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;
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);
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);
}
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);
}
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);
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;
}
}
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;
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;
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");
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;
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));
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);
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);
}
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);
}
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);
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;
}
}
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;
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;
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;
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;
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));
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);
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);
}
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);
}
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);
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;
}
}
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;
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;
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;
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;
}
}
- 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;
}
return -ENOMEM;
}
- scf_loge("soname: %s\n", soname);
+ scf_logi("soname: %s\n", soname);
return 0;
}
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) {
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);
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));
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;
--- /dev/null
+
+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;
+}
--- /dev/null
+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;
+}
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)
}
}
- 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;
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;
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);
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");
return ret;
}
}
-#if 1
+#if 0
if (x64_optimize_peephole(ctx, f) < 0) {
scf_loge("\n");
return -1;
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))
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);
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) {
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)
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)
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)
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)
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)
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
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))
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))
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))
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) {
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) {
return -1;
}
+ scf_logi("fname: %s\n", fname);
if (scf_vector_add(vec, fname) < 0)
return -ENOMEM;
}
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) {
{
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);
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)
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);
scf_function_t* callee2 = v2->func_ptr;
if (caller != callee0) {
-
if (scf_vector_add_unique(caller->callee_functions, callee0) < 0)
return -1;
}
if (caller != callee2) {
-
if (scf_vector_add_unique(caller->callee_functions, callee2) < 0)
return -1;
return -1;
}
- scf_handler_data_t* d = data;
+ scf_handler_data_t* d = data;
int ret = _scf_expr_calculate_internal(ast, nodes[1], d);
scf_function_t* callee = v0->func_ptr;
if (caller != callee) {
-
if (scf_vector_add_unique(caller->callee_functions, callee) < 0)
return -1;
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;
}
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;
#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)
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;
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;
}
root = node_op;
}
+ if (n < array->nb_dimentions) {
+ free(index);
+ index = NULL;
+ }
+
*pnode = root;
return array->nb_dimentions;
}
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) {
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++) {
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;
}
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;
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);
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;
}
}
- scf_loge("needed: %s\n", name);
+ scf_logi("needed: %s\n", name);
void* so = dlopen(name, RTLD_LAZY);
if (!so) {