diff --git a/include/platform.h b/include/platform.h index 6c6170d6..4c060e96 100644 --- 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 */ diff --git a/src/asm/lexer.c b/src/asm/lexer.c index b8f88818..9b9cc1bd 100644 --- a/src/asm/lexer.c +++ b/src/asm/lexer.c @@ -22,7 +22,7 @@ #include #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 +# include +# include +# 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 +# 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 @@ -313,48 +354,6 @@ static void initState(struct LexerState *state) state->expansionOfs = 0; } -/* Neither MSVC nor MinGW provide `mmap` */ -#if defined(_MSC_VER) || defined(__MINGW32__) -# include -# include -# include -# 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 -# 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) { dbgPrint("Opening file \"%s\"\n", path);