remove useless unused attributes in i386 code
[official-gcc.git] / gcc / config / i386 / winnt.c
blobbf6e2b1fec2aebd7349ecf05b1d371e573cb6609
1 /* Subroutines for insn-output.c for Windows NT.
2 Contributed by Douglas Rupp (drupp@cs.washington.edu)
3 Copyright (C) 1995-2014 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "rtl.h"
26 #include "regs.h"
27 #include "hard-reg-set.h"
28 #include "output.h"
29 #include "tree.h"
30 #include "stringpool.h"
31 #include "varasm.h"
32 #include "flags.h"
33 #include "tm_p.h"
34 #include "diagnostic-core.h"
35 #include "hash-table.h"
36 #include "langhooks.h"
37 #include "ggc.h"
38 #include "target.h"
39 #include "except.h"
40 #include "pointer-set.h"
41 #include "hash-table.h"
42 #include "vec.h"
43 #include "basic-block.h"
44 #include "tree-ssa-alias.h"
45 #include "internal-fn.h"
46 #include "gimple-fold.h"
47 #include "tree-eh.h"
48 #include "gimple-expr.h"
49 #include "is-a.h"
50 #include "gimple.h"
51 #include "lto-streamer.h"
52 #include "lto-section-names.h"
53 #include "builtins.h"
55 /* i386/PE specific attribute support.
57 i386/PE has two new attributes:
58 dllexport - for exporting a function/variable that will live in a dll
59 dllimport - for importing a function/variable from a dll
61 Microsoft allows multiple declspecs in one __declspec, separating
62 them with spaces. We do NOT support this. Instead, use __declspec
63 multiple times.
66 /* Handle a "shared" attribute;
67 arguments as in struct attribute_spec.handler. */
68 tree
69 ix86_handle_shared_attribute (tree *node, tree name, tree, int,
70 bool *no_add_attrs)
72 if (TREE_CODE (*node) != VAR_DECL)
74 warning (OPT_Wattributes, "%qE attribute only applies to variables",
75 name);
76 *no_add_attrs = true;
79 return NULL_TREE;
82 /* Handle a "selectany" attribute;
83 arguments as in struct attribute_spec.handler. */
84 tree
85 ix86_handle_selectany_attribute (tree *node, tree name, tree, int,
86 bool *no_add_attrs)
88 /* The attribute applies only to objects that are initialized and have
89 external linkage. However, we may not know about initialization
90 until the language frontend has processed the decl. We'll check for
91 initialization later in encode_section_info. */
92 if (TREE_CODE (*node) != VAR_DECL || !TREE_PUBLIC (*node))
94 error ("%qE attribute applies only to initialized variables"
95 " with external linkage", name);
96 *no_add_attrs = true;
99 return NULL_TREE;
103 /* Return the type that we should use to determine if DECL is
104 imported or exported. */
106 static tree
107 associated_type (tree decl)
109 return (DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
110 ? DECL_CONTEXT (decl) : NULL_TREE);
113 /* Return true if DECL should be a dllexport'd object. */
115 static bool
116 i386_pe_determine_dllexport_p (tree decl)
118 if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != FUNCTION_DECL)
119 return false;
121 /* Don't export local clones of dllexports. */
122 if (!TREE_PUBLIC (decl))
123 return false;
125 if (TREE_CODE (decl) == FUNCTION_DECL
126 && DECL_DECLARED_INLINE_P (decl)
127 && !flag_keep_inline_dllexport)
128 return false;
130 if (lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
131 return true;
133 return false;
136 /* Return true if DECL should be a dllimport'd object. */
138 static bool
139 i386_pe_determine_dllimport_p (tree decl)
141 tree assoc;
143 if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != FUNCTION_DECL)
144 return false;
146 if (DECL_DLLIMPORT_P (decl))
147 return true;
149 /* The DECL_DLLIMPORT_P flag was set for decls in the class definition
150 by targetm.cxx.adjust_class_at_definition. Check again to emit
151 error message if the class attribute has been overridden by an
152 out-of-class definition of static data. */
153 assoc = associated_type (decl);
154 if (assoc && lookup_attribute ("dllimport", TYPE_ATTRIBUTES (assoc))
155 && TREE_CODE (decl) == VAR_DECL
156 && TREE_STATIC (decl) && TREE_PUBLIC (decl)
157 && !DECL_EXTERNAL (decl)
158 /* vtable's are linkonce constants, so defining a vtable is not
159 an error as long as we don't try to import it too. */
160 && !DECL_VIRTUAL_P (decl))
161 error ("definition of static data member %q+D of "
162 "dllimport%'d class", decl);
164 return false;
167 /* Handle the -mno-fun-dllimport target switch. */
169 bool
170 i386_pe_valid_dllimport_attribute_p (const_tree decl)
172 if (TARGET_NOP_FUN_DLLIMPORT && TREE_CODE (decl) == FUNCTION_DECL)
173 return false;
174 return true;
177 /* Return string which is the function name, identified by ID, modified
178 with a suffix consisting of an atsign (@) followed by the number of
179 bytes of arguments. If ID is NULL use the DECL_NAME as base. If
180 FASTCALL is true, also add the FASTCALL_PREFIX.
181 Return NULL if no change required. */
183 static tree
184 gen_stdcall_or_fastcall_suffix (tree decl, tree id, bool fastcall)
186 HOST_WIDE_INT total = 0;
187 const char *old_str = IDENTIFIER_POINTER (id != NULL_TREE ? id : DECL_NAME (decl));
188 char *new_str, *p;
189 tree type = TREE_TYPE (DECL_ORIGIN (decl));
190 tree arg;
191 function_args_iterator args_iter;
193 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
195 if (prototype_p (type))
197 /* This attribute is ignored for variadic functions. */
198 if (stdarg_p (type))
199 return NULL_TREE;
201 /* Quit if we hit an incomplete type. Error is reported
202 by convert_arguments in c-typeck.c or cp/typeck.c. */
203 FOREACH_FUNCTION_ARGS(type, arg, args_iter)
205 HOST_WIDE_INT parm_size;
206 HOST_WIDE_INT parm_boundary_bytes = PARM_BOUNDARY / BITS_PER_UNIT;
208 if (! COMPLETE_TYPE_P (arg))
209 break;
211 parm_size = int_size_in_bytes (arg);
212 if (parm_size < 0)
213 break;
215 /* Must round up to include padding. This is done the same
216 way as in store_one_arg. */
217 parm_size = ((parm_size + parm_boundary_bytes - 1)
218 / parm_boundary_bytes * parm_boundary_bytes);
219 total += parm_size;
223 /* Assume max of 8 base 10 digits in the suffix. */
224 p = new_str = XALLOCAVEC (char, 1 + strlen (old_str) + 1 + 8 + 1);
225 if (fastcall)
226 *p++ = FASTCALL_PREFIX;
227 sprintf (p, "%s@" HOST_WIDE_INT_PRINT_DEC, old_str, total);
229 return get_identifier (new_str);
232 /* Maybe decorate and get a new identifier for the DECL of a stdcall or
233 fastcall function. The original identifier is supplied in ID. */
235 static tree
236 i386_pe_maybe_mangle_decl_assembler_name (tree decl, tree id)
238 tree new_id = NULL_TREE;
240 if (TREE_CODE (decl) == FUNCTION_DECL)
242 unsigned int ccvt = ix86_get_callcvt (TREE_TYPE (decl));
243 if ((ccvt & IX86_CALLCVT_STDCALL) != 0)
245 if (TARGET_RTD)
246 /* If we are using -mrtd emit undecorated symbol and let linker
247 do the proper resolving. */
248 return NULL_TREE;
249 new_id = gen_stdcall_or_fastcall_suffix (decl, id, false);
251 else if ((ccvt & IX86_CALLCVT_FASTCALL) != 0)
252 new_id = gen_stdcall_or_fastcall_suffix (decl, id, true);
255 return new_id;
258 /* Emit an assembler directive to set symbol for DECL visibility to
259 the visibility type VIS, which must not be VISIBILITY_DEFAULT.
260 As for PE there is no hidden support in gas, we just warn for
261 user-specified visibility attributes. */
263 void
264 i386_pe_assemble_visibility (tree decl, int)
266 if (!decl
267 || !lookup_attribute ("visibility", DECL_ATTRIBUTES (decl)))
268 return;
269 if (!DECL_ARTIFICIAL (decl))
270 warning (OPT_Wattributes, "visibility attribute not supported "
271 "in this configuration; ignored");
274 /* This is used as a target hook to modify the DECL_ASSEMBLER_NAME
275 in the language-independent default hook
276 langhooks,c:lhd_set_decl_assembler_name ()
277 and in cp/mangle,c:mangle_decl (). */
278 tree
279 i386_pe_mangle_decl_assembler_name (tree decl, tree id)
281 tree new_id = i386_pe_maybe_mangle_decl_assembler_name (decl, id);
283 return (new_id ? new_id : id);
286 /* This hook behaves the same as varasm.c/assemble_name(), but
287 generates the name into memory rather than outputting it to
288 a file stream. */
290 tree
291 i386_pe_mangle_assembler_name (const char *name)
293 const char *skipped = name + (*name == '*' ? 1 : 0);
294 const char *stripped = targetm.strip_name_encoding (skipped);
295 if (*name != '*' && *user_label_prefix && *stripped != FASTCALL_PREFIX)
296 stripped = ACONCAT ((user_label_prefix, stripped, NULL));
297 return get_identifier (stripped);
300 void
301 i386_pe_encode_section_info (tree decl, rtx rtl, int first)
303 rtx symbol;
304 int flags;
306 /* Do this last, due to our frobbing of DECL_DLLIMPORT_P above. */
307 default_encode_section_info (decl, rtl, first);
309 /* Careful not to prod global register variables. */
310 if (!MEM_P (rtl))
311 return;
313 symbol = XEXP (rtl, 0);
314 gcc_assert (GET_CODE (symbol) == SYMBOL_REF);
316 switch (TREE_CODE (decl))
318 case FUNCTION_DECL:
319 /* FIXME: Imported stdcall names are not modified by the Ada frontend.
320 Check and decorate the RTL name now. */
321 if (strcmp (lang_hooks.name, "GNU Ada") == 0)
323 tree new_id;
324 tree old_id = DECL_ASSEMBLER_NAME (decl);
325 const char* asm_str = IDENTIFIER_POINTER (old_id);
326 /* Do not change the identifier if a verbatim asmspec
327 or if stdcall suffix already added. */
328 if (!(*asm_str == '*' || strchr (asm_str, '@'))
329 && (new_id = i386_pe_maybe_mangle_decl_assembler_name (decl,
330 old_id)))
331 XSTR (symbol, 0) = IDENTIFIER_POINTER (new_id);
333 break;
335 case VAR_DECL:
336 if (lookup_attribute ("selectany", DECL_ATTRIBUTES (decl)))
338 if (DECL_INITIAL (decl)
339 /* If an object is initialized with a ctor, the static
340 initialization and destruction code for it is present in
341 each unit defining the object. The code that calls the
342 ctor is protected by a link-once guard variable, so that
343 the object still has link-once semantics, */
344 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
345 make_decl_one_only (decl, DECL_ASSEMBLER_NAME (decl));
346 else
347 error ("%q+D:'selectany' attribute applies only to "
348 "initialized objects", decl);
350 break;
352 default:
353 return;
356 /* Mark the decl so we can tell from the rtl whether the object is
357 dllexport'd or dllimport'd. tree.c: merge_dllimport_decl_attributes
358 handles dllexport/dllimport override semantics. */
359 flags = (SYMBOL_REF_FLAGS (symbol) &
360 ~(SYMBOL_FLAG_DLLIMPORT | SYMBOL_FLAG_DLLEXPORT));
361 if (i386_pe_determine_dllexport_p (decl))
362 flags |= SYMBOL_FLAG_DLLEXPORT;
363 else if (i386_pe_determine_dllimport_p (decl))
364 flags |= SYMBOL_FLAG_DLLIMPORT;
366 SYMBOL_REF_FLAGS (symbol) = flags;
370 bool
371 i386_pe_binds_local_p (const_tree exp)
373 if ((TREE_CODE (exp) == VAR_DECL || TREE_CODE (exp) == FUNCTION_DECL)
374 && DECL_DLLIMPORT_P (exp))
375 return false;
377 /* External public symbols, which aren't weakref-s,
378 have local-binding for PE targets. */
379 if (DECL_P (exp)
380 && !lookup_attribute ("weakref", DECL_ATTRIBUTES (exp))
381 && TREE_PUBLIC (exp)
382 && DECL_EXTERNAL (exp))
383 return true;
384 return default_binds_local_p_1 (exp, 0);
387 /* Also strip the fastcall prefix and stdcall suffix. */
389 const char *
390 i386_pe_strip_name_encoding_full (const char *str)
392 const char *p;
393 const char *name = default_strip_name_encoding (str);
395 /* Strip leading '@' on fastcall symbols. */
396 if (*name == '@')
397 name++;
399 /* Strip trailing "@n". */
400 p = strchr (name, '@');
401 if (p)
402 return ggc_alloc_string (name, p - name);
404 return name;
407 void
408 i386_pe_unique_section (tree decl, int reloc)
410 int len;
411 const char *name, *prefix;
412 char *string;
414 /* Ignore RELOC, if we are allowed to put relocated
415 const data into read-only section. */
416 if (!flag_writable_rel_rdata)
417 reloc = 0;
418 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
419 name = i386_pe_strip_name_encoding_full (name);
421 /* The object is put in, for example, section .text$foo.
422 The linker will then ultimately place them in .text
423 (everything from the $ on is stripped). Don't put
424 read-only data in .rdata section to avoid a PE linker
425 bug when .rdata$* grouped sections are used in code
426 without a .rdata section. */
427 if (TREE_CODE (decl) == FUNCTION_DECL)
428 prefix = ".text$";
429 else if (decl_readonly_section (decl, reloc))
430 prefix = ".rdata$";
431 else
432 prefix = ".data$";
433 len = strlen (name) + strlen (prefix);
434 string = XALLOCAVEC (char, len + 1);
435 sprintf (string, "%s%s", prefix, name);
437 set_decl_section_name (decl, string);
440 /* Local and global relocs can be placed always into readonly memory for
441 memory for PE-COFF targets. */
443 i386_pe_reloc_rw_mask (void)
445 return 0;
448 /* Select a set of attributes for section NAME based on the properties
449 of DECL and whether or not RELOC indicates that DECL's initializer
450 might contain runtime relocations.
452 We make the section read-only and executable for a function decl,
453 read-only for a const data decl, and writable for a non-const data decl.
455 If the section has already been defined, to not allow it to have
456 different attributes, as (1) this is ambiguous since we're not seeing
457 all the declarations up front and (2) some assemblers (e.g. SVR4)
458 do not recognize section redefinitions. */
459 /* ??? This differs from the "standard" PE implementation in that we
460 handle the SHARED variable attribute. Should this be done for all
461 PE targets? */
463 #define SECTION_PE_SHARED SECTION_MACH_DEP
465 unsigned int
466 i386_pe_section_type_flags (tree decl, const char *, int reloc)
468 unsigned int flags;
470 /* Ignore RELOC, if we are allowed to put relocated
471 const data into read-only section. */
472 if (!flag_writable_rel_rdata)
473 reloc = 0;
475 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
476 flags = SECTION_CODE;
477 else if (decl && decl_readonly_section (decl, reloc))
478 flags = 0;
479 else
481 flags = SECTION_WRITE;
483 if (decl && TREE_CODE (decl) == VAR_DECL
484 && lookup_attribute ("shared", DECL_ATTRIBUTES (decl)))
485 flags |= SECTION_PE_SHARED;
488 if (decl && DECL_P (decl) && DECL_ONE_ONLY (decl))
489 flags |= SECTION_LINKONCE;
491 return flags;
494 void
495 i386_pe_asm_named_section (const char *name, unsigned int flags,
496 tree decl)
498 char flagchars[8], *f = flagchars;
500 #if defined (HAVE_GAS_SECTION_EXCLUDE) && HAVE_GAS_SECTION_EXCLUDE == 1
501 if ((flags & SECTION_EXCLUDE) != 0)
502 *f++ = 'e';
503 #endif
505 if ((flags & (SECTION_CODE | SECTION_WRITE)) == 0)
506 /* readonly data */
508 *f++ ='d'; /* This is necessary for older versions of gas. */
509 *f++ ='r';
511 else
513 if (flags & SECTION_CODE)
514 *f++ = 'x';
515 if (flags & SECTION_WRITE)
516 *f++ = 'w';
517 if (flags & SECTION_PE_SHARED)
518 *f++ = 's';
519 #if !defined (HAVE_GAS_SECTION_EXCLUDE) || HAVE_GAS_SECTION_EXCLUDE == 0
520 /* If attribute "e" isn't supported we mark this section as
521 never-load. */
522 if ((flags & SECTION_EXCLUDE) != 0)
523 *f++ = 'n';
524 #endif
527 /* LTO sections need 1-byte alignment to avoid confusing the
528 zlib decompression algorithm with trailing zero pad bytes. */
529 if (strncmp (name, LTO_SECTION_NAME_PREFIX,
530 strlen (LTO_SECTION_NAME_PREFIX)) == 0)
531 *f++ = '0';
533 *f = '\0';
535 fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
537 if (flags & SECTION_LINKONCE)
539 /* Functions may have been compiled at various levels of
540 optimization so we can't use `same_size' here.
541 Instead, have the linker pick one, without warning.
542 If 'selectany' attribute has been specified, MS compiler
543 sets 'discard' characteristic, rather than telling linker
544 to warn of size or content mismatch, so do the same. */
545 bool discard = (flags & SECTION_CODE)
546 || (TREE_CODE (decl) != IDENTIFIER_NODE
547 && lookup_attribute ("selectany",
548 DECL_ATTRIBUTES (decl)));
549 fprintf (asm_out_file, "\t.linkonce %s\n",
550 (discard ? "discard" : "same_size"));
554 /* Beware, DECL may be NULL if compile_file() is emitting the LTO marker. */
556 void
557 i386_pe_asm_output_aligned_decl_common (FILE *stream, tree decl,
558 const char *name, HOST_WIDE_INT size,
559 HOST_WIDE_INT align)
561 HOST_WIDE_INT rounded;
563 /* Compute as in assemble_noswitch_variable, since we don't have
564 support for aligned common on older binutils. We must also
565 avoid emitting a common symbol of size zero, as this is the
566 overloaded representation that indicates an undefined external
567 symbol in the PE object file format. */
568 rounded = size ? size : 1;
569 rounded += (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1;
570 rounded = (rounded / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
571 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
573 i386_pe_maybe_record_exported_symbol (decl, name, 1);
575 fprintf (stream, "\t.comm\t");
576 assemble_name (stream, name);
577 if (use_pe_aligned_common)
578 fprintf (stream, ", " HOST_WIDE_INT_PRINT_DEC ", %d\n",
579 size ? size : (HOST_WIDE_INT) 1,
580 exact_log2 (align) - exact_log2 (CHAR_BIT));
581 else
582 fprintf (stream, ", " HOST_WIDE_INT_PRINT_DEC "\t" ASM_COMMENT_START
583 " " HOST_WIDE_INT_PRINT_DEC "\n", rounded, size);
586 /* The Microsoft linker requires that every function be marked as
587 DT_FCN. When using gas on cygwin, we must emit appropriate .type
588 directives. */
590 #include "gsyms.h"
592 /* Mark a function appropriately. This should only be called for
593 functions for which we are not emitting COFF debugging information.
594 FILE is the assembler output file, NAME is the name of the
595 function, and PUB is nonzero if the function is globally
596 visible. */
598 void
599 i386_pe_declare_function_type (FILE *file, const char *name, int pub)
601 fprintf (file, "\t.def\t");
602 assemble_name (file, name);
603 fprintf (file, ";\t.scl\t%d;\t.type\t%d;\t.endef\n",
604 pub ? (int) C_EXT : (int) C_STAT,
605 (int) DT_FCN << N_BTSHFT);
608 /* Keep a list of external functions. */
610 struct GTY(()) extern_list
612 struct extern_list *next;
613 tree decl;
614 const char *name;
617 static GTY(()) struct extern_list *extern_head;
619 /* Assemble an external function reference. We need to keep a list of
620 these, so that we can output the function types at the end of the
621 assembly. We can't output the types now, because we might see a
622 definition of the function later on and emit debugging information
623 for it then. */
625 void
626 i386_pe_record_external_function (tree decl, const char *name)
628 struct extern_list *p;
630 p = ggc_alloc<extern_list> ();
631 p->next = extern_head;
632 p->decl = decl;
633 p->name = name;
634 extern_head = p;
637 /* Keep a list of exported symbols. */
639 struct GTY(()) export_list
641 struct export_list *next;
642 const char *name;
643 int is_data; /* used to type tag exported symbols. */
646 /* Keep a list of stub symbols. */
648 struct GTY(()) stub_list
650 struct stub_list *next;
651 const char *name;
654 static GTY(()) struct export_list *export_head;
656 static GTY(()) struct stub_list *stub_head;
658 /* Assemble an export symbol entry. We need to keep a list of
659 these, so that we can output the export list at the end of the
660 assembly. We used to output these export symbols in each function,
661 but that causes problems with GNU ld when the sections are
662 linkonce. Beware, DECL may be NULL if compile_file() is emitting
663 the LTO marker. */
665 void
666 i386_pe_maybe_record_exported_symbol (tree decl, const char *name, int is_data)
668 rtx symbol;
669 struct export_list *p;
671 if (!decl)
672 return;
674 symbol = XEXP (DECL_RTL (decl), 0);
675 gcc_assert (GET_CODE (symbol) == SYMBOL_REF);
676 if (!SYMBOL_REF_DLLEXPORT_P (symbol))
677 return;
679 gcc_assert (TREE_PUBLIC (decl));
681 p = ggc_alloc<export_list> ();
682 p->next = export_head;
683 p->name = name;
684 p->is_data = is_data;
685 export_head = p;
688 void
689 i386_pe_record_stub (const char *name)
691 struct stub_list *p;
693 if (!name || *name == 0)
694 return;
696 p = stub_head;
697 while (p != NULL)
699 if (p->name[0] == *name
700 && !strcmp (p->name, name))
701 return;
702 p = p->next;
705 p = ggc_alloc<stub_list> ();
706 p->next = stub_head;
707 p->name = name;
708 stub_head = p;
712 #ifdef CXX_WRAP_SPEC_LIST
714 /* Hashtable helpers. */
716 struct wrapped_symbol_hasher : typed_noop_remove <char>
718 typedef char value_type;
719 typedef char compare_type;
720 static inline hashval_t hash (const value_type *);
721 static inline bool equal (const value_type *, const compare_type *);
722 static inline void remove (value_type *);
725 inline hashval_t
726 wrapped_symbol_hasher::hash (const value_type *v)
728 return htab_hash_string (v);
731 /* Hash table equality helper function. */
733 inline bool
734 wrapped_symbol_hasher::equal (const value_type *x, const compare_type *y)
736 return !strcmp (x, y);
739 /* Search for a function named TARGET in the list of library wrappers
740 we are using, returning a pointer to it if found or NULL if not.
741 This function might be called on quite a few symbols, and we only
742 have the list of names of wrapped functions available to us as a
743 spec string, so first time round we lazily initialise a hash table
744 to make things quicker. */
746 static const char *
747 i386_find_on_wrapper_list (const char *target)
749 static char first_time = 1;
750 static hash_table<wrapped_symbol_hasher> *wrappers;
752 if (first_time)
754 /* Beware that this is not a complicated parser, it assumes
755 that any sequence of non-whitespace beginning with an
756 underscore is one of the wrapped symbols. For now that's
757 adequate to distinguish symbols from spec substitutions
758 and command-line options. */
759 static char wrapper_list_buffer[] = CXX_WRAP_SPEC_LIST;
760 char *bufptr;
761 /* Breaks up the char array into separated strings
762 strings and enter them into the hash table. */
763 wrappers = new hash_table<wrapped_symbol_hasher> (8);
764 for (bufptr = wrapper_list_buffer; *bufptr; ++bufptr)
766 char *found = NULL;
767 if (ISSPACE (*bufptr))
768 continue;
769 if (*bufptr == '_')
770 found = bufptr;
771 while (*bufptr && !ISSPACE (*bufptr))
772 ++bufptr;
773 if (*bufptr)
774 *bufptr = 0;
775 if (found)
776 *wrappers->find_slot (found, INSERT) = found;
778 first_time = 0;
781 return wrappers->find (target);
784 #endif /* CXX_WRAP_SPEC_LIST */
786 /* This is called at the end of assembly. For each external function
787 which has not been defined, we output a declaration now. We also
788 output the .drectve section. */
790 void
791 i386_pe_file_end (void)
793 struct extern_list *p;
795 for (p = extern_head; p != NULL; p = p->next)
797 tree decl;
799 decl = p->decl;
801 /* Positively ensure only one declaration for any given symbol. */
802 if (! TREE_ASM_WRITTEN (decl)
803 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
805 #ifdef CXX_WRAP_SPEC_LIST
806 /* To ensure the DLL that provides the corresponding real
807 functions is still loaded at runtime, we must reference
808 the real function so that an (unused) import is created. */
809 const char *realsym = i386_find_on_wrapper_list (p->name);
810 if (realsym)
811 i386_pe_declare_function_type (asm_out_file,
812 concat ("__real_", realsym, NULL), TREE_PUBLIC (decl));
813 #endif /* CXX_WRAP_SPEC_LIST */
814 TREE_ASM_WRITTEN (decl) = 1;
815 i386_pe_declare_function_type (asm_out_file, p->name,
816 TREE_PUBLIC (decl));
820 if (export_head)
822 struct export_list *q;
823 drectve_section ();
824 for (q = export_head; q != NULL; q = q->next)
826 fprintf (asm_out_file, "\t.ascii \" -export:\\\"%s\\\"%s\"\n",
827 default_strip_name_encoding (q->name),
828 (q->is_data ? ",data" : ""));
832 if (stub_head)
834 struct stub_list *q;
836 for (q = stub_head; q != NULL; q = q->next)
838 const char *name = q->name;
839 const char *oname;
841 if (name[0] == '*')
842 ++name;
843 oname = name;
844 if (name[0] == '.')
845 ++name;
846 if (strncmp (name, "refptr.", 7) != 0)
847 continue;
848 name += 7;
849 fprintf (asm_out_file, "\t.section\t.rdata$%s, \"dr\"\n"
850 "\t.globl\t%s\n"
851 "\t.linkonce\tdiscard\n", oname, oname);
852 fprintf (asm_out_file, "%s:\n\t.quad\t%s\n", oname, name);
858 /* x64 Structured Exception Handling unwind info. */
860 struct seh_frame_state
862 /* SEH records saves relative to the "current" stack pointer, whether
863 or not there's a frame pointer in place. This tracks the current
864 stack pointer offset from the CFA. */
865 HOST_WIDE_INT sp_offset;
867 /* The CFA is located at CFA_REG + CFA_OFFSET. */
868 HOST_WIDE_INT cfa_offset;
869 rtx cfa_reg;
872 /* Set up data structures beginning output for SEH. */
874 void
875 i386_pe_seh_init (FILE *f)
877 struct seh_frame_state *seh;
879 if (!TARGET_SEH)
880 return;
881 if (cfun->is_thunk)
882 return;
884 /* We cannot support DRAP with SEH. We turned off support for it by
885 re-defining MAX_STACK_ALIGNMENT when SEH is enabled. */
886 gcc_assert (!stack_realign_drap);
888 seh = XCNEW (struct seh_frame_state);
889 cfun->machine->seh = seh;
891 seh->sp_offset = INCOMING_FRAME_SP_OFFSET;
892 seh->cfa_offset = INCOMING_FRAME_SP_OFFSET;
893 seh->cfa_reg = stack_pointer_rtx;
895 fputs ("\t.seh_proc\t", f);
896 assemble_name (f, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (cfun->decl)));
897 fputc ('\n', f);
900 void
901 i386_pe_seh_end_prologue (FILE *f)
903 struct seh_frame_state *seh;
905 if (!TARGET_SEH)
906 return;
907 if (cfun->is_thunk)
908 return;
909 seh = cfun->machine->seh;
911 XDELETE (seh);
912 cfun->machine->seh = NULL;
914 fputs ("\t.seh_endprologue\n", f);
917 static void
918 i386_pe_seh_fini (FILE *f)
920 if (!TARGET_SEH)
921 return;
922 if (cfun->is_thunk)
923 return;
924 fputs ("\t.seh_endproc\n", f);
927 /* Emit an assembler directive to save REG via a PUSH. */
929 static void
930 seh_emit_push (FILE *f, struct seh_frame_state *seh, rtx reg)
932 unsigned int regno = REGNO (reg);
934 gcc_checking_assert (GENERAL_REGNO_P (regno));
936 seh->sp_offset += UNITS_PER_WORD;
937 if (seh->cfa_reg == stack_pointer_rtx)
938 seh->cfa_offset += UNITS_PER_WORD;
940 fputs ("\t.seh_pushreg\t", f);
941 print_reg (reg, 0, f);
942 fputc ('\n', f);
945 /* Emit an assembler directive to save REG at CFA - CFA_OFFSET. */
947 static void
948 seh_emit_save (FILE *f, struct seh_frame_state *seh,
949 rtx reg, HOST_WIDE_INT cfa_offset)
951 unsigned int regno = REGNO (reg);
952 HOST_WIDE_INT offset;
954 /* Negative save offsets are of course not supported, since that
955 would be a store below the stack pointer and thus clobberable. */
956 gcc_assert (seh->sp_offset >= cfa_offset);
957 offset = seh->sp_offset - cfa_offset;
959 fputs ((SSE_REGNO_P (regno) ? "\t.seh_savexmm\t"
960 : GENERAL_REGNO_P (regno) ? "\t.seh_savereg\t"
961 : (gcc_unreachable (), "")), f);
962 print_reg (reg, 0, f);
963 fprintf (f, ", " HOST_WIDE_INT_PRINT_DEC "\n", offset);
966 /* Emit an assembler directive to adjust RSP by OFFSET. */
968 static void
969 seh_emit_stackalloc (FILE *f, struct seh_frame_state *seh,
970 HOST_WIDE_INT offset)
972 /* We're only concerned with prologue stack allocations, which all
973 are subtractions from the stack pointer. */
974 gcc_assert (offset < 0);
975 offset = -offset;
977 if (seh->cfa_reg == stack_pointer_rtx)
978 seh->cfa_offset += offset;
979 seh->sp_offset += offset;
981 /* Do not output the stackalloc in that case (it won't work as there is no
982 encoding for very large frame size). */
983 if (offset < SEH_MAX_FRAME_SIZE)
984 fprintf (f, "\t.seh_stackalloc\t" HOST_WIDE_INT_PRINT_DEC "\n", offset);
987 /* Process REG_CFA_ADJUST_CFA for SEH. */
989 static void
990 seh_cfa_adjust_cfa (FILE *f, struct seh_frame_state *seh, rtx pat)
992 rtx dest, src;
993 HOST_WIDE_INT reg_offset = 0;
994 unsigned int dest_regno;
996 dest = SET_DEST (pat);
997 src = SET_SRC (pat);
999 if (GET_CODE (src) == PLUS)
1001 reg_offset = INTVAL (XEXP (src, 1));
1002 src = XEXP (src, 0);
1004 else if (GET_CODE (src) == MINUS)
1006 reg_offset = -INTVAL (XEXP (src, 1));
1007 src = XEXP (src, 0);
1009 gcc_assert (src == stack_pointer_rtx);
1010 gcc_assert (seh->cfa_reg == stack_pointer_rtx);
1011 dest_regno = REGNO (dest);
1013 if (dest_regno == STACK_POINTER_REGNUM)
1014 seh_emit_stackalloc (f, seh, reg_offset);
1015 else if (dest_regno == HARD_FRAME_POINTER_REGNUM)
1017 HOST_WIDE_INT offset;
1019 seh->cfa_reg = dest;
1020 seh->cfa_offset -= reg_offset;
1022 offset = seh->sp_offset - seh->cfa_offset;
1024 gcc_assert ((offset & 15) == 0);
1025 gcc_assert (IN_RANGE (offset, 0, 240));
1027 fputs ("\t.seh_setframe\t", f);
1028 print_reg (seh->cfa_reg, 0, f);
1029 fprintf (f, ", " HOST_WIDE_INT_PRINT_DEC "\n", offset);
1031 else
1032 gcc_unreachable ();
1035 /* Process REG_CFA_OFFSET for SEH. */
1037 static void
1038 seh_cfa_offset (FILE *f, struct seh_frame_state *seh, rtx pat)
1040 rtx dest, src;
1041 HOST_WIDE_INT reg_offset;
1043 dest = SET_DEST (pat);
1044 src = SET_SRC (pat);
1046 gcc_assert (MEM_P (dest));
1047 dest = XEXP (dest, 0);
1048 if (REG_P (dest))
1049 reg_offset = 0;
1050 else
1052 gcc_assert (GET_CODE (dest) == PLUS);
1053 reg_offset = INTVAL (XEXP (dest, 1));
1054 dest = XEXP (dest, 0);
1056 gcc_assert (dest == seh->cfa_reg);
1058 seh_emit_save (f, seh, src, seh->cfa_offset - reg_offset);
1061 /* Process a FRAME_RELATED_EXPR for SEH. */
1063 static void
1064 seh_frame_related_expr (FILE *f, struct seh_frame_state *seh, rtx pat)
1066 rtx dest, src;
1067 HOST_WIDE_INT addend;
1069 /* See the full loop in dwarf2out_frame_debug_expr. */
1070 if (GET_CODE (pat) == PARALLEL || GET_CODE (pat) == SEQUENCE)
1072 int i, n = XVECLEN (pat, 0), pass, npass;
1074 npass = (GET_CODE (pat) == PARALLEL ? 2 : 1);
1075 for (pass = 0; pass < npass; ++pass)
1076 for (i = 0; i < n; ++i)
1078 rtx ele = XVECEXP (pat, 0, i);
1080 if (GET_CODE (ele) != SET)
1081 continue;
1082 dest = SET_DEST (ele);
1084 /* Process each member of the PARALLEL independently. The first
1085 member is always processed; others only if they are marked. */
1086 if (i == 0 || RTX_FRAME_RELATED_P (ele))
1088 /* Evaluate all register saves in the first pass and all
1089 register updates in the second pass. */
1090 if ((MEM_P (dest) ^ pass) || npass == 1)
1091 seh_frame_related_expr (f, seh, ele);
1094 return;
1097 dest = SET_DEST (pat);
1098 src = SET_SRC (pat);
1100 switch (GET_CODE (dest))
1102 case REG:
1103 switch (GET_CODE (src))
1105 case REG:
1106 /* REG = REG: This should be establishing a frame pointer. */
1107 gcc_assert (src == stack_pointer_rtx);
1108 gcc_assert (dest == hard_frame_pointer_rtx);
1109 seh_cfa_adjust_cfa (f, seh, pat);
1110 break;
1112 case PLUS:
1113 addend = INTVAL (XEXP (src, 1));
1114 src = XEXP (src, 0);
1115 if (dest == hard_frame_pointer_rtx)
1116 seh_cfa_adjust_cfa (f, seh, pat);
1117 else if (dest == stack_pointer_rtx)
1119 gcc_assert (src == stack_pointer_rtx);
1120 seh_emit_stackalloc (f, seh, addend);
1122 else
1123 gcc_unreachable ();
1124 break;
1126 default:
1127 gcc_unreachable ();
1129 break;
1131 case MEM:
1132 /* A save of some kind. */
1133 dest = XEXP (dest, 0);
1134 if (GET_CODE (dest) == PRE_DEC)
1136 gcc_checking_assert (GET_MODE (src) == Pmode);
1137 gcc_checking_assert (REG_P (src));
1138 seh_emit_push (f, seh, src);
1140 else
1141 seh_cfa_offset (f, seh, pat);
1142 break;
1144 default:
1145 gcc_unreachable ();
1149 /* This function looks at a single insn and emits any SEH directives
1150 required for unwind of this insn. */
1152 void
1153 i386_pe_seh_unwind_emit (FILE *asm_out_file, rtx insn)
1155 rtx note, pat;
1156 bool handled_one = false;
1157 struct seh_frame_state *seh;
1159 if (!TARGET_SEH)
1160 return;
1162 /* We free the SEH data once done with the prologue. Ignore those
1163 RTX_FRAME_RELATED_P insns that are associated with the epilogue. */
1164 seh = cfun->machine->seh;
1165 if (seh == NULL)
1166 return;
1168 if (NOTE_P (insn) || !RTX_FRAME_RELATED_P (insn))
1169 return;
1171 for (note = REG_NOTES (insn); note ; note = XEXP (note, 1))
1173 switch (REG_NOTE_KIND (note))
1175 case REG_FRAME_RELATED_EXPR:
1176 pat = XEXP (note, 0);
1177 goto found;
1179 case REG_CFA_DEF_CFA:
1180 case REG_CFA_EXPRESSION:
1181 /* Only emitted with DRAP, which we disable. */
1182 gcc_unreachable ();
1183 break;
1185 case REG_CFA_REGISTER:
1186 /* Only emitted in epilogues, which we skip. */
1187 gcc_unreachable ();
1189 case REG_CFA_ADJUST_CFA:
1190 pat = XEXP (note, 0);
1191 if (pat == NULL)
1193 pat = PATTERN (insn);
1194 if (GET_CODE (pat) == PARALLEL)
1195 pat = XVECEXP (pat, 0, 0);
1197 seh_cfa_adjust_cfa (asm_out_file, seh, pat);
1198 handled_one = true;
1199 break;
1201 case REG_CFA_OFFSET:
1202 pat = XEXP (note, 0);
1203 if (pat == NULL)
1204 pat = single_set (insn);
1205 seh_cfa_offset (asm_out_file, seh, pat);
1206 handled_one = true;
1207 break;
1209 default:
1210 break;
1213 if (handled_one)
1214 return;
1215 pat = PATTERN (insn);
1216 found:
1217 seh_frame_related_expr (asm_out_file, seh, pat);
1220 void
1221 i386_pe_seh_emit_except_personality (rtx personality)
1223 int flags = 0;
1225 if (!TARGET_SEH)
1226 return;
1228 fputs ("\t.seh_handler\t", asm_out_file);
1229 output_addr_const (asm_out_file, personality);
1231 #if 0
1232 /* ??? The current implementation of _GCC_specific_handler requires
1233 both except and unwind handling, regardless of which sorts the
1234 user-level function requires. */
1235 eh_region r;
1236 FOR_ALL_EH_REGION(r)
1238 if (r->type == ERT_CLEANUP)
1239 flags |= 1;
1240 else
1241 flags |= 2;
1243 #else
1244 flags = 3;
1245 #endif
1247 if (flags & 1)
1248 fputs (", @unwind", asm_out_file);
1249 if (flags & 2)
1250 fputs (", @except", asm_out_file);
1251 fputc ('\n', asm_out_file);
1254 void
1255 i386_pe_seh_init_sections (void)
1257 if (TARGET_SEH)
1258 exception_section = get_unnamed_section (0, output_section_asm_op,
1259 "\t.seh_handlerdata");
1262 void
1263 i386_pe_start_function (FILE *f, const char *name, tree decl)
1265 i386_pe_maybe_record_exported_symbol (decl, name, 0);
1266 if (write_symbols != SDB_DEBUG)
1267 i386_pe_declare_function_type (f, name, TREE_PUBLIC (decl));
1268 /* In case section was altered by debugging output. */
1269 if (decl != NULL_TREE)
1270 switch_to_section (function_section (decl));
1271 ASM_OUTPUT_FUNCTION_LABEL (f, name, decl);
1274 void
1275 i386_pe_end_function (FILE *f, const char *, tree)
1277 i386_pe_seh_fini (f);
1281 #include "gt-winnt.h"