shithub: rgbds

Download patch

ref: 8e7afb0ab38aa80464317774a5166e09bd5aa084
parent: 138523570e1b8a6a4e45f5c64a4335807c37c657
author: ISSOtm <[email protected]>
date: Mon Aug 31 12:29:51 EDT 2020

Move some MSVC-specific defines to `platform.h`

--- a/include/platform.h
+++ b/include/platform.h
@@ -32,4 +32,11 @@
 # define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
 #endif
 
+/* MSVC doesn't use POSIX types or defines for `read` */
+#ifdef _MSC_VER
+# define STDIN_FILENO 0
+# define ssize_t int
+# define SSIZE_MAX INT_MAX
+#endif
+
 #endif /* RGBDS_PLATFORM_H */
--- a/src/asm/lexer.c
+++ b/src/asm/lexer.c
@@ -22,7 +22,7 @@
 #include <unistd.h>
 
 #include "extern/utf8decoder.h"
-#include "platform.h" /* For `mmap` */
+#include "platform.h" /* For `ssize_t` */
 
 #include "asm/asm.h"
 #include "asm/lexer.h"
@@ -42,6 +42,47 @@
   #define dbgPrint(...)
 #endif
 
+/* Neither MSVC nor MinGW provide `mmap` */
+#if defined(_MSC_VER) || defined(__MINGW32__)
+# include <windows.h>
+# include <fileapi.h>
+# include <winbase.h>
+# define MAP_FAILED NULL
+# define mapFile(ptr, fd, path, size) do { \
+	(ptr) = MAP_FAILED; \
+	HANDLE file = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, \
+				  FILE_FLAG_POSIX_SEMANTICS | FILE_FLAG_RANDOM_ACCESS, NULL); \
+	HANDLE mappingObj; \
+	\
+	if (file == INVALID_HANDLE_VALUE) \
+		break; \
+	mappingObj  = CreateFileMappingA(file, NULL, PAGE_READONLY, 0, 0, NULL); \
+	if (mappingObj != INVALID_HANDLE_VALUE) \
+		(ptr) = MapViewOfFile(mappingObj, FILE_MAP_READ, 0, 0, 0); \
+	CloseHandle(mappingObj); \
+	CloseHandle(file); \
+} while (0)
+# define munmap(ptr, size)  UnmapViewOfFile((ptr))
+
+#else /* defined(_MSC_VER) || defined(__MINGW32__) */
+
+# include <sys/mman.h>
+# define mapFile(ptr, fd, path, size) do { \
+	(ptr) = mmap(NULL, (size), PROT_READ, MAP_PRIVATE, (fd), 0); \
+	\
+	if ((ptr) == MAP_FAILED && errno == ENOTSUP) { \
+		/*
+		 * The implementation may not support MAP_PRIVATE; try again with MAP_SHARED
+		 * instead, offering, I believe, weaker guarantees about external modifications to
+		 * the file while reading it. That's still better than not opening it at all, though
+		 */ \
+		if (verbose) \
+			printf("mmap(%s, MAP_PRIVATE) failed, retrying with MAP_SHARED\n", path); \
+		(ptr) = mmap(NULL, (size), PROT_READ, MAP_SHARED, (fd), 0); \
+	} \
+} while (0)
+#endif /* !( defined(_MSC_VER) || defined(__MINGW32__) ) */
+
 /*
  * Identifiers that are also keywords are listed here. This ONLY applies to ones
  * that would normally be matched as identifiers! Check out `yylex_NORMAL` to
@@ -312,48 +353,6 @@
 	state->expansions = NULL;
 	state->expansionOfs = 0;
 }
-
-/* Neither MSVC nor MinGW provide `mmap` */
-#if defined(_MSC_VER) || defined(__MINGW32__)
-# include <windows.h>
-# include <fileapi.h>
-# include <winbase.h>
-# define MAP_FAILED NULL
-# define mapFile(ptr, fd, path, size) do { \
-	(ptr) = MAP_FAILED; \
-	HANDLE file = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, \
-				  FILE_FLAG_POSIX_SEMANTICS | FILE_FLAG_RANDOM_ACCESS, NULL); \
-	HANDLE mappingObj; \
-	\
-	if (file == INVALID_HANDLE_VALUE) \
-		break; \
-	mappingObj  = CreateFileMappingA(file, NULL, PAGE_READONLY, 0, 0, NULL); \
-	if (mappingObj != INVALID_HANDLE_VALUE) \
-		(ptr) = MapViewOfFile(mappingObj, FILE_MAP_READ, 0, 0, 0); \
-	CloseHandle(mappingObj); \
-	CloseHandle(file); \
-} while (0)
-# define munmap(ptr, size)  UnmapViewOfFile((ptr))
-
-#else /* defined(_MSC_VER) || defined(__MINGW32__) */
-
-# include <sys/mman.h>
-# define mapFile(ptr, fd, path, size) do { \
-	(ptr) = mmap(NULL, (size), PROT_READ, MAP_PRIVATE, (fd), 0); \
-	\
-	if ((ptr) == MAP_FAILED && errno == ENOTSUP) { \
-		/*
-		 * The implementation may not support MAP_PRIVATE; try again with MAP_SHARED
-		 * instead, offering, I believe, weaker guarantees about external
-		 * modifications to the file while reading it. That's still better than not
-		 * opening it at all, though.
-		 */ \
-		if (verbose) \
-			printf("mmap(%s, MAP_PRIVATE) failed, retrying with MAP_SHARED\n", path); \
-		(ptr) = mmap(NULL, (size), PROT_READ, MAP_SHARED, (fd), 0); \
-	} \
-} while (0)
-#endif /* !( defined(_MSC_VER) || defined(__MINGW32__) ) */
 
 struct LexerState *lexer_OpenFile(char const *path)
 {