OSDN Git Service

4d1ea827c5e2fe3a9c66adbc8c28e9366b6d37a5
[pf3gnuchains/gcc-fork.git] / gcc / go / go-backend.c
1 /* go-backend.c -- Go frontend interface to gcc backend.
2    Copyright (C) 2010, 2011 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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.
10
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.
15
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/>.  */
19
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"
30 #include "target.h"
31 #include "common/common-target.h"
32
33 #include "go-c.h"
34
35 /* The segment name we pass to simple_object_start_read to find Go
36    export data.  */
37
38 #ifndef GO_EXPORT_SEGMENT_NAME
39 #define GO_EXPORT_SEGMENT_NAME "__GNU_GO"
40 #endif
41
42 /* The section name we use when reading and writing export data.  */
43
44 #ifndef GO_EXPORT_SECTION_NAME
45 #define GO_EXPORT_SECTION_NAME ".go_export"
46 #endif
47
48 /* This file holds all the cases where the Go frontend needs
49    information from gcc's backend.  */
50
51 /* Return the alignment in bytes of a value of type T.  */
52
53 unsigned int
54 go_type_alignment (tree t)
55 {
56   return TYPE_ALIGN_UNIT (t);
57 }
58
59 /* Return the alignment in bytes of a struct field of type T.  */
60
61 unsigned int
62 go_field_alignment (tree t)
63 {
64   unsigned int v;
65
66   v = TYPE_ALIGN (t);
67
68 #ifdef BIGGEST_FIELD_ALIGNMENT
69   if (v > BIGGEST_FIELD_ALIGNMENT)
70     v = BIGGEST_FIELD_ALIGNMENT;
71 #endif
72
73 #ifdef ADJUST_FIELD_ALIGN
74   {
75     tree field ATTRIBUTE_UNUSED;
76     field = build_decl (UNKNOWN_LOCATION, FIELD_DECL, NULL, t);
77     v = ADJUST_FIELD_ALIGN (field, v);
78   }
79 #endif
80
81   return v / BITS_PER_UNIT;
82 }
83
84 /* Return the size and alignment of a trampoline.  */
85
86 void
87 go_trampoline_info (unsigned int *size, unsigned int *alignment)
88 {
89   *size = TRAMPOLINE_SIZE;
90   *alignment = TRAMPOLINE_ALIGNMENT;
91 }
92
93 /* This is called by the Go frontend proper if the unsafe package was
94    imported.  When that happens we can not do type-based alias
95    analysis.  */
96
97 void
98 go_imported_unsafe (void)
99 {
100   flag_strict_aliasing = false;
101
102   /* This is a real hack.  init_varasm_once has already grabbed an
103      alias set, which we don't want when we aren't doing strict
104      aliasing.  We reinitialize to make it do it again.  This should
105      be OK in practice since we haven't really done anything yet.  */
106   init_varasm_once ();
107
108   /* Let the backend know that the options have changed.  */
109   targetm.override_options_after_change ();
110 }
111
112 /* This is called by the Go frontend proper to add data to the
113    section containing Go export data.  */
114
115 void
116 go_write_export_data (const char *bytes, unsigned int size)
117 {
118   static section* sec;
119
120   if (sec == NULL)
121     {
122       gcc_assert (targetm_common.have_named_sections);
123       sec = get_section (GO_EXPORT_SECTION_NAME, SECTION_DEBUG, NULL);
124     }
125
126   switch_to_section (sec);
127   assemble_string (bytes, size);
128 }
129
130 /* The go_read_export_data function is called by the Go frontend
131    proper to read Go export data from an object file.  FD is a file
132    descriptor open for reading.  OFFSET is the offset within the file
133    where the object file starts; this will be 0 except when reading an
134    archive.  On success this returns NULL and sets *PBUF to a buffer
135    allocated using malloc, of size *PLEN, holding the export data.  If
136    the data is not found, this returns NULL and sets *PBUF to NULL and
137    *PLEN to 0.  If some error occurs, this returns an error message
138    and sets *PERR to an errno value or 0 if there is no relevant
139    errno.  */
140
141 const char *
142 go_read_export_data (int fd, off_t offset, char **pbuf, size_t *plen,
143                      int *perr)
144 {
145   simple_object_read *sobj;
146   const char *errmsg;
147   off_t sec_offset;
148   off_t sec_length;
149   int found;
150   char *buf;
151   ssize_t c;
152
153   *pbuf = NULL;
154   *plen = 0;
155
156   sobj = simple_object_start_read (fd, offset, GO_EXPORT_SEGMENT_NAME,
157                                    &errmsg, perr);
158   if (sobj == NULL)
159     {
160       /* If we get an error here, just pretend that we didn't find any
161          export data.  This is the right thing to do if the error is
162          that the file was not recognized as an object file.  This
163          will ignore file I/O errors, but it's not too big a deal
164          because we will wind up giving some other error later.  */
165       return NULL;
166     }
167
168   found = simple_object_find_section (sobj, GO_EXPORT_SECTION_NAME,
169                                       &sec_offset, &sec_length,
170                                       &errmsg, perr);
171   simple_object_release_read (sobj);
172   if (!found)
173     return errmsg;
174
175   if (lseek (fd, offset + sec_offset, SEEK_SET) < 0)
176     {
177       *perr = errno;
178       return _("lseek failed while reading export data");
179     }
180
181   buf = XNEWVEC (char, sec_length);
182   if (buf == NULL)
183     {
184       *perr = errno;
185       return _("memory allocation failed while reading export data");
186     }
187
188   c = read (fd, buf, sec_length);
189   if (c < 0)
190     {
191       *perr = errno;
192       free (buf);
193       return _("read failed while reading export data");
194     }
195
196   if (c < sec_length)
197     {
198       free (buf);
199       return _("short read while reading export data");
200     }
201
202   *pbuf = buf;
203   *plen = sec_length;
204
205   return NULL;
206 }