2013-05-30 Ed Smith-Rowland <3dw4rd@verizon.net>
[official-gcc.git] / gcc / go / go-backend.c
blobea47138a597940ee26f96b053ad5776bf2cd3fe0
1 /* go-backend.c -- Go frontend interface to gcc backend.
2 Copyright (C) 2010-2013 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 "rtl.h"
26 #include "tree.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"
33 #include "go-c.h"
35 /* The segment name we pass to simple_object_start_read to find Go
36 export data. */
38 #ifndef GO_EXPORT_SEGMENT_NAME
39 #define GO_EXPORT_SEGMENT_NAME "__GNU_GO"
40 #endif
42 /* The section name we use when reading and writing export data. */
44 #ifndef GO_EXPORT_SECTION_NAME
45 #define GO_EXPORT_SECTION_NAME ".go_export"
46 #endif
48 /* This file holds all the cases where the Go frontend needs
49 information from gcc's backend. */
51 /* Return the alignment in bytes of a struct field of type T. */
53 unsigned int
54 go_field_alignment (tree t)
56 unsigned int v;
58 v = TYPE_ALIGN (t);
60 #ifdef BIGGEST_FIELD_ALIGNMENT
61 if (v > BIGGEST_FIELD_ALIGNMENT)
62 v = BIGGEST_FIELD_ALIGNMENT;
63 #endif
65 #ifdef ADJUST_FIELD_ALIGN
67 tree field ATTRIBUTE_UNUSED;
68 field = build_decl (UNKNOWN_LOCATION, FIELD_DECL, NULL, t);
69 v = ADJUST_FIELD_ALIGN (field, v);
71 #endif
73 return v / BITS_PER_UNIT;
76 /* Return the size and alignment of a trampoline. */
78 void
79 go_trampoline_info (unsigned int *size, unsigned int *alignment)
81 *size = TRAMPOLINE_SIZE;
82 *alignment = TRAMPOLINE_ALIGNMENT;
85 /* This is called by the Go frontend proper if the unsafe package was
86 imported. When that happens we can not do type-based alias
87 analysis. */
89 void
90 go_imported_unsafe (void)
92 flag_strict_aliasing = false;
94 /* This is a real hack. init_varasm_once has already grabbed an
95 alias set, which we don't want when we aren't doing strict
96 aliasing. We reinitialize to make it do it again. This should
97 be OK in practice since we haven't really done anything yet. */
98 init_varasm_once ();
100 /* Let the backend know that the options have changed. */
101 targetm.override_options_after_change ();
104 /* This is called by the Go frontend proper to add data to the
105 section containing Go export data. */
107 void
108 go_write_export_data (const char *bytes, unsigned int size)
110 static section* sec;
112 if (sec == NULL)
114 gcc_assert (targetm_common.have_named_sections);
115 sec = get_section (GO_EXPORT_SECTION_NAME, SECTION_DEBUG, NULL);
118 switch_to_section (sec);
119 assemble_string (bytes, size);
122 /* The go_read_export_data function is called by the Go frontend
123 proper to read Go export data from an object file. FD is a file
124 descriptor open for reading. OFFSET is the offset within the file
125 where the object file starts; this will be 0 except when reading an
126 archive. On success this returns NULL and sets *PBUF to a buffer
127 allocated using malloc, of size *PLEN, holding the export data. If
128 the data is not found, this returns NULL and sets *PBUF to NULL and
129 *PLEN to 0. If some error occurs, this returns an error message
130 and sets *PERR to an errno value or 0 if there is no relevant
131 errno. */
133 const char *
134 go_read_export_data (int fd, off_t offset, char **pbuf, size_t *plen,
135 int *perr)
137 simple_object_read *sobj;
138 const char *errmsg;
139 off_t sec_offset;
140 off_t sec_length;
141 int found;
142 char *buf;
143 ssize_t c;
145 *pbuf = NULL;
146 *plen = 0;
148 sobj = simple_object_start_read (fd, offset, GO_EXPORT_SEGMENT_NAME,
149 &errmsg, perr);
150 if (sobj == NULL)
152 /* If we get an error here, just pretend that we didn't find any
153 export data. This is the right thing to do if the error is
154 that the file was not recognized as an object file. This
155 will ignore file I/O errors, but it's not too big a deal
156 because we will wind up giving some other error later. */
157 return NULL;
160 found = simple_object_find_section (sobj, GO_EXPORT_SECTION_NAME,
161 &sec_offset, &sec_length,
162 &errmsg, perr);
163 simple_object_release_read (sobj);
164 if (!found)
165 return errmsg;
167 if (lseek (fd, offset + sec_offset, SEEK_SET) < 0)
169 *perr = errno;
170 return _("lseek failed while reading export data");
173 buf = XNEWVEC (char, sec_length);
174 if (buf == NULL)
176 *perr = errno;
177 return _("memory allocation failed while reading export data");
180 c = read (fd, buf, sec_length);
181 if (c < 0)
183 *perr = errno;
184 free (buf);
185 return _("read failed while reading export data");
188 if (c < sec_length)
190 free (buf);
191 return _("short read while reading export data");
194 *pbuf = buf;
195 *plen = sec_length;
197 return NULL;