Commit Graph

102 Commits

Author SHA1 Message Date
Peter Ross
77a1261d3b Merge the foreign_type pragma changes from the dotnet branch to the main
Estimated hours taken: 10
Branches: main

Merge the foreign_type pragma changes from the dotnet branch to the main
branch, plus do some more development work to generalise the change.

compiler/prog_data.m:
    Add a type to hold the data from parsing a pragma foreign_type decl.

compiler/prog_io_pragma.m:
    Parse the pragma foreign_type.  This code is currently commented
    out, while we decide on the syntax.

compiler/hlds_data.m:
    Add a new alternative to hlds_type_body where the body of the type
    is a foreign type.

compiler/make_hlds.m:
    Place the foreign_type pragmas into the HLDS.

compiler/foreign.m:
    Implement to_type_string which replaces export__type_to_type_string,
    unlike export__type_to_type_string foreign__to_type_string takes an
    argument specifying which language the representation is meant to be
    in.  to_type_string also needs to take a module_info to handle
    foreign_types correctly.  To avoid the need for the module_info to
    be passed around the MLDS backend we provide a new type
    exported_type which provides enough information for an alternate
    version of to_type_string to be called.

compiler/export.m:
    Delete export__type_to_type_string.

compiler/llds.m:
    Since foreign__to_type_string needs a module_info, we add a new
    field to pragma_c_arg_decl which is the result of calling
    foreign__to_type_string.  This avoids threading the module_info
    around various llds passes.

compiler/mlds.m:
    Record with in the mercury_type the exported_type, this avoids
    passing the module_info around the MLDS backend.
    Also add the foreign_type alternative to mlds__type.
    Update mercury_type_to_mlds_type so that it handles types which are
    foreign types.

compiler/mlds_to_il.m:
    Convert a mlds__foreign_type into an ilds__type.

compiler/ilds.m:
    The CLR spec requires that System.Object and System.String be
    treated specially in the IL assembly so add them as simple types.

compiler/ilasm.m:
    Before outputting a class name into the IL assembly check whether it
    it can be simplified to a builtin type, and if so output that name
    instead as required by the ECMA spec.
    Changes for the addition of string and object as simple types.

doc/reference_manual.texi:
    Document the new pragma, this is currently commented out because it
    refers to syntax that has not yet been finalised.

compiler/fact_table.m:
compiler/llds_out.m:
compiler/ml_code_gen.m:
compiler/ml_code_util.m:
compiler/ml_simplify_switch.m:
compiler/ml_switch_gen.m:
compiler/ml_unify_gen.m:
compiler/mlds_to_c.m:
compiler/mlds_to_csharp.m:
compiler/mlds_to_gcc.m:
compiler/mlds_to_java.m:
compiler/mlds_to_mcpp.m:
compiler/pragma_c_gen.m:
compiler/rtti_to_mlds.m:
    Changes to handle using foreign__to_type_string.

compiler/hlds_out.m:
compiler/intermod.m:
compiler/magic_util.m:
compiler/ml_type_gen.m:
compiler/recompilation_usage.m:
compiler/recompilation_version.m:
compiler/term_util.m:
compiler/type_ctor_info.m:
compiler/unify_proc.m:
    Changes to handle the new hlds_type_body.

compiler/mercury_to_mercury.m:
    Output the pragma foreign_type declaration.

compiler/module_qual.m:
    Qualify the pragma foreign_type declarations.

compiler/modules.m:
    Pragma foreign_type is allowed in the interface.
2001-10-24 13:34:41 +00:00
Tyson Dowd
8ec5c48d3a Fix semidet C# code to use SUCCESS_INDICATOR.
Estimated hours taken: 10
Branches: main

Fix semidet C# code to use SUCCESS_INDICATOR.

Represent mlds__native_bool_type as bool, not int32.

In the library, fix incorrect uses of MC++ to implement functions -- these
weren't being caught because they returned int32, and we assumed
this was a semidet return value, not an actual integer.

These incorrect uses were all buggy -- functions don't work in MC++.
The compiler is now catches cases where functions returning int are
implemented in MC++ -- but it won't catch cases where
IL's bool type is returned, but that should not be a such a problem.

compiler/ilasm.m:
	Handle the output of boolean integer constants.

compiler/ml_code_gen.m:
	Add SUCCESS_INDICATOR to the locals of the MLDS for semidet C# code,
	copy SUCCESS_INDICATOR into succeeded after the execution of the
	user's code.

compiler/mlds_to_csharp.m:
	Implement assignments (so we can copy SUCCESS_INDICATOR to
	succeeded).

compiler/mlds_to_il.m:
	Copy return values into SUCCESS_INDICATOR for forwarded
	predicates (it will then be copied into succeeded).
	This all gets optimized away in the generated IL anyway.

	Represent native_bool_type as bool, not int32.
	Check for MC++ functions returning bool (not int32).
	Output true and false as boolean constants for 1 and 0.

compiler/mlds_to_mcpp.m:
	Map bool to MR_Bool, not MR_Integer.

library/int.m:
	Implement much of the code in Mercury rather than C# or MC++.
	The more efficient C bindings remain, but for other backends we
	fall back on the Mercury bindings.

library/io.m:
	Implement io__get_stream_id as a pred in MC++.
	It is easier to keep this as MC++ at the moment as that is what
	the rest of the io.m code is implemented in (including the
	Mercury stream data structure).

library/std_util.m:
	Add some missing assignments to SUCCESS_INDICATOR.
2001-10-17 05:10:42 +00:00
Tyson Dowd
416ca83320 Merge changes to add attributes to the HLDS, MLDS and ILDS from the
Estimated hours taken: 2
Branches: main

Merge changes to add attributes to the HLDS, MLDS and ILDS from the
dotnet-foreign branch.  We don't merge the changes to add syntax for
attributes, as the syntax is still very experimental.

compiler/hlds_pred.m:
compiler/prog_data.m:
	Add attributes to the pred_info (they are a bit like markers,
	but are more than just boolean flags).

compiler/ilasm.m:
	Add custom attributes to appropriate positions (on assemblies,
	IL types and methods).

compiler/ml_code_gen.m:
compiler/ml_code_util.m:
compiler/ml_elim_nested.m:
compiler/ml_optimize.m:
compiler/ml_tailcall.m:
compiler/ml_type_gen.m:
compiler/ml_util.m:
compiler/mlds.m:
compiler/mlds_to_c.m:
compiler/mlds_to_csharp.m:
compiler/mlds_to_gcc.m:
compiler/mlds_to_java.m:
compiler/mlds_to_mcpp.m:
	Add mlds__attributes, which are the MLDS version of custom attributes.
	Convert hlds_pred__attributes into mlds__attributes.
	Add a list of mlds__attributes to the mlds__function defn.

compiler/mlds_to_il.m:
	Convert MLDS attributes to IL custom attributes.
2001-08-24 15:44:57 +00:00
Tyson Dowd
dfc7cd3ddf Implement pragma export on the .NET backend.
Estimated hours taken: 8
Branches: main

Implement pragma export on the .NET backend.

We just generate a forwarding call from the "exported" name (which
occurs in the same class and module) to the specified predicate.

compiler/ml_code_gen.m:
	Generate exports for IL as well as C.

compiler/mlds_to_il.m:
	Generate a forwarding function for each exported predicate.
	Put the forwarding function inside the wrapper class.
	Generate a more specific error message if an return statement
	has an empty list of return values.
2001-08-23 09:28:03 +00:00
Tyson Dowd
fca6396414 Address fjh's review comments regarding the fixes required to
Estimated hours taken: 1
Branches: main

compiler/ml_code_gen.m:
	Address fjh's review comments regarding the fixes required to
	get solutions working in the .NET backend.
2001-08-07 13:53:33 +00:00
Tyson Dowd
cdbbaa5ac2 Support multiple language foreign_procs in the one file.
Estimated hours taken: 16
Branches: main

Support multiple language foreign_procs in the one file.

If there is more then one applicable foreign_proc for a
given clause, select the most "preferred" programming
language to use as the implementation.

Currently only the IL backend has multiple languages
supported by the backend, and C# is preferred over MC++.

compiler/options.m:
compiler/handle_options.m:
	Rename backend_foreign_language as backend_foreign_languages,
	and use it to record the list of foreign languages the selected
	backend can handle.
	Remove --use-foreign-language as it doesn't do anything right now.

compiler/foreign.m:
	Update code to use the list of backend foreign languages.
	Add prefer_foreign_language function, to compute the preferred
	foreign language ordering for each backend.
	Move simple_foreign_language_string here from globals.m,
	and add foreign_language_file_extension and
	foreign_language_module_name functions.

	(much of the rest of the code in this module is intended to deal with
	the case where the backend *doesn't* handle the foreign
	language, but we don't have any working support for that at the
	moment).

compiler/globals.m:
	Add globals__io_get_backend_foreign_languages and
	globals__get_backend_foreign_languages.

compiler/make_hlds.m:
	Handle selection of foreign_proc code depending upon the preferred
	language.
	Rename a few *_foreign_code predicates as *_foreign_proc
	predicates.
	Handle the extra field in clause.

compiler/ml_code_gen.m:
compiler/mlds.m:
	Generate different mlds__foreign_code for each language (only
	one language is selected for each predicate, but there can be
	multiple languages in the onle module).
	and put them in a map which is indexed on foreign language.

compiler/modules.m:
	Generate appropriate dependencies for foreign language modules.
	We now record which languages a list of items uses (taking into
	account the preferred foreign language for foreign_proc).
	We also allow for the fact that some foreign_proc declarations
	won't generate any external modules.


compiler/hlds_pred.m:
	Add an extra field to clause which records which language this
	clause has been implemented in.

compiler/assertion.m:
compiler/clause_to_proc.m:
compiler/dead_proc_elim.m:
compiler/goal_util.m:
compiler/hlds_out.m:
compiler/inlining.m:
compiler/intermod.m:
compiler/modes.m:
compiler/polymorphism.m:
compiler/purity.m:
compiler/typecheck.m:
compiler/unify_proc.m:
	Handle the extra field in clause.


compiler/mlds_to_c.m:
compiler/mlds_to_csharp.m:
compiler/mlds_to_gcc.m:
compiler/mlds_to_mcpp.m:
	Select the appropriate mlds__foreign code from the map and
	generate code for it.
2001-07-20 14:14:30 +00:00
Tyson Dowd
b994075a22 Add support for foreign_proc("il", ....)
Estimated hours taken: 50
Branches: main


Add support for foreign_proc("il", ....)

To use it, you need to give the options
	--use-foreign-language il
	--backend-foreign-language il

Begin documenting foreign_proc.


compiler/foreign.m:
compiler/globals.m:
	Handle the addition of il as a language option.

compiler/inlining.m:
	Mention that we don't allow il code to be inlined yet and give a
	pointer into ml_code_gen.m which has the reason along with the
	offending code.

compiler/il_peephole.m:
compiler/ilasm.m:
	Handle the addition of il_asm_code as inlineable code.

compiler/ilds.m:
	Add a handwritten scope to the different scope types.

compiler/ml_code_gen.m:
	Handle the generation of code for IL foreign language interfacing.
	Put the max_stack_size attribute into IL foreign language code.
	Document the problem with inlining.

compiler/ml_elim_nested.m:
	Handle the new field in blocks indicating whether the block contains
	handwritten code.

compiler/mlds.m:
	Add lang_il as a possible target language.
	Add attributes to target code (max_stack_size is the only one so far).

compiler/mlds_to_c.m:
compiler/mlds_to_csharp.m:
compiler/mlds_to_java.m:
compiler/mlds_to_mcpp.m:
	Handle the addition of il as a language option.

compiler/mlds_to_il.m:
compiler/mlds_to_ilasm.m:
	Generate inline code for foreign_proc using IL.

compiler/prog_data.m:
	Add il as a language option.

	Add extra attributes to the pragma_foreign_proc_attributes.
	Currently there is just one extra attribute, max_stack_size.

compiler/prog_io_pragma.m:
	Parse max_stack_size as an attribute on foreign_proc.
	Improve error message output: previously we tried to parse the
	third term of a foreign_proc, and then tried to parse the second term
	(which we will accept for "c_code" but not foreign_proc).
	But we should give the error message as if the "c_code" handling is
	not present, as this will eventually go away.
	Check for foreign_language attributes such as max_stack_size.

doc/reference_manual.texi:
	Add a section for foreign_proc (and other multi-language foreign
	language interfacing pragmas).
	At the moment we have some reasonable documentation for "C" and
	"IL" foreign_proc, and some documentation on foreign_decl and
	foreign_code for C.

doc/user_guide.texi:
	The language and implementation specific documentation of
	the foreign language interface.
2001-07-18 10:21:03 +00:00
Peter Ross
e40f48eaed Get solutions working on the IL backend.
Estimated hours taken: 4
Branches: main

Get solutions working on the IL backend.

compiler/ml_code_gen.m:
    Avoid generating casts and assignments from MR_Box to MR_Word as
    these operations aren't type-safe on the IL backend.

compiler/mlds_to_il.m:
    Workaround a bug where private classes aren't accessible from
    classes in the same assembly when that assembly is created by
    al.exe.  The workaround is to make the class public.  The only
    assembly which is created by al.exe currently is the mercury std
    library.

library/std_util.m:
    Implement the MC++ code required for builtin__aggregate to work.
2001-07-16 09:49:41 +00:00
Fergus Henderson
6558f7b503 Improve the efficiency and maintainability of the way we handle
Estimated hours taken: 1
Branches: main

Improve the efficiency and maintainability of the way we handle
commits for the IL back-end.

compiler/ml_code_gen.m:
	Delete the IL-specific code for commit handling.
	This code belongs in mlds_to_il.m.

compiler/mlds_to_il.m:
	Handle do_commit instructions by just doing the equivalent of
	"throw new mercury::runtime::Commit();".  This is more efficient
	than the old code that we used to generate, which would assign
	the result of the newobj instruction to a variable or environment
	field, only to immediately get it back again.
2001-07-13 12:02:18 +00:00
Fergus Henderson
aa9dfe0339 Back out part of Tyson's earlier change (-r1.90) that was not
Estimated hours taken: 5
Branches: main

compiler/ml_code_gen.m:
	Back out part of Tyson's earlier change (-r1.90) that was not
	documented in the log message, since it breaks the IL back-end;
	`cd library; mmc --grade ilc ops.m' fails.
2001-07-12 23:58:38 +00:00
Tyson Dowd
bf0ed70782 Use a new mlds__function_body type to represent function bodies, as the old
Estimated hours taken: 0.75
Branches: main

Use a new mlds__function_body type to represent function bodies, as the old
usage of maybe/1 was error prone ("no" meant the function had been declared
using :- pragma external, not merely that the body was missing).

compiler/mlds.m:
	Add mlds__function_body type.

compiler/ml_code_gen.m:
compiler/ml_code_util.m:
compiler/ml_elim_nested.m:
compiler/ml_optimize.m:
compiler/ml_tailcall.m:
compiler/ml_util.m:
compiler/mlds_to_c.m:
compiler/mlds_to_csharp.m:
compiler/mlds_to_gcc.m:
compiler/mlds_to_il.m:
compiler/mlds_to_java.m:
compiler/mlds_to_mcpp.m:
	Handle this change.
2001-07-12 15:44:58 +00:00
Fergus Henderson
fd5d7f1150 Improve the comments about commit handling.
Estimated hours taken: 0.75
Branches: main

compiler/mlds.m:
compiler/ml_code_gen.m:
	Improve the comments about commit handling.
2001-07-11 13:52:35 +00:00
Tyson Dowd
fa63fc86b9 Fix the handling of commits in .NET.
Estimated hours taken: 1
Branches: main

Fix the handling of commits in .NET.

compiler/ml_code_gen.m:
	Initialize (new_object) the commit object just before we throw it.

compiler/ml_elim_nested.m:
	Don't initialize the commit object inside the constructor for the
	class -- this hard to maintain and may not always be correct.

compiler/mlds_to_il.m:
	Fix rval_to_type to correct get the type from var references, and
	handle box/unbox and cast unops.
2001-07-11 12:45:18 +00:00
Peter Ross
5c2103005e The .NET backend requires that names are not only qualified with their
Estimated hours taken: 4
Branches: main

The .NET backend requires that names are not only qualified with their
namespace but the source package the name comes from.  In this change we
back out a previous solution to this problem and implement a much
neater solution where we hide the package name in the abstract type
mlds_module_name.

This solution is neater because the package name shouldn't change once
the name is defined.  All that we may have to change is the qualifiers
to the name.

compiler/mlds.m:
    Add the package name to the abstract type mlds_module_name.

compiler/ml_call_gen.m:
compiler/ml_code_gen.m:
compiler/ml_code_util.m:
compiler/ml_elim_nested.m:
compiler/ml_optimize.m:
compiler/ml_tailcall.m:
compiler/ml_type_gen.m:
compiler/ml_unify_gen.m:
compiler/ml_util.m:
compiler/mlds_to_c.m:
compiler/mlds_to_csharp.m:
compiler/mlds_to_il.m:
compiler/mlds_to_java.m:
compiler/mlds_to_mcpp.m:
compiler/rtti_to_mlds.m:
    Back out previous solution.
2001-06-22 09:14:39 +00:00
Peter Ross
f092670758 The .NET backend requires that names are not only qualified with their
Estimated hours taken: 16
Branches: main

The .NET backend requires that names are not only qualified with their
namespace but the source package the name comes from.  In this change we
add to the name type the name of the source package which this name is
defined in.  This change will be needed for implementing foreign_class
in the .NET backend where it will no longer be possible to determine
the package name from the fully qualified name.

compiler/mlds.m:
    Add the new field to the mlds__fully_qualified_name type.

compiler/ml_call_gen.m:
compiler/ml_code_gen.m:
compiler/ml_code_util.m:
compiler/ml_elim_nested.m:
compiler/ml_optimize.m:
compiler/ml_tailcall.m:
compiler/ml_type_gen.m:
compiler/ml_unify_gen.m:
compiler/ml_util.m:
compiler/mlds_to_c.m:
compiler/mlds_to_csharp.m:
compiler/mlds_to_il.m:
compiler/mlds_to_java.m:
compiler/mlds_to_mcpp.m:
compiler/rtti_to_mlds.m:
    Propogate the changes around.
2001-06-08 09:13:45 +00:00
Fergus Henderson
45be173693 Fix a couple of minor mistakes in the comments.
Estimated hours taken: 0.1
Branches: main

compiler/ml_code_gen:
	Fix a couple of minor mistakes in the comments.
2001-05-26 17:27:41 +00:00
Tyson Dowd
cbdd5a68d5 Minimize the amount of procedure name mangling done by the .NET backend.
Estimated hours taken: 5
Branches: main, dotnet-foreign

Minimize the amount of procedure name mangling done by the .NET backend.

compiler/ml_code_gen.m:
compiler/ml_code_util.m:
compiler/ml_elim_nested.m:
compiler/ml_util.m:
compiler/mlds.m:
compiler/mlds_to_c.m:
compiler/mlds_to_gcc.m:
compiler/mlds_to_java.m:
compiler/rtti_to_mlds.m:
	Add the code_model and a boolean indicating whether this
	function (if it is a function) *doesn't* have a return value
	(i.e. it has a non-default mode).

	Also move is_output_det_function into ml_code_util.m and rename it
	ml_is_output_det_function.

compiler/mlds_to_il.m:
	Mangle much more carefully.  We still mangle in a predictable,
	context-insensitive manner, however we take advantage of the
	overloading available in the .NET backend to avoid name clashes
	in most cases.
2001-05-10 15:07:59 +00:00
Tyson Dowd
7663bd72a4 Generate foreign language code using MR_Bool instead of bool.
Estimated hours taken: 1
Branches: main, dotnet-foreign

compiler/ml_code_gen.m:
	Generate foreign language code using MR_Bool instead of bool.
	MC++ doesn't like us using "bool", and we should use the typedef
	in mercury_types.h instead anyway.
2001-05-09 11:30:56 +00:00
Fergus Henderson
91549e3095 Fix some incorrect indentation.
Estimated hours taken: 0.1
Branches: main

compiler/ml_code_gen.m:
	Fix some incorrect indentation.
2001-05-02 15:16:06 +00:00
Tyson Dowd
76ac44335d Implement a C# interface for the .NET backend.
Estimated hours taken: 45
Branches: main

Implement a C# interface for the .NET backend.

To use it, you currently need to set
	--backend-foreign-language csharp --use-foreign-language csharp
in your MCFLAGS.

The C# foreign language interface works by introducing a new sort of
MLDS statement called outline_foreign_proc.  outline_foreign_proc is expected
to be turned into a separate procedure in a separate file.  This is
quite different to normal foreign code which has been renamed as inline
target code, as it is really intended to be generated inline, inside the
generated code.

Because outline_foreign_proc is expected to be generated outside the
normal code, we don't need to generate variable renamings,
initializations, casts and other complicated interfacing code.

Any marshalling is done by the backend, which knows how to marshall
arguments across the boundary into the outline code and back.  In the
case of marshalling to C# from the .NET backend, we currently don't do
anything special (part of the point of .NET is that data
representation don't have to change very often just because you are
using different languages, so this is a property we should try to
preserve).

The actual implementation of the foreign code is therefore very simple.
Simply generate an appropriate procedure, and insert the user's code in
the middle.

The bulk of this change to delay the mangling of MLDS var names, so we
can still use the original user's var name when we output the outline
procedure (since the user's foreign code will refer to these var names,
it's important to keep them around).

compiler/foreign.m:
	Handle the csharp foreign language.

compiler/globals.m:
	Fix an XXX about converting to lowercase to do language name
	comparisons.
	Add new predicates to make conversion of foreign languages
	to strings more uniform.

compiler/handle_options.m:
	Don't set backend_foreign_language to the default if it has
	already been set by hand.

compiler/ml_call_gen.m:
compiler/ml_code_gen.m:
compiler/ml_code_util.m:
	Delay the mangling of MLDS var names by keeping the variable
	number around until the output phase.

	Slightly generalize the handling of foreign language interfacing.
	Handle C# foreign language interfacing.

	Add value_output_vars to the ml_gen_info, which are the variables
	returned rather than passed by reference.  We need to know
	these variables for C# interfacing so that we can handle the return
	value of the forwarding function.

	Mark the beginning and end of the MLDS foreign language processing as
	a "sub-module" (in comments at least).  Later I may put this code
	into a separate module.

	Rename some predicates from c_code to foreign_code.

compiler/ml_elim_nested.m:
compiler/ml_optimize.m:
compiler/ml_string_switch.m:
compiler/ml_type_gen.m:
compiler/ml_unify_gen.m:
compiler/ml_util.m:
compiler/rtti_to_mlds.m:
	Handle the new var_name type, and the new target_code constructors.

compiler/mlds.m:
	Add outline_foreign_proc which is handled differently to the old
	target_code (which has been renamed inline_target_code).

	Change the definiton for mlds__var_name.

compiler/mlds_to_c.m:
	Factor out mlds_output_to_file.
	Handle the new var_name type, and the new target_code constructors.

compiler/mlds_to_csharp.m:
	A new module to generate C# code suitable for foreign language
	interfacing.  This is largely lifted from the MC++ code, with a few
	changes to the output syntax.

compiler/mlds_to_il.m:
	Return the set of foreign languages processed instead of a bool
	saying wither MC++ was present.  This is so we can generate the
	appropriate output .cs or .cpp files, and because we need to keep
	track of all the external assembly references we need to put in the
	.il file.

	Handle the inline_target_code and mlds__var_name changes.

compiler/mlds_to_ilasm.m:
	Output .cpp and .cs files conditionally.
	Factor out output_to_file.
	Move MC++ output code to mlds_to_mcpp.m

compiler/mlds_to_java.m:
	Factor out output_to_file.
	Handle the new var_name type, and the new target_code constructors.

compiler/mlds_to_mcpp.m:
	New file to handle generating MC++ code suitable for foreign language
	interfacing.

compiler/options.m:
	Add a way of setting the backend-foreign-language option.

compiler/passes_aux.m:
	Add output_to_file which is used by the MLDS backend to generate
	output files.

compiler/prog_data.m:
	Uncomment csharp as a foreign language.
2001-05-02 11:36:41 +00:00
Fergus Henderson
254f556e63 Update the TODO list: trailing support for the MLDS back-end
Estimated hours taken: 0.1
Branches: main

compiler/ml_code_gen.m:
	Update the TODO list: trailing support for the MLDS back-end
	was added a while back.
2001-04-18 15:15:25 +00:00
Fergus Henderson
f305687202 Fix a bug that occaisionally caused some type errors in the
Estimated hours taken: 2
Branches: main, release

compiler/ml_code_gen.m:
	Fix a bug that occaisionally caused some type errors in the
	generated code for --high-level-code grades (gcc was just issuing
	a warning, but lcc was reporting an error message).  The bug was
	that we were using the wrong type when determining whether or
	not a cast was needed for some code emitted for `pragma c_code'
	procedures -- the type we need to use is the variable's original
	type, not the type after inlining (which can be an instance
	of the original type, if the original type is polymorphic).
2001-04-17 14:08:57 +00:00
Adrian Pellas-Rice
1c65d003f7 Add the shorthand_goal_expr wrapper type to ease hlds->hlds transformations.
Estimated hours taken: 4.5
Branches: main

Add the shorthand_goal_expr wrapper type to ease hlds->hlds transformations.

compiler/hlds_goal.m
        Create a new type, the `shorthand_goal_expr', for goals kinds that
        are implemented by a (ordinary_hlds + shorthand) -> (ordinary_hlds)
        transformation.  At present, bi_implication is the only kind of
        of goal that is implemented in this way.

        Moved bi_implication functor from the type goal_expr to the new
        shorthand_goal_expr type.

        Added the functor shorthand to the goal_expr type.

compiler/*.m
        Change switches on hlds_goal_expr that call error when they recognise
        `bi_implication' from calling error when they recognise
        `bi_implication' to calling error when they recognise `shorthand'.

        For all predicates K that
                a) switch on hlds_goal_expr and
                b) perform non-trivial processing when they recognise
                   `bi_implication'
        change K such that it now calls K_shorthand upon recognising the
        functor `shorthand'. Define K_shorthand to switch on
        shorthand_goal_expr, where the code for the `bi_implication' case
        formerly contained in K is now contained in K_shorthand.
2001-04-07 14:05:03 +00:00
Tyson Dowd
711da78188 Rename foreign_code as foreign_proc where appropriate in the compiler.
Estimated hours taken: 4.0
Branches: main

Rename foreign_code as foreign_proc where appropriate in the compiler.
The rationale for this change is that it makes maintaining the code much
simpler because it is clear whether `foreign' refers to a slab of code
(foreign_code) or a procedure (foreign_proc).

:- type pragma_foreign_code_attributes
:- type pragma_foreign_proc_attributes

The functors for pragma_type
	foreign(Lang, BodyCode)
	foreign(Attributes, Name, PredOrFunc, Vars, Varset, Impl)
become
	foreign_code(Lang, BodyCode)
	foreign_proc(Attributes, Name, PredOrFunc, Vars, Varset, Impl)

And the HLDS goal `pragma_foreign_code' becomes `foreign_proc'.

compiler/*.m:
	Update the compiler to use the new names.
2001-04-03 03:20:33 +00:00
Fergus Henderson
bcad0c81c8 Fix a bug that broke tabling for the GCC back-end.
Estimated hours taken: 1

Fix a bug that broke tabling for the GCC back-end.

compiler/mlds.m:
	Document that `one_copy' (static) variables
	must have initializers.

compiler/ml_code_gen.m:
	Explicitly initialize the tabling_pointer variables to null.
2001-02-28 15:59:18 +00:00
Fergus Henderson
77b6a155cc Simplify the code for ml_gen_proc_defn a bit by moving some
Estimated hours taken: 0.5

compiler/ml_code_gen.m:
	Simplify the code for ml_gen_proc_defn a bit by moving some
	code into a new subroutine ml_det_copy_out_vars.
2001-02-28 12:54:28 +00:00
Julien Fischer
00d8243570 General improvements and bug fixes to the MLDS backend, most
Estimated hours taken: 20

General improvements and bug fixes to the MLDS backend, most
of which were prompted by working on the Java backend.

The definition of mlds__lval now includes type information for
variables.  This is necessary because if enumerations are treated
as objects (as in the Java backend) rather than integers we need to know
when to create new objects.  At the level this occurs there was
previously no way to distinguish between an integer that is an integer,
and one that represents an enumeration.

Added the access specifier `local' to the declaration flags.  This fixes
a bug in which the local variables of a function were being declared
`private'.

Redefined ctor_name so that they are fully qualified.  This was necessary
because the Java backend represents discriminated unions as nested
classes and we need to be able to determine the fully qualified name of
the constructor in order to call it, do casts, etc.

Added `mlds__unknown_type' to `mlds__type'.  This is due to the change
in the definition of mlds_lval above.  In ml_code_util.m, env_ptr's are
created as dangling references.  The new definition of mlds__lval expects
there to be a type as well, but at this point it hasn't been
generated (and won't be until the ml_elim_nested pass).  Rather than just
guess the type we should declare the type to be unknown and print out an
error message if an unknown type makes it through to one of the backends.

Fixed a bug in the `--det-copy-out' option.

Shifted code for detecting entry point to main/2 from mercury_compile.m
to ml_util.m

compiler/mercury_compile.m:
compiler/ml_util.m:
	Shifted code for detecting entry point to main/2 from mercury_compile.m
	to ml_util.m
compiler/mlds.m:
	Added `local' as an access specifier.
	Extended definition of mlds__lval to include type information
	for variables.
	Added `mlds__unknown_type' to the mlds types so that when
	the compiler generates variables without yet knowing their
	type we can mark them as this, rather than hoping that the
	correct types eventually get added.
	Redefined ctor_name so that it is fully qualified.
	Made changes to comments to reflect above changes.

compiler/ml_code_gen.m:
	Mark the generated functions as `one_copy' rather than `per_instance',
	so that they get generated as static methods for the Java back-end.
	Fixed a bug with the --det-copy-out option.

compiler/ml_code_util.m:
	Fixed a bug that was causing the wrong declaration flags to be
	set for fields in du constructors.
	Changed the name of the predicate `ml_qualify_var' to
	`ml_gen_var_lval'.

compiler/ml_type_gen.m:
	Mark the generated types as `one_copy' rather than `per_instance',
	so that they get generated as static nested classes for the Java
	back-end.
	Changed comments to reflect that classes and enumeration constants
	should be static.
	Export functions that generate declaration flags because they
	are used in other modules as well.
	Added a new predicate `ml_gen_mlds_field_decl' that correctly
	generates fields of classes in discriminated unions.

compiler/ml_unify_gen.m:
	Changed the code that generates ctor_id's so that it generates
	the new sort.

compiler/ml_call_gen.m:
compiler/ml_elim_nested.m:
compiler/ml_optimize.m:
compiler/ml_string_switch.m:
compiler/ml_tailcall.m:
compiler/mlds_to_il.m:
compiler/mlds_to_c.m:
compiler/mlds_to_gcc.m:
compiler/mlds_to_ilasm.m:
compiler/rtti_to_mlds.m:
	Fixed things so that they conform to the changes above.
2001-02-20 07:52:19 +00:00
Fergus Henderson
db238abc24 Fix a bug which caused an internal compiler error when compiling
Estimated hours taken: 4

Fix a bug which caused an internal compiler error when compiling
tests/hard_coded/cut_test.m with `-O5 --high-level-code'.

compiler/simplify.m:
	Document that simplify does NOT ensure that conditions
	of if-then-elses don't have determinism `det' or `failure'.

compiler/ml_code_gen.m:
	Handle if-then-elses with `det' conditions.
2001-02-03 22:39:28 +00:00
Fergus Henderson
247075bd3b Fix a bug that broke extras/trailed_update/samples/vqueens.m
Estimated hours taken: 3

Fix a bug that broke extras/trailed_update/samples/vqueens.m
when compiled with `-H -O4'.

compiler/ml_code_gen.m:
	Export ml_gen_wrap_goal, for use by ml_unify_gen.m.

compiler/ml_unify_gen.m:
	Handle deconstruct unifications where the goal_info determinism
	doesn't match the can_fail field of the deconstruct by calling
	ml_gen_wrap_goal, rather than aborting.
2001-01-23 06:01:05 +00:00
Fergus Henderson
452d9b27de Some efficiency improvements to the MLDS->C back-end.
Estimated hours taken: 8

Some efficiency improvements to the MLDS->C back-end.
In particular, use GCC's __builtin_setjmp/__builtin_longjmp(),
and generate better code for switches whose default case is
unreachable.

These changes also help to ensure that the MLDS->C back-end should
generate exactly the same assembler code as the MLDS->GCC back-end,
which makes it easier to debug the latter (by using `diff' on the
generated `.s' files).

compiler/mlds_to_c.m:
	For unreachable default cases, use `MR_assert(0)' rather than
	`assert(0)'.  This improves efficiency, since `MR_assert' is
	disabled unless you compile with -DMR_LOWLEVEL_DEBUG.
	These checks were useful in debugging the MLDS back-end, but
	it's probably not worth keeping them enabled by default now.

	Put the default case first, so that if it is empty (as it will
	be if the default is unreachable and MR_assert() is not enabled),
	it gets merged with.

	Replace an obsolete comment about a problem with setjmp() and
	volatile with a comment explaining how that problem was fixed.

	Generate calls to MR_builtin_setjmp() and MR_builtin_longjmp()
	rather than to setjmp() and longjmp().

runtime/mercury.h:
	Define MR_builtin_setjmp() and MR_builtin_longjmp().
	These expand to __builtin_setjmp()/__builtin_longjmp() for GNU C
	and to the standard setjmp()/longjmp() otherwise.

compiler/mlds.m:
compiler/ml_code_gen.m:
	Add some comments about __builtin_setjmp() and __builtin_longjmp().
2001-01-17 17:37:24 +00:00
Fergus Henderson
f1bfe5b6c5 Fix a bug in my previous change (I had the two arguments
Estimated hours taken: 0.5

compiler/ml_code_gen.m:
	Fix a bug in my previous change (I had the two arguments
	to unsafe_type_cast in the wrong order).
2000-12-14 08:02:18 +00:00
Fergus Henderson
0df51740ff Fix a problem with unsafe_type_cast that stopped the
Estimated hours taken: 2

Fix a problem with unsafe_type_cast that stopped the
compiler from bootstrapping in MLDS grades.

compiler/builtin_ops.m:
	Add a comment about unsafe_type_cast.

compiler/ml_code_gen.m:
	Generate code for unsafe_type_cast as an inline builtin.

compiler/mlds_to_c.m:
	Delete a hack that was previously needed for unsafe_type_cast.

library/private_builtin.m:
	Delete the `:- external' declaration for unsafe_type_cast;
	this is not needed, and could cause problems because it
	suppresses the definition for unsafe_type_cast,
	which could cause problems if you take its address.

runtime/mercury.h:
	Delete the definition of unsafe_type_cast, since it is
	no longer needed.
2000-12-14 02:04:59 +00:00
Fergus Henderson
ef91da404e Fix the handling of procedures declared `external' for the MLDS back-end.
Estimated hours taken: 3

Fix the handling of procedures declared `external' for the MLDS back-end.
This change fixes some problems that broke many of the test cases in
tests/valid in the hl*prof* grades.

compiler/hlds_pred.m:
	Add `external' as a new import_status, rather than using
	`imported'.

compiler/assertion.m:
compiler/higher_order.m:
compiler/hlds_out.m:
compiler/hlds_pred.m:
compiler/intermod.m:
compiler/make_hlds.m:
	Minor changes to handle the new import_status.

compiler/mlds.m:
	Add a comment about the handling of procedures declared
	`:- external'.

compiler/ml_code_gen.m:
	Generate MLDS function definitions, with no function body,
	for procedures declared `external'.

compiler/mlds_to_c.m:
	Declare private functions with no function body as `extern'
	rather than `static'.
2000-12-11 04:52:46 +00:00
Fergus Henderson
4e44dfb287 Fix a bug with the handling of commits in the high-level C back-end.
Estimated hours taken: 12

Fix a bug with the handling of commits in the high-level C back-end.

It was implementing commits via calls to setjmp()/longjmp(),
but ANSI/ISO C says that longjmp() is allowed to clobber the values
of any non-volatile local variables in the function that called setjmp()
which have been modified between the setjmp() and the longjmp().

The fix is that whenever we generate a commit, we put it in its own
nested function, with the local variables (e.g. `succeeded',
plus any outputs from the goal that we're committing over)
remaining in the containing function.  This ensures that
none of the variables which get modified between the setjmp()
and the longjmp() and which get referenced after the longjmp()
are local variables in the function containing the setjmp().

[The obvious alternative of declaring the local variables in the
function containing setjmp() as `volatile' doesn't work, since
the assignments to those output variables may be deep in some
function called indirectly from the goal that we're committing
across, and assigning to a volatile-qualified variable via a
non-volatile pointer is undefined behaviour.  The only way to
make it work would be to be to declare *every* output argument
that we pass by reference as `volatile T *'.  But that would
impose distributed fat and would make interoperability difficult.]

compiler/options.m:
	Add a new option `--put-commits-in-own-function'.

compiler/handle_options.m:
	If the target is high-level C, set the
	`--put-commits-in-own-function' option.

compiler/ml_code_util.m:
	Add a routine for accessing the new option.

compiler/ml_code_gen.m:
	If the new option is set, generate each commit in its own
	nested function.

tests/hard_coded/Mmakefile:
tests/hard_coded/setjmp_test.m:
tests/hard_coded/setjmp_test.exp:
	A regression test.
	Previous versions of the compiler generated code for this test
	case that did the wrong thing on *some* systems.
2000-12-10 07:39:47 +00:00
Fergus Henderson
4be69fa961 Eliminated a lot of the dependencies on the the `code_model' type,
Estimated hours taken: 6

Eliminated a lot of the dependencies on the the `code_model' type,
and move that type from llds.m into a new module `code_model'.
The aim of this change is to improve the modularity of the compiler by
reducing the number of places in the compiler front-end that depend
on back-end concepts and the number of places in the MLDS back-end
which depend on the LLDS.

compiler/code_model.m:
	New module.  Contains the code_model type and associated
	procedures.

compiler/llds.m:
	Move the code_model type into code_model.m.

compiler/hlds_goal.m:
	Move the goal_info_get_code_model procedure into code_model.m,
	to avoid having the HLDS modules import code_model.

compiler/hlds_out.m:
	Delete `hlds_out__write_code_model', since it wasn't being used.

compiler/hlds_pred.m:
	Move the proc_info_interface_code_model procedure into code_model.m,
	to avoid having the HLDS modules import code_model.

compiler/goal_path.m:
	When computing the `maybe_cut' field for `some' goals,
	compute it by comparing the determinism rather than by
	comparing the goal_infos.

compiler/unique_modes.m:
	Use determinism and test for soln_count = at_most_many
	rather than using code_model and testing for model_non.

compiler/inlining.m:
	Test for determinism nondet/multi rather than testing
	for code_model model_non.

compiler/hlds_pred.m:
compiler/det_report.m:
	Change valid_code_model_for_eval_method, which succeeded unless
	the eval_method was minimal_model and the code_model was model_det,
	to valid_determinism_for_eval_method, which succeeds unless the
	eval_method is minimal_model and the determinism cannot fail.
	As well as avoiding a dependency on code_model in the HLDS
	modules, this also fixes a bug where det_report could give
	misleading error messages, saying that `multi' was a valid
	determinism for `minimal_model' predicates, when in fact the
	compiler will always report a determinism error if you declare
	a `minimal_model' predicate with determinism `multi'.
	(Actually the code in which this bug occurs is in fact
	unreachable, but this is no doubt also a bug... I'll address
	that one in a separate change.)

compiler/lookup_switch.m:
	Simplify the code a bit by using globals__lookup_*_option
	rather than globals__get_option and then getopt__lookup_option.

compiler/*.m:
	Add `import_module' declarations for `code_model', and in some
	cases remove `import_module' declarations for `llds'.
2000-11-23 04:32:51 +00:00
Fergus Henderson
df5f31e072 Fix a bug that broke the `hlc.gc.memprof' grade when intermodule
Estimated hours taken: 1

Fix a bug that broke the `hlc.gc.memprof' grade when intermodule
optimization was enabled.  Also eliminate an XXX in ml_elim_nested.m.

compiler/ml_code_gen.m:
	Use the pred_id and proc_id of the current procedure,
	rather than the one from the `pragma foreign_code',
	for generating the name/1 used for MR_PROC_LABEL.
	Also, generate the fully qualified name rather than
	discarding the module name.

compiler/mlds.m:
	Make the argument of the name/1 target_code_component
	a fully qualified entity name, rather than an unqualified one.

compiler/mlds_to_c.m:
	Output the fully qualified name, rather than assuming that the
	name comes from the current module.

compiler/ml_elim_nested.m:
	Modify target_code_component_contains_var to handle the
	new type of name/1.  This allowed eliminating an XXX
	which was due to the name being unqualified.
2000-11-20 01:45:13 +00:00
Tyson Dowd
477ecb18f6 Implement pragma foreign_code for Managed C++.
Estimated hours taken: 60

Implement pragma foreign_code for Managed C++.

Currently you can only write MC++ code if your backend is capable of
generating use MC++ as its "native" foreign language.  The IL backend is
the only backend that does this at the moment (the other backends have C
as their "native" foreign language).

Most of the machinery is in place to call from C to (normal) C++
but there is little work done on actually spitting out the C++ code into
a separate file.  The IL backend does this step already with managed C++.
The intention is to turn foreign_code for C++ into a pragma import
(which imports the C++ function from a separate file) and
foreign_code for C (which calls the imported function).  The C++ code
will be inserted into a separate file that is compiled using C linkage.

The important improvement this change gives is that you can write a
module with a C and a MC++ implementations side-by-side.  The target
backend will select the most appropriate foreign language to use.
You can override its choice using --use-foreign-language.  Later on
we will probably want more flexibility than just a single language
selection option).

This change also implements :- pragma foreign_decl, which allows header
file style declarations to be written in languages other than C.

compiler/code_gen.m:
	Reject code that is not C when generating LLDS.

compiler/export.m:
	Start renaming C as foreign.
	Reject code that is not C when generating exports.

compiler/foreign.m:
	A new module to handle foreign language interfacing.
	The bulk of the code for pragma import has been moved here from
	make_hlds.

compiler/globals.m:
	Convert foreign language names to foreign_language.
	This code has been moved closer to the similar conversion we do
	for target language names.
	Add globals__io_lookup_foreign_language_option to make it easier
	to deterministically lookup the options relating to foreign
	languages.


compiler/hlds_module.m:
	Move module_add_foreign_decl and module_add_foreign_body_code
	from make_hlds.m (where they were called module_add_c_header and
	module_add_c_code).

compiler/hlds_out.m:
	Write the foreign language out in HLDS dumps.

compiler/llds.m:
	Change foreign_header_info to foreign_decl_info.
	Change definitions of foreign_decl_code and foreign_body_code to
	include the language.

compiler/llds_out.m:
	Reject code that is not C when writing out LLDS.

compiler/make_hlds.m:
	Add foreign language information to the bodys and decls when
	creating them.
	Update error messages to refer to foreign code instead of C
	code.
	Use foreign.m to generate interfaces from the backend language
	to the foreign language.
	Hardcode C as the language for fact tables.

compiler/mercury_compile.m:
	Collect the appropriate foreign language code together for
	output to the backend.

compiler/intermod.m:
compiler/mercury_to_mercury.m:
	Output the foreign language string.
	Change a few names to foreign_code instead of c_code.

compiler/ml_code_gen.m:
	Filter the foreign language bodys and decls so that we only get
	the ones we are in (given by the use-foreign-language option).

compiler/mlds_to_c.m:
	Abort if we are given non C foreign language code to output
	(we might handle it here in future, or we might handle it
	elsewhere).

compiler/mlds_to_ilasm.m:
	Abort if we are given non MC++ foreign language code to output
	(we might handle it here in future, or we might handle it
	elsewhere).

compiler/options.m:
compiler/handle_options.m:
	Add --use-foreign-language as a user option to control the
	preferred foreign language to use as the implementation of this
	module.
	Add backend_foreign_language as an internal option which stores
	the foreign language that the compiler will use as a default
	(e.g. the natural foreign language for the backend to use).
	Set the preferred backend foreign language depending on the
	target.

compiler/prog_data.m:
	Add managedcplusplus as a new alternative for the
	foreign_language type.
	Make c_header_code into foreign_decl.
	Give the foreign language for foreign_code as an attribute of
	the code.
	Write code to turn attributes into a list of strings (suitable
	for writing out by mercury_to_mercury).  This fixes what appears
	to be a bug in tabled_for_io -- the tabled_for_io attribute was not
	being written out.  Structure the code so this bug is
	difficult to repeat in future.

compiler/prog_io_pragma.m:
	Parse foreign_decl.
	Turn c_header_code into a special case of foreign_decl.

compiler/*.m:
	Remove the language field from pragma_foreign_code, it is now an
	attribute of the code.
	Various type and variable renamings.

tests/invalid/pragma_c_code_and_clauses1.err_exp:
tests/invalid/pragma_c_code_dup_var.err_exp:
tests/warnings/singleton_test.exp:
	Update the tests to reflect the new error messages talking
	about :- pragma foreign_code rather than :- pragma c_code.
2000-11-17 17:48:52 +00:00
Fergus Henderson
b8f181c7c2 In the initialization functions, output calls to MR_init_entry()
Estimated hours taken: 1

compiler/mlds_to_c.m:
	In the initialization functions, output calls to MR_init_entry()
	only if they are needed (i.e. if profiling is enabled).
	Likewise output the code that ensures that the initialization
	is only run once only if the initialization is non-empty.
	Use `bool', `TRUE', and `FALSE' rather than `int', 0, and 1
	for the `initialised' variable.
	The aim of all these changes is to improve the readability
	of the generated code.

compiler/ml_code_gen.m:
	Document the principle that the MLDS back-end should avoid
	relying on macros and conditional compilation, to improve
	readability and to make it easier to target languages like Java.

runtime/mercury_conf_param.h:
runtime/mercury_goto.h:
	Add some comments about the MLDS back-end.
2000-11-13 04:26:02 +00:00
Fergus Henderson
dd43cdab61 Get the MLDS back-end to generate better code for switches.
Estimated hours taken: 8

Get the MLDS back-end to generate better code for switches.
It now compiles Mercury switches on int/char/enums into C switches.

compiler/mlds.m:
	Add `switch' as a new alternative in the `mlds__stmt' type.

compiler/ml_elim_nested.m:
compiler/ml_optimize.m:
compiler/ml_tailcall.m:
compiler/ml_util.m:
	Minor changes to handle the new `switch' statement.

compiler/ml_code_gen.m:
	Move the code for handling switches to ml_switch_gen.m.
	Export `ml_gen_goal', for use in ml_switch_gen.m.

compiler/ml_switch_gen.m:
compiler/ml_dense_switch.m:
	New files, adapted from switch_gen.m and dense_switch.m, but
	with significant changes.  These now handle three forms of switch:
	    - dense switches, implemented as computed gotos;
	    - "direct mapped" switches, which get implemented using the
	      MLDS switch statement, which in turn gets mapped to the
	      target language's switch statement;
	    - if-then-else chains

compiler/ml_code_util.m:
	Add a label counter to the ml_gen_info, and define predicates
	for allocating new labels.

compiler/mlds_to_c.m:
	Output switch statements.  Also fix a layout bug in the output:
	make sure we output newlines at the end of comments.

compiler/mlds_to_il.m:
	Call error/1 for MLDS switch statements.  The code generator
	will generate MLDS computed_gotos (which map to IL `switch'
	instructions) rather than MLDS switch statements, so we should
	never get MLDS switch statements here.

compiler/options.m:
	Add a new option `--prefer-switch', defaulting to enabled,
	which says to generate switches rather than computed gotos
	where possible.
2000-11-08 07:23:11 +00:00
Fergus Henderson
4fd150c78f Change the MLDS calling convention so that for model_det Mercury functions
Estimated hours taken: 6

Change the MLDS calling convention so that for model_det Mercury functions
with output mode results, the function results get mapped to MLDS function
return values rather than to by-ref parameters.  The rationale for this is
to make interoperability simpler (especially for the IL & Java back-ends).

compiler/lambda.m:
	Change the rules for compatibility of closures so that for
	MLDS grades function closures are not treated as compatible
	with predicate closures.

compiler/ml_code_util.m:
	Change ml_gen_params so that it takes a pred_or_func parameter, and
	for model_det functions it maps the output-moded function results
	to MLDS return values.

compiler/ml_code_gen.m:
	For model_det functions with output mode results,
	return the function result by value.
	Rename the `output_vars' field of the ml_gen_info as
	`byref_output_vars'.

compiler/ml_call_gen.m:
	Pass down the pred_or_func parameter to ml_gen_params.
	For calls to model_det functions with output mode results,
	return the function result by value.

compiler/hlds_goal.m:
	Add new predicate generic_call_pred_or_func, for use by ml_call_gen.m.

compiler/ml_unify_gen.m:
	Modify the code for generating wrapper functions for closures so
	that it reflects the new calling convention for Mercury functions.

compiler/mlds.m:
compiler/mlds_to_c.m:
compiler/ml_code_gen.m:
	Don't handle model_det functions with output mode results specially
	in `pragma export' anymore, since the internal MLDS form now
	has the same prototype as the exported one.
2000-10-30 07:09:04 +00:00
Fergus Henderson
9b850aeb86 Update the TODO list at the top of this module.
Estimated hours taken: 0.5

compiler/ml_code_gen.m:
	Update the TODO list at the top of this module.
2000-10-22 15:57:36 +00:00
Fergus Henderson
e823aa5838 Fix some bugs in the static ground term optimization for the MLDS
Estimated hours taken: 20

Fix some bugs in the static ground term optimization for the MLDS
back-end.

compiler/ml_code_util.m:
compiler/ml_code_gen.m:
	Ensure that declarations for a goal are generated in such a
	way that they scope over the C code generated for any
	following goals.  This is needed to ensure that we don't
	generate references to undeclared names in static constants.

compiler/ml_unify_gen.m:
compiler/ml_code_util.m:
	Ensure that all static consts get unique names, so that
	ml_elim_nested.m can hoist them to the top level.
	Also move ml_gen_static_const_decl_flags from ml_unify_gen.m
	to ml_code_util.m, for use by ml_code_gen.m.

compiler/ml_elim_nested.m:
	Hoist out the definitions of static constants to the top level
	in cases where they might be referenced from nested functions.
	Also change the name of the local_vars field of the ml_elim_info
	to local_data, to make it clear that it can hold constants too.

compiler/mark_static_terms.m:
	Fix some typos in the comments.

compiler/mlds_to_c.m:
	Fix an XXX: it was not outputting `static' in the right places.

tests/valid/Mmakefile:
tests/valid/static.m:
	Add some regression tests.
2000-10-22 13:57:55 +00:00
Tyson Dowd
169db1b8fd The .NET MSIL (Microsoft Intermediate Language) backend.
Estimated hours taken: 220

The .NET MSIL (Microsoft Intermediate Language) backend.

While not complete, this backend implements enough of Mercury to run
programs such as eliza (with an appropriate runtime, which is not part
of this change).  The IL backend TODO list is in mlds_to_il.m.

We generate IL "assembler" from MLDS.  The IL assembler (ILASM) then
turns this into bytecode in a .DLL or .EXE file.

Pragma C code is put into a separate .cpp file and compiled using the managed
C++ compiler.

compiler/il_peephole.m:
	Peephole optimize the IL.

compiler/ilasm.m:
	Generate IL assembler from IL.
	Also a representation of the entities in the IL assembler
	syntax.

compiler/ilds.m:
	The IL instruction set.

compiler/mercury_compile.m:
	Generate IL if --target il is set.
	Define mercury_compile__mlds_has_main/1.
	Assemble the IL if --target-code-only is not set.
	Use "target_debug" option instead of "c_debug".

compiler/ml_code_gen.m:
	If the target is IL and we are generating MLDS for foreign code
	that calls continuations (that is, model_non pragma C code),
	create a nested function (not implemented in foreign code) to
	call the continuation.  This is because in managed C++ it isn't
	possible to call a continuation, but it's fine to call a method
	written in IL that calls the continuation instead.

compiler/ml_code_util.m:
	Add unexpected/1 as another error message handler.
	Add code for generating indirect calls to success continutation
	(via a nested function).

compiler/ml_elim_nested.m:
	Generate slightly different code for IL environments.  We don't
	use the address of the environment variable (like in the C
	backend) but use the environment variable directly.  We also
	have to initialize the environment as a new object.
	This is because we use a class to represent the environment,
	whereas the C backend uses a struct.

	Also, if there is no environment required for nested functions
	(that is, the nested functions don't use any of the local
	variables of the parent), don't generate an environment.
	This is important for the nested functions generated in mangaged
	C++ -- otherwise have to be able to handle defining and
	initializing environments in managed C++ just to do the continuation
	calls for model non pragma C code.

	Add field names to elim_info.

compiler/mlds_to_il.m:
	The IL code generator.
	Also contains the mapping from MLDS type to IL types (which
	is useful when generating forieng language stubs to interface
	with IL).

compiler/options.m:
	Add a debugging option for IL assember -- it prints out each
	line of assembler before it executes.  This is very much a
	developer only option.
	Make a "target_debug" option -- the old "c_debug" option is just
	an alias for this option.  If the target it IL, "target_debug"
	can also turn on debugging for IL, allowing the IL debugger to
	display IL instructions as it traces execution.
2000-10-14 04:00:46 +00:00
Tyson Dowd
6d78224020 Add new options and grades for the IL back-end.
Estimated hours taken: 24	(by fjh)

Add new options and grades for the IL back-end.

Implement an option to handle output parameters for nondeterministic
procedures by passing the outputs (by value) to the continuation,
rather than using pass-by-reference.  This is needed for IL
because IL does not allow managed pointers (which we used to
implement pass-by-refernece) to be stored as fields of environment
structs.

Also add an option to return outputs by value in det/semidet code.
This is not yet tested, since none of our target languages support
returning multiple values.

(Note that the IL backend is not activated by these changes -- it
hasn't been checked in).

compiler/globals.m:
	Use field names for the globals structure.
	Add new `target' field to the globals.

compiler/options.m:
compiler/handle_options.m:
	Add new options `--target', `--target-only', `--il', and `--il-only'.
	`--target' specifies the output langauge (e.g. C or IL).
	`--target-only' says to only compile to the output language
	(e.g. similar to -C or --compile-to-c option).
	`--il' sets target to IL.
	`--il-only' sets target to IL and specifies --target-only.

	Add new grades `il' and `ilc'.
	These grades are used for compiling to il grades.  `il' is the
	normal `il' grade, while `ilc' uses low-level (C-like) data
	representations.

	Change `--compile-to-c' from a boolean option to an abbreviation
	for `--target c --target-only', and move it from the "Output options"
	section to the "Compilation model options" section.

	Comment out the documentation for `--generate-prolog' and
	`--prolog-dialect', since those options are not yet implemented.

	Document the `--infer-all' option.

compiler/mercury_compile.m:
	Check the `target_code_only' option rather than `compile_to_c',
	since the latter is just an abbreviation now, not a real option.

compiler/ml_call_gen.m:
compiler/ml_code_gen.m:
compiler/ml_code_util.m:
compiler/ml_unify_gen.m:
	Use the `--det-copy-out' option to decide whether to pass
	output arguments for model_det/model_semi procedures by
	reference, or whether to just return multiple values.
	Use the `--nondet-copy-out' option to decide whether to pass
	output arguments for model_non procedures by reference, or
	whether to just pass them to the continuation.

compiler/mlds.m:
        Change the mlds__cont_type so that it includes a list of
        the continuation's argument types (if any).

compiler/mlds_to_c.m:
        Update to reflect the change to mlds.m.

doc/user_guide.texi:
        Update the documentation to reflect the above changes.


scripts/parse_grade_options.sh-subr:
	Add new options `--target' and `--il'.

scripts/init_grade_options.sh-subr
        Add new grades `il' and `ilc'.

scripts/final_grade_options.sh-subr
        Make `--target il' imply `--high-level-code'.
2000-09-16 00:08:30 +00:00
Fergus Henderson
2006d7d21f Fix a bug in the MLDS back-end: handle `pragma export' of procedures
Estimated hours taken: 1.25

Fix a bug in the MLDS back-end: handle `pragma export' of procedures
defined in modules other than the current module.

compiler/mlds.m:
	Change the entity_name in the pragma_export type to a
	qualified_entity_name.

compiler/ml_code_util.m:
	Change ml_gen_proc_label so that it returns the module name too,
	not just the entity_name.

compiler/ml_code_gen.m:
compiler/mlds_to_c.m:
	Modify to reflect the changes described above.

tests/hard_coded/Mmakefile:
tests/hard_coded/pragma_export.m:
tests/hard_coded/pragma_export.exp:
	A regression test for the above-mentioned bug fix.
2000-08-24 10:01:55 +00:00
Tyson Dowd
c192d50143 Add preliminary support for a new pragma:
Estimated hours taken: 15

Add preliminary support for a new pragma:

:- pragma foreign_code(LanguageString, .... <same args as c_code>).

This is intended to be the eventual replacement of pragma c_code.
Presently the only valid language is "C".
The existing pragma c_code is simply turned into pragma foreign_code.

pragma foreign_code is not a supported pragma at the moment.  There are
several other changes that are intended (for example, foreign_code will
be impure by default).

This change also changes the HLDS goal pragma_c_code/7 to
pragma_foreign_code/8 where the extra argument is the foreign language.

Any code currently generating output for pragma C code simply checks
that the foreign language is set to "c".  Since this is the only
alternative of the type foreign_language, it will always succeed.
However when new alternatives are added it should be fairly easy to find
where the changes need to be made.

Some type names and predicate names have also been updated, however
there are many more that haven't yet been touched.

compiler/prog_io_pragma.m:
	Accept the new syntax.	Turn the old syntax into the new item.

compiler/hlds_goal.m:
	Change pragma_c_code/7 to pragma_foreign_code/8.
	Define the foreign_language type.

compiler/llds.m:
	Change user_c_code/2 to user_foreign_code/3.

compiler/*.m:
	Update the rest of the compiler to handle these types.
	Make a few small changes to update variable names, predicate
	names and type names.
2000-08-09 07:48:04 +00:00
Peter Ross
113dca2cad Bootstrap in the grade hlc.gc.memprof.
Estimated hours taken: 24

Bootstrap in the grade hlc.gc.memprof.
To analyse the generated Prof.* files you need to supply the option
--no-demangle to the profiler.

compiler/ml_code_gen.m:
    Define MR_PROC_LABEL in pragma c_code.

compiler/mlds.m:
    Add a new alternative to the type target_code_component which
    records a mlds_entity_name.  This information is needed when
    outputing the MR_PROC_LABEL #define.

compiler/ml_elim_nested.m:
    Changes due to the change to the type target_code_component.

compiler/mlds_to_c.m:
    Define a new predicate mlds_maybe_output_init_fn which outputs an
    initialisation function, if necessary.  This body of the
    initialisation function consists of calls to init_entry for each
    function in the src module.
    If profiling is turned on at each function call: call
    MR_prof_call_profile(callee, caller) to record the arc in the call
    graph.
    If profiling is turned on each heap allocation call
    MR_maybe_record_allocation().
    Changes due to the change to the type target_code_component.

library/array.m:
library/builtin.m:
library/exception.m:
library/private_builtin.m:
library/std_util.m:
    As c2init doesn't understand preprocessor directives we need to
    define some empty initialisation functions.

trace/mercury_trace_vars.c:
    Avoid a linking problem with MR_trace_ignored_type_ctors.
    Add a dummy member to MR_trace_ignored_type_ctors so that the array
    is never empty.

runtime/mercury.c:
    Call MR_init_entry for each hand defined procedure.

runtime/mercury.h:
    If profiling is turned on include the relevant header files.

runtime/mercury_goto.h:
    Define MR_init_entry.

runtime/mercury_prof.c:
runtime/mercury_prof.h:
    Make decl_fptr an extern global pointer so that mercury_wrapper.c
    can call fclose on it.

runtime/mercury_wrapper.c:
    Call fclose on decl_fptr.
2000-08-02 14:13:14 +00:00
Peter Ross
a48beedc9d Fix a bug where pragma imports of polymorphic predicates where not
Estimated hours taken: 16

Fix a bug where pragma imports of polymorphic predicates where not
having typeinfos prepended to the call.

compiler/prog_data.m:
    Add a the new functor import/4 to pragma_c_code impl.
    The functor stores all the information needed to constuct a C code
    fragment a pragma import.

compiler/make_hlds.m:
    Rather then constructing the C code fragment immediately, construct
    the sub-parts and store them in import/4.

compiler/polymorphism.m:
    Prepend the typeinfos to the list of variables passed to the
    imported C function.

compiler/ml_code_gen.m:
compiler/pragma_c_gen.m:
    Construct the C code fragment then call the routine which handles
    `ordinary' pragma c_code.

compiler/hlds_out.m:
compiler/mercury_to_mercury.m:
    Add code to recognise the import/4 functor.
2000-07-20 11:24:12 +00:00
Peter Ross
d38a833145 Implement `pragma export' for the MLDS backend.
Estimated hours taken: 30

Implement `pragma export' for the MLDS backend.

compiler/mlds.m:
    Add a new type mlds__pragma_export.  It records the exported name,
    the MLDS name of the exported procedure, the function parameters
    of the MLDS entity and the context of the export declaration.
    Add a new mlds_entity_name type export, for names which come from
    pragma export.

compiler/ml_code_gen.m:
    For every pragma export declaration, we associate the information
    used to generate the function prototype for the MLDS entity.  We
    also record whether or not the procedure is a det function in the
    forward mode.

compiler/mlds_to_c.m:
    Output a declaration for the exported procedure to the header file.
    Output a definition for the exported procedure to the source file.
    Providing that the procedure is not a det func in the forward mode
    the declaration is constructed by outputing the function prototype
    for the MLDS function, but using the exported name instead and
    the definition is constructed by defining a function which directly
    calls the MLDS function.
    If the procedure is a det func in the forward mode then the output
    argument is returned by the function instead of being passed in call
    by reference.  The signature of the exported function is changed to
    this new convention.  This change is required so the an exported
    procedure can by re-imported.
    Define in each header file MR_BOOTSTRAPPED_PRAGMA_EXPORT, this code
    should be removed once the compiler has bootstrapped.
    Move output_c_defns so that it comes after all the declarations.
    Modify mlds_output_params so that it takes a module name, not an
    entity name as all it needs is the module name.
    When outputing an mlds_entity_name of type export, don't module
    qualify the name and just output the string.

compiler/hlds_module.m:
    Add a prog_context field to the type pragma_exported_proc.

compiler/make_hlds.m:
    Record the context of the pragma export.

compiler/dead_proc_elim.m:
compiler/export.m:
    Ignore extra field added to pragma_exported_proc.

compiler/ml_elim_nested.m:
    In ml_env_name report error for export mlds__entity_name's.

library/io.m:
runtime/mercury.c:
runtime/mercury.h:
runtime/mercury_init.h:
    Check to see if MR_BOOTSTRAPPED_PRAGMA_EXPORT is defined when
    deciding whether hacks to get around the lack of pragma export need
    to turned on.  This code should be removed once the compiler has
    bootstrapped.
    This also required some definitions to be moved from mercury.c to
    io.m so that MR_BOOTSTRAPPED_PRAGMA_EXPORT is defined when the
    definition is reached.
2000-07-20 10:39:36 +00:00
Peter Ross
30e36b47f4 Avoid a fixed limit problem where only N (around 256) levels of block
Estimated hours taken: 4

Avoid a fixed limit problem where only N (around 256) levels of block
nesting is allowed in the MSVC compiler.

compiler/ml_code_gen.m:
    ml_join_decls creates block nesting proportional to the size of the
    disjunction when generating disjunctions in a model_non context.
    Now we always create a block for each arm of the disjunction.  This
    allows us to safely concatenate blocks together, and hence only nest
    blocks one level deeper.
2000-07-19 09:46:59 +00:00