Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: changes necessary to compile to Windows-native programs #240

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions ccomp/runtime/pats_ccomp_typedefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
** the terms of the GNU GENERAL PUBLIC LICENSE (GPL) as published by the
** Free Software Foundation; either version 3, or (at your option) any
** later version.
**
**
** ATS is distributed in the hope that it will be useful, but WITHOUT ANY
** WARRANTY; without even the implied warranty of MERCHANTABILITY or
** FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
** for more details.
**
**
** You should have received a copy of the GNU General Public License
** along with ATS; see the file COPYING. If not, please write to the
** Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
Expand Down Expand Up @@ -56,8 +56,15 @@ typedef void atsvoid_t0ype ;
typedef int atstype_int ;
typedef unsigned int atstype_uint ;

#if _WIN32
// in Windows, sizeof(long) = sizeof(int),
// but we need more space than 32-bit
typedef long long int atstype_lint ;
typedef unsigned long long int atstype_ulint ;
#else
typedef long int atstype_lint ;
typedef unsigned long int atstype_ulint ;
#endif

typedef long long int atstype_llint ;
typedef unsigned long long int atstype_ullint ;
Expand Down
10 changes: 10 additions & 0 deletions doc/EXAMPLE/INTRO/areverse.dats
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,23 @@ _(*RG*) = "{$MYTESTING}/DATS/randgen.dats"
//
#include <time.h>
//
#if _WIN32
// NOTE:
// 1. replace srand48(seed) by srand(seed)
// 2. replace drand48() by (double(rand()) / RAND_MAX)
#else
extern void srand48 (long int) ; // in [stdlib.h]
extern double drand48 (/*void*/) ; // in [stdlib.h]
#endif
//
atsvoid_t0ype
srand48_with_time ()
{
#if _WIN32
srand(time(0)) ; return ;
#else
srand48(time(0)) ; return ;
#endif
}
%}
extern
Expand Down
65 changes: 65 additions & 0 deletions doc/EXAMPLE/MISC/foreach_getline.dats
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,71 @@ absvtype line = ptr

(* ****** ****** *)

%{

#if _WIN32


#include <errno.h> /* EINVAL */

/* The original code is public domain -- Will Hartung 4/9/09 */
/* Modifications, public domain as well, by Antti Haapala, 11/10/17 */
/* source: https://stackoverflow.com/questions/735126/are-there-alternate-implementations-of-gnu-getline-interface */

ssize_t getline(char **lineptr, size_t *n, FILE *stream) {
size_t pos;
int c;

if (lineptr == NULL || stream == NULL || n == NULL) {
errno = EINVAL;
return -1;
}

c = fgetc(stream);
if (c == EOF) {
return -1;
}

if (*lineptr == NULL) {
*lineptr = malloc(128);
if (*lineptr == NULL) {
return -1;
}
*n = 128;
}

pos = 0;
while(c != EOF) {
if (pos + 1 >= *n) {
size_t new_size = *n + (*n >> 2);
if (new_size < 128) {
new_size = 128;
}
char *new_ptr = realloc(*lineptr, new_size);
if (new_ptr == NULL) {
return -1;
}
*n = new_size;
*lineptr = new_ptr;
}

((unsigned char *)(*lineptr))[pos ++] = c;
if (c == '\n') {
break;
}
c = fgetc(stream);
}

(*lineptr)[pos] = '\0';
return pos;
}

#endif

%}

(* ****** ****** *)

extern
fun{}
line_nil(): line
Expand Down
19 changes: 19 additions & 0 deletions doc/EXAMPLE/MISC/wclines.dats
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,28 @@ staload "libats/libc/SATS/stdio.sats"
(* ****** ****** *)

%{^
#if _WIN32

static
void
*rawmemchr(const void *s, int c) {
if (s == NULL)
return NULL ;
char* p = (char*)s;
while (1) {
int c1 = *p;
if (c1 == c)
return (void*)p ;
p++;
}
return NULL; /* deadcode! */
}

#else
extern
void
*rawmemchr(const void *s, int c);
#endif
#define atslib_rawmemchr rawmemchr
%}
extern
Expand Down
5 changes: 5 additions & 0 deletions libats/DATS/linmap_skiplist.dats
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,12 @@ stadef mytkind = $extkind"atslib_linmap_skiplist"
//
// HX: it is in stdlib.h
//
#if _WIN32
extern void srand (unsigned int);
#define srand48 srand
#else
extern void srand48 (long int);
#endif
//
%}
typedef time_t = $extype"time_t"
Expand Down
8 changes: 8 additions & 0 deletions libats/libc/CATS/arpa/inet.cats
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@

/* ****** ****** */
//
#if _WIN32
// FIXME: error?
#else
#include <arpa/inet.h>
#include <netinet/in.h>
#endif
//
/* ****** ****** */

Expand All @@ -62,6 +66,9 @@
//
/* ****** ****** */

#if _WIN32
// FIXME: error?
#else
extern
int
inet_aton
Expand All @@ -84,6 +91,7 @@ atslib_libats_libc_inet_aton
return (rtn ? atsbool_true : atsbool_false) ;
//
} // end of [atslib_libats_libc_inet_aton]
#endif

/* ****** ****** */

Expand Down
4 changes: 4 additions & 0 deletions libats/libc/CATS/dirent.cats
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@

/* ****** ****** */

#if _WIN32
#include "mingw/dirent.h"
#else
#include <sys/types.h>
#include <dirent.h> // HX: after sys/types
#endif

/* ****** ****** */

Expand Down
4 changes: 4 additions & 0 deletions libats/libc/CATS/fcntl.cats
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,14 @@ atslib_libats_libc_fcntlflags_lor(x1, x2) ((x1)|(x2))

/* ****** ****** */

#if _WIN32
// FIXME: error out?
#else
#define \
atslib_libats_libc_fcntl_getfl(fd) fcntl(fd, F_GETFL)
#define \
atslib_libats_libc_fcntl_setfl(fd, flags) fcntl(fd, F_SETFL, flags)
#endif

/* ****** ****** */

Expand Down
4 changes: 4 additions & 0 deletions libats/libc/CATS/fnmatch.cats
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@

/* ****** ****** */

#if _WIN32
#include "mingw/fnmatch.h"
#else
#include <fnmatch.h>
#endif

/* ****** ****** */

Expand Down
33 changes: 33 additions & 0 deletions libats/libc/CATS/malloc.cats
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,49 @@

/* ****** ****** */

#if _WIN32
#include <stdio.h>
#else
#include <malloc.h>
#endif

/* ****** ****** */

#if _WIN32

static
int atslib_libats_libc_mallopt(int param, int value) {
return 0; // error
}
static
int atslib_libats_libc_malloc_trim(size_t pad) {
return 0; // unable to release any memory back to system
}
static
size_t atslib_libats_libc_malloc_usable_size(void *ptr) {
return 0; // no usable bytes in the pointer that we know of...
}
static
void atslib_libats_libc_malloc_stats() {
fprintf(stderr, "malloc_stats() is not implemented");
}
static
void *atslib_libats_libc_malloc_get_state() {
return NULL; // error: unable to allocate memory for storing state
}
static
int atslib_libats_libc_malloc_set_state(void *state) {
return -1; // error: unable to restore state
}

#else
#define atslib_libats_libc_mallopt mallopt
#define atslib_libats_libc_malloc_trim malloc_trim
#define atslib_libats_libc_malloc_usable_size malloc_usable_size
#define atslib_libats_libc_malloc_stats malloc_stats
#define atslib_libats_libc_malloc_get_state malloc_get_state
#define atslib_libats_libc_malloc_set_state malloc_set_state
#endif

/* ****** ****** */

Expand Down
16 changes: 16 additions & 0 deletions libats/libc/CATS/netinet/in.cats
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,32 @@
#define ATSLIB_LIBATS_LIBC_NETINET_IN_CATS

/* ****** ****** */
#if _WIN32
// FIXME: error?
#else
//
#include <arpa/inet.h>
#include <netinet/in.h>
//
#endif
/* ****** ****** */
//
#if _WIN32
// FIXME: error?
#else
typedef
struct in_addr
in_addr_struct;
typedef
struct in6_addr
in6_addr_struct;
#endif
//
/* ****** ****** */
//
#if _WIN32
// FIXME: error?
#else
typedef
struct sockaddr_in
sockaddr_in_struct ;
Expand All @@ -67,6 +78,7 @@ atslib_libats_libc_socklen_in \
#define \
atslib_libats_libc_socklen_in6 \
(sizeof(sockaddr_in6_struct))
#endif
//
/* ****** ****** */

Expand All @@ -77,6 +89,9 @@ atslib_libats_libc_in_port_nbo_uint(nport) htons(nport)

/* ****** ****** */

#if _WIN32
// FIXME: error?
#else
ATSinline()
in_addr_t
atslib_libats_libc_in_addr_hbo2nbo
Expand All @@ -85,6 +100,7 @@ atslib_libats_libc_in_addr_hbo2nbo
return htonl(addr_hbo) ;
}
/* end of [atslib_libats_libc_in_addr_hbo2nbo] */
#endif

/* ****** ****** */

Expand Down
14 changes: 14 additions & 0 deletions libats/libc/CATS/stdlib.cats
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,29 @@ strtod((char*)(nptr), (char**)(endptr))

/* ****** ****** */

#if _WIN32
#define atslib_libats_libc_random rand
#define atslib_libats_libc_srandom srand
#else
#define atslib_libats_libc_random random
#define atslib_libats_libc_srandom srandom
#endif

/* ****** ****** */
//
#define atslib_libats_libc_seed48 seed48
#if _WIN32
#define atslib_libats_libc_srand48 srand
#else
#define atslib_libats_libc_srand48 srand48
#endif

//
#if _WIN32
#define atslib_libats_libc_drand48() ((double)(rand()) / RAND_MAX)
#else
#define atslib_libats_libc_drand48 drand48
#endif
#define atslib_libats_libc_erand48 erand48
#define atslib_libats_libc_lrand48 lrand48
#define atslib_libats_libc_nrand48 nrand48
Expand Down
4 changes: 4 additions & 0 deletions libats/libc/CATS/string.cats
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,11 @@
/* ****** ****** */

#define atslib_libats_libc_strerror strerror
#if _WIN32
#define atslib_libats_libc_strerror_r(err_code, buf, buflen) (!strerror_s(buf, buflen, err_code))
#else
#define atslib_libats_libc_strerror_r strerror_r
#endif

/* ****** ****** */

Expand Down
Loading