1*e4b17023SJohn Marino /* Routines for saving various data types to a file stream.  This deals
2*e4b17023SJohn Marino    with various data types like strings, integers, enums, etc.
3*e4b17023SJohn Marino 
4*e4b17023SJohn Marino    Copyright 2011 Free Software Foundation, Inc.
5*e4b17023SJohn Marino    Contributed by Diego Novillo <dnovillo@google.com>
6*e4b17023SJohn Marino 
7*e4b17023SJohn Marino This file is part of GCC.
8*e4b17023SJohn Marino 
9*e4b17023SJohn Marino GCC is free software; you can redistribute it and/or modify it under
10*e4b17023SJohn Marino the terms of the GNU General Public License as published by the Free
11*e4b17023SJohn Marino Software Foundation; either version 3, or (at your option) any later
12*e4b17023SJohn Marino version.
13*e4b17023SJohn Marino 
14*e4b17023SJohn Marino GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15*e4b17023SJohn Marino WARRANTY; without even the implied warranty of MERCHANTABILITY or
16*e4b17023SJohn Marino FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
17*e4b17023SJohn Marino for more details.
18*e4b17023SJohn Marino 
19*e4b17023SJohn Marino You should have received a copy of the GNU General Public License
20*e4b17023SJohn Marino along with GCC; see the file COPYING3.  If not see
21*e4b17023SJohn Marino <http://www.gnu.org/licenses/>.  */
22*e4b17023SJohn Marino 
23*e4b17023SJohn Marino #include "config.h"
24*e4b17023SJohn Marino #include "system.h"
25*e4b17023SJohn Marino #include "coretypes.h"
26*e4b17023SJohn Marino #include "data-streamer.h"
27*e4b17023SJohn Marino 
28*e4b17023SJohn Marino /* Return index used to reference STRING of LEN characters in the string table
29*e4b17023SJohn Marino    in OB.  The string might or might not include a trailing '\0'.
30*e4b17023SJohn Marino    Then put the index onto the INDEX_STREAM.
31*e4b17023SJohn Marino    When PERSISTENT is set, the string S is supposed to not change during
32*e4b17023SJohn Marino    duration of the OB and thus OB can keep pointer into it.  */
33*e4b17023SJohn Marino 
34*e4b17023SJohn Marino unsigned
streamer_string_index(struct output_block * ob,const char * s,unsigned int len,bool persistent)35*e4b17023SJohn Marino streamer_string_index (struct output_block *ob, const char *s, unsigned int len,
36*e4b17023SJohn Marino 		       bool persistent)
37*e4b17023SJohn Marino {
38*e4b17023SJohn Marino   struct string_slot **slot;
39*e4b17023SJohn Marino   struct string_slot s_slot;
40*e4b17023SJohn Marino 
41*e4b17023SJohn Marino   s_slot.s = s;
42*e4b17023SJohn Marino   s_slot.len = len;
43*e4b17023SJohn Marino   s_slot.slot_num = 0;
44*e4b17023SJohn Marino 
45*e4b17023SJohn Marino   slot = (struct string_slot **) htab_find_slot (ob->string_hash_table,
46*e4b17023SJohn Marino 						 &s_slot, INSERT);
47*e4b17023SJohn Marino   if (*slot == NULL)
48*e4b17023SJohn Marino     {
49*e4b17023SJohn Marino       struct lto_output_stream *string_stream = ob->string_stream;
50*e4b17023SJohn Marino       unsigned int start = string_stream->total_size;
51*e4b17023SJohn Marino       struct string_slot *new_slot = XOBNEW (&ob->obstack, struct string_slot);
52*e4b17023SJohn Marino       const char *string;
53*e4b17023SJohn Marino 
54*e4b17023SJohn Marino       if (!persistent)
55*e4b17023SJohn Marino 	{
56*e4b17023SJohn Marino 	  char *tmp;
57*e4b17023SJohn Marino 	  string = tmp = XOBNEWVEC (&ob->obstack, char, len);
58*e4b17023SJohn Marino           memcpy (tmp, s, len);
59*e4b17023SJohn Marino         }
60*e4b17023SJohn Marino       else
61*e4b17023SJohn Marino 	string = s;
62*e4b17023SJohn Marino 
63*e4b17023SJohn Marino       new_slot->s = string;
64*e4b17023SJohn Marino       new_slot->len = len;
65*e4b17023SJohn Marino       new_slot->slot_num = start;
66*e4b17023SJohn Marino       *slot = new_slot;
67*e4b17023SJohn Marino       streamer_write_uhwi_stream (string_stream, len);
68*e4b17023SJohn Marino       lto_output_data_stream (string_stream, string, len);
69*e4b17023SJohn Marino       return start + 1;
70*e4b17023SJohn Marino     }
71*e4b17023SJohn Marino   else
72*e4b17023SJohn Marino     {
73*e4b17023SJohn Marino       struct string_slot *old_slot = *slot;
74*e4b17023SJohn Marino       return old_slot->slot_num + 1;
75*e4b17023SJohn Marino     }
76*e4b17023SJohn Marino }
77*e4b17023SJohn Marino 
78*e4b17023SJohn Marino 
79*e4b17023SJohn Marino /* Output STRING of LEN characters to the string table in OB. The
80*e4b17023SJohn Marino    string might or might not include a trailing '\0'. Then put the
81*e4b17023SJohn Marino    index onto the INDEX_STREAM.
82*e4b17023SJohn Marino    When PERSISTENT is set, the string S is supposed to not change during
83*e4b17023SJohn Marino    duration of the OB and thus OB can keep pointer into it.  */
84*e4b17023SJohn Marino 
85*e4b17023SJohn Marino void
streamer_write_string_with_length(struct output_block * ob,struct lto_output_stream * index_stream,const char * s,unsigned int len,bool persistent)86*e4b17023SJohn Marino streamer_write_string_with_length (struct output_block *ob,
87*e4b17023SJohn Marino 				   struct lto_output_stream *index_stream,
88*e4b17023SJohn Marino 				   const char *s, unsigned int len,
89*e4b17023SJohn Marino 				   bool persistent)
90*e4b17023SJohn Marino {
91*e4b17023SJohn Marino   if (s)
92*e4b17023SJohn Marino     streamer_write_uhwi_stream (index_stream,
93*e4b17023SJohn Marino 			        streamer_string_index (ob, s, len, persistent));
94*e4b17023SJohn Marino   else
95*e4b17023SJohn Marino     streamer_write_char_stream (index_stream, 0);
96*e4b17023SJohn Marino }
97*e4b17023SJohn Marino 
98*e4b17023SJohn Marino 
99*e4b17023SJohn Marino /* Output the '\0' terminated STRING to the string
100*e4b17023SJohn Marino    table in OB.  Then put the index onto the INDEX_STREAM.
101*e4b17023SJohn Marino    When PERSISTENT is set, the string S is supposed to not change during
102*e4b17023SJohn Marino    duration of the OB and thus OB can keep pointer into it.  */
103*e4b17023SJohn Marino 
104*e4b17023SJohn Marino void
streamer_write_string(struct output_block * ob,struct lto_output_stream * index_stream,const char * string,bool persistent)105*e4b17023SJohn Marino streamer_write_string (struct output_block *ob,
106*e4b17023SJohn Marino 		       struct lto_output_stream *index_stream,
107*e4b17023SJohn Marino 		       const char *string, bool persistent)
108*e4b17023SJohn Marino {
109*e4b17023SJohn Marino   if (string)
110*e4b17023SJohn Marino     streamer_write_string_with_length (ob, index_stream, string,
111*e4b17023SJohn Marino 				       strlen (string) + 1,
112*e4b17023SJohn Marino 				       persistent);
113*e4b17023SJohn Marino   else
114*e4b17023SJohn Marino     streamer_write_char_stream (index_stream, 0);
115*e4b17023SJohn Marino }
116*e4b17023SJohn Marino 
117*e4b17023SJohn Marino 
118*e4b17023SJohn Marino /* Write a zero to the output stream.  */
119*e4b17023SJohn Marino 
120*e4b17023SJohn Marino void
streamer_write_zero(struct output_block * ob)121*e4b17023SJohn Marino streamer_write_zero (struct output_block *ob)
122*e4b17023SJohn Marino {
123*e4b17023SJohn Marino   streamer_write_char_stream (ob->main_stream, 0);
124*e4b17023SJohn Marino }
125*e4b17023SJohn Marino 
126*e4b17023SJohn Marino 
127*e4b17023SJohn Marino /* Write an unsigned HOST_WIDE_INT value WORK to OB->main_stream.  */
128*e4b17023SJohn Marino 
129*e4b17023SJohn Marino void
streamer_write_uhwi(struct output_block * ob,unsigned HOST_WIDE_INT work)130*e4b17023SJohn Marino streamer_write_uhwi (struct output_block *ob, unsigned HOST_WIDE_INT work)
131*e4b17023SJohn Marino {
132*e4b17023SJohn Marino   streamer_write_uhwi_stream (ob->main_stream, work);
133*e4b17023SJohn Marino }
134*e4b17023SJohn Marino 
135*e4b17023SJohn Marino 
136*e4b17023SJohn Marino /* Write a HOST_WIDE_INT value WORK to OB->main_stream.  */
137*e4b17023SJohn Marino 
138*e4b17023SJohn Marino void
streamer_write_hwi(struct output_block * ob,HOST_WIDE_INT work)139*e4b17023SJohn Marino streamer_write_hwi (struct output_block *ob, HOST_WIDE_INT work)
140*e4b17023SJohn Marino {
141*e4b17023SJohn Marino   streamer_write_hwi_stream (ob->main_stream, work);
142*e4b17023SJohn Marino }
143*e4b17023SJohn Marino 
144*e4b17023SJohn Marino 
145*e4b17023SJohn Marino /* Write an unsigned HOST_WIDE_INT value WORK to OBS.  */
146*e4b17023SJohn Marino 
147*e4b17023SJohn Marino void
streamer_write_uhwi_stream(struct lto_output_stream * obs,unsigned HOST_WIDE_INT work)148*e4b17023SJohn Marino streamer_write_uhwi_stream (struct lto_output_stream *obs,
149*e4b17023SJohn Marino                             unsigned HOST_WIDE_INT work)
150*e4b17023SJohn Marino {
151*e4b17023SJohn Marino   do
152*e4b17023SJohn Marino     {
153*e4b17023SJohn Marino       unsigned int byte = (work & 0x7f);
154*e4b17023SJohn Marino       work >>= 7;
155*e4b17023SJohn Marino       if (work != 0)
156*e4b17023SJohn Marino 	/* More bytes to follow.  */
157*e4b17023SJohn Marino 	byte |= 0x80;
158*e4b17023SJohn Marino 
159*e4b17023SJohn Marino       streamer_write_char_stream (obs, byte);
160*e4b17023SJohn Marino     }
161*e4b17023SJohn Marino   while (work != 0);
162*e4b17023SJohn Marino }
163*e4b17023SJohn Marino 
164*e4b17023SJohn Marino 
165*e4b17023SJohn Marino /* Write a HOST_WIDE_INT value WORK to OBS.  */
166*e4b17023SJohn Marino 
167*e4b17023SJohn Marino void
streamer_write_hwi_stream(struct lto_output_stream * obs,HOST_WIDE_INT work)168*e4b17023SJohn Marino streamer_write_hwi_stream (struct lto_output_stream *obs, HOST_WIDE_INT work)
169*e4b17023SJohn Marino {
170*e4b17023SJohn Marino   int more, byte;
171*e4b17023SJohn Marino 
172*e4b17023SJohn Marino   do
173*e4b17023SJohn Marino     {
174*e4b17023SJohn Marino       byte = (work & 0x7f);
175*e4b17023SJohn Marino       /* arithmetic shift */
176*e4b17023SJohn Marino       work >>= 7;
177*e4b17023SJohn Marino       more = !((work == 0 && (byte & 0x40) == 0)
178*e4b17023SJohn Marino 	       || (work == -1 && (byte & 0x40) != 0));
179*e4b17023SJohn Marino       if (more)
180*e4b17023SJohn Marino 	byte |= 0x80;
181*e4b17023SJohn Marino 
182*e4b17023SJohn Marino       streamer_write_char_stream (obs, byte);
183*e4b17023SJohn Marino     }
184*e4b17023SJohn Marino   while (more);
185*e4b17023SJohn Marino }
186