OSDN Git Service

8de14adbc4a567c6dad4fa89279cacf0d9bb6dcb
[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 (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 formal_type;
169
170   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);  
171
172   formal_type = TYPE_ARG_TYPES (TREE_TYPE (decl));
173   if (formal_type != NULL_TREE)
174     while (1)
175       {
176         HOST_WIDE_INT parm_size;
177         HOST_WIDE_INT parm_boundary_bytes = PARM_BOUNDARY / BITS_PER_UNIT;
178
179         /* We got to the end of the list without seeing void_list_node,
180            which means the function is variadic.  The suffix is to be
181            ignored in that case.  */
182         if (formal_type == NULL_TREE)
183           return NULL_TREE;
184
185         /* End of arguments, non-varargs marker.  */
186         if (formal_type == void_list_node)
187           break;
188
189         /* Quit if we hit an incomplete type.  Error is reported
190            by convert_arguments in c-typeck.c or cp/typeck.c.  */
191         parm_size = int_size_in_bytes (TREE_VALUE (formal_type));
192         if (parm_size < 0)
193           break;
194
195         /* Must round up to include padding.  This is done the same
196            way as in store_one_arg.  */
197         parm_size = ((parm_size + parm_boundary_bytes - 1)
198                      / parm_boundary_bytes * parm_boundary_bytes);
199         total += parm_size;
200
201         formal_type = TREE_CHAIN (formal_type);
202       }
203   /* Assume max of 8 base 10 digits in the suffix.  */
204   p = new_str = alloca (1 + strlen (old_str) + 1 + 8 + 1);
205   if (fastcall)
206     *p++ = FASTCALL_PREFIX;
207   sprintf (p, "%s@" HOST_WIDE_INT_PRINT_DEC, old_str, total);
208
209   return get_identifier (new_str);
210 }
211
212 /* Maybe decorate and get a new identifier for the DECL of a stdcall or
213    fastcall function. The original identifier is supplied in ID. */
214
215 static tree
216 i386_pe_maybe_mangle_decl_assembler_name (tree decl, tree id)
217 {
218   tree new_id = NULL_TREE;
219
220   if (TREE_CODE (decl) == FUNCTION_DECL)
221     { 
222       tree type_attributes = TYPE_ATTRIBUTES (TREE_TYPE (decl));
223       if (lookup_attribute ("stdcall", type_attributes))
224         new_id = gen_stdcall_or_fastcall_suffix (decl, id, false);
225       else if (lookup_attribute ("fastcall", type_attributes))
226         new_id = gen_stdcall_or_fastcall_suffix (decl, id, true);
227     }
228
229   return new_id;
230 }
231
232 /* This is used as a target hook to modify the DECL_ASSEMBLER_NAME
233    in the language-independent default hook
234    langhooks,c:lhd_set_decl_assembler_name ()
235    and in cp/mangle,c:mangle_decl ().  */
236 tree
237 i386_pe_mangle_decl_assembler_name (tree decl, tree id)
238 {
239   tree new_id = i386_pe_maybe_mangle_decl_assembler_name (decl, id);   
240
241   return (new_id ? new_id : id);
242 }
243
244 void
245 i386_pe_encode_section_info (tree decl, rtx rtl, int first)
246 {
247   rtx symbol;
248   int flags;
249
250   /* Do this last, due to our frobbing of DECL_DLLIMPORT_P above.  */
251   default_encode_section_info (decl, rtl, first);
252
253   /* Careful not to prod global register variables.  */
254   if (!MEM_P (rtl))
255     return;
256
257   symbol = XEXP (rtl, 0);
258   gcc_assert (GET_CODE (symbol) == SYMBOL_REF);
259
260   switch (TREE_CODE (decl))
261     {
262     case FUNCTION_DECL:
263       if (first)
264         {
265           /* FIXME: In Ada, and perhaps other language frontends,
266              imported stdcall names may not yet have been modified.
267              Check and do it know.  */
268          tree new_id;
269          tree old_id = DECL_ASSEMBLER_NAME (decl);
270           const char* asm_str = IDENTIFIER_POINTER (old_id);
271           /* Do not change the identifier if a verbatim asmspec
272              or if stdcall suffix already added. */
273           if (*asm_str == '*' || strchr (asm_str, '@'))
274             break;
275           if ((new_id = i386_pe_maybe_mangle_decl_assembler_name (decl, old_id)))
276             {
277               /* These attributes must be present on first declaration,
278                  change_decl_assembler_name will warn if they are added
279                  later and the decl has been referenced, but duplicate_decls
280                  should catch the mismatch first.  */
281               change_decl_assembler_name (decl, new_id);
282               XSTR (symbol, 0) = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
283             }
284         }
285       break;
286
287     case VAR_DECL:
288       if (lookup_attribute ("selectany", DECL_ATTRIBUTES (decl)))
289         {
290           if (DECL_INITIAL (decl)
291               /* If an object is initialized with a ctor, the static
292                  initialization and destruction code for it is present in
293                  each unit defining the object.  The code that calls the
294                  ctor is protected by a link-once guard variable, so that
295                  the object still has link-once semantics,  */
296               || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
297             make_decl_one_only (decl);
298           else
299             error ("%q+D:'selectany' attribute applies only to "
300                    "initialized objects", decl);
301         }
302       break;
303
304     default:
305       return;
306     }
307
308   /* Mark the decl so we can tell from the rtl whether the object is
309      dllexport'd or dllimport'd.  tree.c: merge_dllimport_decl_attributes
310      handles dllexport/dllimport override semantics.  */
311   flags = (SYMBOL_REF_FLAGS (symbol) &
312            ~(SYMBOL_FLAG_DLLIMPORT | SYMBOL_FLAG_DLLEXPORT));
313   if (i386_pe_determine_dllexport_p (decl))
314     flags |= SYMBOL_FLAG_DLLEXPORT;
315   else if (i386_pe_determine_dllimport_p (decl))
316     {
317       flags |= SYMBOL_FLAG_DLLIMPORT;
318       /* If we went through the associated_type path, this won't already
319          be set.  Though, frankly, this seems wrong, and should be fixed
320          elsewhere.  */
321       if (!DECL_DLLIMPORT_P (decl))
322         {
323           DECL_DLLIMPORT_P (decl) = 1;
324           flags &= ~SYMBOL_FLAG_LOCAL;
325         }
326     }
327   SYMBOL_REF_FLAGS (symbol) = flags;
328 }
329
330 bool
331 i386_pe_binds_local_p (tree exp)
332 {
333   /* PE does not do dynamic binding.  Indeed, the only kind of
334      non-local reference comes from a dllimport'd symbol.  */
335   if ((TREE_CODE (exp) == VAR_DECL || TREE_CODE (exp) == FUNCTION_DECL)
336       && DECL_DLLIMPORT_P (exp))
337     return false;
338
339   return true;
340 }
341
342 /* Also strip the fastcall prefix and stdcall suffix.  */
343
344 const char *
345 i386_pe_strip_name_encoding_full (const char *str)
346 {
347   const char *p;
348   const char *name = default_strip_name_encoding (str);
349
350   /* Strip leading '@' on fastcall symbols.  */
351   if (*name == '@')
352     name++;
353
354   /* Strip trailing "@n".  */
355   p = strchr (name, '@');
356   if (p)
357     return ggc_alloc_string (name, p - name);
358
359   return name;
360 }
361
362 void
363 i386_pe_unique_section (tree decl, int reloc)
364 {
365   int len;
366   const char *name, *prefix;
367   char *string;
368
369   name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
370   name = i386_pe_strip_name_encoding_full (name);
371
372   /* The object is put in, for example, section .text$foo.
373      The linker will then ultimately place them in .text
374      (everything from the $ on is stripped). Don't put
375      read-only data in .rdata section to avoid a PE linker
376      bug when .rdata$* grouped sections are used in code
377      without a .rdata section.  */
378   if (TREE_CODE (decl) == FUNCTION_DECL)
379     prefix = ".text$";
380   else if (decl_readonly_section (decl, reloc))
381     prefix = ".rdata$";
382   else
383     prefix = ".data$";
384   len = strlen (name) + strlen (prefix);
385   string = alloca (len + 1);
386   sprintf (string, "%s%s", prefix, name);
387
388   DECL_SECTION_NAME (decl) = build_string (len, string);
389 }
390
391 /* Select a set of attributes for section NAME based on the properties
392    of DECL and whether or not RELOC indicates that DECL's initializer
393    might contain runtime relocations.
394
395    We make the section read-only and executable for a function decl,
396    read-only for a const data decl, and writable for a non-const data decl.
397
398    If the section has already been defined, to not allow it to have
399    different attributes, as (1) this is ambiguous since we're not seeing
400    all the declarations up front and (2) some assemblers (e.g. SVR4)
401    do not recognize section redefinitions.  */
402 /* ??? This differs from the "standard" PE implementation in that we
403    handle the SHARED variable attribute.  Should this be done for all
404    PE targets?  */
405
406 #define SECTION_PE_SHARED       SECTION_MACH_DEP
407
408 unsigned int
409 i386_pe_section_type_flags (tree decl, const char *name, int reloc)
410 {
411   static htab_t htab;
412   unsigned int flags;
413   unsigned int **slot;
414
415   /* The names we put in the hashtable will always be the unique
416      versions given to us by the stringtable, so we can just use
417      their addresses as the keys.  */
418   if (!htab)
419     htab = htab_create (31, htab_hash_pointer, htab_eq_pointer, NULL);
420
421   if (decl && TREE_CODE (decl) == FUNCTION_DECL)
422     flags = SECTION_CODE;
423   else if (decl && decl_readonly_section (decl, reloc))
424     flags = 0;
425   else
426     {
427       flags = SECTION_WRITE;
428
429       if (decl && TREE_CODE (decl) == VAR_DECL
430           && lookup_attribute ("shared", DECL_ATTRIBUTES (decl)))
431         flags |= SECTION_PE_SHARED;
432     }
433
434   if (decl && DECL_ONE_ONLY (decl))
435     flags |= SECTION_LINKONCE;
436
437   /* See if we already have an entry for this section.  */
438   slot = (unsigned int **) htab_find_slot (htab, name, INSERT);
439   if (!*slot)
440     {
441       *slot = (unsigned int *) xmalloc (sizeof (unsigned int));
442       **slot = flags;
443     }
444   else
445     {
446       if (decl && **slot != flags)
447         error ("%q+D causes a section type conflict", decl);
448     }
449
450   return flags;
451 }
452
453 void
454 i386_pe_asm_named_section (const char *name, unsigned int flags, 
455                            tree decl)
456 {
457   char flagchars[8], *f = flagchars;
458
459   if ((flags & (SECTION_CODE | SECTION_WRITE)) == 0)
460     /* readonly data */
461     {
462       *f++ ='d';  /* This is necessary for older versions of gas.  */
463       *f++ ='r';
464     }
465   else  
466     {
467       if (flags & SECTION_CODE)
468         *f++ = 'x';
469       if (flags & SECTION_WRITE)
470         *f++ = 'w';
471       if (flags & SECTION_PE_SHARED)
472         *f++ = 's';
473     }
474
475   *f = '\0';
476
477   fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
478
479   if (flags & SECTION_LINKONCE)
480     {
481       /* Functions may have been compiled at various levels of
482          optimization so we can't use `same_size' here.
483          Instead, have the linker pick one, without warning.
484          If 'selectany' attribute has been specified,  MS compiler
485          sets 'discard' characteristic, rather than telling linker
486          to warn of size or content mismatch, so do the same.  */ 
487       bool discard = (flags & SECTION_CODE)
488                       || lookup_attribute ("selectany",
489                                            DECL_ATTRIBUTES (decl));      
490       fprintf (asm_out_file, "\t.linkonce %s\n",
491                (discard  ? "discard" : "same_size"));
492     }
493 }
494
495 void
496 i386_pe_asm_output_aligned_decl_common (FILE *stream, tree decl,
497                                         const char *name, HOST_WIDE_INT size,
498                                         HOST_WIDE_INT align ATTRIBUTE_UNUSED)
499 {
500   HOST_WIDE_INT rounded;
501
502   /* Compute as in assemble_noswitch_variable, since we don't actually
503      support aligned common.  */
504   rounded = size ? size : 1;
505   rounded += (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1;
506   rounded = (rounded / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
507              * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
508   
509   i386_pe_maybe_record_exported_symbol (decl, name, 1);
510
511   fprintf (stream, "\t.comm\t");
512   assemble_name (stream, name);
513   fprintf (stream, ", " HOST_WIDE_INT_PRINT_DEC "\t" ASM_COMMENT_START
514            " " HOST_WIDE_INT_PRINT_DEC "\n",
515            rounded, size);
516 }
517 \f
518 /* The Microsoft linker requires that every function be marked as
519    DT_FCN.  When using gas on cygwin, we must emit appropriate .type
520    directives.  */
521
522 #include "gsyms.h"
523
524 /* Mark a function appropriately.  This should only be called for
525    functions for which we are not emitting COFF debugging information.
526    FILE is the assembler output file, NAME is the name of the
527    function, and PUBLIC is nonzero if the function is globally
528    visible.  */
529
530 void
531 i386_pe_declare_function_type (FILE *file, const char *name, int public)
532 {
533   fprintf (file, "\t.def\t");
534   assemble_name (file, name);
535   fprintf (file, ";\t.scl\t%d;\t.type\t%d;\t.endef\n",
536            public ? (int) C_EXT : (int) C_STAT,
537            (int) DT_FCN << N_BTSHFT);
538 }
539
540 /* Keep a list of external functions.  */
541
542 struct extern_list GTY(())
543 {
544   struct extern_list *next;
545   tree decl;
546   const char *name;
547 };
548
549 static GTY(()) struct extern_list *extern_head;
550
551 /* Assemble an external function reference.  We need to keep a list of
552    these, so that we can output the function types at the end of the
553    assembly.  We can't output the types now, because we might see a
554    definition of the function later on and emit debugging information
555    for it then.  */
556
557 void
558 i386_pe_record_external_function (tree decl, const char *name)
559 {
560   struct extern_list *p;
561
562   p = (struct extern_list *) ggc_alloc (sizeof *p);
563   p->next = extern_head;
564   p->decl = decl;
565   p->name = name;
566   extern_head = p;
567 }
568
569 /* Keep a list of exported symbols.  */
570
571 struct export_list GTY(())
572 {
573   struct export_list *next;
574   const char *name;
575   int is_data;          /* used to type tag exported symbols.  */
576 };
577
578 static GTY(()) struct export_list *export_head;
579
580 /* Assemble an export symbol entry.  We need to keep a list of
581    these, so that we can output the export list at the end of the
582    assembly.  We used to output these export symbols in each function,
583    but that causes problems with GNU ld when the sections are
584    linkonce.  */
585
586 void
587 i386_pe_maybe_record_exported_symbol (tree decl, const char *name, int is_data)
588 {
589   rtx symbol;
590   struct export_list *p;
591
592   symbol = XEXP (DECL_RTL (decl), 0);
593   gcc_assert (GET_CODE (symbol) == SYMBOL_REF);
594   if (!SYMBOL_REF_DLLEXPORT_P (symbol))
595     return;
596
597   p = (struct export_list *) ggc_alloc (sizeof *p);
598   p->next = export_head;
599   p->name = name;
600   p->is_data = is_data;
601   export_head = p;
602 }
603
604 /* This is called at the end of assembly.  For each external function
605    which has not been defined, we output a declaration now.  We also
606    output the .drectve section.  */
607
608 void
609 i386_pe_file_end (void)
610 {
611   struct extern_list *p;
612
613   ix86_file_end ();
614
615   for (p = extern_head; p != NULL; p = p->next)
616     {
617       tree decl;
618
619       decl = p->decl;
620
621       /* Positively ensure only one declaration for any given symbol.  */
622       if (! TREE_ASM_WRITTEN (decl)
623           && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
624         {
625           TREE_ASM_WRITTEN (decl) = 1;
626           i386_pe_declare_function_type (asm_out_file, p->name,
627                                          TREE_PUBLIC (decl));
628         }
629     }
630
631   if (export_head)
632     {
633       struct export_list *q;
634       drectve_section ();
635       for (q = export_head; q != NULL; q = q->next)
636         {
637           fprintf (asm_out_file, "\t.ascii \" -export:%s%s\"\n",
638                    default_strip_name_encoding (q->name),
639                    (q->is_data ? ",data" : ""));
640         }
641     }
642 }
643
644 #include "gt-winnt.h"