Commit Graph

74 Commits

Author SHA1 Message Date
Julien Fischer
1c0704f335 Fix some typos.
java/runtime/MercuryThreadPool.java:
    As above.
2016-03-06 00:41:33 +11:00
Mark Brown
3acbf03059 Implement combined higher-order types and insts.
These allow types to be defined in the following manner:

    :- type job ---> job(pred(int::out, io::di, io::uo) is det).

For any construction unification using this functor the argument must
have the required higher-order inst; it is a mode error if it does not.
When terms of type job with inst ground are deconstructed, the argument
is inferred to have the given inst, allowing a higher-order call in that
mode.

The new type syntax is currently only permitted as the direct argument of
a functor in a du type definition. In future it would be meaningful to
support this syntax in other locations, but that is left for a separate
change.

In order to correctly implement the construct/3 library predicate, we
need to be able to dynamically check that arguments do not violate
any constraints on the argument insts. At the moment, we conservatively
abort if any such constraints are present irrespective of whether they
are satisfied or not. Since these constraints are a new feature, no
existing code will abort in this way.

The implementation refers to the inst information associated with types
as "subtype information". This is because, generally, we think of the
combination of a type with a fully bound inst (i.e., one that describes
terms that contain no unbound variables) describes a subtype of that type.

compiler/inst_util.m:
	Ensure that arguments have the necessary insts in construction
	unifications.

	Where available, propagate the insts into arguments rather than
	using ground(shared, none).

compiler/prog_io_type_name.m:
	Parse the new form of types.

compiler/unparse.m:
	Unparse the new form of types.

compiler/prog_io_type_defn.m:
	Allow the new form of types in functor arguments.

compiler/prog_ctgc.m:
compiler/prog_io_item.m:
compiler/prog_io_mutable.m:
compiler/prog_io_pragma.m:
compiler/prog_io_typeclass.m:
compiler/superhomogeneous.m:
	Disallow the new form of types in places other than functor
	arguments.

compiler/prog_data.m:
	Go back to representing function types with result type appended
	to the arguments. In most case this now results in simpler code.

compiler/prog_type.m:
	Abstract away the representation of predicate vs function arguments
	by using a predicate to construct these types.

compiler/rtti.m:
compiler/type_ctor_info.m:
	Include subtype information about the arguments of a du functor
	and about the argument of a notag functor. Generate this
	information from the argument types.

	Currently, the information is one bit which says whether or not
	any subtypes exist in the arguments.

	Bump the RTTI version number from the compiler side.

compiler/rtti_out.m:
	Output functor subtype information for the low-level C backend.

compiler/rtti_to_mlds.m:
	Include functor subtype information in the MLDS.

compiler/mlds_to_cs.m:
	Add the new runtime type to the special cases.

compiler/erl_rtti.m:
compiler/erlang_rtti.m:
library/erlang_rtti_implementation.m:
	Include functor subtype info in the erlang RTTI.

java/runtime/DuFunctorDesc.java:
java/runtime/FunctorSubtypeInfo.java:
	Include functor subtype information in the Java runtime.

runtime/mercury_dotnet.cs.in:
	Include functor subtype information in the C# runtime.

runtime/mercury_type_info.h:
	Include functor subtype information in the C runtime.

	Bump the RTTI version number in the runtime.

	Define macros to access the new field. These macros can correctly
	handle the previous RTTI version, therefore we do not need to
	change the minimum version at this time.

library/private_builtin.m:
	Define constants for use by the Java backend.

library/construct.m:
library/rtti_implementation.m:
	Use the new RTTI to ensure we don't attempt to construct terms
	that violate the new insts.

compiler/prog_rep_tables.m:
	Ignore the new inst info for now.

compiler/*.m:
	Changes to conform to above.

doc/reference_manual.texi:
	Document the new feature.

tests/hard_coded/functor_ho_inst.{m,exp}:
tests/hard_coded/functor_ho_inst_2.{m,exp}:
tests/hard_coded/functor_ho_inst_excp.{m,exp}:
tests/hard_coded/functor_ho_inst_excp_2.{m,exp}:
	Test the new functionality.

tests/invalid/combined_ho_type_inst.{m,err_exp}:
tests/invalid/combined_ho_type_inst_2.{m,err_exp}:
	Test that we don't allow the new types where they are not permitted,
	or are incomplete.

tests/invalid/functor_ho_inst_bad.{m,err_exp}:
tests/invalid/functor_ho_inst_bad_2.{m,err_exp}:
tests/invalid/functor_ho_inst_bad_3.{m,err_exp}:
	Test that the argument inst information is enforced as required.

tests/hard_coded/Mmakefile:
tests/invalid/Mmakefile:
	Run the new test cases.
2016-02-08 16:09:01 +11:00
Zoltan Somogyi
d33273d033 Tell vim not to expand tabs in Makefiles.
This file-specific setting will override a default setting of expandtabs
in $HOME/.vimrc.

*/Makefile:
*/Mmakefile:
    As above.

tests/hard_coded/.gitignore:
    Don't ignore the purity subdir. This ignore must have been left over
    from when purity.m was a test in hard_coded, not hard_coded/purity,
    and it ignored an executable, not a directory.
2015-01-08 22:07:29 +11:00
Paul Bone
21fb0a74fc [java] Run finalisers only if main/2 returns normally
finalisers should be executed only if main/2 returns normally, it does not
throw an exception.  The C backends already do this correctly but the Java
backend did not.  The C# backend has the same bug, this patch does not fix
the C# backend.

java/runtime/MercuryThreadPool.java:
    Add a parameter to the shutdown() method to specify whether the backend
    is aborting.

    In runMain(), run finalisers only if the runtime is exiting normally.

java/runtime/MercuryRuntime.java:
    Add a new finalise() method that takes a parameter allowing standalone
    Java applications to specify whether or not they are aborting when the
    finalise the RTS.

doc/reference_manual.texi:
    Specify the behaviour of finalise directives if main/2 terminates
    with an uncaught exception.

samples/java_interface/standalone_java/JavaMain.java:
    Conform to changes in MercuryRuntime.java.
2015-01-05 16:01:37 +11:00
Paul Bone
5c4671ce9b [java] Fix non-termination when main/2 throws an exception
When main/2 throws an exception we did not properly shutdown the thread pool
and therefore the JVM would not shut down.  Simply calling shutdown() in a
finally block is insufficient because then the primordial thread may finish
before the worker thread is able to report the exception thrown by main/2.
This doesn't seem right because the JVM is supposed to wait for all the
non-daemon threads to finish before it exits.  I suspect that the primordial
thread is closing stdout and stderr as it exits and therefore the exception
is never seen, but I don't know.

This change fixes the issue by ensuring that shutdown() is always called (in
a finally block) and that the main thread waits for the thread pool to
shutdown before it exits.

java/runtime/MercuryThreadPool.java:
    runMain() will not exit until the worker threads have exited.

    Create a new method waitForShutdown() that will wait for the thread pool
    to shutdown.

    Signal the main thread when a worker thread exits.

java/runtime/MercuryWorkerThread.java:
    Worker threads now exit if their task raises an unhanded exception.

java/runtime/MercuryRuntime.java:
    Allow standalone programs to have the same behavour as programs whose
    entrypoint is written in Mercury.
2014-12-30 17:07:35 +11:00
Julien Fischer
d56411e142 Fix typos in Paul's recent change.
java/runtime/MercuryRuntime.java:
java/runtime/MercuryThreadPool.java:
samples/java_interface/standalone_java/JavaMain.java
	As above.
2014-12-16 14:17:46 +11:00
Paul Bone
8b48c420d7 [java] Make changes following Julien's review of my recent patch
java/runtime/JavaInternal.java:
java/runtime/MercuryRuntime.java:
java/runtime/MercuryThreadPool.java:
samples/java_interface/standalone_java/JavaMain.java:
samples/java_interface/standalone_java/Makefile:
samples/java_interface/standalone_java/mercury_lib.m:
    As above.
2014-12-16 12:45:37 +11:00
Paul Bone
e4a0adf424 [java] The thread pool now works when Mercury is used as a library
The thread pool code used in the Java backend was tied the execution of
main/2.  However if Mercury is used as a library the thread pool won't have
been started and threads created with thread.spawn would not be executed.

This patch makes it possible to start and stop the thread pool independently of
main/2 by calling startup() and shutdown().  These calls are called
implicitly by calling runMain().  The thread pool can also be started on
demand.

This patch also adds the MercuryRuntime class, which now contains methods
that may be called by users' Java code to interact with the Mercury runtime
system, including a new finalise() method.

java/runtime/MercuryThreadPool.java:
    Add startup() method.

    shutdown() method is now public and it's meaning has changed, it now
    requests the shutdown rather than performing it.

    Renamed some variables to make their meanings clearer.

java/runtime/JavaInternal.java:
    Initialise the ThreadPool and MercuryOptions objects on demand.

    Make all members of this class static to avoid confusion.

    Add a private constructor.

java/runtime/MercuryRuntime.java:
    Add methods that can be called by Mercury users to interact with the
    runtime system.  Including a convenient finalise() method that does all
    the finalisation.

samples/java_interface/standalone_java/mercury_lib.m:
samples/java_interface/standalone_java/JavaMain.java:
    Extend the standalone Java example so that it makes use of threads: Add
    a fibs function in Mercury that uses concurrency and therefore starts
    the thread pool; call it from the Java code.

    Use the new finalise() method from the MercuryRuntime class inside of a
    finally block.

samples/java_interface/standalone_java/Makefile:
    Fix a minor error.
2014-12-16 10:16:08 +11:00
Paul Bone
0b5e06e4ac [java] Fix locking bug in Java ThreadPool.
Fix a bug that made it possible that the tasks_lock could be locked but
never unlocked if an exception was thrown.

java/runtime/MercuryThreadPool.java:
    As above.
2014-12-16 10:16:08 +11:00
Paul Bone
a17d976d86 [java] Remove dead code
java/runtime/MercuryThreadPool.java:
    As above.
2014-12-16 10:16:08 +11:00
Paul Bone
a2879c6837 Make MercuryThreadPool (java) notice when a thread blocks on a semaphore.
If threads are blocked while there is work in the queue extra threads may be
spawned to keep the processors busy.

Beginning now, tasks created with thread.spawn are use the thread pool.
(thread.spawn_native does not use the thread pool.)

java/runtime/Semaphore.java:
    Wrap Java's Semaphore class which call the current thread's blocked()
    and running() methods when a thread blocks and then runs after being
    blocked.

library/thread.semaphore.m:
    Use our own Semaphore class.

java/runtime/MercuryThread.java:
java/runtime/MercuryWorkerThread.java:
    Define blocked() and running() on our threads.

java/runtime/NativeThread.java:
    This class is used by spawn_native/4 and is required to define blocked()
    and running(), however it implements them as no-ops as it isn't included
    in the thread pool.

java/runtime/ThreadStatus.java:
    Define the BLOCKED status.

java/runtime/MercuryThreadPool.java:
    Count blocked threads seperatly and allow the creation of new threads
    when existing threads become blocked.

    Add some tracing code to help debug the thread management code.  This is
    disabled by default.

library/thread.m:
    Implement spawn for Java using the thread pool.  This was not enabled
    earlier because without using java/runtime/Semaphore.java it was
    possible to deadlock the system.

java/runtime/Task.java:
    Add some tracing code to debug thread state changes, this is disabled by
    default.
2014-10-03 19:22:32 +10:00
Paul Bone
ab44bbad3f Use a better algorithm for unwinding version_arrays.
The old version_array rewind code used linear stack space in order to
perform it's updates in the right order (newest to oldest) by following the
structure's next pointers (which are a oldest to newest list).  I previously
introduced week prev pointers so that walking over this structure newest to
oldest could be done with constant stack space.  However that is less
efficient than the method suggested by Peter Wang.

Peter suggested using a bitmap and traversing oldest-to-newest, marking
each update in the bitmap and checking the bitmap before making any update.
Thus preserving older values over newer ones (which is good, this code
_rewinds_ updates).  This patch implements Peter's suggestion and removes
the prev pointers from the structure for C and Java backends.

Thanks to Peter Wang for giving me a hand with C#.

library/version_array.m:
    As above.

runtime/mercury_bitmap.h:
    Add some macros for initialising bitmaps and testing, setting and clearing
    their bits.

library/bitmap.m:
java/runtime/MercuryBitmap.java:
    Move the Java MercuryBitmap class into the runtime library.  This makes
    it easier for other standard library modules to use this Java class.

library/bitmap.m:
runtime/mercury_dotnet.cs.in:
    Move C# MercuryBitmap class into runtime/mercury_dotnet.cs

library/bitmap.m:
    Add extra methods to the C# MercuryBitmap class.

tests/hard_coded/version_array_test.exp:
tests/hard_coded/version_array_test.m:
    Ensure that the test verifies that rolling back updates to a version
    array rolls back changes in the correct order: If two updates modify the
    same cell, then the older one should be visible in the result.

    Use longer arrays so that the bitmap used in the rollback code is more
    than one byte in length.
2014-09-10 12:04:53 +10:00
Paul Bone
496952e5ab Use a thread pool to manage threads on the Java backend
Thread pools are often used to reduce poor performance on embarrassingly
parallel work loads.  This isn't immediately necessary on the Java backend
however I've implemented it because:
    + It will be required when we implement parallel conjunctions for the
      Java backend.
    + I want to implement parallel profiling on the Java backend and don't
      want to have to implement this once now, and then re-implement it after
      introducing thread pools later.

We want the thread pool to generally restrict the number of threads that are
in use, this reduces overheads.  However, when one or more tasks become
blocked then it can be desirable to create extra threads, this helps ensure
that all processors are kept busy and that thread pooling doesn't contribute
to any deadlocks itself.  The implementation is a work in prograss and
currently does not implement this second feature.

Java's API provides several different thread pools, see
java.util.concurrent.Executors, none of which are suitable.  Specifically
the fixed thread pool is unsuitable as we want to be able to temporarily
exceed the normal number of threads as explained above; and the cached
thread pools, which are also very similar to the ThreadPoolExecutor class,
do not implement the correct algorithm for determining when a new thread
should be created (they can still perform poorly for embarassingly parallel
workloads).  Additionally we cannot instrument this code as easily for
parallel profiling.

These changes alter the behaviour of Mercury threads on the Java backend in
two ways, they now behave more correctly and more like threads on the C
backends.
    + If a thread throws an exception it is now reported and the program is
      aborted.  Previously it was ignored and let pass to the Java runtime
      where I assume it was reported.
    + The program now exits only after all threads have exited.

The ThreadPool will automatically detect the number of threads to use, or if
the -P flag is given in the MERCURY_OPTIONS environment variable it will
honor that.

java/runtime/MercuryThread.java:
java/runtime/MercuryThreadPool.java:
java/runtime/MercuryWorkerThread.java:
java/runtime/Task.java:
java/runtime/ThreadStatus.java:
    These new classes make up the thread pool.  A MercuryThread is an
    abstract class for Mercury threads, MercuryWorkerThread is a concrete
    subclass of MercuryThread which includes the worker thread behaviour.
    A Task is a computation/closure that has not yet been started, it
    provides some methods not available in Java's generic Runnable and
    Callable classes.  The others should be self-explanatory and all files
    contain documentation.

java/runtime/Getopt.java:
java/runtime/MercuryOptions.java:
    Parse the MERCURY_OPTIONS environment variable for the -P flag.

java/runtime/JavaInternal.java:
    Add support for handling Mercury exceptions, this is used in case a
    worker thread's task (a Mercury thread) throws an exception.

compiler/mlds_to_java.m:
    The main method of the main Java class of an application now starts and
    uses the thread pool to execute main/2.

library/exception.m:
    Export exception reporting code to the Java runtime system.

library/thread.m:
    Use the thread pool for thread.spawn.
2014-08-01 12:32:51 +10:00
Paul Bone
1fabbb8d52 Fix whatespace
java/runtime/JavaInternal.java:
    Use tabs/spaces consistently (four spaces)

    Remove trailing whitespace

library/exception.m?
    Remove trailing whitespace
2014-07-15 17:03:39 +10:00
Paul Bone
88acd153d1 Add constant structure support to Java backend
This change enables constant structure support for the Java backend for the
structures introduced by the polymorphism pass.

compiler/const_struct.m:
compiler/ml_unify_gen.m:
    Enable constant structure support

java/runtime/TypeInfo_Struct.java:
    Add a constructor to the TypeInfo structure for Java.  This constructor
    has the interface expected by the polymorphism code.

    Never expect to be passed the arity to the varargs constructor.

compiler/polymorphism.m:
compiler/ml_unify_gen.m:
    Change how we avoid creating TypeInfos with an explicit arity argument
    for Java

    We never specify the arity for a TypeInfo_Struct in a Java grade.  There
    are two reasons: this is a little difficult to handle due to overloading
    rules and it is not necessary.

    This is handled by removing these arguments in the MLDS backend.
    However, the current solution does not work when we create TypeInfos as
    constant structures.  Therefore this change removes the special case
    from the MLDS backend and instead never creates the arity arguments
    during the polymorphism pass.  rtti_to_mlds.m didn't need updating as it
    already had the correct behaviour.

compiler/rtti_to_mlds.m:
    Update a comment.
2014-02-01 16:02:42 +11:00
Julien Fischer
1eda59e3da Convert .cvsignore files into .gitignore files.
Delete the empty lazy_evaluation directory from extras.

*/.cvsignore:
     Make this into .gitignore files.
     (Update them where necessary.)

extra/lazy_evalution:
    Delete this directory; its former contents were moved
    elsewhere some time ago.
2013-01-03 13:12:22 +11:00
Julien Fischer
8225fdbe76 Fix a problem that prevents Java 1.7 from compiling the Java code generated by
Branches: main, 11.07

Fix a problem that prevents Java 1.7 from compiling the Java code generated by
the Mercury compiler.  Previously, the TypeInfo_Struct class in the Java
version of Mercury's runtime defined the following constructors:

    public TypeInfo_Struct(TypeInfoStruct ti, int arity, Object... as)
    public TypeInfo_Struct(TypeInfoStruct ti, Object... as)

Changes to the way most specific varargs method selection works in Java 1.7
means the above is now rejected by the Java compiler.  (It should apparently
have never been allowed in the first place.)

The fix is to delete the first constructor above from the runtime and not
generate code that provides the arity.  (The arity argument was only ever used
as a sanity check on the number of the following arguments.)

java/runtime/TypeInfo_Struct.java
	Delete one of the ambiguous constructors from this class.

compiler/rtti_to_mlds.m:
	Don't pass an arity argument to the constructor for TypeInfo_Struct
	objects.
2011-11-30 15:38:57 +00:00
Peter Wang
0ae65de577 Pack consecutive enumeration arguments in discriminated union types into a
Branches: main

Pack consecutive enumeration arguments in discriminated union types into a
single word to reduce cell sizes.  Argument packing is only enabled on C
back-ends with low-level data, and reordering arguments to improve
opportunities for packing is not yet attempted.  The RTTI implementations for
other back-ends will need to be updated, but that is best left until after any
argument reordering change.

Modules which import abstract enumeration types are notified so by writing
declarations of the form:

	:- type foo where type_is_abstract_enum(NumBits).

into the interface file for the module which defines the type.


compiler/prog_data.m:
	Add an `arg_width' argument to constructor arguments.

	Replace `is_solver_type' by `abstract_type_details', with an extra
	option for abstract exported enumeration types.

compiler/handle_options.m:
compiler/options.m:
	Add an internal option `--allow-argument-packing'.

compiler/make_hlds_passes.m:
	Determine whether and how to pack enumeration arguments, updating the
	`arg_width' fields of constructor arguments before constructors are
	added to the HLDS.

compiler/mercury_to_mercury.m:
compiler/modules.m:
	Write `where type_is_abstract_enum(NumBits)' to interface files
	for abstract exported enumeration types.

compiler/prog_io_type_defn.m:
	Parse `where type_is_abstract_enum(NumBits)' attributes on type
	definitions.

compiler/arg_pack.m:
compiler/backend_libs.m:
	Add a new module.  This mainly contains a predicate which packs rvals
	according to arg_widths, which is used by both LLDS and MLDS back-ends.

compiler/ml_unify_gen.m:
compiler/unify_gen.m:
	Take argument packing into account when generating code for
	constructions and deconstructions.  Only a relatively small part of the
	compiler actually needs to understand argument packing.  The rest works
	at the HLDS level with constructor arguments and variables, or at the
	LLDS and MLDS levels with structure fields.

compiler/code_info.m:
compiler/var_locn.m:
	Add assign_field_lval_expr_to_var and
	var_locn_assign_field_lval_expr_to_var.

	Allow more kinds of rvals in assign_cell_arg.  I do not know why it was
	previously restricted, except that the other kinds of rvals were not
	encountered as cell arguments before.

compiler/mlds.m:
	We can now rely on the compiler to pack arguments in the
	mlds_decl_flags type instead of doing it manually.  A slight downside
	is that though the type is packed down to a single word cell, it will
	still incur a memory allocation per cell.  However, I did not notice
	any difference in compiler speed.

compiler/rtti.m:
compiler/rtti_out.m:
	Add and output a new field for MR_DuFunctorDesc instances, which, if
	any arguments are packed, points to an array of MR_DuArgLocn.  Each
	array element describes the offset in the cell at which the argument's
	value is held, and which bits of the word it occupies.  In the more
	common case where no arguments are packed, the new field is simply
	null.

compiler/rtti_to_mlds.m:
	Generate the new field to MR_DuFunctorDesc.

compiler/structure_reuse.direct.choose_reuse.m:
	For now, prevent structure reuse reusing a dead cell which has a
	different constructor to the new cell.  The code to determine whether a
	dead cell will hold the arguments of a new cell with a different
	constructor will need to be updated to account for argument packing.

compiler/type_ctor_info.m:
	Bump RTTI version number.

	Conform to changes.

compiler/add_type.m:
compiler/check_typeclass.m:
compiler/equiv_type.m:
compiler/equiv_type_hlds.m:
compiler/erl_rtti.m:
compiler/hlds_data.m:
compiler/hlds_out_module.m:
compiler/intermod.m:
compiler/make_tags.m:
compiler/mlds_to_gcc.m:
compiler/opt_debug.m:
compiler/prog_type.m:
compiler/recompilation.check.m:
compiler/recompilation.version.m:
compiler/special_pred.m:
compiler/type_constraints.m:
compiler/type_util.m:
compiler/unify_proc.m:
compiler/xml_documentation.m:
	Conform to changes.

	Reduce code duplication in classify_type_defn.

compiler/hlds_goal.m:
	Clarify a comment.

library/construct.m:
	Make `construct' pack arguments when necessary.

	Remove an old RTTI version number check as recommended in
	mercury_grade.h.

library/store.m:
	Deal with packed arguments in this module.

runtime/mercury_grade.h:
	Bump binary compatibility version number.

runtime/mercury_type_info.c:
runtime/mercury_type_info.h:
	Bump RTTI version number.

	Add MR_DuArgLocn structure definition.

	Add a macro to unpack an argument as described by MR_DuArgLocn.

	Add a function to determine a cell's size, since the number of
	arguments is no longer correct.

runtime/mercury_deconstruct.c:
runtime/mercury_deconstruct.h:
runtime/mercury_deconstruct_macros.h:
runtime/mercury_ml_arg_body.h:
runtime/mercury_ml_expand_body.h:
	Deal with packed arguments when deconstructing.

	Remove an old RTTI version number check as recommended in
	mercury_grade.h.

runtime/mercury_deep_copy_body.h:
	Deal with packed arguments when copying.

runtime/mercury_table_type_body.h:
	Deal with packed arguments in tabling.

runtime/mercury_dotnet.cs.in:
	Add DuArgLocn field to DuFunctorDesc. Argument packing is not enabled
	for the C# back-end yet so this is unused.

trace/mercury_trace_vars.c:
	Deal with packed arguments in MR_select_specified_subterm,
	use for the `hold' command.

java/runtime/DuArgLocn.java:
java/runtime/DuFunctorDesc.java:
	Add DuArgLocn field to DuFunctorDesc. Argument packing is not enabled
	for the Java back-end yet so this is unused.

extras/trailed_update/tr_store.m:
	Deal with packed arguments in this module (untested).

extras/trailed_update/samples/interpreter.m:
extras/trailed_update/tr_array.m:
	Conform to argument reordering in the array, map and other modules in
	previous changes.

tests/hard_coded/Mercury.options:
tests/hard_coded/Mmakefile:
tests/hard_coded/lco_pack_args.exp:
tests/hard_coded/lco_pack_args.m:
tests/hard_coded/pack_args.exp:
tests/hard_coded/pack_args.m:
tests/hard_coded/pack_args_copy.exp:
tests/hard_coded/pack_args_copy.m:
tests/hard_coded/pack_args_intermod1.exp:
tests/hard_coded/pack_args_intermod1.m:
tests/hard_coded/pack_args_intermod2.m:
tests/hard_coded/pack_args_reuse.exp:
tests/hard_coded/pack_args_reuse.m:
tests/hard_coded/store_ref.exp:
tests/hard_coded/store_ref.m:
tests/invalid/Mmakefile:
tests/invalid/where_abstract_enum.err_exp:
tests/invalid/where_abstract_enum.m:
tests/tabling/Mmakefile:
tests/tabling/pack_args_memo.exp:
tests/tabling/pack_args_memo.m:
	Add new test cases.

tests/hard_coded/deconstruct_arg.exp:
tests/hard_coded/deconstruct_arg.exp2:
tests/hard_coded/deconstruct_arg.m:
	Add constructors with packed arguments to these cases.

tests/invalid/where_direct_arg.err_exp:
	Update expected output.
2011-07-05 03:34:39 +00:00
Peter Wang
2209caecea The direct argument functor change added the constant MR_SECTAG_NONE_DIRECT_ARG
Branches: main

The direct argument functor change added the constant MR_SECTAG_NONE_DIRECT_ARG
in some places but not others, breaking deconstruct on C# and Java back-ends.

compiler/mlds_to_gcc.m:
java/runtime/Sectag_Locn.java:
library/rtti_implementation.m:
runtime/mercury_dotnet.cs.in:
	Add missing constants.
2011-06-27 06:40:36 +00:00
Peter Wang
ea6a590462 Add overloads of `TypeInfo_Struct.maybe_new' so the C# and Java
Branches: main

java/runtime/TypeInfo_Struct.java:
runtime/mercury_dotnet.cs.in:
        Add overloads of `TypeInfo_Struct.maybe_new' so the C# and Java
        compilers can pick the more appropriate version at compile time,
        as they know the type of the argument.

        Simplify the code.
2010-10-08 03:29:48 +00:00
Peter Wang
d953a1d17f Collapse equivalences when comparing type_infos for equality.
Branches: main, 10.04

java/runtime/TypeInfo_Struct.java:
        Collapse equivalences when comparing type_infos for equality.

java/runtime/TypeCtorRep.java:
        Duplicate the required constants which otherwise are in
        private_builtin.m.  They should all be moved here but I don't want to
        make that change on the 10.04 branch.

library/rtti_implementation.m:
        Collapse equivalences in type_info_num_functors.

tests/hard_coded/construct_bug.m:
        Use text streams in this test case, as the current hacky implementation
        of `io.write_binary' doesn't work on the Java backend.
2010-09-22 04:11:34 +00:00
Peter Wang
8da3c6f4a9 Make TypeInfo_Struct.unify() method compare correctly two type_infos
Branches: main

java/runtime/TypeInfo_Struct.java:
        Make TypeInfo_Struct.unify() method compare correctly two type_infos
        where one type_info has a null `args' field and another has a
        zero-length `args' array.
2010-01-27 03:40:19 +00:00
Peter Wang
408b1621c9 Implement some RTTI procedures for Java:
Branches: main

Implement some RTTI procedures for Java:

        get_functor_ordinal/2,
        get_functor_lex/2,
        type_ctor/2,
        pseudo_type_ctor_and_args/3,
        make_type/2

Make type_desc/type_ctor_desc/pseudo_type_desc into synonyms for type_info/etc.,
mainly to simplify the hand-written RTTI handling functions.  Previously the
*_desc types were represented by *Desc wrapper classes.


compiler/mlds_to_java.m:
compiler/builtin_lib_types.m:
        Map *_desc types to TypeInfo_Struct, TypeCtorInfo_Struct,
        PseudoTypeInfo_Struct.  We cannot simply use
        `:- pragma foreign_type("Java", ...)' in the library as the compiler
        will complain about missing type definitions for other language
        backends.

library/construct.m:
        Implement get_functor_ordinal, get_functor_lex for Java backend.

        Conform to removal of *Desc classes.

library/rtti_implementation.m:
        Implement get_functor_ordinal, get_functor_lex,
        pseudo_type_ctor_and_args for Java backend.

        Handle existentially quantified arguments in get_functor_du.

        Make type_info/type_ctor_info comparison procedures compare module
        names before constructor names, to match MR_compare_type_info,
        MR_compare_type_ctor_info.

library/type_desc.m:
        Implement type_ctor, pseudo_type_ctor_and_args, make_type on Java
        backend.

        Conform to removal of *Desc wrapper classes.

java/runtime/TypeCtorInfo_Struct.java:
        Add constructor for variable arity type constructors.

java/runtime/TypeCtorRep.java:
        Delete unused constants.
2010-01-20 06:59:35 +00:00
Peter Wang
43052bb247 Add a new calling convention for Mercury procedures exported to Java with
Branches: main

Add a new calling convention for Mercury procedures exported to Java with
`:- pragma foreign_export'.  When the procedure has multiple output arguments,
require the caller to pass instances of a `Ref<T>' class which contain a field
to hold the output value.  This is more verbose than returning an `Object[]'
array, and requires extra memory allocations, but is more type-safe.  Another
advantage is that the Ref arguments will appear at the same positions as the
output arguments in the Mercury procedure.

The new convention must be enabled with `--java-export-ref-out'.  The plan is
to enable it by default in the future.

compiler/options.m:
doc/reference_manual.texi:
        Add the option.

compiler/ml_proc_gen.m:
        When the option is set, disable `--det-copy-out' and generate the
        function parameters assuming pass-by-reference for outputs.
        Only use the new convention when necessary.

compiler/mlds_to_java.m:
        Make the code to generate the exported forwarding methods account for
        `mlds_ptr_type' parameters.  These are converted to `Ref<T>' arguments.

java/runtime/Ref.java:
        Add the reference class.
2009-11-30 00:31:56 +00:00
Ian MacLarty
6aea768459 Override Object#equals for Mercury enumerations in the Java grades
Estimated hours taken: 2
Branches: main

Override Object#equals for Mercury enumerations in the Java grades
and document that this should be used for equality testing in Java
code.  The problem with using == is that instances might not have
the same addresses if they are serialized and then deserialized.

MercuryEnum is now a class and not an interface.

compiler/ml_type_gen.m:
    Inherit the MercuryEnum class in Mercury enumerations
    instead of implementing the MercuryEnum interface.

compiler/mlds_to_java.m:
    Don't output the MR_value member in Java classes that correspond to
    Mercury enumerations.  This member is now inherited from MercuryEnum.

    Don't mangle classes in the Mercury runtime.

doc/reference_manual.texi:
    Document that the equals method should be used for exported
    Mercury enumerations.

java/runtime/MercuryEnum.java:
    Make MercuryEnum a class and implement the equals method.

    Also implement the hashcode method.
2009-09-22 07:16:38 +00:00
Peter Wang
b573e83761 Mark Java RTTI classes as implementing java.io.Serializable to
Branches: main

Mark Java RTTI classes as implementing java.io.Serializable to
enable serialization of existentially typed values.

java/runtime/DuExistInfo.java:
java/runtime/DuExistLocn.java:
java/runtime/DuFunctorDesc.java:
java/runtime/DuPtagLayout.java:
java/runtime/EnumFunctorDesc.java:
java/runtime/ForeignEnumFunctorDesc.java:
java/runtime/MethodPtr.java:
java/runtime/NotagFunctorDesc.java:
java/runtime/PseudoTypeInfo.java:
java/runtime/ReservedAddrFunctorDesc.java:
java/runtime/Sectag_Locn.java:
java/runtime/TypeClassConstraint.java:
java/runtime/TypeClassDeclStruct.java:
java/runtime/TypeClassId.java:
java/runtime/TypeClassMethod.java:
java/runtime/TypeCtorInfo_Struct.java:
java/runtime/TypeCtorRep.java:
java/runtime/TypeFunctors.java:
java/runtime/TypeInfo_Struct.java:
java/runtime/TypeLayout.java:
        As above.

        TypeCtorInfo_Struct.unify() cannot rely on the uniqueness of
        TypeCtorInfo_Struct instances, as deserialisation will create new
        copies.
2009-09-07 03:10:02 +00:00
Peter Wang
68a5c0047a On the Java backend, use specialised MethodPtr interfaces so that when calling
Branches: main

On the Java backend, use specialised MethodPtr interfaces so that when calling
a method pointer input arguments do not have to be passed via a temporary
array, for arities up to 15.  For higher arities the temporary array is still
used.

java/runtime/MethodPtr.java:
java/runtime/MethodPtr1.java:
java/runtime/MethodPtr10.java:
java/runtime/MethodPtr11.java:
java/runtime/MethodPtr12.java:
java/runtime/MethodPtr13.java:
java/runtime/MethodPtr14.java:
java/runtime/MethodPtr15.java:
java/runtime/MethodPtr2.java:
java/runtime/MethodPtr3.java:
java/runtime/MethodPtr4.java:
java/runtime/MethodPtr5.java:
java/runtime/MethodPtr6.java:
java/runtime/MethodPtr7.java:
java/runtime/MethodPtr8.java:
java/runtime/MethodPtr9.java:
java/runtime/MethodPtrN.java:
        Add specialised MethodPtr interfaces and MethodPtrN for any higher
        arities.

compiler/mlds_to_java.m:
        Make the code generator use the specialised MethodPtr interfaces.

library/Mmakefile:
        Compile java/runtime/*.java files explicitly as some MethodPtr*.java
        files won't be compiled implicitly when compiling the standard library.

library/exception.m:
java/runtime/Exception.java:
library/rtti_implementation.m:
        Conform to changes.
2009-09-03 05:04:16 +00:00
Peter Wang
de1329a55e Support `thread_local' mutables on Java backend.
Branches: main

compiler/make_hlds_passes.m:
        Support `thread_local' mutables on Java backend.

        Add some newlines in generated code for mutables.

compiler/prog_mutable.m:
        Update documentation.

java/runtime/JavaInternal.java:
        Avoid a warning.
2009-09-02 07:12:26 +00:00
Peter Wang
9fc47f5c91 Make the Java backend not use native code to implement any standard library
Branches: main

Make the Java backend not use native code to implement any standard library
procedures.  The only such procedures were related to benchmarking and timing,
which aren't very important so I just make a best effort using methods that
the Java standard library provides.

library/benchmarking.m:
        Implement `benchmarking.report_stats' for Java as closely as possible
        without native code.  Add real time output.

        Add `benchmarking.ML_initialise' to remember the time when the
        program starts up.

compiler/mlds_to_java.m:
        Make the main wrapper call `benchmarking.ML_initialise' at startup.

library/time.m:
        Implement `time.clock', `time.times', `time.clocks_per_sec',
        `time.clk_tck' as closely as possible without native code.

library/Mmakefile:
        Comment out commands to build and install Native.so.

java/runtime/Native.c:
        Even though this is not used any more, update the function names for
        the "jmercury" package prefix.

README.Java:
        Update section on unimplemented procedures.
2009-08-14 06:34:03 +00:00
Peter Wang
26498eb230 Make the Java jmercury.runtime.Exception class implement the getMessage()
Branches: main

Make the Java jmercury.runtime.Exception class implement the getMessage()
method for Throwable objects.  This is useful when Mercury code is called
from Java code, without the main/2 wrapper.

java/runtime/Exception.java:
        Add a method pointer as a class variable.

        Implement the getMessage() method by calling the method pointer
        if set.

library/exception.m:
        On module initialisation set the method pointer in the
        jmercury.runtime.Exception class.

        Factor out code from report_uncaught_exception_2.
2009-08-03 05:13:55 +00:00
Peter Wang
c6b277dd3a Implementation of construct and construct_tuple for Java backend.
Branches: main

Implementation of construct and construct_tuple for Java backend.

java/runtime/TypeCtorInfo_Struct.java:
        The type_ctor_num_functors field was not being initialised.

library/construct.m:
        Call the predicates in rtti_implementation.m when appropriate.

library/rtti_implementation.m:
        Fix get_functor_enum which was confused lexical ordering and functor
        ordinals.

        Implement construct and construct_tuple for Java backend.

        Delete some out of date comments.

library/type_desc.m:
        Add Pseudo_type_desc_0 class which mirrors the
        jmercury.runtime.PseudoTypeInfo class with Type_desc_0 and
        Type_ctor_desc_0 as subclasses.

        Fill in stubs that work with pseudo_type_descs.

library/univ.m:
        Export predicates.

README.Java:
        Update readme.
2009-07-31 06:54:21 +00:00
Peter Wang
dfca5e04f0 Make Java classes corresponding to Mercury types automatically serialisable.
Branches: main

Make Java classes corresponding to Mercury types automatically serialisable.

java/runtime/MercuryType.java:
        Make MercuryType extend java.io.Serializable.

compiler/mlds_to_java.m:
java/runtime/MercuryEnum.java:
        Add a MercuryEnum interface which is implemented by classes
        corresponding to Mercury enumerations.  Previously enumerations
        didn't implement MercuryType.

compiler/ml_type_gen.m:
        Make enumeration classes implement MercuryType and MercuryEnum.

library/builtin.m:
        Make builtin.copy check for MercuryEnum instead of relying on
        enumeration classes not to implement MercuryType.  This is not
        strictly necessary but avoids copying instances of enumeration
        classes.
2009-07-09 05:18:08 +00:00
Peter Wang
6be2fe20ee The Java backend was sometimes generating code like this to initialise RTTI
Branches: main

The Java backend was sometimes generating code like this to initialise RTTI
data structures:

    foo_type_info.init(...,
        /* cast */ new TypeInfo_Struct(bar_type_ctor_info), ...);
    bar_type_ctor_info.init(...);

where `bar_type_ctor_info' is actually a type_info.

The problem is that the fields of the non-initialised `bar_type_ctor_info'
would be copied into the new TypeInfo_Struct object.  The "cast" is of course
also unnecessary as the bar is already a TypeInfo_Struct.

This patch attempts to fix the problem in two ways:

1. Don't allocate a new TypeInfo_Struct object to emulate the "cast" unless the
value is actually a TypeCtorInfo_Struct, avoiding the problem of copying
uninitialised fields.  Currently this is implemented by a runtime check because
the MLDS `cast' operation doesn't record the original type of the value being
cast.

2. Instead of relying on mlds_to_rtti.m to return a list of RTTI data
structures where sub-structures appear before the structures that reference
them, explicitly perform a topological sort.  This should be more robust.


Unrelated change: use pre-allocated PseudoTypeInfo instances for common
variable numbers (1 through 5).


compiler/rtti_to_mlds.m:
        Add a function to order a list of RTTI definitions as above.

        Use cons instead of list.append in some places now that we can.

compiler/mlds_to_java.m:
        Call the function to order RTTI definitions before outputing
        the initialisations.

        Call TypeInfo_Struct.maybe_new instead of allocating new
        TypeInfo_Structs.

        Generate code that uses pre-allocated PseudoTypeInfo instances.

java/runtime/TypeInfo_Struct.java:
        Add a `maybe_new' method for "casting" TypeCtorInfo_Structs to
        TypeInfos or returning the argument unchanged.

        Delete a copy constructor; now unused.

        Add some assertions.

java/runtime/PseudoTypeInfo.java:
        Add static instances of PseudoTypeInfo.

tests/hard_coded/Mmakefile:
tests/hard_coded/java_rtti_bug.exp:
tests/hard_coded/java_rtti_bug.m:
        Add test case.
2009-06-26 01:12:00 +00:00
Peter Wang
8eb879e701 Make :- initialise' and :- finalise' declarations work in Java grades.
Branches: main

Make `:- initialise' and `:- finalise' declarations work in Java grades.
Initialisation predicates are simply called from static initialisation blocks.
Finalisation predicates work by registering methods (objects) with the
JavaInternal runtime class.  After main/2 terminates each of the registered
methods is called.

compiler/make_hlds_passes.m:
        Make add_pass_3_initialise, add_pass_3_finalise generate predicates
        for backends other than C.

compiler/mlds_to_java.m:
        Generate code to register finalisation predicates with the runtime.

        Make the generated code that calls main/2 call run the registered
        finalisers as well.

java/runtime/JavaInternal.java:
        Add code to register and call finalisers when main/2 terminates.
2009-06-22 03:31:30 +00:00
Peter Wang
1b648d0ac2 Put all Mercury-generated Java classes into the package `jmercury' and
Branches: main

Put all Mercury-generated Java classes into the package `jmercury' and
runtime classes into `jmercury.runtime'.  The Mercury module hierarchy is
not reflected in the package name.  We name sub-module classes using
their fully-qualified module names with `__' between components, e.g.
`bit_buffer.read' produces `class bit_buffer__read'.

As all generated Java code is in the same package we don't need to package
qualify identifiers, and we don't need the hack to avoid clashing package
and class names.  It also makes it easier to write Java foreign code because
generated Java class names are easier to predict from Mercury module names.

The package names are not `mercury' and `mercury.runtime' because on
case-insensitive file systems we may end up with a `mercury' directory
that could be confused with the `Mercury' directory.


compiler/java_names.m:
        Delete code related to mangling package names.

        Remove the extra `mercury' prefix added to standard library module
        names, as it is redundant with `jmercury'.

        Change runtime package name.

compiler/mlds_to_java.m:
        Make generated code follow the new packaging scheme.

        Don't automatically import all runtime classes.  It doesn't seem
        necessary.

        Update for new packaging scheme.

compiler/file_names.m:
        Fix Java file paths for the new packaging scheme.

compiler/module_cmds.m:
compiler/rtti.m:
library/array.m:
library/backjump.m:
library/benchmarking.m:
library/bitmap.m:
library/builtin.m:
library/exception.m:
library/io.m:
library/library.m:
library/mutvar.m:
library/private_builtin.m:
library/rtti_implementation.m:
library/store.m:
library/string.m:
library/time.m:
library/type_desc.m:
java/runtime/*.java:
        Rename package names.

        Delete unnecessary package qualification.

compiler/mlds.m:
        Add some XXXs to be fixed later.

library/Mmakefile:
        Update for new packaging scheme.

        Let mmake --use-mmc-make work in this directory.
2009-06-17 07:48:16 +00:00
Peter Wang
79e0b9eb1d Work on RTTI implementation for Java.
Branches: main

Work on RTTI implementation for Java.

library/construct.m:
library/type_desc.m:
        Properly distinguish between type_infos and type_descs.  These are not
        the same in Java grades (for now, at least).
        Use special purpose conversion functions instead of unsafe_casts.

        Don't call erlang_rtti_implementation.m predicates in other grades.

library/rtti_implementation.m:
        Properly distinguish between type_infos and type_descs.
        Use special purpose conversion functions instead of unsafe_casts.

        Implement some stub predicates for Java.

        Fix get_arg_type_info which assumed the usual low-level type_info
        representation.  In Java we have a TypeInfo_Struct class with a
        distinct array for type_info arguments so all the indices to access
        arguments were wrong.

        Try to be consistent about how variables are numbered (start from 1).

        Rename some predicates to make them less ambiguous.

        Don't use field access to call functions as it is confusing.

        Update unsafe_get_enum_value for an earlier change where I renamed the
        `value' field in enumeration classes to `MR_value'.

library/stream.string_writer.m:
library/string.m:
        Convert type_infos to type_descs properly.

library/erlang_rtti_implementation.m:
        Unrelated: use private_builtin.unsafe_type_cast instead of a
        foreign_proc.

library/io.m:
        Unrelated: add Java implementation of io.stdout_stream_2/1.

java/runtime/TypeInfo_Struct.java:
        Add a copy method.
2009-06-10 03:31:57 +00:00
Peter Wang
e0a564f0f4 Fix two problems implementation of arrays for Java backend:
Branches: main

Fix two problems implementation of arrays for Java backend:

- array.from_list([1, 2, 3]) didn't work because it would create an Integer[]
  array, but the code generated by the Mercury compiler would try to assign
  it to a int[] variable.  Fix this by special casing the primitive types to
  create arrays with the expected types.

- We used the class of a representative element to determine the type of
  an array to create.  Enumeration types only have the one class
  corresponding to the Mercury type, so the class of the representative
  element is correct.

  However, general d.u. types have subclasses for each functor, so the class
  of a representative element could correspond to a functor.  This meant that
  arrays could only hold more elements with the same functor.  What is needed
  is actually the super class, corresponding to a Mercury type.

  The solution adopted here is to make Java classes corresponding to Mercury
  types implement an interface called `MercuryType'.  Then we can tell
  whether the class or the superclass of the representative element is the
  one needed.


java/runtime/MercuryType.java:
        Add the new interface.

compiler/ml_type_gen.m:
        When the target language is Java, make d.u. parent types implement
        the `MercuryType' interface.

compiler/mlds_to_java.m:
        Make the `MercuryType' interface special so it is printed without an
        arity suffix.

library/array.m:
        Fix the problems with creating/resizing ararys.
2009-06-05 04:17:10 +00:00
Peter Wang
5012b8f45e The Java 5 compiler warns about a method with an array argument:
Branches: main

The Java 5 compiler warns about a method with an array argument:

	m1(Object[] args)

overriding a method declared with varargs:

	m1(Object... args)

The Java 6 compiler accepts it, though I don't know if that was intended.


java/runtime/MethodPtr.java:
	Don't use varargs syntax for MethodPtr.call___0_0.

library/rtti_implementation.m:
	Explicitly create the Object[] arrays in foreign_procs.
2009-06-01 11:06:36 +00:00
Peter Wang
2ed239c286 Implement exception handling predicates for Java.
Branches: main

library/exception.m:
        Implement exception handling predicates for Java.

java/runtime/Exception.java:
        Add a helper class.
2009-05-07 07:05:39 +00:00
Peter Wang
d1cffc4523 RTTI improvements for Java backend. io.write/3 works for some simple types
Branches: main

RTTI improvements for Java backend.  io.write/3 works for some simple types
(builtin types and non-existential d.u. types).

compiler/mlds_to_java.m:
        Fix problem with cyclic RTTI definitions.  Initialisers could refer to
        other RTTI structures which weren't yet allocated, leading to fields
        being null.  The fix is to allocate all top-level RTTI objects first
        and initialise in a second phase.

java/runtime/DuExistInfo.java:
java/runtime/DuExistLocn.java:
java/runtime/DuFunctorDesc.java:
java/runtime/EnumFunctorDesc.java:
java/runtime/ForeignEnumFunctorDesc.java:
java/runtime/TypeClassConstraint.java:
java/runtime/TypeClassDeclStruct.java:
java/runtime/TypeClassId.java:
java/runtime/TypeCtorInfo_Struct.java:
java/runtime/TypeInfo_Struct.java:
        Separate constructors into constructors for the initial allocation,
        and an `init' method to fill in the fields.

java/runtime/MethodPtr.java:
        Use variadic method support to simplify semidet_call_* and
        result_call_* in rtti_implementation.m.

library/builtin.m:
        Make Java definitions of builtin.unify/2 and builtin.compare/3 call
        rtti_implementation.generic_unify and generic_compare.

library/private_builtin.m:
        Add missing MR_TYPECTOR_REP_FOREIGN_ENUM{,_USEREQ} constants
        for C# and Java.

library/rtti_implementation.m:
        Fix and add missing Java versions of many foreign_procs.

        Add more attributes to foreign_procs.

        Clean up the code a bit (fewer casts and ^ field access functions).

README.Java:
        Bump Java version requirement to J2SE 1.5 or higher.
2009-04-30 00:43:35 +00:00
Peter Wang
32736b18d3 Modify this class enough so that Java code generated for the standard
Branches: main

java/runtime/TypeInfo_Struct.java:
        Modify this class enough so that Java code generated for the standard
        library compiles with it.

library/io.m:
        A few more Java foreign procs were overriding the wrong procedures.

library/type_desc.m:
        Add missing class and methods.
2009-04-24 05:26:24 +00:00
Peter Wang
e4777b9c48 In RTTI data, map integers to Java `int' instead of the nonsensical
Branches: main

compiler/rtti.m:
        In RTTI data, map integers to Java `int' instead of the nonsensical
        `mercury.runtime.Integer'.

java/runtime/TypeCtorInfo_Struct.java:
        Conform to the change.
2009-04-22 07:12:13 +00:00
Peter Wang
dd53891e9f Assorted library fixes and stubs for the Java backend.
Branches: main

java/runtime/ForeignEnumFunctorDesc.java:
java/runtime/TypeCtorInfo_Struct.java:
java/runtime/TypeFunctors.java:
library/backjump.m:
library/builtin.m:
library/io.m:
library/math.m:
library/mutvar.m:
library/par_builtin.m:
library/private_builtin.m:
library/region_builtin.m:
library/rtti_implementation.m:
library/store.m:
library/string.m:
library/thread.semaphore.m:
library/time.m:
library/type_desc.m:
        Assorted library fixes and stubs for the Java backend.
2009-04-22 03:03:17 +00:00
Julien Fischer
1fac629e6d Add support for foreign enumerations to Mercury.
Estimated hours taken: 50
Branches: main

Add support for foreign enumerations to Mercury.  These allow the
programmer to assign foreign language values as the representation of
enumeration constructors.

e.g.
	:- type status
		--->	optimal
		;	infeasible
		;	unbounded
		;	unknown.

	:- pragma foreign_enum("C", status/0, [
		optimal    - "STATUS_OPTIMAL",
		infeasible - "STATUS_INFEASIBLE",
		unbounded  - "STATUS_UNBOUNDED",
		unknown    - "STATUS_UNKNOWN"
	]).

The advantage of this is that when values of type status/0 are passed to
foreign code (C in this case) no translation is necessary.  This should
simplify the task of writing bindings to foreign language libraries.

Unification and comparison for foreign enumerations are the usual
unification and comparison for enumeration types, except that the default
ordering on them is determined by the foreign representation of the
constructors.  User-defined equality and comparison also work for foreign
enumeration types.

In order to implement foreign enumerations we have to introduce two
new type_ctor representations.  The existing ones for enum type do not
work since they use the value of an enumeration constructor to perform
table lookups in the RTTI data structures.  For foreign enumerations
we need to perform a linear search at the corresponding points.  This
means that some RTTI operations related to deconstruction are more
expensive.

The dummy type optimisation is not applied to foreign enumerations as
the code generators currently initialise the arguments of non-builtin
dummy type foreign_proc arguments to zero.  For unit foreign enumerations
they should be initialised to the correct foreign value.  (This is could be
implemented but in practice it's probably not going to be worth it.)

Currently, foreign enumerations are only supported by the C backends.

compiler/prog_io_pragma.m:
	Parse foreign_enum pragmas.

	Generalise the code used to parse association lists of sym_names
	and strings since this is now used by the code to parse foreign_enum
	pragmas as well as that for foreign_export_enum pragmas.

	Fix a typo: s/foreign_expor_enum/foreign_export_enum/

compiler/prog_item.m:
	Represent foreign_enum pragmas in the parse tree.

compiler/prog_type.m:
	Add a new type category for foreign enumerations.

compiler/modules.m:
	Add any foreign_enum pragmas for enumeration types defined in the
	interface of a module to the interface files.

	Output foreign_import_module pragmas in the interface file
	if any foreign_enum pragmas are included in it.  This ensures that
	the contents that any foreign declarations that are needed by the
	foreign_enum pragmas are visible.

compiler/make_hlds_passes.m:
compiler/add_pragma.m:
	Add pragma foreign_enum items to the HLDS after all the types
	have been added.  As they are added, error check them.

	Change the constructor tag values of foreign enum types to their
	foreign values.

compiler/module_qual.m:
	Module qualify pragma foreign_enum items.

compiler/mercury_to_mercury.m:
	Output foreign_enum pragmas.

	Generalise some of the existing code for writing out association
	lists in foreign_export_enum pragmas for use with foreign_enum
	pragmas as well.

compiler/hlds_data.m:
	Add the alternative `is_foreign_type' to the type enum_or_dummy/0.

	Add new type of cons_tag, foreign_tag, whose values are directly
	embedded in the target language.

compiler/intermod.m:
	Write out any foreign_enum pragmas for opt_exported types.
	(The XXX concerning attaching language information to foreign tags
	will be addressed in a subsequent change.)

compiler/llds.m:
compiler/mlds.m:
	Support new kinds of rval constants: llconst_foreign and
	mlconst_foreign respectively.  Both of these represent tag values
	as strings that are intended to be directly embedded in the target
	language.

compiler/llds_out.m:
	Add code to write out the new kind of rval_const.

	s/Integer/MR_Integer/ in a spot.
	s/Float/MR_Float/ in a spot.

compiler/rtti.m:
compiler/rtti_out.m:
compiler/rtti_to_mlds.m:
compiler/type_ctor_info.m:
	Add support the RTTI required by foreign enums.

compiler/switch_util.m:
	Handle switches on foreign_enums as-per normal enumerations.

compiler/table_gen.m:
	Tabling of foreign_enums is also like normal enumerations.

compiler/type_util.m:
	Add a predicate that tests whether a type is a foreign enumeration.

compiler/unify_gen.m:
compiler/unify_proc.m:
compiler/ml_unify_gen.m:
	Handle unification and comparison of foreign enumeration values.
	They are treated like normal enumerations for the purposes of
	implementing these operations.

compiler/ml_type_gen.m:
	Handle foreign enumerations when generating the MLDS representation
	of enumerations.

compiler/ml_util.m:
	Add a function to create an initializer for an object with a
	foreign tag.

compiler/mlds_to_c.m:
	Handle mlconst_foreign/1 rval constants.

compiler/bytecode_gen.m:
compiler/dupproc.m:
compiler/erl_rtti.m:
compiler/exception_analysis.m:
compiler/export.m:
compiler/exprn_aux.m:
compiler/global_data.m:
compiler/hlds_out.m:
compiler/higher_order.m:
compiler/inst_match.m:
compiler/jumpopt.m:
compiler/llds_to_x86_64.m:
compiler/ml_code_util.m:
compiler/mlds_to_gcc.m:
compiler/mlds_to_il.m:
compiler/mlds_to_java.m:
compiler/mlds_to_managed.m:
compiler/opt_debug.m:
compiler/opt_util.m:
compiler/polymorphism.m:
compiler/recompilation.version.m:
compiler/term_norm.m:
compiler/trailing_analysis.m:
	Conform to the above changes.

doc/reference_manual.texi:
	Document the new pragma.

	Fix some typos: s/pramga/pragma/, s/behavior/behaviour/

library/construct.m:
	Handle the two new type_ctor reps.

	Break an over-long line.

library/rtti_implementation.m:
	Support the two new type_ctor reps.
	(XXX The Java versions of some of this cannot be implemented until
	support for foreign enumerations is added to mlds_to_java.m.)

	Reformat the inst usereq/0 and extend it to include foreign enums.

runtime/mercury_type_info.h:
	Add two new type_ctor reps.  One for foreign enumerations and
	another for foreign enumerations with user equality.

	Define new types (and extend existing ones) in order to support
	RTTI for foreign enumerations.

runtime/mercury_unify_compare_body.h:
	Implement generic unify and compare for foreign enumerations.
	(It is the same as that for regular enumerations.)

runtime/mercury_construct.[ch]:
runtime/mercury_deconstruct.h:
	Handle (de)construction of foreign enumeration values.

runtime/mercury_deep_copy_body.h:
	Implement deep copy for foreign enumerations.

runtime/mercury_table_type_body.h:
runtime/mercury_term_size.c:
	Handle the new type_ctor representations.

java/runtime/ForeignEnumFunctorDesc.java:
	Add a Java version of the MR_ForeignEnumFuntorDesc structure.
	(Note: this is untested, as the java grade runtime doesn't work
	anyway.)

java/runtime/TypeFunctors.java:
	Add a constructor method for foreign enumerations.
	(Likewise, untested.)

NEWS:
	Announce pragma foreign_enum.

vim/syntax/mercury.vim:
	Highlight the new pragma appropriately.

tests/hard_coded/.cvsignore:
	Ignore executables generated by the new tests.

	Ignore a bunch of other files create by the Mercury compiler.

tests/hard_coded/Mmakefile:
tests/hard_coded/foreign_enum_rtti.{m,exp}:
	Test RTTI for foreign enumerations.

tests/hard_coded/foreign_enum_dummy.{m,exp}:
	Check that dummy type optimisation is disabled for foreign
	enumerations.

tests/hard_coded/Mercury.options:
tests/hard_coded/foreign_enum_mod1.{m,exp}:
tests/hard_coded/foreign_enum_mod2.m:
	Test that foreign_enum pragmas are hoisted into interface files
	and that they are handled correctly in optimization interfaces.

tests/invalid/Mercury.options:
tests/invalid/Mmakefile:
tests/invalid/foreign_enum_import.{m,err_exp}:
tests/invalid/foreign_enum_invalid.{m,err_exp}:
	Test that errors in foreign_enum pragmas are reported.

tests/tabling/Mmakefile:
tests/hard_coded/table_foreign_enum.{m,exp}:
	Test case for tabling of foreign enumerations.
2007-08-20 03:39:31 +00:00
Peter Wang
8396edc4b2 Let benchmark.report_stats/0 report real times on POSIX platforms.
Estimated hours taken: 1
Branches: main

Let benchmark.report_stats/0 report real times on POSIX platforms.

configure.in:
	Check for time.h and gettimeofday().

runtime/mercury_conf.h.in:
	Add MR_HAVE_TIME_H, MR_HAVE_GETTIMEOFDAY.

	Unrelated change: add MR_HAVE_PTHREAD_H.

runtime/mercury_timing.c:
runtime/mercury_timing.h:
	Add `MR_get_real_milliseconds'.

runtime/mercury_wrapper.c:
runtime/mercury_wrapper.h:
	Rename MR_time_* globals to MR_user_time_*.

	Add and initialise MR_real_time_* globals.

library/benchmarking.m:
	Output real times in ML_report_stats().

	Correct spelling of milliseconds.

java/runtime/Native.c:
java/runtime/Native.java.in:
trace/mercury_trace_declarative.c:
	Correct spelling of milliseconds.
2006-08-07 06:21:32 +00:00
Zoltan Somogyi
d609181cb9 Consider types of the form
Estimated hours taken: 30
Branches: main

Consider types of the form

	:- type x ---> f.

to be dummy types, since they contain no information. Optimize them the same
way we currently optimize io.state and store.store.

runtime/mercury_type_info.h:
	Add a new type_ctor_rep for dummy types.

runtime/mercury_tabling.h:
	Add a representation for "tabled" dummy types, which don't actually
	have a level in the trie, so that the runtime system can handle that
	fact.

runtime/mercury_ml_expand_body.h:
	When deconstructing a value of a dummy type, ignore the actual value
	(since it will contain garbage) and instead return the only possible
	value of the type.

runtime/mercury_construct.c:
runtime/mercury_deconstruct.c:
runtime/mercury_deep_copy_body.c:
runtime/mercury_tabling.c:
runtime/mercury_unify_compare_body.h:
library/rtti_implementation.m:
	Handle the type_ctor_rep of dummy types.

runtime/mercury_builtin_types.c:
	Provide a place to record profiling information about unifications and
	comparisons for dummy types.

runtime/mercury_mcpp.h:
java/runtime/TypeCtorRep.java:
library/private_builtin.m:
	Add a new type_ctor_rep for dummy types, and fix some previous
	discrepancies in type_ctor_reps.

mdbcomp/prim_data.m:
	Move a bunch of predicates for manipulating special_pred_ids here from
	the browser and compiler directories.

	Rename the function symbols of the special_pred_id type to avoid the
	need to parenthesize the old `initialise' function symbol.

	Convert to four-space indentation.

mdbcomp/rtti_access.m:
	Don't hardcode the names of special preds: use the predicates in
	prim_data.m.

	Convert to four-space indentation.

browser/declarative_execution.m:
	Delete some predicates whose functionality is now in
	mdbcomp/prim_data.m.

compiler/hlds_data.m:
	Replace the part of du type that says whether a type an enum, which
	used to be a bool, with something that also says whether the type is a
	dummy type.

	Convert to four-space indentation.

compiler/make_tags.m:
	Compute the value for the new field of du type definitions.

compiler/hlds_out.m:
	Write out the new field of du type definitions.

compiler/rtti.m:
	Modify the data structures we use to create type_ctor_infos to allow
	for dummy types.

	Convert to four-space indentation.

compiler/type_ctor_info.m:
	Modify the code that generates type_ctor_infos to handle dummy types.

compiler/type_util.m:
	Provide predicates for recognizing dummy types.

	Convert to four-space indentation.

compiler/unify_proc.m:
	Generate the unify and compare predicates of dummy types using a new
	code scheme that avoids referencing arguments that contain garbage.

	When generating code for unifying or comparing other types, ignore
	any arguments of function symbols that are dummy types.

	Don't use DCG style access predicates.

compiler/higher_order.m:
	Specialize the unification and comparison of values of dummy types.

	Break up an excessively large predicate, and factor out common code
	from the conditions of a chain of if-then-elses.

compiler/llds.m:
	For each input and output of a foreign_proc, include a field saying
	whether the value is of a dummy type.

compiler/pragma_c_gen.m:
	Fill in the new fields in foreign_proc arguments.

compiler/hlds_goal.m:
	Rename some predicates for constructing unifications to avoid
	unnecessary ad-hoc overloading. Clarify their documentation.

	Rename a predicate to make clear the restriction on its use,
	and document the restriction.

	Add a predicate for creating simple tests.

	Add a utility predicate for setting the context of a goal directly.

compiler/modules.m:
	Include dummy types interface files, even if they are private to the
	module. This is necessary because with the MLDS backend, the generated
	code inside the module and outside the module must agree whether a
	function returning a value of the type returns a real value or a void
	value, and this requires them to agree on whether the type is dummy
	or not.

	The impact on interface files is minimal, since very few types are
	dummy types, and changing a type from a dummy type to a non-dummy type
	or vice versa is an ever rarer change.

compiler/hlds_pred.m:
	Provide a representation in the compiler of the trie step for dummy
	types.

compiler/layout_out.m:
	Print the trie step for dummy types.

compiler/table_gen.m:
	Don't table values of dummy types, and record the fact that we don't
	by including a dummy trie step in the list of trie steps.

compiler/add_pragma.m:
compiler/add_special_pred.m:
compiler/add_type.m:
compiler/aditi_builtin_ops.m:
compiler/bytecode.m:
compiler/bytecode_gen.m:
compiler/code_gen.m:
compiler/code_info.m:
compiler/continuation_info.m:
compiler/cse_detection.m:
compiler/det_report.m:
compiler/exception_analysis.m:
compiler/inst_match.m:
compiler/livemap.m:
compiler/llds_out.m:
compiler/llds_out.m:
compiler/middle_rec.m:
compiler/ml_call_gen.m:
compiler/ml_closure_gen.m:
compiler/ml_code_gen.m:
compiler/ml_code_util.m:
compiler/ml_type_gen.m:
compiler/ml_unify_gen.m:
compiler/mlds_to_c.m:
compiler/mlds_to_gcc.m:
compiler/mlds_to_il.m:
compiler/mlds_to_il.m:
compiler/modecheck_unify.m:
compiler/modes.m:
compiler/opt_util.m:
compiler/post_term_analysis.m:
compiler/post_typecheck.m:
compiler/qual_info.m:
compiler/rl.m:
compiler/rl_exprn.m:
compiler/rl_key.m:
compiler/rtti_out.m:
compiler/simplify.m:
compiler/size_prof.m:
compiler/term_constr_initial.m:
compiler/term_constr_util.m:
compiler/term_norm.m:
compiler/termination.m:
compiler/trace.m:
compiler/typecheck.m:
compiler/unify_gen.m:
	Conform to the changes above.

compiler/export.m:
compiler/exprn_aux.m:
compiler/foreign.m:
compiler/polymorphism.m:
compiler/proc_label.m:
compiler/rtti_to_mlds.m:
compiler/special_pred.m:
compiler/stack_alloc.m:
compiler/stack_layout.m:
compiler/state_var.m:
compiler/switch_util.m:
compiler/trace_params.m:
	Conform to the changes above.

	Convert to four-space indentation.

compiler/mlds_to_java.m:
compiler/var_locn.m:
	Conform to the changes above, which requires threading the module_info
	through the module.

	Convert to four-space indentation.

compiler/mercury_compile.m:
	Pass the module_info to mlds_to_java.m.

compiler/ml_util.m:
compiler/polymorphism.m:
compiler/type_ctor_info.m:
compiler/type_util.m:
	Delete some previously missed references to the temporary types used
	to bootstrap the change to the type_info type's arity.

compiler/polymorphism.m:
	Turn back on an optimization that avoids passing parameters (such as
	type_infos) to foreign_procs if they are not actually referred to.

compiler/prog_data.m:
	Convert to four-space indentation.

library/svvarset.m:
	Add a missing predicate.

trace/mercury_trace.c:
	Delete the unused function that used to check for dummy types.

tests/debugger/field_names.{m,inp,exp}:
	Add to this test case a test of the handling of dummy types. Check that
	their values can be printed out during normal execution, and that the
	debugger doesn't consider them live nondummy variables, just as it
	doesn't consider I/O states live nondummy variables.
2005-10-05 06:34:27 +00:00
Julien Fischer
be87bc1cfa Make cvs ignore *.so *.pic_o files in this directory.
java/runtime/.cvsignore:
	Make cvs ignore *.so *.pic_o files in this directory.
2004-09-28 07:20:53 +00:00
Zoltan Somogyi
e4b0328ade Provide a mechanism for declaring foreign types that can be operated on by
Estimated hours taken: 4
Branches: main

Provide a mechanism for declaring foreign types that can be operated on by
compare_representation. The intended use of the mechanism is to declare
a foreign type to represent proc_layouts in the declarative debugger,
as part of the representation of atoms; since atoms are keys in maps
in the oracle, they are input to compare_representation.

Since we don't want the result of compare_representation to change as
execution proceeds, we require an assertion that the foreign value is
stable, i.e. that the value of the foreign type variable completely
determines the data it points to, directly and indirectly. Proc_layouts
are static, so this is not a problem. Being able to do the comparison
requires the foreign type to be an integral type or a pointer, which
is what the existing can_pass_as_mercury_type assertion promises.
The stability is promised by a new assertion.

For foreign types that have both assertions, we use a new type_ctor_rep,
which differs from the existing type_ctor_rep for foreign types by doing
a real comparison instead of an abort in compare_representation.

doc/reference_manual.texi:
	Document the new kind of assertion.

compiler/prog_data.m:
	Add the new kind of assertion.

compiler/prog_io_pragma.m:
	Parse the new kind of assertion.

compiler/rtti.m:
	Add the representation of the new type_ctor_rep. Factor out the
	stability of c_pointers as well as foreign types.

compiler/type_ctor_info.m:
	Generate the new type_ctor_rep for types with both assertions.

compiler/foreign.m:
	Export a predicate for use by type_ctor_info.m.

compiler/mercury_to_mercury.m:
	Print the new assertion.

compiler/*.m:
	Minor changes to conform to the diffs above.

	Use state variable notation.

library/rtti_implementation.m:
	Handle the new type_ctor_rep.

runtime/mercury_mcpp.h:
runtime/mercury_type_info.h:
java/runtime/TypeCtorRep.java:
	Add the new type_ctor_rep to the runtime.

runtime/mercury_mcpp.h:
runtime/mercury_type_info.h:
compiler/type_ctor_info.m:
	Increment the rtti version number.

	When we rely on the availability of this new capability,
	we should add a test for the new rtti version number to configure.in.

runtime/mercury_construct.c:
runtime/mercury_deconstruct.c:
runtime/mercury_ml_expand_body.h:
runtime/mercury_term_size.h:
	Handle stable foreign types the same way as other foreign types.

runtime/mercury_deep_copy_body.h:
runtime/mercury_tabling.h:
runtime/mercury_unify_compare_body.h:
	Handle stable foreign types in a useful manner, relying on the
	assertions behind them.

tests/hard_coded/stable_foreign.{m,exp}:
	A test case for the handling of values of a stable foreign type.

tests/hard_coded/Mmakefile:
	Enable the new test case.
2004-06-28 04:50:10 +00:00
Julien Fischer
87165a6b87 Have CVS ignore various files generated by the java grade and
Estimated hours taken: 0.1
Branches: main

Have CVS ignore various files generated by the java grade and
some other bits and pieces in the browser directory.

browser/.cvsignore:
library/.cvsignore:
java/runtime/.cvsignore:
	Update these files.
2004-02-27 14:19:06 +00:00
James Goddard
1aa44ba2da Implement RTTI for the Java-backend (incomplete)
Estimated hours taken: 30
Branches: main

Implement RTTI for the Java-backend (incomplete)

java/runtime/PseudoTypeInfo.java:
java/runtime/TypeCtorInfo_Struct.java:
java/runtime/TypeInfo_Struct.java:
	Implement the `unify' operation for these classes.  This implementation
	actually just compares them.

library/rtti_implementation.m:
	Document a bug in notag_functor_arg_type/1

	Define the foreign type of `type_layout' as `TypeLayout' in Java.

	Correct an off-by-one error in type_ctor_and_args/3.

	Implement deconstruct/8 for TypeCtorRep = enum.

	Implement the following predicates in Java:
		get_var_arity_typeinfo_arity/1
		get_pti_from_arg_types/2
		typeinfo_is_variable/2
		get_type_ctor_info/1
		get_primary_tag/1
		get_remote_secondary_tag/1
		ptag_index/2
		sectag_locn/1
		du_sectag_alternatives/2
		type_info_index/2
		type_ctor_arity/1
		type_ctor_rep/1
		type_ctor_module_name/1
		type_ctor_name/1
		type_ctor_functors/1
		type_layout/1
		unsafe_cast/1
		du_functor_desc/3
		du_functor_name/1
		du_functor_arity/1
		du_functor_arg_type_contains_var/1
		du_functor_sectag_locn/1
		du_functor_primary/1
		du_functor_secondary/1
		du_functor_ordinal/1
		du_functor_arg_types/1
		du_functor_arg_names/1
		du_functor_exist_info/1
		enum_functor_desc/3
		enum_functor_name/1
		enum_functor_ordinal/1
		notag_functor_desc/3
		notag_functor_name/1
		notag_functor_arg_type/1
		notag_functor_arg_name/1
		null/1
		null_string/0
		unsafe_get_enum_value/1

library/type_desc.m:
	Implement the following procedures for Java:
		type_of/1
		has_type/1
		type_ctor_and_args/3
		type_ctor_name_and_arity/4

	Implement the Java classes `type_desc_0' and `type_ctor_desc_0'.
2004-02-26 08:17:54 +00:00