[MLton-commit] r4300

Matthew Fluet MLton@mlton.org
Fri, 6 Jan 2006 15:19:21 -0800


Playing with C-types
----------------------------------------------------------------------

U   mlton/branches/on-20050822-x86_64-branch/runtime/cenv.h
U   mlton/branches/on-20050822-x86_64-branch/runtime/gen/gen-types.c

----------------------------------------------------------------------

Modified: mlton/branches/on-20050822-x86_64-branch/runtime/cenv.h
===================================================================
--- mlton/branches/on-20050822-x86_64-branch/runtime/cenv.h	2006-01-06 23:17:36 UTC (rev 4299)
+++ mlton/branches/on-20050822-x86_64-branch/runtime/cenv.h	2006-01-06 23:19:20 UTC (rev 4300)
@@ -9,43 +9,53 @@
 #ifndef _MLTON_CENV_H_
 #define _MLTON_CENV_H_
 
+/* GNU C Library Feature Macros */
 #define _ISOC99_SOURCE
 #define _BSD_SOURCE
-
-/* Only enable _POSIX_C_SOURCE on platforms that don't have broken system
- * headers.
+// #define _XOPEN_SOURCE 600
+/* Only enable _POSIX_C_SOURCE on platforms that don't have broken
+ * system headers.
  */
 #if (defined (__linux__))
 #define _POSIX_C_SOURCE 200112L
 #endif
 
-/* C99-specific headers */
-#include <stddef.h>
+/* C99 headers */
+// #include <assert.h>
+// #include <complex.h>
+// #include <ctype.h>
+#include <errno.h>
+// #include <fenv.h>
+#include <float.h>
+#include <inttypes.h>
+#include <iso646.h>
+#include <limits.h>
+// #include <locale.h>
+#include <math.h>
+// #include <setjmp.h>
+#include <signal.h>
 #include <stdarg.h>
-#include <stdlib.h>
 #include <stdbool.h>
-#include <iso646.h>
+#include <stddef.h>
 #include <stdint.h>
-#include <inttypes.h>
-#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
-#include <stdio.h>
-#include <math.h>
+// #include <tgmath.h>
+#include <time.h>
+// #include <wchar.h>
+// #include <wctype.h>
 
-#include <errno.h>
 #include <fcntl.h>
 #include <unistd.h>
 
 #include <dirent.h>
-#include <signal.h>
 #include <termios.h>
-#include <time.h>
 #include <utime.h>
 #include <sys/resource.h>
 #include <sys/stat.h>
 #include <sys/time.h>
 
-
 #include "gmp.h"
 
 #endif /* _MLTON_CENV_H_ */

Modified: mlton/branches/on-20050822-x86_64-branch/runtime/gen/gen-types.c
===================================================================
--- mlton/branches/on-20050822-x86_64-branch/runtime/gen/gen-types.c	2006-01-06 23:17:36 UTC (rev 4299)
+++ mlton/branches/on-20050822-x86_64-branch/runtime/gen/gen-types.c	2006-01-06 23:19:20 UTC (rev 4300)
@@ -5,10 +5,10 @@
  * See the file MLton-LICENSE for details.
  */
 
+#include "cenv.h"
 #include "util.h"
 
-#define prefixLines 26
-static char* prefix[prefixLines] = {
+static char* prefix[] = {
   "/* Copyright (C) 2004-2005 Henry Cejtin, Matthew Fluet, Suresh",
   " *    Jagannathan, and Stephen Weeks.",
   " *",
@@ -16,13 +16,15 @@
   " * See the file MLton-LICENSE for details.",
   " */",
   "",
-  "/* Can't use _TYPES_H_ because MSVCRT uses it.  So, we use _MLTON_TYPES_H_. */",
+  "/* Can't use _TYPES_H_ because MSVCRT uses it.",
+  " * So, we use _MLTON_TYPES_H_.",
+  " */",
   "",
   "#ifndef _MLTON_TYPES_H_",
   "#define _MLTON_TYPES_H_",
   "",
-  "/* We need these because in header files for exported SML functions, types.h is",
-  " * included without platform.h.",
+  "/* We need these because in header files for exported SML functions, ",
+  " * types.h is included without cenv.h.",
   " */",
   "#ifndef _ISOC99_SOURCE",
   "#define _ISOC99_SOURCE",
@@ -34,16 +36,21 @@
   "#else",
   "#include <stdint.h>",
   "#endif",
-  ""
+  "",
+  NULL
 };
 
-#define stdtypesLines 21
-static char* stdtypes[stdtypesLines] = {
+static char* stdtypes[] = {
+  "/* ML types */",
+  "typedef unsigned char* Pointer;",
+  "#define Array(t) Pointer",
+  "#define Ref(t) Pointer",
+  "#define Vector(t) Pointer",
+  "",
   "typedef int8_t Int8;",
   "typedef int16_t Int16;",
   "typedef int32_t Int32;",
   "typedef int64_t Int64;",
-  "typedef unsigned char* Pointer;",
   "typedef float Real32;",
   "typedef double Real64;",
   "typedef uint8_t Word8;",
@@ -59,12 +66,33 @@
   "typedef Word8 WordU8;",
   "typedef Word16 WordU16;",
   "typedef Word32 WordU32;",
-  "typedef Word64 WordU64;"
+  "typedef Word64 WordU64;",
+  "",
+  "typedef WordS8 Char8;",
+  "typedef WordS16 Char16;",
+  "typedef WordS32 Char32;",
+  "",
+  "typedef Vector(Char8) String8;",
+  "typedef Vector(Char16) String16;",
+  "typedef Vector(Char32) String32;",
+  "",
+  "typedef Int32 Bool;",
+  "typedef Char8 Char;",
+  "typedef Int32 Int;",
+  "typedef Real64 Real;",
+  "typedef String8 String;",
+  "typedef Word32 Word;",
+  ""
+  "typedef String NullString;",
+  NULL
 };
 
 #define systype(t, bt, name)               \
   do {                                     \
   writeString (fd, "typedef ");            \
+  writeString (fd, "/* ");                 \
+  writeString (fd, #t);                    \
+  writeString (fd, " */ ");                \
   writeString (fd, bt);                    \
   writeUintmaxU (fd, CHAR_BIT * sizeof(t));\
   writeString (fd, " ");                   \
@@ -72,52 +100,119 @@
   writeString (fd, ";");                   \
   writeNewline (fd);                       \
   } while (0)
+#define chkintsystype(t, name)             \
+  do {                                     \
+  if ((double)((t)(-1)) > 0)               \
+  systype(t, "Word", name);                \
+  else                                     \
+  systype(t, "Int", name);                 \
+  } while (0)
+#define chknumsystype(t, name)             \
+  do {                                     \
+  if ((double)((t)(0.25)) > 0)             \
+  systype(t, "Real", name);                \
+  else                                     \
+  chkintsystype(t, name);                  \
+  } while (0)
 
-#define suffixLines 2
-static char* suffix[suffixLines] = {
+static char* suffix[] = {
   "",
-  "#endif /* _MLTON_TYPES_H_ */"
+  "typedef C_Pointer Cpointer;",
+  "typedef C_Size Size;",
+  "typedef C_SSize Ssize;",
+  "typedef C_String Cstring;",
+  "typedef C_StringArray CstringArray;",
+  "typedef C_Off Position;"
+  "",
+  "typedef C_Fd Fd;",
+  "typedef C_TCFlag Flag;",
+  "typedef C_GId Gid;",
+  "typedef C_Mode Mode;",
+  "typedef C_PId Pid;",
+  "typedef C_Resource Resource;",
+  "typedef C_RLim Rlimit;",
+  "typedef C_Signal Signal;",
+  "typedef C_Speed Speed;",
+  "typedef C_Status Status;",
+  "typedef C_UId Uid;",
+  "",
+  "#endif /* _MLTON_TYPES_H_ */",
+  NULL
 };
 
 int main (int argc, char* argv[]) {
   int fd;
 
   fd = open_safe ("types.h", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
-  for (int i = 0; i < prefixLines; i++) {
+  for (int i = 0; prefix[i] != NULL; i++) {
     writeString (fd, prefix[i]);
     writeNewline (fd);
   }
-  for (int i = 0; i < stdtypesLines; i++) {
+  for (int i = 0; stdtypes[i] != NULL; i++) {
     writeString (fd, stdtypes[i]);
     writeNewline (fd);
   }
   writeNewline (fd);
-  systype(char, "Word", "Char");
-  systype(int, "Int", "Int");
-  systype(off_t, "Int", "Position");
-  systype(double, "Real", "Real");
-  systype(unsigned int, "Word", "Word");
-  systype(int, "Int", "Bool");
+  writeString (fd, "/* C */");
   writeNewline (fd);
-  systype(unsigned char*, "Word", "Cpointer");
-  systype(char*, "Word", "Cstring");
-  systype(char**, "Word", "CstringArray");
-  systype(size_t, "Int", "Size");
-  systype(ssize_t, "Int", "Ssize");
+  chkintsystype(char, "C_Char");
+  systype(signed char, "Int", "C_SChar");
+  systype(unsigned char, "Word", "C_UChar");
+  systype(short, "Int", "C_Short");
+  systype(unsigned short, "Word", "C_UShort");
+  systype(int, "Int", "C_Int");
+  systype(unsigned int, "Word", "C_UInt");
+  systype(long, "Int", "C_Long");
+  systype(unsigned long, "Word", "C_ULong");
+  systype(long long, "Int", "C_LongLong");
+  systype(unsigned long long, "Word", "C_ULongLong");
+  systype(float, "Real", "C_Float");
+  systype(double, "Real", "C_Double");
+  // systype(long double, "Real", "C_LongDouble");
+  systype(size_t, "Word", "C_Size");
   writeNewline (fd);
-  systype(int, "Int", "Fd");
-  systype(tcflag_t, "Word", "Flag");
-  systype(gid_t, "Word", "Gid");
-  systype(mode_t, "Int", "Mode");
-  systype(pid_t, "Int", "Pid");
-  systype(int, "Int", "Resource");
-  systype(rlim_t, "Word", "Rlimit");
-  systype(int, "Int", "Signal");
-  systype(speed_t, "Word", "Speed");
-  systype(int, "Int", "Status");
-  systype(uid_t, "Word", "Uid");
+  systype(void*, "Word", "C_Pointer");
+  systype(char*, "Word", "C_String");
+  systype(char**, "Word", "C_StringArray");
   writeNewline (fd);
-  for (int i = 0; i < suffixLines; i++) {
+  writeString (fd, "/* C99 */");
+  writeNewline (fd);
+  systype(intmax_t, "Int", "C_Intmax");
+  systype(uintmax_t, "Word", "C_Uintmax");
+  systype(intptr_t, "Int", "C_Intptr");
+  systype(uintptr_t, "Word", "C_UIntptr");
+  writeNewline (fd);
+  writeString (fd, "/* from <sys/resource.h> */");
+  writeNewline (fd);
+  systype(rlim_t, "Word", "C_RLim");
+  writeNewline (fd);
+  writeString (fd, "/* from <sys/types.h> */");
+  writeNewline (fd);
+  chknumsystype(clock_t, "C_Clock");
+  chkintsystype(gid_t, "C_GId");
+  chkintsystype(id_t, "C_Id");
+  chkintsystype(mode_t, "C_Mode");
+  systype(off_t, "Int", "C_Off");
+  systype(pid_t, "Int", "C_PId");
+  systype(ssize_t, "Int", "C_SSize");
+  systype(suseconds_t, "Int", "C_SUSeconds");
+  chknumsystype(time_t, "C_Time");
+  chkintsystype(uid_t, "C_UId");
+  systype(useconds_t, "Word", "C_USeconds");
+  writeNewline (fd);
+  writeString (fd, "/* from <termios.h> */");
+  writeNewline (fd);
+  systype(tcflag_t, "Word", "C_TCFlag");
+  systype(speed_t, "Word", "C_Speed");
+  writeNewline (fd);
+  writeString (fd, "/* Generic integers */");
+  writeNewline (fd);
+  systype(int, "Int", "C_Fd");
+  systype(int, "Int", "C_Resource");
+  systype(int, "Int", "C_Signal");
+  systype(int, "Int", "C_Status");
+  writeNewline (fd);
+  for (int i = 0; suffix[i] != NULL; i++) {
     writeString (fd, suffix[i]);
     writeNewline (fd);
   }