aboutsummaryrefslogtreecommitdiffstats
path: root/lib/asn1/src/prepare_templates.erl
blob: ccd15548d833362f9352d02e5035e073b98b9f55 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 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%
%%

-module(prepare_templates).
-export([gen_asn1ct_rtt/1,gen_asn1ct_eval/1]).

gen_asn1ct_rtt(Ms) ->
    {ok,Fd} = file:open("asn1ct_rtt.erl", [write]),
    io:format(Fd,
	      "%% Generated by ~s. DO NOT EDIT THIS FILE.\n"
	      "%%\n"
	      "%% Input files:\n", [?MODULE]),
    [io:put_chars(Fd, ["%%  ",M,$\n]) || M <- Ms],
    io:nl(Fd),
    io:put_chars(Fd,
		 "-module(asn1ct_rtt).\n"
		 "-export([assert_defined/1,dependencies/1,code/0]).\n"
		 "\n"),
    Forms = lists:sort(lists:append([abstract(M) || M <- Ms])),
    Exp = lists:sort(exports(Forms)),
    defined(Fd, Exp),
    io:nl(Fd),
    Calls = calls(Forms),
    R = sofs:relation(Calls),
    Fam0 = sofs:relation_to_family(R),
    Fam = sofs:to_external(Fam0),
    dependencies(Fd, Fam),
    io:nl(Fd),
    Funcs = [begin
		 Bin = list_to_binary([$\n|erl_pp:function(Func)]),
		 {{M,F,A},Bin}
	     end || {M,{function,_,F,A,_}=Func} <- Forms],
    io:format(Fd, "code() ->\n~p.\n\n", [Funcs]),
    ok = file:close(Fd),
    halt(0).

gen_asn1ct_eval([File]) ->
    Output = filename:rootname(File, ".funcs") ++ ".erl",
    {ok,Fd} = file:open(Output, [write]),
    {ok,Funcs} = file:consult(File),
    asn1ct_func:start_link(),
    [asn1ct_func:need(MFA) || MFA <- Funcs],
    io:format(Fd,
	      "%% Generated by ~s. DO NOT EDIT THIS FILE.\n"
	      "%%\n"
	      "%% Input file: ~s\n\n", [?MODULE,File]),
    io:format(Fd, "-module(~s).\n", [filename:rootname(File)]),
    gen_asn1ct_eval_exp(Fd, Funcs),
    asn1ct_func:generate(Fd),
    ok = file:close(Fd),
    halt(0).

gen_asn1ct_eval_exp(Fd, Funcs) ->
    io:put_chars(Fd, "-export(["),
    gen_asn1ct_eval_exp_1(Fd, Funcs, ""),
    io:put_chars(Fd, "]).\n").

gen_asn1ct_eval_exp_1(Fd, [{_,F,A}|T], Sep) ->
    io:put_chars(Fd, Sep),
    io:format(Fd, "~p/~p", [F,A]),
    gen_asn1ct_eval_exp_1(Fd, T, ",\n");
gen_asn1ct_eval_exp_1(_, [], _) -> ok.

defined(Fd, [H|T]) ->
    io:format(Fd, "assert_defined(~p) -> ok", [H]),
    case T of
	[] ->
	    io:put_chars(Fd, ".\n");
	[_|_] ->
	    io:put_chars(Fd, ";\n"),
	    defined(Fd, T)
    end.

dependencies(Fd, [{K,V}|T]) ->
    io:format(Fd, "dependencies(~p) ->\n~p;\n", [K,V]),
    dependencies(Fd, T);
dependencies(Fd, []) ->
    io:put_chars(Fd, "dependencies(_) -> [].\n").

abstract(File) ->
    {ok,{M0,[{abstract_code,Abstract}]}} =
	beam_lib:chunks(File, [abstract_code]),
    {raw_abstract_v1,Forms} = Abstract,
    M = module(M0),
    [{M,F} || F <- Forms].

module(M0) ->
    "asn1rtt_" ++ M = atom_to_list(M0),
    list_to_atom(M).

exports([{M,{attribute,_,export,L}}|T]) ->
    [{M,F,A} || {F,A} <- L] ++ exports(T);
exports([_|T]) ->
    exports(T);
exports([]) -> [].

calls([{M,{function,_,F,A,Body}}|T]) ->
    MFA = {M,F,A},
    case find_calls(Body, M) -- [MFA] of
	[] ->
	    calls(T);
	[_|_]=Calls ->
	    [{MFA,Callee} || Callee <- Calls] ++ calls(T)
    end;
calls([_|T]) ->
    calls(T);
calls([]) -> [].

find_calls([{call,_,{atom,_,F},Args}|T], M) ->
    Calls = find_calls(Args, M) ++ find_calls(T, M),
    Arity = length(Args),
    case is_bif(F, Arity) of
	false ->
	    [{M,F,Arity}|Calls];
	true ->
	    Calls
    end;
find_calls([{'fun',_,{function,F,A}}|T], M) ->
    [{M,F,A}|find_calls(T, M)];
find_calls([H|T], M) ->
    find_calls(H, M) ++ find_calls(T, M);
find_calls(Tuple, M) when is_tuple(Tuple) ->
    find_calls(tuple_to_list(Tuple), M);
find_calls(_, _) -> [].

is_bif(F, Arity) ->
    erl_internal:bif(F, Arity).