2014-10-24 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / go / go-backend.c
blob3f1a9f93b7136c00170167dfae6ce1a6864ed388
1 /* go-backend.c -- Go frontend interface to gcc backend.
2 Copyright (C) 2010-2014 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 "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "simple-object.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "stor-layout.h"
27 #include "tm_p.h"
28 #include "intl.h"
29 #include "output.h" /* for assemble_string */
30 #include "target.h"
31 #include "common/common-target.h"
32 #include "diagnostic.h"
34 #include "go-c.h"
36 /* The segment name we pass to simple_object_start_read to find Go
37 export data. */
39 #ifndef GO_EXPORT_SEGMENT_NAME
40 #define GO_EXPORT_SEGMENT_NAME "__GNU_GO"
41 #endif
43 /* The section name we use when reading and writing export data. */
45 #ifndef GO_EXPORT_SECTION_NAME
46 #define GO_EXPORT_SECTION_NAME ".go_export"
47 #endif
49 /* This file holds all the cases where the Go frontend needs
50 information from gcc's backend. */
52 /* Return whether or not GCC has reported any errors. */
54 bool
55 saw_errors (void)
57 return errorcount != 0 || sorrycount != 0;
60 /* Return the alignment in bytes of a struct field of type T. */
62 unsigned int
63 go_field_alignment (tree t)
65 unsigned int v;
67 v = TYPE_ALIGN (t);
69 #ifdef BIGGEST_FIELD_ALIGNMENT
70 if (v > BIGGEST_FIELD_ALIGNMENT)
71 v = BIGGEST_FIELD_ALIGNMENT;
72 #endif
74 #ifdef ADJUST_FIELD_ALIGN
76 tree field ATTRIBUTE_UNUSED;
77 field = build_decl (UNKNOWN_LOCATION, FIELD_DECL, NULL, t);
78 v = ADJUST_FIELD_ALIGN (field, v);
80 #endif
82 return v / BITS_PER_UNIT;
85 /* Return the size and alignment of a trampoline. */
87 void
88 go_trampoline_info (unsigned int *size, unsigned int *alignment)
90 *size = TRAMPOLINE_SIZE;
91 *alignment = TRAMPOLINE_ALIGNMENT;
94 /* This is called by the Go frontend proper if the unsafe package was
95 imported. When that happens we can not do type-based alias
96 analysis. */
98 void
99 go_imported_unsafe (void)
101 flag_strict_aliasing = false;
103 /* Let the backend know that the options have changed. */
104 targetm.override_options_after_change ();
107 /* This is called by the Go frontend proper to add data to the
108 section containing Go export data. */
110 void
111 go_write_export_data (const char *bytes, unsigned int size)
113 static section* sec;
115 if (sec == NULL)
117 gcc_assert (targetm_common.have_named_sections);
118 sec = get_section (GO_EXPORT_SECTION_NAME, SECTION_DEBUG, NULL);
121 switch_to_section (sec);
122 assemble_string (bytes, size);
125 /* The go_read_export_data function is called by the Go frontend
126 proper to read Go export data from an object file. FD is a file
127 descriptor open for reading. OFFSET is the offset within the file
128 where the object file starts; this will be 0 except when reading an
129 archive. On success this returns NULL and sets *PBUF to a buffer
130 allocated using malloc, of size *PLEN, holding the export data. If
131 the data is not found, this returns NULL and sets *PBUF to NULL and
132 *PLEN to 0. If some error occurs, this returns an error message
133 and sets *PERR to an errno value or 0 if there is no relevant
134 errno. */
136 const char *
137 go_read_export_data (int fd, off_t offset, char **pbuf, size_t *plen,
138 int *perr)
140 simple_object_read *sobj;
141 const char *errmsg;
142 off_t sec_offset;
143 off_t sec_length;
144 int found;
145 char *buf;
146 ssize_t c;
148 *pbuf = NULL;
149 *plen = 0;
151 sobj = simple_object_start_read (fd, offset, GO_EXPORT_SEGMENT_NAME,
152 &errmsg, perr);
153 if (sobj == NULL)
155 /* If we get an error here, just pretend that we didn't find any
156 export data. This is the right thing to do if the error is
157 that the file was not recognized as an object file. This
158 will ignore file I/O errors, but it's not too big a deal
159 because we will wind up giving some other error later. */
160 return NULL;
163 found = simple_object_find_section (sobj, GO_EXPORT_SECTION_NAME,
164 &sec_offset, &sec_length,
165 &errmsg, perr);
166 simple_object_release_read (sobj);
167 if (!found)
168 return errmsg;
170 if (lseek (fd, offset + sec_offset, SEEK_SET) < 0)
172 *perr = errno;
173 return _("lseek failed while reading export data");
176 buf = XNEWVEC (char, sec_length);
177 if (buf == NULL)
179 *perr = errno;
180 return _("memory allocation failed while reading export data");
183 c = read (fd, buf, sec_length);
184 if (c < 0)
186 *perr = errno;
187 free (buf);
188 return _("read failed while reading export data");
191 if (c < sec_length)
193 free (buf);
194 return _("short read while reading export data");
197 *pbuf = buf;
198 *plen = sec_length;
200 return NULL;