Commit Graph

187 Commits (master)

Author SHA1 Message Date
Gabriel Scherer 67ba8c36cc matching: use toplevel_handler in for_tupled_function
This appears to change the function behavior with respect to the
Unused exception, but we believe that the change is correct. It makes
the code more consistent with other toplevel compilation functions.
2020-11-21 10:16:48 +01:00
Gabriel Scherer 7d777f20af matching: factorize the code handling toplevel matching failures 2020-11-21 10:16:48 +01:00
Gabriel Scherer def877e351 matching: [minor] inline the single-use split_and_precompile
This comes from a suggestion by Florian Angeletti in
  https://github.com/ocaml/ocaml/pull/9447#discussion_r408910756
2020-11-21 10:16:48 +01:00
Gabriel Scherer dacaddc265 matching: clarify the ~vars argument of Simple.explode_or_pat
(suggested by Thomas Refis' review)
2020-10-21 11:49:29 +02:00
Gabriel Scherer 0ab43f822a matching: simplify the interface of Simple.explode_or_pat 2020-10-21 11:49:29 +02:00
Gabriel Scherer cbf69c7f53 Matching.Simple.explode_or_pat: on-demand generation of fresh identifiers
This change was suggested by Thomas Refis during code review.
2020-10-21 11:49:29 +02:00
Gabriel Scherer 8fc38d247f add a comment to explain what Simple.explode_or_pat does 2020-10-21 11:49:29 +02:00
Gabriel Scherer 940149a45a matching: avoid useless bindings in do_for_multiple_match 2020-10-21 11:49:29 +02:00
Gabriel Scherer 7aa43acacd matching: towards a correctness argument for flatten_* ignoring variables
Before we ignored as-patterns in the flatten_* functions because
as-patterns would either be half-simplified or raise Cannot_flatten
(in any case, never reach the flattening functions).

Now the reasoning is a bit more subtle: the only non-simple matrices
we flatten are used as "ghost" information (default environments,
provenance) where variables do not matter, only the shape of matched values.
2020-10-21 11:49:29 +02:00
Gabriel Scherer c0d1e8157d Match.do_for_multiple_match: we can always flatten 2020-10-21 11:49:29 +02:00
Gabriel Scherer bd83d67d26 Matching.explode_or_pat: handle the case where the argument has no id 2020-10-21 10:08:48 +02:00
Gabriel Scherer cc462e0f76 matching: document the behavior of precompile_or through an example 2020-10-21 10:08:48 +02:00
Gabriel Scherer 82b29828d2
Allow `[@tailcall true]` and `[@tailcall false]` (#9754)
* remove the unused is_native_tail_call_heuristic forward reference

This forward-reference from Lambda to Asmcomp was used to generate
machine-specific tailcall information in -annot output; this only use
was removed in 57d329e07b, so we can now
remove it to simplify the codebase.

The logic was non-trivial and might be useful again in the future.

* [minor] testsuite: convert warnings/w51.ml to an expect-test

* [minor] translattribute: refactor attribute payload deconstruction

* [@tailcall false]: warn if the call *is* a tailcall

(+ constructor renaming suggested by Nicolás during review)

* Changes

* testsuite: add an example with the 'invalid payload' exception

(suggested by Nicolás during review)
2020-10-10 13:41:39 +02:00
Leo White fdbb4e201e Fix PR#7538 2020-09-18 09:38:08 +01:00
hhugo 49aa87c316
Introduce warning 68 to warn about hidden allocation due to pattern match of mutable field in curried functions (#9751)
Introduce new warning 68
2020-08-17 09:47:36 +01:00
Fourchaux 44e6cf4e0f
typos (#9806) 2020-07-28 12:22:03 +01:00
Nicolás Ojeda Bär 5789d6c6df Rename Expect_tailcall => Tailcall_expected 2020-07-20 08:31:18 +02:00
Gabriel Scherer fdf86ad403 matching: a first-order representation for match-failure handling 2020-07-08 22:38:47 +02:00
Gabriel Scherer 6aeed1c84d pass a proper location to Matching.for_trywith
Actually *using* the location in the failure handler would be
incorrect, as it adds noise to backtraces involving partial exception
handlers. This is now properly documented.

We still take the caller location (which is currently ignored) for
consistency with other toplevel Matching functions, and because that
may become useful if we want to add more error-reporting or warnings
in compile_matching.
2020-07-08 22:38:10 +02:00
Gabriel Scherer 50806ced7f
Merge pull request #9216 from gasche/lambda-duplicate
extend Lambda.subst on bound variables, add Lambda.duplicate
2020-07-02 22:55:49 +02:00
Jacques Garrigue 95a8fbfd5b change API for Env.open_signature to clarify errors 2020-06-23 16:36:55 +02:00
Nicolás Ojeda Bär 33416d11db
Merge pull request #9469 from lpw25/fix-lazy-backtraces
Better backtraces for lazy values
2020-06-14 11:34:04 +02:00
Leo White a02707c610 Better backtraces for lazy values 2020-06-14 07:57:35 +01:00
Gabriel Scherer b7509ca82f
Merge pull request #9442 from gasche/tailcall-attribute-refactoring
[minor] refactoring the datatype for the [@tailcall] attribute
2020-06-10 10:18:12 +02:00
Gabriel Scherer cc7d557b55 extend Lambda.subst on bound variables, add Lambda.duplicate
It is invalid to reuse a Lambda.t term twice, because bound variables
may be used non-uniquely. If we want to perform a code transformation
may duplicate subterms in some cases, we have to refresh all bound
variables of the copied subterm.

The present PR implements a function

    Lambda.duplicate : lambda -> lambda

that does exactly this. It is implemented by making Lambda.subst
parametrized over a transformation on bound variables.
2020-06-09 09:38:26 +02:00
Gabriel Scherer d260a79416 [refactoring] gives tailcall attributes a more standard structure
We want to start allowing more information in the payload of
[@tailcall] attributes (currently no payload is supported), for
example we could consider using [@tailcall false] to ask the code
generator to disable a tail call.

A first required step in this direction is to use a custom datatype to
represent the tail-call attribute, instead of a boolean. This is
consistent with the other application-site
attributes (inline_attribute, specialise_attribute, local_attribute),
so it makes the code more regular -- but the change itself is
boilerplate-y.
2020-06-08 15:39:50 +02:00
Gabriel Scherer 792deb120f [minor] printlambda: print the 'tailcall' attribute in the same style as others 2020-06-08 15:39:20 +02:00
Gabriel Scherer 68a3c8eef9 matching: [minor] refactor the local control flow of the Unused exception 2020-06-07 16:27:16 +02:00
Thomas Refis ddf93aa22c matching: try => match with exception
This makes it clearer where the exception comes from.
2020-06-06 18:46:34 +02:00
Thomas Refis e17f81511c matching: flatten_simple_pattern does not raise 2020-06-06 18:46:34 +02:00
Thomas Refis f491929b3b matching: push simple types to flatten_pattern
Unfortunately since the function is exposed and used in translcore we
need to keep the generic one, and introduce a flatten_simple_pattern.
2020-06-06 18:46:34 +02:00
Thomas Refis 2f57af2c40 matching: argo => arg_id 2020-06-05 23:07:16 +02:00
Xavier Leroy 4aa90e9784
Limit the number of parameters for an uncurried or untupled function (#9620)
This commit introduces a quantity Lambda.max_arity that is the maximal
number of parameters that a Lambda function can have.

Uncurrying is throttled so that, for example, assuming the limit is 10,
a 15-argument curried function fun x1 ... x15 -> e
becomes a 10-argument function (x1...x10) that returns a 5-argument
function (x11...x15).

Concerning untupling, a function that takes a N-tuple of arguments,
where N is above the limit, remains a function that takes a single
argument that is a tuple.

Currently, max_arity is set to 126 in native-code, to match the new
representation of closures implemented by #9619.  A signed 8-bit field
is used to store the arity.  126 instead of 127 to account for the
extra "environment" argument.

In bytecode the limit is infinity (max_int) because there are no needs
yet for a limit on the number of parameters.
2020-06-05 18:45:38 +02:00
Stephen Dolan 0d44a6cfe6 Remove Const_pointer from Lambda and Clambda (#9585)
Lambda and Clambda distinguish Const_int from Const_pointer only so
that they can pass the information to Cmm. But now that that
Const_pointer is gone from Cmm (#9578), there's no need for the
distinction in Lambda either.

This PR requires a bootstrap, because the .cmo format changes:
Lambda.structured_constant has one fewer constructor.  The bootstrap
is in the following commit.
2020-06-02 11:19:20 +02:00
Gabriel Scherer bf95a24739 Matching: propagate constructor descriptions in complete_pats_constrs
This simplifies this particular interface boundary between Matching
and Parmatch.

(Suggested by Florian Angeletti)
2020-05-26 15:47:41 +02:00
Gabriel Scherer d333ac83ec matching: use constructor descriptions instead of tags as matching keys
This loses no information (descriptions contain the tag), but it will
make it easier to obtain the descriptions inside `combine_constructor`
without doing a dynamic check on the patterns. This will in turn help
simplify the interaction with `Parmatch.complete_constrs`.

Note: after this patch we use `Types.equal_tag` instead of `( = )` to
compare tags during pattern-matching compilation. This is better code,
and we believe that it does not change the behavior: `Types.equal_tag`
is mostly similar to a type-specialized version of `( = )`, except
that it calls `Ident.same` that just compares the stamps and ignore
the names, which (assuming well-formedness of idents) is equivalent
and slightly faster.
2020-05-25 17:06:31 +02:00
Gabriel Scherer 1ee6ee4194 fixup! matching: use pattern views in Parmatch as well 2020-05-21 09:51:30 +02:00
Gabriel Scherer 68dc87c9e9 matching: use pattern views in Parmatch as well 2020-05-14 10:27:50 +02:00
Gabriel Scherer e19a3afcb4 matching: move {general,simple,half_simple}_view to Patterns 2020-05-14 10:27:15 +02:00
Gabriel Scherer 4d6267d3ba matching: move (Non_empty_row, views, General) to patterns.ml 2020-05-14 10:26:01 +02:00
Gabriel Scherer 0e979b7ea9 patterns: reuse ('a Typedtree.pattern_) to define Patterns.Head.t 2020-05-14 10:26:01 +02:00
Gabriel Scherer f5f4ba67a9 matching: separate types for rows and clauses
"rows" are common abstraction of both pattern analysis and
compilation, but clauses carrying a Lambda.t term are
compilation-specific. Separating rows will thus enable moving more
logic to typing/patterns for use in both settings.
2020-05-14 10:16:34 +02:00
Gabriel Scherer 54b79d3b27 [minor] matching: rename Non_empty_clause.{map_head => map_first} 2020-05-14 10:13:02 +02:00
Gabriel Scherer ffb6caef8b patterns: move Parmatch.Pattern_head into Patterns.Head
The aim is to also move the Simple/Half_simple/General stuff from
matching, but we need to split in those modules the part that are
purely structural (they go in Patterns) and the parts that are
actually compilation logic (Half_simple.of_clause), those stay in
Matching.
2020-05-14 10:11:36 +02:00
Gabriel Scherer b3434751e2
Merge pull request #9520 from trefis/rematch-make_matching-cleanup
pattern-matching refactoring: refactor the `make_<foo>_matching` functions
2020-05-14 09:21:07 +02:00
Nicolás Ojeda Bär 4e33dcf35f Add %loc_FUNCTION primitive 2020-05-13 20:49:01 +02:00
Gabriel Scherer 065139617f matching: factorize the make_*_matching functions
Before, each head construction had a `make_<foo>_matching` construct that
was responsible for three things:
- consuming the argument from the argument list
  (the "argument" is a piece of lambda code to access
   the value of the current scrutinee)
- building arguments for the subpatterns of the scrutinee
  and pushing them to the argument list
- building a `cell` structure out of this, representing a head group
  during compilation

Only the second point is really specific to each construction.

This refactoring turns this second point into a construct-specific
`get_expr_args_<foo>` function (similarly to `get_pat_args_<foo>`),
and moves the first and third point to a generic `make_matching`
function.

Note: this commit contains a minor improvement to the location used to
force (lazy ..) arguments.
2020-05-13 17:18:08 +02:00
Gabriel Scherer 3527653363 matching: add a comment suggested by Florian Angeletti's review 2020-05-01 21:58:28 +02:00
Gabriel Scherer 50fdc06fcd [minor] matching.ml: tune ~scopes handling 2020-05-01 21:56:27 +02:00
Gabriel Scherer ac1243cbd1 matching: use heads in the make_*_matching specialization calls 2020-05-01 21:56:27 +02:00