Skip to content

Commit

Permalink
For ckb-js-vm
Browse files Browse the repository at this point in the history
  • Loading branch information
mohanson committed Dec 25, 2024
1 parent 744c62e commit 77c3efa
Show file tree
Hide file tree
Showing 27 changed files with 4,086 additions and 0 deletions.
45 changes: 45 additions & 0 deletions ckb_cell_fs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#ifndef CKB_C_STDLIB_CKB_CELL_FS_H
#define CKB_C_STDLIB_CKB_CELL_FS_H 1

typedef struct FSBlob {
uint32_t offset;
uint32_t length;
} FSBlob;

typedef struct FSEntry {
FSBlob filename;
FSBlob content;
} FSEntry;

typedef struct CellFileSystemNode {
uint32_t count;
FSEntry *files;
void *start;
} CellFileSystemNode;

typedef struct CellFileSystem {
CellFileSystemNode *current;
struct CellFileSystem *next;
} CellFileSystem;

typedef struct FSFile {
const char *filename;
const void *content;
uint32_t size;
// indicate how many active users there are, used to avoid excessive opening
// of the same file.
// Currently the only valid values are 1 and 0.
uint8_t rc;
} FSFile;

int get_file(const CellFileSystem *fs, const char *filename, FSFile **f);

int ckb_get_file(const char *filename, FSFile **file);

int load_fs(CellFileSystem **fs, void *buf, uint64_t buflen);

int ckb_load_fs(void *buf, uint64_t buflen);

void ckb_reset_fs();

#endif
14 changes: 14 additions & 0 deletions libc/assert.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef CKB_C_STDLIB_ASSERT_H_
#define CKB_C_STDLIB_ASSERT_H_

#include "ckb_syscall_apis.h"

#define assert(s) \
do { \
if (!(s)) { \
printf("Failed at %s:%d: %s\n", __FILE__, __LINE__, (#s)); \
ckb_exit(-1); \
} \
} while (0)

#endif // CKB_C_STDLIB_ASSERT_H_
16 changes: 16 additions & 0 deletions libc/ctype.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef CKB_C_STDLIB_CTYPE_H_
#define CKB_C_STDLIB_CTYPE_H_
#ifdef __cplusplus
extern "C" {
#endif

int islower(int);
int isupper(int);

int tolower(int);
int toupper(int);

#ifdef __cplusplus
}
#endif
#endif
18 changes: 18 additions & 0 deletions libc/errno.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef CKB_C_STDLIB_ERRNO_H_
#define CKB_C_STDLIB_ERRNO_H_

#ifdef __cplusplus
extern "C" {
#endif

#ifdef __GNUC__
__attribute__((const))
#endif
int *__errno_location(void);
#define errno (*__errno_location())

#ifdef __cplusplus
}
#endif

#endif
39 changes: 39 additions & 0 deletions libc/features.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#ifndef CKB_C_STDLIB_FEATURES_H_
#define CKB_C_STDLIB_FEATURES_H_

#if defined(_ALL_SOURCE) && !defined(_GNU_SOURCE)
#define _GNU_SOURCE 1
#endif

#if defined(_DEFAULT_SOURCE) && !defined(_BSD_SOURCE)
#define _BSD_SOURCE 1
#endif

#if !defined(_POSIX_SOURCE) && !defined(_POSIX_C_SOURCE) && !defined(_XOPEN_SOURCE) && !defined(_GNU_SOURCE) && \
!defined(_BSD_SOURCE) && !defined(__STRICT_ANSI__)
#define _BSD_SOURCE 1
#define _XOPEN_SOURCE 700
#endif

#if __STDC_VERSION__ >= 199901L
#define __restrict restrict
#elif !defined(__GNUC__)
#define __restrict
#endif

#if __STDC_VERSION__ >= 199901L || defined(__cplusplus)
#define __inline inline
#elif !defined(__GNUC__)
#define __inline
#endif

#if __STDC_VERSION__ >= 201112L
#elif defined(__GNUC__)
#define _Noreturn __attribute__((__noreturn__))
#else
#define _Noreturn
#endif

#define __REDIR(x, y) __typeof__(x) x __asm__(#y)

#endif
125 changes: 125 additions & 0 deletions libc/float.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#ifndef CKB_C_STDLIB_FLOAT_H_
#define CKB_C_STDLIB_FLOAT_H_

#include <stdint.h>

#undef FLT_MAX
#undef DBL_MAX
#undef LDBL_MAX
#define FLT_MAX __FLT_MAX__
#define DBL_MAX __DBL_MAX__
#define LDBL_MAX __LDBL_MAX__

typedef union {
double value;
struct {
uint32_t lsw;
uint32_t msw;
} parts;
struct {
uint64_t w;
} xparts;
} ieee_double_shape_type;

/* Get two 32 bit ints from a double. */

#define EXTRACT_WORDS(ix0, ix1, d) \
do { \
ieee_double_shape_type ew_u; \
ew_u.value = (d); \
(ix0) = ew_u.parts.msw; \
(ix1) = ew_u.parts.lsw; \
} while (0)

/* Get the more significant 32 bit int from a double. */

#define GET_HIGH_WORD(i, d) \
do { \
ieee_double_shape_type gh_u; \
gh_u.value = (d); \
(i) = gh_u.parts.msw; \
} while (0)

/* Get the less significant 32 bit int from a double. */

#define GET_LOW_WORD(i, d) \
do { \
ieee_double_shape_type gl_u; \
gl_u.value = (d); \
(i) = gl_u.parts.lsw; \
} while (0)

/* Set the more significant 32 bits of a double from an int. */

#define SET_HIGH_WORD(d, v) \
do { \
ieee_double_shape_type sh_u; \
sh_u.value = (d); \
sh_u.parts.msw = (v); \
(d) = sh_u.value; \
} while (0)

/* Set the less significant 32 bits of a double from an int. */

#define SET_LOW_WORD(d, v) \
do { \
ieee_double_shape_type sl_u; \
sl_u.value = (d); \
sl_u.parts.lsw = (v); \
(d) = sl_u.value; \
} while (0)

static const double ln2_hi = 6.93147180369123816490e-01, /* 3fe62e42 fee00000 */
ln2_lo = 1.90821492927058770002e-10, /* 3dea39ef 35793c76 */
two54 = 1.80143985094819840000e+16, /* 43500000 00000000 */
twom54 = 5.55111512312578270212e-17, /* 0x3C900000, 0x00000000 */
huge = 1.0e+300, tiny = 1.0e-300, Lg1 = 6.666666666666735130e-01, /* 3FE55555 55555593 */
Lg2 = 3.999999999940941908e-01, /* 3FD99999 9997FA04 */
Lg3 = 2.857142874366239149e-01, /* 3FD24924 94229359 */
Lg4 = 2.222219843214978396e-01, /* 3FCC71C5 1D8E78AF */
Lg5 = 1.818357216161805012e-01, /* 3FC74664 96CB03DE */
Lg6 = 1.531383769920937332e-01, /* 3FC39A09 D078C69F */
Lg7 = 1.479819860511658591e-01; /* 3FC2F112 DF3E5244 */

static const double zero = 0.0;

static const double bp[] =
{
1.0,
1.5,
},
dp_h[] =
{
0.0,
5.84962487220764160156e-01,
}, /* 0x3FE2B803, 0x40000000 */
dp_l[] =
{
0.0,
1.35003920212974897128e-08,
}, /* 0x3E4CFDEB, 0x43CFD006 */
one = 1.0, two = 2.0, two53 = 9007199254740992.0, /* 0x43400000, 0x00000000 */
/* poly coefs for (3/2)*(log(x)-2s-2/3*s**3 */
L1 = 5.99999999999994648725e-01, /* 0x3FE33333, 0x33333303 */
L2 = 4.28571428578550184252e-01, /* 0x3FDB6DB6, 0xDB6FABFF */
L3 = 3.33333329818377432918e-01, /* 0x3FD55555, 0x518F264D */
L4 = 2.72728123808534006489e-01, /* 0x3FD17460, 0xA91D4101 */
L5 = 2.30660745775561754067e-01, /* 0x3FCD864A, 0x93C9DB65 */
L6 = 2.06975017800338417784e-01, /* 0x3FCA7E28, 0x4A454EEF */
P1 = 1.66666666666666019037e-01, /* 0x3FC55555, 0x5555553E */
P2 = -2.77777777770155933842e-03, /* 0xBF66C16C, 0x16BEBD93 */
P3 = 6.61375632143793436117e-05, /* 0x3F11566A, 0xAF25DE2C */
P4 = -1.65339022054652515390e-06, /* 0xBEBBBD41, 0xC5D26BF1 */
P5 = 4.13813679705723846039e-08, /* 0x3E663769, 0x72BEA4D0 */
lg2 = 6.93147180559945286227e-01, /* 0x3FE62E42, 0xFEFA39EF */
lg2_h = 6.93147182464599609375e-01, /* 0x3FE62E43, 0x00000000 */
lg2_l = -1.90465429995776804525e-09, /* 0xBE205C61, 0x0CA86C39 */
ovt = 8.0085662595372944372e-0017, /* -(1024-log2(ovfl+.5ulp)) */
cp = 9.61796693925975554329e-01, /* 0x3FEEC709, 0xDC3A03FD =2/(3ln2) */
cp_h = 9.61796700954437255859e-01, /* 0x3FEEC709, 0xE0000000 =(float)cp */
cp_l = -7.02846165095275826516e-09, /* 0xBE3E2FE0, 0x145B01F5 =tail of cp_h*/
ivln2 = 1.44269504088896338700e+00, /* 0x3FF71547, 0x652B82FE =1/ln2 */
ivln2_h = 1.44269502162933349609e+00, /* 0x3FF71547, 0x60000000 =24b 1/ln2*/
ivln2_l = 1.92596299112661746887e-08; /* 0x3E54AE0B, 0xF85DDF44 =1/ln2 tail*/

#endif
111 changes: 111 additions & 0 deletions libc/inttypes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#ifndef CKB_C_STDLIB_INTTYPES_H_
#define CKB_C_STDLIB_INTTYPES_H_

#include <stdint.h>

#if UINTPTR_MAX == UINT64_MAX
#define __PRI64 "l"
#define __PRIPTR "l"
#else
#define __PRI64 "ll"
#define __PRIPTR ""
#endif

#define PRId8 "d"
#define PRId16 "d"
#define PRId32 "d"
#define PRId64 __PRI64 "d"

#define PRIdLEAST8 "d"
#define PRIdLEAST16 "d"
#define PRIdLEAST32 "d"
#define PRIdLEAST64 __PRI64 "d"

#define PRIdFAST8 "d"
#define PRIdFAST16 "d"
#define PRIdFAST32 "d"
#define PRIdFAST64 __PRI64 "d"

#define PRIi8 "i"
#define PRIi16 "i"
#define PRIi32 "i"
#define PRIi64 __PRI64 "i"

#define PRIiLEAST8 "i"
#define PRIiLEAST16 "i"
#define PRIiLEAST32 "i"
#define PRIiLEAST64 __PRI64 "i"

#define PRIiFAST8 "i"
#define PRIiFAST16 "i"
#define PRIiFAST32 "i"
#define PRIiFAST64 __PRI64 "i"

#define PRIo8 "o"
#define PRIo16 "o"
#define PRIo32 "o"
#define PRIo64 __PRI64 "o"

#define PRIoLEAST8 "o"
#define PRIoLEAST16 "o"
#define PRIoLEAST32 "o"
#define PRIoLEAST64 __PRI64 "o"

#define PRIoFAST8 "o"
#define PRIoFAST16 "o"
#define PRIoFAST32 "o"
#define PRIoFAST64 __PRI64 "o"

#define PRIu8 "u"
#define PRIu16 "u"
#define PRIu32 "u"
#define PRIu64 __PRI64 "u"

#define PRIuLEAST8 "u"
#define PRIuLEAST16 "u"
#define PRIuLEAST32 "u"
#define PRIuLEAST64 __PRI64 "u"

#define PRIuFAST8 "u"
#define PRIuFAST16 "u"
#define PRIuFAST32 "u"
#define PRIuFAST64 __PRI64 "u"

#define PRIx8 "x"
#define PRIx16 "x"
#define PRIx32 "x"
#define PRIx64 __PRI64 "x"

#define PRIxLEAST8 "x"
#define PRIxLEAST16 "x"
#define PRIxLEAST32 "x"
#define PRIxLEAST64 __PRI64 "x"

#define PRIxFAST8 "x"
#define PRIxFAST16 "x"
#define PRIxFAST32 "x"
#define PRIxFAST64 __PRI64 "x"

#define PRIX8 "X"
#define PRIX16 "X"
#define PRIX32 "X"
#define PRIX64 __PRI64 "X"

#define PRIXLEAST8 "X"
#define PRIXLEAST16 "X"
#define PRIXLEAST32 "X"
#define PRIXLEAST64 __PRI64 "X"

#define PRIXFAST8 "X"
#define PRIXFAST16 "X"
#define PRIXFAST32 "X"
#define PRIXFAST64 __PRI64 "X"

#define PRIdMAX __PRI64 "d"
#define PRIiMAX __PRI64 "i"
#define PRIoMAX __PRI64 "o"
#define PRIuMAX __PRI64 "u"
#define PRIxMAX __PRI64 "x"
#define PRIXMAX __PRI64 "X"

#endif
Loading

0 comments on commit 77c3efa

Please sign in to comment.