Skip to content

Commit

Permalink
Merge pull request #101 from SergioFLS/msvc40
Browse files Browse the repository at this point in the history
Make WASI and w2c2_base.h compile on MSVC 4.0
  • Loading branch information
turbolent authored Jul 31, 2024
2 parents 22a9ade + d21b775 commit f0f16b3
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 23 deletions.
3 changes: 2 additions & 1 deletion w2c2/c.c
Original file line number Diff line number Diff line change
Expand Up @@ -1045,8 +1045,9 @@ wasmCWriteLiteral(
break;
}
case wasmValueTypeI64: {
MUST (stringBuilderAppend(builder, "W2C2_LL("))
MUST (stringBuilderAppendI64(builder, value.i64))
MUST (stringBuilderAppend(builder, "ULL"))
MUST (stringBuilderAppend(builder, "U)"))
break;
}
case wasmValueTypeF32: {
Expand Down
52 changes: 34 additions & 18 deletions w2c2/w2c2_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <math.h>
#include <string.h>
#include <stdlib.h>
#ifndef __NeXT__
#if !(defined(__NeXT__) || (defined(_MSC_VER) && _MSC_VER <= 1000))
#include <stdint.h>
#endif

Expand Down Expand Up @@ -35,12 +35,23 @@ typedef signed short I16;
typedef unsigned int U32;
typedef signed int I32;

#if defined(_MSC_VER) && _MSC_VER <= 1000
typedef unsigned __int64 U64;
typedef signed __int64 I64;
#else
typedef unsigned long long int U64;
typedef signed long long int I64;
#endif

typedef float F32;
typedef double F64;

#if defined(_MSC_VER) && _MSC_VER <= 1000
#define W2C2_LL(x) x ## i64
#else
#define W2C2_LL(x) x ## ll
#endif

#define MUST(_) { if (!(_)) { return false; }; }

#define WASM_LITTLE_ENDIAN 0
Expand Down Expand Up @@ -144,14 +155,14 @@ typedef double F64;
| (((x) & 0x00FF0000) >> 8 ) \
| (((x) & 0x0000FF00) << 8 ) \
| (((x) & 0x000000FF) << 24))
#define swapU64(x) ((((x) & 0xff00000000000000ull) >> 56) \
| (((x) & 0x00ff000000000000ull) >> 40) \
| (((x) & 0x0000ff0000000000ull) >> 24) \
| (((x) & 0x000000ff00000000ull) >> 8 ) \
| (((x) & 0x00000000ff000000ull) << 8 ) \
| (((x) & 0x0000000000ff0000ull) << 24) \
| (((x) & 0x000000000000ff00ull) << 40) \
| (((x) & 0x00000000000000ffull) << 56))
#define swapU64(x) ((((x) & W2C2_LL(0xff00000000000000u)) >> 56) \
| (((x) & W2C2_LL(0x00ff000000000000u)) >> 40) \
| (((x) & W2C2_LL(0x0000ff0000000000u)) >> 24) \
| (((x) & W2C2_LL(0x000000ff00000000u)) >> 8 ) \
| (((x) & W2C2_LL(0x00000000ff000000u)) << 8 ) \
| (((x) & W2C2_LL(0x0000000000ff0000u)) << 24) \
| (((x) & W2C2_LL(0x000000000000ff00u)) << 40) \
| (((x) & W2C2_LL(0x00000000000000ffu)) << 56))

#endif

Expand Down Expand Up @@ -186,11 +197,11 @@ typedef double F64;
#endif

#ifndef LLONG_MIN
#define LLONG_MIN (-0x7fffffffffffffffLL-1)
#define LLONG_MIN (W2C2_LL(-0x7fffffffffffffff)-1)
#endif

#ifndef LLONG_MAX
#define LLONG_MAX 0x7fffffffffffffffLL
#define LLONG_MAX W2C2_LL(0x7fffffffffffffff)
#endif

#ifndef INT32_MAX
Expand All @@ -202,7 +213,7 @@ typedef double F64;
#endif

#ifndef INT64_MAX
#define INT64_MAX 9223372036854775807LL
#define INT64_MAX W2C2_LL(9223372036854775807)
#endif

#ifndef INT64_MIN
Expand All @@ -214,7 +225,7 @@ typedef double F64;
#endif

#ifndef UINT64_MAX
#define UINT64_MAX 18446744073709551615ULL
#define UINT64_MAX W2C2_LL(18446744073709551615U)
#endif

#if defined(_MSC_VER) && _MSC_VER <= 1500
Expand Down Expand Up @@ -341,10 +352,10 @@ I32_POPCNT(
static
W2C2_INLINE
U64 I64_POPCNT(U64 x) {
x -= ((x >> 1) & 0x5555555555555555ull);
x = ((x >> 2) & 0x3333333333333333ull) + (x & 0x3333333333333333ull);
x = ((x >> 4) + x) & 0xf0f0f0f0f0f0f0full;
x *= 0x101010101010101ull;
x -= ((x >> 1) & W2C2_LL(0x5555555555555555u));
x = ((x >> 2) & W2C2_LL(0x3333333333333333u)) + (x & W2C2_LL(0x3333333333333333u));
x = ((x >> 4) + x) & W2C2_LL(0xf0f0f0f0f0f0f0fu);
x *= W2C2_LL(0x101010101010101u);
return (x >> 56);
}
#endif
Expand Down Expand Up @@ -1049,8 +1060,13 @@ DEFINE_SWAP(32, i, int)
DEFINE_SWAP(32, I, unsigned int)
DEFINE_SWAP(32, l, long)
DEFINE_SWAP(32, L, unsigned long)
#if defined(_MSC_VER) && _MSC_VER <= 1000
DEFINE_SWAP(64, q, signed __int64)
DEFINE_SWAP(64, Q, unsigned __int64)
#else
DEFINE_SWAP(64, q, long long)
DEFINE_SWAP(64, Q, unsigned long long)
#endif
DEFINE_SWAP(32, f, float)
DEFINE_SWAP(64, d, double)

Expand Down Expand Up @@ -1110,7 +1126,7 @@ typedef struct wasmModuleInstance {
#define __has_extension __has_feature
#endif

#ifdef _MSC_VER
#if defined(_MSC_VER) && _MSC_VER >= 1500
#define WASM_ATOMICS_MSVC
#elif defined(__GNUC__) && (GCC_VERSION >= 40700 || __has_extension(c_atomic))
#define WASM_ATOMICS_GCC
Expand Down
12 changes: 8 additions & 4 deletions wasi/wasi.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@
#endif /* HAS_UNISTD */

#ifdef _MSC_VER
#if _MSC_VER <= 1000
typedef signed int ssize_t; /* assuming target machine is ILP32 ! */
#else
#include <BaseTsd.h>
typedef SSIZE_T ssize_t;
#endif
#define INT64_C(val) val##i64
#endif /* _MSC_VER */

Expand Down Expand Up @@ -1662,10 +1666,10 @@ WASI_IMPORT(U32, fd_close, (
})

#ifndef NSEC_PER_SEC
#define NSEC_PER_SEC 1000000000LL
#define NSEC_PER_SEC W2C2_LL(1000000000)
#endif
#ifndef NSEC_PER_USEC
#define NSEC_PER_USEC 1000LL
#define NSEC_PER_USEC W2C2_LL(1000)
#endif

static
Expand Down Expand Up @@ -3901,7 +3905,7 @@ wasiRandomGet(

bufferStart = memory->data + bufferPointer;

#ifdef _WIN32
#if defined(_WIN32) && !(defined(_MSC_VER) && _MSC_VER <= 1000)
#include <wincrypt.h>
{
HCRYPTPROV provider;
Expand Down Expand Up @@ -3970,7 +3974,7 @@ wasiRandomGet(
return WASI_ERRNO_SUCCESS;
}
}
#if defined(__MWERKS__) && defined(macintosh)
#if (defined(__MWERKS__) && defined(macintosh)) || (defined(_MSC_VER) && _MSC_VER <= 1000)
/* Fall back to rand */
{
U32 i = 0;
Expand Down
21 changes: 21 additions & 0 deletions wasi/win32.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,27 @@
extern "C" {
#endif

/* defines for MSVC 4.0 */
#ifndef ERROR_DELETE_PENDING
#define ERROR_DELETE_PENDING 303
#endif

#ifndef ERROR_CANT_RESOLVE_FILENAME
#define ERROR_CANT_RESOLVE_FILENAME 1921
#endif

#ifndef INVALID_FILE_ATTRIBUTES
#define INVALID_FILE_ATTRIBUTES (~0u)
#endif

#ifndef FILE_ATTRIBUTE_REPARSE_POINT
#define FILE_ATTRIBUTE_REPARSE_POINT 0x00000400
#endif

#ifndef IO_REPARSE_TAG_MOUNT_POINT
#define IO_REPARSE_TAG_MOUNT_POINT 0xA0000003l
#endif

struct dirent {
long d_ino;
unsigned short d_reclen;
Expand Down

0 comments on commit f0f16b3

Please sign in to comment.