From 69f262f6f48e4d47c176073a77f96dacb39c8b4f Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Thu, 29 Dec 2011 11:43:36 +0000 Subject: [PATCH] Add preprocessor. --- .gitignore | 3 ++ APIs/Makefile.am | 35 ++++++++++++++++++++ APIs/block_devices.api | 2 +- APIs/mkdir.api | 2 +- APIs/mknod.api | 9 ++---- APIs/processes.api | 2 +- Makefile.am | 9 ++++++ configure.ac | 15 +++++++-- generator/.depend | 0 generator/Makefile.am | 42 ++++++++++++++++++++++++ lib/Makefile.am | 16 +++++++++ preprocessor/.depend | 4 +++ preprocessor/Makefile.am | 49 ++++++++++++++++++++++++++++ preprocessor/pa_wrap.ml | 77 ++++++++++++++++++++++++++++++++++++++++++++ preprocessor/wrappi_types.ml | 21 ++++++++++++ 15 files changed, 274 insertions(+), 12 deletions(-) create mode 100644 APIs/Makefile.am create mode 100644 generator/.depend create mode 100644 generator/Makefile.am create mode 100644 lib/Makefile.am create mode 100644 preprocessor/.depend create mode 100644 preprocessor/Makefile.am create mode 100644 preprocessor/pa_wrap.ml create mode 100644 preprocessor/wrappi_types.ml diff --git a/.gitignore b/.gitignore index 41a0ade..892cb64 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,7 @@ *~ +*.cmi +*.cmo +*.cmx Makefile Makefile.in /aclocal.m4 diff --git a/APIs/Makefile.am b/APIs/Makefile.am new file mode 100644 index 0000000..deac8a9 --- /dev/null +++ b/APIs/Makefile.am @@ -0,0 +1,35 @@ +# wrappi +# Copyright (C) 2011 Red Hat Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program 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 this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +OCAMLCFLAGS = -g -warn-error CDEFLMPSUVYZX +OCAMLOPTFLAGS = $(OCAMLCFLAGS) + +PP = -pp '$(CAMLP4O) ../preprocessor/pa_wrap.cmo -impl' + +#API_FILES = $(wildcard *.api) +API_FILES = mkdir.api mknod.api + +OBJ_FILES = $(patsubst %.api,%.cmo,$(API_FILES)) + +noinst_SCRIPTS = $(OBJ_FILES) + +%.cmo: %.api + $(OCAMLFIND) ocamlc $(OCAMLOPTFLAGS) $(PP) -impl $< -c -o $@ + +CLEANFILES = *.cmi *.cmo *.cmx *~ + +SUFFIXES = .cmo .cmi .cmx .ml .mli .mll .mly .api diff --git a/APIs/block_devices.api b/APIs/block_devices.api index 8d4fc30..5b19993 100644 --- a/APIs/block_devices.api +++ b/APIs/block_devices.api @@ -1,4 +1,4 @@ -object block_device +system_object block_device dir_list "/sys/block/[hsv]d[a-z]*" property string name << return safe_strdup ($basename); >> diff --git a/APIs/mkdir.api b/APIs/mkdir.api index 2070c41..09e9527 100644 --- a/APIs/mkdir.api +++ b/APIs/mkdir.api @@ -1 +1 @@ -function err mkdir (pathname path, fileperm perm) (* implicit *);; +entry_point err mkdir (pathname path, fileperm perm) (* implicit *);; diff --git a/APIs/mknod.api b/APIs/mknod.api index 3ed3c55..ceedd78 100644 --- a/APIs/mknod.api +++ b/APIs/mknod.api @@ -1,13 +1,8 @@ -function -err mknod_char (pathname path, fileperm perm, int major, int minor) +entry_point +err mknod_char (pathname path, fileperm perm, int64 major, int64 minor) << return mknod (path, S_IFCHR | perm, makedev (major, minor)); >> -<:doc< - - - ->> ;; diff --git a/APIs/processes.api b/APIs/processes.api index 090245e..98fc527 100644 --- a/APIs/processes.api +++ b/APIs/processes.api @@ -1,4 +1,4 @@ -object process +system_object process dir_list "/proc/[1-9]*" constructor << diff --git a/Makefile.am b/Makefile.am index 300b8f8..8731aaf 100644 --- a/Makefile.am +++ b/Makefile.am @@ -16,3 +16,12 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ACLOCAL_AMFLAGS = -I m4 + +# The API files are actually linked into the generator, but an extra +# camlp4-based preprocessor must be compiled first before we can +# compile the rest of the generator. Hence the rather convoluted set +# of subdirectories here. +SUBDIRS = preprocessor APIs generator + +# The C library. +SUBDIRS += lib diff --git a/configure.ac b/configure.ac index f1c908b..21832b1 100644 --- a/configure.ac +++ b/configure.ac @@ -37,17 +37,28 @@ AM_PROG_CC_C_O dnl Check support for 64 bit file offsets. AC_SYS_LARGEFILE -dnl Check for OCaml compiler and camlp4. +dnl Check for OCaml compiler. AC_PROG_OCAML if test "$OCAMLC" = "no"; then AC_MSG_ERROR([You must install the OCaml compiler]) fi +dnl Camlp4 is required. AC_PROG_CAMLP4 if test "$CAMLP4" = "no"; then AC_MSG_ERROR([You must install camlp4 (the OCaml macro preprocessor)]) fi +dnl OCaml findlib ("ocamlfind") is required. +AC_PROG_FINDLIB +if test "$OCAMLFIND" = "no"; then + AC_MSG_ERROR([You must install OCaml findlib (the ocamlfind command)]) +fi + AC_CONFIG_HEADERS([config.h]) -AC_CONFIG_FILES([Makefile]) +AC_CONFIG_FILES([Makefile + APIs/Makefile + generator/Makefile + lib/Makefile + preprocessor/Makefile]) AC_OUTPUT diff --git a/generator/.depend b/generator/.depend new file mode 100644 index 0000000..e69de29 diff --git a/generator/Makefile.am b/generator/Makefile.am new file mode 100644 index 0000000..6cfc2c2 --- /dev/null +++ b/generator/Makefile.am @@ -0,0 +1,42 @@ +# wrappi +# Copyright (C) 2011 Red Hat Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program 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 this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +OCAMLCFLAGS = -g -warn-error CDEFLMPSUVYZX +OCAMLOPTFLAGS = $(OCAMLCFLAGS) + +%.cmi: %.mli + $(OCAMLFIND) ocamlc $(OCAMLCFLAGS) -c $< -o $@ +%.cmo: %.ml + $(OCAMLFIND) ocamlc $(OCAMLCFLAGS) -c $< -o $@ +%.cmx: %.ml + $(OCAMLFIND) ocamlopt $(OCAMLOPTFLAGS) -c $< -o $@ + +CLEANFILES = *.cmi *.cmo *.cmx *~ + +depend: .depend + +.depend: $(wildcard *.mli) $(wildcard *.ml) + rm -f $@ $@-t + $(OCAMLFIND) ocamldep $^ | \ + $(SED) -e 's/ *$$//' | \ + $(SED) -e :a -e '/ *\\$$/N; s/ *\\\n */ /; ta' | \ + LANG=C sort > $@-t + mv $@-t $@ + +include .depend + +SUFFIXES = .cmo .cmi .cmx .ml .mli .mll .mly diff --git a/lib/Makefile.am b/lib/Makefile.am new file mode 100644 index 0000000..5e79bf3 --- /dev/null +++ b/lib/Makefile.am @@ -0,0 +1,16 @@ +# wrappi +# Copyright (C) 2011 Red Hat Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program 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 this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. diff --git a/preprocessor/.depend b/preprocessor/.depend new file mode 100644 index 0000000..c44a80c --- /dev/null +++ b/preprocessor/.depend @@ -0,0 +1,4 @@ +pa_wrap.cmo: wrappi_types.cmo +pa_wrap.cmx: wrappi_types.cmx +wrappi_types.cmo: +wrappi_types.cmx: diff --git a/preprocessor/Makefile.am b/preprocessor/Makefile.am new file mode 100644 index 0000000..f0e8e93 --- /dev/null +++ b/preprocessor/Makefile.am @@ -0,0 +1,49 @@ +# wrappi +# Copyright (C) 2011 Red Hat Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program 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 this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +OCAMLCFLAGS = -g -warn-error CDEFLMPSUVYZX +OCAMLOPTFLAGS = $(OCAMLCFLAGS) + +noinst_SCRIPTS = pa_wrap.cmo + +pa_wrap.cmo: pa_wrap.ml wrappi_types.cmo + $(OCAMLFIND) ocamlc -I +camlp4 dynlink.cma camlp4lib.cma \ + -pp $(CAMLP4OF) \ + wrappi_types.cmo -c $< -o $@ + +%.cmi: %.mli + $(OCAMLFIND) ocamlc $(OCAMLCFLAGS) -c $< -o $@ +%.cmo: %.ml + $(OCAMLFIND) ocamlc $(OCAMLCFLAGS) -c $< -o $@ +%.cmx: %.ml + $(OCAMLFIND) ocamlopt $(OCAMLOPTFLAGS) -c $< -o $@ + +CLEANFILES = *.cmi *.cmo *.cmx *~ + +depend: .depend + +.depend: $(wildcard *.mli) $(wildcard *.ml) + rm -f $@ $@-t + $(OCAMLFIND) ocamldep -pp $(CAMLP4OF) $^ | \ + $(SED) -e 's/ *$$//' | \ + $(SED) -e :a -e '/ *\\$$/N; s/ *\\\n */ /; ta' | \ + LANG=C sort > $@-t + mv $@-t $@ + +include .depend + +SUFFIXES = .cmo .cmi .cmx .ml .mli .mll .mly diff --git a/preprocessor/pa_wrap.ml b/preprocessor/pa_wrap.ml new file mode 100644 index 0000000..c8e8af8 --- /dev/null +++ b/preprocessor/pa_wrap.ml @@ -0,0 +1,77 @@ +(* wrappi + * Copyright (C) 2011 Red Hat Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + *) + +(* For general information about camlp4, see: + * http://brion.inria.fr/gallium/index.php/Camlp4 + * For information about quotations, see: + * http://brion.inria.fr/gallium/index.php/Quotation + *) + +open Camlp4.PreCast +open Syntax +open Ast + +open Wrappi_types + +let add_entry_point _loc name parameters return_type code = + (* XXX *) + <:str_item< >> + +let () = + (* Quotation expander for C code. *) + let c_quotation_expander _loc _ code = + (* XXX Expand %- or $- expressions in code. *) + ExStr (_loc, code) + in + Quotation.add "c" Quotation.DynAst.expr_tag c_quotation_expander; + + (* Default quotation expander (<< .. >>) should be C code ("c"). *) + Quotation.default := "c" + +;; + +(* Extend the regular OCaml grammar. *) +EXTEND Gram + GLOBAL: str_item; + + (* A parameter or return type. *) + any_type: [ + [ "int32" -> TInt32 ] + | [ "int64" -> TInt64 ] + | [ t = LIDENT -> Type t ] + ]; + + (* A return type. *) + return_type: [ + [ "err" -> RErr ] + | [ t = any_type -> Return t ] + ]; + + (* A single function parameter. *) + parameter: [[ t = any_type; name = LIDENT -> (t, name) ]]; + + str_item: LEVEL "top" [ + [ "entry_point"; + return_type = return_type; name = LIDENT; + "("; parameters = LIST0 parameter SEP ","; ")"; + code = OPT [ code = expr -> code ] -> + add_entry_point _loc name parameters return_type code + ] + ]; + +END diff --git a/preprocessor/wrappi_types.ml b/preprocessor/wrappi_types.ml new file mode 100644 index 0000000..3f3ca58 --- /dev/null +++ b/preprocessor/wrappi_types.ml @@ -0,0 +1,21 @@ +(* wrappi + * Copyright (C) 2011 Red Hat Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + *) + +type any_type = TInt32 | TInt64 | Type of string + +type return_type = RErr | Return of any_type -- 1.8.3.1