aboutsummaryrefslogtreecommitdiffstats
path: root/lib/compiler/src/v3_core.erl
AgeCommit message (Collapse)Author
2015-06-18Change license text to APLv2Bruce Yinhe
2015-04-30compiler: Use module erl_annoHans Bolinder
2015-04-29v3_core, v3_codegen: Eliminate old-style catchesBjörn Gustavsson
2015-04-15Raise more descriptive error messages for failed map operationsBjörn Gustavsson
According to EEP-43 for maps, a 'badmap' exception should be generated when an attempt is made to update non-map term such as: <<>>#{a=>42} That was not implemented in the OTP 17. José Valim suggested that we should take the opportunity to improve the errors coming from map operations: http://erlang.org/pipermail/erlang-questions/2015-February/083588.html This commit implement better errors from map operations similar to his suggestion. When a map update operation (Map#{...}) or a BIF that expects a map is given a non-map term, the exception will be: {badmap,Term} This kind of exception is similar to the {badfun,Term} exception from operations that expect a fun. When a map operation requires a key that is not present in a map, the following exception will be raised: {badkey,Key} José Valim suggested that the exception should be {badkey,Key,Map}. We decided not to do that because the map could potentially be huge and cause problems if the error propagated through links to other processes. For BIFs, it could be argued that the exceptions could be simply 'badmap' and 'badkey', because the bad map and bad key can be found in the argument list for the BIF in the stack backtrace. However, for the map update operation (Map#{...}), the bad map or bad key will not be included in the stack backtrace, so that information must be included in the exception reason itself. For consistency, the BIFs should raise the same exceptions as update operation. If more than one key is missing, it is undefined which of keys that will be reported in the {badkey,Key} exception.
2015-03-09v3_core: Teach pat_alias/2 to eliminate duplicated variablesBjörn Gustavsson
Duplicated variables as aliases in patterns, such as: f({_,_}=Dup=Dup) -> ... will work, but produce sub-optimal code similar to: f({_,_}=Dup=NewVar) when Dup =:= NewVar -> ... with one extra guard test for each duplicated variable. Rewrite pat_alias/2 to eliminate all duplicated variables. While we are at it, also simplify handling of tuples, conses, and literals by using the data functions in the cerl module.
2015-03-09v3_core: Eliminate the sloppiness-encouraging get_ianno/1 functionBjörn Gustavsson
get_ianno/1 would retrieve either a bare annotation or an annotation wrapped in an #a{} record. In both cases, it would return a wrapped annotation. We can replace the calls to get_ianno/1 with calls to get_anno/1, because the argument is always an #iclause{} and all iclause records are always initialized with a wrapped annotation.
2015-03-09v3_core: Add is_map tests before map instructionsBjörn Gustavsson
If we have a sequence of put_map_* instructions operating on the same map, it will be more efficient if we can have one is_map/2 instruction before put_map_* instructions, so that each put_map_* does not need to test whether the argument is a map.
2015-02-27v3_core: Simplify conversion of map patternsBjörn Gustavsson
2015-02-12v3_core: Simplify translation of mapsBjörn Gustavsson
There is no need to always introduce a new variable to hold a map. Maps are novars (constructs that don't export variables).
2015-02-11v3_core: Suppress compiler-generated calls in guardsBjörn Gustavsson
Compiling the following function: f(V) when not (bar and V) -> true; %Line 4 f(_) -> false. would produce the following warnings: no_file: Warning: the call to is_boolean/1 has no effect t.erl:4: Warning: the guard for this clause evaluates to 'false' t.erl:4: Warning: use of operator '=:=' has no effect Two of the warnings refer to calls to is_boolean/1 and '=:='/2 which v3_core added when translating the code to Core Erlang. The only relevant warning is: t.erl:4: Warning: the guard for this clause evaluates to 'false' Suppress the other two warning by marking the compiler-generated calls with a 'compiler_generated' annotation.
2015-02-11v3_core: Remove out-commented codeBjörn Gustavsson
2015-02-11v3_core: Remove unused function argument for bc_tq()Björn Gustavsson
2015-02-11v3_core: Use Core Erlang annotations in a type-safe wayBjörn Gustavsson
Core Erlang annotations are supposed to be a list of terms. v3_core could temporarily stuff a record in the 'anno' field of a Core Erlang record. That will cause Dialyzer warnings if we would tighten the type specs for annotations. (We want to tighten the warnings in order to catch more real problems.) Avoid abusing the annotation by wrapping the entire Core Erlang record in a #isimple{} record. Reported-by: Kostis Sagonas
2015-02-03Suppress warnings for expressions that are assigned to '_'Björn Gustavsson
In c34ad2d5, the compiler learned to silence some warnings for expressions that were explicitly assigned to the '_' variable, as in this example: _ = list_to_integer(S), ok That commit intentionally only made it possible to silence warnings for BIFs that could cause an exception. Warnings would still be produced for: _ = date(), ok because date/0 can never fail and thus making the call completely useless. The reasoning was that such warnings can always be eliminated by eliminating the offending code. While that is true, there is the question about rules and their consistency. It is surprising that '_' can be used to silence some warnings, but has no effect on other warnings. Therefore, we will teach the compiler to silence warnings for the following constructs: * Calls to safe BIFs such as date/0 * Expressions that will cause an exception such as 'X/0' * Terms that are built but not used, such as '{x,X}'
2015-01-29Move grouping of map constructions from v3_core to v3_kernelBjörn Gustavsson
When translating a function with map construction: f(A) -> B = b, C = c, #{A=>1,B=>2,C=>3}. v3_core would break apart the map construction into three parts because of the way the map instructions in BEAM work -- variable keys need to be in their own instruction. In the example, constant propagation will turn two of the keys to literal keys. But the initial breaking apart will not be undone, so there will still be three map constructions: 'f'/1 = fun (_cor0) -> let <_cor3> = ~{::<_cor0,1>}~ in let <_cor4> = ~{::<'b',2>|_cor3}~ in ~{::<'c',3>|_cor4}~ It would be possible to complicate the sys_core_fold pass to regroup map operations so that we would get: 'f'/1 = fun (_cor0) -> let <_cor3> = ~{::<_cor0,1>}~ in ~{::<'b',2>,::<'c',3>|_cor3}~ A simpler way that allows to simplify the translation is to skip the grouping in v3_core and translate the function to: 'f'/1 = fun (_cor0) -> ~{::<_cor0,1>,::<'b',2>,::<'c',3>}~ We will then let v3_kernel do the grouping while translating from Core Erlang to Kernel Erlang.
2015-01-26cerl: Make sure that we preserve the invariants for mapsBjörn Gustavsson
Maps have certain invariants that must be preserved: (1) A map as a pattern must be represented as #c_map{} record, never as a literal. The reason is that the pattern '#{}' will match any map, not just the empty map. The literal '#{}' will only match the empty map. (2) In a map pattern, the key must be a literal, a variable, or data (list or tuple). Keys that are binaries or maps *must* be represented as literals. (3) Maps in expressions should be represented as literals if possible. Nothing is broken if this invariant is broken, but the generated code will be less efficient. To preserve invariant (1), cerl:update_c_map/3 must never collapse a map to a literal. To preserve invariant (3), cerl:update_c_map/3 must collapse a map to a literal if possible. To preserve both invariants, we need a way for cerl:update_c_map/3 to know whether the map is used as a pattern or as an expression. The simplest way is to have an 'is_pat' boolean in the #c_map{} record which is set when a #c_map{} record is initially created. We also need to update core_parse.yrl to establish the invariants in the same way as v3_core, to ensure that compiling from a .core file will work even if all optimizations on Core Erlang are disabled.
2015-01-26Fix handling of binary map keys in comprehensionsBjörn Gustavsson
The translation of list comprehension with a map pattern with a big literal binary as key such as: lc(L) -> [V || #{<<2:301>> := V} <- L]. would generate Core Erlang code where an unbound variable were referenced: 'lc'/1 = fun (L) -> letrec 'lc$^0'/1 = fun (_cor4) -> case _cor4 of <[~{~<_cor1,V>}~|_cor3]> when 'true' -> let <_cor5> = apply 'lc$^0'/1(_cor3) in [V|_cor5] <[_cor2|_cor3]> when 'true' -> apply 'lc$^0'/1(_cor3) <[]> when 'true' -> [] end in let <_cor1> = #{#<2>(301,1,'integer',['unsigned'|['big']])}# in apply 'lc$^0'/1(L) In the map pattern in the 'case' in the 'letrec', the key is the variable '_cor1' which should be bound in the enclosing environment. It is not. There is binding of '_cor1', but in the wrong place (at the end of the function). Because of the way v3_kernel translates letrecs, the code *happens* to work. The code will break if Core Erlang optimizations were strengthened to more aggressively eliminate variable bindings that are not used, or if the translation from Core Erlang to Kernel Erlang were changed. Correct the translation so that '_cor1' is bound in the environment enclosing the 'letrec': 'lc'/1 = fun (L) -> let <_cor1> = #{#<2>(301,1,'integer',['unsigned'|['big']])}# in letrec 'lc$^0'/1 = fun (_cor4) -> case _cor4 of <[~{~<_cor1,V>}~|_cor3]> when 'true' -> let <_cor5> = apply 'lc$^0'/1(_cor3) in [V|_cor5] <[_cor2|_cor3]> when 'true' -> apply 'lc$^0'/1(_cor3) <[]> when 'true' -> [] end in apply 'lc$^0'/1(L) Unfortunately I was not able to come up with a test case that demonstrates the bug.
2014-11-17Merge branch 'maint'Björn Gustavsson
* maint: Fix miscompilation when module contains multiple named funs Fix locations of shadowing warnings in ms_transform
2014-10-27Extract annotations from filter qualifiers in a type-friendly wayBjörn Gustavsson
Commit 78ce8917d started to use get_anno/1 to extract the line annotation from filter qualifiers in comprehensions, but this does not respect the spec of this function and resuls in a dialyzer warning. To make the code more type-friendly, introduce a get_qual_anno/1 function. Kostis Sagonas suggested that the function should be implemented similar to this to also ensure that the qualifiers are of the appropriate form: get_qual_anno({call,Line,_,_}) -> Line; get_qual_anno({op,Line,_,_,_}) -> Line; . . . get_qual_anno({var,Line,_}) -> Line. The problem is that it is difficult to know exacly which forms that may occur and the function will need to be updated if new abstract forms are added. Thus this implementation would complicate maintanance without any real payoff. Reported-by: Kostis Sagonas
2014-10-27Fix miscompilation when module contains multiple named funsAnthony Ramine
A module containing two named funs bearing the same name and arity could be miscompiled. Reported-by: Sam Chapin
2014-10-02compiler: Properly support Map aliasingBjörn-Egil Dahlberg
Matching of type: #{K := V1} = #{K := V2} = M, Will alias (coalesce) to #{K := V1 = V2} = M.
2014-10-01compiler: Refactor Map pairs aliasingBjörn-Egil Dahlberg
2014-08-26compiler: Fix v3_core Maps pair chainsBjörn-Egil Dahlberg
Check for literals instead of variables when constructing chains.
2014-08-26compiler: Use expressions in core patternsBjörn-Egil Dahlberg
Two patterns, binary_segment size and map_pair key, are expressions even in matching. If only bound variables are used we are fine but some expressions which appears as literals needs to be lifted. Currently only Map key binaries will use this.
2014-08-26compiler: Reintroduce binary limit for Map keysBjörn-Egil Dahlberg
2014-08-26compiler: Shameless v3_core hack for variablesBjörn-Egil Dahlberg
2014-08-22compiler: Use variables in Map core passBjörn-Egil Dahlberg
2014-06-17[dialyzer] Use the option 'dialyzer' to control the compilerHans Bolinder
2014-06-17[dialyzer] Fix handling of literal recordsHans Bolinder
This ticket is about records in Erlang code, and when to check the fields against the (optional) types given when defining records. Dialyzer operates on the Erlang Core format, where there are no trace of records. The fix implemented is a Real Hack: Given the new option 'dialyzer' erl_expand_records marks the line number of records in a way that is undone by v3_core, which in turn inserts annotations that can be recognized by Dialyzer.
2014-04-03compiler,stdlib: Fix Map literals as keys for Maps in patternsBjörn-Egil Dahlberg
2014-03-25compiler: Do not evaluate map expressions with bad keysBjörn-Egil Dahlberg
Map keys with large (non literal) binary keys must fail.
2014-03-25compiler: Throw 'nomatch' on matching with bad binary keysBjörn-Egil Dahlberg
Even if a binary key is written as a literal the compiler may choose to make an expression. Emit a warning in those cases and saying the case will not match. This is a limitation in current implementation.
2014-03-18Properly collect variables in map expressions in v3_coreAnthony Ramine
Reported-by: José Valim
2014-03-17compiler: Transform M#{} to is_map(M)Björn-Egil Dahlberg
Core should not understand M#{} Instead transform M#{} to case _cor0 of <_cor1> when call 'erlang':'is_map' (_cor0) -> _cor1 ( <_cor2> when 'true' -> primop 'match_fail' ('badarg') -| ['compiler_generated'] ) end
2014-03-17compiler: Validate Map srcBjörn-Egil Dahlberg
Reject all expressions that are known to fail. Emit 'badarg' for those expressions. Ex. []#{ a => 1} Is not a valid map update expression.
2014-03-14compiler: Create literal Maps in creation if possibleBjörn-Egil Dahlberg
2014-03-06Merge branch 'nox/compiler/v3_core-comprehension-no-export'Björn Gustavsson
* nox/compiler/v3_core-comprehension-no-export: Do not export variables from comprehension cases in v3_core OTP-11770
2014-03-05Do not export variables from comprehension cases in v3_coreAnthony Ramine
Code like the following snippet could make the compiler crash: f() -> [X = a || false] ++ [X = a || false]. Reported-by: Ulf Norell
2014-03-05Compile BIF calls and operator expressions to Core the same wayAnthony Ramine
Previously, erlang:'or'(X, Y) and X or Y were not compiled to the same Core code.
2014-03-01Do not emit blatantly illformed Core Erlang apply expressionsAnthony Ramine
(fun f/1)() should be compiled to let X = 'f'/1 in apply X () to let the compiler properly generate code that will fail with badarity at runtime. Reported-by: Ulf Norell
2014-02-21Merge branch 'egil/compiler/maps-get_map_elements'Björn-Egil Dahlberg
* egil/compiler/maps-get_map_elements: compiler: Strengthen Maps compile tests compiler: Remove dead warning erts: Fix erts_debug:disassemble/1 compiler: Transform list of Args to exact literal type compiler: Test Maps aliasing compiler: Use aliasing in map pair patterns compiler: Check literal order in beam_validator erts: Introduce new instructions for combined key fetches compiler: Change map instructions for fetching values
2014-02-19compiler: Use aliasing in map pair patternsBjörn-Egil Dahlberg
2014-02-17Simplify comprehension compilation in v3_coreAnthony Ramine
Given that map comprehensions and generators and maybe && generators will be added to the language in the future, v3_core:lc_tq and v3_core:bc_tq could use a rewrite to avoid a complexity explosion, where there are as many related clauses as the product of the number of types of generators and the number of types of comprehensions. The new code abstract over all generators at the same time, there is only one clause for generators per type of comprehension, and all the filter code has been put in a common function filter_tq. It should also be noted that generator inputs are now compiled before the rest of the qualifiers, reversing names of nested comprehensions.
2014-02-13Merge branch 'bjorn/lc-warnings/OTP-11626' (early part)Björn Gustavsson
* 'bjorn/lc-warnings/OTP-11626' (early part): v3_core: Annotate list comprehensions to help out dialyzer
2014-02-07Merge branch 'hsv/using_lists_droplast'Henrik Nord
* hsv/using_lists_droplast: lib/mnesia/test/ - Replace reverse(tl(reverse(L))) with lists:droplast/1 lib/ssh - Replace reverse(tl(reverse(L))) with lists:droplast/1 lib/wx - Replace reverse(tl(reverse(L))) with lists:droplast/1 Use lists:droplast/1 in orber/orber_interceptors.erl Import and use lists:droplast/1 in v3_core/v3_kernel OTP-11678 OTP-11677
2014-01-31v3_core: Annotate list comprehensions to help out dialyzerBjörn Gustavsson
2014-01-30Issue a warning when a named fun is constructed but not usedBjörn Gustavsson
2014-01-29compiler: Squash #c_map_pair_*{} to #c_map_pair{}Björn-Egil Dahlberg
Simplify compiler internals and parsing of core format.
2014-01-28compiler: Fix v3_core for map update syntaxBjörn-Egil Dahlberg
Make map update expressions safe, i.e. (foo())#{ k1 := 1 }
2014-01-28compiler: Implement support for exact Op in MapsBjörn-Egil Dahlberg
The syntax is handled upto v3_kernel where it is reduced to previous behaviour for construction and updates. Meaning, the ':=' operator is handled exactly as '=>' operator.