mirror of
https://github.com/Mercury-Language/mercury.git
synced 2026-04-19 11:23:46 +00:00
compiler/decide_type_repn.m:
Previously, this module computed type_repn items to put into .int3 files
for a subset of the type constructors defined in the current module:
the direct_dummy, enum and notag types (the *simple* types),
and the du types whose representation is guaranteed to be
a word-aligned pointer when targeting C. (We care about pointers
being word-aligned only when applying the direct arg optimization.
This optimization is applicable only with the low level data
representation, which we use only when targeting C.)
This diff adds code to decide the representations of *all* the
type constructors defined in the current module.
This code is based on the existing code in du_type_layout.m,
which it is intended to eventually replace, but its job is more general,
because it decides the representation of each type not just for
one platform (the one we want to generate code), but for all possible
platforms. This is because we want to put the descriptions of type
representations into the module's .int file to serve as a single source
of truth for all modules that use the types defined in this module,
and the contents of .int files should be platform-independent.
For our purposes, there are six kinds of platforms, which are
distinguished along three axes: 64 vs 32 bit machines, spf vs non-spf
grades, and direct arg optimization enabled vs disabled. That is eight
combinations, but on 64 bit machines, a float takes up one word whether
that float is single or double precision, so two combinations aren't valid.
Some of the change to this module consists of generalizing the existing
code so that it can decide simple types not just when targeting .int3 files
but .int files as well. However, the bulk of it is code for deciding
the representations of non-simple types. The code is not lifted straight
from du_type_layout.m. There are two main kinds of changes.
First, I took the opportunity to simplify the algorithms used.
For example, while du_type_layout.m passes over each function symbol
in the most general kind of type twice: once to assign it a cons_tag,
and once to decide how to pack its arguments, the code here does both jobs
in one pass. Another example is that for historical reasons,
du_type_layout.m computed the amount of space needed for an argument
in one place for sub-word-sized arguments, and in another place
for more-than-word-sized arguments; decide_type_repn.m does it all
in one place.
Second, since we compute a representation for each type six times,
I tried to avoid obvious inefficiencies, but only if the code
remained simple. In the future, we may want to use an approach
based on the idea that in the process of computing the first
representation, we look out for any indication that the representation
may be different on any of the other five platforms, and if not,
we just reuse the first representation on the other five platforms as well.
However, that would be appropriate only *after* we have a simpler
system that has proven to work in practice.
There is a third, smaller change: when deciding whether an argument
is packable, we take into account not just equivalence type
definitions, but the definitions of notag types as well.
This takes advantage of the fact that if a notag type is abstract
exported, its representation is put into the relevant .int3 file
even though its definition isn't. (This is why du_type_layout.m
couldn't "see through" notag types: it couldn't depend on knowing
which types were notags.)
compiler/prog_item.m:
Change the types we use for type representation information.
Their previous definitions baked in the assumption that the only
distinction between platforms that mattered was the 64 vs 32 bit
distinction, which is not the case.
Use a more consistent naming scheme for the types we use
to represent type representation information.
Include the "dereferenced" types of the arguments in functors'
representations. (I use "dereferencing" here to mean expanding
equivalence types and throwing away any notag wrappers.).
We don't need it when generating C code using the low level
data representation, but we do need it to create constructors
when generating e.g. Java code that uses the high level data
representation.
compiler/parse_type_repn.m:
Rewrite most of this module due to the changes in prog_item.m.
compiler/parse_tree_out_type_repn.m:
A new module containing the code for writing out type representations.
The original code used to be in parse_tree_out.m, but it has been
mostly rewritten. Partly this is due the changes in prog_item.m,
but partly it is to provide much more structured output for humans,
since this makes debugging so much easier.
compiler/parse_tree.m:
Add the new module to the parse_tree package.
compiler/parse_tree_out.m:
Delete the code moved to parse_tree_out_type_repn.m.
compiler/parse_tree_out_info.m:
Provide a mechanism for selecting between output for machines
(the default) and output for humans.
compiler/hlds_data.m:
compiler/prog_data.m:
Move the ptag type from hlds_data.m to prog_data.m, to make it
accessible in prog_item.m.
Add some documentation in prog_data.m.
compiler/comp_unit_interface.m:
If the experiment1 option is enabled, invoke decide_type_repn.m
to decide what type_repn items to put into the .int file we are
generating. Otherwise, maintain the status quo.
compiler/write_module_interface_files.m:
Pass the globals to comp_unit_interface.m so it can look up experiment1.
compiler/equiv_type.m:
Add a predicate for expanding equivalence types for use by
decide_type_repn.m. This predicate expands just one type,
but reports any use of circular equivalence types in that type.
Improve the error message for circular equivalence types by *naming*
the type constructors involved. To make this possible, pass around
sets of such type constructors instead of just a boolean saying
*whether* we have found *some* circular equivalence type.
Replace bools used as changed/unchanged flag with a bespoke type.
Standardize some variable names.
compiler/options.m:
Add the developer-only option --pack-everything, which, if set,
tells decide_type_repn.m to turn on all the packing options
that currently work. This is to allow the testing of decide_type_repn.m
in the eventual intended mode of operation, even if the various
allow-packing-... options used by du_type_layout.m are set to "no".
compiler/disj_gen.m:
compiler/equiv_type_hlds.m:
compiler/llds_out_data.m:
compiler/lookup_util.m:
compiler/ml_call_gen.m:
compiler/ml_closure_gen.m:
compiler/mlds_to_c_data.m:
compiler/rtti.m:
compiler/rtti_out.m:
compiler/rtti_to_mlds.m:
compiler/tag_switch.m:
Conform to the changes above (mostly the move of ptag to prog_data.m.)
compiler/parse_pragma.m:
Improve indentation.
tests/valid_make_int/test_repn.m:
tests/valid_make_int/test_repn_sub.m:
A fairly comprehensive test case of the new functionality.
test_repn_sub.m defines one ore more simple type constructors
of each possible kind, and test_repn.m uses them to define types
that use each possible kind of complex type representation.
tests/valid_make_int/Mmakefile:
tests/valid_make_int/Mercury.options:
Enable the new test case.
161 lines
4.0 KiB
Mathematica
161 lines
4.0 KiB
Mathematica
%---------------------------------------------------------------------------%
|
|
% vim: ft=mercury ts=4 sw=4 et
|
|
%---------------------------------------------------------------------------%
|
|
|
|
:- module test_repn.
|
|
:- interface.
|
|
|
|
:- import_module test_repn_sub.
|
|
|
|
:- import_module char.
|
|
|
|
:- type only_functor_local1
|
|
---> ofl1_f1(
|
|
animal, d, int8, animal, uint32, animal
|
|
).
|
|
|
|
:- type only_functor_local2
|
|
---> ofl2_f1(
|
|
nt(d), animal, int8, uint16
|
|
).
|
|
|
|
:- type only_functor_remote1
|
|
---> ofr1_f1( % on 64 bit machines
|
|
animal, animal, d, % word 0
|
|
int, % word 1
|
|
string, % word 2
|
|
animal, int8, animal, uint32, animal % word 3
|
|
).
|
|
|
|
:- type only_functor_remote2
|
|
---> ofr2_f1( % on 64 bit machines
|
|
d, nt(animal), d, animal, d, % word 0
|
|
int, % word 1
|
|
string, % word 2
|
|
animal, int8, animal, uint32, animal % word 3
|
|
).
|
|
|
|
:- type more_functors_local1
|
|
---> mfl1_f1(
|
|
animal, int8, animal, uint32, animal
|
|
)
|
|
; mfl1_f2(
|
|
animal, animal
|
|
).
|
|
|
|
:- type more_functors_local2
|
|
---> mfl2_f1
|
|
; mfl2_f2
|
|
; mfl2_f3
|
|
; mfl2_f4(
|
|
animal, int8, animal, uint32, animal
|
|
)
|
|
; mfl2_f5(
|
|
animal, animal
|
|
).
|
|
|
|
:- type more_functors_remote1
|
|
---> mfr1_f1
|
|
; mfr1_f2
|
|
; mfr1_f3
|
|
; mfr1_f4(
|
|
int, int
|
|
)
|
|
; mfr1_f5(
|
|
int, float
|
|
).
|
|
|
|
:- type more_functors_local_remote1
|
|
---> mflr1_f1
|
|
; mflr1_f2
|
|
; mflr1_f3
|
|
; mflr1_f4(
|
|
char, animal
|
|
)
|
|
; mflr1_f5(
|
|
int, int
|
|
).
|
|
|
|
:- type more_functors_local_remote2
|
|
---> mflr2_f1
|
|
; mflr2_f2
|
|
; mflr2_f3
|
|
; mflr2_f4(
|
|
char, animal
|
|
)
|
|
; mflr2_f5(
|
|
float, int, float
|
|
).
|
|
|
|
:- type more_functors_local_remote3
|
|
---> mflr3_f1
|
|
; mflr3_f2
|
|
; mflr3_f3(
|
|
char, animal
|
|
)
|
|
; some [A, B] mflr3_f4(
|
|
int8, animal, d, A, B, float, int, float
|
|
)
|
|
; mflr3_f5(
|
|
int, int
|
|
)
|
|
; mflr3_f6(
|
|
int, int
|
|
)
|
|
; mflr3_f7(
|
|
int, int
|
|
)
|
|
; mflr3_f8(
|
|
int, int
|
|
)
|
|
; mflr3_f9(
|
|
int, int
|
|
)
|
|
; mflr3_f10(
|
|
int, int
|
|
)
|
|
; some [C, D, E] mflr3_f11(
|
|
nt(d), animal, char, C, D, E, int, int
|
|
)
|
|
; mflr3_f12(
|
|
eqv(nt(eqv(d))), animal, char, int, int
|
|
)
|
|
; mflr3_f13(
|
|
animal, nt(d), char, int, int
|
|
)
|
|
; mflr3_f13(
|
|
animal, char, nt(nt(d)), int, int
|
|
).
|
|
|
|
:- type more_functors_local_remote_da1
|
|
---> mflrd1_f1
|
|
; mflrd1_f2
|
|
; mflrd1_f3
|
|
; mflrd1_f5(
|
|
wa
|
|
)
|
|
; mflrd1_f6(
|
|
wa
|
|
)
|
|
; mflrd1_f7(
|
|
wa
|
|
)
|
|
; mflrd1_f8(
|
|
wa
|
|
)
|
|
; mflrd1_f9(
|
|
wa
|
|
)
|
|
; mflrd1_f10(
|
|
wa
|
|
)
|
|
; mflrd1_f11(
|
|
int, int
|
|
).
|
|
|
|
:- type abs.
|
|
:- pragma foreign_type("C", abs, "void *",
|
|
[can_pass_as_mercury_type, word_aligned_pointer]).
|
|
:- pragma foreign_type("C#", abs, "object",
|
|
[can_pass_as_mercury_type]).
|