OSDN Git Service

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