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