OSDN Git Service

Add this missing ChangeLog entry:
[pf3gnuchains/gcc-fork.git] / gcc / config / i386 / winnt.c
1 /* Subroutines for insn-output.c for Windows NT.
2    Contributed by Douglas Rupp (drupp@cs.washington.edu)
3    Copyright (C) 1995, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4    2005, 2006, 2007 Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "rtl.h"
27 #include "regs.h"
28 #include "hard-reg-set.h"
29 #include "output.h"
30 #include "tree.h"
31 #include "flags.h"
32 #include "tm_p.h"
33 #include "toplev.h"
34 #include "hashtab.h"
35 #include "ggc.h"
36 #include "target.h"
37
38 /* i386/PE specific attribute support.
39
40    i386/PE has two new attributes:
41    dllexport - for exporting a function/variable that will live in a dll
42    dllimport - for importing a function/variable from a dll
43
44    Microsoft allows multiple declspecs in one __declspec, separating
45    them with spaces.  We do NOT support this.  Instead, use __declspec
46    multiple times.
47 */
48
49 /* Handle a "shared" attribute;
50    arguments as in struct attribute_spec.handler.  */
51 tree
52 ix86_handle_shared_attribute (tree *node, tree name,
53                               tree args ATTRIBUTE_UNUSED,
54                               int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
55 {
56   if (TREE_CODE (*node) != VAR_DECL)
57     {
58       warning (OPT_Wattributes, "%qs attribute only applies to variables",
59                IDENTIFIER_POINTER (name));
60       *no_add_attrs = true;
61     }
62
63   return NULL_TREE;
64 }
65
66 /* Handle a "selectany" attribute;
67    arguments as in struct attribute_spec.handler.  */
68 tree
69 ix86_handle_selectany_attribute (tree *node, tree name,
70                                  tree args ATTRIBUTE_UNUSED,
71                                  int flags ATTRIBUTE_UNUSED,
72                                  bool *no_add_attrs)
73 {
74   /* The attribute applies only to objects that are initialized and have
75      external linkage.  However, we may not know about initialization
76      until the language frontend has processed the decl. We'll check for
77      initialization later in encode_section_info.  */
78   if (TREE_CODE (*node) != VAR_DECL || !TREE_PUBLIC (*node))
79     {   
80       error ("%qs attribute applies only to initialized variables"
81              " with external linkage",  IDENTIFIER_POINTER (name));
82       *no_add_attrs = true;
83     }
84
85   return NULL_TREE;
86 }
87
88 \f
89 /* Return the type that we should use to determine if DECL is
90    imported or exported.  */
91
92 static tree
93 associated_type (tree decl)
94 {
95   return (DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
96           ?  DECL_CONTEXT (decl) : NULL_TREE);
97 }
98
99 /* Return true if DECL should be a dllexport'd object.  */
100
101 static bool
102 i386_pe_determine_dllexport_p (tree decl)
103 {
104   tree assoc;
105
106   if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != FUNCTION_DECL)
107     return false;
108
109   if (lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
110     return true;
111
112   /* Also mark class members of exported classes with dllexport.  */
113   assoc = associated_type (decl);
114   if (assoc && lookup_attribute ("dllexport", TYPE_ATTRIBUTES (assoc)))
115     return i386_pe_type_dllexport_p (decl);
116
117   return false;
118 }
119
120 /* Return true if DECL should be a dllimport'd object.  */
121
122 static bool
123 i386_pe_determine_dllimport_p (tree decl)
124 {
125   tree assoc;
126
127   if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != FUNCTION_DECL)
128     return false;
129
130   /* Lookup the attribute in addition to checking the DECL_DLLIMPORT_P flag.
131      We may need to override an earlier decision.  */
132   if (DECL_DLLIMPORT_P (decl))
133     return true;
134
135   /* The DECL_DLLIMPORT_P flag was set for decls in the class definition
136      by  targetm.cxx.adjust_class_at_definition.  Check again to emit
137      warnings if the class attribute has been overridden by an
138      out-of-class definition.  */
139   assoc = associated_type (decl);
140   if (assoc && lookup_attribute ("dllimport", TYPE_ATTRIBUTES (assoc)))
141     return i386_pe_type_dllimport_p (decl);
142
143   return false;
144 }
145
146 /* Handle the -mno-fun-dllimport target switch.  */
147
148 bool
149 i386_pe_valid_dllimport_attribute_p (const_tree decl)
150 {
151    if (TARGET_NOP_FUN_DLLIMPORT && TREE_CODE (decl) == FUNCTION_DECL)
152      return false;
153    return true;
154 }
155
156 /* Return string which is the function name, identified by ID, modified
157    with a suffix consisting of an atsign (@) followed by the number of
158    bytes of arguments.  If ID is NULL use the DECL_NAME as base. If
159    FASTCALL is true, also add the FASTCALL_PREFIX.
160    Return NULL if no change required.  */
161
162 static tree
163 gen_stdcall_or_fastcall_suffix (tree decl, tree id, bool fastcall)
164 {
165   HOST_WIDE_INT total = 0;
166   const char *old_str = IDENTIFIER_POINTER (id != NULL_TREE ? id : DECL_NAME (decl));
167   char *new_str, *p;
168   tree type = TREE_TYPE (decl);
169   tree arg;
170   function_args_iterator args_iter;
171
172   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);  
173
174   if (prototype_p (type))
175     {
176       /* This attribute is ignored for variadic functions.  */ 
177       if (stdarg_p (type))
178         return NULL_TREE;
179
180       /* Quit if we hit an incomplete type.  Error is reported
181          by convert_arguments in c-typeck.c or cp/typeck.c.  */
182       FOREACH_FUNCTION_ARGS(type, arg, args_iter)
183         {
184           HOST_WIDE_INT parm_size;
185           HOST_WIDE_INT parm_boundary_bytes = PARM_BOUNDARY / BITS_PER_UNIT;
186
187           if (! COMPLETE_TYPE_P (arg))
188             break;
189
190           parm_size = int_size_in_bytes (arg);
191           if (parm_size < 0)
192             break;
193
194           /* Must round up to include padding.  This is done the same
195              way as in store_one_arg.  */
196           parm_size = ((parm_size + parm_boundary_bytes - 1)
197                        / parm_boundary_bytes * parm_boundary_bytes);
198           total += parm_size;
199         }
200       }
201   /* Assume max of 8 base 10 digits in the suffix.  */
202   p = new_str = XALLOCAVEC (char, 1 + strlen (old_str) + 1 + 8 + 1);
203   if (fastcall)
204     *p++ = FASTCALL_PREFIX;
205   sprintf (p, "%s@" HOST_WIDE_INT_PRINT_DEC, old_str, total);
206
207   return get_identifier (new_str);
208 }
209
210 /* Maybe decorate and get a new identifier for the DECL of a stdcall or
211    fastcall function. The original identifier is supplied in ID. */
212
213 static tree
214 i386_pe_maybe_mangle_decl_assembler_name (tree decl, tree id)
215 {
216   tree new_id = NULL_TREE;
217
218   if (TREE_CODE (decl) == FUNCTION_DECL)
219     { 
220       tree type_attributes = TYPE_ATTRIBUTES (TREE_TYPE (decl));
221       if (lookup_attribute ("stdcall", type_attributes))
222         new_id = gen_stdcall_or_fastcall_suffix (decl, id, false);
223       else if (lookup_attribute ("fastcall", type_attributes))
224         new_id = gen_stdcall_or_fastcall_suffix (decl, id, true);
225     }
226
227   return new_id;
228 }
229
230 /* This is used as a target hook to modify the DECL_ASSEMBLER_NAME
231    in the language-independent default hook
232    langhooks,c:lhd_set_decl_assembler_name ()
233    and in cp/mangle,c:mangle_decl ().  */
234 tree
235 i386_pe_mangle_decl_assembler_name (tree decl, tree id)
236 {
237   tree new_id = i386_pe_maybe_mangle_decl_assembler_name (decl, id);   
238
239   return (new_id ? new_id : id);
240 }
241
242 void
243 i386_pe_encode_section_info (tree decl, rtx rtl, int first)
244 {
245   rtx symbol;
246   int flags;
247
248   /* Do this last, due to our frobbing of DECL_DLLIMPORT_P above.  */
249   default_encode_section_info (decl, rtl, first);
250
251   /* Careful not to prod global register variables.  */
252   if (!MEM_P (rtl))
253     return;
254
255   symbol = XEXP (rtl, 0);
256   gcc_assert (GET_CODE (symbol) == SYMBOL_REF);
257
258   switch (TREE_CODE (decl))
259     {
260     case FUNCTION_DECL:
261       if (first)
262         {
263           /* FIXME: In Ada, and perhaps other language frontends,
264              imported stdcall names may not yet have been modified.
265              Check and do it know.  */
266          tree new_id;
267          tree old_id = DECL_ASSEMBLER_NAME (decl);
268           const char* asm_str = IDENTIFIER_POINTER (old_id);
269           /* Do not change the identifier if a verbatim asmspec
270              or if stdcall suffix already added. */
271           if (*asm_str == '*' || strchr (asm_str, '@'))
272             break;
273           if ((new_id = i386_pe_maybe_mangle_decl_assembler_name (decl, old_id)))
274             {
275               /* These attributes must be present on first declaration,
276                  change_decl_assembler_name will warn if they are added
277                  later and the decl has been referenced, but duplicate_decls
278                  should catch the mismatch first.  */
279               change_decl_assembler_name (decl, new_id);
280               XSTR (symbol, 0) = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
281             }
282         }
283       break;
284
285     case VAR_DECL:
286       if (lookup_attribute ("selectany", DECL_ATTRIBUTES (decl)))
287         {
288           if (DECL_INITIAL (decl)
289               /* If an object is initialized with a ctor, the static
290                  initialization and destruction code for it is present in
291                  each unit defining the object.  The code that calls the
292                  ctor is protected by a link-once guard variable, so that
293                  the object still has link-once semantics,  */
294               || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
295             make_decl_one_only (decl);
296           else
297             error ("%q+D:'selectany' attribute applies only to "
298                    "initialized objects", decl);
299         }
300       break;
301
302     default:
303       return;
304     }
305
306   /* Mark the decl so we can tell from the rtl whether the object is
307      dllexport'd or dllimport'd.  tree.c: merge_dllimport_decl_attributes
308      handles dllexport/dllimport override semantics.  */
309   flags = (SYMBOL_REF_FLAGS (symbol) &
310            ~(SYMBOL_FLAG_DLLIMPORT | SYMBOL_FLAG_DLLEXPORT));
311   if (i386_pe_determine_dllexport_p (decl))
312     flags |= SYMBOL_FLAG_DLLEXPORT;
313   else if (i386_pe_determine_dllimport_p (decl))
314     {
315       flags |= SYMBOL_FLAG_DLLIMPORT;
316       /* If we went through the associated_type path, this won't already
317          be set.  Though, frankly, this seems wrong, and should be fixed
318          elsewhere.  */
319       if (!DECL_DLLIMPORT_P (decl))
320         {
321           DECL_DLLIMPORT_P (decl) = 1;
322           flags &= ~SYMBOL_FLAG_LOCAL;
323         }
324     }
325   SYMBOL_REF_FLAGS (symbol) = flags;
326 }
327
328 bool
329 i386_pe_binds_local_p (const_tree exp)
330 {
331   /* PE does not do dynamic binding.  Indeed, the only kind of
332      non-local reference comes from a dllimport'd symbol.  */
333   if ((TREE_CODE (exp) == VAR_DECL || TREE_CODE (exp) == FUNCTION_DECL)
334       && DECL_DLLIMPORT_P (exp))
335     return false;
336
337   return true;
338 }
339
340 /* Also strip the fastcall prefix and stdcall suffix.  */
341
342 const char *
343 i386_pe_strip_name_encoding_full (const char *str)
344 {
345   const char *p;
346   const char *name = default_strip_name_encoding (str);
347
348   /* Strip leading '@' on fastcall symbols.  */
349   if (*name == '@')
350     name++;
351
352   /* Strip trailing "@n".  */
353   p = strchr (name, '@');
354   if (p)
355     return ggc_alloc_string (name, p - name);
356
357   return name;
358 }
359
360 void
361 i386_pe_unique_section (tree decl, int reloc)
362 {
363   int len;
364   const char *name, *prefix;
365   char *string;
366
367   name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
368   name = i386_pe_strip_name_encoding_full (name);
369
370   /* The object is put in, for example, section .text$foo.
371      The linker will then ultimately place them in .text
372      (everything from the $ on is stripped). Don't put
373      read-only data in .rdata section to avoid a PE linker
374      bug when .rdata$* grouped sections are used in code
375      without a .rdata section.  */
376   if (TREE_CODE (decl) == FUNCTION_DECL)
377     prefix = ".text$";
378   else if (decl_readonly_section (decl, reloc))
379     prefix = ".rdata$";
380   else
381     prefix = ".data$";
382   len = strlen (name) + strlen (prefix);
383   string = XALLOCAVEC (char, len + 1);
384   sprintf (string, "%s%s", prefix, name);
385
386   DECL_SECTION_NAME (decl) = build_string (len, string);
387 }
388
389 /* Select a set of attributes for section NAME based on the properties
390    of DECL and whether or not RELOC indicates that DECL's initializer
391    might contain runtime relocations.
392
393    We make the section read-only and executable for a function decl,
394    read-only for a const data decl, and writable for a non-const data decl.
395
396    If the section has already been defined, to not allow it to have
397    different attributes, as (1) this is ambiguous since we're not seeing
398    all the declarations up front and (2) some assemblers (e.g. SVR4)
399    do not recognize section redefinitions.  */
400 /* ??? This differs from the "standard" PE implementation in that we
401    handle the SHARED variable attribute.  Should this be done for all
402    PE targets?  */
403
404 #define SECTION_PE_SHARED       SECTION_MACH_DEP
405
406 unsigned int
407 i386_pe_section_type_flags (tree decl, const char *name, int reloc)
408 {
409   static htab_t htab;
410   unsigned int flags;
411   unsigned int **slot;
412
413   /* The names we put in the hashtable will always be the unique
414      versions given to us by the stringtable, so we can just use
415      their addresses as the keys.  */
416   if (!htab)
417     htab = htab_create (31, htab_hash_pointer, htab_eq_pointer, NULL);
418
419   if (decl && TREE_CODE (decl) == FUNCTION_DECL)
420     flags = SECTION_CODE;
421   else if (decl && decl_readonly_section (decl, reloc))
422     flags = 0;
423   else if (current_function_decl
424            && cfun
425            && crtl->subsections.unlikely_text_section_name
426            && strcmp (name, crtl->subsections.unlikely_text_section_name) == 0)
427     flags = SECTION_CODE;
428   else if (!decl
429            && (!current_function_decl || !cfun)
430            && strcmp (name, UNLIKELY_EXECUTED_TEXT_SECTION_NAME) == 0)
431     flags = SECTION_CODE;
432   else
433     {
434       flags = SECTION_WRITE;
435
436       if (decl && TREE_CODE (decl) == VAR_DECL
437           && lookup_attribute ("shared", DECL_ATTRIBUTES (decl)))
438         flags |= SECTION_PE_SHARED;
439     }
440
441   if (decl && DECL_ONE_ONLY (decl))
442     flags |= SECTION_LINKONCE;
443
444   /* See if we already have an entry for this section.  */
445   slot = (unsigned int **) htab_find_slot (htab, name, INSERT);
446   if (!*slot)
447     {
448       *slot = (unsigned int *) xmalloc (sizeof (unsigned int));
449       **slot = flags;
450     }
451   else
452     {
453       if (decl && **slot != flags)
454         error ("%q+D causes a section type conflict", decl);
455     }
456
457   return flags;
458 }
459
460 void
461 i386_pe_asm_named_section (const char *name, unsigned int flags, 
462                            tree decl)
463 {
464   char flagchars[8], *f = flagchars;
465
466   if ((flags & (SECTION_CODE | SECTION_WRITE)) == 0)
467     /* readonly data */
468     {
469       *f++ ='d';  /* This is necessary for older versions of gas.  */
470       *f++ ='r';
471     }
472   else  
473     {
474       if (flags & SECTION_CODE)
475         *f++ = 'x';
476       if (flags & SECTION_WRITE)
477         *f++ = 'w';
478       if (flags & SECTION_PE_SHARED)
479         *f++ = 's';
480     }
481
482   *f = '\0';
483
484   fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
485
486   if (flags & SECTION_LINKONCE)
487     {
488       /* Functions may have been compiled at various levels of
489          optimization so we can't use `same_size' here.
490          Instead, have the linker pick one, without warning.
491          If 'selectany' attribute has been specified,  MS compiler
492          sets 'discard' characteristic, rather than telling linker
493          to warn of size or content mismatch, so do the same.  */ 
494       bool discard = (flags & SECTION_CODE)
495                       || lookup_attribute ("selectany",
496                                            DECL_ATTRIBUTES (decl));      
497       fprintf (asm_out_file, "\t.linkonce %s\n",
498                (discard  ? "discard" : "same_size"));
499     }
500 }
501
502 void
503 i386_pe_asm_output_aligned_decl_common (FILE *stream, tree decl,
504                                         const char *name, HOST_WIDE_INT size,
505                                         HOST_WIDE_INT align ATTRIBUTE_UNUSED)
506 {
507   HOST_WIDE_INT rounded;
508
509   /* Compute as in assemble_noswitch_variable, since we don't actually
510      support aligned common.  */
511   rounded = size ? size : 1;
512   rounded += (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1;
513   rounded = (rounded / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
514              * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
515   
516   i386_pe_maybe_record_exported_symbol (decl, name, 1);
517
518   switch_to_section (bss_section);
519   fprintf (stream, "\t.balign %d\n\t.comm \t", ((int) align) / BITS_PER_UNIT);
520   assemble_name (stream, name);
521   fprintf (stream, ", " HOST_WIDE_INT_PRINT_DEC "\t" ASM_COMMENT_START
522            " " HOST_WIDE_INT_PRINT_DEC "\n",
523            rounded, size);
524 }
525 \f
526 /* The Microsoft linker requires that every function be marked as
527    DT_FCN.  When using gas on cygwin, we must emit appropriate .type
528    directives.  */
529
530 #include "gsyms.h"
531
532 /* Mark a function appropriately.  This should only be called for
533    functions for which we are not emitting COFF debugging information.
534    FILE is the assembler output file, NAME is the name of the
535    function, and PUB is nonzero if the function is globally
536    visible.  */
537
538 void
539 i386_pe_declare_function_type (FILE *file, const char *name, int pub)
540 {
541   fprintf (file, "\t.def\t");
542   assemble_name (file, name);
543   fprintf (file, ";\t.scl\t%d;\t.type\t%d;\t.endef\n",
544            pub ? (int) C_EXT : (int) C_STAT,
545            (int) DT_FCN << N_BTSHFT);
546 }
547
548 /* Keep a list of external functions.  */
549
550 struct extern_list GTY(())
551 {
552   struct extern_list *next;
553   tree decl;
554   const char *name;
555 };
556
557 static GTY(()) struct extern_list *extern_head;
558
559 /* Assemble an external function reference.  We need to keep a list of
560    these, so that we can output the function types at the end of the
561    assembly.  We can't output the types now, because we might see a
562    definition of the function later on and emit debugging information
563    for it then.  */
564
565 void
566 i386_pe_record_external_function (tree decl, const char *name)
567 {
568   struct extern_list *p;
569
570   p = (struct extern_list *) ggc_alloc (sizeof *p);
571   p->next = extern_head;
572   p->decl = decl;
573   p->name = name;
574   extern_head = p;
575 }
576
577 /* Keep a list of exported symbols.  */
578
579 struct export_list GTY(())
580 {
581   struct export_list *next;
582   const char *name;
583   int is_data;          /* used to type tag exported symbols.  */
584 };
585
586 static GTY(()) struct export_list *export_head;
587
588 /* Assemble an export symbol entry.  We need to keep a list of
589    these, so that we can output the export list at the end of the
590    assembly.  We used to output these export symbols in each function,
591    but that causes problems with GNU ld when the sections are
592    linkonce.  */
593
594 void
595 i386_pe_maybe_record_exported_symbol (tree decl, const char *name, int is_data)
596 {
597   rtx symbol;
598   struct export_list *p;
599
600   symbol = XEXP (DECL_RTL (decl), 0);
601   gcc_assert (GET_CODE (symbol) == SYMBOL_REF);
602   if (!SYMBOL_REF_DLLEXPORT_P (symbol))
603     return;
604
605   p = (struct export_list *) ggc_alloc (sizeof *p);
606   p->next = export_head;
607   p->name = name;
608   p->is_data = is_data;
609   export_head = p;
610 }
611
612 /* This is called at the end of assembly.  For each external function
613    which has not been defined, we output a declaration now.  We also
614    output the .drectve section.  */
615
616 void
617 i386_pe_file_end (void)
618 {
619   struct extern_list *p;
620
621   ix86_file_end ();
622
623   for (p = extern_head; p != NULL; p = p->next)
624     {
625       tree decl;
626
627       decl = p->decl;
628
629       /* Positively ensure only one declaration for any given symbol.  */
630       if (! TREE_ASM_WRITTEN (decl)
631           && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
632         {
633           TREE_ASM_WRITTEN (decl) = 1;
634           i386_pe_declare_function_type (asm_out_file, p->name,
635                                          TREE_PUBLIC (decl));
636         }
637     }
638
639   if (export_head)
640     {
641       struct export_list *q;
642       drectve_section ();
643       for (q = export_head; q != NULL; q = q->next)
644         {
645           fprintf (asm_out_file, "\t.ascii \" -export:%s%s\"\n",
646                    default_strip_name_encoding (q->name),
647                    (q->is_data ? ",data" : ""));
648         }
649     }
650 }
651
652 #include "gt-winnt.h"