]> gitweb.factorcode.org Git - factor.git/commitdiff
started merging eiz's win32 port
authorSlava Pestov <slava@factorcode.org>
Sat, 11 Dec 2004 02:39:45 +0000 (02:39 +0000)
committerSlava Pestov <slava@factorcode.org>
Sat, 11 Dec 2004 02:39:45 +0000 (02:39 +0000)
18 files changed:
native/error.c
native/factor.h
native/ffi.c
native/float.c
native/gc.c
native/gc.h
native/memory.c
native/memory.h
native/misc.c
native/misc.h
native/run.c
native/s48_bignum.c
native/s48_bignum.h
native/s48_bignumint.h
native/sbuf.c
native/sbuf.h
native/string.c
native/string.h

index 0b8ddbe59bfc7282e2136e804dc20750a1a8bb06..b4d494b5d31067aecc582d99448c24142f979d0d 100644 (file)
@@ -26,7 +26,11 @@ void throw_error(CELL error, bool keep_stacks)
        thrown_cs = cs;
 
        /* Return to run() method */
+#ifdef WIN32
+       longjmp(toplevel,1);
+#else
        siglongjmp(toplevel,1);
+#endif
 }
 
 void early_error(CELL error)
index d01264d9ecfdfef5137a2c22103b980ed2bb5b1d..f0216e4ef402d2fcad48b590deb61de7391ba24d 100644 (file)
@@ -25,33 +25,61 @@ CELL cs_bot;
 /* 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)
@@ -63,8 +91,7 @@ CELL cs;
 #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;
index e7eb2ecbd5cb543da307d2f9b5b89119c7f18c28..dd2742d44ee37cef6b74a6daeb250f3d1352d004 100644 (file)
@@ -181,7 +181,7 @@ void primitive_alien_2(void)
 {
 #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
@@ -192,7 +192,7 @@ void primitive_set_alien_2(void)
 #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
index 929bededb24ff2ea374aa81dd27994b092e9cfcc..31ee45c0d551ad007729254d179fe7facb20ee61 100644 (file)
@@ -62,12 +62,12 @@ void primitive_float_to_str(void)
 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)));
 }
 
index 481c63bb8365318db5df7b301842f50c9034302e..a66b049e9275eaacd2daa918b18de25b604130db 100644 (file)
@@ -123,7 +123,7 @@ void collect_roots(void)
 
 void primitive_gc(void)
 {
-       long long start = current_millis();
+       int64_t start = current_millis();
 
        gc_in_progress = true;
 
index ef9dcec7cd61f97ead267605c5ee51991076d4d4..a52c9a327024337c1fa3974f6a00bbea6e4f3fcc 100644 (file)
@@ -1,6 +1,6 @@
 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)
index 5ac34efb1d42f440f51d389991e166c28c089c65..64e2d794f02cf712e0f7db60dacbc91bcb304084 100644 (file)
@@ -2,6 +2,26 @@
 
 /* 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();
@@ -19,6 +39,7 @@ void* alloc_guarded(CELL size)
        /* return bottom of actual array */
        return array + pagesize;
 }
+#endif
 
 void init_zone(ZONE* z, CELL size)
 {
index 2b0e89275ba28bb02c05a62507c7562bc7d67339..301bb10a0e21d9a04f9fc956da4f9b4073841edf 100644 (file)
@@ -41,14 +41,14 @@ INLINE void put(CELL where, CELL what)
        *((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)
index fa82d091a9c86fa1afa3066a458241209a659d9c..e3d9822965ee92606cf81e3b6b4331fd5fd8cfbb 100644 (file)
@@ -24,12 +24,22 @@ void primitive_eq(void)
        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)
 {
@@ -39,17 +49,11 @@ 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())));
 }
index edf46e179ada600a77c5c3e9eaf6544de4bfe8d1..a0035d013d44dd1d691fddd24936e52fdf4c68f7 100644 (file)
@@ -1,7 +1,7 @@
 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);
index c2d94cd70f38a985b63f5d5e0c02f72e36e38331..70f943f63d8c5beb837da87cf7a9117682c9af0d 100644 (file)
@@ -15,7 +15,14 @@ void run(void)
        CELL next;
 
        /* Error handling. */
+#ifdef WIN32
+       setjmp(toplevel);
+       __try
+       {
+#else
        sigsetjmp(toplevel, 1);
+#endif
+
        if(thrown_error != F)
        {
                if(thrown_keep_stacks)
@@ -53,6 +60,15 @@ void run(void)
                else
                        dpush(next);
        }
+
+#ifdef WIN32
+       }
+       __except (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION ?
+               EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH)
+       {
+               signal_error(SIGSEGV);
+       }
+#endif
 }
 
 /* XT of deferred words */
index ac978de42fc41c441b763cf6592cc02173acbe56..7cacd40828a4ad275dfa146ca3526c43fa86144d 100644 (file)
@@ -383,7 +383,7 @@ s48_long_to_bignum(long n)
 }
 
 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];
@@ -393,7 +393,7 @@ s48_long_long_to_bignum(long long n)
   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);
index a4c1c6ac196acec5ca14c8e2b76787f874d6dbc0..9126e94c4a927c451e6ac561e645ec533c9f655a 100644 (file)
@@ -66,7 +66,7 @@ s48_bignum_divide(bignum_type numerator, bignum_type denominator,
 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);
index 4b8a8bab0dadfeea8674abed064bb1ff8b9c5676..7b2dfb76e84974015060d09bf68cb283b4ea1391 100644 (file)
@@ -119,7 +119,7 @@ typedef long bignum_length_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
 
index 32586d413e55198ad8ea172793fb871380032b44..a0aaa926915fc0678f19453fe69677cdf9ab96bf 100644 (file)
@@ -56,7 +56,7 @@ void sbuf_ensure_capacity(SBUF* sbuf, FIXNUM top)
        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);
index cccaff033283fd030ec90a53537dca401e7524fb..173c85863abe2bb967edad9b3926a4d8783b77dc 100644 (file)
@@ -20,7 +20,7 @@ void primitive_sbuf_length(void);
 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);
index 70b8b082a52d05f288dd35581e9831015fc3f3c8..d2c30410b15c78dfc051f5eb6001dd58804f1a07 100644 (file)
@@ -44,7 +44,7 @@ STRING* string(FIXNUM capacity, CELL fill)
        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;
@@ -90,7 +90,7 @@ BYTE* to_c_string(STRING* s)
 
        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));
        }
@@ -140,8 +140,8 @@ FIXNUM string_compare_head(STRING* s1, STRING* s2, CELL len)
        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++;
@@ -299,7 +299,7 @@ void primitive_substring(void)
 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;
index 86aca38d8e19dde347d4085d04e076a4b7abf58d..eaa4331c80caebd83e6cc5d7dc93698675cdd2a5 100644 (file)
@@ -16,7 +16,7 @@ STRING* allot_string(FIXNUM capacity);
 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);
@@ -35,7 +35,7 @@ INLINE CELL string_nth(STRING* string, CELL index)
 }
 
 /* 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);
 }