[m-rev.] for review: module initialisation for erlang

Julien Fischer juliensf at csse.unimelb.edu.au
Thu Jun 7 13:51:19 AEST 2007


On Thu, 7 Jun 2007, Peter Wang wrote:

> Estimated hours taken: 10
> Branches: main
>
> Support :- initialise and :- finalise predicates in the Erlang backend.
>
> compiler/compile_target_code.m:
> 	Refactor the code relating to running mkinit to generate .init and
> 	_init.c files, and compiling _init.c files.
>
> 	Generalise the code so that it can call mkinit_erl with the right
> 	options.
>
> compiler/dead_proc_elim.m:
> 	Add initialisation and finalisation predicates to the queue at the
> 	start of the pass so that they won't be eliminated.  For the C
> 	backends we implicitly :- foreign_export init and final preds so they
> 	would never be eliminated, but we don't do that in the Erlang
> 	backend.
>
> compiler/elds.m:
> 	Extend the ELDS to hold pred_proc_ids of init/final preds.
>
> compiler/erl_code_gen.m:
> compiler/mercury_compile.m:
> 	Conform to change in ELDS.
>
> compiler/elds_to_erlang.m:
> 	Output mercury__required_init and mercury__required_final functions
> 	which call user initialisation and finalisation predicates, if any.
>
> 	Write out -export annotations for those functions.
>
> 	Write out REQUIRED_INIT and REQUIRED_FINAL directives for those
> 	functions.
>
> compiler/hlds_module.m:
> compiler/make_hlds_passes.m:
> 	Remember the arity of init/final preds in the HLDS, not just the
> 	sym_name.
>
> 	Add predicates to return the pred_proc_ids of init/final preds.
>
> 	Delete module_info_user_init_pred_c_name and
> 	module_info_user_final_pred_c_name as they are unused.
>
> compiler/make.program_target.m:
> 	Call make_erlang_program_init_file and make_erlang_library_init_file
> 	when building an Erlang executable or library, respectively.
>
> 	Install a .init file when installing an Erlang library.
>
> 	Conform to changes in compile_target_code.m.
>
> compiler/modules.m:
> 	In module_name_to_file_name, treat extensions specially if they end
> 	".erl" and ".beam", not only if they are exactly those strings.  This
> 	is because we may pass "_init.erl" or "_init.beam" as the extension.
>
> compiler/options.m:
> 	Add an option `--mkinit-erl-command'.
>
> util/Mmakefile:
> util/.cvsignore:
> util/mkinit_erl.c:
> 	Add `mkinit_erl' program which is a modified version of `mkinit'.
>
> util/mkinit.c:
> 	Make `mkinit' accept and ignore the `-m' option, which is needed by
> 	`mkinit_erl'.
>
> tests/hard_coded/Mmakefile:
> 	Fix misspellings of "MERCURY_SUPPRESS_STACK_TRACE".
>
> tests/hard_coded/impure_init_and_final.m:
> 	Add Erlang foreign proc.

...

> Index: util/mkinit.c
> ===================================================================
> RCS file: /home/mercury/mercury1/repository/mercury/util/mkinit.c,v
> retrieving revision 1.115
> diff -u -r1.115 mkinit.c
> --- util/mkinit.c	9 Feb 2007 04:05:18 -0000	1.115
> +++ util/mkinit.c	7 Jun 2007 02:41:39 -0000
> @@ -31,6 +31,7 @@
> **      - compiler/compile_target_code.m
> **          in particular the predicates make_init_obj/7 and
> **          make_standalone_interface/3.
> +**      - util/mkinit_erl.c
> **
> */
>
> @@ -784,7 +785,11 @@
>     int         i;
>     String_List *tmp_slist;
>
> -    while ((c = getopt(argc, argv, "A:c:g:iI:lo:r:tw:xX:ks")) != EOF) {
> +    /*
> +    ** The set of options for mkinit and mkinit_erl should be
> +    ** kept in sync, even if they may not necessarily make sense.
> +    */

Why?  The compiler shouldn't be invoking mkinit_erl (mkinit) with
options that don't make sense.

> +    while ((c = getopt(argc, argv, "A:c:g:iI:lm:o:r:tw:xX:ks")) != EOF) {
>         switch (c) {
>         case 'A':
>             /*
> @@ -834,6 +839,10 @@
>             output_main_func = MR_FALSE;
>             break;
>
> +        case 'm':
> +            /* Ignored: used by mkinit_erl. */
> +            break;
> +
>         case 'o':
>             if (strcmp(optarg, "-") == 0) {
>                 output_file_name = NULL; /* output to stdout */
> @@ -912,6 +921,7 @@
>     fputs("  -I dir:\tadd dir to the search path for init files\n", stderr);
>     fputs("  -k:\t\tgenerate the .init for a library\n", stderr);
>     fputs("  -s:\t\tgenerate a standalone runtime interface\n", stderr);
> +    fputs("  -m:\t\t(ignored)\n", stderr);
>     exit(EXIT_FAILURE);
> }
>
> Index: util/mkinit_erl.c
> ===================================================================
> RCS file: util/mkinit_erl.c
> diff -N util/mkinit_erl.c
> --- /dev/null	1 Jan 1970 00:00:00 -0000
> +++ util/mkinit_erl.c	7 Jun 2007 02:41:39 -0000
> @@ -0,0 +1,955 @@
> +/*
> +** vim:sw=4 ts=4 expandtab
> +*/
> +/*
> +** Copyright (C) 1995-2007 The University of Melbourne.
> +** This file may only be copied under the terms of the GNU General
> +** Public License - see the file COPYING in the Mercury distribution.
> +*/
> +
> +/*
> +** File: mkinit_erl.c
> +** Main authors: zs, fjh, wangp
> +**
> +** Given a list of .erl or .init files on the command line, this program
> +** produces the initialization file (usually called *_init.erl) on stdout.
> +** The initialization file is a small program that calls the initialization
> +** functions for all the modules in a Mercury program.
> +**
> +** Alternatively, if invoked with the -k option, this program produces a
> +** list of intialization directives on stdout.  This mode of operation is
> +** is used when building .init files for libraries.
> +**
> +** If invoked with the -s option, this program produces a standalone
> +** runtime interface on stdout.  This mode of operation is used when
> +** using Mercury libraries from applications written in foreign languages.
> +**
> +** NOTE: any changes to this program may need to be reflected in the
> +** following places:
> +**
> +**      - scripts/c2init.in
> +**      - compiler/compile_target_code.m
> +**          in particular the predicates make_init_obj/7 and
> +**          make_standalone_interface/3.
> +**      - util/mkinit.c

The code that is common to mkinit.c mkinit_erl could (and should) be
factored out and placed in a separate source file.

...

> +/* mercury_std.h includes mercury_regs.h, and must precede system headers */
> +#include    "mercury_conf.h"
> +#include    "mercury_std.h"
> +#include    "getopt.h"
> +#include    "mercury_array_macros.h"
> +
> +/*
> +** mercury_array_macros.h uses the MR_NEW_ARRAY and MR_RESIZE_ARRAY macros.
> +*/
> +
> +#define MR_NEW_ARRAY(type, num) \
> +        ((type *) malloc((num) * sizeof(type)))
> +
> +#define MR_RESIZE_ARRAY(ptr, type, num) \
> +        ((type *) realloc((ptr), (num) * sizeof(type)))
> +
> +
> +#include    <stdio.h>
> +#include    <stdlib.h>
> +#include    <string.h>
> +#include    <ctype.h>
> +#include    <errno.h>
> +
> +#ifdef MR_HAVE_SYS_STAT_H
> +  #include  <sys/stat.h>
> +#endif
> +
> +#ifdef MR_HAVE_UNISTD_H
> +  #include  <unistd.h>
> +#endif
> +
> +/* --- adjustable limits --- */
> +#define MAXLINE     256 /* maximum number of characters per line */
> +                        /* (characters after this limit are ignored) */
> +
> +/* --- used to collect a list of strings --- */
> +
> +typedef struct String_List_struct {
> +    char                        *data;
> +    struct String_List_struct   *next;
> +} String_List;
> +
> +typedef enum
> +{
> +    TASK_OUTPUT_INIT_PROG = 0,
> +    TASK_OUTPUT_LIB_INIT  = 1
> +} Task;
> +
> +/*
> +** Most of these won't apply to the Erlang backend, but let's not introduce
> +** unnecessary differences with mkinit.
> +*/

I disagree, they're different programs anyway and mkinit_erl would be
considerably simpler if you removed all the C specific stuff
(particulary if you factor out any common code as well.)

> +typedef enum
> +{
> +    PURPOSE_INIT = 0,
> +    PURPOSE_TYPE_TABLE = 1,
> +    PURPOSE_DEBUGGER = 2,
> +    PURPOSE_COMPLEXITY = 3,
> +    PURPOSE_PROC_STATIC = 4,
> +    PURPOSE_REQ_INIT = 5,
> +    PURPOSE_REQ_FINAL = 6
> +} Purpose;

Options 1, 2, 3 and 4 won't be used by the erlang backend.
Likewise elsewhere.

The rest of the diff looks fine.  Please bootcheck in asm_fast.gc before
committing - to make sure that shifting the code in compile_target_code.m
hasn't introduced bugs into the build system.

Julien.
--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to:       mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions:          mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the reviews mailing list