diff --git a/.gitmodules b/.gitmodules index 4319731..56fd3cf 100644 --- a/.gitmodules +++ b/.gitmodules @@ -5,3 +5,6 @@ path = deps/compiler-rt-builtins-riscv url = https://github.com/libraries/compiler-rt-builtins-riscv branch = update +[submodule "deps/musl"] + path = deps/musl + url = https://github.com/xxuejie/musl diff --git a/Makefile b/Makefile index 02c4b8a..7f91830 100644 --- a/Makefile +++ b/Makefile @@ -15,16 +15,16 @@ CFLAGS := --target=riscv64 -march=rv64imc_zba_zbb_zbc_zbs CFLAGS += -g -Os \ -Wall -Werror -Wno-nonnull -Wno-unused-function \ -fno-builtin-printf -fno-builtin-memcmp \ - -nostdinc -nostdlib\ + -nostdinc -nostdlib \ -fdata-sections -ffunction-sections CFLAGS += -I deps/ckb-c-stdlib/libc -I deps/ckb-c-stdlib CFLAGS += -I include -I include/c-stdlib CFLAGS += -I deps/compiler-rt-builtins-riscv/compiler-rt/lib/builtins -CFLAGS += -Wextra -Wno-sign-compare -Wno-missing-field-initializers -Wundef -Wuninitialized\ --Wunused -Wno-unused-parameter -Wchar-subscripts -funsigned-char -Wno-unused-function \ --DCONFIG_VERSION=\"2021-03-27-CKB\" +CFLAGS += -Wextra -Wno-sign-compare -Wno-missing-field-initializers -Wundef -Wuninitialized \ + -Wunused -Wno-unused-parameter -Wchar-subscripts -funsigned-char -Wno-unused-function \ + -DCONFIG_VERSION=\"2021-03-27-CKB\" CFLAGS += -Wno-incompatible-library-redeclaration -Wno-implicit-const-int-float-conversion -Wno-invalid-noreturn CFLAGS += -DCKB_DECLARATION_ONLY @@ -52,6 +52,10 @@ all: build/ckb-js-vm deps/compiler-rt-builtins-riscv/build/libcompiler-rt.a: cd deps/compiler-rt-builtins-riscv && make +deps/musl/release: + cd deps/musl && \ + CLANG=$(CC) ./ckb/build.sh + build/ckb-js-vm: $(STD_OBJS) $(QJS_OBJS) $(OBJDIR)/impl.o deps/compiler-rt-builtins-riscv/build/libcompiler-rt.a $(LD) $(LDFLAGS) -o $@ $^ cp $@ $@.debug diff --git a/deps/musl b/deps/musl new file mode 160000 index 0000000..dcb753e --- /dev/null +++ b/deps/musl @@ -0,0 +1 @@ +Subproject commit dcb753ebccc0539a1c60bfd69b2a7cfd88001f40 diff --git a/include/Makefile b/include/Makefile index bfb43c4..cb6e0cc 100644 --- a/include/Makefile +++ b/include/Makefile @@ -1,10 +1,8 @@ - STYLE := "{BasedOnStyle: Google, TabWidth: 4, IndentWidth: 4, UseTab: Never, SortIncludes: false, ColumnLimit: 120}" fmt: - clang-format -i -style=$(STYLE) *.c *.h - clang-format -i -style=$(STYLE) c-stdlib/*.h - clang-format -i -style=$(STYLE) c-stdlib/src/*.c - clang-format -i -style=$(STYLE) ../quickjs/qjs.c ../quickjs/std_module.c ../quickjs/std_module.h ../quickjs/ckb_module.c ../quickjs/ckb_module.h - \ No newline at end of file + clang-format-18 -i -style=$(STYLE) *.c *.h + clang-format-18 -i -style=$(STYLE) c-stdlib/*.h + clang-format-18 -i -style=$(STYLE) c-stdlib/src/*.c + clang-format-18 -i -style=$(STYLE) ../quickjs/qjs.c ../quickjs/std_module.c ../quickjs/std_module.h ../quickjs/ckb_module.c ../quickjs/ckb_module.h diff --git a/include/c-stdlib/src/malloc_impl.c b/include/c-stdlib/src/malloc_impl.c index 22ed241..72b56c8 100644 --- a/include/c-stdlib/src/malloc_impl.c +++ b/include/c-stdlib/src/malloc_impl.c @@ -6,7 +6,7 @@ #ifndef CKB_BRK_MIN extern char _end[]; /* _end is set in the linker */ -#define CKB_BRK_MIN ((uintptr_t)&_end) +#define CKB_BRK_MIN ((uintptr_t) & _end) #endif #ifndef CKB_BRK_MAX #define CKB_BRK_MAX 0x00300000 @@ -32,9 +32,9 @@ struct bin { #define CKB_CHUNK_SIZE(c) ((c)->csize & -2) #define CKB_CHUNK_PSIZE(c) ((c)->psize & -2) -#define CKB_PREV_CHUNK(c) ((struct chunk *)((char *)(c)-CKB_CHUNK_PSIZE(c))) +#define CKB_PREV_CHUNK(c) ((struct chunk *)((char *)(c) - CKB_CHUNK_PSIZE(c))) #define CKB_NEXT_CHUNK(c) ((struct chunk *)((char *)(c) + CKB_CHUNK_SIZE(c))) -#define CKB_MEM_TO_CHUNK(p) (struct chunk *)((char *)(p)-CKB_OVERHEAD) +#define CKB_MEM_TO_CHUNK(p) (struct chunk *)((char *)(p) - CKB_OVERHEAD) #define CKB_CHUNK_TO_MEM(c) (void *)((char *)(c) + CKB_OVERHEAD) #define CKB_BIN_TO_CHUNK(i) (CKB_MEM_TO_CHUNK(&mal.bins[i].head)) #define CKB_C_INUSE ((size_t)1) diff --git a/include/c-stdlib/src/math_impl.c b/include/c-stdlib/src/math_impl.c index ce38a64..82f516e 100644 --- a/include/c-stdlib/src/math_impl.c +++ b/include/c-stdlib/src/math_impl.c @@ -217,16 +217,14 @@ double fmod(double x, double y) { /* normalize x and y */ if (!ex) { - for (i = uxi << 12; i >> 63 == 0; ex--, i <<= 1) - ; + for (i = uxi << 12; i >> 63 == 0; ex--, i <<= 1); uxi <<= -ex + 1; } else { uxi &= -1ULL >> 12; uxi |= 1ULL << 52; } if (!ey) { - for (i = uy.i << 12; i >> 63 == 0; ey--, i <<= 1) - ; + for (i = uy.i << 12; i >> 63 == 0; ey--, i <<= 1); uy.i <<= -ey + 1; } else { uy.i &= -1ULL >> 12; @@ -247,8 +245,7 @@ double fmod(double x, double y) { if (i == 0) return 0 * x; uxi = i; } - for (; uxi >> 52 == 0; uxi <<= 1, ex--) - ; + for (; uxi >> 52 == 0; uxi <<= 1, ex--); /* scale result */ if (ex > 0) { diff --git a/include/c-stdlib/src/printf_impl.c b/include/c-stdlib/src/printf_impl.c index e293b13..b221003 100644 --- a/include/c-stdlib/src/printf_impl.c +++ b/include/c-stdlib/src/printf_impl.c @@ -236,8 +236,7 @@ static inline void _out_fct(char character, void *buffer, size_t idx, size_t max // 'maxsize' static inline unsigned int _strnlen_s(const char *str, size_t maxsize) { const char *s; - for (s = str; *s && maxsize--; ++s) - ; + for (s = str; *s && maxsize--; ++s); return (unsigned int)(s - str); } diff --git a/include/c-stdlib/src/string_impl.c b/include/c-stdlib/src/string_impl.c index 7df2207..13b7180 100644 --- a/include/c-stdlib/src/string_impl.c +++ b/include/c-stdlib/src/string_impl.c @@ -12,27 +12,24 @@ int ckb_exit(int8_t code); #define CKB_SS (sizeof(size_t)) #define CKB_ALIGN (sizeof(size_t) - 1) -#define CKB_ONES ((size_t)-1 / UCHAR_MAX) +#define CKB_ONES ((size_t) - 1 / UCHAR_MAX) #define CKB_HIGHS (CKB_ONES * (UCHAR_MAX / 2 + 1)) -#define CKB_HASZERO(x) (((x)-CKB_ONES) & ~(x)&CKB_HIGHS) +#define CKB_HASZERO(x) (((x) - CKB_ONES) & ~(x) & CKB_HIGHS) void *memchr(const void *src, int c, size_t n) { const unsigned char *s = src; c = (unsigned char)c; #ifdef __GNUC__ - for (; ((uintptr_t)s & CKB_ALIGN) && n && *s != c; s++, n--) - ; + for (; ((uintptr_t)s & CKB_ALIGN) && n && *s != c; s++, n--); if (n && *s != c) { typedef size_t __attribute__((__may_alias__)) word; const word *w; size_t k = CKB_ONES * c; - for (w = (const void *)s; n >= CKB_SS && !CKB_HASZERO(*w ^ k); w++, n -= CKB_SS) - ; + for (w = (const void *)s; n >= CKB_SS && !CKB_HASZERO(*w ^ k); w++, n -= CKB_SS); s = (const void *)w; } #endif - for (; n && *s != c; s++, n--) - ; + for (; n && *s != c; s++, n--); return n ? (void *)s : 0; } @@ -42,8 +39,7 @@ char *__strchrnul(const char *s, int c) { c = (unsigned char)c; if (!c) return (char *)s + strlen(s); - for (; *s && *(unsigned char *)s != c; s++) - ; + for (; *s && *(unsigned char *)s != c; s++); return (char *)s; } @@ -55,8 +51,7 @@ char *strchr(const char *s, int c) { int strncmp(const char *_l, const char *_r, size_t n) { const unsigned char *l = (void *)_l, *r = (void *)_r; if (!n--) return 0; - for (; *l && *r && n && *l == *r; l++, r++, n--) - ; + for (; *l && *r && n && *l == *r; l++, r++, n--); return *l - *r; } @@ -66,15 +61,12 @@ size_t strspn(const char *s, const char *c) { if (!c[0]) return 0; if (!c[1]) { - for (; *s == *c; s++) - ; + for (; *s == *c; s++); return s - a; } - for (; *c && BITOP(byteset, *(unsigned char *)c, |=); c++) - ; - for (; *s && BITOP(byteset, *(unsigned char *)s, &); s++) - ; + for (; *c && BITOP(byteset, *(unsigned char *)c, |=); c++); + for (; *s && BITOP(byteset, *(unsigned char *)s, &); s++); return s - a; } @@ -85,10 +77,8 @@ size_t strcspn(const char *s, const char *c) { if (!c[0] || !c[1]) return __strchrnul(s, *c) - a; memset(byteset, 0, sizeof byteset); - for (; *c && BITOP(byteset, *(unsigned char *)c, |=); c++) - ; - for (; *s && !BITOP(byteset, *(unsigned char *)s, &); s++) - ; + for (; *c && BITOP(byteset, *(unsigned char *)c, |=); c++); + for (; *s && !BITOP(byteset, *(unsigned char *)s, &); s++); return s - a; } @@ -99,24 +89,21 @@ char *strpbrk(const char *s, const char *b) { static char *twobyte_strstr(const unsigned char *h, const unsigned char *n) { uint16_t nw = n[0] << 8 | n[1], hw = h[0] << 8 | h[1]; - for (h++; *h && hw != nw; hw = hw << 8 | *++h) - ; + for (h++; *h && hw != nw; hw = hw << 8 | *++h); return *h ? (char *)h - 1 : 0; } static char *threebyte_strstr(const unsigned char *h, const unsigned char *n) { uint32_t nw = (uint32_t)n[0] << 24 | n[1] << 16 | n[2] << 8; uint32_t hw = (uint32_t)h[0] << 24 | h[1] << 16 | h[2] << 8; - for (h += 2; *h && hw != nw; hw = (hw | *++h) << 8) - ; + for (h += 2; *h && hw != nw; hw = (hw | *++h) << 8); return *h ? (char *)h - 2 : 0; } static char *fourbyte_strstr(const unsigned char *h, const unsigned char *n) { uint32_t nw = (uint32_t)n[0] << 24 | n[1] << 16 | n[2] << 8 | n[3]; uint32_t hw = (uint32_t)h[0] << 24 | h[1] << 16 | h[2] << 8 | h[3]; - for (h += 3; *h && hw != nw; hw = hw << 8 | *++h) - ; + for (h += 3; *h && hw != nw; hw = hw << 8 | *++h); return *h ? (char *)h - 3 : 0; } @@ -224,16 +211,14 @@ static char *twoway_strstr(const unsigned char *h, const unsigned char *n) { } /* Compare right half */ - for (k = MAX(ms + 1, mem); n[k] && n[k] == h[k]; k++) - ; + for (k = MAX(ms + 1, mem); n[k] && n[k] == h[k]; k++); if (n[k]) { h += k - ms; mem = 0; continue; } /* Compare left half */ - for (k = ms + 1; k > mem && n[k - 1] == h[k - 1]; k--) - ; + for (k = ms + 1; k > mem && n[k - 1] == h[k - 1]; k--); if (k <= mem) return (char *)h; h += p; mem = mem0; @@ -352,7 +337,7 @@ int toupper(int c) { return c; } -#define X(x) (((x) / 256 | (x)*256) % 65536) +#define X(x) (((x) / 256 | (x) * 256) % 65536) const unsigned short **__ctype_b_loc(void) { static const unsigned short table[] = { diff --git a/quickjs/ckb_module.c b/quickjs/ckb_module.c index 118e30a..9ba6532 100644 --- a/quickjs/ckb_module.c +++ b/quickjs/ckb_module.c @@ -13,7 +13,7 @@ // argument 3: length (optional, default to full length) // argument 4: offset (optional, default to 0) // -#define NO_VALUE ((size_t)-1) +#define NO_VALUE ((size_t) - 1) enum SyscallErrorCode { SyscallErrorUnknown = 80, @@ -455,7 +455,7 @@ int js_init_module_ckb(JSContext *ctx) { #define JS_LOADER_ARGS_SIZE 2 #define BLAKE2B_BLOCK_SIZE 32 -int load_cell_code_info_explicit(size_t *buf_size, size_t *index, const uint8_t* code_hash, uint8_t hash_type) { +int load_cell_code_info_explicit(size_t *buf_size, size_t *index, const uint8_t *code_hash, uint8_t hash_type) { int err = 0; *index = 0; err = ckb_look_for_dep_with_hash2(code_hash, hash_type, index); diff --git a/quickjs/ckb_module.h b/quickjs/ckb_module.h index cad861b..f91c81f 100644 --- a/quickjs/ckb_module.h +++ b/quickjs/ckb_module.h @@ -10,7 +10,7 @@ int js_init_module_ckb(JSContext *ctx); int read_local_file(char *buf, int size); -int load_cell_code_info_explicit(size_t *buf_size, size_t *index, const uint8_t* code_hash, uint8_t hash_type); +int load_cell_code_info_explicit(size_t *buf_size, size_t *index, const uint8_t *code_hash, uint8_t hash_type); int load_cell_code_info(size_t *buf_size, size_t *index); int load_cell_code(size_t buf_size, size_t index, uint8_t *buf); diff --git a/quickjs/qjs.c b/quickjs/qjs.c index d2c5188..4e22a39 100644 --- a/quickjs/qjs.c +++ b/quickjs/qjs.c @@ -280,13 +280,13 @@ static JSContext *JS_NewCustomContext(JSRuntime *rt) { } static const CMDOptDesc js_vm_options[] = { - { "h,help", 0, "show the help" }, - { "c", 0, "compile javascript to bytecode" }, - { "e", CMD_HAS_ARG, "run javascript from argument value" }, - { "r", 0, "read from file" }, - { "t", CMD_HAS_ARG, "specify target code_hash and hash_type in hex" }, - { "f", 0, "use file system" }, - { NULL }, + {"h,help", 0, "show the help"}, + {"c", 0, "compile javascript to bytecode"}, + {"e", CMD_HAS_ARG, "run javascript from argument value"}, + {"r", 0, "read from file"}, + {"t", CMD_HAS_ARG, "specify target code_hash and hash_type in hex"}, + {"f", 0, "use file system"}, + {NULL}, }; int main(int argc, const char **argv) {