[MLton-commit] r5000

Vesa Karvonen vesak at mlton.org
Thu Dec 28 10:36:33 PST 2006


Initial commit of libc mlnlffi interface.  There is still much to do
(perhaps even including extensions to mlnlffigen).

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

A   mltonlib/trunk/org/mlton/vesak/
A   mltonlib/trunk/org/mlton/vesak/libc/
A   mltonlib/trunk/org/mlton/vesak/libc/unstable/
A   mltonlib/trunk/org/mlton/vesak/libc/unstable/LICENSE
A   mltonlib/trunk/org/mlton/vesak/libc/unstable/Makefile
A   mltonlib/trunk/org/mlton/vesak/libc/unstable/README
A   mltonlib/trunk/org/mlton/vesak/libc/unstable/detail/
A   mltonlib/trunk/org/mlton/vesak/libc/unstable/detail/c/
A   mltonlib/trunk/org/mlton/vesak/libc/unstable/detail/c/stdio.c
A   mltonlib/trunk/org/mlton/vesak/libc/unstable/detail/c/util.h
A   mltonlib/trunk/org/mlton/vesak/libc/unstable/detail/config-gen.c
A   mltonlib/trunk/org/mlton/vesak/libc/unstable/libc.mlb
A   mltonlib/trunk/org/mlton/vesak/libc/unstable/public/
A   mltonlib/trunk/org/mlton/vesak/libc/unstable/public/common.h
A   mltonlib/trunk/org/mlton/vesak/libc/unstable/public/stdio.h

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


Property changes on: mltonlib/trunk/org/mlton/vesak/libc/unstable
___________________________________________________________________
Name: svn:ignore
   + *.a
.*
generated


Added: mltonlib/trunk/org/mlton/vesak/libc/unstable/LICENSE
===================================================================
--- mltonlib/trunk/org/mlton/vesak/libc/unstable/LICENSE	2006-12-28 14:26:15 UTC (rev 4999)
+++ mltonlib/trunk/org/mlton/vesak/libc/unstable/LICENSE	2006-12-28 18:36:31 UTC (rev 5000)
@@ -0,0 +1,20 @@
+COPYRIGHT NOTICE, LICENSE AND DISCLAIMER.
+
+Copyright (C) 2006 Vesa Karvonen
+
+Permission to use, copy, modify, and distribute this software and its
+documentation for any purpose and without fee is hereby granted,
+provided that the above copyright notice appear in all copies and that
+both the copyright notice and this permission notice and warranty
+disclaimer appear in supporting documentation, and that the name of
+the above copyright holders, or their entities, not be used in
+advertising or publicity pertaining to distribution of the software
+without specific, written prior permission.
+
+The above copyright holders disclaim all warranties with regard to
+this software, including all implied warranties of merchantability and
+fitness. In no event shall the above copyright holders be liable for
+any special, indirect or consequential damages or any damages
+whatsoever resulting from loss of use, data or profits, whether in an
+action of contract, negligence or other tortious action, arising out
+of or in connection with the use or performance of this software.

Added: mltonlib/trunk/org/mlton/vesak/libc/unstable/Makefile
===================================================================
--- mltonlib/trunk/org/mlton/vesak/libc/unstable/Makefile	2006-12-28 14:26:15 UTC (rev 4999)
+++ mltonlib/trunk/org/mlton/vesak/libc/unstable/Makefile	2006-12-28 18:36:31 UTC (rev 5000)
@@ -0,0 +1,56 @@
+# Copyright (C) 2006 Vesa Karvonen
+#
+# This code is released under the MLton license, a BSD-style license.
+# See the LICENSE file or http://mlton.org/License for details.
+
+target_arch := $(shell mlton -show-path-map true | \
+                       awk '/^TARGET_ARCH/ {print $$2}')
+target_os   := $(shell mlton -show-path-map true | \
+                       awk '/^TARGET_OS/ {print $$2}')
+target_id   := $(target_arch)-$(target_os)
+
+lib_file    := libc-nlffi-$(target_id).a
+mlb_file    := libc.mlb
+
+config_h    := public/config-$(target_id).h
+cc_opts     := -Wall -std=c99
+
+bin_dir     := .bin/$(target_id)
+gen_dir     := generated/$(target_id)
+
+c_dir       := detail/c
+c_files     := $(wildcard $(c_dir)/*.c)
+o_files     := $(patsubst $(c_dir)/%.c,$(bin_dir)/%.o,$(c_files))
+
+.PHONY : all clean help
+
+help :
+	@echo "Targets:"
+	@echo "    all      Builds the library ($(lib_file)) and NLFFI files"
+	@echo "    clean    Removes generated files"
+	@echo "    help     Prints this message"
+
+all : $(gen_dir)/$(mlb_file) $(lib_file)
+
+clean :
+	rm -rf $(gen_dir) $(bin_dir) $(config_h) $(lib_file)
+
+$(config_h) : detail/config-gen.c
+	mkdir -p $(bin_dir)
+	gcc $(cc_opts) -o $(bin_dir)/config-gen $<
+	$(bin_dir)/config-gen > $@
+
+$(gen_dir)/$(mlb_file) : $(config_h) $(wildcard public/*.h)
+	mkdir -p $(gen_dir)
+	mlnlffigen -dir $(gen_dir)                                         \
+	           -mlbfile $(mlb_file)                                    \
+	           -cppopt '-U$(target_arch) -DTARGET_ARCH=$(target_arch)' \
+	           -cppopt '-U$(target_os) -DTARGET_OS=$(target_os)'       \
+	           -linkage static $^
+
+$(lib_file) : $(o_files)
+	ar cr $@ $^
+
+$(bin_dir)/%.o : $(c_dir)/%.c
+	mkdir -p $(bin_dir)
+	gcc $(cc_opts) -c -o $@ $<


Property changes on: mltonlib/trunk/org/mlton/vesak/libc/unstable/Makefile
___________________________________________________________________
Name: svn:eol-style
   + native

Added: mltonlib/trunk/org/mlton/vesak/libc/unstable/README
===================================================================
--- mltonlib/trunk/org/mlton/vesak/libc/unstable/README	2006-12-28 14:26:15 UTC (rev 4999)
+++ mltonlib/trunk/org/mlton/vesak/libc/unstable/README	2006-12-28 18:36:31 UTC (rev 5000)
@@ -0,0 +1,61 @@
+LibC MLNLFFI Interface
+----------------------
+
+   This library provides a MLNLFFI interface to (some portions of) the C
+   standard library (libc).
+
+
+Info
+----
+
+   License:         MLton license (a BSD-style license)
+   Portability:     portable
+   Stability:       experimental
+   Maintainer:      Vesa Karvonen <vesa.karvonen at cs.helsinki.fi>
+
+
+About Library Organization
+--------------------------
+
+   Makefile
+
+      This is a GNU makefile for building the library.  It is designed to
+      allow a single source tree to be built on multiple platforms.  Run
+      the command
+
+         make
+
+      to see a list of supported targets.
+
+   libc-nlffi-$(TARGET_ARCH)-$(TARGET_OS).a
+
+      This library contains the C compiled part of the libc interface.
+      You need to link your program against this library.  The variables
+      TARGET_ARCH and TARGET_OS refer to the corresponding variables as
+      defined by the compiler.  For example, with the mlton compiler
+      running the command
+
+         mlton -show-path-map true | grep TARGET
+
+      shows the variables.
+
+   public/
+
+      This directory contains the public header files used as source files
+      to mlnlffigen.
+
+   libc.mlb
+
+      This the MLB-file for the SML part of the libc interface.
+
+   detail/
+
+      This directory contains the implementation details of the library.
+
+
+About Motivation and Scope
+--------------------------
+
+   The main motivation of this library is to make it easier to implement
+   bindings to C libraries.  This library should eventually include all
+   parts of libc that make sense from that perspective.

Added: mltonlib/trunk/org/mlton/vesak/libc/unstable/detail/c/stdio.c
===================================================================
--- mltonlib/trunk/org/mlton/vesak/libc/unstable/detail/c/stdio.c	2006-12-28 14:26:15 UTC (rev 4999)
+++ mltonlib/trunk/org/mlton/vesak/libc/unstable/detail/c/stdio.c	2006-12-28 18:36:31 UTC (rev 5000)
@@ -0,0 +1,25 @@
+/* Copyright (C) 2006 Vesa Karvonen
+ *
+ * This code is released under the MLton license, a BSD-style license.
+ * See the LICENSE file or http://mlton.org/License for details.
+ */
+
+#include <stdio.h>
+#include "util.h"
+
+CONSTANT(int, EOF)
+CONSTANT(int, FILENAME_MAX)
+CONSTANT(int, FOPEN_MAX)
+CONSTANT(int, L_tmpnam)
+CONSTANT(int, SEEK_CUR)
+CONSTANT(int, SEEK_END)
+CONSTANT(int, SEEK_SET)
+CONSTANT(int, TMP_MAX)
+CONSTANT(int, _IOFBF)
+CONSTANT(int, _IOLBF)
+CONSTANT(int, _IONBF)
+CONSTANT(size_t, BUFSIZ)
+
+CONST_FN(FILE *, stderr)
+CONST_FN(FILE *, stdin)
+CONST_FN(FILE *, stdout)


Property changes on: mltonlib/trunk/org/mlton/vesak/libc/unstable/detail/c/stdio.c
___________________________________________________________________
Name: svn:eol-style
   + native

Added: mltonlib/trunk/org/mlton/vesak/libc/unstable/detail/c/util.h
===================================================================
--- mltonlib/trunk/org/mlton/vesak/libc/unstable/detail/c/util.h	2006-12-28 14:26:15 UTC (rev 4999)
+++ mltonlib/trunk/org/mlton/vesak/libc/unstable/detail/c/util.h	2006-12-28 18:36:31 UTC (rev 5000)
@@ -0,0 +1,8 @@
+/* Copyright (C) 2006 Vesa Karvonen
+ *
+ * This code is released under the MLton license, a BSD-style license.
+ * See the LICENSE file or http://mlton.org/License for details.
+ */
+
+#define CONSTANT(type, name) const type name##_ = name;
+#define CONST_FN(type, name) type name##_(void) {return name;}


Property changes on: mltonlib/trunk/org/mlton/vesak/libc/unstable/detail/c/util.h
___________________________________________________________________
Name: svn:eol-style
   + native

Added: mltonlib/trunk/org/mlton/vesak/libc/unstable/detail/config-gen.c
===================================================================
--- mltonlib/trunk/org/mlton/vesak/libc/unstable/detail/config-gen.c	2006-12-28 14:26:15 UTC (rev 4999)
+++ mltonlib/trunk/org/mlton/vesak/libc/unstable/detail/config-gen.c	2006-12-28 18:36:31 UTC (rev 5000)
@@ -0,0 +1,161 @@
+/* Copyright (C) 2006 Vesa Karvonen
+ *
+ * This code is released under the MLton license, a BSD-style license.
+ * See the LICENSE file or http://mlton.org/License for details.
+ */
+
+/* This simple C program is used to generate a configuration header that
+ * defines typedefs for standard types and typedefs.  For each type[def],
+ * this program simply chooses a type that has the correct size and kind
+ * (signed or unsigned integer).  The reason for doing this is that system
+ * and/or compiler headers may (and often do) contain non-standard C and
+ * choke mlnlffigen.  Also, mlnlffigen can't export macros, so we need to
+ * do something about them.  So, instead of #including system headers, we
+ * make our own headers.
+ */
+
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+/************************************************************************/
+
+static void
+fail(const char *format, ...) {
+  va_list args;
+  va_start(args, format);
+  fprintf(stderr, "Error: ");
+  vfprintf(stderr, format, args);
+  fprintf(stderr, "\n");
+  va_end(args);
+  exit(EXIT_FAILURE);
+}
+
+#define fail(...) do fail(__VA_ARGS__); while (true)
+
+/************************************************************************/
+
+typedef enum integer_kind {
+  signed_integer,
+  unsigned_integer
+} integer_kind;
+
+static const char *
+choose_integer_type(const size_t size, const integer_kind kind) {
+  switch (kind) {
+  case signed_integer:
+    if (sizeof(signed char) == size) return "signed char";
+    if (sizeof(short) == size) return "short";
+    if (sizeof(int) == size) return "int";
+    if (sizeof(long) == size) return "long";
+    if (sizeof(long long) == size) return "long long";
+    break;
+  case unsigned_integer:
+    if (sizeof(unsigned char) == size) return "unsigned char";
+    if (sizeof(unsigned short) == size) return "unsigned short";
+    if (sizeof(unsigned int) == size) return "unsigned int";
+    if (sizeof(unsigned long) == size) return "unsigned long";
+    if (sizeof(unsigned long long) == size) return "unsigned long long";
+    break;
+  }
+  fail("Couldn't find a %s type of %zd bytes.",
+       signed_integer == kind ? "signed" : "unsigned",
+       size);
+}
+
+/************************************************************************/
+
+static void
+print_header(void) {
+  printf("#ifndef CONFIG_H\n"
+         "#define CONFIG_H\n"
+         "\n"
+         "/* THIS FILE IS GENERATED.  DO NOT EDIT! */\n");
+}
+
+static void
+print_separator(const char *text) {
+  printf("\n/** <%s> *", text);
+  for (size_t width = strlen(text) + strlen("/** <> *"); width < 72; ++width)
+    printf("*");
+  printf("*/\n\n");
+}
+
+static void
+print_integer_type(const size_t size,
+                   const integer_kind kind,
+                   const char *name) {
+  printf("typedef %s %s;\n", choose_integer_type(size, kind), name);
+}
+
+static void
+print_footer(void) {
+  printf("\n"
+         "#endif\n");
+}
+
+/************************************************************************/
+
+#define INTEGER_TYPE(type)                      \
+print_integer_type(sizeof(type),                \
+                   (type)-1 < (type)0           \
+                   ? signed_integer             \
+                   : unsigned_integer,          \
+                   #type)
+
+int
+main(int argc, char *argv[]) {
+  print_header();
+
+  print_separator("stdbool.h");
+
+  INTEGER_TYPE(bool);
+
+  print_separator("stddef.h");
+
+  INTEGER_TYPE(size_t);
+  INTEGER_TYPE(ptrdiff_t);
+
+  print_separator("stdint.h");
+
+  INTEGER_TYPE(int8_t);
+  INTEGER_TYPE(int16_t);
+  INTEGER_TYPE(int32_t);
+  INTEGER_TYPE(int64_t);
+  INTEGER_TYPE(uint8_t);
+  INTEGER_TYPE(uint16_t);
+  INTEGER_TYPE(uint32_t);
+  INTEGER_TYPE(uint64_t);
+  INTEGER_TYPE(int_least8_t);
+  INTEGER_TYPE(int_least16_t);
+  INTEGER_TYPE(int_least32_t);
+  INTEGER_TYPE(int_least64_t);
+  INTEGER_TYPE(uint_least8_t);
+  INTEGER_TYPE(uint_least16_t);
+  INTEGER_TYPE(uint_least32_t);
+  INTEGER_TYPE(uint_least64_t);
+  INTEGER_TYPE(int_fast8_t);
+  INTEGER_TYPE(int_fast16_t);
+  INTEGER_TYPE(int_fast32_t);
+  INTEGER_TYPE(int_fast64_t);
+  INTEGER_TYPE(uint_fast8_t);
+  INTEGER_TYPE(uint_fast16_t);
+  INTEGER_TYPE(uint_fast32_t);
+  INTEGER_TYPE(uint_fast64_t);
+  INTEGER_TYPE(intptr_t);
+  INTEGER_TYPE(uintptr_t);
+  INTEGER_TYPE(intmax_t);
+  INTEGER_TYPE(uintmax_t);
+
+  print_separator("wchar.h");
+
+  INTEGER_TYPE(wchar_t);
+
+  print_footer();
+
+  return EXIT_SUCCESS;
+}


Property changes on: mltonlib/trunk/org/mlton/vesak/libc/unstable/detail/config-gen.c
___________________________________________________________________
Name: svn:eol-style
   + native

Added: mltonlib/trunk/org/mlton/vesak/libc/unstable/libc.mlb
===================================================================
--- mltonlib/trunk/org/mlton/vesak/libc/unstable/libc.mlb	2006-12-28 14:26:15 UTC (rev 4999)
+++ mltonlib/trunk/org/mlton/vesak/libc/unstable/libc.mlb	2006-12-28 18:36:31 UTC (rev 5000)
@@ -0,0 +1,7 @@
+(* Copyright (C) 2006 Vesa Karvonen
+ *
+ * This code is released under the MLton license, a BSD-style license.
+ * See the LICENSE file or http://mlton.org/License for details.
+ *)
+
+generated/$(TARGET_ARCH)-$(TARGET_OS)/libc.mlb


Property changes on: mltonlib/trunk/org/mlton/vesak/libc/unstable/libc.mlb
___________________________________________________________________
Name: svn:eol-style
   + native


Property changes on: mltonlib/trunk/org/mlton/vesak/libc/unstable/public
___________________________________________________________________
Name: svn:ignore
   + config-*-*.h


Added: mltonlib/trunk/org/mlton/vesak/libc/unstable/public/common.h
===================================================================
--- mltonlib/trunk/org/mlton/vesak/libc/unstable/public/common.h	2006-12-28 14:26:15 UTC (rev 4999)
+++ mltonlib/trunk/org/mlton/vesak/libc/unstable/public/common.h	2006-12-28 18:36:31 UTC (rev 5000)
@@ -0,0 +1,21 @@
+#ifndef COMMON_H
+#define COMMON_H
+
+/* Copyright (C) 2006 Vesa Karvonen
+ *
+ * This code is released under the MLton license, a BSD-style license.
+ * See the LICENSE file or http://mlton.org/License for details.
+ */
+
+#if !defined(TARGET_ARCH) || !defined(TARGET_OS)
+#error TARGET_ARCH and TARGET_OS must be defined
+#endif
+
+#define STRINGIFY(x) STRINGIFY_DELAY(x)
+#define STRINGIFY_DELAY(x) #x
+
+#include STRINGIFY(config-TARGET_ARCH-TARGET_OS.h)
+
+#define restrict /* mlnlffigen can't parse restrict ATM */
+
+#endif


Property changes on: mltonlib/trunk/org/mlton/vesak/libc/unstable/public/common.h
___________________________________________________________________
Name: svn:eol-style
   + native

Added: mltonlib/trunk/org/mlton/vesak/libc/unstable/public/stdio.h
===================================================================
--- mltonlib/trunk/org/mlton/vesak/libc/unstable/public/stdio.h	2006-12-28 14:26:15 UTC (rev 4999)
+++ mltonlib/trunk/org/mlton/vesak/libc/unstable/public/stdio.h	2006-12-28 18:36:31 UTC (rev 5000)
@@ -0,0 +1,80 @@
+#ifndef STDIO_H
+#define STDIO_H
+
+/* Copyright (C) 2006 Vesa Karvonen
+ *
+ * This code is released under the MLton license, a BSD-style license.
+ * See the LICENSE file or http://mlton.org/License for details.
+ */
+
+#include "common.h"
+
+typedef struct FILE FILE;
+typedef struct fpos_t fpos_t;
+
+extern const int EOF_;
+extern const int FILENAME_MAX_;
+extern const int FOPEN_MAX_;
+extern const int L_tmpnam_;
+extern const int SEEK_CUR_;
+extern const int SEEK_END_;
+extern const int SEEK_SET_;
+extern const int TMP_MAX_;
+extern const int _IOFBF_;
+extern const int _IOLBF_;
+extern const int _IONBF_;
+extern const size_t BUFSIZ_;
+
+FILE *stderr_(void);
+FILE *stdin_(void);
+FILE *stdout_(void);
+
+int remove(const char *filename);
+int rename(const char *old, const char *new);
+FILE *tmpfile(void);
+char *tmpnam(char *s);
+int fclose(FILE *stream);
+int fflush(FILE *stream);
+FILE *fopen(const char * restrict filename, const char * restrict mode);
+FILE *freopen(const char * restrict filename, const char * restrict mode,
+FILE * restrict stream);
+void setbuf(FILE * restrict stream, char * restrict buf);
+int setvbuf(FILE * restrict stream, char * restrict buf, int mode, size_t size);
+/* int fprintf(FILE * restrict stream, const char * restrict format, ...); */
+/* int fscanf(FILE * restrict stream, const char * restrict format, ...); */
+/* int printf(const char * restrict format, ...); */
+/* int scanf(const char * restrict format, ...); */
+/* int snprintf(char * restrict s, size_t n, const char * restrict format, ...); */
+/* int sprintf(char * restrict s, const char * restrict format, ...); */
+/* int sscanf(const char * restrict s, const char * restrict format, ...); */
+/* int vfprintf(FILE * restrict stream, const char * restrict format, va_list arg); */
+/* int vfscanf(FILE * restrict stream, const char * restrict format, va_list arg); */
+/* int vprintf(const char * restrict format, va_list arg); */
+/* int vscanf(const char * restrict format, va_list arg); */
+/* int vsnprintf(char * restrict s, size_t n, const char * restrict format, va_list arg); */
+/* int vsprintf(char * restrict s, const char * restrict format, va_list arg); */
+/* int vsscanf(const char * restrict s, const char * restrict format, va_list arg); */
+int fgetc(FILE *stream);
+char *fgets(char * restrict s, int n, FILE * restrict stream);
+int fputc(int c, FILE *stream);
+int fputs(const char * restrict s, FILE * restrict stream);
+int getc(FILE *stream);
+int getchar(void);
+char *gets(char *s);
+int putc(int c, FILE *stream);
+int putchar(int c);
+int puts(const char *s);
+int ungetc(int c, FILE *stream);
+size_t fread(void * restrict ptr, size_t size, size_t nmemb, FILE * restrict stream);
+size_t fwrite(const void * restrict ptr, size_t size, size_t nmemb, FILE * restrict stream);
+int fgetpos(FILE * restrict stream, fpos_t * restrict pos);
+int fseek(FILE *stream, long int offset, int whence);
+int fsetpos(FILE *stream, const fpos_t *pos);
+long int ftell(FILE *stream);
+void rewind(FILE *stream);
+void clearerr(FILE *stream);
+int feof(FILE *stream);
+int ferror(FILE *stream);
+void perror(const char *s);
+
+#endif


Property changes on: mltonlib/trunk/org/mlton/vesak/libc/unstable/public/stdio.h
___________________________________________________________________
Name: svn:eol-style
   + native




More information about the MLton-commit mailing list