Skip to content

Commit

Permalink
Add musl
Browse files Browse the repository at this point in the history
  • Loading branch information
mohanson committed Sep 2, 2024
1 parent 6250add commit efbcbda
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 64 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 8 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions deps/musl
Submodule musl added at dcb753
10 changes: 4 additions & 6 deletions include/Makefile
Original file line number Diff line number Diff line change
@@ -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

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
6 changes: 3 additions & 3 deletions include/c-stdlib/src/malloc_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
9 changes: 3 additions & 6 deletions include/c-stdlib/src/math_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
3 changes: 1 addition & 2 deletions include/c-stdlib/src/printf_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
51 changes: 18 additions & 33 deletions include/c-stdlib/src/string_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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[] = {
Expand Down
4 changes: 2 additions & 2 deletions quickjs/ckb_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion quickjs/ckb_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
14 changes: 7 additions & 7 deletions quickjs/qjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit efbcbda

Please sign in to comment.