Skip to content

Commit

Permalink
Split quickjs-port.h out
Browse files Browse the repository at this point in the history
Move all platform related functions into quickjs-port.h and quickjs-port.c
Add full msvc support.

BOOL conflict with Windows.h, do not define it in header file, define it in c file directly

Now run-test262 pass all tests match unix platform after add atomic/thread support on win32 by using quickjs-port.h
win/stdatomic.h comes from https://github.com/cdschreiber/c11
win/dirent.h comes from https://github.com/tronkko/dirent
win/getopt.h comes from mingw

Now `make test2-update` passed all tests under msys2/mingw

Signed-off-by: Yonggang Luo <[email protected]>
  • Loading branch information
lygstate committed Feb 16, 2021
1 parent c3181c4 commit f72176c
Show file tree
Hide file tree
Showing 18 changed files with 4,328 additions and 324 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
ifeq ($(shell uname -s),Darwin)
CONFIG_DARWIN=y
endif
ifeq ($(OS),Windows_NT)
CONFIG_WIN32=y
endif
# Windows cross compilation from Linux
#CONFIG_WIN32=y
# use link time optimization (smaller and faster executables but slower build)
Expand Down Expand Up @@ -166,7 +169,7 @@ endif

all: $(OBJDIR) $(OBJDIR)/quickjs.check.o $(OBJDIR)/qjs.check.o $(PROGS)

QJS_LIB_OBJS=$(OBJDIR)/quickjs.o $(OBJDIR)/libregexp.o $(OBJDIR)/libunicode.o $(OBJDIR)/cutils.o $(OBJDIR)/quickjs-libc.o
QJS_LIB_OBJS=$(OBJDIR)/quickjs.o $(OBJDIR)/libregexp.o $(OBJDIR)/libunicode.o $(OBJDIR)/cutils.o $(OBJDIR)/quickjs-libc.o $(OBJDIR)/quickjs-port.o

QJS_OBJS=$(OBJDIR)/qjs.o $(OBJDIR)/repl.o $(QJS_LIB_OBJS)
ifdef CONFIG_BIGNUM
Expand Down
6 changes: 3 additions & 3 deletions cutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <assert.h>

#include "cutils.h"

Expand Down Expand Up @@ -89,15 +90,14 @@ static void *dbuf_default_realloc(void *opaque, void *ptr, size_t size)
void dbuf_init2(DynBuf *s, void *opaque, DynBufReallocFunc *realloc_func)
{
memset(s, 0, sizeof(*s));
if (!realloc_func)
realloc_func = dbuf_default_realloc;
assert(realloc_func != NULL);
s->opaque = opaque;
s->realloc_func = realloc_func;
}

void dbuf_init(DynBuf *s)
{
dbuf_init2(s, NULL, NULL);
dbuf_init2(s, NULL, dbuf_default_realloc);
}

/* return < 0 if error */
Expand Down
89 changes: 84 additions & 5 deletions cutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,29 @@
#include <stdlib.h>
#include <inttypes.h>

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

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

#if defined(_MSC_VER)
#define likely(x) (x)
#define unlikely(x) (x)
#define force_inline __forceinline
#define no_inline __declspec(noinline)
#define __maybe_unused
#define __attribute__(x)
#define __attribute(x)
typedef intptr_t ssize_t;
#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 All @@ -49,7 +64,6 @@
#define countof(x) (sizeof(x) / sizeof((x)[0]))
#endif

typedef int BOOL;

#ifndef FALSE
enum {
Expand Down Expand Up @@ -114,27 +128,91 @@ 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)
{
return __builtin_clzll(a);
#ifdef _MSC_VER
unsigned long where;
// BitScanReverse scans from MSB to LSB for first set bit.
// Returns 0 if no set bit is found.
#if INTPTR_MAX >= INT64_MAX // 64-bit
if (_BitScanReverse64(&where, a))
return (int)(63 - where);
#else
// Scan the high 32 bits.
if (_BitScanReverse(&where, (uint32_t)(a >> 32)))
return (int)(63 - (where + 32)); // Create a bit offset from the MSB.
// Scan the low 32 bits.
if (_BitScanReverse(&where, (uint32_t)(a)))
return (int)(63 - where);
#endif
return 64; // Undefined Behavior.
#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 idx;
#else
return __builtin_ctz(a);
#endif
}

/* WARNING: undefined if a = 0 */
static inline int ctz64(uint64_t a)
{
return __builtin_ctzll(a);
#ifdef _MSC_VER
unsigned long where;
// Search from LSB to MSB for first set bit.
// Returns zero if no set bit is found.
#if INTPTR_MAX >= INT64_MAX // 64-bit
if (_BitScanForward64(&where, a))
return (int)(where);
#else
// Win32 doesn't have _BitScanForward64 so emulate it with two 32 bit calls.
// Scan the Low Word.
if (_BitScanForward(&where, (uint32_t)(a)))
return (int)(where);
// Scan the High Word.
if (_BitScanForward(&where, (uint32_t)(a >> 32)))
return (int)(where + 32); // Create a bit offset from the LSB.
#endif
return 64;
#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 +224,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 @@ -237,7 +316,7 @@ typedef struct DynBuf {
uint8_t *buf;
size_t size;
size_t allocated_size;
BOOL error; /* true if a memory allocation error occurred */
int error; /* true if a memory allocation error occurred */
DynBufReallocFunc *realloc_func;
void *opaque; /* for realloc_func */
} DynBuf;
Expand Down Expand Up @@ -265,7 +344,7 @@ static inline int dbuf_put_u64(DynBuf *s, uint64_t val)
int __attribute__((format(printf, 2, 3))) dbuf_printf(DynBuf *s,
const char *fmt, ...);
void dbuf_free(DynBuf *s);
static inline BOOL dbuf_error(DynBuf *s) {
static inline int dbuf_error(DynBuf *s) {
return s->error;
}
static inline void dbuf_set_error(DynBuf *s)
Expand Down
2 changes: 2 additions & 0 deletions libbf.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
#include "cutils.h"
#include "libbf.h"

#define BOOL int

/* enable it to check the multiplication result */
//#define USE_MUL_CHECK
/* enable it to use FFT/NTT multiplication */
Expand Down
2 changes: 2 additions & 0 deletions libregexp.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#include "cutils.h"
#include "libregexp.h"

#define BOOL int

/*
TODO:
Expand Down
2 changes: 2 additions & 0 deletions libunicode.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#include "libunicode.h"
#include "libunicode-table.h"

#define BOOL int

enum {
RUN_TYPE_U,
RUN_TYPE_L,
Expand Down
Loading

0 comments on commit f72176c

Please sign in to comment.