thrown_cs = cs;
/* Return to run() method */
+#ifdef WIN32
+ longjmp(toplevel,1);
+#else
siglongjmp(toplevel,1);
+#endif
}
void early_error(CELL error)
/* raw pointer to callstack top */
CELL cs;
-#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <math.h>
#include <setjmp.h>
#include <signal.h>
-#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <sys/mman.h>
-#include <sys/param.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <sys/stat.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include <unistd.h>
-#include <sys/time.h>
-#include <netdb.h>
+
+#ifdef WIN32
+ #include <windows.h>
+#else
+ #include <dirent.h>
+ #include <sys/mman.h>
+ #include <sys/param.h>
+ #include <sys/types.h>
+ #include <sys/socket.h>
+ #include <sys/stat.h>
+ #include <netinet/in.h>
+ #include <arpa/inet.h>
+ #include <unistd.h>
+ #include <sys/time.h>
+ #include <netdb.h>
+#endif
+
+#include <time.h>
+
+#if defined(_MSC_VER)
+ #pragma warning(disable:4312)
+ #pragma warning(disable:4311)
+ typedef enum { false, true } _Bool;
+ typedef enum _Bool bool;
+ typedef unsigned char uint8_t;
+ typedef unsigned short uint16_t;
+ typedef unsigned int uint32_t;
+ typedef unsigned __int64 uint64_t;
+ typedef signed char int8_t;
+ typedef signed short int16_t;
+ typedef signed int int32_t;
+ typedef signed __int64 int64_t;
+ #define snprintf _snprintf
+#else
+ #include <stdbool.h>
+#endif
#ifdef FFI
#include <dlfcn.h>
#endif /* FFI */
-#define INLINE inline static
+#if defined(_MSC_VER)
+ #define INLINE static __inline
+#else
+ #define INLINE inline static
+#endif
#define FIXNUM_MAX (LONG_MAX >> TAG_BITS)
#define FIXNUM_MIN (LONG_MIN >> TAG_BITS)
#define HALF_WORD_MASK (((unsigned long)1<<HALF_WORD_SIZE)-1)
/* must always be 16 bits */
-typedef unsigned short CHAR;
-#define CHARS ((signed)sizeof(CHAR))
+#define CHARS ((signed)sizeof(uint16_t))
/* must always be 8 bits */
typedef unsigned char BYTE;
{
#ifdef FFI
CELL ptr = alien_pointer();
- box_signed_2(*(CHAR*)ptr);
+ box_signed_2(*(uint16_t*)ptr);
#else
general_error(ERROR_FFI_DISABLED,F);
#endif
#ifdef FFI
CELL ptr = alien_pointer();
CELL value = unbox_signed_2();
- *(CHAR*)ptr = value;
+ *(uint16_t*)ptr = value;
#else
general_error(ERROR_FFI_DISABLED,F);
#endif
void primitive_float_to_bits(void)
{
double f;
- long long f_raw;
+ int64_t f_raw;
maybe_garbage_collection();
f = untag_float(dpeek());
- f_raw = *(long long*)&f;
+ f_raw = *(int64_t*)&f;
drepl(tag_object(s48_long_long_to_bignum(f_raw)));
}
void primitive_gc(void)
{
- long long start = current_millis();
+ int64_t start = current_millis();
gc_in_progress = true;
CELL scan;
bool gc_in_progress;
-long long gc_time;
+int64_t gc_time;
/* Given a pointer to oldspace, copy it to newspace. */
INLINE void* copy_untagged_object(void* pointer, CELL size)
/* set up guard pages to check for under/overflow.
size must be a multiple of the page size */
+
+#ifdef WIN32
+void *alloc_guarded(CELL size)
+{
+ SYSTEM_INFO si;
+ char *mem;
+ DWORD ignore;
+
+ GetSystemInfo(&si);
+ mem = (char *)VirtualAlloc(NULL, si.dwPageSize*2 + size, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
+
+ if (!VirtualProtect(mem, si.dwPageSize, PAGE_NOACCESS, &ignore))
+ fatal_error("Cannot allocate low guard page", (CELL)mem);
+
+ if (!VirtualProtect(mem+size+si.dwPageSize, si.dwPageSize, PAGE_NOACCESS, &ignore))
+ fatal_error("Cannot allocate high guard page", (CELL)mem);
+
+ return mem + si.dwPageSize;
+}
+#else
void* alloc_guarded(CELL size)
{
int pagesize = getpagesize();
/* return bottom of actual array */
return array + pagesize;
}
+#endif
void init_zone(ZONE* z, CELL size)
{
*((CELL*)where) = what;
}
-INLINE CHAR cget(CELL where)
+INLINE uint16_t cget(CELL where)
{
- return *((CHAR*)where);
+ return *((uint16_t*)where);
}
-INLINE void cput(CELL where, CHAR what)
+INLINE void cput(CELL where, uint16_t what)
{
- *((CHAR*)where) = what;
+ *((uint16_t*)where) = what;
}
INLINE BYTE bget(CELL where)
box_boolean(dpop() == dpop());
}
-long long current_millis(void)
+#ifdef WIN32
+int64_t current_millis(void)
+{
+ FILETIME t;
+ GetSystemTimeAsFileTime(&t);
+ return ((int64_t)t.dwLowDateTime | (int64_t)t.dwHighDateTime<<32) / 100000
+ - 172456224000;
+}
+#else
+int64_t current_millis(void)
{
struct timeval t;
gettimeofday(&t,NULL);
- return (long long)t.tv_sec * 1000 + t.tv_usec/1000;
+ return (int64_t)t.tv_sec * 1000 + t.tv_usec/1000;
}
+#endif
void primitive_millis(void)
{
void primitive_init_random(void)
{
-#ifdef HAVE_SRANDOMDEV
- srandomdev();
-#else
- struct timeval t;
- gettimeofday(&t,NULL);
- srandom(t.tv_sec);
-#endif
+ srand((unsigned)time(NULL));
}
void primitive_random_int(void)
{
maybe_garbage_collection();
- dpush(tag_object(s48_long_to_bignum(random())));
+ dpush(tag_object(s48_long_to_bignum(rand())));
}
void primitive_exit(void);
void primitive_os_env(void);
void primitive_eq(void);
-long long current_millis(void);
+int64_t current_millis(void);
void primitive_millis(void);
void primitive_init_random(void);
void primitive_random_int(void);
CELL next;
/* Error handling. */
+#ifdef WIN32
+ setjmp(toplevel);
+ __try
+ {
+#else
sigsetjmp(toplevel, 1);
+#endif
+
if(thrown_error != F)
{
if(thrown_keep_stacks)
else
dpush(next);
}
+
+#ifdef WIN32
+ }
+ __except (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION ?
+ EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH)
+ {
+ signal_error(SIGSEGV);
+ }
+#endif
}
/* XT of deferred words */
}
bignum_type
-s48_long_long_to_bignum(long long n)
+s48_long_long_to_bignum(int64_t n)
{
int negative_p;
bignum_digit_type result_digits [BIGNUM_DIGITS_FOR_LONG_LONG];
if (n == 1) return (BIGNUM_ONE (0));
if (n == -1) return (BIGNUM_ONE (1));
{
- unsigned long long accumulator = ((negative_p = (n < 0)) ? (-n) : n);
+ uint64_t accumulator = ((negative_p = (n < 0)) ? (-n) : n);
do
{
(*end_digits++) = (accumulator & BIGNUM_DIGIT_MASK);
bignum_type s48_bignum_quotient(bignum_type, bignum_type);
bignum_type s48_bignum_remainder(bignum_type, bignum_type);
bignum_type s48_long_to_bignum(long);
-bignum_type s48_long_long_to_bignum(long long n);
+bignum_type s48_long_long_to_bignum(int64_t n);
bignum_type s48_ulong_to_bignum(unsigned long);
long s48_bignum_to_long(bignum_type);
unsigned long s48_bignum_to_ulong(bignum_type);
(BIGNUM_BITS_TO_DIGITS ((sizeof (long)) * CHAR_BIT))
#define BIGNUM_DIGITS_FOR_LONG_LONG \
- (BIGNUM_BITS_TO_DIGITS ((sizeof (long long)) * CHAR_BIT))
+ (BIGNUM_BITS_TO_DIGITS ((sizeof (int64_t)) * CHAR_BIT))
#ifndef BIGNUM_DISABLE_ASSERTION_CHECKS
sbuf->top = top;
}
-void set_sbuf_nth(SBUF* sbuf, CELL index, CHAR value)
+void set_sbuf_nth(SBUF* sbuf, CELL index, uint16_t value)
{
if(index < 0)
range_error(tag_object(sbuf),index,sbuf->top);
void primitive_set_sbuf_length(void);
void primitive_sbuf_nth(void);
void sbuf_ensure_capacity(SBUF* sbuf, FIXNUM top);
-void set_sbuf_nth(SBUF* sbuf, CELL index, CHAR value);
+void set_sbuf_nth(SBUF* sbuf, CELL index, uint16_t value);
void primitive_set_sbuf_nth(void);
void sbuf_append_string(SBUF* sbuf, STRING* string);
void primitive_sbuf_append(void);
return string;
}
-STRING* grow_string(STRING* string, FIXNUM capacity, CHAR fill)
+STRING* grow_string(STRING* string, FIXNUM capacity, uint16_t fill)
{
/* later on, do an optimization: if end of array is here, just grow */
CELL i;
for(i = 0; i < s->capacity; i++)
{
- CHAR ch = string_nth(s,i);
+ uint16_t ch = string_nth(s,i);
if(ch == '\0' || ch > 255)
general_error(ERROR_C_STRING,tag_object(s));
}
CELL i = 0;
while(i < len)
{
- CHAR c1 = string_nth(s1,i);
- CHAR c2 = string_nth(s2,i);
+ uint16_t c1 = string_nth(s1,i);
+ uint16_t c2 = string_nth(s2,i);
if(c1 != c2)
return c1 - c2;
i++;
void string_reverse(STRING* s, int len)
{
int i, j;
- CHAR ch1, ch2;
+ uint16_t ch1, ch2;
for(i = 0; i < len / 2; i++)
{
j = len - i - 1;
STRING* string(FIXNUM capacity, CELL fill);
FIXNUM hash_string(STRING* str, FIXNUM len);
void rehash_string(STRING* str);
-STRING* grow_string(STRING* string, FIXNUM capacity, CHAR fill);
+STRING* grow_string(STRING* string, FIXNUM capacity, uint16_t fill);
BYTE* to_c_string(STRING* s);
BYTE* to_c_string_unchecked(STRING* s);
void box_c_string(const BYTE* c_string);
}
/* untagged & unchecked */
-INLINE void set_string_nth(STRING* string, CELL index, CHAR value)
+INLINE void set_string_nth(STRING* string, CELL index, uint16_t value)
{
cput(SREF(string,index),value);
}