mirror of
https://github.com/Mercury-Language/mercury.git
synced 2025-12-14 21:35:49 +00:00
Estimated hours taken: 36
Switch to a closure representation that includes runtime type and procedure id
information, so that closures can be copied, garbage collected, printed, etc.
This RTTI information is not yet used. Adding code to use it would be futile
until Tyson finishes his changes to the other RTTI data structures.
Note also that this change provides the information required for solving the
problem of trying to deep copy closures only for grades that include
--typeinfo-liveness. Providing this info for other grades is future work.
configure.in:
Find out what the right way to refer to a variable-sized array
at the end of a struct is.
runtime/mercury_ho_call.h:
New file to define the structure of closures and macros for accessing
closures.
runtime/Mmakefile:
Add the new header file.
runtime/mercury_ho_call.c:
Add an entry point to handle calls to new-style closures. The code
to handle old-style closures, which was unnecessarily duplicated for
each code model, stays until all the installed compilers use the new
closure representation.
Until that time, the new entry point will contain code to detect
the use of old-style closures and invoke the old code instead.
This allows stage1s compiled with old compilers to use the old style
and stage2 to use the new style without any special tricks anywhere
else.
Add a new entry point to handle method calls of all code models.
The old entry points, which had the same code, will also be deleted
after this change has been bootstrapped.
runtime/mercury_calls.h:
Remove the macros that call closures. Their interface sucked, they
were not used, and their implementation is now out of date.
runtime/mercury_stack_layout.h:
Add a new type, MR_Type_Param_Locns, for use by the C type
representing closures. Since MR_Stack_Layout_Vars has a field,
MR_slvs_tvars, which references a data structure identical
in every way to MR_Type_Param_Locns, change the type of that field
to this new type, instead of the previous cheat.
runtime/mercury_layout_util.h:
Minor update to conform to the new type of the MR_slvs_tvars field.
(This is the only use of that field in the system.)
runtime/mercury_type_info.h:
Add new types MR_TypeInfo and MR_PseudoTypeInfo. For now, they
are just Word, but later we can make them more accurate.
In the meantime, we can refer to them instead of to Word,
making code clearer. One such reference is now in mercury_ho_call.h.
compiler/notes/release_checklist.html:
Add a reminder to remove the redundant code from mercury_ho_call.c
after bootstrapping.
compiler/llds.m:
Replace three code addresses for calling closures and another three
for calling methods with one each.
compiler/call_gen.m:
compiler/dupelim.m:
compiler/opt_debug.m:
compiler/opt_util.m:
compiler/llds_out.m:
Trivial updates in accordance with the change to llds.m
compiler/code_info.m:
Move the code to handle layouts to continuation_info.m,
since that's where it belongs. Leave only the code for picking
up parameters from code_infos and for putting results back in there.
Remove the redundant arguments of code_info__init, and extract
them from ProcInfo, to make clear that they are related.
compiler/code_gen.m:
Since we pass ProcInfo to code_info__init, don't pass its components.
compiler/continuation_info.m:
Add the code moved from code_info.m, in a form which takes explicit
arguments for things that used be hidden in the code_info.
Add new code, closely related to the moved code, that creates
layout info from a procedure's argument info, rather than from a
(part of) the current code generator state. This way, it can be
invoked from places that don't have a code_info for the procedure
for which they want to generate layouts. This is the case when
we generate layouts for closures.
compiler/par_conj_gen.m:
compiler/trace.m:
Minor changes required by the move of stuff from code_info to
continuation_info.
compiler/stack_layout.m:
Export some predicates for use by unify_gen.
compiler/unify_gen.m:
Switch to creating new style closures, complete with layout info.
Optimize the code for extending closures a bit. By copying the
fixed words of the closure outside the loop, we avoid incurring
the loop overhead twice.
compiler/code_util.m:
Add a couple of utility predicates for continuation_info.m and
unify_gen.m
library/benchmarking.m:
library/std_util.m:
Refer to the new entry point for handling closures.
browser/dl.m:
Use the new closure representation.
Note that extras/dynamic_linking/dl.m, which is supposed to be
the same as browser/dl.m but is not, should also be updated, but
this will be handled later by Fergus.
tests/hard_coded/closure_extension.{m,exp}:
A new test case to exercise the code for extending closures.
tests/hard_coded/Mmakefile:
Enable the new test case.
247 lines
7.1 KiB
Mathematica
247 lines
7.1 KiB
Mathematica
%-----------------------------------------------------------------------------%
|
|
% Copyright (C) 1998-1999 The University of Melbourne.
|
|
% This file may only be copied under the terms of the GNU Library General
|
|
% Public License - see the file COPYING.LIB in the Mercury distribution.
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
% File: dl.m.
|
|
% Purpose: dynamic linking support.
|
|
% Main author: fjh.
|
|
% Stability: medium.
|
|
|
|
% This file provides an interface to the C functions dlopen(), dlsym(),
|
|
% and dlclose(). For details about the behaviour of those procedures,
|
|
% see the documentation for those procedures (i.e. `man dlopen').
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
:- module dl.
|
|
:- interface.
|
|
:- import_module io.
|
|
:- import_module name_mangle.
|
|
|
|
:- type (mode) ---> lazy ; now. % RTLD_LAZY or RTLD_NOW
|
|
:- type scope ---> local ; global. % RTLD_GLOBAL or not.
|
|
:- type handle.
|
|
:- type result(T) ---> ok(T) ; error(string).
|
|
:- type result ---> ok ; error(string).
|
|
|
|
% interface to the C function dlopen()
|
|
:- pred dl__open(string::in, (mode)::in, scope::in, dl__result(handle)::out,
|
|
io__state::di, io__state::uo) is det.
|
|
|
|
% low-level interface to the C function dlsym() -- returns a c_pointer.
|
|
:- pred dl__sym(handle::in, string::in, dl__result(c_pointer)::out,
|
|
io__state::di, io__state::uo) is det.
|
|
|
|
% high-level interface to the C function dlsym().
|
|
% This version returns a higher-order predicate or function term.
|
|
% The user must use an inst cast (implemented using pragma c_code)
|
|
% to cast this term to the appropriate higher-order inst before calling
|
|
% it; see dl_test.m for an example of this.
|
|
%
|
|
% The type `T' below must be a higher-order type whose arity and
|
|
% argument types match that of the specified procedure.
|
|
% The implementation may check this at runtime, but is not required
|
|
% to do so. (The current implementation checks that the type is a
|
|
% higher-order type with the appropriate arity, but it does not
|
|
% check the argument types.)
|
|
:- pred dl__mercury_sym(handle::in, mercury_proc::in, dl__result(T)::out,
|
|
io__state::di, io__state::uo) is det.
|
|
|
|
% interface to the C function dlclose()
|
|
:- pred dl__close(handle::in, dl__result::out,
|
|
io__state::di, io__state::uo) is det.
|
|
|
|
:- implementation.
|
|
:- import_module std_util, require, string, list.
|
|
|
|
:- pragma c_header_code("
|
|
#include <stdio.h>
|
|
#include ""mercury_conf.h""
|
|
#ifdef HAVE_DLFCN_H
|
|
#include <dlfcn.h>
|
|
#endif
|
|
").
|
|
|
|
:- type handle ---> handle(c_pointer).
|
|
|
|
:- pred is_null(c_pointer::in) is semidet.
|
|
:- pragma c_code(is_null(Pointer::in),
|
|
[will_not_call_mercury, thread_safe],
|
|
"SUCCESS_INDICATOR = ((void *)Pointer == NULL)").
|
|
|
|
open(FileName, Mode, Scope, Result) -->
|
|
dlopen(FileName, Mode, Scope, Pointer),
|
|
( { is_null(Pointer) } ->
|
|
dlerror(ErrorMsg),
|
|
{ Result = error(ErrorMsg) }
|
|
;
|
|
{ Result = ok(handle(Pointer)) }
|
|
).
|
|
|
|
/*
|
|
** Note that dlopen() may call startup code (e.g. constructors for global
|
|
** variables in C++) which may end up calling Mercury, so it's not safe
|
|
** to declare this as `will_not_call_mercury'.
|
|
*/
|
|
|
|
:- pred dlopen(string::in, (mode)::in, scope::in, c_pointer::out,
|
|
io__state::di, io__state::uo) is det.
|
|
:- pragma c_code(dlopen(FileName::in, Mode::in, Scope::in, Result::out,
|
|
_IO0::di, _IO::uo), [], "
|
|
{
|
|
#if defined(HAVE_DLFCN_H) && defined(HAVE_DLOPEN) \
|
|
&& defined(RTLD_NOW) && defined(RTLD_LAZY)
|
|
int mode = (Mode ? RTLD_NOW : RTLD_LAZY);
|
|
/* not all systems have RTLD_GLOBAL */
|
|
#ifdef RTLD_GLOBAL
|
|
if (Scope) mode |= RTLD_GLOBAL;
|
|
#endif
|
|
Result = (Word) dlopen(FileName, mode);
|
|
#else
|
|
Result = (Word) NULL;
|
|
#endif
|
|
}").
|
|
|
|
:- type closure_layout
|
|
---> closure_layout(
|
|
int,
|
|
string,
|
|
string,
|
|
string,
|
|
int,
|
|
int,
|
|
int
|
|
).
|
|
|
|
:- type closure
|
|
---> closure(
|
|
closure_layout,
|
|
c_pointer,
|
|
int
|
|
).
|
|
|
|
mercury_sym(Handle, MercuryProc0, Result) -->
|
|
{ check_proc_spec_matches_result_type(Result, _,
|
|
MercuryProc0, MercuryProc) },
|
|
{ MangledName = proc_name_mangle(MercuryProc) },
|
|
sym(Handle, MangledName, Result0),
|
|
{
|
|
Result0 = error(Msg),
|
|
Result = error(Msg)
|
|
;
|
|
Result0 = ok(Address),
|
|
%
|
|
% convert the procedure address to a closure
|
|
%
|
|
NumCurriedInputArgs = 0,
|
|
ClosureLayout = closure_layout(0, "unknown", "unknown",
|
|
"unknown", -1, -1, -1),
|
|
Closure = closure(ClosureLayout, Address, NumCurriedInputArgs),
|
|
private_builtin__unsafe_type_cast(Closure, Value),
|
|
Result = ok(Value)
|
|
}.
|
|
|
|
%
|
|
% Check that the result type matches the information
|
|
% in the procedure specification.
|
|
%
|
|
:- pred check_proc_spec_matches_result_type(dl__result(T)::unused, T::unused,
|
|
mercury_proc::in, mercury_proc::out) is det.
|
|
check_proc_spec_matches_result_type(_Result, Value, Proc0, Proc) :-
|
|
Proc0 = mercury_proc(IsPredOrFunc, _Module, _Name, ProcArity, _Mode),
|
|
type_ctor_name_and_arity(type_ctor(type_of(Value)),
|
|
TypeModule, TypeName, TypeArity),
|
|
(
|
|
( TypeModule \= "builtin"
|
|
; TypeName \= "pred", TypeName \= "func"
|
|
)
|
|
->
|
|
error(
|
|
"dl__mercury_sym: result type is not a higher-order type")
|
|
;
|
|
IsPredOrFunc = predicate, TypeName \= "pred"
|
|
->
|
|
string__append(
|
|
"dl__mercury_sym: predicate/function mismatch: ",
|
|
"argument is a predicate, result type is a function",
|
|
Msg),
|
|
error(Msg)
|
|
;
|
|
IsPredOrFunc = function, TypeName \= "func"
|
|
->
|
|
string__append(
|
|
"dl__mercury_sym: predicate/function mismatch: ",
|
|
"argument is a function, result type is a predicate",
|
|
Msg),
|
|
error(Msg)
|
|
;
|
|
ProcArity \= TypeArity
|
|
->
|
|
string__int_to_string(ProcArity, ProcArityString),
|
|
string__int_to_string(TypeArity, TypeArityString),
|
|
string__append_list([
|
|
"dl__mercury_sym: arity mismatch: ",
|
|
"argument has ", ProcArityString, " argument(s), ",
|
|
"result type has ", TypeArityString, " arguments(s)"],
|
|
Msg),
|
|
error(Msg)
|
|
;
|
|
Proc = Proc0
|
|
).
|
|
|
|
sym(handle(Handle), Name, Result) -->
|
|
dlsym(Handle, Name, Pointer),
|
|
( { is_null(Pointer) } ->
|
|
dlerror(ErrorMsg),
|
|
{ Result = error(ErrorMsg) }
|
|
;
|
|
{ Result = ok(Pointer) }
|
|
).
|
|
|
|
:- pred dlsym(c_pointer::in, string::in, c_pointer::out,
|
|
io__state::di, io__state::uo) is det.
|
|
:- pragma c_code(dlsym(Handle::in, Name::in, Pointer::out,
|
|
_IO0::di, _IO::uo), [will_not_call_mercury], "
|
|
{
|
|
#if defined(HAVE_DLFCN_H) && defined(HAVE_DLSYM)
|
|
Pointer = (Word) dlsym((void *) Handle, Name);
|
|
#else
|
|
Pointer = (Word) NULL;
|
|
#endif
|
|
}").
|
|
|
|
:- pred dlerror(string::out, io__state::di, io__state::uo) is det.
|
|
:- pragma c_code(dlerror(ErrorMsg::out, _IO0::di, _IO::uo),
|
|
[will_not_call_mercury], "
|
|
{
|
|
const char *msg;
|
|
|
|
#if defined(HAVE_DLFCN_H) && defined(HAVE_DLERROR)
|
|
msg = dlerror();
|
|
if (msg == NULL) msg = """";
|
|
#else
|
|
make_aligned_string(msg, ""sorry, not implemented: ""
|
|
""dynamic linking not supported on this platform"");
|
|
#endif
|
|
|
|
make_aligned_string_copy(ErrorMsg, msg);
|
|
}").
|
|
|
|
close(handle(Handle), Result) -->
|
|
dlclose(Handle),
|
|
dlerror(ErrorMsg),
|
|
{ Result = (if ErrorMsg = "" then ok else error(ErrorMsg)) }.
|
|
|
|
/*
|
|
** Note that dlclose() may call finalization code (e.g. destructors for global
|
|
** variables in C++) which may end up calling Mercury, so it's not safe
|
|
** to declare this as `will_not_call_mercury'.
|
|
*/
|
|
:- pred dlclose(c_pointer::in, io__state::di, io__state::uo) is det.
|
|
:- pragma c_code(dlclose(Handle::in, _IO0::di, _IO::uo), [], "
|
|
#if defined(HAVE_DLFCN_H) && defined(HAVE_DLCLOSE)
|
|
dlclose((void *)Handle)
|
|
#endif
|
|
").
|