1*e4b17023SJohn Marino /* Generic streaming support for basic data types.
2*e4b17023SJohn Marino
3*e4b17023SJohn Marino Copyright 2011 Free Software Foundation, Inc.
4*e4b17023SJohn Marino Contributed by Diego Novillo <dnovillo@google.com>
5*e4b17023SJohn Marino
6*e4b17023SJohn Marino This file is part of GCC.
7*e4b17023SJohn Marino
8*e4b17023SJohn Marino GCC is free software; you can redistribute it and/or modify it under
9*e4b17023SJohn Marino the terms of the GNU General Public License as published by the Free
10*e4b17023SJohn Marino Software Foundation; either version 3, or (at your option) any later
11*e4b17023SJohn Marino version.
12*e4b17023SJohn Marino
13*e4b17023SJohn Marino GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14*e4b17023SJohn Marino WARRANTY; without even the implied warranty of MERCHANTABILITY or
15*e4b17023SJohn Marino FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16*e4b17023SJohn Marino for more details.
17*e4b17023SJohn Marino
18*e4b17023SJohn Marino You should have received a copy of the GNU General Public License
19*e4b17023SJohn Marino along with GCC; see the file COPYING3. If not see
20*e4b17023SJohn Marino <http://www.gnu.org/licenses/>. */
21*e4b17023SJohn Marino
22*e4b17023SJohn Marino #include "config.h"
23*e4b17023SJohn Marino #include "system.h"
24*e4b17023SJohn Marino #include "coretypes.h"
25*e4b17023SJohn Marino #include "data-streamer.h"
26*e4b17023SJohn Marino
27*e4b17023SJohn Marino /* Pack WORK into BP in a variant of uleb format. */
28*e4b17023SJohn Marino
29*e4b17023SJohn Marino void
bp_pack_var_len_unsigned(struct bitpack_d * bp,unsigned HOST_WIDE_INT work)30*e4b17023SJohn Marino bp_pack_var_len_unsigned (struct bitpack_d *bp, unsigned HOST_WIDE_INT work)
31*e4b17023SJohn Marino {
32*e4b17023SJohn Marino do
33*e4b17023SJohn Marino {
34*e4b17023SJohn Marino unsigned int half_byte = (work & 0x7);
35*e4b17023SJohn Marino work >>= 3;
36*e4b17023SJohn Marino if (work != 0)
37*e4b17023SJohn Marino /* More half_bytes to follow. */
38*e4b17023SJohn Marino half_byte |= 0x8;
39*e4b17023SJohn Marino
40*e4b17023SJohn Marino bp_pack_value (bp, half_byte, 4);
41*e4b17023SJohn Marino }
42*e4b17023SJohn Marino while (work != 0);
43*e4b17023SJohn Marino }
44*e4b17023SJohn Marino
45*e4b17023SJohn Marino
46*e4b17023SJohn Marino /* Pack WORK into BP in a variant of sleb format. */
47*e4b17023SJohn Marino
48*e4b17023SJohn Marino void
bp_pack_var_len_int(struct bitpack_d * bp,HOST_WIDE_INT work)49*e4b17023SJohn Marino bp_pack_var_len_int (struct bitpack_d *bp, HOST_WIDE_INT work)
50*e4b17023SJohn Marino {
51*e4b17023SJohn Marino int more, half_byte;
52*e4b17023SJohn Marino
53*e4b17023SJohn Marino do
54*e4b17023SJohn Marino {
55*e4b17023SJohn Marino half_byte = (work & 0x7);
56*e4b17023SJohn Marino /* arithmetic shift */
57*e4b17023SJohn Marino work >>= 3;
58*e4b17023SJohn Marino more = !((work == 0 && (half_byte & 0x4) == 0)
59*e4b17023SJohn Marino || (work == -1 && (half_byte & 0x4) != 0));
60*e4b17023SJohn Marino if (more)
61*e4b17023SJohn Marino half_byte |= 0x8;
62*e4b17023SJohn Marino
63*e4b17023SJohn Marino bp_pack_value (bp, half_byte, 4);
64*e4b17023SJohn Marino }
65*e4b17023SJohn Marino while (more);
66*e4b17023SJohn Marino }
67*e4b17023SJohn Marino
68*e4b17023SJohn Marino
69*e4b17023SJohn Marino /* Unpack VAL from BP in a variant of uleb format. */
70*e4b17023SJohn Marino
71*e4b17023SJohn Marino unsigned HOST_WIDE_INT
bp_unpack_var_len_unsigned(struct bitpack_d * bp)72*e4b17023SJohn Marino bp_unpack_var_len_unsigned (struct bitpack_d *bp)
73*e4b17023SJohn Marino {
74*e4b17023SJohn Marino unsigned HOST_WIDE_INT result = 0;
75*e4b17023SJohn Marino int shift = 0;
76*e4b17023SJohn Marino unsigned HOST_WIDE_INT half_byte;
77*e4b17023SJohn Marino
78*e4b17023SJohn Marino while (true)
79*e4b17023SJohn Marino {
80*e4b17023SJohn Marino half_byte = bp_unpack_value (bp, 4);
81*e4b17023SJohn Marino result |= (half_byte & 0x7) << shift;
82*e4b17023SJohn Marino shift += 3;
83*e4b17023SJohn Marino if ((half_byte & 0x8) == 0)
84*e4b17023SJohn Marino return result;
85*e4b17023SJohn Marino }
86*e4b17023SJohn Marino }
87*e4b17023SJohn Marino
88*e4b17023SJohn Marino
89*e4b17023SJohn Marino /* Unpack VAL from BP in a variant of sleb format. */
90*e4b17023SJohn Marino
91*e4b17023SJohn Marino HOST_WIDE_INT
bp_unpack_var_len_int(struct bitpack_d * bp)92*e4b17023SJohn Marino bp_unpack_var_len_int (struct bitpack_d *bp)
93*e4b17023SJohn Marino {
94*e4b17023SJohn Marino HOST_WIDE_INT result = 0;
95*e4b17023SJohn Marino int shift = 0;
96*e4b17023SJohn Marino unsigned HOST_WIDE_INT half_byte;
97*e4b17023SJohn Marino
98*e4b17023SJohn Marino while (true)
99*e4b17023SJohn Marino {
100*e4b17023SJohn Marino half_byte = bp_unpack_value (bp, 4);
101*e4b17023SJohn Marino result |= (half_byte & 0x7) << shift;
102*e4b17023SJohn Marino shift += 3;
103*e4b17023SJohn Marino if ((half_byte & 0x8) == 0)
104*e4b17023SJohn Marino {
105*e4b17023SJohn Marino if ((shift < HOST_BITS_PER_WIDE_INT) && (half_byte & 0x4))
106*e4b17023SJohn Marino result |= - ((HOST_WIDE_INT)1 << shift);
107*e4b17023SJohn Marino
108*e4b17023SJohn Marino return result;
109*e4b17023SJohn Marino }
110*e4b17023SJohn Marino }
111*e4b17023SJohn Marino }
112