[pure-lang-svn] SF.net SVN: pure-lang: [393] pure/trunk
Status: Beta
Brought to you by:
agraef
From: <ag...@us...> - 2008-07-05 23:50:03
|
Revision: 393 http://pure-lang.svn.sourceforge.net/pure-lang/?rev=393&view=rev Author: agraef Date: 2008-07-05 16:50:12 -0700 (Sat, 05 Jul 2008) Log Message: ----------- Add time functions to system interface. Modified Paths: -------------- pure/trunk/ChangeLog pure/trunk/config.h.in pure/trunk/configure pure/trunk/configure.ac pure/trunk/lib/system.pure pure/trunk/runtime.cc pure/trunk/runtime.h Modified: pure/trunk/ChangeLog =================================================================== --- pure/trunk/ChangeLog 2008-07-05 21:49:42 UTC (rev 392) +++ pure/trunk/ChangeLog 2008-07-05 23:50:12 UTC (rev 393) @@ -1,3 +1,8 @@ +2008-07-06 Albert Graef <Dr....@t-...> + + * runtime.cc/h, lib/system.pure: Added a few time functions to the + system interface. + 2008-07-03 Albert Graef <Dr....@t-...> * interpreter.cc (run): Temporarily suppress verbose output for Modified: pure/trunk/config.h.in =================================================================== --- pure/trunk/config.h.in 2008-07-05 21:49:42 UTC (rev 392) +++ pure/trunk/config.h.in 2008-07-05 23:50:12 UTC (rev 393) @@ -15,6 +15,12 @@ */ #undef HAVE_ALLOCA_H +/* Define to 1 if you have the `ftime' function. */ +#undef HAVE_FTIME + +/* Define to 1 if you have the `gettimeofday' function. */ +#undef HAVE_GETTIMEOFDAY + /* Define if you have the iconv() function. */ #undef HAVE_ICONV @@ -42,6 +48,9 @@ /* Define to 1 if you have the <memory.h> header file. */ #undef HAVE_MEMORY_H +/* Define to 1 if you have the `nanosleep' function. */ +#undef HAVE_NANOSLEEP + /* Define to 1 if you have the <stdint.h> header file. */ #undef HAVE_STDINT_H @@ -63,6 +72,9 @@ /* Define to 1 if you have the <unistd.h> header file. */ #undef HAVE_UNISTD_H +/* Define to 1 if you have the `usleep' function. */ +#undef HAVE_USLEEP + /* Define to the name of the host system. */ #undef HOST Modified: pure/trunk/configure =================================================================== --- pure/trunk/configure 2008-07-05 21:49:42 UTC (rev 392) +++ pure/trunk/configure 2008-07-05 23:50:12 UTC (rev 393) @@ -5366,6 +5366,103 @@ fi + + + + +for ac_func in ftime gettimeofday nanosleep usleep +do +as_ac_var=`echo "ac_cv_func_$ac_func" | $as_tr_sh` +{ echo "$as_me:$LINENO: checking for $ac_func" >&5 +echo $ECHO_N "checking for $ac_func... $ECHO_C" >&6; } +if { as_var=$as_ac_var; eval "test \"\${$as_var+set}\" = set"; }; then + echo $ECHO_N "(cached) $ECHO_C" >&6 +else + cat >conftest.$ac_ext <<_ACEOF +/* confdefs.h. */ +_ACEOF +cat confdefs.h >>conftest.$ac_ext +cat >>conftest.$ac_ext <<_ACEOF +/* end confdefs.h. */ +/* Define $ac_func to an innocuous variant, in case <limits.h> declares $ac_func. + For example, HP-UX 11i <limits.h> declares gettimeofday. */ +#define $ac_func innocuous_$ac_func + +/* System header to define __stub macros and hopefully few prototypes, + which can conflict with char $ac_func (); below. + Prefer <limits.h> to <assert.h> if __STDC__ is defined, since + <limits.h> exists even on freestanding compilers. */ + +#ifdef __STDC__ +# include <limits.h> +#else +# include <assert.h> +#endif + +#undef $ac_func + +/* Override any GCC internal prototype to avoid an error. + Use char because int might match the return type of a GCC + builtin and then its argument prototype would still apply. */ +#ifdef __cplusplus +extern "C" +#endif +char $ac_func (); +/* The GNU C library defines this for functions which it implements + to always fail with ENOSYS. Some functions are actually named + something starting with __ and the normal name is an alias. */ +#if defined __stub_$ac_func || defined __stub___$ac_func +choke me +#endif + +int +main () +{ +return $ac_func (); + ; + return 0; +} +_ACEOF +rm -f conftest.$ac_objext conftest$ac_exeext +if { (ac_try="$ac_link" +case "(($ac_try" in + *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;; + *) ac_try_echo=$ac_try;; +esac +eval "echo \"\$as_me:$LINENO: $ac_try_echo\"") >&5 + (eval "$ac_link") 2>conftest.er1 + ac_status=$? + grep -v '^ *+' conftest.er1 >conftest.err + rm -f conftest.er1 + cat conftest.err >&5 + echo "$as_me:$LINENO: \$? = $ac_status" >&5 + (exit $ac_status); } && { + test -z "$ac_c_werror_flag" || + test ! -s conftest.err + } && test -s conftest$ac_exeext && + $as_test_x conftest$ac_exeext; then + eval "$as_ac_var=yes" +else + echo "$as_me: failed program was:" >&5 +sed 's/^/| /' conftest.$ac_ext >&5 + + eval "$as_ac_var=no" +fi + +rm -f core conftest.err conftest.$ac_objext conftest_ipa8_conftest.oo \ + conftest$ac_exeext conftest.$ac_ext +fi +ac_res=`eval echo '${'$as_ac_var'}'` + { echo "$as_me:$LINENO: result: $ac_res" >&5 +echo "${ECHO_T}$ac_res" >&6; } +if test `eval echo '${'$as_ac_var'}'` = yes; then + cat >>confdefs.h <<_ACEOF +#define `echo "HAVE_$ac_func" | $as_tr_cpp` 1 +_ACEOF + +fi +done + ac_config_files="$ac_config_files Makefile" cat >confcache <<\_ACEOF Modified: pure/trunk/configure.ac =================================================================== --- pure/trunk/configure.ac 2008-07-05 21:49:42 UTC (rev 392) +++ pure/trunk/configure.ac 2008-07-05 23:50:12 UTC (rev 393) @@ -85,6 +85,8 @@ AM_LANGINFO_CODESET dnl Determine how to get alloca. AC_FUNC_ALLOCA +dnl Platform-dependent time functions. +AC_CHECK_FUNCS(ftime gettimeofday nanosleep usleep) AC_CONFIG_FILES([Makefile]) AC_OUTPUT Modified: pure/trunk/lib/system.pure =================================================================== --- pure/trunk/lib/system.pure 2008-07-05 21:49:42 UTC (rev 392) +++ pure/trunk/lib/system.pure 2008-07-05 23:50:12 UTC (rev 393) @@ -47,6 +47,48 @@ errno = pure_errno; set_errno val::int = pure_set_errno val; +/* Time functions. 'time' reports the current time in seconds since the + "epoch" a.k.a. 00:00:00 UTC, Jan 1 1970. The result is always a bigint (in + fact, the time value is already 64 bit on many OSes nowadays). The ctime + and gmtime functions convert a time value to a string in either local time + or UTC. (Note that the latter is actually a combination of the C gmtime() + and asctime() functions.) */ + +extern long pure_time() = time; +extern char* pure_ctime(long) = ctime; +extern char* pure_gmtime(long) = gmtime; + +/* The gettimeofday function also returns wallclock time as seconds since the + epoch, but theoretically offers resolutions in the microsec range (actual + resolutions vary, but are usually in the msec range for contemporary + systems). The result is returned as a double value (which also limits + precision). This function may actually be implemented through different + system calls, depending on what's available on the host OS. */ + +extern double pure_gettimeofday() = gettimeofday; + +/* The clock function returns the current CPU (not wallclock) time since an + arbitrary point in the past, as a machine int. The number of "ticks" per + second is given by the CLOCKS_PER_SEC constant. Note that this value will + wrap around approximately every 72 minutes. */ + +extern int clock(); + +/* The sleep and nanosleep functions suspend execution for a given time + interval in seconds. 'sleep' takes integer (int/bigint) arguments only and + uses the sleep() system function. 'nanosleep' also accepts double arguments + and theoretically supports resolutions down to 1 nanosecond (again, actual + resolutions vary). This function may actually be implemented through + different system calls, depending on what's available on the host OS. Both + functions usually return zero, unless the sleep was interrupted by a + signal, in which case the time remaining to be slept is returned. */ + +extern int sleep(int); +extern double pure_nanosleep(double) = nanosleep; + +sleep t::bigint = sleep (int t); +nanosleep t::int | nanosleep t::bigint = nanosleep (double t); + /* Basic process operations: system executes a shell command, exit terminates the program with the given status code. */ Modified: pure/trunk/runtime.cc =================================================================== --- pure/trunk/runtime.cc 2008-07-05 21:49:42 UTC (rev 392) +++ pure/trunk/runtime.cc 2008-07-05 23:50:12 UTC (rev 393) @@ -25,6 +25,7 @@ #include <readline/history.h> #include <stdlib.h> #include <stdarg.h> +#include <unistd.h> #include <iostream> #include <sstream> @@ -2340,7 +2341,112 @@ errno = value; } +#include <time.h> + +extern "C" +int64_t pure_time(void) +{ + return (int64_t)time(NULL); +} + +extern "C" +char *pure_ctime(int64_t t) +{ + time_t time = (time_t)t; + return ctime(&time); +} + +extern "C" +char *pure_gmtime(int64_t t) +{ + time_t time = (time_t)t; + return asctime(gmtime(&time)); +} + +#ifdef HAVE_GETTIMEOFDAY +#include <sys/time.h> +extern "C" +double pure_gettimeofday(void) +{ + struct timeval tv; + gettimeofday(&tv, NULL); + return ((double)tv.tv_sec)+((double)tv.tv_usec)*1e-6; +} +#else +#ifdef HAVE_FTIME +#include <sys/timeb.h> +extern "C" +double pure_gettimeofday(void) +{ + struct timeb tb; + ftime(&tb); + return ((double)tb.time)+((double)tb.millitm)*1e-3; +} +#else +extern "C" +double pure_gettimeofday(void) +{ + return (double)time(NULL); +} +#endif +#endif + #ifdef __MINGW32__ +#include <windows.h> +double pure_nanosleep(double t) +{ + if (t > 0.0) { + unsigned long secs; + unsigned short msecs; + double ip, fp; + if (t > LONG_MAX) t = LONG_MAX; + fp = modf(t, &ip); + secs = (unsigned long)ip; + msecs = (unsigned short)(fp*1e3); + Sleep(secs*1000U+msecs); + } + return 0.0; +} +#else +double pure_nanosleep(double t) +{ + if (t > 0.0) { + double ip, fp; + unsigned long secs; +#ifdef HAVE_NANOSLEEP + unsigned long nsecs; + struct timespec req, rem; + fp = modf(t, &ip); + if (ip > LONG_MAX) { ip = (double)LONG_MAX; fp = 0.0; } + secs = (unsigned long)ip; + nsecs = (unsigned long)(fp*1e9); + req.tv_sec = secs; req.tv_nsec = nsecs; + if (nanosleep(&req, &rem)) + return ((double)rem.tv_sec)+((double)rem.tv_nsec)*1e-9; + else + return 0.0; +#else +#ifdef HAVE_USLEEP + unsigned long usecs; + if (t > LONG_MAX) t = LONG_MAX; + fp = modf(t, &ip); + secs = (unsigned long)ip; + usecs = (unsigned long)(fp*1e6); + usleep(secs*1000000U+usecs); + return 0.0; +#else + fp = modf(t, &ip); + if (ip > LONG_MAX) ip = (double)LONG_MAX; + secs = (unsigned long)ip; + return (double)sleep(secs); +#endif +#endif + } else + return 0.0; +} +#endif + +#ifdef __MINGW32__ extern "C" FILE *popen(const char *command, const char *type) { @@ -2621,9 +2727,11 @@ { interpreter& interp = *interpreter::g_interp; // standard I/O streams - interp.defn("stdin", pure_pointer(stdin)); + interp.defn("stdin", pure_pointer(stdin)); interp.defn("stdout", pure_pointer(stdout)); interp.defn("stderr", pure_pointer(stderr)); + // clock + interp.defn("CLOCKS_PER_SEC", pure_int(CLOCKS_PER_SEC)); // fnmatch, glob interp.defn("FNM_NOESCAPE", pure_int(FNM_NOESCAPE)); interp.defn("FNM_PATHNAME", pure_int(FNM_PATHNAME)); Modified: pure/trunk/runtime.h =================================================================== --- pure/trunk/runtime.h 2008-07-05 21:49:42 UTC (rev 392) +++ pure/trunk/runtime.h 2008-07-05 23:50:12 UTC (rev 393) @@ -546,6 +546,26 @@ int pure_errno(void); void pure_set_errno(int value); +/* time() function. We provide an interface to this routine to account for + platform incompatibilities. The result is always int64_t, as time_t + nowadays is a 64 bit type on many OSes. We also provide wrappers for + ctime() and gmtime() which convert a time value to a string, using either + local or UTC time. */ + +int64_t pure_time(void); +char *pure_ctime(int64_t t); +char *pure_gmtime(int64_t t); + +/* gettimeofday() interface. This may actually be implemented using different + system functions, depending on what's available on the host OS. */ + +double pure_gettimeofday(void); + +/* nanosleep() interface. This may actually be implemented using different + system functions, depending on what's available on the host OS. */ + +double pure_nanosleep(double t); + #ifdef __MINGW32__ /* Windows compatibility. */ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |