From: yu.dongliang <18588496441@163.com> Date: Thu, 24 Sep 2026 03:52:17 +0000 (+0800) Subject: rename 'scf__auto_*()' to '__builtin_auto_*()' as C compiler habits. X-Git-Url: http://baseworks.info/?a=commitdiff_plain;h=a7e7a665d68b9b43b742642eb1e5cdab26406bdc;p=scf.git rename 'scf__auto_*()' to '__builtin_auto_*()' as C compiler habits. --- diff --git a/core/scf_auto_gc_3ac.c b/core/scf_auto_gc_3ac.c index 9362d84..25b4a87 100644 --- a/core/scf_auto_gc_3ac.c +++ b/core/scf_auto_gc_3ac.c @@ -49,7 +49,7 @@ static scf_3ac_code_t* _auto_gc_code_ref(scf_list_t* dag_list_head, scf_ast_t* a scf_variable_t* v; scf_type_t* t; - int ret = scf_ast_find_function(&f, ast, "scf__auto_ref"); + int ret = scf_ast_find_function(&f, ast, "__builtin_auto_ref"); if (!f) return NULL; @@ -191,7 +191,7 @@ static scf_3ac_code_t* _auto_gc_code_free_array(scf_list_t* dag_list_head, scf_a scf_variable_t* v; scf_type_t* t; - int ret = scf_ast_find_function(&f, ast, "scf__auto_free_array"); + int ret = scf_ast_find_function(&f, ast, "__builtin_auto_free_array"); if (!f) return NULL; @@ -253,7 +253,7 @@ static scf_3ac_code_t* _auto_gc_code_freep_array(scf_list_t* dag_list_head, scf_ scf_variable_t* v; scf_type_t* t; - int ret = scf_ast_find_function(&f, ast, "scf__auto_freep_array"); + int ret = scf_ast_find_function(&f, ast, "__builtin_auto_freep_array"); if (!f) return NULL; diff --git a/core/scf_auto_gc_find.c b/core/scf_auto_gc_find.c index 012d2df..0a96c7a 100644 --- a/core/scf_auto_gc_find.c +++ b/core/scf_auto_gc_find.c @@ -272,7 +272,7 @@ static int _bfs_sort_function(scf_vector_t* fqueue, scf_vector_t* functions) f->visited_flag = 0; - if (!fmalloc && !strcmp(f->node.w->text->data, "scf__auto_malloc")) + if (!fmalloc && !strcmp(f->node.w->text->data, "__builtin_auto_malloc")) fmalloc = f; } diff --git a/core/scf_optimizer_auto_gc.c b/core/scf_optimizer_auto_gc.c index 57f2a3b..9efe1bf 100644 --- a/core/scf_optimizer_auto_gc.c +++ b/core/scf_optimizer_auto_gc.c @@ -757,7 +757,7 @@ static int _auto_gc_retval(scf_dn_status_t* ds_obj, scf_dag_node_t* dn, scf_vect f2 = pf->var->func_ptr; fret = f2->rets->data[i]; - if (!strcmp(f2->node.w->text->data, "scf__auto_malloc") || fret->auto_gc_flag) { + if (!strcmp(f2->node.w->text->data, "__builtin_auto_malloc") || fret->auto_gc_flag) { assert(SCF_OP_RETURN != c->op->type); // fret = f->rets->data[i]; @@ -983,7 +983,7 @@ static int _optimize_auto_gc(scf_ast_t* ast, scf_function_t* f, scf_vector_t* fu if (!ast || !f) return -EINVAL; - if (!strcmp(f->node.w->text->data, "scf__auto_malloc")) + if (!strcmp(f->node.w->text->data, "__builtin_auto_malloc")) return 0; scf_list_t* bb_list_head = &f->basic_block_list_head; diff --git a/core/scf_optimizer_auto_gc_find.c b/core/scf_optimizer_auto_gc_find.c index 5b0ea3d..fb3775c 100644 --- a/core/scf_optimizer_auto_gc_find.c +++ b/core/scf_optimizer_auto_gc_find.c @@ -495,7 +495,7 @@ static int _auto_gc_find_ref(scf_dn_status_t* ds_obj, scf_dag_node_t* dn, scf_3a scf_logd("f2: %s, rets[%d], auto_gc_flag: %d\n", f2->node.w->text->data, i, fret->auto_gc_flag); - if (!strcmp(f2->node.w->text->data, "scf__auto_malloc")) { + if (!strcmp(f2->node.w->text->data, "__builtin_auto_malloc")) { _bb_add_ds(cur_bb, ds_obj); return 1; } @@ -896,7 +896,7 @@ static int _optimize_auto_gc_find(scf_ast_t* ast, scf_function_t* f, scf_vector_ if (!f->node.define_flag) continue; - if (!strcmp(f->node.w->text->data, "scf__auto_malloc")) + if (!strcmp(f->node.w->text->data, "__builtin_auto_malloc")) continue; ret = _auto_gc_function_find(ast, f, &f->basic_block_list_head); diff --git a/examples/auto_gc_0.c b/examples/auto_gc_0.c index 2f71432..8474cce 100644 --- a/examples/auto_gc_0.c +++ b/examples/auto_gc_0.c @@ -1,8 +1,8 @@ -#include "../sysroot/lib/scf_capi.c" +int printf(const char* fmt, ...); int* f() { - return scf__auto_malloc(sizeof(int)); + return __builtin_auto_malloc(sizeof(int)); } int main() diff --git a/examples/auto_gc_multi_rets.c b/examples/auto_gc_multi_rets.c index e2b92e5..3d7c7ef 100644 --- a/examples/auto_gc_multi_rets.c +++ b/examples/auto_gc_multi_rets.c @@ -1,9 +1,9 @@ -#include "../sysroot/lib/scf_capi.c" +int printf(const char* fmt, ...); int*, int* f() { - int* p0 = scf__auto_malloc(sizeof(int)); - int* p1 = scf__auto_malloc(sizeof(int)); + int* p0 = __builtin_auto_malloc(sizeof(int)); + int* p1 = __builtin_auto_malloc(sizeof(int)); *p0 = 1; *p1 = 2; diff --git a/examples/mat.c b/examples/mat.c index 8f696a0..98af491 100644 --- a/examples/mat.c +++ b/examples/mat.c @@ -56,7 +56,7 @@ struct mat } else return -1; - this->data = scf__auto_malloc(size); + this->data = __builtin_auto_malloc(size); if (!this->data) return -1; @@ -68,7 +68,7 @@ struct mat void __release(mat* this) { if (this->data) - scf__auto_freep(&this->data, NULL); + __builtin_auto_freep(&this->data, NULL); } }; diff --git a/examples/mat2.c b/examples/mat2.c index 78499f3..826abfa 100644 --- a/examples/mat2.c +++ b/examples/mat2.c @@ -56,7 +56,7 @@ struct mat } else return -1; - this->data = scf__auto_malloc(size); + this->data = __builtin_auto_malloc(size); if (!this->data) return -1; @@ -126,7 +126,7 @@ struct mat void __release(mat* this) { if (this->data) - scf__auto_freep(&this->data, NULL); + __builtin_auto_freep(&this->data, NULL); } }; diff --git a/examples/recursive_pointer.c b/examples/recursive_pointer.c index b2f1f8a..4605f1e 100644 --- a/examples/recursive_pointer.c +++ b/examples/recursive_pointer.c @@ -5,7 +5,7 @@ int f(int** t0, int **t1); int g(int** s0, int** s1) { if (!*s1) - *s1 = scf__auto_malloc(sizeof(int)); + *s1 = __builtin_auto_malloc(sizeof(int)); if (!*s0) f(s0, s1); @@ -15,7 +15,7 @@ int g(int** s0, int** s1) int f(int** t0, int **t1) { if (!*t0) - *t0 = scf__auto_malloc(sizeof(int)); + *t0 = __builtin_auto_malloc(sizeof(int)); if (!*t1) g(t0, t1); diff --git a/examples/ret2_err.c b/examples/ret2_err.c index 38dd665..d8327fe 100644 --- a/examples/ret2_err.c +++ b/examples/ret2_err.c @@ -11,7 +11,7 @@ struct str this->len = 0; this->capacity = 16; - this->data = scf__auto_malloc(16); + this->data = __builtin_auto_malloc(16); if (!this->data) return -1; @@ -22,7 +22,7 @@ struct str void __release(str* this) { if (this->data) - scf__auto_freep(&this->data, NULL); + __builtin_auto_freep(&this->data, NULL); } }; diff --git a/examples/scf_malloc.c b/examples/scf_malloc.c deleted file mode 100644 index bf204b0..0000000 --- a/examples/scf_malloc.c +++ /dev/null @@ -1,364 +0,0 @@ - -int brk (uint8_t* addr); -uint8_t* sbrk(uintptr_t inc); - -int printf(const char* fmt, ...); - -uint8_t* memset(uint8_t* dst, int c, uintptr_t n); -uint8_t* memcpy(uint8_t* dst, uint8_t* src, uintptr_t n); - -// min size of block is 64, so low 6 bits can be used for flags -// flags in prev_size: -// Free Last First -// 2 1 0 -struct scf_mblock_t -{ - uintptr_t magic; - uintptr_t prev_size; - uintptr_t cur_size; - uintptr_t free; -}; - -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 = sbrk(pages << 12); - if (!p) - return NULL; - scf__last_brk = p + (pages << 12); - - rest = (pages << 12) - bytes; - - b = (scf_mblock_t*)p; - - b->prev_size = 0x3; - b->cur_size = bytes; - b->magic = 0x10f0; - } else { - b = scf__mblocks[i]; - scf__mblocks[i] = (scf_mblock_t*)b->free; - - p = (uint8_t*)b; - - rest = b->cur_size - bytes; - - b->prev_size &= ~0x4; - b->magic = 0x10f0; - } - - addr = p + sizeof(scf_mblock_t); - - if (0 == rest) { - printf("%s(),%d, b: %p, scf__last_brk: %p\n", __func__, __LINE__, b, scf__last_brk); - return addr; - } - - p += bytes; - b2 = (scf_mblock_t*)p; - - for (; i >= 0; i--) { - - if (rest >= (64 << i)) { - b2->free = (uintptr_t)scf__mblocks[i]; - scf__mblocks[i] = b2; - break; - } - } - - b ->cur_size = bytes; - b2->cur_size = rest; - b2->prev_size = bytes | 0x4; - b2->magic = 0xf010; - - if (b->prev_size & 0x2) { - b->prev_size &= ~0x2; - b2->prev_size |= 0x2; - } - - printf("%s(),%d, b: %p, scf__last_brk: %p\n", __func__, __LINE__, 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 = b->cur_size; - intptr_t nblocks = sizeof(scf__mblocks) / sizeof(scf__mblocks[0]); - intptr_t i; - - if (b->prev_size & 0x4) { - printf("%s(), %d, error: double free: %p\n", __func__, __LINE__, p); - return -1; - } - - if (0x10f0 != b->magic) { - printf("%s(), %d, error: corruption free: %p\n", __func__, __LINE__, p); - return -1; - } - - b->prev_size |= 0x4; - b->magic = 0xf010; - - while (!(b->prev_size & 0x2)) { - - b2 = (scf_mblock_t*)((uintptr_t)b + bytes); - - uintptr_t bytes2 = b2->cur_size; - - for (i = nblocks - 1; i >= 0; i--) { - - if (bytes2 >= (64 << i)) - break; - } - - for (pb = &scf__mblocks[i]; *pb; pb = (scf_mblock_t**)&(*pb)->free) { - - if (*pb == b2) - break; - } - if (!*pb) - break; - - *pb = (scf_mblock_t*)b2->free; - - bytes += bytes2; - b->cur_size = bytes; - - if (b2->prev_size & 0x2) - b->prev_size |= 0x2; - else { - b3 = (scf_mblock_t*)((uintptr_t)b2 + bytes2); - - b3->prev_size = bytes | (b3->prev_size & 0x7); - } - } - - while (!(b->prev_size & 0x1)) { - - uintptr_t bytes2 = b->prev_size & ~0x7; - - b2 = (scf_mblock_t*)((uintptr_t)b - bytes2); - - bytes2 = b2->cur_size; - - for (i = nblocks - 1; i >= 0; i--) { - - if (bytes2 >= (64 << i)) - break; - } - - for (pb = &scf__mblocks[i]; *pb; pb = (scf_mblock_t**)&(*pb)->free) { - if (*pb == b2) - break; - } - if (!*pb) - break; - - *pb = (scf_mblock_t*)b2->free; - - bytes += bytes2; - b2->cur_size = bytes; - - if (b->prev_size & 0x2) - b2->prev_size |= 0x2; - else { - b3 = (scf_mblock_t*)((uintptr_t)b2 + bytes); - - b3->prev_size = bytes | (b3->prev_size & 0x7); - } - - b = b2; - } - - bytes = b->cur_size; - - if (0x7 == (b->prev_size) & 0x7) { - - if (scf__last_brk == (uint8_t*)b + bytes) { - - printf("%s(), %d, b: %p, scf__last_brk: %p\n", __func__, __LINE__, b, scf__last_brk); - scf__last_brk = (uint8_t*)b; - brk((uint8_t*)b); - - int flag = 1; - while (flag) { - flag = 0; - - pb = &scf__free_blocks; - while (*pb) { - - b = *pb; - bytes = b->cur_size; - - if (scf__last_brk != (uint8_t*)b + bytes) { - - pb = (scf_mblock_t**)&b->free; - continue; - } - *pb = (scf_mblock_t*)b->free; - - printf("%s(), %d, b: %p, scf__last_brk: %p\n", __func__, __LINE__, b, scf__last_brk); - scf__last_brk = (uint8_t*)b; - brk((uint8_t*)b); - flag = 1; - } - } - } else { - b->free = (uintptr_t)scf__free_blocks; - scf__free_blocks = b; - printf("%s(), %d, b: %p\n", __func__, __LINE__, b); - } - return 0; - } - - for (i = nblocks - 1; i >= 0; i--) { - if (bytes >= (64 << i)) - break; - } - - b->free = (uintptr_t)scf__mblocks[i]; - scf__mblocks[i] = b; - - printf("%s(), %d, b: %p\n", __func__, __LINE__, b); - return 0; -} - -uint8_t* scf__calloc(uintptr_t n, uintptr_t size) -{ - scf_mblock_t* b; - uintptr_t bytes; - uint8_t* p; - - size *= n; - - p = scf__malloc(size); - if (!p) - return NULL; - - b = (scf_mblock_t*)(p - sizeof(scf_mblock_t)); - - bytes = b->cur_size; - bytes -= sizeof(scf_mblock_t); - printf("%s(),%d, calloc, b: %p, bytes: %ld\n", __func__, __LINE__, b, bytes); - - memset(p, 0, bytes); - - return p; -} - -uint8_t* scf__realloc(uint8_t* p, uintptr_t size) -{ - scf_mblock_t* b; - scf_mblock_t* b2; - scf_mblock_t* b3; - uintptr_t bytes; - uint8_t* p2; - - b = (scf_mblock_t*)(p - sizeof(scf_mblock_t)); - - bytes = b->cur_size; - bytes -= sizeof(scf_mblock_t); - - if (bytes < size) { - p2 = scf__malloc(size); - if (!p2) - return NULL; - - memcpy(p2, p, bytes); - scf__free(p); - - printf("%s(), %d, realloc: %p->%p, size: %ld\n", __func__, __LINE__, p, p2, size); - return p2; - } - - size = (sizeof(scf_mblock_t) + 63 + size) >> 6 << 6; - bytes += sizeof(scf_mblock_t); - - if (bytes < size + 64) - return p; - - b2 = (scf_mblock_t*)((uintptr_t)b + size); - - b ->cur_size = size; - b2->cur_size = bytes - size; - - if (b->prev_size & 0x2) { - b->prev_size &= ~0x2; - b2->prev_size = size | 0x2; - } else { - b3 = (scf_mblock_t*)((uintptr_t)b + bytes); - - b2->prev_size = size; - b3->prev_size = (bytes - size) | (b3->prev_size & 0x7); - } - - b2->magic = 0x10f0; - - p2 = (uint8_t*)b2 + sizeof(scf_mblock_t); - scf__free(p2); - - printf("%s(), %d, realloc: %p, free b2: %p, size: %ld\n", __func__, __LINE__, p, p2, size); - return p; -} - -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/examples/str.c b/examples/str.c index 74bcf54..992e82d 100644 --- a/examples/str.c +++ b/examples/str.c @@ -11,7 +11,7 @@ struct str this->len = 0; this->capacity = 16; - this->data = scf__auto_malloc(16); + this->data = __builtin_auto_malloc(16); if (!this->data) return -1; @@ -22,7 +22,7 @@ struct str void __release(str* this) { if (this->data) - scf__auto_freep(&this->data, NULL); + __builtin_auto_freep(&this->data, NULL); } }; diff --git a/examples/string.c b/examples/string.c index 9d258ab..6bcb8e2 100644 --- a/examples/string.c +++ b/examples/string.c @@ -8,7 +8,7 @@ struct string int __init(string* this) { - this->data = scf__auto_malloc(16); + this->data = __builtin_auto_malloc(16); if (!this->data) return -1; @@ -23,7 +23,7 @@ struct string { int64_t len = strlen(s); - this->data = scf__auto_malloc(len + 1); + this->data = __builtin_auto_malloc(len + 1); if (!this->data) return -1; @@ -38,7 +38,7 @@ struct string int __init(string* this, const char* s, int64_t len) { - this->data = scf__auto_malloc(len + 1); + this->data = __builtin_auto_malloc(len + 1); if (!this->data) return -1; @@ -56,7 +56,7 @@ struct string int64_t len = this->len + that->len; if (len >= this->capacity) { - uint8_t* p = scf__auto_malloc(len + 16); + uint8_t* p = __builtin_auto_malloc(len + 16); if (!p) return -1; @@ -85,7 +85,7 @@ struct string int64_t len = this->len + that->len; if (s->len < len) { - s->data = scf__auto_malloc(len + 1); + s->data = __builtin_auto_malloc(len + 1); if (!s->data) return NULL, -1; } @@ -112,7 +112,7 @@ struct string void __release(string* this) { if (this->data) - scf__auto_freep(&this->data, NULL); + __builtin_auto_freep(&this->data, NULL); } }; @@ -133,4 +133,3 @@ int main() printf("### s0 == s3: %d, s0->data: %s, s2: %s\n", s0 == s3, s0->data, s2->data); return 0; } - diff --git a/examples/vla_0.c b/examples/vla_0.c deleted file mode 100644 index 1647712..0000000 --- a/examples/vla_0.c +++ /dev/null @@ -1,15 +0,0 @@ -int printf(const char* fmt, ...); - -int main(int argc, char* argv[]) -{ - int i; - for (i = 1; i < 4; i++) { - char a[i]; - - a[0] = '1'; - - printf("i: %d, a[0]: %c, %lg\n", i, a[0], 3.14); - } - - return 0; -} diff --git a/examples/vla_2.c b/examples/vla_2.c deleted file mode 100644 index d93f8f1..0000000 --- a/examples/vla_2.c +++ /dev/null @@ -1,20 +0,0 @@ -int printf(const char* fmt, ...); - -int main(int argc, char* argv[]) -{ - int i; - int j; - - for (i = 1; i < 4; i++) { - char a[i]; - - for (j = 0; j < i; j++) { - - a[j] = '1'; - - printf("i: %d, j: %d, a: %c, %lg\n", i, j, a[j], 3.14); - } - } - - return 0; -} diff --git a/parse/main.c b/parse/main.c index 7248531..56416ee 100644 --- a/parse/main.c +++ b/parse/main.c @@ -10,7 +10,20 @@ "intptr_t ireg;" \ "intptr_t freg;" \ "}; " \ -"void *memcpy(void *dest, const void *src, uintptr_t n);" +"struct __builtin_object_t {" \ +" intptr_t refs;" \ +" uintptr_t size;" \ +"};" \ +"void *memcpy(void *dest, const void *src, uintptr_t n);" \ +"void __builtin_atomic_inc(intptr_t* p);" \ +"void __builtin_atomic_dec(intptr_t* p);" \ +"int __builtin_atomic_dec_and_test(intptr_t* p);" \ +"void __builtin_release_pt(void* objdata);" \ +"void* __builtin_auto_malloc(uintptr_t size);" \ +"void __builtin_auto_ref(void* data);" \ +"void __builtin_auto_freep (void** pp, __builtin_release_pt* release);" \ +"void __builtin_auto_freep_array(void** pp, int nb_pointers, __builtin_release_pt* release);" \ +"void __builtin_auto_free_array (void** pp, int size, int nb_pointers, __builtin_release_pt* release);" static scf_string_t __builtin_h = { diff --git a/parse/scf_dfa_type.c b/parse/scf_dfa_type.c index b341ccb..22b83af 100644 --- a/parse/scf_dfa_type.c +++ b/parse/scf_dfa_type.c @@ -235,26 +235,6 @@ static int _type_action_base_type(scf_dfa_t* dfa, scf_vector_t* words, void* dat return SCF_DFA_NEXT_WORD; } -static scf_function_t* _type_find_function(scf_block_t* b, const char* name) -{ - while (b) { - if (!b->node.file_flag && !b->node.root_flag) { - b = (scf_block_t*)b->node.parent; - continue; - } - - assert(b->scope); - - scf_function_t* f = scf_scope_find_function(b->scope, name); - if (f) - return f; - - b = (scf_block_t*)b->node.parent; - } - - return NULL; -} - int _type_find_type(scf_dfa_t* dfa, dfa_identity_t* id) { scf_parse_t* parse = dfa->priv; @@ -294,7 +274,9 @@ int _type_find_type(scf_dfa_t* dfa, dfa_identity_t* id) if (SCF_FUNCTION_PTR == id->type->node.type && !id->type_def) { - id->func_ptr = _type_find_function(parse->ast->current_block, id->identity->text->data); + ret = scf_ast_find_function(&id->func_ptr, parse->ast, id->identity->text->data); + if (ret < 0) + return SCF_DFA_ERROR; if (!id->func_ptr) { scf_loge("can't find funcptr type '%s'\n", id->identity->text->data); diff --git a/parse/scf_operator_handler_semantic.c b/parse/scf_operator_handler_semantic.c index c7429c1..cde8ace 100644 --- a/parse/scf_operator_handler_semantic.c +++ b/parse/scf_operator_handler_semantic.c @@ -701,7 +701,7 @@ static int _scf_op_semantic_new(scf_ast_t* ast, scf_node_t** nodes, int nb_nodes assert(class); fmalloc = NULL; - ret = scf_ast_find_function(&fmalloc, ast, "scf__auto_malloc"); + ret = scf_ast_find_function(&fmalloc, ast, "__builtin_auto_malloc"); if (ret < 0) return ret; if (!fmalloc) { diff --git a/sysroot/lib/x64/_start.s b/sysroot/lib/_start_x64.s similarity index 100% rename from sysroot/lib/x64/_start.s rename to sysroot/lib/_start_x64.s diff --git a/sysroot/lib/scf_atomic_x64.s b/sysroot/lib/scf_atomic_x64.s new file mode 100644 index 0000000..98aa986 --- /dev/null +++ b/sysroot/lib/scf_atomic_x64.s @@ -0,0 +1,17 @@ +.text +.global __builtin_atomic_inc, __builtin_atomic_dec, __builtin_atomic_dec_and_test + +__builtin_atomic_inc: + lock incq (%rdi) + ret + +__builtin_atomic_dec: + lock decq (%rdi) + ret + +__builtin_atomic_dec_and_test: + lock decq (%rdi) + setz %al + movzbl %al, %eax + ret +.fill 3, 1, 0 diff --git a/sysroot/lib/scf_capi.c b/sysroot/lib/scf_capi.c index 384b957..c1a2eaf 100644 --- a/sysroot/lib/scf_capi.c +++ b/sysroot/lib/scf_capi.c @@ -10,15 +10,6 @@ void* calloc (uintptr_t n, uintptr_t size); void* realloc(void* p, uintptr_t size); int free (void* p); -void scf__release_pt (void* objdata); - -void* scf__auto_malloc(uintptr_t size); -void scf__auto_ref(void* data); - -void scf__auto_freep (void** pp, scf__release_pt* release); -void scf__auto_freep_array(void** pp, int nb_pointers, scf__release_pt* release); -void scf__auto_free_array (void** pp, int size, int nb_pointers, scf__release_pt* release); - uintptr_t time(uintptr_t* t); uintptr_t strlen (const char *s); @@ -27,4 +18,3 @@ int strncmp(const char *s1, const char *s2, uintptr_t n); char* strncpy(char *dst, const char *src, uintptr_t n); int memcpy (void* dst, const void* src, uintptr_t n); int memcmp (void* dst, const void* src, uintptr_t n); - diff --git a/sysroot/lib/scf_linux_api.c b/sysroot/lib/scf_linux_api.c deleted file mode 100644 index 3995bd8..0000000 --- a/sysroot/lib/scf_linux_api.c +++ /dev/null @@ -1,58 +0,0 @@ -intptr_t scf_syscall(intptr_t number, ...); - -const intptr_t SCF_read = 0; -const intptr_t SCF_write = 1; -const intptr_t SCF_open = 2; -const intptr_t SCF_close = 3; -const intptr_t SCF_stat = 4; -const intptr_t SCF_fstat = 5; -const intptr_t SCF_lstat = 6; -const intptr_t SCF_poll = 7; -const intptr_t SCF_lseek = 8; -const intptr_t SCF_mmap = 9; -const intptr_t SCF_mprotect = 10; -const intptr_t SCF_munmap = 11; - -const int O_RDONLY = 0; -const int O_WRONLY = 1; -const int O_RDWR = 2; -const int O_CREAT = 0x40; -const int O_TRUNC = 0x200; -const int O_NONBLOCK = 0x800; - -const int EBADF = 9; -const int EAGAIN = 11; -const int ENOMEM = 12; -const int EINVAL = 22; - -const int PROT_READ = 1; -const int PROT_WRITE = 2; -const int PROT_EXEC = 4; - -const int MAP_SHARED = 1; -const int MAP_PRIVATE = 2; - -struct timespec -{ - uint64_t tv_sec; /* seconds */ - intptr_t tv_nsec; /* nanoseconds */ -}; - -struct stat -{ - uint64_t st_dev; /* ID of device containing file */ - uint64_t st_ino; /* Inode number */ - uint32_t st_mode; /* File type and mode */ - uint64_t st_nlink; /* Number of hard links */ - uint32_t st_uid; /* User ID of owner */ - uint32_t st_gid; /* Group ID of owner */ - uint64_t st_rdev; /* Device ID (if special file) */ - uint64_t st_size; /* Total size, in bytes */ - uint64_t st_blksize; /* Block size for filesystem I/O */ - uint64_t st_blocks; /* Number of 512B blocks allocated */ - - struct timespec st_atim; /* Time of last access */ - struct timespec st_mtim; /* Time of last modification */ - struct timespec st_ctim; /* Time of last status change */ -}; - diff --git a/sysroot/lib/scf_object.c b/sysroot/lib/scf_object.c index 64ae090..94609c2 100644 --- a/sysroot/lib/scf_object.c +++ b/sysroot/lib/scf_object.c @@ -1,10 +1,4 @@ -struct scf_object_t -{ - intptr_t refs; - uintptr_t size; -}; - int printf(const char* fmt, ...); void* malloc (uintptr_t size); @@ -12,39 +6,34 @@ void* calloc (uintptr_t n, uintptr_t size); void* realloc(void* p, uintptr_t size); int free (void* p); -void scf__atomic_inc(intptr_t* p); -void scf__atomic_dec(intptr_t* p); -int scf__atomic_dec_and_test(intptr_t* p); - -void scf__release_pt(void* objdata); - -void* scf__auto_malloc(uintptr_t size) +void* __builtin_auto_malloc(uintptr_t size) { - scf_object_t* obj = calloc(1, sizeof(scf_object_t) + size); + __builtin_object_t* obj = calloc(1, sizeof(__builtin_object_t) + size); if (!obj) return NULL; - scf__atomic_inc(&obj->refs); + __builtin_atomic_inc(&obj->refs); obj->size = size; - void* data = (void*)obj + sizeof(scf_object_t); + void* data = (void*)obj + sizeof(__builtin_object_t); printf("%s(), obj: %p, size: %d\n", __func__, obj, size); return data; } -void scf__auto_freep(void** pp, scf__release_pt* release) +void __builtin_auto_freep(void** pp, __builtin_release_pt* release) { if (!pp || !*pp) { printf("%s()\n\n", __func__); return; } - void* data = *pp; - scf_object_t* obj = data - sizeof(scf_object_t); + void* data = *pp; - if (scf__atomic_dec_and_test(&obj->refs)) { + __builtin_object_t* obj = data - sizeof(__builtin_object_t); + + if (__builtin_atomic_dec_and_test(&obj->refs)) { printf("%s(), obj: %p\n", __func__, obj); @@ -59,12 +48,12 @@ void scf__auto_freep(void** pp, scf__release_pt* release) *pp = NULL; } -void scf__auto_freep_array(void** pp, int nb_pointers, scf__release_pt* release) +void __builtin_auto_freep_array(void** pp, int nb_pointers, __builtin_release_pt* release) { if (!pp || !*pp) return; - scf_object_t* obj; + __builtin_object_t* obj; void** p = (void**) *pp; @@ -73,7 +62,7 @@ void scf__auto_freep_array(void** pp, int nb_pointers, scf__release_pt* release) intptr_t size; intptr_t i; - obj = (void*)p - sizeof(scf_object_t); + obj = (void*)p - sizeof(__builtin_object_t); size = obj->size / sizeof(void*); @@ -85,18 +74,18 @@ void scf__auto_freep_array(void** pp, int nb_pointers, scf__release_pt* release) if (!data) continue; - obj = data - sizeof(scf_object_t); + obj = data - sizeof(__builtin_object_t); - scf__auto_freep_array(&p[i], nb_pointers - 1, release); + __builtin_auto_freep_array(&p[i], nb_pointers - 1, release); p[i] = NULL; } } - obj = (void*)p - sizeof(scf_object_t); + obj = (void*)p - sizeof(__builtin_object_t); // printf("%s(), pp: %p, p: %p, nb_pointers: %d\n\n", __func__, pp, p, nb_pointers); - if (scf__atomic_dec_and_test(&obj->refs)) { + if (__builtin_atomic_dec_and_test(&obj->refs)) { if (release && 1 == nb_pointers) release(p); @@ -109,15 +98,16 @@ void scf__auto_freep_array(void** pp, int nb_pointers, scf__release_pt* release) *pp = NULL; } -void scf__auto_free_array(void** pp, int size, int nb_pointers, scf__release_pt* release) +void __builtin_auto_free_array(void** pp, int size, int nb_pointers, __builtin_release_pt* release) { if (!pp) return; - scf_object_t* obj; - void* data; + __builtin_object_t* obj; + + void* data; + int i; - int i; for (i = 0; i < size; i++) { data = pp[i]; @@ -125,15 +115,15 @@ void scf__auto_free_array(void** pp, int size, int nb_pointers, scf__release_pt* if (!data) continue; - obj = data - sizeof(scf_object_t); + obj = data - sizeof(__builtin_object_t); - if (scf__atomic_dec_and_test(&obj->refs)) { + if (__builtin_atomic_dec_and_test(&obj->refs)) { printf("%s(), obj: %p, pp: %p, i: %d, nb_pointers: %d\n", __func__, obj, pp, i, nb_pointers); if (nb_pointers > 1) - scf__auto_freep_array(&pp[i], nb_pointers - 1, release); + __builtin_auto_freep_array(&pp[i], nb_pointers - 1, release); else { if (release) release(data); @@ -146,15 +136,14 @@ void scf__auto_free_array(void** pp, int size, int nb_pointers, scf__release_pt* } } -void scf__auto_ref(void* data) +void __builtin_auto_ref(void* data) { if (data) { - scf_object_t* obj = data - sizeof(scf_object_t); + __builtin_object_t* obj = data - sizeof(__builtin_object_t); printf("%s(), obj: %p\n", __func__, obj); - scf__atomic_inc(&obj->refs); + __builtin_atomic_inc(&obj->refs); } } - diff --git a/sysroot/lib/x64/_start.o b/sysroot/lib/x64/_start.o new file mode 100644 index 0000000..9a65885 Binary files /dev/null and b/sysroot/lib/x64/_start.o differ diff --git a/sysroot/lib/x64/scf_atomic.o b/sysroot/lib/x64/scf_atomic.o new file mode 100644 index 0000000..e388dcf Binary files /dev/null and b/sysroot/lib/x64/scf_atomic.o differ diff --git a/sysroot/lib/x64/scf_atomic.s b/sysroot/lib/x64/scf_atomic.s deleted file mode 100644 index 15dad0f..0000000 --- a/sysroot/lib/x64/scf_atomic.s +++ /dev/null @@ -1,17 +0,0 @@ -.text -.global scf__atomic_inc, scf__atomic_dec, scf__atomic_dec_and_test - -scf__atomic_inc: - lock incq (%rdi) - ret - -scf__atomic_dec: - lock decq (%rdi) - ret - -scf__atomic_dec_and_test: - lock decq (%rdi) - setz %al - movzbl %al, %eax - ret -.fill 3, 1, 0 diff --git a/sysroot/lib/x64/scf_object.o b/sysroot/lib/x64/scf_object.o new file mode 100644 index 0000000..fa447a5 Binary files /dev/null and b/sysroot/lib/x64/scf_object.o differ diff --git a/sysroot/lib/x64/scf_syscall.s b/sysroot/lib/x64/scf_syscall.s deleted file mode 100644 index 357a5de..0000000 --- a/sysroot/lib/x64/scf_syscall.s +++ /dev/null @@ -1,20 +0,0 @@ -.text -.global scf_syscall - -scf_syscall: - - movq %rdi, %rax - movq %rsi, %rdi - movq %rdx, %rsi - movq %rcx, %rdx - movq %r8, %r10 - movq %r9, %r8 - movq 8(%rsp), %r9 - -# movq 8(%rsp), %r10 -# movq 16(%rsp), %r8 -# movq 24(%rsp), %r9 - - syscall - ret -.fill 6, 1, 0