]> gitweb.factorcode.org Git - factor.git/blobdiff - vm/os-windows.hpp
webapps.wiki: adding search bar
[factor.git] / vm / os-windows.hpp
index b12ebd0610372807975afe2f4cca6fe18cc42e3a..a21a67361b692a3f7ec9c9e10490cc230303912b 100644 (file)
@@ -1,14 +1,36 @@
 #include <ctype.h>
 
 #ifndef wcslen
-  /* for cygwin */
-  #include <wchar.h>
+// for cygwin
+#include <wchar.h>
 #endif
 
-namespace factor
-{
+#if _WIN32_WINNT != 0x0600
+#undef _WIN32_WINNT
+#define _WIN32_WINNT 0x0501  // For AddVectoredExceptionHandler, WinXP support
+//#define _WIN32_WINNT 0x0600  // For CancelSynchronousIo
+#endif
+
+#ifndef UNICODE
+#define UNICODE
+#endif
+
+#include <windows.h>
+#include <shellapi.h>
+
+#ifdef _MSC_VER
+#undef min
+#undef max
+#endif
+
+// Difference between Jan 1 00:00:00 1601 and Jan 1 00:00:00 1970
+#define EPOCH_OFFSET 0x019db1ded53e8000LL
+
+namespace factor {
 
 typedef wchar_t vm_char;
+typedef char symbol_char;
+typedef HANDLE THREADHANDLE;
 
 #define STRING_LITERAL(string) L##string
 
@@ -18,34 +40,88 @@ typedef wchar_t vm_char;
 #define STRCMP wcscmp
 #define STRNCMP wcsncmp
 #define STRDUP _wcsdup
-#define MIN(a,b) ((a)>(b)?(b):(a))
-#define FTELL ftello64
-#define FSEEK fseeko64
 
-#ifdef WIN64
-       #define CELL_FORMAT "%Iu"
-       #define CELL_HEX_FORMAT "%Ix"
-       #define CELL_HEX_PAD_FORMAT "%016Ix"
-       #define FIXNUM_FORMAT "%Id"
+#ifdef _MSC_VER
+#define FTELL ftell
+#define FSEEK fseek
 #else
-       #define CELL_FORMAT "%lu"
-       #define CELL_HEX_FORMAT "%lx"
-       #define CELL_HEX_PAD_FORMAT "%08lx"
-       #define FIXNUM_FORMAT "%ld"
+#define FTELL ftello64
+#define FSEEK fseeko64
 #endif
 
-#define OPEN_READ(path) _wfopen(path,L"rb")
-#define OPEN_WRITE(path) _wfopen(path,L"wb")
+#define FACTOR_OS_STRING "windows"
 
-#define print_native_string(string) wprintf(L"%s",string)
+// SSE traps raise these exception codes, which are defined in internal NT
+// headers
+// but not winbase.h
+#ifndef STATUS_FLOAT_MULTIPLE_FAULTS
+#define STATUS_FLOAT_MULTIPLE_FAULTS 0xC00002B4
+#endif
 
-/* Difference between Jan 1 00:00:00 1601 and Jan 1 00:00:00 1970 */
-#define EPOCH_OFFSET 0x019db1ded53e8000LL
+#ifndef STATUS_FLOAT_MULTIPLE_TRAPS
+#define STATUS_FLOAT_MULTIPLE_TRAPS 0xC00002B5
+#endif
 
-inline static void init_signals() {}
-inline static void early_init() {}
+#define OPEN_READ(path) _wfopen((path), L"rb")
+#define OPEN_WRITE(path) _wfopen((path), L"wb")
+#define THREADSAFE_STRERROR(errnum, buf, buflen) strerror_s(buf, buflen, errnum)
 
-s64 current_micros();
+inline static void early_init() {}
+uint64_t nano_count();
+void sleep_nanos(uint64_t nsec);
 long getpagesize();
+VM_C_API LONG exception_handler(PEXCEPTION_RECORD e, void* frame, PCONTEXT c,
+                                void* dispatch);
+THREADHANDLE start_thread(void* (*start_routine)(void*), void* args);
+
+inline static THREADHANDLE thread_id() {
+  DWORD id = GetCurrentThreadId();
+  HANDLE threadHandle = OpenThread(
+      THREAD_GET_CONTEXT | THREAD_SET_CONTEXT | THREAD_SUSPEND_RESUME, FALSE,
+      id);
+  FACTOR_ASSERT(threadHandle != NULL);
+  return threadHandle;
+}
+
+inline static void breakpoint() { DebugBreak(); }
+
+#define CODE_TO_FUNCTION_POINTER(code) (void)0
+#define CODE_TO_FUNCTION_POINTER_CALLBACK(vm, code) (void)0
+#define FUNCTION_CODE_POINTER(ptr) ptr
+#define FUNCTION_TOC_POINTER(ptr) ptr
+
+extern HANDLE boot_thread;
+
+inline static std::string to_utf8(const wchar_t* buffer, int len) {
+  int nChars = ::WideCharToMultiByte(
+    CP_UTF8,
+    0,
+    buffer,
+    len,
+    NULL,
+    0,
+    NULL,
+    NULL);
+  if (nChars == 0) return "";
+
+  std::string newbuffer;
+  newbuffer.resize(nChars) ;
+  ::WideCharToMultiByte(
+    CP_UTF8,
+    0,
+    buffer,
+    len,
+    const_cast<char*>(newbuffer.c_str()),
+    nChars,
+    NULL,
+    NULL);
+  return newbuffer;
+}
+
+inline static std::string to_utf8(const std::wstring& str) {
+  return to_utf8(str.c_str(), (int)str.size());
+}
+
+#define AS_UTF8(ptr) to_utf8(ptr)
 
 }