aboutsummaryrefslogtreecommitdiffstats
path: root/lib/hipe
diff options
context:
space:
mode:
Diffstat (limited to 'lib/hipe')
-rw-r--r--lib/hipe/Makefile2
-rw-r--r--lib/hipe/arm/hipe_arm_assemble.erl48
-rw-r--r--lib/hipe/cerl/Makefile11
-rw-r--r--lib/hipe/cerl/cerl_closurean.erl8
-rw-r--r--lib/hipe/cerl/cerl_messagean.erl4
-rw-r--r--lib/hipe/cerl/cerl_prettypr.erl41
-rw-r--r--lib/hipe/cerl/cerl_to_icode.erl38
-rw-r--r--lib/hipe/cerl/cerl_typean.erl4
-rw-r--r--lib/hipe/cerl/erl_bif_types.erl1820
-rw-r--r--lib/hipe/cerl/erl_types.erl2401
-rw-r--r--lib/hipe/doc/src/book.xml4
-rw-r--r--lib/hipe/doc/src/fascicules.xml2
-rw-r--r--lib/hipe/doc/src/hipe_app.xml2
-rw-r--r--lib/hipe/doc/src/notes.xml118
-rw-r--r--lib/hipe/doc/src/part_notes.xml4
-rw-r--r--lib/hipe/doc/src/ref_man.xml4
-rw-r--r--lib/hipe/flow/cfg.hrl6
-rw-r--r--lib/hipe/flow/cfg.inc4
-rw-r--r--lib/hipe/flow/hipe_dominators.erl8
-rw-r--r--lib/hipe/flow/liveness.inc6
-rw-r--r--lib/hipe/icode/hipe_beam_to_icode.erl143
-rw-r--r--lib/hipe/icode/hipe_icode.erl14
-rw-r--r--lib/hipe/icode/hipe_icode.hrl8
-rw-r--r--lib/hipe/icode/hipe_icode_callgraph.erl4
-rw-r--r--lib/hipe/icode/hipe_icode_cfg.erl6
-rw-r--r--lib/hipe/icode/hipe_icode_coordinator.erl8
-rw-r--r--lib/hipe/icode/hipe_icode_exceptions.erl8
-rw-r--r--lib/hipe/icode/hipe_icode_fp.erl8
-rw-r--r--lib/hipe/icode/hipe_icode_instruction_counter.erl5
-rw-r--r--lib/hipe/icode/hipe_icode_mulret.erl20
-rw-r--r--lib/hipe/icode/hipe_icode_range.erl10
-rw-r--r--lib/hipe/icode/hipe_icode_ssa.erl4
-rw-r--r--lib/hipe/icode/hipe_icode_ssa_struct_reuse.erl16
-rw-r--r--lib/hipe/icode/hipe_icode_type.erl53
-rw-r--r--lib/hipe/llvm/Makefile109
-rw-r--r--lib/hipe/llvm/elf32_format.hrl59
-rw-r--r--lib/hipe/llvm/elf64_format.hrl58
-rw-r--r--lib/hipe/llvm/elf_format.erl790
-rw-r--r--lib/hipe/llvm/elf_format.hrl488
-rw-r--r--lib/hipe/llvm/hipe_llvm.erl1131
-rw-r--r--lib/hipe/llvm/hipe_llvm_arch.hrl11
-rw-r--r--lib/hipe/llvm/hipe_llvm_liveness.erl112
-rw-r--r--lib/hipe/llvm/hipe_llvm_main.erl506
-rw-r--r--lib/hipe/llvm/hipe_llvm_merge.erl114
-rw-r--r--lib/hipe/llvm/hipe_rtl_to_llvm.erl1613
-rw-r--r--lib/hipe/main/hipe.app.src11
-rw-r--r--lib/hipe/main/hipe.appup.src7
-rw-r--r--lib/hipe/main/hipe.erl85
-rw-r--r--lib/hipe/main/hipe_main.erl42
-rw-r--r--lib/hipe/misc/hipe_consttab.erl8
-rw-r--r--lib/hipe/misc/hipe_consttab.hrl4
-rw-r--r--lib/hipe/misc/hipe_gensym.erl2
-rw-r--r--lib/hipe/misc/hipe_pack_constants.erl102
-rw-r--r--lib/hipe/misc/hipe_sdi.erl15
-rw-r--r--lib/hipe/ppc/hipe_ppc_assemble.erl48
-rw-r--r--lib/hipe/regalloc/hipe_coalescing_regalloc.erl4
-rw-r--r--lib/hipe/regalloc/hipe_ig_moves.erl4
-rw-r--r--lib/hipe/regalloc/hipe_ls_regalloc.erl4
-rw-r--r--lib/hipe/regalloc/hipe_optimistic_regalloc.erl8
-rw-r--r--lib/hipe/regalloc/hipe_reg_worklists.erl4
-rw-r--r--lib/hipe/rtl/hipe_icode2rtl.erl4
-rw-r--r--lib/hipe/rtl/hipe_rtl.erl174
-rw-r--r--lib/hipe/rtl/hipe_rtl.hrl3
-rw-r--r--lib/hipe/rtl/hipe_rtl_arith.inc4
-rw-r--r--lib/hipe/rtl/hipe_rtl_liveness.erl3
-rw-r--r--lib/hipe/rtl/hipe_rtl_mk_switch.erl4
-rw-r--r--lib/hipe/rtl/hipe_rtl_ssa_const_prop.erl4
-rw-r--r--lib/hipe/rtl/hipe_rtl_ssapre.erl4
-rw-r--r--lib/hipe/rtl/hipe_tagscheme.erl37
-rw-r--r--lib/hipe/sparc/hipe_sparc_assemble.erl48
-rw-r--r--lib/hipe/ssa/hipe_ssa.inc4
-rw-r--r--lib/hipe/ssa/hipe_ssa_const_prop.inc10
-rw-r--r--lib/hipe/test/Makefile80
-rw-r--r--lib/hipe/test/bs_SUITE_data/bs_add.erl18
-rw-r--r--lib/hipe/test/bs_SUITE_data/bs_bincomp.erl79
-rw-r--r--lib/hipe/test/bs_SUITE_data/bs_bits.erl150
-rw-r--r--lib/hipe/test/bs_SUITE_data/bs_bitsize.erl23
-rw-r--r--lib/hipe/test/bs_SUITE_data/bs_bugs_R08.erl32
-rw-r--r--lib/hipe/test/bs_SUITE_data/bs_bugs_R09.erl35
-rw-r--r--lib/hipe/test/bs_SUITE_data/bs_bugs_R12.erl133
-rw-r--r--lib/hipe/test/bs_SUITE_data/bs_build.erl41
-rw-r--r--lib/hipe/test/bs_SUITE_data/bs_catch_bug.erl25
-rw-r--r--lib/hipe/test/bs_SUITE_data/bs_checksum.erl35
-rw-r--r--lib/hipe/test/bs_SUITE_data/bs_construct.erl128
-rw-r--r--lib/hipe/test/bs_SUITE_data/bs_decode.erl980
-rw-r--r--lib/hipe/test/bs_SUITE_data/bs_decode_extract.hrl91
-rw-r--r--lib/hipe/test/bs_SUITE_data/bs_des.erl734
-rw-r--r--lib/hipe/test/bs_SUITE_data/bs_extract.erl94
-rw-r--r--lib/hipe/test/bs_SUITE_data/bs_flatb.erl29
-rw-r--r--lib/hipe/test/bs_SUITE_data/bs_id3.erl75
-rw-r--r--lib/hipe/test/bs_SUITE_data/bs_match.erl175
-rw-r--r--lib/hipe/test/bs_SUITE_data/bs_native_float.erl22
-rw-r--r--lib/hipe/test/bs_SUITE_data/bs_orber.erl26
-rw-r--r--lib/hipe/test/bs_SUITE_data/bs_pmatch.erl269
-rw-r--r--lib/hipe/test/bs_SUITE_data/bs_pmatch_bugs.erl67
-rw-r--r--lib/hipe/test/bs_SUITE_data/bs_pmatch_in_guards.erl23
-rw-r--r--lib/hipe/test/bs_SUITE_data/bs_potpurri.erl200
-rw-r--r--lib/hipe/test/bs_SUITE_data/bs_remove3.erl104
-rw-r--r--lib/hipe/test/bs_SUITE_data/bs_save.erl21
-rw-r--r--lib/hipe/test/bs_SUITE_data/bs_shell_native.erl275
-rw-r--r--lib/hipe/test/bs_SUITE_data/bs_split.erl105
-rw-r--r--lib/hipe/test/bs_SUITE_data/bs_system_limit_32.erl26
-rw-r--r--lib/hipe/test/bs_SUITE_data/bs_utf.erl18
-rw-r--r--lib/hipe/test/bs_SUITE_data/bs_var_segs.erl76
-rw-r--r--lib/hipe/test/hipe.spec6
-rw-r--r--lib/hipe/test/hipe_SUITE.erl55
-rw-r--r--lib/hipe/test/hipe_testsuite_driver.erl201
-rw-r--r--lib/hipe/test/maps_SUITE_data/maps_build_and_match_aliasing.erl20
-rw-r--r--lib/hipe/test/maps_SUITE_data/maps_build_and_match_empty_val.erl17
-rw-r--r--lib/hipe/test/maps_SUITE_data/maps_build_and_match_literals.erl40
-rw-r--r--lib/hipe/test/maps_SUITE_data/maps_build_and_match_over_alloc.erl16
-rw-r--r--lib/hipe/test/maps_SUITE_data/maps_build_and_match_val.erl23
-rw-r--r--lib/hipe/test/maps_SUITE_data/maps_expand_map_update.erl7
-rw-r--r--lib/hipe/test/maps_SUITE_data/maps_export.erl11
-rw-r--r--lib/hipe/test/maps_SUITE_data/maps_get_map_elements.erl23
-rw-r--r--lib/hipe/test/maps_SUITE_data/maps_guard_bifs.erl31
-rw-r--r--lib/hipe/test/maps_SUITE_data/maps_guard_fun.erl36
-rw-r--r--lib/hipe/test/maps_SUITE_data/maps_guard_receive.erl54
-rw-r--r--lib/hipe/test/maps_SUITE_data/maps_guard_sequence.erl35
-rw-r--r--lib/hipe/test/maps_SUITE_data/maps_guard_update.erl14
-rw-r--r--lib/hipe/test/maps_SUITE_data/maps_has_map_fields.erl46
-rw-r--r--lib/hipe/test/maps_SUITE_data/maps_is_map.erl24
-rw-r--r--lib/hipe/test/maps_SUITE_data/maps_list_comprehension.erl6
-rw-r--r--lib/hipe/test/maps_SUITE_data/maps_map_size.erl29
-rw-r--r--lib/hipe/test/maps_SUITE_data/maps_map_sort_literals.erl41
-rw-r--r--lib/hipe/test/maps_SUITE_data/maps_match_and_update_literals.erl24
-rw-r--r--lib/hipe/test/maps_SUITE_data/maps_put_map_assoc.erl23
-rw-r--r--lib/hipe/test/maps_SUITE_data/maps_put_map_exact.erl28
-rw-r--r--lib/hipe/test/maps_SUITE_data/maps_update_assoc.erl22
-rw-r--r--lib/hipe/test/maps_SUITE_data/maps_update_exact.erl32
-rw-r--r--lib/hipe/test/maps_SUITE_data/maps_update_literals.erl13
-rw-r--r--lib/hipe/test/maps_SUITE_data/maps_update_map_expressions.erl32
-rw-r--r--lib/hipe/test/maps_SUITE_data/maps_update_values.erl28
-rw-r--r--lib/hipe/test/maps_SUITE_data/maps_warn_pair_key_overloaded.erl27
-rw-r--r--lib/hipe/test/maps_SUITE_data/maps_warn_useless_build.erl9
-rw-r--r--lib/hipe/tools/Makefile3
-rw-r--r--lib/hipe/tools/hipe_tool.erl525
-rw-r--r--lib/hipe/util/hipe_digraph.erl8
-rw-r--r--lib/hipe/util/hipe_dot.erl6
-rw-r--r--lib/hipe/util/hipe_vectors.hrl4
-rw-r--r--lib/hipe/vsn.mk2
-rw-r--r--lib/hipe/x86/hipe_x86_assemble.erl51
-rw-r--r--lib/hipe/x86/hipe_x86_postpass.erl4
143 files changed, 13609 insertions, 2636 deletions
diff --git a/lib/hipe/Makefile b/lib/hipe/Makefile
index a9e24f4d17..46cbc33ae2 100644
--- a/lib/hipe/Makefile
+++ b/lib/hipe/Makefile
@@ -22,7 +22,7 @@ include $(ERL_TOP)/make/target.mk
include $(ERL_TOP)/make/$(TARGET)/otp.mk
ifdef HIPE_ENABLED
-HIPE_SUBDIRS = regalloc sparc ppc x86 amd64 arm opt tools
+HIPE_SUBDIRS = regalloc sparc ppc x86 amd64 arm opt tools llvm
else
HIPE_SUBDIRS =
endif
diff --git a/lib/hipe/arm/hipe_arm_assemble.erl b/lib/hipe/arm/hipe_arm_assemble.erl
index 2af786994e..e9de96a927 100644
--- a/lib/hipe/arm/hipe_arm_assemble.erl
+++ b/lib/hipe/arm/hipe_arm_assemble.erl
@@ -44,8 +44,8 @@ assemble(CompiledCode, Closures, Exports, Options) ->
print("Total num bytes=~w\n", [CodeSize], Options),
%%
SC = hipe_pack_constants:slim_constmap(ConstMap),
- DataRelocs = mk_data_relocs(RefsFromConsts, LabelMap),
- SSE = slim_sorted_exportmap(ExportMap,Closures,Exports),
+ DataRelocs = hipe_pack_constants:mk_data_relocs(RefsFromConsts, LabelMap),
+ SSE = hipe_pack_constants:slim_sorted_exportmap(ExportMap,Closures,Exports),
SlimRefs = hipe_pack_constants:slim_refs(AccRefs),
Bin = term_to_binary([{?VERSION_STRING(),?HIPE_SYSTEM_CRC},
ConstAlign, ConstSize,
@@ -320,7 +320,7 @@ do_pseudo_li(I, MFA, ConstMap, Address, PrevImms, PendImms) ->
Atom when is_atom(Atom) ->
{load_atom, Atom};
{Label,constant} ->
- ConstNo = find_const({MFA,Label}, ConstMap),
+ ConstNo = hipe_pack_constants:find_const({MFA,Label}, ConstMap),
{load_address, {constant,ConstNo}};
{Label,closure} ->
{load_address, {closure,Label}};
@@ -518,37 +518,6 @@ fix_pc_refs(I, InsnAddress, FunAddress, LabelMap) ->
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-mk_data_relocs(RefsFromConsts, LabelMap) ->
- lists:flatten(mk_data_relocs(RefsFromConsts, LabelMap, [])).
-
-mk_data_relocs([{MFA,Labels} | Rest], LabelMap, Acc) ->
- Map = [case Label of
- {L,Pos} ->
- Offset = find({MFA,L}, LabelMap),
- {Pos,Offset};
- {sorted,Base,OrderedLabels} ->
- {sorted, Base, [begin
- Offset = find({MFA,L}, LabelMap),
- {Order, Offset}
- end
- || {L,Order} <- OrderedLabels]}
- end
- || Label <- Labels],
- %% msg("Map: ~w Map\n",[Map]),
- mk_data_relocs(Rest, LabelMap, [Map,Acc]);
-mk_data_relocs([],_,Acc) -> Acc.
-
-find({_MFA,_L} = MFAL, LabelMap) ->
- gb_trees:get(MFAL, LabelMap).
-
-slim_sorted_exportmap([{Addr,M,F,A}|Rest], Closures, Exports) ->
- IsClosure = lists:member({M,F,A}, Closures),
- IsExported = is_exported(F, A, Exports),
- [Addr,M,F,A,IsClosure,IsExported | slim_sorted_exportmap(Rest, Closures, Exports)];
-slim_sorted_exportmap([],_,_) -> [].
-
-is_exported(F, A, Exports) -> lists:member({F,A}, Exports).
-
%%%
%%% Assembly listing support (pp_asm option).
%%%
@@ -594,17 +563,6 @@ fill_spaces(N) when N > 0 ->
fill_spaces(0) ->
[].
-%%%
-%%% Lookup a constant in a ConstMap.
-%%%
-
-find_const({MFA,Label},[{pcm_entry,MFA,Label,ConstNo,_,_,_}|_]) ->
- ConstNo;
-find_const(N,[_|R]) ->
- find_const(N,R);
-find_const(C,[]) ->
- ?EXIT({constant_not_found,C}).
-
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%
diff --git a/lib/hipe/cerl/Makefile b/lib/hipe/cerl/Makefile
index 506e993ff4..d13dfb33c2 100644
--- a/lib/hipe/cerl/Makefile
+++ b/lib/hipe/cerl/Makefile
@@ -42,8 +42,8 @@ RELSYSDIR = $(RELEASE_PATH)/lib/hipe-$(VSN)
# ----------------------------------------------------
# Target Specs
# ----------------------------------------------------
-MODULES = cerl_cconv cerl_closurean cerl_hipeify \
- cerl_lib cerl_messagean cerl_pmatch cerl_prettypr cerl_to_icode \
+MODULES = cerl_cconv cerl_closurean cerl_hipeify cerl_lib \
+ cerl_messagean cerl_pmatch cerl_prettypr cerl_to_icode \
cerl_typean erl_bif_types erl_types
HRL_FILES= cerl_hipe_primops.hrl
@@ -65,7 +65,7 @@ DOC_FILES= $(MODULES:%=$(DOCS)/%.html)
include ../native.mk
-ERL_COMPILE_FLAGS += +inline +warn_exported_vars +warn_unused_import +warn_missing_spec# +warn_untyped_record
+ERL_COMPILE_FLAGS += -Werror +inline +warn_exported_vars +warn_unused_import +warn_missing_spec #+warn_untyped_record
# ----------------------------------------------------
# Targets
@@ -107,7 +107,6 @@ release_spec: opt
release_docs_spec:
-$(EBIN)/cerl_to_icode.beam: cerl_hipe_primops.hrl ../icode/hipe_icode_primops.hrl
+$(EBIN)/cerl_cconv.beam: cerl_hipe_primops.hrl
$(EBIN)/cerl_hipeify.beam: cerl_hipe_primops.hrl
-$(EBIN)/cerl_lambdalift.beam: cerl_hipe_primops.hrl
-$(EBIN)/erl_bif_types.beam: ../icode/hipe_icode_primops.hrl
+$(EBIN)/cerl_to_icode.beam: cerl_hipe_primops.hrl ../icode/hipe_icode_primops.hrl
diff --git a/lib/hipe/cerl/cerl_closurean.erl b/lib/hipe/cerl/cerl_closurean.erl
index 021acd5b35..1b325703ae 100644
--- a/lib/hipe/cerl/cerl_closurean.erl
+++ b/lib/hipe/cerl/cerl_closurean.erl
@@ -1,7 +1,7 @@
%%
%% %CopyrightBegin%
%%
-%% Copyright Ericsson AB 2003-2010. All Rights Reserved.
+%% Copyright Ericsson AB 2003-2014. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
@@ -78,7 +78,8 @@
%% function; see `analyze' for details.
-spec annotate(cerl:cerl()) ->
- {cerl:cerl(), outlist(), dict(), escapes(), dict(), dict()}.
+ {cerl:cerl(), outlist(), dict:dict(),
+ escapes(), dict:dict(), dict:dict()}.
annotate(Tree) ->
{Xs, Out, Esc, Deps, Par} = analyze(Tree),
@@ -206,7 +207,8 @@ append_ann(Tag, Val, []) ->
%% variable labeled `escape', which will hold the set of escaped labels.
%% initially it contains `top' and `external'.
--spec analyze(cerl:cerl()) -> {outlist(), dict(), escapes(), dict(), dict()}.
+-spec analyze(cerl:cerl()) ->
+ {outlist(), dict:dict(), escapes(), dict:dict(), dict:dict()}.
analyze(Tree) ->
%% Note that we use different name spaces for variable labels and
diff --git a/lib/hipe/cerl/cerl_messagean.erl b/lib/hipe/cerl/cerl_messagean.erl
index ca812a0f0d..7911b875a9 100644
--- a/lib/hipe/cerl/cerl_messagean.erl
+++ b/lib/hipe/cerl/cerl_messagean.erl
@@ -1,7 +1,7 @@
%% =====================================================================
%% %CopyrightBegin%
%%
-%% Copyright Ericsson AB 2004-2012. All Rights Reserved.
+%% Copyright Ericsson AB 2004-2014. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
@@ -182,7 +182,7 @@
-type label() :: integer() | 'external' | 'top'.
-type ordset(X) :: [X]. % XXX: TAKE ME OUT
--spec annotate(cerl:cerl()) -> {cerl:cerl(), ordset(label()), dict()}.
+-spec annotate(cerl:cerl()) -> {cerl:cerl(), ordset(label()), dict:dict()}.
annotate(Tree) ->
{Esc0, Vars} = analyze(Tree),
diff --git a/lib/hipe/cerl/cerl_prettypr.erl b/lib/hipe/cerl/cerl_prettypr.erl
index fba9a48cda..9a3873f46d 100644
--- a/lib/hipe/cerl/cerl_prettypr.erl
+++ b/lib/hipe/cerl/cerl_prettypr.erl
@@ -62,7 +62,10 @@
receive_action/1, receive_clauses/1, receive_timeout/1,
seq_arg/1, seq_body/1, string_lit/1, try_arg/1,
try_body/1, try_vars/1, try_evars/1, try_handler/1,
- tuple_es/1, type/1, values_es/1, var_name/1]).
+ tuple_es/1, type/1, values_es/1, var_name/1,
+ c_map/1, map_arg/1, map_es/1, is_c_map_empty/1,
+ c_map_pair/2, map_pair_key/1, map_pair_val/1, map_pair_op/1
+ ]).
-define(PAPER, 76).
-define(RIBBON, 45).
@@ -424,6 +427,10 @@ lay_1(Node, Ctxt) ->
lay_cons(Node, Ctxt);
tuple ->
lay_tuple(Node, Ctxt);
+ map ->
+ lay_map(Node, Ctxt);
+ map_pair ->
+ lay_map_pair(Node, Ctxt);
'let' ->
lay_let(Node, Ctxt);
seq ->
@@ -483,7 +490,13 @@ lay_literal(Node, Ctxt) ->
%% `lay_cons' will check for strings.
lay_cons(Node, Ctxt);
V when is_tuple(V) ->
- lay_tuple(Node, Ctxt)
+ lay_tuple(Node, Ctxt);
+ M when is_map(M), map_size(M) =:= 0 ->
+ text("~{}~");
+ M when is_map(M) ->
+ lay_map(c_map([c_map_pair(abstract(K),abstract(V))
+ || {K,V} <- maps:to_list(M)]),
+ Ctxt)
end.
lay_var(Node, Ctxt) ->
@@ -589,6 +602,30 @@ lay_tuple(Node, Ctxt) ->
Ctxt, fun lay/2)),
floating(text("}")))).
+lay_map(Node, Ctxt) ->
+ Arg = map_arg(Node),
+ After = case is_c_map_empty(Arg) of
+ true -> floating(text("}~"));
+ false ->
+ beside(floating(text(" | ")),
+ beside(lay(Arg,Ctxt),
+ floating(text("}~"))))
+ end,
+ beside(floating(text("~{")),
+ beside(par(seq(map_es(Node), floating(text(",")), Ctxt, fun lay/2)),
+ After)).
+
+lay_map_pair(Node, Ctxt) ->
+ K = map_pair_key(Node),
+ V = map_pair_val(Node),
+ OpTxt = case concrete(map_pair_op(Node)) of
+ assoc -> "::<";
+ exact -> "~<"
+ end,
+ beside(floating(text(OpTxt)),
+ beside(lay(K,Ctxt),beside(floating(text(",")), beside(lay(V,Ctxt),
+ floating(text(">")))))).
+
lay_let(Node, Ctxt) ->
V = lay_value_list(let_vars(Node), Ctxt),
D1 = par([follow(text("let"),
diff --git a/lib/hipe/cerl/cerl_to_icode.erl b/lib/hipe/cerl/cerl_to_icode.erl
index 1c1c10d9b0..2645056be1 100644
--- a/lib/hipe/cerl/cerl_to_icode.erl
+++ b/lib/hipe/cerl/cerl_to_icode.erl
@@ -29,9 +29,9 @@
-define(NO_UNUSED, true).
--export([module/2]).
+-export([module/1, module/2]).
-ifndef(NO_UNUSED).
--export([function/3, function/4, module/1]).
+-export([function/3, function/4]).
-endif.
%% Added in an attempt to suppress message by Dialyzer, but I run into
@@ -102,36 +102,32 @@
%% Record definitions
--record(ctxt, {final = false :: boolean(),
- effect = false,
- fail = [], % [] or fail-to label
- class = expr, % expr | guard
- line = 0, % current line number
- 'receive' % undefined | #receive{}
- }).
-
-record('receive', {loop}).
-record(cerl_to_icode__var, {name}).
-record('fun', {label, vars}).
+-record(ctxt, {final = false :: boolean(),
+ effect = false :: boolean(),
+ fail = [], % [] or fail-to label
+ class = expr :: 'expr' | 'guard',
+ line = 0 :: erl_scan:line(), % current line number
+ 'receive' :: 'undefined' | #'receive'{}
+ }).
%% ---------------------------------------------------------------------
%% Code
-
-%% @spec module(Module::cerl()) -> [icode()]
+%% @spec module(Module::cerl()) -> [{mfa(), icode()}]
%% @equiv module(Module, [])
--ifndef(NO_UNUSED).
+-spec module(cerl:c_module()) -> [{mfa(), hipe_icode:icode()}].
+
module(E) ->
module(E, []).
--endif.
-%% @clear
-
-%% @spec module(Module::cerl(), Options::[term()]) -> [icode()]
+%% @spec module(Module::cerl(), Options::[term()]) -> [{mfa(), icode()}]
%%
-%% cerl() = cerl:cerl()
+%% cerl() = cerl:c_module()
%% icode() = hipe_icode:icode()
%%
%% @doc Transforms a Core Erlang module to linear HiPE Icode. The result
@@ -149,7 +145,7 @@ module(E) ->
%% @see function/4
%% @see cerl_hipeify:transform/1
-%% -spec module(cerl:c_module(), [term()]) -> [{mfa(), hipe_icode:icode()}].
+-spec module(cerl:c_module(), [term()]) -> [{mfa(), hipe_icode:icode()}].
module(E, Options) ->
module_1(cerl_hipeify:transform(E, Options), Options).
@@ -163,8 +159,8 @@ module_1(E, Options) ->
throw(error)
end,
S0 = init(M),
- S1 = s__set_pmatch(proplists:get_value(pmatch, Options), S0),
- S2 = s__set_bitlevel_binaries(proplists:get_value(
+ S1 = s__set_pmatch(proplists:get_value(pmatch, Options), S0),
+ S2 = s__set_bitlevel_binaries(proplists:get_value(
bitlevel_binaries, Options), S1),
{Icode, _} = lists:mapfoldl(fun function_definition/2,
S2, cerl:module_defs(E)),
diff --git a/lib/hipe/cerl/cerl_typean.erl b/lib/hipe/cerl/cerl_typean.erl
index ccd8903658..f694c07c82 100644
--- a/lib/hipe/cerl/cerl_typean.erl
+++ b/lib/hipe/cerl/cerl_typean.erl
@@ -2,7 +2,7 @@
%%
%% %CopyrightBegin%
%%
-%% Copyright Ericsson AB 2003-2009. All Rights Reserved.
+%% Copyright Ericsson AB 2003-2014. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
@@ -242,7 +242,7 @@ delete_ann(_, []) ->
-type labelset() :: ordset(label()).
-type outlist() :: [labelset()] | 'none'.
--spec analyze(cerl:cerl()) -> {outlist(), dict(), dict()}.
+-spec analyze(cerl:cerl()) -> {outlist(), dict:dict(), dict:dict()}.
analyze(Tree) ->
analyze(Tree, ?DEF_LIMIT).
diff --git a/lib/hipe/cerl/erl_bif_types.erl b/lib/hipe/cerl/erl_bif_types.erl
index 42c7e360c1..a460f16272 100644
--- a/lib/hipe/cerl/erl_bif_types.erl
+++ b/lib/hipe/cerl/erl_bif_types.erl
@@ -30,19 +30,17 @@
%-define(BITS, (hipe_rtl_arch:word_size() * 8) - ?TAG_IMMED1_SIZE).
-define(BITS, 128). %This is only in bsl to convert answer to pos_inf/neg_inf.
--define(TAG_IMMED1_SIZE, 4).
+-export([type/3, type/4, type/5, arg_types/3,
+ is_known/3, opaque_args/5, infinity_add/2]).
--export([type/3, type/4, arg_types/3,
- is_known/3, structure_inspecting_args/3, infinity_add/2]).
-
--import(erl_types, [number_max/1,
- number_min/1,
+-import(erl_types, [number_max/2,
+ number_min/2,
t_any/0,
t_arity/0,
t_atom/0,
t_atom/1,
t_atoms/1,
- t_atom_vals/1,
+ t_atom_vals/2,
t_binary/0,
t_bitstr/0,
t_boolean/0,
@@ -60,10 +58,11 @@
t_from_term/1,
t_fun/0,
t_fun/2,
- t_fun_args/1,
- t_fun_range/1,
+ t_fun_args/2,
+ t_fun_range/2,
t_identifier/0,
- t_inf/2,
+ t_has_opaque_subtype/2,
+ t_inf/3,
t_integer/0,
t_integer/1,
t_non_neg_fixnum/0,
@@ -71,30 +70,28 @@
t_pos_integer/0,
t_integers/1,
t_is_any/1,
- t_is_atom/1,
- t_is_binary/1,
- t_is_bitstr/1,
- t_is_boolean/1,
- t_is_cons/1,
- t_is_float/1,
- t_is_float/1,
- t_is_fun/1,
- t_is_integer/1,
- t_is_integer/1,
- t_is_nil/1,
+ t_is_atom/2,
+ t_is_binary/2,
+ t_is_bitstr/2,
+ t_is_boolean/2,
+ t_is_cons/2,
+ t_is_float/2,
+ t_is_fun/2,
+ t_is_integer/2,
+ t_is_nil/1, t_is_nil/2,
t_is_none/1,
t_is_none_or_unit/1,
- t_is_number/1,
- t_is_pid/1,
- t_is_port/1,
- t_is_maybe_improper_list/1,
- t_is_reference/1,
+ t_is_number/2,
+ t_is_pid/2,
+ t_is_port/2,
+ t_is_maybe_improper_list/2,
+ t_is_reference/2,
t_is_string/1,
t_is_subtype/2,
- t_is_tuple/1,
+ t_is_tuple/2,
t_list/0,
t_list/1,
- t_list_elements/1,
+ t_list_elements/2,
t_list_termination/1,
t_mfa/0,
t_module/0,
@@ -104,7 +101,7 @@
t_nonempty_list/0,
t_nonempty_list/1,
t_number/0,
- t_number_vals/1,
+ t_number_vals/2,
t_pid/0,
t_port/0,
t_maybe_improper_list/0,
@@ -115,9 +112,11 @@
t_sup/2,
t_tuple/0,
t_tuple/1,
- t_tuple_args/1,
- t_tuple_size/1,
- t_tuple_subtypes/1
+ t_tuple_args/2,
+ t_tuple_size/2,
+ t_tuple_subtypes/2,
+ t_is_map/2,
+ t_map/0
]).
-ifdef(DO_ERL_BIF_TYPES_TEST).
@@ -129,47 +128,61 @@
-spec type(atom(), atom(), arity()) -> erl_types:erl_type().
type(M, F, A) ->
- type(M, F, A, any_list(A)).
+ type(M, F, A, any_list(A), []).
%% Arguments should be checked for undefinedness, so we do not make
%% unnecessary overapproximations.
-spec type(atom(), atom(), arity(), [erl_types:erl_type()]) -> erl_types:erl_type().
+type(M, F, A, Xs) ->
+ type(M, F, A, Xs, 'universe').
+
+-type opaques() :: 'universe' | [erl_types:erl_type()].
+
+-type arg_types() :: [erl_types:erl_type()].
+
+-spec type(atom(), atom(), arity(), arg_types(), opaques()) ->
+ erl_types:erl_type().
+
%%-- erlang -------------------------------------------------------------------
-type(erlang, halt, 0, _) -> t_none();
-type(erlang, halt, 1, _) -> t_none();
-type(erlang, halt, 2, _) -> t_none();
-type(erlang, exit, 1, _) -> t_none();
-type(erlang, error, 1, _) -> t_none();
-type(erlang, error, 2, _) -> t_none();
-type(erlang, throw, 1, _) -> t_none();
-type(erlang, '==', 2, Xs = [X1, X2]) ->
- case t_is_atom(X1) andalso t_is_atom(X2) of
- true -> type(erlang, '=:=', 2, Xs);
+type(erlang, halt, 0, _, _) -> t_none();
+type(erlang, halt, 1, _, _) -> t_none();
+type(erlang, halt, 2, _, _) -> t_none();
+type(erlang, exit, 1, _, _) -> t_none();
+type(erlang, error, 1, _, _) -> t_none();
+type(erlang, error, 2, _, _) -> t_none();
+type(erlang, throw, 1, _, _) -> t_none();
+type(erlang, '==', 2, Xs = [X1, X2], Opaques) ->
+ case
+ t_is_atom(X1, Opaques) andalso t_is_atom(X2, Opaques)
+ of
+ true -> type(erlang, '=:=', 2, Xs, Opaques);
false ->
- case t_is_integer(X1) andalso t_is_integer(X2) of
- true -> type(erlang, '=:=', 2, Xs);
- false -> strict(Xs, t_boolean())
+ case t_is_integer(X1, Opaques) andalso t_is_integer(X2, Opaques) of
+ true -> type(erlang, '=:=', 2, Xs, Opaques);
+ false -> strict2(Xs, t_boolean())
end
end;
-type(erlang, '/=', 2, Xs = [X1, X2]) ->
- case t_is_atom(X1) andalso t_is_atom(X2) of
- true -> type(erlang, '=/=', 2, Xs);
+type(erlang, '/=', 2, Xs = [X1, X2], Opaques) ->
+ case
+ t_is_atom(X1, Opaques) andalso t_is_atom(X2, Opaques)
+ of
+ true -> type(erlang, '=/=', 2, Xs, Opaques);
false ->
- case t_is_integer(X1) andalso t_is_integer(X2) of
- true -> type(erlang, '=/=', 2, Xs);
- false -> strict(Xs, t_boolean())
+ case t_is_integer(X1, Opaques) andalso t_is_integer(X2, Opaques) of
+ true -> type(erlang, '=/=', 2, Xs, Opaques);
+ false -> strict2(Xs, t_boolean())
end
end;
-type(erlang, '=:=', 2, Xs = [Lhs, Rhs]) ->
+type(erlang, '=:=', 2, Xs = [Lhs, Rhs], Opaques) ->
Ans =
- case t_is_none(t_inf(Lhs, Rhs)) of
+ case t_is_none(t_inf(Lhs, Rhs, Opaques)) of
true -> t_atom('false');
false ->
- case t_is_atom(Lhs) andalso t_is_atom(Rhs) of
+ case t_is_atom(Lhs, Opaques) andalso t_is_atom(Rhs, Opaques) of
true ->
- case {t_atom_vals(Lhs), t_atom_vals(Rhs)} of
+ case {t_atom_vals(Lhs, Opaques), t_atom_vals(Rhs, Opaques)} of
{unknown, _} -> t_boolean();
{_, unknown} -> t_boolean();
{[X], [X]} -> t_atom('true');
@@ -181,16 +194,20 @@ type(erlang, '=:=', 2, Xs = [Lhs, Rhs]) ->
end
end;
false ->
- case t_is_integer(Lhs) andalso t_is_integer(Rhs) of
+ case
+ t_is_integer(Lhs, Opaques) andalso t_is_integer(Rhs, Opaques)
+ of
false -> t_boolean();
true ->
- case {t_number_vals(Lhs), t_number_vals(Rhs)} of
+ case
+ {t_number_vals(Lhs, Opaques), t_number_vals(Rhs, Opaques)}
+ of
{[X], [X]} when is_integer(X) -> t_atom('true');
_ ->
- LhsMax = number_max(Lhs),
- LhsMin = number_min(Lhs),
- RhsMax = number_max(Rhs),
- RhsMin = number_min(Rhs),
+ LhsMax = number_max(Lhs, Opaques),
+ LhsMin = number_min(Lhs, Opaques),
+ RhsMax = number_max(Rhs, Opaques),
+ RhsMin = number_min(Rhs, Opaques),
Ans1 = (is_integer(LhsMin)
andalso is_integer(RhsMax)
andalso (LhsMin > RhsMax)),
@@ -205,15 +222,15 @@ type(erlang, '=:=', 2, Xs = [Lhs, Rhs]) ->
end
end
end,
- strict(Xs, Ans);
-type(erlang, '=/=', 2, Xs = [Lhs, Rhs]) ->
+ strict2(Xs, Ans);
+type(erlang, '=/=', 2, Xs = [Lhs, Rhs], Opaques) ->
Ans =
- case t_is_none(t_inf(Lhs, Rhs)) of
+ case t_is_none(t_inf(Lhs, Rhs, Opaques)) of
true -> t_atom('true');
false ->
- case t_is_atom(Lhs) andalso t_is_atom(Rhs) of
+ case t_is_atom(Lhs, Opaques) andalso t_is_atom(Rhs, Opaques) of
true ->
- case {t_atom_vals(Lhs), t_atom_vals(Rhs)} of
+ case {t_atom_vals(Lhs, Opaques), t_atom_vals(Rhs, Opaques)} of
{unknown, _} -> t_boolean();
{_, unknown} -> t_boolean();
{[Val], [Val]} -> t_atom('false');
@@ -221,13 +238,15 @@ type(erlang, '=/=', 2, Xs = [Lhs, Rhs]) ->
t_sup([t_from_term(X =/= Y) || X <- LhsVals, Y <- RhsVals])
end;
false ->
- case t_is_integer(Lhs) andalso t_is_integer(Rhs) of
+ case
+ t_is_integer(Lhs, Opaques) andalso t_is_integer(Rhs, Opaques)
+ of
false -> t_boolean();
true ->
- LhsMax = number_max(Lhs),
- LhsMin = number_min(Lhs),
- RhsMax = number_max(Rhs),
- RhsMin = number_min(Rhs),
+ LhsMax = number_max(Lhs, Opaques),
+ LhsMin = number_min(Lhs, Opaques),
+ RhsMax = number_max(Rhs, Opaques),
+ RhsMin = number_min(Rhs, Opaques),
Ans1 = (is_integer(LhsMin) andalso is_integer(RhsMax)
andalso (LhsMin > RhsMax)),
Ans2 = (is_integer(LhsMax) andalso is_integer(RhsMin)
@@ -244,15 +263,15 @@ type(erlang, '=/=', 2, Xs = [Lhs, Rhs]) ->
end
end
end,
- strict(Xs, Ans);
-type(erlang, '>', 2, Xs = [Lhs, Rhs]) ->
+ strict2(Xs, Ans);
+type(erlang, '>', 2, Xs = [Lhs, Rhs], Opaques) ->
Ans =
- case t_is_integer(Lhs) andalso t_is_integer(Rhs) of
+ case t_is_integer(Lhs, Opaques) andalso t_is_integer(Rhs, Opaques) of
true ->
- LhsMax = number_max(Lhs),
- LhsMin = number_min(Lhs),
- RhsMax = number_max(Rhs),
- RhsMin = number_min(Rhs),
+ LhsMax = number_max(Lhs, Opaques),
+ LhsMin = number_min(Lhs, Opaques),
+ RhsMax = number_max(Rhs, Opaques),
+ RhsMin = number_min(Rhs, Opaques),
T = t_atom('true'),
F = t_atom('false'),
if
@@ -260,17 +279,17 @@ type(erlang, '>', 2, Xs = [Lhs, Rhs]) ->
is_integer(LhsMax), is_integer(RhsMin), RhsMin >= LhsMax -> F;
true -> t_boolean()
end;
- false -> compare('>', Lhs, Rhs)
+ false -> compare('>', Lhs, Rhs, Opaques)
end,
- strict(Xs, Ans);
-type(erlang, '>=', 2, Xs = [Lhs, Rhs]) ->
+ strict2(Xs, Ans);
+type(erlang, '>=', 2, Xs = [Lhs, Rhs], Opaques) ->
Ans =
- case t_is_integer(Lhs) andalso t_is_integer(Rhs) of
+ case t_is_integer(Lhs, Opaques) andalso t_is_integer(Rhs, Opaques) of
true ->
- LhsMax = number_max(Lhs),
- LhsMin = number_min(Lhs),
- RhsMax = number_max(Rhs),
- RhsMin = number_min(Rhs),
+ LhsMax = number_max(Lhs, Opaques),
+ LhsMin = number_min(Lhs, Opaques),
+ RhsMax = number_max(Rhs, Opaques),
+ RhsMin = number_min(Rhs, Opaques),
T = t_atom('true'),
F = t_atom('false'),
if
@@ -278,17 +297,17 @@ type(erlang, '>=', 2, Xs = [Lhs, Rhs]) ->
is_integer(LhsMax), is_integer(RhsMin), RhsMin > LhsMax -> F;
true -> t_boolean()
end;
- false -> compare('>=', Lhs, Rhs)
+ false -> compare('>=', Lhs, Rhs, Opaques)
end,
- strict(Xs, Ans);
-type(erlang, '<', 2, Xs = [Lhs, Rhs]) ->
+ strict2(Xs, Ans);
+type(erlang, '<', 2, Xs = [Lhs, Rhs], Opaques) ->
Ans =
- case t_is_integer(Lhs) andalso t_is_integer(Rhs) of
+ case t_is_integer(Lhs, Opaques) andalso t_is_integer(Rhs, Opaques) of
true ->
- LhsMax = number_max(Lhs),
- LhsMin = number_min(Lhs),
- RhsMax = number_max(Rhs),
- RhsMin = number_min(Rhs),
+ LhsMax = number_max(Lhs, Opaques),
+ LhsMin = number_min(Lhs, Opaques),
+ RhsMax = number_max(Rhs, Opaques),
+ RhsMin = number_min(Rhs, Opaques),
T = t_atom('true'),
F = t_atom('false'),
if
@@ -296,17 +315,17 @@ type(erlang, '<', 2, Xs = [Lhs, Rhs]) ->
is_integer(LhsMin), is_integer(RhsMax), RhsMax =< LhsMin -> F;
true -> t_boolean()
end;
- false -> compare('<', Lhs, Rhs)
+ false -> compare('<', Lhs, Rhs, Opaques)
end,
- strict(Xs, Ans);
-type(erlang, '=<', 2, Xs = [Lhs, Rhs]) ->
+ strict2(Xs, Ans);
+type(erlang, '=<', 2, Xs = [Lhs, Rhs], Opaques) ->
Ans =
- case t_is_integer(Lhs) andalso t_is_integer(Rhs) of
+ case t_is_integer(Lhs, Opaques) andalso t_is_integer(Rhs, Opaques) of
true ->
- LhsMax = number_max(Lhs),
- LhsMin = number_min(Lhs),
- RhsMax = number_max(Rhs),
- RhsMin = number_min(Rhs),
+ LhsMax = number_max(Lhs, Opaques),
+ LhsMin = number_min(Lhs, Opaques),
+ RhsMax = number_max(Rhs, Opaques),
+ RhsMin = number_min(Rhs, Opaques),
T = t_atom('true'),
F = t_atom('false'),
if
@@ -314,232 +333,237 @@ type(erlang, '=<', 2, Xs = [Lhs, Rhs]) ->
is_integer(LhsMin), is_integer(RhsMax), RhsMax < LhsMin -> F;
true -> t_boolean()
end;
- false -> compare('=<', Lhs, Rhs)
+ false -> compare('=<', Lhs, Rhs, Opaques)
end,
- strict(Xs, Ans);
-type(erlang, '+', 1, Xs) ->
- strict(arg_types(erlang, '+', 1), Xs,
- fun ([X]) -> X end);
-type(erlang, '-', 1, Xs) ->
- strict(arg_types(erlang, '-', 1), Xs,
+ strict2(Xs, Ans);
+type(erlang, '+', 1, Xs, Opaques) ->
+ strict(erlang, '+', 1, Xs, fun ([X]) -> X end, Opaques);
+type(erlang, '-', 1, Xs, Opaques) ->
+ strict(erlang, '-', 1, Xs,
fun ([X]) ->
- case t_is_integer(X) of
+ case t_is_integer(X, Opaques) of
true -> type(erlang, '-', 2, [t_integer(0), X]);
false -> X
end
- end);
-type(erlang, '!', 2, Xs) ->
- strict(arg_types(erlang, '!', 2), Xs, fun ([_, X2]) -> X2 end);
-type(erlang, '+', 2, Xs) ->
- strict(arg_types(erlang, '+', 2), Xs,
+ end, Opaques);
+type(erlang, '!', 2, Xs, Opaques) ->
+ strict(erlang, '!', 2, Xs, fun ([_, X2]) -> X2 end, Opaques);
+type(erlang, '+', 2, Xs, Opaques) ->
+ strict(erlang, '+', 2, Xs,
fun ([X1, X2]) ->
- case arith('+', X1, X2) of
+ case arith('+', X1, X2, Opaques) of
{ok, T} -> T;
error ->
- case t_is_float(X1) orelse t_is_float(X2) of
+ case
+ t_is_float(X1, Opaques) orelse t_is_float(X2, Opaques)
+ of
true -> t_float();
false -> t_number()
end
end
- end);
-type(erlang, '-', 2, Xs) ->
- strict(arg_types(erlang, '-', 2), Xs,
+ end, Opaques);
+type(erlang, '-', 2, Xs, Opaques) ->
+ strict(erlang, '-', 2, Xs,
fun ([X1, X2]) ->
- case arith('-', X1, X2) of
+ case arith('-', X1, X2, Opaques) of
{ok, T} -> T;
error ->
- case t_is_float(X1) orelse t_is_float(X2) of
+ case
+ t_is_float(X1, Opaques) orelse t_is_float(X2, Opaques)
+ of
true -> t_float();
false -> t_number()
end
end
- end);
-type(erlang, '*', 2, Xs) ->
- strict(arg_types(erlang, '*', 2), Xs,
+ end, Opaques);
+type(erlang, '*', 2, Xs, Opaques) ->
+ strict(erlang, '*', 2, Xs,
fun ([X1, X2]) ->
- case arith('*', X1, X2) of
+ case arith('*', X1, X2, Opaques) of
{ok, T} -> T;
error ->
- case t_is_float(X1) orelse t_is_float(X2) of
+ case
+ t_is_float(X1, Opaques) orelse t_is_float(X2, Opaques)
+ of
true -> t_float();
false -> t_number()
end
end
- end);
-type(erlang, '/', 2, Xs) ->
- strict(arg_types(erlang, '/', 2), Xs,
- fun (_) -> t_float() end);
-type(erlang, 'div', 2, Xs) ->
- strict(arg_types(erlang, 'div', 2), Xs,
+ end, Opaques);
+type(erlang, '/', 2, Xs, Opaques) ->
+ strict(erlang, '/', 2, Xs, fun (_) -> t_float() end, Opaques);
+type(erlang, 'div', 2, Xs, Opaques) ->
+ strict(erlang, 'div', 2, Xs,
fun ([X1, X2]) ->
- case arith('div', X1, X2) of
+ case arith('div', X1, X2, Opaques) of
error -> t_integer();
{ok, T} -> T
end
- end);
-type(erlang, 'rem', 2, Xs) ->
- strict(arg_types(erlang, 'rem', 2), Xs,
+ end, Opaques);
+type(erlang, 'rem', 2, Xs, Opaques) ->
+ strict(erlang, 'rem', 2, Xs,
fun ([X1, X2]) ->
- case arith('rem', X1, X2) of
+ case arith('rem', X1, X2, Opaques) of
error -> t_non_neg_integer();
{ok, T} -> T
end
- end);
-type(erlang, '++', 2, Xs) ->
- strict(arg_types(erlang, '++', 2), Xs,
+ end, Opaques);
+type(erlang, '++', 2, Xs, Opaques) ->
+ strict(erlang, '++', 2, Xs,
fun ([X1, X2]) ->
- case t_is_nil(X1) of
+ case t_is_nil(X1, Opaques) of
true -> X2; % even if X2 is not a list
false ->
- case t_is_nil(X2) of
+ case t_is_nil(X2, Opaques) of
true -> X1;
false ->
- E1 = t_list_elements(X1),
- case t_is_cons(X1) of
+ E1 = t_list_elements(X1, Opaques),
+ case t_is_cons(X1, Opaques) of
true -> t_cons(E1, X2);
false ->
t_sup(X2, t_cons(E1, X2))
end
end
end
- end);
-type(erlang, '--', 2, Xs) ->
+ end, Opaques);
+type(erlang, '--', 2, Xs, Opaques) ->
%% We don't know which elements (if any) in X2 will be found and
%% removed from X1, even if they would have the same type. Thus, we
%% must assume that X1 can remain unchanged. However, if we succeed,
%% we know that X1 must be a proper list, but the result could
%% possibly be empty even if X1 is nonempty.
- strict(arg_types(erlang, '--', 2), Xs,
+ strict(erlang, '--', 2, Xs,
fun ([X1, X2]) ->
- case t_is_nil(X1) of
+ case t_is_nil(X1, Opaques) of
true -> t_nil();
false ->
- case t_is_nil(X2) of
+ case t_is_nil(X2, Opaques) of
true -> X1;
- false -> t_list(t_list_elements(X1))
+ false -> t_list(t_list_elements(X1, Opaques))
end
end
- end);
-type(erlang, 'and', 2, Xs) ->
- strict(arg_types(erlang, 'and', 2), Xs, fun (_) -> t_boolean() end);
-type(erlang, 'or', 2, Xs) ->
- strict(arg_types(erlang, 'or', 2), Xs, fun (_) -> t_boolean() end);
-type(erlang, 'xor', 2, Xs) ->
- strict(arg_types(erlang, 'xor', 2), Xs, fun (_) -> t_boolean() end);
-type(erlang, 'not', 1, Xs) ->
- strict(arg_types(erlang, 'not', 1), Xs, fun (_) -> t_boolean() end);
-type(erlang, 'band', 2, Xs) ->
- strict(arg_types(erlang, 'band', 2), Xs,
+ end, Opaques);
+type(erlang, 'and', 2, Xs, Opaques) ->
+ strict(erlang, 'and', 2, Xs, fun (_) -> t_boolean() end, Opaques);
+type(erlang, 'or', 2, Xs, Opaques) ->
+ strict(erlang, 'or', 2, Xs, fun (_) -> t_boolean() end, Opaques);
+type(erlang, 'xor', 2, Xs, Opaques) ->
+ strict(erlang, 'xor', 2, Xs, fun (_) -> t_boolean() end, Opaques);
+type(erlang, 'not', 1, Xs, Opaques) ->
+ strict(erlang, 'not', 1, Xs, fun (_) -> t_boolean() end, Opaques);
+type(erlang, 'band', 2, Xs, Opaques) ->
+ strict(erlang, 'band', 2, Xs,
fun ([X1, X2]) ->
- case arith('band', X1, X2) of
+ case arith('band', X1, X2, Opaques) of
error -> t_integer();
{ok, T} -> T
end
- end);
+ end, Opaques);
%% The result is not wider than the smallest argument. We need to
%% kill any value-sets in the result.
-%% strict(arg_types(erlang, 'band', 2), Xs,
-%% fun ([X1, X2]) -> t_sup(t_inf(X1, X2), t_byte()) end);
-type(erlang, 'bor', 2, Xs) ->
- strict(arg_types(erlang, 'bor', 2), Xs,
+%% strict(erlang, 'band', 2, Xs,
+%% fun ([X1, X2]) -> t_sup(t_inf(X1, X2, Opaques), t_byte()) end, Opaques);
+type(erlang, 'bor', 2, Xs, Opaques) ->
+ strict(erlang, 'bor', 2, Xs,
fun ([X1, X2]) ->
- case arith('bor', X1, X2) of
+ case arith('bor', X1, X2, Opaques) of
error -> t_integer();
{ok, T} -> T
end
- end);
+ end, Opaques);
%% The result is not wider than the largest argument. We need to
%% kill any value-sets in the result.
-%% strict(arg_types(erlang, 'bor', 2), Xs,
-%% fun ([X1, X2]) -> t_sup(t_sup(X1, X2), t_byte()) end);
-type(erlang, 'bxor', 2, Xs) ->
- strict(arg_types(erlang, 'bxor', 2), Xs,
+%% strict(erlang, 'bor', 2, Xs,
+%% fun ([X1, X2]) -> t_sup(t_sup(X1, X2), t_byte()) end, Opaques);
+type(erlang, 'bxor', 2, Xs, Opaques) ->
+ strict(erlang, 'bxor', 2, Xs,
fun ([X1, X2]) ->
- case arith('bxor', X1, X2) of
+ case arith('bxor', X1, X2, Opaques) of
error -> t_integer();
{ok, T} -> T
end
- end);
+ end, Opaques);
%% The result is not wider than the largest argument. We need to
%% kill any value-sets in the result.
-%% strict(arg_types(erlang, 'bxor', 2), Xs,
-%% fun ([X1, X2]) -> t_sup(t_sup(X1, X2), t_byte()) end);
-type(erlang, 'bsr', 2, Xs) ->
- strict(arg_types(erlang, 'bsr', 2), Xs,
+%% strict(erlang, 'bxor', 2, Xs,
+%% fun ([X1, X2]) -> t_sup(t_sup(X1, X2), t_byte()) end, Opaques);
+type(erlang, 'bsr', 2, Xs, Opaques) ->
+ strict(erlang, 'bsr', 2, Xs,
fun ([X1, X2]) ->
- case arith('bsr', X1, X2) of
+ case arith('bsr', X1, X2, Opaques) of
error -> t_integer();
{ok, T} -> T
end
- end);
+ end, Opaques);
%% If the first argument is unsigned (which is the case for
%% characters and bytes), the result is never wider. We need to kill
%% any value-sets in the result.
-%% strict(arg_types(erlang, 'bsr', 2), Xs,
-%% fun ([X, _]) -> t_sup(X, t_byte()) end);
-type(erlang, 'bsl', 2, Xs) ->
- strict(arg_types(erlang, 'bsl', 2), Xs,
+%% strict(erlang, 'bsr', 2, Xs,
+%% fun ([X, _]) -> t_sup(X, t_byte()) end, Opaques);
+type(erlang, 'bsl', 2, Xs, Opaques) ->
+ strict(erlang, 'bsl', 2, Xs,
fun ([X1, X2]) ->
- case arith('bsl', X1, X2) of
+ case arith('bsl', X1, X2, Opaques) of
error -> t_integer();
{ok, T} -> T
end
- end);
+ end, Opaques);
%% Not worth doing anything special here.
-%% strict(arg_types(erlang, 'bsl', 2), Xs, fun (_) -> t_integer() end);
-type(erlang, 'bnot', 1, Xs) ->
- strict(arg_types(erlang, 'bnot', 1), Xs,
+%% strict(erlang, 'bsl', 2, Xs, fun (_) -> t_integer() end, Opaques);
+type(erlang, 'bnot', 1, Xs, Opaques) ->
+ strict(erlang, 'bnot', 1, Xs,
fun ([X1]) ->
- case arith('bnot', X1) of
+ case arith('bnot', X1, Opaques) of
error -> t_integer();
{ok, T} -> T
end
- end);
+ end, Opaques);
%% Guard bif, needs to be here.
-type(erlang, abs, 1, Xs) ->
- strict(arg_types(erlang, abs, 1), Xs, fun ([X]) -> X end);
+type(erlang, abs, 1, Xs, Opaques) ->
+ strict(erlang, abs, 1, Xs, fun ([X]) -> X end, Opaques);
%% This returns (-X)-1, so it often gives a negative result.
-%% strict(arg_types(erlang, 'bnot', 1), Xs, fun (_) -> t_integer() end);
-type(erlang, append, 2, Xs) -> type(erlang, '++', 2, Xs); % alias
-type(erlang, apply, 2, Xs) ->
+%% strict(erlang, 'bnot', 1, Xs, fun (_) -> t_integer() end, Opaques);
+type(erlang, append, 2, Xs, _Opaques) -> type(erlang, '++', 2, Xs); % alias
+type(erlang, apply, 2, Xs, Opaques) ->
Fun = fun ([X, _Y]) ->
- case t_is_fun(X) of
+ case t_is_fun(X, Opaques) of
true ->
- t_fun_range(X);
+ t_fun_range(X, Opaques);
false ->
t_any()
end
end,
- strict(arg_types(erlang, apply, 2), Xs, Fun);
-type(erlang, apply, 3, Xs) ->
- strict(arg_types(erlang, apply, 3), Xs, fun (_) -> t_any() end);
+ strict(erlang, apply, 2, Xs, Fun, Opaques);
+type(erlang, apply, 3, Xs, Opaques) ->
+ strict(erlang, apply, 3, Xs, fun (_) -> t_any() end, Opaques);
%% Guard bif, needs to be here.
-type(erlang, binary_part, 2, Xs) ->
- strict(arg_types(erlang, binary_part, 2), Xs, fun (_) -> t_binary() end);
+type(erlang, binary_part, 2, Xs, Opaques) ->
+ strict(erlang, binary_part, 2, Xs, fun (_) -> t_binary() end, Opaques);
%% Guard bif, needs to be here.
-type(erlang, binary_part, 3, Xs) ->
- strict(arg_types(erlang, binary_part, 3), Xs, fun (_) -> t_binary() end);
+type(erlang, binary_part, 3, Xs, Opaques) ->
+ strict(erlang, binary_part, 3, Xs, fun (_) -> t_binary() end, Opaques);
%% Guard bif, needs to be here.
-type(erlang, bit_size, 1, Xs) ->
- strict(arg_types(erlang, bit_size, 1), Xs,
- fun (_) -> t_non_neg_integer() end);
+type(erlang, bit_size, 1, Xs, Opaques) ->
+ strict(erlang, bit_size, 1, Xs,
+ fun (_) -> t_non_neg_integer() end, Opaques);
%% Guard bif, needs to be here.
-type(erlang, byte_size, 1, Xs) ->
- strict(arg_types(erlang, byte_size, 1), Xs,
- fun (_) -> t_non_neg_integer() end);
-type(erlang, disconnect_node, 1, Xs) ->
- strict(arg_types(erlang, disconnect_node, 1), Xs, fun (_) -> t_sup([t_boolean(), t_atom('ignored')]) end);
+type(erlang, byte_size, 1, Xs, Opaques) ->
+ strict(erlang, byte_size, 1, Xs,
+ fun (_) -> t_non_neg_integer() end, Opaques);
+type(erlang, disconnect_node, 1, Xs, Opaques) ->
+ strict(erlang, disconnect_node, 1, Xs,
+ fun (_) -> t_sup([t_boolean(), t_atom('ignored')]) end, Opaques);
%% Guard bif, needs to be here.
%% Also much more expressive than anything you could write in a spec...
-type(erlang, element, 2, Xs) ->
- strict(arg_types(erlang, element, 2), Xs,
+type(erlang, element, 2, Xs, Opaques) ->
+ strict(erlang, element, 2, Xs,
fun ([X1, X2]) ->
- case t_tuple_subtypes(X2) of
+ case t_tuple_subtypes(X2, Opaques) of
unknown -> t_any();
[_] ->
- Sz = t_tuple_size(X2),
- As = t_tuple_args(X2),
- case t_number_vals(X1) of
+ Sz = t_tuple_size(X2, Opaques),
+ As = t_tuple_args(X2, Opaques),
+ case t_number_vals(X1, Opaques) of
unknown -> t_sup(As);
Ns when is_list(Ns) ->
Fun = fun
@@ -553,165 +577,166 @@ type(erlang, element, 2, Xs) ->
Ts when is_list(Ts) ->
t_sup([type(erlang, element, 2, [X1, Y]) || Y <- Ts])
end
- end);
+ end, Opaques);
%% Guard bif, needs to be here.
-type(erlang, float, 1, Xs) ->
- strict(arg_types(erlang, float, 1), Xs, fun (_) -> t_float() end);
-type(erlang, fun_info, 1, Xs) ->
- strict(arg_types(erlang, fun_info, 1), Xs,
- fun (_) -> t_list(t_tuple([t_atom(), t_any()])) end);
-type(erlang, get_cookie, 0, _) -> t_atom(); % | t_atom('nocookie')
+type(erlang, float, 1, Xs, Opaques) ->
+ strict(erlang, float, 1, Xs, fun (_) -> t_float() end, Opaques);
+type(erlang, fun_info, 1, Xs, Opaques) ->
+ strict(erlang, fun_info, 1, Xs,
+ fun (_) -> t_list(t_tuple([t_atom(), t_any()])) end, Opaques);
+type(erlang, get_cookie, 0, _, _Opaques) -> t_atom(); % | t_atom('nocookie')
%% Guard bif, needs to be here.
-type(erlang, hd, 1, Xs) ->
- strict(arg_types(erlang, hd, 1), Xs, fun ([X]) -> t_cons_hd(X) end);
-type(erlang, integer_to_list, 2, Xs) ->
- strict(arg_types(erlang, integer_to_list, 2), Xs,
- fun (_) -> t_string() end);
-type(erlang, info, 1, Xs) -> type(erlang, system_info, 1, Xs); % alias
+type(erlang, hd, 1, Xs, Opaques) ->
+ strict(erlang, hd, 1, Xs, fun ([X]) -> t_cons_hd(X) end, Opaques);
+type(erlang, integer_to_list, 2, Xs, Opaques) ->
+ strict(erlang, integer_to_list, 2, Xs,
+ fun (_) -> t_string() end, Opaques);
+type(erlang, info, 1, Xs, _) -> type(erlang, system_info, 1, Xs); % alias
%% All type tests are guard BIF's and may be implemented in ways that
%% cannot be expressed in a type spec, why they are kept in erl_bif_types.
-type(erlang, is_atom, 1, Xs) ->
- Fun = fun (X) -> check_guard(X, fun (Y) -> t_is_atom(Y) end, t_atom()) end,
- strict(arg_types(erlang, is_atom, 1), Xs, Fun);
-type(erlang, is_binary, 1, Xs) ->
+type(erlang, is_atom, 1, Xs, Opaques) ->
Fun = fun (X) ->
- check_guard(X, fun (Y) -> t_is_binary(Y) end, t_binary())
+ check_guard(X, fun (Y) -> t_is_atom(Y, Opaques) end,
+ t_atom(), Opaques)
+ end,
+ strict(erlang, is_atom, 1, Xs, Fun, Opaques);
+type(erlang, is_binary, 1, Xs, Opaques) ->
+ Fun = fun (X) ->
+ check_guard(X, fun (Y) -> t_is_binary(Y, Opaques) end,
+ t_binary(), Opaques)
end,
- strict(arg_types(erlang, is_binary, 1), Xs, Fun);
-type(erlang, is_bitstring, 1, Xs) ->
+ strict(erlang, is_binary, 1, Xs, Fun, Opaques);
+type(erlang, is_bitstring, 1, Xs, Opaques) ->
Fun = fun (X) ->
- check_guard(X, fun (Y) -> t_is_bitstr(Y) end, t_bitstr())
+ check_guard(X, fun (Y) -> t_is_bitstr(Y, Opaques) end,
+ t_bitstr(), Opaques)
end,
- strict(arg_types(erlang, is_bitstring, 1), Xs, Fun);
-type(erlang, is_boolean, 1, Xs) ->
+ strict(erlang, is_bitstring, 1, Xs, Fun, Opaques);
+type(erlang, is_boolean, 1, Xs, Opaques) ->
Fun = fun (X) ->
- check_guard(X, fun (Y) -> t_is_boolean(Y) end, t_boolean())
+ check_guard(X, fun (Y) -> t_is_boolean(Y, Opaques) end,
+ t_boolean(), Opaques)
end,
- strict(arg_types(erlang, is_boolean, 1), Xs, Fun);
-type(erlang, is_float, 1, Xs) ->
+ strict(erlang, is_boolean, 1, Xs, Fun, Opaques);
+type(erlang, is_float, 1, Xs, Opaques) ->
Fun = fun (X) ->
- check_guard(X, fun (Y) -> t_is_float(Y) end, t_float())
+ check_guard(X, fun (Y) -> t_is_float(Y, Opaques) end,
+ t_float(), Opaques)
end,
- strict(arg_types(erlang, is_float, 1), Xs, Fun);
-type(erlang, is_function, 1, Xs) ->
- Fun = fun (X) -> check_guard(X, fun (Y) -> t_is_fun(Y) end, t_fun()) end,
- strict(arg_types(erlang, is_function, 1), Xs, Fun);
-type(erlang, is_function, 2, Xs) ->
+ strict(erlang, is_float, 1, Xs, Fun, Opaques);
+type(erlang, is_function, 1, Xs, Opaques) ->
+ Fun = fun (X) ->
+ check_guard(X, fun (Y) -> t_is_fun(Y, Opaques) end,
+ t_fun(), Opaques)
+ end,
+ strict(erlang, is_function, 1, Xs, Fun, Opaques);
+type(erlang, is_function, 2, Xs, Opaques) ->
Fun = fun ([FunType, ArityType]) ->
- case t_number_vals(ArityType) of
+ case t_number_vals(ArityType, Opaques) of
unknown -> t_boolean();
[Val] ->
FunConstr = t_fun(any_list(Val), t_any()),
Fun2 = fun (X) ->
t_is_subtype(X, FunConstr) andalso (not t_is_none(X))
end,
- check_guard_single(FunType, Fun2, FunConstr);
+ check_guard_single(FunType, Fun2, FunConstr, Opaques);
IntList when is_list(IntList) -> t_boolean() %% true?
end
end,
- strict(arg_types(erlang, is_function, 2), Xs, Fun);
-type(erlang, is_integer, 1, Xs) ->
+ strict(erlang, is_function, 2, Xs, Fun, Opaques);
+type(erlang, is_integer, 1, Xs, Opaques) ->
Fun = fun (X) ->
- check_guard(X, fun (Y) -> t_is_integer(Y) end, t_integer())
+ check_guard(X, fun (Y) -> t_is_integer(Y, Opaques) end,
+ t_integer(), Opaques)
end,
- strict(arg_types(erlang, is_integer, 1), Xs, Fun);
-type(erlang, is_list, 1, Xs) ->
+ strict(erlang, is_integer, 1, Xs, Fun, Opaques);
+type(erlang, is_list, 1, Xs, Opaques) ->
Fun = fun (X) ->
- Fun2 = fun (Y) -> t_is_maybe_improper_list(Y) end,
- check_guard(X, Fun2, t_maybe_improper_list())
+ Fun2 = fun (Y) -> t_is_maybe_improper_list(Y, Opaques) end,
+ check_guard(X, Fun2, t_maybe_improper_list(), Opaques)
end,
- strict(arg_types(erlang, is_list, 1), Xs, Fun);
-type(erlang, is_number, 1, Xs) ->
+ strict(erlang, is_list, 1, Xs, Fun, Opaques);
+type(erlang, is_map, 1, Xs, Opaques) ->
+ Fun = fun (X) ->
+ check_guard(X, fun (Y) -> t_is_map(Y, Opaques) end,
+ t_map(), Opaques) end,
+ strict(erlang, is_map, 1, Xs, Fun, Opaques);
+type(erlang, is_number, 1, Xs, Opaques) ->
Fun = fun (X) ->
- check_guard(X, fun (Y) -> t_is_number(Y) end, t_number())
+ check_guard(X, fun (Y) -> t_is_number(Y, Opaques) end,
+ t_number(), Opaques)
end,
- strict(arg_types(erlang, is_number, 1), Xs, Fun);
-type(erlang, is_pid, 1, Xs) ->
- Fun = fun (X) -> check_guard(X, fun (Y) -> t_is_pid(Y) end, t_pid()) end,
- strict(arg_types(erlang, is_pid, 1), Xs, Fun);
-type(erlang, is_port, 1, Xs) ->
- Fun = fun (X) -> check_guard(X, fun (Y) -> t_is_port(Y) end, t_port()) end,
- strict(arg_types(erlang, is_port, 1), Xs, Fun);
-type(erlang, is_record, 2, Xs) ->
+ strict(erlang, is_number, 1, Xs, Fun, Opaques);
+type(erlang, is_pid, 1, Xs, Opaques) ->
+ Fun = fun (X) ->
+ check_guard(X, fun (Y) -> t_is_pid(Y, Opaques) end,
+ t_pid(), Opaques)
+ end,
+ strict(erlang, is_pid, 1, Xs, Fun, Opaques);
+type(erlang, is_port, 1, Xs, Opaques) ->
+ Fun = fun (X) ->
+ check_guard(X, fun (Y) -> t_is_port(Y, Opaques) end,
+ t_port(), Opaques)
+ end,
+ strict(erlang, is_port, 1, Xs, Fun, Opaques);
+type(erlang, is_record, 2, Xs, Opaques) ->
Fun = fun ([X, Y]) ->
- case t_is_tuple(X) of
+ case t_is_tuple(X, Opaques) of
false ->
- case t_is_none(t_inf(t_tuple(), X)) of
- true -> t_atom('false');
+ case t_is_none(t_inf(t_tuple(), X, Opaques)) of
+ true ->
+ case t_has_opaque_subtype(X, Opaques) of
+ true -> t_none();
+ false -> t_atom('false')
+ end;
false -> t_boolean()
end;
true ->
- case t_tuple_subtypes(X) of
+ case t_tuple_subtypes(X, Opaques) of
unknown -> t_boolean();
[Tuple] ->
- case t_tuple_args(Tuple) of
+ case t_tuple_args(Tuple, Opaques) of
%% any -> t_boolean();
- [Tag|_] ->
- case t_is_atom(Tag) of
- false ->
- TagAtom = t_inf(Tag, t_atom()),
- case t_is_none(TagAtom) of
- true -> t_atom('false');
- false -> t_boolean()
- end;
- true ->
- case t_atom_vals(Tag) of
- [RealTag] ->
- case t_atom_vals(Y) of
- [RealTag] -> t_atom('true');
- _ -> t_boolean()
- end;
- _ -> t_boolean()
- end
- end
+ [Tag|_] -> check_record_tag(Tag, Y, Opaques)
end;
List when length(List) >= 2 ->
t_sup([type(erlang, is_record, 2, [T, Y]) || T <- List])
end
end
end,
- strict(arg_types(erlang, is_record, 2), Xs, Fun);
-type(erlang, is_record, 3, Xs) ->
+ strict(erlang, is_record, 2, Xs, Fun, Opaques);
+type(erlang, is_record, 3, Xs, Opaques) ->
Fun = fun ([X, Y, Z]) ->
- Arity = t_number_vals(Z),
- case t_is_tuple(X) of
+ Arity = t_number_vals(Z, Opaques),
+ case t_is_tuple(X, Opaques) of
false when length(Arity) =:= 1 ->
[RealArity] = Arity,
- case t_is_none(t_inf(t_tuple(RealArity), X)) of
- true -> t_atom('false');
+ case t_is_none(t_inf(t_tuple(RealArity), X, Opaques)) of
+ true ->
+ case t_has_opaque_subtype(X, Opaques) of
+ true -> t_none();
+ false -> t_atom('false')
+ end;
false -> t_boolean()
end;
false ->
- case t_is_none(t_inf(t_tuple(), X)) of
- true -> t_atom('false');
+ case t_is_none(t_inf(t_tuple(), X, Opaques)) of
+ true ->
+ case t_has_opaque_subtype(X, Opaques) of
+ true -> t_none();
+ false -> t_atom('false')
+ end;
false -> t_boolean()
end;
true when length(Arity) =:= 1 ->
[RealArity] = Arity,
- case t_tuple_subtypes(X) of
+ case t_tuple_subtypes(X, Opaques) of
unknown -> t_boolean();
[Tuple] ->
- case t_tuple_args(Tuple) of
+ case t_tuple_args(Tuple, Opaques) of
%% any -> t_boolean();
Args when length(Args) =:= RealArity ->
- Tag = hd(Args),
- case t_is_atom(Tag) of
- false ->
- TagAtom = t_inf(Tag, t_atom()),
- case t_is_none(TagAtom) of
- true -> t_atom('false');
- false -> t_boolean()
- end;
- true ->
- case t_atom_vals(Tag) of
- [RealTag] ->
- case t_atom_vals(Y) of
- [RealTag] -> t_atom('true');
- _ -> t_boolean()
- end;
- _ -> t_boolean()
- end
- end;
+ check_record_tag(hd(Args), Y, Opaques);
Args when length(Args) =/= RealArity ->
t_atom('false')
end;
@@ -722,62 +747,69 @@ type(erlang, is_record, 3, Xs) ->
t_boolean()
end
end,
- strict(arg_types(erlang, is_record, 3), Xs, Fun);
-type(erlang, is_reference, 1, Xs) ->
+ strict(erlang, is_record, 3, Xs, Fun, Opaques);
+type(erlang, is_reference, 1, Xs, Opaques) ->
Fun = fun (X) ->
- check_guard(X, fun (Y) -> t_is_reference(Y) end, t_reference())
+ check_guard(X, fun (Y) -> t_is_reference(Y, Opaques) end,
+ t_reference(), Opaques)
end,
- strict(arg_types(erlang, is_reference, 1), Xs, Fun);
-type(erlang, is_tuple, 1, Xs) ->
+ strict(erlang, is_reference, 1, Xs, Fun, Opaques);
+type(erlang, is_tuple, 1, Xs, Opaques) ->
Fun = fun (X) ->
- check_guard(X, fun (Y) -> t_is_tuple(Y) end, t_tuple())
+ check_guard(X, fun (Y) -> t_is_tuple(Y, Opaques) end,
+ t_tuple(), Opaques)
end,
- strict(arg_types(erlang, is_tuple, 1), Xs, Fun);
+ strict(erlang, is_tuple, 1, Xs, Fun, Opaques);
+%% Guard bif, needs to be here.
+type(erlang, length, 1, Xs, Opaques) ->
+ strict(erlang, length, 1, Xs, fun (_) -> t_non_neg_fixnum() end, Opaques);
%% Guard bif, needs to be here.
-type(erlang, length, 1, Xs) ->
- strict(arg_types(erlang, length, 1), Xs, fun (_) -> t_non_neg_fixnum() end);
-type(erlang, make_tuple, 2, Xs) ->
- strict(arg_types(erlang, make_tuple, 2), Xs,
+type(erlang, map_size, 1, Xs, Opaques) ->
+ strict(erlang, map_size, 1, Xs, fun (_) -> t_non_neg_integer() end, Opaques);
+type(erlang, make_tuple, 2, Xs, Opaques) ->
+ strict(erlang, make_tuple, 2, Xs,
fun ([Int, _]) ->
- case t_number_vals(Int) of
+ case t_number_vals(Int, Opaques) of
[N] when is_integer(N), N >= 0 -> t_tuple(N);
_Other -> t_tuple()
end
- end);
-type(erlang, make_tuple, 3, Xs) ->
- strict(arg_types(erlang, make_tuple, 3), Xs,
+ end, Opaques);
+type(erlang, make_tuple, 3, Xs, Opaques) ->
+ strict(erlang, make_tuple, 3, Xs,
fun ([Int, _, _]) ->
- case t_number_vals(Int) of
+ case t_number_vals(Int, Opaques) of
[N] when is_integer(N), N >= 0 -> t_tuple(N);
_Other -> t_tuple()
end
- end);
-type(erlang, memory, 0, _) -> t_list(t_tuple([t_atom(), t_non_neg_fixnum()]));
-type(erlang, nif_error, 1, _) ->
- t_any(); % this BIF and the next one are stubs for NIFs and never return
-type(erlang, nif_error, 2, Xs) ->
- strict(arg_types(erlang, nif_error, 2), Xs, fun (_) -> t_any() end);
+ end, Opaques);
+type(erlang, memory, 0, _, _Opaques) ->
+ t_list(t_tuple([t_atom(), t_non_neg_fixnum()]));
+type(erlang, nif_error, 1, Xs, Opaques) ->
+ %% this BIF and the next one are stubs for NIFs and never return
+ strict(erlang, nif_error, 1, Xs, fun (_) -> t_any() end, Opaques);
+type(erlang, nif_error, 2, Xs, Opaques) ->
+ strict(erlang, nif_error, 2, Xs, fun (_) -> t_any() end, Opaques);
%% Guard bif, needs to be here.
-type(erlang, node, 0, _) -> t_node();
+type(erlang, node, 0, _, _Opaques) -> t_node();
%% Guard bif, needs to be here.
-type(erlang, node, 1, Xs) ->
- strict(arg_types(erlang, node, 1), Xs, fun (_) -> t_node() end);
+type(erlang, node, 1, Xs, Opaques) ->
+ strict(erlang, node, 1, Xs, fun (_) -> t_node() end, Opaques);
%% Guard bif, needs to be here.
-type(erlang, round, 1, Xs) ->
- strict(arg_types(erlang, round, 1), Xs, fun (_) -> t_integer() end);
+type(erlang, round, 1, Xs, Opaques) ->
+ strict(erlang, round, 1, Xs, fun (_) -> t_integer() end, Opaques);
%% Guard bif, needs to be here.
-type(erlang, self, 0, _) -> t_pid();
-type(erlang, set_cookie, 2, Xs) ->
- strict(arg_types(erlang, set_cookie, 2), Xs, fun (_) -> t_atom('true') end);
-type(erlang, setelement, 3, Xs) ->
- strict(arg_types(erlang, setelement, 3), Xs,
+type(erlang, self, 0, _, _Opaques) -> t_pid();
+type(erlang, set_cookie, 2, Xs, Opaques) ->
+ strict(erlang, set_cookie, 2, Xs, fun (_) -> t_atom('true') end, Opaques);
+type(erlang, setelement, 3, Xs, Opaques) ->
+ strict(erlang, setelement, 3, Xs,
fun ([X1, X2, X3]) ->
- case t_tuple_subtypes(X2) of
+ case t_tuple_subtypes(X2, Opaques) of
unknown -> t_tuple();
[_] ->
- Sz = t_tuple_size(X2),
- As = t_tuple_args(X2),
- case t_number_vals(X1) of
+ Sz = t_tuple_size(X2, Opaques),
+ As = t_tuple_args(X2, Opaques),
+ case t_number_vals(X1, Opaques) of
unknown ->
t_tuple([t_sup(X, X3) || X <- As]);
[N] when is_integer(N), 1 =< N, N =< Sz ->
@@ -799,29 +831,29 @@ type(erlang, setelement, 3, Xs) ->
Ts when is_list(Ts) ->
t_sup([type(erlang, setelement, 3, [X1, Y, X3]) || Y <- Ts])
end
- end);
+ end, Opaques);
%% Guard bif, needs to be here.
-type(erlang, size, 1, Xs) ->
- strict(arg_types(erlang, size, 1), Xs, fun (_) -> t_non_neg_integer() end);
-type(erlang, spawn, 1, Xs) ->
- strict(arg_types(erlang, spawn, 1), Xs, fun (_) -> t_pid() end);
-type(erlang, spawn, 2, Xs) ->
- strict(arg_types(erlang, spawn, 2), Xs, fun (_) -> t_pid() end);
-type(erlang, spawn, 4, Xs) ->
- strict(arg_types(erlang, spawn, 4), Xs, fun (_) -> t_pid() end);
-type(erlang, spawn_link, 1, Xs) -> type(erlang, spawn, 1, Xs); % same
-type(erlang, spawn_link, 2, Xs) -> type(erlang, spawn, 2, Xs); % same
-type(erlang, spawn_link, 4, Xs) -> type(erlang, spawn, 4, Xs); % same
-type(erlang, subtract, 2, Xs) -> type(erlang, '--', 2, Xs); % alias
-type(erlang, suspend_process, 1, Xs) ->
- strict(arg_types(erlang, suspend_process, 1), Xs,
- fun (_) -> t_atom('true') end);
-type(erlang, system_info, 1, Xs) ->
- strict(arg_types(erlang, system_info, 1), Xs,
+type(erlang, size, 1, Xs, Opaques) ->
+ strict(erlang, size, 1, Xs, fun (_) -> t_non_neg_integer() end, Opaques);
+type(erlang, spawn, 1, Xs, Opaques) ->
+ strict(erlang, spawn, 1, Xs, fun (_) -> t_pid() end, Opaques);
+type(erlang, spawn, 2, Xs, Opaques) ->
+ strict(erlang, spawn, 2, Xs, fun (_) -> t_pid() end, Opaques);
+type(erlang, spawn, 4, Xs, Opaques) ->
+ strict(erlang, spawn, 4, Xs, fun (_) -> t_pid() end, Opaques);
+type(erlang, spawn_link, 1, Xs, _) -> type(erlang, spawn, 1, Xs); % same
+type(erlang, spawn_link, 2, Xs, _) -> type(erlang, spawn, 2, Xs); % same
+type(erlang, spawn_link, 4, Xs, _) -> type(erlang, spawn, 4, Xs); % same
+type(erlang, subtract, 2, Xs, _Opaques) -> type(erlang, '--', 2, Xs); % alias
+type(erlang, suspend_process, 1, Xs, Opaques) ->
+ strict(erlang, suspend_process, 1, Xs,
+ fun (_) -> t_atom('true') end, Opaques);
+type(erlang, system_info, 1, Xs, Opaques) ->
+ strict(erlang, system_info, 1, Xs,
fun ([Type]) ->
- case t_is_atom(Type) of
+ case t_is_atom(Type, Opaques) of
true ->
- case t_atom_vals(Type) of
+ case t_atom_vals(Type, Opaques) of
['allocated_areas'] ->
t_list(t_sup([t_tuple([t_atom(),t_non_neg_integer()]),
t_tuple([t_atom(),
@@ -880,7 +912,8 @@ type(erlang, system_info, 1, Xs) ->
t_list(t_pid());
['os_type'] ->
t_tuple([t_sup([t_atom('unix'),
- t_atom('win32')]),
+ t_atom('win32'),
+ t_atom('ose')]),
t_atom()]);
['os_version'] ->
t_sup(t_tuple([t_non_neg_fixnum(),
@@ -936,26 +969,28 @@ type(erlang, system_info, 1, Xs) ->
false -> %% This currently handles only {allocator, Alloc}
t_any() %% overapproximation as the return value might change
end
- end);
+ end, Opaques);
%% Guard bif, needs to be here.
-type(erlang, tl, 1, Xs) ->
- strict(arg_types(erlang, tl, 1), Xs, fun ([X]) -> t_cons_tl(X) end);
+type(erlang, tl, 1, Xs, Opaques) ->
+ strict(erlang, tl, 1, Xs, fun ([X]) -> t_cons_tl(X) end, Opaques);
%% Guard bif, needs to be here.
-type(erlang, trunc, 1, Xs) ->
- strict(arg_types(erlang, trunc, 1), Xs, fun (_) -> t_integer() end);
+type(erlang, trunc, 1, Xs, Opaques) ->
+ strict(erlang, trunc, 1, Xs, fun (_) -> t_integer() end, Opaques);
%% Guard bif, needs to be here.
-type(erlang, tuple_size, 1, Xs) ->
- strict(arg_types(erlang, tuple_size, 1), Xs, fun (_) -> t_non_neg_integer() end);
-type(erlang, tuple_to_list, 1, Xs) ->
- strict(arg_types(erlang, tuple_to_list, 1), Xs,
+type(erlang, tuple_size, 1, Xs, Opaques) ->
+ strict(erlang, tuple_size, 1, Xs,
+ fun (_) -> t_non_neg_integer() end, Opaques);
+type(erlang, tuple_to_list, 1, Xs, Opaques) ->
+ strict(erlang, tuple_to_list, 1, Xs,
fun ([X]) ->
- case t_tuple_subtypes(X) of
+ case t_tuple_subtypes(X, Opaques) of
unknown -> t_list();
SubTypes ->
- Args = lists:flatten([t_tuple_args(ST) || ST <- SubTypes]),
+ Args = lists:append([t_tuple_args(ST, Opaques) ||
+ ST <- SubTypes]),
%% Can be nil if the tuple can be {}
case lists:any(fun (T) ->
- t_tuple_size(T) =:= 0
+ t_tuple_size(T, Opaques) =:= 0
end, SubTypes) of
true ->
%% Be careful here. If we had only {} we need to
@@ -965,279 +1000,287 @@ type(erlang, tuple_to_list, 1, Xs) ->
t_nonempty_list(t_sup(Args))
end
end
- end);
-type(erlang, yield, 0, _) -> t_atom('true');
+ end, Opaques);
+type(erlang, yield, 0, _, _Opaques) -> t_atom('true');
%%-- ets ----------------------------------------------------------------------
-type(ets, rename, 2, Xs) ->
- strict(arg_types(ets, rename, 2), Xs, fun ([_, Name]) -> Name end);
+type(ets, rename, 2, Xs, Opaques) ->
+ strict(ets, rename, 2, Xs, fun ([_, Name]) -> Name end, Opaques);
%%-- hipe_bifs ----------------------------------------------------------------
-type(hipe_bifs, add_ref, 2, Xs) ->
- strict(arg_types(hipe_bifs, add_ref, 2), Xs, fun (_) -> t_nil() end);
-type(hipe_bifs, alloc_data, 2, Xs) ->
- strict(arg_types(hipe_bifs, alloc_data, 2), Xs,
- fun (_) -> t_integer() end); % address
-type(hipe_bifs, array, 2, Xs) ->
- strict(arg_types(hipe_bifs, array, 2), Xs, fun (_) -> t_immarray() end);
-type(hipe_bifs, array_length, 1, Xs) ->
- strict(arg_types(hipe_bifs, array_length, 1), Xs,
- fun (_) -> t_non_neg_fixnum() end);
-type(hipe_bifs, array_sub, 2, Xs) ->
- strict(arg_types(hipe_bifs, array_sub, 2), Xs, fun (_) -> t_immediate() end);
-type(hipe_bifs, array_update, 3, Xs) ->
- strict(arg_types(hipe_bifs, array_update, 3), Xs,
- fun (_) -> t_immarray() end);
-type(hipe_bifs, atom_to_word, 1, Xs) ->
- strict(arg_types(hipe_bifs, atom_to_word, 1), Xs,
- fun (_) -> t_integer() end);
-type(hipe_bifs, bif_address, 3, Xs) ->
- strict(arg_types(hipe_bifs, bif_address, 3), Xs,
- fun (_) -> t_sup(t_integer(), t_atom('false')) end);
-type(hipe_bifs, bitarray, 2, Xs) ->
- strict(arg_types(hipe_bifs, bitarray, 2), Xs, fun (_) -> t_bitarray() end);
-type(hipe_bifs, bitarray_sub, 2, Xs) ->
- strict(arg_types(hipe_bifs, bitarray_sub, 2), Xs, fun (_) -> t_boolean() end);
-type(hipe_bifs, bitarray_update, 3, Xs) ->
- strict(arg_types(hipe_bifs, bitarray_update, 3), Xs,
- fun (_) -> t_bitarray() end);
-type(hipe_bifs, bytearray, 2, Xs) ->
- strict(arg_types(hipe_bifs, bytearray, 2), Xs, fun (_) -> t_bytearray() end);
-type(hipe_bifs, bytearray_sub, 2, Xs) ->
- strict(arg_types(hipe_bifs, bytearray_sub, 2), Xs, fun (_) -> t_byte() end);
-type(hipe_bifs, bytearray_update, 3, Xs) ->
- strict(arg_types(hipe_bifs, bytearray_update, 3), Xs,
- fun (_) -> t_bytearray() end);
-type(hipe_bifs, call_count_clear, 1, Xs) ->
- strict(arg_types(hipe_bifs, call_count_clear, 1), Xs,
- fun (_) -> t_sup(t_non_neg_integer(), t_atom('false')) end);
-type(hipe_bifs, call_count_get, 1, Xs) ->
- strict(arg_types(hipe_bifs, call_count_get, 1), Xs,
- fun (_) -> t_sup(t_non_neg_integer(), t_atom('false')) end);
-type(hipe_bifs, call_count_off, 1, Xs) ->
- strict(arg_types(hipe_bifs, call_count_off, 1), Xs,
- fun (_) -> t_sup(t_non_neg_integer(), t_atom('false')) end);
-type(hipe_bifs, call_count_on, 1, Xs) ->
- strict(arg_types(hipe_bifs, call_count_on, 1), Xs,
- fun (_) -> t_sup(t_atom('true'), t_nil()) end);
-type(hipe_bifs, check_crc, 1, Xs) ->
- strict(arg_types(hipe_bifs, check_crc, 1), Xs, fun (_) -> t_boolean() end);
-type(hipe_bifs, enter_code, 2, Xs) ->
- strict(arg_types(hipe_bifs, enter_code, 2), Xs,
+type(hipe_bifs, add_ref, 2, Xs, Opaques) ->
+ strict(hipe_bifs, add_ref, 2, Xs, fun (_) -> t_nil() end, Opaques);
+type(hipe_bifs, alloc_data, 2, Xs, Opaques) ->
+ strict(hipe_bifs, alloc_data, 2, Xs,
+ fun (_) -> t_integer() end, Opaques); % address
+type(hipe_bifs, array, 2, Xs, Opaques) ->
+ strict(hipe_bifs, array, 2, Xs, fun (_) -> t_immarray() end, Opaques);
+type(hipe_bifs, array_length, 1, Xs, Opaques) ->
+ strict(hipe_bifs, array_length, 1, Xs,
+ fun (_) -> t_non_neg_fixnum() end, Opaques);
+type(hipe_bifs, array_sub, 2, Xs, Opaques) ->
+ strict(hipe_bifs, array_sub, 2, Xs, fun (_) -> t_immediate() end, Opaques);
+type(hipe_bifs, array_update, 3, Xs, Opaques) ->
+ strict(hipe_bifs, array_update, 3, Xs,
+ fun (_) -> t_immarray() end, Opaques);
+type(hipe_bifs, atom_to_word, 1, Xs, Opaques) ->
+ strict(hipe_bifs, atom_to_word, 1, Xs,
+ fun (_) -> t_integer() end, Opaques);
+type(hipe_bifs, bif_address, 3, Xs, Opaques) ->
+ strict(hipe_bifs, bif_address, 3, Xs,
+ fun (_) -> t_sup(t_integer(), t_atom('false')) end, Opaques);
+type(hipe_bifs, bitarray, 2, Xs, Opaques) ->
+ strict(hipe_bifs, bitarray, 2, Xs, fun (_) -> t_bitarray() end, Opaques);
+type(hipe_bifs, bitarray_sub, 2, Xs, Opaques) ->
+ strict(hipe_bifs, bitarray_sub, 2, Xs,
+ fun (_) -> t_boolean() end, Opaques);
+type(hipe_bifs, bitarray_update, 3, Xs, Opaques) ->
+ strict(hipe_bifs, bitarray_update, 3, Xs,
+ fun (_) -> t_bitarray() end, Opaques);
+type(hipe_bifs, bytearray, 2, Xs, Opaques) ->
+ strict(hipe_bifs, bytearray, 2, Xs, fun (_) -> t_bytearray() end, Opaques);
+type(hipe_bifs, bytearray_sub, 2, Xs, Opaques) ->
+ strict(hipe_bifs, bytearray_sub, 2, Xs, fun (_) -> t_byte() end, Opaques);
+type(hipe_bifs, bytearray_update, 3, Xs, Opaques) ->
+ strict(hipe_bifs, bytearray_update, 3, Xs,
+ fun (_) -> t_bytearray() end, Opaques);
+type(hipe_bifs, call_count_clear, 1, Xs, Opaques) ->
+ strict(hipe_bifs, call_count_clear, 1, Xs,
+ fun (_) -> t_sup(t_non_neg_integer(), t_atom('false')) end, Opaques);
+type(hipe_bifs, call_count_get, 1, Xs, Opaques) ->
+ strict(hipe_bifs, call_count_get, 1, Xs,
+ fun (_) -> t_sup(t_non_neg_integer(), t_atom('false')) end, Opaques);
+type(hipe_bifs, call_count_off, 1, Xs, Opaques) ->
+ strict(hipe_bifs, call_count_off, 1, Xs,
+ fun (_) -> t_sup(t_non_neg_integer(), t_atom('false')) end, Opaques);
+type(hipe_bifs, call_count_on, 1, Xs, Opaques) ->
+ strict(hipe_bifs, call_count_on, 1, Xs,
+ fun (_) -> t_sup(t_atom('true'), t_nil()) end, Opaques);
+type(hipe_bifs, check_crc, 1, Xs, Opaques) ->
+ strict(hipe_bifs, check_crc, 1, Xs, fun (_) -> t_boolean() end, Opaques);
+type(hipe_bifs, enter_code, 2, Xs, Opaques) ->
+ strict(hipe_bifs, enter_code, 2, Xs,
fun (_) -> t_tuple([t_integer(),
%% XXX: The tuple below contains integers and
%% is of size same as the length of the MFA list
- t_sup(t_nil(), t_binary())]) end);
-type(hipe_bifs, enter_sdesc, 1, Xs) ->
- strict(arg_types(hipe_bifs, enter_sdesc, 1), Xs, fun (_) -> t_nil() end);
-type(hipe_bifs, find_na_or_make_stub, 2, Xs) ->
- strict(arg_types(hipe_bifs, find_na_or_make_stub, 2), Xs,
- fun (_) -> t_integer() end); % address
-type(hipe_bifs, fun_to_address, 1, Xs) ->
- strict(arg_types(hipe_bifs, fun_to_address, 1), Xs,
- fun (_) -> t_integer() end);
-%% type(hipe_bifs, get_emu_address, 1, Xs) ->
-%% strict(arg_types(hipe_bifs, get_emu_address, 1), Xs,
-%% fun (_) -> t_integer() end); % address
-type(hipe_bifs, get_rts_param, 1, Xs) ->
- strict(arg_types(hipe_bifs, get_rts_param, 1), Xs,
- fun (_) -> t_sup(t_integer(), t_nil()) end);
-type(hipe_bifs, invalidate_funinfo_native_addresses, 1, Xs) ->
- strict(arg_types(hipe_bifs, invalidate_funinfo_native_addresses, 1), Xs,
- fun (_) -> t_nil() end);
-type(hipe_bifs, make_fe, 3, Xs) ->
- strict(arg_types(hipe_bifs, make_fe, 3), Xs, fun (_) -> t_integer() end);
-%% type(hipe_bifs, make_native_stub, 2, Xs) ->
-%% strict(arg_types(hipe_bifs, make_native_stub, 2), Xs,
-%% fun (_) -> t_integer() end); % address
-type(hipe_bifs, mark_referred_from, 1, Xs) ->
- strict(arg_types(hipe_bifs, mark_referred_from, 1), Xs,
- fun (_) -> t_nil() end);
-type(hipe_bifs, merge_term, 1, Xs) ->
- strict(arg_types(hipe_bifs, merge_term, 1), Xs, fun ([X]) -> X end);
-type(hipe_bifs, nstack_used_size, 0, _) ->
+ t_sup(t_nil(), t_binary())]) end, Opaques);
+type(hipe_bifs, enter_sdesc, 1, Xs, Opaques) ->
+ strict(hipe_bifs, enter_sdesc, 1, Xs, fun (_) -> t_nil() end, Opaques);
+type(hipe_bifs, find_na_or_make_stub, 2, Xs, Opaques) ->
+ strict(hipe_bifs, find_na_or_make_stub, 2, Xs,
+ fun (_) -> t_integer() end, Opaques); % address
+type(hipe_bifs, fun_to_address, 1, Xs, Opaques) ->
+ strict(hipe_bifs, fun_to_address, 1, Xs,
+ fun (_) -> t_integer() end, Opaques);
+%% type(hipe_bifs, get_emu_address, 1, Xs, Opaques) ->
+%% strict(hipe_bifs, get_emu_address, 1, Xs,
+%% fun (_) -> t_integer() end, Opaques); % address
+type(hipe_bifs, get_fe, 2, Xs, Opaques) ->
+ strict(hipe_bifs, get_fe, 2, Xs, fun (_) -> t_integer() end, Opaques);
+type(hipe_bifs, get_rts_param, 1, Xs, Opaques) ->
+ strict(hipe_bifs, get_rts_param, 1, Xs,
+ fun (_) -> t_sup(t_integer(), t_nil()) end, Opaques);
+type(hipe_bifs, invalidate_funinfo_native_addresses, 1, Xs, Opaques) ->
+ strict(hipe_bifs, invalidate_funinfo_native_addresses, 1, Xs,
+ fun (_) -> t_nil() end, Opaques);
+%% type(hipe_bifs, make_native_stub, 2, Xs, Opaques) ->
+%% strict(hipe_bifs, make_native_stub, 2, Xs,
+%% fun (_) -> t_integer() end, Opaques); % address
+type(hipe_bifs, mark_referred_from, 1, Xs, Opaques) ->
+ strict(hipe_bifs, mark_referred_from, 1, Xs,
+ fun (_) -> t_nil() end, Opaques);
+type(hipe_bifs, merge_term, 1, Xs, Opaques) ->
+ strict(hipe_bifs, merge_term, 1, Xs, fun ([X]) -> X end, Opaques);
+type(hipe_bifs, nstack_used_size, 0, _, _Opaques) ->
t_non_neg_fixnum();
-type(hipe_bifs, patch_call, 3, Xs) ->
- strict(arg_types(hipe_bifs, patch_call, 3), Xs, fun (_) -> t_nil() end);
-type(hipe_bifs, patch_insn, 3, Xs) ->
- strict(arg_types(hipe_bifs, patch_insn, 3), Xs, fun (_) -> t_nil() end);
-type(hipe_bifs, primop_address, 1, Xs) ->
- strict(arg_types(hipe_bifs, primop_address, 1), Xs,
- fun (_) -> t_sup(t_integer(), t_atom('false')) end);
-type(hipe_bifs, redirect_referred_from, 1, Xs) ->
- strict(arg_types(hipe_bifs, redirect_referred_from, 1), Xs,
- fun (_) -> t_nil() end);
-type(hipe_bifs, ref, 1, Xs) ->
- strict(arg_types(hipe_bifs, ref, 1), Xs, fun (_) -> t_immarray() end);
-type(hipe_bifs, ref_get, 1, Xs) ->
- strict(arg_types(hipe_bifs, ref_get, 1), Xs, fun (_) -> t_immediate() end);
-type(hipe_bifs, ref_set, 2, Xs) ->
- strict(arg_types(hipe_bifs, ref_set, 2), Xs, fun (_) -> t_nil() end);
-type(hipe_bifs, remove_refs_from, 1, Xs) ->
- strict(arg_types(hipe_bifs, remove_refs_from, 1), Xs,
- fun (_) -> t_atom('ok') end);
-type(hipe_bifs, set_funinfo_native_address, 3, Xs) ->
- strict(arg_types(hipe_bifs, set_funinfo_native_address, 3), Xs,
- fun (_) -> t_nil() end);
-type(hipe_bifs, set_native_address, 3, Xs) ->
- strict(arg_types(hipe_bifs, set_native_address, 3), Xs,
- fun (_) -> t_nil() end);
-type(hipe_bifs, system_crc, 1, Xs) ->
- strict(arg_types(hipe_bifs, system_crc, 1), Xs, fun (_) -> t_crc32() end);
-type(hipe_bifs, term_to_word, 1, Xs) ->
- strict(arg_types(hipe_bifs, term_to_word, 1), Xs,
- fun (_) -> t_integer() end);
-type(hipe_bifs, update_code_size, 3, Xs) ->
- strict(arg_types(hipe_bifs, update_code_size, 3), Xs,
- fun (_) -> t_nil() end);
-type(hipe_bifs, write_u8, 2, Xs) ->
- strict(arg_types(hipe_bifs, write_u8, 2), Xs, fun (_) -> t_nil() end);
-type(hipe_bifs, write_u32, 2, Xs) ->
- strict(arg_types(hipe_bifs, write_u32, 2), Xs, fun (_) -> t_nil() end);
-type(hipe_bifs, write_u64, 2, Xs) ->
- strict(arg_types(hipe_bifs, write_u64, 2), Xs, fun (_) -> t_nil() end);
+type(hipe_bifs, patch_call, 3, Xs, Opaques) ->
+ strict(hipe_bifs, patch_call, 3, Xs, fun (_) -> t_nil() end, Opaques);
+type(hipe_bifs, patch_insn, 3, Xs, Opaques) ->
+ strict(hipe_bifs, patch_insn, 3, Xs, fun (_) -> t_nil() end, Opaques);
+type(hipe_bifs, primop_address, 1, Xs, Opaques) ->
+ strict(hipe_bifs, primop_address, 1, Xs,
+ fun (_) -> t_sup(t_integer(), t_atom('false')) end, Opaques);
+type(hipe_bifs, redirect_referred_from, 1, Xs, Opaques) ->
+ strict(hipe_bifs, redirect_referred_from, 1, Xs,
+ fun (_) -> t_nil() end, Opaques);
+type(hipe_bifs, ref, 1, Xs, Opaques) ->
+ strict(hipe_bifs, ref, 1, Xs, fun (_) -> t_immarray() end, Opaques);
+type(hipe_bifs, ref_get, 1, Xs, Opaques) ->
+ strict(hipe_bifs, ref_get, 1, Xs, fun (_) -> t_immediate() end, Opaques);
+type(hipe_bifs, ref_set, 2, Xs, Opaques) ->
+ strict(hipe_bifs, ref_set, 2, Xs, fun (_) -> t_nil() end, Opaques);
+type(hipe_bifs, remove_refs_from, 1, Xs, Opaques) ->
+ strict(hipe_bifs, remove_refs_from, 1, Xs,
+ fun (_) -> t_atom('ok') end, Opaques);
+type(hipe_bifs, set_funinfo_native_address, 3, Xs, Opaques) ->
+ strict(hipe_bifs, set_funinfo_native_address, 3, Xs,
+ fun (_) -> t_nil() end, Opaques);
+type(hipe_bifs, set_native_address, 3, Xs, Opaques) ->
+ strict(hipe_bifs, set_native_address, 3, Xs,
+ fun (_) -> t_nil() end, Opaques);
+type(hipe_bifs, set_native_address_in_fe, 2, Xs, Opaques) ->
+ strict(hipe_bifs, set_native_address_in_fe, 2, Xs,
+ fun (_) -> t_atom('true') end, Opaques);
+type(hipe_bifs, system_crc, 1, Xs, Opaques) ->
+ strict(hipe_bifs, system_crc, 1, Xs, fun (_) -> t_crc32() end, Opaques);
+type(hipe_bifs, term_to_word, 1, Xs, Opaques) ->
+ strict(hipe_bifs, term_to_word, 1, Xs,
+ fun (_) -> t_integer() end, Opaques);
+type(hipe_bifs, update_code_size, 3, Xs, Opaques) ->
+ strict(hipe_bifs, update_code_size, 3, Xs,
+ fun (_) -> t_nil() end, Opaques);
+type(hipe_bifs, write_u8, 2, Xs, Opaques) ->
+ strict(hipe_bifs, write_u8, 2, Xs, fun (_) -> t_nil() end, Opaques);
+type(hipe_bifs, write_u32, 2, Xs, Opaques) ->
+ strict(hipe_bifs, write_u32, 2, Xs, fun (_) -> t_nil() end, Opaques);
+type(hipe_bifs, write_u64, 2, Xs, Opaques) ->
+ strict(hipe_bifs, write_u64, 2, Xs, fun (_) -> t_nil() end, Opaques);
%%-- lists --------------------------------------------------------------------
-type(lists, all, 2, Xs) ->
- strict(arg_types(lists, all, 2), Xs,
+type(lists, all, 2, Xs, Opaques) ->
+ strict(lists, all, 2, Xs,
fun ([F, L]) ->
- case t_is_nil(L) of
+ case t_is_nil(L, Opaques) of
true -> t_atom('true');
false ->
- El = t_list_elements(L),
- case check_fun_application(F, [El]) of
+ El = t_list_elements(L, Opaques),
+ case check_fun_application(F, [El], Opaques) of
ok ->
- case t_is_cons(L) of
- true -> t_fun_range(F);
+ case t_is_cons(L, Opaques) of
+ true -> t_fun_range(F, Opaques);
false ->
%% The list can be empty.
- t_sup(t_atom('true'), t_fun_range(F))
+ t_sup(t_atom('true'), t_fun_range(F, Opaques))
end;
error ->
- case t_is_cons(L) of
+ case t_is_cons(L, Opaques) of
true -> t_none();
- false -> t_fun_range(F)
+ false -> t_fun_range(F, Opaques)
end
end
end
- end);
-type(lists, any, 2, Xs) ->
- strict(arg_types(lists, any, 2), Xs,
+ end, Opaques);
+type(lists, any, 2, Xs, Opaques) ->
+ strict(lists, any, 2, Xs,
fun ([F, L]) ->
- case t_is_nil(L) of
+ case t_is_nil(L, Opaques) of
true -> t_atom('false');
false ->
- El = t_list_elements(L),
- case check_fun_application(F, [El]) of
+ El = t_list_elements(L, Opaques),
+ case check_fun_application(F, [El], Opaques) of
ok ->
- case t_is_cons(L) of
- true -> t_fun_range(F);
+ case t_is_cons(L, Opaques) of
+ true -> t_fun_range(F, Opaques);
false ->
%% The list can be empty
- t_sup(t_atom('false'), t_fun_range(F))
+ t_sup(t_atom('false'), t_fun_range(F, Opaques))
end;
error ->
- case t_is_cons(L) of
+ case t_is_cons(L, Opaques) of
true -> t_none();
- false -> t_fun_range(F)
+ false -> t_fun_range(F, Opaques)
end
end
end
- end);
-type(lists, append, 2, Xs) -> type(erlang, '++', 2, Xs); % alias
-type(lists, delete, 2, Xs) ->
- strict(arg_types(lists, delete, 2), Xs,
+ end, Opaques);
+type(lists, append, 2, Xs, _Opaques) -> type(erlang, '++', 2, Xs); % alias
+type(lists, delete, 2, Xs, Opaques) ->
+ strict(lists, delete, 2, Xs,
fun ([_, List]) ->
- case t_is_cons(List) of
+ case t_is_cons(List, Opaques) of
true -> t_cons_tl(List);
false -> List
end
- end);
-type(lists, dropwhile, 2, Xs) ->
- strict(arg_types(lists, dropwhile, 2), Xs,
+ end, Opaques);
+type(lists, dropwhile, 2, Xs, Opaques) ->
+ strict(lists, dropwhile, 2, Xs,
fun ([F, X]) ->
- case t_is_nil(X) of
+ case t_is_nil(X, Opaques) of
true -> t_nil();
false ->
- X1 = t_list_elements(X),
- case check_fun_application(F, [X1]) of
+ X1 = t_list_elements(X, Opaques),
+ case check_fun_application(F, [X1], Opaques) of
ok ->
- case t_atom_vals(t_fun_range(F)) of
+ case t_atom_vals(t_fun_range(F, Opaques), Opaques) of
['true'] ->
- case t_is_none(t_inf(t_list(), X)) of
+ case t_is_none(t_inf(t_list(), X, Opaques)) of
true -> t_none();
false -> t_nil()
end;
['false'] ->
- case t_is_none(t_inf(t_list(), X)) of
+ case t_is_none(t_inf(t_list(), X, Opaques)) of
true -> t_none();
false -> X
end;
_ ->
- t_inf(t_cons_tl(t_inf(X, t_cons())),
- t_maybe_improper_list())
+ t_inf(t_cons_tl(t_inf(X, t_cons(), Opaques)),
+ t_maybe_improper_list(), Opaques)
end;
error ->
- case t_is_cons(X) of
+ case t_is_cons(X, Opaques) of
true -> t_none();
false -> t_nil()
end
end
end
- end);
-type(lists, filter, 2, Xs) ->
- strict(arg_types(lists, filter, 2), Xs,
+ end, Opaques);
+type(lists, filter, 2, Xs, Opaques) ->
+ strict(lists, filter, 2, Xs,
fun ([F, L]) ->
- case t_is_nil(L) of
+ case t_is_nil(L, Opaques) of
true -> t_nil();
false ->
- T = t_list_elements(L),
- case check_fun_application(F, [T]) of
+ T = t_list_elements(L, Opaques),
+ case check_fun_application(F, [T], Opaques) of
ok ->
- case t_atom_vals(t_fun_range(F)) =:= ['false'] of
+ RangeVals = t_atom_vals(t_fun_range(F, Opaques), Opaques),
+ case RangeVals =:= ['false'] of
true -> t_nil();
false ->
- case t_atom_vals(t_fun_range(F)) =:= ['true'] of
+ case RangeVals =:= ['true'] of
true -> L;
false -> t_list(T)
end
end;
error ->
- case t_is_cons(L) of
+ case t_is_cons(L, Opaques) of
true -> t_none();
false -> t_nil()
end
end
end
- end);
-type(lists, flatten, 1, Xs) ->
- strict(arg_types(lists, flatten, 1), Xs,
+ end, Opaques);
+type(lists, flatten, 1, Xs, Opaques) ->
+ strict(lists, flatten, 1, Xs,
fun ([L]) ->
- case t_is_nil(L) of
+ case t_is_nil(L, Opaques) of
true -> L; % (nil has undefined elements)
false ->
%% Avoiding infinite recursion is tricky
- X1 = t_list_elements(L),
+ X1 = t_list_elements(L, Opaques),
case t_is_any(X1) of
true ->
t_list();
false ->
- X2 = type(lists, flatten, 1, [t_inf(X1, t_list())]),
+ X2 = type(lists, flatten, 1, [t_inf(X1, t_list(), Opaques)]),
t_sup(t_list(t_subtract(X1, t_list())), X2)
end
end
- end);
-type(lists, flatmap, 2, Xs) ->
- strict(arg_types(lists, flatmap, 2), Xs,
+ end, Opaques);
+type(lists, flatmap, 2, Xs, Opaques) ->
+ strict(lists, flatmap, 2, Xs,
fun ([F, List]) ->
- case t_is_nil(List) of
+ case t_is_nil(List, Opaques) of
true -> t_nil();
false ->
- case check_fun_application(F, [t_list_elements(List)]) of
+ case
+ check_fun_application(F, [t_list_elements(List, Opaques)],
+ Opaques)
+ of
ok ->
- R = t_fun_range(F),
+ R = t_fun_range(F, Opaques),
case t_is_nil(R) of
true -> t_nil();
false ->
- Elems = t_list_elements(R),
- case t_is_cons(List) of
+ Elems = t_list_elements(R, Opaques),
+ case t_is_cons(List, Opaques) of
true ->
case t_is_subtype(t_nil(), R) of
true -> t_list(Elems);
@@ -1247,58 +1290,65 @@ type(lists, flatmap, 2, Xs) ->
end
end;
error ->
- case t_is_cons(List) of
+ case t_is_cons(List, Opaques) of
true -> t_none();
false -> t_nil()
end
end
end
- end);
-type(lists, foreach, 2, Xs) ->
- strict(arg_types(lists, foreach, 2), Xs,
+ end, Opaques);
+type(lists, foreach, 2, Xs, Opaques) ->
+ strict(lists, foreach, 2, Xs,
fun ([F, List]) ->
- case t_is_cons(List) of
+ case t_is_cons(List, Opaques) of
true ->
- case check_fun_application(F, [t_list_elements(List)]) of
+ case
+ check_fun_application(F, [t_list_elements(List, Opaques)],
+ Opaques)
+ of
ok -> t_atom('ok');
error -> t_none()
end;
false ->
t_atom('ok')
end
- end);
-type(lists, foldl, 3, Xs) ->
- strict(arg_types(lists, foldl, 3), Xs,
+ end, Opaques);
+type(lists, foldl, 3, Xs, Opaques) ->
+ strict(lists, foldl, 3, Xs,
fun ([F, Acc, List]) ->
- case t_is_nil(List) of
+ case t_is_nil(List, Opaques) of
true -> Acc;
false ->
- case check_fun_application(F, [t_list_elements(List), Acc]) of
+ case
+ check_fun_application(F,
+ [t_list_elements(List, Opaques),Acc],
+ Opaques)
+ of
ok ->
- case t_is_cons(List) of
- true -> t_fun_range(F);
- false -> t_sup(t_fun_range(F), Acc)
+ case t_is_cons(List, Opaques) of
+ true -> t_fun_range(F, Opaques);
+ false -> t_sup(t_fun_range(F, Opaques), Acc)
end;
error ->
- case t_is_cons(List) of
+ case t_is_cons(List, Opaques) of
true -> t_none();
false -> Acc
end
end
end
- end);
-type(lists, foldr, 3, Xs) -> type(lists, foldl, 3, Xs); % same
-type(lists, keydelete, 3, Xs) ->
- strict(arg_types(lists, keydelete, 3), Xs,
+ end, Opaques);
+type(lists, foldr, 3, Xs, _Opaques) -> type(lists, foldl, 3, Xs); % same
+type(lists, keydelete, 3, Xs, Opaques) ->
+ strict(lists, keydelete, 3, Xs,
fun ([_, _, L]) ->
Term = t_list_termination(L),
t_sup(Term, erl_types:lift_list_to_pos_empty(L))
- end);
-type(lists, keyfind, 3, Xs) ->
- strict(arg_types(lists, keyfind, 3), Xs,
+ end, Opaques);
+type(lists, keyfind, 3, Xs, Opaques) ->
+ strict(lists, keyfind, 3, Xs,
fun ([X, Y, Z]) ->
- ListEs = t_list_elements(Z),
- Tuple = t_inf(t_tuple(), ListEs),
+ ListEs = t_list_elements(Z, Opaques),
+ Tuple = t_inf(t_tuple(), ListEs, Opaques),
case t_is_none(Tuple) of
true -> t_atom('false');
false ->
@@ -1308,58 +1358,61 @@ type(lists, keyfind, 3, Xs) ->
case t_is_any(X) of
true -> Ret;
false ->
- case t_tuple_subtypes(Tuple) of
+ case t_tuple_subtypes(Tuple, Opaques) of
unknown -> Ret;
List ->
- case key_comparisons_fail(X, Y, List) of
+ case key_comparisons_fail(X, Y, List, Opaques) of
true -> t_atom('false');
false -> Ret
end
end
end
end
- end);
-type(lists, keymap, 3, Xs) ->
- strict(arg_types(lists, keymap, 3), Xs,
+ end, Opaques);
+type(lists, keymap, 3, Xs, Opaques) ->
+ strict(lists, keymap, 3, Xs,
fun ([F, _I, L]) ->
- case t_is_nil(L) of
+ case t_is_nil(L, Opaques) of
true -> L;
- false -> t_list(t_sup(t_fun_range(F), t_list_elements(L)))
+ false -> t_list(t_sup(t_fun_range(F, Opaques),
+ t_list_elements(L, Opaques)))
end
- end);
-type(lists, keymember, 3, Xs) ->
- strict(arg_types(lists, keymember, 3), Xs,
+ end, Opaques);
+type(lists, keymember, 3, Xs, Opaques) ->
+ strict(lists, keymember, 3, Xs,
fun ([X, Y, Z]) ->
- ListEs = t_list_elements(Z),
- Tuple = t_inf(t_tuple(), ListEs),
+ ListEs = t_list_elements(Z, Opaques),
+ Tuple = t_inf(t_tuple(), ListEs, Opaques),
case t_is_none(Tuple) of
true -> t_atom('false');
false ->
case t_is_any(X) of
true -> t_boolean();
false ->
- case t_tuple_subtypes(Tuple) of
+ case t_tuple_subtypes(Tuple, Opaques) of
unknown -> t_boolean();
List ->
- case key_comparisons_fail(X, Y, List) of
+ case key_comparisons_fail(X, Y, List, Opaques) of
true -> t_atom('false');
false -> t_boolean()
end
end
end
end
- end);
-type(lists, keymerge, 3, Xs) ->
- strict(arg_types(lists, keymerge, 3), Xs,
- fun ([_I, L1, L2]) -> type(lists, merge, 2, [L1, L2]) end);
-type(lists, keyreplace, 4, Xs) ->
- strict(arg_types(lists, keyreplace, 4), Xs,
- fun ([_K, _I, L, T]) -> t_list(t_sup(t_list_elements(L), T)) end);
-type(lists, keysearch, 3, Xs) ->
- strict(arg_types(lists, keysearch, 3), Xs,
+ end, Opaques);
+type(lists, keymerge, 3, Xs, Opaques) ->
+ strict(lists, keymerge, 3, Xs,
+ fun ([_I, L1, L2]) -> type(lists, merge, 2, [L1, L2]) end, Opaques);
+type(lists, keyreplace, 4, Xs, Opaques) ->
+ strict(lists, keyreplace, 4, Xs,
+ fun ([_K, _I, L, T]) ->
+ t_list(t_sup(t_list_elements(L, Opaques), T))
+ end, Opaques);
+type(lists, keysearch, 3, Xs, Opaques) ->
+ strict(lists, keysearch, 3, Xs,
fun ([X, Y, Z]) ->
- ListEs = t_list_elements(Z),
- Tuple = t_inf(t_tuple(), ListEs),
+ ListEs = t_list_elements(Z, Opaques),
+ Tuple = t_inf(t_tuple(), ListEs, Opaques),
case t_is_none(Tuple) of
true -> t_atom('false');
false ->
@@ -1368,91 +1421,93 @@ type(lists, keysearch, 3, Xs) ->
case t_is_any(X) of
true -> Ret;
false ->
- case t_tuple_subtypes(Tuple) of
+ case t_tuple_subtypes(Tuple, Opaques) of
unknown -> Ret;
List ->
- case key_comparisons_fail(X, Y, List) of
+ case key_comparisons_fail(X, Y, List, Opaques) of
true -> t_atom('false');
false -> Ret
end
end
end
end
- end);
-type(lists, keysort, 2, Xs) ->
- strict(arg_types(lists, keysort, 2), Xs, fun ([_, L]) -> L end);
-type(lists, last, 1, Xs) ->
- strict(arg_types(lists, last, 1), Xs, fun ([L]) -> t_list_elements(L) end);
-type(lists, map, 2, Xs) ->
- strict(arg_types(lists, map, 2), Xs,
+ end, Opaques);
+type(lists, keysort, 2, Xs, Opaques) ->
+ strict(lists, keysort, 2, Xs, fun ([_, L]) -> L end, Opaques);
+type(lists, last, 1, Xs, Opaques) ->
+ strict(lists, last, 1, Xs,
+ fun ([L]) -> t_list_elements(L, Opaques) end, Opaques);
+type(lists, map, 2, Xs, Opaques) ->
+ strict(lists, map, 2, Xs,
fun ([F, L]) ->
- case t_is_nil(L) of
+ case t_is_nil(L, Opaques) of
true -> L;
false ->
- El = t_list_elements(L),
- case t_is_cons(L) of
+ El = t_list_elements(L, Opaques),
+ case t_is_cons(L, Opaques) of
true ->
- case check_fun_application(F, [El]) of
- ok -> t_nonempty_list(t_fun_range(F));
+ case check_fun_application(F, [El], Opaques) of
+ ok -> t_nonempty_list(t_fun_range(F, Opaques));
error -> t_none()
end;
false ->
- case check_fun_application(F, [El]) of
- ok -> t_list(t_fun_range(F));
+ case check_fun_application(F, [El], Opaques) of
+ ok -> t_list(t_fun_range(F, Opaques));
error -> t_nil()
end
end
end
- end);
-type(lists, mapfoldl, 3, Xs) ->
- strict(arg_types(lists, mapfoldl, 3), Xs,
+ end, Opaques);
+type(lists, mapfoldl, 3, Xs, Opaques) ->
+ strict(lists, mapfoldl, 3, Xs,
fun ([F, Acc, List]) ->
- case t_is_nil(List) of
+ case t_is_nil(List, Opaques) of
true -> t_tuple([List, Acc]);
false ->
- El = t_list_elements(List),
- R = t_fun_range(F),
- case t_is_cons(List) of
+ El = t_list_elements(List, Opaques),
+ R = t_fun_range(F, Opaques),
+ case t_is_cons(List, Opaques) of
true ->
- case check_fun_application(F, [El, Acc]) of
+ case check_fun_application(F, [El, Acc], Opaques) of
ok ->
Fun = fun (RangeTuple) ->
- [T1, T2] = t_tuple_args(RangeTuple),
+ [T1, T2] = t_tuple_args(RangeTuple, Opaques),
t_tuple([t_nonempty_list(T1), T2])
end,
- t_sup([Fun(ST) || ST <- t_tuple_subtypes(R)]);
+ t_sup([Fun(ST) || ST <- t_tuple_subtypes(R, Opaques)]);
error ->
t_none()
end;
false ->
- case check_fun_application(F, [El, Acc]) of
+ case check_fun_application(F, [El, Acc], Opaques) of
ok ->
Fun = fun (RangeTuple) ->
- [T1, T2] = t_tuple_args(RangeTuple),
+ [T1, T2] = t_tuple_args(RangeTuple, Opaques),
t_tuple([t_list(T1), t_sup(Acc, T2)])
end,
- t_sup([Fun(ST) || ST <- t_tuple_subtypes(R)]);
+ t_sup([Fun(ST) || ST <- t_tuple_subtypes(R, Opaques)]);
error ->
t_tuple([t_nil(), Acc])
end
end
end
- end);
-type(lists, mapfoldr, 3, Xs) -> type(lists, mapfoldl, 3, Xs); % same
-type(lists, max, 1, Xs) ->
- strict(arg_types(lists, max, 1), Xs, fun ([L]) -> t_list_elements(L) end);
-type(lists, member, 2, Xs) ->
- strict(arg_types(lists, member, 2), Xs,
+ end, Opaques);
+type(lists, mapfoldr, 3, Xs, _Opaques) -> type(lists, mapfoldl, 3, Xs); % same
+type(lists, max, 1, Xs, Opaques) ->
+ strict(lists, max, 1, Xs,
+ fun ([L]) -> t_list_elements(L, Opaques) end, Opaques);
+type(lists, member, 2, Xs, Opaques) ->
+ strict(lists, member, 2, Xs,
fun ([X, Y]) ->
- Y1 = t_list_elements(Y),
- case t_is_none(t_inf(Y1, X)) of
+ Y1 = t_list_elements(Y, Opaques),
+ case t_is_none(t_inf(Y1, X, Opaques)) of
true -> t_atom('false');
false -> t_boolean()
end
- end);
-%% type(lists, merge, 1, Xs) ->
-type(lists, merge, 2, Xs) ->
- strict(arg_types(lists, merge, 2), Xs,
+ end, Opaques);
+%% type(lists, merge, 1, Xs, Opaques) ->
+type(lists, merge, 2, Xs, Opaques) ->
+ strict(lists, merge, 2, Xs,
fun ([L1, L2]) ->
case t_is_none(L1) of
true -> L2;
@@ -1462,30 +1517,31 @@ type(lists, merge, 2, Xs) ->
false -> t_sup(L1, L2)
end
end
- end);
-type(lists, min, 1, Xs) ->
- strict(arg_types(lists, min, 1), Xs, fun ([L]) -> t_list_elements(L) end);
-type(lists, nth, 2, Xs) ->
- strict(arg_types(lists, nth, 2), Xs,
- fun ([_, Y]) -> t_list_elements(Y) end);
-type(lists, nthtail, 2, Xs) ->
- strict(arg_types(lists, nthtail, 2), Xs,
- fun ([_, Y]) -> t_sup(Y, t_list()) end);
-type(lists, partition, 2, Xs) ->
- strict(arg_types(lists, partition, 2), Xs,
+ end, Opaques);
+type(lists, min, 1, Xs, Opaques) ->
+ strict(lists, min, 1, Xs,
+ fun ([L]) -> t_list_elements(L, Opaques) end, Opaques);
+type(lists, nth, 2, Xs, Opaques) ->
+ strict(lists, nth, 2, Xs,
+ fun ([_, Y]) -> t_list_elements(Y, Opaques) end, Opaques);
+type(lists, nthtail, 2, Xs, Opaques) ->
+ strict(lists, nthtail, 2, Xs,
+ fun ([_, Y]) -> t_sup(Y, t_list()) end, Opaques);
+type(lists, partition, 2, Xs, Opaques) ->
+ strict(lists, partition, 2, Xs,
fun ([F, L]) ->
- case t_is_nil(L) of
+ case t_is_nil(L, Opaques) of
true -> t_tuple([L,L]);
false ->
- El = t_list_elements(L),
- case check_fun_application(F, [El]) of
+ El = t_list_elements(L, Opaques),
+ case check_fun_application(F, [El], Opaques) of
error ->
- case t_is_cons(L) of
+ case t_is_cons(L, Opaques) of
true -> t_none();
false -> t_tuple([t_nil(), t_nil()])
end;
ok ->
- case t_atom_vals(t_fun_range(F)) of
+ case t_atom_vals(t_fun_range(F, Opaques), Opaques) of
['true'] -> t_tuple([L, t_nil()]);
['false'] -> t_tuple([t_nil(), L]);
[_, _] ->
@@ -1494,123 +1550,131 @@ type(lists, partition, 2, Xs) ->
end
end
end
- end);
-type(lists, reverse, 1, Xs) ->
- strict(arg_types(lists, reverse, 1), Xs, fun ([X]) -> X end);
-type(lists, reverse, 2, Xs) ->
+ end, Opaques);
+type(lists, reverse, 1, Xs, Opaques) ->
+ strict(lists, reverse, 1, Xs, fun ([X]) -> X end, Opaques);
+type(lists, reverse, 2, Xs, _Opaques) ->
type(erlang, '++', 2, Xs); % reverse-onto is just like append
-type(lists, sort, 1, Xs) ->
- strict(arg_types(lists, sort, 1), Xs, fun ([X]) -> X end);
-type(lists, sort, 2, Xs) ->
- strict(arg_types(lists, sort, 2), Xs,
+type(lists, sort, 1, Xs, Opaques) ->
+ strict(lists, sort, 1, Xs, fun ([X]) -> X end, Opaques);
+type(lists, sort, 2, Xs, Opaques) ->
+ strict(lists, sort, 2, Xs,
fun ([F, L]) ->
- R = t_fun_range(F),
- case t_is_boolean(R) of
+ R = t_fun_range(F, Opaques),
+ case t_is_boolean(R, Opaques) of
true -> L;
false ->
- case t_is_nil(L) of
+ case t_is_nil(L, Opaques) of
true -> t_nil();
false -> t_none()
end
end
- end);
-type(lists, split, 2, Xs) ->
- strict(arg_types(lists, split, 2), Xs,
+ end, Opaques);
+type(lists, split, 2, Xs, Opaques) ->
+ strict(lists, split, 2, Xs,
fun ([_, L]) ->
- case t_is_nil(L) of
+ case t_is_nil(L, Opaques) of
true -> t_tuple([L, L]);
false ->
- T = t_list_elements(L),
+ T = t_list_elements(L, Opaques),
t_tuple([t_list(T), t_list(T)])
end
- end);
-type(lists, splitwith, 2, Xs) ->
+ end, Opaques);
+type(lists, splitwith, 2, Xs, _Opaques) ->
T1 = type(lists, takewhile, 2, Xs),
T2 = type(lists, dropwhile, 2, Xs),
case t_is_none(T1) orelse t_is_none(T2) of
true -> t_none();
false -> t_tuple([T1, T2])
end;
-type(lists, subtract, 2, Xs) -> type(erlang, '--', 2, Xs); % alias
-type(lists, takewhile, 2, Xs) ->
- strict(arg_types(lists, takewhile, 2), Xs,
+type(lists, subtract, 2, Xs, _Opaques) -> type(erlang, '--', 2, Xs); % alias
+type(lists, takewhile, 2, Xs, Opaques) ->
+ strict(lists, takewhile, 2, Xs,
fun([F, L]) ->
- case t_is_none(t_inf(t_list(), L)) of
+ case t_is_none(t_inf(t_list(), L, Opaques)) of
false -> type(lists, filter, 2, Xs);
true ->
%% This works for non-proper lists as well.
- El = t_list_elements(L),
+ El = t_list_elements(L, Opaques),
type(lists, filter, 2, [F, t_list(El)])
end
- end);
-type(lists, usort, 1, Xs) -> type(lists, sort, 1, Xs); % same
-type(lists, usort, 2, Xs) -> type(lists, sort, 2, Xs); % same
-type(lists, unzip, 1, Xs) ->
- strict(arg_types(lists, unzip, 1), Xs,
+ end, Opaques);
+type(lists, usort, 1, Xs, _Opaques) -> type(lists, sort, 1, Xs); % same
+type(lists, usort, 2, Xs, _Opaques) -> type(lists, sort, 2, Xs); % same
+type(lists, unzip, 1, Xs, Opaques) ->
+ strict(lists, unzip, 1, Xs,
fun ([Ps]) ->
- case t_is_nil(Ps) of
+ case t_is_nil(Ps, Opaques) of
true ->
t_tuple([t_nil(), t_nil()]);
false -> % Ps is a proper list of pairs
- TupleTypes = t_tuple_subtypes(t_list_elements(Ps)),
+ TupleTypes = t_tuple_subtypes(t_list_elements(Ps, Opaques),
+ Opaques),
lists:foldl(fun(Tuple, Acc) ->
- [A, B] = t_tuple_args(Tuple),
+ [A, B] = t_tuple_args(Tuple, Opaques),
t_sup(t_tuple([t_list(A), t_list(B)]), Acc)
end, t_none(), TupleTypes)
end
- end);
-type(lists, unzip3, 1, Xs) ->
- strict(arg_types(lists, unzip3, 1), Xs,
+ end, Opaques);
+type(lists, unzip3, 1, Xs, Opaques) ->
+ strict(lists, unzip3, 1, Xs,
fun ([Ts]) ->
- case t_is_nil(Ts) of
+ case t_is_nil(Ts, Opaques) of
true ->
t_tuple([t_nil(), t_nil(), t_nil()]);
false -> % Ps is a proper list of triples
- TupleTypes = t_tuple_subtypes(t_list_elements(Ts)),
+ TupleTypes = t_tuple_subtypes(t_list_elements(Ts, Opaques),
+ Opaques),
lists:foldl(fun(T, Acc) ->
- [A, B, C] = t_tuple_args(T),
+ [A, B, C] = t_tuple_args(T, Opaques),
t_sup(t_tuple([t_list(A),
t_list(B),
t_list(C)]),
Acc)
end, t_none(), TupleTypes)
end
- end);
-type(lists, zip, 2, Xs) ->
- strict(arg_types(lists, zip, 2), Xs,
+ end, Opaques);
+type(lists, zip, 2, Xs, Opaques) ->
+ strict(lists, zip, 2, Xs,
fun ([As, Bs]) ->
- case (t_is_nil(As) orelse t_is_nil(Bs)) of
+ case (t_is_nil(As, Opaques) orelse t_is_nil(Bs, Opaques)) of
true -> t_nil();
false ->
- A = t_list_elements(As),
- B = t_list_elements(Bs),
+ A = t_list_elements(As, Opaques),
+ B = t_list_elements(Bs, Opaques),
t_list(t_tuple([A, B]))
end
- end);
-type(lists, zip3, 3, Xs) ->
- strict(arg_types(lists, zip3, 3), Xs,
+ end, Opaques);
+type(lists, zip3, 3, Xs, Opaques) ->
+ strict(lists, zip3, 3, Xs,
fun ([As, Bs, Cs]) ->
- case (t_is_nil(As) orelse t_is_nil(Bs) orelse t_is_nil(Cs)) of
+ case
+ (t_is_nil(As, Opaques)
+ orelse t_is_nil(Bs, Opaques)
+ orelse t_is_nil(Cs, Opaques))
+ of
true -> t_nil();
false ->
- A = t_list_elements(As),
- B = t_list_elements(Bs),
- C = t_list_elements(Cs),
+ A = t_list_elements(As, Opaques),
+ B = t_list_elements(Bs, Opaques),
+ C = t_list_elements(Cs, Opaques),
t_list(t_tuple([A, B, C]))
end
- end);
-type(lists, zipwith, 3, Xs) ->
- strict(arg_types(lists, zipwith, 3), Xs,
- fun ([F, _As, _Bs]) -> t_sup(t_list(t_fun_range(F)), t_nil()) end);
-type(lists, zipwith3, 4, Xs) ->
- strict(arg_types(lists, zipwith3, 4), Xs,
- fun ([F,_As,_Bs,_Cs]) -> t_sup(t_list(t_fun_range(F)), t_nil()) end);
+ end, Opaques);
+type(lists, zipwith, 3, Xs, Opaques) ->
+ strict(lists, zipwith, 3, Xs,
+ fun ([F, _As, _Bs]) -> t_sup(t_list(t_fun_range(F, Opaques)),
+ t_nil()) end, Opaques);
+type(lists, zipwith3, 4, Xs, Opaques) ->
+ strict(lists, zipwith3, 4, Xs,
+ fun ([F,_As,_Bs,_Cs]) -> t_sup(t_list(t_fun_range(F, Opaques)),
+ t_nil()) end, Opaques);
%%-- string -------------------------------------------------------------------
-type(string, chars, 2, Xs) -> % NOTE: added to avoid loss of information
- strict(arg_types(string, chars, 2), Xs, fun (_) -> t_string() end);
-type(string, chars, 3, Xs) -> % NOTE: added to avoid loss of information
- strict(arg_types(string, chars, 3), Xs,
+type(string, chars, 2, Xs, Opaques) -> % NOTE: added to avoid loss of info
+ strict(string, chars, 2, Xs, fun (_) -> t_string() end, Opaques);
+type(string, chars, 3, Xs, Opaques) -> % NOTE: added to avoid loss of info
+ strict(string, chars, 3, Xs,
fun ([Char, N, Tail]) ->
case t_is_nil(Tail) of
true ->
@@ -1623,10 +1687,10 @@ type(string, chars, 3, Xs) -> % NOTE: added to avoid loss of information
t_sup(t_sup(t_string(), Tail), t_cons(Char, Tail))
end
end
- end);
+ end, Opaques);
%%-----------------------------------------------------------------------------
-type(M, F, A, Xs) when is_atom(M), is_atom(F),
+type(M, F, A, Xs, _O) when is_atom(M), is_atom(F),
is_integer(A), 0 =< A, A =< 255 ->
strict(Xs, t_any()). % safe approximation for all functions.
@@ -1635,13 +1699,20 @@ type(M, F, A, Xs) when is_atom(M), is_atom(F),
%% Auxiliary functions
%%-----------------------------------------------------------------------------
-strict(Xs, Ts, F) ->
- %% io:format("inf lists arg~n1:~p~n2:~p ~n", [Xs, Ts]),
- Xs1 = inf_lists(Xs, Ts),
+strict(M, F, A, Xs, Fun, Opaques) ->
+ Ts = arg_types(M, F, A),
+ %% io:format("inf lists arg~nXs: ~p~nTs: ~p ~n", [Xs, Ts]),
+ Xs1 = inf_lists(Xs, Ts, Opaques),
%% io:format("inf lists return ~p ~n", [Xs1]),
case any_is_none_or_unit(Xs1) of
true -> t_none();
- false -> F(Xs1)
+ false -> Fun(Xs1)
+ end.
+
+strict2(Xs, X) ->
+ case any_is_none_or_unit(Xs) of
+ true -> t_none();
+ false -> X
end.
strict(Xs, X) ->
@@ -1650,9 +1721,9 @@ strict(Xs, X) ->
false -> X
end.
-inf_lists([X | Xs], [T | Ts]) ->
- [t_inf(X, T) | inf_lists(Xs, Ts)];
-inf_lists([], []) ->
+inf_lists([X | Xs], [T | Ts], Opaques) ->
+ [t_inf(X, T, Opaques) | inf_lists(Xs, Ts, Opaques)];
+inf_lists([], [], _Opaques) ->
[].
any_list(N) -> any_list(N, t_any()).
@@ -1670,20 +1741,43 @@ list_replace(1, E, [_X | Xs]) ->
any_is_none_or_unit(Ts) ->
lists:any(fun erl_types:t_is_none_or_unit/1, Ts).
-check_guard([X], Test, Type) ->
- check_guard_single(X, Test, Type).
+check_guard([X], Test, Type, Opaques) ->
+ check_guard_single(X, Test, Type, Opaques).
-check_guard_single(X, Test, Type) ->
+check_guard_single(X, Test, Type, Opaques) ->
case Test(X) of
true -> t_atom('true');
false ->
- case erl_types:t_is_opaque(X) of
- true -> t_none();
- false ->
- case t_is_none(t_inf(Type, X)) of
- true -> t_atom('false');
- false -> t_boolean()
- end
+ case t_is_none(t_inf(Type, X, Opaques)) of
+ true ->
+ case t_has_opaque_subtype(X, Opaques) of
+ true -> t_none();
+ false -> t_atom('false')
+ end;
+ false -> t_boolean()
+ end
+ end.
+
+check_record_tag(Tag, Y, Opaques) ->
+ case t_is_atom(Tag, Opaques) of
+ false ->
+ TagAtom = t_inf(Tag, t_atom(), Opaques),
+ case t_is_none(TagAtom) of
+ true ->
+ case t_has_opaque_subtype(Tag, Opaques) of
+ true -> t_none();
+ false -> t_atom('false')
+ end;
+ false -> t_boolean()
+ end;
+ true ->
+ case t_atom_vals(Tag, Opaques) of
+ [RealTag] ->
+ case t_atom_vals(Y, Opaques) of
+ [RealTag] -> t_atom('true');
+ _ -> t_boolean()
+ end;
+ _ -> t_boolean()
end
end.
@@ -1828,12 +1922,12 @@ negwidth(X, N) ->
false -> negwidth(X, N+1)
end.
-arith('bnot', X1) ->
- case t_is_integer(X1) of
+arith('bnot', X1, Opaques) ->
+ case t_is_integer(X1, Opaques) of
false -> error;
true ->
- Min1 = number_min(X1),
- Max1 = number_max(X1),
+ Min1 = number_min(X1, Opaques),
+ Max1 = number_max(X1, Opaques),
{ok, t_from_range(infinity_add(infinity_inv(Max1), -1),
infinity_add(infinity_inv(Min1), -1))}
end.
@@ -1907,13 +2001,13 @@ arith_bor_range_set({Min, Max}, [Int|IntList]) ->
IntList),
{infinity_bor(Min, SafeAnd), infinity_bor(Max, SafeAnd)}.
-arith_band(X1, X2) ->
- L1 = t_number_vals(X1),
- L2 = t_number_vals(X2),
- Min1 = number_min(X1),
- Max1 = number_max(X1),
- Min2 = number_min(X2),
- Max2 = number_max(X2),
+arith_band(X1, X2, Opaques) ->
+ L1 = t_number_vals(X1, Opaques),
+ L2 = t_number_vals(X2, Opaques),
+ Min1 = number_min(X1, Opaques),
+ Max1 = number_max(X1, Opaques),
+ Min2 = number_min(X2, Opaques),
+ Max2 = number_max(X2, Opaques),
case {L1 =:= unknown, L2 =:= unknown} of
{true, false} ->
arith_band_range_set(arith_band_ranges(Min1, Max1, Min2, Max2), L2);
@@ -1923,13 +2017,13 @@ arith_band(X1, X2) ->
arith_band_ranges(Min1, Max1, Min2, Max2)
end.
-arith_bor(X1, X2) ->
- L1 = t_number_vals(X1),
- L2 = t_number_vals(X2),
- Min1 = number_min(X1),
- Max1 = number_max(X1),
- Min2 = number_min(X2),
- Max2 = number_max(X2),
+arith_bor(X1, X2, Opaques) ->
+ L1 = t_number_vals(X1, Opaques),
+ L2 = t_number_vals(X2, Opaques),
+ Min1 = number_min(X1, Opaques),
+ Max1 = number_max(X1, Opaques),
+ Min2 = number_min(X2, Opaques),
+ Max2 = number_max(X2, Opaques),
case {L1 =:= unknown, L2 =:= unknown} of
{true, false} ->
arith_bor_range_set(arith_bor_ranges(Min1, Max1, Min2, Max2), L2);
@@ -1967,19 +2061,19 @@ arith_bor_ranges(Min1, Max1, Min2, Max2) ->
end,
{Min, Max}.
-arith(Op, X1, X2) ->
+arith(Op, X1, X2, Opaques) ->
%% io:format("arith ~p ~p ~p~n", [Op, X1, X2]),
- case t_is_integer(X1) andalso t_is_integer(X2) of
+ case t_is_integer(X1, Opaques) andalso t_is_integer(X2, Opaques) of
false -> error;
true ->
- L1 = t_number_vals(X1),
- L2 = t_number_vals(X2),
+ L1 = t_number_vals(X1, Opaques),
+ L2 = t_number_vals(X2, Opaques),
case (L1 =:= unknown) orelse (L2 =:= unknown) of
true ->
- Min1 = number_min(X1),
- Max1 = number_max(X1),
- Min2 = number_min(X2),
- Max2 = number_max(X2),
+ Min1 = number_min(X1, Opaques),
+ Max1 = number_max(X1, Opaques),
+ Min2 = number_min(X2, Opaques),
+ Max2 = number_max(X2, Opaques),
{NewMin, NewMax} =
case Op of
'+' -> {infinity_add(Min1, Min2), infinity_add(Max1, Max2)};
@@ -1992,8 +2086,8 @@ arith(Op, X1, X2) ->
'bsr' -> NewMin2 = infinity_inv(Max2),
NewMax2 = infinity_inv(Min2),
arith_bsl(Min1, Max1, NewMin2, NewMax2);
- 'band' -> arith_band(X1, X2);
- 'bor' -> arith_bor(X1, X2);
+ 'band' -> arith_band(X1, X2, Opaques);
+ 'bor' -> arith_bor(X1, X2, Opaques);
'bxor' -> arith_bor_ranges(Min1, Max1, Min2, Max2) %% overaprox.
end,
%% io:format("done arith ~p = ~p~n", [Op, {NewMin, NewMax}]),
@@ -2025,58 +2119,62 @@ arith(Op, X1, X2) ->
%% Comparison of terms
%%=============================================================================
-compare(Op, Lhs, Rhs) ->
- case t_is_none(t_inf(Lhs, Rhs)) of
+compare(Op, Lhs, Rhs, Opaques) ->
+ case t_is_none(t_inf(Lhs, Rhs, Opaques)) of
false -> t_boolean();
true ->
- case Op of
- '<' -> always_smaller(Lhs, Rhs);
- '>' -> always_smaller(Rhs, Lhs);
- '=<' -> always_smaller(Lhs, Rhs);
- '>=' -> always_smaller(Rhs, Lhs)
+ case opaque_args(erlang, Op, 2, [Lhs, Rhs], Opaques) =:= [] of
+ true ->
+ case Op of
+ '<' -> always_smaller(Lhs, Rhs, Opaques);
+ '>' -> always_smaller(Rhs, Lhs, Opaques);
+ '=<' -> always_smaller(Lhs, Rhs, Opaques);
+ '>=' -> always_smaller(Rhs, Lhs, Opaques)
+ end;
+ false -> t_none()
end
end.
-always_smaller(Type1, Type2) ->
- {Min1, Max1} = type_ranks(Type1),
- {Min2, Max2} = type_ranks(Type2),
+always_smaller(Type1, Type2, Opaques) ->
+ {Min1, Max1} = type_ranks(Type1, Opaques),
+ {Min2, Max2} = type_ranks(Type2, Opaques),
if Max1 < Min2 -> t_atom('true');
Min1 > Max2 -> t_atom('false');
true -> t_boolean()
end.
-type_ranks(Type) ->
- type_ranks(Type, 1, 0, 0, type_order()).
+type_ranks(Type, Opaques) ->
+ type_ranks(Type, 1, 0, 0, type_order(), Opaques).
-type_ranks(_Type, _I, Min, Max, []) -> {Min, Max};
-type_ranks(Type, I, Min, Max, [TypeClass|Rest]) ->
+type_ranks(_Type, _I, Min, Max, [], _Opaques) -> {Min, Max};
+type_ranks(Type, I, Min, Max, [TypeClass|Rest], Opaques) ->
{NewMin, NewMax} =
- case t_is_none(t_inf(Type, TypeClass)) of
+ case t_is_none(t_inf(Type, TypeClass, Opaques)) of
true -> {Min, Max};
false -> case Min of
0 -> {I, I};
_ -> {Min, I}
end
end,
- type_ranks(Type, I+1, NewMin, NewMax, Rest).
+ type_ranks(Type, I+1, NewMin, NewMax, Rest, Opaques).
type_order() ->
[t_number(), t_atom(), t_reference(), t_fun(), t_port(), t_pid(), t_tuple(),
t_list(), t_binary()].
-key_comparisons_fail(X0, KeyPos, TupleList) ->
- X = case t_is_number(t_inf(X0, t_number())) of
+key_comparisons_fail(X0, KeyPos, TupleList, Opaques) ->
+ X = case t_is_number(t_inf(X0, t_number(), Opaques), Opaques) of
false -> X0;
true -> t_number()
end,
lists:all(fun(Tuple) ->
Key = type(erlang, element, 2, [KeyPos, Tuple]),
- t_is_none(t_inf(Key, X))
+ t_is_none(t_inf(Key, X, Opaques))
end, TupleList).
%%=============================================================================
--spec arg_types(atom(), atom(), arity()) -> [erl_types:erl_type()] | 'unknown'.
+-spec arg_types(atom(), atom(), arity()) -> arg_types() | 'unknown'.
%%------- erlang --------------------------------------------------------------
arg_types(erlang, '!', 2) ->
@@ -2213,6 +2311,8 @@ arg_types(erlang, is_integer, 1) ->
[t_any()];
arg_types(erlang, is_list, 1) ->
[t_any()];
+arg_types(erlang, is_map, 1) ->
+ [t_any()];
arg_types(erlang, is_number, 1) ->
[t_any()];
arg_types(erlang, is_pid, 1) ->
@@ -2230,6 +2330,9 @@ arg_types(erlang, is_tuple, 1) ->
%% Guard bif, needs to be here.
arg_types(erlang, length, 1) ->
[t_list()];
+%% Guard bif, needs to be here.
+arg_types(erlang, map_size, 1) ->
+ [t_map()];
arg_types(erlang, make_tuple, 2) ->
[t_non_neg_fixnum(), t_any()]; % the value 0 is OK as first argument
arg_types(erlang, make_tuple, 3) ->
@@ -2351,12 +2454,12 @@ arg_types(hipe_bifs, fun_to_address, 1) ->
[t_mfa()];
%% arg_types(hipe_bifs, get_emu_address, 1) ->
%% [t_mfa()];
+arg_types(hipe_bifs, get_fe, 2) ->
+ [t_atom(), t_tuple([t_integer(), t_integer(), t_integer()])];
arg_types(hipe_bifs, get_rts_param, 1) ->
[t_fixnum()];
arg_types(hipe_bifs, invalidate_funinfo_native_addresses, 1) ->
[t_list(t_mfa())];
-arg_types(hipe_bifs, make_fe, 3) ->
- [t_integer(), t_atom(), t_tuple([t_integer(), t_integer(), t_integer()])];
%% arg_types(hipe_bifs, make_native_stub, 2) ->
%% [t_integer(), t_arity()];
arg_types(hipe_bifs, mark_referred_from, 1) ->
@@ -2385,6 +2488,8 @@ arg_types(hipe_bifs, set_funinfo_native_address, 3) ->
arg_types(hipe_bifs, set_native_address, 3);
arg_types(hipe_bifs, set_native_address, 3) ->
[t_mfa(), t_integer(), t_boolean()];
+arg_types(hipe_bifs, set_native_address_in_fe, 2) ->
+ [t_integer(), t_integer()];
arg_types(hipe_bifs, system_crc, 1) ->
[t_crc32()];
arg_types(hipe_bifs, term_to_word, 1) ->
@@ -2508,47 +2613,78 @@ arg_types(M, F, A) when is_atom(M), is_atom(F),
unknown. % safe approximation for all functions.
--spec is_known(atom(), atom(), arity()) -> boolean().
+-spec is_known(module(), atom(), arity()) -> boolean().
is_known(M, F, A) ->
arg_types(M, F, A) =/= unknown.
+-spec opaque_args(module(), atom(), arity(),
+ arg_types(), opaques()) -> [pos_integer()].
+
+%% Use this function to find out which argument caused empty type.
+
+opaque_args(_M, _F, _A, _Xs, 'universe') -> [];
+opaque_args(M, F, A, Xs, Opaques) ->
+ case kind_of_check(M, F, A) of
+ record ->
+ [X,Y|_] = Xs,
+ [1 ||
+ case t_is_tuple(X, Opaques) of
+ true ->
+ case t_tuple_subtypes(X, Opaques) of
+ unknown -> false;
+ List when length(List) >= 1 -> opaque_recargs(List, Y, Opaques)
+ end;
+ false -> t_has_opaque_subtype(X, Opaques)
+ end];
+ subtype ->
+ [N ||
+ {N, X} <- lists:zip(lists:seq(1, length(Xs)), Xs),
+ t_has_opaque_subtype(X, Opaques)];
+ find_unknown ->
+ [L, R] = Xs,
+ erl_types:t_find_unknown_opaque(L, R, Opaques);
+ no_check -> []
+ end.
--spec structure_inspecting_args(atom(), atom(), arity()) -> [1..255].
-
-structure_inspecting_args(erlang, element, 2) -> [2];
-structure_inspecting_args(erlang, is_atom, 1) -> [1];
-structure_inspecting_args(erlang, is_boolean, 1) -> [1];
-structure_inspecting_args(erlang, is_binary, 1) -> [1];
-structure_inspecting_args(erlang, is_bitstring, 1) -> [1];
-structure_inspecting_args(erlang, is_float, 1) -> [1];
-structure_inspecting_args(erlang, is_function, 1) -> [1];
-structure_inspecting_args(erlang, is_integer, 1) -> [1];
-structure_inspecting_args(erlang, is_list, 1) -> [1];
-structure_inspecting_args(erlang, is_number, 1) -> [1];
-structure_inspecting_args(erlang, is_pid, 1) -> [1];
-structure_inspecting_args(erlang, is_port, 1) -> [1];
-structure_inspecting_args(erlang, is_reference, 1) -> [1];
-structure_inspecting_args(erlang, is_tuple, 1) -> [1];
-structure_inspecting_args(erlang, length, 1) -> [1];
-%%structure_inspecting_args(erlang, setelement, 3) -> [2].
-structure_inspecting_args(_, _, _) -> []. % XXX: assume no arg needs inspection
-
-
-check_fun_application(Fun, Args) ->
- case t_is_fun(Fun) of
+kind_of_check(erlang, is_record, 3) ->
+ record;
+kind_of_check(erlang, is_record, 2) ->
+ record;
+kind_of_check(erlang, F, A) ->
+ case erl_internal:guard_bif(F, A) orelse erl_internal:bool_op(F, A) of
+ true -> subtype;
+ false ->
+ case erl_internal:comp_op(F, A) of
+ true -> find_unknown;
+ false -> no_check
+ end
+ end;
+kind_of_check(_M, _F, _A) -> no_check.
+
+opaque_recargs(Tuples, Y, Opaques) ->
+ Fun = fun(Tuple) ->
+ case t_tuple_args(Tuple, Opaques) of
+ [Tag|_] -> t_is_none(check_record_tag(Tag, Y, Opaques));
+ _ -> false
+ end
+ end,
+ lists:all(Fun, Tuples).
+
+check_fun_application(Fun, Args, Opaques) ->
+ case t_is_fun(Fun, Opaques) of
true ->
- case t_fun_args(Fun) of
+ case t_fun_args(Fun, Opaques) of
unknown ->
- case t_is_none_or_unit(t_fun_range(Fun)) of
+ case t_is_none_or_unit(t_fun_range(Fun, Opaques)) of
true -> error;
false -> ok
end;
FunDom when length(FunDom) =:= length(Args) ->
- case any_is_none_or_unit(inf_lists(FunDom, Args)) of
+ case any_is_none_or_unit(inf_lists(FunDom, Args, Opaques)) of
true -> error;
false ->
- case t_is_none_or_unit(t_fun_range(Fun)) of
+ case t_is_none_or_unit(t_fun_range(Fun, Opaques)) of
true -> error;
false -> ok
end
diff --git a/lib/hipe/cerl/erl_types.erl b/lib/hipe/cerl/erl_types.erl
index d7d8a878c5..67661130a5 100644
--- a/lib/hipe/cerl/erl_types.erl
+++ b/lib/hipe/cerl/erl_types.erl
@@ -2,7 +2,7 @@
%%
%% %CopyrightBegin%
%%
-%% Copyright Ericsson AB 2003-2013. All Rights Reserved.
+%% Copyright Ericsson AB 2003-2014. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
@@ -42,15 +42,15 @@
max/2,
module_builtin_opaques/1,
min/2,
- number_max/1,
- number_min/1,
+ number_max/1, number_max/2,
+ number_min/1, number_min/2,
t_abstract_records/2,
t_any/0,
t_arity/0,
t_atom/0,
t_atom/1,
t_atoms/1,
- t_atom_vals/1,
+ t_atom_vals/1, t_atom_vals/2,
t_binary/0,
t_bitstr/0,
t_bitstr/2,
@@ -66,12 +66,14 @@
t_collect_vars/1,
t_cons/0,
t_cons/2,
- t_cons_hd/1,
- t_cons_tl/1,
+ t_cons_hd/1, t_cons_hd/2,
+ t_cons_tl/1, t_cons_tl/2,
t_constant/0,
- t_contains_opaque/1,
+ t_contains_opaque/1, t_contains_opaque/2,
+ t_decorate_with_opaque/3,
t_elements/1,
t_find_opaque_mismatch/2,
+ t_find_unknown_opaque/3,
t_fixnum/0,
t_map/2,
t_non_neg_fixnum/0,
@@ -87,18 +89,18 @@
t_fun/0,
t_fun/1,
t_fun/2,
- t_fun_args/1,
- t_fun_arity/1,
- t_fun_range/1,
- t_has_opaque_subtype/1,
+ t_fun_args/1, t_fun_args/2,
+ t_fun_arity/1, t_fun_arity/2,
+ t_fun_range/1, t_fun_range/2,
+ t_has_opaque_subtype/2,
t_has_var/1,
t_identifier/0,
%% t_improper_list/2,
- t_inf/2,
- t_inf/3,
- t_inf_lists/2,
- t_inf_lists/3,
- t_inf_lists_masked/3,
+ t_inf/1,
+ t_inf/2,
+ t_inf/3,
+ t_inf_lists/2,
+ t_inf_lists/3,
t_integer/0,
t_integer/1,
t_non_neg_integer/0,
@@ -107,45 +109,49 @@
t_iodata/0,
t_iolist/0,
t_is_any/1,
- t_is_atom/1,
- t_is_atom/2,
- t_is_binary/1,
- t_is_bitstr/1,
+ t_is_atom/1, t_is_atom/2,
+ t_is_any_atom/2, t_is_any_atom/3,
+ t_is_binary/1, t_is_binary/2,
+ t_is_bitstr/1, t_is_bitstr/2,
t_is_bitwidth/1,
- t_is_boolean/1,
+ t_is_boolean/1, t_is_boolean/2,
%% t_is_byte/1,
%% t_is_char/1,
- t_is_cons/1,
+ t_is_cons/1, t_is_cons/2,
t_is_constant/1,
t_is_equal/2,
t_is_fixnum/1,
- t_is_float/1,
- t_is_fun/1,
+ t_is_float/1, t_is_float/2,
+ t_is_fun/1, t_is_fun/2,
t_is_instance/2,
- t_is_integer/1,
+ t_is_integer/1, t_is_integer/2,
t_is_list/1,
+ t_is_map/1,
+ t_is_map/2,
t_is_matchstate/1,
- t_is_nil/1,
+ t_is_nil/1, t_is_nil/2,
t_is_non_neg_integer/1,
t_is_none/1,
t_is_none_or_unit/1,
- t_is_number/1,
- t_is_opaque/1,
- t_is_pid/1,
- t_is_port/1,
- t_is_maybe_improper_list/1,
- t_is_reference/1,
+ t_is_number/1, t_is_number/2,
+ t_is_opaque/1, t_is_opaque/2,
+ t_is_pid/1, t_is_pid/2,
+ t_is_port/1, t_is_port/2,
+ t_is_maybe_improper_list/1, t_is_maybe_improper_list/2,
+ t_is_reference/1, t_is_reference/2,
t_is_remote/1,
t_is_string/1,
t_is_subtype/2,
- t_is_tuple/1,
+ t_is_tuple/1, t_is_tuple/2,
t_is_unit/1,
t_is_var/1,
t_limit/2,
t_list/0,
t_list/1,
- t_list_elements/1,
+ t_list_elements/1, t_list_elements/2,
t_list_termination/1,
+ t_map/0,
+ t_map/1,
t_matchstate/0,
t_matchstate/2,
t_matchstate_present/1,
@@ -163,11 +169,8 @@
t_nonempty_string/0,
t_number/0,
t_number/1,
- t_number_vals/1,
+ t_number_vals/1, t_number_vals/2,
t_opaque_from_records/1,
- t_opaque_match_atom/2,
- t_opaque_match_record/2,
- t_opaque_matching_structure/2,
t_opaque_structure/1,
%% t_parameterized_module/0,
t_pid/0,
@@ -192,23 +195,23 @@
t_to_tlist/1,
t_tuple/0,
t_tuple/1,
- t_tuple_args/1,
- t_tuple_size/1,
+ t_tuple_args/1, t_tuple_args/2,
+ t_tuple_size/1, t_tuple_size/2,
t_tuple_sizes/1,
t_tuple_subtypes/1,
+ t_tuple_subtypes/2,
t_unify/2,
- t_unify/3,
t_unit/0,
- t_unopaque/1,
- t_unopaque/2,
- t_unopaque_on_mismatch/3,
+ t_unopaque/1, t_unopaque/2,
t_var/1,
t_var_name/1,
%% t_assign_variables_to_subtype/2,
type_is_defined/4,
record_field_diffs_to_string/2,
subst_all_vars_to_any/1,
+ subst_all_remote/2,
lift_list_to_pos_empty/1,
+ is_opaque_type/2,
is_erl_type/1,
atom_to_string/1
]).
@@ -226,7 +229,15 @@
-export([t_is_identifier/1]).
-endif.
--export_type([erl_type/0]).
+-export_type([erl_type/0, type_table/0, var_table/0]).
+
+%%-define(DEBUG, true).
+
+-ifdef(DEBUG).
+-define(debug(__A), __A).
+-else.
+-define(debug(__A), ok).
+-endif.
%%=============================================================================
%%
@@ -260,6 +271,7 @@
-define(function_tag, function).
-define(identifier_tag, identifier).
-define(list_tag, list).
+-define(map_tag, map).
-define(matchstate_tag, matchstate).
-define(nil_tag, nil).
-define(number_tag, number).
@@ -272,7 +284,7 @@
-define(var_tag, var).
-type tag() :: ?atom_tag | ?binary_tag | ?function_tag | ?identifier_tag
- | ?list_tag | ?matchstate_tag | ?nil_tag | ?number_tag
+ | ?list_tag | ?map_tag | ?matchstate_tag | ?nil_tag | ?number_tag
| ?opaque_tag | ?product_tag | ?remote_tag
| ?tuple_tag | ?tuple_set_tag | ?union_tag | ?var_tag.
@@ -310,6 +322,9 @@
-record(int_set, {set :: [integer()]}).
-record(int_rng, {from :: rng_elem(), to :: rng_elem()}).
+%% Note: the definition of #opaque{} was changed to 'mod' and 'name';
+%% it used to be an ordsets of {Mod, Name} pairs. The Dialyzer version
+%% was updated to 2.7 due to this change.
-record(opaque, {mod :: module(), name :: atom(),
args = [] :: [erl_type()], struct :: erl_type()}).
-record(remote, {mod:: module(), name :: atom(), args = [] :: [erl_type()]}).
@@ -329,6 +344,7 @@
-define(nonempty_list(Types, Term),?list(Types, Term, ?nonempty_qual)).
-define(number(Set, Qualifier), #c{tag=?number_tag, elements=Set,
qualifier=Qualifier}).
+-define(map(Pairs), #c{tag=?map_tag, elements=Pairs}).
-define(opaque(Optypes), #c{tag=?opaque_tag, elements=Optypes}).
-define(product(Types), #c{tag=?product_tag, elements=Types}).
-define(remote(RemTypes), #c{tag=?remote_tag, elements=RemTypes}).
@@ -346,22 +362,34 @@
-define(integer_non_neg, ?int_range(0, pos_inf)).
-define(integer_neg, ?int_range(neg_inf, -1)).
+-type opaques() :: [erl_type()] | 'universe'.
+
+-type record_key() :: {'record', atom()}.
+-type type_key() :: {'type' | 'opaque', atom(), arity()}.
+-type record_value() :: orddict:orddict(). % XXX. To be refined
+-type type_value() :: {module(), erl_type(), atom()}.
+-type type_table() :: dict:dict(record_key(), record_value())
+ | dict:dict(type_key(), type_value()).
+
+-type var_table() :: dict:dict(atom(), erl_type()).
+
%%-----------------------------------------------------------------------------
%% Unions
%%
--define(union(List), #c{tag=?union_tag, elements=[_,_,_,_,_,_,_,_,_,_]=List}).
-
--define(atom_union(T), ?union([T,?none,?none,?none,?none,?none,?none,?none,?none,?none])).
--define(bitstr_union(T), ?union([?none,T,?none,?none,?none,?none,?none,?none,?none,?none])).
--define(function_union(T), ?union([?none,?none,T,?none,?none,?none,?none,?none,?none,?none])).
--define(identifier_union(T), ?union([?none,?none,?none,T,?none,?none,?none,?none,?none,?none])).
--define(list_union(T), ?union([?none,?none,?none,?none,T,?none,?none,?none,?none,?none])).
--define(number_union(T), ?union([?none,?none,?none,?none,?none,T,?none,?none,?none,?none])).
--define(tuple_union(T), ?union([?none,?none,?none,?none,?none,?none,T,?none,?none,?none])).
--define(matchstate_union(T), ?union([?none,?none,?none,?none,?none,?none,?none,T,?none,?none])).
--define(opaque_union(T), ?union([?none,?none,?none,?none,?none,?none,?none,?none,T,?none])).
--define(remote_union(T), ?union([?none,?none,?none,?none,?none,?none,?none,?none,?none,T])).
+-define(union(List), #c{tag=?union_tag, elements=[_,_,_,_,_,_,_,_,_,_,_]=List}).
+
+-define(atom_union(T), ?union([T,?none,?none,?none,?none,?none,?none,?none,?none,?none,?none])).
+-define(bitstr_union(T), ?union([?none,T,?none,?none,?none,?none,?none,?none,?none,?none,?none])).
+-define(function_union(T), ?union([?none,?none,T,?none,?none,?none,?none,?none,?none,?none,?none])).
+-define(identifier_union(T), ?union([?none,?none,?none,T,?none,?none,?none,?none,?none,?none,?none])).
+-define(list_union(T), ?union([?none,?none,?none,?none,T,?none,?none,?none,?none,?none,?none])).
+-define(number_union(T), ?union([?none,?none,?none,?none,?none,T,?none,?none,?none,?none,?none])).
+-define(tuple_union(T), ?union([?none,?none,?none,?none,?none,?none,T,?none,?none,?none,?none])).
+-define(matchstate_union(T), ?union([?none,?none,?none,?none,?none,?none,?none,T,?none,?none,?none])).
+-define(opaque_union(T), ?union([?none,?none,?none,?none,?none,?none,?none,?none,T,?none,?none])).
+-define(remote_union(T), ?union([?none,?none,?none,?none,?none,?none,?none,?none,?none,T,?none])).
+-define(map_union(T), ?union([?none,?none,?none,?none,?none,?none,?none,?none,?none,?none,T])).
-define(integer_union(T), ?number_union(T)).
-define(float_union(T), ?number_union(T)).
-define(nil_union(T), ?list_union(T)).
@@ -384,8 +412,11 @@ t_any() ->
-spec t_is_any(erl_type()) -> boolean().
-t_is_any(?any) -> true;
-t_is_any(_) -> false.
+t_is_any(Type) ->
+ do_opaque(Type, 'universe', fun is_any/1).
+
+is_any(?any) -> true;
+is_any(_) -> false.
-spec t_none() -> erl_type().
@@ -407,16 +438,25 @@ t_opaque(Mod, Name, Args, Struct) ->
O = #opaque{mod = Mod, name = Name, args = Args, struct = Struct},
?opaque(set_singleton(O)).
+-spec t_is_opaque(erl_type(), [erl_type()]) -> boolean().
+
+t_is_opaque(?opaque(_) = Type, Opaques) ->
+ not is_opaque_type(Type, Opaques);
+t_is_opaque(_Type, _Opaques) -> false.
+
-spec t_is_opaque(erl_type()) -> boolean().
t_is_opaque(?opaque(_)) -> true;
t_is_opaque(_) -> false.
--spec t_has_opaque_subtype(erl_type()) -> boolean().
+-spec t_has_opaque_subtype(erl_type(), opaques()) -> boolean().
+
+t_has_opaque_subtype(Type, Opaques) ->
+ do_opaque(Type, Opaques, fun has_opaque_subtype/1).
-t_has_opaque_subtype(?union(Ts)) ->
+has_opaque_subtype(?union(Ts)) ->
lists:any(fun t_is_opaque/1, Ts);
-t_has_opaque_subtype(T) ->
+has_opaque_subtype(T) ->
t_is_opaque(T).
-spec t_opaque_structure(erl_type()) -> erl_type().
@@ -424,74 +464,65 @@ t_has_opaque_subtype(T) ->
t_opaque_structure(?opaque(Elements)) ->
t_sup([Struct || #opaque{struct = Struct} <- ordsets:to_list(Elements)]).
--spec t_opaque_module(erl_type()) -> module().
+-spec t_opaque_modules(erl_type()) -> [module()].
-t_opaque_module(?opaque(Elements)) ->
+t_opaque_modules(?opaque(Elements)) ->
case ordsets:size(Elements) of
1 ->
- [#opaque{mod = Module}] = ordsets:to_list(Elements),
- Module;
+ [#opaque{mod = Mod}] = set_to_list(Elements),
+ [Mod];
_ -> throw({error, "Unexpected multiple opaque types"})
end.
-%% This only makes sense if we know that Type matches Opaque
--spec t_opaque_matching_structure(erl_type(), erl_type()) -> erl_type().
-
-t_opaque_matching_structure(Type, Opaque) ->
- OpaqueStruct = t_opaque_structure(Opaque),
- case OpaqueStruct of
- ?union(L1) ->
- case Type of
- ?union(_L2) -> OpaqueStruct;
- _OtherType -> t_opaque_matching_structure_list(Type, L1)
- end;
- ?tuple_set(_Set1) = TupleSet ->
- case Type of
- ?tuple_set(_Set2) -> OpaqueStruct;
- _ -> t_opaque_matching_structure_list(Type, t_tuple_subtypes(TupleSet))
- end;
- _Other -> OpaqueStruct
- end.
-
-t_opaque_matching_structure_list(Type, List) ->
- NewList = [t_inf(Element, Type) || Element <- List],
- Results = [NotNone || NotNone <- NewList, NotNone =/= ?none],
- case Results of
- [] -> ?none;
- [First|_] -> First
- end.
-
-spec t_contains_opaque(erl_type()) -> boolean().
-t_contains_opaque(?any) -> false;
-t_contains_opaque(?none) -> false;
-t_contains_opaque(?unit) -> false;
-t_contains_opaque(?atom(_Set)) -> false;
-t_contains_opaque(?bitstr(_Unit, _Base)) -> false;
-t_contains_opaque(?float) -> false;
-t_contains_opaque(?function(Domain, Range)) ->
- t_contains_opaque(Domain) orelse t_contains_opaque(Range);
-t_contains_opaque(?identifier(_Types)) -> false;
-t_contains_opaque(?integer(_Types)) -> false;
-t_contains_opaque(?int_range(_From, _To)) -> false;
-t_contains_opaque(?int_set(_Set)) -> false;
-t_contains_opaque(?list(Type, _, _)) -> t_contains_opaque(Type);
-t_contains_opaque(?matchstate(_P, _Slots)) -> false;
-t_contains_opaque(?nil) -> false;
-t_contains_opaque(?number(_Set, _Tag)) -> false;
-t_contains_opaque(?opaque(_)) -> true;
-t_contains_opaque(?product(Types)) -> list_contains_opaque(Types);
-t_contains_opaque(?tuple(?any, _, _)) -> false;
-t_contains_opaque(?tuple(Types, _, _)) -> list_contains_opaque(Types);
-t_contains_opaque(?tuple_set(_Set) = T) ->
- list_contains_opaque(t_tuple_subtypes(T));
-t_contains_opaque(?union(List)) -> list_contains_opaque(List);
-t_contains_opaque(?var(_Id)) -> false.
-
--spec list_contains_opaque([erl_type()]) -> boolean().
-
-list_contains_opaque(List) ->
- lists:any(fun t_contains_opaque/1, List).
+t_contains_opaque(Type) ->
+ t_contains_opaque(Type, []).
+
+%% Returns 'true' iff there is an opaque type that is *not* one of
+%% the types of the second argument.
+
+-spec t_contains_opaque(erl_type(), [erl_type()]) -> boolean().
+
+t_contains_opaque(?any, _Opaques) -> false;
+t_contains_opaque(?none, _Opaques) -> false;
+t_contains_opaque(?unit, _Opaques) -> false;
+t_contains_opaque(?atom(_Set), _Opaques) -> false;
+t_contains_opaque(?bitstr(_Unit, _Base), _Opaques) -> false;
+t_contains_opaque(?float, _Opaques) -> false;
+t_contains_opaque(?function(Domain, Range), Opaques) ->
+ t_contains_opaque(Domain, Opaques)
+ orelse t_contains_opaque(Range, Opaques);
+t_contains_opaque(?identifier(_Types), _Opaques) -> false;
+t_contains_opaque(?integer(_Types), _Opaques) -> false;
+t_contains_opaque(?int_range(_From, _To), _Opaques) -> false;
+t_contains_opaque(?int_set(_Set), _Opaques) -> false;
+t_contains_opaque(?list(Type, Tail, _), Opaques) ->
+ t_contains_opaque(Type, Opaques) orelse t_contains_opaque(Tail, Opaques);
+t_contains_opaque(?map(_) = Map, Opaques) ->
+ list_contains_opaque(map_values(Map), Opaques) orelse
+ list_contains_opaque(map_keys(Map), Opaques);
+t_contains_opaque(?matchstate(_P, _Slots), _Opaques) -> false;
+t_contains_opaque(?nil, _Opaques) -> false;
+t_contains_opaque(?number(_Set, _Tag), _Opaques) -> false;
+t_contains_opaque(?opaque(_)=T, Opaques) ->
+ not is_opaque_type(T, Opaques)
+ orelse t_contains_opaque(t_opaque_structure(T));
+t_contains_opaque(?product(Types), Opaques) ->
+ list_contains_opaque(Types, Opaques);
+t_contains_opaque(?tuple(?any, _, _), _Opaques) -> false;
+t_contains_opaque(?tuple(Types, _, _), Opaques) ->
+ list_contains_opaque(Types, Opaques);
+t_contains_opaque(?tuple_set(_Set) = T, Opaques) ->
+ list_contains_opaque(t_tuple_subtypes(T), Opaques);
+t_contains_opaque(?union(List), Opaques) ->
+ list_contains_opaque(List, Opaques);
+t_contains_opaque(?var(_Id), _Opaques) -> false.
+
+-spec list_contains_opaque([erl_type()], [erl_type()]) -> boolean().
+
+list_contains_opaque(List, Opaques) ->
+ lists:any(fun(E) -> t_contains_opaque(E, Opaques) end, List).
%% t_find_opaque_mismatch/2 of two types should only be used if their
%% t_inf is t_none() due to some opaque type violation.
@@ -506,9 +537,12 @@ t_find_opaque_mismatch(T1, T2) ->
t_find_opaque_mismatch(?any, _Type, _TopType) -> error;
t_find_opaque_mismatch(?none, _Type, _TopType) -> error;
-t_find_opaque_mismatch(?list(T1, _, _), ?list(T2, _, _), TopType) ->
- t_find_opaque_mismatch(T1, T2, TopType);
+t_find_opaque_mismatch(?list(T1, Tl1, _), ?list(T2, Tl2, _), TopType) ->
+ t_find_opaque_mismatch_ordlists([T1, Tl1], [T2, Tl2], TopType);
t_find_opaque_mismatch(_T1, ?opaque(_) = T2, TopType) -> {ok, TopType, T2};
+t_find_opaque_mismatch(?opaque(_) = T1, _T2, TopType) ->
+ %% The generated message is somewhat misleading:
+ {ok, TopType, T1};
t_find_opaque_mismatch(?product(T1), ?product(T2), TopType) ->
t_find_opaque_mismatch_ordlists(T1, T2, TopType);
t_find_opaque_mismatch(?tuple(T1, Arity, _), ?tuple(T2, Arity, _), TopType) ->
@@ -538,7 +572,169 @@ t_find_opaque_mismatch_list([H|T]) ->
error -> t_find_opaque_mismatch_list(T)
end.
--spec t_opaque_from_records(dict()) -> [erl_type()].
+-spec t_find_unknown_opaque(erl_type(), erl_type(), opaques()) ->
+ [pos_integer()].
+
+%% The nice thing about using two types and t_inf() as compared to
+%% calling t_contains_opaque/2 is that the traversal stops when
+%% there is a mismatch which means that unknown opaque types "below"
+%% the mismatch are not found.
+%% XXX. Returns one element even if both oparands contain opaque types.
+%% XXX. Slow since t_inf() is called but the results are ignored.
+t_find_unknown_opaque(_T1, _T2, 'universe') -> [];
+t_find_unknown_opaque(T1, T2, Opaques) ->
+ try t_inf(T1, T2, {match, Opaques}) of
+ _ -> []
+ catch throw:N when is_integer(N) -> [N]
+ end.
+
+-spec t_decorate_with_opaque(erl_type(), erl_type(), [erl_type()]) -> erl_type().
+
+%% The first argument can contain opaque types. The second argument
+%% is assumed to be taken from the contract.
+
+t_decorate_with_opaque(T1, T2, Opaques) ->
+ case t_is_equal(T1, T2) orelse not t_contains_opaque(T2) of
+ true -> T1;
+ false ->
+ T = t_inf(T1, T2),
+ case t_contains_opaque(T) of
+ false -> T1;
+ true ->
+ R = decorate(T1, T, Opaques),
+ ?debug(case catch t_is_equal(t_unopaque(R), t_unopaque(T1)) of
+ true -> ok;
+ false ->
+ io:format("T1 = ~p,\n", [T1]),
+ io:format("T2 = ~p,\n", [T2]),
+ io:format("O = ~p,\n", [Opaques]),
+ io:format("erl_types:t_decorate_with_opaque(T1,T2,O).\n"),
+ throw({error, "Failed to handle opaque types"})
+ end),
+ R
+ end
+ end.
+
+decorate(Type, ?none, _Opaques) -> Type;
+decorate(?function(Domain, Range), ?function(D, R), Opaques) ->
+ ?function(decorate(Domain, D, Opaques), decorate(Range, R, Opaques));
+decorate(?list(Types, Tail, Size), ?list(Ts, Tl, _Sz), Opaques) ->
+ ?list(decorate(Types, Ts, Opaques), decorate(Tail, Tl, Opaques), Size);
+decorate(?product(Types), ?product(Ts), Opaques) ->
+ ?product(list_decorate(Types, Ts, Opaques));
+decorate(?tuple(_, _, _)=T, ?tuple(?any, _, _), _Opaques) -> T;
+decorate(?tuple(?any, _, _)=T, ?tuple(_, _, _), _Opaques) -> T;
+decorate(?tuple(Types, Arity, Tag), ?tuple(Ts, Arity, _), Opaques) ->
+ ?tuple(list_decorate(Types, Ts, Opaques), Arity, Tag);
+decorate(?tuple_set(List), ?tuple(_, Arity, _) = T, Opaques) ->
+ decorate_tuple_sets(List, [{Arity, [T]}], Opaques);
+decorate(?tuple_set(List), ?tuple_set(L), Opaques) ->
+ decorate_tuple_sets(List, L, Opaques);
+decorate(?union(List), T, Opaques) when T =/= ?any ->
+ ?union(L) = force_union(T),
+ union_decorate(List, L, Opaques);
+decorate(?opaque(_)=T, _, _Opaques) -> T;
+decorate(T, ?union(L), Opaques) when T =/= ?any ->
+ ?union(List) = force_union(T),
+ union_decorate(List, L, Opaques);
+decorate(Type, ?opaque(_)=T, Opaques) ->
+ decorate_with_opaque(Type, T, Opaques);
+decorate(Type, _T, _Opaques) -> Type.
+
+%% Note: it is important that #opaque.struct is a subtype of the
+%% opaque type.
+decorate_with_opaque(Type, ?opaque(Set2), Opaques) ->
+ case decoration(set_to_list(Set2), Type, Opaques, [], false) of
+ {[], false} -> Type;
+ {List, All} when List =/= [] ->
+ NewType = ?opaque(ordsets:from_list(List)),
+ case All of
+ true -> NewType;
+ false -> t_sup(NewType, Type)
+ end
+ end.
+
+decoration([#opaque{struct = S} = Opaque|OpaqueTypes], Type, Opaques,
+ NewOpaqueTypes0, All) ->
+ IsOpaque = is_opaque_type2(Opaque, Opaques),
+ I = t_inf(Type, S),
+ case not IsOpaque orelse t_is_none(I) of
+ true -> decoration(OpaqueTypes, Type, Opaques, NewOpaqueTypes0, All);
+ false ->
+ NewOpaque = Opaque#opaque{struct = decorate(I, S, Opaques)},
+ NewAll = All orelse t_is_equal(I, Type),
+ NewOpaqueTypes = [NewOpaque|NewOpaqueTypes0],
+ decoration(OpaqueTypes, Type, Opaques, NewOpaqueTypes, NewAll)
+ end;
+decoration([], _Type, _Opaques, NewOpaqueTypes, All) ->
+ {NewOpaqueTypes, All}.
+
+-spec list_decorate([erl_type()], [erl_type()], opaques()) -> [erl_type()].
+
+list_decorate(List, L, Opaques) ->
+ [decorate(Elem, E, Opaques) || {Elem, E} <- lists:zip(List, L)].
+
+union_decorate(U1, U2, Opaques) ->
+ Union = union_decorate(U1, U2, Opaques, 0, []),
+ [A,B,F,I,L,N,T,M,_,_R,Map] = U1,
+ [_,_,_,_,_,_,_,_,Opaque,_,_] = U2,
+ List = [A,B,F,I,L,N,T,M,Map],
+ DecList = [Dec ||
+ E <- List,
+ not t_is_none(E),
+ not t_is_none(Dec = decorate(E, Opaque, Opaques))],
+ t_sup([Union|DecList]).
+
+union_decorate([?none|Left1], [_|Left2], Opaques, N, Acc) ->
+ union_decorate(Left1, Left2, Opaques, N, [?none|Acc]);
+union_decorate([T1|Left1], [?none|Left2], Opaques, N, Acc) ->
+ union_decorate(Left1, Left2, Opaques, N+1, [T1|Acc]);
+union_decorate([T1|Left1], [T2|Left2], Opaques, N, Acc) ->
+ union_decorate(Left1, Left2, Opaques, N+1, [decorate(T1, T2, Opaques)|Acc]);
+union_decorate([], [], _Opaques, N, Acc) ->
+ if N =:= 0 -> ?none;
+ N =:= 1 ->
+ [Type] = [T || T <- Acc, T =/= ?none],
+ Type;
+ N >= 2 -> ?union(lists:reverse(Acc))
+ end.
+
+decorate_tuple_sets(List, L, Opaques) ->
+ decorate_tuple_sets(List, L, Opaques, []).
+
+decorate_tuple_sets([{Arity, Tuples}|List], [{Arity, Ts}|L], Opaques, Acc) ->
+ DecTs = decorate_tuples_in_sets(Tuples, Ts, Opaques),
+ decorate_tuple_sets(List, L, Opaques, [{Arity, DecTs}|Acc]);
+decorate_tuple_sets([ArTup|List], L, Opaques, Acc) ->
+ decorate_tuple_sets(List, L, Opaques, [ArTup|Acc]);
+decorate_tuple_sets([], _L, _Opaques, Acc) ->
+ ?tuple_set(lists:reverse(Acc)).
+
+decorate_tuples_in_sets([?tuple(Elements, _, ?any)], Ts, Opaques) ->
+ NewList = [list_decorate(Elements, Es, Opaques) || ?tuple(Es, _, _) <- Ts],
+ case t_sup([t_tuple(Es) || Es <- NewList]) of
+ ?tuple_set([{_Arity, Tuples}]) -> Tuples;
+ ?tuple(_, _, _)=Tuple -> [Tuple]
+ end;
+decorate_tuples_in_sets(Tuples, Ts, Opaques) ->
+ decorate_tuples_in_sets(Tuples, Ts, Opaques, []).
+
+decorate_tuples_in_sets([?tuple(Elements, Arity, Tag1) = T1|Tuples] = L1,
+ [?tuple(Es, Arity, Tag2)|Ts] = L2, Opaques, Acc) ->
+ if
+ Tag1 < Tag2 -> decorate_tuples_in_sets(Tuples, L2, Opaques, [T1|Acc]);
+ Tag1 > Tag2 -> decorate_tuples_in_sets(L1, Ts, Opaques, Acc);
+ Tag1 =:= Tag2 ->
+ NewElements = list_decorate(Elements, Es, Opaques),
+ NewAcc = [?tuple(NewElements, Arity, Tag1)|Acc],
+ decorate_tuples_in_sets(Tuples, Ts, Opaques, NewAcc)
+ end;
+decorate_tuples_in_sets([T1|Tuples], L2, Opaques, Acc) ->
+ decorate_tuples_in_sets(Tuples, L2, Opaques, [T1|Acc]);
+decorate_tuples_in_sets([], _L, _Opaques, Acc) ->
+ lists:reverse(Acc).
+
+-spec t_opaque_from_records(type_table()) -> [erl_type()].
t_opaque_from_records(RecDict) ->
OpaqueRecDict =
@@ -549,54 +745,17 @@ t_opaque_from_records(RecDict) ->
end
end, RecDict),
OpaqueTypeDict =
- dict:map(fun({opaque, Name, _Arity}, {Module, Type, ArgNames}) ->
- case ArgNames of
- [] ->
- t_opaque(Module, Name, [], t_from_form(Type, RecDict));
- _ ->
- throw({error,"Polymorphic opaque types not supported yet"})
- end
+ dict:map(fun({opaque, Name, _Arity}, {Module, _Type, ArgNames}) ->
+ %% Args = args_to_types(ArgNames),
+ %% List = lists:zip(ArgNames, Args),
+ %% TmpVarDict = dict:from_list(List),
+ %% Rep = t_from_form(Type, RecDict, TmpVarDict),
+ Rep = t_none(), % not used for anything right now
+ Args = [t_any() || _ <- ArgNames],
+ skip_opaque_alias(Rep, Module, Name, Args)
end, OpaqueRecDict),
[OpaqueType || {_Key, OpaqueType} <- dict:to_list(OpaqueTypeDict)].
--spec t_opaque_match_atom(erl_type(), [erl_type()]) -> [erl_type()].
-
-t_opaque_match_atom(?atom(_) = Atom, Opaques) ->
- case t_atom_vals(Atom) of
- unknown -> [];
- _ -> [O || O <- Opaques, t_inf(Atom, O, opaque) =/= ?none,
- t_opaque_atom_vals(t_opaque_structure(O)) =/= unknown]
- end;
-t_opaque_match_atom(_, _) -> [].
-
--spec t_opaque_atom_vals(erl_type()) -> 'unknown' | [atom(),...].
-
-t_opaque_atom_vals(OpaqueStruct) ->
- case OpaqueStruct of
- ?atom(_) -> t_atom_vals(OpaqueStruct);
- ?union([Atom,_,_,_,_,_,_,_,_,_]) -> t_atom_vals(Atom);
- _ -> unknown
- end.
-
--spec t_opaque_match_record(erl_type(), [erl_type()]) -> [erl_type()].
-
-t_opaque_match_record(?tuple([?atom(_) = Tag|_Fields], _, _) = Rec, Opaques) ->
- [O || O <- Opaques, t_inf(Rec, O, opaque) =/= ?none,
- lists:member(Tag, t_opaque_tuple_tags(t_opaque_structure(O)))];
-t_opaque_match_record(_, _) -> [].
-
--spec t_opaque_tuple_tags(erl_type()) -> [erl_type()].
-
-t_opaque_tuple_tags(OpaqueStruct) ->
- case OpaqueStruct of
- ?tuple([?atom(_) = Tag|_Fields], _, _) -> [Tag];
- ?tuple_set(_) = TupleSet ->
- Tuples = t_tuple_subtypes(TupleSet),
- lists:flatten([t_opaque_tuple_tags(T) || T <- Tuples]);
- ?union([_,_,_,_,_,_,Tuples,_,_,_]) -> t_opaque_tuple_tags(Tuples);
- _ -> []
- end.
-
%% Decompose opaque instances of type arg2 to structured types, in arg1
%% XXX: Same as t_unopaque
-spec t_struct_from_opaque(erl_type(), [erl_type()]) -> erl_type().
@@ -605,9 +764,10 @@ t_struct_from_opaque(?function(Domain, Range), Opaques) ->
?function(t_struct_from_opaque(Domain, Opaques),
t_struct_from_opaque(Range, Opaques));
t_struct_from_opaque(?list(Types, Term, Size), Opaques) ->
- ?list(t_struct_from_opaque(Types, Opaques), Term, Size);
+ ?list(t_struct_from_opaque(Types, Opaques),
+ t_struct_from_opaque(Term, Opaques), Size);
t_struct_from_opaque(?opaque(_) = T, Opaques) ->
- case lists:member(T, Opaques) of
+ case is_opaque_type(T, Opaques) of
true -> t_opaque_structure(T);
false -> T
end;
@@ -627,24 +787,10 @@ t_struct_from_opaque(Type, _Opaques) -> Type.
list_struct_from_opaque(Types, Opaques) ->
[t_struct_from_opaque(Type, Opaques) || Type <- Types].
--spec t_unopaque_on_mismatch(erl_type(), erl_type(), [erl_type()]) -> erl_type().
-
-t_unopaque_on_mismatch(GenType, Type, Opaques) ->
- case t_inf(GenType, Type) of
- ?none ->
- Unopaqued = t_unopaque(Type, Opaques),
- %% XXX: Unions might be a problem, must investigate.
- case t_inf(GenType, Unopaqued) of
- ?none -> Type;
- _ -> Unopaqued
- end;
- _ -> Type
- end.
-
-spec module_builtin_opaques(module()) -> [erl_type()].
module_builtin_opaques(Module) ->
- [O || O <- all_opaque_builtins(), t_opaque_module(O) =:= Module].
+ [O || O <- all_opaque_builtins(), lists:member(Module, t_opaque_modules(O))].
%%-----------------------------------------------------------------------------
%% Remote types: these types are used for preprocessing;
@@ -657,10 +803,15 @@ t_remote(Mod, Name, Args) ->
-spec t_is_remote(erl_type()) -> boolean().
-t_is_remote(?remote(_)) -> true;
-t_is_remote(_) -> false.
+t_is_remote(Type) ->
+ do_opaque(Type, 'universe', fun is_remote/1).
+
+is_remote(?remote(_)) -> true;
+is_remote(_) -> false.
--spec t_solve_remote(erl_type(), set(), dict()) -> erl_type().
+-type mod_records() :: dict:dict(module(), type_table()).
+
+-spec t_solve_remote(erl_type(), sets:set(mfa()), mod_records()) -> erl_type().
t_solve_remote(Type, ExpTypes, Records) ->
{RT, _RR} = t_solve_remote(Type, ExpTypes, Records, []),
@@ -697,8 +848,12 @@ t_solve_remote(?union(List), ET, R, C) ->
{t_sup(RL), RR};
t_solve_remote(T, _ET, _R, _C) -> {T, []}.
-t_solve_remote_type(#remote{mod = RemMod, name = Name, args = Args} = RemType,
+t_solve_remote_type(#remote{mod = RemMod, name = Name, args = Args0} = RemType,
ET, R, C) ->
+ Args = lists:map(fun(A) ->
+ {Arg, _} = t_solve_remote(A, ET, R, C),
+ Arg
+ end, Args0),
ArgsLen = length(Args),
case dict:find(RemMod, R) of
error ->
@@ -744,9 +899,7 @@ t_solve_remote_type(#remote{mod = RemMod, name = Name, args = Args} = RemType,
true -> t_limit(NewRep, ?REC_TYPE_LIMIT);
false -> NewRep
end,
- {t_from_form({opaque, -1, Name, {Mod, Args, RT1}},
- RemDict, TmpVarDict),
- RetRR};
+ {skip_opaque_alias(RT1, Mod, Name, Args), RetRR};
error ->
Msg = io_lib:format("Unable to find remote type ~w:~w()\n",
[RemMod, Name]),
@@ -827,40 +980,75 @@ t_atoms(List) when is_list(List) ->
-spec t_atom_vals(erl_type()) -> 'unknown' | [atom(),...].
-t_atom_vals(?atom(?any)) -> unknown;
-t_atom_vals(?atom(Set)) -> set_to_list(Set);
-t_atom_vals(Other) ->
+t_atom_vals(Type) ->
+ t_atom_vals(Type, 'universe').
+
+-spec t_atom_vals(erl_type(), opaques()) -> 'unknown' | [atom(),...].
+
+t_atom_vals(Type, Opaques) ->
+ do_opaque(Type, Opaques, fun atom_vals/1).
+
+atom_vals(?atom(?any)) -> unknown;
+atom_vals(?atom(Set)) -> set_to_list(Set);
+atom_vals(?opaque(_)) -> unknown;
+atom_vals(Other) ->
?atom(_) = Atm = t_inf(t_atom(), Other),
- t_atom_vals(Atm).
+ atom_vals(Atm).
-spec t_is_atom(erl_type()) -> boolean().
-t_is_atom(?atom(_)) -> true;
-t_is_atom(_) -> false.
+t_is_atom(Type) ->
+ t_is_atom(Type, 'universe').
+
+-spec t_is_atom(erl_type(), opaques()) -> boolean().
+
+t_is_atom(Type, Opaques) ->
+ do_opaque(Type, Opaques, fun is_atom1/1).
+
+is_atom1(?atom(_)) -> true;
+is_atom1(_) -> false.
--spec t_is_atom(atom(), erl_type()) -> boolean().
+-spec t_is_any_atom(atom(), erl_type()) -> boolean().
-t_is_atom(Atom, ?atom(?any)) when is_atom(Atom) -> false;
-t_is_atom(Atom, ?atom(Set)) when is_atom(Atom) -> set_is_singleton(Atom, Set);
-t_is_atom(Atom, _) when is_atom(Atom) -> false.
+t_is_any_atom(Atom, SomeAtomsType) ->
+ t_is_any_atom(Atom, SomeAtomsType, 'universe').
+
+-spec t_is_any_atom(atom(), erl_type(), opaques()) -> boolean().
+
+t_is_any_atom(Atom, SomeAtomsType, Opaques) ->
+ do_opaque(SomeAtomsType, Opaques,
+ fun(AtomsType) -> is_any_atom(Atom, AtomsType) end).
+
+is_any_atom(Atom, ?atom(?any)) when is_atom(Atom) -> false;
+is_any_atom(Atom, ?atom(Set)) when is_atom(Atom) ->
+ set_is_singleton(Atom, Set);
+is_any_atom(Atom, _) when is_atom(Atom) -> false.
%%------------------------------------
+-spec t_is_boolean(erl_type()) -> boolean().
+
+t_is_boolean(Type) ->
+ t_is_boolean(Type, 'universe').
+
+-spec t_is_boolean(erl_type(), opaques()) -> boolean().
+
+t_is_boolean(Type, Opaques) ->
+ do_opaque(Type, Opaques, fun is_boolean/1).
+
-spec t_boolean() -> erl_type().
t_boolean() ->
?atom(set_from_list([false, true])).
--spec t_is_boolean(erl_type()) -> boolean().
-
-t_is_boolean(?atom(?any)) -> false;
-t_is_boolean(?atom(Set)) ->
+is_boolean(?atom(?any)) -> false;
+is_boolean(?atom(Set)) ->
case set_size(Set) of
1 -> set_is_element(true, Set) orelse set_is_element(false, Set);
2 -> set_is_element(true, Set) andalso set_is_element(false, Set);
N when is_integer(N), N > 2 -> false
end;
-t_is_boolean(_) -> false.
+is_boolean(_) -> false.
%%-----------------------------------------------------------------------------
%% Binaries
@@ -873,9 +1061,17 @@ t_binary() ->
-spec t_is_binary(erl_type()) -> boolean().
-t_is_binary(?bitstr(U, B)) ->
+t_is_binary(Type) ->
+ t_is_binary(Type, 'universe').
+
+-spec t_is_binary(erl_type(), opaques()) -> boolean().
+
+t_is_binary(Type, Opaques) ->
+ do_opaque(Type, Opaques, fun is_binary/1).
+
+is_binary(?bitstr(U, B)) ->
((U rem 8) =:= 0) andalso ((B rem 8) =:= 0);
-t_is_binary(_) -> false.
+is_binary(_) -> false.
%%-----------------------------------------------------------------------------
%% Bitstrings
@@ -922,19 +1118,27 @@ t_bitstr_concat_1([], Acc) ->
t_bitstr_concat(T1, T2) ->
T1p = t_inf(t_bitstr(), T1),
T2p = t_inf(t_bitstr(), T2),
- bitstr_concat(T1p, T2p).
+ bitstr_concat(t_unopaque(T1p), t_unopaque(T2p)).
-spec t_bitstr_match(erl_type(), erl_type()) -> erl_type().
t_bitstr_match(T1, T2) ->
T1p = t_inf(t_bitstr(), T1),
T2p = t_inf(t_bitstr(), T2),
- bitstr_match(T1p, T2p).
+ bitstr_match(t_unopaque(T1p), t_unopaque(T2p)).
-spec t_is_bitstr(erl_type()) -> boolean().
-t_is_bitstr(?bitstr(_, _)) -> true;
-t_is_bitstr(_) -> false.
+t_is_bitstr(Type) ->
+ t_is_bitstr(Type, 'universe').
+
+-spec t_is_bitstr(erl_type(), opaques()) -> boolean().
+
+t_is_bitstr(Type, Opaques) ->
+ do_opaque(Type, Opaques, fun is_bitstr/1).
+
+is_bitstr(?bitstr(_, _)) -> true;
+is_bitstr(_) -> false.
%%-----------------------------------------------------------------------------
%% Matchstates
@@ -1045,27 +1249,59 @@ t_fun(Arity, Range) when is_integer(Arity), 0 =< Arity, Arity =< 255 ->
-spec t_fun_args(erl_type()) -> 'unknown' | [erl_type()].
-t_fun_args(?function(?any, _)) ->
+t_fun_args(Type) ->
+ t_fun_args(Type, 'universe').
+
+-spec t_fun_args(erl_type(), opaques()) -> 'unknown' | [erl_type()].
+
+t_fun_args(Type, Opaques) ->
+ do_opaque(Type, Opaques, fun fun_args/1).
+
+fun_args(?function(?any, _)) ->
unknown;
-t_fun_args(?function(?product(Domain), _)) when is_list(Domain) ->
+fun_args(?function(?product(Domain), _)) when is_list(Domain) ->
Domain.
-spec t_fun_arity(erl_type()) -> 'unknown' | non_neg_integer().
-t_fun_arity(?function(?any, _)) ->
+t_fun_arity(Type) ->
+ t_fun_arity(Type, 'universe').
+
+-spec t_fun_arity(erl_type(), opaques()) -> 'unknown' | non_neg_integer().
+
+t_fun_arity(Type, Opaques) ->
+ do_opaque(Type, Opaques, fun fun_arity/1).
+
+fun_arity(?function(?any, _)) ->
unknown;
-t_fun_arity(?function(?product(Domain), _)) ->
+fun_arity(?function(?product(Domain), _)) ->
length(Domain).
-spec t_fun_range(erl_type()) -> erl_type().
-t_fun_range(?function(_, Range)) ->
+t_fun_range(Type) ->
+ t_fun_range(Type, 'universe').
+
+-spec t_fun_range(erl_type(), opaques()) -> erl_type().
+
+t_fun_range(Type, Opaques) ->
+ do_opaque(Type, Opaques, fun fun_range/1).
+
+fun_range(?function(_, Range)) ->
Range.
-spec t_is_fun(erl_type()) -> boolean().
-t_is_fun(?function(_, _)) -> true;
-t_is_fun(_) -> false.
+t_is_fun(Type) ->
+ t_is_fun(Type, 'universe').
+
+-spec t_is_fun(erl_type(), opaques()) -> boolean().
+
+t_is_fun(Type, Opaques) ->
+ do_opaque(Type, Opaques, fun is_fun/1).
+
+is_fun(?function(_, _)) -> true;
+is_fun(_) -> false.
%%-----------------------------------------------------------------------------
%% Identifiers. Includes ports, pids and refs.
@@ -1092,9 +1328,17 @@ t_port() ->
-spec t_is_port(erl_type()) -> boolean().
-t_is_port(?identifier(?any)) -> false;
-t_is_port(?identifier(Set)) -> set_is_singleton(?port_qual, Set);
-t_is_port(_) -> false.
+t_is_port(Type) ->
+ t_is_port(Type, 'universe').
+
+-spec t_is_port(erl_type(), opaques()) -> boolean().
+
+t_is_port(Type, Opaques) ->
+ do_opaque(Type, Opaques, fun is_port1/1).
+
+is_port1(?identifier(?any)) -> false;
+is_port1(?identifier(Set)) -> set_is_singleton(?port_qual, Set);
+is_port1(_) -> false.
%%------------------------------------
@@ -1105,9 +1349,17 @@ t_pid() ->
-spec t_is_pid(erl_type()) -> boolean().
-t_is_pid(?identifier(?any)) -> false;
-t_is_pid(?identifier(Set)) -> set_is_singleton(?pid_qual, Set);
-t_is_pid(_) -> false.
+t_is_pid(Type) ->
+ t_is_pid(Type, 'universe').
+
+-spec t_is_pid(erl_type(), opaques()) -> boolean().
+
+t_is_pid(Type, Opaques) ->
+ do_opaque(Type, Opaques, fun is_pid1/1).
+
+is_pid1(?identifier(?any)) -> false;
+is_pid1(?identifier(Set)) -> set_is_singleton(?pid_qual, Set);
+is_pid1(_) -> false.
%%------------------------------------
@@ -1118,9 +1370,17 @@ t_reference() ->
-spec t_is_reference(erl_type()) -> boolean().
-t_is_reference(?identifier(?any)) -> false;
-t_is_reference(?identifier(Set)) -> set_is_singleton(?reference_qual, Set);
-t_is_reference(_) -> false.
+t_is_reference(Type) ->
+ t_is_reference(Type, 'universe').
+
+-spec t_is_reference(erl_type(), opaques()) -> boolean().
+
+t_is_reference(Type, Opaques) ->
+ do_opaque(Type, Opaques, fun is_reference1/1).
+
+is_reference1(?identifier(?any)) -> false;
+is_reference1(?identifier(Set)) -> set_is_singleton(?reference_qual, Set);
+is_reference1(_) -> false.
%%-----------------------------------------------------------------------------
%% Numbers are divided into floats, integers, chars and bytes.
@@ -1138,21 +1398,39 @@ t_number(X) when is_integer(X) ->
-spec t_is_number(erl_type()) -> boolean().
-t_is_number(?number(_, _)) -> true;
-t_is_number(_) -> false.
+t_is_number(Type) ->
+ t_is_number(Type, 'universe').
+
+-spec t_is_number(erl_type(), opaques()) -> boolean().
+
+t_is_number(Type, Opaques) ->
+ do_opaque(Type, Opaques, fun is_number/1).
+
+is_number(?number(_, _)) -> true;
+is_number(_) -> false.
%% Currently, the type system collapses all floats to ?float and does
%% not keep any information about their values. As a result, the list
%% that this function returns contains only integers.
+
-spec t_number_vals(erl_type()) -> 'unknown' | [integer(),...].
-t_number_vals(?int_set(?any)) -> unknown;
-t_number_vals(?int_set(Set)) -> set_to_list(Set);
-t_number_vals(?number(_, _)) -> unknown;
-t_number_vals(Other) ->
+t_number_vals(Type) ->
+ t_number_vals(Type, 'universe').
+
+-spec t_number_vals(erl_type(), opaques()) -> 'unknown' | [integer(),...].
+
+t_number_vals(Type, Opaques) ->
+ do_opaque(Type, Opaques, fun number_vals/1).
+
+number_vals(?int_set(?any)) -> unknown;
+number_vals(?int_set(Set)) -> set_to_list(Set);
+number_vals(?number(_, _)) -> unknown;
+number_vals(?opaque(_)) -> unknown;
+number_vals(Other) ->
Inf = t_inf(Other, t_number()),
false = t_is_none(Inf), % sanity check
- t_number_vals(Inf).
+ number_vals(Inf).
%%------------------------------------
@@ -1163,8 +1441,16 @@ t_float() ->
-spec t_is_float(erl_type()) -> boolean().
-t_is_float(?float) -> true;
-t_is_float(_) -> false.
+t_is_float(Type) ->
+ t_is_float(Type, 'universe').
+
+-spec t_is_float(erl_type(), opaques()) -> boolean().
+
+t_is_float(Type, Opaques) ->
+ do_opaque(Type, Opaques, fun is_float1/1).
+
+is_float1(?float) -> true;
+is_float1(_) -> false.
%%------------------------------------
@@ -1185,8 +1471,16 @@ t_integers(List) when is_list(List) ->
-spec t_is_integer(erl_type()) -> boolean().
-t_is_integer(?integer(_)) -> true;
-t_is_integer(_) -> false.
+t_is_integer(Type) ->
+ t_is_integer(Type, 'universe').
+
+-spec t_is_integer(erl_type(), opaques()) -> boolean().
+
+t_is_integer(Type, Opaques) ->
+ do_opaque(Type, Opaques, fun is_integer1/1).
+
+is_integer1(?integer(_)) -> true;
+is_integer1(_) -> false.
%%------------------------------------
@@ -1250,7 +1544,7 @@ t_cons(Hd, ?nil) ->
t_cons(Hd, ?list(Contents, Termination, _)) ->
?nonempty_list(t_sup(Contents, Hd), Termination);
t_cons(Hd, Tail) ->
- case t_inf(Tail, t_maybe_improper_list()) of
+ case cons_tail(t_inf(Tail, t_maybe_improper_list())) of
?list(Contents, Termination, _Size) ->
%% Collapse the list part of the termination but keep the
%% non-list part intact.
@@ -1262,18 +1556,45 @@ t_cons(Hd, Tail) ->
?unit -> ?none
end.
+cons_tail(Type) ->
+ do_opaque(Type, 'universe', fun(T) -> T end).
+
-spec t_is_cons(erl_type()) -> boolean().
-t_is_cons(?nonempty_list(_, _)) -> true;
-t_is_cons(_) -> false.
+t_is_cons(Type) ->
+ t_is_cons(Type, 'universe').
+
+-spec t_is_cons(erl_type(), opaques()) -> boolean().
+
+t_is_cons(Type, Opaques) ->
+ do_opaque(Type, Opaques, fun is_cons/1).
+
+is_cons(?nonempty_list(_, _)) -> true;
+is_cons(_) -> false.
-spec t_cons_hd(erl_type()) -> erl_type().
-t_cons_hd(?nonempty_list(Contents, _Termination)) -> Contents.
+t_cons_hd(Type) ->
+ t_cons_hd(Type, 'universe').
+
+-spec t_cons_hd(erl_type(), opaques()) -> erl_type().
+
+t_cons_hd(Type, Opaques) ->
+ do_opaque(Type, Opaques, fun cons_hd/1).
+
+cons_hd(?nonempty_list(Contents, _Termination)) -> Contents.
-spec t_cons_tl(erl_type()) -> erl_type().
-t_cons_tl(?nonempty_list(_Contents, Termination) = T) ->
+t_cons_tl(Type) ->
+ t_cons_tl(Type, 'universe').
+
+-spec t_cons_tl(erl_type(), opaques()) -> erl_type().
+
+t_cons_tl(Type, Opaques) ->
+ do_opaque(Type, Opaques, fun cons_tl/1).
+
+cons_tl(?nonempty_list(_Contents, Termination) = T) ->
t_sup(Termination, T).
-spec t_nil() -> erl_type().
@@ -1283,8 +1604,16 @@ t_nil() ->
-spec t_is_nil(erl_type()) -> boolean().
-t_is_nil(?nil) -> true;
-t_is_nil(_) -> false.
+t_is_nil(Type) ->
+ t_is_nil(Type, 'universe').
+
+-spec t_is_nil(erl_type(), opaques()) -> boolean().
+
+t_is_nil(Type, Opaques) ->
+ do_opaque(Type, Opaques, fun is_nil/1).
+
+is_nil(?nil) -> true;
+is_nil(_) -> false.
-spec t_list() -> erl_type().
@@ -1300,8 +1629,16 @@ t_list(Contents) ->
-spec t_list_elements(erl_type()) -> erl_type().
-t_list_elements(?list(Contents, _, _)) -> Contents;
-t_list_elements(?nil) -> ?none.
+t_list_elements(Type) ->
+ t_list_elements(Type, 'universe').
+
+-spec t_list_elements(erl_type(), opaques()) -> erl_type().
+
+t_list_elements(Type, Opaques) ->
+ do_opaque(Type, Opaques, fun list_elements/1).
+
+list_elements(?list(Contents, _, _)) -> Contents;
+list_elements(?nil) -> ?none.
-spec t_list_termination(erl_type()) -> erl_type().
@@ -1356,9 +1693,17 @@ t_maybe_improper_list(Content, Termination) ->
-spec t_is_maybe_improper_list(erl_type()) -> boolean().
-t_is_maybe_improper_list(?list(_, _, _)) -> true;
-t_is_maybe_improper_list(?nil) -> true;
-t_is_maybe_improper_list(_) -> false.
+t_is_maybe_improper_list(Type) ->
+ t_is_maybe_improper_list(Type, 'universe').
+
+-spec t_is_maybe_improper_list(erl_type(), opaques()) -> boolean().
+
+t_is_maybe_improper_list(Type, Opaques) ->
+ do_opaque(Type, Opaques, fun is_maybe_improper_list/1).
+
+is_maybe_improper_list(?list(_, _, _)) -> true;
+is_maybe_improper_list(?nil) -> true;
+is_maybe_improper_list(_) -> false.
%% %% Should only be used if you know what you are doing. See t_cons/2
%% -spec t_improper_list(erl_type(), erl_type()) -> erl_type().
@@ -1377,6 +1722,33 @@ lift_list_to_pos_empty(?list(Content, Termination, _)) ->
?list(Content, Termination, ?unknown_qual).
%%-----------------------------------------------------------------------------
+%% Maps
+%%
+
+-spec t_map() -> erl_type().
+
+t_map() ->
+ ?map([]).
+
+-spec t_map([{erl_type(), erl_type()}]) -> erl_type().
+
+t_map(_) ->
+ ?map([]).
+
+-spec t_is_map(erl_type()) -> boolean().
+
+t_is_map(Type) ->
+ t_is_map(Type, 'universe').
+
+-spec t_is_map(erl_type(), opaques()) -> boolean().
+
+t_is_map(Type, Opaques) ->
+ do_opaque(Type, Opaques, fun is_map1/1).
+
+is_map1(?map(_)) -> true;
+is_map1(_) -> false.
+
+%%-----------------------------------------------------------------------------
%% Tuples
%%
@@ -1405,32 +1777,77 @@ t_tuple(List) ->
-spec get_tuple_tags([erl_type()]) -> [erl_type(),...].
-get_tuple_tags([?atom(?any)|_]) -> [?any];
-get_tuple_tags([?atom(Set)|_]) ->
+get_tuple_tags([Tag|_]) ->
+ do_opaque(Tag, 'universe', fun tuple_tags/1);
+get_tuple_tags(_) -> [?any].
+
+tuple_tags(?atom(?any)) -> [?any];
+tuple_tags(?atom(Set)) ->
case set_size(Set) > ?TUPLE_TAG_LIMIT of
true -> [?any];
false -> [t_atom(A) || A <- set_to_list(Set)]
end;
-get_tuple_tags(_) -> [?any].
+tuple_tags(_) -> [?any].
%% to be used for a tuple with known types for its arguments (not ?any)
-spec t_tuple_args(erl_type()) -> [erl_type()].
-t_tuple_args(?tuple(Args, _, _)) when is_list(Args) -> Args.
+t_tuple_args(Type) ->
+ t_tuple_args(Type, 'universe').
+
+%% to be used for a tuple with known types for its arguments (not ?any)
+-spec t_tuple_args(erl_type(), opaques()) -> [erl_type()].
+
+t_tuple_args(Type, Opaques) ->
+ do_opaque(Type, Opaques, fun tuple_args/1).
+
+tuple_args(?tuple(Args, _, _)) when is_list(Args) -> Args.
%% to be used for a tuple with a known size (not ?any)
-spec t_tuple_size(erl_type()) -> non_neg_integer().
-t_tuple_size(?tuple(_, Size, _)) when is_integer(Size) -> Size.
+t_tuple_size(Type) ->
+ t_tuple_size(Type, 'universe').
+
+%% to be used for a tuple with a known size (not ?any)
+-spec t_tuple_size(erl_type(), opaques()) -> non_neg_integer().
+
+t_tuple_size(Type, Opaques) ->
+ do_opaque(Type, Opaques, fun tuple_size1/1).
+
+tuple_size1(?tuple(_, Size, _)) when is_integer(Size) -> Size.
-spec t_tuple_sizes(erl_type()) -> 'unknown' | [non_neg_integer(),...].
-t_tuple_sizes(?tuple(?any, ?any, ?any)) -> unknown;
-t_tuple_sizes(?tuple(_, Size, _)) when is_integer(Size) -> [Size];
-t_tuple_sizes(?tuple_set(List)) -> [Size || {Size, _} <- List].
+t_tuple_sizes(Type) ->
+ do_opaque(Type, 'universe', fun tuple_sizes/1).
+
+tuple_sizes(?tuple(?any, ?any, ?any)) -> unknown;
+tuple_sizes(?tuple(_, Size, _)) when is_integer(Size) -> [Size];
+tuple_sizes(?tuple_set(List)) -> [Size || {Size, _} <- List].
+
+-spec t_tuple_subtypes(erl_type(), opaques()) ->
+ 'unknown' | [erl_type(),...].
+
+t_tuple_subtypes(Type, Opaques) ->
+ Fun = fun(?tuple_set(List)) ->
+ t_tuple_subtypes_tuple_list(List, Opaques);
+ (?opaque(_)) -> unknown;
+ (T) -> t_tuple_subtypes(T)
+ end,
+ do_opaque(Type, Opaques, Fun).
+
+t_tuple_subtypes_tuple_list(List, Opaques) ->
+ lists:append([t_tuple_subtypes_list(Tuples, Opaques) ||
+ {_Size, Tuples} <- List]).
+
+t_tuple_subtypes_list(List, Opaques) ->
+ ListOfLists = [t_tuple_subtypes(E, Opaques) || E <- List, E =/= ?none],
+ lists:append([L || L <- ListOfLists, L =/= 'unknown']).
-spec t_tuple_subtypes(erl_type()) -> 'unknown' | [erl_type(),...].
+%% XXX. Not the same as t_tuple_subtypes(T, 'universe')...
t_tuple_subtypes(?tuple(?any, ?any, ?any)) -> unknown;
t_tuple_subtypes(?tuple(_, _, _) = T) -> [T];
t_tuple_subtypes(?tuple_set(List)) ->
@@ -1438,9 +1855,17 @@ t_tuple_subtypes(?tuple_set(List)) ->
-spec t_is_tuple(erl_type()) -> boolean().
-t_is_tuple(?tuple(_, _, _)) -> true;
-t_is_tuple(?tuple_set(_)) -> true;
-t_is_tuple(_) -> false.
+t_is_tuple(Type) ->
+ t_is_tuple(Type, 'universe').
+
+-spec t_is_tuple(erl_type(), opaques()) -> boolean().
+
+t_is_tuple(Type, Opaques) ->
+ do_opaque(Type, Opaques, fun is_tuple1/1).
+
+is_tuple1(?tuple(_, _, _)) -> true;
+is_tuple1(?tuple_set(_)) -> true;
+is_tuple1(_) -> false.
%%-----------------------------------------------------------------------------
%% Non-primitive types, including some handy syntactic sugar types
@@ -1451,6 +1876,7 @@ t_is_tuple(_) -> false.
t_bitstrlist() ->
t_iolist(1, t_bitstr()).
+%% XXX. To be removed.
-spec t_constant() -> erl_type().
t_constant() ->
@@ -1553,20 +1979,26 @@ t_timeout() ->
-spec t_array() -> erl_type().
t_array() ->
- t_opaque(array, array, [],
+ t_opaque(array, array, [t_any()],
t_tuple([t_atom('array'),
- t_non_neg_integer(), t_non_neg_integer(),
- t_any(), t_any()])).
+ t_sup([t_atom('undefined'), t_non_neg_integer()]),
+ t_sup([t_atom('undefined'), t_non_neg_integer()]),
+ t_any(),
+ t_any()])).
-spec t_dict() -> erl_type().
t_dict() ->
- t_opaque(dict, dict, [],
+ t_opaque(dict, dict, [t_any(), t_any()],
t_tuple([t_atom('dict'),
- t_non_neg_integer(), t_non_neg_integer(),
- t_non_neg_integer(), t_non_neg_integer(),
- t_non_neg_integer(), t_non_neg_integer(),
- t_tuple(), t_tuple()])).
+ t_sup([t_atom('undefined'), t_non_neg_integer()]),
+ t_sup([t_atom('undefined'), t_non_neg_integer()]),
+ t_sup([t_atom('undefined'), t_non_neg_integer()]),
+ t_sup([t_atom('undefined'), t_non_neg_integer()]),
+ t_sup([t_atom('undefined'), t_non_neg_integer()]),
+ t_sup([t_atom('undefined'), t_non_neg_integer()]),
+ t_sup([t_atom('undefined'), t_tuple()]),
+ t_sup([t_atom('undefined'), t_tuple()])])).
-spec t_digraph() -> erl_type().
@@ -1593,15 +2025,17 @@ t_gb_tree() ->
-spec t_queue() -> erl_type().
t_queue() ->
- t_opaque(queue, queue, [], t_tuple([t_list(), t_list()])).
+ t_opaque(queue, queue, [t_any()], t_tuple([t_list(), t_list()])).
-spec t_set() -> erl_type().
t_set() ->
- t_opaque(sets, set, [],
+ t_opaque(sets, set, [t_any()],
t_tuple([t_atom('set'), t_non_neg_integer(), t_non_neg_integer(),
t_pos_integer(), t_non_neg_integer(), t_non_neg_integer(),
- t_non_neg_integer(), t_tuple(), t_tuple()])).
+ t_non_neg_integer(),
+ t_sup([t_atom('undefined'), t_tuple()]),
+ t_sup([t_atom('undefined'), t_tuple()])])).
-spec t_tid() -> erl_type().
@@ -1614,18 +2048,6 @@ all_opaque_builtins() ->
[t_array(), t_dict(), t_digraph(), t_gb_set(),
t_gb_tree(), t_queue(), t_set(), t_tid()].
--spec is_opaque_builtin(atom(), atom()) -> boolean().
-
-is_opaque_builtin(array, array) -> true;
-is_opaque_builtin(dict, dict) -> true;
-is_opaque_builtin(digraph, digraph) -> true;
-is_opaque_builtin(gb_sets, gb_set) -> true;
-is_opaque_builtin(gb_trees, gb_tree) -> true;
-is_opaque_builtin(queue, queue) -> true;
-is_opaque_builtin(sets, set) -> true;
-is_opaque_builtin(ets, tid) -> true;
-is_opaque_builtin(_, _) -> false.
-
%%------------------------------------
%% ?none is allowed in products. A product of size 1 is not a product.
@@ -1673,8 +2095,13 @@ t_has_var(?tuple(Elements, _, _)) ->
t_has_var_list(Elements);
t_has_var(?tuple_set(_) = T) ->
t_has_var_list(t_tuple_subtypes(T));
-%% t_has_var(?union(_) = U) ->
-%% exit(lists:flatten(io_lib:format("Union happens in t_has_var/1 ~p\n",[U])));
+t_has_var(?map(_)= Map) ->
+ t_has_var_list(map_keys(Map)) orelse t_has_var_list(map_values(Map));
+t_has_var(?opaque(Set)) ->
+ %% Assume variables in 'args' are also present i 'struct'
+ t_has_var_list([O#opaque.struct || O <- set_to_list(Set)]);
+t_has_var(?union(List)) ->
+ t_has_var_list(List);
t_has_var(_) -> false.
-spec t_has_var_list([erl_type()]) -> boolean().
@@ -1697,17 +2124,28 @@ t_collect_vars(?function(Domain, Range), Acc) ->
t_collect_vars(?list(Contents, Termination, _), Acc) ->
ordsets:union(t_collect_vars(Contents, Acc), t_collect_vars(Termination, []));
t_collect_vars(?product(Types), Acc) ->
- lists:foldl(fun(T, TmpAcc) -> t_collect_vars(T, TmpAcc) end, Acc, Types);
+ t_collect_vars_list(Types, Acc);
t_collect_vars(?tuple(?any, ?any, ?any), Acc) ->
Acc;
t_collect_vars(?tuple(Types, _, _), Acc) ->
- lists:foldl(fun(T, TmpAcc) -> t_collect_vars(T, TmpAcc) end, Acc, Types);
+ t_collect_vars_list(Types, Acc);
t_collect_vars(?tuple_set(_) = TS, Acc) ->
- lists:foldl(fun(T, TmpAcc) -> t_collect_vars(T, TmpAcc) end, Acc,
- t_tuple_subtypes(TS));
+ t_collect_vars_list(t_tuple_subtypes(TS), Acc);
+t_collect_vars(?map(_) = Map, Acc0) ->
+ Acc = t_collect_vars_list(map_keys(Map), Acc0),
+ t_collect_vars_list(map_values(Map), Acc);
+t_collect_vars(?opaque(Set), Acc) ->
+ %% Assume variables in 'args' are also present i 'struct'
+ t_collect_vars_list([O#opaque.struct || O <- set_to_list(Set)], Acc);
+t_collect_vars(?union(List), Acc) ->
+ t_collect_vars_list(List, Acc);
t_collect_vars(_, Acc) ->
Acc.
+t_collect_vars_list([T|Ts], Acc0) ->
+ Acc = t_collect_vars(T, Acc0),
+ t_collect_vars_list(Ts, Acc);
+t_collect_vars_list([], Acc) -> Acc.
%%=============================================================================
%%
@@ -1730,6 +2168,7 @@ t_from_term(T) when is_function(T) ->
{arity, Arity} = erlang:fun_info(T, arity),
t_fun(Arity, t_any());
t_from_term(T) when is_integer(T) -> t_integer(T);
+t_from_term(T) when is_map(T) -> t_map();
t_from_term(T) when is_pid(T) -> t_pid();
t_from_term(T) when is_port(T) -> t_port();
t_from_term(T) when is_reference(T) -> t_reference();
@@ -1827,15 +2266,31 @@ t_is_bitwidth(_) -> false.
-spec number_min(erl_type()) -> rng_elem().
-number_min(?int_range(From, _)) -> From;
-number_min(?int_set(Set)) -> set_min(Set);
-number_min(?number(?any, _Tag)) -> neg_inf.
+number_min(Type) ->
+ number_min(Type, 'universe').
+
+-spec number_min(erl_type(), opaques()) -> rng_elem().
+
+number_min(Type, Opaques) ->
+ do_opaque(Type, Opaques, fun number_min2/1).
+
+number_min2(?int_range(From, _)) -> From;
+number_min2(?int_set(Set)) -> set_min(Set);
+number_min2(?number(?any, _Tag)) -> neg_inf.
-spec number_max(erl_type()) -> rng_elem().
-number_max(?int_range(_, To)) -> To;
-number_max(?int_set(Set)) -> set_max(Set);
-number_max(?number(?any, _Tag)) -> pos_inf.
+number_max(Type) ->
+ number_max(Type, 'universe').
+
+-spec number_max(erl_type(), opaques()) -> rng_elem().
+
+number_max(Type, Opaques) ->
+ do_opaque(Type, Opaques, fun number_max2/1).
+
+number_max2(?int_range(_, To)) -> To;
+number_max2(?int_set(Set)) -> set_max(Set);
+number_max2(?number(?any, _Tag)) -> pos_inf.
%% -spec int_range(rgn_elem(), rng_elem()) -> erl_type().
%%
@@ -1917,7 +2372,7 @@ t_sup(?function(Domain1, Range1), ?function(Domain2, Range2)) ->
t_sup(?identifier(Set1), ?identifier(Set2)) ->
?identifier(set_union(Set1, Set2));
t_sup(?opaque(Set1), ?opaque(Set2)) ->
- ?opaque(set_union_no_limit(Set1, Set2));
+ sup_opaque(set_to_list(ordsets:union(Set1, Set2)));
%%Disallow unions with opaque types
%%t_sup(T1=?opaque(_,_,_), T2) ->
%% io:format("Debug: t_sup executed with args ~w and ~w~n",[T1, T2]), ?none;
@@ -2005,6 +2460,27 @@ t_sup(T1, T2) ->
?union(U2) = force_union(T2),
sup_union(U1, U2).
+sup_opaque([]) -> ?none;
+sup_opaque(List) ->
+ L = sup_opaq(List),
+ ?opaque(ordsets:from_list(L)).
+
+sup_opaq(L0) ->
+ L1 = [{{Mod,Name,Args}, T} ||
+ #opaque{mod = Mod, name = Name, args = Args}=T <- L0],
+ F = family(L1),
+ [supl(Ts) || {_, Ts} <- F].
+
+supl([O]) -> O;
+supl(Ts) -> supl(Ts, t_none()).
+
+supl([#opaque{struct = S}=O|L], S0) ->
+ S1 = t_sup(S, S0),
+ case L =:= [] of
+ true -> O#opaque{struct = S1};
+ false -> supl(L, S1)
+ end.
+
-spec t_sup_lists([erl_type()], [erl_type()]) -> [erl_type()].
t_sup_lists([T1|Left1], [T2|Left2]) ->
@@ -2095,9 +2571,10 @@ force_union(T = ?function(_, _)) -> ?function_union(T);
force_union(T = ?identifier(_)) -> ?identifier_union(T);
force_union(T = ?list(_, _, _)) -> ?list_union(T);
force_union(T = ?nil) -> ?list_union(T);
-force_union(T = ?number(_,_)) -> ?number_union(T);
+force_union(T = ?number(_, _)) -> ?number_union(T);
force_union(T = ?opaque(_)) -> ?opaque_union(T);
force_union(T = ?remote(_)) -> ?remote_union(T);
+force_union(T = ?map(_)) -> ?map_union(T);
force_union(T = ?tuple(_, _, _)) -> ?tuple_union(T);
force_union(T = ?tuple_set(_)) -> ?tuple_union(T);
force_union(T = ?matchstate(_, _)) -> ?matchstate_union(T);
@@ -2132,19 +2609,27 @@ t_elements(?number(_, _) = T) ->
?int_set(Set) ->
[t_integer(I) || I <- Set]
end;
-t_elements(?opaque(_) = T) -> [T];
+t_elements(?opaque(_) = T) ->
+ do_elements(T);
+t_elements(?map(_) = T) -> [T];
t_elements(?tuple(_, _, _) = T) -> [T];
t_elements(?tuple_set(_) = TS) ->
case t_tuple_subtypes(TS) of
unknown -> [];
Elems -> Elems
end;
-t_elements(?union(List)) ->
- lists:append([t_elements(T) || T <- List]);
+t_elements(?union(_) = T) ->
+ do_elements(T);
t_elements(?var(_)) -> [?any]. %% yes, vars exist -- what else to do here?
%% t_elements(T) ->
%% io:format("T_ELEMENTS => ~p\n", [T]).
+do_elements(Type0) ->
+ case do_opaque(Type0, 'universe', fun(T) -> T end) of
+ ?union(List) -> lists:append([t_elements(T) || T <- List]);
+ Type -> t_elements(Type)
+ end.
+
%%-----------------------------------------------------------------------------
%% Infimum
%%
@@ -2162,74 +2647,77 @@ t_inf([]) -> ?none.
-spec t_inf(erl_type(), erl_type()) -> erl_type().
t_inf(T1, T2) ->
- t_inf(T1, T2, structured).
-
--type t_inf_mode() :: 'opaque' | 'structured'.
--spec t_inf(erl_type(), erl_type(), t_inf_mode()) -> erl_type().
-
-t_inf(?var(_), ?var(_), _Mode) -> ?any;
-t_inf(?var(_), T, _Mode) -> subst_all_vars_to_any(T);
-t_inf(T, ?var(_), _Mode) -> subst_all_vars_to_any(T);
-t_inf(?any, T, _Mode) -> subst_all_vars_to_any(T);
-t_inf(T, ?any, _Mode) -> subst_all_vars_to_any(T);
-t_inf(?none, _, _Mode) -> ?none;
-t_inf(_, ?none, _Mode) -> ?none;
-t_inf(?unit, _, _Mode) -> ?unit; % ?unit cases should appear below ?none
-t_inf(_, ?unit, _Mode) -> ?unit;
-t_inf(T, T, _Mode) -> subst_all_vars_to_any(T);
+ t_inf(T1, T2, 'universe').
+
+%% 'match' should be used from t_find_unknown_opaque() only
+-type t_inf_opaques() :: 'universe'
+ | [erl_type()] | {'match', [erl_type() | 'universe']}.
+
+-spec t_inf(erl_type(), erl_type(), t_inf_opaques()) -> erl_type().
+
+t_inf(?var(_), ?var(_), _Opaques) -> ?any;
+t_inf(?var(_), T, _Opaques) -> subst_all_vars_to_any(T);
+t_inf(T, ?var(_), _Opaques) -> subst_all_vars_to_any(T);
+t_inf(?any, T, _Opaques) -> subst_all_vars_to_any(T);
+t_inf(T, ?any, _Opaques) -> subst_all_vars_to_any(T);
+t_inf(?none, _, _Opaques) -> ?none;
+t_inf(_, ?none, _Opaques) -> ?none;
+t_inf(?unit, _, _Opaques) -> ?unit; % ?unit cases should appear below ?none
+t_inf(_, ?unit, _Opaques) -> ?unit;
+t_inf(T, T, _Opaques) -> subst_all_vars_to_any(T);
t_inf(?atom(Set1), ?atom(Set2), _) ->
case set_intersection(Set1, Set2) of
?none -> ?none;
NewSet -> ?atom(NewSet)
end;
-t_inf(?bitstr(U1, B1), ?bitstr(0, B2), _Mode) ->
+t_inf(?bitstr(U1, B1), ?bitstr(0, B2), _Opaques) ->
if B2 >= B1 andalso (B2-B1) rem U1 =:= 0 -> t_bitstr(0, B2);
true -> ?none
end;
-t_inf(?bitstr(0, B1), ?bitstr(U2, B2), _Mode) ->
+t_inf(?bitstr(0, B1), ?bitstr(U2, B2), _Opaques) ->
if B1 >= B2 andalso (B1-B2) rem U2 =:= 0 -> t_bitstr(0, B1);
true -> ?none
end;
-t_inf(?bitstr(U1, B1), ?bitstr(U1, B1), _Mode) ->
+t_inf(?bitstr(U1, B1), ?bitstr(U1, B1), _Opaques) ->
t_bitstr(U1, B1);
-t_inf(?bitstr(U1, B1), ?bitstr(U2, B2), _Mode) when U2 > U1 ->
+t_inf(?bitstr(U1, B1), ?bitstr(U2, B2), _Opaques) when U2 > U1 ->
inf_bitstr(U2, B2, U1, B1);
-t_inf(?bitstr(U1, B1), ?bitstr(U2, B2), _Mode) ->
+t_inf(?bitstr(U1, B1), ?bitstr(U2, B2), _Opaques) ->
inf_bitstr(U1, B1, U2, B2);
-t_inf(?function(Domain1, Range1), ?function(Domain2, Range2), Mode) ->
- case t_inf(Domain1, Domain2, Mode) of
+t_inf(?function(Domain1, Range1), ?function(Domain2, Range2), Opaques) ->
+ case t_inf(Domain1, Domain2, Opaques) of
?none -> ?none;
- Domain -> ?function(Domain, t_inf(Range1, Range2, Mode))
+ Domain -> ?function(Domain, t_inf(Range1, Range2, Opaques))
end;
-t_inf(?identifier(Set1), ?identifier(Set2), _Mode) ->
+t_inf(?identifier(Set1), ?identifier(Set2), _Opaques) ->
case set_intersection(Set1, Set2) of
?none -> ?none;
Set -> ?identifier(Set)
end;
-t_inf(?matchstate(Pres1, Slots1), ?matchstate(Pres2, Slots2), _Mode) ->
+t_inf(?matchstate(Pres1, Slots1), ?matchstate(Pres2, Slots2), _Opaques) ->
?matchstate(t_inf(Pres1, Pres2), t_inf(Slots1, Slots2));
-t_inf(?nil, ?nil, _Mode) -> ?nil;
-t_inf(?nil, ?nonempty_list(_, _), _Mode) ->
+t_inf(?nil, ?nil, _Opaques) -> ?nil;
+t_inf(?nil, ?nonempty_list(_, _), _Opaques) ->
?none;
-t_inf(?nonempty_list(_, _), ?nil, _Mode) ->
+t_inf(?nonempty_list(_, _), ?nil, _Opaques) ->
?none;
-t_inf(?nil, ?list(_Contents, Termination, _), Mode) ->
- t_inf(?nil, Termination, Mode);
-t_inf(?list(_Contents, Termination, _), ?nil, Mode) ->
- t_inf(?nil, Termination, Mode);
+t_inf(?nil, ?list(_Contents, Termination, _), Opaques) ->
+ t_inf(?nil, t_unopaque(Termination), Opaques);
+t_inf(?list(_Contents, Termination, _), ?nil, Opaques) ->
+ t_inf(?nil, t_unopaque(Termination), Opaques);
t_inf(?list(Contents1, Termination1, Size1),
- ?list(Contents2, Termination2, Size2), Mode) ->
- case t_inf(Termination1, Termination2, Mode) of
+ ?list(Contents2, Termination2, Size2), Opaques) ->
+ case t_inf(Termination1, Termination2, Opaques) of
?none -> ?none;
Termination ->
- case t_inf(Contents1, Contents2, Mode) of
- ?none ->
+ case t_inf(Contents1, Contents2, Opaques) of
+ ?none ->
%% If none of the lists are nonempty, then the infimum is nil.
case (Size1 =:= ?unknown_qual) andalso (Size2 =:= ?unknown_qual) of
true -> t_nil();
false -> ?none
end;
- Contents ->
+ Contents ->
Size =
case {Size1, Size2} of
{?unknown_qual, ?unknown_qual} -> ?unknown_qual;
@@ -2240,7 +2728,7 @@ t_inf(?list(Contents1, Termination1, Size1),
?list(Contents, Termination, Size)
end
end;
-t_inf(?number(_, _) = T1, ?number(_, _) = T2, _Mode) ->
+t_inf(?number(_, _) = T1, ?number(_, _) = T2, _Opaques) ->
case {T1, T2} of
{T, T} -> T;
{_, ?number(?any, ?unknown_qual)} -> T1;
@@ -2249,16 +2737,16 @@ t_inf(?number(_, _) = T1, ?number(_, _) = T2, _Mode) ->
{?integer(_), ?float} -> ?none;
{?integer(?any), ?integer(_)} -> T2;
{?integer(_), ?integer(?any)} -> T1;
- {?int_set(Set1), ?int_set(Set2)} ->
+ {?int_set(Set1), ?int_set(Set2)} ->
case set_intersection(Set1, Set2) of
?none -> ?none;
Set -> ?int_set(Set)
end;
- {?int_range(From1, To1), ?int_range(From2, To2)} ->
+ {?int_range(From1, To1), ?int_range(From2, To2)} ->
t_from_range(max(From1, From2), min(To1, To2));
{Range = ?int_range(_, _), ?int_set(Set)} ->
%% io:format("t_inf range, set args ~p ~p ~n", [T1, T2]),
- Ans2 =
+ Ans2 =
case set_filter(fun(X) -> in_range(X, Range) end, Set) of
?none -> ?none;
NewSet -> ?int_set(NewSet)
@@ -2271,198 +2759,271 @@ t_inf(?number(_, _) = T1, ?number(_, _) = T2, _Mode) ->
NewSet -> ?int_set(NewSet)
end
end;
-t_inf(?product(Types1), ?product(Types2), Mode) ->
+t_inf(?product(Types1), ?product(Types2), Opaques) ->
L1 = length(Types1),
L2 = length(Types2),
- if L1 =:= L2 -> ?product(t_inf_lists(Types1, Types2, Mode));
+ if L1 =:= L2 -> ?product(t_inf_lists(Types1, Types2, Opaques));
true -> ?none
end;
-t_inf(?product(_), _, _Mode) ->
+t_inf(?product(_), _, _Opaques) ->
?none;
-t_inf(_, ?product(_), _Mode) ->
+t_inf(_, ?product(_), _Opaques) ->
?none;
-t_inf(?tuple(?any, ?any, ?any), ?tuple(_, _, _) = T, _Mode) ->
+t_inf(?tuple(?any, ?any, ?any), ?tuple(_, _, _) = T, _Opaques) ->
subst_all_vars_to_any(T);
-t_inf(?tuple(_, _, _) = T, ?tuple(?any, ?any, ?any), _Mode) ->
+t_inf(?tuple(_, _, _) = T, ?tuple(?any, ?any, ?any), _Opaques) ->
subst_all_vars_to_any(T);
-t_inf(?tuple(?any, ?any, ?any), ?tuple_set(_) = T, _Mode) ->
+t_inf(?tuple(?any, ?any, ?any), ?tuple_set(_) = T, _Opaques) ->
subst_all_vars_to_any(T);
-t_inf(?tuple_set(_) = T, ?tuple(?any, ?any, ?any), _Mode) ->
+t_inf(?tuple_set(_) = T, ?tuple(?any, ?any, ?any), _Opaques) ->
subst_all_vars_to_any(T);
-t_inf(?tuple(Elements1, Arity, _Tag1), ?tuple(Elements2, Arity, _Tag2), Mode) ->
- case t_inf_lists_strict(Elements1, Elements2, Mode) of
+t_inf(?tuple(Elements1, Arity, _Tag1), ?tuple(Elements2, Arity, _Tag2), Opaques) ->
+ case t_inf_lists_strict(Elements1, Elements2, Opaques) of
bottom -> ?none;
NewElements -> t_tuple(NewElements)
end;
-t_inf(?tuple_set(List1), ?tuple_set(List2), Mode) ->
- inf_tuple_sets(List1, List2, Mode);
-t_inf(?tuple_set(List), ?tuple(_, Arity, _) = T, Mode) ->
- inf_tuple_sets(List, [{Arity, [T]}], Mode);
-t_inf(?tuple(_, Arity, _) = T, ?tuple_set(List), Mode) ->
- inf_tuple_sets(List, [{Arity, [T]}], Mode);
+t_inf(?tuple_set(List1), ?tuple_set(List2), Opaques) ->
+ inf_tuple_sets(List1, List2, Opaques);
+t_inf(?tuple_set(List), ?tuple(_, Arity, _) = T, Opaques) ->
+ inf_tuple_sets(List, [{Arity, [T]}], Opaques);
+t_inf(?tuple(_, Arity, _) = T, ?tuple_set(List), Opaques) ->
+ inf_tuple_sets(List, [{Arity, [T]}], Opaques);
%% be careful: here and in the next clause T can be ?opaque
-t_inf(?union(U1), T, Mode) ->
+t_inf(?union(U1), T, Opaques) ->
?union(U2) = force_union(T),
- inf_union(U1, U2, Mode);
-t_inf(T, ?union(U2), Mode) ->
+ inf_union(U1, U2, Opaques);
+t_inf(T, ?union(U2), Opaques) ->
?union(U1) = force_union(T),
- inf_union(U1, U2, Mode);
+ inf_union(U1, U2, Opaques);
+t_inf(?opaque(Set1), ?opaque(Set2), Opaques) ->
+ inf_opaque(Set1, Set2, Opaques);
+t_inf(?opaque(_) = T1, T2, Opaques) ->
+ inf_opaque1(T2, T1, 1, Opaques);
+t_inf(T1, ?opaque(_) = T2, Opaques) ->
+ inf_opaque1(T1, T2, 2, Opaques);
%% and as a result, the cases for ?opaque should appear *after* ?union
-t_inf(?opaque(Set1) = T1, ?opaque(Set2) = T2, Mode) ->
- case set_intersection(Set1, Set2) of
- ?none ->
- case Mode =:= opaque of
- true ->
- Struct1 = t_opaque_structure(T1),
- case t_inf(Struct1, T2) of
- ?none ->
- Struct2 = t_opaque_structure(T2),
- case t_inf(Struct2, T1) of
- ?none -> ?none;
- _ -> T2
- end;
- _ -> T1
- end;
- false -> ?none
- end;
- NewSet -> ?opaque(NewSet)
- end;
-t_inf(?opaque(_) = T1, T2, opaque) ->
- case t_inf(t_opaque_structure(T1), T2, structured) of
- ?none -> ?none;
- _Type -> T1
- end;
-t_inf(T1, ?opaque(_) = T2, opaque) ->
- case t_inf(T1, t_opaque_structure(T2), structured) of
- ?none -> ?none;
- _Type -> T2
- end;
t_inf(#c{}, #c{}, _) ->
?none.
+inf_opaque1(T1, ?opaque(Set2)=T2, Pos, Opaques) ->
+ case Opaques =:= 'universe' orelse inf_is_opaque_type(T2, Pos, Opaques) of
+ false -> ?none;
+ true ->
+ List2 = set_to_list(Set2),
+ case inf_collect(T1, List2, Opaques, []) of
+ [] -> ?none;
+ OpL -> ?opaque(ordsets:from_list(OpL))
+ end
+ end.
+
+inf_is_opaque_type(T, Pos, {match, Opaques}) ->
+ is_opaque_type(T, Opaques) orelse throw(Pos);
+inf_is_opaque_type(T, _Pos, Opaques) ->
+ is_opaque_type(T, Opaques).
+
+inf_collect(T1, [T2|List2], Opaques, OpL) ->
+ #opaque{struct = S2} = T2,
+ case t_inf(T1, S2, Opaques) of
+ ?none -> inf_collect(T1, List2, Opaques, OpL);
+ Inf ->
+ Op = T2#opaque{struct = Inf},
+ inf_collect(T1, List2, Opaques, [Op|OpL])
+ end;
+inf_collect(_T1, [], _Opaques, OpL) ->
+ OpL.
+
+combine(S, T1, T2) ->
+ #opaque{mod = Mod1, name = Name1, args = Args1} = T1,
+ #opaque{mod = Mod2, name = Name2, args = Args2} = T2,
+ case is_same_type_name({Mod1, Name1, Args1}, {Mod2, Name2, Args2}) of
+ true -> [comb(Mod1, Name1, Args1, S, T1)];
+ false -> [comb(Mod1, Name1, Args1, S, T1), comb(Mod2, Name2, Args2, S, T2)]
+ end.
+
+comb(Mod, Name, Args, S, T) ->
+ case is_same_name(Mod, Name, Args, S) of
+ true -> S;
+ false -> T#opaque{struct = S}
+ end.
+
+is_same_name(Mod1, Name1, Args1,
+ ?opaque([#opaque{mod = Mod2, name = Name2, args = Args2}])) ->
+ is_same_type_name({Mod1, Name1, Args1}, {Mod2, Name2, Args2});
+is_same_name(_, _, _, _) -> false.
+
+%% Combining two lists this way can be very time consuming...
+%% Note: two parameterized opaque types are not the same if their
+%% actual parameters differ
+inf_opaque(Set1, Set2, Opaques) ->
+ List1 = inf_look_up(Set1, 1, Opaques),
+ List2 = inf_look_up(Set2, 2, Opaques),
+ List0 = [combine(Inf, T1, T2) ||
+ {Is1, ModNameArgs1, T1} <- List1,
+ {Is2, ModNameArgs2, T2} <- List2,
+ not t_is_none(Inf = inf_opaque_types(Is1, ModNameArgs1, T1,
+ Is2, ModNameArgs2, T2,
+ Opaques))],
+ List = lists:sort(lists:append(List0)),
+ sup_opaque(List).
+
+%% Optimization: do just one lookup.
+inf_look_up(Set, Pos, Opaques) ->
+ [{Opaques =:= 'universe' orelse inf_is_opaque_type2(T, Pos, Opaques),
+ {M, N, Args}, T} ||
+ #opaque{mod = M, name = N, args = Args} = T <- set_to_list(Set)].
+
+inf_is_opaque_type2(T, Pos, {match, Opaques}) ->
+ is_opaque_type2(T, Opaques) orelse throw(Pos);
+inf_is_opaque_type2(T, _Pos, Opaques) ->
+ is_opaque_type2(T, Opaques).
+
+inf_opaque_types(IsOpaque1, ModNameArgs1, T1,
+ IsOpaque2, ModNameArgs2, T2, Opaques) ->
+ #opaque{struct = S1}=T1,
+ #opaque{struct = S2}=T2,
+ case
+ Opaques =:= 'universe' orelse
+ is_same_type_name(ModNameArgs1, ModNameArgs2)
+ of
+ true -> t_inf(S1, S2, Opaques);
+ false ->
+ case {IsOpaque1, IsOpaque2} of
+ {true, true} -> t_inf(S1, S2, Opaques);
+ {true, false} -> t_inf(S1, ?opaque(set_singleton(T2)), Opaques);
+ {false, true} -> t_inf(?opaque(set_singleton(T1)), S2, Opaques);
+ {false, false} -> t_none()
+ end
+ end.
+
-spec t_inf_lists([erl_type()], [erl_type()]) -> [erl_type()].
t_inf_lists(L1, L2) ->
- t_inf_lists(L1, L2, structured).
+ t_inf_lists(L1, L2, 'universe').
--spec t_inf_lists([erl_type()], [erl_type()], t_inf_mode()) -> [erl_type()].
+-spec t_inf_lists([erl_type()], [erl_type()], t_inf_opaques()) -> [erl_type()].
-t_inf_lists(L1, L2, Mode) ->
- t_inf_lists(L1, L2, [], Mode).
+t_inf_lists(L1, L2, Opaques) ->
+ t_inf_lists(L1, L2, [], Opaques).
--spec t_inf_lists([erl_type()], [erl_type()], [erl_type()], t_inf_mode()) -> [erl_type()].
+-spec t_inf_lists([erl_type()], [erl_type()], [erl_type()], [erl_type()]) -> [erl_type()].
-t_inf_lists([T1|Left1], [T2|Left2], Acc, Mode) ->
- t_inf_lists(Left1, Left2, [t_inf(T1, T2, Mode)|Acc], Mode);
-t_inf_lists([], [], Acc, _Mode) ->
+t_inf_lists([T1|Left1], [T2|Left2], Acc, Opaques) ->
+ t_inf_lists(Left1, Left2, [t_inf(T1, T2, Opaques)|Acc], Opaques);
+t_inf_lists([], [], Acc, _Opaques) ->
lists:reverse(Acc).
%% Infimum of lists with strictness.
%% If any element is the ?none type, the value 'bottom' is returned.
--spec t_inf_lists_strict([erl_type()], [erl_type()], t_inf_mode()) -> 'bottom' | [erl_type()].
+-spec t_inf_lists_strict([erl_type()], [erl_type()], [erl_type()]) -> 'bottom' | [erl_type()].
-t_inf_lists_strict(L1, L2, Mode) ->
- t_inf_lists_strict(L1, L2, [], Mode).
+t_inf_lists_strict(L1, L2, Opaques) ->
+ t_inf_lists_strict(L1, L2, [], Opaques).
--spec t_inf_lists_strict([erl_type()], [erl_type()], [erl_type()], t_inf_mode()) -> 'bottom' | [erl_type()].
+-spec t_inf_lists_strict([erl_type()], [erl_type()], [erl_type()], [erl_type()]) -> 'bottom' | [erl_type()].
-t_inf_lists_strict([T1|Left1], [T2|Left2], Acc, Mode) ->
- case t_inf(T1, T2, Mode) of
+t_inf_lists_strict([T1|Left1], [T2|Left2], Acc, Opaques) ->
+ case t_inf(T1, T2, Opaques) of
?none -> bottom;
- T -> t_inf_lists_strict(Left1, Left2, [T|Acc], Mode)
+ T -> t_inf_lists_strict(Left1, Left2, [T|Acc], Opaques)
end;
-t_inf_lists_strict([], [], Acc, _Mode) ->
+t_inf_lists_strict([], [], Acc, _Opaques) ->
lists:reverse(Acc).
--spec t_inf_lists_masked([erl_type()], [erl_type()], [t_inf_mode()]) -> [erl_type()].
-
-t_inf_lists_masked(List1, List2, Mask) ->
- List = lists:zip3(List1, List2, Mask),
- [t_inf(T1, T2, Mode) || {T1, T2, Mode} <- List].
-
-inf_tuple_sets(L1, L2, Mode) ->
- case inf_tuple_sets(L1, L2, [], Mode) of
+inf_tuple_sets(L1, L2, Opaques) ->
+ case inf_tuple_sets(L1, L2, [], Opaques) of
[] -> ?none;
[{_Arity, [?tuple(_, _, _) = OneTuple]}] -> OneTuple;
List -> ?tuple_set(List)
end.
-inf_tuple_sets([{Arity, Tuples1}|Ts1], [{Arity, Tuples2}|Ts2], Acc, Mode) ->
- case inf_tuples_in_sets(Tuples1, Tuples2, Mode) of
- [] -> inf_tuple_sets(Ts1, Ts2, Acc, Mode);
+inf_tuple_sets([{Arity, Tuples1}|Ts1], [{Arity, Tuples2}|Ts2], Acc, Opaques) ->
+ case inf_tuples_in_sets(Tuples1, Tuples2, Opaques) of
+ [] -> inf_tuple_sets(Ts1, Ts2, Acc, Opaques);
[?tuple_set([{Arity, NewTuples}])] ->
- inf_tuple_sets(Ts1, Ts2, [{Arity, NewTuples}|Acc], Mode);
- NewTuples -> inf_tuple_sets(Ts1, Ts2, [{Arity, NewTuples}|Acc], Mode)
+ inf_tuple_sets(Ts1, Ts2, [{Arity, NewTuples}|Acc], Opaques);
+ NewTuples -> inf_tuple_sets(Ts1, Ts2, [{Arity, NewTuples}|Acc], Opaques)
end;
-inf_tuple_sets([{Arity1, _}|Ts1] = L1, [{Arity2, _}|Ts2] = L2, Acc, Mode) ->
- if Arity1 < Arity2 -> inf_tuple_sets(Ts1, L2, Acc, Mode);
- Arity1 > Arity2 -> inf_tuple_sets(L1, Ts2, Acc, Mode)
+inf_tuple_sets([{Arity1, _}|Ts1] = L1, [{Arity2, _}|Ts2] = L2, Acc, Opaques) ->
+ if Arity1 < Arity2 -> inf_tuple_sets(Ts1, L2, Acc, Opaques);
+ Arity1 > Arity2 -> inf_tuple_sets(L1, Ts2, Acc, Opaques)
end;
-inf_tuple_sets([], _, Acc, _Mode) -> lists:reverse(Acc);
-inf_tuple_sets(_, [], Acc, _Mode) -> lists:reverse(Acc).
-
-inf_tuples_in_sets([?tuple(Elements1, _, ?any)], L2, Mode) ->
- NewList = [t_inf_lists_strict(Elements1, Elements2, Mode)
+inf_tuple_sets([], _, Acc, _Opaques) -> lists:reverse(Acc);
+inf_tuple_sets(_, [], Acc, _Opaques) -> lists:reverse(Acc).
+
+inf_tuples_in_sets([?tuple(Elements1, _, ?any)], L2, Opaques) ->
+ NewList = [t_inf_lists_strict(Elements1, Elements2, Opaques)
|| ?tuple(Elements2, _, _) <- L2],
[t_tuple(Es) || Es <- NewList, Es =/= bottom];
-inf_tuples_in_sets(L1, [?tuple(Elements2, _, ?any)], Mode) ->
- NewList = [t_inf_lists_strict(Elements1, Elements2, Mode)
+inf_tuples_in_sets(L1, [?tuple(Elements2, _, ?any)], Opaques) ->
+ NewList = [t_inf_lists_strict(Elements1, Elements2, Opaques)
|| ?tuple(Elements1, _, _) <- L1],
[t_tuple(Es) || Es <- NewList, Es =/= bottom];
-inf_tuples_in_sets(L1, L2, Mode) ->
- inf_tuples_in_sets(L1, L2, [], Mode).
+inf_tuples_in_sets(L1, L2, Opaques) ->
+ inf_tuples_in_sets2(L1, L2, [], Opaques).
-inf_tuples_in_sets([?tuple(Elements1, Arity, Tag)|Ts1],
- [?tuple(Elements2, Arity, Tag)|Ts2], Acc, Mode) ->
- case t_inf_lists_strict(Elements1, Elements2, Mode) of
- bottom -> inf_tuples_in_sets(Ts1, Ts2, Acc, Mode);
+inf_tuples_in_sets2([?tuple(Elements1, Arity, Tag)|Ts1],
+ [?tuple(Elements2, Arity, Tag)|Ts2], Acc, Opaques) ->
+ case t_inf_lists_strict(Elements1, Elements2, Opaques) of
+ bottom -> inf_tuples_in_sets2(Ts1, Ts2, Acc, Opaques);
NewElements ->
- inf_tuples_in_sets(Ts1, Ts2, [?tuple(NewElements, Arity, Tag)|Acc], Mode)
+ inf_tuples_in_sets2(Ts1, Ts2, [?tuple(NewElements, Arity, Tag)|Acc],
+ Opaques)
end;
-inf_tuples_in_sets([?tuple(_, _, Tag1)|Ts1] = L1,
- [?tuple(_, _, Tag2)|Ts2] = L2, Acc, Mode) ->
- if Tag1 < Tag2 -> inf_tuples_in_sets(Ts1, L2, Acc, Mode);
- Tag1 > Tag2 -> inf_tuples_in_sets(L1, Ts2, Acc, Mode)
+inf_tuples_in_sets2([?tuple(_, _, Tag1)|Ts1] = L1,
+ [?tuple(_, _, Tag2)|Ts2] = L2, Acc, Opaques) ->
+ if Tag1 < Tag2 -> inf_tuples_in_sets2(Ts1, L2, Acc, Opaques);
+ Tag1 > Tag2 -> inf_tuples_in_sets2(L1, Ts2, Acc, Opaques)
end;
-inf_tuples_in_sets([], _, Acc, _Mode) -> lists:reverse(Acc);
-inf_tuples_in_sets(_, [], Acc, _Mode) -> lists:reverse(Acc).
-
-inf_union(U1, U2, opaque) ->
-%%---------------------------------------------------------------------
-%% Under Testing
-%%----------------------------------------------------------------------
-%% OpaqueFun =
-%% fun(Union1, Union2) ->
-%% [_,_,_,_,_,_,_,_,Opaque,_] = Union1,
-%% [A,B,F,I,L,N,T,M,_,_R] = Union2,
-%% List = [A,B,F,I,L,N,T,M],
-%% case [T || T <- List, t_inf(T, Opaque, opaque) =/= ?none] of
-%% [] -> ?none;
-%% _ -> Opaque
-%% end
-%% end,
-%% O1 = OpaqueFun(U1, U2),
-%% O2 = OpaqueFun(U2, U1),
-%% Union = inf_union(U1, U2, 0, [], opaque),
-%% t_sup([O1, O2, Union]);
- inf_union(U1, U2, 0, [], opaque);
-inf_union(U1, U2, OtherMode) ->
- inf_union(U1, U2, 0, [], OtherMode).
-
-inf_union([?none|Left1], [?none|Left2], N, Acc, Mode) ->
- inf_union(Left1, Left2, N, [?none|Acc], Mode);
-inf_union([T1|Left1], [T2|Left2], N, Acc, Mode) ->
- case t_inf(T1, T2, Mode) of
- ?none -> inf_union(Left1, Left2, N, [?none|Acc], Mode);
- T -> inf_union(Left1, Left2, N+1, [T|Acc], Mode)
+inf_tuples_in_sets2([], _, Acc, _Opaques) -> lists:reverse(Acc);
+inf_tuples_in_sets2(_, [], Acc, _Opaques) -> lists:reverse(Acc).
+
+inf_union(U1, U2, Opaques) ->
+ OpaqueFun =
+ fun(Union1, Union2, InfFun) ->
+ [_,_,_,_,_,_,_,_,Opaque,_,_] = Union1,
+ [A,B,F,I,L,N,T,M,_,_R,Map] = Union2,
+ List = [A,B,F,I,L,N,T,M,Map],
+ inf_union_collect(List, Opaque, InfFun, [], [])
+ end,
+ {O1, ThrowList1} =
+ OpaqueFun(U1, U2, fun(E, Opaque) -> t_inf(Opaque, E, Opaques) end),
+ {O2, ThrowList2}
+ = OpaqueFun(U2, U1, fun(E, Opaque) -> t_inf(E, Opaque, Opaques) end),
+ {Union, ThrowList3} = inf_union(U1, U2, 0, [], [], Opaques),
+ ThrowList = lists:merge3(ThrowList1, ThrowList2, ThrowList3),
+ case t_sup([O1, O2, Union]) of
+ ?none when ThrowList =/= [] -> throw(hd(ThrowList));
+ Sup -> Sup
+ end.
+
+inf_union_collect([], _Opaque, _InfFun, InfList, ThrowList) ->
+ {t_sup(InfList), lists:usort(ThrowList)};
+inf_union_collect([?none|L], Opaque, InfFun, InfList, ThrowList) ->
+ inf_union_collect(L, Opaque, InfFun, [?none|InfList], ThrowList);
+inf_union_collect([E|L], Opaque, InfFun, InfList, ThrowList) ->
+ try InfFun(E, Opaque)of
+ Inf ->
+ inf_union_collect(L, Opaque, InfFun, [Inf|InfList], ThrowList)
+ catch throw:N when is_integer(N) ->
+ inf_union_collect(L, Opaque, InfFun, InfList, [N|ThrowList])
+ end.
+
+inf_union([?none|Left1], [?none|Left2], N, Acc, ThrowList, Opaques) ->
+ inf_union(Left1, Left2, N, [?none|Acc], ThrowList, Opaques);
+inf_union([T1|Left1], [T2|Left2], N, Acc, ThrowList, Opaques) ->
+ try t_inf(T1, T2, Opaques) of
+ ?none -> inf_union(Left1, Left2, N, [?none|Acc], ThrowList, Opaques);
+ T -> inf_union(Left1, Left2, N+1, [T|Acc], ThrowList, Opaques)
+ catch throw:N when is_integer(N) ->
+ inf_union(Left1, Left2, N, [?none|Acc], [N|ThrowList], Opaques)
end;
-inf_union([], [], N, Acc, _Mode) ->
- if N =:= 0 -> ?none;
+inf_union([], [], N, Acc, ThrowList, _Opaques) ->
+ if N =:= 0 -> {?none, ThrowList};
N =:= 1 ->
[Type] = [T || T <- Acc, T =/= ?none],
- Type;
- N >= 2 -> ?union(lists:reverse(Acc))
+ {Type, ThrowList};
+ N >= 2 -> {?union(lists:reverse(Acc)), ThrowList}
end.
inf_bitstr(U1, B1, U2, B2) ->
@@ -2494,13 +3055,13 @@ findfirst(N1, N2, U1, B1, U2, B2) ->
%% to types. Hans Bolinder suggested the use of lists of Key-Value pairs for
%% this data structure and measurements showed a non-trivial speedup when using
%% them for operations within this module (e.g. in t_unify/2). However, there
-%% is code outside erl_types that still passes a dict() in the 2nd argument.
+%% is code outside erl_types that still passes a dict:dict() in the 2nd argument.
%% So, for the time being, this module provides a t_subst/2 function for these
%% external calls and a clone of it (t_subst_kv/2) which is used from all calls
%% from within this module. This code duplication needs to be eliminated at
%% some point.
--spec t_subst(erl_type(), dict()) -> erl_type().
+-spec t_subst(erl_type(), dict:dict(atom(), erl_type())) -> erl_type().
t_subst(T, Dict) ->
case t_has_var(T) of
@@ -2536,6 +3097,16 @@ t_subst_dict(?tuple(Elements, _Arity, _Tag), Dict) ->
t_tuple([t_subst_dict(E, Dict) || E <- Elements]);
t_subst_dict(?tuple_set(_) = TS, Dict) ->
t_sup([t_subst_dict(T, Dict) || T <- t_tuple_subtypes(TS)]);
+t_subst_dict(?map(Pairs), Dict) ->
+ ?map([{t_subst_dict(K, Dict), t_subst_dict(V, Dict)} ||
+ {K, V} <- Pairs]);
+t_subst_dict(?opaque(Es), Dict) ->
+ List = [Opaque#opaque{args = [t_subst_dict(Arg, Dict) || Arg <- Args],
+ struct = t_subst_dict(S, Dict)} ||
+ Opaque = #opaque{args = Args, struct = S} <- set_to_list(Es)],
+ ?opaque(ordsets:from_list(List));
+t_subst_dict(?union(List), Dict) ->
+ ?union([t_subst_dict(E, Dict) || E <- List]);
t_subst_dict(T, _Dict) ->
T.
@@ -2578,9 +3149,31 @@ t_subst_aux(?tuple(Elements, _Arity, _Tag), VarMap) ->
t_tuple([t_subst_aux(E, VarMap) || E <- Elements]);
t_subst_aux(?tuple_set(_) = TS, VarMap) ->
t_sup([t_subst_aux(T, VarMap) || T <- t_tuple_subtypes(TS)]);
+t_subst_aux(?map(Pairs), VarMap) ->
+ ?map([{t_subst_aux(K, VarMap), t_subst_aux(V, VarMap)} ||
+ {K, V} <- Pairs]);
+t_subst_aux(?opaque(Es), VarMap) ->
+ List = [Opaque#opaque{args = [t_subst_aux(Arg, VarMap) || Arg <- Args],
+ struct = t_subst_aux(S, VarMap)} ||
+ Opaque = #opaque{args = Args, struct = S} <- set_to_list(Es)],
+ ?opaque(ordsets:from_list(List));
+t_subst_aux(?union(List), VarMap) ->
+ ?union([t_subst_aux(E, VarMap) || E <- List]);
t_subst_aux(T, _VarMap) ->
T.
+-spec subst_all_remote(erl_type(), erl_type()) -> erl_type().
+
+subst_all_remote(Type0, Substitute) ->
+ Map =
+ fun(Type) ->
+ case erl_types:t_is_remote(Type) of
+ true -> Substitute;
+ false -> Type
+ end
+ end,
+ erl_types:t_map(Map, Type0).
+
%%-----------------------------------------------------------------------------
%% Unification
%%
@@ -2590,112 +3183,152 @@ t_subst_aux(T, _VarMap) ->
-spec t_unify(erl_type(), erl_type()) -> t_unify_ret().
t_unify(T1, T2) ->
- t_unify(T1, T2, []).
-
--spec t_unify(erl_type(), erl_type(), [erl_type()]) -> t_unify_ret().
-
-t_unify(T1, T2, Opaques) ->
- {T, VarMap} = t_unify(T1, T2, [], Opaques),
+ {T, VarMap} = t_unify(T1, T2, []),
{t_subst_kv(T, VarMap), lists:keysort(1, VarMap)}.
-t_unify(?var(Id) = T, ?var(Id), VarMap, _Opaques) ->
+t_unify(?var(Id) = T, ?var(Id), VarMap) ->
{T, VarMap};
-t_unify(?var(Id1) = T, ?var(Id2), VarMap, Opaques) ->
+t_unify(?var(Id1) = T, ?var(Id2), VarMap) ->
case lists:keyfind(Id1, 1, VarMap) of
false ->
case lists:keyfind(Id2, 1, VarMap) of
false -> {T, [{Id2, T} | VarMap]};
- {Id2, Type} -> t_unify(T, Type, VarMap, Opaques)
+ {Id2, Type} -> t_unify(T, Type, VarMap)
end;
{Id1, Type1} ->
case lists:keyfind(Id2, 1, VarMap) of
false -> {Type1, [{Id2, T} | VarMap]};
- {Id2, Type2} -> t_unify(Type1, Type2, VarMap, Opaques)
+ {Id2, Type2} -> t_unify(Type1, Type2, VarMap)
end
end;
-t_unify(?var(Id), Type, VarMap, Opaques) ->
+t_unify(?var(Id), Type, VarMap) ->
case lists:keyfind(Id, 1, VarMap) of
false -> {Type, [{Id, Type} | VarMap]};
- {Id, VarType} -> t_unify(VarType, Type, VarMap, Opaques)
+ {Id, VarType} -> t_unify(VarType, Type, VarMap)
end;
-t_unify(Type, ?var(Id), VarMap, Opaques) ->
+t_unify(Type, ?var(Id), VarMap) ->
case lists:keyfind(Id, 1, VarMap) of
false -> {Type, [{Id, Type} | VarMap]};
- {Id, VarType} -> t_unify(VarType, Type, VarMap, Opaques)
+ {Id, VarType} -> t_unify(VarType, Type, VarMap)
end;
-t_unify(?function(Domain1, Range1), ?function(Domain2, Range2), VarMap, Opaques) ->
- {Domain, VarMap1} = t_unify(Domain1, Domain2, VarMap, Opaques),
- {Range, VarMap2} = t_unify(Range1, Range2, VarMap1, Opaques),
+t_unify(?function(Domain1, Range1), ?function(Domain2, Range2), VarMap) ->
+ {Domain, VarMap1} = t_unify(Domain1, Domain2, VarMap),
+ {Range, VarMap2} = t_unify(Range1, Range2, VarMap1),
{?function(Domain, Range), VarMap2};
t_unify(?list(Contents1, Termination1, Size),
- ?list(Contents2, Termination2, Size), VarMap, Opaques) ->
- {Contents, VarMap1} = t_unify(Contents1, Contents2, VarMap, Opaques),
- {Termination, VarMap2} = t_unify(Termination1, Termination2, VarMap1, Opaques),
+ ?list(Contents2, Termination2, Size), VarMap) ->
+ {Contents, VarMap1} = t_unify(Contents1, Contents2, VarMap),
+ {Termination, VarMap2} = t_unify(Termination1, Termination2, VarMap1),
{?list(Contents, Termination, Size), VarMap2};
-t_unify(?product(Types1), ?product(Types2), VarMap, Opaques) ->
- {Types, VarMap1} = unify_lists(Types1, Types2, VarMap, Opaques),
+t_unify(?product(Types1), ?product(Types2), VarMap) ->
+ {Types, VarMap1} = unify_lists(Types1, Types2, VarMap),
{?product(Types), VarMap1};
-t_unify(?tuple(?any, ?any, ?any) = T, ?tuple(?any, ?any, ?any), VarMap, _Opaques) ->
+t_unify(?tuple(?any, ?any, ?any) = T, ?tuple(?any, ?any, ?any), VarMap) ->
{T, VarMap};
t_unify(?tuple(Elements1, Arity, _),
- ?tuple(Elements2, Arity, _), VarMap, Opaques) when Arity =/= ?any ->
- {NewElements, VarMap1} = unify_lists(Elements1, Elements2, VarMap, Opaques),
+ ?tuple(Elements2, Arity, _), VarMap) when Arity =/= ?any ->
+ {NewElements, VarMap1} = unify_lists(Elements1, Elements2, VarMap),
{t_tuple(NewElements), VarMap1};
t_unify(?tuple_set([{Arity, _}]) = T1,
- ?tuple(_, Arity, _) = T2, VarMap, Opaques) when Arity =/= ?any ->
- unify_tuple_set_and_tuple(T1, T2, VarMap, Opaques);
+ ?tuple(_, Arity, _) = T2, VarMap) when Arity =/= ?any ->
+ unify_tuple_set_and_tuple1(T1, T2, VarMap);
t_unify(?tuple(_, Arity, _) = T1,
- ?tuple_set([{Arity, _}]) = T2, VarMap, Opaques) when Arity =/= ?any ->
- unify_tuple_set_and_tuple(T2, T1, VarMap, Opaques);
-t_unify(?tuple_set(List1), ?tuple_set(List2), VarMap, Opaques) ->
- {Tuples, NewVarMap} =
- unify_lists(lists:append([T || {_Arity, T} <- List1]),
- lists:append([T || {_Arity, T} <- List2]), VarMap, Opaques),
- {t_sup(Tuples), NewVarMap};
-t_unify(?opaque(Elements) = T, ?opaque(Elements), VarMap, _Opaques) ->
- {T, VarMap};
-t_unify(?opaque(_) = T1, ?opaque(_) = T2, _VarMap, _Opaques) ->
- throw({mismatch, T1, T2});
-t_unify(Type, ?opaque(_) = OpType, VarMap, Opaques) ->
- t_unify_with_opaque(Type, OpType, VarMap, Opaques);
-t_unify(?opaque(_) = OpType, Type, VarMap, Opaques) ->
- t_unify_with_opaque(Type, OpType, VarMap, Opaques);
-t_unify(T, T, VarMap, _Opaques) ->
+ ?tuple_set([{Arity, _}]) = T2, VarMap) when Arity =/= ?any ->
+ unify_tuple_set_and_tuple2(T1, T2, VarMap);
+t_unify(?tuple_set(List1) = T1, ?tuple_set(List2) = T2, VarMap) ->
+ try
+ unify_lists(lists:append([T || {_Arity, T} <- List1]),
+ lists:append([T || {_Arity, T} <- List2]), VarMap)
+ of
+ {Tuples, NewVarMap} -> {t_sup(Tuples), NewVarMap}
+ catch _:_ -> throw({mismatch, T1, T2})
+ end;
+t_unify(?opaque(_) = T1, ?opaque(_) = T2, VarMap) ->
+ t_unify(t_opaque_structure(T1), t_opaque_structure(T2), VarMap);
+t_unify(T1, ?opaque(_) = T2, VarMap) ->
+ t_unify(T1, t_opaque_structure(T2), VarMap);
+t_unify(?opaque(_) = T1, T2, VarMap) ->
+ t_unify(t_opaque_structure(T1), T2, VarMap);
+t_unify(T, T, VarMap) ->
{T, VarMap};
-t_unify(T1, T2, _, _) ->
+t_unify(?union(_)=T1, ?union(_)=T2, VarMap) ->
+ {Type1, Type2} = unify_union2(T1, T2),
+ t_unify(Type1, Type2, VarMap);
+t_unify(?union(_)=T1, T2, VarMap) ->
+ t_unify(unify_union1(T1, T1, T2), T2, VarMap);
+t_unify(T1, ?union(_)=T2, VarMap) ->
+ t_unify(T1, unify_union1(T2, T1, T2), VarMap);
+t_unify(T1, T2, _) ->
throw({mismatch, T1, T2}).
-t_unify_with_opaque(Type, OpType, VarMap, Opaques) ->
- case lists:member(OpType, Opaques) of
+unify_union2(?union(List1)=T1, ?union(List2)=T2) ->
+ case {unify_union(List1), unify_union(List2)} of
+ {{yes, Type1}, {yes, Type2}} -> {Type1, Type2};
+ {{yes, Type1}, no} -> {Type1, T2};
+ {no, {yes, Type2}} -> {T1, Type2};
+ {no, no} -> throw({mismatch, T1, T2})
+ end.
+
+unify_union1(?union(List), T1, T2) ->
+ case unify_union(List) of
+ {yes, Type} -> Type;
+ no -> throw({mismatch, T1, T2})
+ end.
+
+unify_union(List) ->
+ [A,B,F,I,L,N,T,M,O,R,Map] = List,
+ if O =:= ?none -> no;
true ->
- Struct = t_opaque_structure(OpType),
- try t_unify(Type, Struct, VarMap, Opaques) of
- {_T, VarMap1} -> {OpType, VarMap1}
- catch
- throw:{mismatch, _T1, _T2} ->
- case t_inf(OpType, Type, opaque) of
- ?none -> throw({mismatch, Type, OpType});
- _ -> {OpType, VarMap}
- end
- end;
- false ->
- throw({mismatch, Type, OpType})
+ S = t_opaque_structure(O),
+ {yes, t_sup([A,B,F,I,L,N,T,M,S,R,Map])}
end.
-unify_tuple_set_and_tuple(?tuple_set([{Arity, List}]),
- ?tuple(Elements2, Arity, _), VarMap, Opaques) ->
+-spec is_opaque_type(erl_type(), [erl_type()]) -> boolean().
+
+%% An opaque type is a union of types. Returns true iff any of the type
+%% names (Module and Name) of the first argument (the opaque type to
+%% check) occurs in any of the opaque types of the second argument.
+is_opaque_type(?opaque(Elements), Opaques) ->
+ lists:any(fun(Opaque) -> is_opaque_type2(Opaque, Opaques) end, Elements).
+
+is_opaque_type2(#opaque{mod = Mod1, name = Name1, args = Args1}, Opaques) ->
+ F1 = fun(?opaque(Es)) ->
+ F2 = fun(#opaque{mod = Mod, name = Name, args = Args}) ->
+ is_type_name(Mod1, Name1, Args1, Mod, Name, Args)
+ end,
+ lists:any(F2, Es)
+ end,
+ lists:any(F1, Opaques).
+
+is_type_name(Mod, Name, Args1, Mod, Name, Args2) ->
+ length(Args1) =:= length(Args2);
+is_type_name(Mod1, Name1, Args1, Mod2, Name2, Args2) ->
+ is_same_type_name2(Mod1, Name1, Args1, Mod2, Name2, Args2).
+
+%% Two functions since t_unify is not symmetric.
+unify_tuple_set_and_tuple1(?tuple_set([{Arity, List}]),
+ ?tuple(Elements2, Arity, _), VarMap) ->
+ %% Can only work if the single tuple has variables at correct places.
+ %% Collapse the tuple set.
+ {NewElements, VarMap1} =
+ unify_lists(sup_tuple_elements(List), Elements2, VarMap),
+ {t_tuple(NewElements), VarMap1}.
+
+unify_tuple_set_and_tuple2(?tuple(Elements2, Arity, _),
+ ?tuple_set([{Arity, List}]), VarMap) ->
%% Can only work if the single tuple has variables at correct places.
%% Collapse the tuple set.
- {NewElements, VarMap1} = unify_lists(sup_tuple_elements(List), Elements2, VarMap, Opaques),
+ {NewElements, VarMap1} =
+ unify_lists(Elements2, sup_tuple_elements(List), VarMap),
{t_tuple(NewElements), VarMap1}.
-unify_lists(L1, L2, VarMap, Opaques) ->
- unify_lists(L1, L2, VarMap, [], Opaques).
+unify_lists(L1, L2, VarMap) ->
+ unify_lists(L1, L2, VarMap, []).
-unify_lists([T1|Left1], [T2|Left2], VarMap, Acc, Opaques) ->
- {NewT, NewVarMap} = t_unify(T1, T2, VarMap, Opaques),
- unify_lists(Left1, Left2, NewVarMap, [NewT|Acc], Opaques);
-unify_lists([], [], VarMap, Acc, _Opaques) ->
+unify_lists([T1|Left1], [T2|Left2], VarMap, Acc) ->
+ {NewT, NewVarMap} = t_unify(T1, T2, VarMap),
+ unify_lists(Left1, Left2, NewVarMap, [NewT|Acc]);
+unify_lists([], [], VarMap, Acc) ->
{lists:reverse(Acc), VarMap}.
%%t_assign_variables_to_subtype(T1, T2) ->
@@ -2837,11 +3470,12 @@ t_subtract(?identifier(Set1), ?identifier(Set2)) ->
?none -> ?none;
Set -> ?identifier(Set)
end;
-t_subtract(?opaque(Set1), ?opaque(Set2)) ->
- case set_subtract(Set1, Set2) of
- ?none -> ?none;
- Set -> ?opaque(Set)
- end;
+t_subtract(?opaque(_)=T1, ?opaque(_)=T2) ->
+ opaque_subtract(T1, t_opaque_structure(T2));
+t_subtract(?opaque(_)=T1, T2) ->
+ opaque_subtract(T1, T2);
+t_subtract(T1, ?opaque(_)=T2) ->
+ t_subtract(T1, t_opaque_structure(T2));
t_subtract(?matchstate(Pres1, Slots1), ?matchstate(Pres2, _Slots2)) ->
Pres = t_subtract(Pres1, Pres2),
case t_is_none(Pres) of
@@ -2965,6 +3599,8 @@ t_subtract(?product(Elements1) = T1, ?product(Elements2)) ->
_ -> T1
end
end;
+t_subtract(?map(_) = T, _) -> % XXX: very crude; will probably need refinement
+ T;
t_subtract(?product(P1), _) ->
?product(P1);
t_subtract(T, ?product(_)) ->
@@ -2976,6 +3612,17 @@ t_subtract(T1, T2) ->
?union(U2) = force_union(T2),
subtract_union(U1, U2).
+-spec opaque_subtract(erl_type(), erl_type()) -> erl_type().
+
+opaque_subtract(?opaque(Set1), T2) ->
+ List = [T1#opaque{struct = Sub} ||
+ #opaque{struct = S1}=T1 <- set_to_list(Set1),
+ not t_is_none(Sub = t_subtract(S1, T2))],
+ case List of
+ [] -> ?none;
+ _ -> ?opaque(ordsets:from_list(List))
+ end.
+
-spec t_subtract_lists([erl_type()], [erl_type()]) -> [erl_type()].
t_subtract_lists(L1, L2) ->
@@ -2991,7 +3638,18 @@ t_subtract_lists([], [], Acc) ->
-spec subtract_union([erl_type(),...], [erl_type(),...]) -> erl_type().
subtract_union(U1, U2) ->
- subtract_union(U1, U2, 0, []).
+ [A1,B1,F1,I1,L1,N1,T1,M1,O1,R1,Map1] = U1,
+ [A2,B2,F2,I2,L2,N2,T2,M2,O2,R2,Map2] = U2,
+ List1 = [A1,B1,F1,I1,L1,N1,T1,M1,?none,R1,Map1],
+ List2 = [A2,B2,F2,I2,L2,N2,T2,M2,?none,R2,Map2],
+ Sub1 = subtract_union(List1, List2, 0, []),
+ O = if O1 =:= ?none -> O1;
+ true -> t_subtract(O1, ?union(U2))
+ end,
+ Sub2 = if O2 =:= ?none -> Sub1;
+ true -> t_subtract(Sub1, t_opaque_structure(O2))
+ end,
+ t_sup(O, Sub2).
-spec subtract_union([erl_type()], [erl_type()], non_neg_integer(), [erl_type()]) -> erl_type().
@@ -3052,10 +3710,24 @@ t_is_equal(_, _) -> false.
t_is_subtype(T1, T2) ->
Inf = t_inf(T1, T2),
- t_is_equal(T1, Inf).
+ subtype_is_equal(T1, Inf).
+
+%% The subtype relation has to behave correctly irrespective of opaque
+%% types.
+subtype_is_equal(T, T) -> true;
+subtype_is_equal(T1, T2) ->
+ t_is_equal(case t_contains_opaque(T1) of
+ true -> t_unopaque(T1);
+ false -> T1
+ end,
+ case t_contains_opaque(T2) of
+ true -> t_unopaque(T2);
+ false -> T2
+ end).
-spec t_is_instance(erl_type(), erl_type()) -> boolean().
+%% XXX. To be removed.
t_is_instance(ConcreteType, Type) ->
t_is_subtype(ConcreteType, t_unopaque(Type)).
@@ -3067,12 +3739,12 @@ t_unopaque(T) ->
-spec t_unopaque(erl_type(), 'universe' | [erl_type()]) -> erl_type().
t_unopaque(?opaque(_) = T, Opaques) ->
- case Opaques =:= universe orelse lists:member(T, Opaques) of
+ case Opaques =:= 'universe' orelse is_opaque_type(T, Opaques) of
true -> t_unopaque(t_opaque_structure(T), Opaques);
- false -> T % XXX: needs revision for parametric opaque data types
+ false -> T
end;
t_unopaque(?list(ElemT, Termination, Sz), Opaques) ->
- ?list(t_unopaque(ElemT, Opaques), Termination, Sz);
+ ?list(t_unopaque(ElemT, Opaques), t_unopaque(Termination, Opaques), Sz);
t_unopaque(?tuple(?any, _, _) = T, _) -> T;
t_unopaque(?tuple(ArgTs, Sz, Tag), Opaques) when is_list(ArgTs) ->
NewArgTs = [t_unopaque(A, Opaques) || A <- ArgTs],
@@ -3081,14 +3753,20 @@ t_unopaque(?tuple_set(Set), Opaques) ->
NewSet = [{Sz, [t_unopaque(T, Opaques) || T <- Tuples]}
|| {Sz, Tuples} <- Set],
?tuple_set(NewSet);
-t_unopaque(?union([A,B,F,I,L,N,T,M,O,R]), Opaques) ->
+t_unopaque(?product(Types), Opaques) ->
+ ?product([t_unopaque(T, Opaques) || T <- Types]);
+t_unopaque(?function(Domain, Range), Opaques) ->
+ ?function(t_unopaque(Domain, Opaques), t_unopaque(Range, Opaques));
+t_unopaque(?union([A,B,F,I,L,N,T,M,O,R,Map]), Opaques) ->
UL = t_unopaque(L, Opaques),
UT = t_unopaque(T, Opaques),
- UO = case O of
- ?none -> [];
- ?opaque(Os) -> [t_unopaque(S, Opaques) || #opaque{struct = S} <- Os]
- end,
- t_sup([?union([A,B,F,I,UL,N,UT,M,?none,R])|UO]);
+ UF = t_unopaque(F, Opaques),
+ UMap = t_unopaque(Map, Opaques),
+ {OF,UO} = case t_unopaque(O, Opaques) of
+ ?opaque(_) = O1 -> {O1, []};
+ Type -> {?none, [Type]}
+ end,
+ t_sup([?union([A,B,UF,I,UL,N,UT,M,OF,R,UMap])|UO]);
t_unopaque(T, _) ->
T.
@@ -3134,6 +3812,12 @@ t_limit_k(?product(Elements), K) ->
?product([t_limit_k(X, K - 1) || X <- Elements]);
t_limit_k(?union(Elements), K) ->
?union([t_limit_k(X, K) || X <- Elements]);
+t_limit_k(?opaque(Es), K) ->
+ List = [begin
+ NewS = t_limit_k(S, K),
+ Opaque#opaque{struct = NewS}
+ end || #opaque{struct = S} = Opaque <- set_to_list(Es)],
+ ?opaque(ordsets:from_list(List));
t_limit_k(T, _K) -> T.
%%============================================================================
@@ -3142,7 +3826,7 @@ t_limit_k(T, _K) -> T.
%%
%%============================================================================
--spec t_abstract_records(erl_type(), dict()) -> erl_type().
+-spec t_abstract_records(erl_type(), type_table()) -> erl_type().
t_abstract_records(?list(Contents, Termination, Size), RecDict) ->
case t_abstract_records(Contents, RecDict) of
@@ -3167,7 +3851,7 @@ t_abstract_records(?union(Types), RecDict) ->
t_abstract_records(?tuple(?any, ?any, ?any) = T, _RecDict) ->
T;
t_abstract_records(?tuple(Elements, Arity, ?atom(_) = Tag), RecDict) ->
- [TagAtom] = t_atom_vals(Tag),
+ [TagAtom] = atom_vals(Tag),
case lookup_record(TagAtom, Arity - 1, RecDict) of
error -> t_tuple([t_abstract_records(E, RecDict) || E <- Elements]);
{ok, Fields} -> t_tuple([Tag|[T || {_Name, T} <- Fields]])
@@ -3176,6 +3860,8 @@ t_abstract_records(?tuple(Elements, _Arity, _Tag), RecDict) ->
t_tuple([t_abstract_records(E, RecDict) || E <- Elements]);
t_abstract_records(?tuple_set(_) = Tuples, RecDict) ->
t_sup([t_abstract_records(T, RecDict) || T <- t_tuple_subtypes(Tuples)]);
+t_abstract_records(?opaque(_)=Type, RecDict) ->
+ t_abstract_records(t_opaque_structure(Type), RecDict);
t_abstract_records(T, _RecDict) ->
T.
@@ -3198,6 +3884,14 @@ t_map(Fun, ?tuple(Elements, _Arity, _Tag)) ->
Fun(t_tuple([t_map(Fun, E) || E <- Elements]));
t_map(Fun, ?tuple_set(_) = Tuples) ->
Fun(t_sup([t_map(Fun, T) || T <- t_tuple_subtypes(Tuples)]));
+t_map(Fun, ?opaque(Set)) ->
+ L = [Opaque#opaque{struct = NewS} ||
+ #opaque{struct = S} = Opaque <- set_to_list(Set),
+ not t_is_none(NewS = t_map(Fun, S))],
+ Fun(case L of
+ [] -> ?none;
+ _ -> ?opaque(ordsets:from_list(L))
+ end);
t_map(Fun, T) ->
Fun(T).
@@ -3212,7 +3906,7 @@ t_map(Fun, T) ->
t_to_string(T) ->
t_to_string(T, dict:new()).
--spec t_to_string(erl_type(), dict()) -> string().
+-spec t_to_string(erl_type(), type_table()) -> string().
t_to_string(?any, _RecDict) ->
"any()";
@@ -3239,11 +3933,11 @@ t_to_string(?bitstr(8, 0), _RecDict) ->
t_to_string(?bitstr(1, 0), _RecDict) ->
"bitstring()";
t_to_string(?bitstr(0, B), _RecDict) ->
- lists:flatten(io_lib:format("<<_:~w>>", [B]));
+ flat_format("<<_:~w>>", [B]);
t_to_string(?bitstr(U, 0), _RecDict) ->
- lists:flatten(io_lib:format("<<_:_*~w>>", [U]));
+ flat_format("<<_:_*~w>>", [U]);
t_to_string(?bitstr(U, B), _RecDict) ->
- lists:flatten(io_lib:format("<<_:~w,_:_*~w>>", [B, U]));
+ flat_format("<<_:~w,_:_*~w>>", [B, U]);
t_to_string(?function(?any, ?any), _RecDict) ->
"fun()";
t_to_string(?function(?any, Range), RecDict) ->
@@ -3255,18 +3949,16 @@ t_to_string(?identifier(Set), _RecDict) ->
case Set of
?any -> "identifier()";
_ ->
- string:join([io_lib:format("~w()", [T]) || T <- set_to_list(Set)], " | ")
+ string:join([flat_format("~w()", [T]) || T <- set_to_list(Set)], " | ")
end;
-t_to_string(?opaque(Set), _RecDict) ->
- string:join([case is_opaque_builtin(Mod, Name) of
- true -> io_lib:format("~w()", [Name]);
- false -> io_lib:format("~w:~w()", [Mod, Name])
- end
- || #opaque{mod = Mod, name = Name} <- set_to_list(Set)],
+t_to_string(?opaque(Set), RecDict) ->
+ string:join([opaque_type(Mod, Name, Args, S, RecDict) ||
+ #opaque{mod = Mod, name = Name, struct = S, args = Args}
+ <- set_to_list(Set)],
" | ");
t_to_string(?matchstate(Pres, Slots), RecDict) ->
- io_lib:format("ms(~s,~s)", [t_to_string(Pres, RecDict),
- t_to_string(Slots,RecDict)]);
+ flat_format("ms(~s,~s)", [t_to_string(Pres, RecDict),
+ t_to_string(Slots,RecDict)]);
t_to_string(?nil, _RecDict) ->
"[]";
t_to_string(?nonempty_list(Contents, Termination), RecDict) ->
@@ -3282,7 +3974,9 @@ t_to_string(?nonempty_list(Contents, Termination), RecDict) ->
case Contents =:= ?any of
true -> ok;
false ->
- erlang:error({illegal_list, ?nonempty_list(Contents, Termination)})
+ %% XXX. See comment below.
+ %% erlang:error({illegal_list, ?nonempty_list(Contents, Termination)})
+ ok
end,
"nonempty_maybe_improper_list()";
_ ->
@@ -3305,11 +3999,14 @@ t_to_string(?list(Contents, Termination, ?unknown_qual), RecDict) ->
end;
?any ->
%% Just a safety check.
+ %% XXX. Types such as "maybe_improper_list(integer(), any())"
+ %% are OK, but cannot be printed!?
case Contents =:= ?any of
true -> ok;
false ->
- L = ?list(Contents, Termination, ?unknown_qual),
- erlang:error({illegal_list, L})
+ ok
+ %% L = ?list(Contents, Termination, ?unknown_qual),
+ %% erlang:error({illegal_list, L})
end,
"maybe_improper_list()";
_ ->
@@ -3330,7 +4027,7 @@ t_to_string(?integer_pos, _RecDict) -> "pos_integer()";
t_to_string(?integer_non_neg, _RecDict) -> "non_neg_integer()";
t_to_string(?integer_neg, _RecDict) -> "neg_integer()";
t_to_string(?int_range(From, To), _RecDict) ->
- lists:flatten(io_lib:format("~w..~w", [From, To]));
+ flat_format("~w..~w", [From, To]);
t_to_string(?integer(?any), _RecDict) -> "integer()";
t_to_string(?float, _RecDict) -> "float()";
t_to_string(?number(?any, ?unknown_qual), _RecDict) -> "number()";
@@ -3338,19 +4035,21 @@ t_to_string(?product(List), RecDict) ->
"<" ++ comma_sequence(List, RecDict) ++ ">";
t_to_string(?remote(Set), RecDict) ->
string:join([case Args =:= [] of
- true -> io_lib:format("~w:~w()", [Mod, Name]);
+ true -> flat_format("~w:~w()", [Mod, Name]);
false ->
ArgString = comma_sequence(Args, RecDict),
- io_lib:format("~w:~w(~s)", [Mod, Name, ArgString])
+ flat_format("~w:~w(~s)", [Mod, Name, ArgString])
end
|| #remote{mod = Mod, name = Name, args = Args} <-
set_to_list(Set)],
" | ");
+t_to_string(?map(Pairs), RecDict) ->
+ "#{" ++ map_pairs_to_string(Pairs,RecDict) ++ "}";
t_to_string(?tuple(?any, ?any, ?any), _RecDict) -> "tuple()";
t_to_string(?tuple(Elements, _Arity, ?any), RecDict) ->
"{" ++ comma_sequence(Elements, RecDict) ++ "}";
t_to_string(?tuple(Elements, Arity, Tag), RecDict) ->
- [TagAtom] = t_atom_vals(Tag),
+ [TagAtom] = atom_vals(Tag),
case lookup_record(TagAtom, Arity-1, RecDict) of
error -> "{" ++ comma_sequence(Elements, RecDict) ++ "}";
{ok, FieldNames} ->
@@ -3361,9 +4060,16 @@ t_to_string(?tuple_set(_) = T, RecDict) ->
t_to_string(?union(Types), RecDict) ->
union_sequence([T || T <- Types, T =/= ?none], RecDict);
t_to_string(?var(Id), _RecDict) when is_atom(Id) ->
- io_lib:format("~s", [atom_to_list(Id)]);
+ flat_format("~s", [atom_to_list(Id)]);
t_to_string(?var(Id), _RecDict) when is_integer(Id) ->
- io_lib:format("var(~w)", [Id]).
+ flat_format("var(~w)", [Id]).
+
+
+map_pairs_to_string([],_) -> [];
+map_pairs_to_string(Pairs,RecDict) ->
+ StrPairs = [{t_to_string(K,RecDict),t_to_string(V,RecDict)}||{K,V}<-Pairs],
+ string:join([K ++ "=>" ++ V||{K,V}<-StrPairs], ", ").
+
record_to_string(Tag, [_|Fields], FieldNames, RecDict) ->
FieldStrings = record_fields_to_string(Fields, FieldNames, RecDict, []),
@@ -3371,7 +4077,7 @@ record_to_string(Tag, [_|Fields], FieldNames, RecDict) ->
record_fields_to_string([F|Fs], [{FName, _DefType}|FDefs], RecDict, Acc) ->
NewAcc =
- case t_is_any(F) orelse t_is_atom('undefined', F) of
+ case t_is_equal(F, t_any()) orelse t_is_any_atom('undefined', F) of
true -> Acc;
false ->
StrFV = atom_to_string(FName) ++ "::" ++ t_to_string(F, RecDict),
@@ -3386,16 +4092,17 @@ record_fields_to_string([F|Fs], [{FName, _DefType}|FDefs], RecDict, Acc) ->
record_fields_to_string([], [], _RecDict, Acc) ->
lists:reverse(Acc).
--spec record_field_diffs_to_string(erl_type(), dict()) -> string().
+-spec record_field_diffs_to_string(erl_type(), type_table()) -> string().
record_field_diffs_to_string(?tuple([_|Fs], Arity, Tag), RecDict) ->
- [TagAtom] = t_atom_vals(Tag),
+ [TagAtom] = atom_vals(Tag),
{ok, FieldNames} = lookup_record(TagAtom, Arity-1, RecDict),
%% io:format("RecCElems = ~p\nRecTypes = ~p\n", [Fs, FieldNames]),
FieldDiffs = field_diffs(Fs, FieldNames, RecDict, []),
string:join(FieldDiffs, " and ").
field_diffs([F|Fs], [{FName, DefType}|FDefs], RecDict, Acc) ->
+ %% Don't care about opaqueness for now.
NewAcc =
case not t_is_none(t_inf(F, DefType)) of
true -> Acc;
@@ -3418,6 +4125,32 @@ union_sequence(Types, RecDict) ->
List = [t_to_string(T, RecDict) || T <- Types],
string:join(List, " | ").
+-ifdef(DEBUG).
+opaque_type(Mod, Name, _Args, S, RecDict) ->
+ ArgsString = comma_sequence(_Args, RecDict),
+ String = t_to_string(S, RecDict),
+ opaque_name(Mod, Name, ArgsString) ++ "[" ++ String ++ "]".
+-else.
+opaque_type(Mod, Name, Args, _S, RecDict) ->
+ ArgsString = comma_sequence(Args, RecDict),
+ opaque_name(Mod, Name, ArgsString).
+-endif.
+
+opaque_name(Mod, Name, Extra) ->
+ S = mod_name(Mod, Name),
+ flat_format("~s(~s)", [S, Extra]).
+
+mod_name(Mod, Name) ->
+ case is_obsolete_opaque_builtin(Mod, Name) of
+ true -> flat_format("~w", [Name]);
+ false -> flat_format("~w:~w", [Mod, Name])
+ end.
+
+is_obsolete_opaque_builtin(digraph, digraph) -> true;
+is_obsolete_opaque_builtin(gb_sets, gb_set) -> true;
+is_obsolete_opaque_builtin(gb_trees, gb_tree) -> true;
+is_obsolete_opaque_builtin(_, _) -> false.
+
%%=============================================================================
%%
%% Build a type from parse forms.
@@ -3429,318 +4162,270 @@ union_sequence(Types, RecDict) ->
t_from_form(Form) ->
t_from_form(Form, dict:new()).
--spec t_from_form(parse_form(), dict()) -> erl_type().
+-spec t_from_form(parse_form(), type_table()) -> erl_type().
t_from_form(Form, RecDict) ->
t_from_form(Form, RecDict, dict:new()).
--spec t_from_form(parse_form(), dict(), dict()) -> erl_type().
+-spec t_from_form(parse_form(), type_table(), var_table()) -> erl_type().
t_from_form(Form, RecDict, VarDict) ->
- {T, _R} = t_from_form(Form, [], false, RecDict, VarDict),
+ {T, _R} = t_from_form(Form, [], RecDict, VarDict),
T.
--type type_names() :: [{'type' | 'opaque' | 'record', atom()}].
--spec t_from_form(parse_form(), type_names(), boolean(), dict(), dict()) ->
+-type type_names() :: [type_key() | record_key()].
+
+-spec t_from_form(parse_form(), type_names(), type_table(), var_table()) ->
{erl_type(), type_names()}.
-t_from_form({var, _L, '_'}, _TypeNames, _InOpaque, _RecDict, _VarDict) ->
+t_from_form({var, _L, '_'}, _TypeNames, _RecDict, _VarDict) ->
{t_any(), []};
-t_from_form({var, _L, Name}, _TypeNames, _InOpaque, _RecDict, VarDict) ->
+t_from_form({var, _L, Name}, _TypeNames, _RecDict, VarDict) ->
case dict:find(Name, VarDict) of
error -> {t_var(Name), []};
{ok, Val} -> {Val, []}
end;
-t_from_form({ann_type, _L, [_Var, Type]}, TypeNames, InOpaque, RecDict,
- VarDict) ->
- t_from_form(Type, TypeNames, InOpaque, RecDict, VarDict);
-t_from_form({paren_type, _L, [Type]}, TypeNames, InOpaque, RecDict,
- VarDict) ->
- t_from_form(Type, TypeNames, InOpaque, RecDict, VarDict);
+t_from_form({ann_type, _L, [_Var, Type]}, TypeNames, RecDict, VarDict) ->
+ t_from_form(Type, TypeNames, RecDict, VarDict);
+t_from_form({paren_type, _L, [Type]}, TypeNames, RecDict, VarDict) ->
+ t_from_form(Type, TypeNames, RecDict, VarDict);
t_from_form({remote_type, _L, [{atom, _, Module}, {atom, _, Type}, Args]},
- TypeNames, InOpaque, RecDict, VarDict) ->
- {L, R} = list_from_form(Args, TypeNames, InOpaque, RecDict, VarDict),
+ TypeNames, RecDict, VarDict) ->
+ {L, R} = list_from_form(Args, TypeNames, RecDict, VarDict),
{t_remote(Module, Type, L), R};
-t_from_form({atom, _L, Atom}, _TypeNames, _InOpaque, _RecDict, _VarDict) ->
+t_from_form({atom, _L, Atom}, _TypeNames, _RecDict, _VarDict) ->
{t_atom(Atom), []};
-t_from_form({integer, _L, Int}, _TypeNames, _InOpaque, _RecDict, _VarDict) ->
+t_from_form({integer, _L, Int}, _TypeNames, _RecDict, _VarDict) ->
{t_integer(Int), []};
-t_from_form({op, _L, _Op, _Arg} = Op, _TypeNames, _InOpaque, _RecDict,
- _VarDict) ->
+t_from_form({op, _L, _Op, _Arg} = Op, _TypeNames, _RecDict, _VarDict) ->
case erl_eval:partial_eval(Op) of
{integer, _, Val} ->
{t_integer(Val), []};
_ -> throw({error, io_lib:format("Unable to evaluate type ~w\n", [Op])})
end;
-t_from_form({op, _L, _Op, _Arg1, _Arg2} = Op, _TypeNames, _InOpaque,
+t_from_form({op, _L, _Op, _Arg1, _Arg2} = Op, _TypeNames,
_RecDict, _VarDict) ->
case erl_eval:partial_eval(Op) of
{integer, _, Val} ->
{t_integer(Val), []};
_ -> throw({error, io_lib:format("Unable to evaluate type ~w\n", [Op])})
end;
-t_from_form({type, _L, any, []}, _TypeNames, _InOpaque, _RecDict,
- _VarDict) ->
+t_from_form({type, _L, any, []}, _TypeNames, _RecDict, _VarDict) ->
{t_any(), []};
-t_from_form({type, _L, arity, []}, _TypeNames, _InOpaque, _RecDict,
- _VarDict) ->
+t_from_form({type, _L, arity, []}, _TypeNames, _RecDict, _VarDict) ->
{t_arity(), []};
-t_from_form({type, _L, array, []}, _TypeNames, _InOpaque, _RecDict,
- _VarDict) ->
- {t_array(), []};
-t_from_form({type, _L, atom, []}, _TypeNames, _InOpaque, _RecDict,
- _VarDict) ->
+t_from_form({type, _L, array, []}, TypeNames, RecDict, VarDict) ->
+ builtin_type(array, t_array(), TypeNames, RecDict, VarDict);
+t_from_form({type, _L, atom, []}, _TypeNames, _RecDict, _VarDict) ->
{t_atom(), []};
-t_from_form({type, _L, binary, []}, _TypeNames, _InOpaque, _RecDict,
- _VarDict) ->
+t_from_form({type, _L, binary, []}, _TypeNames, _RecDict, _VarDict) ->
{t_binary(), []};
t_from_form({type, _L, binary, [Base, Unit]} = Type,
- _TypeNames, _InOpaque, _RecDict, _VarDict) ->
+ _TypeNames, _RecDict, _VarDict) ->
case {erl_eval:partial_eval(Base), erl_eval:partial_eval(Unit)} of
{{integer, _, B}, {integer, _, U}} when B >= 0, U >= 0 ->
{t_bitstr(U, B), []};
_ -> throw({error, io_lib:format("Unable to evaluate type ~w\n", [Type])})
end;
-t_from_form({type, _L, bitstring, []}, _TypeNames, _InOpaque, _RecDict,
- _VarDict) ->
+t_from_form({type, _L, bitstring, []}, _TypeNames, _RecDict, _VarDict) ->
{t_bitstr(), []};
-t_from_form({type, _L, bool, []}, _TypeNames, _InOpaque, _RecDict,
- _VarDict) ->
+t_from_form({type, _L, bool, []}, _TypeNames, _RecDict, _VarDict) ->
{t_boolean(), []}; % XXX: Temporarily
-t_from_form({type, _L, boolean, []}, _TypeNames, _InOpaque, _RecDict,
- _VarDict) ->
+t_from_form({type, _L, boolean, []}, _TypeNames, _RecDict, _VarDict) ->
{t_boolean(), []};
-t_from_form({type, _L, byte, []}, _TypeNames, _InOpaque, _RecDict,
- _VarDict) ->
+t_from_form({type, _L, byte, []}, _TypeNames, _RecDict, _VarDict) ->
{t_byte(), []};
-t_from_form({type, _L, char, []}, _TypeNames, _InOpaque, _RecDict,
- _VarDict) ->
+t_from_form({type, _L, char, []}, _TypeNames, _RecDict, _VarDict) ->
{t_char(), []};
-t_from_form({type, _L, dict, []}, _TypeNames, _InOpaque, _RecDict,
- _VarDict) ->
- {t_dict(), []};
-t_from_form({type, _L, digraph, []}, _TypeNames, _InOpaque, _RecDict,
- _VarDict) ->
- {t_digraph(), []};
-t_from_form({type, _L, float, []}, _TypeNames, _InOpaque, _RecDict,
- _VarDict) ->
+t_from_form({type, _L, dict, []}, TypeNames, RecDict, VarDict) ->
+ builtin_type(dict, t_dict(), TypeNames, RecDict, VarDict);
+t_from_form({type, _L, digraph, []}, TypeNames, RecDict, VarDict) ->
+ builtin_type(digraph, t_digraph(), TypeNames, RecDict, VarDict);
+t_from_form({type, _L, float, []}, _TypeNames, _RecDict, _VarDict) ->
{t_float(), []};
-t_from_form({type, _L, function, []}, _TypeNames, _InOpaque, _RecDict,
- _VarDict) ->
+t_from_form({type, _L, function, []}, _TypeNames, _RecDict, _VarDict) ->
{t_fun(), []};
-t_from_form({type, _L, 'fun', []}, _TypeNames, _InOpaque, _RecDict,
- _VarDict) ->
+t_from_form({type, _L, 'fun', []}, _TypeNames, _RecDict, _VarDict) ->
{t_fun(), []};
t_from_form({type, _L, 'fun', [{type, _, any}, Range]}, TypeNames,
- InOpaque, RecDict, VarDict) ->
- {T, R} = t_from_form(Range, TypeNames, InOpaque, RecDict, VarDict),
+ RecDict, VarDict) ->
+ {T, R} = t_from_form(Range, TypeNames, RecDict, VarDict),
{t_fun(T), R};
t_from_form({type, _L, 'fun', [{type, _, product, Domain}, Range]},
- TypeNames, InOpaque, RecDict, VarDict) ->
- {L, R1} = list_from_form(Domain, TypeNames, InOpaque, RecDict, VarDict),
- {T, R2} = t_from_form(Range, TypeNames, InOpaque, RecDict, VarDict),
+ TypeNames, RecDict, VarDict) ->
+ {L, R1} = list_from_form(Domain, TypeNames, RecDict, VarDict),
+ {T, R2} = t_from_form(Range, TypeNames, RecDict, VarDict),
{t_fun(L, T), R1 ++ R2};
-t_from_form({type, _L, gb_set, []}, _TypeNames, _InOpaque, _RecDict,
- _VarDict) ->
- {t_gb_set(), []};
-t_from_form({type, _L, gb_tree, []}, _TypeNames, _InOpaque, _RecDict,
- _VarDict) ->
- {t_gb_tree(), []};
-t_from_form({type, _L, identifier, []}, _TypeNames, _InOpaque, _RecDict,
- _VarDict) ->
+t_from_form({type, _L, gb_set, []}, TypeNames, RecDict, VarDict) ->
+ builtin_type(gb_set, t_gb_set(), TypeNames, RecDict, VarDict);
+t_from_form({type, _L, gb_tree, []}, TypeNames, RecDict, VarDict) ->
+ builtin_type(gb_tree, t_gb_tree(), TypeNames, RecDict, VarDict);
+t_from_form({type, _L, identifier, []}, _TypeNames, _RecDict, _VarDict) ->
{t_identifier(), []};
-t_from_form({type, _L, integer, []}, _TypeNames, _InOpaque, _RecDict,
- _VarDict) ->
+t_from_form({type, _L, integer, []}, _TypeNames, _RecDict, _VarDict) ->
{t_integer(), []};
-t_from_form({type, _L, iodata, []}, _TypeNames, _InOpaque, _RecDict,
- _VarDict) ->
+t_from_form({type, _L, iodata, []}, _TypeNames, _RecDict, _VarDict) ->
{t_iodata(), []};
-t_from_form({type, _L, iolist, []}, _TypeNames, _InOpaque, _RecDict,
- _VarDict) ->
+t_from_form({type, _L, iolist, []}, _TypeNames, _RecDict, _VarDict) ->
{t_iolist(), []};
-t_from_form({type, _L, list, []}, _TypeNames, _InOpaque, _RecDict,
- _VarDict) ->
+t_from_form({type, _L, list, []}, _TypeNames, _RecDict, _VarDict) ->
{t_list(), []};
-t_from_form({type, _L, list, [Type]}, TypeNames, InOpaque, RecDict,
- VarDict) ->
- {T, R} = t_from_form(Type, TypeNames, InOpaque, RecDict, VarDict),
+t_from_form({type, _L, list, [Type]}, TypeNames, RecDict, VarDict) ->
+ {T, R} = t_from_form(Type, TypeNames, RecDict, VarDict),
{t_list(T), R};
-t_from_form({type, _L, mfa, []}, _TypeNames, _InOpaque, _RecDict,
- _VarDict) ->
+t_from_form({type, _L, map, _}, TypeNames, RecDict, VarDict) ->
+ builtin_type(map, t_map([]), TypeNames, RecDict, VarDict);
+t_from_form({type, _L, mfa, []}, _TypeNames, _RecDict, _VarDict) ->
{t_mfa(), []};
-t_from_form({type, _L, module, []}, _TypeNames, _InOpaque, _RecDict,
- _VarDict) ->
+t_from_form({type, _L, module, []}, _TypeNames, _RecDict, _VarDict) ->
{t_module(), []};
-t_from_form({type, _L, nil, []}, _TypeNames, _InOpaque, _RecDict,
- _VarDict) ->
+t_from_form({type, _L, nil, []}, _TypeNames, _RecDict, _VarDict) ->
{t_nil(), []};
-t_from_form({type, _L, neg_integer, []}, _TypeNames, _InOpaque, _RecDict,
- _VarDict) ->
+t_from_form({type, _L, neg_integer, []}, _TypeNames, _RecDict, _VarDict) ->
{t_neg_integer(), []};
-t_from_form({type, _L, non_neg_integer, []}, _TypeNames, _InOpaque, _RecDict,
+t_from_form({type, _L, non_neg_integer, []}, _TypeNames, _RecDict,
_VarDict) ->
{t_non_neg_integer(), []};
-t_from_form({type, _L, no_return, []}, _TypeNames, _InOpaque, _RecDict,
- _VarDict) ->
+t_from_form({type, _L, no_return, []}, _TypeNames, _RecDict, _VarDict) ->
{t_unit(), []};
-t_from_form({type, _L, node, []}, _TypeNames, _InOpaque, _RecDict,
- _VarDict) ->
+t_from_form({type, _L, node, []}, _TypeNames, _RecDict, _VarDict) ->
{t_node(), []};
-t_from_form({type, _L, none, []}, _TypeNames, _InOpaque, _RecDict,
- _VarDict) ->
+t_from_form({type, _L, none, []}, _TypeNames, _RecDict, _VarDict) ->
{t_none(), []};
-t_from_form({type, _L, nonempty_list, []}, _TypeNames, _InOpaque, _RecDict,
- _VarDict) ->
+t_from_form({type, _L, nonempty_list, []}, _TypeNames, _RecDict, _VarDict) ->
{t_nonempty_list(), []};
-t_from_form({type, _L, nonempty_list, [Type]}, TypeNames, InOpaque, RecDict,
- VarDict) ->
- {T, R} = t_from_form(Type, TypeNames, InOpaque, RecDict, VarDict),
+t_from_form({type, _L, nonempty_list, [Type]}, TypeNames, RecDict, VarDict) ->
+ {T, R} = t_from_form(Type, TypeNames, RecDict, VarDict),
{t_nonempty_list(T), R};
t_from_form({type, _L, nonempty_improper_list, [Cont, Term]}, TypeNames,
- InOpaque, RecDict, VarDict) ->
- {T1, R1} = t_from_form(Cont, TypeNames, InOpaque, RecDict, VarDict),
- {T2, R2} = t_from_form(Term, TypeNames, InOpaque, RecDict, VarDict),
+ RecDict, VarDict) ->
+ {T1, R1} = t_from_form(Cont, TypeNames, RecDict, VarDict),
+ {T2, R2} = t_from_form(Term, TypeNames, RecDict, VarDict),
{t_cons(T1, T2), R1 ++ R2};
t_from_form({type, _L, nonempty_maybe_improper_list, []}, _TypeNames,
- _InOpaque, _RecDict, _VarDict) ->
+ _RecDict, _VarDict) ->
{t_cons(?any, ?any), []};
t_from_form({type, _L, nonempty_maybe_improper_list, [Cont, Term]},
- TypeNames, InOpaque, RecDict, VarDict) ->
- {T1, R1} = t_from_form(Cont, TypeNames, InOpaque, RecDict, VarDict),
- {T2, R2} = t_from_form(Term, TypeNames, InOpaque, RecDict, VarDict),
+ TypeNames, RecDict, VarDict) ->
+ {T1, R1} = t_from_form(Cont, TypeNames, RecDict, VarDict),
+ {T2, R2} = t_from_form(Term, TypeNames, RecDict, VarDict),
{t_cons(T1, T2), R1 ++ R2};
-t_from_form({type, _L, nonempty_string, []}, _TypeNames, _InOpaque, _RecDict,
+t_from_form({type, _L, nonempty_string, []}, _TypeNames, _RecDict,
_VarDict) ->
{t_nonempty_string(), []};
-t_from_form({type, _L, number, []}, _TypeNames, _InOpaque, _RecDict,
- _VarDict) ->
+t_from_form({type, _L, number, []}, _TypeNames, _RecDict, _VarDict) ->
{t_number(), []};
-t_from_form({type, _L, pid, []}, _TypeNames, _InOpaque, _RecDict,
- _VarDict) ->
+t_from_form({type, _L, pid, []}, _TypeNames, _RecDict, _VarDict) ->
{t_pid(), []};
-t_from_form({type, _L, port, []}, _TypeNames, _InOpaque, _RecDict,
- _VarDict) ->
+t_from_form({type, _L, port, []}, _TypeNames, _RecDict, _VarDict) ->
{t_port(), []};
-t_from_form({type, _L, pos_integer, []}, _TypeNames, _InOpaque, _RecDict,
- _VarDict) ->
+t_from_form({type, _L, pos_integer, []}, _TypeNames, _RecDict, _VarDict) ->
{t_pos_integer(), []};
-t_from_form({type, _L, maybe_improper_list, []}, _TypeNames, _InOpaque,
+t_from_form({type, _L, maybe_improper_list, []}, _TypeNames,
_RecDict, _VarDict) ->
{t_maybe_improper_list(), []};
t_from_form({type, _L, maybe_improper_list, [Content, Termination]},
- TypeNames, InOpaque, RecDict, VarDict) ->
- {T1, R1} = t_from_form(Content, TypeNames, InOpaque, RecDict, VarDict),
- {T2, R2} = t_from_form(Termination, TypeNames, InOpaque, RecDict, VarDict),
+ TypeNames, RecDict, VarDict) ->
+ {T1, R1} = t_from_form(Content, TypeNames, RecDict, VarDict),
+ {T2, R2} = t_from_form(Termination, TypeNames, RecDict, VarDict),
{t_maybe_improper_list(T1, T2), R1 ++ R2};
-t_from_form({type, _L, product, Elements}, TypeNames, InOpaque, RecDict,
- VarDict) ->
- {L, R} = list_from_form(Elements, TypeNames, InOpaque, RecDict, VarDict),
+t_from_form({type, _L, product, Elements}, TypeNames, RecDict, VarDict) ->
+ {L, R} = list_from_form(Elements, TypeNames, RecDict, VarDict),
{t_product(L), R};
-t_from_form({type, _L, queue, []}, _TypeNames, _InOpaque, _RecDict,
- _VarDict) ->
- {t_queue(), []};
+t_from_form({type, _L, queue, []}, TypeNames, RecDict, VarDict) ->
+ builtin_type(queue, t_queue(), TypeNames, RecDict, VarDict);
t_from_form({type, _L, range, [From, To]} = Type,
- _TypeNames, _InOpaque, _RecDict, _VarDict) ->
+ _TypeNames, _RecDict, _VarDict) ->
case {erl_eval:partial_eval(From), erl_eval:partial_eval(To)} of
{{integer, _, FromVal}, {integer, _, ToVal}} ->
{t_from_range(FromVal, ToVal), []};
_ -> throw({error, io_lib:format("Unable to evaluate type ~w\n", [Type])})
end;
-t_from_form({type, _L, record, [Name|Fields]}, TypeNames, InOpaque, RecDict,
- VarDict) ->
- record_from_form(Name, Fields, TypeNames, InOpaque, RecDict, VarDict);
-t_from_form({type, _L, reference, []}, _TypeNames, _InOpaque, _RecDict,
- _VarDict) ->
+t_from_form({type, _L, record, [Name|Fields]}, TypeNames, RecDict, VarDict) ->
+ record_from_form(Name, Fields, TypeNames, RecDict, VarDict);
+t_from_form({type, _L, reference, []}, _TypeNames, _RecDict, _VarDict) ->
{t_reference(), []};
-t_from_form({type, _L, set, []}, _TypeNames, _InOpaque, _RecDict,
- _VarDict) ->
- {t_set(), []};
-t_from_form({type, _L, string, []}, _TypeNames, _InOpaque, _RecDict,
- _VarDict) ->
+t_from_form({type, _L, set, []}, TypeNames, RecDict, VarDict) ->
+ builtin_type(set, t_set(), TypeNames, RecDict, VarDict);
+t_from_form({type, _L, string, []}, _TypeNames, _RecDict, _VarDict) ->
{t_string(), []};
-t_from_form({type, _L, term, []}, _TypeNames, _InOpaque, _RecDict,
- _VarDict) ->
+t_from_form({type, _L, term, []}, _TypeNames, _RecDict, _VarDict) ->
{t_any(), []};
-t_from_form({type, _L, tid, []}, _TypeNames, _InOpaque, _RecDict,
- _VarDict) ->
- {t_tid(), []};
-t_from_form({type, _L, timeout, []}, _TypeNames, _InOpaque, _RecDict,
- _VarDict) ->
+t_from_form({type, _L, tid, []}, TypeNames, RecDict, VarDict) ->
+ builtin_type(tid, t_tid(), TypeNames, RecDict, VarDict);
+t_from_form({type, _L, timeout, []}, _TypeNames, _RecDict, _VarDict) ->
{t_timeout(), []};
-t_from_form({type, _L, tuple, any}, _TypeNames, _InOpaque, _RecDict,
- _VarDict) ->
+t_from_form({type, _L, tuple, any}, _TypeNames, _RecDict, _VarDict) ->
{t_tuple(), []};
-t_from_form({type, _L, tuple, Args}, TypeNames, InOpaque, RecDict, VarDict) ->
- {L, R} = list_from_form(Args, TypeNames, InOpaque, RecDict, VarDict),
+t_from_form({type, _L, tuple, Args}, TypeNames, RecDict, VarDict) ->
+ {L, R} = list_from_form(Args, TypeNames, RecDict, VarDict),
{t_tuple(L), R};
-t_from_form({type, _L, union, Args}, TypeNames, InOpaque, RecDict, VarDict) ->
- {L, R} = list_from_form(Args, TypeNames, InOpaque, RecDict, VarDict),
+t_from_form({type, _L, union, Args}, TypeNames, RecDict, VarDict) ->
+ {L, R} = list_from_form(Args, TypeNames, RecDict, VarDict),
{t_sup(L), R};
-t_from_form({type, _L, Name, Args}, TypeNames, InOpaque, RecDict, VarDict) ->
+t_from_form({type, _L, Name, Args}, TypeNames, RecDict, VarDict) ->
+ type_from_form(Name, Args, TypeNames, RecDict, VarDict);
+t_from_form({opaque, _L, Name, {Mod, Args, Rep}}, _TypeNames,
+ _RecDict, _VarDict) ->
+ {t_opaque(Mod, Name, Args, Rep), []}.
+
+builtin_type(Name, Type, TypeNames, RecDict, VarDict) ->
+ case lookup_type(Name, 0, RecDict) of
+ {_, {_M, _T, _A}} ->
+ type_from_form(Name, [], TypeNames, RecDict, VarDict);
+ error ->
+ {Type, []}
+ end.
+
+type_from_form(Name, Args, TypeNames, RecDict, VarDict) ->
ArgsLen = length(Args),
+ ArgTypes = forms_to_types(Args, TypeNames, RecDict, VarDict),
case lookup_type(Name, ArgsLen, RecDict) of
{type, {_Module, Type, ArgNames}} ->
- case can_unfold_more({type, Name}, TypeNames) of
+ TypeName = {type, Name, ArgsLen},
+ case can_unfold_more(TypeName, TypeNames) of
true ->
- List = lists:zipwith(
- fun(ArgName, ArgType) ->
- {Ttemp, _R} = t_from_form(ArgType, TypeNames,
- InOpaque, RecDict,
- VarDict),
- {ArgName, Ttemp}
- end,
- ArgNames, Args),
+ List = lists:zip(ArgNames, ArgTypes),
TmpVarDict = dict:from_list(List),
- {T, R} = t_from_form(Type, [{type, Name}|TypeNames], InOpaque,
+ {T, R} = t_from_form(Type, [TypeName|TypeNames],
RecDict, TmpVarDict),
- case lists:member({type, Name}, R) of
+ case lists:member(TypeName, R) of
true -> {t_limit(T, ?REC_TYPE_LIMIT), R};
false -> {T, R}
end;
- false -> {t_any(), [{type, Name}]}
+ false -> {t_any(), [TypeName]}
end;
{opaque, {Module, Type, ArgNames}} ->
+ TypeName = {opaque, Name, ArgsLen},
{Rep, Rret} =
- case can_unfold_more({opaque, Name}, TypeNames) of
+ case can_unfold_more(TypeName, TypeNames) of
true ->
- List = lists:zipwith(
- fun(ArgName, ArgType) ->
- {Ttemp, _R} = t_from_form(ArgType, TypeNames,
- InOpaque, RecDict,
- VarDict),
- {ArgName, Ttemp}
- end,
- ArgNames, Args),
+ List = lists:zip(ArgNames, ArgTypes),
TmpVarDict = dict:from_list(List),
- {T, R} = t_from_form(Type, [{opaque, Name}|TypeNames], true,
+ {T, R} = t_from_form(Type, [TypeName|TypeNames],
RecDict, TmpVarDict),
- case lists:member({opaque, Name}, R) of
+ case lists:member(TypeName, R) of
true -> {t_limit(T, ?REC_TYPE_LIMIT), R};
false -> {T, R}
end;
- false -> {t_any(), [{opaque, Name}]}
+ false -> {t_any(), [TypeName]}
end,
- Tret =
- case InOpaque of
- true -> Rep;
- false ->
- t_from_form({opaque, -1, Name, {Module, Args, Rep}},
- RecDict, VarDict)
- end,
- {Tret, Rret};
+ Args2 = [subst_all_vars_to_any(ArgType) || ArgType <- ArgTypes],
+ {skip_opaque_alias(Rep, Module, Name, Args2), Rret};
error ->
Msg = io_lib:format("Unable to find type ~w/~w\n", [Name, ArgsLen]),
throw({error, Msg})
- end;
-t_from_form({opaque, _L, Name, {Mod, Args, Rep}}, _TypeNames, _InOpaque,
- _RecDict, _VarDict) ->
- case Args of
- [] -> {t_opaque(Mod, Name, Args, Rep), []};
- _ -> throw({error, "Polymorphic opaque types not supported yet"})
end.
-record_from_form({atom, _, Name}, ModFields, TypeNames, InOpaque, RecDict,
- VarDict) ->
+forms_to_types(Forms, TypeNames, RecDict, VarDict) ->
+ {Types, _} = list_from_form(Forms, TypeNames, RecDict, VarDict),
+ Types.
+
+skip_opaque_alias(?opaque(_) = T, _Mod, _Name, _Args) -> T;
+skip_opaque_alias(T, Module, Name, Args) ->
+ t_opaque(Module, Name, Args, T).
+
+record_from_form({atom, _, Name}, ModFields, TypeNames, RecDict, VarDict) ->
case can_unfold_more({record, Name}, TypeNames) of
true ->
case lookup_record(Name, RecDict) of
@@ -3751,11 +4436,11 @@ record_from_form({atom, _, Name}, ModFields, TypeNames, InOpaque, RecDict,
{DeclFields1, R1} =
case lists:all(fun(Elem) -> Elem end, AreTyped) of
true -> {DeclFields, []};
- false -> fields_from_form(DeclFields, TypeNames1, InOpaque,
+ false -> fields_from_form(DeclFields, TypeNames1,
RecDict, dict:new())
end,
{GetModRec, R2} = get_mod_record(ModFields, DeclFields1,
- TypeNames1, InOpaque,
+ TypeNames1,
RecDict, VarDict),
case GetModRec of
{error, FieldName} ->
@@ -3772,13 +4457,11 @@ record_from_form({atom, _, Name}, ModFields, TypeNames, InOpaque, RecDict,
false -> {t_any(), []}
end.
-get_mod_record([], DeclFields, _TypeNames, _InOpaque, _RecDict,
- _VarDict) ->
+get_mod_record([], DeclFields, _TypeNames, _RecDict, _VarDict) ->
{{ok, DeclFields}, []};
-get_mod_record(ModFields, DeclFields, TypeNames, InOpaque, RecDict,
- VarDict) ->
+get_mod_record(ModFields, DeclFields, TypeNames, RecDict, VarDict) ->
DeclFieldsDict = orddict:from_list(DeclFields),
- {ModFieldsDict, R} = build_field_dict(ModFields, TypeNames, InOpaque,
+ {ModFieldsDict, R} = build_field_dict(ModFields, TypeNames,
RecDict, VarDict),
case get_mod_record(DeclFieldsDict, ModFieldsDict, []) of
{error, _FieldName} = Error -> {Error, R};
@@ -3788,23 +4471,26 @@ get_mod_record(ModFields, DeclFields, TypeNames, InOpaque, RecDict,
R}
end.
-build_field_dict(FieldTypes, TypeNames, InOpaque, RecDict, VarDict) ->
- build_field_dict(FieldTypes, TypeNames, InOpaque, RecDict, VarDict, []).
+build_field_dict(FieldTypes, TypeNames, RecDict, VarDict) ->
+ build_field_dict(FieldTypes, TypeNames, RecDict, VarDict, []).
build_field_dict([{type, _, field_type, [{atom, _, Name}, Type]}|Left],
- TypeNames, InOpaque, RecDict, VarDict, Acc) ->
- {T, R1} = t_from_form(Type, TypeNames, InOpaque, RecDict, VarDict),
+ TypeNames, RecDict, VarDict, Acc) ->
+ {T, R1} = t_from_form(Type, TypeNames, RecDict, VarDict),
NewAcc = [{Name, T}|Acc],
- {D, R2} = build_field_dict(Left, TypeNames, InOpaque, RecDict, VarDict,
- NewAcc),
+ {D, R2} = build_field_dict(Left, TypeNames, RecDict, VarDict, NewAcc),
{D, R1 ++ R2};
-build_field_dict([], _TypeNames, _InOpaque, _RecDict, _VarDict, Acc) ->
+build_field_dict([], _TypeNames, _RecDict, _VarDict, Acc) ->
{orddict:from_list(Acc), []}.
get_mod_record([{FieldName, DeclType}|Left1],
[{FieldName, ModType}|Left2], Acc) ->
- case t_is_var(ModType) orelse t_is_remote(ModType) orelse
- t_is_subtype(ModType, DeclType) of
+ ModTypeNoVars = subst_all_vars_to_any(ModType),
+ case
+ contains_remote(ModTypeNoVars)
+ orelse contains_remote(DeclType)
+ orelse t_is_subtype(ModTypeNoVars, DeclType)
+ of
false -> {error, FieldName};
true -> get_mod_record(Left1, Left2, [{FieldName, ModType}|Acc])
end;
@@ -3817,19 +4503,23 @@ get_mod_record(DeclFields, [], Acc) ->
get_mod_record(_, [{FieldName2, _ModType}|_], _Acc) ->
{error, FieldName2}.
-fields_from_form([], _TypeNames, _InOpaque, _RecDict, _VarDict) ->
+contains_remote(Type) ->
+ TypeNoRemote = subst_all_remote(Type, t_none()),
+ not t_is_equal(Type, TypeNoRemote).
+
+fields_from_form([], _TypeNames, _RecDict, _VarDict) ->
{[], []};
-fields_from_form([{Name, Type}|Tail], TypeNames, InOpaque, RecDict,
+fields_from_form([{Name, Type}|Tail], TypeNames, RecDict,
VarDict) ->
- {T, R1} = t_from_form(Type, TypeNames, InOpaque, RecDict, VarDict),
- {F, R2} = fields_from_form(Tail, TypeNames, InOpaque, RecDict, VarDict),
+ {T, R1} = t_from_form(Type, TypeNames, RecDict, VarDict),
+ {F, R2} = fields_from_form(Tail, TypeNames, RecDict, VarDict),
{[{Name, T}|F], R1 ++ R2}.
-list_from_form([], _TypeNames, _InOpaque, _RecDict, _VarDict) ->
+list_from_form([], _TypeNames, _RecDict, _VarDict) ->
{[], []};
-list_from_form([H|Tail], TypeNames, InOpaque, RecDict, VarDict) ->
- {T, R1} = t_from_form(H, TypeNames, InOpaque, RecDict, VarDict),
- {L, R2} = list_from_form(Tail, TypeNames, InOpaque, RecDict, VarDict),
+list_from_form([H|Tail], TypeNames, RecDict, VarDict) ->
+ {T, R1} = t_from_form(H, TypeNames, RecDict, VarDict),
+ {L, R2} = list_from_form(Tail, TypeNames, RecDict, VarDict),
{[T|L], R1 ++ R2}.
-spec t_form_to_string(parse_form()) -> string().
@@ -3852,10 +4542,10 @@ t_form_to_string({op, _L, _Op, _Arg1, _Arg2} = Op) ->
t_form_to_string({ann_type, _L, [Var, Type]}) ->
t_form_to_string(Var) ++ "::" ++ t_form_to_string(Type);
t_form_to_string({paren_type, _L, [Type]}) ->
- io_lib:format("(~s)", [t_form_to_string(Type)]);
+ flat_format("(~s)", [t_form_to_string(Type)]);
t_form_to_string({remote_type, _L, [{atom, _, Mod}, {atom, _, Name}, Args]}) ->
ArgString = "(" ++ string:join(t_form_to_string_list(Args), ",") ++ ")",
- io_lib:format("~w:~w", [Mod, Name]) ++ ArgString;
+ flat_format("~w:~w", [Mod, Name]) ++ ArgString;
t_form_to_string({type, _L, arity, []}) -> "arity()";
t_form_to_string({type, _L, binary, []}) -> "binary()";
t_form_to_string({type, _L, binary, [Base, Unit]} = Type) ->
@@ -3866,9 +4556,9 @@ t_form_to_string({type, _L, binary, [Base, Unit]} = Type) ->
{0, 0} -> "<<>>";
{8, 0} -> "binary()";
{1, 0} -> "bitstring()";
- {0, B} -> lists:flatten(io_lib:format("<<_:~w>>", [B]));
- {U, 0} -> lists:flatten(io_lib:format("<<_:_*~w>>", [U]));
- {U, B} -> lists:flatten(io_lib:format("<<_:~w,_:_*~w>>", [B, U]))
+ {0, B} -> flat_format("<<_:~w>>", [B]);
+ {U, 0} -> flat_format("<<_:_*~w>>", [U]);
+ {U, B} -> flat_format("<<_:~w,_:_*~w>>", [B, U])
end;
_ -> io_lib:format("Badly formed bitstr type ~w", [Type])
end;
@@ -3883,6 +4573,8 @@ t_form_to_string({type, _L, iodata, []}) -> "iodata()";
t_form_to_string({type, _L, iolist, []}) -> "iolist()";
t_form_to_string({type, _L, list, [Type]}) ->
"[" ++ t_form_to_string(Type) ++ "]";
+t_form_to_string({type, _L, map, _}) ->
+ "#{}";
t_form_to_string({type, _L, mfa, []}) -> "mfa()";
t_form_to_string({type, _L, module, []}) -> "module()";
t_form_to_string({type, _L, node, []}) -> "node()";
@@ -3894,16 +4586,16 @@ t_form_to_string({type, _L, product, Elements}) ->
t_form_to_string({type, _L, range, [From, To]} = Type) ->
case {erl_eval:partial_eval(From), erl_eval:partial_eval(To)} of
{{integer, _, FromVal}, {integer, _, ToVal}} ->
- io_lib:format("~w..~w", [FromVal, ToVal]);
- _ -> io_lib:format("Badly formed type ~w",[Type])
+ flat_format("~w..~w", [FromVal, ToVal]);
+ _ -> flat_format("Badly formed type ~w",[Type])
end;
t_form_to_string({type, _L, record, [{atom, _, Name}]}) ->
- io_lib:format("#~w{}", [Name]);
+ flat_format("#~w{}", [Name]);
t_form_to_string({type, _L, record, [{atom, _, Name}|Fields]}) ->
FieldString = string:join(t_form_to_string_list(Fields), ","),
- io_lib:format("#~w{~s}", [Name, FieldString]);
+ flat_format("#~w{~s}", [Name, FieldString]);
t_form_to_string({type, _L, field_type, [{atom, _, Name}, Type]}) ->
- io_lib:format("~w::~s", [Name, t_form_to_string(Type)]);
+ flat_format("~w::~s", [Name, t_form_to_string(Type)]);
t_form_to_string({type, _L, term, []}) -> "term()";
t_form_to_string({type, _L, timeout, []}) -> "timeout()";
t_form_to_string({type, _L, tuple, any}) -> "tuple()";
@@ -3916,8 +4608,8 @@ t_form_to_string({type, _L, Name, []} = T) ->
catch throw:{error, _} -> atom_to_string(Name) ++ "()"
end;
t_form_to_string({type, _L, Name, List}) ->
- io_lib:format("~w(~s)",
- [Name, string:join(t_form_to_string_list(List), ",")]).
+ flat_format("~w(~s)",
+ [Name, string:join(t_form_to_string_list(List), ",")]).
t_form_to_string_list(List) ->
t_form_to_string_list(List, []).
@@ -3930,7 +4622,7 @@ t_form_to_string_list([], Acc) ->
-spec atom_to_string(atom()) -> string().
atom_to_string(Atom) ->
- lists:flatten(io_lib:format("~w", [Atom])).
+ flat_format("~w", [Atom]).
%%=============================================================================
%%
@@ -3959,7 +4651,7 @@ is_erl_type(?unit) -> true;
is_erl_type(#c{}) -> true;
is_erl_type(_) -> false.
--spec lookup_record(atom(), dict()) ->
+-spec lookup_record(atom(), type_table()) ->
'error' | {'ok', [{atom(), parse_form() | erl_type()}]}.
lookup_record(Tag, RecDict) when is_atom(Tag) ->
@@ -3974,7 +4666,8 @@ lookup_record(Tag, RecDict) when is_atom(Tag) ->
error
end.
--spec lookup_record(atom(), arity(), dict()) -> 'error' | {'ok', [{atom(), erl_type()}]}.
+-spec lookup_record(atom(), arity(), type_table()) ->
+ 'error' | {'ok', [{atom(), erl_type()}]}.
lookup_record(Tag, Arity, RecDict) when is_atom(Tag) ->
case dict:find({record, Tag}, RecDict) of
@@ -3993,7 +4686,8 @@ lookup_type(Name, Arity, RecDict) ->
{ok, Found} -> {type, Found}
end.
--spec type_is_defined('type' | 'opaque', atom(), arity(), dict()) -> boolean().
+-spec type_is_defined('type' | 'opaque', atom(), arity(), type_table()) ->
+ boolean().
type_is_defined(TypeOrOpaque, Name, Arity, RecDict) ->
dict:is_key({TypeOrOpaque, Name, Arity}, RecDict).
@@ -4002,6 +4696,59 @@ can_unfold_more(TypeName, TypeNames) ->
Fun = fun(E, Acc) -> case E of TypeName -> Acc + 1; _ -> Acc end end,
lists:foldl(Fun, 0, TypeNames) < ?REC_TYPE_LIMIT.
+-spec do_opaque(erl_type(), opaques(), fun((_) -> T)) -> T.
+
+%% Probably a little faster than calling t_unopaque/2.
+%% Unions that are due to opaque types are unopaqued.
+do_opaque(?opaque(_) = Type, Opaques, Pred) ->
+ case Opaques =:= 'universe' orelse is_opaque_type(Type, Opaques) of
+ true -> do_opaque(t_opaque_structure(Type), Opaques, Pred);
+ false -> Pred(Type)
+ end;
+do_opaque(?union(List) = Type, Opaques, Pred) ->
+ [A,B,F,I,L,N,T,M,O,R,Map] = List,
+ if O =:= ?none -> Pred(Type);
+ true ->
+ case Opaques =:= 'universe' orelse is_opaque_type(O, Opaques) of
+ true ->
+ S = t_opaque_structure(O),
+ do_opaque(t_sup([A,B,F,I,L,N,T,M,S,R,Map]), Opaques, Pred);
+ false -> Pred(Type)
+ end
+ end;
+do_opaque(Type, _Opaques, Pred) ->
+ Pred(Type).
+
+is_same_type_name(ModNameArgs, ModNameArgs) -> true;
+is_same_type_name({Mod, Name, Args1}, {Mod, Name, Args2}) ->
+ all_any(Args1) orelse all_any(Args2);
+is_same_type_name({Mod1, Name1, Args1}, {Mod2, Name2, Args2}) ->
+ is_same_type_name2(Mod1, Name1, Args1, Mod2, Name2, Args2).
+
+all_any([]) -> true;
+all_any([T|L]) ->
+ t_is_any(T) andalso all_any(L);
+all_any(_) -> false.
+
+%% Compatibility. In Erlang/OTP 17 the pre-defined opaque types
+%% digraph() and so on can be used, but there are also new types such
+%% as digraph:graph() with the exact same meaning. In Erlang/OTP R18.0
+%% all but the last clause can be removed.
+
+is_same_type_name2(digraph, digraph, [], digraph, graph, []) -> true;
+is_same_type_name2(digraph, graph, [], digraph, digraph, []) -> true;
+is_same_type_name2(gb_sets, gb_set, [], gb_sets, set, [_]) -> true;
+is_same_type_name2(gb_sets, set, [_], gb_sets, gb_set, []) -> true;
+is_same_type_name2(gb_trees, gb_tree, [], gb_trees, tree, [_, _]) -> true;
+is_same_type_name2(gb_trees, tree, [_, _], gb_trees, gb_tree, []) -> true;
+is_same_type_name2(_, _, _, _, _, _) -> false.
+
+map_keys(?map(Pairs)) ->
+ [K || {K, _} <- Pairs].
+
+map_values(?map(Pairs)) ->
+ [V || {_, V} <- Pairs].
+
%% -----------------------------------
%% Set
%%
@@ -4068,7 +4815,7 @@ set_size(Set) ->
set_to_string(Set) ->
L = [case is_atom(X) of
true -> io_lib:write_string(atom_to_list(X), $'); % stupid emacs '
- false -> io_lib:format("~w", [X])
+ false -> flat_format("~w", [X])
end || X <- set_to_list(Set)],
string:join(L, " | ").
@@ -4077,6 +4824,9 @@ set_min([H|_]) -> H.
set_max(Set) ->
hd(lists:reverse(Set)).
+flat_format(F, S) ->
+ lists:flatten(io_lib:format(F, S)).
+
%%=============================================================================
%%
%% Utilities for the binary type
@@ -4131,6 +4881,11 @@ handle_base(Unit, Pos) when Pos >= 0 ->
handle_base(Unit, Neg) ->
(Unit+(Neg rem Unit)) rem Unit.
+family(L) ->
+ R = sofs:relation(L),
+ F = sofs:relation_to_family(R),
+ sofs:to_external(F).
+
%%=============================================================================
%% Consistency-testing function(s) below
%%=============================================================================
diff --git a/lib/hipe/doc/src/book.xml b/lib/hipe/doc/src/book.xml
index 9c95e3a827..438be134a2 100644
--- a/lib/hipe/doc/src/book.xml
+++ b/lib/hipe/doc/src/book.xml
@@ -1,10 +1,10 @@
-<?xml version="1.0" encoding="latin1" ?>
+<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE book SYSTEM "book.dtd">
<book xmlns:xi="http://www.w3.org/2001/XInclude">
<header titlestyle="normal">
<copyright>
- <year>2006</year><year>2010</year>
+ <year>2006</year><year>2013</year>
<holder>Ericsson AB. All Rights Reserved.</holder>
</copyright>
<legalnotice>
diff --git a/lib/hipe/doc/src/fascicules.xml b/lib/hipe/doc/src/fascicules.xml
index 28acc14624..b15610fa8b 100644
--- a/lib/hipe/doc/src/fascicules.xml
+++ b/lib/hipe/doc/src/fascicules.xml
@@ -1,4 +1,4 @@
-<?xml version="1.0" encoding="latin1" ?>
+<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE fascicules SYSTEM "fascicules.dtd">
<fascicules>
diff --git a/lib/hipe/doc/src/hipe_app.xml b/lib/hipe/doc/src/hipe_app.xml
index 0612392c3f..2ddce664cc 100644
--- a/lib/hipe/doc/src/hipe_app.xml
+++ b/lib/hipe/doc/src/hipe_app.xml
@@ -1,4 +1,4 @@
-<?xml version="1.0" encoding="latin1" ?>
+<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE appref SYSTEM "appref.dtd">
<appref>
diff --git a/lib/hipe/doc/src/notes.xml b/lib/hipe/doc/src/notes.xml
index e0b1622d19..c7faf733c7 100644
--- a/lib/hipe/doc/src/notes.xml
+++ b/lib/hipe/doc/src/notes.xml
@@ -30,6 +30,124 @@
</header>
<p>This document describes the changes made to HiPE.</p>
+<section><title>Hipe 3.10.3</title>
+
+ <section><title>Fixed Bugs and Malfunctions</title>
+ <list>
+ <item>
+ <p>
+ Fix compilation with 'no_remove_comments' (Thanks to
+ Johannes Weißl)</p>
+ <p>
+ Own Id: OTP-11564</p>
+ </item>
+ <item>
+ <p>
+ Application upgrade (appup) files are corrected for the
+ following applications: </p>
+ <p>
+ <c>asn1, common_test, compiler, crypto, debugger,
+ dialyzer, edoc, eldap, erl_docgen, et, eunit, gs, hipe,
+ inets, observer, odbc, os_mon, otp_mibs, parsetools,
+ percept, public_key, reltool, runtime_tools, ssh,
+ syntax_tools, test_server, tools, typer, webtool, wx,
+ xmerl</c></p>
+ <p>
+ A new test utility for testing appup files is added to
+ test_server. This is now used by most applications in
+ OTP.</p>
+ <p>
+ (Thanks to Tobias Schlager)</p>
+ <p>
+ Own Id: OTP-11744</p>
+ </item>
+ <item>
+ <p>
+ There is now a test suite for the Hipe application</p>
+ <p>
+ Own Id: OTP-11748</p>
+ </item>
+ <item>
+ <p>
+ Support for a LLVM backend has been added in HiPE</p>
+ <p>
+ Own Id: OTP-11801</p>
+ </item>
+ </list>
+ </section>
+
+
+ <section><title>Improvements and New Features</title>
+ <list>
+ <item>
+ <p> The pre-defined types <c>array/0</c>, <c>dict/0</c>,
+ <c>digraph/0</c>, <c>gb_set/0</c>, <c>gb_tree/0</c>,
+ <c>queue/0</c>, <c>set/0</c>, and <c>tid/0</c> have been
+ deprecated. They will be removed in Erlang/OTP 18.0. </p>
+ <p> Instead the types <c>array:array/0</c>,
+ <c>dict:dict/0</c>, <c>digraph:graph/0</c>,
+ <c>gb_set:set/0</c>, <c>gb_tree:tree/0</c>,
+ <c>queue:queue/0</c>, <c>sets:set/0</c>, and
+ <c>ets:tid/0</c> can be used. (Note: it has always been
+ necessary to use <c>ets:tid/0</c>.) </p> <p> It is
+ allowed in Erlang/OTP 17.0 to locally re-define the types
+ <c>array/0</c>, <c>dict/0</c>, and so on. </p> <p> New
+ types <c>array:array/1</c>, <c>dict:dict/2</c>,
+ <c>gb_sets:set/1</c>, <c>gb_trees:tree/2</c>,
+ <c>queue:queue/1</c>, and <c>sets:set/1</c> have been
+ added. </p> <p> A compiler option,
+ <c>nowarn_deprecated_type</c>, has been introduced. By
+ including the attribute </p> <c>
+ -compile(nowarn_deprecated_type).</c> <p> in an Erlang
+ source file, warnings about deprecated types can be
+ avoided in Erlang/OTP 17.0. </p> <p> The option can also
+ be given as a compiler flag: </p> <c> erlc
+ +nowarn_deprecated_type file.erl</c>
+ <p>
+ Own Id: OTP-10342</p>
+ </item>
+ <item>
+ <p>
+ EEP43: New data type - Maps</p>
+ <p>
+ With Maps you may for instance: <taglist> <item><c>M0 =
+ #{ a =&gt; 1, b =&gt; 2}, % create
+ associations</c></item> <item><c>M1 = M0#{ a := 10 }, %
+ update values</c></item> <item><c>M2 = M1#{ "hi" =&gt;
+ "hello"}, % add new associations</c></item> <item><c>#{
+ "hi" := V1, a := V2, b := V3} = M2. % match keys with
+ values</c></item> </taglist></p>
+ <p>
+ For information on how to use Maps please see the
+ <seealso marker="doc/reference_manual:maps">Reference
+ Manual</seealso>.</p>
+ <p>
+ The current implementation is without the following
+ features: <taglist> <item>No variable keys</item>
+ <item>No single value access</item> <item>No map
+ comprehensions</item> </taglist></p>
+ <p>
+ Note that Maps is <em>experimental</em> during OTP 17.0.</p>
+ <p>
+ Own Id: OTP-11616</p>
+ </item>
+ <item>
+ <p> Parameterized opaque types have been introduced. </p>
+ <p>
+ Own Id: OTP-11625</p>
+ </item>
+ <item>
+ <p>
+ Add support for the compilation of the is_map/1 and
+ map_size/1 guards to native code.</p>
+ <p>
+ Own Id: OTP-11831</p>
+ </item>
+ </list>
+ </section>
+
+</section>
+
<section><title>Hipe 3.10.2.2</title>
<section><title>Fixed Bugs and Malfunctions</title>
diff --git a/lib/hipe/doc/src/part_notes.xml b/lib/hipe/doc/src/part_notes.xml
index 8a3e82027b..f912fcfcdb 100644
--- a/lib/hipe/doc/src/part_notes.xml
+++ b/lib/hipe/doc/src/part_notes.xml
@@ -1,10 +1,10 @@
-<?xml version="1.0" encoding="latin1" ?>
+<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE part SYSTEM "part.dtd">
<part xmlns:xi="http://www.w3.org/2001/XInclude">
<header>
<copyright>
- <year>2006</year><year>2009</year>
+ <year>2006</year><year>2013</year>
<holder>Ericsson AB. All Rights Reserved.</holder>
</copyright>
<legalnotice>
diff --git a/lib/hipe/doc/src/ref_man.xml b/lib/hipe/doc/src/ref_man.xml
index bdafb61d08..0e1288b4c4 100644
--- a/lib/hipe/doc/src/ref_man.xml
+++ b/lib/hipe/doc/src/ref_man.xml
@@ -1,10 +1,10 @@
-<?xml version="1.0" encoding="latin1" ?>
+<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE application SYSTEM "application.dtd">
<application xmlns:xi="http://www.w3.org/2001/XInclude">
<header>
<copyright>
- <year>1996</year><year>2011</year>
+ <year>1996</year><year>2013</year>
<holder>Ericsson AB. All Rights Reserved.</holder>
</copyright>
<legalnotice>
diff --git a/lib/hipe/flow/cfg.hrl b/lib/hipe/flow/cfg.hrl
index 95bf5f7194..1f7a162f27 100644
--- a/lib/hipe/flow/cfg.hrl
+++ b/lib/hipe/flow/cfg.hrl
@@ -2,7 +2,7 @@
%%
%% %CopyrightBegin%
%%
-%% Copyright Ericsson AB 2007-2012. All Rights Reserved.
+%% Copyright Ericsson AB 2007-2014. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
@@ -42,12 +42,12 @@
%%
%% Data is a triple with a dict of constants, a list of labels and an integer
%%
--type cfg_data() :: {dict(), [cfg_lbl()], non_neg_integer()}.
+-type cfg_data() :: {dict:dict(), [cfg_lbl()], non_neg_integer()}.
%%
%% The following is to be used by other modules
%%
--record(cfg, {table = gb_trees:empty() :: gb_tree(),
+-record(cfg, {table = gb_trees:empty() :: gb_trees:tree(),
info :: #cfg_info{},
data :: cfg_data()}).
-type cfg() :: #cfg{}.
diff --git a/lib/hipe/flow/cfg.inc b/lib/hipe/flow/cfg.inc
index 62f399a81c..f0b1a75737 100644
--- a/lib/hipe/flow/cfg.inc
+++ b/lib/hipe/flow/cfg.inc
@@ -4,7 +4,7 @@
%%
%% %CopyrightBegin%
%%
-%% Copyright Ericsson AB 2001-2009. All Rights Reserved.
+%% Copyright Ericsson AB 2001-2014. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
@@ -534,7 +534,7 @@ breadth_list([], Vis, _CFG, BO) ->
{Vis, BO}.
-endif.
--spec none_visited() -> gb_set().
+-spec none_visited() -> gb_sets:set().
none_visited() ->
gb_sets:empty().
diff --git a/lib/hipe/flow/hipe_dominators.erl b/lib/hipe/flow/hipe_dominators.erl
index 1f2c830eaf..50d45c7c72 100644
--- a/lib/hipe/flow/hipe_dominators.erl
+++ b/lib/hipe/flow/hipe_dominators.erl
@@ -1,8 +1,8 @@
-%% -*- coding: utf-8; erlang-indent-level: 2 -*-
+%% -*- erlang-indent-level: 2 -*-
%%
%% %CopyrightBegin%
%%
-%% Copyright Ericsson AB 2004-2013. All Rights Reserved.
+%% Copyright Ericsson AB 2004-2014. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
@@ -57,7 +57,7 @@
-record(domTree, {root :: cfg_lbl(),
size = 0 :: non_neg_integer(),
- nodes = gb_trees:empty() :: gb_tree()}).
+ nodes = gb_trees:empty() :: gb_trees:tree()}).
-type domTree() :: #domTree{}.
%%>----------------------------------------------------------------------<
@@ -590,7 +590,7 @@ domTree_pp_children([], _) ->
%%
%%========================================================================
--type domFrontier() :: gb_tree().
+-type domFrontier() :: gb_trees:tree().
%%>----------------------------------------------------------------------<
%% Procedure : domFrontier_create
diff --git a/lib/hipe/flow/liveness.inc b/lib/hipe/flow/liveness.inc
index 9c5eaf3e68..6f161fb269 100644
--- a/lib/hipe/flow/liveness.inc
+++ b/lib/hipe/flow/liveness.inc
@@ -3,7 +3,7 @@
%%
%% %CopyrightBegin%
%%
-%% Copyright Ericsson AB 2001-2009. All Rights Reserved.
+%% Copyright Ericsson AB 2001-2014. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
@@ -71,7 +71,7 @@
%% The generic liveness analysis
%%
--spec analyze(cfg()) -> gb_tree().
+-spec analyze(cfg()) -> gb_trees:tree().
-ifdef(HIPE_LIVENESS_CALC_LARGEST_LIVESET).
analyze(CFG) ->
@@ -209,7 +209,7 @@ successors(L, Liveness) ->
{_GK, _LiveIn, Successors} = liveness_lookup(L, Liveness),
Successors.
--spec livein(gb_tree(), _) -> [_].
+-spec livein(gb_trees:tree(), _) -> [_].
livein(Liveness, L) ->
{_GK, LiveIn, _Successors} = liveness_lookup(L, Liveness),
diff --git a/lib/hipe/icode/hipe_beam_to_icode.erl b/lib/hipe/icode/hipe_beam_to_icode.erl
index 81249c958e..4691662f9f 100644
--- a/lib/hipe/icode/hipe_beam_to_icode.erl
+++ b/lib/hipe/icode/hipe_beam_to_icode.erl
@@ -509,6 +509,10 @@ trans_fun([{test,test_arity,{f,Lbl},[Reg,N]}|Instructions], Env) ->
I = hipe_icode:mk_type([trans_arg(Reg)],{tuple,N},
hipe_icode:label_name(True),map_label(Lbl)),
[I,True | trans_fun(Instructions,Env)];
+%%--- is_map ---
+trans_fun([{test,is_map,{f,Lbl},[Arg]}|Instructions], Env) ->
+ {Code,Env1} = trans_type_test(map,Lbl,Arg,Env),
+ [Code | trans_fun(Instructions,Env1)];
%%--------------------------------------------------------------------
%%--- select_val ---
trans_fun([{select_val,Reg,{f,Lbl},{list,Cases}}|Instructions], Env) ->
@@ -1121,6 +1125,49 @@ trans_fun([{trim,N,NY}|Instructions], Env) ->
trans_fun([{line,_}|Instructions], Env) ->
trans_fun(Instructions,Env);
%%--------------------------------------------------------------------
+%% Map instructions added in Spring 2014 (17.0).
+%%--------------------------------------------------------------------
+trans_fun([{test,has_map_fields,{f,Lbl},Map,{list,Keys}}|Instructions], Env) ->
+ {MapMove, MapVar, Env1} = mk_move_and_var(Map, Env),
+ %% We assume that hipe_icode:mk_call has no side-effects, and reuse
+ %% the help function of get_map_elements below, discarding the value
+ %% assignment instruction list.
+ {TestInstructions, _GetInstructions, Env2} =
+ trans_map_query(MapVar, map_label(Lbl), Env1,
+ lists:flatten([[K, {r, 0}] || K <- Keys])),
+ [MapMove, TestInstructions | trans_fun(Instructions, Env2)];
+trans_fun([{get_map_elements,{f,Lbl},Map,{list,KVPs}}|Instructions], Env) ->
+ {MapMove, MapVar, Env1} = mk_move_and_var(Map, Env),
+ {TestInstructions, GetInstructions, Env2} =
+ trans_map_query(MapVar, map_label(Lbl), Env1, KVPs),
+ [MapMove, TestInstructions, GetInstructions | trans_fun(Instructions, Env2)];
+%%--- put_map_assoc ---
+trans_fun([{put_map_assoc,{f,Lbl},Map,Dst,_N,{list,Pairs}}|Instructions], Env) ->
+ {MapMove, MapVar, Env1} = mk_move_and_var(Map, Env),
+ TempMapVar = mk_var(new),
+ TempMapMove = hipe_icode:mk_move(TempMapVar, MapVar),
+ {PutInstructions, Env2}
+ = case Lbl > 0 of
+ true ->
+ gen_put_map_instrs(exists, assoc, TempMapVar, Dst, Lbl, Pairs, Env1);
+ false ->
+ gen_put_map_instrs(new, assoc, TempMapVar, Dst, new, Pairs, Env1)
+ end,
+ [MapMove, TempMapMove, PutInstructions | trans_fun(Instructions, Env2)];
+%%--- put_map_exact ---
+trans_fun([{put_map_exact,{f,Lbl},Map,Dst,_N,{list,Pairs}}|Instructions], Env) ->
+ {MapMove, MapVar, Env1} = mk_move_and_var(Map, Env),
+ TempMapVar = mk_var(new),
+ TempMapMove = hipe_icode:mk_move(TempMapVar, MapVar),
+ {PutInstructions, Env2}
+ = case Lbl > 0 of
+ true ->
+ gen_put_map_instrs(exists, exact, TempMapVar, Dst, Lbl, Pairs, Env1);
+ false ->
+ gen_put_map_instrs(new, exact, TempMapVar, Dst, new, Pairs, Env1)
+ end,
+ [MapMove, TempMapMove, PutInstructions | trans_fun(Instructions, Env2)];
+%%--------------------------------------------------------------------
%%--- ERROR HANDLING ---
%%--------------------------------------------------------------------
trans_fun([X|_], _) ->
@@ -1500,6 +1547,102 @@ trans_type_test2(function2, Lbl, Arg, Arity, Env) ->
hipe_icode:label_name(True), map_label(Lbl)),
{[Move1,Move2,I,True],Env2}.
+%%
+%% Handles the get_map_elements instruction and the has_map_fields
+%% test instruction.
+%%
+trans_map_query(_MapVar, _FailLabel, Env, []) ->
+ {[], [], Env};
+trans_map_query(MapVar, FailLabel, Env, [Key,Val|KVPs]) ->
+ {Move,KeyVar,Env1} = mk_move_and_var(Key,Env),
+ PassLabel = mk_label(new),
+ BoolVar = hipe_icode:mk_new_var(),
+ ValVar = mk_var(Val),
+ IsKeyCall = hipe_icode:mk_call([BoolVar], maps, is_key, [KeyVar, MapVar],
+ remote),
+ TrueTest = hipe_icode:mk_if('=:=', [BoolVar, hipe_icode:mk_const(true)],
+ hipe_icode:label_name(PassLabel), FailLabel),
+ GetCall = hipe_icode:mk_call([ValVar], maps, get, [KeyVar, MapVar], remote),
+ {TestList, GetList, Env2} = trans_map_query(MapVar, FailLabel, Env1, KVPs),
+ {[Move, IsKeyCall, TrueTest, PassLabel|TestList], [GetCall|GetList], Env2}.
+
+%%
+%% Generates a fail label if necessary when translating put_map_* instructions.
+%%
+gen_put_map_instrs(exists, Op, TempMapVar, Dst, FailLbl, Pairs, Env) ->
+ TrueLabel = mk_label(new),
+ IsMapCode = hipe_icode:mk_type([TempMapVar], map,
+ hipe_icode:label_name(TrueLabel), map_label(FailLbl)),
+ DstMapVar = mk_var(Dst),
+ {ReturnLbl, PutInstructions, Env1}
+ = case Op of
+ assoc ->
+ trans_put_map_assoc(TempMapVar, DstMapVar, Pairs, Env, []);
+ exact ->
+ trans_put_map_exact(TempMapVar, DstMapVar,
+ map_label(FailLbl), Pairs, Env, [])
+ end,
+ {[IsMapCode, TrueLabel, PutInstructions, ReturnLbl], Env1};
+gen_put_map_instrs(new, Op, TempMapVar, Dst, new, Pairs, Env) ->
+ TrueLabel = mk_label(new),
+ FailLbl = mk_label(new),
+ IsMapCode = hipe_icode:mk_type([TempMapVar], map,
+ hipe_icode:label_name(TrueLabel),
+ hipe_icode:label_name(FailLbl)),
+ DstMapVar = mk_var(Dst),
+ {ReturnLbl, PutInstructions, Env1}
+ = case Op of
+ assoc ->
+ trans_put_map_assoc(TempMapVar, DstMapVar, Pairs, Env, []);
+ exact ->
+ trans_put_map_exact(TempMapVar, DstMapVar,
+ hipe_icode:label_name(FailLbl), Pairs, Env, [])
+ end,
+ Fail = hipe_icode:mk_fail([hipe_icode:mk_const(badarg)], error),
+ {[IsMapCode, TrueLabel, PutInstructions, FailLbl, Fail, ReturnLbl], Env1}.
+
+%%-----------------------------------------------------------------------
+%% This function generates the instructions needed to insert several
+%% (Key, Value) pairs into an existing map, each recursive call inserts
+%% one (Key, Value) pair.
+%%-----------------------------------------------------------------------
+trans_put_map_assoc(MapVar, DestMapVar, [], Env, Acc) ->
+ MoveToReturnVar = hipe_icode:mk_move(DestMapVar, MapVar),
+ ReturnLbl = mk_label(new),
+ GotoReturn = hipe_icode:mk_goto(hipe_icode:label_name(ReturnLbl)),
+ {ReturnLbl, lists:reverse([GotoReturn, MoveToReturnVar | Acc]), Env};
+trans_put_map_assoc(MapVar, DestMapVar, [Key, Value | Rest], Env, Acc) ->
+ {MoveKey, KeyVar, Env1} = mk_move_and_var(Key, Env),
+ {MoveVal, ValVar, Env2} = mk_move_and_var(Value, Env1),
+ BifCall = hipe_icode:mk_call([MapVar], maps, put,
+ [KeyVar, ValVar, MapVar], remote),
+ trans_put_map_assoc(MapVar, DestMapVar, Rest, Env2,
+ [BifCall, MoveVal, MoveKey | Acc]).
+
+%%-----------------------------------------------------------------------
+%% This function generates the instructions needed to update several
+%% (Key, Value) pairs in an existing map, each recursive call inserts
+%% one (Key, Value) pair.
+%%-----------------------------------------------------------------------
+trans_put_map_exact(MapVar, DestMapVar, _FLbl, [], Env, Acc) ->
+ MoveToReturnVar = hipe_icode:mk_move(DestMapVar, MapVar),
+ ReturnLbl = mk_label(new),
+ GotoReturn = hipe_icode:mk_goto(hipe_icode:label_name(ReturnLbl)),
+ {ReturnLbl, lists:reverse([GotoReturn, MoveToReturnVar | Acc]), Env};
+trans_put_map_exact(MapVar, DestMapVar, FLbl, [Key, Value | Rest], Env, Acc) ->
+ SuccLbl = mk_label(new),
+ {MoveKey, KeyVar, Env1} = mk_move_and_var(Key, Env),
+ {MoveVal, ValVar, Env2} = mk_move_and_var(Value, Env1),
+ IsKey = hipe_icode:mk_new_var(),
+ BifCallIsKey = hipe_icode:mk_call([IsKey], maps, is_key,
+ [KeyVar, MapVar], remote),
+ IsKeyTest = hipe_icode:mk_if('=:=', [IsKey, hipe_icode:mk_const(true)],
+ hipe_icode:label_name(SuccLbl), FLbl),
+ BifCallPut = hipe_icode:mk_call([MapVar], maps, put,
+ [KeyVar, ValVar, MapVar], remote),
+ Acc1 = [BifCallPut, SuccLbl, IsKeyTest, BifCallIsKey, MoveVal, MoveKey | Acc],
+ trans_put_map_exact(MapVar, DestMapVar, FLbl, Rest, Env2, Acc1).
+
%%-----------------------------------------------------------------------
%% trans_puts(Code, Environment) ->
%% {Movs, Code, Vars, NewEnv}
diff --git a/lib/hipe/icode/hipe_icode.erl b/lib/hipe/icode/hipe_icode.erl
index 6d4758bbf1..7b3d087e2d 100644
--- a/lib/hipe/icode/hipe_icode.erl
+++ b/lib/hipe/icode/hipe_icode.erl
@@ -503,7 +503,6 @@
enter_args_update/2,
enter_type/1,
is_enter/1,
-
mk_return/1, %% mk_return(Vars)
%% mk_fail/1, %% mk_fail(Args) class = exit
@@ -606,6 +605,12 @@
-export([highest_var/1, highest_label/1]).
+%%
+%% Exported types
+%%
+
+-export_type([icode/0]).
+
%%---------------------------------------------------------------------
%%
%% Icode
@@ -614,7 +619,7 @@
-spec mk_icode(mfa(), [icode_var()], boolean(), boolean(), [icode_instr()],
{non_neg_integer(),non_neg_integer()},
- {icode_lbl(),icode_lbl()}) -> #icode{}.
+ {icode_lbl(),icode_lbl()}) -> icode().
mk_icode(Fun, Params, IsClosure, IsLeaf, Code, VarRange, LabelRange) ->
#icode{'fun'=Fun, params=Params, code=Code,
is_closure=IsClosure,
@@ -1434,8 +1439,8 @@ subst1([_|Pairs], I) -> subst1(Pairs, I).
%%
%% @doc Returns the successors of an Icode instruction.
%% In CFG form only branch instructions have successors,
-%% but in linear form other instructions like e.g. moves and
-%% others might be the last instruction of some basic block.
+%% but in linear form other instructions like e.g. moves
+%% might be the last instruction of some basic block.
%%
-spec successors(icode_instr()) -> [icode_lbl()].
@@ -1464,6 +1469,7 @@ successors(I) ->
case fail_label(I) of [] -> []; L when is_integer(L) -> [L] end;
#icode_enter{} -> [];
#icode_return{} -> [];
+ #icode_comment{} -> [];
%% the following are included here for handling linear code
#icode_move{} -> [];
#icode_begin_handler{} -> []
diff --git a/lib/hipe/icode/hipe_icode.hrl b/lib/hipe/icode/hipe_icode.hrl
index 060493e61e..46c04beb40 100644
--- a/lib/hipe/icode/hipe_icode.hrl
+++ b/lib/hipe/icode/hipe_icode.hrl
@@ -1,7 +1,7 @@
%%
%% %CopyrightBegin%
%%
-%% Copyright Ericsson AB 2004-2013. All Rights Reserved.
+%% Copyright Ericsson AB 2004-2014. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
@@ -61,8 +61,8 @@
| 'op_exact_eqeq_2' | 'suspend_msg_timeout'.
-type icode_type_test() :: 'atom' | 'bignum' | 'binary' | 'bitstr' | 'boolean'
- | 'cons' | 'fixnum' | 'float'
- | 'function' | 'function2' | 'integer' | 'list' | 'nil'
+ | 'cons' | 'fixnum' | 'float' | 'function'
+ | 'function2' | 'integer' | 'list' | 'map' | 'nil'
| 'number' | 'pid' | 'port' | 'reference' | 'tuple'
| {'atom', atom()} | {'integer', integer()}
| {'record', atom(), non_neg_integer()}
@@ -108,7 +108,6 @@
length :: non_neg_integer(),
cases :: [icode_switch_case()]}).
-
-record(icode_type, {test :: icode_type_test(),
args :: [icode_term_arg()],
true_label :: icode_lbl(),
@@ -178,5 +177,6 @@
var_range :: {non_neg_integer(), non_neg_integer()},
label_range :: {icode_lbl(), icode_lbl()},
info = [] :: icode_info()}).
+-type icode() :: #icode{}.
%%---------------------------------------------------------------------
diff --git a/lib/hipe/icode/hipe_icode_callgraph.erl b/lib/hipe/icode/hipe_icode_callgraph.erl
index 5789328f47..ccf97ecc17 100644
--- a/lib/hipe/icode/hipe_icode_callgraph.erl
+++ b/lib/hipe/icode/hipe_icode_callgraph.erl
@@ -2,7 +2,7 @@
%%
%% %CopyrightBegin%
%%
-%% Copyright Ericsson AB 2004-2013. All Rights Reserved.
+%% Copyright Ericsson AB 2004-2014. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
@@ -46,7 +46,7 @@
-type mfa_icode() :: {mfa(), #icode{}}.
--record(icode_callgraph, {codedict :: dict(), ordered_sccs :: [[mfa()]]}).
+-record(icode_callgraph, {codedict :: dict:dict(), ordered_sccs :: [[mfa()]]}).
%%------------------------------------------------------------------------
%% Exported functions
diff --git a/lib/hipe/icode/hipe_icode_cfg.erl b/lib/hipe/icode/hipe_icode_cfg.erl
index 9b4a10e273..f6c2b0600b 100644
--- a/lib/hipe/icode/hipe_icode_cfg.erl
+++ b/lib/hipe/icode/hipe_icode_cfg.erl
@@ -3,7 +3,7 @@
%%
%% %CopyrightBegin%
%%
-%% Copyright Ericsson AB 2001-2009. All Rights Reserved.
+%% Copyright Ericsson AB 2001-2014. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
@@ -54,8 +54,8 @@
-spec postorder(cfg()) -> [icode_lbl()].
-spec reverse_postorder(cfg()) -> [icode_lbl()].
--spec is_visited(icode_lbl(), gb_set()) -> boolean().
--spec visit(icode_lbl(), gb_set()) -> gb_set().
+-spec is_visited(icode_lbl(), gb_sets:set()) -> boolean().
+-spec visit(icode_lbl(), gb_sets:set()) -> gb_sets:set().
-spec bb(cfg(), icode_lbl()) -> 'not_found' | bb().
-spec bb_add(cfg(), icode_lbl(), bb()) -> cfg().
diff --git a/lib/hipe/icode/hipe_icode_coordinator.erl b/lib/hipe/icode/hipe_icode_coordinator.erl
index 79e3304e6f..c69db9afa9 100644
--- a/lib/hipe/icode/hipe_icode_coordinator.erl
+++ b/lib/hipe/icode/hipe_icode_coordinator.erl
@@ -2,7 +2,7 @@
%%
%% %CopyrightBegin%
%%
-%% Copyright Ericsson AB 2007-2013. All Rights Reserved.
+%% Copyright Ericsson AB 2007-2014. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
@@ -49,9 +49,9 @@ coordinate(CG, Escaping, NonEscaping, Mod) ->
-type mfalists() :: {[mfa()], [mfa()]}.
--spec coordinate(mfalists(), hipe_digraph:hdg(), gb_tree(),
- fun((mfalists(), gb_tree()) -> mfalists()),
- fun((gb_tree()) -> 'ok'), pid()) -> no_return().
+-spec coordinate(mfalists(), hipe_digraph:hdg(), gb_trees:tree(),
+ fun((mfalists(), gb_trees:tree()) -> mfalists()),
+ fun((gb_trees:tree()) -> 'ok'), pid()) -> no_return().
coordinate(MFALists, CG, PM, Restart, LastAction, ServerPid) ->
case MFALists of
diff --git a/lib/hipe/icode/hipe_icode_exceptions.erl b/lib/hipe/icode/hipe_icode_exceptions.erl
index 00caffb24b..6191c536ad 100644
--- a/lib/hipe/icode/hipe_icode_exceptions.erl
+++ b/lib/hipe/icode/hipe_icode_exceptions.erl
@@ -2,7 +2,7 @@
%%
%% %CopyrightBegin%
%%
-%% Copyright Ericsson AB 2004-2011. All Rights Reserved.
+%% Copyright Ericsson AB 2004-2014. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
@@ -397,9 +397,9 @@ get_renaming(C, Map) ->
succ :: #cfg{},
pred :: #cfg{},
start_labels :: [icode_lbl(),...],
- visited = hipe_icode_cfg:none_visited() :: gb_set(),
- out = gb_trees:empty() :: gb_tree(),
- in = gb_trees:empty() :: gb_tree()
+ visited = hipe_icode_cfg:none_visited() :: gb_sets:set(),
+ out = gb_trees:empty() :: gb_trees:tree(),
+ in = gb_trees:empty() :: gb_trees:tree()
}).
init_state(CFG) ->
diff --git a/lib/hipe/icode/hipe_icode_fp.erl b/lib/hipe/icode/hipe_icode_fp.erl
index a2ca6132d1..38b3881a77 100644
--- a/lib/hipe/icode/hipe_icode_fp.erl
+++ b/lib/hipe/icode/hipe_icode_fp.erl
@@ -2,7 +2,7 @@
%%
%% %CopyrightBegin%
%%
-%% Copyright Ericsson AB 2003-2009. All Rights Reserved.
+%% Copyright Ericsson AB 2003-2014. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
@@ -33,8 +33,8 @@
-include("hipe_icode.hrl").
-include("../flow/cfg.hrl").
--record(state, {edge_map = gb_trees:empty() :: gb_tree(),
- fp_ebb_map = gb_trees:empty() :: gb_tree(),
+-record(state, {edge_map = gb_trees:empty() :: gb_trees:tree(),
+ fp_ebb_map = gb_trees:empty() :: gb_trees:tree(),
cfg :: #cfg{}}).
%%--------------------------------------------------------------------
@@ -424,7 +424,7 @@ redirect_phis([I|Is] = Code, OldFrom, NewFrom, Acc) ->
NewI = hipe_icode:phi_redirect_pred(I, OldFrom, NewFrom),
redirect_phis(Is, OldFrom, NewFrom, [NewI|Acc]);
_ ->
- lists:reverse(Acc) ++ Code
+ lists:reverse(Acc, Code)
end;
redirect_phis([], _OldFrom, _NewFrom, Acc) ->
lists:reverse(Acc).
diff --git a/lib/hipe/icode/hipe_icode_instruction_counter.erl b/lib/hipe/icode/hipe_icode_instruction_counter.erl
index 92658d294a..f44adfe149 100644
--- a/lib/hipe/icode/hipe_icode_instruction_counter.erl
+++ b/lib/hipe/icode/hipe_icode_instruction_counter.erl
@@ -2,7 +2,7 @@
%%
%% %CopyrightBegin%
%%
-%% Copyright Ericsson AB 2006-2009. All Rights Reserved.
+%% Copyright Ericsson AB 2006-2014. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
@@ -64,7 +64,8 @@ walktrough_bb(BB, Info) ->
%% The counter specific functions
%%-------------------------------------------------------------------
--spec compare(gb_tree(), gb_tree(), gb_tree()) -> gb_tree().
+-spec compare(gb_trees:tree(), gb_trees:tree(), gb_trees:tree()) ->
+ gb_trees:tree().
compare(Name, Old, New) ->
NewList = gb_trees:to_list(New),
diff --git a/lib/hipe/icode/hipe_icode_mulret.erl b/lib/hipe/icode/hipe_icode_mulret.erl
index 0bf9f89994..99522f6430 100644
--- a/lib/hipe/icode/hipe_icode_mulret.erl
+++ b/lib/hipe/icode/hipe_icode_mulret.erl
@@ -1,8 +1,8 @@
-%% -*- coding: utf-8; erlang-indent-level: 2 -*-
+%% -*- erlang-indent-level: 2 -*-
%%
%% %CopyrightBegin%
%%
-%% Copyright Ericsson AB 2005-2012. All Rights Reserved.
+%% Copyright Ericsson AB 2005-2013. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
@@ -1166,9 +1166,9 @@ printCallList([]) -> io:format("~n").
%% removeUnElems([#icode_call{'fun'={unsafe_element,_}, args=Var}|List], Var, Res) ->
%% removeUnElems(List, Var, Res);
%% removeUnElems([I=#icode_move{dst=Var}|List], [Var], Res) ->
-%% lists:reverse(Res) ++ [I|List];
+%% lists:reverse(Res, [I|List]);
%% removeUnElems([I=#icode_call{dstlist=Var}|List], Var, Res) ->
-%% lists:reverse(Res) ++ [I|List];
+%% lists:reverse(Res, [I|List]);
%% removeUnElems([I|List], Var, Res) ->
%% removeUnElems(List, Var, [I|Res]);
%% removeUnElems([], _, Res) -> lists:reverse(Res).
@@ -1187,7 +1187,7 @@ printCallList([]) -> io:format("~n").
%% false ->
%% case lists:member(Var, Defs) of
%% true ->
-%% lists:reverse(Res) ++ [I|List];
+%% lists:reverse(Res, [I|List]);
%% false ->
%% removeUnElems(List, Var, [I|Res])
%% end
@@ -1195,7 +1195,7 @@ printCallList([]) -> io:format("~n").
%% false ->
%% case lists:member(Var, Defs) of
%% true ->
-%% lists:reverse(Res) ++ [I|List];
+%% lists:reverse(Res, [I|List]);
%% false ->
%% removeUnElems(List, Var, [I|Res])
%% end
@@ -1203,7 +1203,7 @@ printCallList([]) -> io:format("~n").
%% false ->
%% case lists:member(Var, Defs) of
%% true ->
-%% lists:reverse(Res) ++ [I|List];
+%% lists:reverse(Res, [I|List]);
%% false ->
%% removeUnElems(List, Var, [I|Res])
%% end
@@ -1248,16 +1248,16 @@ printCallList([]) -> io:format("~n").
%% modifyCode([I|Code], Var, Res) ->
%% case scanInstr(I, Var) of
%% {move, Arity, VarLst} ->
-%% Code2 = [#icode_return{vars=VarLst}, I |lists:reverse(Res) ++ Code],
+%% Code2 = [#icode_return{vars=VarLst}, I |lists:reverse(Res, Code)],
%% {Arity, lists:reverse(Code2)};
%% {mktuple, Arity, VarLst} ->
-%% Code2 = [#icode_return{vars=VarLst}|lists:reverse(Res) ++ Code],
+%% Code2 = [#icode_return{vars=VarLst}|lists:reverse(Res, Code)],
%% {Arity, lists:reverse(Code2)};
%% other ->
%% modifyCode(Code, Var, [I|Res])
%% end;
%% modifyCode([], Var, Res) ->
-%% {1, lists:reverse(Res) ++ [#icode_return{vars=Var}]}.
+%% {1, lists:reverse(Res, [#icode_return{vars=Var}]}.
%% scanInstr(#icode_call{dstlist=Var, 'fun'=mktuple, args=Lst}, Var) ->
%% {mktuple, length(Lst), Lst};
diff --git a/lib/hipe/icode/hipe_icode_range.erl b/lib/hipe/icode/hipe_icode_range.erl
index 1a2cbfae31..fbc58f3568 100644
--- a/lib/hipe/icode/hipe_icode_range.erl
+++ b/lib/hipe/icode/hipe_icode_range.erl
@@ -2,7 +2,7 @@
%%
%% %CopyrightBegin%
%%
-%% Copyright Ericsson AB 2007-2013. All Rights Reserved.
+%% Copyright Ericsson AB 2007-2014. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
@@ -72,8 +72,8 @@
-type final_fun() :: fun((mfa(), [range()]) -> 'ok').
-type data() :: {mfa(), args_fun(), call_fun(), final_fun()}.
-type label() :: non_neg_integer().
--type info() :: gb_tree().
--type work_list() :: {[label()], [label()], set()}.
+-type info() :: gb_trees:tree().
+-type work_list() :: {[label()], [label()], sets:set()}.
-type variable() :: #icode_variable{}.
-type annotated_variable() :: #icode_variable{}.
-type argument() :: #icode_const{} | variable().
@@ -82,9 +82,9 @@
-type last_instr_return() :: {instr_split_info(), range()}.
-record(state, {info_map = gb_trees:empty() :: info(),
- counter = dict:new() :: dict(),
+ counter = dict:new() :: dict:dict(),
cfg :: cfg(),
- liveness = gb_trees:empty() :: gb_tree(),
+ liveness = gb_trees:empty() :: gb_trees:tree(),
ret_type :: range(),
lookup_fun :: call_fun(),
result_action :: final_fun()}).
diff --git a/lib/hipe/icode/hipe_icode_ssa.erl b/lib/hipe/icode/hipe_icode_ssa.erl
index 4607a96dda..2c4b6d9409 100644
--- a/lib/hipe/icode/hipe_icode_ssa.erl
+++ b/lib/hipe/icode/hipe_icode_ssa.erl
@@ -2,7 +2,7 @@
%%
%% %CopyrightBegin%
%%
-%% Copyright Ericsson AB 2002-2011. All Rights Reserved.
+%% Copyright Ericsson AB 2002-2014. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
@@ -37,7 +37,7 @@
-include("../ssa/hipe_ssa.inc").
%% Declarations for exported functions which are Icode-specific.
--spec ssa_liveness__analyze(#cfg{}) -> gb_tree().
+-spec ssa_liveness__analyze(#cfg{}) -> gb_trees:tree().
-spec ssa_liveness__livein(_, icode_lbl()) -> [#icode_variable{}].
%% -spec ssa_liveness__livein(_, icode_lbl(), _) -> [#icode_var{}].
diff --git a/lib/hipe/icode/hipe_icode_ssa_struct_reuse.erl b/lib/hipe/icode/hipe_icode_ssa_struct_reuse.erl
index 2337ef9323..772e30eada 100644
--- a/lib/hipe/icode/hipe_icode_ssa_struct_reuse.erl
+++ b/lib/hipe/icode/hipe_icode_ssa_struct_reuse.erl
@@ -2,7 +2,7 @@
%%
%% %CopyrightBegin%
%%
-%% Copyright Ericsson AB 2007-2013. All Rights Reserved.
+%% Copyright Ericsson AB 2007-2014. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
@@ -70,9 +70,9 @@
%% var - maps variables to expression value numbers. These variables are
%% defined or used by the structure expressions.
--record(maps, {var = gb_trees:empty() :: gb_tree(),
- instr = gb_trees:empty() :: gb_tree(),
- expr = gb_trees:empty() :: gb_tree()}).
+-record(maps, {var = gb_trees:empty() :: gb_trees:tree(),
+ instr = gb_trees:empty() :: gb_trees:tree(),
+ expr = gb_trees:empty() :: gb_trees:tree()}).
maps_var(#maps{var = Out}) -> Out.
maps_instr(#maps{instr = Out}) -> Out.
@@ -211,10 +211,10 @@ varinfo_use_add(#varinfo{use = UseSet} = I, Use) ->
pred = none :: 'none' | [icode_lbl()],
succ = none :: 'none' | [icode_lbl()],
code = [] :: [tuple()], % [illegal_icode_instr()]
- phi = gb_trees:empty() :: gb_tree(),
+ phi = gb_trees:empty() :: gb_trees:tree(),
varmap = [] :: [{icode_var(), icode_var()}],
pre_loop = false :: boolean(),
- non_struct_defs = gb_sets:new() :: gb_set(),
+ non_struct_defs = gb_sets:new() :: gb_sets:set(),
up_expr = none :: 'none' | ?SETS:?SET(_),
killed_expr = none :: 'none' | ?SETS:?SET(_),
sub_inserts = ?SETS:new() :: ?SETS:?SET(_),
@@ -319,7 +319,7 @@ node_create(Label, Pred, Succ) ->
start_label = none :: 'none' | icode_lbl(),
rev_postorder = none :: 'none' | [icode_lbl()],
all_expr = none :: 'none' | [non_neg_integer()],
- tree = gb_trees:empty() :: gb_tree()}).
+ tree = gb_trees:empty() :: gb_trees:tree()}).
nodes_postorder(#nodes{postorder = Out}) -> Out.
nodes_rev_postorder(#nodes{rev_postorder = Out}) -> Out.
@@ -356,7 +356,7 @@ nodes_create() -> #nodes{}.
%% del_red_test - flag that is set to true when the reduction test
%% has been inserted is used to move the reduction test.
--record(update, {inserted = gb_trees:empty() :: gb_tree(),
+-record(update, {inserted = gb_trees:empty() :: gb_trees:tree(),
del_red_test = false :: boolean()}).
update_inserted_lookup(#update{inserted = Inserted}, ExprId) ->
diff --git a/lib/hipe/icode/hipe_icode_type.erl b/lib/hipe/icode/hipe_icode_type.erl
index 046949d2f2..ebeb5e2c10 100644
--- a/lib/hipe/icode/hipe_icode_type.erl
+++ b/lib/hipe/icode/hipe_icode_type.erl
@@ -2,7 +2,7 @@
%%
%% %CopyrightBegin%
%%
-%% Copyright Ericsson AB 2003-2012. All Rights Reserved.
+%% Copyright Ericsson AB 2003-2014. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
@@ -84,22 +84,22 @@
t_fun/0, t_fun/1, t_fun/2, t_fun_args/1, t_fun_arity/1,
t_inf/2, t_inf_lists/2, t_integer/0,
t_integer/1, t_is_atom/1, t_is_any/1,
- t_is_binary/1, t_is_bitstr/1, t_is_bitwidth/1, t_is_boolean/1,
- t_is_fixnum/1, t_is_cons/1,
+ t_is_binary/1, t_is_bitstr/1, t_is_bitwidth/1,
+ t_is_boolean/1, t_is_fixnum/1, t_is_cons/1, t_is_map/1,
t_is_maybe_improper_list/1, t_is_equal/2, t_is_float/1,
t_is_fun/1, t_is_integer/1, t_is_non_neg_integer/1,
t_is_number/1, t_is_matchstate/1,
- t_is_nil/1, t_is_none/1, t_is_port/1, t_is_pid/1,
+ t_is_none/1, t_is_port/1, t_is_pid/1,
t_is_reference/1, t_is_subtype/2, t_is_tuple/1,
t_limit/2, t_matchstate_present/1, t_matchstate/0,
- t_matchstate_slots/1, t_maybe_improper_list/0,
+ t_matchstate_slots/1, t_maybe_improper_list/0, t_map/0,
t_nil/0, t_none/0, t_number/0, t_number/1, t_number_vals/1,
t_pid/0, t_port/0, t_reference/0, t_subtract/2, t_sup/2,
t_to_tlist/1, t_tuple/0, t_tuple/1, t_tuple_sizes/1]).
--record(state, {info_map = gb_trees:empty() :: gb_tree(),
+-record(state, {info_map = gb_trees:empty() :: gb_trees:tree(),
cfg :: cfg(),
- liveness = gb_trees:empty() :: gb_tree(),
+ liveness = gb_trees:empty() :: gb_trees:tree(),
arg_types :: [erl_types:erl_type()],
ret_type = [t_none()] :: [erl_types:erl_type()],
lookupfun :: call_fun(),
@@ -213,7 +213,7 @@ analyse_blocks(Work, State, MFA) ->
{NewState, NewLabels} =
try analyse_block(Label, Info, State)
catch throw:none_type ->
- %% io:format("received none type at label: ~p~n",[Label]),
+ %% io:format("received none type at label: ~p~n", [Label]),
{State,[]}
end,
NewWork2 = add_work(NewWork, NewLabels),
@@ -265,7 +265,7 @@ analyse_insn(I, Info, LookupFun) ->
do_move(I, Info);
#icode_call{} ->
NewInfo = do_call(I, Info, LookupFun),
- %%io:format("Analysing Call: ~w~n~w~n", [I,NewInfo]),
+ %% io:format("Analysing Call: ~w~n~w~n", [I, NewInfo]),
update_call_arguments(I, NewInfo);
#icode_phi{} ->
Type = t_limit(join_list(hipe_icode:args(I), Info), ?TYPE_DEPTH),
@@ -788,16 +788,16 @@ test_record(Atom, Size, Var, VarInfo, TrueLab, FalseLab, Info) ->
end.
test_type(Test, Type) ->
- %%io:format("Test is: ~w\n", [Test]),
- %%io:format("Type is: ~s\n", [format_type(Type)]),
+ %% io:format("Test is: ~w\n", [Test]),
+ %% io:format("Type is: ~s\n", [format_type(Type)]),
Ans =
case t_is_any(Type) of
true -> maybe;
false ->
TrueTest = true_branch_info(Test),
Inf = t_inf(TrueTest, Type),
- %%io:format("TrueTest is: ~s\n", [format_type(TrueTest)]),
- %%io:format("Inf is: ~s\n", [format_type(Inf)]),
+ %% io:format("TrueTest is: ~s\n", [format_type(TrueTest)]),
+ %% io:format("Inf is: ~s\n", [format_type(Inf)]),
case t_is_equal(Type, Inf) of
true ->
not t_is_none(Type);
@@ -895,11 +895,12 @@ test_type0(boolean, T) ->
t_is_boolean(T);
test_type0(list, T) ->
t_is_maybe_improper_list(T);
-test_type0(cons, T) ->
- t_is_cons(T);
-test_type0(nil, T) ->
- t_is_nil(T).
-
+%% test_type0(cons, T) ->
+%% t_is_cons(T);
+%% test_type0(nil, T) ->
+%% t_is_nil(T).
+test_type0(map, T) ->
+ t_is_map(T).
true_branch_info(integer) ->
t_integer();
@@ -931,22 +932,24 @@ true_branch_info(reference) ->
t_reference();
true_branch_info(function) ->
t_fun();
-true_branch_info(cons) ->
- t_cons();
-true_branch_info(nil) ->
- t_nil();
+%% true_branch_info(cons) ->
+%% t_cons();
+%% true_branch_info(nil) ->
+%% t_nil();
true_branch_info(boolean) ->
t_boolean();
+true_branch_info(map) ->
+ t_map();
true_branch_info(T) ->
- exit({?MODULE,unknown_typetest,T}).
+ exit({?MODULE, unknown_typetest, T}).
%% _________________________________________________________________
%%
%% Remove the redundant type tests. If a test is removed, the trace
-%% that isn't taken is explicitly removed from the CFG to simpilify
+%% that isn't taken is explicitly removed from the CFG to simplify
%% the handling of Phi nodes. If a Phi node is left and at least one
-%% branch into it has disappeared, the SSA propagation pass can't
+%% branch into it has disappeared, the SSA propagation pass cannot
%% handle it.
%%
%% If the CFG has changed at the end of this pass, the analysis is
diff --git a/lib/hipe/llvm/Makefile b/lib/hipe/llvm/Makefile
new file mode 100644
index 0000000000..92f378924a
--- /dev/null
+++ b/lib/hipe/llvm/Makefile
@@ -0,0 +1,109 @@
+#
+# %CopyrightBegin%
+#
+# Copyright Ericsson AB 2001-2014. All Rights Reserved.
+#
+# The contents of this file are subject to the Erlang Public License,
+# Version 1.1, (the "License"); you may not use this file except in
+# compliance with the License. You should have received a copy of the
+# Erlang Public License along with this software. If not, it can be
+# retrieved online at http://www.erlang.org/.
+#
+# Software distributed under the License is distributed on an "AS IS"
+# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
+# the License for the specific language governing rights and limitations
+# under the License.
+#
+# %CopyrightEnd%
+#
+
+ifndef EBIN
+EBIN = ../ebin
+endif
+
+include $(ERL_TOP)/make/target.mk
+include $(ERL_TOP)/make/$(TARGET)/otp.mk
+
+# ----------------------------------------------------
+# Application version
+# ----------------------------------------------------
+include ../vsn.mk
+VSN=$(HIPE_VSN)
+
+# ----------------------------------------------------
+# Release directory specification
+# ----------------------------------------------------
+RELSYSDIR = $(RELEASE_PATH)/lib/hipe-$(VSN)
+
+# ----------------------------------------------------
+# Target Specs
+# ----------------------------------------------------
+ifdef HIPE_ENABLED
+HIPE_MODULES = hipe_rtl_to_llvm \
+ hipe_llvm \
+ elf_format \
+ hipe_llvm_main \
+ hipe_llvm_merge \
+ hipe_llvm_liveness
+else
+HIPE_MODULES =
+endif
+
+MODULES = $(HIPE_MODULES)
+
+HRL_FILES= elf_format.hrl elf32_format.hrl elf64_format.hrl \
+ hipe_llvm_arch.hrl
+ERL_FILES= $(MODULES:%=%.erl)
+TARGET_FILES= $(MODULES:%=$(EBIN)/%.$(EMULATOR))
+
+# APP_FILE=
+# App_SRC= $(APP_FILE).src
+# APP_TARGET= $(EBIN)/$(APP_FILE)
+#
+# APPUP_FILE=
+# APPUP_SRC= $(APPUP_FILE).src
+# APPUP_TARGET= $(EBIN)/$(APPUP_FILE)
+
+# ----------------------------------------------------
+# FLAGS: Please keep +inline below
+# ----------------------------------------------------
+
+include ../native.mk
+
+ERL_COMPILE_FLAGS += +inline #+warn_missing_spec
+
+# if in 32 bit backend define BIT32 symbol
+ARCH = $(shell echo $(TARGET) | sed 's/^\(x86_64\)-.*/64bit/')
+ifneq ($(ARCH), 64bit)
+ERL_COMPILE_FLAGS += -DBIT32
+endif
+
+# ----------------------------------------------------
+# Targets
+# ----------------------------------------------------
+
+debug opt: $(TARGET_FILES)
+
+docs:
+
+clean:
+ rm -f $(TARGET_FILES)
+ rm -f core erl_crash.dump
+
+# ----------------------------------------------------
+# Special Build Targets
+# ----------------------------------------------------
+
+
+# ----------------------------------------------------
+# Release Target
+# ----------------------------------------------------
+include $(ERL_TOP)/make/otp_release_targets.mk
+
+release_spec: opt
+ $(INSTALL_DIR) $(RELSYSDIR)/llvm
+ $(INSTALL_DATA) $(ERL_FILES) $(HRL_FILES) $(RELSYSDIR)/llvm
+ $(INSTALL_DIR) $(RELSYSDIR)/ebin
+ $(INSTALL_DATA) $(TARGET_FILES) $(RELSYSDIR)/ebin
+
+release_docs_spec:
diff --git a/lib/hipe/llvm/elf32_format.hrl b/lib/hipe/llvm/elf32_format.hrl
new file mode 100644
index 0000000000..af1d95bf5b
--- /dev/null
+++ b/lib/hipe/llvm/elf32_format.hrl
@@ -0,0 +1,59 @@
+%% -*- erlang-indent-level: 2 -*-
+
+%%% @copyright 2011-2014 Yiannis Tsiouris <[email protected]>,
+%%% Chris Stavrakakis <[email protected]>
+%%% @author Yiannis Tsiouris <[email protected]>
+%%% [http://www.softlab.ntua.gr/~gtsiour/]
+
+%%% @doc This header file contains very very useful macros for handling
+%%% various segments of an ELF-32 formated object file, such as sizes,
+%%% offsets and predefined constants. For further information about
+%%% each field take a quick look at
+%%% "[http://www.sco.com/developers/gabi/latest/contents.html]"
+%%% that contain the current HP/Intel definition of the ELF object
+%%% file format.
+
+%%------------------------------------------------------------------------------
+%% ELF-32 Data Types (in bytes)
+%%------------------------------------------------------------------------------
+-define(ELF_ADDR_SIZE, 4).
+-define(ELF_OFF_SIZE, 4).
+-define(ELF_HALF_SIZE, 2).
+-define(ELF_WORD_SIZE, 4).
+-define(ELF_SWORD_SIZE, 4).
+-define(ELF_XWORD_SIZE, ?ELF_WORD_SIZE). % for compatibility
+-define(ELF_SXWORD_SIZE, ?ELF_WORD_SIZE).
+-define(ELF_UNSIGNED_CHAR_SIZE, 1).
+
+%%------------------------------------------------------------------------------
+%% ELF-32 Symbol Table Entries
+%%------------------------------------------------------------------------------
+%% Precomputed offset for Symbol Table entries in SymTab binary (needed because
+%% of the different offsets in 32 and 64 bit formats).
+-define(ST_NAME_OFFSET, 0).
+-define(ST_VALUE_OFFSET, (?ST_NAME_OFFSET + ?ST_NAME_SIZE) ).
+-define(ST_SIZE_OFFSET, (?ST_VALUE_OFFSET + ?ST_VALUE_SIZE) ).
+-define(ST_INFO_OFFSET, (?ST_SIZE_OFFSET + ?ST_SIZE_SIZE) ).
+-define(ST_OTHER_OFFSET, (?ST_INFO_OFFSET + ?ST_INFO_SIZE) ).
+-define(ST_SHNDX_OFFSET, (?ST_OTHER_OFFSET + ?ST_OTHER_SIZE) ).
+
+%%------------------------------------------------------------------------------
+%% ELF-64 Relocation Entries
+%%------------------------------------------------------------------------------
+%% Useful macros to extract information from r_info field
+-define(ELF_R_SYM(I), (I bsr 8) ).
+-define(ELF_R_TYPE(I), (I band 16#ff) ).
+-define(ELF_R_INFO(S, T), ((S bsl 8) + (T band 16#ff)) ).
+
+%%------------------------------------------------------------------------------
+%% ELF-64 Program Header Table
+%%------------------------------------------------------------------------------
+%% Offsets of various fields in a Program Header Table entry binary.
+-define(P_TYPE_OFFSET, 0).
+-define(P_OFFSET_OFFSET, (?P_FLAGS_OFFSET + ?P_FLAGS_SIZE) ).
+-define(P_VADDR_OFFSET, (?P_OFFSET_OFFSET + ?P_OFFSET_SIZE) ).
+-define(P_PADDR_OFFSET, (?P_VADDR_OFFSET + ?P_VADDR_SIZE) ).
+-define(P_FILESZ_OFFSET, (?P_PVADDR_OFFSET + ?P_PVADDR_SIZE) ).
+-define(P_MEMSZ_OFFSET, (?P_FILESZ_OFFSET + ?P_FILESZ_SIZE) ).
+-define(P_FLAGS_OFFSET, (?P_TYPE_OFFSET + ?P_TYPE_SIZE) ).
+-define(P_ALIGN_OFFSET, (?P_MEMSZ_OFFSET + ?P_MEMSZ_SIZE) ).
diff --git a/lib/hipe/llvm/elf64_format.hrl b/lib/hipe/llvm/elf64_format.hrl
new file mode 100644
index 0000000000..794746ffdc
--- /dev/null
+++ b/lib/hipe/llvm/elf64_format.hrl
@@ -0,0 +1,58 @@
+%% -*- erlang-indent-level: 2 -*-
+
+%%% @copyright 2011-2014 Yiannis Tsiouris <[email protected]>,
+%%% Chris Stavrakakis <[email protected]>
+%%% @author Yiannis Tsiouris <[email protected]>
+%%% [http://www.softlab.ntua.gr/~gtsiour/]
+
+%%% @doc This header file contains very very useful macros for handling
+%%% various segments of an ELF-64 formated object file, such as sizes,
+%%% offsets and predefined constants. For further information about
+%%% each field take a quick look at
+%%% "[http://downloads.openwatcom.org/ftp/devel/docs/elf-64-gen.pdf]"
+%%% that contain the current HP/Intel definition of the ELF object
+%%% file format.
+
+%%------------------------------------------------------------------------------
+%% ELF-64 Data Types (in bytes)
+%%------------------------------------------------------------------------------
+-define(ELF_ADDR_SIZE, 8).
+-define(ELF_OFF_SIZE, 8).
+-define(ELF_HALF_SIZE, 2).
+-define(ELF_WORD_SIZE, 4).
+-define(ELF_SWORD_SIZE, 4).
+-define(ELF_XWORD_SIZE, 8).
+-define(ELF_SXWORD_SIZE, 8).
+-define(ELF_UNSIGNED_CHAR_SIZE, 1).
+
+%%------------------------------------------------------------------------------
+%% ELF-64 Symbol Table Entries
+%%------------------------------------------------------------------------------
+%% Precomputed offset for Symbol Table entries in SymTab binary
+-define(ST_NAME_OFFSET, 0).
+-define(ST_INFO_OFFSET, (?ST_NAME_OFFSET + ?ST_NAME_SIZE) ).
+-define(ST_OTHER_OFFSET, (?ST_INFO_OFFSET + ?ST_INFO_SIZE) ).
+-define(ST_SHNDX_OFFSET, (?ST_OTHER_OFFSET + ?ST_OTHER_SIZE) ).
+-define(ST_VALUE_OFFSET, (?ST_SHNDX_OFFSET + ?ST_SHNDX_SIZE) ).
+-define(ST_SIZE_OFFSET, (?ST_VALUE_OFFSET + ?ST_VALUE_SIZE) ).
+
+%%------------------------------------------------------------------------------
+%% ELF-64 Relocation Entries
+%%------------------------------------------------------------------------------
+%% Useful macros to extract information from r_info field
+-define(ELF_R_SYM(I), (I bsr 32) ).
+-define(ELF_R_TYPE(I), (I band 16#ffffffff) ).
+-define(ELF_R_INFO(S, T), ((S bsl 32) + (T band 16#ffffffff)) ).
+
+%%------------------------------------------------------------------------------
+%% ELF-64 Program Header Table
+%%------------------------------------------------------------------------------
+%% Offsets of various fields in a Program Header Table entry binary.
+-define(P_TYPE_OFFSET, 0).
+-define(P_FLAGS_OFFSET, (?P_TYPE_OFFSET + ?P_TYPE_SIZE) ).
+-define(P_OFFSET_OFFSET, (?P_FLAGS_OFFSET + ?P_FLAGS_SIZE) ).
+-define(P_VADDR_OFFSET, (?P_OFFSET_OFFSET + ?P_OFFSET_SIZE) ).
+-define(P_PADDR_OFFSET, (?P_VADDR_OFFSET + ?P_VADDR_SIZE) ).
+-define(P_FILESZ_OFFSET, (?P_PVADDR_OFFSET + ?P_PVADDR_SIZE) ).
+-define(P_MEMSZ_OFFSET, (?P_FILESZ_OFFSET + ?P_FILESZ_SIZE) ).
+-define(P_ALIGN_OFFSET, (?P_MEMSZ_OFFSET + ?P_MEMSZ_SIZE) ).
diff --git a/lib/hipe/llvm/elf_format.erl b/lib/hipe/llvm/elf_format.erl
new file mode 100644
index 0000000000..260da9b5e6
--- /dev/null
+++ b/lib/hipe/llvm/elf_format.erl
@@ -0,0 +1,790 @@
+%% -*- erlang-indent-level: 2 -*-
+
+%%% @copyright 2011-2014 Yiannis Tsiouris <[email protected]>,
+%%% Chris Stavrakakis <[email protected]>,
+%%% Kostis Sagonas <[email protected]>
+%%% @author Yiannis Tsiouris <[email protected]>
+%%% [http://www.softlab.ntua.gr/~gtsiour/]
+
+%%% @doc This module contains functions for extracting various pieces of
+%%% information from an ELF formated Object file. To fully understand
+%%% the ELF format and the use of these functions please read
+%%% "[http://www.linuxjournal.com/article/1060?page=0,0]" carefully.
+
+-module(elf_format).
+
+-export([get_tab_entries/1,
+ %% Relocations
+ get_rodata_relocs/1,
+ get_text_relocs/1,
+ extract_rela/2,
+ get_rela_addends/1,
+ %% Note
+ extract_note/2,
+ %% Executable code
+ extract_text/1,
+ %% GCC Exception Table
+ get_exn_handlers/1,
+ %% Misc.
+ set_architecture_flag/1,
+ is64bit/0
+ ]).
+
+-include("elf_format.hrl").
+
+%%------------------------------------------------------------------------------
+%% Types
+%%------------------------------------------------------------------------------
+
+-type elf() :: binary().
+
+-type lp() :: non_neg_integer(). % landing pad
+-type num() :: non_neg_integer().
+-type index() :: non_neg_integer().
+-type offset() :: non_neg_integer().
+-type size() :: non_neg_integer().
+-type start() :: non_neg_integer().
+
+-type info() :: index().
+-type nameoff() :: offset().
+-type valueoff() :: offset().
+
+-type name() :: string().
+-type name_size() :: {name(), size()}.
+-type name_sizes() :: [name_size()].
+
+%%------------------------------------------------------------------------------
+%% Abstract Data Types and Accessors for ELF Structures.
+%%------------------------------------------------------------------------------
+
+%% File header
+-record(elf_ehdr, {ident, % ELF identification
+ type, % Object file type
+ machine, % Machine Type
+ version, % Object file version
+ entry, % Entry point address
+ phoff, % Program header offset
+ shoff :: offset(), % Section header offset
+ flags, % Processor-specific flags
+ ehsize :: size(), % ELF header size
+ phentsize :: size(), % Size of program header entry
+ phnum :: num(), % Number of program header entries
+ shentsize :: size(), % Size of section header entry
+ shnum :: num(), % Number of section header entries
+ shstrndx :: index() % Section name string table index
+ }).
+-type elf_ehdr() :: #elf_ehdr{}.
+
+-record(elf_ehdr_ident, {class, % File class
+ data, % Data encoding
+ version, % File version
+ osabi, % OS/ABI identification
+ abiversion, % ABI version
+ pad, % Start of padding bytes
+ nident % Size of e_ident[]
+ }).
+%% -type elf_ehdr_ident() :: #elf_ehdr_ident{}.
+
+%% Section header entries
+-record(elf_shdr, {name, % Section name
+ type, % Section type
+ flags, % Section attributes
+ addr, % Virtual address in memory
+ offset :: offset(), % Offset in file
+ size :: size(), % Size of section
+ link, % Link to other section
+ info, % Miscellaneous information
+ addralign, % Address align boundary
+ entsize % Size of entries, if section has table
+ }).
+%% -type elf_shdr() :: #elf_shdr{}.
+
+%% Symbol table entries
+-record(elf_sym, {name :: nameoff(), % Symbol name
+ info, % Type and Binding attributes
+ other, % Reserved
+ shndx, % Section table index
+ value :: valueoff(), % Symbol value
+ size :: size() % Size of object
+ }).
+-type elf_sym() :: #elf_sym{}.
+
+%% Relocations
+-record(elf_rel, {r_offset :: offset(), % Address of reference
+ r_info :: info() % Symbol index and type of relocation
+ }).
+-type elf_rel() :: #elf_rel{}.
+
+-record(elf_rela, {r_offset :: offset(), % Address of reference
+ r_info :: info(), % Symbol index and type of relocation
+ r_addend :: offset() % Constant part of expression
+ }).
+-type elf_rela() :: #elf_rela{}.
+
+%% %% Program header table
+%% -record(elf_phdr, {type, % Type of segment
+%% flags, % Segment attributes
+%% offset, % Offset in file
+%% vaddr, % Virtual address in memory
+%% paddr, % Reserved
+%% filesz, % Size of segment in file
+%% memsz, % Size of segment in memory
+%% align % Alignment of segment
+%% }).
+
+%% %% GCC exception table
+%% -record(elf_gccexntab, {lpbenc, % Landing pad base encoding
+%% lpbase, % Landing pad base
+%% ttenc, % Type table encoding
+%% ttoff, % Type table offset
+%% csenc, % Call-site table encoding
+%% cstabsize, % Call-site table size
+%% cstab :: cstab() % Call-site table
+%% }).
+%% -type elf_gccexntab() :: #elf_gccexntab{}.
+
+-record(elf_gccexntab_callsite, {start :: start(), % Call-site start
+ size :: size(), % Call-site size
+ lp :: lp(), % Call-site landing pad
+ % (exception handler)
+ onaction % On action (e.g. cleanup)
+ }).
+%% -type elf_gccexntab_callsite() :: #elf_gccexntab_callsite{}.
+
+%%------------------------------------------------------------------------------
+%% Accessor Functions
+%%------------------------------------------------------------------------------
+
+%% File header
+%% -spec mk_ehdr(...) -> elf_ehrd().
+mk_ehdr(Ident, Type, Machine, Version, Entry, Phoff, Shoff, Flags, Ehsize,
+ Phentsize, Phnum, Shentsize, Shnum, Shstrndx) ->
+ #elf_ehdr{ident = Ident, type = Type, machine = Machine, version = Version,
+ entry = Entry, phoff = Phoff, shoff = Shoff, flags = Flags,
+ ehsize = Ehsize, phentsize = Phentsize, phnum = Phnum,
+ shentsize = Shentsize, shnum = Shnum, shstrndx = Shstrndx}.
+
+%% -spec ehdr_shoff(elf_ehdr()) -> offset().
+%% ehdr_shoff(#elf_ehdr{shoff = Offset}) -> Offset.
+%%
+%% -spec ehdr_shentsize(elf_ehdr()) -> size().
+%% ehdr_shentsize(#elf_ehdr{shentsize = Size}) -> Size.
+%%
+%% -spec ehdr_shnum(elf_ehdr()) -> num().
+%% ehdr_shnum(#elf_ehdr{shnum = Num}) -> Num.
+%%
+%% -spec ehdr_shstrndx(elf_ehdr()) -> index().
+%% ehdr_shstrndx(#elf_ehdr{shstrndx = Index}) -> Index.
+
+
+%%-spec mk_ehdr_ident(...) -> elf_ehdr_ident().
+mk_ehdr_ident(Class, Data, Version, OsABI, AbiVersion, Pad, Nident) ->
+ #elf_ehdr_ident{class = Class, data = Data, version = Version, osabi = OsABI,
+ abiversion = AbiVersion, pad = Pad, nident = Nident}.
+
+%%%-------------------------
+%%% Section header entries
+%%%-------------------------
+mk_shdr(Name, Type, Flags, Addr, Offset, Size, Link, Info, AddrAlign, EntSize) ->
+ #elf_shdr{name = Name, type = Type, flags = Flags, addr = Addr,
+ offset = Offset, size = Size, link = Link, info = Info,
+ addralign = AddrAlign, entsize = EntSize}.
+
+%% -spec shdr_offset(elf_shdr()) -> offset().
+%% shdr_offset(#elf_shdr{offset = Offset}) -> Offset.
+%%
+%% -spec shdr_size(elf_shdr()) -> size().
+%% shdr_size(#elf_shdr{size = Size}) -> Size.
+
+%%%-------------------------
+%%% Symbol Table Entries
+%%%-------------------------
+mk_sym(Name, Info, Other, Shndx, Value, Size) ->
+ #elf_sym{name = Name, info = Info, other = Other,
+ shndx = Shndx, value = Value, size = Size}.
+
+-spec sym_name(elf_sym()) -> nameoff().
+sym_name(#elf_sym{name = Name}) -> Name.
+
+%% -spec sym_value(elf_sym()) -> valueoff().
+%% sym_value(#elf_sym{value = Value}) -> Value.
+%%
+%% -spec sym_size(elf_sym()) -> size().
+%% sym_size(#elf_sym{size = Size}) -> Size.
+
+%%%-------------------------
+%%% Relocations
+%%%-------------------------
+-spec mk_rel(offset(), info()) -> elf_rel().
+mk_rel(Offset, Info) ->
+ #elf_rel{r_offset = Offset, r_info = Info}.
+
+%% The following two functions capitalize on the fact that the two kinds of
+%% relocation records (for 32- and 64-bit architectures have similar structure.
+
+-spec r_offset(elf_rel() | elf_rela()) -> offset().
+r_offset(#elf_rel{r_offset = Offset}) -> Offset;
+r_offset(#elf_rela{r_offset = Offset}) -> Offset.
+
+-spec r_info(elf_rel() | elf_rela()) -> info().
+r_info(#elf_rel{r_info = Info}) -> Info;
+r_info(#elf_rela{r_info = Info}) -> Info.
+
+-spec mk_rela(offset(), info(), offset()) -> elf_rela().
+mk_rela(Offset, Info, Addend) ->
+ #elf_rela{r_offset = Offset, r_info = Info, r_addend = Addend}.
+
+-spec rela_addend(elf_rela()) -> offset().
+rela_addend(#elf_rela{r_addend = Addend}) -> Addend.
+
+%% %%%-------------------------
+%% %%% GCC exception table
+%% %%%-------------------------
+%% -type cstab() :: [elf_gccexntab_callsite()].
+%%
+%% mk_gccexntab(LPbenc, LPbase, TTenc, TToff, CSenc, CStabsize, CStab) ->
+%% #elf_gccexntab{lpbenc = LPbenc, lpbase = LPbase, ttenc = TTenc,
+%% ttoff = TToff, csenc = CSenc, cstabsize = CStabsize,
+%% cstab = CStab}.
+%%
+%% -spec gccexntab_cstab(elf_gccexntab()) -> cstab().
+%% gccexntab_cstab(#elf_gccexntab{cstab = CSTab}) -> CSTab.
+
+mk_gccexntab_callsite(Start, Size, LP, Action) ->
+ #elf_gccexntab_callsite{start = Start, size=Size, lp=LP, onaction=Action}.
+
+%% -spec gccexntab_callsite_start(elf_gccexntab_callsite()) -> start().
+%% gccexntab_callsite_start(#elf_gccexntab_callsite{start = Start}) -> Start.
+%%
+%% -spec gccexntab_callsite_size(elf_gccexntab_callsite()) -> size().
+%% gccexntab_callsite_size(#elf_gccexntab_callsite{size = Size}) -> Size.
+%%
+%% -spec gccexntab_callsite_lp(elf_gccexntab_callsite()) -> lp().
+%% gccexntab_callsite_lp(#elf_gccexntab_callsite{lp = LP}) -> LP.
+
+%%------------------------------------------------------------------------------
+%% Functions to manipulate the ELF File Header
+%%------------------------------------------------------------------------------
+
+%% @doc Extracts the File Header from an ELF formatted object file. Also sets
+%% the ELF class variable in the process dictionary (used by many functions
+%% in this and hipe_llvm_main modules).
+-spec extract_header(elf()) -> elf_ehdr().
+extract_header(Elf) ->
+ Ehdr_bin = get_binary_segment(Elf, 0, ?ELF_EHDR_SIZE),
+ << %% Structural pattern matching on fields.
+ Ident_bin:?E_IDENT_SIZE/binary,
+ Type:?bits(?E_TYPE_SIZE)/integer-little,
+ Machine:?bits(?E_MACHINE_SIZE)/integer-little,
+ Version:?bits(?E_VERSION_SIZE)/integer-little,
+ Entry:?bits(?E_ENTRY_SIZE)/integer-little,
+ Phoff:?bits(?E_PHOFF_SIZE)/integer-little,
+ Shoff:?bits(?E_SHOFF_SIZE)/integer-little,
+ Flags:?bits(?E_FLAGS_SIZE)/integer-little,
+ Ehsize:?bits(?E_EHSIZE_SIZE)/integer-little,
+ Phentsize:?bits(?E_PHENTSIZE_SIZE)/integer-little,
+ Phnum:?bits(?E_PHNUM_SIZE)/integer-little,
+ Shentsize:?bits(?E_SHENTSIZE_SIZE)/integer-little,
+ Shnum:?bits(?E_SHENTSIZE_SIZE)/integer-little,
+ Shstrndx:?bits(?E_SHSTRNDX_SIZE)/integer-little
+ >> = Ehdr_bin,
+ <<16#7f, $E, $L, $F, Class, Data, Version, Osabi, Abiversion,
+ Pad:6/binary, Nident
+ >> = Ident_bin,
+ Ident = mk_ehdr_ident(Class, Data, Version, Osabi,
+ Abiversion, Pad, Nident),
+ mk_ehdr(Ident, Type, Machine, Version, Entry, Phoff, Shoff, Flags,
+ Ehsize, Phentsize, Phnum, Shentsize, Shnum, Shstrndx).
+
+%%------------------------------------------------------------------------------
+%% Functions to manipulate Section Header Entries
+%%------------------------------------------------------------------------------
+
+%% @doc Extracts the Section Header Table from an ELF formated Object File.
+extract_shdrtab(Elf) ->
+ %% Extract File Header to get info about Section Header Offset (in bytes),
+ %% Entry Size (in bytes) and Number of entries
+ #elf_ehdr{shoff = ShOff, shentsize = ShEntsize, shnum = ShNum} =
+ extract_header(Elf),
+ %% Get actual Section header table (binary)
+ ShdrBin = get_binary_segment(Elf, ShOff, ShNum * ShEntsize),
+ get_shdrtab_entries(ShdrBin, []).
+
+get_shdrtab_entries(<<>>, Acc) ->
+ lists:reverse(Acc);
+get_shdrtab_entries(ShdrBin, Acc) ->
+ <<%% Structural pattern matching on fields.
+ Name:?bits(?SH_NAME_SIZE)/integer-little,
+ Type:?bits(?SH_TYPE_SIZE)/integer-little,
+ Flags:?bits(?SH_FLAGS_SIZE)/integer-little,
+ Addr:?bits(?SH_ADDR_SIZE)/integer-little,
+ Offset:?bits(?SH_OFFSET_SIZE)/integer-little,
+ Size:?bits(?SH_SIZE_SIZE)/integer-little,
+ Link:?bits(?SH_LINK_SIZE)/integer-little,
+ Info:?bits(?SH_INFO_SIZE)/integer-little,
+ Addralign:?bits(?SH_ADDRALIGN_SIZE)/integer-little,
+ Entsize:?bits(?SH_ENTSIZE_SIZE)/integer-little,
+ MoreShdrE/binary
+ >> = ShdrBin,
+ ShdrE = mk_shdr(Name, Type, Flags, Addr, Offset,
+ Size, Link, Info, Addralign, Entsize),
+ get_shdrtab_entries(MoreShdrE, [ShdrE | Acc]).
+
+%% @doc Extracts a specific Entry of a Section Header Table. This function
+%% takes as argument the Section Header Table (`SHdrTab') and the entry's
+%% serial number (`EntryNum') and returns the entry (`shdr').
+get_shdrtab_entry(SHdrTab, EntryNum) ->
+ lists:nth(EntryNum + 1, SHdrTab).
+
+%%------------------------------------------------------------------------------
+%% Functions to manipulate Section Header String Table
+%%------------------------------------------------------------------------------
+
+%% @doc Extracts the Section Header String Table. This section is not a known
+%% ELF Object File section. It is just a "hidden" table storing the
+%% names of all sections that exist in current object file.
+-spec extract_shstrtab(elf()) -> [name()].
+extract_shstrtab(Elf) ->
+ %% Extract Section Name String Table Index
+ #elf_ehdr{shstrndx = ShStrNdx} = extract_header(Elf),
+ ShHdrTab = extract_shdrtab(Elf),
+ %% Extract Section header entry and get actual Section-header String Table
+ #elf_shdr{offset = ShStrOffset, size = ShStrSize} =
+ get_shdrtab_entry(ShHdrTab, ShStrNdx),
+ case get_binary_segment(Elf, ShStrOffset, ShStrSize) of
+ <<>> -> %% Segment empty
+ [];
+ ShStrTab -> %% Convert to string table
+ [Name || {Name, _Size} <- get_names(ShStrTab)]
+ end.
+
+%%------------------------------------------------------------------------------
+
+-spec get_tab_entries(elf()) -> [{name(), valueoff(), size()}].
+get_tab_entries(Elf) ->
+ SymTab = extract_symtab(Elf),
+ Ts = [{Name, Value, Size div ?ELF_XWORD_SIZE}
+ || #elf_sym{name = Name, value = Value, size = Size} <- SymTab,
+ Name =/= 0],
+ {NameIndices, ValueOffs, Sizes} = lists:unzip3(Ts),
+ %% Find the names of the symbols.
+ %% Get string table entries ([{Name, Offset in strtab section}]). Keep only
+ %% relevant entries:
+ StrTab = extract_strtab(Elf),
+ Relevant = [get_strtab_entry(StrTab, Off) || Off <- NameIndices],
+ %% Zip back to {Name, ValueOff, Size}
+ lists:zip3(Relevant, ValueOffs, Sizes).
+
+%%------------------------------------------------------------------------------
+%% Functions to manipulate Symbol Table
+%%------------------------------------------------------------------------------
+
+%% @doc Function that extracts Symbol Table from an ELF Object file.
+extract_symtab(Elf) ->
+ Symtab_bin = extract_segment_by_name(Elf, ?SYMTAB),
+ get_symtab_entries(Symtab_bin, []).
+
+get_symtab_entries(<<>>, Acc) ->
+ lists:reverse(Acc);
+get_symtab_entries(Symtab_bin, Acc) ->
+ <<SymE_bin:?ELF_SYM_SIZE/binary, MoreSymE/binary>> = Symtab_bin,
+ case is64bit() of
+ true ->
+ <<%% Structural pattern matching on fields.
+ Name:?bits(?ST_NAME_SIZE)/integer-little,
+ Info:?bits(?ST_INFO_SIZE)/integer-little,
+ Other:?bits(?ST_OTHER_SIZE)/integer-little,
+ Shndx:?bits(?ST_SHNDX_SIZE)/integer-little,
+ Value:?bits(?ST_VALUE_SIZE)/integer-little,
+ Size:?bits(?ST_SIZE_SIZE)/integer-little
+ >> = SymE_bin;
+ false ->
+ << %% Same fields in different order:
+ Name:?bits(?ST_NAME_SIZE)/integer-little,
+ Value:?bits(?ST_VALUE_SIZE)/integer-little,
+ Size:?bits(?ST_SIZE_SIZE)/integer-little,
+ Info:?bits(?ST_INFO_SIZE)/integer-little,
+ Other:?bits(?ST_OTHER_SIZE)/integer-little,
+ Shndx:?bits(?ST_SHNDX_SIZE)/integer-little
+ >> = SymE_bin
+ end,
+ SymE = mk_sym(Name, Info, Other, Shndx, Value, Size),
+ get_symtab_entries(MoreSymE, [SymE | Acc]).
+
+%% @doc Extracts a specific entry from the Symbol Table (as binary).
+%% This function takes as arguments the Symbol Table (`SymTab')
+%% and the entry's serial number and returns that entry (`sym').
+get_symtab_entry(SymTab, EntryNum) ->
+ lists:nth(EntryNum + 1, SymTab).
+
+%%------------------------------------------------------------------------------
+%% Functions to manipulate String Table
+%%------------------------------------------------------------------------------
+
+%% @doc Extracts String Table from an ELF formated Object File.
+-spec extract_strtab(elf()) -> [{string(), offset()}].
+extract_strtab(Elf) ->
+ Strtab_bin = extract_segment_by_name(Elf, ?STRTAB),
+ NamesSizes = get_names(Strtab_bin),
+ make_offsets(NamesSizes).
+
+%% @doc Returns the name of the symbol at the given offset. The string table
+%% contains entries of the form {Name, Offset}. If no such offset exists
+%% returns the empty string (`""').
+%% XXX: There might be a bug here because of the "compact" saving the ELF
+%% format uses: e.g. only stores ".rela.text" for ".rela.text" and ".text".
+get_strtab_entry(Strtab, Offset) ->
+ case lists:keyfind(Offset, 2, Strtab) of
+ {Name, Offset} -> Name;
+ false -> ""
+ end.
+
+%%------------------------------------------------------------------------------
+%% Functions to manipulate Relocations
+%%------------------------------------------------------------------------------
+
+%% @doc This function gets as argument an ELF binary file and returns a list
+%% with all .rela.rodata labels (i.e. constants and literals in code)
+%% or an empty list if no ".rela.rodata" section exists in code.
+-spec get_rodata_relocs(elf()) -> [offset()].
+get_rodata_relocs(Elf) ->
+ case is64bit() of
+ true ->
+ %% Only care about the addends (== offsets):
+ get_rela_addends(extract_rela(Elf, ?RODATA));
+ false ->
+ %% Find offsets hardcoded in ".rodata" entry
+ %%XXX: Treat all 0s as padding and skip them!
+ [SkipPadding || SkipPadding <- extract_rodata(Elf), SkipPadding =/= 0]
+ end.
+
+-spec get_rela_addends([elf_rela()]) -> [offset()].
+get_rela_addends(RelaEntries) ->
+ [rela_addend(E) || E <- RelaEntries].
+
+%% @doc Extract a list of the form `[{SymbolName, Offset}]' with all relocatable
+%% symbols and their offsets in the code from the ".text" section.
+-spec get_text_relocs(elf()) -> [{name(), offset()}].
+get_text_relocs(Elf) ->
+ %% Only care about the symbol table index and the offset:
+ NameOffsetTemp = [{?ELF_R_SYM(r_info(E)), r_offset(E)}
+ || E <- extract_rela(Elf, ?TEXT)],
+ {NameIndices, ActualOffsets} = lists:unzip(NameOffsetTemp),
+ %% Find the names of the symbols:
+ %%
+ %% Get those symbol table entries that are related to Text relocs:
+ Symtab = extract_symtab(Elf),
+ SymtabEs = [get_symtab_entry(Symtab, Index) || Index <- NameIndices],
+ %XXX: not zero-indexed!
+ %% Symbol table entries contain the offset of the name of the symbol in
+ %% String Table:
+ SymtabEs2 = [sym_name(E) || E <- SymtabEs], %XXX: Do we need to sort SymtabE?
+ %% Get string table entries ([{Name, Offset in strtab section}]). Keep only
+ %% relevant entries:
+ Strtab = extract_strtab(Elf),
+ Relevant = [get_strtab_entry(Strtab, Off) || Off <- SymtabEs2],
+ %% Zip back with actual offsets:
+ lists:zip(Relevant, ActualOffsets).
+
+%% @doc Extract the Relocations segment for section `Name' (that is passed
+%% as second argument) from an ELF formated Object file binary.
+-spec extract_rela(elf(), name()) -> [elf_rel() | elf_rela()].
+extract_rela(Elf, Name) ->
+ SegName =
+ case is64bit() of
+ true -> ?RELA(Name); % ELF-64 uses ".rela"
+ false -> ?REL(Name) % ...while ELF-32 uses ".rel"
+ end,
+ Rela_bin = extract_segment_by_name(Elf, SegName),
+ get_rela_entries(Rela_bin, []).
+
+get_rela_entries(<<>>, Acc) ->
+ lists:reverse(Acc);
+get_rela_entries(Bin, Acc) ->
+ E = case is64bit() of
+ true ->
+ <<%% Structural pattern matching on fields of a Rela Entry.
+ Offset:?bits(?R_OFFSET_SIZE)/integer-little,
+ Info:?bits(?R_INFO_SIZE)/integer-little,
+ Addend:?bits(?R_ADDEND_SIZE)/integer-little,
+ Rest/binary
+ >> = Bin,
+ mk_rela(Offset, Info, Addend);
+ false ->
+ <<%% Structural pattern matching on fields of a Rel Entry.
+ Offset:?bits(?R_OFFSET_SIZE)/integer-little,
+ Info:?bits(?R_INFO_SIZE)/integer-little,
+ Rest/binary
+ >> = Bin,
+ mk_rel(Offset, Info)
+ end,
+ get_rela_entries(Rest, [E | Acc]).
+
+%% %% @doc Extract the `EntryNum' (serial number) Relocation Entry.
+%% get_rela_entry(Rela, EntryNum) ->
+%% lists:nth(EntryNum + 1, Rela).
+
+%%------------------------------------------------------------------------------
+%% Functions to manipulate Executable Code segment
+%%------------------------------------------------------------------------------
+
+%% @doc This function gets as arguments an ELF formated binary file and
+%% returns the Executable Code (".text" segment) or an empty binary if it
+%% is not found.
+-spec extract_text(elf()) -> binary().
+extract_text(Elf) ->
+ extract_segment_by_name(Elf, ?TEXT).
+
+%%------------------------------------------------------------------------------
+%% Functions to manipulate Note Section
+%%------------------------------------------------------------------------------
+
+%% @doc Extract specific Note Section from an ELF Object file. The function
+%% takes as first argument the object file (`Elf') and the `Name' of the
+%% wanted Note Section (<b>without</b> the ".note" prefix!). It returns
+%% the specified binary segment or an empty binary if no such section
+%% exists.
+-spec extract_note(elf(), string()) -> binary().
+extract_note(Elf, Name) ->
+ extract_segment_by_name(Elf, ?NOTE(Name)).
+
+%%------------------------------------------------------------------------------
+%% Functions to manipulate GCC Exception Table segment
+%%------------------------------------------------------------------------------
+
+%% A description for the C++ exception table formats can be found at Exception
+%% Handling Tables (http://www.codesourcery.com/cxx-abi/exceptions.pdf).
+
+%% A list with `{Start, End, HandlerOffset}' for all call sites in the code
+-spec get_exn_handlers(elf()) -> [{start(), start(), lp()}].
+get_exn_handlers(Elf) ->
+ CallSites = extract_gccexntab_callsites(Elf),
+ [{Start, Start + Size, LP}
+ || #elf_gccexntab_callsite{start = Start, size = Size, lp = LP} <- CallSites].
+
+%% @doc This function gets as argument an ELF binary file and returns
+%% the table (list) of call sites which is stored in GCC
+%% Exception Table (".gcc_except_table") section.
+%% It returns an empty list if the Exception Table is not found.
+%% XXX: Assumes there is *no* Action Record Table.
+extract_gccexntab_callsites(Elf) ->
+ case extract_segment_by_name(Elf, ?GCC_EXN_TAB) of
+ <<>> ->
+ [];
+ ExnTab ->
+ %% First byte of LSDA is Landing Pad base encoding.
+ <<LBenc:8, More/binary>> = ExnTab,
+ %% Second byte is the Landing Pad base (if its encoding is not
+ %% DW_EH_PE_omit) (optional).
+ {_LPBase, LSDACont} =
+ case LBenc =:= ?DW_EH_PE_omit of
+ true -> % No landing pad base byte. (-1 denotes that)
+ {-1, More};
+ false -> % Landing pad base.
+ <<Base:8, More2/binary>> = More,
+ {Base, More2}
+ end,
+ %% Next byte of LSDA is the encoding of the Type Table.
+ <<TTenc:8, More3/binary>> = LSDACont,
+ %% Next byte is the Types Table offset encoded in U-LEB128 (optional).
+ {_TTOff, LSDACont2} =
+ case TTenc =:= ?DW_EH_PE_omit of
+ true -> % There is no Types Table pointer. (-1 denotes that)
+ {-1, More3};
+ false -> % The byte offset from this field to the start of the Types
+ % Table used for exception matching.
+ leb128_decode(More3)
+ end,
+ %% Next byte of LSDA is the encoding of the fields in the Call-site Table.
+ <<_CSenc:8, More4/binary>> = LSDACont2,
+ %% Sixth byte is the size (in bytes) of the Call-site Table encoded in
+ %% U-LEB128.
+ {_CSTabSize, CSTab} = leb128_decode(More4),
+ %% Extract all call site information
+ get_gccexntab_callsites(CSTab, [])
+ end.
+
+get_gccexntab_callsites(<<>>, Acc) ->
+ lists:reverse(Acc);
+get_gccexntab_callsites(CSTab, Acc) ->
+ %% We are only interested in the Landing Pad of every entry.
+ <<Start:32/integer-little, Size:32/integer-little,
+ LP:32/integer-little, OnAction:8, More/binary
+ >> = CSTab,
+ GccCS = mk_gccexntab_callsite(Start, Size, LP, OnAction),
+ get_gccexntab_callsites(More, [GccCS | Acc]).
+
+%%------------------------------------------------------------------------------
+%% Functions to manipulate Read-only Data (.rodata)
+%%------------------------------------------------------------------------------
+extract_rodata(Elf) ->
+ Rodata_bin = extract_segment_by_name(Elf, ?RODATA),
+ get_rodata_entries(Rodata_bin, []).
+
+get_rodata_entries(<<>>, Acc) ->
+ lists:reverse(Acc);
+get_rodata_entries(Rodata_bin, Acc) ->
+ <<Num:?bits(?ELF_ADDR_SIZE)/integer-little, More/binary>> = Rodata_bin,
+ get_rodata_entries(More, [Num | Acc]).
+
+%%------------------------------------------------------------------------------
+%% Helper functions
+%%------------------------------------------------------------------------------
+
+%% @doc Returns the binary segment starting at `Offset' with length `Size'
+%% (bytes) from a binary file. If `Offset' is bigger than the byte size of
+%% the binary, an empty binary (`<<>>') is returned.
+-spec get_binary_segment(binary(), offset(), size()) -> binary().
+get_binary_segment(Bin, Offset, _Size) when Offset > byte_size(Bin) ->
+ <<>>;
+get_binary_segment(Bin, Offset, Size) ->
+ <<_Hdr:Offset/binary, BinSeg:Size/binary, _More/binary>> = Bin,
+ BinSeg.
+
+%% @doc This function gets as arguments an ELF formated binary object and
+%% a string with the segments' name and returns the specified segment or
+%% an empty binary (`<<>>') if there exists no segment with that name.
+%% There are handy macros defined in elf_format.hrl for all Standard
+%% Section Names.
+-spec extract_segment_by_name(elf(), string()) -> binary().
+extract_segment_by_name(Elf, SectionName) ->
+ %% Extract Section Header Table and Section Header String Table from binary
+ SHdrTable = extract_shdrtab(Elf),
+ Names = extract_shstrtab(Elf),
+ %% Zip to a list of (Name,ShdrE)
+ [_Zero | ShdrEs] = lists:keysort(2, SHdrTable), % Skip first entry (zeros).
+ L = lists:zip(Names, ShdrEs),
+ %% Find Section Header Table entry by name
+ case lists:keyfind(SectionName, 1, L) of
+ {SectionName, ShdrE} -> %% Note: Same name.
+ #elf_shdr{offset = Offset, size = Size} = ShdrE,
+ get_binary_segment(Elf, Offset, Size);
+ false -> %% Not found.
+ <<>>
+ end.
+
+%% @doc Extracts a list of strings with (zero-separated) names from a binary.
+%% Returns tuples of `{Name, Size}'.
+%% XXX: Skip trailing 0.
+-spec get_names(<<_:8,_:_*8>>) -> name_sizes().
+get_names(<<0, Bin/binary>>) ->
+ NamesSizes = get_names(Bin, []),
+ fix_names(NamesSizes, []).
+
+get_names(<<>>, Acc) ->
+ lists:reverse(Acc);
+get_names(Bin, Acc) ->
+ {Name, MoreNames} = bin_get_string(Bin),
+ get_names(MoreNames, [{Name, length(Name)} | Acc]).
+
+%% @doc Fix names:
+%% e.g. If ".rela.text" exists, ".text" does not. Same goes for
+%% ".rel.text". In that way, the Section Header String Table is more
+%% compact. Add ".text" just *before* the corresponding rela-field,
+%% etc.
+-spec fix_names(name_sizes(), name_sizes()) -> name_sizes().
+fix_names([], Acc) ->
+ lists:reverse(Acc);
+fix_names([{Name, Size}=T | Names], Acc) ->
+ case is64bit() of
+ true ->
+ case string:str(Name, ".rela") =:= 1 of
+ true -> %% Name starts with ".rela":
+ Section = string:substr(Name, 6),
+ fix_names(Names, [{Section, Size - 5}
+ | [T | Acc]]); % XXX: Is order ok? (".text"
+ % always before ".rela.text")
+ false -> %% Name does not start with ".rela":
+ fix_names(Names, [T | Acc])
+ end;
+ false ->
+ case string:str(Name, ".rel") =:= 1 of
+ true -> %% Name starts with ".rel":
+ Section = string:substr(Name, 5),
+ fix_names(Names, [{Section, Size - 4}
+ | [T | Acc]]); % XXX: Is order ok? (".text"
+ % always before ".rela.text")
+ false -> %% Name does not start with ".rel":
+ fix_names(Names, [T | Acc])
+ end
+ end.
+
+
+%% @doc A function that byte-reverses a binary. This might be needed because of
+%% little (fucking!) endianess.
+-spec bin_reverse(binary()) -> binary().
+bin_reverse(Bin) when is_binary(Bin) ->
+ bin_reverse(Bin, <<>>).
+
+-spec bin_reverse(binary(), binary()) -> binary().
+bin_reverse(<<>>, Acc) ->
+ Acc;
+bin_reverse(<<Head, More/binary>>, Acc) ->
+ bin_reverse(More, <<Head, Acc/binary>>).
+
+%% @doc A function that extracts a null-terminated string from a binary. It
+%% returns the found string along with the rest of the binary.
+-spec bin_get_string(binary()) -> {string(), binary()}.
+bin_get_string(Bin) ->
+ bin_get_string(Bin, <<>>).
+
+bin_get_string(<<>>, BinAcc) ->
+ Bin = bin_reverse(BinAcc), % little endian!
+ {binary_to_list(Bin), <<>>};
+bin_get_string(<<0, MoreBin/binary>>, BinAcc) ->
+ Bin = bin_reverse(BinAcc), % little endian!
+ {binary_to_list(Bin), MoreBin};
+bin_get_string(<<Letter, Tail/binary>>, BinAcc) ->
+ bin_get_string(Tail, <<Letter, BinAcc/binary>>).
+
+%% @doc
+make_offsets(NamesSizes) ->
+ {Names, Sizes} = lists:unzip(NamesSizes),
+ Offsets = make_offsets_from_sizes(Sizes, 1, []),
+ lists:zip(Names, Offsets).
+
+make_offsets_from_sizes([], _, Acc) ->
+ lists:reverse(Acc);
+make_offsets_from_sizes([Size | Sizes], Cur, Acc) ->
+ make_offsets_from_sizes(Sizes, Size+Cur+1, [Cur | Acc]). % For the "."!
+
+%% @doc Little-Endian Base 128 (LEB128) Decoder
+%% This function extracts the <b>first</b> LEB128-encoded integer in a
+%% binary and returns that integer along with the remaining binary. This is
+%% done because a LEB128 number has variable bit-size and that is a way of
+%% extracting only one number in a binary and continuing parsing the binary
+%% for other kind of data (e.g. different encoding).
+%% FIXME: Only decodes unsigned data!
+-spec leb128_decode(binary()) -> {integer(), binary()}.
+leb128_decode(LebNum) ->
+ leb128_decode(LebNum, 0, <<>>).
+
+-spec leb128_decode(binary(), integer(), binary()) -> {integer(), binary()}.
+leb128_decode(LebNum, NoOfBits, Acc) ->
+ <<Sentinel:1/bits, NextBundle:7/bits, MoreLebNums/bits>> = LebNum,
+ case Sentinel of
+ <<1:1>> -> % more bytes to follow
+ leb128_decode(MoreLebNums, NoOfBits+7, <<NextBundle:7/bits, Acc/bits>>);
+ <<0:1>> -> % byte bundle stop
+ Size = NoOfBits+7,
+ <<Num:Size/integer>> = <<NextBundle:7/bits, Acc/bits>>,
+ {Num, MoreLebNums}
+ end.
+
+%% @doc Extract ELF Class from ELF header and export symbol to process
+%% dictionary.
+-spec set_architecture_flag(elf()) -> 'ok'.
+set_architecture_flag(Elf) ->
+ %% Extract information about ELF Class from ELF Header
+ <<16#7f, $E, $L, $F, EI_Class, _MoreHeader/binary>>
+ = get_binary_segment(Elf, 0, ?ELF_EHDR_SIZE),
+ put(elf_class, EI_Class),
+ ok.
+
+%% @doc Read from object file header if the file class is ELF32 or ELF64.
+-spec is64bit() -> boolean().
+is64bit() ->
+ case get(elf_class) of
+ ?ELFCLASS64 -> true;
+ ?ELFCLASS32 -> false
+ end.
diff --git a/lib/hipe/llvm/elf_format.hrl b/lib/hipe/llvm/elf_format.hrl
new file mode 100644
index 0000000000..78592e6e2a
--- /dev/null
+++ b/lib/hipe/llvm/elf_format.hrl
@@ -0,0 +1,488 @@
+%% -*- erlang-indent-level: 2 -*-
+
+%%% @copyright 2011-2014 Yiannis Tsiouris <[email protected]>,
+%%% Chris Stavrakakis <[email protected]>
+%%% @author Yiannis Tsiouris <[email protected]>
+%%% [http://www.softlab.ntua.gr/~gtsiour/]
+
+%%------------------------------------------------------------------------------
+%%
+%% ELF Header File
+%%
+%%------------------------------------------------------------------------------
+
+-ifdef(BIT32).
+-include("elf32_format.hrl"). % ELF32-specific definitions.
+-else.
+-include("elf64_format.hrl"). % ELF64-specific definitions.
+-endif.
+
+%%------------------------------------------------------------------------------
+%% ELF Data Types (in bytes)
+%%------------------------------------------------------------------------------
+%%XXX: Included in either elf32_format or elf64_format.
+
+%%------------------------------------------------------------------------------
+%% ELF File Header
+%%------------------------------------------------------------------------------
+-define(ELF_EHDR_SIZE, (?E_IDENT_SIZE + ?E_TYPE_SIZE + ?E_MACHINE_SIZE
+ +?E_VERSION_SIZE + ?E_ENTRY_SIZE + ?E_PHOFF_SIZE
+ +?E_SHOFF_SIZE + ?E_FLAGS_SIZE + ?E_EHSIZE_SIZE
+ +?E_PHENTSIZE_SIZE + ?E_PHNUM_SIZE + ?E_SHENTSIZE_SIZE
+ +?E_SHNUM_SIZE + ?E_SHSTRNDX_SIZE) ).
+
+-define(E_IDENT_SIZE, (16 * ?ELF_UNSIGNED_CHAR_SIZE) ).
+-define(E_TYPE_SIZE, ?ELF_HALF_SIZE).
+-define(E_MACHINE_SIZE, ?ELF_HALF_SIZE).
+-define(E_VERSION_SIZE, ?ELF_WORD_SIZE).
+-define(E_ENTRY_SIZE, ?ELF_ADDR_SIZE).
+-define(E_PHOFF_SIZE, ?ELF_OFF_SIZE).
+-define(E_SHOFF_SIZE, ?ELF_OFF_SIZE).
+-define(E_FLAGS_SIZE, ?ELF_WORD_SIZE).
+-define(E_EHSIZE_SIZE, ?ELF_HALF_SIZE).
+-define(E_PHENTSIZE_SIZE, ?ELF_HALF_SIZE).
+-define(E_PHNUM_SIZE, ?ELF_HALF_SIZE).
+-define(E_SHENTSIZE_SIZE, ?ELF_HALF_SIZE).
+-define(E_SHNUM_SIZE, ?ELF_HALF_SIZE).
+-define(E_SHSTRNDX_SIZE, ?ELF_HALF_SIZE).
+
+%% Useful arithmetics for computing byte offsets for various File Header
+%% entries from a File Header (erlang) binary
+-define(E_IDENT_OFFSET, 0).
+-define(E_TYPE_OFFSET, (?E_IDENT_OFFSET + ?E_IDENT_SIZE) ).
+-define(E_MACHINE_OFFSET, (?E_TYPE_OFFSET + ?E_TYPE_SIZE) ).
+-define(E_VERSION_OFFSET, (?E_MACHINE_OFFSET + ?E_MACHINE_SIZE) ).
+-define(E_ENTRY_OFFSET, (?E_VERSION_OFFSET + ?E_VERSION_SIZE) ).
+-define(E_PHOFF_OFFSET, (?E_ENTRY_OFFSET + ?E_ENTRY_SIZE) ).
+-define(E_SHOFF_OFFSET, (?E_PHOFF_OFFSET + ?E_PHOFF_SIZE) ).
+-define(E_FLAGS_OFFSET, (?E_SHOFF_OFFSET + ?E_SHOFF_SIZE) ).
+-define(E_EHSIZE_OFFSET, (?E_FLAGS_OFFSET + ?E_FLAGS_SIZE) ).
+-define(E_PHENTSIZE_OFFSET, (?E_EHSIZE_OFFSET + ?E_EHSIZE_SIZE) ).
+-define(E_PHNUM_OFFSET, (?E_PHENTSIZE_OFFSET + ?E_PHENTSIZE_SIZE) ).
+-define(E_SHENTSIZE_OFFSET, (?E_PHNUM_OFFSET + ?E_PHNUM_SIZE) ).
+-define(E_SHNUM_OFFSET, (?E_SHENTSIZE_OFFSET + ?E_SHENTSIZE_SIZE) ).
+-define(E_SHSTRNDX_OFFSET, (?E_SHNUM_OFFSET + ?E_SHNUM_SIZE) ).
+
+%% Name aliases of File Header fields information used in get_header_field
+%% function of elf64_format module.
+-define(E_IDENT, {?E_IDENT_OFFSET, ?E_IDENT_SIZE}).
+-define(E_TYPE, {?E_TYPE_OFFSET, ?E_TYPE_SIZE}).
+-define(E_MACHINE, {?E_MACHINE_OFFSET, ?E_MACHINE_SIZE}).
+-define(E_VERSION, {?E_VERSION_OFFSET, ?E_VERSION_SIZE})
+-define(E_ENTRY, {?E_ENTRY_OFFSET, ?E_ENTRY_SIZE}).
+-define(E_PHOFF, {?E_PHOFF_OFFSET, ?E_PHOFF_SIZE}).
+-define(E_SHOFF, {?E_SHOFF_OFFSET, ?E_SHOFF_SIZE}).
+-define(E_FLAGS, {?E_FLAGS_OFFSET, ?E_FLAGS_SIZE}).
+-define(E_EHSIZE, {?E_EHSIZE_OFFSET, ?E_EHSIZE_SIZE}).
+-define(E_PHENTSIZE, {?E_PHENTSIZE_OFFSET, ?E_PHENTSIZE_SIZE}).
+-define(E_PHNUM, {?E_PHNUM_OFFSET, ?E_PHNUM_SIZE}).
+-define(E_SHENTSIZE, {?E_SHENTSIZE_OFFSET, ?E_SHENTSIZE_SIZE}).
+-define(E_SHNUM, {?E_SHNUM_OFFSET, ?E_SHNUM_SIZE}).
+-define(E_SHSTRNDX, {?E_SHSTRNDX_OFFSET, ?E_SHSTRNDX_SIZE}).
+
+%% ELF Identification (e_ident)
+-define(EI_MAG0, 0).
+-define(EI_MAG1, 1).
+-define(EI_MAG2, 2).
+-define(EI_MAG3, 3).
+-define(EI_CLASS, 4).
+-define(EI_DATA, 5).
+-define(EI_VERSION, 6).
+-define(EI_OSABI, 7).
+-define(EI_ABIVERSION, 8).
+-define(EI_PAD, 9).
+-define(EI_NIDENT, 16).
+
+%% Object File Classes (e_ident[EI_CLASS])
+-define(ELFCLASSNONE, 0).
+-define(ELFCLASS32, 1).
+-define(ELFCLASS64, 2).
+
+%% Data Encodings (e_ident[EI_DATA])
+-define(ELFDATA2LSB, 1).
+-define(ELFDATA2MSB, 2).
+
+%% Operating System and ABI Identifiers (e_ident[EI_OSABI])
+-define(ELFOSABI_SYSV, 0).
+-define(ELFOSABI_HPUX, 1).
+-define(ELFOSABI_STANDALONE, 255).
+
+%% Object File Types (e_type)
+-define(ET_NONE, 0).
+-define(ET_REL, 1).
+-define(ET_EXEC, 2).
+-define(ET_DYN, 3).
+-define(ET_CORE, 4).
+-define(ET_LOOS, 16#FE00).
+-define(ET_HIOS, 16#FEFF).
+-define(ET_LOPROC, 16#FF00).
+-define(ET_HIPROC, 16#FFFF).
+
+%%------------------------------------------------------------------------------
+%% ELF Section Header
+%%------------------------------------------------------------------------------
+-define(ELF_SHDRENTRY_SIZE, (?SH_NAME_SIZE + ?SH_TYPE_SIZE + ?SH_FLAGS_SIZE
+ +?SH_ADDR_SIZE + ?SH_OFFSET_SIZE + ?SH_SIZE_SIZE
+ +?SH_LINK_SIZE + ?SH_INFO_SIZE
+ +?SH_ADDRALIGN_SIZE + ?SH_ENTSIZE_SIZE) ).
+
+-define(SH_NAME_SIZE, ?ELF_WORD_SIZE).
+-define(SH_TYPE_SIZE, ?ELF_WORD_SIZE).
+-define(SH_FLAGS_SIZE, ?ELF_XWORD_SIZE).
+-define(SH_ADDR_SIZE, ?ELF_ADDR_SIZE).
+-define(SH_OFFSET_SIZE, ?ELF_OFF_SIZE).
+-define(SH_SIZE_SIZE, ?ELF_XWORD_SIZE).
+-define(SH_LINK_SIZE, ?ELF_WORD_SIZE).
+-define(SH_INFO_SIZE, ?ELF_WORD_SIZE).
+-define(SH_ADDRALIGN_SIZE, ?ELF_XWORD_SIZE).
+-define(SH_ENTSIZE_SIZE, ?ELF_XWORD_SIZE).
+
+%% Useful arithmetics for computing byte offsets for various fields from a
+%% Section Header Entry (erlang) binary
+-define(SH_NAME_OFFSET, 0).
+-define(SH_TYPE_OFFSET, (?SH_NAME_OFFSET + ?SH_NAME_SIZE) ).
+-define(SH_FLAGS_OFFSET, (?SH_TYPE_OFFSET + ?SH_TYPE_SIZE) ).
+-define(SH_ADDR_OFFSET, (?SH_FLAGS_OFFSET + ?SH_FLAGS_SIZE) ).
+-define(SH_OFFSET_OFFSET, (?SH_ADDR_OFFSET + ?SH_ADDR_SIZE) ).
+-define(SH_SIZE_OFFSET, (?SH_OFFSET_OFFSET + ?SH_OFFSET_SIZE) ).
+-define(SH_LINK_OFFSET, (?SH_SIZE_OFFSET + ?SH_SIZE_SIZE) ).
+-define(SH_INFO_OFFSET, (?SH_LINK_OFFSET + ?SH_LINK_SIZE) ).
+-define(SH_ADDRALIGN_OFFSET, (?SH_INFO_OFFSET + ?SH_INFO_SIZE) ).
+-define(SH_ENTSIZE_OFFSET, (?SH_ADDRALIGN_OFFSET + ?SH_ADDRALIGN_SIZE) ).
+
+%% Name aliases of Section Header Table entry information used in
+%% get_shdrtab_entry function of elf64_format module.
+-define(SH_NAME, {?SH_NAME_OFFSET, ?SH_NAME_SIZE}).
+-define(SH_TYPE, {?SH_TYPE_OFFSET, ?SH_TYPE_SIZE}).
+-define(SH_FLAGS, {?SH_FLAGS_OFFSET, ?SH_FLAGS_SIZE}).
+-define(SH_ADDR, {?SH_ADDR_OFFSET, ?SH_ADDR_SIZE}).
+-define(SH_OFFSET, {?SH_OFFSET_OFFSET, ?SH_OFFSET_SIZE}).
+-define(SH_SIZE, {?SH_SIZE_OFFSET, ?SH_SIZE_SIZE}).
+-define(SH_LINK, {?SH_LINK_OFFSET, ?SH_LINK_SIZE}).
+-define(SH_INFO, {?SH_INFO_OFFSET, ?SH_INFO_SIZE}).
+-define(SH_ADDRALIGN, {?SH_ADDRALIGN_OFFSET, ?SH_ADDRALIGN_SIZE}).
+-define(SH_ENTSIZE, {?SH_ENTSIZE_OFFSET, ?SH_ENTSIZE_SIZE}).
+
+%% Section Indices
+-define(SHN_UNDEF, 0).
+-define(SHN_LOPROC, 16#FF00).
+-define(SHN_HIPROC, 16#FF1F).
+-define(SHN_LOOS, 16#FF20).
+-define(SHN_HIOS, 16#FF3F).
+-define(SHN_ABS, 16#FFF1).
+-define(SHN_COMMON, 16#FFF2).
+
+%% Section Types (sh_type)
+-define(SHT_NULL, 0).
+-define(SHT_PROGBITS, 1).
+-define(SHT_SYMTAB, 2).
+-define(SHT_STRTAB, 3).
+-define(SHT_RELA, 4).
+-define(SHT_HASH, 5).
+-define(SHT_DYNAMIC, 6).
+-define(SHT_NOTE, 7).
+-define(SHT_NOBITS, 8).
+-define(SHT_REL, 9).
+-define(SHT_SHLIB, 10).
+-define(SHT_DYNSYM, 11).
+-define(SHT_LOOS, 16#60000000).
+-define(SHT_HIOS, 16#6FFFFFFF).
+-define(SHT_LOPROC, 16#70000000).
+-define(SHT_HIPROC, 16#7FFFFFFF).
+
+%% Section Attributes (sh_flags)
+-define(SHF_WRITE, 16#1).
+-define(SHF_ALLOC, 16#2).
+-define(SHF_EXECINSTR, 16#4).
+-define(SHF_MASKOS, 16#0F000000).
+-define(SHF_MASKPROC, 16#F0000000).
+
+%%
+%% Standard Section names for Code and Data
+%%
+-define(BSS, ".bss").
+-define(DATA, ".data").
+-define(INTERP, ".interp").
+-define(RODATA, ".rodata").
+-define(TEXT, ".text").
+%% Other Standard Section names
+-define(COMMENT, ".comment").
+-define(DYNAMIC, ".dynamic").
+-define(DYNSTR, ".dynstr").
+-define(GOT, ".got").
+-define(HASH, ".hash").
+-define(NOTE(Name), (".note" ++ Name)).
+-define(PLT, ".plt").
+-define(REL(Name), (".rel" ++ Name) ).
+-define(RELA(Name), (".rela" ++ Name) ).
+-define(SHSTRTAB, ".shstrtab").
+-define(STRTAB, ".strtab").
+-define(SYMTAB, ".symtab").
+-define(GCC_EXN_TAB, ".gcc_except_table").
+
+%%------------------------------------------------------------------------------
+%% ELF Symbol Table Entries
+%%------------------------------------------------------------------------------
+-define(ELF_SYM_SIZE, (?ST_NAME_SIZE + ?ST_INFO_SIZE + ?ST_OTHER_SIZE
+ +?ST_SHNDX_SIZE + ?ST_VALUE_SIZE + ?ST_SIZE_SIZE) ).
+
+-define(ST_NAME_SIZE, ?ELF_WORD_SIZE).
+-define(ST_INFO_SIZE, ?ELF_UNSIGNED_CHAR_SIZE).
+-define(ST_OTHER_SIZE, ?ELF_UNSIGNED_CHAR_SIZE).
+-define(ST_SHNDX_SIZE, ?ELF_HALF_SIZE).
+-define(ST_VALUE_SIZE, ?ELF_ADDR_SIZE).
+-define(ST_SIZE_SIZE, ?ELF_XWORD_SIZE).
+
+%% Precomputed offset for Symbol Table entries in SymTab binary
+%%XXX: Included in either elf32_format or elf64_format.
+
+%% Name aliases for Symbol Table entry information
+-define(ST_NAME, {?ST_NAME_OFFSET, ?ST_NAME_SIZE}).
+-define(ST_INFO, {?ST_INFO_OFFSET, ?ST_INFO_SIZE}).
+-define(ST_OTHER, {?ST_OTHER_OFFSET, ?ST_OTHER_SIZE}).
+-define(ST_SHNDX, {?ST_SHNDX_OFFSET, ?ST_SHNDX_SIZE}).
+-define(ST_VALUE, {?ST_VALUE_OFFSET, ?ST_VALUE_SIZE}).
+-define(ST_SIZE, {?ST_SIZE_OFFSET, ?ST_SIZE_SIZE}).
+
+%% Macros to extract information from st_type
+-define(ELF_ST_BIND(I), (I bsr 4) ).
+-define(ELF_ST_TYPE(I), (I band 16#f) ).
+-define(ELF_ST_INFO(B,T), (B bsl 4 + T band 16#f) ).
+
+%% Symbol Bindings
+-define(STB_LOCAL, 0).
+-define(STB_GLOBAL, 1).
+-define(STB_WEAK, 2).
+-define(STB_LOOS, 10).
+-define(STB_HIOS, 12).
+-define(STB_LOPROC, 13).
+-define(STB_HIPROC, 15).
+
+%% Symbol Types
+-define(STT_NOTYPE, 0).
+-define(STT_OBJECT, 1).
+-define(STT_FUNC, 2).
+-define(STT_SECTION, 3).
+-define(STT_FILE, 4).
+-define(STT_LOOS, 10).
+-define(STT_HIOS, 12).
+-define(STT_LOPROC, 13).
+-define(STT_HIPROC, 15).
+
+%%------------------------------------------------------------------------------
+%% ELF Relocation Entries
+%%------------------------------------------------------------------------------
+-define(ELF_REL_SIZE, (?R_OFFSET_SIZE + ?R_INFO_SIZE) ).
+-define(ELF_RELA_SIZE, (?R_OFFSET_SIZE + ?R_INFO_SIZE + ?R_ADDEND_SIZE) ).
+
+-define(R_OFFSET_SIZE, ?ELF_ADDR_SIZE).
+-define(R_INFO_SIZE, ?ELF_XWORD_SIZE).
+-define(R_ADDEND_SIZE, ?ELF_SXWORD_SIZE).
+
+%% Arithmetics for computing byte offsets in a Relocation entry binary
+-define(R_OFFSET_OFFSET, 0).
+-define(R_INFO_OFFSET, (?R_OFFSET_OFFSET + ?R_OFFSET_SIZE) ).
+-define(R_ADDEND_OFFSET, (?R_INFO_OFFSET + ?R_INFO_SIZE) ).
+
+%% Name aliases for Relocation field information
+-define(R_OFFSET, {?R_OFFSET_OFFSET, ?R_OFFSET_SIZE}).
+-define(R_INFO, {?R_INFO_OFFSET, ?R_INFO_SIZE}).
+-define(R_ADDEND, {?R_ADDEND_OFFSET, ?R_ADDEND_SIZE}).
+
+%% Useful macros to extract information from r_info field
+%%XXX: Included in either elf32_format or elf64_format.
+
+%%------------------------------------------------------------------------------
+%% ELF Program Header Table
+%%------------------------------------------------------------------------------
+-define(ELF_PHDR_SIZE, (?P_TYPE_SIZE + ?P_FLAGS_SIZE + ?P_OFFSET_SIZE
+ +?P_VADDR_SIZE + ?P_PADDR_SIZE + ?P_FILESZ_SIZE
+ +?P_MEMSZ_SIZE + ?P_ALIGN_SIZE) ).
+
+-define(P_TYPE_SIZE, ?ELF_WORD_SIZE).
+-define(P_FLAGS_SIZE, ?ELF_WORD_SIZE).
+-define(P_OFFSET_SIZE, ?ELF_OFF_SIZE).
+-define(P_VADDR_SIZE, ?ELF_ADDR_SIZE).
+-define(P_PADDR_SIZE, ?ELF_ADDR_SIZE).
+-define(P_FILESZ_SIZE, ?ELF_XWORD_SIZE).
+-define(P_MEMSZ_SIZE, ?ELF_XWORD_SIZE).
+-define(P_ALIGN_SIZE, ?ELF_XWORD_SIZE).
+
+%% Offsets of various fields in a Program Header Table entry binary.
+%%XXX: Included in either elf32_format or elf64_format.
+
+%% Name aliases for each Program Header Table entry field information.
+-define(P_TYPE, {?P_TYPE_OFFSET, ?P_TYPE_SIZE} ).
+-define(P_FLAGS, {?P_FLAGS_OFFSET, ?P_FLAGS_SIZE} ).
+-define(P_OFFSET, {?P_OFFSET_OFFSET, ?P_OFFSET_SIZE} ).
+-define(P_VADDR, {?P_VADDR_OFFSET, ?P_VADDR_SIZE} ).
+-define(P_PADDR, {?P_PADDR_OFFSET, ?P_PADDR_SIZE} ).
+-define(P_FILESZ, {?P_FILESZ_OFFSET, ?P_FILESZ_SIZE} ).
+-define(P_MEMSZ, {?P_MEMSZ_OFFSET, ?P_MEMSZ_SIZE} ).
+-define(P_ALIGN, {?P_ALIGN_OFFSET, ?P_ALIGN_SIZE} ).
+
+%% Segment Types (p_type)
+-define(PT_NULL, 0).
+-define(PT_LOAD, 1).
+-define(PT_DYNAMIC, 2).
+-define(PT_INTERP, 3).
+-define(PT_NOTE, 4).
+-define(PT_SHLIB, 5).
+-define(PT_PHDR, 6).
+-define(PT_LOOS, 16#60000000).
+-define(PT_HIOS, 16#6FFFFFFF).
+-define(PT_LOPROC, 16#70000000).
+-define(PT_HIPROC, 16#7FFFFFFF).
+
+%% Segment Attributes (p_flags)
+-define(PF_X, 16#1).
+-define(PF_W, 16#2).
+-define(PF_R, 16#4).
+-define(PF_MASKOS, 16#00FF0000).
+-define(PF_MASKPROC, 16#FF000000).
+
+%%------------------------------------------------------------------------------
+%% ELF Dynamic Table
+%%------------------------------------------------------------------------------
+-define(ELF_DYN_SIZE, (?D_TAG_SIZE + ?D_VAL_PTR_SIZE) ).
+
+-define(D_TAG_SIZE, ?ELF_SXWORD_SIZE).
+-define(D_VAL_PTR_SIZE, ?ELF_ADDR_SIZE).
+
+%% Offsets of each field in Dynamic Table entry in binary
+-define(D_TAG_OFFSET, 0).
+-define(D_VAL_PTR_OFFSET, (?D_TAG_OFFSET + ?D_TAG_SIZE)).
+
+%% Name aliases for each field of a Dynamic Table entry information
+-define(D_TAG, {?D_TAG_OFFSET, ?D_TAG_SIZE} ).
+-define(D_VAL_PTR, {?D_VAL_PTR_OFFSET, ?D_VAL_PTR_SIZE} ).
+
+%% Dynamic Table Entries
+-define(DT_NULL, 0).
+-define(DT_NEEDED, 1).
+-define(DT_PLTRELSZ, 2).
+-define(DT_PLTGOT, 3).
+-define(DT_HASH, 4).
+-define(DT_STRTAB, 5).
+-define(DT_SYMTAB, 6).
+-define(DT_RELA, 7).
+-define(DT_RELASZ, 8).
+-define(DT_RELAENT, 9).
+-define(DT_STRSZ, 10).
+-define(DT_SYMENT, 11).
+-define(DT_INIT, 12).
+-define(DT_FINI, 13).
+-define(DT_SONAME, 14).
+-define(DT_RPATH, 15).
+-define(DT_SYMBOLIC, 16).
+-define(DT_REL, 17).
+-define(DT_RELSZ, 18).
+-define(DT_RELENT, 19).
+-define(DT_PLTREL, 20).
+-define(DT_DEBUG, 21).
+-define(DT_TEXTREL, 22).
+-define(DT_JMPREL, 23).
+-define(DT_BIND_NOW, 24).
+-define(DT_INIT_ARRAY, 25).
+-define(DT_FINI_ARRAY, 26).
+-define(DT_INIT_ARRAYSZ, 27).
+-define(DT_FINI_ARRAYSZ, 28).
+-define(DT_LOOS, 16#60000000).
+-define(DT_HIOS, 16#6FFFFFFF).
+-define(DT_LOPROC, 16#700000000).
+-define(DT_HIPROC, 16#7FFFFFFFF).
+
+%%------------------------------------------------------------------------------
+%% ELF GCC Exception Table
+%%------------------------------------------------------------------------------
+
+%% The DWARF Exception Header Encoding is used to describe the type of data used
+%% in the .eh_frame_hdr (and .gcc_except_table) section. The upper 4 bits
+%% indicate how the value is to be applied. The lower 4 bits indicate the format
+%% of the data.
+
+%% DWARF Exception Header value format
+-define(DW_EH_PE_omit, 16#ff). % No value is present.
+-define(DW_EH_PE_uleb128, 16#01). % Unsigned value encoded using LEB128.
+-define(DW_EH_PE_udata2, 16#02). % A 2 bytes unsigned value.
+-define(DW_EH_PE_udata4, 16#03). % A 4 bytes unsigned value.
+-define(DW_EH_PE_udata8, 16#04). % An 8 bytes unsigned value.
+-define(DW_EH_PE_sleb128, 16#09). % Signed value encoded using LEB128.
+-define(DW_EH_PE_sdata2, 16#0a). % A 2 bytes signed value.
+-define(DW_EH_PE_sdata4, 16#0b). % A 4 bytes signed value.
+-define(DW_EH_PE_sdata8, 16#0c). % An 8 bytes signed value.
+
+%% DWARF Exception Header application
+-define(DW_EH_PE_absptr, 16#00). % Value is used with no modification.
+-define(DW_EH_PE_pcrel, 16#10). % Value is relative to the current PC.
+-define(DW_EH_PE_datarel, 16#30). % Value is relative to the beginning of the
+ % section.
+
+%%------------------------------------------------------------------------------
+%% ELF Read-only data (constants, literlas etc.)
+%%------------------------------------------------------------------------------
+-define(RO_ENTRY_SIZE, 8).
+
+%%------------------------------------------------------------------------------
+%% Custom Note section: ".note.gc" for Erlang GC
+%%------------------------------------------------------------------------------
+
+%% The structure of this section is the following:
+%%
+%% .short <n> # number of safe points in code
+%%
+%% .long .L<label1> # safe point address |
+%% .long .L<label2> # safe point address |-> safe point addrs
+%% ..... |
+%% .long .L<label3> # safe point address |
+%%
+%% .short <n> # stack frame size (in words) |-> fixed-size part
+%% .short <n> # stack arity |
+%% .short <n> # number of live roots that follow |
+%%
+%% .short <n> # live root's stack index |
+%% ..... |-> live root indices
+%% .short <n> # >> |
+
+%% The name of the custom Note Section
+-define(NOTE_ERLGC_NAME, ".gc").
+
+%% The first word of a Note Section for Erlang GC (".note.gc") is always the
+%% number of safepoints in code.
+-define(SP_COUNT, {?SP_COUNT_OFFSET, ?SP_COUNT_SIZE}).
+-define(SP_COUNT_SIZE, ?ELF_HALF_SIZE).
+-define(SP_COUNT_OFFSET, 0). %(always the first entry in sdesc)
+
+%% The fixed-size part of a safe point (SP) entry consists of 4 words: the SP
+%% address (offset in code), the stack frame size of the function (where the SP
+%% is located), the stack arity of the function (the registered values are *not*
+%% counted), the number of live roots in the specific SP.
+-define(SP_FIXED, {?SP_FIXED_OFF, ?SP_FIXED_SIZE}).
+-define(SP_FIXED_OFF, 0).
+%%XXX: Exclude SP_ADDR_SIZE from SP_FIXED_SIZE in lew of new GC layout
+-define(SP_FIXED_SIZE, (?SP_STKFRAME_SIZE + ?SP_STKARITY_SIZE
+ + ?SP_LIVEROOTCNT_SIZE)).
+
+-define(SP_ADDR_SIZE, ?ELF_WORD_SIZE).
+-define(SP_STKFRAME_SIZE, ?ELF_HALF_SIZE).
+-define(SP_STKARITY_SIZE, ?ELF_HALF_SIZE).
+-define(SP_LIVEROOTCNT_SIZE, ?ELF_HALF_SIZE).
+
+%%XXX: SP_STKFRAME is the first piece of information in the new GC layout
+-define(SP_STKFRAME_OFFSET, 0).
+-define(SP_STKARITY_OFFSET, (?SP_STKFRAME_OFFSET + ?SP_STKFRAME_SIZE) ).
+-define(SP_LIVEROOTCNT_OFFSET, (?SP_STKARITY_OFFSET + ?SP_STKARITY_SIZE) ).
+
+%% Name aliases for safepoint fields.
+-define(SP_STKFRAME, {?SP_STKFRAME_OFFSET, ?SP_STKFRAME_SIZE}).
+-define(SP_STKARITY, {?SP_STKARITY_OFFSET, ?SP_STKARITY_SIZE}).
+-define(SP_LIVEROOTCNT, {?SP_LIVEROOTCNT_OFFSET, ?SP_LIVEROOTCNT_SIZE}).
+
+%% After the fixed-size part a variable-size part exists. This part holds the
+%% stack frame index of every live root in the specific SP.
+-define(LR_STKINDEX_SIZE, ?ELF_HALF_SIZE).
+
+%%------------------------------------------------------------------------------
+%% Misc.
+%%------------------------------------------------------------------------------
+-define(bits(Bytes), ((Bytes) bsl 3)).
diff --git a/lib/hipe/llvm/hipe_llvm.erl b/lib/hipe/llvm/hipe_llvm.erl
new file mode 100644
index 0000000000..5e33731a2b
--- /dev/null
+++ b/lib/hipe/llvm/hipe_llvm.erl
@@ -0,0 +1,1131 @@
+%% -*- erlang-indent-level: 2 -*-
+
+-module(hipe_llvm).
+
+-export([
+ mk_ret/1,
+ ret_ret_list/1,
+
+ mk_br/1,
+ br_dst/1,
+
+ mk_br_cond/3,
+ mk_br_cond/4,
+ br_cond_cond/1,
+ br_cond_true_label/1,
+ br_cond_false_label/1,
+ br_cond_meta/1,
+
+ mk_indirectbr/3,
+ indirectbr_type/1,
+ indirectbr_address/1,
+ indirectbr_label_list/1,
+
+ mk_switch/4,
+ switch_type/1,
+ switch_value/1,
+ switch_default_label/1,
+ switch_value_label_list/1,
+
+ mk_invoke/9,
+ invoke_dst/1,
+ invoke_cconv/1,
+ invoke_ret_attrs/1,
+ invoke_type/1,
+ invoke_fnptrval/1,
+ invoke_arglist/1,
+ invoke_fn_attrs/1,
+ invoke_to_label/1,
+ invoke_unwind_label/1,
+
+ mk_operation/6,
+ operation_dst/1,
+ operation_op/1,
+ operation_type/1,
+ operation_src1/1,
+ operation_src2/1,
+ operation_options/1,
+
+ mk_extractvalue/5,
+ extractvalue_dst/1,
+ extractvalue_type/1,
+ extractvalue_val/1,
+ extractvalue_idx/1,
+ extractvalue_idxs/1,
+
+ mk_insertvalue/7,
+ insertvalue_dst/1,
+ insertvalue_val_type/1,
+ insertvalue_val/1,
+ insertvalue_elem_type/1,
+ insertvalue_elem/1,
+ insertvalue_idx/1,
+ insertvalue_idxs/1,
+
+ mk_alloca/4,
+ alloca_dst/1,
+ alloca_type/1,
+ alloca_num/1,
+ alloca_align/1,
+
+ mk_load/6,
+ load_dst/1,
+ load_p_type/1,
+ load_pointer/1,
+ load_alignment/1,
+ load_nontemporal/1,
+ load_volatile/1,
+
+ mk_store/7,
+ store_type/1,
+ store_value/1,
+ store_p_type/1,
+ store_pointer/1,
+ store_alignment/1,
+ store_nontemporal/1,
+ store_volatile/1,
+
+ mk_getelementptr/5,
+ getelementptr_dst/1,
+ getelementptr_p_type/1,
+ getelementptr_value/1,
+ getelementptr_typed_idxs/1,
+ getelementptr_inbounds/1,
+
+ mk_conversion/5,
+ conversion_dst/1,
+ conversion_op/1,
+ conversion_src_type/1,
+ conversion_src/1,
+ conversion_dst_type/1,
+
+ mk_sitofp/4,
+ sitofp_dst/1,
+ sitofp_src_type/1,
+ sitofp_src/1,
+ sitofp_dst_type/1,
+
+ mk_ptrtoint/4,
+ ptrtoint_dst/1,
+ ptrtoint_src_type/1,
+ ptrtoint_src/1,
+ ptrtoint_dst_type/1,
+
+ mk_inttoptr/4,
+ inttoptr_dst/1,
+ inttoptr_src_type/1,
+ inttoptr_src/1,
+ inttoptr_dst_type/1,
+
+ mk_icmp/5,
+ icmp_dst/1,
+ icmp_cond/1,
+ icmp_type/1,
+ icmp_src1/1,
+ icmp_src2/1,
+
+ mk_fcmp/5,
+ fcmp_dst/1,
+ fcmp_cond/1,
+ fcmp_type/1,
+ fcmp_src1/1,
+ fcmp_src2/1,
+
+ mk_phi/3,
+ phi_dst/1,
+ phi_type/1,
+ phi_value_label_list/1,
+
+ mk_select/6,
+ select_dst/1,
+ select_cond/1,
+ select_typ1/1,
+ select_val1/1,
+ select_typ2/1,
+ select_val2/1,
+
+ mk_call/8,
+ call_dst/1,
+ call_is_tail/1,
+ call_cconv/1,
+ call_ret_attrs/1,
+ call_type/1,
+ call_fnptrval/1,
+ call_arglist/1,
+ call_fn_attrs/1,
+
+ mk_fun_def/10,
+ fun_def_linkage/1,
+ fun_def_visibility/1,
+ fun_def_cconv/1,
+ fun_def_ret_attrs/1,
+ fun_def_type/1,
+ fun_def_name/1,
+ fun_def_arglist/1,
+ fun_def_fn_attrs/1,
+ fun_def_align/1,
+ fun_def_body/1,
+
+ mk_fun_decl/8,
+ fun_decl_linkage/1,
+ fun_decl_visibility/1,
+ fun_decl_cconv/1,
+ fun_decl_ret_attrs/1,
+ fun_decl_type/1,
+ fun_decl_name/1,
+ fun_decl_arglist/1,
+ fun_decl_align/1,
+
+ mk_landingpad/0,
+
+ mk_comment/1,
+ comment_text/1,
+
+ mk_label/1,
+ label_label/1,
+ is_label/1,
+
+ mk_const_decl/4,
+ const_decl_dst/1,
+ const_decl_decl_type/1,
+ const_decl_type/1,
+ const_decl_value/1,
+
+ mk_asm/1,
+ asm_instruction/1,
+
+ mk_adj_stack/3,
+ adj_stack_offset/1,
+ adj_stack_register/1,
+ adj_stack_type/1,
+
+ mk_branch_meta/3,
+ branch_meta_id/1,
+ branch_meta_true_weight/1,
+ branch_meta_false_weight/1
+ ]).
+
+-export([
+ mk_void/0,
+
+ mk_label_type/0,
+
+ mk_int/1,
+ int_width/1,
+
+ mk_double/0,
+
+ mk_pointer/1,
+ pointer_type/1,
+
+ mk_array/2,
+ array_size/1,
+ array_type/1,
+
+ mk_vector/2,
+ vector_size/1,
+ vector_type/1,
+
+ mk_struct/1,
+ struct_type_list/1,
+
+ mk_fun/2,
+ function_ret_type/1,
+ function_arg_type_list/1
+ ]).
+
+-export([pp_ins_list/2, pp_ins/2]).
+
+
+%%-----------------------------------------------------------------------------
+%% Abstract Data Types for LLVM Assembly.
+%%-----------------------------------------------------------------------------
+
+%% Terminator Instructions
+-record(llvm_ret, {ret_list=[]}).
+-type llvm_ret() :: #llvm_ret{}.
+
+-record(llvm_br, {dst}).
+-type llvm_br() :: #llvm_br{}.
+
+-record(llvm_br_cond, {'cond', true_label, false_label, meta=[]}).
+-type llvm_br_cond() :: #llvm_br_cond{}.
+
+-record(llvm_indirectbr, {type, address, label_list}).
+-type llvm_indirectbr() :: #llvm_indirectbr{}.
+
+-record(llvm_switch, {type, value, default_label, value_label_list=[]}).
+-type llvm_switch() :: #llvm_switch{}.
+
+-record(llvm_invoke, {dst, cconv=[], ret_attrs=[], type, fnptrval, arglist=[],
+ fn_attrs=[], to_label, unwind_label}).
+-type llvm_invoke() :: #llvm_invoke{}.
+
+%% Binary Operations
+-record(llvm_operation, {dst, op, type, src1, src2, options=[]}).
+-type llvm_operation() :: #llvm_operation{}.
+
+%% Aggregate Operations
+-record(llvm_extractvalue, {dst, type, val, idx, idxs=[]}).
+-type llvm_extractvalue() :: #llvm_extractvalue{}.
+
+-record(llvm_insertvalue, {dst, val_type, val, elem_type, elem, idx, idxs=[]}).
+-type llvm_insertvalue() :: #llvm_insertvalue{}.
+
+%% Memory Access and Addressing Operations
+-record(llvm_alloca, {dst, type, num=[], align=[]}).
+-type llvm_alloca() :: #llvm_alloca{}.
+
+-record(llvm_load, {dst, p_type, pointer, alignment=[], nontemporal=[],
+ volatile=false}).
+-type llvm_load() :: #llvm_load{}.
+
+-record(llvm_store, {type, value, p_type, pointer, alignment=[],
+ nontemporal=[], volatile=false}).
+-type llvm_store() :: #llvm_store{}.
+
+-record(llvm_getelementptr, {dst, p_type, value, typed_idxs, inbounds}).
+-type llvm_getelementptr() :: #llvm_getelementptr{}.
+
+%% Conversion Operations
+-record(llvm_conversion, {dst, op, src_type, src, dst_type}).
+-type llvm_conversion() :: #llvm_conversion{}.
+
+-record(llvm_sitofp, {dst, src_type, src, dst_type}).
+-type llvm_sitofp() :: #llvm_sitofp{}.
+
+-record(llvm_ptrtoint, {dst, src_type, src, dst_type}).
+-type llvm_ptrtoint() :: #llvm_ptrtoint{}.
+
+-record(llvm_inttoptr, {dst, src_type, src, dst_type}).
+-type llvm_inttoptr() :: #llvm_inttoptr{}.
+
+%% Other Operations
+-record(llvm_icmp, {dst, 'cond', type, src1, src2}).
+-type llvm_icmp() :: #llvm_icmp{}.
+
+-record(llvm_fcmp, {dst, 'cond', type, src1, src2}).
+-type llvm_fcmp() :: #llvm_fcmp{}.
+
+-record(llvm_phi, {dst, type, value_label_list}).
+-type llvm_phi() :: #llvm_phi{}.
+
+-record(llvm_select, {dst, 'cond', typ1, val1, typ2, val2}).
+-type llvm_select() :: #llvm_select{}.
+
+-record(llvm_call, {dst=[], is_tail = false, cconv = [], ret_attrs = [], type,
+ fnptrval, arglist = [], fn_attrs = []}).
+-type llvm_call() :: #llvm_call{}.
+
+-record(llvm_fun_def, {linkage=[], visibility=[], cconv=[], ret_attrs=[],
+ type, 'name', arglist=[], fn_attrs=[], align=[], body=[]}).
+-type llvm_fun_def() :: #llvm_fun_def{}.
+
+-record(llvm_fun_decl, {linkage=[], visibility=[], cconv=[], ret_attrs=[],
+ type, 'name', arglist=[], align=[]}).
+-type llvm_fun_decl() :: #llvm_fun_decl{}.
+
+-record(llvm_landingpad, {}).
+-type llvm_landingpad() :: #llvm_landingpad{}.
+
+-record(llvm_comment, {text}).
+-type llvm_comment() :: #llvm_comment{}.
+
+-record(llvm_label, {label}).
+-type llvm_label() :: #llvm_label{}.
+
+-record(llvm_const_decl, {dst, decl_type, type, value}).
+-type llvm_const_decl() :: #llvm_const_decl{}.
+
+-record(llvm_asm, {instruction}).
+-type llvm_asm() :: #llvm_asm{}.
+
+-record(llvm_adj_stack, {offset, 'register', type}).
+-type llvm_adj_stack() :: #llvm_adj_stack{}.
+
+-record(llvm_branch_meta, {id, true_weight, false_weight}).
+-type llvm_branch_meta() :: #llvm_branch_meta{}.
+
+%% A type for any LLVM instruction
+-type llvm_instr() :: llvm_ret() | llvm_br() | llvm_br_cond()
+ | llvm_indirectbr() | llvm_switch() | llvm_invoke()
+ | llvm_operation() | llvm_extractvalue()
+ | llvm_insertvalue() | llvm_alloca() | llvm_load()
+ | llvm_store() | llvm_getelementptr() | llvm_conversion()
+ | llvm_sitofp() | llvm_ptrtoint() | llvm_inttoptr()
+ | llvm_icmp() | llvm_fcmp() | llvm_phi() | llvm_select()
+ | llvm_call() | llvm_fun_def() | llvm_fun_decl()
+ | llvm_landingpad() | llvm_comment() | llvm_label()
+ | llvm_const_decl() | llvm_asm() | llvm_adj_stack()
+ | llvm_branch_meta().
+
+%% Types
+-record(llvm_void, {}).
+%-type llvm_void() :: #llvm_void{}.
+
+-record(llvm_label_type, {}).
+%-type llvm_label_type() :: #llvm_label_type{}.
+
+-record(llvm_int, {width}).
+%-type llvm_int() :: #llvm_int{}.
+
+-record(llvm_float, {}).
+%-type llvm_float() :: #llvm_float{}.
+
+-record(llvm_double, {}).
+%-type llvm_double() :: #llvm_double{}.
+
+-record(llvm_fp80, {}).
+%-type llvm_fp80() :: #llvm_fp80{}.
+
+-record(llvm_fp128, {}).
+%-type llvm_fp128() :: #llvm_fp128{}.
+
+-record(llvm_ppc_fp128, {}).
+%-type llvm_ppc_fp128() :: #llvm_ppc_fp128{}.
+
+-record(llvm_pointer, {type}).
+%-type llvm_pointer() :: #llvm_pointer{}.
+
+-record(llvm_vector, {'size', type}).
+%-type llvm_vector() :: #llvm_vector{}.
+
+-record(llvm_struct, {type_list}).
+%-type llvm_struct() :: #llvm_struct{}.
+
+-record(llvm_array, {'size', type}).
+%-type llvm_array() :: #llvm_array{}.
+
+-record(llvm_fun, {ret_type, arg_type_list}).
+%-type llvm_fun() :: #llvm_fun{}.
+
+%%-----------------------------------------------------------------------------
+%% Accessor Functions
+%%-----------------------------------------------------------------------------
+
+%% ret
+mk_ret(Ret_list) -> #llvm_ret{ret_list=Ret_list}.
+ret_ret_list(#llvm_ret{ret_list=Ret_list}) -> Ret_list.
+
+%% br
+mk_br(Dst) -> #llvm_br{dst=Dst}.
+br_dst(#llvm_br{dst=Dst}) -> Dst.
+
+%% br_cond
+mk_br_cond(Cond, True_label, False_label) ->
+ #llvm_br_cond{'cond'=Cond, true_label=True_label, false_label=False_label}.
+mk_br_cond(Cond, True_label, False_label, Metadata) ->
+ #llvm_br_cond{'cond'=Cond, true_label=True_label, false_label=False_label,
+ meta=Metadata}.
+br_cond_cond(#llvm_br_cond{'cond'=Cond}) -> Cond.
+br_cond_true_label(#llvm_br_cond{true_label=True_label}) -> True_label.
+br_cond_false_label(#llvm_br_cond{false_label=False_label}) ->
+ False_label.
+br_cond_meta(#llvm_br_cond{meta=Metadata}) -> Metadata.
+
+%% indirectbr
+mk_indirectbr(Type, Address, Label_list) -> #llvm_indirectbr{type=Type, address=Address, label_list=Label_list}.
+indirectbr_type(#llvm_indirectbr{type=Type}) -> Type.
+indirectbr_address(#llvm_indirectbr{address=Address}) -> Address.
+indirectbr_label_list(#llvm_indirectbr{label_list=Label_list}) -> Label_list.
+
+%% invoke
+mk_invoke(Dst, Cconv, Ret_attrs, Type, Fnptrval, Arglist, Fn_attrs, To_label, Unwind_label) ->
+ #llvm_invoke{dst=Dst, cconv=Cconv, ret_attrs=Ret_attrs, type=Type,
+ fnptrval=Fnptrval, arglist=Arglist, fn_attrs=Fn_attrs, to_label=To_label,
+ unwind_label=Unwind_label}.
+invoke_dst(#llvm_invoke{dst=Dst}) -> Dst.
+invoke_cconv(#llvm_invoke{cconv=Cconv}) -> Cconv.
+invoke_ret_attrs(#llvm_invoke{ret_attrs=Ret_attrs}) -> Ret_attrs.
+invoke_type(#llvm_invoke{type=Type}) -> Type.
+invoke_fnptrval(#llvm_invoke{fnptrval=Fnptrval}) -> Fnptrval.
+invoke_arglist(#llvm_invoke{arglist=Arglist}) -> Arglist.
+invoke_fn_attrs(#llvm_invoke{fn_attrs=Fn_attrs}) -> Fn_attrs.
+invoke_to_label(#llvm_invoke{to_label=To_label}) -> To_label.
+invoke_unwind_label(#llvm_invoke{unwind_label=Unwind_label}) -> Unwind_label.
+
+%% switch
+mk_switch(Type, Value, Default_label, Value_label_list) ->
+ #llvm_switch{type=Type, value=Value, default_label=Default_label,
+ value_label_list=Value_label_list}.
+switch_type(#llvm_switch{type=Type}) -> Type.
+switch_value(#llvm_switch{value=Value}) -> Value.
+switch_default_label(#llvm_switch{default_label=Default_label}) ->
+ Default_label.
+switch_value_label_list(#llvm_switch{value_label_list=Value_label_list}) ->
+ Value_label_list.
+
+%% operation
+mk_operation(Dst, Op, Type, Src1, Src2, Options) ->
+ #llvm_operation{dst=Dst, op=Op, type=Type, src1=Src1, src2=Src2,
+ options=Options}.
+operation_dst(#llvm_operation{dst=Dst}) -> Dst.
+operation_op(#llvm_operation{op=Op}) -> Op.
+operation_type(#llvm_operation{type=Type}) -> Type.
+operation_src1(#llvm_operation{src1=Src1}) -> Src1.
+operation_src2(#llvm_operation{src2=Src2}) -> Src2.
+operation_options(#llvm_operation{options=Options}) -> Options.
+
+%% extractvalue
+mk_extractvalue(Dst, Type, Val, Idx, Idxs) ->
+ #llvm_extractvalue{dst=Dst,type=Type,val=Val,idx=Idx,idxs=Idxs}.
+extractvalue_dst(#llvm_extractvalue{dst=Dst}) -> Dst.
+extractvalue_type(#llvm_extractvalue{type=Type}) -> Type.
+extractvalue_val(#llvm_extractvalue{val=Val}) -> Val.
+extractvalue_idx(#llvm_extractvalue{idx=Idx}) -> Idx.
+extractvalue_idxs(#llvm_extractvalue{idxs=Idxs}) -> Idxs.
+
+%% insertvalue
+mk_insertvalue(Dst, Val_type, Val, Elem_type, Elem, Idx, Idxs) ->
+ #llvm_insertvalue{dst=Dst, val_type=Val_type, val=Val, elem_type=Elem_type,
+ elem=Elem, idx=Idx, idxs=Idxs}.
+insertvalue_dst(#llvm_insertvalue{dst=Dst}) -> Dst.
+insertvalue_val_type(#llvm_insertvalue{val_type=Val_type}) -> Val_type.
+insertvalue_val(#llvm_insertvalue{val=Val}) -> Val.
+insertvalue_elem_type(#llvm_insertvalue{elem_type=Elem_type}) -> Elem_type.
+insertvalue_elem(#llvm_insertvalue{elem=Elem}) -> Elem.
+insertvalue_idx(#llvm_insertvalue{idx=Idx}) -> Idx.
+insertvalue_idxs(#llvm_insertvalue{idxs=Idxs}) -> Idxs.
+
+%% alloca
+mk_alloca(Dst, Type, Num, Align) ->
+ #llvm_alloca{dst=Dst, type=Type, num=Num, align=Align}.
+alloca_dst(#llvm_alloca{dst=Dst}) -> Dst.
+alloca_type(#llvm_alloca{type=Type}) -> Type.
+alloca_num(#llvm_alloca{num=Num}) -> Num.
+alloca_align(#llvm_alloca{align=Align}) -> Align.
+
+%% load
+mk_load(Dst, Type, Pointer, Alignment, Nontemporal, Volatile) ->
+ #llvm_load{dst=Dst, p_type=Type, pointer=Pointer, alignment=Alignment,
+ nontemporal=Nontemporal, volatile=Volatile}.
+load_dst(#llvm_load{dst=Dst}) -> Dst.
+load_p_type(#llvm_load{p_type=Type}) -> Type.
+load_pointer(#llvm_load{pointer=Pointer}) -> Pointer.
+load_alignment(#llvm_load{alignment=Alignment}) -> Alignment.
+load_nontemporal(#llvm_load{nontemporal=Nontemporal}) -> Nontemporal.
+load_volatile(#llvm_load{volatile=Volatile}) -> Volatile.
+
+%% store
+mk_store(Type, Value, P_Type, Pointer, Alignment, Nontemporal, Volatile) ->
+ #llvm_store{type=Type, value=Value, p_type=P_Type, pointer=Pointer, alignment=Alignment,
+ nontemporal=Nontemporal, volatile=Volatile}.
+store_type(#llvm_store{type=Type}) -> Type.
+store_value(#llvm_store{value=Value}) -> Value.
+store_p_type(#llvm_store{p_type=P_Type}) -> P_Type.
+store_pointer(#llvm_store{pointer=Pointer}) -> Pointer.
+store_alignment(#llvm_store{alignment=Alignment}) -> Alignment.
+store_nontemporal(#llvm_store{nontemporal=Nontemporal}) -> Nontemporal.
+store_volatile(#llvm_store{volatile=Volatile}) -> Volatile.
+
+%% getelementptr
+mk_getelementptr(Dst, P_Type, Value, Typed_Idxs, Inbounds) ->
+ #llvm_getelementptr{dst=Dst,p_type=P_Type, value=Value,
+ typed_idxs=Typed_Idxs, inbounds=Inbounds}.
+getelementptr_dst(#llvm_getelementptr{dst=Dst}) -> Dst.
+getelementptr_p_type(#llvm_getelementptr{p_type=P_Type}) -> P_Type.
+getelementptr_value(#llvm_getelementptr{value=Value}) -> Value.
+getelementptr_typed_idxs(#llvm_getelementptr{typed_idxs=Typed_Idxs}) -> Typed_Idxs.
+getelementptr_inbounds(#llvm_getelementptr{inbounds=Inbounds}) -> Inbounds.
+
+%% conversion
+mk_conversion(Dst, Op, Src_type, Src, Dst_type) ->
+ #llvm_conversion{dst=Dst, op=Op, src_type=Src_type, src=Src, dst_type=Dst_type}.
+conversion_dst(#llvm_conversion{dst=Dst}) -> Dst.
+conversion_op(#llvm_conversion{op=Op}) -> Op.
+conversion_src_type(#llvm_conversion{src_type=Src_type}) -> Src_type.
+conversion_src(#llvm_conversion{src=Src}) -> Src.
+conversion_dst_type(#llvm_conversion{dst_type=Dst_type}) -> Dst_type.
+
+%% sitofp
+mk_sitofp(Dst, Src_type, Src, Dst_type) ->
+ #llvm_sitofp{dst=Dst, src_type=Src_type, src=Src, dst_type=Dst_type}.
+sitofp_dst(#llvm_sitofp{dst=Dst}) -> Dst.
+sitofp_src_type(#llvm_sitofp{src_type=Src_type}) -> Src_type.
+sitofp_src(#llvm_sitofp{src=Src}) -> Src.
+sitofp_dst_type(#llvm_sitofp{dst_type=Dst_type}) -> Dst_type.
+
+%% ptrtoint
+mk_ptrtoint(Dst, Src_Type, Src, Dst_Type) ->
+ #llvm_ptrtoint{dst=Dst, src_type=Src_Type, src=Src, dst_type=Dst_Type}.
+ptrtoint_dst(#llvm_ptrtoint{dst=Dst}) -> Dst.
+ptrtoint_src_type(#llvm_ptrtoint{src_type=Src_Type}) -> Src_Type.
+ptrtoint_src(#llvm_ptrtoint{src=Src}) -> Src.
+ptrtoint_dst_type(#llvm_ptrtoint{dst_type=Dst_Type}) -> Dst_Type .
+
+%% inttoptr
+mk_inttoptr(Dst, Src_Type, Src, Dst_Type) ->
+ #llvm_inttoptr{dst=Dst, src_type=Src_Type, src=Src, dst_type=Dst_Type}.
+inttoptr_dst(#llvm_inttoptr{dst=Dst}) -> Dst.
+inttoptr_src_type(#llvm_inttoptr{src_type=Src_Type}) -> Src_Type.
+inttoptr_src(#llvm_inttoptr{src=Src}) -> Src.
+inttoptr_dst_type(#llvm_inttoptr{dst_type=Dst_Type}) -> Dst_Type .
+
+%% icmp
+mk_icmp(Dst, Cond, Type, Src1, Src2) ->
+ #llvm_icmp{dst=Dst,'cond'=Cond,type=Type,src1=Src1,src2=Src2}.
+icmp_dst(#llvm_icmp{dst=Dst}) -> Dst.
+icmp_cond(#llvm_icmp{'cond'=Cond}) -> Cond.
+icmp_type(#llvm_icmp{type=Type}) -> Type.
+icmp_src1(#llvm_icmp{src1=Src1}) -> Src1.
+icmp_src2(#llvm_icmp{src2=Src2}) -> Src2.
+
+%% fcmp
+mk_fcmp(Dst, Cond, Type, Src1, Src2) ->
+ #llvm_fcmp{dst=Dst,'cond'=Cond,type=Type,src1=Src1,src2=Src2}.
+fcmp_dst(#llvm_fcmp{dst=Dst}) -> Dst.
+fcmp_cond(#llvm_fcmp{'cond'=Cond}) -> Cond.
+fcmp_type(#llvm_fcmp{type=Type}) -> Type.
+fcmp_src1(#llvm_fcmp{src1=Src1}) -> Src1.
+fcmp_src2(#llvm_fcmp{src2=Src2}) -> Src2.
+
+%% phi
+mk_phi(Dst, Type, Value_label_list) ->
+ #llvm_phi{dst=Dst, type=Type,value_label_list=Value_label_list}.
+phi_dst(#llvm_phi{dst=Dst}) -> Dst.
+phi_type(#llvm_phi{type=Type}) -> Type.
+phi_value_label_list(#llvm_phi{value_label_list=Value_label_list}) ->
+ Value_label_list.
+
+%% select
+mk_select(Dst, Cond, Typ1, Val1, Typ2, Val2) ->
+ #llvm_select{dst=Dst, 'cond'=Cond, typ1=Typ1, val1=Val1, typ2=Typ2, val2=Val2}.
+select_dst(#llvm_select{dst=Dst}) -> Dst.
+select_cond(#llvm_select{'cond'=Cond}) -> Cond.
+select_typ1(#llvm_select{typ1=Typ1}) -> Typ1.
+select_val1(#llvm_select{val1=Val1}) -> Val1.
+select_typ2(#llvm_select{typ2=Typ2}) -> Typ2.
+select_val2(#llvm_select{val2=Val2}) -> Val2.
+
+%% call
+mk_call(Dst, Is_tail, Cconv, Ret_attrs, Type, Fnptrval, Arglist, Fn_attrs) ->
+ #llvm_call{dst=Dst, is_tail=Is_tail, cconv=Cconv, ret_attrs=Ret_attrs,
+ type=Type, fnptrval=Fnptrval, arglist=Arglist, fn_attrs=Fn_attrs}.
+call_dst(#llvm_call{dst=Dst}) -> Dst.
+call_is_tail(#llvm_call{is_tail=Is_tail}) -> Is_tail.
+call_cconv(#llvm_call{cconv=Cconv}) -> Cconv.
+call_ret_attrs(#llvm_call{ret_attrs=Ret_attrs}) -> Ret_attrs.
+call_type(#llvm_call{type=Type}) -> Type.
+call_fnptrval(#llvm_call{fnptrval=Fnptrval}) -> Fnptrval.
+call_arglist(#llvm_call{arglist=Arglist}) -> Arglist.
+call_fn_attrs(#llvm_call{fn_attrs=Fn_attrs}) -> Fn_attrs.
+
+%% fun_def
+mk_fun_def(Linkage, Visibility, Cconv, Ret_attrs, Type, Name, Arglist,
+ Fn_attrs, Align, Body) ->
+ #llvm_fun_def{
+ linkage=Linkage,
+ visibility=Visibility,
+ cconv=Cconv,
+ ret_attrs=Ret_attrs,
+ type=Type,
+ 'name'=Name,
+ arglist=Arglist,
+ fn_attrs=Fn_attrs,
+ align=Align,
+ body=Body
+ }.
+
+fun_def_linkage(#llvm_fun_def{linkage=Linkage}) -> Linkage.
+fun_def_visibility(#llvm_fun_def{visibility=Visibility}) -> Visibility.
+fun_def_cconv(#llvm_fun_def{cconv=Cconv}) -> Cconv .
+fun_def_ret_attrs(#llvm_fun_def{ret_attrs=Ret_attrs}) -> Ret_attrs.
+fun_def_type(#llvm_fun_def{type=Type}) -> Type.
+fun_def_name(#llvm_fun_def{'name'=Name}) -> Name.
+fun_def_arglist(#llvm_fun_def{arglist=Arglist}) -> Arglist.
+fun_def_fn_attrs(#llvm_fun_def{fn_attrs=Fn_attrs}) -> Fn_attrs.
+fun_def_align(#llvm_fun_def{align=Align}) -> Align.
+fun_def_body(#llvm_fun_def{body=Body}) -> Body.
+
+%% fun_decl
+mk_fun_decl(Linkage, Visibility, Cconv, Ret_attrs, Type, Name, Arglist, Align)->
+ #llvm_fun_decl{
+ linkage=Linkage,
+ visibility=Visibility,
+ cconv=Cconv,
+ ret_attrs=Ret_attrs,
+ type=Type,
+ 'name'=Name,
+ arglist=Arglist,
+ align=Align
+ }.
+
+fun_decl_linkage(#llvm_fun_decl{linkage=Linkage}) -> Linkage.
+fun_decl_visibility(#llvm_fun_decl{visibility=Visibility}) -> Visibility.
+fun_decl_cconv(#llvm_fun_decl{cconv=Cconv}) -> Cconv .
+fun_decl_ret_attrs(#llvm_fun_decl{ret_attrs=Ret_attrs}) -> Ret_attrs.
+fun_decl_type(#llvm_fun_decl{type=Type}) -> Type.
+fun_decl_name(#llvm_fun_decl{'name'=Name}) -> Name.
+fun_decl_arglist(#llvm_fun_decl{arglist=Arglist}) -> Arglist.
+fun_decl_align(#llvm_fun_decl{align=Align}) -> Align.
+
+%% landingpad
+mk_landingpad() -> #llvm_landingpad{}.
+
+%% comment
+mk_comment(Text) -> #llvm_comment{text=Text}.
+comment_text(#llvm_comment{text=Text}) -> Text.
+
+%% label
+mk_label(Label) -> #llvm_label{label=Label}.
+label_label(#llvm_label{label=Label}) -> Label.
+
+-spec is_label(llvm_instr()) -> boolean().
+is_label(#llvm_label{}) -> true;
+is_label(#llvm_ret{}) -> false;
+is_label(#llvm_br{}) -> false;
+is_label(#llvm_br_cond{}) -> false;
+is_label(#llvm_indirectbr{}) -> false;
+is_label(#llvm_switch{}) -> false;
+is_label(#llvm_invoke{}) -> false;
+is_label(#llvm_operation{}) -> false;
+is_label(#llvm_extractvalue{}) -> false;
+is_label(#llvm_insertvalue{}) -> false;
+is_label(#llvm_alloca{}) -> false;
+is_label(#llvm_load{}) -> false;
+is_label(#llvm_store{}) -> false;
+is_label(#llvm_getelementptr{}) -> false;
+is_label(#llvm_conversion{}) -> false;
+is_label(#llvm_sitofp{}) -> false;
+is_label(#llvm_ptrtoint{}) -> false;
+is_label(#llvm_inttoptr{}) -> false;
+is_label(#llvm_icmp{}) -> false;
+is_label(#llvm_fcmp{}) -> false;
+is_label(#llvm_phi{}) -> false;
+is_label(#llvm_select{}) -> false;
+is_label(#llvm_call{}) -> false;
+is_label(#llvm_fun_def{}) -> false;
+is_label(#llvm_fun_decl{}) -> false;
+is_label(#llvm_landingpad{}) -> false;
+is_label(#llvm_comment{}) -> false;
+is_label(#llvm_const_decl{}) -> false;
+is_label(#llvm_asm{}) -> false;
+is_label(#llvm_adj_stack{}) -> false;
+is_label(#llvm_branch_meta{}) -> false.
+
+%% const_decl
+mk_const_decl(Dst, Decl_type, Type, Value) ->
+ #llvm_const_decl{dst=Dst, decl_type=Decl_type, type=Type, value=Value}.
+const_decl_dst(#llvm_const_decl{dst=Dst}) -> Dst.
+const_decl_decl_type(#llvm_const_decl{decl_type=Decl_type}) -> Decl_type.
+const_decl_type(#llvm_const_decl{type=Type}) -> Type.
+const_decl_value(#llvm_const_decl{value=Value}) -> Value.
+
+%% asm
+mk_asm(Instruction) -> #llvm_asm{instruction=Instruction}.
+asm_instruction(#llvm_asm{instruction=Instruction}) -> Instruction.
+
+%% adj_stack
+mk_adj_stack(Offset, Register, Type) ->
+ #llvm_adj_stack{offset=Offset, 'register'=Register, type=Type}.
+adj_stack_offset(#llvm_adj_stack{offset=Offset}) -> Offset.
+adj_stack_register(#llvm_adj_stack{'register'=Register}) -> Register.
+adj_stack_type(#llvm_adj_stack{type=Type}) -> Type.
+
+%% branch meta-data
+mk_branch_meta(Id, True_weight, False_weight) ->
+ #llvm_branch_meta{id=Id, true_weight=True_weight, false_weight=False_weight}.
+branch_meta_id(#llvm_branch_meta{id=Id}) -> Id.
+branch_meta_true_weight(#llvm_branch_meta{true_weight=True_weight}) ->
+ True_weight.
+branch_meta_false_weight(#llvm_branch_meta{false_weight=False_weight}) ->
+ False_weight.
+
+%% types
+mk_void() -> #llvm_void{}.
+
+mk_label_type() -> #llvm_label_type{}.
+
+mk_int(Width) -> #llvm_int{width=Width}.
+int_width(#llvm_int{width=Width}) -> Width.
+
+mk_double() -> #llvm_double{}.
+
+mk_pointer(Type) -> #llvm_pointer{type=Type}.
+pointer_type(#llvm_pointer{type=Type}) -> Type.
+
+mk_array(Size, Type) -> #llvm_array{'size'=Size, type=Type}.
+array_size(#llvm_array{'size'=Size}) -> Size.
+array_type(#llvm_array{type=Type}) -> Type.
+
+mk_vector(Size, Type) -> #llvm_vector{'size'=Size, type=Type}.
+vector_size(#llvm_vector{'size'=Size}) -> Size.
+vector_type(#llvm_vector{type=Type}) -> Type.
+
+mk_struct(Type_list) -> #llvm_struct{type_list=Type_list}.
+struct_type_list(#llvm_struct{type_list=Type_list}) -> Type_list.
+
+mk_fun(Ret_type, Arg_type_list) ->
+ #llvm_fun{ret_type=Ret_type, arg_type_list=Arg_type_list}.
+function_ret_type(#llvm_fun{ret_type=Ret_type}) -> Ret_type.
+function_arg_type_list(#llvm_fun{arg_type_list=Arg_type_list}) ->
+ Arg_type_list.
+
+%%----------------------------------------------------------------------------
+%% Pretty-printer Functions
+%%----------------------------------------------------------------------------
+
+%% @doc Pretty-print a list of LLVM instructions to a Device.
+pp_ins_list(_Dev, []) -> ok;
+pp_ins_list(Dev, [I|Is]) ->
+ pp_ins(Dev, I),
+ pp_ins_list(Dev, Is).
+
+pp_ins(Dev, I) ->
+ case indent(I) of
+ true -> write(Dev, " ");
+ false -> ok
+ end,
+ case I of
+ #llvm_ret{} ->
+ write(Dev, "ret "),
+ case ret_ret_list(I) of
+ [] -> write(Dev, "void");
+ List -> pp_args(Dev, List)
+ end,
+ write(Dev, "\n");
+ #llvm_br{} ->
+ write(Dev, ["br label ", br_dst(I), "\n"]);
+ #llvm_switch{} ->
+ write(Dev, "switch "),
+ pp_type(Dev, switch_type(I)),
+ write(Dev, [" ", switch_value(I), ", label ", switch_default_label(I),
+ " \n [\n"]),
+ pp_switch_value_label_list(Dev, switch_type(I),
+ switch_value_label_list(I)),
+ write(Dev, " ]\n");
+ #llvm_invoke{} ->
+ write(Dev, [invoke_dst(I), " = invoke ", invoke_cconv(I), " "]),
+ pp_options(Dev, invoke_ret_attrs(I)),
+ pp_type(Dev, invoke_type(I)),
+ write(Dev, [" ", invoke_fnptrval(I), "("]),
+ pp_args(Dev, invoke_arglist(I)),
+ write(Dev, ") "),
+ pp_options(Dev, invoke_fn_attrs(I)),
+ write(Dev, [" to label ", invoke_to_label(I)," unwind label ",
+ invoke_unwind_label(I), " \n"]);
+ #llvm_br_cond{} ->
+ write(Dev, ["br i1 ", br_cond_cond(I), ", label ", br_cond_true_label(I),
+ ", label ", br_cond_false_label(I)]),
+ case br_cond_meta(I) of
+ [] -> ok;
+ Metadata ->
+ write(Dev, [", !prof !", Metadata])
+ end,
+ write(Dev, "\n");
+ #llvm_indirectbr{} ->
+ write(Dev, "indirectbr "),
+ pp_type(Dev, indirectbr_type(I)),
+ write(Dev, [" ", indirectbr_address(I), ", [ "]),
+ pp_args(Dev, indirectbr_label_list(I)),
+ write(Dev, " ]\n");
+ #llvm_operation{} ->
+ write(Dev, [operation_dst(I), " = ", atom_to_list(operation_op(I)), " "]),
+ case op_has_options(operation_op(I)) of
+ true -> pp_options(Dev, operation_options(I));
+ false -> ok
+ end,
+ pp_type(Dev, operation_type(I)),
+ write(Dev, [" ", operation_src1(I), ", ", operation_src2(I), "\n"]);
+ #llvm_extractvalue{} ->
+ write(Dev, [extractvalue_dst(I), " = extractvalue "]),
+ pp_type(Dev, extractvalue_type(I)),
+ %% TODO Print idxs
+ write(Dev, [" ", extractvalue_val(I), ", ", extractvalue_idx(I), "\n"]);
+ #llvm_insertvalue{} ->
+ write(Dev, [insertvalue_dst(I), " = insertvalue "]),
+ pp_type(Dev, insertvalue_val_type(I)),
+ write(Dev, [" ", insertvalue_val(I), ", "]),
+ pp_type(Dev, insertvalue_elem_type(I)),
+ %%TODO Print idxs
+ write(Dev, [" ", insertvalue_elem(I), ", ", insertvalue_idx(I), "\n"]);
+ #llvm_alloca{} ->
+ write(Dev, [alloca_dst(I), " = alloca "]),
+ pp_type(Dev, alloca_type(I)),
+ case alloca_num(I) of
+ [] -> ok;
+ Num ->
+ write(Dev, ", "),
+ pp_type(Dev, alloca_type(I)),
+ write(Dev, [" ", Num, " "])
+ end,
+ case alloca_align(I) of
+ [] -> ok;
+ Align -> write(Dev, [",align ", Align])
+ end,
+ write(Dev, "\n");
+ #llvm_load{} ->
+ write(Dev, [load_dst(I), " = "]),
+ write(Dev, "load "),
+ case load_volatile(I) of
+ true -> write(Dev, "volatile ");
+ false -> ok
+ end,
+ pp_type(Dev, load_p_type(I)),
+ write(Dev, [" ", load_pointer(I), " "]),
+ case load_alignment(I) of
+ [] -> ok;
+ Al -> write(Dev, [", align ", Al, " "])
+ end,
+ case load_nontemporal(I) of
+ [] -> ok;
+ In -> write(Dev, [", !nontemporal !", In])
+ end,
+ write(Dev, "\n");
+ #llvm_store{} ->
+ write(Dev, "store "),
+ case store_volatile(I) of
+ true -> write(Dev, "volatile ");
+ false -> ok
+ end,
+ pp_type(Dev, store_type(I)),
+ write(Dev, [" ", store_value(I), ", "]),
+ pp_type(Dev, store_p_type(I)),
+ write(Dev, [" ", store_pointer(I), " "]),
+ case store_alignment(I) of
+ [] -> ok;
+ Al -> write(Dev, [", align ", Al, " "])
+ end,
+ case store_nontemporal(I) of
+ [] -> ok;
+ In -> write(Dev, [", !nontemporal !", In])
+ end,
+ write(Dev, "\n");
+ #llvm_getelementptr{} ->
+ write(Dev, [getelementptr_dst(I), " = getelementptr "]),
+ case getelementptr_inbounds(I) of
+ true -> write(Dev, "inbounds ");
+ false -> ok
+ end,
+ pp_type(Dev, getelementptr_p_type(I)),
+ write(Dev, [" ", getelementptr_value(I)]),
+ pp_typed_idxs(Dev, getelementptr_typed_idxs(I)),
+ write(Dev, "\n");
+ #llvm_conversion{} ->
+ write(Dev, [conversion_dst(I), " = ", atom_to_list(conversion_op(I)), " "]),
+ pp_type(Dev, conversion_src_type(I)),
+ write(Dev, [" ", conversion_src(I), " to "]),
+ pp_type(Dev, conversion_dst_type(I)),
+ write(Dev, "\n");
+ #llvm_icmp{} ->
+ write(Dev, [icmp_dst(I), " = icmp ", atom_to_list(icmp_cond(I)), " "]),
+ pp_type(Dev, icmp_type(I)),
+ write(Dev, [" ", icmp_src1(I), ", ", icmp_src2(I), "\n"]);
+ #llvm_fcmp{} ->
+ write(Dev, [fcmp_dst(I), " = fcmp ", atom_to_list(fcmp_cond(I)), " "]),
+ pp_type(Dev, fcmp_type(I)),
+ write(Dev, [" ", fcmp_src1(I), ", ", fcmp_src2(I), "\n"]);
+ #llvm_phi{} ->
+ write(Dev, [phi_dst(I), " = phi "]),
+ pp_type(Dev, phi_type(I)),
+ pp_phi_value_labels(Dev, phi_value_label_list(I)),
+ write(Dev, "\n");
+ #llvm_select{} ->
+ write(Dev, [select_dst(I), " = select i1 ", select_cond(I), ", "]),
+ pp_type(Dev, select_typ1(I)),
+ write(Dev, [" ", select_val1(I), ", "]),
+ pp_type(Dev, select_typ2(I)),
+ write(Dev, [" ", select_val2(I), "\n"]);
+ #llvm_call{} ->
+ case call_dst(I) of
+ [] -> ok;
+ Dst -> write(Dev, [Dst, " = "])
+ end,
+ case call_is_tail(I) of
+ true -> write(Dev, "tail ");
+ false -> ok
+ end,
+ write(Dev, ["call ", call_cconv(I), " "]),
+ pp_options(Dev, call_ret_attrs(I)),
+ pp_type(Dev, call_type(I)),
+ write(Dev, [" ", call_fnptrval(I), "("]),
+ pp_args(Dev, call_arglist(I)),
+ write(Dev, ") "),
+ pp_options(Dev, call_fn_attrs(I)),
+ write(Dev, "\n");
+ #llvm_fun_def{} ->
+ write(Dev, "define "),
+ pp_options(Dev, fun_def_linkage(I)),
+ pp_options(Dev, fun_def_visibility(I)),
+ case fun_def_cconv(I) of
+ [] -> ok;
+ Cc -> write(Dev, [Cc, " "])
+ end,
+ pp_options(Dev, fun_def_ret_attrs(I)),
+ write(Dev, " "),
+ pp_type(Dev, fun_def_type(I)),
+ write(Dev, [" @", fun_def_name(I), "("]),
+ pp_args(Dev, fun_def_arglist(I)),
+ write(Dev, ") "),
+ pp_options(Dev, fun_def_fn_attrs(I)),
+ case fun_def_align(I) of
+ [] -> ok;
+ N -> write(Dev, ["align ", N])
+ end,
+ write(Dev, "{\n"),
+ pp_ins_list(Dev, fun_def_body(I)),
+ write(Dev, "}\n");
+ #llvm_fun_decl{} ->
+ write(Dev, "declare "),
+ pp_options(Dev, fun_decl_linkage(I)),
+ pp_options(Dev, fun_decl_visibility(I)),
+ case fun_decl_cconv(I) of
+ [] -> ok;
+ Cc -> write(Dev, [Cc, " "])
+ end,
+ pp_options(Dev, fun_decl_ret_attrs(I)),
+ pp_type(Dev, fun_decl_type(I)),
+ write(Dev, [" ", fun_decl_name(I), "("]),
+ pp_type_list(Dev, fun_decl_arglist(I)),
+ write(Dev, ") "),
+ case fun_decl_align(I) of
+ [] -> ok;
+ N -> write(Dev, ["align ", N])
+ end,
+ write(Dev, "\n");
+ #llvm_comment{} ->
+ write(Dev, ["; ", atom_to_list(comment_text(I)), "\n"]);
+ #llvm_label{} ->
+ write(Dev, [label_label(I), ":\n"]);
+ #llvm_const_decl{} ->
+ write(Dev, [const_decl_dst(I), " = ", const_decl_decl_type(I), " "]),
+ pp_type(Dev, const_decl_type(I)),
+ write(Dev, [" ", const_decl_value(I), "\n"]);
+ #llvm_landingpad{} ->
+ write(Dev, "landingpad { i8*, i32 } personality i32 (i32, i64, i8*,i8*)*
+ @__gcc_personality_v0 cleanup\n");
+ #llvm_asm{} ->
+ write(Dev, [asm_instruction(I), "\n"]);
+ #llvm_adj_stack{} ->
+ write(Dev, ["call void asm sideeffect \"sub $0, ",
+ adj_stack_register(I), "\", \"r\"("]),
+ pp_type(Dev, adj_stack_type(I)),
+ write(Dev, [" ", adj_stack_offset(I),")\n"]);
+ #llvm_branch_meta{} ->
+ write(Dev, ["!", branch_meta_id(I), " = metadata !{metadata !\"branch_weights\",
+ i32 ", branch_meta_true_weight(I), ", i32 ",
+ branch_meta_false_weight(I), "}\n"]);
+ Other ->
+ exit({?MODULE, pp_ins, {"Unknown LLVM instruction", Other}})
+ end.
+
+%% @doc Pretty-print a list of types
+pp_type_list(_Dev, []) -> ok;
+pp_type_list(Dev, [T]) ->
+ pp_type(Dev, T);
+pp_type_list(Dev, [T|Ts]) ->
+ pp_type(Dev, T),
+ write(Dev, ", "),
+ pp_type_list(Dev, Ts).
+
+pp_type(Dev, Type) ->
+ case Type of
+ #llvm_void{} ->
+ write(Dev, "void");
+ #llvm_label_type{} ->
+ write(Dev, "label");
+ %% Integer
+ #llvm_int{} ->
+ write(Dev, ["i", integer_to_list(int_width(Type))]);
+ %% Float
+ #llvm_float{} ->
+ write(Dev, "float");
+ #llvm_double{} ->
+ write(Dev, "double");
+ #llvm_fp80{} ->
+ write(Dev, "x86_fp80");
+ #llvm_fp128{} ->
+ write(Dev, "fp128");
+ #llvm_ppc_fp128{} ->
+ write(Dev, "ppc_fp128");
+ %% Pointer
+ #llvm_pointer{} ->
+ pp_type(Dev, pointer_type(Type)),
+ write(Dev, "*");
+ %% Function
+ #llvm_fun{} ->
+ pp_type(Dev, function_ret_type(Type)),
+ write(Dev, " ("),
+ pp_type_list(Dev, function_arg_type_list(Type)),
+ write(Dev, ")");
+ %% Aggregate
+ #llvm_array{} ->
+ write(Dev, ["[", integer_to_list(array_size(Type)), " x "]),
+ pp_type(Dev, array_type(Type)),
+ write(Dev, "]");
+ #llvm_struct{} ->
+ write(Dev, "{"),
+ pp_type_list(Dev, struct_type_list(Type)),
+ write(Dev, "}");
+ #llvm_vector{} ->
+ write(Dev, ["{", integer_to_list(vector_size(Type)), " x "]),
+ pp_type(Dev, vector_type(Type)),
+ write(Dev, "}")
+ end.
+
+%% @doc Pretty-print a list of typed arguments
+pp_args(_Dev, []) -> ok;
+pp_args(Dev, [{Type, Arg} | []]) ->
+ pp_type(Dev, Type),
+ write(Dev, [" ", Arg]);
+pp_args(Dev, [{Type, Arg} | Args]) ->
+ pp_type(Dev, Type),
+ write(Dev, [" ", Arg, ", "]),
+ pp_args(Dev, Args).
+
+%% @doc Pretty-print a list of options
+pp_options(_Dev, []) -> ok;
+pp_options(Dev, [O|Os]) ->
+ write(Dev, [atom_to_list(O), " "]),
+ pp_options(Dev, Os).
+
+%% @doc Pretty-print a list of phi value-labels
+pp_phi_value_labels(_Dev, []) -> ok;
+pp_phi_value_labels(Dev, [{Value, Label}|[]]) ->
+ write(Dev, ["[ ", Value, ", ", Label, " ]"]);
+pp_phi_value_labels(Dev,[{Value, Label}|VL]) ->
+ write(Dev, ["[ ", Value, ", ", Label, " ], "]),
+ pp_phi_value_labels(Dev, VL).
+
+%% @doc Pretty-print a list of typed indexes
+pp_typed_idxs(_Dev, []) -> ok;
+pp_typed_idxs(Dev, [{Type, Id} | Tids]) ->
+ write(Dev, ", "),
+ pp_type(Dev, Type),
+ write(Dev, [" ", Id]),
+ pp_typed_idxs(Dev, Tids).
+
+%% @doc Pretty-print a switch label list
+pp_switch_value_label_list(_Dev, _Type, []) -> ok;
+pp_switch_value_label_list(Dev, Type, [{Value, Label} | VLs]) ->
+ write(Dev, " "),
+ pp_type(Dev, Type),
+ write(Dev, [" ", Value, ", label ", Label, "\n"]),
+ pp_switch_value_label_list(Dev, Type, VLs).
+
+%%----------------------------------------------------------------------------
+%% Auxiliary Functions
+%%----------------------------------------------------------------------------
+
+%% @doc Returns if an instruction needs to be intended
+indent(I) ->
+ case I of
+ #llvm_label{} -> false;
+ #llvm_fun_def{} -> false;
+ #llvm_fun_decl{} -> false;
+ #llvm_const_decl{} -> false;
+ #llvm_branch_meta{} -> false;
+ _ -> true
+ end.
+
+op_has_options(Op) ->
+ case Op of
+ 'and' -> false;
+ 'or' -> false;
+ 'xor' -> false;
+ _ -> true
+ end.
+
+%% @doc Abstracts actual writing to file operations
+write(Dev, Msg) ->
+ ok = file:write(Dev, Msg).
diff --git a/lib/hipe/llvm/hipe_llvm_arch.hrl b/lib/hipe/llvm/hipe_llvm_arch.hrl
new file mode 100644
index 0000000000..689a5a52ea
--- /dev/null
+++ b/lib/hipe/llvm/hipe_llvm_arch.hrl
@@ -0,0 +1,11 @@
+-ifdef(BIT32).
+-define(NR_PINNED_REGS, 2).
+-define(NR_ARG_REGS, 3).
+-define(ARCH_REGISTERS, hipe_x86_registers).
+-define(FLOAT_OFFSET, 2).
+-else.
+-define(NR_PINNED_REGS, 2).
+-define(NR_ARG_REGS, 4).
+-define(ARCH_REGISTERS, hipe_amd64_registers).
+-define(FLOAT_OFFSET, 6).
+-endif.
diff --git a/lib/hipe/llvm/hipe_llvm_liveness.erl b/lib/hipe/llvm/hipe_llvm_liveness.erl
new file mode 100644
index 0000000000..d1c90ed4c9
--- /dev/null
+++ b/lib/hipe/llvm/hipe_llvm_liveness.erl
@@ -0,0 +1,112 @@
+-module(hipe_llvm_liveness).
+
+-export([analyze/1]).
+
+%% @doc Find gc roots and explicitly mark when they go out of scope, based
+%% on the liveness analyzis performed by the hipe_rtl_liveness:analyze/1.
+analyze(RtlCfg) ->
+ Liveness = hipe_rtl_liveness:analyze(RtlCfg),
+ Roots = find_roots(RtlCfg, Liveness),
+ %% erlang:display(Roots),
+ NewRtlCfg = mark_dead_roots(RtlCfg, Liveness, Roots),
+ {NewRtlCfg, Roots}.
+
+%% @doc Determine which are the GC Roots.Possible roots are all
+%% RTL variables (rtl_var). However, since safe points are function calls, we
+%% consider as possible GC roots only RTL variables that are live around
+%% function calls.
+find_roots(Cfg, Liveness) ->
+ Labels = hipe_rtl_cfg:postorder(Cfg),
+ Roots = find_roots_bb(Labels, Cfg, Liveness, []),
+ lists:usort(lists:flatten(Roots)).
+
+find_roots_bb([], _Cfg, _Liveness, RootAcc) ->
+ RootAcc;
+find_roots_bb([L|Ls], Cfg, Liveness, RootAcc) ->
+ Block = hipe_rtl_cfg:bb(Cfg, L),
+ BlockCode = hipe_bb:code(Block),
+ LiveIn = ordsets:from_list(strip(hipe_rtl_liveness:livein(Liveness, L))),
+ LiveOut = ordsets:from_list(strip(hipe_rtl_liveness:liveout(Liveness, L))),
+ Roots = do_find_roots_bb(BlockCode, L, LiveOut, LiveIn, []),
+ find_roots_bb(Ls, Cfg, Liveness, Roots++RootAcc).
+
+%% For each call inside a BB the GC roots are those RTL variables that
+%% are live before and after the call.
+%% --> Live Before Call: These are the RTL variables that belong to the
+%% LiveIn list or are initialized inside the BB before the call
+%% --> Live After Call: These are the RTL variables that belong to the
+%% LiveOut list or are used after the call inside the BB (they die
+%% inside the BB and so do not belong to the LiveOut list)
+do_find_roots_bb([], _Label, _LiveOut, _LiveBefore, RootAcc) ->
+ RootAcc;
+do_find_roots_bb([I|Is], L, LiveOut, LiveBefore, RootAcc) ->
+ case hipe_rtl:is_call(I) of
+ true ->
+ %% Used inside the BB after the call
+ UsedAfterCall_ = strip(lists:flatten([hipe_rtl:uses(V) || V <- Is])),
+ UsedAfterCall = ordsets:from_list(UsedAfterCall_),
+ LiveAfter = ordsets:union(UsedAfterCall, LiveOut),
+ %% The Actual Roots
+ Roots = ordsets:intersection(LiveBefore, LiveAfter),
+ %% The result of the instruction
+ Defines = ordsets:from_list(strip(hipe_rtl:defines(I))),
+ LiveBefore1 = ordsets:union(LiveBefore, Defines),
+ do_find_roots_bb(Is, L, LiveOut, LiveBefore1, [Roots|RootAcc]);
+ false ->
+ %% The result of the instruction
+ Defines = ordsets:from_list(strip(hipe_rtl:defines(I))),
+ LiveBefore1 = ordsets:union(LiveBefore, Defines),
+ do_find_roots_bb(Is, L, LiveOut, LiveBefore1, RootAcc)
+ end.
+
+%% @doc This function is responsible for marking when GC Roots, which can be
+%% only RTL variables go out of scope (dead). This pass is needed for the LLVM
+%% back end because the LLVM framework forces us to explicit mark when gc roots
+%% are no longer live.
+mark_dead_roots(CFG, Liveness, Roots) ->
+ Labels = hipe_rtl_cfg:postorder(CFG),
+ mark_dead_bb(Labels, CFG, Liveness, Roots).
+
+mark_dead_bb([], Cfg, _Liveness, _Roots) ->
+ Cfg;
+mark_dead_bb([L|Ls], Cfg, Liveness, Roots) ->
+ Block = hipe_rtl_cfg:bb(Cfg, L),
+ BlockCode = hipe_bb:code(Block),
+ LiveOut = ordsets:from_list(strip(hipe_rtl_liveness:liveout(Liveness, L))),
+ NewBlockCode = do_mark_dead_bb(BlockCode, LiveOut, Roots, []),
+ %% Update the CFG
+ NewBB = hipe_bb:code_update(Block, NewBlockCode),
+ NewCFG = hipe_rtl_cfg:bb_add(Cfg, L, NewBB),
+ mark_dead_bb(Ls, NewCFG, Liveness, Roots).
+
+do_mark_dead_bb([], _LiveOut, _Roots, NewBlockCode) ->
+ lists:reverse(NewBlockCode);
+do_mark_dead_bb([I|Is], LiveOut ,Roots, NewBlockCode) ->
+ Uses = ordsets:from_list(strip(hipe_rtl:uses(I))),
+ %% GC roots that are used in this instruction
+ RootsUsed = ordsets:intersection(Roots, Uses),
+ UsedAfter_ = strip(lists:flatten([hipe_rtl:uses(V) || V <- Is])),
+ UsedAfter = ordsets:from_list(UsedAfter_),
+ %% GC roots that are live after this instruction
+ LiveAfter = ordsets:union(LiveOut, UsedAfter),
+ %% GC roots that their last use is in this instruction
+ DeadRoots = ordsets:subtract(RootsUsed, LiveAfter),
+ %% Recreate the RTL variable from the corresponding Index
+ OldVars = [hipe_rtl:mk_var(V1) || V1 <- DeadRoots],
+ %% Mark the RTL variable as DEAD (last use)
+ NewVars = [kill_var(V2) || V2 <- OldVars],
+ %% Create a list with the substitution of the old vars with the new
+ %% ones which are marked with the dead keyword
+ Subtitution = lists:zip(OldVars, NewVars),
+ NewI = case Subtitution of
+ [] -> I;
+ _ -> hipe_rtl:subst_uses_llvm(Subtitution, I)
+ end,
+ do_mark_dead_bb(Is, LiveOut, Roots, [NewI|NewBlockCode]).
+
+%% Update the liveness of a var,in order to mark that this is the last use.
+kill_var(Var) -> hipe_rtl:var_liveness_update(Var, dead).
+
+%% We are only interested for rtl_vars, since only rtl_vars are possible gc
+%% roots.
+strip(L) -> [Y || {rtl_var, Y, _} <- L].
diff --git a/lib/hipe/llvm/hipe_llvm_main.erl b/lib/hipe/llvm/hipe_llvm_main.erl
new file mode 100644
index 0000000000..0e50c9539b
--- /dev/null
+++ b/lib/hipe/llvm/hipe_llvm_main.erl
@@ -0,0 +1,506 @@
+%% -*- erlang-indent-level: 2 -*-
+-module(hipe_llvm_main).
+
+-export([rtl_to_native/4]).
+
+-include("../../kernel/src/hipe_ext_format.hrl").
+-include("hipe_llvm_arch.hrl").
+-include("elf_format.hrl").
+
+%% @doc Translation of RTL to a loadable object. This function takes the RTL
+%% code and calls hipe_rtl_to_llvm:translate/2 to translate the RTL code to
+%% LLVM code. After this, LLVM asm is printed to a file and the LLVM tool
+%% chain is invoked in order to produce an object file.
+rtl_to_native(MFA, RTL, Roots, Options) ->
+ %% Compile to LLVM and get Instruction List (along with infos)
+ {LLVMCode, RelocsDict, ConstTab} =
+ hipe_rtl_to_llvm:translate(RTL, Roots),
+ %% Fix function name to an acceptable LLVM identifier (needed for closures)
+ {_Module, Fun, Arity} = hipe_rtl_to_llvm:fix_mfa_name(MFA),
+ %% Write LLVM Assembly to intermediate file (on disk)
+ {ok, Dir, ObjectFile} =
+ compile_with_llvm(Fun, Arity, LLVMCode, Options, false),
+ %%
+ %% Extract information from object file
+ %%
+ ObjBin = open_object_file(ObjectFile),
+ %% Read and set the ELF class
+ elf_format:set_architecture_flag(ObjBin),
+ %% Get labels info (for switches and jump tables)
+ Labels = elf_format:get_rodata_relocs(ObjBin),
+ {Switches, Closures} = get_tables(ObjBin),
+ %% Associate Labels with Switches and Closures with stack args
+ {SwitchInfos, ExposedClosures} =
+ correlate_labels(Switches ++ Closures, Labels),
+ %% SwitchInfos: [{"table_50", [Labels]}]
+ %% ExposedClosures: [{"table_closures", [Labels]}]
+
+ %% Labelmap contains the offsets of the labels in the code that are
+ %% used for switch's jump tables
+ LabelMap = create_labelmap(MFA, SwitchInfos, RelocsDict),
+ %% Get relocation info
+ TextRelocs = elf_format:get_text_relocs(ObjBin),
+ %% AccRefs contains the offsets of all references to relocatable symbols in
+ %% the code:
+ AccRefs = fix_relocations(TextRelocs, RelocsDict, MFA),
+ %% Get stack descriptors
+ SDescs = get_sdescs(ObjBin),
+ %% FixedSDescs are the stack descriptors after correcting calls that have
+ %% arguments in the stack
+ FixedSDescs =
+ fix_stack_descriptors(RelocsDict, AccRefs, SDescs, ExposedClosures),
+ Refs = AccRefs ++ FixedSDescs,
+ %% Get binary code from object file
+ BinCode = elf_format:extract_text(ObjBin),
+ %% Remove temp files (if needed)
+ ok = remove_temp_folder(Dir, Options),
+ %% Return the code together with information that will be used in the
+ %% hipe_llvm_merge module to produce the final binary that will be loaded
+ %% by the hipe unified loader.
+ {MFA, BinCode, byte_size(BinCode), ConstTab, Refs, LabelMap}.
+
+%%------------------------------------------------------------------------------
+%% LLVM tool chain
+%%------------------------------------------------------------------------------
+
+%% @doc Compile function FunName/Arity to LLVM. Return Dir (in order to remove
+%% it if we do not want to store temporary files) and ObjectFile name that
+%% is created by the LLVM tools.
+compile_with_llvm(FunName, Arity, LLVMCode, Options, UseBuffer) ->
+ Filename = atom_to_list(FunName) ++ "_" ++ integer_to_list(Arity),
+ %% Save temp files in a unique folder
+ Dir = unique_folder(FunName, Arity, Options),
+ ok = file:make_dir(Dir),
+ %% Print LLVM assembly to file
+ OpenOpts = [append, raw] ++
+ case UseBuffer of
+ %% true -> [delayed_write]; % Use delayed_write!
+ false -> []
+ end,
+ {ok, File_llvm} = file:open(Dir ++ Filename ++ ".ll", OpenOpts),
+ hipe_llvm:pp_ins_list(File_llvm, LLVMCode),
+ %% delayed_write can cause file:close not to do a close, hence the two calls
+ ok = file:close(File_llvm),
+ __ = file:close(File_llvm),
+ %% Invoke LLVM compiler tools to produce an object file
+ llvm_opt(Dir, Filename, Options),
+ llvm_llc(Dir, Filename, Options),
+ compile(Dir, Filename, "gcc"), %%FIXME: use llc -filetype=obj and skip this!
+ {ok, Dir, Dir ++ Filename ++ ".o"}.
+
+%% @doc Invoke opt tool to optimize the bitcode (_name.ll -> _name.bc).
+llvm_opt(Dir, Filename, Options) ->
+ Source = Dir ++ Filename ++ ".ll",
+ Dest = Dir ++ Filename ++ ".bc",
+ OptLevel = trans_optlev_flag(opt, Options),
+ OptFlags = [OptLevel, "-mem2reg", "-strip"],
+ Command = "opt " ++ fix_opts(OptFlags) ++ " " ++ Source ++ " -o " ++ Dest,
+ %% io:format("OPT: ~s~n", [Command]),
+ case os:cmd(Command) of
+ "" -> ok;
+ Error -> exit({?MODULE, opt, Error})
+ end.
+
+%% @doc Invoke llc tool to compile the bitcode to object file
+%% (_name.bc -> _name.o).
+llvm_llc(Dir, Filename, Options) ->
+ Source = Dir ++ Filename ++ ".bc",
+ OptLevel = trans_optlev_flag(llc, Options),
+ Align = find_stack_alignment(),
+ LlcFlags = [OptLevel, "-code-model=medium", "-stack-alignment=" ++ Align
+ , "-tailcallopt", "-filetype=asm"], %%FIXME
+ Command = "llc " ++ fix_opts(LlcFlags) ++ " " ++ Source,
+ %% io:format("LLC: ~s~n", [Command]),
+ case os:cmd(Command) of
+ "" -> ok;
+ Error -> exit({?MODULE, llc, Error})
+ end.
+
+%% @doc Invoke the compiler tool ("gcc", "llvmc", etc.) to generate an object
+%% file from native assembly.
+compile(Dir, Fun_Name, Compiler) ->
+ Source = Dir ++ Fun_Name ++ ".s",
+ Dest = Dir ++ Fun_Name ++ ".o",
+ Command = Compiler ++ " -c " ++ Source ++ " -o " ++ Dest,
+ %% io:format("~s: ~s~n", [Compiler, Command]),
+ case os:cmd(Command) of
+ "" -> ok;
+ Error -> exit({?MODULE, cc, Error})
+ end.
+
+find_stack_alignment() ->
+ case get(hipe_target_arch) of
+ x86 -> "4";
+ amd64 -> "8";
+ _ -> exit({?MODULE, find_stack_alignment, "Unimplemented architecture"})
+ end.
+
+%% @doc Join options.
+fix_opts(Opts) ->
+ string:join(Opts, " ").
+
+%% @doc Translate optimization-level flag (default is "O2").
+trans_optlev_flag(Tool, Options) ->
+ Flag = case Tool of
+ opt -> llvm_opt;
+ llc -> llvm_llc
+ end,
+ case proplists:get_value(Flag, Options) of
+ o0 -> ""; % "-O0" does not exist in opt tool
+ o1 -> "-O1";
+ o2 -> "-O2";
+ o3 -> "-O3";
+ undefined -> "-O2"
+ end.
+
+%%------------------------------------------------------------------------------
+%% Functions to manage Relocations
+%%------------------------------------------------------------------------------
+
+%% @doc Get switch table and closure table.
+get_tables(Elf) ->
+ %% Search Symbol Table for an entry with name prefixed with "table_":
+ Triples = elf_format:get_tab_entries(Elf),
+ Switches = [T || T={"table_" ++ _, _, _} <- Triples],
+ Closures = [T || T={"table_closures" ++ _, _, _} <- Switches],
+ {Switches, Closures}.
+
+%% @doc This function associates symbols who point to some table of labels with
+%% the corresponding offsets of the labels in the code. These tables can
+%% either be jump tables for switches or a table which contains the labels
+%% of blocks that contain closure calls with more than ?NR_ARG_REGS.
+correlate_labels([], _L) -> {[], []};
+correlate_labels(Tables, Labels) ->
+ %% Sort "Tables" based on "ValueOffsets"
+ OffsetSortedTb = lists:ukeysort(2, Tables),
+ %% Unzip offset-sorted list of "Switches"
+ {Names, _Offsets, TablesSizeList} = lists:unzip3(OffsetSortedTb),
+ %% Associate switch names with labels
+ L = split_list(Labels, TablesSizeList),
+ %% Zip back! (to [{SwitchName, Values}])
+ NamesValues = lists:zip(Names, L),
+ case lists:keytake("table_closures", 1, NamesValues) of
+ false -> %% No closures in the code, no closure table
+ {NamesValues, []};
+ {value, ClosureTableNV, SwitchesNV} ->
+ {SwitchesNV, ClosureTableNV}
+ end.
+
+%% @doc Create a gb_tree which contains information about the labels that used
+%% for switch's jump tables. The keys of the gb_tree are of the form
+%% {MFA, Label} and the values are the actual Offsets.
+create_labelmap(MFA, SwitchInfos, RelocsDict) ->
+ create_labelmap(MFA, SwitchInfos, RelocsDict, gb_trees:empty()).
+
+create_labelmap(_, [], _, LabelMap) -> LabelMap;
+create_labelmap(MFA, [{Name, Offsets} | Rest], RelocsDict, LabelMap) ->
+ case dict:fetch(Name, RelocsDict) of
+ {switch, {_TableType, LabelList, _NrLabels, _SortOrder}, _JTabLab} ->
+ KVDict = lists:ukeysort(1, lists:zip(LabelList, Offsets)),
+ NewLabelMap = insert_to_labelmap(KVDict, LabelMap),
+ create_labelmap(MFA, Rest, RelocsDict, NewLabelMap);
+ _ ->
+ exit({?MODULE, create_labelmap, "Not a jump table!"})
+ end.
+
+%% @doc Insert a list of [{Key,Value}] to a LabelMap (gb_tree).
+insert_to_labelmap([], LabelMap) -> LabelMap;
+insert_to_labelmap([{Key, Value}|Rest], LabelMap) ->
+ case gb_trees:lookup(Key, LabelMap) of
+ none ->
+ insert_to_labelmap(Rest, gb_trees:insert(Key, Value, LabelMap));
+ {value, Value} -> %% Exists with the *exact* same Value.
+ insert_to_labelmap(Rest, LabelMap)
+ end.
+
+%% @doc Correlate object file relocation symbols with info from translation to
+%% llvm code.
+fix_relocations(Relocs, RelocsDict, MFA) ->
+ fix_relocs(Relocs, RelocsDict, MFA, []).
+
+fix_relocs([], _, _, RelocAcc) -> RelocAcc;
+fix_relocs([{Name, Offset}|Rs], RelocsDict, {ModName,_,_}=MFA, RelocAcc) ->
+ case dict:fetch(Name, RelocsDict) of
+ {atom, AtomName} ->
+ fix_relocs(Rs, RelocsDict, MFA,
+ [{?LOAD_ATOM, Offset, AtomName}|RelocAcc]);
+ {constant, Label} ->
+ fix_relocs(Rs, RelocsDict, MFA,
+ [{?LOAD_ADDRESS, Offset, {constant, Label}}|RelocAcc]);
+ {switch, _, JTabLab} -> %% Treat switch exactly as constant
+ fix_relocs(Rs, RelocsDict, MFA,
+ [{?LOAD_ADDRESS, Offset, {constant, JTabLab}}|RelocAcc]);
+ {closure, _}=Closure ->
+ fix_relocs(Rs, RelocsDict, MFA,
+ [{?LOAD_ADDRESS, Offset, Closure}|RelocAcc]);
+ {call, {bif, BifName, _}} ->
+ fix_relocs(Rs, RelocsDict, MFA,
+ [{?CALL_LOCAL, Offset, BifName}|RelocAcc]);
+ %% MFA calls to functions in the same module are of type 3, while all
+ %% other MFA calls are of type 2.
+ {call, {ModName,_F,_A}=CallMFA} ->
+ fix_relocs(Rs, RelocsDict, MFA,
+ [{?CALL_LOCAL, Offset, CallMFA}|RelocAcc]);
+ {call, CallMFA} ->
+ fix_relocs(Rs, RelocsDict, MFA,
+ [{?CALL_REMOTE, Offset, CallMFA}|RelocAcc]);
+ Other ->
+ exit({?MODULE, fix_relocs,
+ {"Relocation not in relocation dictionary", Other}})
+ end.
+
+%%------------------------------------------------------------------------------
+%% Functions to manage Stack Descriptors
+%%------------------------------------------------------------------------------
+
+%% @doc This function takes an ELF Object File binary and returns a proper sdesc
+%% list for Erlang/OTP System's loader. The return value should be of the
+%% form:
+%% {
+%% 4, Safepoint Address,
+%% {ExnLabel OR [], FrameSize, StackArity, {Liveroot stack frame indexes}},
+%% }
+get_sdescs(Elf) ->
+ case elf_format:extract_note(Elf, ?NOTE_ERLGC_NAME) of
+ <<>> -> % Object file has no ".note.gc" section!
+ [];
+ NoteGC_bin ->
+ %% Get safe point addresses (stored in ".rela.note.gc" section):
+ RelaNoteGC = elf_format:extract_rela(Elf, ?NOTE(?NOTE_ERLGC_NAME)),
+ SPCount = length(RelaNoteGC),
+ T = SPCount * ?SP_ADDR_SIZE,
+ %% Pattern match fields of ".note.gc":
+ <<SPCount:(?bits(?SP_COUNT_SIZE))/integer-little, % Sanity check!
+ SPAddrs:T/binary, % NOTE: In 64bit they are relocs!
+ StkFrameSize:(?bits(?SP_STKFRAME_SIZE))/integer-little,
+ StkArity:(?bits(?SP_STKARITY_SIZE))/integer-little,
+ _LiveRootCount:(?bits(?SP_LIVEROOTCNT_SIZE))/integer-little, % Skip
+ Roots/binary>> = NoteGC_bin,
+ LiveRoots = get_liveroots(Roots, []),
+ %% Extract information about the safe point addresses:
+ SPOffs =
+ case elf_format:is64bit() of
+ true -> %% Find offsets in ".rela.note.gc":
+ elf_format:get_rela_addends(RelaNoteGC);
+ false -> %% Find offsets in SPAddrs (in ".note.gc"):
+ get_spoffs(SPAddrs, [])
+ end,
+ %% Extract Exception Handler labels:
+ ExnHandlers = elf_format:get_exn_handlers(Elf),
+ %% Combine ExnHandlers and Safe point addresses (return addresses):
+ ExnAndSPOffs = combine_ras_and_exns(ExnHandlers, SPOffs, []),
+ create_sdesc_list(ExnAndSPOffs, StkFrameSize, StkArity, LiveRoots, [])
+ end.
+
+%% @doc Extracts a bunch of integers (live roots) from a binary. Returns a tuple
+%% as need for stack descriptors.
+get_liveroots(<<>>, Acc) ->
+ list_to_tuple(Acc);
+get_liveroots(<<Root:?bits(?LR_STKINDEX_SIZE)/integer-little,
+ MoreRoots/binary>>, Acc) ->
+ get_liveroots(MoreRoots, [Root | Acc]).
+
+%% @doc Extracts a bunch of integers (safepoint offsets) from a binary. Returns
+%% a tuple as need for stack descriptors.
+get_spoffs(<<>>, Acc) ->
+ lists:reverse(Acc);
+get_spoffs(<<SPOff:?bits(?SP_ADDR_SIZE)/integer-little, More/binary>>, Acc) ->
+ get_spoffs(More, [SPOff | Acc]).
+
+combine_ras_and_exns(_, [], Acc) ->
+ lists:reverse(Acc);
+combine_ras_and_exns(ExnHandlers, [RA | MoreRAs], Acc) ->
+ %% FIXME: do something better than O(n^2) by taking advantage of the property
+ %% ||ExnHandlers|| <= ||RAs||
+ Handler = find_exn_handler(RA, ExnHandlers),
+ combine_ras_and_exns(ExnHandlers, MoreRAs, [{Handler, RA} | Acc]).
+
+find_exn_handler(_, []) ->
+ [];
+find_exn_handler(RA, [{Start, End, Handler} | MoreExnHandlers]) ->
+ case (RA >= Start andalso RA =< End) of
+ true ->
+ Handler;
+ false ->
+ find_exn_handler(RA, MoreExnHandlers)
+ end.
+
+create_sdesc_list([], _, _, _, Acc) ->
+ lists:reverse(Acc);
+create_sdesc_list([{ExnLbl, SPOff} | MoreExnAndSPOffs],
+ StkFrameSize, StkArity, LiveRoots, Acc) ->
+ Hdlr = case ExnLbl of
+ 0 -> [];
+ N -> N
+ end,
+ create_sdesc_list(MoreExnAndSPOffs, StkFrameSize, StkArity, LiveRoots,
+ [{?SDESC, SPOff, {Hdlr, StkFrameSize, StkArity, LiveRoots}}
+ | Acc]).
+
+%% @doc This function is responsible for correcting the stack descriptors of
+%% the calls that are found in the code and have more than NR_ARG_REGS
+%% (thus, some of their arguments are passed to the stack). Because of the
+%% Reserved Call Frame feature that the LLVM uses, the stack descriptors
+%% are not correct since at the point of call the frame size is reduced
+%% by the number of arguments that are passed on the stack. Also, the
+%% offsets of the roots need to be re-adjusted.
+fix_stack_descriptors(_, _, [], _) ->
+ [];
+fix_stack_descriptors(RelocsDict, Relocs, SDescs, ExposedClosures) ->
+ %% NamedCalls are MFA and BIF calls that need fix
+ NamedCalls = calls_with_stack_args(RelocsDict),
+ NamedCallsOffs = calls_offsets_arity(Relocs, NamedCalls),
+ ExposedClosures1 =
+ case dict:is_key("table_closures", RelocsDict) of
+ true -> %% A Table with closures exists
+ {table_closures, ArityList} = dict:fetch("table_closures", RelocsDict),
+ case ExposedClosures of
+ {_, Offsets} ->
+ lists:zip(Offsets, ArityList);
+ _ ->
+ exit({?MODULE, fix_stack_descriptors,
+ {"Wrong exposed closures", ExposedClosures}})
+ end;
+ false ->
+ []
+ end,
+ ClosuresOffs = closures_offsets_arity(ExposedClosures1, SDescs),
+ fix_sdescs(NamedCallsOffs ++ ClosuresOffs, SDescs).
+
+%% @doc This function takes as argument the relocation dictionary as produced by
+%% the translation of RTL code to LLVM and finds the names of the calls
+%% (MFA and BIF calls) that have more than NR_ARG_REGS.
+calls_with_stack_args(Dict) ->
+ calls_with_stack_args(dict:to_list(Dict), []).
+
+calls_with_stack_args([], Calls) -> Calls;
+calls_with_stack_args([ {_Name, {call, {M, F, A}}} | Rest], Calls)
+ when A > ?NR_ARG_REGS ->
+ Call =
+ case M of
+ bif -> {F,A};
+ _ -> {M,F,A}
+ end,
+ calls_with_stack_args(Rest, [Call|Calls]);
+calls_with_stack_args([_|Rest], Calls) ->
+ calls_with_stack_args(Rest, Calls).
+
+%% @doc This function extracts the stack arity and the offset in the code of
+%% the named calls (MFAs, BIFs) that have stack arguments.
+calls_offsets_arity(AccRefs, CallsWithStackArgs) ->
+ calls_offsets_arity(AccRefs, CallsWithStackArgs, []).
+
+calls_offsets_arity([], _, Acc) -> Acc;
+calls_offsets_arity([{Type, Offset, Term} | Rest], CallsWithStackArgs, Acc)
+ when Type =:= ?CALL_REMOTE orelse Type =:= ?CALL_LOCAL ->
+ case lists:member(Term, CallsWithStackArgs) of
+ true ->
+ Arity =
+ case Term of
+ {_M, _F, A} -> A;
+ {_F, A} -> A
+ end,
+ calls_offsets_arity(Rest, CallsWithStackArgs,
+ [{Offset + 4, Arity - ?NR_ARG_REGS} | Acc]);
+ false ->
+ calls_offsets_arity(Rest, CallsWithStackArgs, Acc)
+ end;
+calls_offsets_arity([_|Rest], CallsWithStackArgs, Acc) ->
+ calls_offsets_arity(Rest, CallsWithStackArgs, Acc).
+
+%% @doc This function extracts the stack arity and the offsets of closures that
+%% have stack arity. The Closures argument represents the
+%% hipe_bifs:llvm_exposure_closure/0 calls in the code. The actual closure
+%% is the next call in the code, so the offset of the next call must be
+%% calculated from the stack descriptors.
+closures_offsets_arity([], _) ->
+ [];
+closures_offsets_arity(ExposedClosures, SDescs) ->
+ Offsets = [Offset || {_, Offset, _} <- SDescs],
+ %% Offsets and closures must be sorted in order for find_offsets/3 to work
+ SortedOffsets = lists:sort(Offsets),
+ SortedExposedClosures = lists:keysort(1, ExposedClosures),
+ find_offsets(SortedExposedClosures, SortedOffsets, []).
+
+find_offsets([], _, Acc) -> Acc;
+find_offsets([{Off,Arity}|Rest], Offsets, Acc) ->
+ [I | RestOffsets] = lists:dropwhile(fun (Y) -> Y<Off end, Offsets),
+ find_offsets(Rest, RestOffsets, [{I, Arity}|Acc]).
+
+%% The function below corrects the stack descriptors of calls with arguments
+%% that are passed on the stack (more than NR_ARG_REGS) by subtracting the
+%% number of stacked arguments from the frame size and from the offset of the
+%% roots.
+fix_sdescs([], SDescs) -> SDescs;
+fix_sdescs([{Offset, Arity} | Rest], SDescs) ->
+ case lists:keyfind(Offset, 2, SDescs) of
+ false ->
+ fix_sdescs(Rest, SDescs);
+ {?SDESC, Offset, {ExnHandler, FrameSize, StkArity, Roots}} ->
+ FixedRoots = list_to_tuple([Ri - Arity || Ri <- tuple_to_list(Roots)]),
+ FixedSDesc =
+ {?SDESC, Offset, {ExnHandler, FrameSize - Arity, StkArity, FixedRoots}},
+ fix_sdescs(Rest, [FixedSDesc | lists:keydelete(Offset, 2, SDescs)])
+ end.
+
+%%------------------------------------------------------------------------------
+%% Miscellaneous functions
+%%------------------------------------------------------------------------------
+
+%% @doc A function that opens a file as binary. The function takes as argument
+%% the name of the file and returns an Erlang binary.
+-spec open_object_file(string()) -> binary().
+open_object_file(ObjFile) ->
+ case file:read_file(ObjFile) of
+ {ok, Binary} ->
+ Binary;
+ {error, Reason} ->
+ exit({?MODULE, open_file, Reason})
+ end.
+
+remove_temp_folder(Dir, Options) ->
+ case proplists:get_bool(llvm_save_temps, Options) of
+ true -> ok;
+ false -> spawn(fun () -> "" = os:cmd("rm -rf " ++ Dir) end), ok
+ end.
+
+unique_id(FunName, Arity) ->
+ integer_to_list(erlang:phash2({FunName, Arity, now()})).
+
+unique_folder(FunName, Arity, Options) ->
+ DirName = "llvm_" ++ unique_id(FunName, Arity) ++ "/",
+ Dir =
+ case proplists:get_bool(llvm_save_temps, Options) of
+ true -> %% Store folder in current directory
+ DirName;
+ false -> %% Temporarily store folder in tempfs (/dev/shm/)
+ "/dev/shm/" ++ DirName
+ end,
+ %% Make sure it does not exist
+ case dir_exists(Dir) of
+ true -> %% Dir already exists! Generate again.
+ unique_folder(FunName, Arity, Options);
+ false ->
+ Dir
+ end.
+
+%% @doc Function that checks that a given Filename is an existing Directory
+%% Name (from http://rosettacode.org/wiki/Ensure_that_a_file_exists#Erlang)
+dir_exists(Filename) ->
+ {Flag, Info} = file:read_file_info(Filename),
+ (Flag =:= ok) andalso (element(3, Info) =:= directory).
+
+%% @doc Function that takes as arguments a list of integers and a list with
+%% numbers indicating how many items should each tuple have and splits
+%% the original list to a list of lists of integers (with the specified
+%% number of elements), i.e. [ [...], [...] ].
+-spec split_list([integer()], [integer()]) -> [ [integer()] ].
+split_list(List, ElemsPerTuple) ->
+ split_list(List, ElemsPerTuple, []).
+
+-spec split_list([integer()], [integer()], [ [integer()] ]) -> [ [integer()] ].
+split_list([], [], Acc) ->
+ lists:reverse(Acc);
+split_list(List, [NumOfElems | MoreNums], Acc) ->
+ {L1, L2} = lists:split(NumOfElems, List),
+ split_list(L2, MoreNums, [ L1 | Acc]).
diff --git a/lib/hipe/llvm/hipe_llvm_merge.erl b/lib/hipe/llvm/hipe_llvm_merge.erl
new file mode 100644
index 0000000000..3ababfc21a
--- /dev/null
+++ b/lib/hipe/llvm/hipe_llvm_merge.erl
@@ -0,0 +1,114 @@
+%%% -*- erlang-indent-level: 2 -*-
+-module(hipe_llvm_merge).
+
+-export([finalize/3]).
+
+-include("hipe_llvm_arch.hrl").
+-include("../../kernel/src/hipe_ext_format.hrl").
+-include("../rtl/hipe_literals.hrl").
+-include("../main/hipe.hrl").
+
+finalize(CompiledCode, Closures, Exports) ->
+ CompiledCode1 = [CodePack || {_, CodePack} <- CompiledCode],
+ Code = [{MFA, [], ConstTab}
+ || {MFA, _, _ , ConstTab, _, _} <- CompiledCode1],
+ {ConstAlign, ConstSize, ConstMap, RefsFromConsts} =
+ hipe_pack_constants:pack_constants(Code, ?ARCH_REGISTERS:alignment()),
+ %% Compute total code size separately as a sanity check for alignment
+ CodeSize = compute_code_size(CompiledCode1, 0),
+ %% io:format("Code Size (pre-computed): ~w~n", [CodeSize]),
+ {CodeBinary, ExportMap} = merge_mfas(CompiledCode1, 0, <<>>, []),
+ %% io:format("Code Size (post-computed): ~w~n", [byte_size(CodeBinary)]),
+ ?VERBOSE_ASSERT(CodeSize =:= byte_size(CodeBinary)),
+ AccRefs = merge_refs(CompiledCode1, ConstMap, 0, []),
+ %% Bring CompiledCode to a combine_label_maps-acceptable form.
+ LabelMap = combine_label_maps(CompiledCode1, 0, gb_trees:empty()),
+ SC = hipe_pack_constants:slim_constmap(ConstMap),
+ DataRelocs = hipe_pack_constants:mk_data_relocs(RefsFromConsts, LabelMap),
+ SSE = hipe_pack_constants:slim_sorted_exportmap(ExportMap, Closures, Exports),
+ SlimRefs = hipe_pack_constants:slim_refs(AccRefs),
+ term_to_binary([{?VERSION_STRING(),?HIPE_SYSTEM_CRC},
+ ConstAlign, ConstSize,
+ SC, % ConstMap
+ DataRelocs, % LabelMap
+ SSE, % ExportMap
+ CodeSize, CodeBinary, SlimRefs,
+ 0,[] % ColdCodeSize, SlimColdRefs
+ ]).
+
+%% Copied from hipe_x86_assemble.erl
+nr_pad_bytes(Address) ->
+ (4 - (Address rem 4)) rem 4. % XXX: 16 or 32 instead?
+
+align_entry(Address) ->
+ Address + nr_pad_bytes(Address).
+
+compute_code_size([{_MFA, _BinaryCode, CodeSize, _, _, _}|Code], Size) ->
+ compute_code_size(Code, align_entry(Size+CodeSize));
+compute_code_size([], Size) -> Size.
+
+combine_label_maps([{MFA, _, CodeSize, _, _, LabelMap}|Code], Address, CLM) ->
+ NewCLM = merge_label_map(gb_trees:to_list(LabelMap), MFA, Address, CLM),
+ combine_label_maps(Code, align_entry(Address+CodeSize), NewCLM);
+combine_label_maps([], _Address, CLM) -> CLM.
+
+merge_label_map([{Label,Offset}|Rest], MFA, Address, CLM) ->
+ NewCLM = gb_trees:insert({MFA,Label}, Address+Offset, CLM),
+ merge_label_map(Rest, MFA, Address, NewCLM);
+merge_label_map([], _MFA, _Address, CLM) -> CLM.
+
+%% @doc Merge the MFAs' binary code to one continuous binary and compute the
+%% size of this binary. At the same time create an exportmap in a form
+%% of {Address, M, F, A}.
+%% XXX: Is alignment correct/optimal for X86/AMD64?
+merge_mfas([{{M,F,A}, CodeBinary, CodeSize, _, _, _}|Code],
+ Address, AccCode, AccExportMap) ->
+ ?VERBOSE_ASSERT(CodeSize =:= byte_size(CodeBinary)),
+ {Address1, Code1} =
+ case nr_pad_bytes(Address + CodeSize) of
+ 0 -> %% Retains alignment:
+ {Address + CodeSize, CodeBinary};
+ NrPadBytes -> %% Needs padding!
+ Padding = list_to_binary(lists:duplicate(NrPadBytes, 0)),
+ {Address + CodeSize + NrPadBytes, % =:= align_entry(Address+CodeSize)
+ <<CodeBinary/binary, Padding/binary>>}
+ end,
+ ?VERBOSE_ASSERT(Address1 =:=
+ align_entry(Address + CodeSize)), %XXX: Should address be aligned?
+ AccCode1 = <<AccCode/binary, Code1/binary>>,
+ merge_mfas(Code, Address1, AccCode1, [{Address, M, F, A}|AccExportMap]);
+merge_mfas([], _Address, AccCode, AccExportMap) ->
+ {AccCode, AccExportMap}.
+
+%% @doc Merge the references of relocatable symbols in the binary code. The
+%% offsets must be updated because of the merging of the code binaries!
+merge_refs([], _ConstMap, _Addr, AccRefs) -> AccRefs;
+merge_refs([{MFA, _, CodeSize, _, Refs, _}|Rest], ConstMap, Address, AccRefs) ->
+ %% Important!: The hipe_pack_constants:pack_constants/2 function assignes
+ %% unique numbers to constants (ConstNo). This numbers are used from now on,
+ %% instead of labels that were used before. So, in order to be compatible, we
+ %% must change all the constant labels in the Refs to the corresponding
+ %% ConstNo, that can be found in the ConstMap (#pcm_entry{}).
+ UpdatedRefs = [update_ref(label_to_constno(Ref, MFA, ConstMap), Address)
+ || Ref <- Refs],
+ merge_refs(Rest, ConstMap, align_entry(Address+CodeSize),
+ UpdatedRefs++AccRefs).
+
+label_to_constno({Type, Offset, {constant, Label}}, MFA, ConstMap) ->
+ ConstNo = hipe_pack_constants:find_const({MFA, Label}, ConstMap),
+ {Type, Offset, {constant, ConstNo}};
+label_to_constno(Other, _MFA, _ConstMap) ->
+ Other.
+
+%% @doc Update offset to a reference. In case of stack descriptors we must check
+%% if there exists an exception handler, because it must also be updated.
+update_ref({?SDESC, Offset, SDesc}, CodeAddr) ->
+ NewRefAddr = Offset+CodeAddr,
+ case SDesc of
+ {[], _, _, _} -> % No handler; only update offset
+ {?SDESC, NewRefAddr, SDesc};
+ {ExnHandler, FrameSize, StackArity, Roots} -> % Update exception handler
+ {?SDESC, NewRefAddr, {ExnHandler+CodeAddr, FrameSize, StackArity, Roots}}
+ end;
+update_ref({Type, Offset, Term}, CodeAddr) ->
+ {Type, Offset+CodeAddr, Term}.
diff --git a/lib/hipe/llvm/hipe_rtl_to_llvm.erl b/lib/hipe/llvm/hipe_rtl_to_llvm.erl
new file mode 100644
index 0000000000..d7d8d1b049
--- /dev/null
+++ b/lib/hipe/llvm/hipe_rtl_to_llvm.erl
@@ -0,0 +1,1613 @@
+%% -*- erlang-indent-level: 2 -*-
+
+-module(hipe_rtl_to_llvm).
+-author("Chris Stavrakakis, Yiannis Tsiouris").
+
+-export([translate/2]). % the main function of this module
+-export([fix_mfa_name/1]). % a help function used in hipe_llvm_main
+
+-include("../rtl/hipe_rtl.hrl").
+-include("../rtl/hipe_literals.hrl").
+-include("hipe_llvm_arch.hrl").
+
+-define(WORD_WIDTH, (?bytes_to_bits(hipe_rtl_arch:word_size()))).
+-define(BRANCH_META_TAKEN, "0").
+-define(BRANCH_META_NOT_TAKEN, "1").
+
+%%------------------------------------------------------------------------------
+%% @doc Main function for translating an RTL function to LLVM Assembly. Takes as
+%% input the RTL code and the variable indexes of possible garbage
+%% collection roots and returns the corresponing LLVM, a dictionary with
+%% all the relocations in the code and a hipe_consttab() with informaton
+%% about data.
+%%------------------------------------------------------------------------------
+translate(RTL, Roots) ->
+ Fun = hipe_rtl:rtl_fun(RTL),
+ Params = hipe_rtl:rtl_params(RTL),
+ Data = hipe_rtl:rtl_data(RTL),
+ Code = hipe_rtl:rtl_code(RTL),
+ %% Init unique symbol generator and initialize the label counter to the last
+ %% RTL label.
+ hipe_gensym:init(llvm),
+ {_, MaxLabel} = hipe_rtl:rtl_label_range(RTL),
+ put({llvm,label_count}, MaxLabel + 1),
+ %% Put first label of RTL code in process dictionary
+ find_code_entry_label(Code),
+ %% Initialize relocations symbol dictionary
+ Relocs = dict:new(),
+ %% Print RTL to file
+ %% {ok, File_rtl} = file:open("rtl_" ++integer_to_list(random:uniform(2000))
+ %% ++ ".rtl", [write]),
+ %% hipe_rtl:pp(File_rtl, RTL),
+ %% file:close(File_rtl),
+
+ %% Pass on RTL code to handle exception handling and identify labels of Fail
+ %% Blocks
+ {Code1, FailLabels} = fix_code(Code),
+ %% Allocate stack slots for each virtual register and declare gc roots
+ AllocaStackCode = alloca_stack(Code1, Params, Roots),
+ %% Translate Code
+ {LLVM_Code1, Relocs1, NewData} =
+ translate_instr_list(Code1, [], Relocs, Data),
+ %% Create LLVM code to declare relocation symbols as external symbols along
+ %% with local variables in order to use them as just any other variable
+ {FinalRelocs, ExternalDecl, LocalVars} =
+ handle_relocations(Relocs1, Data, Fun),
+ %% Pass on LLVM code in order to create Fail blocks and a landingpad
+ %% instruction to each one
+ LLVM_Code2 = add_landingpads(LLVM_Code1, FailLabels),
+ %% Create LLVM Code for the compiled function
+ LLVM_Code3 = create_function_definition(Fun, Params, LLVM_Code2,
+ AllocaStackCode ++ LocalVars),
+ %% Final Code = CompiledFunction + External Declarations
+ FinalLLVMCode = [LLVM_Code3 | ExternalDecl],
+ {FinalLLVMCode, FinalRelocs, NewData}.
+
+find_code_entry_label([]) ->
+ exit({?MODULE, find_code_entry_label, "Empty code"});
+find_code_entry_label([I|_]) ->
+ case hipe_rtl:is_label(I) of
+ true ->
+ put(first_label, hipe_rtl:label_name(I));
+ false ->
+ exit({?MODULE, find_code_entry_label, "First instruction is not a label"})
+ end.
+
+%% @doc Create a stack slot for each virtual register. The stack slots
+%% that correspond to possible garbage collection roots must be
+%% marked as such.
+alloca_stack(Code, Params, Roots) ->
+ %% Find all assigned virtual registers
+ Destinations = collect_destinations(Code),
+ %% Declare virtual registers, and declare garbage collection roots
+ do_alloca_stack(Destinations++Params, Params, Roots).
+
+collect_destinations(Code) ->
+ lists:usort(lists:flatmap(fun insn_dst/1, Code)).
+
+do_alloca_stack(Destinations, Params, Roots) ->
+ do_alloca_stack(Destinations, Params, Roots, []).
+
+do_alloca_stack([], _, _, Acc) ->
+ Acc;
+do_alloca_stack([D|Ds], Params, Roots, Acc) ->
+ {Name, _I} = trans_dst(D),
+ WordTy = hipe_llvm:mk_int(?WORD_WIDTH),
+ WordTyPtr = hipe_llvm:mk_pointer(WordTy),
+ ByteTyPtr = hipe_llvm:mk_pointer(hipe_llvm:mk_int(8)),
+ case hipe_rtl:is_var(D) of
+ true ->
+ Num = hipe_rtl:var_index(D),
+ I1 = hipe_llvm:mk_alloca(Name, WordTy, [], []),
+ case lists:member(Num, Roots) of
+ true -> %% Variable is a possible Root
+ T1 = mk_temp(),
+ BYTE_TYPE_PP = hipe_llvm:mk_pointer(ByteTyPtr),
+ I2 =
+ hipe_llvm:mk_conversion(T1, bitcast, WordTyPtr, Name, BYTE_TYPE_PP),
+ GcRootArgs = [{BYTE_TYPE_PP, T1}, {ByteTyPtr, "@gc_metadata"}],
+ I3 = hipe_llvm:mk_call([], false, [], [], hipe_llvm:mk_void(),
+ "@llvm.gcroot", GcRootArgs, []),
+ I4 = case lists:member(D, Params) of
+ false ->
+ hipe_llvm:mk_store(WordTy, "-5", WordTyPtr, Name,
+ [], [], false);
+ true -> []
+ end,
+ do_alloca_stack(Ds, Params, Roots, [I1, I2, I3, I4 | Acc]);
+ false ->
+ do_alloca_stack(Ds, Params, Roots, [I1|Acc])
+ end;
+ false ->
+ case hipe_rtl:is_reg(D) andalso isPrecoloured(D) of
+ true -> %% Precoloured registers are mapped to "special" stack slots
+ do_alloca_stack(Ds, Params, Roots, Acc);
+ false ->
+ I1 = case hipe_rtl:is_fpreg(D) of
+ true ->
+ FloatTy = hipe_llvm:mk_double(),
+ hipe_llvm:mk_alloca(Name, FloatTy, [], []);
+ false -> hipe_llvm:mk_alloca(Name, WordTy, [], [])
+ end,
+ do_alloca_stack(Ds, Params, Roots, [I1|Acc])
+ end
+ end.
+
+%%------------------------------------------------------------------------------
+%% @doc Translation of the linearized RTL Code. Each RTL instruction is
+%% translated to a list of LLVM Assembly instructions. The relocation
+%% dictionary is updated when needed.
+%%------------------------------------------------------------------------------
+translate_instr_list([], Acc, Relocs, Data) ->
+ {lists:reverse(lists:flatten(Acc)), Relocs, Data};
+translate_instr_list([I | Is], Acc, Relocs, Data) ->
+ {Acc1, NewRelocs, NewData} = translate_instr(I, Relocs, Data),
+ translate_instr_list(Is, [Acc1 | Acc], NewRelocs, NewData).
+
+translate_instr(I, Relocs, Data) ->
+ case I of
+ #alu{} ->
+ {I2, Relocs2} = trans_alu(I, Relocs),
+ {I2, Relocs2, Data};
+ #alub{} ->
+ {I2, Relocs2} = trans_alub(I, Relocs),
+ {I2, Relocs2, Data};
+ #branch{} ->
+ {I2, Relocs2} = trans_branch(I, Relocs),
+ {I2, Relocs2, Data};
+ #call{} ->
+ {I2, Relocs2} =
+ case hipe_rtl:call_fun(I) of
+ %% In AMD64 this instruction does nothing!
+ %% TODO: chech use of fwait in other architectures!
+ fwait ->
+ {[], Relocs};
+ _ ->
+ trans_call(I, Relocs)
+ end,
+ {I2, Relocs2, Data};
+ #comment{} ->
+ {I2, Relocs2} = trans_comment(I, Relocs),
+ {I2, Relocs2, Data};
+ #enter{} ->
+ {I2, Relocs2} = trans_enter(I, Relocs),
+ {I2, Relocs2, Data};
+ #fconv{} ->
+ {I2, Relocs2} = trans_fconv(I, Relocs),
+ {I2, Relocs2, Data};
+ #fload{} ->
+ {I2, Relocs2} = trans_fload(I, Relocs),
+ {I2, Relocs2, Data};
+ #fmove{} ->
+ {I2, Relocs2} = trans_fmove(I, Relocs),
+ {I2, Relocs2, Data};
+ #fp{} ->
+ {I2, Relocs2} = trans_fp(I, Relocs),
+ {I2, Relocs2, Data};
+ #fp_unop{} ->
+ {I2, Relocs2} = trans_fp_unop(I, Relocs),
+ {I2, Relocs2, Data};
+ #fstore{} ->
+ {I2, Relocs2} = trans_fstore(I, Relocs),
+ {I2, Relocs2, Data};
+ #goto{} ->
+ {I2, Relocs2} = trans_goto(I, Relocs),
+ {I2, Relocs2, Data};
+ #label{} ->
+ {I2, Relocs2} = trans_label(I, Relocs),
+ {I2, Relocs2, Data};
+ #load{} ->
+ {I2, Relocs2} = trans_load(I, Relocs),
+ {I2, Relocs2, Data};
+ #load_address{} ->
+ {I2, Relocs2} = trans_load_address(I, Relocs),
+ {I2, Relocs2, Data};
+ #load_atom{} ->
+ {I2, Relocs2} = trans_load_atom(I, Relocs),
+ {I2, Relocs2, Data};
+ #move{} ->
+ {I2, Relocs2} = trans_move(I, Relocs),
+ {I2, Relocs2, Data};
+ #return{} ->
+ {I2, Relocs2} = trans_return(I, Relocs),
+ {I2, Relocs2, Data};
+ #store{} ->
+ {I2, Relocs2} = trans_store(I, Relocs),
+ {I2, Relocs2, Data};
+ #switch{} -> %% Only switch instruction updates Data
+ {I2, Relocs2, NewData} = trans_switch(I, Relocs, Data),
+ {I2, Relocs2, NewData};
+ Other ->
+ exit({?MODULE, translate_instr, {"Unknown RTL instruction", Other}})
+ end.
+
+%%
+%% alu
+%%
+trans_alu(I, Relocs) ->
+ RtlDst = hipe_rtl:alu_dst(I),
+ TmpDst = mk_temp(),
+ {Src1, I1} = trans_src(hipe_rtl:alu_src1(I)),
+ {Src2, I2} = trans_src(hipe_rtl:alu_src2(I)),
+ Op = trans_op(hipe_rtl:alu_op(I)),
+ WordTy = hipe_llvm:mk_int(?WORD_WIDTH),
+ I3 = hipe_llvm:mk_operation(TmpDst, Op, WordTy, Src1, Src2, []),
+ I4 = store_stack_dst(TmpDst, RtlDst),
+ {[I4, I3, I2, I1], Relocs}.
+
+%%
+%% alub
+%%
+trans_alub(I, Relocs) ->
+ case hipe_rtl:alub_cond(I) of
+ Op when Op =:= overflow orelse Op =:= not_overflow ->
+ trans_alub_overflow(I, signed, Relocs);
+ ltu -> %% ltu means unsigned overflow
+ trans_alub_overflow(I, unsigned, Relocs);
+ _ ->
+ trans_alub_no_overflow(I, Relocs)
+ end.
+
+trans_alub_overflow(I, Sign, Relocs) ->
+ {Src1, I1} = trans_src(hipe_rtl:alub_src1(I)),
+ {Src2, I2} = trans_src(hipe_rtl:alub_src2(I)),
+ RtlDst = hipe_rtl:alub_dst(I),
+ TmpDst = mk_temp(),
+ Name = trans_alub_op(I, Sign),
+ NewRelocs = relocs_store(Name, {call, {llvm, Name, 2}}, Relocs),
+ WordTy = hipe_llvm:mk_int(?WORD_WIDTH),
+ ReturnType = hipe_llvm:mk_struct([WordTy, hipe_llvm:mk_int(1)]),
+ T1 = mk_temp(),
+ I3 = hipe_llvm:mk_call(T1, false, [], [], ReturnType, "@" ++ Name,
+ [{WordTy, Src1}, {WordTy, Src2}], []),
+ %% T1{0}: result of the operation
+ I4 = hipe_llvm:mk_extractvalue(TmpDst, ReturnType, T1 , "0", []),
+ I5 = store_stack_dst(TmpDst, RtlDst),
+ T2 = mk_temp(),
+ %% T1{1}: Boolean variable indicating overflow
+ I6 = hipe_llvm:mk_extractvalue(T2, ReturnType, T1, "1", []),
+ case hipe_rtl:alub_cond(I) of
+ Op when Op =:= overflow orelse Op =:= ltu ->
+ True_label = mk_jump_label(hipe_rtl:alub_true_label(I)),
+ False_label = mk_jump_label(hipe_rtl:alub_false_label(I)),
+ MetaData = branch_metadata(hipe_rtl:alub_pred(I));
+ not_overflow ->
+ True_label = mk_jump_label(hipe_rtl:alub_false_label(I)),
+ False_label = mk_jump_label(hipe_rtl:alub_true_label(I)),
+ MetaData = branch_metadata(1 - hipe_rtl:alub_pred(I))
+ end,
+ I7 = hipe_llvm:mk_br_cond(T2, True_label, False_label, MetaData),
+ {[I7, I6, I5, I4, I3, I2, I1], NewRelocs}.
+
+trans_alub_op(I, Sign) ->
+ Name =
+ case Sign of
+ signed ->
+ case hipe_rtl:alub_op(I) of
+ add -> "llvm.sadd.with.overflow.";
+ mul -> "llvm.smul.with.overflow.";
+ sub -> "llvm.ssub.with.overflow.";
+ Op -> exit({?MODULE, trans_alub_op, {"Unknown alub operator", Op}})
+ end;
+ unsigned ->
+ case hipe_rtl:alub_op(I) of
+ add -> "llvm.uadd.with.overflow.";
+ mul -> "llvm.umul.with.overflow.";
+ sub -> "llvm.usub.with.overflow.";
+ Op -> exit({?MODULE, trans_alub_op, {"Unknown alub operator", Op}})
+ end
+ end,
+ Type =
+ case hipe_rtl_arch:word_size() of
+ 4 -> "i32";
+ 8 -> "i64"
+ %% Other -> exit({?MODULE, trans_alub_op, {"Unknown type", Other}})
+ end,
+ Name ++ Type.
+
+trans_alub_no_overflow(I, Relocs) ->
+ %% alu
+ T = hipe_rtl:mk_alu(hipe_rtl:alub_dst(I), hipe_rtl:alub_src1(I),
+ hipe_rtl:alub_op(I), hipe_rtl:alub_src2(I)),
+ %% A trans_alu instruction cannot change relocations
+ {I1, _} = trans_alu(T, Relocs),
+ %% icmp
+ %% Translate destination as src, to match with the semantics of instruction
+ {Dst, I2} = trans_src(hipe_rtl:alub_dst(I)),
+ Cond = trans_rel_op(hipe_rtl:alub_cond(I)),
+ T3 = mk_temp(),
+ WordTy = hipe_llvm:mk_int(?WORD_WIDTH),
+ I5 = hipe_llvm:mk_icmp(T3, Cond, WordTy, Dst, "0"),
+ %% br
+ Metadata = branch_metadata(hipe_rtl:alub_pred(I)),
+ True_label = mk_jump_label(hipe_rtl:alub_true_label(I)),
+ False_label = mk_jump_label(hipe_rtl:alub_false_label(I)),
+ I6 = hipe_llvm:mk_br_cond(T3, True_label, False_label, Metadata),
+ {[I6, I5, I2, I1], Relocs}.
+
+%%
+%% branch
+%%
+trans_branch(I, Relocs) ->
+ {Src1, I1} = trans_src(hipe_rtl:branch_src1(I)),
+ {Src2, I2} = trans_src(hipe_rtl:branch_src2(I)),
+ Cond = trans_rel_op(hipe_rtl:branch_cond(I)),
+ %% icmp
+ T1 = mk_temp(),
+ WordTy = hipe_llvm:mk_int(?WORD_WIDTH),
+ I3 = hipe_llvm:mk_icmp(T1, Cond, WordTy, Src1, Src2),
+ %% br
+ True_label = mk_jump_label(hipe_rtl:branch_true_label(I)),
+ False_label = mk_jump_label(hipe_rtl:branch_false_label(I)),
+ Metadata = branch_metadata(hipe_rtl:branch_pred(I)),
+ I4 = hipe_llvm:mk_br_cond(T1, True_label, False_label, Metadata),
+ {[I4, I3, I2, I1], Relocs}.
+
+branch_metadata(X) when X =:= 0.5 -> [];
+branch_metadata(X) when X > 0.5 -> ?BRANCH_META_TAKEN;
+branch_metadata(X) when X < 0.5 -> ?BRANCH_META_NOT_TAKEN.
+
+%%
+%% call
+%%
+trans_call(I, Relocs) ->
+ RtlCallArgList= hipe_rtl:call_arglist(I),
+ RtlCallName = hipe_rtl:call_fun(I),
+ {I0, Relocs1} = expose_closure(RtlCallName, RtlCallArgList, Relocs),
+ TmpDst = mk_temp(),
+ {CallArgs, I1} = trans_call_args(RtlCallArgList),
+ FixedRegs = fixed_registers(),
+ {LoadedFixedRegs, I2} = load_fixed_regs(FixedRegs),
+ FinalArgs = fix_reg_args(LoadedFixedRegs) ++ CallArgs,
+ {Name, I3, Relocs2} =
+ trans_call_name(RtlCallName, Relocs1, CallArgs, FinalArgs),
+ T1 = mk_temp(),
+ WordTy = hipe_llvm:mk_int(?WORD_WIDTH),
+ FunRetTy = hipe_llvm:mk_struct(lists:duplicate(?NR_PINNED_REGS + 1, WordTy)),
+ I4 =
+ case hipe_rtl:call_fail(I) of
+ %% Normal Call
+ [] ->
+ hipe_llvm:mk_call(T1, false, "cc 11", [], FunRetTy, Name, FinalArgs,
+ []);
+ %% Call With Exception
+ FailLabelNum ->
+ TrueLabel = "L" ++ integer_to_list(hipe_rtl:call_normal(I)),
+ FailLabel = "%FL" ++ integer_to_list(FailLabelNum),
+ II1 =
+ hipe_llvm:mk_invoke(T1, "cc 11", [], FunRetTy, Name, FinalArgs, [],
+ "%" ++ TrueLabel, FailLabel),
+ II2 = hipe_llvm:mk_label(TrueLabel),
+ [II2, II1]
+ end,
+ I5 = store_fixed_regs(FixedRegs, T1),
+ I6 =
+ case hipe_rtl:call_dstlist(I) of
+ [] -> []; %% No return value
+ [Destination] ->
+ II3 =
+ hipe_llvm:mk_extractvalue(TmpDst, FunRetTy, T1,
+ integer_to_list(?NR_PINNED_REGS), []),
+ II4 = store_stack_dst(TmpDst, Destination),
+ [II4, II3]
+ end,
+ I7 =
+ case hipe_rtl:call_continuation(I) of
+ [] -> []; %% No continuation
+ CC ->
+ {II5, _} = trans_goto(hipe_rtl:mk_goto(CC), Relocs2),
+ II5
+ end,
+ {[I7, I6, I5, I4, I3, I2, I1, I0], Relocs2}.
+
+%% In case of call to a register (closure call) with more than ?NR_ARG_REGS
+%% arguments we must track the offset this call in the code, in order to
+%% to correct the stack descriptor. So, we insert a new Label and add this label
+%% to the "table_closures"
+%% --------------------------------|--------------------------------------------
+%% Old Code | New Code
+%% --------------------------------|--------------------------------------------
+%% | br %ClosureLabel
+%% call %reg(Args) | ClosureLabel:
+%% | call %reg(Args)
+expose_closure(CallName, CallArgs, Relocs) ->
+ CallArgsNr = length(CallArgs),
+ case hipe_rtl:is_reg(CallName) andalso CallArgsNr > ?NR_ARG_REGS of
+ true ->
+ LabelNum = hipe_gensym:new_label(llvm),
+ ClosureLabel = hipe_llvm:mk_label(mk_label(LabelNum)),
+ JumpIns = hipe_llvm:mk_br(mk_jump_label(LabelNum)),
+ Relocs1 =
+ relocs_store({CallName, LabelNum},
+ {closure_label, LabelNum, CallArgsNr - ?NR_ARG_REGS},
+ Relocs),
+ {[ClosureLabel, JumpIns], Relocs1};
+ false ->
+ {[], Relocs}
+ end.
+
+trans_call_name(RtlCallName, Relocs, CallArgs, FinalArgs) ->
+ case RtlCallName of
+ PrimOp when is_atom(PrimOp) ->
+ LlvmName = trans_prim_op(PrimOp),
+ Relocs1 =
+ relocs_store(LlvmName, {call, {bif, PrimOp, length(CallArgs)}}, Relocs),
+ {"@" ++ LlvmName, [], Relocs1};
+ {M, F, A} when is_atom(M), is_atom(F), is_integer(A) ->
+ LlvmName = trans_mfa_name({M, F, A}),
+ Relocs1 =
+ relocs_store(LlvmName, {call, {M, F, length(CallArgs)}}, Relocs),
+ {"@" ++ LlvmName, [], Relocs1};
+ Reg ->
+ case hipe_rtl:is_reg(Reg) of
+ true ->
+ %% In case of a closure call, the register holding the address
+ %% of the closure must be converted to function type in
+ %% order to make the call
+ TT1 = mk_temp(),
+ {RegName, II1} = trans_src(Reg),
+ WordTy = hipe_llvm:mk_int(?WORD_WIDTH),
+ WordTyPtr = hipe_llvm:mk_pointer(WordTy),
+ II2 =
+ hipe_llvm:mk_conversion(TT1, inttoptr, WordTy, RegName, WordTyPtr),
+ TT2 = mk_temp(),
+ ArgsTypeList = lists:duplicate(length(FinalArgs), WordTy),
+ FunRetTy =
+ hipe_llvm:mk_struct(lists:duplicate(?NR_PINNED_REGS + 1, WordTy)),
+ FunType = hipe_llvm:mk_fun(FunRetTy, ArgsTypeList),
+ FunTypeP = hipe_llvm:mk_pointer(FunType),
+ II3 = hipe_llvm:mk_conversion(TT2, bitcast, WordTyPtr, TT1, FunTypeP),
+ {TT2, [II3, II2, II1], Relocs};
+ false ->
+ exit({?MODULE, trans_call, {"Unimplemented call to", RtlCallName}})
+ end
+ end.
+
+%%
+trans_call_args(ArgList) ->
+ {Args, I} = lists:unzip(trans_args(ArgList)),
+ %% Reverse arguments that are passed to stack to match with the Erlang
+ %% calling convention. (Propably not needed in prim calls.)
+ ReversedArgs =
+ case erlang:length(Args) > ?NR_ARG_REGS of
+ false ->
+ Args;
+ true ->
+ {ArgsInRegs, ArgsInStack} = lists:split(?NR_ARG_REGS, Args),
+ ArgsInRegs ++ lists:reverse(ArgsInStack)
+ end,
+ %% Reverse I, because some of the arguments may go out of scope and
+ %% should be killed(store -5). When two or more arguments are they
+ %% same, then order matters!
+ {ReversedArgs, lists:reverse(I)}.
+
+%%
+%% trans_comment
+%%
+trans_comment(I, Relocs) ->
+ I1 = hipe_llvm:mk_comment(hipe_rtl:comment_text(I)),
+ {I1, Relocs}.
+
+%%
+%% enter
+%%
+trans_enter(I, Relocs) ->
+ {CallArgs, I0} = trans_call_args(hipe_rtl:enter_arglist(I)),
+ FixedRegs = fixed_registers(),
+ {LoadedFixedRegs, I1} = load_fixed_regs(FixedRegs),
+ FinalArgs = fix_reg_args(LoadedFixedRegs) ++ CallArgs,
+ {Name, I2, NewRelocs} =
+ trans_call_name(hipe_rtl:enter_fun(I), Relocs, CallArgs, FinalArgs),
+ T1 = mk_temp(),
+ WordTy = hipe_llvm:mk_int(?WORD_WIDTH),
+ FunRetTy = hipe_llvm:mk_struct(lists:duplicate(?NR_PINNED_REGS + 1, WordTy)),
+ I3 = hipe_llvm:mk_call(T1, true, "cc 11", [], FunRetTy, Name, FinalArgs, []),
+ I4 = hipe_llvm:mk_ret([{FunRetTy, T1}]),
+ {[I4, I3, I2, I1, I0], NewRelocs}.
+
+%%
+%% fconv
+%%
+trans_fconv(I, Relocs) ->
+ %% XXX: Can a fconv destination be a precoloured reg?
+ RtlDst = hipe_rtl:fconv_dst(I),
+ TmpDst = mk_temp(),
+ {Src, I1} = trans_float_src(hipe_rtl:fconv_src(I)),
+ FloatTy = hipe_llvm:mk_double(),
+ WordTy = hipe_llvm:mk_int(?WORD_WIDTH),
+ I2 = hipe_llvm:mk_conversion(TmpDst, sitofp, WordTy, Src, FloatTy),
+ I3 = store_float_stack(TmpDst, RtlDst),
+ {[I3, I2, I1], Relocs}.
+
+
+%% TODO: fload, fstore, fmove, and fp are almost the same with load, store, move
+%% and alu. Maybe we should join them.
+
+%%
+%% fload
+%%
+trans_fload(I, Relocs) ->
+ RtlDst = hipe_rtl:fload_dst(I),
+ RtlSrc = hipe_rtl:fload_src(I),
+ _Offset = hipe_rtl:fload_offset(I),
+ TmpDst = mk_temp(),
+ {Src, I1} = trans_float_src(RtlSrc),
+ {Offset, I2} = trans_float_src(_Offset),
+ T1 = mk_temp(),
+ WordTy = hipe_llvm:mk_int(?WORD_WIDTH),
+ FloatTyPtr = hipe_llvm:mk_pointer(hipe_llvm:mk_double()),
+ I3 = hipe_llvm:mk_operation(T1, add, WordTy, Src, Offset, []),
+ T2 = mk_temp(),
+ I4 = hipe_llvm:mk_conversion(T2, inttoptr, WordTy, T1, FloatTyPtr),
+ I5 = hipe_llvm:mk_load(TmpDst, FloatTyPtr, T2, [], [], false),
+ I6 = store_float_stack(TmpDst, RtlDst),
+ {[I6, I5, I4, I3, I2, I1], Relocs}.
+
+%%
+%% fmove
+%%
+trans_fmove(I, Relocs) ->
+ RtlDst = hipe_rtl:fmove_dst(I),
+ RtlSrc = hipe_rtl:fmove_src(I),
+ {Src, I1} = trans_float_src(RtlSrc),
+ I2 = store_float_stack(Src, RtlDst),
+ {[I2, I1], Relocs}.
+
+%%
+%% fp
+%%
+trans_fp(I, Relocs) ->
+ %% XXX: Just copied trans_alu...think again..
+ RtlDst = hipe_rtl:fp_dst(I),
+ RtlSrc1 = hipe_rtl:fp_src1(I),
+ RtlSrc2 = hipe_rtl:fp_src2(I),
+ %% Destination cannot be a precoloured register
+ FloatTy = hipe_llvm:mk_double(),
+ FloatTyPtr = hipe_llvm:mk_pointer(FloatTy),
+ TmpDst = mk_temp(),
+ {Src1, I1} = trans_float_src(RtlSrc1),
+ {Src2, I2} = trans_float_src(RtlSrc2),
+ Op = trans_fp_op(hipe_rtl:fp_op(I)),
+ I3 = hipe_llvm:mk_operation(TmpDst, Op, FloatTy, Src1, Src2, []),
+ I4 = store_float_stack(TmpDst, RtlDst),
+ %% Synchronization for floating point exceptions
+ I5 = hipe_llvm:mk_store(FloatTy, TmpDst, FloatTyPtr, "%exception_sync", [],
+ [], true),
+ T1 = mk_temp(),
+ I6 = hipe_llvm:mk_load(T1, FloatTyPtr, "%exception_sync", [], [], true),
+ {[I6, I5, I4, I3, I2, I1], Relocs}.
+
+%%
+%% fp_unop
+%%
+trans_fp_unop(I, Relocs) ->
+ RtlDst = hipe_rtl:fp_unop_dst(I),
+ RtlSrc = hipe_rtl:fp_unop_src(I),
+ %% Destination cannot be a precoloured register
+ TmpDst = mk_temp(),
+ {Src, I1} = trans_float_src(RtlSrc),
+ Op = trans_fp_op(hipe_rtl:fp_unop_op(I)),
+ FloatTy = hipe_llvm:mk_double(),
+ I2 = hipe_llvm:mk_operation(TmpDst, Op, FloatTy, "0.0", Src, []),
+ I3 = store_float_stack(TmpDst, RtlDst),
+ {[I3, I2, I1], Relocs}.
+%% TODO: Fix fp_unop in a way like the following. You must change trans_dest,
+%% in order to call float_to_list in a case of float constant. Maybe the type
+%% check is expensive...
+%% Dst = hipe_rtl:fp_unop_dst(I),
+%% Src = hipe_rtl:fp_unop_src(I),
+%% Op = hipe_rtl:fp_unop_op(I),
+%% Zero = hipe_rtl:mk_imm(0.0),
+%% I1 = hipe_rtl:mk_fp(Dst, Zero, Op, Src),
+%% trans_fp(I, Relocs1).
+
+%%
+%% fstore
+%%
+trans_fstore(I, Relocs) ->
+ Base = hipe_rtl:fstore_base(I),
+ case isPrecoloured(Base) of
+ true ->
+ trans_fstore_reg(I, Relocs);
+ false ->
+ exit({?MODULE, trans_fstore ,{"Not implemented yet", false}})
+ end.
+
+trans_fstore_reg(I, Relocs) ->
+ {Base, I0} = trans_reg(hipe_rtl:fstore_base(I), dst),
+ WordTy = hipe_llvm:mk_int(?WORD_WIDTH),
+ WordTyPtr = hipe_llvm:mk_pointer(WordTy),
+ FloatTy = hipe_llvm:mk_double(),
+ FloatTyPtr = hipe_llvm:mk_pointer(FloatTy),
+ T1 = mk_temp(),
+ I1 = hipe_llvm:mk_load(T1, WordTyPtr, Base, [], [], false),
+ {Offset, I2} = trans_src(hipe_rtl:fstore_offset(I)),
+ T2 = mk_temp(),
+ I3 = hipe_llvm:mk_operation(T2, add, WordTy, T1, Offset, []),
+ T3 = mk_temp(),
+ I4 = hipe_llvm:mk_conversion(T3, inttoptr, WordTy, T2, FloatTyPtr),
+ {Value, I5} = trans_src(hipe_rtl:fstore_src(I)),
+ I6 = hipe_llvm:mk_store(FloatTy, Value, FloatTyPtr, T3, [], [], false),
+ {[I6, I5, I4, I3, I2, I1, I0], Relocs}.
+
+%%
+%% goto
+%%
+trans_goto(I, Relocs) ->
+ I1 = hipe_llvm:mk_br(mk_jump_label(hipe_rtl:goto_label(I))),
+ {I1, Relocs}.
+
+%%
+%% label
+%%
+trans_label(I, Relocs) ->
+ Label = mk_label(hipe_rtl:label_name(I)),
+ I1 = hipe_llvm:mk_label(Label),
+ {I1, Relocs}.
+
+%%
+%% load
+%%
+trans_load(I, Relocs) ->
+ RtlDst = hipe_rtl:load_dst(I),
+ TmpDst = mk_temp(),
+ %% XXX: Why translate them independently? ------------------------
+ {Src, I1} = trans_src(hipe_rtl:load_src(I)),
+ {Offset, I2} = trans_src(hipe_rtl:load_offset(I)),
+ T1 = mk_temp(),
+ WordTy = hipe_llvm:mk_int(?WORD_WIDTH),
+ WordTyPtr = hipe_llvm:mk_pointer(WordTy),
+ I3 = hipe_llvm:mk_operation(T1, add, WordTy, Src, Offset, []),
+ %%----------------------------------------------------------------
+ I4 = case hipe_rtl:load_size(I) of
+ word ->
+ T2 = mk_temp(),
+ II1 = hipe_llvm:mk_conversion(T2, inttoptr, WordTy, T1, WordTyPtr),
+ II2 = hipe_llvm:mk_load(TmpDst, WordTyPtr, T2, [], [], false),
+ [II2, II1];
+ Size ->
+ LoadType = llvm_type_from_size(Size),
+ LoadTypeP = hipe_llvm:mk_pointer(LoadType),
+ T2 = mk_temp(),
+ II1 = hipe_llvm:mk_conversion(T2, inttoptr, WordTy, T1, LoadTypeP),
+ T3 = mk_temp(),
+ LoadTypePointer = hipe_llvm:mk_pointer(LoadType),
+ II2 = hipe_llvm:mk_load(T3, LoadTypePointer, T2, [], [], false),
+ Conversion =
+ case hipe_rtl:load_sign(I) of
+ signed -> sext;
+ unsigned -> zext
+ end,
+ II3 =
+ hipe_llvm:mk_conversion(TmpDst, Conversion, LoadType, T3, WordTy),
+ [II3, II2, II1]
+ end,
+ I5 = store_stack_dst(TmpDst, RtlDst),
+ {[I5, I4, I3, I2, I1], Relocs}.
+
+%%
+%% load_address
+%%
+trans_load_address(I, Relocs) ->
+ RtlDst = hipe_rtl:load_address_dst(I),
+ RtlAddr = hipe_rtl:load_address_addr(I),
+ {Addr, NewRelocs} =
+ case hipe_rtl:load_address_type(I) of
+ constant ->
+ {"%DL" ++ integer_to_list(RtlAddr) ++ "_var", Relocs};
+ closure ->
+ {{_, ClosureName, _}, _, _} = RtlAddr,
+ FixedClosureName = fix_closure_name(ClosureName),
+ Relocs1 = relocs_store(FixedClosureName, {closure, RtlAddr}, Relocs),
+ {"%" ++ FixedClosureName ++ "_var", Relocs1};
+ type ->
+ exit({?MODULE, trans_load_address,
+ {"Type not implemented in load_address", RtlAddr}})
+ end,
+ I1 = store_stack_dst(Addr, RtlDst),
+ {[I1], NewRelocs}.
+
+%%
+%% load_atom
+%%
+trans_load_atom(I, Relocs) ->
+ RtlDst = hipe_rtl:load_atom_dst(I),
+ RtlAtom = hipe_rtl:load_atom_atom(I),
+ AtomName = "atom_" ++ make_llvm_id(atom_to_list(RtlAtom)),
+ AtomVar = "%" ++ AtomName ++ "_var",
+ NewRelocs = relocs_store(AtomName, {atom, RtlAtom}, Relocs),
+ I1 = store_stack_dst(AtomVar, RtlDst),
+ {[I1], NewRelocs}.
+
+%%
+%% move
+%%
+trans_move(I, Relocs) ->
+ RtlDst = hipe_rtl:move_dst(I),
+ RtlSrc = hipe_rtl:move_src(I),
+ {Src, I1} = trans_src(RtlSrc),
+ I2 = store_stack_dst(Src, RtlDst),
+ {[I2, I1], Relocs}.
+
+%%
+%% return
+%%
+trans_return(I, Relocs) ->
+ WordTy = hipe_llvm:mk_int(?WORD_WIDTH),
+ {VarRet, I1} =
+ case hipe_rtl:return_varlist(I) of
+ [] ->
+ {[], []};
+ [A] ->
+ {Name, II1} = trans_src(A),
+ {[{WordTy, Name}], II1}
+ end,
+ FixedRegs = fixed_registers(),
+ {LoadedFixedRegs, I2} = load_fixed_regs(FixedRegs),
+ FixedRet = [{WordTy, X} || X <- LoadedFixedRegs],
+ Ret = FixedRet ++ VarRet,
+ {RetTypes, _RetNames} = lists:unzip(Ret),
+ Type = hipe_llvm:mk_struct(RetTypes),
+ {RetStruct, I3} = mk_return_struct(Ret, Type),
+ I4 = hipe_llvm:mk_ret([{Type, RetStruct}]),
+ {[I4, I3, I2, I1], Relocs}.
+
+%% @doc Create a structure to hold the return value and the precoloured
+%% registers.
+mk_return_struct(RetValues, Type) ->
+ mk_return_struct(RetValues, Type, [], "undef", 0).
+
+mk_return_struct([], _, Acc, StructName, _) ->
+ {StructName, Acc};
+mk_return_struct([{ElemType, ElemName}|Rest], Type, Acc, StructName, Index) ->
+ T1 = mk_temp(),
+ I1 = hipe_llvm:mk_insertvalue(T1, Type, StructName, ElemType, ElemName,
+ integer_to_list(Index), []),
+ mk_return_struct(Rest, Type, [I1 | Acc], T1, Index+1).
+
+%%
+%% store
+%%
+trans_store(I, Relocs) ->
+ {Base, I1} = trans_src(hipe_rtl:store_base(I)),
+ {Offset, I2} = trans_src(hipe_rtl:store_offset(I)),
+ {Value, I3} = trans_src(hipe_rtl:store_src(I)),
+ T1 = mk_temp(),
+ WordTy = hipe_llvm:mk_int(?WORD_WIDTH),
+ WordTyPtr = hipe_llvm:mk_pointer(WordTy),
+ I4 = hipe_llvm:mk_operation(T1, add, WordTy, Base, Offset, []),
+ I5 =
+ case hipe_rtl:store_size(I) of
+ word ->
+ T2 = mk_temp(),
+ II1 = hipe_llvm:mk_conversion(T2, inttoptr, WordTy, T1, WordTyPtr),
+ II2 = hipe_llvm:mk_store(WordTy, Value, WordTyPtr, T2, [], [],
+ false),
+ [II2, II1];
+ Size ->
+ %% XXX: Is always trunc correct ?
+ LoadType = llvm_type_from_size(Size),
+ LoadTypePointer = hipe_llvm:mk_pointer(LoadType),
+ T2 = mk_temp(),
+ II1 = hipe_llvm:mk_conversion(T2, inttoptr, WordTy, T1, LoadTypePointer),
+ T3 = mk_temp(),
+ II2 = hipe_llvm:mk_conversion(T3, 'trunc', WordTy, Value, LoadType),
+ II3 = hipe_llvm:mk_store(LoadType, T3, LoadTypePointer, T2, [], [], false),
+ [II3, II2, II1]
+ end,
+ {[I5, I4, I3, I2, I1], Relocs}.
+
+%%
+%% switch
+%%
+trans_switch(I, Relocs, Data) ->
+ RtlSrc = hipe_rtl:switch_src(I),
+ {Src, I1} = trans_src(RtlSrc),
+ Labels = hipe_rtl:switch_labels(I),
+ JumpLabels = [mk_jump_label(L) || L <- Labels],
+ SortOrder = hipe_rtl:switch_sort_order(I),
+ NrLabels = length(Labels),
+ ByteTyPtr = hipe_llvm:mk_pointer(hipe_llvm:mk_int(8)),
+ TableType = hipe_llvm:mk_array(NrLabels, ByteTyPtr),
+ TableTypeP = hipe_llvm:mk_pointer(TableType),
+ TypedJumpLabels = [{hipe_llvm:mk_label_type(), X} || X <- JumpLabels],
+ T1 = mk_temp(),
+ {Src2, []} = trans_dst(RtlSrc),
+ TableName = "table_" ++ tl(Src2),
+ WordTy = hipe_llvm:mk_int(?WORD_WIDTH),
+ I2 = hipe_llvm:mk_getelementptr(T1, TableTypeP, "@"++TableName,
+ [{WordTy, "0"}, {WordTy, Src}], false),
+ T2 = mk_temp(),
+ BYTE_TYPE_PP = hipe_llvm:mk_pointer(ByteTyPtr),
+ I3 = hipe_llvm:mk_load(T2, BYTE_TYPE_PP, T1, [], [], false),
+ I4 = hipe_llvm:mk_indirectbr(ByteTyPtr, T2, TypedJumpLabels),
+ LMap = [{label, L} || L <- Labels],
+ %% Update data with the info for the jump table
+ {NewData, JTabLab} =
+ case hipe_rtl:switch_sort_order(I) of
+ [] ->
+ hipe_consttab:insert_block(Data, word, LMap);
+ SortOrder ->
+ hipe_consttab:insert_sorted_block(Data, word, LMap, SortOrder)
+ end,
+ Relocs2 = relocs_store(TableName, {switch, {TableType, Labels, NrLabels,
+ SortOrder}, JTabLab}, Relocs),
+ {[I4, I3, I2, I1], Relocs2, NewData}.
+
+%% @doc Pass on RTL code in order to fix invoke and closure calls.
+fix_code(Code) ->
+ fix_calls(Code).
+
+%% @doc Fix invoke calls and closure calls with more than ?NR_ARG_REGS
+%% arguments.
+fix_calls(Code) ->
+ fix_calls(Code, [], []).
+
+fix_calls([], Acc, FailLabels) ->
+ {lists:reverse(Acc), FailLabels};
+fix_calls([I | Is], Acc, FailLabels) ->
+ case hipe_rtl:is_call(I) of
+ true ->
+ {NewCall, NewFailLabels} =
+ case hipe_rtl:call_fail(I) of
+ [] ->
+ {I, FailLabels};
+ FailLabel ->
+ fix_invoke_call(I, FailLabel, FailLabels)
+ end,
+ fix_calls(Is, [NewCall|Acc], NewFailLabels);
+ false ->
+ fix_calls(Is, [I|Acc], FailLabels)
+ end.
+
+%% @doc When a call has a fail continuation label it must be extended with a
+%% normal continuation label to go with the LLVM's invoke instruction.
+%% FailLabels is the list of labels of all fail blocks, which are needed to
+%% be declared as landing pads. Furtermore, we must add to fail labels a
+%% call to hipe_bifs:llvm_fix_pinned_regs/0 in order to avoid reloading old
+%% values of pinned registers. This may happen because the result of an
+%% invoke instruction is not available at fail-labels, and, thus, we cannot
+%% get the correct values of pinned registers. Finally, the stack needs to
+%% be re-adjusted when there are stack arguments.
+fix_invoke_call(I, FailLabel, FailLabels) ->
+ NewLabel = hipe_gensym:new_label(llvm),
+ NewCall1 = hipe_rtl:call_normal_update(I, NewLabel),
+ SpAdj = find_sp_adj(hipe_rtl:call_arglist(I)),
+ case lists:keyfind(FailLabel, 1, FailLabels) of
+ %% Same fail label with same Stack Pointer adjustment
+ {FailLabel, NewFailLabel, SpAdj} ->
+ NewCall2 = hipe_rtl:call_fail_update(NewCall1, NewFailLabel),
+ {NewCall2, FailLabels};
+ %% Same fail label but with different Stack Pointer adjustment
+ {_, _, _} ->
+ NewFailLabel = hipe_gensym:new_label(llvm),
+ NewCall2 = hipe_rtl:call_fail_update(NewCall1, NewFailLabel),
+ {NewCall2, [{FailLabel, NewFailLabel, SpAdj} | FailLabels]};
+ %% New Fail label
+ false ->
+ NewFailLabel = hipe_gensym:new_label(llvm),
+ NewCall2 = hipe_rtl:call_fail_update(NewCall1, NewFailLabel),
+ {NewCall2, [{FailLabel, NewFailLabel, SpAdj} | FailLabels]}
+ end.
+
+find_sp_adj(ArgList) ->
+ NrArgs = length(ArgList),
+ case NrArgs > ?NR_ARG_REGS of
+ true ->
+ (NrArgs - ?NR_ARG_REGS) * hipe_rtl_arch:word_size();
+ false ->
+ 0
+ end.
+
+%% @doc Add landingpad instruction in Fail Blocks.
+add_landingpads(LLVM_Code, FailLabels) ->
+ FailLabels2 = [convert_label(T) || T <- FailLabels],
+ add_landingpads(LLVM_Code, FailLabels2, []).
+
+add_landingpads([], _, Acc) ->
+ lists:reverse(Acc);
+add_landingpads([I | Is], FailLabels, Acc) ->
+ case hipe_llvm:is_label(I) of
+ true ->
+ Label = hipe_llvm:label_label(I),
+ Ins = create_fail_blocks(Label, FailLabels),
+ add_landingpads(Is, FailLabels, [I | Ins] ++ Acc);
+ false ->
+ add_landingpads(Is, FailLabels, [I | Acc])
+ end.
+
+convert_label({X,Y,Z}) ->
+ {"L" ++ integer_to_list(X), "FL" ++ integer_to_list(Y), Z}.
+
+%% @doc Create a fail block wich.
+create_fail_blocks(_, []) -> [];
+create_fail_blocks(Label, FailLabels) ->
+ create_fail_blocks(Label, FailLabels, []).
+
+create_fail_blocks(Label, FailLabels, Acc) ->
+ case lists:keytake(Label, 1, FailLabels) of
+ false ->
+ Acc;
+ {value, {Label, FailLabel, SpAdj}, RestFailLabels} ->
+ WordTy = hipe_llvm:mk_int(?WORD_WIDTH),
+ I1 = hipe_llvm:mk_label(FailLabel),
+ LP = hipe_llvm:mk_landingpad(),
+ I2 =
+ case SpAdj > 0 of
+ true ->
+ StackPointer = ?ARCH_REGISTERS:reg_name(?ARCH_REGISTERS:sp()),
+ hipe_llvm:mk_adj_stack(integer_to_list(SpAdj), StackPointer,
+ WordTy);
+ false -> []
+ end,
+ T1 = mk_temp(),
+ FixedRegs = fixed_registers(),
+ FunRetTy =
+ hipe_llvm:mk_struct(lists:duplicate(?NR_PINNED_REGS + 1, WordTy)),
+ I3 = hipe_llvm:mk_call(T1, false, "cc 11", [], FunRetTy,
+ "@hipe_bifs.llvm_fix_pinned_regs.0", [], []),
+ I4 = store_fixed_regs(FixedRegs, T1),
+ I5 = hipe_llvm:mk_br("%" ++ Label),
+ Ins = lists:flatten([I5, I4, I3, I2, LP,I1]),
+ create_fail_blocks(Label, RestFailLabels, Ins ++ Acc)
+ end.
+
+%%------------------------------------------------------------------------------
+%% Miscellaneous Functions
+%%------------------------------------------------------------------------------
+
+%% @doc Convert RTL argument list to LLVM argument list.
+trans_args(ArgList) ->
+ WordTy = hipe_llvm:mk_int(?WORD_WIDTH),
+ MakeArg =
+ fun(A) ->
+ {Name, I1} = trans_src(A),
+ {{WordTy, Name}, I1}
+ end,
+ [MakeArg(A) || A <- ArgList].
+
+%% @doc Convert a list of Precoloured registers to LLVM argument list.
+fix_reg_args(ArgList) ->
+ WordTy = hipe_llvm:mk_int(?WORD_WIDTH),
+ [{WordTy, A} || A <- ArgList].
+
+%% @doc Load Precoloured registers.
+load_fixed_regs(RegList) ->
+ Names = [mk_temp_reg(R) || R <- RegList],
+ WordTy = hipe_llvm:mk_int(?WORD_WIDTH),
+ WordTyPtr = hipe_llvm:mk_pointer(WordTy),
+ Fun1 =
+ fun (X, Y) ->
+ hipe_llvm:mk_load(X, WordTyPtr, "%" ++ Y ++ "_reg_var", [], [], false)
+ end,
+ Ins = lists:zipwith(Fun1, Names, RegList),
+ {Names, Ins}.
+
+%% @doc Store Precoloured registers.
+store_fixed_regs(RegList, Name) ->
+ Names = [mk_temp_reg(R) || R <- RegList],
+ Indexes = lists:seq(0, erlang:length(RegList) - 1),
+ WordTy = hipe_llvm:mk_int(?WORD_WIDTH),
+ WordTyPtr = hipe_llvm:mk_pointer(WordTy),
+ FunRetTy = hipe_llvm:mk_struct(lists:duplicate(?NR_PINNED_REGS + 1, WordTy)),
+ Fun1 =
+ fun(X,Y) ->
+ hipe_llvm:mk_extractvalue(X, FunRetTy, Name, integer_to_list(Y), [])
+ end,
+ I1 = lists:zipwith(Fun1, Names, Indexes),
+ Fun2 =
+ fun (X, Y) ->
+ hipe_llvm:mk_store(WordTy, X, WordTyPtr, "%" ++ Y ++ "_reg_var", [], [],
+ false)
+ end,
+ I2 = lists:zipwith(Fun2, Names, RegList),
+ [I2, I1].
+
+%%------------------------------------------------------------------------------
+%% Translation of Names
+%%------------------------------------------------------------------------------
+
+%% @doc Fix F in MFA tuple to acceptable LLVM identifier (case of closure).
+-spec fix_mfa_name(mfa()) -> mfa().
+fix_mfa_name({Mod_Name, Closure_Name, Arity}) ->
+ Fun_Name = list_to_atom(fix_closure_name(Closure_Name)),
+ {Mod_Name, Fun_Name, Arity}.
+
+%% @doc Make an acceptable LLVM identifier for a closure name.
+fix_closure_name(ClosureName) ->
+ make_llvm_id(atom_to_list(ClosureName)).
+
+%% @doc Create an acceptable LLVM identifier.
+make_llvm_id(Name) ->
+ case Name of
+ "" -> "Empty";
+ Other -> lists:flatten([llvm_id(C) || C <- Other])
+ end.
+
+llvm_id(C) when C=:=46; C>47 andalso C<58; C>64 andalso C<91; C=:=95;
+ C>96 andalso C<123 ->
+ C;
+llvm_id(C) ->
+ io_lib:format("_~2.16.0B_",[C]).
+
+%% @doc Create an acceptable LLVM identifier for an MFA.
+trans_mfa_name({M,F,A}) ->
+ N = atom_to_list(M) ++ "." ++ atom_to_list(F) ++ "." ++ integer_to_list(A),
+ make_llvm_id(N).
+
+%%------------------------------------------------------------------------------
+%% Creation of Labels and Temporaries
+%%------------------------------------------------------------------------------
+mk_label(N) ->
+ "L" ++ integer_to_list(N).
+
+mk_jump_label(N) ->
+ "%L" ++ integer_to_list(N).
+
+mk_temp() ->
+ "%t" ++ integer_to_list(hipe_gensym:new_var(llvm)).
+
+mk_temp_reg(Name) ->
+ "%" ++ Name ++ integer_to_list(hipe_gensym:new_var(llvm)).
+
+%%----------------------------------------------------------------------------
+%% Translation of Operands
+%%----------------------------------------------------------------------------
+
+store_stack_dst(TempDst, Dst) ->
+ {Dst2, II1} = trans_dst(Dst),
+ WordTy = hipe_llvm:mk_int(?WORD_WIDTH),
+ WordTyPtr = hipe_llvm:mk_pointer(WordTy),
+ II2 = hipe_llvm:mk_store(WordTy, TempDst, WordTyPtr, Dst2, [], [], false),
+ [II2, II1].
+
+store_float_stack(TempDst, Dst) ->
+ {Dst2, II1} = trans_dst(Dst),
+ FloatTy = hipe_llvm:mk_double(),
+ FloatTyPtr = hipe_llvm:mk_pointer(FloatTy),
+ II2 = hipe_llvm:mk_store(FloatTy, TempDst, FloatTyPtr, Dst2, [], [], false),
+ [II2, II1].
+
+trans_float_src(Src) ->
+ case hipe_rtl:is_const_label(Src) of
+ true ->
+ Name = "@DL" ++ integer_to_list(hipe_rtl:const_label_label(Src)),
+ T1 = mk_temp(),
+ %% XXX: Hardcoded offset
+ ByteTy = hipe_llvm:mk_int(8),
+ ByteTyPtr = hipe_llvm:mk_pointer(ByteTy),
+ I1 = hipe_llvm:mk_getelementptr(T1, ByteTyPtr, Name,
+ [{ByteTy, integer_to_list(?FLOAT_OFFSET)}], true),
+ T2 = mk_temp(),
+ FloatTy = hipe_llvm:mk_double(),
+ FloatTyPtr = hipe_llvm:mk_pointer(FloatTy),
+ I2 = hipe_llvm:mk_conversion(T2, bitcast, ByteTyPtr, T1, FloatTyPtr),
+ T3 = mk_temp(),
+ I3 = hipe_llvm:mk_load(T3, FloatTyPtr, T2, [], [], false),
+ {T3, [I3, I2, I1]};
+ false ->
+ trans_src(Src)
+ end.
+
+trans_src(A) ->
+ WordTy = hipe_llvm:mk_int(?WORD_WIDTH),
+ WordTyPtr = hipe_llvm:mk_pointer(WordTy),
+ case hipe_rtl:is_imm(A) of
+ true ->
+ Value = integer_to_list(hipe_rtl:imm_value(A)),
+ {Value, []};
+ false ->
+ case hipe_rtl:is_reg(A) of
+ true ->
+ case isPrecoloured(A) of
+ true -> trans_reg(A, src);
+ false ->
+ {Name, []} = trans_reg(A, src),
+ T1 = mk_temp(),
+ I1 = hipe_llvm:mk_load(T1, WordTyPtr, Name, [], [], false),
+ {T1, [I1]}
+ end;
+ false ->
+ case hipe_rtl:is_var(A) of
+ true ->
+ RootName = "%vr" ++ integer_to_list(hipe_rtl:var_index(A)),
+ T1 = mk_temp(),
+ I1 = hipe_llvm:mk_load(T1, WordTyPtr, RootName, [], [], false),
+ I2 =
+ case hipe_rtl:var_liveness(A) of
+ live ->
+ [];
+ dead ->
+ NilValue = hipe_tagscheme:mk_nil(),
+ hipe_llvm:mk_store(WordTy, integer_to_list(NilValue), WordTyPtr, RootName,
+ [], [], false)
+ end,
+ {T1, [I2, I1]};
+ false ->
+ case hipe_rtl:is_fpreg(A) of
+ true ->
+ {Name, []} = trans_dst(A),
+ T1 = mk_temp(),
+ FloatTyPtr = hipe_llvm:mk_pointer(hipe_llvm:mk_double()),
+ I1 = hipe_llvm:mk_load(T1, FloatTyPtr, Name, [], [], false),
+ {T1, [I1]};
+ false -> trans_dst(A)
+ end
+ end
+ end
+ end.
+
+trans_dst(A) ->
+ case hipe_rtl:is_reg(A) of
+ true ->
+ trans_reg(A, dst);
+ false ->
+ Name = case hipe_rtl:is_var(A) of
+ true ->
+ "%vr" ++ integer_to_list(hipe_rtl:var_index(A));
+ false ->
+ case hipe_rtl:is_fpreg(A) of
+ true -> "%fr" ++ integer_to_list(hipe_rtl:fpreg_index(A));
+ false ->
+ case hipe_rtl:is_const_label(A) of
+ true ->
+ "%DL" ++ integer_to_list(hipe_rtl:const_label_label(A)) ++ "_var";
+ false ->
+ exit({?MODULE, trans_dst, {"Bad RTL argument",A}})
+ end
+ end
+ end,
+ {Name, []}
+ end.
+
+%% @doc Translate a register. If it is precoloured it must be mapped to the
+%% correct stack slot that holds the precoloured register value.
+trans_reg(Arg, Position) ->
+ Index = hipe_rtl:reg_index(Arg),
+ case isPrecoloured(Arg) of
+ true ->
+ Name = map_precoloured_reg(Index),
+ case Position of
+ src -> fix_reg_src(Name);
+ dst -> fix_reg_dst(Name)
+ end;
+ false ->
+ {hipe_rtl_arch:reg_name(Index), []}
+ end.
+
+map_precoloured_reg(Index) ->
+ case hipe_rtl_arch:reg_name(Index) of
+ "%r15" -> "%hp_reg_var";
+ "%rbp" -> "%p_reg_var";
+ "%esi" -> "%hp_reg_var";
+ "%ebp" -> "%p_reg_var";
+ "%fcalls" ->
+ {"%p_reg_var", ?ARCH_REGISTERS:proc_offset(?ARCH_REGISTERS:fcalls())};
+ "%hplim" ->
+ {"%p_reg_var", ?ARCH_REGISTERS:proc_offset(?ARCH_REGISTERS:heap_limit())};
+ _ ->
+ exit({?MODULE, map_precoloured_reg, {"Register not mapped yet", Index}})
+ end.
+
+%% @doc Load precoloured dst register.
+fix_reg_dst(Register) ->
+ case Register of
+ {Name, Offset} -> %% Case of %fcalls, %hplim
+ WordTy = hipe_llvm:mk_int(?WORD_WIDTH),
+ pointer_from_reg(Name, WordTy, Offset);
+ Name -> %% Case of %p and %hp
+ {Name, []}
+ end.
+
+%% @doc Load precoloured src register.
+fix_reg_src(Register) ->
+ WordTy = hipe_llvm:mk_int(?WORD_WIDTH),
+ WordTyPtr = hipe_llvm:mk_pointer(WordTy),
+ case Register of
+ {Name, Offset} -> %% Case of %fcalls, %hplim
+ {T1, I1} = pointer_from_reg(Name, WordTy, Offset),
+ T2 = mk_temp(),
+ I2 = hipe_llvm:mk_load(T2, WordTyPtr, T1, [], [] , false),
+ {T2, [I2, I1]};
+ Name -> %% Case of %p and %hp
+ T1 = mk_temp(),
+ {T1, hipe_llvm:mk_load(T1, WordTyPtr, Name, [], [], false)}
+ end.
+
+%% @doc Load %fcalls and %hplim.
+pointer_from_reg(RegName, Type, Offset) ->
+ PointerType = hipe_llvm:mk_pointer(Type),
+ T1 = mk_temp(),
+ I1 = hipe_llvm:mk_load(T1, PointerType, RegName, [], [] ,false),
+ T2 = mk_temp(),
+ I2 = hipe_llvm:mk_conversion(T2, inttoptr, Type, T1, PointerType),
+ T3 = mk_temp(),
+ %% XXX: Offsets should be a power of 2.
+ I3 = hipe_llvm:mk_getelementptr(T3, PointerType, T2,
+ [{Type, integer_to_list(Offset div hipe_rtl_arch:word_size())}], true),
+ {T3, [I3, I2, I1]}.
+
+isPrecoloured(X) ->
+ hipe_rtl_arch:is_precoloured(X).
+
+%%------------------------------------------------------------------------------
+%% Translation of operators
+%%------------------------------------------------------------------------------
+
+trans_op(Op) ->
+ case Op of
+ add -> add;
+ sub -> sub;
+ 'or' -> 'or';
+ 'and' -> 'and';
+ 'xor' -> 'xor';
+ sll -> shl;
+ srl -> lshr;
+ sra -> ashr;
+ mul -> mul;
+ 'fdiv' -> fdiv;
+ 'sdiv' -> sdiv;
+ 'srem' -> srem;
+ Other -> exit({?MODULE, trans_op, {"Unknown RTL operator", Other}})
+ end.
+
+trans_rel_op(Op) ->
+ case Op of
+ eq -> eq;
+ ne -> ne;
+ gtu -> ugt;
+ geu -> uge;
+ ltu -> ult;
+ leu -> ule;
+ gt -> sgt;
+ ge -> sge;
+ lt -> slt;
+ le -> sle
+ end.
+
+trans_prim_op(Op) ->
+ case Op of
+ '+' -> "bif_add";
+ '-' -> "bif_sub";
+ '*' -> "bif_mul";
+ 'div' -> "bif_div";
+ '/' -> "bif_div";
+ Other -> atom_to_list(Other)
+ end.
+
+trans_fp_op(Op) ->
+ case Op of
+ fadd -> fadd;
+ fsub -> fsub;
+ fdiv -> fdiv;
+ fmul -> fmul;
+ fchs -> fsub;
+ Other -> exit({?MODULE, trans_fp_op, {"Unknown RTL float operator",Other}})
+ end.
+
+%% Misc.
+insn_dst(I) ->
+ case I of
+ #alu{} ->
+ [hipe_rtl:alu_dst(I)];
+ #alub{} ->
+ [hipe_rtl:alub_dst(I)];
+ #call{} ->
+ case hipe_rtl:call_dstlist(I) of
+ [] -> [];
+ [Dst] -> [Dst]
+ end;
+ #load{} ->
+ [hipe_rtl:load_dst(I)];
+ #load_address{} ->
+ [hipe_rtl:load_address_dst(I)];
+ #load_atom{} ->
+ [hipe_rtl:load_atom_dst(I)];
+ #move{} ->
+ [hipe_rtl:move_dst(I)];
+ #phi{} ->
+ [hipe_rtl:phi_dst(I)];
+ #fconv{} ->
+ [hipe_rtl:fconv_dst(I)];
+ #fload{} ->
+ [hipe_rtl:fload_dst(I)];
+ #fmove{} ->
+ [hipe_rtl:fmove_dst(I)];
+ #fp{} ->
+ [hipe_rtl:fp_dst(I)];
+ #fp_unop{} ->
+ [hipe_rtl:fp_unop_dst(I)];
+ _ ->
+ []
+ end.
+
+llvm_type_from_size(Size) ->
+ case Size of
+ byte -> hipe_llvm:mk_int(8);
+ int16 -> hipe_llvm:mk_int(16);
+ int32 -> hipe_llvm:mk_int(32);
+ word -> hipe_llvm:mk_int(64)
+ end.
+
+%% @doc Create definition for the compiled function. The parameters that are
+%% passed to the stack must be reversed to match with the CC. Also
+%% precoloured registers that are passed as arguments must be stored to
+%% the corresonding stack slots.
+create_function_definition(Fun, Params, Code, LocalVars) ->
+ FunctionName = trans_mfa_name(Fun),
+ FixedRegs = fixed_registers(),
+ %% Reverse parameters to match with the Erlang calling convention
+ ReversedParams =
+ case erlang:length(Params) > ?NR_ARG_REGS of
+ false ->
+ Params;
+ true ->
+ {ParamsInRegs, ParamsInStack} = lists:split(?NR_ARG_REGS, Params),
+ ParamsInRegs ++ lists:reverse(ParamsInStack)
+ end,
+ Args = header_regs(FixedRegs) ++ header_params(ReversedParams),
+ EntryLabel = hipe_llvm:mk_label("Entry"),
+ FloatTy = hipe_llvm:mk_double(),
+ ExceptionSync = hipe_llvm:mk_alloca("%exception_sync", FloatTy, [], []),
+ I2 = load_regs(FixedRegs),
+ I3 = hipe_llvm:mk_br(mk_jump_label(get(first_label))),
+ StoredParams = store_params(Params),
+ EntryBlock =
+ lists:flatten([EntryLabel, ExceptionSync, I2, LocalVars, StoredParams, I3]),
+ Final_Code = EntryBlock ++ Code,
+ FunctionOptions = [nounwind, noredzone, list_to_atom("gc \"erlang\"")],
+ WordTy = hipe_llvm:mk_int(?WORD_WIDTH),
+ FunRetTy = hipe_llvm:mk_struct(lists:duplicate(?NR_PINNED_REGS + 1, WordTy)),
+ hipe_llvm:mk_fun_def([], [], "cc 11", [], FunRetTy, FunctionName, Args,
+ FunctionOptions, [], Final_Code).
+
+header_params(Params) ->
+ WordTy = hipe_llvm:mk_int(?WORD_WIDTH),
+ [{WordTy, "%v" ++ integer_to_list(hipe_rtl:var_index(P))} || P <- Params].
+
+store_params(Params) ->
+ Fun1 =
+ fun(X) ->
+ Index = hipe_rtl:var_index(X),
+ {Name, _} = trans_dst(X),
+ ParamName = "%v" ++ integer_to_list(Index),
+ WordTy = hipe_llvm:mk_int(?WORD_WIDTH),
+ WordTyPtr = hipe_llvm:mk_pointer(WordTy),
+ hipe_llvm:mk_store(WordTy, ParamName, WordTyPtr, Name, [], [], false)
+ end,
+ lists:map(Fun1, Params).
+
+fixed_registers() ->
+ case get(hipe_target_arch) of
+ x86 ->
+ ["hp", "p"];
+ amd64 ->
+ ["hp", "p"];
+ Other ->
+ exit({?MODULE, map_registers, {"Unknown architecture", Other}})
+ end.
+
+header_regs(Registers) ->
+ WordTy = hipe_llvm:mk_int(?WORD_WIDTH),
+ [{WordTy, "%" ++ X ++ "_in"} || X <- Registers].
+
+load_regs(Registers) ->
+ WordTy = hipe_llvm:mk_int(?WORD_WIDTH),
+ WordTyPtr = hipe_llvm:mk_pointer(WordTy),
+ Fun1 =
+ fun(X) ->
+ I1 = hipe_llvm:mk_alloca("%" ++ X ++ "_reg_var", WordTy, [], []),
+ I2 = hipe_llvm:mk_store(WordTy, "%" ++ X ++ "_in", WordTyPtr,
+ "%" ++ X ++ "_reg_var", [], [], false),
+ [I1, I2]
+ end,
+ lists:map(Fun1, Registers).
+
+%%------------------------------------------------------------------------------
+%% Relocation-specific Stuff
+%%------------------------------------------------------------------------------
+
+relocs_store(Key, Value, Relocs) ->
+ dict:store(Key, Value, Relocs).
+
+relocs_to_list(Relocs) ->
+ dict:to_list(Relocs).
+
+%% @doc This function is responsible for the actions needed to handle
+%% relocations:
+%% 1) Updates relocations with constants and switch jump tables.
+%% 2) Creates LLVM code to declare relocations as external
+%% functions/constants.
+%% 3) Creates LLVM code in order to create local variables for the external
+%% constants/labels.
+handle_relocations(Relocs, Data, Fun) ->
+ RelocsList = relocs_to_list(Relocs),
+ %% Seperate Relocations according to their type
+ {CallList, AtomList, ClosureList, ClosureLabels, SwitchList} =
+ seperate_relocs(RelocsList),
+ %% Create code to declare atoms
+ AtomDecl = [declare_atom(A) || A <- AtomList],
+ %% Create code to create local name for atoms
+ AtomLoad = [load_atom(A) || A <- AtomList],
+ %% Create code to declare closures
+ ClosureDecl = [declare_closure(C) || C <- ClosureList],
+ %% Create code to create local name for closures
+ ClosureLoad = [load_closure(C) || C <- ClosureList],
+ %% Find function calls
+ IsExternalCall = fun (X) -> is_external_call(X, Fun) end,
+ ExternalCallList = lists:filter(IsExternalCall, CallList),
+ %% Create code to declare external function
+ FunDecl = fixed_fun_decl() ++ [call_to_decl(C) || C <- ExternalCallList],
+ %% Extract constant labels from Constant Map (remove duplicates)
+ ConstLabels = hipe_consttab:labels(Data),
+ %% Create code to declare constants
+ ConstDecl = [declare_constant(C) || C <- ConstLabels],
+ %% Create code to create local name for constants
+ ConstLoad = [load_constant(C) || C <- ConstLabels],
+ %% Create code to create jump tables
+ SwitchDecl = declare_switches(SwitchList, Fun),
+ %% Create code to create a table with the labels of all closure calls
+ {ClosureLabelDecl, Relocs1} =
+ declare_closure_labels(ClosureLabels, Relocs, Fun),
+ %% Enter constants to relocations
+ Relocs2 = lists:foldl(fun const_to_dict/2, Relocs1, ConstLabels),
+ %% Temporary Store inc_stack and llvm_fix_pinned_regs to Dictionary
+ %% TODO: Remove this
+ Relocs3 = dict:store("inc_stack_0", {call, {bif, inc_stack_0, 0}}, Relocs2),
+ Relocs4 = dict:store("hipe_bifs.llvm_fix_pinned_regs.0",
+ {call, {hipe_bifs, llvm_fix_pinned_regs, 0}}, Relocs3),
+ BranchMetaData = [
+ hipe_llvm:mk_branch_meta(?BRANCH_META_TAKEN, "99", "1")
+ , hipe_llvm:mk_branch_meta(?BRANCH_META_NOT_TAKEN, "1", "99")
+ ],
+ ExternalDeclarations = AtomDecl ++ ClosureDecl ++ ConstDecl ++ FunDecl ++
+ ClosureLabelDecl ++ SwitchDecl ++ BranchMetaData,
+ LocalVariables = AtomLoad ++ ClosureLoad ++ ConstLoad,
+ {Relocs4, ExternalDeclarations, LocalVariables}.
+
+%% @doc Seperate relocations according to their type.
+seperate_relocs(Relocs) ->
+ seperate_relocs(Relocs, [], [], [], [], []).
+
+seperate_relocs([], CallAcc, AtomAcc, ClosureAcc, LabelAcc, JmpTableAcc) ->
+ {CallAcc, AtomAcc, ClosureAcc, LabelAcc, JmpTableAcc};
+seperate_relocs([R|Rs], CallAcc, AtomAcc, ClosureAcc, LabelAcc, JmpTableAcc) ->
+ case R of
+ {_, {call, _}} ->
+ seperate_relocs(Rs, [R | CallAcc], AtomAcc, ClosureAcc, LabelAcc,
+ JmpTableAcc);
+ {_, {atom, _}} ->
+ seperate_relocs(Rs, CallAcc, [R | AtomAcc], ClosureAcc, LabelAcc,
+ JmpTableAcc);
+ {_, {closure, _}} ->
+ seperate_relocs(Rs, CallAcc, AtomAcc, [R | ClosureAcc], LabelAcc,
+ JmpTableAcc);
+ {_, {switch, _, _}} ->
+ seperate_relocs(Rs, CallAcc, AtomAcc, ClosureAcc, LabelAcc,
+ [R | JmpTableAcc]);
+ {_, {closure_label, _, _}} ->
+ seperate_relocs(Rs, CallAcc, AtomAcc, ClosureAcc, [R | LabelAcc],
+ JmpTableAcc)
+ end.
+
+%% @doc External declaration of an atom.
+declare_atom({AtomName, _}) ->
+ WordTy = hipe_llvm:mk_int(?WORD_WIDTH),
+ hipe_llvm:mk_const_decl("@" ++ AtomName, "external constant", WordTy, "").
+
+%% @doc Creation of local variable for an atom.
+load_atom({AtomName, _}) ->
+ Dst = "%" ++ AtomName ++ "_var",
+ Name = "@" ++ AtomName,
+ WordTy = hipe_llvm:mk_int(?WORD_WIDTH),
+ WordTyPtr = hipe_llvm:mk_pointer(WordTy),
+ hipe_llvm:mk_conversion(Dst, ptrtoint, WordTyPtr, Name, WordTy).
+
+%% @doc External declaration of a closure.
+declare_closure({ClosureName, _})->
+ ByteTy = hipe_llvm:mk_int(8),
+ hipe_llvm:mk_const_decl("@" ++ ClosureName, "external constant", ByteTy, "").
+
+%% @doc Creation of local variable for a closure.
+load_closure({ClosureName, _})->
+ Dst = "%" ++ ClosureName ++ "_var",
+ Name = "@" ++ ClosureName,
+ WordTy = hipe_llvm:mk_int(?WORD_WIDTH),
+ ByteTyPtr = hipe_llvm:mk_pointer(hipe_llvm:mk_int(8)),
+ hipe_llvm:mk_conversion(Dst, ptrtoint, ByteTyPtr, Name, WordTy).
+
+%% @doc Declaration of a local variable for a switch jump table.
+declare_switches(JumpTableList, Fun) ->
+ FunName = trans_mfa_name(Fun),
+ [declare_switch_table(X, FunName) || X <- JumpTableList].
+
+declare_switch_table({Name, {switch, {TableType, Labels, _, _}, _}}, FunName) ->
+ LabelList = [mk_jump_label(L) || L <- Labels],
+ Fun1 = fun(X) -> "i8* blockaddress(@" ++ FunName ++ ", " ++ X ++ ")" end,
+ List2 = lists:map(Fun1, LabelList),
+ List3 = string:join(List2, ",\n"),
+ List4 = "[\n" ++ List3 ++ "\n]\n",
+ hipe_llvm:mk_const_decl("@" ++ Name, "constant", TableType, List4).
+
+%% @doc Declaration of a variable for a table with the labels of all closure
+%% calls in the code.
+declare_closure_labels([], Relocs, _Fun) ->
+ {[], Relocs};
+declare_closure_labels(ClosureLabels, Relocs, Fun) ->
+ FunName = trans_mfa_name(Fun),
+ {LabelList, ArityList} =
+ lists:unzip([{mk_jump_label(Label), A} ||
+ {_, {closure_label, Label, A}} <- ClosureLabels]),
+ Relocs1 = relocs_store("table_closures", {table_closures, ArityList}, Relocs),
+ List2 =
+ ["i8* blockaddress(@" ++ FunName ++ ", " ++ L ++ ")" || L <- LabelList],
+ List3 = string:join(List2, ",\n"),
+ List4 = "[\n" ++ List3 ++ "\n]\n",
+ NrLabels = length(LabelList),
+ ByteTyPtr = hipe_llvm:mk_pointer(hipe_llvm:mk_int(8)),
+ TableType = hipe_llvm:mk_array(NrLabels, ByteTyPtr),
+ ConstDecl =
+ hipe_llvm:mk_const_decl("@table_closures", "constant", TableType, List4),
+ {[ConstDecl], Relocs1}.
+
+%% @doc A call is treated as non external only in a case of a recursive
+%% function.
+is_external_call({_, {call, Fun}}, Fun) -> false;
+is_external_call(_, _) -> true.
+
+%% @doc External declaration of a function.
+call_to_decl({Name, {call, MFA}}) ->
+ {M, _F, A} = MFA,
+ CConv = "cc 11",
+ WordTy = hipe_llvm:mk_int(?WORD_WIDTH),
+ FunRetTy = hipe_llvm:mk_struct(lists:duplicate(?NR_PINNED_REGS + 1, WordTy)),
+ {Type, Args} =
+ case M of
+ llvm ->
+ {hipe_llvm:mk_struct([WordTy, hipe_llvm:mk_int(1)]), [1, 2]};
+ %% +precoloured regs
+ _ ->
+ {FunRetTy, lists:seq(1, A + ?NR_PINNED_REGS)}
+ end,
+ ArgsTypes = lists:duplicate(length(Args), WordTy),
+ hipe_llvm:mk_fun_decl([], [], CConv, [], Type, "@" ++ Name, ArgsTypes, []).
+
+%% @doc These functions are always declared, even if not used.
+fixed_fun_decl() ->
+ ByteTy = hipe_llvm:mk_int(8),
+ ByteTyPtr = hipe_llvm:mk_pointer(ByteTy),
+ LandPad = hipe_llvm:mk_fun_decl([], [], [], [], hipe_llvm:mk_int(32),
+ "@__gcc_personality_v0", [hipe_llvm:mk_int(32), hipe_llvm:mk_int(64),
+ ByteTyPtr, ByteTyPtr], []),
+ GCROOTDecl = hipe_llvm:mk_fun_decl([], [], [], [], hipe_llvm:mk_void(),
+ "@llvm.gcroot", [hipe_llvm:mk_pointer(ByteTyPtr), ByteTyPtr], []),
+ WordTy = hipe_llvm:mk_int(?WORD_WIDTH),
+ FunRetTy = hipe_llvm:mk_struct(lists:duplicate(?NR_PINNED_REGS + 1, WordTy)),
+ FixPinnedRegs = hipe_llvm:mk_fun_decl([], [], [], [], FunRetTy,
+ "@hipe_bifs.llvm_fix_pinned_regs.0", [], []),
+ GcMetadata = hipe_llvm:mk_const_decl("@gc_metadata", "external constant",
+ ByteTy, ""),
+ [LandPad, GCROOTDecl, FixPinnedRegs, GcMetadata].
+
+%% @doc Declare an External Consant. We declare all constants as i8 in order to
+%% be able to calcucate pointers of the form DL+6, with the getelementptr
+%% instruction. Otherwise we have to convert constants form pointers to
+%% values, add the offset and convert them again to pointers.
+declare_constant(Label) ->
+ Name = "@DL" ++ integer_to_list(Label),
+ ByteTy = hipe_llvm:mk_int(8),
+ hipe_llvm:mk_const_decl(Name, "external constant", ByteTy, "").
+
+%% @doc Load a constant is achieved by converting a pointer to an integer of
+%% the correct width.
+load_constant(Label) ->
+ Dst = "%DL" ++ integer_to_list(Label) ++ "_var",
+ Name = "@DL" ++ integer_to_list(Label),
+ WordTy = hipe_llvm:mk_int(?WORD_WIDTH),
+ ByteTyPtr = hipe_llvm:mk_pointer(hipe_llvm:mk_int(8)),
+ hipe_llvm:mk_conversion(Dst, ptrtoint, ByteTyPtr, Name, WordTy).
+
+%% @doc Store external constants and calls to dictionary.
+const_to_dict(Elem, Dict) ->
+ Name = "DL" ++ integer_to_list(Elem),
+ dict:store(Name, {'constant', Elem}, Dict).
diff --git a/lib/hipe/main/hipe.app.src b/lib/hipe/main/hipe.app.src
index 7db4db8a57..e81212d4dc 100644
--- a/lib/hipe/main/hipe.app.src
+++ b/lib/hipe/main/hipe.app.src
@@ -30,6 +30,7 @@
cerl_prettypr,
cerl_to_icode,
cerl_typean,
+ elf_format,
erl_bif_types,
erl_types,
hipe,
@@ -108,6 +109,10 @@
hipe_ig,
hipe_ig_moves,
hipe_jit,
+ hipe_llvm,
+ hipe_llvm_liveness,
+ hipe_llvm_main,
+ hipe_llvm_merge,
hipe_ls_regalloc,
hipe_main,
hipe_moves,
@@ -159,6 +164,7 @@
hipe_rtl_symbolic,
hipe_rtl_to_amd64,
hipe_rtl_to_arm,
+ hipe_rtl_to_llvm,
hipe_rtl_to_ppc,
hipe_rtl_to_sparc,
hipe_rtl_to_x86,
@@ -192,7 +198,6 @@
hipe_tagscheme,
hipe_temp_map,
hipe_timing,
- hipe_tool,
hipe_vectors,
hipe_x86,
hipe_x86_assemble,
@@ -217,4 +222,6 @@
hipe_x86_x87]},
{registered,[]},
{applications, [kernel,stdlib]},
- {env, []}]}.
+ {env, []},
+ {runtime_dependencies, ["syntax_tools-1.6.14","stdlib-2.0","kernel-3.0",
+ "erts-6.0","compiler-5.0"]}]}.
diff --git a/lib/hipe/main/hipe.appup.src b/lib/hipe/main/hipe.appup.src
index 1d5a0d93f5..02679fab21 100644
--- a/lib/hipe/main/hipe.appup.src
+++ b/lib/hipe/main/hipe.appup.src
@@ -1,7 +1,7 @@
-%%
+%% -*- erlang -*-
%% %CopyrightBegin%
%%
-%% Copyright Ericsson AB 2002-2009. All Rights Reserved.
+%% Copyright Ericsson AB 2002-2014. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
@@ -15,5 +15,4 @@
%% under the License.
%%
%% %CopyrightEnd%
-%%
-{"%VSN%",[],[]}.
+{"%VSN%", [], []}.
diff --git a/lib/hipe/main/hipe.erl b/lib/hipe/main/hipe.erl
index 434d5c3061..539ce883c0 100644
--- a/lib/hipe/main/hipe.erl
+++ b/lib/hipe/main/hipe.erl
@@ -200,6 +200,7 @@
compile_core/4,
file/1,
file/2,
+ llvm_support_available/0,
load/1,
help/0,
help_hiper/0,
@@ -648,7 +649,18 @@ run_compiler_1(DisasmFun, IcodeFun, Options) ->
%% The full option expansion is not done
%% until the DisasmFun returns.
{Code, CompOpts} = DisasmFun(Options),
- Opts = expand_options(Options ++ CompOpts),
+ Opts0 = expand_options(Options ++ CompOpts),
+ Opts =
+ case proplists:get_bool(to_llvm, Opts0) andalso
+ not llvm_support_available() of
+ true ->
+ ?error_msg("No LLVM version 3.4 or greater "
+ "found in $PATH; aborting "
+ "native code compilation.\n", []),
+ ?EXIT(cant_find_required_llvm_version);
+ false ->
+ Opts0
+ end,
check_options(Opts),
?when_option(verbose, Options,
?debug_msg("Options: ~p.\n",[Opts])),
@@ -821,7 +833,9 @@ finalize_fun_sequential({MFA, Icode}, Opts, Servers) ->
?debug_msg("Compiled ~w in ~.2f s\n", [MFA,(T2-T1)/1000])),
{MFA, Code};
{rtl, LinearRtl} ->
- {MFA, LinearRtl}
+ {MFA, LinearRtl};
+ {llvm_binary, Binary} ->
+ {MFA, Binary}
catch
error:Error ->
?when_option(verbose, Opts, ?debug_untagged_msg("\n", [])),
@@ -890,21 +904,27 @@ do_load(Mod, Bin, BeamBinOrPath) when is_binary(BeamBinOrPath);
end.
assemble(CompiledCode, Closures, Exports, Options) ->
- case get(hipe_target_arch) of
- ultrasparc ->
- hipe_sparc_assemble:assemble(CompiledCode, Closures, Exports, Options);
- powerpc ->
- hipe_ppc_assemble:assemble(CompiledCode, Closures, Exports, Options);
- ppc64 ->
- hipe_ppc_assemble:assemble(CompiledCode, Closures, Exports, Options);
- arm ->
- hipe_arm_assemble:assemble(CompiledCode, Closures, Exports, Options);
- x86 ->
- hipe_x86_assemble:assemble(CompiledCode, Closures, Exports, Options);
- amd64 ->
- hipe_amd64_assemble:assemble(CompiledCode, Closures, Exports, Options);
- Arch ->
- ?EXIT({executing_on_an_unsupported_architecture, Arch})
+ case proplists:get_bool(to_llvm, Options) of
+ false ->
+ case get(hipe_target_arch) of
+ ultrasparc ->
+ hipe_sparc_assemble:assemble(CompiledCode, Closures, Exports, Options);
+ powerpc ->
+ hipe_ppc_assemble:assemble(CompiledCode, Closures, Exports, Options);
+ ppc64 ->
+ hipe_ppc_assemble:assemble(CompiledCode, Closures, Exports, Options);
+ arm ->
+ hipe_arm_assemble:assemble(CompiledCode, Closures, Exports, Options);
+ x86 ->
+ hipe_x86_assemble:assemble(CompiledCode, Closures, Exports, Options);
+ amd64 ->
+ hipe_amd64_assemble:assemble(CompiledCode, Closures, Exports, Options);
+ Arch ->
+ ?EXIT({executing_on_an_unsupported_architecture, Arch})
+ end;
+ true ->
+ %% Merge already compiled code (per MFA) to a single binary.
+ hipe_llvm_merge:finalize(CompiledCode, Closures, Exports)
end.
%% --------------------------------------------------------------------
@@ -1330,6 +1350,11 @@ opt_keys() ->
timeregalloc,
timers,
to_rtl,
+ to_llvm, % Use the LLVM backend for compilation.
+ llvm_save_temps, % Save the LLVM intermediate files in the current
+ % directory; useful for debugging.
+ llvm_llc, % Specify llc optimization-level: o1, o2, o3, undefined.
+ llvm_opt, % Specify opt optimization-level: o1, o2, o3, undefined.
use_indexing,
use_inline_atom_search,
use_callgraph,
@@ -1468,11 +1493,19 @@ opt_expansions() ->
[{o1, o1_opts()},
{o2, o2_opts()},
{o3, o3_opts()},
+ {to_llvm, llvm_opts(o3)},
+ {{to_llvm, o0}, llvm_opts(o0)},
+ {{to_llvm, o1}, llvm_opts(o1)},
+ {{to_llvm, o2}, llvm_opts(o2)},
+ {{to_llvm, o3}, llvm_opts(o3)},
{x87, [x87, inline_fp]},
{inline_fp, case get(hipe_target_arch) of %% XXX: Temporary until x86
x86 -> [x87, inline_fp]; %% has sse2
_ -> [inline_fp] end}].
+llvm_opts(O) ->
+ [to_llvm, {llvm_opt, O}, {llvm_llc, O}].
+
%% This expands "basic" options, which may be tested early and cannot be
%% in conflict with options found in the source code.
@@ -1516,4 +1549,22 @@ check_options(Opts) ->
ok
end.
+-spec llvm_support_available() -> boolean().
+
+llvm_support_available() ->
+ get_llvm_version() >= 3.4.
+
+get_llvm_version() ->
+ OptStr = os:cmd("opt -version"),
+ SubStr = "LLVM version ", N = length(SubStr),
+ case string:str(OptStr, SubStr) of
+ 0 -> % No opt available
+ 0.0;
+ S ->
+ case string:to_float(string:sub_string(OptStr, S + N)) of
+ {error, _} -> 0.0; %XXX: Assumes no revision numbers in versioning
+ {Float, _} -> Float
+ end
+ end.
+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
diff --git a/lib/hipe/main/hipe_main.erl b/lib/hipe/main/hipe_main.erl
index 99028cc3c1..89b79998be 100644
--- a/lib/hipe/main/hipe_main.erl
+++ b/lib/hipe/main/hipe_main.erl
@@ -49,7 +49,7 @@
%%=====================================================================
-type comp_icode_ret() :: {'native',hipe_architecture(),{'unprofiled',_}}
- | {'rtl',tuple()}.
+ | {'rtl',tuple()} | {'llvm_binary',term()}.
%%=====================================================================
@@ -115,11 +115,18 @@ compile_icode(MFA, LinearIcode0, Options, Servers, DebugState) ->
pp(IcodeCfg7, MFA, icode_liveness, pp_icode_liveness, Options, Servers),
FinalIcode = hipe_icode_cfg:cfg_to_linear(IcodeCfg7),
?opt_stop_timer("Icode"),
- LinearRTL = ?option_time(icode_to_rtl(MFA,FinalIcode,Options, Servers),
- "RTL", Options),
+ {LinearRTL, Roots} = ?option_time(icode_to_rtl(MFA, FinalIcode, Options, Servers),
+ "RTL", Options),
case proplists:get_bool(to_rtl, Options) of
false ->
- rtl_to_native(MFA, LinearRTL, Options, DebugState);
+ case proplists:get_bool(to_llvm, Options) of
+ false ->
+ rtl_to_native(MFA, LinearRTL, Options, DebugState);
+ true ->
+ %% The LLVM backend returns binary code, unlike the rest of the HiPE
+ %% backends which return native assembly.
+ rtl_to_llvm_to_binary(MFA, LinearRTL, Roots, Options, DebugState)
+ end;
true ->
put(hipe_debug, DebugState),
{rtl, LinearRTL}
@@ -385,11 +392,21 @@ icode_to_rtl(MFA, Icode, Options, Servers) ->
%% hipe_rtl_cfg:pp(RtlCfg3),
pp(RtlCfg3, MFA, rtl_liveness, pp_rtl_liveness, Options, Servers),
RtlCfg4 = rtl_lcm(RtlCfg3, Options),
- pp(RtlCfg4, MFA, rtl, pp_rtl, Options, Servers),
- LinearRTL1 = hipe_rtl_cfg:linearize(RtlCfg4),
+ %% LLVM: A liveness analysis on RTL must be performed in order to find the GC
+ %% roots and explicitly mark them (in RTL) when they go out of scope (only
+ %% when the LLVM backend is used).
+ {RtlCfg5, Roots} =
+ case proplists:get_bool(to_llvm, Options) of
+ false ->
+ {RtlCfg4, []};
+ true ->
+ hipe_llvm_liveness:analyze(RtlCfg4)
+ end,
+ pp(RtlCfg5, MFA, rtl, pp_rtl, Options, Servers),
+ LinearRTL1 = hipe_rtl_cfg:linearize(RtlCfg5),
LinearRTL2 = hipe_rtl_cleanup_const:cleanup(LinearRTL1),
%% hipe_rtl:pp(standard_io, LinearRTL2),
- LinearRTL2.
+ {LinearRTL2, Roots}.
translate_to_rtl(Icode, Options) ->
%% GC tests should have been added in the conversion to Icode.
@@ -540,6 +557,17 @@ rtl_to_native(MFA, LinearRTL, Options, DebugState) ->
put(hipe_debug, DebugState),
LinearNativeCode.
+%% Translate Linear RTL to binary code using LLVM.
+rtl_to_llvm_to_binary(MFA, LinearRTL, Roots, Options, DebugState) ->
+ ?opt_start_timer("LLVM native code"),
+ %% BinaryCode is a tuple, as defined in llvm/hipe_llvm_main module, which
+ %% contains the binary code together with info needed by the loader, e.g.
+ %% ConstTab, Refs, LabelMap, etc.
+ BinaryCode = hipe_llvm_main:rtl_to_native(MFA, LinearRTL, Roots, Options),
+ ?opt_stop_timer("LLVM native code"),
+ put(hipe_debug, DebugState),
+ {llvm_binary, BinaryCode}.
+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Debugging stuff ...
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
diff --git a/lib/hipe/misc/hipe_consttab.erl b/lib/hipe/misc/hipe_consttab.erl
index c381e6a057..2b02f54b5c 100644
--- a/lib/hipe/misc/hipe_consttab.erl
+++ b/lib/hipe/misc/hipe_consttab.erl
@@ -2,7 +2,7 @@
%%
%% %CopyrightBegin%
%%
-%% Copyright Ericsson AB 2001-2009. All Rights Reserved.
+%% Copyright Ericsson AB 2001-2014. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
@@ -462,7 +462,7 @@ update_referred_labels(Table, LabelMap) ->
tree_keys(T) ->
dict:fetch_keys(T).
--spec tree_to_list(dict()) -> [{_, _}].
+-spec tree_to_list(dict:dict()) -> [{_, _}].
tree_to_list(T) ->
dict:to_list(T).
@@ -486,11 +486,11 @@ tree_lookup(Key, T) ->
none
end.
--spec tree_empty() -> dict().
+-spec tree_empty() -> dict:dict().
tree_empty() ->
dict:new().
--spec tree_lookup_key_for_value(ctdata(), dict()) -> 'none' | {'value', _}.
+-spec tree_lookup_key_for_value(ctdata(), dict:dict()) -> 'none' | {'value', _}.
tree_lookup_key_for_value(Val, T) ->
tree_lookup_key_for_value_1(tree_to_list(T), Val).
diff --git a/lib/hipe/misc/hipe_consttab.hrl b/lib/hipe/misc/hipe_consttab.hrl
index 39018dac34..aea3c5bc88 100644
--- a/lib/hipe/misc/hipe_consttab.hrl
+++ b/lib/hipe/misc/hipe_consttab.hrl
@@ -2,7 +2,7 @@
%%
%% %CopyrightBegin%
%%
-%% Copyright Ericsson AB 2008-2009. All Rights Reserved.
+%% Copyright Ericsson AB 2008-2014. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
@@ -22,6 +22,6 @@
-type ct_alignment() :: 4 | 8.
-type hipe_constlbl() :: non_neg_integer().
--type hipe_consttab() :: {dict(), [hipe_constlbl()], hipe_constlbl()}.
+-type hipe_consttab() :: {dict:dict(), [hipe_constlbl()], hipe_constlbl()}.
%%-----------------------------------------------------------------------------
diff --git a/lib/hipe/misc/hipe_gensym.erl b/lib/hipe/misc/hipe_gensym.erl
index 84fc8fa7e8..4d2a237188 100644
--- a/lib/hipe/misc/hipe_gensym.erl
+++ b/lib/hipe/misc/hipe_gensym.erl
@@ -44,7 +44,7 @@
%% Types of allowable entities to set global variables for
%%-----------------------------------------------------------------------
--type gvarname() :: 'icode' | 'rtl' | 'arm' | 'ppc' | 'sparc' | 'x86'.
+-type gvarname() :: 'icode' | 'rtl' | 'arm' | 'ppc' | 'sparc' | 'x86' | 'llvm'.
%%-----------------------------------------------------------------------
diff --git a/lib/hipe/misc/hipe_pack_constants.erl b/lib/hipe/misc/hipe_pack_constants.erl
index e214d7ebbc..300f9ae43a 100644
--- a/lib/hipe/misc/hipe_pack_constants.erl
+++ b/lib/hipe/misc/hipe_pack_constants.erl
@@ -3,7 +3,7 @@
%%
%% %CopyrightBegin%
%%
-%% Copyright Ericsson AB 2003-2009. All Rights Reserved.
+%% Copyright Ericsson AB 2003-2014. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
@@ -20,30 +20,48 @@
%%
-module(hipe_pack_constants).
--export([pack_constants/2, slim_refs/1, slim_constmap/1]).
+-export([pack_constants/2, slim_refs/1, slim_constmap/1,
+ find_const/2, mk_data_relocs/2, slim_sorted_exportmap/3]).
-include("hipe_consttab.hrl").
-include("../../kernel/src/hipe_ext_format.hrl").
+-include("../main/hipe.hrl"). % Needed for the EXIT macro in find_const/2.
%%-----------------------------------------------------------------------------
--type raw_data() :: binary() | number() | list() | tuple().
--type tbl_ref() :: {hipe_constlbl(), non_neg_integer()}.
+-type const_num() :: non_neg_integer().
+-type raw_data() :: binary() | number() | list() | tuple().
+
+-type addr() :: non_neg_integer().
+-type ref_p() :: {DataPos :: hipe_constlbl(), CodeOffset :: addr()}.
+-type ref() :: ref_p() | {'sorted', Base :: addr(), [ref_p()]}.
+
+-type mfa_refs() :: {mfa(), [ref()]}.
+
+%% XXX: these types may not belong here: FIX!
+-type fa() :: {atom(), arity()}.
+-type export_map() :: [{addr(), module(), atom(), arity()}].
-record(pcm_entry, {mfa :: mfa(),
label :: hipe_constlbl(),
- const_num :: non_neg_integer(),
- start :: non_neg_integer(),
+ const_num :: const_num(),
+ start :: addr(),
type :: 0 | 1 | 2,
raw_data :: raw_data()}).
+-type pcm_entry() :: #pcm_entry{}.
+
+-type label_map() :: gb_trees:tree({mfa(), hipe_constlbl()}, addr()).
+
+%% Some of the following types may possibly need to be exported
+-type data_relocs() :: [ref()].
+-type packed_const_map() :: [pcm_entry()].
+-type mfa_refs_map() :: [mfa_refs()].
+-type slim_export_map() :: [addr() | module() | atom() | arity() | boolean()].
%%-----------------------------------------------------------------------------
-spec pack_constants([{mfa(),[_],hipe_consttab()}], ct_alignment()) ->
- {ct_alignment(),
- non_neg_integer(),
- [#pcm_entry{}],
- [{mfa(),[tbl_ref() | {'sorted',non_neg_integer(),[tbl_ref()]}]}]}.
+ {ct_alignment(), non_neg_integer(), packed_const_map(), mfa_refs_map()}.
pack_constants(Data, Align) ->
pack_constants(Data, 0, Align, 0, [], []).
@@ -194,13 +212,12 @@ compact_dests([], Dest, AccofDest, Acc) ->
%% to the slimmed and flattened format ConstMap which is put in object
%% files.
%%
--spec slim_constmap([#pcm_entry{}]) -> [raw_data()].
+-spec slim_constmap(packed_const_map()) -> [raw_data()].
slim_constmap(Map) ->
slim_constmap(Map, gb_sets:new(), []).
--spec slim_constmap([#pcm_entry{}], gb_set(), [raw_data()]) -> [raw_data()].
-slim_constmap([#pcm_entry{const_num=ConstNo, start=Offset,
- type=Type, raw_data=Term}|Rest], Inserted, Acc) ->
+slim_constmap([#pcm_entry{const_num = ConstNo, start = Offset,
+ type = Type, raw_data = Term}|Rest], Inserted, Acc) ->
case gb_sets:is_member(ConstNo, Inserted) of
true ->
slim_constmap(Rest, Inserted, Acc);
@@ -209,3 +226,60 @@ slim_constmap([#pcm_entry{const_num=ConstNo, start=Offset,
slim_constmap(Rest, NewInserted, [ConstNo, Offset, Type, Term|Acc])
end;
slim_constmap([], _Inserted, Acc) -> Acc.
+
+%%
+%% Lookup a constant in a ConstMap.
+%%
+-spec find_const({mfa(), hipe_constlbl()}, packed_const_map()) -> const_num().
+
+find_const({MFA, Label}, [E = #pcm_entry{mfa = MFA, label = Label}|_]) ->
+ E#pcm_entry.const_num;
+find_const(N, [_|R]) ->
+ find_const(N, R);
+find_const(C, []) ->
+ ?EXIT({constant_not_found, C}).
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+%%
+%% Functions to build and handle Refs, ExportMap and LabelMap.
+%% Note: Moved here because they are used by all backends in
+%% hipe_{arm,sparc,ppc,x86}_assemble.erl
+%% XXX: Is this the right place for them?
+%%
+
+-spec mk_data_relocs(mfa_refs_map(), label_map()) -> data_relocs().
+
+mk_data_relocs(RefsFromConsts, LabelMap) ->
+ lists:flatten(mk_data_relocs(RefsFromConsts, LabelMap, [])).
+
+mk_data_relocs([{MFA, Labels} | Rest], LabelMap, Acc) ->
+ Map = [case Label of
+ {L,Pos} ->
+ Offset = find({MFA,L}, LabelMap),
+ {Pos,Offset};
+ {sorted,Base,OrderedLabels} ->
+ {sorted, Base, [begin
+ Offset = find({MFA,L}, LabelMap),
+ {Order, Offset}
+ end
+ || {L,Order} <- OrderedLabels]}
+ end
+ || Label <- Labels],
+ %% msg("Map: ~w Map\n", [Map]),
+ mk_data_relocs(Rest, LabelMap, [Map,Acc]);
+mk_data_relocs([], _, Acc) -> Acc.
+
+find({MFA,L}, LabelMap) ->
+ gb_trees:get({MFA,L}, LabelMap).
+
+-spec slim_sorted_exportmap(export_map(), [mfa()], [fa()]) -> slim_export_map().
+
+slim_sorted_exportmap([{Addr,M,F,A}|Rest], Closures, Exports) ->
+ IsClosure = lists:member({M,F,A}, Closures),
+ IsExported = is_exported(F, A, Exports),
+ [Addr,M,F,A,IsClosure,IsExported | slim_sorted_exportmap(Rest, Closures, Exports)];
+slim_sorted_exportmap([], _, _) -> [].
+
+is_exported(F, A, Exports) ->
+ lists:member({F,A}, Exports).
diff --git a/lib/hipe/misc/hipe_sdi.erl b/lib/hipe/misc/hipe_sdi.erl
index ef1b5b48c5..9a2ff78ecf 100644
--- a/lib/hipe/misc/hipe_sdi.erl
+++ b/lib/hipe/misc/hipe_sdi.erl
@@ -3,7 +3,7 @@
%%%
%%% %CopyrightBegin%
%%%
-%%% Copyright Ericsson AB 2004-2009. All Rights Reserved.
+%%% Copyright Ericsson AB 2004-2014. All Rights Reserved.
%%%
%%% The contents of this file are subject to the Erlang Public License,
%%% Version 1.1, (the "License"); you may not use this file except in
@@ -50,7 +50,7 @@
-record(pass1, {prevSdi :: integer(),
preS = [] :: [#pre_sdi_data{}],
- labelMap = gb_trees:empty() :: gb_tree()}).
+ labelMap = gb_trees:empty() :: gb_trees:tree()}).
-record(sdi_data, {address :: address(),
label_address :: address(),
@@ -105,11 +105,11 @@ pass1_add_sdi(Pass1, Address, Label, SdiInfo) ->
PreSdiData = #pre_sdi_data{address=Address, label=Label, si=SdiInfo},
Pass1#pass1{prevSdi=PrevSdi+1, preS=[PreSdiData|PreS]}.
--spec pass1_finalise(#pass1{}) -> {non_neg_integer(),tuple(),gb_tree()}.
+-spec pass1_finalise(#pass1{}) -> {non_neg_integer(),tuple(),gb_trees:tree()}.
pass1_finalise(#pass1{prevSdi=PrevSdi, preS=PreS, labelMap=LabelMap}) ->
{PrevSdi+1, pass1_finalise_preS(PreS, LabelMap, []), LabelMap}.
--spec pass1_finalise_preS([#pre_sdi_data{}], gb_tree(), [#sdi_data{}]) ->
+-spec pass1_finalise_preS([#pre_sdi_data{}], gb_trees:tree(), [#sdi_data{}]) ->
tuple().
pass1_finalise_preS([], _LabelMap, S) -> vector_from_list(S);
pass1_finalise_preS([PreSdiData|PreS], LabelMap, S) ->
@@ -122,7 +122,7 @@ pass1_finalise_preS([PreSdiData|PreS], LabelMap, S) ->
%%% Pass2.
--spec pass2(#pass1{}) -> {gb_tree(), non_neg_integer()}.
+-spec pass2(#pass1{}) -> {gb_trees:tree(), non_neg_integer()}.
pass2(Pass1) ->
{N,SDIS,LabelMap} = pass1_finalise(Pass1),
LONG = mk_long(N),
@@ -339,13 +339,14 @@ initINCR(SdiNr, PrevIncr, N, LONG, INCREMENT) ->
%%% a and previous sdi i is remapped to a+incr(i), where
%%% incr(i) = if i < 0 then 0 else INCREMENT[i].
--spec adjust_label_map(gb_tree(), hipe_array()) -> gb_tree().
+-spec adjust_label_map(gb_trees:tree(), hipe_array()) -> gb_trees:tree().
adjust_label_map(LabelMap, INCREMENT) ->
applyIncr(gb_trees:to_list(LabelMap), INCREMENT, gb_trees:empty()).
-type label_pair() :: {label(), #label_data{}}.
--spec applyIncr([label_pair()], hipe_array(), gb_tree()) -> gb_tree().
+-spec applyIncr([label_pair()], hipe_array(), gb_trees:tree()) ->
+ gb_trees:tree().
applyIncr([], _INCREMENT, LabelMap) -> LabelMap;
applyIncr([{Label,LabelData}|List], INCREMENT, LabelMap) ->
#label_data{address=Address, prevSdi=PrevSdi} = LabelData,
diff --git a/lib/hipe/ppc/hipe_ppc_assemble.erl b/lib/hipe/ppc/hipe_ppc_assemble.erl
index b2fd50517b..3ad91f4051 100644
--- a/lib/hipe/ppc/hipe_ppc_assemble.erl
+++ b/lib/hipe/ppc/hipe_ppc_assemble.erl
@@ -46,8 +46,8 @@ assemble(CompiledCode, Closures, Exports, Options) ->
print("Total num bytes=~w\n", [CodeSize], Options),
%%
SC = hipe_pack_constants:slim_constmap(ConstMap),
- DataRelocs = mk_data_relocs(RefsFromConsts, LabelMap),
- SSE = slim_sorted_exportmap(ExportMap,Closures,Exports),
+ DataRelocs = hipe_pack_constants:mk_data_relocs(RefsFromConsts, LabelMap),
+ SSE = hipe_pack_constants:slim_sorted_exportmap(ExportMap,Closures,Exports),
SlimRefs = hipe_pack_constants:slim_refs(AccRefs),
Bin = term_to_binary([{?VERSION_STRING(),?HIPE_SYSTEM_CRC},
ConstAlign, ConstSize,
@@ -288,7 +288,7 @@ do_pseudo_li(I, MFA, ConstMap) ->
%%% end,
%%% {load_address, {Tag,untag_mfa_or_prim(MFAorPrim)}};
{Label,constant} ->
- ConstNo = find_const({MFA,Label}, ConstMap),
+ ConstNo = hipe_pack_constants:find_const({MFA,Label}, ConstMap),
{load_address, {constant,ConstNo}};
{Label,closure} ->
{load_address, {closure,Label}};
@@ -574,37 +574,6 @@ mk_y(Pred, BD) ->
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-mk_data_relocs(RefsFromConsts, LabelMap) ->
- lists:flatten(mk_data_relocs(RefsFromConsts, LabelMap, [])).
-
-mk_data_relocs([{MFA,Labels} | Rest], LabelMap, Acc) ->
- Map = [case Label of
- {L,Pos} ->
- Offset = find({MFA,L}, LabelMap),
- {Pos,Offset};
- {sorted,Base,OrderedLabels} ->
- {sorted, Base, [begin
- Offset = find({MFA,L}, LabelMap),
- {Order, Offset}
- end
- || {L,Order} <- OrderedLabels]}
- end
- || Label <- Labels],
- %% msg("Map: ~w Map\n",[Map]),
- mk_data_relocs(Rest, LabelMap, [Map,Acc]);
-mk_data_relocs([],_,Acc) -> Acc.
-
-find({_MFA,_L} = MFAL,LabelMap) ->
- gb_trees:get(MFAL, LabelMap).
-
-slim_sorted_exportmap([{Addr,M,F,A}|Rest], Closures, Exports) ->
- IsClosure = lists:member({M,F,A}, Closures),
- IsExported = is_exported(F, A, Exports),
- [Addr,M,F,A,IsClosure,IsExported | slim_sorted_exportmap(Rest, Closures, Exports)];
-slim_sorted_exportmap([],_,_) -> [].
-
-is_exported(F, A, Exports) -> lists:member({F,A}, Exports).
-
%%%
%%% Assembly listing support (pp_asm option).
%%%
@@ -642,14 +611,3 @@ fill_spaces(N) when N > 0 ->
fill_spaces(N-1);
fill_spaces(0) ->
[].
-
-%%%
-%%% Lookup a constant in a ConstMap.
-%%%
-
-find_const({MFA,Label}, [{pcm_entry,MFA,Label,ConstNo,_,_,_}|_]) ->
- ConstNo;
-find_const(N, [_|R]) ->
- find_const(N, R);
-find_const(C, []) ->
- ?EXIT({constant_not_found,C}).
diff --git a/lib/hipe/regalloc/hipe_coalescing_regalloc.erl b/lib/hipe/regalloc/hipe_coalescing_regalloc.erl
index 7169dd18f3..e231098e0a 100644
--- a/lib/hipe/regalloc/hipe_coalescing_regalloc.erl
+++ b/lib/hipe/regalloc/hipe_coalescing_regalloc.erl
@@ -1,8 +1,8 @@
-%% -*- coding: utf-8; erlang-indent-level: 2 -*-
+%% -*- erlang-indent-level: 2 -*-
%%
%% %CopyrightBegin%
%%
-%% Copyright Ericsson AB 2001-2012. All Rights Reserved.
+%% Copyright Ericsson AB 2001-2013. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
diff --git a/lib/hipe/regalloc/hipe_ig_moves.erl b/lib/hipe/regalloc/hipe_ig_moves.erl
index 186c87a690..ebc6ebc20d 100644
--- a/lib/hipe/regalloc/hipe_ig_moves.erl
+++ b/lib/hipe/regalloc/hipe_ig_moves.erl
@@ -2,7 +2,7 @@
%%
%% %CopyrightBegin%
%%
-%% Copyright Ericsson AB 2001-2009. All Rights Reserved.
+%% Copyright Ericsson AB 2001-2014. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
@@ -36,7 +36,7 @@
-record(ig_moves, {movelist :: hipe_vector(),
nrmoves = 0 :: non_neg_integer(),
moveinsns = [] :: [{_,_}],
- moveset = gb_sets:empty() :: gb_set()}).
+ moveset = gb_sets:empty() :: gb_sets:set()}).
%%-----------------------------------------------------------------------------
diff --git a/lib/hipe/regalloc/hipe_ls_regalloc.erl b/lib/hipe/regalloc/hipe_ls_regalloc.erl
index 4276b8f968..7a00a0534a 100644
--- a/lib/hipe/regalloc/hipe_ls_regalloc.erl
+++ b/lib/hipe/regalloc/hipe_ls_regalloc.erl
@@ -722,7 +722,7 @@ is_free(R, Free) ->
is_free(R, Free, []).
is_free(R, [{R,_}|Rest], Acc) ->
- {true,lists:reverse(Acc)++Rest};
+ {true, lists:reverse(Acc, Rest)};
is_free(R, [X|Rs],Acc) ->
is_free(R, Rs, [X|Acc]);
is_free(_, [], _) ->
@@ -733,7 +733,7 @@ exists_free_register(Start, Regs) ->
exists_free_register(Start, [{Phys, Start0}|Rest], Acc)
when Start > Start0 ->
- {true, Phys, lists:reverse(Acc)++Rest};
+ {true, Phys, lists:reverse(Acc, Rest)};
exists_free_register(Start, [Free|Rest], Acc) ->
exists_free_register(Start, Rest, [Free|Acc]);
exists_free_register(_, [], _) ->
diff --git a/lib/hipe/regalloc/hipe_optimistic_regalloc.erl b/lib/hipe/regalloc/hipe_optimistic_regalloc.erl
index fc3718cbc0..0278a896d2 100644
--- a/lib/hipe/regalloc/hipe_optimistic_regalloc.erl
+++ b/lib/hipe/regalloc/hipe_optimistic_regalloc.erl
@@ -1,8 +1,8 @@
-%% -*- coding: utf-8; erlang-indent-level: 2 -*-
+%% -*- erlang-indent-level: 2 -*-
%%
%% %CopyrightBegin%
%%
-%% Copyright Ericsson AB 2005-2012. All Rights Reserved.
+%% Copyright Ericsson AB 2005-2013. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
@@ -958,9 +958,9 @@ splits_2({Cols, NonCols, OldSpillCost}, L, SpillCost) ->
%% Merge two ordered sub-splits into one.
spillCostOrderedMerge(Spl1, [], Spl) ->
- lists:reverse(Spl) ++ Spl1;
+ lists:reverse(Spl, Spl1);
spillCostOrderedMerge([], Spl2, Spl) ->
- lists:reverse(Spl) ++ Spl2;
+ lists:reverse(Spl, Spl2);
spillCostOrderedMerge(Spl1, Spl2, Spl) ->
{_, _, SpillCost1} = hd(Spl1),
{_, _, SpillCost2} = hd(Spl2),
diff --git a/lib/hipe/regalloc/hipe_reg_worklists.erl b/lib/hipe/regalloc/hipe_reg_worklists.erl
index e22cc8dc07..897bf0ef77 100644
--- a/lib/hipe/regalloc/hipe_reg_worklists.erl
+++ b/lib/hipe/regalloc/hipe_reg_worklists.erl
@@ -1,8 +1,8 @@
-%%% -*- coding: utf-8; erlang-indent-level: 2 -*-
+%%% -*- erlang-indent-level: 2 -*-
%%%
%%% %CopyrightBegin%
%%%
-%%% Copyright Ericsson AB 2001-2012. All Rights Reserved.
+%%% Copyright Ericsson AB 2001-2013. All Rights Reserved.
%%%
%%% The contents of this file are subject to the Erlang Public License,
%%% Version 1.1, (the "License"); you may not use this file except in
diff --git a/lib/hipe/rtl/hipe_icode2rtl.erl b/lib/hipe/rtl/hipe_icode2rtl.erl
index 034153a3cb..483d0b37f7 100644
--- a/lib/hipe/rtl/hipe_icode2rtl.erl
+++ b/lib/hipe/rtl/hipe_icode2rtl.erl
@@ -427,8 +427,6 @@ gen_type_test([X], Type, TrueLbl, FalseLbl, Pred, ConstTab) ->
hipe_rtl:mk_branch(X, eq, TmpF, TrueLbl, FalseLbl, Pred)], ConstTab};
cons ->
{hipe_tagscheme:test_cons(X, TrueLbl, FalseLbl, Pred), ConstTab};
- constant ->
- {hipe_tagscheme:test_constant(X, TrueLbl, FalseLbl, Pred), ConstTab};
fixnum ->
{hipe_tagscheme:test_fixnum(X, TrueLbl, FalseLbl, Pred), ConstTab};
float ->
@@ -439,6 +437,8 @@ gen_type_test([X], Type, TrueLbl, FalseLbl, Pred, ConstTab) ->
{hipe_tagscheme:test_integer(X, TrueLbl, FalseLbl, Pred), ConstTab};
list ->
{hipe_tagscheme:test_list(X, TrueLbl, FalseLbl, Pred), ConstTab};
+ map ->
+ {hipe_tagscheme:test_map(X, TrueLbl, FalseLbl, Pred), ConstTab};
nil ->
{hipe_tagscheme:test_nil(X, TrueLbl, FalseLbl, Pred), ConstTab};
number ->
diff --git a/lib/hipe/rtl/hipe_rtl.erl b/lib/hipe/rtl/hipe_rtl.erl
index 4bf4eb6bd7..bc61bec0bd 100644
--- a/lib/hipe/rtl/hipe_rtl.erl
+++ b/lib/hipe/rtl/hipe_rtl.erl
@@ -29,7 +29,7 @@
%% <li> {alu, Dst, Src1, Op, Src2} </li>
%% <li> {alub, Dst, Src1, Op, Src2, RelOp, TrueLabel, FalseLabel, P} </li>
%% <li> {branch, Src1, Src2, RelOp, TrueLabel, FalseLabel, P} </li>
-%% <li> {call, DsListt, Fun, ArgList, Type, Continuation, FailContinuation}
+%% <li> {call, DsListt, Fun, ArgList, Type, Continuation, FailContinuation, NormalContinuation}
%% Type is one of {local, remote, primop, closure} </li>
%% <li> {comment, Text} </li>
%% <li> {enter, Fun, ArgList, Type}
@@ -106,7 +106,7 @@
%% rtl_data_update/2,
%% rtl_var_range/1,
%% rtl_var_range_update/2,
- %% rtl_label_range/1,
+ rtl_label_range/1,
%% rtl_label_range_update/2,
rtl_info/1,
rtl_info_update/2]).
@@ -226,6 +226,7 @@
%% goto_label_update/2,
mk_call/6,
+ mk_call/7,
call_fun/1,
call_dstlist/1,
call_dstlist_update/2,
@@ -233,8 +234,10 @@
call_continuation/1,
call_fail/1,
call_type/1,
+ call_normal/1,
+ call_normal_update/2,
%% call_continuation_update/2,
- %% call_fail_update/2,
+ call_fail_update/2,
is_call/1,
mk_enter/3,
@@ -290,10 +293,13 @@
%% fconv_src_update/2,
%% is_fconv/1,
- %% mk_var/1,
+ mk_var/1,
+ mk_var/2,
mk_new_var/0,
is_var/1,
var_index/1,
+ var_liveness/1,
+ var_liveness_update/2,
%% change_vars_to_regs/1,
@@ -350,10 +356,15 @@
%% move_dst_update/2,
fixnumop_dst_update/2,
pp_instr/2,
- %% pp_arg/2,
+ %% Uber hack!
+ pp_var/2,
+ pp_reg/2,
+ pp_arg/2,
phi_arglist_update/2,
phi_redirect_pred/3]).
+-export([subst_uses_llvm/2]).
+
-export_type([alub_cond/0]).
%%
@@ -387,7 +398,7 @@ rtl_data(#rtl{data=Data}) -> Data.
%% rtl_data_update(Rtl, Data) -> Rtl#rtl{data=Data}.
%% rtl_var_range(#rtl{var_range=VarRange}) -> VarRange.
%% rtl_var_range_update(Rtl, VarRange) -> Rtl#rtl{var_range=VarRange}.
-%% rtl_label_range(#rtl{label_range=LabelRange}) -> LabelRange.
+rtl_label_range(#rtl{label_range=LabelRange}) -> LabelRange.
%% rtl_label_range_update(Rtl, LabelRange) -> Rtl#rtl{label_range=LabelRange}.
rtl_info(#rtl{info=Info}) -> Info.
rtl_info_update(Rtl, Info) -> Rtl#rtl{info=Info}.
@@ -643,6 +654,17 @@ is_goto(_) -> false.
%% call
%%
+%% LLVM: Call with normal continuation
+mk_call(DstList, Fun, ArgList, Continuation, FailContinuation,
+ NormalContinuation, Type) ->
+ case Type of
+ remote -> ok;
+ not_remote -> ok
+ end,
+ #call{dstlist=DstList, 'fun'=Fun, arglist=ArgList, type=Type,
+ continuation=Continuation, failcontinuation=FailContinuation,
+ normalcontinuation=NormalContinuation}.
+
mk_call(DstList, Fun, ArgList, Continuation, FailContinuation, Type) ->
case Type of
remote -> ok;
@@ -651,6 +673,10 @@ mk_call(DstList, Fun, ArgList, Continuation, FailContinuation, Type) ->
#call{dstlist=DstList, 'fun'=Fun, arglist=ArgList, type=Type,
continuation=Continuation,
failcontinuation=FailContinuation}.
+
+call_normal(#call{normalcontinuation=NormalContinuation}) -> NormalContinuation.
+call_normal_update(C, NewNormalContinuation) ->
+ C#call{normalcontinuation=NewNormalContinuation}.
call_dstlist(#call{dstlist=DstList}) -> DstList.
call_dstlist_update(C, NewDstList) -> C#call{dstlist=NewDstList}.
call_fun(#call{'fun'=Fun}) -> Fun.
@@ -853,11 +879,14 @@ reg_is_gcsafe(#rtl_reg{is_gc_safe=IsGcSafe}) -> IsGcSafe.
is_reg(#rtl_reg{}) -> true;
is_reg(_) -> false.
--record(rtl_var, {index :: non_neg_integer()}).
+-record(rtl_var, {index :: non_neg_integer(), liveness=live :: dead | live}).
mk_var(Num) when is_integer(Num), Num >= 0 -> #rtl_var{index=Num}.
+mk_var(Num, Liveness) when is_integer(Num), Num>=0 -> #rtl_var{index=Num, liveness=Liveness}.
mk_new_var() -> mk_var(hipe_gensym:get_next_var(rtl)).
var_index(#rtl_var{index=Index}) -> Index.
+var_liveness(#rtl_var{liveness=Liveness}) -> Liveness.
+var_liveness_update(RtlVar, Liveness) -> RtlVar#rtl_var{liveness=Liveness}.
is_var(#rtl_var{}) -> true;
is_var(_) -> false.
@@ -1077,6 +1106,131 @@ subst_uses(Subst, I) ->
switch_src_update(I, subst1(Subst, switch_src(I)))
end.
+subst_uses_llvm(Subst, I) ->
+ case I of
+ #alu{} ->
+ {NewSrc2, Subst1} = subst1_llvm(Subst, alu_src2(I)),
+ {NewSrc1, _ } = subst1_llvm(Subst1, alu_src1(I)),
+ I0 = alu_src1_update(I, NewSrc1),
+ alu_src2_update(I0, NewSrc2);
+ #alub{} ->
+ {NewSrc2, Subst1} = subst1_llvm(Subst, alub_src2(I)),
+ {NewSrc1, _ } = subst1_llvm(Subst1, alub_src1(I)),
+ I0 = alub_src1_update(I, NewSrc1),
+ alub_src2_update(I0, NewSrc2);
+ #branch{} ->
+ {NewSrc2, Subst1} = subst1_llvm(Subst, branch_src2(I)),
+ {NewSrc1, _ } = subst1_llvm(Subst1, branch_src1(I)),
+ I0 = branch_src1_update(I, NewSrc1),
+ branch_src2_update(I0, NewSrc2);
+ #call{} ->
+ case call_is_known(I) of
+ false ->
+ {NewFun, Subst1} = subst1_llvm(Subst, call_fun(I)),
+ {NewArgList, _} = subst_list_llvm(Subst1, call_arglist(I)),
+ I0 = call_fun_update(I, NewFun),
+ call_arglist_update(I0, NewArgList);
+ true ->
+ {NewArgList, _} = subst_list_llvm(Subst, call_arglist(I)),
+ call_arglist_update(I, NewArgList)
+ end;
+ #comment{} ->
+ I;
+ #enter{} ->
+ case enter_is_known(I) of
+ false ->
+ {NewFun, Subst1} = subst1_llvm(Subst, enter_fun(I)),
+ {NewArgList, _} = subst_list_llvm(Subst1, enter_arglist(I)),
+ I0 = enter_fun_update(I, NewFun),
+ enter_arglist_update(I0, NewArgList);
+ true ->
+ {NewArgList, _} = subst_list_llvm(Subst, enter_arglist(I)),
+ enter_arglist_update(I, NewArgList)
+ end;
+ #fconv{} ->
+ {NewSrc, _ } = subst1_llvm(Subst, fconv_src(I)),
+ fconv_src_update(I, NewSrc);
+ #fixnumop{} ->
+ {NewSrc, _ } = subst1_llvm(Subst, fixnumop_src(I)),
+ fixnumop_src_update(I, NewSrc);
+ #fload{} ->
+ {NewSrc, Subst1} = subst1_llvm(Subst, fload_src(I)),
+ {NewOffset, _ } = subst1_llvm(Subst1, fload_offset(I)),
+ I0 = fload_src_update(I, NewSrc),
+ fload_offset_update(I0, NewOffset);
+ #fmove{} ->
+ {NewSrc, _ } = subst1_llvm(Subst, fmove_src(I)),
+ fmove_src_update(I, NewSrc);
+ #fp{} ->
+ {NewSrc2, Subst1} = subst1_llvm(Subst, fp_src2(I)),
+ {NewSrc1, _ } = subst1_llvm(Subst1, fp_src1(I)),
+ I0 = fp_src1_update(I, NewSrc1),
+ fp_src2_update(I0, NewSrc2);
+ #fp_unop{} ->
+ {NewSrc, _ } = subst1_llvm(Subst, fp_unop_src(I)),
+ fp_unop_src_update(I, NewSrc);
+ #fstore{} ->
+ {NewSrc, Subst1} = subst1_llvm(Subst, fstore_src(I)),
+ {NewBase, Subst2} = subst1_llvm(Subst1, fstore_base(I)),
+ {NewOffset, _ } = subst1_llvm(Subst2, fstore_offset(I)),
+ I0 = fstore_src_update(I, NewSrc),
+ I1 = fstore_base_update(I0, NewBase),
+ fstore_offset_update(I1, NewOffset);
+ #goto{} ->
+ I;
+ #goto_index{} ->
+ I;
+ #gctest{} ->
+ {NewWords, _ } = subst1_llvm(Subst, gctest_words(I)),
+ gctest_words_update(I, NewWords);
+ #label{} ->
+ I;
+ #load{} ->
+ {NewSrc, Subst1} = subst1_llvm(Subst, load_src(I)),
+ {NewOffset, _ } = subst1_llvm(Subst1, load_offset(I)),
+ I0 = load_src_update(I, NewSrc),
+ load_offset_update(I0, NewOffset);
+ #load_address{} ->
+ I;
+ #load_atom{} ->
+ I;
+ #load_word_index{} ->
+ I;
+ #move{} ->
+ {NewSrc, _ } = subst1_llvm(Subst, move_src(I)),
+ move_src_update(I, NewSrc);
+ #multimove{} ->
+ {NewSrcList, _} = subst_list_llvm(Subst, multimove_srclist(I)),
+ multimove_srclist_update(I, NewSrcList);
+ #phi{} ->
+ phi_argvar_subst(I, Subst);
+ #return{} ->
+ {NewVarList, _} = subst_list_llvm(Subst, return_varlist(I)),
+ return_varlist_update(I, NewVarList);
+ #store{} ->
+ {NewSrc, Subst1} = subst1_llvm(Subst, store_src(I)),
+ {NewBase, Subst2} = subst1_llvm(Subst1, store_base(I)),
+ {NewOffset, _ } = subst1_llvm(Subst2, store_offset(I)),
+ I0 = store_src_update(I, NewSrc),
+ I1 = store_base_update(I0, NewBase),
+ store_offset_update(I1, NewOffset);
+ #switch{} ->
+ {NewSrc, _ } = subst1_llvm(Subst, switch_src(I)),
+ switch_src_update(I, NewSrc)
+ end.
+
+subst_list_llvm(S,X) -> subst_list_llvm(S, lists:reverse(X), []).
+subst_list_llvm(S, [], Acc) -> {Acc, S};
+subst_list_llvm(S, [X|Xs], Acc) ->
+ {NewX, RestS} = subst1_llvm(S, X),
+ subst_list_llvm(RestS, Xs, [NewX|Acc]).
+
+subst1_llvm(A,B) -> subst1_llvm(A,B,[]).
+
+subst1_llvm([], X, Acc) -> {X, Acc};
+subst1_llvm([{X,Y}|Rs], X, Acc) -> {Y, Acc++Rs};
+subst1_llvm([R|Xs], X, Acc) -> subst1_llvm(Xs,X,[R|Acc]).
+
subst_defines(Subst, I)->
case I of
#alu{} ->
@@ -1614,7 +1768,11 @@ pp_var(Dev, Arg) ->
true ->
pp_hard_reg(Dev, var_index(Arg));
false ->
- io:format(Dev, "v~w", [var_index(Arg)])
+ io:format(Dev, "v~w", [var_index(Arg)]),
+ case var_liveness(Arg) of
+ dead -> io:format(Dev, "(dead)", []);
+ _ -> ok
+ end
end.
pp_arg(Dev, A) ->
diff --git a/lib/hipe/rtl/hipe_rtl.hrl b/lib/hipe/rtl/hipe_rtl.hrl
index 974e40f830..fbdf9ac524 100644
--- a/lib/hipe/rtl/hipe_rtl.hrl
+++ b/lib/hipe/rtl/hipe_rtl.hrl
@@ -28,7 +28,8 @@
-record(alu, {dst, src1, op, src2}).
-record(alub, {dst, src1, op, src2, 'cond', true_label, false_label, p}).
-record(branch, {src1, src2, 'cond', true_label, false_label, p}).
--record(call, {dstlist, 'fun', arglist, type, continuation, failcontinuation}).
+-record(call, {dstlist, 'fun', arglist, type, continuation,
+ failcontinuation, normalcontinuation = []}).
-record(comment, {text}).
-record(enter, {'fun', arglist, type}).
-record(fconv, {dst, src}).
diff --git a/lib/hipe/rtl/hipe_rtl_arith.inc b/lib/hipe/rtl/hipe_rtl_arith.inc
index 7b587e882d..1d13e59420 100644
--- a/lib/hipe/rtl/hipe_rtl_arith.inc
+++ b/lib/hipe/rtl/hipe_rtl_arith.inc
@@ -1,9 +1,9 @@
%% -*- Erlang -*-
-%% -*- coding: utf-8; erlang-indent-level: 2 -*-
+%% -*- erlang-indent-level: 2 -*-
%%
%% %CopyrightBegin%
%%
-%% Copyright Ericsson AB 2004-2012. All Rights Reserved.
+%% Copyright Ericsson AB 2004-2013. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
diff --git a/lib/hipe/rtl/hipe_rtl_liveness.erl b/lib/hipe/rtl/hipe_rtl_liveness.erl
index 3cfada9d6c..0c4b6b2e11 100644
--- a/lib/hipe/rtl/hipe_rtl_liveness.erl
+++ b/lib/hipe/rtl/hipe_rtl_liveness.erl
@@ -34,7 +34,8 @@
-module(hipe_rtl_liveness).
-%% -define(LIVEOUT_NEEDED,true). % needed for liveness.inc below.
+%% -define(DEBUG_LIVENESS,true).
+-define(LIVEOUT_NEEDED,true). % needed for liveness.inc below.
-define(PRETTY_PRINT,false).
-include("hipe_rtl.hrl").
diff --git a/lib/hipe/rtl/hipe_rtl_mk_switch.erl b/lib/hipe/rtl/hipe_rtl_mk_switch.erl
index d859c50b7d..c14fa5628e 100644
--- a/lib/hipe/rtl/hipe_rtl_mk_switch.erl
+++ b/lib/hipe/rtl/hipe_rtl_mk_switch.erl
@@ -1,8 +1,8 @@
-%% -*- coding: utf-8; erlang-indent-level: 2 -*-
+%% -*- erlang-indent-level: 2 -*-
%%
%% %CopyrightBegin%
%%
-%% Copyright Ericsson AB 2001-2012. All Rights Reserved.
+%% Copyright Ericsson AB 2001-2013. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
diff --git a/lib/hipe/rtl/hipe_rtl_ssa_const_prop.erl b/lib/hipe/rtl/hipe_rtl_ssa_const_prop.erl
index 1c900d767e..2f594333c1 100644
--- a/lib/hipe/rtl/hipe_rtl_ssa_const_prop.erl
+++ b/lib/hipe/rtl/hipe_rtl_ssa_const_prop.erl
@@ -1,8 +1,8 @@
-%% -*- coding: utf-8; erlang-indent-level: 2 -*-
+%% -*- erlang-indent-level: 2 -*-
%%
%% %CopyrightBegin%
%%
-%% Copyright Ericsson AB 2004-2012. All Rights Reserved.
+%% Copyright Ericsson AB 2004-2013. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
diff --git a/lib/hipe/rtl/hipe_rtl_ssapre.erl b/lib/hipe/rtl/hipe_rtl_ssapre.erl
index 34897ba4b7..2ebebb5197 100644
--- a/lib/hipe/rtl/hipe_rtl_ssapre.erl
+++ b/lib/hipe/rtl/hipe_rtl_ssapre.erl
@@ -1,8 +1,8 @@
-%% -*- coding: utf-8; erlang-indent-level: 2 -*-
+%% -*- erlang-indent-level: 2 -*-
%%
%% %CopyrightBegin%
%%
-%% Copyright Ericsson AB 2005-2012. All Rights Reserved.
+%% Copyright Ericsson AB 2005-2013. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
diff --git a/lib/hipe/rtl/hipe_tagscheme.erl b/lib/hipe/rtl/hipe_tagscheme.erl
index f1e8d1ef41..c27c682915 100644
--- a/lib/hipe/rtl/hipe_tagscheme.erl
+++ b/lib/hipe/rtl/hipe_tagscheme.erl
@@ -39,14 +39,13 @@
test_tuple/4, test_atom/4, test_bignum/4, test_pos_bignum/4,
test_any_pid/4, test_any_port/4,
test_ref/4, test_fun/4, test_fun2/5, test_matchstate/4,
- test_binary/4, test_bitstr/4, test_list/4,
- test_integer/4, test_number/4, test_constant/4, test_tuple_N/5]).
+ test_binary/4, test_bitstr/4, test_list/4, test_map/4,
+ test_integer/4, test_number/4, test_tuple_N/5]).
-export([realtag_fixnum/2, tag_fixnum/2, realuntag_fixnum/2, untag_fixnum/2]).
-export([test_two_fixnums/3, test_fixnums/4, unsafe_fixnum_add/3,
unsafe_fixnum_sub/3,
fixnum_gt/5, fixnum_lt/5, fixnum_ge/5, fixnum_le/5, fixnum_val/1,
- fixnum_mul/4,
- fixnum_addsub/5, fixnum_andorxor/4, fixnum_not/2,
+ fixnum_mul/4, fixnum_addsub/5, fixnum_andorxor/4, fixnum_not/2,
fixnum_bsr/3, fixnum_bsl/3]).
-export([unsafe_car/2, unsafe_cdr/2,
unsafe_constant_element/3, unsafe_update_element/3, element/6]).
@@ -113,13 +112,15 @@
-define(TAG_HEADER_EXTERNAL_PID, ((16#C bsl ?TAG_PRIMARY_SIZE) bor ?TAG_PRIMARY_HEADER)).
-define(TAG_HEADER_EXTERNAL_PORT,((16#D bsl ?TAG_PRIMARY_SIZE) bor ?TAG_PRIMARY_HEADER)).
-define(TAG_HEADER_EXTERNAL_REF, ((16#E bsl ?TAG_PRIMARY_SIZE) bor ?TAG_PRIMARY_HEADER)).
+-define(TAG_HEADER_MAP, ((16#F bsl ?TAG_PRIMARY_SIZE) bor ?TAG_PRIMARY_HEADER)).
-define(TAG_HEADER_MASK, 16#3F).
-define(HEADER_ARITY_OFFS, 6).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-mk_header(SZ,TAG) -> (SZ bsl ?HEADER_ARITY_OFFS) + TAG.
+mk_header(SZ, TAG) -> (SZ bsl ?HEADER_ARITY_OFFS) + TAG.
+
mk_arityval(SZ) -> mk_header(SZ, ?TAG_HEADER_ARITYVAL).
size_from_header(Sz, Header) ->
@@ -133,9 +134,9 @@ mk_var_header(Header, Size, Tag) ->
mk_fixnum(X) -> (X bsl ?TAG_IMMED1_SIZE) + ?TAG_IMMED1_SMALL.
-define(NIL, ((-1 bsl ?TAG_IMMED2_SIZE) bor ?TAG_IMMED2_NIL)).
-mk_nil() -> ?NIL.
-%% mk_atom(X) -> (X bsl ?TAG_IMMED2_SIZE) + ?TAG_IMMED2_ATOM.
-mk_non_value() -> ?THE_NON_VALUE.
+mk_nil() -> ?NIL.
+%% mk_atom(X) -> (X bsl ?TAG_IMMED2_SIZE) + ?TAG_IMMED2_ATOM.
+mk_non_value() -> ?THE_NON_VALUE.
-spec is_fixnum(integer()) -> boolean().
is_fixnum(N) when is_integer(N) ->
@@ -253,6 +254,15 @@ test_tuple_N(X, N, TrueLab, FalseLab, Pred) ->
hipe_rtl:mk_branch(Tmp, 'eq', hipe_rtl:mk_imm(mk_arityval(N)),
TrueLab, FalseLab, Pred)].
+test_map(X, TrueLab, FalseLab, Pred) ->
+ Tmp = hipe_rtl:mk_new_reg_gcsafe(),
+ HalfTrueLab = hipe_rtl:mk_new_label(),
+ MapMask = ?TAG_HEADER_MASK,
+ [test_is_boxed(X, hipe_rtl:label_name(HalfTrueLab), FalseLab, Pred),
+ HalfTrueLab,
+ get_header(Tmp, X),
+ mask_and_compare(Tmp, MapMask, ?TAG_HEADER_MAP, TrueLab, FalseLab, Pred)].
+
test_ref(X, TrueLab, FalseLab, Pred) ->
Hdr = hipe_rtl:mk_new_reg_gcsafe(),
Tag = hipe_rtl:mk_new_reg_gcsafe(),
@@ -405,17 +415,6 @@ test_number(X, TrueLab, FalseLab, Pred) ->
hipe_rtl:mk_branch(Tmp, 'eq', hipe_rtl:mk_imm(HeaderFlonum),
TrueLab, FalseLab, Pred)].
-%% CONS, NIL, and TUPLE are not constants, everything else is
-test_constant(X, TrueLab, FalseLab, Pred) ->
- Lab1 = hipe_rtl:mk_new_label(),
- Lab2 = hipe_rtl:mk_new_label(),
- Pred1 = 1-Pred,
- [test_cons(X, FalseLab, hipe_rtl:label_name(Lab1), Pred1),
- Lab1,
- test_nil(X, FalseLab, hipe_rtl:label_name(Lab2), Pred1),
- Lab2,
- test_tuple(X, FalseLab, TrueLab, Pred1)].
-
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tag_fixnum(DestVar, SrcReg) ->
diff --git a/lib/hipe/sparc/hipe_sparc_assemble.erl b/lib/hipe/sparc/hipe_sparc_assemble.erl
index b534fe20ec..68a4e1b349 100644
--- a/lib/hipe/sparc/hipe_sparc_assemble.erl
+++ b/lib/hipe/sparc/hipe_sparc_assemble.erl
@@ -45,8 +45,8 @@ assemble(CompiledCode, Closures, Exports, Options) ->
print("Total num bytes=~w\n", [CodeSize], Options),
%%
SC = hipe_pack_constants:slim_constmap(ConstMap),
- DataRelocs = mk_data_relocs(RefsFromConsts, LabelMap),
- SSE = slim_sorted_exportmap(ExportMap,Closures,Exports),
+ DataRelocs = hipe_pack_constants:mk_data_relocs(RefsFromConsts, LabelMap),
+ SSE = hipe_pack_constants:slim_sorted_exportmap(ExportMap,Closures,Exports),
SlimRefs = hipe_pack_constants:slim_refs(AccRefs),
Bin = term_to_binary([{?VERSION_STRING(),?HIPE_SYSTEM_CRC},
ConstAlign, ConstSize,
@@ -222,7 +222,7 @@ do_pseudo_set(I, MFA, ConstMap) ->
%%% end,
%%% {load_address, {Tag,untag_mfa_or_prim(MFAorPrim)}};
{Label,constant} ->
- ConstNo = find_const({MFA,Label}, ConstMap),
+ ConstNo = hipe_pack_constants:find_const({MFA,Label}, ConstMap),
{load_address, {constant,ConstNo}};
{Label,closure} ->
{load_address, {closure,Label}};
@@ -507,37 +507,6 @@ px({pred,Pred}) -> % XXX: use pt/pn throughout entire backend
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-mk_data_relocs(RefsFromConsts, LabelMap) ->
- lists:flatten(mk_data_relocs(RefsFromConsts, LabelMap, [])).
-
-mk_data_relocs([{MFA,Labels} | Rest], LabelMap, Acc) ->
- Map = [case Label of
- {L,Pos} ->
- Offset = find({MFA,L}, LabelMap),
- {Pos,Offset};
- {sorted,Base,OrderedLabels} ->
- {sorted, Base, [begin
- Offset = find({MFA,L}, LabelMap),
- {Order, Offset}
- end
- || {L,Order} <- OrderedLabels]}
- end
- || Label <- Labels],
- %% msg("Map: ~w Map\n",[Map]),
- mk_data_relocs(Rest, LabelMap, [Map,Acc]);
-mk_data_relocs([],_,Acc) -> Acc.
-
-find({_MFA,_L} = MFAL, LabelMap) ->
- gb_trees:get(MFAL, LabelMap).
-
-slim_sorted_exportmap([{Addr,M,F,A}|Rest], Closures, Exports) ->
- IsClosure = lists:member({M,F,A}, Closures),
- IsExported = is_exported(F, A, Exports),
- [Addr,M,F,A,IsClosure,IsExported | slim_sorted_exportmap(Rest, Closures, Exports)];
-slim_sorted_exportmap([],_,_) -> [].
-
-is_exported(F, A, Exports) -> lists:member({F,A}, Exports).
-
%%%
%%% Assembly listing support (pp_asm option).
%%%
@@ -575,14 +544,3 @@ fill_spaces(N) when N > 0 ->
fill_spaces(N-1);
fill_spaces(0) ->
[].
-
-%%%
-%%% Lookup a constant in a ConstMap.
-%%%
-
-find_const({MFA,Label},[{pcm_entry,MFA,Label,ConstNo,_,_,_}|_]) ->
- ConstNo;
-find_const(N,[_|R]) ->
- find_const(N,R);
-find_const(C,[]) ->
- ?EXIT({constant_not_found,C}).
diff --git a/lib/hipe/ssa/hipe_ssa.inc b/lib/hipe/ssa/hipe_ssa.inc
index e766a83c41..2766c10e4f 100644
--- a/lib/hipe/ssa/hipe_ssa.inc
+++ b/lib/hipe/ssa/hipe_ssa.inc
@@ -1,8 +1,8 @@
-%% -*- coding: utf-8; erlang-indent-level: 2 -*-
+%% -*- erlang-indent-level: 2 -*-
%%
%% %CopyrightBegin%
%%
-%% Copyright Ericsson AB 2002-2012. All Rights Reserved.
+%% Copyright Ericsson AB 2002-2013. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
diff --git a/lib/hipe/ssa/hipe_ssa_const_prop.inc b/lib/hipe/ssa/hipe_ssa_const_prop.inc
index 2fce384197..0876fca34a 100644
--- a/lib/hipe/ssa/hipe_ssa_const_prop.inc
+++ b/lib/hipe/ssa/hipe_ssa_const_prop.inc
@@ -3,7 +3,7 @@
%%
%% %CopyrightBegin%
%%
-%% Copyright Ericsson AB 2004-2009. All Rights Reserved.
+%% Copyright Ericsson AB 2004-2014. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
@@ -72,10 +72,10 @@ visit_expressions([Inst | Insts], Environment, FlowWork, SSAWork) ->
%%-----------------------------------------------------------------------------
-record(env, {cfg :: #cfg{},
- executable_flags = gb_sets:empty() :: gb_set(),
- handled_blocks = gb_sets:empty() :: gb_set(),
- lattice_values = gb_trees:empty() :: gb_tree(),
- ssa_edges = gb_trees:empty() :: gb_tree()
+ executable_flags = gb_sets:empty() :: gb_sets:set(),
+ handled_blocks = gb_sets:empty() :: gb_sets:set(),
+ lattice_values = gb_trees:empty() :: gb_trees:tree(),
+ ssa_edges = gb_trees:empty() :: gb_trees:tree()
}).
create_env(CFG) ->
diff --git a/lib/hipe/test/Makefile b/lib/hipe/test/Makefile
new file mode 100644
index 0000000000..009f503abb
--- /dev/null
+++ b/lib/hipe/test/Makefile
@@ -0,0 +1,80 @@
+include $(ERL_TOP)/make/target.mk
+include $(ERL_TOP)/make/$(TARGET)/otp.mk
+
+# ----------------------------------------------------
+# Target Specs
+# ----------------------------------------------------
+
+MODULES= \
+ hipe_SUITE
+
+# .erl files for these modules are automatically generated
+GEN_MODULES= \
+ bs_SUITE \
+ maps_SUITE
+
+ERL_FILES= $(MODULES:%=%.erl)
+
+TARGET_FILES= $(MODULES:%=$(EBIN)/%.$(EMULATOR))
+INSTALL_PROGS= $(TARGET_FILES)
+
+# ----------------------------------------------------
+# Files
+# ----------------------------------------------------
+EMAKEFILE = Emakefile
+AUXILIARY_FILES = hipe.spec hipe_testsuite_driver.erl $(EMAKEFILE)
+
+# ----------------------------------------------------
+# Release directory specification
+# ----------------------------------------------------
+RELSYSDIR = $(RELEASE_PATH)/hipe_test
+
+# ----------------------------------------------------
+# FLAGS
+# ----------------------------------------------------
+
+ERL_MAKE_FLAGS +=
+ERL_COMPILE_FLAGS += -I$(ERL_TOP)/lib/test_server/include
+
+EBIN = .
+
+# ----------------------------------------------------
+# Targets
+# ----------------------------------------------------
+
+make_emakefile:
+ $(ERL_TOP)/make/make_emakefile $(ERL_COMPILE_FLAGS) -o$(EBIN) $(MODULES) \
+ > $(EMAKEFILE)
+ $(ERL_TOP)/make/make_emakefile $(ERL_COMPILE_FLAGS) -o$(EBIN) $(GEN_MODULES) \
+ >> $(EMAKEFILE)
+ $(ERL_TOP)/make/make_emakefile $(ERL_COMPILE_FLAGS) -o$(EBIN) '*_SUITE_make' \
+ >> $(EMAKEFILE)
+
+tests debug opt: make_emakefile
+ erl $(ERL_MAKE_FLAGS) -make
+
+clean:
+ rm -f $(EMAKEFILE)
+ rm -f $(TARGET_FILES) $(GEN_FILES)
+ rm -f core
+
+docs:
+
+# ----------------------------------------------------
+# Release Target
+# ----------------------------------------------------
+include $(ERL_TOP)/make/otp_release_targets.mk
+
+release_spec: opt
+
+release_docs_spec:
+
+release_tests_spec: make_emakefile
+ $(INSTALL_DIR) "$(RELSYSDIR)"
+ chmod -R u+w "$(RELSYSDIR)"
+ $(INSTALL_DATA) $(AUXILIARY_FILES) "$(RELSYSDIR)"
+ $(INSTALL_DATA) $(ERL_FILES) "$(RELSYSDIR)"
+ @tar cf - *_SUITE_data | (cd "$(RELSYSDIR)"; tar xf -)
+ cd "$(RELSYSDIR)";\
+ erlc hipe_testsuite_driver.erl;\
+ erl -noshell -run hipe_testsuite_driver create_all_suites -s erlang halt
diff --git a/lib/hipe/test/bs_SUITE_data/bs_add.erl b/lib/hipe/test/bs_SUITE_data/bs_add.erl
new file mode 100644
index 0000000000..af5a3b2f23
--- /dev/null
+++ b/lib/hipe/test/bs_SUITE_data/bs_add.erl
@@ -0,0 +1,18 @@
+%% -*- erlang-indent-level: 2 -*-
+%%-------------------------------------------------------------------------
+%% The guard in f/3 revealed a problem in the translation of the 'bs_add'
+%% BEAM instruction to Icode. The fail label was not properly translated.
+%% Fixed 3/2/2011.
+%%-------------------------------------------------------------------------
+-module(bs_add).
+
+-export([test/0]).
+
+test() ->
+ 42 = f(<<12345:16>>, 4711, <<42>>),
+ ok.
+
+f(Bin, A, B) when <<A:9, B:7/binary>> == Bin ->
+ gazonk;
+f(Bin, _, _) when is_binary(Bin) ->
+ 42.
diff --git a/lib/hipe/test/bs_SUITE_data/bs_bincomp.erl b/lib/hipe/test/bs_SUITE_data/bs_bincomp.erl
new file mode 100644
index 0000000000..082b83bab9
--- /dev/null
+++ b/lib/hipe/test/bs_SUITE_data/bs_bincomp.erl
@@ -0,0 +1,79 @@
+%%% -*- erlang-indent-level: 2 -*-
+%%%-------------------------------------------------------------------
+%%% File : bs_bincomp.erl
+%%% Author : Per Gustafsson <[email protected]>
+%%% Purpose : Test bit comprehensions
+%%% Created : 13 Sep 2006
+%%%-------------------------------------------------------------------
+-module(bs_bincomp).
+
+-export([test/0]).
+
+test() ->
+ ok = byte_aligned(),
+ ok = bit_aligned(),
+ ok = extended_byte_aligned(),
+ ok = extended_bit_aligned(),
+ ok = mixed(),
+ ok.
+
+byte_aligned() ->
+ <<"abcdefg">> = << <<(X+32)>> || <<X>> <= <<"ABCDEFG">> >>,
+ <<1:32/little,2:32/little,3:32/little,4:32/little>> =
+ << <<X:32/little>> || <<X:32>> <= <<1:32,2:32,3:32,4:32>> >>,
+ <<1:32/little,2:32/little,3:32/little,4:32/little>> =
+ << <<X:32/little>> || <<X:16>> <= <<1:16,2:16,3:16,4:16>> >>,
+ ok.
+
+bit_aligned() ->
+ <<$a:7,$b:7,$c:7,$d:7,$e:7,$f:7,$g:7>> =
+ << <<(X+32):7>> || <<X>> <= <<"ABCDEFG">> >>,
+ <<"ABCDEFG">> =
+ << <<(X-32)>> || <<X:7>> <= <<$a:7,$b:7,$c:7,$d:7,$e:7,$f:7,$g:7>> >>,
+ <<1:31/little,2:31/little,3:31/little,4:31/little>> =
+ << <<X:31/little>> || <<X:31>> <= <<1:31,2:31,3:31,4:31>> >>,
+ <<1:31/little,2:31/little,3:31/little,4:31/little>> =
+ << <<X:31/little>> || <<X:15>> <= <<1:15,2:15,3:15,4:15>> >>,
+ ok.
+
+extended_byte_aligned() ->
+ <<"abcdefg">> = << <<(X+32)>> || X <- "ABCDEFG" >>,
+ "abcdefg" = [(X+32) || <<X>> <= <<"ABCDEFG">>],
+ <<1:32/little,2:32/little,3:32/little,4:32/little>> =
+ << <<X:32/little>> || X <- [1,2,3,4] >>,
+ [256,512,768,1024] =
+ [X || <<X:16/little>> <= <<1:16,2:16,3:16,4:16>>],
+ ok.
+
+extended_bit_aligned() ->
+ <<$a:7,$b:7,$c:7,$d:7,$e:7,$f:7,$g:7>> =
+ << <<(X+32):7>> || X <- "ABCDEFG" >>,
+ "ABCDEFG" = [(X-32) || <<X:7>> <= <<$a:7,$b:7,$c:7,$d:7,$e:7,$f:7,$g:7>>],
+ <<1:31/little,2:31/little,3:31/little,4:31/little>> =
+ << <<X:31/little>> || X <- [1,2,3,4] >>,
+ [256,512,768,1024] =
+ [X || <<X:15/little>> <= <<1:15,2:15,3:15,4:15>>],
+ ok.
+
+mixed() ->
+ <<2,3,3,4,4,5,5,6>> =
+ << <<(X+Y)>> || <<X>> <= <<1,2,3,4>>, <<Y>> <= <<1,2>> >>,
+ <<2,3,3,4,4,5,5,6>> =
+ << <<(X+Y)>> || <<X>> <= <<1,2,3,4>>, Y <- [1,2] >>,
+ <<2,3,3,4,4,5,5,6>> =
+ << <<(X+Y)>> || X <- [1,2,3,4], Y <- [1,2] >>,
+ [2,3,3,4,4,5,5,6] =
+ [(X+Y) || <<X>> <= <<1,2,3,4>>, <<Y>> <= <<1,2>>],
+ [2,3,3,4,4,5,5,6] =
+ [(X+Y) || <<X>> <= <<1,2,3,4>>, Y <- [1,2]],
+ <<2:3,3:3,3:3,4:3,4:3,5:3,5:3,6:3>> =
+ << <<(X+Y):3>> || <<X:3>> <= <<1:3,2:3,3:3,4:3>>, <<Y:3>> <= <<1:3,2:3>> >>,
+ <<2:3,3:3,3:3,4:3,4:3,5:3,5:3,6:3>> =
+ << <<(X+Y):3>> || <<X:3>> <= <<1:3,2:3,3:3,4:3>>, Y <- [1,2] >>,
+ <<2:3,3:3,3:3,4:3,4:3,5:3,5:3,6:3>> =
+ << <<(X+Y):3>> || X <- [1,2,3,4], Y <- [1,2] >>,
+ [2,3,3,4,4,5,5,6] =
+ [(X+Y) || <<X:3>> <= <<1:3,2:3,3:3,4:3>>, <<Y:3>> <= <<1:3,2:3>>],
+ [2,3,3,4,4,5,5,6] =
+ [(X+Y) || <<X:3>> <= <<1:3,2:3,3:3,4:3>>, Y <- [1,2]],
+ ok.
diff --git a/lib/hipe/test/bs_SUITE_data/bs_bits.erl b/lib/hipe/test/bs_SUITE_data/bs_bits.erl
new file mode 100644
index 0000000000..ef9a6bb137
--- /dev/null
+++ b/lib/hipe/test/bs_SUITE_data/bs_bits.erl
@@ -0,0 +1,150 @@
+%%% -*- erlang-indent-level: 2 -*-
+%%%-------------------------------------------------------------------
+%%% File : bs_bits.erl
+%%% Author : Per Gustafsson <[email protected]>
+%%% Purpose : Tests for bit stream operations including matching,
+%%% construction, binary_to_list and list_to_binary
+%%% Created : 6 Sep 2006
+%%%-------------------------------------------------------------------
+-module(bs_bits).
+
+-export([test/0]).
+
+test() ->
+ <<1:100>> = <<1:100>>,
+ ok = match(7),
+ ok = match(9),
+ ok = match1(15),
+ ok = match1(31),
+ ok = horrid_match(),
+ ok = test_bitstr(),
+ ok = test_is_bitstr(<<1:1>>,<<8>>),
+ ok = test_is_binary(<<1:1>>,<<8>>),
+ ok = test_bitsize(),
+ ok = asymmetric_tests(),
+ ok = big_asymmetric_tests(),
+ ok = bitstr_to_and_from_list(),
+ ok = big_bitstr_to_and_from_list(),
+ ok = send_and_receive(),
+ ok = send_and_receive_alot(),
+ ok.
+
+match(N) ->
+ <<0:N>> = <<0:N>>,
+ ok.
+
+match1(N) ->
+ <<42:N/little>> = <<42:N/little>>,
+ ok.
+
+test_is_bitstr(Bitstr, Binary) ->
+ true = is_bitstring(Bitstr),
+ true = is_bitstring(Binary),
+ ok = if is_bitstring(Bitstr) -> ok end,
+ ok = if is_bitstring(Binary) -> ok end.
+
+test_is_binary(Bitstr, Binary) ->
+ false = is_binary(Bitstr),
+ true = is_binary(Binary),
+ ok = if is_binary(Bitstr) -> not_ok; true -> ok end,
+ ok = if is_binary(Binary) -> ok end.
+
+test_bitsize() ->
+ 101 = erlang:bit_size(<<1:101>>),
+ 1001 = erlang:bit_size(<<1:1001>>),
+ 80 = erlang:bit_size(<<1:80>>),
+ 800 = erlang:bit_size(<<1:800>>),
+ Bin = <<0:16#1000000>>,
+ BigBin = list_to_bitstring([Bin||_ <- lists:seq(1,16#10)]++[<<1:1>>]),
+ 16#10000001 = bit_size(BigBin),
+ %% Only run these on computers with lots of memory
+ %% HugeBin = list_to_bitstring([BigBin||_ <- lists:seq(1,16#10)]++[<<1:1>>]),
+ %% 16#100000011 = bit_size(HugeBin),
+ 0 = erlang:bit_size(<<>>),
+ ok.
+
+horrid_match() ->
+ <<1:4,B:24/bitstring>> = <<1:4,42:24/little>>,
+ <<42:24/little>> = B,
+ ok.
+
+test_bitstr() ->
+ <<1:7,B/bitstring>> = <<1:7,<<1:1,6>>/bitstring>>,
+ <<1:1,6>> = B,
+ B = <<1:1,6>>,
+ ok.
+
+asymmetric_tests() ->
+ <<1:12>> = <<0,1:4>>,
+ <<0,1:4>> = <<1:12>>,
+ <<1:1,X/bitstring>> = <<128,255,0,0:2>>,
+ <<1,254,0,0:1>> = X,
+ X = <<1,254,0,0:1>>,
+ <<1:1,X1:25/bitstring>> = <<128,255,0,0:2>>,
+ <<1,254,0,0:1>> = X1,
+ X1 = <<1,254,0,0:1>>,
+ ok.
+
+big_asymmetric_tests() ->
+ <<1:875,1:12>> = <<1:875,0,1:4>>,
+ <<1:875,0,1:4>> = <<1:875,1:12>>,
+ <<1:1,X/bitstring>> = <<128,255,0,0:2,1:875>>,
+ <<1,254,0,0:1,1:875>> = X,
+ X = <<1,254,0,0:1,1:875>>,
+ <<1:1,X1:900/bitstring>> = <<128,255,0,0:2,1:875>>,
+ <<1,254,0,0:1,1:875>> = X1,
+ X1 = <<1,254,0,0:1,1:875>>,
+ ok.
+
+bitstr_to_and_from_list() ->
+ <<1:7>> = list_to_bitstring(bitstring_to_list(<<1:7>>)),
+ <<1,2,3,4,1:1>> = list_to_bitstring(bitstring_to_list(<<1,2,3,4,1:1>>)),
+ [1,2,3,4,<<1:1>>] = bitstring_to_list(<<1,2,3,4,1:1>>),
+ <<1:1,1,2,3,4>> = list_to_bitstring([<<1:1>>,1,2,3,4]),
+ [128,129,1,130,<<0:1>>] = bitstring_to_list(<<1:1,1,2,3,4>>),
+ ok.
+
+big_bitstr_to_and_from_list() ->
+ <<1:800,2,3,4,1:1>> = list_to_bitstring(bitstring_to_list(<<1:800,2,3,4,1:1>>)),
+ [1,2,3,4|_Rest1] = bitstring_to_list(<<1,2,3,4,1:800,1:1>>),
+ <<1:801,1,2,3,4>> = list_to_bitstring([<<1:801>>,1,2,3,4]),
+ ok.
+
+send_and_receive() ->
+ Bin = <<1,2:7>>,
+ Pid = spawn(fun() -> receiver(Bin) end),
+ Pid ! {self(),<<1:7,8:5,Bin/bitstring>>},
+ receive
+ ok ->
+ ok
+ end.
+
+receiver(Bin) ->
+ receive
+ {Pid,<<1:7,8:5,Bin/bitstring>>} ->
+ Pid ! ok
+ end.
+
+send_and_receive_alot() ->
+ Bin = <<1:1000001>>,
+ Pid = spawn(fun() -> receiver_alot(Bin) end),
+ send_alot(100,Bin,Pid).
+
+send_alot(N,Bin,Pid) when N > 0 ->
+ Pid ! {self(),<<1:7,8:5,Bin/bitstring>>},
+ receive
+ ok ->
+ ok
+ end,
+ send_alot(N-1,Bin,Pid);
+send_alot(0,_Bin,Pid) ->
+ Pid ! no_more,
+ ok.
+
+receiver_alot(Bin) ->
+ receive
+ {Pid,<<1:7,8:5,Bin/bitstring>>} ->
+ Pid ! ok;
+ no_more -> ok
+ end,
+ receiver_alot(Bin).
diff --git a/lib/hipe/test/bs_SUITE_data/bs_bitsize.erl b/lib/hipe/test/bs_SUITE_data/bs_bitsize.erl
new file mode 100644
index 0000000000..c0774e7279
--- /dev/null
+++ b/lib/hipe/test/bs_SUITE_data/bs_bitsize.erl
@@ -0,0 +1,23 @@
+%% -*- erlang-indent-level: 2 -*-
+%%-------------------------------------------------------------------
+-module(bs_bitsize).
+
+-export([test/0]).
+
+test() ->
+ true = bitsize_in_body(<<1:42>>),
+ true = bitsize_in_guard(<<1:7>>),
+ 8 = constant_binary(42),
+ ok.
+
+bitsize_in_body(Bin) ->
+ 42 =:= erlang:bit_size(Bin).
+
+bitsize_in_guard(Bin) when erlang:bit_size(Bin) rem 7 =:= 0 ->
+ true;
+bitsize_in_guard(Bin) when is_bitstring(Bin) ->
+ false.
+
+%% Tests that binary constants can properly be treated in Icode
+constant_binary(N) when N > 0 ->
+ bit_size(<<42>>).
diff --git a/lib/hipe/test/bs_SUITE_data/bs_bugs_R08.erl b/lib/hipe/test/bs_SUITE_data/bs_bugs_R08.erl
new file mode 100644
index 0000000000..7b62a17cfb
--- /dev/null
+++ b/lib/hipe/test/bs_SUITE_data/bs_bugs_R08.erl
@@ -0,0 +1,32 @@
+%% -*- erlang-indent-level: 2 -*-
+%%-------------------------------------------------------------------
+%% When executing this in R8 (and compiled with R8) the result was
+%% {ok,[148,129,0,0]} but should be {ok,[145,148,113,129,0,0,0,0]}
+%% Thanks to Kenneth Lundin for sending this to us.
+%%-------------------------------------------------------------------
+
+-module(bs_bugs_R08).
+
+-export([test/0]).
+
+test() ->
+ List = [145,148,113,129,0,0,0,0],
+ {ok, List} = msisdn_internal_storage(<<145,148,113,129,0,0,0,0>>, []),
+ ok.
+
+%% msisdn_internal_storage/3
+%% Convert MSISDN binary to internal datatype (TBCD-octet list)
+
+msisdn_internal_storage(<<>>, MSISDN) ->
+ {ok, lists:reverse(MSISDN)};
+msisdn_internal_storage(<<2#11111111:8,_Rest/binary>>, MSISDN) ->
+ {ok, lists:reverse(MSISDN)};
+msisdn_internal_storage(<<2#1111:4,DigitN:4,_Rest/binary>>, MSISDN) when
+ DigitN < 10 ->
+ {ok, lists:reverse([(DigitN bor 2#11110000)|MSISDN])};
+msisdn_internal_storage(<<DigitNplus1:4,DigitN:4,Rest/binary>>, MSISDN) when
+ DigitNplus1 < 10, DigitN < 10 ->
+ NewMSISDN = [((DigitNplus1 bsl 4) bor DigitN)|MSISDN],
+ msisdn_internal_storage(Rest, NewMSISDN);
+msisdn_internal_storage(_Rest, _MSISDN) ->
+ {fault}. %% Mandatory IE incorrect
diff --git a/lib/hipe/test/bs_SUITE_data/bs_bugs_R09.erl b/lib/hipe/test/bs_SUITE_data/bs_bugs_R09.erl
new file mode 100644
index 0000000000..670f2a08bb
--- /dev/null
+++ b/lib/hipe/test/bs_SUITE_data/bs_bugs_R09.erl
@@ -0,0 +1,35 @@
+%% -*- erlang-indent-level: 2 -*-
+%%--------------------------------------------------------------------
+%% Date: Mon, 7 Jun 2004 13:07:39 +0300
+%% From: Einar Karttunen
+%% To: Erlang ML <[email protected]>
+%% Subject: Apparent binary matching bug with native compilation
+%%
+%% It seems that there is a problem with binary matching when
+%% compiling native code. A length prefixed field matches one
+%% byte too short in the native case.
+%%
+%% The test module works when compiled with no options, but
+%% crashes with case_clause when compiled with [native].
+%% This has been confirmed with R9C-0 and hipe snapshot 5/4/2004.
+%%--------------------------------------------------------------------
+
+-module(bs_bugs_R09).
+
+-export([test/0]).
+
+test() ->
+ ["rei",".",[]] = pp(<<3,$r,$e,$i,0>>),
+ ok.
+
+pp(Bin) ->
+ %% io:format("PP with ~p~n", [Bin]),
+ case Bin of
+ <<>> ->
+ ["."];
+ <<_:2, Len:6, Part:Len/binary>> ->
+ [binary_to_list(Part)];
+ <<_:2, Len:6, Part:Len/binary, Rest/binary>> ->
+ %% io:format("Len ~p Part ~p Rest ~p~n", [Len,Part,Rest]),
+ [binary_to_list(Part), "." | pp(Rest)]
+ end.
diff --git a/lib/hipe/test/bs_SUITE_data/bs_bugs_R12.erl b/lib/hipe/test/bs_SUITE_data/bs_bugs_R12.erl
new file mode 100644
index 0000000000..43ee9eb85b
--- /dev/null
+++ b/lib/hipe/test/bs_SUITE_data/bs_bugs_R12.erl
@@ -0,0 +1,133 @@
+%% -*- erlang-indent-level: 2 -*-
+%%--------------------------------------------------------------------
+%% Contains three cases of bugs that were reported for R12B
+%%--------------------------------------------------------------------
+-module(bs_bugs_R12).
+
+-export([test/0]).
+
+test() ->
+ ok = test_beam_bug(),
+ ok = test_v3_codegen(),
+ ok = test_hipe_bug(),
+ ok.
+
+%%--------------------------------
+%% First test case: a bug in BEAM
+%%--------------------------------
+test_beam_bug() ->
+ lists:foreach(fun (_) -> ok = run(100) end, [1,2,3,4]).
+
+%% For testing - runs scanner N number of times with same input
+run(N) ->
+ lists:foreach(fun(_) -> scan(<<"region:whatever">>, []) end, lists:seq(1, N)).
+
+scan(<<>>, TokAcc) ->
+ lists:reverse(['$thats_all_folks$' | TokAcc]);
+scan(<<D, Z, Rest/binary>>, TokAcc)
+ when (D =:= $D orelse D =:= $d) and
+ ((Z =:= $\s) or (Z =:= $() or (Z =:= $))) ->
+ scan(<<Z, Rest/binary>>, ['AND' | TokAcc]);
+scan(<<D>>, TokAcc) when (D =:= $D) or (D =:= $d) ->
+ scan(<<>>, ['AND' | TokAcc]);
+scan(<<N, Z, Rest/binary>>, TokAcc)
+ when (N =:= $N orelse N =:= $n) and
+ ((Z =:= $\s) or (Z =:= $() or (Z =:= $))) ->
+ scan(<<Z, Rest/binary>>, ['NOT' | TokAcc]);
+scan(<<C, Rest/binary>>, TokAcc) when (C >= $A) and (C =< $Z);
+ (C >= $a) and (C =< $z);
+ (C >= $0) and (C =< $9) ->
+ case Rest of
+ <<$:, R/binary>> ->
+ scan(R, [{'FIELD', C} | TokAcc]);
+ _ ->
+ scan(Rest, [{'KEYWORD', C} | TokAcc])
+ end.
+
+%%---------------------------------------------------
+%% Second test case: an internal error in v3_codegen
+%% Reported by Mateusz Berezecki on 19/1/2008
+%%---------------------------------------------------
+-define(S, {42, 4242, 4711}).
+-define(R, <<90,164,116>>).
+
+test_v3_codegen() ->
+ _ = random:seed(?S),
+ B0 = gen_bit(120, <<>>),
+ B1 = set_bit(B0, 5),
+ B2 = clr_bit(B1, 5),
+ ?R = set_bit(B2, 5),
+ ok.
+
+gen_bit(0, Acc) -> Acc;
+gen_bit(N, Acc) when is_integer(N), N > 0 ->
+ gen_bit(N-1, <<Acc/bits, (random:uniform(2)-1):1>>).
+
+%% sets bit K in the Bitmap
+set_bit(<<_Start:32/unsigned-little-integer, Bitmap/bits>>, K)
+ when is_integer(K), 0 < K, K =< bit_size(Bitmap) ->
+ Before = K-1,
+ After = bit_size(Bitmap) - K,
+ <<BeforeBits:Before/bits, _:1, AfterBits:After/bits>> = Bitmap,
+ <<BeforeBits/bits, 1:1, AfterBits/bits>>.
+
+%% clears bit K in the Bitmap
+clr_bit(<<_Start:32/unsigned-little-integer, Bitmap/bits>>, K)
+ when is_integer(K), 0 < K, K =< bit_size(Bitmap) ->
+ Before = K-1,
+ After = bit_size(Bitmap) - K,
+ <<BeforeBits:Before/bits, _:1, AfterBits:After/bits>> = Bitmap,
+ <<BeforeBits/bits, 0:1, AfterBits/bits>>.
+
+%%--------------------------------------------------------------------
+%% Third test case: a bug in HiPE
+%% Reported by Steve Vinoski on 1/3/2008
+%%
+%% Below find the results of compiling and running the example code at
+%% the bottom of this message. Using "c" to compile gives the right
+%% answer; using "hipe:c" gives the wrong answer. This is with R12B-1.
+%%
+%% Within the code, on the second instance of function check/2 you'll
+%% find a commented-out guard. If you uncomment the guard, then the
+%% code works correctly with both "c" and "hipe:c".
+%%---------------------------------------------------------------------
+
+test_hipe_bug() ->
+ String = "2006/10/02/Linux-Journal",
+ Binary = list_to_binary(String),
+ StringToMatch = "200x/" ++ String ++ " ",
+ BinaryToMatch = list_to_binary(StringToMatch),
+ {ok, Binary} = match(BinaryToMatch),
+ ok.
+
+match(<<>>) ->
+ nomatch;
+match(Bin) ->
+ <<Front:16/binary, Tail/binary>> = Bin,
+ case Front of
+ <<_:3/binary,"x/",Y:4/binary,$/,M:2/binary,$/,D:2/binary,$/>> ->
+ case check(Tail) of
+ {ok, Match} ->
+ {ok, <<Y/binary,$/,M/binary,$/,D/binary,$/,Match/binary>>};
+ {nomatch, Skip} ->
+ {skip, Skip+size(Front)};
+ _ ->
+ wrong_answer
+ end;
+ _ ->
+ nomatch
+ end.
+
+check(Bin) ->
+ check(Bin, 0).
+check(<<$ , _/binary>>, 0) ->
+ {nomatch, 0};
+check(Bin, Len) -> %when Len < size(Bin) ->
+ case Bin of
+ <<Front:Len/binary, $ , _/binary>> ->
+ {ok, Front};
+ <<_:Len/binary, $., _/binary>> ->
+ {nomatch, Len};
+ _ ->
+ check(Bin, Len+1)
+ end.
diff --git a/lib/hipe/test/bs_SUITE_data/bs_build.erl b/lib/hipe/test/bs_SUITE_data/bs_build.erl
new file mode 100644
index 0000000000..256cea9403
--- /dev/null
+++ b/lib/hipe/test/bs_SUITE_data/bs_build.erl
@@ -0,0 +1,41 @@
+%%% -*- erlang-indent-level: 2 -*-
+%%%-------------------------------------------------------------------
+%%% File : bs_build.erl
+%%% Author : Per Gustafsson <[email protected]>
+%%% Purpose :
+%%%
+%%% Created : 12 Sep 2007
+%%%-------------------------------------------------------------------
+-module(bs_build).
+
+-export([test/0]).
+
+test() ->
+ <<0,1,2,3,4,5,6>> = Bin = << <<X>> || X <- lists:seq(0, 6)>>,
+ test(Bin).
+
+test(Bin) ->
+ <<0,1,2,3,4,5,6,0,1,2,3,4,5,6>> = RealBin = multiply(Bin, 2),
+ <<6,5,4,3,2,1,0,6,5,4,3,2,1,0>> = reverse(RealBin),
+ RealBin = copy(RealBin),
+ RealBin = bc(RealBin),
+ ok.
+
+multiply(Bin, 1) ->
+ Bin;
+multiply(Bin, N) when N > 0 ->
+ <<(multiply(Bin, N-1))/binary, Bin/binary>>.
+
+bc(Bin) ->
+ << <<X>> || <<X>> <= Bin >>.
+
+reverse(<<X, Rest/binary>>) ->
+ <<(reverse(Rest))/binary, X>>;
+reverse(<<>>) -> <<>>.
+
+copy(Bin) ->
+ copy(Bin, <<>>).
+
+copy(<<X, Rest/binary>>, Bin) ->
+ copy(Rest, <<Bin/binary, X>>);
+copy(<<>>, Bin) -> Bin.
diff --git a/lib/hipe/test/bs_SUITE_data/bs_catch_bug.erl b/lib/hipe/test/bs_SUITE_data/bs_catch_bug.erl
new file mode 100644
index 0000000000..6125f8f87f
--- /dev/null
+++ b/lib/hipe/test/bs_SUITE_data/bs_catch_bug.erl
@@ -0,0 +1,25 @@
+%%% -*- erlang-indent-level: 2 -*-
+%%%-------------------------------------------------------------------
+%%% File : bs_catch_bug.erl
+%%% Author : Per Gustafsson <[email protected]>
+%%% Purpose : Tests a catch-related bug which might destroy properties
+%%% of ICode CFGs which are assumed by the subsequent ICode
+%%% binary pass.
+%%% Created : 22 Jan 2004
+%%% -------------------------------------------------------------------
+-module(bs_catch_bug).
+
+-export([test/0]).
+
+test() ->
+ test(foo, <<>>).
+
+%% Introduced auxiliary test/2 function so that constant propagation
+%% does not destroy the properties of the test. - Kostis 26/1/2004
+test(X, Bin) ->
+ catch (<<_/binary>> = X),
+ X = case Bin of
+ <<42,_/binary>> -> weird_bs_match;
+ _ -> X
+ end,
+ ok.
diff --git a/lib/hipe/test/bs_SUITE_data/bs_checksum.erl b/lib/hipe/test/bs_SUITE_data/bs_checksum.erl
new file mode 100644
index 0000000000..ca4f254f12
--- /dev/null
+++ b/lib/hipe/test/bs_SUITE_data/bs_checksum.erl
@@ -0,0 +1,35 @@
+%% -*- erlang-indent-level: 2 -*-
+%%--------------------------------------------------------------------
+%% Code from Zoltan Toth that crashed the HiPE compiler (in R11B-3).
+%% The problem was that the binary matching produces a pretty large
+%% integer and we tried to find the range for this integer in a bad way.
+%% Fixed on the same day -- 6th March 2007.
+%%--------------------------------------------------------------------
+
+-module(bs_checksum).
+
+-export([test/0]).
+
+test() ->
+ "3389DAE361AF79B04C9C8E7057F60CC6" = checksum(<<42>>),
+ ok.
+
+checksum(Bin) ->
+ Context = erlang:md5_init(),
+ checksum(Context, Bin).
+
+checksum(Context, <<>>) ->
+ bin_to_hex(erlang:md5_final(Context));
+checksum(Context, <<Bin:20480/binary,Rest/binary>>) ->
+ checksum(erlang:md5_update(Context, Bin), Rest);
+checksum(Context,Bin) ->
+ checksum(erlang:md5_update(Context, Bin), <<>>).
+
+bin_to_hex(Bin) ->
+ lists:flatten([byte_to_hex(X) || X <- binary_to_list(Bin)]).
+
+byte_to_hex(Byte) ->
+ [int_to_hex(Byte div 16), int_to_hex(Byte rem 16)].
+
+int_to_hex(Int) when Int < 10 -> $0 + Int;
+int_to_hex(Int) when Int > 9 -> $A + Int - 10.
diff --git a/lib/hipe/test/bs_SUITE_data/bs_construct.erl b/lib/hipe/test/bs_SUITE_data/bs_construct.erl
new file mode 100644
index 0000000000..9cc9ac848c
--- /dev/null
+++ b/lib/hipe/test/bs_SUITE_data/bs_construct.erl
@@ -0,0 +1,128 @@
+%% -*- erlang-indent-level: 2 -*-
+%%--------------------------------------------------------------------
+%% Tests that basic cases of binary construction work
+%%--------------------------------------------------------------------
+-module(bs_construct).
+
+-export([test/0]).
+
+test() ->
+ <<42>> = sz(8),
+ <<42:8/little>> = sz_little(8),
+ <<55>> = take_five(1, 3, 1, 7, 4),
+ ok = bs5(),
+ 16#10000008 = bit_size(large_bin(1, 2, 3, 4)),
+ ok = bad_ones(),
+ ok.
+
+%%--------------------------------------------------------------------
+%% Taken from a bug report submitted by Dan Wallin (24 Oct 2003), the
+%% following cases test construction of binaries whose segments have
+%% sizes that are statically unknown.
+
+sz(S) ->
+ <<42:S>>.
+
+sz_little(S) ->
+ <<42:S/little>>.
+
+take_five(A, Head, FB, C, Tail) ->
+ <<A:Head, FB:1, C:Tail>>.
+
+%%--------------------------------------------------------------------
+
+bs5() ->
+ Const = mk_constant(),
+ Pairs = mk_pairs(),
+ true = are_same(Const, Pairs),
+ true = lists:all(fun ({B, L}) -> binary_to_list(B) =:= L end, Pairs),
+ ok.
+
+are_same(C, L) ->
+ C =:= L.
+
+mk_constant() ->
+ [{<<213>>,[213]},
+ {<<56>>,[56]},
+ {<<1,2>>,[1,2]},
+ {<<71>>,[71]},
+ {<<8,1>>,[8,1]},
+ {<<3,9>>,[3,9]},
+ {<<9,3>>,[9,3]},
+ {<<0,0,0,0>>,[0,0,0,0]},
+ {<<62,0,0,0>>,[62,0,0,0]},
+ {<<0,0,0,62>>,[0,0,0,62]},
+ {<<138,99,0,147>>,[138,99,0,147]},
+ {<<138,99,0,148>>,[138,99,0,148]},
+ {<<147,0,99,138>>,[147,0,99,138]},
+ {<<255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255>>,
+ [255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255]},
+ {<<13>>,[13]},
+ {<<0,4,0,5>>,[0,4,0,5]},
+ {<<129>>,[129]},
+ {<<129>>,[129]},
+ {<<1,2>>,[1,2]},
+ {<<1>>,[1]},
+ {<<4,3,1>>,[4,3,1]},
+ {<<47>>,[47]},
+ {<<>>,[]},
+ {<<97,112,97>>,[97,112,97]},
+ {<<46,110,142,77,45,204,233>>,[46,110,142,77,45,204,233]},
+ {<<>>,[]}].
+
+mk_pairs() ->
+ L4 = [138,99,0,147],
+ [{<<-43>>,[256-43]},
+ {<<56>>,[56]},
+ {<<1,2>>,[1,2]},
+ {<<4:4,7:4>>,[4*16+7]},
+ {<<1:5,1:11>>,[1*8,1]},
+ {<<777:16/big>>,[3,9]},
+ {<<777:16/little>>,[9,3]},
+ {<<0.0:32/float>>,[0,0,0,0]},
+ {<<0.125:32/float>>,[62,0,0,0]},
+ {<<0.125:32/little-float>>,[0,0,0,62]},
+ {<<57285702734876389752897683:32>>,L4},
+ {<<57285702734876389752897684:32>>,[138,99,0,148]},
+ {<<57285702734876389752897683:32/little>>,lists:reverse(L4)},
+ {<<-1:17/unit:8>>,lists:duplicate(17,255)},
+ {<<13>>,[13]},
+ {<<4:8/unit:2,5:2/unit:8>>,[0,4,0,5]},
+ {<<1:1,0:6,1:1>>,[129]},
+ {<<1:1/little,0:6/little,1:1/little>>,[129]},
+ {<<<<1,2>>/binary>>,[1,2]},
+ {<<<<1,2>>:1/binary>>,[1]},
+ {<<4,3,<<1,2>>:1/binary>>,[4,3,1]},
+ {<<(256*45+47)>>,[47]},
+ {<<57:0>>,[]},
+ {<<"apa">>,"apa"},
+ {<<1:3,"string",9:5>>,[46,110,142,77,45,204,233]},
+ {<<>>,[]}].
+
+%%--------------------------------------------------------------------
+%% Constructs a big enough binary to have a bit size that needs a
+%% bignum on 32-bit architectures
+
+large_bin(X1, X2, X3, X4) ->
+ Sz = 16#4000000,
+ <<1, <<X1:Sz, X2:Sz, X3:Sz, X4:Sz>>/bits>>.
+
+%%--------------------------------------------------------------------
+%% Test construction of "bad" binaries
+
+-define(FAIL(Expr), {'EXIT', {badarg, _}} = (catch Expr)).
+
+bad_ones() ->
+ PI = math:pi(),
+ ?FAIL(<<PI>>),
+ Bin12 = <<1,2>>,
+ ?FAIL(<<Bin12>>),
+ E = 2.71,
+ ?FAIL(<<E/binary>>),
+ Int = 24334,
+ ?FAIL(<<Int/binary>>),
+ BigInt = 24334344294788947129487129487219847,
+ ?FAIL(<<BigInt/binary>>),
+ Bin123 = <<1,2,3>>,
+ ?FAIL(<<Bin123/float>>),
+ ok.
diff --git a/lib/hipe/test/bs_SUITE_data/bs_decode.erl b/lib/hipe/test/bs_SUITE_data/bs_decode.erl
new file mode 100644
index 0000000000..d12654a1e3
--- /dev/null
+++ b/lib/hipe/test/bs_SUITE_data/bs_decode.erl
@@ -0,0 +1,980 @@
+%% -*- erlang-indent-level: 2 -*-
+
+-module(bs_decode).
+
+-export([test/0]).
+
+-include("bs_decode_extract.hrl").
+
+-define(PDU, <<30,16,0,90,0,1,0,0,255,255,255,255,81,67,101,7,0,0,0,96,
+ 6,12,146,18,14,0,15,252,16,0,0,17,0,0,128,0,2,241,33,131,
+ 0,20,7,97,112,110,48,49,51,97,8,101,114,105,99,115,115,
+ 111,110,2,115,101,132,0,20,128,192,35,16,1,5,0,16,5,117,
+ 115,101,114,53,5,112,97,115,115,53,133,0,4,172,28,12,1,
+ 133,0,4,172,28,12,3,134,0,8,145,148,113,129,0,0,0,0>>).
+
+-define(RES, {ok,{sesT_createReqV0,
+ {mvsgT_tid,{mvsgT_imsi,<<81,67,101,7,0,0,0,240>>},6},
+ [81,67,101,7,0,0,0,96],
+ {sesT_qualityOfServiceV0,1,4,9,2,18},
+ 0,subscribed,0,0,
+ {mvsgT_pdpAddressType,ietf_ipv4,[]},
+ [<<"apn013a">>,<<"ericsson">>,<<"se">>],
+ {masT_protocolConfigOptions,[],
+ {masT_pap,true,1,5,"user5","pass5"},
+ []},
+ {mvsgT_ipAddress,ipv4,172,28,12,1,0,0,0,0},
+ {mvsgT_ipAddress,ipv4,172,28,12,3,0,0,0,0},
+ {mvsT_msisdn,<<145,148,113,129,0,0,0,0>>}},
+ 1}).
+
+test() ->
+ ?RES = decode_v0_opt(42, ?PDU),
+ ok.
+
+decode_v0_opt(0, Pdu) ->
+ decode_gtpc_msg(Pdu);
+decode_v0_opt(N, Pdu) ->
+ decode_gtpc_msg(Pdu),
+ decode_v0_opt(N-1, Pdu).
+
+%%% --------------------------------------------------------------
+%%% #3.1.2 DECODE GTP-C MESSAGE
+%%% --------------------------------------------------------------
+
+%%% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+%%% Function : decode_gtpc_msg(GTP_C_Message)->
+%%% {ok,Request,ControlDataUs} |
+%%% {fault,Cause,Request,ControlDataUs}
+%%%
+%%% Types : GTP_C_Message = binary(), GTP-C message from SGSN
+%%% Request = record(), Containing decoded request
+%%% ControlDataUS = record(), Containing header info
+%%% Cause = integer(), Error code
+%%%
+%%% Description: This function decodes a binary GTP-C message and
+%%% stores it in a record. Different records are used
+%%% for different message types.
+%%%
+%%% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+
+%%% Create PDP Context Request
+%%% GTP97, SNN=0
+%%% (No SNDCP N-PDU number)
+decode_gtpc_msg(<<0:3,_:4,0:1,16:8,_Length:16,SequenceNumber:16,
+ _FlowLabel:16,_SNDCP_N_PDU_Number:8,_:3/binary-unit:8,
+ TID:8/binary-unit:8,InformationElements/binary>>) ->
+ Errors = #protocolErrors{},
+ {ok,TID2} = tid_internal_storage(TID,[]),
+ EmptyCreateReq = #sesT_createReqV0{tid = TID2,
+ tidRaw = binary_to_list(TID)},
+ case catch decode_ie_create(InformationElements,0,Errors,EmptyCreateReq) of
+ {ok,CreateReq} ->
+ {ok,CreateReq,SequenceNumber};
+ {fault,Cause,CreateReq} ->
+ {fault,Cause,CreateReq,SequenceNumber};
+ {'EXIT',_Reason} ->
+ {fault,193,EmptyCreateReq,SequenceNumber}
+ end;
+
+%%% Update PDP Context Request
+%%% GTP97, SNN=0
+%%% (No SNDCP N-PDU number)
+decode_gtpc_msg(<<0:3,_:4,0:1,18:8,_Length:16,SequenceNumber:16,
+ _FlowLabel:16,_SNDCP_N_PDU_Number:8,_:3/binary-unit:8,
+ TID:8/binary-unit:8,InformationElements/binary>>) ->
+ io:format("hej", []),
+ Errors = #protocolErrors{},
+ {ok,TID2}=tid_internal_storage(TID,[]),
+ EmptyUpdateReq=#sesT_updateReqV0{tid=TID2,
+ tidRaw=binary_to_list(TID)},
+ case catch decode_ie_update(InformationElements,0,Errors,
+ EmptyUpdateReq) of
+ {ok,UpdateReq} ->
+ {ok,UpdateReq,SequenceNumber};
+ {fault,Cause,UpdateReq} ->
+ {fault,Cause,UpdateReq,SequenceNumber};
+ {'EXIT',Reason} ->
+ io:format("hej", []),
+ {fault,193,EmptyUpdateReq,SequenceNumber, Reason}
+ end;
+
+%%% Delete PDP Context Request
+%%% GTP97, SNN=0
+%%% (No SNDCP N-PDU number)
+decode_gtpc_msg(<<0:3,_:4,0:1,20:8,_Length:16,SequenceNumber:16,
+ _FlowLabel:16,_SNDCP_N_PDU_Number:8,_:3/binary-unit:8,
+ TID:8/binary-unit:8,_InformationElements/binary>>) ->
+ {ok,TID2} = tid_internal_storage(TID,[]),
+ DeleteReq = #sesT_deleteReqV0{tid=TID2},
+ {ok,DeleteReq,SequenceNumber};
+
+%%% Delete PDP Context Response
+%%% GTP97, SNN=0
+%%% (No SNDCP N-PDU number)
+decode_gtpc_msg(<<0:3,_:4,0:1,21:8,_Length:16,SequenceNumber:16,
+ _FlowLabel:16,_SNDCP_N_PDU_Number:8,_:3/binary-unit:8,
+ TID:8/binary-unit:8,InformationElements/binary>>) ->
+ {ok,TID2} = tid_internal_storage(TID,[]),
+ EmptyDeleteRes = #sesT_deleteResV0{tid=TID2},
+ case catch decode_ie_delete_res(InformationElements,0,EmptyDeleteRes) of
+ {ok, DeleteRes} ->
+ {ok,DeleteRes,SequenceNumber};
+ {fault,Cause,DeleteRes} ->
+ {fault,Cause,DeleteRes,SequenceNumber};
+ {'EXIT',_Reason} ->
+ {fault,193,EmptyDeleteRes,SequenceNumber}
+ end;
+
+%%% Error handling
+decode_gtpc_msg(_GTP_C_Message) ->
+ {fault}.
+
+%%% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+%%% decode_ie_create/4
+%%% Decode information elements for Create PDP Context Request
+
+%%% All elements decoded
+decode_ie_create(<<>>,PresentIEs,Errors,CreateReq) ->
+ %% Check mandatory IE's
+ if
+ (PresentIEs band 16#77D) =/= 16#77D ->
+ {fault,202,CreateReq}; %Mandatory IE missing
+ true -> %OK
+ %% Check errors during decoding
+ case Errors of
+ #protocolErrors{invalidManIE=true} -> %Invalid mandatory IE
+ {fault,201,CreateReq}; %Mandatory IE incorrect
+ #protocolErrors{outOfSequence=true} -> %Out of sequence
+ {fault,193,CreateReq}; %Invalid message format
+ #protocolErrors{incorrectOptIE=true} -> %Incorrect optional IE
+ {fault,203,CreateReq}; %Optional IE incorrect
+ _ -> %OK
+ {ok,CreateReq}
+ end
+ end;
+
+%%% Quality of Service Profile, Mandatory
+decode_ie_create(<<6:8,QoSElement:3/binary-unit:8,Rest/binary>>,PresentIEs,
+ Errors,CreateReq) ->
+ if
+ (PresentIEs band 16#00000001) =:= 16#00000001 -> %Repeated IE's, ignore
+ decode_ie_create(Rest,PresentIEs,Errors,CreateReq);
+ PresentIEs > 16#00000001 -> %Out of sequence
+ UpdatedErrors=Errors#protocolErrors{outOfSequence=true},
+ <<_:2,DelayClass:3,ReliabilityClass:3,
+ PeakThroughput:4,_:1,PrecedenceClass:3,
+ _:3,MeanThroughput:5>> = QoSElement,
+ QoS=#sesT_qualityOfServiceV0{delayClass=DelayClass,
+ reliabilityClass=ReliabilityClass,
+ peakThroughput=PeakThroughput,
+ precedenceClass=PrecedenceClass,
+ meanThroughput=MeanThroughput},
+ UpdatedCreateReq=CreateReq#sesT_createReqV0{qos=QoS},
+ decode_ie_create(Rest,(PresentIEs bor 16#00000001),
+ UpdatedErrors,UpdatedCreateReq);
+ true -> %OK
+ <<_:2,DelayClass:3,ReliabilityClass:3,
+ PeakThroughput:4,_:1,PrecedenceClass:3,
+ _:3,MeanThroughput:5>> = QoSElement,
+ QoS=#sesT_qualityOfServiceV0{delayClass=DelayClass,
+ reliabilityClass=ReliabilityClass,
+ peakThroughput=PeakThroughput,
+ precedenceClass=PrecedenceClass,
+ meanThroughput=MeanThroughput},
+ UpdatedCreateReq=CreateReq#sesT_createReqV0{qos=QoS},
+ decode_ie_create(Rest,(PresentIEs bor 16#00000001),
+ Errors,UpdatedCreateReq)
+ end;
+
+%%% Recovery, Optional
+decode_ie_create(<<14:8,Recovery:8,Rest/binary>>,
+ PresentIEs,Errors,CreateReq) ->
+ if
+ (PresentIEs band 16#00000002) =:= 16#00000002 -> %Repeated IE, ignored
+ decode_ie_create(Rest,PresentIEs,Errors,CreateReq);
+ PresentIEs > 16#00000002 -> %Out of sequence
+ UpdatedErrors=Errors#protocolErrors{outOfSequence=true},
+ UpdatedCreateReq=CreateReq#sesT_createReqV0{recovery=Recovery},
+ decode_ie_create(Rest,(PresentIEs bor 16#00000002),
+ UpdatedErrors,UpdatedCreateReq);
+ true -> %OK
+ UpdatedCreateReq=CreateReq#sesT_createReqV0{recovery=Recovery},
+ decode_ie_create(Rest,(PresentIEs bor 16#00000002),Errors,
+ UpdatedCreateReq)
+ end;
+
+%%% Selection mode, Mandatory
+decode_ie_create(<<15:8,_:6,SelectionMode:2,Rest/binary>>,PresentIEs,
+ Errors,CreateReq) ->
+ if
+ (PresentIEs band 16#00000004) =:= 16#00000004 -> %Repeated IE, ignored
+ decode_ie_create(Rest,PresentIEs,Errors,CreateReq);
+ PresentIEs > 16#00000004 -> %Out of sequence
+ UpdatedErrors=Errors#protocolErrors{outOfSequence=true},
+ UpdatedCreateReq=CreateReq#sesT_createReqV0{
+ selMode=selection_mode_internal_storage(SelectionMode)},
+ decode_ie_create(Rest,(PresentIEs bor 16#00000004),
+ UpdatedErrors,UpdatedCreateReq);
+ true -> %OK
+ UpdatedCreateReq=CreateReq#sesT_createReqV0{
+ selMode=selection_mode_internal_storage(SelectionMode)},
+ decode_ie_create(Rest,(PresentIEs bor 16#00000004),Errors,
+ UpdatedCreateReq)
+ end;
+
+%%% Flow Label Data I, Mandatory
+decode_ie_create(<<16:8,FlowLabel:16,Rest/binary>>,PresentIEs,Errors,CreateReq) ->
+ if
+ (PresentIEs band 16#00000008) =:= 16#00000008 -> %Repeated IE, ignored
+ decode_ie_create(Rest,PresentIEs,Errors,CreateReq);
+ PresentIEs > 16#00000008 -> %Out of sequence
+ UpdatedErrors=Errors#protocolErrors{outOfSequence=true},
+ UpdatedCreateReq=CreateReq#sesT_createReqV0{flowLblData=FlowLabel},
+ decode_ie_create(Rest,(PresentIEs bor 16#00000008),
+ UpdatedErrors,UpdatedCreateReq);
+ true -> %OK
+ UpdatedCreateReq=CreateReq#sesT_createReqV0{flowLblData=FlowLabel},
+ decode_ie_create(Rest,(PresentIEs bor 16#00000008),Errors,
+ UpdatedCreateReq)
+ end;
+
+%%% Flow Label Signalling, Mandatory
+decode_ie_create(<<17:8,FlowLabel:16,Rest/binary>>,PresentIEs,Errors,CreateReq) ->
+ if
+ (PresentIEs band 16#00000010) =:= 16#00000010 -> %Repeated IE, ignored
+ decode_ie_create(Rest,PresentIEs,Errors,CreateReq);
+ PresentIEs > 16#00000010 -> %Out of sequence
+ UpdatedErrors=Errors#protocolErrors{outOfSequence=true},
+ UpdatedCreateReq=CreateReq#sesT_createReqV0{flowLblSig=FlowLabel},
+ decode_ie_create(Rest,(PresentIEs bor 16#00000010),
+ UpdatedErrors,UpdatedCreateReq);
+ true -> %OK
+ UpdatedCreateReq=CreateReq#sesT_createReqV0{flowLblSig=FlowLabel},
+ decode_ie_create(Rest,(PresentIEs bor 16#00000010),Errors,
+ UpdatedCreateReq)
+ end;
+
+%%% End User Address, Mandatory
+decode_ie_create(<<128:8,Length:16,More/binary>>,PresentIEs,
+ Errors,CreateReq) ->
+ <<PDPElement:Length/binary-unit:8,Rest/binary>> = More,
+ if
+ (PresentIEs band 16#00000020) =:= 16#00000020 -> %Repeated IE, ignore
+ decode_ie_create(Rest,PresentIEs,Errors,CreateReq);
+ PresentIEs > 16#00000020 -> %Out of sequence
+ case pdp_addr_internal_storage(PDPElement) of
+ {ok,PDPAddress} ->
+ UpdatedErrors=Errors#protocolErrors{outOfSequence=true},
+ UpdatedCreateReq=CreateReq#sesT_createReqV0{endUserAdd=PDPAddress},
+ decode_ie_create(Rest,(PresentIEs bor 16#00000020),
+ UpdatedErrors,UpdatedCreateReq);
+ {fault} ->
+ UpdatedErrors=Errors#protocolErrors{invalidManIE=true,
+ outOfSequence=true},
+ decode_ie_create(Rest,(PresentIEs bor 16#00000020),
+ UpdatedErrors,CreateReq)
+ end;
+ true -> %OK
+ case pdp_addr_internal_storage(PDPElement) of
+ {ok,PDPAddress} ->
+ UpdatedCreateReq=CreateReq#sesT_createReqV0{endUserAdd=PDPAddress},
+ decode_ie_create(Rest,(PresentIEs bor 16#00000020),
+ Errors,UpdatedCreateReq);
+ {fault} ->
+ UpdatedErrors=Errors#protocolErrors{invalidManIE=true},
+ decode_ie_create(Rest,(PresentIEs bor 16#00000020),
+ UpdatedErrors,CreateReq)
+ end
+ end;
+
+%%% Access Point Name, Mandatory
+decode_ie_create(<<131:8,Length:16,More/binary>>,PresentIEs,
+ Errors,CreateReq) ->
+ <<APNElement:Length/binary-unit:8,Rest/binary>> = More,
+ if
+ (PresentIEs band 16#00000040) =:= 16#00000040 -> %Repeated IE, ignore
+ decode_ie_create(Rest,PresentIEs,Errors,CreateReq);
+ PresentIEs > 16#00000040 -> %Out of sequence
+ case catch apn_internal_storage(APNElement,[]) of
+ {ok,APN} ->
+ UpdatedErrors=Errors#protocolErrors{outOfSequence=true},
+ UpdatedCreateReq=CreateReq#sesT_createReqV0{accPointName=APN},
+ decode_ie_create(Rest,(PresentIEs bor 16#00000040),
+ UpdatedErrors,UpdatedCreateReq);
+ _ ->
+ UpdatedErrors=Errors#protocolErrors{outOfSequence=true,
+ invalidManIE=true},
+ decode_ie_create(Rest,(PresentIEs bor 16#00000040),
+ UpdatedErrors,CreateReq)
+ end;
+ true -> %OK
+ case catch apn_internal_storage(APNElement,[]) of
+ {ok,APN} ->
+ UpdatedCreateReq=CreateReq#sesT_createReqV0{accPointName=APN},
+ decode_ie_create(Rest,(PresentIEs bor 16#00000040),
+ Errors,UpdatedCreateReq);
+ _ ->
+ UpdatedErrors=Errors#protocolErrors{invalidManIE=true},
+ decode_ie_create(Rest,(PresentIEs bor 16#00000040),
+ UpdatedErrors,CreateReq)
+ end
+ end;
+
+%%% Protocol Configuration Options, Optional
+decode_ie_create(<<132:8,Length:16,More/binary>>,PresentIEs,Errors,CreateReq) ->
+ <<ConfigurationElement:Length/binary-unit:8,Rest/binary>> = More,
+ if
+ (PresentIEs band 16#00000080) =:= 16#00000080 -> %Repeated IE, ignore
+ decode_ie_create(Rest,PresentIEs,Errors,CreateReq);
+ PresentIEs > 16#00000080 -> %Out of sequence
+ case catch pco_internal_storage(ConfigurationElement) of
+ {ok,PCO} ->
+ UpdatedErrors=Errors#protocolErrors{outOfSequence=true},
+ UpdatedCreateReq=CreateReq#sesT_createReqV0{protConOpt=PCO},
+ decode_ie_create(Rest,(PresentIEs bor 16#00000080),
+ UpdatedErrors,UpdatedCreateReq);
+ _ ->
+ UpdatedErrors=Errors#protocolErrors{outOfSequence=true,
+ incorrectOptIE=true},
+ decode_ie_create(Rest,(PresentIEs bor 16#00000080),
+ UpdatedErrors,CreateReq)
+ end;
+ true -> %OK
+ case catch pco_internal_storage(ConfigurationElement) of
+ {ok,PCO} ->
+ UpdatedCreateReq=CreateReq#sesT_createReqV0{protConOpt=PCO},
+ decode_ie_create(Rest,(PresentIEs bor 16#00000080),
+ Errors,UpdatedCreateReq);
+ _ ->
+ UpdatedErrors=Errors#protocolErrors{incorrectOptIE=true},
+ decode_ie_create(Rest,(PresentIEs bor 16#00000080),
+ UpdatedErrors,CreateReq)
+ end
+ end;
+
+%%% SGSN Address for signalling, Mandatory OR SGSN Address for user traffic, Mandatory
+decode_ie_create(<<133:8,Length:16,More/binary>>,PresentIEs,
+ Errors,CreateReq) ->
+ <<AddressElement:Length/binary-unit:8,Rest/binary>> = More,
+ if
+ (PresentIEs band 16#00000300) =:= 16#00000300 -> %Repeated IE, ignore
+ decode_ie_create(Rest,PresentIEs,Errors,CreateReq);
+ PresentIEs > 16#00000200 -> %Out of sequence
+ if
+ (PresentIEs band 16#00000100) =:= 16#00000000 -> %Signalling
+ case gsn_addr_internal_storage(AddressElement) of
+ {ok,GSNAddr} ->
+ UpdatedErrors=Errors#protocolErrors{outOfSequence=true},
+ UpdatedCreateReq=CreateReq#sesT_createReqV0{sgsnAddSig=GSNAddr},
+ decode_ie_create(Rest,(PresentIEs bor 16#00000100),
+ UpdatedErrors,UpdatedCreateReq);
+ {fault} ->
+ UpdatedErrors=Errors#protocolErrors{invalidManIE=true,
+ outOfSequence=true},
+ decode_ie_create(Rest,(PresentIEs bor 16#00000100),
+ UpdatedErrors,CreateReq)
+ end;
+ true -> % User traffic
+ case gsn_addr_internal_storage(AddressElement) of
+ {ok,GSNAddr} ->
+ UpdatedErrors=Errors#protocolErrors{outOfSequence=true},
+ UpdatedCreateReq=CreateReq#sesT_createReqV0{sgsnAddUser=GSNAddr},
+ decode_ie_create(Rest,(PresentIEs bor 16#00000200),
+ UpdatedErrors,UpdatedCreateReq);
+ {fault} ->
+ UpdatedErrors=Errors#protocolErrors{invalidManIE=true,
+ outOfSequence=true},
+ decode_ie_create(Rest,(PresentIEs bor 16#00000200),
+ UpdatedErrors,CreateReq)
+ end
+ end;
+ PresentIEs < 16#00000100 -> %OK, SGSN Address for signalling
+ case gsn_addr_internal_storage(AddressElement) of
+ {ok,GSNAddr} ->
+ UpdatedCreateReq=CreateReq#sesT_createReqV0{sgsnAddSig=GSNAddr},
+ decode_ie_create(Rest,(PresentIEs bor 16#00000100),
+ Errors,UpdatedCreateReq);
+ {fault} ->
+ UpdatedErrors=Errors#protocolErrors{invalidManIE=true},
+ decode_ie_create(Rest,(PresentIEs bor 16#00000100),
+ UpdatedErrors,CreateReq)
+ end;
+ true -> %OK, SGSN Address for user traffic
+ case gsn_addr_internal_storage(AddressElement) of
+ {ok,GSNAddr} ->
+ UpdatedCreateReq=CreateReq#sesT_createReqV0{sgsnAddUser=GSNAddr},
+ decode_ie_create(Rest,(PresentIEs bor 16#00000200),
+ Errors,UpdatedCreateReq);
+ {fault} ->
+ UpdatedErrors=Errors#protocolErrors{invalidManIE=true},
+ decode_ie_create(Rest,(PresentIEs bor 16#00000200),
+ UpdatedErrors,CreateReq)
+ end
+ end;
+
+%%% MSISDN, Mandatory
+decode_ie_create(<<134:8,Length:16,More/binary>>,PresentIEs,
+ Errors,CreateReq) ->
+ <<MSISDNElement:Length/binary-unit:8,Rest/binary>> = More,
+ if
+ (PresentIEs band 16#00000400) =:= 16#00000400 -> %Repeated IE, ignore
+ decode_ie_create(Rest,PresentIEs,Errors,CreateReq);
+ PresentIEs > 16#00000400 -> %Out of sequence
+ case msisdn_internal_storage(MSISDNElement,[]) of
+ {ok,MSISDN} ->
+ UpdatedErrors=Errors#protocolErrors{outOfSequence=true},
+ UpdatedCreateReq=CreateReq#sesT_createReqV0{msisdn=MSISDN},
+ decode_ie_create(Rest,(PresentIEs bor 16#00000400),
+ UpdatedErrors,UpdatedCreateReq);
+ {fault} ->
+ UpdatedErrors=Errors#protocolErrors{outOfSequence=true,invalidManIE=true},
+ decode_ie_create(Rest,(PresentIEs bor 16#00000400),
+ UpdatedErrors,CreateReq)
+ end;
+ true -> %OK
+ UpdatedCreateReq=CreateReq#sesT_createReqV0{msisdn=#mvsT_msisdn{value=MSISDNElement}},
+ decode_ie_create(Rest,(PresentIEs bor 16#00000400),
+ Errors,UpdatedCreateReq)
+
+ end;
+
+%%% Private Extension, Optional
+%%% Not implemented
+
+%%% Error handling, Unexpected or unknown IE
+decode_ie_create(UnexpectedIE,PresentIEs,Errors,CreateReq) ->
+ case check_ie(UnexpectedIE) of
+ {defined_ie,Rest} -> %OK, ignored
+ decode_ie_create(Rest,PresentIEs,Errors,CreateReq);
+ {handled_ie,Rest} -> %OK, ignored
+ decode_ie_create(Rest,PresentIEs,Errors,CreateReq);
+ {unhandled_ie} -> %Error, abort decoding
+ {fault,193,CreateReq} %Invalid message format
+ end.
+
+%%% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+%%% decode_ie_update/4
+%%% Decode information elements for Update PDP Context Request
+
+%%% All elements decoded
+decode_ie_update(<<>>,PresentIEs,Errors,UpdateReq) ->
+ %% Check mandatory IE's
+ if
+ (PresentIEs band 16#3D) =/= 16#3D ->
+ {fault,202,UpdateReq}; %Mandatory IE missing
+ true -> %OK
+ %% Check errors during decoding
+ case Errors of
+ #protocolErrors{invalidManIE=true} -> %Invalid mandatory IE
+ {fault,201,UpdateReq}; %Mandatory IE incorrect
+ #protocolErrors{outOfSequence=true} -> %Out of sequence
+ {fault,193,UpdateReq}; %Invalid message format
+ _ -> %OK
+ {ok,UpdateReq}
+ end
+ end;
+
+%%% Quality of Service Profile, Mandatory
+decode_ie_update(<<6:8,QoSElement:3/binary-unit:8,Rest/binary>>,PresentIEs,
+ Errors,UpdateReq) ->
+ if
+ (PresentIEs band 16#00000001) =:= 16#00000001 -> %Repeated IE's, ignore
+ decode_ie_update(Rest,PresentIEs,Errors,UpdateReq);
+ PresentIEs > 16#00000001 -> %Out of sequence
+ UpdatedErrors=Errors#protocolErrors{outOfSequence=true},
+ <<_:2,DelayClass:3,ReliabilityClass:3,
+ PeakThroughput:4,_:1,PrecedenceClass:3,
+ _:3,MeanThroughput:5>> = QoSElement,
+ QoS=#sesT_qualityOfServiceV0{delayClass=DelayClass,
+ reliabilityClass=ReliabilityClass,
+ peakThroughput=PeakThroughput,
+ precedenceClass=PrecedenceClass,
+ meanThroughput=MeanThroughput},
+ UpdatedUpdateReq=UpdateReq#sesT_updateReqV0{qos=QoS},
+ decode_ie_update(Rest,(PresentIEs bor 16#00000001),
+ UpdatedErrors,UpdatedUpdateReq);
+ true -> %OK
+ <<_:2,DelayClass:3,ReliabilityClass:3,
+ PeakThroughput:4,_:1,PrecedenceClass:3,
+ _:3,MeanThroughput:5>> = QoSElement,
+ QoS=#sesT_qualityOfServiceV0{delayClass=DelayClass,
+ reliabilityClass=ReliabilityClass,
+ peakThroughput=PeakThroughput,
+ precedenceClass=PrecedenceClass,
+ meanThroughput=MeanThroughput},
+ UpdatedUpdateReq=UpdateReq#sesT_updateReqV0{qos=QoS},
+ decode_ie_update(Rest,(PresentIEs bor 16#00000001),
+ Errors,UpdatedUpdateReq)
+ end;
+
+%%% Recovery, Optional
+decode_ie_update(<<14:8,Recovery:8,Rest/binary>>,PresentIEs,Errors,UpdateReq) ->
+ if
+ (PresentIEs band 16#00000002) =:= 16#00000002 -> %Repeated IE, ignored
+ decode_ie_update(Rest,PresentIEs,Errors,UpdateReq);
+ PresentIEs > 16#00000002 -> %Out of sequence
+ UpdatedErrors=Errors#protocolErrors{outOfSequence=true},
+ UpdatedUpdateReq=UpdateReq#sesT_updateReqV0{recovery=Recovery},
+ decode_ie_update(Rest,(PresentIEs bor 16#00000002),
+ UpdatedErrors,UpdatedUpdateReq);
+ true -> %OK
+ UpdatedUpdateReq=UpdateReq#sesT_updateReqV0{recovery=Recovery},
+ decode_ie_update(Rest,(PresentIEs bor 16#00000002),Errors,
+ UpdatedUpdateReq)
+ end;
+
+%%% Flow Label Data I, Mandatory
+decode_ie_update(<<16:8,FlowLabel:16,Rest/binary>>,PresentIEs,Errors,UpdateReq) ->
+ if
+ (PresentIEs band 16#00000004) =:= 16#00000004 -> %Repeated IE, ignored
+ decode_ie_update(Rest,PresentIEs,Errors,UpdateReq);
+ PresentIEs > 16#00000004 -> %Out of sequence
+ UpdatedErrors=Errors#protocolErrors{outOfSequence=true},
+ UpdatedUpdateReq=UpdateReq#sesT_updateReqV0{flowLblData=FlowLabel},
+ decode_ie_update(Rest,(PresentIEs bor 16#00000004),
+ UpdatedErrors,UpdatedUpdateReq);
+ true -> %OK
+ UpdatedUpdateReq=UpdateReq#sesT_updateReqV0{flowLblData=FlowLabel},
+ decode_ie_update(Rest,(PresentIEs bor 16#00000004),Errors,
+ UpdatedUpdateReq)
+ end;
+
+%%% Flow Label Signalling, Mandatory
+decode_ie_update(<<17:8,FlowLabel:16,Rest/binary>>,PresentIEs,Errors,UpdateReq) ->
+ if
+ (PresentIEs band 16#00000008) =:= 16#00000008 -> %Repeated IE, ignored
+ decode_ie_update(Rest,PresentIEs,Errors,UpdateReq);
+ PresentIEs > 16#00000008 -> %Out of sequence
+ UpdatedErrors=Errors#protocolErrors{outOfSequence=true},
+ UpdatedUpdateReq=UpdateReq#sesT_updateReqV0{flowLblSig=FlowLabel},
+ decode_ie_update(Rest,(PresentIEs bor 16#00000008),
+ UpdatedErrors,UpdatedUpdateReq);
+ true -> %OK
+ UpdatedUpdateReq=UpdateReq#sesT_updateReqV0{flowLblSig=FlowLabel},
+ decode_ie_update(Rest,(PresentIEs bor 16#00000008),Errors,
+ UpdatedUpdateReq)
+ end;
+
+%%% SGSN Address for signalling, Mandatory OR SGSN Address for user traffic, Mandatory
+decode_ie_update(<<133:8,Length:16,More/binary>>,PresentIEs,
+ Errors,UpdateReq) ->
+ <<AddressElement:Length/binary-unit:8,Rest/binary>> = More,
+ if
+ (PresentIEs band 16#00000030) =:= 16#00000030 -> %Repeated IE, ignore
+ decode_ie_update(Rest,PresentIEs,Errors,UpdateReq);
+ PresentIEs > 16#00000020 -> %Out of sequence
+ if
+ (PresentIEs band 16#00000010) =:= 16#00000000 -> %Signalling
+ case gsn_addr_internal_storage(AddressElement) of
+ {ok,GSNAddr} ->
+ UpdatedErrors=Errors#protocolErrors{outOfSequence=true},
+ UpdatedUpdateReq=UpdateReq#sesT_updateReqV0{sgsnAddSig=GSNAddr},
+ decode_ie_update(Rest,(PresentIEs bor 16#00000010),
+ UpdatedErrors,UpdatedUpdateReq);
+ {fault} ->
+ UpdatedErrors=Errors#protocolErrors{invalidManIE=true,
+ outOfSequence=true},
+ decode_ie_update(Rest,(PresentIEs bor 16#00000010),
+ UpdatedErrors,UpdateReq)
+ end;
+ true -> % User traffic
+ case gsn_addr_internal_storage(AddressElement) of
+ {ok,GSNAddr} ->
+ UpdatedErrors=Errors#protocolErrors{outOfSequence=true},
+ UpdatedUpdateReq=UpdateReq#sesT_updateReqV0{sgsnAddUser=GSNAddr},
+ decode_ie_update(Rest,(PresentIEs bor 16#00000020),
+ UpdatedErrors,UpdatedUpdateReq);
+ {fault} ->
+ UpdatedErrors=Errors#protocolErrors{invalidManIE=true,
+ outOfSequence=true},
+ decode_ie_update(Rest,(PresentIEs bor 16#00000020),
+ UpdatedErrors,UpdateReq)
+ end
+ end;
+ PresentIEs < 16#00000010 -> %OK, SGSN Address for signalling
+ case gsn_addr_internal_storage(AddressElement) of
+ {ok,GSNAddr} ->
+ UpdatedUpdateReq=UpdateReq#sesT_updateReqV0{sgsnAddSig=GSNAddr},
+ decode_ie_update(Rest,(PresentIEs bor 16#00000010),
+ Errors,UpdatedUpdateReq);
+ {fault} ->
+ UpdatedErrors=Errors#protocolErrors{invalidManIE=true},
+ decode_ie_update(Rest,(PresentIEs bor 16#00000010),
+ UpdatedErrors,UpdateReq)
+ end;
+ true -> %OK, SGSN Address for user traffic
+ case gsn_addr_internal_storage(AddressElement) of
+ {ok,GSNAddr} ->
+ UpdatedUpdateReq=UpdateReq#sesT_updateReqV0{sgsnAddUser=GSNAddr},
+ decode_ie_update(Rest,(PresentIEs bor 16#00000020),
+ Errors,UpdatedUpdateReq);
+ {fault} ->
+ UpdatedErrors=Errors#protocolErrors{invalidManIE=true},
+ decode_ie_update(Rest,(PresentIEs bor 16#00000020),
+ UpdatedErrors,UpdateReq)
+ end
+ end;
+
+%%% Private Extension, Optional
+%%% Not implemented
+
+%%% Error handling, Unexpected or unknown IE
+decode_ie_update(UnexpectedIE,PresentIEs,Errors,UpdateReq) ->
+ case check_ie(UnexpectedIE) of
+ {defined_ie,Rest} -> %OK, ignored
+ decode_ie_update(Rest,PresentIEs,Errors,UpdateReq);
+ {handled_ie,Rest} -> %OK, ignored
+ decode_ie_update(Rest,PresentIEs,Errors,UpdateReq);
+ {unhandled_ie} -> %Error, abort decoding
+ {fault,193,UpdateReq} %Invalid message format
+ end.
+
+
+%%% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+%%% decode_ie_delete_req/4
+%%% Decode information elements for Delete PDP Context Request
+
+%%% Private Extension, Optional
+%%% Not implemented
+
+
+%%% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+%%% decode_ie_delete_res/4
+%%% Decode information elements for Delete PDP Context Response
+
+%%% All elements decoded
+decode_ie_delete_res(<<>>,PresentIEs,DeleteRes) ->
+ %% Check mandatory IE's
+ if
+ (PresentIEs band 16#0001) =/= 16#0001 ->
+ {fault,202,DeleteRes}; %Mandatory IE missing
+ true -> %OK
+ {ok,DeleteRes}
+ end;
+
+%%% Cause, Mandatory
+decode_ie_delete_res(<<1:8,Cause:8,Rest/binary>>,PresentIEs,DeleteRes) ->
+ if
+ (PresentIEs band 16#00000001) =:= 16#00000001 -> %Repeated IE, ignored
+ decode_ie_delete_res(Rest,PresentIEs,DeleteRes);
+ true -> %OK
+ UpdatedDeleteRes=DeleteRes#sesT_deleteResV0{cause=Cause},
+ decode_ie_delete_res(Rest,(PresentIEs bor 16#00000001),
+ UpdatedDeleteRes)
+ end;
+
+%%% Private Extension, Optional
+%%% Not implemented
+
+%%% Error handling, Unexpected or unknown IE
+decode_ie_delete_res(UnexpectedIE,PresentIEs,DeleteRes) ->
+ case check_ie(UnexpectedIE) of
+ {defined_ie,Rest} -> %OK, ignored
+ decode_ie_delete_res(Rest,PresentIEs,DeleteRes);
+ {handled_ie,Rest} -> %OK, ignored
+ decode_ie_delete_res(Rest,PresentIEs,DeleteRes);
+ {unhandled_ie} -> %Error, abort decoding
+ {fault,193,DeleteRes} %Invalid message format
+ end.
+
+%%% --------------------------------------------------------------
+%%% #3.2 COMMON INTERNAL FUNCTIONS
+%%% --------------------------------------------------------------
+
+%%% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+%%% check_ie/1
+%%% Check Information Element, Unexpected or Unknown
+check_ie(<<1:8,_:8,Rest/binary>>) ->
+ {defined_ie,Rest};
+%%% IMSI
+check_ie(<<2:8,_:8/binary-unit:8,Rest/binary>>) ->
+ {defined_ie,Rest};
+%%% RAI
+check_ie(<<3:8,_:6/binary-unit:8,Rest/binary>>) ->
+ {defined_ie,Rest};
+%%% TTLI
+check_ie(<<4:8,_:4/binary-unit:8,Rest/binary>>) ->
+ {defined_ie,Rest};
+%%% P-TMSI
+check_ie(<<5:8,_:4/binary-unit:8,Rest/binary>>) ->
+ {defined_ie,Rest};
+%%% Quality of Service Profile
+check_ie(<<6:8,_:3/binary-unit:8,Rest/binary>>) ->
+ {defined_ie,Rest};
+%%% Reordering Required
+check_ie(<<8:8,_:8,Rest/binary>>) ->
+ {defined_ie,Rest};
+%%% Authentication Triplet
+check_ie(<<9:8,_:28/binary-unit:8,Rest/binary>>) ->
+ {defined_ie,Rest};
+%%% MAP Cause
+check_ie(<<11:8,_:8,Rest/binary>>) ->
+ {defined_ie,Rest};
+%%% P-TMSI Signature
+check_ie(<<12:8,_:3/binary-unit:8,Rest/binary>>) ->
+ {defined_ie,Rest};
+%%% MS Validated
+check_ie(<<13:8,_:8,Rest/binary>>) ->
+ {defined_ie,Rest};
+%%% Recovery
+check_ie(<<14:8,_:8,Rest/binary>>) ->
+ {defined_ie,Rest};
+%%% Selection Mode
+check_ie(<<15:8,_:8,Rest/binary>>) ->
+ {defined_ie,Rest};
+%%% Flow Label Data I
+check_ie(<<16:8,_:16,Rest/binary>>) ->
+ {defined_ie,Rest};
+%%% Flow Label Signalling
+check_ie(<<17:8,_:16,Rest/binary>>) ->
+ {defined_ie,Rest};
+%%% Flow Label Data II
+check_ie(<<18:8,_:32,Rest/binary>>) ->
+ {defined_ie,Rest};
+%%% MS Not Reachable Reason
+check_ie(<<19:8,_:8,Rest/binary>>) ->
+ {defined_ie,Rest};
+%%% Charging ID
+check_ie(<<127:8,_:4/binary-unit:8,Rest/binary>>) ->
+ {defined_ie,Rest};
+%%% TLV element, skipped using Length
+check_ie(<<1:1,_:7,Length:16,More/binary>>) ->
+ if
+ Length > byte_size(More) ->
+ {unhandled_ie};
+ true ->
+ <<_:Length/binary-unit:8,Rest/binary>> = More,
+ {handled_ie,Rest}
+ end;
+%%% TV element, unknown size. Can not be handled.
+check_ie(_UnhandledIE) ->
+ {unhandled_ie}.
+
+%%% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+%%% tid_internal_storage/3
+%%% Convert TID binary to internal datatype
+tid_internal_storage(Bin,_) ->
+ Size = byte_size(Bin) - 1,
+ <<Front:Size/binary,NSAPI:4,DigitN:4>> = Bin,
+ Result =
+ case DigitN of
+ 2#1111 ->
+ #mvsgT_tid{imsi = #mvsgT_imsi{value = Front}, nsapi = NSAPI};
+ _ ->
+ Value = <<Front/binary,2#1111:4,DigitN:4>>,
+ #mvsgT_tid{imsi = #mvsgT_imsi{value = Value}, nsapi = NSAPI}
+ end,
+ {ok,Result}.
+%% tid_internal_storage(<<NSAPI:4,2#1111:4>>,IMSI) ->
+%% {ok,#mvsgT_tid{imsi=#mvsgT_imsi{value=lists:reverse(IMSI)},
+%% nsapi=NSAPI}};
+%% tid_internal_storage(<<NSAPI:4,DigitN:4>>,IMSI) when
+%% DigitN < 10 ->
+%% {ok,#mvsgT_tid{imsi=#mvsgT_imsi{value=lists:reverse([(DigitN bor 2#11110000)|IMSI])},
+%% nsapi=NSAPI}};
+%% tid_internal_storage(<<2#11111111:8,Rest/binary>>,IMSI) ->
+%% tid_internal_storage(Rest,IMSI);
+%% tid_internal_storage(<<2#1111:4,DigitN:4,Rest/binary>>,IMSI) when
+%% DigitN < 10 ->
+%% tid_internal_storage(Rest,[(DigitN bor 2#11110000)|IMSI]);
+%% tid_internal_storage(<<DigitNplus1:4,DigitN:4,Rest/binary>>,IMSI) when
+%% DigitNplus1 < 10,
+%% DigitN < 10 ->
+%% tid_internal_storage(Rest,[((DigitNplus1 bsl 4) bor DigitN)|IMSI]);
+%% tid_internal_storage(_Rest,_IMSI) ->
+%% {fault}. %% Mandatory IE incorrect
+
+%%% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+%%% selection_mode_internal_storage/1
+%%% Convert Selection Mode integer to internal datatype (enum)
+selection_mode_internal_storage(0) ->
+ subscribed;
+selection_mode_internal_storage(1) ->
+ msRequested;
+selection_mode_internal_storage(2) ->
+ sgsnSelected;
+selection_mode_internal_storage(3) ->
+ sgsnSelected.
+
+%%% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+%%% pdp_addr_internal_storage/1
+%%% Convert PDP address to internal datatype (record containing
+%%% addresstype and value)
+pdp_addr_internal_storage(<<_:4,0:4,1:8>>) ->
+ {ok,#mvsgT_pdpAddressType{pdpTypeNbr=etsi_ppp,address=[]}};
+pdp_addr_internal_storage(<<_:4,0:4,2:8>>) ->
+ {ok,#mvsgT_pdpAddressType{pdpTypeNbr=etsi_osp_ihoss,address=[]}};
+pdp_addr_internal_storage(<<_:4,1:4,16#21:8>>) ->
+ {ok,#mvsgT_pdpAddressType{pdpTypeNbr=ietf_ipv4,address=[]}};
+pdp_addr_internal_storage(<<_:4,1:4,16#21:8,IP_A:8,IP_B:8,IP_C:8,IP_D:8>>) ->
+ {ok,#mvsgT_pdpAddressType{pdpTypeNbr=ietf_ipv4,
+ address=[IP_A,IP_B,IP_C,IP_D]}};
+pdp_addr_internal_storage(<<_:4,1:4,16#57:8,IP_A:16,IP_B:16,IP_C:16,IP_D:16,
+ IP_E:16,IP_F:16,IP_G:16,IP_H:16>>) ->
+ {ok,#mvsgT_pdpAddressType{pdpTypeNbr=ietf_ipv6,
+ address=[IP_A,IP_B,IP_C,IP_D,IP_E,IP_F,IP_G,IP_H]}};
+pdp_addr_internal_storage(_PDP_ADDR) ->
+ {fault}.
+
+%%% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+%%% apn_internal_storage/2
+%%% Convert APN to internal datatype (List containing APN labels)
+apn_internal_storage(<<>>,APN) ->
+ {ok,lists:reverse(APN)};
+apn_internal_storage(<<Length:8,Rest/binary>>,APN) ->
+ <<Label:Length/binary-unit:8,MoreAPNLabels/binary>> = Rest,
+ apn_internal_storage(MoreAPNLabels,[Label|APN]).
+
+%%% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+%%% pco_internal_storage/1
+%%% Convert Protocol Configuration Options to internal datatype.
+%%% Implemented configuration options:
+%%% For PPP:
+%%% LCP - Not implemented
+%%% PAP - Authenticate request
+%%% CHAP - Challenge
+%%% - Response
+%%% IPCP - IP-Address
+%%% For OSP:IHOSS
+%%% Nothing implemented
+pco_internal_storage(<<1:1,_:4,0:3,PPPConfigurationOptions/binary>>) ->
+ case ppp_configuration_options(PPPConfigurationOptions,
+ #masT_pap{exists=false},[],[]) of
+ {ok,PAP,CHAP,IPCP} ->
+ {ok,#masT_protocolConfigOptions{pap=PAP,chap=CHAP,ipcp=IPCP}};
+ {fault} ->
+ {fault}
+ end;
+pco_internal_storage(<<1:1,_:4,1:3,_OSP_IHOSSConfigurationOptions/binary>>) ->
+ {ok,osp_ihoss};
+pco_internal_storage(_UnknownConfigurationOptions) ->
+ {fault}. %% Optional IE incorrect
+
+ppp_configuration_options(<<>>,PAP,CHAP,IPCP) ->
+ {ok,PAP,CHAP,IPCP};
+ppp_configuration_options(<<16#C021:16,Length:8,More/binary>>,PAP,CHAP,IPCP) ->
+ %% LCP - Not implemented
+ <<_LCP:Length/binary-unit:8,Rest/binary>> = More,
+ ppp_configuration_options(Rest,PAP,CHAP,IPCP);
+ppp_configuration_options(<<16#C023:16,_Length:8,1:8,Identifier:8,DataLength:16,
+ More/binary>>,_PAP,CHAP,IPCP) ->
+ %% PAP - Authenticate request
+ ActualDataLength=DataLength-4, %% DataLength includes Code, Identifier and itself
+ <<Data:ActualDataLength/binary-unit:8,Rest/binary>> = More,
+ <<PeerIDLength:8,PeerData/binary>> = Data,
+ <<PeerID:PeerIDLength/binary-unit:8,PasswdLength:8,PasswordData/binary>> = PeerData,
+ <<Password:PasswdLength/binary,_Padding/binary>> = PasswordData,
+ ppp_configuration_options(Rest,#masT_pap{exists=true,code=1,id=Identifier,
+ username=binary_to_list(PeerID),
+ password=binary_to_list(Password)},CHAP,IPCP);
+
+ppp_configuration_options(<<16#C023:16,Length:8,More/binary>>,PAP,CHAP,IPCP) ->
+ %% PAP - Other, not implemented
+ <<_PAP:Length/binary-unit:8,Rest/binary>> = More,
+ ppp_configuration_options(Rest,PAP,CHAP,IPCP);
+ppp_configuration_options(<<16#C223:16,_Length:8,1:8,Identifier:8,DataLength:16,
+ More/binary>>,PAP,CHAP,IPCP) ->
+ %% CHAP - Challenge
+ ActualDataLength=DataLength-4, %% DataLength includes Code, Identifier and itself
+ <<Data:ActualDataLength/binary-unit:8,Rest/binary>> = More,
+ <<ValueSize:8,ValueAndName/binary>> = Data,
+ <<Value:ValueSize/binary-unit:8,Name/binary>> = ValueAndName,
+ ppp_configuration_options(Rest,PAP,[#masT_chap{code=1,id=Identifier,
+ value=binary_to_list(Value),
+ name=binary_to_list(Name)}|CHAP],
+ IPCP);
+ppp_configuration_options(<<16#C223:16,_Length:8,2:8,Identifier:8,DataLength:16,
+ More/binary>>,PAP,CHAP,IPCP) ->
+ %% CHAP - Response
+ ActualDataLength=DataLength-4, %% DataLength includes Code, Identifier and itself
+ <<Data:ActualDataLength/binary-unit:8,Rest/binary>> = More,
+ <<ValueSize:8,ValueAndName/binary>> = Data,
+ <<Value:ValueSize/binary-unit:8,Name/binary>> = ValueAndName,
+ ppp_configuration_options(Rest,PAP,[#masT_chap{code=2,id=Identifier,
+ value=binary_to_list(Value),
+ name=binary_to_list(Name)}|CHAP],
+ IPCP);
+ppp_configuration_options(<<16#C223:16,Length:8,More/binary>>,PAP,CHAP,IPCP) ->
+ %% CHAP - Other, not implemented
+ <<_CHAP:Length/binary-unit:8,Rest/binary>> = More,
+ ppp_configuration_options(Rest,PAP,CHAP,IPCP);
+ppp_configuration_options(<<16#8021:16,_Length:8,1:8,Identifier:8,OptionsLength:16,
+ More/binary>>,PAP,CHAP,IPCP) ->
+ %% IPCP - Configure request
+ ActualOptionsLength=OptionsLength-4, %% OptionsLength includes Code, Identifier and itself
+ <<Options:ActualOptionsLength/binary-unit:8,Rest/binary>> = More,
+ case Options of
+ <<3:8,6:8,A1:8,A2:8,A3:8,A4:8>> ->
+ %% IP Address, version 4
+ ppp_configuration_options(Rest,PAP,CHAP,
+ [#masT_ipcp{exists=true,code=1,
+ id=Identifier,
+ ipcpList=[#masT_ipcpData{type=3,ipAddress=
+ #mvsgT_ipAddress{version=ipv4,
+ a1=A1,a2=A2,
+ a3=A3,a4=A4,
+ a5=0,a6=0,
+ a7=0,a8=0},
+ rawMessage=binary_to_list(Options)}]}|IPCP]);
+ <<129:8,6:8,B1:8,B2:8,B3:8,B4:8>> ->
+ %% IP Address, version 4
+ ppp_configuration_options(Rest,PAP,CHAP,
+ [#masT_ipcp{exists=true,code=1,
+ id=Identifier,
+ ipcpList=[#masT_ipcpData{type=129,ipAddress=
+ #mvsgT_ipAddress{version=ipv4,
+ a1=B1,a2=B2,
+ a3=B3,a4=B4},
+ rawMessage=binary_to_list(Options)}]}|IPCP]);
+
+ <<131:8,6:8,C1:8,C2:8,C3:8,C4:8>> ->
+ %% IP Address, version 4
+ ppp_configuration_options(Rest,PAP,CHAP,
+ [#masT_ipcp{exists=true,code=1,
+ id=Identifier,
+ ipcpList=[#masT_ipcpData{type=131,ipAddress=
+ #mvsgT_ipAddress{version=ipv4,
+ a1=C1,a2=C2,
+ a3=C3,a4=C4},
+ rawMessage=binary_to_list(Options)}]}|IPCP]);
+ _ ->
+ ppp_configuration_options(Rest,PAP,CHAP,IPCP)
+ end;
+ppp_configuration_options(<<_UnknownProtocolID:16,Length:8,More/binary>>,
+ PAP,CHAP,IPCP) ->
+ <<_Skipped:Length/binary-unit:8,Rest/binary>> = More,
+ ppp_configuration_options(Rest,PAP,CHAP,IPCP);
+ppp_configuration_options(_Unhandled,_PAP,_CHAP,_IPCP) ->
+ {fault}.
+
+%%% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+%%% gsn_addr_internal_storage/1
+%%% Convert GSN Address to internal datatype
+gsn_addr_internal_storage(<<IP_A:8,IP_B:8,IP_C:8,IP_D:8>>) ->
+ {ok,#mvsgT_ipAddress{version=ipv4,a1=IP_A,a2=IP_B,a3=IP_C,a4=IP_D,a5=0,a6=0,a7=0,a8=0}};
+gsn_addr_internal_storage(<<IP_A:16,IP_B:16,IP_C:16,IP_D:16,
+ IP_E:16,IP_F:16,IP_G:16,IP_H:16>>) ->
+ {ok,#mvsgT_ipAddress{version=ipv6,a1=IP_A,a2=IP_B,a3=IP_C,a4=IP_D,
+ a5=IP_E,a6=IP_F,a7=IP_G,a8=IP_H}};
+gsn_addr_internal_storage(_GSN_ADDR) ->
+ {fault}.
+
+%%% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+%%% msisdn_internal_storage/3
+%%% Convert MSISDN binary to internal datatype (TBCD-octet list)
+
+msisdn_internal_storage(<<>>,MSISDN) ->
+ {ok,#mvsT_msisdn{value=lists:reverse(MSISDN)}};
+msisdn_internal_storage(<<2#11111111:8,_Rest/binary>>,MSISDN) ->
+ {ok,#mvsT_msisdn{value=lists:reverse(MSISDN)}};
+msisdn_internal_storage(<<2#1111:4,DigitN:4,_Rest/binary>>,MSISDN) when
+ DigitN < 10 ->
+ {ok,#mvsT_msisdn{value=lists:reverse([(DigitN bor 2#11110000)|MSISDN])}};
+msisdn_internal_storage(<<DigitNplus1:4,DigitN:4,Rest/binary>>,MSISDN) when
+ DigitNplus1 < 10,
+ DigitN < 10 ->
+ NewMSISDN=[((DigitNplus1 bsl 4) bor DigitN)|MSISDN],
+ msisdn_internal_storage(Rest,NewMSISDN);
+msisdn_internal_storage(_Rest,_MSISDN) ->
+ {fault}. %% Mandatory IE incorrect
diff --git a/lib/hipe/test/bs_SUITE_data/bs_decode_extract.hrl b/lib/hipe/test/bs_SUITE_data/bs_decode_extract.hrl
new file mode 100644
index 0000000000..80add514a0
--- /dev/null
+++ b/lib/hipe/test/bs_SUITE_data/bs_decode_extract.hrl
@@ -0,0 +1,91 @@
+-ifndef(EXTDEC_HRL).
+-define(EXTDEC_HRL, true).
+
+-record(protocolErrors,{
+ invalidManIE=false,
+ outOfSequence=false,
+ incorrectOptIE=false}).
+-record(mvsT_msisdn, {value}).
+-record(mvsT_isdnAddress, {value}).
+-record(mvsT_hlrAddress, {value}).
+-record(mvsT_authenticationTriplet, {rand, sres, kc}).
+-record(mvsT_authenticationQuintuplet, {rand, xres, ck, ik, autn}).
+-record(mvsT_resynchInfo, {rand, auts}).
+-record(mvsT_resynch, {label, value}).
+-record(mvsT_storeImsiFault, {label, value}).
+-record(mvsT_additionalImsisResults, {roamingStatus, defaultApnOperatorId, misc1, misc2, misc3}).
+-record(mvsT_pdpActiveRecord, {contextId, nsapi, pdpTypeReq, pdpAddrReq, apnReq, qosReq, pdpTypeInUse, pdpAddressNature, pdpAddressInUse, apnInUse, ggsnAddrInUse, qosNegotiated}).
+-record(mvsgT_rai, {mcc, mnc, lac, rac}).
+-record(mvsgT_lai, {mcc, mnc, lac}).
+-record(mvsgT_errorInd, {dummyElement}).
+-record(mvsgT_deleteRes, {cause}).
+-record(mvsgT_deleteReq, {dummyElement}).
+-record(mvsgT_ptmsi, {value}).
+-record(mvsgT_ddRef, {cid, extId, validity}).
+-record(mvsgT_dpRef, {cid, devId}).
+-record(mvsgT_qualityOfService, {delayClass, relClass, peakThrput, precClass, meanThrput}).
+-record(mvsgT_pdpAddressType, {pdpTypeNbr, address}).
+-record(mvsgT_msNetworkCapability, {gea1, smCapDediccatedChannel, smCapGprsChannel, ucs2Support, ssScreenInd}).
+-record(mvsgT_cellId, {mcc, mnc, lac, rac, ci}).
+-record(mvsgT_ipAddress, {version, a1, a2, a3, a4, a5, a6, a7, a8}).
+-record(mvsgT_restartContextData, {gsn_address, restart_counter}).
+-record(mvsgT_updateRes, {cause, qos, ggsnAddSig, ggsnAddUser, recovery, flowLabDataI, flowLabSig, chargId, optFlags}).
+-record(mvsgT_updateReq, {qos, sgsnAddSig, sgsnAddUser, recovery, flowLabDataI, flowLabSig, otpFlags}).
+-record(mvsgT_imsi, {value}).
+-record(mvsgT_tid, {imsi, nsapi}).
+-record(mvsgT_extQualityOfService, {allocRetention, trfClass, delOrder, delOfErrSDU, maxSDUSize, maxBRUp, maxBRDown, residualBER, sduErrorRatio, transferDelay, traffHandlPrio, guarBRUp, guarBRDown}).
+-record(mvsgT_qualServ, {label, value}).
+-record(sesT_gnDevContextData, {numberOfContext, recoveryInfoArray}).
+-record(sesT_tid, {imsi, nsapi}).
+-record(sesT_gnDevContextDataInfo, {dummy}).
+-record(sesT_teid, {value}).
+-record(sesT_qualityOfServiceV1, {allocRetPriority, delayClass, reliabilityClass, peakThroughput, precedenceClass, meanThroughput, trafficClass, deliveryOrder, delivOfErrSDU, maxSDUsize, maxBrUp, maxBrDown, residualBER, sduErrorRatio, transferDelay, trafficHandlPrio, guaranteedBrUp, guaranteedBrDown}).
+-record(sesT_flowLbl, {value}).
+-record(sesT_qualityOfServiceV0, {delayClass, reliabilityClass, peakThroughput, precedenceClass, meanThroughput}).
+-record(sesT_createReq, {dummy}).
+-record(sesT_createRes, {dummy}).
+-record(sesT_deleteReq, {dummy}).
+-record(sesT_deleteRes, {dummy}).
+-record(sesT_gtid, {imsi, nsapi}).
+-record(sesT_updateReq, {dummy}).
+-record(sesT_updateRes, {dummy}).
+-record(sesT_gcontrolDataUs, {gtpSeqNr, gsnAddress, gtunnelId, gsnPort}).
+-record(sesT_gcontrolDataDs, {gtpSeqNr, gsnAddress, protocol, gtunnelId, flowLabSig, gsnPort}).
+-record(sesT_createResV1, {cause, teidSignalling, teidData, ggsnAddSig, ggsnAddUser, reorderingReq, recovery, chargId, endUserAdd, optFlags, protConOpt, qos}).
+-record(sesT_createReqV1, {qos, sgsnAddSig, sgsnAddUser, selMode, recovery, msisdn, endUserAdd, accPointName, optFlags, protConOpt, imsi, teidData, teidSignalling, nsapi}).
+-record(sesT_deleteReqV1, {teardownInd, nsapi}).
+-record(sesT_deleteResV1, {cause}).
+-record(sesT_updateReqV1, {imsi, recovery, teidData, teidSignalling, nsapi, sgsnAddSig, sgsnAddUser, qos}).
+-record(sesT_updateResV1, {cause, recovery, teidData, teidSignalling, chargId, ggsnAddSig, ggsnAddUser, qos}).
+-record(sesT_deleteReqV0, {tid}).
+-record(sesT_deleteResV0, {tid, cause}).
+-record(sesT_createReqV0, {tid, tidRaw, qos, recovery, selMode, flowLblData, flowLblSig, endUserAdd, accPointName, protConOpt, sgsnAddSig, sgsnAddUser, msisdn}).
+-record(sesT_createResV0, {tid, cause, qos, reorderingReq, recovery, flowLblData, flowLblSig, chargId, endUserAdd, protConOpt, ggsnAddSig, ggsnAddUser}).
+-record(sesT_updateReqV0, {tid, tidRaw, qos, recovery, flowLblData, flowLblSig, sgsnAddSig, sgsnAddUser}).
+-record(sesT_updateResV0, {tid, cause, qos, recovery, flowLblData, flowLblSig, chargId, ggsnAddSig, ggsnAddUser}).
+-record(sesT_echoReq, {dummy}).
+-record(sesT_echoRes, {dummy}).
+-record(sesT_echoReqV1, {dummy}).
+-record(sesT_echoResV1, {recovery}).
+-record(sesT_echoReqV0, {dummy}).
+-record(sesT_echoResV0, {recovery}).
+-record(masT_apnSecurity, {sgsnSel, subscribedSel, userSel, ipSpoofing}).
+-record(masT_radiusServer, {radiusApn, radiusAddress, radiusMepAddress, timer, tries, secret}).
+-record(masT_ipSegment, {startSegAddress, stopSegAddress, netmask}).
+-record(masT_llf, {name, metric, id}).
+-record(masT_apnLink, {ggsnAddress, ipSegList, ipAddressOrigin, llfConnName, mepAddress}).
+-record(masT_ispSubObj, {label, value}).
+-record(masT_ipcpData, {type, ipAddress, rawMessage}).
+-record(masT_ipcp, {exists, code, id, ipcpList}).
+-record(masT_pap, {exists, code, id, username, password}).
+-record(masT_chap, {code, id, value, name}).
+-record(masT_ispDevContextData, {nsapi, ipAddress, apnhandle}).
+-record(masT_protocolConfigOptions, {chap, pap, ipcp}).
+-record(masT_apnRadius, {radiusAddress, timer, tries, secret}).
+-record(masT_outbandRadius, {gwAddress, llfConnName, primRadius, secRadius}).
+-record(masT_radiusPair, {primRadius, secRadius}).
+-record(masT_radiusOpt, {dummyMsisdnAuth, dummyMsisdnAcct, msisdnInAuth, msisdnInAcct, sendFullImsi, sendMccMnc, sendSelMode, sendChargingId, asynchAcct}).
+-record(masT_radiusConfig, {hostApn, authPair, acctList, radiusOptions}).
+-record(masT_apnConfig, {link, security, radiusConfig, primDns, secDns, dhcpAddress, indAcct, indAuth, userNameBasedSelection}).
+
+-endif.
diff --git a/lib/hipe/test/bs_SUITE_data/bs_des.erl b/lib/hipe/test/bs_SUITE_data/bs_des.erl
new file mode 100644
index 0000000000..9c495d37ad
--- /dev/null
+++ b/lib/hipe/test/bs_SUITE_data/bs_des.erl
@@ -0,0 +1,734 @@
+%%% -*- erlang-indent-level: 2 -*-
+%%%-------------------------------------------------------------------
+%%% File : bs_des.erl
+%%% Author : Per Gustafsson <[email protected]>
+%%% Purpose : An implementation of the DES Encryption/Descryption
+%%% algorithm using Erlang binaries.
+%%%
+%%% Created : 14 Apr 2004
+%%%-------------------------------------------------------------------
+-module(bs_des).
+
+-export([encode/2, decode/2, test/0]).
+
+-define(ITERATIONS, 42). %% for benchmarking use a higher number
+
+test() ->
+ Bin = <<1:64>>,
+ Size= byte_size(Bin),
+ Key = <<4704650607608769871263876:64>>,
+ Jumbled = run_encode(?ITERATIONS, Bin, Key),
+ Unjumbled = run_decode(?ITERATIONS, Jumbled, Key),
+ <<Bin:Size/binary,_/binary>> = Unjumbled,
+ ok.
+
+run_encode(1, Bin, Key) ->
+ encode(Bin, Key);
+run_encode(N, Bin, Key) ->
+ encode(Bin, Key),
+ run_encode(N-1, Bin, Key).
+
+run_decode(1, Bin, Key) ->
+ decode(Bin, Key);
+run_decode(N, Bin, Key) ->
+ decode(Bin, Key),
+ run_decode(N-1, Bin, Key).
+
+encode(Data, Key) ->
+ Keys = schedule(Key),
+ list_to_binary(encode_data(Data, Keys)).
+
+decode(Data, Key) ->
+ Keys = lists:reverse(schedule(Key)),
+ list_to_binary(decode_data(Data, Keys)).
+
+encode_data(<<Data:8/binary, Rest/binary>>, Keys) ->
+ [ipinv(des_core(ip(Data), Keys))|encode_data(Rest, Keys)];
+encode_data(<<Rest/binary>>, Keys) ->
+ case byte_size(Rest) of
+ 0 -> [];
+ X ->
+ Y = 8 - X,
+ Data = <<Rest/binary, 0:Y/integer-unit:8>>,
+ [ipinv(des_core(ip(Data), Keys))]
+ end.
+
+decode_data(<<Data:8/binary, Rest/binary>>, Keys) ->
+ [ipinv(dechiper(ip(Data), Keys))|decode_data(Rest, Keys)];
+decode_data(_, _Keys) ->
+ [].
+
+schedule(Key) ->
+ NewKey = pc1(Key),
+ subkeys(NewKey, 1).
+
+subkeys(_Key, 17) ->
+ [];
+subkeys(Key, N) ->
+ TmpKey =
+ case rotate(N) of
+ 1 ->
+ <<X1:1, L:27, X2:1, R:27>> = Key,
+ <<L:27, X1:1, R:27, X2:1>>;
+ 2 ->
+ <<X1:2, L:26, X2:2, R:26>> = Key,
+ <<L:26, X1:2, R:26, X2:2>>
+ end,
+ [pc2(TmpKey)|subkeys(TmpKey, N+1)].
+
+pc2(<<I1:1, I2:1, I3:1, I4:1, I5:1, I6:1, I7:1, I8:1,
+ _I9:1, I10:1, I11:1, I12:1, I13:1, I14:1, I15:1, I16:1,
+ I17:1, _I18:1, I19:1, I20:1, I21:1, _I22:1, I23:1, I24:1,
+ _I25:1, I26:1, I27:1, I28:1, I29:1, I30:1, I31:1, I32:1,
+ I33:1, I34:1, _I35:1, I36:1, I37:1, _I38:1, I39:1, I40:1,
+ I41:1, I42:1, _I43:1, I44:1, I45:1, I46:1, I47:1, I48:1,
+ I49:1, I50:1, I51:1, I52:1, I53:1, _I54:1, I55:1, I56:1>>) ->
+ <<I14:1, I17:1, I11:1, I24:1, I1:1, I5:1, I3:1, I28:1,
+ I15:1, I6:1, I21:1, I10:1, I23:1, I19:1, I12:1, I4:1,
+ I26:1, I8:1, I16:1, I7:1, I27:1, I20:1, I13:1, I2:1,
+ I41:1, I52:1, I31:1, I37:1, I47:1, I55:1, I30:1, I40:1,
+ I51:1, I45:1, I33:1, I48:1, I44:1, I49:1, I39:1, I56:1,
+ I34:1, I53:1, I46:1, I42:1, I50:1, I36:1, I29:1, I32:1>>.
+
+pc1(<<I1:1, I2:1, I3:1, I4:1, I5:1, I6:1, I7:1, _:1,
+ I9:1, I10:1, I11:1, I12:1, I13:1, I14:1, I15:1, _:1,
+ I17:1, I18:1, I19:1, I20:1, I21:1, I22:1, I23:1, _:1,
+ I25:1, I26:1, I27:1, I28:1, I29:1, I30:1, I31:1, _:1,
+ I33:1, I34:1, I35:1, I36:1, I37:1, I38:1, I39:1, _:1,
+ I41:1, I42:1, I43:1, I44:1, I45:1, I46:1, I47:1, _:1,
+ I49:1, I50:1, I51:1, I52:1, I53:1, I54:1, I55:1, _:1,
+ I57:1, I58:1, I59:1, I60:1, I61:1, I62:1, I63:1, _:1>>) ->
+ <<I57:1, I49:1, I41:1, I33:1, I25:1, I17:1, I9:1, I1:1,
+ I58:1, I50:1, I42:1, I34:1, I26:1, I18:1, I10:1, I2:1,
+ I59:1, I51:1, I43:1, I35:1, I27:1, I19:1, I11:1, I3:1,
+ I60:1, I52:1, I44:1, I36:1, I63:1, I55:1, I47:1, I39:1,
+ I31:1, I23:1, I15:1, I7:1, I62:1, I54:1, I46:1, I38:1,
+ I30:1, I22:1, I14:1, I6:1, I61:1, I53:1, I45:1, I37:1,
+ I29:1, I21:1, I13:1, I5:1, I28:1, I20:1, I12:1, I4:1>>.
+
+ip(<<I1:1, I2:1, I3:1, I4:1, I5:1, I6:1, I7:1, I8:1,
+ I9:1, I10:1, I11:1, I12:1, I13:1, I14:1, I15:1, I16:1,
+ I17:1, I18:1, I19:1, I20:1, I21:1, I22:1, I23:1, I24:1,
+ I25:1, I26:1, I27:1, I28:1, I29:1, I30:1, I31:1, I32:1,
+ I33:1, I34:1, I35:1, I36:1, I37:1, I38:1, I39:1, I40:1,
+ I41:1, I42:1, I43:1, I44:1, I45:1, I46:1, I47:1, I48:1,
+ I49:1, I50:1, I51:1, I52:1, I53:1, I54:1, I55:1, I56:1,
+ I57:1, I58:1, I59:1, I60:1, I61:1, I62:1, I63:1, I64:1>>) ->
+ <<I58:1, I50:1, I42:1, I34:1, I26:1, I18:1, I10:1, I2:1,
+ I60:1, I52:1, I44:1, I36:1, I28:1, I20:1, I12:1, I4:1,
+ I62:1, I54:1, I46:1, I38:1, I30:1, I22:1, I14:1, I6:1,
+ I64:1, I56:1, I48:1, I40:1, I32:1, I24:1, I16:1, I8:1,
+ I57:1, I49:1, I41:1, I33:1, I25:1, I17:1, I9:1, I1:1,
+ I59:1, I51:1, I43:1, I35:1, I27:1, I19:1, I11:1, I3:1,
+ I61:1, I53:1, I45:1, I37:1, I29:1, I21:1, I13:1, I5:1,
+ I63:1, I55:1, I47:1, I39:1, I31:1, I23:1, I15:1, I7:1>>.
+
+ipinv(<<I58:1, I50:1, I42:1, I34:1, I26:1, I18:1, I10:1, I2:1,
+ I60:1, I52:1, I44:1, I36:1, I28:1, I20:1, I12:1, I4:1,
+ I62:1, I54:1, I46:1, I38:1, I30:1, I22:1, I14:1, I6:1,
+ I64:1, I56:1, I48:1, I40:1, I32:1, I24:1, I16:1, I8:1,
+ I57:1, I49:1, I41:1, I33:1, I25:1, I17:1, I9:1, I1:1,
+ I59:1, I51:1, I43:1, I35:1, I27:1, I19:1, I11:1, I3:1,
+ I61:1, I53:1, I45:1, I37:1, I29:1, I21:1, I13:1, I5:1,
+ I63:1, I55:1, I47:1, I39:1, I31:1, I23:1, I15:1, I7:1>>) ->
+ <<I1:1, I2:1, I3:1, I4:1, I5:1, I6:1, I7:1, I8:1,
+ I9:1, I10:1, I11:1, I12:1, I13:1, I14:1, I15:1, I16:1,
+ I17:1, I18:1, I19:1, I20:1, I21:1, I22:1, I23:1, I24:1,
+ I25:1, I26:1, I27:1, I28:1, I29:1, I30:1, I31:1, I32:1,
+ I33:1, I34:1, I35:1, I36:1, I37:1, I38:1, I39:1, I40:1,
+ I41:1, I42:1, I43:1, I44:1, I45:1, I46:1, I47:1, I48:1,
+ I49:1, I50:1, I51:1, I52:1, I53:1, I54:1, I55:1, I56:1,
+ I57:1, I58:1, I59:1, I60:1, I61:1, I62:1, I63:1, I64:1>>.
+
+dechiper(<<L:4/binary, R:4/binary>>, Keys) ->
+ dechiper(L, R, Keys, 16).
+
+dechiper(L, R, [], 0) ->
+ <<L:4/binary, R:4/binary>>;
+dechiper(L, R, [Key|Rest], I) ->
+ NewL = ebit(L),
+ XorL = xor48(NewL, Key),
+ Sboxed = sboxing(XorL),
+ Ped = p(Sboxed),
+ EndL = xor32(Ped, R),
+ dechiper(EndL, L, Rest, I-1).
+
+des_core(<<L:4/binary, R:4/binary>>, Keys) ->
+ des_core(L, R, Keys, 0).
+
+des_core(L, R, [], 16) ->
+ <<L:4/binary, R:4/binary>>;
+des_core(L, R, [Key|Rest], I) when I<16 ->
+ NewR = ebit(R),
+ XorR = xor48(NewR, Key),
+ Sboxed = sboxing(XorR),
+ Ped = p(Sboxed),
+ EndR = xor32(Ped, L),
+ des_core(R, EndR, Rest, I+1).
+
+ebit(<<I1:1, I2:2, I3:2,I4:2,I5:2,I6:2,
+ I7:2,I8:2,I9:2,I10:2,I11:2,I12:2,
+ I13:2,I14:2,I15:2,I16:2,I17:1>>) ->
+ <<I17:1, I1:1, I2:2, I3:2, I3:2,
+ I4:2, I5:2, I5:2, I6:2,
+ I7:2, I7:2, I8:2, I9:2,
+ I9:2, I10:2, I11:2, I11:2,
+ I12:2, I13:2, I13:2, I14:2,
+ I15:2, I15:2, I16:2, I17:1, I1:1>>.
+
+p(<<I1:1, I2:1, I3:1, I4:1, I5:1, I6:1, I7:1, I8:1,
+ I9:1, I10:1, I11:1, I12:1, I13:1, I14:1, I15:1, I16:1,
+ I17:1, I18:1, I19:1, I20:1, I21:1, I22:1, I23:1, I24:1,
+ I25:1, I26:1, I27:1, I28:1, I29:1, I30:1, I31:1, I32:1>>) ->
+ <<I16:1, I7:1, I20:1, I21:1, I29:1, I12:1, I28:1, I17:1,
+ I1:1, I15:1, I23:1, I26:1, I5:1, I18:1, I31:1, I10:1,
+ I2:1, I8:1, I24:1, I14:1, I32:1, I27:1, I3:1, I9:1,
+ I19:1, I13:1, I30:1, I6:1, I22:1, I11:1, I4:1, I25:1>>.
+
+rotate(1) -> 1;
+rotate(2) -> 1;
+rotate(9) -> 1;
+rotate(16) -> 1;
+rotate(N) when N>0, N<17 -> 2.
+
+%% xor64(<<I1:16, I2:16, I3:16, I4:16>>,<<J1:16, J2:16, J3:16, J4:16>>) ->
+%% K1 = I1 bxor J1,
+%% K2 = I2 bxor J2,
+%% K3 = I3 bxor J3,
+%% K4 = I4 bxor J4,
+%% <<K1:16, K2:16, K3:16, K4:16>>.
+
+xor48(<<I1:16, I2:16, I3:16>>,<<J1:16, J2:16, J3:16>>) ->
+ K1 = I1 bxor J1,
+ K2 = I2 bxor J2,
+ K3 = I3 bxor J3,
+ <<K1:16, K2:16, K3:16>>.
+
+xor32(<<I1:16, I2:16>>,<<J1:16, J2:16>>) ->
+ K1 = I1 bxor J1,
+ K2 = I2 bxor J2,
+ <<K1:16, K2:16>>.
+
+sboxing(<<A1:6, A2:6, A3:6, A4:6, A5:6, A6:6, A7:6, A8:6>>) ->
+ S1 = sbox(A1, 1),
+ S2 = sbox(A2, 2),
+ S3 = sbox(A3, 3),
+ S4 = sbox(A4, 4),
+ S5 = sbox(A5, 5),
+ S6 = sbox(A6, 6),
+ S7 = sbox(A7, 7),
+ S8 = sbox(A8, 8),
+ <<S1:4,S2:4,S3:4,S4:4,S5:4,S6:4,S7:4,S8:4>>.
+
+sbox(0,1) -> 14;
+sbox(1,1) -> 0;
+sbox(2,1) -> 4;
+sbox(3,1) -> 15;
+sbox(4,1) -> 13;
+sbox(5,1) -> 7;
+sbox(6,1) -> 1;
+sbox(7,1) -> 4;
+sbox(8,1) -> 2;
+sbox(9,1) -> 14;
+sbox(10,1) -> 15;
+sbox(11,1) -> 2;
+sbox(12,1) -> 11;
+sbox(13,1) -> 13;
+sbox(14,1) -> 8;
+sbox(15,1) -> 1;
+sbox(16,1) -> 3;
+sbox(17,1) -> 10;
+sbox(18,1) -> 10;
+sbox(19,1) -> 6;
+sbox(20,1) -> 6;
+sbox(21,1) -> 12;
+sbox(22,1) -> 12;
+sbox(23,1) -> 11;
+sbox(24,1) -> 5;
+sbox(25,1) -> 9;
+sbox(26,1) -> 9;
+sbox(27,1) -> 5;
+sbox(28,1) -> 0;
+sbox(29,1) -> 3;
+sbox(30,1) -> 7;
+sbox(31,1) -> 8;
+sbox(32,1) -> 4;
+sbox(33,1) -> 15;
+sbox(34,1) -> 1;
+sbox(35,1) -> 12;
+sbox(36,1) -> 14;
+sbox(37,1) -> 8;
+sbox(38,1) -> 8;
+sbox(39,1) -> 2;
+sbox(40,1) -> 13;
+sbox(41,1) -> 4;
+sbox(42,1) -> 6;
+sbox(43,1) -> 9;
+sbox(44,1) -> 2;
+sbox(45,1) -> 1;
+sbox(46,1) -> 11;
+sbox(47,1) -> 7;
+sbox(48,1) -> 15;
+sbox(49,1) -> 5;
+sbox(50,1) -> 12;
+sbox(51,1) -> 11;
+sbox(52,1) -> 9;
+sbox(53,1) -> 3;
+sbox(54,1) -> 7;
+sbox(55,1) -> 14;
+sbox(56,1) -> 3;
+sbox(57,1) -> 10;
+sbox(58,1) -> 10;
+sbox(59,1) -> 0;
+sbox(60,1) -> 5;
+sbox(61,1) -> 6;
+sbox(62,1) -> 0;
+sbox(63,1) -> 13;
+sbox(0,2) -> 15;
+sbox(1,2) -> 3;
+sbox(2,2) -> 1;
+sbox(3,2) -> 13;
+sbox(4,2) -> 8;
+sbox(5,2) -> 4;
+sbox(6,2) -> 14;
+sbox(7,2) -> 7;
+sbox(8,2) -> 6;
+sbox(9,2) -> 15;
+sbox(10,2) -> 11;
+sbox(11,2) -> 2;
+sbox(12,2) -> 3;
+sbox(13,2) -> 8;
+sbox(14,2) -> 4;
+sbox(15,2) -> 14;
+sbox(16,2) -> 9;
+sbox(17,2) -> 12;
+sbox(18,2) -> 7;
+sbox(19,2) -> 0;
+sbox(20,2) -> 2;
+sbox(21,2) -> 1;
+sbox(22,2) -> 13;
+sbox(23,2) -> 10;
+sbox(24,2) -> 12;
+sbox(25,2) -> 6;
+sbox(26,2) -> 0;
+sbox(27,2) -> 9;
+sbox(28,2) -> 5;
+sbox(29,2) -> 11;
+sbox(30,2) -> 10;
+sbox(31,2) -> 5;
+sbox(32,2) -> 0;
+sbox(33,2) -> 13;
+sbox(34,2) -> 14;
+sbox(35,2) -> 8;
+sbox(36,2) -> 7;
+sbox(37,2) -> 10;
+sbox(38,2) -> 11;
+sbox(39,2) -> 1;
+sbox(40,2) -> 10;
+sbox(41,2) -> 3;
+sbox(42,2) -> 4;
+sbox(43,2) -> 15;
+sbox(44,2) -> 13;
+sbox(45,2) -> 4;
+sbox(46,2) -> 1;
+sbox(47,2) -> 2;
+sbox(48,2) -> 5;
+sbox(49,2) -> 11;
+sbox(50,2) -> 8;
+sbox(51,2) -> 6;
+sbox(52,2) -> 12;
+sbox(53,2) -> 7;
+sbox(54,2) -> 6;
+sbox(55,2) -> 12;
+sbox(56,2) -> 9;
+sbox(57,2) -> 0;
+sbox(58,2) -> 3;
+sbox(59,2) -> 5;
+sbox(60,2) -> 2;
+sbox(61,2) -> 14;
+sbox(62,2) -> 15;
+sbox(63,2) -> 9;
+sbox(0,3) -> 10;
+sbox(1,3) -> 13;
+sbox(2,3) -> 0;
+sbox(3,3) -> 7;
+sbox(4,3) -> 9;
+sbox(5,3) -> 0;
+sbox(6,3) -> 14;
+sbox(7,3) -> 9;
+sbox(8,3) -> 6;
+sbox(9,3) -> 3;
+sbox(10,3) -> 3;
+sbox(11,3) -> 4;
+sbox(12,3) -> 15;
+sbox(13,3) -> 6;
+sbox(14,3) -> 5;
+sbox(15,3) -> 10;
+sbox(16,3) -> 1;
+sbox(17,3) -> 2;
+sbox(18,3) -> 13;
+sbox(19,3) -> 8;
+sbox(20,3) -> 12;
+sbox(21,3) -> 5;
+sbox(22,3) -> 7;
+sbox(23,3) -> 14;
+sbox(24,3) -> 11;
+sbox(25,3) -> 12;
+sbox(26,3) -> 4;
+sbox(27,3) -> 11;
+sbox(28,3) -> 2;
+sbox(29,3) -> 15;
+sbox(30,3) -> 8;
+sbox(31,3) -> 1;
+sbox(32,3) -> 13;
+sbox(33,3) -> 1;
+sbox(34,3) -> 6;
+sbox(35,3) -> 10;
+sbox(36,3) -> 4;
+sbox(37,3) -> 13;
+sbox(38,3) -> 9;
+sbox(39,3) -> 0;
+sbox(40,3) -> 8;
+sbox(41,3) -> 6;
+sbox(42,3) -> 15;
+sbox(43,3) -> 9;
+sbox(44,3) -> 3;
+sbox(45,3) -> 8;
+sbox(46,3) -> 0;
+sbox(47,3) -> 7;
+sbox(48,3) -> 11;
+sbox(49,3) -> 4;
+sbox(50,3) -> 1;
+sbox(51,3) -> 15;
+sbox(52,3) -> 2;
+sbox(53,3) -> 14;
+sbox(54,3) -> 12;
+sbox(55,3) -> 3;
+sbox(56,3) -> 5;
+sbox(57,3) -> 11;
+sbox(58,3) -> 10;
+sbox(59,3) -> 5;
+sbox(60,3) -> 14;
+sbox(61,3) -> 2;
+sbox(62,3) -> 7;
+sbox(63,3) -> 12;
+sbox(0,4) -> 7;
+sbox(1,4) -> 13;
+sbox(2,4) -> 13;
+sbox(3,4) -> 8;
+sbox(4,4) -> 14;
+sbox(5,4) -> 11;
+sbox(6,4) -> 3;
+sbox(7,4) -> 5;
+sbox(8,4) -> 0;
+sbox(9,4) -> 6;
+sbox(10,4) -> 6;
+sbox(11,4) -> 15;
+sbox(12,4) -> 9;
+sbox(13,4) -> 0;
+sbox(14,4) -> 10;
+sbox(15,4) -> 3;
+sbox(16,4) -> 1;
+sbox(17,4) -> 4;
+sbox(18,4) -> 2;
+sbox(19,4) -> 7;
+sbox(20,4) -> 8;
+sbox(21,4) -> 2;
+sbox(22,4) -> 5;
+sbox(23,4) -> 12;
+sbox(24,4) -> 11;
+sbox(25,4) -> 1;
+sbox(26,4) -> 12;
+sbox(27,4) -> 10;
+sbox(28,4) -> 4;
+sbox(29,4) -> 14;
+sbox(30,4) -> 15;
+sbox(31,4) -> 9;
+sbox(32,4) -> 10;
+sbox(33,4) -> 3;
+sbox(34,4) -> 6;
+sbox(35,4) -> 15;
+sbox(36,4) -> 9;
+sbox(37,4) -> 0;
+sbox(38,4) -> 0;
+sbox(39,4) -> 6;
+sbox(40,4) -> 12;
+sbox(41,4) -> 10;
+sbox(42,4) -> 11;
+sbox(43,4) -> 1;
+sbox(44,4) -> 7;
+sbox(45,4) -> 13;
+sbox(46,4) -> 13;
+sbox(47,4) -> 8;
+sbox(48,4) -> 15;
+sbox(49,4) -> 9;
+sbox(50,4) -> 1;
+sbox(51,4) -> 4;
+sbox(52,4) -> 3;
+sbox(53,4) -> 5;
+sbox(54,4) -> 14;
+sbox(55,4) -> 11;
+sbox(56,4) -> 5;
+sbox(57,4) -> 12;
+sbox(58,4) -> 2;
+sbox(59,4) -> 7;
+sbox(60,4) -> 8;
+sbox(61,4) -> 2;
+sbox(62,4) -> 4;
+sbox(63,4) -> 14;
+sbox(0,5) -> 2;
+sbox(1,5) -> 14;
+sbox(2,5) -> 12;
+sbox(3,5) -> 11;
+sbox(4,5) -> 4;
+sbox(5,5) -> 2;
+sbox(6,5) -> 1;
+sbox(7,5) -> 12;
+sbox(8,5) -> 7;
+sbox(9,5) -> 4;
+sbox(10,5) -> 10;
+sbox(11,5) -> 7;
+sbox(12,5) -> 11;
+sbox(13,5) -> 13;
+sbox(14,5) -> 6;
+sbox(15,5) -> 1;
+sbox(16,5) -> 8;
+sbox(17,5) -> 5;
+sbox(18,5) -> 5;
+sbox(19,5) -> 0;
+sbox(20,5) -> 3;
+sbox(21,5) -> 15;
+sbox(22,5) -> 15;
+sbox(23,5) -> 10;
+sbox(24,5) -> 13;
+sbox(25,5) -> 3;
+sbox(26,5) -> 0;
+sbox(27,5) -> 9;
+sbox(28,5) -> 14;
+sbox(29,5) -> 8;
+sbox(30,5) -> 9;
+sbox(31,5) -> 6;
+sbox(32,5) -> 4;
+sbox(33,5) -> 11;
+sbox(34,5) -> 2;
+sbox(35,5) -> 8;
+sbox(36,5) -> 1;
+sbox(37,5) -> 12;
+sbox(38,5) -> 11;
+sbox(39,5) -> 7;
+sbox(40,5) -> 10;
+sbox(41,5) -> 1;
+sbox(42,5) -> 13;
+sbox(43,5) -> 14;
+sbox(44,5) -> 7;
+sbox(45,5) -> 2;
+sbox(46,5) -> 8;
+sbox(47,5) -> 13;
+sbox(48,5) -> 15;
+sbox(49,5) -> 6;
+sbox(50,5) -> 9;
+sbox(51,5) -> 15;
+sbox(52,5) -> 12;
+sbox(53,5) -> 0;
+sbox(54,5) -> 5;
+sbox(55,5) -> 9;
+sbox(56,5) -> 6;
+sbox(57,5) -> 10;
+sbox(58,5) -> 3;
+sbox(59,5) -> 4;
+sbox(60,5) -> 0;
+sbox(61,5) -> 5;
+sbox(62,5) -> 14;
+sbox(63,5) -> 3;
+sbox(0,6) -> 12;
+sbox(1,6) -> 10;
+sbox(2,6) -> 1;
+sbox(3,6) -> 15;
+sbox(4,6) -> 10;
+sbox(5,6) -> 4;
+sbox(6,6) -> 15;
+sbox(7,6) -> 2;
+sbox(8,6) -> 9;
+sbox(9,6) -> 7;
+sbox(10,6) -> 2;
+sbox(11,6) -> 12;
+sbox(12,6) -> 6;
+sbox(13,6) -> 9;
+sbox(14,6) -> 8;
+sbox(15,6) -> 5;
+sbox(16,6) -> 0;
+sbox(17,6) -> 6;
+sbox(18,6) -> 13;
+sbox(19,6) -> 1;
+sbox(20,6) -> 3;
+sbox(21,6) -> 13;
+sbox(22,6) -> 4;
+sbox(23,6) -> 14;
+sbox(24,6) -> 14;
+sbox(25,6) -> 0;
+sbox(26,6) -> 7;
+sbox(27,6) -> 11;
+sbox(28,6) -> 5;
+sbox(29,6) -> 3;
+sbox(30,6) -> 11;
+sbox(31,6) -> 8;
+sbox(32,6) -> 9;
+sbox(33,6) -> 4;
+sbox(34,6) -> 14;
+sbox(35,6) -> 3;
+sbox(36,6) -> 15;
+sbox(37,6) -> 2;
+sbox(38,6) -> 5;
+sbox(39,6) -> 12;
+sbox(40,6) -> 2;
+sbox(41,6) -> 9;
+sbox(42,6) -> 8;
+sbox(43,6) -> 5;
+sbox(44,6) -> 12;
+sbox(45,6) -> 15;
+sbox(46,6) -> 3;
+sbox(47,6) -> 10;
+sbox(48,6) -> 7;
+sbox(49,6) -> 11;
+sbox(50,6) -> 0;
+sbox(51,6) -> 14;
+sbox(52,6) -> 4;
+sbox(53,6) -> 1;
+sbox(54,6) -> 10;
+sbox(55,6) -> 7;
+sbox(56,6) -> 1;
+sbox(57,6) -> 6;
+sbox(58,6) -> 13;
+sbox(59,6) -> 0;
+sbox(60,6) -> 11;
+sbox(61,6) -> 8;
+sbox(62,6) -> 6;
+sbox(63,6) -> 13;
+sbox(0,7) -> 4;
+sbox(1,7) -> 13;
+sbox(2,7) -> 11;
+sbox(3,7) -> 0;
+sbox(4,7) -> 2;
+sbox(5,7) -> 11;
+sbox(6,7) -> 14;
+sbox(7,7) -> 7;
+sbox(8,7) -> 15;
+sbox(9,7) -> 4;
+sbox(10,7) -> 0;
+sbox(11,7) -> 9;
+sbox(12,7) -> 8;
+sbox(13,7) -> 1;
+sbox(14,7) -> 13;
+sbox(15,7) -> 10;
+sbox(16,7) -> 3;
+sbox(17,7) -> 14;
+sbox(18,7) -> 12;
+sbox(19,7) -> 3;
+sbox(20,7) -> 9;
+sbox(21,7) -> 5;
+sbox(22,7) -> 7;
+sbox(23,7) -> 12;
+sbox(24,7) -> 5;
+sbox(25,7) -> 2;
+sbox(26,7) -> 10;
+sbox(27,7) -> 15;
+sbox(28,7) -> 6;
+sbox(29,7) -> 8;
+sbox(30,7) -> 1;
+sbox(31,7) -> 6;
+sbox(32,7) -> 1;
+sbox(33,7) -> 6;
+sbox(34,7) -> 4;
+sbox(35,7) -> 11;
+sbox(36,7) -> 11;
+sbox(37,7) -> 13;
+sbox(38,7) -> 13;
+sbox(39,7) -> 8;
+sbox(40,7) -> 12;
+sbox(41,7) -> 1;
+sbox(42,7) -> 3;
+sbox(43,7) -> 4;
+sbox(44,7) -> 7;
+sbox(45,7) -> 10;
+sbox(46,7) -> 14;
+sbox(47,7) -> 7;
+sbox(48,7) -> 10;
+sbox(49,7) -> 9;
+sbox(50,7) -> 15;
+sbox(51,7) -> 5;
+sbox(52,7) -> 6;
+sbox(53,7) -> 0;
+sbox(54,7) -> 8;
+sbox(55,7) -> 15;
+sbox(56,7) -> 0;
+sbox(57,7) -> 14;
+sbox(58,7) -> 5;
+sbox(59,7) -> 2;
+sbox(60,7) -> 9;
+sbox(61,7) -> 3;
+sbox(62,7) -> 2;
+sbox(63,7) -> 12;
+sbox(0,8) -> 13;
+sbox(1,8) -> 1;
+sbox(2,8) -> 2;
+sbox(3,8) -> 15;
+sbox(4,8) -> 8;
+sbox(5,8) -> 13;
+sbox(6,8) -> 4;
+sbox(7,8) -> 8;
+sbox(8,8) -> 6;
+sbox(9,8) -> 10;
+sbox(10,8) -> 15;
+sbox(11,8) -> 3;
+sbox(12,8) -> 11;
+sbox(13,8) -> 7;
+sbox(14,8) -> 1;
+sbox(15,8) -> 4;
+sbox(16,8) -> 10;
+sbox(17,8) -> 12;
+sbox(18,8) -> 9;
+sbox(19,8) -> 5;
+sbox(20,8) -> 3;
+sbox(21,8) -> 6;
+sbox(22,8) -> 14;
+sbox(23,8) -> 11;
+sbox(24,8) -> 5;
+sbox(25,8) -> 0;
+sbox(26,8) -> 0;
+sbox(27,8) -> 14;
+sbox(28,8) -> 12;
+sbox(29,8) -> 9;
+sbox(30,8) -> 7;
+sbox(31,8) -> 2;
+sbox(32,8) -> 7;
+sbox(33,8) -> 2;
+sbox(34,8) -> 11;
+sbox(35,8) -> 1;
+sbox(36,8) -> 4;
+sbox(37,8) -> 14;
+sbox(38,8) -> 1;
+sbox(39,8) -> 7;
+sbox(40,8) -> 9;
+sbox(41,8) -> 4;
+sbox(42,8) -> 12;
+sbox(43,8) -> 10;
+sbox(44,8) -> 14;
+sbox(45,8) -> 8;
+sbox(46,8) -> 2;
+sbox(47,8) -> 13;
+sbox(48,8) -> 0;
+sbox(49,8) -> 15;
+sbox(50,8) -> 6;
+sbox(51,8) -> 12;
+sbox(52,8) -> 10;
+sbox(53,8) -> 9;
+sbox(54,8) -> 13;
+sbox(55,8) -> 0;
+sbox(56,8) -> 15;
+sbox(57,8) -> 3;
+sbox(58,8) -> 3;
+sbox(59,8) -> 5;
+sbox(60,8) -> 5;
+sbox(61,8) -> 6;
+sbox(62,8) -> 8;
+sbox(63,8) -> 11.
diff --git a/lib/hipe/test/bs_SUITE_data/bs_extract.erl b/lib/hipe/test/bs_SUITE_data/bs_extract.erl
new file mode 100644
index 0000000000..0492689fa8
--- /dev/null
+++ b/lib/hipe/test/bs_SUITE_data/bs_extract.erl
@@ -0,0 +1,94 @@
+%% -*- erlang-indent-level: 2 -*-
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%%
+%% Among testing other things, this module shows why performing LCM on
+%% SPARC is currently problematic. SPARC does not mark untagged values
+%% as dead when they are live over function calls which in turn causes
+%% them to be traced by the garbage collector leading to crashes.
+%%
+%% A simple way to get this behaviour is to compile just the function
+%%
+%% {bsextract,tid_internal_storage,2}
+%%
+%% with the compiler option "rtl_lcm" on and without.
+%%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+-module(bs_extract).
+
+-export([test/0]).
+
+-include("bs_decode_extract.hrl").
+
+-define(PDU, <<30,16,0,90,0,1,0,0,255,255,255,255,81,67,101,7,0,0,0,96,
+ 6,12,146,18,14,0,15,252,16,0,0,17,0,0,128,0,2,241,33,131,
+ 0,20,7,97,112,110,48,49,51,97,8,101,114,105,99,115,115,
+ 111,110,2,115,101,132,0,20,128,192,35,16,1,5,0,16,5,117,
+ 115,101,114,53,5,112,97,115,115,53,133,0,4,172,28,12,1,
+ 133,0,4,172,28,12,3,134,0,8,145,148,113,129,0,0,0,0>>).
+
+-define(RES, {ok, {mvsgT_imsi, <<81,67,101,7,0,0,0,240>>}}).
+
+test() ->
+ ?RES = extract_v0_opt(1000, ?PDU),
+ ok.
+
+extract_v0_opt(0, Pdu) ->
+ get_external_id(Pdu);
+extract_v0_opt(N, Pdu) ->
+ {ok,_} = get_external_id(Pdu),
+ extract_v0_opt(N-1, Pdu).
+
+get_external_id(<<0:3,_:4,0:1,1:8,_Length:16,SequenceNumber:16,
+ _FlowLabel:16,_SNDCP_N_PDU_Number:8,_:3/binary-unit:8,
+ _TID:8/binary-unit:8,_InformationElements/binary>>) ->
+ {echo,#sesT_echoReqV0{},SequenceNumber};
+%% Create PDP Context Request
+%% GTP97, SNN=0
+%% (No SNDCP N-PDU number)
+get_external_id(<<0:3,_:4,0:1,16:8,_Length:16,_SequenceNumber:16,
+ _FlowLabel:16,_SNDCP_N_PDU_Number:8,_:3/binary-unit:8,
+ TID:8/binary-unit:8,_InformationElements/binary>>) ->
+ {ok,_IMSI} = extract_imsi(TID);
+%%% Update PDP Context Request
+%%% GTP97, SNN=0
+%%% (No SNDCP N-PDU number)
+get_external_id(<<0:3,_:4,0:1,18:8,_Length:16,_SequenceNumber:16,
+ _FlowLabel:16,_SNDCP_N_PDU_Number:8,_:3/binary-unit:8,
+ TID:8/binary-unit:8,_InformationElements/binary>>) ->
+ {ok,_IMSI} = extract_imsi(TID);
+%%% Delete PDP Context Request
+%%% GTP97, SNN=0
+%%% (No SNDCP N-PDU number)
+get_external_id(<<0:3,_:4,0:1,20:8,_Length:16,_SequenceNumber:16,
+ _FlowLabel:16,_SNDCP_N_PDU_Number:8,_:3/binary-unit:8,
+ TID:8/binary-unit:8,_InformationElements/binary>>) ->
+ {ok,_IMSI} = extract_imsi(TID);
+%%% Error handling: GTP Message Too Short
+%%% Error handling: Unknown GTP Signalling message.
+%%% Error handling: Unexpected GTP Signalling message.
+get_external_id(_GTP_Message) ->
+ fault.
+
+%% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+%% extract_imsi/1
+%% Get the IMSI element from TID
+extract_imsi(TID) ->
+ {ok,#mvsgT_tid{imsi=IMSI}} = tid_internal_storage(TID,[]),
+ {ok,IMSI}.
+
+%%% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+%%% tid_internal_storage/3
+%%% Convert TID binary to internal datatype
+tid_internal_storage(Bin,_) ->
+ Size = byte_size(Bin) - 1,
+ <<Front:Size/binary,NSAPI:4,DigitN:4>> = Bin,
+ Result =
+ case DigitN of
+ 2#1111 ->
+ #mvsgT_tid{imsi = #mvsgT_imsi{value=Front}, nsapi = NSAPI};
+ _ ->
+ Value = <<Front/binary,2#1111:4,DigitN:4>>,
+ #mvsgT_tid{imsi = #mvsgT_imsi{value = Value}, nsapi = NSAPI}
+ end,
+ {ok,Result}.
diff --git a/lib/hipe/test/bs_SUITE_data/bs_flatb.erl b/lib/hipe/test/bs_SUITE_data/bs_flatb.erl
new file mode 100644
index 0000000000..6163917965
--- /dev/null
+++ b/lib/hipe/test/bs_SUITE_data/bs_flatb.erl
@@ -0,0 +1,29 @@
+%% -*- erlang-indent-level: 2 -*-
+%%--------------------------------------------------------------------------
+%% Program which resulted in a badarg crash when compiled to native code.
+%% The problem was that hipe_icode_primops was stating that the primop
+%% {bs_start_match, ok_matchstate} could not fail which made the icode_type
+%% pass removing the third clause of flatb/1.
+%%
+%% (The program was working correctly with hipe option 'no_icode_type'.)
+%%
+%% Reported by Andreas Sandberg on 3/1/2011 and fixed by Kostis on 5/1/2011
+%% with the help of Per Gustafsson.
+%% --------------------------------------------------------------------------
+-module(bs_flatb).
+
+-export([hipe_options/0, test/0]).
+
+hipe_options() ->
+ [icode_type].
+
+test() ->
+ [] = flatb([<<>>], []),
+ ok.
+
+flatb(<<X:8, Rest/binary>>, Acc) ->
+ flatb(Rest, [X|Acc]);
+flatb(<<>>, Acc) ->
+ Acc;
+flatb([V], Acc) ->
+ flatb(V, Acc).
diff --git a/lib/hipe/test/bs_SUITE_data/bs_id3.erl b/lib/hipe/test/bs_SUITE_data/bs_id3.erl
new file mode 100644
index 0000000000..a6152f05cd
--- /dev/null
+++ b/lib/hipe/test/bs_SUITE_data/bs_id3.erl
@@ -0,0 +1,75 @@
+%% -*- erlang-indent-level: 2 -*-
+%%==========================================================================
+%% From: Tomas Stejskal -- 23/02/2008
+%% I've found some strange behavior regarding binary matching. The module's
+%% purpose is reading an id3 version 1 or version 1.1 tag from an mp3 bin.
+%% When I use the function read_v1_or_v11_tag on a mp3 binary containing
+%% version 1 tag, it returns an error. However, when the function
+%% read_only_v1_tag is applied on the same file, it reads the tag data
+%% correctly. The only difference between these two functions is that the
+%% former has an extra branch to read version 1.1 tag.
+%% This was a BEAM compiler bug which was fixed by a patch to beam_dead.
+%%==========================================================================
+
+-module(bs_id3).
+
+-export([test/0]).
+
+-define(BIN, <<84,65,71,68,117,154,105,232,107,121,0,0,0,0,0,0,0,0,0,0,0,
+ 0,0,0,0,0,0,0,0,0,0,0,0,68,97,110,105,101,108,32,76,97,110,
+ 100,97,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,66,101,115,116,
+ 32,79,102,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
+ 50,48,48,48,50,48,48,48,32,45,32,66,101,115,116,32,79,102,
+ 32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,12>>).
+
+test() ->
+ R1 = parse_only_v1_tag(?BIN),
+ R2 = parse_v1_or_v11_tag(?BIN),
+ %% io:format("~p\n~p\n", [R1, R2]),
+ R1 = R2, % crash if not equal
+ ok.
+
+parse_only_v1_tag(<<"TAG", Title:30/binary,
+ Artist:30/binary, Album:30/binary,
+ _Year:4/binary, _Comment:30/binary,
+ _Genre:8>>) ->
+ {ok,
+ {"ID3v1",
+ [{title, trim(Title)},
+ {artist, trim(Artist)},
+ {album, trim(Album)}]}};
+parse_only_v1_tag(_) ->
+ error.
+
+parse_v1_or_v11_tag(<<"TAG", Title:30/binary,
+ Artist:30/binary, Album:30/binary,
+ _Year:4/binary, _Comment:28/binary,
+ 0:8, Track:8, _Genre:8>>) ->
+ {ok,
+ {"ID3v1.1",
+ [{track, Track}, {title, trim(Title)},
+ {artist, trim(Artist)}, {album, trim(Album)}]}};
+parse_v1_or_v11_tag(<<"TAG", Title:30/binary,
+ Artist:30/binary, Album:30/binary,
+ _Year:4/binary, _Comment:30/binary,
+ _Genre:8>>) ->
+ {ok,
+ {"ID3v1",
+ [{title, trim(Title)},
+ {artist, trim(Artist)},
+ {album, trim(Album)}]}};
+parse_v1_or_v11_tag(_) ->
+ error.
+
+trim(Bin) ->
+ list_to_binary(trim_blanks(binary_to_list(Bin))).
+
+trim_blanks(L) ->
+ lists:reverse(skip_blanks_and_zero(lists:reverse(L))).
+
+skip_blanks_and_zero([$\s|T]) ->
+ skip_blanks_and_zero(T);
+skip_blanks_and_zero([0|T]) ->
+ skip_blanks_and_zero(T);
+skip_blanks_and_zero(L) ->
+ L.
diff --git a/lib/hipe/test/bs_SUITE_data/bs_match.erl b/lib/hipe/test/bs_SUITE_data/bs_match.erl
new file mode 100644
index 0000000000..8194d878b8
--- /dev/null
+++ b/lib/hipe/test/bs_SUITE_data/bs_match.erl
@@ -0,0 +1,175 @@
+%%% -*- erlang-indent-level: 2 -*-
+%%%-------------------------------------------------------------------
+%%% File : bs_match.erl
+%%% Author : Per Gustafsson <[email protected]>
+%%% Purpose : Performs simple matching and construction of binaries
+%%% TODO : Add binary and float tests
+%%% Created : 20 Feb 2004
+%%%-------------------------------------------------------------------
+-module(bs_match).
+
+-export([test/0]).
+
+test() ->
+ Funs = [fun test_aligned/0, fun test_unaligned/0,
+ fun test_zero_tail/0, fun test_integer_matching/0],
+ lists:foreach(fun (F) -> ok = F() end, Funs).
+
+%%-------------------------------------------------------------------
+%% Test aligned accesses
+
+test_aligned() ->
+ 10 = aligned_skip_bits_all(1, <<10,11,12>>),
+ ok = aligned().
+
+aligned_skip_bits_all(N, Bin) ->
+ <<X:N/integer-unit:8, _/binary>> = Bin,
+ X.
+
+aligned() ->
+ Tail1 = mkbin([]),
+ {258, Tail1} = al_get_tail_used(mkbin([1,2])),
+ Tail2 = mkbin(lists:seq(1, 127)),
+ {35091, Tail2} = al_get_tail_used(mkbin([137,19|Tail2])),
+ 64896 = al_get_tail_unused(mkbin([253,128])),
+ 64895 = al_get_tail_unused(mkbin([253,127|lists:seq(42, 255)])),
+ Tail3 = mkbin(lists:seq(0, 19)),
+ {0, Tail1} = get_dyn_tail_used(Tail1, 0),
+ {0, Tail3} = get_dyn_tail_used(mkbin([Tail3]), 0),
+ {73, Tail3} = get_dyn_tail_used(mkbin([73|Tail3]), 8),
+ 0 = get_dyn_tail_unused(mkbin([]), 0),
+ 233 = get_dyn_tail_unused(mkbin([233]), 8),
+ 23 = get_dyn_tail_unused(mkbin([23,22,2]), 8),
+ ok.
+
+mkbin(L) when is_list(L) -> list_to_binary(L).
+
+al_get_tail_used(<<A:16,T/binary>>) -> {A, T}.
+
+al_get_tail_unused(<<A:16,_/binary>>) -> A.
+
+%%-------------------------------------------------------------------
+%% Test unaligned accesses
+
+test_unaligned() ->
+ 10 = unaligned_skip_bits_all(8, <<10,11,12>>),
+ ok = unaligned().
+
+unaligned_skip_bits_all(N, Bin) ->
+ <<X:N, _/binary>> = Bin,
+ X.
+
+unaligned() ->
+ {'EXIT', {function_clause,_}} = (catch get_tail_used(mkbin([42]))),
+ {'EXIT', {{badmatch,_},_}} = (catch get_dyn_tail_used(mkbin([137]), 3)),
+ {'EXIT', {function_clause,_}} = (catch get_tail_unused(mkbin([42,33]))),
+ {'EXIT', {{badmatch,_},_}} = (catch get_dyn_tail_unused(mkbin([44]), 7)),
+ ok.
+
+get_tail_used(<<A:1, T/binary>>) -> {A, T}.
+
+get_tail_unused(<<A:15, _/binary>>) -> A.
+
+get_dyn_tail_used(Bin, Sz) ->
+ <<A:Sz, T/binary>> = Bin,
+ {A,T}.
+
+get_dyn_tail_unused(Bin, Sz) ->
+ <<A:Sz, _T/binary>> = Bin,
+ A.
+
+%%-------------------------------------------------------------------
+%% Test zero tail
+
+test_zero_tail() ->
+ 42 = zt8(mkbin([42])),
+ {'EXIT', {function_clause, _}} = (catch zt8(mkbin([1,2]))),
+ {'EXIT', {function_clause, _}} = (catch zt44(mkbin([1,2]))),
+ ok.
+
+zt8(<<A:8>>) -> A.
+
+zt44(<<_:4,_:4>>) -> ok.
+
+%%-------------------------------------------------------------------
+%% Test integer matching
+
+test_integer_matching() ->
+ ok = test_static_integer_matching_1(),
+ ok = test_static_integer_matching_2(),
+ ok = test_static_integer_matching_3(),
+ ok = test_static_integer_matching_4(),
+ DynFun = fun (N) -> ok = test_dynamic_integer_matching(N) end,
+ lists:foreach(DynFun, [28, 27, 9, 17, 25, 8, 16, 24, 32]).
+
+test_static_integer_matching_1() ->
+ <<0:6, -25:28/integer-signed, 0:6>> = s11(),
+ <<0:6, -25:28/integer-little-signed, 0:6>> = s12(),
+ <<0:6, 25:28/integer-little, 0:6>> = s13(),
+ <<0:6, 25:28, 0:6>> = s14(),
+ ok.
+
+s11() ->
+ <<0:6, -25:28/integer-signed, 0:6>>.
+s12() ->
+ <<0:6, -25:28/integer-little-signed, 0:6>>.
+s13() ->
+ <<0:6, 25:28/integer-little, 0:6>>.
+s14() ->
+ <<0:6, 25:28, 0:6>>.
+
+test_static_integer_matching_2() ->
+ <<0:6, -25:20/integer-signed, 0:6>> = s21(),
+ <<0:6, -25:20/integer-little-signed, 0:6>> = s22(),
+ <<0:6, 25:20/integer-little, 0:6>> = s23(),
+ <<0:6, 25:20, 0:6>> = s24(),
+ ok.
+
+s21() ->
+ <<0:6, -25:20/integer-signed, 0:6>>.
+s22() ->
+ <<0:6, -25:20/integer-little-signed, 0:6>>.
+s23() ->
+ <<0:6, 25:20/integer-little, 0:6>>.
+s24() ->
+ <<0:6, 25:20, 0:6>>.
+
+test_static_integer_matching_3() ->
+ <<0:6, -25:12/integer-signed, 0:6>> = s31(),
+ <<0:6, -25:12/integer-little-signed, 0:6>> = s32(),
+ <<0:6, 25:12/integer-little, 0:6>> = s33(),
+ <<0:6, 25:12, 0:6>> = s34(),
+ ok.
+
+s31() ->
+ <<0:6, -25:12/integer-signed, 0:6>>.
+s32() ->
+ <<0:6, -25:12/integer-little-signed, 0:6>>.
+s33() ->
+ <<0:6, 25:12/integer-little, 0:6>>.
+s34() ->
+ <<0:6, 25:12, 0:6>>.
+
+test_static_integer_matching_4() ->
+ <<0:6, -3:4/integer-signed, 0:6>> = s41(),
+ <<0:6, -3:4/integer-little-signed, 0:6>> = s42(),
+ <<0:6, 7:4/integer-little, 0:6>> = s43(),
+ <<0:6, 7:4, 0:6>> = s44(),
+ ok.
+
+s41() ->
+ <<0:6, -3:4/integer-signed, 0:6>>.
+s42() ->
+ <<0:6, -3:4/integer-little-signed, 0:6>>.
+s43() ->
+ <<0:6, 7:4/integer-little, 0:6>>.
+s44() ->
+ <<0:6, 7:4, 0:6>>.
+
+test_dynamic_integer_matching(N) ->
+ S = 32 - N,
+ <<-12:N/integer-signed, 0:S>> = <<-12:N/integer-signed, 0:S>>,
+ <<-12:N/integer-little-signed, 0:S>> = <<-12:N/integer-little-signed, 0:S>>,
+ <<12:N/integer, 0:S>> = <<12:N/integer, 0:S>>,
+ <<12:N/integer-little, 0:S>> = <<12:N/integer-little, 0:S>>,
+ ok.
diff --git a/lib/hipe/test/bs_SUITE_data/bs_native_float.erl b/lib/hipe/test/bs_SUITE_data/bs_native_float.erl
new file mode 100644
index 0000000000..15fe0bf0c6
--- /dev/null
+++ b/lib/hipe/test/bs_SUITE_data/bs_native_float.erl
@@ -0,0 +1,22 @@
+%% -*- erlang-indent-level: 2 -*-
+%%-------------------------------------------------------------------
+%% File : bs_native_float.erl
+%% Author : Kostis Sagonas
+%% Description : Test sent by Bjorn Gustavsson to report a bug in the
+%% handling of the 'native' endian specifier.
+%% Created : 28 Nov 2004
+%%-------------------------------------------------------------------
+-module(bs_native_float).
+
+-export([test/0]).
+
+test() ->
+ BeamRes = mk_bin(1.0, 2.0, 3.0),
+ hipe:c(?MODULE), %% Original was: hipe:c({?MODULE,vs_to_bin,1}, [o2]),
+ HipeRes = mk_bin(1.0, 2.0, 3.0),
+ %% io:format("Beam result = ~w\nHiPE result = ~w\n", [BeamRes,HipeRes]),
+ BeamRes = HipeRes,
+ ok.
+
+mk_bin(X, Y, Z) ->
+ <<X:64/native-float, Y:64/native-float, Z:64/native-float>>.
diff --git a/lib/hipe/test/bs_SUITE_data/bs_orber.erl b/lib/hipe/test/bs_SUITE_data/bs_orber.erl
new file mode 100644
index 0000000000..c80ab8928d
--- /dev/null
+++ b/lib/hipe/test/bs_SUITE_data/bs_orber.erl
@@ -0,0 +1,26 @@
+%%% -*- erlang-indent-level: 2 -*-
+%%%-------------------------------------------------------------------
+%%% Author : Per Gustafsson <[email protected]>
+%%% Purpose : Checks that labels are handled properly from Core
+%%% Created : 2 Nov 2004
+%%%-------------------------------------------------------------------
+-module(bs_orber).
+
+-export([test/0]).
+
+test() ->
+ 1 = dec_giop_message_header(<<1,1:32/little-integer>>),
+ 1 = dec_giop_message_header(<<0,1:32/big-integer>>),
+ {2, 1} = dec_giop_message_header(<<2,1:32/little-integer>>),
+ {3, 1} = dec_giop_message_header(<<3,1:32/big-integer>>),
+ ok.
+
+dec_giop_message_header(<<1:8, MessSize:32/little-integer>>) ->
+ MessSize;
+dec_giop_message_header(<<0:8, MessSize:32/big-integer>>) ->
+ MessSize;
+dec_giop_message_header(<<Flags:8, MessSize:32/little-integer>>) when
+ ((Flags band 16#03) =:= 16#02) ->
+ {Flags, MessSize};
+dec_giop_message_header(<<Flags:8, MessSize:32/big-integer>>) ->
+ {Flags, MessSize}.
diff --git a/lib/hipe/test/bs_SUITE_data/bs_pmatch.erl b/lib/hipe/test/bs_SUITE_data/bs_pmatch.erl
new file mode 100644
index 0000000000..9474ffea4a
--- /dev/null
+++ b/lib/hipe/test/bs_SUITE_data/bs_pmatch.erl
@@ -0,0 +1,269 @@
+%% -*- erlang-indent-level: 2 -*-
+%%--------------------------------------------------------------------
+%% Tests that basic cases of binary pattern matching work
+%%--------------------------------------------------------------------
+-module(bs_pmatch).
+
+-export([test/0]).
+
+test() ->
+ %% construct some binaries
+ Bin42 = <<42>>,
+ Bin = <<12,17,42,0,0,0>>,
+ BinSS = <<0,1,0,0,0>>,
+ %% do some pattern matching
+ ok = pm_const(Bin42),
+ <<17,42,0,0,0>> = pm_tail(Bin),
+ 42 = pm_little(<<0:1,42:7>>),
+ 42 = pm_rec(Bin),
+ 30 = pm_rec_acc(<<1,2,3,4,5,6,7,8,9,10>>, 0),
+ 42 = pm_binary_tuple(Bin42),
+ -1 = pm_with_illegal_float(),
+ %% do some pattern matching with bound segments
+ ok = pm_bound_var(),
+ ok = pm_bound_tail(),
+ %% do some tests with floating point numbers
+ ok = pm_float(),
+ ok = pm_float_little(),
+ %% do some pattern matching with segments of unknown sizes
+ {<<17>>, <<42,0,0,0>>} = pm_body_s(Bin, 1),
+ {<<17>>, <<42,0,0,0>>} = pm_body_ss(Bin, 1, 4),
+ {<<45>>, <<>>} = pm_size_split(<<1:16,45>>),
+ {<<45>>, <<46,47>>} = pm_size_split(<<1:16,45,46,47>>),
+ {<<45,46>>, <<47>>} = pm_size_split(<<2:16,45,46,47>>),
+ {<<45,46>>, <<47>>} = pm_size_split_2(2, <<2:16,45,46,47>>),
+ {'EXIT',{function_clause,_}} = (catch pm_size_split_2(42, <<2:16,45,46,47>>)),
+ {<<45,46,47>>, <<48>>} = pm_sizes_split(<<16:8,3:16,45,46,47,48>>),
+ <<"cdef">> = pm_skip_segment(<<2:8, "abcdef">>),
+ -1 = pm_double_size_in_head(BinSS),
+ -1 = pm_double_size_in_body(BinSS),
+ %% and finally some cases which were problematic for various reasons
+ ok = pm_bigs(),
+ ok = pm_sean(),
+ ok = pm_bin8(<<1,2,3,4,5,6,7,8>>),
+ ok = pm_bs_match_string(),
+ ok = pm_till_gc(),
+ ok.
+
+%%--------------------
+%% Test cases below
+%%--------------------
+
+pm_const(<<42>>) ->
+ ok.
+
+pm_tail(<<12, Bin/binary>>) ->
+ Bin.
+
+pm_little(<<_:1, X:15/little>>) ->
+ {wrong, X};
+pm_little(<<_:1, X:7/little>>) ->
+ X.
+
+pm_rec(<<12, Bin/binary>>) ->
+ pm_rec(Bin);
+pm_rec(<<17, Word:4/little-signed-integer-unit:8>>) ->
+ Word.
+
+pm_rec_acc(<<_:4, A:4, Rest/binary>>, Acc) ->
+ case Rest of
+ <<X, Y, 9, NewRest/binary>> ->
+ pm_rec_acc(NewRest, X+Y+Acc);
+ <<X, 5, NewRest/binary>> ->
+ pm_rec_acc(NewRest, X+Acc);
+ <<2, NewRest/binary>> ->
+ pm_rec_acc(NewRest, 1+Acc);
+ <<NewRest/binary>> ->
+ pm_rec_acc(NewRest, A+Acc)
+ end;
+pm_rec_acc(<<>>, Acc) ->
+ Acc.
+
+pm_binary_tuple(<<X>>) ->
+ X;
+pm_binary_tuple({Y, Z}) ->
+ Y + Z.
+
+pm_with_illegal_float() ->
+ Bin = <<-1:64>>, % create a binary which is illegal as float
+ pm_float_integer(Bin). % try to match it out as a float
+
+pm_float_integer(<<F:64/float>>) -> F;
+pm_float_integer(<<I:64/integer-signed>>) -> I.
+
+%%--------------------------------------------------------------------
+%% Some tests with bound variables in segments
+
+pm_bound_var() ->
+ ok = pm_bound_var(42, 13, <<42,13>>),
+ no = pm_bound_var(42, 13, <<42,255>>),
+ no = pm_bound_var(42, 13, <<154,255>>),
+ ok.
+
+pm_bound_var(A, B, <<A:8, B:8>>) -> ok;
+pm_bound_var(_, _, _) -> no.
+
+pm_bound_tail() ->
+ ok = pm_bound_tail(<<>>, <<13,14>>),
+ ok = pm_bound_tail(<<2,3>>, <<1,1,2,3>>),
+ no = pm_bound_tail(<<2,3>>, <<1,1,2,7>>),
+ no = pm_bound_tail(<<2,3>>, <<1,1,2,3,4>>),
+ no = pm_bound_tail(<<2,3>>, <<>>),
+ ok.
+
+pm_bound_tail(T, <<_:16, T/binary>>) -> ok;
+pm_bound_tail(_, _) -> no.
+
+%%--------------------------------------------------------------------
+%% Floating point tests
+
+pm_float() ->
+ F = f1(),
+ G = f_one(),
+ G = match_float(<<63,128,0,0>>, 32, 0),
+ G = match_float(<<63,240,0,0,0,0,0,0>>, 64, 0),
+ fcmp(F, match_float(<<F:32/float>>, 32, 0)),
+ fcmp(F, match_float(<<F:64/float>>, 64, 0)),
+ fcmp(F, match_float(<<1:1,F:32/float,127:7>>, 32, 1)),
+ fcmp(F, match_float(<<1:1,F:64/float,127:7>>, 64, 1)),
+ fcmp(F, match_float(<<1:13,F:32/float,127:3>>, 32, 13)),
+ fcmp(F, match_float(<<1:13,F:64/float,127:3>>, 64, 13)),
+ ok.
+
+fcmp(F1, F2) when (F1 - F2) / F2 < 0.0000001 -> ok.
+
+match_float(Bin0, Fsz, I) ->
+ Bin = make_sub_bin(Bin0),
+ Bsz = bit_size(Bin),
+ Tsz = Bsz - Fsz - I,
+ <<_:I,F:Fsz/float,_:Tsz>> = Bin,
+ F.
+
+pm_float_little() ->
+ F = f2(),
+ G = f_one(),
+ G = match_float_little(<<0,0,0,0,0,0,240,63>>, 64, 0),
+ G = match_float_little(<<0,0,128,63>>, 32, 0),
+ fcmp(F, match_float_little(<<F:32/float-little>>, 32, 0)),
+ fcmp(F, match_float_little(<<F:64/float-little>>, 64, 0)),
+ fcmp(F, match_float_little(<<1:1,F:32/float-little,127:7>>, 32, 1)),
+ fcmp(F, match_float_little(<<1:1,F:64/float-little,127:7>>, 64, 1)),
+ fcmp(F, match_float_little(<<1:13,F:32/float-little,127:3>>, 32, 13)),
+ fcmp(F, match_float_little(<<1:13,F:64/float-little,127:3>>, 64, 13)),
+ ok.
+
+match_float_little(Bin0, Fsz, I) ->
+ Bin = make_sub_bin(Bin0),
+ Bsz = bit_size(Bin),
+ Tsz = Bsz - Fsz - I,
+ <<_:I, F:Fsz/float-little, _:Tsz>> = Bin,
+ F.
+
+make_sub_bin(Bin0) ->
+ Sz = byte_size(Bin0),
+ Bin1 = <<37,Bin0/binary,38,39>>,
+ <<_:8,Bin:Sz/binary,_:8,_:8>> = Bin1,
+ Bin.
+
+f1() -> 3.1415.
+
+f2() -> 2.7133.
+
+f_one() -> 1.0.
+
+%%--------------------------------------------------------------------
+%% Some tests using size fields specified within the binary
+pm_body_s(Bin, S1) ->
+ <<12, B1:S1/binary, B2:4/binary>> = Bin, %% 4 is hard-coded
+ {B1, B2}.
+
+pm_body_ss(Bin, S1, S2) ->
+ <<12, B1:S1/binary, B2:S2/binary>> = Bin,
+ {B1, B2}.
+
+pm_size_split(<<N:16, B:N/binary, T/binary>>) ->
+ {B, T}.
+
+pm_size_split_2(N, <<N:16, B:N/binary, T/binary>>) ->
+ {B, T}.
+
+pm_sizes_split(<<N0:8, N:N0, B:N/binary, T/binary>>) ->
+ {B, T}.
+
+pm_skip_segment(<<N:8, _:N/binary, T/binary>>) -> T.
+
+%%--------------------------------------------------------------------
+%% Some tests using multiple occurrences of size fields
+pm_double_size_in_head(<<S:16, _:S/binary, _:S/binary, _/binary>>) ->
+ -S.
+
+pm_double_size_in_body(Bin) ->
+ <<S:16, _:S/binary, _:S/binary, _/binary>> = Bin,
+ -S.
+
+%%--------------------------------------------------------------------
+%% matching with 64-bit integers which become big nums
+-define(BIG, 16#7fffffff7fffffff).
+
+pm_bigs() ->
+ <<X:64/little>> = <<?BIG:64/little>>,
+ true = (X =:= big()),
+ <<Y:64>> = <<?BIG:64>>,
+ true = (Y =:= big()),
+ ok.
+
+big() -> ?BIG.
+
+%%--------------------------------------------------------------------
+
+pm_sean() ->
+ small = sean1(<<>>),
+ small = sean1(<<1>>),
+ small = sean1(<<1,2>>),
+ small = sean1(<<1,2,3>>),
+ large = sean1(<<1,2,3,4>>),
+ small = sean1(<<4>>),
+ small = sean1(<<4,5>>),
+ small = sean1(<<4,5,6>>),
+ {'EXIT', {function_clause, _}} = (catch sean1(<<4,5,6,7>>)),
+ ok.
+
+sean1(<<B/binary>>) when byte_size(B) < 4 -> small;
+sean1(<<1, _/binary>>) -> large.
+
+%%--------------------------------------------------------------------
+%% Crashed on SPARC due to a bug in linear scan register allocator
+pm_bin8(<<A, B, C, D, E, F, G, H>>) ->
+ 10 = add4(A, B, C, D),
+ 26 = add4(E, F, G, H),
+ ok.
+
+add4(X, Y, Z, W) ->
+ X + Y + Z + W.
+
+%%--------------------------------------------------------------------
+%% Cases that exposed bugs in the handling of bs_match_string with an
+%% empty destination list. Reported on 2013/2/12 and fixed 2013/3/10.
+
+pm_bs_match_string() ->
+ Bin = <<42,42>>,
+ Bin = pm_match_string_head(Bin),
+ ok = (pm_match_string_fun())(Bin).
+
+pm_match_string_head(<<42, _/bits>> = B) -> B.
+
+pm_match_string_fun() ->
+ fun (<<X, _/bits>>) when X =:= 42 -> ok end.
+
+%%--------------------------------------------------------------------
+%% Match a lot to force a garbage collection which exposed a bug
+
+pm_till_gc() ->
+ Bin = <<16#76543210:32>>,
+ 16#76543210 = pm_a_lot(Bin, 1000000),
+ ok.
+
+pm_a_lot(<<X:32>>, 0) ->
+ X;
+pm_a_lot(<<X:32>>, N) ->
+ pm_a_lot(<<X:32>>, N-1).
diff --git a/lib/hipe/test/bs_SUITE_data/bs_pmatch_bugs.erl b/lib/hipe/test/bs_SUITE_data/bs_pmatch_bugs.erl
new file mode 100644
index 0000000000..b280705a47
--- /dev/null
+++ b/lib/hipe/test/bs_SUITE_data/bs_pmatch_bugs.erl
@@ -0,0 +1,67 @@
+%% -*- erlang-indent-level: 2 -*-
+%%--------------------------------------------------------------------
+-module(bs_pmatch_bugs).
+
+-export([test/0]).
+
+test() ->
+ Bin = <<"123.123">>,
+ <<49,50,51>> = lex_digits1(Bin, 1, []),
+ <<49,50,51>> = lex_digits2(Bin, 1, []),
+ ok = var_bind_bug(<<1, 2, 3, 4, 5, 6, 7, 8>>),
+ ok.
+
+%%--------------------------------------------------------------------
+%% One of the lex_digits functions below gave incorrect results due to
+%% incorrect pattern matching compilation of binaries by the byte code
+%% compiler. Fixed by Bjorn Gustavsson on 5/3/2003.
+%% --------------------------------------------------------------------
+lex_digits1(<<$., Rest/binary>>, _Val, _Acc) ->
+ Rest;
+lex_digits1(<<N, Rest/binary>>, Val, Acc) when N >= $0, N =< $9 ->
+ lex_digits1(Rest, Val * 10 + dec(N), Acc);
+lex_digits1(_Other, _Val, _Acc) ->
+ not_ok.
+
+lex_digits2(<<N, Rest/binary>>,Val, Acc) when N >= $0, N =< $9 ->
+ lex_digits2(Rest, Val * 10 + dec(N), Acc);
+lex_digits2(<<$., Rest/binary>>, _Val, _Acc) ->
+ Rest;
+lex_digits2(_Other, _Val, _Acc) ->
+ not_ok.
+
+dec(A) ->
+ A - $0.
+
+%%--------------------------------------------------------------------
+%% From: Bernard Duggan
+%% Date: 11/3/2011
+%%
+%% I've just run into an interesting little bit of behaviour that
+%% doesn't seem quite right. erlc gives me the warning
+%%
+%% 43: Warning: this clause cannot match because a previous
+%% clause at line 42 always matches
+%% (line 42 is the "B -> wrong;" line).
+%%
+%% And sure enough, if you run test/0 you get 'wrong' back.
+%%
+%% That, in itself, is curious to me since by my understanding B should
+%% be bound by the function header, and have no guarantee of being the
+%% same as A. I can't see how it could be unbound.
+%%
+%% Doubly curious, is that if I stop using B as the size specifier of C,
+%% like this:
+%%
+%% match(<<A:1/binary, B:8/integer, _C:1/binary, _Rest/binary>>) ->
+%%
+%% the warning goes away. And the result becomes 'ok' (in spite of
+%% nothing in the body having changed, and the only thing changing in
+%% the header being the size of an unused variable at the tail of the
+%% binary).
+%%--------------------------------------------------------------------
+var_bind_bug(<<A:1/binary, B:8/integer, _C:B/binary, _Rest/binary>>) ->
+ case A of
+ B -> wrong;
+ _ -> ok
+ end.
diff --git a/lib/hipe/test/bs_SUITE_data/bs_pmatch_in_guards.erl b/lib/hipe/test/bs_SUITE_data/bs_pmatch_in_guards.erl
new file mode 100644
index 0000000000..159227bb92
--- /dev/null
+++ b/lib/hipe/test/bs_SUITE_data/bs_pmatch_in_guards.erl
@@ -0,0 +1,23 @@
+%% -*- erlang-indent-level: 2 -*-
+%%--------------------------------------------------------------------
+%% Tests that basic cases of binary pattern matching in guards work
+%%--------------------------------------------------------------------
+-module(bs_pmatch_in_guards).
+
+-export([test/0]).
+
+test() ->
+ 1 = in_guard(<<16#74ad:16>>, 16#e95, 5),
+ 2 = in_guard(<<16#3A,16#F7,"hello">>, 16#3AF7, <<"hello">>),
+ 3 = in_guard(<<16#FBCD:14,3.1415/float,3:2>>, 16#FBCD, 3.1415),
+ nope = in_guard(<<1>>, 42, b),
+ nope = in_guard(<<1>>, a, b),
+ nope = in_guard(<<1,2>>, 1, 1),
+ nope = in_guard(<<4,5>>, 1, 2.71),
+ nope = in_guard(<<4,5>>, 1, <<12,13>>),
+ ok.
+
+in_guard(Bin, A, B) when <<A:13,B:3>> == Bin -> 1;
+in_guard(Bin, A, B) when <<A:16,B/binary>> == Bin -> 2;
+in_guard(Bin, A, B) when <<A:14,B/float,3:2>> == Bin -> 3;
+in_guard(_, _, _) -> nope.
diff --git a/lib/hipe/test/bs_SUITE_data/bs_potpurri.erl b/lib/hipe/test/bs_SUITE_data/bs_potpurri.erl
new file mode 100644
index 0000000000..8bc4fe5c88
--- /dev/null
+++ b/lib/hipe/test/bs_SUITE_data/bs_potpurri.erl
@@ -0,0 +1,200 @@
+%%% -*- erlang-indent-level: 2 -*-
+%%%-------------------------------------------------------------------
+-module(bs_potpurri).
+
+-export([test/0]).
+
+test() ->
+ ok = integer(),
+ ok = signed_integer(),
+ ok = dynamic(),
+ ok = more_dynamic(),
+ ok = mml(),
+ ok.
+
+%% compile(Opts0) ->
+%% case proplists:get_bool(core, Opts0) of
+%% true ->
+%% test:note(?MODULE, "disabling compilation from core - BUG"),
+%% Opts = [{core,false}|Opts0];
+%% false ->
+%% Opts = Opts0
+%% end,
+%% hipe:c(?MODULE, Opts).
+
+integer() ->
+ 0 = get_int(mkbin([])),
+ 0 = get_int(mkbin([0])),
+ 42 = get_int(mkbin([42])),
+ 255 = get_int(mkbin([255])),
+ 256 = get_int(mkbin([1,0])),
+ 257 = get_int(mkbin([1,1])),
+ 258 = get_int(mkbin([1,2])),
+ 258 = get_int(mkbin([1,2])),
+ 65534 = get_int(mkbin([255,254])),
+ 16776455 = get_int(mkbin([255,253,7])),
+ 4245492555 = get_int(mkbin([253,13,19,75])),
+ L = [200,1,19,128,222,42,97,111,200,1,19,128,222,42,97,111],
+ ok = cmp128(mkbin(L), uint(L)),
+ ok = fun_clause(catch get_int(mkbin(lists:seq(1,5)))),
+ ok.
+
+get_int(<<I:0>>) -> I;
+get_int(<<I:8>>) -> I;
+get_int(<<I:16>>) -> I;
+get_int(<<I:24>>) -> I;
+get_int(<<I:32>>) -> I.
+
+cmp128(<<I:128>>, I) -> ok;
+cmp128(_Bin, _I) -> not_ok.
+
+signed_integer() ->
+ {no_match,_} = sint(mkbin([])),
+ {no_match,_} = sint(mkbin([1,2,3])),
+ 127 = sint(mkbin([127])),
+ -1 = sint(mkbin([255])),
+ -128 = sint(mkbin([128])),
+ 42 = sint(mkbin([42,255])),
+ 127 = sint(mkbin([127,255])),
+ ok.
+
+sint(Bin) ->
+ case Bin of
+ <<I:8/signed>> -> I;
+ <<I:8/signed,_:3,_:5>> -> I;
+ Other -> {no_match,Other}
+ end.
+
+uint(L) -> uint(L, 0).
+
+uint([H|T], Acc) -> uint(T, Acc bsl 8 bor H);
+uint([], Acc) -> Acc.
+
+dynamic() ->
+ ok = dynamic(mkbin([255]), 8),
+ ok = dynamic(mkbin([255,255]), 16),
+ ok = dynamic(mkbin([255,255,255]), 24),
+ ok = dynamic(mkbin([255,255,255,255]), 32),
+ ok.
+
+dynamic(Bin, S1) when S1 >= 0 ->
+ S2 = bit_size(Bin) - S1,
+ dynamic(Bin, S1, S2, (1 bsl S1) - 1, (1 bsl S2) - 1),
+ dynamic(Bin, S1-1);
+dynamic(_Bin, _) -> ok.
+
+dynamic(Bin, S1, S2, A, B) ->
+ %% io:format("~p ~p ~p ~p\n", [S1,S2,A,B]),
+ case Bin of
+ <<A:S1,B:S2>> ->
+ %% io:format("~p ~p ~p ~p\n", [S1,S2,A,B]),
+ ok;
+ <<A1:S1,B2:S2>> -> erlang:error(badmatch, [Bin,S1,S2,A,B,A1,B2])
+ end.
+
+more_dynamic() ->
+ %% Unsigned big-endian numbers.
+ Unsigned = fun(Bin, List, SkipBef, N) ->
+ SkipAft = bit_size(Bin) - N - SkipBef,
+ <<_I1:SkipBef,Int:N,_I2:SkipAft>> = Bin,
+ Int = make_int(List, N, 0)
+ end,
+ ok = more_dynamic1(Unsigned, funny_binary(42)),
+
+ %% Signed big-endian numbers.
+ Signed = fun(Bin, List, SkipBef, N) ->
+ SkipAft = bit_size(Bin) - N - SkipBef,
+ <<_I1:SkipBef,Int:N/signed,_I2:SkipAft>> = Bin,
+ case make_signed_int(List, N) of
+ Int -> ok;
+ Other ->
+ io:format("Bin = ~p,", [Bin]),
+ io:format("SkipBef = ~p, N = ~p", [SkipBef,N]),
+ io:format("Expected ~p, got ~p", [Int,Other]),
+ exit(Other)
+ end
+ end,
+ ok = more_dynamic1(Signed, funny_binary(43)),
+
+ %% Unsigned little-endian numbers.
+ UnsLittle = fun(Bin, List, SkipBef, N) ->
+ SkipAft = bit_size(Bin) - N - SkipBef,
+ <<_I1:SkipBef,Int:N/little,_I2:SkipAft>> = Bin,
+ Int = make_int(big_to_little(List, N), N, 0)
+ end,
+ more_dynamic1(UnsLittle, funny_binary(44)),
+
+ %% Signed little-endian numbers.
+ SignLittle = fun(Bin, List, SkipBef, N) ->
+ SkipAft = bit_size(Bin) - N - SkipBef,
+ <<_I1:SkipBef,Int:N/signed-little,_I2:SkipAft>> = Bin,
+ Little = big_to_little(List, N),
+ Int = make_signed_int(Little, N)
+ end,
+ ok = more_dynamic1(SignLittle, funny_binary(45)),
+
+ ok.
+
+funny_binary(N) ->
+ B0 = erlang:md5([N]),
+ {B1,_B2} = split_binary(B0, byte_size(B0) div 2),
+ B1.
+
+more_dynamic1(Action, Bin) ->
+ BitList = bits_to_list(binary_to_list(Bin), 16#80),
+ more_dynamic2(Action, Bin, BitList, 0).
+
+more_dynamic2(Action, Bin, [_|T]=List, Bef) ->
+ more_dynamic3(Action, Bin, List, Bef, bit_size(Bin)),
+ more_dynamic2(Action, Bin, T, Bef+1);
+more_dynamic2(_Action, _Bin, [], _Bef) -> ok.
+
+more_dynamic3(Action, Bin, List, Bef, Aft) when Bef =< Aft ->
+ %% io:format("~p, ~p", [Bef,Aft-Bef]),
+ Action(Bin, List, Bef, Aft-Bef),
+ more_dynamic3(Action, Bin, List, Bef, Aft-1);
+more_dynamic3(_, _, _, _, _) -> ok.
+
+big_to_little(List, N) -> big_to_little(List, N, []).
+
+big_to_little([B0,B1,B2,B3,B4,B5,B6,B7|T], N, Acc) when N >= 8 ->
+ big_to_little(T, N-8, [B0,B1,B2,B3,B4,B5,B6,B7|Acc]);
+big_to_little(List, N, Acc) -> lists:sublist(List, 1, N) ++ Acc.
+
+make_signed_int(_List, 0) -> 0;
+make_signed_int([0|_]=List, N) -> make_int(List, N, 0);
+make_signed_int([1|_]=List0, N) ->
+ List1 = reversed_sublist(List0, N, []),
+ List2 = two_complement_and_reverse(List1, 1, []),
+ -make_int(List2, length(List2), 0).
+
+reversed_sublist(_List, 0, Acc) -> Acc;
+reversed_sublist([H|T], N, Acc) -> reversed_sublist(T, N-1, [H|Acc]).
+
+two_complement_and_reverse([H|T], Carry, Acc) ->
+ Sum = 1 - H + Carry,
+ two_complement_and_reverse(T, Sum div 2, [Sum rem 2|Acc]);
+two_complement_and_reverse([], Carry, Acc) -> [Carry|Acc].
+
+make_int(_List, 0, Acc) -> Acc;
+make_int([H|T], N, Acc) -> make_int(T, N-1, Acc bsl 1 bor H).
+
+bits_to_list([_|T], 0) -> bits_to_list(T, 16#80);
+bits_to_list([H|_]=List, Mask) ->
+ [case H band Mask of
+ 0 -> 0;
+ _ -> 1
+ end|bits_to_list(List, Mask bsr 1)];
+bits_to_list([], _) -> [].
+
+fun_clause({'EXIT',{function_clause,_}}) -> ok.
+
+mkbin(L) when is_list(L) -> list_to_binary(L).
+
+mml() ->
+ single_byte_binary = mml_choose(<<42>>),
+ multi_byte_binary = mml_choose(<<42,43>>),
+ ok.
+
+mml_choose(<<_:8>>) -> single_byte_binary;
+mml_choose(<<_:8, _T/binary>>) -> multi_byte_binary.
diff --git a/lib/hipe/test/bs_SUITE_data/bs_remove3.erl b/lib/hipe/test/bs_SUITE_data/bs_remove3.erl
new file mode 100644
index 0000000000..a98b0b5b28
--- /dev/null
+++ b/lib/hipe/test/bs_SUITE_data/bs_remove3.erl
@@ -0,0 +1,104 @@
+%%% -*- erlang-indent-level: 2 -*-
+%%%-------------------------------------------------------------------
+%%% File : bs_remove3.erl
+%%% Author : Per Gustafsson <[email protected]>
+%%% Purpose :
+%%%
+%%% Created : 13 Apr 2004 by Per Gustafsson
+%%%-------------------------------------------------------------------
+-module(bs_remove3).
+
+-export([test/0]).
+
+-define(A, <<56,0,120,0,0,31,255,255,102,42,12,0,3,3,16,5,24,3,240,0,0,32,0,196,
+ 2,128,4,0,255,255,254,33,68,96,0,8,8,213,40,192,31,196,0,4,0,0>>).
+-define(B, <<28,32,0,96,0,8,0,7,255,255,212,33,98,12,0,0,1,0,48,72,66,3,0,7,240,
+ 64,0,0,8,0,0,224,0,10,128,0,64,0,63,255,254,133,10,80,96,0,0,8,1,6,
+ 18,4,24,0,63,128,0,0,4,64,0,0>>).
+
+test() ->
+ Bin1 = <<30,16,0,90,0,1,0,0,255,255,255,255,81,67,101,7,0,
+ 0,0,96,6,12,146,18,14,0,15,252,16,0,0,17,0,0>>,
+ Bin = <<Bin1/binary, Bin1/binary>>,
+ ?A = loop(Bin, 10, fun run_list/1),
+ ?A = loop(Bin, 10, fun run_bin/1),
+ ?B = loop(Bin, 10, fun r31/1),
+ ok.
+
+loop(Arg, 0, F) ->
+ F(Arg);
+loop(Arg, N, F) ->
+ F(Arg),
+ loop(Arg, N-1, F).
+
+run_list(Bin) ->
+ List = run1(Bin),
+ list_to_binary(List).
+
+run1(<<A1:2,_:1,A2:2,_:1,A3:2,_:1,A4:2,_:1,
+ A5:2,_:1,A6:2,_:1,A7:2,_:1,A8:2,_:1,Rest/binary>>) ->
+ [<<A1:2,A2:2,A3:2,A4:2,A5:2,A6:2,A7:2,A8:2>>, run2(Rest)];
+run1(<<A1:2,_:1,A2:2,_:1,A3:2,_:1,A4:2,_:1,A5:2,_:1,A6:1>>) ->
+ [<<A1:2,A2:2,A3:2,A4:2,A5:2,A6:1,0:5>>];
+run1(<<A1:2,_:1,A2:2,_:1,A3:2>>) ->
+ [<<A1:2,A2:2,A3:2,0:2>>];
+run1(<<>>) ->
+ [].
+
+run_bin(Bin) ->
+ run2(Bin).
+
+run2(<<A1:2,_:1,A2:2,_:1,A3:2,_:1,A4:2,_:1,
+ A5:2,_:1,A6:2,_:1,A7:2,_:1,A8:2,_:1,Rest/binary>>) ->
+ Bin = run2(Rest),
+ <<A1:2,A2:2,A3:2,A4:2,A5:2,A6:2,A7:2,A8:2,Bin/binary>>;
+run2(<<A1:2,_:1,A2:2,_:1,A3:2,_:1,A4:2,_:1,A5:2,_:1,A6:1>>) ->
+ <<A1:2,A2:2,A3:2,A4:2,A5:2,A6:1,0:5>>;
+run2(<<A1:2,_:1,A2:2,_:1,A3:2>>) ->
+ <<A1:2,A2:2,A3:2,0:2>>;
+run2(<<>>) ->
+ <<>>.
+
+r31(Bin) ->
+ List = remove3rd1(0, 0, Bin, [-1]),
+ build(List, Bin, 0, <<>>).
+
+build([N1, N2|Rest], Bin, N, Present) ->
+ X = N1+1, Y = N2-X,
+ S = rest(N2),
+ <<_:X,A:Y,_:S,_/binary>> = Bin,
+ S1 = rest(N+Y),
+ NewPresent = <<Present:N/binary-unit:1, A:Y, 0:S1>>,
+ build([N2|Rest], Bin, N+Y, NewPresent);
+
+build([_], _Bin, _N, Present) ->
+ Present.
+
+rest(X) ->
+ case 8 - (X rem 8) of
+ 8 -> 0;
+ H -> H
+ end.
+
+remove3rd1(N, 2, Bin, List) ->
+ S = rest(N+1),
+ case Bin of
+ <<_:N, 1:1, _:S,_/binary>> ->
+ remove3rd1(N+1, 0, Bin, [N|List]);
+ <<_:N, 0:1, _:S,_/binary>> ->
+ remove3rd1(N+1, 2, Bin, List);
+ _ ->
+ Size = byte_size(Bin) * 8,
+ lists:reverse([Size|List])
+ end;
+remove3rd1(N, I, Bin, List) ->
+ S = rest(N+1),
+ case Bin of
+ <<_:N, 1:1, _:S,_/binary>> ->
+ remove3rd1(N+1, I+1, Bin, List);
+ <<_:N, 0:1, _:S,_/binary>> ->
+ remove3rd1(N+1, I, Bin, List);
+ _ ->
+ Size = byte_size(Bin) * 8,
+ lists:reverse([Size|List])
+ end.
diff --git a/lib/hipe/test/bs_SUITE_data/bs_save.erl b/lib/hipe/test/bs_SUITE_data/bs_save.erl
new file mode 100644
index 0000000000..fe2b1105f2
--- /dev/null
+++ b/lib/hipe/test/bs_SUITE_data/bs_save.erl
@@ -0,0 +1,21 @@
+%%% -*- erlang-indent-level: 2 -*-
+%%%-------------------------------------------------------------------
+%%% File : bs_save.erl
+%%% Author : Per Gustafsson
+%%% Purpose : Tests that compilation works for bs_save
+%%% Created : 1 Nov 2007
+%%%-------------------------------------------------------------------
+-module(bs_save).
+
+-export([test/0]).
+
+test() ->
+ {[16257, 1], <<0>>} = inc_on_ones(<<255,1,128,1,128,0>>, 0, [], 5),
+ ok.
+
+inc_on_ones(Buffer, _Av, Al, 0) ->
+ {lists:reverse(Al), Buffer};
+inc_on_ones(<<1:1, H:7, T/binary>>, Av, Al, Len) ->
+ inc_on_ones(T, (Av bsl 7) bor H, Al, Len-1);
+inc_on_ones(<<H, T/binary>>, Av, Al, Len) ->
+ inc_on_ones(T, 0, [((Av bsl 7) bor H)|Al], Len-1).
diff --git a/lib/hipe/test/bs_SUITE_data/bs_shell_native.erl b/lib/hipe/test/bs_SUITE_data/bs_shell_native.erl
new file mode 100644
index 0000000000..b438f8d9ef
--- /dev/null
+++ b/lib/hipe/test/bs_SUITE_data/bs_shell_native.erl
@@ -0,0 +1,275 @@
+%%% -*- erlang-indent-level: 2 -*-
+%%%-------------------------------------------------------------------
+%%% File : bs_shell_native.erl
+%%% Author : Per Gustafsson <[email protected]>
+%%% Purpose : Tests that the Erlang shell works well when in native
+%%% Created : 6 Sep 2006
+%%%-------------------------------------------------------------------
+-module(bs_shell_native).
+
+-export([prepare_for_test/0, test/0]).
+%% These need to be exported so that we emulate calling them from the shell
+-export([parse_and_eval/1, receiver/1, receiver_alot/1, send_alot/3]).
+
+%% This makes sure the shell runs native code
+prepare_for_test() ->
+ lists:foreach(fun (M) -> {ok, M} = hipe:c(M) end, [erl_bits, erl_eval]).
+
+test() ->
+ ok = eval_bits_in_shell(),
+ ok = eval_bin_comp_in_shell(),
+ ok.
+
+%%--------------------------------------------------------------------
+%% Tests for bit stream operations including matching, construction
+%% and binary_to_list, list_to_binary in the shell
+eval_bits_in_shell() ->
+ <<1:100>> = parse_and_eval("<<1:100>> = <<1:100>>."),
+ ok = match(7),
+ ok = match(9),
+ ok = match1(15),
+ ok = match1(31),
+ ok = horrid_match(),
+ ok = test_bitstr(),
+ ok = test_bitsize(),
+ ok = asymmetric_tests(),
+ ok = big_asymmetric_tests(),
+ ok = binary_to_and_from_list(),
+ ok = big_binary_to_and_from_list(),
+ ok = send_and_receive(),
+ ok = send_and_receive_alot(),
+ ok.
+
+parse_and_eval(String) ->
+ {ok, Toks, _} = erl_scan:string(String),
+ {ok, Exprs} = erl_parse:parse_exprs(Toks),
+ Bnds = erl_eval:new_bindings(),
+ case erl_eval:exprs(Exprs, Bnds) of
+ {value, V, _} ->
+ V;
+ V ->
+ V
+ end.
+
+match(N) ->
+ Str = "N =" ++ integer_to_list(N) ++ ", <<0:N>> = <<0:N>>.",
+ <<0:N>> = parse_and_eval(Str),
+ ok.
+
+match1(N) ->
+ Str = "N =" ++ integer_to_list(N) ++ ", <<42:N/little>> = <<42:N/little>>.",
+ <<42:N/little>> = parse_and_eval(Str),
+ ok.
+
+test_bitsize() ->
+ 101 = parse_and_eval("101 = erlang:bit_size(<<1:101>>)."),
+ 1001 = parse_and_eval("1001 = erlang:bit_size(<<1:1001>>)."),
+ 80 = parse_and_eval("80 = erlang:bit_size(<<1:80>>)."),
+ 800 = parse_and_eval("800 = erlang:bit_size(<<1:800>>)."),
+ S =
+ "Bin = <<0:16#1000000>>,"
+ "BigBin = list_to_bitstring([Bin||_ <- lists:seq(1,16#10)] ++ [<<1:1>>]),"
+ "16#10000001 = erlang:bit_size(BigBin).",
+ 16#10000001 = parse_and_eval(S),
+ %% Only run these on computers with lots of memory
+ %% HugeBin = list_to_bitstring([BigBin||_ <- lists:seq(1,16#10)]++[<<1:1>>]),
+ %% 16#100000011 = erlang:bit_size(HugeBin),
+ 0 = parse_and_eval("0 = erlang:bit_size(<<>>)."),
+ ok.
+
+horrid_match() ->
+ S = "<<1:4,B:24/bitstring>> = <<1:4,42:24/little>>, <<42:24/little>> = B.",
+ <<42:24/little>> = parse_and_eval(S),
+ ok.
+
+test_bitstr() ->
+ S =
+ "<<1:7,B/bitstring>> = <<1:7,<<1:1,6>>/bitstring>>,"
+ "<<1:1,6>> = B,"
+ "B = <<1:1,6>>.",
+ <<1:1,6>> = parse_and_eval(S),
+ ok.
+
+asymmetric_tests() ->
+ <<1:12>> = parse_and_eval("<<1:12>> = <<0,1:4>>."),
+ <<0,1:4>> = parse_and_eval("<<0,1:4>> = <<1:12>>."),
+ S1 =
+ "<<1:1,X/bitstring>> = <<128,255,0,0:2>>,"
+ "<<1,254,0,0:1>> = X,"
+ "X = <<1,254,0,0:1>>.",
+ <<1,254,0,0:1>> = parse_and_eval(S1),
+ S2 =
+ "<<1:1,X1:25/bitstring>> = <<128,255,0,0:2>>,"
+ "<<1,254,0,0:1>> = X1,"
+ "X1 = <<1,254,0,0:1>>.",
+ <<1,254,0,0:1>> = parse_and_eval(S2),
+ ok.
+
+big_asymmetric_tests() ->
+ <<1:875,1:12>> = parse_and_eval("<<1:875,1:12>> = <<1:875,0,1:4>>."),
+ <<1:875,0,1:4>> = parse_and_eval("<<1:875,0,1:4>> = <<1:875,1:12>>."),
+ S1 =
+ "<<1:1,X/bitstring>> = <<128,255,0,0:2,1:875>>,"
+ "<<1,254,0,0:1,1:875>> = X,"
+ "X = <<1,254,0,0:1,1:875>>.",
+ <<1,254,0,0:1,1:875>> = parse_and_eval(S1),
+ S2 =
+ "<<1:1,X1:900/bitstring>> = <<128,255,0,0:2,1:875>>,"
+ "<<1,254,0,0:1,1:875>> = X1,"
+ "X1 = <<1,254,0,0:1,1:875>>.",
+ parse_and_eval(S2),
+ ok.
+
+binary_to_and_from_list() ->
+ <<1:7>> = parse_and_eval("list_to_bitstring(bitstring_to_list(<<1:7>>))."),
+ <<1,2,3,4,1:1>> = parse_and_eval("list_to_bitstring(bitstring_to_list(<<1,2,3,4,1:1>>))."),
+ [1,2,3,4,<<1:1>>] = parse_and_eval("bitstring_to_list(<<1,2,3,4,1:1>>)."),
+ <<1:1,1,2,3,4>> = parse_and_eval("list_to_bitstring([<<1:1>>,1,2,3,4])."),
+ [128,129,1,130,<<0:1>>] = parse_and_eval("bitstring_to_list(<<1:1,1,2,3,4>>)."),
+ ok.
+
+big_binary_to_and_from_list() ->
+ S1 = "erlang:list_to_bitstring(bitstring_to_list(<<1:800,2,3,4,1:1>>)).",
+ <<1:800,2,3,4,1:1>> = parse_and_eval(S1),
+ S2 = "erlang:bitstring_to_list(<<1,2,3,4,1:800,1:1>>).",
+ [1,2,3,4|_Rest1] = parse_and_eval(S2),
+ S3 = "erlang:list_to_bitstring([<<1:801>>,1,2,3,4]).",
+ <<1:801,1,2,3,4>> = parse_and_eval(S3),
+ ok.
+
+send_and_receive() ->
+ S =
+ "Bin = <<1,2:7>>,"
+ "Pid = spawn(fun() -> bs_shell_native:receiver(Bin) end),"
+ "Pid ! {self(),<<1:7,8:5,Bin/bitstring>>},"
+ "receive ok -> ok end.",
+ parse_and_eval(S).
+
+receiver(Bin) ->
+ receive
+ {Pid, <<1:7,8:5,Bin/bitstring>>} ->
+ Pid ! ok
+ end.
+
+send_and_receive_alot() ->
+ S =
+ "Bin = <<1:1000001>>,"
+ "Pid = spawn(fun() -> bs_shell_native:receiver_alot(Bin) end),"
+ "bs_shell_native:send_alot(100,Bin,Pid).",
+ parse_and_eval(S).
+
+send_alot(N,Bin,Pid) when N > 0 ->
+ Pid ! {self(),<<1:7,8:5,Bin/bitstring>>},
+ receive
+ ok ->
+ ok
+ end,
+ send_alot(N-1,Bin,Pid);
+send_alot(0,_Bin,Pid) ->
+ Pid ! no_more,
+ ok.
+
+receiver_alot(Bin) ->
+ receive
+ {Pid, <<1:7,8:5,Bin/bitstring>>} ->
+ Pid ! ok;
+ no_more -> ok
+ end,
+ receiver_alot(Bin).
+
+%%--------------------------------------------------------------------
+
+eval_bin_comp_in_shell() ->
+ ok = byte_aligned(),
+ ok = bit_aligned(),
+ ok = extended_byte_aligned(),
+ ok = extended_bit_aligned(),
+ ok = mixed(),
+ ok.
+
+byte_aligned() ->
+ <<"abcdefg">> =
+ parse_and_eval("<<\"abcdefg\">> = << <<(X+32)>> || <<X>> <= <<\"ABCDEFG\">> >>."),
+ <<1:32/little,2:32/little,3:32/little,4:32/little>> =
+ parse_and_eval("<<1:32/little,2:32/little,3:32/little,4:32/little>> =
+ << <<X:32/little>> || <<X:32>> <= <<1:32,2:32,3:32,4:32>> >>."),
+ <<1:32/little,2:32/little,3:32/little,4:32/little>> =
+ parse_and_eval("<<1:32/little,2:32/little,3:32/little,4:32/little>> =
+ << <<X:32/little>> || <<X:16>> <= <<1:16,2:16,3:16,4:16>> >>."),
+ ok.
+
+bit_aligned() ->
+ <<$a:7,$b:7,$c:7,$d:7,$e:7,$f:7,$g:7>> =
+ parse_and_eval("<<$a:7,$b:7,$c:7,$d:7,$e:7,$f:7,$g:7>> =
+ << <<(X+32):7>> || <<X>> <= <<\"ABCDEFG\">> >>."),
+ <<"ABCDEFG">> =
+ parse_and_eval("<<\"ABCDEFG\">> =
+ << <<(X-32)>> || <<X:7>> <= <<$a:7,$b:7,$c:7,$d:7,$e:7,$f:7,$g:7>> >>."),
+ <<1:31/little,2:31/little,3:31/little,4:31/little>> =
+ parse_and_eval("<<1:31/little,2:31/little,3:31/little,4:31/little>> =
+ << <<X:31/little>> || <<X:31>> <= <<1:31,2:31,3:31,4:31>> >>."),
+ <<1:31/little,2:31/little,3:31/little,4:31/little>> =
+ parse_and_eval("<<1:31/little,2:31/little,3:31/little,4:31/little>> =
+ << <<X:31/little>> || <<X:15>> <= <<1:15,2:15,3:15,4:15>> >>."),
+ ok.
+
+extended_byte_aligned() ->
+ <<"abcdefg">> =
+ parse_and_eval("<<\"abcdefg\">> = << <<(X+32)>> || X <- \"ABCDEFG\" >>."),
+ "abcdefg" =
+ parse_and_eval("\"abcdefg\" = [(X+32) || <<X>> <= <<\"ABCDEFG\">>]."),
+ <<1:32/little,2:32/little,3:32/little,4:32/little>> =
+ parse_and_eval("<<1:32/little,2:32/little,3:32/little,4:32/little>> =
+ << <<X:32/little>> || X <- [1,2,3,4] >>."),
+ [256,512,768,1024] =
+ parse_and_eval("[256,512,768,1024] =
+ [X || <<X:16/little>> <= <<1:16,2:16,3:16,4:16>>]."),
+ ok.
+
+extended_bit_aligned() ->
+ <<$a:7,$b:7,$c:7,$d:7,$e:7,$f:7,$g:7>> =
+ parse_and_eval("<<$a:7,$b:7,$c:7,$d:7,$e:7,$f:7,$g:7>> =
+ << <<(X+32):7>> || X <- \"ABCDEFG\" >>."),
+ "ABCDEFG" =
+ parse_and_eval("\"ABCDEFG\" = [(X-32) || <<X:7>> <=
+<<$a:7,$b:7,$c:7,$d:7,$e:7,$f:7,$g:7>>]."),
+ <<1:31/little,2:31/little,3:31/little,4:31/little>> =
+ parse_and_eval("<<1:31/little,2:31/little,3:31/little,4:31/little>> =
+ << <<X:31/little>> || X <- [1,2,3,4] >>."),
+ [256,512,768,1024] =
+ parse_and_eval("[256,512,768,1024] =
+ [X || <<X:15/little>> <= <<1:15,2:15,3:15,4:15>>]."),
+ ok.
+
+mixed() ->
+ <<2,3,3,4,4,5,5,6>> =
+ parse_and_eval("<<2,3,3,4,4,5,5,6>> =
+ << <<(X+Y)>> || <<X>> <= <<1,2,3,4>>, <<Y>> <= <<1,2>> >>."),
+ <<2,3,3,4,4,5,5,6>> =
+ parse_and_eval("<<2,3,3,4,4,5,5,6>> =
+ << <<(X+Y)>> || <<X>> <= <<1,2,3,4>>, Y <- [1,2] >>."),
+ <<2,3,3,4,4,5,5,6>> =
+ parse_and_eval("<<2,3,3,4,4,5,5,6>> =
+ << <<(X+Y)>> || X <- [1,2,3,4], Y <- [1,2] >>."),
+ [2,3,3,4,4,5,5,6] =
+ parse_and_eval("[2,3,3,4,4,5,5,6] =
+ [(X+Y) || <<X>> <= <<1,2,3,4>>, <<Y>> <= <<1,2>>]."),
+ [2,3,3,4,4,5,5,6] =
+ parse_and_eval("[2,3,3,4,4,5,5,6] =
+ [(X+Y) || <<X>> <= <<1,2,3,4>>, Y <- [1,2]]."),
+ <<2:3,3:3,3:3,4:3,4:3,5:3,5:3,6:3>> =
+ parse_and_eval("<<2:3,3:3,3:3,4:3,4:3,5:3,5:3,6:3>> =
+ << <<(X+Y):3>> || <<X:3>> <= <<1:3,2:3,3:3,4:3>>, <<Y:3>> <= <<1:3,2:3>> >>."),
+ <<2:3,3:3,3:3,4:3,4:3,5:3,5:3,6:3>> =
+ parse_and_eval("<<2:3,3:3,3:3,4:3,4:3,5:3,5:3,6:3>> =
+ << <<(X+Y):3>> || <<X:3>> <= <<1:3,2:3,3:3,4:3>>, Y <- [1,2] >>."),
+ <<2:3,3:3,3:3,4:3,4:3,5:3,5:3,6:3>> =
+ parse_and_eval("<<2:3,3:3,3:3,4:3,4:3,5:3,5:3,6:3>> =
+ << <<(X+Y):3>> || X <- [1,2,3,4], Y <- [1,2] >>."),
+ [2,3,3,4,4,5,5,6] =
+ parse_and_eval("[2,3,3,4,4,5,5,6] =
+ [(X+Y) || <<X:3>> <= <<1:3,2:3,3:3,4:3>>, <<Y:3>> <= <<1:3,2:3>>]."),
+ [2,3,3,4,4,5,5,6] =
+ parse_and_eval("[2,3,3,4,4,5,5,6] =
+ [(X+Y) || <<X:3>> <= <<1:3,2:3,3:3,4:3>>, Y <- [1,2]]."),
+ ok.
diff --git a/lib/hipe/test/bs_SUITE_data/bs_split.erl b/lib/hipe/test/bs_SUITE_data/bs_split.erl
new file mode 100644
index 0000000000..2e52308a77
--- /dev/null
+++ b/lib/hipe/test/bs_SUITE_data/bs_split.erl
@@ -0,0 +1,105 @@
+%% -*- erlang-indent-level: 2 -*-
+%%--------------------------------------------------------------------
+
+-module(bs_split).
+
+-export([test/0]).
+
+test() ->
+ Funs = [fun byte_split_binary/0, fun bit_split_binary/0, fun z_split/0],
+ lists:foreach(fun (F) -> ok = F() end, Funs).
+
+%%--------------------------------------------------------------------
+
+byte_split_binary() ->
+ L = lists:seq(0, 57),
+ B = mkbin(L),
+ byte_split(L, B, byte_size(B)).
+
+byte_split(L, B, Pos) when Pos >= 0 ->
+ Sz1 = Pos,
+ Sz2 = byte_size(B) - Pos,
+ bs1(L, B, Pos, Sz1, Sz2);
+byte_split(_, _, _) -> ok.
+
+bs1(L, B, Pos, Sz1, Sz2) ->
+ <<B1:Sz1/binary, B2:Sz2/binary>> = B,
+ bs2(L, B, Pos, B1, B2).
+
+bs2(L, B, Pos, B1, B2)->
+ B1 = list_to_binary(lists:sublist(L, 1, Pos)),
+ bs3(L, B, Pos, B2).
+
+bs3(L, B, Pos, B2) ->
+ B2 = list_to_binary(lists:nthtail(Pos, L)),
+ byte_split(L, B, Pos-1).
+
+%%--------------------------------------------------------------------
+
+bit_split_binary() ->
+ Fun = fun(Bin, List, SkipBef, N) ->
+ SkipAft = bit_size(Bin) - N - SkipBef,
+ %% io:format("~p, ~p, ~p", [SkipBef,N,SkipAft]),
+ <<_I1:SkipBef,OutBin:N/binary-unit:1,_I2:SkipAft>> = Bin,
+ OutBin = make_bin_from_list(List, N)
+ end,
+ bit_split_binary1(Fun, erlang:md5(<<1,2,3>>)).
+
+bit_split_binary1(Action, Bin) ->
+ BitList = bits_to_list(binary_to_list(Bin), 16#80),
+ bit_split_binary2(Action, Bin, BitList, 0).
+
+bit_split_binary2(Action, Bin, [_|T]=List, Bef) ->
+ bit_split_binary3(Action, Bin, List, Bef, bit_size(Bin)),
+ bit_split_binary2(Action, Bin, T, Bef+1);
+bit_split_binary2(_Action, _Bin, [], _Bef) -> ok.
+
+bit_split_binary3(Action, Bin, List, Bef, Aft) when Bef =< Aft ->
+ Action(Bin, List, Bef, (Aft-Bef) div 8 * 8),
+ bit_split_binary3(Action, Bin, List, Bef, Aft-8);
+bit_split_binary3(_, _, _, _, _) -> ok.
+
+make_bin_from_list(_List, 0) ->
+ mkbin([]);
+make_bin_from_list(List, N) ->
+ list_to_binary([make_int(List, 8, 0),
+ make_bin_from_list(lists:nthtail(8, List), N-8)]).
+
+make_int(_List, 0, Acc) -> Acc;
+make_int([H|T], N, Acc) -> make_int(T, N-1, Acc bsl 1 bor H).
+
+bits_to_list([_|T], 0) -> bits_to_list(T, 16#80);
+bits_to_list([H|_]=List, Mask) ->
+ [case H band Mask of
+ 0 -> 0;
+ _ -> 1
+ end|bits_to_list(List, Mask bsr 1)];
+bits_to_list([], _) -> [].
+
+mkbin(L) when is_list(L) -> list_to_binary(L).
+
+%%--------------------------------------------------------------------
+%% Splits a series of null terminated segments of a binary without
+%% creating any new sub-binaries until the zero is found.
+
+z_split() ->
+ [<<61,62,63>>] = z_split(<<61,62,63>>),
+ [<<61,62,63>>, <<>>] = z_split(<<61,62,63,0>>),
+ [<<61,62,63>>, <<64>>] = z_split(<<61,62,63,0,64>>),
+ [<<61,62,63>>, <<64,65,66>>] = z_split(<<61,62,63,0,64,65,66>>),
+ [<<61,62>>, <<64>>, <<>>, <<65,66>>] = z_split(<<61,62,0,64,0,0,65,66>>),
+ ok.
+
+z_split(B) when is_binary(B) ->
+ z_split(B, 0).
+
+z_split(B, N) ->
+ case B of
+ <<_B1:N/binary,0,_B2/binary>> -> % use skip_bits for B1, B2
+ <<B1:N/binary,_,B2/binary>> = B, % and postpone the matching
+ [B1 | z_split(B2)];
+ <<_:N/binary>> ->
+ [B];
+ _ ->
+ z_split(B, N+1)
+ end.
diff --git a/lib/hipe/test/bs_SUITE_data/bs_system_limit_32.erl b/lib/hipe/test/bs_SUITE_data/bs_system_limit_32.erl
new file mode 100644
index 0000000000..eccb0083bd
--- /dev/null
+++ b/lib/hipe/test/bs_SUITE_data/bs_system_limit_32.erl
@@ -0,0 +1,26 @@
+%%% -*- erlang-indent-level: 2 -*-
+%%%-------------------------------------------------------------------
+%%% File : bs_system_limit_32.erl
+%%% Author : Per Gustafsson <[email protected]>
+%%% Purpose : Checks binary system limits on 32-bit machines
+%%% Created : 14 May 2008
+%%%-------------------------------------------------------------------
+-module(bs_system_limit_32).
+
+-export([test/0]).
+
+test() ->
+ case erlang:system_info(wordsize) of
+ 4 -> system_limit_32();
+ 8 -> ok
+ end.
+
+system_limit_32() ->
+ {'EXIT', {badarg, _}} = (catch <<42:(id(-1))>>),
+ {'EXIT', {badarg, _}} = (catch <<42:(id(-389739873536870912))/unit:8>>),
+ {'EXIT', {system_limit, _}} = (catch <<32:536870912/unit:8>>),
+ {'EXIT', {system_limit, _}} = (catch <<42:(id(536870912))/unit:8>>),
+ {'EXIT', {system_limit, _}} = (catch <<42:(id(536870912))/unit:8,1:1>>),
+ ok.
+
+id(X) -> X.
diff --git a/lib/hipe/test/bs_SUITE_data/bs_utf.erl b/lib/hipe/test/bs_SUITE_data/bs_utf.erl
new file mode 100644
index 0000000000..f50ae08964
--- /dev/null
+++ b/lib/hipe/test/bs_SUITE_data/bs_utf.erl
@@ -0,0 +1,18 @@
+%% -*- erlang-indent-level: 2 -*-
+%%-------------------------------------------------------------------
+%% Purpose: test support for UTF datatypes in binaries - INCOMPLETE
+%%-------------------------------------------------------------------
+
+-module(bs_utf).
+
+-export([test/0]).
+
+test() ->
+ <<65>> = b65utf8(),
+ ok = m(<<65>>).
+
+m(<<65/utf8>>) ->
+ ok.
+
+b65utf8() ->
+ <<65/utf8>>.
diff --git a/lib/hipe/test/bs_SUITE_data/bs_var_segs.erl b/lib/hipe/test/bs_SUITE_data/bs_var_segs.erl
new file mode 100644
index 0000000000..a20df04b53
--- /dev/null
+++ b/lib/hipe/test/bs_SUITE_data/bs_var_segs.erl
@@ -0,0 +1,76 @@
+%% -*- erlang-indent-level: 2 -*-
+%%--------------------------------------------------------------------
+%% Author : Kostis Sagonas
+%% Purpose : These tests are intended to test the construction and
+%% matching of binaries using variable sizes
+%% Notes :
+%% - Added test that crashed BEAM compiler
+%% - Added test that crashed when segments of size zero were used
+%% and one that did not convert integers to floats when constructing
+%% binaries.
+%% - Added a construction test which crashed from core because of
+%% problems with the effect flag (2004/11/15)
+%%--------------------------------------------------------------------
+-module(bs_var_segs).
+
+-export([test/0]).
+
+test() ->
+ N1 = 18,
+ A1 = 2,
+ A1 = match1(N1, <<1:12, 2:N1, A1:2>>),
+ A1 = match2(N1, <<1:12, 2:N1/integer-little, A1:2>>),
+ N3 = 3,
+ A3 = <<1,2,3>>,
+ B3 = 2,
+ {A3, B3} = match3(N3, <<1:12, A3:N3/binary, B3:4>>),
+ N4 = 12,
+ B4 = <<1,2,3>>,
+ A4 = 2,
+ {A4, B4} = match4(N4, <<1:N4, A4:4, B4/binary>>),
+ Y = <<5>>,
+ Y = match5(a, Y),
+ <<73>> = gen1(8, 0, <<73>>),
+ <<171>> = gen2(8, 7, 2#10101010101010101),
+ <<0:64>> = construct(),
+ <<0:32>> = construct2(0),
+ ok = in_guard(<<16#BCD:14,3:2>>, 16#BCD),
+ ok.
+
+construct() ->
+ <<0:64/float>>.
+
+construct2(X) ->
+ <<X:32/little>>.
+
+match1(N, Bin) ->
+ <<1:12, 2:N, A:2>>=Bin,
+ A.
+
+match2(N, Bin) ->
+ <<1:12, 2:N/integer-little, A:2>>=Bin,
+ A.
+
+match3(N, Bin) ->
+ <<1:12, A:N/binary, B:4>>=Bin,
+ {A,B}.
+
+match4(N, Bin) ->
+ <<1:N, A:4, B/binary>>=Bin,
+ {A,B}.
+
+match5(X, Y) ->
+ case X of
+ a ->
+ Y2 = 8
+ end,
+ <<5:Y2>> = Y.
+
+gen1(N, S, A) ->
+ <<A:S/binary-unit:1, A:(N-S)/binary-unit:1>>.
+
+gen2(N, S, A) ->
+ <<A:S/little, A:(N-S)/little>>.
+
+in_guard(Bin, A) when <<A:14,3:2>> == Bin -> ok;
+in_guard(_, _) -> no.
diff --git a/lib/hipe/test/hipe.spec b/lib/hipe/test/hipe.spec
new file mode 100644
index 0000000000..2894f40354
--- /dev/null
+++ b/lib/hipe/test/hipe.spec
@@ -0,0 +1,6 @@
+%% -*- erlang -*-
+
+{alias, tests, "../hipe_test"}.
+
+{suites, tests, all}.
+
diff --git a/lib/hipe/test/hipe_SUITE.erl b/lib/hipe/test/hipe_SUITE.erl
new file mode 100644
index 0000000000..554bc972f6
--- /dev/null
+++ b/lib/hipe/test/hipe_SUITE.erl
@@ -0,0 +1,55 @@
+%% ``The contents of this file are subject to the Erlang Public License,
+%% Version 1.1, (the "License"); you may not use this file except in
+%% compliance with the License. You should have received a copy of the
+%% Erlang Public License along with this software. If not, it can be
+%% retrieved via the world wide web at http://www.erlang.org/.
+%%
+%% Software distributed under the License is distributed on an "AS IS"
+%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
+%% the License for the specific language governing rights and limitations
+%% under the License.
+%%
+%% The Initial Developer of the Original Code is Ericsson Utvecklings AB.
+%% Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings
+%% AB. All Rights Reserved.''
+%%
+-module(hipe_SUITE).
+
+-compile([export_all]).
+-include_lib("common_test/include/ct.hrl").
+
+suite() ->
+ [{ct_hooks, [ts_install_cth]}].
+
+all() ->
+ [app, appup].
+
+groups() ->
+ [].
+
+init_per_suite(Config) ->
+ case erlang:system_info(hipe_architecture) of
+ undefined -> {skip, "HiPE not available or enabled"};
+ _ -> Config
+ end.
+
+end_per_suite(_Config) ->
+ ok.
+
+init_per_group(_GroupName, Config) ->
+ Config.
+
+end_per_group(_GroupName, Config) ->
+ Config.
+
+app() ->
+ [{doc, "Test that the hipe app file is ok"}].
+app(Config) when is_list(Config) ->
+ ok = ?t:app_test(hipe, tolerant).
+
+appup() ->
+ [{doc, "Test that the hipe appup file is ok"}].
+appup(Config) when is_list(Config) ->
+ AppupFile = "hipe.appup",
+ AppupPath = filename:join([code:lib_dir(hipe), "ebin", AppupFile]),
+ {ok, [{_Vsn, [], []}]} = file:consult(AppupPath).
diff --git a/lib/hipe/test/hipe_testsuite_driver.erl b/lib/hipe/test/hipe_testsuite_driver.erl
new file mode 100644
index 0000000000..5f05a716bc
--- /dev/null
+++ b/lib/hipe/test/hipe_testsuite_driver.erl
@@ -0,0 +1,201 @@
+-module(hipe_testsuite_driver).
+
+-export([create_all_suites/0, run/3]).
+
+-include_lib("kernel/include/file.hrl").
+
+-type testcase() :: atom().
+-type file_type() :: 'device' | 'directory' | 'regular' | 'other'.
+-type ext_posix() :: file:posix() | 'badarg'.
+
+-define(suite_suffix, "_SUITE").
+-define(data_folder, "_data").
+-define(suite_data, ?suite_suffix ++ ?data_folder).
+
+-record(suite, {suitename :: string(),
+ outputfile :: file:io_device(),
+ testcases :: [testcase()]}).
+
+-spec create_all_suites() -> 'ok'.
+
+create_all_suites() ->
+ {ok, Cwd} = file:get_cwd(),
+ Suites = get_suites(Cwd),
+ lists:foreach(fun create_suite/1, Suites).
+
+-spec get_suites(file:filename()) -> [string()].
+
+get_suites(Dir) ->
+ case file:list_dir(Dir) of
+ {error, _} -> [];
+ {ok, Filenames} ->
+ FullFilenames = [filename:join(Dir, F) || F <- Filenames],
+ Dirs = [suffix(filename:basename(F), ?suite_data) ||
+ F <- FullFilenames,
+ file_type(F) =:= {ok, 'directory'}],
+ [S || {yes, S} <- Dirs]
+ end.
+
+suffix(String, Suffix) ->
+ case string:rstr(String, Suffix) of
+ 0 -> no;
+ Index ->
+ case string:substr(String, Index) =:= Suffix of
+ true -> {yes, string:sub_string(String, 1, Index-1)};
+ false -> no
+ end
+ end.
+
+-spec file_type(file:filename()) -> {ok, file_type()} | {error, ext_posix()}.
+
+file_type(Filename) ->
+ case file:read_file_info(Filename) of
+ {ok, FI} -> {ok, FI#file_info.type};
+ Error -> Error
+ end.
+
+-spec create_suite(string()) -> 'ok'.
+
+create_suite(SuiteName) ->
+ {ok, Cwd} = file:get_cwd(),
+ SuiteDirN = filename:join(Cwd, SuiteName ++ ?suite_data),
+ OutputFile = generate_suite_file(Cwd, SuiteName),
+ generate_suite(SuiteName, OutputFile, SuiteDirN).
+
+generate_suite_file(Cwd, SuiteName) ->
+ F = filename:join(Cwd, SuiteName ++ ?suite_suffix ++ ".erl"),
+ case file:open(F, [write]) of
+ {ok, IoDevice} -> IoDevice;
+ {error, _} = E -> exit({E, F})
+ end.
+
+generate_suite(SuiteName, OutputFile, SuiteDirN) ->
+ TestCases = list_testcases(SuiteDirN),
+ Suite = #suite{suitename = SuiteName, outputfile = OutputFile,
+ testcases = TestCases},
+ write_suite(Suite),
+ file:close(OutputFile).
+
+list_testcases(Dirname) ->
+ {ok, Files} = list_dir(Dirname, ".erl", true),
+ [list_to_atom(filename:basename(F, ".erl")) || F <- Files].
+
+-spec list_dir(file:filename(), string(), boolean()) ->
+ {error, ext_posix()} | {ok, [file:filename()]}.
+
+list_dir(Dir, Extension, Dirs) ->
+ case file:list_dir(Dir) of
+ {error, _} = Error -> Error;
+ {ok, Filenames} ->
+ FullFilenames = [filename:join(Dir, F) || F <- Filenames],
+ Matches1 = case Dirs of
+ true ->
+ [F || F <- FullFilenames,
+ file_type(F) =:= {ok, 'directory'}];
+ false -> []
+ end,
+ Matches2 = [F || F <- FullFilenames,
+ file_type(F) =:= {ok, 'regular'},
+ filename:extension(F) =:= Extension],
+ {ok, lists:sort(Matches1 ++ Matches2)}
+ end.
+
+write_suite(Suite) ->
+ write_header(Suite),
+ write_testcases(Suite).
+
+write_header(#suite{suitename = SuiteName, outputfile = OutputFile,
+ testcases = TestCases}) ->
+ Exports = format_export(TestCases),
+ TimeLimit = 2, %% with 1 it fails on some slow machines...
+ io:format(OutputFile,
+ "%% ATTENTION!\n"
+ "%% This is an automatically generated file. Do not edit.\n\n"
+ "-module(~s).\n\n"
+ "-export([suite/0, init_per_suite/0, init_per_suite/1,\n"
+ " end_per_suite/1, all/0]).\n"
+ "~s\n\n"
+ "-include_lib(\"common_test/include/ct.hrl\").\n\n"
+ "suite() ->\n"
+ " [{timetrap, {minutes, ~w}}].\n\n"
+ "init_per_suite() ->\n"
+ " [].\n\n"
+ "init_per_suite(Config) ->\n"
+ " case erlang:system_info(hipe_architecture) of\n"
+ " undefined -> {skip, \"HiPE not available or enabled\"};\n"
+ " _ -> Config\n"
+ " end.\n\n"
+ "end_per_suite(_Config) ->\n"
+ " ok.\n\n"
+ "all() ->\n"
+ " ~p.\n\n"
+ "test(Config, TestCase) ->\n"
+ " Dir = ?config(data_dir, Config),\n"
+ " OutDir = ?config(priv_dir, Config),\n"
+ " hipe_testsuite_driver:run(TestCase, Dir, OutDir)."
+ "\n\n",
+ [SuiteName ++ ?suite_suffix, Exports, TimeLimit, TestCases]).
+
+format_export(TestCases) ->
+ TL = [list_to_atom(atom_to_list(N)++"/1") || N <- TestCases],
+ TestCaseString = io_lib:format("-export(~p).", [TL]),
+ strip_quotes(lists:flatten(TestCaseString), []).
+
+strip_quotes([], Result) ->
+ lists:reverse(Result);
+strip_quotes([$' |Rest], Result) ->
+ strip_quotes(Rest, Result);
+strip_quotes([$\, |Rest], Result) ->
+ strip_quotes(Rest, [$\ , $\, |Result]);
+strip_quotes([C|Rest], Result) ->
+ strip_quotes(Rest, [C|Result]).
+
+write_testcases(#suite{outputfile = OutputFile, testcases = TestCases}) ->
+ lists:foreach(fun (T) -> write_testcase(OutputFile, T) end, TestCases).
+
+write_testcase(OutputFile, TestCase) ->
+ io:format(OutputFile,
+ "~p(Config) ->\n"
+ " test(Config, ~p).\n\n",
+ [TestCase, TestCase]).
+
+-spec run(atom(), string(), string()) -> 'ok'.
+
+run(TestCase, Dir, _OutDir) ->
+ F = filename:join(Dir, atom_to_list(TestCase) ++ ".erl"),
+ {ok, TestCase} = compile:file(F),
+ ok = try TestCase:prepare_for_test() catch _:_ -> ok end,
+ %% DataFiles = try TestCase:datafiles() catch _:_ -> [] end,
+ %% lists:foreach(fun (DF) ->
+ %% Src = filename:join(Dir, DF),
+ %% Dst = filename:join(OutDir, DF),
+ %% {ok, _} = file:copy(Src, Dst)
+ %% end, DataFiles),
+ %% try
+ ok = TestCase:test(),
+ HiPEOpts = try TestCase:hipe_options() catch error:undef -> [] end,
+ {ok, TestCase} = hipe:c(TestCase, HiPEOpts),
+ ok = TestCase:test(),
+ case is_llvm_opt_available() of
+ true ->
+ {ok, TestCase} = hipe:c(TestCase, [to_llvm|HiPEOpts]),
+ ok = TestCase:test();
+ false -> ok
+ end.
+ %% after
+ %% lists:foreach(fun (DF) -> ok end, % = file:delete(DF) end,
+ %% [filename:join(OutDir, D) || D <- DataFiles])
+ %% end.
+
+
+%% This function, which is supposed to check whether the right LLVM
+%% infrastructure is available, should be probably written in a better
+%% and more portable way and moved to the hipe application.
+
+is_llvm_opt_available() ->
+ OptStr = os:cmd("opt -version"),
+ SubStr = "LLVM version ", N = length(SubStr),
+ case string:str(OptStr, SubStr) of
+ 0 -> false;
+ S -> P = S + N, string:sub_string(OptStr, P, P + 2) >= "3.4"
+ end.
diff --git a/lib/hipe/test/maps_SUITE_data/maps_build_and_match_aliasing.erl b/lib/hipe/test/maps_SUITE_data/maps_build_and_match_aliasing.erl
new file mode 100644
index 0000000000..14d8320cdf
--- /dev/null
+++ b/lib/hipe/test/maps_SUITE_data/maps_build_and_match_aliasing.erl
@@ -0,0 +1,20 @@
+-module(maps_build_and_match_aliasing).
+-export([test/0]).
+
+test() ->
+ M1 = id(#{a=>1,b=>2,c=>3,d=>4}),
+ #{c:=C1=_=_=C2} = M1,
+ true = C1 =:= C2,
+ #{a:=A,a:=A,a:=A,b:=B,b:=B} = M1,
+ #{a:=A,a:=A,a:=A,b:=B,b:=B,b:=2} = M1,
+ #{a:=A=1,a:=A,a:=A,b:=B=2,b:=B,b:=2} = M1,
+ #{c:=C1, c:=_, c:=3, c:=_, c:=C2} = M1,
+ #{c:=C=_=3=_=C} = M1,
+
+ M2 = id(#{"a"=>1,"b"=>2,"c"=>3,"d"=>4}),
+ #{"a":=A2,"a":=A2,"a":=A2,"b":=B2,"b":=B2,"b":=2} = M2,
+ #{"a":=_,"a":=_,"a":=_,"b":=_,"b":=_,"b":=2} = M2,
+ ok.
+
+%% Use this function to avoid compile-time evaluation of an expression.
+id(I) -> I.
diff --git a/lib/hipe/test/maps_SUITE_data/maps_build_and_match_empty_val.erl b/lib/hipe/test/maps_SUITE_data/maps_build_and_match_empty_val.erl
new file mode 100644
index 0000000000..2abfa4e5b3
--- /dev/null
+++ b/lib/hipe/test/maps_SUITE_data/maps_build_and_match_empty_val.erl
@@ -0,0 +1,17 @@
+-module(maps_build_and_match_empty_val).
+-export([test/0]).
+
+test() ->
+ F = fun(#{ "hi":=_,{1,2}:=_,1337:=_}) -> ok end,
+ ok = F(id(#{"hi"=>ok,{1,2}=>ok,1337=>ok})),
+
+ %% error case
+ case (catch (F(id(#{"hi"=>ok})))) of
+ {'EXIT',{function_clause,_}} -> ok;
+ {'EXIT', {{case_clause,_},_}} -> {comment,inlined};
+ Other ->
+ test_server:fail({no_match, Other})
+ end.
+
+%% Use this function to avoid compile-time evaluation of an expression.
+id(I) -> I.
diff --git a/lib/hipe/test/maps_SUITE_data/maps_build_and_match_literals.erl b/lib/hipe/test/maps_SUITE_data/maps_build_and_match_literals.erl
new file mode 100644
index 0000000000..dc2c63fab2
--- /dev/null
+++ b/lib/hipe/test/maps_SUITE_data/maps_build_and_match_literals.erl
@@ -0,0 +1,40 @@
+-module(maps_build_and_match_literals).
+-export([test/0]).
+
+test() ->
+ #{} = id(#{}),
+ #{1:=a} = id(#{1=>a}),
+ #{1:=a,2:=b} = id(#{1=>a,2=>b}),
+ #{1:=a,2:=b,3:="c"} = id(#{1=>a,2=>b,3=>"c"}),
+ #{1:=a,2:=b,3:="c","4":="d"} = id(#{1=>a,2=>b,3=>"c","4"=>"d"}),
+ #{1:=a,2:=b,3:="c","4":="d",<<"5">>:=<<"e">>} =
+ id(#{1=>a,2=>b,3=>"c","4"=>"d",<<"5">>=><<"e">>}),
+ #{1:=a,2:=b,3:="c","4":="d",<<"5">>:=<<"e">>,{"6",7}:="f"} =
+ id(#{1=>a,2=>b,3=>"c","4"=>"d",<<"5">>=><<"e">>,{"6",7}=>"f"}),
+ #{1:=a,2:=b,3:="c","4":="d",<<"5">>:=<<"e">>,{"6",7}:="f",8:=g} =
+ id(#{1=>a,2=>b,3=>"c","4"=>"d",<<"5">>=><<"e">>,{"6",7}=>"f",8=>g}),
+
+ #{<<"hi all">> := 1} = id(#{<<"hi",32,"all">> => 1}),
+
+ #{a:=X,a:=X=3,b:=4} = id(#{a=>3,b=>4}), % weird but ok =)
+
+ #{ a:=#{ b:=#{c := third, b:=second}}, b:=first} =
+ id(#{ b=>first, a=>#{ b=>#{c => third, b=> second}}}),
+
+ M = #{ map_1=>#{ map_2=>#{value_3 => third}, value_2=> second}, value_1=>first},
+ M = #{ map_1:=#{ map_2:=#{value_3 := third}, value_2:= second}, value_1:=first} =
+ id(#{ map_1=>#{ map_2=>#{value_3 => third}, value_2=> second}, value_1=>first}),
+
+ %% nil key
+ #{[]:=ok,1:=2} = id(#{[]=>ok,1=>2}),
+
+ %% error case
+ {'EXIT',{{badmatch,_},_}} = (catch (#{x:=3,x:=2} = id(#{x=>3}))),
+ {'EXIT',{{badmatch,_},_}} = (catch (#{x:=2} = id(#{x=>3}))),
+ {'EXIT',{{badmatch,_},_}} = (catch (#{x:=3} = id({a,b,c}))),
+ {'EXIT',{{badmatch,_},_}} = (catch (#{x:=3} = id(#{y=>3}))),
+ {'EXIT',{{badmatch,_},_}} = (catch (#{x:=3} = id(#{x=>"three"}))),
+ ok.
+
+%% Use this function to avoid compile-time evaluation of an expression.
+id(I) -> I.
diff --git a/lib/hipe/test/maps_SUITE_data/maps_build_and_match_over_alloc.erl b/lib/hipe/test/maps_SUITE_data/maps_build_and_match_over_alloc.erl
new file mode 100644
index 0000000000..dae6f64e5f
--- /dev/null
+++ b/lib/hipe/test/maps_SUITE_data/maps_build_and_match_over_alloc.erl
@@ -0,0 +1,16 @@
+-module(maps_build_and_match_over_alloc).
+-export([test/0]).
+
+test() ->
+ Ls = id([1,2,3]),
+ V0 = [a|Ls],
+ M0 = id(#{ "a" => V0 }),
+ #{ "a" := V1 } = M0,
+ V2 = id([c|Ls]),
+ M2 = id(#{ "a" => V2 }),
+ #{ "a" := V3 } = M2,
+ {[a,1,2,3],[c,1,2,3]} = id({V1,V3}),
+ ok.
+
+%% Use this function to avoid compile-time evaluation of an expression.
+id(I) -> I.
diff --git a/lib/hipe/test/maps_SUITE_data/maps_build_and_match_val.erl b/lib/hipe/test/maps_SUITE_data/maps_build_and_match_val.erl
new file mode 100644
index 0000000000..284f69e06c
--- /dev/null
+++ b/lib/hipe/test/maps_SUITE_data/maps_build_and_match_val.erl
@@ -0,0 +1,23 @@
+-module(maps_build_and_match_val).
+-export([test/0]).
+
+test() ->
+ F = fun
+ (#{ "hi" := first, v := V}) -> {1,V};
+ (#{ "hi" := second, v := V}) -> {2,V}
+ end,
+
+
+ {1,"hello"} = F(id(#{"hi"=>first,v=>"hello"})),
+ {2,"second"} = F(id(#{"hi"=>second,v=>"second"})),
+
+ %% error case
+ case (catch (F(id(#{"hi"=>ok})))) of
+ {'EXIT',{function_clause,_}} -> ok;
+ {'EXIT', {{case_clause,_},_}} -> {comment,inlined};
+ Other ->
+ test_server:fail({no_match, Other})
+ end.
+
+%% Use this function to avoid compile-time evaluation of an expression.
+id(I) -> I.
diff --git a/lib/hipe/test/maps_SUITE_data/maps_expand_map_update.erl b/lib/hipe/test/maps_SUITE_data/maps_expand_map_update.erl
new file mode 100644
index 0000000000..df0f77ea47
--- /dev/null
+++ b/lib/hipe/test/maps_SUITE_data/maps_expand_map_update.erl
@@ -0,0 +1,7 @@
+-module(maps_expand_map_update).
+-export([test/0]).
+
+test() ->
+ M = #{<<"hello">> => <<"world">>}#{<<"hello">> := <<"les gens">>},
+ #{<<"hello">> := <<"les gens">>} = M,
+ ok.
diff --git a/lib/hipe/test/maps_SUITE_data/maps_export.erl b/lib/hipe/test/maps_SUITE_data/maps_export.erl
new file mode 100644
index 0000000000..4d43fc96ed
--- /dev/null
+++ b/lib/hipe/test/maps_SUITE_data/maps_export.erl
@@ -0,0 +1,11 @@
+-module(maps_export).
+-export([test/0]).
+
+test() ->
+ Raclette = id(#{}),
+ case brie of brie -> Fromage = Raclette end,
+ Raclette = Fromage#{},
+ ok.
+
+%% Use this function to avoid compile-time evaluation of an expression.
+id(I) -> I.
diff --git a/lib/hipe/test/maps_SUITE_data/maps_get_map_elements.erl b/lib/hipe/test/maps_SUITE_data/maps_get_map_elements.erl
new file mode 100644
index 0000000000..b2d749796a
--- /dev/null
+++ b/lib/hipe/test/maps_SUITE_data/maps_get_map_elements.erl
@@ -0,0 +1,23 @@
+%% -*- erlang-indent-level: 2 -*-
+%%-------------------------------------------------------------------------
+-module(maps_get_map_elements).
+
+-export([test/0]).
+
+test() ->
+ {A, B} = id({"hej", <<123>>}),
+ Map = maps:from_list([{a, A}, {b, B}]),
+ #{a := A, b := B} = id(Map),
+ false = test_pattern(Map),
+ true = test_pattern(#{b => 1, a => "hej"}),
+ case Map of
+ #{a := C, b := <<124>>} -> yay;
+ _ -> C = B, nay
+ end,
+ C = id(B),
+ ok.
+
+id(X) -> X.
+
+test_pattern(#{a := _, b := 1}) -> true;
+test_pattern(#{}) -> false.
diff --git a/lib/hipe/test/maps_SUITE_data/maps_guard_bifs.erl b/lib/hipe/test/maps_SUITE_data/maps_guard_bifs.erl
new file mode 100644
index 0000000000..61a0eaa1e7
--- /dev/null
+++ b/lib/hipe/test/maps_SUITE_data/maps_guard_bifs.erl
@@ -0,0 +1,31 @@
+-module(maps_guard_bifs).
+-export([test/0]).
+
+test() ->
+ true = map_guard_empty(),
+ true = map_guard_empty_2(),
+ true = map_guard_head(#{a=>1}),
+ false = map_guard_head([]),
+ true = map_guard_body(#{a=>1}),
+ false = map_guard_body({}),
+ true = map_guard_pattern(#{a=>1, <<"hi">> => "hi" }),
+ false = map_guard_pattern("list"),
+ true = map_guard_tautology(),
+ true = map_guard_ill_map_size(),
+ ok.
+
+map_guard_empty() when is_map(#{}); false -> true.
+
+map_guard_empty_2() when true; #{} andalso false -> true.
+
+map_guard_head(M) when is_map(M) -> true;
+map_guard_head(_) -> false.
+
+map_guard_body(M) -> is_map(M).
+
+map_guard_pattern(#{}) -> true;
+map_guard_pattern(_) -> false.
+
+map_guard_tautology() when #{} =:= #{}; true -> true.
+
+map_guard_ill_map_size() when true; map_size(0) -> true.
diff --git a/lib/hipe/test/maps_SUITE_data/maps_guard_fun.erl b/lib/hipe/test/maps_SUITE_data/maps_guard_fun.erl
new file mode 100644
index 0000000000..9f6eb3a04e
--- /dev/null
+++ b/lib/hipe/test/maps_SUITE_data/maps_guard_fun.erl
@@ -0,0 +1,36 @@
+-module(maps_guard_fun).
+-export([test/0]).
+
+test() ->
+ F1 = fun
+ (#{s:=v,v:=V}) -> {v,V};
+ (#{s:=t,v:={V,V}}) -> {t,V};
+ (#{s:=l,v:=[V,V]}) -> {l,V}
+ end,
+
+ F2 = fun
+ (#{s:=T,v:={V,V}}) -> {T,V};
+ (#{s:=T,v:=[V,V]}) -> {T,V};
+ (#{s:=T,v:=V}) -> {T,V}
+ end,
+ V = <<"hi">>,
+
+ {v,V} = F1(#{s=>v,v=>V}),
+ {t,V} = F1(#{s=>t,v=>{V,V}}),
+ {l,V} = F1(#{s=>l,v=>[V,V]}),
+
+ {v,V} = F2(#{s=>v,v=>V}),
+ {t,V} = F2(#{s=>t,v=>{V,V}}),
+ {l,V} = F2(#{s=>l,v=>[V,V]}),
+
+ %% error case
+ case (catch F1(#{s=>none,v=>none})) of
+ {'EXIT', {function_clause,[{?MODULE,_,[#{s:=none,v:=none}],_}|_]}} -> ok;
+ {'EXIT', {function_clause,[{?MODULE,_,1,[#{s:=none,v:=none}]}|_]}} -> ok;
+ {'EXIT', {function_clause,[Frame|_]}}
+ when is_tuple(Frame), element(1, Frame) =:= ?MODULE ->
+ test_server:comment("Unexpected trace format, probably using HiPE");
+ {'EXIT', {{case_clause,_},_}} -> {comment,inlined};
+ Other ->
+ test_server:fail({no_match, Other})
+ end.
diff --git a/lib/hipe/test/maps_SUITE_data/maps_guard_receive.erl b/lib/hipe/test/maps_SUITE_data/maps_guard_receive.erl
new file mode 100644
index 0000000000..f84ba19c86
--- /dev/null
+++ b/lib/hipe/test/maps_SUITE_data/maps_guard_receive.erl
@@ -0,0 +1,54 @@
+-module(maps_guard_receive).
+-export([test/0]).
+
+test() ->
+ M0 = #{ id => 0 },
+ Pid = spawn_link(fun() -> guard_receive_loop() end),
+ Big = 36893488147419103229,
+ B1 = <<"some text">>,
+ B2 = <<"was appended">>,
+ B3 = <<B1/binary, B2/binary>>,
+
+ #{id:=1, res:=Big} = M1 = call(Pid, M0#{op=>sub,in=>{1 bsl 65, 3}}),
+ #{id:=2, res:=26} = M2 = call(Pid, M1#{op=>idiv,in=>{53,2}}),
+ #{id:=3, res:=832} = M3 = call(Pid, M2#{op=>imul,in=>{26,32}}),
+ #{id:=4, res:=4} = M4 = call(Pid, M3#{op=>add,in=>{1,3}}),
+ #{id:=5, res:=Big} = M5 = call(Pid, M4#{op=>sub,in=>{1 bsl 65, 3}}),
+ #{id:=6, res:=B3} = M6 = call(Pid, M5#{op=>"append",in=>{B1,B2}}),
+ #{id:=7, res:=4} = _ = call(Pid, M6#{op=>add,in=>{1,3}}),
+
+
+ %% update old maps and check id update
+ #{id:=2, res:=B3} = call(Pid, M1#{op=>"append",in=>{B1,B2}}),
+ #{id:=5, res:=99} = call(Pid, M4#{op=>add,in=>{33, 66}}),
+
+ %% cleanup
+ done = call(Pid, done),
+ ok.
+
+call(Pid, M) ->
+ Pid ! {self(), M}, receive {Pid, Res} -> Res end.
+
+guard_receive_loop() ->
+ receive
+ {Pid, #{ id:=Id, op:="append", in:={X,Y}}=M} when is_binary(X), is_binary(Y) ->
+ Pid ! {self(), M#{ id=>Id+1, res=><<X/binary,Y/binary>>}},
+ guard_receive_loop();
+ {Pid, #{ id:=Id, op:=add, in:={X,Y}}} ->
+ Pid ! {self(), #{ id=>Id+1, res=>X+Y}},
+ guard_receive_loop();
+ {Pid, #{ id:=Id, op:=sub, in:={X,Y}}=M} ->
+ Pid ! {self(), M#{ id=>Id+1, res=>X-Y}},
+ guard_receive_loop();
+ {Pid, #{ id:=Id, op:=idiv, in:={X,Y}}=M} ->
+ Pid ! {self(), M#{ id=>Id+1, res=>X div Y}},
+ guard_receive_loop();
+ {Pid, #{ id:=Id, op:=imul, in:={X,Y}}=M} ->
+ Pid ! {self(), M#{ id=>Id+1, res=>X * Y}},
+ guard_receive_loop();
+ {Pid, done} ->
+ Pid ! {self(), done};
+ {Pid, Other} ->
+ Pid ! {error, Other},
+ guard_receive_loop()
+ end.
diff --git a/lib/hipe/test/maps_SUITE_data/maps_guard_sequence.erl b/lib/hipe/test/maps_SUITE_data/maps_guard_sequence.erl
new file mode 100644
index 0000000000..4eb18dcea1
--- /dev/null
+++ b/lib/hipe/test/maps_SUITE_data/maps_guard_sequence.erl
@@ -0,0 +1,35 @@
+-module(maps_guard_sequence).
+-export([test/0]).
+
+test() ->
+ {1, "a"} = map_guard_sequence_1(#{seq=>1,val=>id("a")}),
+ {2, "b"} = map_guard_sequence_1(#{seq=>2,val=>id("b")}),
+ {3, "c"} = map_guard_sequence_1(#{seq=>3,val=>id("c")}),
+ {4, "d"} = map_guard_sequence_1(#{seq=>4,val=>id("d")}),
+ {5, "e"} = map_guard_sequence_1(#{seq=>5,val=>id("e")}),
+
+ {1,M1} = map_guard_sequence_2(M1 = id(#{a=>3})),
+ {2,M2} = map_guard_sequence_2(M2 = id(#{a=>4, b=>4})),
+ {3,gg,M3} = map_guard_sequence_2(M3 = id(#{a=>gg, b=>4})),
+ {4,sc,sc,M4} = map_guard_sequence_2(M4 = id(#{a=>sc, b=>3, c=>sc2})),
+ {5,kk,kk,M5} = map_guard_sequence_2(M5 = id(#{a=>kk, b=>other, c=>sc2})),
+
+ %% error case
+ {'EXIT',{function_clause,_}} = (catch map_guard_sequence_1(#{seq=>6,val=>id("e")})),
+ {'EXIT',{function_clause,_}} = (catch map_guard_sequence_2(#{b=>5})),
+ ok.
+
+map_guard_sequence_1(#{seq:=1=Seq, val:=Val}) -> {Seq,Val};
+map_guard_sequence_1(#{seq:=2=Seq, val:=Val}) -> {Seq,Val};
+map_guard_sequence_1(#{seq:=3=Seq, val:=Val}) -> {Seq,Val};
+map_guard_sequence_1(#{seq:=4=Seq, val:=Val}) -> {Seq,Val};
+map_guard_sequence_1(#{seq:=5=Seq, val:=Val}) -> {Seq,Val}.
+
+map_guard_sequence_2(#{ a:=3 }=M) -> {1, M};
+map_guard_sequence_2(#{ a:=4 }=M) -> {2, M};
+map_guard_sequence_2(#{ a:=X, a:=X, b:=4 }=M) -> {3,X,M};
+map_guard_sequence_2(#{ a:=X, a:=Y, b:=3 }=M) when X =:= Y -> {4,X,Y,M};
+map_guard_sequence_2(#{ a:=X, a:=Y }=M) when X =:= Y -> {5,X,Y,M}.
+
+%% Use this function to avoid compile-time evaluation of an expression.
+id(I) -> I.
diff --git a/lib/hipe/test/maps_SUITE_data/maps_guard_update.erl b/lib/hipe/test/maps_SUITE_data/maps_guard_update.erl
new file mode 100644
index 0000000000..254c1c2984
--- /dev/null
+++ b/lib/hipe/test/maps_SUITE_data/maps_guard_update.erl
@@ -0,0 +1,14 @@
+-module(maps_guard_update).
+-export([test/0]).
+
+test() ->
+ error = map_guard_update(#{},#{}),
+ first = map_guard_update(#{}, #{x=>first}),
+ second = map_guard_update(#{y=>old}, #{x=>second,y=>old}),
+ third = map_guard_update(#{x=>old,y=>old}, #{x=>third,y=>old}),
+ ok.
+
+map_guard_update(M1, M2) when M1#{x=>first} =:= M2 -> first;
+map_guard_update(M1, M2) when M1#{x=>second} =:= M2 -> second;
+map_guard_update(M1, M2) when M1#{x:=third} =:= M2 -> third;
+map_guard_update(_, _) -> error.
diff --git a/lib/hipe/test/maps_SUITE_data/maps_has_map_fields.erl b/lib/hipe/test/maps_SUITE_data/maps_has_map_fields.erl
new file mode 100644
index 0000000000..61653aa519
--- /dev/null
+++ b/lib/hipe/test/maps_SUITE_data/maps_has_map_fields.erl
@@ -0,0 +1,46 @@
+%% -*- erlang-indent-level: 2 -*-
+%%-------------------------------------------------------------------------
+-module(maps_has_map_fields).
+
+-export([test/0]).
+
+test() ->
+ false = has_a_field(#{}),
+ false = has_a_field(#{b => 2}),
+ true = has_a_field(#{a => 3}),
+ true = has_a_field(#{b => c, a => false}),
+
+ false = has_a_b_field(#{a => true}),
+ false = has_a_b_field(#{b => a}),
+ true = has_a_b_field(#{a => 1, b => 2}),
+ true = has_a_b_field(#{b => 3, a => 4}),
+
+ false = has_binary_field(#{}),
+ false = has_binary_field(#{#{} => yay}),
+ true = has_binary_field(#{<<"true">> => false}),
+
+ false = has_binary_but_no_map_field(#{}),
+ false = has_map_but_no_binary_field(#{}),
+ false = has_binary_but_no_map_field(#{#{} => 1}),
+ false = has_map_but_no_binary_field(#{<<"true">> => true}),
+ true = has_binary_but_no_map_field(#{<<"true">> => false}),
+ true = has_map_but_no_binary_field(#{#{} => 1}),
+ false = has_binary_but_no_map_field(#{<<"true">> => true, #{} => 1}),
+ false = has_map_but_no_binary_field(#{<<"true">> => true, #{} => 1}),
+ ok.
+
+has_a_field(#{a := _}) -> true;
+has_a_field(#{}) -> false.
+
+has_a_b_field(#{a := _, b := _}) -> true;
+has_a_b_field(#{}) -> false.
+
+has_binary_field(#{<<"true">> := _}) -> true;
+has_binary_field(#{}) -> false.
+
+has_map_but_no_binary_field(#{<<"true">> := _}) -> false;
+has_map_but_no_binary_field(#{} = M) -> maps:is_key(#{}, M).
+
+has_binary_but_no_map_field(#{<<"true">> := _} = M) ->
+ not maps:is_key(#{}, M);
+has_binary_but_no_map_field(#{}) -> false.
diff --git a/lib/hipe/test/maps_SUITE_data/maps_is_map.erl b/lib/hipe/test/maps_SUITE_data/maps_is_map.erl
new file mode 100644
index 0000000000..e84f4b8c44
--- /dev/null
+++ b/lib/hipe/test/maps_SUITE_data/maps_is_map.erl
@@ -0,0 +1,24 @@
+%% -*- erlang-indent-level: 2 -*-
+%%-------------------------------------------------------------------------
+-module(maps_is_map).
+
+-export([test/0]).
+
+test() ->
+ true = test_is_map(#{}),
+ false = test_is_map(<<"hej">>),
+ true = test_is_map_guard(#{a => b}),
+ false = test_is_map_guard(3),
+ true = test_is_map_with_binary_guard(#{"a" => <<"b">>}),
+ false = test_is_map_with_binary_guard(12),
+ ok.
+
+test_is_map(X) ->
+ is_map(X).
+
+test_is_map_guard(Map) when is_map(Map) -> true;
+test_is_map_guard(_) -> false.
+
+test_is_map_with_binary_guard(B) when is_binary(B) -> false;
+test_is_map_with_binary_guard(#{}) -> true;
+test_is_map_with_binary_guard(_) -> false.
diff --git a/lib/hipe/test/maps_SUITE_data/maps_list_comprehension.erl b/lib/hipe/test/maps_SUITE_data/maps_list_comprehension.erl
new file mode 100644
index 0000000000..ad2c726d65
--- /dev/null
+++ b/lib/hipe/test/maps_SUITE_data/maps_list_comprehension.erl
@@ -0,0 +1,6 @@
+-module(maps_list_comprehension).
+-export([test/0]).
+
+test() ->
+ [#{k:=1},#{k:=2},#{k:=3}] = [#{k=>I} || I <- [1,2,3]],
+ ok.
diff --git a/lib/hipe/test/maps_SUITE_data/maps_map_size.erl b/lib/hipe/test/maps_SUITE_data/maps_map_size.erl
new file mode 100644
index 0000000000..25c8e5d4c7
--- /dev/null
+++ b/lib/hipe/test/maps_SUITE_data/maps_map_size.erl
@@ -0,0 +1,29 @@
+-module(maps_map_size).
+-export([test/0]).
+
+test() ->
+ 0 = map_size(id(#{})),
+ 1 = map_size(id(#{a=>1})),
+ 1 = map_size(id(#{a=>"wat"})),
+ 2 = map_size(id(#{a=>1, b=>2})),
+ 3 = map_size(id(#{a=>1, b=>2, b=>"3","33"=><<"n">>})),
+
+ true = map_is_size(#{a=>1}, 1),
+ true = map_is_size(#{a=>1, a=>2}, 1),
+ M = #{ "a" => 1, "b" => 2},
+ true = map_is_size(M, 2),
+ false = map_is_size(M, 3),
+ true = map_is_size(M#{ "a" => 2}, 2),
+ false = map_is_size(M#{ "c" => 2}, 2),
+
+ %% Error cases.
+ {'EXIT',{badarg,_}} = (catch map_size([])),
+ {'EXIT',{badarg,_}} = (catch map_size(<<1,2,3>>)),
+ {'EXIT',{badarg,_}} = (catch map_size(1)),
+ ok.
+
+map_is_size(M,N) when map_size(M) =:= N -> true;
+map_is_size(_,_) -> false.
+
+%% Use this function to avoid compile-time evaluation of an expression.
+id(I) -> I.
diff --git a/lib/hipe/test/maps_SUITE_data/maps_map_sort_literals.erl b/lib/hipe/test/maps_SUITE_data/maps_map_sort_literals.erl
new file mode 100644
index 0000000000..31abf15d49
--- /dev/null
+++ b/lib/hipe/test/maps_SUITE_data/maps_map_sort_literals.erl
@@ -0,0 +1,41 @@
+-module(maps_map_sort_literals).
+-export([test/0]).
+
+test() ->
+ % test relation
+
+ %% size order
+ true = #{ a => 1, b => 2} < id(#{ a => 1, b => 1, c => 1}),
+ true = #{ b => 1, a => 1} < id(#{ c => 1, a => 1, b => 1}),
+ false = #{ c => 1, b => 1, a => 1} < id(#{ c => 1, a => 1}),
+
+ %% key order
+ true = id(#{ a => 1 }) < id(#{ b => 1}),
+ false = id(#{ b => 1 }) < id(#{ a => 1}),
+ true = id(#{ a => 1, b => 1, c => 1 }) < id(#{ b => 1, c => 1, d => 1}),
+ true = id(#{ b => 1, c => 1, d => 1 }) > id(#{ a => 1, b => 1, c => 1}),
+ true = id(#{ c => 1, b => 1, a => 1 }) < id(#{ b => 1, c => 1, d => 1}),
+ true = id(#{ "a" => 1 }) < id(#{ <<"a">> => 1}),
+ false = id(#{ <<"a">> => 1 }) < id(#{ "a" => 1}),
+ false = id(#{ 1 => 1 }) < id(#{ 1.0 => 1}),
+ false = id(#{ 1.0 => 1 }) < id(#{ 1 => 1}),
+
+ %% value order
+ true = id(#{ a => 1 }) < id(#{ a => 2}),
+ false = id(#{ a => 2 }) < id(#{ a => 1}),
+ false = id(#{ a => 2, b => 1 }) < id(#{ a => 1, b => 3}),
+ true = id(#{ a => 1, b => 1 }) < id(#{ a => 1, b => 3}),
+
+ true = id(#{ "a" => "hi", b => 134 }) == id(#{ b => 134,"a" => "hi"}),
+
+ %% lists:sort
+
+ SortVs = [#{"a"=>1},#{a=>2},#{1=>3},#{<<"a">>=>4}],
+ [#{1:=ok},#{a:=ok},#{"a":=ok},#{<<"a">>:=ok}] = lists:sort([#{"a"=>ok},#{a=>ok},#{1=>ok},#{<<"a">>=>ok}]),
+ [#{1:=3},#{a:=2},#{"a":=1},#{<<"a">>:=4}] = lists:sort(SortVs),
+ [#{1:=3},#{a:=2},#{"a":=1},#{<<"a">>:=4}] = lists:sort(lists:reverse(SortVs)),
+
+ ok.
+
+%% Use this function to avoid compile-time evaluation of an expression.
+id(I) -> I.
diff --git a/lib/hipe/test/maps_SUITE_data/maps_match_and_update_literals.erl b/lib/hipe/test/maps_SUITE_data/maps_match_and_update_literals.erl
new file mode 100644
index 0000000000..29a6a29290
--- /dev/null
+++ b/lib/hipe/test/maps_SUITE_data/maps_match_and_update_literals.erl
@@ -0,0 +1,24 @@
+-module(maps_match_and_update_literals).
+-export([test/0]).
+
+test() ->
+ Map = #{x=>0,y=>"untouched",z=>"also untouched",q=>1},
+ #{x:=16,q:=21,y:="untouched",z:="also untouched"} = loop_match_and_update_literals_x_q(Map, [
+ {1,2},{3,4},{5,6},{7,8}
+ ]),
+ M0 = id(#{ "hi" => "hello", int => 3, <<"key">> => <<"value">>,
+ 4 => number, 18446744073709551629 => wat}),
+ M1 = id(#{}),
+ M2 = M1#{ "hi" => "hello", int => 3, <<"key">> => <<"value">>,
+ 4 => number, 18446744073709551629 => wat},
+ M0 = M2,
+
+ #{ 4 := another_number, int := 3 } = M2#{ 4 => another_number },
+ ok.
+
+loop_match_and_update_literals_x_q(Map, []) -> Map;
+loop_match_and_update_literals_x_q(#{q:=Q0,x:=X0} = Map, [{X,Q}|Vs]) ->
+ loop_match_and_update_literals_x_q(Map#{q=>Q0+Q,x=>X0+X},Vs).
+
+%% Use this function to avoid compile-time evaluation of an expression.
+id(I) -> I.
diff --git a/lib/hipe/test/maps_SUITE_data/maps_put_map_assoc.erl b/lib/hipe/test/maps_SUITE_data/maps_put_map_assoc.erl
new file mode 100644
index 0000000000..72ac9ce078
--- /dev/null
+++ b/lib/hipe/test/maps_SUITE_data/maps_put_map_assoc.erl
@@ -0,0 +1,23 @@
+%% -*- erlang-indent-level: 2 -*-
+%%-------------------------------------------------------------------------
+-module(maps_put_map_assoc).
+
+-export([test/0]).
+
+test() ->
+ true = assoc_guard(#{}),
+ false = assoc_guard(not_a_map),
+ #{a := true} = assoc_update(#{}),
+ {'EXIT', {badarg, [{?MODULE, assoc_update, 1, _}|_]}}
+ = (catch assoc_update(not_a_map)),
+ ok = assoc_guard_clause(#{}),
+ {'EXIT', {function_clause, [{?MODULE, assoc_guard_clause, _, _}|_]}}
+ = (catch assoc_guard_clause(not_a_map)),
+ ok.
+
+assoc_guard(M) when is_map(M#{a => b}) -> true;
+assoc_guard(_) -> false.
+
+assoc_update(M) -> M#{a => true}.
+
+assoc_guard_clause(M) when is_map(M#{a => 3}) -> ok.
diff --git a/lib/hipe/test/maps_SUITE_data/maps_put_map_exact.erl b/lib/hipe/test/maps_SUITE_data/maps_put_map_exact.erl
new file mode 100644
index 0000000000..1cfcd80180
--- /dev/null
+++ b/lib/hipe/test/maps_SUITE_data/maps_put_map_exact.erl
@@ -0,0 +1,28 @@
+%% -*- erlang-indent-level: 2 -*-
+%%-------------------------------------------------------------------------
+-module(maps_put_map_exact).
+
+-export([test/0]).
+
+test() ->
+ false = exact_guard(#{b => a}),
+ false = exact_guard(not_a_map),
+ true = exact_guard(#{a => false}),
+ #{a := true} = exact_update(#{a => false}),
+ {'EXIT', {badarg, [{?MODULE, exact_update, 1, _}|_]}}
+ = (catch exact_update(not_a_map)),
+ {'EXIT', {badarg, [{?MODULE, exact_update, 1, _}|_]}}
+ = (catch exact_update(#{})),
+ ok = exact_guard_clause(#{a => yes}),
+ {'EXIT', {function_clause, [{?MODULE, exact_guard_clause, _, _}|_]}}
+ = (catch exact_guard_clause(#{})),
+ {'EXIT', {function_clause, [{?MODULE, exact_guard_clause, _, _}|_]}}
+ = (catch exact_guard_clause(not_a_map)),
+ ok.
+
+exact_guard(M) when is_map(M#{a := b}) -> true;
+exact_guard(_) -> false.
+
+exact_update(M) -> M#{a := true}.
+
+exact_guard_clause(M) when is_map(M#{a := 42}) -> ok.
diff --git a/lib/hipe/test/maps_SUITE_data/maps_update_assoc.erl b/lib/hipe/test/maps_SUITE_data/maps_update_assoc.erl
new file mode 100644
index 0000000000..cc7c1353de
--- /dev/null
+++ b/lib/hipe/test/maps_SUITE_data/maps_update_assoc.erl
@@ -0,0 +1,22 @@
+-module(maps_update_assoc).
+-export([test/0]).
+
+test() ->
+ M0 = id(#{1=>a,2=>b,3.0=>c,4=>d,5=>e}),
+
+ M1 = M0#{1=>42,2=>100,4=>[a,b,c]},
+ #{1:=42,2:=100,3.0:=c,4:=[a,b,c],5:=e} = M1,
+ #{1:=42,2:=b,4:=d,5:=e,2.0:=100,3.0:=c,4.0:=[a,b,c]} = M0#{1.0=>float,1:=42,2.0=>wrong,2.0=>100,4.0=>[a,b,c]},
+
+ M2 = M0#{3.0=>new},
+ #{1:=a,2:=b,3.0:=new,4:=d,5:=e} = M2,
+ M2 = M0#{3.0:=wrong,3.0=>new},
+
+ %% Errors cases.
+ BadMap = id(badmap),
+ {'EXIT',{badarg,_}} = (catch BadMap#{nonexisting=>val}),
+
+ ok.
+
+%% Use this function to avoid compile-time evaluation of an expression.
+id(I) -> I.
diff --git a/lib/hipe/test/maps_SUITE_data/maps_update_exact.erl b/lib/hipe/test/maps_SUITE_data/maps_update_exact.erl
new file mode 100644
index 0000000000..6e5acb3283
--- /dev/null
+++ b/lib/hipe/test/maps_SUITE_data/maps_update_exact.erl
@@ -0,0 +1,32 @@
+-module(maps_update_exact).
+-export([test/0]).
+
+test() ->
+ M0 = id(#{1=>a,2=>b,3.0=>c,4=>d,5=>e}),
+
+ M1 = M0#{1:=42,2:=100,4:=[a,b,c]},
+ #{1:=42,2:=100,3.0:=c,4:=[a,b,c],5:=e} = M1,
+ M1 = M0#{1:=wrong,1=>42,2=>wrong,2:=100,4:=[a,b,c]},
+
+ M2 = M0#{3.0:=new},
+ #{1:=a,2:=b,3.0:=new,4:=d,5:=e} = M2,
+ M2 = M0#{3.0=>wrong,3.0:=new},
+ true = M2 =/= M0#{3=>right,3.0:=new},
+ #{ 3 := right, 3.0 := new } = M0#{3=>right,3.0:=new},
+
+ M3 = id(#{ 1 => val}),
+ #{1 := update2,1.0 := new_val4} = M3#{
+ 1.0 => new_val1, 1 := update, 1=> update3,
+ 1 := update2, 1.0 := new_val2, 1.0 => new_val3,
+ 1.0 => new_val4 },
+
+ %% Errors cases.
+ {'EXIT',{badarg,_}} = (catch ((id(nil))#{ a := b })),
+ {'EXIT',{badarg,_}} = (catch M0#{nonexisting:=val}),
+ {'EXIT',{badarg,_}} = (catch M0#{1.0:=v,1.0=>v2}),
+ {'EXIT',{badarg,_}} = (catch M0#{42.0:=v,42:=v2}),
+ {'EXIT',{badarg,_}} = (catch M0#{42=>v1,42.0:=v2,42:=v3}),
+ ok.
+
+%% Use this function to avoid compile-time evaluation of an expression.
+id(I) -> I.
diff --git a/lib/hipe/test/maps_SUITE_data/maps_update_literals.erl b/lib/hipe/test/maps_SUITE_data/maps_update_literals.erl
new file mode 100644
index 0000000000..87aea3d8e1
--- /dev/null
+++ b/lib/hipe/test/maps_SUITE_data/maps_update_literals.erl
@@ -0,0 +1,13 @@
+-module(maps_update_literals).
+-export([test/0]).
+
+test() ->
+ Map = #{x=>1,y=>2,z=>3,q=>4},
+ #{x:="d",q:="4"} = loop_update_literals_x_q(Map, [
+ {"a","1"},{"b","2"},{"c","3"},{"d","4"}
+ ]),
+ ok.
+
+loop_update_literals_x_q(Map, []) -> Map;
+loop_update_literals_x_q(Map, [{X,Q}|Vs]) ->
+ loop_update_literals_x_q(Map#{q=>Q,x=>X},Vs).
diff --git a/lib/hipe/test/maps_SUITE_data/maps_update_map_expressions.erl b/lib/hipe/test/maps_SUITE_data/maps_update_map_expressions.erl
new file mode 100644
index 0000000000..181e3f18f7
--- /dev/null
+++ b/lib/hipe/test/maps_SUITE_data/maps_update_map_expressions.erl
@@ -0,0 +1,32 @@
+-module(maps_update_map_expressions).
+-export([test/0]).
+
+test() ->
+ M = maps:new(),
+ X = id(fondue),
+ M1 = #{ a := 1 } = M#{a => 1},
+ #{ b := {X} } = M1#{ a := 1, b => {X} },
+
+ #{ b := 2 } = (maps:new())#{ b => 2 },
+
+ #{ a :=42, b:=42, c:=42 } = (maps:from_list([{a,1},{b,2},{c,3}]))#{ a := 42, b := 42, c := 42 },
+ #{ "a" :=1, "b":=42, "c":=42 } = (maps:from_list([{"a",1},{"b",2}]))#{ "b" := 42, "c" => 42 },
+
+ %% Test need to be in a fun.
+ %% This tests that let expr optimisation in sys_core_fold
+ %% covers maps correctly.
+ F = fun() ->
+ M0 = id(#{ "a" => [1,2,3] }),
+ #{ "a" := _ } = M0,
+ M0#{ "a" := b }
+ end,
+
+ #{ "a" := b } = F(),
+
+ %% Error cases, FIXME: should be 'badmap'?
+ {'EXIT',{badarg,_}} = (catch (id(<<>>))#{ a := 42, b => 2 }),
+ {'EXIT',{badarg,_}} = (catch (id([]))#{ a := 42, b => 2 }),
+ ok.
+
+%% Use this function to avoid compile-time evaluation of an expression.
+id(I) -> I.
diff --git a/lib/hipe/test/maps_SUITE_data/maps_update_values.erl b/lib/hipe/test/maps_SUITE_data/maps_update_values.erl
new file mode 100644
index 0000000000..bbad5ac19e
--- /dev/null
+++ b/lib/hipe/test/maps_SUITE_data/maps_update_values.erl
@@ -0,0 +1,28 @@
+-module(maps_update_values).
+-export([test/0]).
+
+test() ->
+ V0 = id(1337),
+ M0 = #{ a => 1, val => V0},
+ V1 = get_val(M0),
+ M1 = M0#{ val := [V0,V1], "wazzup" => 42 },
+ [1337, {some_val, 1337}] = get_val(M1),
+
+ N = 110,
+ List = [{[I,1,2,3,I],{1,2,3,"wat",I}}|| I <- lists:seq(1,N)],
+
+ {_,_,#{val2 := {1,2,3,"wat",N}, val1 := [N,1,2,3,N]}} = lists:foldl(fun
+ ({V2,V3},{Old2,Old3,Mi}) ->
+ ok = check_val(Mi,Old2,Old3),
+ #{ val1 := Old2, val2 := Old3 } = Mi,
+ {V2,V3, Mi#{ val1 := id(V2), val2 := V1, val2 => id(V3)}}
+ end, {none, none, #{val1=>none,val2=>none}},List),
+ ok.
+
+get_val(#{ "wazzup" := _, val := V}) -> V;
+get_val(#{ val := V }) -> {some_val, V}.
+
+check_val(#{val1:=V1, val2:=V2},V1,V2) -> ok.
+
+%% Use this function to avoid compile-time evaluation of an expression.
+id(I) -> I.
diff --git a/lib/hipe/test/maps_SUITE_data/maps_warn_pair_key_overloaded.erl b/lib/hipe/test/maps_SUITE_data/maps_warn_pair_key_overloaded.erl
new file mode 100644
index 0000000000..76b2a91f94
--- /dev/null
+++ b/lib/hipe/test/maps_SUITE_data/maps_warn_pair_key_overloaded.erl
@@ -0,0 +1,27 @@
+-module(maps_warn_pair_key_overloaded).
+-export([test/0]).
+
+test() ->
+ #{ "hi1" := 42 } = id(#{ "hi1" => 1, "hi1" => 42 }),
+
+ #{ "hi1" := 1337, "hi2" := [2], "hi3" := 3 } = id(#{
+ "hi1" => erlang:atom_to_binary(?MODULE,utf8),
+ "hi1" => erlang:binary_to_atom(<<"wazzup">>,utf8),
+ "hi1" => erlang:binary_to_float(<<"3.1416">>),
+ "hi1" => erlang:float_to_binary(3.1416),
+ "hi2" => erlang:pid_to_list(self()),
+ "hi3" => erlang:float_to_binary(3.1416),
+ "hi2" => lists:subtract([1,2],[1]),
+ "hi3" => +3,
+ "hi1" => erlang:min(1,2),
+ "hi1" => erlang:hash({1,2},35),
+ "hi1" => erlang:phash({1,2},33),
+ "hi1" => erlang:phash2({1,2},34),
+ "hi1" => erlang:integer_to_binary(1337),
+ "hi1" => erlang:binary_to_integer(<<"1337">>),
+ "hi4" => erlang:float_to_binary(3.1416)
+ }),
+ ok.
+
+%% Use this function to avoid compile-time evaluation of an expression.
+id(I) -> I.
diff --git a/lib/hipe/test/maps_SUITE_data/maps_warn_useless_build.erl b/lib/hipe/test/maps_SUITE_data/maps_warn_useless_build.erl
new file mode 100644
index 0000000000..6cb0366314
--- /dev/null
+++ b/lib/hipe/test/maps_SUITE_data/maps_warn_useless_build.erl
@@ -0,0 +1,9 @@
+-module(maps_warn_useless_build).
+-export([test/0]).
+
+test() ->
+ [#{ a => id(I)} || I <- [1,2,3]],
+ ok.
+
+%% Use this function to avoid compile-time evaluation of an expression.
+id(I) -> I.
diff --git a/lib/hipe/tools/Makefile b/lib/hipe/tools/Makefile
index 3ce8ad5dd7..ed80eb075b 100644
--- a/lib/hipe/tools/Makefile
+++ b/lib/hipe/tools/Makefile
@@ -42,7 +42,7 @@ RELSYSDIR = $(RELEASE_PATH)/lib/hipe-$(VSN)
# ----------------------------------------------------
# Target Specs
# ----------------------------------------------------
-MODULES = hipe_tool hipe_profile hipe_jit
+MODULES = hipe_profile hipe_jit
# hipe_timer
HRL_FILES=
@@ -110,5 +110,4 @@ realclean: clean
# ----------------------------------------------------
$(EBIN)/hipe_ceach.beam: ../main/hipe.hrl
-$(EBIN)/hipe_tool.beam: ../main/hipe.hrl
diff --git a/lib/hipe/tools/hipe_tool.erl b/lib/hipe/tools/hipe_tool.erl
deleted file mode 100644
index efcf073efa..0000000000
--- a/lib/hipe/tools/hipe_tool.erl
+++ /dev/null
@@ -1,525 +0,0 @@
-%% -*- erlang-indent-level: 2 -*-
-%%
-%% %CopyrightBegin%
-%%
-%% Copyright Ericsson AB 2002-2012. All Rights Reserved.
-%%
-%% The contents of this file are subject to the Erlang Public License,
-%% Version 1.1, (the "License"); you may not use this file except in
-%% compliance with the License. You should have received a copy of the
-%% Erlang Public License along with this software. If not, it can be
-%% retrieved online at http://www.erlang.org/.
-%%
-%% Software distributed under the License is distributed on an "AS IS"
-%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
-%% the License for the specific language governing rights and limitations
-%% under the License.
-%%
-%% %CopyrightEnd%
-%%
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%% Copyright (c) 2002 by Erik Johansson.
-%% ====================================================================
-%% Module : hipe_tool
-%% Purpose :
-%% Notes :
-%% History : * 2002-03-13 Erik Johansson ([email protected]): Created.
-%% ====================================================================
-%% Exports :
-%%
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-
--module(hipe_tool).
--compile([{nowarn_deprecated_function,{gs,button,3}},
- {nowarn_deprecated_function,{gs,config,2}},
- {nowarn_deprecated_function,{gs,destroy,1}},
- {nowarn_deprecated_function,{gs,editor,3}},
- {nowarn_deprecated_function,{gs,label,3}},
- {nowarn_deprecated_function,{gs,listbox,3}},
- {nowarn_deprecated_function,{gs,menu,3}},
- {nowarn_deprecated_function,{gs,menubar,3}},
- {nowarn_deprecated_function,{gs,menubutton,3}},
- {nowarn_deprecated_function,{gs,menuitem,2}},
- {nowarn_deprecated_function,{gs,start,0}},
- {nowarn_deprecated_function,{gs,window,3}}]).
-
--export([start/0]).
-
-%%---------------------------------------------------------------------
-
--include("../main/hipe.hrl").
-
-%%---------------------------------------------------------------------
-
--define(WINDOW_WIDTH, 920).
--define(WINDOW_HEIGHT, 460).
--define(DEFAULT_BG_COLOR, {217,217,217}).
--define(POLL_INTERVAL, 5000).
--define(FONT, {screen, 12}).
--define(HEADER_FONT, {screen, [bold], 12}).
--define(NORMAL_FG_COLOR, {0,0,0}).
-
-%%---------------------------------------------------------------------
-
--type fa() :: {atom(), arity()}. % {Fun,Arity}
--type fa_address() :: {atom(), arity(), non_neg_integer()}. % {F,A,Address}
-
-%%---------------------------------------------------------------------
-
--record(state, {win_created = false :: boolean(),
- mindex = 0 :: integer(),
- mod :: atom(),
- funs = [] :: [fa()],
- mods = [] :: [atom()],
- options = [o2] :: comp_options(),
- compiling = false :: 'false' | pid()
- }).
-
-%%---------------------------------------------------------------------
-
--spec start() -> pid().
-
-start() ->
- spawn(fun () -> init() end).
-
-init() ->
- process_flag(trap_exit, true),
- gs:start(),
- S = init_window(#state{}),
- loop(S).
-
--spec loop(#state{}) -> no_return().
-
-loop(State) ->
- receive
- {gs, code_listbox, click, Data, [Idx, Txt | _]} ->
- NewState = update_module_box(State,Idx,Data,Txt),
- loop(NewState);
- {gs, module_listbox, click, Data, [Idx, _Txt | _]} ->
- NewState = update_fun(State,Idx,Data),
- loop(NewState);
- {gs, compmod, click, _, _} ->
- loop(compile(State));
- {gs, prof, click, [], ["Turn off\nProfiling"]} ->
- hipe_profile:prof_module_off(State#state.mod),
- loop(update_module_box(State,State#state.mindex,State#state.mods,""));
- {gs, prof, click, [], _} ->
- hipe_profile:prof_module(State#state.mod),
- loop(update_module_box(State,State#state.mindex,State#state.mods,""));
- {gs, win, configure, _, _} ->
- gs:config(win, [{width, ?WINDOW_WIDTH}, {height, ?WINDOW_HEIGHT}]),
- loop(State);
-
- show_window when State#state.win_created =:= true ->
- gs:config(win, [raise]),
- loop(State);
- show_window when State#state.win_created =:= false ->
- loop((init_window(State))#state{win_created = true});
-
- {gs, _Id, click, close_menu, _Args} ->
- gs:destroy(win),
- loop(State#state{win_created = false});
- {gs, _Id, keypress, _Data, [c, _, 0, 1 | _]} ->
- gs:destroy(win),
- loop(State#state{win_created = false});
- {gs, _Id, keypress, _Data, ['C', _, 1, 1 | _]} ->
- gs:destroy(win),
- loop(State#state{win_created = false});
- {gs, _Id, keypress, _Data, _Args} ->
- loop(State);
- {gs, _, destroy, _, _} ->
- loop(State#state{win_created = false});
-
- {compilation_done, _Res, Sender} ->
- case State#state.compiling of
- Sender ->
- catch gs:config(compmod, [{enable, true}]),
- update_text(compiling, ""),
- loop(update_module_box(State,
- State#state.mindex,
- State#state.mods, ""));
- _ ->
- loop(State)
- end;
-
- {'EXIT', _Pid, _Reason} ->
- exit(normal);
- _Other ->
- io:format("HiPE window received message ~p ~n", [_Other]),
- loop(State)
- after
- ?POLL_INTERVAL ->
- loop(update_code_listbox(State))
- end.
-
--spec init_window(#state{}) -> #state{}.
-
-init_window(State) ->
- create_window(State),
- gs:config(win, [{map,true}]),
- update_code_listbox(State#state{win_created = true}).
-
--spec create_window(#state{}) -> 'ok'.
-
-create_window(State) ->
- gs:window(win, gs:start(), [{width, ?WINDOW_WIDTH},
- {height, ?WINDOW_HEIGHT},
- {bg, ?DEFAULT_BG_COLOR},
- {title, "[HiPE] Code list"},
- {configure, true},
- {destroy, true},
- {cursor, arrow},
- {keypress, true}
- ]),
- create_menu(),
- Xpos = 4,
- Ypos1 = 60,
- Width = (?WINDOW_WIDTH - (Xpos*4)) div 3,
- create_labels([{mods,Ypos1-20,"Loaded Modules"}], Xpos + 1 + 3),
- Xpos2 = Xpos*2+Width,
- create_labels([{mod,Ypos1-20,"Module:"++atom_to_list(State#state.mod)},
- {ver,Ypos1,""},
- {time,Ypos1+20,""},
- {native,Ypos1+40,""},
- {compiling,Ypos1+60,""}], Xpos2),
- create_labels([{function,Ypos1-20,"Function:"},
- {nativefun,Ypos1,""}], Xpos*3+Width*2),
- Ypos = 240,
- Height1 = ?WINDOW_HEIGHT - Ypos1 - Xpos,
- Height = ?WINDOW_HEIGHT - Ypos - Xpos,
- gs:listbox(code_listbox, win, [{x, Xpos},
- {y, Ypos1},
- {width, Width},
- {height, Height1},
- {bg, {255,255,255}},
- {vscroll, right},
- {hscroll, true},
- {click, true}]),
- gs:listbox(module_listbox, win, [{x, Xpos*2+Width},
- {y, Ypos},
- {width, Width},
- {height, Height},
- {bg, {255,255,255}},
- {vscroll, right},
- {hscroll, true},
- {click, true}]),
- gs:listbox(profile_listbox, win, [{x, Xpos*3+Width*2},
- {y, Ypos1+40},
- {width, Width},
- {height, Height-60},
- {bg, {255,255,255}},
- {vscroll, right},
- {hscroll, true},
- {click, true}]),
- gs:button(compmod,win,[{label,{text,"Compile\nModule"}},
- {justify,center},
- {x,Xpos*2+Width*1},
- {height,60},
- {y,Ypos-80}]),
- gs:button(prof,win,[{label,{text,"Profile\nModule"}},
- {justify,center},
- {x,Xpos*2+Width*1+100},
- {height,60},
- {y,Ypos-80}]),
- gs:button(clearprof,win,[{label, {text,"Clear\nProfile"}},
- {justify, center},
- {x, Xpos*2+Width*1+200},
- {height, 60},
- {y, Ypos-80}]),
- gs:editor(edoc,win,[{x, Xpos*3+Width*2}, {y, Ypos},
- {width, Width}, {height, Height},
- {insert, {'end',"Edit this text!"}},
- {vscroll, right},
- {hscroll, true},
- {wrap, none}]),
- ok.
-
--spec create_menu() -> 'ok'.
-
-create_menu() ->
- gs:menubar(menubar, win, [{bg, ?DEFAULT_BG_COLOR}]),
- create_sub_menus([{mbutt, fmenu, " File",
- [{" Close Ctrl-C ",close_menu}]},
- {mbuttc,cmenu, " Compile ",
- [{" Compile Module", comp_mod}]},
- {mbuttp,pmenu, " Profile ",
- [{" Profile Module", prof_mod}]},
- {mbutte,emenu, " Edoc", [separator]},
- {mbutta,amenu, " Analyze ", [separator]},
- {mbuttb,bmenu, " Benchmark ", [separator]},
- {mbuttj,jmenu, " Jit ", [separator]}]),
- ok.
-
-create_menuitems(Parent, [{Text,Data}|Rest]) ->
- gs:menuitem(Parent, [{bg, ?DEFAULT_BG_COLOR},
- {fg, {178, 34, 34}},
- {label, {text, Text}},
- {data, Data},
- {underline, 1}
- ]),
- create_menuitems(Parent, Rest);
-create_menuitems(Parent, [separator|Rest]) ->
- gs:menuitem(Parent, [{itemtype, separator}]),
- create_menuitems(Parent, Rest);
-create_menuitems(_, []) -> ok.
-
-create_sub_menus([{Parent, Name, Text, Items}|Rest]) ->
- BG = {bg, ?DEFAULT_BG_COLOR},
- FG = {fg, {178, 34, 34}}, % firebrick
- Label = {label, {text, Text}},
- gs:menubutton(Parent, menubar, [BG, FG, Label, {underline, 1}]),
- gs:menu(Name, Parent, [BG, FG]),
- create_menuitems(Name, Items),
- create_sub_menus(Rest);
-create_sub_menus([]) -> ok.
-
-create_labels([{Name,Y,Text}|Rest], Xpos) ->
- gs:label(Name, win, [{width, (?WINDOW_WIDTH - 16) div 3},
- {height, 20},
- {x, Xpos + 1 + 3},
- {y, Y},
- {bg, ?DEFAULT_BG_COLOR},
- {fg, ?NORMAL_FG_COLOR},
- {font, ?HEADER_FONT},
- {align, w},
- {label, {text, Text}}
- ]),
- create_labels(Rest,Xpos);
-create_labels([],_) -> ok.
-
--spec update_code_listbox(#state{}) -> #state{}.
-
-update_code_listbox(State) ->
- Mods = lists:sort(mods()),
- case State#state.win_created of
- false ->
- State;
- true ->
- case Mods =:= State#state.mods of
- true -> State;
- false ->
- update_text(mods,
- "Loaded Modules ("++
- integer_to_list(length(Mods))++")"),
- catch gs:config(code_listbox, [{data, Mods},
- {items, Mods},
- {selection, 0}]),
- update_module_box(State#state{mods = Mods}, 0, Mods, "")
- end
- end.
-
--spec update_fun(#state{}, integer(), [mfa()]) -> #state{}.
-
-update_fun(State, Idx, Data) ->
- case State#state.win_created of
- false ->
- State;
- true ->
- MFA = {M,F,A} = get_selection(Idx, Data, {?MODULE,start,0}),
- update_text(function, "Function: "++mfa_to_string(MFA)),
- case in_native(F, A, native_code(M)) of
- true -> update_text(nativefun, "Native");
- false -> update_text(nativefun, "Emulated")
- end,
- State
- end.
-
-get_selection(Idx, Data, Default) ->
- try lists:nth(Idx+1, Data) catch _:_ -> Default end.
-
--spec update_module_box(#state{}, integer(), [atom()], string()) -> #state{}.
-
-update_module_box(State, Idx, Data, _Txt) ->
- case State#state.win_created of
- false ->
- State;
- true ->
- Mod = get_selection(Idx, Data, hipe_tool),
- %% io:format("~w\n", [Mod:module_info()]),
- Info = Mod:module_info(),
- Funs = lists:usort(funs(Mod)),
- MFAs = mfas(Mod, Funs),
- ModText = atom_to_list(Mod),
- update_text(mod, "Module:"++ModText),
- update_text(compmod, "Compile\nModule\n"++ModText),
- Options = get_compile(Info),
- update_text(ver, get_version(Options)),
- update_text(time, get_time(Options)),
- NativeCode = native_code(Mod),
-
- Prof = is_profiled(Mod),
- if Prof -> update_text(prof, "Turn off\nProfiling");
- true -> update_text(prof, "Profile\n"++ModText)
- end,
-
- Mode = get_mode(Funs, NativeCode),
-
- update_text(native, Mode),
- Items = fun_names(Mod, Funs, NativeCode, Prof),
-
- Selection = {selection, 0},
- catch gs:config(module_listbox, [{data, MFAs},
- {items, Items},
- Selection]),
- ProfData = [mfa_to_string(element(1, X)) ++ " " ++
- integer_to_list(element(2,X))
- || X <- hipe_profile:res(), element(2, X) > 0],
- catch gs:config(profile_listbox, [{data, ProfData},
- {items, ProfData},
- Selection]),
- get_edoc(Mod),
- update_fun(State#state{mindex = Idx, mod = Mod, funs = Funs}, 0, MFAs)
- end.
-
-update_text(Lab, Text) ->
- catch gs:config(Lab, [{label, {text, Text}}]).
-
-%%---------------------------------------------------------------------
-%% @doc Returns a list of all loaded modules.
-%%---------------------------------------------------------------------
-
--spec mods() -> [atom()].
-
-mods() ->
- [Mod || {Mod,_File} <- code:all_loaded()].
-
--spec funs(module()) -> [fa()].
-
-funs(Mod) ->
- Mod:module_info(functions).
-
--spec native_code(module()) -> [fa_address()].
-
-native_code(Mod) ->
- Mod:module_info(native_addresses).
-
--spec mfas(atom(), [fa()]) -> [mfa()].
-
-mfas(Mod, Funs) ->
- [{Mod,F,A} || {F,A} <- Funs].
-
--spec fun_names(atom(), [fa()], [fa_address()], boolean()) -> [string()].
-
-fun_names(M, Funs, NativeCode, Prof) ->
- [atom_to_list(F) ++ "/" ++ integer_to_list(A)
- ++
- (case in_native(F, A, NativeCode) of
- true -> " [native] ";
- false -> ""
- end)
- ++
- if Prof ->
- (catch integer_to_list(hipe_bifs:call_count_get({M,F,A})));
- true -> ""
- end
- || {F,A} <- Funs].
-
--spec in_native(atom(), arity(), [fa_address()]) -> boolean().
-
-in_native(F, A, NativeCode) ->
- lists:any(fun({Fun,Arity,_}) ->
- (Fun =:= F andalso Arity =:= A)
- end,
- NativeCode).
-
--spec mfa_to_string(mfa()) -> [char(),...].
-
-mfa_to_string({M,F,A}) ->
- atom_to_list(M) ++ ":" ++ atom_to_list(F) ++ "/" ++ integer_to_list(A).
-
-get_mode(Funs, NativeCode) ->
- case NativeCode of
- [] -> "Emulated";
- InNative when is_list(InNative) ->
- if length(InNative) =:= length(Funs) ->
- "Native";
- true -> "Mixed"
- end
- end.
-
-get_time(Comp) ->
- case lists:keyfind(time, 1, Comp) of
- {_, {Y,Month,D,H,Min,S}} ->
- integer_to_list(Y) ++
- "-" ++ integer_to_list(Month) ++
- "-" ++ integer_to_list(D) ++ " " ++
- integer_to_list(H) ++ ":" ++ integer_to_list(Min) ++
- ":" ++ integer_to_list(S);
- false -> ""
- end.
-
-get_version(Comp) ->
- case lists:keyfind(version, 1, Comp) of
- {_, V} when is_list(V) -> V;
- false -> ""
- end.
-
-get_cwd(Options) ->
- case lists:keyfind(cwd, 1, Options) of
- {_, V} when is_atom(V) -> atom_to_list(V);
- {_, V} -> V;
- false -> ""
- end.
-
-get_options(Comp) ->
- case lists:keyfind(options, 1, Comp) of
- {_, V} when is_list(V) -> V;
- false -> ""
- end.
-
-get_compile(Info) ->
- case lists:keyfind(compile, 1, Info) of
- {_, O} when is_list(O) -> O;
- false -> []
- end.
-
--spec is_profiled(atom()) -> boolean().
-
-is_profiled(Mod) ->
- case hipe_bifs:call_count_get({Mod,module_info,0}) of
- false -> false;
- C when is_integer(C) -> true
- end.
-
--spec compile(#state{}) -> #state{}.
-
-compile(State) ->
- catch gs:config(compmod, [{enable, false}]),
- update_text(compiling, "Compiling..."),
- Parent = self(),
- P = spawn(fun() -> c(Parent, State#state.mod, State#state.options) end),
- State#state{compiling = P}.
-
--spec c(pid(), atom(), comp_options()) -> 'ok'.
-
-c(Parent, Mod, Options) ->
- Res = hipe:c(Mod, Options),
- Parent ! {compilation_done,Res,self()},
- ok.
-
-get_edoc(Mod) ->
- Info = Mod:module_info(),
- Comp = get_compile(Info),
- Options = get_options(Comp),
- Dir = get_cwd(Options),
- File =
- case Dir of
- "" -> atom_to_list(Mod) ++ ".erl";
- _ -> Dir ++"/" ++ atom_to_list(Mod) ++ ".erl"
- end,
- %% io:format("Get ~s\n", [File]),
- Text = try edoc(File, [{xml_export,xmerl_text}, no_output])
- catch _:_ -> "error"
- end,
- gs:config(edoc, {enable, true}),
- gs:config(edoc, clear),
- gs:config(edoc, {insert, {insert, Text}}),
- gs:config(edoc, {enable, false}),
- ok.
-
-edoc(Name, Opts) ->
- Doc = edoc:get_doc(Name, Opts),
- %% Comments = edoc:read_comments(Name, Opts),
- %% Text = edoc:forms(Forms, Comments, Name, Opts),
- edoc:layout(Doc, Opts),
- ok.
diff --git a/lib/hipe/util/hipe_digraph.erl b/lib/hipe/util/hipe_digraph.erl
index fcfaa64684..01b1f8c77c 100644
--- a/lib/hipe/util/hipe_digraph.erl
+++ b/lib/hipe/util/hipe_digraph.erl
@@ -2,7 +2,7 @@
%%
%% %CopyrightBegin%
%%
-%% Copyright Ericsson AB 2005-2010. All Rights Reserved.
+%% Copyright Ericsson AB 2005-2014. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
@@ -36,10 +36,10 @@
-type ordset(T) :: [T]. % XXX: temporarily
--record(hipe_digraph, {edges = dict:new() :: dict(),
- rev_edges = dict:new() :: dict(),
+-record(hipe_digraph, {edges = dict:new() :: dict:dict(),
+ rev_edges = dict:new() :: dict:dict(),
leaves = ordsets:new() :: ordset(_), % ???
- nodes = sets:new() :: set()}).
+ nodes = sets:new() :: sets:set()}).
-opaque hdg() :: #hipe_digraph{}.
diff --git a/lib/hipe/util/hipe_dot.erl b/lib/hipe/util/hipe_dot.erl
index e4a47ae0c4..94f7fd60cc 100644
--- a/lib/hipe/util/hipe_dot.erl
+++ b/lib/hipe/util/hipe_dot.erl
@@ -2,7 +2,7 @@
%%%
%%% %CopyrightBegin%
%%%
-%%% Copyright Ericsson AB 2004-2011. All Rights Reserved.
+%%% Copyright Ericsson AB 2004-2014. All Rights Reserved.
%%%
%%% The contents of this file are subject to the Erlang Public License,
%%% Version 1.1, (the "License"); you may not use this file except in
@@ -70,13 +70,13 @@
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
--spec translate_digraph(digraph(), string(), string()) -> 'ok'.
+-spec translate_digraph(digraph:graph(), string(), string()) -> 'ok'.
translate_digraph(G, FileName, GName) ->
translate_digraph(G, FileName, GName,
fun(X) -> io_lib:format("~p", [X]) end, []).
--spec translate_digraph(digraph(), string(), string(),
+-spec translate_digraph(digraph:graph(), string(), string(),
fun((_) -> string()), [_]) -> 'ok'.
translate_digraph(G, FileName, GName, Fun, Opts) ->
diff --git a/lib/hipe/util/hipe_vectors.hrl b/lib/hipe/util/hipe_vectors.hrl
index 043faf4c91..5e24db238d 100644
--- a/lib/hipe/util/hipe_vectors.hrl
+++ b/lib/hipe/util/hipe_vectors.hrl
@@ -1,7 +1,7 @@
%%
%% %CopyrightBegin%
%%
-%% Copyright Ericsson AB 2008-2009. All Rights Reserved.
+%% Copyright Ericsson AB 2008-2014. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
@@ -24,5 +24,5 @@
-endif.
-ifdef(USE_GBTREES).
--type hipe_vector() :: gb_tree().
+-type hipe_vector() :: gb_trees:tree().
-endif.
diff --git a/lib/hipe/vsn.mk b/lib/hipe/vsn.mk
index ed4b4dc8d2..fb7e4b91a0 100644
--- a/lib/hipe/vsn.mk
+++ b/lib/hipe/vsn.mk
@@ -1 +1 @@
-HIPE_VSN = 3.10.2.2
+HIPE_VSN = 3.10.3
diff --git a/lib/hipe/x86/hipe_x86_assemble.erl b/lib/hipe/x86/hipe_x86_assemble.erl
index 7878c7219d..3f756769c4 100644
--- a/lib/hipe/x86/hipe_x86_assemble.erl
+++ b/lib/hipe/x86/hipe_x86_assemble.erl
@@ -21,7 +21,6 @@
%%%
%%% TODO:
%%% - Simplify combine_label_maps and mk_data_relocs.
-%%% - Move find_const to hipe_pack_constants?
-ifdef(HIPE_AMD64).
-define(HIPE_X86_ASSEMBLE, hipe_amd64_assemble).
@@ -80,8 +79,8 @@ assemble(CompiledCode, Closures, Exports, Options) ->
%% ?debug_msg("Constants are ~w bytes\n",[ConstSize])),
%%
SC = hipe_pack_constants:slim_constmap(ConstMap),
- DataRelocs = mk_data_relocs(RefsFromConsts, LabelMap),
- SSE = slim_sorted_exportmap(ExportMap,Closures,Exports),
+ DataRelocs = hipe_pack_constants:mk_data_relocs(RefsFromConsts, LabelMap),
+ SSE = hipe_pack_constants:slim_sorted_exportmap(ExportMap,Closures,Exports),
SlimRefs = hipe_pack_constants:slim_refs(AccRefs),
Bin = term_to_binary([{?VERSION_STRING(),?HIPE_SYSTEM_CRC},
ConstAlign, ConstSize,
@@ -442,7 +441,7 @@ translate_imm(#x86_imm{value=Imm}, Context, MayTrunc8) ->
case Imm of
{Label,constant} ->
{MFA,ConstMap} = Context,
- ConstNo = find_const({MFA,Label}, ConstMap),
+ ConstNo = hipe_pack_constants:find_const({MFA,Label}, ConstMap),
{constant,ConstNo};
{Label,closure} ->
{closure,Label};
@@ -712,7 +711,7 @@ resolve_jmp_switch_arg(I, _Context) ->
{rm64,hipe_amd64_encode:rm_mem(EA)}.
-else.
resolve_jmp_switch_arg(I, {MFA,ConstMap}) ->
- ConstNo = find_const({MFA,hipe_x86:jmp_switch_jtab(I)}, ConstMap),
+ ConstNo = hipe_pack_constants:find_const({MFA,hipe_x86:jmp_switch_jtab(I)}, ConstMap),
Disp32 = {?LOAD_ADDRESS,{constant,ConstNo}},
SINDEX = ?HIPE_X86_ENCODE:sindex(2, hipe_x86:temp_reg(hipe_x86:jmp_switch_temp(I))),
EA = ?HIPE_X86_ENCODE:ea_disp32_sindex(Disp32, SINDEX), % this creates a SIB implicitly
@@ -932,37 +931,6 @@ resolve_x87_binop_args(Src=#x86_fpreg{}, Dst=#x86_fpreg{})->
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-mk_data_relocs(RefsFromConsts, LabelMap) ->
- lists:flatten(mk_data_relocs(RefsFromConsts, LabelMap, [])).
-
-mk_data_relocs([{MFA,Labels} | Rest], LabelMap, Acc) ->
- Map = [case Label of
- {L,Pos} ->
- Offset = find({MFA,L}, LabelMap),
- {Pos,Offset};
- {sorted,Base,OrderedLabels} ->
- {sorted, Base, [begin
- Offset = find({MFA,L}, LabelMap),
- {Order, Offset}
- end
- || {L,Order} <- OrderedLabels]}
- end
- || Label <- Labels],
- %% msg("Map: ~w Map\n",[Map]),
- mk_data_relocs(Rest, LabelMap, [Map,Acc]);
-mk_data_relocs([],_,Acc) -> Acc.
-
-find({MFA,L},LabelMap) ->
- gb_trees:get({MFA,L}, LabelMap).
-
-slim_sorted_exportmap([{Addr,M,F,A}|Rest], Closures, Exports) ->
- IsClosure = lists:member({M,F,A}, Closures),
- IsExported = is_exported(F, A, Exports),
- [Addr,M,F,A,IsClosure,IsExported | slim_sorted_exportmap(Rest, Closures, Exports)];
-slim_sorted_exportmap([],_,_) -> [].
-
-is_exported(F, A, Exports) -> lists:member({F,A}, Exports).
-
%%%
%%% Assembly listing support (pp_asm option).
%%%
@@ -1001,14 +969,3 @@ fill_spaces(N) when N > 0 ->
fill_spaces(N-1);
fill_spaces(0) ->
[].
-
-%%%
-%%% Lookup a constant in a ConstMap.
-%%%
-
-find_const({MFA,Label},[{pcm_entry,MFA,Label,ConstNo,_,_,_}|_]) ->
- ConstNo;
-find_const(N,[_|R]) ->
- find_const(N,R);
-find_const(C,[]) ->
- ?EXIT({constant_not_found,C}).
diff --git a/lib/hipe/x86/hipe_x86_postpass.erl b/lib/hipe/x86/hipe_x86_postpass.erl
index c0918c4f89..a95a8745ba 100644
--- a/lib/hipe/x86/hipe_x86_postpass.erl
+++ b/lib/hipe/x86/hipe_x86_postpass.erl
@@ -1,8 +1,8 @@
-%%% -*- coding: utf-8; erlang-indent-level: 2 -*-
+%%% -*- erlang-indent-level: 2 -*-
%%%
%%% %CopyrightBegin%
%%%
-%%% Copyright Ericsson AB 2003-2012. All Rights Reserved.
+%%% Copyright Ericsson AB 2003-2013. All Rights Reserved.
%%%
%%% The contents of this file are subject to the Erlang Public License,
%%% Version 1.1, (the "License"); you may not use this file except in