OSDN Git Service

* config/m68hc11/m68hc11.c (m68hc11_initial_elimination_offset):
[pf3gnuchains/gcc-fork.git] / gcc / config / arm / pe.c
1 /* Routines for GCC for ARM/pe.
2    Copyright (C) 1995, 1996, 2000 Free Software Foundation, Inc.
3    Contributed by Doug Evans (dje@cygnus.com).
4
5 This file is part of GNU CC.
6
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "rtl.h"
25 #include "output.h"
26 #include "flags.h"
27 #include "tree.h"
28 #include "expr.h"
29 #include "toplev.h"
30 #include "tm_p.h"
31
32 extern int current_function_anonymous_args;
33
34 \f
35 /* Return non-zero if DECL is a dllexport'd object.  */
36
37 tree current_class_type; /* FIXME */
38
39 int
40 arm_dllexport_p (decl)
41      tree decl;
42 {
43   tree exp;
44
45   if (TREE_CODE (decl) != VAR_DECL
46       && TREE_CODE (decl) != FUNCTION_DECL)
47     return 0;
48   exp = lookup_attribute ("dllexport", DECL_MACHINE_ATTRIBUTES (decl));
49   if (exp)
50     return 1;
51
52   return 0;
53 }
54
55 /* Return non-zero if DECL is a dllimport'd object.  */
56
57 int
58 arm_dllimport_p (decl)
59      tree decl;
60 {
61   tree imp;
62
63   if (TREE_CODE (decl) == FUNCTION_DECL
64       && TARGET_NOP_FUN_DLLIMPORT)
65     return 0;
66
67   if (TREE_CODE (decl) != VAR_DECL
68       && TREE_CODE (decl) != FUNCTION_DECL)
69     return 0;
70   imp = lookup_attribute ("dllimport", DECL_MACHINE_ATTRIBUTES (decl));
71   if (imp)
72     return 1;
73
74   return 0;
75 }
76
77 /* Return non-zero if SYMBOL is marked as being dllexport'd.  */
78
79 int
80 arm_dllexport_name_p (symbol)
81      const char * symbol;
82 {
83   return symbol[0] == ARM_PE_FLAG_CHAR && symbol[1] == 'e' && symbol[2] == '.';
84 }
85
86 /* Return non-zero if SYMBOL is marked as being dllimport'd.  */
87
88 int
89 arm_dllimport_name_p (symbol)
90      const char * symbol;
91 {
92   return symbol[0] == ARM_PE_FLAG_CHAR && symbol[1] == 'i' && symbol[2] == '.';
93 }
94
95 /* Mark a DECL as being dllexport'd.
96    Note that we override the previous setting (eg: dllimport).  */
97
98 void
99 arm_mark_dllexport (decl)
100      tree decl;
101 {
102   const char * oldname;
103   char * newname;
104   rtx rtlname;
105   tree idp;
106
107   rtlname = XEXP (DECL_RTL (decl), 0);
108   if (GET_CODE (rtlname) == SYMBOL_REF)
109     oldname = XSTR (rtlname, 0);
110   else if (GET_CODE (rtlname) == MEM
111            && GET_CODE (XEXP (rtlname, 0)) == SYMBOL_REF)
112     oldname = XSTR (XEXP (rtlname, 0), 0);
113   else
114     abort ();
115   if (arm_dllimport_name_p (oldname))
116     oldname += 9;
117   else if (arm_dllexport_name_p (oldname))
118     return; /* already done */
119
120   newname = alloca (strlen (oldname) + 4);
121   sprintf (newname, "%ce.%s", ARM_PE_FLAG_CHAR, oldname);
122
123   /* We pass newname through get_identifier to ensure it has a unique
124      address.  RTL processing can sometimes peek inside the symbol ref
125      and compare the string's addresses to see if two symbols are
126      identical.  */
127   /* ??? At least I think that's why we do this.  */
128   idp = get_identifier (newname);
129
130   XEXP (DECL_RTL (decl), 0) =
131     gen_rtx (SYMBOL_REF, Pmode, IDENTIFIER_POINTER (idp));
132 }
133
134 /* Mark a DECL as being dllimport'd.  */
135
136 void
137 arm_mark_dllimport (decl)
138      tree decl;
139 {
140   const char * oldname;
141   char * newname;
142   tree idp;
143   rtx rtlname, newrtl;
144
145   rtlname = XEXP (DECL_RTL (decl), 0);
146   
147   if (GET_CODE (rtlname) == SYMBOL_REF)
148     oldname = XSTR (rtlname, 0);
149   else if (GET_CODE (rtlname) == MEM
150            && GET_CODE (XEXP (rtlname, 0)) == SYMBOL_REF)
151     oldname = XSTR (XEXP (rtlname, 0), 0);
152   else
153     abort ();
154   
155   if (arm_dllexport_name_p (oldname))
156     abort (); /* this shouldn't happen */
157   else if (arm_dllimport_name_p (oldname))
158     return; /* already done */
159
160   /* ??? One can well ask why we're making these checks here,
161      and that would be a good question.  */
162
163   /* Imported variables can't be initialized.  */
164   if (TREE_CODE (decl) == VAR_DECL
165       && !DECL_VIRTUAL_P (decl)
166       && DECL_INITIAL (decl))
167     {
168       error_with_decl (decl, "initialized variable `%s' is marked dllimport");
169       return;
170     }
171   /* Nor can they be static.  */
172   if (TREE_CODE (decl) == VAR_DECL
173       /* ??? Is this test for vtables needed?  */
174       && !DECL_VIRTUAL_P (decl)
175       && 0 /*???*/)
176     {
177       error_with_decl (decl, "static variable `%s' is marked dllimport");
178       return;
179     }
180
181   /* `extern' needn't be specified with dllimport.
182      Specify `extern' now and hope for the best.  Sigh.  */
183   if (TREE_CODE (decl) == VAR_DECL
184       /* ??? Is this test for vtables needed?  */
185       && !DECL_VIRTUAL_P (decl))
186     {
187       DECL_EXTERNAL (decl) = 1;
188       TREE_PUBLIC (decl) = 1;
189     }
190
191   newname = alloca (strlen (oldname) + 11);
192   sprintf (newname, "%ci.__imp_%s", ARM_PE_FLAG_CHAR, oldname);
193
194   /* We pass newname through get_identifier to ensure it has a unique
195      address.  RTL processing can sometimes peek inside the symbol ref
196      and compare the string's addresses to see if two symbols are
197      identical.  */
198   /* ??? At least I think that's why we do this.  */
199   idp = get_identifier (newname);
200
201   newrtl = gen_rtx (MEM, Pmode,
202                     gen_rtx (SYMBOL_REF, Pmode,
203                              IDENTIFIER_POINTER (idp)));
204   XEXP (DECL_RTL (decl), 0) = newrtl;
205 }
206
207 /* Cover function to implement ENCODE_SECTION_INFO.  */
208
209 void
210 arm_pe_encode_section_info (decl)
211      tree decl;
212 {
213   /* This bit is copied from arm.h.  */
214   if (optimize > 0 && TREE_CONSTANT (decl)
215       && (!flag_writable_strings || TREE_CODE (decl) != STRING_CST))
216     {
217       rtx rtl = (TREE_CODE_CLASS (TREE_CODE (decl)) != 'd'
218                  ? TREE_CST_RTL (decl) : DECL_RTL (decl));
219       SYMBOL_REF_FLAG (XEXP (rtl, 0)) = 1;
220     }
221
222   /* Mark the decl so we can tell from the rtl whether the object is
223      dllexport'd or dllimport'd.  */
224   if (arm_dllexport_p (decl))
225     arm_mark_dllexport (decl);
226   else if (arm_dllimport_p (decl))
227     arm_mark_dllimport (decl);
228   /* It might be that DECL has already been marked as dllimport, but a
229      subsequent definition nullified that.  The attribute is gone but
230      DECL_RTL still has @i.__imp_foo.  We need to remove that.  */
231   else if ((TREE_CODE (decl) == FUNCTION_DECL
232             || TREE_CODE (decl) == VAR_DECL)
233            && DECL_RTL (decl) != NULL_RTX
234            && GET_CODE (DECL_RTL (decl)) == MEM
235            && GET_CODE (XEXP (DECL_RTL (decl), 0)) == MEM
236            && GET_CODE (XEXP (XEXP (DECL_RTL (decl), 0), 0)) == SYMBOL_REF
237            && arm_dllimport_name_p (XSTR (XEXP (XEXP (DECL_RTL (decl), 0), 0), 0)))
238     {
239       const char *oldname = XSTR (XEXP (XEXP (DECL_RTL (decl), 0), 0), 0);
240       tree idp = get_identifier (oldname + 9);
241       rtx newrtl = gen_rtx (SYMBOL_REF, Pmode, IDENTIFIER_POINTER (idp));
242
243       XEXP (DECL_RTL (decl), 0) = newrtl;
244
245       /* We previously set TREE_PUBLIC and DECL_EXTERNAL.
246          ??? We leave these alone for now.  */
247     }
248 }
249
250 /* Cover function for UNIQUE_SECTION.  */
251
252 void
253 arm_pe_unique_section (decl, reloc)
254      tree decl;
255      int reloc;
256 {
257   int len;
258   const char * name;
259   char * string;
260   char * prefix;
261
262   name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
263   /* Strip off any encoding in fnname.  */
264   STRIP_NAME_ENCODING (name, name);
265
266   /* The object is put in, for example, section .text$foo.
267      The linker will then ultimately place them in .text
268      (everything from the $ on is stripped).  */
269   if (TREE_CODE (decl) == FUNCTION_DECL)
270     prefix = ".text$";
271   else if (DECL_READONLY_SECTION (decl, reloc))
272     prefix = ".rdata$";
273   else
274     prefix = ".data$";
275   len = strlen (name) + strlen (prefix);
276   string = alloca (len + 1);
277   sprintf (string, "%s%s", prefix, name);
278
279   DECL_SECTION_NAME (decl) = build_string (len, string);
280 }