Skip to content

Commit

Permalink
Merge pull request #57 from openwebf/feat/add-property-ic
Browse files Browse the repository at this point in the history
add property inline cache and support on/off mimalloc
  • Loading branch information
ErosZy authored Jan 17, 2024
2 parents 5ccbbdc + 1f8dbca commit 848a1eb
Show file tree
Hide file tree
Showing 35 changed files with 8,595 additions and 7,982 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ set(C_STANDARD 17)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(MI_OVERRIDE OFF)

add_definitions(-DENABLE_MI_MALLOC)

include(CheckIPOSupported)
check_ipo_supported(RESULT supported OUTPUT error)
if( supported )
Expand Down
73 changes: 65 additions & 8 deletions include/quickjs/cutils.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* C utilities
*
*
* Copyright (c) 2017 Fabrice Bellard
* Copyright (c) 2018 Charlie Gordon
*
Expand Down Expand Up @@ -28,14 +28,31 @@
#include <stdlib.h>
#include <inttypes.h>

#if ENABLE_MI_MALLOC
#include "mimalloc.h"
#endif

#ifdef _MSC_VER
#include <intrin.h>
#endif

/* set if CPU is big endian */
#undef WORDS_BIGENDIAN

#ifdef _MSC_VER
#define likely(x) (x)
#define unlikely(x) (x)
#define force_inline __forceinline
#define no_inline __declspec(noinline)
#define __maybe_unused
#define __attribute__(...)
#else
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
#define force_inline inline __attribute__((always_inline))
#define no_inline __attribute__((noinline))
#define __maybe_unused __attribute__((unused))
#endif

#define xglue(x, y) x ## y
#define glue(x, y) xglue(x, y)
Expand Down Expand Up @@ -114,27 +131,66 @@ static inline int64_t min_int64(int64_t a, int64_t b)
/* WARNING: undefined if a = 0 */
static inline int clz32(unsigned int a)
{
#ifdef _MSC_VER
unsigned long idx;
_BitScanReverse(&idx, a);
return 31 ^ idx;
#else
return __builtin_clz(a);
#endif
}

/* WARNING: undefined if a = 0 */
static inline int clz64(uint64_t a)
{
#ifdef _MSC_VER
unsigned long idx;
_BitScanReverse64(&idx, a);
return 63 ^ idx;
#else
return __builtin_clzll(a);
#endif
}

/* WARNING: undefined if a = 0 */
static inline int ctz32(unsigned int a)
{
#ifdef _MSC_VER
unsigned long idx;
_BitScanForward(&idx, a);
return 31 ^ idx;
#else
return __builtin_ctz(a);
#endif
}

/* WARNING: undefined if a = 0 */
static inline int ctz64(uint64_t a)
{
#ifdef _MSC_VER
unsigned long idx;
_BitScanForward64(&idx, a);
return 63 ^ idx;
#else
return __builtin_ctzll(a);
#endif
}

#ifdef _MSC_VER
#pragma pack(push, 1)
struct packed_u64 {
uint64_t v;
};

struct packed_u32 {
uint32_t v;
};

struct packed_u16 {
uint16_t v;
};
#pragma pack(pop)
#else
struct __attribute__((packed)) packed_u64 {
uint64_t v;
};
Expand All @@ -146,6 +202,7 @@ struct __attribute__((packed)) packed_u32 {
struct __attribute__((packed)) packed_u16 {
uint16_t v;
};
#endif

static inline uint64_t get_u64(const uint8_t *tab)
{
Expand Down Expand Up @@ -220,13 +277,13 @@ static inline uint32_t bswap32(uint32_t v)

static inline uint64_t bswap64(uint64_t v)
{
return ((v & ((uint64_t)0xff << (7 * 8))) >> (7 * 8)) |
((v & ((uint64_t)0xff << (6 * 8))) >> (5 * 8)) |
((v & ((uint64_t)0xff << (5 * 8))) >> (3 * 8)) |
((v & ((uint64_t)0xff << (4 * 8))) >> (1 * 8)) |
((v & ((uint64_t)0xff << (3 * 8))) << (1 * 8)) |
((v & ((uint64_t)0xff << (2 * 8))) << (3 * 8)) |
((v & ((uint64_t)0xff << (1 * 8))) << (5 * 8)) |
return ((v & ((uint64_t)0xff << (7 * 8))) >> (7 * 8)) |
((v & ((uint64_t)0xff << (6 * 8))) >> (5 * 8)) |
((v & ((uint64_t)0xff << (5 * 8))) >> (3 * 8)) |
((v & ((uint64_t)0xff << (4 * 8))) >> (1 * 8)) |
((v & ((uint64_t)0xff << (3 * 8))) << (1 * 8)) |
((v & ((uint64_t)0xff << (2 * 8))) << (3 * 8)) |
((v & ((uint64_t)0xff << (1 * 8))) << (5 * 8)) |
((v & ((uint64_t)0xff << (0 * 8))) << (7 * 8));
}

Expand Down
20 changes: 10 additions & 10 deletions include/quickjs/libbf.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Tiny arbitrary precision floating point library
*
*
* Copyright (c) 2017-2021 Fabrice Bellard
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down Expand Up @@ -171,7 +171,7 @@ static inline bf_flags_t bf_set_exp_bits(int n)
#define BF_ST_UNDERFLOW (1 << 3)
#define BF_ST_INEXACT (1 << 4)
/* indicate that a memory allocation error occured. NaN is returned */
#define BF_ST_MEM_ERROR (1 << 5)
#define BF_ST_MEM_ERROR (1 << 5)

#define BF_RADIX_MAX 36 /* maximum radix for bf_atof() and bf_ftoa() */

Expand Down Expand Up @@ -284,7 +284,7 @@ int bf_sub(bf_t *r, const bf_t *a, const bf_t *b, limb_t prec, bf_flags_t flags)
int bf_add_si(bf_t *r, const bf_t *a, int64_t b1, limb_t prec, bf_flags_t flags);
int bf_mul(bf_t *r, const bf_t *a, const bf_t *b, limb_t prec, bf_flags_t flags);
int bf_mul_ui(bf_t *r, const bf_t *a, uint64_t b1, limb_t prec, bf_flags_t flags);
int bf_mul_si(bf_t *r, const bf_t *a, int64_t b1, limb_t prec,
int bf_mul_si(bf_t *r, const bf_t *a, int64_t b1, limb_t prec,
bf_flags_t flags);
int bf_mul_2exp(bf_t *r, slimb_t e, limb_t prec, bf_flags_t flags);
int bf_div(bf_t *r, const bf_t *a, const bf_t *b, limb_t prec, bf_flags_t flags);
Expand Down Expand Up @@ -341,12 +341,12 @@ int bf_mul_pow_radix(bf_t *r, const bf_t *T, limb_t radix,
/* fractional format: prec digits after the decimal point rounded with
(flags & BF_RND_MASK) */
#define BF_FTOA_FORMAT_FRAC (1 << 16)
/* free format:
/* free format:
For binary radices with bf_ftoa() and for bfdec_ftoa(): use the minimum
number of digits to represent 'a'. The precision and the rounding
mode are ignored.
For the non binary radices with bf_ftoa(): use as many digits as
necessary so that bf_atof() return the same number when using
precision 'prec', rounding to nearest and the subnormal
Expand All @@ -373,7 +373,7 @@ char *bf_ftoa(size_t *plen, const bf_t *a, int radix, limb_t prec,
bf_flags_t flags);

/* modulo 2^n instead of saturation. NaN and infinity return 0 */
#define BF_GET_INT_MOD (1 << 0)
#define BF_GET_INT_MOD (1 << 0)
int bf_get_int32(int *pres, const bf_t *a, int flags);
int bf_get_int64(int64_t *pres, const bf_t *a, int flags);
int bf_get_uint64(uint64_t *pres, const bf_t *a);
Expand All @@ -387,10 +387,10 @@ int bf_normalize_and_round(bf_t *r, limb_t prec1, bf_flags_t flags);
int bf_can_round(const bf_t *a, slimb_t prec, bf_rnd_t rnd_mode, slimb_t k);
slimb_t bf_mul_log2_radix(slimb_t a1, unsigned int radix, int is_inv,
int is_ceil1);
int mp_mul(bf_context_t *s, limb_t *result,
const limb_t *op1, limb_t op1_size,
int mp_mul(bf_context_t *s, limb_t *result,
const limb_t *op1, limb_t op1_size,
const limb_t *op2, limb_t op2_size);
limb_t mp_add(limb_t *res, const limb_t *op1, const limb_t *op2,
limb_t mp_add(limb_t *res, const limb_t *op1, const limb_t *op2,
limb_t n, limb_t carry);
limb_t mp_add_ui(limb_t *tab, limb_t b, size_t n);
int mp_sqrtrem(bf_context_t *s, limb_t *tabs, limb_t *taba, limb_t n);
Expand Down
1 change: 1 addition & 0 deletions include/quickjs/libunicode-table.h
Original file line number Diff line number Diff line change
Expand Up @@ -4447,3 +4447,4 @@ static const uint16_t unicode_prop_len_table[] = {
};

#endif /* CONFIG_ALL_UNICODE */

35 changes: 31 additions & 4 deletions include/quickjs/quickjs.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ extern "C" {
#else
#define js_likely(x) (x)
#define js_unlikely(x) (x)
#ifdef _MSC_VER
#define js_force_inline __forceinline
#else
#define js_force_inline inline
#endif
#define __js_printf_like(a, b)
#endif

Expand All @@ -70,6 +74,10 @@ typedef uint32_t JSAtom;
#define JS_NAN_BOXING
#endif

#ifdef _MSC_VER
typedef size_t ssize_t;
#endif

enum {
/* all tags with a reference count are negative */
JS_TAG_FIRST = -11, /* first negative tag */
Expand Down Expand Up @@ -215,8 +223,23 @@ typedef struct JSValue {
#define JS_VALUE_GET_FLOAT64(v) ((v).u.float64)
#define JS_VALUE_GET_PTR(v) ((v).u.ptr)

#ifdef _MSC_VER
static inline JSValue JS_MKVAL(int tag, int32_t val) {
JSValue v;
v.u.int32 = val;
v.tag = tag;
return v;
}
static inline JSValue JS_MKPTR(int tag, void *val) {
JSValue v;
v.u.ptr = val;
v.tag = tag;
return v;
}
#else
#define JS_MKVAL(tag, val) (JSValue){ (JSValueUnion){ .int32 = val }, tag }
#define JS_MKPTR(tag, p) (JSValue){ (JSValueUnion){ .ptr = p }, tag }
#endif

#define JS_TAG_IS_FLOAT64(tag) ((unsigned)(tag) == JS_TAG_FLOAT64)

Expand Down Expand Up @@ -331,6 +354,8 @@ JSRuntime *JS_NewRuntime(void);
void JS_SetRuntimeInfo(JSRuntime *rt, const char *info);
void JS_SetMemoryLimit(JSRuntime *rt, size_t limit);
void JS_SetGCThreshold(JSRuntime *rt, size_t gc_threshold);
void JS_TurnOffGC(JSRuntime *rt);
void JS_TurnOnGC(JSRuntime *rt);
/* use 0 to disable maximum stack size check */
void JS_SetMaxStackSize(JSRuntime *rt, size_t stack_size);
/* should be called when changing thread to update the stack top value
Expand Down Expand Up @@ -433,12 +458,13 @@ static const char js_atom_init[] =
#define DEF(name, str) str "\0"
#include "quickjs/quickjs-atom.h"
#undef DEF
;
;

JSAtom JS_NewAtomLen(JSContext *ctx, const char *str, size_t len);
JSAtom JS_NewAtom(JSContext *ctx, const char *str);
JSAtom JS_NewAtomUInt32(JSContext *ctx, uint32_t n);
JSAtom JS_DupAtom(JSContext *ctx, JSAtom v);
JSAtom JS_DupAtomRT(JSRuntime *runtime, JSAtom v);
void JS_FreeAtom(JSContext *ctx, JSAtom v);
void JS_FreeAtomRT(JSRuntime *rt, JSAtom v);
JSValue JS_AtomToValue(JSContext *ctx, JSAtom atom);
Expand Down Expand Up @@ -526,7 +552,7 @@ static js_force_inline JSValue JS_NewCatchOffset(JSContext *ctx, int32_t val)
static js_force_inline JSValue JS_NewInt64(JSContext *ctx, int64_t val) {
JSValue v;
if (val == (int32_t)val) {
v = JS_NewInt32(ctx, val);
v = JS_NewInt32(ctx, (int32_t) val);
} else {
v = __JS_NewFloat64(ctx, val);
}
Expand Down Expand Up @@ -662,15 +688,15 @@ static inline JSValue JS_DupValue(JSContext *ctx, JSValueConst v) {
JSRefCountHeader* p = (JSRefCountHeader*)JS_VALUE_GET_PTR(v);
p->ref_count++;
}
return (JSValue)v;
return v;
}

static inline JSValue JS_DupValueRT(JSRuntime *rt, JSValueConst v) {
if (JS_VALUE_HAS_REF_COUNT(v)) {
JSRefCountHeader* p = (JSRefCountHeader*)JS_VALUE_GET_PTR(v);
p->ref_count++;
}
return (JSValue)v;
return v;
}

int JS_ToBool(JSContext *ctx, JSValueConst val); /* return -1 for JS_EXCEPTION */
Expand Down Expand Up @@ -729,6 +755,7 @@ int JS_SetPropertyInternalWithIC(JSContext* ctx, JSValueConst this_obj, JSAtom p
static inline int JS_SetProperty(JSContext* ctx, JSValueConst this_obj, JSAtom prop, JSValue val) {
return JS_SetPropertyInternal(ctx, this_obj, prop, val, JS_PROP_THROW, NULL);
}

int JS_SetPropertyUint32(JSContext* ctx, JSValueConst this_obj, uint32_t idx, JSValue val);
int JS_SetPropertyInt64(JSContext* ctx, JSValueConst this_obj, int64_t idx, JSValue val);
int JS_SetPropertyStr(JSContext* ctx, JSValueConst this_obj, const char* prop, JSValue val);
Expand Down
Loading

0 comments on commit 848a1eb

Please sign in to comment.