From a8f5061a1ce134e96b5b98ed050871599d1ba95b Mon Sep 17 00:00:00 2001 From: "yu.dongliang" <18588496441@163.com> Date: Sat, 20 May 2023 23:49:04 +0800 Subject: [PATCH] support arm32: printf() for double / float test ok. --- native/risc/scf_arm32.c | 136 ++++++++++---- native/risc/scf_risc.c | 4 +- native/risc/scf_risc_inst.c | 15 +- native/risc/scf_risc_reg_arm32.c | 63 ++++++- native/scf_native.h | 1 + parse/auto_gc.test | 31 ---- parse/create_obj_auto_release.test | 29 --- parse/malloc_free.test | 288 ----------------------------- parse/operator_overload.test | 36 ---- parse/qsort_class.test | 69 ------- parse/rbtree.test | 107 ----------- parse/vargs.test | 215 --------------------- parse/vargs_neg_float.test | 234 ----------------------- parse/variable_args.test | 11 -- 14 files changed, 175 insertions(+), 1064 deletions(-) delete mode 100644 parse/auto_gc.test delete mode 100644 parse/create_obj_auto_release.test delete mode 100644 parse/malloc_free.test delete mode 100644 parse/operator_overload.test delete mode 100644 parse/qsort_class.test delete mode 100644 parse/rbtree.test delete mode 100644 parse/vargs.test delete mode 100644 parse/vargs_neg_float.test delete mode 100644 parse/variable_args.test diff --git a/native/risc/scf_arm32.c b/native/risc/scf_arm32.c index 7a1b96d..b3193be 100644 --- a/native/risc/scf_arm32.c +++ b/native/risc/scf_arm32.c @@ -324,20 +324,7 @@ int arm32_inst_G2MF(scf_3ac_code_t* c, scf_function_t* f, scf_register_t* rs, sc offset = vd->offset; } - - if (2 == size) { - - if (offset & 0x1) { - scf_loge("memory align error\n"); - return -EINVAL; - } - - offset >>= 1; - SIZE = 1; - D = rs->id & 0x1; - V = rs->id >> 1; - - } else if (4 == size) { + if (4 == size) { if (offset & 0x3) { scf_loge("memory align error\n"); @@ -375,7 +362,7 @@ int arm32_inst_G2MF(scf_3ac_code_t* c, scf_function_t* f, scf_register_t* rs, sc if (ret < 0) return ret; - opcode = (0xe0 << 24) | (0x8 << 20) | (rb->id << 16) | (r12->id << 12) | (SIZE << 7) | r12->id; + opcode = (0xe0 << 24) | (0x8 << 20) | (rb->id << 16) | (r12->id << 12) | (0x2 << 7) | r12->id; inst = risc_make_inst(c, opcode); RISC_INST_ADD_CHECK(c->instructions, inst); @@ -560,10 +547,66 @@ int arm32_inst_ISTR2G(scf_3ac_code_t* c, scf_function_t* f, scf_register_t* rd, return 0; } +int arm32_inst_G2PF(scf_3ac_code_t* c, scf_function_t* f, scf_register_t* rs, scf_register_t* rb, int32_t offset, int size) +{ + scf_register_t* r12 = f->rops->find_register("x12"); + scf_instruction_t* inst = NULL; + + uint32_t opcode; + uint32_t SIZE = 0; + uint32_t D = 0; + uint32_t Vd = 0; + + if (offset & 0x3) { + scf_loge("memory align error\n"); + return -EINVAL; + } + + if (4 == size) { + + D = rs->id & 0x1; + Vd = rs->id >> 1; + + offset >>= 2; + SIZE = 2; + + } else if (8 == size) { + D = rs->id >> 4; + Vd = rs->id & 0xf; + + offset >>= 2; + SIZE = 3; + } else + return -EINVAL; + + if (offset >= 0 && offset < 0xff) + opcode = (0xed << 24) | (0x8 << 20) | (D << 22) | (rb->id << 16) | (Vd << 12) | (0x2 << 10) | (SIZE << 8) | offset; + + else if (offset < 0 && offset >= -0xff) { + offset = -offset; + opcode = (0xed << 24) | (0x0 << 20) | (D << 22) | (rb->id << 16) | (Vd << 12) | (0x2 << 10) | (SIZE << 8) | offset; + + } else { + int ret = arm32_inst_I2G(c, r12, offset, 4); + if (ret < 0) + return ret; + + opcode = (0xe0 << 24) | (0x8 << 20) | (rb->id << 16) | (r12->id << 12) | (0x2 << 7) | r12->id; + inst = risc_make_inst(c, opcode); + RISC_INST_ADD_CHECK(c->instructions, inst); + + opcode = (0xed << 24) | (0x8 << 20) | (D << 22) | (r12->id << 16) | (Vd << 12) | (0x2 << 10) | (SIZE << 8) | 0; + } + + inst = risc_make_inst(c, opcode); + RISC_INST_ADD_CHECK(c->instructions, inst); + + return 0; +} + int arm32_inst_G2P(scf_3ac_code_t* c, scf_function_t* f, scf_register_t* rs, scf_register_t* rb, int32_t offset, int size) { scf_register_t* r12 = f->rops->find_register("x12"); - scf_register_t* ri = NULL; scf_instruction_t* inst = NULL; uint32_t opcode; @@ -573,6 +616,9 @@ int arm32_inst_G2P(scf_3ac_code_t* c, scf_function_t* f, scf_register_t* rs, scf if (!rb) return -EINVAL; + if (RISC_COLOR_TYPE(rs->color)) + return arm32_inst_G2PF(c, f, rs, rb, offset, size); + if (1 == size) { if (offset >= 0 && offset < 0xfff) @@ -906,19 +952,7 @@ int arm32_inst_M2GF(scf_3ac_code_t* c, scf_function_t* f, scf_register_t* rd, sc } - if (2 == size) { - - if (offset & 0x1) { - scf_loge("memory align error\n"); - return -EINVAL; - } - - offset >>= 1; - SIZE = 1; - D = rd->id & 0x1; - V = rd->id >> 1; - - } else if (4 == size) { + if (4 == size) { if (offset & 0x3) { scf_loge("memory align error\n"); @@ -956,7 +990,7 @@ int arm32_inst_M2GF(scf_3ac_code_t* c, scf_function_t* f, scf_register_t* rd, sc if (ret < 0) return ret; - opcode = (0xe0 << 24) | (0x8 << 20) | (rb->id << 16) | (r12->id << 12) | (SIZE << 7) | r12->id; + opcode = (0xe0 << 24) | (0x8 << 20) | (rb->id << 16) | (r12->id << 12) | (0x2 << 7) | r12->id; inst = risc_make_inst(c, opcode); RISC_INST_ADD_CHECK(c->instructions, inst); @@ -1041,8 +1075,46 @@ scf_instruction_t* arm32_inst_FMOV_G(scf_3ac_code_t* c, scf_register_t* rd, scf_ scf_instruction_t* inst; uint32_t opcode; - opcode = (0xee << 24) | (0xe << 20) | (rd->id << 12) | rs->id; - inst = risc_make_inst(c, opcode); + uint32_t D; + uint32_t M; + uint32_t Vd; + uint32_t Vm; + uint32_t SIZE; + + if (8 == rd->bytes) { + D = rd->id >> 4; + Vd = rd->id & 0xf; + + M = rs->id >> 4; + Vm = rs->id & 0xf; + + SIZE = 3; + + } else { + D = rd->id & 0x1; + Vd = rd->id >> 1; + + M = rs->id & 0x1; + Vm = rs->id >> 1; + + SIZE = 2; + } + + if (RISC_COLOR_TYPE(rd->color)) { // float to float register + + opcode = (0xee << 24) | (0xb << 20) | (D << 22) | (Vd << 12) | (0x2 << 10) | (SIZE << 8) | (0x1 << 6) | (M << 5) | Vm; + inst = risc_make_inst(c, opcode); + + } else { // float to general register + + if (8 != rd->bytes) { + scf_loge("\n"); + return NULL; + } + + opcode = (0xec << 24) | (0x5 << 20) | ((rd->id + 1) << 16) | (rd->id << 12) | (0xb << 8) | (M << 5) | (0x1 << 4) | Vm; + inst = risc_make_inst(c, opcode); + } return inst; } diff --git a/native/risc/scf_risc.c b/native/risc/scf_risc.c index 7531842..0a4c4dd 100644 --- a/native/risc/scf_risc.c +++ b/native/risc/scf_risc.c @@ -214,10 +214,10 @@ static int _risc_function_finish(scf_native_t* ctx, scf_function_t* f) RISC_INST_ADD_CHECK(f->init_code->instructions, inst); f->init_code_bytes += inst->len; - uint32_t local = f->local_vars_size; + uint32_t local = f->local_vars_size + fp->bytes; if (!(local & 0xf)) - local += 8; + local = (local + 0xf) & ~0xf; if (local > 0xfff) { local += 1 << 12; diff --git a/native/risc/scf_risc_inst.c b/native/risc/scf_risc_inst.c index c403ea8..e5517c2 100644 --- a/native/risc/scf_risc_inst.c +++ b/native/risc/scf_risc_inst.c @@ -81,7 +81,8 @@ static int _risc_inst_call_argv(scf_native_t* ctx, scf_3ac_code_t* c, scf_functi int size = f->rops->variable_size (v); int is_float = scf_variable_float(v); - if (!rabi) { + if (!rabi || is_float != RISC_COLOR_TYPE(rabi->color)) { + rabi = f->rops->find_register_type_id_bytes(is_float, SCF_RISC_REG_X0, size); ret = f->rops->overflow_reg(rabi, c, f); @@ -91,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\n", i, size, v->w->text->data); + scf_loge("i: %d, size: %d, v: %s, rabi: %s\n", i, size, v->w->text->data, rabi->name); movx = NULL; @@ -157,7 +158,8 @@ static int _risc_inst_call_argv(scf_native_t* ctx, scf_3ac_code_t* c, scf_functi } RISC_SELECT_REG_CHECK(&rs, src->dag_node, c, f, 1); - rs = f->rops->find_register_color_bytes(rs->color, f->rops->MAX_BYTES); + + rs = f->rops->find_register_color_bytes(rs->color, is_float ? 8 : f->rops->MAX_BYTES); } if (movx) { @@ -196,6 +198,8 @@ static int _risc_inst_call_argv(scf_native_t* ctx, scf_3ac_code_t* c, scf_functi if (!f->rops->color_conflict(rd->color, rs->color)) { rd = f->rops->find_register_color_bytes(rd->color, rs->bytes); + scf_loge("i: %d, rd: %s, rs: %s\n", i, rd->name, rs->name); + if (!is_float) inst = ctx->iops->MOV_G(c, rd, rs); else @@ -463,7 +467,10 @@ static int _risc_inst_call_handler(scf_native_t* ctx, scf_3ac_code_t* c) return ret; } - f->rops->call_rabi(c, f, NULL, NULL, NULL); + if (f->rops->call_rabi_varg && pf->vargs_flag) + f->rops->call_rabi_varg(c, f); + else + f->rops->call_rabi(c, f, NULL, NULL, NULL); int32_t stack_size = _risc_inst_call_stack_size(c, f); if (stack_size > 0) { diff --git a/native/risc/scf_risc_reg_arm32.c b/native/risc/scf_risc_reg_arm32.c index c46d4eb..ef69b29 100644 --- a/native/risc/scf_risc_reg_arm32.c +++ b/native/risc/scf_risc_reg_arm32.c @@ -208,16 +208,21 @@ static int arm32_color_conflict(intptr_t c0, intptr_t c1) intptr_t id0 = c0 >> 16; intptr_t id1 = c1 >> 16; + intptr_t t0 = c0 >> 24; + intptr_t t1 = c1 >> 24; + if (c0 & c1 & 0xffff) { if (id0 == id1) return 1; - if (id0 == id1 + 1 && 0xff == (c1 & 0xffff)) - return 1; + if (0 == t0 && 0 == t1) { + if (id0 == id1 + 1 && 0xff == (c1 & 0xffff)) + return 1; - if (id0 == id1 - 1 && 0xff == (c0 & 0xffff)) - return 1; + if (id0 == id1 - 1 && 0xff == (c0 & 0xffff)) + return 1; + } } return 0; @@ -946,7 +951,7 @@ static void arm32_argv_rabi(scf_function_t* f) bp_int -= 8; f->args_int += 2; - scf_loge("i: %d, is_float: %d, size: %d, rid: %d, %s\n", i, is_float, size, rid, v->rabi->name); + scf_logd("i: %d, is_float: %d, size: %d, rid: %d, %s\n", i, is_float, size, rid, v->rabi->name); continue; } } @@ -1014,7 +1019,8 @@ void arm32_call_rabi(scf_3ac_code_t* c, scf_function_t* f, int* p_nints, int* p_ if (nints < f->rops->ABI_NB) { dn->rabi2 = f->rops->find_register_type_id_bytes(is_float, rid, size); nints += 2; - } + } else + dn->rabi2 = NULL; } src->rabi = dn->rabi2; @@ -1030,6 +1036,50 @@ void arm32_call_rabi(scf_3ac_code_t* c, scf_function_t* f, int* p_nints, int* p_ *p_ndoubles = ndoubles; } +void arm32_call_rabi_varg(scf_3ac_code_t* c, scf_function_t* f) +{ + scf_3ac_operand_t* src = NULL; + scf_dag_node_t* dn = NULL; + + int nints = 0; + int i; + + for (i = 1; i < c->srcs->size; i++) { + src = c->srcs->data[i]; + dn = src->dag_node; + dn->rabi2 = NULL; + + int size = f->rops->variable_size (dn->var); + int rid; + + if (scf_variable_float(dn->var)) + size = 8; + + if (size <= 4) { + + if (nints < f->rops->ABI_NB) + dn->rabi2 = f->rops->find_register_type_id_bytes(0, f->rops->abi_regs[nints++], size); + + } else { + while (nints < f->rops->ABI_NB) { + rid = f->rops->abi_regs[nints]; + + if (!(rid & 0x1)) + break; + + nints++; + } + + if (nints < f->rops->ABI_NB) { + dn->rabi2 = f->rops->find_register_type_id_bytes(0, rid, size); + nints += 2; + } + } + + src->rabi = dn->rabi2; + } +} + int arm32_push_callee_regs(scf_3ac_code_t* c, scf_function_t* f) { scf_instruction_t* inst; @@ -1123,6 +1173,7 @@ scf_regs_ops_t regs_ops_arm32 = .argv_rabi = arm32_argv_rabi, .call_rabi = arm32_call_rabi, + .call_rabi_varg = arm32_call_rabi_varg, .reg_used = arm32_reg_used, .reg_cached_vars = arm32_reg_cached_vars, diff --git a/native/scf_native.h b/native/scf_native.h index a8b05bc..dee18f6 100644 --- a/native/scf_native.h +++ b/native/scf_native.h @@ -124,6 +124,7 @@ struct scf_regs_ops_s void (*argv_rabi )(scf_function_t* f); void (*call_rabi )(scf_3ac_code_t* c, scf_function_t* f, int* p_nints, int* p_nfloats, int* p_ndoubles); + void (*call_rabi_varg )(scf_3ac_code_t* c, scf_function_t* f); int (*reg_used )(scf_register_t* r, scf_dag_node_t* dn); int (*reg_cached_vars )(scf_register_t* r); diff --git a/parse/auto_gc.test b/parse/auto_gc.test deleted file mode 100644 index d4594d4..0000000 --- a/parse/auto_gc.test +++ /dev/null @@ -1,31 +0,0 @@ - -uint8* scf_malloc(int size); -int scf_freep(uint8** pp); -int scf_free_array(uint8** pp, int size); - -class Point -{ - uint8* data; - - int __init__(Point* this, int size) - { - this->data = scf_malloc(size); - - return 0; - } - -}; - -int main(int i) -{ - Point* pp[2] = {NULL, NULL}; - int err; - - i = 1; - - pp[i], err = create Point(8); - - pp[i] = NULL; - - return 0; -} diff --git a/parse/create_obj_auto_release.test b/parse/create_obj_auto_release.test deleted file mode 100644 index 554f555..0000000 --- a/parse/create_obj_auto_release.test +++ /dev/null @@ -1,29 +0,0 @@ - -uint8* scf_malloc(int size); - -class Point -{ - int x; - int y; - - int __init__(Point* this, int a, int b) - { - this->x = a; - this->y = b; - return 0; - } - -}; - -int main() -{ - Point* p; - int err; - - p, err = create Point(1, 2); - - if (-1 == err) - return -1; - return 0; -} - diff --git a/parse/malloc_free.test b/parse/malloc_free.test deleted file mode 100644 index 508fd45..0000000 --- a/parse/malloc_free.test +++ /dev/null @@ -1,288 +0,0 @@ - -int scf__brk (uint8_t* addr); -uint8_t* scf__sbrk(uintptr_t inc); - -int scf_printf(const char* fmt, ...); - -struct scf_mblock_t -{ - uintptr_t prev; - uintptr_t next; - uintptr_t size; -}; - -scf_mblock_t* scf__mblocks[30]; -scf_mblock_t* scf__free_blocks = NULL; -uint8_t* scf__last_brk = NULL; - -uint8_t* scf__malloc(uintptr_t size) -{ - scf_mblock_t* b; - scf_mblock_t* b2; - - uintptr_t bytes = (sizeof(scf_mblock_t) + size + 63) >> 6 << 6; - intptr_t nblocks = sizeof(scf__mblocks) / sizeof(scf__mblocks[0]); - - uintptr_t rest; - uint8_t* addr = NULL; - uint8_t* p = NULL; - intptr_t i; - - for (i = 0; i < nblocks; i++) { - - if ((64 << i) < bytes) - continue; - - if (scf__mblocks[i]) - break; - } - - if (i == nblocks) { - - uintptr_t pages = (bytes + 4095) >> 12; - - p = scf__sbrk(pages << 12); - if (!p) - return NULL; - scf__last_brk = p + (pages << 12); - - rest = (pages << 12) - bytes; - - b = (scf_mblock_t*)p; - - b->prev = 0x3; - b->next = bytes; - - } else { - b = scf__mblocks[i]; - scf__mblocks[i] = (scf_mblock_t*)b->size; - - uintptr_t bytes2; - if (b->prev & 0x2) - bytes2 = b->next; - else - bytes2 = b->next - (uintptr_t)b; - - p = (uint8_t*)b; - - rest = bytes2 - bytes; - } - - addr = p + sizeof(scf_mblock_t); - - if (0 == rest) { - scf_printf("74, b: %p, scf__last_brk: %#lx\n", b, scf__last_brk); - return addr; - } - - p += bytes; - b2 = (scf_mblock_t*)p; - - for (; i >= 0; i--) { - - if (rest >= (64 << i)) { - b2->size = (uintptr_t)scf__mblocks[i]; - scf__mblocks[i] = b2; - break; - } - } - - b ->next = (uintptr_t)b2; - b2->prev = (uintptr_t)b; - - if (b->prev & 0x2) { - b2->prev |= 0x2; - b2->prev &= ~0x4; - b ->prev &= ~0x2; - b2->next = rest; - } - - scf_printf("100, b: %p, scf__last_brk: %#lx\n", b, scf__last_brk); - return addr; -} - -int scf__free(uint8_t* p) -{ - p -= sizeof(scf_mblock_t); - - scf_mblock_t* b = (scf_mblock_t*)p; - scf_mblock_t* b2; - scf_mblock_t* b3; - scf_mblock_t** pb; - - uintptr_t bytes; - if (b->prev & 0x2) - bytes = b->next; - else - bytes = b->next - (uintptr_t)b; - - intptr_t nblocks = sizeof(scf__mblocks) / sizeof(scf__mblocks[0]); - intptr_t i; - - for (i = nblocks - 1; i >= 0; i--) { - - if (bytes >= (64 << i)) - break; - } - - while (!(b->prev & 0x2)) { - - b2 = (scf_mblock_t*)b->next; - - uintptr_t bytes2; - if (b2->prev & 0x2) - bytes2 = b2->next; - else - bytes2 = b2->next - (uintptr_t)b2; - - for (i = nblocks - 1; i >= 0; i--) { - - if (bytes2 >= (64 << i)) - break; - } - - for (pb = &scf__mblocks[i]; *pb; pb = (scf_mblock_t**)&(*pb)->size) { - - if (*pb == b2) - break; - } - if (!*pb) - break; - - *pb = (scf_mblock_t*)b2->size; - - bytes += bytes2; - - if (b2->prev & 0x2) { - b->prev |= 0x2; - b->next = bytes; - } else { - b3 = (scf_mblock_t*)b2->next; - b->next = b2->next; - b3->prev = (uintptr_t)b | (b3->prev & 0x3); - } - } - - while (!(b->prev & 0x1)) { - - b2 = (scf_mblock_t*)(b->prev & ~0x7); - - uintptr_t bytes2 = (uintptr_t)b - (uintptr_t)b2; - - for (i = nblocks - 1; i >= 0; i--) { - - if (bytes2 >= (64 << i)) - break; - } - - for (pb = &scf__mblocks[i]; *pb; pb = (scf_mblock_t**)&(*pb)->size) { - if (*pb == b2) - break; - } - if (!*pb) - break; - - *pb = (scf_mblock_t*)b2->size; - - bytes += bytes2; - - if (b->prev & 0x2) { - b2->prev |= 0x2; - b2->next = bytes; - - } else { - b3 = (scf_mblock_t*)b->next; - b2->next = b->next; - b3->prev = (uintptr_t)b2 | (b3->prev & 0x3); - } - - b = b2; - } - - if (b->prev & 0x2) - bytes = b->next; - else - bytes = b->next - (uintptr_t)b; - - if (0x3 == (b->prev) & 0x3) { - - if (scf__last_brk == (uint8_t*)b + bytes) { - - scf_printf("211, b: %p, scf__last_brk: %#lx\n", b, scf__last_brk); - - scf__last_brk = (uint8_t*)b; - scf__brk((uint8_t*)b); - - int flag = 1; - while (flag) { - flag = 0; - - pb = &scf__free_blocks; - while (*pb) { - - b = *pb; - bytes = b->next; - - if (scf__last_brk != (uint8_t*)b + bytes) { - - pb = (scf_mblock_t**)&b->size; - continue; - } - *pb = (scf_mblock_t*)b->size; - - scf_printf("232, b: %p, scf__last_brk: %#lx\n", b, scf__last_brk); - - scf__last_brk = (uint8_t*)b; - scf__brk((uint8_t*)b); - flag = 1; - } - } - } else { - b->size = (uintptr_t)scf__free_blocks; - scf__free_blocks = b; - scf_printf("242, b: %p, scf__last_brk: %#lx\n", b, scf__last_brk); - } - return 0; - } - - for (i = nblocks - 1; i >= 0; i--) { - if (bytes >= (64 << i)) - break; - } - - b->size = (uintptr_t)scf__mblocks[i]; - scf__mblocks[i] = b; - - scf_printf("255, b: %p\n", b); - return 0; -} - -int main() -{ - uint8_t* p0 = scf__malloc(1000); - uint8_t* p1 = scf__malloc(1320); - uint8_t* p2 = scf__malloc(2510); - uint8_t* p3 = scf__malloc(4510); - uint8_t* p4 = scf__malloc(510); - uint8_t* p5 = scf__malloc(6510); - uint8_t* p6 = scf__malloc(510); - uint8_t* p7 = scf__malloc(11510); - - scf__free(p0); - - *p1 = 1; - scf__free(p1); - - *p2 = 2; - *p4 = 4; - - scf__free(p4); - scf__free(p5); - scf__free(p2); - scf__free(p6); - scf__free(p3); - - scf__free(p7); - return 0; -} - diff --git a/parse/operator_overload.test b/parse/operator_overload.test deleted file mode 100644 index c6e7c4d..0000000 --- a/parse/operator_overload.test +++ /dev/null @@ -1,36 +0,0 @@ -int printf(char* fmt, int a); - -class Point; - -Point* malloc(int size); - -class Point -{ - int x; - int y; - - Point* operator+(Point* p0, Point* p1) - { - Point* p = malloc(sizeof(Point)); - if (p) { - p->x = p0->x + p1->x; - p->y = p0->y + p1->y; - } - return p; - } -}; - -int main() -{ - Point p0 = {1, 2}; - Point p1 = {3, 4}; - - Point* pp0 = &p0; - Point* pp1 = &p1; - - Point* pp2 = pp0 + pp1; - - printf("pp2->x: %d\n", pp2->x); - printf("pp2->y: %d\n", pp2->y); - return 0; -} diff --git a/parse/qsort_class.test b/parse/qsort_class.test deleted file mode 100644 index 6ee5280..0000000 --- a/parse/qsort_class.test +++ /dev/null @@ -1,69 +0,0 @@ -int printf(char* fmt, int a); - -class Point -{ - int x; - int y; -}; - -int cmp_pt(Point* v0, Point* v1); - -int cmp_point(Point* p0, Point* p1) -{ - if (p0->x < p1->x) - return -1; - else if (p0->x > p1->x) - return 1; - return 0; -} - -int sort(Point** a, cmp_pt* cmp, int m, int n) -{ - if (m >= n) - return 0; - - int i = m; - int j = n; - - Point* t = a[i]; - - while (i < j) { - - while (i < j && cmp(t, a[j]) < 0) - j--; - - a[i] = a[j]; - a[j] = t; - - while (i < j && cmp(a[i], t) < 0) - i++; - a[j] = a[i]; - a[i] = t; - } - - sort(a, cmp, m, i - 1); - sort(a, cmp, i + 1, n); - return 0; -} - -int main() -{ - Point p0 = {0, 1}; - Point p1 = {4, 1}; - Point p2 = {2, 1}; - Point p3 = {3, 1}; - Point p4 = {1, 1}; - - Point* pp[5] = {&p0, &p1, &p2, &p3, &p4}; - - Point** a = (Point**)pp; - - sort(a, cmp_point, 0, 4); - - int i; - for (i = 0; i < 5; i++) { - printf("%d\n", pp[i]->x); - } - - return 0; -} diff --git a/parse/rbtree.test b/parse/rbtree.test deleted file mode 100644 index 5d91baa..0000000 --- a/parse/rbtree.test +++ /dev/null @@ -1,107 +0,0 @@ - -include "../lib/scf_rbtree.c"; - -struct rbtree_test_t -{ - scf_rbtree_node_t node; - int d; -}; - -static int test_cmp(scf_rbtree_node_t* node0, void* data) -{ - rbtree_test_t* v0 = (rbtree_test_t*)node0; - rbtree_test_t* v1 = (rbtree_test_t*)data; - - if (v0->d < v1->d) - return -1; - else if (v0->d > v1->d) - return 1; - return 0; -} - -static int test_find(scf_rbtree_node_t* node0, void* data) -{ - rbtree_test_t* v0 = (rbtree_test_t*)node0; - int d1 = (intptr_t)data; - - if (v0->d < d1) - return -1; - else if (v0->d > d1) - return 1; - return 0; -} - -static int test_print(scf_rbtree_node_t* node0, void* data) -{ - rbtree_test_t* v0 = (rbtree_test_t*)node0; - - scf_printf("v0->d: %d\n", v0->d); - return 0; -} - -const int N = 37; - -int main() -{ - scf_rbtree_t tree; - scf_rbtree_init(&tree); - - rbtree_test_t* d; - - int i; - for (i = 0; i < N; i++) { - d = scf__calloc(1, sizeof(rbtree_test_t)); - - d->d = i; - - int ret = scf_rbtree_insert(&tree, &d->node, test_cmp); - if (ret < 0) - return -1; - } - - scf_rbtree_foreach(&tree, tree->root, NULL, test_print); - - scf_rbtree_depth(&tree, tree->root); - - for (i = 0; i < N / 2; i++) { - d = (rbtree_test_t*) scf_rbtree_find(&tree, (void*)(intptr_t)i, test_find); - - int ret = scf_rbtree_delete(&tree, &d->node); - - scf__free(d); - d = NULL; - } - - scf_rbtree_foreach(&tree, tree->root, NULL, test_print); - - scf_rbtree_depth(&tree, tree->root); - - for (i = 0; i < N / 2; i++) { - d = scf__calloc(1, sizeof(rbtree_test_t)); - - d->d = i; - - int ret = scf_rbtree_insert(&tree, &d->node, test_cmp); - if (ret < 0) - return -1; - } - scf_rbtree_foreach_reverse(&tree, tree->root, NULL, test_print); - - scf_rbtree_depth(&tree, tree->root); - - for (i = 0; i < N; i++) { - d = (rbtree_test_t*) scf_rbtree_find(&tree, (void*)(intptr_t)i, test_find); - - int ret = scf_rbtree_delete(&tree, &d->node); - - scf__free(d); - d = NULL; - } - - scf_rbtree_foreach_reverse(&tree, tree->root, NULL, test_print); - scf_rbtree_depth(&tree, tree->root); - - scf_printf("tree->root: %p, sentinel: %p\n", tree->root, tree->sentinel); - return 0; -} - diff --git a/parse/vargs.test b/parse/vargs.test deleted file mode 100644 index 041360f..0000000 --- a/parse/vargs.test +++ /dev/null @@ -1,215 +0,0 @@ -struct va_list -{ - uint8_t* iptr; - uint8_t* fptr; - uint8_t* optr; - - intptr_t ireg; - intptr_t freg; - intptr_t others; -}; - -int scf__write(int fd, uint8_t* buf, uint64_t size); - -int scf_ulong2a(char* buf, int* pn, int size, uint64_t num) -{ - int n = *pn; - int i = n; - - while (n < size - 1) { - - buf[n++] = num % 10 + '0'; - - num /= 10; - if (0 == num) - break; - } - - *pn = n--; - - while (i < n) { - char c = buf[i]; - buf[i++] = buf[n]; - buf[n--] = c; - } - return *pn; -} - -int scf_long2a(char* buf, int* pn, int size, int64_t num) -{ - int n = *pn; - - if (n < size - 1 && num < 0) { - buf[n++] = '-'; - num = -num; - } - - *pn = n; - return scf_ulong2a(buf, pn, size, num); -} - -int scf_hex2a(char* buf, int* pn, int size, uint64_t num) -{ - int n = *pn; - int i = n; - - while (n < size - 1) { - - uint8_t x = num % 16; - - if (x > 9) - buf[n++] = x - 10 + 'a'; - else - buf[n++] = x + '0'; - - num /= 16; - if (0 == num) - break; - } - - *pn = n--; - - while (i < n) { - char c = buf[i]; - buf[i++] = buf[n]; - buf[n--] = c; - } - return *pn; -} - -int scf_hex2a_prefix(char* buf, int* pn, int size, uint64_t num) -{ - int n = *pn; - - if (n < size - 1 - 2) { - buf[n++] = '0'; - buf[n++] = 'x'; - } - - *pn = n; - return scf_hex2a(buf, pn, size, num); -} - -int scf_p2a(char* buf, int* pn, int size, uint64_t num) -{ - if (0 == num) { - int n = *pn; - char* p = "null"; - - while (n < size - 1 && *p) - buf[n++] = *p++; - - *pn = n; - return n; - } - - return scf_hex2a_prefix(buf, pn, size, num); -} - -int scf_str2a(char* buf, int* pn, int size, const char* str) -{ - int n = *pn; - - while (n < size - 1 && *str) - buf[n++] = *str++; - - *pn = n; - return n; -} - -int scf_vsnprintf(char* buf, int size, const char* fmt, va_list* ap) -{ - int n = 0; - - while (*fmt) { - - if ('%' != *fmt) { - buf[n++] = *fmt++; - continue; - } - - fmt++; - if ('%' == *fmt) { - buf[n++] = *fmt++; - continue; - } - - int prefix = 0; - if ('#' == *fmt) { - prefix++; - fmt++; - } - - int l = 0; - if ('l' == *fmt) { - l++; - fmt++; - } - - if ('c' == *fmt) - buf[n++] = va_arg(ap, int32_t); - - else if ('u' == *fmt) { - if (l) - scf_ulong2a(buf, &n, size, va_arg(ap, uint64_t)); - else - scf_ulong2a(buf, &n, size, va_arg(ap, uint32_t)); - - } else if ('d' == *fmt) { - if (l) - scf_long2a(buf, &n, size, va_arg(ap, int64_t)); - else - scf_long2a(buf, &n, size, va_arg(ap, int32_t)); - - } else if ('x' == *fmt) { - if (prefix) { - if (l) - scf_hex2a_prefix(buf, &n, size, va_arg(ap, uint64_t)); - else - scf_hex2a_prefix(buf, &n, size, va_arg(ap, uint32_t)); - } else { - if (l) - scf_hex2a(buf, &n, size, va_arg(ap, uint64_t)); - else - scf_hex2a(buf, &n, size, va_arg(ap, uint32_t)); - } - } else if ('p' == *fmt) - scf_p2a(buf, &n, size, va_arg(ap, uint64_t)); - - else if ('s' == *fmt) - scf_str2a(buf, &n, size, va_arg(ap, char*)); - else - return -1; - - fmt++; - } - - buf[n] = '\0'; - return n; -} - -int scf_printf(const char* fmt, ...) -{ - va_list ap; - - char buf[100]; - - va_start(ap, fmt); - int ret = scf_vsnprintf(buf, sizeof(buf) - 1, fmt, &ap); - va_end(ap); - - if (ret > 0) { - ret = scf__write(1, buf, ret); - } - - return ret; -} - -int main() -{ - int64_t i64 = -1245; - - int ret = scf_printf("hello: %u, %x, %d, %ld, %p, %s\n", 1234, 0x7f1c, -5, i64, &i64, "world"); - return ret; -} - diff --git a/parse/vargs_neg_float.test b/parse/vargs_neg_float.test deleted file mode 100644 index 742a4a7..0000000 --- a/parse/vargs_neg_float.test +++ /dev/null @@ -1,234 +0,0 @@ -struct va_list -{ - uint8_t* iptr; - uint8_t* fptr; - uint8_t* optr; - - intptr_t ireg; - intptr_t freg; - intptr_t others; -}; - -int scf__write(int fd, uint8_t* buf, uint64_t size); - -int scf_ulong2a(char* buf, int* pn, int size, uint64_t num) -{ - int n = *pn; - int i = n; - - while (n < size - 1) { - - buf[n++] = num % 10 + '0'; - - num /= 10; - if (0 == num) - break; - } - - *pn = n--; - - while (i < n) { - char c = buf[i]; - buf[i++] = buf[n]; - buf[n--] = c; - } - return *pn; -} - -int scf_long2a(char* buf, int* pn, int size, int64_t num) -{ - int n = *pn; - - if (n < size - 1 && num < 0) { - buf[n++] = '-'; - num = -num; - } - - *pn = n; - return scf_ulong2a(buf, pn, size, num); -} - -int scf_hex2a(char* buf, int* pn, int size, uint64_t num) -{ - int n = *pn; - int i = n; - - while (n < size - 1) { - - uint8_t x = num % 16; - - if (x > 9) - buf[n++] = x - 10 + 'a'; - else - buf[n++] = x + '0'; - - num /= 16; - if (0 == num) - break; - } - - *pn = n--; - - while (i < n) { - char c = buf[i]; - buf[i++] = buf[n]; - buf[n--] = c; - } - return *pn; -} - -int scf_hex2a_prefix(char* buf, int* pn, int size, uint64_t num) -{ - int n = *pn; - - if (n < size - 1 - 2) { - buf[n++] = '0'; - buf[n++] = 'x'; - } - - *pn = n; - return scf_hex2a(buf, pn, size, num); -} - -int scf_p2a(char* buf, int* pn, int size, uint64_t num) -{ - if (0 == num) { - int n = *pn; - char* p = "null"; - - while (n < size - 1 && *p) - buf[n++] = *p++; - - *pn = n; - return n; - } - - return scf_hex2a_prefix(buf, pn, size, num); -} - -int scf_str2a(char* buf, int* pn, int size, const char* str) -{ - int n = *pn; - - while (n < size - 1 && *str) - buf[n++] = *str++; - - *pn = n; - return n; -} - -int scf_double2a(char* buf, int* pn, int size, double num) -{ - if (*pn < size - 1 && num < 0.0) { - buf[(*pn)++] = '-'; - num = -num; - } - - int64_t l = (int64_t)num; - int64_t diff = (int64_t)((num - l) * 1000000); - - scf_ulong2a(buf, pn, size, l); - - if (*pn < size - 1) - buf[(*pn)++] = '.'; - - return scf_ulong2a(buf, pn, size, diff); -} - -int scf_vsnprintf(char* buf, int size, const char* fmt, va_list* ap) -{ - int n = 0; - - while (*fmt) { - - if ('%' != *fmt) { - buf[n++] = *fmt++; - continue; - } - - fmt++; - if ('%' == *fmt) { - buf[n++] = *fmt++; - continue; - } - - int prefix = 0; - if ('#' == *fmt) { - prefix++; - fmt++; - } - - int l = 0; - if ('l' == *fmt) { - l++; - fmt++; - } - - if ('c' == *fmt) - buf[n++] = va_arg(ap, int32_t); - - else if ('u' == *fmt) { - if (l) - scf_ulong2a(buf, &n, size, va_arg(ap, uint64_t)); - else - scf_ulong2a(buf, &n, size, va_arg(ap, uint32_t)); - - } else if ('d' == *fmt) { - if (l) - scf_long2a(buf, &n, size, va_arg(ap, int64_t)); - else - scf_long2a(buf, &n, size, va_arg(ap, int32_t)); - - } else if ('x' == *fmt) { - if (prefix) { - if (l) - scf_hex2a_prefix(buf, &n, size, va_arg(ap, uint64_t)); - else - scf_hex2a_prefix(buf, &n, size, va_arg(ap, uint32_t)); - } else { - if (l) - scf_hex2a(buf, &n, size, va_arg(ap, uint64_t)); - else - scf_hex2a(buf, &n, size, va_arg(ap, uint32_t)); - } - } else if ('p' == *fmt) - scf_p2a(buf, &n, size, va_arg(ap, uint64_t)); - - else if ('s' == *fmt) - scf_str2a(buf, &n, size, va_arg(ap, char*)); - - else if ('f' == *fmt) - scf_double2a(buf, &n, size, va_arg(ap, double)); - else - return -1; - - fmt++; - } - - buf[n] = '\0'; - return n; -} - -int scf_printf(const char* fmt, ...) -{ - va_list ap; - - char buf[100]; - - va_start(ap, fmt); - int ret = scf_vsnprintf(buf, sizeof(buf) - 1, fmt, &ap); - va_end(ap); - - if (ret > 0) { - ret = scf__write(1, buf, ret); - } - - return ret; -} - -int main() -{ - int ret = scf_printf("hello: %f, %f\n", 11.5, -1.5); - return ret; -} - diff --git a/parse/variable_args.test b/parse/variable_args.test deleted file mode 100644 index bab50f7..0000000 --- a/parse/variable_args.test +++ /dev/null @@ -1,11 +0,0 @@ -int printf(char* fmt, ...); - -int main() -{ - int a = 1; - - printf("a: %d\n", a); - printf("hello\n"); - printf("a: %d, b: %d\n", a, 1); - return 0; -} -- 2.25.1