on master help sign in
Set C language standard to C99 in runtime and codegen (#4308)
a4c183cAlex Queiroz
config.mk+1−1
@@ -14,7 +14,7 @@ RANLIB ?=ranlib
14CABAL :=cabal
14CABAL :=cabal
15# IDRIS_ENABLE_STATS should not be set in final release
15# IDRIS_ENABLE_STATS should not be set in final release
16# Any flags defined here which alter the RTS API must also be added to src/IRTS/CodegenC.hs
16# Any flags defined here which alter the RTS API must also be added to src/IRTS/CodegenC.hs
17−CFLAGS :=-O2 -Wall -DHAS_PTHREAD -DIDRIS_ENABLE_STATS $(CFLAGS)
17+CFLAGS :=-O2 -Wall -std=c99 -D_POSIX_C_SOURCE=200809L -DHAS_PTHREAD -DIDRIS_ENABLE_STATS $(CFLAGS)
18
18
19# CABALFLAGS :=
19# CABALFLAGS :=
20CABALFLAGS += --enable-tests
20CABALFLAGS += --enable-tests
libs/base/System.idr+3−3
@@ -93,9 +93,9 @@ time : IO Integer
93time = do MkRaw t <- foreign FFI_C "idris_time" (IO (Raw Integer))
93time = do MkRaw t <- foreign FFI_C "idris_time" (IO (Raw Integer))
94 pure t
94 pure t
95
95
96−||| Specify interval to sleep for, must be in range [0, 1000000]
96+||| Specify interval to sleep for in microseconds, must be nonnegative
97−usleep : (i : Int) -> { auto prf : So (i >= 0 && i <= 1000000) } -> IO ()
97+usleep : (i : Int) -> { auto prf : So (i >= 0) } -> IO ()
98−usleep i = foreign FFI_C "usleep" (Int -> IO ()) i
98+usleep i = foreign FFI_C "idris_usleep" (Int -> IO ()) i
99
99
100||| Execute a program and returns its exit status code.
100||| Execute a program and returns its exit status code.
101system : String -> IO Int
101system : String -> IO Int
libs/effects/Effect/System.idr+2−2
@@ -15,7 +15,7 @@ data System : Effect where
15 Time : sig System Integer
15 Time : sig System Integer
16 GetEnv : String -> sig System (Maybe String)
16 GetEnv : String -> sig System (Maybe String)
17 CSystem : String -> sig System Int
17 CSystem : String -> sig System Int
18− Usleep : (i : Int) -> (inbounds : So (i >= 0 && i <= 1000000)) -> sig System ()
18+ Usleep : (i : Int) -> (inbounds : So (i >= 0)) -> sig System ()
19
19
20implementation Handler System IO where
20implementation Handler System IO where
21 handle () Args k = do x <- getArgs; k x ()
21 handle () Args k = do x <- getArgs; k x ()
@@ -48,5 +48,5 @@ getEnv s = call $ GetEnv s
48system : String -> Eff Int [SYSTEM]
48system : String -> Eff Int [SYSTEM]
49system s = call $ CSystem s
49system s = call $ CSystem s
50
50
51−usleep : (i : Int) -> { auto prf : So (i >= 0 && i <= 1000000) } -> Eff () [SYSTEM]
51+usleep : (i : Int) -> { auto prf : So (i >= 0) } -> Eff () [SYSTEM]
52usleep i {prf} = call $ Usleep i prf
52usleep i {prf} = call $ Usleep i prf
rts/idris_main.c+6−2
@@ -4,8 +4,11 @@
4#include "idris_stats.h"
4#include "idris_stats.h"
5
5
6void _idris__123_runMain_95_0_125_(VM* vm, VAL* oldbase);
6void _idris__123_runMain_95_0_125_(VM* vm, VAL* oldbase);
7−#if defined(WIN32) || defined(__WIN32) || defined(__WIN32__)
7+
8+#ifdef _WIN32
9+
8#include <Windows.h>
10#include <Windows.h>
11+
9int win32_get_argv_utf8(int *argc_ptr, char ***argv_ptr)
12int win32_get_argv_utf8(int *argc_ptr, char ***argv_ptr)
10{
13{
11 int argc;
14 int argc;
@@ -26,6 +29,7 @@ int win32_get_argv_utf8(int *argc_ptr, char ***argv_ptr)
26 *argv_ptr = argv;
29 *argv_ptr = argv;
27 return 0;
30 return 0;
28}
31}
32+
29#endif
33#endif
30
34
31// The default options should give satisfactory results under many circumstances.
35// The default options should give satisfactory results under many circumstances.
@@ -35,7 +39,7 @@ RTSOpts opts = {
35 .show_summary = 0
39 .show_summary = 0
36};
40};
37
41
38−#if defined(WIN32) || defined(__WIN32) || defined(__WIN32__)
42+#ifdef _WIN32
39int main() {
43int main() {
40 int argc;
44 int argc;
41 char **argv;
45 char **argv;
rts/idris_net.c+2−2
@@ -8,7 +8,7 @@
8#include <stdio.h>
8#include <stdio.h>
9#include <string.h>
9#include <string.h>
10
10
11−#ifndef WIN32
11+#ifndef _WIN32
12#include <netinet/in.h>
12#include <netinet/in.h>
13#include <arpa/inet.h>
13#include <arpa/inet.h>
14#else
14#else
@@ -58,7 +58,7 @@ void idrnet_free(void* ptr) {
58
58
59
59
60int idrnet_socket(int domain, int type, int protocol) {
60int idrnet_socket(int domain, int type, int protocol) {
61−#ifdef WIN32
61+#ifdef _WIN32
62 if (!check_init()) {
62 if (!check_init()) {
63 return -1;
63 return -1;
64 }
64 }
rts/idris_net.h+1−1
@@ -2,7 +2,7 @@
2#define IDRISNET_H
2#define IDRISNET_H
3
3
4// Includes used by the idris-file.
4// Includes used by the idris-file.
5−#ifdef WIN32
5+#ifdef _WIN32
6#include <winsock2.h>
6#include <winsock2.h>
7#include <Ws2tcpip.h>
7#include <Ws2tcpip.h>
8#else
8#else
rts/idris_rts.c+8
@@ -1225,6 +1225,14 @@ void idris_disableBuffering(void) {
1225 setvbuf(stdout, NULL, _IONBF, 0);
1225 setvbuf(stdout, NULL, _IONBF, 0);
1226}
1226}
1227
1227
1228+int idris_usleep(int usec) {
1229+ struct timespec t;
1230+ t.tv_sec = usec / 1000000;
1231+ t.tv_nsec = (usec % 1000000) * 1000;
1232+
1233+ return nanosleep(&t, NULL);
1234+}
1235+
1228void stackOverflow(void) {
1236void stackOverflow(void) {
1229 fprintf(stderr, "Stack overflow");
1237 fprintf(stderr, "Stack overflow");
1230 exit(-1);
1238 exit(-1);
rts/idris_rts.h+2
@@ -442,6 +442,8 @@ const char *idris_getArg(int i);
442// disable stdin/stdout buffering
442// disable stdin/stdout buffering
443void idris_disableBuffering(void);
443void idris_disableBuffering(void);
444
444
445+int idris_usleep(int usec);
446+
445// Handle stack overflow.
447// Handle stack overflow.
446// Just reports an error and exits.
448// Just reports an error and exits.
447
449
rts/idris_stdfgn.c+5−5
@@ -10,7 +10,7 @@
10#include <time.h>
10#include <time.h>
11#include <dirent.h>
11#include <dirent.h>
12
12
13−#if defined(WIN32) || defined(__WIN32) || defined(__WIN32__)
13+#ifdef _WIN32
14int win_fpoll(void* h);
14int win_fpoll(void* h);
15FILE *win32_u8fopen(const char *path, const char *mode);
15FILE *win32_u8fopen(const char *path, const char *mode);
16FILE *win32_u8popen(const char *path, const char *mode);
16FILE *win32_u8popen(const char *path, const char *mode);
@@ -25,7 +25,7 @@ void putStr(char* str) {
25}
25}
26
26
27void *fileOpen(char *name, char *mode) {
27void *fileOpen(char *name, char *mode) {
28−#if defined(WIN32) || defined(__WIN32) || defined(__WIN32__)
28+#ifdef _WIN32
29 FILE *f = win32_u8fopen(name, mode);
29 FILE *f = win32_u8fopen(name, mode);
30#else
30#else
31 FILE *f = fopen(name, mode);
31 FILE *f = fopen(name, mode);
@@ -97,7 +97,7 @@ char* idris_nextDirEntry(void* h) {
97}
97}
98
98
99int idris_mkdir(char* dname) {
99int idris_mkdir(char* dname) {
100−#if defined(WIN32) || defined(__WIN32) || defined(__WIN32__)
100+#ifdef _WIN32
101 return mkdir(dname);
101 return mkdir(dname);
102#else
102#else
103 return mkdir(dname, S_IRWXU | S_IRGRP | S_IROTH);
103 return mkdir(dname, S_IRWXU | S_IRGRP | S_IROTH);
@@ -119,7 +119,7 @@ int idris_writeStr(void* h, char* str) {
119
119
120int fpoll(void* h)
120int fpoll(void* h)
121{
121{
122−#if defined(WIN32) || defined(__WIN32) || defined(__WIN32__)
122+#ifdef _WIN32
123 return win_fpoll(h);
123 return win_fpoll(h);
124#else
124#else
125 FILE* f = (FILE*)h;
125 FILE* f = (FILE*)h;
@@ -138,7 +138,7 @@ int fpoll(void* h)
138}
138}
139
139
140void *do_popen(const char *cmd, const char *mode) {
140void *do_popen(const char *cmd, const char *mode) {
141−#if defined(WIN32) || defined(__WIN32) || defined(__WIN32__)
141+#ifdef _WIN32
142 FILE *f = win32_u8popen(cmd, mode);
142 FILE *f = win32_u8popen(cmd, mode);
143#else
143#else
144 FILE *f = popen(cmd, mode);
144 FILE *f = popen(cmd, mode);
src/IRTS/CodegenC.hs+1−1
@@ -83,7 +83,7 @@ codegenC' defs out exec incs objs libs flags exports iface dbg
83 let args = gccDbg dbg ++
83 let args = gccDbg dbg ++
84 gccFlags iface ++
84 gccFlags iface ++
85 -- # Any flags defined here which alter the RTS API must also be added to config.mk
85 -- # Any flags defined here which alter the RTS API must also be added to config.mk
86− ["-DHAS_PTHREAD", "-DIDRIS_ENABLE_STATS",
86+ ["-std=c99", "-D_POSIX_C_SOURCE=200809L", "-DHAS_PTHREAD", "-DIDRIS_ENABLE_STATS",
87 "-I."] ++ objs ++ envFlags ++
87 "-I."] ++ objs ++ envFlags ++
88 (if (exec == Executable) then [] else ["-c"]) ++
88 (if (exec == Executable) then [] else ["-c"]) ++
89 [tmpn] ++
89 [tmpn] ++