re PR tree-optimization/79408 (Missed VRP optimization of integer modulo)
[official-gcc.git] / libcc1 / marshall.cc
blobcf53cf7c4f2da4deb7c20c0eb69f0af43cd052e6
1 /* Marshalling and unmarshalling.
2 Copyright (C) 2014-2017 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include <cc1plugin-config.h>
21 #include <new>
22 #include <string.h>
23 #include "marshall.hh"
24 #include "connection.hh"
26 cc1_plugin::status
27 cc1_plugin::unmarshall_check (connection *conn, unsigned long long check)
29 unsigned long long r;
31 if (!unmarshall (conn, &r))
32 return FAIL;
33 return check == r ? OK : FAIL;
36 cc1_plugin::status
37 cc1_plugin::marshall_intlike (connection *conn, unsigned long long val)
39 if (!conn->send ('i'))
40 return FAIL;
41 return conn->send (&val, sizeof (val));
44 cc1_plugin::status
45 cc1_plugin::unmarshall_intlike (connection *conn, unsigned long long *result)
47 if (!conn->require ('i'))
48 return FAIL;
49 return conn->get (result, sizeof (*result));
52 cc1_plugin::status
53 cc1_plugin::marshall (connection *conn, const char *str)
55 if (!conn->send ('s'))
56 return FAIL;
58 unsigned long long len = str == NULL ? -1ULL : strlen (str);
59 if (!conn->send (&len, sizeof (len)))
60 return FAIL;
62 if (str == NULL)
63 return OK;
65 return conn->send (str, len);
68 cc1_plugin::status
69 cc1_plugin::unmarshall (connection *conn, char **result)
71 unsigned long long len;
73 if (!conn->require ('s'))
74 return FAIL;
75 if (!conn->get (&len, sizeof (len)))
76 return FAIL;
78 if (len == -1ULL)
80 *result = NULL;
81 return OK;
84 char *str = new (std::nothrow) char[len + 1];
85 if (str == NULL)
86 return FAIL;
88 if (!conn->get (str, len))
90 delete[] str;
91 return FAIL;
94 str[len] = '\0';
95 *result = str;
97 return OK;
100 cc1_plugin::status
101 cc1_plugin::marshall_array_start (connection *conn, char id,
102 size_t n_elements)
104 if (!conn->send (id))
105 return FAIL;
107 unsigned long long r = n_elements;
108 if (!conn->send (&r, sizeof (r)))
109 return FAIL;
111 return OK;
114 cc1_plugin::status
115 cc1_plugin::marshall_array_elmts (connection *conn, size_t n_bytes,
116 void *elements)
118 return conn->send (elements, n_bytes);
121 cc1_plugin::status
122 cc1_plugin::unmarshall_array_start (connection *conn, char id,
123 size_t *n_elements)
125 unsigned long long len;
127 if (!conn->require (id))
128 return FAIL;
129 if (!conn->get (&len, sizeof (len)))
130 return FAIL;
132 *n_elements = len;
134 return OK;
137 cc1_plugin::status
138 cc1_plugin::unmarshall_array_elmts (connection *conn, size_t n_bytes,
139 void *elements)
141 return conn->get (elements, n_bytes);
144 cc1_plugin::status
145 cc1_plugin::marshall (connection *conn, const gcc_type_array *a)
147 size_t len;
149 if (a)
150 len = a->n_elements;
151 else
152 len = (size_t)-1;
154 if (!marshall_array_start (conn, 'a', len))
155 return FAIL;
157 if (!a)
158 return OK;
160 return marshall_array_elmts (conn, len * sizeof (a->elements[0]),
161 a->elements);
164 cc1_plugin::status
165 cc1_plugin::unmarshall (connection *conn, gcc_type_array **result)
167 size_t len;
169 if (!unmarshall_array_start (conn, 'a', &len))
170 return FAIL;
172 if (len == (size_t)-1)
174 *result = NULL;
175 return OK;
178 gcc_type_array *gta = new gcc_type_array;
180 gta->n_elements = len;
181 gta->elements = new gcc_type[len];
183 if (!unmarshall_array_elmts (conn,
184 len * sizeof (gta->elements[0]),
185 gta->elements))
187 delete[] gta->elements;
188 delete *result;
189 return FAIL;
192 *result = gta;
194 return OK;