aboutsummaryrefslogtreecommitdiffstats
path: root/lib/stdlib/src/erl_bits.erl
blob: 5851401026192b8580322f9d7c5fc9a5847c422a (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
%%
%% %CopyrightBegin%
%% 
%% Copyright Ericsson AB 1999-2016. All Rights Reserved.
%% 
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%     http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%% 
%% %CopyrightEnd%
%%

-module(erl_bits).

-export([system_bittypes/0, 
	 system_bitdefault/0,
	 set_bit_type/2,
	 as_list/1]).

-include("../include/erl_bits.hrl").

%% Dummies.

-spec system_bitdefault() -> 'no_system_bitdefault'.

system_bitdefault() -> no_system_bitdefault.

-spec system_bittypes() -> 'no_system_types'.

system_bittypes() -> no_system_types.

-spec as_list(#bittype{}) ->
    [bt_endian() | bt_sign() | bt_type() | {'unit', 'undefined' | bt_unit()}].

as_list(Bt) ->
    [Bt#bittype.type,{unit,Bt#bittype.unit},Bt#bittype.sign,Bt#bittype.endian].

%% XXX: tuple() below stands for what's produced by the parser
%%   {integer,L,M} | {var,L,VAR} | {atom,L,ATOM} | {op,L,OP,OP1,OP2} | ...
-type size() :: 'all' | 'unknown' | non_neg_integer() | tuple(). % XXX: REFINE
-type type() :: 'bytes' | 'bitstring' | 'bits'
              | bt_type() | bt_endian() | bt_sign()
              | {'unit', 'undefined' | bt_unit()}.

-spec set_bit_type('default' | size(), 'default' | [type()]) ->
        {'ok', 'undefined' | size(), #bittype{}} |
        {'error', {'undefined_bittype', term()}} |
        {'error', {'bittype_mismatch', term(), term(), string()}}.

set_bit_type(Size, default) ->
    set_bit_type(Size, []);
set_bit_type(Size, TypeList) ->
    try
	#bittype{type=Type,unit=Unit,sign=Sign,endian=Endian} =
	set_bit(TypeList),
	apply_defaults(Type, Size, Unit, Sign, Endian)
    catch
	throw:Error -> Error
    end.

set_bit([]) -> #bittype{};
set_bit([H|T]) -> set_bit_1(T, type_to_record(H)).

set_bit_1([T0|Ts], Bt0) ->
    Type = type_to_record(T0),
    Bt = merge_bittype(Type, Bt0),
    set_bit_1(Ts, Bt);
set_bit_1([], Bt) -> Bt.

type_to_record(integer) ->   #bittype{type   = integer};
type_to_record(utf8) ->      #bittype{type   = utf8};
type_to_record(utf16) ->     #bittype{type   = utf16};
type_to_record(utf32) ->     #bittype{type   = utf32};
type_to_record(float) ->     #bittype{type   = float};
type_to_record(binary) ->    #bittype{type   = binary};
type_to_record(bytes) ->     #bittype{type   = binary, unit = 8};
type_to_record(bitstring) -> #bittype{type   = binary, unit = 1};
type_to_record(bits) ->      #bittype{type   = binary, unit = 1};

type_to_record({unit,undefined}) ->
    #bittype{unit=undefined};
type_to_record({unit,Sz}) when is_integer(Sz), Sz > 0, Sz =< 256 ->
    #bittype{unit=Sz};

type_to_record(big) ->       #bittype{endian = big};
type_to_record(little) ->    #bittype{endian = little};
type_to_record(native) ->    #bittype{endian = native};

type_to_record(signed) ->    #bittype{sign   = signed};
type_to_record(unsigned) ->  #bittype{sign   = unsigned};

type_to_record(Name) ->      throw({error,{undefined_bittype,Name}}).

%%
%% Merge two bit type specifications.
%%
merge_bittype(B1, B2) ->
    Endian = merge_field(B1#bittype.endian, B2#bittype.endian, endianness),
    Sign   = merge_field(B1#bittype.sign, B2#bittype.sign, sign),
    Type   = merge_field(B1#bittype.type, B2#bittype.type, type),
    Unit   = merge_field(B1#bittype.unit, B2#bittype.unit, unit),
    #bittype{type=Type,unit=Unit,endian=Endian,sign=Sign}.

merge_field(undefined, B, _) -> B;
merge_field(A, undefined, _) -> A;
merge_field(A, A, _) -> A;
merge_field(X, Y, What) ->
    throw({error,{bittype_mismatch,X,Y,atom_to_list(What)}}).

%%
%% Defaults are as follows.
%% 
%% The default is integer.
%% The default size is 'all' for binaries, 8 for integers, 64 for floats.
%% No unit must be given if the size is not given.
%% The default unit size is 8 for binaries, and 1 for integers and floats.
%% The default sign is always unsigned.
%% The default endian is always big.
%%

apply_defaults(undefined, Size, Unit, Sign, Endian) -> %default type
    apply_defaults(integer, Size, Unit, Sign, Endian);

apply_defaults(binary, default, Unit, Sign, Endian) -> %default size
    %% check_unit(Unit), removed to allow bitlevel binaries
    apply_defaults(binary, all, Unit, Sign, Endian);
apply_defaults(integer, default, Unit, Sign, Endian) ->
    check_unit(Unit),
    apply_defaults(integer, 8, 1, Sign, Endian);
apply_defaults(utf8=Type, default, Unit, Sign, Endian) ->
    apply_defaults(Type, undefined, Unit, Sign, Endian);
apply_defaults(utf16=Type, default, Unit, Sign, Endian) ->
    apply_defaults(Type, undefined, Unit, Sign, Endian);
apply_defaults(utf32=Type, default, Unit, Sign, Endian) ->
    apply_defaults(Type, undefined, Unit, Sign, Endian);
apply_defaults(float, default, Unit, Sign, Endian) ->
    check_unit(Unit),
    apply_defaults(float, 64, 1, Sign, Endian);

apply_defaults(binary, Size, undefined, Sign, Endian) -> %default unit
    apply_defaults(binary, Size, 8, Sign, Endian);
apply_defaults(integer, Size, undefined, Sign, Endian) ->
    apply_defaults(integer, Size, 1, Sign, Endian);
apply_defaults(float, Size, undefined, Sign, Endian) ->
    apply_defaults(float, Size, 1, Sign, Endian);

apply_defaults(Type, Size, Unit, undefined, Endian) -> %default sign
    apply_defaults(Type, Size, Unit, unsigned, Endian);

apply_defaults(Type, Size, Unit, Sign, undefined) -> %default endian
    apply_defaults(Type, Size, Unit, Sign, big);

apply_defaults(Type, Size, Unit, Sign, Endian) -> %done
    check_size_unit(Type, Size, Unit),
    {ok,Size,#bittype{type=Type,unit=Unit,sign=Sign,endian=Endian}}.

check_size_unit(utf8, Size, Unit) ->
    check_size_unit_1(Size, Unit);
check_size_unit(utf16, Size, Unit) ->
    check_size_unit_1(Size, Unit);
check_size_unit(utf32, Size, Unit) ->
    check_size_unit_1(Size, Unit);
check_size_unit(_, _, _) -> ok.

check_size_unit_1(Size, Unit) ->
    case Size of
	default -> ok;
	undefined -> ok;
	{atom,_,undefined} -> ok;
	{value,_,undefined} -> ok;
	_ -> throw({error,utf_bittype_size_or_unit})
    end,
    case Unit of
	undefined -> ok;
	_ -> throw({error,utf_bittype_size_or_unit})
    end.

check_unit(undefined) -> ok;
check_unit(_) -> throw({error,bittype_unit}).